*** empty log message ***
[bpt/emacs.git] / src / xterm.c
CommitLineData
dc6f92b8 1/* X Communication module for terminals which understand the X protocol.
1c7e22fd 2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000
06a2c219 3 Free Software Foundation, Inc.
dc6f92b8
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
4746118a 9the Free Software Foundation; either version 2, or (at your option)
dc6f92b8
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
dc6f92b8 21
06a2c219 22/* New display code by Gerd Moellmann <gerd@gnu.org>. */
3afe33e7
RS
23/* Xt features made by Fred Pierresteguy. */
24
68c45bf0
PE
25#include <config.h>
26
039440c4 27/* On 4.3 these lose if they come after xterm.h. */
039440c4 28/* Putting these at the beginning seems to be standard for other .c files. */
039440c4
RS
29#include <signal.h>
30
4846819e
RS
31#include <stdio.h>
32
dc6f92b8
JB
33#ifdef HAVE_X_WINDOWS
34
35#include "lisp.h"
9ac0d9e0 36#include "blockinput.h"
dc6f92b8 37
ae79c227
AS
38/* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40#include "syssignal.h"
41
dc6f92b8
JB
42/* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44#include "xterm.h"
f451eb13 45#include <X11/cursorfont.h>
dc6f92b8
JB
46
47/* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49#ifndef makedev
50#include <sys/types.h>
c118dd06 51#endif /* makedev */
dc6f92b8 52
6df54671 53#ifdef BSD_SYSTEM
dc6f92b8 54#include <sys/ioctl.h>
6df54671 55#endif /* ! defined (BSD_SYSTEM) */
dc6f92b8 56
2d368234 57#include "systty.h"
3a2712f9 58#include "systime.h"
dc6f92b8 59
b8009dd1 60#ifndef INCLUDED_FCNTL
dc6f92b8 61#include <fcntl.h>
b8009dd1 62#endif
dc6f92b8
JB
63#include <ctype.h>
64#include <errno.h>
65#include <setjmp.h>
66#include <sys/stat.h>
a0a7635f
RS
67/* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
68/* #include <sys/param.h> */
dc6f92b8 69
dc43ef94 70#include "charset.h"
379b5ac0 71#include "coding.h"
dc43ef94 72#include "ccl.h"
7a13e894 73#include "frame.h"
dc6f92b8 74#include "dispextern.h"
ee569018 75#include "fontset.h"
dc6f92b8
JB
76#include "termhooks.h"
77#include "termopts.h"
78#include "termchar.h"
79#if 0
80#include "sink.h"
81#include "sinkmask.h"
c118dd06 82#endif /* ! 0 */
dc6f92b8 83#include "gnu.h"
dc6f92b8 84#include "disptab.h"
dc6f92b8 85#include "buffer.h"
f451eb13 86#include "window.h"
3b2fa4e6 87#include "keyboard.h"
bde7c500 88#include "intervals.h"
dfcf069d 89#include "process.h"
bffcfca9 90#include "atimer.h"
dc6f92b8 91
d2bd6bc4
RS
92#ifdef USE_X_TOOLKIT
93#include <X11/Shell.h>
94#endif
95
06a2c219
GM
96#ifdef HAVE_SYS_TIME_H
97#include <sys/time.h>
98#endif
99#ifdef HAVE_UNISTD_H
100#include <unistd.h>
101#endif
102
3afe33e7 103#ifdef USE_X_TOOLKIT
06a2c219 104
952291d9
GM
105extern void free_frame_menubar P_ ((struct frame *));
106extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
107 int));
06a2c219 108
0fdff6bb
RS
109#if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
110#define HACK_EDITRES
111extern void _XEditResCheckMessages ();
112#endif /* not NO_EDITRES */
06a2c219
GM
113
114/* Include toolkit specific headers for the scroll bar widget. */
115
116#ifdef USE_TOOLKIT_SCROLL_BARS
117#if defined USE_MOTIF
118#include <Xm/Xm.h> /* for LESSTIF_VERSION */
119#include <Xm/ScrollBar.h>
120#include <Xm/ScrollBarP.h>
ec18280f
SM
121#else /* !USE_MOTIF i.e. use Xaw */
122
123#ifdef HAVE_XAW3D
06a2c219 124#include <X11/Xaw3d/Simple.h>
06a2c219
GM
125#include <X11/Xaw3d/Scrollbar.h>
126#define ARROW_SCROLLBAR
127#include <X11/Xaw3d/ScrollbarP.h>
ec18280f
SM
128#else /* !HAVE_XAW3D */
129#include <X11/Xaw/Simple.h>
130#include <X11/Xaw/Scrollbar.h>
131#endif /* !HAVE_XAW3D */
132#ifndef XtNpickTop
133#define XtNpickTop "pickTop"
134#endif /* !XtNpickTop */
135#endif /* !USE_MOTIF */
06a2c219
GM
136#endif /* USE_TOOLKIT_SCROLL_BARS */
137
3afe33e7
RS
138#endif /* USE_X_TOOLKIT */
139
b849c413
RS
140#ifndef USE_X_TOOLKIT
141#define x_any_window_to_frame x_window_to_frame
5627c40e 142#define x_top_window_to_frame x_window_to_frame
b849c413
RS
143#endif
144
546e6d5b 145#ifdef USE_X_TOOLKIT
d067ea8b 146#include "widget.h"
546e6d5b
RS
147#ifndef XtNinitialState
148#define XtNinitialState "initialState"
149#endif
150#endif
151
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
06a2c219
GM
233/* Non-zero means Emacs uses toolkit scroll bars. */
234
235int x_toolkit_scroll_bars_p;
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);
66ac4b0e 1507 it->descent = it->phys_descent = img->height + 2 * img->margin - it->ascent;
06a2c219
GM
1508 it->pixel_width = img->width + 2 * img->margin;
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. */
3847 if (s->img->margin)
3848 {
3849 x += s->img->margin;
3850 y += s->img->margin;
3851 }
3852
3853 if (s->img->pixmap)
3854 {
3855 if (s->img->mask)
3856 {
3857 /* We can't set both a clip mask and use XSetClipRectangles
3858 because the latter also sets a clip mask. We also can't
3859 trust on the shape extension to be available
3860 (XShapeCombineRegion). So, compute the rectangle to draw
3861 manually. */
3862 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3863 | GCFunction);
3864 XGCValues xgcv;
3865 XRectangle clip_rect, image_rect, r;
3866
3867 xgcv.clip_mask = s->img->mask;
3868 xgcv.clip_x_origin = x;
3869 xgcv.clip_y_origin = y;
3870 xgcv.function = GXcopy;
3871 XChangeGC (s->display, s->gc, mask, &xgcv);
3872
3873 x_get_glyph_string_clip_rect (s, &clip_rect);
3874 image_rect.x = x;
3875 image_rect.y = y;
3876 image_rect.width = s->img->width;
3877 image_rect.height = s->img->height;
3878 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3879 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3880 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3881 }
3882 else
3883 {
49ad1d99
GM
3884 unsigned long mask = GCClipXOrigin | GCClipYOrigin | GCFunction;
3885 XGCValues xgcv;
3886 XRectangle clip_rect, image_rect, r;
3887
3888 x_get_glyph_string_clip_rect (s, &clip_rect);
3889 image_rect.x = x;
3890 image_rect.y = y;
3891 image_rect.width = s->img->width;
3892 image_rect.height = s->img->height;
3893 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3894 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3895 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
06a2c219
GM
3896
3897 /* When the image has a mask, we can expect that at
3898 least part of a mouse highlight or a block cursor will
3899 be visible. If the image doesn't have a mask, make
3900 a block cursor visible by drawing a rectangle around
3901 the image. I believe it's looking better if we do
3902 nothing here for mouse-face. */
3903 if (s->hl == DRAW_CURSOR)
3904 XDrawRectangle (s->display, s->window, s->gc, x, y,
3905 s->img->width - 1, s->img->height - 1);
3906 }
3907 }
3908 else
3909 /* Draw a rectangle if image could not be loaded. */
3910 XDrawRectangle (s->display, s->window, s->gc, x, y,
3911 s->img->width - 1, s->img->height - 1);
3912}
3913
3914
3915/* Draw a relief around the image glyph string S. */
3916
3917static void
3918x_draw_image_relief (s)
3919 struct glyph_string *s;
3920{
3921 int x0, y0, x1, y1, thick, raised_p;
3922 XRectangle r;
3923 int x;
95af8492 3924 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
3925
3926 /* If first glyph of S has a left box line, start drawing it to the
3927 right of that line. */
3928 if (s->face->box != FACE_NO_BOX
3929 && s->first_glyph->left_box_line_p)
3930 x = s->x + s->face->box_line_width;
3931 else
3932 x = s->x;
3933
3934 /* If there is a margin around the image, adjust x- and y-position
3935 by that margin. */
3936 if (s->img->margin)
3937 {
3938 x += s->img->margin;
3939 y += s->img->margin;
3940 }
3941
3942 if (s->hl == DRAW_IMAGE_SUNKEN
3943 || s->hl == DRAW_IMAGE_RAISED)
3944 {
9ea173e8 3945 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
06a2c219
GM
3946 raised_p = s->hl == DRAW_IMAGE_RAISED;
3947 }
3948 else
3949 {
3950 thick = abs (s->img->relief);
3951 raised_p = s->img->relief > 0;
3952 }
3953
3954 x0 = x - thick;
3955 y0 = y - thick;
3956 x1 = x + s->img->width + thick - 1;
3957 y1 = y + s->img->height + thick - 1;
3958
3959 x_setup_relief_colors (s);
3960 x_get_glyph_string_clip_rect (s, &r);
3961 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3962}
3963
3964
3965/* Draw the foreground of image glyph string S to PIXMAP. */
3966
3967static void
3968x_draw_image_foreground_1 (s, pixmap)
3969 struct glyph_string *s;
3970 Pixmap pixmap;
3971{
3972 int x;
95af8492 3973 int y = s->ybase - s->y - image_ascent (s->img, s->face);
06a2c219
GM
3974
3975 /* If first glyph of S has a left box line, start drawing it to the
3976 right of that line. */
3977 if (s->face->box != FACE_NO_BOX
3978 && s->first_glyph->left_box_line_p)
3979 x = s->face->box_line_width;
3980 else
3981 x = 0;
3982
3983 /* If there is a margin around the image, adjust x- and y-position
3984 by that margin. */
3985 if (s->img->margin)
3986 {
3987 x += s->img->margin;
3988 y += s->img->margin;
3989 }
dc43ef94 3990
06a2c219
GM
3991 if (s->img->pixmap)
3992 {
3993 if (s->img->mask)
3994 {
3995 /* We can't set both a clip mask and use XSetClipRectangles
3996 because the latter also sets a clip mask. We also can't
3997 trust on the shape extension to be available
3998 (XShapeCombineRegion). So, compute the rectangle to draw
3999 manually. */
4000 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4001 | GCFunction);
4002 XGCValues xgcv;
4003
4004 xgcv.clip_mask = s->img->mask;
4005 xgcv.clip_x_origin = x;
4006 xgcv.clip_y_origin = y;
4007 xgcv.function = GXcopy;
4008 XChangeGC (s->display, s->gc, mask, &xgcv);
4009
4010 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4011 0, 0, s->img->width, s->img->height, x, y);
4012 XSetClipMask (s->display, s->gc, None);
4013 }
4014 else
4015 {
4016 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4017 0, 0, s->img->width, s->img->height, x, y);
4018
4019 /* When the image has a mask, we can expect that at
4020 least part of a mouse highlight or a block cursor will
4021 be visible. If the image doesn't have a mask, make
4022 a block cursor visible by drawing a rectangle around
4023 the image. I believe it's looking better if we do
4024 nothing here for mouse-face. */
4025 if (s->hl == DRAW_CURSOR)
4026 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4027 s->img->width - 1, s->img->height - 1);
4028 }
4029 }
4030 else
4031 /* Draw a rectangle if image could not be loaded. */
4032 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4033 s->img->width - 1, s->img->height - 1);
4034}
dc43ef94 4035
990ba854 4036
06a2c219
GM
4037/* Draw part of the background of glyph string S. X, Y, W, and H
4038 give the rectangle to draw. */
a9a5b0a5 4039
06a2c219
GM
4040static void
4041x_draw_glyph_string_bg_rect (s, x, y, w, h)
4042 struct glyph_string *s;
4043 int x, y, w, h;
4044{
4045 if (s->stippled_p)
4046 {
4047 /* Fill background with a stipple pattern. */
4048 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4049 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4050 XSetFillStyle (s->display, s->gc, FillSolid);
4051 }
4052 else
4053 x_clear_glyph_string_rect (s, x, y, w, h);
4054}
07e34cb0 4055
b5210ea7 4056
06a2c219 4057/* Draw image glyph string S.
dc43ef94 4058
06a2c219
GM
4059 s->y
4060 s->x +-------------------------
4061 | s->face->box
4062 |
4063 | +-------------------------
4064 | | s->img->margin
4065 | |
4066 | | +-------------------
4067 | | | the image
dc43ef94 4068
06a2c219 4069 */
dc43ef94 4070
06a2c219
GM
4071static void
4072x_draw_image_glyph_string (s)
4073 struct glyph_string *s;
4074{
4075 int x, y;
4076 int box_line_width = s->face->box_line_width;
4077 int margin = s->img->margin;
4078 int height;
4079 Pixmap pixmap = None;
4080
4081 height = s->height - 2 * box_line_width;
4082
4083 /* Fill background with face under the image. Do it only if row is
4084 taller than image or if image has a clip mask to reduce
4085 flickering. */
4086 s->stippled_p = s->face->stipple != 0;
4087 if (height > s->img->height
4088 || margin
4089 || s->img->mask
4090 || s->img->pixmap == 0
4091 || s->width != s->background_width)
4092 {
4093 if (box_line_width && s->first_glyph->left_box_line_p)
4094 x = s->x + box_line_width;
4095 else
4096 x = s->x;
4097
4098 y = s->y + box_line_width;
4099
4100 if (s->img->mask)
4101 {
f9b5db02
GM
4102 /* Create a pixmap as large as the glyph string. Fill it
4103 with the background color. Copy the image to it, using
4104 its mask. Copy the temporary pixmap to the display. */
06a2c219
GM
4105 Screen *screen = FRAME_X_SCREEN (s->f);
4106 int depth = DefaultDepthOfScreen (screen);
4107
4108 /* Create a pixmap as large as the glyph string. */
4109 pixmap = XCreatePixmap (s->display, s->window,
4110 s->background_width,
4111 s->height, depth);
4112
4113 /* Don't clip in the following because we're working on the
4114 pixmap. */
4115 XSetClipMask (s->display, s->gc, None);
4116
4117 /* Fill the pixmap with the background color/stipple. */
4118 if (s->stippled_p)
4119 {
4120 /* Fill background with a stipple pattern. */
4121 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4122 XFillRectangle (s->display, pixmap, s->gc,
4123 0, 0, s->background_width, s->height);
4124 XSetFillStyle (s->display, s->gc, FillSolid);
4125 }
4126 else
4127 {
4128 XGCValues xgcv;
4129 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4130 &xgcv);
4131 XSetForeground (s->display, s->gc, xgcv.background);
4132 XFillRectangle (s->display, pixmap, s->gc,
4133 0, 0, s->background_width, s->height);
4134 XSetForeground (s->display, s->gc, xgcv.foreground);
4135 }
4136 }
4137 else
06a2c219
GM
4138 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4139
4140 s->background_filled_p = 1;
4141 }
dc43ef94 4142
06a2c219
GM
4143 /* Draw the foreground. */
4144 if (pixmap != None)
4145 {
4146 x_draw_image_foreground_1 (s, pixmap);
4147 x_set_glyph_string_clipping (s);
4148 XCopyArea (s->display, pixmap, s->window, s->gc,
4149 0, 0, s->background_width, s->height, s->x, s->y);
4150 XFreePixmap (s->display, pixmap);
4151 }
4152 else
4153 x_draw_image_foreground (s);
b5210ea7 4154
06a2c219
GM
4155 /* If we must draw a relief around the image, do it. */
4156 if (s->img->relief
4157 || s->hl == DRAW_IMAGE_RAISED
4158 || s->hl == DRAW_IMAGE_SUNKEN)
4159 x_draw_image_relief (s);
4160}
8c1a6a84 4161
990ba854 4162
06a2c219 4163/* Draw stretch glyph string S. */
dc43ef94 4164
06a2c219
GM
4165static void
4166x_draw_stretch_glyph_string (s)
4167 struct glyph_string *s;
4168{
4169 xassert (s->first_glyph->type == STRETCH_GLYPH);
4170 s->stippled_p = s->face->stipple != 0;
990ba854 4171
06a2c219
GM
4172 if (s->hl == DRAW_CURSOR
4173 && !x_stretch_cursor_p)
4174 {
4175 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4176 as wide as the stretch glyph. */
4177 int width = min (CANON_X_UNIT (s->f), s->background_width);
990ba854 4178
06a2c219
GM
4179 /* Draw cursor. */
4180 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
0cdd0c9f 4181
06a2c219
GM
4182 /* Clear rest using the GC of the original non-cursor face. */
4183 if (width < s->background_width)
4184 {
4185 GC gc = s->face->gc;
4186 int x = s->x + width, y = s->y;
4187 int w = s->background_width - width, h = s->height;
4188 XRectangle r;
dc43ef94 4189
06a2c219
GM
4190 x_get_glyph_string_clip_rect (s, &r);
4191 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
97210f4e 4192
06a2c219
GM
4193 if (s->face->stipple)
4194 {
4195 /* Fill background with a stipple pattern. */
4196 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4197 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4198 XSetFillStyle (s->display, gc, FillSolid);
4199 }
4200 else
4201 {
4202 XGCValues xgcv;
4203 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4204 XSetForeground (s->display, gc, xgcv.background);
4205 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4206 XSetForeground (s->display, gc, xgcv.foreground);
4207 }
4208 }
4209 }
4210 else
4211 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4212 s->height);
4213
4214 s->background_filled_p = 1;
4215}
4216
4217
4218/* Draw glyph string S. */
4219
4220static void
4221x_draw_glyph_string (s)
4222 struct glyph_string *s;
4223{
4224 /* If S draws into the background of its successor, draw the
4225 background of the successor first so that S can draw into it.
4226 This makes S->next use XDrawString instead of XDrawImageString. */
66ac4b0e 4227 if (s->next && s->right_overhang && !s->for_overlaps_p)
06a2c219
GM
4228 {
4229 xassert (s->next->img == NULL);
4230 x_set_glyph_string_gc (s->next);
4231 x_set_glyph_string_clipping (s->next);
4232 x_draw_glyph_string_background (s->next, 1);
4233 }
97210f4e 4234
06a2c219
GM
4235 /* Set up S->gc, set clipping and draw S. */
4236 x_set_glyph_string_gc (s);
4237 x_set_glyph_string_clipping (s);
4238
4239 switch (s->first_glyph->type)
4240 {
4241 case IMAGE_GLYPH:
4242 x_draw_image_glyph_string (s);
4243 break;
4244
4245 case STRETCH_GLYPH:
4246 x_draw_stretch_glyph_string (s);
4247 break;
4248
4249 case CHAR_GLYPH:
66ac4b0e
GM
4250 if (s->for_overlaps_p)
4251 s->background_filled_p = 1;
4252 else
4253 x_draw_glyph_string_background (s, 0);
06a2c219
GM
4254 x_draw_glyph_string_foreground (s);
4255 break;
4256
b4192550
KH
4257 case COMPOSITE_GLYPH:
4258 if (s->for_overlaps_p || s->gidx > 0)
4259 s->background_filled_p = 1;
4260 else
4261 x_draw_glyph_string_background (s, 1);
4262 x_draw_composite_glyph_string_foreground (s);
4263 break;
4264
06a2c219
GM
4265 default:
4266 abort ();
4267 }
4268
66ac4b0e 4269 if (!s->for_overlaps_p)
06a2c219 4270 {
66ac4b0e
GM
4271 /* Draw underline. */
4272 if (s->face->underline_p)
4273 {
e24e84cc
GM
4274 unsigned long tem, h;
4275 int y;
06a2c219 4276
e24e84cc 4277 /* Get the underline thickness. Default is 1 pixel. */
66ac4b0e
GM
4278 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4279 h = 1;
e24e84cc
GM
4280
4281 /* Get the underline position. This is the recommended
4282 vertical offset in pixels from the baseline to the top of
4283 the underline. This is a signed value according to the
4284 specs, and its default is
4285
4286 ROUND ((maximum descent) / 2), with
4287 ROUND(x) = floor (x + 0.5) */
4288
4289 if (XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
4290 y = s->ybase + (long) tem;
4291 else if (s->face->font)
4292 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4293 else
4294 y = s->height - h;
06a2c219 4295
66ac4b0e 4296 if (s->face->underline_defaulted_p)
e24e84cc
GM
4297 XFillRectangle (s->display, s->window, s->gc,
4298 s->x, y, s->width, h);
66ac4b0e
GM
4299 else
4300 {
4301 XGCValues xgcv;
4302 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4303 XSetForeground (s->display, s->gc, s->face->underline_color);
e24e84cc
GM
4304 XFillRectangle (s->display, s->window, s->gc,
4305 s->x, y, s->width, h);
66ac4b0e
GM
4306 XSetForeground (s->display, s->gc, xgcv.foreground);
4307 }
dc6f92b8 4308 }
07e34cb0 4309
66ac4b0e
GM
4310 /* Draw overline. */
4311 if (s->face->overline_p)
06a2c219 4312 {
66ac4b0e
GM
4313 unsigned long dy = 0, h = 1;
4314
4315 if (s->face->overline_color_defaulted_p)
4316 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4317 s->width, h);
4318 else
4319 {
4320 XGCValues xgcv;
4321 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4322 XSetForeground (s->display, s->gc, s->face->overline_color);
4323 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4324 s->width, h);
4325 XSetForeground (s->display, s->gc, xgcv.foreground);
4326 }
06a2c219 4327 }
06a2c219 4328
66ac4b0e
GM
4329 /* Draw strike-through. */
4330 if (s->face->strike_through_p)
06a2c219 4331 {
66ac4b0e
GM
4332 unsigned long h = 1;
4333 unsigned long dy = (s->height - h) / 2;
4334
4335 if (s->face->strike_through_color_defaulted_p)
4336 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4337 s->width, h);
4338 else
4339 {
4340 XGCValues xgcv;
4341 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4342 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4343 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4344 s->width, h);
4345 XSetForeground (s->display, s->gc, xgcv.foreground);
4346 }
06a2c219 4347 }
06a2c219 4348
66ac4b0e
GM
4349 /* Draw relief. */
4350 if (s->face->box != FACE_NO_BOX)
4351 x_draw_glyph_string_box (s);
4352 }
06a2c219
GM
4353
4354 /* Reset clipping. */
4355 XSetClipMask (s->display, s->gc, None);
dc6f92b8 4356}
07e34cb0 4357
06a2c219 4358
b4192550
KH
4359static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4360 struct face **, int));
06a2c219 4361
06a2c219 4362
209f68d9
GM
4363/* Fill glyph string S with composition components specified by S->cmp.
4364
b4192550
KH
4365 FACES is an array of faces for all components of this composition.
4366 S->gidx is the index of the first component for S.
4367 OVERLAPS_P non-zero means S should draw the foreground only, and
209f68d9 4368 use its physical height for clipping.
06a2c219 4369
b4192550 4370 Value is the index of a component not in S. */
07e34cb0 4371
b4192550
KH
4372static int
4373x_fill_composite_glyph_string (s, faces, overlaps_p)
06a2c219 4374 struct glyph_string *s;
b4192550 4375 struct face **faces;
66ac4b0e 4376 int overlaps_p;
07e34cb0 4377{
b4192550 4378 int i;
06a2c219 4379
b4192550 4380 xassert (s);
06a2c219 4381
b4192550 4382 s->for_overlaps_p = overlaps_p;
06a2c219 4383
b4192550
KH
4384 s->face = faces[s->gidx];
4385 s->font = s->face->font;
4386 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
06a2c219 4387
b4192550
KH
4388 /* For all glyphs of this composition, starting at the offset
4389 S->gidx, until we reach the end of the definition or encounter a
4390 glyph that requires the different face, add it to S. */
4391 ++s->nchars;
4392 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4393 ++s->nchars;
06a2c219 4394
b4192550
KH
4395 /* All glyph strings for the same composition has the same width,
4396 i.e. the width set for the first component of the composition. */
06a2c219 4397
06a2c219
GM
4398 s->width = s->first_glyph->pixel_width;
4399
4400 /* If the specified font could not be loaded, use the frame's
4401 default font, but record the fact that we couldn't load it in
4402 the glyph string so that we can draw rectangles for the
4403 characters of the glyph string. */
4404 if (s->font == NULL)
4405 {
4406 s->font_not_found_p = 1;
4407 s->font = FRAME_FONT (s->f);
4408 }
4409
4410 /* Adjust base line for subscript/superscript text. */
4411 s->ybase += s->first_glyph->voffset;
4412
4413 xassert (s->face && s->face->gc);
4414
4415 /* This glyph string must always be drawn with 16-bit functions. */
4416 s->two_byte_p = 1;
b4192550
KH
4417
4418 return s->gidx + s->nchars;
06a2c219
GM
4419}
4420
4421
209f68d9
GM
4422/* Fill glyph string S from a sequence of character glyphs.
4423
06a2c219 4424 FACE_ID is the face id of the string. START is the index of the
66ac4b0e
GM
4425 first glyph to consider, END is the index of the last + 1.
4426 OVERLAPS_P non-zero means S should draw the foreground only, and
209f68d9 4427 use its physical height for clipping.
66ac4b0e
GM
4428
4429 Value is the index of the first glyph not in S. */
06a2c219
GM
4430
4431static int
66ac4b0e 4432x_fill_glyph_string (s, face_id, start, end, overlaps_p)
06a2c219
GM
4433 struct glyph_string *s;
4434 int face_id;
66ac4b0e 4435 int start, end, overlaps_p;
06a2c219
GM
4436{
4437 struct glyph *glyph, *last;
4438 int voffset;
ee569018 4439 int glyph_not_available_p;
06a2c219 4440
06a2c219
GM
4441 xassert (s->f == XFRAME (s->w->frame));
4442 xassert (s->nchars == 0);
4443 xassert (start >= 0 && end > start);
4444
66ac4b0e 4445 s->for_overlaps_p = overlaps_p,
06a2c219
GM
4446 glyph = s->row->glyphs[s->area] + start;
4447 last = s->row->glyphs[s->area] + end;
4448 voffset = glyph->voffset;
4449
ee569018
KH
4450 glyph_not_available_p = glyph->glyph_not_available_p;
4451
06a2c219
GM
4452 while (glyph < last
4453 && glyph->type == CHAR_GLYPH
4454 && glyph->voffset == voffset
ee569018
KH
4455 /* Same face id implies same font, nowadays. */
4456 && glyph->face_id == face_id
4457 && glyph->glyph_not_available_p == glyph_not_available_p)
06a2c219 4458 {
ee569018
KH
4459 int two_byte_p;
4460
06a2c219 4461 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
ee569018
KH
4462 s->char2b + s->nchars,
4463 &two_byte_p);
4464 s->two_byte_p = two_byte_p;
06a2c219
GM
4465 ++s->nchars;
4466 xassert (s->nchars <= end - start);
4467 s->width += glyph->pixel_width;
4468 ++glyph;
4469 }
4470
4471 s->font = s->face->font;
4472 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4473
4474 /* If the specified font could not be loaded, use the frame's font,
4475 but record the fact that we couldn't load it in
4476 S->font_not_found_p so that we can draw rectangles for the
4477 characters of the glyph string. */
ee569018 4478 if (s->font == NULL || glyph_not_available_p)
06a2c219
GM
4479 {
4480 s->font_not_found_p = 1;
4481 s->font = FRAME_FONT (s->f);
4482 }
4483
4484 /* Adjust base line for subscript/superscript text. */
4485 s->ybase += voffset;
66ac4b0e 4486
06a2c219
GM
4487 xassert (s->face && s->face->gc);
4488 return glyph - s->row->glyphs[s->area];
07e34cb0 4489}
dc6f92b8 4490
06a2c219
GM
4491
4492/* Fill glyph string S from image glyph S->first_glyph. */
dc6f92b8 4493
dfcf069d 4494static void
06a2c219
GM
4495x_fill_image_glyph_string (s)
4496 struct glyph_string *s;
4497{
4498 xassert (s->first_glyph->type == IMAGE_GLYPH);
43d120d8 4499 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
06a2c219 4500 xassert (s->img);
43d120d8 4501 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
06a2c219
GM
4502 s->font = s->face->font;
4503 s->width = s->first_glyph->pixel_width;
4504
4505 /* Adjust base line for subscript/superscript text. */
4506 s->ybase += s->first_glyph->voffset;
4507}
4508
4509
209f68d9 4510/* Fill glyph string S from a sequence of stretch glyphs.
06a2c219 4511
209f68d9
GM
4512 ROW is the glyph row in which the glyphs are found, AREA is the
4513 area within the row. START is the index of the first glyph to
4514 consider, END is the index of the last + 1.
4515
4516 Value is the index of the first glyph not in S. */
4517
4518static int
4519x_fill_stretch_glyph_string (s, row, area, start, end)
06a2c219 4520 struct glyph_string *s;
209f68d9
GM
4521 struct glyph_row *row;
4522 enum glyph_row_area area;
4523 int start, end;
06a2c219 4524{
209f68d9
GM
4525 struct glyph *glyph, *last;
4526 int voffset, face_id;
4527
06a2c219 4528 xassert (s->first_glyph->type == STRETCH_GLYPH);
209f68d9
GM
4529
4530 glyph = s->row->glyphs[s->area] + start;
4531 last = s->row->glyphs[s->area] + end;
4532 face_id = glyph->face_id;
4533 s->face = FACE_FROM_ID (s->f, face_id);
06a2c219 4534 s->font = s->face->font;
209f68d9
GM
4535 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4536 s->width = glyph->pixel_width;
4537 voffset = glyph->voffset;
4538
4539 for (++glyph;
4540 (glyph < last
4541 && glyph->type == STRETCH_GLYPH
4542 && glyph->voffset == voffset
4543 && glyph->face_id == face_id);
4544 ++glyph)
4545 s->width += glyph->pixel_width;
06a2c219
GM
4546
4547 /* Adjust base line for subscript/superscript text. */
209f68d9
GM
4548 s->ybase += voffset;
4549
4550 xassert (s->face && s->face->gc);
4551 return glyph - s->row->glyphs[s->area];
06a2c219
GM
4552}
4553
4554
4555/* Initialize glyph string S. CHAR2B is a suitably allocated vector
4556 of XChar2b structures for S; it can't be allocated in
4557 x_init_glyph_string because it must be allocated via `alloca'. W
4558 is the window on which S is drawn. ROW and AREA are the glyph row
4559 and area within the row from which S is constructed. START is the
4560 index of the first glyph structure covered by S. HL is a
4561 face-override for drawing S. */
4562
4563static void
4564x_init_glyph_string (s, char2b, w, row, area, start, hl)
4565 struct glyph_string *s;
4566 XChar2b *char2b;
4567 struct window *w;
4568 struct glyph_row *row;
4569 enum glyph_row_area area;
4570 int start;
4571 enum draw_glyphs_face hl;
4572{
4573 bzero (s, sizeof *s);
4574 s->w = w;
4575 s->f = XFRAME (w->frame);
4576 s->display = FRAME_X_DISPLAY (s->f);
4577 s->window = FRAME_X_WINDOW (s->f);
4578 s->char2b = char2b;
4579 s->hl = hl;
4580 s->row = row;
4581 s->area = area;
4582 s->first_glyph = row->glyphs[area] + start;
4583 s->height = row->height;
4584 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4585
9ea173e8
GM
4586 /* Display the internal border below the tool-bar window. */
4587 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219
GM
4588 s->y -= s->f->output_data.x->internal_border_width;
4589
4590 s->ybase = s->y + row->ascent;
4591}
4592
4593
4594/* Set background width of glyph string S. START is the index of the
4595 first glyph following S. LAST_X is the right-most x-position + 1
4596 in the drawing area. */
4597
4598static INLINE void
4599x_set_glyph_string_background_width (s, start, last_x)
4600 struct glyph_string *s;
4601 int start;
4602 int last_x;
4603{
4604 /* If the face of this glyph string has to be drawn to the end of
4605 the drawing area, set S->extends_to_end_of_line_p. */
4606 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4607
4608 if (start == s->row->used[s->area]
4609 && s->hl == DRAW_NORMAL_TEXT
4610 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4611 || s->face->background != default_face->background
4612 || s->face->stipple != default_face->stipple))
4613 s->extends_to_end_of_line_p = 1;
4614
4615 /* If S extends its face to the end of the line, set its
4616 background_width to the distance to the right edge of the drawing
4617 area. */
4618 if (s->extends_to_end_of_line_p)
1da3fd71 4619 s->background_width = last_x - s->x + 1;
06a2c219
GM
4620 else
4621 s->background_width = s->width;
4622}
4623
4624
4625/* Add a glyph string for a stretch glyph to the list of strings
4626 between HEAD and TAIL. START is the index of the stretch glyph in
4627 row area AREA of glyph row ROW. END is the index of the last glyph
4628 in that glyph row area. X is the current output position assigned
4629 to the new glyph string constructed. HL overrides that face of the
4630 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4631 is the right-most x-position of the drawing area. */
4632
8abee2e1
DL
4633/* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4634 and below -- keep them on one line. */
4635#define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4636 do \
4637 { \
4638 s = (struct glyph_string *) alloca (sizeof *s); \
4639 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
209f68d9 4640 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
06a2c219 4641 x_append_glyph_string (&HEAD, &TAIL, s); \
06a2c219
GM
4642 s->x = (X); \
4643 } \
4644 while (0)
4645
4646
4647/* Add a glyph string for an image glyph to the list of strings
4648 between HEAD and TAIL. START is the index of the image glyph in
4649 row area AREA of glyph row ROW. END is the index of the last glyph
4650 in that glyph row area. X is the current output position assigned
4651 to the new glyph string constructed. HL overrides that face of the
4652 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4653 is the right-most x-position of the drawing area. */
4654
8abee2e1 4655#define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4656 do \
4657 { \
4658 s = (struct glyph_string *) alloca (sizeof *s); \
4659 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4660 x_fill_image_glyph_string (s); \
4661 x_append_glyph_string (&HEAD, &TAIL, s); \
4662 ++START; \
4663 s->x = (X); \
4664 } \
4665 while (0)
4666
4667
4668/* Add a glyph string for a sequence of character glyphs to the list
4669 of strings between HEAD and TAIL. START is the index of the first
4670 glyph in row area AREA of glyph row ROW that is part of the new
4671 glyph string. END is the index of the last glyph in that glyph row
4672 area. X is the current output position assigned to the new glyph
4673 string constructed. HL overrides that face of the glyph; e.g. it
4674 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4675 right-most x-position of the drawing area. */
4676
8abee2e1 4677#define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4678 do \
4679 { \
3e71d8f2 4680 int c, face_id; \
06a2c219
GM
4681 XChar2b *char2b; \
4682 \
43d120d8 4683 c = (ROW)->glyphs[AREA][START].u.ch; \
43d120d8 4684 face_id = (ROW)->glyphs[AREA][START].face_id; \
06a2c219 4685 \
b4192550
KH
4686 s = (struct glyph_string *) alloca (sizeof *s); \
4687 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4688 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4689 x_append_glyph_string (&HEAD, &TAIL, s); \
b4192550
KH
4690 s->x = (X); \
4691 START = x_fill_glyph_string (s, face_id, START, END, \
66ac4b0e 4692 OVERLAPS_P); \
06a2c219
GM
4693 } \
4694 while (0)
4695
4696
b4192550
KH
4697/* Add a glyph string for a composite sequence to the list of strings
4698 between HEAD and TAIL. START is the index of the first glyph in
4699 row area AREA of glyph row ROW that is part of the new glyph
4700 string. END is the index of the last glyph in that glyph row area.
4701 X is the current output position assigned to the new glyph string
4702 constructed. HL overrides that face of the glyph; e.g. it is
4703 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4704 x-position of the drawing area. */
4705
6c27ec25 4706#define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
b4192550 4707 do { \
43d120d8
KH
4708 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4709 int face_id = (ROW)->glyphs[AREA][START].face_id; \
ee569018 4710 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
b4192550
KH
4711 struct composition *cmp = composition_table[cmp_id]; \
4712 int glyph_len = cmp->glyph_len; \
4713 XChar2b *char2b; \
4714 struct face **faces; \
4715 struct glyph_string *first_s = NULL; \
4716 int n; \
4717 \
ee569018 4718 base_face = base_face->ascii_face; \
b4192550
KH
4719 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4720 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4721 /* At first, fill in `char2b' and `faces'. */ \
4722 for (n = 0; n < glyph_len; n++) \
4723 { \
43d120d8 4724 int c = COMPOSITION_GLYPH (cmp, n); \
ee569018
KH
4725 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4726 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4727 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4728 this_face_id, char2b + n, 1); \
b4192550
KH
4729 } \
4730 \
4731 /* Make glyph_strings for each glyph sequence that is drawable by \
4732 the same face, and append them to HEAD/TAIL. */ \
4733 for (n = 0; n < cmp->glyph_len;) \
4734 { \
4735 s = (struct glyph_string *) alloca (sizeof *s); \
4736 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4737 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4738 s->cmp = cmp; \
4739 s->gidx = n; \
b4192550
KH
4740 s->x = (X); \
4741 \
4742 if (n == 0) \
4743 first_s = s; \
4744 \
4745 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4746 } \
4747 \
4748 ++START; \
4749 s = first_s; \
4750 } while (0)
4751
4752
06a2c219
GM
4753/* Build a list of glyph strings between HEAD and TAIL for the glyphs
4754 of AREA of glyph row ROW on window W between indices START and END.
4755 HL overrides the face for drawing glyph strings, e.g. it is
4756 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4757 x-positions of the drawing area.
4758
4759 This is an ugly monster macro construct because we must use alloca
4760 to allocate glyph strings (because x_draw_glyphs can be called
4761 asynchronously). */
4762
8abee2e1 4763#define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4764 do \
4765 { \
4766 HEAD = TAIL = NULL; \
4767 while (START < END) \
4768 { \
4769 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4770 switch (first_glyph->type) \
4771 { \
4772 case CHAR_GLYPH: \
4773 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
66ac4b0e
GM
4774 TAIL, HL, X, LAST_X, \
4775 OVERLAPS_P); \
06a2c219
GM
4776 break; \
4777 \
b4192550
KH
4778 case COMPOSITE_GLYPH: \
4779 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4780 HEAD, TAIL, HL, X, LAST_X,\
4781 OVERLAPS_P); \
4782 break; \
4783 \
06a2c219
GM
4784 case STRETCH_GLYPH: \
4785 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4786 HEAD, TAIL, HL, X, LAST_X); \
4787 break; \
4788 \
4789 case IMAGE_GLYPH: \
4790 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4791 TAIL, HL, X, LAST_X); \
4792 break; \
4793 \
4794 default: \
4795 abort (); \
4796 } \
4797 \
4798 x_set_glyph_string_background_width (s, START, LAST_X); \
4799 (X) += s->width; \
4800 } \
4801 } \
4802 while (0)
4803
4804
4805/* Draw glyphs between START and END in AREA of ROW on window W,
4806 starting at x-position X. X is relative to AREA in W. HL is a
4807 face-override with the following meaning:
4808
4809 DRAW_NORMAL_TEXT draw normally
4810 DRAW_CURSOR draw in cursor face
4811 DRAW_MOUSE_FACE draw in mouse face.
4812 DRAW_INVERSE_VIDEO draw in mode line face
4813 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4814 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4815
4816 If REAL_START is non-null, return in *REAL_START the real starting
4817 position for display. This can be different from START in case
4818 overlapping glyphs must be displayed. If REAL_END is non-null,
4819 return in *REAL_END the real end position for display. This can be
4820 different from END in case overlapping glyphs must be displayed.
4821
66ac4b0e
GM
4822 If OVERLAPS_P is non-zero, draw only the foreground of characters
4823 and clip to the physical height of ROW.
4824
06a2c219
GM
4825 Value is the x-position reached, relative to AREA of W. */
4826
4827static int
66ac4b0e
GM
4828x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4829 overlaps_p)
06a2c219
GM
4830 struct window *w;
4831 int x;
4832 struct glyph_row *row;
4833 enum glyph_row_area area;
4834 int start, end;
4835 enum draw_glyphs_face hl;
4836 int *real_start, *real_end;
66ac4b0e 4837 int overlaps_p;
dc6f92b8 4838{
06a2c219
GM
4839 struct glyph_string *head, *tail;
4840 struct glyph_string *s;
4841 int last_x, area_width;
4842 int x_reached;
4843 int i, j;
4844
4845 /* Let's rather be paranoid than getting a SEGV. */
06a2c219 4846 end = min (end, row->used[area]);
a8710abf
GM
4847 start = max (0, start);
4848 start = min (end, start);
06a2c219
GM
4849 if (real_start)
4850 *real_start = start;
4851 if (real_end)
4852 *real_end = end;
4853
4854 /* Translate X to frame coordinates. Set last_x to the right
4855 end of the drawing area. */
4856 if (row->full_width_p)
4857 {
4858 /* X is relative to the left edge of W, without scroll bars
4859 or flag areas. */
4860 struct frame *f = XFRAME (w->frame);
110859fc 4861 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
06a2c219 4862 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
dc6f92b8 4863
06a2c219
GM
4864 x += window_left_x;
4865 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4866 last_x = window_left_x + area_width;
4867
4868 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4869 {
110859fc 4870 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
06a2c219
GM
4871 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4872 last_x += width;
4873 else
4874 x -= width;
4875 }
dc6f92b8 4876
b9432a85
GM
4877 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4878 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
06a2c219
GM
4879 }
4880 else
dc6f92b8 4881 {
06a2c219
GM
4882 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4883 area_width = window_box_width (w, area);
4884 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
dc6f92b8
JB
4885 }
4886
06a2c219
GM
4887 /* Build a doubly-linked list of glyph_string structures between
4888 head and tail from what we have to draw. Note that the macro
4889 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4890 the reason we use a separate variable `i'. */
4891 i = start;
66ac4b0e
GM
4892 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4893 overlaps_p);
06a2c219
GM
4894 if (tail)
4895 x_reached = tail->x + tail->background_width;
4896 else
4897 x_reached = x;
90e65f07 4898
06a2c219
GM
4899 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4900 the row, redraw some glyphs in front or following the glyph
4901 strings built above. */
a8710abf 4902 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
06a2c219
GM
4903 {
4904 int dummy_x = 0;
4905 struct glyph_string *h, *t;
4906
4907 /* Compute overhangs for all glyph strings. */
4908 for (s = head; s; s = s->next)
4909 x_compute_glyph_string_overhangs (s);
4910
4911 /* Prepend glyph strings for glyphs in front of the first glyph
4912 string that are overwritten because of the first glyph
4913 string's left overhang. The background of all strings
4914 prepended must be drawn because the first glyph string
4915 draws over it. */
4916 i = x_left_overwritten (head);
4917 if (i >= 0)
4918 {
4919 j = i;
4920 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
66ac4b0e
GM
4921 DRAW_NORMAL_TEXT, dummy_x, last_x,
4922 overlaps_p);
06a2c219
GM
4923 start = i;
4924 if (real_start)
4925 *real_start = start;
4926 x_compute_overhangs_and_x (t, head->x, 1);
4927 x_prepend_glyph_string_lists (&head, &tail, h, t);
4928 }
58769bee 4929
06a2c219
GM
4930 /* Prepend glyph strings for glyphs in front of the first glyph
4931 string that overwrite that glyph string because of their
4932 right overhang. For these strings, only the foreground must
4933 be drawn, because it draws over the glyph string at `head'.
4934 The background must not be drawn because this would overwrite
4935 right overhangs of preceding glyphs for which no glyph
4936 strings exist. */
4937 i = x_left_overwriting (head);
4938 if (i >= 0)
4939 {
4940 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
66ac4b0e
GM
4941 DRAW_NORMAL_TEXT, dummy_x, last_x,
4942 overlaps_p);
06a2c219
GM
4943 for (s = h; s; s = s->next)
4944 s->background_filled_p = 1;
4945 if (real_start)
4946 *real_start = i;
4947 x_compute_overhangs_and_x (t, head->x, 1);
4948 x_prepend_glyph_string_lists (&head, &tail, h, t);
4949 }
dbcb258a 4950
06a2c219
GM
4951 /* Append glyphs strings for glyphs following the last glyph
4952 string tail that are overwritten by tail. The background of
4953 these strings has to be drawn because tail's foreground draws
4954 over it. */
4955 i = x_right_overwritten (tail);
4956 if (i >= 0)
4957 {
4958 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4959 DRAW_NORMAL_TEXT, x, last_x,
4960 overlaps_p);
06a2c219
GM
4961 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4962 x_append_glyph_string_lists (&head, &tail, h, t);
4963 if (real_end)
4964 *real_end = i;
4965 }
dc6f92b8 4966
06a2c219
GM
4967 /* Append glyph strings for glyphs following the last glyph
4968 string tail that overwrite tail. The foreground of such
4969 glyphs has to be drawn because it writes into the background
4970 of tail. The background must not be drawn because it could
4971 paint over the foreground of following glyphs. */
4972 i = x_right_overwriting (tail);
4973 if (i >= 0)
4974 {
4975 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4976 DRAW_NORMAL_TEXT, x, last_x,
4977 overlaps_p);
06a2c219
GM
4978 for (s = h; s; s = s->next)
4979 s->background_filled_p = 1;
4980 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4981 x_append_glyph_string_lists (&head, &tail, h, t);
4982 if (real_end)
4983 *real_end = i;
4984 }
4985 }
58769bee 4986
06a2c219
GM
4987 /* Draw all strings. */
4988 for (s = head; s; s = s->next)
4989 x_draw_glyph_string (s);
dc6f92b8 4990
06a2c219
GM
4991 /* Value is the x-position up to which drawn, relative to AREA of W.
4992 This doesn't include parts drawn because of overhangs. */
4993 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4994 if (!row->full_width_p)
4995 {
4996 if (area > LEFT_MARGIN_AREA)
4997 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4998 if (area > TEXT_AREA)
4999 x_reached -= window_box_width (w, TEXT_AREA);
5000 }
a8710abf 5001
06a2c219
GM
5002 return x_reached;
5003}
dc6f92b8 5004
dc6f92b8 5005
66ac4b0e
GM
5006/* Fix the display of area AREA of overlapping row ROW in window W. */
5007
5008static void
5009x_fix_overlapping_area (w, row, area)
5010 struct window *w;
5011 struct glyph_row *row;
5012 enum glyph_row_area area;
5013{
5014 int i, x;
5015
5016 BLOCK_INPUT;
5017
5018 if (area == LEFT_MARGIN_AREA)
5019 x = 0;
5020 else if (area == TEXT_AREA)
5021 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5022 else
5023 x = (window_box_width (w, LEFT_MARGIN_AREA)
5024 + window_box_width (w, TEXT_AREA));
5025
5026 for (i = 0; i < row->used[area];)
5027 {
5028 if (row->glyphs[area][i].overlaps_vertically_p)
5029 {
5030 int start = i, start_x = x;
5031
5032 do
5033 {
5034 x += row->glyphs[area][i].pixel_width;
5035 ++i;
5036 }
5037 while (i < row->used[area]
5038 && row->glyphs[area][i].overlaps_vertically_p);
5039
5040 x_draw_glyphs (w, start_x, row, area, start, i,
5041 (row->inverse_p
5042 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5043 NULL, NULL, 1);
5044 }
5045 else
5046 {
5047 x += row->glyphs[area][i].pixel_width;
5048 ++i;
5049 }
5050 }
5051
5052 UNBLOCK_INPUT;
5053}
5054
5055
06a2c219
GM
5056/* Output LEN glyphs starting at START at the nominal cursor position.
5057 Advance the nominal cursor over the text. The global variable
5058 updated_window contains the window being updated, updated_row is
5059 the glyph row being updated, and updated_area is the area of that
5060 row being updated. */
dc6f92b8 5061
06a2c219
GM
5062static void
5063x_write_glyphs (start, len)
5064 struct glyph *start;
5065 int len;
5066{
5067 int x, hpos, real_start, real_end;
d9cdbb3d 5068
06a2c219 5069 xassert (updated_window && updated_row);
dc6f92b8 5070 BLOCK_INPUT;
06a2c219
GM
5071
5072 /* Write glyphs. */
dc6f92b8 5073
06a2c219
GM
5074 hpos = start - updated_row->glyphs[updated_area];
5075 x = x_draw_glyphs (updated_window, output_cursor.x,
5076 updated_row, updated_area,
5077 hpos, hpos + len,
5078 (updated_row->inverse_p
5079 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
66ac4b0e 5080 &real_start, &real_end, 0);
b30ec466 5081
06a2c219
GM
5082 /* If we drew over the cursor, note that it is not visible any more. */
5083 note_overwritten_text_cursor (updated_window, real_start,
5084 real_end - real_start);
dc6f92b8
JB
5085
5086 UNBLOCK_INPUT;
06a2c219
GM
5087
5088 /* Advance the output cursor. */
5089 output_cursor.hpos += len;
5090 output_cursor.x = x;
dc6f92b8
JB
5091}
5092
0cdd0c9f 5093
06a2c219 5094/* Insert LEN glyphs from START at the nominal cursor position. */
0cdd0c9f 5095
06a2c219
GM
5096static void
5097x_insert_glyphs (start, len)
5098 struct glyph *start;
5099 register int len;
5100{
5101 struct frame *f;
5102 struct window *w;
5103 int line_height, shift_by_width, shifted_region_width;
5104 struct glyph_row *row;
5105 struct glyph *glyph;
5106 int frame_x, frame_y, hpos, real_start, real_end;
58769bee 5107
06a2c219 5108 xassert (updated_window && updated_row);
0cdd0c9f 5109 BLOCK_INPUT;
06a2c219
GM
5110 w = updated_window;
5111 f = XFRAME (WINDOW_FRAME (w));
5112
5113 /* Get the height of the line we are in. */
5114 row = updated_row;
5115 line_height = row->height;
5116
5117 /* Get the width of the glyphs to insert. */
5118 shift_by_width = 0;
5119 for (glyph = start; glyph < start + len; ++glyph)
5120 shift_by_width += glyph->pixel_width;
5121
5122 /* Get the width of the region to shift right. */
5123 shifted_region_width = (window_box_width (w, updated_area)
5124 - output_cursor.x
5125 - shift_by_width);
5126
5127 /* Shift right. */
5128 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
5129 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5130 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5131 f->output_data.x->normal_gc,
5132 frame_x, frame_y,
5133 shifted_region_width, line_height,
5134 frame_x + shift_by_width, frame_y);
5135
5136 /* Write the glyphs. */
5137 hpos = start - row->glyphs[updated_area];
5138 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
66ac4b0e 5139 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
06a2c219
GM
5140 note_overwritten_text_cursor (w, real_start, real_end - real_start);
5141
5142 /* Advance the output cursor. */
5143 output_cursor.hpos += len;
5144 output_cursor.x += shift_by_width;
0cdd0c9f
RS
5145 UNBLOCK_INPUT;
5146}
0cdd0c9f 5147
0cdd0c9f 5148
06a2c219
GM
5149/* Delete N glyphs at the nominal cursor position. Not implemented
5150 for X frames. */
c83febd7
RS
5151
5152static void
06a2c219
GM
5153x_delete_glyphs (n)
5154 register int n;
c83febd7 5155{
06a2c219 5156 abort ();
c83febd7
RS
5157}
5158
0cdd0c9f 5159
06a2c219
GM
5160/* Erase the current text line from the nominal cursor position
5161 (inclusive) to pixel column TO_X (exclusive). The idea is that
5162 everything from TO_X onward is already erased.
5163
5164 TO_X is a pixel position relative to updated_area of
5165 updated_window. TO_X == -1 means clear to the end of this area. */
dc6f92b8 5166
06a2c219
GM
5167static void
5168x_clear_end_of_line (to_x)
5169 int to_x;
5170{
5171 struct frame *f;
5172 struct window *w = updated_window;
5173 int max_x, min_y, max_y;
5174 int from_x, from_y, to_y;
5175
5176 xassert (updated_window && updated_row);
5177 f = XFRAME (w->frame);
5178
5179 if (updated_row->full_width_p)
5180 {
5181 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5182 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5183 && !w->pseudo_window_p)
5184 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
0cdd0c9f 5185 }
06a2c219
GM
5186 else
5187 max_x = window_box_width (w, updated_area);
5188 max_y = window_text_bottom_y (w);
dc6f92b8 5189
06a2c219
GM
5190 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5191 of window. For TO_X > 0, truncate to end of drawing area. */
5192 if (to_x == 0)
5193 return;
5194 else if (to_x < 0)
5195 to_x = max_x;
5196 else
5197 to_x = min (to_x, max_x);
dbc4e1c1 5198
06a2c219
GM
5199 to_y = min (max_y, output_cursor.y + updated_row->height);
5200
5201 /* Notice if the cursor will be cleared by this operation. */
5202 if (!updated_row->full_width_p)
5203 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
dbc4e1c1 5204
06a2c219
GM
5205 from_x = output_cursor.x;
5206
5207 /* Translate to frame coordinates. */
5208 if (updated_row->full_width_p)
5209 {
5210 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5211 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5212 }
0cdd0c9f
RS
5213 else
5214 {
06a2c219
GM
5215 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5216 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5217 }
5218
045dee35 5219 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
06a2c219
GM
5220 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5221 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5222
5223 /* Prevent inadvertently clearing to end of the X window. */
5224 if (to_x > from_x && to_y > from_y)
5225 {
5226 BLOCK_INPUT;
5227 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5228 from_x, from_y, to_x - from_x, to_y - from_y,
5229 False);
5230 UNBLOCK_INPUT;
0cdd0c9f 5231 }
0cdd0c9f 5232}
dbc4e1c1 5233
0cdd0c9f 5234
06a2c219 5235/* Clear entire frame. If updating_frame is non-null, clear that
b86bd3dd 5236 frame. Otherwise clear the selected frame. */
06a2c219
GM
5237
5238static void
5239x_clear_frame ()
0cdd0c9f 5240{
06a2c219 5241 struct frame *f;
0cdd0c9f 5242
06a2c219
GM
5243 if (updating_frame)
5244 f = updating_frame;
0cdd0c9f 5245 else
b86bd3dd 5246 f = SELECTED_FRAME ();
58769bee 5247
06a2c219
GM
5248 /* Clearing the frame will erase any cursor, so mark them all as no
5249 longer visible. */
5250 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5251 output_cursor.hpos = output_cursor.vpos = 0;
5252 output_cursor.x = -1;
5253
5254 /* We don't set the output cursor here because there will always
5255 follow an explicit cursor_to. */
5256 BLOCK_INPUT;
5257 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5258
5259 /* We have to clear the scroll bars, too. If we have changed
5260 colors or something like that, then they should be notified. */
5261 x_scroll_bar_clear (f);
0cdd0c9f 5262
06a2c219
GM
5263 XFlush (FRAME_X_DISPLAY (f));
5264 UNBLOCK_INPUT;
dc6f92b8 5265}
06a2c219
GM
5266
5267
dc6f92b8 5268\f
dbc4e1c1
JB
5269/* Invert the middle quarter of the frame for .15 sec. */
5270
06a2c219
GM
5271/* We use the select system call to do the waiting, so we have to make
5272 sure it's available. If it isn't, we just won't do visual bells. */
5273
dbc4e1c1
JB
5274#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5275
06a2c219
GM
5276
5277/* Subtract the `struct timeval' values X and Y, storing the result in
5278 *RESULT. Return 1 if the difference is negative, otherwise 0. */
dbc4e1c1
JB
5279
5280static int
5281timeval_subtract (result, x, y)
5282 struct timeval *result, x, y;
5283{
06a2c219
GM
5284 /* Perform the carry for the later subtraction by updating y. This
5285 is safer because on some systems the tv_sec member is unsigned. */
dbc4e1c1
JB
5286 if (x.tv_usec < y.tv_usec)
5287 {
5288 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5289 y.tv_usec -= 1000000 * nsec;
5290 y.tv_sec += nsec;
5291 }
06a2c219 5292
dbc4e1c1
JB
5293 if (x.tv_usec - y.tv_usec > 1000000)
5294 {
5295 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5296 y.tv_usec += 1000000 * nsec;
5297 y.tv_sec -= nsec;
5298 }
5299
06a2c219
GM
5300 /* Compute the time remaining to wait. tv_usec is certainly
5301 positive. */
dbc4e1c1
JB
5302 result->tv_sec = x.tv_sec - y.tv_sec;
5303 result->tv_usec = x.tv_usec - y.tv_usec;
5304
06a2c219
GM
5305 /* Return indication of whether the result should be considered
5306 negative. */
dbc4e1c1
JB
5307 return x.tv_sec < y.tv_sec;
5308}
dc6f92b8 5309
dfcf069d 5310void
f676886a
JB
5311XTflash (f)
5312 struct frame *f;
dc6f92b8 5313{
dbc4e1c1 5314 BLOCK_INPUT;
dc6f92b8 5315
dbc4e1c1
JB
5316 {
5317 GC gc;
dc6f92b8 5318
06a2c219
GM
5319 /* Create a GC that will use the GXxor function to flip foreground
5320 pixels into background pixels. */
dbc4e1c1
JB
5321 {
5322 XGCValues values;
dc6f92b8 5323
dbc4e1c1 5324 values.function = GXxor;
7556890b
RS
5325 values.foreground = (f->output_data.x->foreground_pixel
5326 ^ f->output_data.x->background_pixel);
58769bee 5327
334208b7 5328 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dbc4e1c1
JB
5329 GCFunction | GCForeground, &values);
5330 }
dc6f92b8 5331
dbc4e1c1 5332 {
e84e14c3
RS
5333 /* Get the height not including a menu bar widget. */
5334 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5335 /* Height of each line to flash. */
5336 int flash_height = FRAME_LINE_HEIGHT (f);
5337 /* These will be the left and right margins of the rectangles. */
5338 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5339 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5340
5341 int width;
5342
5343 /* Don't flash the area between a scroll bar and the frame
5344 edge it is next to. */
5345 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5346 {
5347 case vertical_scroll_bar_left:
5348 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5349 break;
5350
5351 case vertical_scroll_bar_right:
5352 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5353 break;
06a2c219
GM
5354
5355 default:
5356 break;
e84e14c3
RS
5357 }
5358
5359 width = flash_right - flash_left;
5360
5361 /* If window is tall, flash top and bottom line. */
5362 if (height > 3 * FRAME_LINE_HEIGHT (f))
5363 {
5364 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5365 flash_left,
5366 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5367 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5368 width, flash_height);
5369 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5370 flash_left,
5371 (height - flash_height
5372 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5373 width, flash_height);
5374 }
5375 else
5376 /* If it is short, flash it all. */
5377 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5378 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5379 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
dc6f92b8 5380
06a2c219 5381 x_flush (f);
dc6f92b8 5382
dbc4e1c1 5383 {
06a2c219 5384 struct timeval wakeup;
dc6f92b8 5385
66c30ea1 5386 EMACS_GET_TIME (wakeup);
dc6f92b8 5387
dbc4e1c1
JB
5388 /* Compute time to wait until, propagating carry from usecs. */
5389 wakeup.tv_usec += 150000;
5390 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5391 wakeup.tv_usec %= 1000000;
5392
5393 /* Keep waiting until past the time wakeup. */
5394 while (1)
5395 {
5396 struct timeval timeout;
5397
66c30ea1 5398 EMACS_GET_TIME (timeout);
dbc4e1c1
JB
5399
5400 /* In effect, timeout = wakeup - timeout.
5401 Break if result would be negative. */
5402 if (timeval_subtract (&timeout, wakeup, timeout))
5403 break;
5404
5405 /* Try to wait that long--but we might wake up sooner. */
c32cdd9a 5406 select (0, NULL, NULL, NULL, &timeout);
dbc4e1c1
JB
5407 }
5408 }
58769bee 5409
e84e14c3
RS
5410 /* If window is tall, flash top and bottom line. */
5411 if (height > 3 * FRAME_LINE_HEIGHT (f))
5412 {
5413 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5414 flash_left,
5415 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5416 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5417 width, flash_height);
5418 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5419 flash_left,
5420 (height - flash_height
5421 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5422 width, flash_height);
5423 }
5424 else
5425 /* If it is short, flash it all. */
5426 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5427 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5428 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5429
334208b7 5430 XFreeGC (FRAME_X_DISPLAY (f), gc);
06a2c219 5431 x_flush (f);
dc6f92b8 5432 }
dbc4e1c1
JB
5433 }
5434
5435 UNBLOCK_INPUT;
dc6f92b8
JB
5436}
5437
06a2c219 5438#endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
dbc4e1c1
JB
5439
5440
dc6f92b8
JB
5441/* Make audible bell. */
5442
dfcf069d 5443void
dc6f92b8
JB
5444XTring_bell ()
5445{
b86bd3dd
GM
5446 struct frame *f = SELECTED_FRAME ();
5447
5448 if (FRAME_X_DISPLAY (f))
5449 {
dbc4e1c1 5450#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
b86bd3dd
GM
5451 if (visible_bell)
5452 XTflash (f);
5453 else
dbc4e1c1 5454#endif
b86bd3dd
GM
5455 {
5456 BLOCK_INPUT;
5457 XBell (FRAME_X_DISPLAY (f), 0);
5458 XFlush (FRAME_X_DISPLAY (f));
5459 UNBLOCK_INPUT;
5460 }
dc6f92b8
JB
5461 }
5462}
06a2c219 5463
dc6f92b8 5464\f
06a2c219
GM
5465/* Specify how many text lines, from the top of the window,
5466 should be affected by insert-lines and delete-lines operations.
5467 This, and those operations, are used only within an update
5468 that is bounded by calls to x_update_begin and x_update_end. */
dc6f92b8 5469
dfcf069d 5470static void
06a2c219
GM
5471XTset_terminal_window (n)
5472 register int n;
dc6f92b8 5473{
06a2c219 5474 /* This function intentionally left blank. */
dc6f92b8
JB
5475}
5476
06a2c219
GM
5477
5478\f
5479/***********************************************************************
5480 Line Dance
5481 ***********************************************************************/
5482
5483/* Perform an insert-lines or delete-lines operation, inserting N
5484 lines or deleting -N lines at vertical position VPOS. */
5485
dfcf069d 5486static void
06a2c219
GM
5487x_ins_del_lines (vpos, n)
5488 int vpos, n;
dc6f92b8
JB
5489{
5490 abort ();
5491}
06a2c219
GM
5492
5493
5494/* Scroll part of the display as described by RUN. */
dc6f92b8 5495
dfcf069d 5496static void
06a2c219
GM
5497x_scroll_run (w, run)
5498 struct window *w;
5499 struct run *run;
dc6f92b8 5500{
06a2c219
GM
5501 struct frame *f = XFRAME (w->frame);
5502 int x, y, width, height, from_y, to_y, bottom_y;
5503
5504 /* Get frame-relative bounding box of the text display area of W,
5505 without mode lines. Include in this box the flags areas to the
5506 left and right of W. */
5507 window_box (w, -1, &x, &y, &width, &height);
110859fc
GM
5508 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5509 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
5510
5511 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5512 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5513 bottom_y = y + height;
dc6f92b8 5514
06a2c219
GM
5515 if (to_y < from_y)
5516 {
5517 /* Scrolling up. Make sure we don't copy part of the mode
5518 line at the bottom. */
5519 if (from_y + run->height > bottom_y)
5520 height = bottom_y - from_y;
5521 else
5522 height = run->height;
5523 }
dc6f92b8 5524 else
06a2c219
GM
5525 {
5526 /* Scolling down. Make sure we don't copy over the mode line.
5527 at the bottom. */
5528 if (to_y + run->height > bottom_y)
5529 height = bottom_y - to_y;
5530 else
5531 height = run->height;
5532 }
7a13e894 5533
06a2c219
GM
5534 BLOCK_INPUT;
5535
5536 /* Cursor off. Will be switched on again in x_update_window_end. */
5537 updated_window = w;
5538 x_clear_cursor (w);
5539
5540 XCopyArea (FRAME_X_DISPLAY (f),
5541 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5542 f->output_data.x->normal_gc,
5543 x, from_y,
5544 width, height,
5545 x, to_y);
5546
5547 UNBLOCK_INPUT;
5548}
dc6f92b8 5549
dc6f92b8 5550
06a2c219
GM
5551\f
5552/***********************************************************************
5553 Exposure Events
5554 ***********************************************************************/
5555
5556/* Redisplay an exposed area of frame F. X and Y are the upper-left
5557 corner of the exposed rectangle. W and H are width and height of
5558 the exposed area. All are pixel values. W or H zero means redraw
5559 the entire frame. */
dc6f92b8 5560
06a2c219
GM
5561static void
5562expose_frame (f, x, y, w, h)
5563 struct frame *f;
5564 int x, y, w, h;
dc6f92b8 5565{
06a2c219 5566 XRectangle r;
dc6f92b8 5567
06a2c219 5568 TRACE ((stderr, "expose_frame "));
dc6f92b8 5569
06a2c219
GM
5570 /* No need to redraw if frame will be redrawn soon. */
5571 if (FRAME_GARBAGED_P (f))
dc6f92b8 5572 {
06a2c219
GM
5573 TRACE ((stderr, " garbaged\n"));
5574 return;
5575 }
5576
5577 /* If basic faces haven't been realized yet, there is no point in
5578 trying to redraw anything. This can happen when we get an expose
5579 event while Emacs is starting, e.g. by moving another window. */
5580 if (FRAME_FACE_CACHE (f) == NULL
5581 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5582 {
5583 TRACE ((stderr, " no faces\n"));
5584 return;
58769bee 5585 }
06a2c219
GM
5586
5587 if (w == 0 || h == 0)
58769bee 5588 {
06a2c219
GM
5589 r.x = r.y = 0;
5590 r.width = CANON_X_UNIT (f) * f->width;
5591 r.height = CANON_Y_UNIT (f) * f->height;
dc6f92b8
JB
5592 }
5593 else
5594 {
06a2c219
GM
5595 r.x = x;
5596 r.y = y;
5597 r.width = w;
5598 r.height = h;
5599 }
5600
5601 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5602 expose_window_tree (XWINDOW (f->root_window), &r);
5603
9ea173e8 5604 if (WINDOWP (f->tool_bar_window))
06a2c219 5605 {
9ea173e8 5606 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
5607 XRectangle window_rect;
5608 XRectangle intersection_rect;
5609 int window_x, window_y, window_width, window_height;
5610
5611
5612 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5613 window_rect.x = window_x;
5614 window_rect.y = window_y;
5615 window_rect.width = window_width;
5616 window_rect.height = window_height;
5617
5618 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5619 expose_window (w, &intersection_rect);
5620 }
5621
5622#ifndef USE_X_TOOLKIT
5623 if (WINDOWP (f->menu_bar_window))
5624 {
5625 struct window *w = XWINDOW (f->menu_bar_window);
5626 XRectangle window_rect;
5627 XRectangle intersection_rect;
5628 int window_x, window_y, window_width, window_height;
5629
5630
5631 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5632 window_rect.x = window_x;
5633 window_rect.y = window_y;
5634 window_rect.width = window_width;
5635 window_rect.height = window_height;
5636
5637 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5638 expose_window (w, &intersection_rect);
dc6f92b8 5639 }
06a2c219 5640#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
5641}
5642
06a2c219
GM
5643
5644/* Redraw (parts) of all windows in the window tree rooted at W that
5645 intersect R. R contains frame pixel coordinates. */
5646
58769bee 5647static void
06a2c219
GM
5648expose_window_tree (w, r)
5649 struct window *w;
5650 XRectangle *r;
dc6f92b8 5651{
06a2c219
GM
5652 while (w)
5653 {
5654 if (!NILP (w->hchild))
5655 expose_window_tree (XWINDOW (w->hchild), r);
5656 else if (!NILP (w->vchild))
5657 expose_window_tree (XWINDOW (w->vchild), r);
5658 else
5659 {
5660 XRectangle window_rect;
5661 XRectangle intersection_rect;
5662 struct frame *f = XFRAME (w->frame);
5663 int window_x, window_y, window_width, window_height;
5664
5665 /* Frame-relative pixel rectangle of W. */
5666 window_box (w, -1, &window_x, &window_y, &window_width,
5667 &window_height);
5668 window_rect.x
5669 = (window_x
110859fc 5670 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
714dc26c 5671 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
06a2c219
GM
5672 window_rect.y = window_y;
5673 window_rect.width
5674 = (window_width
110859fc 5675 + FRAME_X_FLAGS_AREA_WIDTH (f)
06a2c219
GM
5676 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5677 window_rect.height
5678 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5679
5680 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5681 expose_window (w, &intersection_rect);
5682 }
58769bee 5683
06a2c219
GM
5684 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5685 }
5686}
58769bee 5687
dc6f92b8 5688
06a2c219
GM
5689/* Redraw the part of glyph row area AREA of glyph row ROW on window W
5690 which intersects rectangle R. R is in window-relative coordinates. */
5691
5692static void
5693expose_area (w, row, r, area)
5694 struct window *w;
5695 struct glyph_row *row;
5696 XRectangle *r;
5697 enum glyph_row_area area;
5698{
06a2c219
GM
5699 struct glyph *first = row->glyphs[area];
5700 struct glyph *end = row->glyphs[area] + row->used[area];
5701 struct glyph *last;
4bc6dcc7 5702 int first_x, start_x, x;
06a2c219 5703
6fb13182
GM
5704 if (area == TEXT_AREA && row->fill_line_p)
5705 /* If row extends face to end of line write the whole line. */
4bc6dcc7 5706 x_draw_glyphs (w, 0, row, area,
6fb13182 5707 0, row->used[area],
06a2c219 5708 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5709 NULL, NULL, 0);
6fb13182
GM
5710 else
5711 {
4bc6dcc7
GM
5712 /* Set START_X to the window-relative start position for drawing glyphs of
5713 AREA. The first glyph of the text area can be partially visible.
5714 The first glyphs of other areas cannot. */
5715 if (area == LEFT_MARGIN_AREA)
5716 start_x = 0;
5717 else if (area == TEXT_AREA)
5718 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5719 else
5720 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5721 + window_box_width (w, TEXT_AREA));
5722 x = start_x;
5723
6fb13182
GM
5724 /* Find the first glyph that must be redrawn. */
5725 while (first < end
5726 && x + first->pixel_width < r->x)
5727 {
5728 x += first->pixel_width;
5729 ++first;
5730 }
5731
5732 /* Find the last one. */
5733 last = first;
5734 first_x = x;
5735 while (last < end
5736 && x < r->x + r->width)
5737 {
5738 x += last->pixel_width;
5739 ++last;
5740 }
5741
5742 /* Repaint. */
5743 if (last > first)
4bc6dcc7 5744 x_draw_glyphs (w, first_x - start_x, row, area,
6fb13182
GM
5745 first - row->glyphs[area],
5746 last - row->glyphs[area],
5747 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5748 NULL, NULL, 0);
5749 }
06a2c219
GM
5750}
5751
58769bee 5752
06a2c219
GM
5753/* Redraw the parts of the glyph row ROW on window W intersecting
5754 rectangle R. R is in window-relative coordinates. */
dc6f92b8 5755
06a2c219
GM
5756static void
5757expose_line (w, row, r)
5758 struct window *w;
5759 struct glyph_row *row;
5760 XRectangle *r;
5761{
5762 xassert (row->enabled_p);
5763
5764 if (row->mode_line_p || w->pseudo_window_p)
5765 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5766 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5767 NULL, NULL, 0);
06a2c219
GM
5768 else
5769 {
5770 if (row->used[LEFT_MARGIN_AREA])
5771 expose_area (w, row, r, LEFT_MARGIN_AREA);
5772 if (row->used[TEXT_AREA])
5773 expose_area (w, row, r, TEXT_AREA);
5774 if (row->used[RIGHT_MARGIN_AREA])
5775 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5776 x_draw_row_bitmaps (w, row);
5777 }
5778}
dc6f92b8 5779
58769bee 5780
06a2c219
GM
5781/* Return non-zero if W's cursor intersects rectangle R. */
5782
5783static int
5784x_phys_cursor_in_rect_p (w, r)
5785 struct window *w;
5786 XRectangle *r;
5787{
5788 XRectangle cr, result;
5789 struct glyph *cursor_glyph;
5790
5791 cursor_glyph = get_phys_cursor_glyph (w);
5792 if (cursor_glyph)
5793 {
5794 cr.x = w->phys_cursor.x;
5795 cr.y = w->phys_cursor.y;
5796 cr.width = cursor_glyph->pixel_width;
5797 cr.height = w->phys_cursor_height;
5798 return x_intersect_rectangles (&cr, r, &result);
5799 }
5800 else
5801 return 0;
dc6f92b8 5802}
dc6f92b8 5803
06a2c219
GM
5804
5805/* Redraw a rectangle of window W. R is a rectangle in window
5806 relative coordinates. Call this function with input blocked. */
dc6f92b8
JB
5807
5808static void
06a2c219
GM
5809expose_window (w, r)
5810 struct window *w;
5811 XRectangle *r;
dc6f92b8 5812{
06a2c219
GM
5813 struct glyph_row *row;
5814 int y;
5815 int yb = window_text_bottom_y (w);
5816 int cursor_cleared_p;
dc6f92b8 5817
80c32bcc
GM
5818 /* If window is not yet fully initialized, do nothing. This can
5819 happen when toolkit scroll bars are used and a window is split.
5820 Reconfiguring the scroll bar will generate an expose for a newly
5821 created window. */
5822 if (w->current_matrix == NULL)
5823 return;
5824
06a2c219
GM
5825 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5826 r->x, r->y, r->width, r->height));
dc6f92b8 5827
06a2c219
GM
5828 /* Convert to window coordinates. */
5829 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5830 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
dc6f92b8 5831
06a2c219
GM
5832 /* Turn off the cursor. */
5833 if (!w->pseudo_window_p
5834 && x_phys_cursor_in_rect_p (w, r))
5835 {
5836 x_clear_cursor (w);
5837 cursor_cleared_p = 1;
5838 }
5839 else
5840 cursor_cleared_p = 0;
5841
5842 /* Find the first row intersecting the rectangle R. */
5843 row = w->current_matrix->rows;
5844 y = 0;
5845 while (row->enabled_p
5846 && y < yb
5847 && y + row->height < r->y)
5848 {
5849 y += row->height;
5850 ++row;
5851 }
5852
dc6f92b8 5853 /* Display the text in the rectangle, one text line at a time. */
06a2c219
GM
5854 while (row->enabled_p
5855 && y < yb
5856 && y < r->y + r->height)
5857 {
5858 expose_line (w, row, r);
5859 y += row->height;
5860 ++row;
5861 }
5862
5863 /* Display the mode line if there is one. */
5864 if (WINDOW_WANTS_MODELINE_P (w)
5865 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5866 row->enabled_p)
5867 && row->y < r->y + r->height)
5868 expose_line (w, row, r);
dc6f92b8 5869
06a2c219 5870 if (!w->pseudo_window_p)
dc6f92b8 5871 {
06a2c219
GM
5872 /* Draw border between windows. */
5873 x_draw_vertical_border (w);
5874
5875 /* Turn the cursor on again. */
5876 if (cursor_cleared_p)
5877 x_update_window_cursor (w, 1);
5878 }
5879}
dc6f92b8 5880
dc6f92b8 5881
06a2c219
GM
5882/* Determine the intersection of two rectangles R1 and R2. Return
5883 the intersection in *RESULT. Value is non-zero if RESULT is not
5884 empty. */
5885
5886static int
5887x_intersect_rectangles (r1, r2, result)
5888 XRectangle *r1, *r2, *result;
5889{
5890 XRectangle *left, *right;
5891 XRectangle *upper, *lower;
5892 int intersection_p = 0;
5893
5894 /* Rearrange so that R1 is the left-most rectangle. */
5895 if (r1->x < r2->x)
5896 left = r1, right = r2;
5897 else
5898 left = r2, right = r1;
5899
5900 /* X0 of the intersection is right.x0, if this is inside R1,
5901 otherwise there is no intersection. */
5902 if (right->x <= left->x + left->width)
5903 {
5904 result->x = right->x;
5905
5906 /* The right end of the intersection is the minimum of the
5907 the right ends of left and right. */
5908 result->width = (min (left->x + left->width, right->x + right->width)
5909 - result->x);
5910
5911 /* Same game for Y. */
5912 if (r1->y < r2->y)
5913 upper = r1, lower = r2;
5914 else
5915 upper = r2, lower = r1;
5916
5917 /* The upper end of the intersection is lower.y0, if this is inside
5918 of upper. Otherwise, there is no intersection. */
5919 if (lower->y <= upper->y + upper->height)
dc43ef94 5920 {
06a2c219
GM
5921 result->y = lower->y;
5922
5923 /* The lower end of the intersection is the minimum of the lower
5924 ends of upper and lower. */
5925 result->height = (min (lower->y + lower->height,
5926 upper->y + upper->height)
5927 - result->y);
5928 intersection_p = 1;
dc43ef94 5929 }
dc6f92b8
JB
5930 }
5931
06a2c219 5932 return intersection_p;
dc6f92b8 5933}
06a2c219
GM
5934
5935
5936
5937
dc6f92b8 5938\f
dc6f92b8 5939static void
334208b7
RS
5940frame_highlight (f)
5941 struct frame *f;
dc6f92b8 5942{
b3e1e05c
JB
5943 /* We used to only do this if Vx_no_window_manager was non-nil, but
5944 the ICCCM (section 4.1.6) says that the window's border pixmap
5945 and border pixel are window attributes which are "private to the
5946 client", so we can always change it to whatever we want. */
5947 BLOCK_INPUT;
334208b7 5948 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5949 f->output_data.x->border_pixel);
b3e1e05c 5950 UNBLOCK_INPUT;
5d46f928 5951 x_update_cursor (f, 1);
dc6f92b8
JB
5952}
5953
5954static void
334208b7
RS
5955frame_unhighlight (f)
5956 struct frame *f;
dc6f92b8 5957{
b3e1e05c
JB
5958 /* We used to only do this if Vx_no_window_manager was non-nil, but
5959 the ICCCM (section 4.1.6) says that the window's border pixmap
5960 and border pixel are window attributes which are "private to the
5961 client", so we can always change it to whatever we want. */
5962 BLOCK_INPUT;
334208b7 5963 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5964 f->output_data.x->border_tile);
b3e1e05c 5965 UNBLOCK_INPUT;
5d46f928 5966 x_update_cursor (f, 1);
dc6f92b8 5967}
dc6f92b8 5968
f676886a
JB
5969/* The focus has changed. Update the frames as necessary to reflect
5970 the new situation. Note that we can't change the selected frame
c5acd733 5971 here, because the Lisp code we are interrupting might become confused.
eb8c3be9 5972 Each event gets marked with the frame in which it occurred, so the
c5acd733 5973 Lisp code can tell when the switch took place by examining the events. */
dc6f92b8 5974
6d4238f3 5975static void
0f941935
KH
5976x_new_focus_frame (dpyinfo, frame)
5977 struct x_display_info *dpyinfo;
f676886a 5978 struct frame *frame;
dc6f92b8 5979{
0f941935 5980 struct frame *old_focus = dpyinfo->x_focus_frame;
dc6f92b8 5981
0f941935 5982 if (frame != dpyinfo->x_focus_frame)
dc6f92b8 5983 {
58769bee 5984 /* Set this before calling other routines, so that they see
f676886a 5985 the correct value of x_focus_frame. */
0f941935 5986 dpyinfo->x_focus_frame = frame;
6d4238f3
JB
5987
5988 if (old_focus && old_focus->auto_lower)
f676886a 5989 x_lower_frame (old_focus);
dc6f92b8
JB
5990
5991#if 0
f676886a 5992 selected_frame = frame;
e0c1aef2
KH
5993 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
5994 selected_frame);
f676886a
JB
5995 Fselect_window (selected_frame->selected_window);
5996 choose_minibuf_frame ();
c118dd06 5997#endif /* ! 0 */
dc6f92b8 5998
0f941935
KH
5999 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6000 pending_autoraise_frame = dpyinfo->x_focus_frame;
0134a210
RS
6001 else
6002 pending_autoraise_frame = 0;
6d4238f3 6003 }
dc6f92b8 6004
0f941935 6005 x_frame_rehighlight (dpyinfo);
6d4238f3
JB
6006}
6007
37c2c98b
RS
6008/* Handle an event saying the mouse has moved out of an Emacs frame. */
6009
6010void
0f941935
KH
6011x_mouse_leave (dpyinfo)
6012 struct x_display_info *dpyinfo;
37c2c98b 6013{
0f941935 6014 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
37c2c98b 6015}
6d4238f3 6016
f451eb13
JB
6017/* The focus has changed, or we have redirected a frame's focus to
6018 another frame (this happens when a frame uses a surrogate
06a2c219 6019 mini-buffer frame). Shift the highlight as appropriate.
0f941935
KH
6020
6021 The FRAME argument doesn't necessarily have anything to do with which
06a2c219 6022 frame is being highlighted or un-highlighted; we only use it to find
0f941935 6023 the appropriate X display info. */
06a2c219 6024
6d4238f3 6025static void
0f941935
KH
6026XTframe_rehighlight (frame)
6027 struct frame *frame;
6d4238f3 6028{
0f941935
KH
6029 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6030}
6d4238f3 6031
0f941935
KH
6032static void
6033x_frame_rehighlight (dpyinfo)
6034 struct x_display_info *dpyinfo;
6035{
6036 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6037
6038 if (dpyinfo->x_focus_frame)
6d4238f3 6039 {
0f941935
KH
6040 dpyinfo->x_highlight_frame
6041 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6042 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6043 : dpyinfo->x_focus_frame);
6044 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
f451eb13 6045 {
0f941935
KH
6046 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6047 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
f451eb13 6048 }
dc6f92b8 6049 }
6d4238f3 6050 else
0f941935 6051 dpyinfo->x_highlight_frame = 0;
dc6f92b8 6052
0f941935 6053 if (dpyinfo->x_highlight_frame != old_highlight)
6d4238f3
JB
6054 {
6055 if (old_highlight)
f676886a 6056 frame_unhighlight (old_highlight);
0f941935
KH
6057 if (dpyinfo->x_highlight_frame)
6058 frame_highlight (dpyinfo->x_highlight_frame);
6d4238f3 6059 }
dc6f92b8 6060}
06a2c219
GM
6061
6062
dc6f92b8 6063\f
06a2c219 6064/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
dc6f92b8 6065
28430d3c
JB
6066/* Initialize mode_switch_bit and modifier_meaning. */
6067static void
334208b7
RS
6068x_find_modifier_meanings (dpyinfo)
6069 struct x_display_info *dpyinfo;
28430d3c 6070{
f689eb05 6071 int min_code, max_code;
28430d3c
JB
6072 KeySym *syms;
6073 int syms_per_code;
6074 XModifierKeymap *mods;
6075
334208b7
RS
6076 dpyinfo->meta_mod_mask = 0;
6077 dpyinfo->shift_lock_mask = 0;
6078 dpyinfo->alt_mod_mask = 0;
6079 dpyinfo->super_mod_mask = 0;
6080 dpyinfo->hyper_mod_mask = 0;
58769bee 6081
9658a521 6082#ifdef HAVE_X11R4
334208b7 6083 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
9658a521 6084#else
4a60f8c5
RS
6085 min_code = dpyinfo->display->min_keycode;
6086 max_code = dpyinfo->display->max_keycode;
9658a521
JB
6087#endif
6088
334208b7 6089 syms = XGetKeyboardMapping (dpyinfo->display,
28430d3c
JB
6090 min_code, max_code - min_code + 1,
6091 &syms_per_code);
334208b7 6092 mods = XGetModifierMapping (dpyinfo->display);
28430d3c 6093
58769bee 6094 /* Scan the modifier table to see which modifier bits the Meta and
11edeb03 6095 Alt keysyms are on. */
28430d3c 6096 {
06a2c219 6097 int row, col; /* The row and column in the modifier table. */
28430d3c
JB
6098
6099 for (row = 3; row < 8; row++)
6100 for (col = 0; col < mods->max_keypermod; col++)
6101 {
0299d313
RS
6102 KeyCode code
6103 = mods->modifiermap[(row * mods->max_keypermod) + col];
28430d3c 6104
af92970c
KH
6105 /* Zeroes are used for filler. Skip them. */
6106 if (code == 0)
6107 continue;
6108
28430d3c
JB
6109 /* Are any of this keycode's keysyms a meta key? */
6110 {
6111 int code_col;
6112
6113 for (code_col = 0; code_col < syms_per_code; code_col++)
6114 {
f689eb05 6115 int sym = syms[((code - min_code) * syms_per_code) + code_col];
28430d3c 6116
f689eb05 6117 switch (sym)
28430d3c 6118 {
f689eb05
JB
6119 case XK_Meta_L:
6120 case XK_Meta_R:
334208b7 6121 dpyinfo->meta_mod_mask |= (1 << row);
28430d3c 6122 break;
f689eb05
JB
6123
6124 case XK_Alt_L:
6125 case XK_Alt_R:
334208b7 6126 dpyinfo->alt_mod_mask |= (1 << row);
a3c44b14
RS
6127 break;
6128
6129 case XK_Hyper_L:
6130 case XK_Hyper_R:
334208b7 6131 dpyinfo->hyper_mod_mask |= (1 << row);
a3c44b14
RS
6132 break;
6133
6134 case XK_Super_L:
6135 case XK_Super_R:
334208b7 6136 dpyinfo->super_mod_mask |= (1 << row);
f689eb05 6137 break;
11edeb03
JB
6138
6139 case XK_Shift_Lock:
6140 /* Ignore this if it's not on the lock modifier. */
6141 if ((1 << row) == LockMask)
334208b7 6142 dpyinfo->shift_lock_mask = LockMask;
11edeb03 6143 break;
28430d3c
JB
6144 }
6145 }
6146 }
6147 }
6148 }
6149
f689eb05 6150 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
334208b7 6151 if (! dpyinfo->meta_mod_mask)
a3c44b14 6152 {
334208b7
RS
6153 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6154 dpyinfo->alt_mod_mask = 0;
a3c44b14 6155 }
f689eb05 6156
148c4b70
RS
6157 /* If some keys are both alt and meta,
6158 make them just meta, not alt. */
334208b7 6159 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
148c4b70 6160 {
334208b7 6161 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
148c4b70 6162 }
58769bee 6163
28430d3c 6164 XFree ((char *) syms);
f689eb05 6165 XFreeModifiermap (mods);
28430d3c
JB
6166}
6167
dfeccd2d
JB
6168/* Convert between the modifier bits X uses and the modifier bits
6169 Emacs uses. */
06a2c219 6170
7c5283e4 6171static unsigned int
334208b7
RS
6172x_x_to_emacs_modifiers (dpyinfo, state)
6173 struct x_display_info *dpyinfo;
dc6f92b8
JB
6174 unsigned int state;
6175{
334208b7
RS
6176 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6177 | ((state & ControlMask) ? ctrl_modifier : 0)
6178 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
6179 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
6180 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
6181 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
dc6f92b8
JB
6182}
6183
dfeccd2d 6184static unsigned int
334208b7
RS
6185x_emacs_to_x_modifiers (dpyinfo, state)
6186 struct x_display_info *dpyinfo;
dfeccd2d
JB
6187 unsigned int state;
6188{
334208b7
RS
6189 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
6190 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
6191 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
6192 | ((state & shift_modifier) ? ShiftMask : 0)
6193 | ((state & ctrl_modifier) ? ControlMask : 0)
6194 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
dfeccd2d 6195}
d047c4eb
KH
6196
6197/* Convert a keysym to its name. */
6198
6199char *
6200x_get_keysym_name (keysym)
6201 KeySym keysym;
6202{
6203 char *value;
6204
6205 BLOCK_INPUT;
6206 value = XKeysymToString (keysym);
6207 UNBLOCK_INPUT;
6208
6209 return value;
6210}
06a2c219
GM
6211
6212
e4571a43
JB
6213\f
6214/* Mouse clicks and mouse movement. Rah. */
e4571a43 6215
06a2c219
GM
6216/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6217 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6218 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6219 not force the value into range. */
69388238 6220
c8dba240 6221void
69388238 6222pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
e4571a43 6223 FRAME_PTR f;
69388238 6224 register int pix_x, pix_y;
e4571a43
JB
6225 register int *x, *y;
6226 XRectangle *bounds;
69388238 6227 int noclip;
e4571a43 6228{
06a2c219 6229 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
69388238
RS
6230 even for negative values. */
6231 if (pix_x < 0)
7556890b 6232 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
69388238 6233 if (pix_y < 0)
7556890b 6234 pix_y -= (f)->output_data.x->line_height - 1;
69388238 6235
e4571a43
JB
6236 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6237 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6238
6239 if (bounds)
6240 {
7556890b
RS
6241 bounds->width = FONT_WIDTH (f->output_data.x->font);
6242 bounds->height = f->output_data.x->line_height;
e4571a43
JB
6243 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6244 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6245 }
6246
69388238
RS
6247 if (!noclip)
6248 {
6249 if (pix_x < 0)
6250 pix_x = 0;
3cbd2e0b
RS
6251 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6252 pix_x = FRAME_WINDOW_WIDTH (f);
69388238
RS
6253
6254 if (pix_y < 0)
6255 pix_y = 0;
6256 else if (pix_y > f->height)
6257 pix_y = f->height;
6258 }
e4571a43
JB
6259
6260 *x = pix_x;
6261 *y = pix_y;
6262}
6263
06a2c219
GM
6264
6265/* Given HPOS/VPOS in the current matrix of W, return corresponding
6266 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6267 can't tell the positions because W's display is not up to date,
6268 return 0. */
6269
6270int
6271glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6272 struct window *w;
6273 int hpos, vpos;
6274 int *frame_x, *frame_y;
2b5c9e71 6275{
06a2c219
GM
6276 int success_p;
6277
6278 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6279 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6280
6281 if (display_completed)
6282 {
6283 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6284 struct glyph *glyph = row->glyphs[TEXT_AREA];
6285 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6286
6287 *frame_y = row->y;
6288 *frame_x = row->x;
6289 while (glyph < end)
6290 {
6291 *frame_x += glyph->pixel_width;
6292 ++glyph;
6293 }
6294
6295 success_p = 1;
6296 }
6297 else
6298 {
6299 *frame_y = *frame_x = 0;
6300 success_p = 0;
6301 }
6302
6303 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6304 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6305 return success_p;
2b5c9e71
RS
6306}
6307
06a2c219 6308
dc6f92b8
JB
6309/* Prepare a mouse-event in *RESULT for placement in the input queue.
6310
6311 If the event is a button press, then note that we have grabbed
f451eb13 6312 the mouse. */
dc6f92b8
JB
6313
6314static Lisp_Object
f451eb13 6315construct_mouse_click (result, event, f)
dc6f92b8
JB
6316 struct input_event *result;
6317 XButtonEvent *event;
f676886a 6318 struct frame *f;
dc6f92b8 6319{
f451eb13 6320 /* Make the event type no_event; we'll change that when we decide
dc6f92b8 6321 otherwise. */
f451eb13 6322 result->kind = mouse_click;
69388238 6323 result->code = event->button - Button1;
1113d9db 6324 result->timestamp = event->time;
334208b7
RS
6325 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6326 event->state)
f689eb05 6327 | (event->type == ButtonRelease
58769bee 6328 ? up_modifier
f689eb05 6329 : down_modifier));
dc6f92b8 6330
06a2c219
GM
6331 XSETINT (result->x, event->x);
6332 XSETINT (result->y, event->y);
6333 XSETFRAME (result->frame_or_window, f);
0f8aabe9 6334 result->arg = Qnil;
06a2c219 6335 return Qnil;
dc6f92b8 6336}
b849c413 6337
69388238 6338\f
90e65f07
JB
6339/* Function to report a mouse movement to the mainstream Emacs code.
6340 The input handler calls this.
6341
6342 We have received a mouse movement event, which is given in *event.
6343 If the mouse is over a different glyph than it was last time, tell
6344 the mainstream emacs code by setting mouse_moved. If not, ask for
6345 another motion event, so we can check again the next time it moves. */
b8009dd1 6346
06a2c219
GM
6347static XMotionEvent last_mouse_motion_event;
6348static Lisp_Object last_mouse_motion_frame;
6349
90e65f07 6350static void
12ba150f 6351note_mouse_movement (frame, event)
f676886a 6352 FRAME_PTR frame;
90e65f07 6353 XMotionEvent *event;
90e65f07 6354{
e5d77022 6355 last_mouse_movement_time = event->time;
06a2c219
GM
6356 last_mouse_motion_event = *event;
6357 XSETFRAME (last_mouse_motion_frame, frame);
e5d77022 6358
27f338af
RS
6359 if (event->window != FRAME_X_WINDOW (frame))
6360 {
39d8bb4d 6361 frame->mouse_moved = 1;
27f338af 6362 last_mouse_scroll_bar = Qnil;
27f338af 6363 note_mouse_highlight (frame, -1, -1);
27f338af
RS
6364 }
6365
90e65f07 6366 /* Has the mouse moved off the glyph it was on at the last sighting? */
27f338af
RS
6367 else if (event->x < last_mouse_glyph.x
6368 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6369 || event->y < last_mouse_glyph.y
6370 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
12ba150f 6371 {
39d8bb4d 6372 frame->mouse_moved = 1;
ab648270 6373 last_mouse_scroll_bar = Qnil;
b8009dd1 6374 note_mouse_highlight (frame, event->x, event->y);
90e65f07
JB
6375 }
6376}
6377
bf1c0ba1 6378/* This is used for debugging, to turn off note_mouse_highlight. */
bf1c0ba1 6379
06a2c219
GM
6380 int disable_mouse_highlight;
6381
6382
6383\f
6384/************************************************************************
6385 Mouse Face
6386 ************************************************************************/
6387
6388/* Find the glyph under window-relative coordinates X/Y in window W.
6389 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6390 strings. Return in *HPOS and *VPOS the row and column number of
6391 the glyph found. Return in *AREA the glyph area containing X.
6392 Value is a pointer to the glyph found or null if X/Y is not on
6393 text, or we can't tell because W's current matrix is not up to
6394 date. */
6395
6396static struct glyph *
6397x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6398 struct window *w;
6399 int x, y;
6400 int *hpos, *vpos, *area;
6401{
6402 struct glyph *glyph, *end;
3e71d8f2 6403 struct glyph_row *row = NULL;
06a2c219
GM
6404 int x0, i, left_area_width;
6405
6406 /* Find row containing Y. Give up if some row is not enabled. */
6407 for (i = 0; i < w->current_matrix->nrows; ++i)
6408 {
6409 row = MATRIX_ROW (w->current_matrix, i);
6410 if (!row->enabled_p)
6411 return NULL;
6412 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6413 break;
6414 }
6415
6416 *vpos = i;
6417 *hpos = 0;
6418
6419 /* Give up if Y is not in the window. */
6420 if (i == w->current_matrix->nrows)
6421 return NULL;
6422
6423 /* Get the glyph area containing X. */
6424 if (w->pseudo_window_p)
6425 {
6426 *area = TEXT_AREA;
6427 x0 = 0;
6428 }
6429 else
6430 {
6431 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6432 if (x < left_area_width)
6433 {
6434 *area = LEFT_MARGIN_AREA;
6435 x0 = 0;
6436 }
6437 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6438 {
6439 *area = TEXT_AREA;
6440 x0 = row->x + left_area_width;
6441 }
6442 else
6443 {
6444 *area = RIGHT_MARGIN_AREA;
6445 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6446 }
6447 }
6448
6449 /* Find glyph containing X. */
6450 glyph = row->glyphs[*area];
6451 end = glyph + row->used[*area];
6452 while (glyph < end)
6453 {
6454 if (x < x0 + glyph->pixel_width)
6455 {
6456 if (w->pseudo_window_p)
6457 break;
6458 else if (BUFFERP (glyph->object))
6459 break;
6460 }
6461
6462 x0 += glyph->pixel_width;
6463 ++glyph;
6464 }
6465
6466 if (glyph == end)
6467 return NULL;
6468
6469 *hpos = glyph - row->glyphs[*area];
6470 return glyph;
6471}
6472
6473
6474/* Convert frame-relative x/y to coordinates relative to window W.
6475 Takes pseudo-windows into account. */
6476
6477static void
6478frame_to_window_pixel_xy (w, x, y)
6479 struct window *w;
6480 int *x, *y;
6481{
6482 if (w->pseudo_window_p)
6483 {
6484 /* A pseudo-window is always full-width, and starts at the
6485 left edge of the frame, plus a frame border. */
6486 struct frame *f = XFRAME (w->frame);
6487 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6488 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6489 }
6490 else
6491 {
6492 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6493 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6494 }
6495}
6496
6497
e371a781 6498/* Take proper action when mouse has moved to the mode or header line of
06a2c219
GM
6499 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6500 mode line. X is relative to the start of the text display area of
6501 W, so the width of bitmap areas and scroll bars must be subtracted
6502 to get a position relative to the start of the mode line. */
6503
6504static void
6505note_mode_line_highlight (w, x, mode_line_p)
6506 struct window *w;
6507 int x, mode_line_p;
6508{
6509 struct frame *f = XFRAME (w->frame);
6510 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6511 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6512 struct glyph_row *row;
6513
6514 if (mode_line_p)
6515 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6516 else
045dee35 6517 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
e371a781 6518
06a2c219
GM
6519 if (row->enabled_p)
6520 {
6521 struct glyph *glyph, *end;
6522 Lisp_Object help, map;
6523 int x0;
6524
6525 /* Find the glyph under X. */
6526 glyph = row->glyphs[TEXT_AREA];
6527 end = glyph + row->used[TEXT_AREA];
6528 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
110859fc 6529 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
e371a781 6530
06a2c219
GM
6531 while (glyph < end
6532 && x >= x0 + glyph->pixel_width)
6533 {
6534 x0 += glyph->pixel_width;
6535 ++glyph;
6536 }
6537
6538 if (glyph < end
6539 && STRINGP (glyph->object)
6540 && XSTRING (glyph->object)->intervals
6541 && glyph->charpos >= 0
6542 && glyph->charpos < XSTRING (glyph->object)->size)
6543 {
6544 /* If we're on a string with `help-echo' text property,
6545 arrange for the help to be displayed. This is done by
6546 setting the global variable help_echo to the help string. */
6547 help = Fget_text_property (make_number (glyph->charpos),
6548 Qhelp_echo, glyph->object);
b7e80413 6549 if (!NILP (help))
be010514
GM
6550 {
6551 help_echo = help;
7cea38bc 6552 XSETWINDOW (help_echo_window, w);
be010514
GM
6553 help_echo_object = glyph->object;
6554 help_echo_pos = glyph->charpos;
6555 }
06a2c219
GM
6556
6557 /* Change the mouse pointer according to what is under X/Y. */
6558 map = Fget_text_property (make_number (glyph->charpos),
6559 Qlocal_map, glyph->object);
02067692 6560 if (KEYMAPP (map))
06a2c219 6561 cursor = f->output_data.x->nontext_cursor;
be010514
GM
6562 else
6563 {
6564 map = Fget_text_property (make_number (glyph->charpos),
6565 Qkeymap, glyph->object);
02067692 6566 if (KEYMAPP (map))
be010514
GM
6567 cursor = f->output_data.x->nontext_cursor;
6568 }
06a2c219
GM
6569 }
6570 }
6571
6572 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6573}
6574
6575
6576/* Take proper action when the mouse has moved to position X, Y on
6577 frame F as regards highlighting characters that have mouse-face
6578 properties. Also de-highlighting chars where the mouse was before.
27f338af 6579 X and Y can be negative or out of range. */
b8009dd1
RS
6580
6581static void
6582note_mouse_highlight (f, x, y)
06a2c219 6583 struct frame *f;
c32cdd9a 6584 int x, y;
b8009dd1 6585{
06a2c219
GM
6586 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6587 int portion;
b8009dd1
RS
6588 Lisp_Object window;
6589 struct window *w;
6590
06a2c219
GM
6591 /* When a menu is active, don't highlight because this looks odd. */
6592#ifdef USE_X_TOOLKIT
6593 if (popup_activated ())
6594 return;
6595#endif
6596
04fff9c0
GM
6597 if (disable_mouse_highlight
6598 || !f->glyphs_initialized_p)
bf1c0ba1
RS
6599 return;
6600
06a2c219
GM
6601 dpyinfo->mouse_face_mouse_x = x;
6602 dpyinfo->mouse_face_mouse_y = y;
6603 dpyinfo->mouse_face_mouse_frame = f;
b8009dd1 6604
06a2c219 6605 if (dpyinfo->mouse_face_defer)
b8009dd1
RS
6606 return;
6607
514e4681
RS
6608 if (gc_in_progress)
6609 {
06a2c219 6610 dpyinfo->mouse_face_deferred_gc = 1;
514e4681
RS
6611 return;
6612 }
6613
b8009dd1 6614 /* Which window is that in? */
06a2c219 6615 window = window_from_coordinates (f, x, y, &portion, 1);
b8009dd1
RS
6616
6617 /* If we were displaying active text in another window, clear that. */
06a2c219
GM
6618 if (! EQ (window, dpyinfo->mouse_face_window))
6619 clear_mouse_face (dpyinfo);
6620
6621 /* Not on a window -> return. */
6622 if (!WINDOWP (window))
6623 return;
6624
6625 /* Convert to window-relative pixel coordinates. */
6626 w = XWINDOW (window);
6627 frame_to_window_pixel_xy (w, &x, &y);
6628
9ea173e8 6629 /* Handle tool-bar window differently since it doesn't display a
06a2c219 6630 buffer. */
9ea173e8 6631 if (EQ (window, f->tool_bar_window))
06a2c219 6632 {
9ea173e8 6633 note_tool_bar_highlight (f, x, y);
06a2c219
GM
6634 return;
6635 }
6636
6637 if (portion == 1 || portion == 3)
6638 {
6639 /* Mouse is on the mode or top line. */
6640 note_mode_line_highlight (w, x, portion == 1);
6641 return;
6642 }
e371a781
GM
6643 else if (portion == 2)
6644 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6645 f->output_data.x->horizontal_drag_cursor);
06a2c219
GM
6646 else
6647 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6648 f->output_data.x->text_cursor);
b8009dd1 6649
0cdd0c9f
RS
6650 /* Are we in a window whose display is up to date?
6651 And verify the buffer's text has not changed. */
06a2c219
GM
6652 if (/* Within text portion of the window. */
6653 portion == 0
0cdd0c9f 6654 && EQ (w->window_end_valid, w->buffer)
26459b28
KH
6655 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6656 && (XFASTINT (w->last_overlay_modified)
6657 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
b8009dd1 6658 {
06a2c219
GM
6659 int hpos, vpos, pos, i, area;
6660 struct glyph *glyph;
b8009dd1 6661
06a2c219
GM
6662 /* Find the glyph under X/Y. */
6663 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6664
6665 /* Clear mouse face if X/Y not over text. */
6666 if (glyph == NULL
6667 || area != TEXT_AREA
6668 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
b8009dd1 6669 {
06a2c219
GM
6670 clear_mouse_face (dpyinfo);
6671 return;
6672 }
6673
6674 pos = glyph->charpos;
6675 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6676
6677 /* Check for mouse-face and help-echo. */
6678 {
6679 Lisp_Object mouse_face, overlay, position;
6680 Lisp_Object *overlay_vec;
6681 int len, noverlays;
6682 struct buffer *obuf;
6683 int obegv, ozv;
6684
6685 /* If we get an out-of-range value, return now; avoid an error. */
6686 if (pos > BUF_Z (XBUFFER (w->buffer)))
6687 return;
6688
6689 /* Make the window's buffer temporarily current for
6690 overlays_at and compute_char_face. */
6691 obuf = current_buffer;
6692 current_buffer = XBUFFER (w->buffer);
6693 obegv = BEGV;
6694 ozv = ZV;
6695 BEGV = BEG;
6696 ZV = Z;
6697
6698 /* Is this char mouse-active or does it have help-echo? */
6699 XSETINT (position, pos);
6700
6701 /* Put all the overlays we want in a vector in overlay_vec.
6702 Store the length in len. If there are more than 10, make
6703 enough space for all, and try again. */
6704 len = 10;
6705 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
bc592036 6706 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
06a2c219
GM
6707 if (noverlays > len)
6708 {
6709 len = noverlays;
6710 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
bc592036 6711 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
06a2c219 6712 }
f8349001
GM
6713
6714 /* Sort overlays into increasing priority order. */
06a2c219
GM
6715 noverlays = sort_overlays (overlay_vec, noverlays, w);
6716
6717 /* Check mouse-face highlighting. */
6718 if (! (EQ (window, dpyinfo->mouse_face_window)
6719 && vpos >= dpyinfo->mouse_face_beg_row
6720 && vpos <= dpyinfo->mouse_face_end_row
6721 && (vpos > dpyinfo->mouse_face_beg_row
6722 || hpos >= dpyinfo->mouse_face_beg_col)
6723 && (vpos < dpyinfo->mouse_face_end_row
6724 || hpos < dpyinfo->mouse_face_end_col
6725 || dpyinfo->mouse_face_past_end)))
6726 {
6727 /* Clear the display of the old active region, if any. */
6728 clear_mouse_face (dpyinfo);
6729
6730 /* Find the highest priority overlay that has a mouse-face prop. */
6731 overlay = Qnil;
f8349001 6732 for (i = noverlays - 1; i >= 0; --i)
06a2c219
GM
6733 {
6734 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6735 if (!NILP (mouse_face))
6736 {
6737 overlay = overlay_vec[i];
6738 break;
6739 }
6740 }
6741
6742 /* If no overlay applies, get a text property. */
6743 if (NILP (overlay))
6744 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6745
6746 /* Handle the overlay case. */
6747 if (! NILP (overlay))
6748 {
6749 /* Find the range of text around this char that
6750 should be active. */
6751 Lisp_Object before, after;
6752 int ignore;
6753
6754 before = Foverlay_start (overlay);
6755 after = Foverlay_end (overlay);
6756 /* Record this as the current active region. */
6757 fast_find_position (w, XFASTINT (before),
6758 &dpyinfo->mouse_face_beg_col,
6759 &dpyinfo->mouse_face_beg_row,
6760 &dpyinfo->mouse_face_beg_x,
6761 &dpyinfo->mouse_face_beg_y);
6762 dpyinfo->mouse_face_past_end
6763 = !fast_find_position (w, XFASTINT (after),
6764 &dpyinfo->mouse_face_end_col,
6765 &dpyinfo->mouse_face_end_row,
6766 &dpyinfo->mouse_face_end_x,
6767 &dpyinfo->mouse_face_end_y);
6768 dpyinfo->mouse_face_window = window;
6769 dpyinfo->mouse_face_face_id
6770 = face_at_buffer_position (w, pos, 0, 0,
6771 &ignore, pos + 1, 1);
6772
6773 /* Display it as active. */
6774 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6775 }
6776 /* Handle the text property case. */
6777 else if (! NILP (mouse_face))
6778 {
6779 /* Find the range of text around this char that
6780 should be active. */
6781 Lisp_Object before, after, beginning, end;
6782 int ignore;
6783
6784 beginning = Fmarker_position (w->start);
6785 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6786 - XFASTINT (w->window_end_pos)));
6787 before
6788 = Fprevious_single_property_change (make_number (pos + 1),
6789 Qmouse_face,
6790 w->buffer, beginning);
6791 after
6792 = Fnext_single_property_change (position, Qmouse_face,
6793 w->buffer, end);
6794 /* Record this as the current active region. */
6795 fast_find_position (w, XFASTINT (before),
6796 &dpyinfo->mouse_face_beg_col,
6797 &dpyinfo->mouse_face_beg_row,
6798 &dpyinfo->mouse_face_beg_x,
6799 &dpyinfo->mouse_face_beg_y);
6800 dpyinfo->mouse_face_past_end
6801 = !fast_find_position (w, XFASTINT (after),
6802 &dpyinfo->mouse_face_end_col,
6803 &dpyinfo->mouse_face_end_row,
6804 &dpyinfo->mouse_face_end_x,
6805 &dpyinfo->mouse_face_end_y);
6806 dpyinfo->mouse_face_window = window;
6807 dpyinfo->mouse_face_face_id
6808 = face_at_buffer_position (w, pos, 0, 0,
6809 &ignore, pos + 1, 1);
6810
6811 /* Display it as active. */
6812 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6813 }
6814 }
6815
6816 /* Look for a `help-echo' property. */
6817 {
743934db 6818 Lisp_Object help, overlay;
06a2c219
GM
6819
6820 /* Check overlays first. */
f9b5db02 6821 help = overlay = Qnil;
f8349001 6822 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
743934db
GM
6823 {
6824 overlay = overlay_vec[i];
6825 help = Foverlay_get (overlay, Qhelp_echo);
6826 }
be010514
GM
6827
6828 if (!NILP (help))
6829 {
6830 help_echo = help;
7cea38bc 6831 help_echo_window = window;
743934db 6832 help_echo_object = overlay;
be010514
GM
6833 help_echo_pos = pos;
6834 }
6835 else
6836 {
6837 /* Try text properties. */
6838 if ((STRINGP (glyph->object)
06a2c219
GM
6839 && glyph->charpos >= 0
6840 && glyph->charpos < XSTRING (glyph->object)->size)
6841 || (BUFFERP (glyph->object)
6842 && glyph->charpos >= BEGV
be010514
GM
6843 && glyph->charpos < ZV))
6844 help = Fget_text_property (make_number (glyph->charpos),
6845 Qhelp_echo, glyph->object);
06a2c219 6846
be010514
GM
6847 if (!NILP (help))
6848 {
6849 help_echo = help;
7cea38bc 6850 help_echo_window = window;
be010514
GM
6851 help_echo_object = glyph->object;
6852 help_echo_pos = glyph->charpos;
6853 }
6854 }
06a2c219
GM
6855 }
6856
6857 BEGV = obegv;
6858 ZV = ozv;
6859 current_buffer = obuf;
6860 }
6861 }
6862}
6863
6864static void
6865redo_mouse_highlight ()
6866{
6867 if (!NILP (last_mouse_motion_frame)
6868 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6869 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6870 last_mouse_motion_event.x,
6871 last_mouse_motion_event.y);
6872}
6873
6874
6875\f
6876/***********************************************************************
9ea173e8 6877 Tool-bars
06a2c219
GM
6878 ***********************************************************************/
6879
9ea173e8
GM
6880static int x_tool_bar_item P_ ((struct frame *, int, int,
6881 struct glyph **, int *, int *, int *));
06a2c219 6882
9ea173e8 6883/* Tool-bar item index of the item on which a mouse button was pressed
06a2c219
GM
6884 or -1. */
6885
9ea173e8 6886static int last_tool_bar_item;
06a2c219
GM
6887
6888
9ea173e8
GM
6889/* Get information about the tool-bar item at position X/Y on frame F.
6890 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6891 the current matrix of the tool-bar window of F, or NULL if not
6892 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8daf1204 6893 item in F->tool_bar_items. Value is
06a2c219 6894
9ea173e8 6895 -1 if X/Y is not on a tool-bar item
06a2c219
GM
6896 0 if X/Y is on the same item that was highlighted before.
6897 1 otherwise. */
6898
6899static int
9ea173e8 6900x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
06a2c219
GM
6901 struct frame *f;
6902 int x, y;
6903 struct glyph **glyph;
6904 int *hpos, *vpos, *prop_idx;
6905{
6906 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6907 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6908 int area;
6909
6910 /* Find the glyph under X/Y. */
6911 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6912 if (*glyph == NULL)
6913 return -1;
6914
9ea173e8 6915 /* Get the start of this tool-bar item's properties in
8daf1204 6916 f->tool_bar_items. */
9ea173e8 6917 if (!tool_bar_item_info (f, *glyph, prop_idx))
06a2c219
GM
6918 return -1;
6919
6920 /* Is mouse on the highlighted item? */
9ea173e8 6921 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
06a2c219
GM
6922 && *vpos >= dpyinfo->mouse_face_beg_row
6923 && *vpos <= dpyinfo->mouse_face_end_row
6924 && (*vpos > dpyinfo->mouse_face_beg_row
6925 || *hpos >= dpyinfo->mouse_face_beg_col)
6926 && (*vpos < dpyinfo->mouse_face_end_row
6927 || *hpos < dpyinfo->mouse_face_end_col
6928 || dpyinfo->mouse_face_past_end))
6929 return 0;
6930
6931 return 1;
6932}
6933
6934
9ea173e8 6935/* Handle mouse button event on the tool-bar of frame F, at
06a2c219
GM
6936 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6937 or ButtonRelase. */
6938
6939static void
9ea173e8 6940x_handle_tool_bar_click (f, button_event)
06a2c219
GM
6941 struct frame *f;
6942 XButtonEvent *button_event;
6943{
6944 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6945 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6946 int hpos, vpos, prop_idx;
6947 struct glyph *glyph;
6948 Lisp_Object enabled_p;
6949 int x = button_event->x;
6950 int y = button_event->y;
6951
9ea173e8 6952 /* If not on the highlighted tool-bar item, return. */
06a2c219 6953 frame_to_window_pixel_xy (w, &x, &y);
9ea173e8 6954 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
06a2c219
GM
6955 return;
6956
6957 /* If item is disabled, do nothing. */
8daf1204 6958 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
06a2c219
GM
6959 if (NILP (enabled_p))
6960 return;
6961
6962 if (button_event->type == ButtonPress)
6963 {
6964 /* Show item in pressed state. */
6965 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6966 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9ea173e8 6967 last_tool_bar_item = prop_idx;
06a2c219
GM
6968 }
6969 else
6970 {
6971 Lisp_Object key, frame;
6972 struct input_event event;
6973
6974 /* Show item in released state. */
6975 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6976 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6977
8daf1204 6978 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
06a2c219
GM
6979
6980 XSETFRAME (frame, f);
9ea173e8 6981 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
6982 event.frame_or_window = frame;
6983 event.arg = frame;
06a2c219
GM
6984 kbd_buffer_store_event (&event);
6985
9ea173e8 6986 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
6987 event.frame_or_window = frame;
6988 event.arg = key;
06a2c219
GM
6989 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6990 button_event->state);
6991 kbd_buffer_store_event (&event);
9ea173e8 6992 last_tool_bar_item = -1;
06a2c219
GM
6993 }
6994}
6995
6996
9ea173e8
GM
6997/* Possibly highlight a tool-bar item on frame F when mouse moves to
6998 tool-bar window-relative coordinates X/Y. Called from
06a2c219
GM
6999 note_mouse_highlight. */
7000
7001static void
9ea173e8 7002note_tool_bar_highlight (f, x, y)
06a2c219
GM
7003 struct frame *f;
7004 int x, y;
7005{
9ea173e8 7006 Lisp_Object window = f->tool_bar_window;
06a2c219
GM
7007 struct window *w = XWINDOW (window);
7008 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7009 int hpos, vpos;
7010 struct glyph *glyph;
7011 struct glyph_row *row;
5c187dee 7012 int i;
06a2c219
GM
7013 Lisp_Object enabled_p;
7014 int prop_idx;
7015 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
5c187dee 7016 int mouse_down_p, rc;
06a2c219
GM
7017
7018 /* Function note_mouse_highlight is called with negative x(y
7019 values when mouse moves outside of the frame. */
7020 if (x <= 0 || y <= 0)
7021 {
7022 clear_mouse_face (dpyinfo);
7023 return;
7024 }
7025
9ea173e8 7026 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
06a2c219
GM
7027 if (rc < 0)
7028 {
9ea173e8 7029 /* Not on tool-bar item. */
06a2c219
GM
7030 clear_mouse_face (dpyinfo);
7031 return;
7032 }
7033 else if (rc == 0)
9ea173e8 7034 /* On same tool-bar item as before. */
06a2c219 7035 goto set_help_echo;
b8009dd1 7036
06a2c219
GM
7037 clear_mouse_face (dpyinfo);
7038
9ea173e8 7039 /* Mouse is down, but on different tool-bar item? */
06a2c219
GM
7040 mouse_down_p = (dpyinfo->grabbed
7041 && f == last_mouse_frame
7042 && FRAME_LIVE_P (f));
7043 if (mouse_down_p
9ea173e8 7044 && last_tool_bar_item != prop_idx)
06a2c219
GM
7045 return;
7046
7047 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7048 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7049
9ea173e8 7050 /* If tool-bar item is not enabled, don't highlight it. */
8daf1204 7051 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
06a2c219
GM
7052 if (!NILP (enabled_p))
7053 {
7054 /* Compute the x-position of the glyph. In front and past the
7055 image is a space. We include this is the highlighted area. */
7056 row = MATRIX_ROW (w->current_matrix, vpos);
7057 for (i = x = 0; i < hpos; ++i)
7058 x += row->glyphs[TEXT_AREA][i].pixel_width;
7059
7060 /* Record this as the current active region. */
7061 dpyinfo->mouse_face_beg_col = hpos;
7062 dpyinfo->mouse_face_beg_row = vpos;
7063 dpyinfo->mouse_face_beg_x = x;
7064 dpyinfo->mouse_face_beg_y = row->y;
7065 dpyinfo->mouse_face_past_end = 0;
7066
7067 dpyinfo->mouse_face_end_col = hpos + 1;
7068 dpyinfo->mouse_face_end_row = vpos;
7069 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7070 dpyinfo->mouse_face_end_y = row->y;
7071 dpyinfo->mouse_face_window = window;
9ea173e8 7072 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
06a2c219
GM
7073
7074 /* Display it as active. */
7075 show_mouse_face (dpyinfo, draw);
7076 dpyinfo->mouse_face_image_state = draw;
b8009dd1 7077 }
06a2c219
GM
7078
7079 set_help_echo:
7080
9ea173e8 7081 /* Set help_echo to a help string.to display for this tool-bar item.
06a2c219 7082 XTread_socket does the rest. */
7cea38bc 7083 help_echo_object = help_echo_window = Qnil;
be010514 7084 help_echo_pos = -1;
8daf1204 7085 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
b7e80413 7086 if (NILP (help_echo))
8daf1204 7087 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
b8009dd1 7088}
4d73d038 7089
06a2c219
GM
7090
7091\f
7092/* Find the glyph matrix position of buffer position POS in window W.
7093 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7094 current glyphs must be up to date. If POS is above window start
7095 return (0, 0, 0, 0). If POS is after end of W, return end of
7096 last line in W. */
b8009dd1
RS
7097
7098static int
06a2c219
GM
7099fast_find_position (w, pos, hpos, vpos, x, y)
7100 struct window *w;
b8009dd1 7101 int pos;
06a2c219 7102 int *hpos, *vpos, *x, *y;
b8009dd1 7103{
b8009dd1 7104 int i;
bf1c0ba1 7105 int lastcol;
06a2c219
GM
7106 int maybe_next_line_p = 0;
7107 int line_start_position;
7108 int yb = window_text_bottom_y (w);
7109 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
7110 struct glyph_row *best_row = row;
7111 int row_vpos = 0, best_row_vpos = 0;
7112 int current_x;
7113
7114 while (row->y < yb)
b8009dd1 7115 {
06a2c219
GM
7116 if (row->used[TEXT_AREA])
7117 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7118 else
7119 line_start_position = 0;
7120
7121 if (line_start_position > pos)
b8009dd1 7122 break;
77b68646
RS
7123 /* If the position sought is the end of the buffer,
7124 don't include the blank lines at the bottom of the window. */
06a2c219
GM
7125 else if (line_start_position == pos
7126 && pos == BUF_ZV (XBUFFER (w->buffer)))
77b68646 7127 {
06a2c219 7128 maybe_next_line_p = 1;
77b68646
RS
7129 break;
7130 }
06a2c219
GM
7131 else if (line_start_position > 0)
7132 {
7133 best_row = row;
7134 best_row_vpos = row_vpos;
7135 }
4b0bb6f3
GM
7136
7137 if (row->y + row->height >= yb)
7138 break;
06a2c219
GM
7139
7140 ++row;
7141 ++row_vpos;
b8009dd1 7142 }
06a2c219
GM
7143
7144 /* Find the right column within BEST_ROW. */
7145 lastcol = 0;
7146 current_x = best_row->x;
7147 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
bf1c0ba1 7148 {
06a2c219
GM
7149 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7150 int charpos;
7151
7152 charpos = glyph->charpos;
7153 if (charpos == pos)
bf1c0ba1 7154 {
06a2c219
GM
7155 *hpos = i;
7156 *vpos = best_row_vpos;
7157 *x = current_x;
7158 *y = best_row->y;
bf1c0ba1
RS
7159 return 1;
7160 }
06a2c219 7161 else if (charpos > pos)
4d73d038 7162 break;
06a2c219
GM
7163 else if (charpos > 0)
7164 lastcol = i;
7165
7166 current_x += glyph->pixel_width;
bf1c0ba1 7167 }
b8009dd1 7168
77b68646
RS
7169 /* If we're looking for the end of the buffer,
7170 and we didn't find it in the line we scanned,
7171 use the start of the following line. */
06a2c219 7172 if (maybe_next_line_p)
77b68646 7173 {
06a2c219
GM
7174 ++best_row;
7175 ++best_row_vpos;
7176 lastcol = 0;
7177 current_x = best_row->x;
77b68646
RS
7178 }
7179
06a2c219
GM
7180 *vpos = best_row_vpos;
7181 *hpos = lastcol + 1;
7182 *x = current_x;
7183 *y = best_row->y;
b8009dd1
RS
7184 return 0;
7185}
7186
06a2c219 7187
b8009dd1
RS
7188/* Display the active region described by mouse_face_*
7189 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7190
7191static void
06a2c219 7192show_mouse_face (dpyinfo, draw)
7a13e894 7193 struct x_display_info *dpyinfo;
06a2c219 7194 enum draw_glyphs_face draw;
b8009dd1 7195{
7a13e894 7196 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
06a2c219 7197 struct frame *f = XFRAME (WINDOW_FRAME (w));
b8009dd1 7198 int i;
06a2c219
GM
7199 int cursor_off_p = 0;
7200 struct cursor_pos saved_cursor;
7201
7202 saved_cursor = output_cursor;
7203
7204 /* If window is in the process of being destroyed, don't bother
7205 to do anything. */
7206 if (w->current_matrix == NULL)
7207 goto set_x_cursor;
7208
7209 /* Recognize when we are called to operate on rows that don't exist
7210 anymore. This can happen when a window is split. */
7211 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
7212 goto set_x_cursor;
7213
7214 set_output_cursor (&w->phys_cursor);
7215
7216 /* Note that mouse_face_beg_row etc. are window relative. */
7217 for (i = dpyinfo->mouse_face_beg_row;
7218 i <= dpyinfo->mouse_face_end_row;
7219 i++)
7220 {
7221 int start_hpos, end_hpos, start_x;
7222 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
7223
7224 /* Don't do anything if row doesn't have valid contents. */
7225 if (!row->enabled_p)
7226 continue;
7227
7228 /* For all but the first row, the highlight starts at column 0. */
7229 if (i == dpyinfo->mouse_face_beg_row)
7230 {
7231 start_hpos = dpyinfo->mouse_face_beg_col;
7232 start_x = dpyinfo->mouse_face_beg_x;
7233 }
7234 else
7235 {
7236 start_hpos = 0;
7237 start_x = 0;
7238 }
7239
7240 if (i == dpyinfo->mouse_face_end_row)
7241 end_hpos = dpyinfo->mouse_face_end_col;
7242 else
7243 end_hpos = row->used[TEXT_AREA];
7244
7245 /* If the cursor's in the text we are about to rewrite, turn the
7246 cursor off. */
7247 if (!w->pseudo_window_p
7248 && i == output_cursor.vpos
7249 && output_cursor.hpos >= start_hpos - 1
7250 && output_cursor.hpos <= end_hpos)
514e4681 7251 {
06a2c219
GM
7252 x_update_window_cursor (w, 0);
7253 cursor_off_p = 1;
514e4681 7254 }
b8009dd1 7255
06a2c219 7256 if (end_hpos > start_hpos)
64f26cf5
GM
7257 {
7258 row->mouse_face_p = draw == DRAW_MOUSE_FACE;
7259 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7260 start_hpos, end_hpos, draw, NULL, NULL, 0);
7261 }
b8009dd1
RS
7262 }
7263
514e4681 7264 /* If we turned the cursor off, turn it back on. */
06a2c219
GM
7265 if (cursor_off_p)
7266 x_display_cursor (w, 1,
7267 output_cursor.hpos, output_cursor.vpos,
7268 output_cursor.x, output_cursor.y);
2729a2b5 7269
06a2c219 7270 output_cursor = saved_cursor;
fb3b7de5 7271
06a2c219
GM
7272 set_x_cursor:
7273
7274 /* Change the mouse cursor. */
7275 if (draw == DRAW_NORMAL_TEXT)
7276 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7277 f->output_data.x->text_cursor);
7278 else if (draw == DRAW_MOUSE_FACE)
334208b7 7279 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 7280 f->output_data.x->cross_cursor);
27ead1d5 7281 else
334208b7 7282 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
06a2c219 7283 f->output_data.x->nontext_cursor);
b8009dd1
RS
7284}
7285
7286/* Clear out the mouse-highlighted active region.
06a2c219 7287 Redraw it un-highlighted first. */
b8009dd1 7288
06a2c219 7289void
7a13e894
RS
7290clear_mouse_face (dpyinfo)
7291 struct x_display_info *dpyinfo;
b8009dd1 7292{
06a2c219
GM
7293 if (tip_frame)
7294 return;
7295
7a13e894 7296 if (! NILP (dpyinfo->mouse_face_window))
06a2c219 7297 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
b8009dd1 7298
7a13e894
RS
7299 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7300 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7301 dpyinfo->mouse_face_window = Qnil;
b8009dd1 7302}
e687d06e 7303
71b8321e
GM
7304
7305/* Clear any mouse-face on window W. This function is part of the
7306 redisplay interface, and is called from try_window_id and similar
7307 functions to ensure the mouse-highlight is off. */
7308
7309static void
7310x_clear_mouse_face (w)
7311 struct window *w;
7312{
7313 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7314 Lisp_Object window;
7315
2e636f9d 7316 BLOCK_INPUT;
71b8321e
GM
7317 XSETWINDOW (window, w);
7318 if (EQ (window, dpyinfo->mouse_face_window))
7319 clear_mouse_face (dpyinfo);
2e636f9d 7320 UNBLOCK_INPUT;
71b8321e
GM
7321}
7322
7323
e687d06e
RS
7324/* Just discard the mouse face information for frame F, if any.
7325 This is used when the size of F is changed. */
7326
dfcf069d 7327void
e687d06e
RS
7328cancel_mouse_face (f)
7329 FRAME_PTR f;
7330{
7331 Lisp_Object window;
7332 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7333
7334 window = dpyinfo->mouse_face_window;
7335 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7336 {
7337 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7338 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7339 dpyinfo->mouse_face_window = Qnil;
7340 }
7341}
b8009dd1 7342\f
ab648270
JB
7343static struct scroll_bar *x_window_to_scroll_bar ();
7344static void x_scroll_bar_report_motion ();
12ba150f 7345
90e65f07 7346/* Return the current position of the mouse.
2d7fc7e8 7347 *fp should be a frame which indicates which display to ask about.
90e65f07 7348
2d7fc7e8 7349 If the mouse movement started in a scroll bar, set *fp, *bar_window,
ab648270 7350 and *part to the frame, window, and scroll bar part that the mouse
12ba150f 7351 is over. Set *x and *y to the portion and whole of the mouse's
ab648270 7352 position on the scroll bar.
12ba150f 7353
2d7fc7e8 7354 If the mouse movement started elsewhere, set *fp to the frame the
12ba150f
JB
7355 mouse is on, *bar_window to nil, and *x and *y to the character cell
7356 the mouse is over.
7357
06a2c219 7358 Set *time to the server time-stamp for the time at which the mouse
12ba150f
JB
7359 was at this position.
7360
a135645a
RS
7361 Don't store anything if we don't have a valid set of values to report.
7362
90e65f07 7363 This clears the mouse_moved flag, so we can wait for the next mouse
f5bb65ec 7364 movement. */
90e65f07
JB
7365
7366static void
1cf412ec 7367XTmouse_position (fp, insist, bar_window, part, x, y, time)
334208b7 7368 FRAME_PTR *fp;
1cf412ec 7369 int insist;
12ba150f 7370 Lisp_Object *bar_window;
ab648270 7371 enum scroll_bar_part *part;
90e65f07 7372 Lisp_Object *x, *y;
e5d77022 7373 unsigned long *time;
90e65f07 7374{
a135645a
RS
7375 FRAME_PTR f1;
7376
90e65f07
JB
7377 BLOCK_INPUT;
7378
8bcee03e 7379 if (! NILP (last_mouse_scroll_bar) && insist == 0)
334208b7 7380 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
90e65f07
JB
7381 else
7382 {
12ba150f
JB
7383 Window root;
7384 int root_x, root_y;
90e65f07 7385
12ba150f
JB
7386 Window dummy_window;
7387 int dummy;
7388
39d8bb4d
KH
7389 Lisp_Object frame, tail;
7390
7391 /* Clear the mouse-moved flag for every frame on this display. */
7392 FOR_EACH_FRAME (tail, frame)
7393 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7394 XFRAME (frame)->mouse_moved = 0;
7395
ab648270 7396 last_mouse_scroll_bar = Qnil;
12ba150f
JB
7397
7398 /* Figure out which root window we're on. */
334208b7
RS
7399 XQueryPointer (FRAME_X_DISPLAY (*fp),
7400 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
12ba150f
JB
7401
7402 /* The root window which contains the pointer. */
7403 &root,
7404
7405 /* Trash which we can't trust if the pointer is on
7406 a different screen. */
7407 &dummy_window,
7408
7409 /* The position on that root window. */
58769bee 7410 &root_x, &root_y,
12ba150f
JB
7411
7412 /* More trash we can't trust. */
7413 &dummy, &dummy,
7414
7415 /* Modifier keys and pointer buttons, about which
7416 we don't care. */
7417 (unsigned int *) &dummy);
7418
7419 /* Now we have a position on the root; find the innermost window
7420 containing the pointer. */
7421 {
7422 Window win, child;
7423 int win_x, win_y;
06a2c219 7424 int parent_x = 0, parent_y = 0;
e99db5a1 7425 int count;
12ba150f
JB
7426
7427 win = root;
69388238 7428
2d7fc7e8
RS
7429 /* XTranslateCoordinates can get errors if the window
7430 structure is changing at the same time this function
7431 is running. So at least we must not crash from them. */
7432
e99db5a1 7433 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
2d7fc7e8 7434
334208b7 7435 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
23faf38f 7436 && FRAME_LIVE_P (last_mouse_frame))
12ba150f 7437 {
69388238
RS
7438 /* If mouse was grabbed on a frame, give coords for that frame
7439 even if the mouse is now outside it. */
334208b7 7440 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
69388238 7441
12ba150f 7442 /* From-window, to-window. */
69388238 7443 root, FRAME_X_WINDOW (last_mouse_frame),
12ba150f
JB
7444
7445 /* From-position, to-position. */
7446 root_x, root_y, &win_x, &win_y,
7447
7448 /* Child of win. */
7449 &child);
69388238
RS
7450 f1 = last_mouse_frame;
7451 }
7452 else
7453 {
7454 while (1)
7455 {
334208b7 7456 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
12ba150f 7457
69388238
RS
7458 /* From-window, to-window. */
7459 root, win,
12ba150f 7460
69388238
RS
7461 /* From-position, to-position. */
7462 root_x, root_y, &win_x, &win_y,
7463
7464 /* Child of win. */
7465 &child);
7466
9af3143a 7467 if (child == None || child == win)
69388238
RS
7468 break;
7469
7470 win = child;
7471 parent_x = win_x;
7472 parent_y = win_y;
7473 }
12ba150f 7474
69388238
RS
7475 /* Now we know that:
7476 win is the innermost window containing the pointer
7477 (XTC says it has no child containing the pointer),
7478 win_x and win_y are the pointer's position in it
7479 (XTC did this the last time through), and
7480 parent_x and parent_y are the pointer's position in win's parent.
7481 (They are what win_x and win_y were when win was child.
7482 If win is the root window, it has no parent, and
7483 parent_{x,y} are invalid, but that's okay, because we'll
7484 never use them in that case.) */
7485
7486 /* Is win one of our frames? */
19126e11 7487 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
aad0f6ab
GM
7488
7489#ifdef USE_X_TOOLKIT
7490 /* If we end up with the menu bar window, say it's not
7491 on the frame. */
7492 if (f1 != NULL
7493 && f1->output_data.x->menubar_widget
7494 && win == XtWindow (f1->output_data.x->menubar_widget))
7495 f1 = NULL;
7496#endif /* USE_X_TOOLKIT */
69388238 7497 }
58769bee 7498
2d7fc7e8
RS
7499 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7500 f1 = 0;
7501
e99db5a1 7502 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
2d7fc7e8 7503
ab648270 7504 /* If not, is it one of our scroll bars? */
a135645a 7505 if (! f1)
12ba150f 7506 {
ab648270 7507 struct scroll_bar *bar = x_window_to_scroll_bar (win);
12ba150f
JB
7508
7509 if (bar)
7510 {
a135645a 7511 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
7512 win_x = parent_x;
7513 win_y = parent_y;
7514 }
7515 }
90e65f07 7516
8bcee03e 7517 if (f1 == 0 && insist > 0)
b86bd3dd 7518 f1 = SELECTED_FRAME ();
1cf412ec 7519
a135645a 7520 if (f1)
12ba150f 7521 {
06a2c219
GM
7522 /* Ok, we found a frame. Store all the values.
7523 last_mouse_glyph is a rectangle used to reduce the
7524 generation of mouse events. To not miss any motion
7525 events, we must divide the frame into rectangles of the
7526 size of the smallest character that could be displayed
7527 on it, i.e. into the same rectangles that matrices on
7528 the frame are divided into. */
7529
7530#if OLD_REDISPLAY_CODE
2b5c9e71 7531 int ignore1, ignore2;
2b5c9e71 7532 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
334208b7 7533 &last_mouse_glyph,
1cf412ec
RS
7534 FRAME_X_DISPLAY_INFO (f1)->grabbed
7535 || insist);
06a2c219
GM
7536#else
7537 {
7538 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7539 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7540 int x = win_x;
7541 int y = win_y;
7542
7543 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7544 round down even for negative values. */
7545 if (x < 0)
7546 x -= width - 1;
7547 if (y < 0)
7548 y -= height - 1;
7549
7550 last_mouse_glyph.width = width;
7551 last_mouse_glyph.height = height;
7552 last_mouse_glyph.x = (x + width - 1) / width * width;
7553 last_mouse_glyph.y = (y + height - 1) / height * height;
7554 }
7555#endif
12ba150f
JB
7556
7557 *bar_window = Qnil;
7558 *part = 0;
334208b7 7559 *fp = f1;
e0c1aef2
KH
7560 XSETINT (*x, win_x);
7561 XSETINT (*y, win_y);
12ba150f
JB
7562 *time = last_mouse_movement_time;
7563 }
7564 }
7565 }
90e65f07
JB
7566
7567 UNBLOCK_INPUT;
7568}
f451eb13 7569
06a2c219 7570
06a2c219 7571#ifdef USE_X_TOOLKIT
bffcfca9
GM
7572
7573/* Atimer callback function for TIMER. Called every 0.1s to process
7574 Xt timeouts, if needed. We must avoid calling XtAppPending as
7575 much as possible because that function does an implicit XFlush
7576 that slows us down. */
7577
7578static void
7579x_process_timeouts (timer)
7580 struct atimer *timer;
7581{
7582 if (toolkit_scroll_bar_interaction || popup_activated_flag)
7583 {
7584 BLOCK_INPUT;
7585 while (XtAppPending (Xt_app_con) & XtIMTimer)
7586 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7587 UNBLOCK_INPUT;
7588 }
06a2c219
GM
7589}
7590
bffcfca9 7591#endif /* USE_X_TOOLKIT */
06a2c219
GM
7592
7593\f
7594/* Scroll bar support. */
7595
7596/* Given an X window ID, find the struct scroll_bar which manages it.
7597 This can be called in GC, so we have to make sure to strip off mark
7598 bits. */
bffcfca9 7599
06a2c219
GM
7600static struct scroll_bar *
7601x_window_to_scroll_bar (window_id)
7602 Window window_id;
7603{
7604 Lisp_Object tail;
7605
7606 for (tail = Vframe_list;
7607 XGCTYPE (tail) == Lisp_Cons;
8e713be6 7608 tail = XCDR (tail))
06a2c219
GM
7609 {
7610 Lisp_Object frame, bar, condemned;
7611
8e713be6 7612 frame = XCAR (tail);
06a2c219
GM
7613 /* All elements of Vframe_list should be frames. */
7614 if (! GC_FRAMEP (frame))
7615 abort ();
7616
7617 /* Scan this frame's scroll bar list for a scroll bar with the
7618 right window ID. */
7619 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7620 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7621 /* This trick allows us to search both the ordinary and
7622 condemned scroll bar lists with one loop. */
7623 ! GC_NILP (bar) || (bar = condemned,
7624 condemned = Qnil,
7625 ! GC_NILP (bar));
7626 bar = XSCROLL_BAR (bar)->next)
7627 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7628 return XSCROLL_BAR (bar);
7629 }
7630
7631 return 0;
7632}
7633
7634
7635\f
7636/************************************************************************
7637 Toolkit scroll bars
7638 ************************************************************************/
7639
eccc05db 7640#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
7641
7642static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7643static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7644static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7645 struct scroll_bar *));
7646static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7647 int, int, int));
7648
7649
7650/* Id of action hook installed for scroll bars. */
7651
7652static XtActionHookId action_hook_id;
7653
7654/* Lisp window being scrolled. Set when starting to interact with
7655 a toolkit scroll bar, reset to nil when ending the interaction. */
7656
7657static Lisp_Object window_being_scrolled;
7658
7659/* Last scroll bar part sent in xm_scroll_callback. */
7660
7661static int last_scroll_bar_part;
7662
ec18280f
SM
7663/* Whether this is an Xaw with arrow-scrollbars. This should imply
7664 that movements of 1/20 of the screen size are mapped to up/down. */
7665
7666static Boolean xaw3d_arrow_scroll;
7667
7668/* Whether the drag scrolling maintains the mouse at the top of the
7669 thumb. If not, resizing the thumb needs to be done more carefully
7670 to avoid jerkyness. */
7671
7672static Boolean xaw3d_pick_top;
7673
06a2c219
GM
7674
7675/* Action hook installed via XtAppAddActionHook when toolkit scroll
ec18280f 7676 bars are used.. The hook is responsible for detecting when
06a2c219
GM
7677 the user ends an interaction with the scroll bar, and generates
7678 a `end-scroll' scroll_bar_click' event if so. */
7679
7680static void
7681xt_action_hook (widget, client_data, action_name, event, params,
7682 num_params)
7683 Widget widget;
7684 XtPointer client_data;
7685 String action_name;
7686 XEvent *event;
7687 String *params;
7688 Cardinal *num_params;
7689{
7690 int scroll_bar_p;
7691 char *end_action;
7692
7693#ifdef USE_MOTIF
7694 scroll_bar_p = XmIsScrollBar (widget);
7695 end_action = "Release";
ec18280f 7696#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
7697 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7698 end_action = "EndScroll";
ec18280f 7699#endif /* USE_MOTIF */
06a2c219 7700
06a2c219
GM
7701 if (scroll_bar_p
7702 && strcmp (action_name, end_action) == 0
7703 && WINDOWP (window_being_scrolled))
7704 {
7705 struct window *w;
7706
7707 x_send_scroll_bar_event (window_being_scrolled,
7708 scroll_bar_end_scroll, 0, 0);
7709 w = XWINDOW (window_being_scrolled);
7710 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7711 window_being_scrolled = Qnil;
7712 last_scroll_bar_part = -1;
bffcfca9
GM
7713
7714 /* Xt timeouts no longer needed. */
7715 toolkit_scroll_bar_interaction = 0;
06a2c219
GM
7716 }
7717}
7718
07b3d16e
GM
7719/* A vector of windows used for communication between
7720 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
7721
7722static struct window **scroll_bar_windows;
7723static int scroll_bar_windows_size;
7724
06a2c219
GM
7725
7726/* Send a client message with message type Xatom_Scrollbar for a
7727 scroll action to the frame of WINDOW. PART is a value identifying
7728 the part of the scroll bar that was clicked on. PORTION is the
7729 amount to scroll of a whole of WHOLE. */
7730
7731static void
7732x_send_scroll_bar_event (window, part, portion, whole)
7733 Lisp_Object window;
7734 int part, portion, whole;
7735{
7736 XEvent event;
7737 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
07b3d16e
GM
7738 struct window *w = XWINDOW (window);
7739 struct frame *f = XFRAME (w->frame);
7740 int i;
06a2c219 7741
07b3d16e
GM
7742 BLOCK_INPUT;
7743
06a2c219
GM
7744 /* Construct a ClientMessage event to send to the frame. */
7745 ev->type = ClientMessage;
7746 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7747 ev->display = FRAME_X_DISPLAY (f);
7748 ev->window = FRAME_X_WINDOW (f);
7749 ev->format = 32;
07b3d16e
GM
7750
7751 /* We can only transfer 32 bits in the XClientMessageEvent, which is
7752 not enough to store a pointer or Lisp_Object on a 64 bit system.
7753 So, store the window in scroll_bar_windows and pass the index
7754 into that array in the event. */
7755 for (i = 0; i < scroll_bar_windows_size; ++i)
7756 if (scroll_bar_windows[i] == NULL)
7757 break;
7758
7759 if (i == scroll_bar_windows_size)
7760 {
7761 int new_size = max (10, 2 * scroll_bar_windows_size);
7762 size_t nbytes = new_size * sizeof *scroll_bar_windows;
7763 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
7764
7765 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
7766 nbytes);
7767 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
7768 scroll_bar_windows_size = new_size;
7769 }
7770
7771 scroll_bar_windows[i] = w;
7772 ev->data.l[0] = (long) i;
06a2c219
GM
7773 ev->data.l[1] = (long) part;
7774 ev->data.l[2] = (long) 0;
7775 ev->data.l[3] = (long) portion;
7776 ev->data.l[4] = (long) whole;
7777
bffcfca9
GM
7778 /* Make Xt timeouts work while the scroll bar is active. */
7779 toolkit_scroll_bar_interaction = 1;
7780
06a2c219
GM
7781 /* Setting the event mask to zero means that the message will
7782 be sent to the client that created the window, and if that
7783 window no longer exists, no event will be sent. */
06a2c219
GM
7784 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7785 UNBLOCK_INPUT;
7786}
7787
7788
7789/* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7790 in *IEVENT. */
7791
7792static void
7793x_scroll_bar_to_input_event (event, ievent)
7794 XEvent *event;
7795 struct input_event *ievent;
7796{
7797 XClientMessageEvent *ev = (XClientMessageEvent *) event;
52e386c2
KR
7798 Lisp_Object window;
7799 struct frame *f;
07b3d16e
GM
7800 struct window *w;
7801
7802 w = scroll_bar_windows[ev->data.l[0]];
7803 scroll_bar_windows[ev->data.l[0]] = NULL;
52e386c2 7804
07b3d16e
GM
7805 XSETWINDOW (window, w);
7806 f = XFRAME (w->frame);
06a2c219
GM
7807
7808 ievent->kind = scroll_bar_click;
7809 ievent->frame_or_window = window;
0f8aabe9 7810 ievent->arg = Qnil;
06a2c219
GM
7811 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7812 ievent->part = ev->data.l[1];
7813 ievent->code = ev->data.l[2];
7814 ievent->x = make_number ((int) ev->data.l[3]);
7815 ievent->y = make_number ((int) ev->data.l[4]);
7816 ievent->modifiers = 0;
7817}
7818
7819
7820#ifdef USE_MOTIF
7821
7822/* Minimum and maximum values used for Motif scroll bars. */
7823
7824#define XM_SB_MIN 1
7825#define XM_SB_MAX 10000000
7826#define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7827
7828
7829/* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7830 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7831 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7832
7833static void
7834xm_scroll_callback (widget, client_data, call_data)
7835 Widget widget;
7836 XtPointer client_data, call_data;
7837{
7838 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7839 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7840 double percent;
7841 int part = -1, whole = 0, portion = 0;
7842
7843 switch (cs->reason)
7844 {
7845 case XmCR_DECREMENT:
7846 bar->dragging = Qnil;
7847 part = scroll_bar_up_arrow;
7848 break;
7849
7850 case XmCR_INCREMENT:
7851 bar->dragging = Qnil;
7852 part = scroll_bar_down_arrow;
7853 break;
7854
7855 case XmCR_PAGE_DECREMENT:
7856 bar->dragging = Qnil;
7857 part = scroll_bar_above_handle;
7858 break;
7859
7860 case XmCR_PAGE_INCREMENT:
7861 bar->dragging = Qnil;
7862 part = scroll_bar_below_handle;
7863 break;
7864
7865 case XmCR_TO_TOP:
7866 bar->dragging = Qnil;
7867 part = scroll_bar_to_top;
7868 break;
7869
7870 case XmCR_TO_BOTTOM:
7871 bar->dragging = Qnil;
7872 part = scroll_bar_to_bottom;
7873 break;
7874
7875 case XmCR_DRAG:
7876 {
7877 int slider_size;
7878 int dragging_down_p = (INTEGERP (bar->dragging)
7879 && XINT (bar->dragging) <= cs->value);
7880
7881 /* Get the slider size. */
7882 BLOCK_INPUT;
7883 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7884 UNBLOCK_INPUT;
7885
7886 /* At the max position of the scroll bar, do a line-wise
7887 movement. Without doing anything, the LessTif scroll bar
7888 calls us with the same cs->value again and again. If we
7889 want to make sure that we can reach the end of the buffer,
7890 we have to do something.
7891
7892 Implementation note: setting bar->dragging always to
7893 cs->value gives a smoother movement at the max position.
7894 Setting it to nil when doing line-wise movement gives
7895 a better slider behavior. */
7896
7897 if (cs->value + slider_size == XM_SB_MAX
7898 || (dragging_down_p
7899 && last_scroll_bar_part == scroll_bar_down_arrow))
7900 {
7901 part = scroll_bar_down_arrow;
7902 bar->dragging = Qnil;
7903 }
7904 else
7905 {
7906 whole = XM_SB_RANGE;
7907 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7908 part = scroll_bar_handle;
7909 bar->dragging = make_number (cs->value);
7910 }
7911 }
7912 break;
7913
7914 case XmCR_VALUE_CHANGED:
7915 break;
7916 };
7917
7918 if (part >= 0)
7919 {
7920 window_being_scrolled = bar->window;
7921 last_scroll_bar_part = part;
7922 x_send_scroll_bar_event (bar->window, part, portion, whole);
7923 }
7924}
7925
7926
ec18280f 7927#else /* !USE_MOTIF, i.e. Xaw. */
06a2c219
GM
7928
7929
ec18280f 7930/* Xaw scroll bar callback. Invoked when the thumb is dragged.
06a2c219
GM
7931 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7932 scroll bar struct. CALL_DATA is a pointer to a float saying where
7933 the thumb is. */
7934
7935static void
ec18280f 7936xaw_jump_callback (widget, client_data, call_data)
06a2c219
GM
7937 Widget widget;
7938 XtPointer client_data, call_data;
7939{
7940 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7941 float top = *(float *) call_data;
7942 float shown;
ec18280f
SM
7943 int whole, portion, height;
7944 int part;
06a2c219
GM
7945
7946 /* Get the size of the thumb, a value between 0 and 1. */
7947 BLOCK_INPUT;
ec18280f 7948 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
06a2c219
GM
7949 UNBLOCK_INPUT;
7950
7951 whole = 10000000;
7952 portion = shown < 1 ? top * whole : 0;
06a2c219 7953
ec18280f
SM
7954 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
7955 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
7956 the bottom, so we force the scrolling whenever we see that we're
7957 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
7958 we try to ensure that we always stay two pixels away from the
7959 bottom). */
06a2c219
GM
7960 part = scroll_bar_down_arrow;
7961 else
7962 part = scroll_bar_handle;
7963
7964 window_being_scrolled = bar->window;
7965 bar->dragging = make_number (portion);
7966 last_scroll_bar_part = part;
7967 x_send_scroll_bar_event (bar->window, part, portion, whole);
7968}
7969
7970
ec18280f
SM
7971/* Xaw scroll bar callback. Invoked for incremental scrolling.,
7972 i.e. line or page up or down. WIDGET is the Xaw scroll bar
06a2c219
GM
7973 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
7974 the scroll bar. CALL_DATA is an integer specifying the action that
7975 has taken place. It's magnitude is in the range 0..height of the
7976 scroll bar. Negative values mean scroll towards buffer start.
7977 Values < height of scroll bar mean line-wise movement. */
7978
7979static void
ec18280f 7980xaw_scroll_callback (widget, client_data, call_data)
06a2c219
GM
7981 Widget widget;
7982 XtPointer client_data, call_data;
7983{
7984 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7985 int position = (int) call_data;
7986 Dimension height;
7987 int part;
7988
7989 /* Get the height of the scroll bar. */
7990 BLOCK_INPUT;
7991 XtVaGetValues (widget, XtNheight, &height, NULL);
7992 UNBLOCK_INPUT;
7993
ec18280f
SM
7994 if (abs (position) >= height)
7995 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
7996
7997 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
7998 it maps line-movement to call_data = max(5, height/20). */
7999 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8000 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
06a2c219 8001 else
ec18280f 8002 part = scroll_bar_move_ratio;
06a2c219
GM
8003
8004 window_being_scrolled = bar->window;
8005 bar->dragging = Qnil;
8006 last_scroll_bar_part = part;
ec18280f 8007 x_send_scroll_bar_event (bar->window, part, position, height);
06a2c219
GM
8008}
8009
8010
8011#endif /* not USE_MOTIF */
8012
8013
8014/* Create the widget for scroll bar BAR on frame F. Record the widget
8015 and X window of the scroll bar in BAR. */
8016
8017static void
8018x_create_toolkit_scroll_bar (f, bar)
8019 struct frame *f;
8020 struct scroll_bar *bar;
8021{
8022 Window xwindow;
8023 Widget widget;
8024 Arg av[20];
8025 int ac = 0;
8026 char *scroll_bar_name = "verticalScrollBar";
8027 unsigned long pixel;
8028
8029 BLOCK_INPUT;
8030
8031#ifdef USE_MOTIF
8032 /* LessTif 0.85, problems:
8033
8034 1. When the mouse if over the scroll bar, the scroll bar will
8035 get keyboard events. I didn't find a way to turn this off.
8036
8037 2. Do we have to explicitly set the cursor to get an arrow
8038 cursor (see below)? */
8039
8040 /* Set resources. Create the widget. */
8041 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8042 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8043 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8044 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8045 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8046 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8047 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8048
8049 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8050 if (pixel != -1)
8051 {
8052 XtSetArg (av[ac], XmNforeground, pixel);
8053 ++ac;
8054 }
8055
8056 pixel = f->output_data.x->scroll_bar_background_pixel;
8057 if (pixel != -1)
8058 {
8059 XtSetArg (av[ac], XmNbackground, pixel);
8060 ++ac;
8061 }
8062
8063 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8064 scroll_bar_name, av, ac);
8065
8066 /* Add one callback for everything that can happen. */
8067 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8068 (XtPointer) bar);
8069 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8070 (XtPointer) bar);
8071 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8072 (XtPointer) bar);
8073 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8074 (XtPointer) bar);
8075 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8076 (XtPointer) bar);
8077 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8078 (XtPointer) bar);
8079 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8080 (XtPointer) bar);
8081
8082 /* Realize the widget. Only after that is the X window created. */
8083 XtRealizeWidget (widget);
8084
8085 /* Set the cursor to an arrow. I didn't find a resource to do that.
8086 And I'm wondering why it hasn't an arrow cursor by default. */
8087 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8088 f->output_data.x->nontext_cursor);
8089
ec18280f 8090#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
8091
8092 /* Set resources. Create the widget. The background of the
8093 Xaw3d scroll bar widget is a little bit light for my taste.
8094 We don't alter it here to let users change it according
8095 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8096 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8097 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
ec18280f
SM
8098 /* For smoother scrolling with Xaw3d -sm */
8099 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
8100 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
06a2c219
GM
8101
8102 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8103 if (pixel != -1)
8104 {
8105 XtSetArg (av[ac], XtNforeground, pixel);
8106 ++ac;
8107 }
8108
8109 pixel = f->output_data.x->scroll_bar_background_pixel;
8110 if (pixel != -1)
8111 {
8112 XtSetArg (av[ac], XtNbackground, pixel);
8113 ++ac;
8114 }
8115
8116 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8117 f->output_data.x->edit_widget, av, ac);
ec18280f
SM
8118
8119 {
8120 char *initial = "";
8121 char *val = initial;
8122 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8123 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8124 if (val == initial)
8125 { /* ARROW_SCROLL */
8126 xaw3d_arrow_scroll = True;
8127 /* Isn't that just a personal preference ? -sm */
8128 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8129 }
8130 }
06a2c219
GM
8131
8132 /* Define callbacks. */
ec18280f
SM
8133 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8134 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
06a2c219
GM
8135 (XtPointer) bar);
8136
8137 /* Realize the widget. Only after that is the X window created. */
8138 XtRealizeWidget (widget);
8139
ec18280f 8140#endif /* !USE_MOTIF */
06a2c219
GM
8141
8142 /* Install an action hook that let's us detect when the user
8143 finishes interacting with a scroll bar. */
8144 if (action_hook_id == 0)
8145 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8146
8147 /* Remember X window and widget in the scroll bar vector. */
8148 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8149 xwindow = XtWindow (widget);
8150 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8151
8152 UNBLOCK_INPUT;
8153}
8154
8155
8156/* Set the thumb size and position of scroll bar BAR. We are currently
8157 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8158
8159static void
8160x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8161 struct scroll_bar *bar;
8162 int portion, position, whole;
f451eb13 8163{
e83dc917
GM
8164 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8165 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
06a2c219 8166 float top, shown;
f451eb13 8167
06a2c219
GM
8168 if (whole == 0)
8169 top = 0, shown = 1;
8170 else
f451eb13 8171 {
06a2c219
GM
8172 top = (float) position / whole;
8173 shown = (float) portion / whole;
8174 }
f451eb13 8175
06a2c219 8176 BLOCK_INPUT;
f451eb13 8177
06a2c219
GM
8178#ifdef USE_MOTIF
8179 {
8180 int size, value;
8181 Boolean arrow1_selected, arrow2_selected;
8182 unsigned char flags;
8183 XmScrollBarWidget sb;
8184
ec18280f 8185 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
06a2c219
GM
8186 is the scroll bar's maximum and MIN is the scroll bar's minimum
8187 value. */
8188 size = shown * XM_SB_RANGE;
8189 size = min (size, XM_SB_RANGE);
8190 size = max (size, 1);
8191
8192 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8193 value = top * XM_SB_RANGE;
8194 value = min (value, XM_SB_MAX - size);
8195 value = max (value, XM_SB_MIN);
8196
8197 /* LessTif: Calling XmScrollBarSetValues after an increment or
8198 decrement turns off auto-repeat LessTif-internally. This can
8199 be seen in ScrollBar.c which resets Arrow1Selected and
8200 Arrow2Selected. It also sets internal flags so that LessTif
8201 believes the mouse is in the slider. We either have to change
8202 our code, or work around that by accessing private data. */
8203
8204 sb = (XmScrollBarWidget) widget;
8205 arrow1_selected = sb->scrollBar.arrow1_selected;
8206 arrow2_selected = sb->scrollBar.arrow2_selected;
8207 flags = sb->scrollBar.flags;
8208
8209 if (NILP (bar->dragging))
8210 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8211 else if (last_scroll_bar_part == scroll_bar_down_arrow)
8212 /* This has the negative side effect that the slider value is
ec18280f 8213 not what it would be if we scrolled here using line-wise or
06a2c219
GM
8214 page-wise movement. */
8215 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
8216 else
8217 {
8218 /* If currently dragging, only update the slider size.
8219 This reduces flicker effects. */
8220 int old_value, old_size, increment, page_increment;
8221
8222 XmScrollBarGetValues (widget, &old_value, &old_size,
8223 &increment, &page_increment);
8224 XmScrollBarSetValues (widget, old_value,
8225 min (size, XM_SB_RANGE - old_value),
8226 0, 0, False);
8227 }
8228
8229 sb->scrollBar.arrow1_selected = arrow1_selected;
8230 sb->scrollBar.arrow2_selected = arrow2_selected;
8231 sb->scrollBar.flags = flags;
8232 }
ec18280f 8233#else /* !USE_MOTIF i.e. use Xaw */
06a2c219 8234 {
ec18280f
SM
8235 float old_top, old_shown;
8236 Dimension height;
8237 XtVaGetValues (widget,
8238 XtNtopOfThumb, &old_top,
8239 XtNshown, &old_shown,
8240 XtNheight, &height,
8241 NULL);
8242
8243 /* Massage the top+shown values. */
8244 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8245 top = max (0, min (1, top));
8246 else
8247 top = old_top;
8248 /* Keep two pixels available for moving the thumb down. */
8249 shown = max (0, min (1 - top - (2.0 / height), shown));
06a2c219
GM
8250
8251 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8252 check that your system's configuration file contains a define
8253 for `NARROWPROTO'. See s/freebsd.h for an example. */
ec18280f 8254 if (top != old_top || shown != old_shown)
eb393530 8255 {
ec18280f 8256 if (NILP (bar->dragging))
eb393530 8257 XawScrollbarSetThumb (widget, top, shown);
06a2c219
GM
8258 else
8259 {
ec18280f
SM
8260#ifdef HAVE_XAW3D
8261 ScrollbarWidget sb = (ScrollbarWidget) widget;
3e71d8f2 8262 int scroll_mode = 0;
ec18280f
SM
8263
8264 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8265 if (xaw3d_arrow_scroll)
8266 {
8267 /* Xaw3d stupidly ignores resize requests while dragging
8268 so we have to make it believe it's not in dragging mode. */
8269 scroll_mode = sb->scrollbar.scroll_mode;
8270 if (scroll_mode == 2)
8271 sb->scrollbar.scroll_mode = 0;
8272 }
8273#endif
8274 /* Try to make the scrolling a tad smoother. */
8275 if (!xaw3d_pick_top)
8276 shown = min (shown, old_shown);
8277
8278 XawScrollbarSetThumb (widget, top, shown);
8279
8280#ifdef HAVE_XAW3D
8281 if (xaw3d_arrow_scroll && scroll_mode == 2)
8282 sb->scrollbar.scroll_mode = scroll_mode;
8283#endif
06a2c219 8284 }
06a2c219
GM
8285 }
8286 }
ec18280f 8287#endif /* !USE_MOTIF */
06a2c219
GM
8288
8289 UNBLOCK_INPUT;
f451eb13
JB
8290}
8291
06a2c219
GM
8292#endif /* USE_TOOLKIT_SCROLL_BARS */
8293
8294
8295\f
8296/************************************************************************
8297 Scroll bars, general
8298 ************************************************************************/
8299
8300/* Create a scroll bar and return the scroll bar vector for it. W is
8301 the Emacs window on which to create the scroll bar. TOP, LEFT,
8302 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8303 scroll bar. */
8304
ab648270 8305static struct scroll_bar *
06a2c219
GM
8306x_scroll_bar_create (w, top, left, width, height)
8307 struct window *w;
f451eb13
JB
8308 int top, left, width, height;
8309{
06a2c219 8310 struct frame *f = XFRAME (w->frame);
334208b7
RS
8311 struct scroll_bar *bar
8312 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
f451eb13
JB
8313
8314 BLOCK_INPUT;
8315
eccc05db 8316#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
8317 x_create_toolkit_scroll_bar (f, bar);
8318#else /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8319 {
8320 XSetWindowAttributes a;
8321 unsigned long mask;
5c187dee 8322 Window window;
06a2c219
GM
8323
8324 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8325 if (a.background_pixel == -1)
8326 a.background_pixel = f->output_data.x->background_pixel;
8327
12ba150f 8328 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9a572e2a 8329 | ButtonMotionMask | PointerMotionHintMask
12ba150f 8330 | ExposureMask);
7a13e894 8331 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
f451eb13 8332
dbc4e1c1 8333 mask = (CWBackPixel | CWEventMask | CWCursor);
f451eb13 8334
06a2c219
GM
8335 /* Clear the area of W that will serve as a scroll bar. This is
8336 for the case that a window has been split horizontally. In
8337 this case, no clear_frame is generated to reduce flickering. */
8338 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8339 left, top, width,
8340 window_box_height (w), False);
8341
8342 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8343 /* Position and size of scroll bar. */
8344 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8345 top,
8346 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8347 height,
8348 /* Border width, depth, class, and visual. */
8349 0,
8350 CopyFromParent,
8351 CopyFromParent,
8352 CopyFromParent,
8353 /* Attributes. */
8354 mask, &a);
8355 SET_SCROLL_BAR_X_WINDOW (bar, window);
f451eb13 8356 }
06a2c219 8357#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 8358
06a2c219 8359 XSETWINDOW (bar->window, w);
e0c1aef2
KH
8360 XSETINT (bar->top, top);
8361 XSETINT (bar->left, left);
8362 XSETINT (bar->width, width);
8363 XSETINT (bar->height, height);
8364 XSETINT (bar->start, 0);
8365 XSETINT (bar->end, 0);
12ba150f 8366 bar->dragging = Qnil;
f451eb13
JB
8367
8368 /* Add bar to its frame's list of scroll bars. */
334208b7 8369 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 8370 bar->prev = Qnil;
334208b7 8371 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
06a2c219 8372 if (!NILP (bar->next))
e0c1aef2 8373 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13 8374
06a2c219 8375 /* Map the window/widget. */
eccc05db 8376#ifdef USE_TOOLKIT_SCROLL_BARS
e83dc917
GM
8377 {
8378 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8379 XtConfigureWidget (scroll_bar,
06a2c219
GM
8380 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8381 top,
8382 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8383 height, 0);
e83dc917
GM
8384 XtMapWidget (scroll_bar);
8385 }
06a2c219 8386#else /* not USE_TOOLKIT_SCROLL_BARS */
7f9c7f94 8387 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
06a2c219 8388#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8389
8390 UNBLOCK_INPUT;
12ba150f 8391 return bar;
f451eb13
JB
8392}
8393
06a2c219 8394
12ba150f 8395/* Draw BAR's handle in the proper position.
06a2c219 8396
12ba150f
JB
8397 If the handle is already drawn from START to END, don't bother
8398 redrawing it, unless REBUILD is non-zero; in that case, always
8399 redraw it. (REBUILD is handy for drawing the handle after expose
58769bee 8400 events.)
12ba150f
JB
8401
8402 Normally, we want to constrain the start and end of the handle to
06a2c219
GM
8403 fit inside its rectangle, but if the user is dragging the scroll
8404 bar handle, we want to let them drag it down all the way, so that
8405 the bar's top is as far down as it goes; otherwise, there's no way
8406 to move to the very end of the buffer. */
8407
5c187dee
GM
8408#ifndef USE_TOOLKIT_SCROLL_BARS
8409
f451eb13 8410static void
ab648270
JB
8411x_scroll_bar_set_handle (bar, start, end, rebuild)
8412 struct scroll_bar *bar;
f451eb13 8413 int start, end;
12ba150f 8414 int rebuild;
f451eb13 8415{
12ba150f 8416 int dragging = ! NILP (bar->dragging);
ab648270 8417 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8418 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8419 GC gc = f->output_data.x->normal_gc;
12ba150f
JB
8420
8421 /* If the display is already accurate, do nothing. */
8422 if (! rebuild
8423 && start == XINT (bar->start)
8424 && end == XINT (bar->end))
8425 return;
8426
f451eb13
JB
8427 BLOCK_INPUT;
8428
8429 {
d9cdbb3d
RS
8430 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8431 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8432 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
f451eb13
JB
8433
8434 /* Make sure the values are reasonable, and try to preserve
8435 the distance between start and end. */
12ba150f
JB
8436 {
8437 int length = end - start;
8438
8439 if (start < 0)
8440 start = 0;
8441 else if (start > top_range)
8442 start = top_range;
8443 end = start + length;
8444
8445 if (end < start)
8446 end = start;
8447 else if (end > top_range && ! dragging)
8448 end = top_range;
8449 }
f451eb13 8450
ab648270 8451 /* Store the adjusted setting in the scroll bar. */
e0c1aef2
KH
8452 XSETINT (bar->start, start);
8453 XSETINT (bar->end, end);
f451eb13 8454
12ba150f
JB
8455 /* Clip the end position, just for display. */
8456 if (end > top_range)
8457 end = top_range;
f451eb13 8458
ab648270 8459 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
12ba150f
JB
8460 below top positions, to make sure the handle is always at least
8461 that many pixels tall. */
ab648270 8462 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
f451eb13 8463
12ba150f
JB
8464 /* Draw the empty space above the handle. Note that we can't clear
8465 zero-height areas; that means "clear to end of window." */
8466 if (0 < start)
334208b7 8467 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 8468
12ba150f 8469 /* x, y, width, height, and exposures. */
ab648270
JB
8470 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8471 VERTICAL_SCROLL_BAR_TOP_BORDER,
12ba150f
JB
8472 inside_width, start,
8473 False);
f451eb13 8474
06a2c219
GM
8475 /* Change to proper foreground color if one is specified. */
8476 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8477 XSetForeground (FRAME_X_DISPLAY (f), gc,
8478 f->output_data.x->scroll_bar_foreground_pixel);
8479
12ba150f 8480 /* Draw the handle itself. */
334208b7 8481 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13 8482
12ba150f 8483 /* x, y, width, height */
ab648270
JB
8484 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8485 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
12ba150f 8486 inside_width, end - start);
f451eb13 8487
06a2c219
GM
8488 /* Restore the foreground color of the GC if we changed it above. */
8489 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8490 XSetForeground (FRAME_X_DISPLAY (f), gc,
8491 f->output_data.x->foreground_pixel);
f451eb13 8492
12ba150f
JB
8493 /* Draw the empty space below the handle. Note that we can't
8494 clear zero-height areas; that means "clear to end of window." */
8495 if (end < inside_height)
334208b7 8496 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 8497
12ba150f 8498 /* x, y, width, height, and exposures. */
ab648270
JB
8499 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8500 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
12ba150f
JB
8501 inside_width, inside_height - end,
8502 False);
f451eb13 8503
f451eb13
JB
8504 }
8505
f451eb13
JB
8506 UNBLOCK_INPUT;
8507}
8508
5c187dee 8509#endif /* !USE_TOOLKIT_SCROLL_BARS */
f451eb13 8510
06a2c219
GM
8511/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8512 nil. */
58769bee 8513
12ba150f 8514static void
ab648270
JB
8515x_scroll_bar_remove (bar)
8516 struct scroll_bar *bar;
12ba150f 8517{
e83dc917 8518 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
8519 BLOCK_INPUT;
8520
eccc05db 8521#ifdef USE_TOOLKIT_SCROLL_BARS
e83dc917
GM
8522 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
8523#else
8524 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8525#endif
06a2c219 8526
ab648270
JB
8527 /* Disassociate this scroll bar from its window. */
8528 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
12ba150f
JB
8529
8530 UNBLOCK_INPUT;
8531}
8532
06a2c219 8533
12ba150f
JB
8534/* Set the handle of the vertical scroll bar for WINDOW to indicate
8535 that we are displaying PORTION characters out of a total of WHOLE
ab648270 8536 characters, starting at POSITION. If WINDOW has no scroll bar,
12ba150f 8537 create one. */
06a2c219 8538
12ba150f 8539static void
06a2c219
GM
8540XTset_vertical_scroll_bar (w, portion, whole, position)
8541 struct window *w;
f451eb13
JB
8542 int portion, whole, position;
8543{
06a2c219 8544 struct frame *f = XFRAME (w->frame);
ab648270 8545 struct scroll_bar *bar;
3c6ede7b 8546 int top, height, left, sb_left, width, sb_width;
06a2c219 8547 int window_x, window_y, window_width, window_height;
06a2c219 8548
3c6ede7b 8549 /* Get window dimensions. */
06a2c219 8550 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
3c6ede7b
GM
8551 top = window_y;
8552 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8553 height = window_height;
06a2c219 8554
3c6ede7b 8555 /* Compute the left edge of the scroll bar area. */
06a2c219 8556 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
3c6ede7b
GM
8557 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8558 else
8559 left = XFASTINT (w->left);
8560 left *= CANON_X_UNIT (f);
8561 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8562
8563 /* Compute the width of the scroll bar which might be less than
8564 the width of the area reserved for the scroll bar. */
8565 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8566 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
06a2c219 8567 else
3c6ede7b 8568 sb_width = width;
12ba150f 8569
3c6ede7b
GM
8570 /* Compute the left edge of the scroll bar. */
8571#ifdef USE_TOOLKIT_SCROLL_BARS
8572 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8573 sb_left = left + width - sb_width - (width - sb_width) / 2;
8574 else
8575 sb_left = left + (width - sb_width) / 2;
8576#else
8577 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8578 sb_left = left + width - sb_width;
8579 else
8580 sb_left = left;
8581#endif
8582
ab648270 8583 /* Does the scroll bar exist yet? */
06a2c219 8584 if (NILP (w->vertical_scroll_bar))
3c6ede7b 8585 {
80c32bcc 8586 BLOCK_INPUT;
3c6ede7b
GM
8587 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8588 left, top, width, height, False);
80c32bcc 8589 UNBLOCK_INPUT;
3c6ede7b
GM
8590 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8591 }
f451eb13 8592 else
12ba150f
JB
8593 {
8594 /* It may just need to be moved and resized. */
06a2c219
GM
8595 unsigned int mask = 0;
8596
8597 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8598
8599 BLOCK_INPUT;
8600
3c6ede7b 8601 if (sb_left != XINT (bar->left))
06a2c219 8602 mask |= CWX;
3c6ede7b 8603 if (top != XINT (bar->top))
06a2c219 8604 mask |= CWY;
3c6ede7b 8605 if (sb_width != XINT (bar->width))
06a2c219 8606 mask |= CWWidth;
3c6ede7b 8607 if (height != XINT (bar->height))
06a2c219
GM
8608 mask |= CWHeight;
8609
8610#ifdef USE_TOOLKIT_SCROLL_BARS
fe6f39d9
GM
8611
8612 /* Since toolkit scroll bars are smaller than the space reserved
8613 for them on the frame, we have to clear "under" them. */
8614 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3c6ede7b 8615 left, top, width, height, False);
06a2c219
GM
8616
8617 /* Move/size the scroll bar widget. */
8618 if (mask)
e83dc917 8619 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
3c6ede7b
GM
8620 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8621 top,
8622 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8623 height, 0);
06a2c219
GM
8624
8625#else /* not USE_TOOLKIT_SCROLL_BARS */
8626
e1f6572f
RS
8627 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8628 {
8629 /* Clear areas not covered by the scroll bar. This makes sure a
8630 previous mode line display is cleared after C-x 2 C-x 1, for
8631 example. Non-toolkit scroll bars are as wide as the area
8632 reserved for scroll bars - trim at both sides. */
8633 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8634 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8635 height, False);
8636 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8637 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8638 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8639 height, False);
8640 }
06a2c219
GM
8641
8642 /* Move/size the scroll bar window. */
8643 if (mask)
8644 {
8645 XWindowChanges wc;
8646
3c6ede7b
GM
8647 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8648 wc.y = top;
8649 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8650 wc.height = height;
06a2c219
GM
8651 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8652 mask, &wc);
8653 }
8654
8655#endif /* not USE_TOOLKIT_SCROLL_BARS */
8656
8657 /* Remember new settings. */
3c6ede7b
GM
8658 XSETINT (bar->left, sb_left);
8659 XSETINT (bar->top, top);
8660 XSETINT (bar->width, sb_width);
8661 XSETINT (bar->height, height);
06a2c219
GM
8662
8663 UNBLOCK_INPUT;
12ba150f 8664 }
f451eb13 8665
eccc05db 8666#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
8667 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8668#else /* not USE_TOOLKIT_SCROLL_BARS */
ab648270 8669 /* Set the scroll bar's current state, unless we're currently being
f451eb13 8670 dragged. */
12ba150f 8671 if (NILP (bar->dragging))
f451eb13 8672 {
92857db0 8673 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
f451eb13 8674
12ba150f 8675 if (whole == 0)
ab648270 8676 x_scroll_bar_set_handle (bar, 0, top_range, 0);
12ba150f
JB
8677 else
8678 {
43f868f5
JB
8679 int start = ((double) position * top_range) / whole;
8680 int end = ((double) (position + portion) * top_range) / whole;
ab648270 8681 x_scroll_bar_set_handle (bar, start, end, 0);
12ba150f 8682 }
f451eb13 8683 }
06a2c219 8684#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 8685
06a2c219 8686 XSETVECTOR (w->vertical_scroll_bar, bar);
f451eb13
JB
8687}
8688
12ba150f 8689
f451eb13 8690/* The following three hooks are used when we're doing a thorough
ab648270 8691 redisplay of the frame. We don't explicitly know which scroll bars
f451eb13 8692 are going to be deleted, because keeping track of when windows go
12ba150f
JB
8693 away is a real pain - "Can you say set-window-configuration, boys
8694 and girls?" Instead, we just assert at the beginning of redisplay
ab648270 8695 that *all* scroll bars are to be removed, and then save a scroll bar
12ba150f 8696 from the fiery pit when we actually redisplay its window. */
f451eb13 8697
ab648270
JB
8698/* Arrange for all scroll bars on FRAME to be removed at the next call
8699 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
06a2c219
GM
8700 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8701
58769bee 8702static void
ab648270 8703XTcondemn_scroll_bars (frame)
f451eb13
JB
8704 FRAME_PTR frame;
8705{
f9e24cb9
RS
8706 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8707 while (! NILP (FRAME_SCROLL_BARS (frame)))
8708 {
8709 Lisp_Object bar;
8710 bar = FRAME_SCROLL_BARS (frame);
8711 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8712 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8713 XSCROLL_BAR (bar)->prev = Qnil;
8714 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8715 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8716 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8717 }
f451eb13
JB
8718}
8719
fa2dfc30 8720
06a2c219 8721/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
12ba150f 8722 Note that WINDOW isn't necessarily condemned at all. */
fa2dfc30 8723
f451eb13 8724static void
ab648270 8725XTredeem_scroll_bar (window)
12ba150f 8726 struct window *window;
f451eb13 8727{
ab648270 8728 struct scroll_bar *bar;
fa2dfc30 8729 struct frame *f;
12ba150f 8730
ab648270
JB
8731 /* We can't redeem this window's scroll bar if it doesn't have one. */
8732 if (NILP (window->vertical_scroll_bar))
12ba150f
JB
8733 abort ();
8734
ab648270 8735 bar = XSCROLL_BAR (window->vertical_scroll_bar);
12ba150f
JB
8736
8737 /* Unlink it from the condemned list. */
fa2dfc30
GM
8738 f = XFRAME (WINDOW_FRAME (window));
8739 if (NILP (bar->prev))
8740 {
8741 /* If the prev pointer is nil, it must be the first in one of
8742 the lists. */
8743 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
8744 /* It's not condemned. Everything's fine. */
8745 return;
8746 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8747 window->vertical_scroll_bar))
8748 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
8749 else
8750 /* If its prev pointer is nil, it must be at the front of
8751 one or the other! */
8752 abort ();
8753 }
8754 else
8755 XSCROLL_BAR (bar->prev)->next = bar->next;
12ba150f 8756
fa2dfc30
GM
8757 if (! NILP (bar->next))
8758 XSCROLL_BAR (bar->next)->prev = bar->prev;
12ba150f 8759
fa2dfc30
GM
8760 bar->next = FRAME_SCROLL_BARS (f);
8761 bar->prev = Qnil;
8762 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8763 if (! NILP (bar->next))
8764 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13
JB
8765}
8766
ab648270
JB
8767/* Remove all scroll bars on FRAME that haven't been saved since the
8768 last call to `*condemn_scroll_bars_hook'. */
06a2c219 8769
f451eb13 8770static void
ab648270 8771XTjudge_scroll_bars (f)
12ba150f 8772 FRAME_PTR f;
f451eb13 8773{
12ba150f 8774 Lisp_Object bar, next;
f451eb13 8775
ab648270 8776 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
cf7cb199
JB
8777
8778 /* Clear out the condemned list now so we won't try to process any
ab648270
JB
8779 more events on the hapless scroll bars. */
8780 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
cf7cb199
JB
8781
8782 for (; ! NILP (bar); bar = next)
f451eb13 8783 {
ab648270 8784 struct scroll_bar *b = XSCROLL_BAR (bar);
12ba150f 8785
ab648270 8786 x_scroll_bar_remove (b);
12ba150f
JB
8787
8788 next = b->next;
8789 b->next = b->prev = Qnil;
f451eb13 8790 }
12ba150f 8791
ab648270 8792 /* Now there should be no references to the condemned scroll bars,
12ba150f 8793 and they should get garbage-collected. */
f451eb13
JB
8794}
8795
8796
06a2c219
GM
8797/* Handle an Expose or GraphicsExpose event on a scroll bar. This
8798 is a no-op when using toolkit scroll bars.
ab648270
JB
8799
8800 This may be called from a signal handler, so we have to ignore GC
8801 mark bits. */
06a2c219 8802
f451eb13 8803static void
ab648270
JB
8804x_scroll_bar_expose (bar, event)
8805 struct scroll_bar *bar;
f451eb13
JB
8806 XEvent *event;
8807{
06a2c219
GM
8808#ifndef USE_TOOLKIT_SCROLL_BARS
8809
ab648270 8810 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8811 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8812 GC gc = f->output_data.x->normal_gc;
3cbd2e0b 8813 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
12ba150f 8814
f451eb13
JB
8815 BLOCK_INPUT;
8816
ab648270 8817 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
f451eb13 8818
06a2c219 8819 /* Draw a one-pixel border just inside the edges of the scroll bar. */
334208b7 8820 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13
JB
8821
8822 /* x, y, width, height */
d9cdbb3d 8823 0, 0,
3cbd2e0b 8824 XINT (bar->width) - 1 - width_trim - width_trim,
d9cdbb3d
RS
8825 XINT (bar->height) - 1);
8826
f451eb13 8827 UNBLOCK_INPUT;
06a2c219
GM
8828
8829#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8830}
8831
ab648270
JB
8832/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8833 is set to something other than no_event, it is enqueued.
8834
8835 This may be called from a signal handler, so we have to ignore GC
8836 mark bits. */
06a2c219 8837
5c187dee
GM
8838#ifndef USE_TOOLKIT_SCROLL_BARS
8839
f451eb13 8840static void
ab648270
JB
8841x_scroll_bar_handle_click (bar, event, emacs_event)
8842 struct scroll_bar *bar;
f451eb13
JB
8843 XEvent *event;
8844 struct input_event *emacs_event;
8845{
0299d313 8846 if (! GC_WINDOWP (bar->window))
12ba150f
JB
8847 abort ();
8848
ab648270 8849 emacs_event->kind = scroll_bar_click;
69388238 8850 emacs_event->code = event->xbutton.button - Button1;
0299d313
RS
8851 emacs_event->modifiers
8852 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8853 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8854 event->xbutton.state)
8855 | (event->type == ButtonRelease
8856 ? up_modifier
8857 : down_modifier));
12ba150f 8858 emacs_event->frame_or_window = bar->window;
0f8aabe9 8859 emacs_event->arg = Qnil;
f451eb13 8860 emacs_event->timestamp = event->xbutton.time;
12ba150f 8861 {
06a2c219 8862#if 0
d9cdbb3d 8863 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
0299d313 8864 int internal_height
d9cdbb3d 8865 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8866#endif
0299d313 8867 int top_range
d9cdbb3d 8868 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ab648270 8869 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
12ba150f
JB
8870
8871 if (y < 0) y = 0;
8872 if (y > top_range) y = top_range;
8873
8874 if (y < XINT (bar->start))
ab648270
JB
8875 emacs_event->part = scroll_bar_above_handle;
8876 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8877 emacs_event->part = scroll_bar_handle;
12ba150f 8878 else
ab648270 8879 emacs_event->part = scroll_bar_below_handle;
929787e1
JB
8880
8881 /* Just because the user has clicked on the handle doesn't mean
5116f055
JB
8882 they want to drag it. Lisp code needs to be able to decide
8883 whether or not we're dragging. */
929787e1 8884#if 0
12ba150f
JB
8885 /* If the user has just clicked on the handle, record where they're
8886 holding it. */
8887 if (event->type == ButtonPress
ab648270 8888 && emacs_event->part == scroll_bar_handle)
e0c1aef2 8889 XSETINT (bar->dragging, y - XINT (bar->start));
929787e1 8890#endif
12ba150f
JB
8891
8892 /* If the user has released the handle, set it to its final position. */
8893 if (event->type == ButtonRelease
8894 && ! NILP (bar->dragging))
8895 {
8896 int new_start = y - XINT (bar->dragging);
8897 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
f451eb13 8898
ab648270 8899 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
12ba150f
JB
8900 bar->dragging = Qnil;
8901 }
f451eb13 8902
5116f055
JB
8903 /* Same deal here as the other #if 0. */
8904#if 0
58769bee 8905 /* Clicks on the handle are always reported as occurring at the top of
12ba150f 8906 the handle. */
ab648270 8907 if (emacs_event->part == scroll_bar_handle)
12ba150f
JB
8908 emacs_event->x = bar->start;
8909 else
e0c1aef2 8910 XSETINT (emacs_event->x, y);
5116f055 8911#else
e0c1aef2 8912 XSETINT (emacs_event->x, y);
5116f055 8913#endif
f451eb13 8914
e0c1aef2 8915 XSETINT (emacs_event->y, top_range);
12ba150f
JB
8916 }
8917}
f451eb13 8918
ab648270
JB
8919/* Handle some mouse motion while someone is dragging the scroll bar.
8920
8921 This may be called from a signal handler, so we have to ignore GC
8922 mark bits. */
06a2c219 8923
f451eb13 8924static void
ab648270
JB
8925x_scroll_bar_note_movement (bar, event)
8926 struct scroll_bar *bar;
f451eb13
JB
8927 XEvent *event;
8928{
39d8bb4d
KH
8929 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8930
f451eb13
JB
8931 last_mouse_movement_time = event->xmotion.time;
8932
39d8bb4d 8933 f->mouse_moved = 1;
e0c1aef2 8934 XSETVECTOR (last_mouse_scroll_bar, bar);
f451eb13
JB
8935
8936 /* If we're dragging the bar, display it. */
ab648270 8937 if (! GC_NILP (bar->dragging))
f451eb13
JB
8938 {
8939 /* Where should the handle be now? */
12ba150f 8940 int new_start = event->xmotion.y - XINT (bar->dragging);
f451eb13 8941
12ba150f 8942 if (new_start != XINT (bar->start))
f451eb13 8943 {
12ba150f 8944 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
58769bee 8945
ab648270 8946 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
f451eb13
JB
8947 }
8948 }
f451eb13
JB
8949}
8950
5c187dee
GM
8951#endif /* !USE_TOOLKIT_SCROLL_BARS */
8952
12ba150f 8953/* Return information to the user about the current position of the mouse
ab648270 8954 on the scroll bar. */
06a2c219 8955
12ba150f 8956static void
334208b7
RS
8957x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8958 FRAME_PTR *fp;
12ba150f 8959 Lisp_Object *bar_window;
ab648270 8960 enum scroll_bar_part *part;
12ba150f
JB
8961 Lisp_Object *x, *y;
8962 unsigned long *time;
8963{
ab648270 8964 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
334208b7
RS
8965 Window w = SCROLL_BAR_X_WINDOW (bar);
8966 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f 8967 int win_x, win_y;
559cb2fb
JB
8968 Window dummy_window;
8969 int dummy_coord;
8970 unsigned int dummy_mask;
12ba150f 8971
cf7cb199
JB
8972 BLOCK_INPUT;
8973
ab648270 8974 /* Get the mouse's position relative to the scroll bar window, and
12ba150f 8975 report that. */
334208b7 8976 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
12ba150f 8977
559cb2fb
JB
8978 /* Root, child, root x and root y. */
8979 &dummy_window, &dummy_window,
8980 &dummy_coord, &dummy_coord,
12ba150f 8981
559cb2fb
JB
8982 /* Position relative to scroll bar. */
8983 &win_x, &win_y,
12ba150f 8984
559cb2fb
JB
8985 /* Mouse buttons and modifier keys. */
8986 &dummy_mask))
7a13e894 8987 ;
559cb2fb
JB
8988 else
8989 {
06a2c219 8990#if 0
559cb2fb 8991 int inside_height
d9cdbb3d 8992 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8993#endif
559cb2fb 8994 int top_range
d9cdbb3d 8995 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
559cb2fb
JB
8996
8997 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
8998
8999 if (! NILP (bar->dragging))
9000 win_y -= XINT (bar->dragging);
9001
9002 if (win_y < 0)
9003 win_y = 0;
9004 if (win_y > top_range)
9005 win_y = top_range;
9006
334208b7 9007 *fp = f;
7a13e894 9008 *bar_window = bar->window;
559cb2fb
JB
9009
9010 if (! NILP (bar->dragging))
9011 *part = scroll_bar_handle;
9012 else if (win_y < XINT (bar->start))
9013 *part = scroll_bar_above_handle;
9014 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9015 *part = scroll_bar_handle;
9016 else
9017 *part = scroll_bar_below_handle;
12ba150f 9018
e0c1aef2
KH
9019 XSETINT (*x, win_y);
9020 XSETINT (*y, top_range);
12ba150f 9021
39d8bb4d 9022 f->mouse_moved = 0;
559cb2fb
JB
9023 last_mouse_scroll_bar = Qnil;
9024 }
12ba150f 9025
559cb2fb 9026 *time = last_mouse_movement_time;
cf7cb199 9027
cf7cb199 9028 UNBLOCK_INPUT;
12ba150f
JB
9029}
9030
f451eb13 9031
dbc4e1c1 9032/* The screen has been cleared so we may have changed foreground or
ab648270
JB
9033 background colors, and the scroll bars may need to be redrawn.
9034 Clear out the scroll bars, and ask for expose events, so we can
dbc4e1c1
JB
9035 redraw them. */
9036
dfcf069d 9037void
ab648270 9038x_scroll_bar_clear (f)
dbc4e1c1
JB
9039 FRAME_PTR f;
9040{
06a2c219 9041#ifndef USE_TOOLKIT_SCROLL_BARS
dbc4e1c1
JB
9042 Lisp_Object bar;
9043
b80c363e
RS
9044 /* We can have scroll bars even if this is 0,
9045 if we just turned off scroll bar mode.
9046 But in that case we should not clear them. */
9047 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9048 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9049 bar = XSCROLL_BAR (bar)->next)
9050 XClearArea (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
9051 0, 0, 0, 0, True);
06a2c219 9052#endif /* not USE_TOOLKIT_SCROLL_BARS */
dbc4e1c1
JB
9053}
9054
06a2c219 9055/* This processes Expose events from the menu-bar specific X event
19126e11 9056 loop in xmenu.c. This allows to redisplay the frame if necessary
06a2c219 9057 when handling menu-bar or pop-up items. */
3afe33e7 9058
06a2c219 9059int
3afe33e7
RS
9060process_expose_from_menu (event)
9061 XEvent event;
9062{
9063 FRAME_PTR f;
19126e11 9064 struct x_display_info *dpyinfo;
06a2c219 9065 int frame_exposed_p = 0;
3afe33e7 9066
f94397b5
KH
9067 BLOCK_INPUT;
9068
19126e11
KH
9069 dpyinfo = x_display_info_for_display (event.xexpose.display);
9070 f = x_window_to_frame (dpyinfo, event.xexpose.window);
3afe33e7
RS
9071 if (f)
9072 {
9073 if (f->async_visible == 0)
9074 {
9075 f->async_visible = 1;
9076 f->async_iconified = 0;
06c488fd 9077 f->output_data.x->has_been_visible = 1;
3afe33e7
RS
9078 SET_FRAME_GARBAGED (f);
9079 }
9080 else
9081 {
06a2c219
GM
9082 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9083 event.xexpose.x, event.xexpose.y,
9084 event.xexpose.width, event.xexpose.height);
9085 frame_exposed_p = 1;
3afe33e7
RS
9086 }
9087 }
9088 else
9089 {
9090 struct scroll_bar *bar
9091 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 9092
3afe33e7
RS
9093 if (bar)
9094 x_scroll_bar_expose (bar, &event);
9095 }
f94397b5
KH
9096
9097 UNBLOCK_INPUT;
06a2c219 9098 return frame_exposed_p;
3afe33e7 9099}
09756a85
RS
9100\f
9101/* Define a queue to save up SelectionRequest events for later handling. */
9102
9103struct selection_event_queue
9104 {
9105 XEvent event;
9106 struct selection_event_queue *next;
9107 };
9108
9109static struct selection_event_queue *queue;
9110
9111/* Nonzero means queue up certain events--don't process them yet. */
06a2c219 9112
09756a85
RS
9113static int x_queue_selection_requests;
9114
9115/* Queue up an X event *EVENT, to be processed later. */
dbc4e1c1 9116
09756a85 9117static void
334208b7
RS
9118x_queue_event (f, event)
9119 FRAME_PTR f;
09756a85
RS
9120 XEvent *event;
9121{
9122 struct selection_event_queue *queue_tmp
06a2c219 9123 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
09756a85 9124
58769bee 9125 if (queue_tmp != NULL)
09756a85
RS
9126 {
9127 queue_tmp->event = *event;
9128 queue_tmp->next = queue;
9129 queue = queue_tmp;
9130 }
9131}
9132
9133/* Take all the queued events and put them back
9134 so that they get processed afresh. */
9135
9136static void
db3906fd
RS
9137x_unqueue_events (display)
9138 Display *display;
09756a85 9139{
58769bee 9140 while (queue != NULL)
09756a85
RS
9141 {
9142 struct selection_event_queue *queue_tmp = queue;
db3906fd 9143 XPutBackEvent (display, &queue_tmp->event);
09756a85 9144 queue = queue_tmp->next;
06a2c219 9145 xfree ((char *)queue_tmp);
09756a85
RS
9146 }
9147}
9148
9149/* Start queuing SelectionRequest events. */
9150
9151void
db3906fd
RS
9152x_start_queuing_selection_requests (display)
9153 Display *display;
09756a85
RS
9154{
9155 x_queue_selection_requests++;
9156}
9157
9158/* Stop queuing SelectionRequest events. */
9159
9160void
db3906fd
RS
9161x_stop_queuing_selection_requests (display)
9162 Display *display;
09756a85
RS
9163{
9164 x_queue_selection_requests--;
db3906fd 9165 x_unqueue_events (display);
09756a85 9166}
f451eb13
JB
9167\f
9168/* The main X event-reading loop - XTread_socket. */
dc6f92b8 9169
06a2c219 9170/* Time stamp of enter window event. This is only used by XTread_socket,
dc6f92b8
JB
9171 but we have to put it out here, since static variables within functions
9172 sometimes don't work. */
06a2c219 9173
dc6f92b8
JB
9174static Time enter_timestamp;
9175
11edeb03 9176/* This holds the state XLookupString needs to implement dead keys
58769bee 9177 and other tricks known as "compose processing". _X Window System_
11edeb03
JB
9178 says that a portable program can't use this, but Stephen Gildea assures
9179 me that letting the compiler initialize it to zeros will work okay.
9180
9181 This must be defined outside of XTread_socket, for the same reasons
06a2c219
GM
9182 given for enter_time stamp, above. */
9183
11edeb03
JB
9184static XComposeStatus compose_status;
9185
10e6549c
RS
9186/* Record the last 100 characters stored
9187 to help debug the loss-of-chars-during-GC problem. */
06a2c219 9188
2224b905
RS
9189static int temp_index;
9190static short temp_buffer[100];
10e6549c 9191
7a13e894
RS
9192/* Set this to nonzero to fake an "X I/O error"
9193 on a particular display. */
06a2c219 9194
7a13e894
RS
9195struct x_display_info *XTread_socket_fake_io_error;
9196
2224b905
RS
9197/* When we find no input here, we occasionally do a no-op command
9198 to verify that the X server is still running and we can still talk with it.
9199 We try all the open displays, one by one.
9200 This variable is used for cycling thru the displays. */
06a2c219 9201
2224b905
RS
9202static struct x_display_info *next_noop_dpyinfo;
9203
06a2c219
GM
9204#define SET_SAVED_MENU_EVENT(size) \
9205 do \
9206 { \
9207 if (f->output_data.x->saved_menu_event == 0) \
9208 f->output_data.x->saved_menu_event \
9209 = (XEvent *) xmalloc (sizeof (XEvent)); \
9210 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9211 if (numchars >= 1) \
9212 { \
9213 bufp->kind = menu_bar_activate_event; \
9214 XSETFRAME (bufp->frame_or_window, f); \
0f8aabe9 9215 bufp->arg = Qnil; \
06a2c219
GM
9216 bufp++; \
9217 count++; \
9218 numchars--; \
9219 } \
9220 } \
9221 while (0)
9222
8805890a 9223#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
06a2c219 9224#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8805890a 9225
dc6f92b8
JB
9226/* Read events coming from the X server.
9227 This routine is called by the SIGIO handler.
9228 We return as soon as there are no more events to be read.
9229
9230 Events representing keys are stored in buffer BUFP,
9231 which can hold up to NUMCHARS characters.
9232 We return the number of characters stored into the buffer,
9233 thus pretending to be `read'.
9234
dc6f92b8
JB
9235 EXPECTED is nonzero if the caller knows input is available. */
9236
7c5283e4 9237int
f66868ba 9238XTread_socket (sd, bufp, numchars, expected)
dc6f92b8 9239 register int sd;
8805890a
KH
9240 /* register */ struct input_event *bufp;
9241 /* register */ int numchars;
dc6f92b8
JB
9242 int expected;
9243{
9244 int count = 0;
9245 int nbytes = 0;
dc6f92b8 9246 XEvent event;
f676886a 9247 struct frame *f;
66f55a9d 9248 int event_found = 0;
334208b7 9249 struct x_display_info *dpyinfo;
379b5ac0 9250 struct coding_system coding;
dc6f92b8 9251
9ac0d9e0 9252 if (interrupt_input_blocked)
dc6f92b8 9253 {
9ac0d9e0 9254 interrupt_input_pending = 1;
dc6f92b8
JB
9255 return -1;
9256 }
9257
9ac0d9e0 9258 interrupt_input_pending = 0;
dc6f92b8 9259 BLOCK_INPUT;
c0a04927
RS
9260
9261 /* So people can tell when we have read the available input. */
9262 input_signal_count++;
9263
dc6f92b8 9264 if (numchars <= 0)
06a2c219 9265 abort (); /* Don't think this happens. */
dc6f92b8 9266
bde5503b
GM
9267 ++handling_signal;
9268
379b5ac0
KH
9269 /* The input should be decoded if it is from XIM. Currently the
9270 locale of XIM is the same as that of the system. So, we can use
9271 Vlocale_coding_system which is initialized properly at Emacs
9272 startup time. */
9273 setup_coding_system (Vlocale_coding_system, &coding);
9274 coding.src_multibyte = 0;
9275 coding.dst_multibyte = 1;
9276 /* The input is converted to events, thus we can't handle
9277 composition. Anyway, there's no XIM that gives us composition
9278 information. */
9279 coding.composing = COMPOSITION_DISABLED;
9280
7a13e894
RS
9281 /* Find the display we are supposed to read input for.
9282 It's the one communicating on descriptor SD. */
9283 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9284 {
9285#if 0 /* This ought to be unnecessary; let's verify it. */
dc6f92b8 9286#ifdef FIOSNBIO
7a13e894
RS
9287 /* If available, Xlib uses FIOSNBIO to make the socket
9288 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
e6cbea31 9289 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
06a2c219 9290 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7a13e894 9291 fcntl (dpyinfo->connection, F_SETFL, 0);
c118dd06 9292#endif /* ! defined (FIOSNBIO) */
7a13e894 9293#endif
dc6f92b8 9294
7a13e894
RS
9295#if 0 /* This code can't be made to work, with multiple displays,
9296 and appears not to be used on any system any more.
9297 Also keyboard.c doesn't turn O_NDELAY on and off
9298 for X connections. */
dc6f92b8
JB
9299#ifndef SIGIO
9300#ifndef HAVE_SELECT
7a13e894
RS
9301 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9302 {
9303 extern int read_alarm_should_throw;
9304 read_alarm_should_throw = 1;
9305 XPeekEvent (dpyinfo->display, &event);
9306 read_alarm_should_throw = 0;
9307 }
c118dd06
JB
9308#endif /* HAVE_SELECT */
9309#endif /* SIGIO */
7a13e894 9310#endif
dc6f92b8 9311
7a13e894
RS
9312 /* For debugging, this gives a way to fake an I/O error. */
9313 if (dpyinfo == XTread_socket_fake_io_error)
9314 {
9315 XTread_socket_fake_io_error = 0;
9316 x_io_error_quitter (dpyinfo->display);
9317 }
dc6f92b8 9318
06a2c219 9319 while (XPending (dpyinfo->display))
dc6f92b8 9320 {
7a13e894 9321 XNextEvent (dpyinfo->display, &event);
06a2c219 9322
531483fb 9323#ifdef HAVE_X_I18N
d1bc4182 9324 {
f2be1146
GM
9325 /* Filter events for the current X input method.
9326 XFilterEvent returns non-zero if the input method has
9327 consumed the event. We pass the frame's X window to
9328 XFilterEvent because that's the one for which the IC
9329 was created. */
f5d11644
GM
9330 struct frame *f1 = x_any_window_to_frame (dpyinfo,
9331 event.xclient.window);
9332 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
d1bc4182
RS
9333 break;
9334 }
0cd6403b 9335#endif
7a13e894
RS
9336 event_found = 1;
9337
9338 switch (event.type)
9339 {
9340 case ClientMessage:
c047688c 9341 {
7a13e894
RS
9342 if (event.xclient.message_type
9343 == dpyinfo->Xatom_wm_protocols
9344 && event.xclient.format == 32)
c047688c 9345 {
7a13e894
RS
9346 if (event.xclient.data.l[0]
9347 == dpyinfo->Xatom_wm_take_focus)
c047688c 9348 {
8c1a6a84
RS
9349 /* Use x_any_window_to_frame because this
9350 could be the shell widget window
9351 if the frame has no title bar. */
9352 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6c183ba5
RS
9353#ifdef HAVE_X_I18N
9354 /* Not quite sure this is needed -pd */
8c1a6a84 9355 if (f && FRAME_XIC (f))
6c183ba5
RS
9356 XSetICFocus (FRAME_XIC (f));
9357#endif
f1da8f06
GM
9358#if 0 /* Emacs sets WM hints whose `input' field is `true'. This
9359 instructs the WM to set the input focus automatically for
9360 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
9361 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
9362 it has set the focus. So, XSetInputFocus below is not
9363 needed.
9364
9365 The call to XSetInputFocus below has also caused trouble. In
9366 cases where the XSetInputFocus done by the WM and the one
9367 below are temporally close (on a fast machine), the call
9368 below can generate additional FocusIn events which confuse
9369 Emacs. */
9370
bf7253f4
RS
9371 /* Since we set WM_TAKE_FOCUS, we must call
9372 XSetInputFocus explicitly. But not if f is null,
9373 since that might be an event for a deleted frame. */
7a13e894 9374 if (f)
bf7253f4
RS
9375 {
9376 Display *d = event.xclient.display;
9377 /* Catch and ignore errors, in case window has been
9378 iconified by a window manager such as GWM. */
9379 int count = x_catch_errors (d);
9380 XSetInputFocus (d, event.xclient.window,
e1f6572f
RS
9381 /* The ICCCM says this is
9382 the only valid choice. */
9383 RevertToParent,
bf7253f4
RS
9384 event.xclient.data.l[1]);
9385 /* This is needed to detect the error
9386 if there is an error. */
9387 XSync (d, False);
9388 x_uncatch_errors (d, count);
9389 }
7a13e894 9390 /* Not certain about handling scroll bars here */
f1da8f06 9391#endif /* 0 */
c047688c 9392 }
7a13e894
RS
9393 else if (event.xclient.data.l[0]
9394 == dpyinfo->Xatom_wm_save_yourself)
9395 {
9396 /* Save state modify the WM_COMMAND property to
06a2c219 9397 something which can reinstate us. This notifies
7a13e894
RS
9398 the session manager, who's looking for such a
9399 PropertyNotify. Can restart processing when
06a2c219 9400 a keyboard or mouse event arrives. */
7a13e894
RS
9401 if (numchars > 0)
9402 {
19126e11
KH
9403 f = x_top_window_to_frame (dpyinfo,
9404 event.xclient.window);
7a13e894
RS
9405
9406 /* This is just so we only give real data once
9407 for a single Emacs process. */
b86bd3dd 9408 if (f == SELECTED_FRAME ())
7a13e894
RS
9409 XSetCommand (FRAME_X_DISPLAY (f),
9410 event.xclient.window,
9411 initial_argv, initial_argc);
f000f5c5 9412 else if (f)
7a13e894
RS
9413 XSetCommand (FRAME_X_DISPLAY (f),
9414 event.xclient.window,
9415 0, 0);
9416 }
9417 }
9418 else if (event.xclient.data.l[0]
9419 == dpyinfo->Xatom_wm_delete_window)
1fb20991 9420 {
19126e11
KH
9421 struct frame *f
9422 = x_any_window_to_frame (dpyinfo,
9423 event.xclient.window);
1fb20991 9424
7a13e894
RS
9425 if (f)
9426 {
9427 if (numchars == 0)
9428 abort ();
1fb20991 9429
7a13e894
RS
9430 bufp->kind = delete_window_event;
9431 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9432 bufp->arg = Qnil;
7a13e894
RS
9433 bufp++;
9434
9435 count += 1;
9436 numchars -= 1;
9437 }
1fb20991 9438 }
c047688c 9439 }
7a13e894
RS
9440 else if (event.xclient.message_type
9441 == dpyinfo->Xatom_wm_configure_denied)
9442 {
9443 }
9444 else if (event.xclient.message_type
9445 == dpyinfo->Xatom_wm_window_moved)
9446 {
9447 int new_x, new_y;
19126e11
KH
9448 struct frame *f
9449 = x_window_to_frame (dpyinfo, event.xclient.window);
58769bee 9450
7a13e894
RS
9451 new_x = event.xclient.data.s[0];
9452 new_y = event.xclient.data.s[1];
1fb20991 9453
7a13e894
RS
9454 if (f)
9455 {
7556890b
RS
9456 f->output_data.x->left_pos = new_x;
9457 f->output_data.x->top_pos = new_y;
7a13e894 9458 }
1fb20991 9459 }
0fdff6bb 9460#ifdef HACK_EDITRES
7a13e894
RS
9461 else if (event.xclient.message_type
9462 == dpyinfo->Xatom_editres)
9463 {
19126e11
KH
9464 struct frame *f
9465 = x_any_window_to_frame (dpyinfo, event.xclient.window);
7556890b 9466 _XEditResCheckMessages (f->output_data.x->widget, NULL,
19126e11 9467 &event, NULL);
7a13e894 9468 }
0fdff6bb 9469#endif /* HACK_EDITRES */
06a2c219
GM
9470 else if ((event.xclient.message_type
9471 == dpyinfo->Xatom_DONE)
9472 || (event.xclient.message_type
9473 == dpyinfo->Xatom_PAGE))
9474 {
9475 /* Ghostview job completed. Kill it. We could
9476 reply with "Next" if we received "Page", but we
9477 currently never do because we are interested in
9478 images, only, which should have 1 page. */
06a2c219
GM
9479 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9480 struct frame *f
9481 = x_window_to_frame (dpyinfo, event.xclient.window);
9482 x_kill_gs_process (pixmap, f);
9483 expose_frame (f, 0, 0, 0, 0);
9484 }
9485#ifdef USE_TOOLKIT_SCROLL_BARS
9486 /* Scroll bar callbacks send a ClientMessage from which
9487 we construct an input_event. */
9488 else if (event.xclient.message_type
9489 == dpyinfo->Xatom_Scrollbar)
9490 {
9491 x_scroll_bar_to_input_event (&event, bufp);
9492 ++bufp, ++count, --numchars;
9493 goto out;
9494 }
9495#endif /* USE_TOOLKIT_SCROLL_BARS */
9496 else
9497 goto OTHER;
7a13e894
RS
9498 }
9499 break;
dc6f92b8 9500
7a13e894 9501 case SelectionNotify:
3afe33e7 9502#ifdef USE_X_TOOLKIT
19126e11 9503 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
7a13e894 9504 goto OTHER;
3afe33e7 9505#endif /* not USE_X_TOOLKIT */
dfcf069d 9506 x_handle_selection_notify (&event.xselection);
7a13e894 9507 break;
d56a553a 9508
06a2c219 9509 case SelectionClear: /* Someone has grabbed ownership. */
3afe33e7 9510#ifdef USE_X_TOOLKIT
19126e11 9511 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
7a13e894 9512 goto OTHER;
3afe33e7 9513#endif /* USE_X_TOOLKIT */
7a13e894
RS
9514 {
9515 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
d56a553a 9516
7a13e894
RS
9517 if (numchars == 0)
9518 abort ();
d56a553a 9519
7a13e894
RS
9520 bufp->kind = selection_clear_event;
9521 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9522 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9523 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9524 bufp->frame_or_window = Qnil;
0f8aabe9 9525 bufp->arg = Qnil;
7a13e894 9526 bufp++;
d56a553a 9527
7a13e894
RS
9528 count += 1;
9529 numchars -= 1;
9530 }
9531 break;
dc6f92b8 9532
06a2c219 9533 case SelectionRequest: /* Someone wants our selection. */
3afe33e7 9534#ifdef USE_X_TOOLKIT
19126e11 9535 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
7a13e894 9536 goto OTHER;
3afe33e7 9537#endif /* USE_X_TOOLKIT */
7a13e894 9538 if (x_queue_selection_requests)
19126e11 9539 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
7a13e894
RS
9540 &event);
9541 else
9542 {
9543 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
dc6f92b8 9544
7a13e894
RS
9545 if (numchars == 0)
9546 abort ();
9547
9548 bufp->kind = selection_request_event;
9549 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9550 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9551 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9552 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9553 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9554 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9555 bufp->frame_or_window = Qnil;
0f8aabe9 9556 bufp->arg = Qnil;
7a13e894
RS
9557 bufp++;
9558
9559 count += 1;
9560 numchars -= 1;
9561 }
9562 break;
9563
9564 case PropertyNotify:
3afe33e7 9565#ifdef USE_X_TOOLKIT
19126e11 9566 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
7a13e894 9567 goto OTHER;
3afe33e7 9568#endif /* not USE_X_TOOLKIT */
dfcf069d 9569 x_handle_property_notify (&event.xproperty);
7a13e894 9570 break;
dc6f92b8 9571
7a13e894 9572 case ReparentNotify:
19126e11 9573 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
7a13e894
RS
9574 if (f)
9575 {
9576 int x, y;
7556890b 9577 f->output_data.x->parent_desc = event.xreparent.parent;
7a13e894 9578 x_real_positions (f, &x, &y);
7556890b
RS
9579 f->output_data.x->left_pos = x;
9580 f->output_data.x->top_pos = y;
7a13e894
RS
9581 }
9582 break;
3bd330d4 9583
7a13e894 9584 case Expose:
19126e11 9585 f = x_window_to_frame (dpyinfo, event.xexpose.window);
7a13e894 9586 if (f)
dc6f92b8 9587 {
7a13e894
RS
9588 if (f->async_visible == 0)
9589 {
9590 f->async_visible = 1;
9591 f->async_iconified = 0;
06c488fd 9592 f->output_data.x->has_been_visible = 1;
7a13e894
RS
9593 SET_FRAME_GARBAGED (f);
9594 }
9595 else
06a2c219
GM
9596 expose_frame (x_window_to_frame (dpyinfo,
9597 event.xexpose.window),
9598 event.xexpose.x, event.xexpose.y,
9599 event.xexpose.width, event.xexpose.height);
dc6f92b8
JB
9600 }
9601 else
7a13e894 9602 {
06a2c219
GM
9603#ifdef USE_TOOLKIT_SCROLL_BARS
9604 /* Dispatch event to the widget. */
9605 goto OTHER;
9606#else /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9607 struct scroll_bar *bar
9608 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 9609
7a13e894
RS
9610 if (bar)
9611 x_scroll_bar_expose (bar, &event);
3afe33e7 9612#ifdef USE_X_TOOLKIT
7a13e894
RS
9613 else
9614 goto OTHER;
3afe33e7 9615#endif /* USE_X_TOOLKIT */
06a2c219 9616#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9617 }
9618 break;
dc6f92b8 9619
7a13e894
RS
9620 case GraphicsExpose: /* This occurs when an XCopyArea's
9621 source area was obscured or not
9622 available.*/
19126e11 9623 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
7a13e894
RS
9624 if (f)
9625 {
06a2c219
GM
9626 expose_frame (f,
9627 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9628 event.xgraphicsexpose.width,
9629 event.xgraphicsexpose.height);
7a13e894 9630 }
3afe33e7 9631#ifdef USE_X_TOOLKIT
7a13e894
RS
9632 else
9633 goto OTHER;
3afe33e7 9634#endif /* USE_X_TOOLKIT */
7a13e894 9635 break;
dc6f92b8 9636
7a13e894 9637 case NoExpose: /* This occurs when an XCopyArea's
06a2c219
GM
9638 source area was completely
9639 available */
7a13e894 9640 break;
dc6f92b8 9641
7a13e894 9642 case UnmapNotify:
06a2c219
GM
9643 /* Redo the mouse-highlight after the tooltip has gone. */
9644 if (event.xmap.window == tip_window)
9645 {
9646 tip_window = 0;
9647 redo_mouse_highlight ();
9648 }
9649
91ea2a7a 9650 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
7a13e894
RS
9651 if (f) /* F may no longer exist if
9652 the frame was deleted. */
9653 {
9654 /* While a frame is unmapped, display generation is
9655 disabled; you don't want to spend time updating a
9656 display that won't ever be seen. */
9657 f->async_visible = 0;
9658 /* We can't distinguish, from the event, whether the window
9659 has become iconified or invisible. So assume, if it
9660 was previously visible, than now it is iconified.
1aa6072f
RS
9661 But x_make_frame_invisible clears both
9662 the visible flag and the iconified flag;
9663 and that way, we know the window is not iconified now. */
7a13e894 9664 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
1aa6072f
RS
9665 {
9666 f->async_iconified = 1;
bddd097c 9667
1aa6072f
RS
9668 bufp->kind = iconify_event;
9669 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9670 bufp->arg = Qnil;
1aa6072f
RS
9671 bufp++;
9672 count++;
9673 numchars--;
9674 }
7a13e894 9675 }
7a13e894 9676 goto OTHER;
dc6f92b8 9677
7a13e894 9678 case MapNotify:
06a2c219
GM
9679 if (event.xmap.window == tip_window)
9680 /* The tooltip has been drawn already. Avoid
9681 the SET_FRAME_GARBAGED below. */
9682 goto OTHER;
9683
9684 /* We use x_top_window_to_frame because map events can
9685 come for sub-windows and they don't mean that the
9686 frame is visible. */
19126e11 9687 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
7a13e894
RS
9688 if (f)
9689 {
9690 f->async_visible = 1;
9691 f->async_iconified = 0;
06c488fd 9692 f->output_data.x->has_been_visible = 1;
dc6f92b8 9693
7a13e894
RS
9694 /* wait_reading_process_input will notice this and update
9695 the frame's display structures. */
9696 SET_FRAME_GARBAGED (f);
bddd097c 9697
d806e720
RS
9698 if (f->iconified)
9699 {
9700 bufp->kind = deiconify_event;
9701 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9702 bufp->arg = Qnil;
d806e720
RS
9703 bufp++;
9704 count++;
9705 numchars--;
9706 }
e73ec6fa 9707 else if (! NILP (Vframe_list)
8e713be6 9708 && ! NILP (XCDR (Vframe_list)))
78aa2ba5
KH
9709 /* Force a redisplay sooner or later
9710 to update the frame titles
9711 in case this is the second frame. */
9712 record_asynch_buffer_change ();
7a13e894 9713 }
7a13e894 9714 goto OTHER;
dc6f92b8 9715
7a13e894 9716 case KeyPress:
19126e11 9717 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
f451eb13 9718
eccc05db 9719#if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
9720 /* I couldn't find a way to prevent LessTif scroll bars
9721 from consuming key events. */
9722 if (f == 0)
9723 {
9724 Widget widget = XtWindowToWidget (dpyinfo->display,
9725 event.xkey.window);
9726 if (widget && XmIsScrollBar (widget))
9727 {
9728 widget = XtParent (widget);
9729 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9730 }
9731 }
eccc05db 9732#endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
06a2c219 9733
7a13e894
RS
9734 if (f != 0)
9735 {
9736 KeySym keysym, orig_keysym;
379b5ac0
KH
9737 /* al%imercury@uunet.uu.net says that making this 81
9738 instead of 80 fixed a bug whereby meta chars made
9739 his Emacs hang.
9740
9741 It seems that some version of XmbLookupString has
9742 a bug of not returning XBufferOverflow in
9743 status_return even if the input is too long to
9744 fit in 81 bytes. So, we must prepare sufficient
9745 bytes for copy_buffer. 513 bytes (256 chars for
9746 two-byte character set) seems to be a faily good
9747 approximation. -- 2000.8.10 handa@etl.go.jp */
9748 unsigned char copy_buffer[513];
9749 unsigned char *copy_bufptr = copy_buffer;
9750 int copy_bufsiz = sizeof (copy_buffer);
7a13e894 9751 int modifiers;
64bb1782 9752
7a13e894
RS
9753 event.xkey.state
9754 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9755 extra_keyboard_modifiers);
9756 modifiers = event.xkey.state;
3a2712f9 9757
7a13e894 9758 /* This will have to go some day... */
752a043f 9759
7a13e894
RS
9760 /* make_lispy_event turns chars into control chars.
9761 Don't do it here because XLookupString is too eager. */
9762 event.xkey.state &= ~ControlMask;
5d46f928
RS
9763 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9764 | dpyinfo->super_mod_mask
9765 | dpyinfo->hyper_mod_mask
9766 | dpyinfo->alt_mod_mask);
9767
1cf4a0d1
RS
9768 /* In case Meta is ComposeCharacter,
9769 clear its status. According to Markus Ehrnsperger
9770 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9771 this enables ComposeCharacter to work whether or
9772 not it is combined with Meta. */
9773 if (modifiers & dpyinfo->meta_mod_mask)
9774 bzero (&compose_status, sizeof (compose_status));
9775
6c183ba5
RS
9776#ifdef HAVE_X_I18N
9777 if (FRAME_XIC (f))
9778 {
f5d11644
GM
9779 Status status_return;
9780
6c183ba5 9781 nbytes = XmbLookupString (FRAME_XIC (f),
f5d11644
GM
9782 &event.xkey, copy_bufptr,
9783 copy_bufsiz, &keysym,
6c183ba5 9784 &status_return);
f5d11644
GM
9785 if (status_return == XBufferOverflow)
9786 {
9787 copy_bufsiz = nbytes + 1;
9788 copy_bufptr = (char *) alloca (copy_bufsiz);
9789 nbytes = XmbLookupString (FRAME_XIC (f),
9790 &event.xkey, copy_bufptr,
9791 copy_bufsiz, &keysym,
9792 &status_return);
9793 }
9794
1decb680
PE
9795 if (status_return == XLookupNone)
9796 break;
9797 else if (status_return == XLookupChars)
fdd9d55e
GM
9798 {
9799 keysym = NoSymbol;
9800 modifiers = 0;
9801 }
1decb680
PE
9802 else if (status_return != XLookupKeySym
9803 && status_return != XLookupBoth)
9804 abort ();
6c183ba5
RS
9805 }
9806 else
379b5ac0
KH
9807 nbytes = XLookupString (&event.xkey, copy_bufptr,
9808 copy_bufsiz, &keysym,
9809 &compose_status);
6c183ba5 9810#else
379b5ac0
KH
9811 nbytes = XLookupString (&event.xkey, copy_bufptr,
9812 copy_bufsiz, &keysym,
9813 &compose_status);
6c183ba5 9814#endif
dc6f92b8 9815
7a13e894 9816 orig_keysym = keysym;
55123275 9817
7a13e894
RS
9818 if (numchars > 1)
9819 {
9820 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9821 || keysym == XK_Delete
1097aea0 9822#ifdef XK_ISO_Left_Tab
441affdb 9823 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
1097aea0 9824#endif
852bff8f 9825 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
7a13e894
RS
9826 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9827 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
c34790e0 9828#ifdef HPUX
7a13e894
RS
9829 /* This recognizes the "extended function keys".
9830 It seems there's no cleaner way.
9831 Test IsModifierKey to avoid handling mode_switch
9832 incorrectly. */
9833 || ((unsigned) (keysym) >= XK_Select
9834 && (unsigned)(keysym) < XK_KP_Space)
69388238
RS
9835#endif
9836#ifdef XK_dead_circumflex
7a13e894 9837 || orig_keysym == XK_dead_circumflex
69388238
RS
9838#endif
9839#ifdef XK_dead_grave
7a13e894 9840 || orig_keysym == XK_dead_grave
69388238
RS
9841#endif
9842#ifdef XK_dead_tilde
7a13e894 9843 || orig_keysym == XK_dead_tilde
69388238
RS
9844#endif
9845#ifdef XK_dead_diaeresis
7a13e894 9846 || orig_keysym == XK_dead_diaeresis
69388238
RS
9847#endif
9848#ifdef XK_dead_macron
7a13e894 9849 || orig_keysym == XK_dead_macron
69388238
RS
9850#endif
9851#ifdef XK_dead_degree
7a13e894 9852 || orig_keysym == XK_dead_degree
69388238
RS
9853#endif
9854#ifdef XK_dead_acute
7a13e894 9855 || orig_keysym == XK_dead_acute
69388238
RS
9856#endif
9857#ifdef XK_dead_cedilla
7a13e894 9858 || orig_keysym == XK_dead_cedilla
69388238
RS
9859#endif
9860#ifdef XK_dead_breve
7a13e894 9861 || orig_keysym == XK_dead_breve
69388238
RS
9862#endif
9863#ifdef XK_dead_ogonek
7a13e894 9864 || orig_keysym == XK_dead_ogonek
69388238
RS
9865#endif
9866#ifdef XK_dead_caron
7a13e894 9867 || orig_keysym == XK_dead_caron
69388238
RS
9868#endif
9869#ifdef XK_dead_doubleacute
7a13e894 9870 || orig_keysym == XK_dead_doubleacute
69388238
RS
9871#endif
9872#ifdef XK_dead_abovedot
7a13e894 9873 || orig_keysym == XK_dead_abovedot
c34790e0 9874#endif
7a13e894
RS
9875 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9876 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9877 /* Any "vendor-specific" key is ok. */
9878 || (orig_keysym & (1 << 28)))
9879 && ! (IsModifierKey (orig_keysym)
7719aa06
RS
9880#ifndef HAVE_X11R5
9881#ifdef XK_Mode_switch
7a13e894 9882 || ((unsigned)(orig_keysym) == XK_Mode_switch)
7719aa06
RS
9883#endif
9884#ifdef XK_Num_Lock
7a13e894 9885 || ((unsigned)(orig_keysym) == XK_Num_Lock)
7719aa06
RS
9886#endif
9887#endif /* not HAVE_X11R5 */
7a13e894 9888 ))
dc6f92b8 9889 {
10e6549c
RS
9890 if (temp_index == sizeof temp_buffer / sizeof (short))
9891 temp_index = 0;
7a13e894
RS
9892 temp_buffer[temp_index++] = keysym;
9893 bufp->kind = non_ascii_keystroke;
9894 bufp->code = keysym;
e0c1aef2 9895 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9896 bufp->arg = Qnil;
334208b7
RS
9897 bufp->modifiers
9898 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9899 modifiers);
1113d9db 9900 bufp->timestamp = event.xkey.time;
dc6f92b8 9901 bufp++;
7a13e894
RS
9902 count++;
9903 numchars--;
dc6f92b8 9904 }
7a13e894
RS
9905 else if (numchars > nbytes)
9906 {
9907 register int i;
379b5ac0
KH
9908 register int c;
9909 unsigned char *p, *pend;
9910 int nchars, len;
7a13e894
RS
9911
9912 for (i = 0; i < nbytes; i++)
9913 {
379b5ac0
KH
9914 if (temp_index == (sizeof temp_buffer
9915 / sizeof (short)))
7a13e894 9916 temp_index = 0;
379b5ac0
KH
9917 temp_buffer[temp_index++] = copy_bufptr[i];
9918 }
9919
9920 if (/* If the event is not from XIM, */
9921 event.xkey.keycode != 0
9922 /* or the current locale doesn't request
9923 decoding of the intup data, ... */
9924 || coding.type == coding_type_raw_text
9925 || coding.type == coding_type_no_conversion)
9926 {
9927 /* ... we can use the input data as is. */
9928 nchars = nbytes;
9929 }
9930 else
9931 {
9932 /* We have to decode the input data. */
9933 int require;
9934 unsigned char *p;
9935
9936 require = decoding_buffer_size (&coding, nbytes);
9937 p = (unsigned char *) alloca (require);
9938 coding.mode |= CODING_MODE_LAST_BLOCK;
9939 decode_coding (&coding, copy_bufptr, p,
9940 nbytes, require);
9941 nbytes = coding.produced;
9942 nchars = coding.produced_char;
9943 copy_bufptr = p;
9944 }
9945
9946 /* Convert the input data to a sequence of
9947 character events. */
9948 for (i = 0; i < nbytes; i += len)
9949 {
9950 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
9951 nbytes - i, len);
9952 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
9953 ? ascii_keystroke
9954 : multibyte_char_keystroke);
9955 bufp->code = c;
7a13e894 9956 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9957 bufp->arg = Qnil;
7a13e894
RS
9958 bufp->modifiers
9959 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9960 modifiers);
9961 bufp->timestamp = event.xkey.time;
9962 bufp++;
9963 }
9964
379b5ac0
KH
9965 count += nchars;
9966 numchars -= nchars;
1decb680
PE
9967
9968 if (keysym == NoSymbol)
9969 break;
7a13e894
RS
9970 }
9971 else
9972 abort ();
dc6f92b8 9973 }
10e6549c
RS
9974 else
9975 abort ();
dc6f92b8 9976 }
59ddecde
GM
9977#ifdef HAVE_X_I18N
9978 /* Don't dispatch this event since XtDispatchEvent calls
9979 XFilterEvent, and two calls in a row may freeze the
9980 client. */
9981 break;
9982#else
717ca130 9983 goto OTHER;
59ddecde 9984#endif
f451eb13 9985
f5d11644 9986 case KeyRelease:
59ddecde
GM
9987#ifdef HAVE_X_I18N
9988 /* Don't dispatch this event since XtDispatchEvent calls
9989 XFilterEvent, and two calls in a row may freeze the
9990 client. */
9991 break;
9992#else
f5d11644 9993 goto OTHER;
59ddecde 9994#endif
f5d11644 9995
7a13e894 9996 /* Here's a possible interpretation of the whole
06a2c219
GM
9997 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
9998 you get a FocusIn event, you have to get a FocusOut
9999 event before you relinquish the focus. If you
10000 haven't received a FocusIn event, then a mere
10001 LeaveNotify is enough to free you. */
f451eb13 10002
7a13e894 10003 case EnterNotify:
06a2c219
GM
10004 {
10005 int from_menu_bar_p = 0;
10006
10007 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10008
10009#ifdef LESSTIF_VERSION
10010 /* When clicking outside of a menu bar popup to close
10011 it, we get a FocusIn/ EnterNotify sequence of
10012 events. The flag event.xcrossing.focus is not set
10013 in the EnterNotify event of that sequence because
10014 the focus is in the menu bar,
10015 event.xcrossing.window is the frame's X window.
10016 Unconditionally setting the focus frame to null in
10017 this case is not the right thing, because no event
10018 follows that could set the focus frame to the right
10019 value.
10020
10021 This could be a LessTif bug, but I wasn't able to
10022 reproduce the behavior in a simple test program.
10023
10024 (gerd, LessTif 0.88.1). */
10025
10026 if (!event.xcrossing.focus
10027 && f
10028 && f->output_data.x->menubar_widget)
10029 {
10030 Window focus;
10031 int revert;
10032
10033 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
10034 if (focus == XtWindow (f->output_data.x->menubar_widget))
10035 from_menu_bar_p = 1;
10036 }
10037#endif /* LESSTIF_VERSION */
6d4238f3 10038
06a2c219
GM
10039 if (event.xcrossing.focus || from_menu_bar_p)
10040 {
10041 /* Avoid nasty pop/raise loops. */
10042 if (f && (!(f->auto_raise)
10043 || !(f->auto_lower)
10044 || (event.xcrossing.time - enter_timestamp) > 500))
10045 {
10046 x_new_focus_frame (dpyinfo, f);
10047 enter_timestamp = event.xcrossing.time;
10048 }
10049 }
10050 else if (f == dpyinfo->x_focus_frame)
10051 x_new_focus_frame (dpyinfo, 0);
10052
10053 /* EnterNotify counts as mouse movement,
10054 so update things that depend on mouse position. */
10055 if (f && !f->output_data.x->busy_p)
10056 note_mouse_movement (f, &event.xmotion);
10057 goto OTHER;
10058 }
dc6f92b8 10059
7a13e894 10060 case FocusIn:
19126e11 10061 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 10062 if (event.xfocus.detail != NotifyPointer)
0f941935 10063 dpyinfo->x_focus_event_frame = f;
7a13e894 10064 if (f)
eb72635f
GM
10065 {
10066 x_new_focus_frame (dpyinfo, f);
10067
10068 /* Don't stop displaying the initial startup message
10069 for a switch-frame event we don't need. */
10070 if (GC_NILP (Vterminal_frame)
10071 && GC_CONSP (Vframe_list)
10072 && !GC_NILP (XCDR (Vframe_list)))
10073 {
10074 bufp->kind = FOCUS_IN_EVENT;
10075 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10076 bufp->arg = Qnil;
eb72635f
GM
10077 ++bufp, ++count, --numchars;
10078 }
10079 }
f9e24cb9 10080
6c183ba5
RS
10081#ifdef HAVE_X_I18N
10082 if (f && FRAME_XIC (f))
10083 XSetICFocus (FRAME_XIC (f));
10084#endif
10085
7a13e894 10086 goto OTHER;
10c5e63d 10087
7a13e894 10088 case LeaveNotify:
19126e11 10089 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
7a13e894 10090 if (f)
10c5e63d 10091 {
06a2c219
GM
10092 Lisp_Object frame;
10093 int from_menu_bar_p = 0;
10094
7a13e894 10095 if (f == dpyinfo->mouse_face_mouse_frame)
06a2c219
GM
10096 {
10097 /* If we move outside the frame, then we're
10098 certainly no longer on any text in the frame. */
10099 clear_mouse_face (dpyinfo);
10100 dpyinfo->mouse_face_mouse_frame = 0;
10101 }
10102
10103 /* Generate a nil HELP_EVENT to cancel a help-echo.
10104 Do it only if there's something to cancel.
10105 Otherwise, the startup message is cleared when
10106 the mouse leaves the frame. */
10107 if (any_help_event_p)
10108 {
be010514
GM
10109 Lisp_Object frame;
10110 int n;
10111
06a2c219 10112 XSETFRAME (frame, f);
5ab2570d
GM
10113 n = gen_help_event (bufp, numchars,
10114 Qnil, frame, Qnil, Qnil, 0);
be010514 10115 bufp += n, count += n, numchars -= n;
06a2c219 10116 }
7a13e894 10117
06a2c219
GM
10118#ifdef LESSTIF_VERSION
10119 /* Please see the comment at the start of the
10120 EnterNotify case. */
10121 if (!event.xcrossing.focus
10122 && f->output_data.x->menubar_widget)
10123 {
10124 Window focus;
10125 int revert;
10126 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
10127 if (focus == XtWindow (f->output_data.x->menubar_widget))
10128 from_menu_bar_p = 1;
10129 }
10130#endif /* LESSTIF_VERSION */
10131
10132 if (event.xcrossing.focus || from_menu_bar_p)
0f941935 10133 x_mouse_leave (dpyinfo);
10c5e63d 10134 else
7a13e894 10135 {
0f941935
KH
10136 if (f == dpyinfo->x_focus_event_frame)
10137 dpyinfo->x_focus_event_frame = 0;
10138 if (f == dpyinfo->x_focus_frame)
10139 x_new_focus_frame (dpyinfo, 0);
7a13e894 10140 }
10c5e63d 10141 }
7a13e894 10142 goto OTHER;
dc6f92b8 10143
7a13e894 10144 case FocusOut:
19126e11 10145 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 10146 if (event.xfocus.detail != NotifyPointer
0f941935
KH
10147 && f == dpyinfo->x_focus_event_frame)
10148 dpyinfo->x_focus_event_frame = 0;
10149 if (f && f == dpyinfo->x_focus_frame)
10150 x_new_focus_frame (dpyinfo, 0);
f9e24cb9 10151
6c183ba5
RS
10152#ifdef HAVE_X_I18N
10153 if (f && FRAME_XIC (f))
10154 XUnsetICFocus (FRAME_XIC (f));
10155#endif
10156
7a13e894 10157 goto OTHER;
dc6f92b8 10158
7a13e894 10159 case MotionNotify:
dc6f92b8 10160 {
06a2c219 10161 previous_help_echo = help_echo;
7cea38bc 10162 help_echo = help_echo_object = help_echo_window = Qnil;
be010514 10163 help_echo_pos = -1;
06a2c219 10164
7a13e894
RS
10165 if (dpyinfo->grabbed && last_mouse_frame
10166 && FRAME_LIVE_P (last_mouse_frame))
10167 f = last_mouse_frame;
10168 else
19126e11 10169 f = x_window_to_frame (dpyinfo, event.xmotion.window);
06a2c219 10170
7a13e894
RS
10171 if (f)
10172 note_mouse_movement (f, &event.xmotion);
10173 else
10174 {
e88b3c50 10175#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
10176 struct scroll_bar *bar
10177 = x_window_to_scroll_bar (event.xmotion.window);
f451eb13 10178
7a13e894
RS
10179 if (bar)
10180 x_scroll_bar_note_movement (bar, &event);
e88b3c50 10181#endif /* USE_TOOLKIT_SCROLL_BARS */
b8009dd1 10182
06a2c219
GM
10183 /* If we move outside the frame, then we're
10184 certainly no longer on any text in the frame. */
7a13e894
RS
10185 clear_mouse_face (dpyinfo);
10186 }
06a2c219
GM
10187
10188 /* If the contents of the global variable help_echo
10189 has changed, generate a HELP_EVENT. */
b7e80413
SM
10190 if (!NILP (help_echo)
10191 || !NILP (previous_help_echo))
06a2c219
GM
10192 {
10193 Lisp_Object frame;
be010514 10194 int n;
06a2c219
GM
10195
10196 if (f)
10197 XSETFRAME (frame, f);
10198 else
10199 frame = Qnil;
10200
10201 any_help_event_p = 1;
5ab2570d 10202 n = gen_help_event (bufp, numchars, help_echo, frame,
7cea38bc
GM
10203 help_echo_window, help_echo_object,
10204 help_echo_pos);
be010514 10205 bufp += n, count += n, numchars -= n;
06a2c219
GM
10206 }
10207
10208 goto OTHER;
dc6f92b8 10209 }
dc6f92b8 10210
7a13e894 10211 case ConfigureNotify:
9829ddba
RS
10212 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10213 if (f)
af395ec1 10214 {
5c187dee 10215#ifndef USE_X_TOOLKIT
bf1b7b30
KH
10216 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10217 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
5c187dee 10218
2d7fc7e8
RS
10219 /* In the toolkit version, change_frame_size
10220 is called by the code that handles resizing
10221 of the EmacsFrame widget. */
7a13e894 10222
7a13e894
RS
10223 /* Even if the number of character rows and columns has
10224 not changed, the font size may have changed, so we need
10225 to check the pixel dimensions as well. */
10226 if (columns != f->width
10227 || rows != f->height
7556890b
RS
10228 || event.xconfigure.width != f->output_data.x->pixel_width
10229 || event.xconfigure.height != f->output_data.x->pixel_height)
7a13e894 10230 {
7d1e984f 10231 change_frame_size (f, rows, columns, 0, 1, 0);
7a13e894 10232 SET_FRAME_GARBAGED (f);
e687d06e 10233 cancel_mouse_face (f);
7a13e894 10234 }
2d7fc7e8 10235#endif
af395ec1 10236
7556890b
RS
10237 f->output_data.x->pixel_width = event.xconfigure.width;
10238 f->output_data.x->pixel_height = event.xconfigure.height;
7a13e894
RS
10239
10240 /* What we have now is the position of Emacs's own window.
10241 Convert that to the position of the window manager window. */
dcb07ae9
RS
10242 x_real_positions (f, &f->output_data.x->left_pos,
10243 &f->output_data.x->top_pos);
10244
f5d11644
GM
10245#ifdef HAVE_X_I18N
10246 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10247 xic_set_statusarea (f);
10248#endif
10249
dcb07ae9
RS
10250 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10251 {
10252 /* Since the WM decorations come below top_pos now,
10253 we must put them below top_pos in the future. */
10254 f->output_data.x->win_gravity = NorthWestGravity;
10255 x_wm_set_size_hint (f, (long) 0, 0);
10256 }
8f08dc93
KH
10257#ifdef USE_MOTIF
10258 /* Some window managers pass (0,0) as the location of
10259 the window, and the Motif event handler stores it
10260 in the emacs widget, which messes up Motif menus. */
10261 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
10262 {
10263 event.xconfigure.x = f->output_data.x->widget->core.x;
10264 event.xconfigure.y = f->output_data.x->widget->core.y;
10265 }
06a2c219 10266#endif /* USE_MOTIF */
7a13e894 10267 }
2d7fc7e8 10268 goto OTHER;
dc6f92b8 10269
7a13e894
RS
10270 case ButtonPress:
10271 case ButtonRelease:
10272 {
10273 /* If we decide we want to generate an event to be seen
10274 by the rest of Emacs, we put it here. */
10275 struct input_event emacs_event;
9ea173e8 10276 int tool_bar_p = 0;
06a2c219 10277
7a13e894 10278 emacs_event.kind = no_event;
7a13e894 10279 bzero (&compose_status, sizeof (compose_status));
9b07615b 10280
06a2c219
GM
10281 if (dpyinfo->grabbed
10282 && last_mouse_frame
9f67f20b
RS
10283 && FRAME_LIVE_P (last_mouse_frame))
10284 f = last_mouse_frame;
10285 else
2224b905 10286 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9f67f20b 10287
06a2c219
GM
10288 if (f)
10289 {
9ea173e8
GM
10290 /* Is this in the tool-bar? */
10291 if (WINDOWP (f->tool_bar_window)
10292 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
06a2c219
GM
10293 {
10294 Lisp_Object window;
10295 int p, x, y;
10296
10297 x = event.xbutton.x;
10298 y = event.xbutton.y;
10299
10300 /* Set x and y. */
10301 window = window_from_coordinates (f, x, y, &p, 1);
9ea173e8 10302 if (EQ (window, f->tool_bar_window))
06a2c219 10303 {
9ea173e8
GM
10304 x_handle_tool_bar_click (f, &event.xbutton);
10305 tool_bar_p = 1;
06a2c219
GM
10306 }
10307 }
10308
9ea173e8 10309 if (!tool_bar_p)
06a2c219
GM
10310 if (!dpyinfo->x_focus_frame
10311 || f == dpyinfo->x_focus_frame)
10312 construct_mouse_click (&emacs_event, &event, f);
7a13e894
RS
10313 }
10314 else
10315 {
06a2c219 10316#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
10317 struct scroll_bar *bar
10318 = x_window_to_scroll_bar (event.xbutton.window);
f451eb13 10319
7a13e894
RS
10320 if (bar)
10321 x_scroll_bar_handle_click (bar, &event, &emacs_event);
06a2c219 10322#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
10323 }
10324
10325 if (event.type == ButtonPress)
10326 {
10327 dpyinfo->grabbed |= (1 << event.xbutton.button);
10328 last_mouse_frame = f;
edad46f6
KH
10329 /* Ignore any mouse motion that happened
10330 before this event; any subsequent mouse-movement
10331 Emacs events should reflect only motion after
10332 the ButtonPress. */
a00e91cd
KH
10333 if (f != 0)
10334 f->mouse_moved = 0;
06a2c219 10335
9ea173e8
GM
10336 if (!tool_bar_p)
10337 last_tool_bar_item = -1;
7a13e894 10338 }
3afe33e7
RS
10339 else
10340 {
7a13e894 10341 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
3afe33e7 10342 }
23faf38f 10343
7a13e894
RS
10344 if (numchars >= 1 && emacs_event.kind != no_event)
10345 {
10346 bcopy (&emacs_event, bufp, sizeof (struct input_event));
10347 bufp++;
10348 count++;
10349 numchars--;
10350 }
3afe33e7
RS
10351
10352#ifdef USE_X_TOOLKIT
2224b905
RS
10353 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
10354 /* For a down-event in the menu bar,
10355 don't pass it to Xt right now.
10356 Instead, save it away
10357 and we will pass it to Xt from kbd_buffer_get_event.
10358 That way, we can run some Lisp code first. */
91375f8f
RS
10359 if (f && event.type == ButtonPress
10360 /* Verify the event is really within the menu bar
10361 and not just sent to it due to grabbing. */
10362 && event.xbutton.x >= 0
10363 && event.xbutton.x < f->output_data.x->pixel_width
10364 && event.xbutton.y >= 0
10365 && event.xbutton.y < f->output_data.x->menubar_height
10366 && event.xbutton.same_screen)
2224b905 10367 {
8805890a 10368 SET_SAVED_BUTTON_EVENT;
2237cac9
RS
10369 XSETFRAME (last_mouse_press_frame, f);
10370 }
10371 else if (event.type == ButtonPress)
10372 {
10373 last_mouse_press_frame = Qnil;
30e671c3 10374 goto OTHER;
ce89ef46 10375 }
06a2c219 10376
2237cac9
RS
10377#ifdef USE_MOTIF /* This should do not harm for Lucid,
10378 but I am trying to be cautious. */
ce89ef46
RS
10379 else if (event.type == ButtonRelease)
10380 {
2237cac9 10381 if (!NILP (last_mouse_press_frame))
f10ded1c 10382 {
2237cac9
RS
10383 f = XFRAME (last_mouse_press_frame);
10384 if (f->output_data.x)
06a2c219 10385 SET_SAVED_BUTTON_EVENT;
f10ded1c 10386 }
06a2c219 10387 else
30e671c3 10388 goto OTHER;
2224b905 10389 }
2237cac9 10390#endif /* USE_MOTIF */
2224b905
RS
10391 else
10392 goto OTHER;
3afe33e7 10393#endif /* USE_X_TOOLKIT */
7a13e894
RS
10394 }
10395 break;
dc6f92b8 10396
7a13e894 10397 case CirculateNotify:
06a2c219
GM
10398 goto OTHER;
10399
7a13e894 10400 case CirculateRequest:
06a2c219
GM
10401 goto OTHER;
10402
10403 case VisibilityNotify:
10404 goto OTHER;
dc6f92b8 10405
7a13e894
RS
10406 case MappingNotify:
10407 /* Someone has changed the keyboard mapping - update the
10408 local cache. */
10409 switch (event.xmapping.request)
10410 {
10411 case MappingModifier:
10412 x_find_modifier_meanings (dpyinfo);
10413 /* This is meant to fall through. */
10414 case MappingKeyboard:
10415 XRefreshKeyboardMapping (&event.xmapping);
10416 }
7a13e894 10417 goto OTHER;
dc6f92b8 10418
7a13e894 10419 default:
7a13e894 10420 OTHER:
717ca130 10421#ifdef USE_X_TOOLKIT
7a13e894
RS
10422 BLOCK_INPUT;
10423 XtDispatchEvent (&event);
10424 UNBLOCK_INPUT;
3afe33e7 10425#endif /* USE_X_TOOLKIT */
7a13e894
RS
10426 break;
10427 }
dc6f92b8
JB
10428 }
10429 }
10430
06a2c219
GM
10431 out:;
10432
9a5196d0
RS
10433 /* On some systems, an X bug causes Emacs to get no more events
10434 when the window is destroyed. Detect that. (1994.) */
58769bee 10435 if (! event_found)
ef2a22d0 10436 {
ef2a22d0
RS
10437 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
10438 One XNOOP in 100 loops will make Emacs terminate.
10439 B. Bretthauer, 1994 */
10440 x_noop_count++;
58769bee 10441 if (x_noop_count >= 100)
ef2a22d0
RS
10442 {
10443 x_noop_count=0;
2224b905
RS
10444
10445 if (next_noop_dpyinfo == 0)
10446 next_noop_dpyinfo = x_display_list;
10447
10448 XNoOp (next_noop_dpyinfo->display);
10449
10450 /* Each time we get here, cycle through the displays now open. */
10451 next_noop_dpyinfo = next_noop_dpyinfo->next;
ef2a22d0
RS
10452 }
10453 }
502add23 10454
06a2c219 10455 /* If the focus was just given to an auto-raising frame,
0134a210 10456 raise it now. */
7a13e894 10457 /* ??? This ought to be able to handle more than one such frame. */
0134a210
RS
10458 if (pending_autoraise_frame)
10459 {
10460 x_raise_frame (pending_autoraise_frame);
10461 pending_autoraise_frame = 0;
10462 }
0134a210 10463
dc6f92b8 10464 UNBLOCK_INPUT;
bde5503b 10465 --handling_signal;
dc6f92b8
JB
10466 return count;
10467}
06a2c219
GM
10468
10469
10470
dc6f92b8 10471\f
06a2c219
GM
10472/***********************************************************************
10473 Text Cursor
10474 ***********************************************************************/
10475
10476/* Note if the text cursor of window W has been overwritten by a
10477 drawing operation that outputs N glyphs starting at HPOS in the
10478 line given by output_cursor.vpos. N < 0 means all the rest of the
10479 line after HPOS has been written. */
10480
10481static void
10482note_overwritten_text_cursor (w, hpos, n)
10483 struct window *w;
10484 int hpos, n;
10485{
10486 if (updated_area == TEXT_AREA
10487 && output_cursor.vpos == w->phys_cursor.vpos
10488 && hpos <= w->phys_cursor.hpos
10489 && (n < 0
10490 || hpos + n > w->phys_cursor.hpos))
10491 w->phys_cursor_on_p = 0;
10492}
f451eb13
JB
10493
10494
06a2c219
GM
10495/* Set clipping for output in glyph row ROW. W is the window in which
10496 we operate. GC is the graphics context to set clipping in.
10497 WHOLE_LINE_P non-zero means include the areas used for truncation
10498 mark display and alike in the clipping rectangle.
10499
10500 ROW may be a text row or, e.g., a mode line. Text rows must be
10501 clipped to the interior of the window dedicated to text display,
10502 mode lines must be clipped to the whole window. */
dc6f92b8
JB
10503
10504static void
06a2c219
GM
10505x_clip_to_row (w, row, gc, whole_line_p)
10506 struct window *w;
10507 struct glyph_row *row;
10508 GC gc;
10509 int whole_line_p;
dc6f92b8 10510{
06a2c219
GM
10511 struct frame *f = XFRAME (WINDOW_FRAME (w));
10512 XRectangle clip_rect;
10513 int window_x, window_y, window_width, window_height;
dc6f92b8 10514
06a2c219 10515 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5c1aae96 10516
06a2c219
GM
10517 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10518 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10519 clip_rect.y = max (clip_rect.y, window_y);
10520 clip_rect.width = window_width;
10521 clip_rect.height = row->visible_height;
5c1aae96 10522
06a2c219
GM
10523 /* If clipping to the whole line, including trunc marks, extend
10524 the rectangle to the left and increase its width. */
10525 if (whole_line_p)
10526 {
110859fc
GM
10527 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10528 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
06a2c219 10529 }
5c1aae96 10530
06a2c219 10531 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
dc6f92b8
JB
10532}
10533
06a2c219
GM
10534
10535/* Draw a hollow box cursor on window W in glyph row ROW. */
dc6f92b8
JB
10536
10537static void
06a2c219
GM
10538x_draw_hollow_cursor (w, row)
10539 struct window *w;
10540 struct glyph_row *row;
dc6f92b8 10541{
06a2c219
GM
10542 struct frame *f = XFRAME (WINDOW_FRAME (w));
10543 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10544 Display *dpy = FRAME_X_DISPLAY (f);
10545 int x, y, wd, h;
10546 XGCValues xgcv;
10547 struct glyph *cursor_glyph;
10548 GC gc;
10549
10550 /* Compute frame-relative coordinates from window-relative
10551 coordinates. */
10552 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10553 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10554 + row->ascent - w->phys_cursor_ascent);
10555 h = row->height - 1;
10556
10557 /* Get the glyph the cursor is on. If we can't tell because
10558 the current matrix is invalid or such, give up. */
10559 cursor_glyph = get_phys_cursor_glyph (w);
10560 if (cursor_glyph == NULL)
dc6f92b8
JB
10561 return;
10562
06a2c219
GM
10563 /* Compute the width of the rectangle to draw. If on a stretch
10564 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10565 rectangle as wide as the glyph, but use a canonical character
10566 width instead. */
10567 wd = cursor_glyph->pixel_width - 1;
10568 if (cursor_glyph->type == STRETCH_GLYPH
10569 && !x_stretch_cursor_p)
10570 wd = min (CANON_X_UNIT (f), wd);
10571
10572 /* The foreground of cursor_gc is typically the same as the normal
10573 background color, which can cause the cursor box to be invisible. */
10574 xgcv.foreground = f->output_data.x->cursor_pixel;
10575 if (dpyinfo->scratch_cursor_gc)
10576 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10577 else
10578 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10579 GCForeground, &xgcv);
10580 gc = dpyinfo->scratch_cursor_gc;
10581
10582 /* Set clipping, draw the rectangle, and reset clipping again. */
10583 x_clip_to_row (w, row, gc, 0);
10584 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10585 XSetClipMask (dpy, gc, None);
dc6f92b8
JB
10586}
10587
06a2c219
GM
10588
10589/* Draw a bar cursor on window W in glyph row ROW.
10590
10591 Implementation note: One would like to draw a bar cursor with an
10592 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10593 Unfortunately, I didn't find a font yet that has this property set.
10594 --gerd. */
dc6f92b8
JB
10595
10596static void
f02d8aa0 10597x_draw_bar_cursor (w, row, width)
06a2c219
GM
10598 struct window *w;
10599 struct glyph_row *row;
f02d8aa0 10600 int width;
dc6f92b8 10601{
92f424df
GM
10602 struct frame *f = XFRAME (w->frame);
10603 struct glyph *cursor_glyph;
10604 GC gc;
10605 int x;
10606 unsigned long mask;
10607 XGCValues xgcv;
10608 Display *dpy;
10609 Window window;
06a2c219 10610
92f424df
GM
10611 /* If cursor is out of bounds, don't draw garbage. This can happen
10612 in mini-buffer windows when switching between echo area glyphs
10613 and mini-buffer. */
10614 cursor_glyph = get_phys_cursor_glyph (w);
10615 if (cursor_glyph == NULL)
10616 return;
06a2c219 10617
92f424df
GM
10618 /* If on an image, draw like a normal cursor. That's usually better
10619 visible than drawing a bar, esp. if the image is large so that
10620 the bar might not be in the window. */
10621 if (cursor_glyph->type == IMAGE_GLYPH)
10622 {
10623 struct glyph_row *row;
10624 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
10625 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
10626 }
10627 else
10628 {
06a2c219
GM
10629 xgcv.background = f->output_data.x->cursor_pixel;
10630 xgcv.foreground = f->output_data.x->cursor_pixel;
10631 xgcv.graphics_exposures = 0;
10632 mask = GCForeground | GCBackground | GCGraphicsExposures;
10633 dpy = FRAME_X_DISPLAY (f);
10634 window = FRAME_X_WINDOW (f);
10635 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
92f424df 10636
06a2c219
GM
10637 if (gc)
10638 XChangeGC (dpy, gc, mask, &xgcv);
10639 else
10640 {
10641 gc = XCreateGC (dpy, window, mask, &xgcv);
10642 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10643 }
92f424df 10644
f02d8aa0
GM
10645 if (width < 0)
10646 width = f->output_data.x->cursor_width;
92f424df 10647
06a2c219
GM
10648 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10649 x_clip_to_row (w, row, gc, 0);
10650 XFillRectangle (dpy, window, gc,
10651 x,
10652 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
f02d8aa0 10653 min (cursor_glyph->pixel_width, width),
06a2c219
GM
10654 row->height);
10655 XSetClipMask (dpy, gc, None);
10656 }
dc6f92b8
JB
10657}
10658
06a2c219
GM
10659
10660/* Clear the cursor of window W to background color, and mark the
10661 cursor as not shown. This is used when the text where the cursor
10662 is is about to be rewritten. */
10663
dc6f92b8 10664static void
06a2c219
GM
10665x_clear_cursor (w)
10666 struct window *w;
dc6f92b8 10667{
06a2c219
GM
10668 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10669 x_update_window_cursor (w, 0);
10670}
90e65f07 10671
dbc4e1c1 10672
06a2c219
GM
10673/* Draw the cursor glyph of window W in glyph row ROW. See the
10674 comment of x_draw_glyphs for the meaning of HL. */
dbc4e1c1 10675
06a2c219
GM
10676static void
10677x_draw_phys_cursor_glyph (w, row, hl)
10678 struct window *w;
10679 struct glyph_row *row;
10680 enum draw_glyphs_face hl;
10681{
10682 /* If cursor hpos is out of bounds, don't draw garbage. This can
10683 happen in mini-buffer windows when switching between echo area
10684 glyphs and mini-buffer. */
10685 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
66ac4b0e
GM
10686 {
10687 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10688 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10689 hl, 0, 0, 0);
10690
10691 /* When we erase the cursor, and ROW is overlapped by other
10692 rows, make sure that these overlapping parts of other rows
10693 are redrawn. */
10694 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10695 {
10696 if (row > w->current_matrix->rows
10697 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10698 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10699
10700 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10701 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10702 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10703 }
10704 }
06a2c219 10705}
dbc4e1c1 10706
eea6af04 10707
06a2c219 10708/* Erase the image of a cursor of window W from the screen. */
eea6af04 10709
06a2c219
GM
10710static void
10711x_erase_phys_cursor (w)
10712 struct window *w;
10713{
10714 struct frame *f = XFRAME (w->frame);
10715 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10716 int hpos = w->phys_cursor.hpos;
10717 int vpos = w->phys_cursor.vpos;
10718 int mouse_face_here_p = 0;
10719 struct glyph_matrix *active_glyphs = w->current_matrix;
10720 struct glyph_row *cursor_row;
10721 struct glyph *cursor_glyph;
10722 enum draw_glyphs_face hl;
10723
10724 /* No cursor displayed or row invalidated => nothing to do on the
10725 screen. */
10726 if (w->phys_cursor_type == NO_CURSOR)
10727 goto mark_cursor_off;
10728
10729 /* VPOS >= active_glyphs->nrows means that window has been resized.
10730 Don't bother to erase the cursor. */
10731 if (vpos >= active_glyphs->nrows)
10732 goto mark_cursor_off;
10733
10734 /* If row containing cursor is marked invalid, there is nothing we
10735 can do. */
10736 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10737 if (!cursor_row->enabled_p)
10738 goto mark_cursor_off;
10739
10740 /* This can happen when the new row is shorter than the old one.
10741 In this case, either x_draw_glyphs or clear_end_of_line
10742 should have cleared the cursor. Note that we wouldn't be
10743 able to erase the cursor in this case because we don't have a
10744 cursor glyph at hand. */
10745 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10746 goto mark_cursor_off;
10747
10748 /* If the cursor is in the mouse face area, redisplay that when
10749 we clear the cursor. */
8801a864
KR
10750 if (! NILP (dpyinfo->mouse_face_window)
10751 && w == XWINDOW (dpyinfo->mouse_face_window)
06a2c219
GM
10752 && (vpos > dpyinfo->mouse_face_beg_row
10753 || (vpos == dpyinfo->mouse_face_beg_row
10754 && hpos >= dpyinfo->mouse_face_beg_col))
10755 && (vpos < dpyinfo->mouse_face_end_row
10756 || (vpos == dpyinfo->mouse_face_end_row
10757 && hpos < dpyinfo->mouse_face_end_col))
10758 /* Don't redraw the cursor's spot in mouse face if it is at the
10759 end of a line (on a newline). The cursor appears there, but
10760 mouse highlighting does not. */
10761 && cursor_row->used[TEXT_AREA] > hpos)
10762 mouse_face_here_p = 1;
10763
10764 /* Maybe clear the display under the cursor. */
10765 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10766 {
10767 int x;
045dee35 10768 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dbc4e1c1 10769
06a2c219
GM
10770 cursor_glyph = get_phys_cursor_glyph (w);
10771 if (cursor_glyph == NULL)
10772 goto mark_cursor_off;
dbc4e1c1 10773
06a2c219
GM
10774 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10775
10776 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10777 x,
045dee35 10778 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219
GM
10779 cursor_row->y)),
10780 cursor_glyph->pixel_width,
10781 cursor_row->visible_height,
10782 False);
dbc4e1c1 10783 }
06a2c219
GM
10784
10785 /* Erase the cursor by redrawing the character underneath it. */
10786 if (mouse_face_here_p)
10787 hl = DRAW_MOUSE_FACE;
10788 else if (cursor_row->inverse_p)
10789 hl = DRAW_INVERSE_VIDEO;
10790 else
10791 hl = DRAW_NORMAL_TEXT;
10792 x_draw_phys_cursor_glyph (w, cursor_row, hl);
dbc4e1c1 10793
06a2c219
GM
10794 mark_cursor_off:
10795 w->phys_cursor_on_p = 0;
10796 w->phys_cursor_type = NO_CURSOR;
dbc4e1c1
JB
10797}
10798
10799
06a2c219
GM
10800/* Display or clear cursor of window W. If ON is zero, clear the
10801 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10802 where to put the cursor is specified by HPOS, VPOS, X and Y. */
dbc4e1c1 10803
06a2c219
GM
10804void
10805x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10806 struct window *w;
10807 int on, hpos, vpos, x, y;
dbc4e1c1 10808{
06a2c219
GM
10809 struct frame *f = XFRAME (w->frame);
10810 int new_cursor_type;
f02d8aa0 10811 int new_cursor_width;
06a2c219
GM
10812 struct glyph_matrix *current_glyphs;
10813 struct glyph_row *glyph_row;
10814 struct glyph *glyph;
dbc4e1c1 10815
49d838ea 10816 /* This is pointless on invisible frames, and dangerous on garbaged
06a2c219
GM
10817 windows and frames; in the latter case, the frame or window may
10818 be in the midst of changing its size, and x and y may be off the
10819 window. */
10820 if (! FRAME_VISIBLE_P (f)
10821 || FRAME_GARBAGED_P (f)
10822 || vpos >= w->current_matrix->nrows
10823 || hpos >= w->current_matrix->matrix_w)
dc6f92b8
JB
10824 return;
10825
10826 /* If cursor is off and we want it off, return quickly. */
06a2c219 10827 if (!on && !w->phys_cursor_on_p)
dc6f92b8
JB
10828 return;
10829
06a2c219
GM
10830 current_glyphs = w->current_matrix;
10831 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10832 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10833
10834 /* If cursor row is not enabled, we don't really know where to
10835 display the cursor. */
10836 if (!glyph_row->enabled_p)
10837 {
10838 w->phys_cursor_on_p = 0;
10839 return;
10840 }
10841
10842 xassert (interrupt_input_blocked);
10843
10844 /* Set new_cursor_type to the cursor we want to be displayed. In a
10845 mini-buffer window, we want the cursor only to appear if we are
10846 reading input from this window. For the selected window, we want
10847 the cursor type given by the frame parameter. If explicitly
10848 marked off, draw no cursor. In all other cases, we want a hollow
10849 box cursor. */
f02d8aa0 10850 new_cursor_width = -1;
9b4a7047
GM
10851 if (cursor_in_echo_area
10852 && FRAME_HAS_MINIBUF_P (f)
10853 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
06a2c219 10854 {
9b4a7047
GM
10855 if (w == XWINDOW (echo_area_window))
10856 new_cursor_type = FRAME_DESIRED_CURSOR (f);
06a2c219
GM
10857 else
10858 new_cursor_type = HOLLOW_BOX_CURSOR;
10859 }
06a2c219 10860 else
9b4a7047
GM
10861 {
10862 if (w != XWINDOW (selected_window)
10863 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
10864 {
e55a0b79
GM
10865 extern int cursor_in_non_selected_windows;
10866
5cefa566
GM
10867 if (MINI_WINDOW_P (w)
10868 || !cursor_in_non_selected_windows
10869 || NILP (XBUFFER (w->buffer)->cursor_type))
9b4a7047
GM
10870 new_cursor_type = NO_CURSOR;
10871 else
10872 new_cursor_type = HOLLOW_BOX_CURSOR;
10873 }
10874 else if (w->cursor_off_p)
10875 new_cursor_type = NO_CURSOR;
10876 else
f02d8aa0
GM
10877 {
10878 struct buffer *b = XBUFFER (w->buffer);
10879
10880 if (EQ (b->cursor_type, Qt))
10881 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10882 else
10883 new_cursor_type = x_specified_cursor_type (b->cursor_type,
10884 &new_cursor_width);
10885 }
9b4a7047 10886 }
06a2c219
GM
10887
10888 /* If cursor is currently being shown and we don't want it to be or
10889 it is in the wrong place, or the cursor type is not what we want,
dc6f92b8 10890 erase it. */
06a2c219 10891 if (w->phys_cursor_on_p
dc6f92b8 10892 && (!on
06a2c219
GM
10893 || w->phys_cursor.x != x
10894 || w->phys_cursor.y != y
10895 || new_cursor_type != w->phys_cursor_type))
10896 x_erase_phys_cursor (w);
10897
10898 /* If the cursor is now invisible and we want it to be visible,
10899 display it. */
10900 if (on && !w->phys_cursor_on_p)
10901 {
10902 w->phys_cursor_ascent = glyph_row->ascent;
10903 w->phys_cursor_height = glyph_row->height;
10904
10905 /* Set phys_cursor_.* before x_draw_.* is called because some
10906 of them may need the information. */
10907 w->phys_cursor.x = x;
10908 w->phys_cursor.y = glyph_row->y;
10909 w->phys_cursor.hpos = hpos;
10910 w->phys_cursor.vpos = vpos;
10911 w->phys_cursor_type = new_cursor_type;
10912 w->phys_cursor_on_p = 1;
10913
10914 switch (new_cursor_type)
dc6f92b8 10915 {
06a2c219
GM
10916 case HOLLOW_BOX_CURSOR:
10917 x_draw_hollow_cursor (w, glyph_row);
10918 break;
10919
10920 case FILLED_BOX_CURSOR:
10921 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10922 break;
10923
10924 case BAR_CURSOR:
f02d8aa0 10925 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
06a2c219
GM
10926 break;
10927
10928 case NO_CURSOR:
10929 break;
dc6f92b8 10930
06a2c219
GM
10931 default:
10932 abort ();
10933 }
59ddecde
GM
10934
10935#ifdef HAVE_X_I18N
10936 if (w == XWINDOW (f->selected_window))
10937 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
10938 xic_set_preeditarea (w, x, y);
10939#endif
dc6f92b8
JB
10940 }
10941
06a2c219 10942#ifndef XFlush
f676886a 10943 if (updating_frame != f)
334208b7 10944 XFlush (FRAME_X_DISPLAY (f));
06a2c219 10945#endif
dc6f92b8
JB
10946}
10947
06a2c219
GM
10948
10949/* Display the cursor on window W, or clear it. X and Y are window
10950 relative pixel coordinates. HPOS and VPOS are glyph matrix
10951 positions. If W is not the selected window, display a hollow
10952 cursor. ON non-zero means display the cursor at X, Y which
10953 correspond to HPOS, VPOS, otherwise it is cleared. */
5d46f928 10954
dfcf069d 10955void
06a2c219
GM
10956x_display_cursor (w, on, hpos, vpos, x, y)
10957 struct window *w;
10958 int on, hpos, vpos, x, y;
dc6f92b8 10959{
f94397b5 10960 BLOCK_INPUT;
06a2c219 10961 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
5d46f928
RS
10962 UNBLOCK_INPUT;
10963}
10964
06a2c219
GM
10965
10966/* Display the cursor on window W, or clear it, according to ON_P.
5d46f928
RS
10967 Don't change the cursor's position. */
10968
dfcf069d 10969void
06a2c219 10970x_update_cursor (f, on_p)
5d46f928 10971 struct frame *f;
5d46f928 10972{
06a2c219
GM
10973 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
10974}
10975
10976
10977/* Call x_update_window_cursor with parameter ON_P on all leaf windows
10978 in the window tree rooted at W. */
10979
10980static void
10981x_update_cursor_in_window_tree (w, on_p)
10982 struct window *w;
10983 int on_p;
10984{
10985 while (w)
10986 {
10987 if (!NILP (w->hchild))
10988 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10989 else if (!NILP (w->vchild))
10990 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10991 else
10992 x_update_window_cursor (w, on_p);
10993
10994 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10995 }
10996}
5d46f928 10997
f94397b5 10998
06a2c219
GM
10999/* Switch the display of W's cursor on or off, according to the value
11000 of ON. */
11001
11002static void
11003x_update_window_cursor (w, on)
11004 struct window *w;
11005 int on;
11006{
16b5d424
GM
11007 /* Don't update cursor in windows whose frame is in the process
11008 of being deleted. */
11009 if (w->current_matrix)
11010 {
11011 BLOCK_INPUT;
11012 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11013 w->phys_cursor.x, w->phys_cursor.y);
11014 UNBLOCK_INPUT;
11015 }
dc6f92b8 11016}
06a2c219
GM
11017
11018
11019
dc6f92b8
JB
11020\f
11021/* Icons. */
11022
f676886a 11023/* Refresh bitmap kitchen sink icon for frame F
06a2c219 11024 when we get an expose event for it. */
dc6f92b8 11025
dfcf069d 11026void
f676886a
JB
11027refreshicon (f)
11028 struct frame *f;
dc6f92b8 11029{
06a2c219 11030 /* Normally, the window manager handles this function. */
dc6f92b8
JB
11031}
11032
dbc4e1c1 11033/* Make the x-window of frame F use the gnu icon bitmap. */
dc6f92b8
JB
11034
11035int
990ba854 11036x_bitmap_icon (f, file)
f676886a 11037 struct frame *f;
990ba854 11038 Lisp_Object file;
dc6f92b8 11039{
06a2c219 11040 int bitmap_id;
dc6f92b8 11041
c118dd06 11042 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
11043 return 1;
11044
990ba854 11045 /* Free up our existing icon bitmap if any. */
7556890b
RS
11046 if (f->output_data.x->icon_bitmap > 0)
11047 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11048 f->output_data.x->icon_bitmap = 0;
990ba854
RS
11049
11050 if (STRINGP (file))
7f2ae036
RS
11051 bitmap_id = x_create_bitmap_from_file (f, file);
11052 else
11053 {
990ba854 11054 /* Create the GNU bitmap if necessary. */
5bf01b68 11055 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
334208b7
RS
11056 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11057 = x_create_bitmap_from_data (f, gnu_bits,
11058 gnu_width, gnu_height);
990ba854
RS
11059
11060 /* The first time we create the GNU bitmap,
06a2c219 11061 this increments the ref-count one extra time.
990ba854
RS
11062 As a result, the GNU bitmap is never freed.
11063 That way, we don't have to worry about allocating it again. */
334208b7 11064 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
990ba854 11065
334208b7 11066 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7f2ae036
RS
11067 }
11068
11069 x_wm_set_icon_pixmap (f, bitmap_id);
7556890b 11070 f->output_data.x->icon_bitmap = bitmap_id;
dc6f92b8
JB
11071
11072 return 0;
11073}
11074
11075
1be2d067
KH
11076/* Make the x-window of frame F use a rectangle with text.
11077 Use ICON_NAME as the text. */
dc6f92b8
JB
11078
11079int
f676886a
JB
11080x_text_icon (f, icon_name)
11081 struct frame *f;
dc6f92b8
JB
11082 char *icon_name;
11083{
c118dd06 11084 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
11085 return 1;
11086
1be2d067
KH
11087#ifdef HAVE_X11R4
11088 {
11089 XTextProperty text;
11090 text.value = (unsigned char *) icon_name;
11091 text.encoding = XA_STRING;
11092 text.format = 8;
11093 text.nitems = strlen (icon_name);
11094#ifdef USE_X_TOOLKIT
7556890b 11095 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
1be2d067
KH
11096 &text);
11097#else /* not USE_X_TOOLKIT */
11098 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11099#endif /* not USE_X_TOOLKIT */
11100 }
11101#else /* not HAVE_X11R4 */
11102 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11103#endif /* not HAVE_X11R4 */
58769bee 11104
7556890b
RS
11105 if (f->output_data.x->icon_bitmap > 0)
11106 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11107 f->output_data.x->icon_bitmap = 0;
b1c884c3 11108 x_wm_set_icon_pixmap (f, 0);
dc6f92b8
JB
11109
11110 return 0;
11111}
11112\f
e99db5a1
RS
11113#define X_ERROR_MESSAGE_SIZE 200
11114
11115/* If non-nil, this should be a string.
11116 It means catch X errors and store the error message in this string. */
11117
11118static Lisp_Object x_error_message_string;
11119
11120/* An X error handler which stores the error message in
11121 x_error_message_string. This is called from x_error_handler if
11122 x_catch_errors is in effect. */
11123
06a2c219 11124static void
e99db5a1
RS
11125x_error_catcher (display, error)
11126 Display *display;
11127 XErrorEvent *error;
11128{
11129 XGetErrorText (display, error->error_code,
11130 XSTRING (x_error_message_string)->data,
11131 X_ERROR_MESSAGE_SIZE);
11132}
11133
11134/* Begin trapping X errors for display DPY. Actually we trap X errors
11135 for all displays, but DPY should be the display you are actually
11136 operating on.
11137
11138 After calling this function, X protocol errors no longer cause
11139 Emacs to exit; instead, they are recorded in the string
11140 stored in x_error_message_string.
11141
11142 Calling x_check_errors signals an Emacs error if an X error has
11143 occurred since the last call to x_catch_errors or x_check_errors.
11144
11145 Calling x_uncatch_errors resumes the normal error handling. */
11146
11147void x_check_errors ();
11148static Lisp_Object x_catch_errors_unwind ();
11149
11150int
11151x_catch_errors (dpy)
11152 Display *dpy;
11153{
11154 int count = specpdl_ptr - specpdl;
11155
11156 /* Make sure any errors from previous requests have been dealt with. */
11157 XSync (dpy, False);
11158
11159 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
11160
11161 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
11162 XSTRING (x_error_message_string)->data[0] = 0;
11163
11164 return count;
11165}
11166
11167/* Unbind the binding that we made to check for X errors. */
11168
11169static Lisp_Object
11170x_catch_errors_unwind (old_val)
11171 Lisp_Object old_val;
11172{
11173 x_error_message_string = old_val;
11174 return Qnil;
11175}
11176
11177/* If any X protocol errors have arrived since the last call to
11178 x_catch_errors or x_check_errors, signal an Emacs error using
11179 sprintf (a buffer, FORMAT, the x error message text) as the text. */
11180
11181void
11182x_check_errors (dpy, format)
11183 Display *dpy;
11184 char *format;
11185{
11186 /* Make sure to catch any errors incurred so far. */
11187 XSync (dpy, False);
11188
11189 if (XSTRING (x_error_message_string)->data[0])
11190 error (format, XSTRING (x_error_message_string)->data);
11191}
11192
9829ddba
RS
11193/* Nonzero if we had any X protocol errors
11194 since we did x_catch_errors on DPY. */
e99db5a1
RS
11195
11196int
11197x_had_errors_p (dpy)
11198 Display *dpy;
11199{
11200 /* Make sure to catch any errors incurred so far. */
11201 XSync (dpy, False);
11202
11203 return XSTRING (x_error_message_string)->data[0] != 0;
11204}
11205
9829ddba
RS
11206/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
11207
06a2c219 11208void
9829ddba
RS
11209x_clear_errors (dpy)
11210 Display *dpy;
11211{
11212 XSTRING (x_error_message_string)->data[0] = 0;
11213}
11214
e99db5a1
RS
11215/* Stop catching X protocol errors and let them make Emacs die.
11216 DPY should be the display that was passed to x_catch_errors.
11217 COUNT should be the value that was returned by
11218 the corresponding call to x_catch_errors. */
11219
11220void
11221x_uncatch_errors (dpy, count)
11222 Display *dpy;
11223 int count;
11224{
11225 unbind_to (count, Qnil);
11226}
11227
11228#if 0
11229static unsigned int x_wire_count;
11230x_trace_wire ()
11231{
11232 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
11233}
11234#endif /* ! 0 */
11235
11236\f
11237/* Handle SIGPIPE, which can happen when the connection to a server
11238 simply goes away. SIGPIPE is handled by x_connection_signal.
11239 Don't need to do anything, because the write which caused the
11240 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
06a2c219 11241 which will do the appropriate cleanup for us. */
e99db5a1
RS
11242
11243static SIGTYPE
11244x_connection_signal (signalnum) /* If we don't have an argument, */
06a2c219 11245 int signalnum; /* some compilers complain in signal calls. */
e99db5a1
RS
11246{
11247#ifdef USG
11248 /* USG systems forget handlers when they are used;
11249 must reestablish each time */
11250 signal (signalnum, x_connection_signal);
11251#endif /* USG */
11252}
11253\f
4746118a
JB
11254/* Handling X errors. */
11255
7a13e894 11256/* Handle the loss of connection to display DISPLAY. */
16bd92ea 11257
4746118a 11258static SIGTYPE
7a13e894
RS
11259x_connection_closed (display, error_message)
11260 Display *display;
11261 char *error_message;
4746118a 11262{
7a13e894
RS
11263 struct x_display_info *dpyinfo = x_display_info_for_display (display);
11264 Lisp_Object frame, tail;
11265
6186a4a0
RS
11266 /* Indicate that this display is dead. */
11267
2e465cdd 11268#if 0 /* Closing the display caused a bus error on OpenWindows. */
f613a4c8 11269#ifdef USE_X_TOOLKIT
adabc3a9 11270 XtCloseDisplay (display);
2e465cdd 11271#endif
f613a4c8 11272#endif
adabc3a9 11273
9e80b57d
KR
11274 if (dpyinfo)
11275 dpyinfo->display = 0;
6186a4a0 11276
06a2c219 11277 /* First delete frames whose mini-buffers are on frames
7a13e894
RS
11278 that are on the dead display. */
11279 FOR_EACH_FRAME (tail, frame)
11280 {
11281 Lisp_Object minibuf_frame;
11282 minibuf_frame
11283 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
f48f33ca
RS
11284 if (FRAME_X_P (XFRAME (frame))
11285 && FRAME_X_P (XFRAME (minibuf_frame))
11286 && ! EQ (frame, minibuf_frame)
11287 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7a13e894
RS
11288 Fdelete_frame (frame, Qt);
11289 }
11290
11291 /* Now delete all remaining frames on the dead display.
06a2c219 11292 We are now sure none of these is used as the mini-buffer
7a13e894
RS
11293 for another frame that we need to delete. */
11294 FOR_EACH_FRAME (tail, frame)
f48f33ca
RS
11295 if (FRAME_X_P (XFRAME (frame))
11296 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
07a7096a
KH
11297 {
11298 /* Set this to t so that Fdelete_frame won't get confused
11299 trying to find a replacement. */
11300 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
11301 Fdelete_frame (frame, Qt);
11302 }
7a13e894 11303
482a1bd2
KH
11304 if (dpyinfo)
11305 x_delete_display (dpyinfo);
7a13e894
RS
11306
11307 if (x_display_list == 0)
11308 {
f8d07b62 11309 fprintf (stderr, "%s\n", error_message);
7a13e894
RS
11310 shut_down_emacs (0, 0, Qnil);
11311 exit (70);
11312 }
12ba150f 11313
7a13e894
RS
11314 /* Ordinary stack unwind doesn't deal with these. */
11315#ifdef SIGIO
11316 sigunblock (sigmask (SIGIO));
11317#endif
11318 sigunblock (sigmask (SIGALRM));
11319 TOTALLY_UNBLOCK_INPUT;
11320
aa4d9a9e 11321 clear_waiting_for_input ();
182334bd 11322 handling_signal = 0;
7a13e894 11323 error ("%s", error_message);
4746118a
JB
11324}
11325
7a13e894
RS
11326/* This is the usual handler for X protocol errors.
11327 It kills all frames on the display that we got the error for.
11328 If that was the only one, it prints an error message and kills Emacs. */
11329
06a2c219 11330static void
c118dd06
JB
11331x_error_quitter (display, error)
11332 Display *display;
11333 XErrorEvent *error;
11334{
7a13e894 11335 char buf[256], buf1[356];
dc6f92b8 11336
58769bee 11337 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 11338 original error handler. */
dc6f92b8 11339
c118dd06 11340 XGetErrorText (display, error->error_code, buf, sizeof (buf));
0a0fdc70 11341 sprintf (buf1, "X protocol error: %s on protocol request %d",
c118dd06 11342 buf, error->request_code);
7a13e894 11343 x_connection_closed (display, buf1);
dc6f92b8
JB
11344}
11345
e99db5a1
RS
11346/* This is the first-level handler for X protocol errors.
11347 It calls x_error_quitter or x_error_catcher. */
7a13e894 11348
8922af5f 11349static int
e99db5a1 11350x_error_handler (display, error)
8922af5f 11351 Display *display;
e99db5a1 11352 XErrorEvent *error;
8922af5f 11353{
e99db5a1
RS
11354 if (! NILP (x_error_message_string))
11355 x_error_catcher (display, error);
11356 else
11357 x_error_quitter (display, error);
06a2c219 11358 return 0;
f9e24cb9 11359}
c118dd06 11360
e99db5a1
RS
11361/* This is the handler for X IO errors, always.
11362 It kills all frames on the display that we lost touch with.
11363 If that was the only one, it prints an error message and kills Emacs. */
7a13e894 11364
c118dd06 11365static int
e99db5a1 11366x_io_error_quitter (display)
c118dd06 11367 Display *display;
c118dd06 11368{
e99db5a1 11369 char buf[256];
dc6f92b8 11370
e99db5a1
RS
11371 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
11372 x_connection_closed (display, buf);
06a2c219 11373 return 0;
dc6f92b8 11374}
dc6f92b8 11375\f
f451eb13
JB
11376/* Changing the font of the frame. */
11377
76bcdf39
RS
11378/* Give frame F the font named FONTNAME as its default font, and
11379 return the full name of that font. FONTNAME may be a wildcard
11380 pattern; in that case, we choose some font that fits the pattern.
11381 The return value shows which font we chose. */
11382
b5cf7a0e 11383Lisp_Object
f676886a
JB
11384x_new_font (f, fontname)
11385 struct frame *f;
dc6f92b8
JB
11386 register char *fontname;
11387{
dc43ef94 11388 struct font_info *fontp
ee569018 11389 = FS_LOAD_FONT (f, 0, fontname, -1);
dc6f92b8 11390
dc43ef94
KH
11391 if (!fontp)
11392 return Qnil;
2224a5fc 11393
dc43ef94 11394 f->output_data.x->font = (XFontStruct *) (fontp->font);
b4192550 11395 f->output_data.x->baseline_offset = fontp->baseline_offset;
dc43ef94
KH
11396 f->output_data.x->fontset = -1;
11397
b2cad826
KH
11398 /* Compute the scroll bar width in character columns. */
11399 if (f->scroll_bar_pixel_width > 0)
11400 {
7556890b 11401 int wid = FONT_WIDTH (f->output_data.x->font);
b2cad826
KH
11402 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
11403 }
11404 else
4e61bddf
RS
11405 {
11406 int wid = FONT_WIDTH (f->output_data.x->font);
11407 f->scroll_bar_cols = (14 + wid - 1) / wid;
11408 }
b2cad826 11409
f676886a 11410 /* Now make the frame display the given font. */
c118dd06 11411 if (FRAME_X_WINDOW (f) != 0)
dc6f92b8 11412 {
7556890b
RS
11413 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
11414 f->output_data.x->font->fid);
11415 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
11416 f->output_data.x->font->fid);
11417 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
11418 f->output_data.x->font->fid);
f676886a 11419
a27f9f86 11420 frame_update_line_height (f);
0134a210 11421 x_set_window_size (f, 0, f->width, f->height);
dc6f92b8 11422 }
a27f9f86
RS
11423 else
11424 /* If we are setting a new frame's font for the first time,
11425 there are no faces yet, so this font's height is the line height. */
7556890b 11426 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
dc6f92b8 11427
dc43ef94
KH
11428 return build_string (fontp->full_name);
11429}
11430
11431/* Give frame F the fontset named FONTSETNAME as its default font, and
11432 return the full name of that fontset. FONTSETNAME may be a wildcard
b5210ea7
KH
11433 pattern; in that case, we choose some fontset that fits the pattern.
11434 The return value shows which fontset we chose. */
b5cf7a0e 11435
dc43ef94
KH
11436Lisp_Object
11437x_new_fontset (f, fontsetname)
11438 struct frame *f;
11439 char *fontsetname;
11440{
ee569018 11441 int fontset = fs_query_fontset (build_string (fontsetname), 0);
dc43ef94 11442 Lisp_Object result;
b5cf7a0e 11443
dc43ef94
KH
11444 if (fontset < 0)
11445 return Qnil;
b5cf7a0e 11446
2da424f1
KH
11447 if (f->output_data.x->fontset == fontset)
11448 /* This fontset is already set in frame F. There's nothing more
11449 to do. */
ee569018 11450 return fontset_name (fontset);
dc43ef94 11451
ee569018 11452 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
dc43ef94
KH
11453
11454 if (!STRINGP (result))
11455 /* Can't load ASCII font. */
11456 return Qnil;
11457
11458 /* Since x_new_font doesn't update any fontset information, do it now. */
11459 f->output_data.x->fontset = fontset;
dc43ef94 11460
f5d11644
GM
11461#ifdef HAVE_X_I18N
11462 if (FRAME_XIC (f)
11463 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
ee569018 11464 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
f5d11644
GM
11465#endif
11466
dc43ef94 11467 return build_string (fontsetname);
dc6f92b8 11468}
f5d11644
GM
11469
11470\f
11471/***********************************************************************
11472 X Input Methods
11473 ***********************************************************************/
11474
11475#ifdef HAVE_X_I18N
11476
11477#ifdef HAVE_X11R6
11478
11479/* XIM destroy callback function, which is called whenever the
11480 connection to input method XIM dies. CLIENT_DATA contains a
11481 pointer to the x_display_info structure corresponding to XIM. */
11482
11483static void
11484xim_destroy_callback (xim, client_data, call_data)
11485 XIM xim;
11486 XPointer client_data;
11487 XPointer call_data;
11488{
11489 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
11490 Lisp_Object frame, tail;
11491
11492 BLOCK_INPUT;
11493
11494 /* No need to call XDestroyIC.. */
11495 FOR_EACH_FRAME (tail, frame)
11496 {
11497 struct frame *f = XFRAME (frame);
11498 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
11499 {
11500 FRAME_XIC (f) = NULL;
11501 if (FRAME_XIC_FONTSET (f))
11502 {
11503 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
11504 FRAME_XIC_FONTSET (f) = NULL;
11505 }
11506 }
11507 }
11508
11509 /* No need to call XCloseIM. */
11510 dpyinfo->xim = NULL;
11511 XFree (dpyinfo->xim_styles);
11512 UNBLOCK_INPUT;
11513}
11514
11515#endif /* HAVE_X11R6 */
11516
11517/* Open the connection to the XIM server on display DPYINFO.
11518 RESOURCE_NAME is the resource name Emacs uses. */
11519
11520static void
11521xim_open_dpy (dpyinfo, resource_name)
11522 struct x_display_info *dpyinfo;
11523 char *resource_name;
11524{
287f7dd6 11525#ifdef USE_XIM
f5d11644
GM
11526 XIM xim;
11527
11528 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
11529 dpyinfo->xim = xim;
11530
11531 if (xim)
11532 {
f5d11644
GM
11533#ifdef HAVE_X11R6
11534 XIMCallback destroy;
11535#endif
11536
11537 /* Get supported styles and XIM values. */
11538 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
11539
11540#ifdef HAVE_X11R6
11541 destroy.callback = xim_destroy_callback;
11542 destroy.client_data = (XPointer)dpyinfo;
68642df6 11543 /* This isn't prptotyped in OSF 5.0. */
f5d11644
GM
11544 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
11545#endif
11546 }
287f7dd6
GM
11547
11548#else /* not USE_XIM */
11549 dpyinfo->xim = NULL;
11550#endif /* not USE_XIM */
f5d11644
GM
11551}
11552
11553
b9de836c 11554#ifdef HAVE_X11R6_XIM
f5d11644
GM
11555
11556struct xim_inst_t
11557{
11558 struct x_display_info *dpyinfo;
11559 char *resource_name;
11560};
11561
11562/* XIM instantiate callback function, which is called whenever an XIM
11563 server is available. DISPLAY is teh display of the XIM.
11564 CLIENT_DATA contains a pointer to an xim_inst_t structure created
11565 when the callback was registered. */
11566
11567static void
11568xim_instantiate_callback (display, client_data, call_data)
11569 Display *display;
11570 XPointer client_data;
11571 XPointer call_data;
11572{
11573 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
11574 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
11575
11576 /* We don't support multiple XIM connections. */
11577 if (dpyinfo->xim)
11578 return;
11579
11580 xim_open_dpy (dpyinfo, xim_inst->resource_name);
11581
11582 /* Create XIC for the existing frames on the same display, as long
11583 as they have no XIC. */
11584 if (dpyinfo->xim && dpyinfo->reference_count > 0)
11585 {
11586 Lisp_Object tail, frame;
11587
11588 BLOCK_INPUT;
11589 FOR_EACH_FRAME (tail, frame)
11590 {
11591 struct frame *f = XFRAME (frame);
11592
11593 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
11594 if (FRAME_XIC (f) == NULL)
11595 {
11596 create_frame_xic (f);
11597 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
11598 xic_set_statusarea (f);
11599 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
11600 {
11601 struct window *w = XWINDOW (f->selected_window);
11602 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
11603 }
11604 }
11605 }
11606
11607 UNBLOCK_INPUT;
11608 }
11609}
11610
b9de836c 11611#endif /* HAVE_X11R6_XIM */
f5d11644
GM
11612
11613
11614/* Open a connection to the XIM server on display DPYINFO.
11615 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
11616 connection only at the first time. On X11R6, open the connection
11617 in the XIM instantiate callback function. */
11618
11619static void
11620xim_initialize (dpyinfo, resource_name)
11621 struct x_display_info *dpyinfo;
11622 char *resource_name;
11623{
287f7dd6 11624#ifdef USE_XIM
b9de836c 11625#ifdef HAVE_X11R6_XIM
f5d11644
GM
11626 struct xim_inst_t *xim_inst;
11627 int len;
11628
11629 dpyinfo->xim = NULL;
11630 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
11631 xim_inst->dpyinfo = dpyinfo;
11632 len = strlen (resource_name);
11633 xim_inst->resource_name = (char *) xmalloc (len + 1);
11634 bcopy (resource_name, xim_inst->resource_name, len + 1);
11635 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11636 resource_name, EMACS_CLASS,
11637 xim_instantiate_callback,
2ebb2f8b
DL
11638 /* Fixme: This is XPointer in
11639 XFree86 but (XPointer *) on
11640 Tru64, at least. */
11641 (XPointer) xim_inst);
b9de836c 11642#else /* not HAVE_X11R6_XIM */
f5d11644
GM
11643 dpyinfo->xim = NULL;
11644 xim_open_dpy (dpyinfo, resource_name);
b9de836c 11645#endif /* not HAVE_X11R6_XIM */
287f7dd6
GM
11646
11647#else /* not USE_XIM */
11648 dpyinfo->xim = NULL;
11649#endif /* not USE_XIM */
f5d11644
GM
11650}
11651
11652
11653/* Close the connection to the XIM server on display DPYINFO. */
11654
11655static void
11656xim_close_dpy (dpyinfo)
11657 struct x_display_info *dpyinfo;
11658{
287f7dd6 11659#ifdef USE_XIM
b9de836c 11660#ifdef HAVE_X11R6_XIM
f5d11644
GM
11661 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11662 NULL, EMACS_CLASS,
11663 xim_instantiate_callback, NULL);
b9de836c 11664#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
11665 XCloseIM (dpyinfo->xim);
11666 dpyinfo->xim = NULL;
11667 XFree (dpyinfo->xim_styles);
287f7dd6 11668#endif /* USE_XIM */
f5d11644
GM
11669}
11670
b9de836c 11671#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
11672
11673
dc6f92b8 11674\f
2e365682
RS
11675/* Calculate the absolute position in frame F
11676 from its current recorded position values and gravity. */
11677
dfcf069d 11678void
43bca5d5 11679x_calc_absolute_position (f)
f676886a 11680 struct frame *f;
dc6f92b8 11681{
06a2c219 11682 Window child;
6dba1858 11683 int win_x = 0, win_y = 0;
7556890b 11684 int flags = f->output_data.x->size_hint_flags;
c81412a0
KH
11685 int this_window;
11686
9829ddba
RS
11687 /* We have nothing to do if the current position
11688 is already for the top-left corner. */
11689 if (! ((flags & XNegative) || (flags & YNegative)))
11690 return;
11691
c81412a0 11692#ifdef USE_X_TOOLKIT
7556890b 11693 this_window = XtWindow (f->output_data.x->widget);
c81412a0
KH
11694#else
11695 this_window = FRAME_X_WINDOW (f);
11696#endif
6dba1858
RS
11697
11698 /* Find the position of the outside upper-left corner of
9829ddba
RS
11699 the inner window, with respect to the outer window.
11700 But do this only if we will need the results. */
7556890b 11701 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6dba1858 11702 {
9829ddba
RS
11703 int count;
11704
6dba1858 11705 BLOCK_INPUT;
9829ddba
RS
11706 count = x_catch_errors (FRAME_X_DISPLAY (f));
11707 while (1)
11708 {
11709 x_clear_errors (FRAME_X_DISPLAY (f));
11710 XTranslateCoordinates (FRAME_X_DISPLAY (f),
11711
11712 /* From-window, to-window. */
11713 this_window,
11714 f->output_data.x->parent_desc,
11715
11716 /* From-position, to-position. */
11717 0, 0, &win_x, &win_y,
11718
11719 /* Child of win. */
11720 &child);
11721 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
11722 {
11723 Window newroot, newparent = 0xdeadbeef;
11724 Window *newchildren;
2ebb2f8b 11725 unsigned int nchildren;
9829ddba
RS
11726
11727 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
11728 &newparent, &newchildren, &nchildren))
11729 break;
58769bee 11730
7c3c78a3 11731 XFree ((char *) newchildren);
6dba1858 11732
9829ddba
RS
11733 f->output_data.x->parent_desc = newparent;
11734 }
11735 else
11736 break;
11737 }
6dba1858 11738
9829ddba 11739 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
6dba1858
RS
11740 UNBLOCK_INPUT;
11741 }
11742
11743 /* Treat negative positions as relative to the leftmost bottommost
11744 position that fits on the screen. */
20f55f9a 11745 if (flags & XNegative)
7556890b 11746 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
2e365682
RS
11747 - 2 * f->output_data.x->border_width - win_x
11748 - PIXEL_WIDTH (f)
11749 + f->output_data.x->left_pos);
dc6f92b8 11750
7708ced0
GM
11751 {
11752 int height = PIXEL_HEIGHT (f);
06a2c219 11753
7708ced0
GM
11754#if defined USE_X_TOOLKIT && defined USE_MOTIF
11755 /* Something is fishy here. When using Motif, starting Emacs with
11756 `-g -0-0', the frame appears too low by a few pixels.
11757
11758 This seems to be so because initially, while Emacs is starting,
11759 the column widget's height and the frame's pixel height are
11760 different. The column widget's height is the right one. In
11761 later invocations, when Emacs is up, the frame's pixel height
11762 is right, though.
11763
11764 It's not obvious where the initial small difference comes from.
11765 2000-12-01, gerd. */
11766
11767 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
06a2c219 11768#endif
2e365682 11769
7708ced0
GM
11770 if (flags & YNegative)
11771 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
11772 - 2 * f->output_data.x->border_width
11773 - win_y
11774 - height
11775 + f->output_data.x->top_pos);
11776 }
11777
3a35ab44
RS
11778 /* The left_pos and top_pos
11779 are now relative to the top and left screen edges,
11780 so the flags should correspond. */
7556890b 11781 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
dc6f92b8
JB
11782}
11783
3a35ab44
RS
11784/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
11785 to really change the position, and 0 when calling from
11786 x_make_frame_visible (in that case, XOFF and YOFF are the current
aa3ff7c9
KH
11787 position values). It is -1 when calling from x_set_frame_parameters,
11788 which means, do adjust for borders but don't change the gravity. */
3a35ab44 11789
dfcf069d 11790void
dc05a16b 11791x_set_offset (f, xoff, yoff, change_gravity)
f676886a 11792 struct frame *f;
dc6f92b8 11793 register int xoff, yoff;
dc05a16b 11794 int change_gravity;
dc6f92b8 11795{
4a4cbdd5
KH
11796 int modified_top, modified_left;
11797
aa3ff7c9 11798 if (change_gravity > 0)
3a35ab44 11799 {
7556890b
RS
11800 f->output_data.x->top_pos = yoff;
11801 f->output_data.x->left_pos = xoff;
11802 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
3a35ab44 11803 if (xoff < 0)
7556890b 11804 f->output_data.x->size_hint_flags |= XNegative;
3a35ab44 11805 if (yoff < 0)
7556890b
RS
11806 f->output_data.x->size_hint_flags |= YNegative;
11807 f->output_data.x->win_gravity = NorthWestGravity;
3a35ab44 11808 }
43bca5d5 11809 x_calc_absolute_position (f);
dc6f92b8
JB
11810
11811 BLOCK_INPUT;
c32cdd9a 11812 x_wm_set_size_hint (f, (long) 0, 0);
3a35ab44 11813
7556890b
RS
11814 modified_left = f->output_data.x->left_pos;
11815 modified_top = f->output_data.x->top_pos;
e73ec6fa
RS
11816#if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
11817 this seems to be unnecessary and incorrect. rms, 4/17/97. */
11818 /* It is a mystery why we need to add the border_width here
11819 when the frame is already visible, but experiment says we do. */
aa3ff7c9 11820 if (change_gravity != 0)
4a4cbdd5 11821 {
7556890b
RS
11822 modified_left += f->output_data.x->border_width;
11823 modified_top += f->output_data.x->border_width;
4a4cbdd5 11824 }
e73ec6fa 11825#endif
4a4cbdd5 11826
3afe33e7 11827#ifdef USE_X_TOOLKIT
7556890b 11828 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
4a4cbdd5 11829 modified_left, modified_top);
3afe33e7 11830#else /* not USE_X_TOOLKIT */
334208b7 11831 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4a4cbdd5 11832 modified_left, modified_top);
3afe33e7 11833#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11834 UNBLOCK_INPUT;
11835}
11836
bc20ebbf
FP
11837/* Call this to change the size of frame F's x-window.
11838 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
11839 for this size change and subsequent size changes.
11840 Otherwise we leave the window gravity unchanged. */
dc6f92b8 11841
dfcf069d 11842void
bc20ebbf 11843x_set_window_size (f, change_gravity, cols, rows)
f676886a 11844 struct frame *f;
bc20ebbf 11845 int change_gravity;
b1c884c3 11846 int cols, rows;
dc6f92b8 11847{
06a2c219 11848#ifndef USE_X_TOOLKIT
dc6f92b8 11849 int pixelwidth, pixelheight;
06a2c219 11850#endif
dc6f92b8 11851
80fd1fe2 11852 BLOCK_INPUT;
aee9a898
RS
11853
11854#ifdef USE_X_TOOLKIT
3a20653d
RS
11855 {
11856 /* The x and y position of the widget is clobbered by the
11857 call to XtSetValues within EmacsFrameSetCharSize.
11858 This is a real kludge, but I don't understand Xt so I can't
11859 figure out a correct fix. Can anyone else tell me? -- rms. */
7556890b
RS
11860 int xpos = f->output_data.x->widget->core.x;
11861 int ypos = f->output_data.x->widget->core.y;
11862 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
11863 f->output_data.x->widget->core.x = xpos;
11864 f->output_data.x->widget->core.y = ypos;
3a20653d 11865 }
80fd1fe2
FP
11866
11867#else /* not USE_X_TOOLKIT */
11868
b1c884c3 11869 check_frame_size (f, &rows, &cols);
7556890b 11870 f->output_data.x->vertical_scroll_bar_extra
b2cad826
KH
11871 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
11872 ? 0
11873 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
480407eb 11874 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7556890b 11875 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
06a2c219 11876 f->output_data.x->flags_areas_extra
110859fc 11877 = FRAME_FLAGS_AREA_WIDTH (f);
f451eb13
JB
11878 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
11879 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
dc6f92b8 11880
7556890b 11881 f->output_data.x->win_gravity = NorthWestGravity;
c32cdd9a 11882 x_wm_set_size_hint (f, (long) 0, 0);
6ccf47d1 11883
334208b7
RS
11884 XSync (FRAME_X_DISPLAY (f), False);
11885 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11886 pixelwidth, pixelheight);
b1c884c3
JB
11887
11888 /* Now, strictly speaking, we can't be sure that this is accurate,
11889 but the window manager will get around to dealing with the size
11890 change request eventually, and we'll hear how it went when the
dbc4e1c1
JB
11891 ConfigureNotify event gets here.
11892
11893 We could just not bother storing any of this information here,
11894 and let the ConfigureNotify event set everything up, but that
fddd5ceb 11895 might be kind of confusing to the Lisp code, since size changes
dbc4e1c1 11896 wouldn't be reported in the frame parameters until some random
fddd5ceb
RS
11897 point in the future when the ConfigureNotify event arrives.
11898
11899 We pass 1 for DELAY since we can't run Lisp code inside of
11900 a BLOCK_INPUT. */
7d1e984f 11901 change_frame_size (f, rows, cols, 0, 1, 0);
b1c884c3
JB
11902 PIXEL_WIDTH (f) = pixelwidth;
11903 PIXEL_HEIGHT (f) = pixelheight;
11904
aee9a898
RS
11905 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11906 receive in the ConfigureNotify event; if we get what we asked
11907 for, then the event won't cause the screen to become garbaged, so
11908 we have to make sure to do it here. */
11909 SET_FRAME_GARBAGED (f);
11910
11911 XFlush (FRAME_X_DISPLAY (f));
11912
11913#endif /* not USE_X_TOOLKIT */
11914
4d73d038 11915 /* If cursor was outside the new size, mark it as off. */
06a2c219 11916 mark_window_cursors_off (XWINDOW (f->root_window));
4d73d038 11917
aee9a898
RS
11918 /* Clear out any recollection of where the mouse highlighting was,
11919 since it might be in a place that's outside the new frame size.
11920 Actually checking whether it is outside is a pain in the neck,
11921 so don't try--just let the highlighting be done afresh with new size. */
e687d06e 11922 cancel_mouse_face (f);
dbc4e1c1 11923
dc6f92b8
JB
11924 UNBLOCK_INPUT;
11925}
dc6f92b8 11926\f
d047c4eb 11927/* Mouse warping. */
dc6f92b8 11928
9b378208 11929void
f676886a
JB
11930x_set_mouse_position (f, x, y)
11931 struct frame *f;
dc6f92b8
JB
11932 int x, y;
11933{
11934 int pix_x, pix_y;
11935
7556890b
RS
11936 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
11937 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
f451eb13
JB
11938
11939 if (pix_x < 0) pix_x = 0;
11940 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
11941
11942 if (pix_y < 0) pix_y = 0;
11943 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
dc6f92b8
JB
11944
11945 BLOCK_INPUT;
dc6f92b8 11946
334208b7
RS
11947 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11948 0, 0, 0, 0, pix_x, pix_y);
dc6f92b8
JB
11949 UNBLOCK_INPUT;
11950}
11951
9b378208
RS
11952/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11953
11954void
11955x_set_mouse_pixel_position (f, pix_x, pix_y)
11956 struct frame *f;
11957 int pix_x, pix_y;
11958{
11959 BLOCK_INPUT;
11960
334208b7
RS
11961 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11962 0, 0, 0, 0, pix_x, pix_y);
9b378208
RS
11963 UNBLOCK_INPUT;
11964}
d047c4eb
KH
11965\f
11966/* focus shifting, raising and lowering. */
9b378208 11967
dfcf069d 11968void
f676886a
JB
11969x_focus_on_frame (f)
11970 struct frame *f;
dc6f92b8 11971{
1fb20991 11972#if 0 /* This proves to be unpleasant. */
f676886a 11973 x_raise_frame (f);
1fb20991 11974#endif
6d4238f3
JB
11975#if 0
11976 /* I don't think that the ICCCM allows programs to do things like this
11977 without the interaction of the window manager. Whatever you end up
f676886a 11978 doing with this code, do it to x_unfocus_frame too. */
334208b7 11979 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dc6f92b8 11980 RevertToPointerRoot, CurrentTime);
c118dd06 11981#endif /* ! 0 */
dc6f92b8
JB
11982}
11983
dfcf069d 11984void
f676886a
JB
11985x_unfocus_frame (f)
11986 struct frame *f;
dc6f92b8 11987{
6d4238f3 11988#if 0
f676886a 11989 /* Look at the remarks in x_focus_on_frame. */
0f941935 11990 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
334208b7 11991 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
dc6f92b8 11992 RevertToPointerRoot, CurrentTime);
c118dd06 11993#endif /* ! 0 */
dc6f92b8
JB
11994}
11995
f676886a 11996/* Raise frame F. */
dc6f92b8 11997
dfcf069d 11998void
f676886a
JB
11999x_raise_frame (f)
12000 struct frame *f;
dc6f92b8 12001{
3a88c238 12002 if (f->async_visible)
dc6f92b8
JB
12003 {
12004 BLOCK_INPUT;
3afe33e7 12005#ifdef USE_X_TOOLKIT
7556890b 12006 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 12007#else /* not USE_X_TOOLKIT */
334208b7 12008 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 12009#endif /* not USE_X_TOOLKIT */
334208b7 12010 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
12011 UNBLOCK_INPUT;
12012 }
12013}
12014
f676886a 12015/* Lower frame F. */
dc6f92b8 12016
dfcf069d 12017void
f676886a
JB
12018x_lower_frame (f)
12019 struct frame *f;
dc6f92b8 12020{
3a88c238 12021 if (f->async_visible)
dc6f92b8
JB
12022 {
12023 BLOCK_INPUT;
3afe33e7 12024#ifdef USE_X_TOOLKIT
7556890b 12025 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 12026#else /* not USE_X_TOOLKIT */
334208b7 12027 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 12028#endif /* not USE_X_TOOLKIT */
334208b7 12029 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
12030 UNBLOCK_INPUT;
12031 }
12032}
12033
dbc4e1c1 12034static void
6b0442dc 12035XTframe_raise_lower (f, raise_flag)
dbc4e1c1 12036 FRAME_PTR f;
6b0442dc 12037 int raise_flag;
dbc4e1c1 12038{
6b0442dc 12039 if (raise_flag)
dbc4e1c1
JB
12040 x_raise_frame (f);
12041 else
12042 x_lower_frame (f);
12043}
d047c4eb
KH
12044\f
12045/* Change of visibility. */
dc6f92b8 12046
9382638d
KH
12047/* This tries to wait until the frame is really visible.
12048 However, if the window manager asks the user where to position
12049 the frame, this will return before the user finishes doing that.
12050 The frame will not actually be visible at that time,
12051 but it will become visible later when the window manager
12052 finishes with it. */
12053
dfcf069d 12054void
f676886a
JB
12055x_make_frame_visible (f)
12056 struct frame *f;
dc6f92b8 12057{
990ba854 12058 Lisp_Object type;
1aa6072f 12059 int original_top, original_left;
31be9251
GM
12060 int retry_count = 2;
12061
12062 retry:
dc6f92b8 12063
dc6f92b8 12064 BLOCK_INPUT;
dc6f92b8 12065
990ba854
RS
12066 type = x_icon_type (f);
12067 if (!NILP (type))
12068 x_bitmap_icon (f, type);
bdcd49ba 12069
f676886a 12070 if (! FRAME_VISIBLE_P (f))
90e65f07 12071 {
1aa6072f
RS
12072 /* We test FRAME_GARBAGED_P here to make sure we don't
12073 call x_set_offset a second time
12074 if we get to x_make_frame_visible a second time
12075 before the window gets really visible. */
12076 if (! FRAME_ICONIFIED_P (f)
12077 && ! f->output_data.x->asked_for_visible)
12078 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12079
12080 f->output_data.x->asked_for_visible = 1;
12081
90e65f07 12082 if (! EQ (Vx_no_window_manager, Qt))
f676886a 12083 x_wm_set_window_state (f, NormalState);
3afe33e7 12084#ifdef USE_X_TOOLKIT
d7a38a2e 12085 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 12086 XtMapWidget (f->output_data.x->widget);
3afe33e7 12087#else /* not USE_X_TOOLKIT */
7f9c7f94 12088 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 12089#endif /* not USE_X_TOOLKIT */
0134a210
RS
12090#if 0 /* This seems to bring back scroll bars in the wrong places
12091 if the window configuration has changed. They seem
12092 to come back ok without this. */
ab648270 12093 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
334208b7 12094 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
0134a210 12095#endif
90e65f07 12096 }
dc6f92b8 12097
334208b7 12098 XFlush (FRAME_X_DISPLAY (f));
90e65f07 12099
0dacf791
RS
12100 /* Synchronize to ensure Emacs knows the frame is visible
12101 before we do anything else. We do this loop with input not blocked
12102 so that incoming events are handled. */
12103 {
12104 Lisp_Object frame;
12ce2351 12105 int count;
28c01ffe
RS
12106 /* This must be before UNBLOCK_INPUT
12107 since events that arrive in response to the actions above
12108 will set it when they are handled. */
12109 int previously_visible = f->output_data.x->has_been_visible;
1aa6072f
RS
12110
12111 original_left = f->output_data.x->left_pos;
12112 original_top = f->output_data.x->top_pos;
c0a04927
RS
12113
12114 /* This must come after we set COUNT. */
12115 UNBLOCK_INPUT;
12116
2745e6c4 12117 /* We unblock here so that arriving X events are processed. */
1aa6072f 12118
dcb07ae9
RS
12119 /* Now move the window back to where it was "supposed to be".
12120 But don't do it if the gravity is negative.
12121 When the gravity is negative, this uses a position
28c01ffe
RS
12122 that is 3 pixels too low. Perhaps that's really the border width.
12123
12124 Don't do this if the window has never been visible before,
12125 because the window manager may choose the position
12126 and we don't want to override it. */
1aa6072f 12127
4d3f5d9a 12128 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
06c488fd 12129 && f->output_data.x->win_gravity == NorthWestGravity
28c01ffe 12130 && previously_visible)
1aa6072f 12131 {
2745e6c4
RS
12132 Drawable rootw;
12133 int x, y;
12134 unsigned int width, height, border, depth;
06a2c219 12135
1aa6072f 12136 BLOCK_INPUT;
9829ddba 12137
06a2c219
GM
12138 /* On some window managers (such as FVWM) moving an existing
12139 window, even to the same place, causes the window manager
12140 to introduce an offset. This can cause the window to move
12141 to an unexpected location. Check the geometry (a little
12142 slow here) and then verify that the window is in the right
12143 place. If the window is not in the right place, move it
12144 there, and take the potential window manager hit. */
2745e6c4
RS
12145 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12146 &rootw, &x, &y, &width, &height, &border, &depth);
12147
12148 if (original_left != x || original_top != y)
12149 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12150 original_left, original_top);
12151
1aa6072f
RS
12152 UNBLOCK_INPUT;
12153 }
9829ddba 12154
e0c1aef2 12155 XSETFRAME (frame, f);
c0a04927 12156
12ce2351
GM
12157 /* Wait until the frame is visible. Process X events until a
12158 MapNotify event has been seen, or until we think we won't get a
12159 MapNotify at all.. */
12160 for (count = input_signal_count + 10;
12161 input_signal_count < count && !FRAME_VISIBLE_P (f);)
2a6cf806 12162 {
12ce2351 12163 /* Force processing of queued events. */
334208b7 12164 x_sync (f);
12ce2351
GM
12165
12166 /* Machines that do polling rather than SIGIO have been
12167 observed to go into a busy-wait here. So we'll fake an
12168 alarm signal to let the handler know that there's something
12169 to be read. We used to raise a real alarm, but it seems
12170 that the handler isn't always enabled here. This is
12171 probably a bug. */
8b2f8d4e 12172 if (input_polling_used ())
3b2fa4e6 12173 {
12ce2351
GM
12174 /* It could be confusing if a real alarm arrives while
12175 processing the fake one. Turn it off and let the
12176 handler reset it. */
3e71d8f2 12177 extern void poll_for_input_1 P_ ((void));
bffcfca9
GM
12178 int old_poll_suppress_count = poll_suppress_count;
12179 poll_suppress_count = 1;
12180 poll_for_input_1 ();
12181 poll_suppress_count = old_poll_suppress_count;
3b2fa4e6 12182 }
12ce2351
GM
12183
12184 /* See if a MapNotify event has been processed. */
12185 FRAME_SAMPLE_VISIBILITY (f);
2a6cf806 12186 }
31be9251
GM
12187
12188 /* 2000-09-28: In
12189
12190 (let ((f (selected-frame)))
12191 (iconify-frame f)
12192 (raise-frame f))
12193
12194 the frame is not raised with various window managers on
12195 FreeBSD, Linux and Solaris. It turns out that, for some
12196 unknown reason, the call to XtMapWidget is completely ignored.
12197 Mapping the widget a second time works. */
12198
12199 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
12200 goto retry;
0dacf791 12201 }
dc6f92b8
JB
12202}
12203
06a2c219 12204/* Change from mapped state to withdrawn state. */
dc6f92b8 12205
d047c4eb
KH
12206/* Make the frame visible (mapped and not iconified). */
12207
dfcf069d 12208void
f676886a
JB
12209x_make_frame_invisible (f)
12210 struct frame *f;
dc6f92b8 12211{
546e6d5b
RS
12212 Window window;
12213
12214#ifdef USE_X_TOOLKIT
12215 /* Use the frame's outermost window, not the one we normally draw on. */
7556890b 12216 window = XtWindow (f->output_data.x->widget);
546e6d5b
RS
12217#else /* not USE_X_TOOLKIT */
12218 window = FRAME_X_WINDOW (f);
12219#endif /* not USE_X_TOOLKIT */
dc6f92b8 12220
9319ae23 12221 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
12222 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12223 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 12224
5627c40e 12225#if 0/* This might add unreliability; I don't trust it -- rms. */
9319ae23 12226 if (! f->async_visible && ! f->async_iconified)
dc6f92b8 12227 return;
5627c40e 12228#endif
dc6f92b8
JB
12229
12230 BLOCK_INPUT;
c118dd06 12231
af31d76f
RS
12232 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
12233 that the current position of the window is user-specified, rather than
12234 program-specified, so that when the window is mapped again, it will be
12235 placed at the same location, without forcing the user to position it
12236 by hand again (they have already done that once for this window.) */
c32cdd9a 12237 x_wm_set_size_hint (f, (long) 0, 1);
af31d76f 12238
c118dd06
JB
12239#ifdef HAVE_X11R4
12240
334208b7
RS
12241 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
12242 DefaultScreen (FRAME_X_DISPLAY (f))))
c118dd06
JB
12243 {
12244 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 12245 error ("Can't notify window manager of window withdrawal");
c118dd06 12246 }
c118dd06 12247#else /* ! defined (HAVE_X11R4) */
16bd92ea 12248
c118dd06 12249 /* Tell the window manager what we're going to do. */
dc6f92b8
JB
12250 if (! EQ (Vx_no_window_manager, Qt))
12251 {
16bd92ea 12252 XEvent unmap;
dc6f92b8 12253
16bd92ea 12254 unmap.xunmap.type = UnmapNotify;
546e6d5b 12255 unmap.xunmap.window = window;
334208b7 12256 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
16bd92ea 12257 unmap.xunmap.from_configure = False;
334208b7
RS
12258 if (! XSendEvent (FRAME_X_DISPLAY (f),
12259 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea 12260 False,
06a2c219 12261 SubstructureRedirectMaskSubstructureNotifyMask,
16bd92ea
JB
12262 &unmap))
12263 {
12264 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 12265 error ("Can't notify window manager of withdrawal");
16bd92ea 12266 }
dc6f92b8
JB
12267 }
12268
16bd92ea 12269 /* Unmap the window ourselves. Cheeky! */
334208b7 12270 XUnmapWindow (FRAME_X_DISPLAY (f), window);
c118dd06 12271#endif /* ! defined (HAVE_X11R4) */
dc6f92b8 12272
5627c40e
RS
12273 /* We can't distinguish this from iconification
12274 just by the event that we get from the server.
12275 So we can't win using the usual strategy of letting
12276 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
12277 and synchronize with the server to make sure we agree. */
12278 f->visible = 0;
12279 FRAME_ICONIFIED_P (f) = 0;
12280 f->async_visible = 0;
12281 f->async_iconified = 0;
12282
334208b7 12283 x_sync (f);
5627c40e 12284
dc6f92b8
JB
12285 UNBLOCK_INPUT;
12286}
12287
06a2c219 12288/* Change window state from mapped to iconified. */
dc6f92b8 12289
dfcf069d 12290void
f676886a
JB
12291x_iconify_frame (f)
12292 struct frame *f;
dc6f92b8 12293{
3afe33e7 12294 int result;
990ba854 12295 Lisp_Object type;
dc6f92b8 12296
9319ae23 12297 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
12298 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12299 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 12300
3a88c238 12301 if (f->async_iconified)
dc6f92b8
JB
12302 return;
12303
3afe33e7 12304 BLOCK_INPUT;
546e6d5b 12305
9af3143a
RS
12306 FRAME_SAMPLE_VISIBILITY (f);
12307
990ba854
RS
12308 type = x_icon_type (f);
12309 if (!NILP (type))
12310 x_bitmap_icon (f, type);
bdcd49ba
RS
12311
12312#ifdef USE_X_TOOLKIT
12313
546e6d5b
RS
12314 if (! FRAME_VISIBLE_P (f))
12315 {
12316 if (! EQ (Vx_no_window_manager, Qt))
12317 x_wm_set_window_state (f, IconicState);
12318 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 12319 XtMapWidget (f->output_data.x->widget);
9cf30a30
RS
12320 /* The server won't give us any event to indicate
12321 that an invisible frame was changed to an icon,
12322 so we have to record it here. */
12323 f->iconified = 1;
1e6bc770 12324 f->visible = 1;
9cf30a30 12325 f->async_iconified = 1;
1e6bc770 12326 f->async_visible = 0;
546e6d5b
RS
12327 UNBLOCK_INPUT;
12328 return;
12329 }
12330
334208b7 12331 result = XIconifyWindow (FRAME_X_DISPLAY (f),
7556890b 12332 XtWindow (f->output_data.x->widget),
334208b7 12333 DefaultScreen (FRAME_X_DISPLAY (f)));
3afe33e7
RS
12334 UNBLOCK_INPUT;
12335
12336 if (!result)
546e6d5b 12337 error ("Can't notify window manager of iconification");
3afe33e7
RS
12338
12339 f->async_iconified = 1;
1e6bc770
RS
12340 f->async_visible = 0;
12341
8c002a25
KH
12342
12343 BLOCK_INPUT;
334208b7 12344 XFlush (FRAME_X_DISPLAY (f));
8c002a25 12345 UNBLOCK_INPUT;
3afe33e7
RS
12346#else /* not USE_X_TOOLKIT */
12347
fd13dbb2
RS
12348 /* Make sure the X server knows where the window should be positioned,
12349 in case the user deiconifies with the window manager. */
12350 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
7556890b 12351 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
fd13dbb2 12352
16bd92ea
JB
12353 /* Since we don't know which revision of X we're running, we'll use both
12354 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
12355
12356 /* X11R4: send a ClientMessage to the window manager using the
12357 WM_CHANGE_STATE type. */
12358 {
12359 XEvent message;
58769bee 12360
c118dd06 12361 message.xclient.window = FRAME_X_WINDOW (f);
16bd92ea 12362 message.xclient.type = ClientMessage;
334208b7 12363 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
16bd92ea
JB
12364 message.xclient.format = 32;
12365 message.xclient.data.l[0] = IconicState;
12366
334208b7
RS
12367 if (! XSendEvent (FRAME_X_DISPLAY (f),
12368 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea
JB
12369 False,
12370 SubstructureRedirectMask | SubstructureNotifyMask,
12371 &message))
dc6f92b8
JB
12372 {
12373 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 12374 error ("Can't notify window manager of iconification");
dc6f92b8 12375 }
16bd92ea 12376 }
dc6f92b8 12377
58769bee 12378 /* X11R3: set the initial_state field of the window manager hints to
16bd92ea
JB
12379 IconicState. */
12380 x_wm_set_window_state (f, IconicState);
dc6f92b8 12381
a9c00105
RS
12382 if (!FRAME_VISIBLE_P (f))
12383 {
12384 /* If the frame was withdrawn, before, we must map it. */
7f9c7f94 12385 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
a9c00105
RS
12386 }
12387
3a88c238 12388 f->async_iconified = 1;
1e6bc770 12389 f->async_visible = 0;
dc6f92b8 12390
334208b7 12391 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 12392 UNBLOCK_INPUT;
8c002a25 12393#endif /* not USE_X_TOOLKIT */
dc6f92b8 12394}
d047c4eb 12395\f
c0ff3fab 12396/* Destroy the X window of frame F. */
dc6f92b8 12397
dfcf069d 12398void
c0ff3fab 12399x_destroy_window (f)
f676886a 12400 struct frame *f;
dc6f92b8 12401{
7f9c7f94
RS
12402 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12403
dc6f92b8 12404 BLOCK_INPUT;
c0ff3fab 12405
6186a4a0
RS
12406 /* If a display connection is dead, don't try sending more
12407 commands to the X server. */
12408 if (dpyinfo->display != 0)
12409 {
12410 if (f->output_data.x->icon_desc != 0)
12411 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
31f41daf 12412#ifdef HAVE_X_I18N
f5d11644
GM
12413 if (FRAME_XIC (f))
12414 free_frame_xic (f);
31f41daf 12415#endif
2662734b
GM
12416 if (FRAME_X_WINDOW (f))
12417 {
12418 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12419 FRAME_X_WINDOW (f) = 0;
12420 }
3afe33e7 12421#ifdef USE_X_TOOLKIT
06a2c219
GM
12422 if (f->output_data.x->widget)
12423 XtDestroyWidget (f->output_data.x->widget);
6186a4a0 12424 free_frame_menubar (f);
3afe33e7
RS
12425#endif /* USE_X_TOOLKIT */
12426
3e71d8f2
GM
12427 unload_color (f, f->output_data.x->foreground_pixel);
12428 unload_color (f, f->output_data.x->background_pixel);
12429 unload_color (f, f->output_data.x->cursor_pixel);
12430 unload_color (f, f->output_data.x->cursor_foreground_pixel);
12431 unload_color (f, f->output_data.x->border_pixel);
12432 unload_color (f, f->output_data.x->mouse_pixel);
12433 if (f->output_data.x->scroll_bar_background_pixel != -1)
12434 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
12435 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
12436 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
12437 if (f->output_data.x->white_relief.allocated_p)
12438 unload_color (f, f->output_data.x->white_relief.pixel);
12439 if (f->output_data.x->black_relief.allocated_p)
12440 unload_color (f, f->output_data.x->black_relief.pixel);
12441
6186a4a0
RS
12442 free_frame_faces (f);
12443 XFlush (FRAME_X_DISPLAY (f));
12444 }
dc6f92b8 12445
df89d8a4 12446 if (f->output_data.x->saved_menu_event)
06a2c219 12447 xfree (f->output_data.x->saved_menu_event);
0c49ce2f 12448
7556890b
RS
12449 xfree (f->output_data.x);
12450 f->output_data.x = 0;
0f941935
KH
12451 if (f == dpyinfo->x_focus_frame)
12452 dpyinfo->x_focus_frame = 0;
12453 if (f == dpyinfo->x_focus_event_frame)
12454 dpyinfo->x_focus_event_frame = 0;
12455 if (f == dpyinfo->x_highlight_frame)
12456 dpyinfo->x_highlight_frame = 0;
c0ff3fab 12457
7f9c7f94
RS
12458 dpyinfo->reference_count--;
12459
12460 if (f == dpyinfo->mouse_face_mouse_frame)
dc05a16b 12461 {
7f9c7f94
RS
12462 dpyinfo->mouse_face_beg_row
12463 = dpyinfo->mouse_face_beg_col = -1;
12464 dpyinfo->mouse_face_end_row
12465 = dpyinfo->mouse_face_end_col = -1;
12466 dpyinfo->mouse_face_window = Qnil;
21323706
RS
12467 dpyinfo->mouse_face_deferred_gc = 0;
12468 dpyinfo->mouse_face_mouse_frame = 0;
dc05a16b 12469 }
0134a210 12470
c0ff3fab 12471 UNBLOCK_INPUT;
dc6f92b8
JB
12472}
12473\f
f451eb13
JB
12474/* Setting window manager hints. */
12475
af31d76f
RS
12476/* Set the normal size hints for the window manager, for frame F.
12477 FLAGS is the flags word to use--or 0 meaning preserve the flags
12478 that the window now has.
12479 If USER_POSITION is nonzero, we set the USPosition
12480 flag (this is useful when FLAGS is 0). */
6dba1858 12481
dfcf069d 12482void
af31d76f 12483x_wm_set_size_hint (f, flags, user_position)
f676886a 12484 struct frame *f;
af31d76f
RS
12485 long flags;
12486 int user_position;
dc6f92b8
JB
12487{
12488 XSizeHints size_hints;
3afe33e7
RS
12489
12490#ifdef USE_X_TOOLKIT
7e4f2521
FP
12491 Arg al[2];
12492 int ac = 0;
12493 Dimension widget_width, widget_height;
7556890b 12494 Window window = XtWindow (f->output_data.x->widget);
3afe33e7 12495#else /* not USE_X_TOOLKIT */
c118dd06 12496 Window window = FRAME_X_WINDOW (f);
3afe33e7 12497#endif /* not USE_X_TOOLKIT */
dc6f92b8 12498
b72a58fd
RS
12499 /* Setting PMaxSize caused various problems. */
12500 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
dc6f92b8 12501
7556890b
RS
12502 size_hints.x = f->output_data.x->left_pos;
12503 size_hints.y = f->output_data.x->top_pos;
7553a6b7 12504
7e4f2521
FP
12505#ifdef USE_X_TOOLKIT
12506 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
12507 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
7556890b 12508 XtGetValues (f->output_data.x->widget, al, ac);
7e4f2521
FP
12509 size_hints.height = widget_height;
12510 size_hints.width = widget_width;
12511#else /* not USE_X_TOOLKIT */
f676886a
JB
12512 size_hints.height = PIXEL_HEIGHT (f);
12513 size_hints.width = PIXEL_WIDTH (f);
7e4f2521 12514#endif /* not USE_X_TOOLKIT */
7553a6b7 12515
7556890b
RS
12516 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
12517 size_hints.height_inc = f->output_data.x->line_height;
334208b7
RS
12518 size_hints.max_width
12519 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
12520 size_hints.max_height
12521 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
0134a210 12522
d067ea8b
KH
12523 /* Calculate the base and minimum sizes.
12524
12525 (When we use the X toolkit, we don't do it here.
12526 Instead we copy the values that the widgets are using, below.) */
12527#ifndef USE_X_TOOLKIT
b1c884c3 12528 {
b0342f17 12529 int base_width, base_height;
0134a210 12530 int min_rows = 0, min_cols = 0;
b0342f17 12531
f451eb13
JB
12532 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
12533 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
b0342f17 12534
0134a210 12535 check_frame_size (f, &min_rows, &min_cols);
b0342f17 12536
0134a210
RS
12537 /* The window manager uses the base width hints to calculate the
12538 current number of rows and columns in the frame while
12539 resizing; min_width and min_height aren't useful for this
12540 purpose, since they might not give the dimensions for a
12541 zero-row, zero-column frame.
58769bee 12542
0134a210
RS
12543 We use the base_width and base_height members if we have
12544 them; otherwise, we set the min_width and min_height members
12545 to the size for a zero x zero frame. */
b0342f17
JB
12546
12547#ifdef HAVE_X11R4
0134a210
RS
12548 size_hints.flags |= PBaseSize;
12549 size_hints.base_width = base_width;
12550 size_hints.base_height = base_height;
12551 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
12552 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
b0342f17 12553#else
0134a210
RS
12554 size_hints.min_width = base_width;
12555 size_hints.min_height = base_height;
b0342f17 12556#endif
b1c884c3 12557 }
dc6f92b8 12558
d067ea8b 12559 /* If we don't need the old flags, we don't need the old hint at all. */
af31d76f 12560 if (flags)
dc6f92b8 12561 {
d067ea8b
KH
12562 size_hints.flags |= flags;
12563 goto no_read;
12564 }
12565#endif /* not USE_X_TOOLKIT */
12566
12567 {
12568 XSizeHints hints; /* Sometimes I hate X Windows... */
12569 long supplied_return;
12570 int value;
af31d76f
RS
12571
12572#ifdef HAVE_X11R4
d067ea8b
KH
12573 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
12574 &supplied_return);
af31d76f 12575#else
d067ea8b 12576 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
af31d76f 12577#endif
58769bee 12578
d067ea8b
KH
12579#ifdef USE_X_TOOLKIT
12580 size_hints.base_height = hints.base_height;
12581 size_hints.base_width = hints.base_width;
12582 size_hints.min_height = hints.min_height;
12583 size_hints.min_width = hints.min_width;
12584#endif
12585
12586 if (flags)
12587 size_hints.flags |= flags;
12588 else
12589 {
12590 if (value == 0)
12591 hints.flags = 0;
12592 if (hints.flags & PSize)
12593 size_hints.flags |= PSize;
12594 if (hints.flags & PPosition)
12595 size_hints.flags |= PPosition;
12596 if (hints.flags & USPosition)
12597 size_hints.flags |= USPosition;
12598 if (hints.flags & USSize)
12599 size_hints.flags |= USSize;
12600 }
12601 }
12602
06a2c219 12603#ifndef USE_X_TOOLKIT
d067ea8b 12604 no_read:
06a2c219 12605#endif
0134a210 12606
af31d76f 12607#ifdef PWinGravity
7556890b 12608 size_hints.win_gravity = f->output_data.x->win_gravity;
af31d76f 12609 size_hints.flags |= PWinGravity;
dc05a16b 12610
af31d76f 12611 if (user_position)
6dba1858 12612 {
af31d76f
RS
12613 size_hints.flags &= ~ PPosition;
12614 size_hints.flags |= USPosition;
6dba1858 12615 }
2554751d 12616#endif /* PWinGravity */
6dba1858 12617
b0342f17 12618#ifdef HAVE_X11R4
334208b7 12619 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 12620#else
334208b7 12621 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 12622#endif
dc6f92b8
JB
12623}
12624
12625/* Used for IconicState or NormalState */
06a2c219 12626
dfcf069d 12627void
f676886a
JB
12628x_wm_set_window_state (f, state)
12629 struct frame *f;
dc6f92b8
JB
12630 int state;
12631{
3afe33e7 12632#ifdef USE_X_TOOLKIT
546e6d5b
RS
12633 Arg al[1];
12634
12635 XtSetArg (al[0], XtNinitialState, state);
7556890b 12636 XtSetValues (f->output_data.x->widget, al, 1);
3afe33e7 12637#else /* not USE_X_TOOLKIT */
c118dd06 12638 Window window = FRAME_X_WINDOW (f);
dc6f92b8 12639
7556890b
RS
12640 f->output_data.x->wm_hints.flags |= StateHint;
12641 f->output_data.x->wm_hints.initial_state = state;
b1c884c3 12642
7556890b 12643 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
546e6d5b 12644#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12645}
12646
dfcf069d 12647void
7f2ae036 12648x_wm_set_icon_pixmap (f, pixmap_id)
f676886a 12649 struct frame *f;
7f2ae036 12650 int pixmap_id;
dc6f92b8 12651{
d2bd6bc4
RS
12652 Pixmap icon_pixmap;
12653
06a2c219 12654#ifndef USE_X_TOOLKIT
c118dd06 12655 Window window = FRAME_X_WINDOW (f);
75231bad 12656#endif
dc6f92b8 12657
7f2ae036 12658 if (pixmap_id > 0)
dbc4e1c1 12659 {
d2bd6bc4 12660 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
7556890b 12661 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
dbc4e1c1
JB
12662 }
12663 else
68568555
RS
12664 {
12665 /* It seems there is no way to turn off use of an icon pixmap.
12666 The following line does it, only if no icon has yet been created,
12667 for some window managers. But with mwm it crashes.
12668 Some people say it should clear the IconPixmapHint bit in this case,
12669 but that doesn't work, and the X consortium said it isn't the
12670 right thing at all. Since there is no way to win,
12671 best to explicitly give up. */
12672#if 0
12673 f->output_data.x->wm_hints.icon_pixmap = None;
12674#else
12675 return;
12676#endif
12677 }
b1c884c3 12678
d2bd6bc4
RS
12679#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
12680
12681 {
12682 Arg al[1];
12683 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12684 XtSetValues (f->output_data.x->widget, al, 1);
12685 }
12686
12687#else /* not USE_X_TOOLKIT */
12688
7556890b
RS
12689 f->output_data.x->wm_hints.flags |= IconPixmapHint;
12690 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
d2bd6bc4
RS
12691
12692#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12693}
12694
dfcf069d 12695void
f676886a
JB
12696x_wm_set_icon_position (f, icon_x, icon_y)
12697 struct frame *f;
dc6f92b8
JB
12698 int icon_x, icon_y;
12699{
75231bad 12700#ifdef USE_X_TOOLKIT
7556890b 12701 Window window = XtWindow (f->output_data.x->widget);
75231bad 12702#else
c118dd06 12703 Window window = FRAME_X_WINDOW (f);
75231bad 12704#endif
dc6f92b8 12705
7556890b
RS
12706 f->output_data.x->wm_hints.flags |= IconPositionHint;
12707 f->output_data.x->wm_hints.icon_x = icon_x;
12708 f->output_data.x->wm_hints.icon_y = icon_y;
b1c884c3 12709
7556890b 12710 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
dc6f92b8
JB
12711}
12712
12713\f
06a2c219
GM
12714/***********************************************************************
12715 Fonts
12716 ***********************************************************************/
dc43ef94
KH
12717
12718/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
06a2c219 12719
dc43ef94
KH
12720struct font_info *
12721x_get_font_info (f, font_idx)
12722 FRAME_PTR f;
12723 int font_idx;
12724{
12725 return (FRAME_X_FONT_TABLE (f) + font_idx);
12726}
12727
12728
12729/* Return a list of names of available fonts matching PATTERN on frame
12730 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
12731 to be listed. Frame F NULL means we have not yet created any
12732 frame on X, and consult the first display in x_display_list.
12733 MAXNAMES sets a limit on how many fonts to match. */
12734
12735Lisp_Object
12736x_list_fonts (f, pattern, size, maxnames)
12737 FRAME_PTR f;
12738 Lisp_Object pattern;
12739 int size;
12740 int maxnames;
12741{
06a2c219
GM
12742 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
12743 Lisp_Object tem, second_best;
dc43ef94 12744 Display *dpy = f != NULL ? FRAME_X_DISPLAY (f) : x_display_list->display;
09c6077f 12745 int try_XLoadQueryFont = 0;
53ca4657 12746 int count;
dc43ef94 12747
6b0efe73 12748 patterns = Fassoc (pattern, Valternate_fontname_alist);
2da424f1
KH
12749 if (NILP (patterns))
12750 patterns = Fcons (pattern, Qnil);
81ba44e5 12751
09c6077f
KH
12752 if (maxnames == 1 && !size)
12753 /* We can return any single font matching PATTERN. */
12754 try_XLoadQueryFont = 1;
9a32686f 12755
8e713be6 12756 for (; CONSP (patterns); patterns = XCDR (patterns))
dc43ef94 12757 {
dc43ef94 12758 int num_fonts;
3e71d8f2 12759 char **names = NULL;
dc43ef94 12760
8e713be6 12761 pattern = XCAR (patterns);
536f4067
RS
12762 /* See if we cached the result for this particular query.
12763 The cache is an alist of the form:
12764 (((PATTERN . MAXNAMES) (FONTNAME . WIDTH) ...) ...)
12765 */
8e713be6 12766 if (f && (tem = XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element),
b5210ea7
KH
12767 key = Fcons (pattern, make_number (maxnames)),
12768 !NILP (list = Fassoc (key, tem))))
12769 {
12770 list = Fcdr_safe (list);
12771 /* We have a cashed list. Don't have to get the list again. */
12772 goto label_cached;
12773 }
12774
12775 /* At first, put PATTERN in the cache. */
09c6077f 12776
dc43ef94 12777 BLOCK_INPUT;
17d85edc
KH
12778 count = x_catch_errors (dpy);
12779
09c6077f
KH
12780 if (try_XLoadQueryFont)
12781 {
12782 XFontStruct *font;
12783 unsigned long value;
12784
12785 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
17d85edc
KH
12786 if (x_had_errors_p (dpy))
12787 {
12788 /* This error is perhaps due to insufficient memory on X
12789 server. Let's just ignore it. */
12790 font = NULL;
12791 x_clear_errors (dpy);
12792 }
12793
09c6077f
KH
12794 if (font
12795 && XGetFontProperty (font, XA_FONT, &value))
12796 {
12797 char *name = (char *) XGetAtomName (dpy, (Atom) value);
12798 int len = strlen (name);
01c752b5 12799 char *tmp;
09c6077f 12800
6f6512e8
KH
12801 /* If DXPC (a Differential X Protocol Compressor)
12802 Ver.3.7 is running, XGetAtomName will return null
12803 string. We must avoid such a name. */
12804 if (len == 0)
12805 try_XLoadQueryFont = 0;
12806 else
12807 {
12808 num_fonts = 1;
12809 names = (char **) alloca (sizeof (char *));
12810 /* Some systems only allow alloca assigned to a
12811 simple var. */
12812 tmp = (char *) alloca (len + 1); names[0] = tmp;
12813 bcopy (name, names[0], len + 1);
12814 XFree (name);
12815 }
09c6077f
KH
12816 }
12817 else
12818 try_XLoadQueryFont = 0;
a083fd23
RS
12819
12820 if (font)
12821 XFreeFont (dpy, font);
09c6077f
KH
12822 }
12823
12824 if (!try_XLoadQueryFont)
17d85edc
KH
12825 {
12826 /* We try at least 10 fonts because XListFonts will return
12827 auto-scaled fonts at the head. */
12828 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
12829 &num_fonts);
12830 if (x_had_errors_p (dpy))
12831 {
12832 /* This error is perhaps due to insufficient memory on X
12833 server. Let's just ignore it. */
12834 names = NULL;
12835 x_clear_errors (dpy);
12836 }
12837 }
12838
12839 x_uncatch_errors (dpy, count);
dc43ef94
KH
12840 UNBLOCK_INPUT;
12841
12842 if (names)
12843 {
12844 int i;
dc43ef94
KH
12845
12846 /* Make a list of all the fonts we got back.
12847 Store that in the font cache for the display. */
12848 for (i = 0; i < num_fonts; i++)
12849 {
06a2c219 12850 int width = 0;
dc43ef94 12851 char *p = names[i];
06a2c219
GM
12852 int average_width = -1, dashes = 0;
12853
dc43ef94 12854 /* Count the number of dashes in NAMES[I]. If there are
b5210ea7
KH
12855 14 dashes, and the field value following 12th dash
12856 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
12857 is usually too ugly to be used for editing. Let's
12858 ignore it. */
dc43ef94
KH
12859 while (*p)
12860 if (*p++ == '-')
12861 {
12862 dashes++;
12863 if (dashes == 7) /* PIXEL_SIZE field */
12864 width = atoi (p);
12865 else if (dashes == 12) /* AVERAGE_WIDTH field */
12866 average_width = atoi (p);
12867 }
12868 if (dashes < 14 || average_width != 0)
12869 {
12870 tem = build_string (names[i]);
12871 if (NILP (Fassoc (tem, list)))
12872 {
12873 if (STRINGP (Vx_pixel_size_width_font_regexp)
f39db7ea
RS
12874 && ((fast_c_string_match_ignore_case
12875 (Vx_pixel_size_width_font_regexp, names[i]))
dc43ef94
KH
12876 >= 0))
12877 /* We can set the value of PIXEL_SIZE to the
b5210ea7 12878 width of this font. */
dc43ef94
KH
12879 list = Fcons (Fcons (tem, make_number (width)), list);
12880 else
12881 /* For the moment, width is not known. */
12882 list = Fcons (Fcons (tem, Qnil), list);
12883 }
12884 }
12885 }
09c6077f
KH
12886 if (!try_XLoadQueryFont)
12887 XFreeFontNames (names);
dc43ef94
KH
12888 }
12889
b5210ea7 12890 /* Now store the result in the cache. */
dc43ef94 12891 if (f != NULL)
8e713be6 12892 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element)
dc43ef94 12893 = Fcons (Fcons (key, list),
8e713be6 12894 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
dc43ef94 12895
b5210ea7
KH
12896 label_cached:
12897 if (NILP (list)) continue; /* Try the remaining alternatives. */
dc43ef94 12898
b5210ea7
KH
12899 newlist = second_best = Qnil;
12900 /* Make a list of the fonts that have the right width. */
8e713be6 12901 for (; CONSP (list); list = XCDR (list))
b5210ea7 12902 {
536f4067
RS
12903 int found_size;
12904
8e713be6 12905 tem = XCAR (list);
dc43ef94 12906
8e713be6 12907 if (!CONSP (tem) || NILP (XCAR (tem)))
b5210ea7
KH
12908 continue;
12909 if (!size)
12910 {
8e713be6 12911 newlist = Fcons (XCAR (tem), newlist);
b5210ea7
KH
12912 continue;
12913 }
dc43ef94 12914
8e713be6 12915 if (!INTEGERP (XCDR (tem)))
dc43ef94 12916 {
b5210ea7
KH
12917 /* Since we have not yet known the size of this font, we
12918 must try slow function call XLoadQueryFont. */
dc43ef94
KH
12919 XFontStruct *thisinfo;
12920
12921 BLOCK_INPUT;
17d85edc 12922 count = x_catch_errors (dpy);
dc43ef94 12923 thisinfo = XLoadQueryFont (dpy,
8e713be6 12924 XSTRING (XCAR (tem))->data);
17d85edc
KH
12925 if (x_had_errors_p (dpy))
12926 {
12927 /* This error is perhaps due to insufficient memory on X
12928 server. Let's just ignore it. */
12929 thisinfo = NULL;
12930 x_clear_errors (dpy);
12931 }
12932 x_uncatch_errors (dpy, count);
dc43ef94
KH
12933 UNBLOCK_INPUT;
12934
12935 if (thisinfo)
12936 {
8e713be6 12937 XCDR (tem)
536f4067
RS
12938 = (thisinfo->min_bounds.width == 0
12939 ? make_number (0)
12940 : make_number (thisinfo->max_bounds.width));
dc43ef94
KH
12941 XFreeFont (dpy, thisinfo);
12942 }
12943 else
b5210ea7 12944 /* For unknown reason, the previous call of XListFont had
06a2c219 12945 returned a font which can't be opened. Record the size
b5210ea7 12946 as 0 not to try to open it again. */
8e713be6 12947 XCDR (tem) = make_number (0);
dc43ef94 12948 }
536f4067 12949
8e713be6 12950 found_size = XINT (XCDR (tem));
536f4067 12951 if (found_size == size)
8e713be6 12952 newlist = Fcons (XCAR (tem), newlist);
536f4067 12953 else if (found_size > 0)
b5210ea7 12954 {
536f4067 12955 if (NILP (second_best))
b5210ea7 12956 second_best = tem;
536f4067
RS
12957 else if (found_size < size)
12958 {
8e713be6
KR
12959 if (XINT (XCDR (second_best)) > size
12960 || XINT (XCDR (second_best)) < found_size)
536f4067
RS
12961 second_best = tem;
12962 }
12963 else
12964 {
8e713be6
KR
12965 if (XINT (XCDR (second_best)) > size
12966 && XINT (XCDR (second_best)) > found_size)
536f4067
RS
12967 second_best = tem;
12968 }
b5210ea7
KH
12969 }
12970 }
12971 if (!NILP (newlist))
12972 break;
12973 else if (!NILP (second_best))
12974 {
8e713be6 12975 newlist = Fcons (XCAR (second_best), Qnil);
b5210ea7 12976 break;
dc43ef94 12977 }
dc43ef94
KH
12978 }
12979
12980 return newlist;
12981}
12982
06a2c219
GM
12983
12984#if GLYPH_DEBUG
12985
12986/* Check that FONT is valid on frame F. It is if it can be found in F's
12987 font table. */
12988
12989static void
12990x_check_font (f, font)
12991 struct frame *f;
12992 XFontStruct *font;
12993{
12994 int i;
12995 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12996
12997 xassert (font != NULL);
12998
12999 for (i = 0; i < dpyinfo->n_fonts; i++)
13000 if (dpyinfo->font_table[i].name
13001 && font == dpyinfo->font_table[i].font)
13002 break;
13003
13004 xassert (i < dpyinfo->n_fonts);
13005}
13006
13007#endif /* GLYPH_DEBUG != 0 */
13008
13009/* Set *W to the minimum width, *H to the minimum font height of FONT.
13010 Note: There are (broken) X fonts out there with invalid XFontStruct
13011 min_bounds contents. For example, handa@etl.go.jp reports that
13012 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
13013 have font->min_bounds.width == 0. */
13014
13015static INLINE void
13016x_font_min_bounds (font, w, h)
13017 XFontStruct *font;
13018 int *w, *h;
13019{
13020 *h = FONT_HEIGHT (font);
13021 *w = font->min_bounds.width;
13022
13023 /* Try to handle the case where FONT->min_bounds has invalid
13024 contents. Since the only font known to have invalid min_bounds
13025 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
13026 if (*w <= 0)
13027 *w = font->max_bounds.width;
13028}
13029
13030
13031/* Compute the smallest character width and smallest font height over
13032 all fonts available on frame F. Set the members smallest_char_width
13033 and smallest_font_height in F's x_display_info structure to
13034 the values computed. Value is non-zero if smallest_font_height or
13035 smallest_char_width become smaller than they were before. */
13036
13037static int
13038x_compute_min_glyph_bounds (f)
13039 struct frame *f;
13040{
13041 int i;
13042 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13043 XFontStruct *font;
13044 int old_width = dpyinfo->smallest_char_width;
13045 int old_height = dpyinfo->smallest_font_height;
13046
13047 dpyinfo->smallest_font_height = 100000;
13048 dpyinfo->smallest_char_width = 100000;
13049
13050 for (i = 0; i < dpyinfo->n_fonts; ++i)
13051 if (dpyinfo->font_table[i].name)
13052 {
13053 struct font_info *fontp = dpyinfo->font_table + i;
13054 int w, h;
13055
13056 font = (XFontStruct *) fontp->font;
13057 xassert (font != (XFontStruct *) ~0);
13058 x_font_min_bounds (font, &w, &h);
13059
13060 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
13061 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
13062 }
13063
13064 xassert (dpyinfo->smallest_char_width > 0
13065 && dpyinfo->smallest_font_height > 0);
13066
13067 return (dpyinfo->n_fonts == 1
13068 || dpyinfo->smallest_char_width < old_width
13069 || dpyinfo->smallest_font_height < old_height);
13070}
13071
13072
dc43ef94
KH
13073/* Load font named FONTNAME of the size SIZE for frame F, and return a
13074 pointer to the structure font_info while allocating it dynamically.
13075 If SIZE is 0, load any size of font.
13076 If loading is failed, return NULL. */
13077
13078struct font_info *
13079x_load_font (f, fontname, size)
13080 struct frame *f;
13081 register char *fontname;
13082 int size;
13083{
13084 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13085 Lisp_Object font_names;
d645aaa4 13086 int count;
dc43ef94
KH
13087
13088 /* Get a list of all the fonts that match this name. Once we
13089 have a list of matching fonts, we compare them against the fonts
13090 we already have by comparing names. */
09c6077f 13091 font_names = x_list_fonts (f, build_string (fontname), size, 1);
dc43ef94
KH
13092
13093 if (!NILP (font_names))
13094 {
13095 Lisp_Object tail;
13096 int i;
13097
13098 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 13099 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
06a2c219
GM
13100 if (dpyinfo->font_table[i].name
13101 && (!strcmp (dpyinfo->font_table[i].name,
8e713be6 13102 XSTRING (XCAR (tail))->data)
06a2c219 13103 || !strcmp (dpyinfo->font_table[i].full_name,
8e713be6 13104 XSTRING (XCAR (tail))->data)))
dc43ef94
KH
13105 return (dpyinfo->font_table + i);
13106 }
13107
13108 /* Load the font and add it to the table. */
13109 {
13110 char *full_name;
13111 XFontStruct *font;
13112 struct font_info *fontp;
13113 unsigned long value;
06a2c219 13114 int i;
dc43ef94 13115
2da424f1
KH
13116 /* If we have found fonts by x_list_font, load one of them. If
13117 not, we still try to load a font by the name given as FONTNAME
13118 because XListFonts (called in x_list_font) of some X server has
13119 a bug of not finding a font even if the font surely exists and
13120 is loadable by XLoadQueryFont. */
e1d6d5b9 13121 if (size > 0 && !NILP (font_names))
8e713be6 13122 fontname = (char *) XSTRING (XCAR (font_names))->data;
dc43ef94
KH
13123
13124 BLOCK_INPUT;
d645aaa4 13125 count = x_catch_errors (FRAME_X_DISPLAY (f));
dc43ef94 13126 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
d645aaa4
KH
13127 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
13128 {
13129 /* This error is perhaps due to insufficient memory on X
13130 server. Let's just ignore it. */
13131 font = NULL;
13132 x_clear_errors (FRAME_X_DISPLAY (f));
13133 }
13134 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
dc43ef94 13135 UNBLOCK_INPUT;
b5210ea7 13136 if (!font)
dc43ef94
KH
13137 return NULL;
13138
06a2c219
GM
13139 /* Find a free slot in the font table. */
13140 for (i = 0; i < dpyinfo->n_fonts; ++i)
13141 if (dpyinfo->font_table[i].name == NULL)
13142 break;
13143
13144 /* If no free slot found, maybe enlarge the font table. */
13145 if (i == dpyinfo->n_fonts
13146 && dpyinfo->n_fonts == dpyinfo->font_table_size)
dc43ef94 13147 {
06a2c219
GM
13148 int sz;
13149 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
13150 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
dc43ef94 13151 dpyinfo->font_table
06a2c219 13152 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
dc43ef94
KH
13153 }
13154
06a2c219
GM
13155 fontp = dpyinfo->font_table + i;
13156 if (i == dpyinfo->n_fonts)
13157 ++dpyinfo->n_fonts;
dc43ef94
KH
13158
13159 /* Now fill in the slots of *FONTP. */
13160 BLOCK_INPUT;
13161 fontp->font = font;
06a2c219 13162 fontp->font_idx = i;
dc43ef94
KH
13163 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
13164 bcopy (fontname, fontp->name, strlen (fontname) + 1);
13165
13166 /* Try to get the full name of FONT. Put it in FULL_NAME. */
13167 full_name = 0;
13168 if (XGetFontProperty (font, XA_FONT, &value))
13169 {
13170 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
13171 char *p = name;
13172 int dashes = 0;
13173
13174 /* Count the number of dashes in the "full name".
13175 If it is too few, this isn't really the font's full name,
13176 so don't use it.
13177 In X11R4, the fonts did not come with their canonical names
13178 stored in them. */
13179 while (*p)
13180 {
13181 if (*p == '-')
13182 dashes++;
13183 p++;
13184 }
13185
13186 if (dashes >= 13)
13187 {
13188 full_name = (char *) xmalloc (p - name + 1);
13189 bcopy (name, full_name, p - name + 1);
13190 }
13191
13192 XFree (name);
13193 }
13194
13195 if (full_name != 0)
13196 fontp->full_name = full_name;
13197 else
13198 fontp->full_name = fontp->name;
13199
13200 fontp->size = font->max_bounds.width;
d5749adb
KH
13201 fontp->height = FONT_HEIGHT (font);
13202 {
13203 /* For some font, ascent and descent in max_bounds field is
13204 larger than the above value. */
13205 int max_height = font->max_bounds.ascent + font->max_bounds.descent;
13206 if (max_height > fontp->height)
74848a96 13207 fontp->height = max_height;
d5749adb 13208 }
dc43ef94 13209
2da424f1
KH
13210 if (NILP (font_names))
13211 {
13212 /* We come here because of a bug of XListFonts mentioned at
13213 the head of this block. Let's store this information in
13214 the cache for x_list_fonts. */
13215 Lisp_Object lispy_name = build_string (fontname);
13216 Lisp_Object lispy_full_name = build_string (fontp->full_name);
13217
8e713be6 13218 XCDR (dpyinfo->name_list_element)
2da424f1
KH
13219 = Fcons (Fcons (Fcons (lispy_name, make_number (256)),
13220 Fcons (Fcons (lispy_full_name,
13221 make_number (fontp->size)),
13222 Qnil)),
8e713be6 13223 XCDR (dpyinfo->name_list_element));
2da424f1 13224 if (full_name)
8e713be6 13225 XCDR (dpyinfo->name_list_element)
2da424f1
KH
13226 = Fcons (Fcons (Fcons (lispy_full_name, make_number (256)),
13227 Fcons (Fcons (lispy_full_name,
13228 make_number (fontp->size)),
13229 Qnil)),
8e713be6 13230 XCDR (dpyinfo->name_list_element));
2da424f1
KH
13231 }
13232
dc43ef94
KH
13233 /* The slot `encoding' specifies how to map a character
13234 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
ee569018
KH
13235 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
13236 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
8ff102bd 13237 2:0xA020..0xFF7F). For the moment, we don't know which charset
06a2c219 13238 uses this font. So, we set information in fontp->encoding[1]
8ff102bd
RS
13239 which is never used by any charset. If mapping can't be
13240 decided, set FONT_ENCODING_NOT_DECIDED. */
dc43ef94
KH
13241 fontp->encoding[1]
13242 = (font->max_byte1 == 0
13243 /* 1-byte font */
13244 ? (font->min_char_or_byte2 < 0x80
13245 ? (font->max_char_or_byte2 < 0x80
13246 ? 0 /* 0x20..0x7F */
8ff102bd 13247 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
dc43ef94
KH
13248 : 1) /* 0xA0..0xFF */
13249 /* 2-byte font */
13250 : (font->min_byte1 < 0x80
13251 ? (font->max_byte1 < 0x80
13252 ? (font->min_char_or_byte2 < 0x80
13253 ? (font->max_char_or_byte2 < 0x80
13254 ? 0 /* 0x2020..0x7F7F */
8ff102bd 13255 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
dc43ef94 13256 : 3) /* 0x20A0..0x7FFF */
8ff102bd 13257 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
dc43ef94
KH
13258 : (font->min_char_or_byte2 < 0x80
13259 ? (font->max_char_or_byte2 < 0x80
13260 ? 2 /* 0xA020..0xFF7F */
8ff102bd 13261 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
dc43ef94
KH
13262 : 1))); /* 0xA0A0..0xFFFF */
13263
13264 fontp->baseline_offset
13265 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
13266 ? (long) value : 0);
13267 fontp->relative_compose
13268 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
13269 ? (long) value : 0);
f78798df
KH
13270 fontp->default_ascent
13271 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
13272 ? (long) value : 0);
dc43ef94 13273
06a2c219
GM
13274 /* Set global flag fonts_changed_p to non-zero if the font loaded
13275 has a character with a smaller width than any other character
13276 before, or if the font loaded has a smalle>r height than any
13277 other font loaded before. If this happens, it will make a
13278 glyph matrix reallocation necessary. */
13279 fonts_changed_p = x_compute_min_glyph_bounds (f);
dc43ef94 13280 UNBLOCK_INPUT;
dc43ef94
KH
13281 return fontp;
13282 }
13283}
13284
06a2c219
GM
13285
13286/* Return a pointer to struct font_info of a font named FONTNAME for
13287 frame F. If no such font is loaded, return NULL. */
13288
dc43ef94
KH
13289struct font_info *
13290x_query_font (f, fontname)
13291 struct frame *f;
13292 register char *fontname;
13293{
13294 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13295 int i;
13296
13297 for (i = 0; i < dpyinfo->n_fonts; i++)
06a2c219
GM
13298 if (dpyinfo->font_table[i].name
13299 && (!strcmp (dpyinfo->font_table[i].name, fontname)
13300 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
dc43ef94
KH
13301 return (dpyinfo->font_table + i);
13302 return NULL;
13303}
13304
06a2c219
GM
13305
13306/* Find a CCL program for a font specified by FONTP, and set the member
a6582676
KH
13307 `encoder' of the structure. */
13308
13309void
13310x_find_ccl_program (fontp)
13311 struct font_info *fontp;
13312{
a42f54e6 13313 Lisp_Object list, elt;
a6582676 13314
f9b5db02 13315 elt = Qnil;
8e713be6 13316 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
a6582676 13317 {
8e713be6 13318 elt = XCAR (list);
a6582676 13319 if (CONSP (elt)
8e713be6 13320 && STRINGP (XCAR (elt))
9f2feff6
KH
13321 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
13322 >= 0)
13323 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
13324 >= 0)))
a42f54e6
KH
13325 break;
13326 }
f9b5db02 13327
a42f54e6
KH
13328 if (! NILP (list))
13329 {
d27f8ca7
KH
13330 struct ccl_program *ccl
13331 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
a42f54e6 13332
8e713be6 13333 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
a42f54e6
KH
13334 xfree (ccl);
13335 else
13336 fontp->font_encoder = ccl;
a6582676
KH
13337 }
13338}
13339
06a2c219 13340
dc43ef94 13341\f
06a2c219
GM
13342/***********************************************************************
13343 Initialization
13344 ***********************************************************************/
f451eb13 13345
3afe33e7
RS
13346#ifdef USE_X_TOOLKIT
13347static XrmOptionDescRec emacs_options[] = {
13348 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
13349 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
13350
13351 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
13352 XrmoptionSepArg, NULL},
13353 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
13354
13355 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13356 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13357 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13358 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13359 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13360 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
13361 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
13362};
13363#endif /* USE_X_TOOLKIT */
13364
7a13e894
RS
13365static int x_initialized;
13366
29b38361
KH
13367#ifdef MULTI_KBOARD
13368/* Test whether two display-name strings agree up to the dot that separates
13369 the screen number from the server number. */
13370static int
13371same_x_server (name1, name2)
13372 char *name1, *name2;
13373{
13374 int seen_colon = 0;
cf591cc1
RS
13375 unsigned char *system_name = XSTRING (Vsystem_name)->data;
13376 int system_name_length = strlen (system_name);
13377 int length_until_period = 0;
13378
13379 while (system_name[length_until_period] != 0
13380 && system_name[length_until_period] != '.')
13381 length_until_period++;
13382
13383 /* Treat `unix' like an empty host name. */
13384 if (! strncmp (name1, "unix:", 5))
13385 name1 += 4;
13386 if (! strncmp (name2, "unix:", 5))
13387 name2 += 4;
13388 /* Treat this host's name like an empty host name. */
13389 if (! strncmp (name1, system_name, system_name_length)
13390 && name1[system_name_length] == ':')
13391 name1 += system_name_length;
13392 if (! strncmp (name2, system_name, system_name_length)
13393 && name2[system_name_length] == ':')
13394 name2 += system_name_length;
13395 /* Treat this host's domainless name like an empty host name. */
13396 if (! strncmp (name1, system_name, length_until_period)
13397 && name1[length_until_period] == ':')
13398 name1 += length_until_period;
13399 if (! strncmp (name2, system_name, length_until_period)
13400 && name2[length_until_period] == ':')
13401 name2 += length_until_period;
13402
29b38361
KH
13403 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
13404 {
13405 if (*name1 == ':')
13406 seen_colon++;
13407 if (seen_colon && *name1 == '.')
13408 return 1;
13409 }
13410 return (seen_colon
13411 && (*name1 == '.' || *name1 == '\0')
13412 && (*name2 == '.' || *name2 == '\0'));
13413}
13414#endif
13415
334208b7 13416struct x_display_info *
1f8255f2 13417x_term_init (display_name, xrm_option, resource_name)
334208b7 13418 Lisp_Object display_name;
1f8255f2
RS
13419 char *xrm_option;
13420 char *resource_name;
dc6f92b8 13421{
334208b7 13422 int connection;
7a13e894 13423 Display *dpy;
334208b7
RS
13424 struct x_display_info *dpyinfo;
13425 XrmDatabase xrdb;
13426
60439948
KH
13427 BLOCK_INPUT;
13428
7a13e894
RS
13429 if (!x_initialized)
13430 {
13431 x_initialize ();
13432 x_initialized = 1;
13433 }
dc6f92b8 13434
3afe33e7 13435#ifdef USE_X_TOOLKIT
2d7fc7e8
RS
13436 /* weiner@footloose.sps.mot.com reports that this causes
13437 errors with X11R5:
13438 X protocol error: BadAtom (invalid Atom parameter)
13439 on protocol request 18skiloaf.
13440 So let's not use it until R6. */
13441#ifdef HAVE_X11XTR6
bdcd49ba
RS
13442 XtSetLanguageProc (NULL, NULL, NULL);
13443#endif
13444
7f9c7f94
RS
13445 {
13446 int argc = 0;
13447 char *argv[3];
13448
13449 argv[0] = "";
13450 argc = 1;
13451 if (xrm_option)
13452 {
13453 argv[argc++] = "-xrm";
13454 argv[argc++] = xrm_option;
13455 }
13456 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
13457 resource_name, EMACS_CLASS,
13458 emacs_options, XtNumber (emacs_options),
13459 &argc, argv);
39d8bb4d
KH
13460
13461#ifdef HAVE_X11XTR6
10537cb1 13462 /* I think this is to compensate for XtSetLanguageProc. */
71f8198a 13463 fixup_locale ();
39d8bb4d 13464#endif
7f9c7f94 13465 }
3afe33e7
RS
13466
13467#else /* not USE_X_TOOLKIT */
bdcd49ba
RS
13468#ifdef HAVE_X11R5
13469 XSetLocaleModifiers ("");
13470#endif
7a13e894 13471 dpy = XOpenDisplay (XSTRING (display_name)->data);
3afe33e7 13472#endif /* not USE_X_TOOLKIT */
334208b7 13473
7a13e894
RS
13474 /* Detect failure. */
13475 if (dpy == 0)
60439948
KH
13476 {
13477 UNBLOCK_INPUT;
13478 return 0;
13479 }
7a13e894
RS
13480
13481 /* We have definitely succeeded. Record the new connection. */
13482
13483 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
f04e1297 13484 bzero (dpyinfo, sizeof *dpyinfo);
7a13e894 13485
29b38361
KH
13486#ifdef MULTI_KBOARD
13487 {
13488 struct x_display_info *share;
13489 Lisp_Object tail;
13490
13491 for (share = x_display_list, tail = x_display_name_list; share;
8e713be6
KR
13492 share = share->next, tail = XCDR (tail))
13493 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
29b38361
KH
13494 XSTRING (display_name)->data))
13495 break;
13496 if (share)
13497 dpyinfo->kboard = share->kboard;
13498 else
13499 {
13500 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
13501 init_kboard (dpyinfo->kboard);
59e755be
KH
13502 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
13503 {
13504 char *vendor = ServerVendor (dpy);
9b6ed9f3 13505 UNBLOCK_INPUT;
59e755be
KH
13506 dpyinfo->kboard->Vsystem_key_alist
13507 = call1 (Qvendor_specific_keysyms,
13508 build_string (vendor ? vendor : ""));
9b6ed9f3 13509 BLOCK_INPUT;
59e755be
KH
13510 }
13511
29b38361
KH
13512 dpyinfo->kboard->next_kboard = all_kboards;
13513 all_kboards = dpyinfo->kboard;
0ad5446c
KH
13514 /* Don't let the initial kboard remain current longer than necessary.
13515 That would cause problems if a file loaded on startup tries to
06a2c219 13516 prompt in the mini-buffer. */
0ad5446c
KH
13517 if (current_kboard == initial_kboard)
13518 current_kboard = dpyinfo->kboard;
29b38361
KH
13519 }
13520 dpyinfo->kboard->reference_count++;
13521 }
b9737ad3
KH
13522#endif
13523
7a13e894
RS
13524 /* Put this display on the chain. */
13525 dpyinfo->next = x_display_list;
13526 x_display_list = dpyinfo;
13527
13528 /* Put it on x_display_name_list as well, to keep them parallel. */
13529 x_display_name_list = Fcons (Fcons (display_name, Qnil),
13530 x_display_name_list);
8e713be6 13531 dpyinfo->name_list_element = XCAR (x_display_name_list);
7a13e894
RS
13532
13533 dpyinfo->display = dpy;
dc6f92b8 13534
dc6f92b8 13535#if 0
7a13e894 13536 XSetAfterFunction (x_current_display, x_trace_wire);
c118dd06 13537#endif /* ! 0 */
7a13e894
RS
13538
13539 dpyinfo->x_id_name
fc932ac6
RS
13540 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
13541 + STRING_BYTES (XSTRING (Vsystem_name))
7a13e894
RS
13542 + 2);
13543 sprintf (dpyinfo->x_id_name, "%s@%s",
13544 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
28430d3c
JB
13545
13546 /* Figure out which modifier bits mean what. */
334208b7 13547 x_find_modifier_meanings (dpyinfo);
f451eb13 13548
ab648270 13549 /* Get the scroll bar cursor. */
7a13e894 13550 dpyinfo->vertical_scroll_bar_cursor
334208b7 13551 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
f451eb13 13552
334208b7
RS
13553 xrdb = x_load_resources (dpyinfo->display, xrm_option,
13554 resource_name, EMACS_CLASS);
13555#ifdef HAVE_XRMSETDATABASE
13556 XrmSetDatabase (dpyinfo->display, xrdb);
13557#else
13558 dpyinfo->display->db = xrdb;
13559#endif
547d9db8 13560 /* Put the rdb where we can find it in a way that works on
7a13e894
RS
13561 all versions. */
13562 dpyinfo->xrdb = xrdb;
334208b7
RS
13563
13564 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
13565 DefaultScreen (dpyinfo->display));
5ff67d81 13566 select_visual (dpyinfo);
43bd1b2b 13567 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
334208b7
RS
13568 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
13569 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
13570 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
13571 dpyinfo->grabbed = 0;
13572 dpyinfo->reference_count = 0;
13573 dpyinfo->icon_bitmap_id = -1;
06a2c219 13574 dpyinfo->font_table = NULL;
7a13e894
RS
13575 dpyinfo->n_fonts = 0;
13576 dpyinfo->font_table_size = 0;
13577 dpyinfo->bitmaps = 0;
13578 dpyinfo->bitmaps_size = 0;
13579 dpyinfo->bitmaps_last = 0;
13580 dpyinfo->scratch_cursor_gc = 0;
13581 dpyinfo->mouse_face_mouse_frame = 0;
13582 dpyinfo->mouse_face_deferred_gc = 0;
13583 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
13584 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
06a2c219 13585 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
7a13e894
RS
13586 dpyinfo->mouse_face_window = Qnil;
13587 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
13588 dpyinfo->mouse_face_defer = 0;
0f941935
KH
13589 dpyinfo->x_focus_frame = 0;
13590 dpyinfo->x_focus_event_frame = 0;
13591 dpyinfo->x_highlight_frame = 0;
06a2c219 13592 dpyinfo->image_cache = make_image_cache ();
334208b7 13593
43bd1b2b 13594 /* See if a private colormap is requested. */
5ff67d81
GM
13595 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
13596 {
13597 if (dpyinfo->visual->class == PseudoColor)
13598 {
13599 Lisp_Object value;
13600 value = display_x_get_resource (dpyinfo,
13601 build_string ("privateColormap"),
13602 build_string ("PrivateColormap"),
13603 Qnil, Qnil);
13604 if (STRINGP (value)
13605 && (!strcmp (XSTRING (value)->data, "true")
13606 || !strcmp (XSTRING (value)->data, "on")))
13607 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
13608 }
43bd1b2b 13609 }
5ff67d81
GM
13610 else
13611 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
13612 dpyinfo->visual, AllocNone);
43bd1b2b 13613
06a2c219
GM
13614 {
13615 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
13616 double pixels = DisplayHeight (dpyinfo->display, screen_number);
13617 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
13618 dpyinfo->resy = pixels * 25.4 / mm;
13619 pixels = DisplayWidth (dpyinfo->display, screen_number);
13620 mm = DisplayWidthMM (dpyinfo->display, screen_number);
13621 dpyinfo->resx = pixels * 25.4 / mm;
13622 }
13623
334208b7
RS
13624 dpyinfo->Xatom_wm_protocols
13625 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
13626 dpyinfo->Xatom_wm_take_focus
13627 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
13628 dpyinfo->Xatom_wm_save_yourself
13629 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
13630 dpyinfo->Xatom_wm_delete_window
13631 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
13632 dpyinfo->Xatom_wm_change_state
13633 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
13634 dpyinfo->Xatom_wm_configure_denied
13635 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
13636 dpyinfo->Xatom_wm_window_moved
13637 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
13638 dpyinfo->Xatom_editres
13639 = XInternAtom (dpyinfo->display, "Editres", False);
13640 dpyinfo->Xatom_CLIPBOARD
13641 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
13642 dpyinfo->Xatom_TIMESTAMP
13643 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
13644 dpyinfo->Xatom_TEXT
13645 = XInternAtom (dpyinfo->display, "TEXT", False);
dc43ef94
KH
13646 dpyinfo->Xatom_COMPOUND_TEXT
13647 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
334208b7
RS
13648 dpyinfo->Xatom_DELETE
13649 = XInternAtom (dpyinfo->display, "DELETE", False);
13650 dpyinfo->Xatom_MULTIPLE
13651 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
13652 dpyinfo->Xatom_INCR
13653 = XInternAtom (dpyinfo->display, "INCR", False);
13654 dpyinfo->Xatom_EMACS_TMP
13655 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
13656 dpyinfo->Xatom_TARGETS
13657 = XInternAtom (dpyinfo->display, "TARGETS", False);
13658 dpyinfo->Xatom_NULL
13659 = XInternAtom (dpyinfo->display, "NULL", False);
13660 dpyinfo->Xatom_ATOM_PAIR
13661 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
dc43ef94
KH
13662 /* For properties of font. */
13663 dpyinfo->Xatom_PIXEL_SIZE
13664 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
13665 dpyinfo->Xatom_MULE_BASELINE_OFFSET
13666 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
13667 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
13668 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
f78798df
KH
13669 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
13670 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
334208b7 13671
06a2c219
GM
13672 /* Ghostscript support. */
13673 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
13674 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
13675
13676 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
13677 False);
13678
547d9db8
KH
13679 dpyinfo->cut_buffers_initialized = 0;
13680
334208b7
RS
13681 connection = ConnectionNumber (dpyinfo->display);
13682 dpyinfo->connection = connection;
13683
dc43ef94 13684 {
5d7cc324
RS
13685 char null_bits[1];
13686
13687 null_bits[0] = 0x00;
dc43ef94
KH
13688
13689 dpyinfo->null_pixel
13690 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13691 null_bits, 1, 1, (long) 0, (long) 0,
13692 1);
13693 }
13694
06a2c219
GM
13695 {
13696 extern int gray_bitmap_width, gray_bitmap_height;
13697 extern unsigned char *gray_bitmap_bits;
13698 dpyinfo->gray
13699 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13700 gray_bitmap_bits,
13701 gray_bitmap_width, gray_bitmap_height,
13702 (unsigned long) 1, (unsigned long) 0, 1);
13703 }
13704
f5d11644
GM
13705#ifdef HAVE_X_I18N
13706 xim_initialize (dpyinfo, resource_name);
13707#endif
13708
87485d6f
MW
13709#ifdef subprocesses
13710 /* This is only needed for distinguishing keyboard and process input. */
334208b7 13711 if (connection != 0)
7a13e894 13712 add_keyboard_wait_descriptor (connection);
87485d6f 13713#endif
6d4238f3 13714
041b69ac 13715#ifndef F_SETOWN_BUG
dc6f92b8 13716#ifdef F_SETOWN
dc6f92b8 13717#ifdef F_SETOWN_SOCK_NEG
61c3ce62 13718 /* stdin is a socket here */
334208b7 13719 fcntl (connection, F_SETOWN, -getpid ());
c118dd06 13720#else /* ! defined (F_SETOWN_SOCK_NEG) */
334208b7 13721 fcntl (connection, F_SETOWN, getpid ());
c118dd06
JB
13722#endif /* ! defined (F_SETOWN_SOCK_NEG) */
13723#endif /* ! defined (F_SETOWN) */
041b69ac 13724#endif /* F_SETOWN_BUG */
dc6f92b8
JB
13725
13726#ifdef SIGIO
eee20f6a
KH
13727 if (interrupt_input)
13728 init_sigio (connection);
c118dd06 13729#endif /* ! defined (SIGIO) */
dc6f92b8 13730
51b592fb 13731#ifdef USE_LUCID
f8c39f51 13732#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
51b592fb
RS
13733 /* Make sure that we have a valid font for dialog boxes
13734 so that Xt does not crash. */
13735 {
13736 Display *dpy = dpyinfo->display;
13737 XrmValue d, fr, to;
13738 Font font;
e99db5a1 13739 int count;
51b592fb
RS
13740
13741 d.addr = (XPointer)&dpy;
13742 d.size = sizeof (Display *);
13743 fr.addr = XtDefaultFont;
13744 fr.size = sizeof (XtDefaultFont);
13745 to.size = sizeof (Font *);
13746 to.addr = (XPointer)&font;
e99db5a1 13747 count = x_catch_errors (dpy);
51b592fb
RS
13748 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
13749 abort ();
13750 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
13751 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
e99db5a1 13752 x_uncatch_errors (dpy, count);
51b592fb
RS
13753 }
13754#endif
f8c39f51 13755#endif
51b592fb 13756
34e23e5a
GM
13757 /* See if we should run in synchronous mode. This is useful
13758 for debugging X code. */
13759 {
13760 Lisp_Object value;
13761 value = display_x_get_resource (dpyinfo,
13762 build_string ("synchronous"),
13763 build_string ("Synchronous"),
13764 Qnil, Qnil);
13765 if (STRINGP (value)
13766 && (!strcmp (XSTRING (value)->data, "true")
13767 || !strcmp (XSTRING (value)->data, "on")))
13768 XSynchronize (dpyinfo->display, True);
13769 }
13770
60439948
KH
13771 UNBLOCK_INPUT;
13772
7a13e894
RS
13773 return dpyinfo;
13774}
13775\f
13776/* Get rid of display DPYINFO, assuming all frames are already gone,
13777 and without sending any more commands to the X server. */
dc6f92b8 13778
7a13e894
RS
13779void
13780x_delete_display (dpyinfo)
13781 struct x_display_info *dpyinfo;
13782{
13783 delete_keyboard_wait_descriptor (dpyinfo->connection);
13784
13785 /* Discard this display from x_display_name_list and x_display_list.
13786 We can't use Fdelq because that can quit. */
13787 if (! NILP (x_display_name_list)
8e713be6
KR
13788 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
13789 x_display_name_list = XCDR (x_display_name_list);
7a13e894
RS
13790 else
13791 {
13792 Lisp_Object tail;
13793
13794 tail = x_display_name_list;
8e713be6 13795 while (CONSP (tail) && CONSP (XCDR (tail)))
7a13e894 13796 {
bffcfca9 13797 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
7a13e894 13798 {
8e713be6 13799 XCDR (tail) = XCDR (XCDR (tail));
7a13e894
RS
13800 break;
13801 }
8e713be6 13802 tail = XCDR (tail);
7a13e894
RS
13803 }
13804 }
13805
9bda743f
GM
13806 if (next_noop_dpyinfo == dpyinfo)
13807 next_noop_dpyinfo = dpyinfo->next;
13808
7a13e894
RS
13809 if (x_display_list == dpyinfo)
13810 x_display_list = dpyinfo->next;
7f9c7f94
RS
13811 else
13812 {
13813 struct x_display_info *tail;
7a13e894 13814
7f9c7f94
RS
13815 for (tail = x_display_list; tail; tail = tail->next)
13816 if (tail->next == dpyinfo)
13817 tail->next = tail->next->next;
13818 }
7a13e894 13819
0d777288
RS
13820#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
13821#ifndef AIX /* On AIX, XCloseDisplay calls this. */
7f9c7f94
RS
13822 XrmDestroyDatabase (dpyinfo->xrdb);
13823#endif
0d777288 13824#endif
29b38361
KH
13825#ifdef MULTI_KBOARD
13826 if (--dpyinfo->kboard->reference_count == 0)
39f79001 13827 delete_kboard (dpyinfo->kboard);
b9737ad3 13828#endif
f5d11644
GM
13829#ifdef HAVE_X_I18N
13830 if (dpyinfo->xim)
13831 xim_close_dpy (dpyinfo);
13832#endif
13833
b9737ad3
KH
13834 xfree (dpyinfo->font_table);
13835 xfree (dpyinfo->x_id_name);
f04e1297 13836 xfree (dpyinfo->color_cells);
b9737ad3 13837 xfree (dpyinfo);
7a13e894 13838}
f04e1297 13839
7a13e894
RS
13840\f
13841/* Set up use of X before we make the first connection. */
13842
06a2c219
GM
13843static struct redisplay_interface x_redisplay_interface =
13844{
13845 x_produce_glyphs,
13846 x_write_glyphs,
13847 x_insert_glyphs,
13848 x_clear_end_of_line,
13849 x_scroll_run,
13850 x_after_update_window_line,
13851 x_update_window_begin,
13852 x_update_window_end,
13853 XTcursor_to,
13854 x_flush,
71b8321e 13855 x_clear_mouse_face,
66ac4b0e
GM
13856 x_get_glyph_overhangs,
13857 x_fix_overlapping_area
06a2c219
GM
13858};
13859
dfcf069d 13860void
7a13e894
RS
13861x_initialize ()
13862{
06a2c219
GM
13863 rif = &x_redisplay_interface;
13864
13865 clear_frame_hook = x_clear_frame;
13866 ins_del_lines_hook = x_ins_del_lines;
13867 change_line_highlight_hook = x_change_line_highlight;
13868 delete_glyphs_hook = x_delete_glyphs;
dc6f92b8
JB
13869 ring_bell_hook = XTring_bell;
13870 reset_terminal_modes_hook = XTreset_terminal_modes;
13871 set_terminal_modes_hook = XTset_terminal_modes;
06a2c219
GM
13872 update_begin_hook = x_update_begin;
13873 update_end_hook = x_update_end;
dc6f92b8
JB
13874 set_terminal_window_hook = XTset_terminal_window;
13875 read_socket_hook = XTread_socket;
b8009dd1 13876 frame_up_to_date_hook = XTframe_up_to_date;
dc6f92b8 13877 reassert_line_highlight_hook = XTreassert_line_highlight;
90e65f07 13878 mouse_position_hook = XTmouse_position;
f451eb13 13879 frame_rehighlight_hook = XTframe_rehighlight;
dbc4e1c1 13880 frame_raise_lower_hook = XTframe_raise_lower;
ab648270
JB
13881 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13882 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13883 redeem_scroll_bar_hook = XTredeem_scroll_bar;
13884 judge_scroll_bars_hook = XTjudge_scroll_bars;
06a2c219 13885 estimate_mode_line_height_hook = x_estimate_mode_line_height;
58769bee 13886
f676886a 13887 scroll_region_ok = 1; /* we'll scroll partial frames */
dc6f92b8
JB
13888 char_ins_del_ok = 0; /* just as fast to write the line */
13889 line_ins_del_ok = 1; /* we'll just blt 'em */
13890 fast_clear_end_of_line = 1; /* X does this well */
58769bee 13891 memory_below_frame = 0; /* we don't remember what scrolls
dc6f92b8
JB
13892 off the bottom */
13893 baud_rate = 19200;
13894
7a13e894 13895 x_noop_count = 0;
9ea173e8 13896 last_tool_bar_item = -1;
06a2c219
GM
13897 any_help_event_p = 0;
13898
b30b24cb
RS
13899 /* Try to use interrupt input; if we can't, then start polling. */
13900 Fset_input_mode (Qt, Qnil, Qt, Qnil);
13901
7f9c7f94
RS
13902#ifdef USE_X_TOOLKIT
13903 XtToolkitInitialize ();
13904 Xt_app_con = XtCreateApplicationContext ();
665881ad 13905 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
bffcfca9
GM
13906
13907 /* Install an asynchronous timer that processes Xt timeout events
13908 every 0.1s. This is necessary because some widget sets use
13909 timeouts internally, for example the LessTif menu bar, or the
13910 Xaw3d scroll bar. When Xt timouts aren't processed, these
13911 widgets don't behave normally. */
13912 {
13913 EMACS_TIME interval;
13914 EMACS_SET_SECS_USECS (interval, 0, 100000);
13915 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
13916 }
db74249b 13917#endif
bffcfca9 13918
eccc05db 13919#ifdef USE_TOOLKIT_SCROLL_BARS
ec18280f
SM
13920 xaw3d_arrow_scroll = False;
13921 xaw3d_pick_top = True;
7f9c7f94
RS
13922#endif
13923
58769bee 13924 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 13925 original error handler. */
e99db5a1 13926 XSetErrorHandler (x_error_handler);
334208b7 13927 XSetIOErrorHandler (x_io_error_quitter);
dc6f92b8 13928
06a2c219 13929 /* Disable Window Change signals; they are handled by X events. */
dc6f92b8
JB
13930#ifdef SIGWINCH
13931 signal (SIGWINCH, SIG_DFL);
c118dd06 13932#endif /* ! defined (SIGWINCH) */
dc6f92b8 13933
92e2441b 13934 signal (SIGPIPE, x_connection_signal);
dc6f92b8 13935}
55123275 13936
06a2c219 13937
55123275
JB
13938void
13939syms_of_xterm ()
13940{
e99db5a1
RS
13941 staticpro (&x_error_message_string);
13942 x_error_message_string = Qnil;
13943
7a13e894
RS
13944 staticpro (&x_display_name_list);
13945 x_display_name_list = Qnil;
334208b7 13946
ab648270 13947 staticpro (&last_mouse_scroll_bar);
e53cb100 13948 last_mouse_scroll_bar = Qnil;
59e755be
KH
13949
13950 staticpro (&Qvendor_specific_keysyms);
13951 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
2237cac9
RS
13952
13953 staticpro (&last_mouse_press_frame);
13954 last_mouse_press_frame = Qnil;
06a2c219 13955
06a2c219 13956 help_echo = Qnil;
be010514
GM
13957 staticpro (&help_echo);
13958 help_echo_object = Qnil;
13959 staticpro (&help_echo_object);
7cea38bc
GM
13960 help_echo_window = Qnil;
13961 staticpro (&help_echo_window);
06a2c219 13962 previous_help_echo = Qnil;
be010514
GM
13963 staticpro (&previous_help_echo);
13964 help_echo_pos = -1;
06a2c219
GM
13965
13966 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
13967 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
13968For example, if a block cursor is over a tab, it will be drawn as\n\
13969wide as that tab on the display.");
13970 x_stretch_cursor_p = 0;
13971
13972 DEFVAR_BOOL ("x-toolkit-scroll-bars-p", &x_toolkit_scroll_bars_p,
13973 "If not nil, Emacs uses toolkit scroll bars.");
eccc05db 13974#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
13975 x_toolkit_scroll_bars_p = 1;
13976#else
13977 x_toolkit_scroll_bars_p = 0;
13978#endif
13979
06a2c219
GM
13980 staticpro (&last_mouse_motion_frame);
13981 last_mouse_motion_frame = Qnil;
55123275 13982}
6cf0ae86
RS
13983
13984#endif /* not HAVE_X_WINDOWS */