(save_or_restore_current_matrices): Function removed.
[bpt/emacs.git] / src / xterm.c
CommitLineData
dc6f92b8 1/* X Communication module for terminals which understand the X protocol.
edf36fe6 2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000, 2001
06a2c219 3 Free Software Foundation, Inc.
dc6f92b8
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
4746118a 9the Free Software Foundation; either version 2, or (at your option)
dc6f92b8
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
dc6f92b8 21
06a2c219 22/* New display code by Gerd Moellmann <gerd@gnu.org>. */
3afe33e7
RS
23/* Xt features made by Fred Pierresteguy. */
24
68c45bf0
PE
25#include <config.h>
26
039440c4 27/* On 4.3 these lose if they come after xterm.h. */
039440c4 28/* Putting these at the beginning seems to be standard for other .c files. */
039440c4
RS
29#include <signal.h>
30
4846819e
RS
31#include <stdio.h>
32
dc6f92b8
JB
33#ifdef HAVE_X_WINDOWS
34
35#include "lisp.h"
9ac0d9e0 36#include "blockinput.h"
dc6f92b8 37
ae79c227
AS
38/* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40#include "syssignal.h"
41
dc6f92b8
JB
42/* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44#include "xterm.h"
f451eb13 45#include <X11/cursorfont.h>
dc6f92b8
JB
46
47/* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49#ifndef makedev
50#include <sys/types.h>
c118dd06 51#endif /* makedev */
dc6f92b8 52
6df54671 53#ifdef BSD_SYSTEM
dc6f92b8 54#include <sys/ioctl.h>
6df54671 55#endif /* ! defined (BSD_SYSTEM) */
dc6f92b8 56
2d368234 57#include "systty.h"
3a2712f9 58#include "systime.h"
dc6f92b8 59
b8009dd1 60#ifndef INCLUDED_FCNTL
dc6f92b8 61#include <fcntl.h>
b8009dd1 62#endif
dc6f92b8
JB
63#include <ctype.h>
64#include <errno.h>
65#include <setjmp.h>
66#include <sys/stat.h>
a0a7635f
RS
67/* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
68/* #include <sys/param.h> */
dc6f92b8 69
dc43ef94 70#include "charset.h"
379b5ac0 71#include "coding.h"
dc43ef94 72#include "ccl.h"
7a13e894 73#include "frame.h"
dc6f92b8 74#include "dispextern.h"
ee569018 75#include "fontset.h"
dc6f92b8
JB
76#include "termhooks.h"
77#include "termopts.h"
78#include "termchar.h"
79#if 0
80#include "sink.h"
81#include "sinkmask.h"
c118dd06 82#endif /* ! 0 */
dc6f92b8 83#include "gnu.h"
dc6f92b8 84#include "disptab.h"
dc6f92b8 85#include "buffer.h"
f451eb13 86#include "window.h"
3b2fa4e6 87#include "keyboard.h"
bde7c500 88#include "intervals.h"
dfcf069d 89#include "process.h"
bffcfca9 90#include "atimer.h"
dc6f92b8 91
d2bd6bc4
RS
92#ifdef USE_X_TOOLKIT
93#include <X11/Shell.h>
94#endif
95
06a2c219
GM
96#ifdef HAVE_SYS_TIME_H
97#include <sys/time.h>
98#endif
99#ifdef HAVE_UNISTD_H
100#include <unistd.h>
101#endif
102
3afe33e7 103#ifdef USE_X_TOOLKIT
06a2c219 104
952291d9
GM
105extern void free_frame_menubar P_ ((struct frame *));
106extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
107 int));
06a2c219 108
0fdff6bb
RS
109#if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
110#define HACK_EDITRES
111extern void _XEditResCheckMessages ();
112#endif /* not NO_EDITRES */
06a2c219
GM
113
114/* Include toolkit specific headers for the scroll bar widget. */
115
116#ifdef USE_TOOLKIT_SCROLL_BARS
117#if defined USE_MOTIF
118#include <Xm/Xm.h> /* for LESSTIF_VERSION */
119#include <Xm/ScrollBar.h>
120#include <Xm/ScrollBarP.h>
ec18280f
SM
121#else /* !USE_MOTIF i.e. use Xaw */
122
123#ifdef HAVE_XAW3D
06a2c219 124#include <X11/Xaw3d/Simple.h>
06a2c219
GM
125#include <X11/Xaw3d/Scrollbar.h>
126#define ARROW_SCROLLBAR
127#include <X11/Xaw3d/ScrollbarP.h>
ec18280f
SM
128#else /* !HAVE_XAW3D */
129#include <X11/Xaw/Simple.h>
130#include <X11/Xaw/Scrollbar.h>
131#endif /* !HAVE_XAW3D */
132#ifndef XtNpickTop
133#define XtNpickTop "pickTop"
134#endif /* !XtNpickTop */
135#endif /* !USE_MOTIF */
06a2c219
GM
136#endif /* USE_TOOLKIT_SCROLL_BARS */
137
3afe33e7
RS
138#endif /* USE_X_TOOLKIT */
139
b849c413
RS
140#ifndef USE_X_TOOLKIT
141#define x_any_window_to_frame x_window_to_frame
5627c40e 142#define x_top_window_to_frame x_window_to_frame
b849c413
RS
143#endif
144
546e6d5b 145#ifdef USE_X_TOOLKIT
d067ea8b 146#include "widget.h"
546e6d5b
RS
147#ifndef XtNinitialState
148#define XtNinitialState "initialState"
149#endif
150#endif
151
e4b68333 152#ifndef min
06a2c219 153#define min(a,b) ((a) < (b) ? (a) : (b))
e4b68333
RS
154#endif
155#ifndef max
06a2c219
GM
156#define max(a,b) ((a) > (b) ? (a) : (b))
157#endif
158
159#define abs(x) ((x) < 0 ? -(x) : (x))
160
161#define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
162
163\f
164/* Bitmaps for truncated lines. */
165
166enum bitmap_type
167{
168 NO_BITMAP,
169 LEFT_TRUNCATION_BITMAP,
170 RIGHT_TRUNCATION_BITMAP,
171 OVERLAY_ARROW_BITMAP,
172 CONTINUED_LINE_BITMAP,
173 CONTINUATION_LINE_BITMAP,
174 ZV_LINE_BITMAP
175};
176
177/* Bitmap drawn to indicate lines not displaying text if
178 `indicate-empty-lines' is non-nil. */
179
180#define zv_width 8
181#define zv_height 8
182static unsigned char zv_bits[] = {
183 0x00, 0x00, 0x1e, 0x1e, 0x1e, 0x1e, 0x00, 0x00};
184
185/* An arrow like this: `<-'. */
186
187#define left_width 8
188#define left_height 8
189static unsigned char left_bits[] = {
190 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
191
110859fc
GM
192/* Right truncation arrow bitmap `->'. */
193
194#define right_width 8
195#define right_height 8
196static unsigned char right_bits[] = {
197 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
198
06a2c219
GM
199/* Marker for continued lines. */
200
201#define continued_width 8
202#define continued_height 8
203static unsigned char continued_bits[] = {
110859fc
GM
204 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
205
206/* Marker for continuation lines. */
06a2c219
GM
207
208#define continuation_width 8
209#define continuation_height 8
210static unsigned char continuation_bits[] = {
110859fc 211 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
06a2c219 212
110859fc 213/* Overlay arrow bitmap. */
06a2c219 214
110859fc
GM
215#if 0
216/* A bomb. */
06a2c219
GM
217#define ov_width 8
218#define ov_height 8
219static unsigned char ov_bits[] = {
220 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
06a2c219 221#else
110859fc 222/* A triangular arrow. */
06a2c219
GM
223#define ov_width 8
224#define ov_height 8
225static unsigned char ov_bits[] = {
110859fc
GM
226 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
227
e4b68333 228#endif
06a2c219
GM
229
230extern Lisp_Object Qhelp_echo;
231
69388238 232\f
5bf04520 233/* Non-nil means Emacs uses toolkit scroll bars. */
06a2c219 234
5bf04520 235Lisp_Object Vx_toolkit_scroll_bars;
06a2c219
GM
236
237/* If a string, XTread_socket generates an event to display that string.
238 (The display is done in read_char.) */
239
240static Lisp_Object help_echo;
7cea38bc 241static Lisp_Object help_echo_window;
be010514
GM
242static Lisp_Object help_echo_object;
243static int help_echo_pos;
06a2c219
GM
244
245/* Temporary variable for XTread_socket. */
246
247static Lisp_Object previous_help_echo;
248
249/* Non-zero means that a HELP_EVENT has been generated since Emacs
250 start. */
251
252static int any_help_event_p;
253
254/* Non-zero means draw block and hollow cursor as wide as the glyph
255 under it. For example, if a block cursor is over a tab, it will be
256 drawn as wide as that tab on the display. */
257
258int x_stretch_cursor_p;
259
260/* This is a chain of structures for all the X displays currently in
261 use. */
262
334208b7 263struct x_display_info *x_display_list;
dc6f92b8 264
06a2c219
GM
265/* This is a list of cons cells, each of the form (NAME
266 . FONT-LIST-CACHE), one for each element of x_display_list and in
267 the same order. NAME is the name of the frame. FONT-LIST-CACHE
268 records previous values returned by x-list-fonts. */
269
7a13e894 270Lisp_Object x_display_name_list;
f451eb13 271
987d2ad1 272/* Frame being updated by update_frame. This is declared in term.c.
06a2c219
GM
273 This is set by update_begin and looked at by all the XT functions.
274 It is zero while not inside an update. In that case, the XT
275 functions assume that `selected_frame' is the frame to apply to. */
276
d0386f2a 277extern struct frame *updating_frame;
dc6f92b8 278
dfcf069d 279extern int waiting_for_input;
0e81d8cd 280
06a2c219
GM
281/* This is a frame waiting to be auto-raised, within XTread_socket. */
282
0134a210
RS
283struct frame *pending_autoraise_frame;
284
7f9c7f94
RS
285#ifdef USE_X_TOOLKIT
286/* The application context for Xt use. */
287XtAppContext Xt_app_con;
06a2c219
GM
288static String Xt_default_resources[] = {0};
289#endif /* USE_X_TOOLKIT */
665881ad 290
06a2c219
GM
291/* Nominal cursor position -- where to draw output.
292 HPOS and VPOS are window relative glyph matrix coordinates.
293 X and Y are window relative pixel coordinates. */
dc6f92b8 294
06a2c219 295struct cursor_pos output_cursor;
dc6f92b8 296
bffcfca9
GM
297/* Non-zero means user is interacting with a toolkit scroll bar. */
298
299static int toolkit_scroll_bar_interaction;
dc6f92b8 300
69388238
RS
301/* Mouse movement.
302
06a2c219 303 Formerly, we used PointerMotionHintMask (in standard_event_mask)
f5bb65ec
RS
304 so that we would have to call XQueryPointer after each MotionNotify
305 event to ask for another such event. However, this made mouse tracking
306 slow, and there was a bug that made it eventually stop.
307
308 Simply asking for MotionNotify all the time seems to work better.
309
69388238
RS
310 In order to avoid asking for motion events and then throwing most
311 of them away or busy-polling the server for mouse positions, we ask
312 the server for pointer motion hints. This means that we get only
313 one event per group of mouse movements. "Groups" are delimited by
314 other kinds of events (focus changes and button clicks, for
315 example), or by XQueryPointer calls; when one of these happens, we
316 get another MotionNotify event the next time the mouse moves. This
317 is at least as efficient as getting motion events when mouse
318 tracking is on, and I suspect only negligibly worse when tracking
f5bb65ec 319 is off. */
69388238
RS
320
321/* Where the mouse was last time we reported a mouse event. */
69388238 322
06a2c219
GM
323FRAME_PTR last_mouse_frame;
324static XRectangle last_mouse_glyph;
2237cac9
RS
325static Lisp_Object last_mouse_press_frame;
326
69388238
RS
327/* The scroll bar in which the last X motion event occurred.
328
06a2c219
GM
329 If the last X motion event occurred in a scroll bar, we set this so
330 XTmouse_position can know whether to report a scroll bar motion or
69388238
RS
331 an ordinary motion.
332
06a2c219
GM
333 If the last X motion event didn't occur in a scroll bar, we set
334 this to Qnil, to tell XTmouse_position to return an ordinary motion
335 event. */
336
69388238
RS
337static Lisp_Object last_mouse_scroll_bar;
338
69388238
RS
339/* This is a hack. We would really prefer that XTmouse_position would
340 return the time associated with the position it returns, but there
06a2c219 341 doesn't seem to be any way to wrest the time-stamp from the server
69388238
RS
342 along with the position query. So, we just keep track of the time
343 of the last movement we received, and return that in hopes that
344 it's somewhat accurate. */
06a2c219 345
69388238
RS
346static Time last_mouse_movement_time;
347
06a2c219
GM
348/* Incremented by XTread_socket whenever it really tries to read
349 events. */
350
c0a04927
RS
351#ifdef __STDC__
352static int volatile input_signal_count;
353#else
354static int input_signal_count;
355#endif
356
7a13e894 357/* Used locally within XTread_socket. */
06a2c219 358
7a13e894 359static int x_noop_count;
dc6f92b8 360
7a13e894 361/* Initial values of argv and argc. */
06a2c219 362
7a13e894
RS
363extern char **initial_argv;
364extern int initial_argc;
dc6f92b8 365
7a13e894 366extern Lisp_Object Vcommand_line_args, Vsystem_name;
dc6f92b8 367
06a2c219 368/* Tells if a window manager is present or not. */
7a13e894
RS
369
370extern Lisp_Object Vx_no_window_manager;
dc6f92b8 371
c2df547c 372extern Lisp_Object Qface, Qmouse_face;
b8009dd1 373
dc6f92b8
JB
374extern int errno;
375
dfeccd2d 376/* A mask of extra modifier bits to put into every keyboard char. */
06a2c219 377
64bb1782
RS
378extern int extra_keyboard_modifiers;
379
59e755be
KH
380static Lisp_Object Qvendor_specific_keysyms;
381
952291d9
GM
382extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
383extern Lisp_Object x_icon_type P_ ((struct frame *));
c32cdd9a 384
7a13e894 385
06a2c219
GM
386/* Enumeration for overriding/changing the face to use for drawing
387 glyphs in x_draw_glyphs. */
388
389enum draw_glyphs_face
390{
391 DRAW_NORMAL_TEXT,
392 DRAW_INVERSE_VIDEO,
393 DRAW_CURSOR,
394 DRAW_MOUSE_FACE,
395 DRAW_IMAGE_RAISED,
396 DRAW_IMAGE_SUNKEN
397};
398
f04e1297 399static const XColor *x_color_cells P_ ((struct frame *, int *));
71b8321e 400static void x_update_window_end P_ ((struct window *, int, int));
06a2c219
GM
401static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
402void x_delete_display P_ ((struct x_display_info *));
403static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
404 unsigned));
405static int fast_find_position P_ ((struct window *, int, int *, int *,
406 int *, int *));
407static void set_output_cursor P_ ((struct cursor_pos *));
408static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
409 int *, int *, int *));
410static void note_mode_line_highlight P_ ((struct window *, int, int));
06a2c219 411static void note_mouse_highlight P_ ((struct frame *, int, int));
9ea173e8
GM
412static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
413static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
06a2c219
GM
414static void show_mouse_face P_ ((struct x_display_info *,
415 enum draw_glyphs_face));
416static int x_io_error_quitter P_ ((Display *));
417int x_catch_errors P_ ((Display *));
418void x_uncatch_errors P_ ((Display *, int));
419void x_lower_frame P_ ((struct frame *));
420void x_scroll_bar_clear P_ ((struct frame *));
421int x_had_errors_p P_ ((Display *));
422void x_wm_set_size_hint P_ ((struct frame *, long, int));
423void x_raise_frame P_ ((struct frame *));
424void x_set_window_size P_ ((struct frame *, int, int, int));
425void x_wm_set_window_state P_ ((struct frame *, int));
426void x_wm_set_icon_pixmap P_ ((struct frame *, int));
427void x_initialize P_ ((void));
428static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
429static int x_compute_min_glyph_bounds P_ ((struct frame *));
430static void x_draw_phys_cursor_glyph P_ ((struct window *,
431 struct glyph_row *,
432 enum draw_glyphs_face));
433static void x_update_end P_ ((struct frame *));
434static void XTframe_up_to_date P_ ((struct frame *));
435static void XTreassert_line_highlight P_ ((int, int));
436static void x_change_line_highlight P_ ((int, int, int, int));
437static void XTset_terminal_modes P_ ((void));
438static void XTreset_terminal_modes P_ ((void));
439static void XTcursor_to P_ ((int, int, int, int));
440static void x_write_glyphs P_ ((struct glyph *, int));
441static void x_clear_end_of_line P_ ((int));
442static void x_clear_frame P_ ((void));
443static void x_clear_cursor P_ ((struct window *));
444static void frame_highlight P_ ((struct frame *));
445static void frame_unhighlight P_ ((struct frame *));
446static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
447static void XTframe_rehighlight P_ ((struct frame *));
448static void x_frame_rehighlight P_ ((struct x_display_info *));
449static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
f02d8aa0 450static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
06a2c219
GM
451static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
452 XRectangle *));
453static void expose_frame P_ ((struct frame *, int, int, int, int));
454static void expose_window_tree P_ ((struct window *, XRectangle *));
455static void expose_window P_ ((struct window *, XRectangle *));
456static void expose_area P_ ((struct window *, struct glyph_row *,
457 XRectangle *, enum glyph_row_area));
458static void expose_line P_ ((struct window *, struct glyph_row *,
459 XRectangle *));
460static void x_update_cursor_in_window_tree P_ ((struct window *, int));
461static void x_update_window_cursor P_ ((struct window *, int));
462static void x_erase_phys_cursor P_ ((struct window *));
463void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
464static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
465 enum bitmap_type));
466
467static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
468 GC, int));
469static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
470static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
471static void note_overwritten_text_cursor P_ ((struct window *, int, int));
472static void x_flush P_ ((struct frame *f));
952291d9
GM
473static void x_update_begin P_ ((struct frame *));
474static void x_update_window_begin P_ ((struct window *));
475static void x_draw_vertical_border P_ ((struct window *));
476static void x_after_update_window_line P_ ((struct glyph_row *));
477static INLINE void take_vertical_position_into_account P_ ((struct it *));
478static void x_produce_stretch_glyph P_ ((struct it *));
479
06a2c219
GM
480
481/* Flush display of frame F, or of all frames if F is null. */
482
483static void
484x_flush (f)
485 struct frame *f;
486{
487 BLOCK_INPUT;
488 if (f == NULL)
489 {
490 Lisp_Object rest, frame;
491 FOR_EACH_FRAME (rest, frame)
492 x_flush (XFRAME (frame));
493 }
494 else if (FRAME_X_P (f))
495 XFlush (FRAME_X_DISPLAY (f));
496 UNBLOCK_INPUT;
497}
498
dc6f92b8 499
06a2c219
GM
500/* Remove calls to XFlush by defining XFlush to an empty replacement.
501 Calls to XFlush should be unnecessary because the X output buffer
502 is flushed automatically as needed by calls to XPending,
503 XNextEvent, or XWindowEvent according to the XFlush man page.
504 XTread_socket calls XPending. Removing XFlush improves
505 performance. */
506
507#define XFlush(DISPLAY) (void) 0
b8009dd1 508
334208b7 509\f
06a2c219
GM
510/***********************************************************************
511 Debugging
512 ***********************************************************************/
513
9382638d 514#if 0
06a2c219
GM
515
516/* This is a function useful for recording debugging information about
517 the sequence of occurrences in this file. */
9382638d
KH
518
519struct record
520{
521 char *locus;
522 int type;
523};
524
525struct record event_record[100];
526
527int event_record_index;
528
529record_event (locus, type)
530 char *locus;
531 int type;
532{
533 if (event_record_index == sizeof (event_record) / sizeof (struct record))
534 event_record_index = 0;
535
536 event_record[event_record_index].locus = locus;
537 event_record[event_record_index].type = type;
538 event_record_index++;
539}
540
541#endif /* 0 */
06a2c219
GM
542
543
9382638d 544\f
334208b7
RS
545/* Return the struct x_display_info corresponding to DPY. */
546
547struct x_display_info *
548x_display_info_for_display (dpy)
549 Display *dpy;
550{
551 struct x_display_info *dpyinfo;
552
553 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
554 if (dpyinfo->display == dpy)
555 return dpyinfo;
16bd92ea 556
334208b7
RS
557 return 0;
558}
f451eb13 559
06a2c219
GM
560
561\f
562/***********************************************************************
563 Starting and ending an update
564 ***********************************************************************/
565
566/* Start an update of frame F. This function is installed as a hook
567 for update_begin, i.e. it is called when update_begin is called.
568 This function is called prior to calls to x_update_window_begin for
569 each window being updated. Currently, there is nothing to do here
570 because all interesting stuff is done on a window basis. */
dc6f92b8 571
dfcf069d 572static void
06a2c219 573x_update_begin (f)
f676886a 574 struct frame *f;
58769bee 575{
06a2c219
GM
576 /* Nothing to do. */
577}
dc6f92b8 578
dc6f92b8 579
06a2c219
GM
580/* Start update of window W. Set the global variable updated_window
581 to the window being updated and set output_cursor to the cursor
582 position of W. */
dc6f92b8 583
06a2c219
GM
584static void
585x_update_window_begin (w)
586 struct window *w;
587{
588 struct frame *f = XFRAME (WINDOW_FRAME (w));
589 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
590
591 updated_window = w;
592 set_output_cursor (&w->cursor);
b8009dd1 593
06a2c219 594 BLOCK_INPUT;
d1bc4182 595
06a2c219 596 if (f == display_info->mouse_face_mouse_frame)
b8009dd1 597 {
514e4681 598 /* Don't do highlighting for mouse motion during the update. */
06a2c219 599 display_info->mouse_face_defer = 1;
37c2c98b 600
06a2c219
GM
601 /* If F needs to be redrawn, simply forget about any prior mouse
602 highlighting. */
9f67f20b 603 if (FRAME_GARBAGED_P (f))
06a2c219
GM
604 display_info->mouse_face_window = Qnil;
605
64f26cf5
GM
606#if 0 /* Rows in a current matrix containing glyphs in mouse-face have
607 their mouse_face_p flag set, which means that they are always
608 unequal to rows in a desired matrix which never have that
609 flag set. So, rows containing mouse-face glyphs are never
610 scrolled, and we don't have to switch the mouse highlight off
611 here to prevent it from being scrolled. */
612
06a2c219
GM
613 /* Can we tell that this update does not affect the window
614 where the mouse highlight is? If so, no need to turn off.
615 Likewise, don't do anything if the frame is garbaged;
616 in that case, the frame's current matrix that we would use
617 is all wrong, and we will redisplay that line anyway. */
618 if (!NILP (display_info->mouse_face_window)
619 && w == XWINDOW (display_info->mouse_face_window))
514e4681 620 {
06a2c219 621 int i;
514e4681 622
06a2c219
GM
623 for (i = 0; i < w->desired_matrix->nrows; ++i)
624 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
514e4681
RS
625 break;
626
06a2c219
GM
627 if (i < w->desired_matrix->nrows)
628 clear_mouse_face (display_info);
514e4681 629 }
64f26cf5 630#endif /* 0 */
b8009dd1 631 }
6ccf47d1 632
dc6f92b8
JB
633 UNBLOCK_INPUT;
634}
635
06a2c219
GM
636
637/* Draw a vertical window border to the right of window W if W doesn't
638 have vertical scroll bars. */
639
dfcf069d 640static void
06a2c219
GM
641x_draw_vertical_border (w)
642 struct window *w;
58769bee 643{
06a2c219
GM
644 struct frame *f = XFRAME (WINDOW_FRAME (w));
645
646 /* Redraw borders between horizontally adjacent windows. Don't
647 do it for frames with vertical scroll bars because either the
648 right scroll bar of a window, or the left scroll bar of its
649 neighbor will suffice as a border. */
650 if (!WINDOW_RIGHTMOST_P (w)
651 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
652 {
653 int x0, x1, y0, y1;
dc6f92b8 654
06a2c219 655 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
110859fc 656 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
657 y1 -= 1;
658
659 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
660 f->output_data.x->normal_gc, x1, y0, x1, y1);
661 }
662}
663
664
71b8321e
GM
665/* End update of window W (which is equal to updated_window).
666
667 Draw vertical borders between horizontally adjacent windows, and
668 display W's cursor if CURSOR_ON_P is non-zero.
669
670 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
671 glyphs in mouse-face were overwritten. In that case we have to
672 make sure that the mouse-highlight is properly redrawn.
673
674 W may be a menu bar pseudo-window in case we don't have X toolkit
675 support. Such windows don't have a cursor, so don't display it
676 here. */
06a2c219
GM
677
678static void
71b8321e 679x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
06a2c219 680 struct window *w;
71b8321e 681 int cursor_on_p, mouse_face_overwritten_p;
06a2c219
GM
682{
683 if (!w->pseudo_window_p)
684 {
71b8321e
GM
685 struct x_display_info *dpyinfo
686 = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
687
06a2c219 688 BLOCK_INPUT;
71b8321e
GM
689
690 /* If a row with mouse-face was overwritten, arrange for
691 XTframe_up_to_date to redisplay the mouse highlight. */
692 if (mouse_face_overwritten_p)
693 {
694 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
695 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
696 dpyinfo->mouse_face_window = Qnil;
697 }
698
06a2c219
GM
699 if (cursor_on_p)
700 x_display_and_set_cursor (w, 1, output_cursor.hpos,
701 output_cursor.vpos,
702 output_cursor.x, output_cursor.y);
71b8321e 703
06a2c219
GM
704 x_draw_vertical_border (w);
705 UNBLOCK_INPUT;
706 }
707
708 updated_window = NULL;
709}
dc6f92b8 710
dc6f92b8 711
06a2c219
GM
712/* End update of frame F. This function is installed as a hook in
713 update_end. */
714
715static void
716x_update_end (f)
717 struct frame *f;
718{
719 /* Mouse highlight may be displayed again. */
aa8bff2e 720 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
b8009dd1 721
06a2c219 722 BLOCK_INPUT;
334208b7 723 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
724 UNBLOCK_INPUT;
725}
b8009dd1 726
06a2c219
GM
727
728/* This function is called from various places in xdisp.c whenever a
729 complete update has been performed. The global variable
730 updated_window is not available here. */
b8009dd1 731
dfcf069d 732static void
b8009dd1 733XTframe_up_to_date (f)
06a2c219 734 struct frame *f;
b8009dd1 735{
06a2c219 736 if (FRAME_X_P (f))
514e4681 737 {
06a2c219 738 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
71b8321e 739
06a2c219
GM
740 if (dpyinfo->mouse_face_deferred_gc
741 || f == dpyinfo->mouse_face_mouse_frame)
742 {
743 BLOCK_INPUT;
744 if (dpyinfo->mouse_face_mouse_frame)
745 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
746 dpyinfo->mouse_face_mouse_x,
747 dpyinfo->mouse_face_mouse_y);
748 dpyinfo->mouse_face_deferred_gc = 0;
749 UNBLOCK_INPUT;
750 }
514e4681 751 }
b8009dd1 752}
06a2c219
GM
753
754
755/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
756 arrow bitmaps, or clear the areas where they would be displayed
757 before DESIRED_ROW is made current. The window being updated is
758 found in updated_window. This function It is called from
759 update_window_line only if it is known that there are differences
760 between bitmaps to be drawn between current row and DESIRED_ROW. */
761
762static void
763x_after_update_window_line (desired_row)
764 struct glyph_row *desired_row;
765{
766 struct window *w = updated_window;
767
768 xassert (w);
769
770 if (!desired_row->mode_line_p && !w->pseudo_window_p)
771 {
772 BLOCK_INPUT;
773 x_draw_row_bitmaps (w, desired_row);
774
775 /* When a window has disappeared, make sure that no rest of
776 full-width rows stays visible in the internal border. */
777 if (windows_or_buffers_changed)
778 {
779 struct frame *f = XFRAME (w->frame);
780 int width = FRAME_INTERNAL_BORDER_WIDTH (f);
781 int height = desired_row->visible_height;
110859fc
GM
782 int x = (window_box_right (w, -1)
783 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
06a2c219
GM
784 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
785
786 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
787 x, y, width, height, False);
788 }
789
790 UNBLOCK_INPUT;
791 }
792}
793
794
795/* Draw the bitmap WHICH in one of the areas to the left or right of
796 window W. ROW is the glyph row for which to display the bitmap; it
797 determines the vertical position at which the bitmap has to be
798 drawn. */
799
800static void
801x_draw_bitmap (w, row, which)
802 struct window *w;
803 struct glyph_row *row;
804 enum bitmap_type which;
805{
806 struct frame *f = XFRAME (WINDOW_FRAME (w));
807 Display *display = FRAME_X_DISPLAY (f);
808 Window window = FRAME_X_WINDOW (f);
809 int x, y, wd, h, dy;
810 unsigned char *bits;
811 Pixmap pixmap;
812 GC gc = f->output_data.x->normal_gc;
813 struct face *face;
814 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
815
816 /* Must clip because of partially visible lines. */
817 x_clip_to_row (w, row, gc, 1);
818
819 switch (which)
820 {
821 case LEFT_TRUNCATION_BITMAP:
822 wd = left_width;
823 h = left_height;
824 bits = left_bits;
825 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
826 - wd
110859fc 827 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
828 break;
829
830 case OVERLAY_ARROW_BITMAP:
831 wd = left_width;
832 h = left_height;
833 bits = ov_bits;
834 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
835 - wd
110859fc 836 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
837 break;
838
839 case RIGHT_TRUNCATION_BITMAP:
840 wd = right_width;
841 h = right_height;
842 bits = right_bits;
843 x = window_box_right (w, -1);
110859fc 844 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
845 break;
846
847 case CONTINUED_LINE_BITMAP:
848 wd = right_width;
849 h = right_height;
850 bits = continued_bits;
851 x = window_box_right (w, -1);
110859fc 852 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
853 break;
854
855 case CONTINUATION_LINE_BITMAP:
856 wd = continuation_width;
857 h = continuation_height;
858 bits = continuation_bits;
859 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
860 - wd
110859fc 861 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
862 break;
863
864 case ZV_LINE_BITMAP:
865 wd = zv_width;
866 h = zv_height;
867 bits = zv_bits;
868 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
869 - wd
110859fc 870 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
871 break;
872
873 default:
874 abort ();
875 }
876
877 /* Convert to frame coordinates. Set dy to the offset in the row to
878 start drawing the bitmap. */
879 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
880 dy = (row->height - h) / 2;
881
882 /* Draw the bitmap. I believe these small pixmaps can be cached
883 by the server. */
884 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
885 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
886 face->foreground,
887 face->background, depth);
888 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
889 XFreePixmap (display, pixmap);
890 XSetClipMask (display, gc, None);
891}
892
893
894/* Draw flags bitmaps for glyph row ROW on window W. Call this
895 function with input blocked. */
896
897static void
898x_draw_row_bitmaps (w, row)
899 struct window *w;
900 struct glyph_row *row;
901{
902 struct frame *f = XFRAME (w->frame);
903 enum bitmap_type bitmap;
904 struct face *face;
045dee35 905 int header_line_height = -1;
06a2c219
GM
906
907 xassert (interrupt_input_blocked);
908
909 /* If row is completely invisible, because of vscrolling, we
910 don't have to draw anything. */
911 if (row->visible_height <= 0)
912 return;
913
914 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
915 PREPARE_FACE_FOR_DISPLAY (f, face);
916
917 /* Decide which bitmap to draw at the left side. */
918 if (row->overlay_arrow_p)
919 bitmap = OVERLAY_ARROW_BITMAP;
920 else if (row->truncated_on_left_p)
921 bitmap = LEFT_TRUNCATION_BITMAP;
922 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
923 bitmap = CONTINUATION_LINE_BITMAP;
924 else if (row->indicate_empty_line_p)
925 bitmap = ZV_LINE_BITMAP;
926 else
927 bitmap = NO_BITMAP;
928
929 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
930 the flags area. */
931 if (bitmap == NO_BITMAP
110859fc 932 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
933 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
934 {
935 /* If W has a vertical border to its left, don't draw over it. */
936 int border = ((XFASTINT (w->left) > 0
937 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
938 ? 1 : 0);
939 int left = window_box_left (w, -1);
940
045dee35
GM
941 if (header_line_height < 0)
942 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
943
944 /* In case the same realized face is used for bitmap areas and
945 for something displayed in the text (e.g. face `region' on
946 mono-displays, the fill style may have been changed to
947 FillSolid in x_draw_glyph_string_background. */
948 if (face->stipple)
949 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
950 else
951 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
952
06a2c219
GM
953 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
954 face->gc,
955 (left
110859fc 956 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219 957 + border),
045dee35 958 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 959 row->y)),
110859fc 960 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
06a2c219 961 row->visible_height);
dcd08bfb
GM
962 if (!face->stipple)
963 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
964 }
965
966 /* Draw the left bitmap. */
967 if (bitmap != NO_BITMAP)
968 x_draw_bitmap (w, row, bitmap);
969
970 /* Decide which bitmap to draw at the right side. */
971 if (row->truncated_on_right_p)
972 bitmap = RIGHT_TRUNCATION_BITMAP;
973 else if (row->continued_p)
974 bitmap = CONTINUED_LINE_BITMAP;
975 else
976 bitmap = NO_BITMAP;
977
978 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
979 the flags area. */
980 if (bitmap == NO_BITMAP
110859fc 981 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
982 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
983 {
984 int right = window_box_right (w, -1);
985
045dee35
GM
986 if (header_line_height < 0)
987 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
988
989 /* In case the same realized face is used for bitmap areas and
990 for something displayed in the text (e.g. face `region' on
991 mono-displays, the fill style may have been changed to
992 FillSolid in x_draw_glyph_string_background. */
993 if (face->stipple)
994 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
995 else
996 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
06a2c219
GM
997 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
998 face->gc,
999 right,
045dee35 1000 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 1001 row->y)),
110859fc 1002 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
06a2c219 1003 row->visible_height);
dcd08bfb
GM
1004 if (!face->stipple)
1005 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
1006 }
1007
1008 /* Draw the right bitmap. */
1009 if (bitmap != NO_BITMAP)
1010 x_draw_bitmap (w, row, bitmap);
1011}
1012
dc6f92b8 1013\f
06a2c219
GM
1014/***********************************************************************
1015 Line Highlighting
1016 ***********************************************************************/
dc6f92b8 1017
06a2c219
GM
1018/* External interface to control of standout mode. Not used for X
1019 frames. Aborts when called. */
1020
1021static void
dc6f92b8
JB
1022XTreassert_line_highlight (new, vpos)
1023 int new, vpos;
1024{
06a2c219 1025 abort ();
dc6f92b8
JB
1026}
1027
06a2c219
GM
1028
1029/* Call this when about to modify line at position VPOS and change
1030 whether it is highlighted. Not used for X frames. Aborts when
1031 called. */
dc6f92b8 1032
dfcf069d 1033static void
06a2c219
GM
1034x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1035 int new_highlight, vpos, y, first_unused_hpos;
dc6f92b8 1036{
06a2c219 1037 abort ();
dc6f92b8
JB
1038}
1039
06a2c219
GM
1040
1041/* This is called when starting Emacs and when restarting after
1042 suspend. When starting Emacs, no X window is mapped. And nothing
1043 must be done to Emacs's own window if it is suspended (though that
1044 rarely happens). */
dc6f92b8 1045
dfcf069d 1046static void
dc6f92b8
JB
1047XTset_terminal_modes ()
1048{
1049}
1050
06a2c219
GM
1051/* This is called when exiting or suspending Emacs. Exiting will make
1052 the X-windows go away, and suspending requires no action. */
dc6f92b8 1053
dfcf069d 1054static void
dc6f92b8
JB
1055XTreset_terminal_modes ()
1056{
dc6f92b8 1057}
06a2c219
GM
1058
1059
dc6f92b8 1060\f
06a2c219
GM
1061/***********************************************************************
1062 Output Cursor
1063 ***********************************************************************/
1064
1065/* Set the global variable output_cursor to CURSOR. All cursor
1066 positions are relative to updated_window. */
dc6f92b8 1067
dfcf069d 1068static void
06a2c219
GM
1069set_output_cursor (cursor)
1070 struct cursor_pos *cursor;
dc6f92b8 1071{
06a2c219
GM
1072 output_cursor.hpos = cursor->hpos;
1073 output_cursor.vpos = cursor->vpos;
1074 output_cursor.x = cursor->x;
1075 output_cursor.y = cursor->y;
1076}
1077
1078
1079/* Set a nominal cursor position.
dc6f92b8 1080
06a2c219
GM
1081 HPOS and VPOS are column/row positions in a window glyph matrix. X
1082 and Y are window text area relative pixel positions.
1083
1084 If this is done during an update, updated_window will contain the
1085 window that is being updated and the position is the future output
1086 cursor position for that window. If updated_window is null, use
1087 selected_window and display the cursor at the given position. */
1088
1089static void
1090XTcursor_to (vpos, hpos, y, x)
1091 int vpos, hpos, y, x;
1092{
1093 struct window *w;
1094
1095 /* If updated_window is not set, work on selected_window. */
1096 if (updated_window)
1097 w = updated_window;
1098 else
1099 w = XWINDOW (selected_window);
dbcb258a 1100
06a2c219
GM
1101 /* Set the output cursor. */
1102 output_cursor.hpos = hpos;
1103 output_cursor.vpos = vpos;
1104 output_cursor.x = x;
1105 output_cursor.y = y;
dc6f92b8 1106
06a2c219
GM
1107 /* If not called as part of an update, really display the cursor.
1108 This will also set the cursor position of W. */
1109 if (updated_window == NULL)
dc6f92b8
JB
1110 {
1111 BLOCK_INPUT;
06a2c219 1112 x_display_cursor (w, 1, hpos, vpos, x, y);
b86bd3dd 1113 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
dc6f92b8
JB
1114 UNBLOCK_INPUT;
1115 }
1116}
dc43ef94 1117
06a2c219
GM
1118
1119\f
1120/***********************************************************************
1121 Display Iterator
1122 ***********************************************************************/
1123
1124/* Function prototypes of this page. */
1125
1126static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1127 struct glyph *,
ee569018
KH
1128 XChar2b *,
1129 int *));
06a2c219
GM
1130static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1131 int, XChar2b *, int));
1132static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1133static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1134static void x_append_glyph P_ ((struct it *));
b4192550 1135static void x_append_composite_glyph P_ ((struct it *));
06a2c219
GM
1136static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1137 int, int, double));
1138static void x_produce_glyphs P_ ((struct it *));
06a2c219 1139static void x_produce_image_glyph P_ ((struct it *it));
ee569018
KH
1140
1141
e2ef8ee6
GM
1142/* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1143 is not contained in the font. */
dc43ef94 1144
06a2c219 1145static INLINE XCharStruct *
ee569018 1146x_per_char_metric (font, char2b)
06a2c219
GM
1147 XFontStruct *font;
1148 XChar2b *char2b;
1149{
1150 /* The result metric information. */
1151 XCharStruct *pcm = NULL;
dc6f92b8 1152
06a2c219 1153 xassert (font && char2b);
dc6f92b8 1154
06a2c219 1155 if (font->per_char != NULL)
dc6f92b8 1156 {
06a2c219 1157 if (font->min_byte1 == 0 && font->max_byte1 == 0)
dc43ef94 1158 {
06a2c219
GM
1159 /* min_char_or_byte2 specifies the linear character index
1160 corresponding to the first element of the per_char array,
1161 max_char_or_byte2 is the index of the last character. A
1162 character with non-zero CHAR2B->byte1 is not in the font.
1163 A character with byte2 less than min_char_or_byte2 or
1164 greater max_char_or_byte2 is not in the font. */
1165 if (char2b->byte1 == 0
1166 && char2b->byte2 >= font->min_char_or_byte2
1167 && char2b->byte2 <= font->max_char_or_byte2)
1168 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
dc43ef94 1169 }
06a2c219 1170 else
dc6f92b8 1171 {
06a2c219
GM
1172 /* If either min_byte1 or max_byte1 are nonzero, both
1173 min_char_or_byte2 and max_char_or_byte2 are less than
1174 256, and the 2-byte character index values corresponding
1175 to the per_char array element N (counting from 0) are:
1176
1177 byte1 = N/D + min_byte1
1178 byte2 = N\D + min_char_or_byte2
1179
1180 where:
1181
1182 D = max_char_or_byte2 - min_char_or_byte2 + 1
1183 / = integer division
1184 \ = integer modulus */
1185 if (char2b->byte1 >= font->min_byte1
1186 && char2b->byte1 <= font->max_byte1
1187 && char2b->byte2 >= font->min_char_or_byte2
1188 && char2b->byte2 <= font->max_char_or_byte2)
1189 {
1190 pcm = (font->per_char
1191 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1192 * (char2b->byte1 - font->min_byte1))
1193 + (char2b->byte2 - font->min_char_or_byte2));
1194 }
dc6f92b8 1195 }
06a2c219
GM
1196 }
1197 else
1198 {
1199 /* If the per_char pointer is null, all glyphs between the first
1200 and last character indexes inclusive have the same
1201 information, as given by both min_bounds and max_bounds. */
1202 if (char2b->byte2 >= font->min_char_or_byte2
1203 && char2b->byte2 <= font->max_char_or_byte2)
1204 pcm = &font->max_bounds;
1205 }
dc6f92b8 1206
ee569018 1207 return ((pcm == NULL
3e71d8f2 1208 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
ee569018 1209 ? NULL : pcm);
06a2c219 1210}
b73b6aaf 1211
57b03282 1212
06a2c219
GM
1213/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1214 the two-byte form of C. Encoding is returned in *CHAR2B. */
dc43ef94 1215
06a2c219
GM
1216static INLINE void
1217x_encode_char (c, char2b, font_info)
1218 int c;
1219 XChar2b *char2b;
1220 struct font_info *font_info;
1221{
1222 int charset = CHAR_CHARSET (c);
1223 XFontStruct *font = font_info->font;
1224
1225 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1226 This may be either a program in a special encoder language or a
1227 fixed encoding. */
1228 if (font_info->font_encoder)
1229 {
1230 /* It's a program. */
1231 struct ccl_program *ccl = font_info->font_encoder;
1232
1233 if (CHARSET_DIMENSION (charset) == 1)
1234 {
1235 ccl->reg[0] = charset;
1236 ccl->reg[1] = char2b->byte2;
1237 }
1238 else
1239 {
1240 ccl->reg[0] = charset;
1241 ccl->reg[1] = char2b->byte1;
1242 ccl->reg[2] = char2b->byte2;
1243 }
1244
1245 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1246
1247 /* We assume that MSBs are appropriately set/reset by CCL
1248 program. */
1249 if (font->max_byte1 == 0) /* 1-byte font */
ee569018 1250 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
06a2c219
GM
1251 else
1252 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1253 }
1254 else if (font_info->encoding[charset])
1255 {
1256 /* Fixed encoding scheme. See fontset.h for the meaning of the
1257 encoding numbers. */
1258 int enc = font_info->encoding[charset];
1259
1260 if ((enc == 1 || enc == 2)
1261 && CHARSET_DIMENSION (charset) == 2)
1262 char2b->byte1 |= 0x80;
1263
1264 if (enc == 1 || enc == 3)
1265 char2b->byte2 |= 0x80;
1266 }
1267}
1268
1269
1270/* Get face and two-byte form of character C in face FACE_ID on frame
1271 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1272 means we want to display multibyte text. Value is a pointer to a
1273 realized face that is ready for display. */
1274
1275static INLINE struct face *
1276x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1277 struct frame *f;
1278 int c, face_id;
1279 XChar2b *char2b;
1280 int multibyte_p;
1281{
1282 struct face *face = FACE_FROM_ID (f, face_id);
1283
1284 if (!multibyte_p)
1285 {
1286 /* Unibyte case. We don't have to encode, but we have to make
1287 sure to use a face suitable for unibyte. */
1288 char2b->byte1 = 0;
1289 char2b->byte2 = c;
ee569018
KH
1290 face_id = FACE_FOR_CHAR (f, face, c);
1291 face = FACE_FROM_ID (f, face_id);
06a2c219
GM
1292 }
1293 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1294 {
1295 /* Case of ASCII in a face known to fit ASCII. */
1296 char2b->byte1 = 0;
1297 char2b->byte2 = c;
1298 }
1299 else
1300 {
1301 int c1, c2, charset;
1302
1303 /* Split characters into bytes. If c2 is -1 afterwards, C is
1304 really a one-byte character so that byte1 is zero. */
1305 SPLIT_CHAR (c, charset, c1, c2);
1306 if (c2 > 0)
1307 char2b->byte1 = c1, char2b->byte2 = c2;
1308 else
1309 char2b->byte1 = 0, char2b->byte2 = c1;
1310
06a2c219 1311 /* Maybe encode the character in *CHAR2B. */
ee569018 1312 if (face->font != NULL)
06a2c219
GM
1313 {
1314 struct font_info *font_info
1315 = FONT_INFO_FROM_ID (f, face->font_info_id);
1316 if (font_info)
ee569018 1317 x_encode_char (c, char2b, font_info);
06a2c219
GM
1318 }
1319 }
1320
1321 /* Make sure X resources of the face are allocated. */
1322 xassert (face != NULL);
1323 PREPARE_FACE_FOR_DISPLAY (f, face);
1324
1325 return face;
1326}
1327
1328
1329/* Get face and two-byte form of character glyph GLYPH on frame F.
43d120d8 1330 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
06a2c219
GM
1331 a pointer to a realized face that is ready for display. */
1332
1333static INLINE struct face *
ee569018 1334x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
06a2c219
GM
1335 struct frame *f;
1336 struct glyph *glyph;
1337 XChar2b *char2b;
ee569018 1338 int *two_byte_p;
06a2c219
GM
1339{
1340 struct face *face;
1341
1342 xassert (glyph->type == CHAR_GLYPH);
43d120d8 1343 face = FACE_FROM_ID (f, glyph->face_id);
06a2c219 1344
ee569018
KH
1345 if (two_byte_p)
1346 *two_byte_p = 0;
1347
06a2c219
GM
1348 if (!glyph->multibyte_p)
1349 {
1350 /* Unibyte case. We don't have to encode, but we have to make
1351 sure to use a face suitable for unibyte. */
1352 char2b->byte1 = 0;
43d120d8 1353 char2b->byte2 = glyph->u.ch;
06a2c219 1354 }
43d120d8
KH
1355 else if (glyph->u.ch < 128
1356 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
06a2c219
GM
1357 {
1358 /* Case of ASCII in a face known to fit ASCII. */
1359 char2b->byte1 = 0;
43d120d8 1360 char2b->byte2 = glyph->u.ch;
06a2c219
GM
1361 }
1362 else
1363 {
1364 int c1, c2, charset;
1365
1366 /* Split characters into bytes. If c2 is -1 afterwards, C is
1367 really a one-byte character so that byte1 is zero. */
43d120d8 1368 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
06a2c219
GM
1369 if (c2 > 0)
1370 char2b->byte1 = c1, char2b->byte2 = c2;
1371 else
1372 char2b->byte1 = 0, char2b->byte2 = c1;
1373
1374 /* Maybe encode the character in *CHAR2B. */
1375 if (charset != CHARSET_ASCII)
1376 {
1377 struct font_info *font_info
1378 = FONT_INFO_FROM_ID (f, face->font_info_id);
1379 if (font_info)
1380 {
43d120d8 1381 x_encode_char (glyph->u.ch, char2b, font_info);
ee569018
KH
1382 if (two_byte_p)
1383 *two_byte_p
1384 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
06a2c219
GM
1385 }
1386 }
1387 }
1388
1389 /* Make sure X resources of the face are allocated. */
1390 xassert (face != NULL);
1391 PREPARE_FACE_FOR_DISPLAY (f, face);
1392 return face;
1393}
1394
1395
1396/* Store one glyph for IT->char_to_display in IT->glyph_row.
1397 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1398
1399static INLINE void
1400x_append_glyph (it)
1401 struct it *it;
1402{
1403 struct glyph *glyph;
1404 enum glyph_row_area area = it->area;
1405
1406 xassert (it->glyph_row);
1407 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1408
1409 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1410 if (glyph < it->glyph_row->glyphs[area + 1])
1411 {
06a2c219
GM
1412 glyph->charpos = CHARPOS (it->position);
1413 glyph->object = it->object;
88d75730 1414 glyph->pixel_width = it->pixel_width;
06a2c219 1415 glyph->voffset = it->voffset;
88d75730 1416 glyph->type = CHAR_GLYPH;
06a2c219 1417 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1418 glyph->left_box_line_p = it->start_of_box_run_p;
1419 glyph->right_box_line_p = it->end_of_box_run_p;
66ac4b0e
GM
1420 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1421 || it->phys_descent > it->descent);
88d75730 1422 glyph->padding_p = 0;
ee569018 1423 glyph->glyph_not_available_p = it->glyph_not_available_p;
88d75730
GM
1424 glyph->face_id = it->face_id;
1425 glyph->u.ch = it->char_to_display;
06a2c219
GM
1426 ++it->glyph_row->used[area];
1427 }
1428}
1429
b4192550
KH
1430/* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1431 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1432
1433static INLINE void
1434x_append_composite_glyph (it)
1435 struct it *it;
1436{
1437 struct glyph *glyph;
1438 enum glyph_row_area area = it->area;
1439
1440 xassert (it->glyph_row);
1441
1442 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1443 if (glyph < it->glyph_row->glyphs[area + 1])
1444 {
b4192550
KH
1445 glyph->charpos = CHARPOS (it->position);
1446 glyph->object = it->object;
88d75730 1447 glyph->pixel_width = it->pixel_width;
b4192550 1448 glyph->voffset = it->voffset;
88d75730 1449 glyph->type = COMPOSITE_GLYPH;
b4192550 1450 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1451 glyph->left_box_line_p = it->start_of_box_run_p;
1452 glyph->right_box_line_p = it->end_of_box_run_p;
b4192550
KH
1453 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1454 || it->phys_descent > it->descent);
88d75730
GM
1455 glyph->padding_p = 0;
1456 glyph->glyph_not_available_p = 0;
1457 glyph->face_id = it->face_id;
1458 glyph->u.cmp_id = it->cmp_id;
b4192550
KH
1459 ++it->glyph_row->used[area];
1460 }
1461}
1462
06a2c219
GM
1463
1464/* Change IT->ascent and IT->height according to the setting of
1465 IT->voffset. */
1466
1467static INLINE void
1468take_vertical_position_into_account (it)
1469 struct it *it;
1470{
1471 if (it->voffset)
1472 {
1473 if (it->voffset < 0)
1474 /* Increase the ascent so that we can display the text higher
1475 in the line. */
1476 it->ascent += abs (it->voffset);
1477 else
1478 /* Increase the descent so that we can display the text lower
1479 in the line. */
1480 it->descent += it->voffset;
1481 }
1482}
1483
1484
1485/* Produce glyphs/get display metrics for the image IT is loaded with.
1486 See the description of struct display_iterator in dispextern.h for
1487 an overview of struct display_iterator. */
1488
1489static void
1490x_produce_image_glyph (it)
1491 struct it *it;
1492{
1493 struct image *img;
1494 struct face *face;
1495
1496 xassert (it->what == IT_IMAGE);
1497
1498 face = FACE_FROM_ID (it->f, it->face_id);
1499 img = IMAGE_FROM_ID (it->f, it->image_id);
1500 xassert (img);
1501
1502 /* Make sure X resources of the face and image are loaded. */
1503 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1504 prepare_image_for_display (it->f, img);
1505
95af8492 1506 it->ascent = it->phys_ascent = image_ascent (img, face);
22d650b8
GM
1507 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1508 it->pixel_width = img->width + 2 * img->hmargin;
06a2c219
GM
1509
1510 it->nglyphs = 1;
1511
1512 if (face->box != FACE_NO_BOX)
1513 {
1514 it->ascent += face->box_line_width;
1515 it->descent += face->box_line_width;
1516
1517 if (it->start_of_box_run_p)
1518 it->pixel_width += face->box_line_width;
1519 if (it->end_of_box_run_p)
1520 it->pixel_width += face->box_line_width;
1521 }
1522
1523 take_vertical_position_into_account (it);
1524
1525 if (it->glyph_row)
1526 {
1527 struct glyph *glyph;
1528 enum glyph_row_area area = it->area;
1529
1530 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1531 if (glyph < it->glyph_row->glyphs[area + 1])
1532 {
06a2c219
GM
1533 glyph->charpos = CHARPOS (it->position);
1534 glyph->object = it->object;
88d75730 1535 glyph->pixel_width = it->pixel_width;
06a2c219 1536 glyph->voffset = it->voffset;
88d75730 1537 glyph->type = IMAGE_GLYPH;
06a2c219 1538 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1539 glyph->left_box_line_p = it->start_of_box_run_p;
1540 glyph->right_box_line_p = it->end_of_box_run_p;
1541 glyph->overlaps_vertically_p = 0;
1542 glyph->padding_p = 0;
1543 glyph->glyph_not_available_p = 0;
1544 glyph->face_id = it->face_id;
1545 glyph->u.img_id = img->id;
06a2c219
GM
1546 ++it->glyph_row->used[area];
1547 }
1548 }
1549}
1550
1551
1552/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1553 of the glyph, WIDTH and HEIGHT are the width and height of the
1554 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1555 ascent of the glyph (0 <= ASCENT <= 1). */
1556
1557static void
1558x_append_stretch_glyph (it, object, width, height, ascent)
1559 struct it *it;
1560 Lisp_Object object;
1561 int width, height;
1562 double ascent;
1563{
1564 struct glyph *glyph;
1565 enum glyph_row_area area = it->area;
1566
1567 xassert (ascent >= 0 && ascent <= 1);
1568
1569 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1570 if (glyph < it->glyph_row->glyphs[area + 1])
1571 {
06a2c219
GM
1572 glyph->charpos = CHARPOS (it->position);
1573 glyph->object = object;
88d75730 1574 glyph->pixel_width = width;
06a2c219 1575 glyph->voffset = it->voffset;
88d75730 1576 glyph->type = STRETCH_GLYPH;
06a2c219 1577 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1578 glyph->left_box_line_p = it->start_of_box_run_p;
1579 glyph->right_box_line_p = it->end_of_box_run_p;
1580 glyph->overlaps_vertically_p = 0;
1581 glyph->padding_p = 0;
1582 glyph->glyph_not_available_p = 0;
1583 glyph->face_id = it->face_id;
1584 glyph->u.stretch.ascent = height * ascent;
1585 glyph->u.stretch.height = height;
06a2c219
GM
1586 ++it->glyph_row->used[area];
1587 }
1588}
1589
1590
1591/* Produce a stretch glyph for iterator IT. IT->object is the value
1592 of the glyph property displayed. The value must be a list
1593 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1594 being recognized:
1595
1596 1. `:width WIDTH' specifies that the space should be WIDTH *
1597 canonical char width wide. WIDTH may be an integer or floating
1598 point number.
1599
1600 2. `:relative-width FACTOR' specifies that the width of the stretch
1601 should be computed from the width of the first character having the
1602 `glyph' property, and should be FACTOR times that width.
1603
1604 3. `:align-to HPOS' specifies that the space should be wide enough
1605 to reach HPOS, a value in canonical character units.
1606
1607 Exactly one of the above pairs must be present.
1608
1609 4. `:height HEIGHT' specifies that the height of the stretch produced
1610 should be HEIGHT, measured in canonical character units.
1611
1612 5. `:relative-height FACTOR' specifies that the height of the the
1613 stretch should be FACTOR times the height of the characters having
1614 the glyph property.
1615
1616 Either none or exactly one of 4 or 5 must be present.
1617
1618 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1619 of the stretch should be used for the ascent of the stretch.
1620 ASCENT must be in the range 0 <= ASCENT <= 100. */
1621
1622#define NUMVAL(X) \
1623 ((INTEGERP (X) || FLOATP (X)) \
1624 ? XFLOATINT (X) \
1625 : - 1)
1626
1627
1628static void
1629x_produce_stretch_glyph (it)
1630 struct it *it;
1631{
1632 /* (space :width WIDTH :height HEIGHT. */
3e71d8f2
GM
1633#if GLYPH_DEBUG
1634 extern Lisp_Object Qspace;
1635#endif
1636 extern Lisp_Object QCwidth, QCheight, QCascent;
06a2c219
GM
1637 extern Lisp_Object QCrelative_width, QCrelative_height;
1638 extern Lisp_Object QCalign_to;
1639 Lisp_Object prop, plist;
1640 double width = 0, height = 0, ascent = 0;
1641 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1642 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1643
1644 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1645
1646 /* List should start with `space'. */
1647 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1648 plist = XCDR (it->object);
1649
1650 /* Compute the width of the stretch. */
1651 if (prop = Fplist_get (plist, QCwidth),
1652 NUMVAL (prop) > 0)
1653 /* Absolute width `:width WIDTH' specified and valid. */
1654 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1655 else if (prop = Fplist_get (plist, QCrelative_width),
1656 NUMVAL (prop) > 0)
1657 {
1658 /* Relative width `:relative-width FACTOR' specified and valid.
1659 Compute the width of the characters having the `glyph'
1660 property. */
1661 struct it it2;
1662 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1663
1664 it2 = *it;
1665 if (it->multibyte_p)
1666 {
1667 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1668 - IT_BYTEPOS (*it));
1669 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1670 }
1671 else
1672 it2.c = *p, it2.len = 1;
1673
1674 it2.glyph_row = NULL;
1675 it2.what = IT_CHARACTER;
1676 x_produce_glyphs (&it2);
1677 width = NUMVAL (prop) * it2.pixel_width;
1678 }
1679 else if (prop = Fplist_get (plist, QCalign_to),
1680 NUMVAL (prop) > 0)
1681 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1682 else
1683 /* Nothing specified -> width defaults to canonical char width. */
1684 width = CANON_X_UNIT (it->f);
1685
1686 /* Compute height. */
1687 if (prop = Fplist_get (plist, QCheight),
1688 NUMVAL (prop) > 0)
1689 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1690 else if (prop = Fplist_get (plist, QCrelative_height),
1691 NUMVAL (prop) > 0)
1692 height = FONT_HEIGHT (font) * NUMVAL (prop);
1693 else
1694 height = FONT_HEIGHT (font);
1695
1696 /* Compute percentage of height used for ascent. If
1697 `:ascent ASCENT' is present and valid, use that. Otherwise,
1698 derive the ascent from the font in use. */
1699 if (prop = Fplist_get (plist, QCascent),
1700 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1701 ascent = NUMVAL (prop) / 100.0;
1702 else
1703 ascent = (double) font->ascent / FONT_HEIGHT (font);
1704
1705 if (width <= 0)
1706 width = 1;
1707 if (height <= 0)
1708 height = 1;
1709
1710 if (it->glyph_row)
1711 {
1712 Lisp_Object object = it->stack[it->sp - 1].string;
1713 if (!STRINGP (object))
1714 object = it->w->buffer;
1715 x_append_stretch_glyph (it, object, width, height, ascent);
1716 }
1717
1718 it->pixel_width = width;
66ac4b0e
GM
1719 it->ascent = it->phys_ascent = height * ascent;
1720 it->descent = it->phys_descent = height - it->ascent;
06a2c219
GM
1721 it->nglyphs = 1;
1722
1723 if (face->box != FACE_NO_BOX)
1724 {
1725 it->ascent += face->box_line_width;
1726 it->descent += face->box_line_width;
1727
1728 if (it->start_of_box_run_p)
1729 it->pixel_width += face->box_line_width;
1730 if (it->end_of_box_run_p)
1731 it->pixel_width += face->box_line_width;
1732 }
1733
1734 take_vertical_position_into_account (it);
1735}
1736
b4192550
KH
1737/* Return proper value to be used as baseline offset of font that has
1738 ASCENT and DESCENT to draw characters by the font at the vertical
1739 center of the line of frame F.
1740
1741 Here, out task is to find the value of BOFF in the following figure;
1742
1743 -------------------------+-----------+-
1744 -+-+---------+-+ | |
1745 | | | | | |
1746 | | | | F_ASCENT F_HEIGHT
1747 | | | ASCENT | |
1748 HEIGHT | | | | |
1749 | | |-|-+------+-----------|------- baseline
1750 | | | | BOFF | |
1751 | |---------|-+-+ | |
1752 | | | DESCENT | |
1753 -+-+---------+-+ F_DESCENT |
1754 -------------------------+-----------+-
1755
1756 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1757 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1758 DESCENT = FONT->descent
1759 HEIGHT = FONT_HEIGHT (FONT)
1760 F_DESCENT = (F->output_data.x->font->descent
1761 - F->output_data.x->baseline_offset)
1762 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1763*/
1764
458f45fa
KH
1765#define VCENTER_BASELINE_OFFSET(FONT, F) \
1766 ((FONT)->descent \
1767 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1768 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1769 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
06a2c219
GM
1770
1771/* Produce glyphs/get display metrics for the display element IT is
1772 loaded with. See the description of struct display_iterator in
1773 dispextern.h for an overview of struct display_iterator. */
1774
1775static void
1776x_produce_glyphs (it)
1777 struct it *it;
1778{
ee569018
KH
1779 it->glyph_not_available_p = 0;
1780
06a2c219
GM
1781 if (it->what == IT_CHARACTER)
1782 {
1783 XChar2b char2b;
1784 XFontStruct *font;
ee569018 1785 struct face *face = FACE_FROM_ID (it->f, it->face_id);
06a2c219 1786 XCharStruct *pcm;
06a2c219 1787 int font_not_found_p;
b4192550
KH
1788 struct font_info *font_info;
1789 int boff; /* baseline offset */
a4249304
KH
1790 /* We may change it->multibyte_p upon unibyte<->multibyte
1791 conversion. So, save the current value now and restore it
1792 later.
1793
1794 Note: It seems that we don't have to record multibyte_p in
1795 struct glyph because the character code itself tells if or
1796 not the character is multibyte. Thus, in the future, we must
1797 consider eliminating the field `multibyte_p' in the struct
1798 glyph.
1799 */
1800 int saved_multibyte_p = it->multibyte_p;
06a2c219 1801
ee569018
KH
1802 /* Maybe translate single-byte characters to multibyte, or the
1803 other way. */
06a2c219 1804 it->char_to_display = it->c;
ee569018 1805 if (!ASCII_BYTE_P (it->c))
06a2c219 1806 {
ee569018
KH
1807 if (unibyte_display_via_language_environment
1808 && SINGLE_BYTE_CHAR_P (it->c)
1809 && (it->c >= 0240
1810 || !NILP (Vnonascii_translation_table)))
1811 {
1812 it->char_to_display = unibyte_char_to_multibyte (it->c);
a4249304 1813 it->multibyte_p = 1;
ee569018
KH
1814 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1815 face = FACE_FROM_ID (it->f, it->face_id);
1816 }
1817 else if (!SINGLE_BYTE_CHAR_P (it->c)
1818 && !it->multibyte_p)
1819 {
1820 it->char_to_display = multibyte_char_to_unibyte (it->c, Qnil);
a4249304 1821 it->multibyte_p = 0;
ee569018
KH
1822 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1823 face = FACE_FROM_ID (it->f, it->face_id);
1824 }
06a2c219
GM
1825 }
1826
ee569018
KH
1827 /* Get font to use. Encode IT->char_to_display. */
1828 x_get_char_face_and_encoding (it->f, it->char_to_display,
1829 it->face_id, &char2b,
1830 it->multibyte_p);
06a2c219
GM
1831 font = face->font;
1832
1833 /* When no suitable font found, use the default font. */
1834 font_not_found_p = font == NULL;
1835 if (font_not_found_p)
b4192550
KH
1836 {
1837 font = FRAME_FONT (it->f);
1838 boff = it->f->output_data.x->baseline_offset;
1839 font_info = NULL;
1840 }
1841 else
1842 {
1843 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1844 boff = font_info->baseline_offset;
1845 if (font_info->vertical_centering)
1846 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1847 }
06a2c219
GM
1848
1849 if (it->char_to_display >= ' '
1850 && (!it->multibyte_p || it->char_to_display < 128))
1851 {
1852 /* Either unibyte or ASCII. */
1853 int stretched_p;
1854
1855 it->nglyphs = 1;
06a2c219
GM
1856
1857 pcm = x_per_char_metric (font, &char2b);
b4192550
KH
1858 it->ascent = font->ascent + boff;
1859 it->descent = font->descent - boff;
474848ac
GM
1860
1861 if (pcm)
1862 {
1863 it->phys_ascent = pcm->ascent + boff;
1864 it->phys_descent = pcm->descent - boff;
1865 it->pixel_width = pcm->width;
1866 }
1867 else
1868 {
1869 it->glyph_not_available_p = 1;
1870 it->phys_ascent = font->ascent + boff;
1871 it->phys_descent = font->descent - boff;
1872 it->pixel_width = FONT_WIDTH (font);
1873 }
06a2c219
GM
1874
1875 /* If this is a space inside a region of text with
1876 `space-width' property, change its width. */
1877 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1878 if (stretched_p)
1879 it->pixel_width *= XFLOATINT (it->space_width);
1880
1881 /* If face has a box, add the box thickness to the character
1882 height. If character has a box line to the left and/or
1883 right, add the box line width to the character's width. */
1884 if (face->box != FACE_NO_BOX)
1885 {
1886 int thick = face->box_line_width;
1887
1888 it->ascent += thick;
1889 it->descent += thick;
1890
1891 if (it->start_of_box_run_p)
1892 it->pixel_width += thick;
1893 if (it->end_of_box_run_p)
1894 it->pixel_width += thick;
1895 }
1896
1897 /* If face has an overline, add the height of the overline
1898 (1 pixel) and a 1 pixel margin to the character height. */
1899 if (face->overline_p)
1900 it->ascent += 2;
1901
1902 take_vertical_position_into_account (it);
1903
1904 /* If we have to actually produce glyphs, do it. */
1905 if (it->glyph_row)
1906 {
1907 if (stretched_p)
1908 {
1909 /* Translate a space with a `space-width' property
1910 into a stretch glyph. */
1911 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1912 x_append_stretch_glyph (it, it->object, it->pixel_width,
1913 it->ascent + it->descent, ascent);
1914 }
1915 else
1916 x_append_glyph (it);
1917
1918 /* If characters with lbearing or rbearing are displayed
1919 in this line, record that fact in a flag of the
1920 glyph row. This is used to optimize X output code. */
1c7e22fd 1921 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
06a2c219
GM
1922 it->glyph_row->contains_overlapping_glyphs_p = 1;
1923 }
1924 }
1925 else if (it->char_to_display == '\n')
1926 {
1927 /* A newline has no width but we need the height of the line. */
1928 it->pixel_width = 0;
1929 it->nglyphs = 0;
b4192550
KH
1930 it->ascent = it->phys_ascent = font->ascent + boff;
1931 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1932
1933 if (face->box != FACE_NO_BOX)
1934 {
1935 int thick = face->box_line_width;
1936 it->ascent += thick;
1937 it->descent += thick;
1938 }
1939 }
1940 else if (it->char_to_display == '\t')
1941 {
1942 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
d365f5bb 1943 int x = it->current_x + it->continuation_lines_width;
06a2c219 1944 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
2a32b5ea
GM
1945
1946 /* If the distance from the current position to the next tab
1947 stop is less than a canonical character width, use the
1948 tab stop after that. */
1949 if (next_tab_x - x < CANON_X_UNIT (it->f))
1950 next_tab_x += tab_width;
06a2c219
GM
1951
1952 it->pixel_width = next_tab_x - x;
1953 it->nglyphs = 1;
b4192550
KH
1954 it->ascent = it->phys_ascent = font->ascent + boff;
1955 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1956
1957 if (it->glyph_row)
1958 {
1959 double ascent = (double) it->ascent / (it->ascent + it->descent);
1960 x_append_stretch_glyph (it, it->object, it->pixel_width,
1961 it->ascent + it->descent, ascent);
1962 }
1963 }
1964 else
1965 {
1966 /* A multi-byte character. Assume that the display width of the
1967 character is the width of the character multiplied by the
b4192550 1968 width of the font. */
06a2c219 1969
b4192550
KH
1970 /* If we found a font, this font should give us the right
1971 metrics. If we didn't find a font, use the frame's
1972 default font and calculate the width of the character
1973 from the charset width; this is what old redisplay code
1974 did. */
1975 pcm = x_per_char_metric (font, &char2b);
ee569018
KH
1976 if (font_not_found_p || !pcm)
1977 {
1978 int charset = CHAR_CHARSET (it->char_to_display);
1979
1980 it->glyph_not_available_p = 1;
1981 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
1982 * CHARSET_WIDTH (charset));
1983 it->phys_ascent = font->ascent + boff;
1984 it->phys_descent = font->descent - boff;
1985 }
1986 else
1987 {
1988 it->pixel_width = pcm->width;
1989 it->phys_ascent = pcm->ascent + boff;
1990 it->phys_descent = pcm->descent - boff;
1991 if (it->glyph_row
1992 && (pcm->lbearing < 0
1993 || pcm->rbearing > pcm->width))
1994 it->glyph_row->contains_overlapping_glyphs_p = 1;
1995 }
b4192550
KH
1996 it->nglyphs = 1;
1997 it->ascent = font->ascent + boff;
1998 it->descent = font->descent - boff;
06a2c219
GM
1999 if (face->box != FACE_NO_BOX)
2000 {
2001 int thick = face->box_line_width;
2002 it->ascent += thick;
2003 it->descent += thick;
2004
2005 if (it->start_of_box_run_p)
2006 it->pixel_width += thick;
2007 if (it->end_of_box_run_p)
2008 it->pixel_width += thick;
2009 }
2010
2011 /* If face has an overline, add the height of the overline
2012 (1 pixel) and a 1 pixel margin to the character height. */
2013 if (face->overline_p)
2014 it->ascent += 2;
2015
2016 take_vertical_position_into_account (it);
2017
2018 if (it->glyph_row)
2019 x_append_glyph (it);
2020 }
a4249304 2021 it->multibyte_p = saved_multibyte_p;
06a2c219 2022 }
b4192550
KH
2023 else if (it->what == IT_COMPOSITION)
2024 {
2025 /* Note: A composition is represented as one glyph in the
2026 glyph matrix. There are no padding glyphs. */
2027 XChar2b char2b;
2028 XFontStruct *font;
ee569018 2029 struct face *face = FACE_FROM_ID (it->f, it->face_id);
b4192550
KH
2030 XCharStruct *pcm;
2031 int font_not_found_p;
2032 struct font_info *font_info;
2033 int boff; /* baseline offset */
2034 struct composition *cmp = composition_table[it->cmp_id];
2035
2036 /* Maybe translate single-byte characters to multibyte. */
2037 it->char_to_display = it->c;
2038 if (unibyte_display_via_language_environment
2039 && SINGLE_BYTE_CHAR_P (it->c)
2040 && (it->c >= 0240
2041 || (it->c >= 0200
2042 && !NILP (Vnonascii_translation_table))))
2043 {
2044 it->char_to_display = unibyte_char_to_multibyte (it->c);
b4192550
KH
2045 }
2046
2047 /* Get face and font to use. Encode IT->char_to_display. */
ee569018
KH
2048 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2049 face = FACE_FROM_ID (it->f, it->face_id);
2050 x_get_char_face_and_encoding (it->f, it->char_to_display,
2051 it->face_id, &char2b, it->multibyte_p);
b4192550
KH
2052 font = face->font;
2053
2054 /* When no suitable font found, use the default font. */
2055 font_not_found_p = font == NULL;
2056 if (font_not_found_p)
2057 {
2058 font = FRAME_FONT (it->f);
2059 boff = it->f->output_data.x->baseline_offset;
2060 font_info = NULL;
2061 }
2062 else
2063 {
2064 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2065 boff = font_info->baseline_offset;
2066 if (font_info->vertical_centering)
2067 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2068 }
2069
2070 /* There are no padding glyphs, so there is only one glyph to
2071 produce for the composition. Important is that pixel_width,
2072 ascent and descent are the values of what is drawn by
2073 draw_glyphs (i.e. the values of the overall glyphs composed). */
2074 it->nglyphs = 1;
2075
2076 /* If we have not yet calculated pixel size data of glyphs of
2077 the composition for the current face font, calculate them
2078 now. Theoretically, we have to check all fonts for the
2079 glyphs, but that requires much time and memory space. So,
2080 here we check only the font of the first glyph. This leads
2081 to incorrect display very rarely, and C-l (recenter) can
2082 correct the display anyway. */
2083 if (cmp->font != (void *) font)
2084 {
2085 /* Ascent and descent of the font of the first character of
2086 this composition (adjusted by baseline offset). Ascent
2087 and descent of overall glyphs should not be less than
2088 them respectively. */
2089 int font_ascent = font->ascent + boff;
2090 int font_descent = font->descent - boff;
2091 /* Bounding box of the overall glyphs. */
2092 int leftmost, rightmost, lowest, highest;
329bed06 2093 int i, width, ascent, descent;
b4192550
KH
2094
2095 cmp->font = (void *) font;
2096
2097 /* Initialize the bounding box. */
1bdeec2e
KH
2098 if (font_info
2099 && (pcm = x_per_char_metric (font, &char2b)))
329bed06
GM
2100 {
2101 width = pcm->width;
2102 ascent = pcm->ascent;
2103 descent = pcm->descent;
2104 }
2105 else
2106 {
2107 width = FONT_WIDTH (font);
2108 ascent = font->ascent;
2109 descent = font->descent;
2110 }
2111
2112 rightmost = width;
2113 lowest = - descent + boff;
2114 highest = ascent + boff;
b4192550 2115 leftmost = 0;
329bed06 2116
b4192550
KH
2117 if (font_info
2118 && font_info->default_ascent
2119 && CHAR_TABLE_P (Vuse_default_ascent)
2120 && !NILP (Faref (Vuse_default_ascent,
2121 make_number (it->char_to_display))))
2122 highest = font_info->default_ascent + boff;
2123
2124 /* Draw the first glyph at the normal position. It may be
2125 shifted to right later if some other glyphs are drawn at
2126 the left. */
2127 cmp->offsets[0] = 0;
2128 cmp->offsets[1] = boff;
2129
2130 /* Set cmp->offsets for the remaining glyphs. */
2131 for (i = 1; i < cmp->glyph_len; i++)
2132 {
2133 int left, right, btm, top;
2134 int ch = COMPOSITION_GLYPH (cmp, i);
ee569018
KH
2135 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2136
2137 face = FACE_FROM_ID (it->f, face_id);
2138 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2139 it->multibyte_p);
b4192550
KH
2140 font = face->font;
2141 if (font == NULL)
2142 {
2143 font = FRAME_FONT (it->f);
2144 boff = it->f->output_data.x->baseline_offset;
2145 font_info = NULL;
2146 }
2147 else
2148 {
2149 font_info
2150 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2151 boff = font_info->baseline_offset;
2152 if (font_info->vertical_centering)
2153 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2154 }
2155
1bdeec2e
KH
2156 if (font_info
2157 && (pcm = x_per_char_metric (font, &char2b)))
329bed06
GM
2158 {
2159 width = pcm->width;
2160 ascent = pcm->ascent;
2161 descent = pcm->descent;
2162 }
2163 else
2164 {
2165 width = FONT_WIDTH (font);
1bdeec2e
KH
2166 ascent = 1;
2167 descent = 0;
329bed06 2168 }
b4192550
KH
2169
2170 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2171 {
2172 /* Relative composition with or without
2173 alternate chars. */
329bed06
GM
2174 left = (leftmost + rightmost - width) / 2;
2175 btm = - descent + boff;
b4192550
KH
2176 if (font_info && font_info->relative_compose
2177 && (! CHAR_TABLE_P (Vignore_relative_composition)
2178 || NILP (Faref (Vignore_relative_composition,
2179 make_number (ch)))))
2180 {
2181
329bed06 2182 if (- descent >= font_info->relative_compose)
b4192550
KH
2183 /* One extra pixel between two glyphs. */
2184 btm = highest + 1;
329bed06 2185 else if (ascent <= 0)
b4192550 2186 /* One extra pixel between two glyphs. */
329bed06 2187 btm = lowest - 1 - ascent - descent;
b4192550
KH
2188 }
2189 }
2190 else
2191 {
2192 /* A composition rule is specified by an integer
2193 value that encodes global and new reference
2194 points (GREF and NREF). GREF and NREF are
2195 specified by numbers as below:
2196
2197 0---1---2 -- ascent
2198 | |
2199 | |
2200 | |
2201 9--10--11 -- center
2202 | |
2203 ---3---4---5--- baseline
2204 | |
2205 6---7---8 -- descent
2206 */
2207 int rule = COMPOSITION_RULE (cmp, i);
2208 int gref, nref, grefx, grefy, nrefx, nrefy;
2209
2210 COMPOSITION_DECODE_RULE (rule, gref, nref);
2211 grefx = gref % 3, nrefx = nref % 3;
2212 grefy = gref / 3, nrefy = nref / 3;
2213
2214 left = (leftmost
2215 + grefx * (rightmost - leftmost) / 2
329bed06 2216 - nrefx * width / 2);
b4192550
KH
2217 btm = ((grefy == 0 ? highest
2218 : grefy == 1 ? 0
2219 : grefy == 2 ? lowest
2220 : (highest + lowest) / 2)
329bed06
GM
2221 - (nrefy == 0 ? ascent + descent
2222 : nrefy == 1 ? descent - boff
b4192550 2223 : nrefy == 2 ? 0
329bed06 2224 : (ascent + descent) / 2));
b4192550
KH
2225 }
2226
2227 cmp->offsets[i * 2] = left;
329bed06 2228 cmp->offsets[i * 2 + 1] = btm + descent;
b4192550
KH
2229
2230 /* Update the bounding box of the overall glyphs. */
329bed06
GM
2231 right = left + width;
2232 top = btm + descent + ascent;
b4192550
KH
2233 if (left < leftmost)
2234 leftmost = left;
2235 if (right > rightmost)
2236 rightmost = right;
2237 if (top > highest)
2238 highest = top;
2239 if (btm < lowest)
2240 lowest = btm;
2241 }
2242
2243 /* If there are glyphs whose x-offsets are negative,
2244 shift all glyphs to the right and make all x-offsets
2245 non-negative. */
2246 if (leftmost < 0)
2247 {
2248 for (i = 0; i < cmp->glyph_len; i++)
2249 cmp->offsets[i * 2] -= leftmost;
2250 rightmost -= leftmost;
2251 }
2252
2253 cmp->pixel_width = rightmost;
2254 cmp->ascent = highest;
2255 cmp->descent = - lowest;
2256 if (cmp->ascent < font_ascent)
2257 cmp->ascent = font_ascent;
2258 if (cmp->descent < font_descent)
2259 cmp->descent = font_descent;
2260 }
2261
2262 it->pixel_width = cmp->pixel_width;
2263 it->ascent = it->phys_ascent = cmp->ascent;
2264 it->descent = it->phys_descent = cmp->descent;
2265
2266 if (face->box != FACE_NO_BOX)
2267 {
2268 int thick = face->box_line_width;
2269 it->ascent += thick;
2270 it->descent += thick;
2271
2272 if (it->start_of_box_run_p)
2273 it->pixel_width += thick;
2274 if (it->end_of_box_run_p)
2275 it->pixel_width += thick;
2276 }
2277
2278 /* If face has an overline, add the height of the overline
2279 (1 pixel) and a 1 pixel margin to the character height. */
2280 if (face->overline_p)
2281 it->ascent += 2;
2282
2283 take_vertical_position_into_account (it);
2284
2285 if (it->glyph_row)
2286 x_append_composite_glyph (it);
2287 }
06a2c219
GM
2288 else if (it->what == IT_IMAGE)
2289 x_produce_image_glyph (it);
2290 else if (it->what == IT_STRETCH)
2291 x_produce_stretch_glyph (it);
2292
3017fdd1
GM
2293 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2294 because this isn't true for images with `:ascent 100'. */
2295 xassert (it->ascent >= 0 && it->descent >= 0);
06a2c219
GM
2296 if (it->area == TEXT_AREA)
2297 it->current_x += it->pixel_width;
66ac4b0e 2298
d365f5bb
GM
2299 it->descent += it->extra_line_spacing;
2300
06a2c219
GM
2301 it->max_ascent = max (it->max_ascent, it->ascent);
2302 it->max_descent = max (it->max_descent, it->descent);
66ac4b0e
GM
2303 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2304 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
06a2c219
GM
2305}
2306
2307
2308/* Estimate the pixel height of the mode or top line on frame F.
2309 FACE_ID specifies what line's height to estimate. */
2310
2311int
2312x_estimate_mode_line_height (f, face_id)
2313 struct frame *f;
2314 enum face_id face_id;
2315{
43281ee3 2316 int height = FONT_HEIGHT (FRAME_FONT (f));
06a2c219
GM
2317
2318 /* This function is called so early when Emacs starts that the face
2319 cache and mode line face are not yet initialized. */
2320 if (FRAME_FACE_CACHE (f))
2321 {
2322 struct face *face = FACE_FROM_ID (f, face_id);
2323 if (face)
43281ee3
GM
2324 {
2325 if (face->font)
2326 height = FONT_HEIGHT (face->font);
2327 height += 2 * face->box_line_width;
2328 }
06a2c219
GM
2329 }
2330
2331 return height;
2332}
2333
2334\f
2335/***********************************************************************
2336 Glyph display
2337 ***********************************************************************/
2338
2339/* A sequence of glyphs to be drawn in the same face.
2340
2341 This data structure is not really completely X specific, so it
2342 could possibly, at least partially, be useful for other systems. It
2343 is currently not part of the external redisplay interface because
2344 it's not clear what other systems will need. */
2345
2346struct glyph_string
2347{
2348 /* X-origin of the string. */
2349 int x;
2350
2351 /* Y-origin and y-position of the base line of this string. */
2352 int y, ybase;
2353
2354 /* The width of the string, not including a face extension. */
2355 int width;
2356
2357 /* The width of the string, including a face extension. */
2358 int background_width;
2359
2360 /* The height of this string. This is the height of the line this
2361 string is drawn in, and can be different from the height of the
2362 font the string is drawn in. */
2363 int height;
2364
2365 /* Number of pixels this string overwrites in front of its x-origin.
2366 This number is zero if the string has an lbearing >= 0; it is
2367 -lbearing, if the string has an lbearing < 0. */
2368 int left_overhang;
2369
2370 /* Number of pixels this string overwrites past its right-most
2371 nominal x-position, i.e. x + width. Zero if the string's
2372 rbearing is <= its nominal width, rbearing - width otherwise. */
2373 int right_overhang;
2374
2375 /* The frame on which the glyph string is drawn. */
2376 struct frame *f;
2377
2378 /* The window on which the glyph string is drawn. */
2379 struct window *w;
2380
2381 /* X display and window for convenience. */
2382 Display *display;
2383 Window window;
2384
2385 /* The glyph row for which this string was built. It determines the
2386 y-origin and height of the string. */
2387 struct glyph_row *row;
2388
2389 /* The area within row. */
2390 enum glyph_row_area area;
2391
2392 /* Characters to be drawn, and number of characters. */
2393 XChar2b *char2b;
2394 int nchars;
2395
06a2c219
GM
2396 /* A face-override for drawing cursors, mouse face and similar. */
2397 enum draw_glyphs_face hl;
2398
2399 /* Face in which this string is to be drawn. */
2400 struct face *face;
2401
2402 /* Font in which this string is to be drawn. */
2403 XFontStruct *font;
2404
2405 /* Font info for this string. */
2406 struct font_info *font_info;
2407
b4192550
KH
2408 /* Non-null means this string describes (part of) a composition.
2409 All characters from char2b are drawn composed. */
2410 struct composition *cmp;
06a2c219
GM
2411
2412 /* Index of this glyph string's first character in the glyph
b4192550
KH
2413 definition of CMP. If this is zero, this glyph string describes
2414 the first character of a composition. */
06a2c219
GM
2415 int gidx;
2416
2417 /* 1 means this glyph strings face has to be drawn to the right end
2418 of the window's drawing area. */
2419 unsigned extends_to_end_of_line_p : 1;
2420
2421 /* 1 means the background of this string has been drawn. */
2422 unsigned background_filled_p : 1;
2423
2424 /* 1 means glyph string must be drawn with 16-bit functions. */
2425 unsigned two_byte_p : 1;
2426
2427 /* 1 means that the original font determined for drawing this glyph
2428 string could not be loaded. The member `font' has been set to
2429 the frame's default font in this case. */
2430 unsigned font_not_found_p : 1;
2431
2432 /* 1 means that the face in which this glyph string is drawn has a
2433 stipple pattern. */
2434 unsigned stippled_p : 1;
2435
66ac4b0e
GM
2436 /* 1 means only the foreground of this glyph string must be drawn,
2437 and we should use the physical height of the line this glyph
2438 string appears in as clip rect. */
2439 unsigned for_overlaps_p : 1;
2440
06a2c219
GM
2441 /* The GC to use for drawing this glyph string. */
2442 GC gc;
2443
2444 /* A pointer to the first glyph in the string. This glyph
2445 corresponds to char2b[0]. Needed to draw rectangles if
2446 font_not_found_p is 1. */
2447 struct glyph *first_glyph;
2448
2449 /* Image, if any. */
2450 struct image *img;
2451
2452 struct glyph_string *next, *prev;
2453};
2454
2455
5c187dee 2456#if 0
06a2c219
GM
2457
2458static void
2459x_dump_glyph_string (s)
2460 struct glyph_string *s;
2461{
2462 fprintf (stderr, "glyph string\n");
2463 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2464 s->x, s->y, s->width, s->height);
2465 fprintf (stderr, " ybase = %d\n", s->ybase);
2466 fprintf (stderr, " hl = %d\n", s->hl);
2467 fprintf (stderr, " left overhang = %d, right = %d\n",
2468 s->left_overhang, s->right_overhang);
2469 fprintf (stderr, " nchars = %d\n", s->nchars);
2470 fprintf (stderr, " extends to end of line = %d\n",
2471 s->extends_to_end_of_line_p);
2472 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2473 fprintf (stderr, " bg width = %d\n", s->background_width);
2474}
2475
2476#endif /* GLYPH_DEBUG */
2477
2478
2479
2480static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2481 struct glyph_string **,
2482 struct glyph_string *,
2483 struct glyph_string *));
2484static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2485 struct glyph_string **,
2486 struct glyph_string *,
2487 struct glyph_string *));
2488static void x_append_glyph_string P_ ((struct glyph_string **,
2489 struct glyph_string **,
2490 struct glyph_string *));
2491static int x_left_overwritten P_ ((struct glyph_string *));
2492static int x_left_overwriting P_ ((struct glyph_string *));
2493static int x_right_overwritten P_ ((struct glyph_string *));
2494static int x_right_overwriting P_ ((struct glyph_string *));
66ac4b0e
GM
2495static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2496 int));
06a2c219
GM
2497static void x_init_glyph_string P_ ((struct glyph_string *,
2498 XChar2b *, struct window *,
2499 struct glyph_row *,
2500 enum glyph_row_area, int,
2501 enum draw_glyphs_face));
2502static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2503 enum glyph_row_area, int, int,
66ac4b0e 2504 enum draw_glyphs_face, int *, int *, int));
06a2c219
GM
2505static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2506static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2507static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2508 int));
2509static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
b4192550 2510static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
06a2c219
GM
2511static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2512static void x_draw_glyph_string P_ ((struct glyph_string *));
2513static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2514static void x_set_cursor_gc P_ ((struct glyph_string *));
2515static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2516static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2517static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2518 int *, int *));
2519static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2520static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
68c45bf0 2521 unsigned long *, double, int));
06a2c219 2522static void x_setup_relief_color P_ ((struct frame *, struct relief *,
68c45bf0 2523 double, int, unsigned long));
06a2c219
GM
2524static void x_setup_relief_colors P_ ((struct glyph_string *));
2525static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2526static void x_draw_image_relief P_ ((struct glyph_string *));
2527static void x_draw_image_foreground P_ ((struct glyph_string *));
2528static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2529static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2530static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2531 int, int, int));
2532static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2533 int, int, int, int, XRectangle *));
2534static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2535 int, int, int, XRectangle *));
66ac4b0e
GM
2536static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2537 enum glyph_row_area));
209f68d9
GM
2538static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2539 struct glyph_row *,
2540 enum glyph_row_area, int, int));
06a2c219 2541
163dcff3
GM
2542#if GLYPH_DEBUG
2543static void x_check_font P_ ((struct frame *, XFontStruct *));
2544#endif
2545
06a2c219 2546
06a2c219
GM
2547/* Append the list of glyph strings with head H and tail T to the list
2548 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2549
2550static INLINE void
2551x_append_glyph_string_lists (head, tail, h, t)
2552 struct glyph_string **head, **tail;
2553 struct glyph_string *h, *t;
2554{
2555 if (h)
2556 {
2557 if (*head)
2558 (*tail)->next = h;
2559 else
2560 *head = h;
2561 h->prev = *tail;
2562 *tail = t;
2563 }
2564}
2565
2566
2567/* Prepend the list of glyph strings with head H and tail T to the
2568 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2569 result. */
2570
2571static INLINE void
2572x_prepend_glyph_string_lists (head, tail, h, t)
2573 struct glyph_string **head, **tail;
2574 struct glyph_string *h, *t;
2575{
2576 if (h)
2577 {
2578 if (*head)
2579 (*head)->prev = t;
2580 else
2581 *tail = t;
2582 t->next = *head;
2583 *head = h;
2584 }
2585}
2586
2587
2588/* Append glyph string S to the list with head *HEAD and tail *TAIL.
2589 Set *HEAD and *TAIL to the resulting list. */
2590
2591static INLINE void
2592x_append_glyph_string (head, tail, s)
2593 struct glyph_string **head, **tail;
2594 struct glyph_string *s;
2595{
2596 s->next = s->prev = NULL;
2597 x_append_glyph_string_lists (head, tail, s, s);
2598}
2599
2600
2601/* Set S->gc to a suitable GC for drawing glyph string S in cursor
2602 face. */
2603
2604static void
2605x_set_cursor_gc (s)
2606 struct glyph_string *s;
2607{
2608 if (s->font == FRAME_FONT (s->f)
2609 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2610 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
b4192550 2611 && !s->cmp)
06a2c219
GM
2612 s->gc = s->f->output_data.x->cursor_gc;
2613 else
2614 {
2615 /* Cursor on non-default face: must merge. */
2616 XGCValues xgcv;
2617 unsigned long mask;
2618
2619 xgcv.background = s->f->output_data.x->cursor_pixel;
2620 xgcv.foreground = s->face->background;
2621
2622 /* If the glyph would be invisible, try a different foreground. */
2623 if (xgcv.foreground == xgcv.background)
2624 xgcv.foreground = s->face->foreground;
2625 if (xgcv.foreground == xgcv.background)
2626 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2627 if (xgcv.foreground == xgcv.background)
2628 xgcv.foreground = s->face->foreground;
2629
2630 /* Make sure the cursor is distinct from text in this face. */
2631 if (xgcv.background == s->face->background
2632 && xgcv.foreground == s->face->foreground)
2633 {
2634 xgcv.background = s->face->foreground;
2635 xgcv.foreground = s->face->background;
2636 }
2637
2638 IF_DEBUG (x_check_font (s->f, s->font));
2639 xgcv.font = s->font->fid;
2640 xgcv.graphics_exposures = False;
2641 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2642
2643 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2644 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2645 mask, &xgcv);
2646 else
2647 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2648 = XCreateGC (s->display, s->window, mask, &xgcv);
2649
2650 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2651 }
2652}
2653
2654
2655/* Set up S->gc of glyph string S for drawing text in mouse face. */
2656
2657static void
2658x_set_mouse_face_gc (s)
2659 struct glyph_string *s;
2660{
2661 int face_id;
ee569018 2662 struct face *face;
06a2c219
GM
2663
2664 /* What face has to be used for the mouse face? */
2665 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
ee569018 2666 face = FACE_FROM_ID (s->f, face_id);
033e3e18
GM
2667 if (s->first_glyph->type == CHAR_GLYPH)
2668 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2669 else
2670 face_id = FACE_FOR_CHAR (s->f, face, 0);
06a2c219
GM
2671 s->face = FACE_FROM_ID (s->f, face_id);
2672 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2673
2674 /* If font in this face is same as S->font, use it. */
2675 if (s->font == s->face->font)
2676 s->gc = s->face->gc;
2677 else
2678 {
2679 /* Otherwise construct scratch_cursor_gc with values from FACE
2680 but font FONT. */
2681 XGCValues xgcv;
2682 unsigned long mask;
2683
2684 xgcv.background = s->face->background;
2685 xgcv.foreground = s->face->foreground;
2686 IF_DEBUG (x_check_font (s->f, s->font));
2687 xgcv.font = s->font->fid;
2688 xgcv.graphics_exposures = False;
2689 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2690
2691 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2692 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2693 mask, &xgcv);
2694 else
2695 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2696 = XCreateGC (s->display, s->window, mask, &xgcv);
2697
2698 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2699 }
2700
2701 xassert (s->gc != 0);
2702}
2703
2704
2705/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2706 Faces to use in the mode line have already been computed when the
2707 matrix was built, so there isn't much to do, here. */
2708
2709static INLINE void
2710x_set_mode_line_face_gc (s)
2711 struct glyph_string *s;
2712{
2713 s->gc = s->face->gc;
06a2c219
GM
2714}
2715
2716
2717/* Set S->gc of glyph string S for drawing that glyph string. Set
2718 S->stippled_p to a non-zero value if the face of S has a stipple
2719 pattern. */
2720
2721static INLINE void
2722x_set_glyph_string_gc (s)
2723 struct glyph_string *s;
2724{
209f68d9
GM
2725 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2726
06a2c219
GM
2727 if (s->hl == DRAW_NORMAL_TEXT)
2728 {
2729 s->gc = s->face->gc;
2730 s->stippled_p = s->face->stipple != 0;
2731 }
2732 else if (s->hl == DRAW_INVERSE_VIDEO)
2733 {
2734 x_set_mode_line_face_gc (s);
2735 s->stippled_p = s->face->stipple != 0;
2736 }
2737 else if (s->hl == DRAW_CURSOR)
2738 {
2739 x_set_cursor_gc (s);
2740 s->stippled_p = 0;
2741 }
2742 else if (s->hl == DRAW_MOUSE_FACE)
2743 {
2744 x_set_mouse_face_gc (s);
2745 s->stippled_p = s->face->stipple != 0;
2746 }
2747 else if (s->hl == DRAW_IMAGE_RAISED
2748 || s->hl == DRAW_IMAGE_SUNKEN)
2749 {
2750 s->gc = s->face->gc;
2751 s->stippled_p = s->face->stipple != 0;
2752 }
2753 else
2754 {
2755 s->gc = s->face->gc;
2756 s->stippled_p = s->face->stipple != 0;
2757 }
2758
2759 /* GC must have been set. */
2760 xassert (s->gc != 0);
2761}
2762
2763
2764/* Return in *R the clipping rectangle for glyph string S. */
2765
2766static void
2767x_get_glyph_string_clip_rect (s, r)
2768 struct glyph_string *s;
2769 XRectangle *r;
2770{
2771 if (s->row->full_width_p)
2772 {
2773 /* Draw full-width. X coordinates are relative to S->w->left. */
1da3fd71
GM
2774 int canon_x = CANON_X_UNIT (s->f);
2775
2776 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2777 r->width = XFASTINT (s->w->width) * canon_x;
06a2c219
GM
2778
2779 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2780 {
1da3fd71 2781 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
06a2c219
GM
2782 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2783 r->x -= width;
2784 }
2785
b9432a85 2786 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
1da3fd71 2787
06a2c219
GM
2788 /* Unless displaying a mode or menu bar line, which are always
2789 fully visible, clip to the visible part of the row. */
2790 if (s->w->pseudo_window_p)
2791 r->height = s->row->visible_height;
2792 else
2793 r->height = s->height;
2794 }
2795 else
2796 {
2797 /* This is a text line that may be partially visible. */
2798 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2799 r->width = window_box_width (s->w, s->area);
2800 r->height = s->row->visible_height;
2801 }
2802
2803 /* Don't use S->y for clipping because it doesn't take partially
2804 visible lines into account. For example, it can be negative for
2805 partially visible lines at the top of a window. */
2806 if (!s->row->full_width_p
2807 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
045dee35 2808 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
06a2c219
GM
2809 else
2810 r->y = max (0, s->row->y);
06a2c219 2811
9ea173e8 2812 /* If drawing a tool-bar window, draw it over the internal border
06a2c219 2813 at the top of the window. */
9ea173e8 2814 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219 2815 r->y -= s->f->output_data.x->internal_border_width;
66ac4b0e
GM
2816
2817 /* If S draws overlapping rows, it's sufficient to use the top and
2818 bottom of the window for clipping because this glyph string
2819 intentionally draws over other lines. */
2820 if (s->for_overlaps_p)
2821 {
045dee35 2822 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
66ac4b0e
GM
2823 r->height = window_text_bottom_y (s->w) - r->y;
2824 }
2825
2826 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
06a2c219
GM
2827}
2828
2829
2830/* Set clipping for output of glyph string S. S may be part of a mode
2831 line or menu if we don't have X toolkit support. */
2832
2833static INLINE void
2834x_set_glyph_string_clipping (s)
2835 struct glyph_string *s;
2836{
2837 XRectangle r;
2838 x_get_glyph_string_clip_rect (s, &r);
2839 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2840}
2841
2842
2843/* Compute left and right overhang of glyph string S. If S is a glyph
b4192550 2844 string for a composition, assume overhangs don't exist. */
06a2c219
GM
2845
2846static INLINE void
2847x_compute_glyph_string_overhangs (s)
2848 struct glyph_string *s;
2849{
b4192550 2850 if (s->cmp == NULL
06a2c219
GM
2851 && s->first_glyph->type == CHAR_GLYPH)
2852 {
2853 XCharStruct cs;
2854 int direction, font_ascent, font_descent;
2855 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2856 &font_ascent, &font_descent, &cs);
2857 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2858 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2859 }
2860}
2861
2862
2863/* Compute overhangs and x-positions for glyph string S and its
2864 predecessors, or successors. X is the starting x-position for S.
2865 BACKWARD_P non-zero means process predecessors. */
2866
2867static void
2868x_compute_overhangs_and_x (s, x, backward_p)
2869 struct glyph_string *s;
2870 int x;
2871 int backward_p;
2872{
2873 if (backward_p)
2874 {
2875 while (s)
2876 {
2877 x_compute_glyph_string_overhangs (s);
2878 x -= s->width;
2879 s->x = x;
2880 s = s->prev;
2881 }
2882 }
2883 else
2884 {
2885 while (s)
2886 {
2887 x_compute_glyph_string_overhangs (s);
2888 s->x = x;
2889 x += s->width;
2890 s = s->next;
2891 }
2892 }
2893}
2894
2895
2896/* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
b4192550
KH
2897 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2898 assumed to be zero. */
06a2c219
GM
2899
2900static void
2901x_get_glyph_overhangs (glyph, f, left, right)
2902 struct glyph *glyph;
2903 struct frame *f;
2904 int *left, *right;
2905{
06a2c219
GM
2906 *left = *right = 0;
2907
b4192550 2908 if (glyph->type == CHAR_GLYPH)
06a2c219
GM
2909 {
2910 XFontStruct *font;
2911 struct face *face;
2912 struct font_info *font_info;
2913 XChar2b char2b;
ee569018
KH
2914 XCharStruct *pcm;
2915
2916 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
06a2c219
GM
2917 font = face->font;
2918 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
ee569018
KH
2919 if (font
2920 && (pcm = x_per_char_metric (font, &char2b)))
06a2c219 2921 {
06a2c219
GM
2922 if (pcm->rbearing > pcm->width)
2923 *right = pcm->rbearing - pcm->width;
2924 if (pcm->lbearing < 0)
2925 *left = -pcm->lbearing;
2926 }
2927 }
2928}
2929
2930
2931/* Return the index of the first glyph preceding glyph string S that
2932 is overwritten by S because of S's left overhang. Value is -1
2933 if no glyphs are overwritten. */
2934
2935static int
2936x_left_overwritten (s)
2937 struct glyph_string *s;
2938{
2939 int k;
2940
2941 if (s->left_overhang)
2942 {
2943 int x = 0, i;
2944 struct glyph *glyphs = s->row->glyphs[s->area];
2945 int first = s->first_glyph - glyphs;
2946
2947 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2948 x -= glyphs[i].pixel_width;
2949
2950 k = i + 1;
2951 }
2952 else
2953 k = -1;
2954
2955 return k;
2956}
2957
2958
2959/* Return the index of the first glyph preceding glyph string S that
2960 is overwriting S because of its right overhang. Value is -1 if no
2961 glyph in front of S overwrites S. */
2962
2963static int
2964x_left_overwriting (s)
2965 struct glyph_string *s;
2966{
2967 int i, k, x;
2968 struct glyph *glyphs = s->row->glyphs[s->area];
2969 int first = s->first_glyph - glyphs;
2970
2971 k = -1;
2972 x = 0;
2973 for (i = first - 1; i >= 0; --i)
2974 {
2975 int left, right;
2976 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2977 if (x + right > 0)
2978 k = i;
2979 x -= glyphs[i].pixel_width;
2980 }
2981
2982 return k;
2983}
2984
2985
2986/* Return the index of the last glyph following glyph string S that is
2987 not overwritten by S because of S's right overhang. Value is -1 if
2988 no such glyph is found. */
2989
2990static int
2991x_right_overwritten (s)
2992 struct glyph_string *s;
2993{
2994 int k = -1;
2995
2996 if (s->right_overhang)
2997 {
2998 int x = 0, i;
2999 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 3000 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
3001 int end = s->row->used[s->area];
3002
3003 for (i = first; i < end && s->right_overhang > x; ++i)
3004 x += glyphs[i].pixel_width;
3005
3006 k = i;
3007 }
3008
3009 return k;
3010}
3011
3012
3013/* Return the index of the last glyph following glyph string S that
3014 overwrites S because of its left overhang. Value is negative
3015 if no such glyph is found. */
3016
3017static int
3018x_right_overwriting (s)
3019 struct glyph_string *s;
3020{
3021 int i, k, x;
3022 int end = s->row->used[s->area];
3023 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 3024 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
3025
3026 k = -1;
3027 x = 0;
3028 for (i = first; i < end; ++i)
3029 {
3030 int left, right;
3031 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3032 if (x - left < 0)
3033 k = i;
3034 x += glyphs[i].pixel_width;
3035 }
3036
3037 return k;
3038}
3039
3040
3041/* Fill rectangle X, Y, W, H with background color of glyph string S. */
3042
3043static INLINE void
3044x_clear_glyph_string_rect (s, x, y, w, h)
3045 struct glyph_string *s;
3046 int x, y, w, h;
3047{
3048 XGCValues xgcv;
3049 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3050 XSetForeground (s->display, s->gc, xgcv.background);
3051 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3052 XSetForeground (s->display, s->gc, xgcv.foreground);
3053}
3054
3055
3056/* Draw the background of glyph_string S. If S->background_filled_p
3057 is non-zero don't draw it. FORCE_P non-zero means draw the
3058 background even if it wouldn't be drawn normally. This is used
b4192550
KH
3059 when a string preceding S draws into the background of S, or S
3060 contains the first component of a composition. */
06a2c219
GM
3061
3062static void
3063x_draw_glyph_string_background (s, force_p)
3064 struct glyph_string *s;
3065 int force_p;
3066{
3067 /* Nothing to do if background has already been drawn or if it
3068 shouldn't be drawn in the first place. */
3069 if (!s->background_filled_p)
3070 {
b4192550 3071 if (s->stippled_p)
06a2c219
GM
3072 {
3073 /* Fill background with a stipple pattern. */
3074 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3075 XFillRectangle (s->display, s->window, s->gc, s->x,
3076 s->y + s->face->box_line_width,
3077 s->background_width,
3078 s->height - 2 * s->face->box_line_width);
3079 XSetFillStyle (s->display, s->gc, FillSolid);
3080 s->background_filled_p = 1;
3081 }
3082 else if (FONT_HEIGHT (s->font) < s->height - 2 * s->face->box_line_width
3083 || s->font_not_found_p
3084 || s->extends_to_end_of_line_p
06a2c219
GM
3085 || force_p)
3086 {
3087 x_clear_glyph_string_rect (s, s->x, s->y + s->face->box_line_width,
3088 s->background_width,
3089 s->height - 2 * s->face->box_line_width);
3090 s->background_filled_p = 1;
3091 }
3092 }
3093}
3094
3095
3096/* Draw the foreground of glyph string S. */
3097
3098static void
3099x_draw_glyph_string_foreground (s)
3100 struct glyph_string *s;
3101{
3102 int i, x;
3103
3104 /* If first glyph of S has a left box line, start drawing the text
3105 of S to the right of that box line. */
3106 if (s->face->box != FACE_NO_BOX
3107 && s->first_glyph->left_box_line_p)
3108 x = s->x + s->face->box_line_width;
3109 else
3110 x = s->x;
3111
b4192550
KH
3112 /* Draw characters of S as rectangles if S's font could not be
3113 loaded. */
3114 if (s->font_not_found_p)
06a2c219 3115 {
b4192550 3116 for (i = 0; i < s->nchars; ++i)
06a2c219 3117 {
b4192550
KH
3118 struct glyph *g = s->first_glyph + i;
3119 XDrawRectangle (s->display, s->window,
3120 s->gc, x, s->y, g->pixel_width - 1,
3121 s->height - 1);
3122 x += g->pixel_width;
06a2c219
GM
3123 }
3124 }
3125 else
3126 {
b4192550
KH
3127 char *char1b = (char *) s->char2b;
3128 int boff = s->font_info->baseline_offset;
06a2c219 3129
b4192550
KH
3130 if (s->font_info->vertical_centering)
3131 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3132
3133 /* If we can use 8-bit functions, condense S->char2b. */
3134 if (!s->two_byte_p)
3135 for (i = 0; i < s->nchars; ++i)
3136 char1b[i] = s->char2b[i].byte2;
3137
3138 /* Draw text with XDrawString if background has already been
3139 filled. Otherwise, use XDrawImageString. (Note that
3140 XDrawImageString is usually faster than XDrawString.) Always
3141 use XDrawImageString when drawing the cursor so that there is
3142 no chance that characters under a box cursor are invisible. */
3143 if (s->for_overlaps_p
3144 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3145 {
3146 /* Draw characters with 16-bit or 8-bit functions. */
3147 if (s->two_byte_p)
3148 XDrawString16 (s->display, s->window, s->gc, x,
3149 s->ybase - boff, s->char2b, s->nchars);
3150 else
3151 XDrawString (s->display, s->window, s->gc, x,
3152 s->ybase - boff, char1b, s->nchars);
3153 }
06a2c219
GM
3154 else
3155 {
b4192550
KH
3156 if (s->two_byte_p)
3157 XDrawImageString16 (s->display, s->window, s->gc, x,
3158 s->ybase - boff, s->char2b, s->nchars);
06a2c219 3159 else
b4192550
KH
3160 XDrawImageString (s->display, s->window, s->gc, x,
3161 s->ybase - boff, char1b, s->nchars);
3162 }
3163 }
3164}
06a2c219 3165
b4192550 3166/* Draw the foreground of composite glyph string S. */
06a2c219 3167
b4192550
KH
3168static void
3169x_draw_composite_glyph_string_foreground (s)
3170 struct glyph_string *s;
3171{
3172 int i, x;
06a2c219 3173
b4192550
KH
3174 /* If first glyph of S has a left box line, start drawing the text
3175 of S to the right of that box line. */
3176 if (s->face->box != FACE_NO_BOX
3177 && s->first_glyph->left_box_line_p)
3178 x = s->x + s->face->box_line_width;
3179 else
3180 x = s->x;
06a2c219 3181
b4192550
KH
3182 /* S is a glyph string for a composition. S->gidx is the index of
3183 the first character drawn for glyphs of this composition.
3184 S->gidx == 0 means we are drawing the very first character of
3185 this composition. */
06a2c219 3186
b4192550
KH
3187 /* Draw a rectangle for the composition if the font for the very
3188 first character of the composition could not be loaded. */
3189 if (s->font_not_found_p)
3190 {
3191 if (s->gidx == 0)
3192 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3193 s->width - 1, s->height - 1);
3194 }
3195 else
3196 {
3197 for (i = 0; i < s->nchars; i++, ++s->gidx)
3198 XDrawString16 (s->display, s->window, s->gc,
3199 x + s->cmp->offsets[s->gidx * 2],
3200 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3201 s->char2b + i, 1);
06a2c219
GM
3202 }
3203}
3204
3205
80c32bcc
GM
3206#ifdef USE_X_TOOLKIT
3207
3e71d8f2 3208static struct frame *x_frame_of_widget P_ ((Widget));
80c32bcc 3209
3e71d8f2
GM
3210
3211/* Return the frame on which widget WIDGET is used.. Abort if frame
3212 cannot be determined. */
3213
e851c833 3214static struct frame *
3e71d8f2 3215x_frame_of_widget (widget)
80c32bcc 3216 Widget widget;
80c32bcc 3217{
80c32bcc 3218 struct x_display_info *dpyinfo;
5c187dee 3219 Lisp_Object tail;
3e71d8f2
GM
3220 struct frame *f;
3221
80c32bcc
GM
3222 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3223
3224 /* Find the top-level shell of the widget. Note that this function
3225 can be called when the widget is not yet realized, so XtWindow
3226 (widget) == 0. That's the reason we can't simply use
3227 x_any_window_to_frame. */
3228 while (!XtIsTopLevelShell (widget))
3229 widget = XtParent (widget);
3230
3231 /* Look for a frame with that top-level widget. Allocate the color
3232 on that frame to get the right gamma correction value. */
3233 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3234 if (GC_FRAMEP (XCAR (tail))
3235 && (f = XFRAME (XCAR (tail)),
3236 (f->output_data.nothing != 1
3237 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3238 && f->output_data.x->widget == widget)
3e71d8f2 3239 return f;
80c32bcc
GM
3240
3241 abort ();
3242}
3243
3e71d8f2
GM
3244
3245/* Allocate the color COLOR->pixel on the screen and display of
3246 widget WIDGET in colormap CMAP. If an exact match cannot be
3247 allocated, try the nearest color available. Value is non-zero
3248 if successful. This is called from lwlib. */
3249
3250int
3251x_alloc_nearest_color_for_widget (widget, cmap, color)
3252 Widget widget;
3253 Colormap cmap;
3254 XColor *color;
3255{
3256 struct frame *f = x_frame_of_widget (widget);
3257 return x_alloc_nearest_color (f, cmap, color);
3258}
3259
3260
46d516e5
MB
3261/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3262 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3263 If this produces the same color as PIXEL, try a color where all RGB
3264 values have DELTA added. Return the allocated color in *PIXEL.
3265 DISPLAY is the X display, CMAP is the colormap to operate on.
3266 Value is non-zero if successful. */
3267
3268int
3269x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3270 Widget widget;
3271 Display *display;
3272 Colormap cmap;
3273 unsigned long *pixel;
3274 double factor;
3275 int delta;
3276{
3277 struct frame *f = x_frame_of_widget (widget);
3278 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3279}
3280
3281
80c32bcc
GM
3282#endif /* USE_X_TOOLKIT */
3283
3284
f04e1297
GM
3285/* Value is an array of XColor structures for the contents of the
3286 color map of frame F. Set *NCELLS to the size of the array.
3287 Note that this probably shouldn't be called for large color maps,
3288 say a 24-bit TrueColor map. */
3289
3290static const XColor *
3291x_color_cells (f, ncells)
3292 struct frame *f;
3293 int *ncells;
3294{
3295 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3296
3297 if (dpyinfo->color_cells == NULL)
3298 {
3299 Display *display = FRAME_X_DISPLAY (f);
3300 Screen *screen = FRAME_X_SCREEN (f);
3301 int i;
3302
3303 dpyinfo->ncolor_cells
3304 = XDisplayCells (display, XScreenNumberOfScreen (screen));
3305 dpyinfo->color_cells
3306 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3307 * sizeof *dpyinfo->color_cells);
3308
3309 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3310 dpyinfo->color_cells[i].pixel = i;
3311
3312 XQueryColors (display, FRAME_X_COLORMAP (f),
3313 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3314 }
3315
3316 *ncells = dpyinfo->ncolor_cells;
3317 return dpyinfo->color_cells;
3318}
3319
3320
3321/* On frame F, translate pixel colors to RGB values for the NCOLORS
3322 colors in COLORS. Use cached information, if available. */
3323
3324void
3325x_query_colors (f, colors, ncolors)
3326 struct frame *f;
3327 XColor *colors;
3328 int ncolors;
3329{
3330 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3331
3332 if (dpyinfo->color_cells)
3333 {
3334 int i;
3335 for (i = 0; i < ncolors; ++i)
3336 {
3337 unsigned long pixel = colors[i].pixel;
3338 xassert (pixel < dpyinfo->ncolor_cells);
3339 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3340 colors[i] = dpyinfo->color_cells[pixel];
3341 }
3342 }
3343 else
3344 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3345}
3346
3347
3348/* On frame F, translate pixel color to RGB values for the color in
3349 COLOR. Use cached information, if available. */
3350
3351void
3352x_query_color (f, color)
3353 struct frame *f;
3354 XColor *color;
3355{
3356 x_query_colors (f, color, 1);
3357}
3358
3359
06a2c219
GM
3360/* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3361 CMAP. If an exact match can't be allocated, try the nearest color
3362 available. Value is non-zero if successful. Set *COLOR to the
3363 color allocated. */
3364
3365int
80c32bcc
GM
3366x_alloc_nearest_color (f, cmap, color)
3367 struct frame *f;
06a2c219
GM
3368 Colormap cmap;
3369 XColor *color;
3370{
80c32bcc
GM
3371 Display *display = FRAME_X_DISPLAY (f);
3372 Screen *screen = FRAME_X_SCREEN (f);
3373 int rc;
3374
3375 gamma_correct (f, color);
3376 rc = XAllocColor (display, cmap, color);
06a2c219
GM
3377 if (rc == 0)
3378 {
3379 /* If we got to this point, the colormap is full, so we're going
3380 to try to get the next closest color. The algorithm used is
3381 a least-squares matching, which is what X uses for closest
3382 color matching with StaticColor visuals. */
3383 int nearest, i;
3384 unsigned long nearest_delta = ~0;
f04e1297
GM
3385 int ncells;
3386 const XColor *cells = x_color_cells (f, &ncells);
06a2c219
GM
3387
3388 for (nearest = i = 0; i < ncells; ++i)
3389 {
3390 long dred = (color->red >> 8) - (cells[i].red >> 8);
3391 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3392 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3393 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3394
3395 if (delta < nearest_delta)
3396 {
3397 nearest = i;
3398 nearest_delta = delta;
3399 }
3400 }
3401
3402 color->red = cells[nearest].red;
3403 color->green = cells[nearest].green;
3404 color->blue = cells[nearest].blue;
3405 rc = XAllocColor (display, cmap, color);
3406 }
35efe0a1
GM
3407 else
3408 {
3409 /* If allocation succeeded, and the allocated pixel color is not
3410 equal to a cached pixel color recorded earlier, there was a
3411 change in the colormap, so clear the color cache. */
3412 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3413 XColor *cached_color;
3414
3415 if (dpyinfo->color_cells
3416 && (cached_color = &dpyinfo->color_cells[color->pixel],
cae71efe
GM
3417 (cached_color->red != color->red
3418 || cached_color->blue != color->blue
3419 || cached_color->green != color->green)))
35efe0a1
GM
3420 {
3421 xfree (dpyinfo->color_cells);
3422 dpyinfo->color_cells = NULL;
3423 dpyinfo->ncolor_cells = 0;
3424 }
3425 }
06a2c219 3426
d9c545da
GM
3427#ifdef DEBUG_X_COLORS
3428 if (rc)
3429 register_color (color->pixel);
3430#endif /* DEBUG_X_COLORS */
3431
06a2c219
GM
3432 return rc;
3433}
3434
3435
d9c545da
GM
3436/* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3437 It's necessary to do this instead of just using PIXEL directly to
3438 get color reference counts right. */
3439
3440unsigned long
3441x_copy_color (f, pixel)
3442 struct frame *f;
3443 unsigned long pixel;
3444{
3445 XColor color;
3446
3447 color.pixel = pixel;
3448 BLOCK_INPUT;
f04e1297 3449 x_query_color (f, &color);
d9c545da
GM
3450 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3451 UNBLOCK_INPUT;
3452#ifdef DEBUG_X_COLORS
3453 register_color (pixel);
3454#endif
3455 return color.pixel;
3456}
3457
3458
3e71d8f2
GM
3459/* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3460 It's necessary to do this instead of just using PIXEL directly to
3461 get color reference counts right. */
3462
3463unsigned long
3464x_copy_dpy_color (dpy, cmap, pixel)
3465 Display *dpy;
3466 Colormap cmap;
3467 unsigned long pixel;
3468{
3469 XColor color;
3470
3471 color.pixel = pixel;
3472 BLOCK_INPUT;
3473 XQueryColor (dpy, cmap, &color);
3474 XAllocColor (dpy, cmap, &color);
3475 UNBLOCK_INPUT;
3476#ifdef DEBUG_X_COLORS
3477 register_color (pixel);
3478#endif
3479 return color.pixel;
3480}
3481
3482
6d8b0acd 3483/* Brightness beyond which a color won't have its highlight brightness
d7361edf 3484 boosted.
6d8b0acd 3485
d7361edf
MB
3486 Nominally, highlight colors for `3d' faces are calculated by
3487 brightening an object's color by a constant scale factor, but this
3488 doesn't yield good results for dark colors, so for colors who's
3489 brightness is less than this value (on a scale of 0-65535) have an
3490 use an additional additive factor.
6d8b0acd
MB
3491
3492 The value here is set so that the default menu-bar/mode-line color
3493 (grey75) will not have its highlights changed at all. */
3494#define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3495
3496
06a2c219
GM
3497/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3498 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3499 If this produces the same color as PIXEL, try a color where all RGB
3500 values have DELTA added. Return the allocated color in *PIXEL.
3501 DISPLAY is the X display, CMAP is the colormap to operate on.
3502 Value is non-zero if successful. */
3503
3504static int
3505x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3506 struct frame *f;
3507 Display *display;
3508 Colormap cmap;
3509 unsigned long *pixel;
68c45bf0 3510 double factor;
06a2c219
GM
3511 int delta;
3512{
3513 XColor color, new;
6d8b0acd 3514 long bright;
06a2c219
GM
3515 int success_p;
3516
3517 /* Get RGB color values. */
3518 color.pixel = *pixel;
f04e1297 3519 x_query_color (f, &color);
06a2c219
GM
3520
3521 /* Change RGB values by specified FACTOR. Avoid overflow! */
3522 xassert (factor >= 0);
3523 new.red = min (0xffff, factor * color.red);
3524 new.green = min (0xffff, factor * color.green);
3525 new.blue = min (0xffff, factor * color.blue);
3526
d7361edf
MB
3527 /* Calculate brightness of COLOR. */
3528 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
6d8b0acd
MB
3529
3530 /* We only boost colors that are darker than
3531 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3532 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3533 /* Make an additive adjustment to NEW, because it's dark enough so
3534 that scaling by FACTOR alone isn't enough. */
3535 {
3536 /* How far below the limit this color is (0 - 1, 1 being darker). */
3537 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3538 /* The additive adjustment. */
d7361edf 3539 int min_delta = delta * dimness * factor / 2;
6d8b0acd
MB
3540
3541 if (factor < 1)
3542 {
6d8b0acd
MB
3543 new.red = max (0, new.red - min_delta);
3544 new.green = max (0, new.green - min_delta);
3545 new.blue = max (0, new.blue - min_delta);
3546 }
3547 else
3548 {
3549 new.red = min (0xffff, min_delta + new.red);
3550 new.green = min (0xffff, min_delta + new.green);
3551 new.blue = min (0xffff, min_delta + new.blue);
3552 }
3553 }
3554
06a2c219 3555 /* Try to allocate the color. */
80c32bcc 3556 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3557 if (success_p)
3558 {
3559 if (new.pixel == *pixel)
3560 {
3561 /* If we end up with the same color as before, try adding
3562 delta to the RGB values. */
0d605c67 3563 x_free_colors (f, &new.pixel, 1);
06a2c219
GM
3564
3565 new.red = min (0xffff, delta + color.red);
3566 new.green = min (0xffff, delta + color.green);
3567 new.blue = min (0xffff, delta + color.blue);
80c32bcc 3568 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3569 }
3570 else
3571 success_p = 1;
3572 *pixel = new.pixel;
3573 }
3574
3575 return success_p;
3576}
3577
3578
3579/* Set up the foreground color for drawing relief lines of glyph
3580 string S. RELIEF is a pointer to a struct relief containing the GC
3581 with which lines will be drawn. Use a color that is FACTOR or
3582 DELTA lighter or darker than the relief's background which is found
3583 in S->f->output_data.x->relief_background. If such a color cannot
3584 be allocated, use DEFAULT_PIXEL, instead. */
3585
3586static void
3587x_setup_relief_color (f, relief, factor, delta, default_pixel)
3588 struct frame *f;
3589 struct relief *relief;
68c45bf0 3590 double factor;
06a2c219
GM
3591 int delta;
3592 unsigned long default_pixel;
3593{
3594 XGCValues xgcv;
3595 struct x_output *di = f->output_data.x;
3596 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3597 unsigned long pixel;
3598 unsigned long background = di->relief_background;
43bd1b2b 3599 Colormap cmap = FRAME_X_COLORMAP (f);
dcd08bfb
GM
3600 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3601 Display *dpy = FRAME_X_DISPLAY (f);
06a2c219
GM
3602
3603 xgcv.graphics_exposures = False;
3604 xgcv.line_width = 1;
3605
3606 /* Free previously allocated color. The color cell will be reused
3607 when it has been freed as many times as it was allocated, so this
3608 doesn't affect faces using the same colors. */
3609 if (relief->gc
3610 && relief->allocated_p)
3611 {
0d605c67 3612 x_free_colors (f, &relief->pixel, 1);
06a2c219
GM
3613 relief->allocated_p = 0;
3614 }
3615
3616 /* Allocate new color. */
3617 xgcv.foreground = default_pixel;
3618 pixel = background;
dcd08bfb
GM
3619 if (dpyinfo->n_planes != 1
3620 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
06a2c219
GM
3621 {
3622 relief->allocated_p = 1;
3623 xgcv.foreground = relief->pixel = pixel;
3624 }
3625
3626 if (relief->gc == 0)
3627 {
dcd08bfb 3628 xgcv.stipple = dpyinfo->gray;
06a2c219 3629 mask |= GCStipple;
dcd08bfb 3630 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
06a2c219
GM
3631 }
3632 else
dcd08bfb 3633 XChangeGC (dpy, relief->gc, mask, &xgcv);
06a2c219
GM
3634}
3635
3636
3637/* Set up colors for the relief lines around glyph string S. */
3638
3639static void
3640x_setup_relief_colors (s)
3641 struct glyph_string *s;
3642{
3643 struct x_output *di = s->f->output_data.x;
3644 unsigned long color;
3645
3646 if (s->face->use_box_color_for_shadows_p)
3647 color = s->face->box_color;
3648 else
3649 {
3650 XGCValues xgcv;
3651
3652 /* Get the background color of the face. */
3653 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3654 color = xgcv.background;
3655 }
3656
3657 if (di->white_relief.gc == 0
3658 || color != di->relief_background)
3659 {
3660 di->relief_background = color;
3661 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3662 WHITE_PIX_DEFAULT (s->f));
3663 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3664 BLACK_PIX_DEFAULT (s->f));
3665 }
3666}
3667
3668
3669/* Draw a relief on frame F inside the rectangle given by LEFT_X,
3670 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3671 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3672 relief. LEFT_P non-zero means draw a relief on the left side of
3673 the rectangle. RIGHT_P non-zero means draw a relief on the right
3674 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3675 when drawing. */
3676
3677static void
3678x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3679 raised_p, left_p, right_p, clip_rect)
3680 struct frame *f;
3681 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3682 XRectangle *clip_rect;
3683{
3684 int i;
3685 GC gc;
3686
3687 if (raised_p)
3688 gc = f->output_data.x->white_relief.gc;
3689 else
3690 gc = f->output_data.x->black_relief.gc;
3691 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3692
3693 /* Top. */
3694 for (i = 0; i < width; ++i)
3695 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3696 left_x + i * left_p, top_y + i,
3697 right_x + 1 - i * right_p, top_y + i);
3698
3699 /* Left. */
3700 if (left_p)
3701 for (i = 0; i < width; ++i)
3702 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3703 left_x + i, top_y + i, left_x + i, bottom_y - i);
3704
3705 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3706 if (raised_p)
3707 gc = f->output_data.x->black_relief.gc;
3708 else
3709 gc = f->output_data.x->white_relief.gc;
3710 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3711
3712 /* Bottom. */
3713 for (i = 0; i < width; ++i)
3714 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3715 left_x + i * left_p, bottom_y - i,
3716 right_x + 1 - i * right_p, bottom_y - i);
3717
3718 /* Right. */
3719 if (right_p)
3720 for (i = 0; i < width; ++i)
3721 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3722 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3723
3724 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3725}
3726
3727
3728/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3729 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3730 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3731 left side of the rectangle. RIGHT_P non-zero means draw a line
3732 on the right side of the rectangle. CLIP_RECT is the clipping
3733 rectangle to use when drawing. */
3734
3735static void
3736x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3737 left_p, right_p, clip_rect)
3738 struct glyph_string *s;
3739 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3740 XRectangle *clip_rect;
3741{
3742 XGCValues xgcv;
3743
3744 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3745 XSetForeground (s->display, s->gc, s->face->box_color);
3746 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3747
3748 /* Top. */
3749 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3750 left_x, top_y, right_x - left_x + 1, width);
06a2c219
GM
3751
3752 /* Left. */
3753 if (left_p)
3754 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3755 left_x, top_y, width, bottom_y - top_y + 1);
06a2c219
GM
3756
3757 /* Bottom. */
3758 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3759 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
06a2c219
GM
3760
3761 /* Right. */
3762 if (right_p)
3763 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 3764 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
06a2c219
GM
3765
3766 XSetForeground (s->display, s->gc, xgcv.foreground);
3767 XSetClipMask (s->display, s->gc, None);
3768}
3769
3770
3771/* Draw a box around glyph string S. */
3772
3773static void
3774x_draw_glyph_string_box (s)
3775 struct glyph_string *s;
3776{
3777 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3778 int left_p, right_p;
3779 struct glyph *last_glyph;
3780 XRectangle clip_rect;
3781
3782 last_x = window_box_right (s->w, s->area);
3783 if (s->row->full_width_p
3784 && !s->w->pseudo_window_p)
3785 {
110859fc 3786 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
06a2c219
GM
3787 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3788 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3789 }
3790
3791 /* The glyph that may have a right box line. */
b4192550 3792 last_glyph = (s->cmp || s->img
06a2c219
GM
3793 ? s->first_glyph
3794 : s->first_glyph + s->nchars - 1);
3795
3796 width = s->face->box_line_width;
3797 raised_p = s->face->box == FACE_RAISED_BOX;
3798 left_x = s->x;
3799 right_x = ((s->row->full_width_p
1da3fd71 3800 ? last_x - 1
a7aeb2de 3801 : min (last_x, s->x + s->background_width) - 1));
06a2c219
GM
3802 top_y = s->y;
3803 bottom_y = top_y + s->height - 1;
3804
3805 left_p = (s->first_glyph->left_box_line_p
3806 || (s->hl == DRAW_MOUSE_FACE
3807 && (s->prev == NULL
3808 || s->prev->hl != s->hl)));
3809 right_p = (last_glyph->right_box_line_p
3810 || (s->hl == DRAW_MOUSE_FACE
3811 && (s->next == NULL
3812 || s->next->hl != s->hl)));
3813
3814 x_get_glyph_string_clip_rect (s, &clip_rect);
3815
3816 if (s->face->box == FACE_SIMPLE_BOX)
3817 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3818 left_p, right_p, &clip_rect);
3819 else
3820 {
3821 x_setup_relief_colors (s);
3822 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3823 width, raised_p, left_p, right_p, &clip_rect);
3824 }
3825}
3826
3827
3828/* Draw foreground of image glyph string S. */
3829
3830static void
3831x_draw_image_foreground (s)
3832 struct glyph_string *s;
3833{
3834 int x;
95af8492 3835 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
3836
3837 /* If first glyph of S has a left box line, start drawing it to the
3838 right of that line. */
3839 if (s->face->box != FACE_NO_BOX
3840 && s->first_glyph->left_box_line_p)
3841 x = s->x + s->face->box_line_width;
3842 else
3843 x = s->x;
3844
3845 /* If there is a margin around the image, adjust x- and y-position
3846 by that margin. */
22d650b8
GM
3847 x += s->img->hmargin;
3848 y += s->img->vmargin;
06a2c219
GM
3849
3850 if (s->img->pixmap)
3851 {
3852 if (s->img->mask)
3853 {
3854 /* We can't set both a clip mask and use XSetClipRectangles
3855 because the latter also sets a clip mask. We also can't
3856 trust on the shape extension to be available
3857 (XShapeCombineRegion). So, compute the rectangle to draw
3858 manually. */
3859 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3860 | GCFunction);
3861 XGCValues xgcv;
3862 XRectangle clip_rect, image_rect, r;
3863
3864 xgcv.clip_mask = s->img->mask;
3865 xgcv.clip_x_origin = x;
3866 xgcv.clip_y_origin = y;
3867 xgcv.function = GXcopy;
3868 XChangeGC (s->display, s->gc, mask, &xgcv);
3869
3870 x_get_glyph_string_clip_rect (s, &clip_rect);
3871 image_rect.x = x;
3872 image_rect.y = y;
3873 image_rect.width = s->img->width;
3874 image_rect.height = s->img->height;
3875 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3876 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3877 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3878 }
3879 else
3880 {
49ad1d99
GM
3881 unsigned long mask = GCClipXOrigin | GCClipYOrigin | GCFunction;
3882 XGCValues xgcv;
3883 XRectangle clip_rect, image_rect, r;
3884
3885 x_get_glyph_string_clip_rect (s, &clip_rect);
3886 image_rect.x = x;
3887 image_rect.y = y;
3888 image_rect.width = s->img->width;
3889 image_rect.height = s->img->height;
3890 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3891 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3892 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
06a2c219
GM
3893
3894 /* When the image has a mask, we can expect that at
3895 least part of a mouse highlight or a block cursor will
3896 be visible. If the image doesn't have a mask, make
3897 a block cursor visible by drawing a rectangle around
3898 the image. I believe it's looking better if we do
3899 nothing here for mouse-face. */
3900 if (s->hl == DRAW_CURSOR)
3901 XDrawRectangle (s->display, s->window, s->gc, x, y,
3902 s->img->width - 1, s->img->height - 1);
3903 }
3904 }
3905 else
3906 /* Draw a rectangle if image could not be loaded. */
3907 XDrawRectangle (s->display, s->window, s->gc, x, y,
3908 s->img->width - 1, s->img->height - 1);
3909}
3910
3911
3912/* Draw a relief around the image glyph string S. */
3913
3914static void
3915x_draw_image_relief (s)
3916 struct glyph_string *s;
3917{
3918 int x0, y0, x1, y1, thick, raised_p;
3919 XRectangle r;
3920 int x;
95af8492 3921 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
3922
3923 /* If first glyph of S has a left box line, start drawing it to the
3924 right of that line. */
3925 if (s->face->box != FACE_NO_BOX
3926 && s->first_glyph->left_box_line_p)
3927 x = s->x + s->face->box_line_width;
3928 else
3929 x = s->x;
3930
3931 /* If there is a margin around the image, adjust x- and y-position
3932 by that margin. */
22d650b8
GM
3933 x += s->img->hmargin;
3934 y += s->img->vmargin;
06a2c219
GM
3935
3936 if (s->hl == DRAW_IMAGE_SUNKEN
3937 || s->hl == DRAW_IMAGE_RAISED)
3938 {
9ea173e8 3939 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
06a2c219
GM
3940 raised_p = s->hl == DRAW_IMAGE_RAISED;
3941 }
3942 else
3943 {
3944 thick = abs (s->img->relief);
3945 raised_p = s->img->relief > 0;
3946 }
3947
3948 x0 = x - thick;
3949 y0 = y - thick;
3950 x1 = x + s->img->width + thick - 1;
3951 y1 = y + s->img->height + thick - 1;
3952
3953 x_setup_relief_colors (s);
3954 x_get_glyph_string_clip_rect (s, &r);
3955 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3956}
3957
3958
3959/* Draw the foreground of image glyph string S to PIXMAP. */
3960
3961static void
3962x_draw_image_foreground_1 (s, pixmap)
3963 struct glyph_string *s;
3964 Pixmap pixmap;
3965{
3966 int x;
95af8492 3967 int y = s->ybase - s->y - image_ascent (s->img, s->face);
06a2c219
GM
3968
3969 /* If first glyph of S has a left box line, start drawing it to the
3970 right of that line. */
3971 if (s->face->box != FACE_NO_BOX
3972 && s->first_glyph->left_box_line_p)
3973 x = s->face->box_line_width;
3974 else
3975 x = 0;
3976
3977 /* If there is a margin around the image, adjust x- and y-position
3978 by that margin. */
22d650b8
GM
3979 x += s->img->hmargin;
3980 y += s->img->vmargin;
dc43ef94 3981
06a2c219
GM
3982 if (s->img->pixmap)
3983 {
3984 if (s->img->mask)
3985 {
3986 /* We can't set both a clip mask and use XSetClipRectangles
3987 because the latter also sets a clip mask. We also can't
3988 trust on the shape extension to be available
3989 (XShapeCombineRegion). So, compute the rectangle to draw
3990 manually. */
3991 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3992 | GCFunction);
3993 XGCValues xgcv;
3994
3995 xgcv.clip_mask = s->img->mask;
3996 xgcv.clip_x_origin = x;
3997 xgcv.clip_y_origin = y;
3998 xgcv.function = GXcopy;
3999 XChangeGC (s->display, s->gc, mask, &xgcv);
4000
4001 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4002 0, 0, s->img->width, s->img->height, x, y);
4003 XSetClipMask (s->display, s->gc, None);
4004 }
4005 else
4006 {
4007 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4008 0, 0, s->img->width, s->img->height, x, y);
4009
4010 /* When the image has a mask, we can expect that at
4011 least part of a mouse highlight or a block cursor will
4012 be visible. If the image doesn't have a mask, make
4013 a block cursor visible by drawing a rectangle around
4014 the image. I believe it's looking better if we do
4015 nothing here for mouse-face. */
4016 if (s->hl == DRAW_CURSOR)
4017 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4018 s->img->width - 1, s->img->height - 1);
4019 }
4020 }
4021 else
4022 /* Draw a rectangle if image could not be loaded. */
4023 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4024 s->img->width - 1, s->img->height - 1);
4025}
dc43ef94 4026
990ba854 4027
06a2c219
GM
4028/* Draw part of the background of glyph string S. X, Y, W, and H
4029 give the rectangle to draw. */
a9a5b0a5 4030
06a2c219
GM
4031static void
4032x_draw_glyph_string_bg_rect (s, x, y, w, h)
4033 struct glyph_string *s;
4034 int x, y, w, h;
4035{
4036 if (s->stippled_p)
4037 {
4038 /* Fill background with a stipple pattern. */
4039 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4040 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4041 XSetFillStyle (s->display, s->gc, FillSolid);
4042 }
4043 else
4044 x_clear_glyph_string_rect (s, x, y, w, h);
4045}
07e34cb0 4046
b5210ea7 4047
06a2c219 4048/* Draw image glyph string S.
dc43ef94 4049
06a2c219
GM
4050 s->y
4051 s->x +-------------------------
4052 | s->face->box
4053 |
4054 | +-------------------------
4055 | | s->img->margin
4056 | |
4057 | | +-------------------
4058 | | | the image
dc43ef94 4059
06a2c219 4060 */
dc43ef94 4061
06a2c219
GM
4062static void
4063x_draw_image_glyph_string (s)
4064 struct glyph_string *s;
4065{
4066 int x, y;
4067 int box_line_width = s->face->box_line_width;
06a2c219
GM
4068 int height;
4069 Pixmap pixmap = None;
4070
4071 height = s->height - 2 * box_line_width;
4072
4073 /* Fill background with face under the image. Do it only if row is
4074 taller than image or if image has a clip mask to reduce
4075 flickering. */
4076 s->stippled_p = s->face->stipple != 0;
4077 if (height > s->img->height
22d650b8
GM
4078 || s->img->hmargin
4079 || s->img->vmargin
06a2c219
GM
4080 || s->img->mask
4081 || s->img->pixmap == 0
4082 || s->width != s->background_width)
4083 {
4084 if (box_line_width && s->first_glyph->left_box_line_p)
4085 x = s->x + box_line_width;
4086 else
4087 x = s->x;
4088
4089 y = s->y + box_line_width;
4090
4091 if (s->img->mask)
4092 {
f9b5db02
GM
4093 /* Create a pixmap as large as the glyph string. Fill it
4094 with the background color. Copy the image to it, using
4095 its mask. Copy the temporary pixmap to the display. */
06a2c219
GM
4096 Screen *screen = FRAME_X_SCREEN (s->f);
4097 int depth = DefaultDepthOfScreen (screen);
4098
4099 /* Create a pixmap as large as the glyph string. */
4100 pixmap = XCreatePixmap (s->display, s->window,
4101 s->background_width,
4102 s->height, depth);
4103
4104 /* Don't clip in the following because we're working on the
4105 pixmap. */
4106 XSetClipMask (s->display, s->gc, None);
4107
4108 /* Fill the pixmap with the background color/stipple. */
4109 if (s->stippled_p)
4110 {
4111 /* Fill background with a stipple pattern. */
4112 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4113 XFillRectangle (s->display, pixmap, s->gc,
4114 0, 0, s->background_width, s->height);
4115 XSetFillStyle (s->display, s->gc, FillSolid);
4116 }
4117 else
4118 {
4119 XGCValues xgcv;
4120 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4121 &xgcv);
4122 XSetForeground (s->display, s->gc, xgcv.background);
4123 XFillRectangle (s->display, pixmap, s->gc,
4124 0, 0, s->background_width, s->height);
4125 XSetForeground (s->display, s->gc, xgcv.foreground);
4126 }
4127 }
4128 else
06a2c219
GM
4129 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4130
4131 s->background_filled_p = 1;
4132 }
dc43ef94 4133
06a2c219
GM
4134 /* Draw the foreground. */
4135 if (pixmap != None)
4136 {
4137 x_draw_image_foreground_1 (s, pixmap);
4138 x_set_glyph_string_clipping (s);
4139 XCopyArea (s->display, pixmap, s->window, s->gc,
4140 0, 0, s->background_width, s->height, s->x, s->y);
4141 XFreePixmap (s->display, pixmap);
4142 }
4143 else
4144 x_draw_image_foreground (s);
b5210ea7 4145
06a2c219
GM
4146 /* If we must draw a relief around the image, do it. */
4147 if (s->img->relief
4148 || s->hl == DRAW_IMAGE_RAISED
4149 || s->hl == DRAW_IMAGE_SUNKEN)
4150 x_draw_image_relief (s);
4151}
8c1a6a84 4152
990ba854 4153
06a2c219 4154/* Draw stretch glyph string S. */
dc43ef94 4155
06a2c219
GM
4156static void
4157x_draw_stretch_glyph_string (s)
4158 struct glyph_string *s;
4159{
4160 xassert (s->first_glyph->type == STRETCH_GLYPH);
4161 s->stippled_p = s->face->stipple != 0;
990ba854 4162
06a2c219
GM
4163 if (s->hl == DRAW_CURSOR
4164 && !x_stretch_cursor_p)
4165 {
4166 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4167 as wide as the stretch glyph. */
4168 int width = min (CANON_X_UNIT (s->f), s->background_width);
990ba854 4169
06a2c219
GM
4170 /* Draw cursor. */
4171 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
0cdd0c9f 4172
06a2c219
GM
4173 /* Clear rest using the GC of the original non-cursor face. */
4174 if (width < s->background_width)
4175 {
4176 GC gc = s->face->gc;
4177 int x = s->x + width, y = s->y;
4178 int w = s->background_width - width, h = s->height;
4179 XRectangle r;
dc43ef94 4180
06a2c219
GM
4181 x_get_glyph_string_clip_rect (s, &r);
4182 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
97210f4e 4183
06a2c219
GM
4184 if (s->face->stipple)
4185 {
4186 /* Fill background with a stipple pattern. */
4187 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4188 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4189 XSetFillStyle (s->display, gc, FillSolid);
4190 }
4191 else
4192 {
4193 XGCValues xgcv;
4194 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4195 XSetForeground (s->display, gc, xgcv.background);
4196 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4197 XSetForeground (s->display, gc, xgcv.foreground);
4198 }
4199 }
4200 }
4201 else
4202 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4203 s->height);
4204
4205 s->background_filled_p = 1;
4206}
4207
4208
4209/* Draw glyph string S. */
4210
4211static void
4212x_draw_glyph_string (s)
4213 struct glyph_string *s;
4214{
4215 /* If S draws into the background of its successor, draw the
4216 background of the successor first so that S can draw into it.
4217 This makes S->next use XDrawString instead of XDrawImageString. */
66ac4b0e 4218 if (s->next && s->right_overhang && !s->for_overlaps_p)
06a2c219
GM
4219 {
4220 xassert (s->next->img == NULL);
4221 x_set_glyph_string_gc (s->next);
4222 x_set_glyph_string_clipping (s->next);
4223 x_draw_glyph_string_background (s->next, 1);
4224 }
97210f4e 4225
06a2c219
GM
4226 /* Set up S->gc, set clipping and draw S. */
4227 x_set_glyph_string_gc (s);
4228 x_set_glyph_string_clipping (s);
4229
4230 switch (s->first_glyph->type)
4231 {
4232 case IMAGE_GLYPH:
4233 x_draw_image_glyph_string (s);
4234 break;
4235
4236 case STRETCH_GLYPH:
4237 x_draw_stretch_glyph_string (s);
4238 break;
4239
4240 case CHAR_GLYPH:
66ac4b0e
GM
4241 if (s->for_overlaps_p)
4242 s->background_filled_p = 1;
4243 else
4244 x_draw_glyph_string_background (s, 0);
06a2c219
GM
4245 x_draw_glyph_string_foreground (s);
4246 break;
4247
b4192550
KH
4248 case COMPOSITE_GLYPH:
4249 if (s->for_overlaps_p || s->gidx > 0)
4250 s->background_filled_p = 1;
4251 else
4252 x_draw_glyph_string_background (s, 1);
4253 x_draw_composite_glyph_string_foreground (s);
4254 break;
4255
06a2c219
GM
4256 default:
4257 abort ();
4258 }
4259
66ac4b0e 4260 if (!s->for_overlaps_p)
06a2c219 4261 {
66ac4b0e
GM
4262 /* Draw underline. */
4263 if (s->face->underline_p)
4264 {
e24e84cc
GM
4265 unsigned long tem, h;
4266 int y;
06a2c219 4267
e24e84cc 4268 /* Get the underline thickness. Default is 1 pixel. */
66ac4b0e
GM
4269 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4270 h = 1;
e24e84cc
GM
4271
4272 /* Get the underline position. This is the recommended
4273 vertical offset in pixels from the baseline to the top of
4274 the underline. This is a signed value according to the
4275 specs, and its default is
4276
4277 ROUND ((maximum descent) / 2), with
4278 ROUND(x) = floor (x + 0.5) */
4279
4280 if (XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
4281 y = s->ybase + (long) tem;
4282 else if (s->face->font)
4283 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4284 else
4285 y = s->height - h;
06a2c219 4286
66ac4b0e 4287 if (s->face->underline_defaulted_p)
e24e84cc
GM
4288 XFillRectangle (s->display, s->window, s->gc,
4289 s->x, y, s->width, h);
66ac4b0e
GM
4290 else
4291 {
4292 XGCValues xgcv;
4293 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4294 XSetForeground (s->display, s->gc, s->face->underline_color);
e24e84cc
GM
4295 XFillRectangle (s->display, s->window, s->gc,
4296 s->x, y, s->width, h);
66ac4b0e
GM
4297 XSetForeground (s->display, s->gc, xgcv.foreground);
4298 }
dc6f92b8 4299 }
07e34cb0 4300
66ac4b0e
GM
4301 /* Draw overline. */
4302 if (s->face->overline_p)
06a2c219 4303 {
66ac4b0e
GM
4304 unsigned long dy = 0, h = 1;
4305
4306 if (s->face->overline_color_defaulted_p)
4307 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4308 s->width, h);
4309 else
4310 {
4311 XGCValues xgcv;
4312 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4313 XSetForeground (s->display, s->gc, s->face->overline_color);
4314 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4315 s->width, h);
4316 XSetForeground (s->display, s->gc, xgcv.foreground);
4317 }
06a2c219 4318 }
06a2c219 4319
66ac4b0e
GM
4320 /* Draw strike-through. */
4321 if (s->face->strike_through_p)
06a2c219 4322 {
66ac4b0e
GM
4323 unsigned long h = 1;
4324 unsigned long dy = (s->height - h) / 2;
4325
4326 if (s->face->strike_through_color_defaulted_p)
4327 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4328 s->width, h);
4329 else
4330 {
4331 XGCValues xgcv;
4332 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4333 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4334 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4335 s->width, h);
4336 XSetForeground (s->display, s->gc, xgcv.foreground);
4337 }
06a2c219 4338 }
06a2c219 4339
66ac4b0e
GM
4340 /* Draw relief. */
4341 if (s->face->box != FACE_NO_BOX)
4342 x_draw_glyph_string_box (s);
4343 }
06a2c219
GM
4344
4345 /* Reset clipping. */
4346 XSetClipMask (s->display, s->gc, None);
dc6f92b8 4347}
07e34cb0 4348
06a2c219 4349
b4192550
KH
4350static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4351 struct face **, int));
06a2c219 4352
06a2c219 4353
209f68d9
GM
4354/* Fill glyph string S with composition components specified by S->cmp.
4355
b4192550
KH
4356 FACES is an array of faces for all components of this composition.
4357 S->gidx is the index of the first component for S.
4358 OVERLAPS_P non-zero means S should draw the foreground only, and
209f68d9 4359 use its physical height for clipping.
06a2c219 4360
b4192550 4361 Value is the index of a component not in S. */
07e34cb0 4362
b4192550
KH
4363static int
4364x_fill_composite_glyph_string (s, faces, overlaps_p)
06a2c219 4365 struct glyph_string *s;
b4192550 4366 struct face **faces;
66ac4b0e 4367 int overlaps_p;
07e34cb0 4368{
b4192550 4369 int i;
06a2c219 4370
b4192550 4371 xassert (s);
06a2c219 4372
b4192550 4373 s->for_overlaps_p = overlaps_p;
06a2c219 4374
b4192550
KH
4375 s->face = faces[s->gidx];
4376 s->font = s->face->font;
4377 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
06a2c219 4378
b4192550
KH
4379 /* For all glyphs of this composition, starting at the offset
4380 S->gidx, until we reach the end of the definition or encounter a
4381 glyph that requires the different face, add it to S. */
4382 ++s->nchars;
4383 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4384 ++s->nchars;
06a2c219 4385
b4192550
KH
4386 /* All glyph strings for the same composition has the same width,
4387 i.e. the width set for the first component of the composition. */
06a2c219 4388
06a2c219
GM
4389 s->width = s->first_glyph->pixel_width;
4390
4391 /* If the specified font could not be loaded, use the frame's
4392 default font, but record the fact that we couldn't load it in
4393 the glyph string so that we can draw rectangles for the
4394 characters of the glyph string. */
4395 if (s->font == NULL)
4396 {
4397 s->font_not_found_p = 1;
4398 s->font = FRAME_FONT (s->f);
4399 }
4400
4401 /* Adjust base line for subscript/superscript text. */
4402 s->ybase += s->first_glyph->voffset;
4403
4404 xassert (s->face && s->face->gc);
4405
4406 /* This glyph string must always be drawn with 16-bit functions. */
4407 s->two_byte_p = 1;
b4192550
KH
4408
4409 return s->gidx + s->nchars;
06a2c219
GM
4410}
4411
4412
209f68d9
GM
4413/* Fill glyph string S from a sequence of character glyphs.
4414
06a2c219 4415 FACE_ID is the face id of the string. START is the index of the
66ac4b0e
GM
4416 first glyph to consider, END is the index of the last + 1.
4417 OVERLAPS_P non-zero means S should draw the foreground only, and
209f68d9 4418 use its physical height for clipping.
66ac4b0e
GM
4419
4420 Value is the index of the first glyph not in S. */
06a2c219
GM
4421
4422static int
66ac4b0e 4423x_fill_glyph_string (s, face_id, start, end, overlaps_p)
06a2c219
GM
4424 struct glyph_string *s;
4425 int face_id;
66ac4b0e 4426 int start, end, overlaps_p;
06a2c219
GM
4427{
4428 struct glyph *glyph, *last;
4429 int voffset;
ee569018 4430 int glyph_not_available_p;
06a2c219 4431
06a2c219
GM
4432 xassert (s->f == XFRAME (s->w->frame));
4433 xassert (s->nchars == 0);
4434 xassert (start >= 0 && end > start);
4435
66ac4b0e 4436 s->for_overlaps_p = overlaps_p,
06a2c219
GM
4437 glyph = s->row->glyphs[s->area] + start;
4438 last = s->row->glyphs[s->area] + end;
4439 voffset = glyph->voffset;
4440
ee569018
KH
4441 glyph_not_available_p = glyph->glyph_not_available_p;
4442
06a2c219
GM
4443 while (glyph < last
4444 && glyph->type == CHAR_GLYPH
4445 && glyph->voffset == voffset
ee569018
KH
4446 /* Same face id implies same font, nowadays. */
4447 && glyph->face_id == face_id
4448 && glyph->glyph_not_available_p == glyph_not_available_p)
06a2c219 4449 {
ee569018
KH
4450 int two_byte_p;
4451
06a2c219 4452 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
ee569018
KH
4453 s->char2b + s->nchars,
4454 &two_byte_p);
4455 s->two_byte_p = two_byte_p;
06a2c219
GM
4456 ++s->nchars;
4457 xassert (s->nchars <= end - start);
4458 s->width += glyph->pixel_width;
4459 ++glyph;
4460 }
4461
4462 s->font = s->face->font;
4463 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4464
4465 /* If the specified font could not be loaded, use the frame's font,
4466 but record the fact that we couldn't load it in
4467 S->font_not_found_p so that we can draw rectangles for the
4468 characters of the glyph string. */
ee569018 4469 if (s->font == NULL || glyph_not_available_p)
06a2c219
GM
4470 {
4471 s->font_not_found_p = 1;
4472 s->font = FRAME_FONT (s->f);
4473 }
4474
4475 /* Adjust base line for subscript/superscript text. */
4476 s->ybase += voffset;
66ac4b0e 4477
06a2c219
GM
4478 xassert (s->face && s->face->gc);
4479 return glyph - s->row->glyphs[s->area];
07e34cb0 4480}
dc6f92b8 4481
06a2c219
GM
4482
4483/* Fill glyph string S from image glyph S->first_glyph. */
dc6f92b8 4484
dfcf069d 4485static void
06a2c219
GM
4486x_fill_image_glyph_string (s)
4487 struct glyph_string *s;
4488{
4489 xassert (s->first_glyph->type == IMAGE_GLYPH);
43d120d8 4490 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
06a2c219 4491 xassert (s->img);
43d120d8 4492 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
06a2c219
GM
4493 s->font = s->face->font;
4494 s->width = s->first_glyph->pixel_width;
4495
4496 /* Adjust base line for subscript/superscript text. */
4497 s->ybase += s->first_glyph->voffset;
4498}
4499
4500
209f68d9 4501/* Fill glyph string S from a sequence of stretch glyphs.
06a2c219 4502
209f68d9
GM
4503 ROW is the glyph row in which the glyphs are found, AREA is the
4504 area within the row. START is the index of the first glyph to
4505 consider, END is the index of the last + 1.
4506
4507 Value is the index of the first glyph not in S. */
4508
4509static int
4510x_fill_stretch_glyph_string (s, row, area, start, end)
06a2c219 4511 struct glyph_string *s;
209f68d9
GM
4512 struct glyph_row *row;
4513 enum glyph_row_area area;
4514 int start, end;
06a2c219 4515{
209f68d9
GM
4516 struct glyph *glyph, *last;
4517 int voffset, face_id;
4518
06a2c219 4519 xassert (s->first_glyph->type == STRETCH_GLYPH);
209f68d9
GM
4520
4521 glyph = s->row->glyphs[s->area] + start;
4522 last = s->row->glyphs[s->area] + end;
4523 face_id = glyph->face_id;
4524 s->face = FACE_FROM_ID (s->f, face_id);
06a2c219 4525 s->font = s->face->font;
209f68d9
GM
4526 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4527 s->width = glyph->pixel_width;
4528 voffset = glyph->voffset;
4529
4530 for (++glyph;
4531 (glyph < last
4532 && glyph->type == STRETCH_GLYPH
4533 && glyph->voffset == voffset
4534 && glyph->face_id == face_id);
4535 ++glyph)
4536 s->width += glyph->pixel_width;
06a2c219
GM
4537
4538 /* Adjust base line for subscript/superscript text. */
209f68d9
GM
4539 s->ybase += voffset;
4540
4541 xassert (s->face && s->face->gc);
4542 return glyph - s->row->glyphs[s->area];
06a2c219
GM
4543}
4544
4545
4546/* Initialize glyph string S. CHAR2B is a suitably allocated vector
4547 of XChar2b structures for S; it can't be allocated in
4548 x_init_glyph_string because it must be allocated via `alloca'. W
4549 is the window on which S is drawn. ROW and AREA are the glyph row
4550 and area within the row from which S is constructed. START is the
4551 index of the first glyph structure covered by S. HL is a
4552 face-override for drawing S. */
4553
4554static void
4555x_init_glyph_string (s, char2b, w, row, area, start, hl)
4556 struct glyph_string *s;
4557 XChar2b *char2b;
4558 struct window *w;
4559 struct glyph_row *row;
4560 enum glyph_row_area area;
4561 int start;
4562 enum draw_glyphs_face hl;
4563{
4564 bzero (s, sizeof *s);
4565 s->w = w;
4566 s->f = XFRAME (w->frame);
4567 s->display = FRAME_X_DISPLAY (s->f);
4568 s->window = FRAME_X_WINDOW (s->f);
4569 s->char2b = char2b;
4570 s->hl = hl;
4571 s->row = row;
4572 s->area = area;
4573 s->first_glyph = row->glyphs[area] + start;
4574 s->height = row->height;
4575 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4576
9ea173e8
GM
4577 /* Display the internal border below the tool-bar window. */
4578 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219
GM
4579 s->y -= s->f->output_data.x->internal_border_width;
4580
4581 s->ybase = s->y + row->ascent;
4582}
4583
4584
4585/* Set background width of glyph string S. START is the index of the
4586 first glyph following S. LAST_X is the right-most x-position + 1
4587 in the drawing area. */
4588
4589static INLINE void
4590x_set_glyph_string_background_width (s, start, last_x)
4591 struct glyph_string *s;
4592 int start;
4593 int last_x;
4594{
4595 /* If the face of this glyph string has to be drawn to the end of
4596 the drawing area, set S->extends_to_end_of_line_p. */
4597 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4598
4599 if (start == s->row->used[s->area]
4600 && s->hl == DRAW_NORMAL_TEXT
4601 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4602 || s->face->background != default_face->background
4603 || s->face->stipple != default_face->stipple))
4604 s->extends_to_end_of_line_p = 1;
4605
4606 /* If S extends its face to the end of the line, set its
4607 background_width to the distance to the right edge of the drawing
4608 area. */
4609 if (s->extends_to_end_of_line_p)
1da3fd71 4610 s->background_width = last_x - s->x + 1;
06a2c219
GM
4611 else
4612 s->background_width = s->width;
4613}
4614
4615
4616/* Add a glyph string for a stretch glyph to the list of strings
4617 between HEAD and TAIL. START is the index of the stretch glyph in
4618 row area AREA of glyph row ROW. END is the index of the last glyph
4619 in that glyph row area. X is the current output position assigned
4620 to the new glyph string constructed. HL overrides that face of the
4621 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4622 is the right-most x-position of the drawing area. */
4623
8abee2e1
DL
4624/* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4625 and below -- keep them on one line. */
4626#define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4627 do \
4628 { \
4629 s = (struct glyph_string *) alloca (sizeof *s); \
4630 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
209f68d9 4631 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
06a2c219 4632 x_append_glyph_string (&HEAD, &TAIL, s); \
06a2c219
GM
4633 s->x = (X); \
4634 } \
4635 while (0)
4636
4637
4638/* Add a glyph string for an image glyph to the list of strings
4639 between HEAD and TAIL. START is the index of the image glyph in
4640 row area AREA of glyph row ROW. END is the index of the last glyph
4641 in that glyph row area. X is the current output position assigned
4642 to the new glyph string constructed. HL overrides that face of the
4643 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4644 is the right-most x-position of the drawing area. */
4645
8abee2e1 4646#define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4647 do \
4648 { \
4649 s = (struct glyph_string *) alloca (sizeof *s); \
4650 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4651 x_fill_image_glyph_string (s); \
4652 x_append_glyph_string (&HEAD, &TAIL, s); \
4653 ++START; \
4654 s->x = (X); \
4655 } \
4656 while (0)
4657
4658
4659/* Add a glyph string for a sequence of character glyphs to the list
4660 of strings between HEAD and TAIL. START is the index of the first
4661 glyph in row area AREA of glyph row ROW that is part of the new
4662 glyph string. END is the index of the last glyph in that glyph row
4663 area. X is the current output position assigned to the new glyph
4664 string constructed. HL overrides that face of the glyph; e.g. it
4665 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4666 right-most x-position of the drawing area. */
4667
8abee2e1 4668#define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4669 do \
4670 { \
3e71d8f2 4671 int c, face_id; \
06a2c219
GM
4672 XChar2b *char2b; \
4673 \
43d120d8 4674 c = (ROW)->glyphs[AREA][START].u.ch; \
43d120d8 4675 face_id = (ROW)->glyphs[AREA][START].face_id; \
06a2c219 4676 \
b4192550
KH
4677 s = (struct glyph_string *) alloca (sizeof *s); \
4678 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4679 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4680 x_append_glyph_string (&HEAD, &TAIL, s); \
b4192550
KH
4681 s->x = (X); \
4682 START = x_fill_glyph_string (s, face_id, START, END, \
66ac4b0e 4683 OVERLAPS_P); \
06a2c219
GM
4684 } \
4685 while (0)
4686
4687
b4192550
KH
4688/* Add a glyph string for a composite sequence to the list of strings
4689 between HEAD and TAIL. START is the index of the first glyph in
4690 row area AREA of glyph row ROW that is part of the new glyph
4691 string. END is the index of the last glyph in that glyph row area.
4692 X is the current output position assigned to the new glyph string
4693 constructed. HL overrides that face of the glyph; e.g. it is
4694 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4695 x-position of the drawing area. */
4696
6c27ec25 4697#define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
b4192550 4698 do { \
43d120d8
KH
4699 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4700 int face_id = (ROW)->glyphs[AREA][START].face_id; \
ee569018 4701 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
b4192550
KH
4702 struct composition *cmp = composition_table[cmp_id]; \
4703 int glyph_len = cmp->glyph_len; \
4704 XChar2b *char2b; \
4705 struct face **faces; \
4706 struct glyph_string *first_s = NULL; \
4707 int n; \
4708 \
ee569018 4709 base_face = base_face->ascii_face; \
b4192550
KH
4710 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4711 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4712 /* At first, fill in `char2b' and `faces'. */ \
4713 for (n = 0; n < glyph_len; n++) \
4714 { \
43d120d8 4715 int c = COMPOSITION_GLYPH (cmp, n); \
ee569018
KH
4716 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4717 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4718 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4719 this_face_id, char2b + n, 1); \
b4192550
KH
4720 } \
4721 \
4722 /* Make glyph_strings for each glyph sequence that is drawable by \
4723 the same face, and append them to HEAD/TAIL. */ \
4724 for (n = 0; n < cmp->glyph_len;) \
4725 { \
4726 s = (struct glyph_string *) alloca (sizeof *s); \
4727 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4728 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4729 s->cmp = cmp; \
4730 s->gidx = n; \
b4192550
KH
4731 s->x = (X); \
4732 \
4733 if (n == 0) \
4734 first_s = s; \
4735 \
4736 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4737 } \
4738 \
4739 ++START; \
4740 s = first_s; \
4741 } while (0)
4742
4743
06a2c219
GM
4744/* Build a list of glyph strings between HEAD and TAIL for the glyphs
4745 of AREA of glyph row ROW on window W between indices START and END.
4746 HL overrides the face for drawing glyph strings, e.g. it is
4747 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4748 x-positions of the drawing area.
4749
4750 This is an ugly monster macro construct because we must use alloca
4751 to allocate glyph strings (because x_draw_glyphs can be called
4752 asynchronously). */
4753
8abee2e1 4754#define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4755 do \
4756 { \
4757 HEAD = TAIL = NULL; \
4758 while (START < END) \
4759 { \
4760 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4761 switch (first_glyph->type) \
4762 { \
4763 case CHAR_GLYPH: \
4764 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
66ac4b0e
GM
4765 TAIL, HL, X, LAST_X, \
4766 OVERLAPS_P); \
06a2c219
GM
4767 break; \
4768 \
b4192550
KH
4769 case COMPOSITE_GLYPH: \
4770 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4771 HEAD, TAIL, HL, X, LAST_X,\
4772 OVERLAPS_P); \
4773 break; \
4774 \
06a2c219
GM
4775 case STRETCH_GLYPH: \
4776 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4777 HEAD, TAIL, HL, X, LAST_X); \
4778 break; \
4779 \
4780 case IMAGE_GLYPH: \
4781 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4782 TAIL, HL, X, LAST_X); \
4783 break; \
4784 \
4785 default: \
4786 abort (); \
4787 } \
4788 \
4789 x_set_glyph_string_background_width (s, START, LAST_X); \
4790 (X) += s->width; \
4791 } \
4792 } \
4793 while (0)
4794
4795
4796/* Draw glyphs between START and END in AREA of ROW on window W,
4797 starting at x-position X. X is relative to AREA in W. HL is a
4798 face-override with the following meaning:
4799
4800 DRAW_NORMAL_TEXT draw normally
4801 DRAW_CURSOR draw in cursor face
4802 DRAW_MOUSE_FACE draw in mouse face.
4803 DRAW_INVERSE_VIDEO draw in mode line face
4804 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4805 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4806
4807 If REAL_START is non-null, return in *REAL_START the real starting
4808 position for display. This can be different from START in case
4809 overlapping glyphs must be displayed. If REAL_END is non-null,
4810 return in *REAL_END the real end position for display. This can be
4811 different from END in case overlapping glyphs must be displayed.
4812
66ac4b0e
GM
4813 If OVERLAPS_P is non-zero, draw only the foreground of characters
4814 and clip to the physical height of ROW.
4815
06a2c219
GM
4816 Value is the x-position reached, relative to AREA of W. */
4817
4818static int
66ac4b0e
GM
4819x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4820 overlaps_p)
06a2c219
GM
4821 struct window *w;
4822 int x;
4823 struct glyph_row *row;
4824 enum glyph_row_area area;
4825 int start, end;
4826 enum draw_glyphs_face hl;
4827 int *real_start, *real_end;
66ac4b0e 4828 int overlaps_p;
dc6f92b8 4829{
06a2c219
GM
4830 struct glyph_string *head, *tail;
4831 struct glyph_string *s;
4832 int last_x, area_width;
4833 int x_reached;
4834 int i, j;
4835
4836 /* Let's rather be paranoid than getting a SEGV. */
06a2c219 4837 end = min (end, row->used[area]);
a8710abf
GM
4838 start = max (0, start);
4839 start = min (end, start);
06a2c219
GM
4840 if (real_start)
4841 *real_start = start;
4842 if (real_end)
4843 *real_end = end;
4844
4845 /* Translate X to frame coordinates. Set last_x to the right
4846 end of the drawing area. */
4847 if (row->full_width_p)
4848 {
4849 /* X is relative to the left edge of W, without scroll bars
4850 or flag areas. */
4851 struct frame *f = XFRAME (w->frame);
110859fc 4852 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
06a2c219 4853 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
dc6f92b8 4854
06a2c219
GM
4855 x += window_left_x;
4856 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4857 last_x = window_left_x + area_width;
4858
4859 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4860 {
110859fc 4861 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
06a2c219
GM
4862 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4863 last_x += width;
4864 else
4865 x -= width;
4866 }
dc6f92b8 4867
b9432a85
GM
4868 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4869 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
06a2c219
GM
4870 }
4871 else
dc6f92b8 4872 {
06a2c219
GM
4873 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4874 area_width = window_box_width (w, area);
4875 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
dc6f92b8
JB
4876 }
4877
06a2c219
GM
4878 /* Build a doubly-linked list of glyph_string structures between
4879 head and tail from what we have to draw. Note that the macro
4880 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4881 the reason we use a separate variable `i'. */
4882 i = start;
66ac4b0e
GM
4883 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4884 overlaps_p);
06a2c219
GM
4885 if (tail)
4886 x_reached = tail->x + tail->background_width;
4887 else
4888 x_reached = x;
90e65f07 4889
06a2c219
GM
4890 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4891 the row, redraw some glyphs in front or following the glyph
4892 strings built above. */
a8710abf 4893 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
06a2c219
GM
4894 {
4895 int dummy_x = 0;
4896 struct glyph_string *h, *t;
4897
4898 /* Compute overhangs for all glyph strings. */
4899 for (s = head; s; s = s->next)
4900 x_compute_glyph_string_overhangs (s);
4901
4902 /* Prepend glyph strings for glyphs in front of the first glyph
4903 string that are overwritten because of the first glyph
4904 string's left overhang. The background of all strings
4905 prepended must be drawn because the first glyph string
4906 draws over it. */
4907 i = x_left_overwritten (head);
4908 if (i >= 0)
4909 {
4910 j = i;
4911 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
66ac4b0e
GM
4912 DRAW_NORMAL_TEXT, dummy_x, last_x,
4913 overlaps_p);
06a2c219
GM
4914 start = i;
4915 if (real_start)
4916 *real_start = start;
4917 x_compute_overhangs_and_x (t, head->x, 1);
4918 x_prepend_glyph_string_lists (&head, &tail, h, t);
4919 }
58769bee 4920
06a2c219
GM
4921 /* Prepend glyph strings for glyphs in front of the first glyph
4922 string that overwrite that glyph string because of their
4923 right overhang. For these strings, only the foreground must
4924 be drawn, because it draws over the glyph string at `head'.
4925 The background must not be drawn because this would overwrite
4926 right overhangs of preceding glyphs for which no glyph
4927 strings exist. */
4928 i = x_left_overwriting (head);
4929 if (i >= 0)
4930 {
4931 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
66ac4b0e
GM
4932 DRAW_NORMAL_TEXT, dummy_x, last_x,
4933 overlaps_p);
06a2c219
GM
4934 for (s = h; s; s = s->next)
4935 s->background_filled_p = 1;
4936 if (real_start)
4937 *real_start = i;
4938 x_compute_overhangs_and_x (t, head->x, 1);
4939 x_prepend_glyph_string_lists (&head, &tail, h, t);
4940 }
dbcb258a 4941
06a2c219
GM
4942 /* Append glyphs strings for glyphs following the last glyph
4943 string tail that are overwritten by tail. The background of
4944 these strings has to be drawn because tail's foreground draws
4945 over it. */
4946 i = x_right_overwritten (tail);
4947 if (i >= 0)
4948 {
4949 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4950 DRAW_NORMAL_TEXT, x, last_x,
4951 overlaps_p);
06a2c219
GM
4952 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4953 x_append_glyph_string_lists (&head, &tail, h, t);
4954 if (real_end)
4955 *real_end = i;
4956 }
dc6f92b8 4957
06a2c219
GM
4958 /* Append glyph strings for glyphs following the last glyph
4959 string tail that overwrite tail. The foreground of such
4960 glyphs has to be drawn because it writes into the background
4961 of tail. The background must not be drawn because it could
4962 paint over the foreground of following glyphs. */
4963 i = x_right_overwriting (tail);
4964 if (i >= 0)
4965 {
4966 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4967 DRAW_NORMAL_TEXT, x, last_x,
4968 overlaps_p);
06a2c219
GM
4969 for (s = h; s; s = s->next)
4970 s->background_filled_p = 1;
4971 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4972 x_append_glyph_string_lists (&head, &tail, h, t);
4973 if (real_end)
4974 *real_end = i;
4975 }
4976 }
58769bee 4977
06a2c219
GM
4978 /* Draw all strings. */
4979 for (s = head; s; s = s->next)
4980 x_draw_glyph_string (s);
dc6f92b8 4981
06a2c219
GM
4982 /* Value is the x-position up to which drawn, relative to AREA of W.
4983 This doesn't include parts drawn because of overhangs. */
4984 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4985 if (!row->full_width_p)
4986 {
4987 if (area > LEFT_MARGIN_AREA)
4988 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4989 if (area > TEXT_AREA)
4990 x_reached -= window_box_width (w, TEXT_AREA);
4991 }
a8710abf 4992
06a2c219
GM
4993 return x_reached;
4994}
dc6f92b8 4995
dc6f92b8 4996
66ac4b0e
GM
4997/* Fix the display of area AREA of overlapping row ROW in window W. */
4998
4999static void
5000x_fix_overlapping_area (w, row, area)
5001 struct window *w;
5002 struct glyph_row *row;
5003 enum glyph_row_area area;
5004{
5005 int i, x;
5006
5007 BLOCK_INPUT;
5008
5009 if (area == LEFT_MARGIN_AREA)
5010 x = 0;
5011 else if (area == TEXT_AREA)
5012 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5013 else
5014 x = (window_box_width (w, LEFT_MARGIN_AREA)
5015 + window_box_width (w, TEXT_AREA));
5016
5017 for (i = 0; i < row->used[area];)
5018 {
5019 if (row->glyphs[area][i].overlaps_vertically_p)
5020 {
5021 int start = i, start_x = x;
5022
5023 do
5024 {
5025 x += row->glyphs[area][i].pixel_width;
5026 ++i;
5027 }
5028 while (i < row->used[area]
5029 && row->glyphs[area][i].overlaps_vertically_p);
5030
5031 x_draw_glyphs (w, start_x, row, area, start, i,
5032 (row->inverse_p
5033 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5034 NULL, NULL, 1);
5035 }
5036 else
5037 {
5038 x += row->glyphs[area][i].pixel_width;
5039 ++i;
5040 }
5041 }
5042
5043 UNBLOCK_INPUT;
5044}
5045
5046
06a2c219
GM
5047/* Output LEN glyphs starting at START at the nominal cursor position.
5048 Advance the nominal cursor over the text. The global variable
5049 updated_window contains the window being updated, updated_row is
5050 the glyph row being updated, and updated_area is the area of that
5051 row being updated. */
dc6f92b8 5052
06a2c219
GM
5053static void
5054x_write_glyphs (start, len)
5055 struct glyph *start;
5056 int len;
5057{
5058 int x, hpos, real_start, real_end;
d9cdbb3d 5059
06a2c219 5060 xassert (updated_window && updated_row);
dc6f92b8 5061 BLOCK_INPUT;
06a2c219
GM
5062
5063 /* Write glyphs. */
dc6f92b8 5064
06a2c219
GM
5065 hpos = start - updated_row->glyphs[updated_area];
5066 x = x_draw_glyphs (updated_window, output_cursor.x,
5067 updated_row, updated_area,
5068 hpos, hpos + len,
5069 (updated_row->inverse_p
5070 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
66ac4b0e 5071 &real_start, &real_end, 0);
b30ec466 5072
06a2c219
GM
5073 /* If we drew over the cursor, note that it is not visible any more. */
5074 note_overwritten_text_cursor (updated_window, real_start,
5075 real_end - real_start);
dc6f92b8
JB
5076
5077 UNBLOCK_INPUT;
06a2c219
GM
5078
5079 /* Advance the output cursor. */
5080 output_cursor.hpos += len;
5081 output_cursor.x = x;
dc6f92b8
JB
5082}
5083
0cdd0c9f 5084
06a2c219 5085/* Insert LEN glyphs from START at the nominal cursor position. */
0cdd0c9f 5086
06a2c219
GM
5087static void
5088x_insert_glyphs (start, len)
5089 struct glyph *start;
5090 register int len;
5091{
5092 struct frame *f;
5093 struct window *w;
5094 int line_height, shift_by_width, shifted_region_width;
5095 struct glyph_row *row;
5096 struct glyph *glyph;
5097 int frame_x, frame_y, hpos, real_start, real_end;
58769bee 5098
06a2c219 5099 xassert (updated_window && updated_row);
0cdd0c9f 5100 BLOCK_INPUT;
06a2c219
GM
5101 w = updated_window;
5102 f = XFRAME (WINDOW_FRAME (w));
5103
5104 /* Get the height of the line we are in. */
5105 row = updated_row;
5106 line_height = row->height;
5107
5108 /* Get the width of the glyphs to insert. */
5109 shift_by_width = 0;
5110 for (glyph = start; glyph < start + len; ++glyph)
5111 shift_by_width += glyph->pixel_width;
5112
5113 /* Get the width of the region to shift right. */
5114 shifted_region_width = (window_box_width (w, updated_area)
5115 - output_cursor.x
5116 - shift_by_width);
5117
5118 /* Shift right. */
5119 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
5120 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5121 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5122 f->output_data.x->normal_gc,
5123 frame_x, frame_y,
5124 shifted_region_width, line_height,
5125 frame_x + shift_by_width, frame_y);
5126
5127 /* Write the glyphs. */
5128 hpos = start - row->glyphs[updated_area];
5129 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
66ac4b0e 5130 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
06a2c219
GM
5131 note_overwritten_text_cursor (w, real_start, real_end - real_start);
5132
5133 /* Advance the output cursor. */
5134 output_cursor.hpos += len;
5135 output_cursor.x += shift_by_width;
0cdd0c9f
RS
5136 UNBLOCK_INPUT;
5137}
0cdd0c9f 5138
0cdd0c9f 5139
06a2c219
GM
5140/* Delete N glyphs at the nominal cursor position. Not implemented
5141 for X frames. */
c83febd7
RS
5142
5143static void
06a2c219
GM
5144x_delete_glyphs (n)
5145 register int n;
c83febd7 5146{
06a2c219 5147 abort ();
c83febd7
RS
5148}
5149
0cdd0c9f 5150
06a2c219
GM
5151/* Erase the current text line from the nominal cursor position
5152 (inclusive) to pixel column TO_X (exclusive). The idea is that
5153 everything from TO_X onward is already erased.
5154
5155 TO_X is a pixel position relative to updated_area of
5156 updated_window. TO_X == -1 means clear to the end of this area. */
dc6f92b8 5157
06a2c219
GM
5158static void
5159x_clear_end_of_line (to_x)
5160 int to_x;
5161{
5162 struct frame *f;
5163 struct window *w = updated_window;
5164 int max_x, min_y, max_y;
5165 int from_x, from_y, to_y;
5166
5167 xassert (updated_window && updated_row);
5168 f = XFRAME (w->frame);
5169
5170 if (updated_row->full_width_p)
5171 {
5172 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5173 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5174 && !w->pseudo_window_p)
5175 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
0cdd0c9f 5176 }
06a2c219
GM
5177 else
5178 max_x = window_box_width (w, updated_area);
5179 max_y = window_text_bottom_y (w);
dc6f92b8 5180
06a2c219
GM
5181 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5182 of window. For TO_X > 0, truncate to end of drawing area. */
5183 if (to_x == 0)
5184 return;
5185 else if (to_x < 0)
5186 to_x = max_x;
5187 else
5188 to_x = min (to_x, max_x);
dbc4e1c1 5189
06a2c219
GM
5190 to_y = min (max_y, output_cursor.y + updated_row->height);
5191
5192 /* Notice if the cursor will be cleared by this operation. */
5193 if (!updated_row->full_width_p)
5194 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
dbc4e1c1 5195
06a2c219
GM
5196 from_x = output_cursor.x;
5197
5198 /* Translate to frame coordinates. */
5199 if (updated_row->full_width_p)
5200 {
5201 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5202 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5203 }
0cdd0c9f
RS
5204 else
5205 {
06a2c219
GM
5206 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5207 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5208 }
5209
045dee35 5210 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
06a2c219
GM
5211 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5212 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5213
5214 /* Prevent inadvertently clearing to end of the X window. */
5215 if (to_x > from_x && to_y > from_y)
5216 {
5217 BLOCK_INPUT;
5218 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5219 from_x, from_y, to_x - from_x, to_y - from_y,
5220 False);
5221 UNBLOCK_INPUT;
0cdd0c9f 5222 }
0cdd0c9f 5223}
dbc4e1c1 5224
0cdd0c9f 5225
06a2c219 5226/* Clear entire frame. If updating_frame is non-null, clear that
b86bd3dd 5227 frame. Otherwise clear the selected frame. */
06a2c219
GM
5228
5229static void
5230x_clear_frame ()
0cdd0c9f 5231{
06a2c219 5232 struct frame *f;
0cdd0c9f 5233
06a2c219
GM
5234 if (updating_frame)
5235 f = updating_frame;
0cdd0c9f 5236 else
b86bd3dd 5237 f = SELECTED_FRAME ();
58769bee 5238
06a2c219
GM
5239 /* Clearing the frame will erase any cursor, so mark them all as no
5240 longer visible. */
5241 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5242 output_cursor.hpos = output_cursor.vpos = 0;
5243 output_cursor.x = -1;
5244
5245 /* We don't set the output cursor here because there will always
5246 follow an explicit cursor_to. */
5247 BLOCK_INPUT;
5248 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5249
5250 /* We have to clear the scroll bars, too. If we have changed
5251 colors or something like that, then they should be notified. */
5252 x_scroll_bar_clear (f);
0cdd0c9f 5253
06a2c219
GM
5254 XFlush (FRAME_X_DISPLAY (f));
5255 UNBLOCK_INPUT;
dc6f92b8 5256}
06a2c219
GM
5257
5258
dc6f92b8 5259\f
dbc4e1c1
JB
5260/* Invert the middle quarter of the frame for .15 sec. */
5261
06a2c219
GM
5262/* We use the select system call to do the waiting, so we have to make
5263 sure it's available. If it isn't, we just won't do visual bells. */
5264
dbc4e1c1
JB
5265#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5266
06a2c219
GM
5267
5268/* Subtract the `struct timeval' values X and Y, storing the result in
5269 *RESULT. Return 1 if the difference is negative, otherwise 0. */
dbc4e1c1
JB
5270
5271static int
5272timeval_subtract (result, x, y)
5273 struct timeval *result, x, y;
5274{
06a2c219
GM
5275 /* Perform the carry for the later subtraction by updating y. This
5276 is safer because on some systems the tv_sec member is unsigned. */
dbc4e1c1
JB
5277 if (x.tv_usec < y.tv_usec)
5278 {
5279 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5280 y.tv_usec -= 1000000 * nsec;
5281 y.tv_sec += nsec;
5282 }
06a2c219 5283
dbc4e1c1
JB
5284 if (x.tv_usec - y.tv_usec > 1000000)
5285 {
5286 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5287 y.tv_usec += 1000000 * nsec;
5288 y.tv_sec -= nsec;
5289 }
5290
06a2c219
GM
5291 /* Compute the time remaining to wait. tv_usec is certainly
5292 positive. */
dbc4e1c1
JB
5293 result->tv_sec = x.tv_sec - y.tv_sec;
5294 result->tv_usec = x.tv_usec - y.tv_usec;
5295
06a2c219
GM
5296 /* Return indication of whether the result should be considered
5297 negative. */
dbc4e1c1
JB
5298 return x.tv_sec < y.tv_sec;
5299}
dc6f92b8 5300
dfcf069d 5301void
f676886a
JB
5302XTflash (f)
5303 struct frame *f;
dc6f92b8 5304{
dbc4e1c1 5305 BLOCK_INPUT;
dc6f92b8 5306
dbc4e1c1
JB
5307 {
5308 GC gc;
dc6f92b8 5309
06a2c219
GM
5310 /* Create a GC that will use the GXxor function to flip foreground
5311 pixels into background pixels. */
dbc4e1c1
JB
5312 {
5313 XGCValues values;
dc6f92b8 5314
dbc4e1c1 5315 values.function = GXxor;
7556890b
RS
5316 values.foreground = (f->output_data.x->foreground_pixel
5317 ^ f->output_data.x->background_pixel);
58769bee 5318
334208b7 5319 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dbc4e1c1
JB
5320 GCFunction | GCForeground, &values);
5321 }
dc6f92b8 5322
dbc4e1c1 5323 {
e84e14c3
RS
5324 /* Get the height not including a menu bar widget. */
5325 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5326 /* Height of each line to flash. */
5327 int flash_height = FRAME_LINE_HEIGHT (f);
5328 /* These will be the left and right margins of the rectangles. */
5329 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5330 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5331
5332 int width;
5333
5334 /* Don't flash the area between a scroll bar and the frame
5335 edge it is next to. */
5336 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5337 {
5338 case vertical_scroll_bar_left:
5339 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5340 break;
5341
5342 case vertical_scroll_bar_right:
5343 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5344 break;
06a2c219
GM
5345
5346 default:
5347 break;
e84e14c3
RS
5348 }
5349
5350 width = flash_right - flash_left;
5351
5352 /* If window is tall, flash top and bottom line. */
5353 if (height > 3 * FRAME_LINE_HEIGHT (f))
5354 {
5355 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5356 flash_left,
5357 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5358 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5359 width, flash_height);
5360 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5361 flash_left,
5362 (height - flash_height
5363 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5364 width, flash_height);
5365 }
5366 else
5367 /* If it is short, flash it all. */
5368 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5369 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5370 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
dc6f92b8 5371
06a2c219 5372 x_flush (f);
dc6f92b8 5373
dbc4e1c1 5374 {
06a2c219 5375 struct timeval wakeup;
dc6f92b8 5376
66c30ea1 5377 EMACS_GET_TIME (wakeup);
dc6f92b8 5378
dbc4e1c1
JB
5379 /* Compute time to wait until, propagating carry from usecs. */
5380 wakeup.tv_usec += 150000;
5381 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5382 wakeup.tv_usec %= 1000000;
5383
5384 /* Keep waiting until past the time wakeup. */
5385 while (1)
5386 {
5387 struct timeval timeout;
5388
66c30ea1 5389 EMACS_GET_TIME (timeout);
dbc4e1c1
JB
5390
5391 /* In effect, timeout = wakeup - timeout.
5392 Break if result would be negative. */
5393 if (timeval_subtract (&timeout, wakeup, timeout))
5394 break;
5395
5396 /* Try to wait that long--but we might wake up sooner. */
c32cdd9a 5397 select (0, NULL, NULL, NULL, &timeout);
dbc4e1c1
JB
5398 }
5399 }
58769bee 5400
e84e14c3
RS
5401 /* If window is tall, flash top and bottom line. */
5402 if (height > 3 * FRAME_LINE_HEIGHT (f))
5403 {
5404 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5405 flash_left,
5406 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5407 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5408 width, flash_height);
5409 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5410 flash_left,
5411 (height - flash_height
5412 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5413 width, flash_height);
5414 }
5415 else
5416 /* If it is short, flash it all. */
5417 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5418 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5419 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5420
334208b7 5421 XFreeGC (FRAME_X_DISPLAY (f), gc);
06a2c219 5422 x_flush (f);
dc6f92b8 5423 }
dbc4e1c1
JB
5424 }
5425
5426 UNBLOCK_INPUT;
dc6f92b8
JB
5427}
5428
06a2c219 5429#endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
dbc4e1c1
JB
5430
5431
dc6f92b8
JB
5432/* Make audible bell. */
5433
dfcf069d 5434void
dc6f92b8
JB
5435XTring_bell ()
5436{
b86bd3dd
GM
5437 struct frame *f = SELECTED_FRAME ();
5438
5439 if (FRAME_X_DISPLAY (f))
5440 {
dbc4e1c1 5441#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
b86bd3dd
GM
5442 if (visible_bell)
5443 XTflash (f);
5444 else
dbc4e1c1 5445#endif
b86bd3dd
GM
5446 {
5447 BLOCK_INPUT;
5448 XBell (FRAME_X_DISPLAY (f), 0);
5449 XFlush (FRAME_X_DISPLAY (f));
5450 UNBLOCK_INPUT;
5451 }
dc6f92b8
JB
5452 }
5453}
06a2c219 5454
dc6f92b8 5455\f
06a2c219
GM
5456/* Specify how many text lines, from the top of the window,
5457 should be affected by insert-lines and delete-lines operations.
5458 This, and those operations, are used only within an update
5459 that is bounded by calls to x_update_begin and x_update_end. */
dc6f92b8 5460
dfcf069d 5461static void
06a2c219
GM
5462XTset_terminal_window (n)
5463 register int n;
dc6f92b8 5464{
06a2c219 5465 /* This function intentionally left blank. */
dc6f92b8
JB
5466}
5467
06a2c219
GM
5468
5469\f
5470/***********************************************************************
5471 Line Dance
5472 ***********************************************************************/
5473
5474/* Perform an insert-lines or delete-lines operation, inserting N
5475 lines or deleting -N lines at vertical position VPOS. */
5476
dfcf069d 5477static void
06a2c219
GM
5478x_ins_del_lines (vpos, n)
5479 int vpos, n;
dc6f92b8
JB
5480{
5481 abort ();
5482}
06a2c219
GM
5483
5484
5485/* Scroll part of the display as described by RUN. */
dc6f92b8 5486
dfcf069d 5487static void
06a2c219
GM
5488x_scroll_run (w, run)
5489 struct window *w;
5490 struct run *run;
dc6f92b8 5491{
06a2c219
GM
5492 struct frame *f = XFRAME (w->frame);
5493 int x, y, width, height, from_y, to_y, bottom_y;
5494
5495 /* Get frame-relative bounding box of the text display area of W,
5496 without mode lines. Include in this box the flags areas to the
5497 left and right of W. */
5498 window_box (w, -1, &x, &y, &width, &height);
110859fc
GM
5499 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5500 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
5501
5502 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5503 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5504 bottom_y = y + height;
dc6f92b8 5505
06a2c219
GM
5506 if (to_y < from_y)
5507 {
5508 /* Scrolling up. Make sure we don't copy part of the mode
5509 line at the bottom. */
5510 if (from_y + run->height > bottom_y)
5511 height = bottom_y - from_y;
5512 else
5513 height = run->height;
5514 }
dc6f92b8 5515 else
06a2c219
GM
5516 {
5517 /* Scolling down. Make sure we don't copy over the mode line.
5518 at the bottom. */
5519 if (to_y + run->height > bottom_y)
5520 height = bottom_y - to_y;
5521 else
5522 height = run->height;
5523 }
7a13e894 5524
06a2c219
GM
5525 BLOCK_INPUT;
5526
5527 /* Cursor off. Will be switched on again in x_update_window_end. */
5528 updated_window = w;
5529 x_clear_cursor (w);
5530
5531 XCopyArea (FRAME_X_DISPLAY (f),
5532 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5533 f->output_data.x->normal_gc,
5534 x, from_y,
5535 width, height,
5536 x, to_y);
5537
5538 UNBLOCK_INPUT;
5539}
dc6f92b8 5540
dc6f92b8 5541
06a2c219
GM
5542\f
5543/***********************************************************************
5544 Exposure Events
5545 ***********************************************************************/
5546
5547/* Redisplay an exposed area of frame F. X and Y are the upper-left
5548 corner of the exposed rectangle. W and H are width and height of
5549 the exposed area. All are pixel values. W or H zero means redraw
5550 the entire frame. */
dc6f92b8 5551
06a2c219
GM
5552static void
5553expose_frame (f, x, y, w, h)
5554 struct frame *f;
5555 int x, y, w, h;
dc6f92b8 5556{
06a2c219 5557 XRectangle r;
dc6f92b8 5558
06a2c219 5559 TRACE ((stderr, "expose_frame "));
dc6f92b8 5560
06a2c219
GM
5561 /* No need to redraw if frame will be redrawn soon. */
5562 if (FRAME_GARBAGED_P (f))
dc6f92b8 5563 {
06a2c219
GM
5564 TRACE ((stderr, " garbaged\n"));
5565 return;
5566 }
5567
5568 /* If basic faces haven't been realized yet, there is no point in
5569 trying to redraw anything. This can happen when we get an expose
5570 event while Emacs is starting, e.g. by moving another window. */
5571 if (FRAME_FACE_CACHE (f) == NULL
5572 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5573 {
5574 TRACE ((stderr, " no faces\n"));
5575 return;
58769bee 5576 }
06a2c219
GM
5577
5578 if (w == 0 || h == 0)
58769bee 5579 {
06a2c219
GM
5580 r.x = r.y = 0;
5581 r.width = CANON_X_UNIT (f) * f->width;
5582 r.height = CANON_Y_UNIT (f) * f->height;
dc6f92b8
JB
5583 }
5584 else
5585 {
06a2c219
GM
5586 r.x = x;
5587 r.y = y;
5588 r.width = w;
5589 r.height = h;
5590 }
5591
5592 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5593 expose_window_tree (XWINDOW (f->root_window), &r);
5594
9ea173e8 5595 if (WINDOWP (f->tool_bar_window))
06a2c219 5596 {
9ea173e8 5597 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
5598 XRectangle window_rect;
5599 XRectangle intersection_rect;
5600 int window_x, window_y, window_width, window_height;
5601
5602
5603 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5604 window_rect.x = window_x;
5605 window_rect.y = window_y;
5606 window_rect.width = window_width;
5607 window_rect.height = window_height;
5608
5609 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5610 expose_window (w, &intersection_rect);
5611 }
5612
5613#ifndef USE_X_TOOLKIT
5614 if (WINDOWP (f->menu_bar_window))
5615 {
5616 struct window *w = XWINDOW (f->menu_bar_window);
5617 XRectangle window_rect;
5618 XRectangle intersection_rect;
5619 int window_x, window_y, window_width, window_height;
5620
5621
5622 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5623 window_rect.x = window_x;
5624 window_rect.y = window_y;
5625 window_rect.width = window_width;
5626 window_rect.height = window_height;
5627
5628 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5629 expose_window (w, &intersection_rect);
dc6f92b8 5630 }
06a2c219 5631#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
5632}
5633
06a2c219
GM
5634
5635/* Redraw (parts) of all windows in the window tree rooted at W that
5636 intersect R. R contains frame pixel coordinates. */
5637
58769bee 5638static void
06a2c219
GM
5639expose_window_tree (w, r)
5640 struct window *w;
5641 XRectangle *r;
dc6f92b8 5642{
06a2c219
GM
5643 while (w)
5644 {
5645 if (!NILP (w->hchild))
5646 expose_window_tree (XWINDOW (w->hchild), r);
5647 else if (!NILP (w->vchild))
5648 expose_window_tree (XWINDOW (w->vchild), r);
5649 else
5650 {
5651 XRectangle window_rect;
5652 XRectangle intersection_rect;
5653 struct frame *f = XFRAME (w->frame);
5654 int window_x, window_y, window_width, window_height;
5655
5656 /* Frame-relative pixel rectangle of W. */
5657 window_box (w, -1, &window_x, &window_y, &window_width,
5658 &window_height);
5659 window_rect.x
5660 = (window_x
110859fc 5661 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
714dc26c 5662 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
06a2c219
GM
5663 window_rect.y = window_y;
5664 window_rect.width
5665 = (window_width
110859fc 5666 + FRAME_X_FLAGS_AREA_WIDTH (f)
06a2c219
GM
5667 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5668 window_rect.height
5669 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5670
5671 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5672 expose_window (w, &intersection_rect);
5673 }
58769bee 5674
06a2c219
GM
5675 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5676 }
5677}
58769bee 5678
dc6f92b8 5679
06a2c219
GM
5680/* Redraw the part of glyph row area AREA of glyph row ROW on window W
5681 which intersects rectangle R. R is in window-relative coordinates. */
5682
5683static void
5684expose_area (w, row, r, area)
5685 struct window *w;
5686 struct glyph_row *row;
5687 XRectangle *r;
5688 enum glyph_row_area area;
5689{
06a2c219
GM
5690 struct glyph *first = row->glyphs[area];
5691 struct glyph *end = row->glyphs[area] + row->used[area];
5692 struct glyph *last;
4bc6dcc7 5693 int first_x, start_x, x;
06a2c219 5694
6fb13182
GM
5695 if (area == TEXT_AREA && row->fill_line_p)
5696 /* If row extends face to end of line write the whole line. */
4bc6dcc7 5697 x_draw_glyphs (w, 0, row, area,
6fb13182 5698 0, row->used[area],
06a2c219 5699 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5700 NULL, NULL, 0);
6fb13182
GM
5701 else
5702 {
4bc6dcc7
GM
5703 /* Set START_X to the window-relative start position for drawing glyphs of
5704 AREA. The first glyph of the text area can be partially visible.
5705 The first glyphs of other areas cannot. */
5706 if (area == LEFT_MARGIN_AREA)
5707 start_x = 0;
5708 else if (area == TEXT_AREA)
5709 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5710 else
5711 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5712 + window_box_width (w, TEXT_AREA));
5713 x = start_x;
5714
6fb13182
GM
5715 /* Find the first glyph that must be redrawn. */
5716 while (first < end
5717 && x + first->pixel_width < r->x)
5718 {
5719 x += first->pixel_width;
5720 ++first;
5721 }
5722
5723 /* Find the last one. */
5724 last = first;
5725 first_x = x;
5726 while (last < end
5727 && x < r->x + r->width)
5728 {
5729 x += last->pixel_width;
5730 ++last;
5731 }
5732
5733 /* Repaint. */
5734 if (last > first)
4bc6dcc7 5735 x_draw_glyphs (w, first_x - start_x, row, area,
6fb13182
GM
5736 first - row->glyphs[area],
5737 last - row->glyphs[area],
5738 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5739 NULL, NULL, 0);
5740 }
06a2c219
GM
5741}
5742
58769bee 5743
06a2c219
GM
5744/* Redraw the parts of the glyph row ROW on window W intersecting
5745 rectangle R. R is in window-relative coordinates. */
dc6f92b8 5746
06a2c219
GM
5747static void
5748expose_line (w, row, r)
5749 struct window *w;
5750 struct glyph_row *row;
5751 XRectangle *r;
5752{
5753 xassert (row->enabled_p);
5754
5755 if (row->mode_line_p || w->pseudo_window_p)
5756 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5757 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5758 NULL, NULL, 0);
06a2c219
GM
5759 else
5760 {
5761 if (row->used[LEFT_MARGIN_AREA])
5762 expose_area (w, row, r, LEFT_MARGIN_AREA);
5763 if (row->used[TEXT_AREA])
5764 expose_area (w, row, r, TEXT_AREA);
5765 if (row->used[RIGHT_MARGIN_AREA])
5766 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5767 x_draw_row_bitmaps (w, row);
5768 }
5769}
dc6f92b8 5770
58769bee 5771
06a2c219
GM
5772/* Return non-zero if W's cursor intersects rectangle R. */
5773
5774static int
5775x_phys_cursor_in_rect_p (w, r)
5776 struct window *w;
5777 XRectangle *r;
5778{
5779 XRectangle cr, result;
5780 struct glyph *cursor_glyph;
5781
5782 cursor_glyph = get_phys_cursor_glyph (w);
5783 if (cursor_glyph)
5784 {
5785 cr.x = w->phys_cursor.x;
5786 cr.y = w->phys_cursor.y;
5787 cr.width = cursor_glyph->pixel_width;
5788 cr.height = w->phys_cursor_height;
5789 return x_intersect_rectangles (&cr, r, &result);
5790 }
5791 else
5792 return 0;
dc6f92b8 5793}
dc6f92b8 5794
06a2c219
GM
5795
5796/* Redraw a rectangle of window W. R is a rectangle in window
5797 relative coordinates. Call this function with input blocked. */
dc6f92b8
JB
5798
5799static void
06a2c219
GM
5800expose_window (w, r)
5801 struct window *w;
5802 XRectangle *r;
dc6f92b8 5803{
06a2c219
GM
5804 struct glyph_row *row;
5805 int y;
5806 int yb = window_text_bottom_y (w);
5807 int cursor_cleared_p;
dc6f92b8 5808
80c32bcc
GM
5809 /* If window is not yet fully initialized, do nothing. This can
5810 happen when toolkit scroll bars are used and a window is split.
5811 Reconfiguring the scroll bar will generate an expose for a newly
5812 created window. */
c7911417 5813 if (w->current_matrix == NULL || w == updated_window)
80c32bcc
GM
5814 return;
5815
06a2c219
GM
5816 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5817 r->x, r->y, r->width, r->height));
dc6f92b8 5818
06a2c219
GM
5819 /* Convert to window coordinates. */
5820 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5821 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
dc6f92b8 5822
06a2c219
GM
5823 /* Turn off the cursor. */
5824 if (!w->pseudo_window_p
5825 && x_phys_cursor_in_rect_p (w, r))
5826 {
5827 x_clear_cursor (w);
5828 cursor_cleared_p = 1;
5829 }
5830 else
5831 cursor_cleared_p = 0;
5832
5833 /* Find the first row intersecting the rectangle R. */
5834 row = w->current_matrix->rows;
5835 y = 0;
5836 while (row->enabled_p
5837 && y < yb
5838 && y + row->height < r->y)
5839 {
5840 y += row->height;
5841 ++row;
5842 }
5843
dc6f92b8 5844 /* Display the text in the rectangle, one text line at a time. */
06a2c219
GM
5845 while (row->enabled_p
5846 && y < yb
5847 && y < r->y + r->height)
5848 {
5849 expose_line (w, row, r);
5850 y += row->height;
5851 ++row;
5852 }
5853
5854 /* Display the mode line if there is one. */
5855 if (WINDOW_WANTS_MODELINE_P (w)
5856 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5857 row->enabled_p)
5858 && row->y < r->y + r->height)
5859 expose_line (w, row, r);
dc6f92b8 5860
06a2c219 5861 if (!w->pseudo_window_p)
dc6f92b8 5862 {
06a2c219
GM
5863 /* Draw border between windows. */
5864 x_draw_vertical_border (w);
5865
5866 /* Turn the cursor on again. */
5867 if (cursor_cleared_p)
5868 x_update_window_cursor (w, 1);
5869 }
5870}
dc6f92b8 5871
dc6f92b8 5872
06a2c219
GM
5873/* Determine the intersection of two rectangles R1 and R2. Return
5874 the intersection in *RESULT. Value is non-zero if RESULT is not
5875 empty. */
5876
5877static int
5878x_intersect_rectangles (r1, r2, result)
5879 XRectangle *r1, *r2, *result;
5880{
5881 XRectangle *left, *right;
5882 XRectangle *upper, *lower;
5883 int intersection_p = 0;
5884
5885 /* Rearrange so that R1 is the left-most rectangle. */
5886 if (r1->x < r2->x)
5887 left = r1, right = r2;
5888 else
5889 left = r2, right = r1;
5890
5891 /* X0 of the intersection is right.x0, if this is inside R1,
5892 otherwise there is no intersection. */
5893 if (right->x <= left->x + left->width)
5894 {
5895 result->x = right->x;
5896
5897 /* The right end of the intersection is the minimum of the
5898 the right ends of left and right. */
5899 result->width = (min (left->x + left->width, right->x + right->width)
5900 - result->x);
5901
5902 /* Same game for Y. */
5903 if (r1->y < r2->y)
5904 upper = r1, lower = r2;
5905 else
5906 upper = r2, lower = r1;
5907
5908 /* The upper end of the intersection is lower.y0, if this is inside
5909 of upper. Otherwise, there is no intersection. */
5910 if (lower->y <= upper->y + upper->height)
dc43ef94 5911 {
06a2c219
GM
5912 result->y = lower->y;
5913
5914 /* The lower end of the intersection is the minimum of the lower
5915 ends of upper and lower. */
5916 result->height = (min (lower->y + lower->height,
5917 upper->y + upper->height)
5918 - result->y);
5919 intersection_p = 1;
dc43ef94 5920 }
dc6f92b8
JB
5921 }
5922
06a2c219 5923 return intersection_p;
dc6f92b8 5924}
06a2c219
GM
5925
5926
5927
5928
dc6f92b8 5929\f
dc6f92b8 5930static void
334208b7
RS
5931frame_highlight (f)
5932 struct frame *f;
dc6f92b8 5933{
b3e1e05c
JB
5934 /* We used to only do this if Vx_no_window_manager was non-nil, but
5935 the ICCCM (section 4.1.6) says that the window's border pixmap
5936 and border pixel are window attributes which are "private to the
5937 client", so we can always change it to whatever we want. */
5938 BLOCK_INPUT;
334208b7 5939 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5940 f->output_data.x->border_pixel);
b3e1e05c 5941 UNBLOCK_INPUT;
5d46f928 5942 x_update_cursor (f, 1);
dc6f92b8
JB
5943}
5944
5945static void
334208b7
RS
5946frame_unhighlight (f)
5947 struct frame *f;
dc6f92b8 5948{
b3e1e05c
JB
5949 /* We used to only do this if Vx_no_window_manager was non-nil, but
5950 the ICCCM (section 4.1.6) says that the window's border pixmap
5951 and border pixel are window attributes which are "private to the
5952 client", so we can always change it to whatever we want. */
5953 BLOCK_INPUT;
334208b7 5954 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5955 f->output_data.x->border_tile);
b3e1e05c 5956 UNBLOCK_INPUT;
5d46f928 5957 x_update_cursor (f, 1);
dc6f92b8 5958}
dc6f92b8 5959
f676886a
JB
5960/* The focus has changed. Update the frames as necessary to reflect
5961 the new situation. Note that we can't change the selected frame
c5acd733 5962 here, because the Lisp code we are interrupting might become confused.
eb8c3be9 5963 Each event gets marked with the frame in which it occurred, so the
c5acd733 5964 Lisp code can tell when the switch took place by examining the events. */
dc6f92b8 5965
6d4238f3 5966static void
0f941935
KH
5967x_new_focus_frame (dpyinfo, frame)
5968 struct x_display_info *dpyinfo;
f676886a 5969 struct frame *frame;
dc6f92b8 5970{
0f941935 5971 struct frame *old_focus = dpyinfo->x_focus_frame;
dc6f92b8 5972
0f941935 5973 if (frame != dpyinfo->x_focus_frame)
dc6f92b8 5974 {
58769bee 5975 /* Set this before calling other routines, so that they see
f676886a 5976 the correct value of x_focus_frame. */
0f941935 5977 dpyinfo->x_focus_frame = frame;
6d4238f3
JB
5978
5979 if (old_focus && old_focus->auto_lower)
f676886a 5980 x_lower_frame (old_focus);
dc6f92b8
JB
5981
5982#if 0
f676886a 5983 selected_frame = frame;
e0c1aef2
KH
5984 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
5985 selected_frame);
f676886a
JB
5986 Fselect_window (selected_frame->selected_window);
5987 choose_minibuf_frame ();
c118dd06 5988#endif /* ! 0 */
dc6f92b8 5989
0f941935
KH
5990 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
5991 pending_autoraise_frame = dpyinfo->x_focus_frame;
0134a210
RS
5992 else
5993 pending_autoraise_frame = 0;
6d4238f3 5994 }
dc6f92b8 5995
0f941935 5996 x_frame_rehighlight (dpyinfo);
6d4238f3
JB
5997}
5998
37c2c98b
RS
5999/* Handle an event saying the mouse has moved out of an Emacs frame. */
6000
6001void
0f941935
KH
6002x_mouse_leave (dpyinfo)
6003 struct x_display_info *dpyinfo;
37c2c98b 6004{
0f941935 6005 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
37c2c98b 6006}
6d4238f3 6007
f451eb13
JB
6008/* The focus has changed, or we have redirected a frame's focus to
6009 another frame (this happens when a frame uses a surrogate
06a2c219 6010 mini-buffer frame). Shift the highlight as appropriate.
0f941935
KH
6011
6012 The FRAME argument doesn't necessarily have anything to do with which
06a2c219 6013 frame is being highlighted or un-highlighted; we only use it to find
0f941935 6014 the appropriate X display info. */
06a2c219 6015
6d4238f3 6016static void
0f941935
KH
6017XTframe_rehighlight (frame)
6018 struct frame *frame;
6d4238f3 6019{
0f941935
KH
6020 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6021}
6d4238f3 6022
0f941935
KH
6023static void
6024x_frame_rehighlight (dpyinfo)
6025 struct x_display_info *dpyinfo;
6026{
6027 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6028
6029 if (dpyinfo->x_focus_frame)
6d4238f3 6030 {
0f941935
KH
6031 dpyinfo->x_highlight_frame
6032 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6033 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6034 : dpyinfo->x_focus_frame);
6035 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
f451eb13 6036 {
0f941935
KH
6037 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6038 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
f451eb13 6039 }
dc6f92b8 6040 }
6d4238f3 6041 else
0f941935 6042 dpyinfo->x_highlight_frame = 0;
dc6f92b8 6043
0f941935 6044 if (dpyinfo->x_highlight_frame != old_highlight)
6d4238f3
JB
6045 {
6046 if (old_highlight)
f676886a 6047 frame_unhighlight (old_highlight);
0f941935
KH
6048 if (dpyinfo->x_highlight_frame)
6049 frame_highlight (dpyinfo->x_highlight_frame);
6d4238f3 6050 }
dc6f92b8 6051}
06a2c219
GM
6052
6053
dc6f92b8 6054\f
06a2c219 6055/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
dc6f92b8 6056
28430d3c
JB
6057/* Initialize mode_switch_bit and modifier_meaning. */
6058static void
334208b7
RS
6059x_find_modifier_meanings (dpyinfo)
6060 struct x_display_info *dpyinfo;
28430d3c 6061{
f689eb05 6062 int min_code, max_code;
28430d3c
JB
6063 KeySym *syms;
6064 int syms_per_code;
6065 XModifierKeymap *mods;
6066
334208b7
RS
6067 dpyinfo->meta_mod_mask = 0;
6068 dpyinfo->shift_lock_mask = 0;
6069 dpyinfo->alt_mod_mask = 0;
6070 dpyinfo->super_mod_mask = 0;
6071 dpyinfo->hyper_mod_mask = 0;
58769bee 6072
9658a521 6073#ifdef HAVE_X11R4
334208b7 6074 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
9658a521 6075#else
4a60f8c5
RS
6076 min_code = dpyinfo->display->min_keycode;
6077 max_code = dpyinfo->display->max_keycode;
9658a521
JB
6078#endif
6079
334208b7 6080 syms = XGetKeyboardMapping (dpyinfo->display,
28430d3c
JB
6081 min_code, max_code - min_code + 1,
6082 &syms_per_code);
334208b7 6083 mods = XGetModifierMapping (dpyinfo->display);
28430d3c 6084
58769bee 6085 /* Scan the modifier table to see which modifier bits the Meta and
11edeb03 6086 Alt keysyms are on. */
28430d3c 6087 {
06a2c219 6088 int row, col; /* The row and column in the modifier table. */
28430d3c
JB
6089
6090 for (row = 3; row < 8; row++)
6091 for (col = 0; col < mods->max_keypermod; col++)
6092 {
0299d313
RS
6093 KeyCode code
6094 = mods->modifiermap[(row * mods->max_keypermod) + col];
28430d3c 6095
af92970c
KH
6096 /* Zeroes are used for filler. Skip them. */
6097 if (code == 0)
6098 continue;
6099
28430d3c
JB
6100 /* Are any of this keycode's keysyms a meta key? */
6101 {
6102 int code_col;
6103
6104 for (code_col = 0; code_col < syms_per_code; code_col++)
6105 {
f689eb05 6106 int sym = syms[((code - min_code) * syms_per_code) + code_col];
28430d3c 6107
f689eb05 6108 switch (sym)
28430d3c 6109 {
f689eb05
JB
6110 case XK_Meta_L:
6111 case XK_Meta_R:
334208b7 6112 dpyinfo->meta_mod_mask |= (1 << row);
28430d3c 6113 break;
f689eb05
JB
6114
6115 case XK_Alt_L:
6116 case XK_Alt_R:
334208b7 6117 dpyinfo->alt_mod_mask |= (1 << row);
a3c44b14
RS
6118 break;
6119
6120 case XK_Hyper_L:
6121 case XK_Hyper_R:
334208b7 6122 dpyinfo->hyper_mod_mask |= (1 << row);
a3c44b14
RS
6123 break;
6124
6125 case XK_Super_L:
6126 case XK_Super_R:
334208b7 6127 dpyinfo->super_mod_mask |= (1 << row);
f689eb05 6128 break;
11edeb03
JB
6129
6130 case XK_Shift_Lock:
6131 /* Ignore this if it's not on the lock modifier. */
6132 if ((1 << row) == LockMask)
334208b7 6133 dpyinfo->shift_lock_mask = LockMask;
11edeb03 6134 break;
28430d3c
JB
6135 }
6136 }
6137 }
6138 }
6139 }
6140
f689eb05 6141 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
334208b7 6142 if (! dpyinfo->meta_mod_mask)
a3c44b14 6143 {
334208b7
RS
6144 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6145 dpyinfo->alt_mod_mask = 0;
a3c44b14 6146 }
f689eb05 6147
148c4b70
RS
6148 /* If some keys are both alt and meta,
6149 make them just meta, not alt. */
334208b7 6150 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
148c4b70 6151 {
334208b7 6152 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
148c4b70 6153 }
58769bee 6154
28430d3c 6155 XFree ((char *) syms);
f689eb05 6156 XFreeModifiermap (mods);
28430d3c
JB
6157}
6158
dfeccd2d
JB
6159/* Convert between the modifier bits X uses and the modifier bits
6160 Emacs uses. */
06a2c219 6161
7c5283e4 6162static unsigned int
334208b7
RS
6163x_x_to_emacs_modifiers (dpyinfo, state)
6164 struct x_display_info *dpyinfo;
dc6f92b8
JB
6165 unsigned int state;
6166{
334208b7
RS
6167 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6168 | ((state & ControlMask) ? ctrl_modifier : 0)
6169 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
6170 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
6171 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
6172 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
dc6f92b8
JB
6173}
6174
dfeccd2d 6175static unsigned int
334208b7
RS
6176x_emacs_to_x_modifiers (dpyinfo, state)
6177 struct x_display_info *dpyinfo;
dfeccd2d
JB
6178 unsigned int state;
6179{
334208b7
RS
6180 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
6181 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
6182 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
6183 | ((state & shift_modifier) ? ShiftMask : 0)
6184 | ((state & ctrl_modifier) ? ControlMask : 0)
6185 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
dfeccd2d 6186}
d047c4eb
KH
6187
6188/* Convert a keysym to its name. */
6189
6190char *
6191x_get_keysym_name (keysym)
6192 KeySym keysym;
6193{
6194 char *value;
6195
6196 BLOCK_INPUT;
6197 value = XKeysymToString (keysym);
6198 UNBLOCK_INPUT;
6199
6200 return value;
6201}
06a2c219
GM
6202
6203
e4571a43
JB
6204\f
6205/* Mouse clicks and mouse movement. Rah. */
e4571a43 6206
06a2c219
GM
6207/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6208 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6209 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6210 not force the value into range. */
69388238 6211
c8dba240 6212void
69388238 6213pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
e4571a43 6214 FRAME_PTR f;
69388238 6215 register int pix_x, pix_y;
e4571a43
JB
6216 register int *x, *y;
6217 XRectangle *bounds;
69388238 6218 int noclip;
e4571a43 6219{
06a2c219 6220 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
69388238
RS
6221 even for negative values. */
6222 if (pix_x < 0)
7556890b 6223 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
69388238 6224 if (pix_y < 0)
7556890b 6225 pix_y -= (f)->output_data.x->line_height - 1;
69388238 6226
e4571a43
JB
6227 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6228 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6229
6230 if (bounds)
6231 {
7556890b
RS
6232 bounds->width = FONT_WIDTH (f->output_data.x->font);
6233 bounds->height = f->output_data.x->line_height;
e4571a43
JB
6234 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6235 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6236 }
6237
69388238
RS
6238 if (!noclip)
6239 {
6240 if (pix_x < 0)
6241 pix_x = 0;
3cbd2e0b
RS
6242 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6243 pix_x = FRAME_WINDOW_WIDTH (f);
69388238
RS
6244
6245 if (pix_y < 0)
6246 pix_y = 0;
6247 else if (pix_y > f->height)
6248 pix_y = f->height;
6249 }
e4571a43
JB
6250
6251 *x = pix_x;
6252 *y = pix_y;
6253}
6254
06a2c219
GM
6255
6256/* Given HPOS/VPOS in the current matrix of W, return corresponding
6257 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6258 can't tell the positions because W's display is not up to date,
6259 return 0. */
6260
6261int
6262glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6263 struct window *w;
6264 int hpos, vpos;
6265 int *frame_x, *frame_y;
2b5c9e71 6266{
06a2c219
GM
6267 int success_p;
6268
6269 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6270 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6271
6272 if (display_completed)
6273 {
6274 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6275 struct glyph *glyph = row->glyphs[TEXT_AREA];
6276 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6277
6278 *frame_y = row->y;
6279 *frame_x = row->x;
6280 while (glyph < end)
6281 {
6282 *frame_x += glyph->pixel_width;
6283 ++glyph;
6284 }
6285
6286 success_p = 1;
6287 }
6288 else
6289 {
6290 *frame_y = *frame_x = 0;
6291 success_p = 0;
6292 }
6293
6294 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6295 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6296 return success_p;
2b5c9e71
RS
6297}
6298
06a2c219 6299
dc6f92b8
JB
6300/* Prepare a mouse-event in *RESULT for placement in the input queue.
6301
6302 If the event is a button press, then note that we have grabbed
f451eb13 6303 the mouse. */
dc6f92b8
JB
6304
6305static Lisp_Object
f451eb13 6306construct_mouse_click (result, event, f)
dc6f92b8
JB
6307 struct input_event *result;
6308 XButtonEvent *event;
f676886a 6309 struct frame *f;
dc6f92b8 6310{
f451eb13 6311 /* Make the event type no_event; we'll change that when we decide
dc6f92b8 6312 otherwise. */
f451eb13 6313 result->kind = mouse_click;
69388238 6314 result->code = event->button - Button1;
1113d9db 6315 result->timestamp = event->time;
334208b7
RS
6316 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6317 event->state)
f689eb05 6318 | (event->type == ButtonRelease
58769bee 6319 ? up_modifier
f689eb05 6320 : down_modifier));
dc6f92b8 6321
06a2c219
GM
6322 XSETINT (result->x, event->x);
6323 XSETINT (result->y, event->y);
6324 XSETFRAME (result->frame_or_window, f);
0f8aabe9 6325 result->arg = Qnil;
06a2c219 6326 return Qnil;
dc6f92b8 6327}
b849c413 6328
69388238 6329\f
90e65f07
JB
6330/* Function to report a mouse movement to the mainstream Emacs code.
6331 The input handler calls this.
6332
6333 We have received a mouse movement event, which is given in *event.
6334 If the mouse is over a different glyph than it was last time, tell
6335 the mainstream emacs code by setting mouse_moved. If not, ask for
6336 another motion event, so we can check again the next time it moves. */
b8009dd1 6337
06a2c219
GM
6338static XMotionEvent last_mouse_motion_event;
6339static Lisp_Object last_mouse_motion_frame;
6340
90e65f07 6341static void
12ba150f 6342note_mouse_movement (frame, event)
f676886a 6343 FRAME_PTR frame;
90e65f07 6344 XMotionEvent *event;
90e65f07 6345{
e5d77022 6346 last_mouse_movement_time = event->time;
06a2c219
GM
6347 last_mouse_motion_event = *event;
6348 XSETFRAME (last_mouse_motion_frame, frame);
e5d77022 6349
27f338af
RS
6350 if (event->window != FRAME_X_WINDOW (frame))
6351 {
39d8bb4d 6352 frame->mouse_moved = 1;
27f338af 6353 last_mouse_scroll_bar = Qnil;
27f338af 6354 note_mouse_highlight (frame, -1, -1);
27f338af
RS
6355 }
6356
90e65f07 6357 /* Has the mouse moved off the glyph it was on at the last sighting? */
27f338af
RS
6358 else if (event->x < last_mouse_glyph.x
6359 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6360 || event->y < last_mouse_glyph.y
6361 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
12ba150f 6362 {
39d8bb4d 6363 frame->mouse_moved = 1;
ab648270 6364 last_mouse_scroll_bar = Qnil;
b8009dd1 6365 note_mouse_highlight (frame, event->x, event->y);
90e65f07
JB
6366 }
6367}
6368
bf1c0ba1 6369/* This is used for debugging, to turn off note_mouse_highlight. */
bf1c0ba1 6370
06a2c219
GM
6371 int disable_mouse_highlight;
6372
6373
6374\f
6375/************************************************************************
6376 Mouse Face
6377 ************************************************************************/
6378
6379/* Find the glyph under window-relative coordinates X/Y in window W.
6380 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6381 strings. Return in *HPOS and *VPOS the row and column number of
6382 the glyph found. Return in *AREA the glyph area containing X.
6383 Value is a pointer to the glyph found or null if X/Y is not on
6384 text, or we can't tell because W's current matrix is not up to
6385 date. */
6386
6387static struct glyph *
6388x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6389 struct window *w;
6390 int x, y;
6391 int *hpos, *vpos, *area;
6392{
6393 struct glyph *glyph, *end;
3e71d8f2 6394 struct glyph_row *row = NULL;
06a2c219
GM
6395 int x0, i, left_area_width;
6396
6397 /* Find row containing Y. Give up if some row is not enabled. */
6398 for (i = 0; i < w->current_matrix->nrows; ++i)
6399 {
6400 row = MATRIX_ROW (w->current_matrix, i);
6401 if (!row->enabled_p)
6402 return NULL;
6403 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6404 break;
6405 }
6406
6407 *vpos = i;
6408 *hpos = 0;
6409
6410 /* Give up if Y is not in the window. */
6411 if (i == w->current_matrix->nrows)
6412 return NULL;
6413
6414 /* Get the glyph area containing X. */
6415 if (w->pseudo_window_p)
6416 {
6417 *area = TEXT_AREA;
6418 x0 = 0;
6419 }
6420 else
6421 {
6422 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6423 if (x < left_area_width)
6424 {
6425 *area = LEFT_MARGIN_AREA;
6426 x0 = 0;
6427 }
6428 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6429 {
6430 *area = TEXT_AREA;
6431 x0 = row->x + left_area_width;
6432 }
6433 else
6434 {
6435 *area = RIGHT_MARGIN_AREA;
6436 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6437 }
6438 }
6439
6440 /* Find glyph containing X. */
6441 glyph = row->glyphs[*area];
6442 end = glyph + row->used[*area];
6443 while (glyph < end)
6444 {
6445 if (x < x0 + glyph->pixel_width)
6446 {
6447 if (w->pseudo_window_p)
6448 break;
6449 else if (BUFFERP (glyph->object))
6450 break;
6451 }
6452
6453 x0 += glyph->pixel_width;
6454 ++glyph;
6455 }
6456
6457 if (glyph == end)
6458 return NULL;
6459
6460 *hpos = glyph - row->glyphs[*area];
6461 return glyph;
6462}
6463
6464
6465/* Convert frame-relative x/y to coordinates relative to window W.
6466 Takes pseudo-windows into account. */
6467
6468static void
6469frame_to_window_pixel_xy (w, x, y)
6470 struct window *w;
6471 int *x, *y;
6472{
6473 if (w->pseudo_window_p)
6474 {
6475 /* A pseudo-window is always full-width, and starts at the
6476 left edge of the frame, plus a frame border. */
6477 struct frame *f = XFRAME (w->frame);
6478 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6479 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6480 }
6481 else
6482 {
6483 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6484 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6485 }
6486}
6487
6488
e371a781 6489/* Take proper action when mouse has moved to the mode or header line of
06a2c219
GM
6490 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6491 mode line. X is relative to the start of the text display area of
6492 W, so the width of bitmap areas and scroll bars must be subtracted
6493 to get a position relative to the start of the mode line. */
6494
6495static void
6496note_mode_line_highlight (w, x, mode_line_p)
6497 struct window *w;
6498 int x, mode_line_p;
6499{
6500 struct frame *f = XFRAME (w->frame);
6501 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6502 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6503 struct glyph_row *row;
6504
6505 if (mode_line_p)
6506 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6507 else
045dee35 6508 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
e371a781 6509
06a2c219
GM
6510 if (row->enabled_p)
6511 {
6512 struct glyph *glyph, *end;
6513 Lisp_Object help, map;
6514 int x0;
6515
6516 /* Find the glyph under X. */
6517 glyph = row->glyphs[TEXT_AREA];
6518 end = glyph + row->used[TEXT_AREA];
6519 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
110859fc 6520 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
e371a781 6521
06a2c219
GM
6522 while (glyph < end
6523 && x >= x0 + glyph->pixel_width)
6524 {
6525 x0 += glyph->pixel_width;
6526 ++glyph;
6527 }
6528
6529 if (glyph < end
6530 && STRINGP (glyph->object)
6531 && XSTRING (glyph->object)->intervals
6532 && glyph->charpos >= 0
6533 && glyph->charpos < XSTRING (glyph->object)->size)
6534 {
6535 /* If we're on a string with `help-echo' text property,
6536 arrange for the help to be displayed. This is done by
6537 setting the global variable help_echo to the help string. */
6538 help = Fget_text_property (make_number (glyph->charpos),
6539 Qhelp_echo, glyph->object);
b7e80413 6540 if (!NILP (help))
be010514
GM
6541 {
6542 help_echo = help;
7cea38bc 6543 XSETWINDOW (help_echo_window, w);
be010514
GM
6544 help_echo_object = glyph->object;
6545 help_echo_pos = glyph->charpos;
6546 }
06a2c219
GM
6547
6548 /* Change the mouse pointer according to what is under X/Y. */
6549 map = Fget_text_property (make_number (glyph->charpos),
6550 Qlocal_map, glyph->object);
02067692 6551 if (KEYMAPP (map))
06a2c219 6552 cursor = f->output_data.x->nontext_cursor;
be010514
GM
6553 else
6554 {
6555 map = Fget_text_property (make_number (glyph->charpos),
6556 Qkeymap, glyph->object);
02067692 6557 if (KEYMAPP (map))
be010514
GM
6558 cursor = f->output_data.x->nontext_cursor;
6559 }
06a2c219
GM
6560 }
6561 }
6562
6563 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6564}
6565
6566
6567/* Take proper action when the mouse has moved to position X, Y on
6568 frame F as regards highlighting characters that have mouse-face
6569 properties. Also de-highlighting chars where the mouse was before.
27f338af 6570 X and Y can be negative or out of range. */
b8009dd1
RS
6571
6572static void
6573note_mouse_highlight (f, x, y)
06a2c219 6574 struct frame *f;
c32cdd9a 6575 int x, y;
b8009dd1 6576{
06a2c219
GM
6577 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6578 int portion;
b8009dd1
RS
6579 Lisp_Object window;
6580 struct window *w;
6581
06a2c219
GM
6582 /* When a menu is active, don't highlight because this looks odd. */
6583#ifdef USE_X_TOOLKIT
6584 if (popup_activated ())
6585 return;
6586#endif
6587
04fff9c0
GM
6588 if (disable_mouse_highlight
6589 || !f->glyphs_initialized_p)
bf1c0ba1
RS
6590 return;
6591
06a2c219
GM
6592 dpyinfo->mouse_face_mouse_x = x;
6593 dpyinfo->mouse_face_mouse_y = y;
6594 dpyinfo->mouse_face_mouse_frame = f;
b8009dd1 6595
06a2c219 6596 if (dpyinfo->mouse_face_defer)
b8009dd1
RS
6597 return;
6598
514e4681
RS
6599 if (gc_in_progress)
6600 {
06a2c219 6601 dpyinfo->mouse_face_deferred_gc = 1;
514e4681
RS
6602 return;
6603 }
6604
b8009dd1 6605 /* Which window is that in? */
06a2c219 6606 window = window_from_coordinates (f, x, y, &portion, 1);
b8009dd1
RS
6607
6608 /* If we were displaying active text in another window, clear that. */
06a2c219
GM
6609 if (! EQ (window, dpyinfo->mouse_face_window))
6610 clear_mouse_face (dpyinfo);
6611
6612 /* Not on a window -> return. */
6613 if (!WINDOWP (window))
6614 return;
6615
6616 /* Convert to window-relative pixel coordinates. */
6617 w = XWINDOW (window);
6618 frame_to_window_pixel_xy (w, &x, &y);
6619
9ea173e8 6620 /* Handle tool-bar window differently since it doesn't display a
06a2c219 6621 buffer. */
9ea173e8 6622 if (EQ (window, f->tool_bar_window))
06a2c219 6623 {
9ea173e8 6624 note_tool_bar_highlight (f, x, y);
06a2c219
GM
6625 return;
6626 }
6627
6628 if (portion == 1 || portion == 3)
6629 {
6630 /* Mouse is on the mode or top line. */
6631 note_mode_line_highlight (w, x, portion == 1);
6632 return;
6633 }
e371a781
GM
6634 else if (portion == 2)
6635 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6636 f->output_data.x->horizontal_drag_cursor);
06a2c219
GM
6637 else
6638 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6639 f->output_data.x->text_cursor);
b8009dd1 6640
0cdd0c9f
RS
6641 /* Are we in a window whose display is up to date?
6642 And verify the buffer's text has not changed. */
06a2c219
GM
6643 if (/* Within text portion of the window. */
6644 portion == 0
0cdd0c9f 6645 && EQ (w->window_end_valid, w->buffer)
26459b28
KH
6646 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6647 && (XFASTINT (w->last_overlay_modified)
6648 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
b8009dd1 6649 {
06a2c219
GM
6650 int hpos, vpos, pos, i, area;
6651 struct glyph *glyph;
b8009dd1 6652
06a2c219
GM
6653 /* Find the glyph under X/Y. */
6654 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6655
6656 /* Clear mouse face if X/Y not over text. */
6657 if (glyph == NULL
6658 || area != TEXT_AREA
6659 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
b8009dd1 6660 {
06a2c219
GM
6661 clear_mouse_face (dpyinfo);
6662 return;
6663 }
6664
6665 pos = glyph->charpos;
6666 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6667
6668 /* Check for mouse-face and help-echo. */
6669 {
6670 Lisp_Object mouse_face, overlay, position;
6671 Lisp_Object *overlay_vec;
6672 int len, noverlays;
6673 struct buffer *obuf;
6674 int obegv, ozv;
6675
6676 /* If we get an out-of-range value, return now; avoid an error. */
6677 if (pos > BUF_Z (XBUFFER (w->buffer)))
6678 return;
6679
6680 /* Make the window's buffer temporarily current for
6681 overlays_at and compute_char_face. */
6682 obuf = current_buffer;
6683 current_buffer = XBUFFER (w->buffer);
6684 obegv = BEGV;
6685 ozv = ZV;
6686 BEGV = BEG;
6687 ZV = Z;
6688
6689 /* Is this char mouse-active or does it have help-echo? */
6690 XSETINT (position, pos);
6691
6692 /* Put all the overlays we want in a vector in overlay_vec.
6693 Store the length in len. If there are more than 10, make
6694 enough space for all, and try again. */
6695 len = 10;
6696 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
bc592036 6697 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
06a2c219
GM
6698 if (noverlays > len)
6699 {
6700 len = noverlays;
6701 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
bc592036 6702 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
06a2c219 6703 }
f8349001
GM
6704
6705 /* Sort overlays into increasing priority order. */
06a2c219
GM
6706 noverlays = sort_overlays (overlay_vec, noverlays, w);
6707
6708 /* Check mouse-face highlighting. */
6709 if (! (EQ (window, dpyinfo->mouse_face_window)
6710 && vpos >= dpyinfo->mouse_face_beg_row
6711 && vpos <= dpyinfo->mouse_face_end_row
6712 && (vpos > dpyinfo->mouse_face_beg_row
6713 || hpos >= dpyinfo->mouse_face_beg_col)
6714 && (vpos < dpyinfo->mouse_face_end_row
6715 || hpos < dpyinfo->mouse_face_end_col
6716 || dpyinfo->mouse_face_past_end)))
6717 {
6718 /* Clear the display of the old active region, if any. */
6719 clear_mouse_face (dpyinfo);
6720
6721 /* Find the highest priority overlay that has a mouse-face prop. */
6722 overlay = Qnil;
f8349001 6723 for (i = noverlays - 1; i >= 0; --i)
06a2c219
GM
6724 {
6725 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6726 if (!NILP (mouse_face))
6727 {
6728 overlay = overlay_vec[i];
6729 break;
6730 }
6731 }
6732
6733 /* If no overlay applies, get a text property. */
6734 if (NILP (overlay))
6735 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6736
6737 /* Handle the overlay case. */
6738 if (! NILP (overlay))
6739 {
6740 /* Find the range of text around this char that
6741 should be active. */
6742 Lisp_Object before, after;
6743 int ignore;
6744
6745 before = Foverlay_start (overlay);
6746 after = Foverlay_end (overlay);
6747 /* Record this as the current active region. */
6748 fast_find_position (w, XFASTINT (before),
6749 &dpyinfo->mouse_face_beg_col,
6750 &dpyinfo->mouse_face_beg_row,
6751 &dpyinfo->mouse_face_beg_x,
6752 &dpyinfo->mouse_face_beg_y);
6753 dpyinfo->mouse_face_past_end
6754 = !fast_find_position (w, XFASTINT (after),
6755 &dpyinfo->mouse_face_end_col,
6756 &dpyinfo->mouse_face_end_row,
6757 &dpyinfo->mouse_face_end_x,
6758 &dpyinfo->mouse_face_end_y);
6759 dpyinfo->mouse_face_window = window;
6760 dpyinfo->mouse_face_face_id
6761 = face_at_buffer_position (w, pos, 0, 0,
6762 &ignore, pos + 1, 1);
6763
6764 /* Display it as active. */
6765 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6766 }
6767 /* Handle the text property case. */
6768 else if (! NILP (mouse_face))
6769 {
6770 /* Find the range of text around this char that
6771 should be active. */
6772 Lisp_Object before, after, beginning, end;
6773 int ignore;
6774
6775 beginning = Fmarker_position (w->start);
6776 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6777 - XFASTINT (w->window_end_pos)));
6778 before
6779 = Fprevious_single_property_change (make_number (pos + 1),
6780 Qmouse_face,
6781 w->buffer, beginning);
6782 after
6783 = Fnext_single_property_change (position, Qmouse_face,
6784 w->buffer, end);
6785 /* Record this as the current active region. */
6786 fast_find_position (w, XFASTINT (before),
6787 &dpyinfo->mouse_face_beg_col,
6788 &dpyinfo->mouse_face_beg_row,
6789 &dpyinfo->mouse_face_beg_x,
6790 &dpyinfo->mouse_face_beg_y);
6791 dpyinfo->mouse_face_past_end
6792 = !fast_find_position (w, XFASTINT (after),
6793 &dpyinfo->mouse_face_end_col,
6794 &dpyinfo->mouse_face_end_row,
6795 &dpyinfo->mouse_face_end_x,
6796 &dpyinfo->mouse_face_end_y);
6797 dpyinfo->mouse_face_window = window;
6798 dpyinfo->mouse_face_face_id
6799 = face_at_buffer_position (w, pos, 0, 0,
6800 &ignore, pos + 1, 1);
6801
6802 /* Display it as active. */
6803 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6804 }
6805 }
6806
6807 /* Look for a `help-echo' property. */
6808 {
743934db 6809 Lisp_Object help, overlay;
06a2c219
GM
6810
6811 /* Check overlays first. */
f9b5db02 6812 help = overlay = Qnil;
f8349001 6813 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
743934db
GM
6814 {
6815 overlay = overlay_vec[i];
6816 help = Foverlay_get (overlay, Qhelp_echo);
6817 }
be010514
GM
6818
6819 if (!NILP (help))
6820 {
6821 help_echo = help;
7cea38bc 6822 help_echo_window = window;
743934db 6823 help_echo_object = overlay;
be010514
GM
6824 help_echo_pos = pos;
6825 }
6826 else
6827 {
6828 /* Try text properties. */
6829 if ((STRINGP (glyph->object)
06a2c219
GM
6830 && glyph->charpos >= 0
6831 && glyph->charpos < XSTRING (glyph->object)->size)
6832 || (BUFFERP (glyph->object)
6833 && glyph->charpos >= BEGV
be010514
GM
6834 && glyph->charpos < ZV))
6835 help = Fget_text_property (make_number (glyph->charpos),
6836 Qhelp_echo, glyph->object);
06a2c219 6837
be010514
GM
6838 if (!NILP (help))
6839 {
6840 help_echo = help;
7cea38bc 6841 help_echo_window = window;
be010514
GM
6842 help_echo_object = glyph->object;
6843 help_echo_pos = glyph->charpos;
6844 }
6845 }
06a2c219
GM
6846 }
6847
6848 BEGV = obegv;
6849 ZV = ozv;
6850 current_buffer = obuf;
6851 }
6852 }
6853}
6854
6855static void
6856redo_mouse_highlight ()
6857{
6858 if (!NILP (last_mouse_motion_frame)
6859 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6860 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6861 last_mouse_motion_event.x,
6862 last_mouse_motion_event.y);
6863}
6864
6865
6866\f
6867/***********************************************************************
9ea173e8 6868 Tool-bars
06a2c219
GM
6869 ***********************************************************************/
6870
9ea173e8
GM
6871static int x_tool_bar_item P_ ((struct frame *, int, int,
6872 struct glyph **, int *, int *, int *));
06a2c219 6873
9ea173e8 6874/* Tool-bar item index of the item on which a mouse button was pressed
06a2c219
GM
6875 or -1. */
6876
9ea173e8 6877static int last_tool_bar_item;
06a2c219
GM
6878
6879
9ea173e8
GM
6880/* Get information about the tool-bar item at position X/Y on frame F.
6881 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6882 the current matrix of the tool-bar window of F, or NULL if not
6883 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8daf1204 6884 item in F->tool_bar_items. Value is
06a2c219 6885
9ea173e8 6886 -1 if X/Y is not on a tool-bar item
06a2c219
GM
6887 0 if X/Y is on the same item that was highlighted before.
6888 1 otherwise. */
6889
6890static int
9ea173e8 6891x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
06a2c219
GM
6892 struct frame *f;
6893 int x, y;
6894 struct glyph **glyph;
6895 int *hpos, *vpos, *prop_idx;
6896{
6897 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6898 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6899 int area;
6900
6901 /* Find the glyph under X/Y. */
6902 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6903 if (*glyph == NULL)
6904 return -1;
6905
9ea173e8 6906 /* Get the start of this tool-bar item's properties in
8daf1204 6907 f->tool_bar_items. */
9ea173e8 6908 if (!tool_bar_item_info (f, *glyph, prop_idx))
06a2c219
GM
6909 return -1;
6910
6911 /* Is mouse on the highlighted item? */
9ea173e8 6912 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
06a2c219
GM
6913 && *vpos >= dpyinfo->mouse_face_beg_row
6914 && *vpos <= dpyinfo->mouse_face_end_row
6915 && (*vpos > dpyinfo->mouse_face_beg_row
6916 || *hpos >= dpyinfo->mouse_face_beg_col)
6917 && (*vpos < dpyinfo->mouse_face_end_row
6918 || *hpos < dpyinfo->mouse_face_end_col
6919 || dpyinfo->mouse_face_past_end))
6920 return 0;
6921
6922 return 1;
6923}
6924
6925
9ea173e8 6926/* Handle mouse button event on the tool-bar of frame F, at
06a2c219
GM
6927 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6928 or ButtonRelase. */
6929
6930static void
9ea173e8 6931x_handle_tool_bar_click (f, button_event)
06a2c219
GM
6932 struct frame *f;
6933 XButtonEvent *button_event;
6934{
6935 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6936 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6937 int hpos, vpos, prop_idx;
6938 struct glyph *glyph;
6939 Lisp_Object enabled_p;
6940 int x = button_event->x;
6941 int y = button_event->y;
6942
9ea173e8 6943 /* If not on the highlighted tool-bar item, return. */
06a2c219 6944 frame_to_window_pixel_xy (w, &x, &y);
9ea173e8 6945 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
06a2c219
GM
6946 return;
6947
6948 /* If item is disabled, do nothing. */
8daf1204 6949 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
06a2c219
GM
6950 if (NILP (enabled_p))
6951 return;
6952
6953 if (button_event->type == ButtonPress)
6954 {
6955 /* Show item in pressed state. */
6956 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6957 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9ea173e8 6958 last_tool_bar_item = prop_idx;
06a2c219
GM
6959 }
6960 else
6961 {
6962 Lisp_Object key, frame;
6963 struct input_event event;
6964
6965 /* Show item in released state. */
6966 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6967 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6968
8daf1204 6969 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
06a2c219
GM
6970
6971 XSETFRAME (frame, f);
9ea173e8 6972 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
6973 event.frame_or_window = frame;
6974 event.arg = frame;
06a2c219
GM
6975 kbd_buffer_store_event (&event);
6976
9ea173e8 6977 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
6978 event.frame_or_window = frame;
6979 event.arg = key;
06a2c219
GM
6980 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6981 button_event->state);
6982 kbd_buffer_store_event (&event);
9ea173e8 6983 last_tool_bar_item = -1;
06a2c219
GM
6984 }
6985}
6986
6987
9ea173e8
GM
6988/* Possibly highlight a tool-bar item on frame F when mouse moves to
6989 tool-bar window-relative coordinates X/Y. Called from
06a2c219
GM
6990 note_mouse_highlight. */
6991
6992static void
9ea173e8 6993note_tool_bar_highlight (f, x, y)
06a2c219
GM
6994 struct frame *f;
6995 int x, y;
6996{
9ea173e8 6997 Lisp_Object window = f->tool_bar_window;
06a2c219
GM
6998 struct window *w = XWINDOW (window);
6999 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7000 int hpos, vpos;
7001 struct glyph *glyph;
7002 struct glyph_row *row;
5c187dee 7003 int i;
06a2c219
GM
7004 Lisp_Object enabled_p;
7005 int prop_idx;
7006 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
5c187dee 7007 int mouse_down_p, rc;
06a2c219
GM
7008
7009 /* Function note_mouse_highlight is called with negative x(y
7010 values when mouse moves outside of the frame. */
7011 if (x <= 0 || y <= 0)
7012 {
7013 clear_mouse_face (dpyinfo);
7014 return;
7015 }
7016
9ea173e8 7017 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
06a2c219
GM
7018 if (rc < 0)
7019 {
9ea173e8 7020 /* Not on tool-bar item. */
06a2c219
GM
7021 clear_mouse_face (dpyinfo);
7022 return;
7023 }
7024 else if (rc == 0)
9ea173e8 7025 /* On same tool-bar item as before. */
06a2c219 7026 goto set_help_echo;
b8009dd1 7027
06a2c219
GM
7028 clear_mouse_face (dpyinfo);
7029
9ea173e8 7030 /* Mouse is down, but on different tool-bar item? */
06a2c219
GM
7031 mouse_down_p = (dpyinfo->grabbed
7032 && f == last_mouse_frame
7033 && FRAME_LIVE_P (f));
7034 if (mouse_down_p
9ea173e8 7035 && last_tool_bar_item != prop_idx)
06a2c219
GM
7036 return;
7037
7038 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7039 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7040
9ea173e8 7041 /* If tool-bar item is not enabled, don't highlight it. */
8daf1204 7042 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
06a2c219
GM
7043 if (!NILP (enabled_p))
7044 {
7045 /* Compute the x-position of the glyph. In front and past the
7046 image is a space. We include this is the highlighted area. */
7047 row = MATRIX_ROW (w->current_matrix, vpos);
7048 for (i = x = 0; i < hpos; ++i)
7049 x += row->glyphs[TEXT_AREA][i].pixel_width;
7050
7051 /* Record this as the current active region. */
7052 dpyinfo->mouse_face_beg_col = hpos;
7053 dpyinfo->mouse_face_beg_row = vpos;
7054 dpyinfo->mouse_face_beg_x = x;
7055 dpyinfo->mouse_face_beg_y = row->y;
7056 dpyinfo->mouse_face_past_end = 0;
7057
7058 dpyinfo->mouse_face_end_col = hpos + 1;
7059 dpyinfo->mouse_face_end_row = vpos;
7060 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7061 dpyinfo->mouse_face_end_y = row->y;
7062 dpyinfo->mouse_face_window = window;
9ea173e8 7063 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
06a2c219
GM
7064
7065 /* Display it as active. */
7066 show_mouse_face (dpyinfo, draw);
7067 dpyinfo->mouse_face_image_state = draw;
b8009dd1 7068 }
06a2c219
GM
7069
7070 set_help_echo:
7071
9ea173e8 7072 /* Set help_echo to a help string.to display for this tool-bar item.
06a2c219 7073 XTread_socket does the rest. */
7cea38bc 7074 help_echo_object = help_echo_window = Qnil;
be010514 7075 help_echo_pos = -1;
8daf1204 7076 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
b7e80413 7077 if (NILP (help_echo))
8daf1204 7078 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
b8009dd1 7079}
4d73d038 7080
06a2c219
GM
7081
7082\f
7083/* Find the glyph matrix position of buffer position POS in window W.
7084 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7085 current glyphs must be up to date. If POS is above window start
7086 return (0, 0, 0, 0). If POS is after end of W, return end of
7087 last line in W. */
b8009dd1
RS
7088
7089static int
06a2c219
GM
7090fast_find_position (w, pos, hpos, vpos, x, y)
7091 struct window *w;
b8009dd1 7092 int pos;
06a2c219 7093 int *hpos, *vpos, *x, *y;
b8009dd1 7094{
b8009dd1 7095 int i;
bf1c0ba1 7096 int lastcol;
06a2c219
GM
7097 int maybe_next_line_p = 0;
7098 int line_start_position;
7099 int yb = window_text_bottom_y (w);
7100 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
7101 struct glyph_row *best_row = row;
7102 int row_vpos = 0, best_row_vpos = 0;
7103 int current_x;
7104
7105 while (row->y < yb)
b8009dd1 7106 {
06a2c219
GM
7107 if (row->used[TEXT_AREA])
7108 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7109 else
7110 line_start_position = 0;
7111
7112 if (line_start_position > pos)
b8009dd1 7113 break;
77b68646
RS
7114 /* If the position sought is the end of the buffer,
7115 don't include the blank lines at the bottom of the window. */
06a2c219
GM
7116 else if (line_start_position == pos
7117 && pos == BUF_ZV (XBUFFER (w->buffer)))
77b68646 7118 {
06a2c219 7119 maybe_next_line_p = 1;
77b68646
RS
7120 break;
7121 }
06a2c219
GM
7122 else if (line_start_position > 0)
7123 {
7124 best_row = row;
7125 best_row_vpos = row_vpos;
7126 }
4b0bb6f3
GM
7127
7128 if (row->y + row->height >= yb)
7129 break;
06a2c219
GM
7130
7131 ++row;
7132 ++row_vpos;
b8009dd1 7133 }
06a2c219
GM
7134
7135 /* Find the right column within BEST_ROW. */
7136 lastcol = 0;
7137 current_x = best_row->x;
7138 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
bf1c0ba1 7139 {
06a2c219
GM
7140 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7141 int charpos;
7142
7143 charpos = glyph->charpos;
7144 if (charpos == pos)
bf1c0ba1 7145 {
06a2c219
GM
7146 *hpos = i;
7147 *vpos = best_row_vpos;
7148 *x = current_x;
7149 *y = best_row->y;
bf1c0ba1
RS
7150 return 1;
7151 }
06a2c219 7152 else if (charpos > pos)
4d73d038 7153 break;
06a2c219
GM
7154 else if (charpos > 0)
7155 lastcol = i;
7156
7157 current_x += glyph->pixel_width;
bf1c0ba1 7158 }
b8009dd1 7159
77b68646
RS
7160 /* If we're looking for the end of the buffer,
7161 and we didn't find it in the line we scanned,
7162 use the start of the following line. */
06a2c219 7163 if (maybe_next_line_p)
77b68646 7164 {
06a2c219
GM
7165 ++best_row;
7166 ++best_row_vpos;
7167 lastcol = 0;
7168 current_x = best_row->x;
77b68646
RS
7169 }
7170
06a2c219
GM
7171 *vpos = best_row_vpos;
7172 *hpos = lastcol + 1;
7173 *x = current_x;
7174 *y = best_row->y;
b8009dd1
RS
7175 return 0;
7176}
7177
06a2c219 7178
b8009dd1
RS
7179/* Display the active region described by mouse_face_*
7180 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7181
7182static void
06a2c219 7183show_mouse_face (dpyinfo, draw)
7a13e894 7184 struct x_display_info *dpyinfo;
06a2c219 7185 enum draw_glyphs_face draw;
b8009dd1 7186{
7a13e894 7187 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
06a2c219 7188 struct frame *f = XFRAME (WINDOW_FRAME (w));
b8009dd1 7189 int i;
06a2c219
GM
7190 int cursor_off_p = 0;
7191 struct cursor_pos saved_cursor;
7192
7193 saved_cursor = output_cursor;
7194
7195 /* If window is in the process of being destroyed, don't bother
7196 to do anything. */
7197 if (w->current_matrix == NULL)
7198 goto set_x_cursor;
7199
7200 /* Recognize when we are called to operate on rows that don't exist
7201 anymore. This can happen when a window is split. */
7202 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
7203 goto set_x_cursor;
7204
7205 set_output_cursor (&w->phys_cursor);
7206
7207 /* Note that mouse_face_beg_row etc. are window relative. */
7208 for (i = dpyinfo->mouse_face_beg_row;
7209 i <= dpyinfo->mouse_face_end_row;
7210 i++)
7211 {
7212 int start_hpos, end_hpos, start_x;
7213 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
7214
7215 /* Don't do anything if row doesn't have valid contents. */
7216 if (!row->enabled_p)
7217 continue;
7218
7219 /* For all but the first row, the highlight starts at column 0. */
7220 if (i == dpyinfo->mouse_face_beg_row)
7221 {
7222 start_hpos = dpyinfo->mouse_face_beg_col;
7223 start_x = dpyinfo->mouse_face_beg_x;
7224 }
7225 else
7226 {
7227 start_hpos = 0;
7228 start_x = 0;
7229 }
7230
7231 if (i == dpyinfo->mouse_face_end_row)
7232 end_hpos = dpyinfo->mouse_face_end_col;
7233 else
7234 end_hpos = row->used[TEXT_AREA];
7235
7236 /* If the cursor's in the text we are about to rewrite, turn the
7237 cursor off. */
7238 if (!w->pseudo_window_p
7239 && i == output_cursor.vpos
7240 && output_cursor.hpos >= start_hpos - 1
7241 && output_cursor.hpos <= end_hpos)
514e4681 7242 {
06a2c219
GM
7243 x_update_window_cursor (w, 0);
7244 cursor_off_p = 1;
514e4681 7245 }
b8009dd1 7246
06a2c219 7247 if (end_hpos > start_hpos)
64f26cf5
GM
7248 {
7249 row->mouse_face_p = draw == DRAW_MOUSE_FACE;
7250 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7251 start_hpos, end_hpos, draw, NULL, NULL, 0);
7252 }
b8009dd1
RS
7253 }
7254
514e4681 7255 /* If we turned the cursor off, turn it back on. */
06a2c219
GM
7256 if (cursor_off_p)
7257 x_display_cursor (w, 1,
7258 output_cursor.hpos, output_cursor.vpos,
7259 output_cursor.x, output_cursor.y);
2729a2b5 7260
06a2c219 7261 output_cursor = saved_cursor;
fb3b7de5 7262
06a2c219
GM
7263 set_x_cursor:
7264
7265 /* Change the mouse cursor. */
7266 if (draw == DRAW_NORMAL_TEXT)
7267 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7268 f->output_data.x->text_cursor);
7269 else if (draw == DRAW_MOUSE_FACE)
334208b7 7270 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 7271 f->output_data.x->cross_cursor);
27ead1d5 7272 else
334208b7 7273 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
06a2c219 7274 f->output_data.x->nontext_cursor);
b8009dd1
RS
7275}
7276
7277/* Clear out the mouse-highlighted active region.
06a2c219 7278 Redraw it un-highlighted first. */
b8009dd1 7279
06a2c219 7280void
7a13e894
RS
7281clear_mouse_face (dpyinfo)
7282 struct x_display_info *dpyinfo;
b8009dd1 7283{
607d9f9f
GM
7284#if 0 /* This prevents redrawing tool bar items when changing from one
7285 to another while a tooltip is open, so don't do it. */
685f4368 7286 if (!NILP (tip_frame))
06a2c219 7287 return;
7948d50a 7288#endif
06a2c219 7289
7a13e894 7290 if (! NILP (dpyinfo->mouse_face_window))
06a2c219 7291 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
b8009dd1 7292
7a13e894
RS
7293 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7294 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7295 dpyinfo->mouse_face_window = Qnil;
b8009dd1 7296}
e687d06e 7297
71b8321e
GM
7298
7299/* Clear any mouse-face on window W. This function is part of the
7300 redisplay interface, and is called from try_window_id and similar
7301 functions to ensure the mouse-highlight is off. */
7302
7303static void
7304x_clear_mouse_face (w)
7305 struct window *w;
7306{
7307 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7308 Lisp_Object window;
7309
2e636f9d 7310 BLOCK_INPUT;
71b8321e
GM
7311 XSETWINDOW (window, w);
7312 if (EQ (window, dpyinfo->mouse_face_window))
7313 clear_mouse_face (dpyinfo);
2e636f9d 7314 UNBLOCK_INPUT;
71b8321e
GM
7315}
7316
7317
e687d06e
RS
7318/* Just discard the mouse face information for frame F, if any.
7319 This is used when the size of F is changed. */
7320
dfcf069d 7321void
e687d06e
RS
7322cancel_mouse_face (f)
7323 FRAME_PTR f;
7324{
7325 Lisp_Object window;
7326 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7327
7328 window = dpyinfo->mouse_face_window;
7329 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7330 {
7331 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7332 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7333 dpyinfo->mouse_face_window = Qnil;
7334 }
7335}
b8009dd1 7336\f
ab648270
JB
7337static struct scroll_bar *x_window_to_scroll_bar ();
7338static void x_scroll_bar_report_motion ();
12ba150f 7339
90e65f07 7340/* Return the current position of the mouse.
2d7fc7e8 7341 *fp should be a frame which indicates which display to ask about.
90e65f07 7342
2d7fc7e8 7343 If the mouse movement started in a scroll bar, set *fp, *bar_window,
ab648270 7344 and *part to the frame, window, and scroll bar part that the mouse
12ba150f 7345 is over. Set *x and *y to the portion and whole of the mouse's
ab648270 7346 position on the scroll bar.
12ba150f 7347
2d7fc7e8 7348 If the mouse movement started elsewhere, set *fp to the frame the
12ba150f
JB
7349 mouse is on, *bar_window to nil, and *x and *y to the character cell
7350 the mouse is over.
7351
06a2c219 7352 Set *time to the server time-stamp for the time at which the mouse
12ba150f
JB
7353 was at this position.
7354
a135645a
RS
7355 Don't store anything if we don't have a valid set of values to report.
7356
90e65f07 7357 This clears the mouse_moved flag, so we can wait for the next mouse
f5bb65ec 7358 movement. */
90e65f07
JB
7359
7360static void
1cf412ec 7361XTmouse_position (fp, insist, bar_window, part, x, y, time)
334208b7 7362 FRAME_PTR *fp;
1cf412ec 7363 int insist;
12ba150f 7364 Lisp_Object *bar_window;
ab648270 7365 enum scroll_bar_part *part;
90e65f07 7366 Lisp_Object *x, *y;
e5d77022 7367 unsigned long *time;
90e65f07 7368{
a135645a
RS
7369 FRAME_PTR f1;
7370
90e65f07
JB
7371 BLOCK_INPUT;
7372
8bcee03e 7373 if (! NILP (last_mouse_scroll_bar) && insist == 0)
334208b7 7374 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
90e65f07
JB
7375 else
7376 {
12ba150f
JB
7377 Window root;
7378 int root_x, root_y;
90e65f07 7379
12ba150f
JB
7380 Window dummy_window;
7381 int dummy;
7382
39d8bb4d
KH
7383 Lisp_Object frame, tail;
7384
7385 /* Clear the mouse-moved flag for every frame on this display. */
7386 FOR_EACH_FRAME (tail, frame)
7387 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7388 XFRAME (frame)->mouse_moved = 0;
7389
ab648270 7390 last_mouse_scroll_bar = Qnil;
12ba150f
JB
7391
7392 /* Figure out which root window we're on. */
334208b7
RS
7393 XQueryPointer (FRAME_X_DISPLAY (*fp),
7394 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
12ba150f
JB
7395
7396 /* The root window which contains the pointer. */
7397 &root,
7398
7399 /* Trash which we can't trust if the pointer is on
7400 a different screen. */
7401 &dummy_window,
7402
7403 /* The position on that root window. */
58769bee 7404 &root_x, &root_y,
12ba150f
JB
7405
7406 /* More trash we can't trust. */
7407 &dummy, &dummy,
7408
7409 /* Modifier keys and pointer buttons, about which
7410 we don't care. */
7411 (unsigned int *) &dummy);
7412
7413 /* Now we have a position on the root; find the innermost window
7414 containing the pointer. */
7415 {
7416 Window win, child;
7417 int win_x, win_y;
06a2c219 7418 int parent_x = 0, parent_y = 0;
e99db5a1 7419 int count;
12ba150f
JB
7420
7421 win = root;
69388238 7422
2d7fc7e8
RS
7423 /* XTranslateCoordinates can get errors if the window
7424 structure is changing at the same time this function
7425 is running. So at least we must not crash from them. */
7426
e99db5a1 7427 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
2d7fc7e8 7428
334208b7 7429 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
23faf38f 7430 && FRAME_LIVE_P (last_mouse_frame))
12ba150f 7431 {
69388238
RS
7432 /* If mouse was grabbed on a frame, give coords for that frame
7433 even if the mouse is now outside it. */
334208b7 7434 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
69388238 7435
12ba150f 7436 /* From-window, to-window. */
69388238 7437 root, FRAME_X_WINDOW (last_mouse_frame),
12ba150f
JB
7438
7439 /* From-position, to-position. */
7440 root_x, root_y, &win_x, &win_y,
7441
7442 /* Child of win. */
7443 &child);
69388238
RS
7444 f1 = last_mouse_frame;
7445 }
7446 else
7447 {
7448 while (1)
7449 {
334208b7 7450 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
12ba150f 7451
69388238
RS
7452 /* From-window, to-window. */
7453 root, win,
12ba150f 7454
69388238
RS
7455 /* From-position, to-position. */
7456 root_x, root_y, &win_x, &win_y,
7457
7458 /* Child of win. */
7459 &child);
7460
9af3143a 7461 if (child == None || child == win)
69388238
RS
7462 break;
7463
7464 win = child;
7465 parent_x = win_x;
7466 parent_y = win_y;
7467 }
12ba150f 7468
69388238
RS
7469 /* Now we know that:
7470 win is the innermost window containing the pointer
7471 (XTC says it has no child containing the pointer),
7472 win_x and win_y are the pointer's position in it
7473 (XTC did this the last time through), and
7474 parent_x and parent_y are the pointer's position in win's parent.
7475 (They are what win_x and win_y were when win was child.
7476 If win is the root window, it has no parent, and
7477 parent_{x,y} are invalid, but that's okay, because we'll
7478 never use them in that case.) */
7479
7480 /* Is win one of our frames? */
19126e11 7481 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
aad0f6ab
GM
7482
7483#ifdef USE_X_TOOLKIT
7484 /* If we end up with the menu bar window, say it's not
7485 on the frame. */
7486 if (f1 != NULL
7487 && f1->output_data.x->menubar_widget
7488 && win == XtWindow (f1->output_data.x->menubar_widget))
7489 f1 = NULL;
7490#endif /* USE_X_TOOLKIT */
69388238 7491 }
58769bee 7492
2d7fc7e8
RS
7493 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7494 f1 = 0;
7495
e99db5a1 7496 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
2d7fc7e8 7497
ab648270 7498 /* If not, is it one of our scroll bars? */
a135645a 7499 if (! f1)
12ba150f 7500 {
ab648270 7501 struct scroll_bar *bar = x_window_to_scroll_bar (win);
12ba150f
JB
7502
7503 if (bar)
7504 {
a135645a 7505 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
7506 win_x = parent_x;
7507 win_y = parent_y;
7508 }
7509 }
90e65f07 7510
8bcee03e 7511 if (f1 == 0 && insist > 0)
b86bd3dd 7512 f1 = SELECTED_FRAME ();
1cf412ec 7513
a135645a 7514 if (f1)
12ba150f 7515 {
06a2c219
GM
7516 /* Ok, we found a frame. Store all the values.
7517 last_mouse_glyph is a rectangle used to reduce the
7518 generation of mouse events. To not miss any motion
7519 events, we must divide the frame into rectangles of the
7520 size of the smallest character that could be displayed
7521 on it, i.e. into the same rectangles that matrices on
7522 the frame are divided into. */
7523
7524#if OLD_REDISPLAY_CODE
2b5c9e71 7525 int ignore1, ignore2;
2b5c9e71 7526 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
334208b7 7527 &last_mouse_glyph,
1cf412ec
RS
7528 FRAME_X_DISPLAY_INFO (f1)->grabbed
7529 || insist);
06a2c219
GM
7530#else
7531 {
7532 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7533 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7534 int x = win_x;
7535 int y = win_y;
7536
7537 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7538 round down even for negative values. */
7539 if (x < 0)
7540 x -= width - 1;
7541 if (y < 0)
7542 y -= height - 1;
7543
7544 last_mouse_glyph.width = width;
7545 last_mouse_glyph.height = height;
7546 last_mouse_glyph.x = (x + width - 1) / width * width;
7547 last_mouse_glyph.y = (y + height - 1) / height * height;
7548 }
7549#endif
12ba150f
JB
7550
7551 *bar_window = Qnil;
7552 *part = 0;
334208b7 7553 *fp = f1;
e0c1aef2
KH
7554 XSETINT (*x, win_x);
7555 XSETINT (*y, win_y);
12ba150f
JB
7556 *time = last_mouse_movement_time;
7557 }
7558 }
7559 }
90e65f07
JB
7560
7561 UNBLOCK_INPUT;
7562}
f451eb13 7563
06a2c219 7564
06a2c219 7565#ifdef USE_X_TOOLKIT
bffcfca9
GM
7566
7567/* Atimer callback function for TIMER. Called every 0.1s to process
7568 Xt timeouts, if needed. We must avoid calling XtAppPending as
7569 much as possible because that function does an implicit XFlush
7570 that slows us down. */
7571
7572static void
7573x_process_timeouts (timer)
7574 struct atimer *timer;
7575{
7576 if (toolkit_scroll_bar_interaction || popup_activated_flag)
7577 {
7578 BLOCK_INPUT;
7579 while (XtAppPending (Xt_app_con) & XtIMTimer)
7580 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7581 UNBLOCK_INPUT;
7582 }
06a2c219
GM
7583}
7584
bffcfca9 7585#endif /* USE_X_TOOLKIT */
06a2c219
GM
7586
7587\f
7588/* Scroll bar support. */
7589
7590/* Given an X window ID, find the struct scroll_bar which manages it.
7591 This can be called in GC, so we have to make sure to strip off mark
7592 bits. */
bffcfca9 7593
06a2c219
GM
7594static struct scroll_bar *
7595x_window_to_scroll_bar (window_id)
7596 Window window_id;
7597{
7598 Lisp_Object tail;
7599
7600 for (tail = Vframe_list;
7601 XGCTYPE (tail) == Lisp_Cons;
8e713be6 7602 tail = XCDR (tail))
06a2c219
GM
7603 {
7604 Lisp_Object frame, bar, condemned;
7605
8e713be6 7606 frame = XCAR (tail);
06a2c219
GM
7607 /* All elements of Vframe_list should be frames. */
7608 if (! GC_FRAMEP (frame))
7609 abort ();
7610
7611 /* Scan this frame's scroll bar list for a scroll bar with the
7612 right window ID. */
7613 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7614 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7615 /* This trick allows us to search both the ordinary and
7616 condemned scroll bar lists with one loop. */
7617 ! GC_NILP (bar) || (bar = condemned,
7618 condemned = Qnil,
7619 ! GC_NILP (bar));
7620 bar = XSCROLL_BAR (bar)->next)
7621 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7622 return XSCROLL_BAR (bar);
7623 }
7624
7625 return 0;
7626}
7627
7628
7629\f
7630/************************************************************************
7631 Toolkit scroll bars
7632 ************************************************************************/
7633
eccc05db 7634#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
7635
7636static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7637static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7638static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7639 struct scroll_bar *));
7640static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7641 int, int, int));
7642
7643
7644/* Id of action hook installed for scroll bars. */
7645
7646static XtActionHookId action_hook_id;
7647
7648/* Lisp window being scrolled. Set when starting to interact with
7649 a toolkit scroll bar, reset to nil when ending the interaction. */
7650
7651static Lisp_Object window_being_scrolled;
7652
7653/* Last scroll bar part sent in xm_scroll_callback. */
7654
7655static int last_scroll_bar_part;
7656
ec18280f
SM
7657/* Whether this is an Xaw with arrow-scrollbars. This should imply
7658 that movements of 1/20 of the screen size are mapped to up/down. */
7659
7660static Boolean xaw3d_arrow_scroll;
7661
7662/* Whether the drag scrolling maintains the mouse at the top of the
7663 thumb. If not, resizing the thumb needs to be done more carefully
7664 to avoid jerkyness. */
7665
7666static Boolean xaw3d_pick_top;
7667
06a2c219
GM
7668
7669/* Action hook installed via XtAppAddActionHook when toolkit scroll
ec18280f 7670 bars are used.. The hook is responsible for detecting when
06a2c219
GM
7671 the user ends an interaction with the scroll bar, and generates
7672 a `end-scroll' scroll_bar_click' event if so. */
7673
7674static void
7675xt_action_hook (widget, client_data, action_name, event, params,
7676 num_params)
7677 Widget widget;
7678 XtPointer client_data;
7679 String action_name;
7680 XEvent *event;
7681 String *params;
7682 Cardinal *num_params;
7683{
7684 int scroll_bar_p;
7685 char *end_action;
7686
7687#ifdef USE_MOTIF
7688 scroll_bar_p = XmIsScrollBar (widget);
7689 end_action = "Release";
ec18280f 7690#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
7691 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7692 end_action = "EndScroll";
ec18280f 7693#endif /* USE_MOTIF */
06a2c219 7694
06a2c219
GM
7695 if (scroll_bar_p
7696 && strcmp (action_name, end_action) == 0
7697 && WINDOWP (window_being_scrolled))
7698 {
7699 struct window *w;
7700
7701 x_send_scroll_bar_event (window_being_scrolled,
7702 scroll_bar_end_scroll, 0, 0);
7703 w = XWINDOW (window_being_scrolled);
7704 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7705 window_being_scrolled = Qnil;
7706 last_scroll_bar_part = -1;
bffcfca9
GM
7707
7708 /* Xt timeouts no longer needed. */
7709 toolkit_scroll_bar_interaction = 0;
06a2c219
GM
7710 }
7711}
7712
07b3d16e
GM
7713/* A vector of windows used for communication between
7714 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
7715
7716static struct window **scroll_bar_windows;
7717static int scroll_bar_windows_size;
7718
06a2c219
GM
7719
7720/* Send a client message with message type Xatom_Scrollbar for a
7721 scroll action to the frame of WINDOW. PART is a value identifying
7722 the part of the scroll bar that was clicked on. PORTION is the
7723 amount to scroll of a whole of WHOLE. */
7724
7725static void
7726x_send_scroll_bar_event (window, part, portion, whole)
7727 Lisp_Object window;
7728 int part, portion, whole;
7729{
7730 XEvent event;
7731 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
07b3d16e
GM
7732 struct window *w = XWINDOW (window);
7733 struct frame *f = XFRAME (w->frame);
7734 int i;
06a2c219 7735
07b3d16e
GM
7736 BLOCK_INPUT;
7737
06a2c219
GM
7738 /* Construct a ClientMessage event to send to the frame. */
7739 ev->type = ClientMessage;
7740 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7741 ev->display = FRAME_X_DISPLAY (f);
7742 ev->window = FRAME_X_WINDOW (f);
7743 ev->format = 32;
07b3d16e
GM
7744
7745 /* We can only transfer 32 bits in the XClientMessageEvent, which is
7746 not enough to store a pointer or Lisp_Object on a 64 bit system.
7747 So, store the window in scroll_bar_windows and pass the index
7748 into that array in the event. */
7749 for (i = 0; i < scroll_bar_windows_size; ++i)
7750 if (scroll_bar_windows[i] == NULL)
7751 break;
7752
7753 if (i == scroll_bar_windows_size)
7754 {
7755 int new_size = max (10, 2 * scroll_bar_windows_size);
7756 size_t nbytes = new_size * sizeof *scroll_bar_windows;
7757 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
7758
7759 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
7760 nbytes);
7761 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
7762 scroll_bar_windows_size = new_size;
7763 }
7764
7765 scroll_bar_windows[i] = w;
7766 ev->data.l[0] = (long) i;
06a2c219
GM
7767 ev->data.l[1] = (long) part;
7768 ev->data.l[2] = (long) 0;
7769 ev->data.l[3] = (long) portion;
7770 ev->data.l[4] = (long) whole;
7771
bffcfca9
GM
7772 /* Make Xt timeouts work while the scroll bar is active. */
7773 toolkit_scroll_bar_interaction = 1;
7774
06a2c219
GM
7775 /* Setting the event mask to zero means that the message will
7776 be sent to the client that created the window, and if that
7777 window no longer exists, no event will be sent. */
06a2c219
GM
7778 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7779 UNBLOCK_INPUT;
7780}
7781
7782
7783/* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7784 in *IEVENT. */
7785
7786static void
7787x_scroll_bar_to_input_event (event, ievent)
7788 XEvent *event;
7789 struct input_event *ievent;
7790{
7791 XClientMessageEvent *ev = (XClientMessageEvent *) event;
52e386c2
KR
7792 Lisp_Object window;
7793 struct frame *f;
07b3d16e
GM
7794 struct window *w;
7795
7796 w = scroll_bar_windows[ev->data.l[0]];
7797 scroll_bar_windows[ev->data.l[0]] = NULL;
52e386c2 7798
07b3d16e
GM
7799 XSETWINDOW (window, w);
7800 f = XFRAME (w->frame);
06a2c219
GM
7801
7802 ievent->kind = scroll_bar_click;
7803 ievent->frame_or_window = window;
0f8aabe9 7804 ievent->arg = Qnil;
06a2c219
GM
7805 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7806 ievent->part = ev->data.l[1];
7807 ievent->code = ev->data.l[2];
7808 ievent->x = make_number ((int) ev->data.l[3]);
7809 ievent->y = make_number ((int) ev->data.l[4]);
7810 ievent->modifiers = 0;
7811}
7812
7813
7814#ifdef USE_MOTIF
7815
7816/* Minimum and maximum values used for Motif scroll bars. */
7817
7818#define XM_SB_MIN 1
7819#define XM_SB_MAX 10000000
7820#define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7821
7822
7823/* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7824 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7825 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7826
7827static void
7828xm_scroll_callback (widget, client_data, call_data)
7829 Widget widget;
7830 XtPointer client_data, call_data;
7831{
7832 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7833 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7834 double percent;
7835 int part = -1, whole = 0, portion = 0;
7836
7837 switch (cs->reason)
7838 {
7839 case XmCR_DECREMENT:
7840 bar->dragging = Qnil;
7841 part = scroll_bar_up_arrow;
7842 break;
7843
7844 case XmCR_INCREMENT:
7845 bar->dragging = Qnil;
7846 part = scroll_bar_down_arrow;
7847 break;
7848
7849 case XmCR_PAGE_DECREMENT:
7850 bar->dragging = Qnil;
7851 part = scroll_bar_above_handle;
7852 break;
7853
7854 case XmCR_PAGE_INCREMENT:
7855 bar->dragging = Qnil;
7856 part = scroll_bar_below_handle;
7857 break;
7858
7859 case XmCR_TO_TOP:
7860 bar->dragging = Qnil;
7861 part = scroll_bar_to_top;
7862 break;
7863
7864 case XmCR_TO_BOTTOM:
7865 bar->dragging = Qnil;
7866 part = scroll_bar_to_bottom;
7867 break;
7868
7869 case XmCR_DRAG:
7870 {
7871 int slider_size;
7872 int dragging_down_p = (INTEGERP (bar->dragging)
7873 && XINT (bar->dragging) <= cs->value);
7874
7875 /* Get the slider size. */
7876 BLOCK_INPUT;
7877 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7878 UNBLOCK_INPUT;
7879
7880 /* At the max position of the scroll bar, do a line-wise
7881 movement. Without doing anything, the LessTif scroll bar
7882 calls us with the same cs->value again and again. If we
7883 want to make sure that we can reach the end of the buffer,
7884 we have to do something.
7885
7886 Implementation note: setting bar->dragging always to
7887 cs->value gives a smoother movement at the max position.
7888 Setting it to nil when doing line-wise movement gives
7889 a better slider behavior. */
7890
7891 if (cs->value + slider_size == XM_SB_MAX
7892 || (dragging_down_p
7893 && last_scroll_bar_part == scroll_bar_down_arrow))
7894 {
7895 part = scroll_bar_down_arrow;
7896 bar->dragging = Qnil;
7897 }
7898 else
7899 {
7900 whole = XM_SB_RANGE;
7901 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7902 part = scroll_bar_handle;
7903 bar->dragging = make_number (cs->value);
7904 }
7905 }
7906 break;
7907
7908 case XmCR_VALUE_CHANGED:
7909 break;
7910 };
7911
7912 if (part >= 0)
7913 {
7914 window_being_scrolled = bar->window;
7915 last_scroll_bar_part = part;
7916 x_send_scroll_bar_event (bar->window, part, portion, whole);
7917 }
7918}
7919
7920
ec18280f 7921#else /* !USE_MOTIF, i.e. Xaw. */
06a2c219
GM
7922
7923
ec18280f 7924/* Xaw scroll bar callback. Invoked when the thumb is dragged.
06a2c219
GM
7925 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7926 scroll bar struct. CALL_DATA is a pointer to a float saying where
7927 the thumb is. */
7928
7929static void
ec18280f 7930xaw_jump_callback (widget, client_data, call_data)
06a2c219
GM
7931 Widget widget;
7932 XtPointer client_data, call_data;
7933{
7934 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7935 float top = *(float *) call_data;
7936 float shown;
ec18280f
SM
7937 int whole, portion, height;
7938 int part;
06a2c219
GM
7939
7940 /* Get the size of the thumb, a value between 0 and 1. */
7941 BLOCK_INPUT;
ec18280f 7942 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
06a2c219
GM
7943 UNBLOCK_INPUT;
7944
7945 whole = 10000000;
7946 portion = shown < 1 ? top * whole : 0;
06a2c219 7947
ec18280f
SM
7948 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
7949 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
7950 the bottom, so we force the scrolling whenever we see that we're
7951 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
7952 we try to ensure that we always stay two pixels away from the
7953 bottom). */
06a2c219
GM
7954 part = scroll_bar_down_arrow;
7955 else
7956 part = scroll_bar_handle;
7957
7958 window_being_scrolled = bar->window;
7959 bar->dragging = make_number (portion);
7960 last_scroll_bar_part = part;
7961 x_send_scroll_bar_event (bar->window, part, portion, whole);
7962}
7963
7964
ec18280f
SM
7965/* Xaw scroll bar callback. Invoked for incremental scrolling.,
7966 i.e. line or page up or down. WIDGET is the Xaw scroll bar
06a2c219
GM
7967 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
7968 the scroll bar. CALL_DATA is an integer specifying the action that
7969 has taken place. It's magnitude is in the range 0..height of the
7970 scroll bar. Negative values mean scroll towards buffer start.
7971 Values < height of scroll bar mean line-wise movement. */
7972
7973static void
ec18280f 7974xaw_scroll_callback (widget, client_data, call_data)
06a2c219
GM
7975 Widget widget;
7976 XtPointer client_data, call_data;
7977{
7978 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7979 int position = (int) call_data;
7980 Dimension height;
7981 int part;
7982
7983 /* Get the height of the scroll bar. */
7984 BLOCK_INPUT;
7985 XtVaGetValues (widget, XtNheight, &height, NULL);
7986 UNBLOCK_INPUT;
7987
ec18280f
SM
7988 if (abs (position) >= height)
7989 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
7990
7991 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
7992 it maps line-movement to call_data = max(5, height/20). */
7993 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
7994 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
06a2c219 7995 else
ec18280f 7996 part = scroll_bar_move_ratio;
06a2c219
GM
7997
7998 window_being_scrolled = bar->window;
7999 bar->dragging = Qnil;
8000 last_scroll_bar_part = part;
ec18280f 8001 x_send_scroll_bar_event (bar->window, part, position, height);
06a2c219
GM
8002}
8003
8004
8005#endif /* not USE_MOTIF */
8006
8007
8008/* Create the widget for scroll bar BAR on frame F. Record the widget
8009 and X window of the scroll bar in BAR. */
8010
8011static void
8012x_create_toolkit_scroll_bar (f, bar)
8013 struct frame *f;
8014 struct scroll_bar *bar;
8015{
8016 Window xwindow;
8017 Widget widget;
8018 Arg av[20];
8019 int ac = 0;
8020 char *scroll_bar_name = "verticalScrollBar";
8021 unsigned long pixel;
8022
8023 BLOCK_INPUT;
8024
8025#ifdef USE_MOTIF
8026 /* LessTif 0.85, problems:
8027
8028 1. When the mouse if over the scroll bar, the scroll bar will
8029 get keyboard events. I didn't find a way to turn this off.
8030
8031 2. Do we have to explicitly set the cursor to get an arrow
8032 cursor (see below)? */
8033
8034 /* Set resources. Create the widget. */
8035 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8036 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8037 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8038 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8039 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8040 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8041 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8042
8043 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8044 if (pixel != -1)
8045 {
8046 XtSetArg (av[ac], XmNforeground, pixel);
8047 ++ac;
8048 }
8049
8050 pixel = f->output_data.x->scroll_bar_background_pixel;
8051 if (pixel != -1)
8052 {
8053 XtSetArg (av[ac], XmNbackground, pixel);
8054 ++ac;
8055 }
8056
8057 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8058 scroll_bar_name, av, ac);
8059
8060 /* Add one callback for everything that can happen. */
8061 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8062 (XtPointer) bar);
8063 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8064 (XtPointer) bar);
8065 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8066 (XtPointer) bar);
8067 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8068 (XtPointer) bar);
8069 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8070 (XtPointer) bar);
8071 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8072 (XtPointer) bar);
8073 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8074 (XtPointer) bar);
8075
8076 /* Realize the widget. Only after that is the X window created. */
8077 XtRealizeWidget (widget);
8078
8079 /* Set the cursor to an arrow. I didn't find a resource to do that.
8080 And I'm wondering why it hasn't an arrow cursor by default. */
8081 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8082 f->output_data.x->nontext_cursor);
8083
ec18280f 8084#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
8085
8086 /* Set resources. Create the widget. The background of the
8087 Xaw3d scroll bar widget is a little bit light for my taste.
8088 We don't alter it here to let users change it according
8089 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8090 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8091 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
ec18280f
SM
8092 /* For smoother scrolling with Xaw3d -sm */
8093 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
8094 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
06a2c219
GM
8095
8096 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8097 if (pixel != -1)
8098 {
8099 XtSetArg (av[ac], XtNforeground, pixel);
8100 ++ac;
8101 }
8102
8103 pixel = f->output_data.x->scroll_bar_background_pixel;
8104 if (pixel != -1)
8105 {
8106 XtSetArg (av[ac], XtNbackground, pixel);
8107 ++ac;
8108 }
8109
8110 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8111 f->output_data.x->edit_widget, av, ac);
ec18280f
SM
8112
8113 {
8114 char *initial = "";
8115 char *val = initial;
8116 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8117 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8118 if (val == initial)
8119 { /* ARROW_SCROLL */
8120 xaw3d_arrow_scroll = True;
8121 /* Isn't that just a personal preference ? -sm */
8122 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8123 }
8124 }
06a2c219
GM
8125
8126 /* Define callbacks. */
ec18280f
SM
8127 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8128 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
06a2c219
GM
8129 (XtPointer) bar);
8130
8131 /* Realize the widget. Only after that is the X window created. */
8132 XtRealizeWidget (widget);
8133
ec18280f 8134#endif /* !USE_MOTIF */
06a2c219
GM
8135
8136 /* Install an action hook that let's us detect when the user
8137 finishes interacting with a scroll bar. */
8138 if (action_hook_id == 0)
8139 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8140
8141 /* Remember X window and widget in the scroll bar vector. */
8142 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8143 xwindow = XtWindow (widget);
8144 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8145
8146 UNBLOCK_INPUT;
8147}
8148
8149
8150/* Set the thumb size and position of scroll bar BAR. We are currently
8151 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8152
8153static void
8154x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8155 struct scroll_bar *bar;
8156 int portion, position, whole;
f451eb13 8157{
e83dc917
GM
8158 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8159 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
06a2c219 8160 float top, shown;
f451eb13 8161
06a2c219
GM
8162 if (whole == 0)
8163 top = 0, shown = 1;
8164 else
f451eb13 8165 {
06a2c219
GM
8166 top = (float) position / whole;
8167 shown = (float) portion / whole;
8168 }
f451eb13 8169
06a2c219 8170 BLOCK_INPUT;
f451eb13 8171
06a2c219
GM
8172#ifdef USE_MOTIF
8173 {
8174 int size, value;
8175 Boolean arrow1_selected, arrow2_selected;
8176 unsigned char flags;
8177 XmScrollBarWidget sb;
8178
ec18280f 8179 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
06a2c219
GM
8180 is the scroll bar's maximum and MIN is the scroll bar's minimum
8181 value. */
8182 size = shown * XM_SB_RANGE;
8183 size = min (size, XM_SB_RANGE);
8184 size = max (size, 1);
8185
8186 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8187 value = top * XM_SB_RANGE;
8188 value = min (value, XM_SB_MAX - size);
8189 value = max (value, XM_SB_MIN);
8190
8191 /* LessTif: Calling XmScrollBarSetValues after an increment or
8192 decrement turns off auto-repeat LessTif-internally. This can
8193 be seen in ScrollBar.c which resets Arrow1Selected and
8194 Arrow2Selected. It also sets internal flags so that LessTif
8195 believes the mouse is in the slider. We either have to change
8196 our code, or work around that by accessing private data. */
8197
8198 sb = (XmScrollBarWidget) widget;
8199 arrow1_selected = sb->scrollBar.arrow1_selected;
8200 arrow2_selected = sb->scrollBar.arrow2_selected;
8201 flags = sb->scrollBar.flags;
8202
8203 if (NILP (bar->dragging))
8204 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8205 else if (last_scroll_bar_part == scroll_bar_down_arrow)
8206 /* This has the negative side effect that the slider value is
ec18280f 8207 not what it would be if we scrolled here using line-wise or
06a2c219
GM
8208 page-wise movement. */
8209 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
8210 else
8211 {
8212 /* If currently dragging, only update the slider size.
8213 This reduces flicker effects. */
8214 int old_value, old_size, increment, page_increment;
8215
8216 XmScrollBarGetValues (widget, &old_value, &old_size,
8217 &increment, &page_increment);
8218 XmScrollBarSetValues (widget, old_value,
8219 min (size, XM_SB_RANGE - old_value),
8220 0, 0, False);
8221 }
8222
8223 sb->scrollBar.arrow1_selected = arrow1_selected;
8224 sb->scrollBar.arrow2_selected = arrow2_selected;
8225 sb->scrollBar.flags = flags;
8226 }
ec18280f 8227#else /* !USE_MOTIF i.e. use Xaw */
06a2c219 8228 {
ec18280f
SM
8229 float old_top, old_shown;
8230 Dimension height;
8231 XtVaGetValues (widget,
8232 XtNtopOfThumb, &old_top,
8233 XtNshown, &old_shown,
8234 XtNheight, &height,
8235 NULL);
8236
8237 /* Massage the top+shown values. */
8238 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8239 top = max (0, min (1, top));
8240 else
8241 top = old_top;
8242 /* Keep two pixels available for moving the thumb down. */
8243 shown = max (0, min (1 - top - (2.0 / height), shown));
06a2c219
GM
8244
8245 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8246 check that your system's configuration file contains a define
8247 for `NARROWPROTO'. See s/freebsd.h for an example. */
ec18280f 8248 if (top != old_top || shown != old_shown)
eb393530 8249 {
ec18280f 8250 if (NILP (bar->dragging))
eb393530 8251 XawScrollbarSetThumb (widget, top, shown);
06a2c219
GM
8252 else
8253 {
ec18280f
SM
8254#ifdef HAVE_XAW3D
8255 ScrollbarWidget sb = (ScrollbarWidget) widget;
3e71d8f2 8256 int scroll_mode = 0;
ec18280f
SM
8257
8258 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8259 if (xaw3d_arrow_scroll)
8260 {
8261 /* Xaw3d stupidly ignores resize requests while dragging
8262 so we have to make it believe it's not in dragging mode. */
8263 scroll_mode = sb->scrollbar.scroll_mode;
8264 if (scroll_mode == 2)
8265 sb->scrollbar.scroll_mode = 0;
8266 }
8267#endif
8268 /* Try to make the scrolling a tad smoother. */
8269 if (!xaw3d_pick_top)
8270 shown = min (shown, old_shown);
8271
8272 XawScrollbarSetThumb (widget, top, shown);
8273
8274#ifdef HAVE_XAW3D
8275 if (xaw3d_arrow_scroll && scroll_mode == 2)
8276 sb->scrollbar.scroll_mode = scroll_mode;
8277#endif
06a2c219 8278 }
06a2c219
GM
8279 }
8280 }
ec18280f 8281#endif /* !USE_MOTIF */
06a2c219
GM
8282
8283 UNBLOCK_INPUT;
f451eb13
JB
8284}
8285
06a2c219
GM
8286#endif /* USE_TOOLKIT_SCROLL_BARS */
8287
8288
8289\f
8290/************************************************************************
8291 Scroll bars, general
8292 ************************************************************************/
8293
8294/* Create a scroll bar and return the scroll bar vector for it. W is
8295 the Emacs window on which to create the scroll bar. TOP, LEFT,
8296 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8297 scroll bar. */
8298
ab648270 8299static struct scroll_bar *
06a2c219
GM
8300x_scroll_bar_create (w, top, left, width, height)
8301 struct window *w;
f451eb13
JB
8302 int top, left, width, height;
8303{
06a2c219 8304 struct frame *f = XFRAME (w->frame);
334208b7
RS
8305 struct scroll_bar *bar
8306 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
f451eb13
JB
8307
8308 BLOCK_INPUT;
8309
eccc05db 8310#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
8311 x_create_toolkit_scroll_bar (f, bar);
8312#else /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8313 {
8314 XSetWindowAttributes a;
8315 unsigned long mask;
5c187dee 8316 Window window;
06a2c219
GM
8317
8318 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8319 if (a.background_pixel == -1)
8320 a.background_pixel = f->output_data.x->background_pixel;
8321
12ba150f 8322 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9a572e2a 8323 | ButtonMotionMask | PointerMotionHintMask
12ba150f 8324 | ExposureMask);
7a13e894 8325 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
f451eb13 8326
dbc4e1c1 8327 mask = (CWBackPixel | CWEventMask | CWCursor);
f451eb13 8328
06a2c219
GM
8329 /* Clear the area of W that will serve as a scroll bar. This is
8330 for the case that a window has been split horizontally. In
8331 this case, no clear_frame is generated to reduce flickering. */
8332 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8333 left, top, width,
8334 window_box_height (w), False);
8335
8336 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8337 /* Position and size of scroll bar. */
8338 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8339 top,
8340 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8341 height,
8342 /* Border width, depth, class, and visual. */
8343 0,
8344 CopyFromParent,
8345 CopyFromParent,
8346 CopyFromParent,
8347 /* Attributes. */
8348 mask, &a);
8349 SET_SCROLL_BAR_X_WINDOW (bar, window);
f451eb13 8350 }
06a2c219 8351#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 8352
06a2c219 8353 XSETWINDOW (bar->window, w);
e0c1aef2
KH
8354 XSETINT (bar->top, top);
8355 XSETINT (bar->left, left);
8356 XSETINT (bar->width, width);
8357 XSETINT (bar->height, height);
8358 XSETINT (bar->start, 0);
8359 XSETINT (bar->end, 0);
12ba150f 8360 bar->dragging = Qnil;
f451eb13
JB
8361
8362 /* Add bar to its frame's list of scroll bars. */
334208b7 8363 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 8364 bar->prev = Qnil;
334208b7 8365 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
06a2c219 8366 if (!NILP (bar->next))
e0c1aef2 8367 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13 8368
06a2c219 8369 /* Map the window/widget. */
eccc05db 8370#ifdef USE_TOOLKIT_SCROLL_BARS
e83dc917
GM
8371 {
8372 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8373 XtConfigureWidget (scroll_bar,
06a2c219
GM
8374 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8375 top,
8376 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8377 height, 0);
e83dc917
GM
8378 XtMapWidget (scroll_bar);
8379 }
06a2c219 8380#else /* not USE_TOOLKIT_SCROLL_BARS */
7f9c7f94 8381 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
06a2c219 8382#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8383
8384 UNBLOCK_INPUT;
12ba150f 8385 return bar;
f451eb13
JB
8386}
8387
06a2c219 8388
12ba150f 8389/* Draw BAR's handle in the proper position.
06a2c219 8390
12ba150f
JB
8391 If the handle is already drawn from START to END, don't bother
8392 redrawing it, unless REBUILD is non-zero; in that case, always
8393 redraw it. (REBUILD is handy for drawing the handle after expose
58769bee 8394 events.)
12ba150f
JB
8395
8396 Normally, we want to constrain the start and end of the handle to
06a2c219
GM
8397 fit inside its rectangle, but if the user is dragging the scroll
8398 bar handle, we want to let them drag it down all the way, so that
8399 the bar's top is as far down as it goes; otherwise, there's no way
8400 to move to the very end of the buffer. */
8401
5c187dee
GM
8402#ifndef USE_TOOLKIT_SCROLL_BARS
8403
f451eb13 8404static void
ab648270
JB
8405x_scroll_bar_set_handle (bar, start, end, rebuild)
8406 struct scroll_bar *bar;
f451eb13 8407 int start, end;
12ba150f 8408 int rebuild;
f451eb13 8409{
12ba150f 8410 int dragging = ! NILP (bar->dragging);
ab648270 8411 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8412 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8413 GC gc = f->output_data.x->normal_gc;
12ba150f
JB
8414
8415 /* If the display is already accurate, do nothing. */
8416 if (! rebuild
8417 && start == XINT (bar->start)
8418 && end == XINT (bar->end))
8419 return;
8420
f451eb13
JB
8421 BLOCK_INPUT;
8422
8423 {
d9cdbb3d
RS
8424 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8425 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8426 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
f451eb13
JB
8427
8428 /* Make sure the values are reasonable, and try to preserve
8429 the distance between start and end. */
12ba150f
JB
8430 {
8431 int length = end - start;
8432
8433 if (start < 0)
8434 start = 0;
8435 else if (start > top_range)
8436 start = top_range;
8437 end = start + length;
8438
8439 if (end < start)
8440 end = start;
8441 else if (end > top_range && ! dragging)
8442 end = top_range;
8443 }
f451eb13 8444
ab648270 8445 /* Store the adjusted setting in the scroll bar. */
e0c1aef2
KH
8446 XSETINT (bar->start, start);
8447 XSETINT (bar->end, end);
f451eb13 8448
12ba150f
JB
8449 /* Clip the end position, just for display. */
8450 if (end > top_range)
8451 end = top_range;
f451eb13 8452
ab648270 8453 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
12ba150f
JB
8454 below top positions, to make sure the handle is always at least
8455 that many pixels tall. */
ab648270 8456 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
f451eb13 8457
12ba150f
JB
8458 /* Draw the empty space above the handle. Note that we can't clear
8459 zero-height areas; that means "clear to end of window." */
8460 if (0 < start)
334208b7 8461 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 8462
12ba150f 8463 /* x, y, width, height, and exposures. */
ab648270
JB
8464 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8465 VERTICAL_SCROLL_BAR_TOP_BORDER,
12ba150f
JB
8466 inside_width, start,
8467 False);
f451eb13 8468
06a2c219
GM
8469 /* Change to proper foreground color if one is specified. */
8470 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8471 XSetForeground (FRAME_X_DISPLAY (f), gc,
8472 f->output_data.x->scroll_bar_foreground_pixel);
8473
12ba150f 8474 /* Draw the handle itself. */
334208b7 8475 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13 8476
12ba150f 8477 /* x, y, width, height */
ab648270
JB
8478 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8479 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
12ba150f 8480 inside_width, end - start);
f451eb13 8481
06a2c219
GM
8482 /* Restore the foreground color of the GC if we changed it above. */
8483 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8484 XSetForeground (FRAME_X_DISPLAY (f), gc,
8485 f->output_data.x->foreground_pixel);
f451eb13 8486
12ba150f
JB
8487 /* Draw the empty space below the handle. Note that we can't
8488 clear zero-height areas; that means "clear to end of window." */
8489 if (end < inside_height)
334208b7 8490 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 8491
12ba150f 8492 /* x, y, width, height, and exposures. */
ab648270
JB
8493 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8494 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
12ba150f
JB
8495 inside_width, inside_height - end,
8496 False);
f451eb13 8497
f451eb13
JB
8498 }
8499
f451eb13
JB
8500 UNBLOCK_INPUT;
8501}
8502
5c187dee 8503#endif /* !USE_TOOLKIT_SCROLL_BARS */
f451eb13 8504
06a2c219
GM
8505/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8506 nil. */
58769bee 8507
12ba150f 8508static void
ab648270
JB
8509x_scroll_bar_remove (bar)
8510 struct scroll_bar *bar;
12ba150f 8511{
e83dc917 8512 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
8513 BLOCK_INPUT;
8514
eccc05db 8515#ifdef USE_TOOLKIT_SCROLL_BARS
e83dc917
GM
8516 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
8517#else
8518 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8519#endif
06a2c219 8520
ab648270
JB
8521 /* Disassociate this scroll bar from its window. */
8522 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
12ba150f
JB
8523
8524 UNBLOCK_INPUT;
8525}
8526
06a2c219 8527
12ba150f
JB
8528/* Set the handle of the vertical scroll bar for WINDOW to indicate
8529 that we are displaying PORTION characters out of a total of WHOLE
ab648270 8530 characters, starting at POSITION. If WINDOW has no scroll bar,
12ba150f 8531 create one. */
06a2c219 8532
12ba150f 8533static void
06a2c219
GM
8534XTset_vertical_scroll_bar (w, portion, whole, position)
8535 struct window *w;
f451eb13
JB
8536 int portion, whole, position;
8537{
06a2c219 8538 struct frame *f = XFRAME (w->frame);
ab648270 8539 struct scroll_bar *bar;
3c6ede7b 8540 int top, height, left, sb_left, width, sb_width;
06a2c219 8541 int window_x, window_y, window_width, window_height;
06a2c219 8542
3c6ede7b 8543 /* Get window dimensions. */
06a2c219 8544 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
3c6ede7b
GM
8545 top = window_y;
8546 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8547 height = window_height;
06a2c219 8548
3c6ede7b 8549 /* Compute the left edge of the scroll bar area. */
06a2c219 8550 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
3c6ede7b
GM
8551 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8552 else
8553 left = XFASTINT (w->left);
8554 left *= CANON_X_UNIT (f);
8555 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8556
8557 /* Compute the width of the scroll bar which might be less than
8558 the width of the area reserved for the scroll bar. */
8559 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8560 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
06a2c219 8561 else
3c6ede7b 8562 sb_width = width;
12ba150f 8563
3c6ede7b
GM
8564 /* Compute the left edge of the scroll bar. */
8565#ifdef USE_TOOLKIT_SCROLL_BARS
8566 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8567 sb_left = left + width - sb_width - (width - sb_width) / 2;
8568 else
8569 sb_left = left + (width - sb_width) / 2;
8570#else
8571 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8572 sb_left = left + width - sb_width;
8573 else
8574 sb_left = left;
8575#endif
8576
ab648270 8577 /* Does the scroll bar exist yet? */
06a2c219 8578 if (NILP (w->vertical_scroll_bar))
3c6ede7b 8579 {
80c32bcc 8580 BLOCK_INPUT;
3c6ede7b
GM
8581 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8582 left, top, width, height, False);
80c32bcc 8583 UNBLOCK_INPUT;
3c6ede7b
GM
8584 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8585 }
f451eb13 8586 else
12ba150f
JB
8587 {
8588 /* It may just need to be moved and resized. */
06a2c219
GM
8589 unsigned int mask = 0;
8590
8591 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8592
8593 BLOCK_INPUT;
8594
3c6ede7b 8595 if (sb_left != XINT (bar->left))
06a2c219 8596 mask |= CWX;
3c6ede7b 8597 if (top != XINT (bar->top))
06a2c219 8598 mask |= CWY;
3c6ede7b 8599 if (sb_width != XINT (bar->width))
06a2c219 8600 mask |= CWWidth;
3c6ede7b 8601 if (height != XINT (bar->height))
06a2c219
GM
8602 mask |= CWHeight;
8603
8604#ifdef USE_TOOLKIT_SCROLL_BARS
fe6f39d9
GM
8605
8606 /* Since toolkit scroll bars are smaller than the space reserved
8607 for them on the frame, we have to clear "under" them. */
8608 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3c6ede7b 8609 left, top, width, height, False);
06a2c219
GM
8610
8611 /* Move/size the scroll bar widget. */
8612 if (mask)
e83dc917 8613 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
3c6ede7b
GM
8614 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8615 top,
8616 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8617 height, 0);
06a2c219
GM
8618
8619#else /* not USE_TOOLKIT_SCROLL_BARS */
8620
e1f6572f
RS
8621 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8622 {
8623 /* Clear areas not covered by the scroll bar. This makes sure a
8624 previous mode line display is cleared after C-x 2 C-x 1, for
8625 example. Non-toolkit scroll bars are as wide as the area
8626 reserved for scroll bars - trim at both sides. */
8627 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8628 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8629 height, False);
8630 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8631 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8632 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8633 height, False);
8634 }
06a2c219
GM
8635
8636 /* Move/size the scroll bar window. */
8637 if (mask)
8638 {
8639 XWindowChanges wc;
8640
3c6ede7b
GM
8641 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8642 wc.y = top;
8643 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8644 wc.height = height;
06a2c219
GM
8645 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8646 mask, &wc);
8647 }
8648
8649#endif /* not USE_TOOLKIT_SCROLL_BARS */
8650
8651 /* Remember new settings. */
3c6ede7b
GM
8652 XSETINT (bar->left, sb_left);
8653 XSETINT (bar->top, top);
8654 XSETINT (bar->width, sb_width);
8655 XSETINT (bar->height, height);
06a2c219
GM
8656
8657 UNBLOCK_INPUT;
12ba150f 8658 }
f451eb13 8659
eccc05db 8660#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
8661 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8662#else /* not USE_TOOLKIT_SCROLL_BARS */
ab648270 8663 /* Set the scroll bar's current state, unless we're currently being
f451eb13 8664 dragged. */
12ba150f 8665 if (NILP (bar->dragging))
f451eb13 8666 {
92857db0 8667 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
f451eb13 8668
12ba150f 8669 if (whole == 0)
ab648270 8670 x_scroll_bar_set_handle (bar, 0, top_range, 0);
12ba150f
JB
8671 else
8672 {
43f868f5
JB
8673 int start = ((double) position * top_range) / whole;
8674 int end = ((double) (position + portion) * top_range) / whole;
ab648270 8675 x_scroll_bar_set_handle (bar, start, end, 0);
12ba150f 8676 }
f451eb13 8677 }
06a2c219 8678#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 8679
06a2c219 8680 XSETVECTOR (w->vertical_scroll_bar, bar);
f451eb13
JB
8681}
8682
12ba150f 8683
f451eb13 8684/* The following three hooks are used when we're doing a thorough
ab648270 8685 redisplay of the frame. We don't explicitly know which scroll bars
f451eb13 8686 are going to be deleted, because keeping track of when windows go
12ba150f
JB
8687 away is a real pain - "Can you say set-window-configuration, boys
8688 and girls?" Instead, we just assert at the beginning of redisplay
ab648270 8689 that *all* scroll bars are to be removed, and then save a scroll bar
12ba150f 8690 from the fiery pit when we actually redisplay its window. */
f451eb13 8691
ab648270
JB
8692/* Arrange for all scroll bars on FRAME to be removed at the next call
8693 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
06a2c219
GM
8694 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8695
58769bee 8696static void
ab648270 8697XTcondemn_scroll_bars (frame)
f451eb13
JB
8698 FRAME_PTR frame;
8699{
f9e24cb9
RS
8700 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8701 while (! NILP (FRAME_SCROLL_BARS (frame)))
8702 {
8703 Lisp_Object bar;
8704 bar = FRAME_SCROLL_BARS (frame);
8705 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8706 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8707 XSCROLL_BAR (bar)->prev = Qnil;
8708 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8709 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8710 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8711 }
f451eb13
JB
8712}
8713
fa2dfc30 8714
06a2c219 8715/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
12ba150f 8716 Note that WINDOW isn't necessarily condemned at all. */
fa2dfc30 8717
f451eb13 8718static void
ab648270 8719XTredeem_scroll_bar (window)
12ba150f 8720 struct window *window;
f451eb13 8721{
ab648270 8722 struct scroll_bar *bar;
fa2dfc30 8723 struct frame *f;
12ba150f 8724
ab648270
JB
8725 /* We can't redeem this window's scroll bar if it doesn't have one. */
8726 if (NILP (window->vertical_scroll_bar))
12ba150f
JB
8727 abort ();
8728
ab648270 8729 bar = XSCROLL_BAR (window->vertical_scroll_bar);
12ba150f
JB
8730
8731 /* Unlink it from the condemned list. */
fa2dfc30
GM
8732 f = XFRAME (WINDOW_FRAME (window));
8733 if (NILP (bar->prev))
8734 {
8735 /* If the prev pointer is nil, it must be the first in one of
8736 the lists. */
8737 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
8738 /* It's not condemned. Everything's fine. */
8739 return;
8740 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8741 window->vertical_scroll_bar))
8742 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
8743 else
8744 /* If its prev pointer is nil, it must be at the front of
8745 one or the other! */
8746 abort ();
8747 }
8748 else
8749 XSCROLL_BAR (bar->prev)->next = bar->next;
12ba150f 8750
fa2dfc30
GM
8751 if (! NILP (bar->next))
8752 XSCROLL_BAR (bar->next)->prev = bar->prev;
12ba150f 8753
fa2dfc30
GM
8754 bar->next = FRAME_SCROLL_BARS (f);
8755 bar->prev = Qnil;
8756 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8757 if (! NILP (bar->next))
8758 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13
JB
8759}
8760
ab648270
JB
8761/* Remove all scroll bars on FRAME that haven't been saved since the
8762 last call to `*condemn_scroll_bars_hook'. */
06a2c219 8763
f451eb13 8764static void
ab648270 8765XTjudge_scroll_bars (f)
12ba150f 8766 FRAME_PTR f;
f451eb13 8767{
12ba150f 8768 Lisp_Object bar, next;
f451eb13 8769
ab648270 8770 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
cf7cb199
JB
8771
8772 /* Clear out the condemned list now so we won't try to process any
ab648270
JB
8773 more events on the hapless scroll bars. */
8774 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
cf7cb199
JB
8775
8776 for (; ! NILP (bar); bar = next)
f451eb13 8777 {
ab648270 8778 struct scroll_bar *b = XSCROLL_BAR (bar);
12ba150f 8779
ab648270 8780 x_scroll_bar_remove (b);
12ba150f
JB
8781
8782 next = b->next;
8783 b->next = b->prev = Qnil;
f451eb13 8784 }
12ba150f 8785
ab648270 8786 /* Now there should be no references to the condemned scroll bars,
12ba150f 8787 and they should get garbage-collected. */
f451eb13
JB
8788}
8789
8790
06a2c219
GM
8791/* Handle an Expose or GraphicsExpose event on a scroll bar. This
8792 is a no-op when using toolkit scroll bars.
ab648270
JB
8793
8794 This may be called from a signal handler, so we have to ignore GC
8795 mark bits. */
06a2c219 8796
f451eb13 8797static void
ab648270
JB
8798x_scroll_bar_expose (bar, event)
8799 struct scroll_bar *bar;
f451eb13
JB
8800 XEvent *event;
8801{
06a2c219
GM
8802#ifndef USE_TOOLKIT_SCROLL_BARS
8803
ab648270 8804 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8805 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8806 GC gc = f->output_data.x->normal_gc;
3cbd2e0b 8807 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
12ba150f 8808
f451eb13
JB
8809 BLOCK_INPUT;
8810
ab648270 8811 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
f451eb13 8812
06a2c219 8813 /* Draw a one-pixel border just inside the edges of the scroll bar. */
334208b7 8814 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13
JB
8815
8816 /* x, y, width, height */
d9cdbb3d 8817 0, 0,
3cbd2e0b 8818 XINT (bar->width) - 1 - width_trim - width_trim,
d9cdbb3d
RS
8819 XINT (bar->height) - 1);
8820
f451eb13 8821 UNBLOCK_INPUT;
06a2c219
GM
8822
8823#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8824}
8825
ab648270
JB
8826/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8827 is set to something other than no_event, it is enqueued.
8828
8829 This may be called from a signal handler, so we have to ignore GC
8830 mark bits. */
06a2c219 8831
5c187dee
GM
8832#ifndef USE_TOOLKIT_SCROLL_BARS
8833
f451eb13 8834static void
ab648270
JB
8835x_scroll_bar_handle_click (bar, event, emacs_event)
8836 struct scroll_bar *bar;
f451eb13
JB
8837 XEvent *event;
8838 struct input_event *emacs_event;
8839{
0299d313 8840 if (! GC_WINDOWP (bar->window))
12ba150f
JB
8841 abort ();
8842
ab648270 8843 emacs_event->kind = scroll_bar_click;
69388238 8844 emacs_event->code = event->xbutton.button - Button1;
0299d313
RS
8845 emacs_event->modifiers
8846 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8847 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8848 event->xbutton.state)
8849 | (event->type == ButtonRelease
8850 ? up_modifier
8851 : down_modifier));
12ba150f 8852 emacs_event->frame_or_window = bar->window;
0f8aabe9 8853 emacs_event->arg = Qnil;
f451eb13 8854 emacs_event->timestamp = event->xbutton.time;
12ba150f 8855 {
06a2c219 8856#if 0
d9cdbb3d 8857 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
0299d313 8858 int internal_height
d9cdbb3d 8859 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8860#endif
0299d313 8861 int top_range
d9cdbb3d 8862 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ab648270 8863 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
12ba150f
JB
8864
8865 if (y < 0) y = 0;
8866 if (y > top_range) y = top_range;
8867
8868 if (y < XINT (bar->start))
ab648270
JB
8869 emacs_event->part = scroll_bar_above_handle;
8870 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8871 emacs_event->part = scroll_bar_handle;
12ba150f 8872 else
ab648270 8873 emacs_event->part = scroll_bar_below_handle;
929787e1
JB
8874
8875 /* Just because the user has clicked on the handle doesn't mean
5116f055
JB
8876 they want to drag it. Lisp code needs to be able to decide
8877 whether or not we're dragging. */
929787e1 8878#if 0
12ba150f
JB
8879 /* If the user has just clicked on the handle, record where they're
8880 holding it. */
8881 if (event->type == ButtonPress
ab648270 8882 && emacs_event->part == scroll_bar_handle)
e0c1aef2 8883 XSETINT (bar->dragging, y - XINT (bar->start));
929787e1 8884#endif
12ba150f
JB
8885
8886 /* If the user has released the handle, set it to its final position. */
8887 if (event->type == ButtonRelease
8888 && ! NILP (bar->dragging))
8889 {
8890 int new_start = y - XINT (bar->dragging);
8891 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
f451eb13 8892
ab648270 8893 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
12ba150f
JB
8894 bar->dragging = Qnil;
8895 }
f451eb13 8896
5116f055
JB
8897 /* Same deal here as the other #if 0. */
8898#if 0
58769bee 8899 /* Clicks on the handle are always reported as occurring at the top of
12ba150f 8900 the handle. */
ab648270 8901 if (emacs_event->part == scroll_bar_handle)
12ba150f
JB
8902 emacs_event->x = bar->start;
8903 else
e0c1aef2 8904 XSETINT (emacs_event->x, y);
5116f055 8905#else
e0c1aef2 8906 XSETINT (emacs_event->x, y);
5116f055 8907#endif
f451eb13 8908
e0c1aef2 8909 XSETINT (emacs_event->y, top_range);
12ba150f
JB
8910 }
8911}
f451eb13 8912
ab648270
JB
8913/* Handle some mouse motion while someone is dragging the scroll bar.
8914
8915 This may be called from a signal handler, so we have to ignore GC
8916 mark bits. */
06a2c219 8917
f451eb13 8918static void
ab648270
JB
8919x_scroll_bar_note_movement (bar, event)
8920 struct scroll_bar *bar;
f451eb13
JB
8921 XEvent *event;
8922{
39d8bb4d
KH
8923 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8924
f451eb13
JB
8925 last_mouse_movement_time = event->xmotion.time;
8926
39d8bb4d 8927 f->mouse_moved = 1;
e0c1aef2 8928 XSETVECTOR (last_mouse_scroll_bar, bar);
f451eb13
JB
8929
8930 /* If we're dragging the bar, display it. */
ab648270 8931 if (! GC_NILP (bar->dragging))
f451eb13
JB
8932 {
8933 /* Where should the handle be now? */
12ba150f 8934 int new_start = event->xmotion.y - XINT (bar->dragging);
f451eb13 8935
12ba150f 8936 if (new_start != XINT (bar->start))
f451eb13 8937 {
12ba150f 8938 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
58769bee 8939
ab648270 8940 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
f451eb13
JB
8941 }
8942 }
f451eb13
JB
8943}
8944
5c187dee
GM
8945#endif /* !USE_TOOLKIT_SCROLL_BARS */
8946
12ba150f 8947/* Return information to the user about the current position of the mouse
ab648270 8948 on the scroll bar. */
06a2c219 8949
12ba150f 8950static void
334208b7
RS
8951x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8952 FRAME_PTR *fp;
12ba150f 8953 Lisp_Object *bar_window;
ab648270 8954 enum scroll_bar_part *part;
12ba150f
JB
8955 Lisp_Object *x, *y;
8956 unsigned long *time;
8957{
ab648270 8958 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
334208b7
RS
8959 Window w = SCROLL_BAR_X_WINDOW (bar);
8960 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f 8961 int win_x, win_y;
559cb2fb
JB
8962 Window dummy_window;
8963 int dummy_coord;
8964 unsigned int dummy_mask;
12ba150f 8965
cf7cb199
JB
8966 BLOCK_INPUT;
8967
ab648270 8968 /* Get the mouse's position relative to the scroll bar window, and
12ba150f 8969 report that. */
334208b7 8970 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
12ba150f 8971
559cb2fb
JB
8972 /* Root, child, root x and root y. */
8973 &dummy_window, &dummy_window,
8974 &dummy_coord, &dummy_coord,
12ba150f 8975
559cb2fb
JB
8976 /* Position relative to scroll bar. */
8977 &win_x, &win_y,
12ba150f 8978
559cb2fb
JB
8979 /* Mouse buttons and modifier keys. */
8980 &dummy_mask))
7a13e894 8981 ;
559cb2fb
JB
8982 else
8983 {
06a2c219 8984#if 0
559cb2fb 8985 int inside_height
d9cdbb3d 8986 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8987#endif
559cb2fb 8988 int top_range
d9cdbb3d 8989 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
559cb2fb
JB
8990
8991 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
8992
8993 if (! NILP (bar->dragging))
8994 win_y -= XINT (bar->dragging);
8995
8996 if (win_y < 0)
8997 win_y = 0;
8998 if (win_y > top_range)
8999 win_y = top_range;
9000
334208b7 9001 *fp = f;
7a13e894 9002 *bar_window = bar->window;
559cb2fb
JB
9003
9004 if (! NILP (bar->dragging))
9005 *part = scroll_bar_handle;
9006 else if (win_y < XINT (bar->start))
9007 *part = scroll_bar_above_handle;
9008 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9009 *part = scroll_bar_handle;
9010 else
9011 *part = scroll_bar_below_handle;
12ba150f 9012
e0c1aef2
KH
9013 XSETINT (*x, win_y);
9014 XSETINT (*y, top_range);
12ba150f 9015
39d8bb4d 9016 f->mouse_moved = 0;
559cb2fb
JB
9017 last_mouse_scroll_bar = Qnil;
9018 }
12ba150f 9019
559cb2fb 9020 *time = last_mouse_movement_time;
cf7cb199 9021
cf7cb199 9022 UNBLOCK_INPUT;
12ba150f
JB
9023}
9024
f451eb13 9025
dbc4e1c1 9026/* The screen has been cleared so we may have changed foreground or
ab648270
JB
9027 background colors, and the scroll bars may need to be redrawn.
9028 Clear out the scroll bars, and ask for expose events, so we can
dbc4e1c1
JB
9029 redraw them. */
9030
dfcf069d 9031void
ab648270 9032x_scroll_bar_clear (f)
dbc4e1c1
JB
9033 FRAME_PTR f;
9034{
06a2c219 9035#ifndef USE_TOOLKIT_SCROLL_BARS
dbc4e1c1
JB
9036 Lisp_Object bar;
9037
b80c363e
RS
9038 /* We can have scroll bars even if this is 0,
9039 if we just turned off scroll bar mode.
9040 But in that case we should not clear them. */
9041 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9042 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9043 bar = XSCROLL_BAR (bar)->next)
9044 XClearArea (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
9045 0, 0, 0, 0, True);
06a2c219 9046#endif /* not USE_TOOLKIT_SCROLL_BARS */
dbc4e1c1
JB
9047}
9048
06a2c219 9049/* This processes Expose events from the menu-bar specific X event
19126e11 9050 loop in xmenu.c. This allows to redisplay the frame if necessary
06a2c219 9051 when handling menu-bar or pop-up items. */
3afe33e7 9052
06a2c219 9053int
3afe33e7
RS
9054process_expose_from_menu (event)
9055 XEvent event;
9056{
9057 FRAME_PTR f;
19126e11 9058 struct x_display_info *dpyinfo;
06a2c219 9059 int frame_exposed_p = 0;
3afe33e7 9060
f94397b5
KH
9061 BLOCK_INPUT;
9062
19126e11
KH
9063 dpyinfo = x_display_info_for_display (event.xexpose.display);
9064 f = x_window_to_frame (dpyinfo, event.xexpose.window);
3afe33e7
RS
9065 if (f)
9066 {
9067 if (f->async_visible == 0)
9068 {
9069 f->async_visible = 1;
9070 f->async_iconified = 0;
06c488fd 9071 f->output_data.x->has_been_visible = 1;
3afe33e7
RS
9072 SET_FRAME_GARBAGED (f);
9073 }
9074 else
9075 {
06a2c219
GM
9076 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9077 event.xexpose.x, event.xexpose.y,
9078 event.xexpose.width, event.xexpose.height);
9079 frame_exposed_p = 1;
3afe33e7
RS
9080 }
9081 }
9082 else
9083 {
9084 struct scroll_bar *bar
9085 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 9086
3afe33e7
RS
9087 if (bar)
9088 x_scroll_bar_expose (bar, &event);
9089 }
f94397b5
KH
9090
9091 UNBLOCK_INPUT;
06a2c219 9092 return frame_exposed_p;
3afe33e7 9093}
09756a85
RS
9094\f
9095/* Define a queue to save up SelectionRequest events for later handling. */
9096
9097struct selection_event_queue
9098 {
9099 XEvent event;
9100 struct selection_event_queue *next;
9101 };
9102
9103static struct selection_event_queue *queue;
9104
9105/* Nonzero means queue up certain events--don't process them yet. */
06a2c219 9106
09756a85
RS
9107static int x_queue_selection_requests;
9108
9109/* Queue up an X event *EVENT, to be processed later. */
dbc4e1c1 9110
09756a85 9111static void
334208b7
RS
9112x_queue_event (f, event)
9113 FRAME_PTR f;
09756a85
RS
9114 XEvent *event;
9115{
9116 struct selection_event_queue *queue_tmp
06a2c219 9117 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
09756a85 9118
58769bee 9119 if (queue_tmp != NULL)
09756a85
RS
9120 {
9121 queue_tmp->event = *event;
9122 queue_tmp->next = queue;
9123 queue = queue_tmp;
9124 }
9125}
9126
9127/* Take all the queued events and put them back
9128 so that they get processed afresh. */
9129
9130static void
db3906fd
RS
9131x_unqueue_events (display)
9132 Display *display;
09756a85 9133{
58769bee 9134 while (queue != NULL)
09756a85
RS
9135 {
9136 struct selection_event_queue *queue_tmp = queue;
db3906fd 9137 XPutBackEvent (display, &queue_tmp->event);
09756a85 9138 queue = queue_tmp->next;
06a2c219 9139 xfree ((char *)queue_tmp);
09756a85
RS
9140 }
9141}
9142
9143/* Start queuing SelectionRequest events. */
9144
9145void
db3906fd
RS
9146x_start_queuing_selection_requests (display)
9147 Display *display;
09756a85
RS
9148{
9149 x_queue_selection_requests++;
9150}
9151
9152/* Stop queuing SelectionRequest events. */
9153
9154void
db3906fd
RS
9155x_stop_queuing_selection_requests (display)
9156 Display *display;
09756a85
RS
9157{
9158 x_queue_selection_requests--;
db3906fd 9159 x_unqueue_events (display);
09756a85 9160}
f451eb13
JB
9161\f
9162/* The main X event-reading loop - XTread_socket. */
dc6f92b8 9163
06a2c219 9164/* Time stamp of enter window event. This is only used by XTread_socket,
dc6f92b8
JB
9165 but we have to put it out here, since static variables within functions
9166 sometimes don't work. */
06a2c219 9167
dc6f92b8
JB
9168static Time enter_timestamp;
9169
11edeb03 9170/* This holds the state XLookupString needs to implement dead keys
58769bee 9171 and other tricks known as "compose processing". _X Window System_
11edeb03
JB
9172 says that a portable program can't use this, but Stephen Gildea assures
9173 me that letting the compiler initialize it to zeros will work okay.
9174
9175 This must be defined outside of XTread_socket, for the same reasons
06a2c219
GM
9176 given for enter_time stamp, above. */
9177
11edeb03
JB
9178static XComposeStatus compose_status;
9179
10e6549c
RS
9180/* Record the last 100 characters stored
9181 to help debug the loss-of-chars-during-GC problem. */
06a2c219 9182
2224b905
RS
9183static int temp_index;
9184static short temp_buffer[100];
10e6549c 9185
7a13e894
RS
9186/* Set this to nonzero to fake an "X I/O error"
9187 on a particular display. */
06a2c219 9188
7a13e894
RS
9189struct x_display_info *XTread_socket_fake_io_error;
9190
2224b905
RS
9191/* When we find no input here, we occasionally do a no-op command
9192 to verify that the X server is still running and we can still talk with it.
9193 We try all the open displays, one by one.
9194 This variable is used for cycling thru the displays. */
06a2c219 9195
2224b905
RS
9196static struct x_display_info *next_noop_dpyinfo;
9197
06a2c219
GM
9198#define SET_SAVED_MENU_EVENT(size) \
9199 do \
9200 { \
9201 if (f->output_data.x->saved_menu_event == 0) \
9202 f->output_data.x->saved_menu_event \
9203 = (XEvent *) xmalloc (sizeof (XEvent)); \
9204 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9205 if (numchars >= 1) \
9206 { \
9207 bufp->kind = menu_bar_activate_event; \
9208 XSETFRAME (bufp->frame_or_window, f); \
0f8aabe9 9209 bufp->arg = Qnil; \
06a2c219
GM
9210 bufp++; \
9211 count++; \
9212 numchars--; \
9213 } \
9214 } \
9215 while (0)
9216
8805890a 9217#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
06a2c219 9218#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8805890a 9219
dc6f92b8
JB
9220/* Read events coming from the X server.
9221 This routine is called by the SIGIO handler.
9222 We return as soon as there are no more events to be read.
9223
9224 Events representing keys are stored in buffer BUFP,
9225 which can hold up to NUMCHARS characters.
9226 We return the number of characters stored into the buffer,
9227 thus pretending to be `read'.
9228
dc6f92b8
JB
9229 EXPECTED is nonzero if the caller knows input is available. */
9230
7c5283e4 9231int
f66868ba 9232XTread_socket (sd, bufp, numchars, expected)
dc6f92b8 9233 register int sd;
8805890a
KH
9234 /* register */ struct input_event *bufp;
9235 /* register */ int numchars;
dc6f92b8
JB
9236 int expected;
9237{
9238 int count = 0;
9239 int nbytes = 0;
dc6f92b8 9240 XEvent event;
f676886a 9241 struct frame *f;
66f55a9d 9242 int event_found = 0;
334208b7 9243 struct x_display_info *dpyinfo;
379b5ac0 9244 struct coding_system coding;
dc6f92b8 9245
9ac0d9e0 9246 if (interrupt_input_blocked)
dc6f92b8 9247 {
9ac0d9e0 9248 interrupt_input_pending = 1;
dc6f92b8
JB
9249 return -1;
9250 }
9251
9ac0d9e0 9252 interrupt_input_pending = 0;
dc6f92b8 9253 BLOCK_INPUT;
c0a04927
RS
9254
9255 /* So people can tell when we have read the available input. */
9256 input_signal_count++;
9257
dc6f92b8 9258 if (numchars <= 0)
06a2c219 9259 abort (); /* Don't think this happens. */
dc6f92b8 9260
bde5503b
GM
9261 ++handling_signal;
9262
379b5ac0
KH
9263 /* The input should be decoded if it is from XIM. Currently the
9264 locale of XIM is the same as that of the system. So, we can use
9265 Vlocale_coding_system which is initialized properly at Emacs
9266 startup time. */
9267 setup_coding_system (Vlocale_coding_system, &coding);
9268 coding.src_multibyte = 0;
9269 coding.dst_multibyte = 1;
9270 /* The input is converted to events, thus we can't handle
9271 composition. Anyway, there's no XIM that gives us composition
9272 information. */
9273 coding.composing = COMPOSITION_DISABLED;
9274
7a13e894
RS
9275 /* Find the display we are supposed to read input for.
9276 It's the one communicating on descriptor SD. */
9277 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9278 {
9279#if 0 /* This ought to be unnecessary; let's verify it. */
dc6f92b8 9280#ifdef FIOSNBIO
7a13e894
RS
9281 /* If available, Xlib uses FIOSNBIO to make the socket
9282 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
e6cbea31 9283 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
06a2c219 9284 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7a13e894 9285 fcntl (dpyinfo->connection, F_SETFL, 0);
c118dd06 9286#endif /* ! defined (FIOSNBIO) */
7a13e894 9287#endif
dc6f92b8 9288
7a13e894
RS
9289#if 0 /* This code can't be made to work, with multiple displays,
9290 and appears not to be used on any system any more.
9291 Also keyboard.c doesn't turn O_NDELAY on and off
9292 for X connections. */
dc6f92b8
JB
9293#ifndef SIGIO
9294#ifndef HAVE_SELECT
7a13e894
RS
9295 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9296 {
9297 extern int read_alarm_should_throw;
9298 read_alarm_should_throw = 1;
9299 XPeekEvent (dpyinfo->display, &event);
9300 read_alarm_should_throw = 0;
9301 }
c118dd06
JB
9302#endif /* HAVE_SELECT */
9303#endif /* SIGIO */
7a13e894 9304#endif
dc6f92b8 9305
7a13e894
RS
9306 /* For debugging, this gives a way to fake an I/O error. */
9307 if (dpyinfo == XTread_socket_fake_io_error)
9308 {
9309 XTread_socket_fake_io_error = 0;
9310 x_io_error_quitter (dpyinfo->display);
9311 }
dc6f92b8 9312
06a2c219 9313 while (XPending (dpyinfo->display))
dc6f92b8 9314 {
7a13e894 9315 XNextEvent (dpyinfo->display, &event);
06a2c219 9316
531483fb 9317#ifdef HAVE_X_I18N
d1bc4182 9318 {
f2be1146
GM
9319 /* Filter events for the current X input method.
9320 XFilterEvent returns non-zero if the input method has
9321 consumed the event. We pass the frame's X window to
9322 XFilterEvent because that's the one for which the IC
9323 was created. */
f5d11644
GM
9324 struct frame *f1 = x_any_window_to_frame (dpyinfo,
9325 event.xclient.window);
9326 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
d1bc4182
RS
9327 break;
9328 }
0cd6403b 9329#endif
7a13e894
RS
9330 event_found = 1;
9331
9332 switch (event.type)
9333 {
9334 case ClientMessage:
c047688c 9335 {
7a13e894
RS
9336 if (event.xclient.message_type
9337 == dpyinfo->Xatom_wm_protocols
9338 && event.xclient.format == 32)
c047688c 9339 {
7a13e894
RS
9340 if (event.xclient.data.l[0]
9341 == dpyinfo->Xatom_wm_take_focus)
c047688c 9342 {
8c1a6a84
RS
9343 /* Use x_any_window_to_frame because this
9344 could be the shell widget window
9345 if the frame has no title bar. */
9346 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6c183ba5
RS
9347#ifdef HAVE_X_I18N
9348 /* Not quite sure this is needed -pd */
8c1a6a84 9349 if (f && FRAME_XIC (f))
6c183ba5
RS
9350 XSetICFocus (FRAME_XIC (f));
9351#endif
f1da8f06
GM
9352#if 0 /* Emacs sets WM hints whose `input' field is `true'. This
9353 instructs the WM to set the input focus automatically for
9354 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
9355 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
9356 it has set the focus. So, XSetInputFocus below is not
9357 needed.
9358
9359 The call to XSetInputFocus below has also caused trouble. In
9360 cases where the XSetInputFocus done by the WM and the one
9361 below are temporally close (on a fast machine), the call
9362 below can generate additional FocusIn events which confuse
9363 Emacs. */
9364
bf7253f4
RS
9365 /* Since we set WM_TAKE_FOCUS, we must call
9366 XSetInputFocus explicitly. But not if f is null,
9367 since that might be an event for a deleted frame. */
7a13e894 9368 if (f)
bf7253f4
RS
9369 {
9370 Display *d = event.xclient.display;
9371 /* Catch and ignore errors, in case window has been
9372 iconified by a window manager such as GWM. */
9373 int count = x_catch_errors (d);
9374 XSetInputFocus (d, event.xclient.window,
e1f6572f
RS
9375 /* The ICCCM says this is
9376 the only valid choice. */
9377 RevertToParent,
bf7253f4
RS
9378 event.xclient.data.l[1]);
9379 /* This is needed to detect the error
9380 if there is an error. */
9381 XSync (d, False);
9382 x_uncatch_errors (d, count);
9383 }
7a13e894 9384 /* Not certain about handling scroll bars here */
f1da8f06 9385#endif /* 0 */
c047688c 9386 }
7a13e894
RS
9387 else if (event.xclient.data.l[0]
9388 == dpyinfo->Xatom_wm_save_yourself)
9389 {
9390 /* Save state modify the WM_COMMAND property to
06a2c219 9391 something which can reinstate us. This notifies
7a13e894
RS
9392 the session manager, who's looking for such a
9393 PropertyNotify. Can restart processing when
06a2c219 9394 a keyboard or mouse event arrives. */
7a13e894
RS
9395 if (numchars > 0)
9396 {
19126e11
KH
9397 f = x_top_window_to_frame (dpyinfo,
9398 event.xclient.window);
7a13e894
RS
9399
9400 /* This is just so we only give real data once
9401 for a single Emacs process. */
b86bd3dd 9402 if (f == SELECTED_FRAME ())
7a13e894
RS
9403 XSetCommand (FRAME_X_DISPLAY (f),
9404 event.xclient.window,
9405 initial_argv, initial_argc);
f000f5c5 9406 else if (f)
7a13e894
RS
9407 XSetCommand (FRAME_X_DISPLAY (f),
9408 event.xclient.window,
9409 0, 0);
9410 }
9411 }
9412 else if (event.xclient.data.l[0]
9413 == dpyinfo->Xatom_wm_delete_window)
1fb20991 9414 {
19126e11
KH
9415 struct frame *f
9416 = x_any_window_to_frame (dpyinfo,
9417 event.xclient.window);
1fb20991 9418
7a13e894
RS
9419 if (f)
9420 {
9421 if (numchars == 0)
9422 abort ();
1fb20991 9423
7a13e894
RS
9424 bufp->kind = delete_window_event;
9425 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9426 bufp->arg = Qnil;
7a13e894
RS
9427 bufp++;
9428
9429 count += 1;
9430 numchars -= 1;
9431 }
1fb20991 9432 }
c047688c 9433 }
7a13e894
RS
9434 else if (event.xclient.message_type
9435 == dpyinfo->Xatom_wm_configure_denied)
9436 {
9437 }
9438 else if (event.xclient.message_type
9439 == dpyinfo->Xatom_wm_window_moved)
9440 {
9441 int new_x, new_y;
19126e11
KH
9442 struct frame *f
9443 = x_window_to_frame (dpyinfo, event.xclient.window);
58769bee 9444
7a13e894
RS
9445 new_x = event.xclient.data.s[0];
9446 new_y = event.xclient.data.s[1];
1fb20991 9447
7a13e894
RS
9448 if (f)
9449 {
7556890b
RS
9450 f->output_data.x->left_pos = new_x;
9451 f->output_data.x->top_pos = new_y;
7a13e894 9452 }
1fb20991 9453 }
0fdff6bb 9454#ifdef HACK_EDITRES
7a13e894
RS
9455 else if (event.xclient.message_type
9456 == dpyinfo->Xatom_editres)
9457 {
19126e11
KH
9458 struct frame *f
9459 = x_any_window_to_frame (dpyinfo, event.xclient.window);
7556890b 9460 _XEditResCheckMessages (f->output_data.x->widget, NULL,
19126e11 9461 &event, NULL);
7a13e894 9462 }
0fdff6bb 9463#endif /* HACK_EDITRES */
06a2c219
GM
9464 else if ((event.xclient.message_type
9465 == dpyinfo->Xatom_DONE)
9466 || (event.xclient.message_type
9467 == dpyinfo->Xatom_PAGE))
9468 {
9469 /* Ghostview job completed. Kill it. We could
9470 reply with "Next" if we received "Page", but we
9471 currently never do because we are interested in
9472 images, only, which should have 1 page. */
06a2c219
GM
9473 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9474 struct frame *f
9475 = x_window_to_frame (dpyinfo, event.xclient.window);
9476 x_kill_gs_process (pixmap, f);
9477 expose_frame (f, 0, 0, 0, 0);
9478 }
9479#ifdef USE_TOOLKIT_SCROLL_BARS
9480 /* Scroll bar callbacks send a ClientMessage from which
9481 we construct an input_event. */
9482 else if (event.xclient.message_type
9483 == dpyinfo->Xatom_Scrollbar)
9484 {
9485 x_scroll_bar_to_input_event (&event, bufp);
9486 ++bufp, ++count, --numchars;
9487 goto out;
9488 }
9489#endif /* USE_TOOLKIT_SCROLL_BARS */
9490 else
9491 goto OTHER;
7a13e894
RS
9492 }
9493 break;
dc6f92b8 9494
7a13e894 9495 case SelectionNotify:
3afe33e7 9496#ifdef USE_X_TOOLKIT
19126e11 9497 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
7a13e894 9498 goto OTHER;
3afe33e7 9499#endif /* not USE_X_TOOLKIT */
dfcf069d 9500 x_handle_selection_notify (&event.xselection);
7a13e894 9501 break;
d56a553a 9502
06a2c219 9503 case SelectionClear: /* Someone has grabbed ownership. */
3afe33e7 9504#ifdef USE_X_TOOLKIT
19126e11 9505 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
7a13e894 9506 goto OTHER;
3afe33e7 9507#endif /* USE_X_TOOLKIT */
7a13e894
RS
9508 {
9509 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
d56a553a 9510
7a13e894
RS
9511 if (numchars == 0)
9512 abort ();
d56a553a 9513
7a13e894
RS
9514 bufp->kind = selection_clear_event;
9515 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9516 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9517 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9518 bufp->frame_or_window = Qnil;
0f8aabe9 9519 bufp->arg = Qnil;
7a13e894 9520 bufp++;
d56a553a 9521
7a13e894
RS
9522 count += 1;
9523 numchars -= 1;
9524 }
9525 break;
dc6f92b8 9526
06a2c219 9527 case SelectionRequest: /* Someone wants our selection. */
3afe33e7 9528#ifdef USE_X_TOOLKIT
19126e11 9529 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
7a13e894 9530 goto OTHER;
3afe33e7 9531#endif /* USE_X_TOOLKIT */
7a13e894 9532 if (x_queue_selection_requests)
19126e11 9533 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
7a13e894
RS
9534 &event);
9535 else
9536 {
9537 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
dc6f92b8 9538
7a13e894
RS
9539 if (numchars == 0)
9540 abort ();
9541
9542 bufp->kind = selection_request_event;
9543 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9544 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9545 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9546 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9547 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9548 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9549 bufp->frame_or_window = Qnil;
0f8aabe9 9550 bufp->arg = Qnil;
7a13e894
RS
9551 bufp++;
9552
9553 count += 1;
9554 numchars -= 1;
9555 }
9556 break;
9557
9558 case PropertyNotify:
3afe33e7 9559#ifdef USE_X_TOOLKIT
19126e11 9560 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
7a13e894 9561 goto OTHER;
3afe33e7 9562#endif /* not USE_X_TOOLKIT */
dfcf069d 9563 x_handle_property_notify (&event.xproperty);
7a13e894 9564 break;
dc6f92b8 9565
7a13e894 9566 case ReparentNotify:
19126e11 9567 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
7a13e894
RS
9568 if (f)
9569 {
9570 int x, y;
7556890b 9571 f->output_data.x->parent_desc = event.xreparent.parent;
7a13e894 9572 x_real_positions (f, &x, &y);
7556890b
RS
9573 f->output_data.x->left_pos = x;
9574 f->output_data.x->top_pos = y;
7a13e894
RS
9575 }
9576 break;
3bd330d4 9577
7a13e894 9578 case Expose:
19126e11 9579 f = x_window_to_frame (dpyinfo, event.xexpose.window);
7a13e894 9580 if (f)
dc6f92b8 9581 {
7a13e894
RS
9582 if (f->async_visible == 0)
9583 {
9584 f->async_visible = 1;
9585 f->async_iconified = 0;
06c488fd 9586 f->output_data.x->has_been_visible = 1;
7a13e894
RS
9587 SET_FRAME_GARBAGED (f);
9588 }
9589 else
06a2c219
GM
9590 expose_frame (x_window_to_frame (dpyinfo,
9591 event.xexpose.window),
9592 event.xexpose.x, event.xexpose.y,
9593 event.xexpose.width, event.xexpose.height);
dc6f92b8
JB
9594 }
9595 else
7a13e894 9596 {
06a2c219
GM
9597#ifdef USE_TOOLKIT_SCROLL_BARS
9598 /* Dispatch event to the widget. */
9599 goto OTHER;
9600#else /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9601 struct scroll_bar *bar
9602 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 9603
7a13e894
RS
9604 if (bar)
9605 x_scroll_bar_expose (bar, &event);
3afe33e7 9606#ifdef USE_X_TOOLKIT
7a13e894
RS
9607 else
9608 goto OTHER;
3afe33e7 9609#endif /* USE_X_TOOLKIT */
06a2c219 9610#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9611 }
9612 break;
dc6f92b8 9613
7a13e894
RS
9614 case GraphicsExpose: /* This occurs when an XCopyArea's
9615 source area was obscured or not
9616 available.*/
19126e11 9617 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
7a13e894
RS
9618 if (f)
9619 {
06a2c219
GM
9620 expose_frame (f,
9621 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9622 event.xgraphicsexpose.width,
9623 event.xgraphicsexpose.height);
7a13e894 9624 }
3afe33e7 9625#ifdef USE_X_TOOLKIT
7a13e894
RS
9626 else
9627 goto OTHER;
3afe33e7 9628#endif /* USE_X_TOOLKIT */
7a13e894 9629 break;
dc6f92b8 9630
7a13e894 9631 case NoExpose: /* This occurs when an XCopyArea's
06a2c219
GM
9632 source area was completely
9633 available */
7a13e894 9634 break;
dc6f92b8 9635
7a13e894 9636 case UnmapNotify:
06a2c219
GM
9637 /* Redo the mouse-highlight after the tooltip has gone. */
9638 if (event.xmap.window == tip_window)
9639 {
9640 tip_window = 0;
9641 redo_mouse_highlight ();
9642 }
9643
91ea2a7a 9644 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
7a13e894
RS
9645 if (f) /* F may no longer exist if
9646 the frame was deleted. */
9647 {
9648 /* While a frame is unmapped, display generation is
9649 disabled; you don't want to spend time updating a
9650 display that won't ever be seen. */
9651 f->async_visible = 0;
9652 /* We can't distinguish, from the event, whether the window
9653 has become iconified or invisible. So assume, if it
9654 was previously visible, than now it is iconified.
1aa6072f
RS
9655 But x_make_frame_invisible clears both
9656 the visible flag and the iconified flag;
9657 and that way, we know the window is not iconified now. */
7a13e894 9658 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
1aa6072f
RS
9659 {
9660 f->async_iconified = 1;
bddd097c 9661
1aa6072f
RS
9662 bufp->kind = iconify_event;
9663 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9664 bufp->arg = Qnil;
1aa6072f
RS
9665 bufp++;
9666 count++;
9667 numchars--;
9668 }
7a13e894 9669 }
7a13e894 9670 goto OTHER;
dc6f92b8 9671
7a13e894 9672 case MapNotify:
06a2c219
GM
9673 if (event.xmap.window == tip_window)
9674 /* The tooltip has been drawn already. Avoid
9675 the SET_FRAME_GARBAGED below. */
9676 goto OTHER;
9677
9678 /* We use x_top_window_to_frame because map events can
9679 come for sub-windows and they don't mean that the
9680 frame is visible. */
19126e11 9681 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
7a13e894
RS
9682 if (f)
9683 {
9684 f->async_visible = 1;
9685 f->async_iconified = 0;
06c488fd 9686 f->output_data.x->has_been_visible = 1;
dc6f92b8 9687
7a13e894
RS
9688 /* wait_reading_process_input will notice this and update
9689 the frame's display structures. */
9690 SET_FRAME_GARBAGED (f);
bddd097c 9691
d806e720
RS
9692 if (f->iconified)
9693 {
9694 bufp->kind = deiconify_event;
9695 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9696 bufp->arg = Qnil;
d806e720
RS
9697 bufp++;
9698 count++;
9699 numchars--;
9700 }
e73ec6fa 9701 else if (! NILP (Vframe_list)
8e713be6 9702 && ! NILP (XCDR (Vframe_list)))
78aa2ba5
KH
9703 /* Force a redisplay sooner or later
9704 to update the frame titles
9705 in case this is the second frame. */
9706 record_asynch_buffer_change ();
7a13e894 9707 }
7a13e894 9708 goto OTHER;
dc6f92b8 9709
7a13e894 9710 case KeyPress:
19126e11 9711 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
f451eb13 9712
eccc05db 9713#if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
9714 /* I couldn't find a way to prevent LessTif scroll bars
9715 from consuming key events. */
9716 if (f == 0)
9717 {
9718 Widget widget = XtWindowToWidget (dpyinfo->display,
9719 event.xkey.window);
9720 if (widget && XmIsScrollBar (widget))
9721 {
9722 widget = XtParent (widget);
9723 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9724 }
9725 }
eccc05db 9726#endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
06a2c219 9727
7a13e894
RS
9728 if (f != 0)
9729 {
9730 KeySym keysym, orig_keysym;
379b5ac0
KH
9731 /* al%imercury@uunet.uu.net says that making this 81
9732 instead of 80 fixed a bug whereby meta chars made
9733 his Emacs hang.
9734
9735 It seems that some version of XmbLookupString has
9736 a bug of not returning XBufferOverflow in
9737 status_return even if the input is too long to
9738 fit in 81 bytes. So, we must prepare sufficient
9739 bytes for copy_buffer. 513 bytes (256 chars for
9740 two-byte character set) seems to be a faily good
9741 approximation. -- 2000.8.10 handa@etl.go.jp */
9742 unsigned char copy_buffer[513];
9743 unsigned char *copy_bufptr = copy_buffer;
9744 int copy_bufsiz = sizeof (copy_buffer);
7a13e894 9745 int modifiers;
64bb1782 9746
7a13e894
RS
9747 event.xkey.state
9748 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9749 extra_keyboard_modifiers);
9750 modifiers = event.xkey.state;
3a2712f9 9751
7a13e894 9752 /* This will have to go some day... */
752a043f 9753
7a13e894
RS
9754 /* make_lispy_event turns chars into control chars.
9755 Don't do it here because XLookupString is too eager. */
9756 event.xkey.state &= ~ControlMask;
5d46f928
RS
9757 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9758 | dpyinfo->super_mod_mask
9759 | dpyinfo->hyper_mod_mask
9760 | dpyinfo->alt_mod_mask);
9761
1cf4a0d1
RS
9762 /* In case Meta is ComposeCharacter,
9763 clear its status. According to Markus Ehrnsperger
9764 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9765 this enables ComposeCharacter to work whether or
9766 not it is combined with Meta. */
9767 if (modifiers & dpyinfo->meta_mod_mask)
9768 bzero (&compose_status, sizeof (compose_status));
9769
6c183ba5
RS
9770#ifdef HAVE_X_I18N
9771 if (FRAME_XIC (f))
9772 {
f5d11644
GM
9773 Status status_return;
9774
6c183ba5 9775 nbytes = XmbLookupString (FRAME_XIC (f),
f5d11644
GM
9776 &event.xkey, copy_bufptr,
9777 copy_bufsiz, &keysym,
6c183ba5 9778 &status_return);
f5d11644
GM
9779 if (status_return == XBufferOverflow)
9780 {
9781 copy_bufsiz = nbytes + 1;
9782 copy_bufptr = (char *) alloca (copy_bufsiz);
9783 nbytes = XmbLookupString (FRAME_XIC (f),
9784 &event.xkey, copy_bufptr,
9785 copy_bufsiz, &keysym,
9786 &status_return);
9787 }
9788
1decb680
PE
9789 if (status_return == XLookupNone)
9790 break;
9791 else if (status_return == XLookupChars)
fdd9d55e
GM
9792 {
9793 keysym = NoSymbol;
9794 modifiers = 0;
9795 }
1decb680
PE
9796 else if (status_return != XLookupKeySym
9797 && status_return != XLookupBoth)
9798 abort ();
6c183ba5
RS
9799 }
9800 else
379b5ac0
KH
9801 nbytes = XLookupString (&event.xkey, copy_bufptr,
9802 copy_bufsiz, &keysym,
9803 &compose_status);
6c183ba5 9804#else
379b5ac0
KH
9805 nbytes = XLookupString (&event.xkey, copy_bufptr,
9806 copy_bufsiz, &keysym,
9807 &compose_status);
6c183ba5 9808#endif
dc6f92b8 9809
7a13e894 9810 orig_keysym = keysym;
55123275 9811
7a13e894
RS
9812 if (numchars > 1)
9813 {
9814 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9815 || keysym == XK_Delete
1097aea0 9816#ifdef XK_ISO_Left_Tab
441affdb 9817 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
1097aea0 9818#endif
852bff8f 9819 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
7a13e894
RS
9820 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9821 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
c34790e0 9822#ifdef HPUX
7a13e894
RS
9823 /* This recognizes the "extended function keys".
9824 It seems there's no cleaner way.
9825 Test IsModifierKey to avoid handling mode_switch
9826 incorrectly. */
9827 || ((unsigned) (keysym) >= XK_Select
9828 && (unsigned)(keysym) < XK_KP_Space)
69388238
RS
9829#endif
9830#ifdef XK_dead_circumflex
7a13e894 9831 || orig_keysym == XK_dead_circumflex
69388238
RS
9832#endif
9833#ifdef XK_dead_grave
7a13e894 9834 || orig_keysym == XK_dead_grave
69388238
RS
9835#endif
9836#ifdef XK_dead_tilde
7a13e894 9837 || orig_keysym == XK_dead_tilde
69388238
RS
9838#endif
9839#ifdef XK_dead_diaeresis
7a13e894 9840 || orig_keysym == XK_dead_diaeresis
69388238
RS
9841#endif
9842#ifdef XK_dead_macron
7a13e894 9843 || orig_keysym == XK_dead_macron
69388238
RS
9844#endif
9845#ifdef XK_dead_degree
7a13e894 9846 || orig_keysym == XK_dead_degree
69388238
RS
9847#endif
9848#ifdef XK_dead_acute
7a13e894 9849 || orig_keysym == XK_dead_acute
69388238
RS
9850#endif
9851#ifdef XK_dead_cedilla
7a13e894 9852 || orig_keysym == XK_dead_cedilla
69388238
RS
9853#endif
9854#ifdef XK_dead_breve
7a13e894 9855 || orig_keysym == XK_dead_breve
69388238
RS
9856#endif
9857#ifdef XK_dead_ogonek
7a13e894 9858 || orig_keysym == XK_dead_ogonek
69388238
RS
9859#endif
9860#ifdef XK_dead_caron
7a13e894 9861 || orig_keysym == XK_dead_caron
69388238
RS
9862#endif
9863#ifdef XK_dead_doubleacute
7a13e894 9864 || orig_keysym == XK_dead_doubleacute
69388238
RS
9865#endif
9866#ifdef XK_dead_abovedot
7a13e894 9867 || orig_keysym == XK_dead_abovedot
c34790e0 9868#endif
7a13e894
RS
9869 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9870 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9871 /* Any "vendor-specific" key is ok. */
9872 || (orig_keysym & (1 << 28)))
9873 && ! (IsModifierKey (orig_keysym)
7719aa06
RS
9874#ifndef HAVE_X11R5
9875#ifdef XK_Mode_switch
7a13e894 9876 || ((unsigned)(orig_keysym) == XK_Mode_switch)
7719aa06
RS
9877#endif
9878#ifdef XK_Num_Lock
7a13e894 9879 || ((unsigned)(orig_keysym) == XK_Num_Lock)
7719aa06
RS
9880#endif
9881#endif /* not HAVE_X11R5 */
7a13e894 9882 ))
dc6f92b8 9883 {
10e6549c
RS
9884 if (temp_index == sizeof temp_buffer / sizeof (short))
9885 temp_index = 0;
7a13e894
RS
9886 temp_buffer[temp_index++] = keysym;
9887 bufp->kind = non_ascii_keystroke;
9888 bufp->code = keysym;
e0c1aef2 9889 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9890 bufp->arg = Qnil;
334208b7
RS
9891 bufp->modifiers
9892 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9893 modifiers);
1113d9db 9894 bufp->timestamp = event.xkey.time;
dc6f92b8 9895 bufp++;
7a13e894
RS
9896 count++;
9897 numchars--;
dc6f92b8 9898 }
7a13e894
RS
9899 else if (numchars > nbytes)
9900 {
9901 register int i;
379b5ac0 9902 register int c;
379b5ac0 9903 int nchars, len;
7a13e894
RS
9904
9905 for (i = 0; i < nbytes; i++)
9906 {
379b5ac0
KH
9907 if (temp_index == (sizeof temp_buffer
9908 / sizeof (short)))
7a13e894 9909 temp_index = 0;
379b5ac0
KH
9910 temp_buffer[temp_index++] = copy_bufptr[i];
9911 }
9912
9913 if (/* If the event is not from XIM, */
9914 event.xkey.keycode != 0
9915 /* or the current locale doesn't request
9916 decoding of the intup data, ... */
9917 || coding.type == coding_type_raw_text
9918 || coding.type == coding_type_no_conversion)
9919 {
9920 /* ... we can use the input data as is. */
9921 nchars = nbytes;
9922 }
9923 else
9924 {
9925 /* We have to decode the input data. */
9926 int require;
9927 unsigned char *p;
9928
9929 require = decoding_buffer_size (&coding, nbytes);
9930 p = (unsigned char *) alloca (require);
9931 coding.mode |= CODING_MODE_LAST_BLOCK;
9932 decode_coding (&coding, copy_bufptr, p,
9933 nbytes, require);
9934 nbytes = coding.produced;
9935 nchars = coding.produced_char;
9936 copy_bufptr = p;
9937 }
9938
9939 /* Convert the input data to a sequence of
9940 character events. */
9941 for (i = 0; i < nbytes; i += len)
9942 {
9943 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
9944 nbytes - i, len);
9945 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
9946 ? ascii_keystroke
9947 : multibyte_char_keystroke);
9948 bufp->code = c;
7a13e894 9949 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9950 bufp->arg = Qnil;
7a13e894
RS
9951 bufp->modifiers
9952 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9953 modifiers);
9954 bufp->timestamp = event.xkey.time;
9955 bufp++;
9956 }
9957
379b5ac0
KH
9958 count += nchars;
9959 numchars -= nchars;
1decb680
PE
9960
9961 if (keysym == NoSymbol)
9962 break;
7a13e894
RS
9963 }
9964 else
9965 abort ();
dc6f92b8 9966 }
10e6549c
RS
9967 else
9968 abort ();
dc6f92b8 9969 }
59ddecde
GM
9970#ifdef HAVE_X_I18N
9971 /* Don't dispatch this event since XtDispatchEvent calls
9972 XFilterEvent, and two calls in a row may freeze the
9973 client. */
9974 break;
9975#else
717ca130 9976 goto OTHER;
59ddecde 9977#endif
f451eb13 9978
f5d11644 9979 case KeyRelease:
59ddecde
GM
9980#ifdef HAVE_X_I18N
9981 /* Don't dispatch this event since XtDispatchEvent calls
9982 XFilterEvent, and two calls in a row may freeze the
9983 client. */
9984 break;
9985#else
f5d11644 9986 goto OTHER;
59ddecde 9987#endif
f5d11644 9988
7a13e894 9989 /* Here's a possible interpretation of the whole
06a2c219
GM
9990 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
9991 you get a FocusIn event, you have to get a FocusOut
9992 event before you relinquish the focus. If you
9993 haven't received a FocusIn event, then a mere
9994 LeaveNotify is enough to free you. */
f451eb13 9995
7a13e894 9996 case EnterNotify:
06a2c219
GM
9997 {
9998 int from_menu_bar_p = 0;
9999
10000 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10001
10002#ifdef LESSTIF_VERSION
10003 /* When clicking outside of a menu bar popup to close
10004 it, we get a FocusIn/ EnterNotify sequence of
10005 events. The flag event.xcrossing.focus is not set
10006 in the EnterNotify event of that sequence because
10007 the focus is in the menu bar,
10008 event.xcrossing.window is the frame's X window.
10009 Unconditionally setting the focus frame to null in
10010 this case is not the right thing, because no event
10011 follows that could set the focus frame to the right
10012 value.
10013
10014 This could be a LessTif bug, but I wasn't able to
10015 reproduce the behavior in a simple test program.
10016
10017 (gerd, LessTif 0.88.1). */
10018
10019 if (!event.xcrossing.focus
10020 && f
10021 && f->output_data.x->menubar_widget)
10022 {
10023 Window focus;
10024 int revert;
10025
10026 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
10027 if (focus == XtWindow (f->output_data.x->menubar_widget))
10028 from_menu_bar_p = 1;
10029 }
10030#endif /* LESSTIF_VERSION */
6d4238f3 10031
06a2c219
GM
10032 if (event.xcrossing.focus || from_menu_bar_p)
10033 {
10034 /* Avoid nasty pop/raise loops. */
10035 if (f && (!(f->auto_raise)
10036 || !(f->auto_lower)
10037 || (event.xcrossing.time - enter_timestamp) > 500))
10038 {
10039 x_new_focus_frame (dpyinfo, f);
10040 enter_timestamp = event.xcrossing.time;
10041 }
10042 }
10043 else if (f == dpyinfo->x_focus_frame)
10044 x_new_focus_frame (dpyinfo, 0);
10045
10046 /* EnterNotify counts as mouse movement,
10047 so update things that depend on mouse position. */
10048 if (f && !f->output_data.x->busy_p)
10049 note_mouse_movement (f, &event.xmotion);
10050 goto OTHER;
10051 }
dc6f92b8 10052
7a13e894 10053 case FocusIn:
19126e11 10054 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 10055 if (event.xfocus.detail != NotifyPointer)
0f941935 10056 dpyinfo->x_focus_event_frame = f;
7a13e894 10057 if (f)
eb72635f
GM
10058 {
10059 x_new_focus_frame (dpyinfo, f);
10060
10061 /* Don't stop displaying the initial startup message
10062 for a switch-frame event we don't need. */
10063 if (GC_NILP (Vterminal_frame)
10064 && GC_CONSP (Vframe_list)
10065 && !GC_NILP (XCDR (Vframe_list)))
10066 {
10067 bufp->kind = FOCUS_IN_EVENT;
10068 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10069 bufp->arg = Qnil;
eb72635f
GM
10070 ++bufp, ++count, --numchars;
10071 }
10072 }
f9e24cb9 10073
6c183ba5
RS
10074#ifdef HAVE_X_I18N
10075 if (f && FRAME_XIC (f))
10076 XSetICFocus (FRAME_XIC (f));
10077#endif
10078
7a13e894 10079 goto OTHER;
10c5e63d 10080
7a13e894 10081 case LeaveNotify:
19126e11 10082 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
7a13e894 10083 if (f)
10c5e63d 10084 {
06a2c219
GM
10085 int from_menu_bar_p = 0;
10086
7a13e894 10087 if (f == dpyinfo->mouse_face_mouse_frame)
06a2c219
GM
10088 {
10089 /* If we move outside the frame, then we're
10090 certainly no longer on any text in the frame. */
10091 clear_mouse_face (dpyinfo);
10092 dpyinfo->mouse_face_mouse_frame = 0;
10093 }
10094
10095 /* Generate a nil HELP_EVENT to cancel a help-echo.
10096 Do it only if there's something to cancel.
10097 Otherwise, the startup message is cleared when
10098 the mouse leaves the frame. */
10099 if (any_help_event_p)
10100 {
be010514
GM
10101 Lisp_Object frame;
10102 int n;
10103
06a2c219 10104 XSETFRAME (frame, f);
82c5d67a 10105 help_echo = Qnil;
5ab2570d
GM
10106 n = gen_help_event (bufp, numchars,
10107 Qnil, frame, Qnil, Qnil, 0);
be010514 10108 bufp += n, count += n, numchars -= n;
06a2c219 10109 }
7a13e894 10110
06a2c219
GM
10111#ifdef LESSTIF_VERSION
10112 /* Please see the comment at the start of the
10113 EnterNotify case. */
10114 if (!event.xcrossing.focus
10115 && f->output_data.x->menubar_widget)
10116 {
10117 Window focus;
10118 int revert;
10119 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
10120 if (focus == XtWindow (f->output_data.x->menubar_widget))
10121 from_menu_bar_p = 1;
10122 }
10123#endif /* LESSTIF_VERSION */
10124
10125 if (event.xcrossing.focus || from_menu_bar_p)
0f941935 10126 x_mouse_leave (dpyinfo);
10c5e63d 10127 else
7a13e894 10128 {
0f941935
KH
10129 if (f == dpyinfo->x_focus_event_frame)
10130 dpyinfo->x_focus_event_frame = 0;
10131 if (f == dpyinfo->x_focus_frame)
10132 x_new_focus_frame (dpyinfo, 0);
7a13e894 10133 }
10c5e63d 10134 }
7a13e894 10135 goto OTHER;
dc6f92b8 10136
7a13e894 10137 case FocusOut:
19126e11 10138 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 10139 if (event.xfocus.detail != NotifyPointer
0f941935
KH
10140 && f == dpyinfo->x_focus_event_frame)
10141 dpyinfo->x_focus_event_frame = 0;
10142 if (f && f == dpyinfo->x_focus_frame)
10143 x_new_focus_frame (dpyinfo, 0);
f9e24cb9 10144
6c183ba5
RS
10145#ifdef HAVE_X_I18N
10146 if (f && FRAME_XIC (f))
10147 XUnsetICFocus (FRAME_XIC (f));
10148#endif
10149
7a13e894 10150 goto OTHER;
dc6f92b8 10151
7a13e894 10152 case MotionNotify:
dc6f92b8 10153 {
06a2c219 10154 previous_help_echo = help_echo;
7cea38bc 10155 help_echo = help_echo_object = help_echo_window = Qnil;
be010514 10156 help_echo_pos = -1;
06a2c219 10157
7a13e894
RS
10158 if (dpyinfo->grabbed && last_mouse_frame
10159 && FRAME_LIVE_P (last_mouse_frame))
10160 f = last_mouse_frame;
10161 else
19126e11 10162 f = x_window_to_frame (dpyinfo, event.xmotion.window);
06a2c219 10163
7a13e894
RS
10164 if (f)
10165 note_mouse_movement (f, &event.xmotion);
10166 else
10167 {
e88b3c50 10168#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
10169 struct scroll_bar *bar
10170 = x_window_to_scroll_bar (event.xmotion.window);
f451eb13 10171
7a13e894
RS
10172 if (bar)
10173 x_scroll_bar_note_movement (bar, &event);
e88b3c50 10174#endif /* USE_TOOLKIT_SCROLL_BARS */
b8009dd1 10175
06a2c219
GM
10176 /* If we move outside the frame, then we're
10177 certainly no longer on any text in the frame. */
7a13e894
RS
10178 clear_mouse_face (dpyinfo);
10179 }
06a2c219
GM
10180
10181 /* If the contents of the global variable help_echo
10182 has changed, generate a HELP_EVENT. */
b7e80413
SM
10183 if (!NILP (help_echo)
10184 || !NILP (previous_help_echo))
06a2c219
GM
10185 {
10186 Lisp_Object frame;
be010514 10187 int n;
06a2c219
GM
10188
10189 if (f)
10190 XSETFRAME (frame, f);
10191 else
10192 frame = Qnil;
10193
10194 any_help_event_p = 1;
5ab2570d 10195 n = gen_help_event (bufp, numchars, help_echo, frame,
7cea38bc
GM
10196 help_echo_window, help_echo_object,
10197 help_echo_pos);
be010514 10198 bufp += n, count += n, numchars -= n;
06a2c219
GM
10199 }
10200
10201 goto OTHER;
dc6f92b8 10202 }
dc6f92b8 10203
7a13e894 10204 case ConfigureNotify:
9829ddba
RS
10205 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10206 if (f)
af395ec1 10207 {
5c187dee 10208#ifndef USE_X_TOOLKIT
bf1b7b30
KH
10209 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10210 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
5c187dee 10211
2d7fc7e8
RS
10212 /* In the toolkit version, change_frame_size
10213 is called by the code that handles resizing
10214 of the EmacsFrame widget. */
7a13e894 10215
7a13e894
RS
10216 /* Even if the number of character rows and columns has
10217 not changed, the font size may have changed, so we need
10218 to check the pixel dimensions as well. */
10219 if (columns != f->width
10220 || rows != f->height
7556890b
RS
10221 || event.xconfigure.width != f->output_data.x->pixel_width
10222 || event.xconfigure.height != f->output_data.x->pixel_height)
7a13e894 10223 {
7d1e984f 10224 change_frame_size (f, rows, columns, 0, 1, 0);
7a13e894 10225 SET_FRAME_GARBAGED (f);
e687d06e 10226 cancel_mouse_face (f);
7a13e894 10227 }
2d7fc7e8 10228#endif
af395ec1 10229
7556890b
RS
10230 f->output_data.x->pixel_width = event.xconfigure.width;
10231 f->output_data.x->pixel_height = event.xconfigure.height;
7a13e894
RS
10232
10233 /* What we have now is the position of Emacs's own window.
10234 Convert that to the position of the window manager window. */
dcb07ae9
RS
10235 x_real_positions (f, &f->output_data.x->left_pos,
10236 &f->output_data.x->top_pos);
10237
f5d11644
GM
10238#ifdef HAVE_X_I18N
10239 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10240 xic_set_statusarea (f);
10241#endif
10242
dcb07ae9
RS
10243 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10244 {
10245 /* Since the WM decorations come below top_pos now,
10246 we must put them below top_pos in the future. */
10247 f->output_data.x->win_gravity = NorthWestGravity;
10248 x_wm_set_size_hint (f, (long) 0, 0);
10249 }
8f08dc93
KH
10250#ifdef USE_MOTIF
10251 /* Some window managers pass (0,0) as the location of
10252 the window, and the Motif event handler stores it
10253 in the emacs widget, which messes up Motif menus. */
10254 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
10255 {
10256 event.xconfigure.x = f->output_data.x->widget->core.x;
10257 event.xconfigure.y = f->output_data.x->widget->core.y;
10258 }
06a2c219 10259#endif /* USE_MOTIF */
7a13e894 10260 }
2d7fc7e8 10261 goto OTHER;
dc6f92b8 10262
7a13e894
RS
10263 case ButtonPress:
10264 case ButtonRelease:
10265 {
10266 /* If we decide we want to generate an event to be seen
10267 by the rest of Emacs, we put it here. */
10268 struct input_event emacs_event;
9ea173e8 10269 int tool_bar_p = 0;
06a2c219 10270
7a13e894 10271 emacs_event.kind = no_event;
7a13e894 10272 bzero (&compose_status, sizeof (compose_status));
9b07615b 10273
06a2c219
GM
10274 if (dpyinfo->grabbed
10275 && last_mouse_frame
9f67f20b
RS
10276 && FRAME_LIVE_P (last_mouse_frame))
10277 f = last_mouse_frame;
10278 else
2224b905 10279 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9f67f20b 10280
06a2c219
GM
10281 if (f)
10282 {
9ea173e8
GM
10283 /* Is this in the tool-bar? */
10284 if (WINDOWP (f->tool_bar_window)
10285 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
06a2c219
GM
10286 {
10287 Lisp_Object window;
10288 int p, x, y;
10289
10290 x = event.xbutton.x;
10291 y = event.xbutton.y;
10292
10293 /* Set x and y. */
10294 window = window_from_coordinates (f, x, y, &p, 1);
9ea173e8 10295 if (EQ (window, f->tool_bar_window))
06a2c219 10296 {
9ea173e8
GM
10297 x_handle_tool_bar_click (f, &event.xbutton);
10298 tool_bar_p = 1;
06a2c219
GM
10299 }
10300 }
10301
9ea173e8 10302 if (!tool_bar_p)
06a2c219
GM
10303 if (!dpyinfo->x_focus_frame
10304 || f == dpyinfo->x_focus_frame)
10305 construct_mouse_click (&emacs_event, &event, f);
7a13e894
RS
10306 }
10307 else
10308 {
06a2c219 10309#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
10310 struct scroll_bar *bar
10311 = x_window_to_scroll_bar (event.xbutton.window);
f451eb13 10312
7a13e894
RS
10313 if (bar)
10314 x_scroll_bar_handle_click (bar, &event, &emacs_event);
06a2c219 10315#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
10316 }
10317
10318 if (event.type == ButtonPress)
10319 {
10320 dpyinfo->grabbed |= (1 << event.xbutton.button);
10321 last_mouse_frame = f;
edad46f6
KH
10322 /* Ignore any mouse motion that happened
10323 before this event; any subsequent mouse-movement
10324 Emacs events should reflect only motion after
10325 the ButtonPress. */
a00e91cd
KH
10326 if (f != 0)
10327 f->mouse_moved = 0;
06a2c219 10328
9ea173e8
GM
10329 if (!tool_bar_p)
10330 last_tool_bar_item = -1;
7a13e894 10331 }
3afe33e7
RS
10332 else
10333 {
7a13e894 10334 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
3afe33e7 10335 }
23faf38f 10336
7a13e894
RS
10337 if (numchars >= 1 && emacs_event.kind != no_event)
10338 {
10339 bcopy (&emacs_event, bufp, sizeof (struct input_event));
10340 bufp++;
10341 count++;
10342 numchars--;
10343 }
3afe33e7
RS
10344
10345#ifdef USE_X_TOOLKIT
2224b905
RS
10346 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
10347 /* For a down-event in the menu bar,
10348 don't pass it to Xt right now.
10349 Instead, save it away
10350 and we will pass it to Xt from kbd_buffer_get_event.
10351 That way, we can run some Lisp code first. */
91375f8f
RS
10352 if (f && event.type == ButtonPress
10353 /* Verify the event is really within the menu bar
10354 and not just sent to it due to grabbing. */
10355 && event.xbutton.x >= 0
10356 && event.xbutton.x < f->output_data.x->pixel_width
10357 && event.xbutton.y >= 0
10358 && event.xbutton.y < f->output_data.x->menubar_height
10359 && event.xbutton.same_screen)
2224b905 10360 {
8805890a 10361 SET_SAVED_BUTTON_EVENT;
2237cac9
RS
10362 XSETFRAME (last_mouse_press_frame, f);
10363 }
10364 else if (event.type == ButtonPress)
10365 {
10366 last_mouse_press_frame = Qnil;
30e671c3 10367 goto OTHER;
ce89ef46 10368 }
06a2c219 10369
2237cac9
RS
10370#ifdef USE_MOTIF /* This should do not harm for Lucid,
10371 but I am trying to be cautious. */
ce89ef46
RS
10372 else if (event.type == ButtonRelease)
10373 {
2237cac9 10374 if (!NILP (last_mouse_press_frame))
f10ded1c 10375 {
2237cac9
RS
10376 f = XFRAME (last_mouse_press_frame);
10377 if (f->output_data.x)
06a2c219 10378 SET_SAVED_BUTTON_EVENT;
f10ded1c 10379 }
06a2c219 10380 else
30e671c3 10381 goto OTHER;
2224b905 10382 }
2237cac9 10383#endif /* USE_MOTIF */
2224b905
RS
10384 else
10385 goto OTHER;
3afe33e7 10386#endif /* USE_X_TOOLKIT */
7a13e894
RS
10387 }
10388 break;
dc6f92b8 10389
7a13e894 10390 case CirculateNotify:
06a2c219
GM
10391 goto OTHER;
10392
7a13e894 10393 case CirculateRequest:
06a2c219
GM
10394 goto OTHER;
10395
10396 case VisibilityNotify:
10397 goto OTHER;
dc6f92b8 10398
7a13e894
RS
10399 case MappingNotify:
10400 /* Someone has changed the keyboard mapping - update the
10401 local cache. */
10402 switch (event.xmapping.request)
10403 {
10404 case MappingModifier:
10405 x_find_modifier_meanings (dpyinfo);
10406 /* This is meant to fall through. */
10407 case MappingKeyboard:
10408 XRefreshKeyboardMapping (&event.xmapping);
10409 }
7a13e894 10410 goto OTHER;
dc6f92b8 10411
7a13e894 10412 default:
7a13e894 10413 OTHER:
717ca130 10414#ifdef USE_X_TOOLKIT
7a13e894
RS
10415 BLOCK_INPUT;
10416 XtDispatchEvent (&event);
10417 UNBLOCK_INPUT;
3afe33e7 10418#endif /* USE_X_TOOLKIT */
7a13e894
RS
10419 break;
10420 }
dc6f92b8
JB
10421 }
10422 }
10423
06a2c219
GM
10424 out:;
10425
9a5196d0
RS
10426 /* On some systems, an X bug causes Emacs to get no more events
10427 when the window is destroyed. Detect that. (1994.) */
58769bee 10428 if (! event_found)
ef2a22d0 10429 {
ef2a22d0
RS
10430 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
10431 One XNOOP in 100 loops will make Emacs terminate.
10432 B. Bretthauer, 1994 */
10433 x_noop_count++;
58769bee 10434 if (x_noop_count >= 100)
ef2a22d0
RS
10435 {
10436 x_noop_count=0;
2224b905
RS
10437
10438 if (next_noop_dpyinfo == 0)
10439 next_noop_dpyinfo = x_display_list;
10440
10441 XNoOp (next_noop_dpyinfo->display);
10442
10443 /* Each time we get here, cycle through the displays now open. */
10444 next_noop_dpyinfo = next_noop_dpyinfo->next;
ef2a22d0
RS
10445 }
10446 }
502add23 10447
06a2c219 10448 /* If the focus was just given to an auto-raising frame,
0134a210 10449 raise it now. */
7a13e894 10450 /* ??? This ought to be able to handle more than one such frame. */
0134a210
RS
10451 if (pending_autoraise_frame)
10452 {
10453 x_raise_frame (pending_autoraise_frame);
10454 pending_autoraise_frame = 0;
10455 }
0134a210 10456
dc6f92b8 10457 UNBLOCK_INPUT;
bde5503b 10458 --handling_signal;
dc6f92b8
JB
10459 return count;
10460}
06a2c219
GM
10461
10462
10463
dc6f92b8 10464\f
06a2c219
GM
10465/***********************************************************************
10466 Text Cursor
10467 ***********************************************************************/
10468
10469/* Note if the text cursor of window W has been overwritten by a
10470 drawing operation that outputs N glyphs starting at HPOS in the
10471 line given by output_cursor.vpos. N < 0 means all the rest of the
10472 line after HPOS has been written. */
10473
10474static void
10475note_overwritten_text_cursor (w, hpos, n)
10476 struct window *w;
10477 int hpos, n;
10478{
10479 if (updated_area == TEXT_AREA
10480 && output_cursor.vpos == w->phys_cursor.vpos
10481 && hpos <= w->phys_cursor.hpos
10482 && (n < 0
10483 || hpos + n > w->phys_cursor.hpos))
10484 w->phys_cursor_on_p = 0;
10485}
f451eb13
JB
10486
10487
06a2c219
GM
10488/* Set clipping for output in glyph row ROW. W is the window in which
10489 we operate. GC is the graphics context to set clipping in.
10490 WHOLE_LINE_P non-zero means include the areas used for truncation
10491 mark display and alike in the clipping rectangle.
10492
10493 ROW may be a text row or, e.g., a mode line. Text rows must be
10494 clipped to the interior of the window dedicated to text display,
10495 mode lines must be clipped to the whole window. */
dc6f92b8
JB
10496
10497static void
06a2c219
GM
10498x_clip_to_row (w, row, gc, whole_line_p)
10499 struct window *w;
10500 struct glyph_row *row;
10501 GC gc;
10502 int whole_line_p;
dc6f92b8 10503{
06a2c219
GM
10504 struct frame *f = XFRAME (WINDOW_FRAME (w));
10505 XRectangle clip_rect;
10506 int window_x, window_y, window_width, window_height;
dc6f92b8 10507
06a2c219 10508 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5c1aae96 10509
06a2c219
GM
10510 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10511 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10512 clip_rect.y = max (clip_rect.y, window_y);
10513 clip_rect.width = window_width;
10514 clip_rect.height = row->visible_height;
5c1aae96 10515
06a2c219
GM
10516 /* If clipping to the whole line, including trunc marks, extend
10517 the rectangle to the left and increase its width. */
10518 if (whole_line_p)
10519 {
110859fc
GM
10520 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10521 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
06a2c219 10522 }
5c1aae96 10523
06a2c219 10524 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
dc6f92b8
JB
10525}
10526
06a2c219
GM
10527
10528/* Draw a hollow box cursor on window W in glyph row ROW. */
dc6f92b8
JB
10529
10530static void
06a2c219
GM
10531x_draw_hollow_cursor (w, row)
10532 struct window *w;
10533 struct glyph_row *row;
dc6f92b8 10534{
06a2c219
GM
10535 struct frame *f = XFRAME (WINDOW_FRAME (w));
10536 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10537 Display *dpy = FRAME_X_DISPLAY (f);
10538 int x, y, wd, h;
10539 XGCValues xgcv;
10540 struct glyph *cursor_glyph;
10541 GC gc;
10542
10543 /* Compute frame-relative coordinates from window-relative
10544 coordinates. */
10545 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10546 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10547 + row->ascent - w->phys_cursor_ascent);
10548 h = row->height - 1;
10549
10550 /* Get the glyph the cursor is on. If we can't tell because
10551 the current matrix is invalid or such, give up. */
10552 cursor_glyph = get_phys_cursor_glyph (w);
10553 if (cursor_glyph == NULL)
dc6f92b8
JB
10554 return;
10555
06a2c219
GM
10556 /* Compute the width of the rectangle to draw. If on a stretch
10557 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10558 rectangle as wide as the glyph, but use a canonical character
10559 width instead. */
10560 wd = cursor_glyph->pixel_width - 1;
10561 if (cursor_glyph->type == STRETCH_GLYPH
10562 && !x_stretch_cursor_p)
10563 wd = min (CANON_X_UNIT (f), wd);
10564
10565 /* The foreground of cursor_gc is typically the same as the normal
10566 background color, which can cause the cursor box to be invisible. */
10567 xgcv.foreground = f->output_data.x->cursor_pixel;
10568 if (dpyinfo->scratch_cursor_gc)
10569 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10570 else
10571 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10572 GCForeground, &xgcv);
10573 gc = dpyinfo->scratch_cursor_gc;
10574
10575 /* Set clipping, draw the rectangle, and reset clipping again. */
10576 x_clip_to_row (w, row, gc, 0);
10577 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10578 XSetClipMask (dpy, gc, None);
dc6f92b8
JB
10579}
10580
06a2c219
GM
10581
10582/* Draw a bar cursor on window W in glyph row ROW.
10583
10584 Implementation note: One would like to draw a bar cursor with an
10585 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10586 Unfortunately, I didn't find a font yet that has this property set.
10587 --gerd. */
dc6f92b8
JB
10588
10589static void
f02d8aa0 10590x_draw_bar_cursor (w, row, width)
06a2c219
GM
10591 struct window *w;
10592 struct glyph_row *row;
f02d8aa0 10593 int width;
dc6f92b8 10594{
92f424df
GM
10595 struct frame *f = XFRAME (w->frame);
10596 struct glyph *cursor_glyph;
10597 GC gc;
10598 int x;
10599 unsigned long mask;
10600 XGCValues xgcv;
10601 Display *dpy;
10602 Window window;
06a2c219 10603
92f424df
GM
10604 /* If cursor is out of bounds, don't draw garbage. This can happen
10605 in mini-buffer windows when switching between echo area glyphs
10606 and mini-buffer. */
10607 cursor_glyph = get_phys_cursor_glyph (w);
10608 if (cursor_glyph == NULL)
10609 return;
06a2c219 10610
92f424df
GM
10611 /* If on an image, draw like a normal cursor. That's usually better
10612 visible than drawing a bar, esp. if the image is large so that
10613 the bar might not be in the window. */
10614 if (cursor_glyph->type == IMAGE_GLYPH)
10615 {
10616 struct glyph_row *row;
10617 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
10618 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
10619 }
10620 else
10621 {
06a2c219
GM
10622 xgcv.background = f->output_data.x->cursor_pixel;
10623 xgcv.foreground = f->output_data.x->cursor_pixel;
10624 xgcv.graphics_exposures = 0;
10625 mask = GCForeground | GCBackground | GCGraphicsExposures;
10626 dpy = FRAME_X_DISPLAY (f);
10627 window = FRAME_X_WINDOW (f);
10628 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
92f424df 10629
06a2c219
GM
10630 if (gc)
10631 XChangeGC (dpy, gc, mask, &xgcv);
10632 else
10633 {
10634 gc = XCreateGC (dpy, window, mask, &xgcv);
10635 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10636 }
92f424df 10637
f02d8aa0
GM
10638 if (width < 0)
10639 width = f->output_data.x->cursor_width;
92f424df 10640
06a2c219
GM
10641 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10642 x_clip_to_row (w, row, gc, 0);
10643 XFillRectangle (dpy, window, gc,
10644 x,
10645 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
f02d8aa0 10646 min (cursor_glyph->pixel_width, width),
06a2c219
GM
10647 row->height);
10648 XSetClipMask (dpy, gc, None);
10649 }
dc6f92b8
JB
10650}
10651
06a2c219
GM
10652
10653/* Clear the cursor of window W to background color, and mark the
10654 cursor as not shown. This is used when the text where the cursor
10655 is is about to be rewritten. */
10656
dc6f92b8 10657static void
06a2c219
GM
10658x_clear_cursor (w)
10659 struct window *w;
dc6f92b8 10660{
06a2c219
GM
10661 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10662 x_update_window_cursor (w, 0);
10663}
90e65f07 10664
dbc4e1c1 10665
06a2c219
GM
10666/* Draw the cursor glyph of window W in glyph row ROW. See the
10667 comment of x_draw_glyphs for the meaning of HL. */
dbc4e1c1 10668
06a2c219
GM
10669static void
10670x_draw_phys_cursor_glyph (w, row, hl)
10671 struct window *w;
10672 struct glyph_row *row;
10673 enum draw_glyphs_face hl;
10674{
10675 /* If cursor hpos is out of bounds, don't draw garbage. This can
10676 happen in mini-buffer windows when switching between echo area
10677 glyphs and mini-buffer. */
10678 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
66ac4b0e
GM
10679 {
10680 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10681 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10682 hl, 0, 0, 0);
10683
10684 /* When we erase the cursor, and ROW is overlapped by other
10685 rows, make sure that these overlapping parts of other rows
10686 are redrawn. */
10687 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10688 {
10689 if (row > w->current_matrix->rows
10690 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10691 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10692
10693 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10694 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10695 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10696 }
10697 }
06a2c219 10698}
dbc4e1c1 10699
eea6af04 10700
06a2c219 10701/* Erase the image of a cursor of window W from the screen. */
eea6af04 10702
06a2c219
GM
10703static void
10704x_erase_phys_cursor (w)
10705 struct window *w;
10706{
10707 struct frame *f = XFRAME (w->frame);
10708 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10709 int hpos = w->phys_cursor.hpos;
10710 int vpos = w->phys_cursor.vpos;
10711 int mouse_face_here_p = 0;
10712 struct glyph_matrix *active_glyphs = w->current_matrix;
10713 struct glyph_row *cursor_row;
10714 struct glyph *cursor_glyph;
10715 enum draw_glyphs_face hl;
10716
10717 /* No cursor displayed or row invalidated => nothing to do on the
10718 screen. */
10719 if (w->phys_cursor_type == NO_CURSOR)
10720 goto mark_cursor_off;
10721
10722 /* VPOS >= active_glyphs->nrows means that window has been resized.
10723 Don't bother to erase the cursor. */
10724 if (vpos >= active_glyphs->nrows)
10725 goto mark_cursor_off;
10726
10727 /* If row containing cursor is marked invalid, there is nothing we
10728 can do. */
10729 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10730 if (!cursor_row->enabled_p)
10731 goto mark_cursor_off;
10732
10733 /* This can happen when the new row is shorter than the old one.
10734 In this case, either x_draw_glyphs or clear_end_of_line
10735 should have cleared the cursor. Note that we wouldn't be
10736 able to erase the cursor in this case because we don't have a
10737 cursor glyph at hand. */
10738 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10739 goto mark_cursor_off;
10740
10741 /* If the cursor is in the mouse face area, redisplay that when
10742 we clear the cursor. */
8801a864
KR
10743 if (! NILP (dpyinfo->mouse_face_window)
10744 && w == XWINDOW (dpyinfo->mouse_face_window)
06a2c219
GM
10745 && (vpos > dpyinfo->mouse_face_beg_row
10746 || (vpos == dpyinfo->mouse_face_beg_row
10747 && hpos >= dpyinfo->mouse_face_beg_col))
10748 && (vpos < dpyinfo->mouse_face_end_row
10749 || (vpos == dpyinfo->mouse_face_end_row
10750 && hpos < dpyinfo->mouse_face_end_col))
10751 /* Don't redraw the cursor's spot in mouse face if it is at the
10752 end of a line (on a newline). The cursor appears there, but
10753 mouse highlighting does not. */
10754 && cursor_row->used[TEXT_AREA] > hpos)
10755 mouse_face_here_p = 1;
10756
10757 /* Maybe clear the display under the cursor. */
10758 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10759 {
10760 int x;
045dee35 10761 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dbc4e1c1 10762
06a2c219
GM
10763 cursor_glyph = get_phys_cursor_glyph (w);
10764 if (cursor_glyph == NULL)
10765 goto mark_cursor_off;
dbc4e1c1 10766
06a2c219
GM
10767 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10768
10769 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10770 x,
045dee35 10771 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219
GM
10772 cursor_row->y)),
10773 cursor_glyph->pixel_width,
10774 cursor_row->visible_height,
10775 False);
dbc4e1c1 10776 }
06a2c219
GM
10777
10778 /* Erase the cursor by redrawing the character underneath it. */
10779 if (mouse_face_here_p)
10780 hl = DRAW_MOUSE_FACE;
10781 else if (cursor_row->inverse_p)
10782 hl = DRAW_INVERSE_VIDEO;
10783 else
10784 hl = DRAW_NORMAL_TEXT;
10785 x_draw_phys_cursor_glyph (w, cursor_row, hl);
dbc4e1c1 10786
06a2c219
GM
10787 mark_cursor_off:
10788 w->phys_cursor_on_p = 0;
10789 w->phys_cursor_type = NO_CURSOR;
dbc4e1c1
JB
10790}
10791
10792
06a2c219
GM
10793/* Display or clear cursor of window W. If ON is zero, clear the
10794 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10795 where to put the cursor is specified by HPOS, VPOS, X and Y. */
dbc4e1c1 10796
06a2c219
GM
10797void
10798x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10799 struct window *w;
10800 int on, hpos, vpos, x, y;
dbc4e1c1 10801{
06a2c219
GM
10802 struct frame *f = XFRAME (w->frame);
10803 int new_cursor_type;
f02d8aa0 10804 int new_cursor_width;
06a2c219
GM
10805 struct glyph_matrix *current_glyphs;
10806 struct glyph_row *glyph_row;
10807 struct glyph *glyph;
dbc4e1c1 10808
49d838ea 10809 /* This is pointless on invisible frames, and dangerous on garbaged
06a2c219
GM
10810 windows and frames; in the latter case, the frame or window may
10811 be in the midst of changing its size, and x and y may be off the
10812 window. */
10813 if (! FRAME_VISIBLE_P (f)
10814 || FRAME_GARBAGED_P (f)
10815 || vpos >= w->current_matrix->nrows
10816 || hpos >= w->current_matrix->matrix_w)
dc6f92b8
JB
10817 return;
10818
10819 /* If cursor is off and we want it off, return quickly. */
06a2c219 10820 if (!on && !w->phys_cursor_on_p)
dc6f92b8
JB
10821 return;
10822
06a2c219
GM
10823 current_glyphs = w->current_matrix;
10824 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10825 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10826
10827 /* If cursor row is not enabled, we don't really know where to
10828 display the cursor. */
10829 if (!glyph_row->enabled_p)
10830 {
10831 w->phys_cursor_on_p = 0;
10832 return;
10833 }
10834
10835 xassert (interrupt_input_blocked);
10836
10837 /* Set new_cursor_type to the cursor we want to be displayed. In a
10838 mini-buffer window, we want the cursor only to appear if we are
10839 reading input from this window. For the selected window, we want
10840 the cursor type given by the frame parameter. If explicitly
10841 marked off, draw no cursor. In all other cases, we want a hollow
10842 box cursor. */
f02d8aa0 10843 new_cursor_width = -1;
9b4a7047
GM
10844 if (cursor_in_echo_area
10845 && FRAME_HAS_MINIBUF_P (f)
10846 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
06a2c219 10847 {
9b4a7047
GM
10848 if (w == XWINDOW (echo_area_window))
10849 new_cursor_type = FRAME_DESIRED_CURSOR (f);
06a2c219
GM
10850 else
10851 new_cursor_type = HOLLOW_BOX_CURSOR;
10852 }
06a2c219 10853 else
9b4a7047
GM
10854 {
10855 if (w != XWINDOW (selected_window)
10856 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
10857 {
e55a0b79
GM
10858 extern int cursor_in_non_selected_windows;
10859
5cefa566
GM
10860 if (MINI_WINDOW_P (w)
10861 || !cursor_in_non_selected_windows
10862 || NILP (XBUFFER (w->buffer)->cursor_type))
9b4a7047
GM
10863 new_cursor_type = NO_CURSOR;
10864 else
10865 new_cursor_type = HOLLOW_BOX_CURSOR;
10866 }
10867 else if (w->cursor_off_p)
10868 new_cursor_type = NO_CURSOR;
10869 else
f02d8aa0
GM
10870 {
10871 struct buffer *b = XBUFFER (w->buffer);
10872
10873 if (EQ (b->cursor_type, Qt))
10874 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10875 else
10876 new_cursor_type = x_specified_cursor_type (b->cursor_type,
10877 &new_cursor_width);
10878 }
9b4a7047 10879 }
06a2c219
GM
10880
10881 /* If cursor is currently being shown and we don't want it to be or
10882 it is in the wrong place, or the cursor type is not what we want,
dc6f92b8 10883 erase it. */
06a2c219 10884 if (w->phys_cursor_on_p
dc6f92b8 10885 && (!on
06a2c219
GM
10886 || w->phys_cursor.x != x
10887 || w->phys_cursor.y != y
10888 || new_cursor_type != w->phys_cursor_type))
10889 x_erase_phys_cursor (w);
10890
10891 /* If the cursor is now invisible and we want it to be visible,
10892 display it. */
10893 if (on && !w->phys_cursor_on_p)
10894 {
10895 w->phys_cursor_ascent = glyph_row->ascent;
10896 w->phys_cursor_height = glyph_row->height;
10897
10898 /* Set phys_cursor_.* before x_draw_.* is called because some
10899 of them may need the information. */
10900 w->phys_cursor.x = x;
10901 w->phys_cursor.y = glyph_row->y;
10902 w->phys_cursor.hpos = hpos;
10903 w->phys_cursor.vpos = vpos;
10904 w->phys_cursor_type = new_cursor_type;
10905 w->phys_cursor_on_p = 1;
10906
10907 switch (new_cursor_type)
dc6f92b8 10908 {
06a2c219
GM
10909 case HOLLOW_BOX_CURSOR:
10910 x_draw_hollow_cursor (w, glyph_row);
10911 break;
10912
10913 case FILLED_BOX_CURSOR:
10914 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10915 break;
10916
10917 case BAR_CURSOR:
f02d8aa0 10918 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
06a2c219
GM
10919 break;
10920
10921 case NO_CURSOR:
10922 break;
dc6f92b8 10923
06a2c219
GM
10924 default:
10925 abort ();
10926 }
59ddecde
GM
10927
10928#ifdef HAVE_X_I18N
10929 if (w == XWINDOW (f->selected_window))
10930 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
10931 xic_set_preeditarea (w, x, y);
10932#endif
dc6f92b8
JB
10933 }
10934
06a2c219 10935#ifndef XFlush
f676886a 10936 if (updating_frame != f)
334208b7 10937 XFlush (FRAME_X_DISPLAY (f));
06a2c219 10938#endif
dc6f92b8
JB
10939}
10940
06a2c219
GM
10941
10942/* Display the cursor on window W, or clear it. X and Y are window
10943 relative pixel coordinates. HPOS and VPOS are glyph matrix
10944 positions. If W is not the selected window, display a hollow
10945 cursor. ON non-zero means display the cursor at X, Y which
10946 correspond to HPOS, VPOS, otherwise it is cleared. */
5d46f928 10947
dfcf069d 10948void
06a2c219
GM
10949x_display_cursor (w, on, hpos, vpos, x, y)
10950 struct window *w;
10951 int on, hpos, vpos, x, y;
dc6f92b8 10952{
f94397b5 10953 BLOCK_INPUT;
06a2c219 10954 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
5d46f928
RS
10955 UNBLOCK_INPUT;
10956}
10957
06a2c219
GM
10958
10959/* Display the cursor on window W, or clear it, according to ON_P.
5d46f928
RS
10960 Don't change the cursor's position. */
10961
dfcf069d 10962void
06a2c219 10963x_update_cursor (f, on_p)
5d46f928 10964 struct frame *f;
5d46f928 10965{
06a2c219
GM
10966 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
10967}
10968
10969
10970/* Call x_update_window_cursor with parameter ON_P on all leaf windows
10971 in the window tree rooted at W. */
10972
10973static void
10974x_update_cursor_in_window_tree (w, on_p)
10975 struct window *w;
10976 int on_p;
10977{
10978 while (w)
10979 {
10980 if (!NILP (w->hchild))
10981 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10982 else if (!NILP (w->vchild))
10983 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10984 else
10985 x_update_window_cursor (w, on_p);
10986
10987 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10988 }
10989}
5d46f928 10990
f94397b5 10991
06a2c219
GM
10992/* Switch the display of W's cursor on or off, according to the value
10993 of ON. */
10994
10995static void
10996x_update_window_cursor (w, on)
10997 struct window *w;
10998 int on;
10999{
16b5d424
GM
11000 /* Don't update cursor in windows whose frame is in the process
11001 of being deleted. */
11002 if (w->current_matrix)
11003 {
11004 BLOCK_INPUT;
11005 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11006 w->phys_cursor.x, w->phys_cursor.y);
11007 UNBLOCK_INPUT;
11008 }
dc6f92b8 11009}
06a2c219
GM
11010
11011
11012
dc6f92b8
JB
11013\f
11014/* Icons. */
11015
f676886a 11016/* Refresh bitmap kitchen sink icon for frame F
06a2c219 11017 when we get an expose event for it. */
dc6f92b8 11018
dfcf069d 11019void
f676886a
JB
11020refreshicon (f)
11021 struct frame *f;
dc6f92b8 11022{
06a2c219 11023 /* Normally, the window manager handles this function. */
dc6f92b8
JB
11024}
11025
dbc4e1c1 11026/* Make the x-window of frame F use the gnu icon bitmap. */
dc6f92b8
JB
11027
11028int
990ba854 11029x_bitmap_icon (f, file)
f676886a 11030 struct frame *f;
990ba854 11031 Lisp_Object file;
dc6f92b8 11032{
06a2c219 11033 int bitmap_id;
dc6f92b8 11034
c118dd06 11035 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
11036 return 1;
11037
990ba854 11038 /* Free up our existing icon bitmap if any. */
7556890b
RS
11039 if (f->output_data.x->icon_bitmap > 0)
11040 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11041 f->output_data.x->icon_bitmap = 0;
990ba854
RS
11042
11043 if (STRINGP (file))
7f2ae036
RS
11044 bitmap_id = x_create_bitmap_from_file (f, file);
11045 else
11046 {
990ba854 11047 /* Create the GNU bitmap if necessary. */
5bf01b68 11048 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
334208b7
RS
11049 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11050 = x_create_bitmap_from_data (f, gnu_bits,
11051 gnu_width, gnu_height);
990ba854
RS
11052
11053 /* The first time we create the GNU bitmap,
06a2c219 11054 this increments the ref-count one extra time.
990ba854
RS
11055 As a result, the GNU bitmap is never freed.
11056 That way, we don't have to worry about allocating it again. */
334208b7 11057 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
990ba854 11058
334208b7 11059 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7f2ae036
RS
11060 }
11061
11062 x_wm_set_icon_pixmap (f, bitmap_id);
7556890b 11063 f->output_data.x->icon_bitmap = bitmap_id;
dc6f92b8
JB
11064
11065 return 0;
11066}
11067
11068
1be2d067
KH
11069/* Make the x-window of frame F use a rectangle with text.
11070 Use ICON_NAME as the text. */
dc6f92b8
JB
11071
11072int
f676886a
JB
11073x_text_icon (f, icon_name)
11074 struct frame *f;
dc6f92b8
JB
11075 char *icon_name;
11076{
c118dd06 11077 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
11078 return 1;
11079
1be2d067
KH
11080#ifdef HAVE_X11R4
11081 {
11082 XTextProperty text;
11083 text.value = (unsigned char *) icon_name;
11084 text.encoding = XA_STRING;
11085 text.format = 8;
11086 text.nitems = strlen (icon_name);
11087#ifdef USE_X_TOOLKIT
7556890b 11088 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
1be2d067
KH
11089 &text);
11090#else /* not USE_X_TOOLKIT */
11091 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11092#endif /* not USE_X_TOOLKIT */
11093 }
11094#else /* not HAVE_X11R4 */
11095 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11096#endif /* not HAVE_X11R4 */
58769bee 11097
7556890b
RS
11098 if (f->output_data.x->icon_bitmap > 0)
11099 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11100 f->output_data.x->icon_bitmap = 0;
b1c884c3 11101 x_wm_set_icon_pixmap (f, 0);
dc6f92b8
JB
11102
11103 return 0;
11104}
11105\f
e99db5a1
RS
11106#define X_ERROR_MESSAGE_SIZE 200
11107
11108/* If non-nil, this should be a string.
11109 It means catch X errors and store the error message in this string. */
11110
11111static Lisp_Object x_error_message_string;
11112
11113/* An X error handler which stores the error message in
11114 x_error_message_string. This is called from x_error_handler if
11115 x_catch_errors is in effect. */
11116
06a2c219 11117static void
e99db5a1
RS
11118x_error_catcher (display, error)
11119 Display *display;
11120 XErrorEvent *error;
11121{
11122 XGetErrorText (display, error->error_code,
11123 XSTRING (x_error_message_string)->data,
11124 X_ERROR_MESSAGE_SIZE);
11125}
11126
11127/* Begin trapping X errors for display DPY. Actually we trap X errors
11128 for all displays, but DPY should be the display you are actually
11129 operating on.
11130
11131 After calling this function, X protocol errors no longer cause
11132 Emacs to exit; instead, they are recorded in the string
11133 stored in x_error_message_string.
11134
11135 Calling x_check_errors signals an Emacs error if an X error has
11136 occurred since the last call to x_catch_errors or x_check_errors.
11137
11138 Calling x_uncatch_errors resumes the normal error handling. */
11139
11140void x_check_errors ();
11141static Lisp_Object x_catch_errors_unwind ();
11142
11143int
11144x_catch_errors (dpy)
11145 Display *dpy;
11146{
11147 int count = specpdl_ptr - specpdl;
11148
11149 /* Make sure any errors from previous requests have been dealt with. */
11150 XSync (dpy, False);
11151
11152 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
11153
11154 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
11155 XSTRING (x_error_message_string)->data[0] = 0;
11156
11157 return count;
11158}
11159
11160/* Unbind the binding that we made to check for X errors. */
11161
11162static Lisp_Object
11163x_catch_errors_unwind (old_val)
11164 Lisp_Object old_val;
11165{
11166 x_error_message_string = old_val;
11167 return Qnil;
11168}
11169
11170/* If any X protocol errors have arrived since the last call to
11171 x_catch_errors or x_check_errors, signal an Emacs error using
11172 sprintf (a buffer, FORMAT, the x error message text) as the text. */
11173
11174void
11175x_check_errors (dpy, format)
11176 Display *dpy;
11177 char *format;
11178{
11179 /* Make sure to catch any errors incurred so far. */
11180 XSync (dpy, False);
11181
11182 if (XSTRING (x_error_message_string)->data[0])
11183 error (format, XSTRING (x_error_message_string)->data);
11184}
11185
9829ddba
RS
11186/* Nonzero if we had any X protocol errors
11187 since we did x_catch_errors on DPY. */
e99db5a1
RS
11188
11189int
11190x_had_errors_p (dpy)
11191 Display *dpy;
11192{
11193 /* Make sure to catch any errors incurred so far. */
11194 XSync (dpy, False);
11195
11196 return XSTRING (x_error_message_string)->data[0] != 0;
11197}
11198
9829ddba
RS
11199/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
11200
06a2c219 11201void
9829ddba
RS
11202x_clear_errors (dpy)
11203 Display *dpy;
11204{
11205 XSTRING (x_error_message_string)->data[0] = 0;
11206}
11207
e99db5a1
RS
11208/* Stop catching X protocol errors and let them make Emacs die.
11209 DPY should be the display that was passed to x_catch_errors.
11210 COUNT should be the value that was returned by
11211 the corresponding call to x_catch_errors. */
11212
11213void
11214x_uncatch_errors (dpy, count)
11215 Display *dpy;
11216 int count;
11217{
11218 unbind_to (count, Qnil);
11219}
11220
11221#if 0
11222static unsigned int x_wire_count;
11223x_trace_wire ()
11224{
11225 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
11226}
11227#endif /* ! 0 */
11228
11229\f
11230/* Handle SIGPIPE, which can happen when the connection to a server
11231 simply goes away. SIGPIPE is handled by x_connection_signal.
11232 Don't need to do anything, because the write which caused the
11233 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
06a2c219 11234 which will do the appropriate cleanup for us. */
e99db5a1
RS
11235
11236static SIGTYPE
11237x_connection_signal (signalnum) /* If we don't have an argument, */
06a2c219 11238 int signalnum; /* some compilers complain in signal calls. */
e99db5a1
RS
11239{
11240#ifdef USG
11241 /* USG systems forget handlers when they are used;
11242 must reestablish each time */
11243 signal (signalnum, x_connection_signal);
11244#endif /* USG */
11245}
0da1ab50 11246
e99db5a1 11247\f
0da1ab50
GM
11248/************************************************************************
11249 Handling X errors
11250 ************************************************************************/
4746118a 11251
0da1ab50
GM
11252/* Handle the loss of connection to display DPY. ERROR_MESSAGE is
11253 the text of an error message that lead to the connection loss. */
16bd92ea 11254
4746118a 11255static SIGTYPE
5978125e
GM
11256x_connection_closed (dpy, error_message)
11257 Display *dpy;
7a13e894 11258 char *error_message;
4746118a 11259{
5978125e 11260 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7a13e894 11261 Lisp_Object frame, tail;
0da1ab50
GM
11262 int count;
11263 char *msg;
11264
11265 msg = (char *) alloca (strlen (error_message) + 1);
11266 strcpy (msg, error_message);
1a532e54
GM
11267 handling_signal = 0;
11268
0da1ab50
GM
11269 /* Prevent being called recursively because of an error condition
11270 below. Otherwise, we might end up with printing ``can't find per
11271 display information'' in the recursive call instead of printing
11272 the original message here. */
11273 count = x_catch_errors (dpy);
11274
8a4f36cc
GM
11275 /* We have to close the display to inform Xt that it doesn't
11276 exist anymore. If we don't, Xt will continue to wait for
11277 events from the display. As a consequence, a sequence of
11278
11279 M-x make-frame-on-display RET :1 RET
11280 ...kill the new frame, so that we get an IO error...
11281 M-x make-frame-on-display RET :1 RET
11282
11283 will indefinitely wait in Xt for events for display `:1', opened
11284 in the first class to make-frame-on-display.
6186a4a0 11285
8a4f36cc
GM
11286 Closing the display is reported to lead to a bus error on
11287 OpenWindows in certain situations. I suspect that is a bug
11288 in OpenWindows. I don't know how to cicumvent it here. */
11289
f613a4c8 11290#ifdef USE_X_TOOLKIT
0da1ab50 11291 XtCloseDisplay (dpy);
f613a4c8 11292#endif
adabc3a9 11293
8a4f36cc 11294 /* Indicate that this display is dead. */
9e80b57d
KR
11295 if (dpyinfo)
11296 dpyinfo->display = 0;
6186a4a0 11297
06a2c219 11298 /* First delete frames whose mini-buffers are on frames
7a13e894
RS
11299 that are on the dead display. */
11300 FOR_EACH_FRAME (tail, frame)
11301 {
11302 Lisp_Object minibuf_frame;
11303 minibuf_frame
11304 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
f48f33ca
RS
11305 if (FRAME_X_P (XFRAME (frame))
11306 && FRAME_X_P (XFRAME (minibuf_frame))
11307 && ! EQ (frame, minibuf_frame)
11308 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7a13e894
RS
11309 Fdelete_frame (frame, Qt);
11310 }
11311
11312 /* Now delete all remaining frames on the dead display.
06a2c219 11313 We are now sure none of these is used as the mini-buffer
7a13e894
RS
11314 for another frame that we need to delete. */
11315 FOR_EACH_FRAME (tail, frame)
f48f33ca
RS
11316 if (FRAME_X_P (XFRAME (frame))
11317 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
07a7096a
KH
11318 {
11319 /* Set this to t so that Fdelete_frame won't get confused
11320 trying to find a replacement. */
11321 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
11322 Fdelete_frame (frame, Qt);
11323 }
7a13e894 11324
482a1bd2
KH
11325 if (dpyinfo)
11326 x_delete_display (dpyinfo);
7a13e894 11327
0da1ab50
GM
11328 x_uncatch_errors (dpy, count);
11329
7a13e894
RS
11330 if (x_display_list == 0)
11331 {
0da1ab50 11332 fprintf (stderr, "%s\n", msg);
7a13e894
RS
11333 shut_down_emacs (0, 0, Qnil);
11334 exit (70);
11335 }
12ba150f 11336
7a13e894
RS
11337 /* Ordinary stack unwind doesn't deal with these. */
11338#ifdef SIGIO
11339 sigunblock (sigmask (SIGIO));
11340#endif
11341 sigunblock (sigmask (SIGALRM));
11342 TOTALLY_UNBLOCK_INPUT;
11343
aa4d9a9e 11344 clear_waiting_for_input ();
0da1ab50 11345 error ("%s", msg);
4746118a
JB
11346}
11347
0da1ab50 11348
7a13e894
RS
11349/* This is the usual handler for X protocol errors.
11350 It kills all frames on the display that we got the error for.
11351 If that was the only one, it prints an error message and kills Emacs. */
11352
06a2c219 11353static void
c118dd06
JB
11354x_error_quitter (display, error)
11355 Display *display;
11356 XErrorEvent *error;
11357{
7a13e894 11358 char buf[256], buf1[356];
dc6f92b8 11359
58769bee 11360 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 11361 original error handler. */
dc6f92b8 11362
c118dd06 11363 XGetErrorText (display, error->error_code, buf, sizeof (buf));
0a0fdc70 11364 sprintf (buf1, "X protocol error: %s on protocol request %d",
c118dd06 11365 buf, error->request_code);
7a13e894 11366 x_connection_closed (display, buf1);
dc6f92b8
JB
11367}
11368
0da1ab50 11369
e99db5a1
RS
11370/* This is the first-level handler for X protocol errors.
11371 It calls x_error_quitter or x_error_catcher. */
7a13e894 11372
8922af5f 11373static int
e99db5a1 11374x_error_handler (display, error)
8922af5f 11375 Display *display;
e99db5a1 11376 XErrorEvent *error;
8922af5f 11377{
e99db5a1
RS
11378 if (! NILP (x_error_message_string))
11379 x_error_catcher (display, error);
11380 else
11381 x_error_quitter (display, error);
06a2c219 11382 return 0;
f9e24cb9 11383}
c118dd06 11384
e99db5a1
RS
11385/* This is the handler for X IO errors, always.
11386 It kills all frames on the display that we lost touch with.
11387 If that was the only one, it prints an error message and kills Emacs. */
7a13e894 11388
c118dd06 11389static int
e99db5a1 11390x_io_error_quitter (display)
c118dd06 11391 Display *display;
c118dd06 11392{
e99db5a1 11393 char buf[256];
dc6f92b8 11394
e99db5a1
RS
11395 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
11396 x_connection_closed (display, buf);
06a2c219 11397 return 0;
dc6f92b8 11398}
dc6f92b8 11399\f
f451eb13
JB
11400/* Changing the font of the frame. */
11401
76bcdf39
RS
11402/* Give frame F the font named FONTNAME as its default font, and
11403 return the full name of that font. FONTNAME may be a wildcard
11404 pattern; in that case, we choose some font that fits the pattern.
11405 The return value shows which font we chose. */
11406
b5cf7a0e 11407Lisp_Object
f676886a
JB
11408x_new_font (f, fontname)
11409 struct frame *f;
dc6f92b8
JB
11410 register char *fontname;
11411{
dc43ef94 11412 struct font_info *fontp
ee569018 11413 = FS_LOAD_FONT (f, 0, fontname, -1);
dc6f92b8 11414
dc43ef94
KH
11415 if (!fontp)
11416 return Qnil;
2224a5fc 11417
dc43ef94 11418 f->output_data.x->font = (XFontStruct *) (fontp->font);
b4192550 11419 f->output_data.x->baseline_offset = fontp->baseline_offset;
dc43ef94
KH
11420 f->output_data.x->fontset = -1;
11421
b2cad826
KH
11422 /* Compute the scroll bar width in character columns. */
11423 if (f->scroll_bar_pixel_width > 0)
11424 {
7556890b 11425 int wid = FONT_WIDTH (f->output_data.x->font);
b2cad826
KH
11426 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
11427 }
11428 else
4e61bddf
RS
11429 {
11430 int wid = FONT_WIDTH (f->output_data.x->font);
11431 f->scroll_bar_cols = (14 + wid - 1) / wid;
11432 }
b2cad826 11433
f676886a 11434 /* Now make the frame display the given font. */
c118dd06 11435 if (FRAME_X_WINDOW (f) != 0)
dc6f92b8 11436 {
7556890b
RS
11437 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
11438 f->output_data.x->font->fid);
11439 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
11440 f->output_data.x->font->fid);
11441 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
11442 f->output_data.x->font->fid);
f676886a 11443
a27f9f86 11444 frame_update_line_height (f);
0134a210 11445 x_set_window_size (f, 0, f->width, f->height);
dc6f92b8 11446 }
a27f9f86
RS
11447 else
11448 /* If we are setting a new frame's font for the first time,
11449 there are no faces yet, so this font's height is the line height. */
7556890b 11450 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
dc6f92b8 11451
dc43ef94
KH
11452 return build_string (fontp->full_name);
11453}
11454
11455/* Give frame F the fontset named FONTSETNAME as its default font, and
11456 return the full name of that fontset. FONTSETNAME may be a wildcard
b5210ea7
KH
11457 pattern; in that case, we choose some fontset that fits the pattern.
11458 The return value shows which fontset we chose. */
b5cf7a0e 11459
dc43ef94
KH
11460Lisp_Object
11461x_new_fontset (f, fontsetname)
11462 struct frame *f;
11463 char *fontsetname;
11464{
ee569018 11465 int fontset = fs_query_fontset (build_string (fontsetname), 0);
dc43ef94 11466 Lisp_Object result;
b5cf7a0e 11467
dc43ef94
KH
11468 if (fontset < 0)
11469 return Qnil;
b5cf7a0e 11470
2da424f1
KH
11471 if (f->output_data.x->fontset == fontset)
11472 /* This fontset is already set in frame F. There's nothing more
11473 to do. */
ee569018 11474 return fontset_name (fontset);
dc43ef94 11475
ee569018 11476 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
dc43ef94
KH
11477
11478 if (!STRINGP (result))
11479 /* Can't load ASCII font. */
11480 return Qnil;
11481
11482 /* Since x_new_font doesn't update any fontset information, do it now. */
11483 f->output_data.x->fontset = fontset;
dc43ef94 11484
f5d11644
GM
11485#ifdef HAVE_X_I18N
11486 if (FRAME_XIC (f)
11487 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
ee569018 11488 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
f5d11644
GM
11489#endif
11490
dc43ef94 11491 return build_string (fontsetname);
dc6f92b8 11492}
f5d11644
GM
11493
11494\f
11495/***********************************************************************
11496 X Input Methods
11497 ***********************************************************************/
11498
11499#ifdef HAVE_X_I18N
11500
11501#ifdef HAVE_X11R6
11502
11503/* XIM destroy callback function, which is called whenever the
11504 connection to input method XIM dies. CLIENT_DATA contains a
11505 pointer to the x_display_info structure corresponding to XIM. */
11506
11507static void
11508xim_destroy_callback (xim, client_data, call_data)
11509 XIM xim;
11510 XPointer client_data;
11511 XPointer call_data;
11512{
11513 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
11514 Lisp_Object frame, tail;
11515
11516 BLOCK_INPUT;
11517
11518 /* No need to call XDestroyIC.. */
11519 FOR_EACH_FRAME (tail, frame)
11520 {
11521 struct frame *f = XFRAME (frame);
11522 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
11523 {
11524 FRAME_XIC (f) = NULL;
11525 if (FRAME_XIC_FONTSET (f))
11526 {
11527 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
11528 FRAME_XIC_FONTSET (f) = NULL;
11529 }
11530 }
11531 }
11532
11533 /* No need to call XCloseIM. */
11534 dpyinfo->xim = NULL;
11535 XFree (dpyinfo->xim_styles);
11536 UNBLOCK_INPUT;
11537}
11538
11539#endif /* HAVE_X11R6 */
11540
11541/* Open the connection to the XIM server on display DPYINFO.
11542 RESOURCE_NAME is the resource name Emacs uses. */
11543
11544static void
11545xim_open_dpy (dpyinfo, resource_name)
11546 struct x_display_info *dpyinfo;
11547 char *resource_name;
11548{
287f7dd6 11549#ifdef USE_XIM
f5d11644
GM
11550 XIM xim;
11551
11552 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
11553 dpyinfo->xim = xim;
11554
11555 if (xim)
11556 {
f5d11644
GM
11557#ifdef HAVE_X11R6
11558 XIMCallback destroy;
11559#endif
11560
11561 /* Get supported styles and XIM values. */
11562 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
11563
11564#ifdef HAVE_X11R6
11565 destroy.callback = xim_destroy_callback;
11566 destroy.client_data = (XPointer)dpyinfo;
68642df6 11567 /* This isn't prptotyped in OSF 5.0. */
f5d11644
GM
11568 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
11569#endif
11570 }
287f7dd6
GM
11571
11572#else /* not USE_XIM */
11573 dpyinfo->xim = NULL;
11574#endif /* not USE_XIM */
f5d11644
GM
11575}
11576
11577
b9de836c 11578#ifdef HAVE_X11R6_XIM
f5d11644
GM
11579
11580struct xim_inst_t
11581{
11582 struct x_display_info *dpyinfo;
11583 char *resource_name;
11584};
11585
11586/* XIM instantiate callback function, which is called whenever an XIM
11587 server is available. DISPLAY is teh display of the XIM.
11588 CLIENT_DATA contains a pointer to an xim_inst_t structure created
11589 when the callback was registered. */
11590
11591static void
11592xim_instantiate_callback (display, client_data, call_data)
11593 Display *display;
11594 XPointer client_data;
11595 XPointer call_data;
11596{
11597 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
11598 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
11599
11600 /* We don't support multiple XIM connections. */
11601 if (dpyinfo->xim)
11602 return;
11603
11604 xim_open_dpy (dpyinfo, xim_inst->resource_name);
11605
11606 /* Create XIC for the existing frames on the same display, as long
11607 as they have no XIC. */
11608 if (dpyinfo->xim && dpyinfo->reference_count > 0)
11609 {
11610 Lisp_Object tail, frame;
11611
11612 BLOCK_INPUT;
11613 FOR_EACH_FRAME (tail, frame)
11614 {
11615 struct frame *f = XFRAME (frame);
11616
11617 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
11618 if (FRAME_XIC (f) == NULL)
11619 {
11620 create_frame_xic (f);
11621 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
11622 xic_set_statusarea (f);
11623 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
11624 {
11625 struct window *w = XWINDOW (f->selected_window);
11626 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
11627 }
11628 }
11629 }
11630
11631 UNBLOCK_INPUT;
11632 }
11633}
11634
b9de836c 11635#endif /* HAVE_X11R6_XIM */
f5d11644
GM
11636
11637
11638/* Open a connection to the XIM server on display DPYINFO.
11639 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
11640 connection only at the first time. On X11R6, open the connection
11641 in the XIM instantiate callback function. */
11642
11643static void
11644xim_initialize (dpyinfo, resource_name)
11645 struct x_display_info *dpyinfo;
11646 char *resource_name;
11647{
287f7dd6 11648#ifdef USE_XIM
b9de836c 11649#ifdef HAVE_X11R6_XIM
f5d11644
GM
11650 struct xim_inst_t *xim_inst;
11651 int len;
11652
11653 dpyinfo->xim = NULL;
11654 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
11655 xim_inst->dpyinfo = dpyinfo;
11656 len = strlen (resource_name);
11657 xim_inst->resource_name = (char *) xmalloc (len + 1);
11658 bcopy (resource_name, xim_inst->resource_name, len + 1);
11659 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11660 resource_name, EMACS_CLASS,
11661 xim_instantiate_callback,
2ebb2f8b
DL
11662 /* Fixme: This is XPointer in
11663 XFree86 but (XPointer *) on
11664 Tru64, at least. */
11665 (XPointer) xim_inst);
b9de836c 11666#else /* not HAVE_X11R6_XIM */
f5d11644
GM
11667 dpyinfo->xim = NULL;
11668 xim_open_dpy (dpyinfo, resource_name);
b9de836c 11669#endif /* not HAVE_X11R6_XIM */
287f7dd6
GM
11670
11671#else /* not USE_XIM */
11672 dpyinfo->xim = NULL;
11673#endif /* not USE_XIM */
f5d11644
GM
11674}
11675
11676
11677/* Close the connection to the XIM server on display DPYINFO. */
11678
11679static void
11680xim_close_dpy (dpyinfo)
11681 struct x_display_info *dpyinfo;
11682{
287f7dd6 11683#ifdef USE_XIM
b9de836c 11684#ifdef HAVE_X11R6_XIM
8a4f36cc
GM
11685 if (dpyinfo->display)
11686 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11687 NULL, EMACS_CLASS,
11688 xim_instantiate_callback, NULL);
b9de836c 11689#endif /* not HAVE_X11R6_XIM */
8a4f36cc
GM
11690 if (dpyinfo->display)
11691 XCloseIM (dpyinfo->xim);
f5d11644
GM
11692 dpyinfo->xim = NULL;
11693 XFree (dpyinfo->xim_styles);
287f7dd6 11694#endif /* USE_XIM */
f5d11644
GM
11695}
11696
b9de836c 11697#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
11698
11699
dc6f92b8 11700\f
2e365682
RS
11701/* Calculate the absolute position in frame F
11702 from its current recorded position values and gravity. */
11703
dfcf069d 11704void
43bca5d5 11705x_calc_absolute_position (f)
f676886a 11706 struct frame *f;
dc6f92b8 11707{
06a2c219 11708 Window child;
6dba1858 11709 int win_x = 0, win_y = 0;
7556890b 11710 int flags = f->output_data.x->size_hint_flags;
c81412a0
KH
11711 int this_window;
11712
9829ddba
RS
11713 /* We have nothing to do if the current position
11714 is already for the top-left corner. */
11715 if (! ((flags & XNegative) || (flags & YNegative)))
11716 return;
11717
c81412a0 11718#ifdef USE_X_TOOLKIT
7556890b 11719 this_window = XtWindow (f->output_data.x->widget);
c81412a0
KH
11720#else
11721 this_window = FRAME_X_WINDOW (f);
11722#endif
6dba1858
RS
11723
11724 /* Find the position of the outside upper-left corner of
9829ddba
RS
11725 the inner window, with respect to the outer window.
11726 But do this only if we will need the results. */
7556890b 11727 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6dba1858 11728 {
9829ddba
RS
11729 int count;
11730
6dba1858 11731 BLOCK_INPUT;
9829ddba
RS
11732 count = x_catch_errors (FRAME_X_DISPLAY (f));
11733 while (1)
11734 {
11735 x_clear_errors (FRAME_X_DISPLAY (f));
11736 XTranslateCoordinates (FRAME_X_DISPLAY (f),
11737
11738 /* From-window, to-window. */
11739 this_window,
11740 f->output_data.x->parent_desc,
11741
11742 /* From-position, to-position. */
11743 0, 0, &win_x, &win_y,
11744
11745 /* Child of win. */
11746 &child);
11747 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
11748 {
11749 Window newroot, newparent = 0xdeadbeef;
11750 Window *newchildren;
2ebb2f8b 11751 unsigned int nchildren;
9829ddba
RS
11752
11753 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
11754 &newparent, &newchildren, &nchildren))
11755 break;
58769bee 11756
7c3c78a3 11757 XFree ((char *) newchildren);
6dba1858 11758
9829ddba
RS
11759 f->output_data.x->parent_desc = newparent;
11760 }
11761 else
11762 break;
11763 }
6dba1858 11764
9829ddba 11765 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
6dba1858
RS
11766 UNBLOCK_INPUT;
11767 }
11768
11769 /* Treat negative positions as relative to the leftmost bottommost
11770 position that fits on the screen. */
20f55f9a 11771 if (flags & XNegative)
7556890b 11772 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
2e365682
RS
11773 - 2 * f->output_data.x->border_width - win_x
11774 - PIXEL_WIDTH (f)
11775 + f->output_data.x->left_pos);
dc6f92b8 11776
7708ced0
GM
11777 {
11778 int height = PIXEL_HEIGHT (f);
06a2c219 11779
7708ced0
GM
11780#if defined USE_X_TOOLKIT && defined USE_MOTIF
11781 /* Something is fishy here. When using Motif, starting Emacs with
11782 `-g -0-0', the frame appears too low by a few pixels.
11783
11784 This seems to be so because initially, while Emacs is starting,
11785 the column widget's height and the frame's pixel height are
11786 different. The column widget's height is the right one. In
11787 later invocations, when Emacs is up, the frame's pixel height
11788 is right, though.
11789
11790 It's not obvious where the initial small difference comes from.
11791 2000-12-01, gerd. */
11792
11793 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
06a2c219 11794#endif
2e365682 11795
7708ced0
GM
11796 if (flags & YNegative)
11797 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
11798 - 2 * f->output_data.x->border_width
11799 - win_y
11800 - height
11801 + f->output_data.x->top_pos);
11802 }
11803
3a35ab44
RS
11804 /* The left_pos and top_pos
11805 are now relative to the top and left screen edges,
11806 so the flags should correspond. */
7556890b 11807 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
dc6f92b8
JB
11808}
11809
3a35ab44
RS
11810/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
11811 to really change the position, and 0 when calling from
11812 x_make_frame_visible (in that case, XOFF and YOFF are the current
aa3ff7c9
KH
11813 position values). It is -1 when calling from x_set_frame_parameters,
11814 which means, do adjust for borders but don't change the gravity. */
3a35ab44 11815
dfcf069d 11816void
dc05a16b 11817x_set_offset (f, xoff, yoff, change_gravity)
f676886a 11818 struct frame *f;
dc6f92b8 11819 register int xoff, yoff;
dc05a16b 11820 int change_gravity;
dc6f92b8 11821{
4a4cbdd5
KH
11822 int modified_top, modified_left;
11823
aa3ff7c9 11824 if (change_gravity > 0)
3a35ab44 11825 {
7556890b
RS
11826 f->output_data.x->top_pos = yoff;
11827 f->output_data.x->left_pos = xoff;
11828 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
3a35ab44 11829 if (xoff < 0)
7556890b 11830 f->output_data.x->size_hint_flags |= XNegative;
3a35ab44 11831 if (yoff < 0)
7556890b
RS
11832 f->output_data.x->size_hint_flags |= YNegative;
11833 f->output_data.x->win_gravity = NorthWestGravity;
3a35ab44 11834 }
43bca5d5 11835 x_calc_absolute_position (f);
dc6f92b8
JB
11836
11837 BLOCK_INPUT;
c32cdd9a 11838 x_wm_set_size_hint (f, (long) 0, 0);
3a35ab44 11839
7556890b
RS
11840 modified_left = f->output_data.x->left_pos;
11841 modified_top = f->output_data.x->top_pos;
e73ec6fa
RS
11842#if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
11843 this seems to be unnecessary and incorrect. rms, 4/17/97. */
11844 /* It is a mystery why we need to add the border_width here
11845 when the frame is already visible, but experiment says we do. */
aa3ff7c9 11846 if (change_gravity != 0)
4a4cbdd5 11847 {
7556890b
RS
11848 modified_left += f->output_data.x->border_width;
11849 modified_top += f->output_data.x->border_width;
4a4cbdd5 11850 }
e73ec6fa 11851#endif
4a4cbdd5 11852
3afe33e7 11853#ifdef USE_X_TOOLKIT
7556890b 11854 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
4a4cbdd5 11855 modified_left, modified_top);
3afe33e7 11856#else /* not USE_X_TOOLKIT */
334208b7 11857 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4a4cbdd5 11858 modified_left, modified_top);
3afe33e7 11859#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11860 UNBLOCK_INPUT;
11861}
11862
bc20ebbf
FP
11863/* Call this to change the size of frame F's x-window.
11864 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
11865 for this size change and subsequent size changes.
11866 Otherwise we leave the window gravity unchanged. */
dc6f92b8 11867
dfcf069d 11868void
bc20ebbf 11869x_set_window_size (f, change_gravity, cols, rows)
f676886a 11870 struct frame *f;
bc20ebbf 11871 int change_gravity;
b1c884c3 11872 int cols, rows;
dc6f92b8 11873{
06a2c219 11874#ifndef USE_X_TOOLKIT
dc6f92b8 11875 int pixelwidth, pixelheight;
06a2c219 11876#endif
dc6f92b8 11877
80fd1fe2 11878 BLOCK_INPUT;
aee9a898
RS
11879
11880#ifdef USE_X_TOOLKIT
3a20653d
RS
11881 {
11882 /* The x and y position of the widget is clobbered by the
11883 call to XtSetValues within EmacsFrameSetCharSize.
11884 This is a real kludge, but I don't understand Xt so I can't
11885 figure out a correct fix. Can anyone else tell me? -- rms. */
7556890b
RS
11886 int xpos = f->output_data.x->widget->core.x;
11887 int ypos = f->output_data.x->widget->core.y;
11888 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
11889 f->output_data.x->widget->core.x = xpos;
11890 f->output_data.x->widget->core.y = ypos;
3a20653d 11891 }
80fd1fe2
FP
11892
11893#else /* not USE_X_TOOLKIT */
11894
b1c884c3 11895 check_frame_size (f, &rows, &cols);
7556890b 11896 f->output_data.x->vertical_scroll_bar_extra
b2cad826
KH
11897 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
11898 ? 0
11899 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
480407eb 11900 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7556890b 11901 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
06a2c219 11902 f->output_data.x->flags_areas_extra
110859fc 11903 = FRAME_FLAGS_AREA_WIDTH (f);
f451eb13
JB
11904 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
11905 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
dc6f92b8 11906
7556890b 11907 f->output_data.x->win_gravity = NorthWestGravity;
c32cdd9a 11908 x_wm_set_size_hint (f, (long) 0, 0);
6ccf47d1 11909
334208b7
RS
11910 XSync (FRAME_X_DISPLAY (f), False);
11911 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11912 pixelwidth, pixelheight);
b1c884c3
JB
11913
11914 /* Now, strictly speaking, we can't be sure that this is accurate,
11915 but the window manager will get around to dealing with the size
11916 change request eventually, and we'll hear how it went when the
dbc4e1c1
JB
11917 ConfigureNotify event gets here.
11918
11919 We could just not bother storing any of this information here,
11920 and let the ConfigureNotify event set everything up, but that
fddd5ceb 11921 might be kind of confusing to the Lisp code, since size changes
dbc4e1c1 11922 wouldn't be reported in the frame parameters until some random
fddd5ceb
RS
11923 point in the future when the ConfigureNotify event arrives.
11924
11925 We pass 1 for DELAY since we can't run Lisp code inside of
11926 a BLOCK_INPUT. */
7d1e984f 11927 change_frame_size (f, rows, cols, 0, 1, 0);
b1c884c3
JB
11928 PIXEL_WIDTH (f) = pixelwidth;
11929 PIXEL_HEIGHT (f) = pixelheight;
11930
aee9a898
RS
11931 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11932 receive in the ConfigureNotify event; if we get what we asked
11933 for, then the event won't cause the screen to become garbaged, so
11934 we have to make sure to do it here. */
11935 SET_FRAME_GARBAGED (f);
11936
11937 XFlush (FRAME_X_DISPLAY (f));
11938
11939#endif /* not USE_X_TOOLKIT */
11940
4d73d038 11941 /* If cursor was outside the new size, mark it as off. */
06a2c219 11942 mark_window_cursors_off (XWINDOW (f->root_window));
4d73d038 11943
aee9a898
RS
11944 /* Clear out any recollection of where the mouse highlighting was,
11945 since it might be in a place that's outside the new frame size.
11946 Actually checking whether it is outside is a pain in the neck,
11947 so don't try--just let the highlighting be done afresh with new size. */
e687d06e 11948 cancel_mouse_face (f);
dbc4e1c1 11949
dc6f92b8
JB
11950 UNBLOCK_INPUT;
11951}
dc6f92b8 11952\f
d047c4eb 11953/* Mouse warping. */
dc6f92b8 11954
9b378208 11955void
f676886a
JB
11956x_set_mouse_position (f, x, y)
11957 struct frame *f;
dc6f92b8
JB
11958 int x, y;
11959{
11960 int pix_x, pix_y;
11961
7556890b
RS
11962 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
11963 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
f451eb13
JB
11964
11965 if (pix_x < 0) pix_x = 0;
11966 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
11967
11968 if (pix_y < 0) pix_y = 0;
11969 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
dc6f92b8
JB
11970
11971 BLOCK_INPUT;
dc6f92b8 11972
334208b7
RS
11973 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11974 0, 0, 0, 0, pix_x, pix_y);
dc6f92b8
JB
11975 UNBLOCK_INPUT;
11976}
11977
9b378208
RS
11978/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11979
11980void
11981x_set_mouse_pixel_position (f, pix_x, pix_y)
11982 struct frame *f;
11983 int pix_x, pix_y;
11984{
11985 BLOCK_INPUT;
11986
334208b7
RS
11987 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11988 0, 0, 0, 0, pix_x, pix_y);
9b378208
RS
11989 UNBLOCK_INPUT;
11990}
d047c4eb
KH
11991\f
11992/* focus shifting, raising and lowering. */
9b378208 11993
dfcf069d 11994void
f676886a
JB
11995x_focus_on_frame (f)
11996 struct frame *f;
dc6f92b8 11997{
1fb20991 11998#if 0 /* This proves to be unpleasant. */
f676886a 11999 x_raise_frame (f);
1fb20991 12000#endif
6d4238f3
JB
12001#if 0
12002 /* I don't think that the ICCCM allows programs to do things like this
12003 without the interaction of the window manager. Whatever you end up
f676886a 12004 doing with this code, do it to x_unfocus_frame too. */
334208b7 12005 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dc6f92b8 12006 RevertToPointerRoot, CurrentTime);
c118dd06 12007#endif /* ! 0 */
dc6f92b8
JB
12008}
12009
dfcf069d 12010void
f676886a
JB
12011x_unfocus_frame (f)
12012 struct frame *f;
dc6f92b8 12013{
6d4238f3 12014#if 0
f676886a 12015 /* Look at the remarks in x_focus_on_frame. */
0f941935 12016 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
334208b7 12017 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
dc6f92b8 12018 RevertToPointerRoot, CurrentTime);
c118dd06 12019#endif /* ! 0 */
dc6f92b8
JB
12020}
12021
f676886a 12022/* Raise frame F. */
dc6f92b8 12023
dfcf069d 12024void
f676886a
JB
12025x_raise_frame (f)
12026 struct frame *f;
dc6f92b8 12027{
3a88c238 12028 if (f->async_visible)
dc6f92b8
JB
12029 {
12030 BLOCK_INPUT;
3afe33e7 12031#ifdef USE_X_TOOLKIT
7556890b 12032 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 12033#else /* not USE_X_TOOLKIT */
334208b7 12034 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 12035#endif /* not USE_X_TOOLKIT */
334208b7 12036 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
12037 UNBLOCK_INPUT;
12038 }
12039}
12040
f676886a 12041/* Lower frame F. */
dc6f92b8 12042
dfcf069d 12043void
f676886a
JB
12044x_lower_frame (f)
12045 struct frame *f;
dc6f92b8 12046{
3a88c238 12047 if (f->async_visible)
dc6f92b8
JB
12048 {
12049 BLOCK_INPUT;
3afe33e7 12050#ifdef USE_X_TOOLKIT
7556890b 12051 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 12052#else /* not USE_X_TOOLKIT */
334208b7 12053 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 12054#endif /* not USE_X_TOOLKIT */
334208b7 12055 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
12056 UNBLOCK_INPUT;
12057 }
12058}
12059
dbc4e1c1 12060static void
6b0442dc 12061XTframe_raise_lower (f, raise_flag)
dbc4e1c1 12062 FRAME_PTR f;
6b0442dc 12063 int raise_flag;
dbc4e1c1 12064{
6b0442dc 12065 if (raise_flag)
dbc4e1c1
JB
12066 x_raise_frame (f);
12067 else
12068 x_lower_frame (f);
12069}
d047c4eb
KH
12070\f
12071/* Change of visibility. */
dc6f92b8 12072
9382638d
KH
12073/* This tries to wait until the frame is really visible.
12074 However, if the window manager asks the user where to position
12075 the frame, this will return before the user finishes doing that.
12076 The frame will not actually be visible at that time,
12077 but it will become visible later when the window manager
12078 finishes with it. */
12079
dfcf069d 12080void
f676886a
JB
12081x_make_frame_visible (f)
12082 struct frame *f;
dc6f92b8 12083{
990ba854 12084 Lisp_Object type;
1aa6072f 12085 int original_top, original_left;
31be9251
GM
12086 int retry_count = 2;
12087
12088 retry:
dc6f92b8 12089
dc6f92b8 12090 BLOCK_INPUT;
dc6f92b8 12091
990ba854
RS
12092 type = x_icon_type (f);
12093 if (!NILP (type))
12094 x_bitmap_icon (f, type);
bdcd49ba 12095
f676886a 12096 if (! FRAME_VISIBLE_P (f))
90e65f07 12097 {
1aa6072f
RS
12098 /* We test FRAME_GARBAGED_P here to make sure we don't
12099 call x_set_offset a second time
12100 if we get to x_make_frame_visible a second time
12101 before the window gets really visible. */
12102 if (! FRAME_ICONIFIED_P (f)
12103 && ! f->output_data.x->asked_for_visible)
12104 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12105
12106 f->output_data.x->asked_for_visible = 1;
12107
90e65f07 12108 if (! EQ (Vx_no_window_manager, Qt))
f676886a 12109 x_wm_set_window_state (f, NormalState);
3afe33e7 12110#ifdef USE_X_TOOLKIT
d7a38a2e 12111 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 12112 XtMapWidget (f->output_data.x->widget);
3afe33e7 12113#else /* not USE_X_TOOLKIT */
7f9c7f94 12114 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 12115#endif /* not USE_X_TOOLKIT */
0134a210
RS
12116#if 0 /* This seems to bring back scroll bars in the wrong places
12117 if the window configuration has changed. They seem
12118 to come back ok without this. */
ab648270 12119 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
334208b7 12120 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
0134a210 12121#endif
90e65f07 12122 }
dc6f92b8 12123
334208b7 12124 XFlush (FRAME_X_DISPLAY (f));
90e65f07 12125
0dacf791
RS
12126 /* Synchronize to ensure Emacs knows the frame is visible
12127 before we do anything else. We do this loop with input not blocked
12128 so that incoming events are handled. */
12129 {
12130 Lisp_Object frame;
12ce2351 12131 int count;
28c01ffe
RS
12132 /* This must be before UNBLOCK_INPUT
12133 since events that arrive in response to the actions above
12134 will set it when they are handled. */
12135 int previously_visible = f->output_data.x->has_been_visible;
1aa6072f
RS
12136
12137 original_left = f->output_data.x->left_pos;
12138 original_top = f->output_data.x->top_pos;
c0a04927
RS
12139
12140 /* This must come after we set COUNT. */
12141 UNBLOCK_INPUT;
12142
2745e6c4 12143 /* We unblock here so that arriving X events are processed. */
1aa6072f 12144
dcb07ae9
RS
12145 /* Now move the window back to where it was "supposed to be".
12146 But don't do it if the gravity is negative.
12147 When the gravity is negative, this uses a position
28c01ffe
RS
12148 that is 3 pixels too low. Perhaps that's really the border width.
12149
12150 Don't do this if the window has never been visible before,
12151 because the window manager may choose the position
12152 and we don't want to override it. */
1aa6072f 12153
4d3f5d9a 12154 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
06c488fd 12155 && f->output_data.x->win_gravity == NorthWestGravity
28c01ffe 12156 && previously_visible)
1aa6072f 12157 {
2745e6c4
RS
12158 Drawable rootw;
12159 int x, y;
12160 unsigned int width, height, border, depth;
06a2c219 12161
1aa6072f 12162 BLOCK_INPUT;
9829ddba 12163
06a2c219
GM
12164 /* On some window managers (such as FVWM) moving an existing
12165 window, even to the same place, causes the window manager
12166 to introduce an offset. This can cause the window to move
12167 to an unexpected location. Check the geometry (a little
12168 slow here) and then verify that the window is in the right
12169 place. If the window is not in the right place, move it
12170 there, and take the potential window manager hit. */
2745e6c4
RS
12171 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12172 &rootw, &x, &y, &width, &height, &border, &depth);
12173
12174 if (original_left != x || original_top != y)
12175 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12176 original_left, original_top);
12177
1aa6072f
RS
12178 UNBLOCK_INPUT;
12179 }
9829ddba 12180
e0c1aef2 12181 XSETFRAME (frame, f);
c0a04927 12182
12ce2351
GM
12183 /* Wait until the frame is visible. Process X events until a
12184 MapNotify event has been seen, or until we think we won't get a
12185 MapNotify at all.. */
12186 for (count = input_signal_count + 10;
12187 input_signal_count < count && !FRAME_VISIBLE_P (f);)
2a6cf806 12188 {
12ce2351 12189 /* Force processing of queued events. */
334208b7 12190 x_sync (f);
12ce2351
GM
12191
12192 /* Machines that do polling rather than SIGIO have been
12193 observed to go into a busy-wait here. So we'll fake an
12194 alarm signal to let the handler know that there's something
12195 to be read. We used to raise a real alarm, but it seems
12196 that the handler isn't always enabled here. This is
12197 probably a bug. */
8b2f8d4e 12198 if (input_polling_used ())
3b2fa4e6 12199 {
12ce2351
GM
12200 /* It could be confusing if a real alarm arrives while
12201 processing the fake one. Turn it off and let the
12202 handler reset it. */
3e71d8f2 12203 extern void poll_for_input_1 P_ ((void));
bffcfca9
GM
12204 int old_poll_suppress_count = poll_suppress_count;
12205 poll_suppress_count = 1;
12206 poll_for_input_1 ();
12207 poll_suppress_count = old_poll_suppress_count;
3b2fa4e6 12208 }
12ce2351
GM
12209
12210 /* See if a MapNotify event has been processed. */
12211 FRAME_SAMPLE_VISIBILITY (f);
2a6cf806 12212 }
31be9251
GM
12213
12214 /* 2000-09-28: In
12215
12216 (let ((f (selected-frame)))
12217 (iconify-frame f)
12218 (raise-frame f))
12219
12220 the frame is not raised with various window managers on
12221 FreeBSD, Linux and Solaris. It turns out that, for some
12222 unknown reason, the call to XtMapWidget is completely ignored.
12223 Mapping the widget a second time works. */
12224
12225 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
12226 goto retry;
0dacf791 12227 }
dc6f92b8
JB
12228}
12229
06a2c219 12230/* Change from mapped state to withdrawn state. */
dc6f92b8 12231
d047c4eb
KH
12232/* Make the frame visible (mapped and not iconified). */
12233
dfcf069d 12234void
f676886a
JB
12235x_make_frame_invisible (f)
12236 struct frame *f;
dc6f92b8 12237{
546e6d5b
RS
12238 Window window;
12239
12240#ifdef USE_X_TOOLKIT
12241 /* Use the frame's outermost window, not the one we normally draw on. */
7556890b 12242 window = XtWindow (f->output_data.x->widget);
546e6d5b
RS
12243#else /* not USE_X_TOOLKIT */
12244 window = FRAME_X_WINDOW (f);
12245#endif /* not USE_X_TOOLKIT */
dc6f92b8 12246
9319ae23 12247 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
12248 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12249 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 12250
5627c40e 12251#if 0/* This might add unreliability; I don't trust it -- rms. */
9319ae23 12252 if (! f->async_visible && ! f->async_iconified)
dc6f92b8 12253 return;
5627c40e 12254#endif
dc6f92b8
JB
12255
12256 BLOCK_INPUT;
c118dd06 12257
af31d76f
RS
12258 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
12259 that the current position of the window is user-specified, rather than
12260 program-specified, so that when the window is mapped again, it will be
12261 placed at the same location, without forcing the user to position it
12262 by hand again (they have already done that once for this window.) */
c32cdd9a 12263 x_wm_set_size_hint (f, (long) 0, 1);
af31d76f 12264
c118dd06
JB
12265#ifdef HAVE_X11R4
12266
334208b7
RS
12267 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
12268 DefaultScreen (FRAME_X_DISPLAY (f))))
c118dd06
JB
12269 {
12270 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 12271 error ("Can't notify window manager of window withdrawal");
c118dd06 12272 }
c118dd06 12273#else /* ! defined (HAVE_X11R4) */
16bd92ea 12274
c118dd06 12275 /* Tell the window manager what we're going to do. */
dc6f92b8
JB
12276 if (! EQ (Vx_no_window_manager, Qt))
12277 {
16bd92ea 12278 XEvent unmap;
dc6f92b8 12279
16bd92ea 12280 unmap.xunmap.type = UnmapNotify;
546e6d5b 12281 unmap.xunmap.window = window;
334208b7 12282 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
16bd92ea 12283 unmap.xunmap.from_configure = False;
334208b7
RS
12284 if (! XSendEvent (FRAME_X_DISPLAY (f),
12285 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea 12286 False,
06a2c219 12287 SubstructureRedirectMaskSubstructureNotifyMask,
16bd92ea
JB
12288 &unmap))
12289 {
12290 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 12291 error ("Can't notify window manager of withdrawal");
16bd92ea 12292 }
dc6f92b8
JB
12293 }
12294
16bd92ea 12295 /* Unmap the window ourselves. Cheeky! */
334208b7 12296 XUnmapWindow (FRAME_X_DISPLAY (f), window);
c118dd06 12297#endif /* ! defined (HAVE_X11R4) */
dc6f92b8 12298
5627c40e
RS
12299 /* We can't distinguish this from iconification
12300 just by the event that we get from the server.
12301 So we can't win using the usual strategy of letting
12302 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
12303 and synchronize with the server to make sure we agree. */
12304 f->visible = 0;
12305 FRAME_ICONIFIED_P (f) = 0;
12306 f->async_visible = 0;
12307 f->async_iconified = 0;
12308
334208b7 12309 x_sync (f);
5627c40e 12310
dc6f92b8
JB
12311 UNBLOCK_INPUT;
12312}
12313
06a2c219 12314/* Change window state from mapped to iconified. */
dc6f92b8 12315
dfcf069d 12316void
f676886a
JB
12317x_iconify_frame (f)
12318 struct frame *f;
dc6f92b8 12319{
3afe33e7 12320 int result;
990ba854 12321 Lisp_Object type;
dc6f92b8 12322
9319ae23 12323 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
12324 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12325 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 12326
3a88c238 12327 if (f->async_iconified)
dc6f92b8
JB
12328 return;
12329
3afe33e7 12330 BLOCK_INPUT;
546e6d5b 12331
9af3143a
RS
12332 FRAME_SAMPLE_VISIBILITY (f);
12333
990ba854
RS
12334 type = x_icon_type (f);
12335 if (!NILP (type))
12336 x_bitmap_icon (f, type);
bdcd49ba
RS
12337
12338#ifdef USE_X_TOOLKIT
12339
546e6d5b
RS
12340 if (! FRAME_VISIBLE_P (f))
12341 {
12342 if (! EQ (Vx_no_window_manager, Qt))
12343 x_wm_set_window_state (f, IconicState);
12344 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 12345 XtMapWidget (f->output_data.x->widget);
9cf30a30
RS
12346 /* The server won't give us any event to indicate
12347 that an invisible frame was changed to an icon,
12348 so we have to record it here. */
12349 f->iconified = 1;
1e6bc770 12350 f->visible = 1;
9cf30a30 12351 f->async_iconified = 1;
1e6bc770 12352 f->async_visible = 0;
546e6d5b
RS
12353 UNBLOCK_INPUT;
12354 return;
12355 }
12356
334208b7 12357 result = XIconifyWindow (FRAME_X_DISPLAY (f),
7556890b 12358 XtWindow (f->output_data.x->widget),
334208b7 12359 DefaultScreen (FRAME_X_DISPLAY (f)));
3afe33e7
RS
12360 UNBLOCK_INPUT;
12361
12362 if (!result)
546e6d5b 12363 error ("Can't notify window manager of iconification");
3afe33e7
RS
12364
12365 f->async_iconified = 1;
1e6bc770
RS
12366 f->async_visible = 0;
12367
8c002a25
KH
12368
12369 BLOCK_INPUT;
334208b7 12370 XFlush (FRAME_X_DISPLAY (f));
8c002a25 12371 UNBLOCK_INPUT;
3afe33e7
RS
12372#else /* not USE_X_TOOLKIT */
12373
fd13dbb2
RS
12374 /* Make sure the X server knows where the window should be positioned,
12375 in case the user deiconifies with the window manager. */
12376 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
7556890b 12377 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
fd13dbb2 12378
16bd92ea
JB
12379 /* Since we don't know which revision of X we're running, we'll use both
12380 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
12381
12382 /* X11R4: send a ClientMessage to the window manager using the
12383 WM_CHANGE_STATE type. */
12384 {
12385 XEvent message;
58769bee 12386
c118dd06 12387 message.xclient.window = FRAME_X_WINDOW (f);
16bd92ea 12388 message.xclient.type = ClientMessage;
334208b7 12389 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
16bd92ea
JB
12390 message.xclient.format = 32;
12391 message.xclient.data.l[0] = IconicState;
12392
334208b7
RS
12393 if (! XSendEvent (FRAME_X_DISPLAY (f),
12394 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea
JB
12395 False,
12396 SubstructureRedirectMask | SubstructureNotifyMask,
12397 &message))
dc6f92b8
JB
12398 {
12399 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 12400 error ("Can't notify window manager of iconification");
dc6f92b8 12401 }
16bd92ea 12402 }
dc6f92b8 12403
58769bee 12404 /* X11R3: set the initial_state field of the window manager hints to
16bd92ea
JB
12405 IconicState. */
12406 x_wm_set_window_state (f, IconicState);
dc6f92b8 12407
a9c00105
RS
12408 if (!FRAME_VISIBLE_P (f))
12409 {
12410 /* If the frame was withdrawn, before, we must map it. */
7f9c7f94 12411 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
a9c00105
RS
12412 }
12413
3a88c238 12414 f->async_iconified = 1;
1e6bc770 12415 f->async_visible = 0;
dc6f92b8 12416
334208b7 12417 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 12418 UNBLOCK_INPUT;
8c002a25 12419#endif /* not USE_X_TOOLKIT */
dc6f92b8 12420}
19f71add 12421
d047c4eb 12422\f
19f71add 12423/* Free X resources of frame F. */
dc6f92b8 12424
dfcf069d 12425void
19f71add 12426x_free_frame_resources (f)
f676886a 12427 struct frame *f;
dc6f92b8 12428{
7f9c7f94
RS
12429 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12430
dc6f92b8 12431 BLOCK_INPUT;
c0ff3fab 12432
6186a4a0
RS
12433 /* If a display connection is dead, don't try sending more
12434 commands to the X server. */
19f71add 12435 if (dpyinfo->display)
6186a4a0 12436 {
19f71add 12437 if (f->output_data.x->icon_desc)
6186a4a0 12438 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
19f71add 12439
31f41daf 12440#ifdef HAVE_X_I18N
f5d11644
GM
12441 if (FRAME_XIC (f))
12442 free_frame_xic (f);
31f41daf 12443#endif
19f71add 12444
2662734b 12445 if (FRAME_X_WINDOW (f))
19f71add
GM
12446 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12447
3afe33e7 12448#ifdef USE_X_TOOLKIT
06a2c219
GM
12449 if (f->output_data.x->widget)
12450 XtDestroyWidget (f->output_data.x->widget);
6186a4a0 12451 free_frame_menubar (f);
3afe33e7
RS
12452#endif /* USE_X_TOOLKIT */
12453
3e71d8f2
GM
12454 unload_color (f, f->output_data.x->foreground_pixel);
12455 unload_color (f, f->output_data.x->background_pixel);
12456 unload_color (f, f->output_data.x->cursor_pixel);
12457 unload_color (f, f->output_data.x->cursor_foreground_pixel);
12458 unload_color (f, f->output_data.x->border_pixel);
12459 unload_color (f, f->output_data.x->mouse_pixel);
19f71add 12460
3e71d8f2
GM
12461 if (f->output_data.x->scroll_bar_background_pixel != -1)
12462 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
12463 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
12464 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
12465 if (f->output_data.x->white_relief.allocated_p)
12466 unload_color (f, f->output_data.x->white_relief.pixel);
12467 if (f->output_data.x->black_relief.allocated_p)
12468 unload_color (f, f->output_data.x->black_relief.pixel);
4ca78676 12469
19f71add
GM
12470 if (FRAME_FACE_CACHE (f))
12471 free_frame_faces (f);
12472
4ca78676 12473 x_free_gcs (f);
6186a4a0
RS
12474 XFlush (FRAME_X_DISPLAY (f));
12475 }
dc6f92b8 12476
df89d8a4 12477 if (f->output_data.x->saved_menu_event)
06a2c219 12478 xfree (f->output_data.x->saved_menu_event);
0c49ce2f 12479
7556890b 12480 xfree (f->output_data.x);
19f71add
GM
12481 f->output_data.x = NULL;
12482
0f941935
KH
12483 if (f == dpyinfo->x_focus_frame)
12484 dpyinfo->x_focus_frame = 0;
12485 if (f == dpyinfo->x_focus_event_frame)
12486 dpyinfo->x_focus_event_frame = 0;
12487 if (f == dpyinfo->x_highlight_frame)
12488 dpyinfo->x_highlight_frame = 0;
c0ff3fab 12489
7f9c7f94 12490 if (f == dpyinfo->mouse_face_mouse_frame)
dc05a16b 12491 {
7f9c7f94
RS
12492 dpyinfo->mouse_face_beg_row
12493 = dpyinfo->mouse_face_beg_col = -1;
12494 dpyinfo->mouse_face_end_row
12495 = dpyinfo->mouse_face_end_col = -1;
12496 dpyinfo->mouse_face_window = Qnil;
21323706
RS
12497 dpyinfo->mouse_face_deferred_gc = 0;
12498 dpyinfo->mouse_face_mouse_frame = 0;
dc05a16b 12499 }
0134a210 12500
c0ff3fab 12501 UNBLOCK_INPUT;
dc6f92b8 12502}
19f71add
GM
12503
12504
12505/* Destroy the X window of frame F. */
12506
12507void
12508x_destroy_window (f)
12509 struct frame *f;
12510{
12511 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12512
12513 /* If a display connection is dead, don't try sending more
12514 commands to the X server. */
12515 if (dpyinfo->display != 0)
12516 x_free_frame_resources (f);
12517
12518 dpyinfo->reference_count--;
12519}
12520
dc6f92b8 12521\f
f451eb13
JB
12522/* Setting window manager hints. */
12523
af31d76f
RS
12524/* Set the normal size hints for the window manager, for frame F.
12525 FLAGS is the flags word to use--or 0 meaning preserve the flags
12526 that the window now has.
12527 If USER_POSITION is nonzero, we set the USPosition
12528 flag (this is useful when FLAGS is 0). */
6dba1858 12529
dfcf069d 12530void
af31d76f 12531x_wm_set_size_hint (f, flags, user_position)
f676886a 12532 struct frame *f;
af31d76f
RS
12533 long flags;
12534 int user_position;
dc6f92b8
JB
12535{
12536 XSizeHints size_hints;
3afe33e7
RS
12537
12538#ifdef USE_X_TOOLKIT
7e4f2521
FP
12539 Arg al[2];
12540 int ac = 0;
12541 Dimension widget_width, widget_height;
7556890b 12542 Window window = XtWindow (f->output_data.x->widget);
3afe33e7 12543#else /* not USE_X_TOOLKIT */
c118dd06 12544 Window window = FRAME_X_WINDOW (f);
3afe33e7 12545#endif /* not USE_X_TOOLKIT */
dc6f92b8 12546
b72a58fd
RS
12547 /* Setting PMaxSize caused various problems. */
12548 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
dc6f92b8 12549
7556890b
RS
12550 size_hints.x = f->output_data.x->left_pos;
12551 size_hints.y = f->output_data.x->top_pos;
7553a6b7 12552
7e4f2521
FP
12553#ifdef USE_X_TOOLKIT
12554 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
12555 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
7556890b 12556 XtGetValues (f->output_data.x->widget, al, ac);
7e4f2521
FP
12557 size_hints.height = widget_height;
12558 size_hints.width = widget_width;
12559#else /* not USE_X_TOOLKIT */
f676886a
JB
12560 size_hints.height = PIXEL_HEIGHT (f);
12561 size_hints.width = PIXEL_WIDTH (f);
7e4f2521 12562#endif /* not USE_X_TOOLKIT */
7553a6b7 12563
7556890b
RS
12564 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
12565 size_hints.height_inc = f->output_data.x->line_height;
334208b7
RS
12566 size_hints.max_width
12567 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
12568 size_hints.max_height
12569 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
0134a210 12570
d067ea8b
KH
12571 /* Calculate the base and minimum sizes.
12572
12573 (When we use the X toolkit, we don't do it here.
12574 Instead we copy the values that the widgets are using, below.) */
12575#ifndef USE_X_TOOLKIT
b1c884c3 12576 {
b0342f17 12577 int base_width, base_height;
0134a210 12578 int min_rows = 0, min_cols = 0;
b0342f17 12579
f451eb13
JB
12580 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
12581 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
b0342f17 12582
0134a210 12583 check_frame_size (f, &min_rows, &min_cols);
b0342f17 12584
0134a210
RS
12585 /* The window manager uses the base width hints to calculate the
12586 current number of rows and columns in the frame while
12587 resizing; min_width and min_height aren't useful for this
12588 purpose, since they might not give the dimensions for a
12589 zero-row, zero-column frame.
58769bee 12590
0134a210
RS
12591 We use the base_width and base_height members if we have
12592 them; otherwise, we set the min_width and min_height members
12593 to the size for a zero x zero frame. */
b0342f17
JB
12594
12595#ifdef HAVE_X11R4
0134a210
RS
12596 size_hints.flags |= PBaseSize;
12597 size_hints.base_width = base_width;
12598 size_hints.base_height = base_height;
12599 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
12600 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
b0342f17 12601#else
0134a210
RS
12602 size_hints.min_width = base_width;
12603 size_hints.min_height = base_height;
b0342f17 12604#endif
b1c884c3 12605 }
dc6f92b8 12606
d067ea8b 12607 /* If we don't need the old flags, we don't need the old hint at all. */
af31d76f 12608 if (flags)
dc6f92b8 12609 {
d067ea8b
KH
12610 size_hints.flags |= flags;
12611 goto no_read;
12612 }
12613#endif /* not USE_X_TOOLKIT */
12614
12615 {
12616 XSizeHints hints; /* Sometimes I hate X Windows... */
12617 long supplied_return;
12618 int value;
af31d76f
RS
12619
12620#ifdef HAVE_X11R4
d067ea8b
KH
12621 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
12622 &supplied_return);
af31d76f 12623#else
d067ea8b 12624 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
af31d76f 12625#endif
58769bee 12626
d067ea8b
KH
12627#ifdef USE_X_TOOLKIT
12628 size_hints.base_height = hints.base_height;
12629 size_hints.base_width = hints.base_width;
12630 size_hints.min_height = hints.min_height;
12631 size_hints.min_width = hints.min_width;
12632#endif
12633
12634 if (flags)
12635 size_hints.flags |= flags;
12636 else
12637 {
12638 if (value == 0)
12639 hints.flags = 0;
12640 if (hints.flags & PSize)
12641 size_hints.flags |= PSize;
12642 if (hints.flags & PPosition)
12643 size_hints.flags |= PPosition;
12644 if (hints.flags & USPosition)
12645 size_hints.flags |= USPosition;
12646 if (hints.flags & USSize)
12647 size_hints.flags |= USSize;
12648 }
12649 }
12650
06a2c219 12651#ifndef USE_X_TOOLKIT
d067ea8b 12652 no_read:
06a2c219 12653#endif
0134a210 12654
af31d76f 12655#ifdef PWinGravity
7556890b 12656 size_hints.win_gravity = f->output_data.x->win_gravity;
af31d76f 12657 size_hints.flags |= PWinGravity;
dc05a16b 12658
af31d76f 12659 if (user_position)
6dba1858 12660 {
af31d76f
RS
12661 size_hints.flags &= ~ PPosition;
12662 size_hints.flags |= USPosition;
6dba1858 12663 }
2554751d 12664#endif /* PWinGravity */
6dba1858 12665
b0342f17 12666#ifdef HAVE_X11R4
334208b7 12667 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 12668#else
334208b7 12669 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 12670#endif
dc6f92b8
JB
12671}
12672
12673/* Used for IconicState or NormalState */
06a2c219 12674
dfcf069d 12675void
f676886a
JB
12676x_wm_set_window_state (f, state)
12677 struct frame *f;
dc6f92b8
JB
12678 int state;
12679{
3afe33e7 12680#ifdef USE_X_TOOLKIT
546e6d5b
RS
12681 Arg al[1];
12682
12683 XtSetArg (al[0], XtNinitialState, state);
7556890b 12684 XtSetValues (f->output_data.x->widget, al, 1);
3afe33e7 12685#else /* not USE_X_TOOLKIT */
c118dd06 12686 Window window = FRAME_X_WINDOW (f);
dc6f92b8 12687
7556890b
RS
12688 f->output_data.x->wm_hints.flags |= StateHint;
12689 f->output_data.x->wm_hints.initial_state = state;
b1c884c3 12690
7556890b 12691 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
546e6d5b 12692#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12693}
12694
dfcf069d 12695void
7f2ae036 12696x_wm_set_icon_pixmap (f, pixmap_id)
f676886a 12697 struct frame *f;
7f2ae036 12698 int pixmap_id;
dc6f92b8 12699{
d2bd6bc4
RS
12700 Pixmap icon_pixmap;
12701
06a2c219 12702#ifndef USE_X_TOOLKIT
c118dd06 12703 Window window = FRAME_X_WINDOW (f);
75231bad 12704#endif
dc6f92b8 12705
7f2ae036 12706 if (pixmap_id > 0)
dbc4e1c1 12707 {
d2bd6bc4 12708 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
7556890b 12709 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
dbc4e1c1
JB
12710 }
12711 else
68568555
RS
12712 {
12713 /* It seems there is no way to turn off use of an icon pixmap.
12714 The following line does it, only if no icon has yet been created,
12715 for some window managers. But with mwm it crashes.
12716 Some people say it should clear the IconPixmapHint bit in this case,
12717 but that doesn't work, and the X consortium said it isn't the
12718 right thing at all. Since there is no way to win,
12719 best to explicitly give up. */
12720#if 0
12721 f->output_data.x->wm_hints.icon_pixmap = None;
12722#else
12723 return;
12724#endif
12725 }
b1c884c3 12726
d2bd6bc4
RS
12727#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
12728
12729 {
12730 Arg al[1];
12731 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12732 XtSetValues (f->output_data.x->widget, al, 1);
12733 }
12734
12735#else /* not USE_X_TOOLKIT */
12736
7556890b
RS
12737 f->output_data.x->wm_hints.flags |= IconPixmapHint;
12738 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
d2bd6bc4
RS
12739
12740#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12741}
12742
dfcf069d 12743void
f676886a
JB
12744x_wm_set_icon_position (f, icon_x, icon_y)
12745 struct frame *f;
dc6f92b8
JB
12746 int icon_x, icon_y;
12747{
75231bad 12748#ifdef USE_X_TOOLKIT
7556890b 12749 Window window = XtWindow (f->output_data.x->widget);
75231bad 12750#else
c118dd06 12751 Window window = FRAME_X_WINDOW (f);
75231bad 12752#endif
dc6f92b8 12753
7556890b
RS
12754 f->output_data.x->wm_hints.flags |= IconPositionHint;
12755 f->output_data.x->wm_hints.icon_x = icon_x;
12756 f->output_data.x->wm_hints.icon_y = icon_y;
b1c884c3 12757
7556890b 12758 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
dc6f92b8
JB
12759}
12760
12761\f
06a2c219
GM
12762/***********************************************************************
12763 Fonts
12764 ***********************************************************************/
dc43ef94
KH
12765
12766/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
06a2c219 12767
dc43ef94
KH
12768struct font_info *
12769x_get_font_info (f, font_idx)
12770 FRAME_PTR f;
12771 int font_idx;
12772{
12773 return (FRAME_X_FONT_TABLE (f) + font_idx);
12774}
12775
12776
12777/* Return a list of names of available fonts matching PATTERN on frame
12778 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
12779 to be listed. Frame F NULL means we have not yet created any
12780 frame on X, and consult the first display in x_display_list.
12781 MAXNAMES sets a limit on how many fonts to match. */
12782
12783Lisp_Object
12784x_list_fonts (f, pattern, size, maxnames)
12785 FRAME_PTR f;
12786 Lisp_Object pattern;
12787 int size;
12788 int maxnames;
12789{
06a2c219
GM
12790 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
12791 Lisp_Object tem, second_best;
dc43ef94 12792 Display *dpy = f != NULL ? FRAME_X_DISPLAY (f) : x_display_list->display;
09c6077f 12793 int try_XLoadQueryFont = 0;
53ca4657 12794 int count;
dc43ef94 12795
6b0efe73 12796 patterns = Fassoc (pattern, Valternate_fontname_alist);
2da424f1
KH
12797 if (NILP (patterns))
12798 patterns = Fcons (pattern, Qnil);
81ba44e5 12799
09c6077f
KH
12800 if (maxnames == 1 && !size)
12801 /* We can return any single font matching PATTERN. */
12802 try_XLoadQueryFont = 1;
9a32686f 12803
8e713be6 12804 for (; CONSP (patterns); patterns = XCDR (patterns))
dc43ef94 12805 {
dc43ef94 12806 int num_fonts;
3e71d8f2 12807 char **names = NULL;
dc43ef94 12808
8e713be6 12809 pattern = XCAR (patterns);
536f4067
RS
12810 /* See if we cached the result for this particular query.
12811 The cache is an alist of the form:
12812 (((PATTERN . MAXNAMES) (FONTNAME . WIDTH) ...) ...)
12813 */
8e713be6 12814 if (f && (tem = XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element),
b5210ea7
KH
12815 key = Fcons (pattern, make_number (maxnames)),
12816 !NILP (list = Fassoc (key, tem))))
12817 {
12818 list = Fcdr_safe (list);
12819 /* We have a cashed list. Don't have to get the list again. */
12820 goto label_cached;
12821 }
12822
12823 /* At first, put PATTERN in the cache. */
09c6077f 12824
dc43ef94 12825 BLOCK_INPUT;
17d85edc
KH
12826 count = x_catch_errors (dpy);
12827
09c6077f
KH
12828 if (try_XLoadQueryFont)
12829 {
12830 XFontStruct *font;
12831 unsigned long value;
12832
12833 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
17d85edc
KH
12834 if (x_had_errors_p (dpy))
12835 {
12836 /* This error is perhaps due to insufficient memory on X
12837 server. Let's just ignore it. */
12838 font = NULL;
12839 x_clear_errors (dpy);
12840 }
12841
09c6077f
KH
12842 if (font
12843 && XGetFontProperty (font, XA_FONT, &value))
12844 {
12845 char *name = (char *) XGetAtomName (dpy, (Atom) value);
12846 int len = strlen (name);
01c752b5 12847 char *tmp;
09c6077f 12848
6f6512e8
KH
12849 /* If DXPC (a Differential X Protocol Compressor)
12850 Ver.3.7 is running, XGetAtomName will return null
12851 string. We must avoid such a name. */
12852 if (len == 0)
12853 try_XLoadQueryFont = 0;
12854 else
12855 {
12856 num_fonts = 1;
12857 names = (char **) alloca (sizeof (char *));
12858 /* Some systems only allow alloca assigned to a
12859 simple var. */
12860 tmp = (char *) alloca (len + 1); names[0] = tmp;
12861 bcopy (name, names[0], len + 1);
12862 XFree (name);
12863 }
09c6077f
KH
12864 }
12865 else
12866 try_XLoadQueryFont = 0;
a083fd23
RS
12867
12868 if (font)
12869 XFreeFont (dpy, font);
09c6077f
KH
12870 }
12871
12872 if (!try_XLoadQueryFont)
17d85edc
KH
12873 {
12874 /* We try at least 10 fonts because XListFonts will return
12875 auto-scaled fonts at the head. */
12876 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
12877 &num_fonts);
12878 if (x_had_errors_p (dpy))
12879 {
12880 /* This error is perhaps due to insufficient memory on X
12881 server. Let's just ignore it. */
12882 names = NULL;
12883 x_clear_errors (dpy);
12884 }
12885 }
12886
12887 x_uncatch_errors (dpy, count);
dc43ef94
KH
12888 UNBLOCK_INPUT;
12889
12890 if (names)
12891 {
12892 int i;
dc43ef94
KH
12893
12894 /* Make a list of all the fonts we got back.
12895 Store that in the font cache for the display. */
12896 for (i = 0; i < num_fonts; i++)
12897 {
06a2c219 12898 int width = 0;
dc43ef94 12899 char *p = names[i];
06a2c219
GM
12900 int average_width = -1, dashes = 0;
12901
dc43ef94 12902 /* Count the number of dashes in NAMES[I]. If there are
b5210ea7
KH
12903 14 dashes, and the field value following 12th dash
12904 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
12905 is usually too ugly to be used for editing. Let's
12906 ignore it. */
dc43ef94
KH
12907 while (*p)
12908 if (*p++ == '-')
12909 {
12910 dashes++;
12911 if (dashes == 7) /* PIXEL_SIZE field */
12912 width = atoi (p);
12913 else if (dashes == 12) /* AVERAGE_WIDTH field */
12914 average_width = atoi (p);
12915 }
12916 if (dashes < 14 || average_width != 0)
12917 {
12918 tem = build_string (names[i]);
12919 if (NILP (Fassoc (tem, list)))
12920 {
12921 if (STRINGP (Vx_pixel_size_width_font_regexp)
f39db7ea
RS
12922 && ((fast_c_string_match_ignore_case
12923 (Vx_pixel_size_width_font_regexp, names[i]))
dc43ef94
KH
12924 >= 0))
12925 /* We can set the value of PIXEL_SIZE to the
b5210ea7 12926 width of this font. */
dc43ef94
KH
12927 list = Fcons (Fcons (tem, make_number (width)), list);
12928 else
12929 /* For the moment, width is not known. */
12930 list = Fcons (Fcons (tem, Qnil), list);
12931 }
12932 }
12933 }
09c6077f
KH
12934 if (!try_XLoadQueryFont)
12935 XFreeFontNames (names);
dc43ef94
KH
12936 }
12937
b5210ea7 12938 /* Now store the result in the cache. */
dc43ef94 12939 if (f != NULL)
8e713be6 12940 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element)
dc43ef94 12941 = Fcons (Fcons (key, list),
8e713be6 12942 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
dc43ef94 12943
b5210ea7
KH
12944 label_cached:
12945 if (NILP (list)) continue; /* Try the remaining alternatives. */
dc43ef94 12946
b5210ea7
KH
12947 newlist = second_best = Qnil;
12948 /* Make a list of the fonts that have the right width. */
8e713be6 12949 for (; CONSP (list); list = XCDR (list))
b5210ea7 12950 {
536f4067
RS
12951 int found_size;
12952
8e713be6 12953 tem = XCAR (list);
dc43ef94 12954
8e713be6 12955 if (!CONSP (tem) || NILP (XCAR (tem)))
b5210ea7
KH
12956 continue;
12957 if (!size)
12958 {
8e713be6 12959 newlist = Fcons (XCAR (tem), newlist);
b5210ea7
KH
12960 continue;
12961 }
dc43ef94 12962
8e713be6 12963 if (!INTEGERP (XCDR (tem)))
dc43ef94 12964 {
b5210ea7
KH
12965 /* Since we have not yet known the size of this font, we
12966 must try slow function call XLoadQueryFont. */
dc43ef94
KH
12967 XFontStruct *thisinfo;
12968
12969 BLOCK_INPUT;
17d85edc 12970 count = x_catch_errors (dpy);
dc43ef94 12971 thisinfo = XLoadQueryFont (dpy,
8e713be6 12972 XSTRING (XCAR (tem))->data);
17d85edc
KH
12973 if (x_had_errors_p (dpy))
12974 {
12975 /* This error is perhaps due to insufficient memory on X
12976 server. Let's just ignore it. */
12977 thisinfo = NULL;
12978 x_clear_errors (dpy);
12979 }
12980 x_uncatch_errors (dpy, count);
dc43ef94
KH
12981 UNBLOCK_INPUT;
12982
12983 if (thisinfo)
12984 {
8e713be6 12985 XCDR (tem)
536f4067
RS
12986 = (thisinfo->min_bounds.width == 0
12987 ? make_number (0)
12988 : make_number (thisinfo->max_bounds.width));
dc43ef94
KH
12989 XFreeFont (dpy, thisinfo);
12990 }
12991 else
b5210ea7 12992 /* For unknown reason, the previous call of XListFont had
06a2c219 12993 returned a font which can't be opened. Record the size
b5210ea7 12994 as 0 not to try to open it again. */
8e713be6 12995 XCDR (tem) = make_number (0);
dc43ef94 12996 }
536f4067 12997
8e713be6 12998 found_size = XINT (XCDR (tem));
536f4067 12999 if (found_size == size)
8e713be6 13000 newlist = Fcons (XCAR (tem), newlist);
536f4067 13001 else if (found_size > 0)
b5210ea7 13002 {
536f4067 13003 if (NILP (second_best))
b5210ea7 13004 second_best = tem;
536f4067
RS
13005 else if (found_size < size)
13006 {
8e713be6
KR
13007 if (XINT (XCDR (second_best)) > size
13008 || XINT (XCDR (second_best)) < found_size)
536f4067
RS
13009 second_best = tem;
13010 }
13011 else
13012 {
8e713be6
KR
13013 if (XINT (XCDR (second_best)) > size
13014 && XINT (XCDR (second_best)) > found_size)
536f4067
RS
13015 second_best = tem;
13016 }
b5210ea7
KH
13017 }
13018 }
13019 if (!NILP (newlist))
13020 break;
13021 else if (!NILP (second_best))
13022 {
8e713be6 13023 newlist = Fcons (XCAR (second_best), Qnil);
b5210ea7 13024 break;
dc43ef94 13025 }
dc43ef94
KH
13026 }
13027
13028 return newlist;
13029}
13030
06a2c219
GM
13031
13032#if GLYPH_DEBUG
13033
13034/* Check that FONT is valid on frame F. It is if it can be found in F's
13035 font table. */
13036
13037static void
13038x_check_font (f, font)
13039 struct frame *f;
13040 XFontStruct *font;
13041{
13042 int i;
13043 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13044
13045 xassert (font != NULL);
13046
13047 for (i = 0; i < dpyinfo->n_fonts; i++)
13048 if (dpyinfo->font_table[i].name
13049 && font == dpyinfo->font_table[i].font)
13050 break;
13051
13052 xassert (i < dpyinfo->n_fonts);
13053}
13054
13055#endif /* GLYPH_DEBUG != 0 */
13056
13057/* Set *W to the minimum width, *H to the minimum font height of FONT.
13058 Note: There are (broken) X fonts out there with invalid XFontStruct
13059 min_bounds contents. For example, handa@etl.go.jp reports that
13060 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
13061 have font->min_bounds.width == 0. */
13062
13063static INLINE void
13064x_font_min_bounds (font, w, h)
13065 XFontStruct *font;
13066 int *w, *h;
13067{
13068 *h = FONT_HEIGHT (font);
13069 *w = font->min_bounds.width;
13070
13071 /* Try to handle the case where FONT->min_bounds has invalid
13072 contents. Since the only font known to have invalid min_bounds
13073 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
13074 if (*w <= 0)
13075 *w = font->max_bounds.width;
13076}
13077
13078
13079/* Compute the smallest character width and smallest font height over
13080 all fonts available on frame F. Set the members smallest_char_width
13081 and smallest_font_height in F's x_display_info structure to
13082 the values computed. Value is non-zero if smallest_font_height or
13083 smallest_char_width become smaller than they were before. */
13084
13085static int
13086x_compute_min_glyph_bounds (f)
13087 struct frame *f;
13088{
13089 int i;
13090 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13091 XFontStruct *font;
13092 int old_width = dpyinfo->smallest_char_width;
13093 int old_height = dpyinfo->smallest_font_height;
13094
13095 dpyinfo->smallest_font_height = 100000;
13096 dpyinfo->smallest_char_width = 100000;
13097
13098 for (i = 0; i < dpyinfo->n_fonts; ++i)
13099 if (dpyinfo->font_table[i].name)
13100 {
13101 struct font_info *fontp = dpyinfo->font_table + i;
13102 int w, h;
13103
13104 font = (XFontStruct *) fontp->font;
13105 xassert (font != (XFontStruct *) ~0);
13106 x_font_min_bounds (font, &w, &h);
13107
13108 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
13109 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
13110 }
13111
13112 xassert (dpyinfo->smallest_char_width > 0
13113 && dpyinfo->smallest_font_height > 0);
13114
13115 return (dpyinfo->n_fonts == 1
13116 || dpyinfo->smallest_char_width < old_width
13117 || dpyinfo->smallest_font_height < old_height);
13118}
13119
13120
dc43ef94
KH
13121/* Load font named FONTNAME of the size SIZE for frame F, and return a
13122 pointer to the structure font_info while allocating it dynamically.
13123 If SIZE is 0, load any size of font.
13124 If loading is failed, return NULL. */
13125
13126struct font_info *
13127x_load_font (f, fontname, size)
13128 struct frame *f;
13129 register char *fontname;
13130 int size;
13131{
13132 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13133 Lisp_Object font_names;
d645aaa4 13134 int count;
dc43ef94
KH
13135
13136 /* Get a list of all the fonts that match this name. Once we
13137 have a list of matching fonts, we compare them against the fonts
13138 we already have by comparing names. */
09c6077f 13139 font_names = x_list_fonts (f, build_string (fontname), size, 1);
dc43ef94
KH
13140
13141 if (!NILP (font_names))
13142 {
13143 Lisp_Object tail;
13144 int i;
13145
13146 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 13147 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
06a2c219
GM
13148 if (dpyinfo->font_table[i].name
13149 && (!strcmp (dpyinfo->font_table[i].name,
8e713be6 13150 XSTRING (XCAR (tail))->data)
06a2c219 13151 || !strcmp (dpyinfo->font_table[i].full_name,
8e713be6 13152 XSTRING (XCAR (tail))->data)))
dc43ef94
KH
13153 return (dpyinfo->font_table + i);
13154 }
13155
13156 /* Load the font and add it to the table. */
13157 {
13158 char *full_name;
13159 XFontStruct *font;
13160 struct font_info *fontp;
13161 unsigned long value;
06a2c219 13162 int i;
dc43ef94 13163
2da424f1
KH
13164 /* If we have found fonts by x_list_font, load one of them. If
13165 not, we still try to load a font by the name given as FONTNAME
13166 because XListFonts (called in x_list_font) of some X server has
13167 a bug of not finding a font even if the font surely exists and
13168 is loadable by XLoadQueryFont. */
e1d6d5b9 13169 if (size > 0 && !NILP (font_names))
8e713be6 13170 fontname = (char *) XSTRING (XCAR (font_names))->data;
dc43ef94
KH
13171
13172 BLOCK_INPUT;
d645aaa4 13173 count = x_catch_errors (FRAME_X_DISPLAY (f));
dc43ef94 13174 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
d645aaa4
KH
13175 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
13176 {
13177 /* This error is perhaps due to insufficient memory on X
13178 server. Let's just ignore it. */
13179 font = NULL;
13180 x_clear_errors (FRAME_X_DISPLAY (f));
13181 }
13182 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
dc43ef94 13183 UNBLOCK_INPUT;
b5210ea7 13184 if (!font)
dc43ef94
KH
13185 return NULL;
13186
06a2c219
GM
13187 /* Find a free slot in the font table. */
13188 for (i = 0; i < dpyinfo->n_fonts; ++i)
13189 if (dpyinfo->font_table[i].name == NULL)
13190 break;
13191
13192 /* If no free slot found, maybe enlarge the font table. */
13193 if (i == dpyinfo->n_fonts
13194 && dpyinfo->n_fonts == dpyinfo->font_table_size)
dc43ef94 13195 {
06a2c219
GM
13196 int sz;
13197 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
13198 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
dc43ef94 13199 dpyinfo->font_table
06a2c219 13200 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
dc43ef94
KH
13201 }
13202
06a2c219
GM
13203 fontp = dpyinfo->font_table + i;
13204 if (i == dpyinfo->n_fonts)
13205 ++dpyinfo->n_fonts;
dc43ef94
KH
13206
13207 /* Now fill in the slots of *FONTP. */
13208 BLOCK_INPUT;
13209 fontp->font = font;
06a2c219 13210 fontp->font_idx = i;
dc43ef94
KH
13211 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
13212 bcopy (fontname, fontp->name, strlen (fontname) + 1);
13213
13214 /* Try to get the full name of FONT. Put it in FULL_NAME. */
13215 full_name = 0;
13216 if (XGetFontProperty (font, XA_FONT, &value))
13217 {
13218 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
13219 char *p = name;
13220 int dashes = 0;
13221
13222 /* Count the number of dashes in the "full name".
13223 If it is too few, this isn't really the font's full name,
13224 so don't use it.
13225 In X11R4, the fonts did not come with their canonical names
13226 stored in them. */
13227 while (*p)
13228 {
13229 if (*p == '-')
13230 dashes++;
13231 p++;
13232 }
13233
13234 if (dashes >= 13)
13235 {
13236 full_name = (char *) xmalloc (p - name + 1);
13237 bcopy (name, full_name, p - name + 1);
13238 }
13239
13240 XFree (name);
13241 }
13242
13243 if (full_name != 0)
13244 fontp->full_name = full_name;
13245 else
13246 fontp->full_name = fontp->name;
13247
13248 fontp->size = font->max_bounds.width;
d5749adb 13249 fontp->height = FONT_HEIGHT (font);
dc43ef94 13250
2da424f1
KH
13251 if (NILP (font_names))
13252 {
13253 /* We come here because of a bug of XListFonts mentioned at
13254 the head of this block. Let's store this information in
13255 the cache for x_list_fonts. */
13256 Lisp_Object lispy_name = build_string (fontname);
13257 Lisp_Object lispy_full_name = build_string (fontp->full_name);
13258
8e713be6 13259 XCDR (dpyinfo->name_list_element)
2da424f1
KH
13260 = Fcons (Fcons (Fcons (lispy_name, make_number (256)),
13261 Fcons (Fcons (lispy_full_name,
13262 make_number (fontp->size)),
13263 Qnil)),
8e713be6 13264 XCDR (dpyinfo->name_list_element));
2da424f1 13265 if (full_name)
8e713be6 13266 XCDR (dpyinfo->name_list_element)
2da424f1
KH
13267 = Fcons (Fcons (Fcons (lispy_full_name, make_number (256)),
13268 Fcons (Fcons (lispy_full_name,
13269 make_number (fontp->size)),
13270 Qnil)),
8e713be6 13271 XCDR (dpyinfo->name_list_element));
2da424f1
KH
13272 }
13273
dc43ef94
KH
13274 /* The slot `encoding' specifies how to map a character
13275 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
ee569018
KH
13276 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
13277 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
8ff102bd 13278 2:0xA020..0xFF7F). For the moment, we don't know which charset
06a2c219 13279 uses this font. So, we set information in fontp->encoding[1]
8ff102bd
RS
13280 which is never used by any charset. If mapping can't be
13281 decided, set FONT_ENCODING_NOT_DECIDED. */
dc43ef94
KH
13282 fontp->encoding[1]
13283 = (font->max_byte1 == 0
13284 /* 1-byte font */
13285 ? (font->min_char_or_byte2 < 0x80
13286 ? (font->max_char_or_byte2 < 0x80
13287 ? 0 /* 0x20..0x7F */
8ff102bd 13288 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
dc43ef94
KH
13289 : 1) /* 0xA0..0xFF */
13290 /* 2-byte font */
13291 : (font->min_byte1 < 0x80
13292 ? (font->max_byte1 < 0x80
13293 ? (font->min_char_or_byte2 < 0x80
13294 ? (font->max_char_or_byte2 < 0x80
13295 ? 0 /* 0x2020..0x7F7F */
8ff102bd 13296 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
dc43ef94 13297 : 3) /* 0x20A0..0x7FFF */
8ff102bd 13298 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
dc43ef94
KH
13299 : (font->min_char_or_byte2 < 0x80
13300 ? (font->max_char_or_byte2 < 0x80
13301 ? 2 /* 0xA020..0xFF7F */
8ff102bd 13302 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
dc43ef94
KH
13303 : 1))); /* 0xA0A0..0xFFFF */
13304
13305 fontp->baseline_offset
13306 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
13307 ? (long) value : 0);
13308 fontp->relative_compose
13309 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
13310 ? (long) value : 0);
f78798df
KH
13311 fontp->default_ascent
13312 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
13313 ? (long) value : 0);
dc43ef94 13314
06a2c219
GM
13315 /* Set global flag fonts_changed_p to non-zero if the font loaded
13316 has a character with a smaller width than any other character
13317 before, or if the font loaded has a smalle>r height than any
13318 other font loaded before. If this happens, it will make a
13319 glyph matrix reallocation necessary. */
13320 fonts_changed_p = x_compute_min_glyph_bounds (f);
dc43ef94 13321 UNBLOCK_INPUT;
dc43ef94
KH
13322 return fontp;
13323 }
13324}
13325
06a2c219
GM
13326
13327/* Return a pointer to struct font_info of a font named FONTNAME for
13328 frame F. If no such font is loaded, return NULL. */
13329
dc43ef94
KH
13330struct font_info *
13331x_query_font (f, fontname)
13332 struct frame *f;
13333 register char *fontname;
13334{
13335 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13336 int i;
13337
13338 for (i = 0; i < dpyinfo->n_fonts; i++)
06a2c219
GM
13339 if (dpyinfo->font_table[i].name
13340 && (!strcmp (dpyinfo->font_table[i].name, fontname)
13341 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
dc43ef94
KH
13342 return (dpyinfo->font_table + i);
13343 return NULL;
13344}
13345
06a2c219
GM
13346
13347/* Find a CCL program for a font specified by FONTP, and set the member
a6582676
KH
13348 `encoder' of the structure. */
13349
13350void
13351x_find_ccl_program (fontp)
13352 struct font_info *fontp;
13353{
a42f54e6 13354 Lisp_Object list, elt;
a6582676 13355
f9b5db02 13356 elt = Qnil;
8e713be6 13357 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
a6582676 13358 {
8e713be6 13359 elt = XCAR (list);
a6582676 13360 if (CONSP (elt)
8e713be6 13361 && STRINGP (XCAR (elt))
9f2feff6
KH
13362 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
13363 >= 0)
13364 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
13365 >= 0)))
a42f54e6
KH
13366 break;
13367 }
f9b5db02 13368
a42f54e6
KH
13369 if (! NILP (list))
13370 {
d27f8ca7
KH
13371 struct ccl_program *ccl
13372 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
a42f54e6 13373
8e713be6 13374 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
a42f54e6
KH
13375 xfree (ccl);
13376 else
13377 fontp->font_encoder = ccl;
a6582676
KH
13378 }
13379}
13380
06a2c219 13381
dc43ef94 13382\f
06a2c219
GM
13383/***********************************************************************
13384 Initialization
13385 ***********************************************************************/
f451eb13 13386
3afe33e7
RS
13387#ifdef USE_X_TOOLKIT
13388static XrmOptionDescRec emacs_options[] = {
13389 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
13390 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
13391
13392 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
13393 XrmoptionSepArg, NULL},
13394 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
13395
13396 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13397 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13398 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13399 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13400 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13401 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
13402 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
13403};
13404#endif /* USE_X_TOOLKIT */
13405
7a13e894
RS
13406static int x_initialized;
13407
29b38361
KH
13408#ifdef MULTI_KBOARD
13409/* Test whether two display-name strings agree up to the dot that separates
13410 the screen number from the server number. */
13411static int
13412same_x_server (name1, name2)
13413 char *name1, *name2;
13414{
13415 int seen_colon = 0;
cf591cc1
RS
13416 unsigned char *system_name = XSTRING (Vsystem_name)->data;
13417 int system_name_length = strlen (system_name);
13418 int length_until_period = 0;
13419
13420 while (system_name[length_until_period] != 0
13421 && system_name[length_until_period] != '.')
13422 length_until_period++;
13423
13424 /* Treat `unix' like an empty host name. */
13425 if (! strncmp (name1, "unix:", 5))
13426 name1 += 4;
13427 if (! strncmp (name2, "unix:", 5))
13428 name2 += 4;
13429 /* Treat this host's name like an empty host name. */
13430 if (! strncmp (name1, system_name, system_name_length)
13431 && name1[system_name_length] == ':')
13432 name1 += system_name_length;
13433 if (! strncmp (name2, system_name, system_name_length)
13434 && name2[system_name_length] == ':')
13435 name2 += system_name_length;
13436 /* Treat this host's domainless name like an empty host name. */
13437 if (! strncmp (name1, system_name, length_until_period)
13438 && name1[length_until_period] == ':')
13439 name1 += length_until_period;
13440 if (! strncmp (name2, system_name, length_until_period)
13441 && name2[length_until_period] == ':')
13442 name2 += length_until_period;
13443
29b38361
KH
13444 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
13445 {
13446 if (*name1 == ':')
13447 seen_colon++;
13448 if (seen_colon && *name1 == '.')
13449 return 1;
13450 }
13451 return (seen_colon
13452 && (*name1 == '.' || *name1 == '\0')
13453 && (*name2 == '.' || *name2 == '\0'));
13454}
13455#endif
13456
334208b7 13457struct x_display_info *
1f8255f2 13458x_term_init (display_name, xrm_option, resource_name)
334208b7 13459 Lisp_Object display_name;
1f8255f2
RS
13460 char *xrm_option;
13461 char *resource_name;
dc6f92b8 13462{
334208b7 13463 int connection;
7a13e894 13464 Display *dpy;
334208b7
RS
13465 struct x_display_info *dpyinfo;
13466 XrmDatabase xrdb;
13467
60439948
KH
13468 BLOCK_INPUT;
13469
7a13e894
RS
13470 if (!x_initialized)
13471 {
13472 x_initialize ();
13473 x_initialized = 1;
13474 }
dc6f92b8 13475
3afe33e7 13476#ifdef USE_X_TOOLKIT
2d7fc7e8
RS
13477 /* weiner@footloose.sps.mot.com reports that this causes
13478 errors with X11R5:
13479 X protocol error: BadAtom (invalid Atom parameter)
13480 on protocol request 18skiloaf.
13481 So let's not use it until R6. */
13482#ifdef HAVE_X11XTR6
bdcd49ba
RS
13483 XtSetLanguageProc (NULL, NULL, NULL);
13484#endif
13485
7f9c7f94
RS
13486 {
13487 int argc = 0;
13488 char *argv[3];
13489
13490 argv[0] = "";
13491 argc = 1;
13492 if (xrm_option)
13493 {
13494 argv[argc++] = "-xrm";
13495 argv[argc++] = xrm_option;
13496 }
13497 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
13498 resource_name, EMACS_CLASS,
13499 emacs_options, XtNumber (emacs_options),
13500 &argc, argv);
39d8bb4d
KH
13501
13502#ifdef HAVE_X11XTR6
10537cb1 13503 /* I think this is to compensate for XtSetLanguageProc. */
71f8198a 13504 fixup_locale ();
39d8bb4d 13505#endif
7f9c7f94 13506 }
3afe33e7
RS
13507
13508#else /* not USE_X_TOOLKIT */
bdcd49ba
RS
13509#ifdef HAVE_X11R5
13510 XSetLocaleModifiers ("");
13511#endif
7a13e894 13512 dpy = XOpenDisplay (XSTRING (display_name)->data);
3afe33e7 13513#endif /* not USE_X_TOOLKIT */
334208b7 13514
7a13e894
RS
13515 /* Detect failure. */
13516 if (dpy == 0)
60439948
KH
13517 {
13518 UNBLOCK_INPUT;
13519 return 0;
13520 }
7a13e894
RS
13521
13522 /* We have definitely succeeded. Record the new connection. */
13523
13524 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
f04e1297 13525 bzero (dpyinfo, sizeof *dpyinfo);
7a13e894 13526
29b38361
KH
13527#ifdef MULTI_KBOARD
13528 {
13529 struct x_display_info *share;
13530 Lisp_Object tail;
13531
13532 for (share = x_display_list, tail = x_display_name_list; share;
8e713be6
KR
13533 share = share->next, tail = XCDR (tail))
13534 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
29b38361
KH
13535 XSTRING (display_name)->data))
13536 break;
13537 if (share)
13538 dpyinfo->kboard = share->kboard;
13539 else
13540 {
13541 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
13542 init_kboard (dpyinfo->kboard);
59e755be
KH
13543 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
13544 {
13545 char *vendor = ServerVendor (dpy);
9b6ed9f3 13546 UNBLOCK_INPUT;
59e755be
KH
13547 dpyinfo->kboard->Vsystem_key_alist
13548 = call1 (Qvendor_specific_keysyms,
13549 build_string (vendor ? vendor : ""));
9b6ed9f3 13550 BLOCK_INPUT;
59e755be
KH
13551 }
13552
29b38361
KH
13553 dpyinfo->kboard->next_kboard = all_kboards;
13554 all_kboards = dpyinfo->kboard;
0ad5446c
KH
13555 /* Don't let the initial kboard remain current longer than necessary.
13556 That would cause problems if a file loaded on startup tries to
06a2c219 13557 prompt in the mini-buffer. */
0ad5446c
KH
13558 if (current_kboard == initial_kboard)
13559 current_kboard = dpyinfo->kboard;
29b38361
KH
13560 }
13561 dpyinfo->kboard->reference_count++;
13562 }
b9737ad3
KH
13563#endif
13564
7a13e894
RS
13565 /* Put this display on the chain. */
13566 dpyinfo->next = x_display_list;
13567 x_display_list = dpyinfo;
13568
13569 /* Put it on x_display_name_list as well, to keep them parallel. */
13570 x_display_name_list = Fcons (Fcons (display_name, Qnil),
13571 x_display_name_list);
8e713be6 13572 dpyinfo->name_list_element = XCAR (x_display_name_list);
7a13e894
RS
13573
13574 dpyinfo->display = dpy;
dc6f92b8 13575
dc6f92b8 13576#if 0
7a13e894 13577 XSetAfterFunction (x_current_display, x_trace_wire);
c118dd06 13578#endif /* ! 0 */
7a13e894
RS
13579
13580 dpyinfo->x_id_name
fc932ac6
RS
13581 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
13582 + STRING_BYTES (XSTRING (Vsystem_name))
7a13e894
RS
13583 + 2);
13584 sprintf (dpyinfo->x_id_name, "%s@%s",
13585 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
28430d3c
JB
13586
13587 /* Figure out which modifier bits mean what. */
334208b7 13588 x_find_modifier_meanings (dpyinfo);
f451eb13 13589
ab648270 13590 /* Get the scroll bar cursor. */
7a13e894 13591 dpyinfo->vertical_scroll_bar_cursor
334208b7 13592 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
f451eb13 13593
334208b7
RS
13594 xrdb = x_load_resources (dpyinfo->display, xrm_option,
13595 resource_name, EMACS_CLASS);
13596#ifdef HAVE_XRMSETDATABASE
13597 XrmSetDatabase (dpyinfo->display, xrdb);
13598#else
13599 dpyinfo->display->db = xrdb;
13600#endif
547d9db8 13601 /* Put the rdb where we can find it in a way that works on
7a13e894
RS
13602 all versions. */
13603 dpyinfo->xrdb = xrdb;
334208b7
RS
13604
13605 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
13606 DefaultScreen (dpyinfo->display));
5ff67d81 13607 select_visual (dpyinfo);
43bd1b2b 13608 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
334208b7
RS
13609 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
13610 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
13611 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
13612 dpyinfo->grabbed = 0;
13613 dpyinfo->reference_count = 0;
13614 dpyinfo->icon_bitmap_id = -1;
06a2c219 13615 dpyinfo->font_table = NULL;
7a13e894
RS
13616 dpyinfo->n_fonts = 0;
13617 dpyinfo->font_table_size = 0;
13618 dpyinfo->bitmaps = 0;
13619 dpyinfo->bitmaps_size = 0;
13620 dpyinfo->bitmaps_last = 0;
13621 dpyinfo->scratch_cursor_gc = 0;
13622 dpyinfo->mouse_face_mouse_frame = 0;
13623 dpyinfo->mouse_face_deferred_gc = 0;
13624 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
13625 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
06a2c219 13626 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
7a13e894
RS
13627 dpyinfo->mouse_face_window = Qnil;
13628 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
13629 dpyinfo->mouse_face_defer = 0;
0f941935
KH
13630 dpyinfo->x_focus_frame = 0;
13631 dpyinfo->x_focus_event_frame = 0;
13632 dpyinfo->x_highlight_frame = 0;
06a2c219 13633 dpyinfo->image_cache = make_image_cache ();
334208b7 13634
43bd1b2b 13635 /* See if a private colormap is requested. */
5ff67d81
GM
13636 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
13637 {
13638 if (dpyinfo->visual->class == PseudoColor)
13639 {
13640 Lisp_Object value;
13641 value = display_x_get_resource (dpyinfo,
13642 build_string ("privateColormap"),
13643 build_string ("PrivateColormap"),
13644 Qnil, Qnil);
13645 if (STRINGP (value)
13646 && (!strcmp (XSTRING (value)->data, "true")
13647 || !strcmp (XSTRING (value)->data, "on")))
13648 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
13649 }
43bd1b2b 13650 }
5ff67d81
GM
13651 else
13652 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
13653 dpyinfo->visual, AllocNone);
43bd1b2b 13654
06a2c219
GM
13655 {
13656 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
13657 double pixels = DisplayHeight (dpyinfo->display, screen_number);
13658 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
13659 dpyinfo->resy = pixels * 25.4 / mm;
13660 pixels = DisplayWidth (dpyinfo->display, screen_number);
13661 mm = DisplayWidthMM (dpyinfo->display, screen_number);
13662 dpyinfo->resx = pixels * 25.4 / mm;
13663 }
13664
334208b7
RS
13665 dpyinfo->Xatom_wm_protocols
13666 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
13667 dpyinfo->Xatom_wm_take_focus
13668 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
13669 dpyinfo->Xatom_wm_save_yourself
13670 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
13671 dpyinfo->Xatom_wm_delete_window
13672 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
13673 dpyinfo->Xatom_wm_change_state
13674 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
13675 dpyinfo->Xatom_wm_configure_denied
13676 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
13677 dpyinfo->Xatom_wm_window_moved
13678 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
13679 dpyinfo->Xatom_editres
13680 = XInternAtom (dpyinfo->display, "Editres", False);
13681 dpyinfo->Xatom_CLIPBOARD
13682 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
13683 dpyinfo->Xatom_TIMESTAMP
13684 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
13685 dpyinfo->Xatom_TEXT
13686 = XInternAtom (dpyinfo->display, "TEXT", False);
dc43ef94
KH
13687 dpyinfo->Xatom_COMPOUND_TEXT
13688 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
334208b7
RS
13689 dpyinfo->Xatom_DELETE
13690 = XInternAtom (dpyinfo->display, "DELETE", False);
13691 dpyinfo->Xatom_MULTIPLE
13692 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
13693 dpyinfo->Xatom_INCR
13694 = XInternAtom (dpyinfo->display, "INCR", False);
13695 dpyinfo->Xatom_EMACS_TMP
13696 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
13697 dpyinfo->Xatom_TARGETS
13698 = XInternAtom (dpyinfo->display, "TARGETS", False);
13699 dpyinfo->Xatom_NULL
13700 = XInternAtom (dpyinfo->display, "NULL", False);
13701 dpyinfo->Xatom_ATOM_PAIR
13702 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
dc43ef94
KH
13703 /* For properties of font. */
13704 dpyinfo->Xatom_PIXEL_SIZE
13705 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
13706 dpyinfo->Xatom_MULE_BASELINE_OFFSET
13707 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
13708 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
13709 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
f78798df
KH
13710 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
13711 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
334208b7 13712
06a2c219
GM
13713 /* Ghostscript support. */
13714 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
13715 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
13716
13717 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
13718 False);
13719
547d9db8
KH
13720 dpyinfo->cut_buffers_initialized = 0;
13721
334208b7
RS
13722 connection = ConnectionNumber (dpyinfo->display);
13723 dpyinfo->connection = connection;
13724
dc43ef94 13725 {
5d7cc324
RS
13726 char null_bits[1];
13727
13728 null_bits[0] = 0x00;
dc43ef94
KH
13729
13730 dpyinfo->null_pixel
13731 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13732 null_bits, 1, 1, (long) 0, (long) 0,
13733 1);
13734 }
13735
06a2c219
GM
13736 {
13737 extern int gray_bitmap_width, gray_bitmap_height;
10659c77 13738 extern char *gray_bitmap_bits;
06a2c219
GM
13739 dpyinfo->gray
13740 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13741 gray_bitmap_bits,
13742 gray_bitmap_width, gray_bitmap_height,
13743 (unsigned long) 1, (unsigned long) 0, 1);
13744 }
13745
f5d11644
GM
13746#ifdef HAVE_X_I18N
13747 xim_initialize (dpyinfo, resource_name);
13748#endif
13749
87485d6f
MW
13750#ifdef subprocesses
13751 /* This is only needed for distinguishing keyboard and process input. */
334208b7 13752 if (connection != 0)
7a13e894 13753 add_keyboard_wait_descriptor (connection);
87485d6f 13754#endif
6d4238f3 13755
041b69ac 13756#ifndef F_SETOWN_BUG
dc6f92b8 13757#ifdef F_SETOWN
dc6f92b8 13758#ifdef F_SETOWN_SOCK_NEG
61c3ce62 13759 /* stdin is a socket here */
334208b7 13760 fcntl (connection, F_SETOWN, -getpid ());
c118dd06 13761#else /* ! defined (F_SETOWN_SOCK_NEG) */
334208b7 13762 fcntl (connection, F_SETOWN, getpid ());
c118dd06
JB
13763#endif /* ! defined (F_SETOWN_SOCK_NEG) */
13764#endif /* ! defined (F_SETOWN) */
041b69ac 13765#endif /* F_SETOWN_BUG */
dc6f92b8
JB
13766
13767#ifdef SIGIO
eee20f6a
KH
13768 if (interrupt_input)
13769 init_sigio (connection);
c118dd06 13770#endif /* ! defined (SIGIO) */
dc6f92b8 13771
51b592fb 13772#ifdef USE_LUCID
f8c39f51 13773#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
51b592fb
RS
13774 /* Make sure that we have a valid font for dialog boxes
13775 so that Xt does not crash. */
13776 {
13777 Display *dpy = dpyinfo->display;
13778 XrmValue d, fr, to;
13779 Font font;
e99db5a1 13780 int count;
51b592fb
RS
13781
13782 d.addr = (XPointer)&dpy;
13783 d.size = sizeof (Display *);
13784 fr.addr = XtDefaultFont;
13785 fr.size = sizeof (XtDefaultFont);
13786 to.size = sizeof (Font *);
13787 to.addr = (XPointer)&font;
e99db5a1 13788 count = x_catch_errors (dpy);
51b592fb
RS
13789 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
13790 abort ();
13791 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
13792 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
e99db5a1 13793 x_uncatch_errors (dpy, count);
51b592fb
RS
13794 }
13795#endif
f8c39f51 13796#endif
51b592fb 13797
34e23e5a
GM
13798 /* See if we should run in synchronous mode. This is useful
13799 for debugging X code. */
13800 {
13801 Lisp_Object value;
13802 value = display_x_get_resource (dpyinfo,
13803 build_string ("synchronous"),
13804 build_string ("Synchronous"),
13805 Qnil, Qnil);
13806 if (STRINGP (value)
13807 && (!strcmp (XSTRING (value)->data, "true")
13808 || !strcmp (XSTRING (value)->data, "on")))
13809 XSynchronize (dpyinfo->display, True);
13810 }
13811
60439948
KH
13812 UNBLOCK_INPUT;
13813
7a13e894
RS
13814 return dpyinfo;
13815}
13816\f
13817/* Get rid of display DPYINFO, assuming all frames are already gone,
13818 and without sending any more commands to the X server. */
dc6f92b8 13819
7a13e894
RS
13820void
13821x_delete_display (dpyinfo)
13822 struct x_display_info *dpyinfo;
13823{
13824 delete_keyboard_wait_descriptor (dpyinfo->connection);
13825
13826 /* Discard this display from x_display_name_list and x_display_list.
13827 We can't use Fdelq because that can quit. */
13828 if (! NILP (x_display_name_list)
8e713be6
KR
13829 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
13830 x_display_name_list = XCDR (x_display_name_list);
7a13e894
RS
13831 else
13832 {
13833 Lisp_Object tail;
13834
13835 tail = x_display_name_list;
8e713be6 13836 while (CONSP (tail) && CONSP (XCDR (tail)))
7a13e894 13837 {
bffcfca9 13838 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
7a13e894 13839 {
8e713be6 13840 XCDR (tail) = XCDR (XCDR (tail));
7a13e894
RS
13841 break;
13842 }
8e713be6 13843 tail = XCDR (tail);
7a13e894
RS
13844 }
13845 }
13846
9bda743f
GM
13847 if (next_noop_dpyinfo == dpyinfo)
13848 next_noop_dpyinfo = dpyinfo->next;
13849
7a13e894
RS
13850 if (x_display_list == dpyinfo)
13851 x_display_list = dpyinfo->next;
7f9c7f94
RS
13852 else
13853 {
13854 struct x_display_info *tail;
7a13e894 13855
7f9c7f94
RS
13856 for (tail = x_display_list; tail; tail = tail->next)
13857 if (tail->next == dpyinfo)
13858 tail->next = tail->next->next;
13859 }
7a13e894 13860
0d777288
RS
13861#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
13862#ifndef AIX /* On AIX, XCloseDisplay calls this. */
7f9c7f94
RS
13863 XrmDestroyDatabase (dpyinfo->xrdb);
13864#endif
0d777288 13865#endif
29b38361
KH
13866#ifdef MULTI_KBOARD
13867 if (--dpyinfo->kboard->reference_count == 0)
39f79001 13868 delete_kboard (dpyinfo->kboard);
b9737ad3 13869#endif
f5d11644
GM
13870#ifdef HAVE_X_I18N
13871 if (dpyinfo->xim)
13872 xim_close_dpy (dpyinfo);
13873#endif
13874
b9737ad3
KH
13875 xfree (dpyinfo->font_table);
13876 xfree (dpyinfo->x_id_name);
f04e1297 13877 xfree (dpyinfo->color_cells);
b9737ad3 13878 xfree (dpyinfo);
7a13e894 13879}
f04e1297 13880
7a13e894
RS
13881\f
13882/* Set up use of X before we make the first connection. */
13883
06a2c219
GM
13884static struct redisplay_interface x_redisplay_interface =
13885{
13886 x_produce_glyphs,
13887 x_write_glyphs,
13888 x_insert_glyphs,
13889 x_clear_end_of_line,
13890 x_scroll_run,
13891 x_after_update_window_line,
13892 x_update_window_begin,
13893 x_update_window_end,
13894 XTcursor_to,
13895 x_flush,
71b8321e 13896 x_clear_mouse_face,
66ac4b0e
GM
13897 x_get_glyph_overhangs,
13898 x_fix_overlapping_area
06a2c219
GM
13899};
13900
dfcf069d 13901void
7a13e894
RS
13902x_initialize ()
13903{
06a2c219
GM
13904 rif = &x_redisplay_interface;
13905
13906 clear_frame_hook = x_clear_frame;
13907 ins_del_lines_hook = x_ins_del_lines;
13908 change_line_highlight_hook = x_change_line_highlight;
13909 delete_glyphs_hook = x_delete_glyphs;
dc6f92b8
JB
13910 ring_bell_hook = XTring_bell;
13911 reset_terminal_modes_hook = XTreset_terminal_modes;
13912 set_terminal_modes_hook = XTset_terminal_modes;
06a2c219
GM
13913 update_begin_hook = x_update_begin;
13914 update_end_hook = x_update_end;
dc6f92b8
JB
13915 set_terminal_window_hook = XTset_terminal_window;
13916 read_socket_hook = XTread_socket;
b8009dd1 13917 frame_up_to_date_hook = XTframe_up_to_date;
dc6f92b8 13918 reassert_line_highlight_hook = XTreassert_line_highlight;
90e65f07 13919 mouse_position_hook = XTmouse_position;
f451eb13 13920 frame_rehighlight_hook = XTframe_rehighlight;
dbc4e1c1 13921 frame_raise_lower_hook = XTframe_raise_lower;
ab648270
JB
13922 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13923 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13924 redeem_scroll_bar_hook = XTredeem_scroll_bar;
13925 judge_scroll_bars_hook = XTjudge_scroll_bars;
06a2c219 13926 estimate_mode_line_height_hook = x_estimate_mode_line_height;
58769bee 13927
f676886a 13928 scroll_region_ok = 1; /* we'll scroll partial frames */
dc6f92b8
JB
13929 char_ins_del_ok = 0; /* just as fast to write the line */
13930 line_ins_del_ok = 1; /* we'll just blt 'em */
13931 fast_clear_end_of_line = 1; /* X does this well */
58769bee 13932 memory_below_frame = 0; /* we don't remember what scrolls
dc6f92b8
JB
13933 off the bottom */
13934 baud_rate = 19200;
13935
7a13e894 13936 x_noop_count = 0;
9ea173e8 13937 last_tool_bar_item = -1;
06a2c219
GM
13938 any_help_event_p = 0;
13939
b30b24cb
RS
13940 /* Try to use interrupt input; if we can't, then start polling. */
13941 Fset_input_mode (Qt, Qnil, Qt, Qnil);
13942
7f9c7f94
RS
13943#ifdef USE_X_TOOLKIT
13944 XtToolkitInitialize ();
13945 Xt_app_con = XtCreateApplicationContext ();
665881ad 13946 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
bffcfca9
GM
13947
13948 /* Install an asynchronous timer that processes Xt timeout events
13949 every 0.1s. This is necessary because some widget sets use
13950 timeouts internally, for example the LessTif menu bar, or the
13951 Xaw3d scroll bar. When Xt timouts aren't processed, these
13952 widgets don't behave normally. */
13953 {
13954 EMACS_TIME interval;
13955 EMACS_SET_SECS_USECS (interval, 0, 100000);
13956 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
13957 }
db74249b 13958#endif
bffcfca9 13959
eccc05db 13960#ifdef USE_TOOLKIT_SCROLL_BARS
ec18280f
SM
13961 xaw3d_arrow_scroll = False;
13962 xaw3d_pick_top = True;
7f9c7f94
RS
13963#endif
13964
58769bee 13965 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 13966 original error handler. */
e99db5a1 13967 XSetErrorHandler (x_error_handler);
334208b7 13968 XSetIOErrorHandler (x_io_error_quitter);
dc6f92b8 13969
06a2c219 13970 /* Disable Window Change signals; they are handled by X events. */
dc6f92b8
JB
13971#ifdef SIGWINCH
13972 signal (SIGWINCH, SIG_DFL);
c118dd06 13973#endif /* ! defined (SIGWINCH) */
dc6f92b8 13974
92e2441b 13975 signal (SIGPIPE, x_connection_signal);
dc6f92b8 13976}
55123275 13977
06a2c219 13978
55123275
JB
13979void
13980syms_of_xterm ()
13981{
e99db5a1
RS
13982 staticpro (&x_error_message_string);
13983 x_error_message_string = Qnil;
13984
7a13e894
RS
13985 staticpro (&x_display_name_list);
13986 x_display_name_list = Qnil;
334208b7 13987
ab648270 13988 staticpro (&last_mouse_scroll_bar);
e53cb100 13989 last_mouse_scroll_bar = Qnil;
59e755be
KH
13990
13991 staticpro (&Qvendor_specific_keysyms);
13992 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
2237cac9
RS
13993
13994 staticpro (&last_mouse_press_frame);
13995 last_mouse_press_frame = Qnil;
06a2c219 13996
06a2c219 13997 help_echo = Qnil;
be010514
GM
13998 staticpro (&help_echo);
13999 help_echo_object = Qnil;
14000 staticpro (&help_echo_object);
7cea38bc
GM
14001 help_echo_window = Qnil;
14002 staticpro (&help_echo_window);
06a2c219 14003 previous_help_echo = Qnil;
be010514
GM
14004 staticpro (&previous_help_echo);
14005 help_echo_pos = -1;
06a2c219
GM
14006
14007 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
14008 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
14009For example, if a block cursor is over a tab, it will be drawn as\n\
14010wide as that tab on the display.");
14011 x_stretch_cursor_p = 0;
14012
5bf04520
GM
14013 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
14014 "What X toolkit scroll bars Emacs uses.\n\
14015A value of nil means Emacs doesn't use X toolkit scroll bars.\n\
14016Otherwise, value is a symbol describing the X toolkit.");
eccc05db 14017#ifdef USE_TOOLKIT_SCROLL_BARS
5bf04520
GM
14018#ifdef USE_MOTIF
14019 Vx_toolkit_scroll_bars = intern ("motif");
14020#elif defined HAVE_XAW3D
14021 Vx_toolkit_scroll_bars = intern ("xaw3d");
14022#else
14023 Vx_toolkit_scroll_bars = intern ("xaw");
14024#endif
06a2c219 14025#else
5bf04520 14026 Vx_toolkit_scroll_bars = Qnil;
06a2c219
GM
14027#endif
14028
06a2c219
GM
14029 staticpro (&last_mouse_motion_frame);
14030 last_mouse_motion_frame = Qnil;
55123275 14031}
6cf0ae86
RS
14032
14033#endif /* not HAVE_X_WINDOWS */