(resize_mini_window): Use grow_mini_window and
[bpt/emacs.git] / src / xterm.c
CommitLineData
dc6f92b8 1/* X Communication module for terminals which understand the X protocol.
06a2c219
GM
2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999
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
039440c4
RS
25/* On 4.3 these lose if they come after xterm.h. */
26/* On HP-UX 8.0 signal.h loses if it comes after config.h. */
27/* Putting these at the beginning seems to be standard for other .c files. */
039440c4
RS
28#include <signal.h>
29
18160b98 30#include <config.h>
dc6f92b8 31
4846819e 32#include <stdio.h>
06a2c219
GM
33#ifdef STDC_HEADERS
34#include <stdlib.h>
35#endif
4846819e 36
dc6f92b8
JB
37#ifdef HAVE_X_WINDOWS
38
39#include "lisp.h"
9ac0d9e0 40#include "blockinput.h"
dc6f92b8 41
ae79c227
AS
42/* Need syssignal.h for various externs and definitions that may be required
43 by some configurations for calls to signal later in this source file. */
44#include "syssignal.h"
45
dc6f92b8
JB
46/* This may include sys/types.h, and that somehow loses
47 if this is not done before the other system files. */
48#include "xterm.h"
f451eb13 49#include <X11/cursorfont.h>
dc6f92b8 50
16bd92ea 51#ifndef USG
dc6f92b8
JB
52/* Load sys/types.h if not already loaded.
53 In some systems loading it twice is suicidal. */
54#ifndef makedev
55#include <sys/types.h>
c118dd06
JB
56#endif /* makedev */
57#endif /* USG */
dc6f92b8 58
6df54671 59#ifdef BSD_SYSTEM
dc6f92b8 60#include <sys/ioctl.h>
6df54671 61#endif /* ! defined (BSD_SYSTEM) */
dc6f92b8 62
2d368234 63#include "systty.h"
3a2712f9 64#include "systime.h"
dc6f92b8 65
b8009dd1 66#ifndef INCLUDED_FCNTL
dc6f92b8 67#include <fcntl.h>
b8009dd1 68#endif
dc6f92b8
JB
69#include <ctype.h>
70#include <errno.h>
71#include <setjmp.h>
72#include <sys/stat.h>
a0a7635f
RS
73/* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
74/* #include <sys/param.h> */
dc6f92b8 75
dc43ef94
KH
76#include "charset.h"
77#include "ccl.h"
7a13e894 78#include "frame.h"
a1dfb88a 79#include "fontset.h"
dc6f92b8
JB
80#include "dispextern.h"
81#include "termhooks.h"
82#include "termopts.h"
83#include "termchar.h"
84#if 0
85#include "sink.h"
86#include "sinkmask.h"
c118dd06 87#endif /* ! 0 */
dc6f92b8 88#include "gnu.h"
dc6f92b8 89#include "disptab.h"
dc6f92b8 90#include "buffer.h"
f451eb13 91#include "window.h"
3b2fa4e6 92#include "keyboard.h"
bde7c500 93#include "intervals.h"
dfcf069d 94#include "process.h"
dc6f92b8 95
d2bd6bc4
RS
96#ifdef USE_X_TOOLKIT
97#include <X11/Shell.h>
98#endif
99
06a2c219
GM
100#include <sys/types.h>
101#ifdef HAVE_SYS_TIME_H
102#include <sys/time.h>
103#endif
104#ifdef HAVE_UNISTD_H
105#include <unistd.h>
106#endif
107
3afe33e7 108#ifdef USE_X_TOOLKIT
06a2c219 109
9d7e2e3e 110extern void free_frame_menubar ();
2224b905 111extern FRAME_PTR x_menubar_window_to_frame ();
06a2c219 112
0fdff6bb
RS
113#if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
114#define HACK_EDITRES
115extern void _XEditResCheckMessages ();
116#endif /* not NO_EDITRES */
06a2c219
GM
117
118/* Include toolkit specific headers for the scroll bar widget. */
119
120#ifdef USE_TOOLKIT_SCROLL_BARS
121#if defined USE_MOTIF
122#include <Xm/Xm.h> /* for LESSTIF_VERSION */
123#include <Xm/ScrollBar.h>
124#include <Xm/ScrollBarP.h>
125#elif defined HAVE_XAW3D
126#include <X11/Xaw3d/Simple.h>
127#include <X11/Xaw3d/ThreeD.h>
128#include <X11/Xaw3d/Scrollbar.h>
129#define ARROW_SCROLLBAR
130#include <X11/Xaw3d/ScrollbarP.h>
131#endif /* HAVE_XAW3D */
132#endif /* USE_TOOLKIT_SCROLL_BARS */
133
3afe33e7
RS
134#endif /* USE_X_TOOLKIT */
135
b849c413
RS
136#ifndef USE_X_TOOLKIT
137#define x_any_window_to_frame x_window_to_frame
5627c40e 138#define x_top_window_to_frame x_window_to_frame
b849c413
RS
139#endif
140
546e6d5b 141#ifdef USE_X_TOOLKIT
d067ea8b 142#include "widget.h"
546e6d5b
RS
143#ifndef XtNinitialState
144#define XtNinitialState "initialState"
145#endif
146#endif
147
10537cb1 148#ifdef HAVE_SETLOCALE
39d8bb4d
KH
149/* So we can do setlocale. */
150#include <locale.h>
151#endif
152
80528801
KH
153#ifdef SOLARIS2
154/* memmove will be defined as a macro in Xfuncs.h unless
155 <string.h> is included beforehand. The declaration for memmove in
156 <string.h> will cause a syntax error when Xfuncs.h later includes it. */
157#include <string.h>
158#endif
159
e4b68333 160#ifndef min
06a2c219 161#define min(a,b) ((a) < (b) ? (a) : (b))
e4b68333
RS
162#endif
163#ifndef max
06a2c219
GM
164#define max(a,b) ((a) > (b) ? (a) : (b))
165#endif
166
167#define abs(x) ((x) < 0 ? -(x) : (x))
168
169#define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
170
171\f
172/* Bitmaps for truncated lines. */
173
174enum bitmap_type
175{
176 NO_BITMAP,
177 LEFT_TRUNCATION_BITMAP,
178 RIGHT_TRUNCATION_BITMAP,
179 OVERLAY_ARROW_BITMAP,
180 CONTINUED_LINE_BITMAP,
181 CONTINUATION_LINE_BITMAP,
182 ZV_LINE_BITMAP
183};
184
185/* Bitmap drawn to indicate lines not displaying text if
186 `indicate-empty-lines' is non-nil. */
187
188#define zv_width 8
189#define zv_height 8
190static unsigned char zv_bits[] = {
191 0x00, 0x00, 0x1e, 0x1e, 0x1e, 0x1e, 0x00, 0x00};
192
193/* An arrow like this: `<-'. */
194
195#define left_width 8
196#define left_height 8
197static unsigned char left_bits[] = {
198 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
199
110859fc
GM
200/* Right truncation arrow bitmap `->'. */
201
202#define right_width 8
203#define right_height 8
204static unsigned char right_bits[] = {
205 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
206
06a2c219
GM
207/* Marker for continued lines. */
208
209#define continued_width 8
210#define continued_height 8
211static unsigned char continued_bits[] = {
110859fc
GM
212 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
213
214/* Marker for continuation lines. */
06a2c219
GM
215
216#define continuation_width 8
217#define continuation_height 8
218static unsigned char continuation_bits[] = {
110859fc 219 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
06a2c219 220
110859fc 221/* Overlay arrow bitmap. */
06a2c219 222
110859fc
GM
223#if 0
224/* A bomb. */
06a2c219
GM
225#define ov_width 8
226#define ov_height 8
227static unsigned char ov_bits[] = {
228 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
06a2c219 229#else
110859fc 230/* A triangular arrow. */
06a2c219
GM
231#define ov_width 8
232#define ov_height 8
233static unsigned char ov_bits[] = {
110859fc
GM
234 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
235
e4b68333 236#endif
06a2c219
GM
237
238extern Lisp_Object Qhelp_echo;
239
69388238 240\f
06a2c219
GM
241/* Non-zero means Emacs uses toolkit scroll bars. */
242
243int x_toolkit_scroll_bars_p;
244
245/* If a string, XTread_socket generates an event to display that string.
246 (The display is done in read_char.) */
247
248static Lisp_Object help_echo;
249
250/* Temporary variable for XTread_socket. */
251
252static Lisp_Object previous_help_echo;
253
254/* Non-zero means that a HELP_EVENT has been generated since Emacs
255 start. */
256
257static int any_help_event_p;
258
259/* Non-zero means draw block and hollow cursor as wide as the glyph
260 under it. For example, if a block cursor is over a tab, it will be
261 drawn as wide as that tab on the display. */
262
263int x_stretch_cursor_p;
264
265/* This is a chain of structures for all the X displays currently in
266 use. */
267
334208b7 268struct x_display_info *x_display_list;
dc6f92b8 269
06a2c219
GM
270/* This is a list of cons cells, each of the form (NAME
271 . FONT-LIST-CACHE), one for each element of x_display_list and in
272 the same order. NAME is the name of the frame. FONT-LIST-CACHE
273 records previous values returned by x-list-fonts. */
274
7a13e894 275Lisp_Object x_display_name_list;
f451eb13 276
987d2ad1 277/* Frame being updated by update_frame. This is declared in term.c.
06a2c219
GM
278 This is set by update_begin and looked at by all the XT functions.
279 It is zero while not inside an update. In that case, the XT
280 functions assume that `selected_frame' is the frame to apply to. */
281
d0386f2a 282extern struct frame *updating_frame;
dc6f92b8 283
dfcf069d 284extern int waiting_for_input;
0e81d8cd 285
06a2c219
GM
286/* This is a frame waiting to be auto-raised, within XTread_socket. */
287
0134a210
RS
288struct frame *pending_autoraise_frame;
289
7f9c7f94
RS
290#ifdef USE_X_TOOLKIT
291/* The application context for Xt use. */
292XtAppContext Xt_app_con;
06a2c219
GM
293static String Xt_default_resources[] = {0};
294#endif /* USE_X_TOOLKIT */
665881ad 295
06a2c219
GM
296/* Nominal cursor position -- where to draw output.
297 HPOS and VPOS are window relative glyph matrix coordinates.
298 X and Y are window relative pixel coordinates. */
dc6f92b8 299
06a2c219 300struct cursor_pos output_cursor;
dc6f92b8 301
dc6f92b8 302
69388238
RS
303/* Mouse movement.
304
06a2c219 305 Formerly, we used PointerMotionHintMask (in standard_event_mask)
f5bb65ec
RS
306 so that we would have to call XQueryPointer after each MotionNotify
307 event to ask for another such event. However, this made mouse tracking
308 slow, and there was a bug that made it eventually stop.
309
310 Simply asking for MotionNotify all the time seems to work better.
311
69388238
RS
312 In order to avoid asking for motion events and then throwing most
313 of them away or busy-polling the server for mouse positions, we ask
314 the server for pointer motion hints. This means that we get only
315 one event per group of mouse movements. "Groups" are delimited by
316 other kinds of events (focus changes and button clicks, for
317 example), or by XQueryPointer calls; when one of these happens, we
318 get another MotionNotify event the next time the mouse moves. This
319 is at least as efficient as getting motion events when mouse
320 tracking is on, and I suspect only negligibly worse when tracking
f5bb65ec 321 is off. */
69388238
RS
322
323/* Where the mouse was last time we reported a mouse event. */
69388238 324
06a2c219
GM
325FRAME_PTR last_mouse_frame;
326static XRectangle last_mouse_glyph;
2237cac9
RS
327static Lisp_Object last_mouse_press_frame;
328
69388238
RS
329/* The scroll bar in which the last X motion event occurred.
330
06a2c219
GM
331 If the last X motion event occurred in a scroll bar, we set this so
332 XTmouse_position can know whether to report a scroll bar motion or
69388238
RS
333 an ordinary motion.
334
06a2c219
GM
335 If the last X motion event didn't occur in a scroll bar, we set
336 this to Qnil, to tell XTmouse_position to return an ordinary motion
337 event. */
338
69388238
RS
339static Lisp_Object last_mouse_scroll_bar;
340
69388238
RS
341/* This is a hack. We would really prefer that XTmouse_position would
342 return the time associated with the position it returns, but there
06a2c219 343 doesn't seem to be any way to wrest the time-stamp from the server
69388238
RS
344 along with the position query. So, we just keep track of the time
345 of the last movement we received, and return that in hopes that
346 it's somewhat accurate. */
06a2c219 347
69388238
RS
348static Time last_mouse_movement_time;
349
06a2c219
GM
350/* Incremented by XTread_socket whenever it really tries to read
351 events. */
352
c0a04927
RS
353#ifdef __STDC__
354static int volatile input_signal_count;
355#else
356static int input_signal_count;
357#endif
358
7a13e894 359/* Used locally within XTread_socket. */
06a2c219 360
7a13e894 361static int x_noop_count;
dc6f92b8 362
7a13e894 363/* Initial values of argv and argc. */
06a2c219 364
7a13e894
RS
365extern char **initial_argv;
366extern int initial_argc;
dc6f92b8 367
7a13e894 368extern Lisp_Object Vcommand_line_args, Vsystem_name;
dc6f92b8 369
06a2c219 370/* Tells if a window manager is present or not. */
7a13e894
RS
371
372extern Lisp_Object Vx_no_window_manager;
dc6f92b8 373
c2df547c 374extern Lisp_Object Qface, Qmouse_face;
b8009dd1 375
dc6f92b8
JB
376extern int errno;
377
dfeccd2d 378/* A mask of extra modifier bits to put into every keyboard char. */
06a2c219 379
64bb1782
RS
380extern int extra_keyboard_modifiers;
381
59e755be
KH
382static Lisp_Object Qvendor_specific_keysyms;
383
334208b7 384extern XrmDatabase x_load_resources ();
c32cdd9a
KH
385extern Lisp_Object x_icon_type ();
386
7a13e894 387
06a2c219
GM
388/* Enumeration for overriding/changing the face to use for drawing
389 glyphs in x_draw_glyphs. */
390
391enum draw_glyphs_face
392{
393 DRAW_NORMAL_TEXT,
394 DRAW_INVERSE_VIDEO,
395 DRAW_CURSOR,
396 DRAW_MOUSE_FACE,
397 DRAW_IMAGE_RAISED,
398 DRAW_IMAGE_SUNKEN
399};
400
401static void x_update_window_end P_ ((struct window *, int));
402static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
403void x_delete_display P_ ((struct x_display_info *));
404static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
405 unsigned));
406static int fast_find_position P_ ((struct window *, int, int *, int *,
407 int *, int *));
408static void set_output_cursor P_ ((struct cursor_pos *));
409static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
410 int *, int *, int *));
411static void note_mode_line_highlight P_ ((struct window *, int, int));
412static void x_check_font P_ ((struct frame *, XFontStruct *));
413static void note_mouse_highlight P_ ((struct frame *, int, int));
9ea173e8
GM
414static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
415static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
06a2c219
GM
416static void show_mouse_face P_ ((struct x_display_info *,
417 enum draw_glyphs_face));
418static int x_io_error_quitter P_ ((Display *));
419int x_catch_errors P_ ((Display *));
420void x_uncatch_errors P_ ((Display *, int));
421void x_lower_frame P_ ((struct frame *));
422void x_scroll_bar_clear P_ ((struct frame *));
423int x_had_errors_p P_ ((Display *));
424void x_wm_set_size_hint P_ ((struct frame *, long, int));
425void x_raise_frame P_ ((struct frame *));
426void x_set_window_size P_ ((struct frame *, int, int, int));
427void x_wm_set_window_state P_ ((struct frame *, int));
428void x_wm_set_icon_pixmap P_ ((struct frame *, int));
429void x_initialize P_ ((void));
430static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
431static int x_compute_min_glyph_bounds P_ ((struct frame *));
432static void x_draw_phys_cursor_glyph P_ ((struct window *,
433 struct glyph_row *,
434 enum draw_glyphs_face));
435static void x_update_end P_ ((struct frame *));
436static void XTframe_up_to_date P_ ((struct frame *));
437static void XTreassert_line_highlight P_ ((int, int));
438static void x_change_line_highlight P_ ((int, int, int, int));
439static void XTset_terminal_modes P_ ((void));
440static void XTreset_terminal_modes P_ ((void));
441static void XTcursor_to P_ ((int, int, int, int));
442static void x_write_glyphs P_ ((struct glyph *, int));
443static void x_clear_end_of_line P_ ((int));
444static void x_clear_frame P_ ((void));
445static void x_clear_cursor P_ ((struct window *));
446static void frame_highlight P_ ((struct frame *));
447static void frame_unhighlight P_ ((struct frame *));
448static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
449static void XTframe_rehighlight P_ ((struct frame *));
450static void x_frame_rehighlight P_ ((struct x_display_info *));
451static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
452static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *));
453static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
454 XRectangle *));
455static void expose_frame P_ ((struct frame *, int, int, int, int));
456static void expose_window_tree P_ ((struct window *, XRectangle *));
457static void expose_window P_ ((struct window *, XRectangle *));
458static void expose_area P_ ((struct window *, struct glyph_row *,
459 XRectangle *, enum glyph_row_area));
460static void expose_line P_ ((struct window *, struct glyph_row *,
461 XRectangle *));
462static void x_update_cursor_in_window_tree P_ ((struct window *, int));
463static void x_update_window_cursor P_ ((struct window *, int));
464static void x_erase_phys_cursor P_ ((struct window *));
465void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
466static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
467 enum bitmap_type));
468
469static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
470 GC, int));
471static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
472static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
473static void note_overwritten_text_cursor P_ ((struct window *, int, int));
474static void x_flush P_ ((struct frame *f));
475
476
477/* Flush display of frame F, or of all frames if F is null. */
478
479static void
480x_flush (f)
481 struct frame *f;
482{
483 BLOCK_INPUT;
484 if (f == NULL)
485 {
486 Lisp_Object rest, frame;
487 FOR_EACH_FRAME (rest, frame)
488 x_flush (XFRAME (frame));
489 }
490 else if (FRAME_X_P (f))
491 XFlush (FRAME_X_DISPLAY (f));
492 UNBLOCK_INPUT;
493}
494
dc6f92b8 495
06a2c219
GM
496/* Remove calls to XFlush by defining XFlush to an empty replacement.
497 Calls to XFlush should be unnecessary because the X output buffer
498 is flushed automatically as needed by calls to XPending,
499 XNextEvent, or XWindowEvent according to the XFlush man page.
500 XTread_socket calls XPending. Removing XFlush improves
501 performance. */
502
503#define XFlush(DISPLAY) (void) 0
b8009dd1 504
334208b7 505\f
06a2c219
GM
506/***********************************************************************
507 Debugging
508 ***********************************************************************/
509
9382638d 510#if 0
06a2c219
GM
511
512/* This is a function useful for recording debugging information about
513 the sequence of occurrences in this file. */
9382638d
KH
514
515struct record
516{
517 char *locus;
518 int type;
519};
520
521struct record event_record[100];
522
523int event_record_index;
524
525record_event (locus, type)
526 char *locus;
527 int type;
528{
529 if (event_record_index == sizeof (event_record) / sizeof (struct record))
530 event_record_index = 0;
531
532 event_record[event_record_index].locus = locus;
533 event_record[event_record_index].type = type;
534 event_record_index++;
535}
536
537#endif /* 0 */
06a2c219
GM
538
539
9382638d 540\f
334208b7
RS
541/* Return the struct x_display_info corresponding to DPY. */
542
543struct x_display_info *
544x_display_info_for_display (dpy)
545 Display *dpy;
546{
547 struct x_display_info *dpyinfo;
548
549 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
550 if (dpyinfo->display == dpy)
551 return dpyinfo;
16bd92ea 552
334208b7
RS
553 return 0;
554}
f451eb13 555
06a2c219
GM
556
557\f
558/***********************************************************************
559 Starting and ending an update
560 ***********************************************************************/
561
562/* Start an update of frame F. This function is installed as a hook
563 for update_begin, i.e. it is called when update_begin is called.
564 This function is called prior to calls to x_update_window_begin for
565 each window being updated. Currently, there is nothing to do here
566 because all interesting stuff is done on a window basis. */
dc6f92b8 567
dfcf069d 568static void
06a2c219 569x_update_begin (f)
f676886a 570 struct frame *f;
58769bee 571{
06a2c219
GM
572 /* Nothing to do. */
573}
dc6f92b8 574
dc6f92b8 575
06a2c219
GM
576/* Start update of window W. Set the global variable updated_window
577 to the window being updated and set output_cursor to the cursor
578 position of W. */
dc6f92b8 579
06a2c219
GM
580static void
581x_update_window_begin (w)
582 struct window *w;
583{
584 struct frame *f = XFRAME (WINDOW_FRAME (w));
585 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
586
587 updated_window = w;
588 set_output_cursor (&w->cursor);
b8009dd1 589
06a2c219 590 BLOCK_INPUT;
d1bc4182 591
06a2c219 592 if (f == display_info->mouse_face_mouse_frame)
b8009dd1 593 {
514e4681 594 /* Don't do highlighting for mouse motion during the update. */
06a2c219 595 display_info->mouse_face_defer = 1;
37c2c98b 596
06a2c219
GM
597 /* If F needs to be redrawn, simply forget about any prior mouse
598 highlighting. */
9f67f20b 599 if (FRAME_GARBAGED_P (f))
06a2c219
GM
600 display_info->mouse_face_window = Qnil;
601
602 /* Can we tell that this update does not affect the window
603 where the mouse highlight is? If so, no need to turn off.
604 Likewise, don't do anything if the frame is garbaged;
605 in that case, the frame's current matrix that we would use
606 is all wrong, and we will redisplay that line anyway. */
607 if (!NILP (display_info->mouse_face_window)
608 && w == XWINDOW (display_info->mouse_face_window))
514e4681 609 {
06a2c219 610 int i;
514e4681 611
06a2c219
GM
612 for (i = 0; i < w->desired_matrix->nrows; ++i)
613 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
514e4681
RS
614 break;
615
06a2c219
GM
616 if (i < w->desired_matrix->nrows)
617 clear_mouse_face (display_info);
514e4681 618 }
b8009dd1 619 }
6ccf47d1 620
dc6f92b8
JB
621 UNBLOCK_INPUT;
622}
623
06a2c219
GM
624
625/* Draw a vertical window border to the right of window W if W doesn't
626 have vertical scroll bars. */
627
dfcf069d 628static void
06a2c219
GM
629x_draw_vertical_border (w)
630 struct window *w;
58769bee 631{
06a2c219
GM
632 struct frame *f = XFRAME (WINDOW_FRAME (w));
633
634 /* Redraw borders between horizontally adjacent windows. Don't
635 do it for frames with vertical scroll bars because either the
636 right scroll bar of a window, or the left scroll bar of its
637 neighbor will suffice as a border. */
638 if (!WINDOW_RIGHTMOST_P (w)
639 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
640 {
641 int x0, x1, y0, y1;
dc6f92b8 642
06a2c219 643 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
110859fc 644 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
645 y1 -= 1;
646
647 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
648 f->output_data.x->normal_gc, x1, y0, x1, y1);
649 }
650}
651
652
653/* End update of window W (which is equal to updated_window). Draw
654 vertical borders between horizontally adjacent windows, and display
655 W's cursor if CURSOR_ON_P is non-zero. W may be a menu bar
656 pseudo-window in case we don't have X toolkit support. Such
657 windows don't have a cursor, so don't display it here. */
658
659static void
660x_update_window_end (w, cursor_on_p)
661 struct window *w;
662 int cursor_on_p;
663{
664 if (!w->pseudo_window_p)
665 {
666 BLOCK_INPUT;
667 if (cursor_on_p)
668 x_display_and_set_cursor (w, 1, output_cursor.hpos,
669 output_cursor.vpos,
670 output_cursor.x, output_cursor.y);
671 x_draw_vertical_border (w);
672 UNBLOCK_INPUT;
673 }
674
675 updated_window = NULL;
676}
dc6f92b8 677
dc6f92b8 678
06a2c219
GM
679/* End update of frame F. This function is installed as a hook in
680 update_end. */
681
682static void
683x_update_end (f)
684 struct frame *f;
685{
686 /* Mouse highlight may be displayed again. */
aa8bff2e 687 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
b8009dd1 688
06a2c219 689 BLOCK_INPUT;
334208b7 690 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
691 UNBLOCK_INPUT;
692}
b8009dd1 693
06a2c219
GM
694
695/* This function is called from various places in xdisp.c whenever a
696 complete update has been performed. The global variable
697 updated_window is not available here. */
b8009dd1 698
dfcf069d 699static void
b8009dd1 700XTframe_up_to_date (f)
06a2c219 701 struct frame *f;
b8009dd1 702{
06a2c219 703 if (FRAME_X_P (f))
514e4681 704 {
06a2c219
GM
705 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
706 if (dpyinfo->mouse_face_deferred_gc
707 || f == dpyinfo->mouse_face_mouse_frame)
708 {
709 BLOCK_INPUT;
710 if (dpyinfo->mouse_face_mouse_frame)
711 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
712 dpyinfo->mouse_face_mouse_x,
713 dpyinfo->mouse_face_mouse_y);
714 dpyinfo->mouse_face_deferred_gc = 0;
715 UNBLOCK_INPUT;
716 }
514e4681 717 }
b8009dd1 718}
06a2c219
GM
719
720
721/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
722 arrow bitmaps, or clear the areas where they would be displayed
723 before DESIRED_ROW is made current. The window being updated is
724 found in updated_window. This function It is called from
725 update_window_line only if it is known that there are differences
726 between bitmaps to be drawn between current row and DESIRED_ROW. */
727
728static void
729x_after_update_window_line (desired_row)
730 struct glyph_row *desired_row;
731{
732 struct window *w = updated_window;
733
734 xassert (w);
735
736 if (!desired_row->mode_line_p && !w->pseudo_window_p)
737 {
738 BLOCK_INPUT;
739 x_draw_row_bitmaps (w, desired_row);
740
741 /* When a window has disappeared, make sure that no rest of
742 full-width rows stays visible in the internal border. */
743 if (windows_or_buffers_changed)
744 {
745 struct frame *f = XFRAME (w->frame);
746 int width = FRAME_INTERNAL_BORDER_WIDTH (f);
747 int height = desired_row->visible_height;
110859fc
GM
748 int x = (window_box_right (w, -1)
749 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
06a2c219
GM
750 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
751
752 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
753 x, y, width, height, False);
754 }
755
756 UNBLOCK_INPUT;
757 }
758}
759
760
761/* Draw the bitmap WHICH in one of the areas to the left or right of
762 window W. ROW is the glyph row for which to display the bitmap; it
763 determines the vertical position at which the bitmap has to be
764 drawn. */
765
766static void
767x_draw_bitmap (w, row, which)
768 struct window *w;
769 struct glyph_row *row;
770 enum bitmap_type which;
771{
772 struct frame *f = XFRAME (WINDOW_FRAME (w));
773 Display *display = FRAME_X_DISPLAY (f);
774 Window window = FRAME_X_WINDOW (f);
775 int x, y, wd, h, dy;
776 unsigned char *bits;
777 Pixmap pixmap;
778 GC gc = f->output_data.x->normal_gc;
779 struct face *face;
780 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
781
782 /* Must clip because of partially visible lines. */
783 x_clip_to_row (w, row, gc, 1);
784
785 switch (which)
786 {
787 case LEFT_TRUNCATION_BITMAP:
788 wd = left_width;
789 h = left_height;
790 bits = left_bits;
791 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
792 - wd
110859fc 793 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
794 break;
795
796 case OVERLAY_ARROW_BITMAP:
797 wd = left_width;
798 h = left_height;
799 bits = ov_bits;
800 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
801 - wd
110859fc 802 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
803 break;
804
805 case RIGHT_TRUNCATION_BITMAP:
806 wd = right_width;
807 h = right_height;
808 bits = right_bits;
809 x = window_box_right (w, -1);
110859fc 810 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
811 break;
812
813 case CONTINUED_LINE_BITMAP:
814 wd = right_width;
815 h = right_height;
816 bits = continued_bits;
817 x = window_box_right (w, -1);
110859fc 818 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
819 break;
820
821 case CONTINUATION_LINE_BITMAP:
822 wd = continuation_width;
823 h = continuation_height;
824 bits = continuation_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 ZV_LINE_BITMAP:
831 wd = zv_width;
832 h = zv_height;
833 bits = zv_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 default:
840 abort ();
841 }
842
843 /* Convert to frame coordinates. Set dy to the offset in the row to
844 start drawing the bitmap. */
845 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
846 dy = (row->height - h) / 2;
847
848 /* Draw the bitmap. I believe these small pixmaps can be cached
849 by the server. */
850 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
851 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
852 face->foreground,
853 face->background, depth);
854 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
855 XFreePixmap (display, pixmap);
856 XSetClipMask (display, gc, None);
857}
858
859
860/* Draw flags bitmaps for glyph row ROW on window W. Call this
861 function with input blocked. */
862
863static void
864x_draw_row_bitmaps (w, row)
865 struct window *w;
866 struct glyph_row *row;
867{
868 struct frame *f = XFRAME (w->frame);
869 enum bitmap_type bitmap;
870 struct face *face;
045dee35 871 int header_line_height = -1;
06a2c219
GM
872
873 xassert (interrupt_input_blocked);
874
875 /* If row is completely invisible, because of vscrolling, we
876 don't have to draw anything. */
877 if (row->visible_height <= 0)
878 return;
879
880 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
881 PREPARE_FACE_FOR_DISPLAY (f, face);
882
883 /* Decide which bitmap to draw at the left side. */
884 if (row->overlay_arrow_p)
885 bitmap = OVERLAY_ARROW_BITMAP;
886 else if (row->truncated_on_left_p)
887 bitmap = LEFT_TRUNCATION_BITMAP;
888 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
889 bitmap = CONTINUATION_LINE_BITMAP;
890 else if (row->indicate_empty_line_p)
891 bitmap = ZV_LINE_BITMAP;
892 else
893 bitmap = NO_BITMAP;
894
895 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
896 the flags area. */
897 if (bitmap == NO_BITMAP
110859fc 898 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
899 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
900 {
901 /* If W has a vertical border to its left, don't draw over it. */
902 int border = ((XFASTINT (w->left) > 0
903 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
904 ? 1 : 0);
905 int left = window_box_left (w, -1);
906
045dee35
GM
907 if (header_line_height < 0)
908 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
909
910 /* In case the same realized face is used for bitmap areas and
911 for something displayed in the text (e.g. face `region' on
912 mono-displays, the fill style may have been changed to
913 FillSolid in x_draw_glyph_string_background. */
914 if (face->stipple)
915 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
916 else
917 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
918
06a2c219
GM
919 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
920 face->gc,
921 (left
110859fc 922 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219 923 + border),
045dee35 924 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 925 row->y)),
110859fc 926 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
06a2c219 927 row->visible_height);
dcd08bfb
GM
928 if (!face->stipple)
929 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
930 }
931
932 /* Draw the left bitmap. */
933 if (bitmap != NO_BITMAP)
934 x_draw_bitmap (w, row, bitmap);
935
936 /* Decide which bitmap to draw at the right side. */
937 if (row->truncated_on_right_p)
938 bitmap = RIGHT_TRUNCATION_BITMAP;
939 else if (row->continued_p)
940 bitmap = CONTINUED_LINE_BITMAP;
941 else
942 bitmap = NO_BITMAP;
943
944 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
945 the flags area. */
946 if (bitmap == NO_BITMAP
110859fc 947 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
948 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
949 {
950 int right = window_box_right (w, -1);
951
045dee35
GM
952 if (header_line_height < 0)
953 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
954
955 /* In case the same realized face is used for bitmap areas and
956 for something displayed in the text (e.g. face `region' on
957 mono-displays, the fill style may have been changed to
958 FillSolid in x_draw_glyph_string_background. */
959 if (face->stipple)
960 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
961 else
962 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
06a2c219
GM
963 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
964 face->gc,
965 right,
045dee35 966 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 967 row->y)),
110859fc 968 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
06a2c219 969 row->visible_height);
dcd08bfb
GM
970 if (!face->stipple)
971 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
972 }
973
974 /* Draw the right bitmap. */
975 if (bitmap != NO_BITMAP)
976 x_draw_bitmap (w, row, bitmap);
977}
978
dc6f92b8 979\f
06a2c219
GM
980/***********************************************************************
981 Line Highlighting
982 ***********************************************************************/
dc6f92b8 983
06a2c219
GM
984/* External interface to control of standout mode. Not used for X
985 frames. Aborts when called. */
986
987static void
dc6f92b8
JB
988XTreassert_line_highlight (new, vpos)
989 int new, vpos;
990{
06a2c219 991 abort ();
dc6f92b8
JB
992}
993
06a2c219
GM
994
995/* Call this when about to modify line at position VPOS and change
996 whether it is highlighted. Not used for X frames. Aborts when
997 called. */
dc6f92b8 998
dfcf069d 999static void
06a2c219
GM
1000x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1001 int new_highlight, vpos, y, first_unused_hpos;
dc6f92b8 1002{
06a2c219 1003 abort ();
dc6f92b8
JB
1004}
1005
06a2c219
GM
1006
1007/* This is called when starting Emacs and when restarting after
1008 suspend. When starting Emacs, no X window is mapped. And nothing
1009 must be done to Emacs's own window if it is suspended (though that
1010 rarely happens). */
dc6f92b8 1011
dfcf069d 1012static void
dc6f92b8
JB
1013XTset_terminal_modes ()
1014{
1015}
1016
06a2c219
GM
1017/* This is called when exiting or suspending Emacs. Exiting will make
1018 the X-windows go away, and suspending requires no action. */
dc6f92b8 1019
dfcf069d 1020static void
dc6f92b8
JB
1021XTreset_terminal_modes ()
1022{
dc6f92b8 1023}
06a2c219
GM
1024
1025
dc6f92b8 1026\f
06a2c219
GM
1027/***********************************************************************
1028 Output Cursor
1029 ***********************************************************************/
1030
1031/* Set the global variable output_cursor to CURSOR. All cursor
1032 positions are relative to updated_window. */
dc6f92b8 1033
dfcf069d 1034static void
06a2c219
GM
1035set_output_cursor (cursor)
1036 struct cursor_pos *cursor;
dc6f92b8 1037{
06a2c219
GM
1038 output_cursor.hpos = cursor->hpos;
1039 output_cursor.vpos = cursor->vpos;
1040 output_cursor.x = cursor->x;
1041 output_cursor.y = cursor->y;
1042}
1043
1044
1045/* Set a nominal cursor position.
dc6f92b8 1046
06a2c219
GM
1047 HPOS and VPOS are column/row positions in a window glyph matrix. X
1048 and Y are window text area relative pixel positions.
1049
1050 If this is done during an update, updated_window will contain the
1051 window that is being updated and the position is the future output
1052 cursor position for that window. If updated_window is null, use
1053 selected_window and display the cursor at the given position. */
1054
1055static void
1056XTcursor_to (vpos, hpos, y, x)
1057 int vpos, hpos, y, x;
1058{
1059 struct window *w;
1060
1061 /* If updated_window is not set, work on selected_window. */
1062 if (updated_window)
1063 w = updated_window;
1064 else
1065 w = XWINDOW (selected_window);
dbcb258a 1066
06a2c219
GM
1067 /* Set the output cursor. */
1068 output_cursor.hpos = hpos;
1069 output_cursor.vpos = vpos;
1070 output_cursor.x = x;
1071 output_cursor.y = y;
dc6f92b8 1072
06a2c219
GM
1073 /* If not called as part of an update, really display the cursor.
1074 This will also set the cursor position of W. */
1075 if (updated_window == NULL)
dc6f92b8
JB
1076 {
1077 BLOCK_INPUT;
06a2c219 1078 x_display_cursor (w, 1, hpos, vpos, x, y);
b86bd3dd 1079 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
dc6f92b8
JB
1080 UNBLOCK_INPUT;
1081 }
1082}
dc43ef94 1083
06a2c219
GM
1084
1085\f
1086/***********************************************************************
1087 Display Iterator
1088 ***********************************************************************/
1089
1090/* Function prototypes of this page. */
1091
1092static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1093 struct glyph *,
1094 XChar2b *));
1095static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1096 int, XChar2b *, int));
1097static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1098static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1099static void x_append_glyph P_ ((struct it *));
1100static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1101 int, int, double));
1102static void x_produce_glyphs P_ ((struct it *));
06a2c219
GM
1103static void x_produce_image_glyph P_ ((struct it *it));
1104
1105
1106/* Return a pointer to per-char metric information in FONT of a
1107 character pointed by B which is a pointer to an XChar2b. */
dc43ef94
KH
1108
1109#define PER_CHAR_METRIC(font, b) \
1110 ((font)->per_char \
1111 ? ((font)->per_char + (b)->byte2 - (font)->min_char_or_byte2 \
1112 + (((font)->min_byte1 || (font)->max_byte1) \
1113 ? (((b)->byte1 - (font)->min_byte1) \
1114 * ((font)->max_char_or_byte2 - (font)->min_char_or_byte2 + 1)) \
1115 : 0)) \
1116 : &((font)->max_bounds))
1117
dc6f92b8 1118
06a2c219
GM
1119/* Get metrics of character CHAR2B in FONT. Value is always non-null.
1120 If CHAR2B is not contained in FONT, the font's default character
1121 metric is returned. */
dc43ef94 1122
06a2c219
GM
1123static INLINE XCharStruct *
1124x_per_char_metric (font, char2b)
1125 XFontStruct *font;
1126 XChar2b *char2b;
1127{
1128 /* The result metric information. */
1129 XCharStruct *pcm = NULL;
dc6f92b8 1130
06a2c219 1131 xassert (font && char2b);
dc6f92b8 1132
06a2c219 1133 if (font->per_char != NULL)
dc6f92b8 1134 {
06a2c219 1135 if (font->min_byte1 == 0 && font->max_byte1 == 0)
dc43ef94 1136 {
06a2c219
GM
1137 /* min_char_or_byte2 specifies the linear character index
1138 corresponding to the first element of the per_char array,
1139 max_char_or_byte2 is the index of the last character. A
1140 character with non-zero CHAR2B->byte1 is not in the font.
1141 A character with byte2 less than min_char_or_byte2 or
1142 greater max_char_or_byte2 is not in the font. */
1143 if (char2b->byte1 == 0
1144 && char2b->byte2 >= font->min_char_or_byte2
1145 && char2b->byte2 <= font->max_char_or_byte2)
1146 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
dc43ef94 1147 }
06a2c219 1148 else
dc6f92b8 1149 {
06a2c219
GM
1150 /* If either min_byte1 or max_byte1 are nonzero, both
1151 min_char_or_byte2 and max_char_or_byte2 are less than
1152 256, and the 2-byte character index values corresponding
1153 to the per_char array element N (counting from 0) are:
1154
1155 byte1 = N/D + min_byte1
1156 byte2 = N\D + min_char_or_byte2
1157
1158 where:
1159
1160 D = max_char_or_byte2 - min_char_or_byte2 + 1
1161 / = integer division
1162 \ = integer modulus */
1163 if (char2b->byte1 >= font->min_byte1
1164 && char2b->byte1 <= font->max_byte1
1165 && char2b->byte2 >= font->min_char_or_byte2
1166 && char2b->byte2 <= font->max_char_or_byte2)
1167 {
1168 pcm = (font->per_char
1169 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1170 * (char2b->byte1 - font->min_byte1))
1171 + (char2b->byte2 - font->min_char_or_byte2));
1172 }
dc6f92b8 1173 }
06a2c219
GM
1174 }
1175 else
1176 {
1177 /* If the per_char pointer is null, all glyphs between the first
1178 and last character indexes inclusive have the same
1179 information, as given by both min_bounds and max_bounds. */
1180 if (char2b->byte2 >= font->min_char_or_byte2
1181 && char2b->byte2 <= font->max_char_or_byte2)
1182 pcm = &font->max_bounds;
1183 }
dc6f92b8 1184
4a4dc352 1185
06a2c219
GM
1186 if (pcm == NULL || pcm->width == 0)
1187 {
1188 /* Character not contained in the font. FONT->default_char
1189 gives the character that will be printed. FONT->default_char
1190 is a 16-bit character code with byte1 in the most significant
1191 byte and byte2 in the least significant byte. */
1192 XChar2b default_char;
1193 default_char.byte1 = (font->default_char >> BITS_PER_CHAR) & 0xff;
1194 default_char.byte2 = font->default_char & 0xff;
1195
1196 /* Avoid an endless recursion if FONT->default_char itself
1197 hasn't per char metrics. handa@etl.go.jp reports that some
1198 fonts have this problem. */
1199 if (default_char.byte1 != char2b->byte1
1200 || default_char.byte2 != char2b->byte2)
1201 pcm = x_per_char_metric (font, &default_char);
1202 else
1203 pcm = &font->max_bounds;
1204 }
1205
1206 return pcm;
1207}
b73b6aaf 1208
57b03282 1209
06a2c219
GM
1210/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1211 the two-byte form of C. Encoding is returned in *CHAR2B. */
dc43ef94 1212
06a2c219
GM
1213static INLINE void
1214x_encode_char (c, char2b, font_info)
1215 int c;
1216 XChar2b *char2b;
1217 struct font_info *font_info;
1218{
1219 int charset = CHAR_CHARSET (c);
1220 XFontStruct *font = font_info->font;
1221
1222 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1223 This may be either a program in a special encoder language or a
1224 fixed encoding. */
1225 if (font_info->font_encoder)
1226 {
1227 /* It's a program. */
1228 struct ccl_program *ccl = font_info->font_encoder;
1229
1230 if (CHARSET_DIMENSION (charset) == 1)
1231 {
1232 ccl->reg[0] = charset;
1233 ccl->reg[1] = char2b->byte2;
1234 }
1235 else
1236 {
1237 ccl->reg[0] = charset;
1238 ccl->reg[1] = char2b->byte1;
1239 ccl->reg[2] = char2b->byte2;
1240 }
1241
1242 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1243
1244 /* We assume that MSBs are appropriately set/reset by CCL
1245 program. */
1246 if (font->max_byte1 == 0) /* 1-byte font */
1247 char2b->byte2 = ccl->reg[1];
1248 else
1249 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1250 }
1251 else if (font_info->encoding[charset])
1252 {
1253 /* Fixed encoding scheme. See fontset.h for the meaning of the
1254 encoding numbers. */
1255 int enc = font_info->encoding[charset];
1256
1257 if ((enc == 1 || enc == 2)
1258 && CHARSET_DIMENSION (charset) == 2)
1259 char2b->byte1 |= 0x80;
1260
1261 if (enc == 1 || enc == 3)
1262 char2b->byte2 |= 0x80;
1263 }
1264}
1265
1266
1267/* Get face and two-byte form of character C in face FACE_ID on frame
1268 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1269 means we want to display multibyte text. Value is a pointer to a
1270 realized face that is ready for display. */
1271
1272static INLINE struct face *
1273x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1274 struct frame *f;
1275 int c, face_id;
1276 XChar2b *char2b;
1277 int multibyte_p;
1278{
1279 struct face *face = FACE_FROM_ID (f, face_id);
1280
1281 if (!multibyte_p)
1282 {
1283 /* Unibyte case. We don't have to encode, but we have to make
1284 sure to use a face suitable for unibyte. */
1285 char2b->byte1 = 0;
1286 char2b->byte2 = c;
1287
1288 if (!FACE_SUITABLE_FOR_CHARSET_P (face, -1))
1289 {
1290 face_id = FACE_FOR_CHARSET (f, face_id, -1);
1291 face = FACE_FROM_ID (f, face_id);
1292 }
1293 }
1294 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1295 {
1296 /* Case of ASCII in a face known to fit ASCII. */
1297 char2b->byte1 = 0;
1298 char2b->byte2 = c;
1299 }
1300 else
1301 {
1302 int c1, c2, charset;
1303
1304 /* Split characters into bytes. If c2 is -1 afterwards, C is
1305 really a one-byte character so that byte1 is zero. */
1306 SPLIT_CHAR (c, charset, c1, c2);
1307 if (c2 > 0)
1308 char2b->byte1 = c1, char2b->byte2 = c2;
1309 else
1310 char2b->byte1 = 0, char2b->byte2 = c1;
1311
1312 /* Get the face for displaying C. If `face' is not suitable for
1313 charset, get the one that fits. (This can happen for the
1314 translations of composite characters where the glyph
1315 specifies a face for ASCII, but translations have a different
1316 charset.) */
1317 if (!FACE_SUITABLE_FOR_CHARSET_P (face, charset))
1318 {
1319 face_id = FACE_FOR_CHARSET (f, face_id, charset);
1320 face = FACE_FROM_ID (f, face_id);
1321 }
1322
1323 /* Maybe encode the character in *CHAR2B. */
1324 if (charset != CHARSET_ASCII)
1325 {
1326 struct font_info *font_info
1327 = FONT_INFO_FROM_ID (f, face->font_info_id);
1328 if (font_info)
1329 {
1330 x_encode_char (c, char2b, font_info);
1331 if (charset == charset_latin_iso8859_1)
1332 {
1333 xassert (((XFontStruct *) font_info->font)->max_char_or_byte2
1334 >= 0x80);
1335 char2b->byte2 |= 0x80;
1336 }
1337 }
1338 }
1339 }
1340
1341 /* Make sure X resources of the face are allocated. */
1342 xassert (face != NULL);
1343 PREPARE_FACE_FOR_DISPLAY (f, face);
1344
1345 return face;
1346}
1347
1348
1349/* Get face and two-byte form of character glyph GLYPH on frame F.
1350 The encoding of GLYPH->u.ch.code is returned in *CHAR2B. Value is
1351 a pointer to a realized face that is ready for display. */
1352
1353static INLINE struct face *
1354x_get_glyph_face_and_encoding (f, glyph, char2b)
1355 struct frame *f;
1356 struct glyph *glyph;
1357 XChar2b *char2b;
1358{
1359 struct face *face;
1360
1361 xassert (glyph->type == CHAR_GLYPH);
1362 face = FACE_FROM_ID (f, glyph->u.ch.face_id);
1363
1364 if (!glyph->multibyte_p)
1365 {
1366 /* Unibyte case. We don't have to encode, but we have to make
1367 sure to use a face suitable for unibyte. */
1368 char2b->byte1 = 0;
1369 char2b->byte2 = glyph->u.ch.code;
1370 }
1371 else if (glyph->u.ch.code < 128
1372 && glyph->u.ch.face_id < BASIC_FACE_ID_SENTINEL)
1373 {
1374 /* Case of ASCII in a face known to fit ASCII. */
1375 char2b->byte1 = 0;
1376 char2b->byte2 = glyph->u.ch.code;
1377 }
1378 else
1379 {
1380 int c1, c2, charset;
1381
1382 /* Split characters into bytes. If c2 is -1 afterwards, C is
1383 really a one-byte character so that byte1 is zero. */
1384 SPLIT_CHAR (glyph->u.ch.code, charset, c1, c2);
1385 if (c2 > 0)
1386 char2b->byte1 = c1, char2b->byte2 = c2;
1387 else
1388 char2b->byte1 = 0, char2b->byte2 = c1;
1389
1390 /* Maybe encode the character in *CHAR2B. */
1391 if (charset != CHARSET_ASCII)
1392 {
1393 struct font_info *font_info
1394 = FONT_INFO_FROM_ID (f, face->font_info_id);
1395 if (font_info)
1396 {
1397 x_encode_char (glyph->u.ch.code, char2b, font_info);
1398 if (charset == charset_latin_iso8859_1)
1399 char2b->byte2 |= 0x80;
1400 }
1401 }
1402 }
1403
1404 /* Make sure X resources of the face are allocated. */
1405 xassert (face != NULL);
1406 PREPARE_FACE_FOR_DISPLAY (f, face);
1407 return face;
1408}
1409
1410
1411/* Store one glyph for IT->char_to_display in IT->glyph_row.
1412 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1413
1414static INLINE void
1415x_append_glyph (it)
1416 struct it *it;
1417{
1418 struct glyph *glyph;
1419 enum glyph_row_area area = it->area;
1420
1421 xassert (it->glyph_row);
1422 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1423
1424 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1425 if (glyph < it->glyph_row->glyphs[area + 1])
1426 {
1427 /* Play it safe. If sub-structures of the glyph are not all the
1428 same size, it otherwise be that some bits stay set. This
1429 would prevent a comparison with GLYPH_EQUAL_P. */
1430 glyph->u.val = 0;
1431
1432 glyph->type = CHAR_GLYPH;
1433 glyph->pixel_width = it->pixel_width;
1434 glyph->u.ch.code = it->char_to_display;
1435 glyph->u.ch.face_id = it->face_id;
1436 glyph->charpos = CHARPOS (it->position);
1437 glyph->object = it->object;
1438 glyph->left_box_line_p = it->start_of_box_run_p;
1439 glyph->right_box_line_p = it->end_of_box_run_p;
1440 glyph->voffset = it->voffset;
1441 glyph->multibyte_p = it->multibyte_p;
66ac4b0e
GM
1442 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1443 || it->phys_descent > it->descent);
06a2c219
GM
1444 ++it->glyph_row->used[area];
1445 }
1446}
1447
1448
1449/* Change IT->ascent and IT->height according to the setting of
1450 IT->voffset. */
1451
1452static INLINE void
1453take_vertical_position_into_account (it)
1454 struct it *it;
1455{
1456 if (it->voffset)
1457 {
1458 if (it->voffset < 0)
1459 /* Increase the ascent so that we can display the text higher
1460 in the line. */
1461 it->ascent += abs (it->voffset);
1462 else
1463 /* Increase the descent so that we can display the text lower
1464 in the line. */
1465 it->descent += it->voffset;
1466 }
1467}
1468
1469
1470/* Produce glyphs/get display metrics for the image IT is loaded with.
1471 See the description of struct display_iterator in dispextern.h for
1472 an overview of struct display_iterator. */
1473
1474static void
1475x_produce_image_glyph (it)
1476 struct it *it;
1477{
1478 struct image *img;
1479 struct face *face;
1480
1481 xassert (it->what == IT_IMAGE);
1482
1483 face = FACE_FROM_ID (it->f, it->face_id);
1484 img = IMAGE_FROM_ID (it->f, it->image_id);
1485 xassert (img);
1486
1487 /* Make sure X resources of the face and image are loaded. */
1488 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1489 prepare_image_for_display (it->f, img);
1490
66ac4b0e
GM
1491 it->ascent = it->phys_ascent = IMAGE_ASCENT (img);
1492 it->descent = it->phys_descent = img->height + 2 * img->margin - it->ascent;
06a2c219
GM
1493 it->pixel_width = img->width + 2 * img->margin;
1494
1495 it->nglyphs = 1;
1496
1497 if (face->box != FACE_NO_BOX)
1498 {
1499 it->ascent += face->box_line_width;
1500 it->descent += face->box_line_width;
1501
1502 if (it->start_of_box_run_p)
1503 it->pixel_width += face->box_line_width;
1504 if (it->end_of_box_run_p)
1505 it->pixel_width += face->box_line_width;
1506 }
1507
1508 take_vertical_position_into_account (it);
1509
1510 if (it->glyph_row)
1511 {
1512 struct glyph *glyph;
1513 enum glyph_row_area area = it->area;
1514
1515 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1516 if (glyph < it->glyph_row->glyphs[area + 1])
1517 {
1518 glyph->type = IMAGE_GLYPH;
1519 glyph->u.img.id = img->id;
1520 glyph->u.img.face_id = it->face_id;
1521 glyph->pixel_width = it->pixel_width;
1522 glyph->charpos = CHARPOS (it->position);
1523 glyph->object = it->object;
1524 glyph->left_box_line_p = it->start_of_box_run_p;
1525 glyph->right_box_line_p = it->end_of_box_run_p;
1526 glyph->voffset = it->voffset;
1527 glyph->multibyte_p = it->multibyte_p;
1528 ++it->glyph_row->used[area];
1529 }
1530 }
1531}
1532
1533
1534/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1535 of the glyph, WIDTH and HEIGHT are the width and height of the
1536 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1537 ascent of the glyph (0 <= ASCENT <= 1). */
1538
1539static void
1540x_append_stretch_glyph (it, object, width, height, ascent)
1541 struct it *it;
1542 Lisp_Object object;
1543 int width, height;
1544 double ascent;
1545{
1546 struct glyph *glyph;
1547 enum glyph_row_area area = it->area;
1548
1549 xassert (ascent >= 0 && ascent <= 1);
1550
1551 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1552 if (glyph < it->glyph_row->glyphs[area + 1])
1553 {
1554 glyph->type = STRETCH_GLYPH;
1555 glyph->u.stretch.ascent = height * ascent;
1556 glyph->u.stretch.height = height;
1557 glyph->u.stretch.face_id = it->face_id;
1558 glyph->pixel_width = width;
1559 glyph->charpos = CHARPOS (it->position);
1560 glyph->object = object;
1561 glyph->left_box_line_p = it->start_of_box_run_p;
1562 glyph->right_box_line_p = it->end_of_box_run_p;
1563 glyph->voffset = it->voffset;
1564 glyph->multibyte_p = it->multibyte_p;
1565 ++it->glyph_row->used[area];
1566 }
1567}
1568
1569
1570/* Produce a stretch glyph for iterator IT. IT->object is the value
1571 of the glyph property displayed. The value must be a list
1572 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1573 being recognized:
1574
1575 1. `:width WIDTH' specifies that the space should be WIDTH *
1576 canonical char width wide. WIDTH may be an integer or floating
1577 point number.
1578
1579 2. `:relative-width FACTOR' specifies that the width of the stretch
1580 should be computed from the width of the first character having the
1581 `glyph' property, and should be FACTOR times that width.
1582
1583 3. `:align-to HPOS' specifies that the space should be wide enough
1584 to reach HPOS, a value in canonical character units.
1585
1586 Exactly one of the above pairs must be present.
1587
1588 4. `:height HEIGHT' specifies that the height of the stretch produced
1589 should be HEIGHT, measured in canonical character units.
1590
1591 5. `:relative-height FACTOR' specifies that the height of the the
1592 stretch should be FACTOR times the height of the characters having
1593 the glyph property.
1594
1595 Either none or exactly one of 4 or 5 must be present.
1596
1597 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1598 of the stretch should be used for the ascent of the stretch.
1599 ASCENT must be in the range 0 <= ASCENT <= 100. */
1600
1601#define NUMVAL(X) \
1602 ((INTEGERP (X) || FLOATP (X)) \
1603 ? XFLOATINT (X) \
1604 : - 1)
1605
1606
1607static void
1608x_produce_stretch_glyph (it)
1609 struct it *it;
1610{
1611 /* (space :width WIDTH :height HEIGHT. */
1612 extern Lisp_Object QCwidth, QCheight, QCascent, Qspace;
1613 extern Lisp_Object QCrelative_width, QCrelative_height;
1614 extern Lisp_Object QCalign_to;
1615 Lisp_Object prop, plist;
1616 double width = 0, height = 0, ascent = 0;
1617 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1618 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1619
1620 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1621
1622 /* List should start with `space'. */
1623 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1624 plist = XCDR (it->object);
1625
1626 /* Compute the width of the stretch. */
1627 if (prop = Fplist_get (plist, QCwidth),
1628 NUMVAL (prop) > 0)
1629 /* Absolute width `:width WIDTH' specified and valid. */
1630 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1631 else if (prop = Fplist_get (plist, QCrelative_width),
1632 NUMVAL (prop) > 0)
1633 {
1634 /* Relative width `:relative-width FACTOR' specified and valid.
1635 Compute the width of the characters having the `glyph'
1636 property. */
1637 struct it it2;
1638 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1639
1640 it2 = *it;
1641 if (it->multibyte_p)
1642 {
1643 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1644 - IT_BYTEPOS (*it));
1645 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1646 }
1647 else
1648 it2.c = *p, it2.len = 1;
1649
1650 it2.glyph_row = NULL;
1651 it2.what = IT_CHARACTER;
1652 x_produce_glyphs (&it2);
1653 width = NUMVAL (prop) * it2.pixel_width;
1654 }
1655 else if (prop = Fplist_get (plist, QCalign_to),
1656 NUMVAL (prop) > 0)
1657 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1658 else
1659 /* Nothing specified -> width defaults to canonical char width. */
1660 width = CANON_X_UNIT (it->f);
1661
1662 /* Compute height. */
1663 if (prop = Fplist_get (plist, QCheight),
1664 NUMVAL (prop) > 0)
1665 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1666 else if (prop = Fplist_get (plist, QCrelative_height),
1667 NUMVAL (prop) > 0)
1668 height = FONT_HEIGHT (font) * NUMVAL (prop);
1669 else
1670 height = FONT_HEIGHT (font);
1671
1672 /* Compute percentage of height used for ascent. If
1673 `:ascent ASCENT' is present and valid, use that. Otherwise,
1674 derive the ascent from the font in use. */
1675 if (prop = Fplist_get (plist, QCascent),
1676 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1677 ascent = NUMVAL (prop) / 100.0;
1678 else
1679 ascent = (double) font->ascent / FONT_HEIGHT (font);
1680
1681 if (width <= 0)
1682 width = 1;
1683 if (height <= 0)
1684 height = 1;
1685
1686 if (it->glyph_row)
1687 {
1688 Lisp_Object object = it->stack[it->sp - 1].string;
1689 if (!STRINGP (object))
1690 object = it->w->buffer;
1691 x_append_stretch_glyph (it, object, width, height, ascent);
1692 }
1693
1694 it->pixel_width = width;
66ac4b0e
GM
1695 it->ascent = it->phys_ascent = height * ascent;
1696 it->descent = it->phys_descent = height - it->ascent;
06a2c219
GM
1697 it->nglyphs = 1;
1698
1699 if (face->box != FACE_NO_BOX)
1700 {
1701 it->ascent += face->box_line_width;
1702 it->descent += face->box_line_width;
1703
1704 if (it->start_of_box_run_p)
1705 it->pixel_width += face->box_line_width;
1706 if (it->end_of_box_run_p)
1707 it->pixel_width += face->box_line_width;
1708 }
1709
1710 take_vertical_position_into_account (it);
1711}
1712
1713
1714/* Produce glyphs/get display metrics for the display element IT is
1715 loaded with. See the description of struct display_iterator in
1716 dispextern.h for an overview of struct display_iterator. */
1717
1718static void
1719x_produce_glyphs (it)
1720 struct it *it;
1721{
1722 if (it->what == IT_CHARACTER)
1723 {
1724 XChar2b char2b;
1725 XFontStruct *font;
1726 struct face *face;
1727 XCharStruct *pcm;
06a2c219 1728 int font_not_found_p;
06a2c219
GM
1729
1730 /* Maybe translate single-byte characters to multibyte. */
1731 it->char_to_display = it->c;
1732 if (unibyte_display_via_language_environment
1733 && SINGLE_BYTE_CHAR_P (it->c)
1734 && (it->c >= 0240
1735 || (it->c >= 0200
1736 && !NILP (Vnonascii_translation_table))))
1737 {
1738 it->char_to_display = unibyte_char_to_multibyte (it->c);
1739 it->charset = CHAR_CHARSET (it->char_to_display);
1740 }
1741
1742 /* Get face and font to use. Encode IT->char_to_display. */
1743 face = x_get_char_face_and_encoding (it->f, it->char_to_display,
1744 it->face_id, &char2b,
1745 it->multibyte_p);
1746 font = face->font;
1747
1748 /* When no suitable font found, use the default font. */
1749 font_not_found_p = font == NULL;
1750 if (font_not_found_p)
1751 font = FRAME_FONT (it->f);
1752
1753 if (it->char_to_display >= ' '
1754 && (!it->multibyte_p || it->char_to_display < 128))
1755 {
1756 /* Either unibyte or ASCII. */
1757 int stretched_p;
1758
1759 it->nglyphs = 1;
06a2c219
GM
1760
1761 pcm = x_per_char_metric (font, &char2b);
66ac4b0e
GM
1762 it->ascent = font->ascent;
1763 it->descent = font->descent;
1764 it->phys_ascent = pcm->ascent;
1765 it->phys_descent = pcm->descent;
06a2c219
GM
1766 it->pixel_width = pcm->width;
1767
1768 /* If this is a space inside a region of text with
1769 `space-width' property, change its width. */
1770 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1771 if (stretched_p)
1772 it->pixel_width *= XFLOATINT (it->space_width);
1773
1774 /* If face has a box, add the box thickness to the character
1775 height. If character has a box line to the left and/or
1776 right, add the box line width to the character's width. */
1777 if (face->box != FACE_NO_BOX)
1778 {
1779 int thick = face->box_line_width;
1780
1781 it->ascent += thick;
1782 it->descent += thick;
1783
1784 if (it->start_of_box_run_p)
1785 it->pixel_width += thick;
1786 if (it->end_of_box_run_p)
1787 it->pixel_width += thick;
1788 }
1789
1790 /* If face has an overline, add the height of the overline
1791 (1 pixel) and a 1 pixel margin to the character height. */
1792 if (face->overline_p)
1793 it->ascent += 2;
1794
1795 take_vertical_position_into_account (it);
1796
1797 /* If we have to actually produce glyphs, do it. */
1798 if (it->glyph_row)
1799 {
1800 if (stretched_p)
1801 {
1802 /* Translate a space with a `space-width' property
1803 into a stretch glyph. */
1804 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1805 x_append_stretch_glyph (it, it->object, it->pixel_width,
1806 it->ascent + it->descent, ascent);
1807 }
1808 else
1809 x_append_glyph (it);
1810
1811 /* If characters with lbearing or rbearing are displayed
1812 in this line, record that fact in a flag of the
1813 glyph row. This is used to optimize X output code. */
1814 if (pcm->lbearing < 0
1815 || pcm->rbearing > pcm->width)
1816 it->glyph_row->contains_overlapping_glyphs_p = 1;
1817 }
1818 }
1819 else if (it->char_to_display == '\n')
1820 {
1821 /* A newline has no width but we need the height of the line. */
1822 it->pixel_width = 0;
1823 it->nglyphs = 0;
66ac4b0e
GM
1824 it->ascent = it->phys_ascent = font->ascent;
1825 it->descent = it->phys_descent = font->descent;
06a2c219
GM
1826
1827 if (face->box != FACE_NO_BOX)
1828 {
1829 int thick = face->box_line_width;
1830 it->ascent += thick;
1831 it->descent += thick;
1832 }
1833 }
1834 else if (it->char_to_display == '\t')
1835 {
1836 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1837 int x = (it->current_x
1838 - it->prompt_width
1839 + it->continuation_lines_width);
1840 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1841
1842 it->pixel_width = next_tab_x - x;
1843 it->nglyphs = 1;
66ac4b0e
GM
1844 it->ascent = it->phys_ascent = font->ascent;
1845 it->descent = it->phys_descent = font->descent;
06a2c219
GM
1846
1847 if (it->glyph_row)
1848 {
1849 double ascent = (double) it->ascent / (it->ascent + it->descent);
1850 x_append_stretch_glyph (it, it->object, it->pixel_width,
1851 it->ascent + it->descent, ascent);
1852 }
1853 }
1854 else
1855 {
1856 /* A multi-byte character. Assume that the display width of the
1857 character is the width of the character multiplied by the
1858 width of the font. There has to be better support for
1859 variable sizes in cmpchar_info to do anything better than
1860 that.
1861
1862 Note: composite characters are represented as one glyph in
1863 the glyph matrix. There are no padding glyphs. */
1864 if (it->charset == CHARSET_COMPOSITION)
1865 {
1866 struct cmpchar_info *cmpcharp;
1867 int idx;
1868
1869 idx = COMPOSITE_CHAR_ID (it->char_to_display);
1870 cmpcharp = cmpchar_table[idx];
1871 it->pixel_width = font->max_bounds.width * cmpcharp->width;
1872
1873 /* There are no padding glyphs, so there is only one glyph
1874 to produce for the composite char. Important is that
1875 pixel_width, ascent and descent are the values of what is
1876 drawn by draw_glyphs. */
1877 it->nglyphs = 1;
1878
1879 /* These settings may not be correct. We must have more
1880 information in cmpcharp to do the correct setting. */
1881 it->ascent = font->ascent;
1882 it->descent = font->descent;
66ac4b0e
GM
1883 it->phys_ascent = font->max_bounds.ascent;
1884 it->phys_descent = font->max_bounds.descent;
06a2c219
GM
1885 }
1886 else
1887 {
1888 /* If we found a font, this font should give us the right
1889 metrics. If we didn't find a font, use the frame's
1890 default font and calculate the width of the character
1891 from the charset width; this is what old redisplay code
1892 did. */
1893 pcm = x_per_char_metric (font, &char2b);
1894 it->pixel_width = pcm->width;
1895 if (font_not_found_p)
1896 it->pixel_width *= CHARSET_WIDTH (it->charset);
1897 it->nglyphs = 1;
1898 it->ascent = font->ascent;
1899 it->descent = font->descent;
66ac4b0e
GM
1900 it->phys_ascent = pcm->ascent;
1901 it->phys_descent = pcm->descent;
06a2c219
GM
1902 if (it->glyph_row
1903 && (pcm->lbearing < 0
1904 || pcm->rbearing > pcm->width))
1905 it->glyph_row->contains_overlapping_glyphs_p = 1;
1906 }
1907
1908 if (face->box != FACE_NO_BOX)
1909 {
1910 int thick = face->box_line_width;
1911 it->ascent += thick;
1912 it->descent += thick;
1913
1914 if (it->start_of_box_run_p)
1915 it->pixel_width += thick;
1916 if (it->end_of_box_run_p)
1917 it->pixel_width += thick;
1918 }
1919
1920 /* If face has an overline, add the height of the overline
1921 (1 pixel) and a 1 pixel margin to the character height. */
1922 if (face->overline_p)
1923 it->ascent += 2;
1924
1925 take_vertical_position_into_account (it);
1926
1927 if (it->glyph_row)
1928 x_append_glyph (it);
1929 }
1930 }
1931 else if (it->what == IT_IMAGE)
1932 x_produce_image_glyph (it);
1933 else if (it->what == IT_STRETCH)
1934 x_produce_stretch_glyph (it);
1935
1936 /* Accumulate dimensions. */
1937 xassert (it->ascent >= 0 && it->descent > 0);
1938 if (it->area == TEXT_AREA)
1939 it->current_x += it->pixel_width;
66ac4b0e 1940
06a2c219
GM
1941 it->max_ascent = max (it->max_ascent, it->ascent);
1942 it->max_descent = max (it->max_descent, it->descent);
66ac4b0e
GM
1943 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
1944 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
06a2c219
GM
1945}
1946
1947
1948/* Estimate the pixel height of the mode or top line on frame F.
1949 FACE_ID specifies what line's height to estimate. */
1950
1951int
1952x_estimate_mode_line_height (f, face_id)
1953 struct frame *f;
1954 enum face_id face_id;
1955{
1956 int height = 1;
1957
1958 /* This function is called so early when Emacs starts that the face
1959 cache and mode line face are not yet initialized. */
1960 if (FRAME_FACE_CACHE (f))
1961 {
1962 struct face *face = FACE_FROM_ID (f, face_id);
1963 if (face)
1964 height = FONT_HEIGHT (face->font) + 2 * face->box_line_width;
1965 }
1966
1967 return height;
1968}
1969
1970\f
1971/***********************************************************************
1972 Glyph display
1973 ***********************************************************************/
1974
1975/* A sequence of glyphs to be drawn in the same face.
1976
1977 This data structure is not really completely X specific, so it
1978 could possibly, at least partially, be useful for other systems. It
1979 is currently not part of the external redisplay interface because
1980 it's not clear what other systems will need. */
1981
1982struct glyph_string
1983{
1984 /* X-origin of the string. */
1985 int x;
1986
1987 /* Y-origin and y-position of the base line of this string. */
1988 int y, ybase;
1989
1990 /* The width of the string, not including a face extension. */
1991 int width;
1992
1993 /* The width of the string, including a face extension. */
1994 int background_width;
1995
1996 /* The height of this string. This is the height of the line this
1997 string is drawn in, and can be different from the height of the
1998 font the string is drawn in. */
1999 int height;
2000
2001 /* Number of pixels this string overwrites in front of its x-origin.
2002 This number is zero if the string has an lbearing >= 0; it is
2003 -lbearing, if the string has an lbearing < 0. */
2004 int left_overhang;
2005
2006 /* Number of pixels this string overwrites past its right-most
2007 nominal x-position, i.e. x + width. Zero if the string's
2008 rbearing is <= its nominal width, rbearing - width otherwise. */
2009 int right_overhang;
2010
2011 /* The frame on which the glyph string is drawn. */
2012 struct frame *f;
2013
2014 /* The window on which the glyph string is drawn. */
2015 struct window *w;
2016
2017 /* X display and window for convenience. */
2018 Display *display;
2019 Window window;
2020
2021 /* The glyph row for which this string was built. It determines the
2022 y-origin and height of the string. */
2023 struct glyph_row *row;
2024
2025 /* The area within row. */
2026 enum glyph_row_area area;
2027
2028 /* Characters to be drawn, and number of characters. */
2029 XChar2b *char2b;
2030 int nchars;
2031
2032 /* Character set of this glyph string. */
2033 int charset;
2034
2035 /* A face-override for drawing cursors, mouse face and similar. */
2036 enum draw_glyphs_face hl;
2037
2038 /* Face in which this string is to be drawn. */
2039 struct face *face;
2040
2041 /* Font in which this string is to be drawn. */
2042 XFontStruct *font;
2043
2044 /* Font info for this string. */
2045 struct font_info *font_info;
2046
2047 /* Non-null means this string describes (part of) a composite
2048 character. All characters from char2b are drawn at the same
2049 x-origin in that case. */
2050 struct cmpchar_info *cmpcharp;
2051
2052 /* Index of this glyph string's first character in the glyph
2053 definition of cmpcharp. If this is zero, this glyph string
2054 describes the first character of a composite character. */
2055 int gidx;
2056
2057 /* 1 means this glyph strings face has to be drawn to the right end
2058 of the window's drawing area. */
2059 unsigned extends_to_end_of_line_p : 1;
2060
2061 /* 1 means the background of this string has been drawn. */
2062 unsigned background_filled_p : 1;
2063
2064 /* 1 means glyph string must be drawn with 16-bit functions. */
2065 unsigned two_byte_p : 1;
2066
2067 /* 1 means that the original font determined for drawing this glyph
2068 string could not be loaded. The member `font' has been set to
2069 the frame's default font in this case. */
2070 unsigned font_not_found_p : 1;
2071
2072 /* 1 means that the face in which this glyph string is drawn has a
2073 stipple pattern. */
2074 unsigned stippled_p : 1;
2075
66ac4b0e
GM
2076 /* 1 means only the foreground of this glyph string must be drawn,
2077 and we should use the physical height of the line this glyph
2078 string appears in as clip rect. */
2079 unsigned for_overlaps_p : 1;
2080
06a2c219
GM
2081 /* The GC to use for drawing this glyph string. */
2082 GC gc;
2083
2084 /* A pointer to the first glyph in the string. This glyph
2085 corresponds to char2b[0]. Needed to draw rectangles if
2086 font_not_found_p is 1. */
2087 struct glyph *first_glyph;
2088
2089 /* Image, if any. */
2090 struct image *img;
2091
2092 struct glyph_string *next, *prev;
2093};
2094
2095
5c187dee 2096#if 0
06a2c219
GM
2097
2098static void
2099x_dump_glyph_string (s)
2100 struct glyph_string *s;
2101{
2102 fprintf (stderr, "glyph string\n");
2103 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2104 s->x, s->y, s->width, s->height);
2105 fprintf (stderr, " ybase = %d\n", s->ybase);
2106 fprintf (stderr, " hl = %d\n", s->hl);
2107 fprintf (stderr, " left overhang = %d, right = %d\n",
2108 s->left_overhang, s->right_overhang);
2109 fprintf (stderr, " nchars = %d\n", s->nchars);
2110 fprintf (stderr, " extends to end of line = %d\n",
2111 s->extends_to_end_of_line_p);
2112 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2113 fprintf (stderr, " bg width = %d\n", s->background_width);
2114}
2115
2116#endif /* GLYPH_DEBUG */
2117
2118
2119
2120static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2121 struct glyph_string **,
2122 struct glyph_string *,
2123 struct glyph_string *));
2124static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2125 struct glyph_string **,
2126 struct glyph_string *,
2127 struct glyph_string *));
2128static void x_append_glyph_string P_ ((struct glyph_string **,
2129 struct glyph_string **,
2130 struct glyph_string *));
2131static int x_left_overwritten P_ ((struct glyph_string *));
2132static int x_left_overwriting P_ ((struct glyph_string *));
2133static int x_right_overwritten P_ ((struct glyph_string *));
2134static int x_right_overwriting P_ ((struct glyph_string *));
66ac4b0e
GM
2135static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2136 int));
06a2c219
GM
2137static void x_init_glyph_string P_ ((struct glyph_string *,
2138 XChar2b *, struct window *,
2139 struct glyph_row *,
2140 enum glyph_row_area, int,
2141 enum draw_glyphs_face));
2142static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2143 enum glyph_row_area, int, int,
66ac4b0e 2144 enum draw_glyphs_face, int *, int *, int));
06a2c219
GM
2145static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2146static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2147static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2148 int));
2149static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
06a2c219
GM
2150static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2151static void x_draw_glyph_string P_ ((struct glyph_string *));
2152static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2153static void x_set_cursor_gc P_ ((struct glyph_string *));
2154static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2155static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2156static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2157 int *, int *));
2158static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2159static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2160 unsigned long *, float, int));
2161static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2162 float, int, unsigned long));
2163static void x_setup_relief_colors P_ ((struct glyph_string *));
2164static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2165static void x_draw_image_relief P_ ((struct glyph_string *));
2166static void x_draw_image_foreground P_ ((struct glyph_string *));
2167static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2168static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2169static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2170 int, int, int));
2171static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2172 int, int, int, int, XRectangle *));
2173static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2174 int, int, int, XRectangle *));
66ac4b0e
GM
2175static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2176 enum glyph_row_area));
06a2c219
GM
2177
2178
06a2c219
GM
2179/* Append the list of glyph strings with head H and tail T to the list
2180 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2181
2182static INLINE void
2183x_append_glyph_string_lists (head, tail, h, t)
2184 struct glyph_string **head, **tail;
2185 struct glyph_string *h, *t;
2186{
2187 if (h)
2188 {
2189 if (*head)
2190 (*tail)->next = h;
2191 else
2192 *head = h;
2193 h->prev = *tail;
2194 *tail = t;
2195 }
2196}
2197
2198
2199/* Prepend the list of glyph strings with head H and tail T to the
2200 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2201 result. */
2202
2203static INLINE void
2204x_prepend_glyph_string_lists (head, tail, h, t)
2205 struct glyph_string **head, **tail;
2206 struct glyph_string *h, *t;
2207{
2208 if (h)
2209 {
2210 if (*head)
2211 (*head)->prev = t;
2212 else
2213 *tail = t;
2214 t->next = *head;
2215 *head = h;
2216 }
2217}
2218
2219
2220/* Append glyph string S to the list with head *HEAD and tail *TAIL.
2221 Set *HEAD and *TAIL to the resulting list. */
2222
2223static INLINE void
2224x_append_glyph_string (head, tail, s)
2225 struct glyph_string **head, **tail;
2226 struct glyph_string *s;
2227{
2228 s->next = s->prev = NULL;
2229 x_append_glyph_string_lists (head, tail, s, s);
2230}
2231
2232
2233/* Set S->gc to a suitable GC for drawing glyph string S in cursor
2234 face. */
2235
2236static void
2237x_set_cursor_gc (s)
2238 struct glyph_string *s;
2239{
2240 if (s->font == FRAME_FONT (s->f)
2241 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2242 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2243 && !s->cmpcharp)
2244 s->gc = s->f->output_data.x->cursor_gc;
2245 else
2246 {
2247 /* Cursor on non-default face: must merge. */
2248 XGCValues xgcv;
2249 unsigned long mask;
2250
2251 xgcv.background = s->f->output_data.x->cursor_pixel;
2252 xgcv.foreground = s->face->background;
2253
2254 /* If the glyph would be invisible, try a different foreground. */
2255 if (xgcv.foreground == xgcv.background)
2256 xgcv.foreground = s->face->foreground;
2257 if (xgcv.foreground == xgcv.background)
2258 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2259 if (xgcv.foreground == xgcv.background)
2260 xgcv.foreground = s->face->foreground;
2261
2262 /* Make sure the cursor is distinct from text in this face. */
2263 if (xgcv.background == s->face->background
2264 && xgcv.foreground == s->face->foreground)
2265 {
2266 xgcv.background = s->face->foreground;
2267 xgcv.foreground = s->face->background;
2268 }
2269
2270 IF_DEBUG (x_check_font (s->f, s->font));
2271 xgcv.font = s->font->fid;
2272 xgcv.graphics_exposures = False;
2273 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2274
2275 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2276 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2277 mask, &xgcv);
2278 else
2279 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2280 = XCreateGC (s->display, s->window, mask, &xgcv);
2281
2282 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2283 }
2284}
2285
2286
2287/* Set up S->gc of glyph string S for drawing text in mouse face. */
2288
2289static void
2290x_set_mouse_face_gc (s)
2291 struct glyph_string *s;
2292{
2293 int face_id;
2294
2295 /* What face has to be used for the mouse face? */
2296 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2297 face_id = FACE_FOR_CHARSET (s->f, face_id, s->charset);
2298 s->face = FACE_FROM_ID (s->f, face_id);
2299 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2300
2301 /* If font in this face is same as S->font, use it. */
2302 if (s->font == s->face->font)
2303 s->gc = s->face->gc;
2304 else
2305 {
2306 /* Otherwise construct scratch_cursor_gc with values from FACE
2307 but font FONT. */
2308 XGCValues xgcv;
2309 unsigned long mask;
2310
2311 xgcv.background = s->face->background;
2312 xgcv.foreground = s->face->foreground;
2313 IF_DEBUG (x_check_font (s->f, s->font));
2314 xgcv.font = s->font->fid;
2315 xgcv.graphics_exposures = False;
2316 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2317
2318 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2319 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2320 mask, &xgcv);
2321 else
2322 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2323 = XCreateGC (s->display, s->window, mask, &xgcv);
2324
2325 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2326 }
2327
2328 xassert (s->gc != 0);
2329}
2330
2331
2332/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2333 Faces to use in the mode line have already been computed when the
2334 matrix was built, so there isn't much to do, here. */
2335
2336static INLINE void
2337x_set_mode_line_face_gc (s)
2338 struct glyph_string *s;
2339{
2340 s->gc = s->face->gc;
2341 xassert (s->gc != 0);
2342}
2343
2344
2345/* Set S->gc of glyph string S for drawing that glyph string. Set
2346 S->stippled_p to a non-zero value if the face of S has a stipple
2347 pattern. */
2348
2349static INLINE void
2350x_set_glyph_string_gc (s)
2351 struct glyph_string *s;
2352{
2353 if (s->hl == DRAW_NORMAL_TEXT)
2354 {
2355 s->gc = s->face->gc;
2356 s->stippled_p = s->face->stipple != 0;
2357 }
2358 else if (s->hl == DRAW_INVERSE_VIDEO)
2359 {
2360 x_set_mode_line_face_gc (s);
2361 s->stippled_p = s->face->stipple != 0;
2362 }
2363 else if (s->hl == DRAW_CURSOR)
2364 {
2365 x_set_cursor_gc (s);
2366 s->stippled_p = 0;
2367 }
2368 else if (s->hl == DRAW_MOUSE_FACE)
2369 {
2370 x_set_mouse_face_gc (s);
2371 s->stippled_p = s->face->stipple != 0;
2372 }
2373 else if (s->hl == DRAW_IMAGE_RAISED
2374 || s->hl == DRAW_IMAGE_SUNKEN)
2375 {
2376 s->gc = s->face->gc;
2377 s->stippled_p = s->face->stipple != 0;
2378 }
2379 else
2380 {
2381 s->gc = s->face->gc;
2382 s->stippled_p = s->face->stipple != 0;
2383 }
2384
2385 /* GC must have been set. */
2386 xassert (s->gc != 0);
2387}
2388
2389
2390/* Return in *R the clipping rectangle for glyph string S. */
2391
2392static void
2393x_get_glyph_string_clip_rect (s, r)
2394 struct glyph_string *s;
2395 XRectangle *r;
2396{
2397 if (s->row->full_width_p)
2398 {
2399 /* Draw full-width. X coordinates are relative to S->w->left. */
1da3fd71
GM
2400 int canon_x = CANON_X_UNIT (s->f);
2401
2402 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2403 r->width = XFASTINT (s->w->width) * canon_x;
06a2c219
GM
2404
2405 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2406 {
1da3fd71 2407 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
06a2c219
GM
2408 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2409 r->x -= width;
2410 }
2411
b9432a85 2412 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
1da3fd71 2413
06a2c219
GM
2414 /* Unless displaying a mode or menu bar line, which are always
2415 fully visible, clip to the visible part of the row. */
2416 if (s->w->pseudo_window_p)
2417 r->height = s->row->visible_height;
2418 else
2419 r->height = s->height;
2420 }
2421 else
2422 {
2423 /* This is a text line that may be partially visible. */
2424 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2425 r->width = window_box_width (s->w, s->area);
2426 r->height = s->row->visible_height;
2427 }
2428
2429 /* Don't use S->y for clipping because it doesn't take partially
2430 visible lines into account. For example, it can be negative for
2431 partially visible lines at the top of a window. */
2432 if (!s->row->full_width_p
2433 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
045dee35 2434 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
06a2c219
GM
2435 else
2436 r->y = max (0, s->row->y);
06a2c219 2437
9ea173e8 2438 /* If drawing a tool-bar window, draw it over the internal border
06a2c219 2439 at the top of the window. */
9ea173e8 2440 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219 2441 r->y -= s->f->output_data.x->internal_border_width;
66ac4b0e
GM
2442
2443 /* If S draws overlapping rows, it's sufficient to use the top and
2444 bottom of the window for clipping because this glyph string
2445 intentionally draws over other lines. */
2446 if (s->for_overlaps_p)
2447 {
045dee35 2448 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
66ac4b0e
GM
2449 r->height = window_text_bottom_y (s->w) - r->y;
2450 }
2451
2452 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
06a2c219
GM
2453}
2454
2455
2456/* Set clipping for output of glyph string S. S may be part of a mode
2457 line or menu if we don't have X toolkit support. */
2458
2459static INLINE void
2460x_set_glyph_string_clipping (s)
2461 struct glyph_string *s;
2462{
2463 XRectangle r;
2464 x_get_glyph_string_clip_rect (s, &r);
2465 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2466}
2467
2468
2469/* Compute left and right overhang of glyph string S. If S is a glyph
2470 string for a composite character, assume overhangs don't exist. */
2471
2472static INLINE void
2473x_compute_glyph_string_overhangs (s)
2474 struct glyph_string *s;
2475{
2476 if (s->cmpcharp == NULL
2477 && s->first_glyph->type == CHAR_GLYPH)
2478 {
2479 XCharStruct cs;
2480 int direction, font_ascent, font_descent;
2481 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2482 &font_ascent, &font_descent, &cs);
2483 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2484 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2485 }
2486}
2487
2488
2489/* Compute overhangs and x-positions for glyph string S and its
2490 predecessors, or successors. X is the starting x-position for S.
2491 BACKWARD_P non-zero means process predecessors. */
2492
2493static void
2494x_compute_overhangs_and_x (s, x, backward_p)
2495 struct glyph_string *s;
2496 int x;
2497 int backward_p;
2498{
2499 if (backward_p)
2500 {
2501 while (s)
2502 {
2503 x_compute_glyph_string_overhangs (s);
2504 x -= s->width;
2505 s->x = x;
2506 s = s->prev;
2507 }
2508 }
2509 else
2510 {
2511 while (s)
2512 {
2513 x_compute_glyph_string_overhangs (s);
2514 s->x = x;
2515 x += s->width;
2516 s = s->next;
2517 }
2518 }
2519}
2520
2521
2522/* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2523 frame F. Overhangs of glyphs other than type CHAR_GLYPH or of
2524 character glyphs for composite characters are assumed to be zero. */
2525
2526static void
2527x_get_glyph_overhangs (glyph, f, left, right)
2528 struct glyph *glyph;
2529 struct frame *f;
2530 int *left, *right;
2531{
2532 int c;
2533
2534 *left = *right = 0;
2535
2536 if (glyph->type == CHAR_GLYPH
2537 && (c = glyph->u.ch.code,
2538 CHAR_CHARSET (c) != CHARSET_COMPOSITION))
2539 {
2540 XFontStruct *font;
2541 struct face *face;
2542 struct font_info *font_info;
2543 XChar2b char2b;
2544
2545 face = x_get_glyph_face_and_encoding (f, glyph, &char2b);
2546 font = face->font;
2547 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2548 if (font)
2549 {
2550 XCharStruct *pcm = x_per_char_metric (font, &char2b);
2551
2552 if (pcm->rbearing > pcm->width)
2553 *right = pcm->rbearing - pcm->width;
2554 if (pcm->lbearing < 0)
2555 *left = -pcm->lbearing;
2556 }
2557 }
2558}
2559
2560
2561/* Return the index of the first glyph preceding glyph string S that
2562 is overwritten by S because of S's left overhang. Value is -1
2563 if no glyphs are overwritten. */
2564
2565static int
2566x_left_overwritten (s)
2567 struct glyph_string *s;
2568{
2569 int k;
2570
2571 if (s->left_overhang)
2572 {
2573 int x = 0, i;
2574 struct glyph *glyphs = s->row->glyphs[s->area];
2575 int first = s->first_glyph - glyphs;
2576
2577 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2578 x -= glyphs[i].pixel_width;
2579
2580 k = i + 1;
2581 }
2582 else
2583 k = -1;
2584
2585 return k;
2586}
2587
2588
2589/* Return the index of the first glyph preceding glyph string S that
2590 is overwriting S because of its right overhang. Value is -1 if no
2591 glyph in front of S overwrites S. */
2592
2593static int
2594x_left_overwriting (s)
2595 struct glyph_string *s;
2596{
2597 int i, k, x;
2598 struct glyph *glyphs = s->row->glyphs[s->area];
2599 int first = s->first_glyph - glyphs;
2600
2601 k = -1;
2602 x = 0;
2603 for (i = first - 1; i >= 0; --i)
2604 {
2605 int left, right;
2606 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2607 if (x + right > 0)
2608 k = i;
2609 x -= glyphs[i].pixel_width;
2610 }
2611
2612 return k;
2613}
2614
2615
2616/* Return the index of the last glyph following glyph string S that is
2617 not overwritten by S because of S's right overhang. Value is -1 if
2618 no such glyph is found. */
2619
2620static int
2621x_right_overwritten (s)
2622 struct glyph_string *s;
2623{
2624 int k = -1;
2625
2626 if (s->right_overhang)
2627 {
2628 int x = 0, i;
2629 struct glyph *glyphs = s->row->glyphs[s->area];
2630 int first = (s->first_glyph - glyphs) + (s->cmpcharp ? 1 : s->nchars);
2631 int end = s->row->used[s->area];
2632
2633 for (i = first; i < end && s->right_overhang > x; ++i)
2634 x += glyphs[i].pixel_width;
2635
2636 k = i;
2637 }
2638
2639 return k;
2640}
2641
2642
2643/* Return the index of the last glyph following glyph string S that
2644 overwrites S because of its left overhang. Value is negative
2645 if no such glyph is found. */
2646
2647static int
2648x_right_overwriting (s)
2649 struct glyph_string *s;
2650{
2651 int i, k, x;
2652 int end = s->row->used[s->area];
2653 struct glyph *glyphs = s->row->glyphs[s->area];
2654 int first = (s->first_glyph - glyphs) + (s->cmpcharp ? 1 : s->nchars);
2655
2656 k = -1;
2657 x = 0;
2658 for (i = first; i < end; ++i)
2659 {
2660 int left, right;
2661 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2662 if (x - left < 0)
2663 k = i;
2664 x += glyphs[i].pixel_width;
2665 }
2666
2667 return k;
2668}
2669
2670
2671/* Fill rectangle X, Y, W, H with background color of glyph string S. */
2672
2673static INLINE void
2674x_clear_glyph_string_rect (s, x, y, w, h)
2675 struct glyph_string *s;
2676 int x, y, w, h;
2677{
2678 XGCValues xgcv;
2679 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
2680 XSetForeground (s->display, s->gc, xgcv.background);
2681 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2682 XSetForeground (s->display, s->gc, xgcv.foreground);
2683}
2684
2685
2686/* Draw the background of glyph_string S. If S->background_filled_p
2687 is non-zero don't draw it. FORCE_P non-zero means draw the
2688 background even if it wouldn't be drawn normally. This is used
2689 when a string preceding S draws into the background of S. */
2690
2691static void
2692x_draw_glyph_string_background (s, force_p)
2693 struct glyph_string *s;
2694 int force_p;
2695{
2696 /* Nothing to do if background has already been drawn or if it
2697 shouldn't be drawn in the first place. */
2698 if (!s->background_filled_p)
2699 {
2700 if (s->cmpcharp
2701 && s->gidx > 0
2702 && !s->font_not_found_p
2703 && !s->extends_to_end_of_line_p)
2704 {
2705 /* Don't draw background for glyphs of a composite
2706 characters, except for the first one. */
2707 s->background_filled_p = 1;
2708 }
2709 else if (s->stippled_p)
2710 {
2711 /* Fill background with a stipple pattern. */
2712 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2713 XFillRectangle (s->display, s->window, s->gc, s->x,
2714 s->y + s->face->box_line_width,
2715 s->background_width,
2716 s->height - 2 * s->face->box_line_width);
2717 XSetFillStyle (s->display, s->gc, FillSolid);
2718 s->background_filled_p = 1;
2719 }
2720 else if (FONT_HEIGHT (s->font) < s->height - 2 * s->face->box_line_width
2721 || s->font_not_found_p
2722 || s->extends_to_end_of_line_p
2723 || s->cmpcharp
2724 || force_p)
2725 {
2726 x_clear_glyph_string_rect (s, s->x, s->y + s->face->box_line_width,
2727 s->background_width,
2728 s->height - 2 * s->face->box_line_width);
2729 s->background_filled_p = 1;
2730 }
2731 }
2732}
2733
2734
2735/* Draw the foreground of glyph string S. */
2736
2737static void
2738x_draw_glyph_string_foreground (s)
2739 struct glyph_string *s;
2740{
2741 int i, x;
2742
2743 /* If first glyph of S has a left box line, start drawing the text
2744 of S to the right of that box line. */
2745 if (s->face->box != FACE_NO_BOX
2746 && s->first_glyph->left_box_line_p)
2747 x = s->x + s->face->box_line_width;
2748 else
2749 x = s->x;
2750
2751 if (s->cmpcharp == NULL)
2752 {
2753 /* Not a composite character. Draw characters of S as
2754 rectangles if S's font could not be loaded. */
2755 if (s->font_not_found_p)
2756 {
2757 for (i = 0; i < s->nchars; ++i)
2758 {
2759 struct glyph *g = s->first_glyph + i;
2760 XDrawRectangle (s->display, s->window,
2761 s->gc, x, s->y, g->pixel_width - 1,
2762 s->height - 1);
2763 x += g->pixel_width;
2764 }
2765 }
2766 else
2767 {
2768 char *char1b = (char *) s->char2b;
2769
2770 /* If we can use 8-bit functions, condense S->char2b. */
2771 if (!s->two_byte_p)
2772 for (i = 0; i < s->nchars; ++i)
2773 char1b[i] = s->char2b[i].byte2;
2774
2775 /* Draw text with XDrawString if background has already been
2776 filled. Otherwise, use XDrawImageString. (Note that
2777 XDrawImageString is usually faster than XDrawString.)
2778 Always use XDrawImageString when drawing the cursor so
2779 that there is no chance that characters under a box
2780 cursor are invisible. */
66ac4b0e
GM
2781 if (s->for_overlaps_p
2782 || (s->background_filled_p && s->hl != DRAW_CURSOR))
06a2c219
GM
2783 {
2784 /* Draw characters with 16-bit or 8-bit functions. */
2785 if (s->two_byte_p)
2786 XDrawString16 (s->display, s->window, s->gc, x, s->ybase,
2787 s->char2b, s->nchars);
2788 else
2789 XDrawString (s->display, s->window, s->gc, x, s->ybase,
2790 char1b, s->nchars);
2791 }
2792 else
2793 {
2794 if (s->two_byte_p)
2795 XDrawImageString16 (s->display, s->window, s->gc,
2796 x, s->ybase, s->char2b, s->nchars);
2797 else
2798 XDrawImageString (s->display, s->window, s->gc,
2799 x, s->ybase, char1b, s->nchars);
2800 }
2801 }
2802 }
2803 else
2804 {
2805 /* S is a glyph string for a composite character. S->gidx is the
2806 index of the first character drawn in the vector
2807 S->cmpcharp->glyph. S->gidx == 0 means we are drawing the
2808 very first component character of a composite char. */
2809
2810 /* Draw a single rectangle for the composite character if S's
2811 font could not be loaded. */
2812 if (s->font_not_found_p && s->gidx == 0)
2813 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
2814 s->width - 1, s->height - 1);
2815 else
2816 {
2817 XCharStruct *pcm;
2818 int relative_compose, default_ascent, i;
2819 int highest = 0, lowest = 0;
2820
2821 /* The value of font_info my be null if we couldn't find it
2822 in x_get_char_face_and_encoding. */
2823 if (s->cmpcharp->cmp_rule == NULL && s->font_info)
2824 {
2825 relative_compose = s->font_info->relative_compose;
2826 default_ascent = s->font_info->default_ascent;
2827 }
2828 else
2829 relative_compose = default_ascent = 0;
2830
2831 if ((s->cmpcharp->cmp_rule || relative_compose)
2832 && s->gidx == 0)
2833 {
2834 /* This is the first character. Initialize variables.
2835 Highest is the highest position of glyphs ever
2836 written, lowest the lowest position. */
2837 int x_offset = 0;
2838 int first_ch = s->first_glyph->u.ch.code;
2839
2840 if (default_ascent
2841 && CHAR_TABLE_P (Vuse_default_ascent)
2842 && !NILP (Faref (Vuse_default_ascent, first_ch)))
2843 {
2844 highest = default_ascent;
2845 lowest = 0;
2846 }
2847 else
2848 {
2849 pcm = PER_CHAR_METRIC (s->font, s->char2b);
2850 highest = pcm->ascent + 1;
2851 lowest = - pcm->descent;
2852 }
2853
2854 if (s->cmpcharp->cmp_rule)
2855 x_offset = (s->cmpcharp->col_offset[0]
2856 * FONT_WIDTH (s->f->output_data.x->font));
2857
2858 /* Draw the first character at the normal position. */
2859 XDrawString16 (s->display, s->window, s->gc,
2860 x + x_offset,
2861 s->ybase, s->char2b, 1);
2862 i = 1;
2863 ++s->gidx;
2864 }
2865 else
2866 i = 0;
2867
2868 for (; i < s->nchars; i++, ++s->gidx)
2869 {
2870 int x_offset = 0, y_offset = 0;
2871
2872 if (relative_compose)
2873 {
2874 pcm = PER_CHAR_METRIC (s->font, s->char2b + i);
2875 if (NILP (Vignore_relative_composition)
2876 || NILP (Faref (Vignore_relative_composition,
2877 make_number (s->cmpcharp->glyph[s->gidx]))))
2878 {
2879 if (- pcm->descent >= relative_compose)
2880 {
2881 /* Draw above the current glyphs. */
2882 y_offset = highest + pcm->descent;
2883 highest += pcm->ascent + pcm->descent;
2884 }
2885 else if (pcm->ascent <= 0)
2886 {
2887 /* Draw beneath the current glyphs. */
2888 y_offset = lowest - pcm->ascent;
2889 lowest -= pcm->ascent + pcm->descent;
2890 }
2891 }
2892 else
2893 {
2894 /* Draw the glyph at normal position. If
2895 it sticks out of HIGHEST or LOWEST,
2896 update them appropriately. */
2897 if (pcm->ascent > highest)
2898 highest = pcm->ascent;
2899 else if (- pcm->descent < lowest)
2900 lowest = - pcm->descent;
2901 }
2902 }
2903 else if (s->cmpcharp->cmp_rule)
2904 {
2905 int gref = (s->cmpcharp->cmp_rule[s->gidx] - 0xA0) / 9;
2906 int nref = (s->cmpcharp->cmp_rule[s->gidx] - 0xA0) % 9;
2907 int bottom, top;
2908
2909 /* Re-encode GREF and NREF so that they specify
2910 only Y-axis information:
2911 0:top, 1:base, 2:bottom, 3:center */
2912 gref = gref / 3 + (gref == 4) * 2;
2913 nref = nref / 3 + (nref == 4) * 2;
2914
2915 pcm = PER_CHAR_METRIC (s->font, s->char2b + i);
2916 bottom = ((gref == 0 ? highest : gref == 1 ? 0
2917 : gref == 2 ? lowest
2918 : (highest + lowest) / 2)
2919 - (nref == 0 ? pcm->ascent + pcm->descent
2920 : nref == 1 ? pcm->descent : nref == 2 ? 0
2921 : (pcm->ascent + pcm->descent) / 2));
2922 top = bottom + (pcm->ascent + pcm->descent);
2923 if (top > highest)
2924 highest = top;
2925 if (bottom < lowest)
2926 lowest = bottom;
2927 y_offset = bottom + pcm->descent;
2928 x_offset = (s->cmpcharp->col_offset[s->gidx]
2929 * FONT_WIDTH (FRAME_FONT (s->f)));
2930 }
2931
2932 XDrawString16 (s->display, s->window, s->gc,
2933 x + x_offset, s->ybase - y_offset,
2934 s->char2b + i, 1);
2935 }
2936 }
2937 }
2938}
2939
2940
80c32bcc
GM
2941#ifdef USE_X_TOOLKIT
2942
2943/* Allocate the color COLOR->pixel on the screen and display of
2944 widget WIDGET in colormap CMAP. If an exact match cannot be
2945 allocated, try the nearest color available. Value is non-zero
2946 if successful. This is called from lwlib. */
2947
2948int
2949x_alloc_nearest_color_for_widget (widget, cmap, color)
2950 Widget widget;
2951 Colormap cmap;
2952 XColor *color;
2953{
2954 struct frame *f;
2955 struct x_display_info *dpyinfo;
5c187dee 2956 Lisp_Object tail;
80c32bcc
GM
2957
2958 dpyinfo = x_display_info_for_display (XtDisplay (widget));
2959
2960 /* Find the top-level shell of the widget. Note that this function
2961 can be called when the widget is not yet realized, so XtWindow
2962 (widget) == 0. That's the reason we can't simply use
2963 x_any_window_to_frame. */
2964 while (!XtIsTopLevelShell (widget))
2965 widget = XtParent (widget);
2966
2967 /* Look for a frame with that top-level widget. Allocate the color
2968 on that frame to get the right gamma correction value. */
2969 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
2970 if (GC_FRAMEP (XCAR (tail))
2971 && (f = XFRAME (XCAR (tail)),
2972 (f->output_data.nothing != 1
2973 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
2974 && f->output_data.x->widget == widget)
2975 return x_alloc_nearest_color (f, cmap, color);
2976
2977 abort ();
2978}
2979
2980#endif /* USE_X_TOOLKIT */
2981
2982
06a2c219
GM
2983/* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
2984 CMAP. If an exact match can't be allocated, try the nearest color
2985 available. Value is non-zero if successful. Set *COLOR to the
2986 color allocated. */
2987
2988int
80c32bcc
GM
2989x_alloc_nearest_color (f, cmap, color)
2990 struct frame *f;
06a2c219
GM
2991 Colormap cmap;
2992 XColor *color;
2993{
80c32bcc
GM
2994 Display *display = FRAME_X_DISPLAY (f);
2995 Screen *screen = FRAME_X_SCREEN (f);
2996 int rc;
2997
2998 gamma_correct (f, color);
2999 rc = XAllocColor (display, cmap, color);
06a2c219
GM
3000 if (rc == 0)
3001 {
3002 /* If we got to this point, the colormap is full, so we're going
3003 to try to get the next closest color. The algorithm used is
3004 a least-squares matching, which is what X uses for closest
3005 color matching with StaticColor visuals. */
3006 int nearest, i;
3007 unsigned long nearest_delta = ~0;
3008 int ncells = XDisplayCells (display, XScreenNumberOfScreen (screen));
3009 XColor *cells = (XColor *) alloca (ncells * sizeof *cells);
3010
3011 for (i = 0; i < ncells; ++i)
3012 cells[i].pixel = i;
3013 XQueryColors (display, cmap, cells, ncells);
3014
3015 for (nearest = i = 0; i < ncells; ++i)
3016 {
3017 long dred = (color->red >> 8) - (cells[i].red >> 8);
3018 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3019 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3020 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3021
3022 if (delta < nearest_delta)
3023 {
3024 nearest = i;
3025 nearest_delta = delta;
3026 }
3027 }
3028
3029 color->red = cells[nearest].red;
3030 color->green = cells[nearest].green;
3031 color->blue = cells[nearest].blue;
3032 rc = XAllocColor (display, cmap, color);
3033 }
3034
3035 return rc;
3036}
3037
3038
3039/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3040 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3041 If this produces the same color as PIXEL, try a color where all RGB
3042 values have DELTA added. Return the allocated color in *PIXEL.
3043 DISPLAY is the X display, CMAP is the colormap to operate on.
3044 Value is non-zero if successful. */
3045
3046static int
3047x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3048 struct frame *f;
3049 Display *display;
3050 Colormap cmap;
3051 unsigned long *pixel;
3052 float factor;
3053 int delta;
3054{
3055 XColor color, new;
3056 int success_p;
3057
3058 /* Get RGB color values. */
3059 color.pixel = *pixel;
3060 XQueryColor (display, cmap, &color);
3061
3062 /* Change RGB values by specified FACTOR. Avoid overflow! */
3063 xassert (factor >= 0);
3064 new.red = min (0xffff, factor * color.red);
3065 new.green = min (0xffff, factor * color.green);
3066 new.blue = min (0xffff, factor * color.blue);
3067
3068 /* Try to allocate the color. */
80c32bcc 3069 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3070 if (success_p)
3071 {
3072 if (new.pixel == *pixel)
3073 {
3074 /* If we end up with the same color as before, try adding
3075 delta to the RGB values. */
3076 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
3077
3078 /* If display has an immutable color map, freeing colors is
3079 not necessary and some servers don't allow it. So don't
3080 do it. */
3081 if (class != StaticColor
3082 && class != StaticGray
3083 && class != TrueColor)
3084 XFreeColors (display, cmap, &new.pixel, 1, 0);
3085
3086 new.red = min (0xffff, delta + color.red);
3087 new.green = min (0xffff, delta + color.green);
3088 new.blue = min (0xffff, delta + color.blue);
80c32bcc 3089 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3090 }
3091 else
3092 success_p = 1;
3093 *pixel = new.pixel;
3094 }
3095
3096 return success_p;
3097}
3098
3099
3100/* Set up the foreground color for drawing relief lines of glyph
3101 string S. RELIEF is a pointer to a struct relief containing the GC
3102 with which lines will be drawn. Use a color that is FACTOR or
3103 DELTA lighter or darker than the relief's background which is found
3104 in S->f->output_data.x->relief_background. If such a color cannot
3105 be allocated, use DEFAULT_PIXEL, instead. */
3106
3107static void
3108x_setup_relief_color (f, relief, factor, delta, default_pixel)
3109 struct frame *f;
3110 struct relief *relief;
3111 float factor;
3112 int delta;
3113 unsigned long default_pixel;
3114{
3115 XGCValues xgcv;
3116 struct x_output *di = f->output_data.x;
3117 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3118 unsigned long pixel;
3119 unsigned long background = di->relief_background;
3120 Colormap cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
dcd08bfb
GM
3121 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3122 Display *dpy = FRAME_X_DISPLAY (f);
06a2c219
GM
3123
3124 xgcv.graphics_exposures = False;
3125 xgcv.line_width = 1;
3126
3127 /* Free previously allocated color. The color cell will be reused
3128 when it has been freed as many times as it was allocated, so this
3129 doesn't affect faces using the same colors. */
3130 if (relief->gc
3131 && relief->allocated_p)
3132 {
3133 /* If display has an immutable color map, freeing colors is not
3134 necessary and some servers don't allow it. So don't do it. */
dcd08bfb 3135 int class = dpyinfo->visual->class;
06a2c219
GM
3136 if (class != StaticColor
3137 && class != StaticGray
3138 && class != TrueColor)
dcd08bfb 3139 XFreeColors (dpy, cmap, &relief->pixel, 1, 0);
06a2c219
GM
3140 relief->allocated_p = 0;
3141 }
3142
3143 /* Allocate new color. */
3144 xgcv.foreground = default_pixel;
3145 pixel = background;
dcd08bfb
GM
3146 if (dpyinfo->n_planes != 1
3147 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
06a2c219
GM
3148 {
3149 relief->allocated_p = 1;
3150 xgcv.foreground = relief->pixel = pixel;
3151 }
3152
3153 if (relief->gc == 0)
3154 {
dcd08bfb 3155 xgcv.stipple = dpyinfo->gray;
06a2c219 3156 mask |= GCStipple;
dcd08bfb 3157 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
06a2c219
GM
3158 }
3159 else
dcd08bfb 3160 XChangeGC (dpy, relief->gc, mask, &xgcv);
06a2c219
GM
3161}
3162
3163
3164/* Set up colors for the relief lines around glyph string S. */
3165
3166static void
3167x_setup_relief_colors (s)
3168 struct glyph_string *s;
3169{
3170 struct x_output *di = s->f->output_data.x;
3171 unsigned long color;
3172
3173 if (s->face->use_box_color_for_shadows_p)
3174 color = s->face->box_color;
3175 else
3176 {
3177 XGCValues xgcv;
3178
3179 /* Get the background color of the face. */
3180 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3181 color = xgcv.background;
3182 }
3183
3184 if (di->white_relief.gc == 0
3185 || color != di->relief_background)
3186 {
3187 di->relief_background = color;
3188 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3189 WHITE_PIX_DEFAULT (s->f));
3190 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3191 BLACK_PIX_DEFAULT (s->f));
3192 }
3193}
3194
3195
3196/* Draw a relief on frame F inside the rectangle given by LEFT_X,
3197 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3198 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3199 relief. LEFT_P non-zero means draw a relief on the left side of
3200 the rectangle. RIGHT_P non-zero means draw a relief on the right
3201 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3202 when drawing. */
3203
3204static void
3205x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3206 raised_p, left_p, right_p, clip_rect)
3207 struct frame *f;
3208 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3209 XRectangle *clip_rect;
3210{
3211 int i;
3212 GC gc;
3213
3214 if (raised_p)
3215 gc = f->output_data.x->white_relief.gc;
3216 else
3217 gc = f->output_data.x->black_relief.gc;
3218 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3219
3220 /* Top. */
3221 for (i = 0; i < width; ++i)
3222 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3223 left_x + i * left_p, top_y + i,
3224 right_x + 1 - i * right_p, top_y + i);
3225
3226 /* Left. */
3227 if (left_p)
3228 for (i = 0; i < width; ++i)
3229 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3230 left_x + i, top_y + i, left_x + i, bottom_y - i);
3231
3232 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3233 if (raised_p)
3234 gc = f->output_data.x->black_relief.gc;
3235 else
3236 gc = f->output_data.x->white_relief.gc;
3237 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3238
3239 /* Bottom. */
3240 for (i = 0; i < width; ++i)
3241 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3242 left_x + i * left_p, bottom_y - i,
3243 right_x + 1 - i * right_p, bottom_y - i);
3244
3245 /* Right. */
3246 if (right_p)
3247 for (i = 0; i < width; ++i)
3248 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3249 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3250
3251 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3252}
3253
3254
3255/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3256 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3257 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3258 left side of the rectangle. RIGHT_P non-zero means draw a line
3259 on the right side of the rectangle. CLIP_RECT is the clipping
3260 rectangle to use when drawing. */
3261
3262static void
3263x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3264 left_p, right_p, clip_rect)
3265 struct glyph_string *s;
3266 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3267 XRectangle *clip_rect;
3268{
3269 XGCValues xgcv;
3270
3271 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3272 XSetForeground (s->display, s->gc, s->face->box_color);
3273 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3274
3275 /* Top. */
3276 XFillRectangle (s->display, s->window, s->gc,
3277 left_x, top_y, right_x - left_x, width);
3278
3279 /* Left. */
3280 if (left_p)
3281 XFillRectangle (s->display, s->window, s->gc,
3282 left_x, top_y, width, bottom_y - top_y);
3283
3284 /* Bottom. */
3285 XFillRectangle (s->display, s->window, s->gc,
3286 left_x, bottom_y - width, right_x - left_x, width);
3287
3288 /* Right. */
3289 if (right_p)
3290 XFillRectangle (s->display, s->window, s->gc,
3291 right_x - width, top_y, width, bottom_y - top_y);
3292
3293 XSetForeground (s->display, s->gc, xgcv.foreground);
3294 XSetClipMask (s->display, s->gc, None);
3295}
3296
3297
3298/* Draw a box around glyph string S. */
3299
3300static void
3301x_draw_glyph_string_box (s)
3302 struct glyph_string *s;
3303{
3304 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3305 int left_p, right_p;
3306 struct glyph *last_glyph;
3307 XRectangle clip_rect;
3308
3309 last_x = window_box_right (s->w, s->area);
3310 if (s->row->full_width_p
3311 && !s->w->pseudo_window_p)
3312 {
110859fc 3313 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
06a2c219
GM
3314 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3315 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3316 }
3317
3318 /* The glyph that may have a right box line. */
3319 last_glyph = (s->cmpcharp || s->img
3320 ? s->first_glyph
3321 : s->first_glyph + s->nchars - 1);
3322
3323 width = s->face->box_line_width;
3324 raised_p = s->face->box == FACE_RAISED_BOX;
3325 left_x = s->x;
3326 right_x = ((s->row->full_width_p
1da3fd71 3327 ? last_x - 1
a7aeb2de 3328 : min (last_x, s->x + s->background_width) - 1));
06a2c219
GM
3329 top_y = s->y;
3330 bottom_y = top_y + s->height - 1;
3331
3332 left_p = (s->first_glyph->left_box_line_p
3333 || (s->hl == DRAW_MOUSE_FACE
3334 && (s->prev == NULL
3335 || s->prev->hl != s->hl)));
3336 right_p = (last_glyph->right_box_line_p
3337 || (s->hl == DRAW_MOUSE_FACE
3338 && (s->next == NULL
3339 || s->next->hl != s->hl)));
3340
3341 x_get_glyph_string_clip_rect (s, &clip_rect);
3342
3343 if (s->face->box == FACE_SIMPLE_BOX)
3344 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3345 left_p, right_p, &clip_rect);
3346 else
3347 {
3348 x_setup_relief_colors (s);
3349 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3350 width, raised_p, left_p, right_p, &clip_rect);
3351 }
3352}
3353
3354
3355/* Draw foreground of image glyph string S. */
3356
3357static void
3358x_draw_image_foreground (s)
3359 struct glyph_string *s;
3360{
3361 int x;
3362 int y = s->ybase - IMAGE_ASCENT (s->img);
3363
3364 /* If first glyph of S has a left box line, start drawing it to the
3365 right of that line. */
3366 if (s->face->box != FACE_NO_BOX
3367 && s->first_glyph->left_box_line_p)
3368 x = s->x + s->face->box_line_width;
3369 else
3370 x = s->x;
3371
3372 /* If there is a margin around the image, adjust x- and y-position
3373 by that margin. */
3374 if (s->img->margin)
3375 {
3376 x += s->img->margin;
3377 y += s->img->margin;
3378 }
3379
3380 if (s->img->pixmap)
3381 {
3382 if (s->img->mask)
3383 {
3384 /* We can't set both a clip mask and use XSetClipRectangles
3385 because the latter also sets a clip mask. We also can't
3386 trust on the shape extension to be available
3387 (XShapeCombineRegion). So, compute the rectangle to draw
3388 manually. */
3389 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3390 | GCFunction);
3391 XGCValues xgcv;
3392 XRectangle clip_rect, image_rect, r;
3393
3394 xgcv.clip_mask = s->img->mask;
3395 xgcv.clip_x_origin = x;
3396 xgcv.clip_y_origin = y;
3397 xgcv.function = GXcopy;
3398 XChangeGC (s->display, s->gc, mask, &xgcv);
3399
3400 x_get_glyph_string_clip_rect (s, &clip_rect);
3401 image_rect.x = x;
3402 image_rect.y = y;
3403 image_rect.width = s->img->width;
3404 image_rect.height = s->img->height;
3405 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3406 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3407 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3408 }
3409 else
3410 {
3411 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3412 0, 0, s->img->width, s->img->height, x, y);
3413
3414 /* When the image has a mask, we can expect that at
3415 least part of a mouse highlight or a block cursor will
3416 be visible. If the image doesn't have a mask, make
3417 a block cursor visible by drawing a rectangle around
3418 the image. I believe it's looking better if we do
3419 nothing here for mouse-face. */
3420 if (s->hl == DRAW_CURSOR)
3421 XDrawRectangle (s->display, s->window, s->gc, x, y,
3422 s->img->width - 1, s->img->height - 1);
3423 }
3424 }
3425 else
3426 /* Draw a rectangle if image could not be loaded. */
3427 XDrawRectangle (s->display, s->window, s->gc, x, y,
3428 s->img->width - 1, s->img->height - 1);
3429}
3430
3431
3432/* Draw a relief around the image glyph string S. */
3433
3434static void
3435x_draw_image_relief (s)
3436 struct glyph_string *s;
3437{
3438 int x0, y0, x1, y1, thick, raised_p;
3439 XRectangle r;
3440 int x;
3441 int y = s->ybase - IMAGE_ASCENT (s->img);
3442
3443 /* If first glyph of S has a left box line, start drawing it to the
3444 right of that line. */
3445 if (s->face->box != FACE_NO_BOX
3446 && s->first_glyph->left_box_line_p)
3447 x = s->x + s->face->box_line_width;
3448 else
3449 x = s->x;
3450
3451 /* If there is a margin around the image, adjust x- and y-position
3452 by that margin. */
3453 if (s->img->margin)
3454 {
3455 x += s->img->margin;
3456 y += s->img->margin;
3457 }
3458
3459 if (s->hl == DRAW_IMAGE_SUNKEN
3460 || s->hl == DRAW_IMAGE_RAISED)
3461 {
9ea173e8 3462 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
06a2c219
GM
3463 raised_p = s->hl == DRAW_IMAGE_RAISED;
3464 }
3465 else
3466 {
3467 thick = abs (s->img->relief);
3468 raised_p = s->img->relief > 0;
3469 }
3470
3471 x0 = x - thick;
3472 y0 = y - thick;
3473 x1 = x + s->img->width + thick - 1;
3474 y1 = y + s->img->height + thick - 1;
3475
3476 x_setup_relief_colors (s);
3477 x_get_glyph_string_clip_rect (s, &r);
3478 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3479}
3480
3481
3482/* Draw the foreground of image glyph string S to PIXMAP. */
3483
3484static void
3485x_draw_image_foreground_1 (s, pixmap)
3486 struct glyph_string *s;
3487 Pixmap pixmap;
3488{
3489 int x;
3490 int y = s->ybase - s->y - IMAGE_ASCENT (s->img);
3491
3492 /* If first glyph of S has a left box line, start drawing it to the
3493 right of that line. */
3494 if (s->face->box != FACE_NO_BOX
3495 && s->first_glyph->left_box_line_p)
3496 x = s->face->box_line_width;
3497 else
3498 x = 0;
3499
3500 /* If there is a margin around the image, adjust x- and y-position
3501 by that margin. */
3502 if (s->img->margin)
3503 {
3504 x += s->img->margin;
3505 y += s->img->margin;
3506 }
dc43ef94 3507
06a2c219
GM
3508 if (s->img->pixmap)
3509 {
3510 if (s->img->mask)
3511 {
3512 /* We can't set both a clip mask and use XSetClipRectangles
3513 because the latter also sets a clip mask. We also can't
3514 trust on the shape extension to be available
3515 (XShapeCombineRegion). So, compute the rectangle to draw
3516 manually. */
3517 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3518 | GCFunction);
3519 XGCValues xgcv;
3520
3521 xgcv.clip_mask = s->img->mask;
3522 xgcv.clip_x_origin = x;
3523 xgcv.clip_y_origin = y;
3524 xgcv.function = GXcopy;
3525 XChangeGC (s->display, s->gc, mask, &xgcv);
3526
3527 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3528 0, 0, s->img->width, s->img->height, x, y);
3529 XSetClipMask (s->display, s->gc, None);
3530 }
3531 else
3532 {
3533 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3534 0, 0, s->img->width, s->img->height, x, y);
3535
3536 /* When the image has a mask, we can expect that at
3537 least part of a mouse highlight or a block cursor will
3538 be visible. If the image doesn't have a mask, make
3539 a block cursor visible by drawing a rectangle around
3540 the image. I believe it's looking better if we do
3541 nothing here for mouse-face. */
3542 if (s->hl == DRAW_CURSOR)
3543 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3544 s->img->width - 1, s->img->height - 1);
3545 }
3546 }
3547 else
3548 /* Draw a rectangle if image could not be loaded. */
3549 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3550 s->img->width - 1, s->img->height - 1);
3551}
dc43ef94 3552
990ba854 3553
06a2c219
GM
3554/* Draw part of the background of glyph string S. X, Y, W, and H
3555 give the rectangle to draw. */
a9a5b0a5 3556
06a2c219
GM
3557static void
3558x_draw_glyph_string_bg_rect (s, x, y, w, h)
3559 struct glyph_string *s;
3560 int x, y, w, h;
3561{
3562 if (s->stippled_p)
3563 {
3564 /* Fill background with a stipple pattern. */
3565 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3566 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3567 XSetFillStyle (s->display, s->gc, FillSolid);
3568 }
3569 else
3570 x_clear_glyph_string_rect (s, x, y, w, h);
3571}
07e34cb0 3572
b5210ea7 3573
06a2c219 3574/* Draw image glyph string S.
dc43ef94 3575
06a2c219
GM
3576 s->y
3577 s->x +-------------------------
3578 | s->face->box
3579 |
3580 | +-------------------------
3581 | | s->img->margin
3582 | |
3583 | | +-------------------
3584 | | | the image
dc43ef94 3585
06a2c219 3586 */
dc43ef94 3587
06a2c219
GM
3588static void
3589x_draw_image_glyph_string (s)
3590 struct glyph_string *s;
3591{
3592 int x, y;
3593 int box_line_width = s->face->box_line_width;
3594 int margin = s->img->margin;
3595 int height;
3596 Pixmap pixmap = None;
3597
3598 height = s->height - 2 * box_line_width;
3599
3600 /* Fill background with face under the image. Do it only if row is
3601 taller than image or if image has a clip mask to reduce
3602 flickering. */
3603 s->stippled_p = s->face->stipple != 0;
3604 if (height > s->img->height
3605 || margin
3606 || s->img->mask
3607 || s->img->pixmap == 0
3608 || s->width != s->background_width)
3609 {
3610 if (box_line_width && s->first_glyph->left_box_line_p)
3611 x = s->x + box_line_width;
3612 else
3613 x = s->x;
3614
3615 y = s->y + box_line_width;
3616
3617 if (s->img->mask)
3618 {
3619 /* Create a pixmap as large as the glyph string Fill it with
3620 the background color. Copy the image to it, using its
3621 mask. Copy the temporary pixmap to the display. */
3622 Screen *screen = FRAME_X_SCREEN (s->f);
3623 int depth = DefaultDepthOfScreen (screen);
3624
3625 /* Create a pixmap as large as the glyph string. */
3626 pixmap = XCreatePixmap (s->display, s->window,
3627 s->background_width,
3628 s->height, depth);
3629
3630 /* Don't clip in the following because we're working on the
3631 pixmap. */
3632 XSetClipMask (s->display, s->gc, None);
3633
3634 /* Fill the pixmap with the background color/stipple. */
3635 if (s->stippled_p)
3636 {
3637 /* Fill background with a stipple pattern. */
3638 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3639 XFillRectangle (s->display, pixmap, s->gc,
3640 0, 0, s->background_width, s->height);
3641 XSetFillStyle (s->display, s->gc, FillSolid);
3642 }
3643 else
3644 {
3645 XGCValues xgcv;
3646 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3647 &xgcv);
3648 XSetForeground (s->display, s->gc, xgcv.background);
3649 XFillRectangle (s->display, pixmap, s->gc,
3650 0, 0, s->background_width, s->height);
3651 XSetForeground (s->display, s->gc, xgcv.foreground);
3652 }
3653 }
3654 else
3655 /* Implementation idea: Is it possible to construct a mask?
3656 We could look at the color at the margins of the image, and
3657 say that this color is probably the background color of the
3658 image. */
3659 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
3660
3661 s->background_filled_p = 1;
3662 }
dc43ef94 3663
06a2c219
GM
3664 /* Draw the foreground. */
3665 if (pixmap != None)
3666 {
3667 x_draw_image_foreground_1 (s, pixmap);
3668 x_set_glyph_string_clipping (s);
3669 XCopyArea (s->display, pixmap, s->window, s->gc,
3670 0, 0, s->background_width, s->height, s->x, s->y);
3671 XFreePixmap (s->display, pixmap);
3672 }
3673 else
3674 x_draw_image_foreground (s);
b5210ea7 3675
06a2c219
GM
3676 /* If we must draw a relief around the image, do it. */
3677 if (s->img->relief
3678 || s->hl == DRAW_IMAGE_RAISED
3679 || s->hl == DRAW_IMAGE_SUNKEN)
3680 x_draw_image_relief (s);
3681}
8c1a6a84 3682
990ba854 3683
06a2c219 3684/* Draw stretch glyph string S. */
dc43ef94 3685
06a2c219
GM
3686static void
3687x_draw_stretch_glyph_string (s)
3688 struct glyph_string *s;
3689{
3690 xassert (s->first_glyph->type == STRETCH_GLYPH);
3691 s->stippled_p = s->face->stipple != 0;
990ba854 3692
06a2c219
GM
3693 if (s->hl == DRAW_CURSOR
3694 && !x_stretch_cursor_p)
3695 {
3696 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
3697 as wide as the stretch glyph. */
3698 int width = min (CANON_X_UNIT (s->f), s->background_width);
990ba854 3699
06a2c219
GM
3700 /* Draw cursor. */
3701 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
0cdd0c9f 3702
06a2c219
GM
3703 /* Clear rest using the GC of the original non-cursor face. */
3704 if (width < s->background_width)
3705 {
3706 GC gc = s->face->gc;
3707 int x = s->x + width, y = s->y;
3708 int w = s->background_width - width, h = s->height;
3709 XRectangle r;
dc43ef94 3710
06a2c219
GM
3711 x_get_glyph_string_clip_rect (s, &r);
3712 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
97210f4e 3713
06a2c219
GM
3714 if (s->face->stipple)
3715 {
3716 /* Fill background with a stipple pattern. */
3717 XSetFillStyle (s->display, gc, FillOpaqueStippled);
3718 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3719 XSetFillStyle (s->display, gc, FillSolid);
3720 }
3721 else
3722 {
3723 XGCValues xgcv;
3724 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
3725 XSetForeground (s->display, gc, xgcv.background);
3726 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3727 XSetForeground (s->display, gc, xgcv.foreground);
3728 }
3729 }
3730 }
3731 else
3732 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
3733 s->height);
3734
3735 s->background_filled_p = 1;
3736}
3737
3738
3739/* Draw glyph string S. */
3740
3741static void
3742x_draw_glyph_string (s)
3743 struct glyph_string *s;
3744{
3745 /* If S draws into the background of its successor, draw the
3746 background of the successor first so that S can draw into it.
3747 This makes S->next use XDrawString instead of XDrawImageString. */
66ac4b0e 3748 if (s->next && s->right_overhang && !s->for_overlaps_p)
06a2c219
GM
3749 {
3750 xassert (s->next->img == NULL);
3751 x_set_glyph_string_gc (s->next);
3752 x_set_glyph_string_clipping (s->next);
3753 x_draw_glyph_string_background (s->next, 1);
3754 }
97210f4e 3755
06a2c219
GM
3756 /* Set up S->gc, set clipping and draw S. */
3757 x_set_glyph_string_gc (s);
3758 x_set_glyph_string_clipping (s);
3759
3760 switch (s->first_glyph->type)
3761 {
3762 case IMAGE_GLYPH:
3763 x_draw_image_glyph_string (s);
3764 break;
3765
3766 case STRETCH_GLYPH:
3767 x_draw_stretch_glyph_string (s);
3768 break;
3769
3770 case CHAR_GLYPH:
66ac4b0e
GM
3771 if (s->for_overlaps_p)
3772 s->background_filled_p = 1;
3773 else
3774 x_draw_glyph_string_background (s, 0);
06a2c219
GM
3775 x_draw_glyph_string_foreground (s);
3776 break;
3777
3778 default:
3779 abort ();
3780 }
3781
66ac4b0e 3782 if (!s->for_overlaps_p)
06a2c219 3783 {
66ac4b0e
GM
3784 /* Draw underline. */
3785 if (s->face->underline_p)
3786 {
3787 unsigned long dy, h;
06a2c219 3788
66ac4b0e
GM
3789 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
3790 h = 1;
3791 if (!XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &dy))
3792 dy = s->height - h;
06a2c219 3793
66ac4b0e
GM
3794 if (s->face->underline_defaulted_p)
3795 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3796 s->width, h);
3797 else
3798 {
3799 XGCValues xgcv;
3800 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3801 XSetForeground (s->display, s->gc, s->face->underline_color);
3802 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3803 s->width, h);
3804 XSetForeground (s->display, s->gc, xgcv.foreground);
3805 }
dc6f92b8 3806 }
07e34cb0 3807
66ac4b0e
GM
3808 /* Draw overline. */
3809 if (s->face->overline_p)
06a2c219 3810 {
66ac4b0e
GM
3811 unsigned long dy = 0, h = 1;
3812
3813 if (s->face->overline_color_defaulted_p)
3814 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3815 s->width, h);
3816 else
3817 {
3818 XGCValues xgcv;
3819 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3820 XSetForeground (s->display, s->gc, s->face->overline_color);
3821 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3822 s->width, h);
3823 XSetForeground (s->display, s->gc, xgcv.foreground);
3824 }
06a2c219 3825 }
06a2c219 3826
66ac4b0e
GM
3827 /* Draw strike-through. */
3828 if (s->face->strike_through_p)
06a2c219 3829 {
66ac4b0e
GM
3830 unsigned long h = 1;
3831 unsigned long dy = (s->height - h) / 2;
3832
3833 if (s->face->strike_through_color_defaulted_p)
3834 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3835 s->width, h);
3836 else
3837 {
3838 XGCValues xgcv;
3839 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3840 XSetForeground (s->display, s->gc, s->face->strike_through_color);
3841 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3842 s->width, h);
3843 XSetForeground (s->display, s->gc, xgcv.foreground);
3844 }
06a2c219 3845 }
06a2c219 3846
66ac4b0e
GM
3847 /* Draw relief. */
3848 if (s->face->box != FACE_NO_BOX)
3849 x_draw_glyph_string_box (s);
3850 }
06a2c219
GM
3851
3852 /* Reset clipping. */
3853 XSetClipMask (s->display, s->gc, None);
dc6f92b8 3854}
07e34cb0 3855
06a2c219
GM
3856
3857/* A work-list entry used during the construction of glyph_string
3858 structures for a composite character. */
3859
3860struct work
3861{
3862 /* Pointer to composite char info defining has the composite
3863 character is drawn. */
3864 struct cmpchar_info *cmpcharp;
3865
3866 /* Start index in compcharp->glyph[]. */
3867 int gidx;
3868
3869 /* Next in stack. */
3870 struct work *next;
3871};
3872
3873
3874static void x_fill_composite_glyph_string P_ ((struct glyph_string *,
3875 int, struct work **,
66ac4b0e 3876 struct work **, int));
06a2c219
GM
3877
3878
3879/* Load glyph string S with information from the top of *STACK for a
3880 composite character. FACE_ID is the id of the face in which S is
3881 drawn. *NEW is a pointer to a struct work not on the stack, that
3882 can be used if this function needs to push a new structure on the
66ac4b0e
GM
3883 stack. If it uses it, *NEW is set to null. OVERLAPS_P non-zero
3884 means S should draw the foreground only, and use its lines physical
3885 height for clipping. */
07e34cb0
JB
3886
3887static void
66ac4b0e 3888x_fill_composite_glyph_string (s, face_id, stack, new, overlaps_p)
06a2c219
GM
3889 struct glyph_string *s;
3890 int face_id;
3891 struct work **stack, **new;
66ac4b0e 3892 int overlaps_p;
07e34cb0 3893{
06a2c219
GM
3894 int i, c;
3895 struct work *work;
3896
3897 xassert (s && *new && *stack);
3898
66ac4b0e
GM
3899 s->for_overlaps_p = 1;
3900
06a2c219
GM
3901 /* Pop the work stack. */
3902 work = *stack;
3903 *stack = work->next;
3904
3905 /* For all glyphs of cmpcharp->glyph, starting at the offset
3906 work->offset, until we reach the end of the definition or
3907 encounter another composite char, get the font and face to use,
3908 and add it to S. */
3909 for (i = work->gidx; i < work->cmpcharp->glyph_len; ++i)
3910 {
3911 c = FAST_GLYPH_CHAR (work->cmpcharp->glyph[i]);
3912 if (CHAR_CHARSET (c) == CHARSET_COMPOSITION)
3913 break;
3914 s->face = x_get_char_face_and_encoding (s->f, c, face_id,
3915 s->char2b + s->nchars, 1);
3916 s->font = s->face->font;
3917 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
3918 ++s->nchars;
3919 }
3920
3921 /* If we find another composite char in the glyph definition of
3922 work->cmpcharp, put back the rest of the glyphs on the work
3923 stack, and make a new entry for the composite char. */
3924 if (i < work->cmpcharp->glyph_len)
3925 {
3926 /* Push back an unprocessed rest of this glyph spec. */
3927 if (i < work->cmpcharp->glyph_len - 1)
3928 {
3929 work->gidx = i + 1;
3930 work->next = *stack;
3931 *stack = work;
3932 work = *new;
3933 *new = NULL;
3934 }
3935
3936 /* Make an entry for the composite char on the work stack. */
3937 work->cmpcharp = cmpchar_table[COMPOSITE_CHAR_ID (c)];
3938 work->gidx = 0;
3939 work->next = *stack;
3940 *stack = work;
3941 }
3942
3943 /* The width of this glyph string equals the width of the first
3944 glyph. All characters are drawn at the same x-position. */
3945 s->width = s->first_glyph->pixel_width;
3946
3947 /* If the specified font could not be loaded, use the frame's
3948 default font, but record the fact that we couldn't load it in
3949 the glyph string so that we can draw rectangles for the
3950 characters of the glyph string. */
3951 if (s->font == NULL)
3952 {
3953 s->font_not_found_p = 1;
3954 s->font = FRAME_FONT (s->f);
3955 }
3956
3957 /* Adjust base line for subscript/superscript text. */
3958 s->ybase += s->first_glyph->voffset;
3959
3960 xassert (s->face && s->face->gc);
3961
3962 /* This glyph string must always be drawn with 16-bit functions. */
3963 s->two_byte_p = 1;
3964}
3965
3966
3967/* Load glyph string S with a sequence of non-composite characters.
3968 FACE_ID is the face id of the string. START is the index of the
66ac4b0e
GM
3969 first glyph to consider, END is the index of the last + 1.
3970 OVERLAPS_P non-zero means S should draw the foreground only, and
3971 use its lines physical height for clipping.
3972
3973 Value is the index of the first glyph not in S. */
06a2c219
GM
3974
3975static int
66ac4b0e 3976x_fill_glyph_string (s, face_id, start, end, overlaps_p)
06a2c219
GM
3977 struct glyph_string *s;
3978 int face_id;
66ac4b0e 3979 int start, end, overlaps_p;
06a2c219
GM
3980{
3981 struct glyph *glyph, *last;
3982 int voffset;
3983
3984 xassert (s->charset != CHARSET_COMPOSITION);
3985 xassert (s->f == XFRAME (s->w->frame));
3986 xassert (s->nchars == 0);
3987 xassert (start >= 0 && end > start);
3988
66ac4b0e 3989 s->for_overlaps_p = overlaps_p,
06a2c219
GM
3990 glyph = s->row->glyphs[s->area] + start;
3991 last = s->row->glyphs[s->area] + end;
3992 voffset = glyph->voffset;
3993
3994 while (glyph < last
3995 && glyph->type == CHAR_GLYPH
3996 && glyph->voffset == voffset
3997 /* Same face id implies same charset, nowadays. */
3998 && glyph->u.ch.face_id == face_id)
3999 {
4000 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4001 s->char2b + s->nchars);
4002 if (s->char2b[s->nchars].byte2 != 0)
4003 s->two_byte_p = 1;
4004
4005 ++s->nchars;
4006 xassert (s->nchars <= end - start);
4007 s->width += glyph->pixel_width;
4008 ++glyph;
4009 }
4010
4011 s->font = s->face->font;
4012 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4013
4014 /* If the specified font could not be loaded, use the frame's font,
4015 but record the fact that we couldn't load it in
4016 S->font_not_found_p so that we can draw rectangles for the
4017 characters of the glyph string. */
4018 if (s->font == NULL)
4019 {
4020 s->font_not_found_p = 1;
4021 s->font = FRAME_FONT (s->f);
4022 }
4023
4024 /* Adjust base line for subscript/superscript text. */
4025 s->ybase += voffset;
66ac4b0e 4026
06a2c219
GM
4027 xassert (s->face && s->face->gc);
4028 return glyph - s->row->glyphs[s->area];
07e34cb0 4029}
dc6f92b8 4030
06a2c219
GM
4031
4032/* Fill glyph string S from image glyph S->first_glyph. */
dc6f92b8 4033
dfcf069d 4034static void
06a2c219
GM
4035x_fill_image_glyph_string (s)
4036 struct glyph_string *s;
4037{
4038 xassert (s->first_glyph->type == IMAGE_GLYPH);
4039 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img.id);
4040 xassert (s->img);
4041 s->face = FACE_FROM_ID (s->f, s->first_glyph->u.img.face_id);
4042 s->font = s->face->font;
4043 s->width = s->first_glyph->pixel_width;
4044
4045 /* Adjust base line for subscript/superscript text. */
4046 s->ybase += s->first_glyph->voffset;
4047}
4048
4049
4050/* Fill glyph string S from stretch glyph S->first_glyph. */
4051
4052static void
4053x_fill_stretch_glyph_string (s)
4054 struct glyph_string *s;
4055{
4056 xassert (s->first_glyph->type == STRETCH_GLYPH);
4057 s->face = FACE_FROM_ID (s->f, s->first_glyph->u.stretch.face_id);
4058 s->font = s->face->font;
4059 s->width = s->first_glyph->pixel_width;
4060
4061 /* Adjust base line for subscript/superscript text. */
4062 s->ybase += s->first_glyph->voffset;
4063}
4064
4065
4066/* Initialize glyph string S. CHAR2B is a suitably allocated vector
4067 of XChar2b structures for S; it can't be allocated in
4068 x_init_glyph_string because it must be allocated via `alloca'. W
4069 is the window on which S is drawn. ROW and AREA are the glyph row
4070 and area within the row from which S is constructed. START is the
4071 index of the first glyph structure covered by S. HL is a
4072 face-override for drawing S. */
4073
4074static void
4075x_init_glyph_string (s, char2b, w, row, area, start, hl)
4076 struct glyph_string *s;
4077 XChar2b *char2b;
4078 struct window *w;
4079 struct glyph_row *row;
4080 enum glyph_row_area area;
4081 int start;
4082 enum draw_glyphs_face hl;
4083{
4084 bzero (s, sizeof *s);
4085 s->w = w;
4086 s->f = XFRAME (w->frame);
4087 s->display = FRAME_X_DISPLAY (s->f);
4088 s->window = FRAME_X_WINDOW (s->f);
4089 s->char2b = char2b;
4090 s->hl = hl;
4091 s->row = row;
4092 s->area = area;
4093 s->first_glyph = row->glyphs[area] + start;
4094 s->height = row->height;
4095 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4096
9ea173e8
GM
4097 /* Display the internal border below the tool-bar window. */
4098 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219
GM
4099 s->y -= s->f->output_data.x->internal_border_width;
4100
4101 s->ybase = s->y + row->ascent;
4102}
4103
4104
4105/* Set background width of glyph string S. START is the index of the
4106 first glyph following S. LAST_X is the right-most x-position + 1
4107 in the drawing area. */
4108
4109static INLINE void
4110x_set_glyph_string_background_width (s, start, last_x)
4111 struct glyph_string *s;
4112 int start;
4113 int last_x;
4114{
4115 /* If the face of this glyph string has to be drawn to the end of
4116 the drawing area, set S->extends_to_end_of_line_p. */
4117 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4118
4119 if (start == s->row->used[s->area]
4120 && s->hl == DRAW_NORMAL_TEXT
4121 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4122 || s->face->background != default_face->background
4123 || s->face->stipple != default_face->stipple))
4124 s->extends_to_end_of_line_p = 1;
4125
4126 /* If S extends its face to the end of the line, set its
4127 background_width to the distance to the right edge of the drawing
4128 area. */
4129 if (s->extends_to_end_of_line_p)
1da3fd71 4130 s->background_width = last_x - s->x + 1;
06a2c219
GM
4131 else
4132 s->background_width = s->width;
4133}
4134
4135
4136/* Add a glyph string for a stretch glyph to the list of strings
4137 between HEAD and TAIL. START is the index of the stretch glyph in
4138 row area AREA of glyph row ROW. END is the index of the last glyph
4139 in that glyph row area. X is the current output position assigned
4140 to the new glyph string constructed. HL overrides that face of the
4141 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4142 is the right-most x-position of the drawing area. */
4143
8abee2e1
DL
4144/* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4145 and below -- keep them on one line. */
4146#define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4147 do \
4148 { \
4149 s = (struct glyph_string *) alloca (sizeof *s); \
4150 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4151 x_fill_stretch_glyph_string (s); \
4152 x_append_glyph_string (&HEAD, &TAIL, s); \
4153 ++START; \
4154 s->x = (X); \
4155 } \
4156 while (0)
4157
4158
4159/* Add a glyph string for an image glyph to the list of strings
4160 between HEAD and TAIL. START is the index of the image glyph in
4161 row area AREA of glyph row ROW. END is the index of the last glyph
4162 in that glyph row area. X is the current output position assigned
4163 to the new glyph string constructed. HL overrides that face of the
4164 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4165 is the right-most x-position of the drawing area. */
4166
8abee2e1 4167#define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4168 do \
4169 { \
4170 s = (struct glyph_string *) alloca (sizeof *s); \
4171 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4172 x_fill_image_glyph_string (s); \
4173 x_append_glyph_string (&HEAD, &TAIL, s); \
4174 ++START; \
4175 s->x = (X); \
4176 } \
4177 while (0)
4178
4179
4180/* Add a glyph string for a sequence of character glyphs to the list
4181 of strings between HEAD and TAIL. START is the index of the first
4182 glyph in row area AREA of glyph row ROW that is part of the new
4183 glyph string. END is the index of the last glyph in that glyph row
4184 area. X is the current output position assigned to the new glyph
4185 string constructed. HL overrides that face of the glyph; e.g. it
4186 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4187 right-most x-position of the drawing area. */
4188
8abee2e1 4189#define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4190 do \
4191 { \
4192 int c, charset, face_id; \
4193 XChar2b *char2b; \
4194 \
4195 c = (ROW)->glyphs[AREA][START].u.ch.code; \
4196 charset = CHAR_CHARSET (c); \
4197 face_id = (ROW)->glyphs[AREA][START].u.ch.face_id; \
4198 \
4199 if (charset == CHARSET_COMPOSITION) \
4200 { \
4201 struct work *stack, *work, *new = NULL; \
4202 int n = 0; \
4203 struct glyph_string *first_s = NULL; \
4204 \
4205 /* Push an initial entry for character c on the stack. */ \
4206 stack = NULL; \
4207 work = (struct work *) alloca (sizeof *work); \
4208 work->cmpcharp = cmpchar_table[COMPOSITE_CHAR_ID (c)]; \
4209 work->gidx = 0; \
4210 work->next = stack; \
4211 stack = work; \
4212 \
4213 /* While the stack is not empty, append glyph_strings \
4214 to head/tail for glyphs to draw. */ \
4215 while (stack) \
4216 { \
4217 s = (struct glyph_string *) alloca (sizeof *s); \
4218 char2b = (XChar2b *) alloca (stack->cmpcharp->glyph_len \
4219 * sizeof (XChar2b)); \
4220 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4221 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4222 s->cmpcharp = stack->cmpcharp; \
4223 s->gidx = stack->gidx; \
4224 s->charset = charset; \
4225 s->x = (X); \
4226 \
4227 if (n == 0) \
4228 { \
4229 /* Don't draw the background except for the \
4230 first glyph string. */ \
4231 s->background_filled_p = n > 0; \
4232 first_s = s; \
4233 } \
4234 ++n; \
4235 \
4236 if (new == NULL) \
4237 new = (struct work *) alloca (sizeof *new); \
4238 x_fill_composite_glyph_string (s, face_id, &stack, \
66ac4b0e 4239 &new, OVERLAPS_P); \
06a2c219
GM
4240 } \
4241 \
4242 ++START; \
4243 s = first_s; \
4244 } \
4245 else \
4246 { \
4247 s = (struct glyph_string *) alloca (sizeof *s); \
4248 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4249 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4250 x_append_glyph_string (&HEAD, &TAIL, s); \
4251 s->charset = charset; \
4252 s->x = (X); \
66ac4b0e
GM
4253 START = x_fill_glyph_string (s, face_id, START, END, \
4254 OVERLAPS_P); \
06a2c219
GM
4255 } \
4256 } \
4257 while (0)
4258
4259
4260/* Build a list of glyph strings between HEAD and TAIL for the glyphs
4261 of AREA of glyph row ROW on window W between indices START and END.
4262 HL overrides the face for drawing glyph strings, e.g. it is
4263 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4264 x-positions of the drawing area.
4265
4266 This is an ugly monster macro construct because we must use alloca
4267 to allocate glyph strings (because x_draw_glyphs can be called
4268 asynchronously). */
4269
8abee2e1 4270#define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4271 do \
4272 { \
4273 HEAD = TAIL = NULL; \
4274 while (START < END) \
4275 { \
4276 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4277 switch (first_glyph->type) \
4278 { \
4279 case CHAR_GLYPH: \
4280 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
66ac4b0e
GM
4281 TAIL, HL, X, LAST_X, \
4282 OVERLAPS_P); \
06a2c219
GM
4283 break; \
4284 \
4285 case STRETCH_GLYPH: \
4286 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4287 HEAD, TAIL, HL, X, LAST_X); \
4288 break; \
4289 \
4290 case IMAGE_GLYPH: \
4291 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4292 TAIL, HL, X, LAST_X); \
4293 break; \
4294 \
4295 default: \
4296 abort (); \
4297 } \
4298 \
4299 x_set_glyph_string_background_width (s, START, LAST_X); \
4300 (X) += s->width; \
4301 } \
4302 } \
4303 while (0)
4304
4305
4306/* Draw glyphs between START and END in AREA of ROW on window W,
4307 starting at x-position X. X is relative to AREA in W. HL is a
4308 face-override with the following meaning:
4309
4310 DRAW_NORMAL_TEXT draw normally
4311 DRAW_CURSOR draw in cursor face
4312 DRAW_MOUSE_FACE draw in mouse face.
4313 DRAW_INVERSE_VIDEO draw in mode line face
4314 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4315 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4316
4317 If REAL_START is non-null, return in *REAL_START the real starting
4318 position for display. This can be different from START in case
4319 overlapping glyphs must be displayed. If REAL_END is non-null,
4320 return in *REAL_END the real end position for display. This can be
4321 different from END in case overlapping glyphs must be displayed.
4322
66ac4b0e
GM
4323 If OVERLAPS_P is non-zero, draw only the foreground of characters
4324 and clip to the physical height of ROW.
4325
06a2c219
GM
4326 Value is the x-position reached, relative to AREA of W. */
4327
4328static int
66ac4b0e
GM
4329x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4330 overlaps_p)
06a2c219
GM
4331 struct window *w;
4332 int x;
4333 struct glyph_row *row;
4334 enum glyph_row_area area;
4335 int start, end;
4336 enum draw_glyphs_face hl;
4337 int *real_start, *real_end;
66ac4b0e 4338 int overlaps_p;
dc6f92b8 4339{
06a2c219
GM
4340 struct glyph_string *head, *tail;
4341 struct glyph_string *s;
4342 int last_x, area_width;
4343 int x_reached;
4344 int i, j;
4345
4346 /* Let's rather be paranoid than getting a SEGV. */
4347 start = max (0, start);
4348 end = min (end, row->used[area]);
4349 if (real_start)
4350 *real_start = start;
4351 if (real_end)
4352 *real_end = end;
4353
4354 /* Translate X to frame coordinates. Set last_x to the right
4355 end of the drawing area. */
4356 if (row->full_width_p)
4357 {
4358 /* X is relative to the left edge of W, without scroll bars
4359 or flag areas. */
4360 struct frame *f = XFRAME (w->frame);
110859fc 4361 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
06a2c219 4362 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
dc6f92b8 4363
06a2c219
GM
4364 x += window_left_x;
4365 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4366 last_x = window_left_x + area_width;
4367
4368 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4369 {
110859fc 4370 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
06a2c219
GM
4371 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4372 last_x += width;
4373 else
4374 x -= width;
4375 }
dc6f92b8 4376
b9432a85
GM
4377 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4378 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
06a2c219
GM
4379 }
4380 else
dc6f92b8 4381 {
06a2c219
GM
4382 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4383 area_width = window_box_width (w, area);
4384 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
dc6f92b8
JB
4385 }
4386
06a2c219
GM
4387 /* Build a doubly-linked list of glyph_string structures between
4388 head and tail from what we have to draw. Note that the macro
4389 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4390 the reason we use a separate variable `i'. */
4391 i = start;
66ac4b0e
GM
4392 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4393 overlaps_p);
06a2c219
GM
4394 if (tail)
4395 x_reached = tail->x + tail->background_width;
4396 else
4397 x_reached = x;
90e65f07 4398
06a2c219
GM
4399 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4400 the row, redraw some glyphs in front or following the glyph
4401 strings built above. */
66ac4b0e 4402 if (!overlaps_p && row->contains_overlapping_glyphs_p)
06a2c219
GM
4403 {
4404 int dummy_x = 0;
4405 struct glyph_string *h, *t;
4406
4407 /* Compute overhangs for all glyph strings. */
4408 for (s = head; s; s = s->next)
4409 x_compute_glyph_string_overhangs (s);
4410
4411 /* Prepend glyph strings for glyphs in front of the first glyph
4412 string that are overwritten because of the first glyph
4413 string's left overhang. The background of all strings
4414 prepended must be drawn because the first glyph string
4415 draws over it. */
4416 i = x_left_overwritten (head);
4417 if (i >= 0)
4418 {
4419 j = i;
4420 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
66ac4b0e
GM
4421 DRAW_NORMAL_TEXT, dummy_x, last_x,
4422 overlaps_p);
06a2c219
GM
4423 start = i;
4424 if (real_start)
4425 *real_start = start;
4426 x_compute_overhangs_and_x (t, head->x, 1);
4427 x_prepend_glyph_string_lists (&head, &tail, h, t);
4428 }
58769bee 4429
06a2c219
GM
4430 /* Prepend glyph strings for glyphs in front of the first glyph
4431 string that overwrite that glyph string because of their
4432 right overhang. For these strings, only the foreground must
4433 be drawn, because it draws over the glyph string at `head'.
4434 The background must not be drawn because this would overwrite
4435 right overhangs of preceding glyphs for which no glyph
4436 strings exist. */
4437 i = x_left_overwriting (head);
4438 if (i >= 0)
4439 {
4440 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
66ac4b0e
GM
4441 DRAW_NORMAL_TEXT, dummy_x, last_x,
4442 overlaps_p);
06a2c219
GM
4443 for (s = h; s; s = s->next)
4444 s->background_filled_p = 1;
4445 if (real_start)
4446 *real_start = i;
4447 x_compute_overhangs_and_x (t, head->x, 1);
4448 x_prepend_glyph_string_lists (&head, &tail, h, t);
4449 }
dbcb258a 4450
06a2c219
GM
4451 /* Append glyphs strings for glyphs following the last glyph
4452 string tail that are overwritten by tail. The background of
4453 these strings has to be drawn because tail's foreground draws
4454 over it. */
4455 i = x_right_overwritten (tail);
4456 if (i >= 0)
4457 {
4458 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4459 DRAW_NORMAL_TEXT, x, last_x,
4460 overlaps_p);
06a2c219
GM
4461 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4462 x_append_glyph_string_lists (&head, &tail, h, t);
4463 if (real_end)
4464 *real_end = i;
4465 }
dc6f92b8 4466
06a2c219
GM
4467 /* Append glyph strings for glyphs following the last glyph
4468 string tail that overwrite tail. The foreground of such
4469 glyphs has to be drawn because it writes into the background
4470 of tail. The background must not be drawn because it could
4471 paint over the foreground of following glyphs. */
4472 i = x_right_overwriting (tail);
4473 if (i >= 0)
4474 {
4475 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4476 DRAW_NORMAL_TEXT, x, last_x,
4477 overlaps_p);
06a2c219
GM
4478 for (s = h; s; s = s->next)
4479 s->background_filled_p = 1;
4480 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4481 x_append_glyph_string_lists (&head, &tail, h, t);
4482 if (real_end)
4483 *real_end = i;
4484 }
4485 }
58769bee 4486
06a2c219
GM
4487 /* Draw all strings. */
4488 for (s = head; s; s = s->next)
4489 x_draw_glyph_string (s);
dc6f92b8 4490
06a2c219
GM
4491 /* Value is the x-position up to which drawn, relative to AREA of W.
4492 This doesn't include parts drawn because of overhangs. */
4493 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4494 if (!row->full_width_p)
4495 {
4496 if (area > LEFT_MARGIN_AREA)
4497 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4498 if (area > TEXT_AREA)
4499 x_reached -= window_box_width (w, TEXT_AREA);
4500 }
4501 return x_reached;
4502}
dc6f92b8 4503
dc6f92b8 4504
66ac4b0e
GM
4505/* Fix the display of area AREA of overlapping row ROW in window W. */
4506
4507static void
4508x_fix_overlapping_area (w, row, area)
4509 struct window *w;
4510 struct glyph_row *row;
4511 enum glyph_row_area area;
4512{
4513 int i, x;
4514
4515 BLOCK_INPUT;
4516
4517 if (area == LEFT_MARGIN_AREA)
4518 x = 0;
4519 else if (area == TEXT_AREA)
4520 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
4521 else
4522 x = (window_box_width (w, LEFT_MARGIN_AREA)
4523 + window_box_width (w, TEXT_AREA));
4524
4525 for (i = 0; i < row->used[area];)
4526 {
4527 if (row->glyphs[area][i].overlaps_vertically_p)
4528 {
4529 int start = i, start_x = x;
4530
4531 do
4532 {
4533 x += row->glyphs[area][i].pixel_width;
4534 ++i;
4535 }
4536 while (i < row->used[area]
4537 && row->glyphs[area][i].overlaps_vertically_p);
4538
4539 x_draw_glyphs (w, start_x, row, area, start, i,
4540 (row->inverse_p
4541 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4542 NULL, NULL, 1);
4543 }
4544 else
4545 {
4546 x += row->glyphs[area][i].pixel_width;
4547 ++i;
4548 }
4549 }
4550
4551 UNBLOCK_INPUT;
4552}
4553
4554
06a2c219
GM
4555/* Output LEN glyphs starting at START at the nominal cursor position.
4556 Advance the nominal cursor over the text. The global variable
4557 updated_window contains the window being updated, updated_row is
4558 the glyph row being updated, and updated_area is the area of that
4559 row being updated. */
dc6f92b8 4560
06a2c219
GM
4561static void
4562x_write_glyphs (start, len)
4563 struct glyph *start;
4564 int len;
4565{
4566 int x, hpos, real_start, real_end;
d9cdbb3d 4567
06a2c219 4568 xassert (updated_window && updated_row);
dc6f92b8 4569 BLOCK_INPUT;
06a2c219
GM
4570
4571 /* Write glyphs. */
dc6f92b8 4572
06a2c219
GM
4573 hpos = start - updated_row->glyphs[updated_area];
4574 x = x_draw_glyphs (updated_window, output_cursor.x,
4575 updated_row, updated_area,
4576 hpos, hpos + len,
4577 (updated_row->inverse_p
4578 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
66ac4b0e 4579 &real_start, &real_end, 0);
b30ec466 4580
06a2c219
GM
4581 /* If we drew over the cursor, note that it is not visible any more. */
4582 note_overwritten_text_cursor (updated_window, real_start,
4583 real_end - real_start);
dc6f92b8
JB
4584
4585 UNBLOCK_INPUT;
06a2c219
GM
4586
4587 /* Advance the output cursor. */
4588 output_cursor.hpos += len;
4589 output_cursor.x = x;
dc6f92b8
JB
4590}
4591
0cdd0c9f 4592
06a2c219 4593/* Insert LEN glyphs from START at the nominal cursor position. */
0cdd0c9f 4594
06a2c219
GM
4595static void
4596x_insert_glyphs (start, len)
4597 struct glyph *start;
4598 register int len;
4599{
4600 struct frame *f;
4601 struct window *w;
4602 int line_height, shift_by_width, shifted_region_width;
4603 struct glyph_row *row;
4604 struct glyph *glyph;
4605 int frame_x, frame_y, hpos, real_start, real_end;
58769bee 4606
06a2c219 4607 xassert (updated_window && updated_row);
0cdd0c9f 4608 BLOCK_INPUT;
06a2c219
GM
4609 w = updated_window;
4610 f = XFRAME (WINDOW_FRAME (w));
4611
4612 /* Get the height of the line we are in. */
4613 row = updated_row;
4614 line_height = row->height;
4615
4616 /* Get the width of the glyphs to insert. */
4617 shift_by_width = 0;
4618 for (glyph = start; glyph < start + len; ++glyph)
4619 shift_by_width += glyph->pixel_width;
4620
4621 /* Get the width of the region to shift right. */
4622 shifted_region_width = (window_box_width (w, updated_area)
4623 - output_cursor.x
4624 - shift_by_width);
4625
4626 /* Shift right. */
4627 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
4628 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
4629 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
4630 f->output_data.x->normal_gc,
4631 frame_x, frame_y,
4632 shifted_region_width, line_height,
4633 frame_x + shift_by_width, frame_y);
4634
4635 /* Write the glyphs. */
4636 hpos = start - row->glyphs[updated_area];
4637 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
66ac4b0e 4638 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
06a2c219
GM
4639 note_overwritten_text_cursor (w, real_start, real_end - real_start);
4640
4641 /* Advance the output cursor. */
4642 output_cursor.hpos += len;
4643 output_cursor.x += shift_by_width;
0cdd0c9f
RS
4644 UNBLOCK_INPUT;
4645}
0cdd0c9f 4646
0cdd0c9f 4647
06a2c219
GM
4648/* Delete N glyphs at the nominal cursor position. Not implemented
4649 for X frames. */
c83febd7
RS
4650
4651static void
06a2c219
GM
4652x_delete_glyphs (n)
4653 register int n;
c83febd7 4654{
06a2c219 4655 abort ();
c83febd7
RS
4656}
4657
0cdd0c9f 4658
06a2c219
GM
4659/* Erase the current text line from the nominal cursor position
4660 (inclusive) to pixel column TO_X (exclusive). The idea is that
4661 everything from TO_X onward is already erased.
4662
4663 TO_X is a pixel position relative to updated_area of
4664 updated_window. TO_X == -1 means clear to the end of this area. */
dc6f92b8 4665
06a2c219
GM
4666static void
4667x_clear_end_of_line (to_x)
4668 int to_x;
4669{
4670 struct frame *f;
4671 struct window *w = updated_window;
4672 int max_x, min_y, max_y;
4673 int from_x, from_y, to_y;
4674
4675 xassert (updated_window && updated_row);
4676 f = XFRAME (w->frame);
4677
4678 if (updated_row->full_width_p)
4679 {
4680 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
4681 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
4682 && !w->pseudo_window_p)
4683 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
0cdd0c9f 4684 }
06a2c219
GM
4685 else
4686 max_x = window_box_width (w, updated_area);
4687 max_y = window_text_bottom_y (w);
dc6f92b8 4688
06a2c219
GM
4689 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
4690 of window. For TO_X > 0, truncate to end of drawing area. */
4691 if (to_x == 0)
4692 return;
4693 else if (to_x < 0)
4694 to_x = max_x;
4695 else
4696 to_x = min (to_x, max_x);
dbc4e1c1 4697
06a2c219
GM
4698 to_y = min (max_y, output_cursor.y + updated_row->height);
4699
4700 /* Notice if the cursor will be cleared by this operation. */
4701 if (!updated_row->full_width_p)
4702 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
dbc4e1c1 4703
06a2c219
GM
4704 from_x = output_cursor.x;
4705
4706 /* Translate to frame coordinates. */
4707 if (updated_row->full_width_p)
4708 {
4709 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
4710 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
4711 }
0cdd0c9f
RS
4712 else
4713 {
06a2c219
GM
4714 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
4715 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
4716 }
4717
045dee35 4718 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
06a2c219
GM
4719 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
4720 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
4721
4722 /* Prevent inadvertently clearing to end of the X window. */
4723 if (to_x > from_x && to_y > from_y)
4724 {
4725 BLOCK_INPUT;
4726 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4727 from_x, from_y, to_x - from_x, to_y - from_y,
4728 False);
4729 UNBLOCK_INPUT;
0cdd0c9f 4730 }
0cdd0c9f 4731}
dbc4e1c1 4732
0cdd0c9f 4733
06a2c219 4734/* Clear entire frame. If updating_frame is non-null, clear that
b86bd3dd 4735 frame. Otherwise clear the selected frame. */
06a2c219
GM
4736
4737static void
4738x_clear_frame ()
0cdd0c9f 4739{
06a2c219 4740 struct frame *f;
0cdd0c9f 4741
06a2c219
GM
4742 if (updating_frame)
4743 f = updating_frame;
0cdd0c9f 4744 else
b86bd3dd 4745 f = SELECTED_FRAME ();
58769bee 4746
06a2c219
GM
4747 /* Clearing the frame will erase any cursor, so mark them all as no
4748 longer visible. */
4749 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
4750 output_cursor.hpos = output_cursor.vpos = 0;
4751 output_cursor.x = -1;
4752
4753 /* We don't set the output cursor here because there will always
4754 follow an explicit cursor_to. */
4755 BLOCK_INPUT;
4756 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
4757
4758 /* We have to clear the scroll bars, too. If we have changed
4759 colors or something like that, then they should be notified. */
4760 x_scroll_bar_clear (f);
0cdd0c9f 4761
06a2c219
GM
4762 XFlush (FRAME_X_DISPLAY (f));
4763 UNBLOCK_INPUT;
dc6f92b8 4764}
06a2c219
GM
4765
4766
dc6f92b8 4767\f
dbc4e1c1
JB
4768/* Invert the middle quarter of the frame for .15 sec. */
4769
06a2c219
GM
4770/* We use the select system call to do the waiting, so we have to make
4771 sure it's available. If it isn't, we just won't do visual bells. */
4772
dbc4e1c1
JB
4773#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
4774
06a2c219
GM
4775
4776/* Subtract the `struct timeval' values X and Y, storing the result in
4777 *RESULT. Return 1 if the difference is negative, otherwise 0. */
dbc4e1c1
JB
4778
4779static int
4780timeval_subtract (result, x, y)
4781 struct timeval *result, x, y;
4782{
06a2c219
GM
4783 /* Perform the carry for the later subtraction by updating y. This
4784 is safer because on some systems the tv_sec member is unsigned. */
dbc4e1c1
JB
4785 if (x.tv_usec < y.tv_usec)
4786 {
4787 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
4788 y.tv_usec -= 1000000 * nsec;
4789 y.tv_sec += nsec;
4790 }
06a2c219 4791
dbc4e1c1
JB
4792 if (x.tv_usec - y.tv_usec > 1000000)
4793 {
4794 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
4795 y.tv_usec += 1000000 * nsec;
4796 y.tv_sec -= nsec;
4797 }
4798
06a2c219
GM
4799 /* Compute the time remaining to wait. tv_usec is certainly
4800 positive. */
dbc4e1c1
JB
4801 result->tv_sec = x.tv_sec - y.tv_sec;
4802 result->tv_usec = x.tv_usec - y.tv_usec;
4803
06a2c219
GM
4804 /* Return indication of whether the result should be considered
4805 negative. */
dbc4e1c1
JB
4806 return x.tv_sec < y.tv_sec;
4807}
dc6f92b8 4808
dfcf069d 4809void
f676886a
JB
4810XTflash (f)
4811 struct frame *f;
dc6f92b8 4812{
dbc4e1c1 4813 BLOCK_INPUT;
dc6f92b8 4814
dbc4e1c1
JB
4815 {
4816 GC gc;
dc6f92b8 4817
06a2c219
GM
4818 /* Create a GC that will use the GXxor function to flip foreground
4819 pixels into background pixels. */
dbc4e1c1
JB
4820 {
4821 XGCValues values;
dc6f92b8 4822
dbc4e1c1 4823 values.function = GXxor;
7556890b
RS
4824 values.foreground = (f->output_data.x->foreground_pixel
4825 ^ f->output_data.x->background_pixel);
58769bee 4826
334208b7 4827 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dbc4e1c1
JB
4828 GCFunction | GCForeground, &values);
4829 }
dc6f92b8 4830
dbc4e1c1 4831 {
e84e14c3
RS
4832 /* Get the height not including a menu bar widget. */
4833 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
4834 /* Height of each line to flash. */
4835 int flash_height = FRAME_LINE_HEIGHT (f);
4836 /* These will be the left and right margins of the rectangles. */
4837 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
4838 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
4839
4840 int width;
4841
4842 /* Don't flash the area between a scroll bar and the frame
4843 edge it is next to. */
4844 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
4845 {
4846 case vertical_scroll_bar_left:
4847 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
4848 break;
4849
4850 case vertical_scroll_bar_right:
4851 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
4852 break;
06a2c219
GM
4853
4854 default:
4855 break;
e84e14c3
RS
4856 }
4857
4858 width = flash_right - flash_left;
4859
4860 /* If window is tall, flash top and bottom line. */
4861 if (height > 3 * FRAME_LINE_HEIGHT (f))
4862 {
4863 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
4864 flash_left,
4865 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 4866 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
4867 width, flash_height);
4868 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4869 flash_left,
4870 (height - flash_height
4871 - FRAME_INTERNAL_BORDER_WIDTH (f)),
4872 width, flash_height);
4873 }
4874 else
4875 /* If it is short, flash it all. */
4876 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4877 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
4878 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
dc6f92b8 4879
06a2c219 4880 x_flush (f);
dc6f92b8 4881
dbc4e1c1 4882 {
06a2c219 4883 struct timeval wakeup;
dc6f92b8 4884
66c30ea1 4885 EMACS_GET_TIME (wakeup);
dc6f92b8 4886
dbc4e1c1
JB
4887 /* Compute time to wait until, propagating carry from usecs. */
4888 wakeup.tv_usec += 150000;
4889 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
4890 wakeup.tv_usec %= 1000000;
4891
4892 /* Keep waiting until past the time wakeup. */
4893 while (1)
4894 {
4895 struct timeval timeout;
4896
66c30ea1 4897 EMACS_GET_TIME (timeout);
dbc4e1c1
JB
4898
4899 /* In effect, timeout = wakeup - timeout.
4900 Break if result would be negative. */
4901 if (timeval_subtract (&timeout, wakeup, timeout))
4902 break;
4903
4904 /* Try to wait that long--but we might wake up sooner. */
c32cdd9a 4905 select (0, NULL, NULL, NULL, &timeout);
dbc4e1c1
JB
4906 }
4907 }
58769bee 4908
e84e14c3
RS
4909 /* If window is tall, flash top and bottom line. */
4910 if (height > 3 * FRAME_LINE_HEIGHT (f))
4911 {
4912 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
4913 flash_left,
4914 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 4915 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
4916 width, flash_height);
4917 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4918 flash_left,
4919 (height - flash_height
4920 - FRAME_INTERNAL_BORDER_WIDTH (f)),
4921 width, flash_height);
4922 }
4923 else
4924 /* If it is short, flash it all. */
4925 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4926 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
4927 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
4928
334208b7 4929 XFreeGC (FRAME_X_DISPLAY (f), gc);
06a2c219 4930 x_flush (f);
dc6f92b8 4931 }
dbc4e1c1
JB
4932 }
4933
4934 UNBLOCK_INPUT;
dc6f92b8
JB
4935}
4936
06a2c219 4937#endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
dbc4e1c1
JB
4938
4939
dc6f92b8
JB
4940/* Make audible bell. */
4941
dfcf069d 4942void
dc6f92b8
JB
4943XTring_bell ()
4944{
b86bd3dd
GM
4945 struct frame *f = SELECTED_FRAME ();
4946
4947 if (FRAME_X_DISPLAY (f))
4948 {
dbc4e1c1 4949#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
b86bd3dd
GM
4950 if (visible_bell)
4951 XTflash (f);
4952 else
dbc4e1c1 4953#endif
b86bd3dd
GM
4954 {
4955 BLOCK_INPUT;
4956 XBell (FRAME_X_DISPLAY (f), 0);
4957 XFlush (FRAME_X_DISPLAY (f));
4958 UNBLOCK_INPUT;
4959 }
dc6f92b8
JB
4960 }
4961}
06a2c219 4962
dc6f92b8 4963\f
06a2c219
GM
4964/* Specify how many text lines, from the top of the window,
4965 should be affected by insert-lines and delete-lines operations.
4966 This, and those operations, are used only within an update
4967 that is bounded by calls to x_update_begin and x_update_end. */
dc6f92b8 4968
dfcf069d 4969static void
06a2c219
GM
4970XTset_terminal_window (n)
4971 register int n;
dc6f92b8 4972{
06a2c219 4973 /* This function intentionally left blank. */
dc6f92b8
JB
4974}
4975
06a2c219
GM
4976
4977\f
4978/***********************************************************************
4979 Line Dance
4980 ***********************************************************************/
4981
4982/* Perform an insert-lines or delete-lines operation, inserting N
4983 lines or deleting -N lines at vertical position VPOS. */
4984
dfcf069d 4985static void
06a2c219
GM
4986x_ins_del_lines (vpos, n)
4987 int vpos, n;
dc6f92b8
JB
4988{
4989 abort ();
4990}
06a2c219
GM
4991
4992
4993/* Scroll part of the display as described by RUN. */
dc6f92b8 4994
dfcf069d 4995static void
06a2c219
GM
4996x_scroll_run (w, run)
4997 struct window *w;
4998 struct run *run;
dc6f92b8 4999{
06a2c219
GM
5000 struct frame *f = XFRAME (w->frame);
5001 int x, y, width, height, from_y, to_y, bottom_y;
5002
5003 /* Get frame-relative bounding box of the text display area of W,
5004 without mode lines. Include in this box the flags areas to the
5005 left and right of W. */
5006 window_box (w, -1, &x, &y, &width, &height);
110859fc
GM
5007 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5008 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
5009
5010 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5011 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5012 bottom_y = y + height;
dc6f92b8 5013
06a2c219
GM
5014 if (to_y < from_y)
5015 {
5016 /* Scrolling up. Make sure we don't copy part of the mode
5017 line at the bottom. */
5018 if (from_y + run->height > bottom_y)
5019 height = bottom_y - from_y;
5020 else
5021 height = run->height;
5022 }
dc6f92b8 5023 else
06a2c219
GM
5024 {
5025 /* Scolling down. Make sure we don't copy over the mode line.
5026 at the bottom. */
5027 if (to_y + run->height > bottom_y)
5028 height = bottom_y - to_y;
5029 else
5030 height = run->height;
5031 }
7a13e894 5032
06a2c219
GM
5033 BLOCK_INPUT;
5034
5035 /* Cursor off. Will be switched on again in x_update_window_end. */
5036 updated_window = w;
5037 x_clear_cursor (w);
5038
5039 XCopyArea (FRAME_X_DISPLAY (f),
5040 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5041 f->output_data.x->normal_gc,
5042 x, from_y,
5043 width, height,
5044 x, to_y);
5045
5046 UNBLOCK_INPUT;
5047}
dc6f92b8 5048
dc6f92b8 5049
06a2c219
GM
5050\f
5051/***********************************************************************
5052 Exposure Events
5053 ***********************************************************************/
5054
5055/* Redisplay an exposed area of frame F. X and Y are the upper-left
5056 corner of the exposed rectangle. W and H are width and height of
5057 the exposed area. All are pixel values. W or H zero means redraw
5058 the entire frame. */
dc6f92b8 5059
06a2c219
GM
5060static void
5061expose_frame (f, x, y, w, h)
5062 struct frame *f;
5063 int x, y, w, h;
dc6f92b8 5064{
06a2c219 5065 XRectangle r;
dc6f92b8 5066
06a2c219 5067 TRACE ((stderr, "expose_frame "));
dc6f92b8 5068
06a2c219
GM
5069 /* No need to redraw if frame will be redrawn soon. */
5070 if (FRAME_GARBAGED_P (f))
dc6f92b8 5071 {
06a2c219
GM
5072 TRACE ((stderr, " garbaged\n"));
5073 return;
5074 }
5075
5076 /* If basic faces haven't been realized yet, there is no point in
5077 trying to redraw anything. This can happen when we get an expose
5078 event while Emacs is starting, e.g. by moving another window. */
5079 if (FRAME_FACE_CACHE (f) == NULL
5080 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5081 {
5082 TRACE ((stderr, " no faces\n"));
5083 return;
58769bee 5084 }
06a2c219
GM
5085
5086 if (w == 0 || h == 0)
58769bee 5087 {
06a2c219
GM
5088 r.x = r.y = 0;
5089 r.width = CANON_X_UNIT (f) * f->width;
5090 r.height = CANON_Y_UNIT (f) * f->height;
dc6f92b8
JB
5091 }
5092 else
5093 {
06a2c219
GM
5094 r.x = x;
5095 r.y = y;
5096 r.width = w;
5097 r.height = h;
5098 }
5099
5100 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5101 expose_window_tree (XWINDOW (f->root_window), &r);
5102
9ea173e8 5103 if (WINDOWP (f->tool_bar_window))
06a2c219 5104 {
9ea173e8 5105 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
5106 XRectangle window_rect;
5107 XRectangle intersection_rect;
5108 int window_x, window_y, window_width, window_height;
5109
5110
5111 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5112 window_rect.x = window_x;
5113 window_rect.y = window_y;
5114 window_rect.width = window_width;
5115 window_rect.height = window_height;
5116
5117 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5118 expose_window (w, &intersection_rect);
5119 }
5120
5121#ifndef USE_X_TOOLKIT
5122 if (WINDOWP (f->menu_bar_window))
5123 {
5124 struct window *w = XWINDOW (f->menu_bar_window);
5125 XRectangle window_rect;
5126 XRectangle intersection_rect;
5127 int window_x, window_y, window_width, window_height;
5128
5129
5130 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5131 window_rect.x = window_x;
5132 window_rect.y = window_y;
5133 window_rect.width = window_width;
5134 window_rect.height = window_height;
5135
5136 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5137 expose_window (w, &intersection_rect);
dc6f92b8 5138 }
06a2c219 5139#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
5140}
5141
06a2c219
GM
5142
5143/* Redraw (parts) of all windows in the window tree rooted at W that
5144 intersect R. R contains frame pixel coordinates. */
5145
58769bee 5146static void
06a2c219
GM
5147expose_window_tree (w, r)
5148 struct window *w;
5149 XRectangle *r;
dc6f92b8 5150{
06a2c219
GM
5151 while (w)
5152 {
5153 if (!NILP (w->hchild))
5154 expose_window_tree (XWINDOW (w->hchild), r);
5155 else if (!NILP (w->vchild))
5156 expose_window_tree (XWINDOW (w->vchild), r);
5157 else
5158 {
5159 XRectangle window_rect;
5160 XRectangle intersection_rect;
5161 struct frame *f = XFRAME (w->frame);
5162 int window_x, window_y, window_width, window_height;
5163
5164 /* Frame-relative pixel rectangle of W. */
5165 window_box (w, -1, &window_x, &window_y, &window_width,
5166 &window_height);
5167 window_rect.x
5168 = (window_x
110859fc 5169 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
714dc26c 5170 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
06a2c219
GM
5171 window_rect.y = window_y;
5172 window_rect.width
5173 = (window_width
110859fc 5174 + FRAME_X_FLAGS_AREA_WIDTH (f)
06a2c219
GM
5175 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5176 window_rect.height
5177 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5178
5179 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5180 expose_window (w, &intersection_rect);
5181 }
58769bee 5182
06a2c219
GM
5183 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5184 }
5185}
58769bee 5186
dc6f92b8 5187
06a2c219
GM
5188/* Redraw the part of glyph row area AREA of glyph row ROW on window W
5189 which intersects rectangle R. R is in window-relative coordinates. */
5190
5191static void
5192expose_area (w, row, r, area)
5193 struct window *w;
5194 struct glyph_row *row;
5195 XRectangle *r;
5196 enum glyph_row_area area;
5197{
5198 int x;
5199 struct glyph *first = row->glyphs[area];
5200 struct glyph *end = row->glyphs[area] + row->used[area];
5201 struct glyph *last;
5202 int first_x;
5203
5204 /* Set x to the window-relative start position for drawing glyphs of
5205 AREA. The first glyph of the text area can be partially visible.
5206 The first glyphs of other areas cannot. */
5207 if (area == LEFT_MARGIN_AREA)
5208 x = 0;
5209 else if (area == TEXT_AREA)
5210 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5211 else
5212 x = (window_box_width (w, LEFT_MARGIN_AREA)
5213 + window_box_width (w, TEXT_AREA));
5214
6fb13182
GM
5215 if (area == TEXT_AREA && row->fill_line_p)
5216 /* If row extends face to end of line write the whole line. */
5217 x_draw_glyphs (w, x, row, area,
5218 0, row->used[area],
06a2c219 5219 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5220 NULL, NULL, 0);
6fb13182
GM
5221 else
5222 {
5223 /* Find the first glyph that must be redrawn. */
5224 while (first < end
5225 && x + first->pixel_width < r->x)
5226 {
5227 x += first->pixel_width;
5228 ++first;
5229 }
5230
5231 /* Find the last one. */
5232 last = first;
5233 first_x = x;
5234 while (last < end
5235 && x < r->x + r->width)
5236 {
5237 x += last->pixel_width;
5238 ++last;
5239 }
5240
5241 /* Repaint. */
5242 if (last > first)
5243 x_draw_glyphs (w, first_x, row, area,
5244 first - row->glyphs[area],
5245 last - row->glyphs[area],
5246 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5247 NULL, NULL, 0);
5248 }
06a2c219
GM
5249}
5250
58769bee 5251
06a2c219
GM
5252/* Redraw the parts of the glyph row ROW on window W intersecting
5253 rectangle R. R is in window-relative coordinates. */
dc6f92b8 5254
06a2c219
GM
5255static void
5256expose_line (w, row, r)
5257 struct window *w;
5258 struct glyph_row *row;
5259 XRectangle *r;
5260{
5261 xassert (row->enabled_p);
5262
5263 if (row->mode_line_p || w->pseudo_window_p)
5264 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5265 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5266 NULL, NULL, 0);
06a2c219
GM
5267 else
5268 {
5269 if (row->used[LEFT_MARGIN_AREA])
5270 expose_area (w, row, r, LEFT_MARGIN_AREA);
5271 if (row->used[TEXT_AREA])
5272 expose_area (w, row, r, TEXT_AREA);
5273 if (row->used[RIGHT_MARGIN_AREA])
5274 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5275 x_draw_row_bitmaps (w, row);
5276 }
5277}
dc6f92b8 5278
58769bee 5279
06a2c219
GM
5280/* Return non-zero if W's cursor intersects rectangle R. */
5281
5282static int
5283x_phys_cursor_in_rect_p (w, r)
5284 struct window *w;
5285 XRectangle *r;
5286{
5287 XRectangle cr, result;
5288 struct glyph *cursor_glyph;
5289
5290 cursor_glyph = get_phys_cursor_glyph (w);
5291 if (cursor_glyph)
5292 {
5293 cr.x = w->phys_cursor.x;
5294 cr.y = w->phys_cursor.y;
5295 cr.width = cursor_glyph->pixel_width;
5296 cr.height = w->phys_cursor_height;
5297 return x_intersect_rectangles (&cr, r, &result);
5298 }
5299 else
5300 return 0;
dc6f92b8 5301}
dc6f92b8 5302
06a2c219
GM
5303
5304/* Redraw a rectangle of window W. R is a rectangle in window
5305 relative coordinates. Call this function with input blocked. */
dc6f92b8
JB
5306
5307static void
06a2c219
GM
5308expose_window (w, r)
5309 struct window *w;
5310 XRectangle *r;
dc6f92b8 5311{
06a2c219
GM
5312 struct glyph_row *row;
5313 int y;
5314 int yb = window_text_bottom_y (w);
5315 int cursor_cleared_p;
dc6f92b8 5316
80c32bcc
GM
5317 /* If window is not yet fully initialized, do nothing. This can
5318 happen when toolkit scroll bars are used and a window is split.
5319 Reconfiguring the scroll bar will generate an expose for a newly
5320 created window. */
5321 if (w->current_matrix == NULL)
5322 return;
5323
06a2c219
GM
5324 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5325 r->x, r->y, r->width, r->height));
dc6f92b8 5326
06a2c219
GM
5327 /* Convert to window coordinates. */
5328 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5329 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
dc6f92b8 5330
06a2c219
GM
5331 /* Turn off the cursor. */
5332 if (!w->pseudo_window_p
5333 && x_phys_cursor_in_rect_p (w, r))
5334 {
5335 x_clear_cursor (w);
5336 cursor_cleared_p = 1;
5337 }
5338 else
5339 cursor_cleared_p = 0;
5340
5341 /* Find the first row intersecting the rectangle R. */
5342 row = w->current_matrix->rows;
5343 y = 0;
5344 while (row->enabled_p
5345 && y < yb
5346 && y + row->height < r->y)
5347 {
5348 y += row->height;
5349 ++row;
5350 }
5351
dc6f92b8 5352 /* Display the text in the rectangle, one text line at a time. */
06a2c219
GM
5353 while (row->enabled_p
5354 && y < yb
5355 && y < r->y + r->height)
5356 {
5357 expose_line (w, row, r);
5358 y += row->height;
5359 ++row;
5360 }
5361
5362 /* Display the mode line if there is one. */
5363 if (WINDOW_WANTS_MODELINE_P (w)
5364 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5365 row->enabled_p)
5366 && row->y < r->y + r->height)
5367 expose_line (w, row, r);
dc6f92b8 5368
06a2c219 5369 if (!w->pseudo_window_p)
dc6f92b8 5370 {
06a2c219
GM
5371 /* Draw border between windows. */
5372 x_draw_vertical_border (w);
5373
5374 /* Turn the cursor on again. */
5375 if (cursor_cleared_p)
5376 x_update_window_cursor (w, 1);
5377 }
5378}
dc6f92b8 5379
dc6f92b8 5380
06a2c219
GM
5381/* Determine the intersection of two rectangles R1 and R2. Return
5382 the intersection in *RESULT. Value is non-zero if RESULT is not
5383 empty. */
5384
5385static int
5386x_intersect_rectangles (r1, r2, result)
5387 XRectangle *r1, *r2, *result;
5388{
5389 XRectangle *left, *right;
5390 XRectangle *upper, *lower;
5391 int intersection_p = 0;
5392
5393 /* Rearrange so that R1 is the left-most rectangle. */
5394 if (r1->x < r2->x)
5395 left = r1, right = r2;
5396 else
5397 left = r2, right = r1;
5398
5399 /* X0 of the intersection is right.x0, if this is inside R1,
5400 otherwise there is no intersection. */
5401 if (right->x <= left->x + left->width)
5402 {
5403 result->x = right->x;
5404
5405 /* The right end of the intersection is the minimum of the
5406 the right ends of left and right. */
5407 result->width = (min (left->x + left->width, right->x + right->width)
5408 - result->x);
5409
5410 /* Same game for Y. */
5411 if (r1->y < r2->y)
5412 upper = r1, lower = r2;
5413 else
5414 upper = r2, lower = r1;
5415
5416 /* The upper end of the intersection is lower.y0, if this is inside
5417 of upper. Otherwise, there is no intersection. */
5418 if (lower->y <= upper->y + upper->height)
dc43ef94 5419 {
06a2c219
GM
5420 result->y = lower->y;
5421
5422 /* The lower end of the intersection is the minimum of the lower
5423 ends of upper and lower. */
5424 result->height = (min (lower->y + lower->height,
5425 upper->y + upper->height)
5426 - result->y);
5427 intersection_p = 1;
dc43ef94 5428 }
dc6f92b8
JB
5429 }
5430
06a2c219 5431 return intersection_p;
dc6f92b8 5432}
06a2c219
GM
5433
5434
5435
5436
dc6f92b8 5437\f
dc6f92b8 5438static void
334208b7
RS
5439frame_highlight (f)
5440 struct frame *f;
dc6f92b8 5441{
b3e1e05c
JB
5442 /* We used to only do this if Vx_no_window_manager was non-nil, but
5443 the ICCCM (section 4.1.6) says that the window's border pixmap
5444 and border pixel are window attributes which are "private to the
5445 client", so we can always change it to whatever we want. */
5446 BLOCK_INPUT;
334208b7 5447 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5448 f->output_data.x->border_pixel);
b3e1e05c 5449 UNBLOCK_INPUT;
5d46f928 5450 x_update_cursor (f, 1);
dc6f92b8
JB
5451}
5452
5453static void
334208b7
RS
5454frame_unhighlight (f)
5455 struct frame *f;
dc6f92b8 5456{
b3e1e05c
JB
5457 /* We used to only do this if Vx_no_window_manager was non-nil, but
5458 the ICCCM (section 4.1.6) says that the window's border pixmap
5459 and border pixel are window attributes which are "private to the
5460 client", so we can always change it to whatever we want. */
5461 BLOCK_INPUT;
334208b7 5462 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5463 f->output_data.x->border_tile);
b3e1e05c 5464 UNBLOCK_INPUT;
5d46f928 5465 x_update_cursor (f, 1);
dc6f92b8 5466}
dc6f92b8 5467
f676886a
JB
5468/* The focus has changed. Update the frames as necessary to reflect
5469 the new situation. Note that we can't change the selected frame
c5acd733 5470 here, because the Lisp code we are interrupting might become confused.
eb8c3be9 5471 Each event gets marked with the frame in which it occurred, so the
c5acd733 5472 Lisp code can tell when the switch took place by examining the events. */
dc6f92b8 5473
6d4238f3 5474static void
0f941935
KH
5475x_new_focus_frame (dpyinfo, frame)
5476 struct x_display_info *dpyinfo;
f676886a 5477 struct frame *frame;
dc6f92b8 5478{
0f941935 5479 struct frame *old_focus = dpyinfo->x_focus_frame;
dc6f92b8 5480
0f941935 5481 if (frame != dpyinfo->x_focus_frame)
dc6f92b8 5482 {
58769bee 5483 /* Set this before calling other routines, so that they see
f676886a 5484 the correct value of x_focus_frame. */
0f941935 5485 dpyinfo->x_focus_frame = frame;
6d4238f3
JB
5486
5487 if (old_focus && old_focus->auto_lower)
f676886a 5488 x_lower_frame (old_focus);
dc6f92b8
JB
5489
5490#if 0
f676886a 5491 selected_frame = frame;
e0c1aef2
KH
5492 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
5493 selected_frame);
f676886a
JB
5494 Fselect_window (selected_frame->selected_window);
5495 choose_minibuf_frame ();
c118dd06 5496#endif /* ! 0 */
dc6f92b8 5497
0f941935
KH
5498 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
5499 pending_autoraise_frame = dpyinfo->x_focus_frame;
0134a210
RS
5500 else
5501 pending_autoraise_frame = 0;
6d4238f3 5502 }
dc6f92b8 5503
0f941935 5504 x_frame_rehighlight (dpyinfo);
6d4238f3
JB
5505}
5506
37c2c98b
RS
5507/* Handle an event saying the mouse has moved out of an Emacs frame. */
5508
5509void
0f941935
KH
5510x_mouse_leave (dpyinfo)
5511 struct x_display_info *dpyinfo;
37c2c98b 5512{
0f941935 5513 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
37c2c98b 5514}
6d4238f3 5515
f451eb13
JB
5516/* The focus has changed, or we have redirected a frame's focus to
5517 another frame (this happens when a frame uses a surrogate
06a2c219 5518 mini-buffer frame). Shift the highlight as appropriate.
0f941935
KH
5519
5520 The FRAME argument doesn't necessarily have anything to do with which
06a2c219 5521 frame is being highlighted or un-highlighted; we only use it to find
0f941935 5522 the appropriate X display info. */
06a2c219 5523
6d4238f3 5524static void
0f941935
KH
5525XTframe_rehighlight (frame)
5526 struct frame *frame;
6d4238f3 5527{
0f941935
KH
5528 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
5529}
6d4238f3 5530
0f941935
KH
5531static void
5532x_frame_rehighlight (dpyinfo)
5533 struct x_display_info *dpyinfo;
5534{
5535 struct frame *old_highlight = dpyinfo->x_highlight_frame;
5536
5537 if (dpyinfo->x_focus_frame)
6d4238f3 5538 {
0f941935
KH
5539 dpyinfo->x_highlight_frame
5540 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
5541 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
5542 : dpyinfo->x_focus_frame);
5543 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
f451eb13 5544 {
0f941935
KH
5545 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
5546 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
f451eb13 5547 }
dc6f92b8 5548 }
6d4238f3 5549 else
0f941935 5550 dpyinfo->x_highlight_frame = 0;
dc6f92b8 5551
0f941935 5552 if (dpyinfo->x_highlight_frame != old_highlight)
6d4238f3
JB
5553 {
5554 if (old_highlight)
f676886a 5555 frame_unhighlight (old_highlight);
0f941935
KH
5556 if (dpyinfo->x_highlight_frame)
5557 frame_highlight (dpyinfo->x_highlight_frame);
6d4238f3 5558 }
dc6f92b8 5559}
06a2c219
GM
5560
5561
dc6f92b8 5562\f
06a2c219 5563/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
dc6f92b8 5564
28430d3c
JB
5565/* Initialize mode_switch_bit and modifier_meaning. */
5566static void
334208b7
RS
5567x_find_modifier_meanings (dpyinfo)
5568 struct x_display_info *dpyinfo;
28430d3c 5569{
f689eb05 5570 int min_code, max_code;
28430d3c
JB
5571 KeySym *syms;
5572 int syms_per_code;
5573 XModifierKeymap *mods;
5574
334208b7
RS
5575 dpyinfo->meta_mod_mask = 0;
5576 dpyinfo->shift_lock_mask = 0;
5577 dpyinfo->alt_mod_mask = 0;
5578 dpyinfo->super_mod_mask = 0;
5579 dpyinfo->hyper_mod_mask = 0;
58769bee 5580
9658a521 5581#ifdef HAVE_X11R4
334208b7 5582 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
9658a521 5583#else
4a60f8c5
RS
5584 min_code = dpyinfo->display->min_keycode;
5585 max_code = dpyinfo->display->max_keycode;
9658a521
JB
5586#endif
5587
334208b7 5588 syms = XGetKeyboardMapping (dpyinfo->display,
28430d3c
JB
5589 min_code, max_code - min_code + 1,
5590 &syms_per_code);
334208b7 5591 mods = XGetModifierMapping (dpyinfo->display);
28430d3c 5592
58769bee 5593 /* Scan the modifier table to see which modifier bits the Meta and
11edeb03 5594 Alt keysyms are on. */
28430d3c 5595 {
06a2c219 5596 int row, col; /* The row and column in the modifier table. */
28430d3c
JB
5597
5598 for (row = 3; row < 8; row++)
5599 for (col = 0; col < mods->max_keypermod; col++)
5600 {
0299d313
RS
5601 KeyCode code
5602 = mods->modifiermap[(row * mods->max_keypermod) + col];
28430d3c 5603
af92970c
KH
5604 /* Zeroes are used for filler. Skip them. */
5605 if (code == 0)
5606 continue;
5607
28430d3c
JB
5608 /* Are any of this keycode's keysyms a meta key? */
5609 {
5610 int code_col;
5611
5612 for (code_col = 0; code_col < syms_per_code; code_col++)
5613 {
f689eb05 5614 int sym = syms[((code - min_code) * syms_per_code) + code_col];
28430d3c 5615
f689eb05 5616 switch (sym)
28430d3c 5617 {
f689eb05
JB
5618 case XK_Meta_L:
5619 case XK_Meta_R:
334208b7 5620 dpyinfo->meta_mod_mask |= (1 << row);
28430d3c 5621 break;
f689eb05
JB
5622
5623 case XK_Alt_L:
5624 case XK_Alt_R:
334208b7 5625 dpyinfo->alt_mod_mask |= (1 << row);
a3c44b14
RS
5626 break;
5627
5628 case XK_Hyper_L:
5629 case XK_Hyper_R:
334208b7 5630 dpyinfo->hyper_mod_mask |= (1 << row);
a3c44b14
RS
5631 break;
5632
5633 case XK_Super_L:
5634 case XK_Super_R:
334208b7 5635 dpyinfo->super_mod_mask |= (1 << row);
f689eb05 5636 break;
11edeb03
JB
5637
5638 case XK_Shift_Lock:
5639 /* Ignore this if it's not on the lock modifier. */
5640 if ((1 << row) == LockMask)
334208b7 5641 dpyinfo->shift_lock_mask = LockMask;
11edeb03 5642 break;
28430d3c
JB
5643 }
5644 }
5645 }
5646 }
5647 }
5648
f689eb05 5649 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
334208b7 5650 if (! dpyinfo->meta_mod_mask)
a3c44b14 5651 {
334208b7
RS
5652 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
5653 dpyinfo->alt_mod_mask = 0;
a3c44b14 5654 }
f689eb05 5655
148c4b70
RS
5656 /* If some keys are both alt and meta,
5657 make them just meta, not alt. */
334208b7 5658 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
148c4b70 5659 {
334208b7 5660 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
148c4b70 5661 }
58769bee 5662
28430d3c 5663 XFree ((char *) syms);
f689eb05 5664 XFreeModifiermap (mods);
28430d3c
JB
5665}
5666
dfeccd2d
JB
5667/* Convert between the modifier bits X uses and the modifier bits
5668 Emacs uses. */
06a2c219 5669
7c5283e4 5670static unsigned int
334208b7
RS
5671x_x_to_emacs_modifiers (dpyinfo, state)
5672 struct x_display_info *dpyinfo;
dc6f92b8
JB
5673 unsigned int state;
5674{
334208b7
RS
5675 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
5676 | ((state & ControlMask) ? ctrl_modifier : 0)
5677 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
5678 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
5679 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
5680 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
dc6f92b8
JB
5681}
5682
dfeccd2d 5683static unsigned int
334208b7
RS
5684x_emacs_to_x_modifiers (dpyinfo, state)
5685 struct x_display_info *dpyinfo;
dfeccd2d
JB
5686 unsigned int state;
5687{
334208b7
RS
5688 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
5689 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
5690 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
5691 | ((state & shift_modifier) ? ShiftMask : 0)
5692 | ((state & ctrl_modifier) ? ControlMask : 0)
5693 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
dfeccd2d 5694}
d047c4eb
KH
5695
5696/* Convert a keysym to its name. */
5697
5698char *
5699x_get_keysym_name (keysym)
5700 KeySym keysym;
5701{
5702 char *value;
5703
5704 BLOCK_INPUT;
5705 value = XKeysymToString (keysym);
5706 UNBLOCK_INPUT;
5707
5708 return value;
5709}
06a2c219
GM
5710
5711
e4571a43
JB
5712\f
5713/* Mouse clicks and mouse movement. Rah. */
e4571a43 5714
06a2c219
GM
5715/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
5716 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
5717 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
5718 not force the value into range. */
69388238 5719
c8dba240 5720void
69388238 5721pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
e4571a43 5722 FRAME_PTR f;
69388238 5723 register int pix_x, pix_y;
e4571a43
JB
5724 register int *x, *y;
5725 XRectangle *bounds;
69388238 5726 int noclip;
e4571a43 5727{
06a2c219 5728 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
69388238
RS
5729 even for negative values. */
5730 if (pix_x < 0)
7556890b 5731 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
69388238 5732 if (pix_y < 0)
7556890b 5733 pix_y -= (f)->output_data.x->line_height - 1;
69388238 5734
e4571a43
JB
5735 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
5736 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
5737
5738 if (bounds)
5739 {
7556890b
RS
5740 bounds->width = FONT_WIDTH (f->output_data.x->font);
5741 bounds->height = f->output_data.x->line_height;
e4571a43
JB
5742 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
5743 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
5744 }
5745
69388238
RS
5746 if (!noclip)
5747 {
5748 if (pix_x < 0)
5749 pix_x = 0;
3cbd2e0b
RS
5750 else if (pix_x > FRAME_WINDOW_WIDTH (f))
5751 pix_x = FRAME_WINDOW_WIDTH (f);
69388238
RS
5752
5753 if (pix_y < 0)
5754 pix_y = 0;
5755 else if (pix_y > f->height)
5756 pix_y = f->height;
5757 }
e4571a43
JB
5758
5759 *x = pix_x;
5760 *y = pix_y;
5761}
5762
06a2c219
GM
5763
5764/* Given HPOS/VPOS in the current matrix of W, return corresponding
5765 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
5766 can't tell the positions because W's display is not up to date,
5767 return 0. */
5768
5769int
5770glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
5771 struct window *w;
5772 int hpos, vpos;
5773 int *frame_x, *frame_y;
2b5c9e71 5774{
06a2c219
GM
5775 int success_p;
5776
5777 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
5778 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
5779
5780 if (display_completed)
5781 {
5782 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
5783 struct glyph *glyph = row->glyphs[TEXT_AREA];
5784 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
5785
5786 *frame_y = row->y;
5787 *frame_x = row->x;
5788 while (glyph < end)
5789 {
5790 *frame_x += glyph->pixel_width;
5791 ++glyph;
5792 }
5793
5794 success_p = 1;
5795 }
5796 else
5797 {
5798 *frame_y = *frame_x = 0;
5799 success_p = 0;
5800 }
5801
5802 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
5803 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
5804 return success_p;
2b5c9e71
RS
5805}
5806
06a2c219 5807
dc6f92b8
JB
5808/* Prepare a mouse-event in *RESULT for placement in the input queue.
5809
5810 If the event is a button press, then note that we have grabbed
f451eb13 5811 the mouse. */
dc6f92b8
JB
5812
5813static Lisp_Object
f451eb13 5814construct_mouse_click (result, event, f)
dc6f92b8
JB
5815 struct input_event *result;
5816 XButtonEvent *event;
f676886a 5817 struct frame *f;
dc6f92b8 5818{
f451eb13 5819 /* Make the event type no_event; we'll change that when we decide
dc6f92b8 5820 otherwise. */
f451eb13 5821 result->kind = mouse_click;
69388238 5822 result->code = event->button - Button1;
1113d9db 5823 result->timestamp = event->time;
334208b7
RS
5824 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
5825 event->state)
f689eb05 5826 | (event->type == ButtonRelease
58769bee 5827 ? up_modifier
f689eb05 5828 : down_modifier));
dc6f92b8 5829
06a2c219
GM
5830 XSETINT (result->x, event->x);
5831 XSETINT (result->y, event->y);
5832 XSETFRAME (result->frame_or_window, f);
5833 return Qnil;
dc6f92b8 5834}
b849c413 5835
06a2c219
GM
5836#if 0 /* This function isn't called. --gerd */
5837
b849c413
RS
5838/* Prepare a menu-event in *RESULT for placement in the input queue. */
5839
5840static Lisp_Object
5841construct_menu_click (result, event, f)
5842 struct input_event *result;
5843 XButtonEvent *event;
5844 struct frame *f;
5845{
5846 /* Make the event type no_event; we'll change that when we decide
5847 otherwise. */
5848 result->kind = mouse_click;
26459b28 5849 result->code = event->button - Button1;
b849c413 5850 result->timestamp = event->time;
334208b7
RS
5851 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
5852 event->state)
b849c413 5853 | (event->type == ButtonRelease
58769bee 5854 ? up_modifier
b849c413
RS
5855 : down_modifier));
5856
e0c1aef2
KH
5857 XSETINT (result->x, event->x);
5858 XSETINT (result->y, -1);
5859 XSETFRAME (result->frame_or_window, f);
b849c413 5860}
06a2c219
GM
5861
5862#endif /* 0 */
5863
69388238 5864\f
90e65f07
JB
5865/* Function to report a mouse movement to the mainstream Emacs code.
5866 The input handler calls this.
5867
5868 We have received a mouse movement event, which is given in *event.
5869 If the mouse is over a different glyph than it was last time, tell
5870 the mainstream emacs code by setting mouse_moved. If not, ask for
5871 another motion event, so we can check again the next time it moves. */
b8009dd1 5872
06a2c219
GM
5873static XMotionEvent last_mouse_motion_event;
5874static Lisp_Object last_mouse_motion_frame;
5875
90e65f07 5876static void
12ba150f 5877note_mouse_movement (frame, event)
f676886a 5878 FRAME_PTR frame;
90e65f07 5879 XMotionEvent *event;
90e65f07 5880{
e5d77022 5881 last_mouse_movement_time = event->time;
06a2c219
GM
5882 last_mouse_motion_event = *event;
5883 XSETFRAME (last_mouse_motion_frame, frame);
e5d77022 5884
27f338af
RS
5885 if (event->window != FRAME_X_WINDOW (frame))
5886 {
39d8bb4d 5887 frame->mouse_moved = 1;
27f338af 5888 last_mouse_scroll_bar = Qnil;
27f338af 5889 note_mouse_highlight (frame, -1, -1);
27f338af
RS
5890 }
5891
90e65f07 5892 /* Has the mouse moved off the glyph it was on at the last sighting? */
27f338af
RS
5893 else if (event->x < last_mouse_glyph.x
5894 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
5895 || event->y < last_mouse_glyph.y
5896 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
12ba150f 5897 {
39d8bb4d 5898 frame->mouse_moved = 1;
ab648270 5899 last_mouse_scroll_bar = Qnil;
b8009dd1 5900 note_mouse_highlight (frame, event->x, event->y);
90e65f07
JB
5901 }
5902}
5903
bf1c0ba1 5904/* This is used for debugging, to turn off note_mouse_highlight. */
bf1c0ba1 5905
06a2c219
GM
5906 int disable_mouse_highlight;
5907
5908
5909\f
5910/************************************************************************
5911 Mouse Face
5912 ************************************************************************/
5913
5914/* Find the glyph under window-relative coordinates X/Y in window W.
5915 Consider only glyphs from buffer text, i.e. no glyphs from overlay
5916 strings. Return in *HPOS and *VPOS the row and column number of
5917 the glyph found. Return in *AREA the glyph area containing X.
5918 Value is a pointer to the glyph found or null if X/Y is not on
5919 text, or we can't tell because W's current matrix is not up to
5920 date. */
5921
5922static struct glyph *
5923x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
5924 struct window *w;
5925 int x, y;
5926 int *hpos, *vpos, *area;
5927{
5928 struct glyph *glyph, *end;
5929 struct glyph_row *row;
5930 int x0, i, left_area_width;
5931
5932 /* Find row containing Y. Give up if some row is not enabled. */
5933 for (i = 0; i < w->current_matrix->nrows; ++i)
5934 {
5935 row = MATRIX_ROW (w->current_matrix, i);
5936 if (!row->enabled_p)
5937 return NULL;
5938 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
5939 break;
5940 }
5941
5942 *vpos = i;
5943 *hpos = 0;
5944
5945 /* Give up if Y is not in the window. */
5946 if (i == w->current_matrix->nrows)
5947 return NULL;
5948
5949 /* Get the glyph area containing X. */
5950 if (w->pseudo_window_p)
5951 {
5952 *area = TEXT_AREA;
5953 x0 = 0;
5954 }
5955 else
5956 {
5957 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
5958 if (x < left_area_width)
5959 {
5960 *area = LEFT_MARGIN_AREA;
5961 x0 = 0;
5962 }
5963 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
5964 {
5965 *area = TEXT_AREA;
5966 x0 = row->x + left_area_width;
5967 }
5968 else
5969 {
5970 *area = RIGHT_MARGIN_AREA;
5971 x0 = left_area_width + window_box_width (w, TEXT_AREA);
5972 }
5973 }
5974
5975 /* Find glyph containing X. */
5976 glyph = row->glyphs[*area];
5977 end = glyph + row->used[*area];
5978 while (glyph < end)
5979 {
5980 if (x < x0 + glyph->pixel_width)
5981 {
5982 if (w->pseudo_window_p)
5983 break;
5984 else if (BUFFERP (glyph->object))
5985 break;
5986 }
5987
5988 x0 += glyph->pixel_width;
5989 ++glyph;
5990 }
5991
5992 if (glyph == end)
5993 return NULL;
5994
5995 *hpos = glyph - row->glyphs[*area];
5996 return glyph;
5997}
5998
5999
6000/* Convert frame-relative x/y to coordinates relative to window W.
6001 Takes pseudo-windows into account. */
6002
6003static void
6004frame_to_window_pixel_xy (w, x, y)
6005 struct window *w;
6006 int *x, *y;
6007{
6008 if (w->pseudo_window_p)
6009 {
6010 /* A pseudo-window is always full-width, and starts at the
6011 left edge of the frame, plus a frame border. */
6012 struct frame *f = XFRAME (w->frame);
6013 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6014 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6015 }
6016 else
6017 {
6018 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6019 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6020 }
6021}
6022
6023
6024/* Take proper action when mouse has moved to the mode or top line of
6025 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6026 mode line. X is relative to the start of the text display area of
6027 W, so the width of bitmap areas and scroll bars must be subtracted
6028 to get a position relative to the start of the mode line. */
6029
6030static void
6031note_mode_line_highlight (w, x, mode_line_p)
6032 struct window *w;
6033 int x, mode_line_p;
6034{
6035 struct frame *f = XFRAME (w->frame);
6036 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6037 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6038 struct glyph_row *row;
6039
6040 if (mode_line_p)
6041 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6042 else
045dee35 6043 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
06a2c219
GM
6044
6045 if (row->enabled_p)
6046 {
6047 struct glyph *glyph, *end;
6048 Lisp_Object help, map;
6049 int x0;
6050
6051 /* Find the glyph under X. */
6052 glyph = row->glyphs[TEXT_AREA];
6053 end = glyph + row->used[TEXT_AREA];
6054 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
110859fc 6055 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
06a2c219
GM
6056 while (glyph < end
6057 && x >= x0 + glyph->pixel_width)
6058 {
6059 x0 += glyph->pixel_width;
6060 ++glyph;
6061 }
6062
6063 if (glyph < end
6064 && STRINGP (glyph->object)
6065 && XSTRING (glyph->object)->intervals
6066 && glyph->charpos >= 0
6067 && glyph->charpos < XSTRING (glyph->object)->size)
6068 {
6069 /* If we're on a string with `help-echo' text property,
6070 arrange for the help to be displayed. This is done by
6071 setting the global variable help_echo to the help string. */
6072 help = Fget_text_property (make_number (glyph->charpos),
6073 Qhelp_echo, glyph->object);
6074 if (STRINGP (help))
6075 help_echo = help;
6076
6077 /* Change the mouse pointer according to what is under X/Y. */
6078 map = Fget_text_property (make_number (glyph->charpos),
6079 Qlocal_map, glyph->object);
6080 if (!NILP (Fkeymapp (map)))
6081 cursor = f->output_data.x->nontext_cursor;
6082 }
6083 }
6084
6085 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6086}
6087
6088
6089/* Take proper action when the mouse has moved to position X, Y on
6090 frame F as regards highlighting characters that have mouse-face
6091 properties. Also de-highlighting chars where the mouse was before.
27f338af 6092 X and Y can be negative or out of range. */
b8009dd1
RS
6093
6094static void
6095note_mouse_highlight (f, x, y)
06a2c219 6096 struct frame *f;
c32cdd9a 6097 int x, y;
b8009dd1 6098{
06a2c219
GM
6099 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6100 int portion;
b8009dd1
RS
6101 Lisp_Object window;
6102 struct window *w;
6103
06a2c219
GM
6104 /* When a menu is active, don't highlight because this looks odd. */
6105#ifdef USE_X_TOOLKIT
6106 if (popup_activated ())
6107 return;
6108#endif
6109
bf1c0ba1
RS
6110 if (disable_mouse_highlight)
6111 return;
6112
06a2c219
GM
6113 dpyinfo->mouse_face_mouse_x = x;
6114 dpyinfo->mouse_face_mouse_y = y;
6115 dpyinfo->mouse_face_mouse_frame = f;
b8009dd1 6116
06a2c219 6117 if (dpyinfo->mouse_face_defer)
b8009dd1
RS
6118 return;
6119
514e4681
RS
6120 if (gc_in_progress)
6121 {
06a2c219 6122 dpyinfo->mouse_face_deferred_gc = 1;
514e4681
RS
6123 return;
6124 }
6125
b8009dd1 6126 /* Which window is that in? */
06a2c219 6127 window = window_from_coordinates (f, x, y, &portion, 1);
b8009dd1
RS
6128
6129 /* If we were displaying active text in another window, clear that. */
06a2c219
GM
6130 if (! EQ (window, dpyinfo->mouse_face_window))
6131 clear_mouse_face (dpyinfo);
6132
6133 /* Not on a window -> return. */
6134 if (!WINDOWP (window))
6135 return;
6136
6137 /* Convert to window-relative pixel coordinates. */
6138 w = XWINDOW (window);
6139 frame_to_window_pixel_xy (w, &x, &y);
6140
9ea173e8 6141 /* Handle tool-bar window differently since it doesn't display a
06a2c219 6142 buffer. */
9ea173e8 6143 if (EQ (window, f->tool_bar_window))
06a2c219 6144 {
9ea173e8 6145 note_tool_bar_highlight (f, x, y);
06a2c219
GM
6146 return;
6147 }
6148
6149 if (portion == 1 || portion == 3)
6150 {
6151 /* Mouse is on the mode or top line. */
6152 note_mode_line_highlight (w, x, portion == 1);
6153 return;
6154 }
6155 else
6156 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6157 f->output_data.x->text_cursor);
b8009dd1 6158
0cdd0c9f
RS
6159 /* Are we in a window whose display is up to date?
6160 And verify the buffer's text has not changed. */
06a2c219
GM
6161 if (/* Within text portion of the window. */
6162 portion == 0
0cdd0c9f 6163 && EQ (w->window_end_valid, w->buffer)
26459b28
KH
6164 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6165 && (XFASTINT (w->last_overlay_modified)
6166 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
b8009dd1 6167 {
06a2c219
GM
6168 int hpos, vpos, pos, i, area;
6169 struct glyph *glyph;
b8009dd1 6170
06a2c219
GM
6171 /* Find the glyph under X/Y. */
6172 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6173
6174 /* Clear mouse face if X/Y not over text. */
6175 if (glyph == NULL
6176 || area != TEXT_AREA
6177 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
b8009dd1 6178 {
06a2c219
GM
6179 clear_mouse_face (dpyinfo);
6180 return;
6181 }
6182
6183 pos = glyph->charpos;
6184 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6185
6186 /* Check for mouse-face and help-echo. */
6187 {
6188 Lisp_Object mouse_face, overlay, position;
6189 Lisp_Object *overlay_vec;
6190 int len, noverlays;
6191 struct buffer *obuf;
6192 int obegv, ozv;
6193
6194 /* If we get an out-of-range value, return now; avoid an error. */
6195 if (pos > BUF_Z (XBUFFER (w->buffer)))
6196 return;
6197
6198 /* Make the window's buffer temporarily current for
6199 overlays_at and compute_char_face. */
6200 obuf = current_buffer;
6201 current_buffer = XBUFFER (w->buffer);
6202 obegv = BEGV;
6203 ozv = ZV;
6204 BEGV = BEG;
6205 ZV = Z;
6206
6207 /* Is this char mouse-active or does it have help-echo? */
6208 XSETINT (position, pos);
6209
6210 /* Put all the overlays we want in a vector in overlay_vec.
6211 Store the length in len. If there are more than 10, make
6212 enough space for all, and try again. */
6213 len = 10;
6214 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6215 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6216 if (noverlays > len)
6217 {
6218 len = noverlays;
6219 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6220 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6221 }
6222
6223 noverlays = sort_overlays (overlay_vec, noverlays, w);
6224
6225 /* Check mouse-face highlighting. */
6226 if (! (EQ (window, dpyinfo->mouse_face_window)
6227 && vpos >= dpyinfo->mouse_face_beg_row
6228 && vpos <= dpyinfo->mouse_face_end_row
6229 && (vpos > dpyinfo->mouse_face_beg_row
6230 || hpos >= dpyinfo->mouse_face_beg_col)
6231 && (vpos < dpyinfo->mouse_face_end_row
6232 || hpos < dpyinfo->mouse_face_end_col
6233 || dpyinfo->mouse_face_past_end)))
6234 {
6235 /* Clear the display of the old active region, if any. */
6236 clear_mouse_face (dpyinfo);
6237
6238 /* Find the highest priority overlay that has a mouse-face prop. */
6239 overlay = Qnil;
6240 for (i = 0; i < noverlays; i++)
6241 {
6242 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6243 if (!NILP (mouse_face))
6244 {
6245 overlay = overlay_vec[i];
6246 break;
6247 }
6248 }
6249
6250 /* If no overlay applies, get a text property. */
6251 if (NILP (overlay))
6252 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6253
6254 /* Handle the overlay case. */
6255 if (! NILP (overlay))
6256 {
6257 /* Find the range of text around this char that
6258 should be active. */
6259 Lisp_Object before, after;
6260 int ignore;
6261
6262 before = Foverlay_start (overlay);
6263 after = Foverlay_end (overlay);
6264 /* Record this as the current active region. */
6265 fast_find_position (w, XFASTINT (before),
6266 &dpyinfo->mouse_face_beg_col,
6267 &dpyinfo->mouse_face_beg_row,
6268 &dpyinfo->mouse_face_beg_x,
6269 &dpyinfo->mouse_face_beg_y);
6270 dpyinfo->mouse_face_past_end
6271 = !fast_find_position (w, XFASTINT (after),
6272 &dpyinfo->mouse_face_end_col,
6273 &dpyinfo->mouse_face_end_row,
6274 &dpyinfo->mouse_face_end_x,
6275 &dpyinfo->mouse_face_end_y);
6276 dpyinfo->mouse_face_window = window;
6277 dpyinfo->mouse_face_face_id
6278 = face_at_buffer_position (w, pos, 0, 0,
6279 &ignore, pos + 1, 1);
6280
6281 /* Display it as active. */
6282 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6283 }
6284 /* Handle the text property case. */
6285 else if (! NILP (mouse_face))
6286 {
6287 /* Find the range of text around this char that
6288 should be active. */
6289 Lisp_Object before, after, beginning, end;
6290 int ignore;
6291
6292 beginning = Fmarker_position (w->start);
6293 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6294 - XFASTINT (w->window_end_pos)));
6295 before
6296 = Fprevious_single_property_change (make_number (pos + 1),
6297 Qmouse_face,
6298 w->buffer, beginning);
6299 after
6300 = Fnext_single_property_change (position, Qmouse_face,
6301 w->buffer, end);
6302 /* Record this as the current active region. */
6303 fast_find_position (w, XFASTINT (before),
6304 &dpyinfo->mouse_face_beg_col,
6305 &dpyinfo->mouse_face_beg_row,
6306 &dpyinfo->mouse_face_beg_x,
6307 &dpyinfo->mouse_face_beg_y);
6308 dpyinfo->mouse_face_past_end
6309 = !fast_find_position (w, XFASTINT (after),
6310 &dpyinfo->mouse_face_end_col,
6311 &dpyinfo->mouse_face_end_row,
6312 &dpyinfo->mouse_face_end_x,
6313 &dpyinfo->mouse_face_end_y);
6314 dpyinfo->mouse_face_window = window;
6315 dpyinfo->mouse_face_face_id
6316 = face_at_buffer_position (w, pos, 0, 0,
6317 &ignore, pos + 1, 1);
6318
6319 /* Display it as active. */
6320 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6321 }
6322 }
6323
6324 /* Look for a `help-echo' property. */
6325 {
6326 Lisp_Object help;
6327
6328 /* Check overlays first. */
6329 help = Qnil;
6330 for (i = 0; i < noverlays && !STRINGP (help); ++i)
6331 help = Foverlay_get (overlay_vec[i], Qhelp_echo);
6332
6333 /* Try text properties. */
6334 if (!STRINGP (help)
6335 && ((STRINGP (glyph->object)
6336 && glyph->charpos >= 0
6337 && glyph->charpos < XSTRING (glyph->object)->size)
6338 || (BUFFERP (glyph->object)
6339 && glyph->charpos >= BEGV
6340 && glyph->charpos < ZV)))
6341 help = Fget_text_property (make_number (glyph->charpos),
6342 Qhelp_echo, glyph->object);
6343
6344 if (STRINGP (help))
6345 help_echo = help;
6346 }
6347
6348 BEGV = obegv;
6349 ZV = ozv;
6350 current_buffer = obuf;
6351 }
6352 }
6353}
6354
6355static void
6356redo_mouse_highlight ()
6357{
6358 if (!NILP (last_mouse_motion_frame)
6359 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6360 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6361 last_mouse_motion_event.x,
6362 last_mouse_motion_event.y);
6363}
6364
6365
6366\f
6367/***********************************************************************
9ea173e8 6368 Tool-bars
06a2c219
GM
6369 ***********************************************************************/
6370
9ea173e8
GM
6371static int x_tool_bar_item P_ ((struct frame *, int, int,
6372 struct glyph **, int *, int *, int *));
06a2c219 6373
9ea173e8 6374/* Tool-bar item index of the item on which a mouse button was pressed
06a2c219
GM
6375 or -1. */
6376
9ea173e8 6377static int last_tool_bar_item;
06a2c219
GM
6378
6379
9ea173e8
GM
6380/* Get information about the tool-bar item at position X/Y on frame F.
6381 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6382 the current matrix of the tool-bar window of F, or NULL if not
6383 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
6384 item in F->current_tool_bar_items. Value is
06a2c219 6385
9ea173e8 6386 -1 if X/Y is not on a tool-bar item
06a2c219
GM
6387 0 if X/Y is on the same item that was highlighted before.
6388 1 otherwise. */
6389
6390static int
9ea173e8 6391x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
06a2c219
GM
6392 struct frame *f;
6393 int x, y;
6394 struct glyph **glyph;
6395 int *hpos, *vpos, *prop_idx;
6396{
6397 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6398 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6399 int area;
6400
6401 /* Find the glyph under X/Y. */
6402 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6403 if (*glyph == NULL)
6404 return -1;
6405
9ea173e8
GM
6406 /* Get the start of this tool-bar item's properties in
6407 f->current_tool_bar_items. */
6408 if (!tool_bar_item_info (f, *glyph, prop_idx))
06a2c219
GM
6409 return -1;
6410
6411 /* Is mouse on the highlighted item? */
9ea173e8 6412 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
06a2c219
GM
6413 && *vpos >= dpyinfo->mouse_face_beg_row
6414 && *vpos <= dpyinfo->mouse_face_end_row
6415 && (*vpos > dpyinfo->mouse_face_beg_row
6416 || *hpos >= dpyinfo->mouse_face_beg_col)
6417 && (*vpos < dpyinfo->mouse_face_end_row
6418 || *hpos < dpyinfo->mouse_face_end_col
6419 || dpyinfo->mouse_face_past_end))
6420 return 0;
6421
6422 return 1;
6423}
6424
6425
9ea173e8 6426/* Handle mouse button event on the tool-bar of frame F, at
06a2c219
GM
6427 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6428 or ButtonRelase. */
6429
6430static void
9ea173e8 6431x_handle_tool_bar_click (f, button_event)
06a2c219
GM
6432 struct frame *f;
6433 XButtonEvent *button_event;
6434{
6435 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6436 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6437 int hpos, vpos, prop_idx;
6438 struct glyph *glyph;
6439 Lisp_Object enabled_p;
6440 int x = button_event->x;
6441 int y = button_event->y;
6442
9ea173e8 6443 /* If not on the highlighted tool-bar item, return. */
06a2c219 6444 frame_to_window_pixel_xy (w, &x, &y);
9ea173e8 6445 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
06a2c219
GM
6446 return;
6447
6448 /* If item is disabled, do nothing. */
9ea173e8
GM
6449 enabled_p = (XVECTOR (f->current_tool_bar_items)
6450 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
06a2c219
GM
6451 if (NILP (enabled_p))
6452 return;
6453
6454 if (button_event->type == ButtonPress)
6455 {
6456 /* Show item in pressed state. */
6457 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6458 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9ea173e8 6459 last_tool_bar_item = prop_idx;
06a2c219
GM
6460 }
6461 else
6462 {
6463 Lisp_Object key, frame;
6464 struct input_event event;
6465
6466 /* Show item in released state. */
6467 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6468 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6469
9ea173e8
GM
6470 key = (XVECTOR (f->current_tool_bar_items)
6471 ->contents[prop_idx + TOOL_BAR_ITEM_KEY]);
06a2c219
GM
6472
6473 XSETFRAME (frame, f);
9ea173e8
GM
6474 event.kind = TOOL_BAR_EVENT;
6475 event.frame_or_window = Fcons (frame, Fcons (Qtool_bar, Qnil));
06a2c219
GM
6476 kbd_buffer_store_event (&event);
6477
9ea173e8 6478 event.kind = TOOL_BAR_EVENT;
06a2c219
GM
6479 event.frame_or_window = Fcons (frame, key);
6480 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6481 button_event->state);
6482 kbd_buffer_store_event (&event);
9ea173e8 6483 last_tool_bar_item = -1;
06a2c219
GM
6484 }
6485}
6486
6487
9ea173e8
GM
6488/* Possibly highlight a tool-bar item on frame F when mouse moves to
6489 tool-bar window-relative coordinates X/Y. Called from
06a2c219
GM
6490 note_mouse_highlight. */
6491
6492static void
9ea173e8 6493note_tool_bar_highlight (f, x, y)
06a2c219
GM
6494 struct frame *f;
6495 int x, y;
6496{
9ea173e8 6497 Lisp_Object window = f->tool_bar_window;
06a2c219
GM
6498 struct window *w = XWINDOW (window);
6499 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6500 int hpos, vpos;
6501 struct glyph *glyph;
6502 struct glyph_row *row;
5c187dee 6503 int i;
06a2c219
GM
6504 Lisp_Object enabled_p;
6505 int prop_idx;
6506 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
5c187dee 6507 int mouse_down_p, rc;
06a2c219
GM
6508
6509 /* Function note_mouse_highlight is called with negative x(y
6510 values when mouse moves outside of the frame. */
6511 if (x <= 0 || y <= 0)
6512 {
6513 clear_mouse_face (dpyinfo);
6514 return;
6515 }
6516
9ea173e8 6517 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
06a2c219
GM
6518 if (rc < 0)
6519 {
9ea173e8 6520 /* Not on tool-bar item. */
06a2c219
GM
6521 clear_mouse_face (dpyinfo);
6522 return;
6523 }
6524 else if (rc == 0)
9ea173e8 6525 /* On same tool-bar item as before. */
06a2c219 6526 goto set_help_echo;
b8009dd1 6527
06a2c219
GM
6528 clear_mouse_face (dpyinfo);
6529
9ea173e8 6530 /* Mouse is down, but on different tool-bar item? */
06a2c219
GM
6531 mouse_down_p = (dpyinfo->grabbed
6532 && f == last_mouse_frame
6533 && FRAME_LIVE_P (f));
6534 if (mouse_down_p
9ea173e8 6535 && last_tool_bar_item != prop_idx)
06a2c219
GM
6536 return;
6537
6538 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
6539 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
6540
9ea173e8
GM
6541 /* If tool-bar item is not enabled, don't highlight it. */
6542 enabled_p = (XVECTOR (f->current_tool_bar_items)
6543 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
06a2c219
GM
6544 if (!NILP (enabled_p))
6545 {
6546 /* Compute the x-position of the glyph. In front and past the
6547 image is a space. We include this is the highlighted area. */
6548 row = MATRIX_ROW (w->current_matrix, vpos);
6549 for (i = x = 0; i < hpos; ++i)
6550 x += row->glyphs[TEXT_AREA][i].pixel_width;
6551
6552 /* Record this as the current active region. */
6553 dpyinfo->mouse_face_beg_col = hpos;
6554 dpyinfo->mouse_face_beg_row = vpos;
6555 dpyinfo->mouse_face_beg_x = x;
6556 dpyinfo->mouse_face_beg_y = row->y;
6557 dpyinfo->mouse_face_past_end = 0;
6558
6559 dpyinfo->mouse_face_end_col = hpos + 1;
6560 dpyinfo->mouse_face_end_row = vpos;
6561 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
6562 dpyinfo->mouse_face_end_y = row->y;
6563 dpyinfo->mouse_face_window = window;
9ea173e8 6564 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
06a2c219
GM
6565
6566 /* Display it as active. */
6567 show_mouse_face (dpyinfo, draw);
6568 dpyinfo->mouse_face_image_state = draw;
b8009dd1 6569 }
06a2c219
GM
6570
6571 set_help_echo:
6572
9ea173e8 6573 /* Set help_echo to a help string.to display for this tool-bar item.
06a2c219 6574 XTread_socket does the rest. */
9ea173e8
GM
6575 help_echo = (XVECTOR (f->current_tool_bar_items)
6576 ->contents[prop_idx + TOOL_BAR_ITEM_HELP]);
06a2c219 6577 if (!STRINGP (help_echo))
9ea173e8
GM
6578 help_echo = (XVECTOR (f->current_tool_bar_items)
6579 ->contents[prop_idx + TOOL_BAR_ITEM_CAPTION]);
b8009dd1 6580}
4d73d038 6581
06a2c219
GM
6582
6583\f
6584/* Find the glyph matrix position of buffer position POS in window W.
6585 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
6586 current glyphs must be up to date. If POS is above window start
6587 return (0, 0, 0, 0). If POS is after end of W, return end of
6588 last line in W. */
b8009dd1
RS
6589
6590static int
06a2c219
GM
6591fast_find_position (w, pos, hpos, vpos, x, y)
6592 struct window *w;
b8009dd1 6593 int pos;
06a2c219 6594 int *hpos, *vpos, *x, *y;
b8009dd1 6595{
b8009dd1 6596 int i;
bf1c0ba1 6597 int lastcol;
06a2c219
GM
6598 int maybe_next_line_p = 0;
6599 int line_start_position;
6600 int yb = window_text_bottom_y (w);
6601 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
6602 struct glyph_row *best_row = row;
6603 int row_vpos = 0, best_row_vpos = 0;
6604 int current_x;
6605
6606 while (row->y < yb)
b8009dd1 6607 {
06a2c219
GM
6608 if (row->used[TEXT_AREA])
6609 line_start_position = row->glyphs[TEXT_AREA]->charpos;
6610 else
6611 line_start_position = 0;
6612
6613 if (line_start_position > pos)
b8009dd1 6614 break;
77b68646
RS
6615 /* If the position sought is the end of the buffer,
6616 don't include the blank lines at the bottom of the window. */
06a2c219
GM
6617 else if (line_start_position == pos
6618 && pos == BUF_ZV (XBUFFER (w->buffer)))
77b68646 6619 {
06a2c219 6620 maybe_next_line_p = 1;
77b68646
RS
6621 break;
6622 }
06a2c219
GM
6623 else if (line_start_position > 0)
6624 {
6625 best_row = row;
6626 best_row_vpos = row_vpos;
6627 }
6628
6629 ++row;
6630 ++row_vpos;
b8009dd1 6631 }
06a2c219
GM
6632
6633 /* Find the right column within BEST_ROW. */
6634 lastcol = 0;
6635 current_x = best_row->x;
6636 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
bf1c0ba1 6637 {
06a2c219
GM
6638 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
6639 int charpos;
6640
6641 charpos = glyph->charpos;
6642 if (charpos == pos)
bf1c0ba1 6643 {
06a2c219
GM
6644 *hpos = i;
6645 *vpos = best_row_vpos;
6646 *x = current_x;
6647 *y = best_row->y;
bf1c0ba1
RS
6648 return 1;
6649 }
06a2c219 6650 else if (charpos > pos)
4d73d038 6651 break;
06a2c219
GM
6652 else if (charpos > 0)
6653 lastcol = i;
6654
6655 current_x += glyph->pixel_width;
bf1c0ba1 6656 }
b8009dd1 6657
77b68646
RS
6658 /* If we're looking for the end of the buffer,
6659 and we didn't find it in the line we scanned,
6660 use the start of the following line. */
06a2c219 6661 if (maybe_next_line_p)
77b68646 6662 {
06a2c219
GM
6663 ++best_row;
6664 ++best_row_vpos;
6665 lastcol = 0;
6666 current_x = best_row->x;
77b68646
RS
6667 }
6668
06a2c219
GM
6669 *vpos = best_row_vpos;
6670 *hpos = lastcol + 1;
6671 *x = current_x;
6672 *y = best_row->y;
b8009dd1
RS
6673 return 0;
6674}
6675
06a2c219 6676
b8009dd1
RS
6677/* Display the active region described by mouse_face_*
6678 in its mouse-face if HL > 0, in its normal face if HL = 0. */
6679
6680static void
06a2c219 6681show_mouse_face (dpyinfo, draw)
7a13e894 6682 struct x_display_info *dpyinfo;
06a2c219 6683 enum draw_glyphs_face draw;
b8009dd1 6684{
7a13e894 6685 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
06a2c219 6686 struct frame *f = XFRAME (WINDOW_FRAME (w));
b8009dd1 6687 int i;
06a2c219
GM
6688 int cursor_off_p = 0;
6689 struct cursor_pos saved_cursor;
6690
6691 saved_cursor = output_cursor;
6692
6693 /* If window is in the process of being destroyed, don't bother
6694 to do anything. */
6695 if (w->current_matrix == NULL)
6696 goto set_x_cursor;
6697
6698 /* Recognize when we are called to operate on rows that don't exist
6699 anymore. This can happen when a window is split. */
6700 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
6701 goto set_x_cursor;
6702
6703 set_output_cursor (&w->phys_cursor);
6704
6705 /* Note that mouse_face_beg_row etc. are window relative. */
6706 for (i = dpyinfo->mouse_face_beg_row;
6707 i <= dpyinfo->mouse_face_end_row;
6708 i++)
6709 {
6710 int start_hpos, end_hpos, start_x;
6711 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
6712
6713 /* Don't do anything if row doesn't have valid contents. */
6714 if (!row->enabled_p)
6715 continue;
6716
6717 /* For all but the first row, the highlight starts at column 0. */
6718 if (i == dpyinfo->mouse_face_beg_row)
6719 {
6720 start_hpos = dpyinfo->mouse_face_beg_col;
6721 start_x = dpyinfo->mouse_face_beg_x;
6722 }
6723 else
6724 {
6725 start_hpos = 0;
6726 start_x = 0;
6727 }
6728
6729 if (i == dpyinfo->mouse_face_end_row)
6730 end_hpos = dpyinfo->mouse_face_end_col;
6731 else
6732 end_hpos = row->used[TEXT_AREA];
6733
6734 /* If the cursor's in the text we are about to rewrite, turn the
6735 cursor off. */
6736 if (!w->pseudo_window_p
6737 && i == output_cursor.vpos
6738 && output_cursor.hpos >= start_hpos - 1
6739 && output_cursor.hpos <= end_hpos)
514e4681 6740 {
06a2c219
GM
6741 x_update_window_cursor (w, 0);
6742 cursor_off_p = 1;
514e4681 6743 }
b8009dd1 6744
06a2c219
GM
6745 if (end_hpos > start_hpos)
6746 x_draw_glyphs (w, start_x, row, updated_area,
66ac4b0e 6747 start_hpos, end_hpos, draw, NULL, NULL, 0);
b8009dd1
RS
6748 }
6749
514e4681 6750 /* If we turned the cursor off, turn it back on. */
06a2c219
GM
6751 if (cursor_off_p)
6752 x_display_cursor (w, 1,
6753 output_cursor.hpos, output_cursor.vpos,
6754 output_cursor.x, output_cursor.y);
2729a2b5 6755
06a2c219 6756 output_cursor = saved_cursor;
fb3b7de5 6757
06a2c219
GM
6758 set_x_cursor:
6759
6760 /* Change the mouse cursor. */
6761 if (draw == DRAW_NORMAL_TEXT)
6762 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6763 f->output_data.x->text_cursor);
6764 else if (draw == DRAW_MOUSE_FACE)
334208b7 6765 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 6766 f->output_data.x->cross_cursor);
27ead1d5 6767 else
334208b7 6768 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
06a2c219 6769 f->output_data.x->nontext_cursor);
b8009dd1
RS
6770}
6771
6772/* Clear out the mouse-highlighted active region.
06a2c219 6773 Redraw it un-highlighted first. */
b8009dd1 6774
06a2c219 6775void
7a13e894
RS
6776clear_mouse_face (dpyinfo)
6777 struct x_display_info *dpyinfo;
b8009dd1 6778{
06a2c219
GM
6779 if (tip_frame)
6780 return;
6781
7a13e894 6782 if (! NILP (dpyinfo->mouse_face_window))
06a2c219 6783 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
b8009dd1 6784
7a13e894
RS
6785 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
6786 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
6787 dpyinfo->mouse_face_window = Qnil;
b8009dd1 6788}
e687d06e
RS
6789
6790/* Just discard the mouse face information for frame F, if any.
6791 This is used when the size of F is changed. */
6792
dfcf069d 6793void
e687d06e
RS
6794cancel_mouse_face (f)
6795 FRAME_PTR f;
6796{
6797 Lisp_Object window;
6798 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6799
6800 window = dpyinfo->mouse_face_window;
6801 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
6802 {
6803 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
6804 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
6805 dpyinfo->mouse_face_window = Qnil;
6806 }
6807}
b8009dd1 6808\f
ab648270
JB
6809static struct scroll_bar *x_window_to_scroll_bar ();
6810static void x_scroll_bar_report_motion ();
12ba150f 6811
90e65f07 6812/* Return the current position of the mouse.
2d7fc7e8 6813 *fp should be a frame which indicates which display to ask about.
90e65f07 6814
2d7fc7e8 6815 If the mouse movement started in a scroll bar, set *fp, *bar_window,
ab648270 6816 and *part to the frame, window, and scroll bar part that the mouse
12ba150f 6817 is over. Set *x and *y to the portion and whole of the mouse's
ab648270 6818 position on the scroll bar.
12ba150f 6819
2d7fc7e8 6820 If the mouse movement started elsewhere, set *fp to the frame the
12ba150f
JB
6821 mouse is on, *bar_window to nil, and *x and *y to the character cell
6822 the mouse is over.
6823
06a2c219 6824 Set *time to the server time-stamp for the time at which the mouse
12ba150f
JB
6825 was at this position.
6826
a135645a
RS
6827 Don't store anything if we don't have a valid set of values to report.
6828
90e65f07 6829 This clears the mouse_moved flag, so we can wait for the next mouse
f5bb65ec 6830 movement. */
90e65f07
JB
6831
6832static void
1cf412ec 6833XTmouse_position (fp, insist, bar_window, part, x, y, time)
334208b7 6834 FRAME_PTR *fp;
1cf412ec 6835 int insist;
12ba150f 6836 Lisp_Object *bar_window;
ab648270 6837 enum scroll_bar_part *part;
90e65f07 6838 Lisp_Object *x, *y;
e5d77022 6839 unsigned long *time;
90e65f07 6840{
a135645a
RS
6841 FRAME_PTR f1;
6842
90e65f07
JB
6843 BLOCK_INPUT;
6844
8bcee03e 6845 if (! NILP (last_mouse_scroll_bar) && insist == 0)
334208b7 6846 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
90e65f07
JB
6847 else
6848 {
12ba150f
JB
6849 Window root;
6850 int root_x, root_y;
90e65f07 6851
12ba150f
JB
6852 Window dummy_window;
6853 int dummy;
6854
39d8bb4d
KH
6855 Lisp_Object frame, tail;
6856
6857 /* Clear the mouse-moved flag for every frame on this display. */
6858 FOR_EACH_FRAME (tail, frame)
6859 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
6860 XFRAME (frame)->mouse_moved = 0;
6861
ab648270 6862 last_mouse_scroll_bar = Qnil;
12ba150f
JB
6863
6864 /* Figure out which root window we're on. */
334208b7
RS
6865 XQueryPointer (FRAME_X_DISPLAY (*fp),
6866 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
12ba150f
JB
6867
6868 /* The root window which contains the pointer. */
6869 &root,
6870
6871 /* Trash which we can't trust if the pointer is on
6872 a different screen. */
6873 &dummy_window,
6874
6875 /* The position on that root window. */
58769bee 6876 &root_x, &root_y,
12ba150f
JB
6877
6878 /* More trash we can't trust. */
6879 &dummy, &dummy,
6880
6881 /* Modifier keys and pointer buttons, about which
6882 we don't care. */
6883 (unsigned int *) &dummy);
6884
6885 /* Now we have a position on the root; find the innermost window
6886 containing the pointer. */
6887 {
6888 Window win, child;
6889 int win_x, win_y;
06a2c219 6890 int parent_x = 0, parent_y = 0;
e99db5a1 6891 int count;
12ba150f
JB
6892
6893 win = root;
69388238 6894
2d7fc7e8
RS
6895 /* XTranslateCoordinates can get errors if the window
6896 structure is changing at the same time this function
6897 is running. So at least we must not crash from them. */
6898
e99db5a1 6899 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
2d7fc7e8 6900
334208b7 6901 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
23faf38f 6902 && FRAME_LIVE_P (last_mouse_frame))
12ba150f 6903 {
69388238
RS
6904 /* If mouse was grabbed on a frame, give coords for that frame
6905 even if the mouse is now outside it. */
334208b7 6906 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
69388238 6907
12ba150f 6908 /* From-window, to-window. */
69388238 6909 root, FRAME_X_WINDOW (last_mouse_frame),
12ba150f
JB
6910
6911 /* From-position, to-position. */
6912 root_x, root_y, &win_x, &win_y,
6913
6914 /* Child of win. */
6915 &child);
69388238
RS
6916 f1 = last_mouse_frame;
6917 }
6918 else
6919 {
6920 while (1)
6921 {
334208b7 6922 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
12ba150f 6923
69388238
RS
6924 /* From-window, to-window. */
6925 root, win,
12ba150f 6926
69388238
RS
6927 /* From-position, to-position. */
6928 root_x, root_y, &win_x, &win_y,
6929
6930 /* Child of win. */
6931 &child);
6932
9af3143a 6933 if (child == None || child == win)
69388238
RS
6934 break;
6935
6936 win = child;
6937 parent_x = win_x;
6938 parent_y = win_y;
6939 }
12ba150f 6940
69388238
RS
6941 /* Now we know that:
6942 win is the innermost window containing the pointer
6943 (XTC says it has no child containing the pointer),
6944 win_x and win_y are the pointer's position in it
6945 (XTC did this the last time through), and
6946 parent_x and parent_y are the pointer's position in win's parent.
6947 (They are what win_x and win_y were when win was child.
6948 If win is the root window, it has no parent, and
6949 parent_{x,y} are invalid, but that's okay, because we'll
6950 never use them in that case.) */
6951
6952 /* Is win one of our frames? */
19126e11 6953 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
69388238 6954 }
58769bee 6955
2d7fc7e8
RS
6956 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
6957 f1 = 0;
6958
e99db5a1 6959 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
2d7fc7e8 6960
ab648270 6961 /* If not, is it one of our scroll bars? */
a135645a 6962 if (! f1)
12ba150f 6963 {
ab648270 6964 struct scroll_bar *bar = x_window_to_scroll_bar (win);
12ba150f
JB
6965
6966 if (bar)
6967 {
a135645a 6968 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
6969 win_x = parent_x;
6970 win_y = parent_y;
6971 }
6972 }
90e65f07 6973
8bcee03e 6974 if (f1 == 0 && insist > 0)
b86bd3dd 6975 f1 = SELECTED_FRAME ();
1cf412ec 6976
a135645a 6977 if (f1)
12ba150f 6978 {
06a2c219
GM
6979 /* Ok, we found a frame. Store all the values.
6980 last_mouse_glyph is a rectangle used to reduce the
6981 generation of mouse events. To not miss any motion
6982 events, we must divide the frame into rectangles of the
6983 size of the smallest character that could be displayed
6984 on it, i.e. into the same rectangles that matrices on
6985 the frame are divided into. */
6986
6987#if OLD_REDISPLAY_CODE
2b5c9e71 6988 int ignore1, ignore2;
2b5c9e71 6989 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
334208b7 6990 &last_mouse_glyph,
1cf412ec
RS
6991 FRAME_X_DISPLAY_INFO (f1)->grabbed
6992 || insist);
06a2c219
GM
6993#else
6994 {
6995 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
6996 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
6997 int x = win_x;
6998 int y = win_y;
6999
7000 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7001 round down even for negative values. */
7002 if (x < 0)
7003 x -= width - 1;
7004 if (y < 0)
7005 y -= height - 1;
7006
7007 last_mouse_glyph.width = width;
7008 last_mouse_glyph.height = height;
7009 last_mouse_glyph.x = (x + width - 1) / width * width;
7010 last_mouse_glyph.y = (y + height - 1) / height * height;
7011 }
7012#endif
12ba150f
JB
7013
7014 *bar_window = Qnil;
7015 *part = 0;
334208b7 7016 *fp = f1;
e0c1aef2
KH
7017 XSETINT (*x, win_x);
7018 XSETINT (*y, win_y);
12ba150f
JB
7019 *time = last_mouse_movement_time;
7020 }
7021 }
7022 }
90e65f07
JB
7023
7024 UNBLOCK_INPUT;
7025}
f451eb13 7026
06a2c219
GM
7027
7028DEFUN ("xt-process-timeouts", Fxt_process_timeouts, Sxt_process_timeouts,
7029 0, 0, 0,
7030 "Arrange for Xt timeout callbacks to be called.")
7031 ()
7032{
7033#ifdef USE_X_TOOLKIT
7034 BLOCK_INPUT;
7035 while (XtAppPending (Xt_app_con) & XtIMTimer)
7036 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7037 UNBLOCK_INPUT;
7038#endif /* USE_X_TOOLKIT */
7039
7040 return Qnil;
7041}
7042
7043
7044\f
7045/* Scroll bar support. */
7046
7047/* Given an X window ID, find the struct scroll_bar which manages it.
7048 This can be called in GC, so we have to make sure to strip off mark
7049 bits. */
7050static struct scroll_bar *
7051x_window_to_scroll_bar (window_id)
7052 Window window_id;
7053{
7054 Lisp_Object tail;
7055
7056 for (tail = Vframe_list;
7057 XGCTYPE (tail) == Lisp_Cons;
8e713be6 7058 tail = XCDR (tail))
06a2c219
GM
7059 {
7060 Lisp_Object frame, bar, condemned;
7061
8e713be6 7062 frame = XCAR (tail);
06a2c219
GM
7063 /* All elements of Vframe_list should be frames. */
7064 if (! GC_FRAMEP (frame))
7065 abort ();
7066
7067 /* Scan this frame's scroll bar list for a scroll bar with the
7068 right window ID. */
7069 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7070 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7071 /* This trick allows us to search both the ordinary and
7072 condemned scroll bar lists with one loop. */
7073 ! GC_NILP (bar) || (bar = condemned,
7074 condemned = Qnil,
7075 ! GC_NILP (bar));
7076 bar = XSCROLL_BAR (bar)->next)
7077 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7078 return XSCROLL_BAR (bar);
7079 }
7080
7081 return 0;
7082}
7083
7084
7085\f
7086/************************************************************************
7087 Toolkit scroll bars
7088 ************************************************************************/
7089
7090#if USE_TOOLKIT_SCROLL_BARS
7091
7092static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7093static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7094static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7095 struct scroll_bar *));
7096static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7097 int, int, int));
7098
7099
7100/* Id of action hook installed for scroll bars. */
7101
7102static XtActionHookId action_hook_id;
7103
7104/* Lisp window being scrolled. Set when starting to interact with
7105 a toolkit scroll bar, reset to nil when ending the interaction. */
7106
7107static Lisp_Object window_being_scrolled;
7108
7109/* Last scroll bar part sent in xm_scroll_callback. */
7110
7111static int last_scroll_bar_part;
7112
7113
7114/* Action hook installed via XtAppAddActionHook when toolkit scroll
7115 bars are used.. The hoos is responsible for detecting when
7116 the user ends an interaction with the scroll bar, and generates
7117 a `end-scroll' scroll_bar_click' event if so. */
7118
7119static void
7120xt_action_hook (widget, client_data, action_name, event, params,
7121 num_params)
7122 Widget widget;
7123 XtPointer client_data;
7124 String action_name;
7125 XEvent *event;
7126 String *params;
7127 Cardinal *num_params;
7128{
7129 int scroll_bar_p;
7130 char *end_action;
7131
7132#ifdef USE_MOTIF
7133 scroll_bar_p = XmIsScrollBar (widget);
7134 end_action = "Release";
7135#elif defined HAVE_XAW3D
7136 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7137 end_action = "EndScroll";
7138#else
8abee2e1 7139 #error unknown scroll bar toolkit
06a2c219
GM
7140#endif /* HAVE_XAW3D */
7141
7142 /* Although LessTif uses XtTimeouts like Xaw3d, the timer hack to
7143 let Xt timeouts be processed doesn't work. */
7144 if (scroll_bar_p
7145 && strcmp (action_name, end_action) == 0
7146 && WINDOWP (window_being_scrolled))
7147 {
7148 struct window *w;
7149
7150 x_send_scroll_bar_event (window_being_scrolled,
7151 scroll_bar_end_scroll, 0, 0);
7152 w = XWINDOW (window_being_scrolled);
7153 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7154 window_being_scrolled = Qnil;
7155 last_scroll_bar_part = -1;
7156 }
7157}
7158
7159
7160/* Send a client message with message type Xatom_Scrollbar for a
7161 scroll action to the frame of WINDOW. PART is a value identifying
7162 the part of the scroll bar that was clicked on. PORTION is the
7163 amount to scroll of a whole of WHOLE. */
7164
7165static void
7166x_send_scroll_bar_event (window, part, portion, whole)
7167 Lisp_Object window;
7168 int part, portion, whole;
7169{
7170 XEvent event;
7171 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
7172 struct frame *f = XFRAME (XWINDOW (window)->frame);
7173
7174 /* Construct a ClientMessage event to send to the frame. */
7175 ev->type = ClientMessage;
7176 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7177 ev->display = FRAME_X_DISPLAY (f);
7178 ev->window = FRAME_X_WINDOW (f);
7179 ev->format = 32;
7180 ev->data.l[0] = (long) window;
7181 ev->data.l[1] = (long) part;
7182 ev->data.l[2] = (long) 0;
7183 ev->data.l[3] = (long) portion;
7184 ev->data.l[4] = (long) whole;
7185
7186 /* Setting the event mask to zero means that the message will
7187 be sent to the client that created the window, and if that
7188 window no longer exists, no event will be sent. */
7189 BLOCK_INPUT;
7190 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7191 UNBLOCK_INPUT;
7192}
7193
7194
7195/* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7196 in *IEVENT. */
7197
7198static void
7199x_scroll_bar_to_input_event (event, ievent)
7200 XEvent *event;
7201 struct input_event *ievent;
7202{
7203 XClientMessageEvent *ev = (XClientMessageEvent *) event;
7204 Lisp_Object window = (Lisp_Object) ev->data.l[0];
7205 struct frame *f = XFRAME (XWINDOW (window)->frame);
7206
7207 ievent->kind = scroll_bar_click;
7208 ievent->frame_or_window = window;
7209 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7210 ievent->part = ev->data.l[1];
7211 ievent->code = ev->data.l[2];
7212 ievent->x = make_number ((int) ev->data.l[3]);
7213 ievent->y = make_number ((int) ev->data.l[4]);
7214 ievent->modifiers = 0;
7215}
7216
7217
7218#ifdef USE_MOTIF
7219
7220/* Minimum and maximum values used for Motif scroll bars. */
7221
7222#define XM_SB_MIN 1
7223#define XM_SB_MAX 10000000
7224#define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7225
7226
7227/* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7228 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7229 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7230
7231static void
7232xm_scroll_callback (widget, client_data, call_data)
7233 Widget widget;
7234 XtPointer client_data, call_data;
7235{
7236 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7237 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7238 double percent;
7239 int part = -1, whole = 0, portion = 0;
7240
7241 switch (cs->reason)
7242 {
7243 case XmCR_DECREMENT:
7244 bar->dragging = Qnil;
7245 part = scroll_bar_up_arrow;
7246 break;
7247
7248 case XmCR_INCREMENT:
7249 bar->dragging = Qnil;
7250 part = scroll_bar_down_arrow;
7251 break;
7252
7253 case XmCR_PAGE_DECREMENT:
7254 bar->dragging = Qnil;
7255 part = scroll_bar_above_handle;
7256 break;
7257
7258 case XmCR_PAGE_INCREMENT:
7259 bar->dragging = Qnil;
7260 part = scroll_bar_below_handle;
7261 break;
7262
7263 case XmCR_TO_TOP:
7264 bar->dragging = Qnil;
7265 part = scroll_bar_to_top;
7266 break;
7267
7268 case XmCR_TO_BOTTOM:
7269 bar->dragging = Qnil;
7270 part = scroll_bar_to_bottom;
7271 break;
7272
7273 case XmCR_DRAG:
7274 {
7275 int slider_size;
7276 int dragging_down_p = (INTEGERP (bar->dragging)
7277 && XINT (bar->dragging) <= cs->value);
7278
7279 /* Get the slider size. */
7280 BLOCK_INPUT;
7281 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7282 UNBLOCK_INPUT;
7283
7284 /* At the max position of the scroll bar, do a line-wise
7285 movement. Without doing anything, the LessTif scroll bar
7286 calls us with the same cs->value again and again. If we
7287 want to make sure that we can reach the end of the buffer,
7288 we have to do something.
7289
7290 Implementation note: setting bar->dragging always to
7291 cs->value gives a smoother movement at the max position.
7292 Setting it to nil when doing line-wise movement gives
7293 a better slider behavior. */
7294
7295 if (cs->value + slider_size == XM_SB_MAX
7296 || (dragging_down_p
7297 && last_scroll_bar_part == scroll_bar_down_arrow))
7298 {
7299 part = scroll_bar_down_arrow;
7300 bar->dragging = Qnil;
7301 }
7302 else
7303 {
7304 whole = XM_SB_RANGE;
7305 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7306 part = scroll_bar_handle;
7307 bar->dragging = make_number (cs->value);
7308 }
7309 }
7310 break;
7311
7312 case XmCR_VALUE_CHANGED:
7313 break;
7314 };
7315
7316 if (part >= 0)
7317 {
7318 window_being_scrolled = bar->window;
7319 last_scroll_bar_part = part;
7320 x_send_scroll_bar_event (bar->window, part, portion, whole);
7321 }
7322}
7323
7324
7325#else /* not USE_MOTIF, i.e. XAW3D. */
7326
7327
7328/* Xaw3d scroll bar callback. Invoked when the thumb is dragged.
7329 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7330 scroll bar struct. CALL_DATA is a pointer to a float saying where
7331 the thumb is. */
7332
7333static void
7334xaw3d_jump_callback (widget, client_data, call_data)
7335 Widget widget;
7336 XtPointer client_data, call_data;
7337{
7338 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7339 float top = *(float *) call_data;
7340 float shown;
7341 int whole, portion;
7342 int dragging_down_p, part;
7343 double epsilon = 0.01;
7344
7345 /* Get the size of the thumb, a value between 0 and 1. */
7346 BLOCK_INPUT;
7347 XtVaGetValues (widget, XtNshown, &shown, NULL);
7348 UNBLOCK_INPUT;
7349
7350 whole = 10000000;
7351 portion = shown < 1 ? top * whole : 0;
7352 dragging_down_p = (INTEGERP (bar->dragging)
7353 && XINT (bar->dragging) < portion);
7354
7355 if (shown < 1
7356 && (abs (top + shown - 1) < epsilon
7357 || (dragging_down_p
7358 && last_scroll_bar_part == scroll_bar_down_arrow)))
7359 part = scroll_bar_down_arrow;
7360 else
7361 part = scroll_bar_handle;
7362
7363 window_being_scrolled = bar->window;
7364 bar->dragging = make_number (portion);
7365 last_scroll_bar_part = part;
7366 x_send_scroll_bar_event (bar->window, part, portion, whole);
7367}
7368
7369
7370/* Xaw3d scroll bar callback. Invoked for incremental scrolling.,
7371 i.e. line or page up or down. WIDGET is the Xaw3d scroll bar
7372 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
7373 the scroll bar. CALL_DATA is an integer specifying the action that
7374 has taken place. It's magnitude is in the range 0..height of the
7375 scroll bar. Negative values mean scroll towards buffer start.
7376 Values < height of scroll bar mean line-wise movement. */
7377
7378static void
7379xaw3d_scroll_callback (widget, client_data, call_data)
7380 Widget widget;
7381 XtPointer client_data, call_data;
7382{
7383 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7384 int position = (int) call_data;
7385 Dimension height;
7386 int part;
7387
7388 /* Get the height of the scroll bar. */
7389 BLOCK_INPUT;
7390 XtVaGetValues (widget, XtNheight, &height, NULL);
7391 UNBLOCK_INPUT;
7392
7393 if (position < 0)
7394 {
7395 if (abs (position) < height)
7396 part = scroll_bar_up_arrow;
7397 else
7398 part = scroll_bar_above_handle;
7399 }
7400 else
7401 {
7402 if (abs (position) < height)
7403 part = scroll_bar_down_arrow;
7404 else
7405 part = scroll_bar_below_handle;
7406 }
7407
7408 window_being_scrolled = bar->window;
7409 bar->dragging = Qnil;
7410 last_scroll_bar_part = part;
7411 x_send_scroll_bar_event (bar->window, part, 0, 0);
7412}
7413
7414
7415#endif /* not USE_MOTIF */
7416
7417
7418/* Create the widget for scroll bar BAR on frame F. Record the widget
7419 and X window of the scroll bar in BAR. */
7420
7421static void
7422x_create_toolkit_scroll_bar (f, bar)
7423 struct frame *f;
7424 struct scroll_bar *bar;
7425{
7426 Window xwindow;
7427 Widget widget;
7428 Arg av[20];
7429 int ac = 0;
7430 char *scroll_bar_name = "verticalScrollBar";
7431 unsigned long pixel;
7432
7433 BLOCK_INPUT;
7434
7435#ifdef USE_MOTIF
7436 /* LessTif 0.85, problems:
7437
7438 1. When the mouse if over the scroll bar, the scroll bar will
7439 get keyboard events. I didn't find a way to turn this off.
7440
7441 2. Do we have to explicitly set the cursor to get an arrow
7442 cursor (see below)? */
7443
7444 /* Set resources. Create the widget. */
7445 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7446 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
7447 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
7448 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
7449 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
7450 XtSetArg (av[ac], XmNincrement, 1); ++ac;
7451 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
7452
7453 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7454 if (pixel != -1)
7455 {
7456 XtSetArg (av[ac], XmNforeground, pixel);
7457 ++ac;
7458 }
7459
7460 pixel = f->output_data.x->scroll_bar_background_pixel;
7461 if (pixel != -1)
7462 {
7463 XtSetArg (av[ac], XmNbackground, pixel);
7464 ++ac;
7465 }
7466
7467 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
7468 scroll_bar_name, av, ac);
7469
7470 /* Add one callback for everything that can happen. */
7471 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
7472 (XtPointer) bar);
7473 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
7474 (XtPointer) bar);
7475 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
7476 (XtPointer) bar);
7477 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
7478 (XtPointer) bar);
7479 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
7480 (XtPointer) bar);
7481 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
7482 (XtPointer) bar);
7483 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
7484 (XtPointer) bar);
7485
7486 /* Realize the widget. Only after that is the X window created. */
7487 XtRealizeWidget (widget);
7488
7489 /* Set the cursor to an arrow. I didn't find a resource to do that.
7490 And I'm wondering why it hasn't an arrow cursor by default. */
7491 XDefineCursor (XtDisplay (widget), XtWindow (widget),
7492 f->output_data.x->nontext_cursor);
7493
7494#elif defined HAVE_XAW3D
7495
7496 /* Set resources. Create the widget. The background of the
7497 Xaw3d scroll bar widget is a little bit light for my taste.
7498 We don't alter it here to let users change it according
7499 to their taste with `emacs*verticalScrollBar.background: xxx'. */
7500 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7501 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
7502 XtSetArg (av[ac], XtNcursorName, "left_ptr"); ++ac;
7503 XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac;
7504
7505 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7506 if (pixel != -1)
7507 {
7508 XtSetArg (av[ac], XtNforeground, pixel);
7509 ++ac;
7510 }
7511
7512 pixel = f->output_data.x->scroll_bar_background_pixel;
7513 if (pixel != -1)
7514 {
7515 XtSetArg (av[ac], XtNbackground, pixel);
7516 ++ac;
7517 }
7518
7519 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
7520 f->output_data.x->edit_widget, av, ac);
7521
7522 /* Define callbacks. */
7523 XtAddCallback (widget, XtNjumpProc, xaw3d_jump_callback, (XtPointer) bar);
7524 XtAddCallback (widget, XtNscrollProc, xaw3d_scroll_callback,
7525 (XtPointer) bar);
7526
7527 /* Realize the widget. Only after that is the X window created. */
7528 XtRealizeWidget (widget);
7529
7530#endif /* HAVE_XAW3D */
7531
7532 /* Install an action hook that let's us detect when the user
7533 finishes interacting with a scroll bar. */
7534 if (action_hook_id == 0)
7535 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
7536
7537 /* Remember X window and widget in the scroll bar vector. */
7538 SET_SCROLL_BAR_X_WIDGET (bar, widget);
7539 xwindow = XtWindow (widget);
7540 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
7541
7542 UNBLOCK_INPUT;
7543}
7544
7545
7546/* Set the thumb size and position of scroll bar BAR. We are currently
7547 displaying PORTION out of a whole WHOLE, and our position POSITION. */
7548
7549static void
7550x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
7551 struct scroll_bar *bar;
7552 int portion, position, whole;
f451eb13 7553{
06a2c219 7554 float top, shown;
06a2c219 7555 Widget widget = SCROLL_BAR_X_WIDGET (bar);
f451eb13 7556
06a2c219
GM
7557 if (whole == 0)
7558 top = 0, shown = 1;
7559 else
f451eb13 7560 {
06a2c219
GM
7561 top = (float) position / whole;
7562 shown = (float) portion / whole;
7563 }
f451eb13 7564
06a2c219 7565 BLOCK_INPUT;
f451eb13 7566
06a2c219
GM
7567#ifdef USE_MOTIF
7568 {
7569 int size, value;
7570 Boolean arrow1_selected, arrow2_selected;
7571 unsigned char flags;
7572 XmScrollBarWidget sb;
7573
7574 /* Slider size. Must be in the range [1 .. MAX - MIN] where NAX
7575 is the scroll bar's maximum and MIN is the scroll bar's minimum
7576 value. */
7577 size = shown * XM_SB_RANGE;
7578 size = min (size, XM_SB_RANGE);
7579 size = max (size, 1);
7580
7581 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
7582 value = top * XM_SB_RANGE;
7583 value = min (value, XM_SB_MAX - size);
7584 value = max (value, XM_SB_MIN);
7585
7586 /* LessTif: Calling XmScrollBarSetValues after an increment or
7587 decrement turns off auto-repeat LessTif-internally. This can
7588 be seen in ScrollBar.c which resets Arrow1Selected and
7589 Arrow2Selected. It also sets internal flags so that LessTif
7590 believes the mouse is in the slider. We either have to change
7591 our code, or work around that by accessing private data. */
7592
7593 sb = (XmScrollBarWidget) widget;
7594 arrow1_selected = sb->scrollBar.arrow1_selected;
7595 arrow2_selected = sb->scrollBar.arrow2_selected;
7596 flags = sb->scrollBar.flags;
7597
7598 if (NILP (bar->dragging))
7599 XmScrollBarSetValues (widget, value, size, 0, 0, False);
7600 else if (last_scroll_bar_part == scroll_bar_down_arrow)
7601 /* This has the negative side effect that the slider value is
7602 not would it would be if we scrolled here using line-wise or
7603 page-wise movement. */
7604 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
7605 else
7606 {
7607 /* If currently dragging, only update the slider size.
7608 This reduces flicker effects. */
7609 int old_value, old_size, increment, page_increment;
7610
7611 XmScrollBarGetValues (widget, &old_value, &old_size,
7612 &increment, &page_increment);
7613 XmScrollBarSetValues (widget, old_value,
7614 min (size, XM_SB_RANGE - old_value),
7615 0, 0, False);
7616 }
7617
7618 sb->scrollBar.arrow1_selected = arrow1_selected;
7619 sb->scrollBar.arrow2_selected = arrow2_selected;
7620 sb->scrollBar.flags = flags;
7621 }
7622#elif defined HAVE_XAW3D
7623 {
7624 /* Restrict to [0 1]. */
7625 top = max (0, min (1, top));
7626 shown = max (0, min (1, shown));
7627
7628 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
7629 check that your system's configuration file contains a define
7630 for `NARROWPROTO'. See s/freebsd.h for an example. */
7631 if (NILP (bar->dragging))
eb393530
GM
7632 {
7633 float old_top, old_shown;
7634 XtVaGetValues (widget, XtNtopOfThumb, &old_top, XtNshown, &old_shown,
7635 NULL);
7636 if (top != old_top || shown != old_shown)
7637 XawScrollbarSetThumb (widget, top, shown);
7638 }
06a2c219
GM
7639 else
7640 {
7641 ScrollbarWidget sb = (ScrollbarWidget) widget;
7642 int scroll_mode = sb->scrollbar.scroll_mode;
f451eb13 7643
06a2c219
GM
7644 sb->scrollbar.scroll_mode = 0;
7645
7646 if (last_scroll_bar_part == scroll_bar_down_arrow)
7647 XawScrollbarSetThumb (widget, top, 1 - top);
7648 else
7649 {
7650 float old_top;
7651 XtVaGetValues (widget, XtNtopOfThumb, &old_top, NULL);
7652 XawScrollbarSetThumb (widget, old_top, min (shown, 1 - old_top));
7653 }
7654
7655 sb->scrollbar.scroll_mode = scroll_mode;
7656 }
7657 }
7658#endif /* HAVE_XAW3D */
7659
7660 UNBLOCK_INPUT;
f451eb13
JB
7661}
7662
06a2c219
GM
7663#endif /* USE_TOOLKIT_SCROLL_BARS */
7664
7665
7666\f
7667/************************************************************************
7668 Scroll bars, general
7669 ************************************************************************/
7670
7671/* Create a scroll bar and return the scroll bar vector for it. W is
7672 the Emacs window on which to create the scroll bar. TOP, LEFT,
7673 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
7674 scroll bar. */
7675
ab648270 7676static struct scroll_bar *
06a2c219
GM
7677x_scroll_bar_create (w, top, left, width, height)
7678 struct window *w;
f451eb13
JB
7679 int top, left, width, height;
7680{
06a2c219 7681 struct frame *f = XFRAME (w->frame);
334208b7
RS
7682 struct scroll_bar *bar
7683 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
f451eb13
JB
7684
7685 BLOCK_INPUT;
7686
06a2c219
GM
7687#if USE_TOOLKIT_SCROLL_BARS
7688 x_create_toolkit_scroll_bar (f, bar);
7689#else /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
7690 {
7691 XSetWindowAttributes a;
7692 unsigned long mask;
5c187dee 7693 Window window;
06a2c219
GM
7694
7695 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
7696 if (a.background_pixel == -1)
7697 a.background_pixel = f->output_data.x->background_pixel;
7698
12ba150f 7699 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9a572e2a 7700 | ButtonMotionMask | PointerMotionHintMask
12ba150f 7701 | ExposureMask);
7a13e894 7702 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
f451eb13 7703
dbc4e1c1 7704 mask = (CWBackPixel | CWEventMask | CWCursor);
f451eb13 7705
06a2c219
GM
7706 /* Clear the area of W that will serve as a scroll bar. This is
7707 for the case that a window has been split horizontally. In
7708 this case, no clear_frame is generated to reduce flickering. */
7709 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7710 left, top, width,
7711 window_box_height (w), False);
7712
7713 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7714 /* Position and size of scroll bar. */
7715 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7716 top,
7717 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
7718 height,
7719 /* Border width, depth, class, and visual. */
7720 0,
7721 CopyFromParent,
7722 CopyFromParent,
7723 CopyFromParent,
7724 /* Attributes. */
7725 mask, &a);
7726 SET_SCROLL_BAR_X_WINDOW (bar, window);
f451eb13 7727 }
06a2c219 7728#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 7729
06a2c219 7730 XSETWINDOW (bar->window, w);
e0c1aef2
KH
7731 XSETINT (bar->top, top);
7732 XSETINT (bar->left, left);
7733 XSETINT (bar->width, width);
7734 XSETINT (bar->height, height);
7735 XSETINT (bar->start, 0);
7736 XSETINT (bar->end, 0);
12ba150f 7737 bar->dragging = Qnil;
f451eb13
JB
7738
7739 /* Add bar to its frame's list of scroll bars. */
334208b7 7740 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 7741 bar->prev = Qnil;
334208b7 7742 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
06a2c219 7743 if (!NILP (bar->next))
e0c1aef2 7744 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13 7745
06a2c219
GM
7746 /* Map the window/widget. */
7747#if USE_TOOLKIT_SCROLL_BARS
7748 XtMapWidget (SCROLL_BAR_X_WIDGET (bar));
7749 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
7750 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7751 top,
7752 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
7753 height, 0);
7754#else /* not USE_TOOLKIT_SCROLL_BARS */
7f9c7f94 7755 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
06a2c219 7756#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
7757
7758 UNBLOCK_INPUT;
12ba150f 7759 return bar;
f451eb13
JB
7760}
7761
06a2c219 7762
12ba150f 7763/* Draw BAR's handle in the proper position.
06a2c219 7764
12ba150f
JB
7765 If the handle is already drawn from START to END, don't bother
7766 redrawing it, unless REBUILD is non-zero; in that case, always
7767 redraw it. (REBUILD is handy for drawing the handle after expose
58769bee 7768 events.)
12ba150f
JB
7769
7770 Normally, we want to constrain the start and end of the handle to
06a2c219
GM
7771 fit inside its rectangle, but if the user is dragging the scroll
7772 bar handle, we want to let them drag it down all the way, so that
7773 the bar's top is as far down as it goes; otherwise, there's no way
7774 to move to the very end of the buffer. */
7775
5c187dee
GM
7776#ifndef USE_TOOLKIT_SCROLL_BARS
7777
f451eb13 7778static void
ab648270
JB
7779x_scroll_bar_set_handle (bar, start, end, rebuild)
7780 struct scroll_bar *bar;
f451eb13 7781 int start, end;
12ba150f 7782 int rebuild;
f451eb13 7783{
12ba150f 7784 int dragging = ! NILP (bar->dragging);
ab648270 7785 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 7786 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 7787 GC gc = f->output_data.x->normal_gc;
12ba150f
JB
7788
7789 /* If the display is already accurate, do nothing. */
7790 if (! rebuild
7791 && start == XINT (bar->start)
7792 && end == XINT (bar->end))
7793 return;
7794
f451eb13
JB
7795 BLOCK_INPUT;
7796
7797 {
d9cdbb3d
RS
7798 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
7799 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
7800 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
f451eb13
JB
7801
7802 /* Make sure the values are reasonable, and try to preserve
7803 the distance between start and end. */
12ba150f
JB
7804 {
7805 int length = end - start;
7806
7807 if (start < 0)
7808 start = 0;
7809 else if (start > top_range)
7810 start = top_range;
7811 end = start + length;
7812
7813 if (end < start)
7814 end = start;
7815 else if (end > top_range && ! dragging)
7816 end = top_range;
7817 }
f451eb13 7818
ab648270 7819 /* Store the adjusted setting in the scroll bar. */
e0c1aef2
KH
7820 XSETINT (bar->start, start);
7821 XSETINT (bar->end, end);
f451eb13 7822
12ba150f
JB
7823 /* Clip the end position, just for display. */
7824 if (end > top_range)
7825 end = top_range;
f451eb13 7826
ab648270 7827 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
12ba150f
JB
7828 below top positions, to make sure the handle is always at least
7829 that many pixels tall. */
ab648270 7830 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
f451eb13 7831
12ba150f
JB
7832 /* Draw the empty space above the handle. Note that we can't clear
7833 zero-height areas; that means "clear to end of window." */
7834 if (0 < start)
334208b7 7835 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 7836
12ba150f 7837 /* x, y, width, height, and exposures. */
ab648270
JB
7838 VERTICAL_SCROLL_BAR_LEFT_BORDER,
7839 VERTICAL_SCROLL_BAR_TOP_BORDER,
12ba150f
JB
7840 inside_width, start,
7841 False);
f451eb13 7842
06a2c219
GM
7843 /* Change to proper foreground color if one is specified. */
7844 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
7845 XSetForeground (FRAME_X_DISPLAY (f), gc,
7846 f->output_data.x->scroll_bar_foreground_pixel);
7847
12ba150f 7848 /* Draw the handle itself. */
334208b7 7849 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13 7850
12ba150f 7851 /* x, y, width, height */
ab648270
JB
7852 VERTICAL_SCROLL_BAR_LEFT_BORDER,
7853 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
12ba150f 7854 inside_width, end - start);
f451eb13 7855
06a2c219
GM
7856 /* Restore the foreground color of the GC if we changed it above. */
7857 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
7858 XSetForeground (FRAME_X_DISPLAY (f), gc,
7859 f->output_data.x->foreground_pixel);
f451eb13 7860
12ba150f
JB
7861 /* Draw the empty space below the handle. Note that we can't
7862 clear zero-height areas; that means "clear to end of window." */
7863 if (end < inside_height)
334208b7 7864 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 7865
12ba150f 7866 /* x, y, width, height, and exposures. */
ab648270
JB
7867 VERTICAL_SCROLL_BAR_LEFT_BORDER,
7868 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
12ba150f
JB
7869 inside_width, inside_height - end,
7870 False);
f451eb13 7871
f451eb13
JB
7872 }
7873
f451eb13
JB
7874 UNBLOCK_INPUT;
7875}
7876
5c187dee 7877#endif /* !USE_TOOLKIT_SCROLL_BARS */
f451eb13 7878
06a2c219
GM
7879/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
7880 nil. */
58769bee 7881
12ba150f 7882static void
ab648270
JB
7883x_scroll_bar_remove (bar)
7884 struct scroll_bar *bar;
12ba150f 7885{
12ba150f
JB
7886 BLOCK_INPUT;
7887
06a2c219
GM
7888#if USE_TOOLKIT_SCROLL_BARS
7889 XtDestroyWidget (SCROLL_BAR_X_WIDGET (bar));
7890#else /* not USE_TOOLKIT_SCROLL_BARS */
5c187dee
GM
7891 {
7892 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7893 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
7894 }
06a2c219
GM
7895#endif /* not USE_TOOLKIT_SCROLL_BARS */
7896
ab648270
JB
7897 /* Disassociate this scroll bar from its window. */
7898 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
12ba150f
JB
7899
7900 UNBLOCK_INPUT;
7901}
7902
06a2c219 7903
12ba150f
JB
7904/* Set the handle of the vertical scroll bar for WINDOW to indicate
7905 that we are displaying PORTION characters out of a total of WHOLE
ab648270 7906 characters, starting at POSITION. If WINDOW has no scroll bar,
12ba150f 7907 create one. */
06a2c219 7908
12ba150f 7909static void
06a2c219
GM
7910XTset_vertical_scroll_bar (w, portion, whole, position)
7911 struct window *w;
f451eb13
JB
7912 int portion, whole, position;
7913{
06a2c219 7914 struct frame *f = XFRAME (w->frame);
ab648270 7915 struct scroll_bar *bar;
3c6ede7b 7916 int top, height, left, sb_left, width, sb_width;
06a2c219 7917 int window_x, window_y, window_width, window_height;
06a2c219 7918
3c6ede7b 7919 /* Get window dimensions. */
06a2c219 7920 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
3c6ede7b
GM
7921 top = window_y;
7922 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
7923 height = window_height;
06a2c219 7924
3c6ede7b 7925 /* Compute the left edge of the scroll bar area. */
06a2c219 7926 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
3c6ede7b
GM
7927 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
7928 else
7929 left = XFASTINT (w->left);
7930 left *= CANON_X_UNIT (f);
7931 left += FRAME_INTERNAL_BORDER_WIDTH (f);
7932
7933 /* Compute the width of the scroll bar which might be less than
7934 the width of the area reserved for the scroll bar. */
7935 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
7936 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
06a2c219 7937 else
3c6ede7b 7938 sb_width = width;
12ba150f 7939
3c6ede7b
GM
7940 /* Compute the left edge of the scroll bar. */
7941#ifdef USE_TOOLKIT_SCROLL_BARS
7942 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
7943 sb_left = left + width - sb_width - (width - sb_width) / 2;
7944 else
7945 sb_left = left + (width - sb_width) / 2;
7946#else
7947 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
7948 sb_left = left + width - sb_width;
7949 else
7950 sb_left = left;
7951#endif
7952
ab648270 7953 /* Does the scroll bar exist yet? */
06a2c219 7954 if (NILP (w->vertical_scroll_bar))
3c6ede7b 7955 {
80c32bcc 7956 BLOCK_INPUT;
3c6ede7b
GM
7957 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7958 left, top, width, height, False);
80c32bcc 7959 UNBLOCK_INPUT;
3c6ede7b
GM
7960 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
7961 }
f451eb13 7962 else
12ba150f
JB
7963 {
7964 /* It may just need to be moved and resized. */
06a2c219
GM
7965 unsigned int mask = 0;
7966
7967 bar = XSCROLL_BAR (w->vertical_scroll_bar);
7968
7969 BLOCK_INPUT;
7970
3c6ede7b 7971 if (sb_left != XINT (bar->left))
06a2c219 7972 mask |= CWX;
3c6ede7b 7973 if (top != XINT (bar->top))
06a2c219 7974 mask |= CWY;
3c6ede7b 7975 if (sb_width != XINT (bar->width))
06a2c219 7976 mask |= CWWidth;
3c6ede7b 7977 if (height != XINT (bar->height))
06a2c219
GM
7978 mask |= CWHeight;
7979
7980#ifdef USE_TOOLKIT_SCROLL_BARS
fe6f39d9
GM
7981
7982 /* Since toolkit scroll bars are smaller than the space reserved
7983 for them on the frame, we have to clear "under" them. */
7984 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3c6ede7b 7985 left, top, width, height, False);
06a2c219
GM
7986
7987 /* Move/size the scroll bar widget. */
7988 if (mask)
7989 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
3c6ede7b
GM
7990 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7991 top,
7992 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
7993 height, 0);
06a2c219
GM
7994
7995#else /* not USE_TOOLKIT_SCROLL_BARS */
7996
e1f6572f
RS
7997 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
7998 {
7999 /* Clear areas not covered by the scroll bar. This makes sure a
8000 previous mode line display is cleared after C-x 2 C-x 1, for
8001 example. Non-toolkit scroll bars are as wide as the area
8002 reserved for scroll bars - trim at both sides. */
8003 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8004 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8005 height, False);
8006 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8007 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8008 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8009 height, False);
8010 }
06a2c219
GM
8011
8012 /* Move/size the scroll bar window. */
8013 if (mask)
8014 {
8015 XWindowChanges wc;
8016
3c6ede7b
GM
8017 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8018 wc.y = top;
8019 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8020 wc.height = height;
06a2c219
GM
8021 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8022 mask, &wc);
8023 }
8024
8025#endif /* not USE_TOOLKIT_SCROLL_BARS */
8026
8027 /* Remember new settings. */
3c6ede7b
GM
8028 XSETINT (bar->left, sb_left);
8029 XSETINT (bar->top, top);
8030 XSETINT (bar->width, sb_width);
8031 XSETINT (bar->height, height);
06a2c219
GM
8032
8033 UNBLOCK_INPUT;
12ba150f 8034 }
f451eb13 8035
06a2c219
GM
8036#if USE_TOOLKIT_SCROLL_BARS
8037 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8038#else /* not USE_TOOLKIT_SCROLL_BARS */
ab648270 8039 /* Set the scroll bar's current state, unless we're currently being
f451eb13 8040 dragged. */
12ba150f 8041 if (NILP (bar->dragging))
f451eb13 8042 {
92857db0 8043 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
f451eb13 8044
12ba150f 8045 if (whole == 0)
ab648270 8046 x_scroll_bar_set_handle (bar, 0, top_range, 0);
12ba150f
JB
8047 else
8048 {
43f868f5
JB
8049 int start = ((double) position * top_range) / whole;
8050 int end = ((double) (position + portion) * top_range) / whole;
ab648270 8051 x_scroll_bar_set_handle (bar, start, end, 0);
12ba150f 8052 }
f451eb13 8053 }
06a2c219 8054#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 8055
06a2c219 8056 XSETVECTOR (w->vertical_scroll_bar, bar);
f451eb13
JB
8057}
8058
12ba150f 8059
f451eb13 8060/* The following three hooks are used when we're doing a thorough
ab648270 8061 redisplay of the frame. We don't explicitly know which scroll bars
f451eb13 8062 are going to be deleted, because keeping track of when windows go
12ba150f
JB
8063 away is a real pain - "Can you say set-window-configuration, boys
8064 and girls?" Instead, we just assert at the beginning of redisplay
ab648270 8065 that *all* scroll bars are to be removed, and then save a scroll bar
12ba150f 8066 from the fiery pit when we actually redisplay its window. */
f451eb13 8067
ab648270
JB
8068/* Arrange for all scroll bars on FRAME to be removed at the next call
8069 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
06a2c219
GM
8070 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8071
58769bee 8072static void
ab648270 8073XTcondemn_scroll_bars (frame)
f451eb13
JB
8074 FRAME_PTR frame;
8075{
f9e24cb9
RS
8076 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8077 while (! NILP (FRAME_SCROLL_BARS (frame)))
8078 {
8079 Lisp_Object bar;
8080 bar = FRAME_SCROLL_BARS (frame);
8081 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8082 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8083 XSCROLL_BAR (bar)->prev = Qnil;
8084 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8085 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8086 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8087 }
f451eb13
JB
8088}
8089
06a2c219 8090/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
12ba150f 8091 Note that WINDOW isn't necessarily condemned at all. */
f451eb13 8092static void
ab648270 8093XTredeem_scroll_bar (window)
12ba150f 8094 struct window *window;
f451eb13 8095{
ab648270 8096 struct scroll_bar *bar;
12ba150f 8097
ab648270
JB
8098 /* We can't redeem this window's scroll bar if it doesn't have one. */
8099 if (NILP (window->vertical_scroll_bar))
12ba150f
JB
8100 abort ();
8101
ab648270 8102 bar = XSCROLL_BAR (window->vertical_scroll_bar);
12ba150f
JB
8103
8104 /* Unlink it from the condemned list. */
8105 {
8106 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
8107
8108 if (NILP (bar->prev))
8109 {
8110 /* If the prev pointer is nil, it must be the first in one of
8111 the lists. */
ab648270 8112 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
12ba150f
JB
8113 /* It's not condemned. Everything's fine. */
8114 return;
ab648270
JB
8115 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8116 window->vertical_scroll_bar))
8117 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
12ba150f
JB
8118 else
8119 /* If its prev pointer is nil, it must be at the front of
8120 one or the other! */
8121 abort ();
8122 }
8123 else
ab648270 8124 XSCROLL_BAR (bar->prev)->next = bar->next;
12ba150f
JB
8125
8126 if (! NILP (bar->next))
ab648270 8127 XSCROLL_BAR (bar->next)->prev = bar->prev;
12ba150f 8128
ab648270 8129 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 8130 bar->prev = Qnil;
e0c1aef2 8131 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
12ba150f 8132 if (! NILP (bar->next))
e0c1aef2 8133 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
12ba150f 8134 }
f451eb13
JB
8135}
8136
ab648270
JB
8137/* Remove all scroll bars on FRAME that haven't been saved since the
8138 last call to `*condemn_scroll_bars_hook'. */
06a2c219 8139
f451eb13 8140static void
ab648270 8141XTjudge_scroll_bars (f)
12ba150f 8142 FRAME_PTR f;
f451eb13 8143{
12ba150f 8144 Lisp_Object bar, next;
f451eb13 8145
ab648270 8146 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
cf7cb199
JB
8147
8148 /* Clear out the condemned list now so we won't try to process any
ab648270
JB
8149 more events on the hapless scroll bars. */
8150 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
cf7cb199
JB
8151
8152 for (; ! NILP (bar); bar = next)
f451eb13 8153 {
ab648270 8154 struct scroll_bar *b = XSCROLL_BAR (bar);
12ba150f 8155
ab648270 8156 x_scroll_bar_remove (b);
12ba150f
JB
8157
8158 next = b->next;
8159 b->next = b->prev = Qnil;
f451eb13 8160 }
12ba150f 8161
ab648270 8162 /* Now there should be no references to the condemned scroll bars,
12ba150f 8163 and they should get garbage-collected. */
f451eb13
JB
8164}
8165
8166
06a2c219
GM
8167/* Handle an Expose or GraphicsExpose event on a scroll bar. This
8168 is a no-op when using toolkit scroll bars.
ab648270
JB
8169
8170 This may be called from a signal handler, so we have to ignore GC
8171 mark bits. */
06a2c219 8172
f451eb13 8173static void
ab648270
JB
8174x_scroll_bar_expose (bar, event)
8175 struct scroll_bar *bar;
f451eb13
JB
8176 XEvent *event;
8177{
06a2c219
GM
8178#ifndef USE_TOOLKIT_SCROLL_BARS
8179
ab648270 8180 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8181 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8182 GC gc = f->output_data.x->normal_gc;
3cbd2e0b 8183 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
12ba150f 8184
f451eb13
JB
8185 BLOCK_INPUT;
8186
ab648270 8187 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
f451eb13 8188
06a2c219 8189 /* Draw a one-pixel border just inside the edges of the scroll bar. */
334208b7 8190 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13
JB
8191
8192 /* x, y, width, height */
d9cdbb3d 8193 0, 0,
3cbd2e0b 8194 XINT (bar->width) - 1 - width_trim - width_trim,
d9cdbb3d
RS
8195 XINT (bar->height) - 1);
8196
f451eb13 8197 UNBLOCK_INPUT;
06a2c219
GM
8198
8199#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8200}
8201
ab648270
JB
8202/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8203 is set to something other than no_event, it is enqueued.
8204
8205 This may be called from a signal handler, so we have to ignore GC
8206 mark bits. */
06a2c219 8207
5c187dee
GM
8208#ifndef USE_TOOLKIT_SCROLL_BARS
8209
f451eb13 8210static void
ab648270
JB
8211x_scroll_bar_handle_click (bar, event, emacs_event)
8212 struct scroll_bar *bar;
f451eb13
JB
8213 XEvent *event;
8214 struct input_event *emacs_event;
8215{
0299d313 8216 if (! GC_WINDOWP (bar->window))
12ba150f
JB
8217 abort ();
8218
ab648270 8219 emacs_event->kind = scroll_bar_click;
69388238 8220 emacs_event->code = event->xbutton.button - Button1;
0299d313
RS
8221 emacs_event->modifiers
8222 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8223 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8224 event->xbutton.state)
8225 | (event->type == ButtonRelease
8226 ? up_modifier
8227 : down_modifier));
12ba150f 8228 emacs_event->frame_or_window = bar->window;
f451eb13 8229 emacs_event->timestamp = event->xbutton.time;
12ba150f 8230 {
06a2c219 8231#if 0
d9cdbb3d 8232 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
0299d313 8233 int internal_height
d9cdbb3d 8234 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8235#endif
0299d313 8236 int top_range
d9cdbb3d 8237 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ab648270 8238 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
12ba150f
JB
8239
8240 if (y < 0) y = 0;
8241 if (y > top_range) y = top_range;
8242
8243 if (y < XINT (bar->start))
ab648270
JB
8244 emacs_event->part = scroll_bar_above_handle;
8245 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8246 emacs_event->part = scroll_bar_handle;
12ba150f 8247 else
ab648270 8248 emacs_event->part = scroll_bar_below_handle;
929787e1
JB
8249
8250 /* Just because the user has clicked on the handle doesn't mean
5116f055
JB
8251 they want to drag it. Lisp code needs to be able to decide
8252 whether or not we're dragging. */
929787e1 8253#if 0
12ba150f
JB
8254 /* If the user has just clicked on the handle, record where they're
8255 holding it. */
8256 if (event->type == ButtonPress
ab648270 8257 && emacs_event->part == scroll_bar_handle)
e0c1aef2 8258 XSETINT (bar->dragging, y - XINT (bar->start));
929787e1 8259#endif
12ba150f
JB
8260
8261 /* If the user has released the handle, set it to its final position. */
8262 if (event->type == ButtonRelease
8263 && ! NILP (bar->dragging))
8264 {
8265 int new_start = y - XINT (bar->dragging);
8266 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
f451eb13 8267
ab648270 8268 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
12ba150f
JB
8269 bar->dragging = Qnil;
8270 }
f451eb13 8271
5116f055
JB
8272 /* Same deal here as the other #if 0. */
8273#if 0
58769bee 8274 /* Clicks on the handle are always reported as occurring at the top of
12ba150f 8275 the handle. */
ab648270 8276 if (emacs_event->part == scroll_bar_handle)
12ba150f
JB
8277 emacs_event->x = bar->start;
8278 else
e0c1aef2 8279 XSETINT (emacs_event->x, y);
5116f055 8280#else
e0c1aef2 8281 XSETINT (emacs_event->x, y);
5116f055 8282#endif
f451eb13 8283
e0c1aef2 8284 XSETINT (emacs_event->y, top_range);
12ba150f
JB
8285 }
8286}
f451eb13 8287
ab648270
JB
8288/* Handle some mouse motion while someone is dragging the scroll bar.
8289
8290 This may be called from a signal handler, so we have to ignore GC
8291 mark bits. */
06a2c219 8292
f451eb13 8293static void
ab648270
JB
8294x_scroll_bar_note_movement (bar, event)
8295 struct scroll_bar *bar;
f451eb13
JB
8296 XEvent *event;
8297{
39d8bb4d
KH
8298 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8299
f451eb13
JB
8300 last_mouse_movement_time = event->xmotion.time;
8301
39d8bb4d 8302 f->mouse_moved = 1;
e0c1aef2 8303 XSETVECTOR (last_mouse_scroll_bar, bar);
f451eb13
JB
8304
8305 /* If we're dragging the bar, display it. */
ab648270 8306 if (! GC_NILP (bar->dragging))
f451eb13
JB
8307 {
8308 /* Where should the handle be now? */
12ba150f 8309 int new_start = event->xmotion.y - XINT (bar->dragging);
f451eb13 8310
12ba150f 8311 if (new_start != XINT (bar->start))
f451eb13 8312 {
12ba150f 8313 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
58769bee 8314
ab648270 8315 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
f451eb13
JB
8316 }
8317 }
f451eb13
JB
8318}
8319
5c187dee
GM
8320#endif /* !USE_TOOLKIT_SCROLL_BARS */
8321
12ba150f 8322/* Return information to the user about the current position of the mouse
ab648270 8323 on the scroll bar. */
06a2c219 8324
12ba150f 8325static void
334208b7
RS
8326x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8327 FRAME_PTR *fp;
12ba150f 8328 Lisp_Object *bar_window;
ab648270 8329 enum scroll_bar_part *part;
12ba150f
JB
8330 Lisp_Object *x, *y;
8331 unsigned long *time;
8332{
ab648270 8333 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
334208b7
RS
8334 Window w = SCROLL_BAR_X_WINDOW (bar);
8335 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f 8336 int win_x, win_y;
559cb2fb
JB
8337 Window dummy_window;
8338 int dummy_coord;
8339 unsigned int dummy_mask;
12ba150f 8340
cf7cb199
JB
8341 BLOCK_INPUT;
8342
ab648270 8343 /* Get the mouse's position relative to the scroll bar window, and
12ba150f 8344 report that. */
334208b7 8345 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
12ba150f 8346
559cb2fb
JB
8347 /* Root, child, root x and root y. */
8348 &dummy_window, &dummy_window,
8349 &dummy_coord, &dummy_coord,
12ba150f 8350
559cb2fb
JB
8351 /* Position relative to scroll bar. */
8352 &win_x, &win_y,
12ba150f 8353
559cb2fb
JB
8354 /* Mouse buttons and modifier keys. */
8355 &dummy_mask))
7a13e894 8356 ;
559cb2fb
JB
8357 else
8358 {
06a2c219 8359#if 0
559cb2fb 8360 int inside_height
d9cdbb3d 8361 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8362#endif
559cb2fb 8363 int top_range
d9cdbb3d 8364 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
559cb2fb
JB
8365
8366 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
8367
8368 if (! NILP (bar->dragging))
8369 win_y -= XINT (bar->dragging);
8370
8371 if (win_y < 0)
8372 win_y = 0;
8373 if (win_y > top_range)
8374 win_y = top_range;
8375
334208b7 8376 *fp = f;
7a13e894 8377 *bar_window = bar->window;
559cb2fb
JB
8378
8379 if (! NILP (bar->dragging))
8380 *part = scroll_bar_handle;
8381 else if (win_y < XINT (bar->start))
8382 *part = scroll_bar_above_handle;
8383 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8384 *part = scroll_bar_handle;
8385 else
8386 *part = scroll_bar_below_handle;
12ba150f 8387
e0c1aef2
KH
8388 XSETINT (*x, win_y);
8389 XSETINT (*y, top_range);
12ba150f 8390
39d8bb4d 8391 f->mouse_moved = 0;
559cb2fb
JB
8392 last_mouse_scroll_bar = Qnil;
8393 }
12ba150f 8394
559cb2fb 8395 *time = last_mouse_movement_time;
cf7cb199 8396
cf7cb199 8397 UNBLOCK_INPUT;
12ba150f
JB
8398}
8399
f451eb13 8400
dbc4e1c1 8401/* The screen has been cleared so we may have changed foreground or
ab648270
JB
8402 background colors, and the scroll bars may need to be redrawn.
8403 Clear out the scroll bars, and ask for expose events, so we can
dbc4e1c1
JB
8404 redraw them. */
8405
dfcf069d 8406void
ab648270 8407x_scroll_bar_clear (f)
dbc4e1c1
JB
8408 FRAME_PTR f;
8409{
06a2c219 8410#ifndef USE_TOOLKIT_SCROLL_BARS
dbc4e1c1
JB
8411 Lisp_Object bar;
8412
b80c363e
RS
8413 /* We can have scroll bars even if this is 0,
8414 if we just turned off scroll bar mode.
8415 But in that case we should not clear them. */
8416 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8417 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
8418 bar = XSCROLL_BAR (bar)->next)
8419 XClearArea (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
8420 0, 0, 0, 0, True);
06a2c219 8421#endif /* not USE_TOOLKIT_SCROLL_BARS */
dbc4e1c1
JB
8422}
8423
06a2c219 8424/* This processes Expose events from the menu-bar specific X event
19126e11 8425 loop in xmenu.c. This allows to redisplay the frame if necessary
06a2c219 8426 when handling menu-bar or pop-up items. */
3afe33e7 8427
06a2c219 8428int
3afe33e7
RS
8429process_expose_from_menu (event)
8430 XEvent event;
8431{
8432 FRAME_PTR f;
19126e11 8433 struct x_display_info *dpyinfo;
06a2c219 8434 int frame_exposed_p = 0;
3afe33e7 8435
f94397b5
KH
8436 BLOCK_INPUT;
8437
19126e11
KH
8438 dpyinfo = x_display_info_for_display (event.xexpose.display);
8439 f = x_window_to_frame (dpyinfo, event.xexpose.window);
3afe33e7
RS
8440 if (f)
8441 {
8442 if (f->async_visible == 0)
8443 {
8444 f->async_visible = 1;
8445 f->async_iconified = 0;
06c488fd 8446 f->output_data.x->has_been_visible = 1;
3afe33e7
RS
8447 SET_FRAME_GARBAGED (f);
8448 }
8449 else
8450 {
06a2c219
GM
8451 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
8452 event.xexpose.x, event.xexpose.y,
8453 event.xexpose.width, event.xexpose.height);
8454 frame_exposed_p = 1;
3afe33e7
RS
8455 }
8456 }
8457 else
8458 {
8459 struct scroll_bar *bar
8460 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 8461
3afe33e7
RS
8462 if (bar)
8463 x_scroll_bar_expose (bar, &event);
8464 }
f94397b5
KH
8465
8466 UNBLOCK_INPUT;
06a2c219 8467 return frame_exposed_p;
3afe33e7 8468}
09756a85
RS
8469\f
8470/* Define a queue to save up SelectionRequest events for later handling. */
8471
8472struct selection_event_queue
8473 {
8474 XEvent event;
8475 struct selection_event_queue *next;
8476 };
8477
8478static struct selection_event_queue *queue;
8479
8480/* Nonzero means queue up certain events--don't process them yet. */
06a2c219 8481
09756a85
RS
8482static int x_queue_selection_requests;
8483
8484/* Queue up an X event *EVENT, to be processed later. */
dbc4e1c1 8485
09756a85 8486static void
334208b7
RS
8487x_queue_event (f, event)
8488 FRAME_PTR f;
09756a85
RS
8489 XEvent *event;
8490{
8491 struct selection_event_queue *queue_tmp
06a2c219 8492 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
09756a85 8493
58769bee 8494 if (queue_tmp != NULL)
09756a85
RS
8495 {
8496 queue_tmp->event = *event;
8497 queue_tmp->next = queue;
8498 queue = queue_tmp;
8499 }
8500}
8501
8502/* Take all the queued events and put them back
8503 so that they get processed afresh. */
8504
8505static void
db3906fd
RS
8506x_unqueue_events (display)
8507 Display *display;
09756a85 8508{
58769bee 8509 while (queue != NULL)
09756a85
RS
8510 {
8511 struct selection_event_queue *queue_tmp = queue;
db3906fd 8512 XPutBackEvent (display, &queue_tmp->event);
09756a85 8513 queue = queue_tmp->next;
06a2c219 8514 xfree ((char *)queue_tmp);
09756a85
RS
8515 }
8516}
8517
8518/* Start queuing SelectionRequest events. */
8519
8520void
db3906fd
RS
8521x_start_queuing_selection_requests (display)
8522 Display *display;
09756a85
RS
8523{
8524 x_queue_selection_requests++;
8525}
8526
8527/* Stop queuing SelectionRequest events. */
8528
8529void
db3906fd
RS
8530x_stop_queuing_selection_requests (display)
8531 Display *display;
09756a85
RS
8532{
8533 x_queue_selection_requests--;
db3906fd 8534 x_unqueue_events (display);
09756a85 8535}
f451eb13
JB
8536\f
8537/* The main X event-reading loop - XTread_socket. */
dc6f92b8 8538
06a2c219 8539/* Time stamp of enter window event. This is only used by XTread_socket,
dc6f92b8
JB
8540 but we have to put it out here, since static variables within functions
8541 sometimes don't work. */
06a2c219 8542
dc6f92b8
JB
8543static Time enter_timestamp;
8544
11edeb03 8545/* This holds the state XLookupString needs to implement dead keys
58769bee 8546 and other tricks known as "compose processing". _X Window System_
11edeb03
JB
8547 says that a portable program can't use this, but Stephen Gildea assures
8548 me that letting the compiler initialize it to zeros will work okay.
8549
8550 This must be defined outside of XTread_socket, for the same reasons
06a2c219
GM
8551 given for enter_time stamp, above. */
8552
11edeb03
JB
8553static XComposeStatus compose_status;
8554
10e6549c
RS
8555/* Record the last 100 characters stored
8556 to help debug the loss-of-chars-during-GC problem. */
06a2c219 8557
2224b905
RS
8558static int temp_index;
8559static short temp_buffer[100];
10e6549c 8560
7a13e894
RS
8561/* Set this to nonzero to fake an "X I/O error"
8562 on a particular display. */
06a2c219 8563
7a13e894
RS
8564struct x_display_info *XTread_socket_fake_io_error;
8565
2224b905
RS
8566/* When we find no input here, we occasionally do a no-op command
8567 to verify that the X server is still running and we can still talk with it.
8568 We try all the open displays, one by one.
8569 This variable is used for cycling thru the displays. */
06a2c219 8570
2224b905
RS
8571static struct x_display_info *next_noop_dpyinfo;
8572
06a2c219
GM
8573#define SET_SAVED_MENU_EVENT(size) \
8574 do \
8575 { \
8576 if (f->output_data.x->saved_menu_event == 0) \
8577 f->output_data.x->saved_menu_event \
8578 = (XEvent *) xmalloc (sizeof (XEvent)); \
8579 bcopy (&event, f->output_data.x->saved_menu_event, size); \
8580 if (numchars >= 1) \
8581 { \
8582 bufp->kind = menu_bar_activate_event; \
8583 XSETFRAME (bufp->frame_or_window, f); \
8584 bufp++; \
8585 count++; \
8586 numchars--; \
8587 } \
8588 } \
8589 while (0)
8590
8805890a 8591#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
06a2c219 8592#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8805890a 8593
dc6f92b8
JB
8594/* Read events coming from the X server.
8595 This routine is called by the SIGIO handler.
8596 We return as soon as there are no more events to be read.
8597
8598 Events representing keys are stored in buffer BUFP,
8599 which can hold up to NUMCHARS characters.
8600 We return the number of characters stored into the buffer,
8601 thus pretending to be `read'.
8602
dc6f92b8
JB
8603 EXPECTED is nonzero if the caller knows input is available. */
8604
7c5283e4 8605int
f66868ba 8606XTread_socket (sd, bufp, numchars, expected)
dc6f92b8 8607 register int sd;
8805890a
KH
8608 /* register */ struct input_event *bufp;
8609 /* register */ int numchars;
dc6f92b8
JB
8610 int expected;
8611{
8612 int count = 0;
8613 int nbytes = 0;
dc6f92b8 8614 XEvent event;
f676886a 8615 struct frame *f;
66f55a9d 8616 int event_found = 0;
334208b7 8617 struct x_display_info *dpyinfo;
6c183ba5
RS
8618#ifdef HAVE_X_I18N
8619 Status status_return;
8620#endif
dc6f92b8 8621
9ac0d9e0 8622 if (interrupt_input_blocked)
dc6f92b8 8623 {
9ac0d9e0 8624 interrupt_input_pending = 1;
dc6f92b8
JB
8625 return -1;
8626 }
8627
9ac0d9e0 8628 interrupt_input_pending = 0;
dc6f92b8 8629 BLOCK_INPUT;
c0a04927
RS
8630
8631 /* So people can tell when we have read the available input. */
8632 input_signal_count++;
8633
dc6f92b8 8634 if (numchars <= 0)
06a2c219 8635 abort (); /* Don't think this happens. */
dc6f92b8 8636
7a13e894
RS
8637 /* Find the display we are supposed to read input for.
8638 It's the one communicating on descriptor SD. */
8639 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
8640 {
8641#if 0 /* This ought to be unnecessary; let's verify it. */
dc6f92b8 8642#ifdef FIOSNBIO
7a13e894
RS
8643 /* If available, Xlib uses FIOSNBIO to make the socket
8644 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
e6cbea31 8645 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
06a2c219 8646 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7a13e894 8647 fcntl (dpyinfo->connection, F_SETFL, 0);
c118dd06 8648#endif /* ! defined (FIOSNBIO) */
7a13e894 8649#endif
dc6f92b8 8650
7a13e894
RS
8651#if 0 /* This code can't be made to work, with multiple displays,
8652 and appears not to be used on any system any more.
8653 Also keyboard.c doesn't turn O_NDELAY on and off
8654 for X connections. */
dc6f92b8
JB
8655#ifndef SIGIO
8656#ifndef HAVE_SELECT
7a13e894
RS
8657 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
8658 {
8659 extern int read_alarm_should_throw;
8660 read_alarm_should_throw = 1;
8661 XPeekEvent (dpyinfo->display, &event);
8662 read_alarm_should_throw = 0;
8663 }
c118dd06
JB
8664#endif /* HAVE_SELECT */
8665#endif /* SIGIO */
7a13e894 8666#endif
dc6f92b8 8667
7a13e894
RS
8668 /* For debugging, this gives a way to fake an I/O error. */
8669 if (dpyinfo == XTread_socket_fake_io_error)
8670 {
8671 XTread_socket_fake_io_error = 0;
8672 x_io_error_quitter (dpyinfo->display);
8673 }
dc6f92b8 8674
06a2c219 8675 while (XPending (dpyinfo->display))
dc6f92b8 8676 {
7a13e894 8677 XNextEvent (dpyinfo->display, &event);
06a2c219
GM
8678
8679 if (display_busy_cursor_p)
8680 {
8681 /* Setting inhibit_busy_cursor to 2 inhibits busy-cursor
8682 display until the next X event is read and we come
8683 here again. Setting it to 1 inhibits busy-cursor
8684 display for direct commands. */
8685 if (event.type == MotionNotify
8686 || event.type == EnterNotify
8687 || (dpyinfo->grabbed
8688 && event.type != ButtonRelease))
8689 inhibit_busy_cursor = 2;
8690 else
8691 inhibit_busy_cursor = 1;
8692 }
8693
531483fb 8694#ifdef HAVE_X_I18N
d1bc4182
RS
8695 {
8696 struct frame *f1 = x_any_window_to_frame (dpyinfo,
c99babf2 8697 event.xclient.window);
d1bc4182
RS
8698 /* The necessity of the following line took me
8699 a full work-day to decipher from the docs!! */
8700 if (f1 != 0 && FRAME_XIC (f1) && XFilterEvent (&event, None))
8701 break;
8702 }
0cd6403b 8703#endif
7a13e894
RS
8704 event_found = 1;
8705
8706 switch (event.type)
8707 {
8708 case ClientMessage:
c047688c 8709 {
7a13e894
RS
8710 if (event.xclient.message_type
8711 == dpyinfo->Xatom_wm_protocols
8712 && event.xclient.format == 32)
c047688c 8713 {
7a13e894
RS
8714 if (event.xclient.data.l[0]
8715 == dpyinfo->Xatom_wm_take_focus)
c047688c 8716 {
8c1a6a84
RS
8717 /* Use x_any_window_to_frame because this
8718 could be the shell widget window
8719 if the frame has no title bar. */
8720 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6c183ba5
RS
8721#ifdef HAVE_X_I18N
8722 /* Not quite sure this is needed -pd */
8c1a6a84 8723 if (f && FRAME_XIC (f))
6c183ba5
RS
8724 XSetICFocus (FRAME_XIC (f));
8725#endif
bf7253f4
RS
8726 /* Since we set WM_TAKE_FOCUS, we must call
8727 XSetInputFocus explicitly. But not if f is null,
8728 since that might be an event for a deleted frame. */
7a13e894 8729 if (f)
bf7253f4
RS
8730 {
8731 Display *d = event.xclient.display;
8732 /* Catch and ignore errors, in case window has been
8733 iconified by a window manager such as GWM. */
8734 int count = x_catch_errors (d);
8735 XSetInputFocus (d, event.xclient.window,
e1f6572f
RS
8736 /* The ICCCM says this is
8737 the only valid choice. */
8738 RevertToParent,
bf7253f4
RS
8739 event.xclient.data.l[1]);
8740 /* This is needed to detect the error
8741 if there is an error. */
8742 XSync (d, False);
8743 x_uncatch_errors (d, count);
8744 }
7a13e894 8745 /* Not certain about handling scroll bars here */
c047688c 8746 }
7a13e894
RS
8747 else if (event.xclient.data.l[0]
8748 == dpyinfo->Xatom_wm_save_yourself)
8749 {
8750 /* Save state modify the WM_COMMAND property to
06a2c219 8751 something which can reinstate us. This notifies
7a13e894
RS
8752 the session manager, who's looking for such a
8753 PropertyNotify. Can restart processing when
06a2c219 8754 a keyboard or mouse event arrives. */
7a13e894
RS
8755 if (numchars > 0)
8756 {
19126e11
KH
8757 f = x_top_window_to_frame (dpyinfo,
8758 event.xclient.window);
7a13e894
RS
8759
8760 /* This is just so we only give real data once
8761 for a single Emacs process. */
b86bd3dd 8762 if (f == SELECTED_FRAME ())
7a13e894
RS
8763 XSetCommand (FRAME_X_DISPLAY (f),
8764 event.xclient.window,
8765 initial_argv, initial_argc);
f000f5c5 8766 else if (f)
7a13e894
RS
8767 XSetCommand (FRAME_X_DISPLAY (f),
8768 event.xclient.window,
8769 0, 0);
8770 }
8771 }
8772 else if (event.xclient.data.l[0]
8773 == dpyinfo->Xatom_wm_delete_window)
1fb20991 8774 {
19126e11
KH
8775 struct frame *f
8776 = x_any_window_to_frame (dpyinfo,
8777 event.xclient.window);
1fb20991 8778
7a13e894
RS
8779 if (f)
8780 {
8781 if (numchars == 0)
8782 abort ();
1fb20991 8783
7a13e894
RS
8784 bufp->kind = delete_window_event;
8785 XSETFRAME (bufp->frame_or_window, f);
8786 bufp++;
8787
8788 count += 1;
8789 numchars -= 1;
8790 }
1fb20991 8791 }
c047688c 8792 }
7a13e894
RS
8793 else if (event.xclient.message_type
8794 == dpyinfo->Xatom_wm_configure_denied)
8795 {
8796 }
8797 else if (event.xclient.message_type
8798 == dpyinfo->Xatom_wm_window_moved)
8799 {
8800 int new_x, new_y;
19126e11
KH
8801 struct frame *f
8802 = x_window_to_frame (dpyinfo, event.xclient.window);
58769bee 8803
7a13e894
RS
8804 new_x = event.xclient.data.s[0];
8805 new_y = event.xclient.data.s[1];
1fb20991 8806
7a13e894
RS
8807 if (f)
8808 {
7556890b
RS
8809 f->output_data.x->left_pos = new_x;
8810 f->output_data.x->top_pos = new_y;
7a13e894 8811 }
1fb20991 8812 }
0fdff6bb 8813#ifdef HACK_EDITRES
7a13e894
RS
8814 else if (event.xclient.message_type
8815 == dpyinfo->Xatom_editres)
8816 {
19126e11
KH
8817 struct frame *f
8818 = x_any_window_to_frame (dpyinfo, event.xclient.window);
7556890b 8819 _XEditResCheckMessages (f->output_data.x->widget, NULL,
19126e11 8820 &event, NULL);
7a13e894 8821 }
0fdff6bb 8822#endif /* HACK_EDITRES */
06a2c219
GM
8823 else if ((event.xclient.message_type
8824 == dpyinfo->Xatom_DONE)
8825 || (event.xclient.message_type
8826 == dpyinfo->Xatom_PAGE))
8827 {
8828 /* Ghostview job completed. Kill it. We could
8829 reply with "Next" if we received "Page", but we
8830 currently never do because we are interested in
8831 images, only, which should have 1 page. */
06a2c219
GM
8832 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
8833 struct frame *f
8834 = x_window_to_frame (dpyinfo, event.xclient.window);
8835 x_kill_gs_process (pixmap, f);
8836 expose_frame (f, 0, 0, 0, 0);
8837 }
8838#ifdef USE_TOOLKIT_SCROLL_BARS
8839 /* Scroll bar callbacks send a ClientMessage from which
8840 we construct an input_event. */
8841 else if (event.xclient.message_type
8842 == dpyinfo->Xatom_Scrollbar)
8843 {
8844 x_scroll_bar_to_input_event (&event, bufp);
8845 ++bufp, ++count, --numchars;
8846 goto out;
8847 }
8848#endif /* USE_TOOLKIT_SCROLL_BARS */
8849 else
8850 goto OTHER;
7a13e894
RS
8851 }
8852 break;
dc6f92b8 8853
7a13e894 8854 case SelectionNotify:
3afe33e7 8855#ifdef USE_X_TOOLKIT
19126e11 8856 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
7a13e894 8857 goto OTHER;
3afe33e7 8858#endif /* not USE_X_TOOLKIT */
dfcf069d 8859 x_handle_selection_notify (&event.xselection);
7a13e894 8860 break;
d56a553a 8861
06a2c219 8862 case SelectionClear: /* Someone has grabbed ownership. */
3afe33e7 8863#ifdef USE_X_TOOLKIT
19126e11 8864 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
7a13e894 8865 goto OTHER;
3afe33e7 8866#endif /* USE_X_TOOLKIT */
7a13e894
RS
8867 {
8868 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
d56a553a 8869
7a13e894
RS
8870 if (numchars == 0)
8871 abort ();
d56a553a 8872
7a13e894
RS
8873 bufp->kind = selection_clear_event;
8874 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
8875 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
8876 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 8877 bufp->frame_or_window = Qnil;
7a13e894 8878 bufp++;
d56a553a 8879
7a13e894
RS
8880 count += 1;
8881 numchars -= 1;
8882 }
8883 break;
dc6f92b8 8884
06a2c219 8885 case SelectionRequest: /* Someone wants our selection. */
3afe33e7 8886#ifdef USE_X_TOOLKIT
19126e11 8887 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
7a13e894 8888 goto OTHER;
3afe33e7 8889#endif /* USE_X_TOOLKIT */
7a13e894 8890 if (x_queue_selection_requests)
19126e11 8891 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
7a13e894
RS
8892 &event);
8893 else
8894 {
8895 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
dc6f92b8 8896
7a13e894
RS
8897 if (numchars == 0)
8898 abort ();
8899
8900 bufp->kind = selection_request_event;
8901 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
8902 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
8903 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
8904 SELECTION_EVENT_TARGET (bufp) = eventp->target;
8905 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
8906 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 8907 bufp->frame_or_window = Qnil;
7a13e894
RS
8908 bufp++;
8909
8910 count += 1;
8911 numchars -= 1;
8912 }
8913 break;
8914
8915 case PropertyNotify:
3afe33e7 8916#ifdef USE_X_TOOLKIT
19126e11 8917 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
7a13e894 8918 goto OTHER;
3afe33e7 8919#endif /* not USE_X_TOOLKIT */
dfcf069d 8920 x_handle_property_notify (&event.xproperty);
7a13e894 8921 break;
dc6f92b8 8922
7a13e894 8923 case ReparentNotify:
19126e11 8924 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
7a13e894
RS
8925 if (f)
8926 {
8927 int x, y;
7556890b 8928 f->output_data.x->parent_desc = event.xreparent.parent;
7a13e894 8929 x_real_positions (f, &x, &y);
7556890b
RS
8930 f->output_data.x->left_pos = x;
8931 f->output_data.x->top_pos = y;
7a13e894
RS
8932 }
8933 break;
3bd330d4 8934
7a13e894 8935 case Expose:
19126e11 8936 f = x_window_to_frame (dpyinfo, event.xexpose.window);
7a13e894 8937 if (f)
dc6f92b8 8938 {
7a13e894
RS
8939 if (f->async_visible == 0)
8940 {
8941 f->async_visible = 1;
8942 f->async_iconified = 0;
06c488fd 8943 f->output_data.x->has_been_visible = 1;
7a13e894
RS
8944 SET_FRAME_GARBAGED (f);
8945 }
8946 else
06a2c219
GM
8947 expose_frame (x_window_to_frame (dpyinfo,
8948 event.xexpose.window),
8949 event.xexpose.x, event.xexpose.y,
8950 event.xexpose.width, event.xexpose.height);
dc6f92b8
JB
8951 }
8952 else
7a13e894 8953 {
06a2c219
GM
8954#ifdef USE_TOOLKIT_SCROLL_BARS
8955 /* Dispatch event to the widget. */
8956 goto OTHER;
8957#else /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
8958 struct scroll_bar *bar
8959 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 8960
7a13e894
RS
8961 if (bar)
8962 x_scroll_bar_expose (bar, &event);
3afe33e7 8963#ifdef USE_X_TOOLKIT
7a13e894
RS
8964 else
8965 goto OTHER;
3afe33e7 8966#endif /* USE_X_TOOLKIT */
06a2c219 8967#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
8968 }
8969 break;
dc6f92b8 8970
7a13e894
RS
8971 case GraphicsExpose: /* This occurs when an XCopyArea's
8972 source area was obscured or not
8973 available.*/
19126e11 8974 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
7a13e894
RS
8975 if (f)
8976 {
06a2c219
GM
8977 expose_frame (f,
8978 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
8979 event.xgraphicsexpose.width,
8980 event.xgraphicsexpose.height);
7a13e894 8981 }
3afe33e7 8982#ifdef USE_X_TOOLKIT
7a13e894
RS
8983 else
8984 goto OTHER;
3afe33e7 8985#endif /* USE_X_TOOLKIT */
7a13e894 8986 break;
dc6f92b8 8987
7a13e894 8988 case NoExpose: /* This occurs when an XCopyArea's
06a2c219
GM
8989 source area was completely
8990 available */
7a13e894 8991 break;
dc6f92b8 8992
7a13e894 8993 case UnmapNotify:
06a2c219
GM
8994 /* Redo the mouse-highlight after the tooltip has gone. */
8995 if (event.xmap.window == tip_window)
8996 {
8997 tip_window = 0;
8998 redo_mouse_highlight ();
8999 }
9000
91ea2a7a 9001 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
7a13e894
RS
9002 if (f) /* F may no longer exist if
9003 the frame was deleted. */
9004 {
9005 /* While a frame is unmapped, display generation is
9006 disabled; you don't want to spend time updating a
9007 display that won't ever be seen. */
9008 f->async_visible = 0;
9009 /* We can't distinguish, from the event, whether the window
9010 has become iconified or invisible. So assume, if it
9011 was previously visible, than now it is iconified.
1aa6072f
RS
9012 But x_make_frame_invisible clears both
9013 the visible flag and the iconified flag;
9014 and that way, we know the window is not iconified now. */
7a13e894 9015 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
1aa6072f
RS
9016 {
9017 f->async_iconified = 1;
bddd097c 9018
1aa6072f
RS
9019 bufp->kind = iconify_event;
9020 XSETFRAME (bufp->frame_or_window, f);
9021 bufp++;
9022 count++;
9023 numchars--;
9024 }
7a13e894 9025 }
7a13e894 9026 goto OTHER;
dc6f92b8 9027
7a13e894 9028 case MapNotify:
06a2c219
GM
9029 if (event.xmap.window == tip_window)
9030 /* The tooltip has been drawn already. Avoid
9031 the SET_FRAME_GARBAGED below. */
9032 goto OTHER;
9033
9034 /* We use x_top_window_to_frame because map events can
9035 come for sub-windows and they don't mean that the
9036 frame is visible. */
19126e11 9037 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
7a13e894
RS
9038 if (f)
9039 {
9040 f->async_visible = 1;
9041 f->async_iconified = 0;
06c488fd 9042 f->output_data.x->has_been_visible = 1;
dc6f92b8 9043
7a13e894
RS
9044 /* wait_reading_process_input will notice this and update
9045 the frame's display structures. */
9046 SET_FRAME_GARBAGED (f);
bddd097c 9047
d806e720
RS
9048 if (f->iconified)
9049 {
9050 bufp->kind = deiconify_event;
9051 XSETFRAME (bufp->frame_or_window, f);
9052 bufp++;
9053 count++;
9054 numchars--;
9055 }
e73ec6fa 9056 else if (! NILP (Vframe_list)
8e713be6 9057 && ! NILP (XCDR (Vframe_list)))
78aa2ba5
KH
9058 /* Force a redisplay sooner or later
9059 to update the frame titles
9060 in case this is the second frame. */
9061 record_asynch_buffer_change ();
7a13e894 9062 }
7a13e894 9063 goto OTHER;
dc6f92b8 9064
7a13e894 9065 case KeyPress:
19126e11 9066 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
f451eb13 9067
06a2c219
GM
9068#ifdef USE_MOTIF
9069 /* I couldn't find a way to prevent LessTif scroll bars
9070 from consuming key events. */
9071 if (f == 0)
9072 {
9073 Widget widget = XtWindowToWidget (dpyinfo->display,
9074 event.xkey.window);
9075 if (widget && XmIsScrollBar (widget))
9076 {
9077 widget = XtParent (widget);
9078 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9079 }
9080 }
9081#endif /* USE_MOTIF */
9082
7a13e894
RS
9083 if (f != 0)
9084 {
9085 KeySym keysym, orig_keysym;
9086 /* al%imercury@uunet.uu.net says that making this 81 instead of
9087 80 fixed a bug whereby meta chars made his Emacs hang. */
9088 unsigned char copy_buffer[81];
9089 int modifiers;
64bb1782 9090
7a13e894
RS
9091 event.xkey.state
9092 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9093 extra_keyboard_modifiers);
9094 modifiers = event.xkey.state;
3a2712f9 9095
7a13e894 9096 /* This will have to go some day... */
752a043f 9097
7a13e894
RS
9098 /* make_lispy_event turns chars into control chars.
9099 Don't do it here because XLookupString is too eager. */
9100 event.xkey.state &= ~ControlMask;
5d46f928
RS
9101 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9102 | dpyinfo->super_mod_mask
9103 | dpyinfo->hyper_mod_mask
9104 | dpyinfo->alt_mod_mask);
9105
1cf4a0d1
RS
9106 /* In case Meta is ComposeCharacter,
9107 clear its status. According to Markus Ehrnsperger
9108 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9109 this enables ComposeCharacter to work whether or
9110 not it is combined with Meta. */
9111 if (modifiers & dpyinfo->meta_mod_mask)
9112 bzero (&compose_status, sizeof (compose_status));
9113
6c183ba5
RS
9114#ifdef HAVE_X_I18N
9115 if (FRAME_XIC (f))
9116 {
5d7cc324
RS
9117 /* The necessity of the following line took me
9118 a full work-day to decipher from the docs!! */
9119 if (XFilterEvent (&event, None))
9120 break;
6c183ba5
RS
9121 nbytes = XmbLookupString (FRAME_XIC (f),
9122 &event.xkey, copy_buffer,
9123 80, &keysym,
9124 &status_return);
1decb680
PE
9125 if (status_return == XLookupNone)
9126 break;
9127 else if (status_return == XLookupChars)
9128 keysym = NoSymbol;
9129 else if (status_return != XLookupKeySym
9130 && status_return != XLookupBoth)
9131 abort ();
6c183ba5
RS
9132 }
9133 else
9134 nbytes = XLookupString (&event.xkey, copy_buffer,
9135 80, &keysym, &compose_status);
9136#else
0299d313
RS
9137 nbytes = XLookupString (&event.xkey, copy_buffer,
9138 80, &keysym, &compose_status);
6c183ba5 9139#endif
dc6f92b8 9140
7a13e894 9141 orig_keysym = keysym;
55123275 9142
7a13e894
RS
9143 if (numchars > 1)
9144 {
9145 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9146 || keysym == XK_Delete
1097aea0 9147#ifdef XK_ISO_Left_Tab
441affdb 9148 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
1097aea0 9149#endif
852bff8f 9150 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
7a13e894
RS
9151 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9152 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
c34790e0 9153#ifdef HPUX
7a13e894
RS
9154 /* This recognizes the "extended function keys".
9155 It seems there's no cleaner way.
9156 Test IsModifierKey to avoid handling mode_switch
9157 incorrectly. */
9158 || ((unsigned) (keysym) >= XK_Select
9159 && (unsigned)(keysym) < XK_KP_Space)
69388238
RS
9160#endif
9161#ifdef XK_dead_circumflex
7a13e894 9162 || orig_keysym == XK_dead_circumflex
69388238
RS
9163#endif
9164#ifdef XK_dead_grave
7a13e894 9165 || orig_keysym == XK_dead_grave
69388238
RS
9166#endif
9167#ifdef XK_dead_tilde
7a13e894 9168 || orig_keysym == XK_dead_tilde
69388238
RS
9169#endif
9170#ifdef XK_dead_diaeresis
7a13e894 9171 || orig_keysym == XK_dead_diaeresis
69388238
RS
9172#endif
9173#ifdef XK_dead_macron
7a13e894 9174 || orig_keysym == XK_dead_macron
69388238
RS
9175#endif
9176#ifdef XK_dead_degree
7a13e894 9177 || orig_keysym == XK_dead_degree
69388238
RS
9178#endif
9179#ifdef XK_dead_acute
7a13e894 9180 || orig_keysym == XK_dead_acute
69388238
RS
9181#endif
9182#ifdef XK_dead_cedilla
7a13e894 9183 || orig_keysym == XK_dead_cedilla
69388238
RS
9184#endif
9185#ifdef XK_dead_breve
7a13e894 9186 || orig_keysym == XK_dead_breve
69388238
RS
9187#endif
9188#ifdef XK_dead_ogonek
7a13e894 9189 || orig_keysym == XK_dead_ogonek
69388238
RS
9190#endif
9191#ifdef XK_dead_caron
7a13e894 9192 || orig_keysym == XK_dead_caron
69388238
RS
9193#endif
9194#ifdef XK_dead_doubleacute
7a13e894 9195 || orig_keysym == XK_dead_doubleacute
69388238
RS
9196#endif
9197#ifdef XK_dead_abovedot
7a13e894 9198 || orig_keysym == XK_dead_abovedot
c34790e0 9199#endif
7a13e894
RS
9200 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9201 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9202 /* Any "vendor-specific" key is ok. */
9203 || (orig_keysym & (1 << 28)))
9204 && ! (IsModifierKey (orig_keysym)
7719aa06
RS
9205#ifndef HAVE_X11R5
9206#ifdef XK_Mode_switch
7a13e894 9207 || ((unsigned)(orig_keysym) == XK_Mode_switch)
7719aa06
RS
9208#endif
9209#ifdef XK_Num_Lock
7a13e894 9210 || ((unsigned)(orig_keysym) == XK_Num_Lock)
7719aa06
RS
9211#endif
9212#endif /* not HAVE_X11R5 */
7a13e894 9213 ))
dc6f92b8 9214 {
10e6549c
RS
9215 if (temp_index == sizeof temp_buffer / sizeof (short))
9216 temp_index = 0;
7a13e894
RS
9217 temp_buffer[temp_index++] = keysym;
9218 bufp->kind = non_ascii_keystroke;
9219 bufp->code = keysym;
e0c1aef2 9220 XSETFRAME (bufp->frame_or_window, f);
334208b7
RS
9221 bufp->modifiers
9222 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9223 modifiers);
1113d9db 9224 bufp->timestamp = event.xkey.time;
dc6f92b8 9225 bufp++;
7a13e894
RS
9226 count++;
9227 numchars--;
06a2c219
GM
9228
9229 if (display_busy_cursor_p)
9230 if (keysym != XK_Return || minibuf_level == 0)
9231 inhibit_busy_cursor = 2;
dc6f92b8 9232 }
7a13e894
RS
9233 else if (numchars > nbytes)
9234 {
9235 register int i;
9236
9237 for (i = 0; i < nbytes; i++)
9238 {
9239 if (temp_index == sizeof temp_buffer / sizeof (short))
9240 temp_index = 0;
9241 temp_buffer[temp_index++] = copy_buffer[i];
9242 bufp->kind = ascii_keystroke;
9243 bufp->code = copy_buffer[i];
9244 XSETFRAME (bufp->frame_or_window, f);
9245 bufp->modifiers
9246 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9247 modifiers);
9248 bufp->timestamp = event.xkey.time;
9249 bufp++;
9250 }
9251
9252 count += nbytes;
9253 numchars -= nbytes;
1decb680
PE
9254
9255 if (keysym == NoSymbol)
9256 break;
7a13e894
RS
9257 }
9258 else
9259 abort ();
dc6f92b8 9260 }
10e6549c
RS
9261 else
9262 abort ();
dc6f92b8 9263 }
717ca130 9264 goto OTHER;
f451eb13 9265
7a13e894 9266 /* Here's a possible interpretation of the whole
06a2c219
GM
9267 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
9268 you get a FocusIn event, you have to get a FocusOut
9269 event before you relinquish the focus. If you
9270 haven't received a FocusIn event, then a mere
9271 LeaveNotify is enough to free you. */
f451eb13 9272
7a13e894 9273 case EnterNotify:
06a2c219
GM
9274 {
9275 int from_menu_bar_p = 0;
9276
9277 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
9278
9279#ifdef LESSTIF_VERSION
9280 /* When clicking outside of a menu bar popup to close
9281 it, we get a FocusIn/ EnterNotify sequence of
9282 events. The flag event.xcrossing.focus is not set
9283 in the EnterNotify event of that sequence because
9284 the focus is in the menu bar,
9285 event.xcrossing.window is the frame's X window.
9286 Unconditionally setting the focus frame to null in
9287 this case is not the right thing, because no event
9288 follows that could set the focus frame to the right
9289 value.
9290
9291 This could be a LessTif bug, but I wasn't able to
9292 reproduce the behavior in a simple test program.
9293
9294 (gerd, LessTif 0.88.1). */
9295
9296 if (!event.xcrossing.focus
9297 && f
9298 && f->output_data.x->menubar_widget)
9299 {
9300 Window focus;
9301 int revert;
9302
9303 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9304 if (focus == XtWindow (f->output_data.x->menubar_widget))
9305 from_menu_bar_p = 1;
9306 }
9307#endif /* LESSTIF_VERSION */
6d4238f3 9308
06a2c219
GM
9309 if (event.xcrossing.focus || from_menu_bar_p)
9310 {
9311 /* Avoid nasty pop/raise loops. */
9312 if (f && (!(f->auto_raise)
9313 || !(f->auto_lower)
9314 || (event.xcrossing.time - enter_timestamp) > 500))
9315 {
9316 x_new_focus_frame (dpyinfo, f);
9317 enter_timestamp = event.xcrossing.time;
9318 }
9319 }
9320 else if (f == dpyinfo->x_focus_frame)
9321 x_new_focus_frame (dpyinfo, 0);
9322
9323 /* EnterNotify counts as mouse movement,
9324 so update things that depend on mouse position. */
9325 if (f && !f->output_data.x->busy_p)
9326 note_mouse_movement (f, &event.xmotion);
9327 goto OTHER;
9328 }
dc6f92b8 9329
7a13e894 9330 case FocusIn:
19126e11 9331 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 9332 if (event.xfocus.detail != NotifyPointer)
0f941935 9333 dpyinfo->x_focus_event_frame = f;
7a13e894 9334 if (f)
0f941935 9335 x_new_focus_frame (dpyinfo, f);
f9e24cb9 9336
6c183ba5
RS
9337#ifdef HAVE_X_I18N
9338 if (f && FRAME_XIC (f))
9339 XSetICFocus (FRAME_XIC (f));
9340#endif
9341
7a13e894 9342 goto OTHER;
10c5e63d 9343
7a13e894 9344 case LeaveNotify:
19126e11 9345 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
7a13e894 9346 if (f)
10c5e63d 9347 {
06a2c219
GM
9348 Lisp_Object frame;
9349 int from_menu_bar_p = 0;
9350
7a13e894 9351 if (f == dpyinfo->mouse_face_mouse_frame)
06a2c219
GM
9352 {
9353 /* If we move outside the frame, then we're
9354 certainly no longer on any text in the frame. */
9355 clear_mouse_face (dpyinfo);
9356 dpyinfo->mouse_face_mouse_frame = 0;
9357 }
9358
9359 /* Generate a nil HELP_EVENT to cancel a help-echo.
9360 Do it only if there's something to cancel.
9361 Otherwise, the startup message is cleared when
9362 the mouse leaves the frame. */
9363 if (any_help_event_p)
9364 {
9365 XSETFRAME (frame, f);
9366 bufp->kind = HELP_EVENT;
9367 bufp->frame_or_window = Fcons (frame, Qnil);
9368 ++bufp, ++count, --numchars;
9369 }
7a13e894 9370
06a2c219
GM
9371#ifdef LESSTIF_VERSION
9372 /* Please see the comment at the start of the
9373 EnterNotify case. */
9374 if (!event.xcrossing.focus
9375 && f->output_data.x->menubar_widget)
9376 {
9377 Window focus;
9378 int revert;
9379 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9380 if (focus == XtWindow (f->output_data.x->menubar_widget))
9381 from_menu_bar_p = 1;
9382 }
9383#endif /* LESSTIF_VERSION */
9384
9385 if (event.xcrossing.focus || from_menu_bar_p)
0f941935 9386 x_mouse_leave (dpyinfo);
10c5e63d 9387 else
7a13e894 9388 {
0f941935
KH
9389 if (f == dpyinfo->x_focus_event_frame)
9390 dpyinfo->x_focus_event_frame = 0;
9391 if (f == dpyinfo->x_focus_frame)
9392 x_new_focus_frame (dpyinfo, 0);
7a13e894 9393 }
10c5e63d 9394 }
7a13e894 9395 goto OTHER;
dc6f92b8 9396
7a13e894 9397 case FocusOut:
19126e11 9398 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 9399 if (event.xfocus.detail != NotifyPointer
0f941935
KH
9400 && f == dpyinfo->x_focus_event_frame)
9401 dpyinfo->x_focus_event_frame = 0;
9402 if (f && f == dpyinfo->x_focus_frame)
9403 x_new_focus_frame (dpyinfo, 0);
f9e24cb9 9404
6c183ba5
RS
9405#ifdef HAVE_X_I18N
9406 if (f && FRAME_XIC (f))
9407 XUnsetICFocus (FRAME_XIC (f));
9408#endif
9409
7a13e894 9410 goto OTHER;
dc6f92b8 9411
7a13e894 9412 case MotionNotify:
dc6f92b8 9413 {
06a2c219
GM
9414 previous_help_echo = help_echo;
9415 help_echo = Qnil;
9416
7a13e894
RS
9417 if (dpyinfo->grabbed && last_mouse_frame
9418 && FRAME_LIVE_P (last_mouse_frame))
9419 f = last_mouse_frame;
9420 else
19126e11 9421 f = x_window_to_frame (dpyinfo, event.xmotion.window);
06a2c219 9422
7a13e894
RS
9423 if (f)
9424 note_mouse_movement (f, &event.xmotion);
9425 else
9426 {
06a2c219 9427#ifndef USE_X_TOOLKIT
7a13e894
RS
9428 struct scroll_bar *bar
9429 = x_window_to_scroll_bar (event.xmotion.window);
f451eb13 9430
7a13e894
RS
9431 if (bar)
9432 x_scroll_bar_note_movement (bar, &event);
06a2c219 9433#endif /* USE_X_TOOLKIT */
b8009dd1 9434
06a2c219
GM
9435 /* If we move outside the frame, then we're
9436 certainly no longer on any text in the frame. */
7a13e894
RS
9437 clear_mouse_face (dpyinfo);
9438 }
06a2c219
GM
9439
9440 /* If the contents of the global variable help_echo
9441 has changed, generate a HELP_EVENT. */
9442 if (STRINGP (help_echo)
9443 || STRINGP (previous_help_echo))
9444 {
9445 Lisp_Object frame;
9446
9447 if (f)
9448 XSETFRAME (frame, f);
9449 else
9450 frame = Qnil;
9451
9452 any_help_event_p = 1;
9453 bufp->kind = HELP_EVENT;
9454 bufp->frame_or_window = Fcons (frame, help_echo);
9455 ++bufp, ++count, --numchars;
9456 }
9457
9458 goto OTHER;
dc6f92b8 9459 }
dc6f92b8 9460
7a13e894 9461 case ConfigureNotify:
9829ddba
RS
9462 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
9463 if (f)
af395ec1 9464 {
5c187dee 9465#ifndef USE_X_TOOLKIT
bf1b7b30
KH
9466 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
9467 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
5c187dee 9468
2d7fc7e8
RS
9469 /* In the toolkit version, change_frame_size
9470 is called by the code that handles resizing
9471 of the EmacsFrame widget. */
7a13e894 9472
7a13e894
RS
9473 /* Even if the number of character rows and columns has
9474 not changed, the font size may have changed, so we need
9475 to check the pixel dimensions as well. */
9476 if (columns != f->width
9477 || rows != f->height
7556890b
RS
9478 || event.xconfigure.width != f->output_data.x->pixel_width
9479 || event.xconfigure.height != f->output_data.x->pixel_height)
7a13e894 9480 {
7d1e984f 9481 change_frame_size (f, rows, columns, 0, 1, 0);
7a13e894 9482 SET_FRAME_GARBAGED (f);
e687d06e 9483 cancel_mouse_face (f);
7a13e894 9484 }
2d7fc7e8 9485#endif
af395ec1 9486
7556890b
RS
9487 f->output_data.x->pixel_width = event.xconfigure.width;
9488 f->output_data.x->pixel_height = event.xconfigure.height;
7a13e894
RS
9489
9490 /* What we have now is the position of Emacs's own window.
9491 Convert that to the position of the window manager window. */
dcb07ae9
RS
9492 x_real_positions (f, &f->output_data.x->left_pos,
9493 &f->output_data.x->top_pos);
9494
9495 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
9496 {
9497 /* Since the WM decorations come below top_pos now,
9498 we must put them below top_pos in the future. */
9499 f->output_data.x->win_gravity = NorthWestGravity;
9500 x_wm_set_size_hint (f, (long) 0, 0);
9501 }
8f08dc93
KH
9502#ifdef USE_MOTIF
9503 /* Some window managers pass (0,0) as the location of
9504 the window, and the Motif event handler stores it
9505 in the emacs widget, which messes up Motif menus. */
9506 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
9507 {
9508 event.xconfigure.x = f->output_data.x->widget->core.x;
9509 event.xconfigure.y = f->output_data.x->widget->core.y;
9510 }
06a2c219 9511#endif /* USE_MOTIF */
7a13e894 9512 }
2d7fc7e8 9513 goto OTHER;
dc6f92b8 9514
7a13e894
RS
9515 case ButtonPress:
9516 case ButtonRelease:
9517 {
9518 /* If we decide we want to generate an event to be seen
9519 by the rest of Emacs, we put it here. */
9520 struct input_event emacs_event;
9ea173e8 9521 int tool_bar_p = 0;
06a2c219 9522
7a13e894 9523 emacs_event.kind = no_event;
7a13e894 9524 bzero (&compose_status, sizeof (compose_status));
9b07615b 9525
06a2c219
GM
9526 if (dpyinfo->grabbed
9527 && last_mouse_frame
9f67f20b
RS
9528 && FRAME_LIVE_P (last_mouse_frame))
9529 f = last_mouse_frame;
9530 else
2224b905 9531 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9f67f20b 9532
06a2c219
GM
9533 if (f)
9534 {
9ea173e8
GM
9535 /* Is this in the tool-bar? */
9536 if (WINDOWP (f->tool_bar_window)
9537 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
06a2c219
GM
9538 {
9539 Lisp_Object window;
9540 int p, x, y;
9541
9542 x = event.xbutton.x;
9543 y = event.xbutton.y;
9544
9545 /* Set x and y. */
9546 window = window_from_coordinates (f, x, y, &p, 1);
9ea173e8 9547 if (EQ (window, f->tool_bar_window))
06a2c219 9548 {
9ea173e8
GM
9549 x_handle_tool_bar_click (f, &event.xbutton);
9550 tool_bar_p = 1;
06a2c219
GM
9551 }
9552 }
9553
9ea173e8 9554 if (!tool_bar_p)
06a2c219
GM
9555 if (!dpyinfo->x_focus_frame
9556 || f == dpyinfo->x_focus_frame)
9557 construct_mouse_click (&emacs_event, &event, f);
7a13e894
RS
9558 }
9559 else
9560 {
06a2c219 9561#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
9562 struct scroll_bar *bar
9563 = x_window_to_scroll_bar (event.xbutton.window);
f451eb13 9564
7a13e894
RS
9565 if (bar)
9566 x_scroll_bar_handle_click (bar, &event, &emacs_event);
06a2c219 9567#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9568 }
9569
9570 if (event.type == ButtonPress)
9571 {
9572 dpyinfo->grabbed |= (1 << event.xbutton.button);
9573 last_mouse_frame = f;
edad46f6
KH
9574 /* Ignore any mouse motion that happened
9575 before this event; any subsequent mouse-movement
9576 Emacs events should reflect only motion after
9577 the ButtonPress. */
a00e91cd
KH
9578 if (f != 0)
9579 f->mouse_moved = 0;
06a2c219 9580
9ea173e8
GM
9581 if (!tool_bar_p)
9582 last_tool_bar_item = -1;
06a2c219
GM
9583 if (display_busy_cursor_p)
9584 inhibit_busy_cursor = 2;
7a13e894 9585 }
3afe33e7
RS
9586 else
9587 {
7a13e894 9588 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
3afe33e7 9589 }
23faf38f 9590
7a13e894
RS
9591 if (numchars >= 1 && emacs_event.kind != no_event)
9592 {
9593 bcopy (&emacs_event, bufp, sizeof (struct input_event));
9594 bufp++;
9595 count++;
9596 numchars--;
9597 }
3afe33e7
RS
9598
9599#ifdef USE_X_TOOLKIT
2224b905
RS
9600 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
9601 /* For a down-event in the menu bar,
9602 don't pass it to Xt right now.
9603 Instead, save it away
9604 and we will pass it to Xt from kbd_buffer_get_event.
9605 That way, we can run some Lisp code first. */
91375f8f
RS
9606 if (f && event.type == ButtonPress
9607 /* Verify the event is really within the menu bar
9608 and not just sent to it due to grabbing. */
9609 && event.xbutton.x >= 0
9610 && event.xbutton.x < f->output_data.x->pixel_width
9611 && event.xbutton.y >= 0
9612 && event.xbutton.y < f->output_data.x->menubar_height
9613 && event.xbutton.same_screen)
2224b905 9614 {
8805890a 9615 SET_SAVED_BUTTON_EVENT;
2237cac9
RS
9616 XSETFRAME (last_mouse_press_frame, f);
9617 }
9618 else if (event.type == ButtonPress)
9619 {
9620 last_mouse_press_frame = Qnil;
30e671c3 9621 goto OTHER;
ce89ef46 9622 }
06a2c219 9623
2237cac9
RS
9624#ifdef USE_MOTIF /* This should do not harm for Lucid,
9625 but I am trying to be cautious. */
ce89ef46
RS
9626 else if (event.type == ButtonRelease)
9627 {
2237cac9 9628 if (!NILP (last_mouse_press_frame))
f10ded1c 9629 {
2237cac9
RS
9630 f = XFRAME (last_mouse_press_frame);
9631 if (f->output_data.x)
06a2c219 9632 SET_SAVED_BUTTON_EVENT;
f10ded1c 9633 }
06a2c219 9634 else
30e671c3 9635 goto OTHER;
2224b905 9636 }
2237cac9 9637#endif /* USE_MOTIF */
2224b905
RS
9638 else
9639 goto OTHER;
3afe33e7 9640#endif /* USE_X_TOOLKIT */
7a13e894
RS
9641 }
9642 break;
dc6f92b8 9643
7a13e894 9644 case CirculateNotify:
06a2c219
GM
9645 goto OTHER;
9646
7a13e894 9647 case CirculateRequest:
06a2c219
GM
9648 goto OTHER;
9649
9650 case VisibilityNotify:
9651 goto OTHER;
dc6f92b8 9652
7a13e894
RS
9653 case MappingNotify:
9654 /* Someone has changed the keyboard mapping - update the
9655 local cache. */
9656 switch (event.xmapping.request)
9657 {
9658 case MappingModifier:
9659 x_find_modifier_meanings (dpyinfo);
9660 /* This is meant to fall through. */
9661 case MappingKeyboard:
9662 XRefreshKeyboardMapping (&event.xmapping);
9663 }
7a13e894 9664 goto OTHER;
dc6f92b8 9665
7a13e894 9666 default:
7a13e894 9667 OTHER:
717ca130 9668#ifdef USE_X_TOOLKIT
7a13e894
RS
9669 BLOCK_INPUT;
9670 XtDispatchEvent (&event);
9671 UNBLOCK_INPUT;
3afe33e7 9672#endif /* USE_X_TOOLKIT */
7a13e894
RS
9673 break;
9674 }
dc6f92b8
JB
9675 }
9676 }
9677
06a2c219
GM
9678 out:;
9679
9a5196d0
RS
9680 /* On some systems, an X bug causes Emacs to get no more events
9681 when the window is destroyed. Detect that. (1994.) */
58769bee 9682 if (! event_found)
ef2a22d0 9683 {
ef2a22d0
RS
9684 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
9685 One XNOOP in 100 loops will make Emacs terminate.
9686 B. Bretthauer, 1994 */
9687 x_noop_count++;
58769bee 9688 if (x_noop_count >= 100)
ef2a22d0
RS
9689 {
9690 x_noop_count=0;
2224b905
RS
9691
9692 if (next_noop_dpyinfo == 0)
9693 next_noop_dpyinfo = x_display_list;
9694
9695 XNoOp (next_noop_dpyinfo->display);
9696
9697 /* Each time we get here, cycle through the displays now open. */
9698 next_noop_dpyinfo = next_noop_dpyinfo->next;
ef2a22d0
RS
9699 }
9700 }
502add23 9701
06a2c219 9702 /* If the focus was just given to an auto-raising frame,
0134a210 9703 raise it now. */
7a13e894 9704 /* ??? This ought to be able to handle more than one such frame. */
0134a210
RS
9705 if (pending_autoraise_frame)
9706 {
9707 x_raise_frame (pending_autoraise_frame);
9708 pending_autoraise_frame = 0;
9709 }
0134a210 9710
dc6f92b8
JB
9711 UNBLOCK_INPUT;
9712 return count;
9713}
06a2c219
GM
9714
9715
9716
dc6f92b8 9717\f
06a2c219
GM
9718/***********************************************************************
9719 Text Cursor
9720 ***********************************************************************/
9721
9722/* Note if the text cursor of window W has been overwritten by a
9723 drawing operation that outputs N glyphs starting at HPOS in the
9724 line given by output_cursor.vpos. N < 0 means all the rest of the
9725 line after HPOS has been written. */
9726
9727static void
9728note_overwritten_text_cursor (w, hpos, n)
9729 struct window *w;
9730 int hpos, n;
9731{
9732 if (updated_area == TEXT_AREA
9733 && output_cursor.vpos == w->phys_cursor.vpos
9734 && hpos <= w->phys_cursor.hpos
9735 && (n < 0
9736 || hpos + n > w->phys_cursor.hpos))
9737 w->phys_cursor_on_p = 0;
9738}
f451eb13
JB
9739
9740
06a2c219
GM
9741/* Set clipping for output in glyph row ROW. W is the window in which
9742 we operate. GC is the graphics context to set clipping in.
9743 WHOLE_LINE_P non-zero means include the areas used for truncation
9744 mark display and alike in the clipping rectangle.
9745
9746 ROW may be a text row or, e.g., a mode line. Text rows must be
9747 clipped to the interior of the window dedicated to text display,
9748 mode lines must be clipped to the whole window. */
dc6f92b8
JB
9749
9750static void
06a2c219
GM
9751x_clip_to_row (w, row, gc, whole_line_p)
9752 struct window *w;
9753 struct glyph_row *row;
9754 GC gc;
9755 int whole_line_p;
dc6f92b8 9756{
06a2c219
GM
9757 struct frame *f = XFRAME (WINDOW_FRAME (w));
9758 XRectangle clip_rect;
9759 int window_x, window_y, window_width, window_height;
dc6f92b8 9760
06a2c219 9761 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5c1aae96 9762
06a2c219
GM
9763 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
9764 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
9765 clip_rect.y = max (clip_rect.y, window_y);
9766 clip_rect.width = window_width;
9767 clip_rect.height = row->visible_height;
5c1aae96 9768
06a2c219
GM
9769 /* If clipping to the whole line, including trunc marks, extend
9770 the rectangle to the left and increase its width. */
9771 if (whole_line_p)
9772 {
110859fc
GM
9773 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
9774 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
06a2c219 9775 }
5c1aae96 9776
06a2c219 9777 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
dc6f92b8
JB
9778}
9779
06a2c219
GM
9780
9781/* Draw a hollow box cursor on window W in glyph row ROW. */
dc6f92b8
JB
9782
9783static void
06a2c219
GM
9784x_draw_hollow_cursor (w, row)
9785 struct window *w;
9786 struct glyph_row *row;
dc6f92b8 9787{
06a2c219
GM
9788 struct frame *f = XFRAME (WINDOW_FRAME (w));
9789 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9790 Display *dpy = FRAME_X_DISPLAY (f);
9791 int x, y, wd, h;
9792 XGCValues xgcv;
9793 struct glyph *cursor_glyph;
9794 GC gc;
9795
9796 /* Compute frame-relative coordinates from window-relative
9797 coordinates. */
9798 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9799 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
9800 + row->ascent - w->phys_cursor_ascent);
9801 h = row->height - 1;
9802
9803 /* Get the glyph the cursor is on. If we can't tell because
9804 the current matrix is invalid or such, give up. */
9805 cursor_glyph = get_phys_cursor_glyph (w);
9806 if (cursor_glyph == NULL)
dc6f92b8
JB
9807 return;
9808
06a2c219
GM
9809 /* Compute the width of the rectangle to draw. If on a stretch
9810 glyph, and `x-stretch-block-cursor' is nil, don't draw a
9811 rectangle as wide as the glyph, but use a canonical character
9812 width instead. */
9813 wd = cursor_glyph->pixel_width - 1;
9814 if (cursor_glyph->type == STRETCH_GLYPH
9815 && !x_stretch_cursor_p)
9816 wd = min (CANON_X_UNIT (f), wd);
9817
9818 /* The foreground of cursor_gc is typically the same as the normal
9819 background color, which can cause the cursor box to be invisible. */
9820 xgcv.foreground = f->output_data.x->cursor_pixel;
9821 if (dpyinfo->scratch_cursor_gc)
9822 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
9823 else
9824 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
9825 GCForeground, &xgcv);
9826 gc = dpyinfo->scratch_cursor_gc;
9827
9828 /* Set clipping, draw the rectangle, and reset clipping again. */
9829 x_clip_to_row (w, row, gc, 0);
9830 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
9831 XSetClipMask (dpy, gc, None);
dc6f92b8
JB
9832}
9833
06a2c219
GM
9834
9835/* Draw a bar cursor on window W in glyph row ROW.
9836
9837 Implementation note: One would like to draw a bar cursor with an
9838 angle equal to the one given by the font property XA_ITALIC_ANGLE.
9839 Unfortunately, I didn't find a font yet that has this property set.
9840 --gerd. */
dc6f92b8
JB
9841
9842static void
06a2c219
GM
9843x_draw_bar_cursor (w, row)
9844 struct window *w;
9845 struct glyph_row *row;
dc6f92b8 9846{
06a2c219
GM
9847 /* If cursor hpos is out of bounds, don't draw garbage. This can
9848 happen in mini-buffer windows when switching between echo area
9849 glyphs and mini-buffer. */
9850 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
9851 {
9852 struct frame *f = XFRAME (w->frame);
9853 struct glyph *cursor_glyph;
9854 GC gc;
9855 int x;
9856 unsigned long mask;
9857 XGCValues xgcv;
9858 Display *dpy;
9859 Window window;
9860
9861 cursor_glyph = get_phys_cursor_glyph (w);
9862 if (cursor_glyph == NULL)
9863 return;
9864
9865 xgcv.background = f->output_data.x->cursor_pixel;
9866 xgcv.foreground = f->output_data.x->cursor_pixel;
9867 xgcv.graphics_exposures = 0;
9868 mask = GCForeground | GCBackground | GCGraphicsExposures;
9869 dpy = FRAME_X_DISPLAY (f);
9870 window = FRAME_X_WINDOW (f);
9871 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
9872
9873 if (gc)
9874 XChangeGC (dpy, gc, mask, &xgcv);
9875 else
9876 {
9877 gc = XCreateGC (dpy, window, mask, &xgcv);
9878 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
9879 }
9880
9881 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9882 x_clip_to_row (w, row, gc, 0);
9883 XFillRectangle (dpy, window, gc,
9884 x,
9885 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
9886 min (cursor_glyph->pixel_width,
9887 f->output_data.x->cursor_width),
9888 row->height);
9889 XSetClipMask (dpy, gc, None);
9890 }
dc6f92b8
JB
9891}
9892
06a2c219
GM
9893
9894/* Clear the cursor of window W to background color, and mark the
9895 cursor as not shown. This is used when the text where the cursor
9896 is is about to be rewritten. */
9897
dc6f92b8 9898static void
06a2c219
GM
9899x_clear_cursor (w)
9900 struct window *w;
dc6f92b8 9901{
06a2c219
GM
9902 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
9903 x_update_window_cursor (w, 0);
9904}
90e65f07 9905
dbc4e1c1 9906
06a2c219
GM
9907/* Draw the cursor glyph of window W in glyph row ROW. See the
9908 comment of x_draw_glyphs for the meaning of HL. */
dbc4e1c1 9909
06a2c219
GM
9910static void
9911x_draw_phys_cursor_glyph (w, row, hl)
9912 struct window *w;
9913 struct glyph_row *row;
9914 enum draw_glyphs_face hl;
9915{
9916 /* If cursor hpos is out of bounds, don't draw garbage. This can
9917 happen in mini-buffer windows when switching between echo area
9918 glyphs and mini-buffer. */
9919 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
66ac4b0e
GM
9920 {
9921 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
9922 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
9923 hl, 0, 0, 0);
9924
9925 /* When we erase the cursor, and ROW is overlapped by other
9926 rows, make sure that these overlapping parts of other rows
9927 are redrawn. */
9928 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
9929 {
9930 if (row > w->current_matrix->rows
9931 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
9932 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
9933
9934 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
9935 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
9936 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
9937 }
9938 }
06a2c219 9939}
dbc4e1c1 9940
eea6af04 9941
06a2c219 9942/* Erase the image of a cursor of window W from the screen. */
eea6af04 9943
06a2c219
GM
9944static void
9945x_erase_phys_cursor (w)
9946 struct window *w;
9947{
9948 struct frame *f = XFRAME (w->frame);
9949 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9950 int hpos = w->phys_cursor.hpos;
9951 int vpos = w->phys_cursor.vpos;
9952 int mouse_face_here_p = 0;
9953 struct glyph_matrix *active_glyphs = w->current_matrix;
9954 struct glyph_row *cursor_row;
9955 struct glyph *cursor_glyph;
9956 enum draw_glyphs_face hl;
9957
9958 /* No cursor displayed or row invalidated => nothing to do on the
9959 screen. */
9960 if (w->phys_cursor_type == NO_CURSOR)
9961 goto mark_cursor_off;
9962
9963 /* VPOS >= active_glyphs->nrows means that window has been resized.
9964 Don't bother to erase the cursor. */
9965 if (vpos >= active_glyphs->nrows)
9966 goto mark_cursor_off;
9967
9968 /* If row containing cursor is marked invalid, there is nothing we
9969 can do. */
9970 cursor_row = MATRIX_ROW (active_glyphs, vpos);
9971 if (!cursor_row->enabled_p)
9972 goto mark_cursor_off;
9973
9974 /* This can happen when the new row is shorter than the old one.
9975 In this case, either x_draw_glyphs or clear_end_of_line
9976 should have cleared the cursor. Note that we wouldn't be
9977 able to erase the cursor in this case because we don't have a
9978 cursor glyph at hand. */
9979 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
9980 goto mark_cursor_off;
9981
9982 /* If the cursor is in the mouse face area, redisplay that when
9983 we clear the cursor. */
9984 if (w == XWINDOW (dpyinfo->mouse_face_window)
9985 && (vpos > dpyinfo->mouse_face_beg_row
9986 || (vpos == dpyinfo->mouse_face_beg_row
9987 && hpos >= dpyinfo->mouse_face_beg_col))
9988 && (vpos < dpyinfo->mouse_face_end_row
9989 || (vpos == dpyinfo->mouse_face_end_row
9990 && hpos < dpyinfo->mouse_face_end_col))
9991 /* Don't redraw the cursor's spot in mouse face if it is at the
9992 end of a line (on a newline). The cursor appears there, but
9993 mouse highlighting does not. */
9994 && cursor_row->used[TEXT_AREA] > hpos)
9995 mouse_face_here_p = 1;
9996
9997 /* Maybe clear the display under the cursor. */
9998 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
9999 {
10000 int x;
045dee35 10001 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dbc4e1c1 10002
06a2c219
GM
10003 cursor_glyph = get_phys_cursor_glyph (w);
10004 if (cursor_glyph == NULL)
10005 goto mark_cursor_off;
dbc4e1c1 10006
06a2c219
GM
10007 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10008
10009 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10010 x,
045dee35 10011 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219
GM
10012 cursor_row->y)),
10013 cursor_glyph->pixel_width,
10014 cursor_row->visible_height,
10015 False);
dbc4e1c1 10016 }
06a2c219
GM
10017
10018 /* Erase the cursor by redrawing the character underneath it. */
10019 if (mouse_face_here_p)
10020 hl = DRAW_MOUSE_FACE;
10021 else if (cursor_row->inverse_p)
10022 hl = DRAW_INVERSE_VIDEO;
10023 else
10024 hl = DRAW_NORMAL_TEXT;
10025 x_draw_phys_cursor_glyph (w, cursor_row, hl);
dbc4e1c1 10026
06a2c219
GM
10027 mark_cursor_off:
10028 w->phys_cursor_on_p = 0;
10029 w->phys_cursor_type = NO_CURSOR;
dbc4e1c1
JB
10030}
10031
10032
06a2c219
GM
10033/* Display or clear cursor of window W. If ON is zero, clear the
10034 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10035 where to put the cursor is specified by HPOS, VPOS, X and Y. */
dbc4e1c1 10036
06a2c219
GM
10037void
10038x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10039 struct window *w;
10040 int on, hpos, vpos, x, y;
dbc4e1c1 10041{
06a2c219
GM
10042 struct frame *f = XFRAME (w->frame);
10043 int new_cursor_type;
10044 struct glyph_matrix *current_glyphs;
10045 struct glyph_row *glyph_row;
10046 struct glyph *glyph;
dbc4e1c1 10047
49d838ea 10048 /* This is pointless on invisible frames, and dangerous on garbaged
06a2c219
GM
10049 windows and frames; in the latter case, the frame or window may
10050 be in the midst of changing its size, and x and y may be off the
10051 window. */
10052 if (! FRAME_VISIBLE_P (f)
10053 || FRAME_GARBAGED_P (f)
10054 || vpos >= w->current_matrix->nrows
10055 || hpos >= w->current_matrix->matrix_w)
dc6f92b8
JB
10056 return;
10057
10058 /* If cursor is off and we want it off, return quickly. */
06a2c219 10059 if (!on && !w->phys_cursor_on_p)
dc6f92b8
JB
10060 return;
10061
06a2c219
GM
10062 current_glyphs = w->current_matrix;
10063 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10064 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10065
10066 /* If cursor row is not enabled, we don't really know where to
10067 display the cursor. */
10068 if (!glyph_row->enabled_p)
10069 {
10070 w->phys_cursor_on_p = 0;
10071 return;
10072 }
10073
10074 xassert (interrupt_input_blocked);
10075
10076 /* Set new_cursor_type to the cursor we want to be displayed. In a
10077 mini-buffer window, we want the cursor only to appear if we are
10078 reading input from this window. For the selected window, we want
10079 the cursor type given by the frame parameter. If explicitly
10080 marked off, draw no cursor. In all other cases, we want a hollow
10081 box cursor. */
9b4a7047
GM
10082 if (cursor_in_echo_area
10083 && FRAME_HAS_MINIBUF_P (f)
10084 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
06a2c219 10085 {
9b4a7047
GM
10086 if (w == XWINDOW (echo_area_window))
10087 new_cursor_type = FRAME_DESIRED_CURSOR (f);
06a2c219
GM
10088 else
10089 new_cursor_type = HOLLOW_BOX_CURSOR;
10090 }
06a2c219 10091 else
9b4a7047
GM
10092 {
10093 if (w != XWINDOW (selected_window)
10094 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
10095 {
10096 if (MINI_WINDOW_P (w))
10097 new_cursor_type = NO_CURSOR;
10098 else
10099 new_cursor_type = HOLLOW_BOX_CURSOR;
10100 }
10101 else if (w->cursor_off_p)
10102 new_cursor_type = NO_CURSOR;
10103 else
10104 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10105 }
06a2c219
GM
10106
10107 /* If cursor is currently being shown and we don't want it to be or
10108 it is in the wrong place, or the cursor type is not what we want,
dc6f92b8 10109 erase it. */
06a2c219 10110 if (w->phys_cursor_on_p
dc6f92b8 10111 && (!on
06a2c219
GM
10112 || w->phys_cursor.x != x
10113 || w->phys_cursor.y != y
10114 || new_cursor_type != w->phys_cursor_type))
10115 x_erase_phys_cursor (w);
10116
10117 /* If the cursor is now invisible and we want it to be visible,
10118 display it. */
10119 if (on && !w->phys_cursor_on_p)
10120 {
10121 w->phys_cursor_ascent = glyph_row->ascent;
10122 w->phys_cursor_height = glyph_row->height;
10123
10124 /* Set phys_cursor_.* before x_draw_.* is called because some
10125 of them may need the information. */
10126 w->phys_cursor.x = x;
10127 w->phys_cursor.y = glyph_row->y;
10128 w->phys_cursor.hpos = hpos;
10129 w->phys_cursor.vpos = vpos;
10130 w->phys_cursor_type = new_cursor_type;
10131 w->phys_cursor_on_p = 1;
10132
10133 switch (new_cursor_type)
dc6f92b8 10134 {
06a2c219
GM
10135 case HOLLOW_BOX_CURSOR:
10136 x_draw_hollow_cursor (w, glyph_row);
10137 break;
10138
10139 case FILLED_BOX_CURSOR:
10140 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10141 break;
10142
10143 case BAR_CURSOR:
10144 x_draw_bar_cursor (w, glyph_row);
10145 break;
10146
10147 case NO_CURSOR:
10148 break;
dc6f92b8 10149
06a2c219
GM
10150 default:
10151 abort ();
10152 }
dc6f92b8
JB
10153 }
10154
06a2c219 10155#ifndef XFlush
f676886a 10156 if (updating_frame != f)
334208b7 10157 XFlush (FRAME_X_DISPLAY (f));
06a2c219 10158#endif
dc6f92b8
JB
10159}
10160
06a2c219
GM
10161
10162/* Display the cursor on window W, or clear it. X and Y are window
10163 relative pixel coordinates. HPOS and VPOS are glyph matrix
10164 positions. If W is not the selected window, display a hollow
10165 cursor. ON non-zero means display the cursor at X, Y which
10166 correspond to HPOS, VPOS, otherwise it is cleared. */
5d46f928 10167
dfcf069d 10168void
06a2c219
GM
10169x_display_cursor (w, on, hpos, vpos, x, y)
10170 struct window *w;
10171 int on, hpos, vpos, x, y;
dc6f92b8 10172{
f94397b5 10173 BLOCK_INPUT;
06a2c219 10174 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
5d46f928
RS
10175 UNBLOCK_INPUT;
10176}
10177
06a2c219
GM
10178
10179/* Display the cursor on window W, or clear it, according to ON_P.
5d46f928
RS
10180 Don't change the cursor's position. */
10181
dfcf069d 10182void
06a2c219 10183x_update_cursor (f, on_p)
5d46f928 10184 struct frame *f;
5d46f928 10185{
06a2c219
GM
10186 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
10187}
10188
10189
10190/* Call x_update_window_cursor with parameter ON_P on all leaf windows
10191 in the window tree rooted at W. */
10192
10193static void
10194x_update_cursor_in_window_tree (w, on_p)
10195 struct window *w;
10196 int on_p;
10197{
10198 while (w)
10199 {
10200 if (!NILP (w->hchild))
10201 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10202 else if (!NILP (w->vchild))
10203 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10204 else
10205 x_update_window_cursor (w, on_p);
10206
10207 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10208 }
10209}
5d46f928 10210
f94397b5 10211
06a2c219
GM
10212/* Switch the display of W's cursor on or off, according to the value
10213 of ON. */
10214
10215static void
10216x_update_window_cursor (w, on)
10217 struct window *w;
10218 int on;
10219{
10220 BLOCK_INPUT;
10221 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
10222 w->phys_cursor.x, w->phys_cursor.y);
f94397b5 10223 UNBLOCK_INPUT;
dc6f92b8 10224}
06a2c219
GM
10225
10226
10227
dc6f92b8
JB
10228\f
10229/* Icons. */
10230
f676886a 10231/* Refresh bitmap kitchen sink icon for frame F
06a2c219 10232 when we get an expose event for it. */
dc6f92b8 10233
dfcf069d 10234void
f676886a
JB
10235refreshicon (f)
10236 struct frame *f;
dc6f92b8 10237{
06a2c219 10238 /* Normally, the window manager handles this function. */
dc6f92b8
JB
10239}
10240
dbc4e1c1 10241/* Make the x-window of frame F use the gnu icon bitmap. */
dc6f92b8
JB
10242
10243int
990ba854 10244x_bitmap_icon (f, file)
f676886a 10245 struct frame *f;
990ba854 10246 Lisp_Object file;
dc6f92b8 10247{
06a2c219 10248 int bitmap_id;
dc6f92b8 10249
c118dd06 10250 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
10251 return 1;
10252
990ba854 10253 /* Free up our existing icon bitmap if any. */
7556890b
RS
10254 if (f->output_data.x->icon_bitmap > 0)
10255 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10256 f->output_data.x->icon_bitmap = 0;
990ba854
RS
10257
10258 if (STRINGP (file))
7f2ae036
RS
10259 bitmap_id = x_create_bitmap_from_file (f, file);
10260 else
10261 {
990ba854 10262 /* Create the GNU bitmap if necessary. */
5bf01b68 10263 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
334208b7
RS
10264 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
10265 = x_create_bitmap_from_data (f, gnu_bits,
10266 gnu_width, gnu_height);
990ba854
RS
10267
10268 /* The first time we create the GNU bitmap,
06a2c219 10269 this increments the ref-count one extra time.
990ba854
RS
10270 As a result, the GNU bitmap is never freed.
10271 That way, we don't have to worry about allocating it again. */
334208b7 10272 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
990ba854 10273
334208b7 10274 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7f2ae036
RS
10275 }
10276
10277 x_wm_set_icon_pixmap (f, bitmap_id);
7556890b 10278 f->output_data.x->icon_bitmap = bitmap_id;
dc6f92b8
JB
10279
10280 return 0;
10281}
10282
10283
1be2d067
KH
10284/* Make the x-window of frame F use a rectangle with text.
10285 Use ICON_NAME as the text. */
dc6f92b8
JB
10286
10287int
f676886a
JB
10288x_text_icon (f, icon_name)
10289 struct frame *f;
dc6f92b8
JB
10290 char *icon_name;
10291{
c118dd06 10292 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
10293 return 1;
10294
1be2d067
KH
10295#ifdef HAVE_X11R4
10296 {
10297 XTextProperty text;
10298 text.value = (unsigned char *) icon_name;
10299 text.encoding = XA_STRING;
10300 text.format = 8;
10301 text.nitems = strlen (icon_name);
10302#ifdef USE_X_TOOLKIT
7556890b 10303 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
1be2d067
KH
10304 &text);
10305#else /* not USE_X_TOOLKIT */
10306 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
10307#endif /* not USE_X_TOOLKIT */
10308 }
10309#else /* not HAVE_X11R4 */
10310 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
10311#endif /* not HAVE_X11R4 */
58769bee 10312
7556890b
RS
10313 if (f->output_data.x->icon_bitmap > 0)
10314 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10315 f->output_data.x->icon_bitmap = 0;
b1c884c3 10316 x_wm_set_icon_pixmap (f, 0);
dc6f92b8
JB
10317
10318 return 0;
10319}
10320\f
e99db5a1
RS
10321#define X_ERROR_MESSAGE_SIZE 200
10322
10323/* If non-nil, this should be a string.
10324 It means catch X errors and store the error message in this string. */
10325
10326static Lisp_Object x_error_message_string;
10327
10328/* An X error handler which stores the error message in
10329 x_error_message_string. This is called from x_error_handler if
10330 x_catch_errors is in effect. */
10331
06a2c219 10332static void
e99db5a1
RS
10333x_error_catcher (display, error)
10334 Display *display;
10335 XErrorEvent *error;
10336{
10337 XGetErrorText (display, error->error_code,
10338 XSTRING (x_error_message_string)->data,
10339 X_ERROR_MESSAGE_SIZE);
10340}
10341
10342/* Begin trapping X errors for display DPY. Actually we trap X errors
10343 for all displays, but DPY should be the display you are actually
10344 operating on.
10345
10346 After calling this function, X protocol errors no longer cause
10347 Emacs to exit; instead, they are recorded in the string
10348 stored in x_error_message_string.
10349
10350 Calling x_check_errors signals an Emacs error if an X error has
10351 occurred since the last call to x_catch_errors or x_check_errors.
10352
10353 Calling x_uncatch_errors resumes the normal error handling. */
10354
10355void x_check_errors ();
10356static Lisp_Object x_catch_errors_unwind ();
10357
10358int
10359x_catch_errors (dpy)
10360 Display *dpy;
10361{
10362 int count = specpdl_ptr - specpdl;
10363
10364 /* Make sure any errors from previous requests have been dealt with. */
10365 XSync (dpy, False);
10366
10367 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
10368
10369 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
10370 XSTRING (x_error_message_string)->data[0] = 0;
10371
10372 return count;
10373}
10374
10375/* Unbind the binding that we made to check for X errors. */
10376
10377static Lisp_Object
10378x_catch_errors_unwind (old_val)
10379 Lisp_Object old_val;
10380{
10381 x_error_message_string = old_val;
10382 return Qnil;
10383}
10384
10385/* If any X protocol errors have arrived since the last call to
10386 x_catch_errors or x_check_errors, signal an Emacs error using
10387 sprintf (a buffer, FORMAT, the x error message text) as the text. */
10388
10389void
10390x_check_errors (dpy, format)
10391 Display *dpy;
10392 char *format;
10393{
10394 /* Make sure to catch any errors incurred so far. */
10395 XSync (dpy, False);
10396
10397 if (XSTRING (x_error_message_string)->data[0])
10398 error (format, XSTRING (x_error_message_string)->data);
10399}
10400
9829ddba
RS
10401/* Nonzero if we had any X protocol errors
10402 since we did x_catch_errors on DPY. */
e99db5a1
RS
10403
10404int
10405x_had_errors_p (dpy)
10406 Display *dpy;
10407{
10408 /* Make sure to catch any errors incurred so far. */
10409 XSync (dpy, False);
10410
10411 return XSTRING (x_error_message_string)->data[0] != 0;
10412}
10413
9829ddba
RS
10414/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
10415
06a2c219 10416void
9829ddba
RS
10417x_clear_errors (dpy)
10418 Display *dpy;
10419{
10420 XSTRING (x_error_message_string)->data[0] = 0;
10421}
10422
e99db5a1
RS
10423/* Stop catching X protocol errors and let them make Emacs die.
10424 DPY should be the display that was passed to x_catch_errors.
10425 COUNT should be the value that was returned by
10426 the corresponding call to x_catch_errors. */
10427
10428void
10429x_uncatch_errors (dpy, count)
10430 Display *dpy;
10431 int count;
10432{
10433 unbind_to (count, Qnil);
10434}
10435
10436#if 0
10437static unsigned int x_wire_count;
10438x_trace_wire ()
10439{
10440 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
10441}
10442#endif /* ! 0 */
10443
10444\f
10445/* Handle SIGPIPE, which can happen when the connection to a server
10446 simply goes away. SIGPIPE is handled by x_connection_signal.
10447 Don't need to do anything, because the write which caused the
10448 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
06a2c219 10449 which will do the appropriate cleanup for us. */
e99db5a1
RS
10450
10451static SIGTYPE
10452x_connection_signal (signalnum) /* If we don't have an argument, */
06a2c219 10453 int signalnum; /* some compilers complain in signal calls. */
e99db5a1
RS
10454{
10455#ifdef USG
10456 /* USG systems forget handlers when they are used;
10457 must reestablish each time */
10458 signal (signalnum, x_connection_signal);
10459#endif /* USG */
10460}
10461\f
4746118a
JB
10462/* Handling X errors. */
10463
7a13e894 10464/* Handle the loss of connection to display DISPLAY. */
16bd92ea 10465
4746118a 10466static SIGTYPE
7a13e894
RS
10467x_connection_closed (display, error_message)
10468 Display *display;
10469 char *error_message;
4746118a 10470{
7a13e894
RS
10471 struct x_display_info *dpyinfo = x_display_info_for_display (display);
10472 Lisp_Object frame, tail;
10473
6186a4a0
RS
10474 /* Indicate that this display is dead. */
10475
f613a4c8 10476#ifdef USE_X_TOOLKIT
adabc3a9 10477 XtCloseDisplay (display);
f613a4c8 10478#endif
adabc3a9 10479
6186a4a0
RS
10480 dpyinfo->display = 0;
10481
06a2c219 10482 /* First delete frames whose mini-buffers are on frames
7a13e894
RS
10483 that are on the dead display. */
10484 FOR_EACH_FRAME (tail, frame)
10485 {
10486 Lisp_Object minibuf_frame;
10487 minibuf_frame
10488 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
f48f33ca
RS
10489 if (FRAME_X_P (XFRAME (frame))
10490 && FRAME_X_P (XFRAME (minibuf_frame))
10491 && ! EQ (frame, minibuf_frame)
10492 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7a13e894
RS
10493 Fdelete_frame (frame, Qt);
10494 }
10495
10496 /* Now delete all remaining frames on the dead display.
06a2c219 10497 We are now sure none of these is used as the mini-buffer
7a13e894
RS
10498 for another frame that we need to delete. */
10499 FOR_EACH_FRAME (tail, frame)
f48f33ca
RS
10500 if (FRAME_X_P (XFRAME (frame))
10501 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
07a7096a
KH
10502 {
10503 /* Set this to t so that Fdelete_frame won't get confused
10504 trying to find a replacement. */
10505 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
10506 Fdelete_frame (frame, Qt);
10507 }
7a13e894 10508
482a1bd2
KH
10509 if (dpyinfo)
10510 x_delete_display (dpyinfo);
7a13e894
RS
10511
10512 if (x_display_list == 0)
10513 {
f8d07b62 10514 fprintf (stderr, "%s\n", error_message);
7a13e894
RS
10515 shut_down_emacs (0, 0, Qnil);
10516 exit (70);
10517 }
12ba150f 10518
7a13e894
RS
10519 /* Ordinary stack unwind doesn't deal with these. */
10520#ifdef SIGIO
10521 sigunblock (sigmask (SIGIO));
10522#endif
10523 sigunblock (sigmask (SIGALRM));
10524 TOTALLY_UNBLOCK_INPUT;
10525
aa4d9a9e 10526 clear_waiting_for_input ();
7a13e894 10527 error ("%s", error_message);
4746118a
JB
10528}
10529
7a13e894
RS
10530/* This is the usual handler for X protocol errors.
10531 It kills all frames on the display that we got the error for.
10532 If that was the only one, it prints an error message and kills Emacs. */
10533
06a2c219 10534static void
c118dd06
JB
10535x_error_quitter (display, error)
10536 Display *display;
10537 XErrorEvent *error;
10538{
7a13e894 10539 char buf[256], buf1[356];
dc6f92b8 10540
58769bee 10541 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 10542 original error handler. */
dc6f92b8 10543
c118dd06 10544 XGetErrorText (display, error->error_code, buf, sizeof (buf));
0a0fdc70 10545 sprintf (buf1, "X protocol error: %s on protocol request %d",
c118dd06 10546 buf, error->request_code);
7a13e894 10547 x_connection_closed (display, buf1);
dc6f92b8
JB
10548}
10549
e99db5a1
RS
10550/* This is the first-level handler for X protocol errors.
10551 It calls x_error_quitter or x_error_catcher. */
7a13e894 10552
8922af5f 10553static int
e99db5a1 10554x_error_handler (display, error)
8922af5f 10555 Display *display;
e99db5a1 10556 XErrorEvent *error;
8922af5f 10557{
e99db5a1
RS
10558 if (! NILP (x_error_message_string))
10559 x_error_catcher (display, error);
10560 else
10561 x_error_quitter (display, error);
06a2c219 10562 return 0;
f9e24cb9 10563}
c118dd06 10564
e99db5a1
RS
10565/* This is the handler for X IO errors, always.
10566 It kills all frames on the display that we lost touch with.
10567 If that was the only one, it prints an error message and kills Emacs. */
7a13e894 10568
c118dd06 10569static int
e99db5a1 10570x_io_error_quitter (display)
c118dd06 10571 Display *display;
c118dd06 10572{
e99db5a1 10573 char buf[256];
dc6f92b8 10574
e99db5a1
RS
10575 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
10576 x_connection_closed (display, buf);
06a2c219 10577 return 0;
dc6f92b8 10578}
dc6f92b8 10579\f
f451eb13
JB
10580/* Changing the font of the frame. */
10581
76bcdf39
RS
10582/* Give frame F the font named FONTNAME as its default font, and
10583 return the full name of that font. FONTNAME may be a wildcard
10584 pattern; in that case, we choose some font that fits the pattern.
10585 The return value shows which font we chose. */
10586
b5cf7a0e 10587Lisp_Object
f676886a
JB
10588x_new_font (f, fontname)
10589 struct frame *f;
dc6f92b8
JB
10590 register char *fontname;
10591{
dc43ef94
KH
10592 struct font_info *fontp
10593 = fs_load_font (f, FRAME_X_FONT_TABLE (f), CHARSET_ASCII, fontname, -1);
dc6f92b8 10594
dc43ef94
KH
10595 if (!fontp)
10596 return Qnil;
2224a5fc 10597
dc43ef94
KH
10598 f->output_data.x->font = (XFontStruct *) (fontp->font);
10599 f->output_data.x->font_baseline
10600 = (f->output_data.x->font->ascent + fontp->baseline_offset);
10601 f->output_data.x->fontset = -1;
10602
b2cad826
KH
10603 /* Compute the scroll bar width in character columns. */
10604 if (f->scroll_bar_pixel_width > 0)
10605 {
7556890b 10606 int wid = FONT_WIDTH (f->output_data.x->font);
b2cad826
KH
10607 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
10608 }
10609 else
4e61bddf
RS
10610 {
10611 int wid = FONT_WIDTH (f->output_data.x->font);
10612 f->scroll_bar_cols = (14 + wid - 1) / wid;
10613 }
b2cad826 10614
f676886a 10615 /* Now make the frame display the given font. */
c118dd06 10616 if (FRAME_X_WINDOW (f) != 0)
dc6f92b8 10617 {
7556890b
RS
10618 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
10619 f->output_data.x->font->fid);
10620 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
10621 f->output_data.x->font->fid);
10622 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
10623 f->output_data.x->font->fid);
f676886a 10624
a27f9f86 10625 frame_update_line_height (f);
0134a210 10626 x_set_window_size (f, 0, f->width, f->height);
dc6f92b8 10627 }
a27f9f86
RS
10628 else
10629 /* If we are setting a new frame's font for the first time,
10630 there are no faces yet, so this font's height is the line height. */
7556890b 10631 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
dc6f92b8 10632
dc43ef94
KH
10633 return build_string (fontp->full_name);
10634}
10635
10636/* Give frame F the fontset named FONTSETNAME as its default font, and
10637 return the full name of that fontset. FONTSETNAME may be a wildcard
b5210ea7
KH
10638 pattern; in that case, we choose some fontset that fits the pattern.
10639 The return value shows which fontset we chose. */
b5cf7a0e 10640
dc43ef94
KH
10641Lisp_Object
10642x_new_fontset (f, fontsetname)
10643 struct frame *f;
10644 char *fontsetname;
10645{
10646 int fontset = fs_query_fontset (f, fontsetname);
10647 struct fontset_info *fontsetp;
10648 Lisp_Object result;
b5cf7a0e 10649
dc43ef94
KH
10650 if (fontset < 0)
10651 return Qnil;
b5cf7a0e 10652
2da424f1
KH
10653 if (f->output_data.x->fontset == fontset)
10654 /* This fontset is already set in frame F. There's nothing more
10655 to do. */
10656 return build_string (fontsetname);
10657
dc43ef94
KH
10658 fontsetp = FRAME_FONTSET_DATA (f)->fontset_table[fontset];
10659
10660 if (!fontsetp->fontname[CHARSET_ASCII])
10661 /* This fontset doesn't contain ASCII font. */
10662 return Qnil;
10663
10664 result = x_new_font (f, fontsetp->fontname[CHARSET_ASCII]);
10665
10666 if (!STRINGP (result))
10667 /* Can't load ASCII font. */
10668 return Qnil;
10669
10670 /* Since x_new_font doesn't update any fontset information, do it now. */
10671 f->output_data.x->fontset = fontset;
2da424f1 10672 FS_LOAD_FONT (f, FRAME_X_FONT_TABLE (f),
715a159b 10673 CHARSET_ASCII, fontsetp->fontname[CHARSET_ASCII], fontset);
dc43ef94
KH
10674
10675 return build_string (fontsetname);
dc6f92b8 10676}
dc6f92b8 10677\f
2e365682
RS
10678/* Calculate the absolute position in frame F
10679 from its current recorded position values and gravity. */
10680
dfcf069d 10681void
43bca5d5 10682x_calc_absolute_position (f)
f676886a 10683 struct frame *f;
dc6f92b8 10684{
06a2c219 10685 Window child;
6dba1858 10686 int win_x = 0, win_y = 0;
7556890b 10687 int flags = f->output_data.x->size_hint_flags;
c81412a0
KH
10688 int this_window;
10689
9829ddba
RS
10690 /* We have nothing to do if the current position
10691 is already for the top-left corner. */
10692 if (! ((flags & XNegative) || (flags & YNegative)))
10693 return;
10694
c81412a0 10695#ifdef USE_X_TOOLKIT
7556890b 10696 this_window = XtWindow (f->output_data.x->widget);
c81412a0
KH
10697#else
10698 this_window = FRAME_X_WINDOW (f);
10699#endif
6dba1858
RS
10700
10701 /* Find the position of the outside upper-left corner of
9829ddba
RS
10702 the inner window, with respect to the outer window.
10703 But do this only if we will need the results. */
7556890b 10704 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6dba1858 10705 {
9829ddba
RS
10706 int count;
10707
6dba1858 10708 BLOCK_INPUT;
9829ddba
RS
10709 count = x_catch_errors (FRAME_X_DISPLAY (f));
10710 while (1)
10711 {
10712 x_clear_errors (FRAME_X_DISPLAY (f));
10713 XTranslateCoordinates (FRAME_X_DISPLAY (f),
10714
10715 /* From-window, to-window. */
10716 this_window,
10717 f->output_data.x->parent_desc,
10718
10719 /* From-position, to-position. */
10720 0, 0, &win_x, &win_y,
10721
10722 /* Child of win. */
10723 &child);
10724 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
10725 {
10726 Window newroot, newparent = 0xdeadbeef;
10727 Window *newchildren;
10728 int nchildren;
10729
10730 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
10731 &newparent, &newchildren, &nchildren))
10732 break;
58769bee 10733
7c3c78a3 10734 XFree ((char *) newchildren);
6dba1858 10735
9829ddba
RS
10736 f->output_data.x->parent_desc = newparent;
10737 }
10738 else
10739 break;
10740 }
6dba1858 10741
9829ddba 10742 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
6dba1858
RS
10743 UNBLOCK_INPUT;
10744 }
10745
10746 /* Treat negative positions as relative to the leftmost bottommost
10747 position that fits on the screen. */
20f55f9a 10748 if (flags & XNegative)
7556890b 10749 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
2e365682
RS
10750 - 2 * f->output_data.x->border_width - win_x
10751 - PIXEL_WIDTH (f)
10752 + f->output_data.x->left_pos);
dc6f92b8 10753
20f55f9a 10754 if (flags & YNegative)
06a2c219
GM
10755 {
10756 int menubar_height = 0;
10757
10758#ifdef USE_X_TOOLKIT
10759 if (f->output_data.x->menubar_widget)
10760 menubar_height
10761 = (f->output_data.x->menubar_widget->core.height
10762 + f->output_data.x->menubar_widget->core.border_width);
10763#endif
10764
10765 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
10766 - 2 * f->output_data.x->border_width
10767 - win_y
10768 - PIXEL_HEIGHT (f)
10769 - menubar_height
10770 + f->output_data.x->top_pos);
10771 }
2e365682 10772
3a35ab44
RS
10773 /* The left_pos and top_pos
10774 are now relative to the top and left screen edges,
10775 so the flags should correspond. */
7556890b 10776 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
dc6f92b8
JB
10777}
10778
3a35ab44
RS
10779/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
10780 to really change the position, and 0 when calling from
10781 x_make_frame_visible (in that case, XOFF and YOFF are the current
aa3ff7c9
KH
10782 position values). It is -1 when calling from x_set_frame_parameters,
10783 which means, do adjust for borders but don't change the gravity. */
3a35ab44 10784
dfcf069d 10785void
dc05a16b 10786x_set_offset (f, xoff, yoff, change_gravity)
f676886a 10787 struct frame *f;
dc6f92b8 10788 register int xoff, yoff;
dc05a16b 10789 int change_gravity;
dc6f92b8 10790{
4a4cbdd5
KH
10791 int modified_top, modified_left;
10792
aa3ff7c9 10793 if (change_gravity > 0)
3a35ab44 10794 {
7556890b
RS
10795 f->output_data.x->top_pos = yoff;
10796 f->output_data.x->left_pos = xoff;
10797 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
3a35ab44 10798 if (xoff < 0)
7556890b 10799 f->output_data.x->size_hint_flags |= XNegative;
3a35ab44 10800 if (yoff < 0)
7556890b
RS
10801 f->output_data.x->size_hint_flags |= YNegative;
10802 f->output_data.x->win_gravity = NorthWestGravity;
3a35ab44 10803 }
43bca5d5 10804 x_calc_absolute_position (f);
dc6f92b8
JB
10805
10806 BLOCK_INPUT;
c32cdd9a 10807 x_wm_set_size_hint (f, (long) 0, 0);
3a35ab44 10808
7556890b
RS
10809 modified_left = f->output_data.x->left_pos;
10810 modified_top = f->output_data.x->top_pos;
e73ec6fa
RS
10811#if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
10812 this seems to be unnecessary and incorrect. rms, 4/17/97. */
10813 /* It is a mystery why we need to add the border_width here
10814 when the frame is already visible, but experiment says we do. */
aa3ff7c9 10815 if (change_gravity != 0)
4a4cbdd5 10816 {
7556890b
RS
10817 modified_left += f->output_data.x->border_width;
10818 modified_top += f->output_data.x->border_width;
4a4cbdd5 10819 }
e73ec6fa 10820#endif
4a4cbdd5 10821
3afe33e7 10822#ifdef USE_X_TOOLKIT
7556890b 10823 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
4a4cbdd5 10824 modified_left, modified_top);
3afe33e7 10825#else /* not USE_X_TOOLKIT */
334208b7 10826 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4a4cbdd5 10827 modified_left, modified_top);
3afe33e7 10828#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
10829 UNBLOCK_INPUT;
10830}
10831
bc20ebbf
FP
10832/* Call this to change the size of frame F's x-window.
10833 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
10834 for this size change and subsequent size changes.
10835 Otherwise we leave the window gravity unchanged. */
dc6f92b8 10836
dfcf069d 10837void
bc20ebbf 10838x_set_window_size (f, change_gravity, cols, rows)
f676886a 10839 struct frame *f;
bc20ebbf 10840 int change_gravity;
b1c884c3 10841 int cols, rows;
dc6f92b8 10842{
06a2c219 10843#ifndef USE_X_TOOLKIT
dc6f92b8 10844 int pixelwidth, pixelheight;
06a2c219 10845#endif
dc6f92b8 10846
80fd1fe2 10847 BLOCK_INPUT;
aee9a898
RS
10848
10849#ifdef USE_X_TOOLKIT
3a20653d
RS
10850 {
10851 /* The x and y position of the widget is clobbered by the
10852 call to XtSetValues within EmacsFrameSetCharSize.
10853 This is a real kludge, but I don't understand Xt so I can't
10854 figure out a correct fix. Can anyone else tell me? -- rms. */
7556890b
RS
10855 int xpos = f->output_data.x->widget->core.x;
10856 int ypos = f->output_data.x->widget->core.y;
10857 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
10858 f->output_data.x->widget->core.x = xpos;
10859 f->output_data.x->widget->core.y = ypos;
3a20653d 10860 }
80fd1fe2
FP
10861
10862#else /* not USE_X_TOOLKIT */
10863
b1c884c3 10864 check_frame_size (f, &rows, &cols);
7556890b 10865 f->output_data.x->vertical_scroll_bar_extra
b2cad826
KH
10866 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
10867 ? 0
10868 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
480407eb 10869 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7556890b 10870 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
06a2c219 10871 f->output_data.x->flags_areas_extra
110859fc 10872 = FRAME_FLAGS_AREA_WIDTH (f);
f451eb13
JB
10873 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
10874 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
dc6f92b8 10875
7556890b 10876 f->output_data.x->win_gravity = NorthWestGravity;
c32cdd9a 10877 x_wm_set_size_hint (f, (long) 0, 0);
6ccf47d1 10878
334208b7
RS
10879 XSync (FRAME_X_DISPLAY (f), False);
10880 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10881 pixelwidth, pixelheight);
b1c884c3
JB
10882
10883 /* Now, strictly speaking, we can't be sure that this is accurate,
10884 but the window manager will get around to dealing with the size
10885 change request eventually, and we'll hear how it went when the
dbc4e1c1
JB
10886 ConfigureNotify event gets here.
10887
10888 We could just not bother storing any of this information here,
10889 and let the ConfigureNotify event set everything up, but that
fddd5ceb 10890 might be kind of confusing to the Lisp code, since size changes
dbc4e1c1 10891 wouldn't be reported in the frame parameters until some random
fddd5ceb
RS
10892 point in the future when the ConfigureNotify event arrives.
10893
10894 We pass 1 for DELAY since we can't run Lisp code inside of
10895 a BLOCK_INPUT. */
7d1e984f 10896 change_frame_size (f, rows, cols, 0, 1, 0);
b1c884c3
JB
10897 PIXEL_WIDTH (f) = pixelwidth;
10898 PIXEL_HEIGHT (f) = pixelheight;
10899
aee9a898
RS
10900 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
10901 receive in the ConfigureNotify event; if we get what we asked
10902 for, then the event won't cause the screen to become garbaged, so
10903 we have to make sure to do it here. */
10904 SET_FRAME_GARBAGED (f);
10905
10906 XFlush (FRAME_X_DISPLAY (f));
10907
10908#endif /* not USE_X_TOOLKIT */
10909
4d73d038 10910 /* If cursor was outside the new size, mark it as off. */
06a2c219 10911 mark_window_cursors_off (XWINDOW (f->root_window));
4d73d038 10912
aee9a898
RS
10913 /* Clear out any recollection of where the mouse highlighting was,
10914 since it might be in a place that's outside the new frame size.
10915 Actually checking whether it is outside is a pain in the neck,
10916 so don't try--just let the highlighting be done afresh with new size. */
e687d06e 10917 cancel_mouse_face (f);
dbc4e1c1 10918
dc6f92b8
JB
10919 UNBLOCK_INPUT;
10920}
dc6f92b8 10921\f
d047c4eb 10922/* Mouse warping. */
dc6f92b8 10923
9b378208 10924void
f676886a
JB
10925x_set_mouse_position (f, x, y)
10926 struct frame *f;
dc6f92b8
JB
10927 int x, y;
10928{
10929 int pix_x, pix_y;
10930
7556890b
RS
10931 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
10932 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
f451eb13
JB
10933
10934 if (pix_x < 0) pix_x = 0;
10935 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
10936
10937 if (pix_y < 0) pix_y = 0;
10938 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
dc6f92b8
JB
10939
10940 BLOCK_INPUT;
dc6f92b8 10941
334208b7
RS
10942 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
10943 0, 0, 0, 0, pix_x, pix_y);
dc6f92b8
JB
10944 UNBLOCK_INPUT;
10945}
10946
9b378208
RS
10947/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
10948
10949void
10950x_set_mouse_pixel_position (f, pix_x, pix_y)
10951 struct frame *f;
10952 int pix_x, pix_y;
10953{
10954 BLOCK_INPUT;
10955
334208b7
RS
10956 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
10957 0, 0, 0, 0, pix_x, pix_y);
9b378208
RS
10958 UNBLOCK_INPUT;
10959}
d047c4eb
KH
10960\f
10961/* focus shifting, raising and lowering. */
9b378208 10962
dfcf069d 10963void
f676886a
JB
10964x_focus_on_frame (f)
10965 struct frame *f;
dc6f92b8 10966{
1fb20991 10967#if 0 /* This proves to be unpleasant. */
f676886a 10968 x_raise_frame (f);
1fb20991 10969#endif
6d4238f3
JB
10970#if 0
10971 /* I don't think that the ICCCM allows programs to do things like this
10972 without the interaction of the window manager. Whatever you end up
f676886a 10973 doing with this code, do it to x_unfocus_frame too. */
334208b7 10974 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dc6f92b8 10975 RevertToPointerRoot, CurrentTime);
c118dd06 10976#endif /* ! 0 */
dc6f92b8
JB
10977}
10978
dfcf069d 10979void
f676886a
JB
10980x_unfocus_frame (f)
10981 struct frame *f;
dc6f92b8 10982{
6d4238f3 10983#if 0
f676886a 10984 /* Look at the remarks in x_focus_on_frame. */
0f941935 10985 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
334208b7 10986 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
dc6f92b8 10987 RevertToPointerRoot, CurrentTime);
c118dd06 10988#endif /* ! 0 */
dc6f92b8
JB
10989}
10990
f676886a 10991/* Raise frame F. */
dc6f92b8 10992
dfcf069d 10993void
f676886a
JB
10994x_raise_frame (f)
10995 struct frame *f;
dc6f92b8 10996{
3a88c238 10997 if (f->async_visible)
dc6f92b8
JB
10998 {
10999 BLOCK_INPUT;
3afe33e7 11000#ifdef USE_X_TOOLKIT
7556890b 11001 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 11002#else /* not USE_X_TOOLKIT */
334208b7 11003 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11004#endif /* not USE_X_TOOLKIT */
334208b7 11005 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
11006 UNBLOCK_INPUT;
11007 }
11008}
11009
f676886a 11010/* Lower frame F. */
dc6f92b8 11011
dfcf069d 11012void
f676886a
JB
11013x_lower_frame (f)
11014 struct frame *f;
dc6f92b8 11015{
3a88c238 11016 if (f->async_visible)
dc6f92b8
JB
11017 {
11018 BLOCK_INPUT;
3afe33e7 11019#ifdef USE_X_TOOLKIT
7556890b 11020 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 11021#else /* not USE_X_TOOLKIT */
334208b7 11022 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11023#endif /* not USE_X_TOOLKIT */
334208b7 11024 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
11025 UNBLOCK_INPUT;
11026 }
11027}
11028
dbc4e1c1 11029static void
6b0442dc 11030XTframe_raise_lower (f, raise_flag)
dbc4e1c1 11031 FRAME_PTR f;
6b0442dc 11032 int raise_flag;
dbc4e1c1 11033{
6b0442dc 11034 if (raise_flag)
dbc4e1c1
JB
11035 x_raise_frame (f);
11036 else
11037 x_lower_frame (f);
11038}
d047c4eb
KH
11039\f
11040/* Change of visibility. */
dc6f92b8 11041
9382638d
KH
11042/* This tries to wait until the frame is really visible.
11043 However, if the window manager asks the user where to position
11044 the frame, this will return before the user finishes doing that.
11045 The frame will not actually be visible at that time,
11046 but it will become visible later when the window manager
11047 finishes with it. */
11048
dfcf069d 11049void
f676886a
JB
11050x_make_frame_visible (f)
11051 struct frame *f;
dc6f92b8 11052{
990ba854 11053 Lisp_Object type;
1aa6072f 11054 int original_top, original_left;
dc6f92b8 11055
dc6f92b8 11056 BLOCK_INPUT;
dc6f92b8 11057
990ba854
RS
11058 type = x_icon_type (f);
11059 if (!NILP (type))
11060 x_bitmap_icon (f, type);
bdcd49ba 11061
f676886a 11062 if (! FRAME_VISIBLE_P (f))
90e65f07 11063 {
1aa6072f
RS
11064 /* We test FRAME_GARBAGED_P here to make sure we don't
11065 call x_set_offset a second time
11066 if we get to x_make_frame_visible a second time
11067 before the window gets really visible. */
11068 if (! FRAME_ICONIFIED_P (f)
11069 && ! f->output_data.x->asked_for_visible)
11070 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
11071
11072 f->output_data.x->asked_for_visible = 1;
11073
90e65f07 11074 if (! EQ (Vx_no_window_manager, Qt))
f676886a 11075 x_wm_set_window_state (f, NormalState);
3afe33e7 11076#ifdef USE_X_TOOLKIT
d7a38a2e 11077 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 11078 XtMapWidget (f->output_data.x->widget);
3afe33e7 11079#else /* not USE_X_TOOLKIT */
7f9c7f94 11080 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11081#endif /* not USE_X_TOOLKIT */
0134a210
RS
11082#if 0 /* This seems to bring back scroll bars in the wrong places
11083 if the window configuration has changed. They seem
11084 to come back ok without this. */
ab648270 11085 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
334208b7 11086 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
0134a210 11087#endif
90e65f07 11088 }
dc6f92b8 11089
334208b7 11090 XFlush (FRAME_X_DISPLAY (f));
90e65f07 11091
0dacf791
RS
11092 /* Synchronize to ensure Emacs knows the frame is visible
11093 before we do anything else. We do this loop with input not blocked
11094 so that incoming events are handled. */
11095 {
11096 Lisp_Object frame;
c0a04927 11097 int count = input_signal_count;
28c01ffe
RS
11098 /* This must be before UNBLOCK_INPUT
11099 since events that arrive in response to the actions above
11100 will set it when they are handled. */
11101 int previously_visible = f->output_data.x->has_been_visible;
1aa6072f
RS
11102
11103 original_left = f->output_data.x->left_pos;
11104 original_top = f->output_data.x->top_pos;
c0a04927
RS
11105
11106 /* This must come after we set COUNT. */
11107 UNBLOCK_INPUT;
11108
2745e6c4 11109 /* We unblock here so that arriving X events are processed. */
1aa6072f 11110
dcb07ae9
RS
11111 /* Now move the window back to where it was "supposed to be".
11112 But don't do it if the gravity is negative.
11113 When the gravity is negative, this uses a position
28c01ffe
RS
11114 that is 3 pixels too low. Perhaps that's really the border width.
11115
11116 Don't do this if the window has never been visible before,
11117 because the window manager may choose the position
11118 and we don't want to override it. */
1aa6072f 11119
4d3f5d9a 11120 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
06c488fd 11121 && f->output_data.x->win_gravity == NorthWestGravity
28c01ffe 11122 && previously_visible)
1aa6072f 11123 {
2745e6c4
RS
11124 Drawable rootw;
11125 int x, y;
11126 unsigned int width, height, border, depth;
06a2c219 11127
1aa6072f 11128 BLOCK_INPUT;
9829ddba 11129
06a2c219
GM
11130 /* On some window managers (such as FVWM) moving an existing
11131 window, even to the same place, causes the window manager
11132 to introduce an offset. This can cause the window to move
11133 to an unexpected location. Check the geometry (a little
11134 slow here) and then verify that the window is in the right
11135 place. If the window is not in the right place, move it
11136 there, and take the potential window manager hit. */
2745e6c4
RS
11137 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11138 &rootw, &x, &y, &width, &height, &border, &depth);
11139
11140 if (original_left != x || original_top != y)
11141 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11142 original_left, original_top);
11143
1aa6072f
RS
11144 UNBLOCK_INPUT;
11145 }
9829ddba 11146
e0c1aef2 11147 XSETFRAME (frame, f);
c0a04927
RS
11148
11149 while (1)
2a6cf806 11150 {
334208b7 11151 x_sync (f);
c0a04927
RS
11152 /* Once we have handled input events,
11153 we should have received the MapNotify if one is coming.
11154 So if we have not got it yet, stop looping.
11155 Some window managers make their own decisions
11156 about visibility. */
11157 if (input_signal_count != count)
11158 break;
c12a7cbd 11159 /* Machines that do polling rather than SIGIO have been observed
23cf7c60
KH
11160 to go into a busy-wait here. So we'll fake an alarm signal
11161 to let the handler know that there's something to be read.
11162 We used to raise a real alarm, but it seems that the handler
11163 isn't always enabled here. This is probably a bug. */
8b2f8d4e 11164 if (input_polling_used ())
3b2fa4e6
RS
11165 {
11166 /* It could be confusing if a real alarm arrives while processing
11167 the fake one. Turn it off and let the handler reset it. */
11168 alarm (0);
5e8e68ce 11169 input_poll_signal (0);
3b2fa4e6 11170 }
c0a04927
RS
11171 /* Once we have handled input events,
11172 we should have received the MapNotify if one is coming.
11173 So if we have not got it yet, stop looping.
11174 Some window managers make their own decisions
11175 about visibility. */
11176 if (input_signal_count != count)
11177 break;
2a6cf806 11178 }
0dacf791
RS
11179 FRAME_SAMPLE_VISIBILITY (f);
11180 }
dc6f92b8
JB
11181}
11182
06a2c219 11183/* Change from mapped state to withdrawn state. */
dc6f92b8 11184
d047c4eb
KH
11185/* Make the frame visible (mapped and not iconified). */
11186
dfcf069d 11187void
f676886a
JB
11188x_make_frame_invisible (f)
11189 struct frame *f;
dc6f92b8 11190{
546e6d5b
RS
11191 Window window;
11192
11193#ifdef USE_X_TOOLKIT
11194 /* Use the frame's outermost window, not the one we normally draw on. */
7556890b 11195 window = XtWindow (f->output_data.x->widget);
546e6d5b
RS
11196#else /* not USE_X_TOOLKIT */
11197 window = FRAME_X_WINDOW (f);
11198#endif /* not USE_X_TOOLKIT */
dc6f92b8 11199
9319ae23 11200 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
11201 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11202 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 11203
5627c40e 11204#if 0/* This might add unreliability; I don't trust it -- rms. */
9319ae23 11205 if (! f->async_visible && ! f->async_iconified)
dc6f92b8 11206 return;
5627c40e 11207#endif
dc6f92b8
JB
11208
11209 BLOCK_INPUT;
c118dd06 11210
af31d76f
RS
11211 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
11212 that the current position of the window is user-specified, rather than
11213 program-specified, so that when the window is mapped again, it will be
11214 placed at the same location, without forcing the user to position it
11215 by hand again (they have already done that once for this window.) */
c32cdd9a 11216 x_wm_set_size_hint (f, (long) 0, 1);
af31d76f 11217
c118dd06
JB
11218#ifdef HAVE_X11R4
11219
334208b7
RS
11220 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
11221 DefaultScreen (FRAME_X_DISPLAY (f))))
c118dd06
JB
11222 {
11223 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11224 error ("Can't notify window manager of window withdrawal");
c118dd06 11225 }
c118dd06 11226#else /* ! defined (HAVE_X11R4) */
16bd92ea 11227
c118dd06 11228 /* Tell the window manager what we're going to do. */
dc6f92b8
JB
11229 if (! EQ (Vx_no_window_manager, Qt))
11230 {
16bd92ea 11231 XEvent unmap;
dc6f92b8 11232
16bd92ea 11233 unmap.xunmap.type = UnmapNotify;
546e6d5b 11234 unmap.xunmap.window = window;
334208b7 11235 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
16bd92ea 11236 unmap.xunmap.from_configure = False;
334208b7
RS
11237 if (! XSendEvent (FRAME_X_DISPLAY (f),
11238 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea 11239 False,
06a2c219 11240 SubstructureRedirectMaskSubstructureNotifyMask,
16bd92ea
JB
11241 &unmap))
11242 {
11243 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11244 error ("Can't notify window manager of withdrawal");
16bd92ea 11245 }
dc6f92b8
JB
11246 }
11247
16bd92ea 11248 /* Unmap the window ourselves. Cheeky! */
334208b7 11249 XUnmapWindow (FRAME_X_DISPLAY (f), window);
c118dd06 11250#endif /* ! defined (HAVE_X11R4) */
dc6f92b8 11251
5627c40e
RS
11252 /* We can't distinguish this from iconification
11253 just by the event that we get from the server.
11254 So we can't win using the usual strategy of letting
11255 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
11256 and synchronize with the server to make sure we agree. */
11257 f->visible = 0;
11258 FRAME_ICONIFIED_P (f) = 0;
11259 f->async_visible = 0;
11260 f->async_iconified = 0;
11261
334208b7 11262 x_sync (f);
5627c40e 11263
dc6f92b8
JB
11264 UNBLOCK_INPUT;
11265}
11266
06a2c219 11267/* Change window state from mapped to iconified. */
dc6f92b8 11268
dfcf069d 11269void
f676886a
JB
11270x_iconify_frame (f)
11271 struct frame *f;
dc6f92b8 11272{
3afe33e7 11273 int result;
990ba854 11274 Lisp_Object type;
dc6f92b8 11275
9319ae23 11276 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
11277 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11278 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 11279
3a88c238 11280 if (f->async_iconified)
dc6f92b8
JB
11281 return;
11282
3afe33e7 11283 BLOCK_INPUT;
546e6d5b 11284
9af3143a
RS
11285 FRAME_SAMPLE_VISIBILITY (f);
11286
990ba854
RS
11287 type = x_icon_type (f);
11288 if (!NILP (type))
11289 x_bitmap_icon (f, type);
bdcd49ba
RS
11290
11291#ifdef USE_X_TOOLKIT
11292
546e6d5b
RS
11293 if (! FRAME_VISIBLE_P (f))
11294 {
11295 if (! EQ (Vx_no_window_manager, Qt))
11296 x_wm_set_window_state (f, IconicState);
11297 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 11298 XtMapWidget (f->output_data.x->widget);
9cf30a30
RS
11299 /* The server won't give us any event to indicate
11300 that an invisible frame was changed to an icon,
11301 so we have to record it here. */
11302 f->iconified = 1;
1e6bc770 11303 f->visible = 1;
9cf30a30 11304 f->async_iconified = 1;
1e6bc770 11305 f->async_visible = 0;
546e6d5b
RS
11306 UNBLOCK_INPUT;
11307 return;
11308 }
11309
334208b7 11310 result = XIconifyWindow (FRAME_X_DISPLAY (f),
7556890b 11311 XtWindow (f->output_data.x->widget),
334208b7 11312 DefaultScreen (FRAME_X_DISPLAY (f)));
3afe33e7
RS
11313 UNBLOCK_INPUT;
11314
11315 if (!result)
546e6d5b 11316 error ("Can't notify window manager of iconification");
3afe33e7
RS
11317
11318 f->async_iconified = 1;
1e6bc770
RS
11319 f->async_visible = 0;
11320
8c002a25
KH
11321
11322 BLOCK_INPUT;
334208b7 11323 XFlush (FRAME_X_DISPLAY (f));
8c002a25 11324 UNBLOCK_INPUT;
3afe33e7
RS
11325#else /* not USE_X_TOOLKIT */
11326
fd13dbb2
RS
11327 /* Make sure the X server knows where the window should be positioned,
11328 in case the user deiconifies with the window manager. */
11329 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
7556890b 11330 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
fd13dbb2 11331
16bd92ea
JB
11332 /* Since we don't know which revision of X we're running, we'll use both
11333 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
11334
11335 /* X11R4: send a ClientMessage to the window manager using the
11336 WM_CHANGE_STATE type. */
11337 {
11338 XEvent message;
58769bee 11339
c118dd06 11340 message.xclient.window = FRAME_X_WINDOW (f);
16bd92ea 11341 message.xclient.type = ClientMessage;
334208b7 11342 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
16bd92ea
JB
11343 message.xclient.format = 32;
11344 message.xclient.data.l[0] = IconicState;
11345
334208b7
RS
11346 if (! XSendEvent (FRAME_X_DISPLAY (f),
11347 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea
JB
11348 False,
11349 SubstructureRedirectMask | SubstructureNotifyMask,
11350 &message))
dc6f92b8
JB
11351 {
11352 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11353 error ("Can't notify window manager of iconification");
dc6f92b8 11354 }
16bd92ea 11355 }
dc6f92b8 11356
58769bee 11357 /* X11R3: set the initial_state field of the window manager hints to
16bd92ea
JB
11358 IconicState. */
11359 x_wm_set_window_state (f, IconicState);
dc6f92b8 11360
a9c00105
RS
11361 if (!FRAME_VISIBLE_P (f))
11362 {
11363 /* If the frame was withdrawn, before, we must map it. */
7f9c7f94 11364 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
a9c00105
RS
11365 }
11366
3a88c238 11367 f->async_iconified = 1;
1e6bc770 11368 f->async_visible = 0;
dc6f92b8 11369
334208b7 11370 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 11371 UNBLOCK_INPUT;
8c002a25 11372#endif /* not USE_X_TOOLKIT */
dc6f92b8 11373}
d047c4eb 11374\f
c0ff3fab 11375/* Destroy the X window of frame F. */
dc6f92b8 11376
dfcf069d 11377void
c0ff3fab 11378x_destroy_window (f)
f676886a 11379 struct frame *f;
dc6f92b8 11380{
7f9c7f94
RS
11381 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11382
dc6f92b8 11383 BLOCK_INPUT;
c0ff3fab 11384
6186a4a0
RS
11385 /* If a display connection is dead, don't try sending more
11386 commands to the X server. */
11387 if (dpyinfo->display != 0)
11388 {
11389 if (f->output_data.x->icon_desc != 0)
11390 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
31f41daf
RS
11391#ifdef HAVE_X_I18N
11392 if (FRAME_XIM (f))
11393 {
11394 XDestroyIC (FRAME_XIC (f));
408be661
RS
11395#if ! defined (SOLARIS2) || defined (HAVE_X11R6)
11396 /* This line causes crashes on Solaris with Openwin,
11397 due to an apparent bug in XCloseIM.
11398 X11R6 seems not to have the bug. */
31f41daf 11399 XCloseIM (FRAME_XIM (f));
a9978dd8 11400#endif
31f41daf
RS
11401 }
11402#endif
6186a4a0 11403 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->window_desc);
3afe33e7 11404#ifdef USE_X_TOOLKIT
06a2c219
GM
11405 if (f->output_data.x->widget)
11406 XtDestroyWidget (f->output_data.x->widget);
6186a4a0 11407 free_frame_menubar (f);
3afe33e7
RS
11408#endif /* USE_X_TOOLKIT */
11409
6186a4a0
RS
11410 free_frame_faces (f);
11411 XFlush (FRAME_X_DISPLAY (f));
11412 }
dc6f92b8 11413
df89d8a4 11414 if (f->output_data.x->saved_menu_event)
06a2c219 11415 xfree (f->output_data.x->saved_menu_event);
0c49ce2f 11416
7556890b
RS
11417 xfree (f->output_data.x);
11418 f->output_data.x = 0;
0f941935
KH
11419 if (f == dpyinfo->x_focus_frame)
11420 dpyinfo->x_focus_frame = 0;
11421 if (f == dpyinfo->x_focus_event_frame)
11422 dpyinfo->x_focus_event_frame = 0;
11423 if (f == dpyinfo->x_highlight_frame)
11424 dpyinfo->x_highlight_frame = 0;
c0ff3fab 11425
7f9c7f94
RS
11426 dpyinfo->reference_count--;
11427
11428 if (f == dpyinfo->mouse_face_mouse_frame)
dc05a16b 11429 {
7f9c7f94
RS
11430 dpyinfo->mouse_face_beg_row
11431 = dpyinfo->mouse_face_beg_col = -1;
11432 dpyinfo->mouse_face_end_row
11433 = dpyinfo->mouse_face_end_col = -1;
11434 dpyinfo->mouse_face_window = Qnil;
21323706
RS
11435 dpyinfo->mouse_face_deferred_gc = 0;
11436 dpyinfo->mouse_face_mouse_frame = 0;
dc05a16b 11437 }
0134a210 11438
c0ff3fab 11439 UNBLOCK_INPUT;
dc6f92b8
JB
11440}
11441\f
f451eb13
JB
11442/* Setting window manager hints. */
11443
af31d76f
RS
11444/* Set the normal size hints for the window manager, for frame F.
11445 FLAGS is the flags word to use--or 0 meaning preserve the flags
11446 that the window now has.
11447 If USER_POSITION is nonzero, we set the USPosition
11448 flag (this is useful when FLAGS is 0). */
6dba1858 11449
dfcf069d 11450void
af31d76f 11451x_wm_set_size_hint (f, flags, user_position)
f676886a 11452 struct frame *f;
af31d76f
RS
11453 long flags;
11454 int user_position;
dc6f92b8
JB
11455{
11456 XSizeHints size_hints;
3afe33e7
RS
11457
11458#ifdef USE_X_TOOLKIT
7e4f2521
FP
11459 Arg al[2];
11460 int ac = 0;
11461 Dimension widget_width, widget_height;
7556890b 11462 Window window = XtWindow (f->output_data.x->widget);
3afe33e7 11463#else /* not USE_X_TOOLKIT */
c118dd06 11464 Window window = FRAME_X_WINDOW (f);
3afe33e7 11465#endif /* not USE_X_TOOLKIT */
dc6f92b8 11466
b72a58fd
RS
11467 /* Setting PMaxSize caused various problems. */
11468 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
dc6f92b8 11469
7556890b
RS
11470 size_hints.x = f->output_data.x->left_pos;
11471 size_hints.y = f->output_data.x->top_pos;
7553a6b7 11472
7e4f2521
FP
11473#ifdef USE_X_TOOLKIT
11474 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
11475 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
7556890b 11476 XtGetValues (f->output_data.x->widget, al, ac);
7e4f2521
FP
11477 size_hints.height = widget_height;
11478 size_hints.width = widget_width;
11479#else /* not USE_X_TOOLKIT */
f676886a
JB
11480 size_hints.height = PIXEL_HEIGHT (f);
11481 size_hints.width = PIXEL_WIDTH (f);
7e4f2521 11482#endif /* not USE_X_TOOLKIT */
7553a6b7 11483
7556890b
RS
11484 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
11485 size_hints.height_inc = f->output_data.x->line_height;
334208b7
RS
11486 size_hints.max_width
11487 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
11488 size_hints.max_height
11489 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
0134a210 11490
d067ea8b
KH
11491 /* Calculate the base and minimum sizes.
11492
11493 (When we use the X toolkit, we don't do it here.
11494 Instead we copy the values that the widgets are using, below.) */
11495#ifndef USE_X_TOOLKIT
b1c884c3 11496 {
b0342f17 11497 int base_width, base_height;
0134a210 11498 int min_rows = 0, min_cols = 0;
b0342f17 11499
f451eb13
JB
11500 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
11501 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
b0342f17 11502
0134a210 11503 check_frame_size (f, &min_rows, &min_cols);
b0342f17 11504
0134a210
RS
11505 /* The window manager uses the base width hints to calculate the
11506 current number of rows and columns in the frame while
11507 resizing; min_width and min_height aren't useful for this
11508 purpose, since they might not give the dimensions for a
11509 zero-row, zero-column frame.
58769bee 11510
0134a210
RS
11511 We use the base_width and base_height members if we have
11512 them; otherwise, we set the min_width and min_height members
11513 to the size for a zero x zero frame. */
b0342f17
JB
11514
11515#ifdef HAVE_X11R4
0134a210
RS
11516 size_hints.flags |= PBaseSize;
11517 size_hints.base_width = base_width;
11518 size_hints.base_height = base_height;
11519 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
11520 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
b0342f17 11521#else
0134a210
RS
11522 size_hints.min_width = base_width;
11523 size_hints.min_height = base_height;
b0342f17 11524#endif
b1c884c3 11525 }
dc6f92b8 11526
d067ea8b 11527 /* If we don't need the old flags, we don't need the old hint at all. */
af31d76f 11528 if (flags)
dc6f92b8 11529 {
d067ea8b
KH
11530 size_hints.flags |= flags;
11531 goto no_read;
11532 }
11533#endif /* not USE_X_TOOLKIT */
11534
11535 {
11536 XSizeHints hints; /* Sometimes I hate X Windows... */
11537 long supplied_return;
11538 int value;
af31d76f
RS
11539
11540#ifdef HAVE_X11R4
d067ea8b
KH
11541 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
11542 &supplied_return);
af31d76f 11543#else
d067ea8b 11544 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
af31d76f 11545#endif
58769bee 11546
d067ea8b
KH
11547#ifdef USE_X_TOOLKIT
11548 size_hints.base_height = hints.base_height;
11549 size_hints.base_width = hints.base_width;
11550 size_hints.min_height = hints.min_height;
11551 size_hints.min_width = hints.min_width;
11552#endif
11553
11554 if (flags)
11555 size_hints.flags |= flags;
11556 else
11557 {
11558 if (value == 0)
11559 hints.flags = 0;
11560 if (hints.flags & PSize)
11561 size_hints.flags |= PSize;
11562 if (hints.flags & PPosition)
11563 size_hints.flags |= PPosition;
11564 if (hints.flags & USPosition)
11565 size_hints.flags |= USPosition;
11566 if (hints.flags & USSize)
11567 size_hints.flags |= USSize;
11568 }
11569 }
11570
06a2c219 11571#ifndef USE_X_TOOLKIT
d067ea8b 11572 no_read:
06a2c219 11573#endif
0134a210 11574
af31d76f 11575#ifdef PWinGravity
7556890b 11576 size_hints.win_gravity = f->output_data.x->win_gravity;
af31d76f 11577 size_hints.flags |= PWinGravity;
dc05a16b 11578
af31d76f 11579 if (user_position)
6dba1858 11580 {
af31d76f
RS
11581 size_hints.flags &= ~ PPosition;
11582 size_hints.flags |= USPosition;
6dba1858 11583 }
2554751d 11584#endif /* PWinGravity */
6dba1858 11585
b0342f17 11586#ifdef HAVE_X11R4
334208b7 11587 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 11588#else
334208b7 11589 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 11590#endif
dc6f92b8
JB
11591}
11592
11593/* Used for IconicState or NormalState */
06a2c219 11594
dfcf069d 11595void
f676886a
JB
11596x_wm_set_window_state (f, state)
11597 struct frame *f;
dc6f92b8
JB
11598 int state;
11599{
3afe33e7 11600#ifdef USE_X_TOOLKIT
546e6d5b
RS
11601 Arg al[1];
11602
11603 XtSetArg (al[0], XtNinitialState, state);
7556890b 11604 XtSetValues (f->output_data.x->widget, al, 1);
3afe33e7 11605#else /* not USE_X_TOOLKIT */
c118dd06 11606 Window window = FRAME_X_WINDOW (f);
dc6f92b8 11607
7556890b
RS
11608 f->output_data.x->wm_hints.flags |= StateHint;
11609 f->output_data.x->wm_hints.initial_state = state;
b1c884c3 11610
7556890b 11611 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
546e6d5b 11612#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11613}
11614
dfcf069d 11615void
7f2ae036 11616x_wm_set_icon_pixmap (f, pixmap_id)
f676886a 11617 struct frame *f;
7f2ae036 11618 int pixmap_id;
dc6f92b8 11619{
d2bd6bc4
RS
11620 Pixmap icon_pixmap;
11621
06a2c219 11622#ifndef USE_X_TOOLKIT
c118dd06 11623 Window window = FRAME_X_WINDOW (f);
75231bad 11624#endif
dc6f92b8 11625
7f2ae036 11626 if (pixmap_id > 0)
dbc4e1c1 11627 {
d2bd6bc4 11628 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
7556890b 11629 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
dbc4e1c1
JB
11630 }
11631 else
68568555
RS
11632 {
11633 /* It seems there is no way to turn off use of an icon pixmap.
11634 The following line does it, only if no icon has yet been created,
11635 for some window managers. But with mwm it crashes.
11636 Some people say it should clear the IconPixmapHint bit in this case,
11637 but that doesn't work, and the X consortium said it isn't the
11638 right thing at all. Since there is no way to win,
11639 best to explicitly give up. */
11640#if 0
11641 f->output_data.x->wm_hints.icon_pixmap = None;
11642#else
11643 return;
11644#endif
11645 }
b1c884c3 11646
d2bd6bc4
RS
11647#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
11648
11649 {
11650 Arg al[1];
11651 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
11652 XtSetValues (f->output_data.x->widget, al, 1);
11653 }
11654
11655#else /* not USE_X_TOOLKIT */
11656
7556890b
RS
11657 f->output_data.x->wm_hints.flags |= IconPixmapHint;
11658 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
d2bd6bc4
RS
11659
11660#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11661}
11662
dfcf069d 11663void
f676886a
JB
11664x_wm_set_icon_position (f, icon_x, icon_y)
11665 struct frame *f;
dc6f92b8
JB
11666 int icon_x, icon_y;
11667{
75231bad 11668#ifdef USE_X_TOOLKIT
7556890b 11669 Window window = XtWindow (f->output_data.x->widget);
75231bad 11670#else
c118dd06 11671 Window window = FRAME_X_WINDOW (f);
75231bad 11672#endif
dc6f92b8 11673
7556890b
RS
11674 f->output_data.x->wm_hints.flags |= IconPositionHint;
11675 f->output_data.x->wm_hints.icon_x = icon_x;
11676 f->output_data.x->wm_hints.icon_y = icon_y;
b1c884c3 11677
7556890b 11678 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
dc6f92b8
JB
11679}
11680
11681\f
06a2c219
GM
11682/***********************************************************************
11683 Fonts
11684 ***********************************************************************/
dc43ef94
KH
11685
11686/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
06a2c219 11687
dc43ef94
KH
11688struct font_info *
11689x_get_font_info (f, font_idx)
11690 FRAME_PTR f;
11691 int font_idx;
11692{
11693 return (FRAME_X_FONT_TABLE (f) + font_idx);
11694}
11695
11696
11697/* Return a list of names of available fonts matching PATTERN on frame
11698 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
11699 to be listed. Frame F NULL means we have not yet created any
11700 frame on X, and consult the first display in x_display_list.
11701 MAXNAMES sets a limit on how many fonts to match. */
11702
11703Lisp_Object
11704x_list_fonts (f, pattern, size, maxnames)
11705 FRAME_PTR f;
11706 Lisp_Object pattern;
11707 int size;
11708 int maxnames;
11709{
06a2c219
GM
11710 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
11711 Lisp_Object tem, second_best;
dc43ef94 11712 Display *dpy = f != NULL ? FRAME_X_DISPLAY (f) : x_display_list->display;
09c6077f 11713 int try_XLoadQueryFont = 0;
53ca4657 11714 int count;
dc43ef94 11715
6b0efe73 11716 patterns = Fassoc (pattern, Valternate_fontname_alist);
2da424f1
KH
11717 if (NILP (patterns))
11718 patterns = Fcons (pattern, Qnil);
81ba44e5 11719
09c6077f
KH
11720 if (maxnames == 1 && !size)
11721 /* We can return any single font matching PATTERN. */
11722 try_XLoadQueryFont = 1;
9a32686f 11723
8e713be6 11724 for (; CONSP (patterns); patterns = XCDR (patterns))
dc43ef94 11725 {
dc43ef94
KH
11726 int num_fonts;
11727 char **names;
11728
8e713be6 11729 pattern = XCAR (patterns);
536f4067
RS
11730 /* See if we cached the result for this particular query.
11731 The cache is an alist of the form:
11732 (((PATTERN . MAXNAMES) (FONTNAME . WIDTH) ...) ...)
11733 */
8e713be6 11734 if (f && (tem = XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element),
b5210ea7
KH
11735 key = Fcons (pattern, make_number (maxnames)),
11736 !NILP (list = Fassoc (key, tem))))
11737 {
11738 list = Fcdr_safe (list);
11739 /* We have a cashed list. Don't have to get the list again. */
11740 goto label_cached;
11741 }
11742
11743 /* At first, put PATTERN in the cache. */
09c6077f 11744
dc43ef94 11745 BLOCK_INPUT;
17d85edc
KH
11746 count = x_catch_errors (dpy);
11747
09c6077f
KH
11748 if (try_XLoadQueryFont)
11749 {
11750 XFontStruct *font;
11751 unsigned long value;
11752
11753 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
17d85edc
KH
11754 if (x_had_errors_p (dpy))
11755 {
11756 /* This error is perhaps due to insufficient memory on X
11757 server. Let's just ignore it. */
11758 font = NULL;
11759 x_clear_errors (dpy);
11760 }
11761
09c6077f
KH
11762 if (font
11763 && XGetFontProperty (font, XA_FONT, &value))
11764 {
11765 char *name = (char *) XGetAtomName (dpy, (Atom) value);
11766 int len = strlen (name);
01c752b5 11767 char *tmp;
09c6077f 11768
6f6512e8
KH
11769 /* If DXPC (a Differential X Protocol Compressor)
11770 Ver.3.7 is running, XGetAtomName will return null
11771 string. We must avoid such a name. */
11772 if (len == 0)
11773 try_XLoadQueryFont = 0;
11774 else
11775 {
11776 num_fonts = 1;
11777 names = (char **) alloca (sizeof (char *));
11778 /* Some systems only allow alloca assigned to a
11779 simple var. */
11780 tmp = (char *) alloca (len + 1); names[0] = tmp;
11781 bcopy (name, names[0], len + 1);
11782 XFree (name);
11783 }
09c6077f
KH
11784 }
11785 else
11786 try_XLoadQueryFont = 0;
a083fd23
RS
11787
11788 if (font)
11789 XFreeFont (dpy, font);
09c6077f
KH
11790 }
11791
11792 if (!try_XLoadQueryFont)
17d85edc
KH
11793 {
11794 /* We try at least 10 fonts because XListFonts will return
11795 auto-scaled fonts at the head. */
11796 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
11797 &num_fonts);
11798 if (x_had_errors_p (dpy))
11799 {
11800 /* This error is perhaps due to insufficient memory on X
11801 server. Let's just ignore it. */
11802 names = NULL;
11803 x_clear_errors (dpy);
11804 }
11805 }
11806
11807 x_uncatch_errors (dpy, count);
dc43ef94
KH
11808 UNBLOCK_INPUT;
11809
11810 if (names)
11811 {
11812 int i;
dc43ef94
KH
11813
11814 /* Make a list of all the fonts we got back.
11815 Store that in the font cache for the display. */
11816 for (i = 0; i < num_fonts; i++)
11817 {
06a2c219 11818 int width = 0;
dc43ef94 11819 char *p = names[i];
06a2c219
GM
11820 int average_width = -1, dashes = 0;
11821
dc43ef94 11822 /* Count the number of dashes in NAMES[I]. If there are
b5210ea7
KH
11823 14 dashes, and the field value following 12th dash
11824 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
11825 is usually too ugly to be used for editing. Let's
11826 ignore it. */
dc43ef94
KH
11827 while (*p)
11828 if (*p++ == '-')
11829 {
11830 dashes++;
11831 if (dashes == 7) /* PIXEL_SIZE field */
11832 width = atoi (p);
11833 else if (dashes == 12) /* AVERAGE_WIDTH field */
11834 average_width = atoi (p);
11835 }
11836 if (dashes < 14 || average_width != 0)
11837 {
11838 tem = build_string (names[i]);
11839 if (NILP (Fassoc (tem, list)))
11840 {
11841 if (STRINGP (Vx_pixel_size_width_font_regexp)
f39db7ea
RS
11842 && ((fast_c_string_match_ignore_case
11843 (Vx_pixel_size_width_font_regexp, names[i]))
dc43ef94
KH
11844 >= 0))
11845 /* We can set the value of PIXEL_SIZE to the
b5210ea7 11846 width of this font. */
dc43ef94
KH
11847 list = Fcons (Fcons (tem, make_number (width)), list);
11848 else
11849 /* For the moment, width is not known. */
11850 list = Fcons (Fcons (tem, Qnil), list);
11851 }
11852 }
11853 }
09c6077f
KH
11854 if (!try_XLoadQueryFont)
11855 XFreeFontNames (names);
dc43ef94
KH
11856 }
11857
b5210ea7 11858 /* Now store the result in the cache. */
dc43ef94 11859 if (f != NULL)
8e713be6 11860 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element)
dc43ef94 11861 = Fcons (Fcons (key, list),
8e713be6 11862 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
dc43ef94 11863
b5210ea7
KH
11864 label_cached:
11865 if (NILP (list)) continue; /* Try the remaining alternatives. */
dc43ef94 11866
b5210ea7
KH
11867 newlist = second_best = Qnil;
11868 /* Make a list of the fonts that have the right width. */
8e713be6 11869 for (; CONSP (list); list = XCDR (list))
b5210ea7 11870 {
536f4067
RS
11871 int found_size;
11872
8e713be6 11873 tem = XCAR (list);
dc43ef94 11874
8e713be6 11875 if (!CONSP (tem) || NILP (XCAR (tem)))
b5210ea7
KH
11876 continue;
11877 if (!size)
11878 {
8e713be6 11879 newlist = Fcons (XCAR (tem), newlist);
b5210ea7
KH
11880 continue;
11881 }
dc43ef94 11882
8e713be6 11883 if (!INTEGERP (XCDR (tem)))
dc43ef94 11884 {
b5210ea7
KH
11885 /* Since we have not yet known the size of this font, we
11886 must try slow function call XLoadQueryFont. */
dc43ef94
KH
11887 XFontStruct *thisinfo;
11888
11889 BLOCK_INPUT;
17d85edc 11890 count = x_catch_errors (dpy);
dc43ef94 11891 thisinfo = XLoadQueryFont (dpy,
8e713be6 11892 XSTRING (XCAR (tem))->data);
17d85edc
KH
11893 if (x_had_errors_p (dpy))
11894 {
11895 /* This error is perhaps due to insufficient memory on X
11896 server. Let's just ignore it. */
11897 thisinfo = NULL;
11898 x_clear_errors (dpy);
11899 }
11900 x_uncatch_errors (dpy, count);
dc43ef94
KH
11901 UNBLOCK_INPUT;
11902
11903 if (thisinfo)
11904 {
8e713be6 11905 XCDR (tem)
536f4067
RS
11906 = (thisinfo->min_bounds.width == 0
11907 ? make_number (0)
11908 : make_number (thisinfo->max_bounds.width));
dc43ef94
KH
11909 XFreeFont (dpy, thisinfo);
11910 }
11911 else
b5210ea7 11912 /* For unknown reason, the previous call of XListFont had
06a2c219 11913 returned a font which can't be opened. Record the size
b5210ea7 11914 as 0 not to try to open it again. */
8e713be6 11915 XCDR (tem) = make_number (0);
dc43ef94 11916 }
536f4067 11917
8e713be6 11918 found_size = XINT (XCDR (tem));
536f4067 11919 if (found_size == size)
8e713be6 11920 newlist = Fcons (XCAR (tem), newlist);
536f4067 11921 else if (found_size > 0)
b5210ea7 11922 {
536f4067 11923 if (NILP (second_best))
b5210ea7 11924 second_best = tem;
536f4067
RS
11925 else if (found_size < size)
11926 {
8e713be6
KR
11927 if (XINT (XCDR (second_best)) > size
11928 || XINT (XCDR (second_best)) < found_size)
536f4067
RS
11929 second_best = tem;
11930 }
11931 else
11932 {
8e713be6
KR
11933 if (XINT (XCDR (second_best)) > size
11934 && XINT (XCDR (second_best)) > found_size)
536f4067
RS
11935 second_best = tem;
11936 }
b5210ea7
KH
11937 }
11938 }
11939 if (!NILP (newlist))
11940 break;
11941 else if (!NILP (second_best))
11942 {
8e713be6 11943 newlist = Fcons (XCAR (second_best), Qnil);
b5210ea7 11944 break;
dc43ef94 11945 }
dc43ef94
KH
11946 }
11947
11948 return newlist;
11949}
11950
06a2c219
GM
11951
11952#if GLYPH_DEBUG
11953
11954/* Check that FONT is valid on frame F. It is if it can be found in F's
11955 font table. */
11956
11957static void
11958x_check_font (f, font)
11959 struct frame *f;
11960 XFontStruct *font;
11961{
11962 int i;
11963 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11964
11965 xassert (font != NULL);
11966
11967 for (i = 0; i < dpyinfo->n_fonts; i++)
11968 if (dpyinfo->font_table[i].name
11969 && font == dpyinfo->font_table[i].font)
11970 break;
11971
11972 xassert (i < dpyinfo->n_fonts);
11973}
11974
11975#endif /* GLYPH_DEBUG != 0 */
11976
11977/* Set *W to the minimum width, *H to the minimum font height of FONT.
11978 Note: There are (broken) X fonts out there with invalid XFontStruct
11979 min_bounds contents. For example, handa@etl.go.jp reports that
11980 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
11981 have font->min_bounds.width == 0. */
11982
11983static INLINE void
11984x_font_min_bounds (font, w, h)
11985 XFontStruct *font;
11986 int *w, *h;
11987{
11988 *h = FONT_HEIGHT (font);
11989 *w = font->min_bounds.width;
11990
11991 /* Try to handle the case where FONT->min_bounds has invalid
11992 contents. Since the only font known to have invalid min_bounds
11993 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
11994 if (*w <= 0)
11995 *w = font->max_bounds.width;
11996}
11997
11998
11999/* Compute the smallest character width and smallest font height over
12000 all fonts available on frame F. Set the members smallest_char_width
12001 and smallest_font_height in F's x_display_info structure to
12002 the values computed. Value is non-zero if smallest_font_height or
12003 smallest_char_width become smaller than they were before. */
12004
12005static int
12006x_compute_min_glyph_bounds (f)
12007 struct frame *f;
12008{
12009 int i;
12010 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12011 XFontStruct *font;
12012 int old_width = dpyinfo->smallest_char_width;
12013 int old_height = dpyinfo->smallest_font_height;
12014
12015 dpyinfo->smallest_font_height = 100000;
12016 dpyinfo->smallest_char_width = 100000;
12017
12018 for (i = 0; i < dpyinfo->n_fonts; ++i)
12019 if (dpyinfo->font_table[i].name)
12020 {
12021 struct font_info *fontp = dpyinfo->font_table + i;
12022 int w, h;
12023
12024 font = (XFontStruct *) fontp->font;
12025 xassert (font != (XFontStruct *) ~0);
12026 x_font_min_bounds (font, &w, &h);
12027
12028 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
12029 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
12030 }
12031
12032 xassert (dpyinfo->smallest_char_width > 0
12033 && dpyinfo->smallest_font_height > 0);
12034
12035 return (dpyinfo->n_fonts == 1
12036 || dpyinfo->smallest_char_width < old_width
12037 || dpyinfo->smallest_font_height < old_height);
12038}
12039
12040
dc43ef94
KH
12041/* Load font named FONTNAME of the size SIZE for frame F, and return a
12042 pointer to the structure font_info while allocating it dynamically.
12043 If SIZE is 0, load any size of font.
12044 If loading is failed, return NULL. */
12045
12046struct font_info *
12047x_load_font (f, fontname, size)
12048 struct frame *f;
12049 register char *fontname;
12050 int size;
12051{
12052 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12053 Lisp_Object font_names;
d645aaa4 12054 int count;
dc43ef94
KH
12055
12056 /* Get a list of all the fonts that match this name. Once we
12057 have a list of matching fonts, we compare them against the fonts
12058 we already have by comparing names. */
09c6077f 12059 font_names = x_list_fonts (f, build_string (fontname), size, 1);
dc43ef94
KH
12060
12061 if (!NILP (font_names))
12062 {
12063 Lisp_Object tail;
12064 int i;
12065
12066 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 12067 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
06a2c219
GM
12068 if (dpyinfo->font_table[i].name
12069 && (!strcmp (dpyinfo->font_table[i].name,
8e713be6 12070 XSTRING (XCAR (tail))->data)
06a2c219 12071 || !strcmp (dpyinfo->font_table[i].full_name,
8e713be6 12072 XSTRING (XCAR (tail))->data)))
dc43ef94
KH
12073 return (dpyinfo->font_table + i);
12074 }
12075
12076 /* Load the font and add it to the table. */
12077 {
12078 char *full_name;
12079 XFontStruct *font;
12080 struct font_info *fontp;
12081 unsigned long value;
06a2c219 12082 int i;
dc43ef94 12083
2da424f1
KH
12084 /* If we have found fonts by x_list_font, load one of them. If
12085 not, we still try to load a font by the name given as FONTNAME
12086 because XListFonts (called in x_list_font) of some X server has
12087 a bug of not finding a font even if the font surely exists and
12088 is loadable by XLoadQueryFont. */
e1d6d5b9 12089 if (size > 0 && !NILP (font_names))
8e713be6 12090 fontname = (char *) XSTRING (XCAR (font_names))->data;
dc43ef94
KH
12091
12092 BLOCK_INPUT;
d645aaa4 12093 count = x_catch_errors (FRAME_X_DISPLAY (f));
dc43ef94 12094 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
d645aaa4
KH
12095 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12096 {
12097 /* This error is perhaps due to insufficient memory on X
12098 server. Let's just ignore it. */
12099 font = NULL;
12100 x_clear_errors (FRAME_X_DISPLAY (f));
12101 }
12102 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
dc43ef94 12103 UNBLOCK_INPUT;
b5210ea7 12104 if (!font)
dc43ef94
KH
12105 return NULL;
12106
06a2c219
GM
12107 /* Find a free slot in the font table. */
12108 for (i = 0; i < dpyinfo->n_fonts; ++i)
12109 if (dpyinfo->font_table[i].name == NULL)
12110 break;
12111
12112 /* If no free slot found, maybe enlarge the font table. */
12113 if (i == dpyinfo->n_fonts
12114 && dpyinfo->n_fonts == dpyinfo->font_table_size)
dc43ef94 12115 {
06a2c219
GM
12116 int sz;
12117 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
12118 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
dc43ef94 12119 dpyinfo->font_table
06a2c219 12120 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
dc43ef94
KH
12121 }
12122
06a2c219
GM
12123 fontp = dpyinfo->font_table + i;
12124 if (i == dpyinfo->n_fonts)
12125 ++dpyinfo->n_fonts;
dc43ef94
KH
12126
12127 /* Now fill in the slots of *FONTP. */
12128 BLOCK_INPUT;
12129 fontp->font = font;
06a2c219 12130 fontp->font_idx = i;
dc43ef94
KH
12131 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
12132 bcopy (fontname, fontp->name, strlen (fontname) + 1);
12133
12134 /* Try to get the full name of FONT. Put it in FULL_NAME. */
12135 full_name = 0;
12136 if (XGetFontProperty (font, XA_FONT, &value))
12137 {
12138 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
12139 char *p = name;
12140 int dashes = 0;
12141
12142 /* Count the number of dashes in the "full name".
12143 If it is too few, this isn't really the font's full name,
12144 so don't use it.
12145 In X11R4, the fonts did not come with their canonical names
12146 stored in them. */
12147 while (*p)
12148 {
12149 if (*p == '-')
12150 dashes++;
12151 p++;
12152 }
12153
12154 if (dashes >= 13)
12155 {
12156 full_name = (char *) xmalloc (p - name + 1);
12157 bcopy (name, full_name, p - name + 1);
12158 }
12159
12160 XFree (name);
12161 }
12162
12163 if (full_name != 0)
12164 fontp->full_name = full_name;
12165 else
12166 fontp->full_name = fontp->name;
12167
12168 fontp->size = font->max_bounds.width;
d5749adb
KH
12169 fontp->height = FONT_HEIGHT (font);
12170 {
12171 /* For some font, ascent and descent in max_bounds field is
12172 larger than the above value. */
12173 int max_height = font->max_bounds.ascent + font->max_bounds.descent;
12174 if (max_height > fontp->height)
74848a96 12175 fontp->height = max_height;
d5749adb 12176 }
dc43ef94 12177
2da424f1
KH
12178 if (NILP (font_names))
12179 {
12180 /* We come here because of a bug of XListFonts mentioned at
12181 the head of this block. Let's store this information in
12182 the cache for x_list_fonts. */
12183 Lisp_Object lispy_name = build_string (fontname);
12184 Lisp_Object lispy_full_name = build_string (fontp->full_name);
12185
8e713be6 12186 XCDR (dpyinfo->name_list_element)
2da424f1
KH
12187 = Fcons (Fcons (Fcons (lispy_name, make_number (256)),
12188 Fcons (Fcons (lispy_full_name,
12189 make_number (fontp->size)),
12190 Qnil)),
8e713be6 12191 XCDR (dpyinfo->name_list_element));
2da424f1 12192 if (full_name)
8e713be6 12193 XCDR (dpyinfo->name_list_element)
2da424f1
KH
12194 = Fcons (Fcons (Fcons (lispy_full_name, make_number (256)),
12195 Fcons (Fcons (lispy_full_name,
12196 make_number (fontp->size)),
12197 Qnil)),
8e713be6 12198 XCDR (dpyinfo->name_list_element));
2da424f1
KH
12199 }
12200
dc43ef94
KH
12201 /* The slot `encoding' specifies how to map a character
12202 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
ce667c3e 12203 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF, 0:0x2020..0x7F7F,
8ff102bd
RS
12204 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF,
12205 0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF, or
12206 2:0xA020..0xFF7F). For the moment, we don't know which charset
06a2c219 12207 uses this font. So, we set information in fontp->encoding[1]
8ff102bd
RS
12208 which is never used by any charset. If mapping can't be
12209 decided, set FONT_ENCODING_NOT_DECIDED. */
dc43ef94
KH
12210 fontp->encoding[1]
12211 = (font->max_byte1 == 0
12212 /* 1-byte font */
12213 ? (font->min_char_or_byte2 < 0x80
12214 ? (font->max_char_or_byte2 < 0x80
12215 ? 0 /* 0x20..0x7F */
8ff102bd 12216 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
dc43ef94
KH
12217 : 1) /* 0xA0..0xFF */
12218 /* 2-byte font */
12219 : (font->min_byte1 < 0x80
12220 ? (font->max_byte1 < 0x80
12221 ? (font->min_char_or_byte2 < 0x80
12222 ? (font->max_char_or_byte2 < 0x80
12223 ? 0 /* 0x2020..0x7F7F */
8ff102bd 12224 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
dc43ef94 12225 : 3) /* 0x20A0..0x7FFF */
8ff102bd 12226 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
dc43ef94
KH
12227 : (font->min_char_or_byte2 < 0x80
12228 ? (font->max_char_or_byte2 < 0x80
12229 ? 2 /* 0xA020..0xFF7F */
8ff102bd 12230 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
dc43ef94
KH
12231 : 1))); /* 0xA0A0..0xFFFF */
12232
12233 fontp->baseline_offset
12234 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
12235 ? (long) value : 0);
12236 fontp->relative_compose
12237 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
12238 ? (long) value : 0);
f78798df
KH
12239 fontp->default_ascent
12240 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
12241 ? (long) value : 0);
dc43ef94 12242
06a2c219
GM
12243 /* Set global flag fonts_changed_p to non-zero if the font loaded
12244 has a character with a smaller width than any other character
12245 before, or if the font loaded has a smalle>r height than any
12246 other font loaded before. If this happens, it will make a
12247 glyph matrix reallocation necessary. */
12248 fonts_changed_p = x_compute_min_glyph_bounds (f);
dc43ef94 12249 UNBLOCK_INPUT;
dc43ef94
KH
12250 return fontp;
12251 }
12252}
12253
06a2c219
GM
12254
12255/* Return a pointer to struct font_info of a font named FONTNAME for
12256 frame F. If no such font is loaded, return NULL. */
12257
dc43ef94
KH
12258struct font_info *
12259x_query_font (f, fontname)
12260 struct frame *f;
12261 register char *fontname;
12262{
12263 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12264 int i;
12265
12266 for (i = 0; i < dpyinfo->n_fonts; i++)
06a2c219
GM
12267 if (dpyinfo->font_table[i].name
12268 && (!strcmp (dpyinfo->font_table[i].name, fontname)
12269 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
dc43ef94
KH
12270 return (dpyinfo->font_table + i);
12271 return NULL;
12272}
12273
06a2c219
GM
12274
12275/* Find a CCL program for a font specified by FONTP, and set the member
a6582676
KH
12276 `encoder' of the structure. */
12277
12278void
12279x_find_ccl_program (fontp)
12280 struct font_info *fontp;
12281{
a42f54e6 12282 Lisp_Object list, elt;
a6582676 12283
8e713be6 12284 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
a6582676 12285 {
8e713be6 12286 elt = XCAR (list);
a6582676 12287 if (CONSP (elt)
8e713be6
KR
12288 && STRINGP (XCAR (elt))
12289 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
a6582676 12290 >= 0))
a42f54e6
KH
12291 break;
12292 }
12293 if (! NILP (list))
12294 {
d27f8ca7
KH
12295 struct ccl_program *ccl
12296 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
a42f54e6 12297
8e713be6 12298 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
a42f54e6
KH
12299 xfree (ccl);
12300 else
12301 fontp->font_encoder = ccl;
a6582676
KH
12302 }
12303}
12304
06a2c219 12305
dc43ef94 12306\f
06a2c219
GM
12307/***********************************************************************
12308 Initialization
12309 ***********************************************************************/
f451eb13 12310
3afe33e7
RS
12311#ifdef USE_X_TOOLKIT
12312static XrmOptionDescRec emacs_options[] = {
12313 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
12314 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
12315
12316 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
12317 XrmoptionSepArg, NULL},
12318 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
12319
12320 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12321 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12322 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12323 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12324 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12325 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
12326 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
12327};
12328#endif /* USE_X_TOOLKIT */
12329
7a13e894
RS
12330static int x_initialized;
12331
29b38361
KH
12332#ifdef MULTI_KBOARD
12333/* Test whether two display-name strings agree up to the dot that separates
12334 the screen number from the server number. */
12335static int
12336same_x_server (name1, name2)
12337 char *name1, *name2;
12338{
12339 int seen_colon = 0;
cf591cc1
RS
12340 unsigned char *system_name = XSTRING (Vsystem_name)->data;
12341 int system_name_length = strlen (system_name);
12342 int length_until_period = 0;
12343
12344 while (system_name[length_until_period] != 0
12345 && system_name[length_until_period] != '.')
12346 length_until_period++;
12347
12348 /* Treat `unix' like an empty host name. */
12349 if (! strncmp (name1, "unix:", 5))
12350 name1 += 4;
12351 if (! strncmp (name2, "unix:", 5))
12352 name2 += 4;
12353 /* Treat this host's name like an empty host name. */
12354 if (! strncmp (name1, system_name, system_name_length)
12355 && name1[system_name_length] == ':')
12356 name1 += system_name_length;
12357 if (! strncmp (name2, system_name, system_name_length)
12358 && name2[system_name_length] == ':')
12359 name2 += system_name_length;
12360 /* Treat this host's domainless name like an empty host name. */
12361 if (! strncmp (name1, system_name, length_until_period)
12362 && name1[length_until_period] == ':')
12363 name1 += length_until_period;
12364 if (! strncmp (name2, system_name, length_until_period)
12365 && name2[length_until_period] == ':')
12366 name2 += length_until_period;
12367
29b38361
KH
12368 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
12369 {
12370 if (*name1 == ':')
12371 seen_colon++;
12372 if (seen_colon && *name1 == '.')
12373 return 1;
12374 }
12375 return (seen_colon
12376 && (*name1 == '.' || *name1 == '\0')
12377 && (*name2 == '.' || *name2 == '\0'));
12378}
12379#endif
12380
71f8198a
PE
12381#if defined (HAVE_X_I18N) || (defined (USE_X_TOOLKIT) && defined (HAVE_X11XTR6))
12382/* Recover from setlocale (LC_ALL, ""). */
12383static void
12384fixup_locale ()
12385{
12386 /* Currently we require strerror to use the "C" locale,
12387 since we don't yet support decoding its string result. */
12388#ifdef LC_MESSAGES
12389 setlocale (LC_MESSAGES, "C");
12390#endif
12391
12392 /* The Emacs Lisp reader needs LC_NUMERIC to be "C",
12393 so that numbers are read and printed properly for Emacs Lisp. */
12394 setlocale (LC_NUMERIC, "C");
12395
12396 /* Currently we require strftime to use the "C" locale,
12397 since we don't yet support encoding its format argument,
12398 or decoding its string result. */
12399 setlocale (LC_TIME, "C");
12400}
12401#endif
12402
334208b7 12403struct x_display_info *
1f8255f2 12404x_term_init (display_name, xrm_option, resource_name)
334208b7 12405 Lisp_Object display_name;
1f8255f2
RS
12406 char *xrm_option;
12407 char *resource_name;
dc6f92b8 12408{
334208b7 12409 int connection;
7a13e894 12410 Display *dpy;
334208b7
RS
12411 struct x_display_info *dpyinfo;
12412 XrmDatabase xrdb;
12413
60439948
KH
12414 BLOCK_INPUT;
12415
7a13e894
RS
12416 if (!x_initialized)
12417 {
12418 x_initialize ();
12419 x_initialized = 1;
12420 }
dc6f92b8 12421
6c183ba5 12422#ifdef HAVE_X_I18N
6186a4a0 12423 setlocale (LC_ALL, "");
71f8198a 12424 fixup_locale ();
6c183ba5
RS
12425#endif
12426
3afe33e7 12427#ifdef USE_X_TOOLKIT
2d7fc7e8
RS
12428 /* weiner@footloose.sps.mot.com reports that this causes
12429 errors with X11R5:
12430 X protocol error: BadAtom (invalid Atom parameter)
12431 on protocol request 18skiloaf.
12432 So let's not use it until R6. */
12433#ifdef HAVE_X11XTR6
bdcd49ba
RS
12434 XtSetLanguageProc (NULL, NULL, NULL);
12435#endif
12436
7f9c7f94
RS
12437 {
12438 int argc = 0;
12439 char *argv[3];
12440
12441 argv[0] = "";
12442 argc = 1;
12443 if (xrm_option)
12444 {
12445 argv[argc++] = "-xrm";
12446 argv[argc++] = xrm_option;
12447 }
12448 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
12449 resource_name, EMACS_CLASS,
12450 emacs_options, XtNumber (emacs_options),
12451 &argc, argv);
39d8bb4d
KH
12452
12453#ifdef HAVE_X11XTR6
10537cb1 12454 /* I think this is to compensate for XtSetLanguageProc. */
71f8198a 12455 fixup_locale ();
39d8bb4d 12456#endif
7f9c7f94 12457 }
3afe33e7
RS
12458
12459#else /* not USE_X_TOOLKIT */
bdcd49ba
RS
12460#ifdef HAVE_X11R5
12461 XSetLocaleModifiers ("");
12462#endif
7a13e894 12463 dpy = XOpenDisplay (XSTRING (display_name)->data);
3afe33e7 12464#endif /* not USE_X_TOOLKIT */
334208b7 12465
7a13e894
RS
12466 /* Detect failure. */
12467 if (dpy == 0)
60439948
KH
12468 {
12469 UNBLOCK_INPUT;
12470 return 0;
12471 }
7a13e894
RS
12472
12473 /* We have definitely succeeded. Record the new connection. */
12474
12475 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
12476
29b38361
KH
12477#ifdef MULTI_KBOARD
12478 {
12479 struct x_display_info *share;
12480 Lisp_Object tail;
12481
12482 for (share = x_display_list, tail = x_display_name_list; share;
8e713be6
KR
12483 share = share->next, tail = XCDR (tail))
12484 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
29b38361
KH
12485 XSTRING (display_name)->data))
12486 break;
12487 if (share)
12488 dpyinfo->kboard = share->kboard;
12489 else
12490 {
12491 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
12492 init_kboard (dpyinfo->kboard);
59e755be
KH
12493 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
12494 {
12495 char *vendor = ServerVendor (dpy);
12496 dpyinfo->kboard->Vsystem_key_alist
12497 = call1 (Qvendor_specific_keysyms,
12498 build_string (vendor ? vendor : ""));
12499 }
12500
29b38361
KH
12501 dpyinfo->kboard->next_kboard = all_kboards;
12502 all_kboards = dpyinfo->kboard;
0ad5446c
KH
12503 /* Don't let the initial kboard remain current longer than necessary.
12504 That would cause problems if a file loaded on startup tries to
06a2c219 12505 prompt in the mini-buffer. */
0ad5446c
KH
12506 if (current_kboard == initial_kboard)
12507 current_kboard = dpyinfo->kboard;
29b38361
KH
12508 }
12509 dpyinfo->kboard->reference_count++;
12510 }
b9737ad3
KH
12511#endif
12512
7a13e894
RS
12513 /* Put this display on the chain. */
12514 dpyinfo->next = x_display_list;
12515 x_display_list = dpyinfo;
12516
12517 /* Put it on x_display_name_list as well, to keep them parallel. */
12518 x_display_name_list = Fcons (Fcons (display_name, Qnil),
12519 x_display_name_list);
8e713be6 12520 dpyinfo->name_list_element = XCAR (x_display_name_list);
7a13e894
RS
12521
12522 dpyinfo->display = dpy;
dc6f92b8 12523
dc6f92b8 12524#if 0
7a13e894 12525 XSetAfterFunction (x_current_display, x_trace_wire);
c118dd06 12526#endif /* ! 0 */
7a13e894
RS
12527
12528 dpyinfo->x_id_name
fc932ac6
RS
12529 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
12530 + STRING_BYTES (XSTRING (Vsystem_name))
7a13e894
RS
12531 + 2);
12532 sprintf (dpyinfo->x_id_name, "%s@%s",
12533 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
28430d3c
JB
12534
12535 /* Figure out which modifier bits mean what. */
334208b7 12536 x_find_modifier_meanings (dpyinfo);
f451eb13 12537
ab648270 12538 /* Get the scroll bar cursor. */
7a13e894 12539 dpyinfo->vertical_scroll_bar_cursor
334208b7 12540 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
f451eb13 12541
334208b7
RS
12542 xrdb = x_load_resources (dpyinfo->display, xrm_option,
12543 resource_name, EMACS_CLASS);
12544#ifdef HAVE_XRMSETDATABASE
12545 XrmSetDatabase (dpyinfo->display, xrdb);
12546#else
12547 dpyinfo->display->db = xrdb;
12548#endif
547d9db8 12549 /* Put the rdb where we can find it in a way that works on
7a13e894
RS
12550 all versions. */
12551 dpyinfo->xrdb = xrdb;
334208b7
RS
12552
12553 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
12554 DefaultScreen (dpyinfo->display));
12555 dpyinfo->visual = select_visual (dpyinfo->display, dpyinfo->screen,
12556 &dpyinfo->n_planes);
12557 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
12558 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
12559 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
12560 dpyinfo->grabbed = 0;
12561 dpyinfo->reference_count = 0;
12562 dpyinfo->icon_bitmap_id = -1;
06a2c219 12563 dpyinfo->font_table = NULL;
7a13e894
RS
12564 dpyinfo->n_fonts = 0;
12565 dpyinfo->font_table_size = 0;
12566 dpyinfo->bitmaps = 0;
12567 dpyinfo->bitmaps_size = 0;
12568 dpyinfo->bitmaps_last = 0;
12569 dpyinfo->scratch_cursor_gc = 0;
12570 dpyinfo->mouse_face_mouse_frame = 0;
12571 dpyinfo->mouse_face_deferred_gc = 0;
12572 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
12573 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
06a2c219 12574 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
7a13e894
RS
12575 dpyinfo->mouse_face_window = Qnil;
12576 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
12577 dpyinfo->mouse_face_defer = 0;
0f941935
KH
12578 dpyinfo->x_focus_frame = 0;
12579 dpyinfo->x_focus_event_frame = 0;
12580 dpyinfo->x_highlight_frame = 0;
06a2c219 12581 dpyinfo->image_cache = make_image_cache ();
334208b7 12582
06a2c219
GM
12583 {
12584 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
12585 double pixels = DisplayHeight (dpyinfo->display, screen_number);
12586 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
12587 dpyinfo->resy = pixels * 25.4 / mm;
12588 pixels = DisplayWidth (dpyinfo->display, screen_number);
12589 mm = DisplayWidthMM (dpyinfo->display, screen_number);
12590 dpyinfo->resx = pixels * 25.4 / mm;
12591 }
12592
334208b7
RS
12593 dpyinfo->Xatom_wm_protocols
12594 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
12595 dpyinfo->Xatom_wm_take_focus
12596 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
12597 dpyinfo->Xatom_wm_save_yourself
12598 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
12599 dpyinfo->Xatom_wm_delete_window
12600 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
12601 dpyinfo->Xatom_wm_change_state
12602 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
12603 dpyinfo->Xatom_wm_configure_denied
12604 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
12605 dpyinfo->Xatom_wm_window_moved
12606 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
12607 dpyinfo->Xatom_editres
12608 = XInternAtom (dpyinfo->display, "Editres", False);
12609 dpyinfo->Xatom_CLIPBOARD
12610 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
12611 dpyinfo->Xatom_TIMESTAMP
12612 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
12613 dpyinfo->Xatom_TEXT
12614 = XInternAtom (dpyinfo->display, "TEXT", False);
dc43ef94
KH
12615 dpyinfo->Xatom_COMPOUND_TEXT
12616 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
334208b7
RS
12617 dpyinfo->Xatom_DELETE
12618 = XInternAtom (dpyinfo->display, "DELETE", False);
12619 dpyinfo->Xatom_MULTIPLE
12620 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
12621 dpyinfo->Xatom_INCR
12622 = XInternAtom (dpyinfo->display, "INCR", False);
12623 dpyinfo->Xatom_EMACS_TMP
12624 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
12625 dpyinfo->Xatom_TARGETS
12626 = XInternAtom (dpyinfo->display, "TARGETS", False);
12627 dpyinfo->Xatom_NULL
12628 = XInternAtom (dpyinfo->display, "NULL", False);
12629 dpyinfo->Xatom_ATOM_PAIR
12630 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
dc43ef94
KH
12631 /* For properties of font. */
12632 dpyinfo->Xatom_PIXEL_SIZE
12633 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
12634 dpyinfo->Xatom_MULE_BASELINE_OFFSET
12635 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
12636 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
12637 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
f78798df
KH
12638 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
12639 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
334208b7 12640
06a2c219
GM
12641 /* Ghostscript support. */
12642 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
12643 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
12644
12645 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
12646 False);
12647
547d9db8
KH
12648 dpyinfo->cut_buffers_initialized = 0;
12649
334208b7
RS
12650 connection = ConnectionNumber (dpyinfo->display);
12651 dpyinfo->connection = connection;
12652
dc43ef94 12653 {
5d7cc324
RS
12654 char null_bits[1];
12655
12656 null_bits[0] = 0x00;
dc43ef94
KH
12657
12658 dpyinfo->null_pixel
12659 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
12660 null_bits, 1, 1, (long) 0, (long) 0,
12661 1);
12662 }
12663
06a2c219
GM
12664 {
12665 extern int gray_bitmap_width, gray_bitmap_height;
12666 extern unsigned char *gray_bitmap_bits;
12667 dpyinfo->gray
12668 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
12669 gray_bitmap_bits,
12670 gray_bitmap_width, gray_bitmap_height,
12671 (unsigned long) 1, (unsigned long) 0, 1);
12672 }
12673
87485d6f
MW
12674#ifdef subprocesses
12675 /* This is only needed for distinguishing keyboard and process input. */
334208b7 12676 if (connection != 0)
7a13e894 12677 add_keyboard_wait_descriptor (connection);
87485d6f 12678#endif
6d4238f3 12679
041b69ac 12680#ifndef F_SETOWN_BUG
dc6f92b8 12681#ifdef F_SETOWN
dc6f92b8 12682#ifdef F_SETOWN_SOCK_NEG
61c3ce62 12683 /* stdin is a socket here */
334208b7 12684 fcntl (connection, F_SETOWN, -getpid ());
c118dd06 12685#else /* ! defined (F_SETOWN_SOCK_NEG) */
334208b7 12686 fcntl (connection, F_SETOWN, getpid ());
c118dd06
JB
12687#endif /* ! defined (F_SETOWN_SOCK_NEG) */
12688#endif /* ! defined (F_SETOWN) */
041b69ac 12689#endif /* F_SETOWN_BUG */
dc6f92b8
JB
12690
12691#ifdef SIGIO
eee20f6a
KH
12692 if (interrupt_input)
12693 init_sigio (connection);
c118dd06 12694#endif /* ! defined (SIGIO) */
dc6f92b8 12695
51b592fb 12696#ifdef USE_LUCID
f8c39f51 12697#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
51b592fb
RS
12698 /* Make sure that we have a valid font for dialog boxes
12699 so that Xt does not crash. */
12700 {
12701 Display *dpy = dpyinfo->display;
12702 XrmValue d, fr, to;
12703 Font font;
e99db5a1 12704 int count;
51b592fb
RS
12705
12706 d.addr = (XPointer)&dpy;
12707 d.size = sizeof (Display *);
12708 fr.addr = XtDefaultFont;
12709 fr.size = sizeof (XtDefaultFont);
12710 to.size = sizeof (Font *);
12711 to.addr = (XPointer)&font;
e99db5a1 12712 count = x_catch_errors (dpy);
51b592fb
RS
12713 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
12714 abort ();
12715 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
12716 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
e99db5a1 12717 x_uncatch_errors (dpy, count);
51b592fb
RS
12718 }
12719#endif
f8c39f51 12720#endif
51b592fb 12721
60439948
KH
12722 UNBLOCK_INPUT;
12723
7a13e894
RS
12724 return dpyinfo;
12725}
12726\f
12727/* Get rid of display DPYINFO, assuming all frames are already gone,
12728 and without sending any more commands to the X server. */
dc6f92b8 12729
7a13e894
RS
12730void
12731x_delete_display (dpyinfo)
12732 struct x_display_info *dpyinfo;
12733{
12734 delete_keyboard_wait_descriptor (dpyinfo->connection);
12735
12736 /* Discard this display from x_display_name_list and x_display_list.
12737 We can't use Fdelq because that can quit. */
12738 if (! NILP (x_display_name_list)
8e713be6
KR
12739 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
12740 x_display_name_list = XCDR (x_display_name_list);
7a13e894
RS
12741 else
12742 {
12743 Lisp_Object tail;
12744
12745 tail = x_display_name_list;
8e713be6 12746 while (CONSP (tail) && CONSP (XCDR (tail)))
7a13e894 12747 {
8e713be6 12748 if (EQ (XCAR (XCDR (tail)),
7a13e894
RS
12749 dpyinfo->name_list_element))
12750 {
8e713be6 12751 XCDR (tail) = XCDR (XCDR (tail));
7a13e894
RS
12752 break;
12753 }
8e713be6 12754 tail = XCDR (tail);
7a13e894
RS
12755 }
12756 }
12757
12758 if (x_display_list == dpyinfo)
12759 x_display_list = dpyinfo->next;
7f9c7f94
RS
12760 else
12761 {
12762 struct x_display_info *tail;
7a13e894 12763
7f9c7f94
RS
12764 for (tail = x_display_list; tail; tail = tail->next)
12765 if (tail->next == dpyinfo)
12766 tail->next = tail->next->next;
12767 }
7a13e894 12768
0d777288
RS
12769#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
12770#ifndef AIX /* On AIX, XCloseDisplay calls this. */
7f9c7f94
RS
12771 XrmDestroyDatabase (dpyinfo->xrdb);
12772#endif
0d777288 12773#endif
29b38361
KH
12774#ifdef MULTI_KBOARD
12775 if (--dpyinfo->kboard->reference_count == 0)
39f79001 12776 delete_kboard (dpyinfo->kboard);
b9737ad3
KH
12777#endif
12778 xfree (dpyinfo->font_table);
12779 xfree (dpyinfo->x_id_name);
12780 xfree (dpyinfo);
7a13e894
RS
12781}
12782\f
12783/* Set up use of X before we make the first connection. */
12784
06a2c219
GM
12785static struct redisplay_interface x_redisplay_interface =
12786{
12787 x_produce_glyphs,
12788 x_write_glyphs,
12789 x_insert_glyphs,
12790 x_clear_end_of_line,
12791 x_scroll_run,
12792 x_after_update_window_line,
12793 x_update_window_begin,
12794 x_update_window_end,
12795 XTcursor_to,
12796 x_flush,
66ac4b0e
GM
12797 x_get_glyph_overhangs,
12798 x_fix_overlapping_area
06a2c219
GM
12799};
12800
dfcf069d 12801void
7a13e894
RS
12802x_initialize ()
12803{
06a2c219
GM
12804 rif = &x_redisplay_interface;
12805
12806 clear_frame_hook = x_clear_frame;
12807 ins_del_lines_hook = x_ins_del_lines;
12808 change_line_highlight_hook = x_change_line_highlight;
12809 delete_glyphs_hook = x_delete_glyphs;
dc6f92b8
JB
12810 ring_bell_hook = XTring_bell;
12811 reset_terminal_modes_hook = XTreset_terminal_modes;
12812 set_terminal_modes_hook = XTset_terminal_modes;
06a2c219
GM
12813 update_begin_hook = x_update_begin;
12814 update_end_hook = x_update_end;
dc6f92b8
JB
12815 set_terminal_window_hook = XTset_terminal_window;
12816 read_socket_hook = XTread_socket;
b8009dd1 12817 frame_up_to_date_hook = XTframe_up_to_date;
dc6f92b8 12818 reassert_line_highlight_hook = XTreassert_line_highlight;
90e65f07 12819 mouse_position_hook = XTmouse_position;
f451eb13 12820 frame_rehighlight_hook = XTframe_rehighlight;
dbc4e1c1 12821 frame_raise_lower_hook = XTframe_raise_lower;
ab648270
JB
12822 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
12823 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
12824 redeem_scroll_bar_hook = XTredeem_scroll_bar;
12825 judge_scroll_bars_hook = XTjudge_scroll_bars;
06a2c219 12826 estimate_mode_line_height_hook = x_estimate_mode_line_height;
58769bee 12827
f676886a 12828 scroll_region_ok = 1; /* we'll scroll partial frames */
dc6f92b8
JB
12829 char_ins_del_ok = 0; /* just as fast to write the line */
12830 line_ins_del_ok = 1; /* we'll just blt 'em */
12831 fast_clear_end_of_line = 1; /* X does this well */
58769bee 12832 memory_below_frame = 0; /* we don't remember what scrolls
dc6f92b8
JB
12833 off the bottom */
12834 baud_rate = 19200;
12835
7a13e894 12836 x_noop_count = 0;
9ea173e8 12837 last_tool_bar_item = -1;
06a2c219
GM
12838 any_help_event_p = 0;
12839
b30b24cb
RS
12840 /* Try to use interrupt input; if we can't, then start polling. */
12841 Fset_input_mode (Qt, Qnil, Qt, Qnil);
12842
7f9c7f94
RS
12843#ifdef USE_X_TOOLKIT
12844 XtToolkitInitialize ();
12845 Xt_app_con = XtCreateApplicationContext ();
665881ad 12846 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
7f9c7f94
RS
12847#endif
12848
58769bee 12849 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 12850 original error handler. */
e99db5a1 12851 XSetErrorHandler (x_error_handler);
334208b7 12852 XSetIOErrorHandler (x_io_error_quitter);
dc6f92b8 12853
06a2c219 12854 /* Disable Window Change signals; they are handled by X events. */
dc6f92b8
JB
12855#ifdef SIGWINCH
12856 signal (SIGWINCH, SIG_DFL);
c118dd06 12857#endif /* ! defined (SIGWINCH) */
dc6f92b8 12858
92e2441b 12859 signal (SIGPIPE, x_connection_signal);
dc6f92b8 12860}
55123275 12861
06a2c219 12862
55123275
JB
12863void
12864syms_of_xterm ()
12865{
e99db5a1
RS
12866 staticpro (&x_error_message_string);
12867 x_error_message_string = Qnil;
12868
7a13e894
RS
12869 staticpro (&x_display_name_list);
12870 x_display_name_list = Qnil;
334208b7 12871
ab648270 12872 staticpro (&last_mouse_scroll_bar);
e53cb100 12873 last_mouse_scroll_bar = Qnil;
59e755be
KH
12874
12875 staticpro (&Qvendor_specific_keysyms);
12876 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
2237cac9
RS
12877
12878 staticpro (&last_mouse_press_frame);
12879 last_mouse_press_frame = Qnil;
06a2c219
GM
12880
12881 staticpro (&help_echo);
12882 help_echo = Qnil;
12883 staticpro (&previous_help_echo);
12884 previous_help_echo = Qnil;
12885
12886 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
12887 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
12888For example, if a block cursor is over a tab, it will be drawn as\n\
12889wide as that tab on the display.");
12890 x_stretch_cursor_p = 0;
12891
12892 DEFVAR_BOOL ("x-toolkit-scroll-bars-p", &x_toolkit_scroll_bars_p,
12893 "If not nil, Emacs uses toolkit scroll bars.");
12894#if USE_TOOLKIT_SCROLL_BARS
12895 x_toolkit_scroll_bars_p = 1;
12896#else
12897 x_toolkit_scroll_bars_p = 0;
12898#endif
12899
12900 defsubr (&Sxt_process_timeouts);
12901 staticpro (&last_mouse_motion_frame);
12902 last_mouse_motion_frame = Qnil;
55123275 12903}
6cf0ae86
RS
12904
12905#endif /* not HAVE_X_WINDOWS */
06a2c219 12906