(choose-completion-string): When not leaving the
[bpt/emacs.git] / src / xterm.c
CommitLineData
dc6f92b8 1/* X Communication module for terminals which understand the X protocol.
edf36fe6 2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000, 2001
06a2c219 3 Free Software Foundation, Inc.
dc6f92b8
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
4746118a 9the Free Software Foundation; either version 2, or (at your option)
dc6f92b8
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
dc6f92b8 21
06a2c219 22/* New display code by Gerd Moellmann <gerd@gnu.org>. */
3afe33e7
RS
23/* Xt features made by Fred Pierresteguy. */
24
68c45bf0
PE
25#include <config.h>
26
039440c4 27/* On 4.3 these lose if they come after xterm.h. */
039440c4 28/* Putting these at the beginning seems to be standard for other .c files. */
039440c4
RS
29#include <signal.h>
30
4846819e
RS
31#include <stdio.h>
32
dc6f92b8
JB
33#ifdef HAVE_X_WINDOWS
34
35#include "lisp.h"
9ac0d9e0 36#include "blockinput.h"
dc6f92b8 37
ae79c227
AS
38/* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40#include "syssignal.h"
41
dc6f92b8
JB
42/* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44#include "xterm.h"
f451eb13 45#include <X11/cursorfont.h>
dc6f92b8
JB
46
47/* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49#ifndef makedev
50#include <sys/types.h>
c118dd06 51#endif /* makedev */
dc6f92b8 52
6df54671 53#ifdef BSD_SYSTEM
dc6f92b8 54#include <sys/ioctl.h>
6df54671 55#endif /* ! defined (BSD_SYSTEM) */
dc6f92b8 56
2d368234 57#include "systty.h"
3a2712f9 58#include "systime.h"
dc6f92b8 59
b8009dd1 60#ifndef INCLUDED_FCNTL
dc6f92b8 61#include <fcntl.h>
b8009dd1 62#endif
dc6f92b8
JB
63#include <ctype.h>
64#include <errno.h>
65#include <setjmp.h>
66#include <sys/stat.h>
a0a7635f
RS
67/* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
68/* #include <sys/param.h> */
dc6f92b8 69
dc43ef94 70#include "charset.h"
379b5ac0 71#include "coding.h"
dc43ef94 72#include "ccl.h"
7a13e894 73#include "frame.h"
dc6f92b8 74#include "dispextern.h"
ee569018 75#include "fontset.h"
dc6f92b8
JB
76#include "termhooks.h"
77#include "termopts.h"
78#include "termchar.h"
79#if 0
80#include "sink.h"
81#include "sinkmask.h"
c118dd06 82#endif /* ! 0 */
dc6f92b8 83#include "gnu.h"
dc6f92b8 84#include "disptab.h"
dc6f92b8 85#include "buffer.h"
f451eb13 86#include "window.h"
3b2fa4e6 87#include "keyboard.h"
bde7c500 88#include "intervals.h"
dfcf069d 89#include "process.h"
bffcfca9 90#include "atimer.h"
dc6f92b8 91
d2bd6bc4
RS
92#ifdef USE_X_TOOLKIT
93#include <X11/Shell.h>
94#endif
95
06a2c219
GM
96#ifdef HAVE_SYS_TIME_H
97#include <sys/time.h>
98#endif
99#ifdef HAVE_UNISTD_H
100#include <unistd.h>
101#endif
102
3afe33e7 103#ifdef USE_X_TOOLKIT
06a2c219 104
952291d9
GM
105extern void free_frame_menubar P_ ((struct frame *));
106extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
107 int));
06a2c219 108
0fdff6bb
RS
109#if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
110#define HACK_EDITRES
111extern void _XEditResCheckMessages ();
112#endif /* not NO_EDITRES */
06a2c219
GM
113
114/* Include toolkit specific headers for the scroll bar widget. */
115
116#ifdef USE_TOOLKIT_SCROLL_BARS
117#if defined USE_MOTIF
118#include <Xm/Xm.h> /* for LESSTIF_VERSION */
119#include <Xm/ScrollBar.h>
120#include <Xm/ScrollBarP.h>
ec18280f
SM
121#else /* !USE_MOTIF i.e. use Xaw */
122
123#ifdef HAVE_XAW3D
06a2c219 124#include <X11/Xaw3d/Simple.h>
06a2c219
GM
125#include <X11/Xaw3d/Scrollbar.h>
126#define ARROW_SCROLLBAR
127#include <X11/Xaw3d/ScrollbarP.h>
ec18280f
SM
128#else /* !HAVE_XAW3D */
129#include <X11/Xaw/Simple.h>
130#include <X11/Xaw/Scrollbar.h>
131#endif /* !HAVE_XAW3D */
132#ifndef XtNpickTop
133#define XtNpickTop "pickTop"
134#endif /* !XtNpickTop */
135#endif /* !USE_MOTIF */
06a2c219
GM
136#endif /* USE_TOOLKIT_SCROLL_BARS */
137
3afe33e7
RS
138#endif /* USE_X_TOOLKIT */
139
b849c413
RS
140#ifndef USE_X_TOOLKIT
141#define x_any_window_to_frame x_window_to_frame
5627c40e 142#define x_top_window_to_frame x_window_to_frame
b849c413
RS
143#endif
144
546e6d5b 145#ifdef USE_X_TOOLKIT
d067ea8b 146#include "widget.h"
546e6d5b
RS
147#ifndef XtNinitialState
148#define XtNinitialState "initialState"
149#endif
150#endif
151
e4b68333 152#ifndef min
06a2c219 153#define min(a,b) ((a) < (b) ? (a) : (b))
e4b68333
RS
154#endif
155#ifndef max
06a2c219
GM
156#define max(a,b) ((a) > (b) ? (a) : (b))
157#endif
158
159#define abs(x) ((x) < 0 ? -(x) : (x))
160
161#define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
162
163\f
164/* Bitmaps for truncated lines. */
165
166enum bitmap_type
167{
168 NO_BITMAP,
169 LEFT_TRUNCATION_BITMAP,
170 RIGHT_TRUNCATION_BITMAP,
171 OVERLAY_ARROW_BITMAP,
172 CONTINUED_LINE_BITMAP,
173 CONTINUATION_LINE_BITMAP,
174 ZV_LINE_BITMAP
175};
176
177/* Bitmap drawn to indicate lines not displaying text if
178 `indicate-empty-lines' is non-nil. */
179
180#define zv_width 8
181#define zv_height 8
182static unsigned char zv_bits[] = {
183 0x00, 0x00, 0x1e, 0x1e, 0x1e, 0x1e, 0x00, 0x00};
184
185/* An arrow like this: `<-'. */
186
187#define left_width 8
188#define left_height 8
189static unsigned char left_bits[] = {
190 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
191
110859fc
GM
192/* Right truncation arrow bitmap `->'. */
193
194#define right_width 8
195#define right_height 8
196static unsigned char right_bits[] = {
197 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
198
06a2c219
GM
199/* Marker for continued lines. */
200
201#define continued_width 8
202#define continued_height 8
203static unsigned char continued_bits[] = {
110859fc
GM
204 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
205
206/* Marker for continuation lines. */
06a2c219
GM
207
208#define continuation_width 8
209#define continuation_height 8
210static unsigned char continuation_bits[] = {
110859fc 211 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
06a2c219 212
110859fc 213/* Overlay arrow bitmap. */
06a2c219 214
110859fc
GM
215#if 0
216/* A bomb. */
06a2c219
GM
217#define ov_width 8
218#define ov_height 8
219static unsigned char ov_bits[] = {
220 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
06a2c219 221#else
110859fc 222/* A triangular arrow. */
06a2c219
GM
223#define ov_width 8
224#define ov_height 8
225static unsigned char ov_bits[] = {
110859fc
GM
226 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
227
e4b68333 228#endif
06a2c219
GM
229
230extern Lisp_Object Qhelp_echo;
231
69388238 232\f
5bf04520 233/* Non-nil means Emacs uses toolkit scroll bars. */
06a2c219 234
5bf04520 235Lisp_Object Vx_toolkit_scroll_bars;
06a2c219
GM
236
237/* If a string, XTread_socket generates an event to display that string.
238 (The display is done in read_char.) */
239
240static Lisp_Object help_echo;
7cea38bc 241static Lisp_Object help_echo_window;
be010514
GM
242static Lisp_Object help_echo_object;
243static int help_echo_pos;
06a2c219
GM
244
245/* Temporary variable for XTread_socket. */
246
247static Lisp_Object previous_help_echo;
248
249/* Non-zero means that a HELP_EVENT has been generated since Emacs
250 start. */
251
252static int any_help_event_p;
253
254/* Non-zero means draw block and hollow cursor as wide as the glyph
255 under it. For example, if a block cursor is over a tab, it will be
256 drawn as wide as that tab on the display. */
257
258int x_stretch_cursor_p;
259
260/* This is a chain of structures for all the X displays currently in
261 use. */
262
334208b7 263struct x_display_info *x_display_list;
dc6f92b8 264
06a2c219
GM
265/* This is a list of cons cells, each of the form (NAME
266 . FONT-LIST-CACHE), one for each element of x_display_list and in
267 the same order. NAME is the name of the frame. FONT-LIST-CACHE
268 records previous values returned by x-list-fonts. */
269
7a13e894 270Lisp_Object x_display_name_list;
f451eb13 271
987d2ad1 272/* Frame being updated by update_frame. This is declared in term.c.
06a2c219
GM
273 This is set by update_begin and looked at by all the XT functions.
274 It is zero while not inside an update. In that case, the XT
275 functions assume that `selected_frame' is the frame to apply to. */
276
d0386f2a 277extern struct frame *updating_frame;
dc6f92b8 278
dfcf069d 279extern int waiting_for_input;
0e81d8cd 280
06a2c219
GM
281/* This is a frame waiting to be auto-raised, within XTread_socket. */
282
0134a210
RS
283struct frame *pending_autoraise_frame;
284
7f9c7f94
RS
285#ifdef USE_X_TOOLKIT
286/* The application context for Xt use. */
287XtAppContext Xt_app_con;
06a2c219
GM
288static String Xt_default_resources[] = {0};
289#endif /* USE_X_TOOLKIT */
665881ad 290
06a2c219
GM
291/* Nominal cursor position -- where to draw output.
292 HPOS and VPOS are window relative glyph matrix coordinates.
293 X and Y are window relative pixel coordinates. */
dc6f92b8 294
06a2c219 295struct cursor_pos output_cursor;
dc6f92b8 296
bffcfca9
GM
297/* Non-zero means user is interacting with a toolkit scroll bar. */
298
299static int toolkit_scroll_bar_interaction;
dc6f92b8 300
69388238
RS
301/* Mouse movement.
302
06a2c219 303 Formerly, we used PointerMotionHintMask (in standard_event_mask)
f5bb65ec
RS
304 so that we would have to call XQueryPointer after each MotionNotify
305 event to ask for another such event. However, this made mouse tracking
306 slow, and there was a bug that made it eventually stop.
307
308 Simply asking for MotionNotify all the time seems to work better.
309
69388238
RS
310 In order to avoid asking for motion events and then throwing most
311 of them away or busy-polling the server for mouse positions, we ask
312 the server for pointer motion hints. This means that we get only
313 one event per group of mouse movements. "Groups" are delimited by
314 other kinds of events (focus changes and button clicks, for
315 example), or by XQueryPointer calls; when one of these happens, we
316 get another MotionNotify event the next time the mouse moves. This
317 is at least as efficient as getting motion events when mouse
318 tracking is on, and I suspect only negligibly worse when tracking
f5bb65ec 319 is off. */
69388238
RS
320
321/* Where the mouse was last time we reported a mouse event. */
69388238 322
06a2c219
GM
323FRAME_PTR last_mouse_frame;
324static XRectangle last_mouse_glyph;
2237cac9
RS
325static Lisp_Object last_mouse_press_frame;
326
69388238
RS
327/* The scroll bar in which the last X motion event occurred.
328
06a2c219
GM
329 If the last X motion event occurred in a scroll bar, we set this so
330 XTmouse_position can know whether to report a scroll bar motion or
69388238
RS
331 an ordinary motion.
332
06a2c219
GM
333 If the last X motion event didn't occur in a scroll bar, we set
334 this to Qnil, to tell XTmouse_position to return an ordinary motion
335 event. */
336
69388238
RS
337static Lisp_Object last_mouse_scroll_bar;
338
69388238
RS
339/* This is a hack. We would really prefer that XTmouse_position would
340 return the time associated with the position it returns, but there
06a2c219 341 doesn't seem to be any way to wrest the time-stamp from the server
69388238
RS
342 along with the position query. So, we just keep track of the time
343 of the last movement we received, and return that in hopes that
344 it's somewhat accurate. */
06a2c219 345
69388238
RS
346static Time last_mouse_movement_time;
347
06a2c219
GM
348/* Incremented by XTread_socket whenever it really tries to read
349 events. */
350
c0a04927
RS
351#ifdef __STDC__
352static int volatile input_signal_count;
353#else
354static int input_signal_count;
355#endif
356
7a13e894 357/* Used locally within XTread_socket. */
06a2c219 358
7a13e894 359static int x_noop_count;
dc6f92b8 360
7a13e894 361/* Initial values of argv and argc. */
06a2c219 362
7a13e894
RS
363extern char **initial_argv;
364extern int initial_argc;
dc6f92b8 365
7a13e894 366extern Lisp_Object Vcommand_line_args, Vsystem_name;
dc6f92b8 367
06a2c219 368/* Tells if a window manager is present or not. */
7a13e894
RS
369
370extern Lisp_Object Vx_no_window_manager;
dc6f92b8 371
c2df547c 372extern Lisp_Object Qface, Qmouse_face;
b8009dd1 373
dc6f92b8
JB
374extern int errno;
375
dfeccd2d 376/* A mask of extra modifier bits to put into every keyboard char. */
06a2c219 377
64bb1782
RS
378extern int extra_keyboard_modifiers;
379
59e755be
KH
380static Lisp_Object Qvendor_specific_keysyms;
381
952291d9
GM
382extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
383extern Lisp_Object x_icon_type P_ ((struct frame *));
c32cdd9a 384
7a13e894 385
06a2c219
GM
386/* Enumeration for overriding/changing the face to use for drawing
387 glyphs in x_draw_glyphs. */
388
389enum draw_glyphs_face
390{
391 DRAW_NORMAL_TEXT,
392 DRAW_INVERSE_VIDEO,
393 DRAW_CURSOR,
394 DRAW_MOUSE_FACE,
395 DRAW_IMAGE_RAISED,
396 DRAW_IMAGE_SUNKEN
397};
398
499b1844 399static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
f04e1297 400static const XColor *x_color_cells P_ ((struct frame *, int *));
71b8321e 401static void x_update_window_end P_ ((struct window *, int, int));
06a2c219
GM
402static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
403void x_delete_display P_ ((struct x_display_info *));
404static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
405 unsigned));
406static int fast_find_position P_ ((struct window *, int, int *, int *,
407 int *, int *));
408static void set_output_cursor P_ ((struct cursor_pos *));
409static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
410 int *, int *, int *));
411static void note_mode_line_highlight P_ ((struct window *, int, int));
06a2c219 412static void note_mouse_highlight P_ ((struct frame *, int, int));
9ea173e8
GM
413static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
414static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
06a2c219
GM
415static void show_mouse_face P_ ((struct x_display_info *,
416 enum draw_glyphs_face));
417static int x_io_error_quitter P_ ((Display *));
418int x_catch_errors P_ ((Display *));
419void x_uncatch_errors P_ ((Display *, int));
420void x_lower_frame P_ ((struct frame *));
421void x_scroll_bar_clear P_ ((struct frame *));
422int x_had_errors_p P_ ((Display *));
423void x_wm_set_size_hint P_ ((struct frame *, long, int));
424void x_raise_frame P_ ((struct frame *));
425void x_set_window_size P_ ((struct frame *, int, int, int));
426void x_wm_set_window_state P_ ((struct frame *, int));
427void x_wm_set_icon_pixmap P_ ((struct frame *, int));
428void x_initialize P_ ((void));
429static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
430static int x_compute_min_glyph_bounds P_ ((struct frame *));
431static void x_draw_phys_cursor_glyph P_ ((struct window *,
432 struct glyph_row *,
433 enum draw_glyphs_face));
434static void x_update_end P_ ((struct frame *));
435static void XTframe_up_to_date P_ ((struct frame *));
436static void XTreassert_line_highlight P_ ((int, int));
437static void x_change_line_highlight P_ ((int, int, int, int));
438static void XTset_terminal_modes P_ ((void));
439static void XTreset_terminal_modes P_ ((void));
440static void XTcursor_to P_ ((int, int, int, int));
441static void x_write_glyphs P_ ((struct glyph *, int));
442static void x_clear_end_of_line P_ ((int));
443static void x_clear_frame P_ ((void));
444static void x_clear_cursor P_ ((struct window *));
445static void frame_highlight P_ ((struct frame *));
446static void frame_unhighlight P_ ((struct frame *));
447static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
448static void XTframe_rehighlight P_ ((struct frame *));
449static void x_frame_rehighlight P_ ((struct x_display_info *));
450static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
f02d8aa0 451static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
06a2c219
GM
452static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
453 XRectangle *));
454static void expose_frame P_ ((struct frame *, int, int, int, int));
455static void expose_window_tree P_ ((struct window *, XRectangle *));
456static void expose_window P_ ((struct window *, XRectangle *));
457static void expose_area P_ ((struct window *, struct glyph_row *,
458 XRectangle *, enum glyph_row_area));
459static void expose_line P_ ((struct window *, struct glyph_row *,
460 XRectangle *));
461static void x_update_cursor_in_window_tree P_ ((struct window *, int));
462static void x_update_window_cursor P_ ((struct window *, int));
463static void x_erase_phys_cursor P_ ((struct window *));
464void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
465static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
466 enum bitmap_type));
467
468static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
469 GC, int));
470static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
471static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
472static void note_overwritten_text_cursor P_ ((struct window *, int, int));
473static void x_flush P_ ((struct frame *f));
952291d9
GM
474static void x_update_begin P_ ((struct frame *));
475static void x_update_window_begin P_ ((struct window *));
476static void x_draw_vertical_border P_ ((struct window *));
477static void x_after_update_window_line P_ ((struct glyph_row *));
478static INLINE void take_vertical_position_into_account P_ ((struct it *));
479static void x_produce_stretch_glyph P_ ((struct it *));
b52b65bd
GM
480static struct scroll_bar *x_window_to_scroll_bar P_ ((Window));
481static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
482 enum scroll_bar_part *,
483 Lisp_Object *, Lisp_Object *,
484 unsigned long *));
06a2c219
GM
485
486/* Flush display of frame F, or of all frames if F is null. */
487
488static void
489x_flush (f)
490 struct frame *f;
491{
492 BLOCK_INPUT;
493 if (f == NULL)
494 {
495 Lisp_Object rest, frame;
496 FOR_EACH_FRAME (rest, frame)
497 x_flush (XFRAME (frame));
498 }
499 else if (FRAME_X_P (f))
500 XFlush (FRAME_X_DISPLAY (f));
501 UNBLOCK_INPUT;
502}
503
dc6f92b8 504
06a2c219
GM
505/* Remove calls to XFlush by defining XFlush to an empty replacement.
506 Calls to XFlush should be unnecessary because the X output buffer
507 is flushed automatically as needed by calls to XPending,
508 XNextEvent, or XWindowEvent according to the XFlush man page.
509 XTread_socket calls XPending. Removing XFlush improves
510 performance. */
511
512#define XFlush(DISPLAY) (void) 0
b8009dd1 513
334208b7 514\f
06a2c219
GM
515/***********************************************************************
516 Debugging
517 ***********************************************************************/
518
9382638d 519#if 0
06a2c219
GM
520
521/* This is a function useful for recording debugging information about
522 the sequence of occurrences in this file. */
9382638d
KH
523
524struct record
525{
526 char *locus;
527 int type;
528};
529
530struct record event_record[100];
531
532int event_record_index;
533
534record_event (locus, type)
535 char *locus;
536 int type;
537{
538 if (event_record_index == sizeof (event_record) / sizeof (struct record))
539 event_record_index = 0;
540
541 event_record[event_record_index].locus = locus;
542 event_record[event_record_index].type = type;
543 event_record_index++;
544}
545
546#endif /* 0 */
06a2c219
GM
547
548
9382638d 549\f
334208b7
RS
550/* Return the struct x_display_info corresponding to DPY. */
551
552struct x_display_info *
553x_display_info_for_display (dpy)
554 Display *dpy;
555{
556 struct x_display_info *dpyinfo;
557
558 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
559 if (dpyinfo->display == dpy)
560 return dpyinfo;
16bd92ea 561
334208b7
RS
562 return 0;
563}
f451eb13 564
06a2c219
GM
565
566\f
567/***********************************************************************
568 Starting and ending an update
569 ***********************************************************************/
570
571/* Start an update of frame F. This function is installed as a hook
572 for update_begin, i.e. it is called when update_begin is called.
573 This function is called prior to calls to x_update_window_begin for
574 each window being updated. Currently, there is nothing to do here
575 because all interesting stuff is done on a window basis. */
dc6f92b8 576
dfcf069d 577static void
06a2c219 578x_update_begin (f)
f676886a 579 struct frame *f;
58769bee 580{
06a2c219
GM
581 /* Nothing to do. */
582}
dc6f92b8 583
dc6f92b8 584
06a2c219
GM
585/* Start update of window W. Set the global variable updated_window
586 to the window being updated and set output_cursor to the cursor
587 position of W. */
dc6f92b8 588
06a2c219
GM
589static void
590x_update_window_begin (w)
591 struct window *w;
592{
593 struct frame *f = XFRAME (WINDOW_FRAME (w));
594 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
595
596 updated_window = w;
597 set_output_cursor (&w->cursor);
b8009dd1 598
06a2c219 599 BLOCK_INPUT;
d1bc4182 600
06a2c219 601 if (f == display_info->mouse_face_mouse_frame)
b8009dd1 602 {
514e4681 603 /* Don't do highlighting for mouse motion during the update. */
06a2c219 604 display_info->mouse_face_defer = 1;
37c2c98b 605
06a2c219
GM
606 /* If F needs to be redrawn, simply forget about any prior mouse
607 highlighting. */
9f67f20b 608 if (FRAME_GARBAGED_P (f))
06a2c219
GM
609 display_info->mouse_face_window = Qnil;
610
64f26cf5
GM
611#if 0 /* Rows in a current matrix containing glyphs in mouse-face have
612 their mouse_face_p flag set, which means that they are always
613 unequal to rows in a desired matrix which never have that
614 flag set. So, rows containing mouse-face glyphs are never
615 scrolled, and we don't have to switch the mouse highlight off
616 here to prevent it from being scrolled. */
617
06a2c219
GM
618 /* Can we tell that this update does not affect the window
619 where the mouse highlight is? If so, no need to turn off.
620 Likewise, don't do anything if the frame is garbaged;
621 in that case, the frame's current matrix that we would use
622 is all wrong, and we will redisplay that line anyway. */
623 if (!NILP (display_info->mouse_face_window)
624 && w == XWINDOW (display_info->mouse_face_window))
514e4681 625 {
06a2c219 626 int i;
514e4681 627
06a2c219
GM
628 for (i = 0; i < w->desired_matrix->nrows; ++i)
629 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
514e4681
RS
630 break;
631
06a2c219
GM
632 if (i < w->desired_matrix->nrows)
633 clear_mouse_face (display_info);
514e4681 634 }
64f26cf5 635#endif /* 0 */
b8009dd1 636 }
6ccf47d1 637
dc6f92b8
JB
638 UNBLOCK_INPUT;
639}
640
06a2c219
GM
641
642/* Draw a vertical window border to the right of window W if W doesn't
643 have vertical scroll bars. */
644
dfcf069d 645static void
06a2c219
GM
646x_draw_vertical_border (w)
647 struct window *w;
58769bee 648{
06a2c219
GM
649 struct frame *f = XFRAME (WINDOW_FRAME (w));
650
651 /* Redraw borders between horizontally adjacent windows. Don't
652 do it for frames with vertical scroll bars because either the
653 right scroll bar of a window, or the left scroll bar of its
654 neighbor will suffice as a border. */
655 if (!WINDOW_RIGHTMOST_P (w)
656 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
657 {
658 int x0, x1, y0, y1;
dc6f92b8 659
06a2c219 660 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
110859fc 661 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
662 y1 -= 1;
663
664 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
665 f->output_data.x->normal_gc, x1, y0, x1, y1);
666 }
667}
668
669
71b8321e
GM
670/* End update of window W (which is equal to updated_window).
671
672 Draw vertical borders between horizontally adjacent windows, and
673 display W's cursor if CURSOR_ON_P is non-zero.
674
675 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
676 glyphs in mouse-face were overwritten. In that case we have to
677 make sure that the mouse-highlight is properly redrawn.
678
679 W may be a menu bar pseudo-window in case we don't have X toolkit
680 support. Such windows don't have a cursor, so don't display it
681 here. */
06a2c219
GM
682
683static void
71b8321e 684x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
06a2c219 685 struct window *w;
71b8321e 686 int cursor_on_p, mouse_face_overwritten_p;
06a2c219
GM
687{
688 if (!w->pseudo_window_p)
689 {
71b8321e
GM
690 struct x_display_info *dpyinfo
691 = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
692
06a2c219 693 BLOCK_INPUT;
71b8321e
GM
694
695 /* If a row with mouse-face was overwritten, arrange for
696 XTframe_up_to_date to redisplay the mouse highlight. */
697 if (mouse_face_overwritten_p)
698 {
699 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
700 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
701 dpyinfo->mouse_face_window = Qnil;
702 }
703
06a2c219
GM
704 if (cursor_on_p)
705 x_display_and_set_cursor (w, 1, output_cursor.hpos,
706 output_cursor.vpos,
707 output_cursor.x, output_cursor.y);
71b8321e 708
06a2c219
GM
709 x_draw_vertical_border (w);
710 UNBLOCK_INPUT;
711 }
712
713 updated_window = NULL;
714}
dc6f92b8 715
dc6f92b8 716
06a2c219
GM
717/* End update of frame F. This function is installed as a hook in
718 update_end. */
719
720static void
721x_update_end (f)
722 struct frame *f;
723{
724 /* Mouse highlight may be displayed again. */
aa8bff2e 725 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
b8009dd1 726
06a2c219 727 BLOCK_INPUT;
334208b7 728 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
729 UNBLOCK_INPUT;
730}
b8009dd1 731
06a2c219
GM
732
733/* This function is called from various places in xdisp.c whenever a
734 complete update has been performed. The global variable
735 updated_window is not available here. */
b8009dd1 736
dfcf069d 737static void
b8009dd1 738XTframe_up_to_date (f)
06a2c219 739 struct frame *f;
b8009dd1 740{
06a2c219 741 if (FRAME_X_P (f))
514e4681 742 {
06a2c219 743 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
71b8321e 744
06a2c219
GM
745 if (dpyinfo->mouse_face_deferred_gc
746 || f == dpyinfo->mouse_face_mouse_frame)
747 {
748 BLOCK_INPUT;
749 if (dpyinfo->mouse_face_mouse_frame)
750 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
751 dpyinfo->mouse_face_mouse_x,
752 dpyinfo->mouse_face_mouse_y);
753 dpyinfo->mouse_face_deferred_gc = 0;
754 UNBLOCK_INPUT;
755 }
514e4681 756 }
b8009dd1 757}
06a2c219
GM
758
759
760/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
761 arrow bitmaps, or clear the areas where they would be displayed
762 before DESIRED_ROW is made current. The window being updated is
763 found in updated_window. This function It is called from
764 update_window_line only if it is known that there are differences
765 between bitmaps to be drawn between current row and DESIRED_ROW. */
766
767static void
768x_after_update_window_line (desired_row)
769 struct glyph_row *desired_row;
770{
771 struct window *w = updated_window;
772
773 xassert (w);
774
775 if (!desired_row->mode_line_p && !w->pseudo_window_p)
776 {
c5e6e06b
GM
777 struct frame *f;
778 int width;
779
06a2c219
GM
780 BLOCK_INPUT;
781 x_draw_row_bitmaps (w, desired_row);
782
783 /* When a window has disappeared, make sure that no rest of
784 full-width rows stays visible in the internal border. */
c5e6e06b
GM
785 if (windows_or_buffers_changed
786 && (f = XFRAME (w->frame),
787 width = FRAME_INTERNAL_BORDER_WIDTH (f),
788 width != 0))
06a2c219 789 {
06a2c219 790 int height = desired_row->visible_height;
110859fc
GM
791 int x = (window_box_right (w, -1)
792 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
06a2c219
GM
793 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
794
c5e6e06b
GM
795 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
796 x, y, width, height, False);
06a2c219
GM
797 }
798
799 UNBLOCK_INPUT;
800 }
801}
802
803
804/* Draw the bitmap WHICH in one of the areas to the left or right of
805 window W. ROW is the glyph row for which to display the bitmap; it
806 determines the vertical position at which the bitmap has to be
807 drawn. */
808
809static void
810x_draw_bitmap (w, row, which)
811 struct window *w;
812 struct glyph_row *row;
813 enum bitmap_type which;
814{
815 struct frame *f = XFRAME (WINDOW_FRAME (w));
816 Display *display = FRAME_X_DISPLAY (f);
817 Window window = FRAME_X_WINDOW (f);
818 int x, y, wd, h, dy;
819 unsigned char *bits;
820 Pixmap pixmap;
821 GC gc = f->output_data.x->normal_gc;
822 struct face *face;
823 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
824
825 /* Must clip because of partially visible lines. */
826 x_clip_to_row (w, row, gc, 1);
827
828 switch (which)
829 {
830 case LEFT_TRUNCATION_BITMAP:
831 wd = left_width;
832 h = left_height;
833 bits = left_bits;
834 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
835 - wd
110859fc 836 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
837 break;
838
839 case OVERLAY_ARROW_BITMAP:
840 wd = left_width;
841 h = left_height;
842 bits = ov_bits;
843 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
844 - wd
110859fc 845 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
846 break;
847
848 case RIGHT_TRUNCATION_BITMAP:
849 wd = right_width;
850 h = right_height;
851 bits = right_bits;
852 x = window_box_right (w, -1);
110859fc 853 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
854 break;
855
856 case CONTINUED_LINE_BITMAP:
857 wd = right_width;
858 h = right_height;
859 bits = continued_bits;
860 x = window_box_right (w, -1);
110859fc 861 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
862 break;
863
864 case CONTINUATION_LINE_BITMAP:
865 wd = continuation_width;
866 h = continuation_height;
867 bits = continuation_bits;
868 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
869 - wd
110859fc 870 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
871 break;
872
873 case ZV_LINE_BITMAP:
874 wd = zv_width;
875 h = zv_height;
876 bits = zv_bits;
877 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
878 - wd
110859fc 879 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
880 break;
881
882 default:
883 abort ();
884 }
885
886 /* Convert to frame coordinates. Set dy to the offset in the row to
887 start drawing the bitmap. */
888 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
889 dy = (row->height - h) / 2;
890
891 /* Draw the bitmap. I believe these small pixmaps can be cached
892 by the server. */
893 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
894 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
895 face->foreground,
896 face->background, depth);
897 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
898 XFreePixmap (display, pixmap);
899 XSetClipMask (display, gc, None);
900}
901
902
903/* Draw flags bitmaps for glyph row ROW on window W. Call this
904 function with input blocked. */
905
906static void
907x_draw_row_bitmaps (w, row)
908 struct window *w;
909 struct glyph_row *row;
910{
911 struct frame *f = XFRAME (w->frame);
912 enum bitmap_type bitmap;
913 struct face *face;
045dee35 914 int header_line_height = -1;
06a2c219
GM
915
916 xassert (interrupt_input_blocked);
917
918 /* If row is completely invisible, because of vscrolling, we
919 don't have to draw anything. */
920 if (row->visible_height <= 0)
921 return;
922
923 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
924 PREPARE_FACE_FOR_DISPLAY (f, face);
925
926 /* Decide which bitmap to draw at the left side. */
927 if (row->overlay_arrow_p)
928 bitmap = OVERLAY_ARROW_BITMAP;
929 else if (row->truncated_on_left_p)
930 bitmap = LEFT_TRUNCATION_BITMAP;
931 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
932 bitmap = CONTINUATION_LINE_BITMAP;
933 else if (row->indicate_empty_line_p)
934 bitmap = ZV_LINE_BITMAP;
935 else
936 bitmap = NO_BITMAP;
937
938 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
939 the flags area. */
940 if (bitmap == NO_BITMAP
110859fc 941 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
942 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
943 {
944 /* If W has a vertical border to its left, don't draw over it. */
945 int border = ((XFASTINT (w->left) > 0
946 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
947 ? 1 : 0);
948 int left = window_box_left (w, -1);
949
045dee35
GM
950 if (header_line_height < 0)
951 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
952
953 /* In case the same realized face is used for bitmap areas and
954 for something displayed in the text (e.g. face `region' on
955 mono-displays, the fill style may have been changed to
956 FillSolid in x_draw_glyph_string_background. */
957 if (face->stipple)
958 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
959 else
960 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
961
06a2c219
GM
962 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
963 face->gc,
964 (left
110859fc 965 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219 966 + border),
045dee35 967 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 968 row->y)),
110859fc 969 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
06a2c219 970 row->visible_height);
dcd08bfb
GM
971 if (!face->stipple)
972 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
973 }
974
975 /* Draw the left bitmap. */
976 if (bitmap != NO_BITMAP)
977 x_draw_bitmap (w, row, bitmap);
978
979 /* Decide which bitmap to draw at the right side. */
980 if (row->truncated_on_right_p)
981 bitmap = RIGHT_TRUNCATION_BITMAP;
982 else if (row->continued_p)
983 bitmap = CONTINUED_LINE_BITMAP;
984 else
985 bitmap = NO_BITMAP;
986
987 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
988 the flags area. */
989 if (bitmap == NO_BITMAP
110859fc 990 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
991 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
992 {
993 int right = window_box_right (w, -1);
994
045dee35
GM
995 if (header_line_height < 0)
996 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
997
998 /* In case the same realized face is used for bitmap areas and
999 for something displayed in the text (e.g. face `region' on
1000 mono-displays, the fill style may have been changed to
1001 FillSolid in x_draw_glyph_string_background. */
1002 if (face->stipple)
1003 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
1004 else
1005 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
06a2c219
GM
1006 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1007 face->gc,
1008 right,
045dee35 1009 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 1010 row->y)),
110859fc 1011 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
06a2c219 1012 row->visible_height);
dcd08bfb
GM
1013 if (!face->stipple)
1014 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
1015 }
1016
1017 /* Draw the right bitmap. */
1018 if (bitmap != NO_BITMAP)
1019 x_draw_bitmap (w, row, bitmap);
1020}
1021
dc6f92b8 1022\f
06a2c219
GM
1023/***********************************************************************
1024 Line Highlighting
1025 ***********************************************************************/
dc6f92b8 1026
06a2c219
GM
1027/* External interface to control of standout mode. Not used for X
1028 frames. Aborts when called. */
1029
1030static void
dc6f92b8
JB
1031XTreassert_line_highlight (new, vpos)
1032 int new, vpos;
1033{
06a2c219 1034 abort ();
dc6f92b8
JB
1035}
1036
06a2c219
GM
1037
1038/* Call this when about to modify line at position VPOS and change
1039 whether it is highlighted. Not used for X frames. Aborts when
1040 called. */
dc6f92b8 1041
dfcf069d 1042static void
06a2c219
GM
1043x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1044 int new_highlight, vpos, y, first_unused_hpos;
dc6f92b8 1045{
06a2c219 1046 abort ();
dc6f92b8
JB
1047}
1048
06a2c219
GM
1049
1050/* This is called when starting Emacs and when restarting after
1051 suspend. When starting Emacs, no X window is mapped. And nothing
1052 must be done to Emacs's own window if it is suspended (though that
1053 rarely happens). */
dc6f92b8 1054
dfcf069d 1055static void
dc6f92b8
JB
1056XTset_terminal_modes ()
1057{
1058}
1059
06a2c219
GM
1060/* This is called when exiting or suspending Emacs. Exiting will make
1061 the X-windows go away, and suspending requires no action. */
dc6f92b8 1062
dfcf069d 1063static void
dc6f92b8
JB
1064XTreset_terminal_modes ()
1065{
dc6f92b8 1066}
06a2c219
GM
1067
1068
dc6f92b8 1069\f
06a2c219
GM
1070/***********************************************************************
1071 Output Cursor
1072 ***********************************************************************/
1073
1074/* Set the global variable output_cursor to CURSOR. All cursor
1075 positions are relative to updated_window. */
dc6f92b8 1076
dfcf069d 1077static void
06a2c219
GM
1078set_output_cursor (cursor)
1079 struct cursor_pos *cursor;
dc6f92b8 1080{
06a2c219
GM
1081 output_cursor.hpos = cursor->hpos;
1082 output_cursor.vpos = cursor->vpos;
1083 output_cursor.x = cursor->x;
1084 output_cursor.y = cursor->y;
1085}
1086
1087
1088/* Set a nominal cursor position.
dc6f92b8 1089
06a2c219
GM
1090 HPOS and VPOS are column/row positions in a window glyph matrix. X
1091 and Y are window text area relative pixel positions.
1092
1093 If this is done during an update, updated_window will contain the
1094 window that is being updated and the position is the future output
1095 cursor position for that window. If updated_window is null, use
1096 selected_window and display the cursor at the given position. */
1097
1098static void
1099XTcursor_to (vpos, hpos, y, x)
1100 int vpos, hpos, y, x;
1101{
1102 struct window *w;
1103
1104 /* If updated_window is not set, work on selected_window. */
1105 if (updated_window)
1106 w = updated_window;
1107 else
1108 w = XWINDOW (selected_window);
dbcb258a 1109
06a2c219
GM
1110 /* Set the output cursor. */
1111 output_cursor.hpos = hpos;
1112 output_cursor.vpos = vpos;
1113 output_cursor.x = x;
1114 output_cursor.y = y;
dc6f92b8 1115
06a2c219
GM
1116 /* If not called as part of an update, really display the cursor.
1117 This will also set the cursor position of W. */
1118 if (updated_window == NULL)
dc6f92b8
JB
1119 {
1120 BLOCK_INPUT;
06a2c219 1121 x_display_cursor (w, 1, hpos, vpos, x, y);
b86bd3dd 1122 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
dc6f92b8
JB
1123 UNBLOCK_INPUT;
1124 }
1125}
dc43ef94 1126
06a2c219
GM
1127
1128\f
1129/***********************************************************************
1130 Display Iterator
1131 ***********************************************************************/
1132
1133/* Function prototypes of this page. */
1134
1135static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1136 struct glyph *,
ee569018
KH
1137 XChar2b *,
1138 int *));
06a2c219
GM
1139static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1140 int, XChar2b *, int));
1141static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1142static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1143static void x_append_glyph P_ ((struct it *));
b4192550 1144static void x_append_composite_glyph P_ ((struct it *));
06a2c219
GM
1145static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1146 int, int, double));
1147static void x_produce_glyphs P_ ((struct it *));
06a2c219 1148static void x_produce_image_glyph P_ ((struct it *it));
ee569018
KH
1149
1150
e2ef8ee6
GM
1151/* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1152 is not contained in the font. */
dc43ef94 1153
06a2c219 1154static INLINE XCharStruct *
ee569018 1155x_per_char_metric (font, char2b)
06a2c219
GM
1156 XFontStruct *font;
1157 XChar2b *char2b;
1158{
1159 /* The result metric information. */
1160 XCharStruct *pcm = NULL;
dc6f92b8 1161
06a2c219 1162 xassert (font && char2b);
dc6f92b8 1163
06a2c219 1164 if (font->per_char != NULL)
dc6f92b8 1165 {
06a2c219 1166 if (font->min_byte1 == 0 && font->max_byte1 == 0)
dc43ef94 1167 {
06a2c219
GM
1168 /* min_char_or_byte2 specifies the linear character index
1169 corresponding to the first element of the per_char array,
1170 max_char_or_byte2 is the index of the last character. A
1171 character with non-zero CHAR2B->byte1 is not in the font.
1172 A character with byte2 less than min_char_or_byte2 or
1173 greater max_char_or_byte2 is not in the font. */
1174 if (char2b->byte1 == 0
1175 && char2b->byte2 >= font->min_char_or_byte2
1176 && char2b->byte2 <= font->max_char_or_byte2)
1177 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
dc43ef94 1178 }
06a2c219 1179 else
dc6f92b8 1180 {
06a2c219
GM
1181 /* If either min_byte1 or max_byte1 are nonzero, both
1182 min_char_or_byte2 and max_char_or_byte2 are less than
1183 256, and the 2-byte character index values corresponding
1184 to the per_char array element N (counting from 0) are:
1185
1186 byte1 = N/D + min_byte1
1187 byte2 = N\D + min_char_or_byte2
1188
1189 where:
1190
1191 D = max_char_or_byte2 - min_char_or_byte2 + 1
1192 / = integer division
1193 \ = integer modulus */
1194 if (char2b->byte1 >= font->min_byte1
1195 && char2b->byte1 <= font->max_byte1
1196 && char2b->byte2 >= font->min_char_or_byte2
1197 && char2b->byte2 <= font->max_char_or_byte2)
1198 {
1199 pcm = (font->per_char
1200 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1201 * (char2b->byte1 - font->min_byte1))
1202 + (char2b->byte2 - font->min_char_or_byte2));
1203 }
dc6f92b8 1204 }
06a2c219
GM
1205 }
1206 else
1207 {
1208 /* If the per_char pointer is null, all glyphs between the first
1209 and last character indexes inclusive have the same
1210 information, as given by both min_bounds and max_bounds. */
1211 if (char2b->byte2 >= font->min_char_or_byte2
1212 && char2b->byte2 <= font->max_char_or_byte2)
1213 pcm = &font->max_bounds;
1214 }
dc6f92b8 1215
ee569018 1216 return ((pcm == NULL
3e71d8f2 1217 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
ee569018 1218 ? NULL : pcm);
06a2c219 1219}
b73b6aaf 1220
57b03282 1221
06a2c219
GM
1222/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1223 the two-byte form of C. Encoding is returned in *CHAR2B. */
dc43ef94 1224
06a2c219
GM
1225static INLINE void
1226x_encode_char (c, char2b, font_info)
1227 int c;
1228 XChar2b *char2b;
1229 struct font_info *font_info;
1230{
1231 int charset = CHAR_CHARSET (c);
1232 XFontStruct *font = font_info->font;
1233
1234 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1235 This may be either a program in a special encoder language or a
1236 fixed encoding. */
1237 if (font_info->font_encoder)
1238 {
1239 /* It's a program. */
1240 struct ccl_program *ccl = font_info->font_encoder;
1241
1242 if (CHARSET_DIMENSION (charset) == 1)
1243 {
1244 ccl->reg[0] = charset;
1245 ccl->reg[1] = char2b->byte2;
1246 }
1247 else
1248 {
1249 ccl->reg[0] = charset;
1250 ccl->reg[1] = char2b->byte1;
1251 ccl->reg[2] = char2b->byte2;
1252 }
1253
1254 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1255
1256 /* We assume that MSBs are appropriately set/reset by CCL
1257 program. */
1258 if (font->max_byte1 == 0) /* 1-byte font */
ee569018 1259 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
06a2c219
GM
1260 else
1261 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1262 }
1263 else if (font_info->encoding[charset])
1264 {
1265 /* Fixed encoding scheme. See fontset.h for the meaning of the
1266 encoding numbers. */
1267 int enc = font_info->encoding[charset];
1268
1269 if ((enc == 1 || enc == 2)
1270 && CHARSET_DIMENSION (charset) == 2)
1271 char2b->byte1 |= 0x80;
1272
1273 if (enc == 1 || enc == 3)
1274 char2b->byte2 |= 0x80;
1275 }
1276}
1277
1278
1279/* Get face and two-byte form of character C in face FACE_ID on frame
1280 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1281 means we want to display multibyte text. Value is a pointer to a
1282 realized face that is ready for display. */
1283
1284static INLINE struct face *
1285x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1286 struct frame *f;
1287 int c, face_id;
1288 XChar2b *char2b;
1289 int multibyte_p;
1290{
1291 struct face *face = FACE_FROM_ID (f, face_id);
1292
1293 if (!multibyte_p)
1294 {
1295 /* Unibyte case. We don't have to encode, but we have to make
1296 sure to use a face suitable for unibyte. */
1297 char2b->byte1 = 0;
1298 char2b->byte2 = c;
ee569018
KH
1299 face_id = FACE_FOR_CHAR (f, face, c);
1300 face = FACE_FROM_ID (f, face_id);
06a2c219
GM
1301 }
1302 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1303 {
1304 /* Case of ASCII in a face known to fit ASCII. */
1305 char2b->byte1 = 0;
1306 char2b->byte2 = c;
1307 }
1308 else
1309 {
1310 int c1, c2, charset;
1311
1312 /* Split characters into bytes. If c2 is -1 afterwards, C is
1313 really a one-byte character so that byte1 is zero. */
1314 SPLIT_CHAR (c, charset, c1, c2);
1315 if (c2 > 0)
1316 char2b->byte1 = c1, char2b->byte2 = c2;
1317 else
1318 char2b->byte1 = 0, char2b->byte2 = c1;
1319
06a2c219 1320 /* Maybe encode the character in *CHAR2B. */
ee569018 1321 if (face->font != NULL)
06a2c219
GM
1322 {
1323 struct font_info *font_info
1324 = FONT_INFO_FROM_ID (f, face->font_info_id);
1325 if (font_info)
ee569018 1326 x_encode_char (c, char2b, font_info);
06a2c219
GM
1327 }
1328 }
1329
1330 /* Make sure X resources of the face are allocated. */
1331 xassert (face != NULL);
1332 PREPARE_FACE_FOR_DISPLAY (f, face);
1333
1334 return face;
1335}
1336
1337
1338/* Get face and two-byte form of character glyph GLYPH on frame F.
43d120d8 1339 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
06a2c219
GM
1340 a pointer to a realized face that is ready for display. */
1341
1342static INLINE struct face *
ee569018 1343x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
06a2c219
GM
1344 struct frame *f;
1345 struct glyph *glyph;
1346 XChar2b *char2b;
ee569018 1347 int *two_byte_p;
06a2c219
GM
1348{
1349 struct face *face;
1350
1351 xassert (glyph->type == CHAR_GLYPH);
43d120d8 1352 face = FACE_FROM_ID (f, glyph->face_id);
06a2c219 1353
ee569018
KH
1354 if (two_byte_p)
1355 *two_byte_p = 0;
1356
06a2c219
GM
1357 if (!glyph->multibyte_p)
1358 {
1359 /* Unibyte case. We don't have to encode, but we have to make
1360 sure to use a face suitable for unibyte. */
1361 char2b->byte1 = 0;
43d120d8 1362 char2b->byte2 = glyph->u.ch;
06a2c219 1363 }
43d120d8
KH
1364 else if (glyph->u.ch < 128
1365 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
06a2c219
GM
1366 {
1367 /* Case of ASCII in a face known to fit ASCII. */
1368 char2b->byte1 = 0;
43d120d8 1369 char2b->byte2 = glyph->u.ch;
06a2c219
GM
1370 }
1371 else
1372 {
1373 int c1, c2, charset;
1374
1375 /* Split characters into bytes. If c2 is -1 afterwards, C is
1376 really a one-byte character so that byte1 is zero. */
43d120d8 1377 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
06a2c219
GM
1378 if (c2 > 0)
1379 char2b->byte1 = c1, char2b->byte2 = c2;
1380 else
1381 char2b->byte1 = 0, char2b->byte2 = c1;
1382
1383 /* Maybe encode the character in *CHAR2B. */
1384 if (charset != CHARSET_ASCII)
1385 {
1386 struct font_info *font_info
1387 = FONT_INFO_FROM_ID (f, face->font_info_id);
1388 if (font_info)
1389 {
43d120d8 1390 x_encode_char (glyph->u.ch, char2b, font_info);
ee569018
KH
1391 if (two_byte_p)
1392 *two_byte_p
1393 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
06a2c219
GM
1394 }
1395 }
1396 }
1397
1398 /* Make sure X resources of the face are allocated. */
1399 xassert (face != NULL);
1400 PREPARE_FACE_FOR_DISPLAY (f, face);
1401 return face;
1402}
1403
1404
1405/* Store one glyph for IT->char_to_display in IT->glyph_row.
1406 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1407
1408static INLINE void
1409x_append_glyph (it)
1410 struct it *it;
1411{
1412 struct glyph *glyph;
1413 enum glyph_row_area area = it->area;
1414
1415 xassert (it->glyph_row);
1416 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1417
1418 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1419 if (glyph < it->glyph_row->glyphs[area + 1])
1420 {
06a2c219
GM
1421 glyph->charpos = CHARPOS (it->position);
1422 glyph->object = it->object;
88d75730 1423 glyph->pixel_width = it->pixel_width;
06a2c219 1424 glyph->voffset = it->voffset;
88d75730 1425 glyph->type = CHAR_GLYPH;
06a2c219 1426 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1427 glyph->left_box_line_p = it->start_of_box_run_p;
1428 glyph->right_box_line_p = it->end_of_box_run_p;
66ac4b0e
GM
1429 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1430 || it->phys_descent > it->descent);
88d75730 1431 glyph->padding_p = 0;
ee569018 1432 glyph->glyph_not_available_p = it->glyph_not_available_p;
88d75730
GM
1433 glyph->face_id = it->face_id;
1434 glyph->u.ch = it->char_to_display;
06a2c219
GM
1435 ++it->glyph_row->used[area];
1436 }
1437}
1438
b4192550
KH
1439/* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1440 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1441
1442static INLINE void
1443x_append_composite_glyph (it)
1444 struct it *it;
1445{
1446 struct glyph *glyph;
1447 enum glyph_row_area area = it->area;
1448
1449 xassert (it->glyph_row);
1450
1451 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1452 if (glyph < it->glyph_row->glyphs[area + 1])
1453 {
b4192550
KH
1454 glyph->charpos = CHARPOS (it->position);
1455 glyph->object = it->object;
88d75730 1456 glyph->pixel_width = it->pixel_width;
b4192550 1457 glyph->voffset = it->voffset;
88d75730 1458 glyph->type = COMPOSITE_GLYPH;
b4192550 1459 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1460 glyph->left_box_line_p = it->start_of_box_run_p;
1461 glyph->right_box_line_p = it->end_of_box_run_p;
b4192550
KH
1462 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1463 || it->phys_descent > it->descent);
88d75730
GM
1464 glyph->padding_p = 0;
1465 glyph->glyph_not_available_p = 0;
1466 glyph->face_id = it->face_id;
1467 glyph->u.cmp_id = it->cmp_id;
b4192550
KH
1468 ++it->glyph_row->used[area];
1469 }
1470}
1471
06a2c219
GM
1472
1473/* Change IT->ascent and IT->height according to the setting of
1474 IT->voffset. */
1475
1476static INLINE void
1477take_vertical_position_into_account (it)
1478 struct it *it;
1479{
1480 if (it->voffset)
1481 {
1482 if (it->voffset < 0)
1483 /* Increase the ascent so that we can display the text higher
1484 in the line. */
1485 it->ascent += abs (it->voffset);
1486 else
1487 /* Increase the descent so that we can display the text lower
1488 in the line. */
1489 it->descent += it->voffset;
1490 }
1491}
1492
1493
1494/* Produce glyphs/get display metrics for the image IT is loaded with.
1495 See the description of struct display_iterator in dispextern.h for
1496 an overview of struct display_iterator. */
1497
1498static void
1499x_produce_image_glyph (it)
1500 struct it *it;
1501{
1502 struct image *img;
1503 struct face *face;
1504
1505 xassert (it->what == IT_IMAGE);
1506
1507 face = FACE_FROM_ID (it->f, it->face_id);
1508 img = IMAGE_FROM_ID (it->f, it->image_id);
1509 xassert (img);
1510
1511 /* Make sure X resources of the face and image are loaded. */
1512 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1513 prepare_image_for_display (it->f, img);
1514
95af8492 1515 it->ascent = it->phys_ascent = image_ascent (img, face);
22d650b8
GM
1516 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1517 it->pixel_width = img->width + 2 * img->hmargin;
06a2c219
GM
1518
1519 it->nglyphs = 1;
1520
1521 if (face->box != FACE_NO_BOX)
1522 {
ea2ba0d4
KH
1523 if (face->box_line_width > 0)
1524 {
1525 it->ascent += face->box_line_width;
1526 it->descent += face->box_line_width;
1527 }
06a2c219
GM
1528
1529 if (it->start_of_box_run_p)
ea2ba0d4 1530 it->pixel_width += abs (face->box_line_width);
06a2c219 1531 if (it->end_of_box_run_p)
ea2ba0d4 1532 it->pixel_width += abs (face->box_line_width);
06a2c219
GM
1533 }
1534
1535 take_vertical_position_into_account (it);
1536
1537 if (it->glyph_row)
1538 {
1539 struct glyph *glyph;
1540 enum glyph_row_area area = it->area;
1541
1542 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1543 if (glyph < it->glyph_row->glyphs[area + 1])
1544 {
06a2c219
GM
1545 glyph->charpos = CHARPOS (it->position);
1546 glyph->object = it->object;
88d75730 1547 glyph->pixel_width = it->pixel_width;
06a2c219 1548 glyph->voffset = it->voffset;
88d75730 1549 glyph->type = IMAGE_GLYPH;
06a2c219 1550 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1551 glyph->left_box_line_p = it->start_of_box_run_p;
1552 glyph->right_box_line_p = it->end_of_box_run_p;
1553 glyph->overlaps_vertically_p = 0;
1554 glyph->padding_p = 0;
1555 glyph->glyph_not_available_p = 0;
1556 glyph->face_id = it->face_id;
1557 glyph->u.img_id = img->id;
06a2c219
GM
1558 ++it->glyph_row->used[area];
1559 }
1560 }
1561}
1562
1563
1564/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1565 of the glyph, WIDTH and HEIGHT are the width and height of the
1566 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1567 ascent of the glyph (0 <= ASCENT <= 1). */
1568
1569static void
1570x_append_stretch_glyph (it, object, width, height, ascent)
1571 struct it *it;
1572 Lisp_Object object;
1573 int width, height;
1574 double ascent;
1575{
1576 struct glyph *glyph;
1577 enum glyph_row_area area = it->area;
1578
1579 xassert (ascent >= 0 && ascent <= 1);
1580
1581 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1582 if (glyph < it->glyph_row->glyphs[area + 1])
1583 {
06a2c219
GM
1584 glyph->charpos = CHARPOS (it->position);
1585 glyph->object = object;
88d75730 1586 glyph->pixel_width = width;
06a2c219 1587 glyph->voffset = it->voffset;
88d75730 1588 glyph->type = STRETCH_GLYPH;
06a2c219 1589 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1590 glyph->left_box_line_p = it->start_of_box_run_p;
1591 glyph->right_box_line_p = it->end_of_box_run_p;
1592 glyph->overlaps_vertically_p = 0;
1593 glyph->padding_p = 0;
1594 glyph->glyph_not_available_p = 0;
1595 glyph->face_id = it->face_id;
1596 glyph->u.stretch.ascent = height * ascent;
1597 glyph->u.stretch.height = height;
06a2c219
GM
1598 ++it->glyph_row->used[area];
1599 }
1600}
1601
1602
1603/* Produce a stretch glyph for iterator IT. IT->object is the value
1604 of the glyph property displayed. The value must be a list
1605 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1606 being recognized:
1607
1608 1. `:width WIDTH' specifies that the space should be WIDTH *
1609 canonical char width wide. WIDTH may be an integer or floating
1610 point number.
1611
1612 2. `:relative-width FACTOR' specifies that the width of the stretch
1613 should be computed from the width of the first character having the
1614 `glyph' property, and should be FACTOR times that width.
1615
1616 3. `:align-to HPOS' specifies that the space should be wide enough
1617 to reach HPOS, a value in canonical character units.
1618
1619 Exactly one of the above pairs must be present.
1620
1621 4. `:height HEIGHT' specifies that the height of the stretch produced
1622 should be HEIGHT, measured in canonical character units.
1623
1624 5. `:relative-height FACTOR' specifies that the height of the the
1625 stretch should be FACTOR times the height of the characters having
1626 the glyph property.
1627
1628 Either none or exactly one of 4 or 5 must be present.
1629
1630 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1631 of the stretch should be used for the ascent of the stretch.
1632 ASCENT must be in the range 0 <= ASCENT <= 100. */
1633
1634#define NUMVAL(X) \
1635 ((INTEGERP (X) || FLOATP (X)) \
1636 ? XFLOATINT (X) \
1637 : - 1)
1638
1639
1640static void
1641x_produce_stretch_glyph (it)
1642 struct it *it;
1643{
1644 /* (space :width WIDTH :height HEIGHT. */
3e71d8f2
GM
1645#if GLYPH_DEBUG
1646 extern Lisp_Object Qspace;
1647#endif
1648 extern Lisp_Object QCwidth, QCheight, QCascent;
06a2c219
GM
1649 extern Lisp_Object QCrelative_width, QCrelative_height;
1650 extern Lisp_Object QCalign_to;
1651 Lisp_Object prop, plist;
1652 double width = 0, height = 0, ascent = 0;
1653 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1654 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1655
1656 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1657
1658 /* List should start with `space'. */
1659 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1660 plist = XCDR (it->object);
1661
1662 /* Compute the width of the stretch. */
1663 if (prop = Fplist_get (plist, QCwidth),
1664 NUMVAL (prop) > 0)
1665 /* Absolute width `:width WIDTH' specified and valid. */
1666 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1667 else if (prop = Fplist_get (plist, QCrelative_width),
1668 NUMVAL (prop) > 0)
1669 {
1670 /* Relative width `:relative-width FACTOR' specified and valid.
1671 Compute the width of the characters having the `glyph'
1672 property. */
1673 struct it it2;
1674 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1675
1676 it2 = *it;
1677 if (it->multibyte_p)
1678 {
1679 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1680 - IT_BYTEPOS (*it));
1681 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1682 }
1683 else
1684 it2.c = *p, it2.len = 1;
1685
1686 it2.glyph_row = NULL;
1687 it2.what = IT_CHARACTER;
1688 x_produce_glyphs (&it2);
1689 width = NUMVAL (prop) * it2.pixel_width;
1690 }
1691 else if (prop = Fplist_get (plist, QCalign_to),
1692 NUMVAL (prop) > 0)
1693 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1694 else
1695 /* Nothing specified -> width defaults to canonical char width. */
1696 width = CANON_X_UNIT (it->f);
1697
1698 /* Compute height. */
1699 if (prop = Fplist_get (plist, QCheight),
1700 NUMVAL (prop) > 0)
1701 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1702 else if (prop = Fplist_get (plist, QCrelative_height),
1703 NUMVAL (prop) > 0)
1704 height = FONT_HEIGHT (font) * NUMVAL (prop);
1705 else
1706 height = FONT_HEIGHT (font);
1707
1708 /* Compute percentage of height used for ascent. If
1709 `:ascent ASCENT' is present and valid, use that. Otherwise,
1710 derive the ascent from the font in use. */
1711 if (prop = Fplist_get (plist, QCascent),
1712 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1713 ascent = NUMVAL (prop) / 100.0;
1714 else
1715 ascent = (double) font->ascent / FONT_HEIGHT (font);
1716
1717 if (width <= 0)
1718 width = 1;
1719 if (height <= 0)
1720 height = 1;
1721
1722 if (it->glyph_row)
1723 {
1724 Lisp_Object object = it->stack[it->sp - 1].string;
1725 if (!STRINGP (object))
1726 object = it->w->buffer;
1727 x_append_stretch_glyph (it, object, width, height, ascent);
1728 }
1729
1730 it->pixel_width = width;
66ac4b0e
GM
1731 it->ascent = it->phys_ascent = height * ascent;
1732 it->descent = it->phys_descent = height - it->ascent;
06a2c219
GM
1733 it->nglyphs = 1;
1734
1735 if (face->box != FACE_NO_BOX)
1736 {
ea2ba0d4
KH
1737 if (face->box_line_width > 0)
1738 {
1739 it->ascent += face->box_line_width;
1740 it->descent += face->box_line_width;
1741 }
06a2c219
GM
1742
1743 if (it->start_of_box_run_p)
ea2ba0d4 1744 it->pixel_width += abs (face->box_line_width);
06a2c219 1745 if (it->end_of_box_run_p)
ea2ba0d4 1746 it->pixel_width += abs (face->box_line_width);
06a2c219
GM
1747 }
1748
1749 take_vertical_position_into_account (it);
1750}
1751
b4192550
KH
1752/* Return proper value to be used as baseline offset of font that has
1753 ASCENT and DESCENT to draw characters by the font at the vertical
1754 center of the line of frame F.
1755
1756 Here, out task is to find the value of BOFF in the following figure;
1757
1758 -------------------------+-----------+-
1759 -+-+---------+-+ | |
1760 | | | | | |
1761 | | | | F_ASCENT F_HEIGHT
1762 | | | ASCENT | |
1763 HEIGHT | | | | |
1764 | | |-|-+------+-----------|------- baseline
1765 | | | | BOFF | |
1766 | |---------|-+-+ | |
1767 | | | DESCENT | |
1768 -+-+---------+-+ F_DESCENT |
1769 -------------------------+-----------+-
1770
1771 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1772 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1773 DESCENT = FONT->descent
1774 HEIGHT = FONT_HEIGHT (FONT)
1775 F_DESCENT = (F->output_data.x->font->descent
1776 - F->output_data.x->baseline_offset)
1777 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1778*/
1779
458f45fa
KH
1780#define VCENTER_BASELINE_OFFSET(FONT, F) \
1781 ((FONT)->descent \
1782 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1783 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1784 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
06a2c219
GM
1785
1786/* Produce glyphs/get display metrics for the display element IT is
1787 loaded with. See the description of struct display_iterator in
1788 dispextern.h for an overview of struct display_iterator. */
1789
1790static void
1791x_produce_glyphs (it)
1792 struct it *it;
1793{
ee569018
KH
1794 it->glyph_not_available_p = 0;
1795
06a2c219
GM
1796 if (it->what == IT_CHARACTER)
1797 {
1798 XChar2b char2b;
1799 XFontStruct *font;
ee569018 1800 struct face *face = FACE_FROM_ID (it->f, it->face_id);
06a2c219 1801 XCharStruct *pcm;
06a2c219 1802 int font_not_found_p;
b4192550
KH
1803 struct font_info *font_info;
1804 int boff; /* baseline offset */
a4249304
KH
1805 /* We may change it->multibyte_p upon unibyte<->multibyte
1806 conversion. So, save the current value now and restore it
1807 later.
1808
1809 Note: It seems that we don't have to record multibyte_p in
1810 struct glyph because the character code itself tells if or
1811 not the character is multibyte. Thus, in the future, we must
1812 consider eliminating the field `multibyte_p' in the struct
1813 glyph.
1814 */
1815 int saved_multibyte_p = it->multibyte_p;
06a2c219 1816
ee569018
KH
1817 /* Maybe translate single-byte characters to multibyte, or the
1818 other way. */
06a2c219 1819 it->char_to_display = it->c;
ee569018 1820 if (!ASCII_BYTE_P (it->c))
06a2c219 1821 {
ee569018
KH
1822 if (unibyte_display_via_language_environment
1823 && SINGLE_BYTE_CHAR_P (it->c)
1824 && (it->c >= 0240
1825 || !NILP (Vnonascii_translation_table)))
1826 {
1827 it->char_to_display = unibyte_char_to_multibyte (it->c);
a4249304 1828 it->multibyte_p = 1;
ee569018
KH
1829 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1830 face = FACE_FROM_ID (it->f, it->face_id);
1831 }
1832 else if (!SINGLE_BYTE_CHAR_P (it->c)
1833 && !it->multibyte_p)
1834 {
1835 it->char_to_display = multibyte_char_to_unibyte (it->c, Qnil);
a4249304 1836 it->multibyte_p = 0;
ee569018
KH
1837 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1838 face = FACE_FROM_ID (it->f, it->face_id);
1839 }
06a2c219
GM
1840 }
1841
ee569018
KH
1842 /* Get font to use. Encode IT->char_to_display. */
1843 x_get_char_face_and_encoding (it->f, it->char_to_display,
1844 it->face_id, &char2b,
1845 it->multibyte_p);
06a2c219
GM
1846 font = face->font;
1847
1848 /* When no suitable font found, use the default font. */
1849 font_not_found_p = font == NULL;
1850 if (font_not_found_p)
b4192550
KH
1851 {
1852 font = FRAME_FONT (it->f);
1853 boff = it->f->output_data.x->baseline_offset;
1854 font_info = NULL;
1855 }
1856 else
1857 {
1858 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1859 boff = font_info->baseline_offset;
1860 if (font_info->vertical_centering)
1861 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1862 }
06a2c219
GM
1863
1864 if (it->char_to_display >= ' '
1865 && (!it->multibyte_p || it->char_to_display < 128))
1866 {
1867 /* Either unibyte or ASCII. */
1868 int stretched_p;
1869
1870 it->nglyphs = 1;
06a2c219
GM
1871
1872 pcm = x_per_char_metric (font, &char2b);
b4192550
KH
1873 it->ascent = font->ascent + boff;
1874 it->descent = font->descent - boff;
474848ac
GM
1875
1876 if (pcm)
1877 {
1878 it->phys_ascent = pcm->ascent + boff;
1879 it->phys_descent = pcm->descent - boff;
1880 it->pixel_width = pcm->width;
1881 }
1882 else
1883 {
1884 it->glyph_not_available_p = 1;
1885 it->phys_ascent = font->ascent + boff;
1886 it->phys_descent = font->descent - boff;
1887 it->pixel_width = FONT_WIDTH (font);
1888 }
06a2c219
GM
1889
1890 /* If this is a space inside a region of text with
1891 `space-width' property, change its width. */
1892 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1893 if (stretched_p)
1894 it->pixel_width *= XFLOATINT (it->space_width);
1895
1896 /* If face has a box, add the box thickness to the character
1897 height. If character has a box line to the left and/or
1898 right, add the box line width to the character's width. */
1899 if (face->box != FACE_NO_BOX)
1900 {
1901 int thick = face->box_line_width;
1902
ea2ba0d4
KH
1903 if (thick > 0)
1904 {
1905 it->ascent += thick;
1906 it->descent += thick;
1907 }
1908 else
1909 thick = -thick;
1910
06a2c219
GM
1911 if (it->start_of_box_run_p)
1912 it->pixel_width += thick;
1913 if (it->end_of_box_run_p)
1914 it->pixel_width += thick;
1915 }
1916
1917 /* If face has an overline, add the height of the overline
1918 (1 pixel) and a 1 pixel margin to the character height. */
1919 if (face->overline_p)
1920 it->ascent += 2;
1921
1922 take_vertical_position_into_account (it);
1923
1924 /* If we have to actually produce glyphs, do it. */
1925 if (it->glyph_row)
1926 {
1927 if (stretched_p)
1928 {
1929 /* Translate a space with a `space-width' property
1930 into a stretch glyph. */
1931 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1932 x_append_stretch_glyph (it, it->object, it->pixel_width,
1933 it->ascent + it->descent, ascent);
1934 }
1935 else
1936 x_append_glyph (it);
1937
1938 /* If characters with lbearing or rbearing are displayed
1939 in this line, record that fact in a flag of the
1940 glyph row. This is used to optimize X output code. */
1c7e22fd 1941 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
06a2c219
GM
1942 it->glyph_row->contains_overlapping_glyphs_p = 1;
1943 }
1944 }
1945 else if (it->char_to_display == '\n')
1946 {
1947 /* A newline has no width but we need the height of the line. */
1948 it->pixel_width = 0;
1949 it->nglyphs = 0;
b4192550
KH
1950 it->ascent = it->phys_ascent = font->ascent + boff;
1951 it->descent = it->phys_descent = font->descent - boff;
06a2c219 1952
ea2ba0d4
KH
1953 if (face->box != FACE_NO_BOX
1954 && face->box_line_width > 0)
06a2c219 1955 {
ea2ba0d4
KH
1956 it->ascent += face->box_line_width;
1957 it->descent += face->box_line_width;
06a2c219
GM
1958 }
1959 }
1960 else if (it->char_to_display == '\t')
1961 {
1962 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
d365f5bb 1963 int x = it->current_x + it->continuation_lines_width;
06a2c219 1964 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
2a32b5ea
GM
1965
1966 /* If the distance from the current position to the next tab
1967 stop is less than a canonical character width, use the
1968 tab stop after that. */
1969 if (next_tab_x - x < CANON_X_UNIT (it->f))
1970 next_tab_x += tab_width;
06a2c219
GM
1971
1972 it->pixel_width = next_tab_x - x;
1973 it->nglyphs = 1;
b4192550
KH
1974 it->ascent = it->phys_ascent = font->ascent + boff;
1975 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1976
1977 if (it->glyph_row)
1978 {
1979 double ascent = (double) it->ascent / (it->ascent + it->descent);
1980 x_append_stretch_glyph (it, it->object, it->pixel_width,
1981 it->ascent + it->descent, ascent);
1982 }
1983 }
1984 else
1985 {
1986 /* A multi-byte character. Assume that the display width of the
1987 character is the width of the character multiplied by the
b4192550 1988 width of the font. */
06a2c219 1989
b4192550
KH
1990 /* If we found a font, this font should give us the right
1991 metrics. If we didn't find a font, use the frame's
1992 default font and calculate the width of the character
1993 from the charset width; this is what old redisplay code
1994 did. */
1995 pcm = x_per_char_metric (font, &char2b);
ee569018
KH
1996 if (font_not_found_p || !pcm)
1997 {
1998 int charset = CHAR_CHARSET (it->char_to_display);
1999
2000 it->glyph_not_available_p = 1;
2001 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
2002 * CHARSET_WIDTH (charset));
2003 it->phys_ascent = font->ascent + boff;
2004 it->phys_descent = font->descent - boff;
2005 }
2006 else
2007 {
2008 it->pixel_width = pcm->width;
2009 it->phys_ascent = pcm->ascent + boff;
2010 it->phys_descent = pcm->descent - boff;
2011 if (it->glyph_row
2012 && (pcm->lbearing < 0
2013 || pcm->rbearing > pcm->width))
2014 it->glyph_row->contains_overlapping_glyphs_p = 1;
2015 }
b4192550
KH
2016 it->nglyphs = 1;
2017 it->ascent = font->ascent + boff;
2018 it->descent = font->descent - boff;
06a2c219
GM
2019 if (face->box != FACE_NO_BOX)
2020 {
2021 int thick = face->box_line_width;
ea2ba0d4
KH
2022
2023 if (thick > 0)
2024 {
2025 it->ascent += thick;
2026 it->descent += thick;
2027 }
2028 else
2029 thick = - thick;
06a2c219
GM
2030
2031 if (it->start_of_box_run_p)
2032 it->pixel_width += thick;
2033 if (it->end_of_box_run_p)
2034 it->pixel_width += thick;
2035 }
2036
2037 /* If face has an overline, add the height of the overline
2038 (1 pixel) and a 1 pixel margin to the character height. */
2039 if (face->overline_p)
2040 it->ascent += 2;
2041
2042 take_vertical_position_into_account (it);
2043
2044 if (it->glyph_row)
2045 x_append_glyph (it);
2046 }
a4249304 2047 it->multibyte_p = saved_multibyte_p;
06a2c219 2048 }
b4192550
KH
2049 else if (it->what == IT_COMPOSITION)
2050 {
2051 /* Note: A composition is represented as one glyph in the
2052 glyph matrix. There are no padding glyphs. */
2053 XChar2b char2b;
2054 XFontStruct *font;
ee569018 2055 struct face *face = FACE_FROM_ID (it->f, it->face_id);
b4192550
KH
2056 XCharStruct *pcm;
2057 int font_not_found_p;
2058 struct font_info *font_info;
2059 int boff; /* baseline offset */
2060 struct composition *cmp = composition_table[it->cmp_id];
2061
2062 /* Maybe translate single-byte characters to multibyte. */
2063 it->char_to_display = it->c;
2064 if (unibyte_display_via_language_environment
2065 && SINGLE_BYTE_CHAR_P (it->c)
2066 && (it->c >= 0240
2067 || (it->c >= 0200
2068 && !NILP (Vnonascii_translation_table))))
2069 {
2070 it->char_to_display = unibyte_char_to_multibyte (it->c);
b4192550
KH
2071 }
2072
2073 /* Get face and font to use. Encode IT->char_to_display. */
ee569018
KH
2074 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2075 face = FACE_FROM_ID (it->f, it->face_id);
2076 x_get_char_face_and_encoding (it->f, it->char_to_display,
2077 it->face_id, &char2b, it->multibyte_p);
b4192550
KH
2078 font = face->font;
2079
2080 /* When no suitable font found, use the default font. */
2081 font_not_found_p = font == NULL;
2082 if (font_not_found_p)
2083 {
2084 font = FRAME_FONT (it->f);
2085 boff = it->f->output_data.x->baseline_offset;
2086 font_info = NULL;
2087 }
2088 else
2089 {
2090 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2091 boff = font_info->baseline_offset;
2092 if (font_info->vertical_centering)
2093 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2094 }
2095
2096 /* There are no padding glyphs, so there is only one glyph to
2097 produce for the composition. Important is that pixel_width,
2098 ascent and descent are the values of what is drawn by
2099 draw_glyphs (i.e. the values of the overall glyphs composed). */
2100 it->nglyphs = 1;
2101
2102 /* If we have not yet calculated pixel size data of glyphs of
2103 the composition for the current face font, calculate them
2104 now. Theoretically, we have to check all fonts for the
2105 glyphs, but that requires much time and memory space. So,
2106 here we check only the font of the first glyph. This leads
2107 to incorrect display very rarely, and C-l (recenter) can
2108 correct the display anyway. */
2109 if (cmp->font != (void *) font)
2110 {
2111 /* Ascent and descent of the font of the first character of
2112 this composition (adjusted by baseline offset). Ascent
2113 and descent of overall glyphs should not be less than
2114 them respectively. */
2115 int font_ascent = font->ascent + boff;
2116 int font_descent = font->descent - boff;
2117 /* Bounding box of the overall glyphs. */
2118 int leftmost, rightmost, lowest, highest;
329bed06 2119 int i, width, ascent, descent;
b4192550
KH
2120
2121 cmp->font = (void *) font;
2122
2123 /* Initialize the bounding box. */
1bdeec2e
KH
2124 if (font_info
2125 && (pcm = x_per_char_metric (font, &char2b)))
329bed06
GM
2126 {
2127 width = pcm->width;
2128 ascent = pcm->ascent;
2129 descent = pcm->descent;
2130 }
2131 else
2132 {
2133 width = FONT_WIDTH (font);
2134 ascent = font->ascent;
2135 descent = font->descent;
2136 }
2137
2138 rightmost = width;
2139 lowest = - descent + boff;
2140 highest = ascent + boff;
b4192550 2141 leftmost = 0;
329bed06 2142
b4192550
KH
2143 if (font_info
2144 && font_info->default_ascent
2145 && CHAR_TABLE_P (Vuse_default_ascent)
2146 && !NILP (Faref (Vuse_default_ascent,
2147 make_number (it->char_to_display))))
2148 highest = font_info->default_ascent + boff;
2149
2150 /* Draw the first glyph at the normal position. It may be
2151 shifted to right later if some other glyphs are drawn at
2152 the left. */
2153 cmp->offsets[0] = 0;
2154 cmp->offsets[1] = boff;
2155
2156 /* Set cmp->offsets for the remaining glyphs. */
2157 for (i = 1; i < cmp->glyph_len; i++)
2158 {
2159 int left, right, btm, top;
2160 int ch = COMPOSITION_GLYPH (cmp, i);
ee569018
KH
2161 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2162
2163 face = FACE_FROM_ID (it->f, face_id);
2164 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2165 it->multibyte_p);
b4192550
KH
2166 font = face->font;
2167 if (font == NULL)
2168 {
2169 font = FRAME_FONT (it->f);
2170 boff = it->f->output_data.x->baseline_offset;
2171 font_info = NULL;
2172 }
2173 else
2174 {
2175 font_info
2176 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2177 boff = font_info->baseline_offset;
2178 if (font_info->vertical_centering)
2179 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2180 }
2181
1bdeec2e
KH
2182 if (font_info
2183 && (pcm = x_per_char_metric (font, &char2b)))
329bed06
GM
2184 {
2185 width = pcm->width;
2186 ascent = pcm->ascent;
2187 descent = pcm->descent;
2188 }
2189 else
2190 {
2191 width = FONT_WIDTH (font);
1bdeec2e
KH
2192 ascent = 1;
2193 descent = 0;
329bed06 2194 }
b4192550
KH
2195
2196 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2197 {
2198 /* Relative composition with or without
2199 alternate chars. */
329bed06
GM
2200 left = (leftmost + rightmost - width) / 2;
2201 btm = - descent + boff;
b4192550
KH
2202 if (font_info && font_info->relative_compose
2203 && (! CHAR_TABLE_P (Vignore_relative_composition)
2204 || NILP (Faref (Vignore_relative_composition,
2205 make_number (ch)))))
2206 {
2207
329bed06 2208 if (- descent >= font_info->relative_compose)
b4192550
KH
2209 /* One extra pixel between two glyphs. */
2210 btm = highest + 1;
329bed06 2211 else if (ascent <= 0)
b4192550 2212 /* One extra pixel between two glyphs. */
329bed06 2213 btm = lowest - 1 - ascent - descent;
b4192550
KH
2214 }
2215 }
2216 else
2217 {
2218 /* A composition rule is specified by an integer
2219 value that encodes global and new reference
2220 points (GREF and NREF). GREF and NREF are
2221 specified by numbers as below:
2222
2223 0---1---2 -- ascent
2224 | |
2225 | |
2226 | |
2227 9--10--11 -- center
2228 | |
2229 ---3---4---5--- baseline
2230 | |
2231 6---7---8 -- descent
2232 */
2233 int rule = COMPOSITION_RULE (cmp, i);
2234 int gref, nref, grefx, grefy, nrefx, nrefy;
2235
2236 COMPOSITION_DECODE_RULE (rule, gref, nref);
2237 grefx = gref % 3, nrefx = nref % 3;
2238 grefy = gref / 3, nrefy = nref / 3;
2239
2240 left = (leftmost
2241 + grefx * (rightmost - leftmost) / 2
329bed06 2242 - nrefx * width / 2);
b4192550
KH
2243 btm = ((grefy == 0 ? highest
2244 : grefy == 1 ? 0
2245 : grefy == 2 ? lowest
2246 : (highest + lowest) / 2)
329bed06
GM
2247 - (nrefy == 0 ? ascent + descent
2248 : nrefy == 1 ? descent - boff
b4192550 2249 : nrefy == 2 ? 0
329bed06 2250 : (ascent + descent) / 2));
b4192550
KH
2251 }
2252
2253 cmp->offsets[i * 2] = left;
329bed06 2254 cmp->offsets[i * 2 + 1] = btm + descent;
b4192550
KH
2255
2256 /* Update the bounding box of the overall glyphs. */
329bed06
GM
2257 right = left + width;
2258 top = btm + descent + ascent;
b4192550
KH
2259 if (left < leftmost)
2260 leftmost = left;
2261 if (right > rightmost)
2262 rightmost = right;
2263 if (top > highest)
2264 highest = top;
2265 if (btm < lowest)
2266 lowest = btm;
2267 }
2268
2269 /* If there are glyphs whose x-offsets are negative,
2270 shift all glyphs to the right and make all x-offsets
2271 non-negative. */
2272 if (leftmost < 0)
2273 {
2274 for (i = 0; i < cmp->glyph_len; i++)
2275 cmp->offsets[i * 2] -= leftmost;
2276 rightmost -= leftmost;
2277 }
2278
2279 cmp->pixel_width = rightmost;
2280 cmp->ascent = highest;
2281 cmp->descent = - lowest;
2282 if (cmp->ascent < font_ascent)
2283 cmp->ascent = font_ascent;
2284 if (cmp->descent < font_descent)
2285 cmp->descent = font_descent;
2286 }
2287
2288 it->pixel_width = cmp->pixel_width;
2289 it->ascent = it->phys_ascent = cmp->ascent;
2290 it->descent = it->phys_descent = cmp->descent;
2291
2292 if (face->box != FACE_NO_BOX)
2293 {
2294 int thick = face->box_line_width;
ea2ba0d4
KH
2295
2296 if (thick > 0)
2297 {
2298 it->ascent += thick;
2299 it->descent += thick;
2300 }
2301 else
2302 thick = - thick;
b4192550
KH
2303
2304 if (it->start_of_box_run_p)
2305 it->pixel_width += thick;
2306 if (it->end_of_box_run_p)
2307 it->pixel_width += thick;
2308 }
2309
2310 /* If face has an overline, add the height of the overline
2311 (1 pixel) and a 1 pixel margin to the character height. */
2312 if (face->overline_p)
2313 it->ascent += 2;
2314
2315 take_vertical_position_into_account (it);
2316
2317 if (it->glyph_row)
2318 x_append_composite_glyph (it);
2319 }
06a2c219
GM
2320 else if (it->what == IT_IMAGE)
2321 x_produce_image_glyph (it);
2322 else if (it->what == IT_STRETCH)
2323 x_produce_stretch_glyph (it);
2324
3017fdd1
GM
2325 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2326 because this isn't true for images with `:ascent 100'. */
2327 xassert (it->ascent >= 0 && it->descent >= 0);
06a2c219
GM
2328 if (it->area == TEXT_AREA)
2329 it->current_x += it->pixel_width;
66ac4b0e 2330
d365f5bb
GM
2331 it->descent += it->extra_line_spacing;
2332
06a2c219
GM
2333 it->max_ascent = max (it->max_ascent, it->ascent);
2334 it->max_descent = max (it->max_descent, it->descent);
66ac4b0e
GM
2335 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2336 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
06a2c219
GM
2337}
2338
2339
2340/* Estimate the pixel height of the mode or top line on frame F.
2341 FACE_ID specifies what line's height to estimate. */
2342
2343int
2344x_estimate_mode_line_height (f, face_id)
2345 struct frame *f;
2346 enum face_id face_id;
2347{
43281ee3 2348 int height = FONT_HEIGHT (FRAME_FONT (f));
06a2c219
GM
2349
2350 /* This function is called so early when Emacs starts that the face
2351 cache and mode line face are not yet initialized. */
2352 if (FRAME_FACE_CACHE (f))
2353 {
2354 struct face *face = FACE_FROM_ID (f, face_id);
2355 if (face)
43281ee3
GM
2356 {
2357 if (face->font)
2358 height = FONT_HEIGHT (face->font);
ea2ba0d4
KH
2359 if (face->box_line_width > 0)
2360 height += 2 * face->box_line_width;
43281ee3 2361 }
06a2c219
GM
2362 }
2363
2364 return height;
2365}
2366
2367\f
2368/***********************************************************************
2369 Glyph display
2370 ***********************************************************************/
2371
2372/* A sequence of glyphs to be drawn in the same face.
2373
2374 This data structure is not really completely X specific, so it
2375 could possibly, at least partially, be useful for other systems. It
2376 is currently not part of the external redisplay interface because
2377 it's not clear what other systems will need. */
2378
2379struct glyph_string
2380{
2381 /* X-origin of the string. */
2382 int x;
2383
2384 /* Y-origin and y-position of the base line of this string. */
2385 int y, ybase;
2386
2387 /* The width of the string, not including a face extension. */
2388 int width;
2389
2390 /* The width of the string, including a face extension. */
2391 int background_width;
2392
2393 /* The height of this string. This is the height of the line this
2394 string is drawn in, and can be different from the height of the
2395 font the string is drawn in. */
2396 int height;
2397
2398 /* Number of pixels this string overwrites in front of its x-origin.
2399 This number is zero if the string has an lbearing >= 0; it is
2400 -lbearing, if the string has an lbearing < 0. */
2401 int left_overhang;
2402
2403 /* Number of pixels this string overwrites past its right-most
2404 nominal x-position, i.e. x + width. Zero if the string's
2405 rbearing is <= its nominal width, rbearing - width otherwise. */
2406 int right_overhang;
2407
2408 /* The frame on which the glyph string is drawn. */
2409 struct frame *f;
2410
2411 /* The window on which the glyph string is drawn. */
2412 struct window *w;
2413
2414 /* X display and window for convenience. */
2415 Display *display;
2416 Window window;
2417
2418 /* The glyph row for which this string was built. It determines the
2419 y-origin and height of the string. */
2420 struct glyph_row *row;
2421
2422 /* The area within row. */
2423 enum glyph_row_area area;
2424
2425 /* Characters to be drawn, and number of characters. */
2426 XChar2b *char2b;
2427 int nchars;
2428
06a2c219
GM
2429 /* A face-override for drawing cursors, mouse face and similar. */
2430 enum draw_glyphs_face hl;
2431
2432 /* Face in which this string is to be drawn. */
2433 struct face *face;
2434
2435 /* Font in which this string is to be drawn. */
2436 XFontStruct *font;
2437
2438 /* Font info for this string. */
2439 struct font_info *font_info;
2440
b4192550
KH
2441 /* Non-null means this string describes (part of) a composition.
2442 All characters from char2b are drawn composed. */
2443 struct composition *cmp;
06a2c219
GM
2444
2445 /* Index of this glyph string's first character in the glyph
b4192550
KH
2446 definition of CMP. If this is zero, this glyph string describes
2447 the first character of a composition. */
06a2c219
GM
2448 int gidx;
2449
2450 /* 1 means this glyph strings face has to be drawn to the right end
2451 of the window's drawing area. */
2452 unsigned extends_to_end_of_line_p : 1;
2453
2454 /* 1 means the background of this string has been drawn. */
2455 unsigned background_filled_p : 1;
2456
2457 /* 1 means glyph string must be drawn with 16-bit functions. */
2458 unsigned two_byte_p : 1;
2459
2460 /* 1 means that the original font determined for drawing this glyph
2461 string could not be loaded. The member `font' has been set to
2462 the frame's default font in this case. */
2463 unsigned font_not_found_p : 1;
2464
2465 /* 1 means that the face in which this glyph string is drawn has a
2466 stipple pattern. */
2467 unsigned stippled_p : 1;
2468
66ac4b0e
GM
2469 /* 1 means only the foreground of this glyph string must be drawn,
2470 and we should use the physical height of the line this glyph
2471 string appears in as clip rect. */
2472 unsigned for_overlaps_p : 1;
2473
06a2c219
GM
2474 /* The GC to use for drawing this glyph string. */
2475 GC gc;
2476
2477 /* A pointer to the first glyph in the string. This glyph
2478 corresponds to char2b[0]. Needed to draw rectangles if
2479 font_not_found_p is 1. */
2480 struct glyph *first_glyph;
2481
2482 /* Image, if any. */
2483 struct image *img;
2484
2485 struct glyph_string *next, *prev;
2486};
2487
2488
5c187dee 2489#if 0
06a2c219
GM
2490
2491static void
2492x_dump_glyph_string (s)
2493 struct glyph_string *s;
2494{
2495 fprintf (stderr, "glyph string\n");
2496 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2497 s->x, s->y, s->width, s->height);
2498 fprintf (stderr, " ybase = %d\n", s->ybase);
2499 fprintf (stderr, " hl = %d\n", s->hl);
2500 fprintf (stderr, " left overhang = %d, right = %d\n",
2501 s->left_overhang, s->right_overhang);
2502 fprintf (stderr, " nchars = %d\n", s->nchars);
2503 fprintf (stderr, " extends to end of line = %d\n",
2504 s->extends_to_end_of_line_p);
2505 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2506 fprintf (stderr, " bg width = %d\n", s->background_width);
2507}
2508
2509#endif /* GLYPH_DEBUG */
2510
2511
2512
2513static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2514 struct glyph_string **,
2515 struct glyph_string *,
2516 struct glyph_string *));
2517static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2518 struct glyph_string **,
2519 struct glyph_string *,
2520 struct glyph_string *));
2521static void x_append_glyph_string P_ ((struct glyph_string **,
2522 struct glyph_string **,
2523 struct glyph_string *));
2524static int x_left_overwritten P_ ((struct glyph_string *));
2525static int x_left_overwriting P_ ((struct glyph_string *));
2526static int x_right_overwritten P_ ((struct glyph_string *));
2527static int x_right_overwriting P_ ((struct glyph_string *));
66ac4b0e
GM
2528static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2529 int));
06a2c219
GM
2530static void x_init_glyph_string P_ ((struct glyph_string *,
2531 XChar2b *, struct window *,
2532 struct glyph_row *,
2533 enum glyph_row_area, int,
2534 enum draw_glyphs_face));
2535static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2536 enum glyph_row_area, int, int,
66ac4b0e 2537 enum draw_glyphs_face, int *, int *, int));
06a2c219
GM
2538static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2539static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2540static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2541 int));
2542static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
b4192550 2543static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
06a2c219
GM
2544static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2545static void x_draw_glyph_string P_ ((struct glyph_string *));
2546static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2547static void x_set_cursor_gc P_ ((struct glyph_string *));
2548static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2549static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2550static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2551 int *, int *));
2552static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2553static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
68c45bf0 2554 unsigned long *, double, int));
06a2c219 2555static void x_setup_relief_color P_ ((struct frame *, struct relief *,
68c45bf0 2556 double, int, unsigned long));
06a2c219
GM
2557static void x_setup_relief_colors P_ ((struct glyph_string *));
2558static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2559static void x_draw_image_relief P_ ((struct glyph_string *));
2560static void x_draw_image_foreground P_ ((struct glyph_string *));
2561static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2562static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2563static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2564 int, int, int));
2565static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2566 int, int, int, int, XRectangle *));
2567static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2568 int, int, int, XRectangle *));
66ac4b0e
GM
2569static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2570 enum glyph_row_area));
209f68d9
GM
2571static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2572 struct glyph_row *,
2573 enum glyph_row_area, int, int));
06a2c219 2574
163dcff3
GM
2575#if GLYPH_DEBUG
2576static void x_check_font P_ ((struct frame *, XFontStruct *));
2577#endif
2578
06a2c219 2579
06a2c219
GM
2580/* Append the list of glyph strings with head H and tail T to the list
2581 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2582
2583static INLINE void
2584x_append_glyph_string_lists (head, tail, h, t)
2585 struct glyph_string **head, **tail;
2586 struct glyph_string *h, *t;
2587{
2588 if (h)
2589 {
2590 if (*head)
2591 (*tail)->next = h;
2592 else
2593 *head = h;
2594 h->prev = *tail;
2595 *tail = t;
2596 }
2597}
2598
2599
2600/* Prepend the list of glyph strings with head H and tail T to the
2601 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2602 result. */
2603
2604static INLINE void
2605x_prepend_glyph_string_lists (head, tail, h, t)
2606 struct glyph_string **head, **tail;
2607 struct glyph_string *h, *t;
2608{
2609 if (h)
2610 {
2611 if (*head)
2612 (*head)->prev = t;
2613 else
2614 *tail = t;
2615 t->next = *head;
2616 *head = h;
2617 }
2618}
2619
2620
2621/* Append glyph string S to the list with head *HEAD and tail *TAIL.
2622 Set *HEAD and *TAIL to the resulting list. */
2623
2624static INLINE void
2625x_append_glyph_string (head, tail, s)
2626 struct glyph_string **head, **tail;
2627 struct glyph_string *s;
2628{
2629 s->next = s->prev = NULL;
2630 x_append_glyph_string_lists (head, tail, s, s);
2631}
2632
2633
2634/* Set S->gc to a suitable GC for drawing glyph string S in cursor
2635 face. */
2636
2637static void
2638x_set_cursor_gc (s)
2639 struct glyph_string *s;
2640{
2641 if (s->font == FRAME_FONT (s->f)
2642 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2643 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
b4192550 2644 && !s->cmp)
06a2c219
GM
2645 s->gc = s->f->output_data.x->cursor_gc;
2646 else
2647 {
2648 /* Cursor on non-default face: must merge. */
2649 XGCValues xgcv;
2650 unsigned long mask;
2651
2652 xgcv.background = s->f->output_data.x->cursor_pixel;
2653 xgcv.foreground = s->face->background;
2654
2655 /* If the glyph would be invisible, try a different foreground. */
2656 if (xgcv.foreground == xgcv.background)
2657 xgcv.foreground = s->face->foreground;
2658 if (xgcv.foreground == xgcv.background)
2659 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2660 if (xgcv.foreground == xgcv.background)
2661 xgcv.foreground = s->face->foreground;
2662
2663 /* Make sure the cursor is distinct from text in this face. */
2664 if (xgcv.background == s->face->background
2665 && xgcv.foreground == s->face->foreground)
2666 {
2667 xgcv.background = s->face->foreground;
2668 xgcv.foreground = s->face->background;
2669 }
2670
2671 IF_DEBUG (x_check_font (s->f, s->font));
2672 xgcv.font = s->font->fid;
2673 xgcv.graphics_exposures = False;
2674 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2675
2676 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2677 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2678 mask, &xgcv);
2679 else
2680 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2681 = XCreateGC (s->display, s->window, mask, &xgcv);
2682
2683 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2684 }
2685}
2686
2687
2688/* Set up S->gc of glyph string S for drawing text in mouse face. */
2689
2690static void
2691x_set_mouse_face_gc (s)
2692 struct glyph_string *s;
2693{
2694 int face_id;
ee569018 2695 struct face *face;
06a2c219 2696
e4ded23c 2697 /* What face has to be used last for the mouse face? */
06a2c219 2698 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
ee569018 2699 face = FACE_FROM_ID (s->f, face_id);
e4ded23c
GM
2700 if (face == NULL)
2701 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2702
033e3e18
GM
2703 if (s->first_glyph->type == CHAR_GLYPH)
2704 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2705 else
2706 face_id = FACE_FOR_CHAR (s->f, face, 0);
06a2c219
GM
2707 s->face = FACE_FROM_ID (s->f, face_id);
2708 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2709
2710 /* If font in this face is same as S->font, use it. */
2711 if (s->font == s->face->font)
2712 s->gc = s->face->gc;
2713 else
2714 {
2715 /* Otherwise construct scratch_cursor_gc with values from FACE
2716 but font FONT. */
2717 XGCValues xgcv;
2718 unsigned long mask;
2719
2720 xgcv.background = s->face->background;
2721 xgcv.foreground = s->face->foreground;
2722 IF_DEBUG (x_check_font (s->f, s->font));
2723 xgcv.font = s->font->fid;
2724 xgcv.graphics_exposures = False;
2725 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2726
2727 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2728 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2729 mask, &xgcv);
2730 else
2731 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2732 = XCreateGC (s->display, s->window, mask, &xgcv);
2733
2734 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2735 }
2736
2737 xassert (s->gc != 0);
2738}
2739
2740
2741/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2742 Faces to use in the mode line have already been computed when the
2743 matrix was built, so there isn't much to do, here. */
2744
2745static INLINE void
2746x_set_mode_line_face_gc (s)
2747 struct glyph_string *s;
2748{
2749 s->gc = s->face->gc;
06a2c219
GM
2750}
2751
2752
2753/* Set S->gc of glyph string S for drawing that glyph string. Set
2754 S->stippled_p to a non-zero value if the face of S has a stipple
2755 pattern. */
2756
2757static INLINE void
2758x_set_glyph_string_gc (s)
2759 struct glyph_string *s;
2760{
209f68d9
GM
2761 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2762
06a2c219
GM
2763 if (s->hl == DRAW_NORMAL_TEXT)
2764 {
2765 s->gc = s->face->gc;
2766 s->stippled_p = s->face->stipple != 0;
2767 }
2768 else if (s->hl == DRAW_INVERSE_VIDEO)
2769 {
2770 x_set_mode_line_face_gc (s);
2771 s->stippled_p = s->face->stipple != 0;
2772 }
2773 else if (s->hl == DRAW_CURSOR)
2774 {
2775 x_set_cursor_gc (s);
2776 s->stippled_p = 0;
2777 }
2778 else if (s->hl == DRAW_MOUSE_FACE)
2779 {
2780 x_set_mouse_face_gc (s);
2781 s->stippled_p = s->face->stipple != 0;
2782 }
2783 else if (s->hl == DRAW_IMAGE_RAISED
2784 || s->hl == DRAW_IMAGE_SUNKEN)
2785 {
2786 s->gc = s->face->gc;
2787 s->stippled_p = s->face->stipple != 0;
2788 }
2789 else
2790 {
2791 s->gc = s->face->gc;
2792 s->stippled_p = s->face->stipple != 0;
2793 }
2794
2795 /* GC must have been set. */
2796 xassert (s->gc != 0);
2797}
2798
2799
2800/* Return in *R the clipping rectangle for glyph string S. */
2801
2802static void
2803x_get_glyph_string_clip_rect (s, r)
2804 struct glyph_string *s;
2805 XRectangle *r;
2806{
2807 if (s->row->full_width_p)
2808 {
2809 /* Draw full-width. X coordinates are relative to S->w->left. */
1da3fd71
GM
2810 int canon_x = CANON_X_UNIT (s->f);
2811
2812 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2813 r->width = XFASTINT (s->w->width) * canon_x;
06a2c219
GM
2814
2815 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2816 {
1da3fd71 2817 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
06a2c219
GM
2818 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2819 r->x -= width;
2820 }
2821
b9432a85 2822 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
1da3fd71 2823
06a2c219
GM
2824 /* Unless displaying a mode or menu bar line, which are always
2825 fully visible, clip to the visible part of the row. */
2826 if (s->w->pseudo_window_p)
2827 r->height = s->row->visible_height;
2828 else
2829 r->height = s->height;
2830 }
2831 else
2832 {
2833 /* This is a text line that may be partially visible. */
2834 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2835 r->width = window_box_width (s->w, s->area);
2836 r->height = s->row->visible_height;
2837 }
2838
2839 /* Don't use S->y for clipping because it doesn't take partially
2840 visible lines into account. For example, it can be negative for
2841 partially visible lines at the top of a window. */
2842 if (!s->row->full_width_p
2843 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
045dee35 2844 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
06a2c219
GM
2845 else
2846 r->y = max (0, s->row->y);
06a2c219 2847
9ea173e8 2848 /* If drawing a tool-bar window, draw it over the internal border
06a2c219 2849 at the top of the window. */
9ea173e8 2850 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219 2851 r->y -= s->f->output_data.x->internal_border_width;
66ac4b0e
GM
2852
2853 /* If S draws overlapping rows, it's sufficient to use the top and
2854 bottom of the window for clipping because this glyph string
2855 intentionally draws over other lines. */
2856 if (s->for_overlaps_p)
2857 {
045dee35 2858 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
66ac4b0e
GM
2859 r->height = window_text_bottom_y (s->w) - r->y;
2860 }
2861
2862 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
06a2c219
GM
2863}
2864
2865
2866/* Set clipping for output of glyph string S. S may be part of a mode
2867 line or menu if we don't have X toolkit support. */
2868
2869static INLINE void
2870x_set_glyph_string_clipping (s)
2871 struct glyph_string *s;
2872{
2873 XRectangle r;
2874 x_get_glyph_string_clip_rect (s, &r);
2875 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2876}
2877
2878
2879/* Compute left and right overhang of glyph string S. If S is a glyph
b4192550 2880 string for a composition, assume overhangs don't exist. */
06a2c219
GM
2881
2882static INLINE void
2883x_compute_glyph_string_overhangs (s)
2884 struct glyph_string *s;
2885{
b4192550 2886 if (s->cmp == NULL
06a2c219
GM
2887 && s->first_glyph->type == CHAR_GLYPH)
2888 {
2889 XCharStruct cs;
2890 int direction, font_ascent, font_descent;
2891 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2892 &font_ascent, &font_descent, &cs);
2893 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2894 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2895 }
2896}
2897
2898
2899/* Compute overhangs and x-positions for glyph string S and its
2900 predecessors, or successors. X is the starting x-position for S.
2901 BACKWARD_P non-zero means process predecessors. */
2902
2903static void
2904x_compute_overhangs_and_x (s, x, backward_p)
2905 struct glyph_string *s;
2906 int x;
2907 int backward_p;
2908{
2909 if (backward_p)
2910 {
2911 while (s)
2912 {
2913 x_compute_glyph_string_overhangs (s);
2914 x -= s->width;
2915 s->x = x;
2916 s = s->prev;
2917 }
2918 }
2919 else
2920 {
2921 while (s)
2922 {
2923 x_compute_glyph_string_overhangs (s);
2924 s->x = x;
2925 x += s->width;
2926 s = s->next;
2927 }
2928 }
2929}
2930
2931
2932/* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
b4192550
KH
2933 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2934 assumed to be zero. */
06a2c219
GM
2935
2936static void
2937x_get_glyph_overhangs (glyph, f, left, right)
2938 struct glyph *glyph;
2939 struct frame *f;
2940 int *left, *right;
2941{
06a2c219
GM
2942 *left = *right = 0;
2943
b4192550 2944 if (glyph->type == CHAR_GLYPH)
06a2c219
GM
2945 {
2946 XFontStruct *font;
2947 struct face *face;
2948 struct font_info *font_info;
2949 XChar2b char2b;
ee569018
KH
2950 XCharStruct *pcm;
2951
2952 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
06a2c219
GM
2953 font = face->font;
2954 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
ee569018
KH
2955 if (font
2956 && (pcm = x_per_char_metric (font, &char2b)))
06a2c219 2957 {
06a2c219
GM
2958 if (pcm->rbearing > pcm->width)
2959 *right = pcm->rbearing - pcm->width;
2960 if (pcm->lbearing < 0)
2961 *left = -pcm->lbearing;
2962 }
2963 }
2964}
2965
2966
2967/* Return the index of the first glyph preceding glyph string S that
2968 is overwritten by S because of S's left overhang. Value is -1
2969 if no glyphs are overwritten. */
2970
2971static int
2972x_left_overwritten (s)
2973 struct glyph_string *s;
2974{
2975 int k;
2976
2977 if (s->left_overhang)
2978 {
2979 int x = 0, i;
2980 struct glyph *glyphs = s->row->glyphs[s->area];
2981 int first = s->first_glyph - glyphs;
2982
2983 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2984 x -= glyphs[i].pixel_width;
2985
2986 k = i + 1;
2987 }
2988 else
2989 k = -1;
2990
2991 return k;
2992}
2993
2994
2995/* Return the index of the first glyph preceding glyph string S that
2996 is overwriting S because of its right overhang. Value is -1 if no
2997 glyph in front of S overwrites S. */
2998
2999static int
3000x_left_overwriting (s)
3001 struct glyph_string *s;
3002{
3003 int i, k, x;
3004 struct glyph *glyphs = s->row->glyphs[s->area];
3005 int first = s->first_glyph - glyphs;
3006
3007 k = -1;
3008 x = 0;
3009 for (i = first - 1; i >= 0; --i)
3010 {
3011 int left, right;
3012 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3013 if (x + right > 0)
3014 k = i;
3015 x -= glyphs[i].pixel_width;
3016 }
3017
3018 return k;
3019}
3020
3021
3022/* Return the index of the last glyph following glyph string S that is
3023 not overwritten by S because of S's right overhang. Value is -1 if
3024 no such glyph is found. */
3025
3026static int
3027x_right_overwritten (s)
3028 struct glyph_string *s;
3029{
3030 int k = -1;
3031
3032 if (s->right_overhang)
3033 {
3034 int x = 0, i;
3035 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 3036 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
3037 int end = s->row->used[s->area];
3038
3039 for (i = first; i < end && s->right_overhang > x; ++i)
3040 x += glyphs[i].pixel_width;
3041
3042 k = i;
3043 }
3044
3045 return k;
3046}
3047
3048
3049/* Return the index of the last glyph following glyph string S that
3050 overwrites S because of its left overhang. Value is negative
3051 if no such glyph is found. */
3052
3053static int
3054x_right_overwriting (s)
3055 struct glyph_string *s;
3056{
3057 int i, k, x;
3058 int end = s->row->used[s->area];
3059 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 3060 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
3061
3062 k = -1;
3063 x = 0;
3064 for (i = first; i < end; ++i)
3065 {
3066 int left, right;
3067 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3068 if (x - left < 0)
3069 k = i;
3070 x += glyphs[i].pixel_width;
3071 }
3072
3073 return k;
3074}
3075
3076
3077/* Fill rectangle X, Y, W, H with background color of glyph string S. */
3078
3079static INLINE void
3080x_clear_glyph_string_rect (s, x, y, w, h)
3081 struct glyph_string *s;
3082 int x, y, w, h;
3083{
3084 XGCValues xgcv;
3085 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3086 XSetForeground (s->display, s->gc, xgcv.background);
3087 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3088 XSetForeground (s->display, s->gc, xgcv.foreground);
3089}
3090
3091
3092/* Draw the background of glyph_string S. If S->background_filled_p
3093 is non-zero don't draw it. FORCE_P non-zero means draw the
3094 background even if it wouldn't be drawn normally. This is used
b4192550
KH
3095 when a string preceding S draws into the background of S, or S
3096 contains the first component of a composition. */
06a2c219
GM
3097
3098static void
3099x_draw_glyph_string_background (s, force_p)
3100 struct glyph_string *s;
3101 int force_p;
3102{
3103 /* Nothing to do if background has already been drawn or if it
3104 shouldn't be drawn in the first place. */
3105 if (!s->background_filled_p)
3106 {
ea2ba0d4
KH
3107 int box_line_width = max (s->face->box_line_width, 0);
3108
b4192550 3109 if (s->stippled_p)
06a2c219
GM
3110 {
3111 /* Fill background with a stipple pattern. */
3112 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3113 XFillRectangle (s->display, s->window, s->gc, s->x,
ea2ba0d4 3114 s->y + box_line_width,
06a2c219 3115 s->background_width,
ea2ba0d4 3116 s->height - 2 * box_line_width);
06a2c219
GM
3117 XSetFillStyle (s->display, s->gc, FillSolid);
3118 s->background_filled_p = 1;
3119 }
ea2ba0d4 3120 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
06a2c219
GM
3121 || s->font_not_found_p
3122 || s->extends_to_end_of_line_p
06a2c219
GM
3123 || force_p)
3124 {
ea2ba0d4 3125 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
06a2c219 3126 s->background_width,
ea2ba0d4 3127 s->height - 2 * box_line_width);
06a2c219
GM
3128 s->background_filled_p = 1;
3129 }
3130 }
3131}
3132
3133
3134/* Draw the foreground of glyph string S. */
3135
3136static void
3137x_draw_glyph_string_foreground (s)
3138 struct glyph_string *s;
3139{
3140 int i, x;
3141
3142 /* If first glyph of S has a left box line, start drawing the text
3143 of S to the right of that box line. */
3144 if (s->face->box != FACE_NO_BOX
3145 && s->first_glyph->left_box_line_p)
ea2ba0d4 3146 x = s->x + abs (s->face->box_line_width);
06a2c219
GM
3147 else
3148 x = s->x;
3149
b4192550
KH
3150 /* Draw characters of S as rectangles if S's font could not be
3151 loaded. */
3152 if (s->font_not_found_p)
06a2c219 3153 {
b4192550 3154 for (i = 0; i < s->nchars; ++i)
06a2c219 3155 {
b4192550
KH
3156 struct glyph *g = s->first_glyph + i;
3157 XDrawRectangle (s->display, s->window,
3158 s->gc, x, s->y, g->pixel_width - 1,
3159 s->height - 1);
3160 x += g->pixel_width;
06a2c219
GM
3161 }
3162 }
3163 else
3164 {
b4192550
KH
3165 char *char1b = (char *) s->char2b;
3166 int boff = s->font_info->baseline_offset;
06a2c219 3167
b4192550
KH
3168 if (s->font_info->vertical_centering)
3169 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3170
3171 /* If we can use 8-bit functions, condense S->char2b. */
3172 if (!s->two_byte_p)
3173 for (i = 0; i < s->nchars; ++i)
3174 char1b[i] = s->char2b[i].byte2;
3175
3176 /* Draw text with XDrawString if background has already been
3177 filled. Otherwise, use XDrawImageString. (Note that
3178 XDrawImageString is usually faster than XDrawString.) Always
3179 use XDrawImageString when drawing the cursor so that there is
3180 no chance that characters under a box cursor are invisible. */
3181 if (s->for_overlaps_p
3182 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3183 {
3184 /* Draw characters with 16-bit or 8-bit functions. */
3185 if (s->two_byte_p)
3186 XDrawString16 (s->display, s->window, s->gc, x,
3187 s->ybase - boff, s->char2b, s->nchars);
3188 else
3189 XDrawString (s->display, s->window, s->gc, x,
3190 s->ybase - boff, char1b, s->nchars);
3191 }
06a2c219
GM
3192 else
3193 {
b4192550
KH
3194 if (s->two_byte_p)
3195 XDrawImageString16 (s->display, s->window, s->gc, x,
3196 s->ybase - boff, s->char2b, s->nchars);
06a2c219 3197 else
b4192550
KH
3198 XDrawImageString (s->display, s->window, s->gc, x,
3199 s->ybase - boff, char1b, s->nchars);
3200 }
3201 }
3202}
06a2c219 3203
b4192550 3204/* Draw the foreground of composite glyph string S. */
06a2c219 3205
b4192550
KH
3206static void
3207x_draw_composite_glyph_string_foreground (s)
3208 struct glyph_string *s;
3209{
3210 int i, x;
06a2c219 3211
b4192550
KH
3212 /* If first glyph of S has a left box line, start drawing the text
3213 of S to the right of that box line. */
3214 if (s->face->box != FACE_NO_BOX
3215 && s->first_glyph->left_box_line_p)
ea2ba0d4 3216 x = s->x + abs (s->face->box_line_width);
b4192550
KH
3217 else
3218 x = s->x;
06a2c219 3219
b4192550
KH
3220 /* S is a glyph string for a composition. S->gidx is the index of
3221 the first character drawn for glyphs of this composition.
3222 S->gidx == 0 means we are drawing the very first character of
3223 this composition. */
06a2c219 3224
b4192550
KH
3225 /* Draw a rectangle for the composition if the font for the very
3226 first character of the composition could not be loaded. */
3227 if (s->font_not_found_p)
3228 {
3229 if (s->gidx == 0)
3230 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3231 s->width - 1, s->height - 1);
3232 }
3233 else
3234 {
3235 for (i = 0; i < s->nchars; i++, ++s->gidx)
3236 XDrawString16 (s->display, s->window, s->gc,
3237 x + s->cmp->offsets[s->gidx * 2],
3238 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3239 s->char2b + i, 1);
06a2c219
GM
3240 }
3241}
3242
3243
80c32bcc
GM
3244#ifdef USE_X_TOOLKIT
3245
3e71d8f2 3246static struct frame *x_frame_of_widget P_ ((Widget));
80c32bcc 3247
3e71d8f2
GM
3248
3249/* Return the frame on which widget WIDGET is used.. Abort if frame
3250 cannot be determined. */
3251
e851c833 3252static struct frame *
3e71d8f2 3253x_frame_of_widget (widget)
80c32bcc 3254 Widget widget;
80c32bcc 3255{
80c32bcc 3256 struct x_display_info *dpyinfo;
5c187dee 3257 Lisp_Object tail;
3e71d8f2
GM
3258 struct frame *f;
3259
80c32bcc
GM
3260 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3261
3262 /* Find the top-level shell of the widget. Note that this function
3263 can be called when the widget is not yet realized, so XtWindow
3264 (widget) == 0. That's the reason we can't simply use
3265 x_any_window_to_frame. */
3266 while (!XtIsTopLevelShell (widget))
3267 widget = XtParent (widget);
3268
3269 /* Look for a frame with that top-level widget. Allocate the color
3270 on that frame to get the right gamma correction value. */
3271 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3272 if (GC_FRAMEP (XCAR (tail))
3273 && (f = XFRAME (XCAR (tail)),
3274 (f->output_data.nothing != 1
3275 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3276 && f->output_data.x->widget == widget)
3e71d8f2 3277 return f;
80c32bcc
GM
3278
3279 abort ();
3280}
3281
3e71d8f2
GM
3282
3283/* Allocate the color COLOR->pixel on the screen and display of
3284 widget WIDGET in colormap CMAP. If an exact match cannot be
3285 allocated, try the nearest color available. Value is non-zero
3286 if successful. This is called from lwlib. */
3287
3288int
3289x_alloc_nearest_color_for_widget (widget, cmap, color)
3290 Widget widget;
3291 Colormap cmap;
3292 XColor *color;
3293{
3294 struct frame *f = x_frame_of_widget (widget);
3295 return x_alloc_nearest_color (f, cmap, color);
3296}
3297
3298
46d516e5
MB
3299/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3300 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3301 If this produces the same color as PIXEL, try a color where all RGB
3302 values have DELTA added. Return the allocated color in *PIXEL.
3303 DISPLAY is the X display, CMAP is the colormap to operate on.
3304 Value is non-zero if successful. */
3305
3306int
3307x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3308 Widget widget;
3309 Display *display;
3310 Colormap cmap;
3311 unsigned long *pixel;
3312 double factor;
3313 int delta;
3314{
3315 struct frame *f = x_frame_of_widget (widget);
3316 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3317}
3318
3319
80c32bcc
GM
3320#endif /* USE_X_TOOLKIT */
3321
3322
f04e1297
GM
3323/* Value is an array of XColor structures for the contents of the
3324 color map of frame F. Set *NCELLS to the size of the array.
3325 Note that this probably shouldn't be called for large color maps,
3326 say a 24-bit TrueColor map. */
3327
3328static const XColor *
3329x_color_cells (f, ncells)
3330 struct frame *f;
3331 int *ncells;
3332{
3333 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3334
3335 if (dpyinfo->color_cells == NULL)
3336 {
3337 Display *display = FRAME_X_DISPLAY (f);
3338 Screen *screen = FRAME_X_SCREEN (f);
3339 int i;
3340
3341 dpyinfo->ncolor_cells
3342 = XDisplayCells (display, XScreenNumberOfScreen (screen));
3343 dpyinfo->color_cells
3344 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3345 * sizeof *dpyinfo->color_cells);
3346
3347 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3348 dpyinfo->color_cells[i].pixel = i;
3349
3350 XQueryColors (display, FRAME_X_COLORMAP (f),
3351 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3352 }
3353
3354 *ncells = dpyinfo->ncolor_cells;
3355 return dpyinfo->color_cells;
3356}
3357
3358
3359/* On frame F, translate pixel colors to RGB values for the NCOLORS
3360 colors in COLORS. Use cached information, if available. */
3361
3362void
3363x_query_colors (f, colors, ncolors)
3364 struct frame *f;
3365 XColor *colors;
3366 int ncolors;
3367{
3368 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3369
3370 if (dpyinfo->color_cells)
3371 {
3372 int i;
3373 for (i = 0; i < ncolors; ++i)
3374 {
3375 unsigned long pixel = colors[i].pixel;
3376 xassert (pixel < dpyinfo->ncolor_cells);
3377 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3378 colors[i] = dpyinfo->color_cells[pixel];
3379 }
3380 }
3381 else
3382 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3383}
3384
3385
3386/* On frame F, translate pixel color to RGB values for the color in
3387 COLOR. Use cached information, if available. */
3388
3389void
3390x_query_color (f, color)
3391 struct frame *f;
3392 XColor *color;
3393{
3394 x_query_colors (f, color, 1);
3395}
3396
3397
06a2c219
GM
3398/* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3399 CMAP. If an exact match can't be allocated, try the nearest color
3400 available. Value is non-zero if successful. Set *COLOR to the
3401 color allocated. */
3402
3403int
80c32bcc
GM
3404x_alloc_nearest_color (f, cmap, color)
3405 struct frame *f;
06a2c219
GM
3406 Colormap cmap;
3407 XColor *color;
3408{
80c32bcc
GM
3409 Display *display = FRAME_X_DISPLAY (f);
3410 Screen *screen = FRAME_X_SCREEN (f);
3411 int rc;
3412
3413 gamma_correct (f, color);
3414 rc = XAllocColor (display, cmap, color);
06a2c219
GM
3415 if (rc == 0)
3416 {
3417 /* If we got to this point, the colormap is full, so we're going
3418 to try to get the next closest color. The algorithm used is
3419 a least-squares matching, which is what X uses for closest
3420 color matching with StaticColor visuals. */
3421 int nearest, i;
3422 unsigned long nearest_delta = ~0;
f04e1297
GM
3423 int ncells;
3424 const XColor *cells = x_color_cells (f, &ncells);
06a2c219
GM
3425
3426 for (nearest = i = 0; i < ncells; ++i)
3427 {
3428 long dred = (color->red >> 8) - (cells[i].red >> 8);
3429 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3430 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3431 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3432
3433 if (delta < nearest_delta)
3434 {
3435 nearest = i;
3436 nearest_delta = delta;
3437 }
3438 }
3439
3440 color->red = cells[nearest].red;
3441 color->green = cells[nearest].green;
3442 color->blue = cells[nearest].blue;
3443 rc = XAllocColor (display, cmap, color);
3444 }
35efe0a1
GM
3445 else
3446 {
3447 /* If allocation succeeded, and the allocated pixel color is not
3448 equal to a cached pixel color recorded earlier, there was a
3449 change in the colormap, so clear the color cache. */
3450 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3451 XColor *cached_color;
3452
3453 if (dpyinfo->color_cells
3454 && (cached_color = &dpyinfo->color_cells[color->pixel],
cae71efe
GM
3455 (cached_color->red != color->red
3456 || cached_color->blue != color->blue
3457 || cached_color->green != color->green)))
35efe0a1
GM
3458 {
3459 xfree (dpyinfo->color_cells);
3460 dpyinfo->color_cells = NULL;
3461 dpyinfo->ncolor_cells = 0;
3462 }
3463 }
06a2c219 3464
d9c545da
GM
3465#ifdef DEBUG_X_COLORS
3466 if (rc)
3467 register_color (color->pixel);
3468#endif /* DEBUG_X_COLORS */
3469
06a2c219
GM
3470 return rc;
3471}
3472
3473
d9c545da
GM
3474/* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3475 It's necessary to do this instead of just using PIXEL directly to
3476 get color reference counts right. */
3477
3478unsigned long
3479x_copy_color (f, pixel)
3480 struct frame *f;
3481 unsigned long pixel;
3482{
3483 XColor color;
3484
3485 color.pixel = pixel;
3486 BLOCK_INPUT;
f04e1297 3487 x_query_color (f, &color);
d9c545da
GM
3488 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3489 UNBLOCK_INPUT;
3490#ifdef DEBUG_X_COLORS
3491 register_color (pixel);
3492#endif
3493 return color.pixel;
3494}
3495
3496
3e71d8f2
GM
3497/* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3498 It's necessary to do this instead of just using PIXEL directly to
3499 get color reference counts right. */
3500
3501unsigned long
3502x_copy_dpy_color (dpy, cmap, pixel)
3503 Display *dpy;
3504 Colormap cmap;
3505 unsigned long pixel;
3506{
3507 XColor color;
3508
3509 color.pixel = pixel;
3510 BLOCK_INPUT;
3511 XQueryColor (dpy, cmap, &color);
3512 XAllocColor (dpy, cmap, &color);
3513 UNBLOCK_INPUT;
3514#ifdef DEBUG_X_COLORS
3515 register_color (pixel);
3516#endif
3517 return color.pixel;
3518}
3519
3520
6d8b0acd 3521/* Brightness beyond which a color won't have its highlight brightness
d7361edf 3522 boosted.
6d8b0acd 3523
d7361edf
MB
3524 Nominally, highlight colors for `3d' faces are calculated by
3525 brightening an object's color by a constant scale factor, but this
3526 doesn't yield good results for dark colors, so for colors who's
3527 brightness is less than this value (on a scale of 0-65535) have an
3528 use an additional additive factor.
6d8b0acd
MB
3529
3530 The value here is set so that the default menu-bar/mode-line color
3531 (grey75) will not have its highlights changed at all. */
3532#define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3533
3534
06a2c219
GM
3535/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3536 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3537 If this produces the same color as PIXEL, try a color where all RGB
3538 values have DELTA added. Return the allocated color in *PIXEL.
3539 DISPLAY is the X display, CMAP is the colormap to operate on.
3540 Value is non-zero if successful. */
3541
3542static int
3543x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3544 struct frame *f;
3545 Display *display;
3546 Colormap cmap;
3547 unsigned long *pixel;
68c45bf0 3548 double factor;
06a2c219
GM
3549 int delta;
3550{
3551 XColor color, new;
6d8b0acd 3552 long bright;
06a2c219
GM
3553 int success_p;
3554
3555 /* Get RGB color values. */
3556 color.pixel = *pixel;
f04e1297 3557 x_query_color (f, &color);
06a2c219
GM
3558
3559 /* Change RGB values by specified FACTOR. Avoid overflow! */
3560 xassert (factor >= 0);
3561 new.red = min (0xffff, factor * color.red);
3562 new.green = min (0xffff, factor * color.green);
3563 new.blue = min (0xffff, factor * color.blue);
3564
d7361edf
MB
3565 /* Calculate brightness of COLOR. */
3566 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
6d8b0acd
MB
3567
3568 /* We only boost colors that are darker than
3569 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3570 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3571 /* Make an additive adjustment to NEW, because it's dark enough so
3572 that scaling by FACTOR alone isn't enough. */
3573 {
3574 /* How far below the limit this color is (0 - 1, 1 being darker). */
3575 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3576 /* The additive adjustment. */
d7361edf 3577 int min_delta = delta * dimness * factor / 2;
6d8b0acd
MB
3578
3579 if (factor < 1)
3580 {
6d8b0acd
MB
3581 new.red = max (0, new.red - min_delta);
3582 new.green = max (0, new.green - min_delta);
3583 new.blue = max (0, new.blue - min_delta);
3584 }
3585 else
3586 {
3587 new.red = min (0xffff, min_delta + new.red);
3588 new.green = min (0xffff, min_delta + new.green);
3589 new.blue = min (0xffff, min_delta + new.blue);
3590 }
3591 }
3592
06a2c219 3593 /* Try to allocate the color. */
80c32bcc 3594 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3595 if (success_p)
3596 {
3597 if (new.pixel == *pixel)
3598 {
3599 /* If we end up with the same color as before, try adding
3600 delta to the RGB values. */
0d605c67 3601 x_free_colors (f, &new.pixel, 1);
06a2c219
GM
3602
3603 new.red = min (0xffff, delta + color.red);
3604 new.green = min (0xffff, delta + color.green);
3605 new.blue = min (0xffff, delta + color.blue);
80c32bcc 3606 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3607 }
3608 else
3609 success_p = 1;
3610 *pixel = new.pixel;
3611 }
3612
3613 return success_p;
3614}
3615
3616
3617/* Set up the foreground color for drawing relief lines of glyph
3618 string S. RELIEF is a pointer to a struct relief containing the GC
3619 with which lines will be drawn. Use a color that is FACTOR or
3620 DELTA lighter or darker than the relief's background which is found
3621 in S->f->output_data.x->relief_background. If such a color cannot
3622 be allocated, use DEFAULT_PIXEL, instead. */
3623
3624static void
3625x_setup_relief_color (f, relief, factor, delta, default_pixel)
3626 struct frame *f;
3627 struct relief *relief;
68c45bf0 3628 double factor;
06a2c219
GM
3629 int delta;
3630 unsigned long default_pixel;
3631{
3632 XGCValues xgcv;
3633 struct x_output *di = f->output_data.x;
3634 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3635 unsigned long pixel;
3636 unsigned long background = di->relief_background;
43bd1b2b 3637 Colormap cmap = FRAME_X_COLORMAP (f);
dcd08bfb
GM
3638 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3639 Display *dpy = FRAME_X_DISPLAY (f);
06a2c219
GM
3640
3641 xgcv.graphics_exposures = False;
3642 xgcv.line_width = 1;
3643
3644 /* Free previously allocated color. The color cell will be reused
3645 when it has been freed as many times as it was allocated, so this
3646 doesn't affect faces using the same colors. */
3647 if (relief->gc
3648 && relief->allocated_p)
3649 {
0d605c67 3650 x_free_colors (f, &relief->pixel, 1);
06a2c219
GM
3651 relief->allocated_p = 0;
3652 }
3653
3654 /* Allocate new color. */
3655 xgcv.foreground = default_pixel;
3656 pixel = background;
dcd08bfb
GM
3657 if (dpyinfo->n_planes != 1
3658 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
06a2c219
GM
3659 {
3660 relief->allocated_p = 1;
3661 xgcv.foreground = relief->pixel = pixel;
3662 }
3663
3664 if (relief->gc == 0)
3665 {
dcd08bfb 3666 xgcv.stipple = dpyinfo->gray;
06a2c219 3667 mask |= GCStipple;
dcd08bfb 3668 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
06a2c219
GM
3669 }
3670 else
dcd08bfb 3671 XChangeGC (dpy, relief->gc, mask, &xgcv);
06a2c219
GM
3672}
3673
3674
3675/* Set up colors for the relief lines around glyph string S. */
3676
3677static void
3678x_setup_relief_colors (s)
3679 struct glyph_string *s;
3680{
3681 struct x_output *di = s->f->output_data.x;
3682 unsigned long color;
3683
3684 if (s->face->use_box_color_for_shadows_p)
3685 color = s->face->box_color;
3686 else
3687 {
3688 XGCValues xgcv;
3689
3690 /* Get the background color of the face. */
3691 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3692 color = xgcv.background;
3693 }
3694
3695 if (di->white_relief.gc == 0
3696 || color != di->relief_background)
3697 {
3698 di->relief_background = color;
3699 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3700 WHITE_PIX_DEFAULT (s->f));
3701 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3702 BLACK_PIX_DEFAULT (s->f));
3703 }
3704}
3705
3706
3707/* Draw a relief on frame F inside the rectangle given by LEFT_X,
3708 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3709 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3710 relief. LEFT_P non-zero means draw a relief on the left side of
3711 the rectangle. RIGHT_P non-zero means draw a relief on the right
3712 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3713 when drawing. */
3714
3715static void
3716x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3717 raised_p, left_p, right_p, clip_rect)
3718 struct frame *f;
3719 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3720 XRectangle *clip_rect;
3721{
3722 int i;
3723 GC gc;
3724
3725 if (raised_p)
3726 gc = f->output_data.x->white_relief.gc;
3727 else
3728 gc = f->output_data.x->black_relief.gc;
3729 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3730
3731 /* Top. */
3732 for (i = 0; i < width; ++i)
3733 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3734 left_x + i * left_p, top_y + i,
3735 right_x + 1 - i * right_p, top_y + i);
3736
3737 /* Left. */
3738 if (left_p)
3739 for (i = 0; i < width; ++i)
3740 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3741 left_x + i, top_y + i, left_x + i, bottom_y - i);
3742
3743 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3744 if (raised_p)
3745 gc = f->output_data.x->black_relief.gc;
3746 else
3747 gc = f->output_data.x->white_relief.gc;
3748 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3749
3750 /* Bottom. */
3751 for (i = 0; i < width; ++i)
3752 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3753 left_x + i * left_p, bottom_y - i,
3754 right_x + 1 - i * right_p, bottom_y - i);
3755
3756 /* Right. */
3757 if (right_p)
3758 for (i = 0; i < width; ++i)
3759 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3760 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3761
3762 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3763}
3764
3765
3766/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3767 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3768 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3769 left side of the rectangle. RIGHT_P non-zero means draw a line
3770 on the right side of the rectangle. CLIP_RECT is the clipping
3771 rectangle to use when drawing. */
3772
3773static void
3774x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3775 left_p, right_p, clip_rect)
3776 struct glyph_string *s;
3777 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3778 XRectangle *clip_rect;
3779{
3780 XGCValues xgcv;
3781
3782 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3783 XSetForeground (s->display, s->gc, s->face->box_color);
3784 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3785
3786 /* Top. */
3787 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3788 left_x, top_y, right_x - left_x + 1, width);
06a2c219
GM
3789
3790 /* Left. */
3791 if (left_p)
3792 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3793 left_x, top_y, width, bottom_y - top_y + 1);
06a2c219
GM
3794
3795 /* Bottom. */
3796 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3797 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
06a2c219
GM
3798
3799 /* Right. */
3800 if (right_p)
3801 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3802 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
06a2c219
GM
3803
3804 XSetForeground (s->display, s->gc, xgcv.foreground);
3805 XSetClipMask (s->display, s->gc, None);
3806}
3807
3808
3809/* Draw a box around glyph string S. */
3810
3811static void
3812x_draw_glyph_string_box (s)
3813 struct glyph_string *s;
3814{
3815 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3816 int left_p, right_p;
3817 struct glyph *last_glyph;
3818 XRectangle clip_rect;
3819
3820 last_x = window_box_right (s->w, s->area);
3821 if (s->row->full_width_p
3822 && !s->w->pseudo_window_p)
3823 {
110859fc 3824 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
06a2c219
GM
3825 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3826 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3827 }
3828
3829 /* The glyph that may have a right box line. */
b4192550 3830 last_glyph = (s->cmp || s->img
06a2c219
GM
3831 ? s->first_glyph
3832 : s->first_glyph + s->nchars - 1);
3833
ea2ba0d4 3834 width = abs (s->face->box_line_width);
06a2c219
GM
3835 raised_p = s->face->box == FACE_RAISED_BOX;
3836 left_x = s->x;
3837 right_x = ((s->row->full_width_p
1da3fd71 3838 ? last_x - 1
a7aeb2de 3839 : min (last_x, s->x + s->background_width) - 1));
06a2c219
GM
3840 top_y = s->y;
3841 bottom_y = top_y + s->height - 1;
3842
3843 left_p = (s->first_glyph->left_box_line_p
3844 || (s->hl == DRAW_MOUSE_FACE
3845 && (s->prev == NULL
3846 || s->prev->hl != s->hl)));
3847 right_p = (last_glyph->right_box_line_p
3848 || (s->hl == DRAW_MOUSE_FACE
3849 && (s->next == NULL
3850 || s->next->hl != s->hl)));
3851
3852 x_get_glyph_string_clip_rect (s, &clip_rect);
3853
3854 if (s->face->box == FACE_SIMPLE_BOX)
3855 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3856 left_p, right_p, &clip_rect);
3857 else
3858 {
3859 x_setup_relief_colors (s);
3860 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3861 width, raised_p, left_p, right_p, &clip_rect);
3862 }
3863}
3864
3865
3866/* Draw foreground of image glyph string S. */
3867
3868static void
3869x_draw_image_foreground (s)
3870 struct glyph_string *s;
3871{
3872 int x;
95af8492 3873 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
3874
3875 /* If first glyph of S has a left box line, start drawing it to the
3876 right of that line. */
3877 if (s->face->box != FACE_NO_BOX
3878 && s->first_glyph->left_box_line_p)
ea2ba0d4 3879 x = s->x + abs (s->face->box_line_width);
06a2c219
GM
3880 else
3881 x = s->x;
3882
3883 /* If there is a margin around the image, adjust x- and y-position
3884 by that margin. */
22d650b8
GM
3885 x += s->img->hmargin;
3886 y += s->img->vmargin;
06a2c219
GM
3887
3888 if (s->img->pixmap)
3889 {
3890 if (s->img->mask)
3891 {
3892 /* We can't set both a clip mask and use XSetClipRectangles
3893 because the latter also sets a clip mask. We also can't
3894 trust on the shape extension to be available
3895 (XShapeCombineRegion). So, compute the rectangle to draw
3896 manually. */
3897 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3898 | GCFunction);
3899 XGCValues xgcv;
3900 XRectangle clip_rect, image_rect, r;
3901
3902 xgcv.clip_mask = s->img->mask;
3903 xgcv.clip_x_origin = x;
3904 xgcv.clip_y_origin = y;
3905 xgcv.function = GXcopy;
3906 XChangeGC (s->display, s->gc, mask, &xgcv);
3907
3908 x_get_glyph_string_clip_rect (s, &clip_rect);
3909 image_rect.x = x;
3910 image_rect.y = y;
3911 image_rect.width = s->img->width;
3912 image_rect.height = s->img->height;
3913 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3914 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3915 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3916 }
3917 else
3918 {
49ad1d99
GM
3919 unsigned long mask = GCClipXOrigin | GCClipYOrigin | GCFunction;
3920 XGCValues xgcv;
3921 XRectangle clip_rect, image_rect, r;
3922
3923 x_get_glyph_string_clip_rect (s, &clip_rect);
3924 image_rect.x = x;
3925 image_rect.y = y;
3926 image_rect.width = s->img->width;
3927 image_rect.height = s->img->height;
3928 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3929 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3930 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
06a2c219
GM
3931
3932 /* When the image has a mask, we can expect that at
3933 least part of a mouse highlight or a block cursor will
3934 be visible. If the image doesn't have a mask, make
3935 a block cursor visible by drawing a rectangle around
3936 the image. I believe it's looking better if we do
3937 nothing here for mouse-face. */
3938 if (s->hl == DRAW_CURSOR)
3939 XDrawRectangle (s->display, s->window, s->gc, x, y,
3940 s->img->width - 1, s->img->height - 1);
3941 }
3942 }
3943 else
3944 /* Draw a rectangle if image could not be loaded. */
3945 XDrawRectangle (s->display, s->window, s->gc, x, y,
3946 s->img->width - 1, s->img->height - 1);
3947}
3948
3949
3950/* Draw a relief around the image glyph string S. */
3951
3952static void
3953x_draw_image_relief (s)
3954 struct glyph_string *s;
3955{
3956 int x0, y0, x1, y1, thick, raised_p;
3957 XRectangle r;
3958 int x;
95af8492 3959 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
3960
3961 /* If first glyph of S has a left box line, start drawing it to the
3962 right of that line. */
3963 if (s->face->box != FACE_NO_BOX
3964 && s->first_glyph->left_box_line_p)
ea2ba0d4 3965 x = s->x + abs (s->face->box_line_width);
06a2c219
GM
3966 else
3967 x = s->x;
3968
3969 /* If there is a margin around the image, adjust x- and y-position
3970 by that margin. */
22d650b8
GM
3971 x += s->img->hmargin;
3972 y += s->img->vmargin;
06a2c219
GM
3973
3974 if (s->hl == DRAW_IMAGE_SUNKEN
3975 || s->hl == DRAW_IMAGE_RAISED)
3976 {
9ea173e8 3977 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
06a2c219
GM
3978 raised_p = s->hl == DRAW_IMAGE_RAISED;
3979 }
3980 else
3981 {
3982 thick = abs (s->img->relief);
3983 raised_p = s->img->relief > 0;
3984 }
3985
3986 x0 = x - thick;
3987 y0 = y - thick;
3988 x1 = x + s->img->width + thick - 1;
3989 y1 = y + s->img->height + thick - 1;
3990
3991 x_setup_relief_colors (s);
3992 x_get_glyph_string_clip_rect (s, &r);
3993 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3994}
3995
3996
3997/* Draw the foreground of image glyph string S to PIXMAP. */
3998
3999static void
4000x_draw_image_foreground_1 (s, pixmap)
4001 struct glyph_string *s;
4002 Pixmap pixmap;
4003{
4004 int x;
95af8492 4005 int y = s->ybase - s->y - image_ascent (s->img, s->face);
06a2c219
GM
4006
4007 /* If first glyph of S has a left box line, start drawing it to the
4008 right of that line. */
4009 if (s->face->box != FACE_NO_BOX
4010 && s->first_glyph->left_box_line_p)
ea2ba0d4 4011 x = abs (s->face->box_line_width);
06a2c219
GM
4012 else
4013 x = 0;
4014
4015 /* If there is a margin around the image, adjust x- and y-position
4016 by that margin. */
22d650b8
GM
4017 x += s->img->hmargin;
4018 y += s->img->vmargin;
dc43ef94 4019
06a2c219
GM
4020 if (s->img->pixmap)
4021 {
4022 if (s->img->mask)
4023 {
4024 /* We can't set both a clip mask and use XSetClipRectangles
4025 because the latter also sets a clip mask. We also can't
4026 trust on the shape extension to be available
4027 (XShapeCombineRegion). So, compute the rectangle to draw
4028 manually. */
4029 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4030 | GCFunction);
4031 XGCValues xgcv;
4032
4033 xgcv.clip_mask = s->img->mask;
4034 xgcv.clip_x_origin = x;
4035 xgcv.clip_y_origin = y;
4036 xgcv.function = GXcopy;
4037 XChangeGC (s->display, s->gc, mask, &xgcv);
4038
4039 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4040 0, 0, s->img->width, s->img->height, x, y);
4041 XSetClipMask (s->display, s->gc, None);
4042 }
4043 else
4044 {
4045 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4046 0, 0, s->img->width, s->img->height, x, y);
4047
4048 /* When the image has a mask, we can expect that at
4049 least part of a mouse highlight or a block cursor will
4050 be visible. If the image doesn't have a mask, make
4051 a block cursor visible by drawing a rectangle around
4052 the image. I believe it's looking better if we do
4053 nothing here for mouse-face. */
4054 if (s->hl == DRAW_CURSOR)
4055 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4056 s->img->width - 1, s->img->height - 1);
4057 }
4058 }
4059 else
4060 /* Draw a rectangle if image could not be loaded. */
4061 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4062 s->img->width - 1, s->img->height - 1);
4063}
dc43ef94 4064
990ba854 4065
06a2c219
GM
4066/* Draw part of the background of glyph string S. X, Y, W, and H
4067 give the rectangle to draw. */
a9a5b0a5 4068
06a2c219
GM
4069static void
4070x_draw_glyph_string_bg_rect (s, x, y, w, h)
4071 struct glyph_string *s;
4072 int x, y, w, h;
4073{
4074 if (s->stippled_p)
4075 {
4076 /* Fill background with a stipple pattern. */
4077 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4078 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4079 XSetFillStyle (s->display, s->gc, FillSolid);
4080 }
4081 else
4082 x_clear_glyph_string_rect (s, x, y, w, h);
4083}
07e34cb0 4084
b5210ea7 4085
06a2c219 4086/* Draw image glyph string S.
dc43ef94 4087
06a2c219
GM
4088 s->y
4089 s->x +-------------------------
4090 | s->face->box
4091 |
4092 | +-------------------------
4093 | | s->img->margin
4094 | |
4095 | | +-------------------
4096 | | | the image
dc43ef94 4097
06a2c219 4098 */
dc43ef94 4099
06a2c219
GM
4100static void
4101x_draw_image_glyph_string (s)
4102 struct glyph_string *s;
4103{
4104 int x, y;
ea2ba0d4
KH
4105 int box_line_hwidth = abs (s->face->box_line_width);
4106 int box_line_vwidth = max (s->face->box_line_width, 0);
06a2c219
GM
4107 int height;
4108 Pixmap pixmap = None;
4109
ea2ba0d4 4110 height = s->height - 2 * box_line_vwidth;
06a2c219
GM
4111
4112 /* Fill background with face under the image. Do it only if row is
4113 taller than image or if image has a clip mask to reduce
4114 flickering. */
4115 s->stippled_p = s->face->stipple != 0;
4116 if (height > s->img->height
22d650b8
GM
4117 || s->img->hmargin
4118 || s->img->vmargin
06a2c219
GM
4119 || s->img->mask
4120 || s->img->pixmap == 0
4121 || s->width != s->background_width)
4122 {
ea2ba0d4
KH
4123 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4124 x = s->x + box_line_hwidth;
06a2c219
GM
4125 else
4126 x = s->x;
4127
ea2ba0d4 4128 y = s->y + box_line_vwidth;
06a2c219
GM
4129
4130 if (s->img->mask)
4131 {
f9b5db02
GM
4132 /* Create a pixmap as large as the glyph string. Fill it
4133 with the background color. Copy the image to it, using
4134 its mask. Copy the temporary pixmap to the display. */
06a2c219
GM
4135 Screen *screen = FRAME_X_SCREEN (s->f);
4136 int depth = DefaultDepthOfScreen (screen);
4137
4138 /* Create a pixmap as large as the glyph string. */
4139 pixmap = XCreatePixmap (s->display, s->window,
4140 s->background_width,
4141 s->height, depth);
4142
4143 /* Don't clip in the following because we're working on the
4144 pixmap. */
4145 XSetClipMask (s->display, s->gc, None);
4146
4147 /* Fill the pixmap with the background color/stipple. */
4148 if (s->stippled_p)
4149 {
4150 /* Fill background with a stipple pattern. */
4151 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4152 XFillRectangle (s->display, pixmap, s->gc,
4153 0, 0, s->background_width, s->height);
4154 XSetFillStyle (s->display, s->gc, FillSolid);
4155 }
4156 else
4157 {
4158 XGCValues xgcv;
4159 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4160 &xgcv);
4161 XSetForeground (s->display, s->gc, xgcv.background);
4162 XFillRectangle (s->display, pixmap, s->gc,
4163 0, 0, s->background_width, s->height);
4164 XSetForeground (s->display, s->gc, xgcv.foreground);
4165 }
4166 }
4167 else
06a2c219
GM
4168 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4169
4170 s->background_filled_p = 1;
4171 }
dc43ef94 4172
06a2c219
GM
4173 /* Draw the foreground. */
4174 if (pixmap != None)
4175 {
4176 x_draw_image_foreground_1 (s, pixmap);
4177 x_set_glyph_string_clipping (s);
4178 XCopyArea (s->display, pixmap, s->window, s->gc,
4179 0, 0, s->background_width, s->height, s->x, s->y);
4180 XFreePixmap (s->display, pixmap);
4181 }
4182 else
4183 x_draw_image_foreground (s);
b5210ea7 4184
06a2c219
GM
4185 /* If we must draw a relief around the image, do it. */
4186 if (s->img->relief
4187 || s->hl == DRAW_IMAGE_RAISED
4188 || s->hl == DRAW_IMAGE_SUNKEN)
4189 x_draw_image_relief (s);
4190}
8c1a6a84 4191
990ba854 4192
06a2c219 4193/* Draw stretch glyph string S. */
dc43ef94 4194
06a2c219
GM
4195static void
4196x_draw_stretch_glyph_string (s)
4197 struct glyph_string *s;
4198{
4199 xassert (s->first_glyph->type == STRETCH_GLYPH);
4200 s->stippled_p = s->face->stipple != 0;
990ba854 4201
06a2c219
GM
4202 if (s->hl == DRAW_CURSOR
4203 && !x_stretch_cursor_p)
4204 {
4205 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4206 as wide as the stretch glyph. */
4207 int width = min (CANON_X_UNIT (s->f), s->background_width);
990ba854 4208
06a2c219
GM
4209 /* Draw cursor. */
4210 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
0cdd0c9f 4211
06a2c219
GM
4212 /* Clear rest using the GC of the original non-cursor face. */
4213 if (width < s->background_width)
4214 {
4215 GC gc = s->face->gc;
4216 int x = s->x + width, y = s->y;
4217 int w = s->background_width - width, h = s->height;
4218 XRectangle r;
dc43ef94 4219
06a2c219
GM
4220 x_get_glyph_string_clip_rect (s, &r);
4221 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
97210f4e 4222
06a2c219
GM
4223 if (s->face->stipple)
4224 {
4225 /* Fill background with a stipple pattern. */
4226 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4227 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4228 XSetFillStyle (s->display, gc, FillSolid);
4229 }
4230 else
4231 {
4232 XGCValues xgcv;
4233 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4234 XSetForeground (s->display, gc, xgcv.background);
4235 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4236 XSetForeground (s->display, gc, xgcv.foreground);
4237 }
4238 }
4239 }
4240 else
4241 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4242 s->height);
4243
4244 s->background_filled_p = 1;
4245}
4246
4247
4248/* Draw glyph string S. */
4249
4250static void
4251x_draw_glyph_string (s)
4252 struct glyph_string *s;
4253{
4254 /* If S draws into the background of its successor, draw the
4255 background of the successor first so that S can draw into it.
4256 This makes S->next use XDrawString instead of XDrawImageString. */
66ac4b0e 4257 if (s->next && s->right_overhang && !s->for_overlaps_p)
06a2c219
GM
4258 {
4259 xassert (s->next->img == NULL);
4260 x_set_glyph_string_gc (s->next);
4261 x_set_glyph_string_clipping (s->next);
4262 x_draw_glyph_string_background (s->next, 1);
4263 }
97210f4e 4264
06a2c219
GM
4265 /* Set up S->gc, set clipping and draw S. */
4266 x_set_glyph_string_gc (s);
4267 x_set_glyph_string_clipping (s);
4268
4269 switch (s->first_glyph->type)
4270 {
4271 case IMAGE_GLYPH:
4272 x_draw_image_glyph_string (s);
4273 break;
4274
4275 case STRETCH_GLYPH:
4276 x_draw_stretch_glyph_string (s);
4277 break;
4278
4279 case CHAR_GLYPH:
66ac4b0e
GM
4280 if (s->for_overlaps_p)
4281 s->background_filled_p = 1;
4282 else
4283 x_draw_glyph_string_background (s, 0);
06a2c219
GM
4284 x_draw_glyph_string_foreground (s);
4285 break;
4286
b4192550
KH
4287 case COMPOSITE_GLYPH:
4288 if (s->for_overlaps_p || s->gidx > 0)
4289 s->background_filled_p = 1;
4290 else
4291 x_draw_glyph_string_background (s, 1);
4292 x_draw_composite_glyph_string_foreground (s);
4293 break;
4294
06a2c219
GM
4295 default:
4296 abort ();
4297 }
4298
66ac4b0e 4299 if (!s->for_overlaps_p)
06a2c219 4300 {
66ac4b0e
GM
4301 /* Draw underline. */
4302 if (s->face->underline_p)
4303 {
e24e84cc
GM
4304 unsigned long tem, h;
4305 int y;
06a2c219 4306
e24e84cc 4307 /* Get the underline thickness. Default is 1 pixel. */
66ac4b0e
GM
4308 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4309 h = 1;
e24e84cc
GM
4310
4311 /* Get the underline position. This is the recommended
4312 vertical offset in pixels from the baseline to the top of
4313 the underline. This is a signed value according to the
4314 specs, and its default is
4315
4316 ROUND ((maximum descent) / 2), with
4317 ROUND(x) = floor (x + 0.5) */
4318
4319 if (XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
4320 y = s->ybase + (long) tem;
4321 else if (s->face->font)
4322 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4323 else
4324 y = s->height - h;
06a2c219 4325
66ac4b0e 4326 if (s->face->underline_defaulted_p)
e24e84cc
GM
4327 XFillRectangle (s->display, s->window, s->gc,
4328 s->x, y, s->width, h);
66ac4b0e
GM
4329 else
4330 {
4331 XGCValues xgcv;
4332 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4333 XSetForeground (s->display, s->gc, s->face->underline_color);
e24e84cc
GM
4334 XFillRectangle (s->display, s->window, s->gc,
4335 s->x, y, s->width, h);
66ac4b0e
GM
4336 XSetForeground (s->display, s->gc, xgcv.foreground);
4337 }
dc6f92b8 4338 }
07e34cb0 4339
66ac4b0e
GM
4340 /* Draw overline. */
4341 if (s->face->overline_p)
06a2c219 4342 {
66ac4b0e
GM
4343 unsigned long dy = 0, h = 1;
4344
4345 if (s->face->overline_color_defaulted_p)
4346 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4347 s->width, h);
4348 else
4349 {
4350 XGCValues xgcv;
4351 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4352 XSetForeground (s->display, s->gc, s->face->overline_color);
4353 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4354 s->width, h);
4355 XSetForeground (s->display, s->gc, xgcv.foreground);
4356 }
06a2c219 4357 }
06a2c219 4358
66ac4b0e
GM
4359 /* Draw strike-through. */
4360 if (s->face->strike_through_p)
06a2c219 4361 {
66ac4b0e
GM
4362 unsigned long h = 1;
4363 unsigned long dy = (s->height - h) / 2;
4364
4365 if (s->face->strike_through_color_defaulted_p)
4366 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4367 s->width, h);
4368 else
4369 {
4370 XGCValues xgcv;
4371 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4372 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4373 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4374 s->width, h);
4375 XSetForeground (s->display, s->gc, xgcv.foreground);
4376 }
06a2c219 4377 }
06a2c219 4378
66ac4b0e
GM
4379 /* Draw relief. */
4380 if (s->face->box != FACE_NO_BOX)
4381 x_draw_glyph_string_box (s);
4382 }
06a2c219
GM
4383
4384 /* Reset clipping. */
4385 XSetClipMask (s->display, s->gc, None);
dc6f92b8 4386}
07e34cb0 4387
06a2c219 4388
b4192550
KH
4389static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4390 struct face **, int));
06a2c219 4391
06a2c219 4392
209f68d9
GM
4393/* Fill glyph string S with composition components specified by S->cmp.
4394
b4192550
KH
4395 FACES is an array of faces for all components of this composition.
4396 S->gidx is the index of the first component for S.
4397 OVERLAPS_P non-zero means S should draw the foreground only, and
209f68d9 4398 use its physical height for clipping.
06a2c219 4399
b4192550 4400 Value is the index of a component not in S. */
07e34cb0 4401
b4192550
KH
4402static int
4403x_fill_composite_glyph_string (s, faces, overlaps_p)
06a2c219 4404 struct glyph_string *s;
b4192550 4405 struct face **faces;
66ac4b0e 4406 int overlaps_p;
07e34cb0 4407{
b4192550 4408 int i;
06a2c219 4409
b4192550 4410 xassert (s);
06a2c219 4411
b4192550 4412 s->for_overlaps_p = overlaps_p;
06a2c219 4413
b4192550
KH
4414 s->face = faces[s->gidx];
4415 s->font = s->face->font;
4416 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
06a2c219 4417
b4192550
KH
4418 /* For all glyphs of this composition, starting at the offset
4419 S->gidx, until we reach the end of the definition or encounter a
4420 glyph that requires the different face, add it to S. */
4421 ++s->nchars;
4422 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4423 ++s->nchars;
06a2c219 4424
b4192550
KH
4425 /* All glyph strings for the same composition has the same width,
4426 i.e. the width set for the first component of the composition. */
06a2c219 4427
06a2c219
GM
4428 s->width = s->first_glyph->pixel_width;
4429
4430 /* If the specified font could not be loaded, use the frame's
4431 default font, but record the fact that we couldn't load it in
4432 the glyph string so that we can draw rectangles for the
4433 characters of the glyph string. */
4434 if (s->font == NULL)
4435 {
4436 s->font_not_found_p = 1;
4437 s->font = FRAME_FONT (s->f);
4438 }
4439
4440 /* Adjust base line for subscript/superscript text. */
4441 s->ybase += s->first_glyph->voffset;
4442
4443 xassert (s->face && s->face->gc);
4444
4445 /* This glyph string must always be drawn with 16-bit functions. */
4446 s->two_byte_p = 1;
b4192550
KH
4447
4448 return s->gidx + s->nchars;
06a2c219
GM
4449}
4450
4451
209f68d9
GM
4452/* Fill glyph string S from a sequence of character glyphs.
4453
06a2c219 4454 FACE_ID is the face id of the string. START is the index of the
66ac4b0e
GM
4455 first glyph to consider, END is the index of the last + 1.
4456 OVERLAPS_P non-zero means S should draw the foreground only, and
209f68d9 4457 use its physical height for clipping.
66ac4b0e
GM
4458
4459 Value is the index of the first glyph not in S. */
06a2c219
GM
4460
4461static int
66ac4b0e 4462x_fill_glyph_string (s, face_id, start, end, overlaps_p)
06a2c219
GM
4463 struct glyph_string *s;
4464 int face_id;
66ac4b0e 4465 int start, end, overlaps_p;
06a2c219
GM
4466{
4467 struct glyph *glyph, *last;
4468 int voffset;
ee569018 4469 int glyph_not_available_p;
06a2c219 4470
06a2c219
GM
4471 xassert (s->f == XFRAME (s->w->frame));
4472 xassert (s->nchars == 0);
4473 xassert (start >= 0 && end > start);
4474
66ac4b0e 4475 s->for_overlaps_p = overlaps_p,
06a2c219
GM
4476 glyph = s->row->glyphs[s->area] + start;
4477 last = s->row->glyphs[s->area] + end;
4478 voffset = glyph->voffset;
4479
ee569018
KH
4480 glyph_not_available_p = glyph->glyph_not_available_p;
4481
06a2c219
GM
4482 while (glyph < last
4483 && glyph->type == CHAR_GLYPH
4484 && glyph->voffset == voffset
ee569018
KH
4485 /* Same face id implies same font, nowadays. */
4486 && glyph->face_id == face_id
4487 && glyph->glyph_not_available_p == glyph_not_available_p)
06a2c219 4488 {
ee569018
KH
4489 int two_byte_p;
4490
06a2c219 4491 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
ee569018
KH
4492 s->char2b + s->nchars,
4493 &two_byte_p);
4494 s->two_byte_p = two_byte_p;
06a2c219
GM
4495 ++s->nchars;
4496 xassert (s->nchars <= end - start);
4497 s->width += glyph->pixel_width;
4498 ++glyph;
4499 }
4500
4501 s->font = s->face->font;
4502 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4503
4504 /* If the specified font could not be loaded, use the frame's font,
4505 but record the fact that we couldn't load it in
4506 S->font_not_found_p so that we can draw rectangles for the
4507 characters of the glyph string. */
ee569018 4508 if (s->font == NULL || glyph_not_available_p)
06a2c219
GM
4509 {
4510 s->font_not_found_p = 1;
4511 s->font = FRAME_FONT (s->f);
4512 }
4513
4514 /* Adjust base line for subscript/superscript text. */
4515 s->ybase += voffset;
66ac4b0e 4516
06a2c219
GM
4517 xassert (s->face && s->face->gc);
4518 return glyph - s->row->glyphs[s->area];
07e34cb0 4519}
dc6f92b8 4520
06a2c219
GM
4521
4522/* Fill glyph string S from image glyph S->first_glyph. */
dc6f92b8 4523
dfcf069d 4524static void
06a2c219
GM
4525x_fill_image_glyph_string (s)
4526 struct glyph_string *s;
4527{
4528 xassert (s->first_glyph->type == IMAGE_GLYPH);
43d120d8 4529 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
06a2c219 4530 xassert (s->img);
43d120d8 4531 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
06a2c219
GM
4532 s->font = s->face->font;
4533 s->width = s->first_glyph->pixel_width;
4534
4535 /* Adjust base line for subscript/superscript text. */
4536 s->ybase += s->first_glyph->voffset;
4537}
4538
4539
209f68d9 4540/* Fill glyph string S from a sequence of stretch glyphs.
06a2c219 4541
209f68d9
GM
4542 ROW is the glyph row in which the glyphs are found, AREA is the
4543 area within the row. START is the index of the first glyph to
4544 consider, END is the index of the last + 1.
4545
4546 Value is the index of the first glyph not in S. */
4547
4548static int
4549x_fill_stretch_glyph_string (s, row, area, start, end)
06a2c219 4550 struct glyph_string *s;
209f68d9
GM
4551 struct glyph_row *row;
4552 enum glyph_row_area area;
4553 int start, end;
06a2c219 4554{
209f68d9
GM
4555 struct glyph *glyph, *last;
4556 int voffset, face_id;
4557
06a2c219 4558 xassert (s->first_glyph->type == STRETCH_GLYPH);
209f68d9
GM
4559
4560 glyph = s->row->glyphs[s->area] + start;
4561 last = s->row->glyphs[s->area] + end;
4562 face_id = glyph->face_id;
4563 s->face = FACE_FROM_ID (s->f, face_id);
06a2c219 4564 s->font = s->face->font;
209f68d9
GM
4565 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4566 s->width = glyph->pixel_width;
4567 voffset = glyph->voffset;
4568
4569 for (++glyph;
4570 (glyph < last
4571 && glyph->type == STRETCH_GLYPH
4572 && glyph->voffset == voffset
4573 && glyph->face_id == face_id);
4574 ++glyph)
4575 s->width += glyph->pixel_width;
06a2c219
GM
4576
4577 /* Adjust base line for subscript/superscript text. */
209f68d9
GM
4578 s->ybase += voffset;
4579
4580 xassert (s->face && s->face->gc);
4581 return glyph - s->row->glyphs[s->area];
06a2c219
GM
4582}
4583
4584
4585/* Initialize glyph string S. CHAR2B is a suitably allocated vector
4586 of XChar2b structures for S; it can't be allocated in
4587 x_init_glyph_string because it must be allocated via `alloca'. W
4588 is the window on which S is drawn. ROW and AREA are the glyph row
4589 and area within the row from which S is constructed. START is the
4590 index of the first glyph structure covered by S. HL is a
4591 face-override for drawing S. */
4592
4593static void
4594x_init_glyph_string (s, char2b, w, row, area, start, hl)
4595 struct glyph_string *s;
4596 XChar2b *char2b;
4597 struct window *w;
4598 struct glyph_row *row;
4599 enum glyph_row_area area;
4600 int start;
4601 enum draw_glyphs_face hl;
4602{
4603 bzero (s, sizeof *s);
4604 s->w = w;
4605 s->f = XFRAME (w->frame);
4606 s->display = FRAME_X_DISPLAY (s->f);
4607 s->window = FRAME_X_WINDOW (s->f);
4608 s->char2b = char2b;
4609 s->hl = hl;
4610 s->row = row;
4611 s->area = area;
4612 s->first_glyph = row->glyphs[area] + start;
4613 s->height = row->height;
4614 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4615
9ea173e8
GM
4616 /* Display the internal border below the tool-bar window. */
4617 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219
GM
4618 s->y -= s->f->output_data.x->internal_border_width;
4619
4620 s->ybase = s->y + row->ascent;
4621}
4622
4623
4624/* Set background width of glyph string S. START is the index of the
4625 first glyph following S. LAST_X is the right-most x-position + 1
4626 in the drawing area. */
4627
4628static INLINE void
4629x_set_glyph_string_background_width (s, start, last_x)
4630 struct glyph_string *s;
4631 int start;
4632 int last_x;
4633{
4634 /* If the face of this glyph string has to be drawn to the end of
4635 the drawing area, set S->extends_to_end_of_line_p. */
4636 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4637
4638 if (start == s->row->used[s->area]
4639 && s->hl == DRAW_NORMAL_TEXT
eb79f5cc
GM
4640 && s->area == TEXT_AREA
4641 && (s->row->fill_line_p
06a2c219
GM
4642 || s->face->background != default_face->background
4643 || s->face->stipple != default_face->stipple))
4644 s->extends_to_end_of_line_p = 1;
4645
4646 /* If S extends its face to the end of the line, set its
4647 background_width to the distance to the right edge of the drawing
4648 area. */
4649 if (s->extends_to_end_of_line_p)
1da3fd71 4650 s->background_width = last_x - s->x + 1;
06a2c219
GM
4651 else
4652 s->background_width = s->width;
4653}
4654
4655
4656/* Add a glyph string for a stretch glyph to the list of strings
4657 between HEAD and TAIL. START is the index of the stretch glyph in
4658 row area AREA of glyph row ROW. END is the index of the last glyph
4659 in that glyph row area. X is the current output position assigned
4660 to the new glyph string constructed. HL overrides that face of the
4661 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4662 is the right-most x-position of the drawing area. */
4663
8abee2e1
DL
4664/* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4665 and below -- keep them on one line. */
4666#define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4667 do \
4668 { \
4669 s = (struct glyph_string *) alloca (sizeof *s); \
4670 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
209f68d9 4671 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
06a2c219 4672 x_append_glyph_string (&HEAD, &TAIL, s); \
06a2c219
GM
4673 s->x = (X); \
4674 } \
4675 while (0)
4676
4677
4678/* Add a glyph string for an image glyph to the list of strings
4679 between HEAD and TAIL. START is the index of the image glyph in
4680 row area AREA of glyph row ROW. END is the index of the last glyph
4681 in that glyph row area. X is the current output position assigned
4682 to the new glyph string constructed. HL overrides that face of the
4683 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4684 is the right-most x-position of the drawing area. */
4685
8abee2e1 4686#define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4687 do \
4688 { \
4689 s = (struct glyph_string *) alloca (sizeof *s); \
4690 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4691 x_fill_image_glyph_string (s); \
4692 x_append_glyph_string (&HEAD, &TAIL, s); \
4693 ++START; \
4694 s->x = (X); \
4695 } \
4696 while (0)
4697
4698
4699/* Add a glyph string for a sequence of character glyphs to the list
4700 of strings between HEAD and TAIL. START is the index of the first
4701 glyph in row area AREA of glyph row ROW that is part of the new
4702 glyph string. END is the index of the last glyph in that glyph row
4703 area. X is the current output position assigned to the new glyph
4704 string constructed. HL overrides that face of the glyph; e.g. it
4705 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4706 right-most x-position of the drawing area. */
4707
8abee2e1 4708#define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4709 do \
4710 { \
3e71d8f2 4711 int c, face_id; \
06a2c219
GM
4712 XChar2b *char2b; \
4713 \
43d120d8 4714 c = (ROW)->glyphs[AREA][START].u.ch; \
43d120d8 4715 face_id = (ROW)->glyphs[AREA][START].face_id; \
06a2c219 4716 \
b4192550
KH
4717 s = (struct glyph_string *) alloca (sizeof *s); \
4718 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4719 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4720 x_append_glyph_string (&HEAD, &TAIL, s); \
b4192550
KH
4721 s->x = (X); \
4722 START = x_fill_glyph_string (s, face_id, START, END, \
66ac4b0e 4723 OVERLAPS_P); \
06a2c219
GM
4724 } \
4725 while (0)
4726
4727
b4192550
KH
4728/* Add a glyph string for a composite sequence to the list of strings
4729 between HEAD and TAIL. START is the index of the first glyph in
4730 row area AREA of glyph row ROW that is part of the new glyph
4731 string. END is the index of the last glyph in that glyph row area.
4732 X is the current output position assigned to the new glyph string
4733 constructed. HL overrides that face of the glyph; e.g. it is
4734 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4735 x-position of the drawing area. */
4736
6c27ec25 4737#define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
b4192550 4738 do { \
43d120d8
KH
4739 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4740 int face_id = (ROW)->glyphs[AREA][START].face_id; \
ee569018 4741 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
b4192550
KH
4742 struct composition *cmp = composition_table[cmp_id]; \
4743 int glyph_len = cmp->glyph_len; \
4744 XChar2b *char2b; \
4745 struct face **faces; \
4746 struct glyph_string *first_s = NULL; \
4747 int n; \
4748 \
ee569018 4749 base_face = base_face->ascii_face; \
b4192550
KH
4750 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4751 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4752 /* At first, fill in `char2b' and `faces'. */ \
4753 for (n = 0; n < glyph_len; n++) \
4754 { \
43d120d8 4755 int c = COMPOSITION_GLYPH (cmp, n); \
ee569018
KH
4756 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4757 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4758 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4759 this_face_id, char2b + n, 1); \
b4192550
KH
4760 } \
4761 \
4762 /* Make glyph_strings for each glyph sequence that is drawable by \
4763 the same face, and append them to HEAD/TAIL. */ \
4764 for (n = 0; n < cmp->glyph_len;) \
4765 { \
4766 s = (struct glyph_string *) alloca (sizeof *s); \
4767 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4768 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4769 s->cmp = cmp; \
4770 s->gidx = n; \
b4192550
KH
4771 s->x = (X); \
4772 \
4773 if (n == 0) \
4774 first_s = s; \
4775 \
4776 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4777 } \
4778 \
4779 ++START; \
4780 s = first_s; \
4781 } while (0)
4782
4783
06a2c219
GM
4784/* Build a list of glyph strings between HEAD and TAIL for the glyphs
4785 of AREA of glyph row ROW on window W between indices START and END.
4786 HL overrides the face for drawing glyph strings, e.g. it is
4787 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4788 x-positions of the drawing area.
4789
4790 This is an ugly monster macro construct because we must use alloca
4791 to allocate glyph strings (because x_draw_glyphs can be called
4792 asynchronously). */
4793
8abee2e1 4794#define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4795 do \
4796 { \
4797 HEAD = TAIL = NULL; \
4798 while (START < END) \
4799 { \
4800 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4801 switch (first_glyph->type) \
4802 { \
4803 case CHAR_GLYPH: \
4804 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
66ac4b0e
GM
4805 TAIL, HL, X, LAST_X, \
4806 OVERLAPS_P); \
06a2c219
GM
4807 break; \
4808 \
b4192550
KH
4809 case COMPOSITE_GLYPH: \
4810 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4811 HEAD, TAIL, HL, X, LAST_X,\
4812 OVERLAPS_P); \
4813 break; \
4814 \
06a2c219
GM
4815 case STRETCH_GLYPH: \
4816 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4817 HEAD, TAIL, HL, X, LAST_X); \
4818 break; \
4819 \
4820 case IMAGE_GLYPH: \
4821 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4822 TAIL, HL, X, LAST_X); \
4823 break; \
4824 \
4825 default: \
4826 abort (); \
4827 } \
4828 \
4829 x_set_glyph_string_background_width (s, START, LAST_X); \
4830 (X) += s->width; \
4831 } \
4832 } \
4833 while (0)
4834
4835
4836/* Draw glyphs between START and END in AREA of ROW on window W,
4837 starting at x-position X. X is relative to AREA in W. HL is a
4838 face-override with the following meaning:
4839
4840 DRAW_NORMAL_TEXT draw normally
4841 DRAW_CURSOR draw in cursor face
4842 DRAW_MOUSE_FACE draw in mouse face.
4843 DRAW_INVERSE_VIDEO draw in mode line face
4844 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4845 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4846
4847 If REAL_START is non-null, return in *REAL_START the real starting
4848 position for display. This can be different from START in case
4849 overlapping glyphs must be displayed. If REAL_END is non-null,
4850 return in *REAL_END the real end position for display. This can be
4851 different from END in case overlapping glyphs must be displayed.
4852
66ac4b0e
GM
4853 If OVERLAPS_P is non-zero, draw only the foreground of characters
4854 and clip to the physical height of ROW.
4855
06a2c219
GM
4856 Value is the x-position reached, relative to AREA of W. */
4857
4858static int
66ac4b0e
GM
4859x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4860 overlaps_p)
06a2c219
GM
4861 struct window *w;
4862 int x;
4863 struct glyph_row *row;
4864 enum glyph_row_area area;
4865 int start, end;
4866 enum draw_glyphs_face hl;
4867 int *real_start, *real_end;
66ac4b0e 4868 int overlaps_p;
dc6f92b8 4869{
06a2c219
GM
4870 struct glyph_string *head, *tail;
4871 struct glyph_string *s;
4872 int last_x, area_width;
4873 int x_reached;
4874 int i, j;
4875
4876 /* Let's rather be paranoid than getting a SEGV. */
06a2c219 4877 end = min (end, row->used[area]);
a8710abf
GM
4878 start = max (0, start);
4879 start = min (end, start);
06a2c219
GM
4880 if (real_start)
4881 *real_start = start;
4882 if (real_end)
4883 *real_end = end;
4884
4885 /* Translate X to frame coordinates. Set last_x to the right
4886 end of the drawing area. */
4887 if (row->full_width_p)
4888 {
4889 /* X is relative to the left edge of W, without scroll bars
4890 or flag areas. */
4891 struct frame *f = XFRAME (w->frame);
110859fc 4892 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
06a2c219 4893 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
dc6f92b8 4894
06a2c219
GM
4895 x += window_left_x;
4896 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4897 last_x = window_left_x + area_width;
4898
4899 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4900 {
110859fc 4901 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
06a2c219
GM
4902 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4903 last_x += width;
4904 else
4905 x -= width;
4906 }
dc6f92b8 4907
b9432a85
GM
4908 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4909 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
06a2c219
GM
4910 }
4911 else
dc6f92b8 4912 {
06a2c219
GM
4913 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4914 area_width = window_box_width (w, area);
4915 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
dc6f92b8
JB
4916 }
4917
06a2c219
GM
4918 /* Build a doubly-linked list of glyph_string structures between
4919 head and tail from what we have to draw. Note that the macro
4920 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4921 the reason we use a separate variable `i'. */
4922 i = start;
66ac4b0e
GM
4923 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4924 overlaps_p);
06a2c219
GM
4925 if (tail)
4926 x_reached = tail->x + tail->background_width;
4927 else
4928 x_reached = x;
90e65f07 4929
06a2c219
GM
4930 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4931 the row, redraw some glyphs in front or following the glyph
4932 strings built above. */
a8710abf 4933 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
06a2c219
GM
4934 {
4935 int dummy_x = 0;
4936 struct glyph_string *h, *t;
4937
4938 /* Compute overhangs for all glyph strings. */
4939 for (s = head; s; s = s->next)
4940 x_compute_glyph_string_overhangs (s);
4941
4942 /* Prepend glyph strings for glyphs in front of the first glyph
4943 string that are overwritten because of the first glyph
4944 string's left overhang. The background of all strings
4945 prepended must be drawn because the first glyph string
4946 draws over it. */
4947 i = x_left_overwritten (head);
4948 if (i >= 0)
4949 {
4950 j = i;
4951 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
66ac4b0e
GM
4952 DRAW_NORMAL_TEXT, dummy_x, last_x,
4953 overlaps_p);
06a2c219
GM
4954 start = i;
4955 if (real_start)
4956 *real_start = start;
4957 x_compute_overhangs_and_x (t, head->x, 1);
4958 x_prepend_glyph_string_lists (&head, &tail, h, t);
4959 }
58769bee 4960
06a2c219
GM
4961 /* Prepend glyph strings for glyphs in front of the first glyph
4962 string that overwrite that glyph string because of their
4963 right overhang. For these strings, only the foreground must
4964 be drawn, because it draws over the glyph string at `head'.
4965 The background must not be drawn because this would overwrite
4966 right overhangs of preceding glyphs for which no glyph
4967 strings exist. */
4968 i = x_left_overwriting (head);
4969 if (i >= 0)
4970 {
4971 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
66ac4b0e
GM
4972 DRAW_NORMAL_TEXT, dummy_x, last_x,
4973 overlaps_p);
06a2c219
GM
4974 for (s = h; s; s = s->next)
4975 s->background_filled_p = 1;
4976 if (real_start)
4977 *real_start = i;
4978 x_compute_overhangs_and_x (t, head->x, 1);
4979 x_prepend_glyph_string_lists (&head, &tail, h, t);
4980 }
dbcb258a 4981
06a2c219
GM
4982 /* Append glyphs strings for glyphs following the last glyph
4983 string tail that are overwritten by tail. The background of
4984 these strings has to be drawn because tail's foreground draws
4985 over it. */
4986 i = x_right_overwritten (tail);
4987 if (i >= 0)
4988 {
4989 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4990 DRAW_NORMAL_TEXT, x, last_x,
4991 overlaps_p);
06a2c219
GM
4992 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4993 x_append_glyph_string_lists (&head, &tail, h, t);
4994 if (real_end)
4995 *real_end = i;
4996 }
dc6f92b8 4997
06a2c219
GM
4998 /* Append glyph strings for glyphs following the last glyph
4999 string tail that overwrite tail. The foreground of such
5000 glyphs has to be drawn because it writes into the background
5001 of tail. The background must not be drawn because it could
5002 paint over the foreground of following glyphs. */
5003 i = x_right_overwriting (tail);
5004 if (i >= 0)
5005 {
5006 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
5007 DRAW_NORMAL_TEXT, x, last_x,
5008 overlaps_p);
06a2c219
GM
5009 for (s = h; s; s = s->next)
5010 s->background_filled_p = 1;
5011 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5012 x_append_glyph_string_lists (&head, &tail, h, t);
5013 if (real_end)
5014 *real_end = i;
5015 }
5016 }
58769bee 5017
06a2c219
GM
5018 /* Draw all strings. */
5019 for (s = head; s; s = s->next)
5020 x_draw_glyph_string (s);
dc6f92b8 5021
06a2c219
GM
5022 /* Value is the x-position up to which drawn, relative to AREA of W.
5023 This doesn't include parts drawn because of overhangs. */
5024 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5025 if (!row->full_width_p)
5026 {
5027 if (area > LEFT_MARGIN_AREA)
5028 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5029 if (area > TEXT_AREA)
5030 x_reached -= window_box_width (w, TEXT_AREA);
5031 }
a8710abf 5032
06a2c219
GM
5033 return x_reached;
5034}
dc6f92b8 5035
dc6f92b8 5036
66ac4b0e
GM
5037/* Fix the display of area AREA of overlapping row ROW in window W. */
5038
5039static void
5040x_fix_overlapping_area (w, row, area)
5041 struct window *w;
5042 struct glyph_row *row;
5043 enum glyph_row_area area;
5044{
5045 int i, x;
5046
5047 BLOCK_INPUT;
5048
5049 if (area == LEFT_MARGIN_AREA)
5050 x = 0;
5051 else if (area == TEXT_AREA)
5052 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5053 else
5054 x = (window_box_width (w, LEFT_MARGIN_AREA)
5055 + window_box_width (w, TEXT_AREA));
5056
5057 for (i = 0; i < row->used[area];)
5058 {
5059 if (row->glyphs[area][i].overlaps_vertically_p)
5060 {
5061 int start = i, start_x = x;
5062
5063 do
5064 {
5065 x += row->glyphs[area][i].pixel_width;
5066 ++i;
5067 }
5068 while (i < row->used[area]
5069 && row->glyphs[area][i].overlaps_vertically_p);
5070
5071 x_draw_glyphs (w, start_x, row, area, start, i,
5072 (row->inverse_p
5073 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5074 NULL, NULL, 1);
5075 }
5076 else
5077 {
5078 x += row->glyphs[area][i].pixel_width;
5079 ++i;
5080 }
5081 }
5082
5083 UNBLOCK_INPUT;
5084}
5085
5086
06a2c219
GM
5087/* Output LEN glyphs starting at START at the nominal cursor position.
5088 Advance the nominal cursor over the text. The global variable
5089 updated_window contains the window being updated, updated_row is
5090 the glyph row being updated, and updated_area is the area of that
5091 row being updated. */
dc6f92b8 5092
06a2c219
GM
5093static void
5094x_write_glyphs (start, len)
5095 struct glyph *start;
5096 int len;
5097{
5098 int x, hpos, real_start, real_end;
d9cdbb3d 5099
06a2c219 5100 xassert (updated_window && updated_row);
dc6f92b8 5101 BLOCK_INPUT;
06a2c219
GM
5102
5103 /* Write glyphs. */
dc6f92b8 5104
06a2c219
GM
5105 hpos = start - updated_row->glyphs[updated_area];
5106 x = x_draw_glyphs (updated_window, output_cursor.x,
5107 updated_row, updated_area,
5108 hpos, hpos + len,
5109 (updated_row->inverse_p
5110 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
66ac4b0e 5111 &real_start, &real_end, 0);
b30ec466 5112
06a2c219
GM
5113 /* If we drew over the cursor, note that it is not visible any more. */
5114 note_overwritten_text_cursor (updated_window, real_start,
5115 real_end - real_start);
dc6f92b8
JB
5116
5117 UNBLOCK_INPUT;
06a2c219
GM
5118
5119 /* Advance the output cursor. */
5120 output_cursor.hpos += len;
5121 output_cursor.x = x;
dc6f92b8
JB
5122}
5123
0cdd0c9f 5124
06a2c219 5125/* Insert LEN glyphs from START at the nominal cursor position. */
0cdd0c9f 5126
06a2c219
GM
5127static void
5128x_insert_glyphs (start, len)
5129 struct glyph *start;
5130 register int len;
5131{
5132 struct frame *f;
5133 struct window *w;
5134 int line_height, shift_by_width, shifted_region_width;
5135 struct glyph_row *row;
5136 struct glyph *glyph;
5137 int frame_x, frame_y, hpos, real_start, real_end;
58769bee 5138
06a2c219 5139 xassert (updated_window && updated_row);
0cdd0c9f 5140 BLOCK_INPUT;
06a2c219
GM
5141 w = updated_window;
5142 f = XFRAME (WINDOW_FRAME (w));
5143
5144 /* Get the height of the line we are in. */
5145 row = updated_row;
5146 line_height = row->height;
5147
5148 /* Get the width of the glyphs to insert. */
5149 shift_by_width = 0;
5150 for (glyph = start; glyph < start + len; ++glyph)
5151 shift_by_width += glyph->pixel_width;
5152
5153 /* Get the width of the region to shift right. */
5154 shifted_region_width = (window_box_width (w, updated_area)
5155 - output_cursor.x
5156 - shift_by_width);
5157
5158 /* Shift right. */
bf0ab8a2 5159 frame_x = window_box_left (w, updated_area) + output_cursor.x;
06a2c219
GM
5160 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5161 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5162 f->output_data.x->normal_gc,
5163 frame_x, frame_y,
5164 shifted_region_width, line_height,
5165 frame_x + shift_by_width, frame_y);
5166
5167 /* Write the glyphs. */
5168 hpos = start - row->glyphs[updated_area];
5169 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
66ac4b0e 5170 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
06a2c219
GM
5171 note_overwritten_text_cursor (w, real_start, real_end - real_start);
5172
5173 /* Advance the output cursor. */
5174 output_cursor.hpos += len;
5175 output_cursor.x += shift_by_width;
0cdd0c9f
RS
5176 UNBLOCK_INPUT;
5177}
0cdd0c9f 5178
0cdd0c9f 5179
06a2c219
GM
5180/* Delete N glyphs at the nominal cursor position. Not implemented
5181 for X frames. */
c83febd7
RS
5182
5183static void
06a2c219
GM
5184x_delete_glyphs (n)
5185 register int n;
c83febd7 5186{
06a2c219 5187 abort ();
c83febd7
RS
5188}
5189
0cdd0c9f 5190
c5e6e06b
GM
5191/* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
5192 If they are <= 0, this is probably an error. */
5193
5194void
5195x_clear_area (dpy, window, x, y, width, height, exposures)
5196 Display *dpy;
5197 Window window;
5198 int x, y;
5199 int width, height;
5200 int exposures;
5201{
5202 xassert (width > 0 && height > 0);
5203 XClearArea (dpy, window, x, y, width, height, exposures);
5204}
5205
5206
06a2c219
GM
5207/* Erase the current text line from the nominal cursor position
5208 (inclusive) to pixel column TO_X (exclusive). The idea is that
5209 everything from TO_X onward is already erased.
5210
5211 TO_X is a pixel position relative to updated_area of
5212 updated_window. TO_X == -1 means clear to the end of this area. */
dc6f92b8 5213
06a2c219
GM
5214static void
5215x_clear_end_of_line (to_x)
5216 int to_x;
5217{
5218 struct frame *f;
5219 struct window *w = updated_window;
5220 int max_x, min_y, max_y;
5221 int from_x, from_y, to_y;
5222
5223 xassert (updated_window && updated_row);
5224 f = XFRAME (w->frame);
5225
5226 if (updated_row->full_width_p)
5227 {
5228 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5229 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5230 && !w->pseudo_window_p)
5231 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
0cdd0c9f 5232 }
06a2c219
GM
5233 else
5234 max_x = window_box_width (w, updated_area);
5235 max_y = window_text_bottom_y (w);
dc6f92b8 5236
06a2c219
GM
5237 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5238 of window. For TO_X > 0, truncate to end of drawing area. */
5239 if (to_x == 0)
5240 return;
5241 else if (to_x < 0)
5242 to_x = max_x;
5243 else
5244 to_x = min (to_x, max_x);
dbc4e1c1 5245
06a2c219
GM
5246 to_y = min (max_y, output_cursor.y + updated_row->height);
5247
5248 /* Notice if the cursor will be cleared by this operation. */
5249 if (!updated_row->full_width_p)
5250 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
dbc4e1c1 5251
06a2c219
GM
5252 from_x = output_cursor.x;
5253
5254 /* Translate to frame coordinates. */
5255 if (updated_row->full_width_p)
5256 {
5257 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5258 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5259 }
0cdd0c9f
RS
5260 else
5261 {
06a2c219
GM
5262 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5263 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5264 }
5265
045dee35 5266 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
06a2c219
GM
5267 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5268 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5269
5270 /* Prevent inadvertently clearing to end of the X window. */
5271 if (to_x > from_x && to_y > from_y)
5272 {
5273 BLOCK_INPUT;
c5e6e06b
GM
5274 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5275 from_x, from_y, to_x - from_x, to_y - from_y,
5276 False);
06a2c219 5277 UNBLOCK_INPUT;
0cdd0c9f 5278 }
0cdd0c9f 5279}
dbc4e1c1 5280
0cdd0c9f 5281
06a2c219 5282/* Clear entire frame. If updating_frame is non-null, clear that
b86bd3dd 5283 frame. Otherwise clear the selected frame. */
06a2c219
GM
5284
5285static void
5286x_clear_frame ()
0cdd0c9f 5287{
06a2c219 5288 struct frame *f;
0cdd0c9f 5289
06a2c219
GM
5290 if (updating_frame)
5291 f = updating_frame;
0cdd0c9f 5292 else
b86bd3dd 5293 f = SELECTED_FRAME ();
58769bee 5294
06a2c219
GM
5295 /* Clearing the frame will erase any cursor, so mark them all as no
5296 longer visible. */
5297 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5298 output_cursor.hpos = output_cursor.vpos = 0;
5299 output_cursor.x = -1;
5300
5301 /* We don't set the output cursor here because there will always
5302 follow an explicit cursor_to. */
5303 BLOCK_INPUT;
5304 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5305
5306 /* We have to clear the scroll bars, too. If we have changed
5307 colors or something like that, then they should be notified. */
5308 x_scroll_bar_clear (f);
0cdd0c9f 5309
06a2c219
GM
5310 XFlush (FRAME_X_DISPLAY (f));
5311 UNBLOCK_INPUT;
dc6f92b8 5312}
06a2c219
GM
5313
5314
dc6f92b8 5315\f
dbc4e1c1
JB
5316/* Invert the middle quarter of the frame for .15 sec. */
5317
06a2c219
GM
5318/* We use the select system call to do the waiting, so we have to make
5319 sure it's available. If it isn't, we just won't do visual bells. */
5320
dbc4e1c1
JB
5321#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5322
06a2c219
GM
5323
5324/* Subtract the `struct timeval' values X and Y, storing the result in
5325 *RESULT. Return 1 if the difference is negative, otherwise 0. */
dbc4e1c1
JB
5326
5327static int
5328timeval_subtract (result, x, y)
5329 struct timeval *result, x, y;
5330{
06a2c219
GM
5331 /* Perform the carry for the later subtraction by updating y. This
5332 is safer because on some systems the tv_sec member is unsigned. */
dbc4e1c1
JB
5333 if (x.tv_usec < y.tv_usec)
5334 {
5335 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5336 y.tv_usec -= 1000000 * nsec;
5337 y.tv_sec += nsec;
5338 }
06a2c219 5339
dbc4e1c1
JB
5340 if (x.tv_usec - y.tv_usec > 1000000)
5341 {
5342 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5343 y.tv_usec += 1000000 * nsec;
5344 y.tv_sec -= nsec;
5345 }
5346
06a2c219
GM
5347 /* Compute the time remaining to wait. tv_usec is certainly
5348 positive. */
dbc4e1c1
JB
5349 result->tv_sec = x.tv_sec - y.tv_sec;
5350 result->tv_usec = x.tv_usec - y.tv_usec;
5351
06a2c219
GM
5352 /* Return indication of whether the result should be considered
5353 negative. */
dbc4e1c1
JB
5354 return x.tv_sec < y.tv_sec;
5355}
dc6f92b8 5356
dfcf069d 5357void
f676886a
JB
5358XTflash (f)
5359 struct frame *f;
dc6f92b8 5360{
dbc4e1c1 5361 BLOCK_INPUT;
dc6f92b8 5362
dbc4e1c1
JB
5363 {
5364 GC gc;
dc6f92b8 5365
06a2c219
GM
5366 /* Create a GC that will use the GXxor function to flip foreground
5367 pixels into background pixels. */
dbc4e1c1
JB
5368 {
5369 XGCValues values;
dc6f92b8 5370
dbc4e1c1 5371 values.function = GXxor;
7556890b
RS
5372 values.foreground = (f->output_data.x->foreground_pixel
5373 ^ f->output_data.x->background_pixel);
58769bee 5374
334208b7 5375 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dbc4e1c1
JB
5376 GCFunction | GCForeground, &values);
5377 }
dc6f92b8 5378
dbc4e1c1 5379 {
e84e14c3
RS
5380 /* Get the height not including a menu bar widget. */
5381 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5382 /* Height of each line to flash. */
5383 int flash_height = FRAME_LINE_HEIGHT (f);
5384 /* These will be the left and right margins of the rectangles. */
5385 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5386 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5387
5388 int width;
5389
5390 /* Don't flash the area between a scroll bar and the frame
5391 edge it is next to. */
5392 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5393 {
5394 case vertical_scroll_bar_left:
5395 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5396 break;
5397
5398 case vertical_scroll_bar_right:
5399 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5400 break;
06a2c219
GM
5401
5402 default:
5403 break;
e84e14c3
RS
5404 }
5405
5406 width = flash_right - flash_left;
5407
5408 /* If window is tall, flash top and bottom line. */
5409 if (height > 3 * FRAME_LINE_HEIGHT (f))
5410 {
5411 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5412 flash_left,
5413 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5414 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5415 width, flash_height);
5416 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5417 flash_left,
5418 (height - flash_height
5419 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5420 width, flash_height);
5421 }
5422 else
5423 /* If it is short, flash it all. */
5424 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5425 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5426 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
dc6f92b8 5427
06a2c219 5428 x_flush (f);
dc6f92b8 5429
dbc4e1c1 5430 {
06a2c219 5431 struct timeval wakeup;
dc6f92b8 5432
66c30ea1 5433 EMACS_GET_TIME (wakeup);
dc6f92b8 5434
dbc4e1c1
JB
5435 /* Compute time to wait until, propagating carry from usecs. */
5436 wakeup.tv_usec += 150000;
5437 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5438 wakeup.tv_usec %= 1000000;
5439
101922c3
GM
5440 /* Keep waiting until past the time wakeup or any input gets
5441 available. */
5442 while (! detect_input_pending ())
dbc4e1c1 5443 {
101922c3 5444 struct timeval current;
dbc4e1c1
JB
5445 struct timeval timeout;
5446
101922c3 5447 EMACS_GET_TIME (current);
dbc4e1c1 5448
101922c3
GM
5449 /* Break if result would be negative. */
5450 if (timeval_subtract (&current, wakeup, current))
dbc4e1c1
JB
5451 break;
5452
101922c3
GM
5453 /* How long `select' should wait. */
5454 timeout.tv_sec = 0;
5455 timeout.tv_usec = 10000;
5456
dbc4e1c1 5457 /* Try to wait that long--but we might wake up sooner. */
c32cdd9a 5458 select (0, NULL, NULL, NULL, &timeout);
dbc4e1c1
JB
5459 }
5460 }
58769bee 5461
e84e14c3
RS
5462 /* If window is tall, flash top and bottom line. */
5463 if (height > 3 * FRAME_LINE_HEIGHT (f))
5464 {
5465 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5466 flash_left,
5467 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5468 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5469 width, flash_height);
5470 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5471 flash_left,
5472 (height - flash_height
5473 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5474 width, flash_height);
5475 }
5476 else
5477 /* If it is short, flash it all. */
5478 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5479 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5480 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5481
334208b7 5482 XFreeGC (FRAME_X_DISPLAY (f), gc);
06a2c219 5483 x_flush (f);
dc6f92b8 5484 }
dbc4e1c1
JB
5485 }
5486
5487 UNBLOCK_INPUT;
dc6f92b8
JB
5488}
5489
06a2c219 5490#endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
dbc4e1c1
JB
5491
5492
dc6f92b8
JB
5493/* Make audible bell. */
5494
dfcf069d 5495void
dc6f92b8
JB
5496XTring_bell ()
5497{
b86bd3dd
GM
5498 struct frame *f = SELECTED_FRAME ();
5499
5500 if (FRAME_X_DISPLAY (f))
5501 {
dbc4e1c1 5502#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
b86bd3dd
GM
5503 if (visible_bell)
5504 XTflash (f);
5505 else
dbc4e1c1 5506#endif
b86bd3dd
GM
5507 {
5508 BLOCK_INPUT;
5509 XBell (FRAME_X_DISPLAY (f), 0);
5510 XFlush (FRAME_X_DISPLAY (f));
5511 UNBLOCK_INPUT;
5512 }
dc6f92b8
JB
5513 }
5514}
06a2c219 5515
dc6f92b8 5516\f
06a2c219
GM
5517/* Specify how many text lines, from the top of the window,
5518 should be affected by insert-lines and delete-lines operations.
5519 This, and those operations, are used only within an update
5520 that is bounded by calls to x_update_begin and x_update_end. */
dc6f92b8 5521
dfcf069d 5522static void
06a2c219
GM
5523XTset_terminal_window (n)
5524 register int n;
dc6f92b8 5525{
06a2c219 5526 /* This function intentionally left blank. */
dc6f92b8
JB
5527}
5528
06a2c219
GM
5529
5530\f
5531/***********************************************************************
5532 Line Dance
5533 ***********************************************************************/
5534
5535/* Perform an insert-lines or delete-lines operation, inserting N
5536 lines or deleting -N lines at vertical position VPOS. */
5537
dfcf069d 5538static void
06a2c219
GM
5539x_ins_del_lines (vpos, n)
5540 int vpos, n;
dc6f92b8
JB
5541{
5542 abort ();
5543}
06a2c219
GM
5544
5545
5546/* Scroll part of the display as described by RUN. */
dc6f92b8 5547
dfcf069d 5548static void
06a2c219
GM
5549x_scroll_run (w, run)
5550 struct window *w;
5551 struct run *run;
dc6f92b8 5552{
06a2c219
GM
5553 struct frame *f = XFRAME (w->frame);
5554 int x, y, width, height, from_y, to_y, bottom_y;
5555
5556 /* Get frame-relative bounding box of the text display area of W,
5557 without mode lines. Include in this box the flags areas to the
5558 left and right of W. */
5559 window_box (w, -1, &x, &y, &width, &height);
110859fc
GM
5560 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5561 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
5562
5563 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5564 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5565 bottom_y = y + height;
dc6f92b8 5566
06a2c219
GM
5567 if (to_y < from_y)
5568 {
5569 /* Scrolling up. Make sure we don't copy part of the mode
5570 line at the bottom. */
5571 if (from_y + run->height > bottom_y)
5572 height = bottom_y - from_y;
5573 else
5574 height = run->height;
5575 }
dc6f92b8 5576 else
06a2c219
GM
5577 {
5578 /* Scolling down. Make sure we don't copy over the mode line.
5579 at the bottom. */
5580 if (to_y + run->height > bottom_y)
5581 height = bottom_y - to_y;
5582 else
5583 height = run->height;
5584 }
7a13e894 5585
06a2c219
GM
5586 BLOCK_INPUT;
5587
5588 /* Cursor off. Will be switched on again in x_update_window_end. */
5589 updated_window = w;
5590 x_clear_cursor (w);
5591
5592 XCopyArea (FRAME_X_DISPLAY (f),
5593 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5594 f->output_data.x->normal_gc,
5595 x, from_y,
5596 width, height,
5597 x, to_y);
5598
5599 UNBLOCK_INPUT;
5600}
dc6f92b8 5601
dc6f92b8 5602
06a2c219
GM
5603\f
5604/***********************************************************************
5605 Exposure Events
5606 ***********************************************************************/
5607
5608/* Redisplay an exposed area of frame F. X and Y are the upper-left
5609 corner of the exposed rectangle. W and H are width and height of
5610 the exposed area. All are pixel values. W or H zero means redraw
5611 the entire frame. */
dc6f92b8 5612
06a2c219
GM
5613static void
5614expose_frame (f, x, y, w, h)
5615 struct frame *f;
5616 int x, y, w, h;
dc6f92b8 5617{
06a2c219 5618 XRectangle r;
dc6f92b8 5619
06a2c219 5620 TRACE ((stderr, "expose_frame "));
dc6f92b8 5621
06a2c219
GM
5622 /* No need to redraw if frame will be redrawn soon. */
5623 if (FRAME_GARBAGED_P (f))
dc6f92b8 5624 {
06a2c219
GM
5625 TRACE ((stderr, " garbaged\n"));
5626 return;
5627 }
5628
5629 /* If basic faces haven't been realized yet, there is no point in
5630 trying to redraw anything. This can happen when we get an expose
5631 event while Emacs is starting, e.g. by moving another window. */
5632 if (FRAME_FACE_CACHE (f) == NULL
5633 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5634 {
5635 TRACE ((stderr, " no faces\n"));
5636 return;
58769bee 5637 }
06a2c219
GM
5638
5639 if (w == 0 || h == 0)
58769bee 5640 {
06a2c219
GM
5641 r.x = r.y = 0;
5642 r.width = CANON_X_UNIT (f) * f->width;
5643 r.height = CANON_Y_UNIT (f) * f->height;
dc6f92b8
JB
5644 }
5645 else
5646 {
06a2c219
GM
5647 r.x = x;
5648 r.y = y;
5649 r.width = w;
5650 r.height = h;
5651 }
5652
5653 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5654 expose_window_tree (XWINDOW (f->root_window), &r);
5655
9ea173e8 5656 if (WINDOWP (f->tool_bar_window))
06a2c219 5657 {
9ea173e8 5658 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
5659 XRectangle window_rect;
5660 XRectangle intersection_rect;
5661 int window_x, window_y, window_width, window_height;
5662
5663
5664 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5665 window_rect.x = window_x;
5666 window_rect.y = window_y;
5667 window_rect.width = window_width;
5668 window_rect.height = window_height;
5669
5670 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5671 expose_window (w, &intersection_rect);
5672 }
5673
5674#ifndef USE_X_TOOLKIT
5675 if (WINDOWP (f->menu_bar_window))
5676 {
5677 struct window *w = XWINDOW (f->menu_bar_window);
5678 XRectangle window_rect;
5679 XRectangle intersection_rect;
5680 int window_x, window_y, window_width, window_height;
5681
5682
5683 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5684 window_rect.x = window_x;
5685 window_rect.y = window_y;
5686 window_rect.width = window_width;
5687 window_rect.height = window_height;
5688
5689 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5690 expose_window (w, &intersection_rect);
dc6f92b8 5691 }
06a2c219 5692#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
5693}
5694
06a2c219
GM
5695
5696/* Redraw (parts) of all windows in the window tree rooted at W that
5697 intersect R. R contains frame pixel coordinates. */
5698
58769bee 5699static void
06a2c219
GM
5700expose_window_tree (w, r)
5701 struct window *w;
5702 XRectangle *r;
dc6f92b8 5703{
06a2c219
GM
5704 while (w)
5705 {
5706 if (!NILP (w->hchild))
5707 expose_window_tree (XWINDOW (w->hchild), r);
5708 else if (!NILP (w->vchild))
5709 expose_window_tree (XWINDOW (w->vchild), r);
5710 else
5711 {
5712 XRectangle window_rect;
5713 XRectangle intersection_rect;
5714 struct frame *f = XFRAME (w->frame);
5715 int window_x, window_y, window_width, window_height;
5716
5717 /* Frame-relative pixel rectangle of W. */
5718 window_box (w, -1, &window_x, &window_y, &window_width,
5719 &window_height);
5720 window_rect.x
5721 = (window_x
110859fc 5722 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
714dc26c 5723 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
06a2c219
GM
5724 window_rect.y = window_y;
5725 window_rect.width
5726 = (window_width
110859fc 5727 + FRAME_X_FLAGS_AREA_WIDTH (f)
06a2c219
GM
5728 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5729 window_rect.height
5730 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5731
5732 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5733 expose_window (w, &intersection_rect);
5734 }
58769bee 5735
06a2c219
GM
5736 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5737 }
5738}
58769bee 5739
dc6f92b8 5740
06a2c219
GM
5741/* Redraw the part of glyph row area AREA of glyph row ROW on window W
5742 which intersects rectangle R. R is in window-relative coordinates. */
5743
5744static void
5745expose_area (w, row, r, area)
5746 struct window *w;
5747 struct glyph_row *row;
5748 XRectangle *r;
5749 enum glyph_row_area area;
5750{
06a2c219
GM
5751 struct glyph *first = row->glyphs[area];
5752 struct glyph *end = row->glyphs[area] + row->used[area];
5753 struct glyph *last;
4bc6dcc7 5754 int first_x, start_x, x;
06a2c219 5755
6fb13182
GM
5756 if (area == TEXT_AREA && row->fill_line_p)
5757 /* If row extends face to end of line write the whole line. */
4bc6dcc7 5758 x_draw_glyphs (w, 0, row, area,
6fb13182 5759 0, row->used[area],
06a2c219 5760 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5761 NULL, NULL, 0);
6fb13182
GM
5762 else
5763 {
4bc6dcc7
GM
5764 /* Set START_X to the window-relative start position for drawing glyphs of
5765 AREA. The first glyph of the text area can be partially visible.
5766 The first glyphs of other areas cannot. */
5767 if (area == LEFT_MARGIN_AREA)
5768 start_x = 0;
5769 else if (area == TEXT_AREA)
5770 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5771 else
5772 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5773 + window_box_width (w, TEXT_AREA));
5774 x = start_x;
5775
6fb13182
GM
5776 /* Find the first glyph that must be redrawn. */
5777 while (first < end
5778 && x + first->pixel_width < r->x)
5779 {
5780 x += first->pixel_width;
5781 ++first;
5782 }
5783
5784 /* Find the last one. */
5785 last = first;
5786 first_x = x;
5787 while (last < end
5788 && x < r->x + r->width)
5789 {
5790 x += last->pixel_width;
5791 ++last;
5792 }
5793
5794 /* Repaint. */
5795 if (last > first)
4bc6dcc7 5796 x_draw_glyphs (w, first_x - start_x, row, area,
6fb13182
GM
5797 first - row->glyphs[area],
5798 last - row->glyphs[area],
5799 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5800 NULL, NULL, 0);
5801 }
06a2c219
GM
5802}
5803
58769bee 5804
06a2c219
GM
5805/* Redraw the parts of the glyph row ROW on window W intersecting
5806 rectangle R. R is in window-relative coordinates. */
dc6f92b8 5807
06a2c219
GM
5808static void
5809expose_line (w, row, r)
5810 struct window *w;
5811 struct glyph_row *row;
5812 XRectangle *r;
5813{
5814 xassert (row->enabled_p);
5815
5816 if (row->mode_line_p || w->pseudo_window_p)
5817 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5818 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5819 NULL, NULL, 0);
06a2c219
GM
5820 else
5821 {
5822 if (row->used[LEFT_MARGIN_AREA])
5823 expose_area (w, row, r, LEFT_MARGIN_AREA);
5824 if (row->used[TEXT_AREA])
5825 expose_area (w, row, r, TEXT_AREA);
5826 if (row->used[RIGHT_MARGIN_AREA])
5827 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5828 x_draw_row_bitmaps (w, row);
5829 }
5830}
dc6f92b8 5831
58769bee 5832
06a2c219
GM
5833/* Return non-zero if W's cursor intersects rectangle R. */
5834
5835static int
5836x_phys_cursor_in_rect_p (w, r)
5837 struct window *w;
5838 XRectangle *r;
5839{
5840 XRectangle cr, result;
5841 struct glyph *cursor_glyph;
5842
5843 cursor_glyph = get_phys_cursor_glyph (w);
5844 if (cursor_glyph)
5845 {
5846 cr.x = w->phys_cursor.x;
5847 cr.y = w->phys_cursor.y;
5848 cr.width = cursor_glyph->pixel_width;
5849 cr.height = w->phys_cursor_height;
5850 return x_intersect_rectangles (&cr, r, &result);
5851 }
5852 else
5853 return 0;
dc6f92b8 5854}
dc6f92b8 5855
06a2c219
GM
5856
5857/* Redraw a rectangle of window W. R is a rectangle in window
5858 relative coordinates. Call this function with input blocked. */
dc6f92b8
JB
5859
5860static void
06a2c219
GM
5861expose_window (w, r)
5862 struct window *w;
5863 XRectangle *r;
dc6f92b8 5864{
06a2c219
GM
5865 struct glyph_row *row;
5866 int y;
5867 int yb = window_text_bottom_y (w);
5868 int cursor_cleared_p;
dc6f92b8 5869
80c32bcc
GM
5870 /* If window is not yet fully initialized, do nothing. This can
5871 happen when toolkit scroll bars are used and a window is split.
5872 Reconfiguring the scroll bar will generate an expose for a newly
5873 created window. */
c7911417 5874 if (w->current_matrix == NULL || w == updated_window)
80c32bcc
GM
5875 return;
5876
06a2c219
GM
5877 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5878 r->x, r->y, r->width, r->height));
dc6f92b8 5879
06a2c219
GM
5880 /* Convert to window coordinates. */
5881 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5882 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
dc6f92b8 5883
06a2c219
GM
5884 /* Turn off the cursor. */
5885 if (!w->pseudo_window_p
5886 && x_phys_cursor_in_rect_p (w, r))
5887 {
5888 x_clear_cursor (w);
5889 cursor_cleared_p = 1;
5890 }
5891 else
5892 cursor_cleared_p = 0;
5893
5894 /* Find the first row intersecting the rectangle R. */
5895 row = w->current_matrix->rows;
5896 y = 0;
5897 while (row->enabled_p
5898 && y < yb
5899 && y + row->height < r->y)
5900 {
5901 y += row->height;
5902 ++row;
5903 }
5904
dc6f92b8 5905 /* Display the text in the rectangle, one text line at a time. */
06a2c219
GM
5906 while (row->enabled_p
5907 && y < yb
5908 && y < r->y + r->height)
5909 {
5910 expose_line (w, row, r);
5911 y += row->height;
5912 ++row;
5913 }
5914
5915 /* Display the mode line if there is one. */
5916 if (WINDOW_WANTS_MODELINE_P (w)
5917 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5918 row->enabled_p)
5919 && row->y < r->y + r->height)
5920 expose_line (w, row, r);
dc6f92b8 5921
06a2c219 5922 if (!w->pseudo_window_p)
dc6f92b8 5923 {
06a2c219
GM
5924 /* Draw border between windows. */
5925 x_draw_vertical_border (w);
5926
5927 /* Turn the cursor on again. */
5928 if (cursor_cleared_p)
5929 x_update_window_cursor (w, 1);
5930 }
5931}
dc6f92b8 5932
dc6f92b8 5933
06a2c219
GM
5934/* Determine the intersection of two rectangles R1 and R2. Return
5935 the intersection in *RESULT. Value is non-zero if RESULT is not
5936 empty. */
5937
5938static int
5939x_intersect_rectangles (r1, r2, result)
5940 XRectangle *r1, *r2, *result;
5941{
5942 XRectangle *left, *right;
5943 XRectangle *upper, *lower;
5944 int intersection_p = 0;
5945
5946 /* Rearrange so that R1 is the left-most rectangle. */
5947 if (r1->x < r2->x)
5948 left = r1, right = r2;
5949 else
5950 left = r2, right = r1;
5951
5952 /* X0 of the intersection is right.x0, if this is inside R1,
5953 otherwise there is no intersection. */
5954 if (right->x <= left->x + left->width)
5955 {
5956 result->x = right->x;
5957
5958 /* The right end of the intersection is the minimum of the
5959 the right ends of left and right. */
5960 result->width = (min (left->x + left->width, right->x + right->width)
5961 - result->x);
5962
5963 /* Same game for Y. */
5964 if (r1->y < r2->y)
5965 upper = r1, lower = r2;
5966 else
5967 upper = r2, lower = r1;
5968
5969 /* The upper end of the intersection is lower.y0, if this is inside
5970 of upper. Otherwise, there is no intersection. */
5971 if (lower->y <= upper->y + upper->height)
dc43ef94 5972 {
06a2c219
GM
5973 result->y = lower->y;
5974
5975 /* The lower end of the intersection is the minimum of the lower
5976 ends of upper and lower. */
5977 result->height = (min (lower->y + lower->height,
5978 upper->y + upper->height)
5979 - result->y);
5980 intersection_p = 1;
dc43ef94 5981 }
dc6f92b8
JB
5982 }
5983
06a2c219 5984 return intersection_p;
dc6f92b8 5985}
06a2c219
GM
5986
5987
5988
5989
dc6f92b8 5990\f
dc6f92b8 5991static void
334208b7
RS
5992frame_highlight (f)
5993 struct frame *f;
dc6f92b8 5994{
b3e1e05c
JB
5995 /* We used to only do this if Vx_no_window_manager was non-nil, but
5996 the ICCCM (section 4.1.6) says that the window's border pixmap
5997 and border pixel are window attributes which are "private to the
5998 client", so we can always change it to whatever we want. */
5999 BLOCK_INPUT;
334208b7 6000 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 6001 f->output_data.x->border_pixel);
b3e1e05c 6002 UNBLOCK_INPUT;
5d46f928 6003 x_update_cursor (f, 1);
dc6f92b8
JB
6004}
6005
6006static void
334208b7
RS
6007frame_unhighlight (f)
6008 struct frame *f;
dc6f92b8 6009{
b3e1e05c
JB
6010 /* We used to only do this if Vx_no_window_manager was non-nil, but
6011 the ICCCM (section 4.1.6) says that the window's border pixmap
6012 and border pixel are window attributes which are "private to the
6013 client", so we can always change it to whatever we want. */
6014 BLOCK_INPUT;
334208b7 6015 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 6016 f->output_data.x->border_tile);
b3e1e05c 6017 UNBLOCK_INPUT;
5d46f928 6018 x_update_cursor (f, 1);
dc6f92b8 6019}
dc6f92b8 6020
f676886a
JB
6021/* The focus has changed. Update the frames as necessary to reflect
6022 the new situation. Note that we can't change the selected frame
c5acd733 6023 here, because the Lisp code we are interrupting might become confused.
eb8c3be9 6024 Each event gets marked with the frame in which it occurred, so the
c5acd733 6025 Lisp code can tell when the switch took place by examining the events. */
dc6f92b8 6026
6d4238f3 6027static void
0f941935
KH
6028x_new_focus_frame (dpyinfo, frame)
6029 struct x_display_info *dpyinfo;
f676886a 6030 struct frame *frame;
dc6f92b8 6031{
0f941935 6032 struct frame *old_focus = dpyinfo->x_focus_frame;
dc6f92b8 6033
0f941935 6034 if (frame != dpyinfo->x_focus_frame)
dc6f92b8 6035 {
58769bee 6036 /* Set this before calling other routines, so that they see
f676886a 6037 the correct value of x_focus_frame. */
0f941935 6038 dpyinfo->x_focus_frame = frame;
6d4238f3
JB
6039
6040 if (old_focus && old_focus->auto_lower)
f676886a 6041 x_lower_frame (old_focus);
dc6f92b8
JB
6042
6043#if 0
f676886a 6044 selected_frame = frame;
e0c1aef2
KH
6045 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
6046 selected_frame);
f676886a
JB
6047 Fselect_window (selected_frame->selected_window);
6048 choose_minibuf_frame ();
c118dd06 6049#endif /* ! 0 */
dc6f92b8 6050
0f941935
KH
6051 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6052 pending_autoraise_frame = dpyinfo->x_focus_frame;
0134a210
RS
6053 else
6054 pending_autoraise_frame = 0;
6d4238f3 6055 }
dc6f92b8 6056
0f941935 6057 x_frame_rehighlight (dpyinfo);
6d4238f3
JB
6058}
6059
37c2c98b
RS
6060/* Handle an event saying the mouse has moved out of an Emacs frame. */
6061
6062void
0f941935
KH
6063x_mouse_leave (dpyinfo)
6064 struct x_display_info *dpyinfo;
37c2c98b 6065{
0f941935 6066 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
37c2c98b 6067}
6d4238f3 6068
f451eb13
JB
6069/* The focus has changed, or we have redirected a frame's focus to
6070 another frame (this happens when a frame uses a surrogate
06a2c219 6071 mini-buffer frame). Shift the highlight as appropriate.
0f941935
KH
6072
6073 The FRAME argument doesn't necessarily have anything to do with which
06a2c219 6074 frame is being highlighted or un-highlighted; we only use it to find
0f941935 6075 the appropriate X display info. */
06a2c219 6076
6d4238f3 6077static void
0f941935
KH
6078XTframe_rehighlight (frame)
6079 struct frame *frame;
6d4238f3 6080{
0f941935
KH
6081 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6082}
6d4238f3 6083
0f941935
KH
6084static void
6085x_frame_rehighlight (dpyinfo)
6086 struct x_display_info *dpyinfo;
6087{
6088 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6089
6090 if (dpyinfo->x_focus_frame)
6d4238f3 6091 {
0f941935
KH
6092 dpyinfo->x_highlight_frame
6093 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6094 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6095 : dpyinfo->x_focus_frame);
6096 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
f451eb13 6097 {
0f941935
KH
6098 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6099 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
f451eb13 6100 }
dc6f92b8 6101 }
6d4238f3 6102 else
0f941935 6103 dpyinfo->x_highlight_frame = 0;
dc6f92b8 6104
0f941935 6105 if (dpyinfo->x_highlight_frame != old_highlight)
6d4238f3
JB
6106 {
6107 if (old_highlight)
f676886a 6108 frame_unhighlight (old_highlight);
0f941935
KH
6109 if (dpyinfo->x_highlight_frame)
6110 frame_highlight (dpyinfo->x_highlight_frame);
6d4238f3 6111 }
dc6f92b8 6112}
06a2c219
GM
6113
6114
dc6f92b8 6115\f
06a2c219 6116/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
dc6f92b8 6117
28430d3c
JB
6118/* Initialize mode_switch_bit and modifier_meaning. */
6119static void
334208b7
RS
6120x_find_modifier_meanings (dpyinfo)
6121 struct x_display_info *dpyinfo;
28430d3c 6122{
f689eb05 6123 int min_code, max_code;
28430d3c
JB
6124 KeySym *syms;
6125 int syms_per_code;
6126 XModifierKeymap *mods;
6127
334208b7
RS
6128 dpyinfo->meta_mod_mask = 0;
6129 dpyinfo->shift_lock_mask = 0;
6130 dpyinfo->alt_mod_mask = 0;
6131 dpyinfo->super_mod_mask = 0;
6132 dpyinfo->hyper_mod_mask = 0;
58769bee 6133
9658a521 6134#ifdef HAVE_X11R4
334208b7 6135 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
9658a521 6136#else
4a60f8c5
RS
6137 min_code = dpyinfo->display->min_keycode;
6138 max_code = dpyinfo->display->max_keycode;
9658a521
JB
6139#endif
6140
334208b7 6141 syms = XGetKeyboardMapping (dpyinfo->display,
28430d3c
JB
6142 min_code, max_code - min_code + 1,
6143 &syms_per_code);
334208b7 6144 mods = XGetModifierMapping (dpyinfo->display);
28430d3c 6145
58769bee 6146 /* Scan the modifier table to see which modifier bits the Meta and
11edeb03 6147 Alt keysyms are on. */
28430d3c 6148 {
06a2c219 6149 int row, col; /* The row and column in the modifier table. */
28430d3c
JB
6150
6151 for (row = 3; row < 8; row++)
6152 for (col = 0; col < mods->max_keypermod; col++)
6153 {
0299d313
RS
6154 KeyCode code
6155 = mods->modifiermap[(row * mods->max_keypermod) + col];
28430d3c 6156
af92970c
KH
6157 /* Zeroes are used for filler. Skip them. */
6158 if (code == 0)
6159 continue;
6160
28430d3c
JB
6161 /* Are any of this keycode's keysyms a meta key? */
6162 {
6163 int code_col;
6164
6165 for (code_col = 0; code_col < syms_per_code; code_col++)
6166 {
f689eb05 6167 int sym = syms[((code - min_code) * syms_per_code) + code_col];
28430d3c 6168
f689eb05 6169 switch (sym)
28430d3c 6170 {
f689eb05
JB
6171 case XK_Meta_L:
6172 case XK_Meta_R:
334208b7 6173 dpyinfo->meta_mod_mask |= (1 << row);
28430d3c 6174 break;
f689eb05
JB
6175
6176 case XK_Alt_L:
6177 case XK_Alt_R:
334208b7 6178 dpyinfo->alt_mod_mask |= (1 << row);
a3c44b14
RS
6179 break;
6180
6181 case XK_Hyper_L:
6182 case XK_Hyper_R:
334208b7 6183 dpyinfo->hyper_mod_mask |= (1 << row);
a3c44b14
RS
6184 break;
6185
6186 case XK_Super_L:
6187 case XK_Super_R:
334208b7 6188 dpyinfo->super_mod_mask |= (1 << row);
f689eb05 6189 break;
11edeb03
JB
6190
6191 case XK_Shift_Lock:
6192 /* Ignore this if it's not on the lock modifier. */
6193 if ((1 << row) == LockMask)
334208b7 6194 dpyinfo->shift_lock_mask = LockMask;
11edeb03 6195 break;
28430d3c
JB
6196 }
6197 }
6198 }
6199 }
6200 }
6201
f689eb05 6202 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
334208b7 6203 if (! dpyinfo->meta_mod_mask)
a3c44b14 6204 {
334208b7
RS
6205 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6206 dpyinfo->alt_mod_mask = 0;
a3c44b14 6207 }
f689eb05 6208
148c4b70
RS
6209 /* If some keys are both alt and meta,
6210 make them just meta, not alt. */
334208b7 6211 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
148c4b70 6212 {
334208b7 6213 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
148c4b70 6214 }
58769bee 6215
28430d3c 6216 XFree ((char *) syms);
f689eb05 6217 XFreeModifiermap (mods);
28430d3c
JB
6218}
6219
dfeccd2d
JB
6220/* Convert between the modifier bits X uses and the modifier bits
6221 Emacs uses. */
06a2c219 6222
7c5283e4 6223static unsigned int
334208b7
RS
6224x_x_to_emacs_modifiers (dpyinfo, state)
6225 struct x_display_info *dpyinfo;
dc6f92b8
JB
6226 unsigned int state;
6227{
334208b7
RS
6228 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6229 | ((state & ControlMask) ? ctrl_modifier : 0)
6230 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
6231 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
6232 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
6233 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
dc6f92b8
JB
6234}
6235
dfeccd2d 6236static unsigned int
334208b7
RS
6237x_emacs_to_x_modifiers (dpyinfo, state)
6238 struct x_display_info *dpyinfo;
dfeccd2d
JB
6239 unsigned int state;
6240{
334208b7
RS
6241 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
6242 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
6243 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
6244 | ((state & shift_modifier) ? ShiftMask : 0)
6245 | ((state & ctrl_modifier) ? ControlMask : 0)
6246 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
dfeccd2d 6247}
d047c4eb
KH
6248
6249/* Convert a keysym to its name. */
6250
6251char *
6252x_get_keysym_name (keysym)
6253 KeySym keysym;
6254{
6255 char *value;
6256
6257 BLOCK_INPUT;
6258 value = XKeysymToString (keysym);
6259 UNBLOCK_INPUT;
6260
6261 return value;
6262}
06a2c219
GM
6263
6264
e4571a43
JB
6265\f
6266/* Mouse clicks and mouse movement. Rah. */
e4571a43 6267
06a2c219
GM
6268/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6269 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6270 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6271 not force the value into range. */
69388238 6272
c8dba240 6273void
69388238 6274pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
e4571a43 6275 FRAME_PTR f;
69388238 6276 register int pix_x, pix_y;
e4571a43
JB
6277 register int *x, *y;
6278 XRectangle *bounds;
69388238 6279 int noclip;
e4571a43 6280{
06a2c219 6281 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
69388238
RS
6282 even for negative values. */
6283 if (pix_x < 0)
7556890b 6284 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
69388238 6285 if (pix_y < 0)
7556890b 6286 pix_y -= (f)->output_data.x->line_height - 1;
69388238 6287
e4571a43
JB
6288 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6289 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6290
6291 if (bounds)
6292 {
7556890b
RS
6293 bounds->width = FONT_WIDTH (f->output_data.x->font);
6294 bounds->height = f->output_data.x->line_height;
e4571a43
JB
6295 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6296 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6297 }
6298
69388238
RS
6299 if (!noclip)
6300 {
6301 if (pix_x < 0)
6302 pix_x = 0;
3cbd2e0b
RS
6303 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6304 pix_x = FRAME_WINDOW_WIDTH (f);
69388238
RS
6305
6306 if (pix_y < 0)
6307 pix_y = 0;
6308 else if (pix_y > f->height)
6309 pix_y = f->height;
6310 }
e4571a43
JB
6311
6312 *x = pix_x;
6313 *y = pix_y;
6314}
6315
06a2c219
GM
6316
6317/* Given HPOS/VPOS in the current matrix of W, return corresponding
6318 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6319 can't tell the positions because W's display is not up to date,
6320 return 0. */
6321
6322int
6323glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6324 struct window *w;
6325 int hpos, vpos;
6326 int *frame_x, *frame_y;
2b5c9e71 6327{
06a2c219
GM
6328 int success_p;
6329
6330 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6331 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6332
6333 if (display_completed)
6334 {
6335 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6336 struct glyph *glyph = row->glyphs[TEXT_AREA];
6337 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6338
6339 *frame_y = row->y;
6340 *frame_x = row->x;
6341 while (glyph < end)
6342 {
6343 *frame_x += glyph->pixel_width;
6344 ++glyph;
6345 }
6346
6347 success_p = 1;
6348 }
6349 else
6350 {
6351 *frame_y = *frame_x = 0;
6352 success_p = 0;
6353 }
6354
6355 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6356 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6357 return success_p;
2b5c9e71
RS
6358}
6359
06a2c219 6360
dc6f92b8
JB
6361/* Prepare a mouse-event in *RESULT for placement in the input queue.
6362
6363 If the event is a button press, then note that we have grabbed
f451eb13 6364 the mouse. */
dc6f92b8
JB
6365
6366static Lisp_Object
f451eb13 6367construct_mouse_click (result, event, f)
dc6f92b8
JB
6368 struct input_event *result;
6369 XButtonEvent *event;
f676886a 6370 struct frame *f;
dc6f92b8 6371{
f451eb13 6372 /* Make the event type no_event; we'll change that when we decide
dc6f92b8 6373 otherwise. */
f451eb13 6374 result->kind = mouse_click;
69388238 6375 result->code = event->button - Button1;
1113d9db 6376 result->timestamp = event->time;
334208b7
RS
6377 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6378 event->state)
f689eb05 6379 | (event->type == ButtonRelease
58769bee 6380 ? up_modifier
f689eb05 6381 : down_modifier));
dc6f92b8 6382
06a2c219
GM
6383 XSETINT (result->x, event->x);
6384 XSETINT (result->y, event->y);
6385 XSETFRAME (result->frame_or_window, f);
0f8aabe9 6386 result->arg = Qnil;
06a2c219 6387 return Qnil;
dc6f92b8 6388}
b849c413 6389
69388238 6390\f
90e65f07
JB
6391/* Function to report a mouse movement to the mainstream Emacs code.
6392 The input handler calls this.
6393
6394 We have received a mouse movement event, which is given in *event.
6395 If the mouse is over a different glyph than it was last time, tell
6396 the mainstream emacs code by setting mouse_moved. If not, ask for
6397 another motion event, so we can check again the next time it moves. */
b8009dd1 6398
06a2c219
GM
6399static XMotionEvent last_mouse_motion_event;
6400static Lisp_Object last_mouse_motion_frame;
6401
90e65f07 6402static void
12ba150f 6403note_mouse_movement (frame, event)
f676886a 6404 FRAME_PTR frame;
90e65f07 6405 XMotionEvent *event;
90e65f07 6406{
e5d77022 6407 last_mouse_movement_time = event->time;
06a2c219
GM
6408 last_mouse_motion_event = *event;
6409 XSETFRAME (last_mouse_motion_frame, frame);
e5d77022 6410
27f338af
RS
6411 if (event->window != FRAME_X_WINDOW (frame))
6412 {
39d8bb4d 6413 frame->mouse_moved = 1;
27f338af 6414 last_mouse_scroll_bar = Qnil;
27f338af 6415 note_mouse_highlight (frame, -1, -1);
27f338af
RS
6416 }
6417
90e65f07 6418 /* Has the mouse moved off the glyph it was on at the last sighting? */
27f338af
RS
6419 else if (event->x < last_mouse_glyph.x
6420 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6421 || event->y < last_mouse_glyph.y
6422 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
12ba150f 6423 {
39d8bb4d 6424 frame->mouse_moved = 1;
ab648270 6425 last_mouse_scroll_bar = Qnil;
b8009dd1 6426 note_mouse_highlight (frame, event->x, event->y);
90e65f07
JB
6427 }
6428}
6429
bf1c0ba1 6430/* This is used for debugging, to turn off note_mouse_highlight. */
bf1c0ba1 6431
06a2c219
GM
6432 int disable_mouse_highlight;
6433
6434
6435\f
6436/************************************************************************
6437 Mouse Face
6438 ************************************************************************/
6439
6440/* Find the glyph under window-relative coordinates X/Y in window W.
6441 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6442 strings. Return in *HPOS and *VPOS the row and column number of
6443 the glyph found. Return in *AREA the glyph area containing X.
6444 Value is a pointer to the glyph found or null if X/Y is not on
6445 text, or we can't tell because W's current matrix is not up to
6446 date. */
6447
6448static struct glyph *
6449x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6450 struct window *w;
6451 int x, y;
6452 int *hpos, *vpos, *area;
6453{
6454 struct glyph *glyph, *end;
3e71d8f2 6455 struct glyph_row *row = NULL;
06a2c219
GM
6456 int x0, i, left_area_width;
6457
6458 /* Find row containing Y. Give up if some row is not enabled. */
6459 for (i = 0; i < w->current_matrix->nrows; ++i)
6460 {
6461 row = MATRIX_ROW (w->current_matrix, i);
6462 if (!row->enabled_p)
6463 return NULL;
6464 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6465 break;
6466 }
6467
6468 *vpos = i;
6469 *hpos = 0;
6470
6471 /* Give up if Y is not in the window. */
6472 if (i == w->current_matrix->nrows)
6473 return NULL;
6474
6475 /* Get the glyph area containing X. */
6476 if (w->pseudo_window_p)
6477 {
6478 *area = TEXT_AREA;
6479 x0 = 0;
6480 }
6481 else
6482 {
6483 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6484 if (x < left_area_width)
6485 {
6486 *area = LEFT_MARGIN_AREA;
6487 x0 = 0;
6488 }
6489 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6490 {
6491 *area = TEXT_AREA;
6492 x0 = row->x + left_area_width;
6493 }
6494 else
6495 {
6496 *area = RIGHT_MARGIN_AREA;
6497 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6498 }
6499 }
6500
6501 /* Find glyph containing X. */
6502 glyph = row->glyphs[*area];
6503 end = glyph + row->used[*area];
6504 while (glyph < end)
6505 {
6506 if (x < x0 + glyph->pixel_width)
6507 {
6508 if (w->pseudo_window_p)
6509 break;
6510 else if (BUFFERP (glyph->object))
6511 break;
6512 }
6513
6514 x0 += glyph->pixel_width;
6515 ++glyph;
6516 }
6517
6518 if (glyph == end)
6519 return NULL;
6520
6521 *hpos = glyph - row->glyphs[*area];
6522 return glyph;
6523}
6524
6525
6526/* Convert frame-relative x/y to coordinates relative to window W.
6527 Takes pseudo-windows into account. */
6528
6529static void
6530frame_to_window_pixel_xy (w, x, y)
6531 struct window *w;
6532 int *x, *y;
6533{
6534 if (w->pseudo_window_p)
6535 {
6536 /* A pseudo-window is always full-width, and starts at the
6537 left edge of the frame, plus a frame border. */
6538 struct frame *f = XFRAME (w->frame);
6539 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6540 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6541 }
6542 else
6543 {
6544 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6545 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6546 }
6547}
6548
6549
e371a781 6550/* Take proper action when mouse has moved to the mode or header line of
06a2c219
GM
6551 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6552 mode line. X is relative to the start of the text display area of
6553 W, so the width of bitmap areas and scroll bars must be subtracted
6554 to get a position relative to the start of the mode line. */
6555
6556static void
6557note_mode_line_highlight (w, x, mode_line_p)
6558 struct window *w;
6559 int x, mode_line_p;
6560{
6561 struct frame *f = XFRAME (w->frame);
6562 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6563 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6564 struct glyph_row *row;
6565
6566 if (mode_line_p)
6567 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6568 else
045dee35 6569 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
e371a781 6570
06a2c219
GM
6571 if (row->enabled_p)
6572 {
6573 struct glyph *glyph, *end;
6574 Lisp_Object help, map;
6575 int x0;
6576
6577 /* Find the glyph under X. */
6578 glyph = row->glyphs[TEXT_AREA];
6579 end = glyph + row->used[TEXT_AREA];
6580 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
110859fc 6581 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
e371a781 6582
06a2c219
GM
6583 while (glyph < end
6584 && x >= x0 + glyph->pixel_width)
6585 {
6586 x0 += glyph->pixel_width;
6587 ++glyph;
6588 }
6589
6590 if (glyph < end
6591 && STRINGP (glyph->object)
6592 && XSTRING (glyph->object)->intervals
6593 && glyph->charpos >= 0
6594 && glyph->charpos < XSTRING (glyph->object)->size)
6595 {
6596 /* If we're on a string with `help-echo' text property,
6597 arrange for the help to be displayed. This is done by
6598 setting the global variable help_echo to the help string. */
6599 help = Fget_text_property (make_number (glyph->charpos),
6600 Qhelp_echo, glyph->object);
b7e80413 6601 if (!NILP (help))
be010514
GM
6602 {
6603 help_echo = help;
7cea38bc 6604 XSETWINDOW (help_echo_window, w);
be010514
GM
6605 help_echo_object = glyph->object;
6606 help_echo_pos = glyph->charpos;
6607 }
06a2c219
GM
6608
6609 /* Change the mouse pointer according to what is under X/Y. */
6610 map = Fget_text_property (make_number (glyph->charpos),
6611 Qlocal_map, glyph->object);
02067692 6612 if (KEYMAPP (map))
06a2c219 6613 cursor = f->output_data.x->nontext_cursor;
be010514
GM
6614 else
6615 {
6616 map = Fget_text_property (make_number (glyph->charpos),
6617 Qkeymap, glyph->object);
02067692 6618 if (KEYMAPP (map))
be010514
GM
6619 cursor = f->output_data.x->nontext_cursor;
6620 }
06a2c219
GM
6621 }
6622 }
6623
6624 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6625}
6626
6627
6628/* Take proper action when the mouse has moved to position X, Y on
6629 frame F as regards highlighting characters that have mouse-face
6630 properties. Also de-highlighting chars where the mouse was before.
27f338af 6631 X and Y can be negative or out of range. */
b8009dd1
RS
6632
6633static void
6634note_mouse_highlight (f, x, y)
06a2c219 6635 struct frame *f;
c32cdd9a 6636 int x, y;
b8009dd1 6637{
06a2c219
GM
6638 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6639 int portion;
b8009dd1
RS
6640 Lisp_Object window;
6641 struct window *w;
6642
06a2c219
GM
6643 /* When a menu is active, don't highlight because this looks odd. */
6644#ifdef USE_X_TOOLKIT
6645 if (popup_activated ())
6646 return;
6647#endif
6648
04fff9c0
GM
6649 if (disable_mouse_highlight
6650 || !f->glyphs_initialized_p)
bf1c0ba1
RS
6651 return;
6652
06a2c219
GM
6653 dpyinfo->mouse_face_mouse_x = x;
6654 dpyinfo->mouse_face_mouse_y = y;
6655 dpyinfo->mouse_face_mouse_frame = f;
b8009dd1 6656
06a2c219 6657 if (dpyinfo->mouse_face_defer)
b8009dd1
RS
6658 return;
6659
514e4681
RS
6660 if (gc_in_progress)
6661 {
06a2c219 6662 dpyinfo->mouse_face_deferred_gc = 1;
514e4681
RS
6663 return;
6664 }
6665
b8009dd1 6666 /* Which window is that in? */
06a2c219 6667 window = window_from_coordinates (f, x, y, &portion, 1);
b8009dd1
RS
6668
6669 /* If we were displaying active text in another window, clear that. */
06a2c219
GM
6670 if (! EQ (window, dpyinfo->mouse_face_window))
6671 clear_mouse_face (dpyinfo);
6672
6673 /* Not on a window -> return. */
6674 if (!WINDOWP (window))
6675 return;
6676
6677 /* Convert to window-relative pixel coordinates. */
6678 w = XWINDOW (window);
6679 frame_to_window_pixel_xy (w, &x, &y);
6680
9ea173e8 6681 /* Handle tool-bar window differently since it doesn't display a
06a2c219 6682 buffer. */
9ea173e8 6683 if (EQ (window, f->tool_bar_window))
06a2c219 6684 {
9ea173e8 6685 note_tool_bar_highlight (f, x, y);
06a2c219
GM
6686 return;
6687 }
6688
6689 if (portion == 1 || portion == 3)
6690 {
6691 /* Mouse is on the mode or top line. */
6692 note_mode_line_highlight (w, x, portion == 1);
6693 return;
6694 }
e371a781
GM
6695 else if (portion == 2)
6696 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6697 f->output_data.x->horizontal_drag_cursor);
06a2c219
GM
6698 else
6699 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6700 f->output_data.x->text_cursor);
b8009dd1 6701
0cdd0c9f
RS
6702 /* Are we in a window whose display is up to date?
6703 And verify the buffer's text has not changed. */
06a2c219
GM
6704 if (/* Within text portion of the window. */
6705 portion == 0
0cdd0c9f 6706 && EQ (w->window_end_valid, w->buffer)
26459b28
KH
6707 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6708 && (XFASTINT (w->last_overlay_modified)
6709 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
b8009dd1 6710 {
06a2c219
GM
6711 int hpos, vpos, pos, i, area;
6712 struct glyph *glyph;
b8009dd1 6713
06a2c219
GM
6714 /* Find the glyph under X/Y. */
6715 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6716
6717 /* Clear mouse face if X/Y not over text. */
6718 if (glyph == NULL
6719 || area != TEXT_AREA
6720 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
b8009dd1 6721 {
06a2c219
GM
6722 clear_mouse_face (dpyinfo);
6723 return;
6724 }
6725
6726 pos = glyph->charpos;
6727 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6728
6729 /* Check for mouse-face and help-echo. */
6730 {
0a61c667 6731 Lisp_Object mouse_face = Qnil, overlay, position;
06a2c219
GM
6732 Lisp_Object *overlay_vec;
6733 int len, noverlays;
6734 struct buffer *obuf;
6735 int obegv, ozv;
6736
6737 /* If we get an out-of-range value, return now; avoid an error. */
6738 if (pos > BUF_Z (XBUFFER (w->buffer)))
6739 return;
6740
6741 /* Make the window's buffer temporarily current for
6742 overlays_at and compute_char_face. */
6743 obuf = current_buffer;
6744 current_buffer = XBUFFER (w->buffer);
6745 obegv = BEGV;
6746 ozv = ZV;
6747 BEGV = BEG;
6748 ZV = Z;
6749
6750 /* Is this char mouse-active or does it have help-echo? */
6751 XSETINT (position, pos);
6752
6753 /* Put all the overlays we want in a vector in overlay_vec.
6754 Store the length in len. If there are more than 10, make
6755 enough space for all, and try again. */
6756 len = 10;
6757 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
bc592036 6758 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
06a2c219
GM
6759 if (noverlays > len)
6760 {
6761 len = noverlays;
6762 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
bc592036 6763 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
06a2c219 6764 }
f8349001
GM
6765
6766 /* Sort overlays into increasing priority order. */
06a2c219
GM
6767 noverlays = sort_overlays (overlay_vec, noverlays, w);
6768
6769 /* Check mouse-face highlighting. */
6770 if (! (EQ (window, dpyinfo->mouse_face_window)
6771 && vpos >= dpyinfo->mouse_face_beg_row
6772 && vpos <= dpyinfo->mouse_face_end_row
6773 && (vpos > dpyinfo->mouse_face_beg_row
6774 || hpos >= dpyinfo->mouse_face_beg_col)
6775 && (vpos < dpyinfo->mouse_face_end_row
6776 || hpos < dpyinfo->mouse_face_end_col
0a61c667
GM
6777 || dpyinfo->mouse_face_past_end))
6778 /* If there exists an overlay with mouse-face overlapping
6779 the one we are currently highlighting, we have to
6780 check if we enter the overlapping overlay, and then
6781 highlight only that. */
6782 || (OVERLAYP (dpyinfo->mouse_face_overlay)
6783 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
6784
06a2c219
GM
6785 {
6786 /* Clear the display of the old active region, if any. */
6787 clear_mouse_face (dpyinfo);
6788
6789 /* Find the highest priority overlay that has a mouse-face prop. */
6790 overlay = Qnil;
f8349001 6791 for (i = noverlays - 1; i >= 0; --i)
06a2c219
GM
6792 {
6793 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6794 if (!NILP (mouse_face))
6795 {
6796 overlay = overlay_vec[i];
6797 break;
6798 }
6799 }
6800
6801 /* If no overlay applies, get a text property. */
6802 if (NILP (overlay))
6803 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6804
0a61c667
GM
6805 dpyinfo->mouse_face_overlay = overlay;
6806
06a2c219 6807 /* Handle the overlay case. */
0a61c667 6808 if (!NILP (overlay))
06a2c219
GM
6809 {
6810 /* Find the range of text around this char that
6811 should be active. */
6812 Lisp_Object before, after;
6813 int ignore;
6814
6815 before = Foverlay_start (overlay);
6816 after = Foverlay_end (overlay);
6817 /* Record this as the current active region. */
6818 fast_find_position (w, XFASTINT (before),
6819 &dpyinfo->mouse_face_beg_col,
6820 &dpyinfo->mouse_face_beg_row,
6821 &dpyinfo->mouse_face_beg_x,
6822 &dpyinfo->mouse_face_beg_y);
6823 dpyinfo->mouse_face_past_end
6824 = !fast_find_position (w, XFASTINT (after),
6825 &dpyinfo->mouse_face_end_col,
6826 &dpyinfo->mouse_face_end_row,
6827 &dpyinfo->mouse_face_end_x,
6828 &dpyinfo->mouse_face_end_y);
6829 dpyinfo->mouse_face_window = window;
6830 dpyinfo->mouse_face_face_id
6831 = face_at_buffer_position (w, pos, 0, 0,
6832 &ignore, pos + 1, 1);
6833
6834 /* Display it as active. */
6835 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6836 }
6837 /* Handle the text property case. */
6838 else if (! NILP (mouse_face))
6839 {
6840 /* Find the range of text around this char that
6841 should be active. */
6842 Lisp_Object before, after, beginning, end;
6843 int ignore;
6844
6845 beginning = Fmarker_position (w->start);
6846 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6847 - XFASTINT (w->window_end_pos)));
6848 before
6849 = Fprevious_single_property_change (make_number (pos + 1),
6850 Qmouse_face,
6851 w->buffer, beginning);
6852 after
6853 = Fnext_single_property_change (position, Qmouse_face,
6854 w->buffer, end);
6855 /* Record this as the current active region. */
6856 fast_find_position (w, XFASTINT (before),
6857 &dpyinfo->mouse_face_beg_col,
6858 &dpyinfo->mouse_face_beg_row,
6859 &dpyinfo->mouse_face_beg_x,
6860 &dpyinfo->mouse_face_beg_y);
6861 dpyinfo->mouse_face_past_end
6862 = !fast_find_position (w, XFASTINT (after),
6863 &dpyinfo->mouse_face_end_col,
6864 &dpyinfo->mouse_face_end_row,
6865 &dpyinfo->mouse_face_end_x,
6866 &dpyinfo->mouse_face_end_y);
6867 dpyinfo->mouse_face_window = window;
6868 dpyinfo->mouse_face_face_id
6869 = face_at_buffer_position (w, pos, 0, 0,
6870 &ignore, pos + 1, 1);
6871
6872 /* Display it as active. */
6873 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6874 }
6875 }
6876
6877 /* Look for a `help-echo' property. */
6878 {
743934db 6879 Lisp_Object help, overlay;
06a2c219
GM
6880
6881 /* Check overlays first. */
f9b5db02 6882 help = overlay = Qnil;
f8349001 6883 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
743934db
GM
6884 {
6885 overlay = overlay_vec[i];
6886 help = Foverlay_get (overlay, Qhelp_echo);
6887 }
be010514
GM
6888
6889 if (!NILP (help))
6890 {
6891 help_echo = help;
7cea38bc 6892 help_echo_window = window;
743934db 6893 help_echo_object = overlay;
be010514
GM
6894 help_echo_pos = pos;
6895 }
6896 else
6897 {
6898 /* Try text properties. */
6899 if ((STRINGP (glyph->object)
06a2c219
GM
6900 && glyph->charpos >= 0
6901 && glyph->charpos < XSTRING (glyph->object)->size)
6902 || (BUFFERP (glyph->object)
6903 && glyph->charpos >= BEGV
be010514
GM
6904 && glyph->charpos < ZV))
6905 help = Fget_text_property (make_number (glyph->charpos),
6906 Qhelp_echo, glyph->object);
06a2c219 6907
be010514
GM
6908 if (!NILP (help))
6909 {
6910 help_echo = help;
7cea38bc 6911 help_echo_window = window;
be010514
GM
6912 help_echo_object = glyph->object;
6913 help_echo_pos = glyph->charpos;
6914 }
6915 }
06a2c219
GM
6916 }
6917
6918 BEGV = obegv;
6919 ZV = ozv;
6920 current_buffer = obuf;
6921 }
6922 }
6923}
6924
6925static void
6926redo_mouse_highlight ()
6927{
6928 if (!NILP (last_mouse_motion_frame)
6929 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6930 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6931 last_mouse_motion_event.x,
6932 last_mouse_motion_event.y);
6933}
6934
6935
6936\f
6937/***********************************************************************
9ea173e8 6938 Tool-bars
06a2c219
GM
6939 ***********************************************************************/
6940
9ea173e8
GM
6941static int x_tool_bar_item P_ ((struct frame *, int, int,
6942 struct glyph **, int *, int *, int *));
06a2c219 6943
9ea173e8 6944/* Tool-bar item index of the item on which a mouse button was pressed
06a2c219
GM
6945 or -1. */
6946
9ea173e8 6947static int last_tool_bar_item;
06a2c219
GM
6948
6949
9ea173e8
GM
6950/* Get information about the tool-bar item at position X/Y on frame F.
6951 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6952 the current matrix of the tool-bar window of F, or NULL if not
6953 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8daf1204 6954 item in F->tool_bar_items. Value is
06a2c219 6955
9ea173e8 6956 -1 if X/Y is not on a tool-bar item
06a2c219
GM
6957 0 if X/Y is on the same item that was highlighted before.
6958 1 otherwise. */
6959
6960static int
9ea173e8 6961x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
06a2c219
GM
6962 struct frame *f;
6963 int x, y;
6964 struct glyph **glyph;
6965 int *hpos, *vpos, *prop_idx;
6966{
6967 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6968 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6969 int area;
6970
6971 /* Find the glyph under X/Y. */
6972 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6973 if (*glyph == NULL)
6974 return -1;
6975
9ea173e8 6976 /* Get the start of this tool-bar item's properties in
8daf1204 6977 f->tool_bar_items. */
9ea173e8 6978 if (!tool_bar_item_info (f, *glyph, prop_idx))
06a2c219
GM
6979 return -1;
6980
6981 /* Is mouse on the highlighted item? */
9ea173e8 6982 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
06a2c219
GM
6983 && *vpos >= dpyinfo->mouse_face_beg_row
6984 && *vpos <= dpyinfo->mouse_face_end_row
6985 && (*vpos > dpyinfo->mouse_face_beg_row
6986 || *hpos >= dpyinfo->mouse_face_beg_col)
6987 && (*vpos < dpyinfo->mouse_face_end_row
6988 || *hpos < dpyinfo->mouse_face_end_col
6989 || dpyinfo->mouse_face_past_end))
6990 return 0;
6991
6992 return 1;
6993}
6994
6995
9ea173e8 6996/* Handle mouse button event on the tool-bar of frame F, at
06a2c219
GM
6997 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6998 or ButtonRelase. */
6999
7000static void
9ea173e8 7001x_handle_tool_bar_click (f, button_event)
06a2c219
GM
7002 struct frame *f;
7003 XButtonEvent *button_event;
7004{
7005 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 7006 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
7007 int hpos, vpos, prop_idx;
7008 struct glyph *glyph;
7009 Lisp_Object enabled_p;
7010 int x = button_event->x;
7011 int y = button_event->y;
7012
9ea173e8 7013 /* If not on the highlighted tool-bar item, return. */
06a2c219 7014 frame_to_window_pixel_xy (w, &x, &y);
9ea173e8 7015 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
06a2c219
GM
7016 return;
7017
7018 /* If item is disabled, do nothing. */
8daf1204 7019 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
06a2c219
GM
7020 if (NILP (enabled_p))
7021 return;
7022
7023 if (button_event->type == ButtonPress)
7024 {
7025 /* Show item in pressed state. */
7026 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7027 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9ea173e8 7028 last_tool_bar_item = prop_idx;
06a2c219
GM
7029 }
7030 else
7031 {
7032 Lisp_Object key, frame;
7033 struct input_event event;
7034
7035 /* Show item in released state. */
7036 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7037 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7038
8daf1204 7039 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
06a2c219
GM
7040
7041 XSETFRAME (frame, f);
9ea173e8 7042 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
7043 event.frame_or_window = frame;
7044 event.arg = frame;
06a2c219
GM
7045 kbd_buffer_store_event (&event);
7046
9ea173e8 7047 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
7048 event.frame_or_window = frame;
7049 event.arg = key;
06a2c219
GM
7050 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
7051 button_event->state);
7052 kbd_buffer_store_event (&event);
9ea173e8 7053 last_tool_bar_item = -1;
06a2c219
GM
7054 }
7055}
7056
7057
9ea173e8
GM
7058/* Possibly highlight a tool-bar item on frame F when mouse moves to
7059 tool-bar window-relative coordinates X/Y. Called from
06a2c219
GM
7060 note_mouse_highlight. */
7061
7062static void
9ea173e8 7063note_tool_bar_highlight (f, x, y)
06a2c219
GM
7064 struct frame *f;
7065 int x, y;
7066{
9ea173e8 7067 Lisp_Object window = f->tool_bar_window;
06a2c219
GM
7068 struct window *w = XWINDOW (window);
7069 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7070 int hpos, vpos;
7071 struct glyph *glyph;
7072 struct glyph_row *row;
5c187dee 7073 int i;
06a2c219
GM
7074 Lisp_Object enabled_p;
7075 int prop_idx;
7076 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
5c187dee 7077 int mouse_down_p, rc;
06a2c219
GM
7078
7079 /* Function note_mouse_highlight is called with negative x(y
7080 values when mouse moves outside of the frame. */
7081 if (x <= 0 || y <= 0)
7082 {
7083 clear_mouse_face (dpyinfo);
7084 return;
7085 }
7086
9ea173e8 7087 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
06a2c219
GM
7088 if (rc < 0)
7089 {
9ea173e8 7090 /* Not on tool-bar item. */
06a2c219
GM
7091 clear_mouse_face (dpyinfo);
7092 return;
7093 }
7094 else if (rc == 0)
9ea173e8 7095 /* On same tool-bar item as before. */
06a2c219 7096 goto set_help_echo;
b8009dd1 7097
06a2c219
GM
7098 clear_mouse_face (dpyinfo);
7099
9ea173e8 7100 /* Mouse is down, but on different tool-bar item? */
06a2c219
GM
7101 mouse_down_p = (dpyinfo->grabbed
7102 && f == last_mouse_frame
7103 && FRAME_LIVE_P (f));
7104 if (mouse_down_p
9ea173e8 7105 && last_tool_bar_item != prop_idx)
06a2c219
GM
7106 return;
7107
7108 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7109 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7110
9ea173e8 7111 /* If tool-bar item is not enabled, don't highlight it. */
8daf1204 7112 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
06a2c219
GM
7113 if (!NILP (enabled_p))
7114 {
7115 /* Compute the x-position of the glyph. In front and past the
7116 image is a space. We include this is the highlighted area. */
7117 row = MATRIX_ROW (w->current_matrix, vpos);
7118 for (i = x = 0; i < hpos; ++i)
7119 x += row->glyphs[TEXT_AREA][i].pixel_width;
7120
7121 /* Record this as the current active region. */
7122 dpyinfo->mouse_face_beg_col = hpos;
7123 dpyinfo->mouse_face_beg_row = vpos;
7124 dpyinfo->mouse_face_beg_x = x;
7125 dpyinfo->mouse_face_beg_y = row->y;
7126 dpyinfo->mouse_face_past_end = 0;
7127
7128 dpyinfo->mouse_face_end_col = hpos + 1;
7129 dpyinfo->mouse_face_end_row = vpos;
7130 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7131 dpyinfo->mouse_face_end_y = row->y;
7132 dpyinfo->mouse_face_window = window;
9ea173e8 7133 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
06a2c219
GM
7134
7135 /* Display it as active. */
7136 show_mouse_face (dpyinfo, draw);
7137 dpyinfo->mouse_face_image_state = draw;
b8009dd1 7138 }
06a2c219
GM
7139
7140 set_help_echo:
7141
9ea173e8 7142 /* Set help_echo to a help string.to display for this tool-bar item.
06a2c219 7143 XTread_socket does the rest. */
7cea38bc 7144 help_echo_object = help_echo_window = Qnil;
be010514 7145 help_echo_pos = -1;
8daf1204 7146 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
b7e80413 7147 if (NILP (help_echo))
8daf1204 7148 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
b8009dd1 7149}
4d73d038 7150
06a2c219
GM
7151
7152\f
7153/* Find the glyph matrix position of buffer position POS in window W.
7154 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7155 current glyphs must be up to date. If POS is above window start
7156 return (0, 0, 0, 0). If POS is after end of W, return end of
7157 last line in W. */
b8009dd1
RS
7158
7159static int
06a2c219
GM
7160fast_find_position (w, pos, hpos, vpos, x, y)
7161 struct window *w;
b8009dd1 7162 int pos;
06a2c219 7163 int *hpos, *vpos, *x, *y;
b8009dd1 7164{
b8009dd1 7165 int i;
bf1c0ba1 7166 int lastcol;
06a2c219
GM
7167 int maybe_next_line_p = 0;
7168 int line_start_position;
7169 int yb = window_text_bottom_y (w);
7170 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
7171 struct glyph_row *best_row = row;
7172 int row_vpos = 0, best_row_vpos = 0;
7173 int current_x;
7174
7175 while (row->y < yb)
b8009dd1 7176 {
06a2c219
GM
7177 if (row->used[TEXT_AREA])
7178 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7179 else
7180 line_start_position = 0;
7181
7182 if (line_start_position > pos)
b8009dd1 7183 break;
77b68646
RS
7184 /* If the position sought is the end of the buffer,
7185 don't include the blank lines at the bottom of the window. */
06a2c219
GM
7186 else if (line_start_position == pos
7187 && pos == BUF_ZV (XBUFFER (w->buffer)))
77b68646 7188 {
06a2c219 7189 maybe_next_line_p = 1;
77b68646
RS
7190 break;
7191 }
06a2c219
GM
7192 else if (line_start_position > 0)
7193 {
7194 best_row = row;
7195 best_row_vpos = row_vpos;
7196 }
4b0bb6f3
GM
7197
7198 if (row->y + row->height >= yb)
7199 break;
06a2c219
GM
7200
7201 ++row;
7202 ++row_vpos;
b8009dd1 7203 }
06a2c219
GM
7204
7205 /* Find the right column within BEST_ROW. */
7206 lastcol = 0;
7207 current_x = best_row->x;
7208 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
bf1c0ba1 7209 {
06a2c219
GM
7210 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7211 int charpos;
7212
7213 charpos = glyph->charpos;
7214 if (charpos == pos)
bf1c0ba1 7215 {
06a2c219
GM
7216 *hpos = i;
7217 *vpos = best_row_vpos;
7218 *x = current_x;
7219 *y = best_row->y;
bf1c0ba1
RS
7220 return 1;
7221 }
06a2c219 7222 else if (charpos > pos)
4d73d038 7223 break;
06a2c219
GM
7224 else if (charpos > 0)
7225 lastcol = i;
7226
7227 current_x += glyph->pixel_width;
bf1c0ba1 7228 }
b8009dd1 7229
77b68646
RS
7230 /* If we're looking for the end of the buffer,
7231 and we didn't find it in the line we scanned,
7232 use the start of the following line. */
06a2c219 7233 if (maybe_next_line_p)
77b68646 7234 {
06a2c219
GM
7235 ++best_row;
7236 ++best_row_vpos;
7237 lastcol = 0;
7238 current_x = best_row->x;
77b68646
RS
7239 }
7240
06a2c219
GM
7241 *vpos = best_row_vpos;
7242 *hpos = lastcol + 1;
7243 *x = current_x;
7244 *y = best_row->y;
b8009dd1
RS
7245 return 0;
7246}
7247
06a2c219 7248
b8009dd1
RS
7249/* Display the active region described by mouse_face_*
7250 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7251
7252static void
06a2c219 7253show_mouse_face (dpyinfo, draw)
7a13e894 7254 struct x_display_info *dpyinfo;
06a2c219 7255 enum draw_glyphs_face draw;
b8009dd1 7256{
7a13e894 7257 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
06a2c219 7258 struct frame *f = XFRAME (WINDOW_FRAME (w));
b8009dd1 7259 int i;
06a2c219
GM
7260 int cursor_off_p = 0;
7261 struct cursor_pos saved_cursor;
7262
7263 saved_cursor = output_cursor;
7264
7265 /* If window is in the process of being destroyed, don't bother
7266 to do anything. */
7267 if (w->current_matrix == NULL)
7268 goto set_x_cursor;
7269
7270 /* Recognize when we are called to operate on rows that don't exist
7271 anymore. This can happen when a window is split. */
7272 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
7273 goto set_x_cursor;
7274
7275 set_output_cursor (&w->phys_cursor);
7276
7277 /* Note that mouse_face_beg_row etc. are window relative. */
7278 for (i = dpyinfo->mouse_face_beg_row;
7279 i <= dpyinfo->mouse_face_end_row;
7280 i++)
7281 {
7282 int start_hpos, end_hpos, start_x;
7283 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
7284
7285 /* Don't do anything if row doesn't have valid contents. */
7286 if (!row->enabled_p)
7287 continue;
7288
7289 /* For all but the first row, the highlight starts at column 0. */
7290 if (i == dpyinfo->mouse_face_beg_row)
7291 {
7292 start_hpos = dpyinfo->mouse_face_beg_col;
7293 start_x = dpyinfo->mouse_face_beg_x;
7294 }
7295 else
7296 {
7297 start_hpos = 0;
7298 start_x = 0;
7299 }
7300
7301 if (i == dpyinfo->mouse_face_end_row)
7302 end_hpos = dpyinfo->mouse_face_end_col;
7303 else
7304 end_hpos = row->used[TEXT_AREA];
7305
7306 /* If the cursor's in the text we are about to rewrite, turn the
7307 cursor off. */
7308 if (!w->pseudo_window_p
7309 && i == output_cursor.vpos
7310 && output_cursor.hpos >= start_hpos - 1
7311 && output_cursor.hpos <= end_hpos)
514e4681 7312 {
06a2c219
GM
7313 x_update_window_cursor (w, 0);
7314 cursor_off_p = 1;
514e4681 7315 }
b8009dd1 7316
06a2c219 7317 if (end_hpos > start_hpos)
64f26cf5
GM
7318 {
7319 row->mouse_face_p = draw == DRAW_MOUSE_FACE;
7320 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7321 start_hpos, end_hpos, draw, NULL, NULL, 0);
7322 }
b8009dd1
RS
7323 }
7324
514e4681 7325 /* If we turned the cursor off, turn it back on. */
06a2c219
GM
7326 if (cursor_off_p)
7327 x_display_cursor (w, 1,
7328 output_cursor.hpos, output_cursor.vpos,
7329 output_cursor.x, output_cursor.y);
2729a2b5 7330
06a2c219 7331 output_cursor = saved_cursor;
fb3b7de5 7332
06a2c219
GM
7333 set_x_cursor:
7334
7335 /* Change the mouse cursor. */
7336 if (draw == DRAW_NORMAL_TEXT)
7337 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7338 f->output_data.x->text_cursor);
7339 else if (draw == DRAW_MOUSE_FACE)
334208b7 7340 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 7341 f->output_data.x->cross_cursor);
27ead1d5 7342 else
334208b7 7343 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
06a2c219 7344 f->output_data.x->nontext_cursor);
b8009dd1
RS
7345}
7346
7347/* Clear out the mouse-highlighted active region.
06a2c219 7348 Redraw it un-highlighted first. */
b8009dd1 7349
06a2c219 7350void
7a13e894
RS
7351clear_mouse_face (dpyinfo)
7352 struct x_display_info *dpyinfo;
b8009dd1 7353{
607d9f9f
GM
7354#if 0 /* This prevents redrawing tool bar items when changing from one
7355 to another while a tooltip is open, so don't do it. */
685f4368 7356 if (!NILP (tip_frame))
06a2c219 7357 return;
7948d50a 7358#endif
06a2c219 7359
7a13e894 7360 if (! NILP (dpyinfo->mouse_face_window))
06a2c219 7361 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
b8009dd1 7362
7a13e894
RS
7363 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7364 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7365 dpyinfo->mouse_face_window = Qnil;
b8009dd1 7366}
e687d06e 7367
71b8321e
GM
7368
7369/* Clear any mouse-face on window W. This function is part of the
7370 redisplay interface, and is called from try_window_id and similar
7371 functions to ensure the mouse-highlight is off. */
7372
7373static void
7374x_clear_mouse_face (w)
7375 struct window *w;
7376{
7377 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7378 Lisp_Object window;
7379
2e636f9d 7380 BLOCK_INPUT;
71b8321e
GM
7381 XSETWINDOW (window, w);
7382 if (EQ (window, dpyinfo->mouse_face_window))
7383 clear_mouse_face (dpyinfo);
2e636f9d 7384 UNBLOCK_INPUT;
71b8321e
GM
7385}
7386
7387
e687d06e
RS
7388/* Just discard the mouse face information for frame F, if any.
7389 This is used when the size of F is changed. */
7390
dfcf069d 7391void
e687d06e
RS
7392cancel_mouse_face (f)
7393 FRAME_PTR f;
7394{
7395 Lisp_Object window;
7396 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7397
7398 window = dpyinfo->mouse_face_window;
7399 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7400 {
7401 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7402 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7403 dpyinfo->mouse_face_window = Qnil;
7404 }
7405}
b52b65bd 7406
b8009dd1 7407\f
b52b65bd
GM
7408static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
7409
7410
7411/* Try to determine frame pixel position and size of the glyph under
7412 frame pixel coordinates X/Y on frame F . Return the position and
7413 size in *RECT. Value is non-zero if we could compute these
7414 values. */
7415
7416static int
7417glyph_rect (f, x, y, rect)
7418 struct frame *f;
7419 int x, y;
7420 XRectangle *rect;
7421{
7422 Lisp_Object window;
7423 int part, found = 0;
7424
7425 window = window_from_coordinates (f, x, y, &part, 0);
7426 if (!NILP (window))
7427 {
7428 struct window *w = XWINDOW (window);
7429 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7430 struct glyph_row *end = r + w->current_matrix->nrows - 1;
7431 int area;
7432
7433 frame_to_window_pixel_xy (w, &x, &y);
7434
7435 for (; !found && r < end && r->enabled_p; ++r)
7436 if (r->y >= y)
7437 {
7438 struct glyph *g = r->glyphs[TEXT_AREA];
7439 struct glyph *end = g + r->used[TEXT_AREA];
7440 int gx;
7441
7442 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
7443 if (gx >= x)
7444 {
7445 rect->width = g->pixel_width;
7446 rect->height = r->height;
7447 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
7448 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
7449 found = 1;
7450 }
7451 }
7452 }
7453
7454 return found;
7455}
7456
12ba150f 7457
90e65f07 7458/* Return the current position of the mouse.
b52b65bd 7459 *FP should be a frame which indicates which display to ask about.
90e65f07 7460
b52b65bd
GM
7461 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
7462 and *PART to the frame, window, and scroll bar part that the mouse
7463 is over. Set *X and *Y to the portion and whole of the mouse's
ab648270 7464 position on the scroll bar.
12ba150f 7465
b52b65bd
GM
7466 If the mouse movement started elsewhere, set *FP to the frame the
7467 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
12ba150f
JB
7468 the mouse is over.
7469
b52b65bd 7470 Set *TIME to the server time-stamp for the time at which the mouse
12ba150f
JB
7471 was at this position.
7472
a135645a
RS
7473 Don't store anything if we don't have a valid set of values to report.
7474
90e65f07 7475 This clears the mouse_moved flag, so we can wait for the next mouse
f5bb65ec 7476 movement. */
90e65f07
JB
7477
7478static void
1cf412ec 7479XTmouse_position (fp, insist, bar_window, part, x, y, time)
334208b7 7480 FRAME_PTR *fp;
1cf412ec 7481 int insist;
12ba150f 7482 Lisp_Object *bar_window;
ab648270 7483 enum scroll_bar_part *part;
90e65f07 7484 Lisp_Object *x, *y;
e5d77022 7485 unsigned long *time;
90e65f07 7486{
a135645a
RS
7487 FRAME_PTR f1;
7488
90e65f07
JB
7489 BLOCK_INPUT;
7490
8bcee03e 7491 if (! NILP (last_mouse_scroll_bar) && insist == 0)
334208b7 7492 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
90e65f07
JB
7493 else
7494 {
12ba150f
JB
7495 Window root;
7496 int root_x, root_y;
90e65f07 7497
12ba150f
JB
7498 Window dummy_window;
7499 int dummy;
7500
39d8bb4d
KH
7501 Lisp_Object frame, tail;
7502
7503 /* Clear the mouse-moved flag for every frame on this display. */
7504 FOR_EACH_FRAME (tail, frame)
7505 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7506 XFRAME (frame)->mouse_moved = 0;
7507
ab648270 7508 last_mouse_scroll_bar = Qnil;
12ba150f
JB
7509
7510 /* Figure out which root window we're on. */
334208b7
RS
7511 XQueryPointer (FRAME_X_DISPLAY (*fp),
7512 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
12ba150f
JB
7513
7514 /* The root window which contains the pointer. */
7515 &root,
7516
7517 /* Trash which we can't trust if the pointer is on
7518 a different screen. */
7519 &dummy_window,
7520
7521 /* The position on that root window. */
58769bee 7522 &root_x, &root_y,
12ba150f
JB
7523
7524 /* More trash we can't trust. */
7525 &dummy, &dummy,
7526
7527 /* Modifier keys and pointer buttons, about which
7528 we don't care. */
7529 (unsigned int *) &dummy);
7530
7531 /* Now we have a position on the root; find the innermost window
7532 containing the pointer. */
7533 {
7534 Window win, child;
7535 int win_x, win_y;
06a2c219 7536 int parent_x = 0, parent_y = 0;
e99db5a1 7537 int count;
12ba150f
JB
7538
7539 win = root;
69388238 7540
2d7fc7e8
RS
7541 /* XTranslateCoordinates can get errors if the window
7542 structure is changing at the same time this function
7543 is running. So at least we must not crash from them. */
7544
e99db5a1 7545 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
2d7fc7e8 7546
334208b7 7547 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
23faf38f 7548 && FRAME_LIVE_P (last_mouse_frame))
12ba150f 7549 {
69388238
RS
7550 /* If mouse was grabbed on a frame, give coords for that frame
7551 even if the mouse is now outside it. */
334208b7 7552 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
69388238 7553
12ba150f 7554 /* From-window, to-window. */
69388238 7555 root, FRAME_X_WINDOW (last_mouse_frame),
12ba150f
JB
7556
7557 /* From-position, to-position. */
7558 root_x, root_y, &win_x, &win_y,
7559
7560 /* Child of win. */
7561 &child);
69388238
RS
7562 f1 = last_mouse_frame;
7563 }
7564 else
7565 {
7566 while (1)
7567 {
334208b7 7568 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
12ba150f 7569
69388238
RS
7570 /* From-window, to-window. */
7571 root, win,
12ba150f 7572
69388238
RS
7573 /* From-position, to-position. */
7574 root_x, root_y, &win_x, &win_y,
7575
7576 /* Child of win. */
7577 &child);
7578
9af3143a 7579 if (child == None || child == win)
69388238
RS
7580 break;
7581
7582 win = child;
7583 parent_x = win_x;
7584 parent_y = win_y;
7585 }
12ba150f 7586
69388238
RS
7587 /* Now we know that:
7588 win is the innermost window containing the pointer
7589 (XTC says it has no child containing the pointer),
7590 win_x and win_y are the pointer's position in it
7591 (XTC did this the last time through), and
7592 parent_x and parent_y are the pointer's position in win's parent.
7593 (They are what win_x and win_y were when win was child.
7594 If win is the root window, it has no parent, and
7595 parent_{x,y} are invalid, but that's okay, because we'll
7596 never use them in that case.) */
7597
7598 /* Is win one of our frames? */
19126e11 7599 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
aad0f6ab
GM
7600
7601#ifdef USE_X_TOOLKIT
7602 /* If we end up with the menu bar window, say it's not
7603 on the frame. */
7604 if (f1 != NULL
7605 && f1->output_data.x->menubar_widget
7606 && win == XtWindow (f1->output_data.x->menubar_widget))
7607 f1 = NULL;
7608#endif /* USE_X_TOOLKIT */
69388238 7609 }
58769bee 7610
2d7fc7e8
RS
7611 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7612 f1 = 0;
7613
e99db5a1 7614 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
2d7fc7e8 7615
ab648270 7616 /* If not, is it one of our scroll bars? */
a135645a 7617 if (! f1)
12ba150f 7618 {
ab648270 7619 struct scroll_bar *bar = x_window_to_scroll_bar (win);
12ba150f
JB
7620
7621 if (bar)
7622 {
a135645a 7623 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
7624 win_x = parent_x;
7625 win_y = parent_y;
7626 }
7627 }
90e65f07 7628
8bcee03e 7629 if (f1 == 0 && insist > 0)
b86bd3dd 7630 f1 = SELECTED_FRAME ();
1cf412ec 7631
a135645a 7632 if (f1)
12ba150f 7633 {
06a2c219
GM
7634 /* Ok, we found a frame. Store all the values.
7635 last_mouse_glyph is a rectangle used to reduce the
7636 generation of mouse events. To not miss any motion
7637 events, we must divide the frame into rectangles of the
7638 size of the smallest character that could be displayed
7639 on it, i.e. into the same rectangles that matrices on
7640 the frame are divided into. */
7641
b52b65bd
GM
7642 int width, height, gx, gy;
7643 XRectangle rect;
7644
7645 if (glyph_rect (f1, win_x, win_y, &rect))
7646 last_mouse_glyph = rect;
7647 else
7648 {
7649 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7650 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7651 gx = win_x;
7652 gy = win_y;
06a2c219 7653
b52b65bd
GM
7654 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7655 round down even for negative values. */
7656 if (gx < 0)
7657 gx -= width - 1;
4f00e84d 7658 if (gy < 0)
b52b65bd
GM
7659 gy -= height - 1;
7660 gx = (gx + width - 1) / width * width;
7661 gy = (gy + height - 1) / height * height;
7662
7663 last_mouse_glyph.width = width;
7664 last_mouse_glyph.height = height;
7665 last_mouse_glyph.x = gx;
7666 last_mouse_glyph.y = gy;
7667 }
12ba150f
JB
7668
7669 *bar_window = Qnil;
7670 *part = 0;
334208b7 7671 *fp = f1;
e0c1aef2
KH
7672 XSETINT (*x, win_x);
7673 XSETINT (*y, win_y);
12ba150f
JB
7674 *time = last_mouse_movement_time;
7675 }
7676 }
7677 }
90e65f07
JB
7678
7679 UNBLOCK_INPUT;
7680}
f451eb13 7681
06a2c219 7682
06a2c219 7683#ifdef USE_X_TOOLKIT
bffcfca9
GM
7684
7685/* Atimer callback function for TIMER. Called every 0.1s to process
7686 Xt timeouts, if needed. We must avoid calling XtAppPending as
7687 much as possible because that function does an implicit XFlush
7688 that slows us down. */
7689
7690static void
7691x_process_timeouts (timer)
7692 struct atimer *timer;
7693{
7694 if (toolkit_scroll_bar_interaction || popup_activated_flag)
7695 {
7696 BLOCK_INPUT;
7697 while (XtAppPending (Xt_app_con) & XtIMTimer)
7698 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7699 UNBLOCK_INPUT;
7700 }
06a2c219
GM
7701}
7702
bffcfca9 7703#endif /* USE_X_TOOLKIT */
06a2c219
GM
7704
7705\f
7706/* Scroll bar support. */
7707
7708/* Given an X window ID, find the struct scroll_bar which manages it.
7709 This can be called in GC, so we have to make sure to strip off mark
7710 bits. */
bffcfca9 7711
06a2c219
GM
7712static struct scroll_bar *
7713x_window_to_scroll_bar (window_id)
7714 Window window_id;
7715{
7716 Lisp_Object tail;
7717
7718 for (tail = Vframe_list;
7719 XGCTYPE (tail) == Lisp_Cons;
8e713be6 7720 tail = XCDR (tail))
06a2c219
GM
7721 {
7722 Lisp_Object frame, bar, condemned;
7723
8e713be6 7724 frame = XCAR (tail);
06a2c219
GM
7725 /* All elements of Vframe_list should be frames. */
7726 if (! GC_FRAMEP (frame))
7727 abort ();
7728
7729 /* Scan this frame's scroll bar list for a scroll bar with the
7730 right window ID. */
7731 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7732 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7733 /* This trick allows us to search both the ordinary and
7734 condemned scroll bar lists with one loop. */
7735 ! GC_NILP (bar) || (bar = condemned,
7736 condemned = Qnil,
7737 ! GC_NILP (bar));
7738 bar = XSCROLL_BAR (bar)->next)
7739 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7740 return XSCROLL_BAR (bar);
7741 }
7742
7743 return 0;
7744}
7745
7746
7747\f
7748/************************************************************************
7749 Toolkit scroll bars
7750 ************************************************************************/
7751
eccc05db 7752#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
7753
7754static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7755static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7756static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7757 struct scroll_bar *));
7758static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7759 int, int, int));
7760
7761
7762/* Id of action hook installed for scroll bars. */
7763
7764static XtActionHookId action_hook_id;
7765
7766/* Lisp window being scrolled. Set when starting to interact with
7767 a toolkit scroll bar, reset to nil when ending the interaction. */
7768
7769static Lisp_Object window_being_scrolled;
7770
7771/* Last scroll bar part sent in xm_scroll_callback. */
7772
7773static int last_scroll_bar_part;
7774
ec18280f
SM
7775/* Whether this is an Xaw with arrow-scrollbars. This should imply
7776 that movements of 1/20 of the screen size are mapped to up/down. */
7777
7778static Boolean xaw3d_arrow_scroll;
7779
7780/* Whether the drag scrolling maintains the mouse at the top of the
7781 thumb. If not, resizing the thumb needs to be done more carefully
7782 to avoid jerkyness. */
7783
7784static Boolean xaw3d_pick_top;
7785
06a2c219
GM
7786
7787/* Action hook installed via XtAppAddActionHook when toolkit scroll
ec18280f 7788 bars are used.. The hook is responsible for detecting when
06a2c219
GM
7789 the user ends an interaction with the scroll bar, and generates
7790 a `end-scroll' scroll_bar_click' event if so. */
7791
7792static void
7793xt_action_hook (widget, client_data, action_name, event, params,
7794 num_params)
7795 Widget widget;
7796 XtPointer client_data;
7797 String action_name;
7798 XEvent *event;
7799 String *params;
7800 Cardinal *num_params;
7801{
7802 int scroll_bar_p;
7803 char *end_action;
7804
7805#ifdef USE_MOTIF
7806 scroll_bar_p = XmIsScrollBar (widget);
7807 end_action = "Release";
ec18280f 7808#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
7809 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7810 end_action = "EndScroll";
ec18280f 7811#endif /* USE_MOTIF */
06a2c219 7812
06a2c219
GM
7813 if (scroll_bar_p
7814 && strcmp (action_name, end_action) == 0
7815 && WINDOWP (window_being_scrolled))
7816 {
7817 struct window *w;
7818
7819 x_send_scroll_bar_event (window_being_scrolled,
7820 scroll_bar_end_scroll, 0, 0);
7821 w = XWINDOW (window_being_scrolled);
7822 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7823 window_being_scrolled = Qnil;
7824 last_scroll_bar_part = -1;
bffcfca9
GM
7825
7826 /* Xt timeouts no longer needed. */
7827 toolkit_scroll_bar_interaction = 0;
06a2c219
GM
7828 }
7829}
7830
07b3d16e
GM
7831/* A vector of windows used for communication between
7832 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
7833
7834static struct window **scroll_bar_windows;
7835static int scroll_bar_windows_size;
7836
06a2c219
GM
7837
7838/* Send a client message with message type Xatom_Scrollbar for a
7839 scroll action to the frame of WINDOW. PART is a value identifying
7840 the part of the scroll bar that was clicked on. PORTION is the
7841 amount to scroll of a whole of WHOLE. */
7842
7843static void
7844x_send_scroll_bar_event (window, part, portion, whole)
7845 Lisp_Object window;
7846 int part, portion, whole;
7847{
7848 XEvent event;
7849 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
07b3d16e
GM
7850 struct window *w = XWINDOW (window);
7851 struct frame *f = XFRAME (w->frame);
7852 int i;
06a2c219 7853
07b3d16e
GM
7854 BLOCK_INPUT;
7855
06a2c219
GM
7856 /* Construct a ClientMessage event to send to the frame. */
7857 ev->type = ClientMessage;
7858 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7859 ev->display = FRAME_X_DISPLAY (f);
7860 ev->window = FRAME_X_WINDOW (f);
7861 ev->format = 32;
07b3d16e
GM
7862
7863 /* We can only transfer 32 bits in the XClientMessageEvent, which is
7864 not enough to store a pointer or Lisp_Object on a 64 bit system.
7865 So, store the window in scroll_bar_windows and pass the index
7866 into that array in the event. */
7867 for (i = 0; i < scroll_bar_windows_size; ++i)
7868 if (scroll_bar_windows[i] == NULL)
7869 break;
7870
7871 if (i == scroll_bar_windows_size)
7872 {
7873 int new_size = max (10, 2 * scroll_bar_windows_size);
7874 size_t nbytes = new_size * sizeof *scroll_bar_windows;
7875 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
7876
7877 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
7878 nbytes);
7879 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
7880 scroll_bar_windows_size = new_size;
7881 }
7882
7883 scroll_bar_windows[i] = w;
7884 ev->data.l[0] = (long) i;
06a2c219
GM
7885 ev->data.l[1] = (long) part;
7886 ev->data.l[2] = (long) 0;
7887 ev->data.l[3] = (long) portion;
7888 ev->data.l[4] = (long) whole;
7889
bffcfca9
GM
7890 /* Make Xt timeouts work while the scroll bar is active. */
7891 toolkit_scroll_bar_interaction = 1;
7892
06a2c219
GM
7893 /* Setting the event mask to zero means that the message will
7894 be sent to the client that created the window, and if that
7895 window no longer exists, no event will be sent. */
06a2c219
GM
7896 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7897 UNBLOCK_INPUT;
7898}
7899
7900
7901/* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7902 in *IEVENT. */
7903
7904static void
7905x_scroll_bar_to_input_event (event, ievent)
7906 XEvent *event;
7907 struct input_event *ievent;
7908{
7909 XClientMessageEvent *ev = (XClientMessageEvent *) event;
52e386c2
KR
7910 Lisp_Object window;
7911 struct frame *f;
07b3d16e
GM
7912 struct window *w;
7913
7914 w = scroll_bar_windows[ev->data.l[0]];
7915 scroll_bar_windows[ev->data.l[0]] = NULL;
52e386c2 7916
07b3d16e
GM
7917 XSETWINDOW (window, w);
7918 f = XFRAME (w->frame);
06a2c219
GM
7919
7920 ievent->kind = scroll_bar_click;
7921 ievent->frame_or_window = window;
0f8aabe9 7922 ievent->arg = Qnil;
06a2c219
GM
7923 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7924 ievent->part = ev->data.l[1];
7925 ievent->code = ev->data.l[2];
7926 ievent->x = make_number ((int) ev->data.l[3]);
7927 ievent->y = make_number ((int) ev->data.l[4]);
7928 ievent->modifiers = 0;
7929}
7930
7931
7932#ifdef USE_MOTIF
7933
7934/* Minimum and maximum values used for Motif scroll bars. */
7935
7936#define XM_SB_MIN 1
7937#define XM_SB_MAX 10000000
7938#define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7939
7940
7941/* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7942 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7943 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7944
7945static void
7946xm_scroll_callback (widget, client_data, call_data)
7947 Widget widget;
7948 XtPointer client_data, call_data;
7949{
7950 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7951 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7952 double percent;
7953 int part = -1, whole = 0, portion = 0;
7954
7955 switch (cs->reason)
7956 {
7957 case XmCR_DECREMENT:
7958 bar->dragging = Qnil;
7959 part = scroll_bar_up_arrow;
7960 break;
7961
7962 case XmCR_INCREMENT:
7963 bar->dragging = Qnil;
7964 part = scroll_bar_down_arrow;
7965 break;
7966
7967 case XmCR_PAGE_DECREMENT:
7968 bar->dragging = Qnil;
7969 part = scroll_bar_above_handle;
7970 break;
7971
7972 case XmCR_PAGE_INCREMENT:
7973 bar->dragging = Qnil;
7974 part = scroll_bar_below_handle;
7975 break;
7976
7977 case XmCR_TO_TOP:
7978 bar->dragging = Qnil;
7979 part = scroll_bar_to_top;
7980 break;
7981
7982 case XmCR_TO_BOTTOM:
7983 bar->dragging = Qnil;
7984 part = scroll_bar_to_bottom;
7985 break;
7986
7987 case XmCR_DRAG:
7988 {
7989 int slider_size;
7990 int dragging_down_p = (INTEGERP (bar->dragging)
7991 && XINT (bar->dragging) <= cs->value);
7992
7993 /* Get the slider size. */
7994 BLOCK_INPUT;
7995 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7996 UNBLOCK_INPUT;
7997
7998 /* At the max position of the scroll bar, do a line-wise
7999 movement. Without doing anything, the LessTif scroll bar
8000 calls us with the same cs->value again and again. If we
8001 want to make sure that we can reach the end of the buffer,
8002 we have to do something.
8003
8004 Implementation note: setting bar->dragging always to
8005 cs->value gives a smoother movement at the max position.
8006 Setting it to nil when doing line-wise movement gives
8007 a better slider behavior. */
8008
8009 if (cs->value + slider_size == XM_SB_MAX
8010 || (dragging_down_p
8011 && last_scroll_bar_part == scroll_bar_down_arrow))
8012 {
8013 part = scroll_bar_down_arrow;
8014 bar->dragging = Qnil;
8015 }
8016 else
8017 {
8018 whole = XM_SB_RANGE;
8019 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
8020 part = scroll_bar_handle;
8021 bar->dragging = make_number (cs->value);
8022 }
8023 }
8024 break;
8025
8026 case XmCR_VALUE_CHANGED:
8027 break;
8028 };
8029
8030 if (part >= 0)
8031 {
8032 window_being_scrolled = bar->window;
8033 last_scroll_bar_part = part;
8034 x_send_scroll_bar_event (bar->window, part, portion, whole);
8035 }
8036}
8037
8038
ec18280f 8039#else /* !USE_MOTIF, i.e. Xaw. */
06a2c219
GM
8040
8041
ec18280f 8042/* Xaw scroll bar callback. Invoked when the thumb is dragged.
06a2c219
GM
8043 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
8044 scroll bar struct. CALL_DATA is a pointer to a float saying where
8045 the thumb is. */
8046
8047static void
ec18280f 8048xaw_jump_callback (widget, client_data, call_data)
06a2c219
GM
8049 Widget widget;
8050 XtPointer client_data, call_data;
8051{
8052 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8053 float top = *(float *) call_data;
8054 float shown;
ec18280f
SM
8055 int whole, portion, height;
8056 int part;
06a2c219
GM
8057
8058 /* Get the size of the thumb, a value between 0 and 1. */
8059 BLOCK_INPUT;
ec18280f 8060 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
06a2c219
GM
8061 UNBLOCK_INPUT;
8062
8063 whole = 10000000;
8064 portion = shown < 1 ? top * whole : 0;
06a2c219 8065
ec18280f
SM
8066 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
8067 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
8068 the bottom, so we force the scrolling whenever we see that we're
8069 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
8070 we try to ensure that we always stay two pixels away from the
8071 bottom). */
06a2c219
GM
8072 part = scroll_bar_down_arrow;
8073 else
8074 part = scroll_bar_handle;
8075
8076 window_being_scrolled = bar->window;
8077 bar->dragging = make_number (portion);
8078 last_scroll_bar_part = part;
8079 x_send_scroll_bar_event (bar->window, part, portion, whole);
8080}
8081
8082
ec18280f
SM
8083/* Xaw scroll bar callback. Invoked for incremental scrolling.,
8084 i.e. line or page up or down. WIDGET is the Xaw scroll bar
06a2c219
GM
8085 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
8086 the scroll bar. CALL_DATA is an integer specifying the action that
8087 has taken place. It's magnitude is in the range 0..height of the
8088 scroll bar. Negative values mean scroll towards buffer start.
8089 Values < height of scroll bar mean line-wise movement. */
8090
8091static void
ec18280f 8092xaw_scroll_callback (widget, client_data, call_data)
06a2c219
GM
8093 Widget widget;
8094 XtPointer client_data, call_data;
8095{
8096 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8097 int position = (int) call_data;
8098 Dimension height;
8099 int part;
8100
8101 /* Get the height of the scroll bar. */
8102 BLOCK_INPUT;
8103 XtVaGetValues (widget, XtNheight, &height, NULL);
8104 UNBLOCK_INPUT;
8105
ec18280f
SM
8106 if (abs (position) >= height)
8107 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
8108
8109 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
8110 it maps line-movement to call_data = max(5, height/20). */
8111 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8112 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
06a2c219 8113 else
ec18280f 8114 part = scroll_bar_move_ratio;
06a2c219
GM
8115
8116 window_being_scrolled = bar->window;
8117 bar->dragging = Qnil;
8118 last_scroll_bar_part = part;
ec18280f 8119 x_send_scroll_bar_event (bar->window, part, position, height);
06a2c219
GM
8120}
8121
8122
8123#endif /* not USE_MOTIF */
8124
8125
8126/* Create the widget for scroll bar BAR on frame F. Record the widget
8127 and X window of the scroll bar in BAR. */
8128
8129static void
8130x_create_toolkit_scroll_bar (f, bar)
8131 struct frame *f;
8132 struct scroll_bar *bar;
8133{
8134 Window xwindow;
8135 Widget widget;
8136 Arg av[20];
8137 int ac = 0;
8138 char *scroll_bar_name = "verticalScrollBar";
8139 unsigned long pixel;
8140
8141 BLOCK_INPUT;
8142
8143#ifdef USE_MOTIF
8144 /* LessTif 0.85, problems:
8145
8146 1. When the mouse if over the scroll bar, the scroll bar will
8147 get keyboard events. I didn't find a way to turn this off.
8148
8149 2. Do we have to explicitly set the cursor to get an arrow
8150 cursor (see below)? */
8151
8152 /* Set resources. Create the widget. */
8153 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8154 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8155 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8156 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8157 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8158 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8159 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8160
8161 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8162 if (pixel != -1)
8163 {
8164 XtSetArg (av[ac], XmNforeground, pixel);
8165 ++ac;
8166 }
8167
8168 pixel = f->output_data.x->scroll_bar_background_pixel;
8169 if (pixel != -1)
8170 {
8171 XtSetArg (av[ac], XmNbackground, pixel);
8172 ++ac;
8173 }
8174
8175 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8176 scroll_bar_name, av, ac);
8177
8178 /* Add one callback for everything that can happen. */
8179 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8180 (XtPointer) bar);
8181 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8182 (XtPointer) bar);
8183 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8184 (XtPointer) bar);
8185 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8186 (XtPointer) bar);
8187 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8188 (XtPointer) bar);
8189 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8190 (XtPointer) bar);
8191 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8192 (XtPointer) bar);
8193
8194 /* Realize the widget. Only after that is the X window created. */
8195 XtRealizeWidget (widget);
8196
8197 /* Set the cursor to an arrow. I didn't find a resource to do that.
8198 And I'm wondering why it hasn't an arrow cursor by default. */
8199 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8200 f->output_data.x->nontext_cursor);
8201
ec18280f 8202#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
8203
8204 /* Set resources. Create the widget. The background of the
8205 Xaw3d scroll bar widget is a little bit light for my taste.
8206 We don't alter it here to let users change it according
8207 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8208 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8209 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
ec18280f
SM
8210 /* For smoother scrolling with Xaw3d -sm */
8211 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
8212 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
06a2c219
GM
8213
8214 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8215 if (pixel != -1)
8216 {
8217 XtSetArg (av[ac], XtNforeground, pixel);
8218 ++ac;
8219 }
8220
8221 pixel = f->output_data.x->scroll_bar_background_pixel;
8222 if (pixel != -1)
8223 {
8224 XtSetArg (av[ac], XtNbackground, pixel);
8225 ++ac;
8226 }
8227
8228 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8229 f->output_data.x->edit_widget, av, ac);
ec18280f
SM
8230
8231 {
8232 char *initial = "";
8233 char *val = initial;
8234 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8235 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8236 if (val == initial)
8237 { /* ARROW_SCROLL */
8238 xaw3d_arrow_scroll = True;
8239 /* Isn't that just a personal preference ? -sm */
8240 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8241 }
8242 }
06a2c219
GM
8243
8244 /* Define callbacks. */
ec18280f
SM
8245 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8246 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
06a2c219
GM
8247 (XtPointer) bar);
8248
8249 /* Realize the widget. Only after that is the X window created. */
8250 XtRealizeWidget (widget);
8251
ec18280f 8252#endif /* !USE_MOTIF */
06a2c219
GM
8253
8254 /* Install an action hook that let's us detect when the user
8255 finishes interacting with a scroll bar. */
8256 if (action_hook_id == 0)
8257 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8258
8259 /* Remember X window and widget in the scroll bar vector. */
8260 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8261 xwindow = XtWindow (widget);
8262 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8263
8264 UNBLOCK_INPUT;
8265}
8266
8267
8268/* Set the thumb size and position of scroll bar BAR. We are currently
8269 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8270
8271static void
8272x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8273 struct scroll_bar *bar;
8274 int portion, position, whole;
f451eb13 8275{
e83dc917
GM
8276 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8277 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
06a2c219 8278 float top, shown;
f451eb13 8279
06a2c219
GM
8280 if (whole == 0)
8281 top = 0, shown = 1;
8282 else
f451eb13 8283 {
06a2c219
GM
8284 top = (float) position / whole;
8285 shown = (float) portion / whole;
8286 }
f451eb13 8287
06a2c219 8288 BLOCK_INPUT;
f451eb13 8289
06a2c219
GM
8290#ifdef USE_MOTIF
8291 {
8292 int size, value;
8293 Boolean arrow1_selected, arrow2_selected;
8294 unsigned char flags;
8295 XmScrollBarWidget sb;
8296
ec18280f 8297 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
06a2c219
GM
8298 is the scroll bar's maximum and MIN is the scroll bar's minimum
8299 value. */
8300 size = shown * XM_SB_RANGE;
8301 size = min (size, XM_SB_RANGE);
8302 size = max (size, 1);
8303
8304 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8305 value = top * XM_SB_RANGE;
8306 value = min (value, XM_SB_MAX - size);
8307 value = max (value, XM_SB_MIN);
8308
8309 /* LessTif: Calling XmScrollBarSetValues after an increment or
8310 decrement turns off auto-repeat LessTif-internally. This can
8311 be seen in ScrollBar.c which resets Arrow1Selected and
8312 Arrow2Selected. It also sets internal flags so that LessTif
8313 believes the mouse is in the slider. We either have to change
8314 our code, or work around that by accessing private data. */
8315
8316 sb = (XmScrollBarWidget) widget;
8317 arrow1_selected = sb->scrollBar.arrow1_selected;
8318 arrow2_selected = sb->scrollBar.arrow2_selected;
8319 flags = sb->scrollBar.flags;
8320
8321 if (NILP (bar->dragging))
8322 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8323 else if (last_scroll_bar_part == scroll_bar_down_arrow)
8324 /* This has the negative side effect that the slider value is
ec18280f 8325 not what it would be if we scrolled here using line-wise or
06a2c219
GM
8326 page-wise movement. */
8327 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
8328 else
8329 {
8330 /* If currently dragging, only update the slider size.
8331 This reduces flicker effects. */
8332 int old_value, old_size, increment, page_increment;
8333
8334 XmScrollBarGetValues (widget, &old_value, &old_size,
8335 &increment, &page_increment);
8336 XmScrollBarSetValues (widget, old_value,
8337 min (size, XM_SB_RANGE - old_value),
8338 0, 0, False);
8339 }
8340
8341 sb->scrollBar.arrow1_selected = arrow1_selected;
8342 sb->scrollBar.arrow2_selected = arrow2_selected;
8343 sb->scrollBar.flags = flags;
8344 }
ec18280f 8345#else /* !USE_MOTIF i.e. use Xaw */
06a2c219 8346 {
ec18280f
SM
8347 float old_top, old_shown;
8348 Dimension height;
8349 XtVaGetValues (widget,
8350 XtNtopOfThumb, &old_top,
8351 XtNshown, &old_shown,
8352 XtNheight, &height,
8353 NULL);
8354
8355 /* Massage the top+shown values. */
8356 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8357 top = max (0, min (1, top));
8358 else
8359 top = old_top;
8360 /* Keep two pixels available for moving the thumb down. */
8361 shown = max (0, min (1 - top - (2.0 / height), shown));
06a2c219
GM
8362
8363 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8364 check that your system's configuration file contains a define
8365 for `NARROWPROTO'. See s/freebsd.h for an example. */
ec18280f 8366 if (top != old_top || shown != old_shown)
eb393530 8367 {
ec18280f 8368 if (NILP (bar->dragging))
eb393530 8369 XawScrollbarSetThumb (widget, top, shown);
06a2c219
GM
8370 else
8371 {
ec18280f
SM
8372#ifdef HAVE_XAW3D
8373 ScrollbarWidget sb = (ScrollbarWidget) widget;
3e71d8f2 8374 int scroll_mode = 0;
ec18280f
SM
8375
8376 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8377 if (xaw3d_arrow_scroll)
8378 {
8379 /* Xaw3d stupidly ignores resize requests while dragging
8380 so we have to make it believe it's not in dragging mode. */
8381 scroll_mode = sb->scrollbar.scroll_mode;
8382 if (scroll_mode == 2)
8383 sb->scrollbar.scroll_mode = 0;
8384 }
8385#endif
8386 /* Try to make the scrolling a tad smoother. */
8387 if (!xaw3d_pick_top)
8388 shown = min (shown, old_shown);
8389
8390 XawScrollbarSetThumb (widget, top, shown);
8391
8392#ifdef HAVE_XAW3D
8393 if (xaw3d_arrow_scroll && scroll_mode == 2)
8394 sb->scrollbar.scroll_mode = scroll_mode;
8395#endif
06a2c219 8396 }
06a2c219
GM
8397 }
8398 }
ec18280f 8399#endif /* !USE_MOTIF */
06a2c219
GM
8400
8401 UNBLOCK_INPUT;
f451eb13
JB
8402}
8403
06a2c219
GM
8404#endif /* USE_TOOLKIT_SCROLL_BARS */
8405
8406
8407\f
8408/************************************************************************
8409 Scroll bars, general
8410 ************************************************************************/
8411
8412/* Create a scroll bar and return the scroll bar vector for it. W is
8413 the Emacs window on which to create the scroll bar. TOP, LEFT,
8414 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8415 scroll bar. */
8416
ab648270 8417static struct scroll_bar *
06a2c219
GM
8418x_scroll_bar_create (w, top, left, width, height)
8419 struct window *w;
f451eb13
JB
8420 int top, left, width, height;
8421{
06a2c219 8422 struct frame *f = XFRAME (w->frame);
334208b7
RS
8423 struct scroll_bar *bar
8424 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
f451eb13
JB
8425
8426 BLOCK_INPUT;
8427
eccc05db 8428#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
8429 x_create_toolkit_scroll_bar (f, bar);
8430#else /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8431 {
8432 XSetWindowAttributes a;
8433 unsigned long mask;
5c187dee 8434 Window window;
06a2c219
GM
8435
8436 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8437 if (a.background_pixel == -1)
8438 a.background_pixel = f->output_data.x->background_pixel;
8439
12ba150f 8440 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9a572e2a 8441 | ButtonMotionMask | PointerMotionHintMask
12ba150f 8442 | ExposureMask);
7a13e894 8443 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
f451eb13 8444
dbc4e1c1 8445 mask = (CWBackPixel | CWEventMask | CWCursor);
f451eb13 8446
06a2c219
GM
8447 /* Clear the area of W that will serve as a scroll bar. This is
8448 for the case that a window has been split horizontally. In
8449 this case, no clear_frame is generated to reduce flickering. */
c5e6e06b
GM
8450 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8451 left, top, width,
8452 window_box_height (w), False);
06a2c219
GM
8453
8454 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8455 /* Position and size of scroll bar. */
8456 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8457 top,
8458 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8459 height,
8460 /* Border width, depth, class, and visual. */
8461 0,
8462 CopyFromParent,
8463 CopyFromParent,
8464 CopyFromParent,
8465 /* Attributes. */
8466 mask, &a);
8467 SET_SCROLL_BAR_X_WINDOW (bar, window);
f451eb13 8468 }
06a2c219 8469#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 8470
06a2c219 8471 XSETWINDOW (bar->window, w);
e0c1aef2
KH
8472 XSETINT (bar->top, top);
8473 XSETINT (bar->left, left);
8474 XSETINT (bar->width, width);
8475 XSETINT (bar->height, height);
8476 XSETINT (bar->start, 0);
8477 XSETINT (bar->end, 0);
12ba150f 8478 bar->dragging = Qnil;
f451eb13
JB
8479
8480 /* Add bar to its frame's list of scroll bars. */
334208b7 8481 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 8482 bar->prev = Qnil;
334208b7 8483 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
06a2c219 8484 if (!NILP (bar->next))
e0c1aef2 8485 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13 8486
06a2c219 8487 /* Map the window/widget. */
eccc05db 8488#ifdef USE_TOOLKIT_SCROLL_BARS
f964b4d7
GM
8489 {
8490 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8491 XtConfigureWidget (scroll_bar,
8492 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8493 top,
8494 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8495 max (height, 1), 0);
8496 XtMapWidget (scroll_bar);
8497 }
06a2c219 8498#else /* not USE_TOOLKIT_SCROLL_BARS */
7f9c7f94 8499 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
06a2c219 8500#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8501
8502 UNBLOCK_INPUT;
12ba150f 8503 return bar;
f451eb13
JB
8504}
8505
06a2c219 8506
12ba150f 8507/* Draw BAR's handle in the proper position.
06a2c219 8508
12ba150f
JB
8509 If the handle is already drawn from START to END, don't bother
8510 redrawing it, unless REBUILD is non-zero; in that case, always
8511 redraw it. (REBUILD is handy for drawing the handle after expose
58769bee 8512 events.)
12ba150f
JB
8513
8514 Normally, we want to constrain the start and end of the handle to
06a2c219
GM
8515 fit inside its rectangle, but if the user is dragging the scroll
8516 bar handle, we want to let them drag it down all the way, so that
8517 the bar's top is as far down as it goes; otherwise, there's no way
8518 to move to the very end of the buffer. */
8519
5c187dee
GM
8520#ifndef USE_TOOLKIT_SCROLL_BARS
8521
f451eb13 8522static void
ab648270
JB
8523x_scroll_bar_set_handle (bar, start, end, rebuild)
8524 struct scroll_bar *bar;
f451eb13 8525 int start, end;
12ba150f 8526 int rebuild;
f451eb13 8527{
12ba150f 8528 int dragging = ! NILP (bar->dragging);
ab648270 8529 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8530 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8531 GC gc = f->output_data.x->normal_gc;
12ba150f
JB
8532
8533 /* If the display is already accurate, do nothing. */
8534 if (! rebuild
8535 && start == XINT (bar->start)
8536 && end == XINT (bar->end))
8537 return;
8538
f451eb13
JB
8539 BLOCK_INPUT;
8540
8541 {
d9cdbb3d
RS
8542 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8543 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8544 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
f451eb13
JB
8545
8546 /* Make sure the values are reasonable, and try to preserve
8547 the distance between start and end. */
12ba150f
JB
8548 {
8549 int length = end - start;
8550
8551 if (start < 0)
8552 start = 0;
8553 else if (start > top_range)
8554 start = top_range;
8555 end = start + length;
8556
8557 if (end < start)
8558 end = start;
8559 else if (end > top_range && ! dragging)
8560 end = top_range;
8561 }
f451eb13 8562
ab648270 8563 /* Store the adjusted setting in the scroll bar. */
e0c1aef2
KH
8564 XSETINT (bar->start, start);
8565 XSETINT (bar->end, end);
f451eb13 8566
12ba150f
JB
8567 /* Clip the end position, just for display. */
8568 if (end > top_range)
8569 end = top_range;
f451eb13 8570
ab648270 8571 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
12ba150f
JB
8572 below top positions, to make sure the handle is always at least
8573 that many pixels tall. */
ab648270 8574 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
f451eb13 8575
12ba150f
JB
8576 /* Draw the empty space above the handle. Note that we can't clear
8577 zero-height areas; that means "clear to end of window." */
8578 if (0 < start)
c5e6e06b
GM
8579 x_clear_area (FRAME_X_DISPLAY (f), w,
8580 /* x, y, width, height, and exposures. */
8581 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8582 VERTICAL_SCROLL_BAR_TOP_BORDER,
8583 inside_width, start,
8584 False);
f451eb13 8585
06a2c219
GM
8586 /* Change to proper foreground color if one is specified. */
8587 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8588 XSetForeground (FRAME_X_DISPLAY (f), gc,
8589 f->output_data.x->scroll_bar_foreground_pixel);
8590
12ba150f 8591 /* Draw the handle itself. */
334208b7 8592 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13 8593
12ba150f 8594 /* x, y, width, height */
ab648270
JB
8595 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8596 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
12ba150f 8597 inside_width, end - start);
f451eb13 8598
06a2c219
GM
8599 /* Restore the foreground color of the GC if we changed it above. */
8600 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8601 XSetForeground (FRAME_X_DISPLAY (f), gc,
8602 f->output_data.x->foreground_pixel);
f451eb13 8603
12ba150f
JB
8604 /* Draw the empty space below the handle. Note that we can't
8605 clear zero-height areas; that means "clear to end of window." */
8606 if (end < inside_height)
c5e6e06b
GM
8607 x_clear_area (FRAME_X_DISPLAY (f), w,
8608 /* x, y, width, height, and exposures. */
8609 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8610 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
8611 inside_width, inside_height - end,
8612 False);
f451eb13 8613
f451eb13
JB
8614 }
8615
f451eb13
JB
8616 UNBLOCK_INPUT;
8617}
8618
5c187dee 8619#endif /* !USE_TOOLKIT_SCROLL_BARS */
f451eb13 8620
06a2c219
GM
8621/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8622 nil. */
58769bee 8623
12ba150f 8624static void
ab648270
JB
8625x_scroll_bar_remove (bar)
8626 struct scroll_bar *bar;
12ba150f 8627{
e83dc917 8628 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
8629 BLOCK_INPUT;
8630
eccc05db 8631#ifdef USE_TOOLKIT_SCROLL_BARS
e83dc917
GM
8632 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
8633#else
8634 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8635#endif
06a2c219 8636
ab648270
JB
8637 /* Disassociate this scroll bar from its window. */
8638 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
12ba150f
JB
8639
8640 UNBLOCK_INPUT;
8641}
8642
06a2c219 8643
12ba150f
JB
8644/* Set the handle of the vertical scroll bar for WINDOW to indicate
8645 that we are displaying PORTION characters out of a total of WHOLE
ab648270 8646 characters, starting at POSITION. If WINDOW has no scroll bar,
12ba150f 8647 create one. */
06a2c219 8648
12ba150f 8649static void
06a2c219
GM
8650XTset_vertical_scroll_bar (w, portion, whole, position)
8651 struct window *w;
f451eb13
JB
8652 int portion, whole, position;
8653{
06a2c219 8654 struct frame *f = XFRAME (w->frame);
ab648270 8655 struct scroll_bar *bar;
3c6ede7b 8656 int top, height, left, sb_left, width, sb_width;
06a2c219 8657 int window_x, window_y, window_width, window_height;
06a2c219 8658
3c6ede7b 8659 /* Get window dimensions. */
06a2c219 8660 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
3c6ede7b
GM
8661 top = window_y;
8662 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8663 height = window_height;
06a2c219 8664
3c6ede7b 8665 /* Compute the left edge of the scroll bar area. */
06a2c219 8666 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
3c6ede7b
GM
8667 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8668 else
8669 left = XFASTINT (w->left);
8670 left *= CANON_X_UNIT (f);
8671 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8672
8673 /* Compute the width of the scroll bar which might be less than
8674 the width of the area reserved for the scroll bar. */
8675 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8676 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
06a2c219 8677 else
3c6ede7b 8678 sb_width = width;
12ba150f 8679
3c6ede7b
GM
8680 /* Compute the left edge of the scroll bar. */
8681#ifdef USE_TOOLKIT_SCROLL_BARS
8682 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8683 sb_left = left + width - sb_width - (width - sb_width) / 2;
8684 else
8685 sb_left = left + (width - sb_width) / 2;
8686#else
8687 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8688 sb_left = left + width - sb_width;
8689 else
8690 sb_left = left;
8691#endif
8692
ab648270 8693 /* Does the scroll bar exist yet? */
06a2c219 8694 if (NILP (w->vertical_scroll_bar))
3c6ede7b 8695 {
80c32bcc 8696 BLOCK_INPUT;
f964b4d7
GM
8697 if (width && height)
8698 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8699 left, top, width, height, False);
80c32bcc 8700 UNBLOCK_INPUT;
3c6ede7b
GM
8701 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8702 }
f451eb13 8703 else
12ba150f
JB
8704 {
8705 /* It may just need to be moved and resized. */
06a2c219
GM
8706 unsigned int mask = 0;
8707
8708 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8709
8710 BLOCK_INPUT;
8711
3c6ede7b 8712 if (sb_left != XINT (bar->left))
06a2c219 8713 mask |= CWX;
3c6ede7b 8714 if (top != XINT (bar->top))
06a2c219 8715 mask |= CWY;
3c6ede7b 8716 if (sb_width != XINT (bar->width))
06a2c219 8717 mask |= CWWidth;
3c6ede7b 8718 if (height != XINT (bar->height))
06a2c219
GM
8719 mask |= CWHeight;
8720
8721#ifdef USE_TOOLKIT_SCROLL_BARS
fe6f39d9
GM
8722
8723 /* Since toolkit scroll bars are smaller than the space reserved
8724 for them on the frame, we have to clear "under" them. */
f964b4d7
GM
8725 if (width && height)
8726 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8727 left, top, width, height, False);
06a2c219
GM
8728
8729 /* Move/size the scroll bar widget. */
8730 if (mask)
e83dc917 8731 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
3c6ede7b
GM
8732 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8733 top,
8734 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
f964b4d7 8735 max (height, 1), 0);
06a2c219
GM
8736
8737#else /* not USE_TOOLKIT_SCROLL_BARS */
8738
e1f6572f
RS
8739 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8740 {
8741 /* Clear areas not covered by the scroll bar. This makes sure a
8742 previous mode line display is cleared after C-x 2 C-x 1, for
8743 example. Non-toolkit scroll bars are as wide as the area
8744 reserved for scroll bars - trim at both sides. */
c5e6e06b
GM
8745 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8746 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8747 height, False);
8748 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8749 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8750 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8751 height, False);
e1f6572f 8752 }
06a2c219
GM
8753
8754 /* Move/size the scroll bar window. */
8755 if (mask)
8756 {
8757 XWindowChanges wc;
8758
3c6ede7b
GM
8759 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8760 wc.y = top;
8761 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8762 wc.height = height;
06a2c219
GM
8763 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8764 mask, &wc);
8765 }
8766
8767#endif /* not USE_TOOLKIT_SCROLL_BARS */
8768
8769 /* Remember new settings. */
3c6ede7b
GM
8770 XSETINT (bar->left, sb_left);
8771 XSETINT (bar->top, top);
8772 XSETINT (bar->width, sb_width);
8773 XSETINT (bar->height, height);
06a2c219
GM
8774
8775 UNBLOCK_INPUT;
12ba150f 8776 }
f451eb13 8777
eccc05db 8778#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
8779 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8780#else /* not USE_TOOLKIT_SCROLL_BARS */
ab648270 8781 /* Set the scroll bar's current state, unless we're currently being
f451eb13 8782 dragged. */
12ba150f 8783 if (NILP (bar->dragging))
f451eb13 8784 {
92857db0 8785 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
f451eb13 8786
12ba150f 8787 if (whole == 0)
ab648270 8788 x_scroll_bar_set_handle (bar, 0, top_range, 0);
12ba150f
JB
8789 else
8790 {
43f868f5
JB
8791 int start = ((double) position * top_range) / whole;
8792 int end = ((double) (position + portion) * top_range) / whole;
ab648270 8793 x_scroll_bar_set_handle (bar, start, end, 0);
12ba150f 8794 }
f451eb13 8795 }
06a2c219 8796#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 8797
06a2c219 8798 XSETVECTOR (w->vertical_scroll_bar, bar);
f451eb13
JB
8799}
8800
12ba150f 8801
f451eb13 8802/* The following three hooks are used when we're doing a thorough
ab648270 8803 redisplay of the frame. We don't explicitly know which scroll bars
f451eb13 8804 are going to be deleted, because keeping track of when windows go
12ba150f
JB
8805 away is a real pain - "Can you say set-window-configuration, boys
8806 and girls?" Instead, we just assert at the beginning of redisplay
ab648270 8807 that *all* scroll bars are to be removed, and then save a scroll bar
12ba150f 8808 from the fiery pit when we actually redisplay its window. */
f451eb13 8809
ab648270
JB
8810/* Arrange for all scroll bars on FRAME to be removed at the next call
8811 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
06a2c219
GM
8812 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8813
58769bee 8814static void
ab648270 8815XTcondemn_scroll_bars (frame)
f451eb13
JB
8816 FRAME_PTR frame;
8817{
f9e24cb9
RS
8818 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8819 while (! NILP (FRAME_SCROLL_BARS (frame)))
8820 {
8821 Lisp_Object bar;
8822 bar = FRAME_SCROLL_BARS (frame);
8823 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8824 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8825 XSCROLL_BAR (bar)->prev = Qnil;
8826 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8827 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8828 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8829 }
f451eb13
JB
8830}
8831
fa2dfc30 8832
06a2c219 8833/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
12ba150f 8834 Note that WINDOW isn't necessarily condemned at all. */
fa2dfc30 8835
f451eb13 8836static void
ab648270 8837XTredeem_scroll_bar (window)
12ba150f 8838 struct window *window;
f451eb13 8839{
ab648270 8840 struct scroll_bar *bar;
fa2dfc30 8841 struct frame *f;
12ba150f 8842
ab648270
JB
8843 /* We can't redeem this window's scroll bar if it doesn't have one. */
8844 if (NILP (window->vertical_scroll_bar))
12ba150f
JB
8845 abort ();
8846
ab648270 8847 bar = XSCROLL_BAR (window->vertical_scroll_bar);
12ba150f
JB
8848
8849 /* Unlink it from the condemned list. */
fa2dfc30
GM
8850 f = XFRAME (WINDOW_FRAME (window));
8851 if (NILP (bar->prev))
8852 {
8853 /* If the prev pointer is nil, it must be the first in one of
8854 the lists. */
8855 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
8856 /* It's not condemned. Everything's fine. */
8857 return;
8858 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8859 window->vertical_scroll_bar))
8860 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
8861 else
8862 /* If its prev pointer is nil, it must be at the front of
8863 one or the other! */
8864 abort ();
8865 }
8866 else
8867 XSCROLL_BAR (bar->prev)->next = bar->next;
12ba150f 8868
fa2dfc30
GM
8869 if (! NILP (bar->next))
8870 XSCROLL_BAR (bar->next)->prev = bar->prev;
12ba150f 8871
fa2dfc30
GM
8872 bar->next = FRAME_SCROLL_BARS (f);
8873 bar->prev = Qnil;
8874 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8875 if (! NILP (bar->next))
8876 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13
JB
8877}
8878
ab648270
JB
8879/* Remove all scroll bars on FRAME that haven't been saved since the
8880 last call to `*condemn_scroll_bars_hook'. */
06a2c219 8881
f451eb13 8882static void
ab648270 8883XTjudge_scroll_bars (f)
12ba150f 8884 FRAME_PTR f;
f451eb13 8885{
12ba150f 8886 Lisp_Object bar, next;
f451eb13 8887
ab648270 8888 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
cf7cb199
JB
8889
8890 /* Clear out the condemned list now so we won't try to process any
ab648270
JB
8891 more events on the hapless scroll bars. */
8892 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
cf7cb199
JB
8893
8894 for (; ! NILP (bar); bar = next)
f451eb13 8895 {
ab648270 8896 struct scroll_bar *b = XSCROLL_BAR (bar);
12ba150f 8897
ab648270 8898 x_scroll_bar_remove (b);
12ba150f
JB
8899
8900 next = b->next;
8901 b->next = b->prev = Qnil;
f451eb13 8902 }
12ba150f 8903
ab648270 8904 /* Now there should be no references to the condemned scroll bars,
12ba150f 8905 and they should get garbage-collected. */
f451eb13
JB
8906}
8907
8908
06a2c219
GM
8909/* Handle an Expose or GraphicsExpose event on a scroll bar. This
8910 is a no-op when using toolkit scroll bars.
ab648270
JB
8911
8912 This may be called from a signal handler, so we have to ignore GC
8913 mark bits. */
06a2c219 8914
f451eb13 8915static void
ab648270
JB
8916x_scroll_bar_expose (bar, event)
8917 struct scroll_bar *bar;
f451eb13
JB
8918 XEvent *event;
8919{
06a2c219
GM
8920#ifndef USE_TOOLKIT_SCROLL_BARS
8921
ab648270 8922 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8923 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8924 GC gc = f->output_data.x->normal_gc;
3cbd2e0b 8925 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
12ba150f 8926
f451eb13
JB
8927 BLOCK_INPUT;
8928
ab648270 8929 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
f451eb13 8930
06a2c219 8931 /* Draw a one-pixel border just inside the edges of the scroll bar. */
334208b7 8932 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13
JB
8933
8934 /* x, y, width, height */
d9cdbb3d 8935 0, 0,
3cbd2e0b 8936 XINT (bar->width) - 1 - width_trim - width_trim,
d9cdbb3d
RS
8937 XINT (bar->height) - 1);
8938
f451eb13 8939 UNBLOCK_INPUT;
06a2c219
GM
8940
8941#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8942}
8943
ab648270
JB
8944/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8945 is set to something other than no_event, it is enqueued.
8946
8947 This may be called from a signal handler, so we have to ignore GC
8948 mark bits. */
06a2c219 8949
5c187dee
GM
8950#ifndef USE_TOOLKIT_SCROLL_BARS
8951
f451eb13 8952static void
ab648270
JB
8953x_scroll_bar_handle_click (bar, event, emacs_event)
8954 struct scroll_bar *bar;
f451eb13
JB
8955 XEvent *event;
8956 struct input_event *emacs_event;
8957{
0299d313 8958 if (! GC_WINDOWP (bar->window))
12ba150f
JB
8959 abort ();
8960
ab648270 8961 emacs_event->kind = scroll_bar_click;
69388238 8962 emacs_event->code = event->xbutton.button - Button1;
0299d313
RS
8963 emacs_event->modifiers
8964 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8965 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8966 event->xbutton.state)
8967 | (event->type == ButtonRelease
8968 ? up_modifier
8969 : down_modifier));
12ba150f 8970 emacs_event->frame_or_window = bar->window;
0f8aabe9 8971 emacs_event->arg = Qnil;
f451eb13 8972 emacs_event->timestamp = event->xbutton.time;
12ba150f 8973 {
06a2c219 8974#if 0
d9cdbb3d 8975 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
0299d313 8976 int internal_height
d9cdbb3d 8977 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8978#endif
0299d313 8979 int top_range
d9cdbb3d 8980 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ab648270 8981 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
12ba150f
JB
8982
8983 if (y < 0) y = 0;
8984 if (y > top_range) y = top_range;
8985
8986 if (y < XINT (bar->start))
ab648270
JB
8987 emacs_event->part = scroll_bar_above_handle;
8988 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8989 emacs_event->part = scroll_bar_handle;
12ba150f 8990 else
ab648270 8991 emacs_event->part = scroll_bar_below_handle;
929787e1
JB
8992
8993 /* Just because the user has clicked on the handle doesn't mean
5116f055
JB
8994 they want to drag it. Lisp code needs to be able to decide
8995 whether or not we're dragging. */
929787e1 8996#if 0
12ba150f
JB
8997 /* If the user has just clicked on the handle, record where they're
8998 holding it. */
8999 if (event->type == ButtonPress
ab648270 9000 && emacs_event->part == scroll_bar_handle)
e0c1aef2 9001 XSETINT (bar->dragging, y - XINT (bar->start));
929787e1 9002#endif
12ba150f
JB
9003
9004 /* If the user has released the handle, set it to its final position. */
9005 if (event->type == ButtonRelease
9006 && ! NILP (bar->dragging))
9007 {
9008 int new_start = y - XINT (bar->dragging);
9009 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
f451eb13 9010
ab648270 9011 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
12ba150f
JB
9012 bar->dragging = Qnil;
9013 }
f451eb13 9014
5116f055
JB
9015 /* Same deal here as the other #if 0. */
9016#if 0
58769bee 9017 /* Clicks on the handle are always reported as occurring at the top of
12ba150f 9018 the handle. */
ab648270 9019 if (emacs_event->part == scroll_bar_handle)
12ba150f
JB
9020 emacs_event->x = bar->start;
9021 else
e0c1aef2 9022 XSETINT (emacs_event->x, y);
5116f055 9023#else
e0c1aef2 9024 XSETINT (emacs_event->x, y);
5116f055 9025#endif
f451eb13 9026
e0c1aef2 9027 XSETINT (emacs_event->y, top_range);
12ba150f
JB
9028 }
9029}
f451eb13 9030
ab648270
JB
9031/* Handle some mouse motion while someone is dragging the scroll bar.
9032
9033 This may be called from a signal handler, so we have to ignore GC
9034 mark bits. */
06a2c219 9035
f451eb13 9036static void
ab648270
JB
9037x_scroll_bar_note_movement (bar, event)
9038 struct scroll_bar *bar;
f451eb13
JB
9039 XEvent *event;
9040{
39d8bb4d
KH
9041 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
9042
f451eb13
JB
9043 last_mouse_movement_time = event->xmotion.time;
9044
39d8bb4d 9045 f->mouse_moved = 1;
e0c1aef2 9046 XSETVECTOR (last_mouse_scroll_bar, bar);
f451eb13
JB
9047
9048 /* If we're dragging the bar, display it. */
ab648270 9049 if (! GC_NILP (bar->dragging))
f451eb13
JB
9050 {
9051 /* Where should the handle be now? */
12ba150f 9052 int new_start = event->xmotion.y - XINT (bar->dragging);
f451eb13 9053
12ba150f 9054 if (new_start != XINT (bar->start))
f451eb13 9055 {
12ba150f 9056 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
58769bee 9057
ab648270 9058 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
f451eb13
JB
9059 }
9060 }
f451eb13
JB
9061}
9062
5c187dee
GM
9063#endif /* !USE_TOOLKIT_SCROLL_BARS */
9064
12ba150f 9065/* Return information to the user about the current position of the mouse
ab648270 9066 on the scroll bar. */
06a2c219 9067
12ba150f 9068static void
334208b7
RS
9069x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
9070 FRAME_PTR *fp;
12ba150f 9071 Lisp_Object *bar_window;
ab648270 9072 enum scroll_bar_part *part;
12ba150f
JB
9073 Lisp_Object *x, *y;
9074 unsigned long *time;
9075{
ab648270 9076 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
334208b7
RS
9077 Window w = SCROLL_BAR_X_WINDOW (bar);
9078 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f 9079 int win_x, win_y;
559cb2fb
JB
9080 Window dummy_window;
9081 int dummy_coord;
9082 unsigned int dummy_mask;
12ba150f 9083
cf7cb199
JB
9084 BLOCK_INPUT;
9085
ab648270 9086 /* Get the mouse's position relative to the scroll bar window, and
12ba150f 9087 report that. */
334208b7 9088 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
12ba150f 9089
559cb2fb
JB
9090 /* Root, child, root x and root y. */
9091 &dummy_window, &dummy_window,
9092 &dummy_coord, &dummy_coord,
12ba150f 9093
559cb2fb
JB
9094 /* Position relative to scroll bar. */
9095 &win_x, &win_y,
12ba150f 9096
559cb2fb
JB
9097 /* Mouse buttons and modifier keys. */
9098 &dummy_mask))
7a13e894 9099 ;
559cb2fb
JB
9100 else
9101 {
06a2c219 9102#if 0
559cb2fb 9103 int inside_height
d9cdbb3d 9104 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 9105#endif
559cb2fb 9106 int top_range
d9cdbb3d 9107 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
559cb2fb
JB
9108
9109 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
9110
9111 if (! NILP (bar->dragging))
9112 win_y -= XINT (bar->dragging);
9113
9114 if (win_y < 0)
9115 win_y = 0;
9116 if (win_y > top_range)
9117 win_y = top_range;
9118
334208b7 9119 *fp = f;
7a13e894 9120 *bar_window = bar->window;
559cb2fb
JB
9121
9122 if (! NILP (bar->dragging))
9123 *part = scroll_bar_handle;
9124 else if (win_y < XINT (bar->start))
9125 *part = scroll_bar_above_handle;
9126 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9127 *part = scroll_bar_handle;
9128 else
9129 *part = scroll_bar_below_handle;
12ba150f 9130
e0c1aef2
KH
9131 XSETINT (*x, win_y);
9132 XSETINT (*y, top_range);
12ba150f 9133
39d8bb4d 9134 f->mouse_moved = 0;
559cb2fb
JB
9135 last_mouse_scroll_bar = Qnil;
9136 }
12ba150f 9137
559cb2fb 9138 *time = last_mouse_movement_time;
cf7cb199 9139
cf7cb199 9140 UNBLOCK_INPUT;
12ba150f
JB
9141}
9142
f451eb13 9143
dbc4e1c1 9144/* The screen has been cleared so we may have changed foreground or
ab648270
JB
9145 background colors, and the scroll bars may need to be redrawn.
9146 Clear out the scroll bars, and ask for expose events, so we can
dbc4e1c1
JB
9147 redraw them. */
9148
dfcf069d 9149void
ab648270 9150x_scroll_bar_clear (f)
dbc4e1c1
JB
9151 FRAME_PTR f;
9152{
06a2c219 9153#ifndef USE_TOOLKIT_SCROLL_BARS
dbc4e1c1
JB
9154 Lisp_Object bar;
9155
b80c363e
RS
9156 /* We can have scroll bars even if this is 0,
9157 if we just turned off scroll bar mode.
9158 But in that case we should not clear them. */
9159 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9160 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9161 bar = XSCROLL_BAR (bar)->next)
c5e6e06b
GM
9162 XClearArea (FRAME_X_DISPLAY (f),
9163 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
b80c363e 9164 0, 0, 0, 0, True);
06a2c219 9165#endif /* not USE_TOOLKIT_SCROLL_BARS */
dbc4e1c1
JB
9166}
9167
06a2c219 9168/* This processes Expose events from the menu-bar specific X event
19126e11 9169 loop in xmenu.c. This allows to redisplay the frame if necessary
06a2c219 9170 when handling menu-bar or pop-up items. */
3afe33e7 9171
06a2c219 9172int
3afe33e7
RS
9173process_expose_from_menu (event)
9174 XEvent event;
9175{
9176 FRAME_PTR f;
19126e11 9177 struct x_display_info *dpyinfo;
06a2c219 9178 int frame_exposed_p = 0;
3afe33e7 9179
f94397b5
KH
9180 BLOCK_INPUT;
9181
19126e11
KH
9182 dpyinfo = x_display_info_for_display (event.xexpose.display);
9183 f = x_window_to_frame (dpyinfo, event.xexpose.window);
3afe33e7
RS
9184 if (f)
9185 {
9186 if (f->async_visible == 0)
9187 {
9188 f->async_visible = 1;
9189 f->async_iconified = 0;
06c488fd 9190 f->output_data.x->has_been_visible = 1;
3afe33e7
RS
9191 SET_FRAME_GARBAGED (f);
9192 }
9193 else
9194 {
06a2c219
GM
9195 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9196 event.xexpose.x, event.xexpose.y,
9197 event.xexpose.width, event.xexpose.height);
9198 frame_exposed_p = 1;
3afe33e7
RS
9199 }
9200 }
9201 else
9202 {
9203 struct scroll_bar *bar
9204 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 9205
3afe33e7
RS
9206 if (bar)
9207 x_scroll_bar_expose (bar, &event);
9208 }
f94397b5
KH
9209
9210 UNBLOCK_INPUT;
06a2c219 9211 return frame_exposed_p;
3afe33e7 9212}
09756a85
RS
9213\f
9214/* Define a queue to save up SelectionRequest events for later handling. */
9215
9216struct selection_event_queue
9217 {
9218 XEvent event;
9219 struct selection_event_queue *next;
9220 };
9221
9222static struct selection_event_queue *queue;
9223
9224/* Nonzero means queue up certain events--don't process them yet. */
06a2c219 9225
09756a85
RS
9226static int x_queue_selection_requests;
9227
9228/* Queue up an X event *EVENT, to be processed later. */
dbc4e1c1 9229
09756a85 9230static void
334208b7
RS
9231x_queue_event (f, event)
9232 FRAME_PTR f;
09756a85
RS
9233 XEvent *event;
9234{
9235 struct selection_event_queue *queue_tmp
06a2c219 9236 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
09756a85 9237
58769bee 9238 if (queue_tmp != NULL)
09756a85
RS
9239 {
9240 queue_tmp->event = *event;
9241 queue_tmp->next = queue;
9242 queue = queue_tmp;
9243 }
9244}
9245
9246/* Take all the queued events and put them back
9247 so that they get processed afresh. */
9248
9249static void
db3906fd
RS
9250x_unqueue_events (display)
9251 Display *display;
09756a85 9252{
58769bee 9253 while (queue != NULL)
09756a85
RS
9254 {
9255 struct selection_event_queue *queue_tmp = queue;
db3906fd 9256 XPutBackEvent (display, &queue_tmp->event);
09756a85 9257 queue = queue_tmp->next;
06a2c219 9258 xfree ((char *)queue_tmp);
09756a85
RS
9259 }
9260}
9261
9262/* Start queuing SelectionRequest events. */
9263
9264void
db3906fd
RS
9265x_start_queuing_selection_requests (display)
9266 Display *display;
09756a85
RS
9267{
9268 x_queue_selection_requests++;
9269}
9270
9271/* Stop queuing SelectionRequest events. */
9272
9273void
db3906fd
RS
9274x_stop_queuing_selection_requests (display)
9275 Display *display;
09756a85
RS
9276{
9277 x_queue_selection_requests--;
db3906fd 9278 x_unqueue_events (display);
09756a85 9279}
f451eb13
JB
9280\f
9281/* The main X event-reading loop - XTread_socket. */
dc6f92b8 9282
06a2c219 9283/* Time stamp of enter window event. This is only used by XTread_socket,
dc6f92b8
JB
9284 but we have to put it out here, since static variables within functions
9285 sometimes don't work. */
06a2c219 9286
dc6f92b8
JB
9287static Time enter_timestamp;
9288
11edeb03 9289/* This holds the state XLookupString needs to implement dead keys
58769bee 9290 and other tricks known as "compose processing". _X Window System_
11edeb03
JB
9291 says that a portable program can't use this, but Stephen Gildea assures
9292 me that letting the compiler initialize it to zeros will work okay.
9293
9294 This must be defined outside of XTread_socket, for the same reasons
06a2c219
GM
9295 given for enter_time stamp, above. */
9296
11edeb03
JB
9297static XComposeStatus compose_status;
9298
10e6549c
RS
9299/* Record the last 100 characters stored
9300 to help debug the loss-of-chars-during-GC problem. */
06a2c219 9301
2224b905
RS
9302static int temp_index;
9303static short temp_buffer[100];
10e6549c 9304
7a13e894
RS
9305/* Set this to nonzero to fake an "X I/O error"
9306 on a particular display. */
06a2c219 9307
7a13e894
RS
9308struct x_display_info *XTread_socket_fake_io_error;
9309
2224b905
RS
9310/* When we find no input here, we occasionally do a no-op command
9311 to verify that the X server is still running and we can still talk with it.
9312 We try all the open displays, one by one.
9313 This variable is used for cycling thru the displays. */
06a2c219 9314
2224b905
RS
9315static struct x_display_info *next_noop_dpyinfo;
9316
06a2c219
GM
9317#define SET_SAVED_MENU_EVENT(size) \
9318 do \
9319 { \
9320 if (f->output_data.x->saved_menu_event == 0) \
9321 f->output_data.x->saved_menu_event \
9322 = (XEvent *) xmalloc (sizeof (XEvent)); \
9323 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9324 if (numchars >= 1) \
9325 { \
9326 bufp->kind = menu_bar_activate_event; \
9327 XSETFRAME (bufp->frame_or_window, f); \
0f8aabe9 9328 bufp->arg = Qnil; \
06a2c219
GM
9329 bufp++; \
9330 count++; \
9331 numchars--; \
9332 } \
9333 } \
9334 while (0)
9335
8805890a 9336#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
06a2c219 9337#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8805890a 9338
dc6f92b8
JB
9339/* Read events coming from the X server.
9340 This routine is called by the SIGIO handler.
9341 We return as soon as there are no more events to be read.
9342
9343 Events representing keys are stored in buffer BUFP,
9344 which can hold up to NUMCHARS characters.
9345 We return the number of characters stored into the buffer,
9346 thus pretending to be `read'.
9347
dc6f92b8
JB
9348 EXPECTED is nonzero if the caller knows input is available. */
9349
7c5283e4 9350int
f66868ba 9351XTread_socket (sd, bufp, numchars, expected)
dc6f92b8 9352 register int sd;
8805890a
KH
9353 /* register */ struct input_event *bufp;
9354 /* register */ int numchars;
dc6f92b8
JB
9355 int expected;
9356{
9357 int count = 0;
9358 int nbytes = 0;
dc6f92b8 9359 XEvent event;
f676886a 9360 struct frame *f;
66f55a9d 9361 int event_found = 0;
334208b7 9362 struct x_display_info *dpyinfo;
379b5ac0 9363 struct coding_system coding;
dc6f92b8 9364
9ac0d9e0 9365 if (interrupt_input_blocked)
dc6f92b8 9366 {
9ac0d9e0 9367 interrupt_input_pending = 1;
dc6f92b8
JB
9368 return -1;
9369 }
9370
9ac0d9e0 9371 interrupt_input_pending = 0;
dc6f92b8 9372 BLOCK_INPUT;
c0a04927
RS
9373
9374 /* So people can tell when we have read the available input. */
9375 input_signal_count++;
9376
dc6f92b8 9377 if (numchars <= 0)
06a2c219 9378 abort (); /* Don't think this happens. */
dc6f92b8 9379
bde5503b
GM
9380 ++handling_signal;
9381
379b5ac0
KH
9382 /* The input should be decoded if it is from XIM. Currently the
9383 locale of XIM is the same as that of the system. So, we can use
9384 Vlocale_coding_system which is initialized properly at Emacs
9385 startup time. */
9386 setup_coding_system (Vlocale_coding_system, &coding);
9387 coding.src_multibyte = 0;
9388 coding.dst_multibyte = 1;
9389 /* The input is converted to events, thus we can't handle
9390 composition. Anyway, there's no XIM that gives us composition
9391 information. */
9392 coding.composing = COMPOSITION_DISABLED;
9393
7a13e894
RS
9394 /* Find the display we are supposed to read input for.
9395 It's the one communicating on descriptor SD. */
9396 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9397 {
9398#if 0 /* This ought to be unnecessary; let's verify it. */
dc6f92b8 9399#ifdef FIOSNBIO
7a13e894
RS
9400 /* If available, Xlib uses FIOSNBIO to make the socket
9401 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
e6cbea31 9402 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
06a2c219 9403 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7a13e894 9404 fcntl (dpyinfo->connection, F_SETFL, 0);
c118dd06 9405#endif /* ! defined (FIOSNBIO) */
7a13e894 9406#endif
dc6f92b8 9407
7a13e894
RS
9408#if 0 /* This code can't be made to work, with multiple displays,
9409 and appears not to be used on any system any more.
9410 Also keyboard.c doesn't turn O_NDELAY on and off
9411 for X connections. */
dc6f92b8
JB
9412#ifndef SIGIO
9413#ifndef HAVE_SELECT
7a13e894
RS
9414 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9415 {
9416 extern int read_alarm_should_throw;
9417 read_alarm_should_throw = 1;
9418 XPeekEvent (dpyinfo->display, &event);
9419 read_alarm_should_throw = 0;
9420 }
c118dd06
JB
9421#endif /* HAVE_SELECT */
9422#endif /* SIGIO */
7a13e894 9423#endif
dc6f92b8 9424
7a13e894
RS
9425 /* For debugging, this gives a way to fake an I/O error. */
9426 if (dpyinfo == XTread_socket_fake_io_error)
9427 {
9428 XTread_socket_fake_io_error = 0;
9429 x_io_error_quitter (dpyinfo->display);
9430 }
dc6f92b8 9431
06a2c219 9432 while (XPending (dpyinfo->display))
dc6f92b8 9433 {
7a13e894 9434 XNextEvent (dpyinfo->display, &event);
06a2c219 9435
531483fb 9436#ifdef HAVE_X_I18N
d1bc4182 9437 {
f2be1146
GM
9438 /* Filter events for the current X input method.
9439 XFilterEvent returns non-zero if the input method has
9440 consumed the event. We pass the frame's X window to
9441 XFilterEvent because that's the one for which the IC
9442 was created. */
f5d11644
GM
9443 struct frame *f1 = x_any_window_to_frame (dpyinfo,
9444 event.xclient.window);
9445 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
d1bc4182
RS
9446 break;
9447 }
0cd6403b 9448#endif
7a13e894
RS
9449 event_found = 1;
9450
9451 switch (event.type)
9452 {
9453 case ClientMessage:
c047688c 9454 {
7a13e894
RS
9455 if (event.xclient.message_type
9456 == dpyinfo->Xatom_wm_protocols
9457 && event.xclient.format == 32)
c047688c 9458 {
7a13e894
RS
9459 if (event.xclient.data.l[0]
9460 == dpyinfo->Xatom_wm_take_focus)
c047688c 9461 {
8c1a6a84
RS
9462 /* Use x_any_window_to_frame because this
9463 could be the shell widget window
9464 if the frame has no title bar. */
9465 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6c183ba5
RS
9466#ifdef HAVE_X_I18N
9467 /* Not quite sure this is needed -pd */
8c1a6a84 9468 if (f && FRAME_XIC (f))
6c183ba5
RS
9469 XSetICFocus (FRAME_XIC (f));
9470#endif
f1da8f06
GM
9471#if 0 /* Emacs sets WM hints whose `input' field is `true'. This
9472 instructs the WM to set the input focus automatically for
9473 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
9474 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
9475 it has set the focus. So, XSetInputFocus below is not
9476 needed.
9477
9478 The call to XSetInputFocus below has also caused trouble. In
9479 cases where the XSetInputFocus done by the WM and the one
9480 below are temporally close (on a fast machine), the call
9481 below can generate additional FocusIn events which confuse
9482 Emacs. */
9483
bf7253f4
RS
9484 /* Since we set WM_TAKE_FOCUS, we must call
9485 XSetInputFocus explicitly. But not if f is null,
9486 since that might be an event for a deleted frame. */
7a13e894 9487 if (f)
bf7253f4
RS
9488 {
9489 Display *d = event.xclient.display;
9490 /* Catch and ignore errors, in case window has been
9491 iconified by a window manager such as GWM. */
9492 int count = x_catch_errors (d);
9493 XSetInputFocus (d, event.xclient.window,
e1f6572f
RS
9494 /* The ICCCM says this is
9495 the only valid choice. */
9496 RevertToParent,
bf7253f4
RS
9497 event.xclient.data.l[1]);
9498 /* This is needed to detect the error
9499 if there is an error. */
9500 XSync (d, False);
9501 x_uncatch_errors (d, count);
9502 }
7a13e894 9503 /* Not certain about handling scroll bars here */
f1da8f06 9504#endif /* 0 */
c047688c 9505 }
7a13e894
RS
9506 else if (event.xclient.data.l[0]
9507 == dpyinfo->Xatom_wm_save_yourself)
9508 {
9509 /* Save state modify the WM_COMMAND property to
06a2c219 9510 something which can reinstate us. This notifies
7a13e894
RS
9511 the session manager, who's looking for such a
9512 PropertyNotify. Can restart processing when
06a2c219 9513 a keyboard or mouse event arrives. */
7a13e894
RS
9514 if (numchars > 0)
9515 {
19126e11
KH
9516 f = x_top_window_to_frame (dpyinfo,
9517 event.xclient.window);
7a13e894
RS
9518
9519 /* This is just so we only give real data once
9520 for a single Emacs process. */
b86bd3dd 9521 if (f == SELECTED_FRAME ())
7a13e894
RS
9522 XSetCommand (FRAME_X_DISPLAY (f),
9523 event.xclient.window,
9524 initial_argv, initial_argc);
f000f5c5 9525 else if (f)
7a13e894
RS
9526 XSetCommand (FRAME_X_DISPLAY (f),
9527 event.xclient.window,
9528 0, 0);
9529 }
9530 }
9531 else if (event.xclient.data.l[0]
9532 == dpyinfo->Xatom_wm_delete_window)
1fb20991 9533 {
19126e11
KH
9534 struct frame *f
9535 = x_any_window_to_frame (dpyinfo,
9536 event.xclient.window);
1fb20991 9537
7a13e894
RS
9538 if (f)
9539 {
9540 if (numchars == 0)
9541 abort ();
1fb20991 9542
7a13e894
RS
9543 bufp->kind = delete_window_event;
9544 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9545 bufp->arg = Qnil;
7a13e894
RS
9546 bufp++;
9547
9548 count += 1;
9549 numchars -= 1;
9550 }
1fb20991 9551 }
c047688c 9552 }
7a13e894
RS
9553 else if (event.xclient.message_type
9554 == dpyinfo->Xatom_wm_configure_denied)
9555 {
9556 }
9557 else if (event.xclient.message_type
9558 == dpyinfo->Xatom_wm_window_moved)
9559 {
9560 int new_x, new_y;
19126e11
KH
9561 struct frame *f
9562 = x_window_to_frame (dpyinfo, event.xclient.window);
58769bee 9563
7a13e894
RS
9564 new_x = event.xclient.data.s[0];
9565 new_y = event.xclient.data.s[1];
1fb20991 9566
7a13e894
RS
9567 if (f)
9568 {
7556890b
RS
9569 f->output_data.x->left_pos = new_x;
9570 f->output_data.x->top_pos = new_y;
7a13e894 9571 }
1fb20991 9572 }
0fdff6bb 9573#ifdef HACK_EDITRES
7a13e894
RS
9574 else if (event.xclient.message_type
9575 == dpyinfo->Xatom_editres)
9576 {
19126e11
KH
9577 struct frame *f
9578 = x_any_window_to_frame (dpyinfo, event.xclient.window);
7556890b 9579 _XEditResCheckMessages (f->output_data.x->widget, NULL,
19126e11 9580 &event, NULL);
7a13e894 9581 }
0fdff6bb 9582#endif /* HACK_EDITRES */
06a2c219
GM
9583 else if ((event.xclient.message_type
9584 == dpyinfo->Xatom_DONE)
9585 || (event.xclient.message_type
9586 == dpyinfo->Xatom_PAGE))
9587 {
9588 /* Ghostview job completed. Kill it. We could
9589 reply with "Next" if we received "Page", but we
9590 currently never do because we are interested in
9591 images, only, which should have 1 page. */
06a2c219
GM
9592 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9593 struct frame *f
9594 = x_window_to_frame (dpyinfo, event.xclient.window);
9595 x_kill_gs_process (pixmap, f);
9596 expose_frame (f, 0, 0, 0, 0);
9597 }
9598#ifdef USE_TOOLKIT_SCROLL_BARS
9599 /* Scroll bar callbacks send a ClientMessage from which
9600 we construct an input_event. */
9601 else if (event.xclient.message_type
9602 == dpyinfo->Xatom_Scrollbar)
9603 {
9604 x_scroll_bar_to_input_event (&event, bufp);
9605 ++bufp, ++count, --numchars;
9606 goto out;
9607 }
9608#endif /* USE_TOOLKIT_SCROLL_BARS */
9609 else
9610 goto OTHER;
7a13e894
RS
9611 }
9612 break;
dc6f92b8 9613
7a13e894 9614 case SelectionNotify:
3afe33e7 9615#ifdef USE_X_TOOLKIT
19126e11 9616 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
7a13e894 9617 goto OTHER;
3afe33e7 9618#endif /* not USE_X_TOOLKIT */
dfcf069d 9619 x_handle_selection_notify (&event.xselection);
7a13e894 9620 break;
d56a553a 9621
06a2c219 9622 case SelectionClear: /* Someone has grabbed ownership. */
3afe33e7 9623#ifdef USE_X_TOOLKIT
19126e11 9624 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
7a13e894 9625 goto OTHER;
3afe33e7 9626#endif /* USE_X_TOOLKIT */
7a13e894
RS
9627 {
9628 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
d56a553a 9629
7a13e894
RS
9630 if (numchars == 0)
9631 abort ();
d56a553a 9632
7a13e894
RS
9633 bufp->kind = selection_clear_event;
9634 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9635 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9636 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9637 bufp->frame_or_window = Qnil;
0f8aabe9 9638 bufp->arg = Qnil;
7a13e894 9639 bufp++;
d56a553a 9640
7a13e894
RS
9641 count += 1;
9642 numchars -= 1;
9643 }
9644 break;
dc6f92b8 9645
06a2c219 9646 case SelectionRequest: /* Someone wants our selection. */
3afe33e7 9647#ifdef USE_X_TOOLKIT
19126e11 9648 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
7a13e894 9649 goto OTHER;
3afe33e7 9650#endif /* USE_X_TOOLKIT */
7a13e894 9651 if (x_queue_selection_requests)
19126e11 9652 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
7a13e894
RS
9653 &event);
9654 else
9655 {
9656 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
dc6f92b8 9657
7a13e894
RS
9658 if (numchars == 0)
9659 abort ();
9660
9661 bufp->kind = selection_request_event;
9662 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9663 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9664 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9665 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9666 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9667 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9668 bufp->frame_or_window = Qnil;
0f8aabe9 9669 bufp->arg = Qnil;
7a13e894
RS
9670 bufp++;
9671
9672 count += 1;
9673 numchars -= 1;
9674 }
9675 break;
9676
9677 case PropertyNotify:
3afe33e7 9678#ifdef USE_X_TOOLKIT
19126e11 9679 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
7a13e894 9680 goto OTHER;
3afe33e7 9681#endif /* not USE_X_TOOLKIT */
dfcf069d 9682 x_handle_property_notify (&event.xproperty);
7a13e894 9683 break;
dc6f92b8 9684
7a13e894 9685 case ReparentNotify:
19126e11 9686 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
7a13e894
RS
9687 if (f)
9688 {
9689 int x, y;
7556890b 9690 f->output_data.x->parent_desc = event.xreparent.parent;
7a13e894 9691 x_real_positions (f, &x, &y);
7556890b
RS
9692 f->output_data.x->left_pos = x;
9693 f->output_data.x->top_pos = y;
7a13e894
RS
9694 }
9695 break;
3bd330d4 9696
7a13e894 9697 case Expose:
19126e11 9698 f = x_window_to_frame (dpyinfo, event.xexpose.window);
7a13e894 9699 if (f)
dc6f92b8 9700 {
7a13e894
RS
9701 if (f->async_visible == 0)
9702 {
9703 f->async_visible = 1;
9704 f->async_iconified = 0;
06c488fd 9705 f->output_data.x->has_been_visible = 1;
7a13e894
RS
9706 SET_FRAME_GARBAGED (f);
9707 }
9708 else
06a2c219
GM
9709 expose_frame (x_window_to_frame (dpyinfo,
9710 event.xexpose.window),
9711 event.xexpose.x, event.xexpose.y,
9712 event.xexpose.width, event.xexpose.height);
dc6f92b8
JB
9713 }
9714 else
7a13e894 9715 {
06a2c219
GM
9716#ifdef USE_TOOLKIT_SCROLL_BARS
9717 /* Dispatch event to the widget. */
9718 goto OTHER;
9719#else /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9720 struct scroll_bar *bar
9721 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 9722
7a13e894
RS
9723 if (bar)
9724 x_scroll_bar_expose (bar, &event);
3afe33e7 9725#ifdef USE_X_TOOLKIT
7a13e894
RS
9726 else
9727 goto OTHER;
3afe33e7 9728#endif /* USE_X_TOOLKIT */
06a2c219 9729#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9730 }
9731 break;
dc6f92b8 9732
7a13e894
RS
9733 case GraphicsExpose: /* This occurs when an XCopyArea's
9734 source area was obscured or not
9735 available.*/
19126e11 9736 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
7a13e894
RS
9737 if (f)
9738 {
06a2c219
GM
9739 expose_frame (f,
9740 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9741 event.xgraphicsexpose.width,
9742 event.xgraphicsexpose.height);
7a13e894 9743 }
3afe33e7 9744#ifdef USE_X_TOOLKIT
7a13e894
RS
9745 else
9746 goto OTHER;
3afe33e7 9747#endif /* USE_X_TOOLKIT */
7a13e894 9748 break;
dc6f92b8 9749
7a13e894 9750 case NoExpose: /* This occurs when an XCopyArea's
06a2c219
GM
9751 source area was completely
9752 available */
7a13e894 9753 break;
dc6f92b8 9754
7a13e894 9755 case UnmapNotify:
06a2c219
GM
9756 /* Redo the mouse-highlight after the tooltip has gone. */
9757 if (event.xmap.window == tip_window)
9758 {
9759 tip_window = 0;
9760 redo_mouse_highlight ();
9761 }
9762
91ea2a7a 9763 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
7a13e894
RS
9764 if (f) /* F may no longer exist if
9765 the frame was deleted. */
9766 {
9767 /* While a frame is unmapped, display generation is
9768 disabled; you don't want to spend time updating a
9769 display that won't ever be seen. */
9770 f->async_visible = 0;
9771 /* We can't distinguish, from the event, whether the window
9772 has become iconified or invisible. So assume, if it
9773 was previously visible, than now it is iconified.
1aa6072f
RS
9774 But x_make_frame_invisible clears both
9775 the visible flag and the iconified flag;
9776 and that way, we know the window is not iconified now. */
7a13e894 9777 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
1aa6072f
RS
9778 {
9779 f->async_iconified = 1;
bddd097c 9780
1aa6072f
RS
9781 bufp->kind = iconify_event;
9782 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9783 bufp->arg = Qnil;
1aa6072f
RS
9784 bufp++;
9785 count++;
9786 numchars--;
9787 }
7a13e894 9788 }
7a13e894 9789 goto OTHER;
dc6f92b8 9790
7a13e894 9791 case MapNotify:
06a2c219
GM
9792 if (event.xmap.window == tip_window)
9793 /* The tooltip has been drawn already. Avoid
9794 the SET_FRAME_GARBAGED below. */
9795 goto OTHER;
9796
9797 /* We use x_top_window_to_frame because map events can
9798 come for sub-windows and they don't mean that the
9799 frame is visible. */
19126e11 9800 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
7a13e894
RS
9801 if (f)
9802 {
9803 f->async_visible = 1;
9804 f->async_iconified = 0;
06c488fd 9805 f->output_data.x->has_been_visible = 1;
dc6f92b8 9806
7a13e894
RS
9807 /* wait_reading_process_input will notice this and update
9808 the frame's display structures. */
9809 SET_FRAME_GARBAGED (f);
bddd097c 9810
d806e720
RS
9811 if (f->iconified)
9812 {
9813 bufp->kind = deiconify_event;
9814 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9815 bufp->arg = Qnil;
d806e720
RS
9816 bufp++;
9817 count++;
9818 numchars--;
9819 }
e73ec6fa 9820 else if (! NILP (Vframe_list)
8e713be6 9821 && ! NILP (XCDR (Vframe_list)))
78aa2ba5
KH
9822 /* Force a redisplay sooner or later
9823 to update the frame titles
9824 in case this is the second frame. */
9825 record_asynch_buffer_change ();
7a13e894 9826 }
7a13e894 9827 goto OTHER;
dc6f92b8 9828
7a13e894 9829 case KeyPress:
19126e11 9830 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
f451eb13 9831
eccc05db 9832#if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
9833 /* I couldn't find a way to prevent LessTif scroll bars
9834 from consuming key events. */
9835 if (f == 0)
9836 {
9837 Widget widget = XtWindowToWidget (dpyinfo->display,
9838 event.xkey.window);
9839 if (widget && XmIsScrollBar (widget))
9840 {
9841 widget = XtParent (widget);
9842 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9843 }
9844 }
eccc05db 9845#endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
06a2c219 9846
7a13e894
RS
9847 if (f != 0)
9848 {
9849 KeySym keysym, orig_keysym;
379b5ac0
KH
9850 /* al%imercury@uunet.uu.net says that making this 81
9851 instead of 80 fixed a bug whereby meta chars made
9852 his Emacs hang.
9853
9854 It seems that some version of XmbLookupString has
9855 a bug of not returning XBufferOverflow in
9856 status_return even if the input is too long to
9857 fit in 81 bytes. So, we must prepare sufficient
9858 bytes for copy_buffer. 513 bytes (256 chars for
9859 two-byte character set) seems to be a faily good
9860 approximation. -- 2000.8.10 handa@etl.go.jp */
9861 unsigned char copy_buffer[513];
9862 unsigned char *copy_bufptr = copy_buffer;
9863 int copy_bufsiz = sizeof (copy_buffer);
7a13e894 9864 int modifiers;
64bb1782 9865
7a13e894
RS
9866 event.xkey.state
9867 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9868 extra_keyboard_modifiers);
9869 modifiers = event.xkey.state;
3a2712f9 9870
7a13e894 9871 /* This will have to go some day... */
752a043f 9872
7a13e894
RS
9873 /* make_lispy_event turns chars into control chars.
9874 Don't do it here because XLookupString is too eager. */
9875 event.xkey.state &= ~ControlMask;
5d46f928
RS
9876 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9877 | dpyinfo->super_mod_mask
9878 | dpyinfo->hyper_mod_mask
9879 | dpyinfo->alt_mod_mask);
9880
1cf4a0d1
RS
9881 /* In case Meta is ComposeCharacter,
9882 clear its status. According to Markus Ehrnsperger
9883 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9884 this enables ComposeCharacter to work whether or
9885 not it is combined with Meta. */
9886 if (modifiers & dpyinfo->meta_mod_mask)
9887 bzero (&compose_status, sizeof (compose_status));
9888
6c183ba5
RS
9889#ifdef HAVE_X_I18N
9890 if (FRAME_XIC (f))
9891 {
f5d11644
GM
9892 Status status_return;
9893
6c183ba5 9894 nbytes = XmbLookupString (FRAME_XIC (f),
f5d11644
GM
9895 &event.xkey, copy_bufptr,
9896 copy_bufsiz, &keysym,
6c183ba5 9897 &status_return);
f5d11644
GM
9898 if (status_return == XBufferOverflow)
9899 {
9900 copy_bufsiz = nbytes + 1;
9901 copy_bufptr = (char *) alloca (copy_bufsiz);
9902 nbytes = XmbLookupString (FRAME_XIC (f),
9903 &event.xkey, copy_bufptr,
9904 copy_bufsiz, &keysym,
9905 &status_return);
9906 }
9907
1decb680
PE
9908 if (status_return == XLookupNone)
9909 break;
9910 else if (status_return == XLookupChars)
fdd9d55e
GM
9911 {
9912 keysym = NoSymbol;
9913 modifiers = 0;
9914 }
1decb680
PE
9915 else if (status_return != XLookupKeySym
9916 && status_return != XLookupBoth)
9917 abort ();
6c183ba5
RS
9918 }
9919 else
379b5ac0
KH
9920 nbytes = XLookupString (&event.xkey, copy_bufptr,
9921 copy_bufsiz, &keysym,
9922 &compose_status);
6c183ba5 9923#else
379b5ac0
KH
9924 nbytes = XLookupString (&event.xkey, copy_bufptr,
9925 copy_bufsiz, &keysym,
9926 &compose_status);
6c183ba5 9927#endif
dc6f92b8 9928
7a13e894 9929 orig_keysym = keysym;
55123275 9930
7a13e894
RS
9931 if (numchars > 1)
9932 {
9933 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9934 || keysym == XK_Delete
1097aea0 9935#ifdef XK_ISO_Left_Tab
441affdb 9936 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
1097aea0 9937#endif
852bff8f 9938 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
7a13e894
RS
9939 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9940 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
c34790e0 9941#ifdef HPUX
7a13e894
RS
9942 /* This recognizes the "extended function keys".
9943 It seems there's no cleaner way.
9944 Test IsModifierKey to avoid handling mode_switch
9945 incorrectly. */
9946 || ((unsigned) (keysym) >= XK_Select
9947 && (unsigned)(keysym) < XK_KP_Space)
69388238
RS
9948#endif
9949#ifdef XK_dead_circumflex
7a13e894 9950 || orig_keysym == XK_dead_circumflex
69388238
RS
9951#endif
9952#ifdef XK_dead_grave
7a13e894 9953 || orig_keysym == XK_dead_grave
69388238
RS
9954#endif
9955#ifdef XK_dead_tilde
7a13e894 9956 || orig_keysym == XK_dead_tilde
69388238
RS
9957#endif
9958#ifdef XK_dead_diaeresis
7a13e894 9959 || orig_keysym == XK_dead_diaeresis
69388238
RS
9960#endif
9961#ifdef XK_dead_macron
7a13e894 9962 || orig_keysym == XK_dead_macron
69388238
RS
9963#endif
9964#ifdef XK_dead_degree
7a13e894 9965 || orig_keysym == XK_dead_degree
69388238
RS
9966#endif
9967#ifdef XK_dead_acute
7a13e894 9968 || orig_keysym == XK_dead_acute
69388238
RS
9969#endif
9970#ifdef XK_dead_cedilla
7a13e894 9971 || orig_keysym == XK_dead_cedilla
69388238
RS
9972#endif
9973#ifdef XK_dead_breve
7a13e894 9974 || orig_keysym == XK_dead_breve
69388238
RS
9975#endif
9976#ifdef XK_dead_ogonek
7a13e894 9977 || orig_keysym == XK_dead_ogonek
69388238
RS
9978#endif
9979#ifdef XK_dead_caron
7a13e894 9980 || orig_keysym == XK_dead_caron
69388238
RS
9981#endif
9982#ifdef XK_dead_doubleacute
7a13e894 9983 || orig_keysym == XK_dead_doubleacute
69388238
RS
9984#endif
9985#ifdef XK_dead_abovedot
7a13e894 9986 || orig_keysym == XK_dead_abovedot
c34790e0 9987#endif
7a13e894
RS
9988 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9989 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9990 /* Any "vendor-specific" key is ok. */
9991 || (orig_keysym & (1 << 28)))
9992 && ! (IsModifierKey (orig_keysym)
7719aa06
RS
9993#ifndef HAVE_X11R5
9994#ifdef XK_Mode_switch
7a13e894 9995 || ((unsigned)(orig_keysym) == XK_Mode_switch)
7719aa06
RS
9996#endif
9997#ifdef XK_Num_Lock
7a13e894 9998 || ((unsigned)(orig_keysym) == XK_Num_Lock)
7719aa06
RS
9999#endif
10000#endif /* not HAVE_X11R5 */
7a13e894 10001 ))
dc6f92b8 10002 {
10e6549c
RS
10003 if (temp_index == sizeof temp_buffer / sizeof (short))
10004 temp_index = 0;
7a13e894
RS
10005 temp_buffer[temp_index++] = keysym;
10006 bufp->kind = non_ascii_keystroke;
10007 bufp->code = keysym;
e0c1aef2 10008 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10009 bufp->arg = Qnil;
334208b7
RS
10010 bufp->modifiers
10011 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10012 modifiers);
1113d9db 10013 bufp->timestamp = event.xkey.time;
dc6f92b8 10014 bufp++;
7a13e894
RS
10015 count++;
10016 numchars--;
dc6f92b8 10017 }
7a13e894
RS
10018 else if (numchars > nbytes)
10019 {
10020 register int i;
379b5ac0 10021 register int c;
379b5ac0 10022 int nchars, len;
7a13e894
RS
10023
10024 for (i = 0; i < nbytes; i++)
10025 {
379b5ac0
KH
10026 if (temp_index == (sizeof temp_buffer
10027 / sizeof (short)))
7a13e894 10028 temp_index = 0;
379b5ac0
KH
10029 temp_buffer[temp_index++] = copy_bufptr[i];
10030 }
10031
10032 if (/* If the event is not from XIM, */
10033 event.xkey.keycode != 0
10034 /* or the current locale doesn't request
10035 decoding of the intup data, ... */
10036 || coding.type == coding_type_raw_text
10037 || coding.type == coding_type_no_conversion)
10038 {
10039 /* ... we can use the input data as is. */
10040 nchars = nbytes;
10041 }
10042 else
10043 {
10044 /* We have to decode the input data. */
10045 int require;
10046 unsigned char *p;
10047
10048 require = decoding_buffer_size (&coding, nbytes);
10049 p = (unsigned char *) alloca (require);
10050 coding.mode |= CODING_MODE_LAST_BLOCK;
10051 decode_coding (&coding, copy_bufptr, p,
10052 nbytes, require);
10053 nbytes = coding.produced;
10054 nchars = coding.produced_char;
10055 copy_bufptr = p;
10056 }
10057
10058 /* Convert the input data to a sequence of
10059 character events. */
10060 for (i = 0; i < nbytes; i += len)
10061 {
10062 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
10063 nbytes - i, len);
10064 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
10065 ? ascii_keystroke
10066 : multibyte_char_keystroke);
10067 bufp->code = c;
7a13e894 10068 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10069 bufp->arg = Qnil;
7a13e894
RS
10070 bufp->modifiers
10071 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10072 modifiers);
10073 bufp->timestamp = event.xkey.time;
10074 bufp++;
10075 }
10076
379b5ac0
KH
10077 count += nchars;
10078 numchars -= nchars;
1decb680
PE
10079
10080 if (keysym == NoSymbol)
10081 break;
7a13e894
RS
10082 }
10083 else
10084 abort ();
dc6f92b8 10085 }
10e6549c
RS
10086 else
10087 abort ();
dc6f92b8 10088 }
59ddecde
GM
10089#ifdef HAVE_X_I18N
10090 /* Don't dispatch this event since XtDispatchEvent calls
10091 XFilterEvent, and two calls in a row may freeze the
10092 client. */
10093 break;
10094#else
717ca130 10095 goto OTHER;
59ddecde 10096#endif
f451eb13 10097
f5d11644 10098 case KeyRelease:
59ddecde
GM
10099#ifdef HAVE_X_I18N
10100 /* Don't dispatch this event since XtDispatchEvent calls
10101 XFilterEvent, and two calls in a row may freeze the
10102 client. */
10103 break;
10104#else
f5d11644 10105 goto OTHER;
59ddecde 10106#endif
f5d11644 10107
7a13e894 10108 /* Here's a possible interpretation of the whole
06a2c219
GM
10109 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
10110 you get a FocusIn event, you have to get a FocusOut
10111 event before you relinquish the focus. If you
10112 haven't received a FocusIn event, then a mere
10113 LeaveNotify is enough to free you. */
f451eb13 10114
7a13e894 10115 case EnterNotify:
06a2c219
GM
10116 {
10117 int from_menu_bar_p = 0;
10118
10119 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10120
10121#ifdef LESSTIF_VERSION
10122 /* When clicking outside of a menu bar popup to close
10123 it, we get a FocusIn/ EnterNotify sequence of
10124 events. The flag event.xcrossing.focus is not set
10125 in the EnterNotify event of that sequence because
10126 the focus is in the menu bar,
10127 event.xcrossing.window is the frame's X window.
10128 Unconditionally setting the focus frame to null in
10129 this case is not the right thing, because no event
10130 follows that could set the focus frame to the right
10131 value.
10132
10133 This could be a LessTif bug, but I wasn't able to
10134 reproduce the behavior in a simple test program.
3be7e417
GM
10135 On the other hand, Motif seems to not have this
10136 problem.
06a2c219 10137
3be7e417 10138 (gerd, LessTif 0.92). */
06a2c219
GM
10139
10140 if (!event.xcrossing.focus
10141 && f
10142 && f->output_data.x->menubar_widget)
10143 {
10144 Window focus;
10145 int revert;
10146
10147 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
10148 if (focus == XtWindow (f->output_data.x->menubar_widget))
10149 from_menu_bar_p = 1;
10150 }
10151#endif /* LESSTIF_VERSION */
6d4238f3 10152
06a2c219
GM
10153 if (event.xcrossing.focus || from_menu_bar_p)
10154 {
10155 /* Avoid nasty pop/raise loops. */
10156 if (f && (!(f->auto_raise)
10157 || !(f->auto_lower)
10158 || (event.xcrossing.time - enter_timestamp) > 500))
10159 {
10160 x_new_focus_frame (dpyinfo, f);
10161 enter_timestamp = event.xcrossing.time;
10162 }
10163 }
10164 else if (f == dpyinfo->x_focus_frame)
10165 x_new_focus_frame (dpyinfo, 0);
10166
10167 /* EnterNotify counts as mouse movement,
10168 so update things that depend on mouse position. */
2533c408 10169 if (f && !f->output_data.x->hourglass_p)
06a2c219
GM
10170 note_mouse_movement (f, &event.xmotion);
10171 goto OTHER;
10172 }
dc6f92b8 10173
7a13e894 10174 case FocusIn:
19126e11 10175 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 10176 if (event.xfocus.detail != NotifyPointer)
0f941935 10177 dpyinfo->x_focus_event_frame = f;
7a13e894 10178 if (f)
eb72635f
GM
10179 {
10180 x_new_focus_frame (dpyinfo, f);
10181
10182 /* Don't stop displaying the initial startup message
10183 for a switch-frame event we don't need. */
10184 if (GC_NILP (Vterminal_frame)
10185 && GC_CONSP (Vframe_list)
10186 && !GC_NILP (XCDR (Vframe_list)))
10187 {
10188 bufp->kind = FOCUS_IN_EVENT;
10189 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10190 bufp->arg = Qnil;
eb72635f
GM
10191 ++bufp, ++count, --numchars;
10192 }
10193 }
f9e24cb9 10194
6c183ba5
RS
10195#ifdef HAVE_X_I18N
10196 if (f && FRAME_XIC (f))
10197 XSetICFocus (FRAME_XIC (f));
10198#endif
10199
7a13e894 10200 goto OTHER;
10c5e63d 10201
7a13e894 10202 case LeaveNotify:
19126e11 10203 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
7a13e894 10204 if (f)
10c5e63d 10205 {
06a2c219
GM
10206 int from_menu_bar_p = 0;
10207
7a13e894 10208 if (f == dpyinfo->mouse_face_mouse_frame)
06a2c219
GM
10209 {
10210 /* If we move outside the frame, then we're
10211 certainly no longer on any text in the frame. */
10212 clear_mouse_face (dpyinfo);
10213 dpyinfo->mouse_face_mouse_frame = 0;
10214 }
10215
10216 /* Generate a nil HELP_EVENT to cancel a help-echo.
10217 Do it only if there's something to cancel.
10218 Otherwise, the startup message is cleared when
10219 the mouse leaves the frame. */
10220 if (any_help_event_p)
10221 {
be010514
GM
10222 Lisp_Object frame;
10223 int n;
10224
06a2c219 10225 XSETFRAME (frame, f);
82c5d67a 10226 help_echo = Qnil;
5ab2570d
GM
10227 n = gen_help_event (bufp, numchars,
10228 Qnil, frame, Qnil, Qnil, 0);
be010514 10229 bufp += n, count += n, numchars -= n;
06a2c219 10230 }
7a13e894 10231
06a2c219
GM
10232#ifdef LESSTIF_VERSION
10233 /* Please see the comment at the start of the
10234 EnterNotify case. */
10235 if (!event.xcrossing.focus
10236 && f->output_data.x->menubar_widget)
10237 {
10238 Window focus;
10239 int revert;
10240 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
10241 if (focus == XtWindow (f->output_data.x->menubar_widget))
10242 from_menu_bar_p = 1;
10243 }
10244#endif /* LESSTIF_VERSION */
10245
10246 if (event.xcrossing.focus || from_menu_bar_p)
0f941935 10247 x_mouse_leave (dpyinfo);
10c5e63d 10248 else
7a13e894 10249 {
0f941935
KH
10250 if (f == dpyinfo->x_focus_event_frame)
10251 dpyinfo->x_focus_event_frame = 0;
10252 if (f == dpyinfo->x_focus_frame)
10253 x_new_focus_frame (dpyinfo, 0);
7a13e894 10254 }
10c5e63d 10255 }
7a13e894 10256 goto OTHER;
dc6f92b8 10257
7a13e894 10258 case FocusOut:
19126e11 10259 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 10260 if (event.xfocus.detail != NotifyPointer
0f941935
KH
10261 && f == dpyinfo->x_focus_event_frame)
10262 dpyinfo->x_focus_event_frame = 0;
10263 if (f && f == dpyinfo->x_focus_frame)
10264 x_new_focus_frame (dpyinfo, 0);
f9e24cb9 10265
6c183ba5
RS
10266#ifdef HAVE_X_I18N
10267 if (f && FRAME_XIC (f))
10268 XUnsetICFocus (FRAME_XIC (f));
10269#endif
10270
7a13e894 10271 goto OTHER;
dc6f92b8 10272
7a13e894 10273 case MotionNotify:
dc6f92b8 10274 {
06a2c219 10275 previous_help_echo = help_echo;
7cea38bc 10276 help_echo = help_echo_object = help_echo_window = Qnil;
be010514 10277 help_echo_pos = -1;
06a2c219 10278
7a13e894
RS
10279 if (dpyinfo->grabbed && last_mouse_frame
10280 && FRAME_LIVE_P (last_mouse_frame))
10281 f = last_mouse_frame;
10282 else
19126e11 10283 f = x_window_to_frame (dpyinfo, event.xmotion.window);
06a2c219 10284
7a13e894
RS
10285 if (f)
10286 note_mouse_movement (f, &event.xmotion);
10287 else
10288 {
e88b3c50 10289#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
10290 struct scroll_bar *bar
10291 = x_window_to_scroll_bar (event.xmotion.window);
f451eb13 10292
7a13e894
RS
10293 if (bar)
10294 x_scroll_bar_note_movement (bar, &event);
e88b3c50 10295#endif /* USE_TOOLKIT_SCROLL_BARS */
b8009dd1 10296
06a2c219
GM
10297 /* If we move outside the frame, then we're
10298 certainly no longer on any text in the frame. */
7a13e894
RS
10299 clear_mouse_face (dpyinfo);
10300 }
06a2c219
GM
10301
10302 /* If the contents of the global variable help_echo
10303 has changed, generate a HELP_EVENT. */
b7e80413
SM
10304 if (!NILP (help_echo)
10305 || !NILP (previous_help_echo))
06a2c219
GM
10306 {
10307 Lisp_Object frame;
be010514 10308 int n;
06a2c219
GM
10309
10310 if (f)
10311 XSETFRAME (frame, f);
10312 else
10313 frame = Qnil;
10314
10315 any_help_event_p = 1;
5ab2570d 10316 n = gen_help_event (bufp, numchars, help_echo, frame,
7cea38bc
GM
10317 help_echo_window, help_echo_object,
10318 help_echo_pos);
be010514 10319 bufp += n, count += n, numchars -= n;
06a2c219
GM
10320 }
10321
10322 goto OTHER;
dc6f92b8 10323 }
dc6f92b8 10324
7a13e894 10325 case ConfigureNotify:
9829ddba
RS
10326 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10327 if (f)
af395ec1 10328 {
5c187dee 10329#ifndef USE_X_TOOLKIT
bf1b7b30
KH
10330 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10331 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
5c187dee 10332
2d7fc7e8
RS
10333 /* In the toolkit version, change_frame_size
10334 is called by the code that handles resizing
10335 of the EmacsFrame widget. */
7a13e894 10336
7a13e894
RS
10337 /* Even if the number of character rows and columns has
10338 not changed, the font size may have changed, so we need
10339 to check the pixel dimensions as well. */
10340 if (columns != f->width
10341 || rows != f->height
7556890b
RS
10342 || event.xconfigure.width != f->output_data.x->pixel_width
10343 || event.xconfigure.height != f->output_data.x->pixel_height)
7a13e894 10344 {
7d1e984f 10345 change_frame_size (f, rows, columns, 0, 1, 0);
7a13e894 10346 SET_FRAME_GARBAGED (f);
e687d06e 10347 cancel_mouse_face (f);
7a13e894 10348 }
2d7fc7e8 10349#endif
af395ec1 10350
7556890b
RS
10351 f->output_data.x->pixel_width = event.xconfigure.width;
10352 f->output_data.x->pixel_height = event.xconfigure.height;
7a13e894
RS
10353
10354 /* What we have now is the position of Emacs's own window.
10355 Convert that to the position of the window manager window. */
dcb07ae9
RS
10356 x_real_positions (f, &f->output_data.x->left_pos,
10357 &f->output_data.x->top_pos);
10358
f5d11644
GM
10359#ifdef HAVE_X_I18N
10360 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10361 xic_set_statusarea (f);
10362#endif
10363
dcb07ae9
RS
10364 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10365 {
10366 /* Since the WM decorations come below top_pos now,
10367 we must put them below top_pos in the future. */
10368 f->output_data.x->win_gravity = NorthWestGravity;
10369 x_wm_set_size_hint (f, (long) 0, 0);
10370 }
8f08dc93
KH
10371#ifdef USE_MOTIF
10372 /* Some window managers pass (0,0) as the location of
10373 the window, and the Motif event handler stores it
10374 in the emacs widget, which messes up Motif menus. */
10375 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
10376 {
10377 event.xconfigure.x = f->output_data.x->widget->core.x;
10378 event.xconfigure.y = f->output_data.x->widget->core.y;
10379 }
06a2c219 10380#endif /* USE_MOTIF */
7a13e894 10381 }
2d7fc7e8 10382 goto OTHER;
dc6f92b8 10383
7a13e894
RS
10384 case ButtonPress:
10385 case ButtonRelease:
10386 {
10387 /* If we decide we want to generate an event to be seen
10388 by the rest of Emacs, we put it here. */
10389 struct input_event emacs_event;
9ea173e8 10390 int tool_bar_p = 0;
06a2c219 10391
7a13e894 10392 emacs_event.kind = no_event;
7a13e894 10393 bzero (&compose_status, sizeof (compose_status));
9b07615b 10394
06a2c219
GM
10395 if (dpyinfo->grabbed
10396 && last_mouse_frame
9f67f20b
RS
10397 && FRAME_LIVE_P (last_mouse_frame))
10398 f = last_mouse_frame;
10399 else
2224b905 10400 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9f67f20b 10401
06a2c219
GM
10402 if (f)
10403 {
9ea173e8
GM
10404 /* Is this in the tool-bar? */
10405 if (WINDOWP (f->tool_bar_window)
10406 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
06a2c219
GM
10407 {
10408 Lisp_Object window;
10409 int p, x, y;
10410
10411 x = event.xbutton.x;
10412 y = event.xbutton.y;
10413
10414 /* Set x and y. */
10415 window = window_from_coordinates (f, x, y, &p, 1);
9ea173e8 10416 if (EQ (window, f->tool_bar_window))
06a2c219 10417 {
9ea173e8
GM
10418 x_handle_tool_bar_click (f, &event.xbutton);
10419 tool_bar_p = 1;
06a2c219
GM
10420 }
10421 }
10422
9ea173e8 10423 if (!tool_bar_p)
06a2c219
GM
10424 if (!dpyinfo->x_focus_frame
10425 || f == dpyinfo->x_focus_frame)
10426 construct_mouse_click (&emacs_event, &event, f);
7a13e894
RS
10427 }
10428 else
10429 {
06a2c219 10430#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
10431 struct scroll_bar *bar
10432 = x_window_to_scroll_bar (event.xbutton.window);
f451eb13 10433
7a13e894
RS
10434 if (bar)
10435 x_scroll_bar_handle_click (bar, &event, &emacs_event);
06a2c219 10436#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
10437 }
10438
10439 if (event.type == ButtonPress)
10440 {
10441 dpyinfo->grabbed |= (1 << event.xbutton.button);
10442 last_mouse_frame = f;
edad46f6
KH
10443 /* Ignore any mouse motion that happened
10444 before this event; any subsequent mouse-movement
10445 Emacs events should reflect only motion after
10446 the ButtonPress. */
a00e91cd
KH
10447 if (f != 0)
10448 f->mouse_moved = 0;
06a2c219 10449
9ea173e8
GM
10450 if (!tool_bar_p)
10451 last_tool_bar_item = -1;
7a13e894 10452 }
3afe33e7
RS
10453 else
10454 {
7a13e894 10455 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
3afe33e7 10456 }
23faf38f 10457
7a13e894
RS
10458 if (numchars >= 1 && emacs_event.kind != no_event)
10459 {
10460 bcopy (&emacs_event, bufp, sizeof (struct input_event));
10461 bufp++;
10462 count++;
10463 numchars--;
10464 }
3afe33e7
RS
10465
10466#ifdef USE_X_TOOLKIT
2224b905
RS
10467 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
10468 /* For a down-event in the menu bar,
10469 don't pass it to Xt right now.
10470 Instead, save it away
10471 and we will pass it to Xt from kbd_buffer_get_event.
10472 That way, we can run some Lisp code first. */
91375f8f
RS
10473 if (f && event.type == ButtonPress
10474 /* Verify the event is really within the menu bar
10475 and not just sent to it due to grabbing. */
10476 && event.xbutton.x >= 0
10477 && event.xbutton.x < f->output_data.x->pixel_width
10478 && event.xbutton.y >= 0
10479 && event.xbutton.y < f->output_data.x->menubar_height
10480 && event.xbutton.same_screen)
2224b905 10481 {
8805890a 10482 SET_SAVED_BUTTON_EVENT;
2237cac9
RS
10483 XSETFRAME (last_mouse_press_frame, f);
10484 }
10485 else if (event.type == ButtonPress)
10486 {
10487 last_mouse_press_frame = Qnil;
30e671c3 10488 goto OTHER;
ce89ef46 10489 }
06a2c219 10490
2237cac9
RS
10491#ifdef USE_MOTIF /* This should do not harm for Lucid,
10492 but I am trying to be cautious. */
ce89ef46
RS
10493 else if (event.type == ButtonRelease)
10494 {
2237cac9 10495 if (!NILP (last_mouse_press_frame))
f10ded1c 10496 {
2237cac9
RS
10497 f = XFRAME (last_mouse_press_frame);
10498 if (f->output_data.x)
06a2c219 10499 SET_SAVED_BUTTON_EVENT;
f10ded1c 10500 }
06a2c219 10501 else
30e671c3 10502 goto OTHER;
2224b905 10503 }
2237cac9 10504#endif /* USE_MOTIF */
2224b905
RS
10505 else
10506 goto OTHER;
3afe33e7 10507#endif /* USE_X_TOOLKIT */
7a13e894
RS
10508 }
10509 break;
dc6f92b8 10510
7a13e894 10511 case CirculateNotify:
06a2c219
GM
10512 goto OTHER;
10513
7a13e894 10514 case CirculateRequest:
06a2c219
GM
10515 goto OTHER;
10516
10517 case VisibilityNotify:
10518 goto OTHER;
dc6f92b8 10519
7a13e894
RS
10520 case MappingNotify:
10521 /* Someone has changed the keyboard mapping - update the
10522 local cache. */
10523 switch (event.xmapping.request)
10524 {
10525 case MappingModifier:
10526 x_find_modifier_meanings (dpyinfo);
10527 /* This is meant to fall through. */
10528 case MappingKeyboard:
10529 XRefreshKeyboardMapping (&event.xmapping);
10530 }
7a13e894 10531 goto OTHER;
dc6f92b8 10532
7a13e894 10533 default:
7a13e894 10534 OTHER:
717ca130 10535#ifdef USE_X_TOOLKIT
7a13e894
RS
10536 BLOCK_INPUT;
10537 XtDispatchEvent (&event);
10538 UNBLOCK_INPUT;
3afe33e7 10539#endif /* USE_X_TOOLKIT */
7a13e894
RS
10540 break;
10541 }
dc6f92b8
JB
10542 }
10543 }
10544
06a2c219
GM
10545 out:;
10546
9a5196d0
RS
10547 /* On some systems, an X bug causes Emacs to get no more events
10548 when the window is destroyed. Detect that. (1994.) */
58769bee 10549 if (! event_found)
ef2a22d0 10550 {
ef2a22d0
RS
10551 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
10552 One XNOOP in 100 loops will make Emacs terminate.
10553 B. Bretthauer, 1994 */
10554 x_noop_count++;
58769bee 10555 if (x_noop_count >= 100)
ef2a22d0
RS
10556 {
10557 x_noop_count=0;
2224b905
RS
10558
10559 if (next_noop_dpyinfo == 0)
10560 next_noop_dpyinfo = x_display_list;
10561
10562 XNoOp (next_noop_dpyinfo->display);
10563
10564 /* Each time we get here, cycle through the displays now open. */
10565 next_noop_dpyinfo = next_noop_dpyinfo->next;
ef2a22d0
RS
10566 }
10567 }
502add23 10568
06a2c219 10569 /* If the focus was just given to an auto-raising frame,
0134a210 10570 raise it now. */
7a13e894 10571 /* ??? This ought to be able to handle more than one such frame. */
0134a210
RS
10572 if (pending_autoraise_frame)
10573 {
10574 x_raise_frame (pending_autoraise_frame);
10575 pending_autoraise_frame = 0;
10576 }
0134a210 10577
dc6f92b8 10578 UNBLOCK_INPUT;
bde5503b 10579 --handling_signal;
dc6f92b8
JB
10580 return count;
10581}
06a2c219
GM
10582
10583
10584
dc6f92b8 10585\f
06a2c219
GM
10586/***********************************************************************
10587 Text Cursor
10588 ***********************************************************************/
10589
10590/* Note if the text cursor of window W has been overwritten by a
10591 drawing operation that outputs N glyphs starting at HPOS in the
10592 line given by output_cursor.vpos. N < 0 means all the rest of the
10593 line after HPOS has been written. */
10594
10595static void
10596note_overwritten_text_cursor (w, hpos, n)
10597 struct window *w;
10598 int hpos, n;
10599{
10600 if (updated_area == TEXT_AREA
10601 && output_cursor.vpos == w->phys_cursor.vpos
10602 && hpos <= w->phys_cursor.hpos
10603 && (n < 0
10604 || hpos + n > w->phys_cursor.hpos))
10605 w->phys_cursor_on_p = 0;
10606}
f451eb13
JB
10607
10608
06a2c219
GM
10609/* Set clipping for output in glyph row ROW. W is the window in which
10610 we operate. GC is the graphics context to set clipping in.
10611 WHOLE_LINE_P non-zero means include the areas used for truncation
10612 mark display and alike in the clipping rectangle.
10613
10614 ROW may be a text row or, e.g., a mode line. Text rows must be
10615 clipped to the interior of the window dedicated to text display,
10616 mode lines must be clipped to the whole window. */
dc6f92b8
JB
10617
10618static void
06a2c219
GM
10619x_clip_to_row (w, row, gc, whole_line_p)
10620 struct window *w;
10621 struct glyph_row *row;
10622 GC gc;
10623 int whole_line_p;
dc6f92b8 10624{
06a2c219
GM
10625 struct frame *f = XFRAME (WINDOW_FRAME (w));
10626 XRectangle clip_rect;
10627 int window_x, window_y, window_width, window_height;
dc6f92b8 10628
06a2c219 10629 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5c1aae96 10630
06a2c219
GM
10631 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10632 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10633 clip_rect.y = max (clip_rect.y, window_y);
10634 clip_rect.width = window_width;
10635 clip_rect.height = row->visible_height;
5c1aae96 10636
06a2c219
GM
10637 /* If clipping to the whole line, including trunc marks, extend
10638 the rectangle to the left and increase its width. */
10639 if (whole_line_p)
10640 {
110859fc
GM
10641 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10642 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
06a2c219 10643 }
5c1aae96 10644
06a2c219 10645 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
dc6f92b8
JB
10646}
10647
06a2c219
GM
10648
10649/* Draw a hollow box cursor on window W in glyph row ROW. */
dc6f92b8
JB
10650
10651static void
06a2c219
GM
10652x_draw_hollow_cursor (w, row)
10653 struct window *w;
10654 struct glyph_row *row;
dc6f92b8 10655{
06a2c219
GM
10656 struct frame *f = XFRAME (WINDOW_FRAME (w));
10657 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10658 Display *dpy = FRAME_X_DISPLAY (f);
10659 int x, y, wd, h;
10660 XGCValues xgcv;
10661 struct glyph *cursor_glyph;
10662 GC gc;
10663
10664 /* Compute frame-relative coordinates from window-relative
10665 coordinates. */
10666 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10667 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10668 + row->ascent - w->phys_cursor_ascent);
10669 h = row->height - 1;
10670
10671 /* Get the glyph the cursor is on. If we can't tell because
10672 the current matrix is invalid or such, give up. */
10673 cursor_glyph = get_phys_cursor_glyph (w);
10674 if (cursor_glyph == NULL)
dc6f92b8
JB
10675 return;
10676
06a2c219
GM
10677 /* Compute the width of the rectangle to draw. If on a stretch
10678 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10679 rectangle as wide as the glyph, but use a canonical character
10680 width instead. */
10681 wd = cursor_glyph->pixel_width - 1;
10682 if (cursor_glyph->type == STRETCH_GLYPH
10683 && !x_stretch_cursor_p)
10684 wd = min (CANON_X_UNIT (f), wd);
10685
10686 /* The foreground of cursor_gc is typically the same as the normal
10687 background color, which can cause the cursor box to be invisible. */
10688 xgcv.foreground = f->output_data.x->cursor_pixel;
10689 if (dpyinfo->scratch_cursor_gc)
10690 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10691 else
10692 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10693 GCForeground, &xgcv);
10694 gc = dpyinfo->scratch_cursor_gc;
10695
10696 /* Set clipping, draw the rectangle, and reset clipping again. */
10697 x_clip_to_row (w, row, gc, 0);
10698 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10699 XSetClipMask (dpy, gc, None);
dc6f92b8
JB
10700}
10701
06a2c219
GM
10702
10703/* Draw a bar cursor on window W in glyph row ROW.
10704
10705 Implementation note: One would like to draw a bar cursor with an
10706 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10707 Unfortunately, I didn't find a font yet that has this property set.
10708 --gerd. */
dc6f92b8
JB
10709
10710static void
f02d8aa0 10711x_draw_bar_cursor (w, row, width)
06a2c219
GM
10712 struct window *w;
10713 struct glyph_row *row;
f02d8aa0 10714 int width;
dc6f92b8 10715{
92f424df
GM
10716 struct frame *f = XFRAME (w->frame);
10717 struct glyph *cursor_glyph;
10718 GC gc;
10719 int x;
10720 unsigned long mask;
10721 XGCValues xgcv;
10722 Display *dpy;
10723 Window window;
06a2c219 10724
92f424df
GM
10725 /* If cursor is out of bounds, don't draw garbage. This can happen
10726 in mini-buffer windows when switching between echo area glyphs
10727 and mini-buffer. */
10728 cursor_glyph = get_phys_cursor_glyph (w);
10729 if (cursor_glyph == NULL)
10730 return;
06a2c219 10731
92f424df
GM
10732 /* If on an image, draw like a normal cursor. That's usually better
10733 visible than drawing a bar, esp. if the image is large so that
10734 the bar might not be in the window. */
10735 if (cursor_glyph->type == IMAGE_GLYPH)
10736 {
10737 struct glyph_row *row;
10738 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
10739 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
10740 }
10741 else
10742 {
06a2c219
GM
10743 xgcv.background = f->output_data.x->cursor_pixel;
10744 xgcv.foreground = f->output_data.x->cursor_pixel;
10745 xgcv.graphics_exposures = 0;
10746 mask = GCForeground | GCBackground | GCGraphicsExposures;
10747 dpy = FRAME_X_DISPLAY (f);
10748 window = FRAME_X_WINDOW (f);
10749 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
92f424df 10750
06a2c219
GM
10751 if (gc)
10752 XChangeGC (dpy, gc, mask, &xgcv);
10753 else
10754 {
10755 gc = XCreateGC (dpy, window, mask, &xgcv);
10756 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10757 }
92f424df 10758
f02d8aa0
GM
10759 if (width < 0)
10760 width = f->output_data.x->cursor_width;
92f424df 10761
06a2c219
GM
10762 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10763 x_clip_to_row (w, row, gc, 0);
10764 XFillRectangle (dpy, window, gc,
10765 x,
10766 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
f02d8aa0 10767 min (cursor_glyph->pixel_width, width),
06a2c219
GM
10768 row->height);
10769 XSetClipMask (dpy, gc, None);
10770 }
dc6f92b8
JB
10771}
10772
06a2c219
GM
10773
10774/* Clear the cursor of window W to background color, and mark the
10775 cursor as not shown. This is used when the text where the cursor
10776 is is about to be rewritten. */
10777
dc6f92b8 10778static void
06a2c219
GM
10779x_clear_cursor (w)
10780 struct window *w;
dc6f92b8 10781{
06a2c219
GM
10782 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10783 x_update_window_cursor (w, 0);
10784}
90e65f07 10785
dbc4e1c1 10786
06a2c219
GM
10787/* Draw the cursor glyph of window W in glyph row ROW. See the
10788 comment of x_draw_glyphs for the meaning of HL. */
dbc4e1c1 10789
06a2c219
GM
10790static void
10791x_draw_phys_cursor_glyph (w, row, hl)
10792 struct window *w;
10793 struct glyph_row *row;
10794 enum draw_glyphs_face hl;
10795{
10796 /* If cursor hpos is out of bounds, don't draw garbage. This can
10797 happen in mini-buffer windows when switching between echo area
10798 glyphs and mini-buffer. */
10799 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
66ac4b0e
GM
10800 {
10801 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10802 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10803 hl, 0, 0, 0);
10804
10805 /* When we erase the cursor, and ROW is overlapped by other
10806 rows, make sure that these overlapping parts of other rows
10807 are redrawn. */
10808 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10809 {
10810 if (row > w->current_matrix->rows
10811 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10812 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10813
10814 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10815 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10816 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10817 }
10818 }
06a2c219 10819}
dbc4e1c1 10820
eea6af04 10821
06a2c219 10822/* Erase the image of a cursor of window W from the screen. */
eea6af04 10823
06a2c219
GM
10824static void
10825x_erase_phys_cursor (w)
10826 struct window *w;
10827{
10828 struct frame *f = XFRAME (w->frame);
10829 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10830 int hpos = w->phys_cursor.hpos;
10831 int vpos = w->phys_cursor.vpos;
10832 int mouse_face_here_p = 0;
10833 struct glyph_matrix *active_glyphs = w->current_matrix;
10834 struct glyph_row *cursor_row;
10835 struct glyph *cursor_glyph;
10836 enum draw_glyphs_face hl;
10837
10838 /* No cursor displayed or row invalidated => nothing to do on the
10839 screen. */
10840 if (w->phys_cursor_type == NO_CURSOR)
10841 goto mark_cursor_off;
10842
10843 /* VPOS >= active_glyphs->nrows means that window has been resized.
10844 Don't bother to erase the cursor. */
10845 if (vpos >= active_glyphs->nrows)
10846 goto mark_cursor_off;
10847
10848 /* If row containing cursor is marked invalid, there is nothing we
10849 can do. */
10850 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10851 if (!cursor_row->enabled_p)
10852 goto mark_cursor_off;
10853
10854 /* This can happen when the new row is shorter than the old one.
10855 In this case, either x_draw_glyphs or clear_end_of_line
10856 should have cleared the cursor. Note that we wouldn't be
10857 able to erase the cursor in this case because we don't have a
10858 cursor glyph at hand. */
10859 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10860 goto mark_cursor_off;
10861
10862 /* If the cursor is in the mouse face area, redisplay that when
10863 we clear the cursor. */
8801a864
KR
10864 if (! NILP (dpyinfo->mouse_face_window)
10865 && w == XWINDOW (dpyinfo->mouse_face_window)
06a2c219
GM
10866 && (vpos > dpyinfo->mouse_face_beg_row
10867 || (vpos == dpyinfo->mouse_face_beg_row
10868 && hpos >= dpyinfo->mouse_face_beg_col))
10869 && (vpos < dpyinfo->mouse_face_end_row
10870 || (vpos == dpyinfo->mouse_face_end_row
10871 && hpos < dpyinfo->mouse_face_end_col))
10872 /* Don't redraw the cursor's spot in mouse face if it is at the
10873 end of a line (on a newline). The cursor appears there, but
10874 mouse highlighting does not. */
10875 && cursor_row->used[TEXT_AREA] > hpos)
10876 mouse_face_here_p = 1;
10877
10878 /* Maybe clear the display under the cursor. */
10879 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10880 {
10881 int x;
045dee35 10882 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dbc4e1c1 10883
06a2c219
GM
10884 cursor_glyph = get_phys_cursor_glyph (w);
10885 if (cursor_glyph == NULL)
10886 goto mark_cursor_off;
dbc4e1c1 10887
06a2c219
GM
10888 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10889
c5e6e06b
GM
10890 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10891 x,
10892 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
10893 cursor_row->y)),
10894 cursor_glyph->pixel_width,
10895 cursor_row->visible_height,
10896 False);
dbc4e1c1 10897 }
06a2c219
GM
10898
10899 /* Erase the cursor by redrawing the character underneath it. */
10900 if (mouse_face_here_p)
10901 hl = DRAW_MOUSE_FACE;
10902 else if (cursor_row->inverse_p)
10903 hl = DRAW_INVERSE_VIDEO;
10904 else
10905 hl = DRAW_NORMAL_TEXT;
10906 x_draw_phys_cursor_glyph (w, cursor_row, hl);
dbc4e1c1 10907
06a2c219
GM
10908 mark_cursor_off:
10909 w->phys_cursor_on_p = 0;
10910 w->phys_cursor_type = NO_CURSOR;
dbc4e1c1
JB
10911}
10912
10913
06a2c219
GM
10914/* Display or clear cursor of window W. If ON is zero, clear the
10915 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10916 where to put the cursor is specified by HPOS, VPOS, X and Y. */
dbc4e1c1 10917
06a2c219
GM
10918void
10919x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10920 struct window *w;
10921 int on, hpos, vpos, x, y;
dbc4e1c1 10922{
06a2c219
GM
10923 struct frame *f = XFRAME (w->frame);
10924 int new_cursor_type;
f02d8aa0 10925 int new_cursor_width;
06a2c219
GM
10926 struct glyph_matrix *current_glyphs;
10927 struct glyph_row *glyph_row;
10928 struct glyph *glyph;
dbc4e1c1 10929
49d838ea 10930 /* This is pointless on invisible frames, and dangerous on garbaged
06a2c219
GM
10931 windows and frames; in the latter case, the frame or window may
10932 be in the midst of changing its size, and x and y may be off the
10933 window. */
10934 if (! FRAME_VISIBLE_P (f)
10935 || FRAME_GARBAGED_P (f)
10936 || vpos >= w->current_matrix->nrows
10937 || hpos >= w->current_matrix->matrix_w)
dc6f92b8
JB
10938 return;
10939
10940 /* If cursor is off and we want it off, return quickly. */
06a2c219 10941 if (!on && !w->phys_cursor_on_p)
dc6f92b8
JB
10942 return;
10943
06a2c219
GM
10944 current_glyphs = w->current_matrix;
10945 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10946 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10947
10948 /* If cursor row is not enabled, we don't really know where to
10949 display the cursor. */
10950 if (!glyph_row->enabled_p)
10951 {
10952 w->phys_cursor_on_p = 0;
10953 return;
10954 }
10955
10956 xassert (interrupt_input_blocked);
10957
10958 /* Set new_cursor_type to the cursor we want to be displayed. In a
10959 mini-buffer window, we want the cursor only to appear if we are
10960 reading input from this window. For the selected window, we want
10961 the cursor type given by the frame parameter. If explicitly
10962 marked off, draw no cursor. In all other cases, we want a hollow
10963 box cursor. */
f02d8aa0 10964 new_cursor_width = -1;
9b4a7047
GM
10965 if (cursor_in_echo_area
10966 && FRAME_HAS_MINIBUF_P (f)
10967 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
06a2c219 10968 {
9b4a7047
GM
10969 if (w == XWINDOW (echo_area_window))
10970 new_cursor_type = FRAME_DESIRED_CURSOR (f);
06a2c219
GM
10971 else
10972 new_cursor_type = HOLLOW_BOX_CURSOR;
10973 }
06a2c219 10974 else
9b4a7047 10975 {
7a58ab59
GM
10976 if (f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
10977 || w != XWINDOW (f->selected_window))
9b4a7047 10978 {
e55a0b79
GM
10979 extern int cursor_in_non_selected_windows;
10980
5cefa566
GM
10981 if (MINI_WINDOW_P (w)
10982 || !cursor_in_non_selected_windows
10983 || NILP (XBUFFER (w->buffer)->cursor_type))
9b4a7047
GM
10984 new_cursor_type = NO_CURSOR;
10985 else
10986 new_cursor_type = HOLLOW_BOX_CURSOR;
10987 }
10988 else if (w->cursor_off_p)
10989 new_cursor_type = NO_CURSOR;
10990 else
f02d8aa0
GM
10991 {
10992 struct buffer *b = XBUFFER (w->buffer);
10993
10994 if (EQ (b->cursor_type, Qt))
10995 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10996 else
10997 new_cursor_type = x_specified_cursor_type (b->cursor_type,
10998 &new_cursor_width);
10999 }
9b4a7047 11000 }
06a2c219
GM
11001
11002 /* If cursor is currently being shown and we don't want it to be or
11003 it is in the wrong place, or the cursor type is not what we want,
dc6f92b8 11004 erase it. */
06a2c219 11005 if (w->phys_cursor_on_p
dc6f92b8 11006 && (!on
06a2c219
GM
11007 || w->phys_cursor.x != x
11008 || w->phys_cursor.y != y
11009 || new_cursor_type != w->phys_cursor_type))
11010 x_erase_phys_cursor (w);
11011
11012 /* If the cursor is now invisible and we want it to be visible,
11013 display it. */
11014 if (on && !w->phys_cursor_on_p)
11015 {
11016 w->phys_cursor_ascent = glyph_row->ascent;
11017 w->phys_cursor_height = glyph_row->height;
11018
11019 /* Set phys_cursor_.* before x_draw_.* is called because some
11020 of them may need the information. */
11021 w->phys_cursor.x = x;
11022 w->phys_cursor.y = glyph_row->y;
11023 w->phys_cursor.hpos = hpos;
11024 w->phys_cursor.vpos = vpos;
11025 w->phys_cursor_type = new_cursor_type;
11026 w->phys_cursor_on_p = 1;
11027
11028 switch (new_cursor_type)
dc6f92b8 11029 {
06a2c219
GM
11030 case HOLLOW_BOX_CURSOR:
11031 x_draw_hollow_cursor (w, glyph_row);
11032 break;
11033
11034 case FILLED_BOX_CURSOR:
11035 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
11036 break;
11037
11038 case BAR_CURSOR:
f02d8aa0 11039 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
06a2c219
GM
11040 break;
11041
11042 case NO_CURSOR:
11043 break;
dc6f92b8 11044
06a2c219
GM
11045 default:
11046 abort ();
11047 }
59ddecde
GM
11048
11049#ifdef HAVE_X_I18N
11050 if (w == XWINDOW (f->selected_window))
11051 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
11052 xic_set_preeditarea (w, x, y);
11053#endif
dc6f92b8
JB
11054 }
11055
06a2c219 11056#ifndef XFlush
f676886a 11057 if (updating_frame != f)
334208b7 11058 XFlush (FRAME_X_DISPLAY (f));
06a2c219 11059#endif
dc6f92b8
JB
11060}
11061
06a2c219
GM
11062
11063/* Display the cursor on window W, or clear it. X and Y are window
11064 relative pixel coordinates. HPOS and VPOS are glyph matrix
11065 positions. If W is not the selected window, display a hollow
11066 cursor. ON non-zero means display the cursor at X, Y which
11067 correspond to HPOS, VPOS, otherwise it is cleared. */
5d46f928 11068
dfcf069d 11069void
06a2c219
GM
11070x_display_cursor (w, on, hpos, vpos, x, y)
11071 struct window *w;
11072 int on, hpos, vpos, x, y;
dc6f92b8 11073{
f94397b5 11074 BLOCK_INPUT;
06a2c219 11075 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
5d46f928
RS
11076 UNBLOCK_INPUT;
11077}
11078
06a2c219
GM
11079
11080/* Display the cursor on window W, or clear it, according to ON_P.
5d46f928
RS
11081 Don't change the cursor's position. */
11082
dfcf069d 11083void
06a2c219 11084x_update_cursor (f, on_p)
5d46f928 11085 struct frame *f;
5d46f928 11086{
06a2c219
GM
11087 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
11088}
11089
11090
11091/* Call x_update_window_cursor with parameter ON_P on all leaf windows
11092 in the window tree rooted at W. */
11093
11094static void
11095x_update_cursor_in_window_tree (w, on_p)
11096 struct window *w;
11097 int on_p;
11098{
11099 while (w)
11100 {
11101 if (!NILP (w->hchild))
11102 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
11103 else if (!NILP (w->vchild))
11104 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
11105 else
11106 x_update_window_cursor (w, on_p);
11107
11108 w = NILP (w->next) ? 0 : XWINDOW (w->next);
11109 }
11110}
5d46f928 11111
f94397b5 11112
06a2c219
GM
11113/* Switch the display of W's cursor on or off, according to the value
11114 of ON. */
11115
11116static void
11117x_update_window_cursor (w, on)
11118 struct window *w;
11119 int on;
11120{
16b5d424
GM
11121 /* Don't update cursor in windows whose frame is in the process
11122 of being deleted. */
11123 if (w->current_matrix)
11124 {
11125 BLOCK_INPUT;
11126 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11127 w->phys_cursor.x, w->phys_cursor.y);
11128 UNBLOCK_INPUT;
11129 }
dc6f92b8 11130}
06a2c219
GM
11131
11132
11133
dc6f92b8
JB
11134\f
11135/* Icons. */
11136
f676886a 11137/* Refresh bitmap kitchen sink icon for frame F
06a2c219 11138 when we get an expose event for it. */
dc6f92b8 11139
dfcf069d 11140void
f676886a
JB
11141refreshicon (f)
11142 struct frame *f;
dc6f92b8 11143{
06a2c219 11144 /* Normally, the window manager handles this function. */
dc6f92b8
JB
11145}
11146
dbc4e1c1 11147/* Make the x-window of frame F use the gnu icon bitmap. */
dc6f92b8
JB
11148
11149int
990ba854 11150x_bitmap_icon (f, file)
f676886a 11151 struct frame *f;
990ba854 11152 Lisp_Object file;
dc6f92b8 11153{
06a2c219 11154 int bitmap_id;
dc6f92b8 11155
c118dd06 11156 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
11157 return 1;
11158
990ba854 11159 /* Free up our existing icon bitmap if any. */
7556890b
RS
11160 if (f->output_data.x->icon_bitmap > 0)
11161 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11162 f->output_data.x->icon_bitmap = 0;
990ba854
RS
11163
11164 if (STRINGP (file))
7f2ae036
RS
11165 bitmap_id = x_create_bitmap_from_file (f, file);
11166 else
11167 {
990ba854 11168 /* Create the GNU bitmap if necessary. */
5bf01b68 11169 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
334208b7
RS
11170 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11171 = x_create_bitmap_from_data (f, gnu_bits,
11172 gnu_width, gnu_height);
990ba854
RS
11173
11174 /* The first time we create the GNU bitmap,
06a2c219 11175 this increments the ref-count one extra time.
990ba854
RS
11176 As a result, the GNU bitmap is never freed.
11177 That way, we don't have to worry about allocating it again. */
334208b7 11178 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
990ba854 11179
334208b7 11180 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7f2ae036
RS
11181 }
11182
11183 x_wm_set_icon_pixmap (f, bitmap_id);
7556890b 11184 f->output_data.x->icon_bitmap = bitmap_id;
dc6f92b8
JB
11185
11186 return 0;
11187}
11188
11189
1be2d067
KH
11190/* Make the x-window of frame F use a rectangle with text.
11191 Use ICON_NAME as the text. */
dc6f92b8
JB
11192
11193int
f676886a
JB
11194x_text_icon (f, icon_name)
11195 struct frame *f;
dc6f92b8
JB
11196 char *icon_name;
11197{
c118dd06 11198 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
11199 return 1;
11200
1be2d067
KH
11201#ifdef HAVE_X11R4
11202 {
11203 XTextProperty text;
11204 text.value = (unsigned char *) icon_name;
11205 text.encoding = XA_STRING;
11206 text.format = 8;
11207 text.nitems = strlen (icon_name);
11208#ifdef USE_X_TOOLKIT
7556890b 11209 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
1be2d067
KH
11210 &text);
11211#else /* not USE_X_TOOLKIT */
11212 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11213#endif /* not USE_X_TOOLKIT */
11214 }
11215#else /* not HAVE_X11R4 */
11216 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11217#endif /* not HAVE_X11R4 */
58769bee 11218
7556890b
RS
11219 if (f->output_data.x->icon_bitmap > 0)
11220 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11221 f->output_data.x->icon_bitmap = 0;
b1c884c3 11222 x_wm_set_icon_pixmap (f, 0);
dc6f92b8
JB
11223
11224 return 0;
11225}
11226\f
e99db5a1
RS
11227#define X_ERROR_MESSAGE_SIZE 200
11228
11229/* If non-nil, this should be a string.
11230 It means catch X errors and store the error message in this string. */
11231
11232static Lisp_Object x_error_message_string;
11233
11234/* An X error handler which stores the error message in
11235 x_error_message_string. This is called from x_error_handler if
11236 x_catch_errors is in effect. */
11237
06a2c219 11238static void
e99db5a1
RS
11239x_error_catcher (display, error)
11240 Display *display;
11241 XErrorEvent *error;
11242{
11243 XGetErrorText (display, error->error_code,
11244 XSTRING (x_error_message_string)->data,
11245 X_ERROR_MESSAGE_SIZE);
11246}
11247
11248/* Begin trapping X errors for display DPY. Actually we trap X errors
11249 for all displays, but DPY should be the display you are actually
11250 operating on.
11251
11252 After calling this function, X protocol errors no longer cause
11253 Emacs to exit; instead, they are recorded in the string
11254 stored in x_error_message_string.
11255
11256 Calling x_check_errors signals an Emacs error if an X error has
11257 occurred since the last call to x_catch_errors or x_check_errors.
11258
11259 Calling x_uncatch_errors resumes the normal error handling. */
11260
11261void x_check_errors ();
11262static Lisp_Object x_catch_errors_unwind ();
11263
11264int
11265x_catch_errors (dpy)
11266 Display *dpy;
11267{
11268 int count = specpdl_ptr - specpdl;
11269
11270 /* Make sure any errors from previous requests have been dealt with. */
11271 XSync (dpy, False);
11272
11273 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
11274
11275 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
11276 XSTRING (x_error_message_string)->data[0] = 0;
11277
11278 return count;
11279}
11280
11281/* Unbind the binding that we made to check for X errors. */
11282
11283static Lisp_Object
11284x_catch_errors_unwind (old_val)
11285 Lisp_Object old_val;
11286{
11287 x_error_message_string = old_val;
11288 return Qnil;
11289}
11290
11291/* If any X protocol errors have arrived since the last call to
11292 x_catch_errors or x_check_errors, signal an Emacs error using
11293 sprintf (a buffer, FORMAT, the x error message text) as the text. */
11294
11295void
11296x_check_errors (dpy, format)
11297 Display *dpy;
11298 char *format;
11299{
11300 /* Make sure to catch any errors incurred so far. */
11301 XSync (dpy, False);
11302
11303 if (XSTRING (x_error_message_string)->data[0])
11304 error (format, XSTRING (x_error_message_string)->data);
11305}
11306
9829ddba
RS
11307/* Nonzero if we had any X protocol errors
11308 since we did x_catch_errors on DPY. */
e99db5a1
RS
11309
11310int
11311x_had_errors_p (dpy)
11312 Display *dpy;
11313{
11314 /* Make sure to catch any errors incurred so far. */
11315 XSync (dpy, False);
11316
11317 return XSTRING (x_error_message_string)->data[0] != 0;
11318}
11319
9829ddba
RS
11320/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
11321
06a2c219 11322void
9829ddba
RS
11323x_clear_errors (dpy)
11324 Display *dpy;
11325{
11326 XSTRING (x_error_message_string)->data[0] = 0;
11327}
11328
e99db5a1
RS
11329/* Stop catching X protocol errors and let them make Emacs die.
11330 DPY should be the display that was passed to x_catch_errors.
11331 COUNT should be the value that was returned by
11332 the corresponding call to x_catch_errors. */
11333
11334void
11335x_uncatch_errors (dpy, count)
11336 Display *dpy;
11337 int count;
11338{
11339 unbind_to (count, Qnil);
11340}
11341
11342#if 0
11343static unsigned int x_wire_count;
11344x_trace_wire ()
11345{
11346 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
11347}
11348#endif /* ! 0 */
11349
11350\f
11351/* Handle SIGPIPE, which can happen when the connection to a server
11352 simply goes away. SIGPIPE is handled by x_connection_signal.
11353 Don't need to do anything, because the write which caused the
11354 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
06a2c219 11355 which will do the appropriate cleanup for us. */
e99db5a1
RS
11356
11357static SIGTYPE
11358x_connection_signal (signalnum) /* If we don't have an argument, */
06a2c219 11359 int signalnum; /* some compilers complain in signal calls. */
e99db5a1
RS
11360{
11361#ifdef USG
11362 /* USG systems forget handlers when they are used;
11363 must reestablish each time */
11364 signal (signalnum, x_connection_signal);
11365#endif /* USG */
11366}
0da1ab50 11367
e99db5a1 11368\f
0da1ab50
GM
11369/************************************************************************
11370 Handling X errors
11371 ************************************************************************/
4746118a 11372
0da1ab50
GM
11373/* Handle the loss of connection to display DPY. ERROR_MESSAGE is
11374 the text of an error message that lead to the connection loss. */
16bd92ea 11375
4746118a 11376static SIGTYPE
5978125e
GM
11377x_connection_closed (dpy, error_message)
11378 Display *dpy;
7a13e894 11379 char *error_message;
4746118a 11380{
5978125e 11381 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7a13e894 11382 Lisp_Object frame, tail;
0da1ab50
GM
11383 int count;
11384 char *msg;
11385
11386 msg = (char *) alloca (strlen (error_message) + 1);
11387 strcpy (msg, error_message);
1a532e54
GM
11388 handling_signal = 0;
11389
0da1ab50
GM
11390 /* Prevent being called recursively because of an error condition
11391 below. Otherwise, we might end up with printing ``can't find per
11392 display information'' in the recursive call instead of printing
11393 the original message here. */
11394 count = x_catch_errors (dpy);
11395
8a4f36cc
GM
11396 /* We have to close the display to inform Xt that it doesn't
11397 exist anymore. If we don't, Xt will continue to wait for
11398 events from the display. As a consequence, a sequence of
11399
11400 M-x make-frame-on-display RET :1 RET
11401 ...kill the new frame, so that we get an IO error...
11402 M-x make-frame-on-display RET :1 RET
11403
11404 will indefinitely wait in Xt for events for display `:1', opened
11405 in the first class to make-frame-on-display.
6186a4a0 11406
8a4f36cc
GM
11407 Closing the display is reported to lead to a bus error on
11408 OpenWindows in certain situations. I suspect that is a bug
11409 in OpenWindows. I don't know how to cicumvent it here. */
11410
f613a4c8 11411#ifdef USE_X_TOOLKIT
ae24cb3b
GM
11412 /* If DPYINFO is null, this means we didn't open the display
11413 in the first place, so don't try to close it. */
11414 if (dpyinfo)
11415 XtCloseDisplay (dpy);
f613a4c8 11416#endif
adabc3a9 11417
8a4f36cc 11418 /* Indicate that this display is dead. */
9e80b57d
KR
11419 if (dpyinfo)
11420 dpyinfo->display = 0;
6186a4a0 11421
06a2c219 11422 /* First delete frames whose mini-buffers are on frames
7a13e894
RS
11423 that are on the dead display. */
11424 FOR_EACH_FRAME (tail, frame)
11425 {
11426 Lisp_Object minibuf_frame;
11427 minibuf_frame
11428 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
f48f33ca
RS
11429 if (FRAME_X_P (XFRAME (frame))
11430 && FRAME_X_P (XFRAME (minibuf_frame))
11431 && ! EQ (frame, minibuf_frame)
11432 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7a13e894
RS
11433 Fdelete_frame (frame, Qt);
11434 }
11435
11436 /* Now delete all remaining frames on the dead display.
06a2c219 11437 We are now sure none of these is used as the mini-buffer
7a13e894
RS
11438 for another frame that we need to delete. */
11439 FOR_EACH_FRAME (tail, frame)
f48f33ca
RS
11440 if (FRAME_X_P (XFRAME (frame))
11441 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
07a7096a
KH
11442 {
11443 /* Set this to t so that Fdelete_frame won't get confused
11444 trying to find a replacement. */
11445 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
11446 Fdelete_frame (frame, Qt);
11447 }
7a13e894 11448
482a1bd2
KH
11449 if (dpyinfo)
11450 x_delete_display (dpyinfo);
7a13e894 11451
0da1ab50
GM
11452 x_uncatch_errors (dpy, count);
11453
7a13e894
RS
11454 if (x_display_list == 0)
11455 {
0da1ab50 11456 fprintf (stderr, "%s\n", msg);
7a13e894
RS
11457 shut_down_emacs (0, 0, Qnil);
11458 exit (70);
11459 }
12ba150f 11460
7a13e894
RS
11461 /* Ordinary stack unwind doesn't deal with these. */
11462#ifdef SIGIO
11463 sigunblock (sigmask (SIGIO));
11464#endif
11465 sigunblock (sigmask (SIGALRM));
11466 TOTALLY_UNBLOCK_INPUT;
11467
aa4d9a9e 11468 clear_waiting_for_input ();
0da1ab50 11469 error ("%s", msg);
4746118a
JB
11470}
11471
0da1ab50 11472
7a13e894
RS
11473/* This is the usual handler for X protocol errors.
11474 It kills all frames on the display that we got the error for.
11475 If that was the only one, it prints an error message and kills Emacs. */
11476
06a2c219 11477static void
c118dd06
JB
11478x_error_quitter (display, error)
11479 Display *display;
11480 XErrorEvent *error;
11481{
7a13e894 11482 char buf[256], buf1[356];
dc6f92b8 11483
58769bee 11484 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 11485 original error handler. */
dc6f92b8 11486
c118dd06 11487 XGetErrorText (display, error->error_code, buf, sizeof (buf));
0a0fdc70 11488 sprintf (buf1, "X protocol error: %s on protocol request %d",
c118dd06 11489 buf, error->request_code);
7a13e894 11490 x_connection_closed (display, buf1);
dc6f92b8
JB
11491}
11492
0da1ab50 11493
e99db5a1
RS
11494/* This is the first-level handler for X protocol errors.
11495 It calls x_error_quitter or x_error_catcher. */
7a13e894 11496
8922af5f 11497static int
e99db5a1 11498x_error_handler (display, error)
8922af5f 11499 Display *display;
e99db5a1 11500 XErrorEvent *error;
8922af5f 11501{
e99db5a1
RS
11502 if (! NILP (x_error_message_string))
11503 x_error_catcher (display, error);
11504 else
11505 x_error_quitter (display, error);
06a2c219 11506 return 0;
f9e24cb9 11507}
c118dd06 11508
e99db5a1
RS
11509/* This is the handler for X IO errors, always.
11510 It kills all frames on the display that we lost touch with.
11511 If that was the only one, it prints an error message and kills Emacs. */
7a13e894 11512
c118dd06 11513static int
e99db5a1 11514x_io_error_quitter (display)
c118dd06 11515 Display *display;
c118dd06 11516{
e99db5a1 11517 char buf[256];
dc6f92b8 11518
e99db5a1
RS
11519 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
11520 x_connection_closed (display, buf);
06a2c219 11521 return 0;
dc6f92b8 11522}
dc6f92b8 11523\f
f451eb13
JB
11524/* Changing the font of the frame. */
11525
76bcdf39
RS
11526/* Give frame F the font named FONTNAME as its default font, and
11527 return the full name of that font. FONTNAME may be a wildcard
11528 pattern; in that case, we choose some font that fits the pattern.
11529 The return value shows which font we chose. */
11530
b5cf7a0e 11531Lisp_Object
f676886a
JB
11532x_new_font (f, fontname)
11533 struct frame *f;
dc6f92b8
JB
11534 register char *fontname;
11535{
dc43ef94 11536 struct font_info *fontp
ee569018 11537 = FS_LOAD_FONT (f, 0, fontname, -1);
dc6f92b8 11538
dc43ef94
KH
11539 if (!fontp)
11540 return Qnil;
2224a5fc 11541
dc43ef94 11542 f->output_data.x->font = (XFontStruct *) (fontp->font);
b4192550 11543 f->output_data.x->baseline_offset = fontp->baseline_offset;
dc43ef94
KH
11544 f->output_data.x->fontset = -1;
11545
b2cad826
KH
11546 /* Compute the scroll bar width in character columns. */
11547 if (f->scroll_bar_pixel_width > 0)
11548 {
7556890b 11549 int wid = FONT_WIDTH (f->output_data.x->font);
b2cad826
KH
11550 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
11551 }
11552 else
4e61bddf
RS
11553 {
11554 int wid = FONT_WIDTH (f->output_data.x->font);
11555 f->scroll_bar_cols = (14 + wid - 1) / wid;
11556 }
b2cad826 11557
f676886a 11558 /* Now make the frame display the given font. */
c118dd06 11559 if (FRAME_X_WINDOW (f) != 0)
dc6f92b8 11560 {
7556890b
RS
11561 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
11562 f->output_data.x->font->fid);
11563 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
11564 f->output_data.x->font->fid);
11565 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
11566 f->output_data.x->font->fid);
f676886a 11567
a27f9f86 11568 frame_update_line_height (f);
3497f73e
GM
11569
11570 /* Don't change the size of a tip frame; there's no point in
11571 doing it because it's done in Fx_show_tip, and it leads to
11572 problems because the tip frame has no widget. */
11573 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
11574 x_set_window_size (f, 0, f->width, f->height);
dc6f92b8 11575 }
a27f9f86
RS
11576 else
11577 /* If we are setting a new frame's font for the first time,
11578 there are no faces yet, so this font's height is the line height. */
7556890b 11579 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
dc6f92b8 11580
dc43ef94
KH
11581 return build_string (fontp->full_name);
11582}
11583
11584/* Give frame F the fontset named FONTSETNAME as its default font, and
11585 return the full name of that fontset. FONTSETNAME may be a wildcard
b5210ea7
KH
11586 pattern; in that case, we choose some fontset that fits the pattern.
11587 The return value shows which fontset we chose. */
b5cf7a0e 11588
dc43ef94
KH
11589Lisp_Object
11590x_new_fontset (f, fontsetname)
11591 struct frame *f;
11592 char *fontsetname;
11593{
ee569018 11594 int fontset = fs_query_fontset (build_string (fontsetname), 0);
dc43ef94 11595 Lisp_Object result;
b5cf7a0e 11596
dc43ef94
KH
11597 if (fontset < 0)
11598 return Qnil;
b5cf7a0e 11599
2da424f1
KH
11600 if (f->output_data.x->fontset == fontset)
11601 /* This fontset is already set in frame F. There's nothing more
11602 to do. */
ee569018 11603 return fontset_name (fontset);
dc43ef94 11604
ee569018 11605 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
dc43ef94
KH
11606
11607 if (!STRINGP (result))
11608 /* Can't load ASCII font. */
11609 return Qnil;
11610
11611 /* Since x_new_font doesn't update any fontset information, do it now. */
11612 f->output_data.x->fontset = fontset;
dc43ef94 11613
f5d11644
GM
11614#ifdef HAVE_X_I18N
11615 if (FRAME_XIC (f)
11616 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
ee569018 11617 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
f5d11644
GM
11618#endif
11619
dc43ef94 11620 return build_string (fontsetname);
dc6f92b8 11621}
f5d11644
GM
11622
11623\f
11624/***********************************************************************
11625 X Input Methods
11626 ***********************************************************************/
11627
11628#ifdef HAVE_X_I18N
11629
11630#ifdef HAVE_X11R6
11631
11632/* XIM destroy callback function, which is called whenever the
11633 connection to input method XIM dies. CLIENT_DATA contains a
11634 pointer to the x_display_info structure corresponding to XIM. */
11635
11636static void
11637xim_destroy_callback (xim, client_data, call_data)
11638 XIM xim;
11639 XPointer client_data;
11640 XPointer call_data;
11641{
11642 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
11643 Lisp_Object frame, tail;
11644
11645 BLOCK_INPUT;
11646
11647 /* No need to call XDestroyIC.. */
11648 FOR_EACH_FRAME (tail, frame)
11649 {
11650 struct frame *f = XFRAME (frame);
11651 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
11652 {
11653 FRAME_XIC (f) = NULL;
11654 if (FRAME_XIC_FONTSET (f))
11655 {
11656 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
11657 FRAME_XIC_FONTSET (f) = NULL;
11658 }
11659 }
11660 }
11661
11662 /* No need to call XCloseIM. */
11663 dpyinfo->xim = NULL;
11664 XFree (dpyinfo->xim_styles);
11665 UNBLOCK_INPUT;
11666}
11667
11668#endif /* HAVE_X11R6 */
11669
11670/* Open the connection to the XIM server on display DPYINFO.
11671 RESOURCE_NAME is the resource name Emacs uses. */
11672
11673static void
11674xim_open_dpy (dpyinfo, resource_name)
11675 struct x_display_info *dpyinfo;
11676 char *resource_name;
11677{
287f7dd6 11678#ifdef USE_XIM
f5d11644
GM
11679 XIM xim;
11680
11681 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
11682 dpyinfo->xim = xim;
11683
11684 if (xim)
11685 {
f5d11644
GM
11686#ifdef HAVE_X11R6
11687 XIMCallback destroy;
11688#endif
11689
11690 /* Get supported styles and XIM values. */
11691 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
11692
11693#ifdef HAVE_X11R6
11694 destroy.callback = xim_destroy_callback;
11695 destroy.client_data = (XPointer)dpyinfo;
68642df6 11696 /* This isn't prptotyped in OSF 5.0. */
f5d11644
GM
11697 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
11698#endif
11699 }
287f7dd6
GM
11700
11701#else /* not USE_XIM */
11702 dpyinfo->xim = NULL;
11703#endif /* not USE_XIM */
f5d11644
GM
11704}
11705
11706
b9de836c 11707#ifdef HAVE_X11R6_XIM
f5d11644
GM
11708
11709struct xim_inst_t
11710{
11711 struct x_display_info *dpyinfo;
11712 char *resource_name;
11713};
11714
11715/* XIM instantiate callback function, which is called whenever an XIM
11716 server is available. DISPLAY is teh display of the XIM.
11717 CLIENT_DATA contains a pointer to an xim_inst_t structure created
11718 when the callback was registered. */
11719
11720static void
11721xim_instantiate_callback (display, client_data, call_data)
11722 Display *display;
11723 XPointer client_data;
11724 XPointer call_data;
11725{
11726 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
11727 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
11728
11729 /* We don't support multiple XIM connections. */
11730 if (dpyinfo->xim)
11731 return;
11732
11733 xim_open_dpy (dpyinfo, xim_inst->resource_name);
11734
11735 /* Create XIC for the existing frames on the same display, as long
11736 as they have no XIC. */
11737 if (dpyinfo->xim && dpyinfo->reference_count > 0)
11738 {
11739 Lisp_Object tail, frame;
11740
11741 BLOCK_INPUT;
11742 FOR_EACH_FRAME (tail, frame)
11743 {
11744 struct frame *f = XFRAME (frame);
11745
11746 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
11747 if (FRAME_XIC (f) == NULL)
11748 {
11749 create_frame_xic (f);
11750 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
11751 xic_set_statusarea (f);
11752 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
11753 {
11754 struct window *w = XWINDOW (f->selected_window);
11755 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
11756 }
11757 }
11758 }
11759
11760 UNBLOCK_INPUT;
11761 }
11762}
11763
b9de836c 11764#endif /* HAVE_X11R6_XIM */
f5d11644
GM
11765
11766
11767/* Open a connection to the XIM server on display DPYINFO.
11768 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
11769 connection only at the first time. On X11R6, open the connection
11770 in the XIM instantiate callback function. */
11771
11772static void
11773xim_initialize (dpyinfo, resource_name)
11774 struct x_display_info *dpyinfo;
11775 char *resource_name;
11776{
287f7dd6 11777#ifdef USE_XIM
b9de836c 11778#ifdef HAVE_X11R6_XIM
f5d11644
GM
11779 struct xim_inst_t *xim_inst;
11780 int len;
11781
11782 dpyinfo->xim = NULL;
11783 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
11784 xim_inst->dpyinfo = dpyinfo;
11785 len = strlen (resource_name);
11786 xim_inst->resource_name = (char *) xmalloc (len + 1);
11787 bcopy (resource_name, xim_inst->resource_name, len + 1);
11788 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11789 resource_name, EMACS_CLASS,
11790 xim_instantiate_callback,
2ebb2f8b
DL
11791 /* Fixme: This is XPointer in
11792 XFree86 but (XPointer *) on
11793 Tru64, at least. */
11794 (XPointer) xim_inst);
b9de836c 11795#else /* not HAVE_X11R6_XIM */
f5d11644
GM
11796 dpyinfo->xim = NULL;
11797 xim_open_dpy (dpyinfo, resource_name);
b9de836c 11798#endif /* not HAVE_X11R6_XIM */
287f7dd6
GM
11799
11800#else /* not USE_XIM */
11801 dpyinfo->xim = NULL;
11802#endif /* not USE_XIM */
f5d11644
GM
11803}
11804
11805
11806/* Close the connection to the XIM server on display DPYINFO. */
11807
11808static void
11809xim_close_dpy (dpyinfo)
11810 struct x_display_info *dpyinfo;
11811{
287f7dd6 11812#ifdef USE_XIM
b9de836c 11813#ifdef HAVE_X11R6_XIM
8a4f36cc
GM
11814 if (dpyinfo->display)
11815 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11816 NULL, EMACS_CLASS,
11817 xim_instantiate_callback, NULL);
b9de836c 11818#endif /* not HAVE_X11R6_XIM */
8a4f36cc
GM
11819 if (dpyinfo->display)
11820 XCloseIM (dpyinfo->xim);
f5d11644
GM
11821 dpyinfo->xim = NULL;
11822 XFree (dpyinfo->xim_styles);
287f7dd6 11823#endif /* USE_XIM */
f5d11644
GM
11824}
11825
b9de836c 11826#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
11827
11828
dc6f92b8 11829\f
2e365682
RS
11830/* Calculate the absolute position in frame F
11831 from its current recorded position values and gravity. */
11832
dfcf069d 11833void
43bca5d5 11834x_calc_absolute_position (f)
f676886a 11835 struct frame *f;
dc6f92b8 11836{
06a2c219 11837 Window child;
6dba1858 11838 int win_x = 0, win_y = 0;
7556890b 11839 int flags = f->output_data.x->size_hint_flags;
c81412a0
KH
11840 int this_window;
11841
9829ddba
RS
11842 /* We have nothing to do if the current position
11843 is already for the top-left corner. */
11844 if (! ((flags & XNegative) || (flags & YNegative)))
11845 return;
11846
c81412a0 11847#ifdef USE_X_TOOLKIT
7556890b 11848 this_window = XtWindow (f->output_data.x->widget);
c81412a0
KH
11849#else
11850 this_window = FRAME_X_WINDOW (f);
11851#endif
6dba1858
RS
11852
11853 /* Find the position of the outside upper-left corner of
9829ddba
RS
11854 the inner window, with respect to the outer window.
11855 But do this only if we will need the results. */
7556890b 11856 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6dba1858 11857 {
9829ddba
RS
11858 int count;
11859
6dba1858 11860 BLOCK_INPUT;
9829ddba
RS
11861 count = x_catch_errors (FRAME_X_DISPLAY (f));
11862 while (1)
11863 {
11864 x_clear_errors (FRAME_X_DISPLAY (f));
11865 XTranslateCoordinates (FRAME_X_DISPLAY (f),
11866
11867 /* From-window, to-window. */
11868 this_window,
11869 f->output_data.x->parent_desc,
11870
11871 /* From-position, to-position. */
11872 0, 0, &win_x, &win_y,
11873
11874 /* Child of win. */
11875 &child);
11876 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
11877 {
11878 Window newroot, newparent = 0xdeadbeef;
11879 Window *newchildren;
2ebb2f8b 11880 unsigned int nchildren;
9829ddba
RS
11881
11882 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
11883 &newparent, &newchildren, &nchildren))
11884 break;
58769bee 11885
7c3c78a3 11886 XFree ((char *) newchildren);
6dba1858 11887
9829ddba
RS
11888 f->output_data.x->parent_desc = newparent;
11889 }
11890 else
11891 break;
11892 }
6dba1858 11893
9829ddba 11894 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
6dba1858
RS
11895 UNBLOCK_INPUT;
11896 }
11897
11898 /* Treat negative positions as relative to the leftmost bottommost
11899 position that fits on the screen. */
20f55f9a 11900 if (flags & XNegative)
7556890b 11901 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
2e365682
RS
11902 - 2 * f->output_data.x->border_width - win_x
11903 - PIXEL_WIDTH (f)
11904 + f->output_data.x->left_pos);
dc6f92b8 11905
7708ced0
GM
11906 {
11907 int height = PIXEL_HEIGHT (f);
06a2c219 11908
7708ced0
GM
11909#if defined USE_X_TOOLKIT && defined USE_MOTIF
11910 /* Something is fishy here. When using Motif, starting Emacs with
11911 `-g -0-0', the frame appears too low by a few pixels.
11912
11913 This seems to be so because initially, while Emacs is starting,
11914 the column widget's height and the frame's pixel height are
11915 different. The column widget's height is the right one. In
11916 later invocations, when Emacs is up, the frame's pixel height
11917 is right, though.
11918
11919 It's not obvious where the initial small difference comes from.
11920 2000-12-01, gerd. */
11921
11922 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
06a2c219 11923#endif
2e365682 11924
7708ced0
GM
11925 if (flags & YNegative)
11926 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
11927 - 2 * f->output_data.x->border_width
11928 - win_y
11929 - height
11930 + f->output_data.x->top_pos);
11931 }
11932
3a35ab44
RS
11933 /* The left_pos and top_pos
11934 are now relative to the top and left screen edges,
11935 so the flags should correspond. */
7556890b 11936 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
dc6f92b8
JB
11937}
11938
3a35ab44
RS
11939/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
11940 to really change the position, and 0 when calling from
11941 x_make_frame_visible (in that case, XOFF and YOFF are the current
aa3ff7c9
KH
11942 position values). It is -1 when calling from x_set_frame_parameters,
11943 which means, do adjust for borders but don't change the gravity. */
3a35ab44 11944
dfcf069d 11945void
dc05a16b 11946x_set_offset (f, xoff, yoff, change_gravity)
f676886a 11947 struct frame *f;
dc6f92b8 11948 register int xoff, yoff;
dc05a16b 11949 int change_gravity;
dc6f92b8 11950{
4a4cbdd5
KH
11951 int modified_top, modified_left;
11952
aa3ff7c9 11953 if (change_gravity > 0)
3a35ab44 11954 {
7556890b
RS
11955 f->output_data.x->top_pos = yoff;
11956 f->output_data.x->left_pos = xoff;
11957 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
3a35ab44 11958 if (xoff < 0)
7556890b 11959 f->output_data.x->size_hint_flags |= XNegative;
3a35ab44 11960 if (yoff < 0)
7556890b
RS
11961 f->output_data.x->size_hint_flags |= YNegative;
11962 f->output_data.x->win_gravity = NorthWestGravity;
3a35ab44 11963 }
43bca5d5 11964 x_calc_absolute_position (f);
dc6f92b8
JB
11965
11966 BLOCK_INPUT;
c32cdd9a 11967 x_wm_set_size_hint (f, (long) 0, 0);
3a35ab44 11968
7556890b
RS
11969 modified_left = f->output_data.x->left_pos;
11970 modified_top = f->output_data.x->top_pos;
e73ec6fa
RS
11971#if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
11972 this seems to be unnecessary and incorrect. rms, 4/17/97. */
11973 /* It is a mystery why we need to add the border_width here
11974 when the frame is already visible, but experiment says we do. */
aa3ff7c9 11975 if (change_gravity != 0)
4a4cbdd5 11976 {
7556890b
RS
11977 modified_left += f->output_data.x->border_width;
11978 modified_top += f->output_data.x->border_width;
4a4cbdd5 11979 }
e73ec6fa 11980#endif
4a4cbdd5 11981
3afe33e7 11982#ifdef USE_X_TOOLKIT
7556890b 11983 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
4a4cbdd5 11984 modified_left, modified_top);
3afe33e7 11985#else /* not USE_X_TOOLKIT */
334208b7 11986 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4a4cbdd5 11987 modified_left, modified_top);
3afe33e7 11988#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11989 UNBLOCK_INPUT;
11990}
11991
dc6f92b8 11992
499b1844
GM
11993/* Change the size of frame F's X window to COLS/ROWS in the case F
11994 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
11995 top-left-corner window gravity for this size change and subsequent
11996 size changes. Otherwise we leave the window gravity unchanged. */
11997
11998static void
11999x_set_window_size_1 (f, change_gravity, cols, rows)
f676886a 12000 struct frame *f;
bc20ebbf 12001 int change_gravity;
b1c884c3 12002 int cols, rows;
dc6f92b8
JB
12003{
12004 int pixelwidth, pixelheight;
80fd1fe2 12005
b1c884c3 12006 check_frame_size (f, &rows, &cols);
7556890b 12007 f->output_data.x->vertical_scroll_bar_extra
b2cad826
KH
12008 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
12009 ? 0
12010 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
480407eb 12011 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7556890b 12012 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
06a2c219 12013 f->output_data.x->flags_areas_extra
110859fc 12014 = FRAME_FLAGS_AREA_WIDTH (f);
f451eb13
JB
12015 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
12016 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
dc6f92b8 12017
7556890b 12018 f->output_data.x->win_gravity = NorthWestGravity;
c32cdd9a 12019 x_wm_set_size_hint (f, (long) 0, 0);
6ccf47d1 12020
334208b7
RS
12021 XSync (FRAME_X_DISPLAY (f), False);
12022 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12023 pixelwidth, pixelheight);
b1c884c3
JB
12024
12025 /* Now, strictly speaking, we can't be sure that this is accurate,
12026 but the window manager will get around to dealing with the size
12027 change request eventually, and we'll hear how it went when the
dbc4e1c1
JB
12028 ConfigureNotify event gets here.
12029
12030 We could just not bother storing any of this information here,
12031 and let the ConfigureNotify event set everything up, but that
fddd5ceb 12032 might be kind of confusing to the Lisp code, since size changes
dbc4e1c1 12033 wouldn't be reported in the frame parameters until some random
fddd5ceb
RS
12034 point in the future when the ConfigureNotify event arrives.
12035
12036 We pass 1 for DELAY since we can't run Lisp code inside of
12037 a BLOCK_INPUT. */
7d1e984f 12038 change_frame_size (f, rows, cols, 0, 1, 0);
b1c884c3
JB
12039 PIXEL_WIDTH (f) = pixelwidth;
12040 PIXEL_HEIGHT (f) = pixelheight;
12041
aee9a898
RS
12042 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
12043 receive in the ConfigureNotify event; if we get what we asked
12044 for, then the event won't cause the screen to become garbaged, so
12045 we have to make sure to do it here. */
12046 SET_FRAME_GARBAGED (f);
12047
12048 XFlush (FRAME_X_DISPLAY (f));
499b1844
GM
12049}
12050
12051
12052/* Call this to change the size of frame F's x-window.
12053 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
12054 for this size change and subsequent size changes.
12055 Otherwise we leave the window gravity unchanged. */
aee9a898 12056
499b1844
GM
12057void
12058x_set_window_size (f, change_gravity, cols, rows)
12059 struct frame *f;
12060 int change_gravity;
12061 int cols, rows;
12062{
12063 BLOCK_INPUT;
12064
12065#ifdef USE_X_TOOLKIT
12066
f1f4d345 12067 if (f->output_data.x->widget != NULL)
499b1844
GM
12068 {
12069 /* The x and y position of the widget is clobbered by the
12070 call to XtSetValues within EmacsFrameSetCharSize.
12071 This is a real kludge, but I don't understand Xt so I can't
12072 figure out a correct fix. Can anyone else tell me? -- rms. */
12073 int xpos = f->output_data.x->widget->core.x;
12074 int ypos = f->output_data.x->widget->core.y;
12075 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
12076 f->output_data.x->widget->core.x = xpos;
12077 f->output_data.x->widget->core.y = ypos;
12078 }
12079 else
12080 x_set_window_size_1 (f, change_gravity, cols, rows);
12081
12082#else /* not USE_X_TOOLKIT */
12083
12084 x_set_window_size_1 (f, change_gravity, cols, rows);
12085
aee9a898
RS
12086#endif /* not USE_X_TOOLKIT */
12087
4d73d038 12088 /* If cursor was outside the new size, mark it as off. */
06a2c219 12089 mark_window_cursors_off (XWINDOW (f->root_window));
4d73d038 12090
aee9a898
RS
12091 /* Clear out any recollection of where the mouse highlighting was,
12092 since it might be in a place that's outside the new frame size.
12093 Actually checking whether it is outside is a pain in the neck,
12094 so don't try--just let the highlighting be done afresh with new size. */
e687d06e 12095 cancel_mouse_face (f);
dbc4e1c1 12096
dc6f92b8
JB
12097 UNBLOCK_INPUT;
12098}
dc6f92b8 12099\f
d047c4eb 12100/* Mouse warping. */
dc6f92b8 12101
9b378208 12102void
f676886a
JB
12103x_set_mouse_position (f, x, y)
12104 struct frame *f;
dc6f92b8
JB
12105 int x, y;
12106{
12107 int pix_x, pix_y;
12108
7556890b
RS
12109 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
12110 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
f451eb13
JB
12111
12112 if (pix_x < 0) pix_x = 0;
12113 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
12114
12115 if (pix_y < 0) pix_y = 0;
12116 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
dc6f92b8
JB
12117
12118 BLOCK_INPUT;
dc6f92b8 12119
334208b7
RS
12120 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
12121 0, 0, 0, 0, pix_x, pix_y);
dc6f92b8
JB
12122 UNBLOCK_INPUT;
12123}
12124
9b378208
RS
12125/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
12126
12127void
12128x_set_mouse_pixel_position (f, pix_x, pix_y)
12129 struct frame *f;
12130 int pix_x, pix_y;
12131{
12132 BLOCK_INPUT;
12133
334208b7
RS
12134 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
12135 0, 0, 0, 0, pix_x, pix_y);
9b378208
RS
12136 UNBLOCK_INPUT;
12137}
d047c4eb
KH
12138\f
12139/* focus shifting, raising and lowering. */
9b378208 12140
dfcf069d 12141void
f676886a
JB
12142x_focus_on_frame (f)
12143 struct frame *f;
dc6f92b8 12144{
1fb20991 12145#if 0 /* This proves to be unpleasant. */
f676886a 12146 x_raise_frame (f);
1fb20991 12147#endif
6d4238f3
JB
12148#if 0
12149 /* I don't think that the ICCCM allows programs to do things like this
12150 without the interaction of the window manager. Whatever you end up
f676886a 12151 doing with this code, do it to x_unfocus_frame too. */
334208b7 12152 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dc6f92b8 12153 RevertToPointerRoot, CurrentTime);
c118dd06 12154#endif /* ! 0 */
dc6f92b8
JB
12155}
12156
dfcf069d 12157void
f676886a
JB
12158x_unfocus_frame (f)
12159 struct frame *f;
dc6f92b8 12160{
6d4238f3 12161#if 0
f676886a 12162 /* Look at the remarks in x_focus_on_frame. */
0f941935 12163 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
334208b7 12164 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
dc6f92b8 12165 RevertToPointerRoot, CurrentTime);
c118dd06 12166#endif /* ! 0 */
dc6f92b8
JB
12167}
12168
f676886a 12169/* Raise frame F. */
dc6f92b8 12170
dfcf069d 12171void
f676886a
JB
12172x_raise_frame (f)
12173 struct frame *f;
dc6f92b8 12174{
3a88c238 12175 if (f->async_visible)
dc6f92b8
JB
12176 {
12177 BLOCK_INPUT;
3afe33e7 12178#ifdef USE_X_TOOLKIT
7556890b 12179 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 12180#else /* not USE_X_TOOLKIT */
334208b7 12181 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 12182#endif /* not USE_X_TOOLKIT */
334208b7 12183 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
12184 UNBLOCK_INPUT;
12185 }
12186}
12187
f676886a 12188/* Lower frame F. */
dc6f92b8 12189
dfcf069d 12190void
f676886a
JB
12191x_lower_frame (f)
12192 struct frame *f;
dc6f92b8 12193{
3a88c238 12194 if (f->async_visible)
dc6f92b8
JB
12195 {
12196 BLOCK_INPUT;
3afe33e7 12197#ifdef USE_X_TOOLKIT
7556890b 12198 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 12199#else /* not USE_X_TOOLKIT */
334208b7 12200 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 12201#endif /* not USE_X_TOOLKIT */
334208b7 12202 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
12203 UNBLOCK_INPUT;
12204 }
12205}
12206
dbc4e1c1 12207static void
6b0442dc 12208XTframe_raise_lower (f, raise_flag)
dbc4e1c1 12209 FRAME_PTR f;
6b0442dc 12210 int raise_flag;
dbc4e1c1 12211{
6b0442dc 12212 if (raise_flag)
dbc4e1c1
JB
12213 x_raise_frame (f);
12214 else
12215 x_lower_frame (f);
12216}
d047c4eb
KH
12217\f
12218/* Change of visibility. */
dc6f92b8 12219
9382638d
KH
12220/* This tries to wait until the frame is really visible.
12221 However, if the window manager asks the user where to position
12222 the frame, this will return before the user finishes doing that.
12223 The frame will not actually be visible at that time,
12224 but it will become visible later when the window manager
12225 finishes with it. */
12226
dfcf069d 12227void
f676886a
JB
12228x_make_frame_visible (f)
12229 struct frame *f;
dc6f92b8 12230{
990ba854 12231 Lisp_Object type;
1aa6072f 12232 int original_top, original_left;
31be9251
GM
12233 int retry_count = 2;
12234
12235 retry:
dc6f92b8 12236
dc6f92b8 12237 BLOCK_INPUT;
dc6f92b8 12238
990ba854
RS
12239 type = x_icon_type (f);
12240 if (!NILP (type))
12241 x_bitmap_icon (f, type);
bdcd49ba 12242
f676886a 12243 if (! FRAME_VISIBLE_P (f))
90e65f07 12244 {
1aa6072f
RS
12245 /* We test FRAME_GARBAGED_P here to make sure we don't
12246 call x_set_offset a second time
12247 if we get to x_make_frame_visible a second time
12248 before the window gets really visible. */
12249 if (! FRAME_ICONIFIED_P (f)
12250 && ! f->output_data.x->asked_for_visible)
12251 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12252
12253 f->output_data.x->asked_for_visible = 1;
12254
90e65f07 12255 if (! EQ (Vx_no_window_manager, Qt))
f676886a 12256 x_wm_set_window_state (f, NormalState);
3afe33e7 12257#ifdef USE_X_TOOLKIT
d7a38a2e 12258 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 12259 XtMapWidget (f->output_data.x->widget);
3afe33e7 12260#else /* not USE_X_TOOLKIT */
7f9c7f94 12261 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 12262#endif /* not USE_X_TOOLKIT */
0134a210
RS
12263#if 0 /* This seems to bring back scroll bars in the wrong places
12264 if the window configuration has changed. They seem
12265 to come back ok without this. */
ab648270 12266 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
334208b7 12267 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
0134a210 12268#endif
90e65f07 12269 }
dc6f92b8 12270
334208b7 12271 XFlush (FRAME_X_DISPLAY (f));
90e65f07 12272
0dacf791
RS
12273 /* Synchronize to ensure Emacs knows the frame is visible
12274 before we do anything else. We do this loop with input not blocked
12275 so that incoming events are handled. */
12276 {
12277 Lisp_Object frame;
12ce2351 12278 int count;
28c01ffe
RS
12279 /* This must be before UNBLOCK_INPUT
12280 since events that arrive in response to the actions above
12281 will set it when they are handled. */
12282 int previously_visible = f->output_data.x->has_been_visible;
1aa6072f
RS
12283
12284 original_left = f->output_data.x->left_pos;
12285 original_top = f->output_data.x->top_pos;
c0a04927
RS
12286
12287 /* This must come after we set COUNT. */
12288 UNBLOCK_INPUT;
12289
2745e6c4 12290 /* We unblock here so that arriving X events are processed. */
1aa6072f 12291
dcb07ae9
RS
12292 /* Now move the window back to where it was "supposed to be".
12293 But don't do it if the gravity is negative.
12294 When the gravity is negative, this uses a position
28c01ffe
RS
12295 that is 3 pixels too low. Perhaps that's really the border width.
12296
12297 Don't do this if the window has never been visible before,
12298 because the window manager may choose the position
12299 and we don't want to override it. */
1aa6072f 12300
4d3f5d9a 12301 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
06c488fd 12302 && f->output_data.x->win_gravity == NorthWestGravity
28c01ffe 12303 && previously_visible)
1aa6072f 12304 {
2745e6c4
RS
12305 Drawable rootw;
12306 int x, y;
12307 unsigned int width, height, border, depth;
06a2c219 12308
1aa6072f 12309 BLOCK_INPUT;
9829ddba 12310
06a2c219
GM
12311 /* On some window managers (such as FVWM) moving an existing
12312 window, even to the same place, causes the window manager
12313 to introduce an offset. This can cause the window to move
12314 to an unexpected location. Check the geometry (a little
12315 slow here) and then verify that the window is in the right
12316 place. If the window is not in the right place, move it
12317 there, and take the potential window manager hit. */
2745e6c4
RS
12318 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12319 &rootw, &x, &y, &width, &height, &border, &depth);
12320
12321 if (original_left != x || original_top != y)
12322 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12323 original_left, original_top);
12324
1aa6072f
RS
12325 UNBLOCK_INPUT;
12326 }
9829ddba 12327
e0c1aef2 12328 XSETFRAME (frame, f);
c0a04927 12329
12ce2351
GM
12330 /* Wait until the frame is visible. Process X events until a
12331 MapNotify event has been seen, or until we think we won't get a
12332 MapNotify at all.. */
12333 for (count = input_signal_count + 10;
12334 input_signal_count < count && !FRAME_VISIBLE_P (f);)
2a6cf806 12335 {
12ce2351 12336 /* Force processing of queued events. */
334208b7 12337 x_sync (f);
12ce2351
GM
12338
12339 /* Machines that do polling rather than SIGIO have been
12340 observed to go into a busy-wait here. So we'll fake an
12341 alarm signal to let the handler know that there's something
12342 to be read. We used to raise a real alarm, but it seems
12343 that the handler isn't always enabled here. This is
12344 probably a bug. */
8b2f8d4e 12345 if (input_polling_used ())
3b2fa4e6 12346 {
12ce2351
GM
12347 /* It could be confusing if a real alarm arrives while
12348 processing the fake one. Turn it off and let the
12349 handler reset it. */
3e71d8f2 12350 extern void poll_for_input_1 P_ ((void));
bffcfca9
GM
12351 int old_poll_suppress_count = poll_suppress_count;
12352 poll_suppress_count = 1;
12353 poll_for_input_1 ();
12354 poll_suppress_count = old_poll_suppress_count;
3b2fa4e6 12355 }
12ce2351
GM
12356
12357 /* See if a MapNotify event has been processed. */
12358 FRAME_SAMPLE_VISIBILITY (f);
2a6cf806 12359 }
31be9251
GM
12360
12361 /* 2000-09-28: In
12362
12363 (let ((f (selected-frame)))
12364 (iconify-frame f)
12365 (raise-frame f))
12366
12367 the frame is not raised with various window managers on
12368 FreeBSD, Linux and Solaris. It turns out that, for some
12369 unknown reason, the call to XtMapWidget is completely ignored.
12370 Mapping the widget a second time works. */
12371
12372 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
12373 goto retry;
0dacf791 12374 }
dc6f92b8
JB
12375}
12376
06a2c219 12377/* Change from mapped state to withdrawn state. */
dc6f92b8 12378
d047c4eb
KH
12379/* Make the frame visible (mapped and not iconified). */
12380
dfcf069d 12381void
f676886a
JB
12382x_make_frame_invisible (f)
12383 struct frame *f;
dc6f92b8 12384{
546e6d5b
RS
12385 Window window;
12386
12387#ifdef USE_X_TOOLKIT
12388 /* Use the frame's outermost window, not the one we normally draw on. */
7556890b 12389 window = XtWindow (f->output_data.x->widget);
546e6d5b
RS
12390#else /* not USE_X_TOOLKIT */
12391 window = FRAME_X_WINDOW (f);
12392#endif /* not USE_X_TOOLKIT */
dc6f92b8 12393
9319ae23 12394 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
12395 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12396 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 12397
5627c40e 12398#if 0/* This might add unreliability; I don't trust it -- rms. */
9319ae23 12399 if (! f->async_visible && ! f->async_iconified)
dc6f92b8 12400 return;
5627c40e 12401#endif
dc6f92b8
JB
12402
12403 BLOCK_INPUT;
c118dd06 12404
af31d76f
RS
12405 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
12406 that the current position of the window is user-specified, rather than
12407 program-specified, so that when the window is mapped again, it will be
12408 placed at the same location, without forcing the user to position it
12409 by hand again (they have already done that once for this window.) */
c32cdd9a 12410 x_wm_set_size_hint (f, (long) 0, 1);
af31d76f 12411
c118dd06
JB
12412#ifdef HAVE_X11R4
12413
334208b7
RS
12414 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
12415 DefaultScreen (FRAME_X_DISPLAY (f))))
c118dd06
JB
12416 {
12417 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 12418 error ("Can't notify window manager of window withdrawal");
c118dd06 12419 }
c118dd06 12420#else /* ! defined (HAVE_X11R4) */
16bd92ea 12421
c118dd06 12422 /* Tell the window manager what we're going to do. */
dc6f92b8
JB
12423 if (! EQ (Vx_no_window_manager, Qt))
12424 {
16bd92ea 12425 XEvent unmap;
dc6f92b8 12426
16bd92ea 12427 unmap.xunmap.type = UnmapNotify;
546e6d5b 12428 unmap.xunmap.window = window;
334208b7 12429 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
16bd92ea 12430 unmap.xunmap.from_configure = False;
334208b7
RS
12431 if (! XSendEvent (FRAME_X_DISPLAY (f),
12432 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea 12433 False,
06a2c219 12434 SubstructureRedirectMaskSubstructureNotifyMask,
16bd92ea
JB
12435 &unmap))
12436 {
12437 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 12438 error ("Can't notify window manager of withdrawal");
16bd92ea 12439 }
dc6f92b8
JB
12440 }
12441
16bd92ea 12442 /* Unmap the window ourselves. Cheeky! */
334208b7 12443 XUnmapWindow (FRAME_X_DISPLAY (f), window);
c118dd06 12444#endif /* ! defined (HAVE_X11R4) */
dc6f92b8 12445
5627c40e
RS
12446 /* We can't distinguish this from iconification
12447 just by the event that we get from the server.
12448 So we can't win using the usual strategy of letting
12449 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
12450 and synchronize with the server to make sure we agree. */
12451 f->visible = 0;
12452 FRAME_ICONIFIED_P (f) = 0;
12453 f->async_visible = 0;
12454 f->async_iconified = 0;
12455
334208b7 12456 x_sync (f);
5627c40e 12457
dc6f92b8
JB
12458 UNBLOCK_INPUT;
12459}
12460
06a2c219 12461/* Change window state from mapped to iconified. */
dc6f92b8 12462
dfcf069d 12463void
f676886a
JB
12464x_iconify_frame (f)
12465 struct frame *f;
dc6f92b8 12466{
3afe33e7 12467 int result;
990ba854 12468 Lisp_Object type;
dc6f92b8 12469
9319ae23 12470 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
12471 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12472 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 12473
3a88c238 12474 if (f->async_iconified)
dc6f92b8
JB
12475 return;
12476
3afe33e7 12477 BLOCK_INPUT;
546e6d5b 12478
9af3143a
RS
12479 FRAME_SAMPLE_VISIBILITY (f);
12480
990ba854
RS
12481 type = x_icon_type (f);
12482 if (!NILP (type))
12483 x_bitmap_icon (f, type);
bdcd49ba
RS
12484
12485#ifdef USE_X_TOOLKIT
12486
546e6d5b
RS
12487 if (! FRAME_VISIBLE_P (f))
12488 {
12489 if (! EQ (Vx_no_window_manager, Qt))
12490 x_wm_set_window_state (f, IconicState);
12491 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 12492 XtMapWidget (f->output_data.x->widget);
9cf30a30
RS
12493 /* The server won't give us any event to indicate
12494 that an invisible frame was changed to an icon,
12495 so we have to record it here. */
12496 f->iconified = 1;
1e6bc770 12497 f->visible = 1;
9cf30a30 12498 f->async_iconified = 1;
1e6bc770 12499 f->async_visible = 0;
546e6d5b
RS
12500 UNBLOCK_INPUT;
12501 return;
12502 }
12503
334208b7 12504 result = XIconifyWindow (FRAME_X_DISPLAY (f),
7556890b 12505 XtWindow (f->output_data.x->widget),
334208b7 12506 DefaultScreen (FRAME_X_DISPLAY (f)));
3afe33e7
RS
12507 UNBLOCK_INPUT;
12508
12509 if (!result)
546e6d5b 12510 error ("Can't notify window manager of iconification");
3afe33e7
RS
12511
12512 f->async_iconified = 1;
1e6bc770
RS
12513 f->async_visible = 0;
12514
8c002a25
KH
12515
12516 BLOCK_INPUT;
334208b7 12517 XFlush (FRAME_X_DISPLAY (f));
8c002a25 12518 UNBLOCK_INPUT;
3afe33e7
RS
12519#else /* not USE_X_TOOLKIT */
12520
fd13dbb2
RS
12521 /* Make sure the X server knows where the window should be positioned,
12522 in case the user deiconifies with the window manager. */
12523 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
7556890b 12524 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
fd13dbb2 12525
16bd92ea
JB
12526 /* Since we don't know which revision of X we're running, we'll use both
12527 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
12528
12529 /* X11R4: send a ClientMessage to the window manager using the
12530 WM_CHANGE_STATE type. */
12531 {
12532 XEvent message;
58769bee 12533
c118dd06 12534 message.xclient.window = FRAME_X_WINDOW (f);
16bd92ea 12535 message.xclient.type = ClientMessage;
334208b7 12536 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
16bd92ea
JB
12537 message.xclient.format = 32;
12538 message.xclient.data.l[0] = IconicState;
12539
334208b7
RS
12540 if (! XSendEvent (FRAME_X_DISPLAY (f),
12541 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea
JB
12542 False,
12543 SubstructureRedirectMask | SubstructureNotifyMask,
12544 &message))
dc6f92b8
JB
12545 {
12546 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 12547 error ("Can't notify window manager of iconification");
dc6f92b8 12548 }
16bd92ea 12549 }
dc6f92b8 12550
58769bee 12551 /* X11R3: set the initial_state field of the window manager hints to
16bd92ea
JB
12552 IconicState. */
12553 x_wm_set_window_state (f, IconicState);
dc6f92b8 12554
a9c00105
RS
12555 if (!FRAME_VISIBLE_P (f))
12556 {
12557 /* If the frame was withdrawn, before, we must map it. */
7f9c7f94 12558 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
a9c00105
RS
12559 }
12560
3a88c238 12561 f->async_iconified = 1;
1e6bc770 12562 f->async_visible = 0;
dc6f92b8 12563
334208b7 12564 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 12565 UNBLOCK_INPUT;
8c002a25 12566#endif /* not USE_X_TOOLKIT */
dc6f92b8 12567}
19f71add 12568
d047c4eb 12569\f
19f71add 12570/* Free X resources of frame F. */
dc6f92b8 12571
dfcf069d 12572void
19f71add 12573x_free_frame_resources (f)
f676886a 12574 struct frame *f;
dc6f92b8 12575{
7f9c7f94
RS
12576 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12577
dc6f92b8 12578 BLOCK_INPUT;
c0ff3fab 12579
6186a4a0
RS
12580 /* If a display connection is dead, don't try sending more
12581 commands to the X server. */
19f71add 12582 if (dpyinfo->display)
6186a4a0 12583 {
19f71add 12584 if (f->output_data.x->icon_desc)
6186a4a0 12585 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
19f71add 12586
31f41daf 12587#ifdef HAVE_X_I18N
f5d11644
GM
12588 if (FRAME_XIC (f))
12589 free_frame_xic (f);
31f41daf 12590#endif
19f71add 12591
2662734b 12592 if (FRAME_X_WINDOW (f))
19f71add
GM
12593 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12594
3afe33e7 12595#ifdef USE_X_TOOLKIT
06a2c219
GM
12596 if (f->output_data.x->widget)
12597 XtDestroyWidget (f->output_data.x->widget);
6186a4a0 12598 free_frame_menubar (f);
3afe33e7
RS
12599#endif /* USE_X_TOOLKIT */
12600
3e71d8f2
GM
12601 unload_color (f, f->output_data.x->foreground_pixel);
12602 unload_color (f, f->output_data.x->background_pixel);
12603 unload_color (f, f->output_data.x->cursor_pixel);
12604 unload_color (f, f->output_data.x->cursor_foreground_pixel);
12605 unload_color (f, f->output_data.x->border_pixel);
12606 unload_color (f, f->output_data.x->mouse_pixel);
19f71add 12607
3e71d8f2
GM
12608 if (f->output_data.x->scroll_bar_background_pixel != -1)
12609 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
12610 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
12611 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
12612 if (f->output_data.x->white_relief.allocated_p)
12613 unload_color (f, f->output_data.x->white_relief.pixel);
12614 if (f->output_data.x->black_relief.allocated_p)
12615 unload_color (f, f->output_data.x->black_relief.pixel);
4ca78676 12616
19f71add
GM
12617 if (FRAME_FACE_CACHE (f))
12618 free_frame_faces (f);
12619
4ca78676 12620 x_free_gcs (f);
6186a4a0
RS
12621 XFlush (FRAME_X_DISPLAY (f));
12622 }
dc6f92b8 12623
df89d8a4 12624 if (f->output_data.x->saved_menu_event)
06a2c219 12625 xfree (f->output_data.x->saved_menu_event);
0c49ce2f 12626
7556890b 12627 xfree (f->output_data.x);
19f71add
GM
12628 f->output_data.x = NULL;
12629
0f941935
KH
12630 if (f == dpyinfo->x_focus_frame)
12631 dpyinfo->x_focus_frame = 0;
12632 if (f == dpyinfo->x_focus_event_frame)
12633 dpyinfo->x_focus_event_frame = 0;
12634 if (f == dpyinfo->x_highlight_frame)
12635 dpyinfo->x_highlight_frame = 0;
c0ff3fab 12636
7f9c7f94 12637 if (f == dpyinfo->mouse_face_mouse_frame)
dc05a16b 12638 {
7f9c7f94
RS
12639 dpyinfo->mouse_face_beg_row
12640 = dpyinfo->mouse_face_beg_col = -1;
12641 dpyinfo->mouse_face_end_row
12642 = dpyinfo->mouse_face_end_col = -1;
12643 dpyinfo->mouse_face_window = Qnil;
21323706
RS
12644 dpyinfo->mouse_face_deferred_gc = 0;
12645 dpyinfo->mouse_face_mouse_frame = 0;
dc05a16b 12646 }
0134a210 12647
c0ff3fab 12648 UNBLOCK_INPUT;
dc6f92b8 12649}
19f71add
GM
12650
12651
12652/* Destroy the X window of frame F. */
12653
12654void
12655x_destroy_window (f)
12656 struct frame *f;
12657{
12658 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12659
12660 /* If a display connection is dead, don't try sending more
12661 commands to the X server. */
12662 if (dpyinfo->display != 0)
12663 x_free_frame_resources (f);
12664
12665 dpyinfo->reference_count--;
12666}
12667
dc6f92b8 12668\f
f451eb13
JB
12669/* Setting window manager hints. */
12670
af31d76f
RS
12671/* Set the normal size hints for the window manager, for frame F.
12672 FLAGS is the flags word to use--or 0 meaning preserve the flags
12673 that the window now has.
12674 If USER_POSITION is nonzero, we set the USPosition
12675 flag (this is useful when FLAGS is 0). */
6dba1858 12676
dfcf069d 12677void
af31d76f 12678x_wm_set_size_hint (f, flags, user_position)
f676886a 12679 struct frame *f;
af31d76f
RS
12680 long flags;
12681 int user_position;
dc6f92b8
JB
12682{
12683 XSizeHints size_hints;
3afe33e7
RS
12684
12685#ifdef USE_X_TOOLKIT
7e4f2521
FP
12686 Arg al[2];
12687 int ac = 0;
12688 Dimension widget_width, widget_height;
7556890b 12689 Window window = XtWindow (f->output_data.x->widget);
3afe33e7 12690#else /* not USE_X_TOOLKIT */
c118dd06 12691 Window window = FRAME_X_WINDOW (f);
3afe33e7 12692#endif /* not USE_X_TOOLKIT */
dc6f92b8 12693
b72a58fd
RS
12694 /* Setting PMaxSize caused various problems. */
12695 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
dc6f92b8 12696
7556890b
RS
12697 size_hints.x = f->output_data.x->left_pos;
12698 size_hints.y = f->output_data.x->top_pos;
7553a6b7 12699
7e4f2521
FP
12700#ifdef USE_X_TOOLKIT
12701 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
12702 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
7556890b 12703 XtGetValues (f->output_data.x->widget, al, ac);
7e4f2521
FP
12704 size_hints.height = widget_height;
12705 size_hints.width = widget_width;
12706#else /* not USE_X_TOOLKIT */
f676886a
JB
12707 size_hints.height = PIXEL_HEIGHT (f);
12708 size_hints.width = PIXEL_WIDTH (f);
7e4f2521 12709#endif /* not USE_X_TOOLKIT */
7553a6b7 12710
7556890b
RS
12711 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
12712 size_hints.height_inc = f->output_data.x->line_height;
334208b7
RS
12713 size_hints.max_width
12714 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
12715 size_hints.max_height
12716 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
0134a210 12717
d067ea8b
KH
12718 /* Calculate the base and minimum sizes.
12719
12720 (When we use the X toolkit, we don't do it here.
12721 Instead we copy the values that the widgets are using, below.) */
12722#ifndef USE_X_TOOLKIT
b1c884c3 12723 {
b0342f17 12724 int base_width, base_height;
0134a210 12725 int min_rows = 0, min_cols = 0;
b0342f17 12726
f451eb13
JB
12727 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
12728 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
b0342f17 12729
0134a210 12730 check_frame_size (f, &min_rows, &min_cols);
b0342f17 12731
0134a210
RS
12732 /* The window manager uses the base width hints to calculate the
12733 current number of rows and columns in the frame while
12734 resizing; min_width and min_height aren't useful for this
12735 purpose, since they might not give the dimensions for a
12736 zero-row, zero-column frame.
58769bee 12737
0134a210
RS
12738 We use the base_width and base_height members if we have
12739 them; otherwise, we set the min_width and min_height members
12740 to the size for a zero x zero frame. */
b0342f17
JB
12741
12742#ifdef HAVE_X11R4
0134a210
RS
12743 size_hints.flags |= PBaseSize;
12744 size_hints.base_width = base_width;
12745 size_hints.base_height = base_height;
12746 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
12747 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
b0342f17 12748#else
0134a210
RS
12749 size_hints.min_width = base_width;
12750 size_hints.min_height = base_height;
b0342f17 12751#endif
b1c884c3 12752 }
dc6f92b8 12753
d067ea8b 12754 /* If we don't need the old flags, we don't need the old hint at all. */
af31d76f 12755 if (flags)
dc6f92b8 12756 {
d067ea8b
KH
12757 size_hints.flags |= flags;
12758 goto no_read;
12759 }
12760#endif /* not USE_X_TOOLKIT */
12761
12762 {
12763 XSizeHints hints; /* Sometimes I hate X Windows... */
12764 long supplied_return;
12765 int value;
af31d76f
RS
12766
12767#ifdef HAVE_X11R4
d067ea8b
KH
12768 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
12769 &supplied_return);
af31d76f 12770#else
d067ea8b 12771 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
af31d76f 12772#endif
58769bee 12773
d067ea8b
KH
12774#ifdef USE_X_TOOLKIT
12775 size_hints.base_height = hints.base_height;
12776 size_hints.base_width = hints.base_width;
12777 size_hints.min_height = hints.min_height;
12778 size_hints.min_width = hints.min_width;
12779#endif
12780
12781 if (flags)
12782 size_hints.flags |= flags;
12783 else
12784 {
12785 if (value == 0)
12786 hints.flags = 0;
12787 if (hints.flags & PSize)
12788 size_hints.flags |= PSize;
12789 if (hints.flags & PPosition)
12790 size_hints.flags |= PPosition;
12791 if (hints.flags & USPosition)
12792 size_hints.flags |= USPosition;
12793 if (hints.flags & USSize)
12794 size_hints.flags |= USSize;
12795 }
12796 }
12797
06a2c219 12798#ifndef USE_X_TOOLKIT
d067ea8b 12799 no_read:
06a2c219 12800#endif
0134a210 12801
af31d76f 12802#ifdef PWinGravity
7556890b 12803 size_hints.win_gravity = f->output_data.x->win_gravity;
af31d76f 12804 size_hints.flags |= PWinGravity;
dc05a16b 12805
af31d76f 12806 if (user_position)
6dba1858 12807 {
af31d76f
RS
12808 size_hints.flags &= ~ PPosition;
12809 size_hints.flags |= USPosition;
6dba1858 12810 }
2554751d 12811#endif /* PWinGravity */
6dba1858 12812
b0342f17 12813#ifdef HAVE_X11R4
334208b7 12814 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 12815#else
334208b7 12816 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 12817#endif
dc6f92b8
JB
12818}
12819
12820/* Used for IconicState or NormalState */
06a2c219 12821
dfcf069d 12822void
f676886a
JB
12823x_wm_set_window_state (f, state)
12824 struct frame *f;
dc6f92b8
JB
12825 int state;
12826{
3afe33e7 12827#ifdef USE_X_TOOLKIT
546e6d5b
RS
12828 Arg al[1];
12829
12830 XtSetArg (al[0], XtNinitialState, state);
7556890b 12831 XtSetValues (f->output_data.x->widget, al, 1);
3afe33e7 12832#else /* not USE_X_TOOLKIT */
c118dd06 12833 Window window = FRAME_X_WINDOW (f);
dc6f92b8 12834
7556890b
RS
12835 f->output_data.x->wm_hints.flags |= StateHint;
12836 f->output_data.x->wm_hints.initial_state = state;
b1c884c3 12837
7556890b 12838 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
546e6d5b 12839#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12840}
12841
dfcf069d 12842void
7f2ae036 12843x_wm_set_icon_pixmap (f, pixmap_id)
f676886a 12844 struct frame *f;
7f2ae036 12845 int pixmap_id;
dc6f92b8 12846{
d2bd6bc4
RS
12847 Pixmap icon_pixmap;
12848
06a2c219 12849#ifndef USE_X_TOOLKIT
c118dd06 12850 Window window = FRAME_X_WINDOW (f);
75231bad 12851#endif
dc6f92b8 12852
7f2ae036 12853 if (pixmap_id > 0)
dbc4e1c1 12854 {
d2bd6bc4 12855 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
7556890b 12856 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
dbc4e1c1
JB
12857 }
12858 else
68568555
RS
12859 {
12860 /* It seems there is no way to turn off use of an icon pixmap.
12861 The following line does it, only if no icon has yet been created,
12862 for some window managers. But with mwm it crashes.
12863 Some people say it should clear the IconPixmapHint bit in this case,
12864 but that doesn't work, and the X consortium said it isn't the
12865 right thing at all. Since there is no way to win,
12866 best to explicitly give up. */
12867#if 0
12868 f->output_data.x->wm_hints.icon_pixmap = None;
12869#else
12870 return;
12871#endif
12872 }
b1c884c3 12873
d2bd6bc4
RS
12874#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
12875
12876 {
12877 Arg al[1];
12878 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12879 XtSetValues (f->output_data.x->widget, al, 1);
12880 }
12881
12882#else /* not USE_X_TOOLKIT */
12883
7556890b
RS
12884 f->output_data.x->wm_hints.flags |= IconPixmapHint;
12885 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
d2bd6bc4
RS
12886
12887#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12888}
12889
dfcf069d 12890void
f676886a
JB
12891x_wm_set_icon_position (f, icon_x, icon_y)
12892 struct frame *f;
dc6f92b8
JB
12893 int icon_x, icon_y;
12894{
75231bad 12895#ifdef USE_X_TOOLKIT
7556890b 12896 Window window = XtWindow (f->output_data.x->widget);
75231bad 12897#else
c118dd06 12898 Window window = FRAME_X_WINDOW (f);
75231bad 12899#endif
dc6f92b8 12900
7556890b
RS
12901 f->output_data.x->wm_hints.flags |= IconPositionHint;
12902 f->output_data.x->wm_hints.icon_x = icon_x;
12903 f->output_data.x->wm_hints.icon_y = icon_y;
b1c884c3 12904
7556890b 12905 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
dc6f92b8
JB
12906}
12907
12908\f
06a2c219
GM
12909/***********************************************************************
12910 Fonts
12911 ***********************************************************************/
dc43ef94
KH
12912
12913/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
06a2c219 12914
dc43ef94
KH
12915struct font_info *
12916x_get_font_info (f, font_idx)
12917 FRAME_PTR f;
12918 int font_idx;
12919{
12920 return (FRAME_X_FONT_TABLE (f) + font_idx);
12921}
12922
12923
9c11f79e
GM
12924/* Return a list of names of available fonts matching PATTERN on frame F.
12925
12926 If SIZE is > 0, it is the size (maximum bounds width) of fonts
12927 to be listed.
12928
12929 SIZE < 0 means include scalable fonts.
12930
12931 Frame F null means we have not yet created any frame on X, and
12932 consult the first display in x_display_list. MAXNAMES sets a limit
12933 on how many fonts to match. */
dc43ef94
KH
12934
12935Lisp_Object
12936x_list_fonts (f, pattern, size, maxnames)
9c11f79e 12937 struct frame *f;
dc43ef94
KH
12938 Lisp_Object pattern;
12939 int size;
12940 int maxnames;
12941{
06a2c219
GM
12942 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
12943 Lisp_Object tem, second_best;
9c11f79e
GM
12944 struct x_display_info *dpyinfo
12945 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
12946 Display *dpy = dpyinfo->display;
09c6077f 12947 int try_XLoadQueryFont = 0;
53ca4657 12948 int count;
9c11f79e
GM
12949 int allow_scalable_fonts_p = 0;
12950
12951 if (size < 0)
12952 {
12953 allow_scalable_fonts_p = 1;
12954 size = 0;
12955 }
dc43ef94 12956
6b0efe73 12957 patterns = Fassoc (pattern, Valternate_fontname_alist);
2da424f1
KH
12958 if (NILP (patterns))
12959 patterns = Fcons (pattern, Qnil);
81ba44e5 12960
09c6077f
KH
12961 if (maxnames == 1 && !size)
12962 /* We can return any single font matching PATTERN. */
12963 try_XLoadQueryFont = 1;
9a32686f 12964
8e713be6 12965 for (; CONSP (patterns); patterns = XCDR (patterns))
dc43ef94 12966 {
dc43ef94 12967 int num_fonts;
3e71d8f2 12968 char **names = NULL;
dc43ef94 12969
8e713be6 12970 pattern = XCAR (patterns);
536f4067
RS
12971 /* See if we cached the result for this particular query.
12972 The cache is an alist of the form:
9c11f79e
GM
12973 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
12974 tem = XCDR (dpyinfo->name_list_element);
12975 key = Fcons (Fcons (pattern, make_number (maxnames)),
12976 allow_scalable_fonts_p ? Qt : Qnil);
12977 list = Fassoc (key, tem);
12978 if (!NILP (list))
b5210ea7
KH
12979 {
12980 list = Fcdr_safe (list);
12981 /* We have a cashed list. Don't have to get the list again. */
12982 goto label_cached;
12983 }
12984
12985 /* At first, put PATTERN in the cache. */
09c6077f 12986
dc43ef94 12987 BLOCK_INPUT;
17d85edc
KH
12988 count = x_catch_errors (dpy);
12989
09c6077f
KH
12990 if (try_XLoadQueryFont)
12991 {
12992 XFontStruct *font;
12993 unsigned long value;
12994
12995 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
17d85edc
KH
12996 if (x_had_errors_p (dpy))
12997 {
12998 /* This error is perhaps due to insufficient memory on X
12999 server. Let's just ignore it. */
13000 font = NULL;
13001 x_clear_errors (dpy);
13002 }
13003
09c6077f
KH
13004 if (font
13005 && XGetFontProperty (font, XA_FONT, &value))
13006 {
13007 char *name = (char *) XGetAtomName (dpy, (Atom) value);
13008 int len = strlen (name);
01c752b5 13009 char *tmp;
09c6077f 13010
6f6512e8
KH
13011 /* If DXPC (a Differential X Protocol Compressor)
13012 Ver.3.7 is running, XGetAtomName will return null
13013 string. We must avoid such a name. */
13014 if (len == 0)
13015 try_XLoadQueryFont = 0;
13016 else
13017 {
13018 num_fonts = 1;
13019 names = (char **) alloca (sizeof (char *));
13020 /* Some systems only allow alloca assigned to a
13021 simple var. */
13022 tmp = (char *) alloca (len + 1); names[0] = tmp;
13023 bcopy (name, names[0], len + 1);
13024 XFree (name);
13025 }
09c6077f
KH
13026 }
13027 else
13028 try_XLoadQueryFont = 0;
a083fd23
RS
13029
13030 if (font)
13031 XFreeFont (dpy, font);
09c6077f
KH
13032 }
13033
13034 if (!try_XLoadQueryFont)
17d85edc
KH
13035 {
13036 /* We try at least 10 fonts because XListFonts will return
13037 auto-scaled fonts at the head. */
13038 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
13039 &num_fonts);
13040 if (x_had_errors_p (dpy))
13041 {
13042 /* This error is perhaps due to insufficient memory on X
13043 server. Let's just ignore it. */
13044 names = NULL;
13045 x_clear_errors (dpy);
13046 }
13047 }
13048
13049 x_uncatch_errors (dpy, count);
dc43ef94
KH
13050 UNBLOCK_INPUT;
13051
13052 if (names)
13053 {
13054 int i;
dc43ef94
KH
13055
13056 /* Make a list of all the fonts we got back.
13057 Store that in the font cache for the display. */
13058 for (i = 0; i < num_fonts; i++)
13059 {
06a2c219 13060 int width = 0;
dc43ef94 13061 char *p = names[i];
06a2c219
GM
13062 int average_width = -1, dashes = 0;
13063
dc43ef94 13064 /* Count the number of dashes in NAMES[I]. If there are
9c11f79e
GM
13065 14 dashes, and the field value following 12th dash
13066 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
13067 is usually too ugly to be used for editing. Let's
13068 ignore it. */
dc43ef94
KH
13069 while (*p)
13070 if (*p++ == '-')
13071 {
13072 dashes++;
13073 if (dashes == 7) /* PIXEL_SIZE field */
13074 width = atoi (p);
13075 else if (dashes == 12) /* AVERAGE_WIDTH field */
13076 average_width = atoi (p);
13077 }
9c11f79e
GM
13078
13079 if (allow_scalable_fonts_p
13080 || dashes < 14 || average_width != 0)
dc43ef94
KH
13081 {
13082 tem = build_string (names[i]);
13083 if (NILP (Fassoc (tem, list)))
13084 {
13085 if (STRINGP (Vx_pixel_size_width_font_regexp)
f39db7ea
RS
13086 && ((fast_c_string_match_ignore_case
13087 (Vx_pixel_size_width_font_regexp, names[i]))
dc43ef94
KH
13088 >= 0))
13089 /* We can set the value of PIXEL_SIZE to the
b5210ea7 13090 width of this font. */
dc43ef94
KH
13091 list = Fcons (Fcons (tem, make_number (width)), list);
13092 else
13093 /* For the moment, width is not known. */
13094 list = Fcons (Fcons (tem, Qnil), list);
13095 }
13096 }
13097 }
09c6077f
KH
13098 if (!try_XLoadQueryFont)
13099 XFreeFontNames (names);
dc43ef94
KH
13100 }
13101
b5210ea7 13102 /* Now store the result in the cache. */
9c11f79e
GM
13103 XCDR (dpyinfo->name_list_element)
13104 = Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element));
dc43ef94 13105
b5210ea7
KH
13106 label_cached:
13107 if (NILP (list)) continue; /* Try the remaining alternatives. */
dc43ef94 13108
b5210ea7
KH
13109 newlist = second_best = Qnil;
13110 /* Make a list of the fonts that have the right width. */
8e713be6 13111 for (; CONSP (list); list = XCDR (list))
b5210ea7 13112 {
536f4067
RS
13113 int found_size;
13114
8e713be6 13115 tem = XCAR (list);
dc43ef94 13116
8e713be6 13117 if (!CONSP (tem) || NILP (XCAR (tem)))
b5210ea7
KH
13118 continue;
13119 if (!size)
13120 {
8e713be6 13121 newlist = Fcons (XCAR (tem), newlist);
b5210ea7
KH
13122 continue;
13123 }
dc43ef94 13124
8e713be6 13125 if (!INTEGERP (XCDR (tem)))
dc43ef94 13126 {
b5210ea7 13127 /* Since we have not yet known the size of this font, we
9c11f79e 13128 must try slow function call XLoadQueryFont. */
dc43ef94
KH
13129 XFontStruct *thisinfo;
13130
13131 BLOCK_INPUT;
17d85edc 13132 count = x_catch_errors (dpy);
dc43ef94 13133 thisinfo = XLoadQueryFont (dpy,
8e713be6 13134 XSTRING (XCAR (tem))->data);
17d85edc
KH
13135 if (x_had_errors_p (dpy))
13136 {
13137 /* This error is perhaps due to insufficient memory on X
13138 server. Let's just ignore it. */
13139 thisinfo = NULL;
13140 x_clear_errors (dpy);
13141 }
13142 x_uncatch_errors (dpy, count);
dc43ef94
KH
13143 UNBLOCK_INPUT;
13144
13145 if (thisinfo)
13146 {
8e713be6 13147 XCDR (tem)
536f4067
RS
13148 = (thisinfo->min_bounds.width == 0
13149 ? make_number (0)
13150 : make_number (thisinfo->max_bounds.width));
dc43ef94
KH
13151 XFreeFont (dpy, thisinfo);
13152 }
13153 else
b5210ea7 13154 /* For unknown reason, the previous call of XListFont had
06a2c219 13155 returned a font which can't be opened. Record the size
b5210ea7 13156 as 0 not to try to open it again. */
8e713be6 13157 XCDR (tem) = make_number (0);
dc43ef94 13158 }
536f4067 13159
8e713be6 13160 found_size = XINT (XCDR (tem));
536f4067 13161 if (found_size == size)
8e713be6 13162 newlist = Fcons (XCAR (tem), newlist);
536f4067 13163 else if (found_size > 0)
b5210ea7 13164 {
536f4067 13165 if (NILP (second_best))
b5210ea7 13166 second_best = tem;
536f4067
RS
13167 else if (found_size < size)
13168 {
8e713be6
KR
13169 if (XINT (XCDR (second_best)) > size
13170 || XINT (XCDR (second_best)) < found_size)
536f4067
RS
13171 second_best = tem;
13172 }
13173 else
13174 {
8e713be6
KR
13175 if (XINT (XCDR (second_best)) > size
13176 && XINT (XCDR (second_best)) > found_size)
536f4067
RS
13177 second_best = tem;
13178 }
b5210ea7
KH
13179 }
13180 }
13181 if (!NILP (newlist))
13182 break;
13183 else if (!NILP (second_best))
13184 {
8e713be6 13185 newlist = Fcons (XCAR (second_best), Qnil);
b5210ea7 13186 break;
dc43ef94 13187 }
dc43ef94
KH
13188 }
13189
13190 return newlist;
13191}
13192
06a2c219
GM
13193
13194#if GLYPH_DEBUG
13195
13196/* Check that FONT is valid on frame F. It is if it can be found in F's
13197 font table. */
13198
13199static void
13200x_check_font (f, font)
13201 struct frame *f;
13202 XFontStruct *font;
13203{
13204 int i;
13205 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13206
13207 xassert (font != NULL);
13208
13209 for (i = 0; i < dpyinfo->n_fonts; i++)
13210 if (dpyinfo->font_table[i].name
13211 && font == dpyinfo->font_table[i].font)
13212 break;
13213
13214 xassert (i < dpyinfo->n_fonts);
13215}
13216
13217#endif /* GLYPH_DEBUG != 0 */
13218
13219/* Set *W to the minimum width, *H to the minimum font height of FONT.
13220 Note: There are (broken) X fonts out there with invalid XFontStruct
13221 min_bounds contents. For example, handa@etl.go.jp reports that
13222 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
13223 have font->min_bounds.width == 0. */
13224
13225static INLINE void
13226x_font_min_bounds (font, w, h)
13227 XFontStruct *font;
13228 int *w, *h;
13229{
13230 *h = FONT_HEIGHT (font);
13231 *w = font->min_bounds.width;
13232
13233 /* Try to handle the case where FONT->min_bounds has invalid
13234 contents. Since the only font known to have invalid min_bounds
13235 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
13236 if (*w <= 0)
13237 *w = font->max_bounds.width;
13238}
13239
13240
13241/* Compute the smallest character width and smallest font height over
13242 all fonts available on frame F. Set the members smallest_char_width
13243 and smallest_font_height in F's x_display_info structure to
13244 the values computed. Value is non-zero if smallest_font_height or
13245 smallest_char_width become smaller than they were before. */
13246
13247static int
13248x_compute_min_glyph_bounds (f)
13249 struct frame *f;
13250{
13251 int i;
13252 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13253 XFontStruct *font;
13254 int old_width = dpyinfo->smallest_char_width;
13255 int old_height = dpyinfo->smallest_font_height;
13256
13257 dpyinfo->smallest_font_height = 100000;
13258 dpyinfo->smallest_char_width = 100000;
13259
13260 for (i = 0; i < dpyinfo->n_fonts; ++i)
13261 if (dpyinfo->font_table[i].name)
13262 {
13263 struct font_info *fontp = dpyinfo->font_table + i;
13264 int w, h;
13265
13266 font = (XFontStruct *) fontp->font;
13267 xassert (font != (XFontStruct *) ~0);
13268 x_font_min_bounds (font, &w, &h);
13269
13270 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
13271 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
13272 }
13273
13274 xassert (dpyinfo->smallest_char_width > 0
13275 && dpyinfo->smallest_font_height > 0);
13276
13277 return (dpyinfo->n_fonts == 1
13278 || dpyinfo->smallest_char_width < old_width
13279 || dpyinfo->smallest_font_height < old_height);
13280}
13281
13282
dc43ef94
KH
13283/* Load font named FONTNAME of the size SIZE for frame F, and return a
13284 pointer to the structure font_info while allocating it dynamically.
13285 If SIZE is 0, load any size of font.
13286 If loading is failed, return NULL. */
13287
13288struct font_info *
13289x_load_font (f, fontname, size)
13290 struct frame *f;
13291 register char *fontname;
13292 int size;
13293{
13294 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13295 Lisp_Object font_names;
d645aaa4 13296 int count;
dc43ef94
KH
13297
13298 /* Get a list of all the fonts that match this name. Once we
13299 have a list of matching fonts, we compare them against the fonts
13300 we already have by comparing names. */
09c6077f 13301 font_names = x_list_fonts (f, build_string (fontname), size, 1);
dc43ef94
KH
13302
13303 if (!NILP (font_names))
13304 {
13305 Lisp_Object tail;
13306 int i;
13307
13308 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 13309 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
06a2c219
GM
13310 if (dpyinfo->font_table[i].name
13311 && (!strcmp (dpyinfo->font_table[i].name,
8e713be6 13312 XSTRING (XCAR (tail))->data)
06a2c219 13313 || !strcmp (dpyinfo->font_table[i].full_name,
8e713be6 13314 XSTRING (XCAR (tail))->data)))
dc43ef94
KH
13315 return (dpyinfo->font_table + i);
13316 }
13317
13318 /* Load the font and add it to the table. */
13319 {
13320 char *full_name;
13321 XFontStruct *font;
13322 struct font_info *fontp;
13323 unsigned long value;
06a2c219 13324 int i;
dc43ef94 13325
2da424f1
KH
13326 /* If we have found fonts by x_list_font, load one of them. If
13327 not, we still try to load a font by the name given as FONTNAME
13328 because XListFonts (called in x_list_font) of some X server has
13329 a bug of not finding a font even if the font surely exists and
13330 is loadable by XLoadQueryFont. */
e1d6d5b9 13331 if (size > 0 && !NILP (font_names))
8e713be6 13332 fontname = (char *) XSTRING (XCAR (font_names))->data;
dc43ef94
KH
13333
13334 BLOCK_INPUT;
d645aaa4 13335 count = x_catch_errors (FRAME_X_DISPLAY (f));
dc43ef94 13336 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
d645aaa4
KH
13337 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
13338 {
13339 /* This error is perhaps due to insufficient memory on X
13340 server. Let's just ignore it. */
13341 font = NULL;
13342 x_clear_errors (FRAME_X_DISPLAY (f));
13343 }
13344 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
dc43ef94 13345 UNBLOCK_INPUT;
b5210ea7 13346 if (!font)
dc43ef94
KH
13347 return NULL;
13348
06a2c219
GM
13349 /* Find a free slot in the font table. */
13350 for (i = 0; i < dpyinfo->n_fonts; ++i)
13351 if (dpyinfo->font_table[i].name == NULL)
13352 break;
13353
13354 /* If no free slot found, maybe enlarge the font table. */
13355 if (i == dpyinfo->n_fonts
13356 && dpyinfo->n_fonts == dpyinfo->font_table_size)
dc43ef94 13357 {
06a2c219
GM
13358 int sz;
13359 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
13360 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
dc43ef94 13361 dpyinfo->font_table
06a2c219 13362 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
dc43ef94
KH
13363 }
13364
06a2c219
GM
13365 fontp = dpyinfo->font_table + i;
13366 if (i == dpyinfo->n_fonts)
13367 ++dpyinfo->n_fonts;
dc43ef94
KH
13368
13369 /* Now fill in the slots of *FONTP. */
13370 BLOCK_INPUT;
13371 fontp->font = font;
06a2c219 13372 fontp->font_idx = i;
dc43ef94
KH
13373 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
13374 bcopy (fontname, fontp->name, strlen (fontname) + 1);
13375
13376 /* Try to get the full name of FONT. Put it in FULL_NAME. */
13377 full_name = 0;
13378 if (XGetFontProperty (font, XA_FONT, &value))
13379 {
13380 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
13381 char *p = name;
13382 int dashes = 0;
13383
13384 /* Count the number of dashes in the "full name".
13385 If it is too few, this isn't really the font's full name,
13386 so don't use it.
13387 In X11R4, the fonts did not come with their canonical names
13388 stored in them. */
13389 while (*p)
13390 {
13391 if (*p == '-')
13392 dashes++;
13393 p++;
13394 }
13395
13396 if (dashes >= 13)
13397 {
13398 full_name = (char *) xmalloc (p - name + 1);
13399 bcopy (name, full_name, p - name + 1);
13400 }
13401
13402 XFree (name);
13403 }
13404
13405 if (full_name != 0)
13406 fontp->full_name = full_name;
13407 else
13408 fontp->full_name = fontp->name;
13409
13410 fontp->size = font->max_bounds.width;
d5749adb 13411 fontp->height = FONT_HEIGHT (font);
dc43ef94 13412
2da424f1
KH
13413 if (NILP (font_names))
13414 {
13415 /* We come here because of a bug of XListFonts mentioned at
13416 the head of this block. Let's store this information in
13417 the cache for x_list_fonts. */
13418 Lisp_Object lispy_name = build_string (fontname);
13419 Lisp_Object lispy_full_name = build_string (fontp->full_name);
9c11f79e
GM
13420 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
13421 Qnil);
2da424f1 13422
8e713be6 13423 XCDR (dpyinfo->name_list_element)
9c11f79e 13424 = Fcons (Fcons (key,
2da424f1
KH
13425 Fcons (Fcons (lispy_full_name,
13426 make_number (fontp->size)),
13427 Qnil)),
8e713be6 13428 XCDR (dpyinfo->name_list_element));
2da424f1 13429 if (full_name)
9c11f79e
GM
13430 {
13431 key = Fcons (Fcons (lispy_full_name, make_number (256)),
13432 Qnil);
13433 XCDR (dpyinfo->name_list_element)
13434 = Fcons (Fcons (key,
13435 Fcons (Fcons (lispy_full_name,
13436 make_number (fontp->size)),
13437 Qnil)),
13438 XCDR (dpyinfo->name_list_element));
13439 }
2da424f1
KH
13440 }
13441
dc43ef94
KH
13442 /* The slot `encoding' specifies how to map a character
13443 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
ee569018
KH
13444 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
13445 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
8ff102bd 13446 2:0xA020..0xFF7F). For the moment, we don't know which charset
06a2c219 13447 uses this font. So, we set information in fontp->encoding[1]
8ff102bd
RS
13448 which is never used by any charset. If mapping can't be
13449 decided, set FONT_ENCODING_NOT_DECIDED. */
dc43ef94
KH
13450 fontp->encoding[1]
13451 = (font->max_byte1 == 0
13452 /* 1-byte font */
13453 ? (font->min_char_or_byte2 < 0x80
13454 ? (font->max_char_or_byte2 < 0x80
13455 ? 0 /* 0x20..0x7F */
8ff102bd 13456 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
dc43ef94
KH
13457 : 1) /* 0xA0..0xFF */
13458 /* 2-byte font */
13459 : (font->min_byte1 < 0x80
13460 ? (font->max_byte1 < 0x80
13461 ? (font->min_char_or_byte2 < 0x80
13462 ? (font->max_char_or_byte2 < 0x80
13463 ? 0 /* 0x2020..0x7F7F */
8ff102bd 13464 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
dc43ef94 13465 : 3) /* 0x20A0..0x7FFF */
8ff102bd 13466 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
dc43ef94
KH
13467 : (font->min_char_or_byte2 < 0x80
13468 ? (font->max_char_or_byte2 < 0x80
13469 ? 2 /* 0xA020..0xFF7F */
8ff102bd 13470 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
dc43ef94
KH
13471 : 1))); /* 0xA0A0..0xFFFF */
13472
13473 fontp->baseline_offset
13474 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
13475 ? (long) value : 0);
13476 fontp->relative_compose
13477 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
13478 ? (long) value : 0);
f78798df
KH
13479 fontp->default_ascent
13480 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
13481 ? (long) value : 0);
dc43ef94 13482
06a2c219
GM
13483 /* Set global flag fonts_changed_p to non-zero if the font loaded
13484 has a character with a smaller width than any other character
13485 before, or if the font loaded has a smalle>r height than any
13486 other font loaded before. If this happens, it will make a
13487 glyph matrix reallocation necessary. */
13488 fonts_changed_p = x_compute_min_glyph_bounds (f);
dc43ef94 13489 UNBLOCK_INPUT;
dc43ef94
KH
13490 return fontp;
13491 }
13492}
13493
06a2c219
GM
13494
13495/* Return a pointer to struct font_info of a font named FONTNAME for
13496 frame F. If no such font is loaded, return NULL. */
13497
dc43ef94
KH
13498struct font_info *
13499x_query_font (f, fontname)
13500 struct frame *f;
13501 register char *fontname;
13502{
13503 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13504 int i;
13505
13506 for (i = 0; i < dpyinfo->n_fonts; i++)
06a2c219
GM
13507 if (dpyinfo->font_table[i].name
13508 && (!strcmp (dpyinfo->font_table[i].name, fontname)
13509 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
dc43ef94
KH
13510 return (dpyinfo->font_table + i);
13511 return NULL;
13512}
13513
06a2c219
GM
13514
13515/* Find a CCL program for a font specified by FONTP, and set the member
a6582676
KH
13516 `encoder' of the structure. */
13517
13518void
13519x_find_ccl_program (fontp)
13520 struct font_info *fontp;
13521{
a42f54e6 13522 Lisp_Object list, elt;
a6582676 13523
f9b5db02 13524 elt = Qnil;
8e713be6 13525 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
a6582676 13526 {
8e713be6 13527 elt = XCAR (list);
a6582676 13528 if (CONSP (elt)
8e713be6 13529 && STRINGP (XCAR (elt))
9f2feff6
KH
13530 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
13531 >= 0)
13532 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
13533 >= 0)))
a42f54e6
KH
13534 break;
13535 }
f9b5db02 13536
a42f54e6
KH
13537 if (! NILP (list))
13538 {
d27f8ca7
KH
13539 struct ccl_program *ccl
13540 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
a42f54e6 13541
8e713be6 13542 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
a42f54e6
KH
13543 xfree (ccl);
13544 else
13545 fontp->font_encoder = ccl;
a6582676
KH
13546 }
13547}
13548
06a2c219 13549
dc43ef94 13550\f
06a2c219
GM
13551/***********************************************************************
13552 Initialization
13553 ***********************************************************************/
f451eb13 13554
3afe33e7
RS
13555#ifdef USE_X_TOOLKIT
13556static XrmOptionDescRec emacs_options[] = {
13557 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
13558 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
13559
13560 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
13561 XrmoptionSepArg, NULL},
13562 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
13563
13564 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13565 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13566 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13567 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13568 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13569 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
13570 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
13571};
13572#endif /* USE_X_TOOLKIT */
13573
7a13e894
RS
13574static int x_initialized;
13575
29b38361
KH
13576#ifdef MULTI_KBOARD
13577/* Test whether two display-name strings agree up to the dot that separates
13578 the screen number from the server number. */
13579static int
13580same_x_server (name1, name2)
13581 char *name1, *name2;
13582{
13583 int seen_colon = 0;
cf591cc1
RS
13584 unsigned char *system_name = XSTRING (Vsystem_name)->data;
13585 int system_name_length = strlen (system_name);
13586 int length_until_period = 0;
13587
13588 while (system_name[length_until_period] != 0
13589 && system_name[length_until_period] != '.')
13590 length_until_period++;
13591
13592 /* Treat `unix' like an empty host name. */
13593 if (! strncmp (name1, "unix:", 5))
13594 name1 += 4;
13595 if (! strncmp (name2, "unix:", 5))
13596 name2 += 4;
13597 /* Treat this host's name like an empty host name. */
13598 if (! strncmp (name1, system_name, system_name_length)
13599 && name1[system_name_length] == ':')
13600 name1 += system_name_length;
13601 if (! strncmp (name2, system_name, system_name_length)
13602 && name2[system_name_length] == ':')
13603 name2 += system_name_length;
13604 /* Treat this host's domainless name like an empty host name. */
13605 if (! strncmp (name1, system_name, length_until_period)
13606 && name1[length_until_period] == ':')
13607 name1 += length_until_period;
13608 if (! strncmp (name2, system_name, length_until_period)
13609 && name2[length_until_period] == ':')
13610 name2 += length_until_period;
13611
29b38361
KH
13612 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
13613 {
13614 if (*name1 == ':')
13615 seen_colon++;
13616 if (seen_colon && *name1 == '.')
13617 return 1;
13618 }
13619 return (seen_colon
13620 && (*name1 == '.' || *name1 == '\0')
13621 && (*name2 == '.' || *name2 == '\0'));
13622}
13623#endif
13624
334208b7 13625struct x_display_info *
1f8255f2 13626x_term_init (display_name, xrm_option, resource_name)
334208b7 13627 Lisp_Object display_name;
1f8255f2
RS
13628 char *xrm_option;
13629 char *resource_name;
dc6f92b8 13630{
334208b7 13631 int connection;
7a13e894 13632 Display *dpy;
334208b7
RS
13633 struct x_display_info *dpyinfo;
13634 XrmDatabase xrdb;
13635
60439948
KH
13636 BLOCK_INPUT;
13637
7a13e894
RS
13638 if (!x_initialized)
13639 {
13640 x_initialize ();
13641 x_initialized = 1;
13642 }
dc6f92b8 13643
3afe33e7 13644#ifdef USE_X_TOOLKIT
2d7fc7e8
RS
13645 /* weiner@footloose.sps.mot.com reports that this causes
13646 errors with X11R5:
13647 X protocol error: BadAtom (invalid Atom parameter)
13648 on protocol request 18skiloaf.
13649 So let's not use it until R6. */
13650#ifdef HAVE_X11XTR6
bdcd49ba
RS
13651 XtSetLanguageProc (NULL, NULL, NULL);
13652#endif
13653
7f9c7f94
RS
13654 {
13655 int argc = 0;
13656 char *argv[3];
13657
13658 argv[0] = "";
13659 argc = 1;
13660 if (xrm_option)
13661 {
13662 argv[argc++] = "-xrm";
13663 argv[argc++] = xrm_option;
13664 }
13665 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
13666 resource_name, EMACS_CLASS,
13667 emacs_options, XtNumber (emacs_options),
13668 &argc, argv);
39d8bb4d
KH
13669
13670#ifdef HAVE_X11XTR6
10537cb1 13671 /* I think this is to compensate for XtSetLanguageProc. */
71f8198a 13672 fixup_locale ();
39d8bb4d 13673#endif
7f9c7f94 13674 }
3afe33e7
RS
13675
13676#else /* not USE_X_TOOLKIT */
bdcd49ba
RS
13677#ifdef HAVE_X11R5
13678 XSetLocaleModifiers ("");
13679#endif
7a13e894 13680 dpy = XOpenDisplay (XSTRING (display_name)->data);
3afe33e7 13681#endif /* not USE_X_TOOLKIT */
334208b7 13682
7a13e894
RS
13683 /* Detect failure. */
13684 if (dpy == 0)
60439948
KH
13685 {
13686 UNBLOCK_INPUT;
13687 return 0;
13688 }
7a13e894
RS
13689
13690 /* We have definitely succeeded. Record the new connection. */
13691
13692 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
f04e1297 13693 bzero (dpyinfo, sizeof *dpyinfo);
7a13e894 13694
29b38361
KH
13695#ifdef MULTI_KBOARD
13696 {
13697 struct x_display_info *share;
13698 Lisp_Object tail;
13699
13700 for (share = x_display_list, tail = x_display_name_list; share;
8e713be6
KR
13701 share = share->next, tail = XCDR (tail))
13702 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
29b38361
KH
13703 XSTRING (display_name)->data))
13704 break;
13705 if (share)
13706 dpyinfo->kboard = share->kboard;
13707 else
13708 {
13709 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
13710 init_kboard (dpyinfo->kboard);
59e755be
KH
13711 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
13712 {
13713 char *vendor = ServerVendor (dpy);
9b6ed9f3 13714 UNBLOCK_INPUT;
59e755be
KH
13715 dpyinfo->kboard->Vsystem_key_alist
13716 = call1 (Qvendor_specific_keysyms,
13717 build_string (vendor ? vendor : ""));
9b6ed9f3 13718 BLOCK_INPUT;
59e755be
KH
13719 }
13720
29b38361
KH
13721 dpyinfo->kboard->next_kboard = all_kboards;
13722 all_kboards = dpyinfo->kboard;
0ad5446c
KH
13723 /* Don't let the initial kboard remain current longer than necessary.
13724 That would cause problems if a file loaded on startup tries to
06a2c219 13725 prompt in the mini-buffer. */
0ad5446c
KH
13726 if (current_kboard == initial_kboard)
13727 current_kboard = dpyinfo->kboard;
29b38361
KH
13728 }
13729 dpyinfo->kboard->reference_count++;
13730 }
b9737ad3
KH
13731#endif
13732
7a13e894
RS
13733 /* Put this display on the chain. */
13734 dpyinfo->next = x_display_list;
13735 x_display_list = dpyinfo;
13736
13737 /* Put it on x_display_name_list as well, to keep them parallel. */
13738 x_display_name_list = Fcons (Fcons (display_name, Qnil),
13739 x_display_name_list);
8e713be6 13740 dpyinfo->name_list_element = XCAR (x_display_name_list);
7a13e894
RS
13741
13742 dpyinfo->display = dpy;
dc6f92b8 13743
dc6f92b8 13744#if 0
7a13e894 13745 XSetAfterFunction (x_current_display, x_trace_wire);
c118dd06 13746#endif /* ! 0 */
7a13e894
RS
13747
13748 dpyinfo->x_id_name
fc932ac6
RS
13749 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
13750 + STRING_BYTES (XSTRING (Vsystem_name))
7a13e894
RS
13751 + 2);
13752 sprintf (dpyinfo->x_id_name, "%s@%s",
13753 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
28430d3c
JB
13754
13755 /* Figure out which modifier bits mean what. */
334208b7 13756 x_find_modifier_meanings (dpyinfo);
f451eb13 13757
ab648270 13758 /* Get the scroll bar cursor. */
7a13e894 13759 dpyinfo->vertical_scroll_bar_cursor
334208b7 13760 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
f451eb13 13761
334208b7
RS
13762 xrdb = x_load_resources (dpyinfo->display, xrm_option,
13763 resource_name, EMACS_CLASS);
13764#ifdef HAVE_XRMSETDATABASE
13765 XrmSetDatabase (dpyinfo->display, xrdb);
13766#else
13767 dpyinfo->display->db = xrdb;
13768#endif
547d9db8 13769 /* Put the rdb where we can find it in a way that works on
7a13e894
RS
13770 all versions. */
13771 dpyinfo->xrdb = xrdb;
334208b7
RS
13772
13773 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
13774 DefaultScreen (dpyinfo->display));
5ff67d81 13775 select_visual (dpyinfo);
43bd1b2b 13776 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
334208b7
RS
13777 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
13778 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
13779 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
13780 dpyinfo->grabbed = 0;
13781 dpyinfo->reference_count = 0;
13782 dpyinfo->icon_bitmap_id = -1;
06a2c219 13783 dpyinfo->font_table = NULL;
7a13e894
RS
13784 dpyinfo->n_fonts = 0;
13785 dpyinfo->font_table_size = 0;
13786 dpyinfo->bitmaps = 0;
13787 dpyinfo->bitmaps_size = 0;
13788 dpyinfo->bitmaps_last = 0;
13789 dpyinfo->scratch_cursor_gc = 0;
13790 dpyinfo->mouse_face_mouse_frame = 0;
13791 dpyinfo->mouse_face_deferred_gc = 0;
13792 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
13793 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
06a2c219 13794 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
7a13e894 13795 dpyinfo->mouse_face_window = Qnil;
0a61c667 13796 dpyinfo->mouse_face_overlay = Qnil;
7a13e894
RS
13797 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
13798 dpyinfo->mouse_face_defer = 0;
0f941935
KH
13799 dpyinfo->x_focus_frame = 0;
13800 dpyinfo->x_focus_event_frame = 0;
13801 dpyinfo->x_highlight_frame = 0;
06a2c219 13802 dpyinfo->image_cache = make_image_cache ();
334208b7 13803
43bd1b2b 13804 /* See if a private colormap is requested. */
5ff67d81
GM
13805 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
13806 {
13807 if (dpyinfo->visual->class == PseudoColor)
13808 {
13809 Lisp_Object value;
13810 value = display_x_get_resource (dpyinfo,
13811 build_string ("privateColormap"),
13812 build_string ("PrivateColormap"),
13813 Qnil, Qnil);
13814 if (STRINGP (value)
13815 && (!strcmp (XSTRING (value)->data, "true")
13816 || !strcmp (XSTRING (value)->data, "on")))
13817 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
13818 }
43bd1b2b 13819 }
5ff67d81
GM
13820 else
13821 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
13822 dpyinfo->visual, AllocNone);
43bd1b2b 13823
06a2c219
GM
13824 {
13825 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
13826 double pixels = DisplayHeight (dpyinfo->display, screen_number);
13827 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
13828 dpyinfo->resy = pixels * 25.4 / mm;
13829 pixels = DisplayWidth (dpyinfo->display, screen_number);
13830 mm = DisplayWidthMM (dpyinfo->display, screen_number);
13831 dpyinfo->resx = pixels * 25.4 / mm;
13832 }
13833
334208b7
RS
13834 dpyinfo->Xatom_wm_protocols
13835 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
13836 dpyinfo->Xatom_wm_take_focus
13837 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
13838 dpyinfo->Xatom_wm_save_yourself
13839 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
13840 dpyinfo->Xatom_wm_delete_window
13841 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
13842 dpyinfo->Xatom_wm_change_state
13843 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
13844 dpyinfo->Xatom_wm_configure_denied
13845 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
13846 dpyinfo->Xatom_wm_window_moved
13847 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
13848 dpyinfo->Xatom_editres
13849 = XInternAtom (dpyinfo->display, "Editres", False);
13850 dpyinfo->Xatom_CLIPBOARD
13851 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
13852 dpyinfo->Xatom_TIMESTAMP
13853 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
13854 dpyinfo->Xatom_TEXT
13855 = XInternAtom (dpyinfo->display, "TEXT", False);
dc43ef94
KH
13856 dpyinfo->Xatom_COMPOUND_TEXT
13857 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
334208b7
RS
13858 dpyinfo->Xatom_DELETE
13859 = XInternAtom (dpyinfo->display, "DELETE", False);
13860 dpyinfo->Xatom_MULTIPLE
13861 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
13862 dpyinfo->Xatom_INCR
13863 = XInternAtom (dpyinfo->display, "INCR", False);
13864 dpyinfo->Xatom_EMACS_TMP
13865 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
13866 dpyinfo->Xatom_TARGETS
13867 = XInternAtom (dpyinfo->display, "TARGETS", False);
13868 dpyinfo->Xatom_NULL
13869 = XInternAtom (dpyinfo->display, "NULL", False);
13870 dpyinfo->Xatom_ATOM_PAIR
13871 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
dc43ef94
KH
13872 /* For properties of font. */
13873 dpyinfo->Xatom_PIXEL_SIZE
13874 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
13875 dpyinfo->Xatom_MULE_BASELINE_OFFSET
13876 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
13877 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
13878 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
f78798df
KH
13879 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
13880 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
334208b7 13881
06a2c219
GM
13882 /* Ghostscript support. */
13883 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
13884 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
13885
13886 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
13887 False);
13888
547d9db8
KH
13889 dpyinfo->cut_buffers_initialized = 0;
13890
334208b7
RS
13891 connection = ConnectionNumber (dpyinfo->display);
13892 dpyinfo->connection = connection;
13893
dc43ef94 13894 {
5d7cc324
RS
13895 char null_bits[1];
13896
13897 null_bits[0] = 0x00;
dc43ef94
KH
13898
13899 dpyinfo->null_pixel
13900 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13901 null_bits, 1, 1, (long) 0, (long) 0,
13902 1);
13903 }
13904
06a2c219
GM
13905 {
13906 extern int gray_bitmap_width, gray_bitmap_height;
10659c77 13907 extern char *gray_bitmap_bits;
06a2c219
GM
13908 dpyinfo->gray
13909 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13910 gray_bitmap_bits,
13911 gray_bitmap_width, gray_bitmap_height,
13912 (unsigned long) 1, (unsigned long) 0, 1);
13913 }
13914
f5d11644
GM
13915#ifdef HAVE_X_I18N
13916 xim_initialize (dpyinfo, resource_name);
13917#endif
13918
87485d6f
MW
13919#ifdef subprocesses
13920 /* This is only needed for distinguishing keyboard and process input. */
334208b7 13921 if (connection != 0)
7a13e894 13922 add_keyboard_wait_descriptor (connection);
87485d6f 13923#endif
6d4238f3 13924
041b69ac 13925#ifndef F_SETOWN_BUG
dc6f92b8 13926#ifdef F_SETOWN
dc6f92b8 13927#ifdef F_SETOWN_SOCK_NEG
61c3ce62 13928 /* stdin is a socket here */
334208b7 13929 fcntl (connection, F_SETOWN, -getpid ());
c118dd06 13930#else /* ! defined (F_SETOWN_SOCK_NEG) */
334208b7 13931 fcntl (connection, F_SETOWN, getpid ());
c118dd06
JB
13932#endif /* ! defined (F_SETOWN_SOCK_NEG) */
13933#endif /* ! defined (F_SETOWN) */
041b69ac 13934#endif /* F_SETOWN_BUG */
dc6f92b8
JB
13935
13936#ifdef SIGIO
eee20f6a
KH
13937 if (interrupt_input)
13938 init_sigio (connection);
c118dd06 13939#endif /* ! defined (SIGIO) */
dc6f92b8 13940
51b592fb 13941#ifdef USE_LUCID
f8c39f51 13942#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
51b592fb
RS
13943 /* Make sure that we have a valid font for dialog boxes
13944 so that Xt does not crash. */
13945 {
13946 Display *dpy = dpyinfo->display;
13947 XrmValue d, fr, to;
13948 Font font;
e99db5a1 13949 int count;
51b592fb
RS
13950
13951 d.addr = (XPointer)&dpy;
13952 d.size = sizeof (Display *);
13953 fr.addr = XtDefaultFont;
13954 fr.size = sizeof (XtDefaultFont);
13955 to.size = sizeof (Font *);
13956 to.addr = (XPointer)&font;
e99db5a1 13957 count = x_catch_errors (dpy);
51b592fb
RS
13958 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
13959 abort ();
13960 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
13961 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
e99db5a1 13962 x_uncatch_errors (dpy, count);
51b592fb
RS
13963 }
13964#endif
f8c39f51 13965#endif
51b592fb 13966
34e23e5a
GM
13967 /* See if we should run in synchronous mode. This is useful
13968 for debugging X code. */
13969 {
13970 Lisp_Object value;
13971 value = display_x_get_resource (dpyinfo,
13972 build_string ("synchronous"),
13973 build_string ("Synchronous"),
13974 Qnil, Qnil);
13975 if (STRINGP (value)
13976 && (!strcmp (XSTRING (value)->data, "true")
13977 || !strcmp (XSTRING (value)->data, "on")))
13978 XSynchronize (dpyinfo->display, True);
13979 }
13980
60439948
KH
13981 UNBLOCK_INPUT;
13982
7a13e894
RS
13983 return dpyinfo;
13984}
13985\f
13986/* Get rid of display DPYINFO, assuming all frames are already gone,
13987 and without sending any more commands to the X server. */
dc6f92b8 13988
7a13e894
RS
13989void
13990x_delete_display (dpyinfo)
13991 struct x_display_info *dpyinfo;
13992{
13993 delete_keyboard_wait_descriptor (dpyinfo->connection);
13994
13995 /* Discard this display from x_display_name_list and x_display_list.
13996 We can't use Fdelq because that can quit. */
13997 if (! NILP (x_display_name_list)
8e713be6
KR
13998 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
13999 x_display_name_list = XCDR (x_display_name_list);
7a13e894
RS
14000 else
14001 {
14002 Lisp_Object tail;
14003
14004 tail = x_display_name_list;
8e713be6 14005 while (CONSP (tail) && CONSP (XCDR (tail)))
7a13e894 14006 {
bffcfca9 14007 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
7a13e894 14008 {
8e713be6 14009 XCDR (tail) = XCDR (XCDR (tail));
7a13e894
RS
14010 break;
14011 }
8e713be6 14012 tail = XCDR (tail);
7a13e894
RS
14013 }
14014 }
14015
9bda743f
GM
14016 if (next_noop_dpyinfo == dpyinfo)
14017 next_noop_dpyinfo = dpyinfo->next;
14018
7a13e894
RS
14019 if (x_display_list == dpyinfo)
14020 x_display_list = dpyinfo->next;
7f9c7f94
RS
14021 else
14022 {
14023 struct x_display_info *tail;
7a13e894 14024
7f9c7f94
RS
14025 for (tail = x_display_list; tail; tail = tail->next)
14026 if (tail->next == dpyinfo)
14027 tail->next = tail->next->next;
14028 }
7a13e894 14029
0d777288
RS
14030#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
14031#ifndef AIX /* On AIX, XCloseDisplay calls this. */
7f9c7f94
RS
14032 XrmDestroyDatabase (dpyinfo->xrdb);
14033#endif
0d777288 14034#endif
29b38361
KH
14035#ifdef MULTI_KBOARD
14036 if (--dpyinfo->kboard->reference_count == 0)
39f79001 14037 delete_kboard (dpyinfo->kboard);
b9737ad3 14038#endif
f5d11644
GM
14039#ifdef HAVE_X_I18N
14040 if (dpyinfo->xim)
14041 xim_close_dpy (dpyinfo);
14042#endif
14043
b9737ad3
KH
14044 xfree (dpyinfo->font_table);
14045 xfree (dpyinfo->x_id_name);
f04e1297 14046 xfree (dpyinfo->color_cells);
b9737ad3 14047 xfree (dpyinfo);
7a13e894 14048}
f04e1297 14049
7a13e894
RS
14050\f
14051/* Set up use of X before we make the first connection. */
14052
06a2c219
GM
14053static struct redisplay_interface x_redisplay_interface =
14054{
14055 x_produce_glyphs,
14056 x_write_glyphs,
14057 x_insert_glyphs,
14058 x_clear_end_of_line,
14059 x_scroll_run,
14060 x_after_update_window_line,
14061 x_update_window_begin,
14062 x_update_window_end,
14063 XTcursor_to,
14064 x_flush,
71b8321e 14065 x_clear_mouse_face,
66ac4b0e
GM
14066 x_get_glyph_overhangs,
14067 x_fix_overlapping_area
06a2c219
GM
14068};
14069
dfcf069d 14070void
7a13e894
RS
14071x_initialize ()
14072{
06a2c219
GM
14073 rif = &x_redisplay_interface;
14074
14075 clear_frame_hook = x_clear_frame;
14076 ins_del_lines_hook = x_ins_del_lines;
14077 change_line_highlight_hook = x_change_line_highlight;
14078 delete_glyphs_hook = x_delete_glyphs;
dc6f92b8
JB
14079 ring_bell_hook = XTring_bell;
14080 reset_terminal_modes_hook = XTreset_terminal_modes;
14081 set_terminal_modes_hook = XTset_terminal_modes;
06a2c219
GM
14082 update_begin_hook = x_update_begin;
14083 update_end_hook = x_update_end;
dc6f92b8
JB
14084 set_terminal_window_hook = XTset_terminal_window;
14085 read_socket_hook = XTread_socket;
b8009dd1 14086 frame_up_to_date_hook = XTframe_up_to_date;
dc6f92b8 14087 reassert_line_highlight_hook = XTreassert_line_highlight;
90e65f07 14088 mouse_position_hook = XTmouse_position;
f451eb13 14089 frame_rehighlight_hook = XTframe_rehighlight;
dbc4e1c1 14090 frame_raise_lower_hook = XTframe_raise_lower;
ab648270
JB
14091 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
14092 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
14093 redeem_scroll_bar_hook = XTredeem_scroll_bar;
14094 judge_scroll_bars_hook = XTjudge_scroll_bars;
06a2c219 14095 estimate_mode_line_height_hook = x_estimate_mode_line_height;
58769bee 14096
f676886a 14097 scroll_region_ok = 1; /* we'll scroll partial frames */
9017309f 14098 char_ins_del_ok = 1;
dc6f92b8
JB
14099 line_ins_del_ok = 1; /* we'll just blt 'em */
14100 fast_clear_end_of_line = 1; /* X does this well */
58769bee 14101 memory_below_frame = 0; /* we don't remember what scrolls
dc6f92b8
JB
14102 off the bottom */
14103 baud_rate = 19200;
14104
7a13e894 14105 x_noop_count = 0;
9ea173e8 14106 last_tool_bar_item = -1;
06a2c219
GM
14107 any_help_event_p = 0;
14108
b30b24cb
RS
14109 /* Try to use interrupt input; if we can't, then start polling. */
14110 Fset_input_mode (Qt, Qnil, Qt, Qnil);
14111
7f9c7f94
RS
14112#ifdef USE_X_TOOLKIT
14113 XtToolkitInitialize ();
14114 Xt_app_con = XtCreateApplicationContext ();
665881ad 14115 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
bffcfca9
GM
14116
14117 /* Install an asynchronous timer that processes Xt timeout events
14118 every 0.1s. This is necessary because some widget sets use
14119 timeouts internally, for example the LessTif menu bar, or the
14120 Xaw3d scroll bar. When Xt timouts aren't processed, these
14121 widgets don't behave normally. */
14122 {
14123 EMACS_TIME interval;
14124 EMACS_SET_SECS_USECS (interval, 0, 100000);
14125 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
14126 }
db74249b 14127#endif
bffcfca9 14128
eccc05db 14129#ifdef USE_TOOLKIT_SCROLL_BARS
ec18280f
SM
14130 xaw3d_arrow_scroll = False;
14131 xaw3d_pick_top = True;
7f9c7f94
RS
14132#endif
14133
58769bee 14134 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 14135 original error handler. */
e99db5a1 14136 XSetErrorHandler (x_error_handler);
334208b7 14137 XSetIOErrorHandler (x_io_error_quitter);
dc6f92b8 14138
06a2c219 14139 /* Disable Window Change signals; they are handled by X events. */
dc6f92b8
JB
14140#ifdef SIGWINCH
14141 signal (SIGWINCH, SIG_DFL);
c118dd06 14142#endif /* ! defined (SIGWINCH) */
dc6f92b8 14143
92e2441b 14144 signal (SIGPIPE, x_connection_signal);
dc6f92b8 14145}
55123275 14146
06a2c219 14147
55123275
JB
14148void
14149syms_of_xterm ()
14150{
e99db5a1
RS
14151 staticpro (&x_error_message_string);
14152 x_error_message_string = Qnil;
14153
7a13e894
RS
14154 staticpro (&x_display_name_list);
14155 x_display_name_list = Qnil;
334208b7 14156
ab648270 14157 staticpro (&last_mouse_scroll_bar);
e53cb100 14158 last_mouse_scroll_bar = Qnil;
59e755be
KH
14159
14160 staticpro (&Qvendor_specific_keysyms);
14161 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
2237cac9
RS
14162
14163 staticpro (&last_mouse_press_frame);
14164 last_mouse_press_frame = Qnil;
06a2c219 14165
06a2c219 14166 help_echo = Qnil;
be010514
GM
14167 staticpro (&help_echo);
14168 help_echo_object = Qnil;
14169 staticpro (&help_echo_object);
7cea38bc
GM
14170 help_echo_window = Qnil;
14171 staticpro (&help_echo_window);
06a2c219 14172 previous_help_echo = Qnil;
be010514
GM
14173 staticpro (&previous_help_echo);
14174 help_echo_pos = -1;
06a2c219
GM
14175
14176 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
14177 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
14178For example, if a block cursor is over a tab, it will be drawn as\n\
14179wide as that tab on the display.");
14180 x_stretch_cursor_p = 0;
14181
5bf04520
GM
14182 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
14183 "What X toolkit scroll bars Emacs uses.\n\
14184A value of nil means Emacs doesn't use X toolkit scroll bars.\n\
14185Otherwise, value is a symbol describing the X toolkit.");
eccc05db 14186#ifdef USE_TOOLKIT_SCROLL_BARS
5bf04520
GM
14187#ifdef USE_MOTIF
14188 Vx_toolkit_scroll_bars = intern ("motif");
14189#elif defined HAVE_XAW3D
14190 Vx_toolkit_scroll_bars = intern ("xaw3d");
14191#else
14192 Vx_toolkit_scroll_bars = intern ("xaw");
14193#endif
06a2c219 14194#else
5bf04520 14195 Vx_toolkit_scroll_bars = Qnil;
06a2c219
GM
14196#endif
14197
06a2c219
GM
14198 staticpro (&last_mouse_motion_frame);
14199 last_mouse_motion_frame = Qnil;
55123275 14200}
6cf0ae86
RS
14201
14202#endif /* not HAVE_X_WINDOWS */