(Info-fontify-node): Mark one more char as intangible.
[bpt/emacs.git] / src / xterm.c
CommitLineData
dc6f92b8 1/* X Communication module for terminals which understand the X protocol.
edf36fe6 2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000, 2001
06a2c219 3 Free Software Foundation, Inc.
dc6f92b8
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
4746118a 9the Free Software Foundation; either version 2, or (at your option)
dc6f92b8
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
dc6f92b8 21
06a2c219 22/* New display code by Gerd Moellmann <gerd@gnu.org>. */
3afe33e7
RS
23/* Xt features made by Fred Pierresteguy. */
24
68c45bf0
PE
25#include <config.h>
26
039440c4 27/* On 4.3 these lose if they come after xterm.h. */
039440c4 28/* Putting these at the beginning seems to be standard for other .c files. */
039440c4
RS
29#include <signal.h>
30
4846819e
RS
31#include <stdio.h>
32
dc6f92b8
JB
33#ifdef HAVE_X_WINDOWS
34
35#include "lisp.h"
9ac0d9e0 36#include "blockinput.h"
dc6f92b8 37
ae79c227
AS
38/* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40#include "syssignal.h"
41
dc6f92b8
JB
42/* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44#include "xterm.h"
f451eb13 45#include <X11/cursorfont.h>
dc6f92b8
JB
46
47/* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49#ifndef makedev
50#include <sys/types.h>
c118dd06 51#endif /* makedev */
dc6f92b8 52
6df54671 53#ifdef BSD_SYSTEM
dc6f92b8 54#include <sys/ioctl.h>
6df54671 55#endif /* ! defined (BSD_SYSTEM) */
dc6f92b8 56
2d368234 57#include "systty.h"
3a2712f9 58#include "systime.h"
dc6f92b8 59
b8009dd1 60#ifndef INCLUDED_FCNTL
dc6f92b8 61#include <fcntl.h>
b8009dd1 62#endif
dc6f92b8
JB
63#include <ctype.h>
64#include <errno.h>
65#include <setjmp.h>
66#include <sys/stat.h>
a0a7635f
RS
67/* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
68/* #include <sys/param.h> */
dc6f92b8 69
dc43ef94 70#include "charset.h"
379b5ac0 71#include "coding.h"
dc43ef94 72#include "ccl.h"
7a13e894 73#include "frame.h"
dc6f92b8 74#include "dispextern.h"
ee569018 75#include "fontset.h"
dc6f92b8
JB
76#include "termhooks.h"
77#include "termopts.h"
78#include "termchar.h"
79#if 0
80#include "sink.h"
81#include "sinkmask.h"
c118dd06 82#endif /* ! 0 */
dc6f92b8 83#include "gnu.h"
dc6f92b8 84#include "disptab.h"
dc6f92b8 85#include "buffer.h"
f451eb13 86#include "window.h"
3b2fa4e6 87#include "keyboard.h"
bde7c500 88#include "intervals.h"
dfcf069d 89#include "process.h"
bffcfca9 90#include "atimer.h"
dc6f92b8 91
d2bd6bc4
RS
92#ifdef USE_X_TOOLKIT
93#include <X11/Shell.h>
94#endif
95
06a2c219
GM
96#ifdef HAVE_SYS_TIME_H
97#include <sys/time.h>
98#endif
99#ifdef HAVE_UNISTD_H
100#include <unistd.h>
101#endif
102
3afe33e7 103#ifdef USE_X_TOOLKIT
06a2c219 104
952291d9
GM
105extern void free_frame_menubar P_ ((struct frame *));
106extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
107 int));
06a2c219 108
0fdff6bb
RS
109#if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
110#define HACK_EDITRES
111extern void _XEditResCheckMessages ();
112#endif /* not NO_EDITRES */
06a2c219
GM
113
114/* Include toolkit specific headers for the scroll bar widget. */
115
116#ifdef USE_TOOLKIT_SCROLL_BARS
117#if defined USE_MOTIF
118#include <Xm/Xm.h> /* for LESSTIF_VERSION */
119#include <Xm/ScrollBar.h>
120#include <Xm/ScrollBarP.h>
ec18280f
SM
121#else /* !USE_MOTIF i.e. use Xaw */
122
123#ifdef HAVE_XAW3D
06a2c219 124#include <X11/Xaw3d/Simple.h>
06a2c219
GM
125#include <X11/Xaw3d/Scrollbar.h>
126#define ARROW_SCROLLBAR
127#include <X11/Xaw3d/ScrollbarP.h>
ec18280f
SM
128#else /* !HAVE_XAW3D */
129#include <X11/Xaw/Simple.h>
130#include <X11/Xaw/Scrollbar.h>
131#endif /* !HAVE_XAW3D */
132#ifndef XtNpickTop
133#define XtNpickTop "pickTop"
134#endif /* !XtNpickTop */
135#endif /* !USE_MOTIF */
06a2c219
GM
136#endif /* USE_TOOLKIT_SCROLL_BARS */
137
3afe33e7
RS
138#endif /* USE_X_TOOLKIT */
139
b849c413
RS
140#ifndef USE_X_TOOLKIT
141#define x_any_window_to_frame x_window_to_frame
5627c40e 142#define x_top_window_to_frame x_window_to_frame
b849c413
RS
143#endif
144
546e6d5b 145#ifdef USE_X_TOOLKIT
d067ea8b 146#include "widget.h"
546e6d5b
RS
147#ifndef XtNinitialState
148#define XtNinitialState "initialState"
149#endif
150#endif
151
e4b68333 152#ifndef min
06a2c219 153#define min(a,b) ((a) < (b) ? (a) : (b))
e4b68333
RS
154#endif
155#ifndef max
06a2c219
GM
156#define max(a,b) ((a) > (b) ? (a) : (b))
157#endif
158
159#define abs(x) ((x) < 0 ? -(x) : (x))
160
161#define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
162
163\f
164/* Bitmaps for truncated lines. */
165
166enum bitmap_type
167{
168 NO_BITMAP,
169 LEFT_TRUNCATION_BITMAP,
170 RIGHT_TRUNCATION_BITMAP,
171 OVERLAY_ARROW_BITMAP,
172 CONTINUED_LINE_BITMAP,
173 CONTINUATION_LINE_BITMAP,
174 ZV_LINE_BITMAP
175};
176
177/* Bitmap drawn to indicate lines not displaying text if
178 `indicate-empty-lines' is non-nil. */
179
180#define zv_width 8
181#define zv_height 8
182static unsigned char zv_bits[] = {
183 0x00, 0x00, 0x1e, 0x1e, 0x1e, 0x1e, 0x00, 0x00};
184
185/* An arrow like this: `<-'. */
186
187#define left_width 8
188#define left_height 8
189static unsigned char left_bits[] = {
190 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
191
110859fc
GM
192/* Right truncation arrow bitmap `->'. */
193
194#define right_width 8
195#define right_height 8
196static unsigned char right_bits[] = {
197 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
198
06a2c219
GM
199/* Marker for continued lines. */
200
201#define continued_width 8
202#define continued_height 8
203static unsigned char continued_bits[] = {
110859fc
GM
204 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
205
206/* Marker for continuation lines. */
06a2c219
GM
207
208#define continuation_width 8
209#define continuation_height 8
210static unsigned char continuation_bits[] = {
110859fc 211 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
06a2c219 212
110859fc 213/* Overlay arrow bitmap. */
06a2c219 214
110859fc
GM
215#if 0
216/* A bomb. */
06a2c219
GM
217#define ov_width 8
218#define ov_height 8
219static unsigned char ov_bits[] = {
220 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
06a2c219 221#else
110859fc 222/* A triangular arrow. */
06a2c219
GM
223#define ov_width 8
224#define ov_height 8
225static unsigned char ov_bits[] = {
110859fc
GM
226 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
227
e4b68333 228#endif
06a2c219
GM
229
230extern Lisp_Object Qhelp_echo;
231
69388238 232\f
5bf04520 233/* Non-nil means Emacs uses toolkit scroll bars. */
06a2c219 234
5bf04520 235Lisp_Object Vx_toolkit_scroll_bars;
06a2c219
GM
236
237/* If a string, XTread_socket generates an event to display that string.
238 (The display is done in read_char.) */
239
240static Lisp_Object help_echo;
7cea38bc 241static Lisp_Object help_echo_window;
be010514
GM
242static Lisp_Object help_echo_object;
243static int help_echo_pos;
06a2c219
GM
244
245/* Temporary variable for XTread_socket. */
246
247static Lisp_Object previous_help_echo;
248
249/* Non-zero means that a HELP_EVENT has been generated since Emacs
250 start. */
251
252static int any_help_event_p;
253
254/* Non-zero means draw block and hollow cursor as wide as the glyph
255 under it. For example, if a block cursor is over a tab, it will be
256 drawn as wide as that tab on the display. */
257
258int x_stretch_cursor_p;
259
260/* This is a chain of structures for all the X displays currently in
261 use. */
262
334208b7 263struct x_display_info *x_display_list;
dc6f92b8 264
06a2c219
GM
265/* This is a list of cons cells, each of the form (NAME
266 . FONT-LIST-CACHE), one for each element of x_display_list and in
267 the same order. NAME is the name of the frame. FONT-LIST-CACHE
268 records previous values returned by x-list-fonts. */
269
7a13e894 270Lisp_Object x_display_name_list;
f451eb13 271
987d2ad1 272/* Frame being updated by update_frame. This is declared in term.c.
06a2c219
GM
273 This is set by update_begin and looked at by all the XT functions.
274 It is zero while not inside an update. In that case, the XT
275 functions assume that `selected_frame' is the frame to apply to. */
276
d0386f2a 277extern struct frame *updating_frame;
dc6f92b8 278
dfcf069d 279extern int waiting_for_input;
0e81d8cd 280
06a2c219
GM
281/* This is a frame waiting to be auto-raised, within XTread_socket. */
282
0134a210
RS
283struct frame *pending_autoraise_frame;
284
7f9c7f94
RS
285#ifdef USE_X_TOOLKIT
286/* The application context for Xt use. */
287XtAppContext Xt_app_con;
06a2c219
GM
288static String Xt_default_resources[] = {0};
289#endif /* USE_X_TOOLKIT */
665881ad 290
06a2c219
GM
291/* Nominal cursor position -- where to draw output.
292 HPOS and VPOS are window relative glyph matrix coordinates.
293 X and Y are window relative pixel coordinates. */
dc6f92b8 294
06a2c219 295struct cursor_pos output_cursor;
dc6f92b8 296
bffcfca9
GM
297/* Non-zero means user is interacting with a toolkit scroll bar. */
298
299static int toolkit_scroll_bar_interaction;
dc6f92b8 300
69388238
RS
301/* Mouse movement.
302
06a2c219 303 Formerly, we used PointerMotionHintMask (in standard_event_mask)
f5bb65ec
RS
304 so that we would have to call XQueryPointer after each MotionNotify
305 event to ask for another such event. However, this made mouse tracking
306 slow, and there was a bug that made it eventually stop.
307
308 Simply asking for MotionNotify all the time seems to work better.
309
69388238
RS
310 In order to avoid asking for motion events and then throwing most
311 of them away or busy-polling the server for mouse positions, we ask
312 the server for pointer motion hints. This means that we get only
313 one event per group of mouse movements. "Groups" are delimited by
314 other kinds of events (focus changes and button clicks, for
315 example), or by XQueryPointer calls; when one of these happens, we
316 get another MotionNotify event the next time the mouse moves. This
317 is at least as efficient as getting motion events when mouse
318 tracking is on, and I suspect only negligibly worse when tracking
f5bb65ec 319 is off. */
69388238
RS
320
321/* Where the mouse was last time we reported a mouse event. */
69388238 322
06a2c219
GM
323FRAME_PTR last_mouse_frame;
324static XRectangle last_mouse_glyph;
2237cac9
RS
325static Lisp_Object last_mouse_press_frame;
326
69388238
RS
327/* The scroll bar in which the last X motion event occurred.
328
06a2c219
GM
329 If the last X motion event occurred in a scroll bar, we set this so
330 XTmouse_position can know whether to report a scroll bar motion or
69388238
RS
331 an ordinary motion.
332
06a2c219
GM
333 If the last X motion event didn't occur in a scroll bar, we set
334 this to Qnil, to tell XTmouse_position to return an ordinary motion
335 event. */
336
69388238
RS
337static Lisp_Object last_mouse_scroll_bar;
338
69388238
RS
339/* This is a hack. We would really prefer that XTmouse_position would
340 return the time associated with the position it returns, but there
06a2c219 341 doesn't seem to be any way to wrest the time-stamp from the server
69388238
RS
342 along with the position query. So, we just keep track of the time
343 of the last movement we received, and return that in hopes that
344 it's somewhat accurate. */
06a2c219 345
69388238
RS
346static Time last_mouse_movement_time;
347
06a2c219
GM
348/* Incremented by XTread_socket whenever it really tries to read
349 events. */
350
c0a04927
RS
351#ifdef __STDC__
352static int volatile input_signal_count;
353#else
354static int input_signal_count;
355#endif
356
7a13e894 357/* Used locally within XTread_socket. */
06a2c219 358
7a13e894 359static int x_noop_count;
dc6f92b8 360
7a13e894 361/* Initial values of argv and argc. */
06a2c219 362
7a13e894
RS
363extern char **initial_argv;
364extern int initial_argc;
dc6f92b8 365
7a13e894 366extern Lisp_Object Vcommand_line_args, Vsystem_name;
dc6f92b8 367
06a2c219 368/* Tells if a window manager is present or not. */
7a13e894
RS
369
370extern Lisp_Object Vx_no_window_manager;
dc6f92b8 371
c2df547c 372extern Lisp_Object Qface, Qmouse_face;
b8009dd1 373
dc6f92b8
JB
374extern int errno;
375
dfeccd2d 376/* A mask of extra modifier bits to put into every keyboard char. */
06a2c219 377
64bb1782
RS
378extern int extra_keyboard_modifiers;
379
59e755be
KH
380static Lisp_Object Qvendor_specific_keysyms;
381
952291d9
GM
382extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
383extern Lisp_Object x_icon_type P_ ((struct frame *));
c32cdd9a 384
7a13e894 385
06a2c219
GM
386/* Enumeration for overriding/changing the face to use for drawing
387 glyphs in x_draw_glyphs. */
388
389enum draw_glyphs_face
390{
391 DRAW_NORMAL_TEXT,
392 DRAW_INVERSE_VIDEO,
393 DRAW_CURSOR,
394 DRAW_MOUSE_FACE,
395 DRAW_IMAGE_RAISED,
396 DRAW_IMAGE_SUNKEN
397};
398
f04e1297 399static const XColor *x_color_cells P_ ((struct frame *, int *));
71b8321e 400static void x_update_window_end P_ ((struct window *, int, int));
06a2c219
GM
401static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
402void x_delete_display P_ ((struct x_display_info *));
403static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
404 unsigned));
405static int fast_find_position P_ ((struct window *, int, int *, int *,
406 int *, int *));
407static void set_output_cursor P_ ((struct cursor_pos *));
408static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
409 int *, int *, int *));
410static void note_mode_line_highlight P_ ((struct window *, int, int));
06a2c219 411static void note_mouse_highlight P_ ((struct frame *, int, int));
9ea173e8
GM
412static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
413static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
06a2c219
GM
414static void show_mouse_face P_ ((struct x_display_info *,
415 enum draw_glyphs_face));
416static int x_io_error_quitter P_ ((Display *));
417int x_catch_errors P_ ((Display *));
418void x_uncatch_errors P_ ((Display *, int));
419void x_lower_frame P_ ((struct frame *));
420void x_scroll_bar_clear P_ ((struct frame *));
421int x_had_errors_p P_ ((Display *));
422void x_wm_set_size_hint P_ ((struct frame *, long, int));
423void x_raise_frame P_ ((struct frame *));
424void x_set_window_size P_ ((struct frame *, int, int, int));
425void x_wm_set_window_state P_ ((struct frame *, int));
426void x_wm_set_icon_pixmap P_ ((struct frame *, int));
427void x_initialize P_ ((void));
428static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
429static int x_compute_min_glyph_bounds P_ ((struct frame *));
430static void x_draw_phys_cursor_glyph P_ ((struct window *,
431 struct glyph_row *,
432 enum draw_glyphs_face));
433static void x_update_end P_ ((struct frame *));
434static void XTframe_up_to_date P_ ((struct frame *));
435static void XTreassert_line_highlight P_ ((int, int));
436static void x_change_line_highlight P_ ((int, int, int, int));
437static void XTset_terminal_modes P_ ((void));
438static void XTreset_terminal_modes P_ ((void));
439static void XTcursor_to P_ ((int, int, int, int));
440static void x_write_glyphs P_ ((struct glyph *, int));
441static void x_clear_end_of_line P_ ((int));
442static void x_clear_frame P_ ((void));
443static void x_clear_cursor P_ ((struct window *));
444static void frame_highlight P_ ((struct frame *));
445static void frame_unhighlight P_ ((struct frame *));
446static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
447static void XTframe_rehighlight P_ ((struct frame *));
448static void x_frame_rehighlight P_ ((struct x_display_info *));
449static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
f02d8aa0 450static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
06a2c219
GM
451static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
452 XRectangle *));
453static void expose_frame P_ ((struct frame *, int, int, int, int));
454static void expose_window_tree P_ ((struct window *, XRectangle *));
455static void expose_window P_ ((struct window *, XRectangle *));
456static void expose_area P_ ((struct window *, struct glyph_row *,
457 XRectangle *, enum glyph_row_area));
458static void expose_line P_ ((struct window *, struct glyph_row *,
459 XRectangle *));
460static void x_update_cursor_in_window_tree P_ ((struct window *, int));
461static void x_update_window_cursor P_ ((struct window *, int));
462static void x_erase_phys_cursor P_ ((struct window *));
463void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
464static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
465 enum bitmap_type));
466
467static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
468 GC, int));
469static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
470static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
471static void note_overwritten_text_cursor P_ ((struct window *, int, int));
472static void x_flush P_ ((struct frame *f));
952291d9
GM
473static void x_update_begin P_ ((struct frame *));
474static void x_update_window_begin P_ ((struct window *));
475static void x_draw_vertical_border P_ ((struct window *));
476static void x_after_update_window_line P_ ((struct glyph_row *));
477static INLINE void take_vertical_position_into_account P_ ((struct it *));
478static void x_produce_stretch_glyph P_ ((struct it *));
479
06a2c219
GM
480
481/* Flush display of frame F, or of all frames if F is null. */
482
483static void
484x_flush (f)
485 struct frame *f;
486{
487 BLOCK_INPUT;
488 if (f == NULL)
489 {
490 Lisp_Object rest, frame;
491 FOR_EACH_FRAME (rest, frame)
492 x_flush (XFRAME (frame));
493 }
494 else if (FRAME_X_P (f))
495 XFlush (FRAME_X_DISPLAY (f));
496 UNBLOCK_INPUT;
497}
498
dc6f92b8 499
06a2c219
GM
500/* Remove calls to XFlush by defining XFlush to an empty replacement.
501 Calls to XFlush should be unnecessary because the X output buffer
502 is flushed automatically as needed by calls to XPending,
503 XNextEvent, or XWindowEvent according to the XFlush man page.
504 XTread_socket calls XPending. Removing XFlush improves
505 performance. */
506
507#define XFlush(DISPLAY) (void) 0
b8009dd1 508
334208b7 509\f
06a2c219
GM
510/***********************************************************************
511 Debugging
512 ***********************************************************************/
513
9382638d 514#if 0
06a2c219
GM
515
516/* This is a function useful for recording debugging information about
517 the sequence of occurrences in this file. */
9382638d
KH
518
519struct record
520{
521 char *locus;
522 int type;
523};
524
525struct record event_record[100];
526
527int event_record_index;
528
529record_event (locus, type)
530 char *locus;
531 int type;
532{
533 if (event_record_index == sizeof (event_record) / sizeof (struct record))
534 event_record_index = 0;
535
536 event_record[event_record_index].locus = locus;
537 event_record[event_record_index].type = type;
538 event_record_index++;
539}
540
541#endif /* 0 */
06a2c219
GM
542
543
9382638d 544\f
334208b7
RS
545/* Return the struct x_display_info corresponding to DPY. */
546
547struct x_display_info *
548x_display_info_for_display (dpy)
549 Display *dpy;
550{
551 struct x_display_info *dpyinfo;
552
553 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
554 if (dpyinfo->display == dpy)
555 return dpyinfo;
16bd92ea 556
334208b7
RS
557 return 0;
558}
f451eb13 559
06a2c219
GM
560
561\f
562/***********************************************************************
563 Starting and ending an update
564 ***********************************************************************/
565
566/* Start an update of frame F. This function is installed as a hook
567 for update_begin, i.e. it is called when update_begin is called.
568 This function is called prior to calls to x_update_window_begin for
569 each window being updated. Currently, there is nothing to do here
570 because all interesting stuff is done on a window basis. */
dc6f92b8 571
dfcf069d 572static void
06a2c219 573x_update_begin (f)
f676886a 574 struct frame *f;
58769bee 575{
06a2c219
GM
576 /* Nothing to do. */
577}
dc6f92b8 578
dc6f92b8 579
06a2c219
GM
580/* Start update of window W. Set the global variable updated_window
581 to the window being updated and set output_cursor to the cursor
582 position of W. */
dc6f92b8 583
06a2c219
GM
584static void
585x_update_window_begin (w)
586 struct window *w;
587{
588 struct frame *f = XFRAME (WINDOW_FRAME (w));
589 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
590
591 updated_window = w;
592 set_output_cursor (&w->cursor);
b8009dd1 593
06a2c219 594 BLOCK_INPUT;
d1bc4182 595
06a2c219 596 if (f == display_info->mouse_face_mouse_frame)
b8009dd1 597 {
514e4681 598 /* Don't do highlighting for mouse motion during the update. */
06a2c219 599 display_info->mouse_face_defer = 1;
37c2c98b 600
06a2c219
GM
601 /* If F needs to be redrawn, simply forget about any prior mouse
602 highlighting. */
9f67f20b 603 if (FRAME_GARBAGED_P (f))
06a2c219
GM
604 display_info->mouse_face_window = Qnil;
605
64f26cf5
GM
606#if 0 /* Rows in a current matrix containing glyphs in mouse-face have
607 their mouse_face_p flag set, which means that they are always
608 unequal to rows in a desired matrix which never have that
609 flag set. So, rows containing mouse-face glyphs are never
610 scrolled, and we don't have to switch the mouse highlight off
611 here to prevent it from being scrolled. */
612
06a2c219
GM
613 /* Can we tell that this update does not affect the window
614 where the mouse highlight is? If so, no need to turn off.
615 Likewise, don't do anything if the frame is garbaged;
616 in that case, the frame's current matrix that we would use
617 is all wrong, and we will redisplay that line anyway. */
618 if (!NILP (display_info->mouse_face_window)
619 && w == XWINDOW (display_info->mouse_face_window))
514e4681 620 {
06a2c219 621 int i;
514e4681 622
06a2c219
GM
623 for (i = 0; i < w->desired_matrix->nrows; ++i)
624 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
514e4681
RS
625 break;
626
06a2c219
GM
627 if (i < w->desired_matrix->nrows)
628 clear_mouse_face (display_info);
514e4681 629 }
64f26cf5 630#endif /* 0 */
b8009dd1 631 }
6ccf47d1 632
dc6f92b8
JB
633 UNBLOCK_INPUT;
634}
635
06a2c219
GM
636
637/* Draw a vertical window border to the right of window W if W doesn't
638 have vertical scroll bars. */
639
dfcf069d 640static void
06a2c219
GM
641x_draw_vertical_border (w)
642 struct window *w;
58769bee 643{
06a2c219
GM
644 struct frame *f = XFRAME (WINDOW_FRAME (w));
645
646 /* Redraw borders between horizontally adjacent windows. Don't
647 do it for frames with vertical scroll bars because either the
648 right scroll bar of a window, or the left scroll bar of its
649 neighbor will suffice as a border. */
650 if (!WINDOW_RIGHTMOST_P (w)
651 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
652 {
653 int x0, x1, y0, y1;
dc6f92b8 654
06a2c219 655 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
110859fc 656 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
657 y1 -= 1;
658
659 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
660 f->output_data.x->normal_gc, x1, y0, x1, y1);
661 }
662}
663
664
71b8321e
GM
665/* End update of window W (which is equal to updated_window).
666
667 Draw vertical borders between horizontally adjacent windows, and
668 display W's cursor if CURSOR_ON_P is non-zero.
669
670 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
671 glyphs in mouse-face were overwritten. In that case we have to
672 make sure that the mouse-highlight is properly redrawn.
673
674 W may be a menu bar pseudo-window in case we don't have X toolkit
675 support. Such windows don't have a cursor, so don't display it
676 here. */
06a2c219
GM
677
678static void
71b8321e 679x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
06a2c219 680 struct window *w;
71b8321e 681 int cursor_on_p, mouse_face_overwritten_p;
06a2c219
GM
682{
683 if (!w->pseudo_window_p)
684 {
71b8321e
GM
685 struct x_display_info *dpyinfo
686 = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
687
06a2c219 688 BLOCK_INPUT;
71b8321e
GM
689
690 /* If a row with mouse-face was overwritten, arrange for
691 XTframe_up_to_date to redisplay the mouse highlight. */
692 if (mouse_face_overwritten_p)
693 {
694 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
695 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
696 dpyinfo->mouse_face_window = Qnil;
697 }
698
06a2c219
GM
699 if (cursor_on_p)
700 x_display_and_set_cursor (w, 1, output_cursor.hpos,
701 output_cursor.vpos,
702 output_cursor.x, output_cursor.y);
71b8321e 703
06a2c219
GM
704 x_draw_vertical_border (w);
705 UNBLOCK_INPUT;
706 }
707
708 updated_window = NULL;
709}
dc6f92b8 710
dc6f92b8 711
06a2c219
GM
712/* End update of frame F. This function is installed as a hook in
713 update_end. */
714
715static void
716x_update_end (f)
717 struct frame *f;
718{
719 /* Mouse highlight may be displayed again. */
aa8bff2e 720 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
b8009dd1 721
06a2c219 722 BLOCK_INPUT;
334208b7 723 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
724 UNBLOCK_INPUT;
725}
b8009dd1 726
06a2c219
GM
727
728/* This function is called from various places in xdisp.c whenever a
729 complete update has been performed. The global variable
730 updated_window is not available here. */
b8009dd1 731
dfcf069d 732static void
b8009dd1 733XTframe_up_to_date (f)
06a2c219 734 struct frame *f;
b8009dd1 735{
06a2c219 736 if (FRAME_X_P (f))
514e4681 737 {
06a2c219 738 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
71b8321e 739
06a2c219
GM
740 if (dpyinfo->mouse_face_deferred_gc
741 || f == dpyinfo->mouse_face_mouse_frame)
742 {
743 BLOCK_INPUT;
744 if (dpyinfo->mouse_face_mouse_frame)
745 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
746 dpyinfo->mouse_face_mouse_x,
747 dpyinfo->mouse_face_mouse_y);
748 dpyinfo->mouse_face_deferred_gc = 0;
749 UNBLOCK_INPUT;
750 }
514e4681 751 }
b8009dd1 752}
06a2c219
GM
753
754
755/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
756 arrow bitmaps, or clear the areas where they would be displayed
757 before DESIRED_ROW is made current. The window being updated is
758 found in updated_window. This function It is called from
759 update_window_line only if it is known that there are differences
760 between bitmaps to be drawn between current row and DESIRED_ROW. */
761
762static void
763x_after_update_window_line (desired_row)
764 struct glyph_row *desired_row;
765{
766 struct window *w = updated_window;
767
768 xassert (w);
769
770 if (!desired_row->mode_line_p && !w->pseudo_window_p)
771 {
772 BLOCK_INPUT;
773 x_draw_row_bitmaps (w, desired_row);
774
775 /* When a window has disappeared, make sure that no rest of
776 full-width rows stays visible in the internal border. */
777 if (windows_or_buffers_changed)
778 {
779 struct frame *f = XFRAME (w->frame);
780 int width = FRAME_INTERNAL_BORDER_WIDTH (f);
781 int height = desired_row->visible_height;
110859fc
GM
782 int x = (window_box_right (w, -1)
783 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
06a2c219
GM
784 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
785
786 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
787 x, y, width, height, False);
788 }
789
790 UNBLOCK_INPUT;
791 }
792}
793
794
795/* Draw the bitmap WHICH in one of the areas to the left or right of
796 window W. ROW is the glyph row for which to display the bitmap; it
797 determines the vertical position at which the bitmap has to be
798 drawn. */
799
800static void
801x_draw_bitmap (w, row, which)
802 struct window *w;
803 struct glyph_row *row;
804 enum bitmap_type which;
805{
806 struct frame *f = XFRAME (WINDOW_FRAME (w));
807 Display *display = FRAME_X_DISPLAY (f);
808 Window window = FRAME_X_WINDOW (f);
809 int x, y, wd, h, dy;
810 unsigned char *bits;
811 Pixmap pixmap;
812 GC gc = f->output_data.x->normal_gc;
813 struct face *face;
814 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
815
816 /* Must clip because of partially visible lines. */
817 x_clip_to_row (w, row, gc, 1);
818
819 switch (which)
820 {
821 case LEFT_TRUNCATION_BITMAP:
822 wd = left_width;
823 h = left_height;
824 bits = left_bits;
825 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
826 - wd
110859fc 827 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
828 break;
829
830 case OVERLAY_ARROW_BITMAP:
831 wd = left_width;
832 h = left_height;
833 bits = ov_bits;
834 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
835 - wd
110859fc 836 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
837 break;
838
839 case RIGHT_TRUNCATION_BITMAP:
840 wd = right_width;
841 h = right_height;
842 bits = right_bits;
843 x = window_box_right (w, -1);
110859fc 844 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
845 break;
846
847 case CONTINUED_LINE_BITMAP:
848 wd = right_width;
849 h = right_height;
850 bits = continued_bits;
851 x = window_box_right (w, -1);
110859fc 852 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
853 break;
854
855 case CONTINUATION_LINE_BITMAP:
856 wd = continuation_width;
857 h = continuation_height;
858 bits = continuation_bits;
859 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
860 - wd
110859fc 861 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
862 break;
863
864 case ZV_LINE_BITMAP:
865 wd = zv_width;
866 h = zv_height;
867 bits = zv_bits;
868 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
869 - wd
110859fc 870 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
871 break;
872
873 default:
874 abort ();
875 }
876
877 /* Convert to frame coordinates. Set dy to the offset in the row to
878 start drawing the bitmap. */
879 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
880 dy = (row->height - h) / 2;
881
882 /* Draw the bitmap. I believe these small pixmaps can be cached
883 by the server. */
884 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
885 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
886 face->foreground,
887 face->background, depth);
888 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
889 XFreePixmap (display, pixmap);
890 XSetClipMask (display, gc, None);
891}
892
893
894/* Draw flags bitmaps for glyph row ROW on window W. Call this
895 function with input blocked. */
896
897static void
898x_draw_row_bitmaps (w, row)
899 struct window *w;
900 struct glyph_row *row;
901{
902 struct frame *f = XFRAME (w->frame);
903 enum bitmap_type bitmap;
904 struct face *face;
045dee35 905 int header_line_height = -1;
06a2c219
GM
906
907 xassert (interrupt_input_blocked);
908
909 /* If row is completely invisible, because of vscrolling, we
910 don't have to draw anything. */
911 if (row->visible_height <= 0)
912 return;
913
914 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
915 PREPARE_FACE_FOR_DISPLAY (f, face);
916
917 /* Decide which bitmap to draw at the left side. */
918 if (row->overlay_arrow_p)
919 bitmap = OVERLAY_ARROW_BITMAP;
920 else if (row->truncated_on_left_p)
921 bitmap = LEFT_TRUNCATION_BITMAP;
922 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
923 bitmap = CONTINUATION_LINE_BITMAP;
924 else if (row->indicate_empty_line_p)
925 bitmap = ZV_LINE_BITMAP;
926 else
927 bitmap = NO_BITMAP;
928
929 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
930 the flags area. */
931 if (bitmap == NO_BITMAP
110859fc 932 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
933 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
934 {
935 /* If W has a vertical border to its left, don't draw over it. */
936 int border = ((XFASTINT (w->left) > 0
937 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
938 ? 1 : 0);
939 int left = window_box_left (w, -1);
940
045dee35
GM
941 if (header_line_height < 0)
942 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
943
944 /* In case the same realized face is used for bitmap areas and
945 for something displayed in the text (e.g. face `region' on
946 mono-displays, the fill style may have been changed to
947 FillSolid in x_draw_glyph_string_background. */
948 if (face->stipple)
949 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
950 else
951 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
952
06a2c219
GM
953 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
954 face->gc,
955 (left
110859fc 956 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219 957 + border),
045dee35 958 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 959 row->y)),
110859fc 960 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
06a2c219 961 row->visible_height);
dcd08bfb
GM
962 if (!face->stipple)
963 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
964 }
965
966 /* Draw the left bitmap. */
967 if (bitmap != NO_BITMAP)
968 x_draw_bitmap (w, row, bitmap);
969
970 /* Decide which bitmap to draw at the right side. */
971 if (row->truncated_on_right_p)
972 bitmap = RIGHT_TRUNCATION_BITMAP;
973 else if (row->continued_p)
974 bitmap = CONTINUED_LINE_BITMAP;
975 else
976 bitmap = NO_BITMAP;
977
978 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
979 the flags area. */
980 if (bitmap == NO_BITMAP
110859fc 981 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
982 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
983 {
984 int right = window_box_right (w, -1);
985
045dee35
GM
986 if (header_line_height < 0)
987 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
988
989 /* In case the same realized face is used for bitmap areas and
990 for something displayed in the text (e.g. face `region' on
991 mono-displays, the fill style may have been changed to
992 FillSolid in x_draw_glyph_string_background. */
993 if (face->stipple)
994 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
995 else
996 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
06a2c219
GM
997 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
998 face->gc,
999 right,
045dee35 1000 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 1001 row->y)),
110859fc 1002 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
06a2c219 1003 row->visible_height);
dcd08bfb
GM
1004 if (!face->stipple)
1005 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
1006 }
1007
1008 /* Draw the right bitmap. */
1009 if (bitmap != NO_BITMAP)
1010 x_draw_bitmap (w, row, bitmap);
1011}
1012
dc6f92b8 1013\f
06a2c219
GM
1014/***********************************************************************
1015 Line Highlighting
1016 ***********************************************************************/
dc6f92b8 1017
06a2c219
GM
1018/* External interface to control of standout mode. Not used for X
1019 frames. Aborts when called. */
1020
1021static void
dc6f92b8
JB
1022XTreassert_line_highlight (new, vpos)
1023 int new, vpos;
1024{
06a2c219 1025 abort ();
dc6f92b8
JB
1026}
1027
06a2c219
GM
1028
1029/* Call this when about to modify line at position VPOS and change
1030 whether it is highlighted. Not used for X frames. Aborts when
1031 called. */
dc6f92b8 1032
dfcf069d 1033static void
06a2c219
GM
1034x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1035 int new_highlight, vpos, y, first_unused_hpos;
dc6f92b8 1036{
06a2c219 1037 abort ();
dc6f92b8
JB
1038}
1039
06a2c219
GM
1040
1041/* This is called when starting Emacs and when restarting after
1042 suspend. When starting Emacs, no X window is mapped. And nothing
1043 must be done to Emacs's own window if it is suspended (though that
1044 rarely happens). */
dc6f92b8 1045
dfcf069d 1046static void
dc6f92b8
JB
1047XTset_terminal_modes ()
1048{
1049}
1050
06a2c219
GM
1051/* This is called when exiting or suspending Emacs. Exiting will make
1052 the X-windows go away, and suspending requires no action. */
dc6f92b8 1053
dfcf069d 1054static void
dc6f92b8
JB
1055XTreset_terminal_modes ()
1056{
dc6f92b8 1057}
06a2c219
GM
1058
1059
dc6f92b8 1060\f
06a2c219
GM
1061/***********************************************************************
1062 Output Cursor
1063 ***********************************************************************/
1064
1065/* Set the global variable output_cursor to CURSOR. All cursor
1066 positions are relative to updated_window. */
dc6f92b8 1067
dfcf069d 1068static void
06a2c219
GM
1069set_output_cursor (cursor)
1070 struct cursor_pos *cursor;
dc6f92b8 1071{
06a2c219
GM
1072 output_cursor.hpos = cursor->hpos;
1073 output_cursor.vpos = cursor->vpos;
1074 output_cursor.x = cursor->x;
1075 output_cursor.y = cursor->y;
1076}
1077
1078
1079/* Set a nominal cursor position.
dc6f92b8 1080
06a2c219
GM
1081 HPOS and VPOS are column/row positions in a window glyph matrix. X
1082 and Y are window text area relative pixel positions.
1083
1084 If this is done during an update, updated_window will contain the
1085 window that is being updated and the position is the future output
1086 cursor position for that window. If updated_window is null, use
1087 selected_window and display the cursor at the given position. */
1088
1089static void
1090XTcursor_to (vpos, hpos, y, x)
1091 int vpos, hpos, y, x;
1092{
1093 struct window *w;
1094
1095 /* If updated_window is not set, work on selected_window. */
1096 if (updated_window)
1097 w = updated_window;
1098 else
1099 w = XWINDOW (selected_window);
dbcb258a 1100
06a2c219
GM
1101 /* Set the output cursor. */
1102 output_cursor.hpos = hpos;
1103 output_cursor.vpos = vpos;
1104 output_cursor.x = x;
1105 output_cursor.y = y;
dc6f92b8 1106
06a2c219
GM
1107 /* If not called as part of an update, really display the cursor.
1108 This will also set the cursor position of W. */
1109 if (updated_window == NULL)
dc6f92b8
JB
1110 {
1111 BLOCK_INPUT;
06a2c219 1112 x_display_cursor (w, 1, hpos, vpos, x, y);
b86bd3dd 1113 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
dc6f92b8
JB
1114 UNBLOCK_INPUT;
1115 }
1116}
dc43ef94 1117
06a2c219
GM
1118
1119\f
1120/***********************************************************************
1121 Display Iterator
1122 ***********************************************************************/
1123
1124/* Function prototypes of this page. */
1125
1126static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1127 struct glyph *,
ee569018
KH
1128 XChar2b *,
1129 int *));
06a2c219
GM
1130static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1131 int, XChar2b *, int));
1132static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1133static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1134static void x_append_glyph P_ ((struct it *));
b4192550 1135static void x_append_composite_glyph P_ ((struct it *));
06a2c219
GM
1136static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1137 int, int, double));
1138static void x_produce_glyphs P_ ((struct it *));
06a2c219 1139static void x_produce_image_glyph P_ ((struct it *it));
ee569018
KH
1140
1141
e2ef8ee6
GM
1142/* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1143 is not contained in the font. */
dc43ef94 1144
06a2c219 1145static INLINE XCharStruct *
ee569018 1146x_per_char_metric (font, char2b)
06a2c219
GM
1147 XFontStruct *font;
1148 XChar2b *char2b;
1149{
1150 /* The result metric information. */
1151 XCharStruct *pcm = NULL;
dc6f92b8 1152
06a2c219 1153 xassert (font && char2b);
dc6f92b8 1154
06a2c219 1155 if (font->per_char != NULL)
dc6f92b8 1156 {
06a2c219 1157 if (font->min_byte1 == 0 && font->max_byte1 == 0)
dc43ef94 1158 {
06a2c219
GM
1159 /* min_char_or_byte2 specifies the linear character index
1160 corresponding to the first element of the per_char array,
1161 max_char_or_byte2 is the index of the last character. A
1162 character with non-zero CHAR2B->byte1 is not in the font.
1163 A character with byte2 less than min_char_or_byte2 or
1164 greater max_char_or_byte2 is not in the font. */
1165 if (char2b->byte1 == 0
1166 && char2b->byte2 >= font->min_char_or_byte2
1167 && char2b->byte2 <= font->max_char_or_byte2)
1168 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
dc43ef94 1169 }
06a2c219 1170 else
dc6f92b8 1171 {
06a2c219
GM
1172 /* If either min_byte1 or max_byte1 are nonzero, both
1173 min_char_or_byte2 and max_char_or_byte2 are less than
1174 256, and the 2-byte character index values corresponding
1175 to the per_char array element N (counting from 0) are:
1176
1177 byte1 = N/D + min_byte1
1178 byte2 = N\D + min_char_or_byte2
1179
1180 where:
1181
1182 D = max_char_or_byte2 - min_char_or_byte2 + 1
1183 / = integer division
1184 \ = integer modulus */
1185 if (char2b->byte1 >= font->min_byte1
1186 && char2b->byte1 <= font->max_byte1
1187 && char2b->byte2 >= font->min_char_or_byte2
1188 && char2b->byte2 <= font->max_char_or_byte2)
1189 {
1190 pcm = (font->per_char
1191 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1192 * (char2b->byte1 - font->min_byte1))
1193 + (char2b->byte2 - font->min_char_or_byte2));
1194 }
dc6f92b8 1195 }
06a2c219
GM
1196 }
1197 else
1198 {
1199 /* If the per_char pointer is null, all glyphs between the first
1200 and last character indexes inclusive have the same
1201 information, as given by both min_bounds and max_bounds. */
1202 if (char2b->byte2 >= font->min_char_or_byte2
1203 && char2b->byte2 <= font->max_char_or_byte2)
1204 pcm = &font->max_bounds;
1205 }
dc6f92b8 1206
ee569018 1207 return ((pcm == NULL
3e71d8f2 1208 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
ee569018 1209 ? NULL : pcm);
06a2c219 1210}
b73b6aaf 1211
57b03282 1212
06a2c219
GM
1213/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1214 the two-byte form of C. Encoding is returned in *CHAR2B. */
dc43ef94 1215
06a2c219
GM
1216static INLINE void
1217x_encode_char (c, char2b, font_info)
1218 int c;
1219 XChar2b *char2b;
1220 struct font_info *font_info;
1221{
1222 int charset = CHAR_CHARSET (c);
1223 XFontStruct *font = font_info->font;
1224
1225 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1226 This may be either a program in a special encoder language or a
1227 fixed encoding. */
1228 if (font_info->font_encoder)
1229 {
1230 /* It's a program. */
1231 struct ccl_program *ccl = font_info->font_encoder;
1232
1233 if (CHARSET_DIMENSION (charset) == 1)
1234 {
1235 ccl->reg[0] = charset;
1236 ccl->reg[1] = char2b->byte2;
1237 }
1238 else
1239 {
1240 ccl->reg[0] = charset;
1241 ccl->reg[1] = char2b->byte1;
1242 ccl->reg[2] = char2b->byte2;
1243 }
1244
1245 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1246
1247 /* We assume that MSBs are appropriately set/reset by CCL
1248 program. */
1249 if (font->max_byte1 == 0) /* 1-byte font */
ee569018 1250 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
06a2c219
GM
1251 else
1252 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1253 }
1254 else if (font_info->encoding[charset])
1255 {
1256 /* Fixed encoding scheme. See fontset.h for the meaning of the
1257 encoding numbers. */
1258 int enc = font_info->encoding[charset];
1259
1260 if ((enc == 1 || enc == 2)
1261 && CHARSET_DIMENSION (charset) == 2)
1262 char2b->byte1 |= 0x80;
1263
1264 if (enc == 1 || enc == 3)
1265 char2b->byte2 |= 0x80;
1266 }
1267}
1268
1269
1270/* Get face and two-byte form of character C in face FACE_ID on frame
1271 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1272 means we want to display multibyte text. Value is a pointer to a
1273 realized face that is ready for display. */
1274
1275static INLINE struct face *
1276x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1277 struct frame *f;
1278 int c, face_id;
1279 XChar2b *char2b;
1280 int multibyte_p;
1281{
1282 struct face *face = FACE_FROM_ID (f, face_id);
1283
1284 if (!multibyte_p)
1285 {
1286 /* Unibyte case. We don't have to encode, but we have to make
1287 sure to use a face suitable for unibyte. */
1288 char2b->byte1 = 0;
1289 char2b->byte2 = c;
ee569018
KH
1290 face_id = FACE_FOR_CHAR (f, face, c);
1291 face = FACE_FROM_ID (f, face_id);
06a2c219
GM
1292 }
1293 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1294 {
1295 /* Case of ASCII in a face known to fit ASCII. */
1296 char2b->byte1 = 0;
1297 char2b->byte2 = c;
1298 }
1299 else
1300 {
1301 int c1, c2, charset;
1302
1303 /* Split characters into bytes. If c2 is -1 afterwards, C is
1304 really a one-byte character so that byte1 is zero. */
1305 SPLIT_CHAR (c, charset, c1, c2);
1306 if (c2 > 0)
1307 char2b->byte1 = c1, char2b->byte2 = c2;
1308 else
1309 char2b->byte1 = 0, char2b->byte2 = c1;
1310
06a2c219 1311 /* Maybe encode the character in *CHAR2B. */
ee569018 1312 if (face->font != NULL)
06a2c219
GM
1313 {
1314 struct font_info *font_info
1315 = FONT_INFO_FROM_ID (f, face->font_info_id);
1316 if (font_info)
ee569018 1317 x_encode_char (c, char2b, font_info);
06a2c219
GM
1318 }
1319 }
1320
1321 /* Make sure X resources of the face are allocated. */
1322 xassert (face != NULL);
1323 PREPARE_FACE_FOR_DISPLAY (f, face);
1324
1325 return face;
1326}
1327
1328
1329/* Get face and two-byte form of character glyph GLYPH on frame F.
43d120d8 1330 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
06a2c219
GM
1331 a pointer to a realized face that is ready for display. */
1332
1333static INLINE struct face *
ee569018 1334x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
06a2c219
GM
1335 struct frame *f;
1336 struct glyph *glyph;
1337 XChar2b *char2b;
ee569018 1338 int *two_byte_p;
06a2c219
GM
1339{
1340 struct face *face;
1341
1342 xassert (glyph->type == CHAR_GLYPH);
43d120d8 1343 face = FACE_FROM_ID (f, glyph->face_id);
06a2c219 1344
ee569018
KH
1345 if (two_byte_p)
1346 *two_byte_p = 0;
1347
06a2c219
GM
1348 if (!glyph->multibyte_p)
1349 {
1350 /* Unibyte case. We don't have to encode, but we have to make
1351 sure to use a face suitable for unibyte. */
1352 char2b->byte1 = 0;
43d120d8 1353 char2b->byte2 = glyph->u.ch;
06a2c219 1354 }
43d120d8
KH
1355 else if (glyph->u.ch < 128
1356 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
06a2c219
GM
1357 {
1358 /* Case of ASCII in a face known to fit ASCII. */
1359 char2b->byte1 = 0;
43d120d8 1360 char2b->byte2 = glyph->u.ch;
06a2c219
GM
1361 }
1362 else
1363 {
1364 int c1, c2, charset;
1365
1366 /* Split characters into bytes. If c2 is -1 afterwards, C is
1367 really a one-byte character so that byte1 is zero. */
43d120d8 1368 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
06a2c219
GM
1369 if (c2 > 0)
1370 char2b->byte1 = c1, char2b->byte2 = c2;
1371 else
1372 char2b->byte1 = 0, char2b->byte2 = c1;
1373
1374 /* Maybe encode the character in *CHAR2B. */
1375 if (charset != CHARSET_ASCII)
1376 {
1377 struct font_info *font_info
1378 = FONT_INFO_FROM_ID (f, face->font_info_id);
1379 if (font_info)
1380 {
43d120d8 1381 x_encode_char (glyph->u.ch, char2b, font_info);
ee569018
KH
1382 if (two_byte_p)
1383 *two_byte_p
1384 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
06a2c219
GM
1385 }
1386 }
1387 }
1388
1389 /* Make sure X resources of the face are allocated. */
1390 xassert (face != NULL);
1391 PREPARE_FACE_FOR_DISPLAY (f, face);
1392 return face;
1393}
1394
1395
1396/* Store one glyph for IT->char_to_display in IT->glyph_row.
1397 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1398
1399static INLINE void
1400x_append_glyph (it)
1401 struct it *it;
1402{
1403 struct glyph *glyph;
1404 enum glyph_row_area area = it->area;
1405
1406 xassert (it->glyph_row);
1407 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1408
1409 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1410 if (glyph < it->glyph_row->glyphs[area + 1])
1411 {
06a2c219
GM
1412 glyph->charpos = CHARPOS (it->position);
1413 glyph->object = it->object;
88d75730 1414 glyph->pixel_width = it->pixel_width;
06a2c219 1415 glyph->voffset = it->voffset;
88d75730 1416 glyph->type = CHAR_GLYPH;
06a2c219 1417 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1418 glyph->left_box_line_p = it->start_of_box_run_p;
1419 glyph->right_box_line_p = it->end_of_box_run_p;
66ac4b0e
GM
1420 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1421 || it->phys_descent > it->descent);
88d75730 1422 glyph->padding_p = 0;
ee569018 1423 glyph->glyph_not_available_p = it->glyph_not_available_p;
88d75730
GM
1424 glyph->face_id = it->face_id;
1425 glyph->u.ch = it->char_to_display;
06a2c219
GM
1426 ++it->glyph_row->used[area];
1427 }
1428}
1429
b4192550
KH
1430/* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1431 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1432
1433static INLINE void
1434x_append_composite_glyph (it)
1435 struct it *it;
1436{
1437 struct glyph *glyph;
1438 enum glyph_row_area area = it->area;
1439
1440 xassert (it->glyph_row);
1441
1442 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1443 if (glyph < it->glyph_row->glyphs[area + 1])
1444 {
b4192550
KH
1445 glyph->charpos = CHARPOS (it->position);
1446 glyph->object = it->object;
88d75730 1447 glyph->pixel_width = it->pixel_width;
b4192550 1448 glyph->voffset = it->voffset;
88d75730 1449 glyph->type = COMPOSITE_GLYPH;
b4192550 1450 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1451 glyph->left_box_line_p = it->start_of_box_run_p;
1452 glyph->right_box_line_p = it->end_of_box_run_p;
b4192550
KH
1453 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1454 || it->phys_descent > it->descent);
88d75730
GM
1455 glyph->padding_p = 0;
1456 glyph->glyph_not_available_p = 0;
1457 glyph->face_id = it->face_id;
1458 glyph->u.cmp_id = it->cmp_id;
b4192550
KH
1459 ++it->glyph_row->used[area];
1460 }
1461}
1462
06a2c219
GM
1463
1464/* Change IT->ascent and IT->height according to the setting of
1465 IT->voffset. */
1466
1467static INLINE void
1468take_vertical_position_into_account (it)
1469 struct it *it;
1470{
1471 if (it->voffset)
1472 {
1473 if (it->voffset < 0)
1474 /* Increase the ascent so that we can display the text higher
1475 in the line. */
1476 it->ascent += abs (it->voffset);
1477 else
1478 /* Increase the descent so that we can display the text lower
1479 in the line. */
1480 it->descent += it->voffset;
1481 }
1482}
1483
1484
1485/* Produce glyphs/get display metrics for the image IT is loaded with.
1486 See the description of struct display_iterator in dispextern.h for
1487 an overview of struct display_iterator. */
1488
1489static void
1490x_produce_image_glyph (it)
1491 struct it *it;
1492{
1493 struct image *img;
1494 struct face *face;
1495
1496 xassert (it->what == IT_IMAGE);
1497
1498 face = FACE_FROM_ID (it->f, it->face_id);
1499 img = IMAGE_FROM_ID (it->f, it->image_id);
1500 xassert (img);
1501
1502 /* Make sure X resources of the face and image are loaded. */
1503 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1504 prepare_image_for_display (it->f, img);
1505
95af8492 1506 it->ascent = it->phys_ascent = image_ascent (img, face);
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. */
c7911417 5822 if (w->current_matrix == NULL || w == updated_window)
80c32bcc
GM
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{
7948d50a
GM
7293#if 0 /* This prevents redrawing tool bar items when changing
7294 from one to another, so don't do it. */
685f4368 7295 if (!NILP (tip_frame))
06a2c219 7296 return;
7948d50a 7297#endif
06a2c219 7298
7a13e894 7299 if (! NILP (dpyinfo->mouse_face_window))
06a2c219 7300 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
b8009dd1 7301
7a13e894
RS
7302 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7303 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7304 dpyinfo->mouse_face_window = Qnil;
b8009dd1 7305}
e687d06e 7306
71b8321e
GM
7307
7308/* Clear any mouse-face on window W. This function is part of the
7309 redisplay interface, and is called from try_window_id and similar
7310 functions to ensure the mouse-highlight is off. */
7311
7312static void
7313x_clear_mouse_face (w)
7314 struct window *w;
7315{
7316 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7317 Lisp_Object window;
7318
2e636f9d 7319 BLOCK_INPUT;
71b8321e
GM
7320 XSETWINDOW (window, w);
7321 if (EQ (window, dpyinfo->mouse_face_window))
7322 clear_mouse_face (dpyinfo);
2e636f9d 7323 UNBLOCK_INPUT;
71b8321e
GM
7324}
7325
7326
e687d06e
RS
7327/* Just discard the mouse face information for frame F, if any.
7328 This is used when the size of F is changed. */
7329
dfcf069d 7330void
e687d06e
RS
7331cancel_mouse_face (f)
7332 FRAME_PTR f;
7333{
7334 Lisp_Object window;
7335 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7336
7337 window = dpyinfo->mouse_face_window;
7338 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7339 {
7340 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7341 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7342 dpyinfo->mouse_face_window = Qnil;
7343 }
7344}
b8009dd1 7345\f
ab648270
JB
7346static struct scroll_bar *x_window_to_scroll_bar ();
7347static void x_scroll_bar_report_motion ();
12ba150f 7348
90e65f07 7349/* Return the current position of the mouse.
2d7fc7e8 7350 *fp should be a frame which indicates which display to ask about.
90e65f07 7351
2d7fc7e8 7352 If the mouse movement started in a scroll bar, set *fp, *bar_window,
ab648270 7353 and *part to the frame, window, and scroll bar part that the mouse
12ba150f 7354 is over. Set *x and *y to the portion and whole of the mouse's
ab648270 7355 position on the scroll bar.
12ba150f 7356
2d7fc7e8 7357 If the mouse movement started elsewhere, set *fp to the frame the
12ba150f
JB
7358 mouse is on, *bar_window to nil, and *x and *y to the character cell
7359 the mouse is over.
7360
06a2c219 7361 Set *time to the server time-stamp for the time at which the mouse
12ba150f
JB
7362 was at this position.
7363
a135645a
RS
7364 Don't store anything if we don't have a valid set of values to report.
7365
90e65f07 7366 This clears the mouse_moved flag, so we can wait for the next mouse
f5bb65ec 7367 movement. */
90e65f07
JB
7368
7369static void
1cf412ec 7370XTmouse_position (fp, insist, bar_window, part, x, y, time)
334208b7 7371 FRAME_PTR *fp;
1cf412ec 7372 int insist;
12ba150f 7373 Lisp_Object *bar_window;
ab648270 7374 enum scroll_bar_part *part;
90e65f07 7375 Lisp_Object *x, *y;
e5d77022 7376 unsigned long *time;
90e65f07 7377{
a135645a
RS
7378 FRAME_PTR f1;
7379
90e65f07
JB
7380 BLOCK_INPUT;
7381
8bcee03e 7382 if (! NILP (last_mouse_scroll_bar) && insist == 0)
334208b7 7383 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
90e65f07
JB
7384 else
7385 {
12ba150f
JB
7386 Window root;
7387 int root_x, root_y;
90e65f07 7388
12ba150f
JB
7389 Window dummy_window;
7390 int dummy;
7391
39d8bb4d
KH
7392 Lisp_Object frame, tail;
7393
7394 /* Clear the mouse-moved flag for every frame on this display. */
7395 FOR_EACH_FRAME (tail, frame)
7396 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7397 XFRAME (frame)->mouse_moved = 0;
7398
ab648270 7399 last_mouse_scroll_bar = Qnil;
12ba150f
JB
7400
7401 /* Figure out which root window we're on. */
334208b7
RS
7402 XQueryPointer (FRAME_X_DISPLAY (*fp),
7403 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
12ba150f
JB
7404
7405 /* The root window which contains the pointer. */
7406 &root,
7407
7408 /* Trash which we can't trust if the pointer is on
7409 a different screen. */
7410 &dummy_window,
7411
7412 /* The position on that root window. */
58769bee 7413 &root_x, &root_y,
12ba150f
JB
7414
7415 /* More trash we can't trust. */
7416 &dummy, &dummy,
7417
7418 /* Modifier keys and pointer buttons, about which
7419 we don't care. */
7420 (unsigned int *) &dummy);
7421
7422 /* Now we have a position on the root; find the innermost window
7423 containing the pointer. */
7424 {
7425 Window win, child;
7426 int win_x, win_y;
06a2c219 7427 int parent_x = 0, parent_y = 0;
e99db5a1 7428 int count;
12ba150f
JB
7429
7430 win = root;
69388238 7431
2d7fc7e8
RS
7432 /* XTranslateCoordinates can get errors if the window
7433 structure is changing at the same time this function
7434 is running. So at least we must not crash from them. */
7435
e99db5a1 7436 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
2d7fc7e8 7437
334208b7 7438 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
23faf38f 7439 && FRAME_LIVE_P (last_mouse_frame))
12ba150f 7440 {
69388238
RS
7441 /* If mouse was grabbed on a frame, give coords for that frame
7442 even if the mouse is now outside it. */
334208b7 7443 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
69388238 7444
12ba150f 7445 /* From-window, to-window. */
69388238 7446 root, FRAME_X_WINDOW (last_mouse_frame),
12ba150f
JB
7447
7448 /* From-position, to-position. */
7449 root_x, root_y, &win_x, &win_y,
7450
7451 /* Child of win. */
7452 &child);
69388238
RS
7453 f1 = last_mouse_frame;
7454 }
7455 else
7456 {
7457 while (1)
7458 {
334208b7 7459 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
12ba150f 7460
69388238
RS
7461 /* From-window, to-window. */
7462 root, win,
12ba150f 7463
69388238
RS
7464 /* From-position, to-position. */
7465 root_x, root_y, &win_x, &win_y,
7466
7467 /* Child of win. */
7468 &child);
7469
9af3143a 7470 if (child == None || child == win)
69388238
RS
7471 break;
7472
7473 win = child;
7474 parent_x = win_x;
7475 parent_y = win_y;
7476 }
12ba150f 7477
69388238
RS
7478 /* Now we know that:
7479 win is the innermost window containing the pointer
7480 (XTC says it has no child containing the pointer),
7481 win_x and win_y are the pointer's position in it
7482 (XTC did this the last time through), and
7483 parent_x and parent_y are the pointer's position in win's parent.
7484 (They are what win_x and win_y were when win was child.
7485 If win is the root window, it has no parent, and
7486 parent_{x,y} are invalid, but that's okay, because we'll
7487 never use them in that case.) */
7488
7489 /* Is win one of our frames? */
19126e11 7490 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
aad0f6ab
GM
7491
7492#ifdef USE_X_TOOLKIT
7493 /* If we end up with the menu bar window, say it's not
7494 on the frame. */
7495 if (f1 != NULL
7496 && f1->output_data.x->menubar_widget
7497 && win == XtWindow (f1->output_data.x->menubar_widget))
7498 f1 = NULL;
7499#endif /* USE_X_TOOLKIT */
69388238 7500 }
58769bee 7501
2d7fc7e8
RS
7502 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7503 f1 = 0;
7504
e99db5a1 7505 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
2d7fc7e8 7506
ab648270 7507 /* If not, is it one of our scroll bars? */
a135645a 7508 if (! f1)
12ba150f 7509 {
ab648270 7510 struct scroll_bar *bar = x_window_to_scroll_bar (win);
12ba150f
JB
7511
7512 if (bar)
7513 {
a135645a 7514 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
7515 win_x = parent_x;
7516 win_y = parent_y;
7517 }
7518 }
90e65f07 7519
8bcee03e 7520 if (f1 == 0 && insist > 0)
b86bd3dd 7521 f1 = SELECTED_FRAME ();
1cf412ec 7522
a135645a 7523 if (f1)
12ba150f 7524 {
06a2c219
GM
7525 /* Ok, we found a frame. Store all the values.
7526 last_mouse_glyph is a rectangle used to reduce the
7527 generation of mouse events. To not miss any motion
7528 events, we must divide the frame into rectangles of the
7529 size of the smallest character that could be displayed
7530 on it, i.e. into the same rectangles that matrices on
7531 the frame are divided into. */
7532
7533#if OLD_REDISPLAY_CODE
2b5c9e71 7534 int ignore1, ignore2;
2b5c9e71 7535 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
334208b7 7536 &last_mouse_glyph,
1cf412ec
RS
7537 FRAME_X_DISPLAY_INFO (f1)->grabbed
7538 || insist);
06a2c219
GM
7539#else
7540 {
7541 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7542 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7543 int x = win_x;
7544 int y = win_y;
7545
7546 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7547 round down even for negative values. */
7548 if (x < 0)
7549 x -= width - 1;
7550 if (y < 0)
7551 y -= height - 1;
7552
7553 last_mouse_glyph.width = width;
7554 last_mouse_glyph.height = height;
7555 last_mouse_glyph.x = (x + width - 1) / width * width;
7556 last_mouse_glyph.y = (y + height - 1) / height * height;
7557 }
7558#endif
12ba150f
JB
7559
7560 *bar_window = Qnil;
7561 *part = 0;
334208b7 7562 *fp = f1;
e0c1aef2
KH
7563 XSETINT (*x, win_x);
7564 XSETINT (*y, win_y);
12ba150f
JB
7565 *time = last_mouse_movement_time;
7566 }
7567 }
7568 }
90e65f07
JB
7569
7570 UNBLOCK_INPUT;
7571}
f451eb13 7572
06a2c219 7573
06a2c219 7574#ifdef USE_X_TOOLKIT
bffcfca9
GM
7575
7576/* Atimer callback function for TIMER. Called every 0.1s to process
7577 Xt timeouts, if needed. We must avoid calling XtAppPending as
7578 much as possible because that function does an implicit XFlush
7579 that slows us down. */
7580
7581static void
7582x_process_timeouts (timer)
7583 struct atimer *timer;
7584{
7585 if (toolkit_scroll_bar_interaction || popup_activated_flag)
7586 {
7587 BLOCK_INPUT;
7588 while (XtAppPending (Xt_app_con) & XtIMTimer)
7589 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7590 UNBLOCK_INPUT;
7591 }
06a2c219
GM
7592}
7593
bffcfca9 7594#endif /* USE_X_TOOLKIT */
06a2c219
GM
7595
7596\f
7597/* Scroll bar support. */
7598
7599/* Given an X window ID, find the struct scroll_bar which manages it.
7600 This can be called in GC, so we have to make sure to strip off mark
7601 bits. */
bffcfca9 7602
06a2c219
GM
7603static struct scroll_bar *
7604x_window_to_scroll_bar (window_id)
7605 Window window_id;
7606{
7607 Lisp_Object tail;
7608
7609 for (tail = Vframe_list;
7610 XGCTYPE (tail) == Lisp_Cons;
8e713be6 7611 tail = XCDR (tail))
06a2c219
GM
7612 {
7613 Lisp_Object frame, bar, condemned;
7614
8e713be6 7615 frame = XCAR (tail);
06a2c219
GM
7616 /* All elements of Vframe_list should be frames. */
7617 if (! GC_FRAMEP (frame))
7618 abort ();
7619
7620 /* Scan this frame's scroll bar list for a scroll bar with the
7621 right window ID. */
7622 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7623 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7624 /* This trick allows us to search both the ordinary and
7625 condemned scroll bar lists with one loop. */
7626 ! GC_NILP (bar) || (bar = condemned,
7627 condemned = Qnil,
7628 ! GC_NILP (bar));
7629 bar = XSCROLL_BAR (bar)->next)
7630 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7631 return XSCROLL_BAR (bar);
7632 }
7633
7634 return 0;
7635}
7636
7637
7638\f
7639/************************************************************************
7640 Toolkit scroll bars
7641 ************************************************************************/
7642
eccc05db 7643#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
7644
7645static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7646static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7647static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7648 struct scroll_bar *));
7649static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7650 int, int, int));
7651
7652
7653/* Id of action hook installed for scroll bars. */
7654
7655static XtActionHookId action_hook_id;
7656
7657/* Lisp window being scrolled. Set when starting to interact with
7658 a toolkit scroll bar, reset to nil when ending the interaction. */
7659
7660static Lisp_Object window_being_scrolled;
7661
7662/* Last scroll bar part sent in xm_scroll_callback. */
7663
7664static int last_scroll_bar_part;
7665
ec18280f
SM
7666/* Whether this is an Xaw with arrow-scrollbars. This should imply
7667 that movements of 1/20 of the screen size are mapped to up/down. */
7668
7669static Boolean xaw3d_arrow_scroll;
7670
7671/* Whether the drag scrolling maintains the mouse at the top of the
7672 thumb. If not, resizing the thumb needs to be done more carefully
7673 to avoid jerkyness. */
7674
7675static Boolean xaw3d_pick_top;
7676
06a2c219
GM
7677
7678/* Action hook installed via XtAppAddActionHook when toolkit scroll
ec18280f 7679 bars are used.. The hook is responsible for detecting when
06a2c219
GM
7680 the user ends an interaction with the scroll bar, and generates
7681 a `end-scroll' scroll_bar_click' event if so. */
7682
7683static void
7684xt_action_hook (widget, client_data, action_name, event, params,
7685 num_params)
7686 Widget widget;
7687 XtPointer client_data;
7688 String action_name;
7689 XEvent *event;
7690 String *params;
7691 Cardinal *num_params;
7692{
7693 int scroll_bar_p;
7694 char *end_action;
7695
7696#ifdef USE_MOTIF
7697 scroll_bar_p = XmIsScrollBar (widget);
7698 end_action = "Release";
ec18280f 7699#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
7700 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7701 end_action = "EndScroll";
ec18280f 7702#endif /* USE_MOTIF */
06a2c219 7703
06a2c219
GM
7704 if (scroll_bar_p
7705 && strcmp (action_name, end_action) == 0
7706 && WINDOWP (window_being_scrolled))
7707 {
7708 struct window *w;
7709
7710 x_send_scroll_bar_event (window_being_scrolled,
7711 scroll_bar_end_scroll, 0, 0);
7712 w = XWINDOW (window_being_scrolled);
7713 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7714 window_being_scrolled = Qnil;
7715 last_scroll_bar_part = -1;
bffcfca9
GM
7716
7717 /* Xt timeouts no longer needed. */
7718 toolkit_scroll_bar_interaction = 0;
06a2c219
GM
7719 }
7720}
7721
07b3d16e
GM
7722/* A vector of windows used for communication between
7723 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
7724
7725static struct window **scroll_bar_windows;
7726static int scroll_bar_windows_size;
7727
06a2c219
GM
7728
7729/* Send a client message with message type Xatom_Scrollbar for a
7730 scroll action to the frame of WINDOW. PART is a value identifying
7731 the part of the scroll bar that was clicked on. PORTION is the
7732 amount to scroll of a whole of WHOLE. */
7733
7734static void
7735x_send_scroll_bar_event (window, part, portion, whole)
7736 Lisp_Object window;
7737 int part, portion, whole;
7738{
7739 XEvent event;
7740 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
07b3d16e
GM
7741 struct window *w = XWINDOW (window);
7742 struct frame *f = XFRAME (w->frame);
7743 int i;
06a2c219 7744
07b3d16e
GM
7745 BLOCK_INPUT;
7746
06a2c219
GM
7747 /* Construct a ClientMessage event to send to the frame. */
7748 ev->type = ClientMessage;
7749 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7750 ev->display = FRAME_X_DISPLAY (f);
7751 ev->window = FRAME_X_WINDOW (f);
7752 ev->format = 32;
07b3d16e
GM
7753
7754 /* We can only transfer 32 bits in the XClientMessageEvent, which is
7755 not enough to store a pointer or Lisp_Object on a 64 bit system.
7756 So, store the window in scroll_bar_windows and pass the index
7757 into that array in the event. */
7758 for (i = 0; i < scroll_bar_windows_size; ++i)
7759 if (scroll_bar_windows[i] == NULL)
7760 break;
7761
7762 if (i == scroll_bar_windows_size)
7763 {
7764 int new_size = max (10, 2 * scroll_bar_windows_size);
7765 size_t nbytes = new_size * sizeof *scroll_bar_windows;
7766 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
7767
7768 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
7769 nbytes);
7770 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
7771 scroll_bar_windows_size = new_size;
7772 }
7773
7774 scroll_bar_windows[i] = w;
7775 ev->data.l[0] = (long) i;
06a2c219
GM
7776 ev->data.l[1] = (long) part;
7777 ev->data.l[2] = (long) 0;
7778 ev->data.l[3] = (long) portion;
7779 ev->data.l[4] = (long) whole;
7780
bffcfca9
GM
7781 /* Make Xt timeouts work while the scroll bar is active. */
7782 toolkit_scroll_bar_interaction = 1;
7783
06a2c219
GM
7784 /* Setting the event mask to zero means that the message will
7785 be sent to the client that created the window, and if that
7786 window no longer exists, no event will be sent. */
06a2c219
GM
7787 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7788 UNBLOCK_INPUT;
7789}
7790
7791
7792/* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7793 in *IEVENT. */
7794
7795static void
7796x_scroll_bar_to_input_event (event, ievent)
7797 XEvent *event;
7798 struct input_event *ievent;
7799{
7800 XClientMessageEvent *ev = (XClientMessageEvent *) event;
52e386c2
KR
7801 Lisp_Object window;
7802 struct frame *f;
07b3d16e
GM
7803 struct window *w;
7804
7805 w = scroll_bar_windows[ev->data.l[0]];
7806 scroll_bar_windows[ev->data.l[0]] = NULL;
52e386c2 7807
07b3d16e
GM
7808 XSETWINDOW (window, w);
7809 f = XFRAME (w->frame);
06a2c219
GM
7810
7811 ievent->kind = scroll_bar_click;
7812 ievent->frame_or_window = window;
0f8aabe9 7813 ievent->arg = Qnil;
06a2c219
GM
7814 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7815 ievent->part = ev->data.l[1];
7816 ievent->code = ev->data.l[2];
7817 ievent->x = make_number ((int) ev->data.l[3]);
7818 ievent->y = make_number ((int) ev->data.l[4]);
7819 ievent->modifiers = 0;
7820}
7821
7822
7823#ifdef USE_MOTIF
7824
7825/* Minimum and maximum values used for Motif scroll bars. */
7826
7827#define XM_SB_MIN 1
7828#define XM_SB_MAX 10000000
7829#define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7830
7831
7832/* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7833 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7834 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7835
7836static void
7837xm_scroll_callback (widget, client_data, call_data)
7838 Widget widget;
7839 XtPointer client_data, call_data;
7840{
7841 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7842 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7843 double percent;
7844 int part = -1, whole = 0, portion = 0;
7845
7846 switch (cs->reason)
7847 {
7848 case XmCR_DECREMENT:
7849 bar->dragging = Qnil;
7850 part = scroll_bar_up_arrow;
7851 break;
7852
7853 case XmCR_INCREMENT:
7854 bar->dragging = Qnil;
7855 part = scroll_bar_down_arrow;
7856 break;
7857
7858 case XmCR_PAGE_DECREMENT:
7859 bar->dragging = Qnil;
7860 part = scroll_bar_above_handle;
7861 break;
7862
7863 case XmCR_PAGE_INCREMENT:
7864 bar->dragging = Qnil;
7865 part = scroll_bar_below_handle;
7866 break;
7867
7868 case XmCR_TO_TOP:
7869 bar->dragging = Qnil;
7870 part = scroll_bar_to_top;
7871 break;
7872
7873 case XmCR_TO_BOTTOM:
7874 bar->dragging = Qnil;
7875 part = scroll_bar_to_bottom;
7876 break;
7877
7878 case XmCR_DRAG:
7879 {
7880 int slider_size;
7881 int dragging_down_p = (INTEGERP (bar->dragging)
7882 && XINT (bar->dragging) <= cs->value);
7883
7884 /* Get the slider size. */
7885 BLOCK_INPUT;
7886 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7887 UNBLOCK_INPUT;
7888
7889 /* At the max position of the scroll bar, do a line-wise
7890 movement. Without doing anything, the LessTif scroll bar
7891 calls us with the same cs->value again and again. If we
7892 want to make sure that we can reach the end of the buffer,
7893 we have to do something.
7894
7895 Implementation note: setting bar->dragging always to
7896 cs->value gives a smoother movement at the max position.
7897 Setting it to nil when doing line-wise movement gives
7898 a better slider behavior. */
7899
7900 if (cs->value + slider_size == XM_SB_MAX
7901 || (dragging_down_p
7902 && last_scroll_bar_part == scroll_bar_down_arrow))
7903 {
7904 part = scroll_bar_down_arrow;
7905 bar->dragging = Qnil;
7906 }
7907 else
7908 {
7909 whole = XM_SB_RANGE;
7910 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7911 part = scroll_bar_handle;
7912 bar->dragging = make_number (cs->value);
7913 }
7914 }
7915 break;
7916
7917 case XmCR_VALUE_CHANGED:
7918 break;
7919 };
7920
7921 if (part >= 0)
7922 {
7923 window_being_scrolled = bar->window;
7924 last_scroll_bar_part = part;
7925 x_send_scroll_bar_event (bar->window, part, portion, whole);
7926 }
7927}
7928
7929
ec18280f 7930#else /* !USE_MOTIF, i.e. Xaw. */
06a2c219
GM
7931
7932
ec18280f 7933/* Xaw scroll bar callback. Invoked when the thumb is dragged.
06a2c219
GM
7934 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7935 scroll bar struct. CALL_DATA is a pointer to a float saying where
7936 the thumb is. */
7937
7938static void
ec18280f 7939xaw_jump_callback (widget, client_data, call_data)
06a2c219
GM
7940 Widget widget;
7941 XtPointer client_data, call_data;
7942{
7943 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7944 float top = *(float *) call_data;
7945 float shown;
ec18280f
SM
7946 int whole, portion, height;
7947 int part;
06a2c219
GM
7948
7949 /* Get the size of the thumb, a value between 0 and 1. */
7950 BLOCK_INPUT;
ec18280f 7951 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
06a2c219
GM
7952 UNBLOCK_INPUT;
7953
7954 whole = 10000000;
7955 portion = shown < 1 ? top * whole : 0;
06a2c219 7956
ec18280f
SM
7957 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
7958 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
7959 the bottom, so we force the scrolling whenever we see that we're
7960 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
7961 we try to ensure that we always stay two pixels away from the
7962 bottom). */
06a2c219
GM
7963 part = scroll_bar_down_arrow;
7964 else
7965 part = scroll_bar_handle;
7966
7967 window_being_scrolled = bar->window;
7968 bar->dragging = make_number (portion);
7969 last_scroll_bar_part = part;
7970 x_send_scroll_bar_event (bar->window, part, portion, whole);
7971}
7972
7973
ec18280f
SM
7974/* Xaw scroll bar callback. Invoked for incremental scrolling.,
7975 i.e. line or page up or down. WIDGET is the Xaw scroll bar
06a2c219
GM
7976 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
7977 the scroll bar. CALL_DATA is an integer specifying the action that
7978 has taken place. It's magnitude is in the range 0..height of the
7979 scroll bar. Negative values mean scroll towards buffer start.
7980 Values < height of scroll bar mean line-wise movement. */
7981
7982static void
ec18280f 7983xaw_scroll_callback (widget, client_data, call_data)
06a2c219
GM
7984 Widget widget;
7985 XtPointer client_data, call_data;
7986{
7987 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7988 int position = (int) call_data;
7989 Dimension height;
7990 int part;
7991
7992 /* Get the height of the scroll bar. */
7993 BLOCK_INPUT;
7994 XtVaGetValues (widget, XtNheight, &height, NULL);
7995 UNBLOCK_INPUT;
7996
ec18280f
SM
7997 if (abs (position) >= height)
7998 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
7999
8000 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
8001 it maps line-movement to call_data = max(5, height/20). */
8002 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8003 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
06a2c219 8004 else
ec18280f 8005 part = scroll_bar_move_ratio;
06a2c219
GM
8006
8007 window_being_scrolled = bar->window;
8008 bar->dragging = Qnil;
8009 last_scroll_bar_part = part;
ec18280f 8010 x_send_scroll_bar_event (bar->window, part, position, height);
06a2c219
GM
8011}
8012
8013
8014#endif /* not USE_MOTIF */
8015
8016
8017/* Create the widget for scroll bar BAR on frame F. Record the widget
8018 and X window of the scroll bar in BAR. */
8019
8020static void
8021x_create_toolkit_scroll_bar (f, bar)
8022 struct frame *f;
8023 struct scroll_bar *bar;
8024{
8025 Window xwindow;
8026 Widget widget;
8027 Arg av[20];
8028 int ac = 0;
8029 char *scroll_bar_name = "verticalScrollBar";
8030 unsigned long pixel;
8031
8032 BLOCK_INPUT;
8033
8034#ifdef USE_MOTIF
8035 /* LessTif 0.85, problems:
8036
8037 1. When the mouse if over the scroll bar, the scroll bar will
8038 get keyboard events. I didn't find a way to turn this off.
8039
8040 2. Do we have to explicitly set the cursor to get an arrow
8041 cursor (see below)? */
8042
8043 /* Set resources. Create the widget. */
8044 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8045 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8046 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8047 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8048 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8049 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8050 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8051
8052 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8053 if (pixel != -1)
8054 {
8055 XtSetArg (av[ac], XmNforeground, pixel);
8056 ++ac;
8057 }
8058
8059 pixel = f->output_data.x->scroll_bar_background_pixel;
8060 if (pixel != -1)
8061 {
8062 XtSetArg (av[ac], XmNbackground, pixel);
8063 ++ac;
8064 }
8065
8066 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8067 scroll_bar_name, av, ac);
8068
8069 /* Add one callback for everything that can happen. */
8070 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8071 (XtPointer) bar);
8072 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8073 (XtPointer) bar);
8074 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8075 (XtPointer) bar);
8076 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8077 (XtPointer) bar);
8078 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8079 (XtPointer) bar);
8080 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8081 (XtPointer) bar);
8082 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8083 (XtPointer) bar);
8084
8085 /* Realize the widget. Only after that is the X window created. */
8086 XtRealizeWidget (widget);
8087
8088 /* Set the cursor to an arrow. I didn't find a resource to do that.
8089 And I'm wondering why it hasn't an arrow cursor by default. */
8090 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8091 f->output_data.x->nontext_cursor);
8092
ec18280f 8093#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
8094
8095 /* Set resources. Create the widget. The background of the
8096 Xaw3d scroll bar widget is a little bit light for my taste.
8097 We don't alter it here to let users change it according
8098 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8099 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8100 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
ec18280f
SM
8101 /* For smoother scrolling with Xaw3d -sm */
8102 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
8103 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
06a2c219
GM
8104
8105 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8106 if (pixel != -1)
8107 {
8108 XtSetArg (av[ac], XtNforeground, pixel);
8109 ++ac;
8110 }
8111
8112 pixel = f->output_data.x->scroll_bar_background_pixel;
8113 if (pixel != -1)
8114 {
8115 XtSetArg (av[ac], XtNbackground, pixel);
8116 ++ac;
8117 }
8118
8119 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8120 f->output_data.x->edit_widget, av, ac);
ec18280f
SM
8121
8122 {
8123 char *initial = "";
8124 char *val = initial;
8125 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8126 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8127 if (val == initial)
8128 { /* ARROW_SCROLL */
8129 xaw3d_arrow_scroll = True;
8130 /* Isn't that just a personal preference ? -sm */
8131 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8132 }
8133 }
06a2c219
GM
8134
8135 /* Define callbacks. */
ec18280f
SM
8136 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8137 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
06a2c219
GM
8138 (XtPointer) bar);
8139
8140 /* Realize the widget. Only after that is the X window created. */
8141 XtRealizeWidget (widget);
8142
ec18280f 8143#endif /* !USE_MOTIF */
06a2c219
GM
8144
8145 /* Install an action hook that let's us detect when the user
8146 finishes interacting with a scroll bar. */
8147 if (action_hook_id == 0)
8148 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8149
8150 /* Remember X window and widget in the scroll bar vector. */
8151 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8152 xwindow = XtWindow (widget);
8153 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8154
8155 UNBLOCK_INPUT;
8156}
8157
8158
8159/* Set the thumb size and position of scroll bar BAR. We are currently
8160 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8161
8162static void
8163x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8164 struct scroll_bar *bar;
8165 int portion, position, whole;
f451eb13 8166{
e83dc917
GM
8167 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8168 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
06a2c219 8169 float top, shown;
f451eb13 8170
06a2c219
GM
8171 if (whole == 0)
8172 top = 0, shown = 1;
8173 else
f451eb13 8174 {
06a2c219
GM
8175 top = (float) position / whole;
8176 shown = (float) portion / whole;
8177 }
f451eb13 8178
06a2c219 8179 BLOCK_INPUT;
f451eb13 8180
06a2c219
GM
8181#ifdef USE_MOTIF
8182 {
8183 int size, value;
8184 Boolean arrow1_selected, arrow2_selected;
8185 unsigned char flags;
8186 XmScrollBarWidget sb;
8187
ec18280f 8188 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
06a2c219
GM
8189 is the scroll bar's maximum and MIN is the scroll bar's minimum
8190 value. */
8191 size = shown * XM_SB_RANGE;
8192 size = min (size, XM_SB_RANGE);
8193 size = max (size, 1);
8194
8195 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8196 value = top * XM_SB_RANGE;
8197 value = min (value, XM_SB_MAX - size);
8198 value = max (value, XM_SB_MIN);
8199
8200 /* LessTif: Calling XmScrollBarSetValues after an increment or
8201 decrement turns off auto-repeat LessTif-internally. This can
8202 be seen in ScrollBar.c which resets Arrow1Selected and
8203 Arrow2Selected. It also sets internal flags so that LessTif
8204 believes the mouse is in the slider. We either have to change
8205 our code, or work around that by accessing private data. */
8206
8207 sb = (XmScrollBarWidget) widget;
8208 arrow1_selected = sb->scrollBar.arrow1_selected;
8209 arrow2_selected = sb->scrollBar.arrow2_selected;
8210 flags = sb->scrollBar.flags;
8211
8212 if (NILP (bar->dragging))
8213 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8214 else if (last_scroll_bar_part == scroll_bar_down_arrow)
8215 /* This has the negative side effect that the slider value is
ec18280f 8216 not what it would be if we scrolled here using line-wise or
06a2c219
GM
8217 page-wise movement. */
8218 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
8219 else
8220 {
8221 /* If currently dragging, only update the slider size.
8222 This reduces flicker effects. */
8223 int old_value, old_size, increment, page_increment;
8224
8225 XmScrollBarGetValues (widget, &old_value, &old_size,
8226 &increment, &page_increment);
8227 XmScrollBarSetValues (widget, old_value,
8228 min (size, XM_SB_RANGE - old_value),
8229 0, 0, False);
8230 }
8231
8232 sb->scrollBar.arrow1_selected = arrow1_selected;
8233 sb->scrollBar.arrow2_selected = arrow2_selected;
8234 sb->scrollBar.flags = flags;
8235 }
ec18280f 8236#else /* !USE_MOTIF i.e. use Xaw */
06a2c219 8237 {
ec18280f
SM
8238 float old_top, old_shown;
8239 Dimension height;
8240 XtVaGetValues (widget,
8241 XtNtopOfThumb, &old_top,
8242 XtNshown, &old_shown,
8243 XtNheight, &height,
8244 NULL);
8245
8246 /* Massage the top+shown values. */
8247 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8248 top = max (0, min (1, top));
8249 else
8250 top = old_top;
8251 /* Keep two pixels available for moving the thumb down. */
8252 shown = max (0, min (1 - top - (2.0 / height), shown));
06a2c219
GM
8253
8254 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8255 check that your system's configuration file contains a define
8256 for `NARROWPROTO'. See s/freebsd.h for an example. */
ec18280f 8257 if (top != old_top || shown != old_shown)
eb393530 8258 {
ec18280f 8259 if (NILP (bar->dragging))
eb393530 8260 XawScrollbarSetThumb (widget, top, shown);
06a2c219
GM
8261 else
8262 {
ec18280f
SM
8263#ifdef HAVE_XAW3D
8264 ScrollbarWidget sb = (ScrollbarWidget) widget;
3e71d8f2 8265 int scroll_mode = 0;
ec18280f
SM
8266
8267 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8268 if (xaw3d_arrow_scroll)
8269 {
8270 /* Xaw3d stupidly ignores resize requests while dragging
8271 so we have to make it believe it's not in dragging mode. */
8272 scroll_mode = sb->scrollbar.scroll_mode;
8273 if (scroll_mode == 2)
8274 sb->scrollbar.scroll_mode = 0;
8275 }
8276#endif
8277 /* Try to make the scrolling a tad smoother. */
8278 if (!xaw3d_pick_top)
8279 shown = min (shown, old_shown);
8280
8281 XawScrollbarSetThumb (widget, top, shown);
8282
8283#ifdef HAVE_XAW3D
8284 if (xaw3d_arrow_scroll && scroll_mode == 2)
8285 sb->scrollbar.scroll_mode = scroll_mode;
8286#endif
06a2c219 8287 }
06a2c219
GM
8288 }
8289 }
ec18280f 8290#endif /* !USE_MOTIF */
06a2c219
GM
8291
8292 UNBLOCK_INPUT;
f451eb13
JB
8293}
8294
06a2c219
GM
8295#endif /* USE_TOOLKIT_SCROLL_BARS */
8296
8297
8298\f
8299/************************************************************************
8300 Scroll bars, general
8301 ************************************************************************/
8302
8303/* Create a scroll bar and return the scroll bar vector for it. W is
8304 the Emacs window on which to create the scroll bar. TOP, LEFT,
8305 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8306 scroll bar. */
8307
ab648270 8308static struct scroll_bar *
06a2c219
GM
8309x_scroll_bar_create (w, top, left, width, height)
8310 struct window *w;
f451eb13
JB
8311 int top, left, width, height;
8312{
06a2c219 8313 struct frame *f = XFRAME (w->frame);
334208b7
RS
8314 struct scroll_bar *bar
8315 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
f451eb13
JB
8316
8317 BLOCK_INPUT;
8318
eccc05db 8319#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
8320 x_create_toolkit_scroll_bar (f, bar);
8321#else /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8322 {
8323 XSetWindowAttributes a;
8324 unsigned long mask;
5c187dee 8325 Window window;
06a2c219
GM
8326
8327 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8328 if (a.background_pixel == -1)
8329 a.background_pixel = f->output_data.x->background_pixel;
8330
12ba150f 8331 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9a572e2a 8332 | ButtonMotionMask | PointerMotionHintMask
12ba150f 8333 | ExposureMask);
7a13e894 8334 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
f451eb13 8335
dbc4e1c1 8336 mask = (CWBackPixel | CWEventMask | CWCursor);
f451eb13 8337
06a2c219
GM
8338 /* Clear the area of W that will serve as a scroll bar. This is
8339 for the case that a window has been split horizontally. In
8340 this case, no clear_frame is generated to reduce flickering. */
8341 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8342 left, top, width,
8343 window_box_height (w), False);
8344
8345 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8346 /* Position and size of scroll bar. */
8347 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8348 top,
8349 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8350 height,
8351 /* Border width, depth, class, and visual. */
8352 0,
8353 CopyFromParent,
8354 CopyFromParent,
8355 CopyFromParent,
8356 /* Attributes. */
8357 mask, &a);
8358 SET_SCROLL_BAR_X_WINDOW (bar, window);
f451eb13 8359 }
06a2c219 8360#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 8361
06a2c219 8362 XSETWINDOW (bar->window, w);
e0c1aef2
KH
8363 XSETINT (bar->top, top);
8364 XSETINT (bar->left, left);
8365 XSETINT (bar->width, width);
8366 XSETINT (bar->height, height);
8367 XSETINT (bar->start, 0);
8368 XSETINT (bar->end, 0);
12ba150f 8369 bar->dragging = Qnil;
f451eb13
JB
8370
8371 /* Add bar to its frame's list of scroll bars. */
334208b7 8372 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 8373 bar->prev = Qnil;
334208b7 8374 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
06a2c219 8375 if (!NILP (bar->next))
e0c1aef2 8376 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13 8377
06a2c219 8378 /* Map the window/widget. */
eccc05db 8379#ifdef USE_TOOLKIT_SCROLL_BARS
e83dc917
GM
8380 {
8381 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8382 XtConfigureWidget (scroll_bar,
06a2c219
GM
8383 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8384 top,
8385 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8386 height, 0);
e83dc917
GM
8387 XtMapWidget (scroll_bar);
8388 }
06a2c219 8389#else /* not USE_TOOLKIT_SCROLL_BARS */
7f9c7f94 8390 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
06a2c219 8391#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8392
8393 UNBLOCK_INPUT;
12ba150f 8394 return bar;
f451eb13
JB
8395}
8396
06a2c219 8397
12ba150f 8398/* Draw BAR's handle in the proper position.
06a2c219 8399
12ba150f
JB
8400 If the handle is already drawn from START to END, don't bother
8401 redrawing it, unless REBUILD is non-zero; in that case, always
8402 redraw it. (REBUILD is handy for drawing the handle after expose
58769bee 8403 events.)
12ba150f
JB
8404
8405 Normally, we want to constrain the start and end of the handle to
06a2c219
GM
8406 fit inside its rectangle, but if the user is dragging the scroll
8407 bar handle, we want to let them drag it down all the way, so that
8408 the bar's top is as far down as it goes; otherwise, there's no way
8409 to move to the very end of the buffer. */
8410
5c187dee
GM
8411#ifndef USE_TOOLKIT_SCROLL_BARS
8412
f451eb13 8413static void
ab648270
JB
8414x_scroll_bar_set_handle (bar, start, end, rebuild)
8415 struct scroll_bar *bar;
f451eb13 8416 int start, end;
12ba150f 8417 int rebuild;
f451eb13 8418{
12ba150f 8419 int dragging = ! NILP (bar->dragging);
ab648270 8420 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8421 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8422 GC gc = f->output_data.x->normal_gc;
12ba150f
JB
8423
8424 /* If the display is already accurate, do nothing. */
8425 if (! rebuild
8426 && start == XINT (bar->start)
8427 && end == XINT (bar->end))
8428 return;
8429
f451eb13
JB
8430 BLOCK_INPUT;
8431
8432 {
d9cdbb3d
RS
8433 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8434 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8435 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
f451eb13
JB
8436
8437 /* Make sure the values are reasonable, and try to preserve
8438 the distance between start and end. */
12ba150f
JB
8439 {
8440 int length = end - start;
8441
8442 if (start < 0)
8443 start = 0;
8444 else if (start > top_range)
8445 start = top_range;
8446 end = start + length;
8447
8448 if (end < start)
8449 end = start;
8450 else if (end > top_range && ! dragging)
8451 end = top_range;
8452 }
f451eb13 8453
ab648270 8454 /* Store the adjusted setting in the scroll bar. */
e0c1aef2
KH
8455 XSETINT (bar->start, start);
8456 XSETINT (bar->end, end);
f451eb13 8457
12ba150f
JB
8458 /* Clip the end position, just for display. */
8459 if (end > top_range)
8460 end = top_range;
f451eb13 8461
ab648270 8462 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
12ba150f
JB
8463 below top positions, to make sure the handle is always at least
8464 that many pixels tall. */
ab648270 8465 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
f451eb13 8466
12ba150f
JB
8467 /* Draw the empty space above the handle. Note that we can't clear
8468 zero-height areas; that means "clear to end of window." */
8469 if (0 < start)
334208b7 8470 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 8471
12ba150f 8472 /* x, y, width, height, and exposures. */
ab648270
JB
8473 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8474 VERTICAL_SCROLL_BAR_TOP_BORDER,
12ba150f
JB
8475 inside_width, start,
8476 False);
f451eb13 8477
06a2c219
GM
8478 /* Change to proper foreground color if one is specified. */
8479 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8480 XSetForeground (FRAME_X_DISPLAY (f), gc,
8481 f->output_data.x->scroll_bar_foreground_pixel);
8482
12ba150f 8483 /* Draw the handle itself. */
334208b7 8484 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13 8485
12ba150f 8486 /* x, y, width, height */
ab648270
JB
8487 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8488 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
12ba150f 8489 inside_width, end - start);
f451eb13 8490
06a2c219
GM
8491 /* Restore the foreground color of the GC if we changed it above. */
8492 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8493 XSetForeground (FRAME_X_DISPLAY (f), gc,
8494 f->output_data.x->foreground_pixel);
f451eb13 8495
12ba150f
JB
8496 /* Draw the empty space below the handle. Note that we can't
8497 clear zero-height areas; that means "clear to end of window." */
8498 if (end < inside_height)
334208b7 8499 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 8500
12ba150f 8501 /* x, y, width, height, and exposures. */
ab648270
JB
8502 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8503 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
12ba150f
JB
8504 inside_width, inside_height - end,
8505 False);
f451eb13 8506
f451eb13
JB
8507 }
8508
f451eb13
JB
8509 UNBLOCK_INPUT;
8510}
8511
5c187dee 8512#endif /* !USE_TOOLKIT_SCROLL_BARS */
f451eb13 8513
06a2c219
GM
8514/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8515 nil. */
58769bee 8516
12ba150f 8517static void
ab648270
JB
8518x_scroll_bar_remove (bar)
8519 struct scroll_bar *bar;
12ba150f 8520{
e83dc917 8521 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
8522 BLOCK_INPUT;
8523
eccc05db 8524#ifdef USE_TOOLKIT_SCROLL_BARS
e83dc917
GM
8525 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
8526#else
8527 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8528#endif
06a2c219 8529
ab648270
JB
8530 /* Disassociate this scroll bar from its window. */
8531 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
12ba150f
JB
8532
8533 UNBLOCK_INPUT;
8534}
8535
06a2c219 8536
12ba150f
JB
8537/* Set the handle of the vertical scroll bar for WINDOW to indicate
8538 that we are displaying PORTION characters out of a total of WHOLE
ab648270 8539 characters, starting at POSITION. If WINDOW has no scroll bar,
12ba150f 8540 create one. */
06a2c219 8541
12ba150f 8542static void
06a2c219
GM
8543XTset_vertical_scroll_bar (w, portion, whole, position)
8544 struct window *w;
f451eb13
JB
8545 int portion, whole, position;
8546{
06a2c219 8547 struct frame *f = XFRAME (w->frame);
ab648270 8548 struct scroll_bar *bar;
3c6ede7b 8549 int top, height, left, sb_left, width, sb_width;
06a2c219 8550 int window_x, window_y, window_width, window_height;
06a2c219 8551
3c6ede7b 8552 /* Get window dimensions. */
06a2c219 8553 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
3c6ede7b
GM
8554 top = window_y;
8555 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8556 height = window_height;
06a2c219 8557
3c6ede7b 8558 /* Compute the left edge of the scroll bar area. */
06a2c219 8559 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
3c6ede7b
GM
8560 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8561 else
8562 left = XFASTINT (w->left);
8563 left *= CANON_X_UNIT (f);
8564 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8565
8566 /* Compute the width of the scroll bar which might be less than
8567 the width of the area reserved for the scroll bar. */
8568 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8569 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
06a2c219 8570 else
3c6ede7b 8571 sb_width = width;
12ba150f 8572
3c6ede7b
GM
8573 /* Compute the left edge of the scroll bar. */
8574#ifdef USE_TOOLKIT_SCROLL_BARS
8575 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8576 sb_left = left + width - sb_width - (width - sb_width) / 2;
8577 else
8578 sb_left = left + (width - sb_width) / 2;
8579#else
8580 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8581 sb_left = left + width - sb_width;
8582 else
8583 sb_left = left;
8584#endif
8585
ab648270 8586 /* Does the scroll bar exist yet? */
06a2c219 8587 if (NILP (w->vertical_scroll_bar))
3c6ede7b 8588 {
80c32bcc 8589 BLOCK_INPUT;
3c6ede7b
GM
8590 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8591 left, top, width, height, False);
80c32bcc 8592 UNBLOCK_INPUT;
3c6ede7b
GM
8593 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8594 }
f451eb13 8595 else
12ba150f
JB
8596 {
8597 /* It may just need to be moved and resized. */
06a2c219
GM
8598 unsigned int mask = 0;
8599
8600 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8601
8602 BLOCK_INPUT;
8603
3c6ede7b 8604 if (sb_left != XINT (bar->left))
06a2c219 8605 mask |= CWX;
3c6ede7b 8606 if (top != XINT (bar->top))
06a2c219 8607 mask |= CWY;
3c6ede7b 8608 if (sb_width != XINT (bar->width))
06a2c219 8609 mask |= CWWidth;
3c6ede7b 8610 if (height != XINT (bar->height))
06a2c219
GM
8611 mask |= CWHeight;
8612
8613#ifdef USE_TOOLKIT_SCROLL_BARS
fe6f39d9
GM
8614
8615 /* Since toolkit scroll bars are smaller than the space reserved
8616 for them on the frame, we have to clear "under" them. */
8617 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3c6ede7b 8618 left, top, width, height, False);
06a2c219
GM
8619
8620 /* Move/size the scroll bar widget. */
8621 if (mask)
e83dc917 8622 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
3c6ede7b
GM
8623 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8624 top,
8625 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8626 height, 0);
06a2c219
GM
8627
8628#else /* not USE_TOOLKIT_SCROLL_BARS */
8629
e1f6572f
RS
8630 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8631 {
8632 /* Clear areas not covered by the scroll bar. This makes sure a
8633 previous mode line display is cleared after C-x 2 C-x 1, for
8634 example. Non-toolkit scroll bars are as wide as the area
8635 reserved for scroll bars - trim at both sides. */
8636 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8637 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8638 height, False);
8639 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8640 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8641 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8642 height, False);
8643 }
06a2c219
GM
8644
8645 /* Move/size the scroll bar window. */
8646 if (mask)
8647 {
8648 XWindowChanges wc;
8649
3c6ede7b
GM
8650 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8651 wc.y = top;
8652 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8653 wc.height = height;
06a2c219
GM
8654 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8655 mask, &wc);
8656 }
8657
8658#endif /* not USE_TOOLKIT_SCROLL_BARS */
8659
8660 /* Remember new settings. */
3c6ede7b
GM
8661 XSETINT (bar->left, sb_left);
8662 XSETINT (bar->top, top);
8663 XSETINT (bar->width, sb_width);
8664 XSETINT (bar->height, height);
06a2c219
GM
8665
8666 UNBLOCK_INPUT;
12ba150f 8667 }
f451eb13 8668
eccc05db 8669#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
8670 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8671#else /* not USE_TOOLKIT_SCROLL_BARS */
ab648270 8672 /* Set the scroll bar's current state, unless we're currently being
f451eb13 8673 dragged. */
12ba150f 8674 if (NILP (bar->dragging))
f451eb13 8675 {
92857db0 8676 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
f451eb13 8677
12ba150f 8678 if (whole == 0)
ab648270 8679 x_scroll_bar_set_handle (bar, 0, top_range, 0);
12ba150f
JB
8680 else
8681 {
43f868f5
JB
8682 int start = ((double) position * top_range) / whole;
8683 int end = ((double) (position + portion) * top_range) / whole;
ab648270 8684 x_scroll_bar_set_handle (bar, start, end, 0);
12ba150f 8685 }
f451eb13 8686 }
06a2c219 8687#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 8688
06a2c219 8689 XSETVECTOR (w->vertical_scroll_bar, bar);
f451eb13
JB
8690}
8691
12ba150f 8692
f451eb13 8693/* The following three hooks are used when we're doing a thorough
ab648270 8694 redisplay of the frame. We don't explicitly know which scroll bars
f451eb13 8695 are going to be deleted, because keeping track of when windows go
12ba150f
JB
8696 away is a real pain - "Can you say set-window-configuration, boys
8697 and girls?" Instead, we just assert at the beginning of redisplay
ab648270 8698 that *all* scroll bars are to be removed, and then save a scroll bar
12ba150f 8699 from the fiery pit when we actually redisplay its window. */
f451eb13 8700
ab648270
JB
8701/* Arrange for all scroll bars on FRAME to be removed at the next call
8702 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
06a2c219
GM
8703 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8704
58769bee 8705static void
ab648270 8706XTcondemn_scroll_bars (frame)
f451eb13
JB
8707 FRAME_PTR frame;
8708{
f9e24cb9
RS
8709 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8710 while (! NILP (FRAME_SCROLL_BARS (frame)))
8711 {
8712 Lisp_Object bar;
8713 bar = FRAME_SCROLL_BARS (frame);
8714 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8715 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8716 XSCROLL_BAR (bar)->prev = Qnil;
8717 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8718 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8719 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8720 }
f451eb13
JB
8721}
8722
fa2dfc30 8723
06a2c219 8724/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
12ba150f 8725 Note that WINDOW isn't necessarily condemned at all. */
fa2dfc30 8726
f451eb13 8727static void
ab648270 8728XTredeem_scroll_bar (window)
12ba150f 8729 struct window *window;
f451eb13 8730{
ab648270 8731 struct scroll_bar *bar;
fa2dfc30 8732 struct frame *f;
12ba150f 8733
ab648270
JB
8734 /* We can't redeem this window's scroll bar if it doesn't have one. */
8735 if (NILP (window->vertical_scroll_bar))
12ba150f
JB
8736 abort ();
8737
ab648270 8738 bar = XSCROLL_BAR (window->vertical_scroll_bar);
12ba150f
JB
8739
8740 /* Unlink it from the condemned list. */
fa2dfc30
GM
8741 f = XFRAME (WINDOW_FRAME (window));
8742 if (NILP (bar->prev))
8743 {
8744 /* If the prev pointer is nil, it must be the first in one of
8745 the lists. */
8746 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
8747 /* It's not condemned. Everything's fine. */
8748 return;
8749 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8750 window->vertical_scroll_bar))
8751 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
8752 else
8753 /* If its prev pointer is nil, it must be at the front of
8754 one or the other! */
8755 abort ();
8756 }
8757 else
8758 XSCROLL_BAR (bar->prev)->next = bar->next;
12ba150f 8759
fa2dfc30
GM
8760 if (! NILP (bar->next))
8761 XSCROLL_BAR (bar->next)->prev = bar->prev;
12ba150f 8762
fa2dfc30
GM
8763 bar->next = FRAME_SCROLL_BARS (f);
8764 bar->prev = Qnil;
8765 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8766 if (! NILP (bar->next))
8767 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13
JB
8768}
8769
ab648270
JB
8770/* Remove all scroll bars on FRAME that haven't been saved since the
8771 last call to `*condemn_scroll_bars_hook'. */
06a2c219 8772
f451eb13 8773static void
ab648270 8774XTjudge_scroll_bars (f)
12ba150f 8775 FRAME_PTR f;
f451eb13 8776{
12ba150f 8777 Lisp_Object bar, next;
f451eb13 8778
ab648270 8779 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
cf7cb199
JB
8780
8781 /* Clear out the condemned list now so we won't try to process any
ab648270
JB
8782 more events on the hapless scroll bars. */
8783 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
cf7cb199
JB
8784
8785 for (; ! NILP (bar); bar = next)
f451eb13 8786 {
ab648270 8787 struct scroll_bar *b = XSCROLL_BAR (bar);
12ba150f 8788
ab648270 8789 x_scroll_bar_remove (b);
12ba150f
JB
8790
8791 next = b->next;
8792 b->next = b->prev = Qnil;
f451eb13 8793 }
12ba150f 8794
ab648270 8795 /* Now there should be no references to the condemned scroll bars,
12ba150f 8796 and they should get garbage-collected. */
f451eb13
JB
8797}
8798
8799
06a2c219
GM
8800/* Handle an Expose or GraphicsExpose event on a scroll bar. This
8801 is a no-op when using toolkit scroll bars.
ab648270
JB
8802
8803 This may be called from a signal handler, so we have to ignore GC
8804 mark bits. */
06a2c219 8805
f451eb13 8806static void
ab648270
JB
8807x_scroll_bar_expose (bar, event)
8808 struct scroll_bar *bar;
f451eb13
JB
8809 XEvent *event;
8810{
06a2c219
GM
8811#ifndef USE_TOOLKIT_SCROLL_BARS
8812
ab648270 8813 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8814 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8815 GC gc = f->output_data.x->normal_gc;
3cbd2e0b 8816 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
12ba150f 8817
f451eb13
JB
8818 BLOCK_INPUT;
8819
ab648270 8820 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
f451eb13 8821
06a2c219 8822 /* Draw a one-pixel border just inside the edges of the scroll bar. */
334208b7 8823 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13
JB
8824
8825 /* x, y, width, height */
d9cdbb3d 8826 0, 0,
3cbd2e0b 8827 XINT (bar->width) - 1 - width_trim - width_trim,
d9cdbb3d
RS
8828 XINT (bar->height) - 1);
8829
f451eb13 8830 UNBLOCK_INPUT;
06a2c219
GM
8831
8832#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8833}
8834
ab648270
JB
8835/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8836 is set to something other than no_event, it is enqueued.
8837
8838 This may be called from a signal handler, so we have to ignore GC
8839 mark bits. */
06a2c219 8840
5c187dee
GM
8841#ifndef USE_TOOLKIT_SCROLL_BARS
8842
f451eb13 8843static void
ab648270
JB
8844x_scroll_bar_handle_click (bar, event, emacs_event)
8845 struct scroll_bar *bar;
f451eb13
JB
8846 XEvent *event;
8847 struct input_event *emacs_event;
8848{
0299d313 8849 if (! GC_WINDOWP (bar->window))
12ba150f
JB
8850 abort ();
8851
ab648270 8852 emacs_event->kind = scroll_bar_click;
69388238 8853 emacs_event->code = event->xbutton.button - Button1;
0299d313
RS
8854 emacs_event->modifiers
8855 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8856 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8857 event->xbutton.state)
8858 | (event->type == ButtonRelease
8859 ? up_modifier
8860 : down_modifier));
12ba150f 8861 emacs_event->frame_or_window = bar->window;
0f8aabe9 8862 emacs_event->arg = Qnil;
f451eb13 8863 emacs_event->timestamp = event->xbutton.time;
12ba150f 8864 {
06a2c219 8865#if 0
d9cdbb3d 8866 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
0299d313 8867 int internal_height
d9cdbb3d 8868 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8869#endif
0299d313 8870 int top_range
d9cdbb3d 8871 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ab648270 8872 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
12ba150f
JB
8873
8874 if (y < 0) y = 0;
8875 if (y > top_range) y = top_range;
8876
8877 if (y < XINT (bar->start))
ab648270
JB
8878 emacs_event->part = scroll_bar_above_handle;
8879 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8880 emacs_event->part = scroll_bar_handle;
12ba150f 8881 else
ab648270 8882 emacs_event->part = scroll_bar_below_handle;
929787e1
JB
8883
8884 /* Just because the user has clicked on the handle doesn't mean
5116f055
JB
8885 they want to drag it. Lisp code needs to be able to decide
8886 whether or not we're dragging. */
929787e1 8887#if 0
12ba150f
JB
8888 /* If the user has just clicked on the handle, record where they're
8889 holding it. */
8890 if (event->type == ButtonPress
ab648270 8891 && emacs_event->part == scroll_bar_handle)
e0c1aef2 8892 XSETINT (bar->dragging, y - XINT (bar->start));
929787e1 8893#endif
12ba150f
JB
8894
8895 /* If the user has released the handle, set it to its final position. */
8896 if (event->type == ButtonRelease
8897 && ! NILP (bar->dragging))
8898 {
8899 int new_start = y - XINT (bar->dragging);
8900 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
f451eb13 8901
ab648270 8902 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
12ba150f
JB
8903 bar->dragging = Qnil;
8904 }
f451eb13 8905
5116f055
JB
8906 /* Same deal here as the other #if 0. */
8907#if 0
58769bee 8908 /* Clicks on the handle are always reported as occurring at the top of
12ba150f 8909 the handle. */
ab648270 8910 if (emacs_event->part == scroll_bar_handle)
12ba150f
JB
8911 emacs_event->x = bar->start;
8912 else
e0c1aef2 8913 XSETINT (emacs_event->x, y);
5116f055 8914#else
e0c1aef2 8915 XSETINT (emacs_event->x, y);
5116f055 8916#endif
f451eb13 8917
e0c1aef2 8918 XSETINT (emacs_event->y, top_range);
12ba150f
JB
8919 }
8920}
f451eb13 8921
ab648270
JB
8922/* Handle some mouse motion while someone is dragging the scroll bar.
8923
8924 This may be called from a signal handler, so we have to ignore GC
8925 mark bits. */
06a2c219 8926
f451eb13 8927static void
ab648270
JB
8928x_scroll_bar_note_movement (bar, event)
8929 struct scroll_bar *bar;
f451eb13
JB
8930 XEvent *event;
8931{
39d8bb4d
KH
8932 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8933
f451eb13
JB
8934 last_mouse_movement_time = event->xmotion.time;
8935
39d8bb4d 8936 f->mouse_moved = 1;
e0c1aef2 8937 XSETVECTOR (last_mouse_scroll_bar, bar);
f451eb13
JB
8938
8939 /* If we're dragging the bar, display it. */
ab648270 8940 if (! GC_NILP (bar->dragging))
f451eb13
JB
8941 {
8942 /* Where should the handle be now? */
12ba150f 8943 int new_start = event->xmotion.y - XINT (bar->dragging);
f451eb13 8944
12ba150f 8945 if (new_start != XINT (bar->start))
f451eb13 8946 {
12ba150f 8947 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
58769bee 8948
ab648270 8949 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
f451eb13
JB
8950 }
8951 }
f451eb13
JB
8952}
8953
5c187dee
GM
8954#endif /* !USE_TOOLKIT_SCROLL_BARS */
8955
12ba150f 8956/* Return information to the user about the current position of the mouse
ab648270 8957 on the scroll bar. */
06a2c219 8958
12ba150f 8959static void
334208b7
RS
8960x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8961 FRAME_PTR *fp;
12ba150f 8962 Lisp_Object *bar_window;
ab648270 8963 enum scroll_bar_part *part;
12ba150f
JB
8964 Lisp_Object *x, *y;
8965 unsigned long *time;
8966{
ab648270 8967 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
334208b7
RS
8968 Window w = SCROLL_BAR_X_WINDOW (bar);
8969 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f 8970 int win_x, win_y;
559cb2fb
JB
8971 Window dummy_window;
8972 int dummy_coord;
8973 unsigned int dummy_mask;
12ba150f 8974
cf7cb199
JB
8975 BLOCK_INPUT;
8976
ab648270 8977 /* Get the mouse's position relative to the scroll bar window, and
12ba150f 8978 report that. */
334208b7 8979 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
12ba150f 8980
559cb2fb
JB
8981 /* Root, child, root x and root y. */
8982 &dummy_window, &dummy_window,
8983 &dummy_coord, &dummy_coord,
12ba150f 8984
559cb2fb
JB
8985 /* Position relative to scroll bar. */
8986 &win_x, &win_y,
12ba150f 8987
559cb2fb
JB
8988 /* Mouse buttons and modifier keys. */
8989 &dummy_mask))
7a13e894 8990 ;
559cb2fb
JB
8991 else
8992 {
06a2c219 8993#if 0
559cb2fb 8994 int inside_height
d9cdbb3d 8995 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8996#endif
559cb2fb 8997 int top_range
d9cdbb3d 8998 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
559cb2fb
JB
8999
9000 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
9001
9002 if (! NILP (bar->dragging))
9003 win_y -= XINT (bar->dragging);
9004
9005 if (win_y < 0)
9006 win_y = 0;
9007 if (win_y > top_range)
9008 win_y = top_range;
9009
334208b7 9010 *fp = f;
7a13e894 9011 *bar_window = bar->window;
559cb2fb
JB
9012
9013 if (! NILP (bar->dragging))
9014 *part = scroll_bar_handle;
9015 else if (win_y < XINT (bar->start))
9016 *part = scroll_bar_above_handle;
9017 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9018 *part = scroll_bar_handle;
9019 else
9020 *part = scroll_bar_below_handle;
12ba150f 9021
e0c1aef2
KH
9022 XSETINT (*x, win_y);
9023 XSETINT (*y, top_range);
12ba150f 9024
39d8bb4d 9025 f->mouse_moved = 0;
559cb2fb
JB
9026 last_mouse_scroll_bar = Qnil;
9027 }
12ba150f 9028
559cb2fb 9029 *time = last_mouse_movement_time;
cf7cb199 9030
cf7cb199 9031 UNBLOCK_INPUT;
12ba150f
JB
9032}
9033
f451eb13 9034
dbc4e1c1 9035/* The screen has been cleared so we may have changed foreground or
ab648270
JB
9036 background colors, and the scroll bars may need to be redrawn.
9037 Clear out the scroll bars, and ask for expose events, so we can
dbc4e1c1
JB
9038 redraw them. */
9039
dfcf069d 9040void
ab648270 9041x_scroll_bar_clear (f)
dbc4e1c1
JB
9042 FRAME_PTR f;
9043{
06a2c219 9044#ifndef USE_TOOLKIT_SCROLL_BARS
dbc4e1c1
JB
9045 Lisp_Object bar;
9046
b80c363e
RS
9047 /* We can have scroll bars even if this is 0,
9048 if we just turned off scroll bar mode.
9049 But in that case we should not clear them. */
9050 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9051 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9052 bar = XSCROLL_BAR (bar)->next)
9053 XClearArea (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
9054 0, 0, 0, 0, True);
06a2c219 9055#endif /* not USE_TOOLKIT_SCROLL_BARS */
dbc4e1c1
JB
9056}
9057
06a2c219 9058/* This processes Expose events from the menu-bar specific X event
19126e11 9059 loop in xmenu.c. This allows to redisplay the frame if necessary
06a2c219 9060 when handling menu-bar or pop-up items. */
3afe33e7 9061
06a2c219 9062int
3afe33e7
RS
9063process_expose_from_menu (event)
9064 XEvent event;
9065{
9066 FRAME_PTR f;
19126e11 9067 struct x_display_info *dpyinfo;
06a2c219 9068 int frame_exposed_p = 0;
3afe33e7 9069
f94397b5
KH
9070 BLOCK_INPUT;
9071
19126e11
KH
9072 dpyinfo = x_display_info_for_display (event.xexpose.display);
9073 f = x_window_to_frame (dpyinfo, event.xexpose.window);
3afe33e7
RS
9074 if (f)
9075 {
9076 if (f->async_visible == 0)
9077 {
9078 f->async_visible = 1;
9079 f->async_iconified = 0;
06c488fd 9080 f->output_data.x->has_been_visible = 1;
3afe33e7
RS
9081 SET_FRAME_GARBAGED (f);
9082 }
9083 else
9084 {
06a2c219
GM
9085 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9086 event.xexpose.x, event.xexpose.y,
9087 event.xexpose.width, event.xexpose.height);
9088 frame_exposed_p = 1;
3afe33e7
RS
9089 }
9090 }
9091 else
9092 {
9093 struct scroll_bar *bar
9094 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 9095
3afe33e7
RS
9096 if (bar)
9097 x_scroll_bar_expose (bar, &event);
9098 }
f94397b5
KH
9099
9100 UNBLOCK_INPUT;
06a2c219 9101 return frame_exposed_p;
3afe33e7 9102}
09756a85
RS
9103\f
9104/* Define a queue to save up SelectionRequest events for later handling. */
9105
9106struct selection_event_queue
9107 {
9108 XEvent event;
9109 struct selection_event_queue *next;
9110 };
9111
9112static struct selection_event_queue *queue;
9113
9114/* Nonzero means queue up certain events--don't process them yet. */
06a2c219 9115
09756a85
RS
9116static int x_queue_selection_requests;
9117
9118/* Queue up an X event *EVENT, to be processed later. */
dbc4e1c1 9119
09756a85 9120static void
334208b7
RS
9121x_queue_event (f, event)
9122 FRAME_PTR f;
09756a85
RS
9123 XEvent *event;
9124{
9125 struct selection_event_queue *queue_tmp
06a2c219 9126 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
09756a85 9127
58769bee 9128 if (queue_tmp != NULL)
09756a85
RS
9129 {
9130 queue_tmp->event = *event;
9131 queue_tmp->next = queue;
9132 queue = queue_tmp;
9133 }
9134}
9135
9136/* Take all the queued events and put them back
9137 so that they get processed afresh. */
9138
9139static void
db3906fd
RS
9140x_unqueue_events (display)
9141 Display *display;
09756a85 9142{
58769bee 9143 while (queue != NULL)
09756a85
RS
9144 {
9145 struct selection_event_queue *queue_tmp = queue;
db3906fd 9146 XPutBackEvent (display, &queue_tmp->event);
09756a85 9147 queue = queue_tmp->next;
06a2c219 9148 xfree ((char *)queue_tmp);
09756a85
RS
9149 }
9150}
9151
9152/* Start queuing SelectionRequest events. */
9153
9154void
db3906fd
RS
9155x_start_queuing_selection_requests (display)
9156 Display *display;
09756a85
RS
9157{
9158 x_queue_selection_requests++;
9159}
9160
9161/* Stop queuing SelectionRequest events. */
9162
9163void
db3906fd
RS
9164x_stop_queuing_selection_requests (display)
9165 Display *display;
09756a85
RS
9166{
9167 x_queue_selection_requests--;
db3906fd 9168 x_unqueue_events (display);
09756a85 9169}
f451eb13
JB
9170\f
9171/* The main X event-reading loop - XTread_socket. */
dc6f92b8 9172
06a2c219 9173/* Time stamp of enter window event. This is only used by XTread_socket,
dc6f92b8
JB
9174 but we have to put it out here, since static variables within functions
9175 sometimes don't work. */
06a2c219 9176
dc6f92b8
JB
9177static Time enter_timestamp;
9178
11edeb03 9179/* This holds the state XLookupString needs to implement dead keys
58769bee 9180 and other tricks known as "compose processing". _X Window System_
11edeb03
JB
9181 says that a portable program can't use this, but Stephen Gildea assures
9182 me that letting the compiler initialize it to zeros will work okay.
9183
9184 This must be defined outside of XTread_socket, for the same reasons
06a2c219
GM
9185 given for enter_time stamp, above. */
9186
11edeb03
JB
9187static XComposeStatus compose_status;
9188
10e6549c
RS
9189/* Record the last 100 characters stored
9190 to help debug the loss-of-chars-during-GC problem. */
06a2c219 9191
2224b905
RS
9192static int temp_index;
9193static short temp_buffer[100];
10e6549c 9194
7a13e894
RS
9195/* Set this to nonzero to fake an "X I/O error"
9196 on a particular display. */
06a2c219 9197
7a13e894
RS
9198struct x_display_info *XTread_socket_fake_io_error;
9199
2224b905
RS
9200/* When we find no input here, we occasionally do a no-op command
9201 to verify that the X server is still running and we can still talk with it.
9202 We try all the open displays, one by one.
9203 This variable is used for cycling thru the displays. */
06a2c219 9204
2224b905
RS
9205static struct x_display_info *next_noop_dpyinfo;
9206
06a2c219
GM
9207#define SET_SAVED_MENU_EVENT(size) \
9208 do \
9209 { \
9210 if (f->output_data.x->saved_menu_event == 0) \
9211 f->output_data.x->saved_menu_event \
9212 = (XEvent *) xmalloc (sizeof (XEvent)); \
9213 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9214 if (numchars >= 1) \
9215 { \
9216 bufp->kind = menu_bar_activate_event; \
9217 XSETFRAME (bufp->frame_or_window, f); \
0f8aabe9 9218 bufp->arg = Qnil; \
06a2c219
GM
9219 bufp++; \
9220 count++; \
9221 numchars--; \
9222 } \
9223 } \
9224 while (0)
9225
8805890a 9226#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
06a2c219 9227#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8805890a 9228
dc6f92b8
JB
9229/* Read events coming from the X server.
9230 This routine is called by the SIGIO handler.
9231 We return as soon as there are no more events to be read.
9232
9233 Events representing keys are stored in buffer BUFP,
9234 which can hold up to NUMCHARS characters.
9235 We return the number of characters stored into the buffer,
9236 thus pretending to be `read'.
9237
dc6f92b8
JB
9238 EXPECTED is nonzero if the caller knows input is available. */
9239
7c5283e4 9240int
f66868ba 9241XTread_socket (sd, bufp, numchars, expected)
dc6f92b8 9242 register int sd;
8805890a
KH
9243 /* register */ struct input_event *bufp;
9244 /* register */ int numchars;
dc6f92b8
JB
9245 int expected;
9246{
9247 int count = 0;
9248 int nbytes = 0;
dc6f92b8 9249 XEvent event;
f676886a 9250 struct frame *f;
66f55a9d 9251 int event_found = 0;
334208b7 9252 struct x_display_info *dpyinfo;
379b5ac0 9253 struct coding_system coding;
dc6f92b8 9254
9ac0d9e0 9255 if (interrupt_input_blocked)
dc6f92b8 9256 {
9ac0d9e0 9257 interrupt_input_pending = 1;
dc6f92b8
JB
9258 return -1;
9259 }
9260
9ac0d9e0 9261 interrupt_input_pending = 0;
dc6f92b8 9262 BLOCK_INPUT;
c0a04927
RS
9263
9264 /* So people can tell when we have read the available input. */
9265 input_signal_count++;
9266
dc6f92b8 9267 if (numchars <= 0)
06a2c219 9268 abort (); /* Don't think this happens. */
dc6f92b8 9269
bde5503b
GM
9270 ++handling_signal;
9271
379b5ac0
KH
9272 /* The input should be decoded if it is from XIM. Currently the
9273 locale of XIM is the same as that of the system. So, we can use
9274 Vlocale_coding_system which is initialized properly at Emacs
9275 startup time. */
9276 setup_coding_system (Vlocale_coding_system, &coding);
9277 coding.src_multibyte = 0;
9278 coding.dst_multibyte = 1;
9279 /* The input is converted to events, thus we can't handle
9280 composition. Anyway, there's no XIM that gives us composition
9281 information. */
9282 coding.composing = COMPOSITION_DISABLED;
9283
7a13e894
RS
9284 /* Find the display we are supposed to read input for.
9285 It's the one communicating on descriptor SD. */
9286 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9287 {
9288#if 0 /* This ought to be unnecessary; let's verify it. */
dc6f92b8 9289#ifdef FIOSNBIO
7a13e894
RS
9290 /* If available, Xlib uses FIOSNBIO to make the socket
9291 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
e6cbea31 9292 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
06a2c219 9293 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7a13e894 9294 fcntl (dpyinfo->connection, F_SETFL, 0);
c118dd06 9295#endif /* ! defined (FIOSNBIO) */
7a13e894 9296#endif
dc6f92b8 9297
7a13e894
RS
9298#if 0 /* This code can't be made to work, with multiple displays,
9299 and appears not to be used on any system any more.
9300 Also keyboard.c doesn't turn O_NDELAY on and off
9301 for X connections. */
dc6f92b8
JB
9302#ifndef SIGIO
9303#ifndef HAVE_SELECT
7a13e894
RS
9304 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9305 {
9306 extern int read_alarm_should_throw;
9307 read_alarm_should_throw = 1;
9308 XPeekEvent (dpyinfo->display, &event);
9309 read_alarm_should_throw = 0;
9310 }
c118dd06
JB
9311#endif /* HAVE_SELECT */
9312#endif /* SIGIO */
7a13e894 9313#endif
dc6f92b8 9314
7a13e894
RS
9315 /* For debugging, this gives a way to fake an I/O error. */
9316 if (dpyinfo == XTread_socket_fake_io_error)
9317 {
9318 XTread_socket_fake_io_error = 0;
9319 x_io_error_quitter (dpyinfo->display);
9320 }
dc6f92b8 9321
06a2c219 9322 while (XPending (dpyinfo->display))
dc6f92b8 9323 {
7a13e894 9324 XNextEvent (dpyinfo->display, &event);
06a2c219 9325
531483fb 9326#ifdef HAVE_X_I18N
d1bc4182 9327 {
f2be1146
GM
9328 /* Filter events for the current X input method.
9329 XFilterEvent returns non-zero if the input method has
9330 consumed the event. We pass the frame's X window to
9331 XFilterEvent because that's the one for which the IC
9332 was created. */
f5d11644
GM
9333 struct frame *f1 = x_any_window_to_frame (dpyinfo,
9334 event.xclient.window);
9335 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
d1bc4182
RS
9336 break;
9337 }
0cd6403b 9338#endif
7a13e894
RS
9339 event_found = 1;
9340
9341 switch (event.type)
9342 {
9343 case ClientMessage:
c047688c 9344 {
7a13e894
RS
9345 if (event.xclient.message_type
9346 == dpyinfo->Xatom_wm_protocols
9347 && event.xclient.format == 32)
c047688c 9348 {
7a13e894
RS
9349 if (event.xclient.data.l[0]
9350 == dpyinfo->Xatom_wm_take_focus)
c047688c 9351 {
8c1a6a84
RS
9352 /* Use x_any_window_to_frame because this
9353 could be the shell widget window
9354 if the frame has no title bar. */
9355 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6c183ba5
RS
9356#ifdef HAVE_X_I18N
9357 /* Not quite sure this is needed -pd */
8c1a6a84 9358 if (f && FRAME_XIC (f))
6c183ba5
RS
9359 XSetICFocus (FRAME_XIC (f));
9360#endif
f1da8f06
GM
9361#if 0 /* Emacs sets WM hints whose `input' field is `true'. This
9362 instructs the WM to set the input focus automatically for
9363 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
9364 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
9365 it has set the focus. So, XSetInputFocus below is not
9366 needed.
9367
9368 The call to XSetInputFocus below has also caused trouble. In
9369 cases where the XSetInputFocus done by the WM and the one
9370 below are temporally close (on a fast machine), the call
9371 below can generate additional FocusIn events which confuse
9372 Emacs. */
9373
bf7253f4
RS
9374 /* Since we set WM_TAKE_FOCUS, we must call
9375 XSetInputFocus explicitly. But not if f is null,
9376 since that might be an event for a deleted frame. */
7a13e894 9377 if (f)
bf7253f4
RS
9378 {
9379 Display *d = event.xclient.display;
9380 /* Catch and ignore errors, in case window has been
9381 iconified by a window manager such as GWM. */
9382 int count = x_catch_errors (d);
9383 XSetInputFocus (d, event.xclient.window,
e1f6572f
RS
9384 /* The ICCCM says this is
9385 the only valid choice. */
9386 RevertToParent,
bf7253f4
RS
9387 event.xclient.data.l[1]);
9388 /* This is needed to detect the error
9389 if there is an error. */
9390 XSync (d, False);
9391 x_uncatch_errors (d, count);
9392 }
7a13e894 9393 /* Not certain about handling scroll bars here */
f1da8f06 9394#endif /* 0 */
c047688c 9395 }
7a13e894
RS
9396 else if (event.xclient.data.l[0]
9397 == dpyinfo->Xatom_wm_save_yourself)
9398 {
9399 /* Save state modify the WM_COMMAND property to
06a2c219 9400 something which can reinstate us. This notifies
7a13e894
RS
9401 the session manager, who's looking for such a
9402 PropertyNotify. Can restart processing when
06a2c219 9403 a keyboard or mouse event arrives. */
7a13e894
RS
9404 if (numchars > 0)
9405 {
19126e11
KH
9406 f = x_top_window_to_frame (dpyinfo,
9407 event.xclient.window);
7a13e894
RS
9408
9409 /* This is just so we only give real data once
9410 for a single Emacs process. */
b86bd3dd 9411 if (f == SELECTED_FRAME ())
7a13e894
RS
9412 XSetCommand (FRAME_X_DISPLAY (f),
9413 event.xclient.window,
9414 initial_argv, initial_argc);
f000f5c5 9415 else if (f)
7a13e894
RS
9416 XSetCommand (FRAME_X_DISPLAY (f),
9417 event.xclient.window,
9418 0, 0);
9419 }
9420 }
9421 else if (event.xclient.data.l[0]
9422 == dpyinfo->Xatom_wm_delete_window)
1fb20991 9423 {
19126e11
KH
9424 struct frame *f
9425 = x_any_window_to_frame (dpyinfo,
9426 event.xclient.window);
1fb20991 9427
7a13e894
RS
9428 if (f)
9429 {
9430 if (numchars == 0)
9431 abort ();
1fb20991 9432
7a13e894
RS
9433 bufp->kind = delete_window_event;
9434 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9435 bufp->arg = Qnil;
7a13e894
RS
9436 bufp++;
9437
9438 count += 1;
9439 numchars -= 1;
9440 }
1fb20991 9441 }
c047688c 9442 }
7a13e894
RS
9443 else if (event.xclient.message_type
9444 == dpyinfo->Xatom_wm_configure_denied)
9445 {
9446 }
9447 else if (event.xclient.message_type
9448 == dpyinfo->Xatom_wm_window_moved)
9449 {
9450 int new_x, new_y;
19126e11
KH
9451 struct frame *f
9452 = x_window_to_frame (dpyinfo, event.xclient.window);
58769bee 9453
7a13e894
RS
9454 new_x = event.xclient.data.s[0];
9455 new_y = event.xclient.data.s[1];
1fb20991 9456
7a13e894
RS
9457 if (f)
9458 {
7556890b
RS
9459 f->output_data.x->left_pos = new_x;
9460 f->output_data.x->top_pos = new_y;
7a13e894 9461 }
1fb20991 9462 }
0fdff6bb 9463#ifdef HACK_EDITRES
7a13e894
RS
9464 else if (event.xclient.message_type
9465 == dpyinfo->Xatom_editres)
9466 {
19126e11
KH
9467 struct frame *f
9468 = x_any_window_to_frame (dpyinfo, event.xclient.window);
7556890b 9469 _XEditResCheckMessages (f->output_data.x->widget, NULL,
19126e11 9470 &event, NULL);
7a13e894 9471 }
0fdff6bb 9472#endif /* HACK_EDITRES */
06a2c219
GM
9473 else if ((event.xclient.message_type
9474 == dpyinfo->Xatom_DONE)
9475 || (event.xclient.message_type
9476 == dpyinfo->Xatom_PAGE))
9477 {
9478 /* Ghostview job completed. Kill it. We could
9479 reply with "Next" if we received "Page", but we
9480 currently never do because we are interested in
9481 images, only, which should have 1 page. */
06a2c219
GM
9482 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9483 struct frame *f
9484 = x_window_to_frame (dpyinfo, event.xclient.window);
9485 x_kill_gs_process (pixmap, f);
9486 expose_frame (f, 0, 0, 0, 0);
9487 }
9488#ifdef USE_TOOLKIT_SCROLL_BARS
9489 /* Scroll bar callbacks send a ClientMessage from which
9490 we construct an input_event. */
9491 else if (event.xclient.message_type
9492 == dpyinfo->Xatom_Scrollbar)
9493 {
9494 x_scroll_bar_to_input_event (&event, bufp);
9495 ++bufp, ++count, --numchars;
9496 goto out;
9497 }
9498#endif /* USE_TOOLKIT_SCROLL_BARS */
9499 else
9500 goto OTHER;
7a13e894
RS
9501 }
9502 break;
dc6f92b8 9503
7a13e894 9504 case SelectionNotify:
3afe33e7 9505#ifdef USE_X_TOOLKIT
19126e11 9506 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
7a13e894 9507 goto OTHER;
3afe33e7 9508#endif /* not USE_X_TOOLKIT */
dfcf069d 9509 x_handle_selection_notify (&event.xselection);
7a13e894 9510 break;
d56a553a 9511
06a2c219 9512 case SelectionClear: /* Someone has grabbed ownership. */
3afe33e7 9513#ifdef USE_X_TOOLKIT
19126e11 9514 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
7a13e894 9515 goto OTHER;
3afe33e7 9516#endif /* USE_X_TOOLKIT */
7a13e894
RS
9517 {
9518 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
d56a553a 9519
7a13e894
RS
9520 if (numchars == 0)
9521 abort ();
d56a553a 9522
7a13e894
RS
9523 bufp->kind = selection_clear_event;
9524 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9525 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9526 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9527 bufp->frame_or_window = Qnil;
0f8aabe9 9528 bufp->arg = Qnil;
7a13e894 9529 bufp++;
d56a553a 9530
7a13e894
RS
9531 count += 1;
9532 numchars -= 1;
9533 }
9534 break;
dc6f92b8 9535
06a2c219 9536 case SelectionRequest: /* Someone wants our selection. */
3afe33e7 9537#ifdef USE_X_TOOLKIT
19126e11 9538 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
7a13e894 9539 goto OTHER;
3afe33e7 9540#endif /* USE_X_TOOLKIT */
7a13e894 9541 if (x_queue_selection_requests)
19126e11 9542 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
7a13e894
RS
9543 &event);
9544 else
9545 {
9546 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
dc6f92b8 9547
7a13e894
RS
9548 if (numchars == 0)
9549 abort ();
9550
9551 bufp->kind = selection_request_event;
9552 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9553 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9554 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9555 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9556 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9557 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9558 bufp->frame_or_window = Qnil;
0f8aabe9 9559 bufp->arg = Qnil;
7a13e894
RS
9560 bufp++;
9561
9562 count += 1;
9563 numchars -= 1;
9564 }
9565 break;
9566
9567 case PropertyNotify:
3afe33e7 9568#ifdef USE_X_TOOLKIT
19126e11 9569 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
7a13e894 9570 goto OTHER;
3afe33e7 9571#endif /* not USE_X_TOOLKIT */
dfcf069d 9572 x_handle_property_notify (&event.xproperty);
7a13e894 9573 break;
dc6f92b8 9574
7a13e894 9575 case ReparentNotify:
19126e11 9576 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
7a13e894
RS
9577 if (f)
9578 {
9579 int x, y;
7556890b 9580 f->output_data.x->parent_desc = event.xreparent.parent;
7a13e894 9581 x_real_positions (f, &x, &y);
7556890b
RS
9582 f->output_data.x->left_pos = x;
9583 f->output_data.x->top_pos = y;
7a13e894
RS
9584 }
9585 break;
3bd330d4 9586
7a13e894 9587 case Expose:
19126e11 9588 f = x_window_to_frame (dpyinfo, event.xexpose.window);
7a13e894 9589 if (f)
dc6f92b8 9590 {
7a13e894
RS
9591 if (f->async_visible == 0)
9592 {
9593 f->async_visible = 1;
9594 f->async_iconified = 0;
06c488fd 9595 f->output_data.x->has_been_visible = 1;
7a13e894
RS
9596 SET_FRAME_GARBAGED (f);
9597 }
9598 else
06a2c219
GM
9599 expose_frame (x_window_to_frame (dpyinfo,
9600 event.xexpose.window),
9601 event.xexpose.x, event.xexpose.y,
9602 event.xexpose.width, event.xexpose.height);
dc6f92b8
JB
9603 }
9604 else
7a13e894 9605 {
06a2c219
GM
9606#ifdef USE_TOOLKIT_SCROLL_BARS
9607 /* Dispatch event to the widget. */
9608 goto OTHER;
9609#else /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9610 struct scroll_bar *bar
9611 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 9612
7a13e894
RS
9613 if (bar)
9614 x_scroll_bar_expose (bar, &event);
3afe33e7 9615#ifdef USE_X_TOOLKIT
7a13e894
RS
9616 else
9617 goto OTHER;
3afe33e7 9618#endif /* USE_X_TOOLKIT */
06a2c219 9619#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9620 }
9621 break;
dc6f92b8 9622
7a13e894
RS
9623 case GraphicsExpose: /* This occurs when an XCopyArea's
9624 source area was obscured or not
9625 available.*/
19126e11 9626 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
7a13e894
RS
9627 if (f)
9628 {
06a2c219
GM
9629 expose_frame (f,
9630 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9631 event.xgraphicsexpose.width,
9632 event.xgraphicsexpose.height);
7a13e894 9633 }
3afe33e7 9634#ifdef USE_X_TOOLKIT
7a13e894
RS
9635 else
9636 goto OTHER;
3afe33e7 9637#endif /* USE_X_TOOLKIT */
7a13e894 9638 break;
dc6f92b8 9639
7a13e894 9640 case NoExpose: /* This occurs when an XCopyArea's
06a2c219
GM
9641 source area was completely
9642 available */
7a13e894 9643 break;
dc6f92b8 9644
7a13e894 9645 case UnmapNotify:
06a2c219
GM
9646 /* Redo the mouse-highlight after the tooltip has gone. */
9647 if (event.xmap.window == tip_window)
9648 {
9649 tip_window = 0;
9650 redo_mouse_highlight ();
9651 }
9652
91ea2a7a 9653 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
7a13e894
RS
9654 if (f) /* F may no longer exist if
9655 the frame was deleted. */
9656 {
9657 /* While a frame is unmapped, display generation is
9658 disabled; you don't want to spend time updating a
9659 display that won't ever be seen. */
9660 f->async_visible = 0;
9661 /* We can't distinguish, from the event, whether the window
9662 has become iconified or invisible. So assume, if it
9663 was previously visible, than now it is iconified.
1aa6072f
RS
9664 But x_make_frame_invisible clears both
9665 the visible flag and the iconified flag;
9666 and that way, we know the window is not iconified now. */
7a13e894 9667 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
1aa6072f
RS
9668 {
9669 f->async_iconified = 1;
bddd097c 9670
1aa6072f
RS
9671 bufp->kind = iconify_event;
9672 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9673 bufp->arg = Qnil;
1aa6072f
RS
9674 bufp++;
9675 count++;
9676 numchars--;
9677 }
7a13e894 9678 }
7a13e894 9679 goto OTHER;
dc6f92b8 9680
7a13e894 9681 case MapNotify:
06a2c219
GM
9682 if (event.xmap.window == tip_window)
9683 /* The tooltip has been drawn already. Avoid
9684 the SET_FRAME_GARBAGED below. */
9685 goto OTHER;
9686
9687 /* We use x_top_window_to_frame because map events can
9688 come for sub-windows and they don't mean that the
9689 frame is visible. */
19126e11 9690 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
7a13e894
RS
9691 if (f)
9692 {
9693 f->async_visible = 1;
9694 f->async_iconified = 0;
06c488fd 9695 f->output_data.x->has_been_visible = 1;
dc6f92b8 9696
7a13e894
RS
9697 /* wait_reading_process_input will notice this and update
9698 the frame's display structures. */
9699 SET_FRAME_GARBAGED (f);
bddd097c 9700
d806e720
RS
9701 if (f->iconified)
9702 {
9703 bufp->kind = deiconify_event;
9704 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9705 bufp->arg = Qnil;
d806e720
RS
9706 bufp++;
9707 count++;
9708 numchars--;
9709 }
e73ec6fa 9710 else if (! NILP (Vframe_list)
8e713be6 9711 && ! NILP (XCDR (Vframe_list)))
78aa2ba5
KH
9712 /* Force a redisplay sooner or later
9713 to update the frame titles
9714 in case this is the second frame. */
9715 record_asynch_buffer_change ();
7a13e894 9716 }
7a13e894 9717 goto OTHER;
dc6f92b8 9718
7a13e894 9719 case KeyPress:
19126e11 9720 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
f451eb13 9721
eccc05db 9722#if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
9723 /* I couldn't find a way to prevent LessTif scroll bars
9724 from consuming key events. */
9725 if (f == 0)
9726 {
9727 Widget widget = XtWindowToWidget (dpyinfo->display,
9728 event.xkey.window);
9729 if (widget && XmIsScrollBar (widget))
9730 {
9731 widget = XtParent (widget);
9732 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9733 }
9734 }
eccc05db 9735#endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
06a2c219 9736
7a13e894
RS
9737 if (f != 0)
9738 {
9739 KeySym keysym, orig_keysym;
379b5ac0
KH
9740 /* al%imercury@uunet.uu.net says that making this 81
9741 instead of 80 fixed a bug whereby meta chars made
9742 his Emacs hang.
9743
9744 It seems that some version of XmbLookupString has
9745 a bug of not returning XBufferOverflow in
9746 status_return even if the input is too long to
9747 fit in 81 bytes. So, we must prepare sufficient
9748 bytes for copy_buffer. 513 bytes (256 chars for
9749 two-byte character set) seems to be a faily good
9750 approximation. -- 2000.8.10 handa@etl.go.jp */
9751 unsigned char copy_buffer[513];
9752 unsigned char *copy_bufptr = copy_buffer;
9753 int copy_bufsiz = sizeof (copy_buffer);
7a13e894 9754 int modifiers;
64bb1782 9755
7a13e894
RS
9756 event.xkey.state
9757 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9758 extra_keyboard_modifiers);
9759 modifiers = event.xkey.state;
3a2712f9 9760
7a13e894 9761 /* This will have to go some day... */
752a043f 9762
7a13e894
RS
9763 /* make_lispy_event turns chars into control chars.
9764 Don't do it here because XLookupString is too eager. */
9765 event.xkey.state &= ~ControlMask;
5d46f928
RS
9766 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9767 | dpyinfo->super_mod_mask
9768 | dpyinfo->hyper_mod_mask
9769 | dpyinfo->alt_mod_mask);
9770
1cf4a0d1
RS
9771 /* In case Meta is ComposeCharacter,
9772 clear its status. According to Markus Ehrnsperger
9773 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9774 this enables ComposeCharacter to work whether or
9775 not it is combined with Meta. */
9776 if (modifiers & dpyinfo->meta_mod_mask)
9777 bzero (&compose_status, sizeof (compose_status));
9778
6c183ba5
RS
9779#ifdef HAVE_X_I18N
9780 if (FRAME_XIC (f))
9781 {
f5d11644
GM
9782 Status status_return;
9783
6c183ba5 9784 nbytes = XmbLookupString (FRAME_XIC (f),
f5d11644
GM
9785 &event.xkey, copy_bufptr,
9786 copy_bufsiz, &keysym,
6c183ba5 9787 &status_return);
f5d11644
GM
9788 if (status_return == XBufferOverflow)
9789 {
9790 copy_bufsiz = nbytes + 1;
9791 copy_bufptr = (char *) alloca (copy_bufsiz);
9792 nbytes = XmbLookupString (FRAME_XIC (f),
9793 &event.xkey, copy_bufptr,
9794 copy_bufsiz, &keysym,
9795 &status_return);
9796 }
9797
1decb680
PE
9798 if (status_return == XLookupNone)
9799 break;
9800 else if (status_return == XLookupChars)
fdd9d55e
GM
9801 {
9802 keysym = NoSymbol;
9803 modifiers = 0;
9804 }
1decb680
PE
9805 else if (status_return != XLookupKeySym
9806 && status_return != XLookupBoth)
9807 abort ();
6c183ba5
RS
9808 }
9809 else
379b5ac0
KH
9810 nbytes = XLookupString (&event.xkey, copy_bufptr,
9811 copy_bufsiz, &keysym,
9812 &compose_status);
6c183ba5 9813#else
379b5ac0
KH
9814 nbytes = XLookupString (&event.xkey, copy_bufptr,
9815 copy_bufsiz, &keysym,
9816 &compose_status);
6c183ba5 9817#endif
dc6f92b8 9818
7a13e894 9819 orig_keysym = keysym;
55123275 9820
7a13e894
RS
9821 if (numchars > 1)
9822 {
9823 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9824 || keysym == XK_Delete
1097aea0 9825#ifdef XK_ISO_Left_Tab
441affdb 9826 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
1097aea0 9827#endif
852bff8f 9828 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
7a13e894
RS
9829 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9830 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
c34790e0 9831#ifdef HPUX
7a13e894
RS
9832 /* This recognizes the "extended function keys".
9833 It seems there's no cleaner way.
9834 Test IsModifierKey to avoid handling mode_switch
9835 incorrectly. */
9836 || ((unsigned) (keysym) >= XK_Select
9837 && (unsigned)(keysym) < XK_KP_Space)
69388238
RS
9838#endif
9839#ifdef XK_dead_circumflex
7a13e894 9840 || orig_keysym == XK_dead_circumflex
69388238
RS
9841#endif
9842#ifdef XK_dead_grave
7a13e894 9843 || orig_keysym == XK_dead_grave
69388238
RS
9844#endif
9845#ifdef XK_dead_tilde
7a13e894 9846 || orig_keysym == XK_dead_tilde
69388238
RS
9847#endif
9848#ifdef XK_dead_diaeresis
7a13e894 9849 || orig_keysym == XK_dead_diaeresis
69388238
RS
9850#endif
9851#ifdef XK_dead_macron
7a13e894 9852 || orig_keysym == XK_dead_macron
69388238
RS
9853#endif
9854#ifdef XK_dead_degree
7a13e894 9855 || orig_keysym == XK_dead_degree
69388238
RS
9856#endif
9857#ifdef XK_dead_acute
7a13e894 9858 || orig_keysym == XK_dead_acute
69388238
RS
9859#endif
9860#ifdef XK_dead_cedilla
7a13e894 9861 || orig_keysym == XK_dead_cedilla
69388238
RS
9862#endif
9863#ifdef XK_dead_breve
7a13e894 9864 || orig_keysym == XK_dead_breve
69388238
RS
9865#endif
9866#ifdef XK_dead_ogonek
7a13e894 9867 || orig_keysym == XK_dead_ogonek
69388238
RS
9868#endif
9869#ifdef XK_dead_caron
7a13e894 9870 || orig_keysym == XK_dead_caron
69388238
RS
9871#endif
9872#ifdef XK_dead_doubleacute
7a13e894 9873 || orig_keysym == XK_dead_doubleacute
69388238
RS
9874#endif
9875#ifdef XK_dead_abovedot
7a13e894 9876 || orig_keysym == XK_dead_abovedot
c34790e0 9877#endif
7a13e894
RS
9878 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9879 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9880 /* Any "vendor-specific" key is ok. */
9881 || (orig_keysym & (1 << 28)))
9882 && ! (IsModifierKey (orig_keysym)
7719aa06
RS
9883#ifndef HAVE_X11R5
9884#ifdef XK_Mode_switch
7a13e894 9885 || ((unsigned)(orig_keysym) == XK_Mode_switch)
7719aa06
RS
9886#endif
9887#ifdef XK_Num_Lock
7a13e894 9888 || ((unsigned)(orig_keysym) == XK_Num_Lock)
7719aa06
RS
9889#endif
9890#endif /* not HAVE_X11R5 */
7a13e894 9891 ))
dc6f92b8 9892 {
10e6549c
RS
9893 if (temp_index == sizeof temp_buffer / sizeof (short))
9894 temp_index = 0;
7a13e894
RS
9895 temp_buffer[temp_index++] = keysym;
9896 bufp->kind = non_ascii_keystroke;
9897 bufp->code = keysym;
e0c1aef2 9898 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9899 bufp->arg = Qnil;
334208b7
RS
9900 bufp->modifiers
9901 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9902 modifiers);
1113d9db 9903 bufp->timestamp = event.xkey.time;
dc6f92b8 9904 bufp++;
7a13e894
RS
9905 count++;
9906 numchars--;
dc6f92b8 9907 }
7a13e894
RS
9908 else if (numchars > nbytes)
9909 {
9910 register int i;
379b5ac0 9911 register int c;
379b5ac0 9912 int nchars, len;
7a13e894
RS
9913
9914 for (i = 0; i < nbytes; i++)
9915 {
379b5ac0
KH
9916 if (temp_index == (sizeof temp_buffer
9917 / sizeof (short)))
7a13e894 9918 temp_index = 0;
379b5ac0
KH
9919 temp_buffer[temp_index++] = copy_bufptr[i];
9920 }
9921
9922 if (/* If the event is not from XIM, */
9923 event.xkey.keycode != 0
9924 /* or the current locale doesn't request
9925 decoding of the intup data, ... */
9926 || coding.type == coding_type_raw_text
9927 || coding.type == coding_type_no_conversion)
9928 {
9929 /* ... we can use the input data as is. */
9930 nchars = nbytes;
9931 }
9932 else
9933 {
9934 /* We have to decode the input data. */
9935 int require;
9936 unsigned char *p;
9937
9938 require = decoding_buffer_size (&coding, nbytes);
9939 p = (unsigned char *) alloca (require);
9940 coding.mode |= CODING_MODE_LAST_BLOCK;
9941 decode_coding (&coding, copy_bufptr, p,
9942 nbytes, require);
9943 nbytes = coding.produced;
9944 nchars = coding.produced_char;
9945 copy_bufptr = p;
9946 }
9947
9948 /* Convert the input data to a sequence of
9949 character events. */
9950 for (i = 0; i < nbytes; i += len)
9951 {
9952 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
9953 nbytes - i, len);
9954 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
9955 ? ascii_keystroke
9956 : multibyte_char_keystroke);
9957 bufp->code = c;
7a13e894 9958 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9959 bufp->arg = Qnil;
7a13e894
RS
9960 bufp->modifiers
9961 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9962 modifiers);
9963 bufp->timestamp = event.xkey.time;
9964 bufp++;
9965 }
9966
379b5ac0
KH
9967 count += nchars;
9968 numchars -= nchars;
1decb680
PE
9969
9970 if (keysym == NoSymbol)
9971 break;
7a13e894
RS
9972 }
9973 else
9974 abort ();
dc6f92b8 9975 }
10e6549c
RS
9976 else
9977 abort ();
dc6f92b8 9978 }
59ddecde
GM
9979#ifdef HAVE_X_I18N
9980 /* Don't dispatch this event since XtDispatchEvent calls
9981 XFilterEvent, and two calls in a row may freeze the
9982 client. */
9983 break;
9984#else
717ca130 9985 goto OTHER;
59ddecde 9986#endif
f451eb13 9987
f5d11644 9988 case KeyRelease:
59ddecde
GM
9989#ifdef HAVE_X_I18N
9990 /* Don't dispatch this event since XtDispatchEvent calls
9991 XFilterEvent, and two calls in a row may freeze the
9992 client. */
9993 break;
9994#else
f5d11644 9995 goto OTHER;
59ddecde 9996#endif
f5d11644 9997
7a13e894 9998 /* Here's a possible interpretation of the whole
06a2c219
GM
9999 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
10000 you get a FocusIn event, you have to get a FocusOut
10001 event before you relinquish the focus. If you
10002 haven't received a FocusIn event, then a mere
10003 LeaveNotify is enough to free you. */
f451eb13 10004
7a13e894 10005 case EnterNotify:
06a2c219
GM
10006 {
10007 int from_menu_bar_p = 0;
10008
10009 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10010
10011#ifdef LESSTIF_VERSION
10012 /* When clicking outside of a menu bar popup to close
10013 it, we get a FocusIn/ EnterNotify sequence of
10014 events. The flag event.xcrossing.focus is not set
10015 in the EnterNotify event of that sequence because
10016 the focus is in the menu bar,
10017 event.xcrossing.window is the frame's X window.
10018 Unconditionally setting the focus frame to null in
10019 this case is not the right thing, because no event
10020 follows that could set the focus frame to the right
10021 value.
10022
10023 This could be a LessTif bug, but I wasn't able to
10024 reproduce the behavior in a simple test program.
10025
10026 (gerd, LessTif 0.88.1). */
10027
10028 if (!event.xcrossing.focus
10029 && f
10030 && f->output_data.x->menubar_widget)
10031 {
10032 Window focus;
10033 int revert;
10034
10035 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
10036 if (focus == XtWindow (f->output_data.x->menubar_widget))
10037 from_menu_bar_p = 1;
10038 }
10039#endif /* LESSTIF_VERSION */
6d4238f3 10040
06a2c219
GM
10041 if (event.xcrossing.focus || from_menu_bar_p)
10042 {
10043 /* Avoid nasty pop/raise loops. */
10044 if (f && (!(f->auto_raise)
10045 || !(f->auto_lower)
10046 || (event.xcrossing.time - enter_timestamp) > 500))
10047 {
10048 x_new_focus_frame (dpyinfo, f);
10049 enter_timestamp = event.xcrossing.time;
10050 }
10051 }
10052 else if (f == dpyinfo->x_focus_frame)
10053 x_new_focus_frame (dpyinfo, 0);
10054
10055 /* EnterNotify counts as mouse movement,
10056 so update things that depend on mouse position. */
10057 if (f && !f->output_data.x->busy_p)
10058 note_mouse_movement (f, &event.xmotion);
10059 goto OTHER;
10060 }
dc6f92b8 10061
7a13e894 10062 case FocusIn:
19126e11 10063 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 10064 if (event.xfocus.detail != NotifyPointer)
0f941935 10065 dpyinfo->x_focus_event_frame = f;
7a13e894 10066 if (f)
eb72635f
GM
10067 {
10068 x_new_focus_frame (dpyinfo, f);
10069
10070 /* Don't stop displaying the initial startup message
10071 for a switch-frame event we don't need. */
10072 if (GC_NILP (Vterminal_frame)
10073 && GC_CONSP (Vframe_list)
10074 && !GC_NILP (XCDR (Vframe_list)))
10075 {
10076 bufp->kind = FOCUS_IN_EVENT;
10077 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 10078 bufp->arg = Qnil;
eb72635f
GM
10079 ++bufp, ++count, --numchars;
10080 }
10081 }
f9e24cb9 10082
6c183ba5
RS
10083#ifdef HAVE_X_I18N
10084 if (f && FRAME_XIC (f))
10085 XSetICFocus (FRAME_XIC (f));
10086#endif
10087
7a13e894 10088 goto OTHER;
10c5e63d 10089
7a13e894 10090 case LeaveNotify:
19126e11 10091 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
7a13e894 10092 if (f)
10c5e63d 10093 {
06a2c219
GM
10094 int from_menu_bar_p = 0;
10095
7a13e894 10096 if (f == dpyinfo->mouse_face_mouse_frame)
06a2c219
GM
10097 {
10098 /* If we move outside the frame, then we're
10099 certainly no longer on any text in the frame. */
10100 clear_mouse_face (dpyinfo);
10101 dpyinfo->mouse_face_mouse_frame = 0;
10102 }
10103
10104 /* Generate a nil HELP_EVENT to cancel a help-echo.
10105 Do it only if there's something to cancel.
10106 Otherwise, the startup message is cleared when
10107 the mouse leaves the frame. */
10108 if (any_help_event_p)
10109 {
be010514
GM
10110 Lisp_Object frame;
10111 int n;
10112
06a2c219 10113 XSETFRAME (frame, f);
5ab2570d
GM
10114 n = gen_help_event (bufp, numchars,
10115 Qnil, frame, Qnil, Qnil, 0);
be010514 10116 bufp += n, count += n, numchars -= n;
06a2c219 10117 }
7a13e894 10118
06a2c219
GM
10119#ifdef LESSTIF_VERSION
10120 /* Please see the comment at the start of the
10121 EnterNotify case. */
10122 if (!event.xcrossing.focus
10123 && f->output_data.x->menubar_widget)
10124 {
10125 Window focus;
10126 int revert;
10127 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
10128 if (focus == XtWindow (f->output_data.x->menubar_widget))
10129 from_menu_bar_p = 1;
10130 }
10131#endif /* LESSTIF_VERSION */
10132
10133 if (event.xcrossing.focus || from_menu_bar_p)
0f941935 10134 x_mouse_leave (dpyinfo);
10c5e63d 10135 else
7a13e894 10136 {
0f941935
KH
10137 if (f == dpyinfo->x_focus_event_frame)
10138 dpyinfo->x_focus_event_frame = 0;
10139 if (f == dpyinfo->x_focus_frame)
10140 x_new_focus_frame (dpyinfo, 0);
7a13e894 10141 }
10c5e63d 10142 }
7a13e894 10143 goto OTHER;
dc6f92b8 10144
7a13e894 10145 case FocusOut:
19126e11 10146 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 10147 if (event.xfocus.detail != NotifyPointer
0f941935
KH
10148 && f == dpyinfo->x_focus_event_frame)
10149 dpyinfo->x_focus_event_frame = 0;
10150 if (f && f == dpyinfo->x_focus_frame)
10151 x_new_focus_frame (dpyinfo, 0);
f9e24cb9 10152
6c183ba5
RS
10153#ifdef HAVE_X_I18N
10154 if (f && FRAME_XIC (f))
10155 XUnsetICFocus (FRAME_XIC (f));
10156#endif
10157
7a13e894 10158 goto OTHER;
dc6f92b8 10159
7a13e894 10160 case MotionNotify:
dc6f92b8 10161 {
06a2c219 10162 previous_help_echo = help_echo;
7cea38bc 10163 help_echo = help_echo_object = help_echo_window = Qnil;
be010514 10164 help_echo_pos = -1;
06a2c219 10165
7a13e894
RS
10166 if (dpyinfo->grabbed && last_mouse_frame
10167 && FRAME_LIVE_P (last_mouse_frame))
10168 f = last_mouse_frame;
10169 else
19126e11 10170 f = x_window_to_frame (dpyinfo, event.xmotion.window);
06a2c219 10171
7a13e894
RS
10172 if (f)
10173 note_mouse_movement (f, &event.xmotion);
10174 else
10175 {
e88b3c50 10176#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
10177 struct scroll_bar *bar
10178 = x_window_to_scroll_bar (event.xmotion.window);
f451eb13 10179
7a13e894
RS
10180 if (bar)
10181 x_scroll_bar_note_movement (bar, &event);
e88b3c50 10182#endif /* USE_TOOLKIT_SCROLL_BARS */
b8009dd1 10183
06a2c219
GM
10184 /* If we move outside the frame, then we're
10185 certainly no longer on any text in the frame. */
7a13e894
RS
10186 clear_mouse_face (dpyinfo);
10187 }
06a2c219
GM
10188
10189 /* If the contents of the global variable help_echo
10190 has changed, generate a HELP_EVENT. */
b7e80413
SM
10191 if (!NILP (help_echo)
10192 || !NILP (previous_help_echo))
06a2c219
GM
10193 {
10194 Lisp_Object frame;
be010514 10195 int n;
06a2c219
GM
10196
10197 if (f)
10198 XSETFRAME (frame, f);
10199 else
10200 frame = Qnil;
10201
10202 any_help_event_p = 1;
5ab2570d 10203 n = gen_help_event (bufp, numchars, help_echo, frame,
7cea38bc
GM
10204 help_echo_window, help_echo_object,
10205 help_echo_pos);
be010514 10206 bufp += n, count += n, numchars -= n;
06a2c219
GM
10207 }
10208
10209 goto OTHER;
dc6f92b8 10210 }
dc6f92b8 10211
7a13e894 10212 case ConfigureNotify:
9829ddba
RS
10213 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10214 if (f)
af395ec1 10215 {
5c187dee 10216#ifndef USE_X_TOOLKIT
bf1b7b30
KH
10217 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10218 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
5c187dee 10219
2d7fc7e8
RS
10220 /* In the toolkit version, change_frame_size
10221 is called by the code that handles resizing
10222 of the EmacsFrame widget. */
7a13e894 10223
7a13e894
RS
10224 /* Even if the number of character rows and columns has
10225 not changed, the font size may have changed, so we need
10226 to check the pixel dimensions as well. */
10227 if (columns != f->width
10228 || rows != f->height
7556890b
RS
10229 || event.xconfigure.width != f->output_data.x->pixel_width
10230 || event.xconfigure.height != f->output_data.x->pixel_height)
7a13e894 10231 {
7d1e984f 10232 change_frame_size (f, rows, columns, 0, 1, 0);
7a13e894 10233 SET_FRAME_GARBAGED (f);
e687d06e 10234 cancel_mouse_face (f);
7a13e894 10235 }
2d7fc7e8 10236#endif
af395ec1 10237
7556890b
RS
10238 f->output_data.x->pixel_width = event.xconfigure.width;
10239 f->output_data.x->pixel_height = event.xconfigure.height;
7a13e894
RS
10240
10241 /* What we have now is the position of Emacs's own window.
10242 Convert that to the position of the window manager window. */
dcb07ae9
RS
10243 x_real_positions (f, &f->output_data.x->left_pos,
10244 &f->output_data.x->top_pos);
10245
f5d11644
GM
10246#ifdef HAVE_X_I18N
10247 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10248 xic_set_statusarea (f);
10249#endif
10250
dcb07ae9
RS
10251 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10252 {
10253 /* Since the WM decorations come below top_pos now,
10254 we must put them below top_pos in the future. */
10255 f->output_data.x->win_gravity = NorthWestGravity;
10256 x_wm_set_size_hint (f, (long) 0, 0);
10257 }
8f08dc93
KH
10258#ifdef USE_MOTIF
10259 /* Some window managers pass (0,0) as the location of
10260 the window, and the Motif event handler stores it
10261 in the emacs widget, which messes up Motif menus. */
10262 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
10263 {
10264 event.xconfigure.x = f->output_data.x->widget->core.x;
10265 event.xconfigure.y = f->output_data.x->widget->core.y;
10266 }
06a2c219 10267#endif /* USE_MOTIF */
7a13e894 10268 }
2d7fc7e8 10269 goto OTHER;
dc6f92b8 10270
7a13e894
RS
10271 case ButtonPress:
10272 case ButtonRelease:
10273 {
10274 /* If we decide we want to generate an event to be seen
10275 by the rest of Emacs, we put it here. */
10276 struct input_event emacs_event;
9ea173e8 10277 int tool_bar_p = 0;
06a2c219 10278
7a13e894 10279 emacs_event.kind = no_event;
7a13e894 10280 bzero (&compose_status, sizeof (compose_status));
9b07615b 10281
06a2c219
GM
10282 if (dpyinfo->grabbed
10283 && last_mouse_frame
9f67f20b
RS
10284 && FRAME_LIVE_P (last_mouse_frame))
10285 f = last_mouse_frame;
10286 else
2224b905 10287 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9f67f20b 10288
06a2c219
GM
10289 if (f)
10290 {
9ea173e8
GM
10291 /* Is this in the tool-bar? */
10292 if (WINDOWP (f->tool_bar_window)
10293 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
06a2c219
GM
10294 {
10295 Lisp_Object window;
10296 int p, x, y;
10297
10298 x = event.xbutton.x;
10299 y = event.xbutton.y;
10300
10301 /* Set x and y. */
10302 window = window_from_coordinates (f, x, y, &p, 1);
9ea173e8 10303 if (EQ (window, f->tool_bar_window))
06a2c219 10304 {
9ea173e8
GM
10305 x_handle_tool_bar_click (f, &event.xbutton);
10306 tool_bar_p = 1;
06a2c219
GM
10307 }
10308 }
10309
9ea173e8 10310 if (!tool_bar_p)
06a2c219
GM
10311 if (!dpyinfo->x_focus_frame
10312 || f == dpyinfo->x_focus_frame)
10313 construct_mouse_click (&emacs_event, &event, f);
7a13e894
RS
10314 }
10315 else
10316 {
06a2c219 10317#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
10318 struct scroll_bar *bar
10319 = x_window_to_scroll_bar (event.xbutton.window);
f451eb13 10320
7a13e894
RS
10321 if (bar)
10322 x_scroll_bar_handle_click (bar, &event, &emacs_event);
06a2c219 10323#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
10324 }
10325
10326 if (event.type == ButtonPress)
10327 {
10328 dpyinfo->grabbed |= (1 << event.xbutton.button);
10329 last_mouse_frame = f;
edad46f6
KH
10330 /* Ignore any mouse motion that happened
10331 before this event; any subsequent mouse-movement
10332 Emacs events should reflect only motion after
10333 the ButtonPress. */
a00e91cd
KH
10334 if (f != 0)
10335 f->mouse_moved = 0;
06a2c219 10336
9ea173e8
GM
10337 if (!tool_bar_p)
10338 last_tool_bar_item = -1;
7a13e894 10339 }
3afe33e7
RS
10340 else
10341 {
7a13e894 10342 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
3afe33e7 10343 }
23faf38f 10344
7a13e894
RS
10345 if (numchars >= 1 && emacs_event.kind != no_event)
10346 {
10347 bcopy (&emacs_event, bufp, sizeof (struct input_event));
10348 bufp++;
10349 count++;
10350 numchars--;
10351 }
3afe33e7
RS
10352
10353#ifdef USE_X_TOOLKIT
2224b905
RS
10354 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
10355 /* For a down-event in the menu bar,
10356 don't pass it to Xt right now.
10357 Instead, save it away
10358 and we will pass it to Xt from kbd_buffer_get_event.
10359 That way, we can run some Lisp code first. */
91375f8f
RS
10360 if (f && event.type == ButtonPress
10361 /* Verify the event is really within the menu bar
10362 and not just sent to it due to grabbing. */
10363 && event.xbutton.x >= 0
10364 && event.xbutton.x < f->output_data.x->pixel_width
10365 && event.xbutton.y >= 0
10366 && event.xbutton.y < f->output_data.x->menubar_height
10367 && event.xbutton.same_screen)
2224b905 10368 {
8805890a 10369 SET_SAVED_BUTTON_EVENT;
2237cac9
RS
10370 XSETFRAME (last_mouse_press_frame, f);
10371 }
10372 else if (event.type == ButtonPress)
10373 {
10374 last_mouse_press_frame = Qnil;
30e671c3 10375 goto OTHER;
ce89ef46 10376 }
06a2c219 10377
2237cac9
RS
10378#ifdef USE_MOTIF /* This should do not harm for Lucid,
10379 but I am trying to be cautious. */
ce89ef46
RS
10380 else if (event.type == ButtonRelease)
10381 {
2237cac9 10382 if (!NILP (last_mouse_press_frame))
f10ded1c 10383 {
2237cac9
RS
10384 f = XFRAME (last_mouse_press_frame);
10385 if (f->output_data.x)
06a2c219 10386 SET_SAVED_BUTTON_EVENT;
f10ded1c 10387 }
06a2c219 10388 else
30e671c3 10389 goto OTHER;
2224b905 10390 }
2237cac9 10391#endif /* USE_MOTIF */
2224b905
RS
10392 else
10393 goto OTHER;
3afe33e7 10394#endif /* USE_X_TOOLKIT */
7a13e894
RS
10395 }
10396 break;
dc6f92b8 10397
7a13e894 10398 case CirculateNotify:
06a2c219
GM
10399 goto OTHER;
10400
7a13e894 10401 case CirculateRequest:
06a2c219
GM
10402 goto OTHER;
10403
10404 case VisibilityNotify:
10405 goto OTHER;
dc6f92b8 10406
7a13e894
RS
10407 case MappingNotify:
10408 /* Someone has changed the keyboard mapping - update the
10409 local cache. */
10410 switch (event.xmapping.request)
10411 {
10412 case MappingModifier:
10413 x_find_modifier_meanings (dpyinfo);
10414 /* This is meant to fall through. */
10415 case MappingKeyboard:
10416 XRefreshKeyboardMapping (&event.xmapping);
10417 }
7a13e894 10418 goto OTHER;
dc6f92b8 10419
7a13e894 10420 default:
7a13e894 10421 OTHER:
717ca130 10422#ifdef USE_X_TOOLKIT
7a13e894
RS
10423 BLOCK_INPUT;
10424 XtDispatchEvent (&event);
10425 UNBLOCK_INPUT;
3afe33e7 10426#endif /* USE_X_TOOLKIT */
7a13e894
RS
10427 break;
10428 }
dc6f92b8
JB
10429 }
10430 }
10431
06a2c219
GM
10432 out:;
10433
9a5196d0
RS
10434 /* On some systems, an X bug causes Emacs to get no more events
10435 when the window is destroyed. Detect that. (1994.) */
58769bee 10436 if (! event_found)
ef2a22d0 10437 {
ef2a22d0
RS
10438 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
10439 One XNOOP in 100 loops will make Emacs terminate.
10440 B. Bretthauer, 1994 */
10441 x_noop_count++;
58769bee 10442 if (x_noop_count >= 100)
ef2a22d0
RS
10443 {
10444 x_noop_count=0;
2224b905
RS
10445
10446 if (next_noop_dpyinfo == 0)
10447 next_noop_dpyinfo = x_display_list;
10448
10449 XNoOp (next_noop_dpyinfo->display);
10450
10451 /* Each time we get here, cycle through the displays now open. */
10452 next_noop_dpyinfo = next_noop_dpyinfo->next;
ef2a22d0
RS
10453 }
10454 }
502add23 10455
06a2c219 10456 /* If the focus was just given to an auto-raising frame,
0134a210 10457 raise it now. */
7a13e894 10458 /* ??? This ought to be able to handle more than one such frame. */
0134a210
RS
10459 if (pending_autoraise_frame)
10460 {
10461 x_raise_frame (pending_autoraise_frame);
10462 pending_autoraise_frame = 0;
10463 }
0134a210 10464
dc6f92b8 10465 UNBLOCK_INPUT;
bde5503b 10466 --handling_signal;
dc6f92b8
JB
10467 return count;
10468}
06a2c219
GM
10469
10470
10471
dc6f92b8 10472\f
06a2c219
GM
10473/***********************************************************************
10474 Text Cursor
10475 ***********************************************************************/
10476
10477/* Note if the text cursor of window W has been overwritten by a
10478 drawing operation that outputs N glyphs starting at HPOS in the
10479 line given by output_cursor.vpos. N < 0 means all the rest of the
10480 line after HPOS has been written. */
10481
10482static void
10483note_overwritten_text_cursor (w, hpos, n)
10484 struct window *w;
10485 int hpos, n;
10486{
10487 if (updated_area == TEXT_AREA
10488 && output_cursor.vpos == w->phys_cursor.vpos
10489 && hpos <= w->phys_cursor.hpos
10490 && (n < 0
10491 || hpos + n > w->phys_cursor.hpos))
10492 w->phys_cursor_on_p = 0;
10493}
f451eb13
JB
10494
10495
06a2c219
GM
10496/* Set clipping for output in glyph row ROW. W is the window in which
10497 we operate. GC is the graphics context to set clipping in.
10498 WHOLE_LINE_P non-zero means include the areas used for truncation
10499 mark display and alike in the clipping rectangle.
10500
10501 ROW may be a text row or, e.g., a mode line. Text rows must be
10502 clipped to the interior of the window dedicated to text display,
10503 mode lines must be clipped to the whole window. */
dc6f92b8
JB
10504
10505static void
06a2c219
GM
10506x_clip_to_row (w, row, gc, whole_line_p)
10507 struct window *w;
10508 struct glyph_row *row;
10509 GC gc;
10510 int whole_line_p;
dc6f92b8 10511{
06a2c219
GM
10512 struct frame *f = XFRAME (WINDOW_FRAME (w));
10513 XRectangle clip_rect;
10514 int window_x, window_y, window_width, window_height;
dc6f92b8 10515
06a2c219 10516 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5c1aae96 10517
06a2c219
GM
10518 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10519 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10520 clip_rect.y = max (clip_rect.y, window_y);
10521 clip_rect.width = window_width;
10522 clip_rect.height = row->visible_height;
5c1aae96 10523
06a2c219
GM
10524 /* If clipping to the whole line, including trunc marks, extend
10525 the rectangle to the left and increase its width. */
10526 if (whole_line_p)
10527 {
110859fc
GM
10528 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10529 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
06a2c219 10530 }
5c1aae96 10531
06a2c219 10532 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
dc6f92b8
JB
10533}
10534
06a2c219
GM
10535
10536/* Draw a hollow box cursor on window W in glyph row ROW. */
dc6f92b8
JB
10537
10538static void
06a2c219
GM
10539x_draw_hollow_cursor (w, row)
10540 struct window *w;
10541 struct glyph_row *row;
dc6f92b8 10542{
06a2c219
GM
10543 struct frame *f = XFRAME (WINDOW_FRAME (w));
10544 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10545 Display *dpy = FRAME_X_DISPLAY (f);
10546 int x, y, wd, h;
10547 XGCValues xgcv;
10548 struct glyph *cursor_glyph;
10549 GC gc;
10550
10551 /* Compute frame-relative coordinates from window-relative
10552 coordinates. */
10553 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10554 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10555 + row->ascent - w->phys_cursor_ascent);
10556 h = row->height - 1;
10557
10558 /* Get the glyph the cursor is on. If we can't tell because
10559 the current matrix is invalid or such, give up. */
10560 cursor_glyph = get_phys_cursor_glyph (w);
10561 if (cursor_glyph == NULL)
dc6f92b8
JB
10562 return;
10563
06a2c219
GM
10564 /* Compute the width of the rectangle to draw. If on a stretch
10565 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10566 rectangle as wide as the glyph, but use a canonical character
10567 width instead. */
10568 wd = cursor_glyph->pixel_width - 1;
10569 if (cursor_glyph->type == STRETCH_GLYPH
10570 && !x_stretch_cursor_p)
10571 wd = min (CANON_X_UNIT (f), wd);
10572
10573 /* The foreground of cursor_gc is typically the same as the normal
10574 background color, which can cause the cursor box to be invisible. */
10575 xgcv.foreground = f->output_data.x->cursor_pixel;
10576 if (dpyinfo->scratch_cursor_gc)
10577 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10578 else
10579 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10580 GCForeground, &xgcv);
10581 gc = dpyinfo->scratch_cursor_gc;
10582
10583 /* Set clipping, draw the rectangle, and reset clipping again. */
10584 x_clip_to_row (w, row, gc, 0);
10585 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10586 XSetClipMask (dpy, gc, None);
dc6f92b8
JB
10587}
10588
06a2c219
GM
10589
10590/* Draw a bar cursor on window W in glyph row ROW.
10591
10592 Implementation note: One would like to draw a bar cursor with an
10593 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10594 Unfortunately, I didn't find a font yet that has this property set.
10595 --gerd. */
dc6f92b8
JB
10596
10597static void
f02d8aa0 10598x_draw_bar_cursor (w, row, width)
06a2c219
GM
10599 struct window *w;
10600 struct glyph_row *row;
f02d8aa0 10601 int width;
dc6f92b8 10602{
92f424df
GM
10603 struct frame *f = XFRAME (w->frame);
10604 struct glyph *cursor_glyph;
10605 GC gc;
10606 int x;
10607 unsigned long mask;
10608 XGCValues xgcv;
10609 Display *dpy;
10610 Window window;
06a2c219 10611
92f424df
GM
10612 /* If cursor is out of bounds, don't draw garbage. This can happen
10613 in mini-buffer windows when switching between echo area glyphs
10614 and mini-buffer. */
10615 cursor_glyph = get_phys_cursor_glyph (w);
10616 if (cursor_glyph == NULL)
10617 return;
06a2c219 10618
92f424df
GM
10619 /* If on an image, draw like a normal cursor. That's usually better
10620 visible than drawing a bar, esp. if the image is large so that
10621 the bar might not be in the window. */
10622 if (cursor_glyph->type == IMAGE_GLYPH)
10623 {
10624 struct glyph_row *row;
10625 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
10626 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
10627 }
10628 else
10629 {
06a2c219
GM
10630 xgcv.background = f->output_data.x->cursor_pixel;
10631 xgcv.foreground = f->output_data.x->cursor_pixel;
10632 xgcv.graphics_exposures = 0;
10633 mask = GCForeground | GCBackground | GCGraphicsExposures;
10634 dpy = FRAME_X_DISPLAY (f);
10635 window = FRAME_X_WINDOW (f);
10636 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
92f424df 10637
06a2c219
GM
10638 if (gc)
10639 XChangeGC (dpy, gc, mask, &xgcv);
10640 else
10641 {
10642 gc = XCreateGC (dpy, window, mask, &xgcv);
10643 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10644 }
92f424df 10645
f02d8aa0
GM
10646 if (width < 0)
10647 width = f->output_data.x->cursor_width;
92f424df 10648
06a2c219
GM
10649 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10650 x_clip_to_row (w, row, gc, 0);
10651 XFillRectangle (dpy, window, gc,
10652 x,
10653 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
f02d8aa0 10654 min (cursor_glyph->pixel_width, width),
06a2c219
GM
10655 row->height);
10656 XSetClipMask (dpy, gc, None);
10657 }
dc6f92b8
JB
10658}
10659
06a2c219
GM
10660
10661/* Clear the cursor of window W to background color, and mark the
10662 cursor as not shown. This is used when the text where the cursor
10663 is is about to be rewritten. */
10664
dc6f92b8 10665static void
06a2c219
GM
10666x_clear_cursor (w)
10667 struct window *w;
dc6f92b8 10668{
06a2c219
GM
10669 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10670 x_update_window_cursor (w, 0);
10671}
90e65f07 10672
dbc4e1c1 10673
06a2c219
GM
10674/* Draw the cursor glyph of window W in glyph row ROW. See the
10675 comment of x_draw_glyphs for the meaning of HL. */
dbc4e1c1 10676
06a2c219
GM
10677static void
10678x_draw_phys_cursor_glyph (w, row, hl)
10679 struct window *w;
10680 struct glyph_row *row;
10681 enum draw_glyphs_face hl;
10682{
10683 /* If cursor hpos is out of bounds, don't draw garbage. This can
10684 happen in mini-buffer windows when switching between echo area
10685 glyphs and mini-buffer. */
10686 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
66ac4b0e
GM
10687 {
10688 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10689 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10690 hl, 0, 0, 0);
10691
10692 /* When we erase the cursor, and ROW is overlapped by other
10693 rows, make sure that these overlapping parts of other rows
10694 are redrawn. */
10695 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10696 {
10697 if (row > w->current_matrix->rows
10698 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10699 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10700
10701 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10702 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10703 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10704 }
10705 }
06a2c219 10706}
dbc4e1c1 10707
eea6af04 10708
06a2c219 10709/* Erase the image of a cursor of window W from the screen. */
eea6af04 10710
06a2c219
GM
10711static void
10712x_erase_phys_cursor (w)
10713 struct window *w;
10714{
10715 struct frame *f = XFRAME (w->frame);
10716 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10717 int hpos = w->phys_cursor.hpos;
10718 int vpos = w->phys_cursor.vpos;
10719 int mouse_face_here_p = 0;
10720 struct glyph_matrix *active_glyphs = w->current_matrix;
10721 struct glyph_row *cursor_row;
10722 struct glyph *cursor_glyph;
10723 enum draw_glyphs_face hl;
10724
10725 /* No cursor displayed or row invalidated => nothing to do on the
10726 screen. */
10727 if (w->phys_cursor_type == NO_CURSOR)
10728 goto mark_cursor_off;
10729
10730 /* VPOS >= active_glyphs->nrows means that window has been resized.
10731 Don't bother to erase the cursor. */
10732 if (vpos >= active_glyphs->nrows)
10733 goto mark_cursor_off;
10734
10735 /* If row containing cursor is marked invalid, there is nothing we
10736 can do. */
10737 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10738 if (!cursor_row->enabled_p)
10739 goto mark_cursor_off;
10740
10741 /* This can happen when the new row is shorter than the old one.
10742 In this case, either x_draw_glyphs or clear_end_of_line
10743 should have cleared the cursor. Note that we wouldn't be
10744 able to erase the cursor in this case because we don't have a
10745 cursor glyph at hand. */
10746 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10747 goto mark_cursor_off;
10748
10749 /* If the cursor is in the mouse face area, redisplay that when
10750 we clear the cursor. */
8801a864
KR
10751 if (! NILP (dpyinfo->mouse_face_window)
10752 && w == XWINDOW (dpyinfo->mouse_face_window)
06a2c219
GM
10753 && (vpos > dpyinfo->mouse_face_beg_row
10754 || (vpos == dpyinfo->mouse_face_beg_row
10755 && hpos >= dpyinfo->mouse_face_beg_col))
10756 && (vpos < dpyinfo->mouse_face_end_row
10757 || (vpos == dpyinfo->mouse_face_end_row
10758 && hpos < dpyinfo->mouse_face_end_col))
10759 /* Don't redraw the cursor's spot in mouse face if it is at the
10760 end of a line (on a newline). The cursor appears there, but
10761 mouse highlighting does not. */
10762 && cursor_row->used[TEXT_AREA] > hpos)
10763 mouse_face_here_p = 1;
10764
10765 /* Maybe clear the display under the cursor. */
10766 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10767 {
10768 int x;
045dee35 10769 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dbc4e1c1 10770
06a2c219
GM
10771 cursor_glyph = get_phys_cursor_glyph (w);
10772 if (cursor_glyph == NULL)
10773 goto mark_cursor_off;
dbc4e1c1 10774
06a2c219
GM
10775 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10776
10777 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10778 x,
045dee35 10779 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219
GM
10780 cursor_row->y)),
10781 cursor_glyph->pixel_width,
10782 cursor_row->visible_height,
10783 False);
dbc4e1c1 10784 }
06a2c219
GM
10785
10786 /* Erase the cursor by redrawing the character underneath it. */
10787 if (mouse_face_here_p)
10788 hl = DRAW_MOUSE_FACE;
10789 else if (cursor_row->inverse_p)
10790 hl = DRAW_INVERSE_VIDEO;
10791 else
10792 hl = DRAW_NORMAL_TEXT;
10793 x_draw_phys_cursor_glyph (w, cursor_row, hl);
dbc4e1c1 10794
06a2c219
GM
10795 mark_cursor_off:
10796 w->phys_cursor_on_p = 0;
10797 w->phys_cursor_type = NO_CURSOR;
dbc4e1c1
JB
10798}
10799
10800
06a2c219
GM
10801/* Display or clear cursor of window W. If ON is zero, clear the
10802 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10803 where to put the cursor is specified by HPOS, VPOS, X and Y. */
dbc4e1c1 10804
06a2c219
GM
10805void
10806x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10807 struct window *w;
10808 int on, hpos, vpos, x, y;
dbc4e1c1 10809{
06a2c219
GM
10810 struct frame *f = XFRAME (w->frame);
10811 int new_cursor_type;
f02d8aa0 10812 int new_cursor_width;
06a2c219
GM
10813 struct glyph_matrix *current_glyphs;
10814 struct glyph_row *glyph_row;
10815 struct glyph *glyph;
dbc4e1c1 10816
49d838ea 10817 /* This is pointless on invisible frames, and dangerous on garbaged
06a2c219
GM
10818 windows and frames; in the latter case, the frame or window may
10819 be in the midst of changing its size, and x and y may be off the
10820 window. */
10821 if (! FRAME_VISIBLE_P (f)
10822 || FRAME_GARBAGED_P (f)
10823 || vpos >= w->current_matrix->nrows
10824 || hpos >= w->current_matrix->matrix_w)
dc6f92b8
JB
10825 return;
10826
10827 /* If cursor is off and we want it off, return quickly. */
06a2c219 10828 if (!on && !w->phys_cursor_on_p)
dc6f92b8
JB
10829 return;
10830
06a2c219
GM
10831 current_glyphs = w->current_matrix;
10832 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10833 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10834
10835 /* If cursor row is not enabled, we don't really know where to
10836 display the cursor. */
10837 if (!glyph_row->enabled_p)
10838 {
10839 w->phys_cursor_on_p = 0;
10840 return;
10841 }
10842
10843 xassert (interrupt_input_blocked);
10844
10845 /* Set new_cursor_type to the cursor we want to be displayed. In a
10846 mini-buffer window, we want the cursor only to appear if we are
10847 reading input from this window. For the selected window, we want
10848 the cursor type given by the frame parameter. If explicitly
10849 marked off, draw no cursor. In all other cases, we want a hollow
10850 box cursor. */
f02d8aa0 10851 new_cursor_width = -1;
9b4a7047
GM
10852 if (cursor_in_echo_area
10853 && FRAME_HAS_MINIBUF_P (f)
10854 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
06a2c219 10855 {
9b4a7047
GM
10856 if (w == XWINDOW (echo_area_window))
10857 new_cursor_type = FRAME_DESIRED_CURSOR (f);
06a2c219
GM
10858 else
10859 new_cursor_type = HOLLOW_BOX_CURSOR;
10860 }
06a2c219 10861 else
9b4a7047
GM
10862 {
10863 if (w != XWINDOW (selected_window)
10864 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
10865 {
e55a0b79
GM
10866 extern int cursor_in_non_selected_windows;
10867
5cefa566
GM
10868 if (MINI_WINDOW_P (w)
10869 || !cursor_in_non_selected_windows
10870 || NILP (XBUFFER (w->buffer)->cursor_type))
9b4a7047
GM
10871 new_cursor_type = NO_CURSOR;
10872 else
10873 new_cursor_type = HOLLOW_BOX_CURSOR;
10874 }
10875 else if (w->cursor_off_p)
10876 new_cursor_type = NO_CURSOR;
10877 else
f02d8aa0
GM
10878 {
10879 struct buffer *b = XBUFFER (w->buffer);
10880
10881 if (EQ (b->cursor_type, Qt))
10882 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10883 else
10884 new_cursor_type = x_specified_cursor_type (b->cursor_type,
10885 &new_cursor_width);
10886 }
9b4a7047 10887 }
06a2c219
GM
10888
10889 /* If cursor is currently being shown and we don't want it to be or
10890 it is in the wrong place, or the cursor type is not what we want,
dc6f92b8 10891 erase it. */
06a2c219 10892 if (w->phys_cursor_on_p
dc6f92b8 10893 && (!on
06a2c219
GM
10894 || w->phys_cursor.x != x
10895 || w->phys_cursor.y != y
10896 || new_cursor_type != w->phys_cursor_type))
10897 x_erase_phys_cursor (w);
10898
10899 /* If the cursor is now invisible and we want it to be visible,
10900 display it. */
10901 if (on && !w->phys_cursor_on_p)
10902 {
10903 w->phys_cursor_ascent = glyph_row->ascent;
10904 w->phys_cursor_height = glyph_row->height;
10905
10906 /* Set phys_cursor_.* before x_draw_.* is called because some
10907 of them may need the information. */
10908 w->phys_cursor.x = x;
10909 w->phys_cursor.y = glyph_row->y;
10910 w->phys_cursor.hpos = hpos;
10911 w->phys_cursor.vpos = vpos;
10912 w->phys_cursor_type = new_cursor_type;
10913 w->phys_cursor_on_p = 1;
10914
10915 switch (new_cursor_type)
dc6f92b8 10916 {
06a2c219
GM
10917 case HOLLOW_BOX_CURSOR:
10918 x_draw_hollow_cursor (w, glyph_row);
10919 break;
10920
10921 case FILLED_BOX_CURSOR:
10922 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10923 break;
10924
10925 case BAR_CURSOR:
f02d8aa0 10926 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
06a2c219
GM
10927 break;
10928
10929 case NO_CURSOR:
10930 break;
dc6f92b8 10931
06a2c219
GM
10932 default:
10933 abort ();
10934 }
59ddecde
GM
10935
10936#ifdef HAVE_X_I18N
10937 if (w == XWINDOW (f->selected_window))
10938 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
10939 xic_set_preeditarea (w, x, y);
10940#endif
dc6f92b8
JB
10941 }
10942
06a2c219 10943#ifndef XFlush
f676886a 10944 if (updating_frame != f)
334208b7 10945 XFlush (FRAME_X_DISPLAY (f));
06a2c219 10946#endif
dc6f92b8
JB
10947}
10948
06a2c219
GM
10949
10950/* Display the cursor on window W, or clear it. X and Y are window
10951 relative pixel coordinates. HPOS and VPOS are glyph matrix
10952 positions. If W is not the selected window, display a hollow
10953 cursor. ON non-zero means display the cursor at X, Y which
10954 correspond to HPOS, VPOS, otherwise it is cleared. */
5d46f928 10955
dfcf069d 10956void
06a2c219
GM
10957x_display_cursor (w, on, hpos, vpos, x, y)
10958 struct window *w;
10959 int on, hpos, vpos, x, y;
dc6f92b8 10960{
f94397b5 10961 BLOCK_INPUT;
06a2c219 10962 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
5d46f928
RS
10963 UNBLOCK_INPUT;
10964}
10965
06a2c219
GM
10966
10967/* Display the cursor on window W, or clear it, according to ON_P.
5d46f928
RS
10968 Don't change the cursor's position. */
10969
dfcf069d 10970void
06a2c219 10971x_update_cursor (f, on_p)
5d46f928 10972 struct frame *f;
5d46f928 10973{
06a2c219
GM
10974 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
10975}
10976
10977
10978/* Call x_update_window_cursor with parameter ON_P on all leaf windows
10979 in the window tree rooted at W. */
10980
10981static void
10982x_update_cursor_in_window_tree (w, on_p)
10983 struct window *w;
10984 int on_p;
10985{
10986 while (w)
10987 {
10988 if (!NILP (w->hchild))
10989 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10990 else if (!NILP (w->vchild))
10991 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10992 else
10993 x_update_window_cursor (w, on_p);
10994
10995 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10996 }
10997}
5d46f928 10998
f94397b5 10999
06a2c219
GM
11000/* Switch the display of W's cursor on or off, according to the value
11001 of ON. */
11002
11003static void
11004x_update_window_cursor (w, on)
11005 struct window *w;
11006 int on;
11007{
16b5d424
GM
11008 /* Don't update cursor in windows whose frame is in the process
11009 of being deleted. */
11010 if (w->current_matrix)
11011 {
11012 BLOCK_INPUT;
11013 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11014 w->phys_cursor.x, w->phys_cursor.y);
11015 UNBLOCK_INPUT;
11016 }
dc6f92b8 11017}
06a2c219
GM
11018
11019
11020
dc6f92b8
JB
11021\f
11022/* Icons. */
11023
f676886a 11024/* Refresh bitmap kitchen sink icon for frame F
06a2c219 11025 when we get an expose event for it. */
dc6f92b8 11026
dfcf069d 11027void
f676886a
JB
11028refreshicon (f)
11029 struct frame *f;
dc6f92b8 11030{
06a2c219 11031 /* Normally, the window manager handles this function. */
dc6f92b8
JB
11032}
11033
dbc4e1c1 11034/* Make the x-window of frame F use the gnu icon bitmap. */
dc6f92b8
JB
11035
11036int
990ba854 11037x_bitmap_icon (f, file)
f676886a 11038 struct frame *f;
990ba854 11039 Lisp_Object file;
dc6f92b8 11040{
06a2c219 11041 int bitmap_id;
dc6f92b8 11042
c118dd06 11043 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
11044 return 1;
11045
990ba854 11046 /* Free up our existing icon bitmap if any. */
7556890b
RS
11047 if (f->output_data.x->icon_bitmap > 0)
11048 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11049 f->output_data.x->icon_bitmap = 0;
990ba854
RS
11050
11051 if (STRINGP (file))
7f2ae036
RS
11052 bitmap_id = x_create_bitmap_from_file (f, file);
11053 else
11054 {
990ba854 11055 /* Create the GNU bitmap if necessary. */
5bf01b68 11056 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
334208b7
RS
11057 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11058 = x_create_bitmap_from_data (f, gnu_bits,
11059 gnu_width, gnu_height);
990ba854
RS
11060
11061 /* The first time we create the GNU bitmap,
06a2c219 11062 this increments the ref-count one extra time.
990ba854
RS
11063 As a result, the GNU bitmap is never freed.
11064 That way, we don't have to worry about allocating it again. */
334208b7 11065 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
990ba854 11066
334208b7 11067 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7f2ae036
RS
11068 }
11069
11070 x_wm_set_icon_pixmap (f, bitmap_id);
7556890b 11071 f->output_data.x->icon_bitmap = bitmap_id;
dc6f92b8
JB
11072
11073 return 0;
11074}
11075
11076
1be2d067
KH
11077/* Make the x-window of frame F use a rectangle with text.
11078 Use ICON_NAME as the text. */
dc6f92b8
JB
11079
11080int
f676886a
JB
11081x_text_icon (f, icon_name)
11082 struct frame *f;
dc6f92b8
JB
11083 char *icon_name;
11084{
c118dd06 11085 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
11086 return 1;
11087
1be2d067
KH
11088#ifdef HAVE_X11R4
11089 {
11090 XTextProperty text;
11091 text.value = (unsigned char *) icon_name;
11092 text.encoding = XA_STRING;
11093 text.format = 8;
11094 text.nitems = strlen (icon_name);
11095#ifdef USE_X_TOOLKIT
7556890b 11096 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
1be2d067
KH
11097 &text);
11098#else /* not USE_X_TOOLKIT */
11099 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11100#endif /* not USE_X_TOOLKIT */
11101 }
11102#else /* not HAVE_X11R4 */
11103 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11104#endif /* not HAVE_X11R4 */
58769bee 11105
7556890b
RS
11106 if (f->output_data.x->icon_bitmap > 0)
11107 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11108 f->output_data.x->icon_bitmap = 0;
b1c884c3 11109 x_wm_set_icon_pixmap (f, 0);
dc6f92b8
JB
11110
11111 return 0;
11112}
11113\f
e99db5a1
RS
11114#define X_ERROR_MESSAGE_SIZE 200
11115
11116/* If non-nil, this should be a string.
11117 It means catch X errors and store the error message in this string. */
11118
11119static Lisp_Object x_error_message_string;
11120
11121/* An X error handler which stores the error message in
11122 x_error_message_string. This is called from x_error_handler if
11123 x_catch_errors is in effect. */
11124
06a2c219 11125static void
e99db5a1
RS
11126x_error_catcher (display, error)
11127 Display *display;
11128 XErrorEvent *error;
11129{
11130 XGetErrorText (display, error->error_code,
11131 XSTRING (x_error_message_string)->data,
11132 X_ERROR_MESSAGE_SIZE);
11133}
11134
11135/* Begin trapping X errors for display DPY. Actually we trap X errors
11136 for all displays, but DPY should be the display you are actually
11137 operating on.
11138
11139 After calling this function, X protocol errors no longer cause
11140 Emacs to exit; instead, they are recorded in the string
11141 stored in x_error_message_string.
11142
11143 Calling x_check_errors signals an Emacs error if an X error has
11144 occurred since the last call to x_catch_errors or x_check_errors.
11145
11146 Calling x_uncatch_errors resumes the normal error handling. */
11147
11148void x_check_errors ();
11149static Lisp_Object x_catch_errors_unwind ();
11150
11151int
11152x_catch_errors (dpy)
11153 Display *dpy;
11154{
11155 int count = specpdl_ptr - specpdl;
11156
11157 /* Make sure any errors from previous requests have been dealt with. */
11158 XSync (dpy, False);
11159
11160 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
11161
11162 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
11163 XSTRING (x_error_message_string)->data[0] = 0;
11164
11165 return count;
11166}
11167
11168/* Unbind the binding that we made to check for X errors. */
11169
11170static Lisp_Object
11171x_catch_errors_unwind (old_val)
11172 Lisp_Object old_val;
11173{
11174 x_error_message_string = old_val;
11175 return Qnil;
11176}
11177
11178/* If any X protocol errors have arrived since the last call to
11179 x_catch_errors or x_check_errors, signal an Emacs error using
11180 sprintf (a buffer, FORMAT, the x error message text) as the text. */
11181
11182void
11183x_check_errors (dpy, format)
11184 Display *dpy;
11185 char *format;
11186{
11187 /* Make sure to catch any errors incurred so far. */
11188 XSync (dpy, False);
11189
11190 if (XSTRING (x_error_message_string)->data[0])
11191 error (format, XSTRING (x_error_message_string)->data);
11192}
11193
9829ddba
RS
11194/* Nonzero if we had any X protocol errors
11195 since we did x_catch_errors on DPY. */
e99db5a1
RS
11196
11197int
11198x_had_errors_p (dpy)
11199 Display *dpy;
11200{
11201 /* Make sure to catch any errors incurred so far. */
11202 XSync (dpy, False);
11203
11204 return XSTRING (x_error_message_string)->data[0] != 0;
11205}
11206
9829ddba
RS
11207/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
11208
06a2c219 11209void
9829ddba
RS
11210x_clear_errors (dpy)
11211 Display *dpy;
11212{
11213 XSTRING (x_error_message_string)->data[0] = 0;
11214}
11215
e99db5a1
RS
11216/* Stop catching X protocol errors and let them make Emacs die.
11217 DPY should be the display that was passed to x_catch_errors.
11218 COUNT should be the value that was returned by
11219 the corresponding call to x_catch_errors. */
11220
11221void
11222x_uncatch_errors (dpy, count)
11223 Display *dpy;
11224 int count;
11225{
11226 unbind_to (count, Qnil);
11227}
11228
11229#if 0
11230static unsigned int x_wire_count;
11231x_trace_wire ()
11232{
11233 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
11234}
11235#endif /* ! 0 */
11236
11237\f
11238/* Handle SIGPIPE, which can happen when the connection to a server
11239 simply goes away. SIGPIPE is handled by x_connection_signal.
11240 Don't need to do anything, because the write which caused the
11241 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
06a2c219 11242 which will do the appropriate cleanup for us. */
e99db5a1
RS
11243
11244static SIGTYPE
11245x_connection_signal (signalnum) /* If we don't have an argument, */
06a2c219 11246 int signalnum; /* some compilers complain in signal calls. */
e99db5a1
RS
11247{
11248#ifdef USG
11249 /* USG systems forget handlers when they are used;
11250 must reestablish each time */
11251 signal (signalnum, x_connection_signal);
11252#endif /* USG */
11253}
0da1ab50 11254
e99db5a1 11255\f
0da1ab50
GM
11256/************************************************************************
11257 Handling X errors
11258 ************************************************************************/
4746118a 11259
0da1ab50
GM
11260/* Handle the loss of connection to display DPY. ERROR_MESSAGE is
11261 the text of an error message that lead to the connection loss. */
16bd92ea 11262
4746118a 11263static SIGTYPE
5978125e
GM
11264x_connection_closed (dpy, error_message)
11265 Display *dpy;
7a13e894 11266 char *error_message;
4746118a 11267{
5978125e 11268 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7a13e894 11269 Lisp_Object frame, tail;
0da1ab50
GM
11270 int count;
11271 char *msg;
11272
11273 msg = (char *) alloca (strlen (error_message) + 1);
11274 strcpy (msg, error_message);
1a532e54
GM
11275 handling_signal = 0;
11276
0da1ab50
GM
11277 /* Prevent being called recursively because of an error condition
11278 below. Otherwise, we might end up with printing ``can't find per
11279 display information'' in the recursive call instead of printing
11280 the original message here. */
11281 count = x_catch_errors (dpy);
11282
8a4f36cc
GM
11283 /* We have to close the display to inform Xt that it doesn't
11284 exist anymore. If we don't, Xt will continue to wait for
11285 events from the display. As a consequence, a sequence of
11286
11287 M-x make-frame-on-display RET :1 RET
11288 ...kill the new frame, so that we get an IO error...
11289 M-x make-frame-on-display RET :1 RET
11290
11291 will indefinitely wait in Xt for events for display `:1', opened
11292 in the first class to make-frame-on-display.
6186a4a0 11293
8a4f36cc
GM
11294 Closing the display is reported to lead to a bus error on
11295 OpenWindows in certain situations. I suspect that is a bug
11296 in OpenWindows. I don't know how to cicumvent it here. */
11297
f613a4c8 11298#ifdef USE_X_TOOLKIT
0da1ab50 11299 XtCloseDisplay (dpy);
f613a4c8 11300#endif
adabc3a9 11301
8a4f36cc 11302 /* Indicate that this display is dead. */
9e80b57d
KR
11303 if (dpyinfo)
11304 dpyinfo->display = 0;
6186a4a0 11305
06a2c219 11306 /* First delete frames whose mini-buffers are on frames
7a13e894
RS
11307 that are on the dead display. */
11308 FOR_EACH_FRAME (tail, frame)
11309 {
11310 Lisp_Object minibuf_frame;
11311 minibuf_frame
11312 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
f48f33ca
RS
11313 if (FRAME_X_P (XFRAME (frame))
11314 && FRAME_X_P (XFRAME (minibuf_frame))
11315 && ! EQ (frame, minibuf_frame)
11316 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7a13e894
RS
11317 Fdelete_frame (frame, Qt);
11318 }
11319
11320 /* Now delete all remaining frames on the dead display.
06a2c219 11321 We are now sure none of these is used as the mini-buffer
7a13e894
RS
11322 for another frame that we need to delete. */
11323 FOR_EACH_FRAME (tail, frame)
f48f33ca
RS
11324 if (FRAME_X_P (XFRAME (frame))
11325 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
07a7096a
KH
11326 {
11327 /* Set this to t so that Fdelete_frame won't get confused
11328 trying to find a replacement. */
11329 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
11330 Fdelete_frame (frame, Qt);
11331 }
7a13e894 11332
482a1bd2
KH
11333 if (dpyinfo)
11334 x_delete_display (dpyinfo);
7a13e894 11335
0da1ab50
GM
11336 x_uncatch_errors (dpy, count);
11337
7a13e894
RS
11338 if (x_display_list == 0)
11339 {
0da1ab50 11340 fprintf (stderr, "%s\n", msg);
7a13e894
RS
11341 shut_down_emacs (0, 0, Qnil);
11342 exit (70);
11343 }
12ba150f 11344
7a13e894
RS
11345 /* Ordinary stack unwind doesn't deal with these. */
11346#ifdef SIGIO
11347 sigunblock (sigmask (SIGIO));
11348#endif
11349 sigunblock (sigmask (SIGALRM));
11350 TOTALLY_UNBLOCK_INPUT;
11351
aa4d9a9e 11352 clear_waiting_for_input ();
0da1ab50 11353 error ("%s", msg);
4746118a
JB
11354}
11355
0da1ab50 11356
7a13e894
RS
11357/* This is the usual handler for X protocol errors.
11358 It kills all frames on the display that we got the error for.
11359 If that was the only one, it prints an error message and kills Emacs. */
11360
06a2c219 11361static void
c118dd06
JB
11362x_error_quitter (display, error)
11363 Display *display;
11364 XErrorEvent *error;
11365{
7a13e894 11366 char buf[256], buf1[356];
dc6f92b8 11367
58769bee 11368 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 11369 original error handler. */
dc6f92b8 11370
c118dd06 11371 XGetErrorText (display, error->error_code, buf, sizeof (buf));
0a0fdc70 11372 sprintf (buf1, "X protocol error: %s on protocol request %d",
c118dd06 11373 buf, error->request_code);
7a13e894 11374 x_connection_closed (display, buf1);
dc6f92b8
JB
11375}
11376
0da1ab50 11377
e99db5a1
RS
11378/* This is the first-level handler for X protocol errors.
11379 It calls x_error_quitter or x_error_catcher. */
7a13e894 11380
8922af5f 11381static int
e99db5a1 11382x_error_handler (display, error)
8922af5f 11383 Display *display;
e99db5a1 11384 XErrorEvent *error;
8922af5f 11385{
e99db5a1
RS
11386 if (! NILP (x_error_message_string))
11387 x_error_catcher (display, error);
11388 else
11389 x_error_quitter (display, error);
06a2c219 11390 return 0;
f9e24cb9 11391}
c118dd06 11392
e99db5a1
RS
11393/* This is the handler for X IO errors, always.
11394 It kills all frames on the display that we lost touch with.
11395 If that was the only one, it prints an error message and kills Emacs. */
7a13e894 11396
c118dd06 11397static int
e99db5a1 11398x_io_error_quitter (display)
c118dd06 11399 Display *display;
c118dd06 11400{
e99db5a1 11401 char buf[256];
dc6f92b8 11402
e99db5a1
RS
11403 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
11404 x_connection_closed (display, buf);
06a2c219 11405 return 0;
dc6f92b8 11406}
dc6f92b8 11407\f
f451eb13
JB
11408/* Changing the font of the frame. */
11409
76bcdf39
RS
11410/* Give frame F the font named FONTNAME as its default font, and
11411 return the full name of that font. FONTNAME may be a wildcard
11412 pattern; in that case, we choose some font that fits the pattern.
11413 The return value shows which font we chose. */
11414
b5cf7a0e 11415Lisp_Object
f676886a
JB
11416x_new_font (f, fontname)
11417 struct frame *f;
dc6f92b8
JB
11418 register char *fontname;
11419{
dc43ef94 11420 struct font_info *fontp
ee569018 11421 = FS_LOAD_FONT (f, 0, fontname, -1);
dc6f92b8 11422
dc43ef94
KH
11423 if (!fontp)
11424 return Qnil;
2224a5fc 11425
dc43ef94 11426 f->output_data.x->font = (XFontStruct *) (fontp->font);
b4192550 11427 f->output_data.x->baseline_offset = fontp->baseline_offset;
dc43ef94
KH
11428 f->output_data.x->fontset = -1;
11429
b2cad826
KH
11430 /* Compute the scroll bar width in character columns. */
11431 if (f->scroll_bar_pixel_width > 0)
11432 {
7556890b 11433 int wid = FONT_WIDTH (f->output_data.x->font);
b2cad826
KH
11434 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
11435 }
11436 else
4e61bddf
RS
11437 {
11438 int wid = FONT_WIDTH (f->output_data.x->font);
11439 f->scroll_bar_cols = (14 + wid - 1) / wid;
11440 }
b2cad826 11441
f676886a 11442 /* Now make the frame display the given font. */
c118dd06 11443 if (FRAME_X_WINDOW (f) != 0)
dc6f92b8 11444 {
7556890b
RS
11445 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
11446 f->output_data.x->font->fid);
11447 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
11448 f->output_data.x->font->fid);
11449 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
11450 f->output_data.x->font->fid);
f676886a 11451
a27f9f86 11452 frame_update_line_height (f);
0134a210 11453 x_set_window_size (f, 0, f->width, f->height);
dc6f92b8 11454 }
a27f9f86
RS
11455 else
11456 /* If we are setting a new frame's font for the first time,
11457 there are no faces yet, so this font's height is the line height. */
7556890b 11458 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
dc6f92b8 11459
dc43ef94
KH
11460 return build_string (fontp->full_name);
11461}
11462
11463/* Give frame F the fontset named FONTSETNAME as its default font, and
11464 return the full name of that fontset. FONTSETNAME may be a wildcard
b5210ea7
KH
11465 pattern; in that case, we choose some fontset that fits the pattern.
11466 The return value shows which fontset we chose. */
b5cf7a0e 11467
dc43ef94
KH
11468Lisp_Object
11469x_new_fontset (f, fontsetname)
11470 struct frame *f;
11471 char *fontsetname;
11472{
ee569018 11473 int fontset = fs_query_fontset (build_string (fontsetname), 0);
dc43ef94 11474 Lisp_Object result;
b5cf7a0e 11475
dc43ef94
KH
11476 if (fontset < 0)
11477 return Qnil;
b5cf7a0e 11478
2da424f1
KH
11479 if (f->output_data.x->fontset == fontset)
11480 /* This fontset is already set in frame F. There's nothing more
11481 to do. */
ee569018 11482 return fontset_name (fontset);
dc43ef94 11483
ee569018 11484 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
dc43ef94
KH
11485
11486 if (!STRINGP (result))
11487 /* Can't load ASCII font. */
11488 return Qnil;
11489
11490 /* Since x_new_font doesn't update any fontset information, do it now. */
11491 f->output_data.x->fontset = fontset;
dc43ef94 11492
f5d11644
GM
11493#ifdef HAVE_X_I18N
11494 if (FRAME_XIC (f)
11495 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
ee569018 11496 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
f5d11644
GM
11497#endif
11498
dc43ef94 11499 return build_string (fontsetname);
dc6f92b8 11500}
f5d11644
GM
11501
11502\f
11503/***********************************************************************
11504 X Input Methods
11505 ***********************************************************************/
11506
11507#ifdef HAVE_X_I18N
11508
11509#ifdef HAVE_X11R6
11510
11511/* XIM destroy callback function, which is called whenever the
11512 connection to input method XIM dies. CLIENT_DATA contains a
11513 pointer to the x_display_info structure corresponding to XIM. */
11514
11515static void
11516xim_destroy_callback (xim, client_data, call_data)
11517 XIM xim;
11518 XPointer client_data;
11519 XPointer call_data;
11520{
11521 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
11522 Lisp_Object frame, tail;
11523
11524 BLOCK_INPUT;
11525
11526 /* No need to call XDestroyIC.. */
11527 FOR_EACH_FRAME (tail, frame)
11528 {
11529 struct frame *f = XFRAME (frame);
11530 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
11531 {
11532 FRAME_XIC (f) = NULL;
11533 if (FRAME_XIC_FONTSET (f))
11534 {
11535 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
11536 FRAME_XIC_FONTSET (f) = NULL;
11537 }
11538 }
11539 }
11540
11541 /* No need to call XCloseIM. */
11542 dpyinfo->xim = NULL;
11543 XFree (dpyinfo->xim_styles);
11544 UNBLOCK_INPUT;
11545}
11546
11547#endif /* HAVE_X11R6 */
11548
11549/* Open the connection to the XIM server on display DPYINFO.
11550 RESOURCE_NAME is the resource name Emacs uses. */
11551
11552static void
11553xim_open_dpy (dpyinfo, resource_name)
11554 struct x_display_info *dpyinfo;
11555 char *resource_name;
11556{
287f7dd6 11557#ifdef USE_XIM
f5d11644
GM
11558 XIM xim;
11559
11560 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
11561 dpyinfo->xim = xim;
11562
11563 if (xim)
11564 {
f5d11644
GM
11565#ifdef HAVE_X11R6
11566 XIMCallback destroy;
11567#endif
11568
11569 /* Get supported styles and XIM values. */
11570 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
11571
11572#ifdef HAVE_X11R6
11573 destroy.callback = xim_destroy_callback;
11574 destroy.client_data = (XPointer)dpyinfo;
68642df6 11575 /* This isn't prptotyped in OSF 5.0. */
f5d11644
GM
11576 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
11577#endif
11578 }
287f7dd6
GM
11579
11580#else /* not USE_XIM */
11581 dpyinfo->xim = NULL;
11582#endif /* not USE_XIM */
f5d11644
GM
11583}
11584
11585
b9de836c 11586#ifdef HAVE_X11R6_XIM
f5d11644
GM
11587
11588struct xim_inst_t
11589{
11590 struct x_display_info *dpyinfo;
11591 char *resource_name;
11592};
11593
11594/* XIM instantiate callback function, which is called whenever an XIM
11595 server is available. DISPLAY is teh display of the XIM.
11596 CLIENT_DATA contains a pointer to an xim_inst_t structure created
11597 when the callback was registered. */
11598
11599static void
11600xim_instantiate_callback (display, client_data, call_data)
11601 Display *display;
11602 XPointer client_data;
11603 XPointer call_data;
11604{
11605 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
11606 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
11607
11608 /* We don't support multiple XIM connections. */
11609 if (dpyinfo->xim)
11610 return;
11611
11612 xim_open_dpy (dpyinfo, xim_inst->resource_name);
11613
11614 /* Create XIC for the existing frames on the same display, as long
11615 as they have no XIC. */
11616 if (dpyinfo->xim && dpyinfo->reference_count > 0)
11617 {
11618 Lisp_Object tail, frame;
11619
11620 BLOCK_INPUT;
11621 FOR_EACH_FRAME (tail, frame)
11622 {
11623 struct frame *f = XFRAME (frame);
11624
11625 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
11626 if (FRAME_XIC (f) == NULL)
11627 {
11628 create_frame_xic (f);
11629 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
11630 xic_set_statusarea (f);
11631 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
11632 {
11633 struct window *w = XWINDOW (f->selected_window);
11634 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
11635 }
11636 }
11637 }
11638
11639 UNBLOCK_INPUT;
11640 }
11641}
11642
b9de836c 11643#endif /* HAVE_X11R6_XIM */
f5d11644
GM
11644
11645
11646/* Open a connection to the XIM server on display DPYINFO.
11647 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
11648 connection only at the first time. On X11R6, open the connection
11649 in the XIM instantiate callback function. */
11650
11651static void
11652xim_initialize (dpyinfo, resource_name)
11653 struct x_display_info *dpyinfo;
11654 char *resource_name;
11655{
287f7dd6 11656#ifdef USE_XIM
b9de836c 11657#ifdef HAVE_X11R6_XIM
f5d11644
GM
11658 struct xim_inst_t *xim_inst;
11659 int len;
11660
11661 dpyinfo->xim = NULL;
11662 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
11663 xim_inst->dpyinfo = dpyinfo;
11664 len = strlen (resource_name);
11665 xim_inst->resource_name = (char *) xmalloc (len + 1);
11666 bcopy (resource_name, xim_inst->resource_name, len + 1);
11667 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11668 resource_name, EMACS_CLASS,
11669 xim_instantiate_callback,
2ebb2f8b
DL
11670 /* Fixme: This is XPointer in
11671 XFree86 but (XPointer *) on
11672 Tru64, at least. */
11673 (XPointer) xim_inst);
b9de836c 11674#else /* not HAVE_X11R6_XIM */
f5d11644
GM
11675 dpyinfo->xim = NULL;
11676 xim_open_dpy (dpyinfo, resource_name);
b9de836c 11677#endif /* not HAVE_X11R6_XIM */
287f7dd6
GM
11678
11679#else /* not USE_XIM */
11680 dpyinfo->xim = NULL;
11681#endif /* not USE_XIM */
f5d11644
GM
11682}
11683
11684
11685/* Close the connection to the XIM server on display DPYINFO. */
11686
11687static void
11688xim_close_dpy (dpyinfo)
11689 struct x_display_info *dpyinfo;
11690{
287f7dd6 11691#ifdef USE_XIM
b9de836c 11692#ifdef HAVE_X11R6_XIM
8a4f36cc
GM
11693 if (dpyinfo->display)
11694 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11695 NULL, EMACS_CLASS,
11696 xim_instantiate_callback, NULL);
b9de836c 11697#endif /* not HAVE_X11R6_XIM */
8a4f36cc
GM
11698 if (dpyinfo->display)
11699 XCloseIM (dpyinfo->xim);
f5d11644
GM
11700 dpyinfo->xim = NULL;
11701 XFree (dpyinfo->xim_styles);
287f7dd6 11702#endif /* USE_XIM */
f5d11644
GM
11703}
11704
b9de836c 11705#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
11706
11707
dc6f92b8 11708\f
2e365682
RS
11709/* Calculate the absolute position in frame F
11710 from its current recorded position values and gravity. */
11711
dfcf069d 11712void
43bca5d5 11713x_calc_absolute_position (f)
f676886a 11714 struct frame *f;
dc6f92b8 11715{
06a2c219 11716 Window child;
6dba1858 11717 int win_x = 0, win_y = 0;
7556890b 11718 int flags = f->output_data.x->size_hint_flags;
c81412a0
KH
11719 int this_window;
11720
9829ddba
RS
11721 /* We have nothing to do if the current position
11722 is already for the top-left corner. */
11723 if (! ((flags & XNegative) || (flags & YNegative)))
11724 return;
11725
c81412a0 11726#ifdef USE_X_TOOLKIT
7556890b 11727 this_window = XtWindow (f->output_data.x->widget);
c81412a0
KH
11728#else
11729 this_window = FRAME_X_WINDOW (f);
11730#endif
6dba1858
RS
11731
11732 /* Find the position of the outside upper-left corner of
9829ddba
RS
11733 the inner window, with respect to the outer window.
11734 But do this only if we will need the results. */
7556890b 11735 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6dba1858 11736 {
9829ddba
RS
11737 int count;
11738
6dba1858 11739 BLOCK_INPUT;
9829ddba
RS
11740 count = x_catch_errors (FRAME_X_DISPLAY (f));
11741 while (1)
11742 {
11743 x_clear_errors (FRAME_X_DISPLAY (f));
11744 XTranslateCoordinates (FRAME_X_DISPLAY (f),
11745
11746 /* From-window, to-window. */
11747 this_window,
11748 f->output_data.x->parent_desc,
11749
11750 /* From-position, to-position. */
11751 0, 0, &win_x, &win_y,
11752
11753 /* Child of win. */
11754 &child);
11755 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
11756 {
11757 Window newroot, newparent = 0xdeadbeef;
11758 Window *newchildren;
2ebb2f8b 11759 unsigned int nchildren;
9829ddba
RS
11760
11761 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
11762 &newparent, &newchildren, &nchildren))
11763 break;
58769bee 11764
7c3c78a3 11765 XFree ((char *) newchildren);
6dba1858 11766
9829ddba
RS
11767 f->output_data.x->parent_desc = newparent;
11768 }
11769 else
11770 break;
11771 }
6dba1858 11772
9829ddba 11773 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
6dba1858
RS
11774 UNBLOCK_INPUT;
11775 }
11776
11777 /* Treat negative positions as relative to the leftmost bottommost
11778 position that fits on the screen. */
20f55f9a 11779 if (flags & XNegative)
7556890b 11780 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
2e365682
RS
11781 - 2 * f->output_data.x->border_width - win_x
11782 - PIXEL_WIDTH (f)
11783 + f->output_data.x->left_pos);
dc6f92b8 11784
7708ced0
GM
11785 {
11786 int height = PIXEL_HEIGHT (f);
06a2c219 11787
7708ced0
GM
11788#if defined USE_X_TOOLKIT && defined USE_MOTIF
11789 /* Something is fishy here. When using Motif, starting Emacs with
11790 `-g -0-0', the frame appears too low by a few pixels.
11791
11792 This seems to be so because initially, while Emacs is starting,
11793 the column widget's height and the frame's pixel height are
11794 different. The column widget's height is the right one. In
11795 later invocations, when Emacs is up, the frame's pixel height
11796 is right, though.
11797
11798 It's not obvious where the initial small difference comes from.
11799 2000-12-01, gerd. */
11800
11801 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
06a2c219 11802#endif
2e365682 11803
7708ced0
GM
11804 if (flags & YNegative)
11805 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
11806 - 2 * f->output_data.x->border_width
11807 - win_y
11808 - height
11809 + f->output_data.x->top_pos);
11810 }
11811
3a35ab44
RS
11812 /* The left_pos and top_pos
11813 are now relative to the top and left screen edges,
11814 so the flags should correspond. */
7556890b 11815 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
dc6f92b8
JB
11816}
11817
3a35ab44
RS
11818/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
11819 to really change the position, and 0 when calling from
11820 x_make_frame_visible (in that case, XOFF and YOFF are the current
aa3ff7c9
KH
11821 position values). It is -1 when calling from x_set_frame_parameters,
11822 which means, do adjust for borders but don't change the gravity. */
3a35ab44 11823
dfcf069d 11824void
dc05a16b 11825x_set_offset (f, xoff, yoff, change_gravity)
f676886a 11826 struct frame *f;
dc6f92b8 11827 register int xoff, yoff;
dc05a16b 11828 int change_gravity;
dc6f92b8 11829{
4a4cbdd5
KH
11830 int modified_top, modified_left;
11831
aa3ff7c9 11832 if (change_gravity > 0)
3a35ab44 11833 {
7556890b
RS
11834 f->output_data.x->top_pos = yoff;
11835 f->output_data.x->left_pos = xoff;
11836 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
3a35ab44 11837 if (xoff < 0)
7556890b 11838 f->output_data.x->size_hint_flags |= XNegative;
3a35ab44 11839 if (yoff < 0)
7556890b
RS
11840 f->output_data.x->size_hint_flags |= YNegative;
11841 f->output_data.x->win_gravity = NorthWestGravity;
3a35ab44 11842 }
43bca5d5 11843 x_calc_absolute_position (f);
dc6f92b8
JB
11844
11845 BLOCK_INPUT;
c32cdd9a 11846 x_wm_set_size_hint (f, (long) 0, 0);
3a35ab44 11847
7556890b
RS
11848 modified_left = f->output_data.x->left_pos;
11849 modified_top = f->output_data.x->top_pos;
e73ec6fa
RS
11850#if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
11851 this seems to be unnecessary and incorrect. rms, 4/17/97. */
11852 /* It is a mystery why we need to add the border_width here
11853 when the frame is already visible, but experiment says we do. */
aa3ff7c9 11854 if (change_gravity != 0)
4a4cbdd5 11855 {
7556890b
RS
11856 modified_left += f->output_data.x->border_width;
11857 modified_top += f->output_data.x->border_width;
4a4cbdd5 11858 }
e73ec6fa 11859#endif
4a4cbdd5 11860
3afe33e7 11861#ifdef USE_X_TOOLKIT
7556890b 11862 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
4a4cbdd5 11863 modified_left, modified_top);
3afe33e7 11864#else /* not USE_X_TOOLKIT */
334208b7 11865 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4a4cbdd5 11866 modified_left, modified_top);
3afe33e7 11867#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11868 UNBLOCK_INPUT;
11869}
11870
bc20ebbf
FP
11871/* Call this to change the size of frame F's x-window.
11872 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
11873 for this size change and subsequent size changes.
11874 Otherwise we leave the window gravity unchanged. */
dc6f92b8 11875
dfcf069d 11876void
bc20ebbf 11877x_set_window_size (f, change_gravity, cols, rows)
f676886a 11878 struct frame *f;
bc20ebbf 11879 int change_gravity;
b1c884c3 11880 int cols, rows;
dc6f92b8 11881{
06a2c219 11882#ifndef USE_X_TOOLKIT
dc6f92b8 11883 int pixelwidth, pixelheight;
06a2c219 11884#endif
dc6f92b8 11885
80fd1fe2 11886 BLOCK_INPUT;
aee9a898
RS
11887
11888#ifdef USE_X_TOOLKIT
3a20653d
RS
11889 {
11890 /* The x and y position of the widget is clobbered by the
11891 call to XtSetValues within EmacsFrameSetCharSize.
11892 This is a real kludge, but I don't understand Xt so I can't
11893 figure out a correct fix. Can anyone else tell me? -- rms. */
7556890b
RS
11894 int xpos = f->output_data.x->widget->core.x;
11895 int ypos = f->output_data.x->widget->core.y;
11896 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
11897 f->output_data.x->widget->core.x = xpos;
11898 f->output_data.x->widget->core.y = ypos;
3a20653d 11899 }
80fd1fe2
FP
11900
11901#else /* not USE_X_TOOLKIT */
11902
b1c884c3 11903 check_frame_size (f, &rows, &cols);
7556890b 11904 f->output_data.x->vertical_scroll_bar_extra
b2cad826
KH
11905 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
11906 ? 0
11907 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
480407eb 11908 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7556890b 11909 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
06a2c219 11910 f->output_data.x->flags_areas_extra
110859fc 11911 = FRAME_FLAGS_AREA_WIDTH (f);
f451eb13
JB
11912 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
11913 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
dc6f92b8 11914
7556890b 11915 f->output_data.x->win_gravity = NorthWestGravity;
c32cdd9a 11916 x_wm_set_size_hint (f, (long) 0, 0);
6ccf47d1 11917
334208b7
RS
11918 XSync (FRAME_X_DISPLAY (f), False);
11919 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11920 pixelwidth, pixelheight);
b1c884c3
JB
11921
11922 /* Now, strictly speaking, we can't be sure that this is accurate,
11923 but the window manager will get around to dealing with the size
11924 change request eventually, and we'll hear how it went when the
dbc4e1c1
JB
11925 ConfigureNotify event gets here.
11926
11927 We could just not bother storing any of this information here,
11928 and let the ConfigureNotify event set everything up, but that
fddd5ceb 11929 might be kind of confusing to the Lisp code, since size changes
dbc4e1c1 11930 wouldn't be reported in the frame parameters until some random
fddd5ceb
RS
11931 point in the future when the ConfigureNotify event arrives.
11932
11933 We pass 1 for DELAY since we can't run Lisp code inside of
11934 a BLOCK_INPUT. */
7d1e984f 11935 change_frame_size (f, rows, cols, 0, 1, 0);
b1c884c3
JB
11936 PIXEL_WIDTH (f) = pixelwidth;
11937 PIXEL_HEIGHT (f) = pixelheight;
11938
aee9a898
RS
11939 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11940 receive in the ConfigureNotify event; if we get what we asked
11941 for, then the event won't cause the screen to become garbaged, so
11942 we have to make sure to do it here. */
11943 SET_FRAME_GARBAGED (f);
11944
11945 XFlush (FRAME_X_DISPLAY (f));
11946
11947#endif /* not USE_X_TOOLKIT */
11948
4d73d038 11949 /* If cursor was outside the new size, mark it as off. */
06a2c219 11950 mark_window_cursors_off (XWINDOW (f->root_window));
4d73d038 11951
aee9a898
RS
11952 /* Clear out any recollection of where the mouse highlighting was,
11953 since it might be in a place that's outside the new frame size.
11954 Actually checking whether it is outside is a pain in the neck,
11955 so don't try--just let the highlighting be done afresh with new size. */
e687d06e 11956 cancel_mouse_face (f);
dbc4e1c1 11957
dc6f92b8
JB
11958 UNBLOCK_INPUT;
11959}
dc6f92b8 11960\f
d047c4eb 11961/* Mouse warping. */
dc6f92b8 11962
9b378208 11963void
f676886a
JB
11964x_set_mouse_position (f, x, y)
11965 struct frame *f;
dc6f92b8
JB
11966 int x, y;
11967{
11968 int pix_x, pix_y;
11969
7556890b
RS
11970 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
11971 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
f451eb13
JB
11972
11973 if (pix_x < 0) pix_x = 0;
11974 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
11975
11976 if (pix_y < 0) pix_y = 0;
11977 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
dc6f92b8
JB
11978
11979 BLOCK_INPUT;
dc6f92b8 11980
334208b7
RS
11981 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11982 0, 0, 0, 0, pix_x, pix_y);
dc6f92b8
JB
11983 UNBLOCK_INPUT;
11984}
11985
9b378208
RS
11986/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11987
11988void
11989x_set_mouse_pixel_position (f, pix_x, pix_y)
11990 struct frame *f;
11991 int pix_x, pix_y;
11992{
11993 BLOCK_INPUT;
11994
334208b7
RS
11995 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11996 0, 0, 0, 0, pix_x, pix_y);
9b378208
RS
11997 UNBLOCK_INPUT;
11998}
d047c4eb
KH
11999\f
12000/* focus shifting, raising and lowering. */
9b378208 12001
dfcf069d 12002void
f676886a
JB
12003x_focus_on_frame (f)
12004 struct frame *f;
dc6f92b8 12005{
1fb20991 12006#if 0 /* This proves to be unpleasant. */
f676886a 12007 x_raise_frame (f);
1fb20991 12008#endif
6d4238f3
JB
12009#if 0
12010 /* I don't think that the ICCCM allows programs to do things like this
12011 without the interaction of the window manager. Whatever you end up
f676886a 12012 doing with this code, do it to x_unfocus_frame too. */
334208b7 12013 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dc6f92b8 12014 RevertToPointerRoot, CurrentTime);
c118dd06 12015#endif /* ! 0 */
dc6f92b8
JB
12016}
12017
dfcf069d 12018void
f676886a
JB
12019x_unfocus_frame (f)
12020 struct frame *f;
dc6f92b8 12021{
6d4238f3 12022#if 0
f676886a 12023 /* Look at the remarks in x_focus_on_frame. */
0f941935 12024 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
334208b7 12025 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
dc6f92b8 12026 RevertToPointerRoot, CurrentTime);
c118dd06 12027#endif /* ! 0 */
dc6f92b8
JB
12028}
12029
f676886a 12030/* Raise frame F. */
dc6f92b8 12031
dfcf069d 12032void
f676886a
JB
12033x_raise_frame (f)
12034 struct frame *f;
dc6f92b8 12035{
3a88c238 12036 if (f->async_visible)
dc6f92b8
JB
12037 {
12038 BLOCK_INPUT;
3afe33e7 12039#ifdef USE_X_TOOLKIT
7556890b 12040 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 12041#else /* not USE_X_TOOLKIT */
334208b7 12042 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 12043#endif /* not USE_X_TOOLKIT */
334208b7 12044 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
12045 UNBLOCK_INPUT;
12046 }
12047}
12048
f676886a 12049/* Lower frame F. */
dc6f92b8 12050
dfcf069d 12051void
f676886a
JB
12052x_lower_frame (f)
12053 struct frame *f;
dc6f92b8 12054{
3a88c238 12055 if (f->async_visible)
dc6f92b8
JB
12056 {
12057 BLOCK_INPUT;
3afe33e7 12058#ifdef USE_X_TOOLKIT
7556890b 12059 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 12060#else /* not USE_X_TOOLKIT */
334208b7 12061 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 12062#endif /* not USE_X_TOOLKIT */
334208b7 12063 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
12064 UNBLOCK_INPUT;
12065 }
12066}
12067
dbc4e1c1 12068static void
6b0442dc 12069XTframe_raise_lower (f, raise_flag)
dbc4e1c1 12070 FRAME_PTR f;
6b0442dc 12071 int raise_flag;
dbc4e1c1 12072{
6b0442dc 12073 if (raise_flag)
dbc4e1c1
JB
12074 x_raise_frame (f);
12075 else
12076 x_lower_frame (f);
12077}
d047c4eb
KH
12078\f
12079/* Change of visibility. */
dc6f92b8 12080
9382638d
KH
12081/* This tries to wait until the frame is really visible.
12082 However, if the window manager asks the user where to position
12083 the frame, this will return before the user finishes doing that.
12084 The frame will not actually be visible at that time,
12085 but it will become visible later when the window manager
12086 finishes with it. */
12087
dfcf069d 12088void
f676886a
JB
12089x_make_frame_visible (f)
12090 struct frame *f;
dc6f92b8 12091{
990ba854 12092 Lisp_Object type;
1aa6072f 12093 int original_top, original_left;
31be9251
GM
12094 int retry_count = 2;
12095
12096 retry:
dc6f92b8 12097
dc6f92b8 12098 BLOCK_INPUT;
dc6f92b8 12099
990ba854
RS
12100 type = x_icon_type (f);
12101 if (!NILP (type))
12102 x_bitmap_icon (f, type);
bdcd49ba 12103
f676886a 12104 if (! FRAME_VISIBLE_P (f))
90e65f07 12105 {
1aa6072f
RS
12106 /* We test FRAME_GARBAGED_P here to make sure we don't
12107 call x_set_offset a second time
12108 if we get to x_make_frame_visible a second time
12109 before the window gets really visible. */
12110 if (! FRAME_ICONIFIED_P (f)
12111 && ! f->output_data.x->asked_for_visible)
12112 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12113
12114 f->output_data.x->asked_for_visible = 1;
12115
90e65f07 12116 if (! EQ (Vx_no_window_manager, Qt))
f676886a 12117 x_wm_set_window_state (f, NormalState);
3afe33e7 12118#ifdef USE_X_TOOLKIT
d7a38a2e 12119 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 12120 XtMapWidget (f->output_data.x->widget);
3afe33e7 12121#else /* not USE_X_TOOLKIT */
7f9c7f94 12122 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 12123#endif /* not USE_X_TOOLKIT */
0134a210
RS
12124#if 0 /* This seems to bring back scroll bars in the wrong places
12125 if the window configuration has changed. They seem
12126 to come back ok without this. */
ab648270 12127 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
334208b7 12128 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
0134a210 12129#endif
90e65f07 12130 }
dc6f92b8 12131
334208b7 12132 XFlush (FRAME_X_DISPLAY (f));
90e65f07 12133
0dacf791
RS
12134 /* Synchronize to ensure Emacs knows the frame is visible
12135 before we do anything else. We do this loop with input not blocked
12136 so that incoming events are handled. */
12137 {
12138 Lisp_Object frame;
12ce2351 12139 int count;
28c01ffe
RS
12140 /* This must be before UNBLOCK_INPUT
12141 since events that arrive in response to the actions above
12142 will set it when they are handled. */
12143 int previously_visible = f->output_data.x->has_been_visible;
1aa6072f
RS
12144
12145 original_left = f->output_data.x->left_pos;
12146 original_top = f->output_data.x->top_pos;
c0a04927
RS
12147
12148 /* This must come after we set COUNT. */
12149 UNBLOCK_INPUT;
12150
2745e6c4 12151 /* We unblock here so that arriving X events are processed. */
1aa6072f 12152
dcb07ae9
RS
12153 /* Now move the window back to where it was "supposed to be".
12154 But don't do it if the gravity is negative.
12155 When the gravity is negative, this uses a position
28c01ffe
RS
12156 that is 3 pixels too low. Perhaps that's really the border width.
12157
12158 Don't do this if the window has never been visible before,
12159 because the window manager may choose the position
12160 and we don't want to override it. */
1aa6072f 12161
4d3f5d9a 12162 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
06c488fd 12163 && f->output_data.x->win_gravity == NorthWestGravity
28c01ffe 12164 && previously_visible)
1aa6072f 12165 {
2745e6c4
RS
12166 Drawable rootw;
12167 int x, y;
12168 unsigned int width, height, border, depth;
06a2c219 12169
1aa6072f 12170 BLOCK_INPUT;
9829ddba 12171
06a2c219
GM
12172 /* On some window managers (such as FVWM) moving an existing
12173 window, even to the same place, causes the window manager
12174 to introduce an offset. This can cause the window to move
12175 to an unexpected location. Check the geometry (a little
12176 slow here) and then verify that the window is in the right
12177 place. If the window is not in the right place, move it
12178 there, and take the potential window manager hit. */
2745e6c4
RS
12179 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12180 &rootw, &x, &y, &width, &height, &border, &depth);
12181
12182 if (original_left != x || original_top != y)
12183 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12184 original_left, original_top);
12185
1aa6072f
RS
12186 UNBLOCK_INPUT;
12187 }
9829ddba 12188
e0c1aef2 12189 XSETFRAME (frame, f);
c0a04927 12190
12ce2351
GM
12191 /* Wait until the frame is visible. Process X events until a
12192 MapNotify event has been seen, or until we think we won't get a
12193 MapNotify at all.. */
12194 for (count = input_signal_count + 10;
12195 input_signal_count < count && !FRAME_VISIBLE_P (f);)
2a6cf806 12196 {
12ce2351 12197 /* Force processing of queued events. */
334208b7 12198 x_sync (f);
12ce2351
GM
12199
12200 /* Machines that do polling rather than SIGIO have been
12201 observed to go into a busy-wait here. So we'll fake an
12202 alarm signal to let the handler know that there's something
12203 to be read. We used to raise a real alarm, but it seems
12204 that the handler isn't always enabled here. This is
12205 probably a bug. */
8b2f8d4e 12206 if (input_polling_used ())
3b2fa4e6 12207 {
12ce2351
GM
12208 /* It could be confusing if a real alarm arrives while
12209 processing the fake one. Turn it off and let the
12210 handler reset it. */
3e71d8f2 12211 extern void poll_for_input_1 P_ ((void));
bffcfca9
GM
12212 int old_poll_suppress_count = poll_suppress_count;
12213 poll_suppress_count = 1;
12214 poll_for_input_1 ();
12215 poll_suppress_count = old_poll_suppress_count;
3b2fa4e6 12216 }
12ce2351
GM
12217
12218 /* See if a MapNotify event has been processed. */
12219 FRAME_SAMPLE_VISIBILITY (f);
2a6cf806 12220 }
31be9251
GM
12221
12222 /* 2000-09-28: In
12223
12224 (let ((f (selected-frame)))
12225 (iconify-frame f)
12226 (raise-frame f))
12227
12228 the frame is not raised with various window managers on
12229 FreeBSD, Linux and Solaris. It turns out that, for some
12230 unknown reason, the call to XtMapWidget is completely ignored.
12231 Mapping the widget a second time works. */
12232
12233 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
12234 goto retry;
0dacf791 12235 }
dc6f92b8
JB
12236}
12237
06a2c219 12238/* Change from mapped state to withdrawn state. */
dc6f92b8 12239
d047c4eb
KH
12240/* Make the frame visible (mapped and not iconified). */
12241
dfcf069d 12242void
f676886a
JB
12243x_make_frame_invisible (f)
12244 struct frame *f;
dc6f92b8 12245{
546e6d5b
RS
12246 Window window;
12247
12248#ifdef USE_X_TOOLKIT
12249 /* Use the frame's outermost window, not the one we normally draw on. */
7556890b 12250 window = XtWindow (f->output_data.x->widget);
546e6d5b
RS
12251#else /* not USE_X_TOOLKIT */
12252 window = FRAME_X_WINDOW (f);
12253#endif /* not USE_X_TOOLKIT */
dc6f92b8 12254
9319ae23 12255 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
12256 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12257 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 12258
5627c40e 12259#if 0/* This might add unreliability; I don't trust it -- rms. */
9319ae23 12260 if (! f->async_visible && ! f->async_iconified)
dc6f92b8 12261 return;
5627c40e 12262#endif
dc6f92b8
JB
12263
12264 BLOCK_INPUT;
c118dd06 12265
af31d76f
RS
12266 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
12267 that the current position of the window is user-specified, rather than
12268 program-specified, so that when the window is mapped again, it will be
12269 placed at the same location, without forcing the user to position it
12270 by hand again (they have already done that once for this window.) */
c32cdd9a 12271 x_wm_set_size_hint (f, (long) 0, 1);
af31d76f 12272
c118dd06
JB
12273#ifdef HAVE_X11R4
12274
334208b7
RS
12275 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
12276 DefaultScreen (FRAME_X_DISPLAY (f))))
c118dd06
JB
12277 {
12278 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 12279 error ("Can't notify window manager of window withdrawal");
c118dd06 12280 }
c118dd06 12281#else /* ! defined (HAVE_X11R4) */
16bd92ea 12282
c118dd06 12283 /* Tell the window manager what we're going to do. */
dc6f92b8
JB
12284 if (! EQ (Vx_no_window_manager, Qt))
12285 {
16bd92ea 12286 XEvent unmap;
dc6f92b8 12287
16bd92ea 12288 unmap.xunmap.type = UnmapNotify;
546e6d5b 12289 unmap.xunmap.window = window;
334208b7 12290 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
16bd92ea 12291 unmap.xunmap.from_configure = False;
334208b7
RS
12292 if (! XSendEvent (FRAME_X_DISPLAY (f),
12293 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea 12294 False,
06a2c219 12295 SubstructureRedirectMaskSubstructureNotifyMask,
16bd92ea
JB
12296 &unmap))
12297 {
12298 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 12299 error ("Can't notify window manager of withdrawal");
16bd92ea 12300 }
dc6f92b8
JB
12301 }
12302
16bd92ea 12303 /* Unmap the window ourselves. Cheeky! */
334208b7 12304 XUnmapWindow (FRAME_X_DISPLAY (f), window);
c118dd06 12305#endif /* ! defined (HAVE_X11R4) */
dc6f92b8 12306
5627c40e
RS
12307 /* We can't distinguish this from iconification
12308 just by the event that we get from the server.
12309 So we can't win using the usual strategy of letting
12310 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
12311 and synchronize with the server to make sure we agree. */
12312 f->visible = 0;
12313 FRAME_ICONIFIED_P (f) = 0;
12314 f->async_visible = 0;
12315 f->async_iconified = 0;
12316
334208b7 12317 x_sync (f);
5627c40e 12318
dc6f92b8
JB
12319 UNBLOCK_INPUT;
12320}
12321
06a2c219 12322/* Change window state from mapped to iconified. */
dc6f92b8 12323
dfcf069d 12324void
f676886a
JB
12325x_iconify_frame (f)
12326 struct frame *f;
dc6f92b8 12327{
3afe33e7 12328 int result;
990ba854 12329 Lisp_Object type;
dc6f92b8 12330
9319ae23 12331 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
12332 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12333 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 12334
3a88c238 12335 if (f->async_iconified)
dc6f92b8
JB
12336 return;
12337
3afe33e7 12338 BLOCK_INPUT;
546e6d5b 12339
9af3143a
RS
12340 FRAME_SAMPLE_VISIBILITY (f);
12341
990ba854
RS
12342 type = x_icon_type (f);
12343 if (!NILP (type))
12344 x_bitmap_icon (f, type);
bdcd49ba
RS
12345
12346#ifdef USE_X_TOOLKIT
12347
546e6d5b
RS
12348 if (! FRAME_VISIBLE_P (f))
12349 {
12350 if (! EQ (Vx_no_window_manager, Qt))
12351 x_wm_set_window_state (f, IconicState);
12352 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 12353 XtMapWidget (f->output_data.x->widget);
9cf30a30
RS
12354 /* The server won't give us any event to indicate
12355 that an invisible frame was changed to an icon,
12356 so we have to record it here. */
12357 f->iconified = 1;
1e6bc770 12358 f->visible = 1;
9cf30a30 12359 f->async_iconified = 1;
1e6bc770 12360 f->async_visible = 0;
546e6d5b
RS
12361 UNBLOCK_INPUT;
12362 return;
12363 }
12364
334208b7 12365 result = XIconifyWindow (FRAME_X_DISPLAY (f),
7556890b 12366 XtWindow (f->output_data.x->widget),
334208b7 12367 DefaultScreen (FRAME_X_DISPLAY (f)));
3afe33e7
RS
12368 UNBLOCK_INPUT;
12369
12370 if (!result)
546e6d5b 12371 error ("Can't notify window manager of iconification");
3afe33e7
RS
12372
12373 f->async_iconified = 1;
1e6bc770
RS
12374 f->async_visible = 0;
12375
8c002a25
KH
12376
12377 BLOCK_INPUT;
334208b7 12378 XFlush (FRAME_X_DISPLAY (f));
8c002a25 12379 UNBLOCK_INPUT;
3afe33e7
RS
12380#else /* not USE_X_TOOLKIT */
12381
fd13dbb2
RS
12382 /* Make sure the X server knows where the window should be positioned,
12383 in case the user deiconifies with the window manager. */
12384 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
7556890b 12385 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
fd13dbb2 12386
16bd92ea
JB
12387 /* Since we don't know which revision of X we're running, we'll use both
12388 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
12389
12390 /* X11R4: send a ClientMessage to the window manager using the
12391 WM_CHANGE_STATE type. */
12392 {
12393 XEvent message;
58769bee 12394
c118dd06 12395 message.xclient.window = FRAME_X_WINDOW (f);
16bd92ea 12396 message.xclient.type = ClientMessage;
334208b7 12397 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
16bd92ea
JB
12398 message.xclient.format = 32;
12399 message.xclient.data.l[0] = IconicState;
12400
334208b7
RS
12401 if (! XSendEvent (FRAME_X_DISPLAY (f),
12402 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea
JB
12403 False,
12404 SubstructureRedirectMask | SubstructureNotifyMask,
12405 &message))
dc6f92b8
JB
12406 {
12407 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 12408 error ("Can't notify window manager of iconification");
dc6f92b8 12409 }
16bd92ea 12410 }
dc6f92b8 12411
58769bee 12412 /* X11R3: set the initial_state field of the window manager hints to
16bd92ea
JB
12413 IconicState. */
12414 x_wm_set_window_state (f, IconicState);
dc6f92b8 12415
a9c00105
RS
12416 if (!FRAME_VISIBLE_P (f))
12417 {
12418 /* If the frame was withdrawn, before, we must map it. */
7f9c7f94 12419 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
a9c00105
RS
12420 }
12421
3a88c238 12422 f->async_iconified = 1;
1e6bc770 12423 f->async_visible = 0;
dc6f92b8 12424
334208b7 12425 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 12426 UNBLOCK_INPUT;
8c002a25 12427#endif /* not USE_X_TOOLKIT */
dc6f92b8 12428}
19f71add 12429
d047c4eb 12430\f
19f71add 12431/* Free X resources of frame F. */
dc6f92b8 12432
dfcf069d 12433void
19f71add 12434x_free_frame_resources (f)
f676886a 12435 struct frame *f;
dc6f92b8 12436{
7f9c7f94
RS
12437 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12438
dc6f92b8 12439 BLOCK_INPUT;
c0ff3fab 12440
6186a4a0
RS
12441 /* If a display connection is dead, don't try sending more
12442 commands to the X server. */
19f71add 12443 if (dpyinfo->display)
6186a4a0 12444 {
19f71add 12445 if (f->output_data.x->icon_desc)
6186a4a0 12446 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
19f71add 12447
31f41daf 12448#ifdef HAVE_X_I18N
f5d11644
GM
12449 if (FRAME_XIC (f))
12450 free_frame_xic (f);
31f41daf 12451#endif
19f71add 12452
2662734b 12453 if (FRAME_X_WINDOW (f))
19f71add
GM
12454 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12455
3afe33e7 12456#ifdef USE_X_TOOLKIT
06a2c219
GM
12457 if (f->output_data.x->widget)
12458 XtDestroyWidget (f->output_data.x->widget);
6186a4a0 12459 free_frame_menubar (f);
3afe33e7
RS
12460#endif /* USE_X_TOOLKIT */
12461
3e71d8f2
GM
12462 unload_color (f, f->output_data.x->foreground_pixel);
12463 unload_color (f, f->output_data.x->background_pixel);
12464 unload_color (f, f->output_data.x->cursor_pixel);
12465 unload_color (f, f->output_data.x->cursor_foreground_pixel);
12466 unload_color (f, f->output_data.x->border_pixel);
12467 unload_color (f, f->output_data.x->mouse_pixel);
19f71add 12468
3e71d8f2
GM
12469 if (f->output_data.x->scroll_bar_background_pixel != -1)
12470 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
12471 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
12472 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
12473 if (f->output_data.x->white_relief.allocated_p)
12474 unload_color (f, f->output_data.x->white_relief.pixel);
12475 if (f->output_data.x->black_relief.allocated_p)
12476 unload_color (f, f->output_data.x->black_relief.pixel);
4ca78676 12477
19f71add
GM
12478 if (FRAME_FACE_CACHE (f))
12479 free_frame_faces (f);
12480
4ca78676 12481 x_free_gcs (f);
6186a4a0
RS
12482 XFlush (FRAME_X_DISPLAY (f));
12483 }
dc6f92b8 12484
df89d8a4 12485 if (f->output_data.x->saved_menu_event)
06a2c219 12486 xfree (f->output_data.x->saved_menu_event);
0c49ce2f 12487
7556890b 12488 xfree (f->output_data.x);
19f71add
GM
12489 f->output_data.x = NULL;
12490
0f941935
KH
12491 if (f == dpyinfo->x_focus_frame)
12492 dpyinfo->x_focus_frame = 0;
12493 if (f == dpyinfo->x_focus_event_frame)
12494 dpyinfo->x_focus_event_frame = 0;
12495 if (f == dpyinfo->x_highlight_frame)
12496 dpyinfo->x_highlight_frame = 0;
c0ff3fab 12497
7f9c7f94 12498 if (f == dpyinfo->mouse_face_mouse_frame)
dc05a16b 12499 {
7f9c7f94
RS
12500 dpyinfo->mouse_face_beg_row
12501 = dpyinfo->mouse_face_beg_col = -1;
12502 dpyinfo->mouse_face_end_row
12503 = dpyinfo->mouse_face_end_col = -1;
12504 dpyinfo->mouse_face_window = Qnil;
21323706
RS
12505 dpyinfo->mouse_face_deferred_gc = 0;
12506 dpyinfo->mouse_face_mouse_frame = 0;
dc05a16b 12507 }
0134a210 12508
c0ff3fab 12509 UNBLOCK_INPUT;
dc6f92b8 12510}
19f71add
GM
12511
12512
12513/* Destroy the X window of frame F. */
12514
12515void
12516x_destroy_window (f)
12517 struct frame *f;
12518{
12519 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12520
12521 /* If a display connection is dead, don't try sending more
12522 commands to the X server. */
12523 if (dpyinfo->display != 0)
12524 x_free_frame_resources (f);
12525
12526 dpyinfo->reference_count--;
12527}
12528
dc6f92b8 12529\f
f451eb13
JB
12530/* Setting window manager hints. */
12531
af31d76f
RS
12532/* Set the normal size hints for the window manager, for frame F.
12533 FLAGS is the flags word to use--or 0 meaning preserve the flags
12534 that the window now has.
12535 If USER_POSITION is nonzero, we set the USPosition
12536 flag (this is useful when FLAGS is 0). */
6dba1858 12537
dfcf069d 12538void
af31d76f 12539x_wm_set_size_hint (f, flags, user_position)
f676886a 12540 struct frame *f;
af31d76f
RS
12541 long flags;
12542 int user_position;
dc6f92b8
JB
12543{
12544 XSizeHints size_hints;
3afe33e7
RS
12545
12546#ifdef USE_X_TOOLKIT
7e4f2521
FP
12547 Arg al[2];
12548 int ac = 0;
12549 Dimension widget_width, widget_height;
7556890b 12550 Window window = XtWindow (f->output_data.x->widget);
3afe33e7 12551#else /* not USE_X_TOOLKIT */
c118dd06 12552 Window window = FRAME_X_WINDOW (f);
3afe33e7 12553#endif /* not USE_X_TOOLKIT */
dc6f92b8 12554
b72a58fd
RS
12555 /* Setting PMaxSize caused various problems. */
12556 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
dc6f92b8 12557
7556890b
RS
12558 size_hints.x = f->output_data.x->left_pos;
12559 size_hints.y = f->output_data.x->top_pos;
7553a6b7 12560
7e4f2521
FP
12561#ifdef USE_X_TOOLKIT
12562 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
12563 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
7556890b 12564 XtGetValues (f->output_data.x->widget, al, ac);
7e4f2521
FP
12565 size_hints.height = widget_height;
12566 size_hints.width = widget_width;
12567#else /* not USE_X_TOOLKIT */
f676886a
JB
12568 size_hints.height = PIXEL_HEIGHT (f);
12569 size_hints.width = PIXEL_WIDTH (f);
7e4f2521 12570#endif /* not USE_X_TOOLKIT */
7553a6b7 12571
7556890b
RS
12572 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
12573 size_hints.height_inc = f->output_data.x->line_height;
334208b7
RS
12574 size_hints.max_width
12575 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
12576 size_hints.max_height
12577 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
0134a210 12578
d067ea8b
KH
12579 /* Calculate the base and minimum sizes.
12580
12581 (When we use the X toolkit, we don't do it here.
12582 Instead we copy the values that the widgets are using, below.) */
12583#ifndef USE_X_TOOLKIT
b1c884c3 12584 {
b0342f17 12585 int base_width, base_height;
0134a210 12586 int min_rows = 0, min_cols = 0;
b0342f17 12587
f451eb13
JB
12588 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
12589 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
b0342f17 12590
0134a210 12591 check_frame_size (f, &min_rows, &min_cols);
b0342f17 12592
0134a210
RS
12593 /* The window manager uses the base width hints to calculate the
12594 current number of rows and columns in the frame while
12595 resizing; min_width and min_height aren't useful for this
12596 purpose, since they might not give the dimensions for a
12597 zero-row, zero-column frame.
58769bee 12598
0134a210
RS
12599 We use the base_width and base_height members if we have
12600 them; otherwise, we set the min_width and min_height members
12601 to the size for a zero x zero frame. */
b0342f17
JB
12602
12603#ifdef HAVE_X11R4
0134a210
RS
12604 size_hints.flags |= PBaseSize;
12605 size_hints.base_width = base_width;
12606 size_hints.base_height = base_height;
12607 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
12608 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
b0342f17 12609#else
0134a210
RS
12610 size_hints.min_width = base_width;
12611 size_hints.min_height = base_height;
b0342f17 12612#endif
b1c884c3 12613 }
dc6f92b8 12614
d067ea8b 12615 /* If we don't need the old flags, we don't need the old hint at all. */
af31d76f 12616 if (flags)
dc6f92b8 12617 {
d067ea8b
KH
12618 size_hints.flags |= flags;
12619 goto no_read;
12620 }
12621#endif /* not USE_X_TOOLKIT */
12622
12623 {
12624 XSizeHints hints; /* Sometimes I hate X Windows... */
12625 long supplied_return;
12626 int value;
af31d76f
RS
12627
12628#ifdef HAVE_X11R4
d067ea8b
KH
12629 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
12630 &supplied_return);
af31d76f 12631#else
d067ea8b 12632 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
af31d76f 12633#endif
58769bee 12634
d067ea8b
KH
12635#ifdef USE_X_TOOLKIT
12636 size_hints.base_height = hints.base_height;
12637 size_hints.base_width = hints.base_width;
12638 size_hints.min_height = hints.min_height;
12639 size_hints.min_width = hints.min_width;
12640#endif
12641
12642 if (flags)
12643 size_hints.flags |= flags;
12644 else
12645 {
12646 if (value == 0)
12647 hints.flags = 0;
12648 if (hints.flags & PSize)
12649 size_hints.flags |= PSize;
12650 if (hints.flags & PPosition)
12651 size_hints.flags |= PPosition;
12652 if (hints.flags & USPosition)
12653 size_hints.flags |= USPosition;
12654 if (hints.flags & USSize)
12655 size_hints.flags |= USSize;
12656 }
12657 }
12658
06a2c219 12659#ifndef USE_X_TOOLKIT
d067ea8b 12660 no_read:
06a2c219 12661#endif
0134a210 12662
af31d76f 12663#ifdef PWinGravity
7556890b 12664 size_hints.win_gravity = f->output_data.x->win_gravity;
af31d76f 12665 size_hints.flags |= PWinGravity;
dc05a16b 12666
af31d76f 12667 if (user_position)
6dba1858 12668 {
af31d76f
RS
12669 size_hints.flags &= ~ PPosition;
12670 size_hints.flags |= USPosition;
6dba1858 12671 }
2554751d 12672#endif /* PWinGravity */
6dba1858 12673
b0342f17 12674#ifdef HAVE_X11R4
334208b7 12675 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 12676#else
334208b7 12677 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 12678#endif
dc6f92b8
JB
12679}
12680
12681/* Used for IconicState or NormalState */
06a2c219 12682
dfcf069d 12683void
f676886a
JB
12684x_wm_set_window_state (f, state)
12685 struct frame *f;
dc6f92b8
JB
12686 int state;
12687{
3afe33e7 12688#ifdef USE_X_TOOLKIT
546e6d5b
RS
12689 Arg al[1];
12690
12691 XtSetArg (al[0], XtNinitialState, state);
7556890b 12692 XtSetValues (f->output_data.x->widget, al, 1);
3afe33e7 12693#else /* not USE_X_TOOLKIT */
c118dd06 12694 Window window = FRAME_X_WINDOW (f);
dc6f92b8 12695
7556890b
RS
12696 f->output_data.x->wm_hints.flags |= StateHint;
12697 f->output_data.x->wm_hints.initial_state = state;
b1c884c3 12698
7556890b 12699 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
546e6d5b 12700#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12701}
12702
dfcf069d 12703void
7f2ae036 12704x_wm_set_icon_pixmap (f, pixmap_id)
f676886a 12705 struct frame *f;
7f2ae036 12706 int pixmap_id;
dc6f92b8 12707{
d2bd6bc4
RS
12708 Pixmap icon_pixmap;
12709
06a2c219 12710#ifndef USE_X_TOOLKIT
c118dd06 12711 Window window = FRAME_X_WINDOW (f);
75231bad 12712#endif
dc6f92b8 12713
7f2ae036 12714 if (pixmap_id > 0)
dbc4e1c1 12715 {
d2bd6bc4 12716 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
7556890b 12717 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
dbc4e1c1
JB
12718 }
12719 else
68568555
RS
12720 {
12721 /* It seems there is no way to turn off use of an icon pixmap.
12722 The following line does it, only if no icon has yet been created,
12723 for some window managers. But with mwm it crashes.
12724 Some people say it should clear the IconPixmapHint bit in this case,
12725 but that doesn't work, and the X consortium said it isn't the
12726 right thing at all. Since there is no way to win,
12727 best to explicitly give up. */
12728#if 0
12729 f->output_data.x->wm_hints.icon_pixmap = None;
12730#else
12731 return;
12732#endif
12733 }
b1c884c3 12734
d2bd6bc4
RS
12735#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
12736
12737 {
12738 Arg al[1];
12739 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12740 XtSetValues (f->output_data.x->widget, al, 1);
12741 }
12742
12743#else /* not USE_X_TOOLKIT */
12744
7556890b
RS
12745 f->output_data.x->wm_hints.flags |= IconPixmapHint;
12746 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
d2bd6bc4
RS
12747
12748#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12749}
12750
dfcf069d 12751void
f676886a
JB
12752x_wm_set_icon_position (f, icon_x, icon_y)
12753 struct frame *f;
dc6f92b8
JB
12754 int icon_x, icon_y;
12755{
75231bad 12756#ifdef USE_X_TOOLKIT
7556890b 12757 Window window = XtWindow (f->output_data.x->widget);
75231bad 12758#else
c118dd06 12759 Window window = FRAME_X_WINDOW (f);
75231bad 12760#endif
dc6f92b8 12761
7556890b
RS
12762 f->output_data.x->wm_hints.flags |= IconPositionHint;
12763 f->output_data.x->wm_hints.icon_x = icon_x;
12764 f->output_data.x->wm_hints.icon_y = icon_y;
b1c884c3 12765
7556890b 12766 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
dc6f92b8
JB
12767}
12768
12769\f
06a2c219
GM
12770/***********************************************************************
12771 Fonts
12772 ***********************************************************************/
dc43ef94
KH
12773
12774/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
06a2c219 12775
dc43ef94
KH
12776struct font_info *
12777x_get_font_info (f, font_idx)
12778 FRAME_PTR f;
12779 int font_idx;
12780{
12781 return (FRAME_X_FONT_TABLE (f) + font_idx);
12782}
12783
12784
12785/* Return a list of names of available fonts matching PATTERN on frame
12786 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
12787 to be listed. Frame F NULL means we have not yet created any
12788 frame on X, and consult the first display in x_display_list.
12789 MAXNAMES sets a limit on how many fonts to match. */
12790
12791Lisp_Object
12792x_list_fonts (f, pattern, size, maxnames)
12793 FRAME_PTR f;
12794 Lisp_Object pattern;
12795 int size;
12796 int maxnames;
12797{
06a2c219
GM
12798 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
12799 Lisp_Object tem, second_best;
dc43ef94 12800 Display *dpy = f != NULL ? FRAME_X_DISPLAY (f) : x_display_list->display;
09c6077f 12801 int try_XLoadQueryFont = 0;
53ca4657 12802 int count;
dc43ef94 12803
6b0efe73 12804 patterns = Fassoc (pattern, Valternate_fontname_alist);
2da424f1
KH
12805 if (NILP (patterns))
12806 patterns = Fcons (pattern, Qnil);
81ba44e5 12807
09c6077f
KH
12808 if (maxnames == 1 && !size)
12809 /* We can return any single font matching PATTERN. */
12810 try_XLoadQueryFont = 1;
9a32686f 12811
8e713be6 12812 for (; CONSP (patterns); patterns = XCDR (patterns))
dc43ef94 12813 {
dc43ef94 12814 int num_fonts;
3e71d8f2 12815 char **names = NULL;
dc43ef94 12816
8e713be6 12817 pattern = XCAR (patterns);
536f4067
RS
12818 /* See if we cached the result for this particular query.
12819 The cache is an alist of the form:
12820 (((PATTERN . MAXNAMES) (FONTNAME . WIDTH) ...) ...)
12821 */
8e713be6 12822 if (f && (tem = XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element),
b5210ea7
KH
12823 key = Fcons (pattern, make_number (maxnames)),
12824 !NILP (list = Fassoc (key, tem))))
12825 {
12826 list = Fcdr_safe (list);
12827 /* We have a cashed list. Don't have to get the list again. */
12828 goto label_cached;
12829 }
12830
12831 /* At first, put PATTERN in the cache. */
09c6077f 12832
dc43ef94 12833 BLOCK_INPUT;
17d85edc
KH
12834 count = x_catch_errors (dpy);
12835
09c6077f
KH
12836 if (try_XLoadQueryFont)
12837 {
12838 XFontStruct *font;
12839 unsigned long value;
12840
12841 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
17d85edc
KH
12842 if (x_had_errors_p (dpy))
12843 {
12844 /* This error is perhaps due to insufficient memory on X
12845 server. Let's just ignore it. */
12846 font = NULL;
12847 x_clear_errors (dpy);
12848 }
12849
09c6077f
KH
12850 if (font
12851 && XGetFontProperty (font, XA_FONT, &value))
12852 {
12853 char *name = (char *) XGetAtomName (dpy, (Atom) value);
12854 int len = strlen (name);
01c752b5 12855 char *tmp;
09c6077f 12856
6f6512e8
KH
12857 /* If DXPC (a Differential X Protocol Compressor)
12858 Ver.3.7 is running, XGetAtomName will return null
12859 string. We must avoid such a name. */
12860 if (len == 0)
12861 try_XLoadQueryFont = 0;
12862 else
12863 {
12864 num_fonts = 1;
12865 names = (char **) alloca (sizeof (char *));
12866 /* Some systems only allow alloca assigned to a
12867 simple var. */
12868 tmp = (char *) alloca (len + 1); names[0] = tmp;
12869 bcopy (name, names[0], len + 1);
12870 XFree (name);
12871 }
09c6077f
KH
12872 }
12873 else
12874 try_XLoadQueryFont = 0;
a083fd23
RS
12875
12876 if (font)
12877 XFreeFont (dpy, font);
09c6077f
KH
12878 }
12879
12880 if (!try_XLoadQueryFont)
17d85edc
KH
12881 {
12882 /* We try at least 10 fonts because XListFonts will return
12883 auto-scaled fonts at the head. */
12884 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
12885 &num_fonts);
12886 if (x_had_errors_p (dpy))
12887 {
12888 /* This error is perhaps due to insufficient memory on X
12889 server. Let's just ignore it. */
12890 names = NULL;
12891 x_clear_errors (dpy);
12892 }
12893 }
12894
12895 x_uncatch_errors (dpy, count);
dc43ef94
KH
12896 UNBLOCK_INPUT;
12897
12898 if (names)
12899 {
12900 int i;
dc43ef94
KH
12901
12902 /* Make a list of all the fonts we got back.
12903 Store that in the font cache for the display. */
12904 for (i = 0; i < num_fonts; i++)
12905 {
06a2c219 12906 int width = 0;
dc43ef94 12907 char *p = names[i];
06a2c219
GM
12908 int average_width = -1, dashes = 0;
12909
dc43ef94 12910 /* Count the number of dashes in NAMES[I]. If there are
b5210ea7
KH
12911 14 dashes, and the field value following 12th dash
12912 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
12913 is usually too ugly to be used for editing. Let's
12914 ignore it. */
dc43ef94
KH
12915 while (*p)
12916 if (*p++ == '-')
12917 {
12918 dashes++;
12919 if (dashes == 7) /* PIXEL_SIZE field */
12920 width = atoi (p);
12921 else if (dashes == 12) /* AVERAGE_WIDTH field */
12922 average_width = atoi (p);
12923 }
12924 if (dashes < 14 || average_width != 0)
12925 {
12926 tem = build_string (names[i]);
12927 if (NILP (Fassoc (tem, list)))
12928 {
12929 if (STRINGP (Vx_pixel_size_width_font_regexp)
f39db7ea
RS
12930 && ((fast_c_string_match_ignore_case
12931 (Vx_pixel_size_width_font_regexp, names[i]))
dc43ef94
KH
12932 >= 0))
12933 /* We can set the value of PIXEL_SIZE to the
b5210ea7 12934 width of this font. */
dc43ef94
KH
12935 list = Fcons (Fcons (tem, make_number (width)), list);
12936 else
12937 /* For the moment, width is not known. */
12938 list = Fcons (Fcons (tem, Qnil), list);
12939 }
12940 }
12941 }
09c6077f
KH
12942 if (!try_XLoadQueryFont)
12943 XFreeFontNames (names);
dc43ef94
KH
12944 }
12945
b5210ea7 12946 /* Now store the result in the cache. */
dc43ef94 12947 if (f != NULL)
8e713be6 12948 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element)
dc43ef94 12949 = Fcons (Fcons (key, list),
8e713be6 12950 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
dc43ef94 12951
b5210ea7
KH
12952 label_cached:
12953 if (NILP (list)) continue; /* Try the remaining alternatives. */
dc43ef94 12954
b5210ea7
KH
12955 newlist = second_best = Qnil;
12956 /* Make a list of the fonts that have the right width. */
8e713be6 12957 for (; CONSP (list); list = XCDR (list))
b5210ea7 12958 {
536f4067
RS
12959 int found_size;
12960
8e713be6 12961 tem = XCAR (list);
dc43ef94 12962
8e713be6 12963 if (!CONSP (tem) || NILP (XCAR (tem)))
b5210ea7
KH
12964 continue;
12965 if (!size)
12966 {
8e713be6 12967 newlist = Fcons (XCAR (tem), newlist);
b5210ea7
KH
12968 continue;
12969 }
dc43ef94 12970
8e713be6 12971 if (!INTEGERP (XCDR (tem)))
dc43ef94 12972 {
b5210ea7
KH
12973 /* Since we have not yet known the size of this font, we
12974 must try slow function call XLoadQueryFont. */
dc43ef94
KH
12975 XFontStruct *thisinfo;
12976
12977 BLOCK_INPUT;
17d85edc 12978 count = x_catch_errors (dpy);
dc43ef94 12979 thisinfo = XLoadQueryFont (dpy,
8e713be6 12980 XSTRING (XCAR (tem))->data);
17d85edc
KH
12981 if (x_had_errors_p (dpy))
12982 {
12983 /* This error is perhaps due to insufficient memory on X
12984 server. Let's just ignore it. */
12985 thisinfo = NULL;
12986 x_clear_errors (dpy);
12987 }
12988 x_uncatch_errors (dpy, count);
dc43ef94
KH
12989 UNBLOCK_INPUT;
12990
12991 if (thisinfo)
12992 {
8e713be6 12993 XCDR (tem)
536f4067
RS
12994 = (thisinfo->min_bounds.width == 0
12995 ? make_number (0)
12996 : make_number (thisinfo->max_bounds.width));
dc43ef94
KH
12997 XFreeFont (dpy, thisinfo);
12998 }
12999 else
b5210ea7 13000 /* For unknown reason, the previous call of XListFont had
06a2c219 13001 returned a font which can't be opened. Record the size
b5210ea7 13002 as 0 not to try to open it again. */
8e713be6 13003 XCDR (tem) = make_number (0);
dc43ef94 13004 }
536f4067 13005
8e713be6 13006 found_size = XINT (XCDR (tem));
536f4067 13007 if (found_size == size)
8e713be6 13008 newlist = Fcons (XCAR (tem), newlist);
536f4067 13009 else if (found_size > 0)
b5210ea7 13010 {
536f4067 13011 if (NILP (second_best))
b5210ea7 13012 second_best = tem;
536f4067
RS
13013 else if (found_size < size)
13014 {
8e713be6
KR
13015 if (XINT (XCDR (second_best)) > size
13016 || XINT (XCDR (second_best)) < found_size)
536f4067
RS
13017 second_best = tem;
13018 }
13019 else
13020 {
8e713be6
KR
13021 if (XINT (XCDR (second_best)) > size
13022 && XINT (XCDR (second_best)) > found_size)
536f4067
RS
13023 second_best = tem;
13024 }
b5210ea7
KH
13025 }
13026 }
13027 if (!NILP (newlist))
13028 break;
13029 else if (!NILP (second_best))
13030 {
8e713be6 13031 newlist = Fcons (XCAR (second_best), Qnil);
b5210ea7 13032 break;
dc43ef94 13033 }
dc43ef94
KH
13034 }
13035
13036 return newlist;
13037}
13038
06a2c219
GM
13039
13040#if GLYPH_DEBUG
13041
13042/* Check that FONT is valid on frame F. It is if it can be found in F's
13043 font table. */
13044
13045static void
13046x_check_font (f, font)
13047 struct frame *f;
13048 XFontStruct *font;
13049{
13050 int i;
13051 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13052
13053 xassert (font != NULL);
13054
13055 for (i = 0; i < dpyinfo->n_fonts; i++)
13056 if (dpyinfo->font_table[i].name
13057 && font == dpyinfo->font_table[i].font)
13058 break;
13059
13060 xassert (i < dpyinfo->n_fonts);
13061}
13062
13063#endif /* GLYPH_DEBUG != 0 */
13064
13065/* Set *W to the minimum width, *H to the minimum font height of FONT.
13066 Note: There are (broken) X fonts out there with invalid XFontStruct
13067 min_bounds contents. For example, handa@etl.go.jp reports that
13068 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
13069 have font->min_bounds.width == 0. */
13070
13071static INLINE void
13072x_font_min_bounds (font, w, h)
13073 XFontStruct *font;
13074 int *w, *h;
13075{
13076 *h = FONT_HEIGHT (font);
13077 *w = font->min_bounds.width;
13078
13079 /* Try to handle the case where FONT->min_bounds has invalid
13080 contents. Since the only font known to have invalid min_bounds
13081 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
13082 if (*w <= 0)
13083 *w = font->max_bounds.width;
13084}
13085
13086
13087/* Compute the smallest character width and smallest font height over
13088 all fonts available on frame F. Set the members smallest_char_width
13089 and smallest_font_height in F's x_display_info structure to
13090 the values computed. Value is non-zero if smallest_font_height or
13091 smallest_char_width become smaller than they were before. */
13092
13093static int
13094x_compute_min_glyph_bounds (f)
13095 struct frame *f;
13096{
13097 int i;
13098 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13099 XFontStruct *font;
13100 int old_width = dpyinfo->smallest_char_width;
13101 int old_height = dpyinfo->smallest_font_height;
13102
13103 dpyinfo->smallest_font_height = 100000;
13104 dpyinfo->smallest_char_width = 100000;
13105
13106 for (i = 0; i < dpyinfo->n_fonts; ++i)
13107 if (dpyinfo->font_table[i].name)
13108 {
13109 struct font_info *fontp = dpyinfo->font_table + i;
13110 int w, h;
13111
13112 font = (XFontStruct *) fontp->font;
13113 xassert (font != (XFontStruct *) ~0);
13114 x_font_min_bounds (font, &w, &h);
13115
13116 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
13117 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
13118 }
13119
13120 xassert (dpyinfo->smallest_char_width > 0
13121 && dpyinfo->smallest_font_height > 0);
13122
13123 return (dpyinfo->n_fonts == 1
13124 || dpyinfo->smallest_char_width < old_width
13125 || dpyinfo->smallest_font_height < old_height);
13126}
13127
13128
dc43ef94
KH
13129/* Load font named FONTNAME of the size SIZE for frame F, and return a
13130 pointer to the structure font_info while allocating it dynamically.
13131 If SIZE is 0, load any size of font.
13132 If loading is failed, return NULL. */
13133
13134struct font_info *
13135x_load_font (f, fontname, size)
13136 struct frame *f;
13137 register char *fontname;
13138 int size;
13139{
13140 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13141 Lisp_Object font_names;
d645aaa4 13142 int count;
dc43ef94
KH
13143
13144 /* Get a list of all the fonts that match this name. Once we
13145 have a list of matching fonts, we compare them against the fonts
13146 we already have by comparing names. */
09c6077f 13147 font_names = x_list_fonts (f, build_string (fontname), size, 1);
dc43ef94
KH
13148
13149 if (!NILP (font_names))
13150 {
13151 Lisp_Object tail;
13152 int i;
13153
13154 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 13155 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
06a2c219
GM
13156 if (dpyinfo->font_table[i].name
13157 && (!strcmp (dpyinfo->font_table[i].name,
8e713be6 13158 XSTRING (XCAR (tail))->data)
06a2c219 13159 || !strcmp (dpyinfo->font_table[i].full_name,
8e713be6 13160 XSTRING (XCAR (tail))->data)))
dc43ef94
KH
13161 return (dpyinfo->font_table + i);
13162 }
13163
13164 /* Load the font and add it to the table. */
13165 {
13166 char *full_name;
13167 XFontStruct *font;
13168 struct font_info *fontp;
13169 unsigned long value;
06a2c219 13170 int i;
dc43ef94 13171
2da424f1
KH
13172 /* If we have found fonts by x_list_font, load one of them. If
13173 not, we still try to load a font by the name given as FONTNAME
13174 because XListFonts (called in x_list_font) of some X server has
13175 a bug of not finding a font even if the font surely exists and
13176 is loadable by XLoadQueryFont. */
e1d6d5b9 13177 if (size > 0 && !NILP (font_names))
8e713be6 13178 fontname = (char *) XSTRING (XCAR (font_names))->data;
dc43ef94
KH
13179
13180 BLOCK_INPUT;
d645aaa4 13181 count = x_catch_errors (FRAME_X_DISPLAY (f));
dc43ef94 13182 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
d645aaa4
KH
13183 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
13184 {
13185 /* This error is perhaps due to insufficient memory on X
13186 server. Let's just ignore it. */
13187 font = NULL;
13188 x_clear_errors (FRAME_X_DISPLAY (f));
13189 }
13190 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
dc43ef94 13191 UNBLOCK_INPUT;
b5210ea7 13192 if (!font)
dc43ef94
KH
13193 return NULL;
13194
06a2c219
GM
13195 /* Find a free slot in the font table. */
13196 for (i = 0; i < dpyinfo->n_fonts; ++i)
13197 if (dpyinfo->font_table[i].name == NULL)
13198 break;
13199
13200 /* If no free slot found, maybe enlarge the font table. */
13201 if (i == dpyinfo->n_fonts
13202 && dpyinfo->n_fonts == dpyinfo->font_table_size)
dc43ef94 13203 {
06a2c219
GM
13204 int sz;
13205 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
13206 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
dc43ef94 13207 dpyinfo->font_table
06a2c219 13208 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
dc43ef94
KH
13209 }
13210
06a2c219
GM
13211 fontp = dpyinfo->font_table + i;
13212 if (i == dpyinfo->n_fonts)
13213 ++dpyinfo->n_fonts;
dc43ef94
KH
13214
13215 /* Now fill in the slots of *FONTP. */
13216 BLOCK_INPUT;
13217 fontp->font = font;
06a2c219 13218 fontp->font_idx = i;
dc43ef94
KH
13219 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
13220 bcopy (fontname, fontp->name, strlen (fontname) + 1);
13221
13222 /* Try to get the full name of FONT. Put it in FULL_NAME. */
13223 full_name = 0;
13224 if (XGetFontProperty (font, XA_FONT, &value))
13225 {
13226 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
13227 char *p = name;
13228 int dashes = 0;
13229
13230 /* Count the number of dashes in the "full name".
13231 If it is too few, this isn't really the font's full name,
13232 so don't use it.
13233 In X11R4, the fonts did not come with their canonical names
13234 stored in them. */
13235 while (*p)
13236 {
13237 if (*p == '-')
13238 dashes++;
13239 p++;
13240 }
13241
13242 if (dashes >= 13)
13243 {
13244 full_name = (char *) xmalloc (p - name + 1);
13245 bcopy (name, full_name, p - name + 1);
13246 }
13247
13248 XFree (name);
13249 }
13250
13251 if (full_name != 0)
13252 fontp->full_name = full_name;
13253 else
13254 fontp->full_name = fontp->name;
13255
13256 fontp->size = font->max_bounds.width;
d5749adb 13257 fontp->height = FONT_HEIGHT (font);
dc43ef94 13258
2da424f1
KH
13259 if (NILP (font_names))
13260 {
13261 /* We come here because of a bug of XListFonts mentioned at
13262 the head of this block. Let's store this information in
13263 the cache for x_list_fonts. */
13264 Lisp_Object lispy_name = build_string (fontname);
13265 Lisp_Object lispy_full_name = build_string (fontp->full_name);
13266
8e713be6 13267 XCDR (dpyinfo->name_list_element)
2da424f1
KH
13268 = Fcons (Fcons (Fcons (lispy_name, make_number (256)),
13269 Fcons (Fcons (lispy_full_name,
13270 make_number (fontp->size)),
13271 Qnil)),
8e713be6 13272 XCDR (dpyinfo->name_list_element));
2da424f1 13273 if (full_name)
8e713be6 13274 XCDR (dpyinfo->name_list_element)
2da424f1
KH
13275 = Fcons (Fcons (Fcons (lispy_full_name, make_number (256)),
13276 Fcons (Fcons (lispy_full_name,
13277 make_number (fontp->size)),
13278 Qnil)),
8e713be6 13279 XCDR (dpyinfo->name_list_element));
2da424f1
KH
13280 }
13281
dc43ef94
KH
13282 /* The slot `encoding' specifies how to map a character
13283 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
ee569018
KH
13284 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
13285 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
8ff102bd 13286 2:0xA020..0xFF7F). For the moment, we don't know which charset
06a2c219 13287 uses this font. So, we set information in fontp->encoding[1]
8ff102bd
RS
13288 which is never used by any charset. If mapping can't be
13289 decided, set FONT_ENCODING_NOT_DECIDED. */
dc43ef94
KH
13290 fontp->encoding[1]
13291 = (font->max_byte1 == 0
13292 /* 1-byte font */
13293 ? (font->min_char_or_byte2 < 0x80
13294 ? (font->max_char_or_byte2 < 0x80
13295 ? 0 /* 0x20..0x7F */
8ff102bd 13296 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
dc43ef94
KH
13297 : 1) /* 0xA0..0xFF */
13298 /* 2-byte font */
13299 : (font->min_byte1 < 0x80
13300 ? (font->max_byte1 < 0x80
13301 ? (font->min_char_or_byte2 < 0x80
13302 ? (font->max_char_or_byte2 < 0x80
13303 ? 0 /* 0x2020..0x7F7F */
8ff102bd 13304 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
dc43ef94 13305 : 3) /* 0x20A0..0x7FFF */
8ff102bd 13306 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
dc43ef94
KH
13307 : (font->min_char_or_byte2 < 0x80
13308 ? (font->max_char_or_byte2 < 0x80
13309 ? 2 /* 0xA020..0xFF7F */
8ff102bd 13310 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
dc43ef94
KH
13311 : 1))); /* 0xA0A0..0xFFFF */
13312
13313 fontp->baseline_offset
13314 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
13315 ? (long) value : 0);
13316 fontp->relative_compose
13317 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
13318 ? (long) value : 0);
f78798df
KH
13319 fontp->default_ascent
13320 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
13321 ? (long) value : 0);
dc43ef94 13322
06a2c219
GM
13323 /* Set global flag fonts_changed_p to non-zero if the font loaded
13324 has a character with a smaller width than any other character
13325 before, or if the font loaded has a smalle>r height than any
13326 other font loaded before. If this happens, it will make a
13327 glyph matrix reallocation necessary. */
13328 fonts_changed_p = x_compute_min_glyph_bounds (f);
dc43ef94 13329 UNBLOCK_INPUT;
dc43ef94
KH
13330 return fontp;
13331 }
13332}
13333
06a2c219
GM
13334
13335/* Return a pointer to struct font_info of a font named FONTNAME for
13336 frame F. If no such font is loaded, return NULL. */
13337
dc43ef94
KH
13338struct font_info *
13339x_query_font (f, fontname)
13340 struct frame *f;
13341 register char *fontname;
13342{
13343 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13344 int i;
13345
13346 for (i = 0; i < dpyinfo->n_fonts; i++)
06a2c219
GM
13347 if (dpyinfo->font_table[i].name
13348 && (!strcmp (dpyinfo->font_table[i].name, fontname)
13349 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
dc43ef94
KH
13350 return (dpyinfo->font_table + i);
13351 return NULL;
13352}
13353
06a2c219
GM
13354
13355/* Find a CCL program for a font specified by FONTP, and set the member
a6582676
KH
13356 `encoder' of the structure. */
13357
13358void
13359x_find_ccl_program (fontp)
13360 struct font_info *fontp;
13361{
a42f54e6 13362 Lisp_Object list, elt;
a6582676 13363
f9b5db02 13364 elt = Qnil;
8e713be6 13365 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
a6582676 13366 {
8e713be6 13367 elt = XCAR (list);
a6582676 13368 if (CONSP (elt)
8e713be6 13369 && STRINGP (XCAR (elt))
9f2feff6
KH
13370 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
13371 >= 0)
13372 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
13373 >= 0)))
a42f54e6
KH
13374 break;
13375 }
f9b5db02 13376
a42f54e6
KH
13377 if (! NILP (list))
13378 {
d27f8ca7
KH
13379 struct ccl_program *ccl
13380 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
a42f54e6 13381
8e713be6 13382 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
a42f54e6
KH
13383 xfree (ccl);
13384 else
13385 fontp->font_encoder = ccl;
a6582676
KH
13386 }
13387}
13388
06a2c219 13389
dc43ef94 13390\f
06a2c219
GM
13391/***********************************************************************
13392 Initialization
13393 ***********************************************************************/
f451eb13 13394
3afe33e7
RS
13395#ifdef USE_X_TOOLKIT
13396static XrmOptionDescRec emacs_options[] = {
13397 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
13398 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
13399
13400 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
13401 XrmoptionSepArg, NULL},
13402 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
13403
13404 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13405 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13406 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13407 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13408 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13409 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
13410 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
13411};
13412#endif /* USE_X_TOOLKIT */
13413
7a13e894
RS
13414static int x_initialized;
13415
29b38361
KH
13416#ifdef MULTI_KBOARD
13417/* Test whether two display-name strings agree up to the dot that separates
13418 the screen number from the server number. */
13419static int
13420same_x_server (name1, name2)
13421 char *name1, *name2;
13422{
13423 int seen_colon = 0;
cf591cc1
RS
13424 unsigned char *system_name = XSTRING (Vsystem_name)->data;
13425 int system_name_length = strlen (system_name);
13426 int length_until_period = 0;
13427
13428 while (system_name[length_until_period] != 0
13429 && system_name[length_until_period] != '.')
13430 length_until_period++;
13431
13432 /* Treat `unix' like an empty host name. */
13433 if (! strncmp (name1, "unix:", 5))
13434 name1 += 4;
13435 if (! strncmp (name2, "unix:", 5))
13436 name2 += 4;
13437 /* Treat this host's name like an empty host name. */
13438 if (! strncmp (name1, system_name, system_name_length)
13439 && name1[system_name_length] == ':')
13440 name1 += system_name_length;
13441 if (! strncmp (name2, system_name, system_name_length)
13442 && name2[system_name_length] == ':')
13443 name2 += system_name_length;
13444 /* Treat this host's domainless name like an empty host name. */
13445 if (! strncmp (name1, system_name, length_until_period)
13446 && name1[length_until_period] == ':')
13447 name1 += length_until_period;
13448 if (! strncmp (name2, system_name, length_until_period)
13449 && name2[length_until_period] == ':')
13450 name2 += length_until_period;
13451
29b38361
KH
13452 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
13453 {
13454 if (*name1 == ':')
13455 seen_colon++;
13456 if (seen_colon && *name1 == '.')
13457 return 1;
13458 }
13459 return (seen_colon
13460 && (*name1 == '.' || *name1 == '\0')
13461 && (*name2 == '.' || *name2 == '\0'));
13462}
13463#endif
13464
334208b7 13465struct x_display_info *
1f8255f2 13466x_term_init (display_name, xrm_option, resource_name)
334208b7 13467 Lisp_Object display_name;
1f8255f2
RS
13468 char *xrm_option;
13469 char *resource_name;
dc6f92b8 13470{
334208b7 13471 int connection;
7a13e894 13472 Display *dpy;
334208b7
RS
13473 struct x_display_info *dpyinfo;
13474 XrmDatabase xrdb;
13475
60439948
KH
13476 BLOCK_INPUT;
13477
7a13e894
RS
13478 if (!x_initialized)
13479 {
13480 x_initialize ();
13481 x_initialized = 1;
13482 }
dc6f92b8 13483
3afe33e7 13484#ifdef USE_X_TOOLKIT
2d7fc7e8
RS
13485 /* weiner@footloose.sps.mot.com reports that this causes
13486 errors with X11R5:
13487 X protocol error: BadAtom (invalid Atom parameter)
13488 on protocol request 18skiloaf.
13489 So let's not use it until R6. */
13490#ifdef HAVE_X11XTR6
bdcd49ba
RS
13491 XtSetLanguageProc (NULL, NULL, NULL);
13492#endif
13493
7f9c7f94
RS
13494 {
13495 int argc = 0;
13496 char *argv[3];
13497
13498 argv[0] = "";
13499 argc = 1;
13500 if (xrm_option)
13501 {
13502 argv[argc++] = "-xrm";
13503 argv[argc++] = xrm_option;
13504 }
13505 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
13506 resource_name, EMACS_CLASS,
13507 emacs_options, XtNumber (emacs_options),
13508 &argc, argv);
39d8bb4d
KH
13509
13510#ifdef HAVE_X11XTR6
10537cb1 13511 /* I think this is to compensate for XtSetLanguageProc. */
71f8198a 13512 fixup_locale ();
39d8bb4d 13513#endif
7f9c7f94 13514 }
3afe33e7
RS
13515
13516#else /* not USE_X_TOOLKIT */
bdcd49ba
RS
13517#ifdef HAVE_X11R5
13518 XSetLocaleModifiers ("");
13519#endif
7a13e894 13520 dpy = XOpenDisplay (XSTRING (display_name)->data);
3afe33e7 13521#endif /* not USE_X_TOOLKIT */
334208b7 13522
7a13e894
RS
13523 /* Detect failure. */
13524 if (dpy == 0)
60439948
KH
13525 {
13526 UNBLOCK_INPUT;
13527 return 0;
13528 }
7a13e894
RS
13529
13530 /* We have definitely succeeded. Record the new connection. */
13531
13532 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
f04e1297 13533 bzero (dpyinfo, sizeof *dpyinfo);
7a13e894 13534
29b38361
KH
13535#ifdef MULTI_KBOARD
13536 {
13537 struct x_display_info *share;
13538 Lisp_Object tail;
13539
13540 for (share = x_display_list, tail = x_display_name_list; share;
8e713be6
KR
13541 share = share->next, tail = XCDR (tail))
13542 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
29b38361
KH
13543 XSTRING (display_name)->data))
13544 break;
13545 if (share)
13546 dpyinfo->kboard = share->kboard;
13547 else
13548 {
13549 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
13550 init_kboard (dpyinfo->kboard);
59e755be
KH
13551 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
13552 {
13553 char *vendor = ServerVendor (dpy);
9b6ed9f3 13554 UNBLOCK_INPUT;
59e755be
KH
13555 dpyinfo->kboard->Vsystem_key_alist
13556 = call1 (Qvendor_specific_keysyms,
13557 build_string (vendor ? vendor : ""));
9b6ed9f3 13558 BLOCK_INPUT;
59e755be
KH
13559 }
13560
29b38361
KH
13561 dpyinfo->kboard->next_kboard = all_kboards;
13562 all_kboards = dpyinfo->kboard;
0ad5446c
KH
13563 /* Don't let the initial kboard remain current longer than necessary.
13564 That would cause problems if a file loaded on startup tries to
06a2c219 13565 prompt in the mini-buffer. */
0ad5446c
KH
13566 if (current_kboard == initial_kboard)
13567 current_kboard = dpyinfo->kboard;
29b38361
KH
13568 }
13569 dpyinfo->kboard->reference_count++;
13570 }
b9737ad3
KH
13571#endif
13572
7a13e894
RS
13573 /* Put this display on the chain. */
13574 dpyinfo->next = x_display_list;
13575 x_display_list = dpyinfo;
13576
13577 /* Put it on x_display_name_list as well, to keep them parallel. */
13578 x_display_name_list = Fcons (Fcons (display_name, Qnil),
13579 x_display_name_list);
8e713be6 13580 dpyinfo->name_list_element = XCAR (x_display_name_list);
7a13e894
RS
13581
13582 dpyinfo->display = dpy;
dc6f92b8 13583
dc6f92b8 13584#if 0
7a13e894 13585 XSetAfterFunction (x_current_display, x_trace_wire);
c118dd06 13586#endif /* ! 0 */
7a13e894
RS
13587
13588 dpyinfo->x_id_name
fc932ac6
RS
13589 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
13590 + STRING_BYTES (XSTRING (Vsystem_name))
7a13e894
RS
13591 + 2);
13592 sprintf (dpyinfo->x_id_name, "%s@%s",
13593 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
28430d3c
JB
13594
13595 /* Figure out which modifier bits mean what. */
334208b7 13596 x_find_modifier_meanings (dpyinfo);
f451eb13 13597
ab648270 13598 /* Get the scroll bar cursor. */
7a13e894 13599 dpyinfo->vertical_scroll_bar_cursor
334208b7 13600 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
f451eb13 13601
334208b7
RS
13602 xrdb = x_load_resources (dpyinfo->display, xrm_option,
13603 resource_name, EMACS_CLASS);
13604#ifdef HAVE_XRMSETDATABASE
13605 XrmSetDatabase (dpyinfo->display, xrdb);
13606#else
13607 dpyinfo->display->db = xrdb;
13608#endif
547d9db8 13609 /* Put the rdb where we can find it in a way that works on
7a13e894
RS
13610 all versions. */
13611 dpyinfo->xrdb = xrdb;
334208b7
RS
13612
13613 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
13614 DefaultScreen (dpyinfo->display));
5ff67d81 13615 select_visual (dpyinfo);
43bd1b2b 13616 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
334208b7
RS
13617 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
13618 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
13619 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
13620 dpyinfo->grabbed = 0;
13621 dpyinfo->reference_count = 0;
13622 dpyinfo->icon_bitmap_id = -1;
06a2c219 13623 dpyinfo->font_table = NULL;
7a13e894
RS
13624 dpyinfo->n_fonts = 0;
13625 dpyinfo->font_table_size = 0;
13626 dpyinfo->bitmaps = 0;
13627 dpyinfo->bitmaps_size = 0;
13628 dpyinfo->bitmaps_last = 0;
13629 dpyinfo->scratch_cursor_gc = 0;
13630 dpyinfo->mouse_face_mouse_frame = 0;
13631 dpyinfo->mouse_face_deferred_gc = 0;
13632 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
13633 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
06a2c219 13634 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
7a13e894
RS
13635 dpyinfo->mouse_face_window = Qnil;
13636 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
13637 dpyinfo->mouse_face_defer = 0;
0f941935
KH
13638 dpyinfo->x_focus_frame = 0;
13639 dpyinfo->x_focus_event_frame = 0;
13640 dpyinfo->x_highlight_frame = 0;
06a2c219 13641 dpyinfo->image_cache = make_image_cache ();
334208b7 13642
43bd1b2b 13643 /* See if a private colormap is requested. */
5ff67d81
GM
13644 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
13645 {
13646 if (dpyinfo->visual->class == PseudoColor)
13647 {
13648 Lisp_Object value;
13649 value = display_x_get_resource (dpyinfo,
13650 build_string ("privateColormap"),
13651 build_string ("PrivateColormap"),
13652 Qnil, Qnil);
13653 if (STRINGP (value)
13654 && (!strcmp (XSTRING (value)->data, "true")
13655 || !strcmp (XSTRING (value)->data, "on")))
13656 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
13657 }
43bd1b2b 13658 }
5ff67d81
GM
13659 else
13660 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
13661 dpyinfo->visual, AllocNone);
43bd1b2b 13662
06a2c219
GM
13663 {
13664 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
13665 double pixels = DisplayHeight (dpyinfo->display, screen_number);
13666 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
13667 dpyinfo->resy = pixels * 25.4 / mm;
13668 pixels = DisplayWidth (dpyinfo->display, screen_number);
13669 mm = DisplayWidthMM (dpyinfo->display, screen_number);
13670 dpyinfo->resx = pixels * 25.4 / mm;
13671 }
13672
334208b7
RS
13673 dpyinfo->Xatom_wm_protocols
13674 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
13675 dpyinfo->Xatom_wm_take_focus
13676 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
13677 dpyinfo->Xatom_wm_save_yourself
13678 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
13679 dpyinfo->Xatom_wm_delete_window
13680 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
13681 dpyinfo->Xatom_wm_change_state
13682 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
13683 dpyinfo->Xatom_wm_configure_denied
13684 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
13685 dpyinfo->Xatom_wm_window_moved
13686 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
13687 dpyinfo->Xatom_editres
13688 = XInternAtom (dpyinfo->display, "Editres", False);
13689 dpyinfo->Xatom_CLIPBOARD
13690 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
13691 dpyinfo->Xatom_TIMESTAMP
13692 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
13693 dpyinfo->Xatom_TEXT
13694 = XInternAtom (dpyinfo->display, "TEXT", False);
dc43ef94
KH
13695 dpyinfo->Xatom_COMPOUND_TEXT
13696 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
334208b7
RS
13697 dpyinfo->Xatom_DELETE
13698 = XInternAtom (dpyinfo->display, "DELETE", False);
13699 dpyinfo->Xatom_MULTIPLE
13700 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
13701 dpyinfo->Xatom_INCR
13702 = XInternAtom (dpyinfo->display, "INCR", False);
13703 dpyinfo->Xatom_EMACS_TMP
13704 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
13705 dpyinfo->Xatom_TARGETS
13706 = XInternAtom (dpyinfo->display, "TARGETS", False);
13707 dpyinfo->Xatom_NULL
13708 = XInternAtom (dpyinfo->display, "NULL", False);
13709 dpyinfo->Xatom_ATOM_PAIR
13710 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
dc43ef94
KH
13711 /* For properties of font. */
13712 dpyinfo->Xatom_PIXEL_SIZE
13713 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
13714 dpyinfo->Xatom_MULE_BASELINE_OFFSET
13715 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
13716 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
13717 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
f78798df
KH
13718 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
13719 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
334208b7 13720
06a2c219
GM
13721 /* Ghostscript support. */
13722 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
13723 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
13724
13725 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
13726 False);
13727
547d9db8
KH
13728 dpyinfo->cut_buffers_initialized = 0;
13729
334208b7
RS
13730 connection = ConnectionNumber (dpyinfo->display);
13731 dpyinfo->connection = connection;
13732
dc43ef94 13733 {
5d7cc324
RS
13734 char null_bits[1];
13735
13736 null_bits[0] = 0x00;
dc43ef94
KH
13737
13738 dpyinfo->null_pixel
13739 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13740 null_bits, 1, 1, (long) 0, (long) 0,
13741 1);
13742 }
13743
06a2c219
GM
13744 {
13745 extern int gray_bitmap_width, gray_bitmap_height;
10659c77 13746 extern char *gray_bitmap_bits;
06a2c219
GM
13747 dpyinfo->gray
13748 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13749 gray_bitmap_bits,
13750 gray_bitmap_width, gray_bitmap_height,
13751 (unsigned long) 1, (unsigned long) 0, 1);
13752 }
13753
f5d11644
GM
13754#ifdef HAVE_X_I18N
13755 xim_initialize (dpyinfo, resource_name);
13756#endif
13757
87485d6f
MW
13758#ifdef subprocesses
13759 /* This is only needed for distinguishing keyboard and process input. */
334208b7 13760 if (connection != 0)
7a13e894 13761 add_keyboard_wait_descriptor (connection);
87485d6f 13762#endif
6d4238f3 13763
041b69ac 13764#ifndef F_SETOWN_BUG
dc6f92b8 13765#ifdef F_SETOWN
dc6f92b8 13766#ifdef F_SETOWN_SOCK_NEG
61c3ce62 13767 /* stdin is a socket here */
334208b7 13768 fcntl (connection, F_SETOWN, -getpid ());
c118dd06 13769#else /* ! defined (F_SETOWN_SOCK_NEG) */
334208b7 13770 fcntl (connection, F_SETOWN, getpid ());
c118dd06
JB
13771#endif /* ! defined (F_SETOWN_SOCK_NEG) */
13772#endif /* ! defined (F_SETOWN) */
041b69ac 13773#endif /* F_SETOWN_BUG */
dc6f92b8
JB
13774
13775#ifdef SIGIO
eee20f6a
KH
13776 if (interrupt_input)
13777 init_sigio (connection);
c118dd06 13778#endif /* ! defined (SIGIO) */
dc6f92b8 13779
51b592fb 13780#ifdef USE_LUCID
f8c39f51 13781#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
51b592fb
RS
13782 /* Make sure that we have a valid font for dialog boxes
13783 so that Xt does not crash. */
13784 {
13785 Display *dpy = dpyinfo->display;
13786 XrmValue d, fr, to;
13787 Font font;
e99db5a1 13788 int count;
51b592fb
RS
13789
13790 d.addr = (XPointer)&dpy;
13791 d.size = sizeof (Display *);
13792 fr.addr = XtDefaultFont;
13793 fr.size = sizeof (XtDefaultFont);
13794 to.size = sizeof (Font *);
13795 to.addr = (XPointer)&font;
e99db5a1 13796 count = x_catch_errors (dpy);
51b592fb
RS
13797 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
13798 abort ();
13799 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
13800 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
e99db5a1 13801 x_uncatch_errors (dpy, count);
51b592fb
RS
13802 }
13803#endif
f8c39f51 13804#endif
51b592fb 13805
34e23e5a
GM
13806 /* See if we should run in synchronous mode. This is useful
13807 for debugging X code. */
13808 {
13809 Lisp_Object value;
13810 value = display_x_get_resource (dpyinfo,
13811 build_string ("synchronous"),
13812 build_string ("Synchronous"),
13813 Qnil, Qnil);
13814 if (STRINGP (value)
13815 && (!strcmp (XSTRING (value)->data, "true")
13816 || !strcmp (XSTRING (value)->data, "on")))
13817 XSynchronize (dpyinfo->display, True);
13818 }
13819
60439948
KH
13820 UNBLOCK_INPUT;
13821
7a13e894
RS
13822 return dpyinfo;
13823}
13824\f
13825/* Get rid of display DPYINFO, assuming all frames are already gone,
13826 and without sending any more commands to the X server. */
dc6f92b8 13827
7a13e894
RS
13828void
13829x_delete_display (dpyinfo)
13830 struct x_display_info *dpyinfo;
13831{
13832 delete_keyboard_wait_descriptor (dpyinfo->connection);
13833
13834 /* Discard this display from x_display_name_list and x_display_list.
13835 We can't use Fdelq because that can quit. */
13836 if (! NILP (x_display_name_list)
8e713be6
KR
13837 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
13838 x_display_name_list = XCDR (x_display_name_list);
7a13e894
RS
13839 else
13840 {
13841 Lisp_Object tail;
13842
13843 tail = x_display_name_list;
8e713be6 13844 while (CONSP (tail) && CONSP (XCDR (tail)))
7a13e894 13845 {
bffcfca9 13846 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
7a13e894 13847 {
8e713be6 13848 XCDR (tail) = XCDR (XCDR (tail));
7a13e894
RS
13849 break;
13850 }
8e713be6 13851 tail = XCDR (tail);
7a13e894
RS
13852 }
13853 }
13854
9bda743f
GM
13855 if (next_noop_dpyinfo == dpyinfo)
13856 next_noop_dpyinfo = dpyinfo->next;
13857
7a13e894
RS
13858 if (x_display_list == dpyinfo)
13859 x_display_list = dpyinfo->next;
7f9c7f94
RS
13860 else
13861 {
13862 struct x_display_info *tail;
7a13e894 13863
7f9c7f94
RS
13864 for (tail = x_display_list; tail; tail = tail->next)
13865 if (tail->next == dpyinfo)
13866 tail->next = tail->next->next;
13867 }
7a13e894 13868
0d777288
RS
13869#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
13870#ifndef AIX /* On AIX, XCloseDisplay calls this. */
7f9c7f94
RS
13871 XrmDestroyDatabase (dpyinfo->xrdb);
13872#endif
0d777288 13873#endif
29b38361
KH
13874#ifdef MULTI_KBOARD
13875 if (--dpyinfo->kboard->reference_count == 0)
39f79001 13876 delete_kboard (dpyinfo->kboard);
b9737ad3 13877#endif
f5d11644
GM
13878#ifdef HAVE_X_I18N
13879 if (dpyinfo->xim)
13880 xim_close_dpy (dpyinfo);
13881#endif
13882
b9737ad3
KH
13883 xfree (dpyinfo->font_table);
13884 xfree (dpyinfo->x_id_name);
f04e1297 13885 xfree (dpyinfo->color_cells);
b9737ad3 13886 xfree (dpyinfo);
7a13e894 13887}
f04e1297 13888
7a13e894
RS
13889\f
13890/* Set up use of X before we make the first connection. */
13891
06a2c219
GM
13892static struct redisplay_interface x_redisplay_interface =
13893{
13894 x_produce_glyphs,
13895 x_write_glyphs,
13896 x_insert_glyphs,
13897 x_clear_end_of_line,
13898 x_scroll_run,
13899 x_after_update_window_line,
13900 x_update_window_begin,
13901 x_update_window_end,
13902 XTcursor_to,
13903 x_flush,
71b8321e 13904 x_clear_mouse_face,
66ac4b0e
GM
13905 x_get_glyph_overhangs,
13906 x_fix_overlapping_area
06a2c219
GM
13907};
13908
dfcf069d 13909void
7a13e894
RS
13910x_initialize ()
13911{
06a2c219
GM
13912 rif = &x_redisplay_interface;
13913
13914 clear_frame_hook = x_clear_frame;
13915 ins_del_lines_hook = x_ins_del_lines;
13916 change_line_highlight_hook = x_change_line_highlight;
13917 delete_glyphs_hook = x_delete_glyphs;
dc6f92b8
JB
13918 ring_bell_hook = XTring_bell;
13919 reset_terminal_modes_hook = XTreset_terminal_modes;
13920 set_terminal_modes_hook = XTset_terminal_modes;
06a2c219
GM
13921 update_begin_hook = x_update_begin;
13922 update_end_hook = x_update_end;
dc6f92b8
JB
13923 set_terminal_window_hook = XTset_terminal_window;
13924 read_socket_hook = XTread_socket;
b8009dd1 13925 frame_up_to_date_hook = XTframe_up_to_date;
dc6f92b8 13926 reassert_line_highlight_hook = XTreassert_line_highlight;
90e65f07 13927 mouse_position_hook = XTmouse_position;
f451eb13 13928 frame_rehighlight_hook = XTframe_rehighlight;
dbc4e1c1 13929 frame_raise_lower_hook = XTframe_raise_lower;
ab648270
JB
13930 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13931 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13932 redeem_scroll_bar_hook = XTredeem_scroll_bar;
13933 judge_scroll_bars_hook = XTjudge_scroll_bars;
06a2c219 13934 estimate_mode_line_height_hook = x_estimate_mode_line_height;
58769bee 13935
f676886a 13936 scroll_region_ok = 1; /* we'll scroll partial frames */
dc6f92b8
JB
13937 char_ins_del_ok = 0; /* just as fast to write the line */
13938 line_ins_del_ok = 1; /* we'll just blt 'em */
13939 fast_clear_end_of_line = 1; /* X does this well */
58769bee 13940 memory_below_frame = 0; /* we don't remember what scrolls
dc6f92b8
JB
13941 off the bottom */
13942 baud_rate = 19200;
13943
7a13e894 13944 x_noop_count = 0;
9ea173e8 13945 last_tool_bar_item = -1;
06a2c219
GM
13946 any_help_event_p = 0;
13947
b30b24cb
RS
13948 /* Try to use interrupt input; if we can't, then start polling. */
13949 Fset_input_mode (Qt, Qnil, Qt, Qnil);
13950
7f9c7f94
RS
13951#ifdef USE_X_TOOLKIT
13952 XtToolkitInitialize ();
13953 Xt_app_con = XtCreateApplicationContext ();
665881ad 13954 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
bffcfca9
GM
13955
13956 /* Install an asynchronous timer that processes Xt timeout events
13957 every 0.1s. This is necessary because some widget sets use
13958 timeouts internally, for example the LessTif menu bar, or the
13959 Xaw3d scroll bar. When Xt timouts aren't processed, these
13960 widgets don't behave normally. */
13961 {
13962 EMACS_TIME interval;
13963 EMACS_SET_SECS_USECS (interval, 0, 100000);
13964 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
13965 }
db74249b 13966#endif
bffcfca9 13967
eccc05db 13968#ifdef USE_TOOLKIT_SCROLL_BARS
ec18280f
SM
13969 xaw3d_arrow_scroll = False;
13970 xaw3d_pick_top = True;
7f9c7f94
RS
13971#endif
13972
58769bee 13973 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 13974 original error handler. */
e99db5a1 13975 XSetErrorHandler (x_error_handler);
334208b7 13976 XSetIOErrorHandler (x_io_error_quitter);
dc6f92b8 13977
06a2c219 13978 /* Disable Window Change signals; they are handled by X events. */
dc6f92b8
JB
13979#ifdef SIGWINCH
13980 signal (SIGWINCH, SIG_DFL);
c118dd06 13981#endif /* ! defined (SIGWINCH) */
dc6f92b8 13982
92e2441b 13983 signal (SIGPIPE, x_connection_signal);
dc6f92b8 13984}
55123275 13985
06a2c219 13986
55123275
JB
13987void
13988syms_of_xterm ()
13989{
e99db5a1
RS
13990 staticpro (&x_error_message_string);
13991 x_error_message_string = Qnil;
13992
7a13e894
RS
13993 staticpro (&x_display_name_list);
13994 x_display_name_list = Qnil;
334208b7 13995
ab648270 13996 staticpro (&last_mouse_scroll_bar);
e53cb100 13997 last_mouse_scroll_bar = Qnil;
59e755be
KH
13998
13999 staticpro (&Qvendor_specific_keysyms);
14000 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
2237cac9
RS
14001
14002 staticpro (&last_mouse_press_frame);
14003 last_mouse_press_frame = Qnil;
06a2c219 14004
06a2c219 14005 help_echo = Qnil;
be010514
GM
14006 staticpro (&help_echo);
14007 help_echo_object = Qnil;
14008 staticpro (&help_echo_object);
7cea38bc
GM
14009 help_echo_window = Qnil;
14010 staticpro (&help_echo_window);
06a2c219 14011 previous_help_echo = Qnil;
be010514
GM
14012 staticpro (&previous_help_echo);
14013 help_echo_pos = -1;
06a2c219
GM
14014
14015 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
14016 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
14017For example, if a block cursor is over a tab, it will be drawn as\n\
14018wide as that tab on the display.");
14019 x_stretch_cursor_p = 0;
14020
5bf04520
GM
14021 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
14022 "What X toolkit scroll bars Emacs uses.\n\
14023A value of nil means Emacs doesn't use X toolkit scroll bars.\n\
14024Otherwise, value is a symbol describing the X toolkit.");
eccc05db 14025#ifdef USE_TOOLKIT_SCROLL_BARS
5bf04520
GM
14026#ifdef USE_MOTIF
14027 Vx_toolkit_scroll_bars = intern ("motif");
14028#elif defined HAVE_XAW3D
14029 Vx_toolkit_scroll_bars = intern ("xaw3d");
14030#else
14031 Vx_toolkit_scroll_bars = intern ("xaw");
14032#endif
06a2c219 14033#else
5bf04520 14034 Vx_toolkit_scroll_bars = Qnil;
06a2c219
GM
14035#endif
14036
06a2c219
GM
14037 staticpro (&last_mouse_motion_frame);
14038 last_mouse_motion_frame = Qnil;
55123275 14039}
6cf0ae86
RS
14040
14041#endif /* not HAVE_X_WINDOWS */