(w32_read_socket): Change calls to gen_help_event.
[bpt/emacs.git] / src / xterm.c
CommitLineData
dc6f92b8 1/* X Communication module for terminals which understand the X protocol.
1c7e22fd 2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000
06a2c219 3 Free Software Foundation, Inc.
dc6f92b8
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
4746118a 9the Free Software Foundation; either version 2, or (at your option)
dc6f92b8
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
dc6f92b8 21
06a2c219 22/* New display code by Gerd Moellmann <gerd@gnu.org>. */
3afe33e7
RS
23/* Xt features made by Fred Pierresteguy. */
24
68c45bf0
PE
25#include <config.h>
26
039440c4 27/* On 4.3 these lose if they come after xterm.h. */
039440c4 28/* Putting these at the beginning seems to be standard for other .c files. */
039440c4
RS
29#include <signal.h>
30
4846819e
RS
31#include <stdio.h>
32
dc6f92b8
JB
33#ifdef HAVE_X_WINDOWS
34
35#include "lisp.h"
9ac0d9e0 36#include "blockinput.h"
dc6f92b8 37
ae79c227
AS
38/* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40#include "syssignal.h"
41
dc6f92b8
JB
42/* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44#include "xterm.h"
f451eb13 45#include <X11/cursorfont.h>
dc6f92b8
JB
46
47/* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49#ifndef makedev
50#include <sys/types.h>
c118dd06 51#endif /* makedev */
dc6f92b8 52
6df54671 53#ifdef BSD_SYSTEM
dc6f92b8 54#include <sys/ioctl.h>
6df54671 55#endif /* ! defined (BSD_SYSTEM) */
dc6f92b8 56
2d368234 57#include "systty.h"
3a2712f9 58#include "systime.h"
dc6f92b8 59
b8009dd1 60#ifndef INCLUDED_FCNTL
dc6f92b8 61#include <fcntl.h>
b8009dd1 62#endif
dc6f92b8
JB
63#include <ctype.h>
64#include <errno.h>
65#include <setjmp.h>
66#include <sys/stat.h>
a0a7635f
RS
67/* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
68/* #include <sys/param.h> */
dc6f92b8 69
dc43ef94 70#include "charset.h"
379b5ac0 71#include "coding.h"
dc43ef94 72#include "ccl.h"
7a13e894 73#include "frame.h"
dc6f92b8 74#include "dispextern.h"
ee569018 75#include "fontset.h"
dc6f92b8
JB
76#include "termhooks.h"
77#include "termopts.h"
78#include "termchar.h"
79#if 0
80#include "sink.h"
81#include "sinkmask.h"
c118dd06 82#endif /* ! 0 */
dc6f92b8 83#include "gnu.h"
dc6f92b8 84#include "disptab.h"
dc6f92b8 85#include "buffer.h"
f451eb13 86#include "window.h"
3b2fa4e6 87#include "keyboard.h"
bde7c500 88#include "intervals.h"
dfcf069d 89#include "process.h"
bffcfca9 90#include "atimer.h"
dc6f92b8 91
d2bd6bc4
RS
92#ifdef USE_X_TOOLKIT
93#include <X11/Shell.h>
94#endif
95
06a2c219
GM
96#ifdef HAVE_SYS_TIME_H
97#include <sys/time.h>
98#endif
99#ifdef HAVE_UNISTD_H
100#include <unistd.h>
101#endif
102
3afe33e7 103#ifdef USE_X_TOOLKIT
06a2c219 104
952291d9
GM
105extern void free_frame_menubar P_ ((struct frame *));
106extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
107 int));
06a2c219 108
0fdff6bb
RS
109#if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
110#define HACK_EDITRES
111extern void _XEditResCheckMessages ();
112#endif /* not NO_EDITRES */
06a2c219
GM
113
114/* Include toolkit specific headers for the scroll bar widget. */
115
116#ifdef USE_TOOLKIT_SCROLL_BARS
117#if defined USE_MOTIF
118#include <Xm/Xm.h> /* for LESSTIF_VERSION */
119#include <Xm/ScrollBar.h>
120#include <Xm/ScrollBarP.h>
ec18280f
SM
121#else /* !USE_MOTIF i.e. use Xaw */
122
123#ifdef HAVE_XAW3D
06a2c219 124#include <X11/Xaw3d/Simple.h>
06a2c219
GM
125#include <X11/Xaw3d/Scrollbar.h>
126#define ARROW_SCROLLBAR
127#include <X11/Xaw3d/ScrollbarP.h>
ec18280f
SM
128#else /* !HAVE_XAW3D */
129#include <X11/Xaw/Simple.h>
130#include <X11/Xaw/Scrollbar.h>
131#endif /* !HAVE_XAW3D */
132#ifndef XtNpickTop
133#define XtNpickTop "pickTop"
134#endif /* !XtNpickTop */
135#endif /* !USE_MOTIF */
06a2c219
GM
136#endif /* USE_TOOLKIT_SCROLL_BARS */
137
3afe33e7
RS
138#endif /* USE_X_TOOLKIT */
139
b849c413
RS
140#ifndef USE_X_TOOLKIT
141#define x_any_window_to_frame x_window_to_frame
5627c40e 142#define x_top_window_to_frame x_window_to_frame
b849c413
RS
143#endif
144
546e6d5b 145#ifdef USE_X_TOOLKIT
d067ea8b 146#include "widget.h"
546e6d5b
RS
147#ifndef XtNinitialState
148#define XtNinitialState "initialState"
149#endif
150#endif
151
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;
7cea38bc 248static Lisp_Object help_echo_window;
be010514
GM
249static Lisp_Object help_echo_object;
250static int help_echo_pos;
06a2c219
GM
251
252/* Temporary variable for XTread_socket. */
253
254static Lisp_Object previous_help_echo;
255
256/* Non-zero means that a HELP_EVENT has been generated since Emacs
257 start. */
258
259static int any_help_event_p;
260
261/* Non-zero means draw block and hollow cursor as wide as the glyph
262 under it. For example, if a block cursor is over a tab, it will be
263 drawn as wide as that tab on the display. */
264
265int x_stretch_cursor_p;
266
267/* This is a chain of structures for all the X displays currently in
268 use. */
269
334208b7 270struct x_display_info *x_display_list;
dc6f92b8 271
06a2c219
GM
272/* This is a list of cons cells, each of the form (NAME
273 . FONT-LIST-CACHE), one for each element of x_display_list and in
274 the same order. NAME is the name of the frame. FONT-LIST-CACHE
275 records previous values returned by x-list-fonts. */
276
7a13e894 277Lisp_Object x_display_name_list;
f451eb13 278
987d2ad1 279/* Frame being updated by update_frame. This is declared in term.c.
06a2c219
GM
280 This is set by update_begin and looked at by all the XT functions.
281 It is zero while not inside an update. In that case, the XT
282 functions assume that `selected_frame' is the frame to apply to. */
283
d0386f2a 284extern struct frame *updating_frame;
dc6f92b8 285
dfcf069d 286extern int waiting_for_input;
0e81d8cd 287
06a2c219
GM
288/* This is a frame waiting to be auto-raised, within XTread_socket. */
289
0134a210
RS
290struct frame *pending_autoraise_frame;
291
7f9c7f94
RS
292#ifdef USE_X_TOOLKIT
293/* The application context for Xt use. */
294XtAppContext Xt_app_con;
06a2c219
GM
295static String Xt_default_resources[] = {0};
296#endif /* USE_X_TOOLKIT */
665881ad 297
06a2c219
GM
298/* Nominal cursor position -- where to draw output.
299 HPOS and VPOS are window relative glyph matrix coordinates.
300 X and Y are window relative pixel coordinates. */
dc6f92b8 301
06a2c219 302struct cursor_pos output_cursor;
dc6f92b8 303
bffcfca9
GM
304/* Non-zero means user is interacting with a toolkit scroll bar. */
305
306static int toolkit_scroll_bar_interaction;
dc6f92b8 307
69388238
RS
308/* Mouse movement.
309
06a2c219 310 Formerly, we used PointerMotionHintMask (in standard_event_mask)
f5bb65ec
RS
311 so that we would have to call XQueryPointer after each MotionNotify
312 event to ask for another such event. However, this made mouse tracking
313 slow, and there was a bug that made it eventually stop.
314
315 Simply asking for MotionNotify all the time seems to work better.
316
69388238
RS
317 In order to avoid asking for motion events and then throwing most
318 of them away or busy-polling the server for mouse positions, we ask
319 the server for pointer motion hints. This means that we get only
320 one event per group of mouse movements. "Groups" are delimited by
321 other kinds of events (focus changes and button clicks, for
322 example), or by XQueryPointer calls; when one of these happens, we
323 get another MotionNotify event the next time the mouse moves. This
324 is at least as efficient as getting motion events when mouse
325 tracking is on, and I suspect only negligibly worse when tracking
f5bb65ec 326 is off. */
69388238
RS
327
328/* Where the mouse was last time we reported a mouse event. */
69388238 329
06a2c219
GM
330FRAME_PTR last_mouse_frame;
331static XRectangle last_mouse_glyph;
2237cac9
RS
332static Lisp_Object last_mouse_press_frame;
333
69388238
RS
334/* The scroll bar in which the last X motion event occurred.
335
06a2c219
GM
336 If the last X motion event occurred in a scroll bar, we set this so
337 XTmouse_position can know whether to report a scroll bar motion or
69388238
RS
338 an ordinary motion.
339
06a2c219
GM
340 If the last X motion event didn't occur in a scroll bar, we set
341 this to Qnil, to tell XTmouse_position to return an ordinary motion
342 event. */
343
69388238
RS
344static Lisp_Object last_mouse_scroll_bar;
345
69388238
RS
346/* This is a hack. We would really prefer that XTmouse_position would
347 return the time associated with the position it returns, but there
06a2c219 348 doesn't seem to be any way to wrest the time-stamp from the server
69388238
RS
349 along with the position query. So, we just keep track of the time
350 of the last movement we received, and return that in hopes that
351 it's somewhat accurate. */
06a2c219 352
69388238
RS
353static Time last_mouse_movement_time;
354
06a2c219
GM
355/* Incremented by XTread_socket whenever it really tries to read
356 events. */
357
c0a04927
RS
358#ifdef __STDC__
359static int volatile input_signal_count;
360#else
361static int input_signal_count;
362#endif
363
7a13e894 364/* Used locally within XTread_socket. */
06a2c219 365
7a13e894 366static int x_noop_count;
dc6f92b8 367
7a13e894 368/* Initial values of argv and argc. */
06a2c219 369
7a13e894
RS
370extern char **initial_argv;
371extern int initial_argc;
dc6f92b8 372
7a13e894 373extern Lisp_Object Vcommand_line_args, Vsystem_name;
dc6f92b8 374
06a2c219 375/* Tells if a window manager is present or not. */
7a13e894
RS
376
377extern Lisp_Object Vx_no_window_manager;
dc6f92b8 378
c2df547c 379extern Lisp_Object Qface, Qmouse_face;
b8009dd1 380
dc6f92b8
JB
381extern int errno;
382
dfeccd2d 383/* A mask of extra modifier bits to put into every keyboard char. */
06a2c219 384
64bb1782
RS
385extern int extra_keyboard_modifiers;
386
59e755be
KH
387static Lisp_Object Qvendor_specific_keysyms;
388
952291d9
GM
389extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
390extern Lisp_Object x_icon_type P_ ((struct frame *));
c32cdd9a 391
7a13e894 392
06a2c219
GM
393/* Enumeration for overriding/changing the face to use for drawing
394 glyphs in x_draw_glyphs. */
395
396enum draw_glyphs_face
397{
398 DRAW_NORMAL_TEXT,
399 DRAW_INVERSE_VIDEO,
400 DRAW_CURSOR,
401 DRAW_MOUSE_FACE,
402 DRAW_IMAGE_RAISED,
403 DRAW_IMAGE_SUNKEN
404};
405
71b8321e 406static void x_update_window_end P_ ((struct window *, int, int));
06a2c219
GM
407static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
408void x_delete_display P_ ((struct x_display_info *));
409static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
410 unsigned));
411static int fast_find_position P_ ((struct window *, int, int *, int *,
412 int *, int *));
413static void set_output_cursor P_ ((struct cursor_pos *));
414static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
415 int *, int *, int *));
416static void note_mode_line_highlight P_ ((struct window *, int, int));
06a2c219 417static void note_mouse_highlight P_ ((struct frame *, int, int));
9ea173e8
GM
418static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
419static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
06a2c219
GM
420static void show_mouse_face P_ ((struct x_display_info *,
421 enum draw_glyphs_face));
422static int x_io_error_quitter P_ ((Display *));
423int x_catch_errors P_ ((Display *));
424void x_uncatch_errors P_ ((Display *, int));
425void x_lower_frame P_ ((struct frame *));
426void x_scroll_bar_clear P_ ((struct frame *));
427int x_had_errors_p P_ ((Display *));
428void x_wm_set_size_hint P_ ((struct frame *, long, int));
429void x_raise_frame P_ ((struct frame *));
430void x_set_window_size P_ ((struct frame *, int, int, int));
431void x_wm_set_window_state P_ ((struct frame *, int));
432void x_wm_set_icon_pixmap P_ ((struct frame *, int));
433void x_initialize P_ ((void));
434static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
435static int x_compute_min_glyph_bounds P_ ((struct frame *));
436static void x_draw_phys_cursor_glyph P_ ((struct window *,
437 struct glyph_row *,
438 enum draw_glyphs_face));
439static void x_update_end P_ ((struct frame *));
440static void XTframe_up_to_date P_ ((struct frame *));
441static void XTreassert_line_highlight P_ ((int, int));
442static void x_change_line_highlight P_ ((int, int, int, int));
443static void XTset_terminal_modes P_ ((void));
444static void XTreset_terminal_modes P_ ((void));
445static void XTcursor_to P_ ((int, int, int, int));
446static void x_write_glyphs P_ ((struct glyph *, int));
447static void x_clear_end_of_line P_ ((int));
448static void x_clear_frame P_ ((void));
449static void x_clear_cursor P_ ((struct window *));
450static void frame_highlight P_ ((struct frame *));
451static void frame_unhighlight P_ ((struct frame *));
452static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
453static void XTframe_rehighlight P_ ((struct frame *));
454static void x_frame_rehighlight P_ ((struct x_display_info *));
455static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
f02d8aa0 456static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
06a2c219
GM
457static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
458 XRectangle *));
459static void expose_frame P_ ((struct frame *, int, int, int, int));
460static void expose_window_tree P_ ((struct window *, XRectangle *));
461static void expose_window P_ ((struct window *, XRectangle *));
462static void expose_area P_ ((struct window *, struct glyph_row *,
463 XRectangle *, enum glyph_row_area));
464static void expose_line P_ ((struct window *, struct glyph_row *,
465 XRectangle *));
466static void x_update_cursor_in_window_tree P_ ((struct window *, int));
467static void x_update_window_cursor P_ ((struct window *, int));
468static void x_erase_phys_cursor P_ ((struct window *));
469void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
470static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
471 enum bitmap_type));
472
473static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
474 GC, int));
475static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
476static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
477static void note_overwritten_text_cursor P_ ((struct window *, int, int));
478static void x_flush P_ ((struct frame *f));
952291d9
GM
479static void x_update_begin P_ ((struct frame *));
480static void x_update_window_begin P_ ((struct window *));
481static void x_draw_vertical_border P_ ((struct window *));
482static void x_after_update_window_line P_ ((struct glyph_row *));
483static INLINE void take_vertical_position_into_account P_ ((struct it *));
484static void x_produce_stretch_glyph P_ ((struct it *));
485
06a2c219
GM
486
487/* Flush display of frame F, or of all frames if F is null. */
488
489static void
490x_flush (f)
491 struct frame *f;
492{
493 BLOCK_INPUT;
494 if (f == NULL)
495 {
496 Lisp_Object rest, frame;
497 FOR_EACH_FRAME (rest, frame)
498 x_flush (XFRAME (frame));
499 }
500 else if (FRAME_X_P (f))
501 XFlush (FRAME_X_DISPLAY (f));
502 UNBLOCK_INPUT;
503}
504
dc6f92b8 505
06a2c219
GM
506/* Remove calls to XFlush by defining XFlush to an empty replacement.
507 Calls to XFlush should be unnecessary because the X output buffer
508 is flushed automatically as needed by calls to XPending,
509 XNextEvent, or XWindowEvent according to the XFlush man page.
510 XTread_socket calls XPending. Removing XFlush improves
511 performance. */
512
513#define XFlush(DISPLAY) (void) 0
b8009dd1 514
334208b7 515\f
06a2c219
GM
516/***********************************************************************
517 Debugging
518 ***********************************************************************/
519
9382638d 520#if 0
06a2c219
GM
521
522/* This is a function useful for recording debugging information about
523 the sequence of occurrences in this file. */
9382638d
KH
524
525struct record
526{
527 char *locus;
528 int type;
529};
530
531struct record event_record[100];
532
533int event_record_index;
534
535record_event (locus, type)
536 char *locus;
537 int type;
538{
539 if (event_record_index == sizeof (event_record) / sizeof (struct record))
540 event_record_index = 0;
541
542 event_record[event_record_index].locus = locus;
543 event_record[event_record_index].type = type;
544 event_record_index++;
545}
546
547#endif /* 0 */
06a2c219
GM
548
549
9382638d 550\f
334208b7
RS
551/* Return the struct x_display_info corresponding to DPY. */
552
553struct x_display_info *
554x_display_info_for_display (dpy)
555 Display *dpy;
556{
557 struct x_display_info *dpyinfo;
558
559 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
560 if (dpyinfo->display == dpy)
561 return dpyinfo;
16bd92ea 562
334208b7
RS
563 return 0;
564}
f451eb13 565
06a2c219
GM
566
567\f
568/***********************************************************************
569 Starting and ending an update
570 ***********************************************************************/
571
572/* Start an update of frame F. This function is installed as a hook
573 for update_begin, i.e. it is called when update_begin is called.
574 This function is called prior to calls to x_update_window_begin for
575 each window being updated. Currently, there is nothing to do here
576 because all interesting stuff is done on a window basis. */
dc6f92b8 577
dfcf069d 578static void
06a2c219 579x_update_begin (f)
f676886a 580 struct frame *f;
58769bee 581{
06a2c219
GM
582 /* Nothing to do. */
583}
dc6f92b8 584
dc6f92b8 585
06a2c219
GM
586/* Start update of window W. Set the global variable updated_window
587 to the window being updated and set output_cursor to the cursor
588 position of W. */
dc6f92b8 589
06a2c219
GM
590static void
591x_update_window_begin (w)
592 struct window *w;
593{
594 struct frame *f = XFRAME (WINDOW_FRAME (w));
595 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
596
597 updated_window = w;
598 set_output_cursor (&w->cursor);
b8009dd1 599
06a2c219 600 BLOCK_INPUT;
d1bc4182 601
06a2c219 602 if (f == display_info->mouse_face_mouse_frame)
b8009dd1 603 {
514e4681 604 /* Don't do highlighting for mouse motion during the update. */
06a2c219 605 display_info->mouse_face_defer = 1;
37c2c98b 606
06a2c219
GM
607 /* If F needs to be redrawn, simply forget about any prior mouse
608 highlighting. */
9f67f20b 609 if (FRAME_GARBAGED_P (f))
06a2c219
GM
610 display_info->mouse_face_window = Qnil;
611
64f26cf5
GM
612#if 0 /* Rows in a current matrix containing glyphs in mouse-face have
613 their mouse_face_p flag set, which means that they are always
614 unequal to rows in a desired matrix which never have that
615 flag set. So, rows containing mouse-face glyphs are never
616 scrolled, and we don't have to switch the mouse highlight off
617 here to prevent it from being scrolled. */
618
06a2c219
GM
619 /* Can we tell that this update does not affect the window
620 where the mouse highlight is? If so, no need to turn off.
621 Likewise, don't do anything if the frame is garbaged;
622 in that case, the frame's current matrix that we would use
623 is all wrong, and we will redisplay that line anyway. */
624 if (!NILP (display_info->mouse_face_window)
625 && w == XWINDOW (display_info->mouse_face_window))
514e4681 626 {
06a2c219 627 int i;
514e4681 628
06a2c219
GM
629 for (i = 0; i < w->desired_matrix->nrows; ++i)
630 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
514e4681
RS
631 break;
632
06a2c219
GM
633 if (i < w->desired_matrix->nrows)
634 clear_mouse_face (display_info);
514e4681 635 }
64f26cf5 636#endif /* 0 */
b8009dd1 637 }
6ccf47d1 638
dc6f92b8
JB
639 UNBLOCK_INPUT;
640}
641
06a2c219
GM
642
643/* Draw a vertical window border to the right of window W if W doesn't
644 have vertical scroll bars. */
645
dfcf069d 646static void
06a2c219
GM
647x_draw_vertical_border (w)
648 struct window *w;
58769bee 649{
06a2c219
GM
650 struct frame *f = XFRAME (WINDOW_FRAME (w));
651
652 /* Redraw borders between horizontally adjacent windows. Don't
653 do it for frames with vertical scroll bars because either the
654 right scroll bar of a window, or the left scroll bar of its
655 neighbor will suffice as a border. */
656 if (!WINDOW_RIGHTMOST_P (w)
657 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
658 {
659 int x0, x1, y0, y1;
dc6f92b8 660
06a2c219 661 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
110859fc 662 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
663 y1 -= 1;
664
665 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
666 f->output_data.x->normal_gc, x1, y0, x1, y1);
667 }
668}
669
670
71b8321e
GM
671/* End update of window W (which is equal to updated_window).
672
673 Draw vertical borders between horizontally adjacent windows, and
674 display W's cursor if CURSOR_ON_P is non-zero.
675
676 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
677 glyphs in mouse-face were overwritten. In that case we have to
678 make sure that the mouse-highlight is properly redrawn.
679
680 W may be a menu bar pseudo-window in case we don't have X toolkit
681 support. Such windows don't have a cursor, so don't display it
682 here. */
06a2c219
GM
683
684static void
71b8321e 685x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
06a2c219 686 struct window *w;
71b8321e 687 int cursor_on_p, mouse_face_overwritten_p;
06a2c219
GM
688{
689 if (!w->pseudo_window_p)
690 {
71b8321e
GM
691 struct x_display_info *dpyinfo
692 = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
693
06a2c219 694 BLOCK_INPUT;
71b8321e
GM
695
696 /* If a row with mouse-face was overwritten, arrange for
697 XTframe_up_to_date to redisplay the mouse highlight. */
698 if (mouse_face_overwritten_p)
699 {
700 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
701 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
702 dpyinfo->mouse_face_window = Qnil;
703 }
704
06a2c219
GM
705 if (cursor_on_p)
706 x_display_and_set_cursor (w, 1, output_cursor.hpos,
707 output_cursor.vpos,
708 output_cursor.x, output_cursor.y);
71b8321e 709
06a2c219
GM
710 x_draw_vertical_border (w);
711 UNBLOCK_INPUT;
712 }
713
714 updated_window = NULL;
715}
dc6f92b8 716
dc6f92b8 717
06a2c219
GM
718/* End update of frame F. This function is installed as a hook in
719 update_end. */
720
721static void
722x_update_end (f)
723 struct frame *f;
724{
725 /* Mouse highlight may be displayed again. */
aa8bff2e 726 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
b8009dd1 727
06a2c219 728 BLOCK_INPUT;
334208b7 729 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
730 UNBLOCK_INPUT;
731}
b8009dd1 732
06a2c219
GM
733
734/* This function is called from various places in xdisp.c whenever a
735 complete update has been performed. The global variable
736 updated_window is not available here. */
b8009dd1 737
dfcf069d 738static void
b8009dd1 739XTframe_up_to_date (f)
06a2c219 740 struct frame *f;
b8009dd1 741{
06a2c219 742 if (FRAME_X_P (f))
514e4681 743 {
06a2c219 744 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
71b8321e 745
06a2c219
GM
746 if (dpyinfo->mouse_face_deferred_gc
747 || f == dpyinfo->mouse_face_mouse_frame)
748 {
749 BLOCK_INPUT;
750 if (dpyinfo->mouse_face_mouse_frame)
751 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
752 dpyinfo->mouse_face_mouse_x,
753 dpyinfo->mouse_face_mouse_y);
754 dpyinfo->mouse_face_deferred_gc = 0;
755 UNBLOCK_INPUT;
756 }
514e4681 757 }
b8009dd1 758}
06a2c219
GM
759
760
761/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
762 arrow bitmaps, or clear the areas where they would be displayed
763 before DESIRED_ROW is made current. The window being updated is
764 found in updated_window. This function It is called from
765 update_window_line only if it is known that there are differences
766 between bitmaps to be drawn between current row and DESIRED_ROW. */
767
768static void
769x_after_update_window_line (desired_row)
770 struct glyph_row *desired_row;
771{
772 struct window *w = updated_window;
773
774 xassert (w);
775
776 if (!desired_row->mode_line_p && !w->pseudo_window_p)
777 {
778 BLOCK_INPUT;
779 x_draw_row_bitmaps (w, desired_row);
780
781 /* When a window has disappeared, make sure that no rest of
782 full-width rows stays visible in the internal border. */
783 if (windows_or_buffers_changed)
784 {
785 struct frame *f = XFRAME (w->frame);
786 int width = FRAME_INTERNAL_BORDER_WIDTH (f);
787 int height = desired_row->visible_height;
110859fc
GM
788 int x = (window_box_right (w, -1)
789 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
06a2c219
GM
790 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
791
792 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
793 x, y, width, height, False);
794 }
795
796 UNBLOCK_INPUT;
797 }
798}
799
800
801/* Draw the bitmap WHICH in one of the areas to the left or right of
802 window W. ROW is the glyph row for which to display the bitmap; it
803 determines the vertical position at which the bitmap has to be
804 drawn. */
805
806static void
807x_draw_bitmap (w, row, which)
808 struct window *w;
809 struct glyph_row *row;
810 enum bitmap_type which;
811{
812 struct frame *f = XFRAME (WINDOW_FRAME (w));
813 Display *display = FRAME_X_DISPLAY (f);
814 Window window = FRAME_X_WINDOW (f);
815 int x, y, wd, h, dy;
816 unsigned char *bits;
817 Pixmap pixmap;
818 GC gc = f->output_data.x->normal_gc;
819 struct face *face;
820 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
821
822 /* Must clip because of partially visible lines. */
823 x_clip_to_row (w, row, gc, 1);
824
825 switch (which)
826 {
827 case LEFT_TRUNCATION_BITMAP:
828 wd = left_width;
829 h = left_height;
830 bits = left_bits;
831 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
832 - wd
110859fc 833 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
834 break;
835
836 case OVERLAY_ARROW_BITMAP:
837 wd = left_width;
838 h = left_height;
839 bits = ov_bits;
840 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
841 - wd
110859fc 842 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
843 break;
844
845 case RIGHT_TRUNCATION_BITMAP:
846 wd = right_width;
847 h = right_height;
848 bits = right_bits;
849 x = window_box_right (w, -1);
110859fc 850 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
851 break;
852
853 case CONTINUED_LINE_BITMAP:
854 wd = right_width;
855 h = right_height;
856 bits = continued_bits;
857 x = window_box_right (w, -1);
110859fc 858 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
859 break;
860
861 case CONTINUATION_LINE_BITMAP:
862 wd = continuation_width;
863 h = continuation_height;
864 bits = continuation_bits;
865 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
866 - wd
110859fc 867 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
868 break;
869
870 case ZV_LINE_BITMAP:
871 wd = zv_width;
872 h = zv_height;
873 bits = zv_bits;
874 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
875 - wd
110859fc 876 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
877 break;
878
879 default:
880 abort ();
881 }
882
883 /* Convert to frame coordinates. Set dy to the offset in the row to
884 start drawing the bitmap. */
885 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
886 dy = (row->height - h) / 2;
887
888 /* Draw the bitmap. I believe these small pixmaps can be cached
889 by the server. */
890 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
891 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
892 face->foreground,
893 face->background, depth);
894 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
895 XFreePixmap (display, pixmap);
896 XSetClipMask (display, gc, None);
897}
898
899
900/* Draw flags bitmaps for glyph row ROW on window W. Call this
901 function with input blocked. */
902
903static void
904x_draw_row_bitmaps (w, row)
905 struct window *w;
906 struct glyph_row *row;
907{
908 struct frame *f = XFRAME (w->frame);
909 enum bitmap_type bitmap;
910 struct face *face;
045dee35 911 int header_line_height = -1;
06a2c219
GM
912
913 xassert (interrupt_input_blocked);
914
915 /* If row is completely invisible, because of vscrolling, we
916 don't have to draw anything. */
917 if (row->visible_height <= 0)
918 return;
919
920 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
921 PREPARE_FACE_FOR_DISPLAY (f, face);
922
923 /* Decide which bitmap to draw at the left side. */
924 if (row->overlay_arrow_p)
925 bitmap = OVERLAY_ARROW_BITMAP;
926 else if (row->truncated_on_left_p)
927 bitmap = LEFT_TRUNCATION_BITMAP;
928 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
929 bitmap = CONTINUATION_LINE_BITMAP;
930 else if (row->indicate_empty_line_p)
931 bitmap = ZV_LINE_BITMAP;
932 else
933 bitmap = NO_BITMAP;
934
935 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
936 the flags area. */
937 if (bitmap == NO_BITMAP
110859fc 938 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
939 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
940 {
941 /* If W has a vertical border to its left, don't draw over it. */
942 int border = ((XFASTINT (w->left) > 0
943 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
944 ? 1 : 0);
945 int left = window_box_left (w, -1);
946
045dee35
GM
947 if (header_line_height < 0)
948 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
949
950 /* In case the same realized face is used for bitmap areas and
951 for something displayed in the text (e.g. face `region' on
952 mono-displays, the fill style may have been changed to
953 FillSolid in x_draw_glyph_string_background. */
954 if (face->stipple)
955 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
956 else
957 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
958
06a2c219
GM
959 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
960 face->gc,
961 (left
110859fc 962 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219 963 + border),
045dee35 964 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 965 row->y)),
110859fc 966 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
06a2c219 967 row->visible_height);
dcd08bfb
GM
968 if (!face->stipple)
969 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
970 }
971
972 /* Draw the left bitmap. */
973 if (bitmap != NO_BITMAP)
974 x_draw_bitmap (w, row, bitmap);
975
976 /* Decide which bitmap to draw at the right side. */
977 if (row->truncated_on_right_p)
978 bitmap = RIGHT_TRUNCATION_BITMAP;
979 else if (row->continued_p)
980 bitmap = CONTINUED_LINE_BITMAP;
981 else
982 bitmap = NO_BITMAP;
983
984 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
985 the flags area. */
986 if (bitmap == NO_BITMAP
110859fc 987 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
988 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
989 {
990 int right = window_box_right (w, -1);
991
045dee35
GM
992 if (header_line_height < 0)
993 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
994
995 /* In case the same realized face is used for bitmap areas and
996 for something displayed in the text (e.g. face `region' on
997 mono-displays, the fill style may have been changed to
998 FillSolid in x_draw_glyph_string_background. */
999 if (face->stipple)
1000 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
1001 else
1002 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
06a2c219
GM
1003 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1004 face->gc,
1005 right,
045dee35 1006 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 1007 row->y)),
110859fc 1008 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
06a2c219 1009 row->visible_height);
dcd08bfb
GM
1010 if (!face->stipple)
1011 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
1012 }
1013
1014 /* Draw the right bitmap. */
1015 if (bitmap != NO_BITMAP)
1016 x_draw_bitmap (w, row, bitmap);
1017}
1018
dc6f92b8 1019\f
06a2c219
GM
1020/***********************************************************************
1021 Line Highlighting
1022 ***********************************************************************/
dc6f92b8 1023
06a2c219
GM
1024/* External interface to control of standout mode. Not used for X
1025 frames. Aborts when called. */
1026
1027static void
dc6f92b8
JB
1028XTreassert_line_highlight (new, vpos)
1029 int new, vpos;
1030{
06a2c219 1031 abort ();
dc6f92b8
JB
1032}
1033
06a2c219
GM
1034
1035/* Call this when about to modify line at position VPOS and change
1036 whether it is highlighted. Not used for X frames. Aborts when
1037 called. */
dc6f92b8 1038
dfcf069d 1039static void
06a2c219
GM
1040x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1041 int new_highlight, vpos, y, first_unused_hpos;
dc6f92b8 1042{
06a2c219 1043 abort ();
dc6f92b8
JB
1044}
1045
06a2c219
GM
1046
1047/* This is called when starting Emacs and when restarting after
1048 suspend. When starting Emacs, no X window is mapped. And nothing
1049 must be done to Emacs's own window if it is suspended (though that
1050 rarely happens). */
dc6f92b8 1051
dfcf069d 1052static void
dc6f92b8
JB
1053XTset_terminal_modes ()
1054{
1055}
1056
06a2c219
GM
1057/* This is called when exiting or suspending Emacs. Exiting will make
1058 the X-windows go away, and suspending requires no action. */
dc6f92b8 1059
dfcf069d 1060static void
dc6f92b8
JB
1061XTreset_terminal_modes ()
1062{
dc6f92b8 1063}
06a2c219
GM
1064
1065
dc6f92b8 1066\f
06a2c219
GM
1067/***********************************************************************
1068 Output Cursor
1069 ***********************************************************************/
1070
1071/* Set the global variable output_cursor to CURSOR. All cursor
1072 positions are relative to updated_window. */
dc6f92b8 1073
dfcf069d 1074static void
06a2c219
GM
1075set_output_cursor (cursor)
1076 struct cursor_pos *cursor;
dc6f92b8 1077{
06a2c219
GM
1078 output_cursor.hpos = cursor->hpos;
1079 output_cursor.vpos = cursor->vpos;
1080 output_cursor.x = cursor->x;
1081 output_cursor.y = cursor->y;
1082}
1083
1084
1085/* Set a nominal cursor position.
dc6f92b8 1086
06a2c219
GM
1087 HPOS and VPOS are column/row positions in a window glyph matrix. X
1088 and Y are window text area relative pixel positions.
1089
1090 If this is done during an update, updated_window will contain the
1091 window that is being updated and the position is the future output
1092 cursor position for that window. If updated_window is null, use
1093 selected_window and display the cursor at the given position. */
1094
1095static void
1096XTcursor_to (vpos, hpos, y, x)
1097 int vpos, hpos, y, x;
1098{
1099 struct window *w;
1100
1101 /* If updated_window is not set, work on selected_window. */
1102 if (updated_window)
1103 w = updated_window;
1104 else
1105 w = XWINDOW (selected_window);
dbcb258a 1106
06a2c219
GM
1107 /* Set the output cursor. */
1108 output_cursor.hpos = hpos;
1109 output_cursor.vpos = vpos;
1110 output_cursor.x = x;
1111 output_cursor.y = y;
dc6f92b8 1112
06a2c219
GM
1113 /* If not called as part of an update, really display the cursor.
1114 This will also set the cursor position of W. */
1115 if (updated_window == NULL)
dc6f92b8
JB
1116 {
1117 BLOCK_INPUT;
06a2c219 1118 x_display_cursor (w, 1, hpos, vpos, x, y);
b86bd3dd 1119 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
dc6f92b8
JB
1120 UNBLOCK_INPUT;
1121 }
1122}
dc43ef94 1123
06a2c219
GM
1124
1125\f
1126/***********************************************************************
1127 Display Iterator
1128 ***********************************************************************/
1129
1130/* Function prototypes of this page. */
1131
1132static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1133 struct glyph *,
ee569018
KH
1134 XChar2b *,
1135 int *));
06a2c219
GM
1136static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1137 int, XChar2b *, int));
1138static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1139static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1140static void x_append_glyph P_ ((struct it *));
b4192550 1141static void x_append_composite_glyph P_ ((struct it *));
06a2c219
GM
1142static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1143 int, int, double));
1144static void x_produce_glyphs P_ ((struct it *));
06a2c219 1145static void x_produce_image_glyph P_ ((struct it *it));
ee569018
KH
1146
1147
1148/* Return a pointer to per-char metric information in FONT of a
1149 character pointed by B which is a pointer to an XChar2b. */
1150
1151#define PER_CHAR_METRIC(font, b) \
1152 ((font)->per_char \
1153 ? ((font)->per_char + (b)->byte2 - (font)->min_char_or_byte2 \
1154 + (((font)->min_byte1 || (font)->max_byte1) \
1155 ? (((b)->byte1 - (font)->min_byte1) \
1156 * ((font)->max_char_or_byte2 - (font)->min_char_or_byte2 + 1)) \
1157 : 0)) \
1158 : &((font)->max_bounds))
dc43ef94 1159
dc6f92b8 1160
e2ef8ee6
GM
1161/* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1162 is not contained in the font. */
dc43ef94 1163
06a2c219 1164static INLINE XCharStruct *
ee569018 1165x_per_char_metric (font, char2b)
06a2c219
GM
1166 XFontStruct *font;
1167 XChar2b *char2b;
1168{
1169 /* The result metric information. */
1170 XCharStruct *pcm = NULL;
dc6f92b8 1171
06a2c219 1172 xassert (font && char2b);
dc6f92b8 1173
06a2c219 1174 if (font->per_char != NULL)
dc6f92b8 1175 {
06a2c219 1176 if (font->min_byte1 == 0 && font->max_byte1 == 0)
dc43ef94 1177 {
06a2c219
GM
1178 /* min_char_or_byte2 specifies the linear character index
1179 corresponding to the first element of the per_char array,
1180 max_char_or_byte2 is the index of the last character. A
1181 character with non-zero CHAR2B->byte1 is not in the font.
1182 A character with byte2 less than min_char_or_byte2 or
1183 greater max_char_or_byte2 is not in the font. */
1184 if (char2b->byte1 == 0
1185 && char2b->byte2 >= font->min_char_or_byte2
1186 && char2b->byte2 <= font->max_char_or_byte2)
1187 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
dc43ef94 1188 }
06a2c219 1189 else
dc6f92b8 1190 {
06a2c219
GM
1191 /* If either min_byte1 or max_byte1 are nonzero, both
1192 min_char_or_byte2 and max_char_or_byte2 are less than
1193 256, and the 2-byte character index values corresponding
1194 to the per_char array element N (counting from 0) are:
1195
1196 byte1 = N/D + min_byte1
1197 byte2 = N\D + min_char_or_byte2
1198
1199 where:
1200
1201 D = max_char_or_byte2 - min_char_or_byte2 + 1
1202 / = integer division
1203 \ = integer modulus */
1204 if (char2b->byte1 >= font->min_byte1
1205 && char2b->byte1 <= font->max_byte1
1206 && char2b->byte2 >= font->min_char_or_byte2
1207 && char2b->byte2 <= font->max_char_or_byte2)
1208 {
1209 pcm = (font->per_char
1210 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1211 * (char2b->byte1 - font->min_byte1))
1212 + (char2b->byte2 - font->min_char_or_byte2));
1213 }
dc6f92b8 1214 }
06a2c219
GM
1215 }
1216 else
1217 {
1218 /* If the per_char pointer is null, all glyphs between the first
1219 and last character indexes inclusive have the same
1220 information, as given by both min_bounds and max_bounds. */
1221 if (char2b->byte2 >= font->min_char_or_byte2
1222 && char2b->byte2 <= font->max_char_or_byte2)
1223 pcm = &font->max_bounds;
1224 }
dc6f92b8 1225
ee569018 1226 return ((pcm == NULL
3e71d8f2 1227 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
ee569018 1228 ? NULL : pcm);
06a2c219 1229}
b73b6aaf 1230
57b03282 1231
06a2c219
GM
1232/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1233 the two-byte form of C. Encoding is returned in *CHAR2B. */
dc43ef94 1234
06a2c219
GM
1235static INLINE void
1236x_encode_char (c, char2b, font_info)
1237 int c;
1238 XChar2b *char2b;
1239 struct font_info *font_info;
1240{
1241 int charset = CHAR_CHARSET (c);
1242 XFontStruct *font = font_info->font;
1243
1244 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1245 This may be either a program in a special encoder language or a
1246 fixed encoding. */
1247 if (font_info->font_encoder)
1248 {
1249 /* It's a program. */
1250 struct ccl_program *ccl = font_info->font_encoder;
1251
1252 if (CHARSET_DIMENSION (charset) == 1)
1253 {
1254 ccl->reg[0] = charset;
1255 ccl->reg[1] = char2b->byte2;
1256 }
1257 else
1258 {
1259 ccl->reg[0] = charset;
1260 ccl->reg[1] = char2b->byte1;
1261 ccl->reg[2] = char2b->byte2;
1262 }
1263
1264 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1265
1266 /* We assume that MSBs are appropriately set/reset by CCL
1267 program. */
1268 if (font->max_byte1 == 0) /* 1-byte font */
ee569018 1269 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
06a2c219
GM
1270 else
1271 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1272 }
1273 else if (font_info->encoding[charset])
1274 {
1275 /* Fixed encoding scheme. See fontset.h for the meaning of the
1276 encoding numbers. */
1277 int enc = font_info->encoding[charset];
1278
1279 if ((enc == 1 || enc == 2)
1280 && CHARSET_DIMENSION (charset) == 2)
1281 char2b->byte1 |= 0x80;
1282
1283 if (enc == 1 || enc == 3)
1284 char2b->byte2 |= 0x80;
1285 }
1286}
1287
1288
1289/* Get face and two-byte form of character C in face FACE_ID on frame
1290 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1291 means we want to display multibyte text. Value is a pointer to a
1292 realized face that is ready for display. */
1293
1294static INLINE struct face *
1295x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1296 struct frame *f;
1297 int c, face_id;
1298 XChar2b *char2b;
1299 int multibyte_p;
1300{
1301 struct face *face = FACE_FROM_ID (f, face_id);
1302
1303 if (!multibyte_p)
1304 {
1305 /* Unibyte case. We don't have to encode, but we have to make
1306 sure to use a face suitable for unibyte. */
1307 char2b->byte1 = 0;
1308 char2b->byte2 = c;
ee569018
KH
1309 face_id = FACE_FOR_CHAR (f, face, c);
1310 face = FACE_FROM_ID (f, face_id);
06a2c219
GM
1311 }
1312 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1313 {
1314 /* Case of ASCII in a face known to fit ASCII. */
1315 char2b->byte1 = 0;
1316 char2b->byte2 = c;
1317 }
1318 else
1319 {
1320 int c1, c2, charset;
1321
1322 /* Split characters into bytes. If c2 is -1 afterwards, C is
1323 really a one-byte character so that byte1 is zero. */
1324 SPLIT_CHAR (c, charset, c1, c2);
1325 if (c2 > 0)
1326 char2b->byte1 = c1, char2b->byte2 = c2;
1327 else
1328 char2b->byte1 = 0, char2b->byte2 = c1;
1329
06a2c219 1330 /* Maybe encode the character in *CHAR2B. */
ee569018 1331 if (face->font != NULL)
06a2c219
GM
1332 {
1333 struct font_info *font_info
1334 = FONT_INFO_FROM_ID (f, face->font_info_id);
1335 if (font_info)
ee569018 1336 x_encode_char (c, char2b, font_info);
06a2c219
GM
1337 }
1338 }
1339
1340 /* Make sure X resources of the face are allocated. */
1341 xassert (face != NULL);
1342 PREPARE_FACE_FOR_DISPLAY (f, face);
1343
1344 return face;
1345}
1346
1347
1348/* Get face and two-byte form of character glyph GLYPH on frame F.
43d120d8 1349 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
06a2c219
GM
1350 a pointer to a realized face that is ready for display. */
1351
1352static INLINE struct face *
ee569018 1353x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
06a2c219
GM
1354 struct frame *f;
1355 struct glyph *glyph;
1356 XChar2b *char2b;
ee569018 1357 int *two_byte_p;
06a2c219
GM
1358{
1359 struct face *face;
1360
1361 xassert (glyph->type == CHAR_GLYPH);
43d120d8 1362 face = FACE_FROM_ID (f, glyph->face_id);
06a2c219 1363
ee569018
KH
1364 if (two_byte_p)
1365 *two_byte_p = 0;
1366
06a2c219
GM
1367 if (!glyph->multibyte_p)
1368 {
1369 /* Unibyte case. We don't have to encode, but we have to make
1370 sure to use a face suitable for unibyte. */
1371 char2b->byte1 = 0;
43d120d8 1372 char2b->byte2 = glyph->u.ch;
06a2c219 1373 }
43d120d8
KH
1374 else if (glyph->u.ch < 128
1375 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
06a2c219
GM
1376 {
1377 /* Case of ASCII in a face known to fit ASCII. */
1378 char2b->byte1 = 0;
43d120d8 1379 char2b->byte2 = glyph->u.ch;
06a2c219
GM
1380 }
1381 else
1382 {
1383 int c1, c2, charset;
1384
1385 /* Split characters into bytes. If c2 is -1 afterwards, C is
1386 really a one-byte character so that byte1 is zero. */
43d120d8 1387 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
06a2c219
GM
1388 if (c2 > 0)
1389 char2b->byte1 = c1, char2b->byte2 = c2;
1390 else
1391 char2b->byte1 = 0, char2b->byte2 = c1;
1392
1393 /* Maybe encode the character in *CHAR2B. */
1394 if (charset != CHARSET_ASCII)
1395 {
1396 struct font_info *font_info
1397 = FONT_INFO_FROM_ID (f, face->font_info_id);
1398 if (font_info)
1399 {
43d120d8 1400 x_encode_char (glyph->u.ch, char2b, font_info);
ee569018
KH
1401 if (two_byte_p)
1402 *two_byte_p
1403 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
06a2c219
GM
1404 }
1405 }
1406 }
1407
1408 /* Make sure X resources of the face are allocated. */
1409 xassert (face != NULL);
1410 PREPARE_FACE_FOR_DISPLAY (f, face);
1411 return face;
1412}
1413
1414
1415/* Store one glyph for IT->char_to_display in IT->glyph_row.
1416 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1417
1418static INLINE void
1419x_append_glyph (it)
1420 struct it *it;
1421{
1422 struct glyph *glyph;
1423 enum glyph_row_area area = it->area;
1424
1425 xassert (it->glyph_row);
1426 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1427
1428 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1429 if (glyph < it->glyph_row->glyphs[area + 1])
1430 {
06a2c219
GM
1431 glyph->charpos = CHARPOS (it->position);
1432 glyph->object = it->object;
88d75730 1433 glyph->pixel_width = it->pixel_width;
06a2c219 1434 glyph->voffset = it->voffset;
88d75730 1435 glyph->type = CHAR_GLYPH;
06a2c219 1436 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1437 glyph->left_box_line_p = it->start_of_box_run_p;
1438 glyph->right_box_line_p = it->end_of_box_run_p;
66ac4b0e
GM
1439 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1440 || it->phys_descent > it->descent);
88d75730 1441 glyph->padding_p = 0;
ee569018 1442 glyph->glyph_not_available_p = it->glyph_not_available_p;
88d75730
GM
1443 glyph->face_id = it->face_id;
1444 glyph->u.ch = it->char_to_display;
06a2c219
GM
1445 ++it->glyph_row->used[area];
1446 }
1447}
1448
b4192550
KH
1449/* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1450 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1451
1452static INLINE void
1453x_append_composite_glyph (it)
1454 struct it *it;
1455{
1456 struct glyph *glyph;
1457 enum glyph_row_area area = it->area;
1458
1459 xassert (it->glyph_row);
1460
1461 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1462 if (glyph < it->glyph_row->glyphs[area + 1])
1463 {
b4192550
KH
1464 glyph->charpos = CHARPOS (it->position);
1465 glyph->object = it->object;
88d75730 1466 glyph->pixel_width = it->pixel_width;
b4192550 1467 glyph->voffset = it->voffset;
88d75730 1468 glyph->type = COMPOSITE_GLYPH;
b4192550 1469 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1470 glyph->left_box_line_p = it->start_of_box_run_p;
1471 glyph->right_box_line_p = it->end_of_box_run_p;
b4192550
KH
1472 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1473 || it->phys_descent > it->descent);
88d75730
GM
1474 glyph->padding_p = 0;
1475 glyph->glyph_not_available_p = 0;
1476 glyph->face_id = it->face_id;
1477 glyph->u.cmp_id = it->cmp_id;
b4192550
KH
1478 ++it->glyph_row->used[area];
1479 }
1480}
1481
06a2c219
GM
1482
1483/* Change IT->ascent and IT->height according to the setting of
1484 IT->voffset. */
1485
1486static INLINE void
1487take_vertical_position_into_account (it)
1488 struct it *it;
1489{
1490 if (it->voffset)
1491 {
1492 if (it->voffset < 0)
1493 /* Increase the ascent so that we can display the text higher
1494 in the line. */
1495 it->ascent += abs (it->voffset);
1496 else
1497 /* Increase the descent so that we can display the text lower
1498 in the line. */
1499 it->descent += it->voffset;
1500 }
1501}
1502
1503
1504/* Produce glyphs/get display metrics for the image IT is loaded with.
1505 See the description of struct display_iterator in dispextern.h for
1506 an overview of struct display_iterator. */
1507
1508static void
1509x_produce_image_glyph (it)
1510 struct it *it;
1511{
1512 struct image *img;
1513 struct face *face;
1514
1515 xassert (it->what == IT_IMAGE);
1516
1517 face = FACE_FROM_ID (it->f, it->face_id);
1518 img = IMAGE_FROM_ID (it->f, it->image_id);
1519 xassert (img);
1520
1521 /* Make sure X resources of the face and image are loaded. */
1522 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1523 prepare_image_for_display (it->f, img);
1524
95af8492 1525 it->ascent = it->phys_ascent = image_ascent (img, face);
66ac4b0e 1526 it->descent = it->phys_descent = img->height + 2 * img->margin - it->ascent;
06a2c219
GM
1527 it->pixel_width = img->width + 2 * img->margin;
1528
1529 it->nglyphs = 1;
1530
1531 if (face->box != FACE_NO_BOX)
1532 {
1533 it->ascent += face->box_line_width;
1534 it->descent += face->box_line_width;
1535
1536 if (it->start_of_box_run_p)
1537 it->pixel_width += face->box_line_width;
1538 if (it->end_of_box_run_p)
1539 it->pixel_width += face->box_line_width;
1540 }
1541
1542 take_vertical_position_into_account (it);
1543
1544 if (it->glyph_row)
1545 {
1546 struct glyph *glyph;
1547 enum glyph_row_area area = it->area;
1548
1549 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1550 if (glyph < it->glyph_row->glyphs[area + 1])
1551 {
06a2c219
GM
1552 glyph->charpos = CHARPOS (it->position);
1553 glyph->object = it->object;
88d75730 1554 glyph->pixel_width = it->pixel_width;
06a2c219 1555 glyph->voffset = it->voffset;
88d75730 1556 glyph->type = IMAGE_GLYPH;
06a2c219 1557 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1558 glyph->left_box_line_p = it->start_of_box_run_p;
1559 glyph->right_box_line_p = it->end_of_box_run_p;
1560 glyph->overlaps_vertically_p = 0;
1561 glyph->padding_p = 0;
1562 glyph->glyph_not_available_p = 0;
1563 glyph->face_id = it->face_id;
1564 glyph->u.img_id = img->id;
06a2c219
GM
1565 ++it->glyph_row->used[area];
1566 }
1567 }
1568}
1569
1570
1571/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1572 of the glyph, WIDTH and HEIGHT are the width and height of the
1573 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1574 ascent of the glyph (0 <= ASCENT <= 1). */
1575
1576static void
1577x_append_stretch_glyph (it, object, width, height, ascent)
1578 struct it *it;
1579 Lisp_Object object;
1580 int width, height;
1581 double ascent;
1582{
1583 struct glyph *glyph;
1584 enum glyph_row_area area = it->area;
1585
1586 xassert (ascent >= 0 && ascent <= 1);
1587
1588 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1589 if (glyph < it->glyph_row->glyphs[area + 1])
1590 {
06a2c219
GM
1591 glyph->charpos = CHARPOS (it->position);
1592 glyph->object = object;
88d75730 1593 glyph->pixel_width = width;
06a2c219 1594 glyph->voffset = it->voffset;
88d75730 1595 glyph->type = STRETCH_GLYPH;
06a2c219 1596 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
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->overlaps_vertically_p = 0;
1600 glyph->padding_p = 0;
1601 glyph->glyph_not_available_p = 0;
1602 glyph->face_id = it->face_id;
1603 glyph->u.stretch.ascent = height * ascent;
1604 glyph->u.stretch.height = height;
06a2c219
GM
1605 ++it->glyph_row->used[area];
1606 }
1607}
1608
1609
1610/* Produce a stretch glyph for iterator IT. IT->object is the value
1611 of the glyph property displayed. The value must be a list
1612 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1613 being recognized:
1614
1615 1. `:width WIDTH' specifies that the space should be WIDTH *
1616 canonical char width wide. WIDTH may be an integer or floating
1617 point number.
1618
1619 2. `:relative-width FACTOR' specifies that the width of the stretch
1620 should be computed from the width of the first character having the
1621 `glyph' property, and should be FACTOR times that width.
1622
1623 3. `:align-to HPOS' specifies that the space should be wide enough
1624 to reach HPOS, a value in canonical character units.
1625
1626 Exactly one of the above pairs must be present.
1627
1628 4. `:height HEIGHT' specifies that the height of the stretch produced
1629 should be HEIGHT, measured in canonical character units.
1630
1631 5. `:relative-height FACTOR' specifies that the height of the the
1632 stretch should be FACTOR times the height of the characters having
1633 the glyph property.
1634
1635 Either none or exactly one of 4 or 5 must be present.
1636
1637 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1638 of the stretch should be used for the ascent of the stretch.
1639 ASCENT must be in the range 0 <= ASCENT <= 100. */
1640
1641#define NUMVAL(X) \
1642 ((INTEGERP (X) || FLOATP (X)) \
1643 ? XFLOATINT (X) \
1644 : - 1)
1645
1646
1647static void
1648x_produce_stretch_glyph (it)
1649 struct it *it;
1650{
1651 /* (space :width WIDTH :height HEIGHT. */
3e71d8f2
GM
1652#if GLYPH_DEBUG
1653 extern Lisp_Object Qspace;
1654#endif
1655 extern Lisp_Object QCwidth, QCheight, QCascent;
06a2c219
GM
1656 extern Lisp_Object QCrelative_width, QCrelative_height;
1657 extern Lisp_Object QCalign_to;
1658 Lisp_Object prop, plist;
1659 double width = 0, height = 0, ascent = 0;
1660 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1661 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1662
1663 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1664
1665 /* List should start with `space'. */
1666 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1667 plist = XCDR (it->object);
1668
1669 /* Compute the width of the stretch. */
1670 if (prop = Fplist_get (plist, QCwidth),
1671 NUMVAL (prop) > 0)
1672 /* Absolute width `:width WIDTH' specified and valid. */
1673 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1674 else if (prop = Fplist_get (plist, QCrelative_width),
1675 NUMVAL (prop) > 0)
1676 {
1677 /* Relative width `:relative-width FACTOR' specified and valid.
1678 Compute the width of the characters having the `glyph'
1679 property. */
1680 struct it it2;
1681 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1682
1683 it2 = *it;
1684 if (it->multibyte_p)
1685 {
1686 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1687 - IT_BYTEPOS (*it));
1688 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1689 }
1690 else
1691 it2.c = *p, it2.len = 1;
1692
1693 it2.glyph_row = NULL;
1694 it2.what = IT_CHARACTER;
1695 x_produce_glyphs (&it2);
1696 width = NUMVAL (prop) * it2.pixel_width;
1697 }
1698 else if (prop = Fplist_get (plist, QCalign_to),
1699 NUMVAL (prop) > 0)
1700 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1701 else
1702 /* Nothing specified -> width defaults to canonical char width. */
1703 width = CANON_X_UNIT (it->f);
1704
1705 /* Compute height. */
1706 if (prop = Fplist_get (plist, QCheight),
1707 NUMVAL (prop) > 0)
1708 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1709 else if (prop = Fplist_get (plist, QCrelative_height),
1710 NUMVAL (prop) > 0)
1711 height = FONT_HEIGHT (font) * NUMVAL (prop);
1712 else
1713 height = FONT_HEIGHT (font);
1714
1715 /* Compute percentage of height used for ascent. If
1716 `:ascent ASCENT' is present and valid, use that. Otherwise,
1717 derive the ascent from the font in use. */
1718 if (prop = Fplist_get (plist, QCascent),
1719 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1720 ascent = NUMVAL (prop) / 100.0;
1721 else
1722 ascent = (double) font->ascent / FONT_HEIGHT (font);
1723
1724 if (width <= 0)
1725 width = 1;
1726 if (height <= 0)
1727 height = 1;
1728
1729 if (it->glyph_row)
1730 {
1731 Lisp_Object object = it->stack[it->sp - 1].string;
1732 if (!STRINGP (object))
1733 object = it->w->buffer;
1734 x_append_stretch_glyph (it, object, width, height, ascent);
1735 }
1736
1737 it->pixel_width = width;
66ac4b0e
GM
1738 it->ascent = it->phys_ascent = height * ascent;
1739 it->descent = it->phys_descent = height - it->ascent;
06a2c219
GM
1740 it->nglyphs = 1;
1741
1742 if (face->box != FACE_NO_BOX)
1743 {
1744 it->ascent += face->box_line_width;
1745 it->descent += face->box_line_width;
1746
1747 if (it->start_of_box_run_p)
1748 it->pixel_width += face->box_line_width;
1749 if (it->end_of_box_run_p)
1750 it->pixel_width += face->box_line_width;
1751 }
1752
1753 take_vertical_position_into_account (it);
1754}
1755
b4192550
KH
1756/* Return proper value to be used as baseline offset of font that has
1757 ASCENT and DESCENT to draw characters by the font at the vertical
1758 center of the line of frame F.
1759
1760 Here, out task is to find the value of BOFF in the following figure;
1761
1762 -------------------------+-----------+-
1763 -+-+---------+-+ | |
1764 | | | | | |
1765 | | | | F_ASCENT F_HEIGHT
1766 | | | ASCENT | |
1767 HEIGHT | | | | |
1768 | | |-|-+------+-----------|------- baseline
1769 | | | | BOFF | |
1770 | |---------|-+-+ | |
1771 | | | DESCENT | |
1772 -+-+---------+-+ F_DESCENT |
1773 -------------------------+-----------+-
1774
1775 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1776 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1777 DESCENT = FONT->descent
1778 HEIGHT = FONT_HEIGHT (FONT)
1779 F_DESCENT = (F->output_data.x->font->descent
1780 - F->output_data.x->baseline_offset)
1781 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1782*/
1783
1784#define VCENTER_BASELINE_OFFSET(FONT, F) \
1785 ((FONT)->descent \
1786 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT))) / 2 \
1787 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
06a2c219
GM
1788
1789/* Produce glyphs/get display metrics for the display element IT is
1790 loaded with. See the description of struct display_iterator in
1791 dispextern.h for an overview of struct display_iterator. */
1792
1793static void
1794x_produce_glyphs (it)
1795 struct it *it;
1796{
ee569018
KH
1797 it->glyph_not_available_p = 0;
1798
06a2c219
GM
1799 if (it->what == IT_CHARACTER)
1800 {
1801 XChar2b char2b;
1802 XFontStruct *font;
ee569018 1803 struct face *face = FACE_FROM_ID (it->f, it->face_id);
06a2c219 1804 XCharStruct *pcm;
06a2c219 1805 int font_not_found_p;
b4192550
KH
1806 struct font_info *font_info;
1807 int boff; /* baseline offset */
06a2c219 1808
ee569018
KH
1809 /* Maybe translate single-byte characters to multibyte, or the
1810 other way. */
06a2c219 1811 it->char_to_display = it->c;
ee569018 1812 if (!ASCII_BYTE_P (it->c))
06a2c219 1813 {
ee569018
KH
1814 if (unibyte_display_via_language_environment
1815 && SINGLE_BYTE_CHAR_P (it->c)
1816 && (it->c >= 0240
1817 || !NILP (Vnonascii_translation_table)))
1818 {
1819 it->char_to_display = unibyte_char_to_multibyte (it->c);
1820 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1821 face = FACE_FROM_ID (it->f, it->face_id);
1822 }
1823 else if (!SINGLE_BYTE_CHAR_P (it->c)
1824 && !it->multibyte_p)
1825 {
1826 it->char_to_display = multibyte_char_to_unibyte (it->c, Qnil);
1827 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1828 face = FACE_FROM_ID (it->f, it->face_id);
1829 }
06a2c219
GM
1830 }
1831
ee569018
KH
1832 /* Get font to use. Encode IT->char_to_display. */
1833 x_get_char_face_and_encoding (it->f, it->char_to_display,
1834 it->face_id, &char2b,
1835 it->multibyte_p);
06a2c219
GM
1836 font = face->font;
1837
1838 /* When no suitable font found, use the default font. */
1839 font_not_found_p = font == NULL;
1840 if (font_not_found_p)
b4192550
KH
1841 {
1842 font = FRAME_FONT (it->f);
1843 boff = it->f->output_data.x->baseline_offset;
1844 font_info = NULL;
1845 }
1846 else
1847 {
1848 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1849 boff = font_info->baseline_offset;
1850 if (font_info->vertical_centering)
1851 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1852 }
06a2c219
GM
1853
1854 if (it->char_to_display >= ' '
1855 && (!it->multibyte_p || it->char_to_display < 128))
1856 {
1857 /* Either unibyte or ASCII. */
1858 int stretched_p;
1859
1860 it->nglyphs = 1;
06a2c219
GM
1861
1862 pcm = x_per_char_metric (font, &char2b);
b4192550
KH
1863 it->ascent = font->ascent + boff;
1864 it->descent = font->descent - boff;
474848ac
GM
1865
1866 if (pcm)
1867 {
1868 it->phys_ascent = pcm->ascent + boff;
1869 it->phys_descent = pcm->descent - boff;
1870 it->pixel_width = pcm->width;
1871 }
1872 else
1873 {
1874 it->glyph_not_available_p = 1;
1875 it->phys_ascent = font->ascent + boff;
1876 it->phys_descent = font->descent - boff;
1877 it->pixel_width = FONT_WIDTH (font);
1878 }
06a2c219
GM
1879
1880 /* If this is a space inside a region of text with
1881 `space-width' property, change its width. */
1882 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1883 if (stretched_p)
1884 it->pixel_width *= XFLOATINT (it->space_width);
1885
1886 /* If face has a box, add the box thickness to the character
1887 height. If character has a box line to the left and/or
1888 right, add the box line width to the character's width. */
1889 if (face->box != FACE_NO_BOX)
1890 {
1891 int thick = face->box_line_width;
1892
1893 it->ascent += thick;
1894 it->descent += thick;
1895
1896 if (it->start_of_box_run_p)
1897 it->pixel_width += thick;
1898 if (it->end_of_box_run_p)
1899 it->pixel_width += thick;
1900 }
1901
1902 /* If face has an overline, add the height of the overline
1903 (1 pixel) and a 1 pixel margin to the character height. */
1904 if (face->overline_p)
1905 it->ascent += 2;
1906
1907 take_vertical_position_into_account (it);
1908
1909 /* If we have to actually produce glyphs, do it. */
1910 if (it->glyph_row)
1911 {
1912 if (stretched_p)
1913 {
1914 /* Translate a space with a `space-width' property
1915 into a stretch glyph. */
1916 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1917 x_append_stretch_glyph (it, it->object, it->pixel_width,
1918 it->ascent + it->descent, ascent);
1919 }
1920 else
1921 x_append_glyph (it);
1922
1923 /* If characters with lbearing or rbearing are displayed
1924 in this line, record that fact in a flag of the
1925 glyph row. This is used to optimize X output code. */
1c7e22fd 1926 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
06a2c219
GM
1927 it->glyph_row->contains_overlapping_glyphs_p = 1;
1928 }
1929 }
1930 else if (it->char_to_display == '\n')
1931 {
1932 /* A newline has no width but we need the height of the line. */
1933 it->pixel_width = 0;
1934 it->nglyphs = 0;
b4192550
KH
1935 it->ascent = it->phys_ascent = font->ascent + boff;
1936 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1937
1938 if (face->box != FACE_NO_BOX)
1939 {
1940 int thick = face->box_line_width;
1941 it->ascent += thick;
1942 it->descent += thick;
1943 }
1944 }
1945 else if (it->char_to_display == '\t')
1946 {
1947 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
d365f5bb 1948 int x = it->current_x + it->continuation_lines_width;
06a2c219
GM
1949 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1950
1951 it->pixel_width = next_tab_x - x;
1952 it->nglyphs = 1;
b4192550
KH
1953 it->ascent = it->phys_ascent = font->ascent + boff;
1954 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1955
1956 if (it->glyph_row)
1957 {
1958 double ascent = (double) it->ascent / (it->ascent + it->descent);
1959 x_append_stretch_glyph (it, it->object, it->pixel_width,
1960 it->ascent + it->descent, ascent);
1961 }
1962 }
1963 else
1964 {
1965 /* A multi-byte character. Assume that the display width of the
1966 character is the width of the character multiplied by the
b4192550 1967 width of the font. */
06a2c219 1968
b4192550
KH
1969 /* If we found a font, this font should give us the right
1970 metrics. If we didn't find a font, use the frame's
1971 default font and calculate the width of the character
1972 from the charset width; this is what old redisplay code
1973 did. */
1974 pcm = x_per_char_metric (font, &char2b);
ee569018
KH
1975 if (font_not_found_p || !pcm)
1976 {
1977 int charset = CHAR_CHARSET (it->char_to_display);
1978
1979 it->glyph_not_available_p = 1;
1980 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
1981 * CHARSET_WIDTH (charset));
1982 it->phys_ascent = font->ascent + boff;
1983 it->phys_descent = font->descent - boff;
1984 }
1985 else
1986 {
1987 it->pixel_width = pcm->width;
1988 it->phys_ascent = pcm->ascent + boff;
1989 it->phys_descent = pcm->descent - boff;
1990 if (it->glyph_row
1991 && (pcm->lbearing < 0
1992 || pcm->rbearing > pcm->width))
1993 it->glyph_row->contains_overlapping_glyphs_p = 1;
1994 }
b4192550
KH
1995 it->nglyphs = 1;
1996 it->ascent = font->ascent + boff;
1997 it->descent = font->descent - boff;
06a2c219
GM
1998 if (face->box != FACE_NO_BOX)
1999 {
2000 int thick = face->box_line_width;
2001 it->ascent += thick;
2002 it->descent += thick;
2003
2004 if (it->start_of_box_run_p)
2005 it->pixel_width += thick;
2006 if (it->end_of_box_run_p)
2007 it->pixel_width += thick;
2008 }
2009
2010 /* If face has an overline, add the height of the overline
2011 (1 pixel) and a 1 pixel margin to the character height. */
2012 if (face->overline_p)
2013 it->ascent += 2;
2014
2015 take_vertical_position_into_account (it);
2016
2017 if (it->glyph_row)
2018 x_append_glyph (it);
2019 }
2020 }
b4192550
KH
2021 else if (it->what == IT_COMPOSITION)
2022 {
2023 /* Note: A composition is represented as one glyph in the
2024 glyph matrix. There are no padding glyphs. */
2025 XChar2b char2b;
2026 XFontStruct *font;
ee569018 2027 struct face *face = FACE_FROM_ID (it->f, it->face_id);
b4192550
KH
2028 XCharStruct *pcm;
2029 int font_not_found_p;
2030 struct font_info *font_info;
2031 int boff; /* baseline offset */
2032 struct composition *cmp = composition_table[it->cmp_id];
2033
2034 /* Maybe translate single-byte characters to multibyte. */
2035 it->char_to_display = it->c;
2036 if (unibyte_display_via_language_environment
2037 && SINGLE_BYTE_CHAR_P (it->c)
2038 && (it->c >= 0240
2039 || (it->c >= 0200
2040 && !NILP (Vnonascii_translation_table))))
2041 {
2042 it->char_to_display = unibyte_char_to_multibyte (it->c);
b4192550
KH
2043 }
2044
2045 /* Get face and font to use. Encode IT->char_to_display. */
ee569018
KH
2046 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2047 face = FACE_FROM_ID (it->f, it->face_id);
2048 x_get_char_face_and_encoding (it->f, it->char_to_display,
2049 it->face_id, &char2b, it->multibyte_p);
b4192550
KH
2050 font = face->font;
2051
2052 /* When no suitable font found, use the default font. */
2053 font_not_found_p = font == NULL;
2054 if (font_not_found_p)
2055 {
2056 font = FRAME_FONT (it->f);
2057 boff = it->f->output_data.x->baseline_offset;
2058 font_info = NULL;
2059 }
2060 else
2061 {
2062 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2063 boff = font_info->baseline_offset;
2064 if (font_info->vertical_centering)
2065 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2066 }
2067
2068 /* There are no padding glyphs, so there is only one glyph to
2069 produce for the composition. Important is that pixel_width,
2070 ascent and descent are the values of what is drawn by
2071 draw_glyphs (i.e. the values of the overall glyphs composed). */
2072 it->nglyphs = 1;
2073
2074 /* If we have not yet calculated pixel size data of glyphs of
2075 the composition for the current face font, calculate them
2076 now. Theoretically, we have to check all fonts for the
2077 glyphs, but that requires much time and memory space. So,
2078 here we check only the font of the first glyph. This leads
2079 to incorrect display very rarely, and C-l (recenter) can
2080 correct the display anyway. */
2081 if (cmp->font != (void *) font)
2082 {
2083 /* Ascent and descent of the font of the first character of
2084 this composition (adjusted by baseline offset). Ascent
2085 and descent of overall glyphs should not be less than
2086 them respectively. */
2087 int font_ascent = font->ascent + boff;
2088 int font_descent = font->descent - boff;
2089 /* Bounding box of the overall glyphs. */
2090 int leftmost, rightmost, lowest, highest;
329bed06 2091 int i, width, ascent, descent;
b4192550
KH
2092
2093 cmp->font = (void *) font;
2094
2095 /* Initialize the bounding box. */
2096 pcm = x_per_char_metric (font, &char2b);
329bed06
GM
2097 if (pcm)
2098 {
2099 width = pcm->width;
2100 ascent = pcm->ascent;
2101 descent = pcm->descent;
2102 }
2103 else
2104 {
2105 width = FONT_WIDTH (font);
2106 ascent = font->ascent;
2107 descent = font->descent;
2108 }
2109
2110 rightmost = width;
2111 lowest = - descent + boff;
2112 highest = ascent + boff;
b4192550 2113 leftmost = 0;
329bed06 2114
b4192550
KH
2115 if (font_info
2116 && font_info->default_ascent
2117 && CHAR_TABLE_P (Vuse_default_ascent)
2118 && !NILP (Faref (Vuse_default_ascent,
2119 make_number (it->char_to_display))))
2120 highest = font_info->default_ascent + boff;
2121
2122 /* Draw the first glyph at the normal position. It may be
2123 shifted to right later if some other glyphs are drawn at
2124 the left. */
2125 cmp->offsets[0] = 0;
2126 cmp->offsets[1] = boff;
2127
2128 /* Set cmp->offsets for the remaining glyphs. */
2129 for (i = 1; i < cmp->glyph_len; i++)
2130 {
2131 int left, right, btm, top;
2132 int ch = COMPOSITION_GLYPH (cmp, i);
ee569018
KH
2133 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2134
2135 face = FACE_FROM_ID (it->f, face_id);
2136 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2137 it->multibyte_p);
b4192550
KH
2138 font = face->font;
2139 if (font == NULL)
2140 {
2141 font = FRAME_FONT (it->f);
2142 boff = it->f->output_data.x->baseline_offset;
2143 font_info = NULL;
2144 }
2145 else
2146 {
2147 font_info
2148 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2149 boff = font_info->baseline_offset;
2150 if (font_info->vertical_centering)
2151 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2152 }
2153
2154 pcm = x_per_char_metric (font, &char2b);
329bed06
GM
2155 if (pcm)
2156 {
2157 width = pcm->width;
2158 ascent = pcm->ascent;
2159 descent = pcm->descent;
2160 }
2161 else
2162 {
2163 width = FONT_WIDTH (font);
2164 ascent = font->ascent;
2165 descent = font->descent;
2166 }
b4192550
KH
2167
2168 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2169 {
2170 /* Relative composition with or without
2171 alternate chars. */
329bed06
GM
2172 left = (leftmost + rightmost - width) / 2;
2173 btm = - descent + boff;
b4192550
KH
2174 if (font_info && font_info->relative_compose
2175 && (! CHAR_TABLE_P (Vignore_relative_composition)
2176 || NILP (Faref (Vignore_relative_composition,
2177 make_number (ch)))))
2178 {
2179
329bed06 2180 if (- descent >= font_info->relative_compose)
b4192550
KH
2181 /* One extra pixel between two glyphs. */
2182 btm = highest + 1;
329bed06 2183 else if (ascent <= 0)
b4192550 2184 /* One extra pixel between two glyphs. */
329bed06 2185 btm = lowest - 1 - ascent - descent;
b4192550
KH
2186 }
2187 }
2188 else
2189 {
2190 /* A composition rule is specified by an integer
2191 value that encodes global and new reference
2192 points (GREF and NREF). GREF and NREF are
2193 specified by numbers as below:
2194
2195 0---1---2 -- ascent
2196 | |
2197 | |
2198 | |
2199 9--10--11 -- center
2200 | |
2201 ---3---4---5--- baseline
2202 | |
2203 6---7---8 -- descent
2204 */
2205 int rule = COMPOSITION_RULE (cmp, i);
2206 int gref, nref, grefx, grefy, nrefx, nrefy;
2207
2208 COMPOSITION_DECODE_RULE (rule, gref, nref);
2209 grefx = gref % 3, nrefx = nref % 3;
2210 grefy = gref / 3, nrefy = nref / 3;
2211
2212 left = (leftmost
2213 + grefx * (rightmost - leftmost) / 2
329bed06 2214 - nrefx * width / 2);
b4192550
KH
2215 btm = ((grefy == 0 ? highest
2216 : grefy == 1 ? 0
2217 : grefy == 2 ? lowest
2218 : (highest + lowest) / 2)
329bed06
GM
2219 - (nrefy == 0 ? ascent + descent
2220 : nrefy == 1 ? descent - boff
b4192550 2221 : nrefy == 2 ? 0
329bed06 2222 : (ascent + descent) / 2));
b4192550
KH
2223 }
2224
2225 cmp->offsets[i * 2] = left;
329bed06 2226 cmp->offsets[i * 2 + 1] = btm + descent;
b4192550
KH
2227
2228 /* Update the bounding box of the overall glyphs. */
329bed06
GM
2229 right = left + width;
2230 top = btm + descent + ascent;
b4192550
KH
2231 if (left < leftmost)
2232 leftmost = left;
2233 if (right > rightmost)
2234 rightmost = right;
2235 if (top > highest)
2236 highest = top;
2237 if (btm < lowest)
2238 lowest = btm;
2239 }
2240
2241 /* If there are glyphs whose x-offsets are negative,
2242 shift all glyphs to the right and make all x-offsets
2243 non-negative. */
2244 if (leftmost < 0)
2245 {
2246 for (i = 0; i < cmp->glyph_len; i++)
2247 cmp->offsets[i * 2] -= leftmost;
2248 rightmost -= leftmost;
2249 }
2250
2251 cmp->pixel_width = rightmost;
2252 cmp->ascent = highest;
2253 cmp->descent = - lowest;
2254 if (cmp->ascent < font_ascent)
2255 cmp->ascent = font_ascent;
2256 if (cmp->descent < font_descent)
2257 cmp->descent = font_descent;
2258 }
2259
2260 it->pixel_width = cmp->pixel_width;
2261 it->ascent = it->phys_ascent = cmp->ascent;
2262 it->descent = it->phys_descent = cmp->descent;
2263
2264 if (face->box != FACE_NO_BOX)
2265 {
2266 int thick = face->box_line_width;
2267 it->ascent += thick;
2268 it->descent += thick;
2269
2270 if (it->start_of_box_run_p)
2271 it->pixel_width += thick;
2272 if (it->end_of_box_run_p)
2273 it->pixel_width += thick;
2274 }
2275
2276 /* If face has an overline, add the height of the overline
2277 (1 pixel) and a 1 pixel margin to the character height. */
2278 if (face->overline_p)
2279 it->ascent += 2;
2280
2281 take_vertical_position_into_account (it);
2282
2283 if (it->glyph_row)
2284 x_append_composite_glyph (it);
2285 }
06a2c219
GM
2286 else if (it->what == IT_IMAGE)
2287 x_produce_image_glyph (it);
2288 else if (it->what == IT_STRETCH)
2289 x_produce_stretch_glyph (it);
2290
3017fdd1
GM
2291 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2292 because this isn't true for images with `:ascent 100'. */
2293 xassert (it->ascent >= 0 && it->descent >= 0);
06a2c219
GM
2294 if (it->area == TEXT_AREA)
2295 it->current_x += it->pixel_width;
66ac4b0e 2296
d365f5bb
GM
2297 it->descent += it->extra_line_spacing;
2298
06a2c219
GM
2299 it->max_ascent = max (it->max_ascent, it->ascent);
2300 it->max_descent = max (it->max_descent, it->descent);
66ac4b0e
GM
2301 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2302 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
06a2c219
GM
2303}
2304
2305
2306/* Estimate the pixel height of the mode or top line on frame F.
2307 FACE_ID specifies what line's height to estimate. */
2308
2309int
2310x_estimate_mode_line_height (f, face_id)
2311 struct frame *f;
2312 enum face_id face_id;
2313{
2314 int height = 1;
2315
2316 /* This function is called so early when Emacs starts that the face
2317 cache and mode line face are not yet initialized. */
2318 if (FRAME_FACE_CACHE (f))
2319 {
2320 struct face *face = FACE_FROM_ID (f, face_id);
2321 if (face)
2322 height = FONT_HEIGHT (face->font) + 2 * face->box_line_width;
2323 }
2324
2325 return height;
2326}
2327
2328\f
2329/***********************************************************************
2330 Glyph display
2331 ***********************************************************************/
2332
2333/* A sequence of glyphs to be drawn in the same face.
2334
2335 This data structure is not really completely X specific, so it
2336 could possibly, at least partially, be useful for other systems. It
2337 is currently not part of the external redisplay interface because
2338 it's not clear what other systems will need. */
2339
2340struct glyph_string
2341{
2342 /* X-origin of the string. */
2343 int x;
2344
2345 /* Y-origin and y-position of the base line of this string. */
2346 int y, ybase;
2347
2348 /* The width of the string, not including a face extension. */
2349 int width;
2350
2351 /* The width of the string, including a face extension. */
2352 int background_width;
2353
2354 /* The height of this string. This is the height of the line this
2355 string is drawn in, and can be different from the height of the
2356 font the string is drawn in. */
2357 int height;
2358
2359 /* Number of pixels this string overwrites in front of its x-origin.
2360 This number is zero if the string has an lbearing >= 0; it is
2361 -lbearing, if the string has an lbearing < 0. */
2362 int left_overhang;
2363
2364 /* Number of pixels this string overwrites past its right-most
2365 nominal x-position, i.e. x + width. Zero if the string's
2366 rbearing is <= its nominal width, rbearing - width otherwise. */
2367 int right_overhang;
2368
2369 /* The frame on which the glyph string is drawn. */
2370 struct frame *f;
2371
2372 /* The window on which the glyph string is drawn. */
2373 struct window *w;
2374
2375 /* X display and window for convenience. */
2376 Display *display;
2377 Window window;
2378
2379 /* The glyph row for which this string was built. It determines the
2380 y-origin and height of the string. */
2381 struct glyph_row *row;
2382
2383 /* The area within row. */
2384 enum glyph_row_area area;
2385
2386 /* Characters to be drawn, and number of characters. */
2387 XChar2b *char2b;
2388 int nchars;
2389
06a2c219
GM
2390 /* A face-override for drawing cursors, mouse face and similar. */
2391 enum draw_glyphs_face hl;
2392
2393 /* Face in which this string is to be drawn. */
2394 struct face *face;
2395
2396 /* Font in which this string is to be drawn. */
2397 XFontStruct *font;
2398
2399 /* Font info for this string. */
2400 struct font_info *font_info;
2401
b4192550
KH
2402 /* Non-null means this string describes (part of) a composition.
2403 All characters from char2b are drawn composed. */
2404 struct composition *cmp;
06a2c219
GM
2405
2406 /* Index of this glyph string's first character in the glyph
b4192550
KH
2407 definition of CMP. If this is zero, this glyph string describes
2408 the first character of a composition. */
06a2c219
GM
2409 int gidx;
2410
2411 /* 1 means this glyph strings face has to be drawn to the right end
2412 of the window's drawing area. */
2413 unsigned extends_to_end_of_line_p : 1;
2414
2415 /* 1 means the background of this string has been drawn. */
2416 unsigned background_filled_p : 1;
2417
2418 /* 1 means glyph string must be drawn with 16-bit functions. */
2419 unsigned two_byte_p : 1;
2420
2421 /* 1 means that the original font determined for drawing this glyph
2422 string could not be loaded. The member `font' has been set to
2423 the frame's default font in this case. */
2424 unsigned font_not_found_p : 1;
2425
2426 /* 1 means that the face in which this glyph string is drawn has a
2427 stipple pattern. */
2428 unsigned stippled_p : 1;
2429
66ac4b0e
GM
2430 /* 1 means only the foreground of this glyph string must be drawn,
2431 and we should use the physical height of the line this glyph
2432 string appears in as clip rect. */
2433 unsigned for_overlaps_p : 1;
2434
06a2c219
GM
2435 /* The GC to use for drawing this glyph string. */
2436 GC gc;
2437
2438 /* A pointer to the first glyph in the string. This glyph
2439 corresponds to char2b[0]. Needed to draw rectangles if
2440 font_not_found_p is 1. */
2441 struct glyph *first_glyph;
2442
2443 /* Image, if any. */
2444 struct image *img;
2445
2446 struct glyph_string *next, *prev;
2447};
2448
2449
5c187dee 2450#if 0
06a2c219
GM
2451
2452static void
2453x_dump_glyph_string (s)
2454 struct glyph_string *s;
2455{
2456 fprintf (stderr, "glyph string\n");
2457 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2458 s->x, s->y, s->width, s->height);
2459 fprintf (stderr, " ybase = %d\n", s->ybase);
2460 fprintf (stderr, " hl = %d\n", s->hl);
2461 fprintf (stderr, " left overhang = %d, right = %d\n",
2462 s->left_overhang, s->right_overhang);
2463 fprintf (stderr, " nchars = %d\n", s->nchars);
2464 fprintf (stderr, " extends to end of line = %d\n",
2465 s->extends_to_end_of_line_p);
2466 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2467 fprintf (stderr, " bg width = %d\n", s->background_width);
2468}
2469
2470#endif /* GLYPH_DEBUG */
2471
2472
2473
2474static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2475 struct glyph_string **,
2476 struct glyph_string *,
2477 struct glyph_string *));
2478static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2479 struct glyph_string **,
2480 struct glyph_string *,
2481 struct glyph_string *));
2482static void x_append_glyph_string P_ ((struct glyph_string **,
2483 struct glyph_string **,
2484 struct glyph_string *));
2485static int x_left_overwritten P_ ((struct glyph_string *));
2486static int x_left_overwriting P_ ((struct glyph_string *));
2487static int x_right_overwritten P_ ((struct glyph_string *));
2488static int x_right_overwriting P_ ((struct glyph_string *));
66ac4b0e
GM
2489static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2490 int));
06a2c219
GM
2491static void x_init_glyph_string P_ ((struct glyph_string *,
2492 XChar2b *, struct window *,
2493 struct glyph_row *,
2494 enum glyph_row_area, int,
2495 enum draw_glyphs_face));
2496static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2497 enum glyph_row_area, int, int,
66ac4b0e 2498 enum draw_glyphs_face, int *, int *, int));
06a2c219
GM
2499static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2500static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2501static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2502 int));
2503static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
b4192550 2504static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
06a2c219
GM
2505static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2506static void x_draw_glyph_string P_ ((struct glyph_string *));
2507static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2508static void x_set_cursor_gc P_ ((struct glyph_string *));
2509static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2510static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2511static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2512 int *, int *));
2513static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2514static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
68c45bf0 2515 unsigned long *, double, int));
06a2c219 2516static void x_setup_relief_color P_ ((struct frame *, struct relief *,
68c45bf0 2517 double, int, unsigned long));
06a2c219
GM
2518static void x_setup_relief_colors P_ ((struct glyph_string *));
2519static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2520static void x_draw_image_relief P_ ((struct glyph_string *));
2521static void x_draw_image_foreground P_ ((struct glyph_string *));
2522static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2523static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2524static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2525 int, int, int));
2526static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2527 int, int, int, int, XRectangle *));
2528static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2529 int, int, int, XRectangle *));
66ac4b0e
GM
2530static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2531 enum glyph_row_area));
209f68d9
GM
2532static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2533 struct glyph_row *,
2534 enum glyph_row_area, int, int));
06a2c219 2535
163dcff3
GM
2536#if GLYPH_DEBUG
2537static void x_check_font P_ ((struct frame *, XFontStruct *));
2538#endif
2539
06a2c219 2540
06a2c219
GM
2541/* Append the list of glyph strings with head H and tail T to the list
2542 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2543
2544static INLINE void
2545x_append_glyph_string_lists (head, tail, h, t)
2546 struct glyph_string **head, **tail;
2547 struct glyph_string *h, *t;
2548{
2549 if (h)
2550 {
2551 if (*head)
2552 (*tail)->next = h;
2553 else
2554 *head = h;
2555 h->prev = *tail;
2556 *tail = t;
2557 }
2558}
2559
2560
2561/* Prepend the list of glyph strings with head H and tail T to the
2562 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2563 result. */
2564
2565static INLINE void
2566x_prepend_glyph_string_lists (head, tail, h, t)
2567 struct glyph_string **head, **tail;
2568 struct glyph_string *h, *t;
2569{
2570 if (h)
2571 {
2572 if (*head)
2573 (*head)->prev = t;
2574 else
2575 *tail = t;
2576 t->next = *head;
2577 *head = h;
2578 }
2579}
2580
2581
2582/* Append glyph string S to the list with head *HEAD and tail *TAIL.
2583 Set *HEAD and *TAIL to the resulting list. */
2584
2585static INLINE void
2586x_append_glyph_string (head, tail, s)
2587 struct glyph_string **head, **tail;
2588 struct glyph_string *s;
2589{
2590 s->next = s->prev = NULL;
2591 x_append_glyph_string_lists (head, tail, s, s);
2592}
2593
2594
2595/* Set S->gc to a suitable GC for drawing glyph string S in cursor
2596 face. */
2597
2598static void
2599x_set_cursor_gc (s)
2600 struct glyph_string *s;
2601{
2602 if (s->font == FRAME_FONT (s->f)
2603 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2604 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
b4192550 2605 && !s->cmp)
06a2c219
GM
2606 s->gc = s->f->output_data.x->cursor_gc;
2607 else
2608 {
2609 /* Cursor on non-default face: must merge. */
2610 XGCValues xgcv;
2611 unsigned long mask;
2612
2613 xgcv.background = s->f->output_data.x->cursor_pixel;
2614 xgcv.foreground = s->face->background;
2615
2616 /* If the glyph would be invisible, try a different foreground. */
2617 if (xgcv.foreground == xgcv.background)
2618 xgcv.foreground = s->face->foreground;
2619 if (xgcv.foreground == xgcv.background)
2620 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2621 if (xgcv.foreground == xgcv.background)
2622 xgcv.foreground = s->face->foreground;
2623
2624 /* Make sure the cursor is distinct from text in this face. */
2625 if (xgcv.background == s->face->background
2626 && xgcv.foreground == s->face->foreground)
2627 {
2628 xgcv.background = s->face->foreground;
2629 xgcv.foreground = s->face->background;
2630 }
2631
2632 IF_DEBUG (x_check_font (s->f, s->font));
2633 xgcv.font = s->font->fid;
2634 xgcv.graphics_exposures = False;
2635 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2636
2637 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2638 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2639 mask, &xgcv);
2640 else
2641 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2642 = XCreateGC (s->display, s->window, mask, &xgcv);
2643
2644 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2645 }
2646}
2647
2648
2649/* Set up S->gc of glyph string S for drawing text in mouse face. */
2650
2651static void
2652x_set_mouse_face_gc (s)
2653 struct glyph_string *s;
2654{
2655 int face_id;
ee569018 2656 struct face *face;
06a2c219
GM
2657
2658 /* What face has to be used for the mouse face? */
2659 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
ee569018 2660 face = FACE_FROM_ID (s->f, face_id);
033e3e18
GM
2661 if (s->first_glyph->type == CHAR_GLYPH)
2662 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2663 else
2664 face_id = FACE_FOR_CHAR (s->f, face, 0);
06a2c219
GM
2665 s->face = FACE_FROM_ID (s->f, face_id);
2666 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2667
2668 /* If font in this face is same as S->font, use it. */
2669 if (s->font == s->face->font)
2670 s->gc = s->face->gc;
2671 else
2672 {
2673 /* Otherwise construct scratch_cursor_gc with values from FACE
2674 but font FONT. */
2675 XGCValues xgcv;
2676 unsigned long mask;
2677
2678 xgcv.background = s->face->background;
2679 xgcv.foreground = s->face->foreground;
2680 IF_DEBUG (x_check_font (s->f, s->font));
2681 xgcv.font = s->font->fid;
2682 xgcv.graphics_exposures = False;
2683 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2684
2685 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2686 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2687 mask, &xgcv);
2688 else
2689 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2690 = XCreateGC (s->display, s->window, mask, &xgcv);
2691
2692 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2693 }
2694
2695 xassert (s->gc != 0);
2696}
2697
2698
2699/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2700 Faces to use in the mode line have already been computed when the
2701 matrix was built, so there isn't much to do, here. */
2702
2703static INLINE void
2704x_set_mode_line_face_gc (s)
2705 struct glyph_string *s;
2706{
2707 s->gc = s->face->gc;
06a2c219
GM
2708}
2709
2710
2711/* Set S->gc of glyph string S for drawing that glyph string. Set
2712 S->stippled_p to a non-zero value if the face of S has a stipple
2713 pattern. */
2714
2715static INLINE void
2716x_set_glyph_string_gc (s)
2717 struct glyph_string *s;
2718{
209f68d9
GM
2719 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2720
06a2c219
GM
2721 if (s->hl == DRAW_NORMAL_TEXT)
2722 {
2723 s->gc = s->face->gc;
2724 s->stippled_p = s->face->stipple != 0;
2725 }
2726 else if (s->hl == DRAW_INVERSE_VIDEO)
2727 {
2728 x_set_mode_line_face_gc (s);
2729 s->stippled_p = s->face->stipple != 0;
2730 }
2731 else if (s->hl == DRAW_CURSOR)
2732 {
2733 x_set_cursor_gc (s);
2734 s->stippled_p = 0;
2735 }
2736 else if (s->hl == DRAW_MOUSE_FACE)
2737 {
2738 x_set_mouse_face_gc (s);
2739 s->stippled_p = s->face->stipple != 0;
2740 }
2741 else if (s->hl == DRAW_IMAGE_RAISED
2742 || s->hl == DRAW_IMAGE_SUNKEN)
2743 {
2744 s->gc = s->face->gc;
2745 s->stippled_p = s->face->stipple != 0;
2746 }
2747 else
2748 {
2749 s->gc = s->face->gc;
2750 s->stippled_p = s->face->stipple != 0;
2751 }
2752
2753 /* GC must have been set. */
2754 xassert (s->gc != 0);
2755}
2756
2757
2758/* Return in *R the clipping rectangle for glyph string S. */
2759
2760static void
2761x_get_glyph_string_clip_rect (s, r)
2762 struct glyph_string *s;
2763 XRectangle *r;
2764{
2765 if (s->row->full_width_p)
2766 {
2767 /* Draw full-width. X coordinates are relative to S->w->left. */
1da3fd71
GM
2768 int canon_x = CANON_X_UNIT (s->f);
2769
2770 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2771 r->width = XFASTINT (s->w->width) * canon_x;
06a2c219
GM
2772
2773 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2774 {
1da3fd71 2775 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
06a2c219
GM
2776 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2777 r->x -= width;
2778 }
2779
b9432a85 2780 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
1da3fd71 2781
06a2c219
GM
2782 /* Unless displaying a mode or menu bar line, which are always
2783 fully visible, clip to the visible part of the row. */
2784 if (s->w->pseudo_window_p)
2785 r->height = s->row->visible_height;
2786 else
2787 r->height = s->height;
2788 }
2789 else
2790 {
2791 /* This is a text line that may be partially visible. */
2792 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2793 r->width = window_box_width (s->w, s->area);
2794 r->height = s->row->visible_height;
2795 }
2796
2797 /* Don't use S->y for clipping because it doesn't take partially
2798 visible lines into account. For example, it can be negative for
2799 partially visible lines at the top of a window. */
2800 if (!s->row->full_width_p
2801 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
045dee35 2802 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
06a2c219
GM
2803 else
2804 r->y = max (0, s->row->y);
06a2c219 2805
9ea173e8 2806 /* If drawing a tool-bar window, draw it over the internal border
06a2c219 2807 at the top of the window. */
9ea173e8 2808 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219 2809 r->y -= s->f->output_data.x->internal_border_width;
66ac4b0e
GM
2810
2811 /* If S draws overlapping rows, it's sufficient to use the top and
2812 bottom of the window for clipping because this glyph string
2813 intentionally draws over other lines. */
2814 if (s->for_overlaps_p)
2815 {
045dee35 2816 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
66ac4b0e
GM
2817 r->height = window_text_bottom_y (s->w) - r->y;
2818 }
2819
2820 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
06a2c219
GM
2821}
2822
2823
2824/* Set clipping for output of glyph string S. S may be part of a mode
2825 line or menu if we don't have X toolkit support. */
2826
2827static INLINE void
2828x_set_glyph_string_clipping (s)
2829 struct glyph_string *s;
2830{
2831 XRectangle r;
2832 x_get_glyph_string_clip_rect (s, &r);
2833 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2834}
2835
2836
2837/* Compute left and right overhang of glyph string S. If S is a glyph
b4192550 2838 string for a composition, assume overhangs don't exist. */
06a2c219
GM
2839
2840static INLINE void
2841x_compute_glyph_string_overhangs (s)
2842 struct glyph_string *s;
2843{
b4192550 2844 if (s->cmp == NULL
06a2c219
GM
2845 && s->first_glyph->type == CHAR_GLYPH)
2846 {
2847 XCharStruct cs;
2848 int direction, font_ascent, font_descent;
2849 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2850 &font_ascent, &font_descent, &cs);
2851 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2852 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2853 }
2854}
2855
2856
2857/* Compute overhangs and x-positions for glyph string S and its
2858 predecessors, or successors. X is the starting x-position for S.
2859 BACKWARD_P non-zero means process predecessors. */
2860
2861static void
2862x_compute_overhangs_and_x (s, x, backward_p)
2863 struct glyph_string *s;
2864 int x;
2865 int backward_p;
2866{
2867 if (backward_p)
2868 {
2869 while (s)
2870 {
2871 x_compute_glyph_string_overhangs (s);
2872 x -= s->width;
2873 s->x = x;
2874 s = s->prev;
2875 }
2876 }
2877 else
2878 {
2879 while (s)
2880 {
2881 x_compute_glyph_string_overhangs (s);
2882 s->x = x;
2883 x += s->width;
2884 s = s->next;
2885 }
2886 }
2887}
2888
2889
2890/* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
b4192550
KH
2891 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2892 assumed to be zero. */
06a2c219
GM
2893
2894static void
2895x_get_glyph_overhangs (glyph, f, left, right)
2896 struct glyph *glyph;
2897 struct frame *f;
2898 int *left, *right;
2899{
06a2c219
GM
2900 *left = *right = 0;
2901
b4192550 2902 if (glyph->type == CHAR_GLYPH)
06a2c219
GM
2903 {
2904 XFontStruct *font;
2905 struct face *face;
2906 struct font_info *font_info;
2907 XChar2b char2b;
ee569018
KH
2908 XCharStruct *pcm;
2909
2910 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
06a2c219
GM
2911 font = face->font;
2912 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
ee569018
KH
2913 if (font
2914 && (pcm = x_per_char_metric (font, &char2b)))
06a2c219 2915 {
06a2c219
GM
2916 if (pcm->rbearing > pcm->width)
2917 *right = pcm->rbearing - pcm->width;
2918 if (pcm->lbearing < 0)
2919 *left = -pcm->lbearing;
2920 }
2921 }
2922}
2923
2924
2925/* Return the index of the first glyph preceding glyph string S that
2926 is overwritten by S because of S's left overhang. Value is -1
2927 if no glyphs are overwritten. */
2928
2929static int
2930x_left_overwritten (s)
2931 struct glyph_string *s;
2932{
2933 int k;
2934
2935 if (s->left_overhang)
2936 {
2937 int x = 0, i;
2938 struct glyph *glyphs = s->row->glyphs[s->area];
2939 int first = s->first_glyph - glyphs;
2940
2941 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2942 x -= glyphs[i].pixel_width;
2943
2944 k = i + 1;
2945 }
2946 else
2947 k = -1;
2948
2949 return k;
2950}
2951
2952
2953/* Return the index of the first glyph preceding glyph string S that
2954 is overwriting S because of its right overhang. Value is -1 if no
2955 glyph in front of S overwrites S. */
2956
2957static int
2958x_left_overwriting (s)
2959 struct glyph_string *s;
2960{
2961 int i, k, x;
2962 struct glyph *glyphs = s->row->glyphs[s->area];
2963 int first = s->first_glyph - glyphs;
2964
2965 k = -1;
2966 x = 0;
2967 for (i = first - 1; i >= 0; --i)
2968 {
2969 int left, right;
2970 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2971 if (x + right > 0)
2972 k = i;
2973 x -= glyphs[i].pixel_width;
2974 }
2975
2976 return k;
2977}
2978
2979
2980/* Return the index of the last glyph following glyph string S that is
2981 not overwritten by S because of S's right overhang. Value is -1 if
2982 no such glyph is found. */
2983
2984static int
2985x_right_overwritten (s)
2986 struct glyph_string *s;
2987{
2988 int k = -1;
2989
2990 if (s->right_overhang)
2991 {
2992 int x = 0, i;
2993 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 2994 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
2995 int end = s->row->used[s->area];
2996
2997 for (i = first; i < end && s->right_overhang > x; ++i)
2998 x += glyphs[i].pixel_width;
2999
3000 k = i;
3001 }
3002
3003 return k;
3004}
3005
3006
3007/* Return the index of the last glyph following glyph string S that
3008 overwrites S because of its left overhang. Value is negative
3009 if no such glyph is found. */
3010
3011static int
3012x_right_overwriting (s)
3013 struct glyph_string *s;
3014{
3015 int i, k, x;
3016 int end = s->row->used[s->area];
3017 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 3018 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
3019
3020 k = -1;
3021 x = 0;
3022 for (i = first; i < end; ++i)
3023 {
3024 int left, right;
3025 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3026 if (x - left < 0)
3027 k = i;
3028 x += glyphs[i].pixel_width;
3029 }
3030
3031 return k;
3032}
3033
3034
3035/* Fill rectangle X, Y, W, H with background color of glyph string S. */
3036
3037static INLINE void
3038x_clear_glyph_string_rect (s, x, y, w, h)
3039 struct glyph_string *s;
3040 int x, y, w, h;
3041{
3042 XGCValues xgcv;
3043 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3044 XSetForeground (s->display, s->gc, xgcv.background);
3045 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3046 XSetForeground (s->display, s->gc, xgcv.foreground);
3047}
3048
3049
3050/* Draw the background of glyph_string S. If S->background_filled_p
3051 is non-zero don't draw it. FORCE_P non-zero means draw the
3052 background even if it wouldn't be drawn normally. This is used
b4192550
KH
3053 when a string preceding S draws into the background of S, or S
3054 contains the first component of a composition. */
06a2c219
GM
3055
3056static void
3057x_draw_glyph_string_background (s, force_p)
3058 struct glyph_string *s;
3059 int force_p;
3060{
3061 /* Nothing to do if background has already been drawn or if it
3062 shouldn't be drawn in the first place. */
3063 if (!s->background_filled_p)
3064 {
b4192550 3065 if (s->stippled_p)
06a2c219
GM
3066 {
3067 /* Fill background with a stipple pattern. */
3068 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3069 XFillRectangle (s->display, s->window, s->gc, s->x,
3070 s->y + s->face->box_line_width,
3071 s->background_width,
3072 s->height - 2 * s->face->box_line_width);
3073 XSetFillStyle (s->display, s->gc, FillSolid);
3074 s->background_filled_p = 1;
3075 }
3076 else if (FONT_HEIGHT (s->font) < s->height - 2 * s->face->box_line_width
3077 || s->font_not_found_p
3078 || s->extends_to_end_of_line_p
06a2c219
GM
3079 || force_p)
3080 {
3081 x_clear_glyph_string_rect (s, s->x, s->y + s->face->box_line_width,
3082 s->background_width,
3083 s->height - 2 * s->face->box_line_width);
3084 s->background_filled_p = 1;
3085 }
3086 }
3087}
3088
3089
3090/* Draw the foreground of glyph string S. */
3091
3092static void
3093x_draw_glyph_string_foreground (s)
3094 struct glyph_string *s;
3095{
3096 int i, x;
3097
3098 /* If first glyph of S has a left box line, start drawing the text
3099 of S to the right of that box line. */
3100 if (s->face->box != FACE_NO_BOX
3101 && s->first_glyph->left_box_line_p)
3102 x = s->x + s->face->box_line_width;
3103 else
3104 x = s->x;
3105
b4192550
KH
3106 /* Draw characters of S as rectangles if S's font could not be
3107 loaded. */
3108 if (s->font_not_found_p)
06a2c219 3109 {
b4192550 3110 for (i = 0; i < s->nchars; ++i)
06a2c219 3111 {
b4192550
KH
3112 struct glyph *g = s->first_glyph + i;
3113 XDrawRectangle (s->display, s->window,
3114 s->gc, x, s->y, g->pixel_width - 1,
3115 s->height - 1);
3116 x += g->pixel_width;
06a2c219
GM
3117 }
3118 }
3119 else
3120 {
b4192550
KH
3121 char *char1b = (char *) s->char2b;
3122 int boff = s->font_info->baseline_offset;
06a2c219 3123
b4192550
KH
3124 if (s->font_info->vertical_centering)
3125 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3126
3127 /* If we can use 8-bit functions, condense S->char2b. */
3128 if (!s->two_byte_p)
3129 for (i = 0; i < s->nchars; ++i)
3130 char1b[i] = s->char2b[i].byte2;
3131
3132 /* Draw text with XDrawString if background has already been
3133 filled. Otherwise, use XDrawImageString. (Note that
3134 XDrawImageString is usually faster than XDrawString.) Always
3135 use XDrawImageString when drawing the cursor so that there is
3136 no chance that characters under a box cursor are invisible. */
3137 if (s->for_overlaps_p
3138 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3139 {
3140 /* Draw characters with 16-bit or 8-bit functions. */
3141 if (s->two_byte_p)
3142 XDrawString16 (s->display, s->window, s->gc, x,
3143 s->ybase - boff, s->char2b, s->nchars);
3144 else
3145 XDrawString (s->display, s->window, s->gc, x,
3146 s->ybase - boff, char1b, s->nchars);
3147 }
06a2c219
GM
3148 else
3149 {
b4192550
KH
3150 if (s->two_byte_p)
3151 XDrawImageString16 (s->display, s->window, s->gc, x,
3152 s->ybase - boff, s->char2b, s->nchars);
06a2c219 3153 else
b4192550
KH
3154 XDrawImageString (s->display, s->window, s->gc, x,
3155 s->ybase - boff, char1b, s->nchars);
3156 }
3157 }
3158}
06a2c219 3159
b4192550 3160/* Draw the foreground of composite glyph string S. */
06a2c219 3161
b4192550
KH
3162static void
3163x_draw_composite_glyph_string_foreground (s)
3164 struct glyph_string *s;
3165{
3166 int i, x;
06a2c219 3167
b4192550
KH
3168 /* If first glyph of S has a left box line, start drawing the text
3169 of S to the right of that box line. */
3170 if (s->face->box != FACE_NO_BOX
3171 && s->first_glyph->left_box_line_p)
3172 x = s->x + s->face->box_line_width;
3173 else
3174 x = s->x;
06a2c219 3175
b4192550
KH
3176 /* S is a glyph string for a composition. S->gidx is the index of
3177 the first character drawn for glyphs of this composition.
3178 S->gidx == 0 means we are drawing the very first character of
3179 this composition. */
06a2c219 3180
b4192550
KH
3181 /* Draw a rectangle for the composition if the font for the very
3182 first character of the composition could not be loaded. */
3183 if (s->font_not_found_p)
3184 {
3185 if (s->gidx == 0)
3186 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3187 s->width - 1, s->height - 1);
3188 }
3189 else
3190 {
3191 for (i = 0; i < s->nchars; i++, ++s->gidx)
3192 XDrawString16 (s->display, s->window, s->gc,
3193 x + s->cmp->offsets[s->gidx * 2],
3194 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3195 s->char2b + i, 1);
06a2c219
GM
3196 }
3197}
3198
3199
80c32bcc
GM
3200#ifdef USE_X_TOOLKIT
3201
3e71d8f2 3202static struct frame *x_frame_of_widget P_ ((Widget));
80c32bcc 3203
3e71d8f2
GM
3204
3205/* Return the frame on which widget WIDGET is used.. Abort if frame
3206 cannot be determined. */
3207
e851c833 3208static struct frame *
3e71d8f2 3209x_frame_of_widget (widget)
80c32bcc 3210 Widget widget;
80c32bcc 3211{
80c32bcc 3212 struct x_display_info *dpyinfo;
5c187dee 3213 Lisp_Object tail;
3e71d8f2
GM
3214 struct frame *f;
3215
80c32bcc
GM
3216 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3217
3218 /* Find the top-level shell of the widget. Note that this function
3219 can be called when the widget is not yet realized, so XtWindow
3220 (widget) == 0. That's the reason we can't simply use
3221 x_any_window_to_frame. */
3222 while (!XtIsTopLevelShell (widget))
3223 widget = XtParent (widget);
3224
3225 /* Look for a frame with that top-level widget. Allocate the color
3226 on that frame to get the right gamma correction value. */
3227 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3228 if (GC_FRAMEP (XCAR (tail))
3229 && (f = XFRAME (XCAR (tail)),
3230 (f->output_data.nothing != 1
3231 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3232 && f->output_data.x->widget == widget)
3e71d8f2 3233 return f;
80c32bcc
GM
3234
3235 abort ();
3236}
3237
3e71d8f2
GM
3238
3239/* Allocate the color COLOR->pixel on the screen and display of
3240 widget WIDGET in colormap CMAP. If an exact match cannot be
3241 allocated, try the nearest color available. Value is non-zero
3242 if successful. This is called from lwlib. */
3243
3244int
3245x_alloc_nearest_color_for_widget (widget, cmap, color)
3246 Widget widget;
3247 Colormap cmap;
3248 XColor *color;
3249{
3250 struct frame *f = x_frame_of_widget (widget);
3251 return x_alloc_nearest_color (f, cmap, color);
3252}
3253
3254
80c32bcc
GM
3255#endif /* USE_X_TOOLKIT */
3256
3257
06a2c219
GM
3258/* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3259 CMAP. If an exact match can't be allocated, try the nearest color
3260 available. Value is non-zero if successful. Set *COLOR to the
3261 color allocated. */
3262
3263int
80c32bcc
GM
3264x_alloc_nearest_color (f, cmap, color)
3265 struct frame *f;
06a2c219
GM
3266 Colormap cmap;
3267 XColor *color;
3268{
80c32bcc
GM
3269 Display *display = FRAME_X_DISPLAY (f);
3270 Screen *screen = FRAME_X_SCREEN (f);
3271 int rc;
3272
3273 gamma_correct (f, color);
3274 rc = XAllocColor (display, cmap, color);
06a2c219
GM
3275 if (rc == 0)
3276 {
3277 /* If we got to this point, the colormap is full, so we're going
3278 to try to get the next closest color. The algorithm used is
3279 a least-squares matching, which is what X uses for closest
3280 color matching with StaticColor visuals. */
3281 int nearest, i;
3282 unsigned long nearest_delta = ~0;
3283 int ncells = XDisplayCells (display, XScreenNumberOfScreen (screen));
3284 XColor *cells = (XColor *) alloca (ncells * sizeof *cells);
3285
3286 for (i = 0; i < ncells; ++i)
3287 cells[i].pixel = i;
3288 XQueryColors (display, cmap, cells, ncells);
3289
3290 for (nearest = i = 0; i < ncells; ++i)
3291 {
3292 long dred = (color->red >> 8) - (cells[i].red >> 8);
3293 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3294 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3295 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3296
3297 if (delta < nearest_delta)
3298 {
3299 nearest = i;
3300 nearest_delta = delta;
3301 }
3302 }
3303
3304 color->red = cells[nearest].red;
3305 color->green = cells[nearest].green;
3306 color->blue = cells[nearest].blue;
3307 rc = XAllocColor (display, cmap, color);
3308 }
3309
d9c545da
GM
3310#ifdef DEBUG_X_COLORS
3311 if (rc)
3312 register_color (color->pixel);
3313#endif /* DEBUG_X_COLORS */
3314
06a2c219
GM
3315 return rc;
3316}
3317
3318
d9c545da
GM
3319/* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3320 It's necessary to do this instead of just using PIXEL directly to
3321 get color reference counts right. */
3322
3323unsigned long
3324x_copy_color (f, pixel)
3325 struct frame *f;
3326 unsigned long pixel;
3327{
3328 XColor color;
3329
3330 color.pixel = pixel;
3331 BLOCK_INPUT;
3332 XQueryColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3333 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3334 UNBLOCK_INPUT;
3335#ifdef DEBUG_X_COLORS
3336 register_color (pixel);
3337#endif
3338 return color.pixel;
3339}
3340
3341
3e71d8f2
GM
3342/* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3343 It's necessary to do this instead of just using PIXEL directly to
3344 get color reference counts right. */
3345
3346unsigned long
3347x_copy_dpy_color (dpy, cmap, pixel)
3348 Display *dpy;
3349 Colormap cmap;
3350 unsigned long pixel;
3351{
3352 XColor color;
3353
3354 color.pixel = pixel;
3355 BLOCK_INPUT;
3356 XQueryColor (dpy, cmap, &color);
3357 XAllocColor (dpy, cmap, &color);
3358 UNBLOCK_INPUT;
3359#ifdef DEBUG_X_COLORS
3360 register_color (pixel);
3361#endif
3362 return color.pixel;
3363}
3364
3365
06a2c219
GM
3366/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3367 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3368 If this produces the same color as PIXEL, try a color where all RGB
3369 values have DELTA added. Return the allocated color in *PIXEL.
3370 DISPLAY is the X display, CMAP is the colormap to operate on.
3371 Value is non-zero if successful. */
3372
3373static int
3374x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3375 struct frame *f;
3376 Display *display;
3377 Colormap cmap;
3378 unsigned long *pixel;
68c45bf0 3379 double factor;
06a2c219
GM
3380 int delta;
3381{
3382 XColor color, new;
3383 int success_p;
3384
3385 /* Get RGB color values. */
3386 color.pixel = *pixel;
3387 XQueryColor (display, cmap, &color);
3388
3389 /* Change RGB values by specified FACTOR. Avoid overflow! */
3390 xassert (factor >= 0);
3391 new.red = min (0xffff, factor * color.red);
3392 new.green = min (0xffff, factor * color.green);
3393 new.blue = min (0xffff, factor * color.blue);
3394
3395 /* Try to allocate the color. */
80c32bcc 3396 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3397 if (success_p)
3398 {
3399 if (new.pixel == *pixel)
3400 {
3401 /* If we end up with the same color as before, try adding
3402 delta to the RGB values. */
0d605c67 3403 x_free_colors (f, &new.pixel, 1);
06a2c219
GM
3404
3405 new.red = min (0xffff, delta + color.red);
3406 new.green = min (0xffff, delta + color.green);
3407 new.blue = min (0xffff, delta + color.blue);
80c32bcc 3408 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3409 }
3410 else
3411 success_p = 1;
3412 *pixel = new.pixel;
3413 }
3414
3415 return success_p;
3416}
3417
3418
3419/* Set up the foreground color for drawing relief lines of glyph
3420 string S. RELIEF is a pointer to a struct relief containing the GC
3421 with which lines will be drawn. Use a color that is FACTOR or
3422 DELTA lighter or darker than the relief's background which is found
3423 in S->f->output_data.x->relief_background. If such a color cannot
3424 be allocated, use DEFAULT_PIXEL, instead. */
3425
3426static void
3427x_setup_relief_color (f, relief, factor, delta, default_pixel)
3428 struct frame *f;
3429 struct relief *relief;
68c45bf0 3430 double factor;
06a2c219
GM
3431 int delta;
3432 unsigned long default_pixel;
3433{
3434 XGCValues xgcv;
3435 struct x_output *di = f->output_data.x;
3436 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3437 unsigned long pixel;
3438 unsigned long background = di->relief_background;
43bd1b2b 3439 Colormap cmap = FRAME_X_COLORMAP (f);
dcd08bfb
GM
3440 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3441 Display *dpy = FRAME_X_DISPLAY (f);
06a2c219
GM
3442
3443 xgcv.graphics_exposures = False;
3444 xgcv.line_width = 1;
3445
3446 /* Free previously allocated color. The color cell will be reused
3447 when it has been freed as many times as it was allocated, so this
3448 doesn't affect faces using the same colors. */
3449 if (relief->gc
3450 && relief->allocated_p)
3451 {
0d605c67 3452 x_free_colors (f, &relief->pixel, 1);
06a2c219
GM
3453 relief->allocated_p = 0;
3454 }
3455
3456 /* Allocate new color. */
3457 xgcv.foreground = default_pixel;
3458 pixel = background;
dcd08bfb
GM
3459 if (dpyinfo->n_planes != 1
3460 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
06a2c219
GM
3461 {
3462 relief->allocated_p = 1;
3463 xgcv.foreground = relief->pixel = pixel;
3464 }
3465
3466 if (relief->gc == 0)
3467 {
dcd08bfb 3468 xgcv.stipple = dpyinfo->gray;
06a2c219 3469 mask |= GCStipple;
dcd08bfb 3470 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
06a2c219
GM
3471 }
3472 else
dcd08bfb 3473 XChangeGC (dpy, relief->gc, mask, &xgcv);
06a2c219
GM
3474}
3475
3476
3477/* Set up colors for the relief lines around glyph string S. */
3478
3479static void
3480x_setup_relief_colors (s)
3481 struct glyph_string *s;
3482{
3483 struct x_output *di = s->f->output_data.x;
3484 unsigned long color;
3485
3486 if (s->face->use_box_color_for_shadows_p)
3487 color = s->face->box_color;
3488 else
3489 {
3490 XGCValues xgcv;
3491
3492 /* Get the background color of the face. */
3493 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3494 color = xgcv.background;
3495 }
3496
3497 if (di->white_relief.gc == 0
3498 || color != di->relief_background)
3499 {
3500 di->relief_background = color;
3501 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3502 WHITE_PIX_DEFAULT (s->f));
3503 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3504 BLACK_PIX_DEFAULT (s->f));
3505 }
3506}
3507
3508
3509/* Draw a relief on frame F inside the rectangle given by LEFT_X,
3510 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3511 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3512 relief. LEFT_P non-zero means draw a relief on the left side of
3513 the rectangle. RIGHT_P non-zero means draw a relief on the right
3514 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3515 when drawing. */
3516
3517static void
3518x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3519 raised_p, left_p, right_p, clip_rect)
3520 struct frame *f;
3521 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3522 XRectangle *clip_rect;
3523{
3524 int i;
3525 GC gc;
3526
3527 if (raised_p)
3528 gc = f->output_data.x->white_relief.gc;
3529 else
3530 gc = f->output_data.x->black_relief.gc;
3531 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3532
3533 /* Top. */
3534 for (i = 0; i < width; ++i)
3535 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3536 left_x + i * left_p, top_y + i,
3537 right_x + 1 - i * right_p, top_y + i);
3538
3539 /* Left. */
3540 if (left_p)
3541 for (i = 0; i < width; ++i)
3542 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3543 left_x + i, top_y + i, left_x + i, bottom_y - i);
3544
3545 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3546 if (raised_p)
3547 gc = f->output_data.x->black_relief.gc;
3548 else
3549 gc = f->output_data.x->white_relief.gc;
3550 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3551
3552 /* Bottom. */
3553 for (i = 0; i < width; ++i)
3554 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3555 left_x + i * left_p, bottom_y - i,
3556 right_x + 1 - i * right_p, bottom_y - i);
3557
3558 /* Right. */
3559 if (right_p)
3560 for (i = 0; i < width; ++i)
3561 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3562 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3563
3564 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3565}
3566
3567
3568/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3569 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3570 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3571 left side of the rectangle. RIGHT_P non-zero means draw a line
3572 on the right side of the rectangle. CLIP_RECT is the clipping
3573 rectangle to use when drawing. */
3574
3575static void
3576x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3577 left_p, right_p, clip_rect)
3578 struct glyph_string *s;
3579 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3580 XRectangle *clip_rect;
3581{
3582 XGCValues xgcv;
3583
3584 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3585 XSetForeground (s->display, s->gc, s->face->box_color);
3586 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3587
3588 /* Top. */
3589 XFillRectangle (s->display, s->window, s->gc,
3590 left_x, top_y, right_x - left_x, width);
3591
3592 /* Left. */
3593 if (left_p)
3594 XFillRectangle (s->display, s->window, s->gc,
3595 left_x, top_y, width, bottom_y - top_y);
3596
3597 /* Bottom. */
3598 XFillRectangle (s->display, s->window, s->gc,
3599 left_x, bottom_y - width, right_x - left_x, width);
3600
3601 /* Right. */
3602 if (right_p)
3603 XFillRectangle (s->display, s->window, s->gc,
3604 right_x - width, top_y, width, bottom_y - top_y);
3605
3606 XSetForeground (s->display, s->gc, xgcv.foreground);
3607 XSetClipMask (s->display, s->gc, None);
3608}
3609
3610
3611/* Draw a box around glyph string S. */
3612
3613static void
3614x_draw_glyph_string_box (s)
3615 struct glyph_string *s;
3616{
3617 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3618 int left_p, right_p;
3619 struct glyph *last_glyph;
3620 XRectangle clip_rect;
3621
3622 last_x = window_box_right (s->w, s->area);
3623 if (s->row->full_width_p
3624 && !s->w->pseudo_window_p)
3625 {
110859fc 3626 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
06a2c219
GM
3627 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3628 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3629 }
3630
3631 /* The glyph that may have a right box line. */
b4192550 3632 last_glyph = (s->cmp || s->img
06a2c219
GM
3633 ? s->first_glyph
3634 : s->first_glyph + s->nchars - 1);
3635
3636 width = s->face->box_line_width;
3637 raised_p = s->face->box == FACE_RAISED_BOX;
3638 left_x = s->x;
3639 right_x = ((s->row->full_width_p
1da3fd71 3640 ? last_x - 1
a7aeb2de 3641 : min (last_x, s->x + s->background_width) - 1));
06a2c219
GM
3642 top_y = s->y;
3643 bottom_y = top_y + s->height - 1;
3644
3645 left_p = (s->first_glyph->left_box_line_p
3646 || (s->hl == DRAW_MOUSE_FACE
3647 && (s->prev == NULL
3648 || s->prev->hl != s->hl)));
3649 right_p = (last_glyph->right_box_line_p
3650 || (s->hl == DRAW_MOUSE_FACE
3651 && (s->next == NULL
3652 || s->next->hl != s->hl)));
3653
3654 x_get_glyph_string_clip_rect (s, &clip_rect);
3655
3656 if (s->face->box == FACE_SIMPLE_BOX)
3657 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3658 left_p, right_p, &clip_rect);
3659 else
3660 {
3661 x_setup_relief_colors (s);
3662 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3663 width, raised_p, left_p, right_p, &clip_rect);
3664 }
3665}
3666
3667
3668/* Draw foreground of image glyph string S. */
3669
3670static void
3671x_draw_image_foreground (s)
3672 struct glyph_string *s;
3673{
3674 int x;
95af8492 3675 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
3676
3677 /* If first glyph of S has a left box line, start drawing it to the
3678 right of that line. */
3679 if (s->face->box != FACE_NO_BOX
3680 && s->first_glyph->left_box_line_p)
3681 x = s->x + s->face->box_line_width;
3682 else
3683 x = s->x;
3684
3685 /* If there is a margin around the image, adjust x- and y-position
3686 by that margin. */
3687 if (s->img->margin)
3688 {
3689 x += s->img->margin;
3690 y += s->img->margin;
3691 }
3692
3693 if (s->img->pixmap)
3694 {
3695 if (s->img->mask)
3696 {
3697 /* We can't set both a clip mask and use XSetClipRectangles
3698 because the latter also sets a clip mask. We also can't
3699 trust on the shape extension to be available
3700 (XShapeCombineRegion). So, compute the rectangle to draw
3701 manually. */
3702 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3703 | GCFunction);
3704 XGCValues xgcv;
3705 XRectangle clip_rect, image_rect, r;
3706
3707 xgcv.clip_mask = s->img->mask;
3708 xgcv.clip_x_origin = x;
3709 xgcv.clip_y_origin = y;
3710 xgcv.function = GXcopy;
3711 XChangeGC (s->display, s->gc, mask, &xgcv);
3712
3713 x_get_glyph_string_clip_rect (s, &clip_rect);
3714 image_rect.x = x;
3715 image_rect.y = y;
3716 image_rect.width = s->img->width;
3717 image_rect.height = s->img->height;
3718 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3719 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3720 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3721 }
3722 else
3723 {
49ad1d99
GM
3724 unsigned long mask = GCClipXOrigin | GCClipYOrigin | GCFunction;
3725 XGCValues xgcv;
3726 XRectangle clip_rect, image_rect, r;
3727
3728 x_get_glyph_string_clip_rect (s, &clip_rect);
3729 image_rect.x = x;
3730 image_rect.y = y;
3731 image_rect.width = s->img->width;
3732 image_rect.height = s->img->height;
3733 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3734 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3735 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
06a2c219
GM
3736
3737 /* When the image has a mask, we can expect that at
3738 least part of a mouse highlight or a block cursor will
3739 be visible. If the image doesn't have a mask, make
3740 a block cursor visible by drawing a rectangle around
3741 the image. I believe it's looking better if we do
3742 nothing here for mouse-face. */
3743 if (s->hl == DRAW_CURSOR)
3744 XDrawRectangle (s->display, s->window, s->gc, x, y,
3745 s->img->width - 1, s->img->height - 1);
3746 }
3747 }
3748 else
3749 /* Draw a rectangle if image could not be loaded. */
3750 XDrawRectangle (s->display, s->window, s->gc, x, y,
3751 s->img->width - 1, s->img->height - 1);
3752}
3753
3754
3755/* Draw a relief around the image glyph string S. */
3756
3757static void
3758x_draw_image_relief (s)
3759 struct glyph_string *s;
3760{
3761 int x0, y0, x1, y1, thick, raised_p;
3762 XRectangle r;
3763 int x;
95af8492 3764 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
3765
3766 /* If first glyph of S has a left box line, start drawing it to the
3767 right of that line. */
3768 if (s->face->box != FACE_NO_BOX
3769 && s->first_glyph->left_box_line_p)
3770 x = s->x + s->face->box_line_width;
3771 else
3772 x = s->x;
3773
3774 /* If there is a margin around the image, adjust x- and y-position
3775 by that margin. */
3776 if (s->img->margin)
3777 {
3778 x += s->img->margin;
3779 y += s->img->margin;
3780 }
3781
3782 if (s->hl == DRAW_IMAGE_SUNKEN
3783 || s->hl == DRAW_IMAGE_RAISED)
3784 {
9ea173e8 3785 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
06a2c219
GM
3786 raised_p = s->hl == DRAW_IMAGE_RAISED;
3787 }
3788 else
3789 {
3790 thick = abs (s->img->relief);
3791 raised_p = s->img->relief > 0;
3792 }
3793
3794 x0 = x - thick;
3795 y0 = y - thick;
3796 x1 = x + s->img->width + thick - 1;
3797 y1 = y + s->img->height + thick - 1;
3798
3799 x_setup_relief_colors (s);
3800 x_get_glyph_string_clip_rect (s, &r);
3801 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3802}
3803
3804
3805/* Draw the foreground of image glyph string S to PIXMAP. */
3806
3807static void
3808x_draw_image_foreground_1 (s, pixmap)
3809 struct glyph_string *s;
3810 Pixmap pixmap;
3811{
3812 int x;
95af8492 3813 int y = s->ybase - s->y - image_ascent (s->img, s->face);
06a2c219
GM
3814
3815 /* If first glyph of S has a left box line, start drawing it to the
3816 right of that line. */
3817 if (s->face->box != FACE_NO_BOX
3818 && s->first_glyph->left_box_line_p)
3819 x = s->face->box_line_width;
3820 else
3821 x = 0;
3822
3823 /* If there is a margin around the image, adjust x- and y-position
3824 by that margin. */
3825 if (s->img->margin)
3826 {
3827 x += s->img->margin;
3828 y += s->img->margin;
3829 }
dc43ef94 3830
06a2c219
GM
3831 if (s->img->pixmap)
3832 {
3833 if (s->img->mask)
3834 {
3835 /* We can't set both a clip mask and use XSetClipRectangles
3836 because the latter also sets a clip mask. We also can't
3837 trust on the shape extension to be available
3838 (XShapeCombineRegion). So, compute the rectangle to draw
3839 manually. */
3840 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3841 | GCFunction);
3842 XGCValues xgcv;
3843
3844 xgcv.clip_mask = s->img->mask;
3845 xgcv.clip_x_origin = x;
3846 xgcv.clip_y_origin = y;
3847 xgcv.function = GXcopy;
3848 XChangeGC (s->display, s->gc, mask, &xgcv);
3849
3850 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3851 0, 0, s->img->width, s->img->height, x, y);
3852 XSetClipMask (s->display, s->gc, None);
3853 }
3854 else
3855 {
3856 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3857 0, 0, s->img->width, s->img->height, x, y);
3858
3859 /* When the image has a mask, we can expect that at
3860 least part of a mouse highlight or a block cursor will
3861 be visible. If the image doesn't have a mask, make
3862 a block cursor visible by drawing a rectangle around
3863 the image. I believe it's looking better if we do
3864 nothing here for mouse-face. */
3865 if (s->hl == DRAW_CURSOR)
3866 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3867 s->img->width - 1, s->img->height - 1);
3868 }
3869 }
3870 else
3871 /* Draw a rectangle if image could not be loaded. */
3872 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3873 s->img->width - 1, s->img->height - 1);
3874}
dc43ef94 3875
990ba854 3876
06a2c219
GM
3877/* Draw part of the background of glyph string S. X, Y, W, and H
3878 give the rectangle to draw. */
a9a5b0a5 3879
06a2c219
GM
3880static void
3881x_draw_glyph_string_bg_rect (s, x, y, w, h)
3882 struct glyph_string *s;
3883 int x, y, w, h;
3884{
3885 if (s->stippled_p)
3886 {
3887 /* Fill background with a stipple pattern. */
3888 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3889 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3890 XSetFillStyle (s->display, s->gc, FillSolid);
3891 }
3892 else
3893 x_clear_glyph_string_rect (s, x, y, w, h);
3894}
07e34cb0 3895
b5210ea7 3896
06a2c219 3897/* Draw image glyph string S.
dc43ef94 3898
06a2c219
GM
3899 s->y
3900 s->x +-------------------------
3901 | s->face->box
3902 |
3903 | +-------------------------
3904 | | s->img->margin
3905 | |
3906 | | +-------------------
3907 | | | the image
dc43ef94 3908
06a2c219 3909 */
dc43ef94 3910
06a2c219
GM
3911static void
3912x_draw_image_glyph_string (s)
3913 struct glyph_string *s;
3914{
3915 int x, y;
3916 int box_line_width = s->face->box_line_width;
3917 int margin = s->img->margin;
3918 int height;
3919 Pixmap pixmap = None;
3920
3921 height = s->height - 2 * box_line_width;
3922
3923 /* Fill background with face under the image. Do it only if row is
3924 taller than image or if image has a clip mask to reduce
3925 flickering. */
3926 s->stippled_p = s->face->stipple != 0;
3927 if (height > s->img->height
3928 || margin
3929 || s->img->mask
3930 || s->img->pixmap == 0
3931 || s->width != s->background_width)
3932 {
3933 if (box_line_width && s->first_glyph->left_box_line_p)
3934 x = s->x + box_line_width;
3935 else
3936 x = s->x;
3937
3938 y = s->y + box_line_width;
3939
3940 if (s->img->mask)
3941 {
3942 /* Create a pixmap as large as the glyph string Fill it with
3943 the background color. Copy the image to it, using its
3944 mask. Copy the temporary pixmap to the display. */
3945 Screen *screen = FRAME_X_SCREEN (s->f);
3946 int depth = DefaultDepthOfScreen (screen);
3947
3948 /* Create a pixmap as large as the glyph string. */
3949 pixmap = XCreatePixmap (s->display, s->window,
3950 s->background_width,
3951 s->height, depth);
3952
3953 /* Don't clip in the following because we're working on the
3954 pixmap. */
3955 XSetClipMask (s->display, s->gc, None);
3956
3957 /* Fill the pixmap with the background color/stipple. */
3958 if (s->stippled_p)
3959 {
3960 /* Fill background with a stipple pattern. */
3961 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3962 XFillRectangle (s->display, pixmap, s->gc,
3963 0, 0, s->background_width, s->height);
3964 XSetFillStyle (s->display, s->gc, FillSolid);
3965 }
3966 else
3967 {
3968 XGCValues xgcv;
3969 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3970 &xgcv);
3971 XSetForeground (s->display, s->gc, xgcv.background);
3972 XFillRectangle (s->display, pixmap, s->gc,
3973 0, 0, s->background_width, s->height);
3974 XSetForeground (s->display, s->gc, xgcv.foreground);
3975 }
3976 }
3977 else
3978 /* Implementation idea: Is it possible to construct a mask?
3979 We could look at the color at the margins of the image, and
3980 say that this color is probably the background color of the
3981 image. */
3982 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
3983
3984 s->background_filled_p = 1;
3985 }
dc43ef94 3986
06a2c219
GM
3987 /* Draw the foreground. */
3988 if (pixmap != None)
3989 {
3990 x_draw_image_foreground_1 (s, pixmap);
3991 x_set_glyph_string_clipping (s);
3992 XCopyArea (s->display, pixmap, s->window, s->gc,
3993 0, 0, s->background_width, s->height, s->x, s->y);
3994 XFreePixmap (s->display, pixmap);
3995 }
3996 else
3997 x_draw_image_foreground (s);
b5210ea7 3998
06a2c219
GM
3999 /* If we must draw a relief around the image, do it. */
4000 if (s->img->relief
4001 || s->hl == DRAW_IMAGE_RAISED
4002 || s->hl == DRAW_IMAGE_SUNKEN)
4003 x_draw_image_relief (s);
4004}
8c1a6a84 4005
990ba854 4006
06a2c219 4007/* Draw stretch glyph string S. */
dc43ef94 4008
06a2c219
GM
4009static void
4010x_draw_stretch_glyph_string (s)
4011 struct glyph_string *s;
4012{
4013 xassert (s->first_glyph->type == STRETCH_GLYPH);
4014 s->stippled_p = s->face->stipple != 0;
990ba854 4015
06a2c219
GM
4016 if (s->hl == DRAW_CURSOR
4017 && !x_stretch_cursor_p)
4018 {
4019 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4020 as wide as the stretch glyph. */
4021 int width = min (CANON_X_UNIT (s->f), s->background_width);
990ba854 4022
06a2c219
GM
4023 /* Draw cursor. */
4024 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
0cdd0c9f 4025
06a2c219
GM
4026 /* Clear rest using the GC of the original non-cursor face. */
4027 if (width < s->background_width)
4028 {
4029 GC gc = s->face->gc;
4030 int x = s->x + width, y = s->y;
4031 int w = s->background_width - width, h = s->height;
4032 XRectangle r;
dc43ef94 4033
06a2c219
GM
4034 x_get_glyph_string_clip_rect (s, &r);
4035 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
97210f4e 4036
06a2c219
GM
4037 if (s->face->stipple)
4038 {
4039 /* Fill background with a stipple pattern. */
4040 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4041 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4042 XSetFillStyle (s->display, gc, FillSolid);
4043 }
4044 else
4045 {
4046 XGCValues xgcv;
4047 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4048 XSetForeground (s->display, gc, xgcv.background);
4049 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4050 XSetForeground (s->display, gc, xgcv.foreground);
4051 }
4052 }
4053 }
4054 else
4055 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4056 s->height);
4057
4058 s->background_filled_p = 1;
4059}
4060
4061
4062/* Draw glyph string S. */
4063
4064static void
4065x_draw_glyph_string (s)
4066 struct glyph_string *s;
4067{
4068 /* If S draws into the background of its successor, draw the
4069 background of the successor first so that S can draw into it.
4070 This makes S->next use XDrawString instead of XDrawImageString. */
66ac4b0e 4071 if (s->next && s->right_overhang && !s->for_overlaps_p)
06a2c219
GM
4072 {
4073 xassert (s->next->img == NULL);
4074 x_set_glyph_string_gc (s->next);
4075 x_set_glyph_string_clipping (s->next);
4076 x_draw_glyph_string_background (s->next, 1);
4077 }
97210f4e 4078
06a2c219
GM
4079 /* Set up S->gc, set clipping and draw S. */
4080 x_set_glyph_string_gc (s);
4081 x_set_glyph_string_clipping (s);
4082
4083 switch (s->first_glyph->type)
4084 {
4085 case IMAGE_GLYPH:
4086 x_draw_image_glyph_string (s);
4087 break;
4088
4089 case STRETCH_GLYPH:
4090 x_draw_stretch_glyph_string (s);
4091 break;
4092
4093 case CHAR_GLYPH:
66ac4b0e
GM
4094 if (s->for_overlaps_p)
4095 s->background_filled_p = 1;
4096 else
4097 x_draw_glyph_string_background (s, 0);
06a2c219
GM
4098 x_draw_glyph_string_foreground (s);
4099 break;
4100
b4192550
KH
4101 case COMPOSITE_GLYPH:
4102 if (s->for_overlaps_p || s->gidx > 0)
4103 s->background_filled_p = 1;
4104 else
4105 x_draw_glyph_string_background (s, 1);
4106 x_draw_composite_glyph_string_foreground (s);
4107 break;
4108
06a2c219
GM
4109 default:
4110 abort ();
4111 }
4112
66ac4b0e 4113 if (!s->for_overlaps_p)
06a2c219 4114 {
66ac4b0e
GM
4115 /* Draw underline. */
4116 if (s->face->underline_p)
4117 {
4118 unsigned long dy, h;
06a2c219 4119
66ac4b0e
GM
4120 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4121 h = 1;
4122 if (!XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &dy))
4123 dy = s->height - h;
06a2c219 4124
66ac4b0e
GM
4125 if (s->face->underline_defaulted_p)
4126 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4127 s->width, h);
4128 else
4129 {
4130 XGCValues xgcv;
4131 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4132 XSetForeground (s->display, s->gc, s->face->underline_color);
4133 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4134 s->width, h);
4135 XSetForeground (s->display, s->gc, xgcv.foreground);
4136 }
dc6f92b8 4137 }
07e34cb0 4138
66ac4b0e
GM
4139 /* Draw overline. */
4140 if (s->face->overline_p)
06a2c219 4141 {
66ac4b0e
GM
4142 unsigned long dy = 0, h = 1;
4143
4144 if (s->face->overline_color_defaulted_p)
4145 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4146 s->width, h);
4147 else
4148 {
4149 XGCValues xgcv;
4150 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4151 XSetForeground (s->display, s->gc, s->face->overline_color);
4152 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4153 s->width, h);
4154 XSetForeground (s->display, s->gc, xgcv.foreground);
4155 }
06a2c219 4156 }
06a2c219 4157
66ac4b0e
GM
4158 /* Draw strike-through. */
4159 if (s->face->strike_through_p)
06a2c219 4160 {
66ac4b0e
GM
4161 unsigned long h = 1;
4162 unsigned long dy = (s->height - h) / 2;
4163
4164 if (s->face->strike_through_color_defaulted_p)
4165 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4166 s->width, h);
4167 else
4168 {
4169 XGCValues xgcv;
4170 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4171 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4172 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4173 s->width, h);
4174 XSetForeground (s->display, s->gc, xgcv.foreground);
4175 }
06a2c219 4176 }
06a2c219 4177
66ac4b0e
GM
4178 /* Draw relief. */
4179 if (s->face->box != FACE_NO_BOX)
4180 x_draw_glyph_string_box (s);
4181 }
06a2c219
GM
4182
4183 /* Reset clipping. */
4184 XSetClipMask (s->display, s->gc, None);
dc6f92b8 4185}
07e34cb0 4186
06a2c219 4187
b4192550
KH
4188static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4189 struct face **, int));
06a2c219 4190
06a2c219 4191
209f68d9
GM
4192/* Fill glyph string S with composition components specified by S->cmp.
4193
b4192550
KH
4194 FACES is an array of faces for all components of this composition.
4195 S->gidx is the index of the first component for S.
4196 OVERLAPS_P non-zero means S should draw the foreground only, and
209f68d9 4197 use its physical height for clipping.
06a2c219 4198
b4192550 4199 Value is the index of a component not in S. */
07e34cb0 4200
b4192550
KH
4201static int
4202x_fill_composite_glyph_string (s, faces, overlaps_p)
06a2c219 4203 struct glyph_string *s;
b4192550 4204 struct face **faces;
66ac4b0e 4205 int overlaps_p;
07e34cb0 4206{
b4192550 4207 int i;
06a2c219 4208
b4192550 4209 xassert (s);
06a2c219 4210
b4192550 4211 s->for_overlaps_p = overlaps_p;
06a2c219 4212
b4192550
KH
4213 s->face = faces[s->gidx];
4214 s->font = s->face->font;
4215 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
06a2c219 4216
b4192550
KH
4217 /* For all glyphs of this composition, starting at the offset
4218 S->gidx, until we reach the end of the definition or encounter a
4219 glyph that requires the different face, add it to S. */
4220 ++s->nchars;
4221 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4222 ++s->nchars;
06a2c219 4223
b4192550
KH
4224 /* All glyph strings for the same composition has the same width,
4225 i.e. the width set for the first component of the composition. */
06a2c219 4226
06a2c219
GM
4227 s->width = s->first_glyph->pixel_width;
4228
4229 /* If the specified font could not be loaded, use the frame's
4230 default font, but record the fact that we couldn't load it in
4231 the glyph string so that we can draw rectangles for the
4232 characters of the glyph string. */
4233 if (s->font == NULL)
4234 {
4235 s->font_not_found_p = 1;
4236 s->font = FRAME_FONT (s->f);
4237 }
4238
4239 /* Adjust base line for subscript/superscript text. */
4240 s->ybase += s->first_glyph->voffset;
4241
4242 xassert (s->face && s->face->gc);
4243
4244 /* This glyph string must always be drawn with 16-bit functions. */
4245 s->two_byte_p = 1;
b4192550
KH
4246
4247 return s->gidx + s->nchars;
06a2c219
GM
4248}
4249
4250
209f68d9
GM
4251/* Fill glyph string S from a sequence of character glyphs.
4252
06a2c219 4253 FACE_ID is the face id of the string. START is the index of the
66ac4b0e
GM
4254 first glyph to consider, END is the index of the last + 1.
4255 OVERLAPS_P non-zero means S should draw the foreground only, and
209f68d9 4256 use its physical height for clipping.
66ac4b0e
GM
4257
4258 Value is the index of the first glyph not in S. */
06a2c219
GM
4259
4260static int
66ac4b0e 4261x_fill_glyph_string (s, face_id, start, end, overlaps_p)
06a2c219
GM
4262 struct glyph_string *s;
4263 int face_id;
66ac4b0e 4264 int start, end, overlaps_p;
06a2c219
GM
4265{
4266 struct glyph *glyph, *last;
4267 int voffset;
ee569018 4268 int glyph_not_available_p;
06a2c219 4269
06a2c219
GM
4270 xassert (s->f == XFRAME (s->w->frame));
4271 xassert (s->nchars == 0);
4272 xassert (start >= 0 && end > start);
4273
66ac4b0e 4274 s->for_overlaps_p = overlaps_p,
06a2c219
GM
4275 glyph = s->row->glyphs[s->area] + start;
4276 last = s->row->glyphs[s->area] + end;
4277 voffset = glyph->voffset;
4278
ee569018
KH
4279 glyph_not_available_p = glyph->glyph_not_available_p;
4280
06a2c219
GM
4281 while (glyph < last
4282 && glyph->type == CHAR_GLYPH
4283 && glyph->voffset == voffset
ee569018
KH
4284 /* Same face id implies same font, nowadays. */
4285 && glyph->face_id == face_id
4286 && glyph->glyph_not_available_p == glyph_not_available_p)
06a2c219 4287 {
ee569018
KH
4288 int two_byte_p;
4289
06a2c219 4290 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
ee569018
KH
4291 s->char2b + s->nchars,
4292 &two_byte_p);
4293 s->two_byte_p = two_byte_p;
06a2c219
GM
4294 ++s->nchars;
4295 xassert (s->nchars <= end - start);
4296 s->width += glyph->pixel_width;
4297 ++glyph;
4298 }
4299
4300 s->font = s->face->font;
4301 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4302
4303 /* If the specified font could not be loaded, use the frame's font,
4304 but record the fact that we couldn't load it in
4305 S->font_not_found_p so that we can draw rectangles for the
4306 characters of the glyph string. */
ee569018 4307 if (s->font == NULL || glyph_not_available_p)
06a2c219
GM
4308 {
4309 s->font_not_found_p = 1;
4310 s->font = FRAME_FONT (s->f);
4311 }
4312
4313 /* Adjust base line for subscript/superscript text. */
4314 s->ybase += voffset;
66ac4b0e 4315
06a2c219
GM
4316 xassert (s->face && s->face->gc);
4317 return glyph - s->row->glyphs[s->area];
07e34cb0 4318}
dc6f92b8 4319
06a2c219
GM
4320
4321/* Fill glyph string S from image glyph S->first_glyph. */
dc6f92b8 4322
dfcf069d 4323static void
06a2c219
GM
4324x_fill_image_glyph_string (s)
4325 struct glyph_string *s;
4326{
4327 xassert (s->first_glyph->type == IMAGE_GLYPH);
43d120d8 4328 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
06a2c219 4329 xassert (s->img);
43d120d8 4330 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
06a2c219
GM
4331 s->font = s->face->font;
4332 s->width = s->first_glyph->pixel_width;
4333
4334 /* Adjust base line for subscript/superscript text. */
4335 s->ybase += s->first_glyph->voffset;
4336}
4337
4338
209f68d9 4339/* Fill glyph string S from a sequence of stretch glyphs.
06a2c219 4340
209f68d9
GM
4341 ROW is the glyph row in which the glyphs are found, AREA is the
4342 area within the row. START is the index of the first glyph to
4343 consider, END is the index of the last + 1.
4344
4345 Value is the index of the first glyph not in S. */
4346
4347static int
4348x_fill_stretch_glyph_string (s, row, area, start, end)
06a2c219 4349 struct glyph_string *s;
209f68d9
GM
4350 struct glyph_row *row;
4351 enum glyph_row_area area;
4352 int start, end;
06a2c219 4353{
209f68d9
GM
4354 struct glyph *glyph, *last;
4355 int voffset, face_id;
4356
06a2c219 4357 xassert (s->first_glyph->type == STRETCH_GLYPH);
209f68d9
GM
4358
4359 glyph = s->row->glyphs[s->area] + start;
4360 last = s->row->glyphs[s->area] + end;
4361 face_id = glyph->face_id;
4362 s->face = FACE_FROM_ID (s->f, face_id);
06a2c219 4363 s->font = s->face->font;
209f68d9
GM
4364 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4365 s->width = glyph->pixel_width;
4366 voffset = glyph->voffset;
4367
4368 for (++glyph;
4369 (glyph < last
4370 && glyph->type == STRETCH_GLYPH
4371 && glyph->voffset == voffset
4372 && glyph->face_id == face_id);
4373 ++glyph)
4374 s->width += glyph->pixel_width;
06a2c219
GM
4375
4376 /* Adjust base line for subscript/superscript text. */
209f68d9
GM
4377 s->ybase += voffset;
4378
4379 xassert (s->face && s->face->gc);
4380 return glyph - s->row->glyphs[s->area];
06a2c219
GM
4381}
4382
4383
4384/* Initialize glyph string S. CHAR2B is a suitably allocated vector
4385 of XChar2b structures for S; it can't be allocated in
4386 x_init_glyph_string because it must be allocated via `alloca'. W
4387 is the window on which S is drawn. ROW and AREA are the glyph row
4388 and area within the row from which S is constructed. START is the
4389 index of the first glyph structure covered by S. HL is a
4390 face-override for drawing S. */
4391
4392static void
4393x_init_glyph_string (s, char2b, w, row, area, start, hl)
4394 struct glyph_string *s;
4395 XChar2b *char2b;
4396 struct window *w;
4397 struct glyph_row *row;
4398 enum glyph_row_area area;
4399 int start;
4400 enum draw_glyphs_face hl;
4401{
4402 bzero (s, sizeof *s);
4403 s->w = w;
4404 s->f = XFRAME (w->frame);
4405 s->display = FRAME_X_DISPLAY (s->f);
4406 s->window = FRAME_X_WINDOW (s->f);
4407 s->char2b = char2b;
4408 s->hl = hl;
4409 s->row = row;
4410 s->area = area;
4411 s->first_glyph = row->glyphs[area] + start;
4412 s->height = row->height;
4413 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4414
9ea173e8
GM
4415 /* Display the internal border below the tool-bar window. */
4416 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219
GM
4417 s->y -= s->f->output_data.x->internal_border_width;
4418
4419 s->ybase = s->y + row->ascent;
4420}
4421
4422
4423/* Set background width of glyph string S. START is the index of the
4424 first glyph following S. LAST_X is the right-most x-position + 1
4425 in the drawing area. */
4426
4427static INLINE void
4428x_set_glyph_string_background_width (s, start, last_x)
4429 struct glyph_string *s;
4430 int start;
4431 int last_x;
4432{
4433 /* If the face of this glyph string has to be drawn to the end of
4434 the drawing area, set S->extends_to_end_of_line_p. */
4435 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4436
4437 if (start == s->row->used[s->area]
4438 && s->hl == DRAW_NORMAL_TEXT
4439 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4440 || s->face->background != default_face->background
4441 || s->face->stipple != default_face->stipple))
4442 s->extends_to_end_of_line_p = 1;
4443
4444 /* If S extends its face to the end of the line, set its
4445 background_width to the distance to the right edge of the drawing
4446 area. */
4447 if (s->extends_to_end_of_line_p)
1da3fd71 4448 s->background_width = last_x - s->x + 1;
06a2c219
GM
4449 else
4450 s->background_width = s->width;
4451}
4452
4453
4454/* Add a glyph string for a stretch glyph to the list of strings
4455 between HEAD and TAIL. START is the index of the stretch glyph in
4456 row area AREA of glyph row ROW. END is the index of the last glyph
4457 in that glyph row area. X is the current output position assigned
4458 to the new glyph string constructed. HL overrides that face of the
4459 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4460 is the right-most x-position of the drawing area. */
4461
8abee2e1
DL
4462/* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4463 and below -- keep them on one line. */
4464#define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4465 do \
4466 { \
4467 s = (struct glyph_string *) alloca (sizeof *s); \
4468 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
209f68d9 4469 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
06a2c219 4470 x_append_glyph_string (&HEAD, &TAIL, s); \
06a2c219
GM
4471 s->x = (X); \
4472 } \
4473 while (0)
4474
4475
4476/* Add a glyph string for an image glyph to the list of strings
4477 between HEAD and TAIL. START is the index of the image glyph in
4478 row area AREA of glyph row ROW. END is the index of the last glyph
4479 in that glyph row area. X is the current output position assigned
4480 to the new glyph string constructed. HL overrides that face of the
4481 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4482 is the right-most x-position of the drawing area. */
4483
8abee2e1 4484#define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4485 do \
4486 { \
4487 s = (struct glyph_string *) alloca (sizeof *s); \
4488 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4489 x_fill_image_glyph_string (s); \
4490 x_append_glyph_string (&HEAD, &TAIL, s); \
4491 ++START; \
4492 s->x = (X); \
4493 } \
4494 while (0)
4495
4496
4497/* Add a glyph string for a sequence of character glyphs to the list
4498 of strings between HEAD and TAIL. START is the index of the first
4499 glyph in row area AREA of glyph row ROW that is part of the new
4500 glyph string. END is the index of the last glyph in that glyph row
4501 area. X is the current output position assigned to the new glyph
4502 string constructed. HL overrides that face of the glyph; e.g. it
4503 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4504 right-most x-position of the drawing area. */
4505
8abee2e1 4506#define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4507 do \
4508 { \
3e71d8f2 4509 int c, face_id; \
06a2c219
GM
4510 XChar2b *char2b; \
4511 \
43d120d8 4512 c = (ROW)->glyphs[AREA][START].u.ch; \
43d120d8 4513 face_id = (ROW)->glyphs[AREA][START].face_id; \
06a2c219 4514 \
b4192550
KH
4515 s = (struct glyph_string *) alloca (sizeof *s); \
4516 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4517 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4518 x_append_glyph_string (&HEAD, &TAIL, s); \
b4192550
KH
4519 s->x = (X); \
4520 START = x_fill_glyph_string (s, face_id, START, END, \
66ac4b0e 4521 OVERLAPS_P); \
06a2c219
GM
4522 } \
4523 while (0)
4524
4525
b4192550
KH
4526/* Add a glyph string for a composite sequence to the list of strings
4527 between HEAD and TAIL. START is the index of the first glyph in
4528 row area AREA of glyph row ROW that is part of the new glyph
4529 string. END is the index of the last glyph in that glyph row area.
4530 X is the current output position assigned to the new glyph string
4531 constructed. HL overrides that face of the glyph; e.g. it is
4532 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4533 x-position of the drawing area. */
4534
6c27ec25 4535#define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
b4192550 4536 do { \
43d120d8
KH
4537 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4538 int face_id = (ROW)->glyphs[AREA][START].face_id; \
ee569018 4539 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
b4192550
KH
4540 struct composition *cmp = composition_table[cmp_id]; \
4541 int glyph_len = cmp->glyph_len; \
4542 XChar2b *char2b; \
4543 struct face **faces; \
4544 struct glyph_string *first_s = NULL; \
4545 int n; \
4546 \
ee569018 4547 base_face = base_face->ascii_face; \
b4192550
KH
4548 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4549 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4550 /* At first, fill in `char2b' and `faces'. */ \
4551 for (n = 0; n < glyph_len; n++) \
4552 { \
43d120d8 4553 int c = COMPOSITION_GLYPH (cmp, n); \
ee569018
KH
4554 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4555 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4556 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4557 this_face_id, char2b + n, 1); \
b4192550
KH
4558 } \
4559 \
4560 /* Make glyph_strings for each glyph sequence that is drawable by \
4561 the same face, and append them to HEAD/TAIL. */ \
4562 for (n = 0; n < cmp->glyph_len;) \
4563 { \
4564 s = (struct glyph_string *) alloca (sizeof *s); \
4565 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4566 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4567 s->cmp = cmp; \
4568 s->gidx = n; \
b4192550
KH
4569 s->x = (X); \
4570 \
4571 if (n == 0) \
4572 first_s = s; \
4573 \
4574 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4575 } \
4576 \
4577 ++START; \
4578 s = first_s; \
4579 } while (0)
4580
4581
06a2c219
GM
4582/* Build a list of glyph strings between HEAD and TAIL for the glyphs
4583 of AREA of glyph row ROW on window W between indices START and END.
4584 HL overrides the face for drawing glyph strings, e.g. it is
4585 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4586 x-positions of the drawing area.
4587
4588 This is an ugly monster macro construct because we must use alloca
4589 to allocate glyph strings (because x_draw_glyphs can be called
4590 asynchronously). */
4591
8abee2e1 4592#define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4593 do \
4594 { \
4595 HEAD = TAIL = NULL; \
4596 while (START < END) \
4597 { \
4598 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4599 switch (first_glyph->type) \
4600 { \
4601 case CHAR_GLYPH: \
4602 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
66ac4b0e
GM
4603 TAIL, HL, X, LAST_X, \
4604 OVERLAPS_P); \
06a2c219
GM
4605 break; \
4606 \
b4192550
KH
4607 case COMPOSITE_GLYPH: \
4608 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4609 HEAD, TAIL, HL, X, LAST_X,\
4610 OVERLAPS_P); \
4611 break; \
4612 \
06a2c219
GM
4613 case STRETCH_GLYPH: \
4614 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4615 HEAD, TAIL, HL, X, LAST_X); \
4616 break; \
4617 \
4618 case IMAGE_GLYPH: \
4619 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4620 TAIL, HL, X, LAST_X); \
4621 break; \
4622 \
4623 default: \
4624 abort (); \
4625 } \
4626 \
4627 x_set_glyph_string_background_width (s, START, LAST_X); \
4628 (X) += s->width; \
4629 } \
4630 } \
4631 while (0)
4632
4633
4634/* Draw glyphs between START and END in AREA of ROW on window W,
4635 starting at x-position X. X is relative to AREA in W. HL is a
4636 face-override with the following meaning:
4637
4638 DRAW_NORMAL_TEXT draw normally
4639 DRAW_CURSOR draw in cursor face
4640 DRAW_MOUSE_FACE draw in mouse face.
4641 DRAW_INVERSE_VIDEO draw in mode line face
4642 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4643 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4644
4645 If REAL_START is non-null, return in *REAL_START the real starting
4646 position for display. This can be different from START in case
4647 overlapping glyphs must be displayed. If REAL_END is non-null,
4648 return in *REAL_END the real end position for display. This can be
4649 different from END in case overlapping glyphs must be displayed.
4650
66ac4b0e
GM
4651 If OVERLAPS_P is non-zero, draw only the foreground of characters
4652 and clip to the physical height of ROW.
4653
06a2c219
GM
4654 Value is the x-position reached, relative to AREA of W. */
4655
4656static int
66ac4b0e
GM
4657x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4658 overlaps_p)
06a2c219
GM
4659 struct window *w;
4660 int x;
4661 struct glyph_row *row;
4662 enum glyph_row_area area;
4663 int start, end;
4664 enum draw_glyphs_face hl;
4665 int *real_start, *real_end;
66ac4b0e 4666 int overlaps_p;
dc6f92b8 4667{
06a2c219
GM
4668 struct glyph_string *head, *tail;
4669 struct glyph_string *s;
4670 int last_x, area_width;
4671 int x_reached;
4672 int i, j;
4673
4674 /* Let's rather be paranoid than getting a SEGV. */
4675 start = max (0, start);
4676 end = min (end, row->used[area]);
4677 if (real_start)
4678 *real_start = start;
4679 if (real_end)
4680 *real_end = end;
4681
4682 /* Translate X to frame coordinates. Set last_x to the right
4683 end of the drawing area. */
4684 if (row->full_width_p)
4685 {
4686 /* X is relative to the left edge of W, without scroll bars
4687 or flag areas. */
4688 struct frame *f = XFRAME (w->frame);
110859fc 4689 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
06a2c219 4690 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
dc6f92b8 4691
06a2c219
GM
4692 x += window_left_x;
4693 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4694 last_x = window_left_x + area_width;
4695
4696 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4697 {
110859fc 4698 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
06a2c219
GM
4699 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4700 last_x += width;
4701 else
4702 x -= width;
4703 }
dc6f92b8 4704
b9432a85
GM
4705 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4706 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
06a2c219
GM
4707 }
4708 else
dc6f92b8 4709 {
06a2c219
GM
4710 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4711 area_width = window_box_width (w, area);
4712 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
dc6f92b8
JB
4713 }
4714
06a2c219
GM
4715 /* Build a doubly-linked list of glyph_string structures between
4716 head and tail from what we have to draw. Note that the macro
4717 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4718 the reason we use a separate variable `i'. */
4719 i = start;
66ac4b0e
GM
4720 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4721 overlaps_p);
06a2c219
GM
4722 if (tail)
4723 x_reached = tail->x + tail->background_width;
4724 else
4725 x_reached = x;
90e65f07 4726
06a2c219
GM
4727 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4728 the row, redraw some glyphs in front or following the glyph
4729 strings built above. */
66ac4b0e 4730 if (!overlaps_p && row->contains_overlapping_glyphs_p)
06a2c219
GM
4731 {
4732 int dummy_x = 0;
4733 struct glyph_string *h, *t;
4734
4735 /* Compute overhangs for all glyph strings. */
4736 for (s = head; s; s = s->next)
4737 x_compute_glyph_string_overhangs (s);
4738
4739 /* Prepend glyph strings for glyphs in front of the first glyph
4740 string that are overwritten because of the first glyph
4741 string's left overhang. The background of all strings
4742 prepended must be drawn because the first glyph string
4743 draws over it. */
4744 i = x_left_overwritten (head);
4745 if (i >= 0)
4746 {
4747 j = i;
4748 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
66ac4b0e
GM
4749 DRAW_NORMAL_TEXT, dummy_x, last_x,
4750 overlaps_p);
06a2c219
GM
4751 start = i;
4752 if (real_start)
4753 *real_start = start;
4754 x_compute_overhangs_and_x (t, head->x, 1);
4755 x_prepend_glyph_string_lists (&head, &tail, h, t);
4756 }
58769bee 4757
06a2c219
GM
4758 /* Prepend glyph strings for glyphs in front of the first glyph
4759 string that overwrite that glyph string because of their
4760 right overhang. For these strings, only the foreground must
4761 be drawn, because it draws over the glyph string at `head'.
4762 The background must not be drawn because this would overwrite
4763 right overhangs of preceding glyphs for which no glyph
4764 strings exist. */
4765 i = x_left_overwriting (head);
4766 if (i >= 0)
4767 {
4768 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
66ac4b0e
GM
4769 DRAW_NORMAL_TEXT, dummy_x, last_x,
4770 overlaps_p);
06a2c219
GM
4771 for (s = h; s; s = s->next)
4772 s->background_filled_p = 1;
4773 if (real_start)
4774 *real_start = i;
4775 x_compute_overhangs_and_x (t, head->x, 1);
4776 x_prepend_glyph_string_lists (&head, &tail, h, t);
4777 }
dbcb258a 4778
06a2c219
GM
4779 /* Append glyphs strings for glyphs following the last glyph
4780 string tail that are overwritten by tail. The background of
4781 these strings has to be drawn because tail's foreground draws
4782 over it. */
4783 i = x_right_overwritten (tail);
4784 if (i >= 0)
4785 {
4786 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4787 DRAW_NORMAL_TEXT, x, last_x,
4788 overlaps_p);
06a2c219
GM
4789 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4790 x_append_glyph_string_lists (&head, &tail, h, t);
4791 if (real_end)
4792 *real_end = i;
4793 }
dc6f92b8 4794
06a2c219
GM
4795 /* Append glyph strings for glyphs following the last glyph
4796 string tail that overwrite tail. The foreground of such
4797 glyphs has to be drawn because it writes into the background
4798 of tail. The background must not be drawn because it could
4799 paint over the foreground of following glyphs. */
4800 i = x_right_overwriting (tail);
4801 if (i >= 0)
4802 {
4803 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4804 DRAW_NORMAL_TEXT, x, last_x,
4805 overlaps_p);
06a2c219
GM
4806 for (s = h; s; s = s->next)
4807 s->background_filled_p = 1;
4808 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4809 x_append_glyph_string_lists (&head, &tail, h, t);
4810 if (real_end)
4811 *real_end = i;
4812 }
4813 }
58769bee 4814
06a2c219
GM
4815 /* Draw all strings. */
4816 for (s = head; s; s = s->next)
4817 x_draw_glyph_string (s);
dc6f92b8 4818
06a2c219
GM
4819 /* Value is the x-position up to which drawn, relative to AREA of W.
4820 This doesn't include parts drawn because of overhangs. */
4821 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4822 if (!row->full_width_p)
4823 {
4824 if (area > LEFT_MARGIN_AREA)
4825 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4826 if (area > TEXT_AREA)
4827 x_reached -= window_box_width (w, TEXT_AREA);
4828 }
4829 return x_reached;
4830}
dc6f92b8 4831
dc6f92b8 4832
66ac4b0e
GM
4833/* Fix the display of area AREA of overlapping row ROW in window W. */
4834
4835static void
4836x_fix_overlapping_area (w, row, area)
4837 struct window *w;
4838 struct glyph_row *row;
4839 enum glyph_row_area area;
4840{
4841 int i, x;
4842
4843 BLOCK_INPUT;
4844
4845 if (area == LEFT_MARGIN_AREA)
4846 x = 0;
4847 else if (area == TEXT_AREA)
4848 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
4849 else
4850 x = (window_box_width (w, LEFT_MARGIN_AREA)
4851 + window_box_width (w, TEXT_AREA));
4852
4853 for (i = 0; i < row->used[area];)
4854 {
4855 if (row->glyphs[area][i].overlaps_vertically_p)
4856 {
4857 int start = i, start_x = x;
4858
4859 do
4860 {
4861 x += row->glyphs[area][i].pixel_width;
4862 ++i;
4863 }
4864 while (i < row->used[area]
4865 && row->glyphs[area][i].overlaps_vertically_p);
4866
4867 x_draw_glyphs (w, start_x, row, area, start, i,
4868 (row->inverse_p
4869 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4870 NULL, NULL, 1);
4871 }
4872 else
4873 {
4874 x += row->glyphs[area][i].pixel_width;
4875 ++i;
4876 }
4877 }
4878
4879 UNBLOCK_INPUT;
4880}
4881
4882
06a2c219
GM
4883/* Output LEN glyphs starting at START at the nominal cursor position.
4884 Advance the nominal cursor over the text. The global variable
4885 updated_window contains the window being updated, updated_row is
4886 the glyph row being updated, and updated_area is the area of that
4887 row being updated. */
dc6f92b8 4888
06a2c219
GM
4889static void
4890x_write_glyphs (start, len)
4891 struct glyph *start;
4892 int len;
4893{
4894 int x, hpos, real_start, real_end;
d9cdbb3d 4895
06a2c219 4896 xassert (updated_window && updated_row);
dc6f92b8 4897 BLOCK_INPUT;
06a2c219
GM
4898
4899 /* Write glyphs. */
dc6f92b8 4900
06a2c219
GM
4901 hpos = start - updated_row->glyphs[updated_area];
4902 x = x_draw_glyphs (updated_window, output_cursor.x,
4903 updated_row, updated_area,
4904 hpos, hpos + len,
4905 (updated_row->inverse_p
4906 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
66ac4b0e 4907 &real_start, &real_end, 0);
b30ec466 4908
06a2c219
GM
4909 /* If we drew over the cursor, note that it is not visible any more. */
4910 note_overwritten_text_cursor (updated_window, real_start,
4911 real_end - real_start);
dc6f92b8
JB
4912
4913 UNBLOCK_INPUT;
06a2c219
GM
4914
4915 /* Advance the output cursor. */
4916 output_cursor.hpos += len;
4917 output_cursor.x = x;
dc6f92b8
JB
4918}
4919
0cdd0c9f 4920
06a2c219 4921/* Insert LEN glyphs from START at the nominal cursor position. */
0cdd0c9f 4922
06a2c219
GM
4923static void
4924x_insert_glyphs (start, len)
4925 struct glyph *start;
4926 register int len;
4927{
4928 struct frame *f;
4929 struct window *w;
4930 int line_height, shift_by_width, shifted_region_width;
4931 struct glyph_row *row;
4932 struct glyph *glyph;
4933 int frame_x, frame_y, hpos, real_start, real_end;
58769bee 4934
06a2c219 4935 xassert (updated_window && updated_row);
0cdd0c9f 4936 BLOCK_INPUT;
06a2c219
GM
4937 w = updated_window;
4938 f = XFRAME (WINDOW_FRAME (w));
4939
4940 /* Get the height of the line we are in. */
4941 row = updated_row;
4942 line_height = row->height;
4943
4944 /* Get the width of the glyphs to insert. */
4945 shift_by_width = 0;
4946 for (glyph = start; glyph < start + len; ++glyph)
4947 shift_by_width += glyph->pixel_width;
4948
4949 /* Get the width of the region to shift right. */
4950 shifted_region_width = (window_box_width (w, updated_area)
4951 - output_cursor.x
4952 - shift_by_width);
4953
4954 /* Shift right. */
4955 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
4956 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
4957 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
4958 f->output_data.x->normal_gc,
4959 frame_x, frame_y,
4960 shifted_region_width, line_height,
4961 frame_x + shift_by_width, frame_y);
4962
4963 /* Write the glyphs. */
4964 hpos = start - row->glyphs[updated_area];
4965 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
66ac4b0e 4966 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
06a2c219
GM
4967 note_overwritten_text_cursor (w, real_start, real_end - real_start);
4968
4969 /* Advance the output cursor. */
4970 output_cursor.hpos += len;
4971 output_cursor.x += shift_by_width;
0cdd0c9f
RS
4972 UNBLOCK_INPUT;
4973}
0cdd0c9f 4974
0cdd0c9f 4975
06a2c219
GM
4976/* Delete N glyphs at the nominal cursor position. Not implemented
4977 for X frames. */
c83febd7
RS
4978
4979static void
06a2c219
GM
4980x_delete_glyphs (n)
4981 register int n;
c83febd7 4982{
06a2c219 4983 abort ();
c83febd7
RS
4984}
4985
0cdd0c9f 4986
06a2c219
GM
4987/* Erase the current text line from the nominal cursor position
4988 (inclusive) to pixel column TO_X (exclusive). The idea is that
4989 everything from TO_X onward is already erased.
4990
4991 TO_X is a pixel position relative to updated_area of
4992 updated_window. TO_X == -1 means clear to the end of this area. */
dc6f92b8 4993
06a2c219
GM
4994static void
4995x_clear_end_of_line (to_x)
4996 int to_x;
4997{
4998 struct frame *f;
4999 struct window *w = updated_window;
5000 int max_x, min_y, max_y;
5001 int from_x, from_y, to_y;
5002
5003 xassert (updated_window && updated_row);
5004 f = XFRAME (w->frame);
5005
5006 if (updated_row->full_width_p)
5007 {
5008 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5009 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5010 && !w->pseudo_window_p)
5011 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
0cdd0c9f 5012 }
06a2c219
GM
5013 else
5014 max_x = window_box_width (w, updated_area);
5015 max_y = window_text_bottom_y (w);
dc6f92b8 5016
06a2c219
GM
5017 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5018 of window. For TO_X > 0, truncate to end of drawing area. */
5019 if (to_x == 0)
5020 return;
5021 else if (to_x < 0)
5022 to_x = max_x;
5023 else
5024 to_x = min (to_x, max_x);
dbc4e1c1 5025
06a2c219
GM
5026 to_y = min (max_y, output_cursor.y + updated_row->height);
5027
5028 /* Notice if the cursor will be cleared by this operation. */
5029 if (!updated_row->full_width_p)
5030 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
dbc4e1c1 5031
06a2c219
GM
5032 from_x = output_cursor.x;
5033
5034 /* Translate to frame coordinates. */
5035 if (updated_row->full_width_p)
5036 {
5037 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5038 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5039 }
0cdd0c9f
RS
5040 else
5041 {
06a2c219
GM
5042 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5043 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5044 }
5045
045dee35 5046 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
06a2c219
GM
5047 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5048 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5049
5050 /* Prevent inadvertently clearing to end of the X window. */
5051 if (to_x > from_x && to_y > from_y)
5052 {
5053 BLOCK_INPUT;
5054 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5055 from_x, from_y, to_x - from_x, to_y - from_y,
5056 False);
5057 UNBLOCK_INPUT;
0cdd0c9f 5058 }
0cdd0c9f 5059}
dbc4e1c1 5060
0cdd0c9f 5061
06a2c219 5062/* Clear entire frame. If updating_frame is non-null, clear that
b86bd3dd 5063 frame. Otherwise clear the selected frame. */
06a2c219
GM
5064
5065static void
5066x_clear_frame ()
0cdd0c9f 5067{
06a2c219 5068 struct frame *f;
0cdd0c9f 5069
06a2c219
GM
5070 if (updating_frame)
5071 f = updating_frame;
0cdd0c9f 5072 else
b86bd3dd 5073 f = SELECTED_FRAME ();
58769bee 5074
06a2c219
GM
5075 /* Clearing the frame will erase any cursor, so mark them all as no
5076 longer visible. */
5077 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5078 output_cursor.hpos = output_cursor.vpos = 0;
5079 output_cursor.x = -1;
5080
5081 /* We don't set the output cursor here because there will always
5082 follow an explicit cursor_to. */
5083 BLOCK_INPUT;
5084 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5085
5086 /* We have to clear the scroll bars, too. If we have changed
5087 colors or something like that, then they should be notified. */
5088 x_scroll_bar_clear (f);
0cdd0c9f 5089
06a2c219
GM
5090 XFlush (FRAME_X_DISPLAY (f));
5091 UNBLOCK_INPUT;
dc6f92b8 5092}
06a2c219
GM
5093
5094
dc6f92b8 5095\f
dbc4e1c1
JB
5096/* Invert the middle quarter of the frame for .15 sec. */
5097
06a2c219
GM
5098/* We use the select system call to do the waiting, so we have to make
5099 sure it's available. If it isn't, we just won't do visual bells. */
5100
dbc4e1c1
JB
5101#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5102
06a2c219
GM
5103
5104/* Subtract the `struct timeval' values X and Y, storing the result in
5105 *RESULT. Return 1 if the difference is negative, otherwise 0. */
dbc4e1c1
JB
5106
5107static int
5108timeval_subtract (result, x, y)
5109 struct timeval *result, x, y;
5110{
06a2c219
GM
5111 /* Perform the carry for the later subtraction by updating y. This
5112 is safer because on some systems the tv_sec member is unsigned. */
dbc4e1c1
JB
5113 if (x.tv_usec < y.tv_usec)
5114 {
5115 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5116 y.tv_usec -= 1000000 * nsec;
5117 y.tv_sec += nsec;
5118 }
06a2c219 5119
dbc4e1c1
JB
5120 if (x.tv_usec - y.tv_usec > 1000000)
5121 {
5122 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5123 y.tv_usec += 1000000 * nsec;
5124 y.tv_sec -= nsec;
5125 }
5126
06a2c219
GM
5127 /* Compute the time remaining to wait. tv_usec is certainly
5128 positive. */
dbc4e1c1
JB
5129 result->tv_sec = x.tv_sec - y.tv_sec;
5130 result->tv_usec = x.tv_usec - y.tv_usec;
5131
06a2c219
GM
5132 /* Return indication of whether the result should be considered
5133 negative. */
dbc4e1c1
JB
5134 return x.tv_sec < y.tv_sec;
5135}
dc6f92b8 5136
dfcf069d 5137void
f676886a
JB
5138XTflash (f)
5139 struct frame *f;
dc6f92b8 5140{
dbc4e1c1 5141 BLOCK_INPUT;
dc6f92b8 5142
dbc4e1c1
JB
5143 {
5144 GC gc;
dc6f92b8 5145
06a2c219
GM
5146 /* Create a GC that will use the GXxor function to flip foreground
5147 pixels into background pixels. */
dbc4e1c1
JB
5148 {
5149 XGCValues values;
dc6f92b8 5150
dbc4e1c1 5151 values.function = GXxor;
7556890b
RS
5152 values.foreground = (f->output_data.x->foreground_pixel
5153 ^ f->output_data.x->background_pixel);
58769bee 5154
334208b7 5155 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dbc4e1c1
JB
5156 GCFunction | GCForeground, &values);
5157 }
dc6f92b8 5158
dbc4e1c1 5159 {
e84e14c3
RS
5160 /* Get the height not including a menu bar widget. */
5161 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5162 /* Height of each line to flash. */
5163 int flash_height = FRAME_LINE_HEIGHT (f);
5164 /* These will be the left and right margins of the rectangles. */
5165 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5166 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5167
5168 int width;
5169
5170 /* Don't flash the area between a scroll bar and the frame
5171 edge it is next to. */
5172 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5173 {
5174 case vertical_scroll_bar_left:
5175 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5176 break;
5177
5178 case vertical_scroll_bar_right:
5179 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5180 break;
06a2c219
GM
5181
5182 default:
5183 break;
e84e14c3
RS
5184 }
5185
5186 width = flash_right - flash_left;
5187
5188 /* If window is tall, flash top and bottom line. */
5189 if (height > 3 * FRAME_LINE_HEIGHT (f))
5190 {
5191 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5192 flash_left,
5193 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5194 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5195 width, flash_height);
5196 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5197 flash_left,
5198 (height - flash_height
5199 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5200 width, flash_height);
5201 }
5202 else
5203 /* If it is short, flash it all. */
5204 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5205 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5206 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
dc6f92b8 5207
06a2c219 5208 x_flush (f);
dc6f92b8 5209
dbc4e1c1 5210 {
06a2c219 5211 struct timeval wakeup;
dc6f92b8 5212
66c30ea1 5213 EMACS_GET_TIME (wakeup);
dc6f92b8 5214
dbc4e1c1
JB
5215 /* Compute time to wait until, propagating carry from usecs. */
5216 wakeup.tv_usec += 150000;
5217 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5218 wakeup.tv_usec %= 1000000;
5219
5220 /* Keep waiting until past the time wakeup. */
5221 while (1)
5222 {
5223 struct timeval timeout;
5224
66c30ea1 5225 EMACS_GET_TIME (timeout);
dbc4e1c1
JB
5226
5227 /* In effect, timeout = wakeup - timeout.
5228 Break if result would be negative. */
5229 if (timeval_subtract (&timeout, wakeup, timeout))
5230 break;
5231
5232 /* Try to wait that long--but we might wake up sooner. */
c32cdd9a 5233 select (0, NULL, NULL, NULL, &timeout);
dbc4e1c1
JB
5234 }
5235 }
58769bee 5236
e84e14c3
RS
5237 /* If window is tall, flash top and bottom line. */
5238 if (height > 3 * FRAME_LINE_HEIGHT (f))
5239 {
5240 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5241 flash_left,
5242 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5243 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5244 width, flash_height);
5245 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5246 flash_left,
5247 (height - flash_height
5248 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5249 width, flash_height);
5250 }
5251 else
5252 /* If it is short, flash it all. */
5253 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5254 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5255 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5256
334208b7 5257 XFreeGC (FRAME_X_DISPLAY (f), gc);
06a2c219 5258 x_flush (f);
dc6f92b8 5259 }
dbc4e1c1
JB
5260 }
5261
5262 UNBLOCK_INPUT;
dc6f92b8
JB
5263}
5264
06a2c219 5265#endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
dbc4e1c1
JB
5266
5267
dc6f92b8
JB
5268/* Make audible bell. */
5269
dfcf069d 5270void
dc6f92b8
JB
5271XTring_bell ()
5272{
b86bd3dd
GM
5273 struct frame *f = SELECTED_FRAME ();
5274
5275 if (FRAME_X_DISPLAY (f))
5276 {
dbc4e1c1 5277#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
b86bd3dd
GM
5278 if (visible_bell)
5279 XTflash (f);
5280 else
dbc4e1c1 5281#endif
b86bd3dd
GM
5282 {
5283 BLOCK_INPUT;
5284 XBell (FRAME_X_DISPLAY (f), 0);
5285 XFlush (FRAME_X_DISPLAY (f));
5286 UNBLOCK_INPUT;
5287 }
dc6f92b8
JB
5288 }
5289}
06a2c219 5290
dc6f92b8 5291\f
06a2c219
GM
5292/* Specify how many text lines, from the top of the window,
5293 should be affected by insert-lines and delete-lines operations.
5294 This, and those operations, are used only within an update
5295 that is bounded by calls to x_update_begin and x_update_end. */
dc6f92b8 5296
dfcf069d 5297static void
06a2c219
GM
5298XTset_terminal_window (n)
5299 register int n;
dc6f92b8 5300{
06a2c219 5301 /* This function intentionally left blank. */
dc6f92b8
JB
5302}
5303
06a2c219
GM
5304
5305\f
5306/***********************************************************************
5307 Line Dance
5308 ***********************************************************************/
5309
5310/* Perform an insert-lines or delete-lines operation, inserting N
5311 lines or deleting -N lines at vertical position VPOS. */
5312
dfcf069d 5313static void
06a2c219
GM
5314x_ins_del_lines (vpos, n)
5315 int vpos, n;
dc6f92b8
JB
5316{
5317 abort ();
5318}
06a2c219
GM
5319
5320
5321/* Scroll part of the display as described by RUN. */
dc6f92b8 5322
dfcf069d 5323static void
06a2c219
GM
5324x_scroll_run (w, run)
5325 struct window *w;
5326 struct run *run;
dc6f92b8 5327{
06a2c219
GM
5328 struct frame *f = XFRAME (w->frame);
5329 int x, y, width, height, from_y, to_y, bottom_y;
5330
5331 /* Get frame-relative bounding box of the text display area of W,
5332 without mode lines. Include in this box the flags areas to the
5333 left and right of W. */
5334 window_box (w, -1, &x, &y, &width, &height);
110859fc
GM
5335 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5336 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
5337
5338 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5339 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5340 bottom_y = y + height;
dc6f92b8 5341
06a2c219
GM
5342 if (to_y < from_y)
5343 {
5344 /* Scrolling up. Make sure we don't copy part of the mode
5345 line at the bottom. */
5346 if (from_y + run->height > bottom_y)
5347 height = bottom_y - from_y;
5348 else
5349 height = run->height;
5350 }
dc6f92b8 5351 else
06a2c219
GM
5352 {
5353 /* Scolling down. Make sure we don't copy over the mode line.
5354 at the bottom. */
5355 if (to_y + run->height > bottom_y)
5356 height = bottom_y - to_y;
5357 else
5358 height = run->height;
5359 }
7a13e894 5360
06a2c219
GM
5361 BLOCK_INPUT;
5362
5363 /* Cursor off. Will be switched on again in x_update_window_end. */
5364 updated_window = w;
5365 x_clear_cursor (w);
5366
5367 XCopyArea (FRAME_X_DISPLAY (f),
5368 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5369 f->output_data.x->normal_gc,
5370 x, from_y,
5371 width, height,
5372 x, to_y);
5373
5374 UNBLOCK_INPUT;
5375}
dc6f92b8 5376
dc6f92b8 5377
06a2c219
GM
5378\f
5379/***********************************************************************
5380 Exposure Events
5381 ***********************************************************************/
5382
5383/* Redisplay an exposed area of frame F. X and Y are the upper-left
5384 corner of the exposed rectangle. W and H are width and height of
5385 the exposed area. All are pixel values. W or H zero means redraw
5386 the entire frame. */
dc6f92b8 5387
06a2c219
GM
5388static void
5389expose_frame (f, x, y, w, h)
5390 struct frame *f;
5391 int x, y, w, h;
dc6f92b8 5392{
06a2c219 5393 XRectangle r;
dc6f92b8 5394
06a2c219 5395 TRACE ((stderr, "expose_frame "));
dc6f92b8 5396
06a2c219
GM
5397 /* No need to redraw if frame will be redrawn soon. */
5398 if (FRAME_GARBAGED_P (f))
dc6f92b8 5399 {
06a2c219
GM
5400 TRACE ((stderr, " garbaged\n"));
5401 return;
5402 }
5403
5404 /* If basic faces haven't been realized yet, there is no point in
5405 trying to redraw anything. This can happen when we get an expose
5406 event while Emacs is starting, e.g. by moving another window. */
5407 if (FRAME_FACE_CACHE (f) == NULL
5408 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5409 {
5410 TRACE ((stderr, " no faces\n"));
5411 return;
58769bee 5412 }
06a2c219
GM
5413
5414 if (w == 0 || h == 0)
58769bee 5415 {
06a2c219
GM
5416 r.x = r.y = 0;
5417 r.width = CANON_X_UNIT (f) * f->width;
5418 r.height = CANON_Y_UNIT (f) * f->height;
dc6f92b8
JB
5419 }
5420 else
5421 {
06a2c219
GM
5422 r.x = x;
5423 r.y = y;
5424 r.width = w;
5425 r.height = h;
5426 }
5427
5428 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5429 expose_window_tree (XWINDOW (f->root_window), &r);
5430
9ea173e8 5431 if (WINDOWP (f->tool_bar_window))
06a2c219 5432 {
9ea173e8 5433 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
5434 XRectangle window_rect;
5435 XRectangle intersection_rect;
5436 int window_x, window_y, window_width, window_height;
5437
5438
5439 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5440 window_rect.x = window_x;
5441 window_rect.y = window_y;
5442 window_rect.width = window_width;
5443 window_rect.height = window_height;
5444
5445 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5446 expose_window (w, &intersection_rect);
5447 }
5448
5449#ifndef USE_X_TOOLKIT
5450 if (WINDOWP (f->menu_bar_window))
5451 {
5452 struct window *w = XWINDOW (f->menu_bar_window);
5453 XRectangle window_rect;
5454 XRectangle intersection_rect;
5455 int window_x, window_y, window_width, window_height;
5456
5457
5458 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5459 window_rect.x = window_x;
5460 window_rect.y = window_y;
5461 window_rect.width = window_width;
5462 window_rect.height = window_height;
5463
5464 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5465 expose_window (w, &intersection_rect);
dc6f92b8 5466 }
06a2c219 5467#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
5468}
5469
06a2c219
GM
5470
5471/* Redraw (parts) of all windows in the window tree rooted at W that
5472 intersect R. R contains frame pixel coordinates. */
5473
58769bee 5474static void
06a2c219
GM
5475expose_window_tree (w, r)
5476 struct window *w;
5477 XRectangle *r;
dc6f92b8 5478{
06a2c219
GM
5479 while (w)
5480 {
5481 if (!NILP (w->hchild))
5482 expose_window_tree (XWINDOW (w->hchild), r);
5483 else if (!NILP (w->vchild))
5484 expose_window_tree (XWINDOW (w->vchild), r);
5485 else
5486 {
5487 XRectangle window_rect;
5488 XRectangle intersection_rect;
5489 struct frame *f = XFRAME (w->frame);
5490 int window_x, window_y, window_width, window_height;
5491
5492 /* Frame-relative pixel rectangle of W. */
5493 window_box (w, -1, &window_x, &window_y, &window_width,
5494 &window_height);
5495 window_rect.x
5496 = (window_x
110859fc 5497 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
714dc26c 5498 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
06a2c219
GM
5499 window_rect.y = window_y;
5500 window_rect.width
5501 = (window_width
110859fc 5502 + FRAME_X_FLAGS_AREA_WIDTH (f)
06a2c219
GM
5503 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5504 window_rect.height
5505 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5506
5507 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5508 expose_window (w, &intersection_rect);
5509 }
58769bee 5510
06a2c219
GM
5511 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5512 }
5513}
58769bee 5514
dc6f92b8 5515
06a2c219
GM
5516/* Redraw the part of glyph row area AREA of glyph row ROW on window W
5517 which intersects rectangle R. R is in window-relative coordinates. */
5518
5519static void
5520expose_area (w, row, r, area)
5521 struct window *w;
5522 struct glyph_row *row;
5523 XRectangle *r;
5524 enum glyph_row_area area;
5525{
5526 int x;
5527 struct glyph *first = row->glyphs[area];
5528 struct glyph *end = row->glyphs[area] + row->used[area];
5529 struct glyph *last;
5530 int first_x;
5531
5532 /* Set x to the window-relative start position for drawing glyphs of
5533 AREA. The first glyph of the text area can be partially visible.
5534 The first glyphs of other areas cannot. */
5535 if (area == LEFT_MARGIN_AREA)
5536 x = 0;
5537 else if (area == TEXT_AREA)
5538 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5539 else
5540 x = (window_box_width (w, LEFT_MARGIN_AREA)
5541 + window_box_width (w, TEXT_AREA));
5542
6fb13182
GM
5543 if (area == TEXT_AREA && row->fill_line_p)
5544 /* If row extends face to end of line write the whole line. */
5545 x_draw_glyphs (w, x, row, area,
5546 0, row->used[area],
06a2c219 5547 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5548 NULL, NULL, 0);
6fb13182
GM
5549 else
5550 {
5551 /* Find the first glyph that must be redrawn. */
5552 while (first < end
5553 && x + first->pixel_width < r->x)
5554 {
5555 x += first->pixel_width;
5556 ++first;
5557 }
5558
5559 /* Find the last one. */
5560 last = first;
5561 first_x = x;
5562 while (last < end
5563 && x < r->x + r->width)
5564 {
5565 x += last->pixel_width;
5566 ++last;
5567 }
5568
5569 /* Repaint. */
5570 if (last > first)
5571 x_draw_glyphs (w, first_x, row, area,
5572 first - row->glyphs[area],
5573 last - row->glyphs[area],
5574 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5575 NULL, NULL, 0);
5576 }
06a2c219
GM
5577}
5578
58769bee 5579
06a2c219
GM
5580/* Redraw the parts of the glyph row ROW on window W intersecting
5581 rectangle R. R is in window-relative coordinates. */
dc6f92b8 5582
06a2c219
GM
5583static void
5584expose_line (w, row, r)
5585 struct window *w;
5586 struct glyph_row *row;
5587 XRectangle *r;
5588{
5589 xassert (row->enabled_p);
5590
5591 if (row->mode_line_p || w->pseudo_window_p)
5592 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5593 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5594 NULL, NULL, 0);
06a2c219
GM
5595 else
5596 {
5597 if (row->used[LEFT_MARGIN_AREA])
5598 expose_area (w, row, r, LEFT_MARGIN_AREA);
5599 if (row->used[TEXT_AREA])
5600 expose_area (w, row, r, TEXT_AREA);
5601 if (row->used[RIGHT_MARGIN_AREA])
5602 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5603 x_draw_row_bitmaps (w, row);
5604 }
5605}
dc6f92b8 5606
58769bee 5607
06a2c219
GM
5608/* Return non-zero if W's cursor intersects rectangle R. */
5609
5610static int
5611x_phys_cursor_in_rect_p (w, r)
5612 struct window *w;
5613 XRectangle *r;
5614{
5615 XRectangle cr, result;
5616 struct glyph *cursor_glyph;
5617
5618 cursor_glyph = get_phys_cursor_glyph (w);
5619 if (cursor_glyph)
5620 {
5621 cr.x = w->phys_cursor.x;
5622 cr.y = w->phys_cursor.y;
5623 cr.width = cursor_glyph->pixel_width;
5624 cr.height = w->phys_cursor_height;
5625 return x_intersect_rectangles (&cr, r, &result);
5626 }
5627 else
5628 return 0;
dc6f92b8 5629}
dc6f92b8 5630
06a2c219
GM
5631
5632/* Redraw a rectangle of window W. R is a rectangle in window
5633 relative coordinates. Call this function with input blocked. */
dc6f92b8
JB
5634
5635static void
06a2c219
GM
5636expose_window (w, r)
5637 struct window *w;
5638 XRectangle *r;
dc6f92b8 5639{
06a2c219
GM
5640 struct glyph_row *row;
5641 int y;
5642 int yb = window_text_bottom_y (w);
5643 int cursor_cleared_p;
dc6f92b8 5644
80c32bcc
GM
5645 /* If window is not yet fully initialized, do nothing. This can
5646 happen when toolkit scroll bars are used and a window is split.
5647 Reconfiguring the scroll bar will generate an expose for a newly
5648 created window. */
5649 if (w->current_matrix == NULL)
5650 return;
5651
06a2c219
GM
5652 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5653 r->x, r->y, r->width, r->height));
dc6f92b8 5654
06a2c219
GM
5655 /* Convert to window coordinates. */
5656 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5657 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
dc6f92b8 5658
06a2c219
GM
5659 /* Turn off the cursor. */
5660 if (!w->pseudo_window_p
5661 && x_phys_cursor_in_rect_p (w, r))
5662 {
5663 x_clear_cursor (w);
5664 cursor_cleared_p = 1;
5665 }
5666 else
5667 cursor_cleared_p = 0;
5668
5669 /* Find the first row intersecting the rectangle R. */
5670 row = w->current_matrix->rows;
5671 y = 0;
5672 while (row->enabled_p
5673 && y < yb
5674 && y + row->height < r->y)
5675 {
5676 y += row->height;
5677 ++row;
5678 }
5679
dc6f92b8 5680 /* Display the text in the rectangle, one text line at a time. */
06a2c219
GM
5681 while (row->enabled_p
5682 && y < yb
5683 && y < r->y + r->height)
5684 {
5685 expose_line (w, row, r);
5686 y += row->height;
5687 ++row;
5688 }
5689
5690 /* Display the mode line if there is one. */
5691 if (WINDOW_WANTS_MODELINE_P (w)
5692 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5693 row->enabled_p)
5694 && row->y < r->y + r->height)
5695 expose_line (w, row, r);
dc6f92b8 5696
06a2c219 5697 if (!w->pseudo_window_p)
dc6f92b8 5698 {
06a2c219
GM
5699 /* Draw border between windows. */
5700 x_draw_vertical_border (w);
5701
5702 /* Turn the cursor on again. */
5703 if (cursor_cleared_p)
5704 x_update_window_cursor (w, 1);
5705 }
5706}
dc6f92b8 5707
dc6f92b8 5708
06a2c219
GM
5709/* Determine the intersection of two rectangles R1 and R2. Return
5710 the intersection in *RESULT. Value is non-zero if RESULT is not
5711 empty. */
5712
5713static int
5714x_intersect_rectangles (r1, r2, result)
5715 XRectangle *r1, *r2, *result;
5716{
5717 XRectangle *left, *right;
5718 XRectangle *upper, *lower;
5719 int intersection_p = 0;
5720
5721 /* Rearrange so that R1 is the left-most rectangle. */
5722 if (r1->x < r2->x)
5723 left = r1, right = r2;
5724 else
5725 left = r2, right = r1;
5726
5727 /* X0 of the intersection is right.x0, if this is inside R1,
5728 otherwise there is no intersection. */
5729 if (right->x <= left->x + left->width)
5730 {
5731 result->x = right->x;
5732
5733 /* The right end of the intersection is the minimum of the
5734 the right ends of left and right. */
5735 result->width = (min (left->x + left->width, right->x + right->width)
5736 - result->x);
5737
5738 /* Same game for Y. */
5739 if (r1->y < r2->y)
5740 upper = r1, lower = r2;
5741 else
5742 upper = r2, lower = r1;
5743
5744 /* The upper end of the intersection is lower.y0, if this is inside
5745 of upper. Otherwise, there is no intersection. */
5746 if (lower->y <= upper->y + upper->height)
dc43ef94 5747 {
06a2c219
GM
5748 result->y = lower->y;
5749
5750 /* The lower end of the intersection is the minimum of the lower
5751 ends of upper and lower. */
5752 result->height = (min (lower->y + lower->height,
5753 upper->y + upper->height)
5754 - result->y);
5755 intersection_p = 1;
dc43ef94 5756 }
dc6f92b8
JB
5757 }
5758
06a2c219 5759 return intersection_p;
dc6f92b8 5760}
06a2c219
GM
5761
5762
5763
5764
dc6f92b8 5765\f
dc6f92b8 5766static void
334208b7
RS
5767frame_highlight (f)
5768 struct frame *f;
dc6f92b8 5769{
b3e1e05c
JB
5770 /* We used to only do this if Vx_no_window_manager was non-nil, but
5771 the ICCCM (section 4.1.6) says that the window's border pixmap
5772 and border pixel are window attributes which are "private to the
5773 client", so we can always change it to whatever we want. */
5774 BLOCK_INPUT;
334208b7 5775 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5776 f->output_data.x->border_pixel);
b3e1e05c 5777 UNBLOCK_INPUT;
5d46f928 5778 x_update_cursor (f, 1);
dc6f92b8
JB
5779}
5780
5781static void
334208b7
RS
5782frame_unhighlight (f)
5783 struct frame *f;
dc6f92b8 5784{
b3e1e05c
JB
5785 /* We used to only do this if Vx_no_window_manager was non-nil, but
5786 the ICCCM (section 4.1.6) says that the window's border pixmap
5787 and border pixel are window attributes which are "private to the
5788 client", so we can always change it to whatever we want. */
5789 BLOCK_INPUT;
334208b7 5790 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5791 f->output_data.x->border_tile);
b3e1e05c 5792 UNBLOCK_INPUT;
5d46f928 5793 x_update_cursor (f, 1);
dc6f92b8 5794}
dc6f92b8 5795
f676886a
JB
5796/* The focus has changed. Update the frames as necessary to reflect
5797 the new situation. Note that we can't change the selected frame
c5acd733 5798 here, because the Lisp code we are interrupting might become confused.
eb8c3be9 5799 Each event gets marked with the frame in which it occurred, so the
c5acd733 5800 Lisp code can tell when the switch took place by examining the events. */
dc6f92b8 5801
6d4238f3 5802static void
0f941935
KH
5803x_new_focus_frame (dpyinfo, frame)
5804 struct x_display_info *dpyinfo;
f676886a 5805 struct frame *frame;
dc6f92b8 5806{
0f941935 5807 struct frame *old_focus = dpyinfo->x_focus_frame;
dc6f92b8 5808
0f941935 5809 if (frame != dpyinfo->x_focus_frame)
dc6f92b8 5810 {
58769bee 5811 /* Set this before calling other routines, so that they see
f676886a 5812 the correct value of x_focus_frame. */
0f941935 5813 dpyinfo->x_focus_frame = frame;
6d4238f3
JB
5814
5815 if (old_focus && old_focus->auto_lower)
f676886a 5816 x_lower_frame (old_focus);
dc6f92b8
JB
5817
5818#if 0
f676886a 5819 selected_frame = frame;
e0c1aef2
KH
5820 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
5821 selected_frame);
f676886a
JB
5822 Fselect_window (selected_frame->selected_window);
5823 choose_minibuf_frame ();
c118dd06 5824#endif /* ! 0 */
dc6f92b8 5825
0f941935
KH
5826 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
5827 pending_autoraise_frame = dpyinfo->x_focus_frame;
0134a210
RS
5828 else
5829 pending_autoraise_frame = 0;
6d4238f3 5830 }
dc6f92b8 5831
0f941935 5832 x_frame_rehighlight (dpyinfo);
6d4238f3
JB
5833}
5834
37c2c98b
RS
5835/* Handle an event saying the mouse has moved out of an Emacs frame. */
5836
5837void
0f941935
KH
5838x_mouse_leave (dpyinfo)
5839 struct x_display_info *dpyinfo;
37c2c98b 5840{
0f941935 5841 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
37c2c98b 5842}
6d4238f3 5843
f451eb13
JB
5844/* The focus has changed, or we have redirected a frame's focus to
5845 another frame (this happens when a frame uses a surrogate
06a2c219 5846 mini-buffer frame). Shift the highlight as appropriate.
0f941935
KH
5847
5848 The FRAME argument doesn't necessarily have anything to do with which
06a2c219 5849 frame is being highlighted or un-highlighted; we only use it to find
0f941935 5850 the appropriate X display info. */
06a2c219 5851
6d4238f3 5852static void
0f941935
KH
5853XTframe_rehighlight (frame)
5854 struct frame *frame;
6d4238f3 5855{
0f941935
KH
5856 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
5857}
6d4238f3 5858
0f941935
KH
5859static void
5860x_frame_rehighlight (dpyinfo)
5861 struct x_display_info *dpyinfo;
5862{
5863 struct frame *old_highlight = dpyinfo->x_highlight_frame;
5864
5865 if (dpyinfo->x_focus_frame)
6d4238f3 5866 {
0f941935
KH
5867 dpyinfo->x_highlight_frame
5868 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
5869 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
5870 : dpyinfo->x_focus_frame);
5871 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
f451eb13 5872 {
0f941935
KH
5873 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
5874 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
f451eb13 5875 }
dc6f92b8 5876 }
6d4238f3 5877 else
0f941935 5878 dpyinfo->x_highlight_frame = 0;
dc6f92b8 5879
0f941935 5880 if (dpyinfo->x_highlight_frame != old_highlight)
6d4238f3
JB
5881 {
5882 if (old_highlight)
f676886a 5883 frame_unhighlight (old_highlight);
0f941935
KH
5884 if (dpyinfo->x_highlight_frame)
5885 frame_highlight (dpyinfo->x_highlight_frame);
6d4238f3 5886 }
dc6f92b8 5887}
06a2c219
GM
5888
5889
dc6f92b8 5890\f
06a2c219 5891/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
dc6f92b8 5892
28430d3c
JB
5893/* Initialize mode_switch_bit and modifier_meaning. */
5894static void
334208b7
RS
5895x_find_modifier_meanings (dpyinfo)
5896 struct x_display_info *dpyinfo;
28430d3c 5897{
f689eb05 5898 int min_code, max_code;
28430d3c
JB
5899 KeySym *syms;
5900 int syms_per_code;
5901 XModifierKeymap *mods;
5902
334208b7
RS
5903 dpyinfo->meta_mod_mask = 0;
5904 dpyinfo->shift_lock_mask = 0;
5905 dpyinfo->alt_mod_mask = 0;
5906 dpyinfo->super_mod_mask = 0;
5907 dpyinfo->hyper_mod_mask = 0;
58769bee 5908
9658a521 5909#ifdef HAVE_X11R4
334208b7 5910 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
9658a521 5911#else
4a60f8c5
RS
5912 min_code = dpyinfo->display->min_keycode;
5913 max_code = dpyinfo->display->max_keycode;
9658a521
JB
5914#endif
5915
334208b7 5916 syms = XGetKeyboardMapping (dpyinfo->display,
28430d3c
JB
5917 min_code, max_code - min_code + 1,
5918 &syms_per_code);
334208b7 5919 mods = XGetModifierMapping (dpyinfo->display);
28430d3c 5920
58769bee 5921 /* Scan the modifier table to see which modifier bits the Meta and
11edeb03 5922 Alt keysyms are on. */
28430d3c 5923 {
06a2c219 5924 int row, col; /* The row and column in the modifier table. */
28430d3c
JB
5925
5926 for (row = 3; row < 8; row++)
5927 for (col = 0; col < mods->max_keypermod; col++)
5928 {
0299d313
RS
5929 KeyCode code
5930 = mods->modifiermap[(row * mods->max_keypermod) + col];
28430d3c 5931
af92970c
KH
5932 /* Zeroes are used for filler. Skip them. */
5933 if (code == 0)
5934 continue;
5935
28430d3c
JB
5936 /* Are any of this keycode's keysyms a meta key? */
5937 {
5938 int code_col;
5939
5940 for (code_col = 0; code_col < syms_per_code; code_col++)
5941 {
f689eb05 5942 int sym = syms[((code - min_code) * syms_per_code) + code_col];
28430d3c 5943
f689eb05 5944 switch (sym)
28430d3c 5945 {
f689eb05
JB
5946 case XK_Meta_L:
5947 case XK_Meta_R:
334208b7 5948 dpyinfo->meta_mod_mask |= (1 << row);
28430d3c 5949 break;
f689eb05
JB
5950
5951 case XK_Alt_L:
5952 case XK_Alt_R:
334208b7 5953 dpyinfo->alt_mod_mask |= (1 << row);
a3c44b14
RS
5954 break;
5955
5956 case XK_Hyper_L:
5957 case XK_Hyper_R:
334208b7 5958 dpyinfo->hyper_mod_mask |= (1 << row);
a3c44b14
RS
5959 break;
5960
5961 case XK_Super_L:
5962 case XK_Super_R:
334208b7 5963 dpyinfo->super_mod_mask |= (1 << row);
f689eb05 5964 break;
11edeb03
JB
5965
5966 case XK_Shift_Lock:
5967 /* Ignore this if it's not on the lock modifier. */
5968 if ((1 << row) == LockMask)
334208b7 5969 dpyinfo->shift_lock_mask = LockMask;
11edeb03 5970 break;
28430d3c
JB
5971 }
5972 }
5973 }
5974 }
5975 }
5976
f689eb05 5977 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
334208b7 5978 if (! dpyinfo->meta_mod_mask)
a3c44b14 5979 {
334208b7
RS
5980 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
5981 dpyinfo->alt_mod_mask = 0;
a3c44b14 5982 }
f689eb05 5983
148c4b70
RS
5984 /* If some keys are both alt and meta,
5985 make them just meta, not alt. */
334208b7 5986 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
148c4b70 5987 {
334208b7 5988 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
148c4b70 5989 }
58769bee 5990
28430d3c 5991 XFree ((char *) syms);
f689eb05 5992 XFreeModifiermap (mods);
28430d3c
JB
5993}
5994
dfeccd2d
JB
5995/* Convert between the modifier bits X uses and the modifier bits
5996 Emacs uses. */
06a2c219 5997
7c5283e4 5998static unsigned int
334208b7
RS
5999x_x_to_emacs_modifiers (dpyinfo, state)
6000 struct x_display_info *dpyinfo;
dc6f92b8
JB
6001 unsigned int state;
6002{
334208b7
RS
6003 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6004 | ((state & ControlMask) ? ctrl_modifier : 0)
6005 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
6006 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
6007 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
6008 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
dc6f92b8
JB
6009}
6010
dfeccd2d 6011static unsigned int
334208b7
RS
6012x_emacs_to_x_modifiers (dpyinfo, state)
6013 struct x_display_info *dpyinfo;
dfeccd2d
JB
6014 unsigned int state;
6015{
334208b7
RS
6016 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
6017 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
6018 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
6019 | ((state & shift_modifier) ? ShiftMask : 0)
6020 | ((state & ctrl_modifier) ? ControlMask : 0)
6021 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
dfeccd2d 6022}
d047c4eb
KH
6023
6024/* Convert a keysym to its name. */
6025
6026char *
6027x_get_keysym_name (keysym)
6028 KeySym keysym;
6029{
6030 char *value;
6031
6032 BLOCK_INPUT;
6033 value = XKeysymToString (keysym);
6034 UNBLOCK_INPUT;
6035
6036 return value;
6037}
06a2c219
GM
6038
6039
e4571a43
JB
6040\f
6041/* Mouse clicks and mouse movement. Rah. */
e4571a43 6042
06a2c219
GM
6043/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6044 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6045 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6046 not force the value into range. */
69388238 6047
c8dba240 6048void
69388238 6049pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
e4571a43 6050 FRAME_PTR f;
69388238 6051 register int pix_x, pix_y;
e4571a43
JB
6052 register int *x, *y;
6053 XRectangle *bounds;
69388238 6054 int noclip;
e4571a43 6055{
06a2c219 6056 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
69388238
RS
6057 even for negative values. */
6058 if (pix_x < 0)
7556890b 6059 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
69388238 6060 if (pix_y < 0)
7556890b 6061 pix_y -= (f)->output_data.x->line_height - 1;
69388238 6062
e4571a43
JB
6063 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6064 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6065
6066 if (bounds)
6067 {
7556890b
RS
6068 bounds->width = FONT_WIDTH (f->output_data.x->font);
6069 bounds->height = f->output_data.x->line_height;
e4571a43
JB
6070 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6071 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6072 }
6073
69388238
RS
6074 if (!noclip)
6075 {
6076 if (pix_x < 0)
6077 pix_x = 0;
3cbd2e0b
RS
6078 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6079 pix_x = FRAME_WINDOW_WIDTH (f);
69388238
RS
6080
6081 if (pix_y < 0)
6082 pix_y = 0;
6083 else if (pix_y > f->height)
6084 pix_y = f->height;
6085 }
e4571a43
JB
6086
6087 *x = pix_x;
6088 *y = pix_y;
6089}
6090
06a2c219
GM
6091
6092/* Given HPOS/VPOS in the current matrix of W, return corresponding
6093 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6094 can't tell the positions because W's display is not up to date,
6095 return 0. */
6096
6097int
6098glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6099 struct window *w;
6100 int hpos, vpos;
6101 int *frame_x, *frame_y;
2b5c9e71 6102{
06a2c219
GM
6103 int success_p;
6104
6105 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6106 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6107
6108 if (display_completed)
6109 {
6110 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6111 struct glyph *glyph = row->glyphs[TEXT_AREA];
6112 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6113
6114 *frame_y = row->y;
6115 *frame_x = row->x;
6116 while (glyph < end)
6117 {
6118 *frame_x += glyph->pixel_width;
6119 ++glyph;
6120 }
6121
6122 success_p = 1;
6123 }
6124 else
6125 {
6126 *frame_y = *frame_x = 0;
6127 success_p = 0;
6128 }
6129
6130 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6131 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6132 return success_p;
2b5c9e71
RS
6133}
6134
06a2c219 6135
dc6f92b8
JB
6136/* Prepare a mouse-event in *RESULT for placement in the input queue.
6137
6138 If the event is a button press, then note that we have grabbed
f451eb13 6139 the mouse. */
dc6f92b8
JB
6140
6141static Lisp_Object
f451eb13 6142construct_mouse_click (result, event, f)
dc6f92b8
JB
6143 struct input_event *result;
6144 XButtonEvent *event;
f676886a 6145 struct frame *f;
dc6f92b8 6146{
f451eb13 6147 /* Make the event type no_event; we'll change that when we decide
dc6f92b8 6148 otherwise. */
f451eb13 6149 result->kind = mouse_click;
69388238 6150 result->code = event->button - Button1;
1113d9db 6151 result->timestamp = event->time;
334208b7
RS
6152 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6153 event->state)
f689eb05 6154 | (event->type == ButtonRelease
58769bee 6155 ? up_modifier
f689eb05 6156 : down_modifier));
dc6f92b8 6157
06a2c219
GM
6158 XSETINT (result->x, event->x);
6159 XSETINT (result->y, event->y);
6160 XSETFRAME (result->frame_or_window, f);
0f8aabe9 6161 result->arg = Qnil;
06a2c219 6162 return Qnil;
dc6f92b8 6163}
b849c413 6164
69388238 6165\f
90e65f07
JB
6166/* Function to report a mouse movement to the mainstream Emacs code.
6167 The input handler calls this.
6168
6169 We have received a mouse movement event, which is given in *event.
6170 If the mouse is over a different glyph than it was last time, tell
6171 the mainstream emacs code by setting mouse_moved. If not, ask for
6172 another motion event, so we can check again the next time it moves. */
b8009dd1 6173
06a2c219
GM
6174static XMotionEvent last_mouse_motion_event;
6175static Lisp_Object last_mouse_motion_frame;
6176
90e65f07 6177static void
12ba150f 6178note_mouse_movement (frame, event)
f676886a 6179 FRAME_PTR frame;
90e65f07 6180 XMotionEvent *event;
90e65f07 6181{
e5d77022 6182 last_mouse_movement_time = event->time;
06a2c219
GM
6183 last_mouse_motion_event = *event;
6184 XSETFRAME (last_mouse_motion_frame, frame);
e5d77022 6185
27f338af
RS
6186 if (event->window != FRAME_X_WINDOW (frame))
6187 {
39d8bb4d 6188 frame->mouse_moved = 1;
27f338af 6189 last_mouse_scroll_bar = Qnil;
27f338af 6190 note_mouse_highlight (frame, -1, -1);
27f338af
RS
6191 }
6192
90e65f07 6193 /* Has the mouse moved off the glyph it was on at the last sighting? */
27f338af
RS
6194 else if (event->x < last_mouse_glyph.x
6195 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6196 || event->y < last_mouse_glyph.y
6197 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
12ba150f 6198 {
39d8bb4d 6199 frame->mouse_moved = 1;
ab648270 6200 last_mouse_scroll_bar = Qnil;
b8009dd1 6201 note_mouse_highlight (frame, event->x, event->y);
90e65f07
JB
6202 }
6203}
6204
bf1c0ba1 6205/* This is used for debugging, to turn off note_mouse_highlight. */
bf1c0ba1 6206
06a2c219
GM
6207 int disable_mouse_highlight;
6208
6209
6210\f
6211/************************************************************************
6212 Mouse Face
6213 ************************************************************************/
6214
6215/* Find the glyph under window-relative coordinates X/Y in window W.
6216 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6217 strings. Return in *HPOS and *VPOS the row and column number of
6218 the glyph found. Return in *AREA the glyph area containing X.
6219 Value is a pointer to the glyph found or null if X/Y is not on
6220 text, or we can't tell because W's current matrix is not up to
6221 date. */
6222
6223static struct glyph *
6224x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6225 struct window *w;
6226 int x, y;
6227 int *hpos, *vpos, *area;
6228{
6229 struct glyph *glyph, *end;
3e71d8f2 6230 struct glyph_row *row = NULL;
06a2c219
GM
6231 int x0, i, left_area_width;
6232
6233 /* Find row containing Y. Give up if some row is not enabled. */
6234 for (i = 0; i < w->current_matrix->nrows; ++i)
6235 {
6236 row = MATRIX_ROW (w->current_matrix, i);
6237 if (!row->enabled_p)
6238 return NULL;
6239 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6240 break;
6241 }
6242
6243 *vpos = i;
6244 *hpos = 0;
6245
6246 /* Give up if Y is not in the window. */
6247 if (i == w->current_matrix->nrows)
6248 return NULL;
6249
6250 /* Get the glyph area containing X. */
6251 if (w->pseudo_window_p)
6252 {
6253 *area = TEXT_AREA;
6254 x0 = 0;
6255 }
6256 else
6257 {
6258 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6259 if (x < left_area_width)
6260 {
6261 *area = LEFT_MARGIN_AREA;
6262 x0 = 0;
6263 }
6264 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6265 {
6266 *area = TEXT_AREA;
6267 x0 = row->x + left_area_width;
6268 }
6269 else
6270 {
6271 *area = RIGHT_MARGIN_AREA;
6272 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6273 }
6274 }
6275
6276 /* Find glyph containing X. */
6277 glyph = row->glyphs[*area];
6278 end = glyph + row->used[*area];
6279 while (glyph < end)
6280 {
6281 if (x < x0 + glyph->pixel_width)
6282 {
6283 if (w->pseudo_window_p)
6284 break;
6285 else if (BUFFERP (glyph->object))
6286 break;
6287 }
6288
6289 x0 += glyph->pixel_width;
6290 ++glyph;
6291 }
6292
6293 if (glyph == end)
6294 return NULL;
6295
6296 *hpos = glyph - row->glyphs[*area];
6297 return glyph;
6298}
6299
6300
6301/* Convert frame-relative x/y to coordinates relative to window W.
6302 Takes pseudo-windows into account. */
6303
6304static void
6305frame_to_window_pixel_xy (w, x, y)
6306 struct window *w;
6307 int *x, *y;
6308{
6309 if (w->pseudo_window_p)
6310 {
6311 /* A pseudo-window is always full-width, and starts at the
6312 left edge of the frame, plus a frame border. */
6313 struct frame *f = XFRAME (w->frame);
6314 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6315 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6316 }
6317 else
6318 {
6319 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6320 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6321 }
6322}
6323
6324
6325/* Take proper action when mouse has moved to the mode or top line of
6326 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6327 mode line. X is relative to the start of the text display area of
6328 W, so the width of bitmap areas and scroll bars must be subtracted
6329 to get a position relative to the start of the mode line. */
6330
6331static void
6332note_mode_line_highlight (w, x, mode_line_p)
6333 struct window *w;
6334 int x, mode_line_p;
6335{
6336 struct frame *f = XFRAME (w->frame);
6337 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6338 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6339 struct glyph_row *row;
6340
6341 if (mode_line_p)
6342 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6343 else
045dee35 6344 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
06a2c219
GM
6345
6346 if (row->enabled_p)
6347 {
6348 struct glyph *glyph, *end;
6349 Lisp_Object help, map;
6350 int x0;
6351
6352 /* Find the glyph under X. */
6353 glyph = row->glyphs[TEXT_AREA];
6354 end = glyph + row->used[TEXT_AREA];
6355 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
110859fc 6356 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
06a2c219
GM
6357 while (glyph < end
6358 && x >= x0 + glyph->pixel_width)
6359 {
6360 x0 += glyph->pixel_width;
6361 ++glyph;
6362 }
6363
6364 if (glyph < end
6365 && STRINGP (glyph->object)
6366 && XSTRING (glyph->object)->intervals
6367 && glyph->charpos >= 0
6368 && glyph->charpos < XSTRING (glyph->object)->size)
6369 {
6370 /* If we're on a string with `help-echo' text property,
6371 arrange for the help to be displayed. This is done by
6372 setting the global variable help_echo to the help string. */
6373 help = Fget_text_property (make_number (glyph->charpos),
6374 Qhelp_echo, glyph->object);
b7e80413 6375 if (!NILP (help))
be010514
GM
6376 {
6377 help_echo = help;
7cea38bc 6378 XSETWINDOW (help_echo_window, w);
be010514
GM
6379 help_echo_object = glyph->object;
6380 help_echo_pos = glyph->charpos;
6381 }
06a2c219
GM
6382
6383 /* Change the mouse pointer according to what is under X/Y. */
6384 map = Fget_text_property (make_number (glyph->charpos),
6385 Qlocal_map, glyph->object);
6386 if (!NILP (Fkeymapp (map)))
6387 cursor = f->output_data.x->nontext_cursor;
be010514
GM
6388 else
6389 {
6390 map = Fget_text_property (make_number (glyph->charpos),
6391 Qkeymap, glyph->object);
6392 if (!NILP (Fkeymapp (map)))
6393 cursor = f->output_data.x->nontext_cursor;
6394 }
06a2c219
GM
6395 }
6396 }
6397
6398 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6399}
6400
6401
6402/* Take proper action when the mouse has moved to position X, Y on
6403 frame F as regards highlighting characters that have mouse-face
6404 properties. Also de-highlighting chars where the mouse was before.
27f338af 6405 X and Y can be negative or out of range. */
b8009dd1
RS
6406
6407static void
6408note_mouse_highlight (f, x, y)
06a2c219 6409 struct frame *f;
c32cdd9a 6410 int x, y;
b8009dd1 6411{
06a2c219
GM
6412 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6413 int portion;
b8009dd1
RS
6414 Lisp_Object window;
6415 struct window *w;
6416
06a2c219
GM
6417 /* When a menu is active, don't highlight because this looks odd. */
6418#ifdef USE_X_TOOLKIT
6419 if (popup_activated ())
6420 return;
6421#endif
6422
04fff9c0
GM
6423 if (disable_mouse_highlight
6424 || !f->glyphs_initialized_p)
bf1c0ba1
RS
6425 return;
6426
06a2c219
GM
6427 dpyinfo->mouse_face_mouse_x = x;
6428 dpyinfo->mouse_face_mouse_y = y;
6429 dpyinfo->mouse_face_mouse_frame = f;
b8009dd1 6430
06a2c219 6431 if (dpyinfo->mouse_face_defer)
b8009dd1
RS
6432 return;
6433
514e4681
RS
6434 if (gc_in_progress)
6435 {
06a2c219 6436 dpyinfo->mouse_face_deferred_gc = 1;
514e4681
RS
6437 return;
6438 }
6439
b8009dd1 6440 /* Which window is that in? */
06a2c219 6441 window = window_from_coordinates (f, x, y, &portion, 1);
b8009dd1
RS
6442
6443 /* If we were displaying active text in another window, clear that. */
06a2c219
GM
6444 if (! EQ (window, dpyinfo->mouse_face_window))
6445 clear_mouse_face (dpyinfo);
6446
6447 /* Not on a window -> return. */
6448 if (!WINDOWP (window))
6449 return;
6450
6451 /* Convert to window-relative pixel coordinates. */
6452 w = XWINDOW (window);
6453 frame_to_window_pixel_xy (w, &x, &y);
6454
9ea173e8 6455 /* Handle tool-bar window differently since it doesn't display a
06a2c219 6456 buffer. */
9ea173e8 6457 if (EQ (window, f->tool_bar_window))
06a2c219 6458 {
9ea173e8 6459 note_tool_bar_highlight (f, x, y);
06a2c219
GM
6460 return;
6461 }
6462
6463 if (portion == 1 || portion == 3)
6464 {
6465 /* Mouse is on the mode or top line. */
6466 note_mode_line_highlight (w, x, portion == 1);
6467 return;
6468 }
6469 else
6470 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6471 f->output_data.x->text_cursor);
b8009dd1 6472
0cdd0c9f
RS
6473 /* Are we in a window whose display is up to date?
6474 And verify the buffer's text has not changed. */
06a2c219
GM
6475 if (/* Within text portion of the window. */
6476 portion == 0
0cdd0c9f 6477 && EQ (w->window_end_valid, w->buffer)
26459b28
KH
6478 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6479 && (XFASTINT (w->last_overlay_modified)
6480 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
b8009dd1 6481 {
06a2c219
GM
6482 int hpos, vpos, pos, i, area;
6483 struct glyph *glyph;
b8009dd1 6484
06a2c219
GM
6485 /* Find the glyph under X/Y. */
6486 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6487
6488 /* Clear mouse face if X/Y not over text. */
6489 if (glyph == NULL
6490 || area != TEXT_AREA
6491 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
b8009dd1 6492 {
06a2c219
GM
6493 clear_mouse_face (dpyinfo);
6494 return;
6495 }
6496
6497 pos = glyph->charpos;
6498 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6499
6500 /* Check for mouse-face and help-echo. */
6501 {
6502 Lisp_Object mouse_face, overlay, position;
6503 Lisp_Object *overlay_vec;
6504 int len, noverlays;
6505 struct buffer *obuf;
6506 int obegv, ozv;
6507
6508 /* If we get an out-of-range value, return now; avoid an error. */
6509 if (pos > BUF_Z (XBUFFER (w->buffer)))
6510 return;
6511
6512 /* Make the window's buffer temporarily current for
6513 overlays_at and compute_char_face. */
6514 obuf = current_buffer;
6515 current_buffer = XBUFFER (w->buffer);
6516 obegv = BEGV;
6517 ozv = ZV;
6518 BEGV = BEG;
6519 ZV = Z;
6520
6521 /* Is this char mouse-active or does it have help-echo? */
6522 XSETINT (position, pos);
6523
6524 /* Put all the overlays we want in a vector in overlay_vec.
6525 Store the length in len. If there are more than 10, make
6526 enough space for all, and try again. */
6527 len = 10;
6528 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
bc592036 6529 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
06a2c219
GM
6530 if (noverlays > len)
6531 {
6532 len = noverlays;
6533 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
bc592036 6534 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
06a2c219 6535 }
f8349001
GM
6536
6537 /* Sort overlays into increasing priority order. */
06a2c219
GM
6538 noverlays = sort_overlays (overlay_vec, noverlays, w);
6539
6540 /* Check mouse-face highlighting. */
6541 if (! (EQ (window, dpyinfo->mouse_face_window)
6542 && vpos >= dpyinfo->mouse_face_beg_row
6543 && vpos <= dpyinfo->mouse_face_end_row
6544 && (vpos > dpyinfo->mouse_face_beg_row
6545 || hpos >= dpyinfo->mouse_face_beg_col)
6546 && (vpos < dpyinfo->mouse_face_end_row
6547 || hpos < dpyinfo->mouse_face_end_col
6548 || dpyinfo->mouse_face_past_end)))
6549 {
6550 /* Clear the display of the old active region, if any. */
6551 clear_mouse_face (dpyinfo);
6552
6553 /* Find the highest priority overlay that has a mouse-face prop. */
6554 overlay = Qnil;
f8349001 6555 for (i = noverlays - 1; i >= 0; --i)
06a2c219
GM
6556 {
6557 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6558 if (!NILP (mouse_face))
6559 {
6560 overlay = overlay_vec[i];
6561 break;
6562 }
6563 }
6564
6565 /* If no overlay applies, get a text property. */
6566 if (NILP (overlay))
6567 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6568
6569 /* Handle the overlay case. */
6570 if (! NILP (overlay))
6571 {
6572 /* Find the range of text around this char that
6573 should be active. */
6574 Lisp_Object before, after;
6575 int ignore;
6576
6577 before = Foverlay_start (overlay);
6578 after = Foverlay_end (overlay);
6579 /* Record this as the current active region. */
6580 fast_find_position (w, XFASTINT (before),
6581 &dpyinfo->mouse_face_beg_col,
6582 &dpyinfo->mouse_face_beg_row,
6583 &dpyinfo->mouse_face_beg_x,
6584 &dpyinfo->mouse_face_beg_y);
6585 dpyinfo->mouse_face_past_end
6586 = !fast_find_position (w, XFASTINT (after),
6587 &dpyinfo->mouse_face_end_col,
6588 &dpyinfo->mouse_face_end_row,
6589 &dpyinfo->mouse_face_end_x,
6590 &dpyinfo->mouse_face_end_y);
6591 dpyinfo->mouse_face_window = window;
6592 dpyinfo->mouse_face_face_id
6593 = face_at_buffer_position (w, pos, 0, 0,
6594 &ignore, pos + 1, 1);
6595
6596 /* Display it as active. */
6597 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6598 }
6599 /* Handle the text property case. */
6600 else if (! NILP (mouse_face))
6601 {
6602 /* Find the range of text around this char that
6603 should be active. */
6604 Lisp_Object before, after, beginning, end;
6605 int ignore;
6606
6607 beginning = Fmarker_position (w->start);
6608 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6609 - XFASTINT (w->window_end_pos)));
6610 before
6611 = Fprevious_single_property_change (make_number (pos + 1),
6612 Qmouse_face,
6613 w->buffer, beginning);
6614 after
6615 = Fnext_single_property_change (position, Qmouse_face,
6616 w->buffer, end);
6617 /* Record this as the current active region. */
6618 fast_find_position (w, XFASTINT (before),
6619 &dpyinfo->mouse_face_beg_col,
6620 &dpyinfo->mouse_face_beg_row,
6621 &dpyinfo->mouse_face_beg_x,
6622 &dpyinfo->mouse_face_beg_y);
6623 dpyinfo->mouse_face_past_end
6624 = !fast_find_position (w, XFASTINT (after),
6625 &dpyinfo->mouse_face_end_col,
6626 &dpyinfo->mouse_face_end_row,
6627 &dpyinfo->mouse_face_end_x,
6628 &dpyinfo->mouse_face_end_y);
6629 dpyinfo->mouse_face_window = window;
6630 dpyinfo->mouse_face_face_id
6631 = face_at_buffer_position (w, pos, 0, 0,
6632 &ignore, pos + 1, 1);
6633
6634 /* Display it as active. */
6635 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6636 }
6637 }
6638
6639 /* Look for a `help-echo' property. */
6640 {
743934db 6641 Lisp_Object help, overlay;
06a2c219
GM
6642
6643 /* Check overlays first. */
6644 help = Qnil;
f8349001 6645 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
743934db
GM
6646 {
6647 overlay = overlay_vec[i];
6648 help = Foverlay_get (overlay, Qhelp_echo);
6649 }
be010514
GM
6650
6651 if (!NILP (help))
6652 {
6653 help_echo = help;
7cea38bc 6654 help_echo_window = window;
743934db 6655 help_echo_object = overlay;
be010514
GM
6656 help_echo_pos = pos;
6657 }
6658 else
6659 {
6660 /* Try text properties. */
6661 if ((STRINGP (glyph->object)
06a2c219
GM
6662 && glyph->charpos >= 0
6663 && glyph->charpos < XSTRING (glyph->object)->size)
6664 || (BUFFERP (glyph->object)
6665 && glyph->charpos >= BEGV
be010514
GM
6666 && glyph->charpos < ZV))
6667 help = Fget_text_property (make_number (glyph->charpos),
6668 Qhelp_echo, glyph->object);
06a2c219 6669
be010514
GM
6670 if (!NILP (help))
6671 {
6672 help_echo = help;
7cea38bc 6673 help_echo_window = window;
be010514
GM
6674 help_echo_object = glyph->object;
6675 help_echo_pos = glyph->charpos;
6676 }
6677 }
06a2c219
GM
6678 }
6679
6680 BEGV = obegv;
6681 ZV = ozv;
6682 current_buffer = obuf;
6683 }
6684 }
6685}
6686
6687static void
6688redo_mouse_highlight ()
6689{
6690 if (!NILP (last_mouse_motion_frame)
6691 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6692 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6693 last_mouse_motion_event.x,
6694 last_mouse_motion_event.y);
6695}
6696
6697
6698\f
6699/***********************************************************************
9ea173e8 6700 Tool-bars
06a2c219
GM
6701 ***********************************************************************/
6702
9ea173e8
GM
6703static int x_tool_bar_item P_ ((struct frame *, int, int,
6704 struct glyph **, int *, int *, int *));
06a2c219 6705
9ea173e8 6706/* Tool-bar item index of the item on which a mouse button was pressed
06a2c219
GM
6707 or -1. */
6708
9ea173e8 6709static int last_tool_bar_item;
06a2c219
GM
6710
6711
9ea173e8
GM
6712/* Get information about the tool-bar item at position X/Y on frame F.
6713 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6714 the current matrix of the tool-bar window of F, or NULL if not
6715 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
6716 item in F->current_tool_bar_items. Value is
06a2c219 6717
9ea173e8 6718 -1 if X/Y is not on a tool-bar item
06a2c219
GM
6719 0 if X/Y is on the same item that was highlighted before.
6720 1 otherwise. */
6721
6722static int
9ea173e8 6723x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
06a2c219
GM
6724 struct frame *f;
6725 int x, y;
6726 struct glyph **glyph;
6727 int *hpos, *vpos, *prop_idx;
6728{
6729 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6730 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6731 int area;
6732
6733 /* Find the glyph under X/Y. */
6734 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6735 if (*glyph == NULL)
6736 return -1;
6737
9ea173e8
GM
6738 /* Get the start of this tool-bar item's properties in
6739 f->current_tool_bar_items. */
6740 if (!tool_bar_item_info (f, *glyph, prop_idx))
06a2c219
GM
6741 return -1;
6742
6743 /* Is mouse on the highlighted item? */
9ea173e8 6744 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
06a2c219
GM
6745 && *vpos >= dpyinfo->mouse_face_beg_row
6746 && *vpos <= dpyinfo->mouse_face_end_row
6747 && (*vpos > dpyinfo->mouse_face_beg_row
6748 || *hpos >= dpyinfo->mouse_face_beg_col)
6749 && (*vpos < dpyinfo->mouse_face_end_row
6750 || *hpos < dpyinfo->mouse_face_end_col
6751 || dpyinfo->mouse_face_past_end))
6752 return 0;
6753
6754 return 1;
6755}
6756
6757
9ea173e8 6758/* Handle mouse button event on the tool-bar of frame F, at
06a2c219
GM
6759 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6760 or ButtonRelase. */
6761
6762static void
9ea173e8 6763x_handle_tool_bar_click (f, button_event)
06a2c219
GM
6764 struct frame *f;
6765 XButtonEvent *button_event;
6766{
6767 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6768 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6769 int hpos, vpos, prop_idx;
6770 struct glyph *glyph;
6771 Lisp_Object enabled_p;
6772 int x = button_event->x;
6773 int y = button_event->y;
6774
9ea173e8 6775 /* If not on the highlighted tool-bar item, return. */
06a2c219 6776 frame_to_window_pixel_xy (w, &x, &y);
9ea173e8 6777 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
06a2c219
GM
6778 return;
6779
6780 /* If item is disabled, do nothing. */
9ea173e8
GM
6781 enabled_p = (XVECTOR (f->current_tool_bar_items)
6782 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
06a2c219
GM
6783 if (NILP (enabled_p))
6784 return;
6785
6786 if (button_event->type == ButtonPress)
6787 {
6788 /* Show item in pressed state. */
6789 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6790 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9ea173e8 6791 last_tool_bar_item = prop_idx;
06a2c219
GM
6792 }
6793 else
6794 {
6795 Lisp_Object key, frame;
6796 struct input_event event;
6797
6798 /* Show item in released state. */
6799 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6800 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6801
9ea173e8
GM
6802 key = (XVECTOR (f->current_tool_bar_items)
6803 ->contents[prop_idx + TOOL_BAR_ITEM_KEY]);
06a2c219
GM
6804
6805 XSETFRAME (frame, f);
9ea173e8 6806 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
6807 event.frame_or_window = frame;
6808 event.arg = frame;
06a2c219
GM
6809 kbd_buffer_store_event (&event);
6810
9ea173e8 6811 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
6812 event.frame_or_window = frame;
6813 event.arg = key;
06a2c219
GM
6814 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6815 button_event->state);
6816 kbd_buffer_store_event (&event);
9ea173e8 6817 last_tool_bar_item = -1;
06a2c219
GM
6818 }
6819}
6820
6821
9ea173e8
GM
6822/* Possibly highlight a tool-bar item on frame F when mouse moves to
6823 tool-bar window-relative coordinates X/Y. Called from
06a2c219
GM
6824 note_mouse_highlight. */
6825
6826static void
9ea173e8 6827note_tool_bar_highlight (f, x, y)
06a2c219
GM
6828 struct frame *f;
6829 int x, y;
6830{
9ea173e8 6831 Lisp_Object window = f->tool_bar_window;
06a2c219
GM
6832 struct window *w = XWINDOW (window);
6833 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6834 int hpos, vpos;
6835 struct glyph *glyph;
6836 struct glyph_row *row;
5c187dee 6837 int i;
06a2c219
GM
6838 Lisp_Object enabled_p;
6839 int prop_idx;
6840 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
5c187dee 6841 int mouse_down_p, rc;
06a2c219
GM
6842
6843 /* Function note_mouse_highlight is called with negative x(y
6844 values when mouse moves outside of the frame. */
6845 if (x <= 0 || y <= 0)
6846 {
6847 clear_mouse_face (dpyinfo);
6848 return;
6849 }
6850
9ea173e8 6851 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
06a2c219
GM
6852 if (rc < 0)
6853 {
9ea173e8 6854 /* Not on tool-bar item. */
06a2c219
GM
6855 clear_mouse_face (dpyinfo);
6856 return;
6857 }
6858 else if (rc == 0)
9ea173e8 6859 /* On same tool-bar item as before. */
06a2c219 6860 goto set_help_echo;
b8009dd1 6861
06a2c219
GM
6862 clear_mouse_face (dpyinfo);
6863
9ea173e8 6864 /* Mouse is down, but on different tool-bar item? */
06a2c219
GM
6865 mouse_down_p = (dpyinfo->grabbed
6866 && f == last_mouse_frame
6867 && FRAME_LIVE_P (f));
6868 if (mouse_down_p
9ea173e8 6869 && last_tool_bar_item != prop_idx)
06a2c219
GM
6870 return;
6871
6872 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
6873 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
6874
9ea173e8
GM
6875 /* If tool-bar item is not enabled, don't highlight it. */
6876 enabled_p = (XVECTOR (f->current_tool_bar_items)
6877 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
06a2c219
GM
6878 if (!NILP (enabled_p))
6879 {
6880 /* Compute the x-position of the glyph. In front and past the
6881 image is a space. We include this is the highlighted area. */
6882 row = MATRIX_ROW (w->current_matrix, vpos);
6883 for (i = x = 0; i < hpos; ++i)
6884 x += row->glyphs[TEXT_AREA][i].pixel_width;
6885
6886 /* Record this as the current active region. */
6887 dpyinfo->mouse_face_beg_col = hpos;
6888 dpyinfo->mouse_face_beg_row = vpos;
6889 dpyinfo->mouse_face_beg_x = x;
6890 dpyinfo->mouse_face_beg_y = row->y;
6891 dpyinfo->mouse_face_past_end = 0;
6892
6893 dpyinfo->mouse_face_end_col = hpos + 1;
6894 dpyinfo->mouse_face_end_row = vpos;
6895 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
6896 dpyinfo->mouse_face_end_y = row->y;
6897 dpyinfo->mouse_face_window = window;
9ea173e8 6898 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
06a2c219
GM
6899
6900 /* Display it as active. */
6901 show_mouse_face (dpyinfo, draw);
6902 dpyinfo->mouse_face_image_state = draw;
b8009dd1 6903 }
06a2c219
GM
6904
6905 set_help_echo:
6906
9ea173e8 6907 /* Set help_echo to a help string.to display for this tool-bar item.
06a2c219 6908 XTread_socket does the rest. */
7cea38bc 6909 help_echo_object = help_echo_window = Qnil;
be010514 6910 help_echo_pos = -1;
9ea173e8
GM
6911 help_echo = (XVECTOR (f->current_tool_bar_items)
6912 ->contents[prop_idx + TOOL_BAR_ITEM_HELP]);
b7e80413 6913 if (NILP (help_echo))
9ea173e8
GM
6914 help_echo = (XVECTOR (f->current_tool_bar_items)
6915 ->contents[prop_idx + TOOL_BAR_ITEM_CAPTION]);
b8009dd1 6916}
4d73d038 6917
06a2c219
GM
6918
6919\f
6920/* Find the glyph matrix position of buffer position POS in window W.
6921 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
6922 current glyphs must be up to date. If POS is above window start
6923 return (0, 0, 0, 0). If POS is after end of W, return end of
6924 last line in W. */
b8009dd1
RS
6925
6926static int
06a2c219
GM
6927fast_find_position (w, pos, hpos, vpos, x, y)
6928 struct window *w;
b8009dd1 6929 int pos;
06a2c219 6930 int *hpos, *vpos, *x, *y;
b8009dd1 6931{
b8009dd1 6932 int i;
bf1c0ba1 6933 int lastcol;
06a2c219
GM
6934 int maybe_next_line_p = 0;
6935 int line_start_position;
6936 int yb = window_text_bottom_y (w);
6937 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
6938 struct glyph_row *best_row = row;
6939 int row_vpos = 0, best_row_vpos = 0;
6940 int current_x;
6941
6942 while (row->y < yb)
b8009dd1 6943 {
06a2c219
GM
6944 if (row->used[TEXT_AREA])
6945 line_start_position = row->glyphs[TEXT_AREA]->charpos;
6946 else
6947 line_start_position = 0;
6948
6949 if (line_start_position > pos)
b8009dd1 6950 break;
77b68646
RS
6951 /* If the position sought is the end of the buffer,
6952 don't include the blank lines at the bottom of the window. */
06a2c219
GM
6953 else if (line_start_position == pos
6954 && pos == BUF_ZV (XBUFFER (w->buffer)))
77b68646 6955 {
06a2c219 6956 maybe_next_line_p = 1;
77b68646
RS
6957 break;
6958 }
06a2c219
GM
6959 else if (line_start_position > 0)
6960 {
6961 best_row = row;
6962 best_row_vpos = row_vpos;
6963 }
4b0bb6f3
GM
6964
6965 if (row->y + row->height >= yb)
6966 break;
06a2c219
GM
6967
6968 ++row;
6969 ++row_vpos;
b8009dd1 6970 }
06a2c219
GM
6971
6972 /* Find the right column within BEST_ROW. */
6973 lastcol = 0;
6974 current_x = best_row->x;
6975 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
bf1c0ba1 6976 {
06a2c219
GM
6977 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
6978 int charpos;
6979
6980 charpos = glyph->charpos;
6981 if (charpos == pos)
bf1c0ba1 6982 {
06a2c219
GM
6983 *hpos = i;
6984 *vpos = best_row_vpos;
6985 *x = current_x;
6986 *y = best_row->y;
bf1c0ba1
RS
6987 return 1;
6988 }
06a2c219 6989 else if (charpos > pos)
4d73d038 6990 break;
06a2c219
GM
6991 else if (charpos > 0)
6992 lastcol = i;
6993
6994 current_x += glyph->pixel_width;
bf1c0ba1 6995 }
b8009dd1 6996
77b68646
RS
6997 /* If we're looking for the end of the buffer,
6998 and we didn't find it in the line we scanned,
6999 use the start of the following line. */
06a2c219 7000 if (maybe_next_line_p)
77b68646 7001 {
06a2c219
GM
7002 ++best_row;
7003 ++best_row_vpos;
7004 lastcol = 0;
7005 current_x = best_row->x;
77b68646
RS
7006 }
7007
06a2c219
GM
7008 *vpos = best_row_vpos;
7009 *hpos = lastcol + 1;
7010 *x = current_x;
7011 *y = best_row->y;
b8009dd1
RS
7012 return 0;
7013}
7014
06a2c219 7015
b8009dd1
RS
7016/* Display the active region described by mouse_face_*
7017 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7018
7019static void
06a2c219 7020show_mouse_face (dpyinfo, draw)
7a13e894 7021 struct x_display_info *dpyinfo;
06a2c219 7022 enum draw_glyphs_face draw;
b8009dd1 7023{
7a13e894 7024 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
06a2c219 7025 struct frame *f = XFRAME (WINDOW_FRAME (w));
b8009dd1 7026 int i;
06a2c219
GM
7027 int cursor_off_p = 0;
7028 struct cursor_pos saved_cursor;
7029
7030 saved_cursor = output_cursor;
7031
7032 /* If window is in the process of being destroyed, don't bother
7033 to do anything. */
7034 if (w->current_matrix == NULL)
7035 goto set_x_cursor;
7036
7037 /* Recognize when we are called to operate on rows that don't exist
7038 anymore. This can happen when a window is split. */
7039 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
7040 goto set_x_cursor;
7041
7042 set_output_cursor (&w->phys_cursor);
7043
7044 /* Note that mouse_face_beg_row etc. are window relative. */
7045 for (i = dpyinfo->mouse_face_beg_row;
7046 i <= dpyinfo->mouse_face_end_row;
7047 i++)
7048 {
7049 int start_hpos, end_hpos, start_x;
7050 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
7051
7052 /* Don't do anything if row doesn't have valid contents. */
7053 if (!row->enabled_p)
7054 continue;
7055
7056 /* For all but the first row, the highlight starts at column 0. */
7057 if (i == dpyinfo->mouse_face_beg_row)
7058 {
7059 start_hpos = dpyinfo->mouse_face_beg_col;
7060 start_x = dpyinfo->mouse_face_beg_x;
7061 }
7062 else
7063 {
7064 start_hpos = 0;
7065 start_x = 0;
7066 }
7067
7068 if (i == dpyinfo->mouse_face_end_row)
7069 end_hpos = dpyinfo->mouse_face_end_col;
7070 else
7071 end_hpos = row->used[TEXT_AREA];
7072
7073 /* If the cursor's in the text we are about to rewrite, turn the
7074 cursor off. */
7075 if (!w->pseudo_window_p
7076 && i == output_cursor.vpos
7077 && output_cursor.hpos >= start_hpos - 1
7078 && output_cursor.hpos <= end_hpos)
514e4681 7079 {
06a2c219
GM
7080 x_update_window_cursor (w, 0);
7081 cursor_off_p = 1;
514e4681 7082 }
b8009dd1 7083
06a2c219 7084 if (end_hpos > start_hpos)
64f26cf5
GM
7085 {
7086 row->mouse_face_p = draw == DRAW_MOUSE_FACE;
7087 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7088 start_hpos, end_hpos, draw, NULL, NULL, 0);
7089 }
b8009dd1
RS
7090 }
7091
514e4681 7092 /* If we turned the cursor off, turn it back on. */
06a2c219
GM
7093 if (cursor_off_p)
7094 x_display_cursor (w, 1,
7095 output_cursor.hpos, output_cursor.vpos,
7096 output_cursor.x, output_cursor.y);
2729a2b5 7097
06a2c219 7098 output_cursor = saved_cursor;
fb3b7de5 7099
06a2c219
GM
7100 set_x_cursor:
7101
7102 /* Change the mouse cursor. */
7103 if (draw == DRAW_NORMAL_TEXT)
7104 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7105 f->output_data.x->text_cursor);
7106 else if (draw == DRAW_MOUSE_FACE)
334208b7 7107 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 7108 f->output_data.x->cross_cursor);
27ead1d5 7109 else
334208b7 7110 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
06a2c219 7111 f->output_data.x->nontext_cursor);
b8009dd1
RS
7112}
7113
7114/* Clear out the mouse-highlighted active region.
06a2c219 7115 Redraw it un-highlighted first. */
b8009dd1 7116
06a2c219 7117void
7a13e894
RS
7118clear_mouse_face (dpyinfo)
7119 struct x_display_info *dpyinfo;
b8009dd1 7120{
06a2c219
GM
7121 if (tip_frame)
7122 return;
7123
7a13e894 7124 if (! NILP (dpyinfo->mouse_face_window))
06a2c219 7125 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
b8009dd1 7126
7a13e894
RS
7127 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7128 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7129 dpyinfo->mouse_face_window = Qnil;
b8009dd1 7130}
e687d06e 7131
71b8321e
GM
7132
7133/* Clear any mouse-face on window W. This function is part of the
7134 redisplay interface, and is called from try_window_id and similar
7135 functions to ensure the mouse-highlight is off. */
7136
7137static void
7138x_clear_mouse_face (w)
7139 struct window *w;
7140{
7141 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7142 Lisp_Object window;
7143
7144 XSETWINDOW (window, w);
7145 if (EQ (window, dpyinfo->mouse_face_window))
7146 clear_mouse_face (dpyinfo);
7147}
7148
7149
e687d06e
RS
7150/* Just discard the mouse face information for frame F, if any.
7151 This is used when the size of F is changed. */
7152
dfcf069d 7153void
e687d06e
RS
7154cancel_mouse_face (f)
7155 FRAME_PTR f;
7156{
7157 Lisp_Object window;
7158 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7159
7160 window = dpyinfo->mouse_face_window;
7161 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7162 {
7163 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7164 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7165 dpyinfo->mouse_face_window = Qnil;
7166 }
7167}
b8009dd1 7168\f
ab648270
JB
7169static struct scroll_bar *x_window_to_scroll_bar ();
7170static void x_scroll_bar_report_motion ();
12ba150f 7171
90e65f07 7172/* Return the current position of the mouse.
2d7fc7e8 7173 *fp should be a frame which indicates which display to ask about.
90e65f07 7174
2d7fc7e8 7175 If the mouse movement started in a scroll bar, set *fp, *bar_window,
ab648270 7176 and *part to the frame, window, and scroll bar part that the mouse
12ba150f 7177 is over. Set *x and *y to the portion and whole of the mouse's
ab648270 7178 position on the scroll bar.
12ba150f 7179
2d7fc7e8 7180 If the mouse movement started elsewhere, set *fp to the frame the
12ba150f
JB
7181 mouse is on, *bar_window to nil, and *x and *y to the character cell
7182 the mouse is over.
7183
06a2c219 7184 Set *time to the server time-stamp for the time at which the mouse
12ba150f
JB
7185 was at this position.
7186
a135645a
RS
7187 Don't store anything if we don't have a valid set of values to report.
7188
90e65f07 7189 This clears the mouse_moved flag, so we can wait for the next mouse
f5bb65ec 7190 movement. */
90e65f07
JB
7191
7192static void
1cf412ec 7193XTmouse_position (fp, insist, bar_window, part, x, y, time)
334208b7 7194 FRAME_PTR *fp;
1cf412ec 7195 int insist;
12ba150f 7196 Lisp_Object *bar_window;
ab648270 7197 enum scroll_bar_part *part;
90e65f07 7198 Lisp_Object *x, *y;
e5d77022 7199 unsigned long *time;
90e65f07 7200{
a135645a
RS
7201 FRAME_PTR f1;
7202
90e65f07
JB
7203 BLOCK_INPUT;
7204
8bcee03e 7205 if (! NILP (last_mouse_scroll_bar) && insist == 0)
334208b7 7206 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
90e65f07
JB
7207 else
7208 {
12ba150f
JB
7209 Window root;
7210 int root_x, root_y;
90e65f07 7211
12ba150f
JB
7212 Window dummy_window;
7213 int dummy;
7214
39d8bb4d
KH
7215 Lisp_Object frame, tail;
7216
7217 /* Clear the mouse-moved flag for every frame on this display. */
7218 FOR_EACH_FRAME (tail, frame)
7219 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7220 XFRAME (frame)->mouse_moved = 0;
7221
ab648270 7222 last_mouse_scroll_bar = Qnil;
12ba150f
JB
7223
7224 /* Figure out which root window we're on. */
334208b7
RS
7225 XQueryPointer (FRAME_X_DISPLAY (*fp),
7226 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
12ba150f
JB
7227
7228 /* The root window which contains the pointer. */
7229 &root,
7230
7231 /* Trash which we can't trust if the pointer is on
7232 a different screen. */
7233 &dummy_window,
7234
7235 /* The position on that root window. */
58769bee 7236 &root_x, &root_y,
12ba150f
JB
7237
7238 /* More trash we can't trust. */
7239 &dummy, &dummy,
7240
7241 /* Modifier keys and pointer buttons, about which
7242 we don't care. */
7243 (unsigned int *) &dummy);
7244
7245 /* Now we have a position on the root; find the innermost window
7246 containing the pointer. */
7247 {
7248 Window win, child;
7249 int win_x, win_y;
06a2c219 7250 int parent_x = 0, parent_y = 0;
e99db5a1 7251 int count;
12ba150f
JB
7252
7253 win = root;
69388238 7254
2d7fc7e8
RS
7255 /* XTranslateCoordinates can get errors if the window
7256 structure is changing at the same time this function
7257 is running. So at least we must not crash from them. */
7258
e99db5a1 7259 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
2d7fc7e8 7260
334208b7 7261 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
23faf38f 7262 && FRAME_LIVE_P (last_mouse_frame))
12ba150f 7263 {
69388238
RS
7264 /* If mouse was grabbed on a frame, give coords for that frame
7265 even if the mouse is now outside it. */
334208b7 7266 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
69388238 7267
12ba150f 7268 /* From-window, to-window. */
69388238 7269 root, FRAME_X_WINDOW (last_mouse_frame),
12ba150f
JB
7270
7271 /* From-position, to-position. */
7272 root_x, root_y, &win_x, &win_y,
7273
7274 /* Child of win. */
7275 &child);
69388238
RS
7276 f1 = last_mouse_frame;
7277 }
7278 else
7279 {
7280 while (1)
7281 {
334208b7 7282 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
12ba150f 7283
69388238
RS
7284 /* From-window, to-window. */
7285 root, win,
12ba150f 7286
69388238
RS
7287 /* From-position, to-position. */
7288 root_x, root_y, &win_x, &win_y,
7289
7290 /* Child of win. */
7291 &child);
7292
9af3143a 7293 if (child == None || child == win)
69388238
RS
7294 break;
7295
7296 win = child;
7297 parent_x = win_x;
7298 parent_y = win_y;
7299 }
12ba150f 7300
69388238
RS
7301 /* Now we know that:
7302 win is the innermost window containing the pointer
7303 (XTC says it has no child containing the pointer),
7304 win_x and win_y are the pointer's position in it
7305 (XTC did this the last time through), and
7306 parent_x and parent_y are the pointer's position in win's parent.
7307 (They are what win_x and win_y were when win was child.
7308 If win is the root window, it has no parent, and
7309 parent_{x,y} are invalid, but that's okay, because we'll
7310 never use them in that case.) */
7311
7312 /* Is win one of our frames? */
19126e11 7313 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
69388238 7314 }
58769bee 7315
2d7fc7e8
RS
7316 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7317 f1 = 0;
7318
e99db5a1 7319 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
2d7fc7e8 7320
ab648270 7321 /* If not, is it one of our scroll bars? */
a135645a 7322 if (! f1)
12ba150f 7323 {
ab648270 7324 struct scroll_bar *bar = x_window_to_scroll_bar (win);
12ba150f
JB
7325
7326 if (bar)
7327 {
a135645a 7328 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
7329 win_x = parent_x;
7330 win_y = parent_y;
7331 }
7332 }
90e65f07 7333
8bcee03e 7334 if (f1 == 0 && insist > 0)
b86bd3dd 7335 f1 = SELECTED_FRAME ();
1cf412ec 7336
a135645a 7337 if (f1)
12ba150f 7338 {
06a2c219
GM
7339 /* Ok, we found a frame. Store all the values.
7340 last_mouse_glyph is a rectangle used to reduce the
7341 generation of mouse events. To not miss any motion
7342 events, we must divide the frame into rectangles of the
7343 size of the smallest character that could be displayed
7344 on it, i.e. into the same rectangles that matrices on
7345 the frame are divided into. */
7346
7347#if OLD_REDISPLAY_CODE
2b5c9e71 7348 int ignore1, ignore2;
2b5c9e71 7349 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
334208b7 7350 &last_mouse_glyph,
1cf412ec
RS
7351 FRAME_X_DISPLAY_INFO (f1)->grabbed
7352 || insist);
06a2c219
GM
7353#else
7354 {
7355 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7356 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7357 int x = win_x;
7358 int y = win_y;
7359
7360 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7361 round down even for negative values. */
7362 if (x < 0)
7363 x -= width - 1;
7364 if (y < 0)
7365 y -= height - 1;
7366
7367 last_mouse_glyph.width = width;
7368 last_mouse_glyph.height = height;
7369 last_mouse_glyph.x = (x + width - 1) / width * width;
7370 last_mouse_glyph.y = (y + height - 1) / height * height;
7371 }
7372#endif
12ba150f
JB
7373
7374 *bar_window = Qnil;
7375 *part = 0;
334208b7 7376 *fp = f1;
e0c1aef2
KH
7377 XSETINT (*x, win_x);
7378 XSETINT (*y, win_y);
12ba150f
JB
7379 *time = last_mouse_movement_time;
7380 }
7381 }
7382 }
90e65f07
JB
7383
7384 UNBLOCK_INPUT;
7385}
f451eb13 7386
06a2c219 7387
06a2c219 7388#ifdef USE_X_TOOLKIT
bffcfca9
GM
7389
7390/* Atimer callback function for TIMER. Called every 0.1s to process
7391 Xt timeouts, if needed. We must avoid calling XtAppPending as
7392 much as possible because that function does an implicit XFlush
7393 that slows us down. */
7394
7395static void
7396x_process_timeouts (timer)
7397 struct atimer *timer;
7398{
7399 if (toolkit_scroll_bar_interaction || popup_activated_flag)
7400 {
7401 BLOCK_INPUT;
7402 while (XtAppPending (Xt_app_con) & XtIMTimer)
7403 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7404 UNBLOCK_INPUT;
7405 }
06a2c219
GM
7406}
7407
bffcfca9 7408#endif /* USE_X_TOOLKIT */
06a2c219
GM
7409
7410\f
7411/* Scroll bar support. */
7412
7413/* Given an X window ID, find the struct scroll_bar which manages it.
7414 This can be called in GC, so we have to make sure to strip off mark
7415 bits. */
bffcfca9 7416
06a2c219
GM
7417static struct scroll_bar *
7418x_window_to_scroll_bar (window_id)
7419 Window window_id;
7420{
7421 Lisp_Object tail;
7422
7423 for (tail = Vframe_list;
7424 XGCTYPE (tail) == Lisp_Cons;
8e713be6 7425 tail = XCDR (tail))
06a2c219
GM
7426 {
7427 Lisp_Object frame, bar, condemned;
7428
8e713be6 7429 frame = XCAR (tail);
06a2c219
GM
7430 /* All elements of Vframe_list should be frames. */
7431 if (! GC_FRAMEP (frame))
7432 abort ();
7433
7434 /* Scan this frame's scroll bar list for a scroll bar with the
7435 right window ID. */
7436 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7437 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7438 /* This trick allows us to search both the ordinary and
7439 condemned scroll bar lists with one loop. */
7440 ! GC_NILP (bar) || (bar = condemned,
7441 condemned = Qnil,
7442 ! GC_NILP (bar));
7443 bar = XSCROLL_BAR (bar)->next)
7444 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7445 return XSCROLL_BAR (bar);
7446 }
7447
7448 return 0;
7449}
7450
7451
7452\f
7453/************************************************************************
7454 Toolkit scroll bars
7455 ************************************************************************/
7456
7457#if USE_TOOLKIT_SCROLL_BARS
7458
7459static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7460static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7461static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7462 struct scroll_bar *));
7463static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7464 int, int, int));
7465
7466
7467/* Id of action hook installed for scroll bars. */
7468
7469static XtActionHookId action_hook_id;
7470
7471/* Lisp window being scrolled. Set when starting to interact with
7472 a toolkit scroll bar, reset to nil when ending the interaction. */
7473
7474static Lisp_Object window_being_scrolled;
7475
7476/* Last scroll bar part sent in xm_scroll_callback. */
7477
7478static int last_scroll_bar_part;
7479
ec18280f
SM
7480/* Whether this is an Xaw with arrow-scrollbars. This should imply
7481 that movements of 1/20 of the screen size are mapped to up/down. */
7482
7483static Boolean xaw3d_arrow_scroll;
7484
7485/* Whether the drag scrolling maintains the mouse at the top of the
7486 thumb. If not, resizing the thumb needs to be done more carefully
7487 to avoid jerkyness. */
7488
7489static Boolean xaw3d_pick_top;
7490
06a2c219
GM
7491
7492/* Action hook installed via XtAppAddActionHook when toolkit scroll
ec18280f 7493 bars are used.. The hook is responsible for detecting when
06a2c219
GM
7494 the user ends an interaction with the scroll bar, and generates
7495 a `end-scroll' scroll_bar_click' event if so. */
7496
7497static void
7498xt_action_hook (widget, client_data, action_name, event, params,
7499 num_params)
7500 Widget widget;
7501 XtPointer client_data;
7502 String action_name;
7503 XEvent *event;
7504 String *params;
7505 Cardinal *num_params;
7506{
7507 int scroll_bar_p;
7508 char *end_action;
7509
7510#ifdef USE_MOTIF
7511 scroll_bar_p = XmIsScrollBar (widget);
7512 end_action = "Release";
ec18280f 7513#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
7514 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7515 end_action = "EndScroll";
ec18280f 7516#endif /* USE_MOTIF */
06a2c219 7517
06a2c219
GM
7518 if (scroll_bar_p
7519 && strcmp (action_name, end_action) == 0
7520 && WINDOWP (window_being_scrolled))
7521 {
7522 struct window *w;
7523
7524 x_send_scroll_bar_event (window_being_scrolled,
7525 scroll_bar_end_scroll, 0, 0);
7526 w = XWINDOW (window_being_scrolled);
7527 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7528 window_being_scrolled = Qnil;
7529 last_scroll_bar_part = -1;
bffcfca9
GM
7530
7531 /* Xt timeouts no longer needed. */
7532 toolkit_scroll_bar_interaction = 0;
06a2c219
GM
7533 }
7534}
7535
7536
7537/* Send a client message with message type Xatom_Scrollbar for a
7538 scroll action to the frame of WINDOW. PART is a value identifying
7539 the part of the scroll bar that was clicked on. PORTION is the
7540 amount to scroll of a whole of WHOLE. */
7541
7542static void
7543x_send_scroll_bar_event (window, part, portion, whole)
7544 Lisp_Object window;
7545 int part, portion, whole;
7546{
7547 XEvent event;
7548 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
7549 struct frame *f = XFRAME (XWINDOW (window)->frame);
7550
7551 /* Construct a ClientMessage event to send to the frame. */
7552 ev->type = ClientMessage;
7553 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7554 ev->display = FRAME_X_DISPLAY (f);
7555 ev->window = FRAME_X_WINDOW (f);
7556 ev->format = 32;
52e386c2 7557 ev->data.l[0] = (long) XFASTINT (window);
06a2c219
GM
7558 ev->data.l[1] = (long) part;
7559 ev->data.l[2] = (long) 0;
7560 ev->data.l[3] = (long) portion;
7561 ev->data.l[4] = (long) whole;
7562
bffcfca9
GM
7563 /* Make Xt timeouts work while the scroll bar is active. */
7564 toolkit_scroll_bar_interaction = 1;
7565
06a2c219
GM
7566 /* Setting the event mask to zero means that the message will
7567 be sent to the client that created the window, and if that
7568 window no longer exists, no event will be sent. */
7569 BLOCK_INPUT;
7570 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7571 UNBLOCK_INPUT;
7572}
7573
7574
7575/* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7576 in *IEVENT. */
7577
7578static void
7579x_scroll_bar_to_input_event (event, ievent)
7580 XEvent *event;
7581 struct input_event *ievent;
7582{
7583 XClientMessageEvent *ev = (XClientMessageEvent *) event;
52e386c2
KR
7584 Lisp_Object window;
7585 struct frame *f;
7586
7587 XSETFASTINT (window, ev->data.l[0]);
7588 f = XFRAME (XWINDOW (window)->frame);
06a2c219
GM
7589
7590 ievent->kind = scroll_bar_click;
7591 ievent->frame_or_window = window;
0f8aabe9 7592 ievent->arg = Qnil;
06a2c219
GM
7593 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7594 ievent->part = ev->data.l[1];
7595 ievent->code = ev->data.l[2];
7596 ievent->x = make_number ((int) ev->data.l[3]);
7597 ievent->y = make_number ((int) ev->data.l[4]);
7598 ievent->modifiers = 0;
7599}
7600
7601
7602#ifdef USE_MOTIF
7603
7604/* Minimum and maximum values used for Motif scroll bars. */
7605
7606#define XM_SB_MIN 1
7607#define XM_SB_MAX 10000000
7608#define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7609
7610
7611/* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7612 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7613 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7614
7615static void
7616xm_scroll_callback (widget, client_data, call_data)
7617 Widget widget;
7618 XtPointer client_data, call_data;
7619{
7620 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7621 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7622 double percent;
7623 int part = -1, whole = 0, portion = 0;
7624
7625 switch (cs->reason)
7626 {
7627 case XmCR_DECREMENT:
7628 bar->dragging = Qnil;
7629 part = scroll_bar_up_arrow;
7630 break;
7631
7632 case XmCR_INCREMENT:
7633 bar->dragging = Qnil;
7634 part = scroll_bar_down_arrow;
7635 break;
7636
7637 case XmCR_PAGE_DECREMENT:
7638 bar->dragging = Qnil;
7639 part = scroll_bar_above_handle;
7640 break;
7641
7642 case XmCR_PAGE_INCREMENT:
7643 bar->dragging = Qnil;
7644 part = scroll_bar_below_handle;
7645 break;
7646
7647 case XmCR_TO_TOP:
7648 bar->dragging = Qnil;
7649 part = scroll_bar_to_top;
7650 break;
7651
7652 case XmCR_TO_BOTTOM:
7653 bar->dragging = Qnil;
7654 part = scroll_bar_to_bottom;
7655 break;
7656
7657 case XmCR_DRAG:
7658 {
7659 int slider_size;
7660 int dragging_down_p = (INTEGERP (bar->dragging)
7661 && XINT (bar->dragging) <= cs->value);
7662
7663 /* Get the slider size. */
7664 BLOCK_INPUT;
7665 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7666 UNBLOCK_INPUT;
7667
7668 /* At the max position of the scroll bar, do a line-wise
7669 movement. Without doing anything, the LessTif scroll bar
7670 calls us with the same cs->value again and again. If we
7671 want to make sure that we can reach the end of the buffer,
7672 we have to do something.
7673
7674 Implementation note: setting bar->dragging always to
7675 cs->value gives a smoother movement at the max position.
7676 Setting it to nil when doing line-wise movement gives
7677 a better slider behavior. */
7678
7679 if (cs->value + slider_size == XM_SB_MAX
7680 || (dragging_down_p
7681 && last_scroll_bar_part == scroll_bar_down_arrow))
7682 {
7683 part = scroll_bar_down_arrow;
7684 bar->dragging = Qnil;
7685 }
7686 else
7687 {
7688 whole = XM_SB_RANGE;
7689 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7690 part = scroll_bar_handle;
7691 bar->dragging = make_number (cs->value);
7692 }
7693 }
7694 break;
7695
7696 case XmCR_VALUE_CHANGED:
7697 break;
7698 };
7699
7700 if (part >= 0)
7701 {
7702 window_being_scrolled = bar->window;
7703 last_scroll_bar_part = part;
7704 x_send_scroll_bar_event (bar->window, part, portion, whole);
7705 }
7706}
7707
7708
ec18280f 7709#else /* !USE_MOTIF, i.e. Xaw. */
06a2c219
GM
7710
7711
ec18280f 7712/* Xaw scroll bar callback. Invoked when the thumb is dragged.
06a2c219
GM
7713 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7714 scroll bar struct. CALL_DATA is a pointer to a float saying where
7715 the thumb is. */
7716
7717static void
ec18280f 7718xaw_jump_callback (widget, client_data, call_data)
06a2c219
GM
7719 Widget widget;
7720 XtPointer client_data, call_data;
7721{
7722 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7723 float top = *(float *) call_data;
7724 float shown;
ec18280f
SM
7725 int whole, portion, height;
7726 int part;
06a2c219
GM
7727
7728 /* Get the size of the thumb, a value between 0 and 1. */
7729 BLOCK_INPUT;
ec18280f 7730 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
06a2c219
GM
7731 UNBLOCK_INPUT;
7732
7733 whole = 10000000;
7734 portion = shown < 1 ? top * whole : 0;
06a2c219 7735
ec18280f
SM
7736 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
7737 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
7738 the bottom, so we force the scrolling whenever we see that we're
7739 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
7740 we try to ensure that we always stay two pixels away from the
7741 bottom). */
06a2c219
GM
7742 part = scroll_bar_down_arrow;
7743 else
7744 part = scroll_bar_handle;
7745
7746 window_being_scrolled = bar->window;
7747 bar->dragging = make_number (portion);
7748 last_scroll_bar_part = part;
7749 x_send_scroll_bar_event (bar->window, part, portion, whole);
7750}
7751
7752
ec18280f
SM
7753/* Xaw scroll bar callback. Invoked for incremental scrolling.,
7754 i.e. line or page up or down. WIDGET is the Xaw scroll bar
06a2c219
GM
7755 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
7756 the scroll bar. CALL_DATA is an integer specifying the action that
7757 has taken place. It's magnitude is in the range 0..height of the
7758 scroll bar. Negative values mean scroll towards buffer start.
7759 Values < height of scroll bar mean line-wise movement. */
7760
7761static void
ec18280f 7762xaw_scroll_callback (widget, client_data, call_data)
06a2c219
GM
7763 Widget widget;
7764 XtPointer client_data, call_data;
7765{
7766 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7767 int position = (int) call_data;
7768 Dimension height;
7769 int part;
7770
7771 /* Get the height of the scroll bar. */
7772 BLOCK_INPUT;
7773 XtVaGetValues (widget, XtNheight, &height, NULL);
7774 UNBLOCK_INPUT;
7775
ec18280f
SM
7776 if (abs (position) >= height)
7777 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
7778
7779 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
7780 it maps line-movement to call_data = max(5, height/20). */
7781 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
7782 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
06a2c219 7783 else
ec18280f 7784 part = scroll_bar_move_ratio;
06a2c219
GM
7785
7786 window_being_scrolled = bar->window;
7787 bar->dragging = Qnil;
7788 last_scroll_bar_part = part;
ec18280f 7789 x_send_scroll_bar_event (bar->window, part, position, height);
06a2c219
GM
7790}
7791
7792
7793#endif /* not USE_MOTIF */
7794
7795
7796/* Create the widget for scroll bar BAR on frame F. Record the widget
7797 and X window of the scroll bar in BAR. */
7798
7799static void
7800x_create_toolkit_scroll_bar (f, bar)
7801 struct frame *f;
7802 struct scroll_bar *bar;
7803{
7804 Window xwindow;
7805 Widget widget;
7806 Arg av[20];
7807 int ac = 0;
7808 char *scroll_bar_name = "verticalScrollBar";
7809 unsigned long pixel;
7810
7811 BLOCK_INPUT;
7812
7813#ifdef USE_MOTIF
7814 /* LessTif 0.85, problems:
7815
7816 1. When the mouse if over the scroll bar, the scroll bar will
7817 get keyboard events. I didn't find a way to turn this off.
7818
7819 2. Do we have to explicitly set the cursor to get an arrow
7820 cursor (see below)? */
7821
7822 /* Set resources. Create the widget. */
7823 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7824 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
7825 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
7826 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
7827 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
7828 XtSetArg (av[ac], XmNincrement, 1); ++ac;
7829 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
7830
7831 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7832 if (pixel != -1)
7833 {
7834 XtSetArg (av[ac], XmNforeground, pixel);
7835 ++ac;
7836 }
7837
7838 pixel = f->output_data.x->scroll_bar_background_pixel;
7839 if (pixel != -1)
7840 {
7841 XtSetArg (av[ac], XmNbackground, pixel);
7842 ++ac;
7843 }
7844
7845 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
7846 scroll_bar_name, av, ac);
7847
7848 /* Add one callback for everything that can happen. */
7849 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
7850 (XtPointer) bar);
7851 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
7852 (XtPointer) bar);
7853 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
7854 (XtPointer) bar);
7855 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
7856 (XtPointer) bar);
7857 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
7858 (XtPointer) bar);
7859 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
7860 (XtPointer) bar);
7861 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
7862 (XtPointer) bar);
7863
7864 /* Realize the widget. Only after that is the X window created. */
7865 XtRealizeWidget (widget);
7866
7867 /* Set the cursor to an arrow. I didn't find a resource to do that.
7868 And I'm wondering why it hasn't an arrow cursor by default. */
7869 XDefineCursor (XtDisplay (widget), XtWindow (widget),
7870 f->output_data.x->nontext_cursor);
7871
ec18280f 7872#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
7873
7874 /* Set resources. Create the widget. The background of the
7875 Xaw3d scroll bar widget is a little bit light for my taste.
7876 We don't alter it here to let users change it according
7877 to their taste with `emacs*verticalScrollBar.background: xxx'. */
7878 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7879 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
ec18280f
SM
7880 /* For smoother scrolling with Xaw3d -sm */
7881 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
7882 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
06a2c219
GM
7883
7884 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7885 if (pixel != -1)
7886 {
7887 XtSetArg (av[ac], XtNforeground, pixel);
7888 ++ac;
7889 }
7890
7891 pixel = f->output_data.x->scroll_bar_background_pixel;
7892 if (pixel != -1)
7893 {
7894 XtSetArg (av[ac], XtNbackground, pixel);
7895 ++ac;
7896 }
7897
7898 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
7899 f->output_data.x->edit_widget, av, ac);
ec18280f
SM
7900
7901 {
7902 char *initial = "";
7903 char *val = initial;
7904 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
7905 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
7906 if (val == initial)
7907 { /* ARROW_SCROLL */
7908 xaw3d_arrow_scroll = True;
7909 /* Isn't that just a personal preference ? -sm */
7910 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
7911 }
7912 }
06a2c219
GM
7913
7914 /* Define callbacks. */
ec18280f
SM
7915 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
7916 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
06a2c219
GM
7917 (XtPointer) bar);
7918
7919 /* Realize the widget. Only after that is the X window created. */
7920 XtRealizeWidget (widget);
7921
ec18280f 7922#endif /* !USE_MOTIF */
06a2c219
GM
7923
7924 /* Install an action hook that let's us detect when the user
7925 finishes interacting with a scroll bar. */
7926 if (action_hook_id == 0)
7927 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
7928
7929 /* Remember X window and widget in the scroll bar vector. */
7930 SET_SCROLL_BAR_X_WIDGET (bar, widget);
7931 xwindow = XtWindow (widget);
7932 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
7933
7934 UNBLOCK_INPUT;
7935}
7936
7937
7938/* Set the thumb size and position of scroll bar BAR. We are currently
7939 displaying PORTION out of a whole WHOLE, and our position POSITION. */
7940
7941static void
7942x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
7943 struct scroll_bar *bar;
7944 int portion, position, whole;
f451eb13 7945{
06a2c219 7946 float top, shown;
06a2c219 7947 Widget widget = SCROLL_BAR_X_WIDGET (bar);
f451eb13 7948
06a2c219
GM
7949 if (whole == 0)
7950 top = 0, shown = 1;
7951 else
f451eb13 7952 {
06a2c219
GM
7953 top = (float) position / whole;
7954 shown = (float) portion / whole;
7955 }
f451eb13 7956
06a2c219 7957 BLOCK_INPUT;
f451eb13 7958
06a2c219
GM
7959#ifdef USE_MOTIF
7960 {
7961 int size, value;
7962 Boolean arrow1_selected, arrow2_selected;
7963 unsigned char flags;
7964 XmScrollBarWidget sb;
7965
ec18280f 7966 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
06a2c219
GM
7967 is the scroll bar's maximum and MIN is the scroll bar's minimum
7968 value. */
7969 size = shown * XM_SB_RANGE;
7970 size = min (size, XM_SB_RANGE);
7971 size = max (size, 1);
7972
7973 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
7974 value = top * XM_SB_RANGE;
7975 value = min (value, XM_SB_MAX - size);
7976 value = max (value, XM_SB_MIN);
7977
7978 /* LessTif: Calling XmScrollBarSetValues after an increment or
7979 decrement turns off auto-repeat LessTif-internally. This can
7980 be seen in ScrollBar.c which resets Arrow1Selected and
7981 Arrow2Selected. It also sets internal flags so that LessTif
7982 believes the mouse is in the slider. We either have to change
7983 our code, or work around that by accessing private data. */
7984
7985 sb = (XmScrollBarWidget) widget;
7986 arrow1_selected = sb->scrollBar.arrow1_selected;
7987 arrow2_selected = sb->scrollBar.arrow2_selected;
7988 flags = sb->scrollBar.flags;
7989
7990 if (NILP (bar->dragging))
7991 XmScrollBarSetValues (widget, value, size, 0, 0, False);
7992 else if (last_scroll_bar_part == scroll_bar_down_arrow)
7993 /* This has the negative side effect that the slider value is
ec18280f 7994 not what it would be if we scrolled here using line-wise or
06a2c219
GM
7995 page-wise movement. */
7996 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
7997 else
7998 {
7999 /* If currently dragging, only update the slider size.
8000 This reduces flicker effects. */
8001 int old_value, old_size, increment, page_increment;
8002
8003 XmScrollBarGetValues (widget, &old_value, &old_size,
8004 &increment, &page_increment);
8005 XmScrollBarSetValues (widget, old_value,
8006 min (size, XM_SB_RANGE - old_value),
8007 0, 0, False);
8008 }
8009
8010 sb->scrollBar.arrow1_selected = arrow1_selected;
8011 sb->scrollBar.arrow2_selected = arrow2_selected;
8012 sb->scrollBar.flags = flags;
8013 }
ec18280f 8014#else /* !USE_MOTIF i.e. use Xaw */
06a2c219 8015 {
ec18280f
SM
8016 float old_top, old_shown;
8017 Dimension height;
8018 XtVaGetValues (widget,
8019 XtNtopOfThumb, &old_top,
8020 XtNshown, &old_shown,
8021 XtNheight, &height,
8022 NULL);
8023
8024 /* Massage the top+shown values. */
8025 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8026 top = max (0, min (1, top));
8027 else
8028 top = old_top;
8029 /* Keep two pixels available for moving the thumb down. */
8030 shown = max (0, min (1 - top - (2.0 / height), shown));
06a2c219
GM
8031
8032 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8033 check that your system's configuration file contains a define
8034 for `NARROWPROTO'. See s/freebsd.h for an example. */
ec18280f 8035 if (top != old_top || shown != old_shown)
eb393530 8036 {
ec18280f 8037 if (NILP (bar->dragging))
eb393530 8038 XawScrollbarSetThumb (widget, top, shown);
06a2c219
GM
8039 else
8040 {
ec18280f
SM
8041#ifdef HAVE_XAW3D
8042 ScrollbarWidget sb = (ScrollbarWidget) widget;
3e71d8f2 8043 int scroll_mode = 0;
ec18280f
SM
8044
8045 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8046 if (xaw3d_arrow_scroll)
8047 {
8048 /* Xaw3d stupidly ignores resize requests while dragging
8049 so we have to make it believe it's not in dragging mode. */
8050 scroll_mode = sb->scrollbar.scroll_mode;
8051 if (scroll_mode == 2)
8052 sb->scrollbar.scroll_mode = 0;
8053 }
8054#endif
8055 /* Try to make the scrolling a tad smoother. */
8056 if (!xaw3d_pick_top)
8057 shown = min (shown, old_shown);
8058
8059 XawScrollbarSetThumb (widget, top, shown);
8060
8061#ifdef HAVE_XAW3D
8062 if (xaw3d_arrow_scroll && scroll_mode == 2)
8063 sb->scrollbar.scroll_mode = scroll_mode;
8064#endif
06a2c219 8065 }
06a2c219
GM
8066 }
8067 }
ec18280f 8068#endif /* !USE_MOTIF */
06a2c219
GM
8069
8070 UNBLOCK_INPUT;
f451eb13
JB
8071}
8072
06a2c219
GM
8073#endif /* USE_TOOLKIT_SCROLL_BARS */
8074
8075
8076\f
8077/************************************************************************
8078 Scroll bars, general
8079 ************************************************************************/
8080
8081/* Create a scroll bar and return the scroll bar vector for it. W is
8082 the Emacs window on which to create the scroll bar. TOP, LEFT,
8083 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8084 scroll bar. */
8085
ab648270 8086static struct scroll_bar *
06a2c219
GM
8087x_scroll_bar_create (w, top, left, width, height)
8088 struct window *w;
f451eb13
JB
8089 int top, left, width, height;
8090{
06a2c219 8091 struct frame *f = XFRAME (w->frame);
334208b7
RS
8092 struct scroll_bar *bar
8093 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
f451eb13
JB
8094
8095 BLOCK_INPUT;
8096
06a2c219
GM
8097#if USE_TOOLKIT_SCROLL_BARS
8098 x_create_toolkit_scroll_bar (f, bar);
8099#else /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8100 {
8101 XSetWindowAttributes a;
8102 unsigned long mask;
5c187dee 8103 Window window;
06a2c219
GM
8104
8105 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8106 if (a.background_pixel == -1)
8107 a.background_pixel = f->output_data.x->background_pixel;
8108
12ba150f 8109 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9a572e2a 8110 | ButtonMotionMask | PointerMotionHintMask
12ba150f 8111 | ExposureMask);
7a13e894 8112 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
f451eb13 8113
dbc4e1c1 8114 mask = (CWBackPixel | CWEventMask | CWCursor);
f451eb13 8115
06a2c219
GM
8116 /* Clear the area of W that will serve as a scroll bar. This is
8117 for the case that a window has been split horizontally. In
8118 this case, no clear_frame is generated to reduce flickering. */
8119 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8120 left, top, width,
8121 window_box_height (w), False);
8122
8123 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8124 /* Position and size of scroll bar. */
8125 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8126 top,
8127 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8128 height,
8129 /* Border width, depth, class, and visual. */
8130 0,
8131 CopyFromParent,
8132 CopyFromParent,
8133 CopyFromParent,
8134 /* Attributes. */
8135 mask, &a);
8136 SET_SCROLL_BAR_X_WINDOW (bar, window);
f451eb13 8137 }
06a2c219 8138#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 8139
06a2c219 8140 XSETWINDOW (bar->window, w);
e0c1aef2
KH
8141 XSETINT (bar->top, top);
8142 XSETINT (bar->left, left);
8143 XSETINT (bar->width, width);
8144 XSETINT (bar->height, height);
8145 XSETINT (bar->start, 0);
8146 XSETINT (bar->end, 0);
12ba150f 8147 bar->dragging = Qnil;
f451eb13
JB
8148
8149 /* Add bar to its frame's list of scroll bars. */
334208b7 8150 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 8151 bar->prev = Qnil;
334208b7 8152 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
06a2c219 8153 if (!NILP (bar->next))
e0c1aef2 8154 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13 8155
06a2c219
GM
8156 /* Map the window/widget. */
8157#if USE_TOOLKIT_SCROLL_BARS
8158 XtMapWidget (SCROLL_BAR_X_WIDGET (bar));
8159 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
8160 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8161 top,
8162 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8163 height, 0);
8164#else /* not USE_TOOLKIT_SCROLL_BARS */
7f9c7f94 8165 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
06a2c219 8166#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8167
8168 UNBLOCK_INPUT;
12ba150f 8169 return bar;
f451eb13
JB
8170}
8171
06a2c219 8172
12ba150f 8173/* Draw BAR's handle in the proper position.
06a2c219 8174
12ba150f
JB
8175 If the handle is already drawn from START to END, don't bother
8176 redrawing it, unless REBUILD is non-zero; in that case, always
8177 redraw it. (REBUILD is handy for drawing the handle after expose
58769bee 8178 events.)
12ba150f
JB
8179
8180 Normally, we want to constrain the start and end of the handle to
06a2c219
GM
8181 fit inside its rectangle, but if the user is dragging the scroll
8182 bar handle, we want to let them drag it down all the way, so that
8183 the bar's top is as far down as it goes; otherwise, there's no way
8184 to move to the very end of the buffer. */
8185
5c187dee
GM
8186#ifndef USE_TOOLKIT_SCROLL_BARS
8187
f451eb13 8188static void
ab648270
JB
8189x_scroll_bar_set_handle (bar, start, end, rebuild)
8190 struct scroll_bar *bar;
f451eb13 8191 int start, end;
12ba150f 8192 int rebuild;
f451eb13 8193{
12ba150f 8194 int dragging = ! NILP (bar->dragging);
ab648270 8195 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8196 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8197 GC gc = f->output_data.x->normal_gc;
12ba150f
JB
8198
8199 /* If the display is already accurate, do nothing. */
8200 if (! rebuild
8201 && start == XINT (bar->start)
8202 && end == XINT (bar->end))
8203 return;
8204
f451eb13
JB
8205 BLOCK_INPUT;
8206
8207 {
d9cdbb3d
RS
8208 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8209 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8210 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
f451eb13
JB
8211
8212 /* Make sure the values are reasonable, and try to preserve
8213 the distance between start and end. */
12ba150f
JB
8214 {
8215 int length = end - start;
8216
8217 if (start < 0)
8218 start = 0;
8219 else if (start > top_range)
8220 start = top_range;
8221 end = start + length;
8222
8223 if (end < start)
8224 end = start;
8225 else if (end > top_range && ! dragging)
8226 end = top_range;
8227 }
f451eb13 8228
ab648270 8229 /* Store the adjusted setting in the scroll bar. */
e0c1aef2
KH
8230 XSETINT (bar->start, start);
8231 XSETINT (bar->end, end);
f451eb13 8232
12ba150f
JB
8233 /* Clip the end position, just for display. */
8234 if (end > top_range)
8235 end = top_range;
f451eb13 8236
ab648270 8237 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
12ba150f
JB
8238 below top positions, to make sure the handle is always at least
8239 that many pixels tall. */
ab648270 8240 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
f451eb13 8241
12ba150f
JB
8242 /* Draw the empty space above the handle. Note that we can't clear
8243 zero-height areas; that means "clear to end of window." */
8244 if (0 < start)
334208b7 8245 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 8246
12ba150f 8247 /* x, y, width, height, and exposures. */
ab648270
JB
8248 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8249 VERTICAL_SCROLL_BAR_TOP_BORDER,
12ba150f
JB
8250 inside_width, start,
8251 False);
f451eb13 8252
06a2c219
GM
8253 /* Change to proper foreground color if one is specified. */
8254 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8255 XSetForeground (FRAME_X_DISPLAY (f), gc,
8256 f->output_data.x->scroll_bar_foreground_pixel);
8257
12ba150f 8258 /* Draw the handle itself. */
334208b7 8259 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13 8260
12ba150f 8261 /* x, y, width, height */
ab648270
JB
8262 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8263 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
12ba150f 8264 inside_width, end - start);
f451eb13 8265
06a2c219
GM
8266 /* Restore the foreground color of the GC if we changed it above. */
8267 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8268 XSetForeground (FRAME_X_DISPLAY (f), gc,
8269 f->output_data.x->foreground_pixel);
f451eb13 8270
12ba150f
JB
8271 /* Draw the empty space below the handle. Note that we can't
8272 clear zero-height areas; that means "clear to end of window." */
8273 if (end < inside_height)
334208b7 8274 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 8275
12ba150f 8276 /* x, y, width, height, and exposures. */
ab648270
JB
8277 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8278 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
12ba150f
JB
8279 inside_width, inside_height - end,
8280 False);
f451eb13 8281
f451eb13
JB
8282 }
8283
f451eb13
JB
8284 UNBLOCK_INPUT;
8285}
8286
5c187dee 8287#endif /* !USE_TOOLKIT_SCROLL_BARS */
f451eb13 8288
06a2c219
GM
8289/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8290 nil. */
58769bee 8291
12ba150f 8292static void
ab648270
JB
8293x_scroll_bar_remove (bar)
8294 struct scroll_bar *bar;
12ba150f 8295{
12ba150f
JB
8296 BLOCK_INPUT;
8297
06a2c219
GM
8298#if USE_TOOLKIT_SCROLL_BARS
8299 XtDestroyWidget (SCROLL_BAR_X_WIDGET (bar));
8300#else /* not USE_TOOLKIT_SCROLL_BARS */
5c187dee
GM
8301 {
8302 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8303 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8304 }
06a2c219
GM
8305#endif /* not USE_TOOLKIT_SCROLL_BARS */
8306
ab648270
JB
8307 /* Disassociate this scroll bar from its window. */
8308 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
12ba150f
JB
8309
8310 UNBLOCK_INPUT;
8311}
8312
06a2c219 8313
12ba150f
JB
8314/* Set the handle of the vertical scroll bar for WINDOW to indicate
8315 that we are displaying PORTION characters out of a total of WHOLE
ab648270 8316 characters, starting at POSITION. If WINDOW has no scroll bar,
12ba150f 8317 create one. */
06a2c219 8318
12ba150f 8319static void
06a2c219
GM
8320XTset_vertical_scroll_bar (w, portion, whole, position)
8321 struct window *w;
f451eb13
JB
8322 int portion, whole, position;
8323{
06a2c219 8324 struct frame *f = XFRAME (w->frame);
ab648270 8325 struct scroll_bar *bar;
3c6ede7b 8326 int top, height, left, sb_left, width, sb_width;
06a2c219 8327 int window_x, window_y, window_width, window_height;
06a2c219 8328
3c6ede7b 8329 /* Get window dimensions. */
06a2c219 8330 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
3c6ede7b
GM
8331 top = window_y;
8332 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8333 height = window_height;
06a2c219 8334
3c6ede7b 8335 /* Compute the left edge of the scroll bar area. */
06a2c219 8336 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
3c6ede7b
GM
8337 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8338 else
8339 left = XFASTINT (w->left);
8340 left *= CANON_X_UNIT (f);
8341 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8342
8343 /* Compute the width of the scroll bar which might be less than
8344 the width of the area reserved for the scroll bar. */
8345 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8346 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
06a2c219 8347 else
3c6ede7b 8348 sb_width = width;
12ba150f 8349
3c6ede7b
GM
8350 /* Compute the left edge of the scroll bar. */
8351#ifdef USE_TOOLKIT_SCROLL_BARS
8352 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8353 sb_left = left + width - sb_width - (width - sb_width) / 2;
8354 else
8355 sb_left = left + (width - sb_width) / 2;
8356#else
8357 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8358 sb_left = left + width - sb_width;
8359 else
8360 sb_left = left;
8361#endif
8362
ab648270 8363 /* Does the scroll bar exist yet? */
06a2c219 8364 if (NILP (w->vertical_scroll_bar))
3c6ede7b 8365 {
80c32bcc 8366 BLOCK_INPUT;
3c6ede7b
GM
8367 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8368 left, top, width, height, False);
80c32bcc 8369 UNBLOCK_INPUT;
3c6ede7b
GM
8370 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8371 }
f451eb13 8372 else
12ba150f
JB
8373 {
8374 /* It may just need to be moved and resized. */
06a2c219
GM
8375 unsigned int mask = 0;
8376
8377 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8378
8379 BLOCK_INPUT;
8380
3c6ede7b 8381 if (sb_left != XINT (bar->left))
06a2c219 8382 mask |= CWX;
3c6ede7b 8383 if (top != XINT (bar->top))
06a2c219 8384 mask |= CWY;
3c6ede7b 8385 if (sb_width != XINT (bar->width))
06a2c219 8386 mask |= CWWidth;
3c6ede7b 8387 if (height != XINT (bar->height))
06a2c219
GM
8388 mask |= CWHeight;
8389
8390#ifdef USE_TOOLKIT_SCROLL_BARS
fe6f39d9
GM
8391
8392 /* Since toolkit scroll bars are smaller than the space reserved
8393 for them on the frame, we have to clear "under" them. */
8394 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3c6ede7b 8395 left, top, width, height, False);
06a2c219
GM
8396
8397 /* Move/size the scroll bar widget. */
8398 if (mask)
8399 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
3c6ede7b
GM
8400 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8401 top,
8402 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8403 height, 0);
06a2c219
GM
8404
8405#else /* not USE_TOOLKIT_SCROLL_BARS */
8406
e1f6572f
RS
8407 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8408 {
8409 /* Clear areas not covered by the scroll bar. This makes sure a
8410 previous mode line display is cleared after C-x 2 C-x 1, for
8411 example. Non-toolkit scroll bars are as wide as the area
8412 reserved for scroll bars - trim at both sides. */
8413 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8414 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8415 height, False);
8416 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8417 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8418 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8419 height, False);
8420 }
06a2c219
GM
8421
8422 /* Move/size the scroll bar window. */
8423 if (mask)
8424 {
8425 XWindowChanges wc;
8426
3c6ede7b
GM
8427 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8428 wc.y = top;
8429 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8430 wc.height = height;
06a2c219
GM
8431 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8432 mask, &wc);
8433 }
8434
8435#endif /* not USE_TOOLKIT_SCROLL_BARS */
8436
8437 /* Remember new settings. */
3c6ede7b
GM
8438 XSETINT (bar->left, sb_left);
8439 XSETINT (bar->top, top);
8440 XSETINT (bar->width, sb_width);
8441 XSETINT (bar->height, height);
06a2c219
GM
8442
8443 UNBLOCK_INPUT;
12ba150f 8444 }
f451eb13 8445
06a2c219
GM
8446#if USE_TOOLKIT_SCROLL_BARS
8447 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8448#else /* not USE_TOOLKIT_SCROLL_BARS */
ab648270 8449 /* Set the scroll bar's current state, unless we're currently being
f451eb13 8450 dragged. */
12ba150f 8451 if (NILP (bar->dragging))
f451eb13 8452 {
92857db0 8453 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
f451eb13 8454
12ba150f 8455 if (whole == 0)
ab648270 8456 x_scroll_bar_set_handle (bar, 0, top_range, 0);
12ba150f
JB
8457 else
8458 {
43f868f5
JB
8459 int start = ((double) position * top_range) / whole;
8460 int end = ((double) (position + portion) * top_range) / whole;
ab648270 8461 x_scroll_bar_set_handle (bar, start, end, 0);
12ba150f 8462 }
f451eb13 8463 }
06a2c219 8464#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 8465
06a2c219 8466 XSETVECTOR (w->vertical_scroll_bar, bar);
f451eb13
JB
8467}
8468
12ba150f 8469
f451eb13 8470/* The following three hooks are used when we're doing a thorough
ab648270 8471 redisplay of the frame. We don't explicitly know which scroll bars
f451eb13 8472 are going to be deleted, because keeping track of when windows go
12ba150f
JB
8473 away is a real pain - "Can you say set-window-configuration, boys
8474 and girls?" Instead, we just assert at the beginning of redisplay
ab648270 8475 that *all* scroll bars are to be removed, and then save a scroll bar
12ba150f 8476 from the fiery pit when we actually redisplay its window. */
f451eb13 8477
ab648270
JB
8478/* Arrange for all scroll bars on FRAME to be removed at the next call
8479 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
06a2c219
GM
8480 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8481
58769bee 8482static void
ab648270 8483XTcondemn_scroll_bars (frame)
f451eb13
JB
8484 FRAME_PTR frame;
8485{
f9e24cb9
RS
8486 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8487 while (! NILP (FRAME_SCROLL_BARS (frame)))
8488 {
8489 Lisp_Object bar;
8490 bar = FRAME_SCROLL_BARS (frame);
8491 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8492 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8493 XSCROLL_BAR (bar)->prev = Qnil;
8494 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8495 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8496 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8497 }
f451eb13
JB
8498}
8499
06a2c219 8500/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
12ba150f 8501 Note that WINDOW isn't necessarily condemned at all. */
f451eb13 8502static void
ab648270 8503XTredeem_scroll_bar (window)
12ba150f 8504 struct window *window;
f451eb13 8505{
ab648270 8506 struct scroll_bar *bar;
12ba150f 8507
ab648270
JB
8508 /* We can't redeem this window's scroll bar if it doesn't have one. */
8509 if (NILP (window->vertical_scroll_bar))
12ba150f
JB
8510 abort ();
8511
ab648270 8512 bar = XSCROLL_BAR (window->vertical_scroll_bar);
12ba150f
JB
8513
8514 /* Unlink it from the condemned list. */
8515 {
8516 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
8517
8518 if (NILP (bar->prev))
8519 {
8520 /* If the prev pointer is nil, it must be the first in one of
8521 the lists. */
ab648270 8522 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
12ba150f
JB
8523 /* It's not condemned. Everything's fine. */
8524 return;
ab648270
JB
8525 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8526 window->vertical_scroll_bar))
8527 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
12ba150f
JB
8528 else
8529 /* If its prev pointer is nil, it must be at the front of
8530 one or the other! */
8531 abort ();
8532 }
8533 else
ab648270 8534 XSCROLL_BAR (bar->prev)->next = bar->next;
12ba150f
JB
8535
8536 if (! NILP (bar->next))
ab648270 8537 XSCROLL_BAR (bar->next)->prev = bar->prev;
12ba150f 8538
ab648270 8539 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 8540 bar->prev = Qnil;
e0c1aef2 8541 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
12ba150f 8542 if (! NILP (bar->next))
e0c1aef2 8543 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
12ba150f 8544 }
f451eb13
JB
8545}
8546
ab648270
JB
8547/* Remove all scroll bars on FRAME that haven't been saved since the
8548 last call to `*condemn_scroll_bars_hook'. */
06a2c219 8549
f451eb13 8550static void
ab648270 8551XTjudge_scroll_bars (f)
12ba150f 8552 FRAME_PTR f;
f451eb13 8553{
12ba150f 8554 Lisp_Object bar, next;
f451eb13 8555
ab648270 8556 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
cf7cb199
JB
8557
8558 /* Clear out the condemned list now so we won't try to process any
ab648270
JB
8559 more events on the hapless scroll bars. */
8560 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
cf7cb199
JB
8561
8562 for (; ! NILP (bar); bar = next)
f451eb13 8563 {
ab648270 8564 struct scroll_bar *b = XSCROLL_BAR (bar);
12ba150f 8565
ab648270 8566 x_scroll_bar_remove (b);
12ba150f
JB
8567
8568 next = b->next;
8569 b->next = b->prev = Qnil;
f451eb13 8570 }
12ba150f 8571
ab648270 8572 /* Now there should be no references to the condemned scroll bars,
12ba150f 8573 and they should get garbage-collected. */
f451eb13
JB
8574}
8575
8576
06a2c219
GM
8577/* Handle an Expose or GraphicsExpose event on a scroll bar. This
8578 is a no-op when using toolkit scroll bars.
ab648270
JB
8579
8580 This may be called from a signal handler, so we have to ignore GC
8581 mark bits. */
06a2c219 8582
f451eb13 8583static void
ab648270
JB
8584x_scroll_bar_expose (bar, event)
8585 struct scroll_bar *bar;
f451eb13
JB
8586 XEvent *event;
8587{
06a2c219
GM
8588#ifndef USE_TOOLKIT_SCROLL_BARS
8589
ab648270 8590 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8591 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8592 GC gc = f->output_data.x->normal_gc;
3cbd2e0b 8593 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
12ba150f 8594
f451eb13
JB
8595 BLOCK_INPUT;
8596
ab648270 8597 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
f451eb13 8598
06a2c219 8599 /* Draw a one-pixel border just inside the edges of the scroll bar. */
334208b7 8600 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13
JB
8601
8602 /* x, y, width, height */
d9cdbb3d 8603 0, 0,
3cbd2e0b 8604 XINT (bar->width) - 1 - width_trim - width_trim,
d9cdbb3d
RS
8605 XINT (bar->height) - 1);
8606
f451eb13 8607 UNBLOCK_INPUT;
06a2c219
GM
8608
8609#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8610}
8611
ab648270
JB
8612/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8613 is set to something other than no_event, it is enqueued.
8614
8615 This may be called from a signal handler, so we have to ignore GC
8616 mark bits. */
06a2c219 8617
5c187dee
GM
8618#ifndef USE_TOOLKIT_SCROLL_BARS
8619
f451eb13 8620static void
ab648270
JB
8621x_scroll_bar_handle_click (bar, event, emacs_event)
8622 struct scroll_bar *bar;
f451eb13
JB
8623 XEvent *event;
8624 struct input_event *emacs_event;
8625{
0299d313 8626 if (! GC_WINDOWP (bar->window))
12ba150f
JB
8627 abort ();
8628
ab648270 8629 emacs_event->kind = scroll_bar_click;
69388238 8630 emacs_event->code = event->xbutton.button - Button1;
0299d313
RS
8631 emacs_event->modifiers
8632 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8633 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8634 event->xbutton.state)
8635 | (event->type == ButtonRelease
8636 ? up_modifier
8637 : down_modifier));
12ba150f 8638 emacs_event->frame_or_window = bar->window;
0f8aabe9 8639 emacs_event->arg = Qnil;
f451eb13 8640 emacs_event->timestamp = event->xbutton.time;
12ba150f 8641 {
06a2c219 8642#if 0
d9cdbb3d 8643 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
0299d313 8644 int internal_height
d9cdbb3d 8645 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8646#endif
0299d313 8647 int top_range
d9cdbb3d 8648 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ab648270 8649 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
12ba150f
JB
8650
8651 if (y < 0) y = 0;
8652 if (y > top_range) y = top_range;
8653
8654 if (y < XINT (bar->start))
ab648270
JB
8655 emacs_event->part = scroll_bar_above_handle;
8656 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8657 emacs_event->part = scroll_bar_handle;
12ba150f 8658 else
ab648270 8659 emacs_event->part = scroll_bar_below_handle;
929787e1
JB
8660
8661 /* Just because the user has clicked on the handle doesn't mean
5116f055
JB
8662 they want to drag it. Lisp code needs to be able to decide
8663 whether or not we're dragging. */
929787e1 8664#if 0
12ba150f
JB
8665 /* If the user has just clicked on the handle, record where they're
8666 holding it. */
8667 if (event->type == ButtonPress
ab648270 8668 && emacs_event->part == scroll_bar_handle)
e0c1aef2 8669 XSETINT (bar->dragging, y - XINT (bar->start));
929787e1 8670#endif
12ba150f
JB
8671
8672 /* If the user has released the handle, set it to its final position. */
8673 if (event->type == ButtonRelease
8674 && ! NILP (bar->dragging))
8675 {
8676 int new_start = y - XINT (bar->dragging);
8677 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
f451eb13 8678
ab648270 8679 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
12ba150f
JB
8680 bar->dragging = Qnil;
8681 }
f451eb13 8682
5116f055
JB
8683 /* Same deal here as the other #if 0. */
8684#if 0
58769bee 8685 /* Clicks on the handle are always reported as occurring at the top of
12ba150f 8686 the handle. */
ab648270 8687 if (emacs_event->part == scroll_bar_handle)
12ba150f
JB
8688 emacs_event->x = bar->start;
8689 else
e0c1aef2 8690 XSETINT (emacs_event->x, y);
5116f055 8691#else
e0c1aef2 8692 XSETINT (emacs_event->x, y);
5116f055 8693#endif
f451eb13 8694
e0c1aef2 8695 XSETINT (emacs_event->y, top_range);
12ba150f
JB
8696 }
8697}
f451eb13 8698
ab648270
JB
8699/* Handle some mouse motion while someone is dragging the scroll bar.
8700
8701 This may be called from a signal handler, so we have to ignore GC
8702 mark bits. */
06a2c219 8703
f451eb13 8704static void
ab648270
JB
8705x_scroll_bar_note_movement (bar, event)
8706 struct scroll_bar *bar;
f451eb13
JB
8707 XEvent *event;
8708{
39d8bb4d
KH
8709 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8710
f451eb13
JB
8711 last_mouse_movement_time = event->xmotion.time;
8712
39d8bb4d 8713 f->mouse_moved = 1;
e0c1aef2 8714 XSETVECTOR (last_mouse_scroll_bar, bar);
f451eb13
JB
8715
8716 /* If we're dragging the bar, display it. */
ab648270 8717 if (! GC_NILP (bar->dragging))
f451eb13
JB
8718 {
8719 /* Where should the handle be now? */
12ba150f 8720 int new_start = event->xmotion.y - XINT (bar->dragging);
f451eb13 8721
12ba150f 8722 if (new_start != XINT (bar->start))
f451eb13 8723 {
12ba150f 8724 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
58769bee 8725
ab648270 8726 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
f451eb13
JB
8727 }
8728 }
f451eb13
JB
8729}
8730
5c187dee
GM
8731#endif /* !USE_TOOLKIT_SCROLL_BARS */
8732
12ba150f 8733/* Return information to the user about the current position of the mouse
ab648270 8734 on the scroll bar. */
06a2c219 8735
12ba150f 8736static void
334208b7
RS
8737x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8738 FRAME_PTR *fp;
12ba150f 8739 Lisp_Object *bar_window;
ab648270 8740 enum scroll_bar_part *part;
12ba150f
JB
8741 Lisp_Object *x, *y;
8742 unsigned long *time;
8743{
ab648270 8744 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
334208b7
RS
8745 Window w = SCROLL_BAR_X_WINDOW (bar);
8746 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f 8747 int win_x, win_y;
559cb2fb
JB
8748 Window dummy_window;
8749 int dummy_coord;
8750 unsigned int dummy_mask;
12ba150f 8751
cf7cb199
JB
8752 BLOCK_INPUT;
8753
ab648270 8754 /* Get the mouse's position relative to the scroll bar window, and
12ba150f 8755 report that. */
334208b7 8756 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
12ba150f 8757
559cb2fb
JB
8758 /* Root, child, root x and root y. */
8759 &dummy_window, &dummy_window,
8760 &dummy_coord, &dummy_coord,
12ba150f 8761
559cb2fb
JB
8762 /* Position relative to scroll bar. */
8763 &win_x, &win_y,
12ba150f 8764
559cb2fb
JB
8765 /* Mouse buttons and modifier keys. */
8766 &dummy_mask))
7a13e894 8767 ;
559cb2fb
JB
8768 else
8769 {
06a2c219 8770#if 0
559cb2fb 8771 int inside_height
d9cdbb3d 8772 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8773#endif
559cb2fb 8774 int top_range
d9cdbb3d 8775 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
559cb2fb
JB
8776
8777 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
8778
8779 if (! NILP (bar->dragging))
8780 win_y -= XINT (bar->dragging);
8781
8782 if (win_y < 0)
8783 win_y = 0;
8784 if (win_y > top_range)
8785 win_y = top_range;
8786
334208b7 8787 *fp = f;
7a13e894 8788 *bar_window = bar->window;
559cb2fb
JB
8789
8790 if (! NILP (bar->dragging))
8791 *part = scroll_bar_handle;
8792 else if (win_y < XINT (bar->start))
8793 *part = scroll_bar_above_handle;
8794 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8795 *part = scroll_bar_handle;
8796 else
8797 *part = scroll_bar_below_handle;
12ba150f 8798
e0c1aef2
KH
8799 XSETINT (*x, win_y);
8800 XSETINT (*y, top_range);
12ba150f 8801
39d8bb4d 8802 f->mouse_moved = 0;
559cb2fb
JB
8803 last_mouse_scroll_bar = Qnil;
8804 }
12ba150f 8805
559cb2fb 8806 *time = last_mouse_movement_time;
cf7cb199 8807
cf7cb199 8808 UNBLOCK_INPUT;
12ba150f
JB
8809}
8810
f451eb13 8811
dbc4e1c1 8812/* The screen has been cleared so we may have changed foreground or
ab648270
JB
8813 background colors, and the scroll bars may need to be redrawn.
8814 Clear out the scroll bars, and ask for expose events, so we can
dbc4e1c1
JB
8815 redraw them. */
8816
dfcf069d 8817void
ab648270 8818x_scroll_bar_clear (f)
dbc4e1c1
JB
8819 FRAME_PTR f;
8820{
06a2c219 8821#ifndef USE_TOOLKIT_SCROLL_BARS
dbc4e1c1
JB
8822 Lisp_Object bar;
8823
b80c363e
RS
8824 /* We can have scroll bars even if this is 0,
8825 if we just turned off scroll bar mode.
8826 But in that case we should not clear them. */
8827 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8828 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
8829 bar = XSCROLL_BAR (bar)->next)
8830 XClearArea (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
8831 0, 0, 0, 0, True);
06a2c219 8832#endif /* not USE_TOOLKIT_SCROLL_BARS */
dbc4e1c1
JB
8833}
8834
06a2c219 8835/* This processes Expose events from the menu-bar specific X event
19126e11 8836 loop in xmenu.c. This allows to redisplay the frame if necessary
06a2c219 8837 when handling menu-bar or pop-up items. */
3afe33e7 8838
06a2c219 8839int
3afe33e7
RS
8840process_expose_from_menu (event)
8841 XEvent event;
8842{
8843 FRAME_PTR f;
19126e11 8844 struct x_display_info *dpyinfo;
06a2c219 8845 int frame_exposed_p = 0;
3afe33e7 8846
f94397b5
KH
8847 BLOCK_INPUT;
8848
19126e11
KH
8849 dpyinfo = x_display_info_for_display (event.xexpose.display);
8850 f = x_window_to_frame (dpyinfo, event.xexpose.window);
3afe33e7
RS
8851 if (f)
8852 {
8853 if (f->async_visible == 0)
8854 {
8855 f->async_visible = 1;
8856 f->async_iconified = 0;
06c488fd 8857 f->output_data.x->has_been_visible = 1;
3afe33e7
RS
8858 SET_FRAME_GARBAGED (f);
8859 }
8860 else
8861 {
06a2c219
GM
8862 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
8863 event.xexpose.x, event.xexpose.y,
8864 event.xexpose.width, event.xexpose.height);
8865 frame_exposed_p = 1;
3afe33e7
RS
8866 }
8867 }
8868 else
8869 {
8870 struct scroll_bar *bar
8871 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 8872
3afe33e7
RS
8873 if (bar)
8874 x_scroll_bar_expose (bar, &event);
8875 }
f94397b5
KH
8876
8877 UNBLOCK_INPUT;
06a2c219 8878 return frame_exposed_p;
3afe33e7 8879}
09756a85
RS
8880\f
8881/* Define a queue to save up SelectionRequest events for later handling. */
8882
8883struct selection_event_queue
8884 {
8885 XEvent event;
8886 struct selection_event_queue *next;
8887 };
8888
8889static struct selection_event_queue *queue;
8890
8891/* Nonzero means queue up certain events--don't process them yet. */
06a2c219 8892
09756a85
RS
8893static int x_queue_selection_requests;
8894
8895/* Queue up an X event *EVENT, to be processed later. */
dbc4e1c1 8896
09756a85 8897static void
334208b7
RS
8898x_queue_event (f, event)
8899 FRAME_PTR f;
09756a85
RS
8900 XEvent *event;
8901{
8902 struct selection_event_queue *queue_tmp
06a2c219 8903 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
09756a85 8904
58769bee 8905 if (queue_tmp != NULL)
09756a85
RS
8906 {
8907 queue_tmp->event = *event;
8908 queue_tmp->next = queue;
8909 queue = queue_tmp;
8910 }
8911}
8912
8913/* Take all the queued events and put them back
8914 so that they get processed afresh. */
8915
8916static void
db3906fd
RS
8917x_unqueue_events (display)
8918 Display *display;
09756a85 8919{
58769bee 8920 while (queue != NULL)
09756a85
RS
8921 {
8922 struct selection_event_queue *queue_tmp = queue;
db3906fd 8923 XPutBackEvent (display, &queue_tmp->event);
09756a85 8924 queue = queue_tmp->next;
06a2c219 8925 xfree ((char *)queue_tmp);
09756a85
RS
8926 }
8927}
8928
8929/* Start queuing SelectionRequest events. */
8930
8931void
db3906fd
RS
8932x_start_queuing_selection_requests (display)
8933 Display *display;
09756a85
RS
8934{
8935 x_queue_selection_requests++;
8936}
8937
8938/* Stop queuing SelectionRequest events. */
8939
8940void
db3906fd
RS
8941x_stop_queuing_selection_requests (display)
8942 Display *display;
09756a85
RS
8943{
8944 x_queue_selection_requests--;
db3906fd 8945 x_unqueue_events (display);
09756a85 8946}
f451eb13
JB
8947\f
8948/* The main X event-reading loop - XTread_socket. */
dc6f92b8 8949
06a2c219 8950/* Time stamp of enter window event. This is only used by XTread_socket,
dc6f92b8
JB
8951 but we have to put it out here, since static variables within functions
8952 sometimes don't work. */
06a2c219 8953
dc6f92b8
JB
8954static Time enter_timestamp;
8955
11edeb03 8956/* This holds the state XLookupString needs to implement dead keys
58769bee 8957 and other tricks known as "compose processing". _X Window System_
11edeb03
JB
8958 says that a portable program can't use this, but Stephen Gildea assures
8959 me that letting the compiler initialize it to zeros will work okay.
8960
8961 This must be defined outside of XTread_socket, for the same reasons
06a2c219
GM
8962 given for enter_time stamp, above. */
8963
11edeb03
JB
8964static XComposeStatus compose_status;
8965
10e6549c
RS
8966/* Record the last 100 characters stored
8967 to help debug the loss-of-chars-during-GC problem. */
06a2c219 8968
2224b905
RS
8969static int temp_index;
8970static short temp_buffer[100];
10e6549c 8971
7a13e894
RS
8972/* Set this to nonzero to fake an "X I/O error"
8973 on a particular display. */
06a2c219 8974
7a13e894
RS
8975struct x_display_info *XTread_socket_fake_io_error;
8976
2224b905
RS
8977/* When we find no input here, we occasionally do a no-op command
8978 to verify that the X server is still running and we can still talk with it.
8979 We try all the open displays, one by one.
8980 This variable is used for cycling thru the displays. */
06a2c219 8981
2224b905
RS
8982static struct x_display_info *next_noop_dpyinfo;
8983
06a2c219
GM
8984#define SET_SAVED_MENU_EVENT(size) \
8985 do \
8986 { \
8987 if (f->output_data.x->saved_menu_event == 0) \
8988 f->output_data.x->saved_menu_event \
8989 = (XEvent *) xmalloc (sizeof (XEvent)); \
8990 bcopy (&event, f->output_data.x->saved_menu_event, size); \
8991 if (numchars >= 1) \
8992 { \
8993 bufp->kind = menu_bar_activate_event; \
8994 XSETFRAME (bufp->frame_or_window, f); \
0f8aabe9 8995 bufp->arg = Qnil; \
06a2c219
GM
8996 bufp++; \
8997 count++; \
8998 numchars--; \
8999 } \
9000 } \
9001 while (0)
9002
8805890a 9003#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
06a2c219 9004#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8805890a 9005
dc6f92b8
JB
9006/* Read events coming from the X server.
9007 This routine is called by the SIGIO handler.
9008 We return as soon as there are no more events to be read.
9009
9010 Events representing keys are stored in buffer BUFP,
9011 which can hold up to NUMCHARS characters.
9012 We return the number of characters stored into the buffer,
9013 thus pretending to be `read'.
9014
dc6f92b8
JB
9015 EXPECTED is nonzero if the caller knows input is available. */
9016
7c5283e4 9017int
f66868ba 9018XTread_socket (sd, bufp, numchars, expected)
dc6f92b8 9019 register int sd;
8805890a
KH
9020 /* register */ struct input_event *bufp;
9021 /* register */ int numchars;
dc6f92b8
JB
9022 int expected;
9023{
9024 int count = 0;
9025 int nbytes = 0;
dc6f92b8 9026 XEvent event;
f676886a 9027 struct frame *f;
66f55a9d 9028 int event_found = 0;
334208b7 9029 struct x_display_info *dpyinfo;
379b5ac0 9030 struct coding_system coding;
dc6f92b8 9031
9ac0d9e0 9032 if (interrupt_input_blocked)
dc6f92b8 9033 {
9ac0d9e0 9034 interrupt_input_pending = 1;
dc6f92b8
JB
9035 return -1;
9036 }
9037
9ac0d9e0 9038 interrupt_input_pending = 0;
dc6f92b8 9039 BLOCK_INPUT;
c0a04927
RS
9040
9041 /* So people can tell when we have read the available input. */
9042 input_signal_count++;
9043
dc6f92b8 9044 if (numchars <= 0)
06a2c219 9045 abort (); /* Don't think this happens. */
dc6f92b8 9046
bde5503b
GM
9047 ++handling_signal;
9048
379b5ac0
KH
9049 /* The input should be decoded if it is from XIM. Currently the
9050 locale of XIM is the same as that of the system. So, we can use
9051 Vlocale_coding_system which is initialized properly at Emacs
9052 startup time. */
9053 setup_coding_system (Vlocale_coding_system, &coding);
9054 coding.src_multibyte = 0;
9055 coding.dst_multibyte = 1;
9056 /* The input is converted to events, thus we can't handle
9057 composition. Anyway, there's no XIM that gives us composition
9058 information. */
9059 coding.composing = COMPOSITION_DISABLED;
9060
7a13e894
RS
9061 /* Find the display we are supposed to read input for.
9062 It's the one communicating on descriptor SD. */
9063 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9064 {
9065#if 0 /* This ought to be unnecessary; let's verify it. */
dc6f92b8 9066#ifdef FIOSNBIO
7a13e894
RS
9067 /* If available, Xlib uses FIOSNBIO to make the socket
9068 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
e6cbea31 9069 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
06a2c219 9070 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7a13e894 9071 fcntl (dpyinfo->connection, F_SETFL, 0);
c118dd06 9072#endif /* ! defined (FIOSNBIO) */
7a13e894 9073#endif
dc6f92b8 9074
7a13e894
RS
9075#if 0 /* This code can't be made to work, with multiple displays,
9076 and appears not to be used on any system any more.
9077 Also keyboard.c doesn't turn O_NDELAY on and off
9078 for X connections. */
dc6f92b8
JB
9079#ifndef SIGIO
9080#ifndef HAVE_SELECT
7a13e894
RS
9081 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9082 {
9083 extern int read_alarm_should_throw;
9084 read_alarm_should_throw = 1;
9085 XPeekEvent (dpyinfo->display, &event);
9086 read_alarm_should_throw = 0;
9087 }
c118dd06
JB
9088#endif /* HAVE_SELECT */
9089#endif /* SIGIO */
7a13e894 9090#endif
dc6f92b8 9091
7a13e894
RS
9092 /* For debugging, this gives a way to fake an I/O error. */
9093 if (dpyinfo == XTread_socket_fake_io_error)
9094 {
9095 XTread_socket_fake_io_error = 0;
9096 x_io_error_quitter (dpyinfo->display);
9097 }
dc6f92b8 9098
06a2c219 9099 while (XPending (dpyinfo->display))
dc6f92b8 9100 {
7a13e894 9101 XNextEvent (dpyinfo->display, &event);
06a2c219 9102
531483fb 9103#ifdef HAVE_X_I18N
d1bc4182 9104 {
f2be1146
GM
9105 /* Filter events for the current X input method.
9106 XFilterEvent returns non-zero if the input method has
9107 consumed the event. We pass the frame's X window to
9108 XFilterEvent because that's the one for which the IC
9109 was created. */
f5d11644
GM
9110 struct frame *f1 = x_any_window_to_frame (dpyinfo,
9111 event.xclient.window);
9112 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
d1bc4182
RS
9113 break;
9114 }
0cd6403b 9115#endif
7a13e894
RS
9116 event_found = 1;
9117
9118 switch (event.type)
9119 {
9120 case ClientMessage:
c047688c 9121 {
7a13e894
RS
9122 if (event.xclient.message_type
9123 == dpyinfo->Xatom_wm_protocols
9124 && event.xclient.format == 32)
c047688c 9125 {
7a13e894
RS
9126 if (event.xclient.data.l[0]
9127 == dpyinfo->Xatom_wm_take_focus)
c047688c 9128 {
8c1a6a84
RS
9129 /* Use x_any_window_to_frame because this
9130 could be the shell widget window
9131 if the frame has no title bar. */
9132 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6c183ba5
RS
9133#ifdef HAVE_X_I18N
9134 /* Not quite sure this is needed -pd */
8c1a6a84 9135 if (f && FRAME_XIC (f))
6c183ba5
RS
9136 XSetICFocus (FRAME_XIC (f));
9137#endif
f1da8f06
GM
9138#if 0 /* Emacs sets WM hints whose `input' field is `true'. This
9139 instructs the WM to set the input focus automatically for
9140 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
9141 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
9142 it has set the focus. So, XSetInputFocus below is not
9143 needed.
9144
9145 The call to XSetInputFocus below has also caused trouble. In
9146 cases where the XSetInputFocus done by the WM and the one
9147 below are temporally close (on a fast machine), the call
9148 below can generate additional FocusIn events which confuse
9149 Emacs. */
9150
bf7253f4
RS
9151 /* Since we set WM_TAKE_FOCUS, we must call
9152 XSetInputFocus explicitly. But not if f is null,
9153 since that might be an event for a deleted frame. */
7a13e894 9154 if (f)
bf7253f4
RS
9155 {
9156 Display *d = event.xclient.display;
9157 /* Catch and ignore errors, in case window has been
9158 iconified by a window manager such as GWM. */
9159 int count = x_catch_errors (d);
9160 XSetInputFocus (d, event.xclient.window,
e1f6572f
RS
9161 /* The ICCCM says this is
9162 the only valid choice. */
9163 RevertToParent,
bf7253f4
RS
9164 event.xclient.data.l[1]);
9165 /* This is needed to detect the error
9166 if there is an error. */
9167 XSync (d, False);
9168 x_uncatch_errors (d, count);
9169 }
7a13e894 9170 /* Not certain about handling scroll bars here */
f1da8f06 9171#endif /* 0 */
c047688c 9172 }
7a13e894
RS
9173 else if (event.xclient.data.l[0]
9174 == dpyinfo->Xatom_wm_save_yourself)
9175 {
9176 /* Save state modify the WM_COMMAND property to
06a2c219 9177 something which can reinstate us. This notifies
7a13e894
RS
9178 the session manager, who's looking for such a
9179 PropertyNotify. Can restart processing when
06a2c219 9180 a keyboard or mouse event arrives. */
7a13e894
RS
9181 if (numchars > 0)
9182 {
19126e11
KH
9183 f = x_top_window_to_frame (dpyinfo,
9184 event.xclient.window);
7a13e894
RS
9185
9186 /* This is just so we only give real data once
9187 for a single Emacs process. */
b86bd3dd 9188 if (f == SELECTED_FRAME ())
7a13e894
RS
9189 XSetCommand (FRAME_X_DISPLAY (f),
9190 event.xclient.window,
9191 initial_argv, initial_argc);
f000f5c5 9192 else if (f)
7a13e894
RS
9193 XSetCommand (FRAME_X_DISPLAY (f),
9194 event.xclient.window,
9195 0, 0);
9196 }
9197 }
9198 else if (event.xclient.data.l[0]
9199 == dpyinfo->Xatom_wm_delete_window)
1fb20991 9200 {
19126e11
KH
9201 struct frame *f
9202 = x_any_window_to_frame (dpyinfo,
9203 event.xclient.window);
1fb20991 9204
7a13e894
RS
9205 if (f)
9206 {
9207 if (numchars == 0)
9208 abort ();
1fb20991 9209
7a13e894
RS
9210 bufp->kind = delete_window_event;
9211 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9212 bufp->arg = Qnil;
7a13e894
RS
9213 bufp++;
9214
9215 count += 1;
9216 numchars -= 1;
9217 }
1fb20991 9218 }
c047688c 9219 }
7a13e894
RS
9220 else if (event.xclient.message_type
9221 == dpyinfo->Xatom_wm_configure_denied)
9222 {
9223 }
9224 else if (event.xclient.message_type
9225 == dpyinfo->Xatom_wm_window_moved)
9226 {
9227 int new_x, new_y;
19126e11
KH
9228 struct frame *f
9229 = x_window_to_frame (dpyinfo, event.xclient.window);
58769bee 9230
7a13e894
RS
9231 new_x = event.xclient.data.s[0];
9232 new_y = event.xclient.data.s[1];
1fb20991 9233
7a13e894
RS
9234 if (f)
9235 {
7556890b
RS
9236 f->output_data.x->left_pos = new_x;
9237 f->output_data.x->top_pos = new_y;
7a13e894 9238 }
1fb20991 9239 }
0fdff6bb 9240#ifdef HACK_EDITRES
7a13e894
RS
9241 else if (event.xclient.message_type
9242 == dpyinfo->Xatom_editres)
9243 {
19126e11
KH
9244 struct frame *f
9245 = x_any_window_to_frame (dpyinfo, event.xclient.window);
7556890b 9246 _XEditResCheckMessages (f->output_data.x->widget, NULL,
19126e11 9247 &event, NULL);
7a13e894 9248 }
0fdff6bb 9249#endif /* HACK_EDITRES */
06a2c219
GM
9250 else if ((event.xclient.message_type
9251 == dpyinfo->Xatom_DONE)
9252 || (event.xclient.message_type
9253 == dpyinfo->Xatom_PAGE))
9254 {
9255 /* Ghostview job completed. Kill it. We could
9256 reply with "Next" if we received "Page", but we
9257 currently never do because we are interested in
9258 images, only, which should have 1 page. */
06a2c219
GM
9259 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9260 struct frame *f
9261 = x_window_to_frame (dpyinfo, event.xclient.window);
9262 x_kill_gs_process (pixmap, f);
9263 expose_frame (f, 0, 0, 0, 0);
9264 }
9265#ifdef USE_TOOLKIT_SCROLL_BARS
9266 /* Scroll bar callbacks send a ClientMessage from which
9267 we construct an input_event. */
9268 else if (event.xclient.message_type
9269 == dpyinfo->Xatom_Scrollbar)
9270 {
9271 x_scroll_bar_to_input_event (&event, bufp);
9272 ++bufp, ++count, --numchars;
9273 goto out;
9274 }
9275#endif /* USE_TOOLKIT_SCROLL_BARS */
9276 else
9277 goto OTHER;
7a13e894
RS
9278 }
9279 break;
dc6f92b8 9280
7a13e894 9281 case SelectionNotify:
3afe33e7 9282#ifdef USE_X_TOOLKIT
19126e11 9283 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
7a13e894 9284 goto OTHER;
3afe33e7 9285#endif /* not USE_X_TOOLKIT */
dfcf069d 9286 x_handle_selection_notify (&event.xselection);
7a13e894 9287 break;
d56a553a 9288
06a2c219 9289 case SelectionClear: /* Someone has grabbed ownership. */
3afe33e7 9290#ifdef USE_X_TOOLKIT
19126e11 9291 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
7a13e894 9292 goto OTHER;
3afe33e7 9293#endif /* USE_X_TOOLKIT */
7a13e894
RS
9294 {
9295 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
d56a553a 9296
7a13e894
RS
9297 if (numchars == 0)
9298 abort ();
d56a553a 9299
7a13e894
RS
9300 bufp->kind = selection_clear_event;
9301 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9302 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9303 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9304 bufp->frame_or_window = Qnil;
0f8aabe9 9305 bufp->arg = Qnil;
7a13e894 9306 bufp++;
d56a553a 9307
7a13e894
RS
9308 count += 1;
9309 numchars -= 1;
9310 }
9311 break;
dc6f92b8 9312
06a2c219 9313 case SelectionRequest: /* Someone wants our selection. */
3afe33e7 9314#ifdef USE_X_TOOLKIT
19126e11 9315 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
7a13e894 9316 goto OTHER;
3afe33e7 9317#endif /* USE_X_TOOLKIT */
7a13e894 9318 if (x_queue_selection_requests)
19126e11 9319 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
7a13e894
RS
9320 &event);
9321 else
9322 {
9323 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
dc6f92b8 9324
7a13e894
RS
9325 if (numchars == 0)
9326 abort ();
9327
9328 bufp->kind = selection_request_event;
9329 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9330 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9331 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9332 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9333 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9334 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9335 bufp->frame_or_window = Qnil;
0f8aabe9 9336 bufp->arg = Qnil;
7a13e894
RS
9337 bufp++;
9338
9339 count += 1;
9340 numchars -= 1;
9341 }
9342 break;
9343
9344 case PropertyNotify:
3afe33e7 9345#ifdef USE_X_TOOLKIT
19126e11 9346 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
7a13e894 9347 goto OTHER;
3afe33e7 9348#endif /* not USE_X_TOOLKIT */
dfcf069d 9349 x_handle_property_notify (&event.xproperty);
7a13e894 9350 break;
dc6f92b8 9351
7a13e894 9352 case ReparentNotify:
19126e11 9353 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
7a13e894
RS
9354 if (f)
9355 {
9356 int x, y;
7556890b 9357 f->output_data.x->parent_desc = event.xreparent.parent;
7a13e894 9358 x_real_positions (f, &x, &y);
7556890b
RS
9359 f->output_data.x->left_pos = x;
9360 f->output_data.x->top_pos = y;
7a13e894
RS
9361 }
9362 break;
3bd330d4 9363
7a13e894 9364 case Expose:
19126e11 9365 f = x_window_to_frame (dpyinfo, event.xexpose.window);
7a13e894 9366 if (f)
dc6f92b8 9367 {
7a13e894
RS
9368 if (f->async_visible == 0)
9369 {
9370 f->async_visible = 1;
9371 f->async_iconified = 0;
06c488fd 9372 f->output_data.x->has_been_visible = 1;
7a13e894
RS
9373 SET_FRAME_GARBAGED (f);
9374 }
9375 else
06a2c219
GM
9376 expose_frame (x_window_to_frame (dpyinfo,
9377 event.xexpose.window),
9378 event.xexpose.x, event.xexpose.y,
9379 event.xexpose.width, event.xexpose.height);
dc6f92b8
JB
9380 }
9381 else
7a13e894 9382 {
06a2c219
GM
9383#ifdef USE_TOOLKIT_SCROLL_BARS
9384 /* Dispatch event to the widget. */
9385 goto OTHER;
9386#else /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9387 struct scroll_bar *bar
9388 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 9389
7a13e894
RS
9390 if (bar)
9391 x_scroll_bar_expose (bar, &event);
3afe33e7 9392#ifdef USE_X_TOOLKIT
7a13e894
RS
9393 else
9394 goto OTHER;
3afe33e7 9395#endif /* USE_X_TOOLKIT */
06a2c219 9396#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9397 }
9398 break;
dc6f92b8 9399
7a13e894
RS
9400 case GraphicsExpose: /* This occurs when an XCopyArea's
9401 source area was obscured or not
9402 available.*/
19126e11 9403 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
7a13e894
RS
9404 if (f)
9405 {
06a2c219
GM
9406 expose_frame (f,
9407 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9408 event.xgraphicsexpose.width,
9409 event.xgraphicsexpose.height);
7a13e894 9410 }
3afe33e7 9411#ifdef USE_X_TOOLKIT
7a13e894
RS
9412 else
9413 goto OTHER;
3afe33e7 9414#endif /* USE_X_TOOLKIT */
7a13e894 9415 break;
dc6f92b8 9416
7a13e894 9417 case NoExpose: /* This occurs when an XCopyArea's
06a2c219
GM
9418 source area was completely
9419 available */
7a13e894 9420 break;
dc6f92b8 9421
7a13e894 9422 case UnmapNotify:
06a2c219
GM
9423 /* Redo the mouse-highlight after the tooltip has gone. */
9424 if (event.xmap.window == tip_window)
9425 {
9426 tip_window = 0;
9427 redo_mouse_highlight ();
9428 }
9429
91ea2a7a 9430 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
7a13e894
RS
9431 if (f) /* F may no longer exist if
9432 the frame was deleted. */
9433 {
9434 /* While a frame is unmapped, display generation is
9435 disabled; you don't want to spend time updating a
9436 display that won't ever be seen. */
9437 f->async_visible = 0;
9438 /* We can't distinguish, from the event, whether the window
9439 has become iconified or invisible. So assume, if it
9440 was previously visible, than now it is iconified.
1aa6072f
RS
9441 But x_make_frame_invisible clears both
9442 the visible flag and the iconified flag;
9443 and that way, we know the window is not iconified now. */
7a13e894 9444 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
1aa6072f
RS
9445 {
9446 f->async_iconified = 1;
bddd097c 9447
1aa6072f
RS
9448 bufp->kind = iconify_event;
9449 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9450 bufp->arg = Qnil;
1aa6072f
RS
9451 bufp++;
9452 count++;
9453 numchars--;
9454 }
7a13e894 9455 }
7a13e894 9456 goto OTHER;
dc6f92b8 9457
7a13e894 9458 case MapNotify:
06a2c219
GM
9459 if (event.xmap.window == tip_window)
9460 /* The tooltip has been drawn already. Avoid
9461 the SET_FRAME_GARBAGED below. */
9462 goto OTHER;
9463
9464 /* We use x_top_window_to_frame because map events can
9465 come for sub-windows and they don't mean that the
9466 frame is visible. */
19126e11 9467 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
7a13e894
RS
9468 if (f)
9469 {
9470 f->async_visible = 1;
9471 f->async_iconified = 0;
06c488fd 9472 f->output_data.x->has_been_visible = 1;
dc6f92b8 9473
7a13e894
RS
9474 /* wait_reading_process_input will notice this and update
9475 the frame's display structures. */
9476 SET_FRAME_GARBAGED (f);
bddd097c 9477
d806e720
RS
9478 if (f->iconified)
9479 {
9480 bufp->kind = deiconify_event;
9481 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9482 bufp->arg = Qnil;
d806e720
RS
9483 bufp++;
9484 count++;
9485 numchars--;
9486 }
e73ec6fa 9487 else if (! NILP (Vframe_list)
8e713be6 9488 && ! NILP (XCDR (Vframe_list)))
78aa2ba5
KH
9489 /* Force a redisplay sooner or later
9490 to update the frame titles
9491 in case this is the second frame. */
9492 record_asynch_buffer_change ();
7a13e894 9493 }
7a13e894 9494 goto OTHER;
dc6f92b8 9495
7a13e894 9496 case KeyPress:
19126e11 9497 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
f451eb13 9498
06a2c219
GM
9499#ifdef USE_MOTIF
9500 /* I couldn't find a way to prevent LessTif scroll bars
9501 from consuming key events. */
9502 if (f == 0)
9503 {
9504 Widget widget = XtWindowToWidget (dpyinfo->display,
9505 event.xkey.window);
9506 if (widget && XmIsScrollBar (widget))
9507 {
9508 widget = XtParent (widget);
9509 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9510 }
9511 }
9512#endif /* USE_MOTIF */
9513
7a13e894
RS
9514 if (f != 0)
9515 {
9516 KeySym keysym, orig_keysym;
379b5ac0
KH
9517 /* al%imercury@uunet.uu.net says that making this 81
9518 instead of 80 fixed a bug whereby meta chars made
9519 his Emacs hang.
9520
9521 It seems that some version of XmbLookupString has
9522 a bug of not returning XBufferOverflow in
9523 status_return even if the input is too long to
9524 fit in 81 bytes. So, we must prepare sufficient
9525 bytes for copy_buffer. 513 bytes (256 chars for
9526 two-byte character set) seems to be a faily good
9527 approximation. -- 2000.8.10 handa@etl.go.jp */
9528 unsigned char copy_buffer[513];
9529 unsigned char *copy_bufptr = copy_buffer;
9530 int copy_bufsiz = sizeof (copy_buffer);
7a13e894 9531 int modifiers;
64bb1782 9532
7a13e894
RS
9533 event.xkey.state
9534 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9535 extra_keyboard_modifiers);
9536 modifiers = event.xkey.state;
3a2712f9 9537
7a13e894 9538 /* This will have to go some day... */
752a043f 9539
7a13e894
RS
9540 /* make_lispy_event turns chars into control chars.
9541 Don't do it here because XLookupString is too eager. */
9542 event.xkey.state &= ~ControlMask;
5d46f928
RS
9543 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9544 | dpyinfo->super_mod_mask
9545 | dpyinfo->hyper_mod_mask
9546 | dpyinfo->alt_mod_mask);
9547
1cf4a0d1
RS
9548 /* In case Meta is ComposeCharacter,
9549 clear its status. According to Markus Ehrnsperger
9550 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9551 this enables ComposeCharacter to work whether or
9552 not it is combined with Meta. */
9553 if (modifiers & dpyinfo->meta_mod_mask)
9554 bzero (&compose_status, sizeof (compose_status));
9555
6c183ba5
RS
9556#ifdef HAVE_X_I18N
9557 if (FRAME_XIC (f))
9558 {
f5d11644
GM
9559 Status status_return;
9560
6c183ba5 9561 nbytes = XmbLookupString (FRAME_XIC (f),
f5d11644
GM
9562 &event.xkey, copy_bufptr,
9563 copy_bufsiz, &keysym,
6c183ba5 9564 &status_return);
f5d11644
GM
9565 if (status_return == XBufferOverflow)
9566 {
9567 copy_bufsiz = nbytes + 1;
9568 copy_bufptr = (char *) alloca (copy_bufsiz);
9569 nbytes = XmbLookupString (FRAME_XIC (f),
9570 &event.xkey, copy_bufptr,
9571 copy_bufsiz, &keysym,
9572 &status_return);
9573 }
9574
1decb680
PE
9575 if (status_return == XLookupNone)
9576 break;
9577 else if (status_return == XLookupChars)
fdd9d55e
GM
9578 {
9579 keysym = NoSymbol;
9580 modifiers = 0;
9581 }
1decb680
PE
9582 else if (status_return != XLookupKeySym
9583 && status_return != XLookupBoth)
9584 abort ();
6c183ba5
RS
9585 }
9586 else
379b5ac0
KH
9587 nbytes = XLookupString (&event.xkey, copy_bufptr,
9588 copy_bufsiz, &keysym,
9589 &compose_status);
6c183ba5 9590#else
379b5ac0
KH
9591 nbytes = XLookupString (&event.xkey, copy_bufptr,
9592 copy_bufsiz, &keysym,
9593 &compose_status);
6c183ba5 9594#endif
dc6f92b8 9595
7a13e894 9596 orig_keysym = keysym;
55123275 9597
7a13e894
RS
9598 if (numchars > 1)
9599 {
9600 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9601 || keysym == XK_Delete
1097aea0 9602#ifdef XK_ISO_Left_Tab
441affdb 9603 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
1097aea0 9604#endif
852bff8f 9605 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
7a13e894
RS
9606 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9607 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
c34790e0 9608#ifdef HPUX
7a13e894
RS
9609 /* This recognizes the "extended function keys".
9610 It seems there's no cleaner way.
9611 Test IsModifierKey to avoid handling mode_switch
9612 incorrectly. */
9613 || ((unsigned) (keysym) >= XK_Select
9614 && (unsigned)(keysym) < XK_KP_Space)
69388238
RS
9615#endif
9616#ifdef XK_dead_circumflex
7a13e894 9617 || orig_keysym == XK_dead_circumflex
69388238
RS
9618#endif
9619#ifdef XK_dead_grave
7a13e894 9620 || orig_keysym == XK_dead_grave
69388238
RS
9621#endif
9622#ifdef XK_dead_tilde
7a13e894 9623 || orig_keysym == XK_dead_tilde
69388238
RS
9624#endif
9625#ifdef XK_dead_diaeresis
7a13e894 9626 || orig_keysym == XK_dead_diaeresis
69388238
RS
9627#endif
9628#ifdef XK_dead_macron
7a13e894 9629 || orig_keysym == XK_dead_macron
69388238
RS
9630#endif
9631#ifdef XK_dead_degree
7a13e894 9632 || orig_keysym == XK_dead_degree
69388238
RS
9633#endif
9634#ifdef XK_dead_acute
7a13e894 9635 || orig_keysym == XK_dead_acute
69388238
RS
9636#endif
9637#ifdef XK_dead_cedilla
7a13e894 9638 || orig_keysym == XK_dead_cedilla
69388238
RS
9639#endif
9640#ifdef XK_dead_breve
7a13e894 9641 || orig_keysym == XK_dead_breve
69388238
RS
9642#endif
9643#ifdef XK_dead_ogonek
7a13e894 9644 || orig_keysym == XK_dead_ogonek
69388238
RS
9645#endif
9646#ifdef XK_dead_caron
7a13e894 9647 || orig_keysym == XK_dead_caron
69388238
RS
9648#endif
9649#ifdef XK_dead_doubleacute
7a13e894 9650 || orig_keysym == XK_dead_doubleacute
69388238
RS
9651#endif
9652#ifdef XK_dead_abovedot
7a13e894 9653 || orig_keysym == XK_dead_abovedot
c34790e0 9654#endif
7a13e894
RS
9655 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9656 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9657 /* Any "vendor-specific" key is ok. */
9658 || (orig_keysym & (1 << 28)))
9659 && ! (IsModifierKey (orig_keysym)
7719aa06
RS
9660#ifndef HAVE_X11R5
9661#ifdef XK_Mode_switch
7a13e894 9662 || ((unsigned)(orig_keysym) == XK_Mode_switch)
7719aa06
RS
9663#endif
9664#ifdef XK_Num_Lock
7a13e894 9665 || ((unsigned)(orig_keysym) == XK_Num_Lock)
7719aa06
RS
9666#endif
9667#endif /* not HAVE_X11R5 */
7a13e894 9668 ))
dc6f92b8 9669 {
10e6549c
RS
9670 if (temp_index == sizeof temp_buffer / sizeof (short))
9671 temp_index = 0;
7a13e894
RS
9672 temp_buffer[temp_index++] = keysym;
9673 bufp->kind = non_ascii_keystroke;
9674 bufp->code = keysym;
e0c1aef2 9675 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9676 bufp->arg = Qnil;
334208b7
RS
9677 bufp->modifiers
9678 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9679 modifiers);
1113d9db 9680 bufp->timestamp = event.xkey.time;
dc6f92b8 9681 bufp++;
7a13e894
RS
9682 count++;
9683 numchars--;
dc6f92b8 9684 }
7a13e894
RS
9685 else if (numchars > nbytes)
9686 {
9687 register int i;
379b5ac0
KH
9688 register int c;
9689 unsigned char *p, *pend;
9690 int nchars, len;
7a13e894
RS
9691
9692 for (i = 0; i < nbytes; i++)
9693 {
379b5ac0
KH
9694 if (temp_index == (sizeof temp_buffer
9695 / sizeof (short)))
7a13e894 9696 temp_index = 0;
379b5ac0
KH
9697 temp_buffer[temp_index++] = copy_bufptr[i];
9698 }
9699
9700 if (/* If the event is not from XIM, */
9701 event.xkey.keycode != 0
9702 /* or the current locale doesn't request
9703 decoding of the intup data, ... */
9704 || coding.type == coding_type_raw_text
9705 || coding.type == coding_type_no_conversion)
9706 {
9707 /* ... we can use the input data as is. */
9708 nchars = nbytes;
9709 }
9710 else
9711 {
9712 /* We have to decode the input data. */
9713 int require;
9714 unsigned char *p;
9715
9716 require = decoding_buffer_size (&coding, nbytes);
9717 p = (unsigned char *) alloca (require);
9718 coding.mode |= CODING_MODE_LAST_BLOCK;
9719 decode_coding (&coding, copy_bufptr, p,
9720 nbytes, require);
9721 nbytes = coding.produced;
9722 nchars = coding.produced_char;
9723 copy_bufptr = p;
9724 }
9725
9726 /* Convert the input data to a sequence of
9727 character events. */
9728 for (i = 0; i < nbytes; i += len)
9729 {
9730 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
9731 nbytes - i, len);
9732 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
9733 ? ascii_keystroke
9734 : multibyte_char_keystroke);
9735 bufp->code = c;
7a13e894 9736 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9737 bufp->arg = Qnil;
7a13e894
RS
9738 bufp->modifiers
9739 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9740 modifiers);
9741 bufp->timestamp = event.xkey.time;
9742 bufp++;
9743 }
9744
379b5ac0
KH
9745 count += nchars;
9746 numchars -= nchars;
1decb680
PE
9747
9748 if (keysym == NoSymbol)
9749 break;
7a13e894
RS
9750 }
9751 else
9752 abort ();
dc6f92b8 9753 }
10e6549c
RS
9754 else
9755 abort ();
dc6f92b8 9756 }
59ddecde
GM
9757#ifdef HAVE_X_I18N
9758 /* Don't dispatch this event since XtDispatchEvent calls
9759 XFilterEvent, and two calls in a row may freeze the
9760 client. */
9761 break;
9762#else
717ca130 9763 goto OTHER;
59ddecde 9764#endif
f451eb13 9765
f5d11644 9766 case KeyRelease:
59ddecde
GM
9767#ifdef HAVE_X_I18N
9768 /* Don't dispatch this event since XtDispatchEvent calls
9769 XFilterEvent, and two calls in a row may freeze the
9770 client. */
9771 break;
9772#else
f5d11644 9773 goto OTHER;
59ddecde 9774#endif
f5d11644 9775
7a13e894 9776 /* Here's a possible interpretation of the whole
06a2c219
GM
9777 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
9778 you get a FocusIn event, you have to get a FocusOut
9779 event before you relinquish the focus. If you
9780 haven't received a FocusIn event, then a mere
9781 LeaveNotify is enough to free you. */
f451eb13 9782
7a13e894 9783 case EnterNotify:
06a2c219
GM
9784 {
9785 int from_menu_bar_p = 0;
9786
9787 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
9788
9789#ifdef LESSTIF_VERSION
9790 /* When clicking outside of a menu bar popup to close
9791 it, we get a FocusIn/ EnterNotify sequence of
9792 events. The flag event.xcrossing.focus is not set
9793 in the EnterNotify event of that sequence because
9794 the focus is in the menu bar,
9795 event.xcrossing.window is the frame's X window.
9796 Unconditionally setting the focus frame to null in
9797 this case is not the right thing, because no event
9798 follows that could set the focus frame to the right
9799 value.
9800
9801 This could be a LessTif bug, but I wasn't able to
9802 reproduce the behavior in a simple test program.
9803
9804 (gerd, LessTif 0.88.1). */
9805
9806 if (!event.xcrossing.focus
9807 && f
9808 && f->output_data.x->menubar_widget)
9809 {
9810 Window focus;
9811 int revert;
9812
9813 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9814 if (focus == XtWindow (f->output_data.x->menubar_widget))
9815 from_menu_bar_p = 1;
9816 }
9817#endif /* LESSTIF_VERSION */
6d4238f3 9818
06a2c219
GM
9819 if (event.xcrossing.focus || from_menu_bar_p)
9820 {
9821 /* Avoid nasty pop/raise loops. */
9822 if (f && (!(f->auto_raise)
9823 || !(f->auto_lower)
9824 || (event.xcrossing.time - enter_timestamp) > 500))
9825 {
9826 x_new_focus_frame (dpyinfo, f);
9827 enter_timestamp = event.xcrossing.time;
9828 }
9829 }
9830 else if (f == dpyinfo->x_focus_frame)
9831 x_new_focus_frame (dpyinfo, 0);
9832
9833 /* EnterNotify counts as mouse movement,
9834 so update things that depend on mouse position. */
9835 if (f && !f->output_data.x->busy_p)
9836 note_mouse_movement (f, &event.xmotion);
9837 goto OTHER;
9838 }
dc6f92b8 9839
7a13e894 9840 case FocusIn:
19126e11 9841 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 9842 if (event.xfocus.detail != NotifyPointer)
0f941935 9843 dpyinfo->x_focus_event_frame = f;
7a13e894 9844 if (f)
eb72635f
GM
9845 {
9846 x_new_focus_frame (dpyinfo, f);
9847
9848 /* Don't stop displaying the initial startup message
9849 for a switch-frame event we don't need. */
9850 if (GC_NILP (Vterminal_frame)
9851 && GC_CONSP (Vframe_list)
9852 && !GC_NILP (XCDR (Vframe_list)))
9853 {
9854 bufp->kind = FOCUS_IN_EVENT;
9855 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9856 bufp->arg = Qnil;
eb72635f
GM
9857 ++bufp, ++count, --numchars;
9858 }
9859 }
f9e24cb9 9860
6c183ba5
RS
9861#ifdef HAVE_X_I18N
9862 if (f && FRAME_XIC (f))
9863 XSetICFocus (FRAME_XIC (f));
9864#endif
9865
7a13e894 9866 goto OTHER;
10c5e63d 9867
7a13e894 9868 case LeaveNotify:
19126e11 9869 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
7a13e894 9870 if (f)
10c5e63d 9871 {
06a2c219
GM
9872 Lisp_Object frame;
9873 int from_menu_bar_p = 0;
9874
7a13e894 9875 if (f == dpyinfo->mouse_face_mouse_frame)
06a2c219
GM
9876 {
9877 /* If we move outside the frame, then we're
9878 certainly no longer on any text in the frame. */
9879 clear_mouse_face (dpyinfo);
9880 dpyinfo->mouse_face_mouse_frame = 0;
9881 }
9882
9883 /* Generate a nil HELP_EVENT to cancel a help-echo.
9884 Do it only if there's something to cancel.
9885 Otherwise, the startup message is cleared when
9886 the mouse leaves the frame. */
9887 if (any_help_event_p)
9888 {
be010514
GM
9889 Lisp_Object frame;
9890 int n;
9891
06a2c219 9892 XSETFRAME (frame, f);
7cea38bc 9893 n = gen_help_event (bufp, Qnil, frame, Qnil, Qnil, 0);
be010514 9894 bufp += n, count += n, numchars -= n;
06a2c219 9895 }
7a13e894 9896
06a2c219
GM
9897#ifdef LESSTIF_VERSION
9898 /* Please see the comment at the start of the
9899 EnterNotify case. */
9900 if (!event.xcrossing.focus
9901 && f->output_data.x->menubar_widget)
9902 {
9903 Window focus;
9904 int revert;
9905 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9906 if (focus == XtWindow (f->output_data.x->menubar_widget))
9907 from_menu_bar_p = 1;
9908 }
9909#endif /* LESSTIF_VERSION */
9910
9911 if (event.xcrossing.focus || from_menu_bar_p)
0f941935 9912 x_mouse_leave (dpyinfo);
10c5e63d 9913 else
7a13e894 9914 {
0f941935
KH
9915 if (f == dpyinfo->x_focus_event_frame)
9916 dpyinfo->x_focus_event_frame = 0;
9917 if (f == dpyinfo->x_focus_frame)
9918 x_new_focus_frame (dpyinfo, 0);
7a13e894 9919 }
10c5e63d 9920 }
7a13e894 9921 goto OTHER;
dc6f92b8 9922
7a13e894 9923 case FocusOut:
19126e11 9924 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 9925 if (event.xfocus.detail != NotifyPointer
0f941935
KH
9926 && f == dpyinfo->x_focus_event_frame)
9927 dpyinfo->x_focus_event_frame = 0;
9928 if (f && f == dpyinfo->x_focus_frame)
9929 x_new_focus_frame (dpyinfo, 0);
f9e24cb9 9930
6c183ba5
RS
9931#ifdef HAVE_X_I18N
9932 if (f && FRAME_XIC (f))
9933 XUnsetICFocus (FRAME_XIC (f));
9934#endif
9935
7a13e894 9936 goto OTHER;
dc6f92b8 9937
7a13e894 9938 case MotionNotify:
dc6f92b8 9939 {
06a2c219 9940 previous_help_echo = help_echo;
7cea38bc 9941 help_echo = help_echo_object = help_echo_window = Qnil;
be010514 9942 help_echo_pos = -1;
06a2c219 9943
7a13e894
RS
9944 if (dpyinfo->grabbed && last_mouse_frame
9945 && FRAME_LIVE_P (last_mouse_frame))
9946 f = last_mouse_frame;
9947 else
19126e11 9948 f = x_window_to_frame (dpyinfo, event.xmotion.window);
06a2c219 9949
7a13e894
RS
9950 if (f)
9951 note_mouse_movement (f, &event.xmotion);
9952 else
9953 {
e88b3c50 9954#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
9955 struct scroll_bar *bar
9956 = x_window_to_scroll_bar (event.xmotion.window);
f451eb13 9957
7a13e894
RS
9958 if (bar)
9959 x_scroll_bar_note_movement (bar, &event);
e88b3c50 9960#endif /* USE_TOOLKIT_SCROLL_BARS */
b8009dd1 9961
06a2c219
GM
9962 /* If we move outside the frame, then we're
9963 certainly no longer on any text in the frame. */
7a13e894
RS
9964 clear_mouse_face (dpyinfo);
9965 }
06a2c219
GM
9966
9967 /* If the contents of the global variable help_echo
9968 has changed, generate a HELP_EVENT. */
b7e80413
SM
9969 if (!NILP (help_echo)
9970 || !NILP (previous_help_echo))
06a2c219
GM
9971 {
9972 Lisp_Object frame;
be010514 9973 int n;
06a2c219
GM
9974
9975 if (f)
9976 XSETFRAME (frame, f);
9977 else
9978 frame = Qnil;
9979
9980 any_help_event_p = 1;
be010514 9981 n = gen_help_event (bufp, help_echo, frame,
7cea38bc
GM
9982 help_echo_window, help_echo_object,
9983 help_echo_pos);
be010514 9984 bufp += n, count += n, numchars -= n;
06a2c219
GM
9985 }
9986
9987 goto OTHER;
dc6f92b8 9988 }
dc6f92b8 9989
7a13e894 9990 case ConfigureNotify:
9829ddba
RS
9991 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
9992 if (f)
af395ec1 9993 {
5c187dee 9994#ifndef USE_X_TOOLKIT
bf1b7b30
KH
9995 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
9996 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
5c187dee 9997
2d7fc7e8
RS
9998 /* In the toolkit version, change_frame_size
9999 is called by the code that handles resizing
10000 of the EmacsFrame widget. */
7a13e894 10001
7a13e894
RS
10002 /* Even if the number of character rows and columns has
10003 not changed, the font size may have changed, so we need
10004 to check the pixel dimensions as well. */
10005 if (columns != f->width
10006 || rows != f->height
7556890b
RS
10007 || event.xconfigure.width != f->output_data.x->pixel_width
10008 || event.xconfigure.height != f->output_data.x->pixel_height)
7a13e894 10009 {
7d1e984f 10010 change_frame_size (f, rows, columns, 0, 1, 0);
7a13e894 10011 SET_FRAME_GARBAGED (f);
e687d06e 10012 cancel_mouse_face (f);
7a13e894 10013 }
2d7fc7e8 10014#endif
af395ec1 10015
7556890b
RS
10016 f->output_data.x->pixel_width = event.xconfigure.width;
10017 f->output_data.x->pixel_height = event.xconfigure.height;
7a13e894
RS
10018
10019 /* What we have now is the position of Emacs's own window.
10020 Convert that to the position of the window manager window. */
dcb07ae9
RS
10021 x_real_positions (f, &f->output_data.x->left_pos,
10022 &f->output_data.x->top_pos);
10023
f5d11644
GM
10024#ifdef HAVE_X_I18N
10025 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10026 xic_set_statusarea (f);
10027#endif
10028
dcb07ae9
RS
10029 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10030 {
10031 /* Since the WM decorations come below top_pos now,
10032 we must put them below top_pos in the future. */
10033 f->output_data.x->win_gravity = NorthWestGravity;
10034 x_wm_set_size_hint (f, (long) 0, 0);
10035 }
8f08dc93
KH
10036#ifdef USE_MOTIF
10037 /* Some window managers pass (0,0) as the location of
10038 the window, and the Motif event handler stores it
10039 in the emacs widget, which messes up Motif menus. */
10040 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
10041 {
10042 event.xconfigure.x = f->output_data.x->widget->core.x;
10043 event.xconfigure.y = f->output_data.x->widget->core.y;
10044 }
06a2c219 10045#endif /* USE_MOTIF */
7a13e894 10046 }
2d7fc7e8 10047 goto OTHER;
dc6f92b8 10048
7a13e894
RS
10049 case ButtonPress:
10050 case ButtonRelease:
10051 {
10052 /* If we decide we want to generate an event to be seen
10053 by the rest of Emacs, we put it here. */
10054 struct input_event emacs_event;
9ea173e8 10055 int tool_bar_p = 0;
06a2c219 10056
7a13e894 10057 emacs_event.kind = no_event;
7a13e894 10058 bzero (&compose_status, sizeof (compose_status));
9b07615b 10059
06a2c219
GM
10060 if (dpyinfo->grabbed
10061 && last_mouse_frame
9f67f20b
RS
10062 && FRAME_LIVE_P (last_mouse_frame))
10063 f = last_mouse_frame;
10064 else
2224b905 10065 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9f67f20b 10066
06a2c219
GM
10067 if (f)
10068 {
9ea173e8
GM
10069 /* Is this in the tool-bar? */
10070 if (WINDOWP (f->tool_bar_window)
10071 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
06a2c219
GM
10072 {
10073 Lisp_Object window;
10074 int p, x, y;
10075
10076 x = event.xbutton.x;
10077 y = event.xbutton.y;
10078
10079 /* Set x and y. */
10080 window = window_from_coordinates (f, x, y, &p, 1);
9ea173e8 10081 if (EQ (window, f->tool_bar_window))
06a2c219 10082 {
9ea173e8
GM
10083 x_handle_tool_bar_click (f, &event.xbutton);
10084 tool_bar_p = 1;
06a2c219
GM
10085 }
10086 }
10087
9ea173e8 10088 if (!tool_bar_p)
06a2c219
GM
10089 if (!dpyinfo->x_focus_frame
10090 || f == dpyinfo->x_focus_frame)
10091 construct_mouse_click (&emacs_event, &event, f);
7a13e894
RS
10092 }
10093 else
10094 {
06a2c219 10095#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
10096 struct scroll_bar *bar
10097 = x_window_to_scroll_bar (event.xbutton.window);
f451eb13 10098
7a13e894
RS
10099 if (bar)
10100 x_scroll_bar_handle_click (bar, &event, &emacs_event);
06a2c219 10101#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
10102 }
10103
10104 if (event.type == ButtonPress)
10105 {
10106 dpyinfo->grabbed |= (1 << event.xbutton.button);
10107 last_mouse_frame = f;
edad46f6
KH
10108 /* Ignore any mouse motion that happened
10109 before this event; any subsequent mouse-movement
10110 Emacs events should reflect only motion after
10111 the ButtonPress. */
a00e91cd
KH
10112 if (f != 0)
10113 f->mouse_moved = 0;
06a2c219 10114
9ea173e8
GM
10115 if (!tool_bar_p)
10116 last_tool_bar_item = -1;
7a13e894 10117 }
3afe33e7
RS
10118 else
10119 {
7a13e894 10120 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
3afe33e7 10121 }
23faf38f 10122
7a13e894
RS
10123 if (numchars >= 1 && emacs_event.kind != no_event)
10124 {
10125 bcopy (&emacs_event, bufp, sizeof (struct input_event));
10126 bufp++;
10127 count++;
10128 numchars--;
10129 }
3afe33e7
RS
10130
10131#ifdef USE_X_TOOLKIT
2224b905
RS
10132 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
10133 /* For a down-event in the menu bar,
10134 don't pass it to Xt right now.
10135 Instead, save it away
10136 and we will pass it to Xt from kbd_buffer_get_event.
10137 That way, we can run some Lisp code first. */
91375f8f
RS
10138 if (f && event.type == ButtonPress
10139 /* Verify the event is really within the menu bar
10140 and not just sent to it due to grabbing. */
10141 && event.xbutton.x >= 0
10142 && event.xbutton.x < f->output_data.x->pixel_width
10143 && event.xbutton.y >= 0
10144 && event.xbutton.y < f->output_data.x->menubar_height
10145 && event.xbutton.same_screen)
2224b905 10146 {
8805890a 10147 SET_SAVED_BUTTON_EVENT;
2237cac9
RS
10148 XSETFRAME (last_mouse_press_frame, f);
10149 }
10150 else if (event.type == ButtonPress)
10151 {
10152 last_mouse_press_frame = Qnil;
30e671c3 10153 goto OTHER;
ce89ef46 10154 }
06a2c219 10155
2237cac9
RS
10156#ifdef USE_MOTIF /* This should do not harm for Lucid,
10157 but I am trying to be cautious. */
ce89ef46
RS
10158 else if (event.type == ButtonRelease)
10159 {
2237cac9 10160 if (!NILP (last_mouse_press_frame))
f10ded1c 10161 {
2237cac9
RS
10162 f = XFRAME (last_mouse_press_frame);
10163 if (f->output_data.x)
06a2c219 10164 SET_SAVED_BUTTON_EVENT;
f10ded1c 10165 }
06a2c219 10166 else
30e671c3 10167 goto OTHER;
2224b905 10168 }
2237cac9 10169#endif /* USE_MOTIF */
2224b905
RS
10170 else
10171 goto OTHER;
3afe33e7 10172#endif /* USE_X_TOOLKIT */
7a13e894
RS
10173 }
10174 break;
dc6f92b8 10175
7a13e894 10176 case CirculateNotify:
06a2c219
GM
10177 goto OTHER;
10178
7a13e894 10179 case CirculateRequest:
06a2c219
GM
10180 goto OTHER;
10181
10182 case VisibilityNotify:
10183 goto OTHER;
dc6f92b8 10184
7a13e894
RS
10185 case MappingNotify:
10186 /* Someone has changed the keyboard mapping - update the
10187 local cache. */
10188 switch (event.xmapping.request)
10189 {
10190 case MappingModifier:
10191 x_find_modifier_meanings (dpyinfo);
10192 /* This is meant to fall through. */
10193 case MappingKeyboard:
10194 XRefreshKeyboardMapping (&event.xmapping);
10195 }
7a13e894 10196 goto OTHER;
dc6f92b8 10197
7a13e894 10198 default:
7a13e894 10199 OTHER:
717ca130 10200#ifdef USE_X_TOOLKIT
7a13e894
RS
10201 BLOCK_INPUT;
10202 XtDispatchEvent (&event);
10203 UNBLOCK_INPUT;
3afe33e7 10204#endif /* USE_X_TOOLKIT */
7a13e894
RS
10205 break;
10206 }
dc6f92b8
JB
10207 }
10208 }
10209
06a2c219
GM
10210 out:;
10211
9a5196d0
RS
10212 /* On some systems, an X bug causes Emacs to get no more events
10213 when the window is destroyed. Detect that. (1994.) */
58769bee 10214 if (! event_found)
ef2a22d0 10215 {
ef2a22d0
RS
10216 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
10217 One XNOOP in 100 loops will make Emacs terminate.
10218 B. Bretthauer, 1994 */
10219 x_noop_count++;
58769bee 10220 if (x_noop_count >= 100)
ef2a22d0
RS
10221 {
10222 x_noop_count=0;
2224b905
RS
10223
10224 if (next_noop_dpyinfo == 0)
10225 next_noop_dpyinfo = x_display_list;
10226
10227 XNoOp (next_noop_dpyinfo->display);
10228
10229 /* Each time we get here, cycle through the displays now open. */
10230 next_noop_dpyinfo = next_noop_dpyinfo->next;
ef2a22d0
RS
10231 }
10232 }
502add23 10233
06a2c219 10234 /* If the focus was just given to an auto-raising frame,
0134a210 10235 raise it now. */
7a13e894 10236 /* ??? This ought to be able to handle more than one such frame. */
0134a210
RS
10237 if (pending_autoraise_frame)
10238 {
10239 x_raise_frame (pending_autoraise_frame);
10240 pending_autoraise_frame = 0;
10241 }
0134a210 10242
dc6f92b8 10243 UNBLOCK_INPUT;
bde5503b 10244 --handling_signal;
dc6f92b8
JB
10245 return count;
10246}
06a2c219
GM
10247
10248
10249
dc6f92b8 10250\f
06a2c219
GM
10251/***********************************************************************
10252 Text Cursor
10253 ***********************************************************************/
10254
10255/* Note if the text cursor of window W has been overwritten by a
10256 drawing operation that outputs N glyphs starting at HPOS in the
10257 line given by output_cursor.vpos. N < 0 means all the rest of the
10258 line after HPOS has been written. */
10259
10260static void
10261note_overwritten_text_cursor (w, hpos, n)
10262 struct window *w;
10263 int hpos, n;
10264{
10265 if (updated_area == TEXT_AREA
10266 && output_cursor.vpos == w->phys_cursor.vpos
10267 && hpos <= w->phys_cursor.hpos
10268 && (n < 0
10269 || hpos + n > w->phys_cursor.hpos))
10270 w->phys_cursor_on_p = 0;
10271}
f451eb13
JB
10272
10273
06a2c219
GM
10274/* Set clipping for output in glyph row ROW. W is the window in which
10275 we operate. GC is the graphics context to set clipping in.
10276 WHOLE_LINE_P non-zero means include the areas used for truncation
10277 mark display and alike in the clipping rectangle.
10278
10279 ROW may be a text row or, e.g., a mode line. Text rows must be
10280 clipped to the interior of the window dedicated to text display,
10281 mode lines must be clipped to the whole window. */
dc6f92b8
JB
10282
10283static void
06a2c219
GM
10284x_clip_to_row (w, row, gc, whole_line_p)
10285 struct window *w;
10286 struct glyph_row *row;
10287 GC gc;
10288 int whole_line_p;
dc6f92b8 10289{
06a2c219
GM
10290 struct frame *f = XFRAME (WINDOW_FRAME (w));
10291 XRectangle clip_rect;
10292 int window_x, window_y, window_width, window_height;
dc6f92b8 10293
06a2c219 10294 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5c1aae96 10295
06a2c219
GM
10296 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10297 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10298 clip_rect.y = max (clip_rect.y, window_y);
10299 clip_rect.width = window_width;
10300 clip_rect.height = row->visible_height;
5c1aae96 10301
06a2c219
GM
10302 /* If clipping to the whole line, including trunc marks, extend
10303 the rectangle to the left and increase its width. */
10304 if (whole_line_p)
10305 {
110859fc
GM
10306 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10307 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
06a2c219 10308 }
5c1aae96 10309
06a2c219 10310 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
dc6f92b8
JB
10311}
10312
06a2c219
GM
10313
10314/* Draw a hollow box cursor on window W in glyph row ROW. */
dc6f92b8
JB
10315
10316static void
06a2c219
GM
10317x_draw_hollow_cursor (w, row)
10318 struct window *w;
10319 struct glyph_row *row;
dc6f92b8 10320{
06a2c219
GM
10321 struct frame *f = XFRAME (WINDOW_FRAME (w));
10322 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10323 Display *dpy = FRAME_X_DISPLAY (f);
10324 int x, y, wd, h;
10325 XGCValues xgcv;
10326 struct glyph *cursor_glyph;
10327 GC gc;
10328
10329 /* Compute frame-relative coordinates from window-relative
10330 coordinates. */
10331 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10332 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10333 + row->ascent - w->phys_cursor_ascent);
10334 h = row->height - 1;
10335
10336 /* Get the glyph the cursor is on. If we can't tell because
10337 the current matrix is invalid or such, give up. */
10338 cursor_glyph = get_phys_cursor_glyph (w);
10339 if (cursor_glyph == NULL)
dc6f92b8
JB
10340 return;
10341
06a2c219
GM
10342 /* Compute the width of the rectangle to draw. If on a stretch
10343 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10344 rectangle as wide as the glyph, but use a canonical character
10345 width instead. */
10346 wd = cursor_glyph->pixel_width - 1;
10347 if (cursor_glyph->type == STRETCH_GLYPH
10348 && !x_stretch_cursor_p)
10349 wd = min (CANON_X_UNIT (f), wd);
10350
10351 /* The foreground of cursor_gc is typically the same as the normal
10352 background color, which can cause the cursor box to be invisible. */
10353 xgcv.foreground = f->output_data.x->cursor_pixel;
10354 if (dpyinfo->scratch_cursor_gc)
10355 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10356 else
10357 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10358 GCForeground, &xgcv);
10359 gc = dpyinfo->scratch_cursor_gc;
10360
10361 /* Set clipping, draw the rectangle, and reset clipping again. */
10362 x_clip_to_row (w, row, gc, 0);
10363 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10364 XSetClipMask (dpy, gc, None);
dc6f92b8
JB
10365}
10366
06a2c219
GM
10367
10368/* Draw a bar cursor on window W in glyph row ROW.
10369
10370 Implementation note: One would like to draw a bar cursor with an
10371 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10372 Unfortunately, I didn't find a font yet that has this property set.
10373 --gerd. */
dc6f92b8
JB
10374
10375static void
f02d8aa0 10376x_draw_bar_cursor (w, row, width)
06a2c219
GM
10377 struct window *w;
10378 struct glyph_row *row;
f02d8aa0 10379 int width;
dc6f92b8 10380{
92f424df
GM
10381 struct frame *f = XFRAME (w->frame);
10382 struct glyph *cursor_glyph;
10383 GC gc;
10384 int x;
10385 unsigned long mask;
10386 XGCValues xgcv;
10387 Display *dpy;
10388 Window window;
06a2c219 10389
92f424df
GM
10390 /* If cursor is out of bounds, don't draw garbage. This can happen
10391 in mini-buffer windows when switching between echo area glyphs
10392 and mini-buffer. */
10393 cursor_glyph = get_phys_cursor_glyph (w);
10394 if (cursor_glyph == NULL)
10395 return;
06a2c219 10396
92f424df
GM
10397 /* If on an image, draw like a normal cursor. That's usually better
10398 visible than drawing a bar, esp. if the image is large so that
10399 the bar might not be in the window. */
10400 if (cursor_glyph->type == IMAGE_GLYPH)
10401 {
10402 struct glyph_row *row;
10403 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
10404 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
10405 }
10406 else
10407 {
06a2c219
GM
10408 xgcv.background = f->output_data.x->cursor_pixel;
10409 xgcv.foreground = f->output_data.x->cursor_pixel;
10410 xgcv.graphics_exposures = 0;
10411 mask = GCForeground | GCBackground | GCGraphicsExposures;
10412 dpy = FRAME_X_DISPLAY (f);
10413 window = FRAME_X_WINDOW (f);
10414 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
92f424df 10415
06a2c219
GM
10416 if (gc)
10417 XChangeGC (dpy, gc, mask, &xgcv);
10418 else
10419 {
10420 gc = XCreateGC (dpy, window, mask, &xgcv);
10421 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10422 }
92f424df 10423
f02d8aa0
GM
10424 if (width < 0)
10425 width = f->output_data.x->cursor_width;
92f424df 10426
06a2c219
GM
10427 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10428 x_clip_to_row (w, row, gc, 0);
10429 XFillRectangle (dpy, window, gc,
10430 x,
10431 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
f02d8aa0 10432 min (cursor_glyph->pixel_width, width),
06a2c219
GM
10433 row->height);
10434 XSetClipMask (dpy, gc, None);
10435 }
dc6f92b8
JB
10436}
10437
06a2c219
GM
10438
10439/* Clear the cursor of window W to background color, and mark the
10440 cursor as not shown. This is used when the text where the cursor
10441 is is about to be rewritten. */
10442
dc6f92b8 10443static void
06a2c219
GM
10444x_clear_cursor (w)
10445 struct window *w;
dc6f92b8 10446{
06a2c219
GM
10447 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10448 x_update_window_cursor (w, 0);
10449}
90e65f07 10450
dbc4e1c1 10451
06a2c219
GM
10452/* Draw the cursor glyph of window W in glyph row ROW. See the
10453 comment of x_draw_glyphs for the meaning of HL. */
dbc4e1c1 10454
06a2c219
GM
10455static void
10456x_draw_phys_cursor_glyph (w, row, hl)
10457 struct window *w;
10458 struct glyph_row *row;
10459 enum draw_glyphs_face hl;
10460{
10461 /* If cursor hpos is out of bounds, don't draw garbage. This can
10462 happen in mini-buffer windows when switching between echo area
10463 glyphs and mini-buffer. */
10464 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
66ac4b0e
GM
10465 {
10466 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10467 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10468 hl, 0, 0, 0);
10469
10470 /* When we erase the cursor, and ROW is overlapped by other
10471 rows, make sure that these overlapping parts of other rows
10472 are redrawn. */
10473 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10474 {
10475 if (row > w->current_matrix->rows
10476 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10477 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10478
10479 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10480 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10481 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10482 }
10483 }
06a2c219 10484}
dbc4e1c1 10485
eea6af04 10486
06a2c219 10487/* Erase the image of a cursor of window W from the screen. */
eea6af04 10488
06a2c219
GM
10489static void
10490x_erase_phys_cursor (w)
10491 struct window *w;
10492{
10493 struct frame *f = XFRAME (w->frame);
10494 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10495 int hpos = w->phys_cursor.hpos;
10496 int vpos = w->phys_cursor.vpos;
10497 int mouse_face_here_p = 0;
10498 struct glyph_matrix *active_glyphs = w->current_matrix;
10499 struct glyph_row *cursor_row;
10500 struct glyph *cursor_glyph;
10501 enum draw_glyphs_face hl;
10502
10503 /* No cursor displayed or row invalidated => nothing to do on the
10504 screen. */
10505 if (w->phys_cursor_type == NO_CURSOR)
10506 goto mark_cursor_off;
10507
10508 /* VPOS >= active_glyphs->nrows means that window has been resized.
10509 Don't bother to erase the cursor. */
10510 if (vpos >= active_glyphs->nrows)
10511 goto mark_cursor_off;
10512
10513 /* If row containing cursor is marked invalid, there is nothing we
10514 can do. */
10515 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10516 if (!cursor_row->enabled_p)
10517 goto mark_cursor_off;
10518
10519 /* This can happen when the new row is shorter than the old one.
10520 In this case, either x_draw_glyphs or clear_end_of_line
10521 should have cleared the cursor. Note that we wouldn't be
10522 able to erase the cursor in this case because we don't have a
10523 cursor glyph at hand. */
10524 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10525 goto mark_cursor_off;
10526
10527 /* If the cursor is in the mouse face area, redisplay that when
10528 we clear the cursor. */
8801a864
KR
10529 if (! NILP (dpyinfo->mouse_face_window)
10530 && w == XWINDOW (dpyinfo->mouse_face_window)
06a2c219
GM
10531 && (vpos > dpyinfo->mouse_face_beg_row
10532 || (vpos == dpyinfo->mouse_face_beg_row
10533 && hpos >= dpyinfo->mouse_face_beg_col))
10534 && (vpos < dpyinfo->mouse_face_end_row
10535 || (vpos == dpyinfo->mouse_face_end_row
10536 && hpos < dpyinfo->mouse_face_end_col))
10537 /* Don't redraw the cursor's spot in mouse face if it is at the
10538 end of a line (on a newline). The cursor appears there, but
10539 mouse highlighting does not. */
10540 && cursor_row->used[TEXT_AREA] > hpos)
10541 mouse_face_here_p = 1;
10542
10543 /* Maybe clear the display under the cursor. */
10544 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10545 {
10546 int x;
045dee35 10547 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dbc4e1c1 10548
06a2c219
GM
10549 cursor_glyph = get_phys_cursor_glyph (w);
10550 if (cursor_glyph == NULL)
10551 goto mark_cursor_off;
dbc4e1c1 10552
06a2c219
GM
10553 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10554
10555 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10556 x,
045dee35 10557 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219
GM
10558 cursor_row->y)),
10559 cursor_glyph->pixel_width,
10560 cursor_row->visible_height,
10561 False);
dbc4e1c1 10562 }
06a2c219
GM
10563
10564 /* Erase the cursor by redrawing the character underneath it. */
10565 if (mouse_face_here_p)
10566 hl = DRAW_MOUSE_FACE;
10567 else if (cursor_row->inverse_p)
10568 hl = DRAW_INVERSE_VIDEO;
10569 else
10570 hl = DRAW_NORMAL_TEXT;
10571 x_draw_phys_cursor_glyph (w, cursor_row, hl);
dbc4e1c1 10572
06a2c219
GM
10573 mark_cursor_off:
10574 w->phys_cursor_on_p = 0;
10575 w->phys_cursor_type = NO_CURSOR;
dbc4e1c1
JB
10576}
10577
10578
06a2c219
GM
10579/* Display or clear cursor of window W. If ON is zero, clear the
10580 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10581 where to put the cursor is specified by HPOS, VPOS, X and Y. */
dbc4e1c1 10582
06a2c219
GM
10583void
10584x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10585 struct window *w;
10586 int on, hpos, vpos, x, y;
dbc4e1c1 10587{
06a2c219
GM
10588 struct frame *f = XFRAME (w->frame);
10589 int new_cursor_type;
f02d8aa0 10590 int new_cursor_width;
06a2c219
GM
10591 struct glyph_matrix *current_glyphs;
10592 struct glyph_row *glyph_row;
10593 struct glyph *glyph;
dbc4e1c1 10594
49d838ea 10595 /* This is pointless on invisible frames, and dangerous on garbaged
06a2c219
GM
10596 windows and frames; in the latter case, the frame or window may
10597 be in the midst of changing its size, and x and y may be off the
10598 window. */
10599 if (! FRAME_VISIBLE_P (f)
10600 || FRAME_GARBAGED_P (f)
10601 || vpos >= w->current_matrix->nrows
10602 || hpos >= w->current_matrix->matrix_w)
dc6f92b8
JB
10603 return;
10604
10605 /* If cursor is off and we want it off, return quickly. */
06a2c219 10606 if (!on && !w->phys_cursor_on_p)
dc6f92b8
JB
10607 return;
10608
06a2c219
GM
10609 current_glyphs = w->current_matrix;
10610 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10611 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10612
10613 /* If cursor row is not enabled, we don't really know where to
10614 display the cursor. */
10615 if (!glyph_row->enabled_p)
10616 {
10617 w->phys_cursor_on_p = 0;
10618 return;
10619 }
10620
10621 xassert (interrupt_input_blocked);
10622
10623 /* Set new_cursor_type to the cursor we want to be displayed. In a
10624 mini-buffer window, we want the cursor only to appear if we are
10625 reading input from this window. For the selected window, we want
10626 the cursor type given by the frame parameter. If explicitly
10627 marked off, draw no cursor. In all other cases, we want a hollow
10628 box cursor. */
f02d8aa0 10629 new_cursor_width = -1;
9b4a7047
GM
10630 if (cursor_in_echo_area
10631 && FRAME_HAS_MINIBUF_P (f)
10632 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
06a2c219 10633 {
9b4a7047
GM
10634 if (w == XWINDOW (echo_area_window))
10635 new_cursor_type = FRAME_DESIRED_CURSOR (f);
06a2c219
GM
10636 else
10637 new_cursor_type = HOLLOW_BOX_CURSOR;
10638 }
06a2c219 10639 else
9b4a7047
GM
10640 {
10641 if (w != XWINDOW (selected_window)
10642 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
10643 {
e55a0b79
GM
10644 extern int cursor_in_non_selected_windows;
10645
10646 if (MINI_WINDOW_P (w) || !cursor_in_non_selected_windows)
9b4a7047
GM
10647 new_cursor_type = NO_CURSOR;
10648 else
10649 new_cursor_type = HOLLOW_BOX_CURSOR;
10650 }
10651 else if (w->cursor_off_p)
10652 new_cursor_type = NO_CURSOR;
10653 else
f02d8aa0
GM
10654 {
10655 struct buffer *b = XBUFFER (w->buffer);
10656
10657 if (EQ (b->cursor_type, Qt))
10658 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10659 else
10660 new_cursor_type = x_specified_cursor_type (b->cursor_type,
10661 &new_cursor_width);
10662 }
9b4a7047 10663 }
06a2c219
GM
10664
10665 /* If cursor is currently being shown and we don't want it to be or
10666 it is in the wrong place, or the cursor type is not what we want,
dc6f92b8 10667 erase it. */
06a2c219 10668 if (w->phys_cursor_on_p
dc6f92b8 10669 && (!on
06a2c219
GM
10670 || w->phys_cursor.x != x
10671 || w->phys_cursor.y != y
10672 || new_cursor_type != w->phys_cursor_type))
10673 x_erase_phys_cursor (w);
10674
10675 /* If the cursor is now invisible and we want it to be visible,
10676 display it. */
10677 if (on && !w->phys_cursor_on_p)
10678 {
10679 w->phys_cursor_ascent = glyph_row->ascent;
10680 w->phys_cursor_height = glyph_row->height;
10681
10682 /* Set phys_cursor_.* before x_draw_.* is called because some
10683 of them may need the information. */
10684 w->phys_cursor.x = x;
10685 w->phys_cursor.y = glyph_row->y;
10686 w->phys_cursor.hpos = hpos;
10687 w->phys_cursor.vpos = vpos;
10688 w->phys_cursor_type = new_cursor_type;
10689 w->phys_cursor_on_p = 1;
10690
10691 switch (new_cursor_type)
dc6f92b8 10692 {
06a2c219
GM
10693 case HOLLOW_BOX_CURSOR:
10694 x_draw_hollow_cursor (w, glyph_row);
10695 break;
10696
10697 case FILLED_BOX_CURSOR:
10698 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10699 break;
10700
10701 case BAR_CURSOR:
f02d8aa0 10702 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
06a2c219
GM
10703 break;
10704
10705 case NO_CURSOR:
10706 break;
dc6f92b8 10707
06a2c219
GM
10708 default:
10709 abort ();
10710 }
59ddecde
GM
10711
10712#ifdef HAVE_X_I18N
10713 if (w == XWINDOW (f->selected_window))
10714 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
10715 xic_set_preeditarea (w, x, y);
10716#endif
dc6f92b8
JB
10717 }
10718
06a2c219 10719#ifndef XFlush
f676886a 10720 if (updating_frame != f)
334208b7 10721 XFlush (FRAME_X_DISPLAY (f));
06a2c219 10722#endif
dc6f92b8
JB
10723}
10724
06a2c219
GM
10725
10726/* Display the cursor on window W, or clear it. X and Y are window
10727 relative pixel coordinates. HPOS and VPOS are glyph matrix
10728 positions. If W is not the selected window, display a hollow
10729 cursor. ON non-zero means display the cursor at X, Y which
10730 correspond to HPOS, VPOS, otherwise it is cleared. */
5d46f928 10731
dfcf069d 10732void
06a2c219
GM
10733x_display_cursor (w, on, hpos, vpos, x, y)
10734 struct window *w;
10735 int on, hpos, vpos, x, y;
dc6f92b8 10736{
f94397b5 10737 BLOCK_INPUT;
06a2c219 10738 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
5d46f928
RS
10739 UNBLOCK_INPUT;
10740}
10741
06a2c219
GM
10742
10743/* Display the cursor on window W, or clear it, according to ON_P.
5d46f928
RS
10744 Don't change the cursor's position. */
10745
dfcf069d 10746void
06a2c219 10747x_update_cursor (f, on_p)
5d46f928 10748 struct frame *f;
5d46f928 10749{
06a2c219
GM
10750 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
10751}
10752
10753
10754/* Call x_update_window_cursor with parameter ON_P on all leaf windows
10755 in the window tree rooted at W. */
10756
10757static void
10758x_update_cursor_in_window_tree (w, on_p)
10759 struct window *w;
10760 int on_p;
10761{
10762 while (w)
10763 {
10764 if (!NILP (w->hchild))
10765 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10766 else if (!NILP (w->vchild))
10767 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10768 else
10769 x_update_window_cursor (w, on_p);
10770
10771 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10772 }
10773}
5d46f928 10774
f94397b5 10775
06a2c219
GM
10776/* Switch the display of W's cursor on or off, according to the value
10777 of ON. */
10778
10779static void
10780x_update_window_cursor (w, on)
10781 struct window *w;
10782 int on;
10783{
16b5d424
GM
10784 /* Don't update cursor in windows whose frame is in the process
10785 of being deleted. */
10786 if (w->current_matrix)
10787 {
10788 BLOCK_INPUT;
10789 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
10790 w->phys_cursor.x, w->phys_cursor.y);
10791 UNBLOCK_INPUT;
10792 }
dc6f92b8 10793}
06a2c219
GM
10794
10795
10796
dc6f92b8
JB
10797\f
10798/* Icons. */
10799
f676886a 10800/* Refresh bitmap kitchen sink icon for frame F
06a2c219 10801 when we get an expose event for it. */
dc6f92b8 10802
dfcf069d 10803void
f676886a
JB
10804refreshicon (f)
10805 struct frame *f;
dc6f92b8 10806{
06a2c219 10807 /* Normally, the window manager handles this function. */
dc6f92b8
JB
10808}
10809
dbc4e1c1 10810/* Make the x-window of frame F use the gnu icon bitmap. */
dc6f92b8
JB
10811
10812int
990ba854 10813x_bitmap_icon (f, file)
f676886a 10814 struct frame *f;
990ba854 10815 Lisp_Object file;
dc6f92b8 10816{
06a2c219 10817 int bitmap_id;
dc6f92b8 10818
c118dd06 10819 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
10820 return 1;
10821
990ba854 10822 /* Free up our existing icon bitmap if any. */
7556890b
RS
10823 if (f->output_data.x->icon_bitmap > 0)
10824 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10825 f->output_data.x->icon_bitmap = 0;
990ba854
RS
10826
10827 if (STRINGP (file))
7f2ae036
RS
10828 bitmap_id = x_create_bitmap_from_file (f, file);
10829 else
10830 {
990ba854 10831 /* Create the GNU bitmap if necessary. */
5bf01b68 10832 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
334208b7
RS
10833 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
10834 = x_create_bitmap_from_data (f, gnu_bits,
10835 gnu_width, gnu_height);
990ba854
RS
10836
10837 /* The first time we create the GNU bitmap,
06a2c219 10838 this increments the ref-count one extra time.
990ba854
RS
10839 As a result, the GNU bitmap is never freed.
10840 That way, we don't have to worry about allocating it again. */
334208b7 10841 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
990ba854 10842
334208b7 10843 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7f2ae036
RS
10844 }
10845
10846 x_wm_set_icon_pixmap (f, bitmap_id);
7556890b 10847 f->output_data.x->icon_bitmap = bitmap_id;
dc6f92b8
JB
10848
10849 return 0;
10850}
10851
10852
1be2d067
KH
10853/* Make the x-window of frame F use a rectangle with text.
10854 Use ICON_NAME as the text. */
dc6f92b8
JB
10855
10856int
f676886a
JB
10857x_text_icon (f, icon_name)
10858 struct frame *f;
dc6f92b8
JB
10859 char *icon_name;
10860{
c118dd06 10861 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
10862 return 1;
10863
1be2d067
KH
10864#ifdef HAVE_X11R4
10865 {
10866 XTextProperty text;
10867 text.value = (unsigned char *) icon_name;
10868 text.encoding = XA_STRING;
10869 text.format = 8;
10870 text.nitems = strlen (icon_name);
10871#ifdef USE_X_TOOLKIT
7556890b 10872 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
1be2d067
KH
10873 &text);
10874#else /* not USE_X_TOOLKIT */
10875 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
10876#endif /* not USE_X_TOOLKIT */
10877 }
10878#else /* not HAVE_X11R4 */
10879 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
10880#endif /* not HAVE_X11R4 */
58769bee 10881
7556890b
RS
10882 if (f->output_data.x->icon_bitmap > 0)
10883 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10884 f->output_data.x->icon_bitmap = 0;
b1c884c3 10885 x_wm_set_icon_pixmap (f, 0);
dc6f92b8
JB
10886
10887 return 0;
10888}
10889\f
e99db5a1
RS
10890#define X_ERROR_MESSAGE_SIZE 200
10891
10892/* If non-nil, this should be a string.
10893 It means catch X errors and store the error message in this string. */
10894
10895static Lisp_Object x_error_message_string;
10896
10897/* An X error handler which stores the error message in
10898 x_error_message_string. This is called from x_error_handler if
10899 x_catch_errors is in effect. */
10900
06a2c219 10901static void
e99db5a1
RS
10902x_error_catcher (display, error)
10903 Display *display;
10904 XErrorEvent *error;
10905{
10906 XGetErrorText (display, error->error_code,
10907 XSTRING (x_error_message_string)->data,
10908 X_ERROR_MESSAGE_SIZE);
10909}
10910
10911/* Begin trapping X errors for display DPY. Actually we trap X errors
10912 for all displays, but DPY should be the display you are actually
10913 operating on.
10914
10915 After calling this function, X protocol errors no longer cause
10916 Emacs to exit; instead, they are recorded in the string
10917 stored in x_error_message_string.
10918
10919 Calling x_check_errors signals an Emacs error if an X error has
10920 occurred since the last call to x_catch_errors or x_check_errors.
10921
10922 Calling x_uncatch_errors resumes the normal error handling. */
10923
10924void x_check_errors ();
10925static Lisp_Object x_catch_errors_unwind ();
10926
10927int
10928x_catch_errors (dpy)
10929 Display *dpy;
10930{
10931 int count = specpdl_ptr - specpdl;
10932
10933 /* Make sure any errors from previous requests have been dealt with. */
10934 XSync (dpy, False);
10935
10936 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
10937
10938 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
10939 XSTRING (x_error_message_string)->data[0] = 0;
10940
10941 return count;
10942}
10943
10944/* Unbind the binding that we made to check for X errors. */
10945
10946static Lisp_Object
10947x_catch_errors_unwind (old_val)
10948 Lisp_Object old_val;
10949{
10950 x_error_message_string = old_val;
10951 return Qnil;
10952}
10953
10954/* If any X protocol errors have arrived since the last call to
10955 x_catch_errors or x_check_errors, signal an Emacs error using
10956 sprintf (a buffer, FORMAT, the x error message text) as the text. */
10957
10958void
10959x_check_errors (dpy, format)
10960 Display *dpy;
10961 char *format;
10962{
10963 /* Make sure to catch any errors incurred so far. */
10964 XSync (dpy, False);
10965
10966 if (XSTRING (x_error_message_string)->data[0])
10967 error (format, XSTRING (x_error_message_string)->data);
10968}
10969
9829ddba
RS
10970/* Nonzero if we had any X protocol errors
10971 since we did x_catch_errors on DPY. */
e99db5a1
RS
10972
10973int
10974x_had_errors_p (dpy)
10975 Display *dpy;
10976{
10977 /* Make sure to catch any errors incurred so far. */
10978 XSync (dpy, False);
10979
10980 return XSTRING (x_error_message_string)->data[0] != 0;
10981}
10982
9829ddba
RS
10983/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
10984
06a2c219 10985void
9829ddba
RS
10986x_clear_errors (dpy)
10987 Display *dpy;
10988{
10989 XSTRING (x_error_message_string)->data[0] = 0;
10990}
10991
e99db5a1
RS
10992/* Stop catching X protocol errors and let them make Emacs die.
10993 DPY should be the display that was passed to x_catch_errors.
10994 COUNT should be the value that was returned by
10995 the corresponding call to x_catch_errors. */
10996
10997void
10998x_uncatch_errors (dpy, count)
10999 Display *dpy;
11000 int count;
11001{
11002 unbind_to (count, Qnil);
11003}
11004
11005#if 0
11006static unsigned int x_wire_count;
11007x_trace_wire ()
11008{
11009 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
11010}
11011#endif /* ! 0 */
11012
11013\f
11014/* Handle SIGPIPE, which can happen when the connection to a server
11015 simply goes away. SIGPIPE is handled by x_connection_signal.
11016 Don't need to do anything, because the write which caused the
11017 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
06a2c219 11018 which will do the appropriate cleanup for us. */
e99db5a1
RS
11019
11020static SIGTYPE
11021x_connection_signal (signalnum) /* If we don't have an argument, */
06a2c219 11022 int signalnum; /* some compilers complain in signal calls. */
e99db5a1
RS
11023{
11024#ifdef USG
11025 /* USG systems forget handlers when they are used;
11026 must reestablish each time */
11027 signal (signalnum, x_connection_signal);
11028#endif /* USG */
11029}
11030\f
4746118a
JB
11031/* Handling X errors. */
11032
7a13e894 11033/* Handle the loss of connection to display DISPLAY. */
16bd92ea 11034
4746118a 11035static SIGTYPE
7a13e894
RS
11036x_connection_closed (display, error_message)
11037 Display *display;
11038 char *error_message;
4746118a 11039{
7a13e894
RS
11040 struct x_display_info *dpyinfo = x_display_info_for_display (display);
11041 Lisp_Object frame, tail;
11042
6186a4a0
RS
11043 /* Indicate that this display is dead. */
11044
2e465cdd 11045#if 0 /* Closing the display caused a bus error on OpenWindows. */
f613a4c8 11046#ifdef USE_X_TOOLKIT
adabc3a9 11047 XtCloseDisplay (display);
2e465cdd 11048#endif
f613a4c8 11049#endif
adabc3a9 11050
9e80b57d
KR
11051 if (dpyinfo)
11052 dpyinfo->display = 0;
6186a4a0 11053
06a2c219 11054 /* First delete frames whose mini-buffers are on frames
7a13e894
RS
11055 that are on the dead display. */
11056 FOR_EACH_FRAME (tail, frame)
11057 {
11058 Lisp_Object minibuf_frame;
11059 minibuf_frame
11060 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
f48f33ca
RS
11061 if (FRAME_X_P (XFRAME (frame))
11062 && FRAME_X_P (XFRAME (minibuf_frame))
11063 && ! EQ (frame, minibuf_frame)
11064 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7a13e894
RS
11065 Fdelete_frame (frame, Qt);
11066 }
11067
11068 /* Now delete all remaining frames on the dead display.
06a2c219 11069 We are now sure none of these is used as the mini-buffer
7a13e894
RS
11070 for another frame that we need to delete. */
11071 FOR_EACH_FRAME (tail, frame)
f48f33ca
RS
11072 if (FRAME_X_P (XFRAME (frame))
11073 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
07a7096a
KH
11074 {
11075 /* Set this to t so that Fdelete_frame won't get confused
11076 trying to find a replacement. */
11077 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
11078 Fdelete_frame (frame, Qt);
11079 }
7a13e894 11080
482a1bd2
KH
11081 if (dpyinfo)
11082 x_delete_display (dpyinfo);
7a13e894
RS
11083
11084 if (x_display_list == 0)
11085 {
f8d07b62 11086 fprintf (stderr, "%s\n", error_message);
7a13e894
RS
11087 shut_down_emacs (0, 0, Qnil);
11088 exit (70);
11089 }
12ba150f 11090
7a13e894
RS
11091 /* Ordinary stack unwind doesn't deal with these. */
11092#ifdef SIGIO
11093 sigunblock (sigmask (SIGIO));
11094#endif
11095 sigunblock (sigmask (SIGALRM));
11096 TOTALLY_UNBLOCK_INPUT;
11097
aa4d9a9e 11098 clear_waiting_for_input ();
7a13e894 11099 error ("%s", error_message);
4746118a
JB
11100}
11101
7a13e894
RS
11102/* This is the usual handler for X protocol errors.
11103 It kills all frames on the display that we got the error for.
11104 If that was the only one, it prints an error message and kills Emacs. */
11105
06a2c219 11106static void
c118dd06
JB
11107x_error_quitter (display, error)
11108 Display *display;
11109 XErrorEvent *error;
11110{
7a13e894 11111 char buf[256], buf1[356];
dc6f92b8 11112
58769bee 11113 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 11114 original error handler. */
dc6f92b8 11115
c118dd06 11116 XGetErrorText (display, error->error_code, buf, sizeof (buf));
0a0fdc70 11117 sprintf (buf1, "X protocol error: %s on protocol request %d",
c118dd06 11118 buf, error->request_code);
7a13e894 11119 x_connection_closed (display, buf1);
dc6f92b8
JB
11120}
11121
e99db5a1
RS
11122/* This is the first-level handler for X protocol errors.
11123 It calls x_error_quitter or x_error_catcher. */
7a13e894 11124
8922af5f 11125static int
e99db5a1 11126x_error_handler (display, error)
8922af5f 11127 Display *display;
e99db5a1 11128 XErrorEvent *error;
8922af5f 11129{
e99db5a1
RS
11130 if (! NILP (x_error_message_string))
11131 x_error_catcher (display, error);
11132 else
11133 x_error_quitter (display, error);
06a2c219 11134 return 0;
f9e24cb9 11135}
c118dd06 11136
e99db5a1
RS
11137/* This is the handler for X IO errors, always.
11138 It kills all frames on the display that we lost touch with.
11139 If that was the only one, it prints an error message and kills Emacs. */
7a13e894 11140
c118dd06 11141static int
e99db5a1 11142x_io_error_quitter (display)
c118dd06 11143 Display *display;
c118dd06 11144{
e99db5a1 11145 char buf[256];
dc6f92b8 11146
e99db5a1
RS
11147 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
11148 x_connection_closed (display, buf);
06a2c219 11149 return 0;
dc6f92b8 11150}
dc6f92b8 11151\f
f451eb13
JB
11152/* Changing the font of the frame. */
11153
76bcdf39
RS
11154/* Give frame F the font named FONTNAME as its default font, and
11155 return the full name of that font. FONTNAME may be a wildcard
11156 pattern; in that case, we choose some font that fits the pattern.
11157 The return value shows which font we chose. */
11158
b5cf7a0e 11159Lisp_Object
f676886a
JB
11160x_new_font (f, fontname)
11161 struct frame *f;
dc6f92b8
JB
11162 register char *fontname;
11163{
dc43ef94 11164 struct font_info *fontp
ee569018 11165 = FS_LOAD_FONT (f, 0, fontname, -1);
dc6f92b8 11166
dc43ef94
KH
11167 if (!fontp)
11168 return Qnil;
2224a5fc 11169
dc43ef94 11170 f->output_data.x->font = (XFontStruct *) (fontp->font);
b4192550 11171 f->output_data.x->baseline_offset = fontp->baseline_offset;
dc43ef94
KH
11172 f->output_data.x->fontset = -1;
11173
b2cad826
KH
11174 /* Compute the scroll bar width in character columns. */
11175 if (f->scroll_bar_pixel_width > 0)
11176 {
7556890b 11177 int wid = FONT_WIDTH (f->output_data.x->font);
b2cad826
KH
11178 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
11179 }
11180 else
4e61bddf
RS
11181 {
11182 int wid = FONT_WIDTH (f->output_data.x->font);
11183 f->scroll_bar_cols = (14 + wid - 1) / wid;
11184 }
b2cad826 11185
f676886a 11186 /* Now make the frame display the given font. */
c118dd06 11187 if (FRAME_X_WINDOW (f) != 0)
dc6f92b8 11188 {
7556890b
RS
11189 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
11190 f->output_data.x->font->fid);
11191 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
11192 f->output_data.x->font->fid);
11193 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
11194 f->output_data.x->font->fid);
f676886a 11195
a27f9f86 11196 frame_update_line_height (f);
0134a210 11197 x_set_window_size (f, 0, f->width, f->height);
dc6f92b8 11198 }
a27f9f86
RS
11199 else
11200 /* If we are setting a new frame's font for the first time,
11201 there are no faces yet, so this font's height is the line height. */
7556890b 11202 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
dc6f92b8 11203
dc43ef94
KH
11204 return build_string (fontp->full_name);
11205}
11206
11207/* Give frame F the fontset named FONTSETNAME as its default font, and
11208 return the full name of that fontset. FONTSETNAME may be a wildcard
b5210ea7
KH
11209 pattern; in that case, we choose some fontset that fits the pattern.
11210 The return value shows which fontset we chose. */
b5cf7a0e 11211
dc43ef94
KH
11212Lisp_Object
11213x_new_fontset (f, fontsetname)
11214 struct frame *f;
11215 char *fontsetname;
11216{
ee569018 11217 int fontset = fs_query_fontset (build_string (fontsetname), 0);
dc43ef94 11218 Lisp_Object result;
b5cf7a0e 11219
dc43ef94
KH
11220 if (fontset < 0)
11221 return Qnil;
b5cf7a0e 11222
2da424f1
KH
11223 if (f->output_data.x->fontset == fontset)
11224 /* This fontset is already set in frame F. There's nothing more
11225 to do. */
ee569018 11226 return fontset_name (fontset);
dc43ef94 11227
ee569018 11228 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
dc43ef94
KH
11229
11230 if (!STRINGP (result))
11231 /* Can't load ASCII font. */
11232 return Qnil;
11233
11234 /* Since x_new_font doesn't update any fontset information, do it now. */
11235 f->output_data.x->fontset = fontset;
dc43ef94 11236
f5d11644
GM
11237#ifdef HAVE_X_I18N
11238 if (FRAME_XIC (f)
11239 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
ee569018 11240 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
f5d11644
GM
11241#endif
11242
dc43ef94 11243 return build_string (fontsetname);
dc6f92b8 11244}
f5d11644
GM
11245
11246\f
11247/***********************************************************************
11248 X Input Methods
11249 ***********************************************************************/
11250
11251#ifdef HAVE_X_I18N
11252
11253#ifdef HAVE_X11R6
11254
11255/* XIM destroy callback function, which is called whenever the
11256 connection to input method XIM dies. CLIENT_DATA contains a
11257 pointer to the x_display_info structure corresponding to XIM. */
11258
11259static void
11260xim_destroy_callback (xim, client_data, call_data)
11261 XIM xim;
11262 XPointer client_data;
11263 XPointer call_data;
11264{
11265 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
11266 Lisp_Object frame, tail;
11267
11268 BLOCK_INPUT;
11269
11270 /* No need to call XDestroyIC.. */
11271 FOR_EACH_FRAME (tail, frame)
11272 {
11273 struct frame *f = XFRAME (frame);
11274 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
11275 {
11276 FRAME_XIC (f) = NULL;
11277 if (FRAME_XIC_FONTSET (f))
11278 {
11279 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
11280 FRAME_XIC_FONTSET (f) = NULL;
11281 }
11282 }
11283 }
11284
11285 /* No need to call XCloseIM. */
11286 dpyinfo->xim = NULL;
11287 XFree (dpyinfo->xim_styles);
11288 UNBLOCK_INPUT;
11289}
11290
11291#endif /* HAVE_X11R6 */
11292
11293/* Open the connection to the XIM server on display DPYINFO.
11294 RESOURCE_NAME is the resource name Emacs uses. */
11295
11296static void
11297xim_open_dpy (dpyinfo, resource_name)
11298 struct x_display_info *dpyinfo;
11299 char *resource_name;
11300{
287f7dd6 11301#ifdef USE_XIM
f5d11644
GM
11302 XIM xim;
11303
11304 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
11305 dpyinfo->xim = xim;
11306
11307 if (xim)
11308 {
f5d11644
GM
11309#ifdef HAVE_X11R6
11310 XIMCallback destroy;
11311#endif
11312
11313 /* Get supported styles and XIM values. */
11314 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
11315
11316#ifdef HAVE_X11R6
11317 destroy.callback = xim_destroy_callback;
11318 destroy.client_data = (XPointer)dpyinfo;
68642df6 11319 /* This isn't prptotyped in OSF 5.0. */
f5d11644
GM
11320 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
11321#endif
11322 }
287f7dd6
GM
11323
11324#else /* not USE_XIM */
11325 dpyinfo->xim = NULL;
11326#endif /* not USE_XIM */
f5d11644
GM
11327}
11328
11329
b9de836c 11330#ifdef HAVE_X11R6_XIM
f5d11644
GM
11331
11332struct xim_inst_t
11333{
11334 struct x_display_info *dpyinfo;
11335 char *resource_name;
11336};
11337
11338/* XIM instantiate callback function, which is called whenever an XIM
11339 server is available. DISPLAY is teh display of the XIM.
11340 CLIENT_DATA contains a pointer to an xim_inst_t structure created
11341 when the callback was registered. */
11342
11343static void
11344xim_instantiate_callback (display, client_data, call_data)
11345 Display *display;
11346 XPointer client_data;
11347 XPointer call_data;
11348{
11349 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
11350 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
11351
11352 /* We don't support multiple XIM connections. */
11353 if (dpyinfo->xim)
11354 return;
11355
11356 xim_open_dpy (dpyinfo, xim_inst->resource_name);
11357
11358 /* Create XIC for the existing frames on the same display, as long
11359 as they have no XIC. */
11360 if (dpyinfo->xim && dpyinfo->reference_count > 0)
11361 {
11362 Lisp_Object tail, frame;
11363
11364 BLOCK_INPUT;
11365 FOR_EACH_FRAME (tail, frame)
11366 {
11367 struct frame *f = XFRAME (frame);
11368
11369 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
11370 if (FRAME_XIC (f) == NULL)
11371 {
11372 create_frame_xic (f);
11373 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
11374 xic_set_statusarea (f);
11375 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
11376 {
11377 struct window *w = XWINDOW (f->selected_window);
11378 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
11379 }
11380 }
11381 }
11382
11383 UNBLOCK_INPUT;
11384 }
11385}
11386
b9de836c 11387#endif /* HAVE_X11R6_XIM */
f5d11644
GM
11388
11389
11390/* Open a connection to the XIM server on display DPYINFO.
11391 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
11392 connection only at the first time. On X11R6, open the connection
11393 in the XIM instantiate callback function. */
11394
11395static void
11396xim_initialize (dpyinfo, resource_name)
11397 struct x_display_info *dpyinfo;
11398 char *resource_name;
11399{
287f7dd6 11400#ifdef USE_XIM
b9de836c 11401#ifdef HAVE_X11R6_XIM
f5d11644
GM
11402 struct xim_inst_t *xim_inst;
11403 int len;
11404
11405 dpyinfo->xim = NULL;
11406 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
11407 xim_inst->dpyinfo = dpyinfo;
11408 len = strlen (resource_name);
11409 xim_inst->resource_name = (char *) xmalloc (len + 1);
11410 bcopy (resource_name, xim_inst->resource_name, len + 1);
11411 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11412 resource_name, EMACS_CLASS,
11413 xim_instantiate_callback,
2ebb2f8b
DL
11414 /* Fixme: This is XPointer in
11415 XFree86 but (XPointer *) on
11416 Tru64, at least. */
11417 (XPointer) xim_inst);
b9de836c 11418#else /* not HAVE_X11R6_XIM */
f5d11644
GM
11419 dpyinfo->xim = NULL;
11420 xim_open_dpy (dpyinfo, resource_name);
b9de836c 11421#endif /* not HAVE_X11R6_XIM */
287f7dd6
GM
11422
11423#else /* not USE_XIM */
11424 dpyinfo->xim = NULL;
11425#endif /* not USE_XIM */
f5d11644
GM
11426}
11427
11428
11429/* Close the connection to the XIM server on display DPYINFO. */
11430
11431static void
11432xim_close_dpy (dpyinfo)
11433 struct x_display_info *dpyinfo;
11434{
287f7dd6 11435#ifdef USE_XIM
b9de836c 11436#ifdef HAVE_X11R6_XIM
f5d11644
GM
11437 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11438 NULL, EMACS_CLASS,
11439 xim_instantiate_callback, NULL);
b9de836c 11440#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
11441 XCloseIM (dpyinfo->xim);
11442 dpyinfo->xim = NULL;
11443 XFree (dpyinfo->xim_styles);
287f7dd6 11444#endif /* USE_XIM */
f5d11644
GM
11445}
11446
b9de836c 11447#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
11448
11449
dc6f92b8 11450\f
2e365682
RS
11451/* Calculate the absolute position in frame F
11452 from its current recorded position values and gravity. */
11453
dfcf069d 11454void
43bca5d5 11455x_calc_absolute_position (f)
f676886a 11456 struct frame *f;
dc6f92b8 11457{
06a2c219 11458 Window child;
6dba1858 11459 int win_x = 0, win_y = 0;
7556890b 11460 int flags = f->output_data.x->size_hint_flags;
c81412a0
KH
11461 int this_window;
11462
9829ddba
RS
11463 /* We have nothing to do if the current position
11464 is already for the top-left corner. */
11465 if (! ((flags & XNegative) || (flags & YNegative)))
11466 return;
11467
c81412a0 11468#ifdef USE_X_TOOLKIT
7556890b 11469 this_window = XtWindow (f->output_data.x->widget);
c81412a0
KH
11470#else
11471 this_window = FRAME_X_WINDOW (f);
11472#endif
6dba1858
RS
11473
11474 /* Find the position of the outside upper-left corner of
9829ddba
RS
11475 the inner window, with respect to the outer window.
11476 But do this only if we will need the results. */
7556890b 11477 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6dba1858 11478 {
9829ddba
RS
11479 int count;
11480
6dba1858 11481 BLOCK_INPUT;
9829ddba
RS
11482 count = x_catch_errors (FRAME_X_DISPLAY (f));
11483 while (1)
11484 {
11485 x_clear_errors (FRAME_X_DISPLAY (f));
11486 XTranslateCoordinates (FRAME_X_DISPLAY (f),
11487
11488 /* From-window, to-window. */
11489 this_window,
11490 f->output_data.x->parent_desc,
11491
11492 /* From-position, to-position. */
11493 0, 0, &win_x, &win_y,
11494
11495 /* Child of win. */
11496 &child);
11497 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
11498 {
11499 Window newroot, newparent = 0xdeadbeef;
11500 Window *newchildren;
2ebb2f8b 11501 unsigned int nchildren;
9829ddba
RS
11502
11503 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
11504 &newparent, &newchildren, &nchildren))
11505 break;
58769bee 11506
7c3c78a3 11507 XFree ((char *) newchildren);
6dba1858 11508
9829ddba
RS
11509 f->output_data.x->parent_desc = newparent;
11510 }
11511 else
11512 break;
11513 }
6dba1858 11514
9829ddba 11515 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
6dba1858
RS
11516 UNBLOCK_INPUT;
11517 }
11518
11519 /* Treat negative positions as relative to the leftmost bottommost
11520 position that fits on the screen. */
20f55f9a 11521 if (flags & XNegative)
7556890b 11522 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
2e365682
RS
11523 - 2 * f->output_data.x->border_width - win_x
11524 - PIXEL_WIDTH (f)
11525 + f->output_data.x->left_pos);
dc6f92b8 11526
20f55f9a 11527 if (flags & YNegative)
06a2c219
GM
11528 {
11529 int menubar_height = 0;
11530
11531#ifdef USE_X_TOOLKIT
11532 if (f->output_data.x->menubar_widget)
11533 menubar_height
11534 = (f->output_data.x->menubar_widget->core.height
11535 + f->output_data.x->menubar_widget->core.border_width);
11536#endif
11537
11538 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
11539 - 2 * f->output_data.x->border_width
11540 - win_y
11541 - PIXEL_HEIGHT (f)
11542 - menubar_height
11543 + f->output_data.x->top_pos);
11544 }
2e365682 11545
3a35ab44
RS
11546 /* The left_pos and top_pos
11547 are now relative to the top and left screen edges,
11548 so the flags should correspond. */
7556890b 11549 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
dc6f92b8
JB
11550}
11551
3a35ab44
RS
11552/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
11553 to really change the position, and 0 when calling from
11554 x_make_frame_visible (in that case, XOFF and YOFF are the current
aa3ff7c9
KH
11555 position values). It is -1 when calling from x_set_frame_parameters,
11556 which means, do adjust for borders but don't change the gravity. */
3a35ab44 11557
dfcf069d 11558void
dc05a16b 11559x_set_offset (f, xoff, yoff, change_gravity)
f676886a 11560 struct frame *f;
dc6f92b8 11561 register int xoff, yoff;
dc05a16b 11562 int change_gravity;
dc6f92b8 11563{
4a4cbdd5
KH
11564 int modified_top, modified_left;
11565
aa3ff7c9 11566 if (change_gravity > 0)
3a35ab44 11567 {
7556890b
RS
11568 f->output_data.x->top_pos = yoff;
11569 f->output_data.x->left_pos = xoff;
11570 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
3a35ab44 11571 if (xoff < 0)
7556890b 11572 f->output_data.x->size_hint_flags |= XNegative;
3a35ab44 11573 if (yoff < 0)
7556890b
RS
11574 f->output_data.x->size_hint_flags |= YNegative;
11575 f->output_data.x->win_gravity = NorthWestGravity;
3a35ab44 11576 }
43bca5d5 11577 x_calc_absolute_position (f);
dc6f92b8
JB
11578
11579 BLOCK_INPUT;
c32cdd9a 11580 x_wm_set_size_hint (f, (long) 0, 0);
3a35ab44 11581
7556890b
RS
11582 modified_left = f->output_data.x->left_pos;
11583 modified_top = f->output_data.x->top_pos;
e73ec6fa
RS
11584#if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
11585 this seems to be unnecessary and incorrect. rms, 4/17/97. */
11586 /* It is a mystery why we need to add the border_width here
11587 when the frame is already visible, but experiment says we do. */
aa3ff7c9 11588 if (change_gravity != 0)
4a4cbdd5 11589 {
7556890b
RS
11590 modified_left += f->output_data.x->border_width;
11591 modified_top += f->output_data.x->border_width;
4a4cbdd5 11592 }
e73ec6fa 11593#endif
4a4cbdd5 11594
3afe33e7 11595#ifdef USE_X_TOOLKIT
7556890b 11596 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
4a4cbdd5 11597 modified_left, modified_top);
3afe33e7 11598#else /* not USE_X_TOOLKIT */
334208b7 11599 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4a4cbdd5 11600 modified_left, modified_top);
3afe33e7 11601#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11602 UNBLOCK_INPUT;
11603}
11604
bc20ebbf
FP
11605/* Call this to change the size of frame F's x-window.
11606 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
11607 for this size change and subsequent size changes.
11608 Otherwise we leave the window gravity unchanged. */
dc6f92b8 11609
dfcf069d 11610void
bc20ebbf 11611x_set_window_size (f, change_gravity, cols, rows)
f676886a 11612 struct frame *f;
bc20ebbf 11613 int change_gravity;
b1c884c3 11614 int cols, rows;
dc6f92b8 11615{
06a2c219 11616#ifndef USE_X_TOOLKIT
dc6f92b8 11617 int pixelwidth, pixelheight;
06a2c219 11618#endif
dc6f92b8 11619
80fd1fe2 11620 BLOCK_INPUT;
aee9a898
RS
11621
11622#ifdef USE_X_TOOLKIT
3a20653d
RS
11623 {
11624 /* The x and y position of the widget is clobbered by the
11625 call to XtSetValues within EmacsFrameSetCharSize.
11626 This is a real kludge, but I don't understand Xt so I can't
11627 figure out a correct fix. Can anyone else tell me? -- rms. */
7556890b
RS
11628 int xpos = f->output_data.x->widget->core.x;
11629 int ypos = f->output_data.x->widget->core.y;
11630 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
11631 f->output_data.x->widget->core.x = xpos;
11632 f->output_data.x->widget->core.y = ypos;
3a20653d 11633 }
80fd1fe2
FP
11634
11635#else /* not USE_X_TOOLKIT */
11636
b1c884c3 11637 check_frame_size (f, &rows, &cols);
7556890b 11638 f->output_data.x->vertical_scroll_bar_extra
b2cad826
KH
11639 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
11640 ? 0
11641 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
480407eb 11642 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7556890b 11643 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
06a2c219 11644 f->output_data.x->flags_areas_extra
110859fc 11645 = FRAME_FLAGS_AREA_WIDTH (f);
f451eb13
JB
11646 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
11647 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
dc6f92b8 11648
7556890b 11649 f->output_data.x->win_gravity = NorthWestGravity;
c32cdd9a 11650 x_wm_set_size_hint (f, (long) 0, 0);
6ccf47d1 11651
334208b7
RS
11652 XSync (FRAME_X_DISPLAY (f), False);
11653 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11654 pixelwidth, pixelheight);
b1c884c3
JB
11655
11656 /* Now, strictly speaking, we can't be sure that this is accurate,
11657 but the window manager will get around to dealing with the size
11658 change request eventually, and we'll hear how it went when the
dbc4e1c1
JB
11659 ConfigureNotify event gets here.
11660
11661 We could just not bother storing any of this information here,
11662 and let the ConfigureNotify event set everything up, but that
fddd5ceb 11663 might be kind of confusing to the Lisp code, since size changes
dbc4e1c1 11664 wouldn't be reported in the frame parameters until some random
fddd5ceb
RS
11665 point in the future when the ConfigureNotify event arrives.
11666
11667 We pass 1 for DELAY since we can't run Lisp code inside of
11668 a BLOCK_INPUT. */
7d1e984f 11669 change_frame_size (f, rows, cols, 0, 1, 0);
b1c884c3
JB
11670 PIXEL_WIDTH (f) = pixelwidth;
11671 PIXEL_HEIGHT (f) = pixelheight;
11672
aee9a898
RS
11673 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11674 receive in the ConfigureNotify event; if we get what we asked
11675 for, then the event won't cause the screen to become garbaged, so
11676 we have to make sure to do it here. */
11677 SET_FRAME_GARBAGED (f);
11678
11679 XFlush (FRAME_X_DISPLAY (f));
11680
11681#endif /* not USE_X_TOOLKIT */
11682
4d73d038 11683 /* If cursor was outside the new size, mark it as off. */
06a2c219 11684 mark_window_cursors_off (XWINDOW (f->root_window));
4d73d038 11685
aee9a898
RS
11686 /* Clear out any recollection of where the mouse highlighting was,
11687 since it might be in a place that's outside the new frame size.
11688 Actually checking whether it is outside is a pain in the neck,
11689 so don't try--just let the highlighting be done afresh with new size. */
e687d06e 11690 cancel_mouse_face (f);
dbc4e1c1 11691
dc6f92b8
JB
11692 UNBLOCK_INPUT;
11693}
dc6f92b8 11694\f
d047c4eb 11695/* Mouse warping. */
dc6f92b8 11696
9b378208 11697void
f676886a
JB
11698x_set_mouse_position (f, x, y)
11699 struct frame *f;
dc6f92b8
JB
11700 int x, y;
11701{
11702 int pix_x, pix_y;
11703
7556890b
RS
11704 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
11705 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
f451eb13
JB
11706
11707 if (pix_x < 0) pix_x = 0;
11708 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
11709
11710 if (pix_y < 0) pix_y = 0;
11711 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
dc6f92b8
JB
11712
11713 BLOCK_INPUT;
dc6f92b8 11714
334208b7
RS
11715 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11716 0, 0, 0, 0, pix_x, pix_y);
dc6f92b8
JB
11717 UNBLOCK_INPUT;
11718}
11719
9b378208
RS
11720/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11721
11722void
11723x_set_mouse_pixel_position (f, pix_x, pix_y)
11724 struct frame *f;
11725 int pix_x, pix_y;
11726{
11727 BLOCK_INPUT;
11728
334208b7
RS
11729 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11730 0, 0, 0, 0, pix_x, pix_y);
9b378208
RS
11731 UNBLOCK_INPUT;
11732}
d047c4eb
KH
11733\f
11734/* focus shifting, raising and lowering. */
9b378208 11735
dfcf069d 11736void
f676886a
JB
11737x_focus_on_frame (f)
11738 struct frame *f;
dc6f92b8 11739{
1fb20991 11740#if 0 /* This proves to be unpleasant. */
f676886a 11741 x_raise_frame (f);
1fb20991 11742#endif
6d4238f3
JB
11743#if 0
11744 /* I don't think that the ICCCM allows programs to do things like this
11745 without the interaction of the window manager. Whatever you end up
f676886a 11746 doing with this code, do it to x_unfocus_frame too. */
334208b7 11747 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dc6f92b8 11748 RevertToPointerRoot, CurrentTime);
c118dd06 11749#endif /* ! 0 */
dc6f92b8
JB
11750}
11751
dfcf069d 11752void
f676886a
JB
11753x_unfocus_frame (f)
11754 struct frame *f;
dc6f92b8 11755{
6d4238f3 11756#if 0
f676886a 11757 /* Look at the remarks in x_focus_on_frame. */
0f941935 11758 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
334208b7 11759 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
dc6f92b8 11760 RevertToPointerRoot, CurrentTime);
c118dd06 11761#endif /* ! 0 */
dc6f92b8
JB
11762}
11763
f676886a 11764/* Raise frame F. */
dc6f92b8 11765
dfcf069d 11766void
f676886a
JB
11767x_raise_frame (f)
11768 struct frame *f;
dc6f92b8 11769{
3a88c238 11770 if (f->async_visible)
dc6f92b8
JB
11771 {
11772 BLOCK_INPUT;
3afe33e7 11773#ifdef USE_X_TOOLKIT
7556890b 11774 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 11775#else /* not USE_X_TOOLKIT */
334208b7 11776 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11777#endif /* not USE_X_TOOLKIT */
334208b7 11778 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
11779 UNBLOCK_INPUT;
11780 }
11781}
11782
f676886a 11783/* Lower frame F. */
dc6f92b8 11784
dfcf069d 11785void
f676886a
JB
11786x_lower_frame (f)
11787 struct frame *f;
dc6f92b8 11788{
3a88c238 11789 if (f->async_visible)
dc6f92b8
JB
11790 {
11791 BLOCK_INPUT;
3afe33e7 11792#ifdef USE_X_TOOLKIT
7556890b 11793 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 11794#else /* not USE_X_TOOLKIT */
334208b7 11795 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11796#endif /* not USE_X_TOOLKIT */
334208b7 11797 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
11798 UNBLOCK_INPUT;
11799 }
11800}
11801
dbc4e1c1 11802static void
6b0442dc 11803XTframe_raise_lower (f, raise_flag)
dbc4e1c1 11804 FRAME_PTR f;
6b0442dc 11805 int raise_flag;
dbc4e1c1 11806{
6b0442dc 11807 if (raise_flag)
dbc4e1c1
JB
11808 x_raise_frame (f);
11809 else
11810 x_lower_frame (f);
11811}
d047c4eb
KH
11812\f
11813/* Change of visibility. */
dc6f92b8 11814
9382638d
KH
11815/* This tries to wait until the frame is really visible.
11816 However, if the window manager asks the user where to position
11817 the frame, this will return before the user finishes doing that.
11818 The frame will not actually be visible at that time,
11819 but it will become visible later when the window manager
11820 finishes with it. */
11821
dfcf069d 11822void
f676886a
JB
11823x_make_frame_visible (f)
11824 struct frame *f;
dc6f92b8 11825{
990ba854 11826 Lisp_Object type;
1aa6072f 11827 int original_top, original_left;
dc6f92b8 11828
dc6f92b8 11829 BLOCK_INPUT;
dc6f92b8 11830
990ba854
RS
11831 type = x_icon_type (f);
11832 if (!NILP (type))
11833 x_bitmap_icon (f, type);
bdcd49ba 11834
f676886a 11835 if (! FRAME_VISIBLE_P (f))
90e65f07 11836 {
1aa6072f
RS
11837 /* We test FRAME_GARBAGED_P here to make sure we don't
11838 call x_set_offset a second time
11839 if we get to x_make_frame_visible a second time
11840 before the window gets really visible. */
11841 if (! FRAME_ICONIFIED_P (f)
11842 && ! f->output_data.x->asked_for_visible)
11843 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
11844
11845 f->output_data.x->asked_for_visible = 1;
11846
90e65f07 11847 if (! EQ (Vx_no_window_manager, Qt))
f676886a 11848 x_wm_set_window_state (f, NormalState);
3afe33e7 11849#ifdef USE_X_TOOLKIT
d7a38a2e 11850 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 11851 XtMapWidget (f->output_data.x->widget);
3afe33e7 11852#else /* not USE_X_TOOLKIT */
7f9c7f94 11853 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11854#endif /* not USE_X_TOOLKIT */
0134a210
RS
11855#if 0 /* This seems to bring back scroll bars in the wrong places
11856 if the window configuration has changed. They seem
11857 to come back ok without this. */
ab648270 11858 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
334208b7 11859 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
0134a210 11860#endif
90e65f07 11861 }
dc6f92b8 11862
334208b7 11863 XFlush (FRAME_X_DISPLAY (f));
90e65f07 11864
0dacf791
RS
11865 /* Synchronize to ensure Emacs knows the frame is visible
11866 before we do anything else. We do this loop with input not blocked
11867 so that incoming events are handled. */
11868 {
11869 Lisp_Object frame;
12ce2351 11870 int count;
28c01ffe
RS
11871 /* This must be before UNBLOCK_INPUT
11872 since events that arrive in response to the actions above
11873 will set it when they are handled. */
11874 int previously_visible = f->output_data.x->has_been_visible;
1aa6072f
RS
11875
11876 original_left = f->output_data.x->left_pos;
11877 original_top = f->output_data.x->top_pos;
c0a04927
RS
11878
11879 /* This must come after we set COUNT. */
11880 UNBLOCK_INPUT;
11881
2745e6c4 11882 /* We unblock here so that arriving X events are processed. */
1aa6072f 11883
dcb07ae9
RS
11884 /* Now move the window back to where it was "supposed to be".
11885 But don't do it if the gravity is negative.
11886 When the gravity is negative, this uses a position
28c01ffe
RS
11887 that is 3 pixels too low. Perhaps that's really the border width.
11888
11889 Don't do this if the window has never been visible before,
11890 because the window manager may choose the position
11891 and we don't want to override it. */
1aa6072f 11892
4d3f5d9a 11893 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
06c488fd 11894 && f->output_data.x->win_gravity == NorthWestGravity
28c01ffe 11895 && previously_visible)
1aa6072f 11896 {
2745e6c4
RS
11897 Drawable rootw;
11898 int x, y;
11899 unsigned int width, height, border, depth;
06a2c219 11900
1aa6072f 11901 BLOCK_INPUT;
9829ddba 11902
06a2c219
GM
11903 /* On some window managers (such as FVWM) moving an existing
11904 window, even to the same place, causes the window manager
11905 to introduce an offset. This can cause the window to move
11906 to an unexpected location. Check the geometry (a little
11907 slow here) and then verify that the window is in the right
11908 place. If the window is not in the right place, move it
11909 there, and take the potential window manager hit. */
2745e6c4
RS
11910 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11911 &rootw, &x, &y, &width, &height, &border, &depth);
11912
11913 if (original_left != x || original_top != y)
11914 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11915 original_left, original_top);
11916
1aa6072f
RS
11917 UNBLOCK_INPUT;
11918 }
9829ddba 11919
e0c1aef2 11920 XSETFRAME (frame, f);
c0a04927 11921
12ce2351
GM
11922 /* Wait until the frame is visible. Process X events until a
11923 MapNotify event has been seen, or until we think we won't get a
11924 MapNotify at all.. */
11925 for (count = input_signal_count + 10;
11926 input_signal_count < count && !FRAME_VISIBLE_P (f);)
2a6cf806 11927 {
12ce2351 11928 /* Force processing of queued events. */
334208b7 11929 x_sync (f);
12ce2351
GM
11930
11931 /* Machines that do polling rather than SIGIO have been
11932 observed to go into a busy-wait here. So we'll fake an
11933 alarm signal to let the handler know that there's something
11934 to be read. We used to raise a real alarm, but it seems
11935 that the handler isn't always enabled here. This is
11936 probably a bug. */
8b2f8d4e 11937 if (input_polling_used ())
3b2fa4e6 11938 {
12ce2351
GM
11939 /* It could be confusing if a real alarm arrives while
11940 processing the fake one. Turn it off and let the
11941 handler reset it. */
3e71d8f2 11942 extern void poll_for_input_1 P_ ((void));
bffcfca9
GM
11943 int old_poll_suppress_count = poll_suppress_count;
11944 poll_suppress_count = 1;
11945 poll_for_input_1 ();
11946 poll_suppress_count = old_poll_suppress_count;
3b2fa4e6 11947 }
12ce2351
GM
11948
11949 /* See if a MapNotify event has been processed. */
11950 FRAME_SAMPLE_VISIBILITY (f);
2a6cf806 11951 }
0dacf791 11952 }
dc6f92b8
JB
11953}
11954
06a2c219 11955/* Change from mapped state to withdrawn state. */
dc6f92b8 11956
d047c4eb
KH
11957/* Make the frame visible (mapped and not iconified). */
11958
dfcf069d 11959void
f676886a
JB
11960x_make_frame_invisible (f)
11961 struct frame *f;
dc6f92b8 11962{
546e6d5b
RS
11963 Window window;
11964
11965#ifdef USE_X_TOOLKIT
11966 /* Use the frame's outermost window, not the one we normally draw on. */
7556890b 11967 window = XtWindow (f->output_data.x->widget);
546e6d5b
RS
11968#else /* not USE_X_TOOLKIT */
11969 window = FRAME_X_WINDOW (f);
11970#endif /* not USE_X_TOOLKIT */
dc6f92b8 11971
9319ae23 11972 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
11973 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11974 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 11975
5627c40e 11976#if 0/* This might add unreliability; I don't trust it -- rms. */
9319ae23 11977 if (! f->async_visible && ! f->async_iconified)
dc6f92b8 11978 return;
5627c40e 11979#endif
dc6f92b8
JB
11980
11981 BLOCK_INPUT;
c118dd06 11982
af31d76f
RS
11983 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
11984 that the current position of the window is user-specified, rather than
11985 program-specified, so that when the window is mapped again, it will be
11986 placed at the same location, without forcing the user to position it
11987 by hand again (they have already done that once for this window.) */
c32cdd9a 11988 x_wm_set_size_hint (f, (long) 0, 1);
af31d76f 11989
c118dd06
JB
11990#ifdef HAVE_X11R4
11991
334208b7
RS
11992 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
11993 DefaultScreen (FRAME_X_DISPLAY (f))))
c118dd06
JB
11994 {
11995 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11996 error ("Can't notify window manager of window withdrawal");
c118dd06 11997 }
c118dd06 11998#else /* ! defined (HAVE_X11R4) */
16bd92ea 11999
c118dd06 12000 /* Tell the window manager what we're going to do. */
dc6f92b8
JB
12001 if (! EQ (Vx_no_window_manager, Qt))
12002 {
16bd92ea 12003 XEvent unmap;
dc6f92b8 12004
16bd92ea 12005 unmap.xunmap.type = UnmapNotify;
546e6d5b 12006 unmap.xunmap.window = window;
334208b7 12007 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
16bd92ea 12008 unmap.xunmap.from_configure = False;
334208b7
RS
12009 if (! XSendEvent (FRAME_X_DISPLAY (f),
12010 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea 12011 False,
06a2c219 12012 SubstructureRedirectMaskSubstructureNotifyMask,
16bd92ea
JB
12013 &unmap))
12014 {
12015 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 12016 error ("Can't notify window manager of withdrawal");
16bd92ea 12017 }
dc6f92b8
JB
12018 }
12019
16bd92ea 12020 /* Unmap the window ourselves. Cheeky! */
334208b7 12021 XUnmapWindow (FRAME_X_DISPLAY (f), window);
c118dd06 12022#endif /* ! defined (HAVE_X11R4) */
dc6f92b8 12023
5627c40e
RS
12024 /* We can't distinguish this from iconification
12025 just by the event that we get from the server.
12026 So we can't win using the usual strategy of letting
12027 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
12028 and synchronize with the server to make sure we agree. */
12029 f->visible = 0;
12030 FRAME_ICONIFIED_P (f) = 0;
12031 f->async_visible = 0;
12032 f->async_iconified = 0;
12033
334208b7 12034 x_sync (f);
5627c40e 12035
dc6f92b8
JB
12036 UNBLOCK_INPUT;
12037}
12038
06a2c219 12039/* Change window state from mapped to iconified. */
dc6f92b8 12040
dfcf069d 12041void
f676886a
JB
12042x_iconify_frame (f)
12043 struct frame *f;
dc6f92b8 12044{
3afe33e7 12045 int result;
990ba854 12046 Lisp_Object type;
dc6f92b8 12047
9319ae23 12048 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
12049 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12050 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 12051
3a88c238 12052 if (f->async_iconified)
dc6f92b8
JB
12053 return;
12054
3afe33e7 12055 BLOCK_INPUT;
546e6d5b 12056
9af3143a
RS
12057 FRAME_SAMPLE_VISIBILITY (f);
12058
990ba854
RS
12059 type = x_icon_type (f);
12060 if (!NILP (type))
12061 x_bitmap_icon (f, type);
bdcd49ba
RS
12062
12063#ifdef USE_X_TOOLKIT
12064
546e6d5b
RS
12065 if (! FRAME_VISIBLE_P (f))
12066 {
12067 if (! EQ (Vx_no_window_manager, Qt))
12068 x_wm_set_window_state (f, IconicState);
12069 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 12070 XtMapWidget (f->output_data.x->widget);
9cf30a30
RS
12071 /* The server won't give us any event to indicate
12072 that an invisible frame was changed to an icon,
12073 so we have to record it here. */
12074 f->iconified = 1;
1e6bc770 12075 f->visible = 1;
9cf30a30 12076 f->async_iconified = 1;
1e6bc770 12077 f->async_visible = 0;
546e6d5b
RS
12078 UNBLOCK_INPUT;
12079 return;
12080 }
12081
334208b7 12082 result = XIconifyWindow (FRAME_X_DISPLAY (f),
7556890b 12083 XtWindow (f->output_data.x->widget),
334208b7 12084 DefaultScreen (FRAME_X_DISPLAY (f)));
3afe33e7
RS
12085 UNBLOCK_INPUT;
12086
12087 if (!result)
546e6d5b 12088 error ("Can't notify window manager of iconification");
3afe33e7
RS
12089
12090 f->async_iconified = 1;
1e6bc770
RS
12091 f->async_visible = 0;
12092
8c002a25
KH
12093
12094 BLOCK_INPUT;
334208b7 12095 XFlush (FRAME_X_DISPLAY (f));
8c002a25 12096 UNBLOCK_INPUT;
3afe33e7
RS
12097#else /* not USE_X_TOOLKIT */
12098
fd13dbb2
RS
12099 /* Make sure the X server knows where the window should be positioned,
12100 in case the user deiconifies with the window manager. */
12101 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
7556890b 12102 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
fd13dbb2 12103
16bd92ea
JB
12104 /* Since we don't know which revision of X we're running, we'll use both
12105 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
12106
12107 /* X11R4: send a ClientMessage to the window manager using the
12108 WM_CHANGE_STATE type. */
12109 {
12110 XEvent message;
58769bee 12111
c118dd06 12112 message.xclient.window = FRAME_X_WINDOW (f);
16bd92ea 12113 message.xclient.type = ClientMessage;
334208b7 12114 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
16bd92ea
JB
12115 message.xclient.format = 32;
12116 message.xclient.data.l[0] = IconicState;
12117
334208b7
RS
12118 if (! XSendEvent (FRAME_X_DISPLAY (f),
12119 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea
JB
12120 False,
12121 SubstructureRedirectMask | SubstructureNotifyMask,
12122 &message))
dc6f92b8
JB
12123 {
12124 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 12125 error ("Can't notify window manager of iconification");
dc6f92b8 12126 }
16bd92ea 12127 }
dc6f92b8 12128
58769bee 12129 /* X11R3: set the initial_state field of the window manager hints to
16bd92ea
JB
12130 IconicState. */
12131 x_wm_set_window_state (f, IconicState);
dc6f92b8 12132
a9c00105
RS
12133 if (!FRAME_VISIBLE_P (f))
12134 {
12135 /* If the frame was withdrawn, before, we must map it. */
7f9c7f94 12136 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
a9c00105
RS
12137 }
12138
3a88c238 12139 f->async_iconified = 1;
1e6bc770 12140 f->async_visible = 0;
dc6f92b8 12141
334208b7 12142 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 12143 UNBLOCK_INPUT;
8c002a25 12144#endif /* not USE_X_TOOLKIT */
dc6f92b8 12145}
d047c4eb 12146\f
c0ff3fab 12147/* Destroy the X window of frame F. */
dc6f92b8 12148
dfcf069d 12149void
c0ff3fab 12150x_destroy_window (f)
f676886a 12151 struct frame *f;
dc6f92b8 12152{
7f9c7f94
RS
12153 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12154
dc6f92b8 12155 BLOCK_INPUT;
c0ff3fab 12156
6186a4a0
RS
12157 /* If a display connection is dead, don't try sending more
12158 commands to the X server. */
12159 if (dpyinfo->display != 0)
12160 {
12161 if (f->output_data.x->icon_desc != 0)
12162 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
31f41daf 12163#ifdef HAVE_X_I18N
f5d11644
GM
12164 if (FRAME_XIC (f))
12165 free_frame_xic (f);
31f41daf 12166#endif
6186a4a0 12167 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->window_desc);
3afe33e7 12168#ifdef USE_X_TOOLKIT
06a2c219
GM
12169 if (f->output_data.x->widget)
12170 XtDestroyWidget (f->output_data.x->widget);
6186a4a0 12171 free_frame_menubar (f);
3afe33e7
RS
12172#endif /* USE_X_TOOLKIT */
12173
3e71d8f2
GM
12174 unload_color (f, f->output_data.x->foreground_pixel);
12175 unload_color (f, f->output_data.x->background_pixel);
12176 unload_color (f, f->output_data.x->cursor_pixel);
12177 unload_color (f, f->output_data.x->cursor_foreground_pixel);
12178 unload_color (f, f->output_data.x->border_pixel);
12179 unload_color (f, f->output_data.x->mouse_pixel);
12180 if (f->output_data.x->scroll_bar_background_pixel != -1)
12181 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
12182 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
12183 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
12184 if (f->output_data.x->white_relief.allocated_p)
12185 unload_color (f, f->output_data.x->white_relief.pixel);
12186 if (f->output_data.x->black_relief.allocated_p)
12187 unload_color (f, f->output_data.x->black_relief.pixel);
12188
6186a4a0
RS
12189 free_frame_faces (f);
12190 XFlush (FRAME_X_DISPLAY (f));
12191 }
dc6f92b8 12192
df89d8a4 12193 if (f->output_data.x->saved_menu_event)
06a2c219 12194 xfree (f->output_data.x->saved_menu_event);
0c49ce2f 12195
7556890b
RS
12196 xfree (f->output_data.x);
12197 f->output_data.x = 0;
0f941935
KH
12198 if (f == dpyinfo->x_focus_frame)
12199 dpyinfo->x_focus_frame = 0;
12200 if (f == dpyinfo->x_focus_event_frame)
12201 dpyinfo->x_focus_event_frame = 0;
12202 if (f == dpyinfo->x_highlight_frame)
12203 dpyinfo->x_highlight_frame = 0;
c0ff3fab 12204
7f9c7f94
RS
12205 dpyinfo->reference_count--;
12206
12207 if (f == dpyinfo->mouse_face_mouse_frame)
dc05a16b 12208 {
7f9c7f94
RS
12209 dpyinfo->mouse_face_beg_row
12210 = dpyinfo->mouse_face_beg_col = -1;
12211 dpyinfo->mouse_face_end_row
12212 = dpyinfo->mouse_face_end_col = -1;
12213 dpyinfo->mouse_face_window = Qnil;
21323706
RS
12214 dpyinfo->mouse_face_deferred_gc = 0;
12215 dpyinfo->mouse_face_mouse_frame = 0;
dc05a16b 12216 }
0134a210 12217
c0ff3fab 12218 UNBLOCK_INPUT;
dc6f92b8
JB
12219}
12220\f
f451eb13
JB
12221/* Setting window manager hints. */
12222
af31d76f
RS
12223/* Set the normal size hints for the window manager, for frame F.
12224 FLAGS is the flags word to use--or 0 meaning preserve the flags
12225 that the window now has.
12226 If USER_POSITION is nonzero, we set the USPosition
12227 flag (this is useful when FLAGS is 0). */
6dba1858 12228
dfcf069d 12229void
af31d76f 12230x_wm_set_size_hint (f, flags, user_position)
f676886a 12231 struct frame *f;
af31d76f
RS
12232 long flags;
12233 int user_position;
dc6f92b8
JB
12234{
12235 XSizeHints size_hints;
3afe33e7
RS
12236
12237#ifdef USE_X_TOOLKIT
7e4f2521
FP
12238 Arg al[2];
12239 int ac = 0;
12240 Dimension widget_width, widget_height;
7556890b 12241 Window window = XtWindow (f->output_data.x->widget);
3afe33e7 12242#else /* not USE_X_TOOLKIT */
c118dd06 12243 Window window = FRAME_X_WINDOW (f);
3afe33e7 12244#endif /* not USE_X_TOOLKIT */
dc6f92b8 12245
b72a58fd
RS
12246 /* Setting PMaxSize caused various problems. */
12247 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
dc6f92b8 12248
7556890b
RS
12249 size_hints.x = f->output_data.x->left_pos;
12250 size_hints.y = f->output_data.x->top_pos;
7553a6b7 12251
7e4f2521
FP
12252#ifdef USE_X_TOOLKIT
12253 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
12254 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
7556890b 12255 XtGetValues (f->output_data.x->widget, al, ac);
7e4f2521
FP
12256 size_hints.height = widget_height;
12257 size_hints.width = widget_width;
12258#else /* not USE_X_TOOLKIT */
f676886a
JB
12259 size_hints.height = PIXEL_HEIGHT (f);
12260 size_hints.width = PIXEL_WIDTH (f);
7e4f2521 12261#endif /* not USE_X_TOOLKIT */
7553a6b7 12262
7556890b
RS
12263 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
12264 size_hints.height_inc = f->output_data.x->line_height;
334208b7
RS
12265 size_hints.max_width
12266 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
12267 size_hints.max_height
12268 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
0134a210 12269
d067ea8b
KH
12270 /* Calculate the base and minimum sizes.
12271
12272 (When we use the X toolkit, we don't do it here.
12273 Instead we copy the values that the widgets are using, below.) */
12274#ifndef USE_X_TOOLKIT
b1c884c3 12275 {
b0342f17 12276 int base_width, base_height;
0134a210 12277 int min_rows = 0, min_cols = 0;
b0342f17 12278
f451eb13
JB
12279 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
12280 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
b0342f17 12281
0134a210 12282 check_frame_size (f, &min_rows, &min_cols);
b0342f17 12283
0134a210
RS
12284 /* The window manager uses the base width hints to calculate the
12285 current number of rows and columns in the frame while
12286 resizing; min_width and min_height aren't useful for this
12287 purpose, since they might not give the dimensions for a
12288 zero-row, zero-column frame.
58769bee 12289
0134a210
RS
12290 We use the base_width and base_height members if we have
12291 them; otherwise, we set the min_width and min_height members
12292 to the size for a zero x zero frame. */
b0342f17
JB
12293
12294#ifdef HAVE_X11R4
0134a210
RS
12295 size_hints.flags |= PBaseSize;
12296 size_hints.base_width = base_width;
12297 size_hints.base_height = base_height;
12298 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
12299 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
b0342f17 12300#else
0134a210
RS
12301 size_hints.min_width = base_width;
12302 size_hints.min_height = base_height;
b0342f17 12303#endif
b1c884c3 12304 }
dc6f92b8 12305
d067ea8b 12306 /* If we don't need the old flags, we don't need the old hint at all. */
af31d76f 12307 if (flags)
dc6f92b8 12308 {
d067ea8b
KH
12309 size_hints.flags |= flags;
12310 goto no_read;
12311 }
12312#endif /* not USE_X_TOOLKIT */
12313
12314 {
12315 XSizeHints hints; /* Sometimes I hate X Windows... */
12316 long supplied_return;
12317 int value;
af31d76f
RS
12318
12319#ifdef HAVE_X11R4
d067ea8b
KH
12320 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
12321 &supplied_return);
af31d76f 12322#else
d067ea8b 12323 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
af31d76f 12324#endif
58769bee 12325
d067ea8b
KH
12326#ifdef USE_X_TOOLKIT
12327 size_hints.base_height = hints.base_height;
12328 size_hints.base_width = hints.base_width;
12329 size_hints.min_height = hints.min_height;
12330 size_hints.min_width = hints.min_width;
12331#endif
12332
12333 if (flags)
12334 size_hints.flags |= flags;
12335 else
12336 {
12337 if (value == 0)
12338 hints.flags = 0;
12339 if (hints.flags & PSize)
12340 size_hints.flags |= PSize;
12341 if (hints.flags & PPosition)
12342 size_hints.flags |= PPosition;
12343 if (hints.flags & USPosition)
12344 size_hints.flags |= USPosition;
12345 if (hints.flags & USSize)
12346 size_hints.flags |= USSize;
12347 }
12348 }
12349
06a2c219 12350#ifndef USE_X_TOOLKIT
d067ea8b 12351 no_read:
06a2c219 12352#endif
0134a210 12353
af31d76f 12354#ifdef PWinGravity
7556890b 12355 size_hints.win_gravity = f->output_data.x->win_gravity;
af31d76f 12356 size_hints.flags |= PWinGravity;
dc05a16b 12357
af31d76f 12358 if (user_position)
6dba1858 12359 {
af31d76f
RS
12360 size_hints.flags &= ~ PPosition;
12361 size_hints.flags |= USPosition;
6dba1858 12362 }
2554751d 12363#endif /* PWinGravity */
6dba1858 12364
b0342f17 12365#ifdef HAVE_X11R4
334208b7 12366 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 12367#else
334208b7 12368 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 12369#endif
dc6f92b8
JB
12370}
12371
12372/* Used for IconicState or NormalState */
06a2c219 12373
dfcf069d 12374void
f676886a
JB
12375x_wm_set_window_state (f, state)
12376 struct frame *f;
dc6f92b8
JB
12377 int state;
12378{
3afe33e7 12379#ifdef USE_X_TOOLKIT
546e6d5b
RS
12380 Arg al[1];
12381
12382 XtSetArg (al[0], XtNinitialState, state);
7556890b 12383 XtSetValues (f->output_data.x->widget, al, 1);
3afe33e7 12384#else /* not USE_X_TOOLKIT */
c118dd06 12385 Window window = FRAME_X_WINDOW (f);
dc6f92b8 12386
7556890b
RS
12387 f->output_data.x->wm_hints.flags |= StateHint;
12388 f->output_data.x->wm_hints.initial_state = state;
b1c884c3 12389
7556890b 12390 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
546e6d5b 12391#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12392}
12393
dfcf069d 12394void
7f2ae036 12395x_wm_set_icon_pixmap (f, pixmap_id)
f676886a 12396 struct frame *f;
7f2ae036 12397 int pixmap_id;
dc6f92b8 12398{
d2bd6bc4
RS
12399 Pixmap icon_pixmap;
12400
06a2c219 12401#ifndef USE_X_TOOLKIT
c118dd06 12402 Window window = FRAME_X_WINDOW (f);
75231bad 12403#endif
dc6f92b8 12404
7f2ae036 12405 if (pixmap_id > 0)
dbc4e1c1 12406 {
d2bd6bc4 12407 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
7556890b 12408 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
dbc4e1c1
JB
12409 }
12410 else
68568555
RS
12411 {
12412 /* It seems there is no way to turn off use of an icon pixmap.
12413 The following line does it, only if no icon has yet been created,
12414 for some window managers. But with mwm it crashes.
12415 Some people say it should clear the IconPixmapHint bit in this case,
12416 but that doesn't work, and the X consortium said it isn't the
12417 right thing at all. Since there is no way to win,
12418 best to explicitly give up. */
12419#if 0
12420 f->output_data.x->wm_hints.icon_pixmap = None;
12421#else
12422 return;
12423#endif
12424 }
b1c884c3 12425
d2bd6bc4
RS
12426#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
12427
12428 {
12429 Arg al[1];
12430 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12431 XtSetValues (f->output_data.x->widget, al, 1);
12432 }
12433
12434#else /* not USE_X_TOOLKIT */
12435
7556890b
RS
12436 f->output_data.x->wm_hints.flags |= IconPixmapHint;
12437 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
d2bd6bc4
RS
12438
12439#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12440}
12441
dfcf069d 12442void
f676886a
JB
12443x_wm_set_icon_position (f, icon_x, icon_y)
12444 struct frame *f;
dc6f92b8
JB
12445 int icon_x, icon_y;
12446{
75231bad 12447#ifdef USE_X_TOOLKIT
7556890b 12448 Window window = XtWindow (f->output_data.x->widget);
75231bad 12449#else
c118dd06 12450 Window window = FRAME_X_WINDOW (f);
75231bad 12451#endif
dc6f92b8 12452
7556890b
RS
12453 f->output_data.x->wm_hints.flags |= IconPositionHint;
12454 f->output_data.x->wm_hints.icon_x = icon_x;
12455 f->output_data.x->wm_hints.icon_y = icon_y;
b1c884c3 12456
7556890b 12457 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
dc6f92b8
JB
12458}
12459
12460\f
06a2c219
GM
12461/***********************************************************************
12462 Fonts
12463 ***********************************************************************/
dc43ef94
KH
12464
12465/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
06a2c219 12466
dc43ef94
KH
12467struct font_info *
12468x_get_font_info (f, font_idx)
12469 FRAME_PTR f;
12470 int font_idx;
12471{
12472 return (FRAME_X_FONT_TABLE (f) + font_idx);
12473}
12474
12475
12476/* Return a list of names of available fonts matching PATTERN on frame
12477 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
12478 to be listed. Frame F NULL means we have not yet created any
12479 frame on X, and consult the first display in x_display_list.
12480 MAXNAMES sets a limit on how many fonts to match. */
12481
12482Lisp_Object
12483x_list_fonts (f, pattern, size, maxnames)
12484 FRAME_PTR f;
12485 Lisp_Object pattern;
12486 int size;
12487 int maxnames;
12488{
06a2c219
GM
12489 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
12490 Lisp_Object tem, second_best;
dc43ef94 12491 Display *dpy = f != NULL ? FRAME_X_DISPLAY (f) : x_display_list->display;
09c6077f 12492 int try_XLoadQueryFont = 0;
53ca4657 12493 int count;
dc43ef94 12494
6b0efe73 12495 patterns = Fassoc (pattern, Valternate_fontname_alist);
2da424f1
KH
12496 if (NILP (patterns))
12497 patterns = Fcons (pattern, Qnil);
81ba44e5 12498
09c6077f
KH
12499 if (maxnames == 1 && !size)
12500 /* We can return any single font matching PATTERN. */
12501 try_XLoadQueryFont = 1;
9a32686f 12502
8e713be6 12503 for (; CONSP (patterns); patterns = XCDR (patterns))
dc43ef94 12504 {
dc43ef94 12505 int num_fonts;
3e71d8f2 12506 char **names = NULL;
dc43ef94 12507
8e713be6 12508 pattern = XCAR (patterns);
536f4067
RS
12509 /* See if we cached the result for this particular query.
12510 The cache is an alist of the form:
12511 (((PATTERN . MAXNAMES) (FONTNAME . WIDTH) ...) ...)
12512 */
8e713be6 12513 if (f && (tem = XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element),
b5210ea7
KH
12514 key = Fcons (pattern, make_number (maxnames)),
12515 !NILP (list = Fassoc (key, tem))))
12516 {
12517 list = Fcdr_safe (list);
12518 /* We have a cashed list. Don't have to get the list again. */
12519 goto label_cached;
12520 }
12521
12522 /* At first, put PATTERN in the cache. */
09c6077f 12523
dc43ef94 12524 BLOCK_INPUT;
17d85edc
KH
12525 count = x_catch_errors (dpy);
12526
09c6077f
KH
12527 if (try_XLoadQueryFont)
12528 {
12529 XFontStruct *font;
12530 unsigned long value;
12531
12532 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
17d85edc
KH
12533 if (x_had_errors_p (dpy))
12534 {
12535 /* This error is perhaps due to insufficient memory on X
12536 server. Let's just ignore it. */
12537 font = NULL;
12538 x_clear_errors (dpy);
12539 }
12540
09c6077f
KH
12541 if (font
12542 && XGetFontProperty (font, XA_FONT, &value))
12543 {
12544 char *name = (char *) XGetAtomName (dpy, (Atom) value);
12545 int len = strlen (name);
01c752b5 12546 char *tmp;
09c6077f 12547
6f6512e8
KH
12548 /* If DXPC (a Differential X Protocol Compressor)
12549 Ver.3.7 is running, XGetAtomName will return null
12550 string. We must avoid such a name. */
12551 if (len == 0)
12552 try_XLoadQueryFont = 0;
12553 else
12554 {
12555 num_fonts = 1;
12556 names = (char **) alloca (sizeof (char *));
12557 /* Some systems only allow alloca assigned to a
12558 simple var. */
12559 tmp = (char *) alloca (len + 1); names[0] = tmp;
12560 bcopy (name, names[0], len + 1);
12561 XFree (name);
12562 }
09c6077f
KH
12563 }
12564 else
12565 try_XLoadQueryFont = 0;
a083fd23
RS
12566
12567 if (font)
12568 XFreeFont (dpy, font);
09c6077f
KH
12569 }
12570
12571 if (!try_XLoadQueryFont)
17d85edc
KH
12572 {
12573 /* We try at least 10 fonts because XListFonts will return
12574 auto-scaled fonts at the head. */
12575 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
12576 &num_fonts);
12577 if (x_had_errors_p (dpy))
12578 {
12579 /* This error is perhaps due to insufficient memory on X
12580 server. Let's just ignore it. */
12581 names = NULL;
12582 x_clear_errors (dpy);
12583 }
12584 }
12585
12586 x_uncatch_errors (dpy, count);
dc43ef94
KH
12587 UNBLOCK_INPUT;
12588
12589 if (names)
12590 {
12591 int i;
dc43ef94
KH
12592
12593 /* Make a list of all the fonts we got back.
12594 Store that in the font cache for the display. */
12595 for (i = 0; i < num_fonts; i++)
12596 {
06a2c219 12597 int width = 0;
dc43ef94 12598 char *p = names[i];
06a2c219
GM
12599 int average_width = -1, dashes = 0;
12600
dc43ef94 12601 /* Count the number of dashes in NAMES[I]. If there are
b5210ea7
KH
12602 14 dashes, and the field value following 12th dash
12603 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
12604 is usually too ugly to be used for editing. Let's
12605 ignore it. */
dc43ef94
KH
12606 while (*p)
12607 if (*p++ == '-')
12608 {
12609 dashes++;
12610 if (dashes == 7) /* PIXEL_SIZE field */
12611 width = atoi (p);
12612 else if (dashes == 12) /* AVERAGE_WIDTH field */
12613 average_width = atoi (p);
12614 }
12615 if (dashes < 14 || average_width != 0)
12616 {
12617 tem = build_string (names[i]);
12618 if (NILP (Fassoc (tem, list)))
12619 {
12620 if (STRINGP (Vx_pixel_size_width_font_regexp)
f39db7ea
RS
12621 && ((fast_c_string_match_ignore_case
12622 (Vx_pixel_size_width_font_regexp, names[i]))
dc43ef94
KH
12623 >= 0))
12624 /* We can set the value of PIXEL_SIZE to the
b5210ea7 12625 width of this font. */
dc43ef94
KH
12626 list = Fcons (Fcons (tem, make_number (width)), list);
12627 else
12628 /* For the moment, width is not known. */
12629 list = Fcons (Fcons (tem, Qnil), list);
12630 }
12631 }
12632 }
09c6077f
KH
12633 if (!try_XLoadQueryFont)
12634 XFreeFontNames (names);
dc43ef94
KH
12635 }
12636
b5210ea7 12637 /* Now store the result in the cache. */
dc43ef94 12638 if (f != NULL)
8e713be6 12639 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element)
dc43ef94 12640 = Fcons (Fcons (key, list),
8e713be6 12641 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
dc43ef94 12642
b5210ea7
KH
12643 label_cached:
12644 if (NILP (list)) continue; /* Try the remaining alternatives. */
dc43ef94 12645
b5210ea7
KH
12646 newlist = second_best = Qnil;
12647 /* Make a list of the fonts that have the right width. */
8e713be6 12648 for (; CONSP (list); list = XCDR (list))
b5210ea7 12649 {
536f4067
RS
12650 int found_size;
12651
8e713be6 12652 tem = XCAR (list);
dc43ef94 12653
8e713be6 12654 if (!CONSP (tem) || NILP (XCAR (tem)))
b5210ea7
KH
12655 continue;
12656 if (!size)
12657 {
8e713be6 12658 newlist = Fcons (XCAR (tem), newlist);
b5210ea7
KH
12659 continue;
12660 }
dc43ef94 12661
8e713be6 12662 if (!INTEGERP (XCDR (tem)))
dc43ef94 12663 {
b5210ea7
KH
12664 /* Since we have not yet known the size of this font, we
12665 must try slow function call XLoadQueryFont. */
dc43ef94
KH
12666 XFontStruct *thisinfo;
12667
12668 BLOCK_INPUT;
17d85edc 12669 count = x_catch_errors (dpy);
dc43ef94 12670 thisinfo = XLoadQueryFont (dpy,
8e713be6 12671 XSTRING (XCAR (tem))->data);
17d85edc
KH
12672 if (x_had_errors_p (dpy))
12673 {
12674 /* This error is perhaps due to insufficient memory on X
12675 server. Let's just ignore it. */
12676 thisinfo = NULL;
12677 x_clear_errors (dpy);
12678 }
12679 x_uncatch_errors (dpy, count);
dc43ef94
KH
12680 UNBLOCK_INPUT;
12681
12682 if (thisinfo)
12683 {
8e713be6 12684 XCDR (tem)
536f4067
RS
12685 = (thisinfo->min_bounds.width == 0
12686 ? make_number (0)
12687 : make_number (thisinfo->max_bounds.width));
dc43ef94
KH
12688 XFreeFont (dpy, thisinfo);
12689 }
12690 else
b5210ea7 12691 /* For unknown reason, the previous call of XListFont had
06a2c219 12692 returned a font which can't be opened. Record the size
b5210ea7 12693 as 0 not to try to open it again. */
8e713be6 12694 XCDR (tem) = make_number (0);
dc43ef94 12695 }
536f4067 12696
8e713be6 12697 found_size = XINT (XCDR (tem));
536f4067 12698 if (found_size == size)
8e713be6 12699 newlist = Fcons (XCAR (tem), newlist);
536f4067 12700 else if (found_size > 0)
b5210ea7 12701 {
536f4067 12702 if (NILP (second_best))
b5210ea7 12703 second_best = tem;
536f4067
RS
12704 else if (found_size < size)
12705 {
8e713be6
KR
12706 if (XINT (XCDR (second_best)) > size
12707 || XINT (XCDR (second_best)) < found_size)
536f4067
RS
12708 second_best = tem;
12709 }
12710 else
12711 {
8e713be6
KR
12712 if (XINT (XCDR (second_best)) > size
12713 && XINT (XCDR (second_best)) > found_size)
536f4067
RS
12714 second_best = tem;
12715 }
b5210ea7
KH
12716 }
12717 }
12718 if (!NILP (newlist))
12719 break;
12720 else if (!NILP (second_best))
12721 {
8e713be6 12722 newlist = Fcons (XCAR (second_best), Qnil);
b5210ea7 12723 break;
dc43ef94 12724 }
dc43ef94
KH
12725 }
12726
12727 return newlist;
12728}
12729
06a2c219
GM
12730
12731#if GLYPH_DEBUG
12732
12733/* Check that FONT is valid on frame F. It is if it can be found in F's
12734 font table. */
12735
12736static void
12737x_check_font (f, font)
12738 struct frame *f;
12739 XFontStruct *font;
12740{
12741 int i;
12742 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12743
12744 xassert (font != NULL);
12745
12746 for (i = 0; i < dpyinfo->n_fonts; i++)
12747 if (dpyinfo->font_table[i].name
12748 && font == dpyinfo->font_table[i].font)
12749 break;
12750
12751 xassert (i < dpyinfo->n_fonts);
12752}
12753
12754#endif /* GLYPH_DEBUG != 0 */
12755
12756/* Set *W to the minimum width, *H to the minimum font height of FONT.
12757 Note: There are (broken) X fonts out there with invalid XFontStruct
12758 min_bounds contents. For example, handa@etl.go.jp reports that
12759 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
12760 have font->min_bounds.width == 0. */
12761
12762static INLINE void
12763x_font_min_bounds (font, w, h)
12764 XFontStruct *font;
12765 int *w, *h;
12766{
12767 *h = FONT_HEIGHT (font);
12768 *w = font->min_bounds.width;
12769
12770 /* Try to handle the case where FONT->min_bounds has invalid
12771 contents. Since the only font known to have invalid min_bounds
12772 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
12773 if (*w <= 0)
12774 *w = font->max_bounds.width;
12775}
12776
12777
12778/* Compute the smallest character width and smallest font height over
12779 all fonts available on frame F. Set the members smallest_char_width
12780 and smallest_font_height in F's x_display_info structure to
12781 the values computed. Value is non-zero if smallest_font_height or
12782 smallest_char_width become smaller than they were before. */
12783
12784static int
12785x_compute_min_glyph_bounds (f)
12786 struct frame *f;
12787{
12788 int i;
12789 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12790 XFontStruct *font;
12791 int old_width = dpyinfo->smallest_char_width;
12792 int old_height = dpyinfo->smallest_font_height;
12793
12794 dpyinfo->smallest_font_height = 100000;
12795 dpyinfo->smallest_char_width = 100000;
12796
12797 for (i = 0; i < dpyinfo->n_fonts; ++i)
12798 if (dpyinfo->font_table[i].name)
12799 {
12800 struct font_info *fontp = dpyinfo->font_table + i;
12801 int w, h;
12802
12803 font = (XFontStruct *) fontp->font;
12804 xassert (font != (XFontStruct *) ~0);
12805 x_font_min_bounds (font, &w, &h);
12806
12807 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
12808 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
12809 }
12810
12811 xassert (dpyinfo->smallest_char_width > 0
12812 && dpyinfo->smallest_font_height > 0);
12813
12814 return (dpyinfo->n_fonts == 1
12815 || dpyinfo->smallest_char_width < old_width
12816 || dpyinfo->smallest_font_height < old_height);
12817}
12818
12819
dc43ef94
KH
12820/* Load font named FONTNAME of the size SIZE for frame F, and return a
12821 pointer to the structure font_info while allocating it dynamically.
12822 If SIZE is 0, load any size of font.
12823 If loading is failed, return NULL. */
12824
12825struct font_info *
12826x_load_font (f, fontname, size)
12827 struct frame *f;
12828 register char *fontname;
12829 int size;
12830{
12831 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12832 Lisp_Object font_names;
d645aaa4 12833 int count;
dc43ef94
KH
12834
12835 /* Get a list of all the fonts that match this name. Once we
12836 have a list of matching fonts, we compare them against the fonts
12837 we already have by comparing names. */
09c6077f 12838 font_names = x_list_fonts (f, build_string (fontname), size, 1);
dc43ef94
KH
12839
12840 if (!NILP (font_names))
12841 {
12842 Lisp_Object tail;
12843 int i;
12844
12845 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 12846 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
06a2c219
GM
12847 if (dpyinfo->font_table[i].name
12848 && (!strcmp (dpyinfo->font_table[i].name,
8e713be6 12849 XSTRING (XCAR (tail))->data)
06a2c219 12850 || !strcmp (dpyinfo->font_table[i].full_name,
8e713be6 12851 XSTRING (XCAR (tail))->data)))
dc43ef94
KH
12852 return (dpyinfo->font_table + i);
12853 }
12854
12855 /* Load the font and add it to the table. */
12856 {
12857 char *full_name;
12858 XFontStruct *font;
12859 struct font_info *fontp;
12860 unsigned long value;
06a2c219 12861 int i;
dc43ef94 12862
2da424f1
KH
12863 /* If we have found fonts by x_list_font, load one of them. If
12864 not, we still try to load a font by the name given as FONTNAME
12865 because XListFonts (called in x_list_font) of some X server has
12866 a bug of not finding a font even if the font surely exists and
12867 is loadable by XLoadQueryFont. */
e1d6d5b9 12868 if (size > 0 && !NILP (font_names))
8e713be6 12869 fontname = (char *) XSTRING (XCAR (font_names))->data;
dc43ef94
KH
12870
12871 BLOCK_INPUT;
d645aaa4 12872 count = x_catch_errors (FRAME_X_DISPLAY (f));
dc43ef94 12873 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
d645aaa4
KH
12874 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12875 {
12876 /* This error is perhaps due to insufficient memory on X
12877 server. Let's just ignore it. */
12878 font = NULL;
12879 x_clear_errors (FRAME_X_DISPLAY (f));
12880 }
12881 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
dc43ef94 12882 UNBLOCK_INPUT;
b5210ea7 12883 if (!font)
dc43ef94
KH
12884 return NULL;
12885
06a2c219
GM
12886 /* Find a free slot in the font table. */
12887 for (i = 0; i < dpyinfo->n_fonts; ++i)
12888 if (dpyinfo->font_table[i].name == NULL)
12889 break;
12890
12891 /* If no free slot found, maybe enlarge the font table. */
12892 if (i == dpyinfo->n_fonts
12893 && dpyinfo->n_fonts == dpyinfo->font_table_size)
dc43ef94 12894 {
06a2c219
GM
12895 int sz;
12896 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
12897 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
dc43ef94 12898 dpyinfo->font_table
06a2c219 12899 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
dc43ef94
KH
12900 }
12901
06a2c219
GM
12902 fontp = dpyinfo->font_table + i;
12903 if (i == dpyinfo->n_fonts)
12904 ++dpyinfo->n_fonts;
dc43ef94
KH
12905
12906 /* Now fill in the slots of *FONTP. */
12907 BLOCK_INPUT;
12908 fontp->font = font;
06a2c219 12909 fontp->font_idx = i;
dc43ef94
KH
12910 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
12911 bcopy (fontname, fontp->name, strlen (fontname) + 1);
12912
12913 /* Try to get the full name of FONT. Put it in FULL_NAME. */
12914 full_name = 0;
12915 if (XGetFontProperty (font, XA_FONT, &value))
12916 {
12917 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
12918 char *p = name;
12919 int dashes = 0;
12920
12921 /* Count the number of dashes in the "full name".
12922 If it is too few, this isn't really the font's full name,
12923 so don't use it.
12924 In X11R4, the fonts did not come with their canonical names
12925 stored in them. */
12926 while (*p)
12927 {
12928 if (*p == '-')
12929 dashes++;
12930 p++;
12931 }
12932
12933 if (dashes >= 13)
12934 {
12935 full_name = (char *) xmalloc (p - name + 1);
12936 bcopy (name, full_name, p - name + 1);
12937 }
12938
12939 XFree (name);
12940 }
12941
12942 if (full_name != 0)
12943 fontp->full_name = full_name;
12944 else
12945 fontp->full_name = fontp->name;
12946
12947 fontp->size = font->max_bounds.width;
d5749adb
KH
12948 fontp->height = FONT_HEIGHT (font);
12949 {
12950 /* For some font, ascent and descent in max_bounds field is
12951 larger than the above value. */
12952 int max_height = font->max_bounds.ascent + font->max_bounds.descent;
12953 if (max_height > fontp->height)
74848a96 12954 fontp->height = max_height;
d5749adb 12955 }
dc43ef94 12956
2da424f1
KH
12957 if (NILP (font_names))
12958 {
12959 /* We come here because of a bug of XListFonts mentioned at
12960 the head of this block. Let's store this information in
12961 the cache for x_list_fonts. */
12962 Lisp_Object lispy_name = build_string (fontname);
12963 Lisp_Object lispy_full_name = build_string (fontp->full_name);
12964
8e713be6 12965 XCDR (dpyinfo->name_list_element)
2da424f1
KH
12966 = Fcons (Fcons (Fcons (lispy_name, make_number (256)),
12967 Fcons (Fcons (lispy_full_name,
12968 make_number (fontp->size)),
12969 Qnil)),
8e713be6 12970 XCDR (dpyinfo->name_list_element));
2da424f1 12971 if (full_name)
8e713be6 12972 XCDR (dpyinfo->name_list_element)
2da424f1
KH
12973 = Fcons (Fcons (Fcons (lispy_full_name, make_number (256)),
12974 Fcons (Fcons (lispy_full_name,
12975 make_number (fontp->size)),
12976 Qnil)),
8e713be6 12977 XCDR (dpyinfo->name_list_element));
2da424f1
KH
12978 }
12979
dc43ef94
KH
12980 /* The slot `encoding' specifies how to map a character
12981 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
ee569018
KH
12982 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
12983 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
8ff102bd 12984 2:0xA020..0xFF7F). For the moment, we don't know which charset
06a2c219 12985 uses this font. So, we set information in fontp->encoding[1]
8ff102bd
RS
12986 which is never used by any charset. If mapping can't be
12987 decided, set FONT_ENCODING_NOT_DECIDED. */
dc43ef94
KH
12988 fontp->encoding[1]
12989 = (font->max_byte1 == 0
12990 /* 1-byte font */
12991 ? (font->min_char_or_byte2 < 0x80
12992 ? (font->max_char_or_byte2 < 0x80
12993 ? 0 /* 0x20..0x7F */
8ff102bd 12994 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
dc43ef94
KH
12995 : 1) /* 0xA0..0xFF */
12996 /* 2-byte font */
12997 : (font->min_byte1 < 0x80
12998 ? (font->max_byte1 < 0x80
12999 ? (font->min_char_or_byte2 < 0x80
13000 ? (font->max_char_or_byte2 < 0x80
13001 ? 0 /* 0x2020..0x7F7F */
8ff102bd 13002 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
dc43ef94 13003 : 3) /* 0x20A0..0x7FFF */
8ff102bd 13004 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
dc43ef94
KH
13005 : (font->min_char_or_byte2 < 0x80
13006 ? (font->max_char_or_byte2 < 0x80
13007 ? 2 /* 0xA020..0xFF7F */
8ff102bd 13008 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
dc43ef94
KH
13009 : 1))); /* 0xA0A0..0xFFFF */
13010
13011 fontp->baseline_offset
13012 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
13013 ? (long) value : 0);
13014 fontp->relative_compose
13015 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
13016 ? (long) value : 0);
f78798df
KH
13017 fontp->default_ascent
13018 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
13019 ? (long) value : 0);
dc43ef94 13020
06a2c219
GM
13021 /* Set global flag fonts_changed_p to non-zero if the font loaded
13022 has a character with a smaller width than any other character
13023 before, or if the font loaded has a smalle>r height than any
13024 other font loaded before. If this happens, it will make a
13025 glyph matrix reallocation necessary. */
13026 fonts_changed_p = x_compute_min_glyph_bounds (f);
dc43ef94 13027 UNBLOCK_INPUT;
dc43ef94
KH
13028 return fontp;
13029 }
13030}
13031
06a2c219
GM
13032
13033/* Return a pointer to struct font_info of a font named FONTNAME for
13034 frame F. If no such font is loaded, return NULL. */
13035
dc43ef94
KH
13036struct font_info *
13037x_query_font (f, fontname)
13038 struct frame *f;
13039 register char *fontname;
13040{
13041 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13042 int i;
13043
13044 for (i = 0; i < dpyinfo->n_fonts; i++)
06a2c219
GM
13045 if (dpyinfo->font_table[i].name
13046 && (!strcmp (dpyinfo->font_table[i].name, fontname)
13047 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
dc43ef94
KH
13048 return (dpyinfo->font_table + i);
13049 return NULL;
13050}
13051
06a2c219
GM
13052
13053/* Find a CCL program for a font specified by FONTP, and set the member
a6582676
KH
13054 `encoder' of the structure. */
13055
13056void
13057x_find_ccl_program (fontp)
13058 struct font_info *fontp;
13059{
a42f54e6 13060 Lisp_Object list, elt;
a6582676 13061
8e713be6 13062 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
a6582676 13063 {
8e713be6 13064 elt = XCAR (list);
a6582676 13065 if (CONSP (elt)
8e713be6
KR
13066 && STRINGP (XCAR (elt))
13067 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
a6582676 13068 >= 0))
a42f54e6
KH
13069 break;
13070 }
13071 if (! NILP (list))
13072 {
d27f8ca7
KH
13073 struct ccl_program *ccl
13074 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
a42f54e6 13075
8e713be6 13076 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
a42f54e6
KH
13077 xfree (ccl);
13078 else
13079 fontp->font_encoder = ccl;
a6582676
KH
13080 }
13081}
13082
06a2c219 13083
dc43ef94 13084\f
06a2c219
GM
13085/***********************************************************************
13086 Initialization
13087 ***********************************************************************/
f451eb13 13088
3afe33e7
RS
13089#ifdef USE_X_TOOLKIT
13090static XrmOptionDescRec emacs_options[] = {
13091 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
13092 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
13093
13094 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
13095 XrmoptionSepArg, NULL},
13096 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
13097
13098 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13099 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13100 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13101 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13102 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13103 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
13104 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
13105};
13106#endif /* USE_X_TOOLKIT */
13107
7a13e894
RS
13108static int x_initialized;
13109
29b38361
KH
13110#ifdef MULTI_KBOARD
13111/* Test whether two display-name strings agree up to the dot that separates
13112 the screen number from the server number. */
13113static int
13114same_x_server (name1, name2)
13115 char *name1, *name2;
13116{
13117 int seen_colon = 0;
cf591cc1
RS
13118 unsigned char *system_name = XSTRING (Vsystem_name)->data;
13119 int system_name_length = strlen (system_name);
13120 int length_until_period = 0;
13121
13122 while (system_name[length_until_period] != 0
13123 && system_name[length_until_period] != '.')
13124 length_until_period++;
13125
13126 /* Treat `unix' like an empty host name. */
13127 if (! strncmp (name1, "unix:", 5))
13128 name1 += 4;
13129 if (! strncmp (name2, "unix:", 5))
13130 name2 += 4;
13131 /* Treat this host's name like an empty host name. */
13132 if (! strncmp (name1, system_name, system_name_length)
13133 && name1[system_name_length] == ':')
13134 name1 += system_name_length;
13135 if (! strncmp (name2, system_name, system_name_length)
13136 && name2[system_name_length] == ':')
13137 name2 += system_name_length;
13138 /* Treat this host's domainless name like an empty host name. */
13139 if (! strncmp (name1, system_name, length_until_period)
13140 && name1[length_until_period] == ':')
13141 name1 += length_until_period;
13142 if (! strncmp (name2, system_name, length_until_period)
13143 && name2[length_until_period] == ':')
13144 name2 += length_until_period;
13145
29b38361
KH
13146 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
13147 {
13148 if (*name1 == ':')
13149 seen_colon++;
13150 if (seen_colon && *name1 == '.')
13151 return 1;
13152 }
13153 return (seen_colon
13154 && (*name1 == '.' || *name1 == '\0')
13155 && (*name2 == '.' || *name2 == '\0'));
13156}
13157#endif
13158
334208b7 13159struct x_display_info *
1f8255f2 13160x_term_init (display_name, xrm_option, resource_name)
334208b7 13161 Lisp_Object display_name;
1f8255f2
RS
13162 char *xrm_option;
13163 char *resource_name;
dc6f92b8 13164{
334208b7 13165 int connection;
7a13e894 13166 Display *dpy;
334208b7
RS
13167 struct x_display_info *dpyinfo;
13168 XrmDatabase xrdb;
13169
60439948
KH
13170 BLOCK_INPUT;
13171
7a13e894
RS
13172 if (!x_initialized)
13173 {
13174 x_initialize ();
13175 x_initialized = 1;
13176 }
dc6f92b8 13177
3afe33e7 13178#ifdef USE_X_TOOLKIT
2d7fc7e8
RS
13179 /* weiner@footloose.sps.mot.com reports that this causes
13180 errors with X11R5:
13181 X protocol error: BadAtom (invalid Atom parameter)
13182 on protocol request 18skiloaf.
13183 So let's not use it until R6. */
13184#ifdef HAVE_X11XTR6
bdcd49ba
RS
13185 XtSetLanguageProc (NULL, NULL, NULL);
13186#endif
13187
7f9c7f94
RS
13188 {
13189 int argc = 0;
13190 char *argv[3];
13191
13192 argv[0] = "";
13193 argc = 1;
13194 if (xrm_option)
13195 {
13196 argv[argc++] = "-xrm";
13197 argv[argc++] = xrm_option;
13198 }
13199 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
13200 resource_name, EMACS_CLASS,
13201 emacs_options, XtNumber (emacs_options),
13202 &argc, argv);
39d8bb4d
KH
13203
13204#ifdef HAVE_X11XTR6
10537cb1 13205 /* I think this is to compensate for XtSetLanguageProc. */
71f8198a 13206 fixup_locale ();
39d8bb4d 13207#endif
7f9c7f94 13208 }
3afe33e7
RS
13209
13210#else /* not USE_X_TOOLKIT */
bdcd49ba
RS
13211#ifdef HAVE_X11R5
13212 XSetLocaleModifiers ("");
13213#endif
7a13e894 13214 dpy = XOpenDisplay (XSTRING (display_name)->data);
3afe33e7 13215#endif /* not USE_X_TOOLKIT */
334208b7 13216
7a13e894
RS
13217 /* Detect failure. */
13218 if (dpy == 0)
60439948
KH
13219 {
13220 UNBLOCK_INPUT;
13221 return 0;
13222 }
7a13e894
RS
13223
13224 /* We have definitely succeeded. Record the new connection. */
13225
13226 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
13227
29b38361
KH
13228#ifdef MULTI_KBOARD
13229 {
13230 struct x_display_info *share;
13231 Lisp_Object tail;
13232
13233 for (share = x_display_list, tail = x_display_name_list; share;
8e713be6
KR
13234 share = share->next, tail = XCDR (tail))
13235 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
29b38361
KH
13236 XSTRING (display_name)->data))
13237 break;
13238 if (share)
13239 dpyinfo->kboard = share->kboard;
13240 else
13241 {
13242 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
13243 init_kboard (dpyinfo->kboard);
59e755be
KH
13244 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
13245 {
13246 char *vendor = ServerVendor (dpy);
9b6ed9f3 13247 UNBLOCK_INPUT;
59e755be
KH
13248 dpyinfo->kboard->Vsystem_key_alist
13249 = call1 (Qvendor_specific_keysyms,
13250 build_string (vendor ? vendor : ""));
9b6ed9f3 13251 BLOCK_INPUT;
59e755be
KH
13252 }
13253
29b38361
KH
13254 dpyinfo->kboard->next_kboard = all_kboards;
13255 all_kboards = dpyinfo->kboard;
0ad5446c
KH
13256 /* Don't let the initial kboard remain current longer than necessary.
13257 That would cause problems if a file loaded on startup tries to
06a2c219 13258 prompt in the mini-buffer. */
0ad5446c
KH
13259 if (current_kboard == initial_kboard)
13260 current_kboard = dpyinfo->kboard;
29b38361
KH
13261 }
13262 dpyinfo->kboard->reference_count++;
13263 }
b9737ad3
KH
13264#endif
13265
7a13e894
RS
13266 /* Put this display on the chain. */
13267 dpyinfo->next = x_display_list;
13268 x_display_list = dpyinfo;
13269
13270 /* Put it on x_display_name_list as well, to keep them parallel. */
13271 x_display_name_list = Fcons (Fcons (display_name, Qnil),
13272 x_display_name_list);
8e713be6 13273 dpyinfo->name_list_element = XCAR (x_display_name_list);
7a13e894
RS
13274
13275 dpyinfo->display = dpy;
dc6f92b8 13276
dc6f92b8 13277#if 0
7a13e894 13278 XSetAfterFunction (x_current_display, x_trace_wire);
c118dd06 13279#endif /* ! 0 */
7a13e894
RS
13280
13281 dpyinfo->x_id_name
fc932ac6
RS
13282 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
13283 + STRING_BYTES (XSTRING (Vsystem_name))
7a13e894
RS
13284 + 2);
13285 sprintf (dpyinfo->x_id_name, "%s@%s",
13286 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
28430d3c
JB
13287
13288 /* Figure out which modifier bits mean what. */
334208b7 13289 x_find_modifier_meanings (dpyinfo);
f451eb13 13290
ab648270 13291 /* Get the scroll bar cursor. */
7a13e894 13292 dpyinfo->vertical_scroll_bar_cursor
334208b7 13293 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
f451eb13 13294
334208b7
RS
13295 xrdb = x_load_resources (dpyinfo->display, xrm_option,
13296 resource_name, EMACS_CLASS);
13297#ifdef HAVE_XRMSETDATABASE
13298 XrmSetDatabase (dpyinfo->display, xrdb);
13299#else
13300 dpyinfo->display->db = xrdb;
13301#endif
547d9db8 13302 /* Put the rdb where we can find it in a way that works on
7a13e894
RS
13303 all versions. */
13304 dpyinfo->xrdb = xrdb;
334208b7
RS
13305
13306 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
13307 DefaultScreen (dpyinfo->display));
5ff67d81 13308 select_visual (dpyinfo);
43bd1b2b 13309 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
334208b7
RS
13310 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
13311 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
13312 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
13313 dpyinfo->grabbed = 0;
13314 dpyinfo->reference_count = 0;
13315 dpyinfo->icon_bitmap_id = -1;
06a2c219 13316 dpyinfo->font_table = NULL;
7a13e894
RS
13317 dpyinfo->n_fonts = 0;
13318 dpyinfo->font_table_size = 0;
13319 dpyinfo->bitmaps = 0;
13320 dpyinfo->bitmaps_size = 0;
13321 dpyinfo->bitmaps_last = 0;
13322 dpyinfo->scratch_cursor_gc = 0;
13323 dpyinfo->mouse_face_mouse_frame = 0;
13324 dpyinfo->mouse_face_deferred_gc = 0;
13325 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
13326 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
06a2c219 13327 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
7a13e894
RS
13328 dpyinfo->mouse_face_window = Qnil;
13329 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
13330 dpyinfo->mouse_face_defer = 0;
0f941935
KH
13331 dpyinfo->x_focus_frame = 0;
13332 dpyinfo->x_focus_event_frame = 0;
13333 dpyinfo->x_highlight_frame = 0;
06a2c219 13334 dpyinfo->image_cache = make_image_cache ();
334208b7 13335
43bd1b2b 13336 /* See if a private colormap is requested. */
5ff67d81
GM
13337 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
13338 {
13339 if (dpyinfo->visual->class == PseudoColor)
13340 {
13341 Lisp_Object value;
13342 value = display_x_get_resource (dpyinfo,
13343 build_string ("privateColormap"),
13344 build_string ("PrivateColormap"),
13345 Qnil, Qnil);
13346 if (STRINGP (value)
13347 && (!strcmp (XSTRING (value)->data, "true")
13348 || !strcmp (XSTRING (value)->data, "on")))
13349 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
13350 }
43bd1b2b 13351 }
5ff67d81
GM
13352 else
13353 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
13354 dpyinfo->visual, AllocNone);
43bd1b2b 13355
06a2c219
GM
13356 {
13357 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
13358 double pixels = DisplayHeight (dpyinfo->display, screen_number);
13359 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
13360 dpyinfo->resy = pixels * 25.4 / mm;
13361 pixels = DisplayWidth (dpyinfo->display, screen_number);
13362 mm = DisplayWidthMM (dpyinfo->display, screen_number);
13363 dpyinfo->resx = pixels * 25.4 / mm;
13364 }
13365
334208b7
RS
13366 dpyinfo->Xatom_wm_protocols
13367 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
13368 dpyinfo->Xatom_wm_take_focus
13369 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
13370 dpyinfo->Xatom_wm_save_yourself
13371 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
13372 dpyinfo->Xatom_wm_delete_window
13373 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
13374 dpyinfo->Xatom_wm_change_state
13375 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
13376 dpyinfo->Xatom_wm_configure_denied
13377 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
13378 dpyinfo->Xatom_wm_window_moved
13379 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
13380 dpyinfo->Xatom_editres
13381 = XInternAtom (dpyinfo->display, "Editres", False);
13382 dpyinfo->Xatom_CLIPBOARD
13383 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
13384 dpyinfo->Xatom_TIMESTAMP
13385 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
13386 dpyinfo->Xatom_TEXT
13387 = XInternAtom (dpyinfo->display, "TEXT", False);
dc43ef94
KH
13388 dpyinfo->Xatom_COMPOUND_TEXT
13389 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
334208b7
RS
13390 dpyinfo->Xatom_DELETE
13391 = XInternAtom (dpyinfo->display, "DELETE", False);
13392 dpyinfo->Xatom_MULTIPLE
13393 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
13394 dpyinfo->Xatom_INCR
13395 = XInternAtom (dpyinfo->display, "INCR", False);
13396 dpyinfo->Xatom_EMACS_TMP
13397 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
13398 dpyinfo->Xatom_TARGETS
13399 = XInternAtom (dpyinfo->display, "TARGETS", False);
13400 dpyinfo->Xatom_NULL
13401 = XInternAtom (dpyinfo->display, "NULL", False);
13402 dpyinfo->Xatom_ATOM_PAIR
13403 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
dc43ef94
KH
13404 /* For properties of font. */
13405 dpyinfo->Xatom_PIXEL_SIZE
13406 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
13407 dpyinfo->Xatom_MULE_BASELINE_OFFSET
13408 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
13409 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
13410 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
f78798df
KH
13411 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
13412 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
334208b7 13413
06a2c219
GM
13414 /* Ghostscript support. */
13415 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
13416 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
13417
13418 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
13419 False);
13420
547d9db8
KH
13421 dpyinfo->cut_buffers_initialized = 0;
13422
334208b7
RS
13423 connection = ConnectionNumber (dpyinfo->display);
13424 dpyinfo->connection = connection;
13425
dc43ef94 13426 {
5d7cc324
RS
13427 char null_bits[1];
13428
13429 null_bits[0] = 0x00;
dc43ef94
KH
13430
13431 dpyinfo->null_pixel
13432 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13433 null_bits, 1, 1, (long) 0, (long) 0,
13434 1);
13435 }
13436
06a2c219
GM
13437 {
13438 extern int gray_bitmap_width, gray_bitmap_height;
13439 extern unsigned char *gray_bitmap_bits;
13440 dpyinfo->gray
13441 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13442 gray_bitmap_bits,
13443 gray_bitmap_width, gray_bitmap_height,
13444 (unsigned long) 1, (unsigned long) 0, 1);
13445 }
13446
f5d11644
GM
13447#ifdef HAVE_X_I18N
13448 xim_initialize (dpyinfo, resource_name);
13449#endif
13450
87485d6f
MW
13451#ifdef subprocesses
13452 /* This is only needed for distinguishing keyboard and process input. */
334208b7 13453 if (connection != 0)
7a13e894 13454 add_keyboard_wait_descriptor (connection);
87485d6f 13455#endif
6d4238f3 13456
041b69ac 13457#ifndef F_SETOWN_BUG
dc6f92b8 13458#ifdef F_SETOWN
dc6f92b8 13459#ifdef F_SETOWN_SOCK_NEG
61c3ce62 13460 /* stdin is a socket here */
334208b7 13461 fcntl (connection, F_SETOWN, -getpid ());
c118dd06 13462#else /* ! defined (F_SETOWN_SOCK_NEG) */
334208b7 13463 fcntl (connection, F_SETOWN, getpid ());
c118dd06
JB
13464#endif /* ! defined (F_SETOWN_SOCK_NEG) */
13465#endif /* ! defined (F_SETOWN) */
041b69ac 13466#endif /* F_SETOWN_BUG */
dc6f92b8
JB
13467
13468#ifdef SIGIO
eee20f6a
KH
13469 if (interrupt_input)
13470 init_sigio (connection);
c118dd06 13471#endif /* ! defined (SIGIO) */
dc6f92b8 13472
51b592fb 13473#ifdef USE_LUCID
f8c39f51 13474#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
51b592fb
RS
13475 /* Make sure that we have a valid font for dialog boxes
13476 so that Xt does not crash. */
13477 {
13478 Display *dpy = dpyinfo->display;
13479 XrmValue d, fr, to;
13480 Font font;
e99db5a1 13481 int count;
51b592fb
RS
13482
13483 d.addr = (XPointer)&dpy;
13484 d.size = sizeof (Display *);
13485 fr.addr = XtDefaultFont;
13486 fr.size = sizeof (XtDefaultFont);
13487 to.size = sizeof (Font *);
13488 to.addr = (XPointer)&font;
e99db5a1 13489 count = x_catch_errors (dpy);
51b592fb
RS
13490 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
13491 abort ();
13492 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
13493 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
e99db5a1 13494 x_uncatch_errors (dpy, count);
51b592fb
RS
13495 }
13496#endif
f8c39f51 13497#endif
51b592fb 13498
34e23e5a
GM
13499 /* See if we should run in synchronous mode. This is useful
13500 for debugging X code. */
13501 {
13502 Lisp_Object value;
13503 value = display_x_get_resource (dpyinfo,
13504 build_string ("synchronous"),
13505 build_string ("Synchronous"),
13506 Qnil, Qnil);
13507 if (STRINGP (value)
13508 && (!strcmp (XSTRING (value)->data, "true")
13509 || !strcmp (XSTRING (value)->data, "on")))
13510 XSynchronize (dpyinfo->display, True);
13511 }
13512
60439948
KH
13513 UNBLOCK_INPUT;
13514
7a13e894
RS
13515 return dpyinfo;
13516}
13517\f
13518/* Get rid of display DPYINFO, assuming all frames are already gone,
13519 and without sending any more commands to the X server. */
dc6f92b8 13520
7a13e894
RS
13521void
13522x_delete_display (dpyinfo)
13523 struct x_display_info *dpyinfo;
13524{
13525 delete_keyboard_wait_descriptor (dpyinfo->connection);
13526
13527 /* Discard this display from x_display_name_list and x_display_list.
13528 We can't use Fdelq because that can quit. */
13529 if (! NILP (x_display_name_list)
8e713be6
KR
13530 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
13531 x_display_name_list = XCDR (x_display_name_list);
7a13e894
RS
13532 else
13533 {
13534 Lisp_Object tail;
13535
13536 tail = x_display_name_list;
8e713be6 13537 while (CONSP (tail) && CONSP (XCDR (tail)))
7a13e894 13538 {
bffcfca9 13539 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
7a13e894 13540 {
8e713be6 13541 XCDR (tail) = XCDR (XCDR (tail));
7a13e894
RS
13542 break;
13543 }
8e713be6 13544 tail = XCDR (tail);
7a13e894
RS
13545 }
13546 }
13547
9bda743f
GM
13548 if (next_noop_dpyinfo == dpyinfo)
13549 next_noop_dpyinfo = dpyinfo->next;
13550
7a13e894
RS
13551 if (x_display_list == dpyinfo)
13552 x_display_list = dpyinfo->next;
7f9c7f94
RS
13553 else
13554 {
13555 struct x_display_info *tail;
7a13e894 13556
7f9c7f94
RS
13557 for (tail = x_display_list; tail; tail = tail->next)
13558 if (tail->next == dpyinfo)
13559 tail->next = tail->next->next;
13560 }
7a13e894 13561
0d777288
RS
13562#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
13563#ifndef AIX /* On AIX, XCloseDisplay calls this. */
7f9c7f94
RS
13564 XrmDestroyDatabase (dpyinfo->xrdb);
13565#endif
0d777288 13566#endif
29b38361
KH
13567#ifdef MULTI_KBOARD
13568 if (--dpyinfo->kboard->reference_count == 0)
39f79001 13569 delete_kboard (dpyinfo->kboard);
b9737ad3 13570#endif
f5d11644
GM
13571#ifdef HAVE_X_I18N
13572 if (dpyinfo->xim)
13573 xim_close_dpy (dpyinfo);
13574#endif
13575
b9737ad3
KH
13576 xfree (dpyinfo->font_table);
13577 xfree (dpyinfo->x_id_name);
13578 xfree (dpyinfo);
7a13e894
RS
13579}
13580\f
13581/* Set up use of X before we make the first connection. */
13582
06a2c219
GM
13583static struct redisplay_interface x_redisplay_interface =
13584{
13585 x_produce_glyphs,
13586 x_write_glyphs,
13587 x_insert_glyphs,
13588 x_clear_end_of_line,
13589 x_scroll_run,
13590 x_after_update_window_line,
13591 x_update_window_begin,
13592 x_update_window_end,
13593 XTcursor_to,
13594 x_flush,
71b8321e 13595 x_clear_mouse_face,
66ac4b0e
GM
13596 x_get_glyph_overhangs,
13597 x_fix_overlapping_area
06a2c219
GM
13598};
13599
dfcf069d 13600void
7a13e894
RS
13601x_initialize ()
13602{
06a2c219
GM
13603 rif = &x_redisplay_interface;
13604
13605 clear_frame_hook = x_clear_frame;
13606 ins_del_lines_hook = x_ins_del_lines;
13607 change_line_highlight_hook = x_change_line_highlight;
13608 delete_glyphs_hook = x_delete_glyphs;
dc6f92b8
JB
13609 ring_bell_hook = XTring_bell;
13610 reset_terminal_modes_hook = XTreset_terminal_modes;
13611 set_terminal_modes_hook = XTset_terminal_modes;
06a2c219
GM
13612 update_begin_hook = x_update_begin;
13613 update_end_hook = x_update_end;
dc6f92b8
JB
13614 set_terminal_window_hook = XTset_terminal_window;
13615 read_socket_hook = XTread_socket;
b8009dd1 13616 frame_up_to_date_hook = XTframe_up_to_date;
dc6f92b8 13617 reassert_line_highlight_hook = XTreassert_line_highlight;
90e65f07 13618 mouse_position_hook = XTmouse_position;
f451eb13 13619 frame_rehighlight_hook = XTframe_rehighlight;
dbc4e1c1 13620 frame_raise_lower_hook = XTframe_raise_lower;
ab648270
JB
13621 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13622 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13623 redeem_scroll_bar_hook = XTredeem_scroll_bar;
13624 judge_scroll_bars_hook = XTjudge_scroll_bars;
06a2c219 13625 estimate_mode_line_height_hook = x_estimate_mode_line_height;
58769bee 13626
f676886a 13627 scroll_region_ok = 1; /* we'll scroll partial frames */
dc6f92b8
JB
13628 char_ins_del_ok = 0; /* just as fast to write the line */
13629 line_ins_del_ok = 1; /* we'll just blt 'em */
13630 fast_clear_end_of_line = 1; /* X does this well */
58769bee 13631 memory_below_frame = 0; /* we don't remember what scrolls
dc6f92b8
JB
13632 off the bottom */
13633 baud_rate = 19200;
13634
7a13e894 13635 x_noop_count = 0;
9ea173e8 13636 last_tool_bar_item = -1;
06a2c219
GM
13637 any_help_event_p = 0;
13638
b30b24cb
RS
13639 /* Try to use interrupt input; if we can't, then start polling. */
13640 Fset_input_mode (Qt, Qnil, Qt, Qnil);
13641
7f9c7f94
RS
13642#ifdef USE_X_TOOLKIT
13643 XtToolkitInitialize ();
13644 Xt_app_con = XtCreateApplicationContext ();
665881ad 13645 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
bffcfca9
GM
13646
13647 /* Install an asynchronous timer that processes Xt timeout events
13648 every 0.1s. This is necessary because some widget sets use
13649 timeouts internally, for example the LessTif menu bar, or the
13650 Xaw3d scroll bar. When Xt timouts aren't processed, these
13651 widgets don't behave normally. */
13652 {
13653 EMACS_TIME interval;
13654 EMACS_SET_SECS_USECS (interval, 0, 100000);
13655 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
13656 }
db74249b 13657#endif
bffcfca9 13658
db74249b 13659#if USE_TOOLKIT_SCROLL_BARS
ec18280f
SM
13660 xaw3d_arrow_scroll = False;
13661 xaw3d_pick_top = True;
7f9c7f94
RS
13662#endif
13663
58769bee 13664 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 13665 original error handler. */
e99db5a1 13666 XSetErrorHandler (x_error_handler);
334208b7 13667 XSetIOErrorHandler (x_io_error_quitter);
dc6f92b8 13668
06a2c219 13669 /* Disable Window Change signals; they are handled by X events. */
dc6f92b8
JB
13670#ifdef SIGWINCH
13671 signal (SIGWINCH, SIG_DFL);
c118dd06 13672#endif /* ! defined (SIGWINCH) */
dc6f92b8 13673
92e2441b 13674 signal (SIGPIPE, x_connection_signal);
dc6f92b8 13675}
55123275 13676
06a2c219 13677
55123275
JB
13678void
13679syms_of_xterm ()
13680{
e99db5a1
RS
13681 staticpro (&x_error_message_string);
13682 x_error_message_string = Qnil;
13683
7a13e894
RS
13684 staticpro (&x_display_name_list);
13685 x_display_name_list = Qnil;
334208b7 13686
ab648270 13687 staticpro (&last_mouse_scroll_bar);
e53cb100 13688 last_mouse_scroll_bar = Qnil;
59e755be
KH
13689
13690 staticpro (&Qvendor_specific_keysyms);
13691 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
2237cac9
RS
13692
13693 staticpro (&last_mouse_press_frame);
13694 last_mouse_press_frame = Qnil;
06a2c219 13695
06a2c219 13696 help_echo = Qnil;
be010514
GM
13697 staticpro (&help_echo);
13698 help_echo_object = Qnil;
13699 staticpro (&help_echo_object);
7cea38bc
GM
13700 help_echo_window = Qnil;
13701 staticpro (&help_echo_window);
06a2c219 13702 previous_help_echo = Qnil;
be010514
GM
13703 staticpro (&previous_help_echo);
13704 help_echo_pos = -1;
06a2c219
GM
13705
13706 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
13707 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
13708For example, if a block cursor is over a tab, it will be drawn as\n\
13709wide as that tab on the display.");
13710 x_stretch_cursor_p = 0;
13711
13712 DEFVAR_BOOL ("x-toolkit-scroll-bars-p", &x_toolkit_scroll_bars_p,
13713 "If not nil, Emacs uses toolkit scroll bars.");
13714#if USE_TOOLKIT_SCROLL_BARS
13715 x_toolkit_scroll_bars_p = 1;
13716#else
13717 x_toolkit_scroll_bars_p = 0;
13718#endif
13719
06a2c219
GM
13720 staticpro (&last_mouse_motion_frame);
13721 last_mouse_motion_frame = Qnil;
55123275 13722}
6cf0ae86
RS
13723
13724#endif /* not HAVE_X_WINDOWS */
06a2c219 13725