(Qshow_trailing_whitespace): Removed.
[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
200/* Marker for continued lines. */
201
202#define continued_width 8
203#define continued_height 8
204static unsigned char continued_bits[] = {
205 0x30, 0x30, 0x30, 0x30, 0xb4, 0xfc, 0x78, 0x30};
206
207#define continuation_width 8
208#define continuation_height 8
209static unsigned char continuation_bits[] = {
210 0x0c, 0x1e, 0x3f, 0x2d, 0x0c, 0x0c, 0x0c, 0x0c};
211
212/* Right truncation arrow bitmap `->'. */
213
214#define right_width 8
215#define right_height 8
216static unsigned char right_bits[] = {
217 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
218
219/* Overlay arrow bitmap; a filled `<>'. */
220
221#if 1
222#define ov_width 8
223#define ov_height 8
224static unsigned char ov_bits[] = {
225 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
226
227#elif 0
228#define ov_width 8
229#define ov_height 8
230static unsigned char ov_bits[] = {
231 0x18, 0x04, 0x08, 0x1c, 0x3e, 0x3a, 0x32, 0x1c};
232#else
233#define ov_width 8
234#define ov_height 8
235static unsigned char ov_bits[] = {
236 0x18, 0x3c, 0x7e, 0xff, 0xff, 0x7e, 0x3c, 0x18};
e4b68333 237#endif
06a2c219
GM
238
239extern Lisp_Object Qhelp_echo;
240
69388238 241\f
06a2c219
GM
242/* Non-zero means Emacs uses toolkit scroll bars. */
243
244int x_toolkit_scroll_bars_p;
245
246/* If a string, XTread_socket generates an event to display that string.
247 (The display is done in read_char.) */
248
249static Lisp_Object help_echo;
250
251/* Temporary variable for XTread_socket. */
252
253static Lisp_Object previous_help_echo;
254
255/* Non-zero means that a HELP_EVENT has been generated since Emacs
256 start. */
257
258static int any_help_event_p;
259
260/* Non-zero means draw block and hollow cursor as wide as the glyph
261 under it. For example, if a block cursor is over a tab, it will be
262 drawn as wide as that tab on the display. */
263
264int x_stretch_cursor_p;
265
266/* This is a chain of structures for all the X displays currently in
267 use. */
268
334208b7 269struct x_display_info *x_display_list;
dc6f92b8 270
06a2c219
GM
271/* This is a list of cons cells, each of the form (NAME
272 . FONT-LIST-CACHE), one for each element of x_display_list and in
273 the same order. NAME is the name of the frame. FONT-LIST-CACHE
274 records previous values returned by x-list-fonts. */
275
7a13e894 276Lisp_Object x_display_name_list;
f451eb13 277
987d2ad1 278/* Frame being updated by update_frame. This is declared in term.c.
06a2c219
GM
279 This is set by update_begin and looked at by all the XT functions.
280 It is zero while not inside an update. In that case, the XT
281 functions assume that `selected_frame' is the frame to apply to. */
282
d0386f2a 283extern struct frame *updating_frame;
dc6f92b8 284
dfcf069d 285extern int waiting_for_input;
0e81d8cd 286
06a2c219
GM
287/* This is a frame waiting to be auto-raised, within XTread_socket. */
288
0134a210
RS
289struct frame *pending_autoraise_frame;
290
7f9c7f94
RS
291#ifdef USE_X_TOOLKIT
292/* The application context for Xt use. */
293XtAppContext Xt_app_con;
06a2c219
GM
294static String Xt_default_resources[] = {0};
295#endif /* USE_X_TOOLKIT */
665881ad 296
06a2c219
GM
297/* Nominal cursor position -- where to draw output.
298 HPOS and VPOS are window relative glyph matrix coordinates.
299 X and Y are window relative pixel coordinates. */
dc6f92b8 300
06a2c219 301struct cursor_pos output_cursor;
dc6f92b8 302
dc6f92b8 303
69388238
RS
304/* Mouse movement.
305
06a2c219 306 Formerly, we used PointerMotionHintMask (in standard_event_mask)
f5bb65ec
RS
307 so that we would have to call XQueryPointer after each MotionNotify
308 event to ask for another such event. However, this made mouse tracking
309 slow, and there was a bug that made it eventually stop.
310
311 Simply asking for MotionNotify all the time seems to work better.
312
69388238
RS
313 In order to avoid asking for motion events and then throwing most
314 of them away or busy-polling the server for mouse positions, we ask
315 the server for pointer motion hints. This means that we get only
316 one event per group of mouse movements. "Groups" are delimited by
317 other kinds of events (focus changes and button clicks, for
318 example), or by XQueryPointer calls; when one of these happens, we
319 get another MotionNotify event the next time the mouse moves. This
320 is at least as efficient as getting motion events when mouse
321 tracking is on, and I suspect only negligibly worse when tracking
f5bb65ec 322 is off. */
69388238
RS
323
324/* Where the mouse was last time we reported a mouse event. */
69388238 325
06a2c219
GM
326FRAME_PTR last_mouse_frame;
327static XRectangle last_mouse_glyph;
2237cac9
RS
328static Lisp_Object last_mouse_press_frame;
329
69388238
RS
330/* The scroll bar in which the last X motion event occurred.
331
06a2c219
GM
332 If the last X motion event occurred in a scroll bar, we set this so
333 XTmouse_position can know whether to report a scroll bar motion or
69388238
RS
334 an ordinary motion.
335
06a2c219
GM
336 If the last X motion event didn't occur in a scroll bar, we set
337 this to Qnil, to tell XTmouse_position to return an ordinary motion
338 event. */
339
69388238
RS
340static Lisp_Object last_mouse_scroll_bar;
341
69388238
RS
342/* This is a hack. We would really prefer that XTmouse_position would
343 return the time associated with the position it returns, but there
06a2c219 344 doesn't seem to be any way to wrest the time-stamp from the server
69388238
RS
345 along with the position query. So, we just keep track of the time
346 of the last movement we received, and return that in hopes that
347 it's somewhat accurate. */
06a2c219 348
69388238
RS
349static Time last_mouse_movement_time;
350
06a2c219
GM
351/* Incremented by XTread_socket whenever it really tries to read
352 events. */
353
c0a04927
RS
354#ifdef __STDC__
355static int volatile input_signal_count;
356#else
357static int input_signal_count;
358#endif
359
7a13e894 360/* Used locally within XTread_socket. */
06a2c219 361
7a13e894 362static int x_noop_count;
dc6f92b8 363
7a13e894 364/* Initial values of argv and argc. */
06a2c219 365
7a13e894
RS
366extern char **initial_argv;
367extern int initial_argc;
dc6f92b8 368
7a13e894 369extern Lisp_Object Vcommand_line_args, Vsystem_name;
dc6f92b8 370
06a2c219 371/* Tells if a window manager is present or not. */
7a13e894
RS
372
373extern Lisp_Object Vx_no_window_manager;
dc6f92b8 374
c2df547c 375extern Lisp_Object Qface, Qmouse_face;
b8009dd1 376
dc6f92b8
JB
377extern int errno;
378
dfeccd2d 379/* A mask of extra modifier bits to put into every keyboard char. */
06a2c219 380
64bb1782
RS
381extern int extra_keyboard_modifiers;
382
59e755be
KH
383static Lisp_Object Qvendor_specific_keysyms;
384
334208b7 385extern XrmDatabase x_load_resources ();
c32cdd9a
KH
386extern Lisp_Object x_icon_type ();
387
7a13e894 388
06a2c219
GM
389/* Enumeration for overriding/changing the face to use for drawing
390 glyphs in x_draw_glyphs. */
391
392enum draw_glyphs_face
393{
394 DRAW_NORMAL_TEXT,
395 DRAW_INVERSE_VIDEO,
396 DRAW_CURSOR,
397 DRAW_MOUSE_FACE,
398 DRAW_IMAGE_RAISED,
399 DRAW_IMAGE_SUNKEN
400};
401
402static void x_update_window_end P_ ((struct window *, int));
403static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
404void x_delete_display P_ ((struct x_display_info *));
405static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
406 unsigned));
407static int fast_find_position P_ ((struct window *, int, int *, int *,
408 int *, int *));
409static void set_output_cursor P_ ((struct cursor_pos *));
410static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
411 int *, int *, int *));
412static void note_mode_line_highlight P_ ((struct window *, int, int));
413static void x_check_font P_ ((struct frame *, XFontStruct *));
414static void note_mouse_highlight P_ ((struct frame *, int, int));
415static void note_toolbar_highlight P_ ((struct frame *f, int, int));
416static void x_handle_toolbar_click P_ ((struct frame *, XButtonEvent *));
417static void show_mouse_face P_ ((struct x_display_info *,
418 enum draw_glyphs_face));
419static int x_io_error_quitter P_ ((Display *));
420int x_catch_errors P_ ((Display *));
421void x_uncatch_errors P_ ((Display *, int));
422void x_lower_frame P_ ((struct frame *));
423void x_scroll_bar_clear P_ ((struct frame *));
424int x_had_errors_p P_ ((Display *));
425void x_wm_set_size_hint P_ ((struct frame *, long, int));
426void x_raise_frame P_ ((struct frame *));
427void x_set_window_size P_ ((struct frame *, int, int, int));
428void x_wm_set_window_state P_ ((struct frame *, int));
429void x_wm_set_icon_pixmap P_ ((struct frame *, int));
430void x_initialize P_ ((void));
431static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
432static int x_compute_min_glyph_bounds P_ ((struct frame *));
433static void x_draw_phys_cursor_glyph P_ ((struct window *,
434 struct glyph_row *,
435 enum draw_glyphs_face));
436static void x_update_end P_ ((struct frame *));
437static void XTframe_up_to_date P_ ((struct frame *));
438static void XTreassert_line_highlight P_ ((int, int));
439static void x_change_line_highlight P_ ((int, int, int, int));
440static void XTset_terminal_modes P_ ((void));
441static void XTreset_terminal_modes P_ ((void));
442static void XTcursor_to P_ ((int, int, int, int));
443static void x_write_glyphs P_ ((struct glyph *, int));
444static void x_clear_end_of_line P_ ((int));
445static void x_clear_frame P_ ((void));
446static void x_clear_cursor P_ ((struct window *));
447static void frame_highlight P_ ((struct frame *));
448static void frame_unhighlight P_ ((struct frame *));
449static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
450static void XTframe_rehighlight P_ ((struct frame *));
451static void x_frame_rehighlight P_ ((struct x_display_info *));
452static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
453static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *));
454static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
455 XRectangle *));
456static void expose_frame P_ ((struct frame *, int, int, int, int));
457static void expose_window_tree P_ ((struct window *, XRectangle *));
458static void expose_window P_ ((struct window *, XRectangle *));
459static void expose_area P_ ((struct window *, struct glyph_row *,
460 XRectangle *, enum glyph_row_area));
461static void expose_line P_ ((struct window *, struct glyph_row *,
462 XRectangle *));
463static void x_update_cursor_in_window_tree P_ ((struct window *, int));
464static void x_update_window_cursor P_ ((struct window *, int));
465static void x_erase_phys_cursor P_ ((struct window *));
466void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
467static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
468 enum bitmap_type));
469
470static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
471 GC, int));
472static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
473static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
474static void note_overwritten_text_cursor P_ ((struct window *, int, int));
475static void x_flush P_ ((struct frame *f));
476
477
478/* Flush display of frame F, or of all frames if F is null. */
479
480static void
481x_flush (f)
482 struct frame *f;
483{
484 BLOCK_INPUT;
485 if (f == NULL)
486 {
487 Lisp_Object rest, frame;
488 FOR_EACH_FRAME (rest, frame)
489 x_flush (XFRAME (frame));
490 }
491 else if (FRAME_X_P (f))
492 XFlush (FRAME_X_DISPLAY (f));
493 UNBLOCK_INPUT;
494}
495
dc6f92b8 496
06a2c219
GM
497/* Remove calls to XFlush by defining XFlush to an empty replacement.
498 Calls to XFlush should be unnecessary because the X output buffer
499 is flushed automatically as needed by calls to XPending,
500 XNextEvent, or XWindowEvent according to the XFlush man page.
501 XTread_socket calls XPending. Removing XFlush improves
502 performance. */
503
504#define XFlush(DISPLAY) (void) 0
b8009dd1 505
334208b7 506\f
06a2c219
GM
507/***********************************************************************
508 Debugging
509 ***********************************************************************/
510
9382638d 511#if 0
06a2c219
GM
512
513/* This is a function useful for recording debugging information about
514 the sequence of occurrences in this file. */
9382638d
KH
515
516struct record
517{
518 char *locus;
519 int type;
520};
521
522struct record event_record[100];
523
524int event_record_index;
525
526record_event (locus, type)
527 char *locus;
528 int type;
529{
530 if (event_record_index == sizeof (event_record) / sizeof (struct record))
531 event_record_index = 0;
532
533 event_record[event_record_index].locus = locus;
534 event_record[event_record_index].type = type;
535 event_record_index++;
536}
537
538#endif /* 0 */
06a2c219
GM
539
540
9382638d 541\f
334208b7
RS
542/* Return the struct x_display_info corresponding to DPY. */
543
544struct x_display_info *
545x_display_info_for_display (dpy)
546 Display *dpy;
547{
548 struct x_display_info *dpyinfo;
549
550 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
551 if (dpyinfo->display == dpy)
552 return dpyinfo;
16bd92ea 553
334208b7
RS
554 return 0;
555}
f451eb13 556
06a2c219
GM
557
558\f
559/***********************************************************************
560 Starting and ending an update
561 ***********************************************************************/
562
563/* Start an update of frame F. This function is installed as a hook
564 for update_begin, i.e. it is called when update_begin is called.
565 This function is called prior to calls to x_update_window_begin for
566 each window being updated. Currently, there is nothing to do here
567 because all interesting stuff is done on a window basis. */
dc6f92b8 568
dfcf069d 569static void
06a2c219 570x_update_begin (f)
f676886a 571 struct frame *f;
58769bee 572{
06a2c219
GM
573 /* Nothing to do. */
574}
dc6f92b8 575
dc6f92b8 576
06a2c219
GM
577/* Start update of window W. Set the global variable updated_window
578 to the window being updated and set output_cursor to the cursor
579 position of W. */
dc6f92b8 580
06a2c219
GM
581static void
582x_update_window_begin (w)
583 struct window *w;
584{
585 struct frame *f = XFRAME (WINDOW_FRAME (w));
586 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
587
588 updated_window = w;
589 set_output_cursor (&w->cursor);
b8009dd1 590
06a2c219 591 BLOCK_INPUT;
d1bc4182 592
06a2c219 593 if (f == display_info->mouse_face_mouse_frame)
b8009dd1 594 {
514e4681 595 /* Don't do highlighting for mouse motion during the update. */
06a2c219 596 display_info->mouse_face_defer = 1;
37c2c98b 597
06a2c219
GM
598 /* If F needs to be redrawn, simply forget about any prior mouse
599 highlighting. */
9f67f20b 600 if (FRAME_GARBAGED_P (f))
06a2c219
GM
601 display_info->mouse_face_window = Qnil;
602
603 /* Can we tell that this update does not affect the window
604 where the mouse highlight is? If so, no need to turn off.
605 Likewise, don't do anything if the frame is garbaged;
606 in that case, the frame's current matrix that we would use
607 is all wrong, and we will redisplay that line anyway. */
608 if (!NILP (display_info->mouse_face_window)
609 && w == XWINDOW (display_info->mouse_face_window))
514e4681 610 {
06a2c219 611 int i;
514e4681 612
06a2c219
GM
613 for (i = 0; i < w->desired_matrix->nrows; ++i)
614 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
514e4681
RS
615 break;
616
06a2c219
GM
617 if (i < w->desired_matrix->nrows)
618 clear_mouse_face (display_info);
514e4681 619 }
b8009dd1 620 }
6ccf47d1 621
dc6f92b8
JB
622 UNBLOCK_INPUT;
623}
624
06a2c219
GM
625
626/* Draw a vertical window border to the right of window W if W doesn't
627 have vertical scroll bars. */
628
dfcf069d 629static void
06a2c219
GM
630x_draw_vertical_border (w)
631 struct window *w;
58769bee 632{
06a2c219
GM
633 struct frame *f = XFRAME (WINDOW_FRAME (w));
634
635 /* Redraw borders between horizontally adjacent windows. Don't
636 do it for frames with vertical scroll bars because either the
637 right scroll bar of a window, or the left scroll bar of its
638 neighbor will suffice as a border. */
639 if (!WINDOW_RIGHTMOST_P (w)
640 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
641 {
642 int x0, x1, y0, y1;
dc6f92b8 643
06a2c219
GM
644 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
645 x1 += FRAME_X_FLAGS_AREA_WIDTH (f);
646 y1 -= 1;
647
648 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
649 f->output_data.x->normal_gc, x1, y0, x1, y1);
650 }
651}
652
653
654/* End update of window W (which is equal to updated_window). Draw
655 vertical borders between horizontally adjacent windows, and display
656 W's cursor if CURSOR_ON_P is non-zero. W may be a menu bar
657 pseudo-window in case we don't have X toolkit support. Such
658 windows don't have a cursor, so don't display it here. */
659
660static void
661x_update_window_end (w, cursor_on_p)
662 struct window *w;
663 int cursor_on_p;
664{
665 if (!w->pseudo_window_p)
666 {
667 BLOCK_INPUT;
668 if (cursor_on_p)
669 x_display_and_set_cursor (w, 1, output_cursor.hpos,
670 output_cursor.vpos,
671 output_cursor.x, output_cursor.y);
672 x_draw_vertical_border (w);
673 UNBLOCK_INPUT;
674 }
675
676 updated_window = NULL;
677}
dc6f92b8 678
dc6f92b8 679
06a2c219
GM
680/* End update of frame F. This function is installed as a hook in
681 update_end. */
682
683static void
684x_update_end (f)
685 struct frame *f;
686{
687 /* Mouse highlight may be displayed again. */
aa8bff2e 688 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
b8009dd1 689
06a2c219 690 BLOCK_INPUT;
334208b7 691 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
692 UNBLOCK_INPUT;
693}
b8009dd1 694
06a2c219
GM
695
696/* This function is called from various places in xdisp.c whenever a
697 complete update has been performed. The global variable
698 updated_window is not available here. */
b8009dd1 699
dfcf069d 700static void
b8009dd1 701XTframe_up_to_date (f)
06a2c219 702 struct frame *f;
b8009dd1 703{
06a2c219 704 if (FRAME_X_P (f))
514e4681 705 {
06a2c219
GM
706 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
707 if (dpyinfo->mouse_face_deferred_gc
708 || f == dpyinfo->mouse_face_mouse_frame)
709 {
710 BLOCK_INPUT;
711 if (dpyinfo->mouse_face_mouse_frame)
712 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
713 dpyinfo->mouse_face_mouse_x,
714 dpyinfo->mouse_face_mouse_y);
715 dpyinfo->mouse_face_deferred_gc = 0;
716 UNBLOCK_INPUT;
717 }
514e4681 718 }
b8009dd1 719}
06a2c219
GM
720
721
722/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
723 arrow bitmaps, or clear the areas where they would be displayed
724 before DESIRED_ROW is made current. The window being updated is
725 found in updated_window. This function It is called from
726 update_window_line only if it is known that there are differences
727 between bitmaps to be drawn between current row and DESIRED_ROW. */
728
729static void
730x_after_update_window_line (desired_row)
731 struct glyph_row *desired_row;
732{
733 struct window *w = updated_window;
734
735 xassert (w);
736
737 if (!desired_row->mode_line_p && !w->pseudo_window_p)
738 {
739 BLOCK_INPUT;
740 x_draw_row_bitmaps (w, desired_row);
741
742 /* When a window has disappeared, make sure that no rest of
743 full-width rows stays visible in the internal border. */
744 if (windows_or_buffers_changed)
745 {
746 struct frame *f = XFRAME (w->frame);
747 int width = FRAME_INTERNAL_BORDER_WIDTH (f);
748 int height = desired_row->visible_height;
749 int x = window_box_right (w, -1) + FRAME_X_FLAGS_AREA_WIDTH (f);
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
793 - (FRAME_X_FLAGS_AREA_WIDTH (f) - wd) / 2);
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
802 - (FRAME_X_FLAGS_AREA_WIDTH (f) - wd) / 2);
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);
810 x += (FRAME_X_FLAGS_AREA_WIDTH (f) - wd) / 2;
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);
818 x += (FRAME_X_FLAGS_AREA_WIDTH (f) - wd) / 2;
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
827 - (FRAME_X_FLAGS_AREA_WIDTH (f) - wd) / 2);
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
836 - (FRAME_X_FLAGS_AREA_WIDTH (f) - wd) / 2);
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;
871 int top_line_height = -1;
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
898 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_FLAGS_AREA_WIDTH (f)
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
907 if (top_line_height < 0)
908 top_line_height = WINDOW_DISPLAY_TOP_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
922 - FRAME_X_FLAGS_AREA_WIDTH (f)
923 + border),
924 WINDOW_TO_FRAME_PIXEL_Y (w, max (top_line_height,
925 row->y)),
926 FRAME_X_FLAGS_AREA_WIDTH (f) - border,
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
947 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_FLAGS_AREA_WIDTH (f)
948 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
949 {
950 int right = window_box_right (w, -1);
951
952 if (top_line_height < 0)
953 top_line_height = WINDOW_DISPLAY_TOP_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,
966 WINDOW_TO_FRAME_PIXEL_Y (w, max (top_line_height,
967 row->y)),
968 FRAME_X_FLAGS_AREA_WIDTH (f),
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);
334208b7 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 *));
1103static void x_produce_special_glyphs P_ ((struct it *, enum display_element_type));
1104static void x_produce_image_glyph P_ ((struct it *it));
1105
1106
1107/* Return a pointer to per-char metric information in FONT of a
1108 character pointed by B which is a pointer to an XChar2b. */
dc43ef94
KH
1109
1110#define PER_CHAR_METRIC(font, b) \
1111 ((font)->per_char \
1112 ? ((font)->per_char + (b)->byte2 - (font)->min_char_or_byte2 \
1113 + (((font)->min_byte1 || (font)->max_byte1) \
1114 ? (((b)->byte1 - (font)->min_byte1) \
1115 * ((font)->max_char_or_byte2 - (font)->min_char_or_byte2 + 1)) \
1116 : 0)) \
1117 : &((font)->max_bounds))
1118
dc6f92b8 1119
06a2c219
GM
1120/* Get metrics of character CHAR2B in FONT. Value is always non-null.
1121 If CHAR2B is not contained in FONT, the font's default character
1122 metric is returned. */
dc43ef94 1123
06a2c219
GM
1124static INLINE XCharStruct *
1125x_per_char_metric (font, char2b)
1126 XFontStruct *font;
1127 XChar2b *char2b;
1128{
1129 /* The result metric information. */
1130 XCharStruct *pcm = NULL;
dc6f92b8 1131
06a2c219 1132 xassert (font && char2b);
dc6f92b8 1133
06a2c219 1134 if (font->per_char != NULL)
dc6f92b8 1135 {
06a2c219 1136 if (font->min_byte1 == 0 && font->max_byte1 == 0)
dc43ef94 1137 {
06a2c219
GM
1138 /* min_char_or_byte2 specifies the linear character index
1139 corresponding to the first element of the per_char array,
1140 max_char_or_byte2 is the index of the last character. A
1141 character with non-zero CHAR2B->byte1 is not in the font.
1142 A character with byte2 less than min_char_or_byte2 or
1143 greater max_char_or_byte2 is not in the font. */
1144 if (char2b->byte1 == 0
1145 && char2b->byte2 >= font->min_char_or_byte2
1146 && char2b->byte2 <= font->max_char_or_byte2)
1147 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
dc43ef94 1148 }
06a2c219 1149 else
dc6f92b8 1150 {
06a2c219
GM
1151 /* If either min_byte1 or max_byte1 are nonzero, both
1152 min_char_or_byte2 and max_char_or_byte2 are less than
1153 256, and the 2-byte character index values corresponding
1154 to the per_char array element N (counting from 0) are:
1155
1156 byte1 = N/D + min_byte1
1157 byte2 = N\D + min_char_or_byte2
1158
1159 where:
1160
1161 D = max_char_or_byte2 - min_char_or_byte2 + 1
1162 / = integer division
1163 \ = integer modulus */
1164 if (char2b->byte1 >= font->min_byte1
1165 && char2b->byte1 <= font->max_byte1
1166 && char2b->byte2 >= font->min_char_or_byte2
1167 && char2b->byte2 <= font->max_char_or_byte2)
1168 {
1169 pcm = (font->per_char
1170 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1171 * (char2b->byte1 - font->min_byte1))
1172 + (char2b->byte2 - font->min_char_or_byte2));
1173 }
dc6f92b8 1174 }
06a2c219
GM
1175 }
1176 else
1177 {
1178 /* If the per_char pointer is null, all glyphs between the first
1179 and last character indexes inclusive have the same
1180 information, as given by both min_bounds and max_bounds. */
1181 if (char2b->byte2 >= font->min_char_or_byte2
1182 && char2b->byte2 <= font->max_char_or_byte2)
1183 pcm = &font->max_bounds;
1184 }
dc6f92b8 1185
4a4dc352 1186
06a2c219
GM
1187 if (pcm == NULL || pcm->width == 0)
1188 {
1189 /* Character not contained in the font. FONT->default_char
1190 gives the character that will be printed. FONT->default_char
1191 is a 16-bit character code with byte1 in the most significant
1192 byte and byte2 in the least significant byte. */
1193 XChar2b default_char;
1194 default_char.byte1 = (font->default_char >> BITS_PER_CHAR) & 0xff;
1195 default_char.byte2 = font->default_char & 0xff;
1196
1197 /* Avoid an endless recursion if FONT->default_char itself
1198 hasn't per char metrics. handa@etl.go.jp reports that some
1199 fonts have this problem. */
1200 if (default_char.byte1 != char2b->byte1
1201 || default_char.byte2 != char2b->byte2)
1202 pcm = x_per_char_metric (font, &default_char);
1203 else
1204 pcm = &font->max_bounds;
1205 }
1206
1207 return pcm;
1208}
b73b6aaf 1209
57b03282 1210
06a2c219
GM
1211/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1212 the two-byte form of C. Encoding is returned in *CHAR2B. */
dc43ef94 1213
06a2c219
GM
1214static INLINE void
1215x_encode_char (c, char2b, font_info)
1216 int c;
1217 XChar2b *char2b;
1218 struct font_info *font_info;
1219{
1220 int charset = CHAR_CHARSET (c);
1221 XFontStruct *font = font_info->font;
1222
1223 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1224 This may be either a program in a special encoder language or a
1225 fixed encoding. */
1226 if (font_info->font_encoder)
1227 {
1228 /* It's a program. */
1229 struct ccl_program *ccl = font_info->font_encoder;
1230
1231 if (CHARSET_DIMENSION (charset) == 1)
1232 {
1233 ccl->reg[0] = charset;
1234 ccl->reg[1] = char2b->byte2;
1235 }
1236 else
1237 {
1238 ccl->reg[0] = charset;
1239 ccl->reg[1] = char2b->byte1;
1240 ccl->reg[2] = char2b->byte2;
1241 }
1242
1243 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1244
1245 /* We assume that MSBs are appropriately set/reset by CCL
1246 program. */
1247 if (font->max_byte1 == 0) /* 1-byte font */
1248 char2b->byte2 = ccl->reg[1];
1249 else
1250 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1251 }
1252 else if (font_info->encoding[charset])
1253 {
1254 /* Fixed encoding scheme. See fontset.h for the meaning of the
1255 encoding numbers. */
1256 int enc = font_info->encoding[charset];
1257
1258 if ((enc == 1 || enc == 2)
1259 && CHARSET_DIMENSION (charset) == 2)
1260 char2b->byte1 |= 0x80;
1261
1262 if (enc == 1 || enc == 3)
1263 char2b->byte2 |= 0x80;
1264 }
1265}
1266
1267
1268/* Get face and two-byte form of character C in face FACE_ID on frame
1269 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1270 means we want to display multibyte text. Value is a pointer to a
1271 realized face that is ready for display. */
1272
1273static INLINE struct face *
1274x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1275 struct frame *f;
1276 int c, face_id;
1277 XChar2b *char2b;
1278 int multibyte_p;
1279{
1280 struct face *face = FACE_FROM_ID (f, face_id);
1281
1282 if (!multibyte_p)
1283 {
1284 /* Unibyte case. We don't have to encode, but we have to make
1285 sure to use a face suitable for unibyte. */
1286 char2b->byte1 = 0;
1287 char2b->byte2 = c;
1288
1289 if (!FACE_SUITABLE_FOR_CHARSET_P (face, -1))
1290 {
1291 face_id = FACE_FOR_CHARSET (f, face_id, -1);
1292 face = FACE_FROM_ID (f, face_id);
1293 }
1294 }
1295 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1296 {
1297 /* Case of ASCII in a face known to fit ASCII. */
1298 char2b->byte1 = 0;
1299 char2b->byte2 = c;
1300 }
1301 else
1302 {
1303 int c1, c2, charset;
1304
1305 /* Split characters into bytes. If c2 is -1 afterwards, C is
1306 really a one-byte character so that byte1 is zero. */
1307 SPLIT_CHAR (c, charset, c1, c2);
1308 if (c2 > 0)
1309 char2b->byte1 = c1, char2b->byte2 = c2;
1310 else
1311 char2b->byte1 = 0, char2b->byte2 = c1;
1312
1313 /* Get the face for displaying C. If `face' is not suitable for
1314 charset, get the one that fits. (This can happen for the
1315 translations of composite characters where the glyph
1316 specifies a face for ASCII, but translations have a different
1317 charset.) */
1318 if (!FACE_SUITABLE_FOR_CHARSET_P (face, charset))
1319 {
1320 face_id = FACE_FOR_CHARSET (f, face_id, charset);
1321 face = FACE_FROM_ID (f, face_id);
1322 }
1323
1324 /* Maybe encode the character in *CHAR2B. */
1325 if (charset != CHARSET_ASCII)
1326 {
1327 struct font_info *font_info
1328 = FONT_INFO_FROM_ID (f, face->font_info_id);
1329 if (font_info)
1330 {
1331 x_encode_char (c, char2b, font_info);
1332 if (charset == charset_latin_iso8859_1)
1333 {
1334 xassert (((XFontStruct *) font_info->font)->max_char_or_byte2
1335 >= 0x80);
1336 char2b->byte2 |= 0x80;
1337 }
1338 }
1339 }
1340 }
1341
1342 /* Make sure X resources of the face are allocated. */
1343 xassert (face != NULL);
1344 PREPARE_FACE_FOR_DISPLAY (f, face);
1345
1346 return face;
1347}
1348
1349
1350/* Get face and two-byte form of character glyph GLYPH on frame F.
1351 The encoding of GLYPH->u.ch.code is returned in *CHAR2B. Value is
1352 a pointer to a realized face that is ready for display. */
1353
1354static INLINE struct face *
1355x_get_glyph_face_and_encoding (f, glyph, char2b)
1356 struct frame *f;
1357 struct glyph *glyph;
1358 XChar2b *char2b;
1359{
1360 struct face *face;
1361
1362 xassert (glyph->type == CHAR_GLYPH);
1363 face = FACE_FROM_ID (f, glyph->u.ch.face_id);
1364
1365 if (!glyph->multibyte_p)
1366 {
1367 /* Unibyte case. We don't have to encode, but we have to make
1368 sure to use a face suitable for unibyte. */
1369 char2b->byte1 = 0;
1370 char2b->byte2 = glyph->u.ch.code;
1371 }
1372 else if (glyph->u.ch.code < 128
1373 && glyph->u.ch.face_id < BASIC_FACE_ID_SENTINEL)
1374 {
1375 /* Case of ASCII in a face known to fit ASCII. */
1376 char2b->byte1 = 0;
1377 char2b->byte2 = glyph->u.ch.code;
1378 }
1379 else
1380 {
1381 int c1, c2, charset;
1382
1383 /* Split characters into bytes. If c2 is -1 afterwards, C is
1384 really a one-byte character so that byte1 is zero. */
1385 SPLIT_CHAR (glyph->u.ch.code, charset, c1, c2);
1386 if (c2 > 0)
1387 char2b->byte1 = c1, char2b->byte2 = c2;
1388 else
1389 char2b->byte1 = 0, char2b->byte2 = c1;
1390
1391 /* Maybe encode the character in *CHAR2B. */
1392 if (charset != CHARSET_ASCII)
1393 {
1394 struct font_info *font_info
1395 = FONT_INFO_FROM_ID (f, face->font_info_id);
1396 if (font_info)
1397 {
1398 x_encode_char (glyph->u.ch.code, char2b, font_info);
1399 if (charset == charset_latin_iso8859_1)
1400 char2b->byte2 |= 0x80;
1401 }
1402 }
1403 }
1404
1405 /* Make sure X resources of the face are allocated. */
1406 xassert (face != NULL);
1407 PREPARE_FACE_FOR_DISPLAY (f, face);
1408 return face;
1409}
1410
1411
1412/* Store one glyph for IT->char_to_display in IT->glyph_row.
1413 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1414
1415static INLINE void
1416x_append_glyph (it)
1417 struct it *it;
1418{
1419 struct glyph *glyph;
1420 enum glyph_row_area area = it->area;
1421
1422 xassert (it->glyph_row);
1423 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1424
1425 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1426 if (glyph < it->glyph_row->glyphs[area + 1])
1427 {
1428 /* Play it safe. If sub-structures of the glyph are not all the
1429 same size, it otherwise be that some bits stay set. This
1430 would prevent a comparison with GLYPH_EQUAL_P. */
1431 glyph->u.val = 0;
1432
1433 glyph->type = CHAR_GLYPH;
1434 glyph->pixel_width = it->pixel_width;
1435 glyph->u.ch.code = it->char_to_display;
1436 glyph->u.ch.face_id = it->face_id;
1437 glyph->charpos = CHARPOS (it->position);
1438 glyph->object = it->object;
1439 glyph->left_box_line_p = it->start_of_box_run_p;
1440 glyph->right_box_line_p = it->end_of_box_run_p;
1441 glyph->voffset = it->voffset;
1442 glyph->multibyte_p = it->multibyte_p;
66ac4b0e
GM
1443 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1444 || it->phys_descent > it->descent);
06a2c219
GM
1445 ++it->glyph_row->used[area];
1446 }
1447}
1448
1449
1450/* Change IT->ascent and IT->height according to the setting of
1451 IT->voffset. */
1452
1453static INLINE void
1454take_vertical_position_into_account (it)
1455 struct it *it;
1456{
1457 if (it->voffset)
1458 {
1459 if (it->voffset < 0)
1460 /* Increase the ascent so that we can display the text higher
1461 in the line. */
1462 it->ascent += abs (it->voffset);
1463 else
1464 /* Increase the descent so that we can display the text lower
1465 in the line. */
1466 it->descent += it->voffset;
1467 }
1468}
1469
1470
1471/* Produce glyphs/get display metrics for the image IT is loaded with.
1472 See the description of struct display_iterator in dispextern.h for
1473 an overview of struct display_iterator. */
1474
1475static void
1476x_produce_image_glyph (it)
1477 struct it *it;
1478{
1479 struct image *img;
1480 struct face *face;
1481
1482 xassert (it->what == IT_IMAGE);
1483
1484 face = FACE_FROM_ID (it->f, it->face_id);
1485 img = IMAGE_FROM_ID (it->f, it->image_id);
1486 xassert (img);
1487
1488 /* Make sure X resources of the face and image are loaded. */
1489 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1490 prepare_image_for_display (it->f, img);
1491
66ac4b0e
GM
1492 it->ascent = it->phys_ascent = IMAGE_ASCENT (img);
1493 it->descent = it->phys_descent = img->height + 2 * img->margin - it->ascent;
06a2c219
GM
1494 it->pixel_width = img->width + 2 * img->margin;
1495
1496 it->nglyphs = 1;
1497
1498 if (face->box != FACE_NO_BOX)
1499 {
1500 it->ascent += face->box_line_width;
1501 it->descent += face->box_line_width;
1502
1503 if (it->start_of_box_run_p)
1504 it->pixel_width += face->box_line_width;
1505 if (it->end_of_box_run_p)
1506 it->pixel_width += face->box_line_width;
1507 }
1508
1509 take_vertical_position_into_account (it);
1510
1511 if (it->glyph_row)
1512 {
1513 struct glyph *glyph;
1514 enum glyph_row_area area = it->area;
1515
1516 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1517 if (glyph < it->glyph_row->glyphs[area + 1])
1518 {
1519 glyph->type = IMAGE_GLYPH;
1520 glyph->u.img.id = img->id;
1521 glyph->u.img.face_id = it->face_id;
1522 glyph->pixel_width = it->pixel_width;
1523 glyph->charpos = CHARPOS (it->position);
1524 glyph->object = it->object;
1525 glyph->left_box_line_p = it->start_of_box_run_p;
1526 glyph->right_box_line_p = it->end_of_box_run_p;
1527 glyph->voffset = it->voffset;
1528 glyph->multibyte_p = it->multibyte_p;
1529 ++it->glyph_row->used[area];
1530 }
1531 }
1532}
1533
1534
1535/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1536 of the glyph, WIDTH and HEIGHT are the width and height of the
1537 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1538 ascent of the glyph (0 <= ASCENT <= 1). */
1539
1540static void
1541x_append_stretch_glyph (it, object, width, height, ascent)
1542 struct it *it;
1543 Lisp_Object object;
1544 int width, height;
1545 double ascent;
1546{
1547 struct glyph *glyph;
1548 enum glyph_row_area area = it->area;
1549
1550 xassert (ascent >= 0 && ascent <= 1);
1551
1552 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1553 if (glyph < it->glyph_row->glyphs[area + 1])
1554 {
1555 glyph->type = STRETCH_GLYPH;
1556 glyph->u.stretch.ascent = height * ascent;
1557 glyph->u.stretch.height = height;
1558 glyph->u.stretch.face_id = it->face_id;
1559 glyph->pixel_width = width;
1560 glyph->charpos = CHARPOS (it->position);
1561 glyph->object = object;
1562 glyph->left_box_line_p = it->start_of_box_run_p;
1563 glyph->right_box_line_p = it->end_of_box_run_p;
1564 glyph->voffset = it->voffset;
1565 glyph->multibyte_p = it->multibyte_p;
1566 ++it->glyph_row->used[area];
1567 }
1568}
1569
1570
1571/* Produce a stretch glyph for iterator IT. IT->object is the value
1572 of the glyph property displayed. The value must be a list
1573 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1574 being recognized:
1575
1576 1. `:width WIDTH' specifies that the space should be WIDTH *
1577 canonical char width wide. WIDTH may be an integer or floating
1578 point number.
1579
1580 2. `:relative-width FACTOR' specifies that the width of the stretch
1581 should be computed from the width of the first character having the
1582 `glyph' property, and should be FACTOR times that width.
1583
1584 3. `:align-to HPOS' specifies that the space should be wide enough
1585 to reach HPOS, a value in canonical character units.
1586
1587 Exactly one of the above pairs must be present.
1588
1589 4. `:height HEIGHT' specifies that the height of the stretch produced
1590 should be HEIGHT, measured in canonical character units.
1591
1592 5. `:relative-height FACTOR' specifies that the height of the the
1593 stretch should be FACTOR times the height of the characters having
1594 the glyph property.
1595
1596 Either none or exactly one of 4 or 5 must be present.
1597
1598 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1599 of the stretch should be used for the ascent of the stretch.
1600 ASCENT must be in the range 0 <= ASCENT <= 100. */
1601
1602#define NUMVAL(X) \
1603 ((INTEGERP (X) || FLOATP (X)) \
1604 ? XFLOATINT (X) \
1605 : - 1)
1606
1607
1608static void
1609x_produce_stretch_glyph (it)
1610 struct it *it;
1611{
1612 /* (space :width WIDTH :height HEIGHT. */
1613 extern Lisp_Object QCwidth, QCheight, QCascent, Qspace;
1614 extern Lisp_Object QCrelative_width, QCrelative_height;
1615 extern Lisp_Object QCalign_to;
1616 Lisp_Object prop, plist;
1617 double width = 0, height = 0, ascent = 0;
1618 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1619 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1620
1621 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1622
1623 /* List should start with `space'. */
1624 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1625 plist = XCDR (it->object);
1626
1627 /* Compute the width of the stretch. */
1628 if (prop = Fplist_get (plist, QCwidth),
1629 NUMVAL (prop) > 0)
1630 /* Absolute width `:width WIDTH' specified and valid. */
1631 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1632 else if (prop = Fplist_get (plist, QCrelative_width),
1633 NUMVAL (prop) > 0)
1634 {
1635 /* Relative width `:relative-width FACTOR' specified and valid.
1636 Compute the width of the characters having the `glyph'
1637 property. */
1638 struct it it2;
1639 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1640
1641 it2 = *it;
1642 if (it->multibyte_p)
1643 {
1644 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1645 - IT_BYTEPOS (*it));
1646 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1647 }
1648 else
1649 it2.c = *p, it2.len = 1;
1650
1651 it2.glyph_row = NULL;
1652 it2.what = IT_CHARACTER;
1653 x_produce_glyphs (&it2);
1654 width = NUMVAL (prop) * it2.pixel_width;
1655 }
1656 else if (prop = Fplist_get (plist, QCalign_to),
1657 NUMVAL (prop) > 0)
1658 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1659 else
1660 /* Nothing specified -> width defaults to canonical char width. */
1661 width = CANON_X_UNIT (it->f);
1662
1663 /* Compute height. */
1664 if (prop = Fplist_get (plist, QCheight),
1665 NUMVAL (prop) > 0)
1666 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1667 else if (prop = Fplist_get (plist, QCrelative_height),
1668 NUMVAL (prop) > 0)
1669 height = FONT_HEIGHT (font) * NUMVAL (prop);
1670 else
1671 height = FONT_HEIGHT (font);
1672
1673 /* Compute percentage of height used for ascent. If
1674 `:ascent ASCENT' is present and valid, use that. Otherwise,
1675 derive the ascent from the font in use. */
1676 if (prop = Fplist_get (plist, QCascent),
1677 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1678 ascent = NUMVAL (prop) / 100.0;
1679 else
1680 ascent = (double) font->ascent / FONT_HEIGHT (font);
1681
1682 if (width <= 0)
1683 width = 1;
1684 if (height <= 0)
1685 height = 1;
1686
1687 if (it->glyph_row)
1688 {
1689 Lisp_Object object = it->stack[it->sp - 1].string;
1690 if (!STRINGP (object))
1691 object = it->w->buffer;
1692 x_append_stretch_glyph (it, object, width, height, ascent);
1693 }
1694
1695 it->pixel_width = width;
66ac4b0e
GM
1696 it->ascent = it->phys_ascent = height * ascent;
1697 it->descent = it->phys_descent = height - it->ascent;
06a2c219
GM
1698 it->nglyphs = 1;
1699
1700 if (face->box != FACE_NO_BOX)
1701 {
1702 it->ascent += face->box_line_width;
1703 it->descent += face->box_line_width;
1704
1705 if (it->start_of_box_run_p)
1706 it->pixel_width += face->box_line_width;
1707 if (it->end_of_box_run_p)
1708 it->pixel_width += face->box_line_width;
1709 }
1710
1711 take_vertical_position_into_account (it);
1712}
1713
1714
1715/* Produce glyphs/get display metrics for the display element IT is
1716 loaded with. See the description of struct display_iterator in
1717 dispextern.h for an overview of struct display_iterator. */
1718
1719static void
1720x_produce_glyphs (it)
1721 struct it *it;
1722{
1723 if (it->what == IT_CHARACTER)
1724 {
1725 XChar2b char2b;
1726 XFontStruct *font;
1727 struct face *face;
1728 XCharStruct *pcm;
1729 struct it ci;
1730 int font_not_found_p;
1731 int c;
1732
1733 /* Maybe translate single-byte characters to multibyte. */
1734 it->char_to_display = it->c;
1735 if (unibyte_display_via_language_environment
1736 && SINGLE_BYTE_CHAR_P (it->c)
1737 && (it->c >= 0240
1738 || (it->c >= 0200
1739 && !NILP (Vnonascii_translation_table))))
1740 {
1741 it->char_to_display = unibyte_char_to_multibyte (it->c);
1742 it->charset = CHAR_CHARSET (it->char_to_display);
1743 }
1744
1745 /* Get face and font to use. Encode IT->char_to_display. */
1746 face = x_get_char_face_and_encoding (it->f, it->char_to_display,
1747 it->face_id, &char2b,
1748 it->multibyte_p);
1749 font = face->font;
1750
1751 /* When no suitable font found, use the default font. */
1752 font_not_found_p = font == NULL;
1753 if (font_not_found_p)
1754 font = FRAME_FONT (it->f);
1755
1756 if (it->char_to_display >= ' '
1757 && (!it->multibyte_p || it->char_to_display < 128))
1758 {
1759 /* Either unibyte or ASCII. */
1760 int stretched_p;
1761
1762 it->nglyphs = 1;
06a2c219
GM
1763
1764 pcm = x_per_char_metric (font, &char2b);
66ac4b0e
GM
1765 it->ascent = font->ascent;
1766 it->descent = font->descent;
1767 it->phys_ascent = pcm->ascent;
1768 it->phys_descent = pcm->descent;
06a2c219
GM
1769 it->pixel_width = pcm->width;
1770
1771 /* If this is a space inside a region of text with
1772 `space-width' property, change its width. */
1773 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1774 if (stretched_p)
1775 it->pixel_width *= XFLOATINT (it->space_width);
1776
1777 /* If face has a box, add the box thickness to the character
1778 height. If character has a box line to the left and/or
1779 right, add the box line width to the character's width. */
1780 if (face->box != FACE_NO_BOX)
1781 {
1782 int thick = face->box_line_width;
1783
1784 it->ascent += thick;
1785 it->descent += thick;
1786
1787 if (it->start_of_box_run_p)
1788 it->pixel_width += thick;
1789 if (it->end_of_box_run_p)
1790 it->pixel_width += thick;
1791 }
1792
1793 /* If face has an overline, add the height of the overline
1794 (1 pixel) and a 1 pixel margin to the character height. */
1795 if (face->overline_p)
1796 it->ascent += 2;
1797
1798 take_vertical_position_into_account (it);
1799
1800 /* If we have to actually produce glyphs, do it. */
1801 if (it->glyph_row)
1802 {
1803 if (stretched_p)
1804 {
1805 /* Translate a space with a `space-width' property
1806 into a stretch glyph. */
1807 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1808 x_append_stretch_glyph (it, it->object, it->pixel_width,
1809 it->ascent + it->descent, ascent);
1810 }
1811 else
1812 x_append_glyph (it);
1813
1814 /* If characters with lbearing or rbearing are displayed
1815 in this line, record that fact in a flag of the
1816 glyph row. This is used to optimize X output code. */
1817 if (pcm->lbearing < 0
1818 || pcm->rbearing > pcm->width)
1819 it->glyph_row->contains_overlapping_glyphs_p = 1;
1820 }
1821 }
1822 else if (it->char_to_display == '\n')
1823 {
1824 /* A newline has no width but we need the height of the line. */
1825 it->pixel_width = 0;
1826 it->nglyphs = 0;
66ac4b0e
GM
1827 it->ascent = it->phys_ascent = font->ascent;
1828 it->descent = it->phys_descent = font->descent;
06a2c219
GM
1829
1830 if (face->box != FACE_NO_BOX)
1831 {
1832 int thick = face->box_line_width;
1833 it->ascent += thick;
1834 it->descent += thick;
1835 }
1836 }
1837 else if (it->char_to_display == '\t')
1838 {
1839 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1840 int x = (it->current_x
1841 - it->prompt_width
1842 + it->continuation_lines_width);
1843 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1844
1845 it->pixel_width = next_tab_x - x;
1846 it->nglyphs = 1;
66ac4b0e
GM
1847 it->ascent = it->phys_ascent = font->ascent;
1848 it->descent = it->phys_descent = font->descent;
06a2c219
GM
1849
1850 if (it->glyph_row)
1851 {
1852 double ascent = (double) it->ascent / (it->ascent + it->descent);
1853 x_append_stretch_glyph (it, it->object, it->pixel_width,
1854 it->ascent + it->descent, ascent);
1855 }
1856 }
1857 else
1858 {
1859 /* A multi-byte character. Assume that the display width of the
1860 character is the width of the character multiplied by the
1861 width of the font. There has to be better support for
1862 variable sizes in cmpchar_info to do anything better than
1863 that.
1864
1865 Note: composite characters are represented as one glyph in
1866 the glyph matrix. There are no padding glyphs. */
1867 if (it->charset == CHARSET_COMPOSITION)
1868 {
1869 struct cmpchar_info *cmpcharp;
1870 int idx;
1871
1872 idx = COMPOSITE_CHAR_ID (it->char_to_display);
1873 cmpcharp = cmpchar_table[idx];
1874 it->pixel_width = font->max_bounds.width * cmpcharp->width;
1875
1876 /* There are no padding glyphs, so there is only one glyph
1877 to produce for the composite char. Important is that
1878 pixel_width, ascent and descent are the values of what is
1879 drawn by draw_glyphs. */
1880 it->nglyphs = 1;
1881
1882 /* These settings may not be correct. We must have more
1883 information in cmpcharp to do the correct setting. */
1884 it->ascent = font->ascent;
1885 it->descent = font->descent;
66ac4b0e
GM
1886 it->phys_ascent = font->max_bounds.ascent;
1887 it->phys_descent = font->max_bounds.descent;
06a2c219
GM
1888 }
1889 else
1890 {
1891 /* If we found a font, this font should give us the right
1892 metrics. If we didn't find a font, use the frame's
1893 default font and calculate the width of the character
1894 from the charset width; this is what old redisplay code
1895 did. */
1896 pcm = x_per_char_metric (font, &char2b);
1897 it->pixel_width = pcm->width;
1898 if (font_not_found_p)
1899 it->pixel_width *= CHARSET_WIDTH (it->charset);
1900 it->nglyphs = 1;
1901 it->ascent = font->ascent;
1902 it->descent = font->descent;
66ac4b0e
GM
1903 it->phys_ascent = pcm->ascent;
1904 it->phys_descent = pcm->descent;
06a2c219
GM
1905 if (it->glyph_row
1906 && (pcm->lbearing < 0
1907 || pcm->rbearing > pcm->width))
1908 it->glyph_row->contains_overlapping_glyphs_p = 1;
1909 }
1910
1911 if (face->box != FACE_NO_BOX)
1912 {
1913 int thick = face->box_line_width;
1914 it->ascent += thick;
1915 it->descent += thick;
1916
1917 if (it->start_of_box_run_p)
1918 it->pixel_width += thick;
1919 if (it->end_of_box_run_p)
1920 it->pixel_width += thick;
1921 }
1922
1923 /* If face has an overline, add the height of the overline
1924 (1 pixel) and a 1 pixel margin to the character height. */
1925 if (face->overline_p)
1926 it->ascent += 2;
1927
1928 take_vertical_position_into_account (it);
1929
1930 if (it->glyph_row)
1931 x_append_glyph (it);
1932 }
1933 }
1934 else if (it->what == IT_IMAGE)
1935 x_produce_image_glyph (it);
1936 else if (it->what == IT_STRETCH)
1937 x_produce_stretch_glyph (it);
1938
1939 /* Accumulate dimensions. */
1940 xassert (it->ascent >= 0 && it->descent > 0);
1941 if (it->area == TEXT_AREA)
1942 it->current_x += it->pixel_width;
66ac4b0e 1943
06a2c219
GM
1944 it->max_ascent = max (it->max_ascent, it->ascent);
1945 it->max_descent = max (it->max_descent, it->descent);
66ac4b0e
GM
1946 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
1947 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
06a2c219
GM
1948}
1949
1950
1951/* Estimate the pixel height of the mode or top line on frame F.
1952 FACE_ID specifies what line's height to estimate. */
1953
1954int
1955x_estimate_mode_line_height (f, face_id)
1956 struct frame *f;
1957 enum face_id face_id;
1958{
1959 int height = 1;
1960
1961 /* This function is called so early when Emacs starts that the face
1962 cache and mode line face are not yet initialized. */
1963 if (FRAME_FACE_CACHE (f))
1964 {
1965 struct face *face = FACE_FROM_ID (f, face_id);
1966 if (face)
1967 height = FONT_HEIGHT (face->font) + 2 * face->box_line_width;
1968 }
1969
1970 return height;
1971}
1972
1973\f
1974/***********************************************************************
1975 Glyph display
1976 ***********************************************************************/
1977
1978/* A sequence of glyphs to be drawn in the same face.
1979
1980 This data structure is not really completely X specific, so it
1981 could possibly, at least partially, be useful for other systems. It
1982 is currently not part of the external redisplay interface because
1983 it's not clear what other systems will need. */
1984
1985struct glyph_string
1986{
1987 /* X-origin of the string. */
1988 int x;
1989
1990 /* Y-origin and y-position of the base line of this string. */
1991 int y, ybase;
1992
1993 /* The width of the string, not including a face extension. */
1994 int width;
1995
1996 /* The width of the string, including a face extension. */
1997 int background_width;
1998
1999 /* The height of this string. This is the height of the line this
2000 string is drawn in, and can be different from the height of the
2001 font the string is drawn in. */
2002 int height;
2003
2004 /* Number of pixels this string overwrites in front of its x-origin.
2005 This number is zero if the string has an lbearing >= 0; it is
2006 -lbearing, if the string has an lbearing < 0. */
2007 int left_overhang;
2008
2009 /* Number of pixels this string overwrites past its right-most
2010 nominal x-position, i.e. x + width. Zero if the string's
2011 rbearing is <= its nominal width, rbearing - width otherwise. */
2012 int right_overhang;
2013
2014 /* The frame on which the glyph string is drawn. */
2015 struct frame *f;
2016
2017 /* The window on which the glyph string is drawn. */
2018 struct window *w;
2019
2020 /* X display and window for convenience. */
2021 Display *display;
2022 Window window;
2023
2024 /* The glyph row for which this string was built. It determines the
2025 y-origin and height of the string. */
2026 struct glyph_row *row;
2027
2028 /* The area within row. */
2029 enum glyph_row_area area;
2030
2031 /* Characters to be drawn, and number of characters. */
2032 XChar2b *char2b;
2033 int nchars;
2034
2035 /* Character set of this glyph string. */
2036 int charset;
2037
2038 /* A face-override for drawing cursors, mouse face and similar. */
2039 enum draw_glyphs_face hl;
2040
2041 /* Face in which this string is to be drawn. */
2042 struct face *face;
2043
2044 /* Font in which this string is to be drawn. */
2045 XFontStruct *font;
2046
2047 /* Font info for this string. */
2048 struct font_info *font_info;
2049
2050 /* Non-null means this string describes (part of) a composite
2051 character. All characters from char2b are drawn at the same
2052 x-origin in that case. */
2053 struct cmpchar_info *cmpcharp;
2054
2055 /* Index of this glyph string's first character in the glyph
2056 definition of cmpcharp. If this is zero, this glyph string
2057 describes the first character of a composite character. */
2058 int gidx;
2059
2060 /* 1 means this glyph strings face has to be drawn to the right end
2061 of the window's drawing area. */
2062 unsigned extends_to_end_of_line_p : 1;
2063
2064 /* 1 means the background of this string has been drawn. */
2065 unsigned background_filled_p : 1;
2066
2067 /* 1 means glyph string must be drawn with 16-bit functions. */
2068 unsigned two_byte_p : 1;
2069
2070 /* 1 means that the original font determined for drawing this glyph
2071 string could not be loaded. The member `font' has been set to
2072 the frame's default font in this case. */
2073 unsigned font_not_found_p : 1;
2074
2075 /* 1 means that the face in which this glyph string is drawn has a
2076 stipple pattern. */
2077 unsigned stippled_p : 1;
2078
66ac4b0e
GM
2079 /* 1 means only the foreground of this glyph string must be drawn,
2080 and we should use the physical height of the line this glyph
2081 string appears in as clip rect. */
2082 unsigned for_overlaps_p : 1;
2083
06a2c219
GM
2084 /* The GC to use for drawing this glyph string. */
2085 GC gc;
2086
2087 /* A pointer to the first glyph in the string. This glyph
2088 corresponds to char2b[0]. Needed to draw rectangles if
2089 font_not_found_p is 1. */
2090 struct glyph *first_glyph;
2091
2092 /* Image, if any. */
2093 struct image *img;
2094
2095 struct glyph_string *next, *prev;
2096};
2097
2098
2099#if GLYPH_DEBUG
2100
2101static void
2102x_dump_glyph_string (s)
2103 struct glyph_string *s;
2104{
2105 fprintf (stderr, "glyph string\n");
2106 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2107 s->x, s->y, s->width, s->height);
2108 fprintf (stderr, " ybase = %d\n", s->ybase);
2109 fprintf (stderr, " hl = %d\n", s->hl);
2110 fprintf (stderr, " left overhang = %d, right = %d\n",
2111 s->left_overhang, s->right_overhang);
2112 fprintf (stderr, " nchars = %d\n", s->nchars);
2113 fprintf (stderr, " extends to end of line = %d\n",
2114 s->extends_to_end_of_line_p);
2115 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2116 fprintf (stderr, " bg width = %d\n", s->background_width);
2117}
2118
2119#endif /* GLYPH_DEBUG */
2120
2121
2122
2123static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2124 struct glyph_string **,
2125 struct glyph_string *,
2126 struct glyph_string *));
2127static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2128 struct glyph_string **,
2129 struct glyph_string *,
2130 struct glyph_string *));
2131static void x_append_glyph_string P_ ((struct glyph_string **,
2132 struct glyph_string **,
2133 struct glyph_string *));
2134static int x_left_overwritten P_ ((struct glyph_string *));
2135static int x_left_overwriting P_ ((struct glyph_string *));
2136static int x_right_overwritten P_ ((struct glyph_string *));
2137static int x_right_overwriting P_ ((struct glyph_string *));
66ac4b0e
GM
2138static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2139 int));
06a2c219
GM
2140static void x_init_glyph_string P_ ((struct glyph_string *,
2141 XChar2b *, struct window *,
2142 struct glyph_row *,
2143 enum glyph_row_area, int,
2144 enum draw_glyphs_face));
2145static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2146 enum glyph_row_area, int, int,
66ac4b0e 2147 enum draw_glyphs_face, int *, int *, int));
06a2c219
GM
2148static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2149static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2150static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2151 int));
2152static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2153static void x_draw_glyph_string_underline P_ ((struct glyph_string *));
2154static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2155static void x_draw_glyph_string P_ ((struct glyph_string *));
2156static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2157static void x_set_cursor_gc P_ ((struct glyph_string *));
2158static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2159static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2160static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2161 int *, int *));
2162static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2163static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2164 unsigned long *, float, int));
2165static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2166 float, int, unsigned long));
2167static void x_setup_relief_colors P_ ((struct glyph_string *));
2168static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2169static void x_draw_image_relief P_ ((struct glyph_string *));
2170static void x_draw_image_foreground P_ ((struct glyph_string *));
2171static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2172static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2173static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2174 int, int, int));
2175static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2176 int, int, int, int, XRectangle *));
2177static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2178 int, int, int, XRectangle *));
66ac4b0e
GM
2179static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2180 enum glyph_row_area));
06a2c219
GM
2181
2182
06a2c219
GM
2183/* Append the list of glyph strings with head H and tail T to the list
2184 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2185
2186static INLINE void
2187x_append_glyph_string_lists (head, tail, h, t)
2188 struct glyph_string **head, **tail;
2189 struct glyph_string *h, *t;
2190{
2191 if (h)
2192 {
2193 if (*head)
2194 (*tail)->next = h;
2195 else
2196 *head = h;
2197 h->prev = *tail;
2198 *tail = t;
2199 }
2200}
2201
2202
2203/* Prepend the list of glyph strings with head H and tail T to the
2204 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2205 result. */
2206
2207static INLINE void
2208x_prepend_glyph_string_lists (head, tail, h, t)
2209 struct glyph_string **head, **tail;
2210 struct glyph_string *h, *t;
2211{
2212 if (h)
2213 {
2214 if (*head)
2215 (*head)->prev = t;
2216 else
2217 *tail = t;
2218 t->next = *head;
2219 *head = h;
2220 }
2221}
2222
2223
2224/* Append glyph string S to the list with head *HEAD and tail *TAIL.
2225 Set *HEAD and *TAIL to the resulting list. */
2226
2227static INLINE void
2228x_append_glyph_string (head, tail, s)
2229 struct glyph_string **head, **tail;
2230 struct glyph_string *s;
2231{
2232 s->next = s->prev = NULL;
2233 x_append_glyph_string_lists (head, tail, s, s);
2234}
2235
2236
2237/* Set S->gc to a suitable GC for drawing glyph string S in cursor
2238 face. */
2239
2240static void
2241x_set_cursor_gc (s)
2242 struct glyph_string *s;
2243{
2244 if (s->font == FRAME_FONT (s->f)
2245 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2246 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2247 && !s->cmpcharp)
2248 s->gc = s->f->output_data.x->cursor_gc;
2249 else
2250 {
2251 /* Cursor on non-default face: must merge. */
2252 XGCValues xgcv;
2253 unsigned long mask;
2254
2255 xgcv.background = s->f->output_data.x->cursor_pixel;
2256 xgcv.foreground = s->face->background;
2257
2258 /* If the glyph would be invisible, try a different foreground. */
2259 if (xgcv.foreground == xgcv.background)
2260 xgcv.foreground = s->face->foreground;
2261 if (xgcv.foreground == xgcv.background)
2262 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2263 if (xgcv.foreground == xgcv.background)
2264 xgcv.foreground = s->face->foreground;
2265
2266 /* Make sure the cursor is distinct from text in this face. */
2267 if (xgcv.background == s->face->background
2268 && xgcv.foreground == s->face->foreground)
2269 {
2270 xgcv.background = s->face->foreground;
2271 xgcv.foreground = s->face->background;
2272 }
2273
2274 IF_DEBUG (x_check_font (s->f, s->font));
2275 xgcv.font = s->font->fid;
2276 xgcv.graphics_exposures = False;
2277 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2278
2279 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2280 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2281 mask, &xgcv);
2282 else
2283 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2284 = XCreateGC (s->display, s->window, mask, &xgcv);
2285
2286 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2287 }
2288}
2289
2290
2291/* Set up S->gc of glyph string S for drawing text in mouse face. */
2292
2293static void
2294x_set_mouse_face_gc (s)
2295 struct glyph_string *s;
2296{
2297 int face_id;
2298
2299 /* What face has to be used for the mouse face? */
2300 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2301 face_id = FACE_FOR_CHARSET (s->f, face_id, s->charset);
2302 s->face = FACE_FROM_ID (s->f, face_id);
2303 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2304
2305 /* If font in this face is same as S->font, use it. */
2306 if (s->font == s->face->font)
2307 s->gc = s->face->gc;
2308 else
2309 {
2310 /* Otherwise construct scratch_cursor_gc with values from FACE
2311 but font FONT. */
2312 XGCValues xgcv;
2313 unsigned long mask;
2314
2315 xgcv.background = s->face->background;
2316 xgcv.foreground = s->face->foreground;
2317 IF_DEBUG (x_check_font (s->f, s->font));
2318 xgcv.font = s->font->fid;
2319 xgcv.graphics_exposures = False;
2320 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2321
2322 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2323 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2324 mask, &xgcv);
2325 else
2326 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2327 = XCreateGC (s->display, s->window, mask, &xgcv);
2328
2329 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2330 }
2331
2332 xassert (s->gc != 0);
2333}
2334
2335
2336/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2337 Faces to use in the mode line have already been computed when the
2338 matrix was built, so there isn't much to do, here. */
2339
2340static INLINE void
2341x_set_mode_line_face_gc (s)
2342 struct glyph_string *s;
2343{
2344 s->gc = s->face->gc;
2345 xassert (s->gc != 0);
2346}
2347
2348
2349/* Set S->gc of glyph string S for drawing that glyph string. Set
2350 S->stippled_p to a non-zero value if the face of S has a stipple
2351 pattern. */
2352
2353static INLINE void
2354x_set_glyph_string_gc (s)
2355 struct glyph_string *s;
2356{
2357 if (s->hl == DRAW_NORMAL_TEXT)
2358 {
2359 s->gc = s->face->gc;
2360 s->stippled_p = s->face->stipple != 0;
2361 }
2362 else if (s->hl == DRAW_INVERSE_VIDEO)
2363 {
2364 x_set_mode_line_face_gc (s);
2365 s->stippled_p = s->face->stipple != 0;
2366 }
2367 else if (s->hl == DRAW_CURSOR)
2368 {
2369 x_set_cursor_gc (s);
2370 s->stippled_p = 0;
2371 }
2372 else if (s->hl == DRAW_MOUSE_FACE)
2373 {
2374 x_set_mouse_face_gc (s);
2375 s->stippled_p = s->face->stipple != 0;
2376 }
2377 else if (s->hl == DRAW_IMAGE_RAISED
2378 || s->hl == DRAW_IMAGE_SUNKEN)
2379 {
2380 s->gc = s->face->gc;
2381 s->stippled_p = s->face->stipple != 0;
2382 }
2383 else
2384 {
2385 s->gc = s->face->gc;
2386 s->stippled_p = s->face->stipple != 0;
2387 }
2388
2389 /* GC must have been set. */
2390 xassert (s->gc != 0);
2391}
2392
2393
2394/* Return in *R the clipping rectangle for glyph string S. */
2395
2396static void
2397x_get_glyph_string_clip_rect (s, r)
2398 struct glyph_string *s;
2399 XRectangle *r;
2400{
2401 if (s->row->full_width_p)
2402 {
2403 /* Draw full-width. X coordinates are relative to S->w->left. */
1da3fd71
GM
2404 int canon_x = CANON_X_UNIT (s->f);
2405
2406 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2407 r->width = XFASTINT (s->w->width) * canon_x;
06a2c219
GM
2408
2409 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2410 {
1da3fd71 2411 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
06a2c219
GM
2412 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2413 r->x -= width;
2414 }
2415
b9432a85 2416 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
1da3fd71 2417
06a2c219
GM
2418 /* Unless displaying a mode or menu bar line, which are always
2419 fully visible, clip to the visible part of the row. */
2420 if (s->w->pseudo_window_p)
2421 r->height = s->row->visible_height;
2422 else
2423 r->height = s->height;
2424 }
2425 else
2426 {
2427 /* This is a text line that may be partially visible. */
2428 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2429 r->width = window_box_width (s->w, s->area);
2430 r->height = s->row->visible_height;
2431 }
2432
2433 /* Don't use S->y for clipping because it doesn't take partially
2434 visible lines into account. For example, it can be negative for
2435 partially visible lines at the top of a window. */
2436 if (!s->row->full_width_p
2437 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2438 r->y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (s->w);
2439 else
2440 r->y = max (0, s->row->y);
06a2c219
GM
2441
2442 /* If drawing a toolbar window, draw it over the internal border
2443 at the top of the window. */
2444 if (s->w == XWINDOW (s->f->toolbar_window))
2445 r->y -= s->f->output_data.x->internal_border_width;
66ac4b0e
GM
2446
2447 /* If S draws overlapping rows, it's sufficient to use the top and
2448 bottom of the window for clipping because this glyph string
2449 intentionally draws over other lines. */
2450 if (s->for_overlaps_p)
2451 {
2452 r->y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (s->w);
2453 r->height = window_text_bottom_y (s->w) - r->y;
2454 }
2455
2456 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
06a2c219
GM
2457}
2458
2459
2460/* Set clipping for output of glyph string S. S may be part of a mode
2461 line or menu if we don't have X toolkit support. */
2462
2463static INLINE void
2464x_set_glyph_string_clipping (s)
2465 struct glyph_string *s;
2466{
2467 XRectangle r;
2468 x_get_glyph_string_clip_rect (s, &r);
2469 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2470}
2471
2472
2473/* Compute left and right overhang of glyph string S. If S is a glyph
2474 string for a composite character, assume overhangs don't exist. */
2475
2476static INLINE void
2477x_compute_glyph_string_overhangs (s)
2478 struct glyph_string *s;
2479{
2480 if (s->cmpcharp == NULL
2481 && s->first_glyph->type == CHAR_GLYPH)
2482 {
2483 XCharStruct cs;
2484 int direction, font_ascent, font_descent;
2485 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2486 &font_ascent, &font_descent, &cs);
2487 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2488 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2489 }
2490}
2491
2492
2493/* Compute overhangs and x-positions for glyph string S and its
2494 predecessors, or successors. X is the starting x-position for S.
2495 BACKWARD_P non-zero means process predecessors. */
2496
2497static void
2498x_compute_overhangs_and_x (s, x, backward_p)
2499 struct glyph_string *s;
2500 int x;
2501 int backward_p;
2502{
2503 if (backward_p)
2504 {
2505 while (s)
2506 {
2507 x_compute_glyph_string_overhangs (s);
2508 x -= s->width;
2509 s->x = x;
2510 s = s->prev;
2511 }
2512 }
2513 else
2514 {
2515 while (s)
2516 {
2517 x_compute_glyph_string_overhangs (s);
2518 s->x = x;
2519 x += s->width;
2520 s = s->next;
2521 }
2522 }
2523}
2524
2525
2526/* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2527 frame F. Overhangs of glyphs other than type CHAR_GLYPH or of
2528 character glyphs for composite characters are assumed to be zero. */
2529
2530static void
2531x_get_glyph_overhangs (glyph, f, left, right)
2532 struct glyph *glyph;
2533 struct frame *f;
2534 int *left, *right;
2535{
2536 int c;
2537
2538 *left = *right = 0;
2539
2540 if (glyph->type == CHAR_GLYPH
2541 && (c = glyph->u.ch.code,
2542 CHAR_CHARSET (c) != CHARSET_COMPOSITION))
2543 {
2544 XFontStruct *font;
2545 struct face *face;
2546 struct font_info *font_info;
2547 XChar2b char2b;
2548
2549 face = x_get_glyph_face_and_encoding (f, glyph, &char2b);
2550 font = face->font;
2551 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2552 if (font)
2553 {
2554 XCharStruct *pcm = x_per_char_metric (font, &char2b);
2555
2556 if (pcm->rbearing > pcm->width)
2557 *right = pcm->rbearing - pcm->width;
2558 if (pcm->lbearing < 0)
2559 *left = -pcm->lbearing;
2560 }
2561 }
2562}
2563
2564
2565/* Return the index of the first glyph preceding glyph string S that
2566 is overwritten by S because of S's left overhang. Value is -1
2567 if no glyphs are overwritten. */
2568
2569static int
2570x_left_overwritten (s)
2571 struct glyph_string *s;
2572{
2573 int k;
2574
2575 if (s->left_overhang)
2576 {
2577 int x = 0, i;
2578 struct glyph *glyphs = s->row->glyphs[s->area];
2579 int first = s->first_glyph - glyphs;
2580
2581 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2582 x -= glyphs[i].pixel_width;
2583
2584 k = i + 1;
2585 }
2586 else
2587 k = -1;
2588
2589 return k;
2590}
2591
2592
2593/* Return the index of the first glyph preceding glyph string S that
2594 is overwriting S because of its right overhang. Value is -1 if no
2595 glyph in front of S overwrites S. */
2596
2597static int
2598x_left_overwriting (s)
2599 struct glyph_string *s;
2600{
2601 int i, k, x;
2602 struct glyph *glyphs = s->row->glyphs[s->area];
2603 int first = s->first_glyph - glyphs;
2604
2605 k = -1;
2606 x = 0;
2607 for (i = first - 1; i >= 0; --i)
2608 {
2609 int left, right;
2610 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2611 if (x + right > 0)
2612 k = i;
2613 x -= glyphs[i].pixel_width;
2614 }
2615
2616 return k;
2617}
2618
2619
2620/* Return the index of the last glyph following glyph string S that is
2621 not overwritten by S because of S's right overhang. Value is -1 if
2622 no such glyph is found. */
2623
2624static int
2625x_right_overwritten (s)
2626 struct glyph_string *s;
2627{
2628 int k = -1;
2629
2630 if (s->right_overhang)
2631 {
2632 int x = 0, i;
2633 struct glyph *glyphs = s->row->glyphs[s->area];
2634 int first = (s->first_glyph - glyphs) + (s->cmpcharp ? 1 : s->nchars);
2635 int end = s->row->used[s->area];
2636
2637 for (i = first; i < end && s->right_overhang > x; ++i)
2638 x += glyphs[i].pixel_width;
2639
2640 k = i;
2641 }
2642
2643 return k;
2644}
2645
2646
2647/* Return the index of the last glyph following glyph string S that
2648 overwrites S because of its left overhang. Value is negative
2649 if no such glyph is found. */
2650
2651static int
2652x_right_overwriting (s)
2653 struct glyph_string *s;
2654{
2655 int i, k, x;
2656 int end = s->row->used[s->area];
2657 struct glyph *glyphs = s->row->glyphs[s->area];
2658 int first = (s->first_glyph - glyphs) + (s->cmpcharp ? 1 : s->nchars);
2659
2660 k = -1;
2661 x = 0;
2662 for (i = first; i < end; ++i)
2663 {
2664 int left, right;
2665 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2666 if (x - left < 0)
2667 k = i;
2668 x += glyphs[i].pixel_width;
2669 }
2670
2671 return k;
2672}
2673
2674
2675/* Fill rectangle X, Y, W, H with background color of glyph string S. */
2676
2677static INLINE void
2678x_clear_glyph_string_rect (s, x, y, w, h)
2679 struct glyph_string *s;
2680 int x, y, w, h;
2681{
2682 XGCValues xgcv;
2683 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
2684 XSetForeground (s->display, s->gc, xgcv.background);
2685 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2686 XSetForeground (s->display, s->gc, xgcv.foreground);
2687}
2688
2689
2690/* Draw the background of glyph_string S. If S->background_filled_p
2691 is non-zero don't draw it. FORCE_P non-zero means draw the
2692 background even if it wouldn't be drawn normally. This is used
2693 when a string preceding S draws into the background of S. */
2694
2695static void
2696x_draw_glyph_string_background (s, force_p)
2697 struct glyph_string *s;
2698 int force_p;
2699{
2700 /* Nothing to do if background has already been drawn or if it
2701 shouldn't be drawn in the first place. */
2702 if (!s->background_filled_p)
2703 {
2704 if (s->cmpcharp
2705 && s->gidx > 0
2706 && !s->font_not_found_p
2707 && !s->extends_to_end_of_line_p)
2708 {
2709 /* Don't draw background for glyphs of a composite
2710 characters, except for the first one. */
2711 s->background_filled_p = 1;
2712 }
2713 else if (s->stippled_p)
2714 {
2715 /* Fill background with a stipple pattern. */
2716 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2717 XFillRectangle (s->display, s->window, s->gc, s->x,
2718 s->y + s->face->box_line_width,
2719 s->background_width,
2720 s->height - 2 * s->face->box_line_width);
2721 XSetFillStyle (s->display, s->gc, FillSolid);
2722 s->background_filled_p = 1;
2723 }
2724 else if (FONT_HEIGHT (s->font) < s->height - 2 * s->face->box_line_width
2725 || s->font_not_found_p
2726 || s->extends_to_end_of_line_p
2727 || s->cmpcharp
2728 || force_p)
2729 {
2730 x_clear_glyph_string_rect (s, s->x, s->y + s->face->box_line_width,
2731 s->background_width,
2732 s->height - 2 * s->face->box_line_width);
2733 s->background_filled_p = 1;
2734 }
2735 }
2736}
2737
2738
2739/* Draw the foreground of glyph string S. */
2740
2741static void
2742x_draw_glyph_string_foreground (s)
2743 struct glyph_string *s;
2744{
2745 int i, x;
2746
2747 /* If first glyph of S has a left box line, start drawing the text
2748 of S to the right of that box line. */
2749 if (s->face->box != FACE_NO_BOX
2750 && s->first_glyph->left_box_line_p)
2751 x = s->x + s->face->box_line_width;
2752 else
2753 x = s->x;
2754
2755 if (s->cmpcharp == NULL)
2756 {
2757 /* Not a composite character. Draw characters of S as
2758 rectangles if S's font could not be loaded. */
2759 if (s->font_not_found_p)
2760 {
2761 for (i = 0; i < s->nchars; ++i)
2762 {
2763 struct glyph *g = s->first_glyph + i;
2764 XDrawRectangle (s->display, s->window,
2765 s->gc, x, s->y, g->pixel_width - 1,
2766 s->height - 1);
2767 x += g->pixel_width;
2768 }
2769 }
2770 else
2771 {
2772 char *char1b = (char *) s->char2b;
2773
2774 /* If we can use 8-bit functions, condense S->char2b. */
2775 if (!s->two_byte_p)
2776 for (i = 0; i < s->nchars; ++i)
2777 char1b[i] = s->char2b[i].byte2;
2778
2779 /* Draw text with XDrawString if background has already been
2780 filled. Otherwise, use XDrawImageString. (Note that
2781 XDrawImageString is usually faster than XDrawString.)
2782 Always use XDrawImageString when drawing the cursor so
2783 that there is no chance that characters under a box
2784 cursor are invisible. */
66ac4b0e
GM
2785 if (s->for_overlaps_p
2786 || (s->background_filled_p && s->hl != DRAW_CURSOR))
06a2c219
GM
2787 {
2788 /* Draw characters with 16-bit or 8-bit functions. */
2789 if (s->two_byte_p)
2790 XDrawString16 (s->display, s->window, s->gc, x, s->ybase,
2791 s->char2b, s->nchars);
2792 else
2793 XDrawString (s->display, s->window, s->gc, x, s->ybase,
2794 char1b, s->nchars);
2795 }
2796 else
2797 {
2798 if (s->two_byte_p)
2799 XDrawImageString16 (s->display, s->window, s->gc,
2800 x, s->ybase, s->char2b, s->nchars);
2801 else
2802 XDrawImageString (s->display, s->window, s->gc,
2803 x, s->ybase, char1b, s->nchars);
2804 }
2805 }
2806 }
2807 else
2808 {
2809 /* S is a glyph string for a composite character. S->gidx is the
2810 index of the first character drawn in the vector
2811 S->cmpcharp->glyph. S->gidx == 0 means we are drawing the
2812 very first component character of a composite char. */
2813
2814 /* Draw a single rectangle for the composite character if S's
2815 font could not be loaded. */
2816 if (s->font_not_found_p && s->gidx == 0)
2817 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
2818 s->width - 1, s->height - 1);
2819 else
2820 {
2821 XCharStruct *pcm;
2822 int relative_compose, default_ascent, i;
2823 int highest = 0, lowest = 0;
2824
2825 /* The value of font_info my be null if we couldn't find it
2826 in x_get_char_face_and_encoding. */
2827 if (s->cmpcharp->cmp_rule == NULL && s->font_info)
2828 {
2829 relative_compose = s->font_info->relative_compose;
2830 default_ascent = s->font_info->default_ascent;
2831 }
2832 else
2833 relative_compose = default_ascent = 0;
2834
2835 if ((s->cmpcharp->cmp_rule || relative_compose)
2836 && s->gidx == 0)
2837 {
2838 /* This is the first character. Initialize variables.
2839 Highest is the highest position of glyphs ever
2840 written, lowest the lowest position. */
2841 int x_offset = 0;
2842 int first_ch = s->first_glyph->u.ch.code;
2843
2844 if (default_ascent
2845 && CHAR_TABLE_P (Vuse_default_ascent)
2846 && !NILP (Faref (Vuse_default_ascent, first_ch)))
2847 {
2848 highest = default_ascent;
2849 lowest = 0;
2850 }
2851 else
2852 {
2853 pcm = PER_CHAR_METRIC (s->font, s->char2b);
2854 highest = pcm->ascent + 1;
2855 lowest = - pcm->descent;
2856 }
2857
2858 if (s->cmpcharp->cmp_rule)
2859 x_offset = (s->cmpcharp->col_offset[0]
2860 * FONT_WIDTH (s->f->output_data.x->font));
2861
2862 /* Draw the first character at the normal position. */
2863 XDrawString16 (s->display, s->window, s->gc,
2864 x + x_offset,
2865 s->ybase, s->char2b, 1);
2866 i = 1;
2867 ++s->gidx;
2868 }
2869 else
2870 i = 0;
2871
2872 for (; i < s->nchars; i++, ++s->gidx)
2873 {
2874 int x_offset = 0, y_offset = 0;
2875
2876 if (relative_compose)
2877 {
2878 pcm = PER_CHAR_METRIC (s->font, s->char2b + i);
2879 if (NILP (Vignore_relative_composition)
2880 || NILP (Faref (Vignore_relative_composition,
2881 make_number (s->cmpcharp->glyph[s->gidx]))))
2882 {
2883 if (- pcm->descent >= relative_compose)
2884 {
2885 /* Draw above the current glyphs. */
2886 y_offset = highest + pcm->descent;
2887 highest += pcm->ascent + pcm->descent;
2888 }
2889 else if (pcm->ascent <= 0)
2890 {
2891 /* Draw beneath the current glyphs. */
2892 y_offset = lowest - pcm->ascent;
2893 lowest -= pcm->ascent + pcm->descent;
2894 }
2895 }
2896 else
2897 {
2898 /* Draw the glyph at normal position. If
2899 it sticks out of HIGHEST or LOWEST,
2900 update them appropriately. */
2901 if (pcm->ascent > highest)
2902 highest = pcm->ascent;
2903 else if (- pcm->descent < lowest)
2904 lowest = - pcm->descent;
2905 }
2906 }
2907 else if (s->cmpcharp->cmp_rule)
2908 {
2909 int gref = (s->cmpcharp->cmp_rule[s->gidx] - 0xA0) / 9;
2910 int nref = (s->cmpcharp->cmp_rule[s->gidx] - 0xA0) % 9;
2911 int bottom, top;
2912
2913 /* Re-encode GREF and NREF so that they specify
2914 only Y-axis information:
2915 0:top, 1:base, 2:bottom, 3:center */
2916 gref = gref / 3 + (gref == 4) * 2;
2917 nref = nref / 3 + (nref == 4) * 2;
2918
2919 pcm = PER_CHAR_METRIC (s->font, s->char2b + i);
2920 bottom = ((gref == 0 ? highest : gref == 1 ? 0
2921 : gref == 2 ? lowest
2922 : (highest + lowest) / 2)
2923 - (nref == 0 ? pcm->ascent + pcm->descent
2924 : nref == 1 ? pcm->descent : nref == 2 ? 0
2925 : (pcm->ascent + pcm->descent) / 2));
2926 top = bottom + (pcm->ascent + pcm->descent);
2927 if (top > highest)
2928 highest = top;
2929 if (bottom < lowest)
2930 lowest = bottom;
2931 y_offset = bottom + pcm->descent;
2932 x_offset = (s->cmpcharp->col_offset[s->gidx]
2933 * FONT_WIDTH (FRAME_FONT (s->f)));
2934 }
2935
2936 XDrawString16 (s->display, s->window, s->gc,
2937 x + x_offset, s->ybase - y_offset,
2938 s->char2b + i, 1);
2939 }
2940 }
2941 }
2942}
2943
2944
2945/* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
2946 CMAP. If an exact match can't be allocated, try the nearest color
2947 available. Value is non-zero if successful. Set *COLOR to the
2948 color allocated. */
2949
2950int
2951x_alloc_nearest_color (display, screen, cmap, color)
2952 Display *display;
2953 Screen *screen;
2954 Colormap cmap;
2955 XColor *color;
2956{
2957 int rc = XAllocColor (display, cmap, color);
2958 if (rc == 0)
2959 {
2960 /* If we got to this point, the colormap is full, so we're going
2961 to try to get the next closest color. The algorithm used is
2962 a least-squares matching, which is what X uses for closest
2963 color matching with StaticColor visuals. */
2964 int nearest, i;
2965 unsigned long nearest_delta = ~0;
2966 int ncells = XDisplayCells (display, XScreenNumberOfScreen (screen));
2967 XColor *cells = (XColor *) alloca (ncells * sizeof *cells);
2968
2969 for (i = 0; i < ncells; ++i)
2970 cells[i].pixel = i;
2971 XQueryColors (display, cmap, cells, ncells);
2972
2973 for (nearest = i = 0; i < ncells; ++i)
2974 {
2975 long dred = (color->red >> 8) - (cells[i].red >> 8);
2976 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
2977 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
2978 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
2979
2980 if (delta < nearest_delta)
2981 {
2982 nearest = i;
2983 nearest_delta = delta;
2984 }
2985 }
2986
2987 color->red = cells[nearest].red;
2988 color->green = cells[nearest].green;
2989 color->blue = cells[nearest].blue;
2990 rc = XAllocColor (display, cmap, color);
2991 }
2992
2993 return rc;
2994}
2995
2996
2997/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
2998 or DELTA. Try a color with RGB values multiplied by FACTOR first.
2999 If this produces the same color as PIXEL, try a color where all RGB
3000 values have DELTA added. Return the allocated color in *PIXEL.
3001 DISPLAY is the X display, CMAP is the colormap to operate on.
3002 Value is non-zero if successful. */
3003
3004static int
3005x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3006 struct frame *f;
3007 Display *display;
3008 Colormap cmap;
3009 unsigned long *pixel;
3010 float factor;
3011 int delta;
3012{
3013 XColor color, new;
3014 int success_p;
3015
3016 /* Get RGB color values. */
3017 color.pixel = *pixel;
3018 XQueryColor (display, cmap, &color);
3019
3020 /* Change RGB values by specified FACTOR. Avoid overflow! */
3021 xassert (factor >= 0);
3022 new.red = min (0xffff, factor * color.red);
3023 new.green = min (0xffff, factor * color.green);
3024 new.blue = min (0xffff, factor * color.blue);
3025
3026 /* Try to allocate the color. */
3027 success_p = x_alloc_nearest_color (display, FRAME_X_SCREEN (f), cmap, &new);
3028 if (success_p)
3029 {
3030 if (new.pixel == *pixel)
3031 {
3032 /* If we end up with the same color as before, try adding
3033 delta to the RGB values. */
3034 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
3035
3036 /* If display has an immutable color map, freeing colors is
3037 not necessary and some servers don't allow it. So don't
3038 do it. */
3039 if (class != StaticColor
3040 && class != StaticGray
3041 && class != TrueColor)
3042 XFreeColors (display, cmap, &new.pixel, 1, 0);
3043
3044 new.red = min (0xffff, delta + color.red);
3045 new.green = min (0xffff, delta + color.green);
3046 new.blue = min (0xffff, delta + color.blue);
3047 success_p = x_alloc_nearest_color (display, FRAME_X_SCREEN (f),
3048 cmap, &new);
3049 }
3050 else
3051 success_p = 1;
3052 *pixel = new.pixel;
3053 }
3054
3055 return success_p;
3056}
3057
3058
3059/* Set up the foreground color for drawing relief lines of glyph
3060 string S. RELIEF is a pointer to a struct relief containing the GC
3061 with which lines will be drawn. Use a color that is FACTOR or
3062 DELTA lighter or darker than the relief's background which is found
3063 in S->f->output_data.x->relief_background. If such a color cannot
3064 be allocated, use DEFAULT_PIXEL, instead. */
3065
3066static void
3067x_setup_relief_color (f, relief, factor, delta, default_pixel)
3068 struct frame *f;
3069 struct relief *relief;
3070 float factor;
3071 int delta;
3072 unsigned long default_pixel;
3073{
3074 XGCValues xgcv;
3075 struct x_output *di = f->output_data.x;
3076 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3077 unsigned long pixel;
3078 unsigned long background = di->relief_background;
3079 Colormap cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
dcd08bfb
GM
3080 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3081 Display *dpy = FRAME_X_DISPLAY (f);
06a2c219
GM
3082
3083 xgcv.graphics_exposures = False;
3084 xgcv.line_width = 1;
3085
3086 /* Free previously allocated color. The color cell will be reused
3087 when it has been freed as many times as it was allocated, so this
3088 doesn't affect faces using the same colors. */
3089 if (relief->gc
3090 && relief->allocated_p)
3091 {
3092 /* If display has an immutable color map, freeing colors is not
3093 necessary and some servers don't allow it. So don't do it. */
dcd08bfb 3094 int class = dpyinfo->visual->class;
06a2c219
GM
3095 if (class != StaticColor
3096 && class != StaticGray
3097 && class != TrueColor)
dcd08bfb 3098 XFreeColors (dpy, cmap, &relief->pixel, 1, 0);
06a2c219
GM
3099 relief->allocated_p = 0;
3100 }
3101
3102 /* Allocate new color. */
3103 xgcv.foreground = default_pixel;
3104 pixel = background;
dcd08bfb
GM
3105 if (dpyinfo->n_planes != 1
3106 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
06a2c219
GM
3107 {
3108 relief->allocated_p = 1;
3109 xgcv.foreground = relief->pixel = pixel;
3110 }
3111
3112 if (relief->gc == 0)
3113 {
dcd08bfb 3114 xgcv.stipple = dpyinfo->gray;
06a2c219 3115 mask |= GCStipple;
dcd08bfb 3116 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
06a2c219
GM
3117 }
3118 else
dcd08bfb 3119 XChangeGC (dpy, relief->gc, mask, &xgcv);
06a2c219
GM
3120}
3121
3122
3123/* Set up colors for the relief lines around glyph string S. */
3124
3125static void
3126x_setup_relief_colors (s)
3127 struct glyph_string *s;
3128{
3129 struct x_output *di = s->f->output_data.x;
3130 unsigned long color;
3131
3132 if (s->face->use_box_color_for_shadows_p)
3133 color = s->face->box_color;
3134 else
3135 {
3136 XGCValues xgcv;
3137
3138 /* Get the background color of the face. */
3139 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3140 color = xgcv.background;
3141 }
3142
3143 if (di->white_relief.gc == 0
3144 || color != di->relief_background)
3145 {
3146 di->relief_background = color;
3147 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3148 WHITE_PIX_DEFAULT (s->f));
3149 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3150 BLACK_PIX_DEFAULT (s->f));
3151 }
3152}
3153
3154
3155/* Draw a relief on frame F inside the rectangle given by LEFT_X,
3156 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3157 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3158 relief. LEFT_P non-zero means draw a relief on the left side of
3159 the rectangle. RIGHT_P non-zero means draw a relief on the right
3160 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3161 when drawing. */
3162
3163static void
3164x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3165 raised_p, left_p, right_p, clip_rect)
3166 struct frame *f;
3167 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3168 XRectangle *clip_rect;
3169{
3170 int i;
3171 GC gc;
3172
3173 if (raised_p)
3174 gc = f->output_data.x->white_relief.gc;
3175 else
3176 gc = f->output_data.x->black_relief.gc;
3177 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3178
3179 /* Top. */
3180 for (i = 0; i < width; ++i)
3181 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3182 left_x + i * left_p, top_y + i,
3183 right_x + 1 - i * right_p, top_y + i);
3184
3185 /* Left. */
3186 if (left_p)
3187 for (i = 0; i < width; ++i)
3188 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3189 left_x + i, top_y + i, left_x + i, bottom_y - i);
3190
3191 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3192 if (raised_p)
3193 gc = f->output_data.x->black_relief.gc;
3194 else
3195 gc = f->output_data.x->white_relief.gc;
3196 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3197
3198 /* Bottom. */
3199 for (i = 0; i < width; ++i)
3200 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3201 left_x + i * left_p, bottom_y - i,
3202 right_x + 1 - i * right_p, bottom_y - i);
3203
3204 /* Right. */
3205 if (right_p)
3206 for (i = 0; i < width; ++i)
3207 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3208 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3209
3210 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3211}
3212
3213
3214/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3215 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3216 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3217 left side of the rectangle. RIGHT_P non-zero means draw a line
3218 on the right side of the rectangle. CLIP_RECT is the clipping
3219 rectangle to use when drawing. */
3220
3221static void
3222x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3223 left_p, right_p, clip_rect)
3224 struct glyph_string *s;
3225 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3226 XRectangle *clip_rect;
3227{
3228 XGCValues xgcv;
3229
3230 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3231 XSetForeground (s->display, s->gc, s->face->box_color);
3232 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3233
3234 /* Top. */
3235 XFillRectangle (s->display, s->window, s->gc,
3236 left_x, top_y, right_x - left_x, width);
3237
3238 /* Left. */
3239 if (left_p)
3240 XFillRectangle (s->display, s->window, s->gc,
3241 left_x, top_y, width, bottom_y - top_y);
3242
3243 /* Bottom. */
3244 XFillRectangle (s->display, s->window, s->gc,
3245 left_x, bottom_y - width, right_x - left_x, width);
3246
3247 /* Right. */
3248 if (right_p)
3249 XFillRectangle (s->display, s->window, s->gc,
3250 right_x - width, top_y, width, bottom_y - top_y);
3251
3252 XSetForeground (s->display, s->gc, xgcv.foreground);
3253 XSetClipMask (s->display, s->gc, None);
3254}
3255
3256
3257/* Draw a box around glyph string S. */
3258
3259static void
3260x_draw_glyph_string_box (s)
3261 struct glyph_string *s;
3262{
3263 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3264 int left_p, right_p;
3265 struct glyph *last_glyph;
3266 XRectangle clip_rect;
3267
3268 last_x = window_box_right (s->w, s->area);
3269 if (s->row->full_width_p
3270 && !s->w->pseudo_window_p)
3271 {
3272 last_x += FRAME_X_FLAGS_AREA_WIDTH (s->f);
3273 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3274 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3275 }
3276
3277 /* The glyph that may have a right box line. */
3278 last_glyph = (s->cmpcharp || s->img
3279 ? s->first_glyph
3280 : s->first_glyph + s->nchars - 1);
3281
3282 width = s->face->box_line_width;
3283 raised_p = s->face->box == FACE_RAISED_BOX;
3284 left_x = s->x;
3285 right_x = ((s->row->full_width_p
1da3fd71 3286 ? last_x - 1
a7aeb2de 3287 : min (last_x, s->x + s->background_width) - 1));
06a2c219
GM
3288 top_y = s->y;
3289 bottom_y = top_y + s->height - 1;
3290
3291 left_p = (s->first_glyph->left_box_line_p
3292 || (s->hl == DRAW_MOUSE_FACE
3293 && (s->prev == NULL
3294 || s->prev->hl != s->hl)));
3295 right_p = (last_glyph->right_box_line_p
3296 || (s->hl == DRAW_MOUSE_FACE
3297 && (s->next == NULL
3298 || s->next->hl != s->hl)));
3299
3300 x_get_glyph_string_clip_rect (s, &clip_rect);
3301
3302 if (s->face->box == FACE_SIMPLE_BOX)
3303 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3304 left_p, right_p, &clip_rect);
3305 else
3306 {
3307 x_setup_relief_colors (s);
3308 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3309 width, raised_p, left_p, right_p, &clip_rect);
3310 }
3311}
3312
3313
3314/* Draw foreground of image glyph string S. */
3315
3316static void
3317x_draw_image_foreground (s)
3318 struct glyph_string *s;
3319{
3320 int x;
3321 int y = s->ybase - IMAGE_ASCENT (s->img);
3322
3323 /* If first glyph of S has a left box line, start drawing it to the
3324 right of that line. */
3325 if (s->face->box != FACE_NO_BOX
3326 && s->first_glyph->left_box_line_p)
3327 x = s->x + s->face->box_line_width;
3328 else
3329 x = s->x;
3330
3331 /* If there is a margin around the image, adjust x- and y-position
3332 by that margin. */
3333 if (s->img->margin)
3334 {
3335 x += s->img->margin;
3336 y += s->img->margin;
3337 }
3338
3339 if (s->img->pixmap)
3340 {
3341 if (s->img->mask)
3342 {
3343 /* We can't set both a clip mask and use XSetClipRectangles
3344 because the latter also sets a clip mask. We also can't
3345 trust on the shape extension to be available
3346 (XShapeCombineRegion). So, compute the rectangle to draw
3347 manually. */
3348 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3349 | GCFunction);
3350 XGCValues xgcv;
3351 XRectangle clip_rect, image_rect, r;
3352
3353 xgcv.clip_mask = s->img->mask;
3354 xgcv.clip_x_origin = x;
3355 xgcv.clip_y_origin = y;
3356 xgcv.function = GXcopy;
3357 XChangeGC (s->display, s->gc, mask, &xgcv);
3358
3359 x_get_glyph_string_clip_rect (s, &clip_rect);
3360 image_rect.x = x;
3361 image_rect.y = y;
3362 image_rect.width = s->img->width;
3363 image_rect.height = s->img->height;
3364 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3365 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3366 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3367 }
3368 else
3369 {
3370 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3371 0, 0, s->img->width, s->img->height, x, y);
3372
3373 /* When the image has a mask, we can expect that at
3374 least part of a mouse highlight or a block cursor will
3375 be visible. If the image doesn't have a mask, make
3376 a block cursor visible by drawing a rectangle around
3377 the image. I believe it's looking better if we do
3378 nothing here for mouse-face. */
3379 if (s->hl == DRAW_CURSOR)
3380 XDrawRectangle (s->display, s->window, s->gc, x, y,
3381 s->img->width - 1, s->img->height - 1);
3382 }
3383 }
3384 else
3385 /* Draw a rectangle if image could not be loaded. */
3386 XDrawRectangle (s->display, s->window, s->gc, x, y,
3387 s->img->width - 1, s->img->height - 1);
3388}
3389
3390
3391/* Draw a relief around the image glyph string S. */
3392
3393static void
3394x_draw_image_relief (s)
3395 struct glyph_string *s;
3396{
3397 int x0, y0, x1, y1, thick, raised_p;
3398 XRectangle r;
3399 int x;
3400 int y = s->ybase - IMAGE_ASCENT (s->img);
3401
3402 /* If first glyph of S has a left box line, start drawing it to the
3403 right of that line. */
3404 if (s->face->box != FACE_NO_BOX
3405 && s->first_glyph->left_box_line_p)
3406 x = s->x + s->face->box_line_width;
3407 else
3408 x = s->x;
3409
3410 /* If there is a margin around the image, adjust x- and y-position
3411 by that margin. */
3412 if (s->img->margin)
3413 {
3414 x += s->img->margin;
3415 y += s->img->margin;
3416 }
3417
3418 if (s->hl == DRAW_IMAGE_SUNKEN
3419 || s->hl == DRAW_IMAGE_RAISED)
3420 {
3421 thick = toolbar_button_relief > 0 ? toolbar_button_relief : 3;
3422 raised_p = s->hl == DRAW_IMAGE_RAISED;
3423 }
3424 else
3425 {
3426 thick = abs (s->img->relief);
3427 raised_p = s->img->relief > 0;
3428 }
3429
3430 x0 = x - thick;
3431 y0 = y - thick;
3432 x1 = x + s->img->width + thick - 1;
3433 y1 = y + s->img->height + thick - 1;
3434
3435 x_setup_relief_colors (s);
3436 x_get_glyph_string_clip_rect (s, &r);
3437 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3438}
3439
3440
3441/* Draw the foreground of image glyph string S to PIXMAP. */
3442
3443static void
3444x_draw_image_foreground_1 (s, pixmap)
3445 struct glyph_string *s;
3446 Pixmap pixmap;
3447{
3448 int x;
3449 int y = s->ybase - s->y - IMAGE_ASCENT (s->img);
3450
3451 /* If first glyph of S has a left box line, start drawing it to the
3452 right of that line. */
3453 if (s->face->box != FACE_NO_BOX
3454 && s->first_glyph->left_box_line_p)
3455 x = s->face->box_line_width;
3456 else
3457 x = 0;
3458
3459 /* If there is a margin around the image, adjust x- and y-position
3460 by that margin. */
3461 if (s->img->margin)
3462 {
3463 x += s->img->margin;
3464 y += s->img->margin;
3465 }
dc43ef94 3466
06a2c219
GM
3467 if (s->img->pixmap)
3468 {
3469 if (s->img->mask)
3470 {
3471 /* We can't set both a clip mask and use XSetClipRectangles
3472 because the latter also sets a clip mask. We also can't
3473 trust on the shape extension to be available
3474 (XShapeCombineRegion). So, compute the rectangle to draw
3475 manually. */
3476 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3477 | GCFunction);
3478 XGCValues xgcv;
3479
3480 xgcv.clip_mask = s->img->mask;
3481 xgcv.clip_x_origin = x;
3482 xgcv.clip_y_origin = y;
3483 xgcv.function = GXcopy;
3484 XChangeGC (s->display, s->gc, mask, &xgcv);
3485
3486 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3487 0, 0, s->img->width, s->img->height, x, y);
3488 XSetClipMask (s->display, s->gc, None);
3489 }
3490 else
3491 {
3492 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3493 0, 0, s->img->width, s->img->height, x, y);
3494
3495 /* When the image has a mask, we can expect that at
3496 least part of a mouse highlight or a block cursor will
3497 be visible. If the image doesn't have a mask, make
3498 a block cursor visible by drawing a rectangle around
3499 the image. I believe it's looking better if we do
3500 nothing here for mouse-face. */
3501 if (s->hl == DRAW_CURSOR)
3502 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3503 s->img->width - 1, s->img->height - 1);
3504 }
3505 }
3506 else
3507 /* Draw a rectangle if image could not be loaded. */
3508 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3509 s->img->width - 1, s->img->height - 1);
3510}
dc43ef94 3511
990ba854 3512
06a2c219
GM
3513/* Draw part of the background of glyph string S. X, Y, W, and H
3514 give the rectangle to draw. */
a9a5b0a5 3515
06a2c219
GM
3516static void
3517x_draw_glyph_string_bg_rect (s, x, y, w, h)
3518 struct glyph_string *s;
3519 int x, y, w, h;
3520{
3521 if (s->stippled_p)
3522 {
3523 /* Fill background with a stipple pattern. */
3524 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3525 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3526 XSetFillStyle (s->display, s->gc, FillSolid);
3527 }
3528 else
3529 x_clear_glyph_string_rect (s, x, y, w, h);
3530}
07e34cb0 3531
b5210ea7 3532
06a2c219 3533/* Draw image glyph string S.
dc43ef94 3534
06a2c219
GM
3535 s->y
3536 s->x +-------------------------
3537 | s->face->box
3538 |
3539 | +-------------------------
3540 | | s->img->margin
3541 | |
3542 | | +-------------------
3543 | | | the image
dc43ef94 3544
06a2c219 3545 */
dc43ef94 3546
06a2c219
GM
3547static void
3548x_draw_image_glyph_string (s)
3549 struct glyph_string *s;
3550{
3551 int x, y;
3552 int box_line_width = s->face->box_line_width;
3553 int margin = s->img->margin;
3554 int height;
3555 Pixmap pixmap = None;
3556
3557 height = s->height - 2 * box_line_width;
3558
3559 /* Fill background with face under the image. Do it only if row is
3560 taller than image or if image has a clip mask to reduce
3561 flickering. */
3562 s->stippled_p = s->face->stipple != 0;
3563 if (height > s->img->height
3564 || margin
3565 || s->img->mask
3566 || s->img->pixmap == 0
3567 || s->width != s->background_width)
3568 {
3569 if (box_line_width && s->first_glyph->left_box_line_p)
3570 x = s->x + box_line_width;
3571 else
3572 x = s->x;
3573
3574 y = s->y + box_line_width;
3575
3576 if (s->img->mask)
3577 {
3578 /* Create a pixmap as large as the glyph string Fill it with
3579 the background color. Copy the image to it, using its
3580 mask. Copy the temporary pixmap to the display. */
3581 Screen *screen = FRAME_X_SCREEN (s->f);
3582 int depth = DefaultDepthOfScreen (screen);
3583
3584 /* Create a pixmap as large as the glyph string. */
3585 pixmap = XCreatePixmap (s->display, s->window,
3586 s->background_width,
3587 s->height, depth);
3588
3589 /* Don't clip in the following because we're working on the
3590 pixmap. */
3591 XSetClipMask (s->display, s->gc, None);
3592
3593 /* Fill the pixmap with the background color/stipple. */
3594 if (s->stippled_p)
3595 {
3596 /* Fill background with a stipple pattern. */
3597 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3598 XFillRectangle (s->display, pixmap, s->gc,
3599 0, 0, s->background_width, s->height);
3600 XSetFillStyle (s->display, s->gc, FillSolid);
3601 }
3602 else
3603 {
3604 XGCValues xgcv;
3605 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3606 &xgcv);
3607 XSetForeground (s->display, s->gc, xgcv.background);
3608 XFillRectangle (s->display, pixmap, s->gc,
3609 0, 0, s->background_width, s->height);
3610 XSetForeground (s->display, s->gc, xgcv.foreground);
3611 }
3612 }
3613 else
3614 /* Implementation idea: Is it possible to construct a mask?
3615 We could look at the color at the margins of the image, and
3616 say that this color is probably the background color of the
3617 image. */
3618 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
3619
3620 s->background_filled_p = 1;
3621 }
dc43ef94 3622
06a2c219
GM
3623 /* Draw the foreground. */
3624 if (pixmap != None)
3625 {
3626 x_draw_image_foreground_1 (s, pixmap);
3627 x_set_glyph_string_clipping (s);
3628 XCopyArea (s->display, pixmap, s->window, s->gc,
3629 0, 0, s->background_width, s->height, s->x, s->y);
3630 XFreePixmap (s->display, pixmap);
3631 }
3632 else
3633 x_draw_image_foreground (s);
b5210ea7 3634
06a2c219
GM
3635 /* If we must draw a relief around the image, do it. */
3636 if (s->img->relief
3637 || s->hl == DRAW_IMAGE_RAISED
3638 || s->hl == DRAW_IMAGE_SUNKEN)
3639 x_draw_image_relief (s);
3640}
8c1a6a84 3641
990ba854 3642
06a2c219 3643/* Draw stretch glyph string S. */
dc43ef94 3644
06a2c219
GM
3645static void
3646x_draw_stretch_glyph_string (s)
3647 struct glyph_string *s;
3648{
3649 xassert (s->first_glyph->type == STRETCH_GLYPH);
3650 s->stippled_p = s->face->stipple != 0;
990ba854 3651
06a2c219
GM
3652 if (s->hl == DRAW_CURSOR
3653 && !x_stretch_cursor_p)
3654 {
3655 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
3656 as wide as the stretch glyph. */
3657 int width = min (CANON_X_UNIT (s->f), s->background_width);
990ba854 3658
06a2c219
GM
3659 /* Draw cursor. */
3660 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
0cdd0c9f 3661
06a2c219
GM
3662 /* Clear rest using the GC of the original non-cursor face. */
3663 if (width < s->background_width)
3664 {
3665 GC gc = s->face->gc;
3666 int x = s->x + width, y = s->y;
3667 int w = s->background_width - width, h = s->height;
3668 XRectangle r;
dc43ef94 3669
06a2c219
GM
3670 x_get_glyph_string_clip_rect (s, &r);
3671 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
97210f4e 3672
06a2c219
GM
3673 if (s->face->stipple)
3674 {
3675 /* Fill background with a stipple pattern. */
3676 XSetFillStyle (s->display, gc, FillOpaqueStippled);
3677 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3678 XSetFillStyle (s->display, gc, FillSolid);
3679 }
3680 else
3681 {
3682 XGCValues xgcv;
3683 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
3684 XSetForeground (s->display, gc, xgcv.background);
3685 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3686 XSetForeground (s->display, gc, xgcv.foreground);
3687 }
3688 }
3689 }
3690 else
3691 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
3692 s->height);
3693
3694 s->background_filled_p = 1;
3695}
3696
3697
3698/* Draw glyph string S. */
3699
3700static void
3701x_draw_glyph_string (s)
3702 struct glyph_string *s;
3703{
3704 /* If S draws into the background of its successor, draw the
3705 background of the successor first so that S can draw into it.
3706 This makes S->next use XDrawString instead of XDrawImageString. */
66ac4b0e 3707 if (s->next && s->right_overhang && !s->for_overlaps_p)
06a2c219
GM
3708 {
3709 xassert (s->next->img == NULL);
3710 x_set_glyph_string_gc (s->next);
3711 x_set_glyph_string_clipping (s->next);
3712 x_draw_glyph_string_background (s->next, 1);
3713 }
97210f4e 3714
06a2c219
GM
3715 /* Set up S->gc, set clipping and draw S. */
3716 x_set_glyph_string_gc (s);
3717 x_set_glyph_string_clipping (s);
3718
3719 switch (s->first_glyph->type)
3720 {
3721 case IMAGE_GLYPH:
3722 x_draw_image_glyph_string (s);
3723 break;
3724
3725 case STRETCH_GLYPH:
3726 x_draw_stretch_glyph_string (s);
3727 break;
3728
3729 case CHAR_GLYPH:
66ac4b0e
GM
3730 if (s->for_overlaps_p)
3731 s->background_filled_p = 1;
3732 else
3733 x_draw_glyph_string_background (s, 0);
06a2c219
GM
3734 x_draw_glyph_string_foreground (s);
3735 break;
3736
3737 default:
3738 abort ();
3739 }
3740
66ac4b0e 3741 if (!s->for_overlaps_p)
06a2c219 3742 {
66ac4b0e
GM
3743 /* Draw underline. */
3744 if (s->face->underline_p)
3745 {
3746 unsigned long dy, h;
06a2c219 3747
66ac4b0e
GM
3748 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
3749 h = 1;
3750 if (!XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &dy))
3751 dy = s->height - h;
06a2c219 3752
66ac4b0e
GM
3753 if (s->face->underline_defaulted_p)
3754 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3755 s->width, h);
3756 else
3757 {
3758 XGCValues xgcv;
3759 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3760 XSetForeground (s->display, s->gc, s->face->underline_color);
3761 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3762 s->width, h);
3763 XSetForeground (s->display, s->gc, xgcv.foreground);
3764 }
dc6f92b8 3765 }
07e34cb0 3766
66ac4b0e
GM
3767 /* Draw overline. */
3768 if (s->face->overline_p)
06a2c219 3769 {
66ac4b0e
GM
3770 unsigned long dy = 0, h = 1;
3771
3772 if (s->face->overline_color_defaulted_p)
3773 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3774 s->width, h);
3775 else
3776 {
3777 XGCValues xgcv;
3778 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3779 XSetForeground (s->display, s->gc, s->face->overline_color);
3780 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3781 s->width, h);
3782 XSetForeground (s->display, s->gc, xgcv.foreground);
3783 }
06a2c219 3784 }
06a2c219 3785
66ac4b0e
GM
3786 /* Draw strike-through. */
3787 if (s->face->strike_through_p)
06a2c219 3788 {
66ac4b0e
GM
3789 unsigned long h = 1;
3790 unsigned long dy = (s->height - h) / 2;
3791
3792 if (s->face->strike_through_color_defaulted_p)
3793 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3794 s->width, h);
3795 else
3796 {
3797 XGCValues xgcv;
3798 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3799 XSetForeground (s->display, s->gc, s->face->strike_through_color);
3800 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3801 s->width, h);
3802 XSetForeground (s->display, s->gc, xgcv.foreground);
3803 }
06a2c219 3804 }
06a2c219 3805
66ac4b0e
GM
3806 /* Draw relief. */
3807 if (s->face->box != FACE_NO_BOX)
3808 x_draw_glyph_string_box (s);
3809 }
06a2c219
GM
3810
3811 /* Reset clipping. */
3812 XSetClipMask (s->display, s->gc, None);
dc6f92b8 3813}
07e34cb0 3814
06a2c219
GM
3815
3816/* A work-list entry used during the construction of glyph_string
3817 structures for a composite character. */
3818
3819struct work
3820{
3821 /* Pointer to composite char info defining has the composite
3822 character is drawn. */
3823 struct cmpchar_info *cmpcharp;
3824
3825 /* Start index in compcharp->glyph[]. */
3826 int gidx;
3827
3828 /* Next in stack. */
3829 struct work *next;
3830};
3831
3832
3833static void x_fill_composite_glyph_string P_ ((struct glyph_string *,
3834 int, struct work **,
66ac4b0e 3835 struct work **, int));
06a2c219
GM
3836
3837
3838/* Load glyph string S with information from the top of *STACK for a
3839 composite character. FACE_ID is the id of the face in which S is
3840 drawn. *NEW is a pointer to a struct work not on the stack, that
3841 can be used if this function needs to push a new structure on the
66ac4b0e
GM
3842 stack. If it uses it, *NEW is set to null. OVERLAPS_P non-zero
3843 means S should draw the foreground only, and use its lines physical
3844 height for clipping. */
07e34cb0
JB
3845
3846static void
66ac4b0e 3847x_fill_composite_glyph_string (s, face_id, stack, new, overlaps_p)
06a2c219
GM
3848 struct glyph_string *s;
3849 int face_id;
3850 struct work **stack, **new;
66ac4b0e 3851 int overlaps_p;
07e34cb0 3852{
06a2c219
GM
3853 int i, c;
3854 struct work *work;
3855
3856 xassert (s && *new && *stack);
3857
66ac4b0e
GM
3858 s->for_overlaps_p = 1;
3859
06a2c219
GM
3860 /* Pop the work stack. */
3861 work = *stack;
3862 *stack = work->next;
3863
3864 /* For all glyphs of cmpcharp->glyph, starting at the offset
3865 work->offset, until we reach the end of the definition or
3866 encounter another composite char, get the font and face to use,
3867 and add it to S. */
3868 for (i = work->gidx; i < work->cmpcharp->glyph_len; ++i)
3869 {
3870 c = FAST_GLYPH_CHAR (work->cmpcharp->glyph[i]);
3871 if (CHAR_CHARSET (c) == CHARSET_COMPOSITION)
3872 break;
3873 s->face = x_get_char_face_and_encoding (s->f, c, face_id,
3874 s->char2b + s->nchars, 1);
3875 s->font = s->face->font;
3876 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
3877 ++s->nchars;
3878 }
3879
3880 /* If we find another composite char in the glyph definition of
3881 work->cmpcharp, put back the rest of the glyphs on the work
3882 stack, and make a new entry for the composite char. */
3883 if (i < work->cmpcharp->glyph_len)
3884 {
3885 /* Push back an unprocessed rest of this glyph spec. */
3886 if (i < work->cmpcharp->glyph_len - 1)
3887 {
3888 work->gidx = i + 1;
3889 work->next = *stack;
3890 *stack = work;
3891 work = *new;
3892 *new = NULL;
3893 }
3894
3895 /* Make an entry for the composite char on the work stack. */
3896 work->cmpcharp = cmpchar_table[COMPOSITE_CHAR_ID (c)];
3897 work->gidx = 0;
3898 work->next = *stack;
3899 *stack = work;
3900 }
3901
3902 /* The width of this glyph string equals the width of the first
3903 glyph. All characters are drawn at the same x-position. */
3904 s->width = s->first_glyph->pixel_width;
3905
3906 /* If the specified font could not be loaded, use the frame's
3907 default font, but record the fact that we couldn't load it in
3908 the glyph string so that we can draw rectangles for the
3909 characters of the glyph string. */
3910 if (s->font == NULL)
3911 {
3912 s->font_not_found_p = 1;
3913 s->font = FRAME_FONT (s->f);
3914 }
3915
3916 /* Adjust base line for subscript/superscript text. */
3917 s->ybase += s->first_glyph->voffset;
3918
3919 xassert (s->face && s->face->gc);
3920
3921 /* This glyph string must always be drawn with 16-bit functions. */
3922 s->two_byte_p = 1;
3923}
3924
3925
3926/* Load glyph string S with a sequence of non-composite characters.
3927 FACE_ID is the face id of the string. START is the index of the
66ac4b0e
GM
3928 first glyph to consider, END is the index of the last + 1.
3929 OVERLAPS_P non-zero means S should draw the foreground only, and
3930 use its lines physical height for clipping.
3931
3932 Value is the index of the first glyph not in S. */
06a2c219
GM
3933
3934static int
66ac4b0e 3935x_fill_glyph_string (s, face_id, start, end, overlaps_p)
06a2c219
GM
3936 struct glyph_string *s;
3937 int face_id;
66ac4b0e 3938 int start, end, overlaps_p;
06a2c219
GM
3939{
3940 struct glyph *glyph, *last;
3941 int voffset;
3942
3943 xassert (s->charset != CHARSET_COMPOSITION);
3944 xassert (s->f == XFRAME (s->w->frame));
3945 xassert (s->nchars == 0);
3946 xassert (start >= 0 && end > start);
3947
66ac4b0e 3948 s->for_overlaps_p = overlaps_p,
06a2c219
GM
3949 glyph = s->row->glyphs[s->area] + start;
3950 last = s->row->glyphs[s->area] + end;
3951 voffset = glyph->voffset;
3952
3953 while (glyph < last
3954 && glyph->type == CHAR_GLYPH
3955 && glyph->voffset == voffset
3956 /* Same face id implies same charset, nowadays. */
3957 && glyph->u.ch.face_id == face_id)
3958 {
3959 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
3960 s->char2b + s->nchars);
3961 if (s->char2b[s->nchars].byte2 != 0)
3962 s->two_byte_p = 1;
3963
3964 ++s->nchars;
3965 xassert (s->nchars <= end - start);
3966 s->width += glyph->pixel_width;
3967 ++glyph;
3968 }
3969
3970 s->font = s->face->font;
3971 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
3972
3973 /* If the specified font could not be loaded, use the frame's font,
3974 but record the fact that we couldn't load it in
3975 S->font_not_found_p so that we can draw rectangles for the
3976 characters of the glyph string. */
3977 if (s->font == NULL)
3978 {
3979 s->font_not_found_p = 1;
3980 s->font = FRAME_FONT (s->f);
3981 }
3982
3983 /* Adjust base line for subscript/superscript text. */
3984 s->ybase += voffset;
66ac4b0e 3985
06a2c219
GM
3986 xassert (s->face && s->face->gc);
3987 return glyph - s->row->glyphs[s->area];
07e34cb0 3988}
dc6f92b8 3989
06a2c219
GM
3990
3991/* Fill glyph string S from image glyph S->first_glyph. */
dc6f92b8 3992
dfcf069d 3993static void
06a2c219
GM
3994x_fill_image_glyph_string (s)
3995 struct glyph_string *s;
3996{
3997 xassert (s->first_glyph->type == IMAGE_GLYPH);
3998 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img.id);
3999 xassert (s->img);
4000 s->face = FACE_FROM_ID (s->f, s->first_glyph->u.img.face_id);
4001 s->font = s->face->font;
4002 s->width = s->first_glyph->pixel_width;
4003
4004 /* Adjust base line for subscript/superscript text. */
4005 s->ybase += s->first_glyph->voffset;
4006}
4007
4008
4009/* Fill glyph string S from stretch glyph S->first_glyph. */
4010
4011static void
4012x_fill_stretch_glyph_string (s)
4013 struct glyph_string *s;
4014{
4015 xassert (s->first_glyph->type == STRETCH_GLYPH);
4016 s->face = FACE_FROM_ID (s->f, s->first_glyph->u.stretch.face_id);
4017 s->font = s->face->font;
4018 s->width = s->first_glyph->pixel_width;
4019
4020 /* Adjust base line for subscript/superscript text. */
4021 s->ybase += s->first_glyph->voffset;
4022}
4023
4024
4025/* Initialize glyph string S. CHAR2B is a suitably allocated vector
4026 of XChar2b structures for S; it can't be allocated in
4027 x_init_glyph_string because it must be allocated via `alloca'. W
4028 is the window on which S is drawn. ROW and AREA are the glyph row
4029 and area within the row from which S is constructed. START is the
4030 index of the first glyph structure covered by S. HL is a
4031 face-override for drawing S. */
4032
4033static void
4034x_init_glyph_string (s, char2b, w, row, area, start, hl)
4035 struct glyph_string *s;
4036 XChar2b *char2b;
4037 struct window *w;
4038 struct glyph_row *row;
4039 enum glyph_row_area area;
4040 int start;
4041 enum draw_glyphs_face hl;
4042{
4043 bzero (s, sizeof *s);
4044 s->w = w;
4045 s->f = XFRAME (w->frame);
4046 s->display = FRAME_X_DISPLAY (s->f);
4047 s->window = FRAME_X_WINDOW (s->f);
4048 s->char2b = char2b;
4049 s->hl = hl;
4050 s->row = row;
4051 s->area = area;
4052 s->first_glyph = row->glyphs[area] + start;
4053 s->height = row->height;
4054 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4055
4056 /* Display the internal border below the toolbar window. */
4057 if (s->w == XWINDOW (s->f->toolbar_window))
4058 s->y -= s->f->output_data.x->internal_border_width;
4059
4060 s->ybase = s->y + row->ascent;
4061}
4062
4063
4064/* Set background width of glyph string S. START is the index of the
4065 first glyph following S. LAST_X is the right-most x-position + 1
4066 in the drawing area. */
4067
4068static INLINE void
4069x_set_glyph_string_background_width (s, start, last_x)
4070 struct glyph_string *s;
4071 int start;
4072 int last_x;
4073{
4074 /* If the face of this glyph string has to be drawn to the end of
4075 the drawing area, set S->extends_to_end_of_line_p. */
4076 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4077
4078 if (start == s->row->used[s->area]
4079 && s->hl == DRAW_NORMAL_TEXT
4080 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4081 || s->face->background != default_face->background
4082 || s->face->stipple != default_face->stipple))
4083 s->extends_to_end_of_line_p = 1;
4084
4085 /* If S extends its face to the end of the line, set its
4086 background_width to the distance to the right edge of the drawing
4087 area. */
4088 if (s->extends_to_end_of_line_p)
1da3fd71 4089 s->background_width = last_x - s->x + 1;
06a2c219
GM
4090 else
4091 s->background_width = s->width;
4092}
4093
4094
4095/* Add a glyph string for a stretch glyph to the list of strings
4096 between HEAD and TAIL. START is the index of the stretch glyph in
4097 row area AREA of glyph row ROW. END is the index of the last glyph
4098 in that glyph row area. X is the current output position assigned
4099 to the new glyph string constructed. HL overrides that face of the
4100 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4101 is the right-most x-position of the drawing area. */
4102
4103#define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, \
4104 HL, X, LAST_X) \
4105 do \
4106 { \
4107 s = (struct glyph_string *) alloca (sizeof *s); \
4108 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4109 x_fill_stretch_glyph_string (s); \
4110 x_append_glyph_string (&HEAD, &TAIL, s); \
4111 ++START; \
4112 s->x = (X); \
4113 } \
4114 while (0)
4115
4116
4117/* Add a glyph string for an image glyph to the list of strings
4118 between HEAD and TAIL. START is the index of the image glyph in
4119 row area AREA of glyph row ROW. END is the index of the last glyph
4120 in that glyph row area. X is the current output position assigned
4121 to the new glyph string constructed. HL overrides that face of the
4122 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4123 is the right-most x-position of the drawing area. */
4124
4125#define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, \
4126 HL, X, LAST_X) \
4127 do \
4128 { \
4129 s = (struct glyph_string *) alloca (sizeof *s); \
4130 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4131 x_fill_image_glyph_string (s); \
4132 x_append_glyph_string (&HEAD, &TAIL, s); \
4133 ++START; \
4134 s->x = (X); \
4135 } \
4136 while (0)
4137
4138
4139/* Add a glyph string for a sequence of character glyphs to the list
4140 of strings between HEAD and TAIL. START is the index of the first
4141 glyph in row area AREA of glyph row ROW that is part of the new
4142 glyph string. END is the index of the last glyph in that glyph row
4143 area. X is the current output position assigned to the new glyph
4144 string constructed. HL overrides that face of the glyph; e.g. it
4145 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4146 right-most x-position of the drawing area. */
4147
4148#define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, \
66ac4b0e 4149 X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4150 do \
4151 { \
4152 int c, charset, face_id; \
4153 XChar2b *char2b; \
4154 \
4155 c = (ROW)->glyphs[AREA][START].u.ch.code; \
4156 charset = CHAR_CHARSET (c); \
4157 face_id = (ROW)->glyphs[AREA][START].u.ch.face_id; \
4158 \
4159 if (charset == CHARSET_COMPOSITION) \
4160 { \
4161 struct work *stack, *work, *new = NULL; \
4162 int n = 0; \
4163 struct glyph_string *first_s = NULL; \
4164 \
4165 /* Push an initial entry for character c on the stack. */ \
4166 stack = NULL; \
4167 work = (struct work *) alloca (sizeof *work); \
4168 work->cmpcharp = cmpchar_table[COMPOSITE_CHAR_ID (c)]; \
4169 work->gidx = 0; \
4170 work->next = stack; \
4171 stack = work; \
4172 \
4173 /* While the stack is not empty, append glyph_strings \
4174 to head/tail for glyphs to draw. */ \
4175 while (stack) \
4176 { \
4177 s = (struct glyph_string *) alloca (sizeof *s); \
4178 char2b = (XChar2b *) alloca (stack->cmpcharp->glyph_len \
4179 * sizeof (XChar2b)); \
4180 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4181 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4182 s->cmpcharp = stack->cmpcharp; \
4183 s->gidx = stack->gidx; \
4184 s->charset = charset; \
4185 s->x = (X); \
4186 \
4187 if (n == 0) \
4188 { \
4189 /* Don't draw the background except for the \
4190 first glyph string. */ \
4191 s->background_filled_p = n > 0; \
4192 first_s = s; \
4193 } \
4194 ++n; \
4195 \
4196 if (new == NULL) \
4197 new = (struct work *) alloca (sizeof *new); \
4198 x_fill_composite_glyph_string (s, face_id, &stack, \
66ac4b0e 4199 &new, OVERLAPS_P); \
06a2c219
GM
4200 } \
4201 \
4202 ++START; \
4203 s = first_s; \
4204 } \
4205 else \
4206 { \
4207 s = (struct glyph_string *) alloca (sizeof *s); \
4208 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4209 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4210 x_append_glyph_string (&HEAD, &TAIL, s); \
4211 s->charset = charset; \
4212 s->x = (X); \
66ac4b0e
GM
4213 START = x_fill_glyph_string (s, face_id, START, END, \
4214 OVERLAPS_P); \
06a2c219
GM
4215 } \
4216 } \
4217 while (0)
4218
4219
4220/* Build a list of glyph strings between HEAD and TAIL for the glyphs
4221 of AREA of glyph row ROW on window W between indices START and END.
4222 HL overrides the face for drawing glyph strings, e.g. it is
4223 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4224 x-positions of the drawing area.
4225
4226 This is an ugly monster macro construct because we must use alloca
4227 to allocate glyph strings (because x_draw_glyphs can be called
4228 asynchronously). */
4229
4230#define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, \
66ac4b0e 4231 X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4232 do \
4233 { \
4234 HEAD = TAIL = NULL; \
4235 while (START < END) \
4236 { \
4237 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4238 switch (first_glyph->type) \
4239 { \
4240 case CHAR_GLYPH: \
4241 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
66ac4b0e
GM
4242 TAIL, HL, X, LAST_X, \
4243 OVERLAPS_P); \
06a2c219
GM
4244 break; \
4245 \
4246 case STRETCH_GLYPH: \
4247 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4248 HEAD, TAIL, HL, X, LAST_X); \
4249 break; \
4250 \
4251 case IMAGE_GLYPH: \
4252 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4253 TAIL, HL, X, LAST_X); \
4254 break; \
4255 \
4256 default: \
4257 abort (); \
4258 } \
4259 \
4260 x_set_glyph_string_background_width (s, START, LAST_X); \
4261 (X) += s->width; \
4262 } \
4263 } \
4264 while (0)
4265
4266
4267/* Draw glyphs between START and END in AREA of ROW on window W,
4268 starting at x-position X. X is relative to AREA in W. HL is a
4269 face-override with the following meaning:
4270
4271 DRAW_NORMAL_TEXT draw normally
4272 DRAW_CURSOR draw in cursor face
4273 DRAW_MOUSE_FACE draw in mouse face.
4274 DRAW_INVERSE_VIDEO draw in mode line face
4275 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4276 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4277
4278 If REAL_START is non-null, return in *REAL_START the real starting
4279 position for display. This can be different from START in case
4280 overlapping glyphs must be displayed. If REAL_END is non-null,
4281 return in *REAL_END the real end position for display. This can be
4282 different from END in case overlapping glyphs must be displayed.
4283
66ac4b0e
GM
4284 If OVERLAPS_P is non-zero, draw only the foreground of characters
4285 and clip to the physical height of ROW.
4286
06a2c219
GM
4287 Value is the x-position reached, relative to AREA of W. */
4288
4289static int
66ac4b0e
GM
4290x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4291 overlaps_p)
06a2c219
GM
4292 struct window *w;
4293 int x;
4294 struct glyph_row *row;
4295 enum glyph_row_area area;
4296 int start, end;
4297 enum draw_glyphs_face hl;
4298 int *real_start, *real_end;
66ac4b0e 4299 int overlaps_p;
dc6f92b8 4300{
06a2c219
GM
4301 struct glyph_string *head, *tail;
4302 struct glyph_string *s;
4303 int last_x, area_width;
4304 int x_reached;
4305 int i, j;
4306
4307 /* Let's rather be paranoid than getting a SEGV. */
4308 start = max (0, start);
4309 end = min (end, row->used[area]);
4310 if (real_start)
4311 *real_start = start;
4312 if (real_end)
4313 *real_end = end;
4314
4315 /* Translate X to frame coordinates. Set last_x to the right
4316 end of the drawing area. */
4317 if (row->full_width_p)
4318 {
4319 /* X is relative to the left edge of W, without scroll bars
4320 or flag areas. */
4321 struct frame *f = XFRAME (w->frame);
4322 int width = FRAME_FLAGS_AREA_WIDTH (f);
4323 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
dc6f92b8 4324
06a2c219
GM
4325 x += window_left_x;
4326 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4327 last_x = window_left_x + area_width;
4328
4329 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4330 {
4331 width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4332 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4333 last_x += width;
4334 else
4335 x -= width;
4336 }
dc6f92b8 4337
b9432a85
GM
4338 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4339 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
06a2c219
GM
4340 }
4341 else
dc6f92b8 4342 {
06a2c219
GM
4343 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4344 area_width = window_box_width (w, area);
4345 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
dc6f92b8
JB
4346 }
4347
06a2c219
GM
4348 /* Build a doubly-linked list of glyph_string structures between
4349 head and tail from what we have to draw. Note that the macro
4350 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4351 the reason we use a separate variable `i'. */
4352 i = start;
66ac4b0e
GM
4353 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4354 overlaps_p);
06a2c219
GM
4355 if (tail)
4356 x_reached = tail->x + tail->background_width;
4357 else
4358 x_reached = x;
90e65f07 4359
06a2c219
GM
4360 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4361 the row, redraw some glyphs in front or following the glyph
4362 strings built above. */
66ac4b0e 4363 if (!overlaps_p && row->contains_overlapping_glyphs_p)
06a2c219
GM
4364 {
4365 int dummy_x = 0;
4366 struct glyph_string *h, *t;
4367
4368 /* Compute overhangs for all glyph strings. */
4369 for (s = head; s; s = s->next)
4370 x_compute_glyph_string_overhangs (s);
4371
4372 /* Prepend glyph strings for glyphs in front of the first glyph
4373 string that are overwritten because of the first glyph
4374 string's left overhang. The background of all strings
4375 prepended must be drawn because the first glyph string
4376 draws over it. */
4377 i = x_left_overwritten (head);
4378 if (i >= 0)
4379 {
4380 j = i;
4381 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
66ac4b0e
GM
4382 DRAW_NORMAL_TEXT, dummy_x, last_x,
4383 overlaps_p);
06a2c219
GM
4384 start = i;
4385 if (real_start)
4386 *real_start = start;
4387 x_compute_overhangs_and_x (t, head->x, 1);
4388 x_prepend_glyph_string_lists (&head, &tail, h, t);
4389 }
58769bee 4390
06a2c219
GM
4391 /* Prepend glyph strings for glyphs in front of the first glyph
4392 string that overwrite that glyph string because of their
4393 right overhang. For these strings, only the foreground must
4394 be drawn, because it draws over the glyph string at `head'.
4395 The background must not be drawn because this would overwrite
4396 right overhangs of preceding glyphs for which no glyph
4397 strings exist. */
4398 i = x_left_overwriting (head);
4399 if (i >= 0)
4400 {
4401 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
66ac4b0e
GM
4402 DRAW_NORMAL_TEXT, dummy_x, last_x,
4403 overlaps_p);
06a2c219
GM
4404 for (s = h; s; s = s->next)
4405 s->background_filled_p = 1;
4406 if (real_start)
4407 *real_start = i;
4408 x_compute_overhangs_and_x (t, head->x, 1);
4409 x_prepend_glyph_string_lists (&head, &tail, h, t);
4410 }
dbcb258a 4411
06a2c219
GM
4412 /* Append glyphs strings for glyphs following the last glyph
4413 string tail that are overwritten by tail. The background of
4414 these strings has to be drawn because tail's foreground draws
4415 over it. */
4416 i = x_right_overwritten (tail);
4417 if (i >= 0)
4418 {
4419 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4420 DRAW_NORMAL_TEXT, x, last_x,
4421 overlaps_p);
06a2c219
GM
4422 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4423 x_append_glyph_string_lists (&head, &tail, h, t);
4424 if (real_end)
4425 *real_end = i;
4426 }
dc6f92b8 4427
06a2c219
GM
4428 /* Append glyph strings for glyphs following the last glyph
4429 string tail that overwrite tail. The foreground of such
4430 glyphs has to be drawn because it writes into the background
4431 of tail. The background must not be drawn because it could
4432 paint over the foreground of following glyphs. */
4433 i = x_right_overwriting (tail);
4434 if (i >= 0)
4435 {
4436 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4437 DRAW_NORMAL_TEXT, x, last_x,
4438 overlaps_p);
06a2c219
GM
4439 for (s = h; s; s = s->next)
4440 s->background_filled_p = 1;
4441 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4442 x_append_glyph_string_lists (&head, &tail, h, t);
4443 if (real_end)
4444 *real_end = i;
4445 }
4446 }
58769bee 4447
06a2c219
GM
4448 /* Draw all strings. */
4449 for (s = head; s; s = s->next)
4450 x_draw_glyph_string (s);
dc6f92b8 4451
06a2c219
GM
4452 /* Value is the x-position up to which drawn, relative to AREA of W.
4453 This doesn't include parts drawn because of overhangs. */
4454 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4455 if (!row->full_width_p)
4456 {
4457 if (area > LEFT_MARGIN_AREA)
4458 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4459 if (area > TEXT_AREA)
4460 x_reached -= window_box_width (w, TEXT_AREA);
4461 }
4462 return x_reached;
4463}
dc6f92b8 4464
dc6f92b8 4465
66ac4b0e
GM
4466/* Fix the display of area AREA of overlapping row ROW in window W. */
4467
4468static void
4469x_fix_overlapping_area (w, row, area)
4470 struct window *w;
4471 struct glyph_row *row;
4472 enum glyph_row_area area;
4473{
4474 int i, x;
4475
4476 BLOCK_INPUT;
4477
4478 if (area == LEFT_MARGIN_AREA)
4479 x = 0;
4480 else if (area == TEXT_AREA)
4481 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
4482 else
4483 x = (window_box_width (w, LEFT_MARGIN_AREA)
4484 + window_box_width (w, TEXT_AREA));
4485
4486 for (i = 0; i < row->used[area];)
4487 {
4488 if (row->glyphs[area][i].overlaps_vertically_p)
4489 {
4490 int start = i, start_x = x;
4491
4492 do
4493 {
4494 x += row->glyphs[area][i].pixel_width;
4495 ++i;
4496 }
4497 while (i < row->used[area]
4498 && row->glyphs[area][i].overlaps_vertically_p);
4499
4500 x_draw_glyphs (w, start_x, row, area, start, i,
4501 (row->inverse_p
4502 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4503 NULL, NULL, 1);
4504 }
4505 else
4506 {
4507 x += row->glyphs[area][i].pixel_width;
4508 ++i;
4509 }
4510 }
4511
4512 UNBLOCK_INPUT;
4513}
4514
4515
06a2c219
GM
4516/* Output LEN glyphs starting at START at the nominal cursor position.
4517 Advance the nominal cursor over the text. The global variable
4518 updated_window contains the window being updated, updated_row is
4519 the glyph row being updated, and updated_area is the area of that
4520 row being updated. */
dc6f92b8 4521
06a2c219
GM
4522static void
4523x_write_glyphs (start, len)
4524 struct glyph *start;
4525 int len;
4526{
4527 int x, hpos, real_start, real_end;
d9cdbb3d 4528
06a2c219 4529 xassert (updated_window && updated_row);
dc6f92b8 4530 BLOCK_INPUT;
06a2c219
GM
4531
4532 /* Write glyphs. */
dc6f92b8 4533
06a2c219
GM
4534 hpos = start - updated_row->glyphs[updated_area];
4535 x = x_draw_glyphs (updated_window, output_cursor.x,
4536 updated_row, updated_area,
4537 hpos, hpos + len,
4538 (updated_row->inverse_p
4539 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
66ac4b0e 4540 &real_start, &real_end, 0);
b30ec466 4541
06a2c219
GM
4542 /* If we drew over the cursor, note that it is not visible any more. */
4543 note_overwritten_text_cursor (updated_window, real_start,
4544 real_end - real_start);
dc6f92b8
JB
4545
4546 UNBLOCK_INPUT;
06a2c219
GM
4547
4548 /* Advance the output cursor. */
4549 output_cursor.hpos += len;
4550 output_cursor.x = x;
dc6f92b8
JB
4551}
4552
0cdd0c9f 4553
06a2c219 4554/* Insert LEN glyphs from START at the nominal cursor position. */
0cdd0c9f 4555
06a2c219
GM
4556static void
4557x_insert_glyphs (start, len)
4558 struct glyph *start;
4559 register int len;
4560{
4561 struct frame *f;
4562 struct window *w;
4563 int line_height, shift_by_width, shifted_region_width;
4564 struct glyph_row *row;
4565 struct glyph *glyph;
4566 int frame_x, frame_y, hpos, real_start, real_end;
58769bee 4567
06a2c219 4568 xassert (updated_window && updated_row);
0cdd0c9f 4569 BLOCK_INPUT;
06a2c219
GM
4570 w = updated_window;
4571 f = XFRAME (WINDOW_FRAME (w));
4572
4573 /* Get the height of the line we are in. */
4574 row = updated_row;
4575 line_height = row->height;
4576
4577 /* Get the width of the glyphs to insert. */
4578 shift_by_width = 0;
4579 for (glyph = start; glyph < start + len; ++glyph)
4580 shift_by_width += glyph->pixel_width;
4581
4582 /* Get the width of the region to shift right. */
4583 shifted_region_width = (window_box_width (w, updated_area)
4584 - output_cursor.x
4585 - shift_by_width);
4586
4587 /* Shift right. */
4588 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
4589 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
4590 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
4591 f->output_data.x->normal_gc,
4592 frame_x, frame_y,
4593 shifted_region_width, line_height,
4594 frame_x + shift_by_width, frame_y);
4595
4596 /* Write the glyphs. */
4597 hpos = start - row->glyphs[updated_area];
4598 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
66ac4b0e 4599 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
06a2c219
GM
4600 note_overwritten_text_cursor (w, real_start, real_end - real_start);
4601
4602 /* Advance the output cursor. */
4603 output_cursor.hpos += len;
4604 output_cursor.x += shift_by_width;
0cdd0c9f
RS
4605 UNBLOCK_INPUT;
4606}
0cdd0c9f 4607
0cdd0c9f 4608
06a2c219
GM
4609/* Delete N glyphs at the nominal cursor position. Not implemented
4610 for X frames. */
c83febd7
RS
4611
4612static void
06a2c219
GM
4613x_delete_glyphs (n)
4614 register int n;
c83febd7 4615{
06a2c219 4616 abort ();
c83febd7
RS
4617}
4618
0cdd0c9f 4619
06a2c219
GM
4620/* Erase the current text line from the nominal cursor position
4621 (inclusive) to pixel column TO_X (exclusive). The idea is that
4622 everything from TO_X onward is already erased.
4623
4624 TO_X is a pixel position relative to updated_area of
4625 updated_window. TO_X == -1 means clear to the end of this area. */
dc6f92b8 4626
06a2c219
GM
4627static void
4628x_clear_end_of_line (to_x)
4629 int to_x;
4630{
4631 struct frame *f;
4632 struct window *w = updated_window;
4633 int max_x, min_y, max_y;
4634 int from_x, from_y, to_y;
4635
4636 xassert (updated_window && updated_row);
4637 f = XFRAME (w->frame);
4638
4639 if (updated_row->full_width_p)
4640 {
4641 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
4642 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
4643 && !w->pseudo_window_p)
4644 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
0cdd0c9f 4645 }
06a2c219
GM
4646 else
4647 max_x = window_box_width (w, updated_area);
4648 max_y = window_text_bottom_y (w);
dc6f92b8 4649
06a2c219
GM
4650 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
4651 of window. For TO_X > 0, truncate to end of drawing area. */
4652 if (to_x == 0)
4653 return;
4654 else if (to_x < 0)
4655 to_x = max_x;
4656 else
4657 to_x = min (to_x, max_x);
dbc4e1c1 4658
06a2c219
GM
4659 to_y = min (max_y, output_cursor.y + updated_row->height);
4660
4661 /* Notice if the cursor will be cleared by this operation. */
4662 if (!updated_row->full_width_p)
4663 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
dbc4e1c1 4664
06a2c219
GM
4665 from_x = output_cursor.x;
4666
4667 /* Translate to frame coordinates. */
4668 if (updated_row->full_width_p)
4669 {
4670 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
4671 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
4672 }
0cdd0c9f
RS
4673 else
4674 {
06a2c219
GM
4675 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
4676 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
4677 }
4678
4679 min_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
4680 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
4681 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
4682
4683 /* Prevent inadvertently clearing to end of the X window. */
4684 if (to_x > from_x && to_y > from_y)
4685 {
4686 BLOCK_INPUT;
4687 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4688 from_x, from_y, to_x - from_x, to_y - from_y,
4689 False);
4690 UNBLOCK_INPUT;
0cdd0c9f 4691 }
0cdd0c9f 4692}
dbc4e1c1 4693
0cdd0c9f 4694
06a2c219
GM
4695/* Clear entire frame. If updating_frame is non-null, clear that
4696 frame. Otherwise clear selected_frame. */
4697
4698static void
4699x_clear_frame ()
0cdd0c9f 4700{
06a2c219 4701 struct frame *f;
0cdd0c9f 4702
06a2c219
GM
4703 if (updating_frame)
4704 f = updating_frame;
0cdd0c9f 4705 else
06a2c219 4706 f = selected_frame;
58769bee 4707
06a2c219
GM
4708 /* Clearing the frame will erase any cursor, so mark them all as no
4709 longer visible. */
4710 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
4711 output_cursor.hpos = output_cursor.vpos = 0;
4712 output_cursor.x = -1;
4713
4714 /* We don't set the output cursor here because there will always
4715 follow an explicit cursor_to. */
4716 BLOCK_INPUT;
4717 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
4718
4719 /* We have to clear the scroll bars, too. If we have changed
4720 colors or something like that, then they should be notified. */
4721 x_scroll_bar_clear (f);
0cdd0c9f 4722
06a2c219
GM
4723 XFlush (FRAME_X_DISPLAY (f));
4724 UNBLOCK_INPUT;
dc6f92b8 4725}
06a2c219
GM
4726
4727
dc6f92b8 4728\f
dbc4e1c1
JB
4729/* Invert the middle quarter of the frame for .15 sec. */
4730
06a2c219
GM
4731/* We use the select system call to do the waiting, so we have to make
4732 sure it's available. If it isn't, we just won't do visual bells. */
4733
dbc4e1c1
JB
4734#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
4735
06a2c219
GM
4736
4737/* Subtract the `struct timeval' values X and Y, storing the result in
4738 *RESULT. Return 1 if the difference is negative, otherwise 0. */
dbc4e1c1
JB
4739
4740static int
4741timeval_subtract (result, x, y)
4742 struct timeval *result, x, y;
4743{
06a2c219
GM
4744 /* Perform the carry for the later subtraction by updating y. This
4745 is safer because on some systems the tv_sec member is unsigned. */
dbc4e1c1
JB
4746 if (x.tv_usec < y.tv_usec)
4747 {
4748 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
4749 y.tv_usec -= 1000000 * nsec;
4750 y.tv_sec += nsec;
4751 }
06a2c219 4752
dbc4e1c1
JB
4753 if (x.tv_usec - y.tv_usec > 1000000)
4754 {
4755 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
4756 y.tv_usec += 1000000 * nsec;
4757 y.tv_sec -= nsec;
4758 }
4759
06a2c219
GM
4760 /* Compute the time remaining to wait. tv_usec is certainly
4761 positive. */
dbc4e1c1
JB
4762 result->tv_sec = x.tv_sec - y.tv_sec;
4763 result->tv_usec = x.tv_usec - y.tv_usec;
4764
06a2c219
GM
4765 /* Return indication of whether the result should be considered
4766 negative. */
dbc4e1c1
JB
4767 return x.tv_sec < y.tv_sec;
4768}
dc6f92b8 4769
dfcf069d 4770void
f676886a
JB
4771XTflash (f)
4772 struct frame *f;
dc6f92b8 4773{
dbc4e1c1 4774 BLOCK_INPUT;
dc6f92b8 4775
dbc4e1c1
JB
4776 {
4777 GC gc;
dc6f92b8 4778
06a2c219
GM
4779 /* Create a GC that will use the GXxor function to flip foreground
4780 pixels into background pixels. */
dbc4e1c1
JB
4781 {
4782 XGCValues values;
dc6f92b8 4783
dbc4e1c1 4784 values.function = GXxor;
7556890b
RS
4785 values.foreground = (f->output_data.x->foreground_pixel
4786 ^ f->output_data.x->background_pixel);
58769bee 4787
334208b7 4788 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dbc4e1c1
JB
4789 GCFunction | GCForeground, &values);
4790 }
dc6f92b8 4791
dbc4e1c1 4792 {
e84e14c3
RS
4793 /* Get the height not including a menu bar widget. */
4794 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
4795 /* Height of each line to flash. */
4796 int flash_height = FRAME_LINE_HEIGHT (f);
4797 /* These will be the left and right margins of the rectangles. */
4798 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
4799 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
4800
4801 int width;
4802
4803 /* Don't flash the area between a scroll bar and the frame
4804 edge it is next to. */
4805 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
4806 {
4807 case vertical_scroll_bar_left:
4808 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
4809 break;
4810
4811 case vertical_scroll_bar_right:
4812 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
4813 break;
06a2c219
GM
4814
4815 default:
4816 break;
e84e14c3
RS
4817 }
4818
4819 width = flash_right - flash_left;
4820
4821 /* If window is tall, flash top and bottom line. */
4822 if (height > 3 * FRAME_LINE_HEIGHT (f))
4823 {
4824 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
4825 flash_left,
4826 (FRAME_INTERNAL_BORDER_WIDTH (f)
4827 + FRAME_TOOLBAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
4828 width, flash_height);
4829 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4830 flash_left,
4831 (height - flash_height
4832 - FRAME_INTERNAL_BORDER_WIDTH (f)),
4833 width, flash_height);
4834 }
4835 else
4836 /* If it is short, flash it all. */
4837 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4838 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
4839 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
dc6f92b8 4840
06a2c219 4841 x_flush (f);
dc6f92b8 4842
dbc4e1c1 4843 {
06a2c219 4844 struct timeval wakeup;
dc6f92b8 4845
66c30ea1 4846 EMACS_GET_TIME (wakeup);
dc6f92b8 4847
dbc4e1c1
JB
4848 /* Compute time to wait until, propagating carry from usecs. */
4849 wakeup.tv_usec += 150000;
4850 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
4851 wakeup.tv_usec %= 1000000;
4852
4853 /* Keep waiting until past the time wakeup. */
4854 while (1)
4855 {
4856 struct timeval timeout;
4857
66c30ea1 4858 EMACS_GET_TIME (timeout);
dbc4e1c1
JB
4859
4860 /* In effect, timeout = wakeup - timeout.
4861 Break if result would be negative. */
4862 if (timeval_subtract (&timeout, wakeup, timeout))
4863 break;
4864
4865 /* Try to wait that long--but we might wake up sooner. */
c32cdd9a 4866 select (0, NULL, NULL, NULL, &timeout);
dbc4e1c1
JB
4867 }
4868 }
58769bee 4869
e84e14c3
RS
4870 /* If window is tall, flash top and bottom line. */
4871 if (height > 3 * FRAME_LINE_HEIGHT (f))
4872 {
4873 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
4874 flash_left,
4875 (FRAME_INTERNAL_BORDER_WIDTH (f)
4876 + FRAME_TOOLBAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
4877 width, flash_height);
4878 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4879 flash_left,
4880 (height - flash_height
4881 - FRAME_INTERNAL_BORDER_WIDTH (f)),
4882 width, flash_height);
4883 }
4884 else
4885 /* If it is short, flash it all. */
4886 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4887 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
4888 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
4889
334208b7 4890 XFreeGC (FRAME_X_DISPLAY (f), gc);
06a2c219 4891 x_flush (f);
dc6f92b8 4892 }
dbc4e1c1
JB
4893 }
4894
4895 UNBLOCK_INPUT;
dc6f92b8
JB
4896}
4897
06a2c219 4898#endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
dbc4e1c1
JB
4899
4900
dc6f92b8
JB
4901/* Make audible bell. */
4902
334208b7 4903#define XRINGBELL XBell (FRAME_X_DISPLAY (selected_frame), 0)
dc6f92b8 4904
dfcf069d 4905void
dc6f92b8
JB
4906XTring_bell ()
4907{
334208b7 4908 if (FRAME_X_DISPLAY (selected_frame) == 0)
5a6ef893
RS
4909 return;
4910
dbc4e1c1 4911#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
dc6f92b8 4912 if (visible_bell)
f676886a 4913 XTflash (selected_frame);
dc6f92b8 4914 else
dbc4e1c1 4915#endif
dc6f92b8
JB
4916 {
4917 BLOCK_INPUT;
4918 XRINGBELL;
334208b7 4919 XFlush (FRAME_X_DISPLAY (selected_frame));
dc6f92b8
JB
4920 UNBLOCK_INPUT;
4921 }
4922}
06a2c219 4923
dc6f92b8 4924\f
06a2c219
GM
4925/* Specify how many text lines, from the top of the window,
4926 should be affected by insert-lines and delete-lines operations.
4927 This, and those operations, are used only within an update
4928 that is bounded by calls to x_update_begin and x_update_end. */
dc6f92b8 4929
dfcf069d 4930static void
06a2c219
GM
4931XTset_terminal_window (n)
4932 register int n;
dc6f92b8 4933{
06a2c219 4934 /* This function intentionally left blank. */
dc6f92b8
JB
4935}
4936
06a2c219
GM
4937
4938\f
4939/***********************************************************************
4940 Line Dance
4941 ***********************************************************************/
4942
4943/* Perform an insert-lines or delete-lines operation, inserting N
4944 lines or deleting -N lines at vertical position VPOS. */
4945
dfcf069d 4946static void
06a2c219
GM
4947x_ins_del_lines (vpos, n)
4948 int vpos, n;
dc6f92b8
JB
4949{
4950 abort ();
4951}
06a2c219
GM
4952
4953
4954/* Scroll part of the display as described by RUN. */
dc6f92b8 4955
dfcf069d 4956static void
06a2c219
GM
4957x_scroll_run (w, run)
4958 struct window *w;
4959 struct run *run;
dc6f92b8 4960{
06a2c219
GM
4961 struct frame *f = XFRAME (w->frame);
4962 int x, y, width, height, from_y, to_y, bottom_y;
4963
4964 /* Get frame-relative bounding box of the text display area of W,
4965 without mode lines. Include in this box the flags areas to the
4966 left and right of W. */
4967 window_box (w, -1, &x, &y, &width, &height);
4968 width += 2 * FRAME_X_FLAGS_AREA_WIDTH (f);
4969 x -= FRAME_X_FLAGS_AREA_WIDTH (f);
4970
4971 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
4972 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
4973 bottom_y = y + height;
dc6f92b8 4974
06a2c219
GM
4975 if (to_y < from_y)
4976 {
4977 /* Scrolling up. Make sure we don't copy part of the mode
4978 line at the bottom. */
4979 if (from_y + run->height > bottom_y)
4980 height = bottom_y - from_y;
4981 else
4982 height = run->height;
4983 }
dc6f92b8 4984 else
06a2c219
GM
4985 {
4986 /* Scolling down. Make sure we don't copy over the mode line.
4987 at the bottom. */
4988 if (to_y + run->height > bottom_y)
4989 height = bottom_y - to_y;
4990 else
4991 height = run->height;
4992 }
7a13e894 4993
06a2c219
GM
4994 BLOCK_INPUT;
4995
4996 /* Cursor off. Will be switched on again in x_update_window_end. */
4997 updated_window = w;
4998 x_clear_cursor (w);
4999
5000 XCopyArea (FRAME_X_DISPLAY (f),
5001 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5002 f->output_data.x->normal_gc,
5003 x, from_y,
5004 width, height,
5005 x, to_y);
5006
5007 UNBLOCK_INPUT;
5008}
dc6f92b8 5009
dc6f92b8 5010
06a2c219
GM
5011\f
5012/***********************************************************************
5013 Exposure Events
5014 ***********************************************************************/
5015
5016/* Redisplay an exposed area of frame F. X and Y are the upper-left
5017 corner of the exposed rectangle. W and H are width and height of
5018 the exposed area. All are pixel values. W or H zero means redraw
5019 the entire frame. */
dc6f92b8 5020
06a2c219
GM
5021static void
5022expose_frame (f, x, y, w, h)
5023 struct frame *f;
5024 int x, y, w, h;
dc6f92b8 5025{
06a2c219 5026 XRectangle r;
dc6f92b8 5027
06a2c219 5028 TRACE ((stderr, "expose_frame "));
dc6f92b8 5029
06a2c219
GM
5030 /* No need to redraw if frame will be redrawn soon. */
5031 if (FRAME_GARBAGED_P (f))
dc6f92b8 5032 {
06a2c219
GM
5033 TRACE ((stderr, " garbaged\n"));
5034 return;
5035 }
5036
5037 /* If basic faces haven't been realized yet, there is no point in
5038 trying to redraw anything. This can happen when we get an expose
5039 event while Emacs is starting, e.g. by moving another window. */
5040 if (FRAME_FACE_CACHE (f) == NULL
5041 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5042 {
5043 TRACE ((stderr, " no faces\n"));
5044 return;
58769bee 5045 }
06a2c219
GM
5046
5047 if (w == 0 || h == 0)
58769bee 5048 {
06a2c219
GM
5049 r.x = r.y = 0;
5050 r.width = CANON_X_UNIT (f) * f->width;
5051 r.height = CANON_Y_UNIT (f) * f->height;
dc6f92b8
JB
5052 }
5053 else
5054 {
06a2c219
GM
5055 r.x = x;
5056 r.y = y;
5057 r.width = w;
5058 r.height = h;
5059 }
5060
5061 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5062 expose_window_tree (XWINDOW (f->root_window), &r);
5063
5064 if (WINDOWP (f->toolbar_window))
5065 {
5066 struct window *w = XWINDOW (f->toolbar_window);
5067 XRectangle window_rect;
5068 XRectangle intersection_rect;
5069 int window_x, window_y, window_width, window_height;
5070
5071
5072 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5073 window_rect.x = window_x;
5074 window_rect.y = window_y;
5075 window_rect.width = window_width;
5076 window_rect.height = window_height;
5077
5078 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5079 expose_window (w, &intersection_rect);
5080 }
5081
5082#ifndef USE_X_TOOLKIT
5083 if (WINDOWP (f->menu_bar_window))
5084 {
5085 struct window *w = XWINDOW (f->menu_bar_window);
5086 XRectangle window_rect;
5087 XRectangle intersection_rect;
5088 int window_x, window_y, window_width, window_height;
5089
5090
5091 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5092 window_rect.x = window_x;
5093 window_rect.y = window_y;
5094 window_rect.width = window_width;
5095 window_rect.height = window_height;
5096
5097 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5098 expose_window (w, &intersection_rect);
dc6f92b8 5099 }
06a2c219 5100#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
5101}
5102
06a2c219
GM
5103
5104/* Redraw (parts) of all windows in the window tree rooted at W that
5105 intersect R. R contains frame pixel coordinates. */
5106
58769bee 5107static void
06a2c219
GM
5108expose_window_tree (w, r)
5109 struct window *w;
5110 XRectangle *r;
dc6f92b8 5111{
06a2c219
GM
5112 while (w)
5113 {
5114 if (!NILP (w->hchild))
5115 expose_window_tree (XWINDOW (w->hchild), r);
5116 else if (!NILP (w->vchild))
5117 expose_window_tree (XWINDOW (w->vchild), r);
5118 else
5119 {
5120 XRectangle window_rect;
5121 XRectangle intersection_rect;
5122 struct frame *f = XFRAME (w->frame);
5123 int window_x, window_y, window_width, window_height;
5124
5125 /* Frame-relative pixel rectangle of W. */
5126 window_box (w, -1, &window_x, &window_y, &window_width,
5127 &window_height);
5128 window_rect.x
5129 = (window_x
5130 - FRAME_X_FLAGS_AREA_WIDTH (f)
5131 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_Y_UNIT (f));
5132 window_rect.y = window_y;
5133 window_rect.width
5134 = (window_width
5135 + 2 * FRAME_X_FLAGS_AREA_WIDTH (f)
5136 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5137 window_rect.height
5138 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5139
5140 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5141 expose_window (w, &intersection_rect);
5142 }
58769bee 5143
06a2c219
GM
5144 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5145 }
5146}
58769bee 5147
dc6f92b8 5148
06a2c219
GM
5149/* Redraw the part of glyph row area AREA of glyph row ROW on window W
5150 which intersects rectangle R. R is in window-relative coordinates. */
5151
5152static void
5153expose_area (w, row, r, area)
5154 struct window *w;
5155 struct glyph_row *row;
5156 XRectangle *r;
5157 enum glyph_row_area area;
5158{
5159 int x;
5160 struct glyph *first = row->glyphs[area];
5161 struct glyph *end = row->glyphs[area] + row->used[area];
5162 struct glyph *last;
5163 int first_x;
5164
5165 /* Set x to the window-relative start position for drawing glyphs of
5166 AREA. The first glyph of the text area can be partially visible.
5167 The first glyphs of other areas cannot. */
5168 if (area == LEFT_MARGIN_AREA)
5169 x = 0;
5170 else if (area == TEXT_AREA)
5171 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5172 else
5173 x = (window_box_width (w, LEFT_MARGIN_AREA)
5174 + window_box_width (w, TEXT_AREA));
5175
5176 /* Find the first glyph that must be redrawn. */
5177 while (first < end
5178 && x + first->pixel_width < r->x)
5179 {
5180 x += first->pixel_width;
5181 ++first;
5182 }
5183
5184 /* Find the last one. */
5185 last = first;
5186 first_x = x;
5187 while (last < end
5188 && x < r->x + r->width)
5189 {
5190 x += last->pixel_width;
5191 ++last;
5192 }
5193
5194 /* Repaint. */
5195 if (last > first)
5196 x_draw_glyphs (w, first_x, row, area,
5197 first - row->glyphs[area],
5198 last - row->glyphs[area],
5199 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5200 NULL, NULL, 0);
06a2c219
GM
5201}
5202
58769bee 5203
06a2c219
GM
5204/* Redraw the parts of the glyph row ROW on window W intersecting
5205 rectangle R. R is in window-relative coordinates. */
dc6f92b8 5206
06a2c219
GM
5207static void
5208expose_line (w, row, r)
5209 struct window *w;
5210 struct glyph_row *row;
5211 XRectangle *r;
5212{
5213 xassert (row->enabled_p);
5214
5215 if (row->mode_line_p || w->pseudo_window_p)
5216 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5217 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5218 NULL, NULL, 0);
06a2c219
GM
5219 else
5220 {
5221 if (row->used[LEFT_MARGIN_AREA])
5222 expose_area (w, row, r, LEFT_MARGIN_AREA);
5223 if (row->used[TEXT_AREA])
5224 expose_area (w, row, r, TEXT_AREA);
5225 if (row->used[RIGHT_MARGIN_AREA])
5226 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5227 x_draw_row_bitmaps (w, row);
5228 }
5229}
dc6f92b8 5230
58769bee 5231
06a2c219
GM
5232/* Return non-zero if W's cursor intersects rectangle R. */
5233
5234static int
5235x_phys_cursor_in_rect_p (w, r)
5236 struct window *w;
5237 XRectangle *r;
5238{
5239 XRectangle cr, result;
5240 struct glyph *cursor_glyph;
5241
5242 cursor_glyph = get_phys_cursor_glyph (w);
5243 if (cursor_glyph)
5244 {
5245 cr.x = w->phys_cursor.x;
5246 cr.y = w->phys_cursor.y;
5247 cr.width = cursor_glyph->pixel_width;
5248 cr.height = w->phys_cursor_height;
5249 return x_intersect_rectangles (&cr, r, &result);
5250 }
5251 else
5252 return 0;
dc6f92b8 5253}
dc6f92b8 5254
06a2c219
GM
5255
5256/* Redraw a rectangle of window W. R is a rectangle in window
5257 relative coordinates. Call this function with input blocked. */
dc6f92b8
JB
5258
5259static void
06a2c219
GM
5260expose_window (w, r)
5261 struct window *w;
5262 XRectangle *r;
dc6f92b8 5263{
06a2c219
GM
5264 struct glyph_row *row;
5265 int y;
5266 int yb = window_text_bottom_y (w);
5267 int cursor_cleared_p;
dc6f92b8 5268
06a2c219
GM
5269 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5270 r->x, r->y, r->width, r->height));
dc6f92b8 5271
06a2c219
GM
5272 /* Convert to window coordinates. */
5273 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5274 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
dc6f92b8 5275
06a2c219
GM
5276 /* Turn off the cursor. */
5277 if (!w->pseudo_window_p
5278 && x_phys_cursor_in_rect_p (w, r))
5279 {
5280 x_clear_cursor (w);
5281 cursor_cleared_p = 1;
5282 }
5283 else
5284 cursor_cleared_p = 0;
5285
5286 /* Find the first row intersecting the rectangle R. */
5287 row = w->current_matrix->rows;
5288 y = 0;
5289 while (row->enabled_p
5290 && y < yb
5291 && y + row->height < r->y)
5292 {
5293 y += row->height;
5294 ++row;
5295 }
5296
dc6f92b8 5297 /* Display the text in the rectangle, one text line at a time. */
06a2c219
GM
5298 while (row->enabled_p
5299 && y < yb
5300 && y < r->y + r->height)
5301 {
5302 expose_line (w, row, r);
5303 y += row->height;
5304 ++row;
5305 }
5306
5307 /* Display the mode line if there is one. */
5308 if (WINDOW_WANTS_MODELINE_P (w)
5309 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5310 row->enabled_p)
5311 && row->y < r->y + r->height)
5312 expose_line (w, row, r);
dc6f92b8 5313
06a2c219 5314 if (!w->pseudo_window_p)
dc6f92b8 5315 {
06a2c219
GM
5316 /* Draw border between windows. */
5317 x_draw_vertical_border (w);
5318
5319 /* Turn the cursor on again. */
5320 if (cursor_cleared_p)
5321 x_update_window_cursor (w, 1);
5322 }
5323}
dc6f92b8 5324
dc6f92b8 5325
06a2c219
GM
5326/* Determine the intersection of two rectangles R1 and R2. Return
5327 the intersection in *RESULT. Value is non-zero if RESULT is not
5328 empty. */
5329
5330static int
5331x_intersect_rectangles (r1, r2, result)
5332 XRectangle *r1, *r2, *result;
5333{
5334 XRectangle *left, *right;
5335 XRectangle *upper, *lower;
5336 int intersection_p = 0;
5337
5338 /* Rearrange so that R1 is the left-most rectangle. */
5339 if (r1->x < r2->x)
5340 left = r1, right = r2;
5341 else
5342 left = r2, right = r1;
5343
5344 /* X0 of the intersection is right.x0, if this is inside R1,
5345 otherwise there is no intersection. */
5346 if (right->x <= left->x + left->width)
5347 {
5348 result->x = right->x;
5349
5350 /* The right end of the intersection is the minimum of the
5351 the right ends of left and right. */
5352 result->width = (min (left->x + left->width, right->x + right->width)
5353 - result->x);
5354
5355 /* Same game for Y. */
5356 if (r1->y < r2->y)
5357 upper = r1, lower = r2;
5358 else
5359 upper = r2, lower = r1;
5360
5361 /* The upper end of the intersection is lower.y0, if this is inside
5362 of upper. Otherwise, there is no intersection. */
5363 if (lower->y <= upper->y + upper->height)
dc43ef94 5364 {
06a2c219
GM
5365 result->y = lower->y;
5366
5367 /* The lower end of the intersection is the minimum of the lower
5368 ends of upper and lower. */
5369 result->height = (min (lower->y + lower->height,
5370 upper->y + upper->height)
5371 - result->y);
5372 intersection_p = 1;
dc43ef94 5373 }
dc6f92b8
JB
5374 }
5375
06a2c219 5376 return intersection_p;
dc6f92b8 5377}
06a2c219
GM
5378
5379
5380
5381
dc6f92b8 5382\f
dc6f92b8 5383static void
334208b7
RS
5384frame_highlight (f)
5385 struct frame *f;
dc6f92b8 5386{
b3e1e05c
JB
5387 /* We used to only do this if Vx_no_window_manager was non-nil, but
5388 the ICCCM (section 4.1.6) says that the window's border pixmap
5389 and border pixel are window attributes which are "private to the
5390 client", so we can always change it to whatever we want. */
5391 BLOCK_INPUT;
334208b7 5392 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5393 f->output_data.x->border_pixel);
b3e1e05c 5394 UNBLOCK_INPUT;
5d46f928 5395 x_update_cursor (f, 1);
dc6f92b8
JB
5396}
5397
5398static void
334208b7
RS
5399frame_unhighlight (f)
5400 struct frame *f;
dc6f92b8 5401{
b3e1e05c
JB
5402 /* We used to only do this if Vx_no_window_manager was non-nil, but
5403 the ICCCM (section 4.1.6) says that the window's border pixmap
5404 and border pixel are window attributes which are "private to the
5405 client", so we can always change it to whatever we want. */
5406 BLOCK_INPUT;
334208b7 5407 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5408 f->output_data.x->border_tile);
b3e1e05c 5409 UNBLOCK_INPUT;
5d46f928 5410 x_update_cursor (f, 1);
dc6f92b8 5411}
dc6f92b8 5412
f676886a
JB
5413/* The focus has changed. Update the frames as necessary to reflect
5414 the new situation. Note that we can't change the selected frame
c5acd733 5415 here, because the Lisp code we are interrupting might become confused.
eb8c3be9 5416 Each event gets marked with the frame in which it occurred, so the
c5acd733 5417 Lisp code can tell when the switch took place by examining the events. */
dc6f92b8 5418
6d4238f3 5419static void
0f941935
KH
5420x_new_focus_frame (dpyinfo, frame)
5421 struct x_display_info *dpyinfo;
f676886a 5422 struct frame *frame;
dc6f92b8 5423{
0f941935 5424 struct frame *old_focus = dpyinfo->x_focus_frame;
dc6f92b8 5425
0f941935 5426 if (frame != dpyinfo->x_focus_frame)
dc6f92b8 5427 {
58769bee 5428 /* Set this before calling other routines, so that they see
f676886a 5429 the correct value of x_focus_frame. */
0f941935 5430 dpyinfo->x_focus_frame = frame;
6d4238f3
JB
5431
5432 if (old_focus && old_focus->auto_lower)
f676886a 5433 x_lower_frame (old_focus);
dc6f92b8
JB
5434
5435#if 0
f676886a 5436 selected_frame = frame;
e0c1aef2
KH
5437 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
5438 selected_frame);
f676886a
JB
5439 Fselect_window (selected_frame->selected_window);
5440 choose_minibuf_frame ();
c118dd06 5441#endif /* ! 0 */
dc6f92b8 5442
0f941935
KH
5443 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
5444 pending_autoraise_frame = dpyinfo->x_focus_frame;
0134a210
RS
5445 else
5446 pending_autoraise_frame = 0;
6d4238f3 5447 }
dc6f92b8 5448
0f941935 5449 x_frame_rehighlight (dpyinfo);
6d4238f3
JB
5450}
5451
37c2c98b
RS
5452/* Handle an event saying the mouse has moved out of an Emacs frame. */
5453
5454void
0f941935
KH
5455x_mouse_leave (dpyinfo)
5456 struct x_display_info *dpyinfo;
37c2c98b 5457{
0f941935 5458 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
37c2c98b 5459}
6d4238f3 5460
f451eb13
JB
5461/* The focus has changed, or we have redirected a frame's focus to
5462 another frame (this happens when a frame uses a surrogate
06a2c219 5463 mini-buffer frame). Shift the highlight as appropriate.
0f941935
KH
5464
5465 The FRAME argument doesn't necessarily have anything to do with which
06a2c219 5466 frame is being highlighted or un-highlighted; we only use it to find
0f941935 5467 the appropriate X display info. */
06a2c219 5468
6d4238f3 5469static void
0f941935
KH
5470XTframe_rehighlight (frame)
5471 struct frame *frame;
6d4238f3 5472{
0f941935
KH
5473 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
5474}
6d4238f3 5475
0f941935
KH
5476static void
5477x_frame_rehighlight (dpyinfo)
5478 struct x_display_info *dpyinfo;
5479{
5480 struct frame *old_highlight = dpyinfo->x_highlight_frame;
5481
5482 if (dpyinfo->x_focus_frame)
6d4238f3 5483 {
0f941935
KH
5484 dpyinfo->x_highlight_frame
5485 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
5486 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
5487 : dpyinfo->x_focus_frame);
5488 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
f451eb13 5489 {
0f941935
KH
5490 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
5491 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
f451eb13 5492 }
dc6f92b8 5493 }
6d4238f3 5494 else
0f941935 5495 dpyinfo->x_highlight_frame = 0;
dc6f92b8 5496
0f941935 5497 if (dpyinfo->x_highlight_frame != old_highlight)
6d4238f3
JB
5498 {
5499 if (old_highlight)
f676886a 5500 frame_unhighlight (old_highlight);
0f941935
KH
5501 if (dpyinfo->x_highlight_frame)
5502 frame_highlight (dpyinfo->x_highlight_frame);
6d4238f3 5503 }
dc6f92b8 5504}
06a2c219
GM
5505
5506
dc6f92b8 5507\f
06a2c219 5508/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
dc6f92b8 5509
28430d3c
JB
5510/* Initialize mode_switch_bit and modifier_meaning. */
5511static void
334208b7
RS
5512x_find_modifier_meanings (dpyinfo)
5513 struct x_display_info *dpyinfo;
28430d3c 5514{
f689eb05 5515 int min_code, max_code;
28430d3c
JB
5516 KeySym *syms;
5517 int syms_per_code;
5518 XModifierKeymap *mods;
5519
334208b7
RS
5520 dpyinfo->meta_mod_mask = 0;
5521 dpyinfo->shift_lock_mask = 0;
5522 dpyinfo->alt_mod_mask = 0;
5523 dpyinfo->super_mod_mask = 0;
5524 dpyinfo->hyper_mod_mask = 0;
58769bee 5525
9658a521 5526#ifdef HAVE_X11R4
334208b7 5527 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
9658a521 5528#else
4a60f8c5
RS
5529 min_code = dpyinfo->display->min_keycode;
5530 max_code = dpyinfo->display->max_keycode;
9658a521
JB
5531#endif
5532
334208b7 5533 syms = XGetKeyboardMapping (dpyinfo->display,
28430d3c
JB
5534 min_code, max_code - min_code + 1,
5535 &syms_per_code);
334208b7 5536 mods = XGetModifierMapping (dpyinfo->display);
28430d3c 5537
58769bee 5538 /* Scan the modifier table to see which modifier bits the Meta and
11edeb03 5539 Alt keysyms are on. */
28430d3c 5540 {
06a2c219 5541 int row, col; /* The row and column in the modifier table. */
28430d3c
JB
5542
5543 for (row = 3; row < 8; row++)
5544 for (col = 0; col < mods->max_keypermod; col++)
5545 {
0299d313
RS
5546 KeyCode code
5547 = mods->modifiermap[(row * mods->max_keypermod) + col];
28430d3c 5548
af92970c
KH
5549 /* Zeroes are used for filler. Skip them. */
5550 if (code == 0)
5551 continue;
5552
28430d3c
JB
5553 /* Are any of this keycode's keysyms a meta key? */
5554 {
5555 int code_col;
5556
5557 for (code_col = 0; code_col < syms_per_code; code_col++)
5558 {
f689eb05 5559 int sym = syms[((code - min_code) * syms_per_code) + code_col];
28430d3c 5560
f689eb05 5561 switch (sym)
28430d3c 5562 {
f689eb05
JB
5563 case XK_Meta_L:
5564 case XK_Meta_R:
334208b7 5565 dpyinfo->meta_mod_mask |= (1 << row);
28430d3c 5566 break;
f689eb05
JB
5567
5568 case XK_Alt_L:
5569 case XK_Alt_R:
334208b7 5570 dpyinfo->alt_mod_mask |= (1 << row);
a3c44b14
RS
5571 break;
5572
5573 case XK_Hyper_L:
5574 case XK_Hyper_R:
334208b7 5575 dpyinfo->hyper_mod_mask |= (1 << row);
a3c44b14
RS
5576 break;
5577
5578 case XK_Super_L:
5579 case XK_Super_R:
334208b7 5580 dpyinfo->super_mod_mask |= (1 << row);
f689eb05 5581 break;
11edeb03
JB
5582
5583 case XK_Shift_Lock:
5584 /* Ignore this if it's not on the lock modifier. */
5585 if ((1 << row) == LockMask)
334208b7 5586 dpyinfo->shift_lock_mask = LockMask;
11edeb03 5587 break;
28430d3c
JB
5588 }
5589 }
5590 }
5591 }
5592 }
5593
f689eb05 5594 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
334208b7 5595 if (! dpyinfo->meta_mod_mask)
a3c44b14 5596 {
334208b7
RS
5597 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
5598 dpyinfo->alt_mod_mask = 0;
a3c44b14 5599 }
f689eb05 5600
148c4b70
RS
5601 /* If some keys are both alt and meta,
5602 make them just meta, not alt. */
334208b7 5603 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
148c4b70 5604 {
334208b7 5605 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
148c4b70 5606 }
58769bee 5607
28430d3c 5608 XFree ((char *) syms);
f689eb05 5609 XFreeModifiermap (mods);
28430d3c
JB
5610}
5611
dfeccd2d
JB
5612/* Convert between the modifier bits X uses and the modifier bits
5613 Emacs uses. */
06a2c219 5614
7c5283e4 5615static unsigned int
334208b7
RS
5616x_x_to_emacs_modifiers (dpyinfo, state)
5617 struct x_display_info *dpyinfo;
dc6f92b8
JB
5618 unsigned int state;
5619{
334208b7
RS
5620 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
5621 | ((state & ControlMask) ? ctrl_modifier : 0)
5622 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
5623 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
5624 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
5625 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
dc6f92b8
JB
5626}
5627
dfeccd2d 5628static unsigned int
334208b7
RS
5629x_emacs_to_x_modifiers (dpyinfo, state)
5630 struct x_display_info *dpyinfo;
dfeccd2d
JB
5631 unsigned int state;
5632{
334208b7
RS
5633 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
5634 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
5635 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
5636 | ((state & shift_modifier) ? ShiftMask : 0)
5637 | ((state & ctrl_modifier) ? ControlMask : 0)
5638 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
dfeccd2d 5639}
d047c4eb
KH
5640
5641/* Convert a keysym to its name. */
5642
5643char *
5644x_get_keysym_name (keysym)
5645 KeySym keysym;
5646{
5647 char *value;
5648
5649 BLOCK_INPUT;
5650 value = XKeysymToString (keysym);
5651 UNBLOCK_INPUT;
5652
5653 return value;
5654}
06a2c219
GM
5655
5656
e4571a43
JB
5657\f
5658/* Mouse clicks and mouse movement. Rah. */
e4571a43 5659
06a2c219
GM
5660/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
5661 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
5662 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
5663 not force the value into range. */
69388238 5664
c8dba240 5665void
69388238 5666pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
e4571a43 5667 FRAME_PTR f;
69388238 5668 register int pix_x, pix_y;
e4571a43
JB
5669 register int *x, *y;
5670 XRectangle *bounds;
69388238 5671 int noclip;
e4571a43 5672{
06a2c219 5673 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
69388238
RS
5674 even for negative values. */
5675 if (pix_x < 0)
7556890b 5676 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
69388238 5677 if (pix_y < 0)
7556890b 5678 pix_y -= (f)->output_data.x->line_height - 1;
69388238 5679
e4571a43
JB
5680 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
5681 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
5682
5683 if (bounds)
5684 {
7556890b
RS
5685 bounds->width = FONT_WIDTH (f->output_data.x->font);
5686 bounds->height = f->output_data.x->line_height;
e4571a43
JB
5687 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
5688 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
5689 }
5690
69388238
RS
5691 if (!noclip)
5692 {
5693 if (pix_x < 0)
5694 pix_x = 0;
3cbd2e0b
RS
5695 else if (pix_x > FRAME_WINDOW_WIDTH (f))
5696 pix_x = FRAME_WINDOW_WIDTH (f);
69388238
RS
5697
5698 if (pix_y < 0)
5699 pix_y = 0;
5700 else if (pix_y > f->height)
5701 pix_y = f->height;
5702 }
e4571a43
JB
5703
5704 *x = pix_x;
5705 *y = pix_y;
5706}
5707
06a2c219
GM
5708
5709/* Given HPOS/VPOS in the current matrix of W, return corresponding
5710 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
5711 can't tell the positions because W's display is not up to date,
5712 return 0. */
5713
5714int
5715glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
5716 struct window *w;
5717 int hpos, vpos;
5718 int *frame_x, *frame_y;
2b5c9e71 5719{
06a2c219
GM
5720 int success_p;
5721
5722 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
5723 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
5724
5725 if (display_completed)
5726 {
5727 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
5728 struct glyph *glyph = row->glyphs[TEXT_AREA];
5729 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
5730
5731 *frame_y = row->y;
5732 *frame_x = row->x;
5733 while (glyph < end)
5734 {
5735 *frame_x += glyph->pixel_width;
5736 ++glyph;
5737 }
5738
5739 success_p = 1;
5740 }
5741 else
5742 {
5743 *frame_y = *frame_x = 0;
5744 success_p = 0;
5745 }
5746
5747 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
5748 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
5749 return success_p;
2b5c9e71
RS
5750}
5751
06a2c219 5752
dc6f92b8
JB
5753/* Prepare a mouse-event in *RESULT for placement in the input queue.
5754
5755 If the event is a button press, then note that we have grabbed
f451eb13 5756 the mouse. */
dc6f92b8
JB
5757
5758static Lisp_Object
f451eb13 5759construct_mouse_click (result, event, f)
dc6f92b8
JB
5760 struct input_event *result;
5761 XButtonEvent *event;
f676886a 5762 struct frame *f;
dc6f92b8 5763{
f451eb13 5764 /* Make the event type no_event; we'll change that when we decide
dc6f92b8 5765 otherwise. */
f451eb13 5766 result->kind = mouse_click;
69388238 5767 result->code = event->button - Button1;
1113d9db 5768 result->timestamp = event->time;
334208b7
RS
5769 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
5770 event->state)
f689eb05 5771 | (event->type == ButtonRelease
58769bee 5772 ? up_modifier
f689eb05 5773 : down_modifier));
dc6f92b8 5774
06a2c219
GM
5775 XSETINT (result->x, event->x);
5776 XSETINT (result->y, event->y);
5777 XSETFRAME (result->frame_or_window, f);
5778 return Qnil;
dc6f92b8 5779}
b849c413 5780
06a2c219
GM
5781#if 0 /* This function isn't called. --gerd */
5782
b849c413
RS
5783/* Prepare a menu-event in *RESULT for placement in the input queue. */
5784
5785static Lisp_Object
5786construct_menu_click (result, event, f)
5787 struct input_event *result;
5788 XButtonEvent *event;
5789 struct frame *f;
5790{
5791 /* Make the event type no_event; we'll change that when we decide
5792 otherwise. */
5793 result->kind = mouse_click;
26459b28 5794 result->code = event->button - Button1;
b849c413 5795 result->timestamp = event->time;
334208b7
RS
5796 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
5797 event->state)
b849c413 5798 | (event->type == ButtonRelease
58769bee 5799 ? up_modifier
b849c413
RS
5800 : down_modifier));
5801
e0c1aef2
KH
5802 XSETINT (result->x, event->x);
5803 XSETINT (result->y, -1);
5804 XSETFRAME (result->frame_or_window, f);
b849c413 5805}
06a2c219
GM
5806
5807#endif /* 0 */
5808
69388238 5809\f
90e65f07
JB
5810/* Function to report a mouse movement to the mainstream Emacs code.
5811 The input handler calls this.
5812
5813 We have received a mouse movement event, which is given in *event.
5814 If the mouse is over a different glyph than it was last time, tell
5815 the mainstream emacs code by setting mouse_moved. If not, ask for
5816 another motion event, so we can check again the next time it moves. */
b8009dd1 5817
06a2c219
GM
5818static XMotionEvent last_mouse_motion_event;
5819static Lisp_Object last_mouse_motion_frame;
5820
90e65f07 5821static void
12ba150f 5822note_mouse_movement (frame, event)
f676886a 5823 FRAME_PTR frame;
90e65f07 5824 XMotionEvent *event;
90e65f07 5825{
e5d77022 5826 last_mouse_movement_time = event->time;
06a2c219
GM
5827 last_mouse_motion_event = *event;
5828 XSETFRAME (last_mouse_motion_frame, frame);
e5d77022 5829
27f338af
RS
5830 if (event->window != FRAME_X_WINDOW (frame))
5831 {
39d8bb4d 5832 frame->mouse_moved = 1;
27f338af 5833 last_mouse_scroll_bar = Qnil;
27f338af 5834 note_mouse_highlight (frame, -1, -1);
27f338af
RS
5835 }
5836
90e65f07 5837 /* Has the mouse moved off the glyph it was on at the last sighting? */
27f338af
RS
5838 else if (event->x < last_mouse_glyph.x
5839 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
5840 || event->y < last_mouse_glyph.y
5841 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
12ba150f 5842 {
39d8bb4d 5843 frame->mouse_moved = 1;
ab648270 5844 last_mouse_scroll_bar = Qnil;
b8009dd1 5845 note_mouse_highlight (frame, event->x, event->y);
90e65f07
JB
5846 }
5847}
5848
bf1c0ba1 5849/* This is used for debugging, to turn off note_mouse_highlight. */
bf1c0ba1 5850
06a2c219
GM
5851 int disable_mouse_highlight;
5852
5853
5854\f
5855/************************************************************************
5856 Mouse Face
5857 ************************************************************************/
5858
5859/* Find the glyph under window-relative coordinates X/Y in window W.
5860 Consider only glyphs from buffer text, i.e. no glyphs from overlay
5861 strings. Return in *HPOS and *VPOS the row and column number of
5862 the glyph found. Return in *AREA the glyph area containing X.
5863 Value is a pointer to the glyph found or null if X/Y is not on
5864 text, or we can't tell because W's current matrix is not up to
5865 date. */
5866
5867static struct glyph *
5868x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
5869 struct window *w;
5870 int x, y;
5871 int *hpos, *vpos, *area;
5872{
5873 struct glyph *glyph, *end;
5874 struct glyph_row *row;
5875 int x0, i, left_area_width;
5876
5877 /* Find row containing Y. Give up if some row is not enabled. */
5878 for (i = 0; i < w->current_matrix->nrows; ++i)
5879 {
5880 row = MATRIX_ROW (w->current_matrix, i);
5881 if (!row->enabled_p)
5882 return NULL;
5883 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
5884 break;
5885 }
5886
5887 *vpos = i;
5888 *hpos = 0;
5889
5890 /* Give up if Y is not in the window. */
5891 if (i == w->current_matrix->nrows)
5892 return NULL;
5893
5894 /* Get the glyph area containing X. */
5895 if (w->pseudo_window_p)
5896 {
5897 *area = TEXT_AREA;
5898 x0 = 0;
5899 }
5900 else
5901 {
5902 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
5903 if (x < left_area_width)
5904 {
5905 *area = LEFT_MARGIN_AREA;
5906 x0 = 0;
5907 }
5908 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
5909 {
5910 *area = TEXT_AREA;
5911 x0 = row->x + left_area_width;
5912 }
5913 else
5914 {
5915 *area = RIGHT_MARGIN_AREA;
5916 x0 = left_area_width + window_box_width (w, TEXT_AREA);
5917 }
5918 }
5919
5920 /* Find glyph containing X. */
5921 glyph = row->glyphs[*area];
5922 end = glyph + row->used[*area];
5923 while (glyph < end)
5924 {
5925 if (x < x0 + glyph->pixel_width)
5926 {
5927 if (w->pseudo_window_p)
5928 break;
5929 else if (BUFFERP (glyph->object))
5930 break;
5931 }
5932
5933 x0 += glyph->pixel_width;
5934 ++glyph;
5935 }
5936
5937 if (glyph == end)
5938 return NULL;
5939
5940 *hpos = glyph - row->glyphs[*area];
5941 return glyph;
5942}
5943
5944
5945/* Convert frame-relative x/y to coordinates relative to window W.
5946 Takes pseudo-windows into account. */
5947
5948static void
5949frame_to_window_pixel_xy (w, x, y)
5950 struct window *w;
5951 int *x, *y;
5952{
5953 if (w->pseudo_window_p)
5954 {
5955 /* A pseudo-window is always full-width, and starts at the
5956 left edge of the frame, plus a frame border. */
5957 struct frame *f = XFRAME (w->frame);
5958 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
5959 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
5960 }
5961 else
5962 {
5963 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
5964 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
5965 }
5966}
5967
5968
5969/* Take proper action when mouse has moved to the mode or top line of
5970 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
5971 mode line. X is relative to the start of the text display area of
5972 W, so the width of bitmap areas and scroll bars must be subtracted
5973 to get a position relative to the start of the mode line. */
5974
5975static void
5976note_mode_line_highlight (w, x, mode_line_p)
5977 struct window *w;
5978 int x, mode_line_p;
5979{
5980 struct frame *f = XFRAME (w->frame);
5981 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
5982 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
5983 struct glyph_row *row;
5984
5985 if (mode_line_p)
5986 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
5987 else
5988 row = MATRIX_TOP_LINE_ROW (w->current_matrix);
5989
5990 if (row->enabled_p)
5991 {
5992 struct glyph *glyph, *end;
5993 Lisp_Object help, map;
5994 int x0;
5995
5996 /* Find the glyph under X. */
5997 glyph = row->glyphs[TEXT_AREA];
5998 end = glyph + row->used[TEXT_AREA];
5999 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6000 + FRAME_X_FLAGS_AREA_WIDTH (f));
6001 while (glyph < end
6002 && x >= x0 + glyph->pixel_width)
6003 {
6004 x0 += glyph->pixel_width;
6005 ++glyph;
6006 }
6007
6008 if (glyph < end
6009 && STRINGP (glyph->object)
6010 && XSTRING (glyph->object)->intervals
6011 && glyph->charpos >= 0
6012 && glyph->charpos < XSTRING (glyph->object)->size)
6013 {
6014 /* If we're on a string with `help-echo' text property,
6015 arrange for the help to be displayed. This is done by
6016 setting the global variable help_echo to the help string. */
6017 help = Fget_text_property (make_number (glyph->charpos),
6018 Qhelp_echo, glyph->object);
6019 if (STRINGP (help))
6020 help_echo = help;
6021
6022 /* Change the mouse pointer according to what is under X/Y. */
6023 map = Fget_text_property (make_number (glyph->charpos),
6024 Qlocal_map, glyph->object);
6025 if (!NILP (Fkeymapp (map)))
6026 cursor = f->output_data.x->nontext_cursor;
6027 }
6028 }
6029
6030 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6031}
6032
6033
6034/* Take proper action when the mouse has moved to position X, Y on
6035 frame F as regards highlighting characters that have mouse-face
6036 properties. Also de-highlighting chars where the mouse was before.
27f338af 6037 X and Y can be negative or out of range. */
b8009dd1
RS
6038
6039static void
6040note_mouse_highlight (f, x, y)
06a2c219 6041 struct frame *f;
c32cdd9a 6042 int x, y;
b8009dd1 6043{
06a2c219
GM
6044 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6045 int portion;
b8009dd1
RS
6046 Lisp_Object window;
6047 struct window *w;
6048
06a2c219
GM
6049 /* When a menu is active, don't highlight because this looks odd. */
6050#ifdef USE_X_TOOLKIT
6051 if (popup_activated ())
6052 return;
6053#endif
6054
bf1c0ba1
RS
6055 if (disable_mouse_highlight)
6056 return;
6057
06a2c219
GM
6058 dpyinfo->mouse_face_mouse_x = x;
6059 dpyinfo->mouse_face_mouse_y = y;
6060 dpyinfo->mouse_face_mouse_frame = f;
b8009dd1 6061
06a2c219 6062 if (dpyinfo->mouse_face_defer)
b8009dd1
RS
6063 return;
6064
514e4681
RS
6065 if (gc_in_progress)
6066 {
06a2c219 6067 dpyinfo->mouse_face_deferred_gc = 1;
514e4681
RS
6068 return;
6069 }
6070
b8009dd1 6071 /* Which window is that in? */
06a2c219 6072 window = window_from_coordinates (f, x, y, &portion, 1);
b8009dd1
RS
6073
6074 /* If we were displaying active text in another window, clear that. */
06a2c219
GM
6075 if (! EQ (window, dpyinfo->mouse_face_window))
6076 clear_mouse_face (dpyinfo);
6077
6078 /* Not on a window -> return. */
6079 if (!WINDOWP (window))
6080 return;
6081
6082 /* Convert to window-relative pixel coordinates. */
6083 w = XWINDOW (window);
6084 frame_to_window_pixel_xy (w, &x, &y);
6085
6086 /* Handle toolbar window differently since it doesn't display a
6087 buffer. */
6088 if (EQ (window, f->toolbar_window))
6089 {
6090 note_toolbar_highlight (f, x, y);
6091 return;
6092 }
6093
6094 if (portion == 1 || portion == 3)
6095 {
6096 /* Mouse is on the mode or top line. */
6097 note_mode_line_highlight (w, x, portion == 1);
6098 return;
6099 }
6100 else
6101 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6102 f->output_data.x->text_cursor);
b8009dd1 6103
0cdd0c9f
RS
6104 /* Are we in a window whose display is up to date?
6105 And verify the buffer's text has not changed. */
06a2c219
GM
6106 if (/* Within text portion of the window. */
6107 portion == 0
0cdd0c9f 6108 && EQ (w->window_end_valid, w->buffer)
26459b28
KH
6109 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6110 && (XFASTINT (w->last_overlay_modified)
6111 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
b8009dd1 6112 {
06a2c219
GM
6113 int hpos, vpos, pos, i, area;
6114 struct glyph *glyph;
b8009dd1 6115
06a2c219
GM
6116 /* Find the glyph under X/Y. */
6117 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6118
6119 /* Clear mouse face if X/Y not over text. */
6120 if (glyph == NULL
6121 || area != TEXT_AREA
6122 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
b8009dd1 6123 {
06a2c219
GM
6124 clear_mouse_face (dpyinfo);
6125 return;
6126 }
6127
6128 pos = glyph->charpos;
6129 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6130
6131 /* Check for mouse-face and help-echo. */
6132 {
6133 Lisp_Object mouse_face, overlay, position;
6134 Lisp_Object *overlay_vec;
6135 int len, noverlays;
6136 struct buffer *obuf;
6137 int obegv, ozv;
6138
6139 /* If we get an out-of-range value, return now; avoid an error. */
6140 if (pos > BUF_Z (XBUFFER (w->buffer)))
6141 return;
6142
6143 /* Make the window's buffer temporarily current for
6144 overlays_at and compute_char_face. */
6145 obuf = current_buffer;
6146 current_buffer = XBUFFER (w->buffer);
6147 obegv = BEGV;
6148 ozv = ZV;
6149 BEGV = BEG;
6150 ZV = Z;
6151
6152 /* Is this char mouse-active or does it have help-echo? */
6153 XSETINT (position, pos);
6154
6155 /* Put all the overlays we want in a vector in overlay_vec.
6156 Store the length in len. If there are more than 10, make
6157 enough space for all, and try again. */
6158 len = 10;
6159 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6160 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6161 if (noverlays > len)
6162 {
6163 len = noverlays;
6164 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6165 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6166 }
6167
6168 noverlays = sort_overlays (overlay_vec, noverlays, w);
6169
6170 /* Check mouse-face highlighting. */
6171 if (! (EQ (window, dpyinfo->mouse_face_window)
6172 && vpos >= dpyinfo->mouse_face_beg_row
6173 && vpos <= dpyinfo->mouse_face_end_row
6174 && (vpos > dpyinfo->mouse_face_beg_row
6175 || hpos >= dpyinfo->mouse_face_beg_col)
6176 && (vpos < dpyinfo->mouse_face_end_row
6177 || hpos < dpyinfo->mouse_face_end_col
6178 || dpyinfo->mouse_face_past_end)))
6179 {
6180 /* Clear the display of the old active region, if any. */
6181 clear_mouse_face (dpyinfo);
6182
6183 /* Find the highest priority overlay that has a mouse-face prop. */
6184 overlay = Qnil;
6185 for (i = 0; i < noverlays; i++)
6186 {
6187 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6188 if (!NILP (mouse_face))
6189 {
6190 overlay = overlay_vec[i];
6191 break;
6192 }
6193 }
6194
6195 /* If no overlay applies, get a text property. */
6196 if (NILP (overlay))
6197 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6198
6199 /* Handle the overlay case. */
6200 if (! NILP (overlay))
6201 {
6202 /* Find the range of text around this char that
6203 should be active. */
6204 Lisp_Object before, after;
6205 int ignore;
6206
6207 before = Foverlay_start (overlay);
6208 after = Foverlay_end (overlay);
6209 /* Record this as the current active region. */
6210 fast_find_position (w, XFASTINT (before),
6211 &dpyinfo->mouse_face_beg_col,
6212 &dpyinfo->mouse_face_beg_row,
6213 &dpyinfo->mouse_face_beg_x,
6214 &dpyinfo->mouse_face_beg_y);
6215 dpyinfo->mouse_face_past_end
6216 = !fast_find_position (w, XFASTINT (after),
6217 &dpyinfo->mouse_face_end_col,
6218 &dpyinfo->mouse_face_end_row,
6219 &dpyinfo->mouse_face_end_x,
6220 &dpyinfo->mouse_face_end_y);
6221 dpyinfo->mouse_face_window = window;
6222 dpyinfo->mouse_face_face_id
6223 = face_at_buffer_position (w, pos, 0, 0,
6224 &ignore, pos + 1, 1);
6225
6226 /* Display it as active. */
6227 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6228 }
6229 /* Handle the text property case. */
6230 else if (! NILP (mouse_face))
6231 {
6232 /* Find the range of text around this char that
6233 should be active. */
6234 Lisp_Object before, after, beginning, end;
6235 int ignore;
6236
6237 beginning = Fmarker_position (w->start);
6238 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6239 - XFASTINT (w->window_end_pos)));
6240 before
6241 = Fprevious_single_property_change (make_number (pos + 1),
6242 Qmouse_face,
6243 w->buffer, beginning);
6244 after
6245 = Fnext_single_property_change (position, Qmouse_face,
6246 w->buffer, end);
6247 /* Record this as the current active region. */
6248 fast_find_position (w, XFASTINT (before),
6249 &dpyinfo->mouse_face_beg_col,
6250 &dpyinfo->mouse_face_beg_row,
6251 &dpyinfo->mouse_face_beg_x,
6252 &dpyinfo->mouse_face_beg_y);
6253 dpyinfo->mouse_face_past_end
6254 = !fast_find_position (w, XFASTINT (after),
6255 &dpyinfo->mouse_face_end_col,
6256 &dpyinfo->mouse_face_end_row,
6257 &dpyinfo->mouse_face_end_x,
6258 &dpyinfo->mouse_face_end_y);
6259 dpyinfo->mouse_face_window = window;
6260 dpyinfo->mouse_face_face_id
6261 = face_at_buffer_position (w, pos, 0, 0,
6262 &ignore, pos + 1, 1);
6263
6264 /* Display it as active. */
6265 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6266 }
6267 }
6268
6269 /* Look for a `help-echo' property. */
6270 {
6271 Lisp_Object help;
6272
6273 /* Check overlays first. */
6274 help = Qnil;
6275 for (i = 0; i < noverlays && !STRINGP (help); ++i)
6276 help = Foverlay_get (overlay_vec[i], Qhelp_echo);
6277
6278 /* Try text properties. */
6279 if (!STRINGP (help)
6280 && ((STRINGP (glyph->object)
6281 && glyph->charpos >= 0
6282 && glyph->charpos < XSTRING (glyph->object)->size)
6283 || (BUFFERP (glyph->object)
6284 && glyph->charpos >= BEGV
6285 && glyph->charpos < ZV)))
6286 help = Fget_text_property (make_number (glyph->charpos),
6287 Qhelp_echo, glyph->object);
6288
6289 if (STRINGP (help))
6290 help_echo = help;
6291 }
6292
6293 BEGV = obegv;
6294 ZV = ozv;
6295 current_buffer = obuf;
6296 }
6297 }
6298}
6299
6300static void
6301redo_mouse_highlight ()
6302{
6303 if (!NILP (last_mouse_motion_frame)
6304 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6305 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6306 last_mouse_motion_event.x,
6307 last_mouse_motion_event.y);
6308}
6309
6310
6311\f
6312/***********************************************************************
6313 Toolbars
6314 ***********************************************************************/
6315
6316static int x_toolbar_item P_ ((struct frame *, int, int,
6317 struct glyph **, int *, int *, int *));
6318
6319/* Toolbar item index of the item on which a mouse button was pressed
6320 or -1. */
6321
6322static int last_toolbar_item;
6323
6324
6325/* Get information about the toolbar item at position X/Y on frame F.
6326 Return in *GLYPH a pointer to the glyph of the toolbar item in
6327 the current matrix of the toolbar window of F, or NULL if not
6328 on a toolbar item. Return in *PROP_IDX the index of the toolbar
6329 item in F->current_toolbar_items. Value is
6330
6331 -1 if X/Y is not on a toolbar item
6332 0 if X/Y is on the same item that was highlighted before.
6333 1 otherwise. */
6334
6335static int
6336x_toolbar_item (f, x, y, glyph, hpos, vpos, prop_idx)
6337 struct frame *f;
6338 int x, y;
6339 struct glyph **glyph;
6340 int *hpos, *vpos, *prop_idx;
6341{
6342 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6343 struct window *w = XWINDOW (f->toolbar_window);
6344 int area;
6345
6346 /* Find the glyph under X/Y. */
6347 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6348 if (*glyph == NULL)
6349 return -1;
6350
6351 /* Get the start of this toolbar item's properties in
6352 f->current_toolbar_items. */
6353 if (!toolbar_item_info (f, *glyph, prop_idx))
6354 return -1;
6355
6356 /* Is mouse on the highlighted item? */
6357 if (EQ (f->toolbar_window, dpyinfo->mouse_face_window)
6358 && *vpos >= dpyinfo->mouse_face_beg_row
6359 && *vpos <= dpyinfo->mouse_face_end_row
6360 && (*vpos > dpyinfo->mouse_face_beg_row
6361 || *hpos >= dpyinfo->mouse_face_beg_col)
6362 && (*vpos < dpyinfo->mouse_face_end_row
6363 || *hpos < dpyinfo->mouse_face_end_col
6364 || dpyinfo->mouse_face_past_end))
6365 return 0;
6366
6367 return 1;
6368}
6369
6370
6371/* Handle mouse button event on the toolbar of frame F, at
6372 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6373 or ButtonRelase. */
6374
6375static void
6376x_handle_toolbar_click (f, button_event)
6377 struct frame *f;
6378 XButtonEvent *button_event;
6379{
6380 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6381 struct window *w = XWINDOW (f->toolbar_window);
6382 int hpos, vpos, prop_idx;
6383 struct glyph *glyph;
6384 Lisp_Object enabled_p;
6385 int x = button_event->x;
6386 int y = button_event->y;
6387
6388 /* If not on the highlighted toolbar item, return. */
6389 frame_to_window_pixel_xy (w, &x, &y);
6390 if (x_toolbar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
6391 return;
6392
6393 /* If item is disabled, do nothing. */
6394 enabled_p = (XVECTOR (f->current_toolbar_items)
6395 ->contents[prop_idx + TOOLBAR_ITEM_ENABLED_P]);
6396 if (NILP (enabled_p))
6397 return;
6398
6399 if (button_event->type == ButtonPress)
6400 {
6401 /* Show item in pressed state. */
6402 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6403 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
6404 last_toolbar_item = prop_idx;
6405 }
6406 else
6407 {
6408 Lisp_Object key, frame;
6409 struct input_event event;
6410
6411 /* Show item in released state. */
6412 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6413 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6414
6415 key = (XVECTOR (f->current_toolbar_items)
6416 ->contents[prop_idx + TOOLBAR_ITEM_KEY]);
6417
6418 XSETFRAME (frame, f);
6419 event.kind = TOOLBAR_EVENT;
6420 event.frame_or_window = Fcons (frame, Fcons (Qtoolbar, Qnil));
6421 kbd_buffer_store_event (&event);
6422
6423 event.kind = TOOLBAR_EVENT;
6424 event.frame_or_window = Fcons (frame, key);
6425 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6426 button_event->state);
6427 kbd_buffer_store_event (&event);
6428 last_toolbar_item = -1;
6429 }
6430}
6431
6432
6433/* Possibly highlight a toolbar item on frame F when mouse moves to
6434 toolbar window-relative coordinates X/Y. Called from
6435 note_mouse_highlight. */
6436
6437static void
6438note_toolbar_highlight (f, x, y)
6439 struct frame *f;
6440 int x, y;
6441{
6442 Lisp_Object window = f->toolbar_window;
6443 struct window *w = XWINDOW (window);
6444 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6445 int hpos, vpos;
6446 struct glyph *glyph;
6447 struct glyph_row *row;
6448 int i, j, area;
6449 Lisp_Object enabled_p;
6450 int prop_idx;
6451 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
6452 int on_highlight_p, mouse_down_p, rc;
6453
6454 /* Function note_mouse_highlight is called with negative x(y
6455 values when mouse moves outside of the frame. */
6456 if (x <= 0 || y <= 0)
6457 {
6458 clear_mouse_face (dpyinfo);
6459 return;
6460 }
6461
6462 rc = x_toolbar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
6463 if (rc < 0)
6464 {
6465 /* Not on toolbar item. */
6466 clear_mouse_face (dpyinfo);
6467 return;
6468 }
6469 else if (rc == 0)
6470 /* On same toolbar item as before. */
6471 goto set_help_echo;
b8009dd1 6472
06a2c219
GM
6473 clear_mouse_face (dpyinfo);
6474
6475 /* Mouse is down, but on different toolbar item? */
6476 mouse_down_p = (dpyinfo->grabbed
6477 && f == last_mouse_frame
6478 && FRAME_LIVE_P (f));
6479 if (mouse_down_p
6480 && last_toolbar_item != prop_idx)
6481 return;
6482
6483 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
6484 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
6485
6486 /* If toolbar item is not enabled, don't highlight it. */
6487 enabled_p = (XVECTOR (f->current_toolbar_items)
6488 ->contents[prop_idx + TOOLBAR_ITEM_ENABLED_P]);
6489 if (!NILP (enabled_p))
6490 {
6491 /* Compute the x-position of the glyph. In front and past the
6492 image is a space. We include this is the highlighted area. */
6493 row = MATRIX_ROW (w->current_matrix, vpos);
6494 for (i = x = 0; i < hpos; ++i)
6495 x += row->glyphs[TEXT_AREA][i].pixel_width;
6496
6497 /* Record this as the current active region. */
6498 dpyinfo->mouse_face_beg_col = hpos;
6499 dpyinfo->mouse_face_beg_row = vpos;
6500 dpyinfo->mouse_face_beg_x = x;
6501 dpyinfo->mouse_face_beg_y = row->y;
6502 dpyinfo->mouse_face_past_end = 0;
6503
6504 dpyinfo->mouse_face_end_col = hpos + 1;
6505 dpyinfo->mouse_face_end_row = vpos;
6506 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
6507 dpyinfo->mouse_face_end_y = row->y;
6508 dpyinfo->mouse_face_window = window;
6509 dpyinfo->mouse_face_face_id = TOOLBAR_FACE_ID;
6510
6511 /* Display it as active. */
6512 show_mouse_face (dpyinfo, draw);
6513 dpyinfo->mouse_face_image_state = draw;
b8009dd1 6514 }
06a2c219
GM
6515
6516 set_help_echo:
6517
6518 /* Set help_echo to a help string.to display for this toolbar item.
6519 XTread_socket does the rest. */
6520 help_echo = (XVECTOR (f->current_toolbar_items)
6521 ->contents[prop_idx + TOOLBAR_ITEM_HELP]);
6522 if (!STRINGP (help_echo))
6523 help_echo = (XVECTOR (f->current_toolbar_items)
6524 ->contents[prop_idx + TOOLBAR_ITEM_CAPTION]);
b8009dd1 6525}
4d73d038 6526
06a2c219
GM
6527
6528\f
6529/* Find the glyph matrix position of buffer position POS in window W.
6530 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
6531 current glyphs must be up to date. If POS is above window start
6532 return (0, 0, 0, 0). If POS is after end of W, return end of
6533 last line in W. */
b8009dd1
RS
6534
6535static int
06a2c219
GM
6536fast_find_position (w, pos, hpos, vpos, x, y)
6537 struct window *w;
b8009dd1 6538 int pos;
06a2c219 6539 int *hpos, *vpos, *x, *y;
b8009dd1 6540{
b8009dd1 6541 int i;
bf1c0ba1 6542 int lastcol;
06a2c219
GM
6543 int maybe_next_line_p = 0;
6544 int line_start_position;
6545 int yb = window_text_bottom_y (w);
6546 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
6547 struct glyph_row *best_row = row;
6548 int row_vpos = 0, best_row_vpos = 0;
6549 int current_x;
6550
6551 while (row->y < yb)
b8009dd1 6552 {
06a2c219
GM
6553 if (row->used[TEXT_AREA])
6554 line_start_position = row->glyphs[TEXT_AREA]->charpos;
6555 else
6556 line_start_position = 0;
6557
6558 if (line_start_position > pos)
b8009dd1 6559 break;
77b68646
RS
6560 /* If the position sought is the end of the buffer,
6561 don't include the blank lines at the bottom of the window. */
06a2c219
GM
6562 else if (line_start_position == pos
6563 && pos == BUF_ZV (XBUFFER (w->buffer)))
77b68646 6564 {
06a2c219 6565 maybe_next_line_p = 1;
77b68646
RS
6566 break;
6567 }
06a2c219
GM
6568 else if (line_start_position > 0)
6569 {
6570 best_row = row;
6571 best_row_vpos = row_vpos;
6572 }
6573
6574 ++row;
6575 ++row_vpos;
b8009dd1 6576 }
06a2c219
GM
6577
6578 /* Find the right column within BEST_ROW. */
6579 lastcol = 0;
6580 current_x = best_row->x;
6581 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
bf1c0ba1 6582 {
06a2c219
GM
6583 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
6584 int charpos;
6585
6586 charpos = glyph->charpos;
6587 if (charpos == pos)
bf1c0ba1 6588 {
06a2c219
GM
6589 *hpos = i;
6590 *vpos = best_row_vpos;
6591 *x = current_x;
6592 *y = best_row->y;
bf1c0ba1
RS
6593 return 1;
6594 }
06a2c219 6595 else if (charpos > pos)
4d73d038 6596 break;
06a2c219
GM
6597 else if (charpos > 0)
6598 lastcol = i;
6599
6600 current_x += glyph->pixel_width;
bf1c0ba1 6601 }
b8009dd1 6602
77b68646
RS
6603 /* If we're looking for the end of the buffer,
6604 and we didn't find it in the line we scanned,
6605 use the start of the following line. */
06a2c219 6606 if (maybe_next_line_p)
77b68646 6607 {
06a2c219
GM
6608 ++best_row;
6609 ++best_row_vpos;
6610 lastcol = 0;
6611 current_x = best_row->x;
77b68646
RS
6612 }
6613
06a2c219
GM
6614 *vpos = best_row_vpos;
6615 *hpos = lastcol + 1;
6616 *x = current_x;
6617 *y = best_row->y;
b8009dd1
RS
6618 return 0;
6619}
6620
06a2c219 6621
b8009dd1
RS
6622/* Display the active region described by mouse_face_*
6623 in its mouse-face if HL > 0, in its normal face if HL = 0. */
6624
6625static void
06a2c219 6626show_mouse_face (dpyinfo, draw)
7a13e894 6627 struct x_display_info *dpyinfo;
06a2c219 6628 enum draw_glyphs_face draw;
b8009dd1 6629{
7a13e894 6630 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
06a2c219 6631 struct frame *f = XFRAME (WINDOW_FRAME (w));
b8009dd1 6632 int i;
06a2c219
GM
6633 int cursor_off_p = 0;
6634 struct cursor_pos saved_cursor;
6635
6636 saved_cursor = output_cursor;
6637
6638 /* If window is in the process of being destroyed, don't bother
6639 to do anything. */
6640 if (w->current_matrix == NULL)
6641 goto set_x_cursor;
6642
6643 /* Recognize when we are called to operate on rows that don't exist
6644 anymore. This can happen when a window is split. */
6645 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
6646 goto set_x_cursor;
6647
6648 set_output_cursor (&w->phys_cursor);
6649
6650 /* Note that mouse_face_beg_row etc. are window relative. */
6651 for (i = dpyinfo->mouse_face_beg_row;
6652 i <= dpyinfo->mouse_face_end_row;
6653 i++)
6654 {
6655 int start_hpos, end_hpos, start_x;
6656 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
6657
6658 /* Don't do anything if row doesn't have valid contents. */
6659 if (!row->enabled_p)
6660 continue;
6661
6662 /* For all but the first row, the highlight starts at column 0. */
6663 if (i == dpyinfo->mouse_face_beg_row)
6664 {
6665 start_hpos = dpyinfo->mouse_face_beg_col;
6666 start_x = dpyinfo->mouse_face_beg_x;
6667 }
6668 else
6669 {
6670 start_hpos = 0;
6671 start_x = 0;
6672 }
6673
6674 if (i == dpyinfo->mouse_face_end_row)
6675 end_hpos = dpyinfo->mouse_face_end_col;
6676 else
6677 end_hpos = row->used[TEXT_AREA];
6678
6679 /* If the cursor's in the text we are about to rewrite, turn the
6680 cursor off. */
6681 if (!w->pseudo_window_p
6682 && i == output_cursor.vpos
6683 && output_cursor.hpos >= start_hpos - 1
6684 && output_cursor.hpos <= end_hpos)
514e4681 6685 {
06a2c219
GM
6686 x_update_window_cursor (w, 0);
6687 cursor_off_p = 1;
514e4681 6688 }
b8009dd1 6689
06a2c219
GM
6690 if (end_hpos > start_hpos)
6691 x_draw_glyphs (w, start_x, row, updated_area,
66ac4b0e 6692 start_hpos, end_hpos, draw, NULL, NULL, 0);
b8009dd1
RS
6693 }
6694
514e4681 6695 /* If we turned the cursor off, turn it back on. */
06a2c219
GM
6696 if (cursor_off_p)
6697 x_display_cursor (w, 1,
6698 output_cursor.hpos, output_cursor.vpos,
6699 output_cursor.x, output_cursor.y);
2729a2b5 6700
06a2c219 6701 output_cursor = saved_cursor;
fb3b7de5 6702
06a2c219
GM
6703 set_x_cursor:
6704
6705 /* Change the mouse cursor. */
6706 if (draw == DRAW_NORMAL_TEXT)
6707 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6708 f->output_data.x->text_cursor);
6709 else if (draw == DRAW_MOUSE_FACE)
334208b7 6710 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 6711 f->output_data.x->cross_cursor);
27ead1d5 6712 else
334208b7 6713 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
06a2c219 6714 f->output_data.x->nontext_cursor);
b8009dd1
RS
6715}
6716
6717/* Clear out the mouse-highlighted active region.
06a2c219 6718 Redraw it un-highlighted first. */
b8009dd1 6719
06a2c219 6720void
7a13e894
RS
6721clear_mouse_face (dpyinfo)
6722 struct x_display_info *dpyinfo;
b8009dd1 6723{
06a2c219
GM
6724 if (tip_frame)
6725 return;
6726
7a13e894 6727 if (! NILP (dpyinfo->mouse_face_window))
06a2c219 6728 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
b8009dd1 6729
7a13e894
RS
6730 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
6731 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
6732 dpyinfo->mouse_face_window = Qnil;
b8009dd1 6733}
e687d06e
RS
6734
6735/* Just discard the mouse face information for frame F, if any.
6736 This is used when the size of F is changed. */
6737
dfcf069d 6738void
e687d06e
RS
6739cancel_mouse_face (f)
6740 FRAME_PTR f;
6741{
6742 Lisp_Object window;
6743 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6744
6745 window = dpyinfo->mouse_face_window;
6746 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
6747 {
6748 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
6749 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
6750 dpyinfo->mouse_face_window = Qnil;
6751 }
6752}
b8009dd1 6753\f
ab648270
JB
6754static struct scroll_bar *x_window_to_scroll_bar ();
6755static void x_scroll_bar_report_motion ();
12ba150f 6756
90e65f07 6757/* Return the current position of the mouse.
2d7fc7e8 6758 *fp should be a frame which indicates which display to ask about.
90e65f07 6759
2d7fc7e8 6760 If the mouse movement started in a scroll bar, set *fp, *bar_window,
ab648270 6761 and *part to the frame, window, and scroll bar part that the mouse
12ba150f 6762 is over. Set *x and *y to the portion and whole of the mouse's
ab648270 6763 position on the scroll bar.
12ba150f 6764
2d7fc7e8 6765 If the mouse movement started elsewhere, set *fp to the frame the
12ba150f
JB
6766 mouse is on, *bar_window to nil, and *x and *y to the character cell
6767 the mouse is over.
6768
06a2c219 6769 Set *time to the server time-stamp for the time at which the mouse
12ba150f
JB
6770 was at this position.
6771
a135645a
RS
6772 Don't store anything if we don't have a valid set of values to report.
6773
90e65f07 6774 This clears the mouse_moved flag, so we can wait for the next mouse
f5bb65ec 6775 movement. */
90e65f07
JB
6776
6777static void
1cf412ec 6778XTmouse_position (fp, insist, bar_window, part, x, y, time)
334208b7 6779 FRAME_PTR *fp;
1cf412ec 6780 int insist;
12ba150f 6781 Lisp_Object *bar_window;
ab648270 6782 enum scroll_bar_part *part;
90e65f07 6783 Lisp_Object *x, *y;
e5d77022 6784 unsigned long *time;
90e65f07 6785{
a135645a
RS
6786 FRAME_PTR f1;
6787
90e65f07
JB
6788 BLOCK_INPUT;
6789
8bcee03e 6790 if (! NILP (last_mouse_scroll_bar) && insist == 0)
334208b7 6791 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
90e65f07
JB
6792 else
6793 {
12ba150f
JB
6794 Window root;
6795 int root_x, root_y;
90e65f07 6796
12ba150f
JB
6797 Window dummy_window;
6798 int dummy;
6799
39d8bb4d
KH
6800 Lisp_Object frame, tail;
6801
6802 /* Clear the mouse-moved flag for every frame on this display. */
6803 FOR_EACH_FRAME (tail, frame)
6804 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
6805 XFRAME (frame)->mouse_moved = 0;
6806
ab648270 6807 last_mouse_scroll_bar = Qnil;
12ba150f
JB
6808
6809 /* Figure out which root window we're on. */
334208b7
RS
6810 XQueryPointer (FRAME_X_DISPLAY (*fp),
6811 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
12ba150f
JB
6812
6813 /* The root window which contains the pointer. */
6814 &root,
6815
6816 /* Trash which we can't trust if the pointer is on
6817 a different screen. */
6818 &dummy_window,
6819
6820 /* The position on that root window. */
58769bee 6821 &root_x, &root_y,
12ba150f
JB
6822
6823 /* More trash we can't trust. */
6824 &dummy, &dummy,
6825
6826 /* Modifier keys and pointer buttons, about which
6827 we don't care. */
6828 (unsigned int *) &dummy);
6829
6830 /* Now we have a position on the root; find the innermost window
6831 containing the pointer. */
6832 {
6833 Window win, child;
6834 int win_x, win_y;
06a2c219 6835 int parent_x = 0, parent_y = 0;
e99db5a1 6836 int count;
12ba150f
JB
6837
6838 win = root;
69388238 6839
2d7fc7e8
RS
6840 /* XTranslateCoordinates can get errors if the window
6841 structure is changing at the same time this function
6842 is running. So at least we must not crash from them. */
6843
e99db5a1 6844 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
2d7fc7e8 6845
334208b7 6846 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
23faf38f 6847 && FRAME_LIVE_P (last_mouse_frame))
12ba150f 6848 {
69388238
RS
6849 /* If mouse was grabbed on a frame, give coords for that frame
6850 even if the mouse is now outside it. */
334208b7 6851 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
69388238 6852
12ba150f 6853 /* From-window, to-window. */
69388238 6854 root, FRAME_X_WINDOW (last_mouse_frame),
12ba150f
JB
6855
6856 /* From-position, to-position. */
6857 root_x, root_y, &win_x, &win_y,
6858
6859 /* Child of win. */
6860 &child);
69388238
RS
6861 f1 = last_mouse_frame;
6862 }
6863 else
6864 {
6865 while (1)
6866 {
334208b7 6867 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
12ba150f 6868
69388238
RS
6869 /* From-window, to-window. */
6870 root, win,
12ba150f 6871
69388238
RS
6872 /* From-position, to-position. */
6873 root_x, root_y, &win_x, &win_y,
6874
6875 /* Child of win. */
6876 &child);
6877
9af3143a 6878 if (child == None || child == win)
69388238
RS
6879 break;
6880
6881 win = child;
6882 parent_x = win_x;
6883 parent_y = win_y;
6884 }
12ba150f 6885
69388238
RS
6886 /* Now we know that:
6887 win is the innermost window containing the pointer
6888 (XTC says it has no child containing the pointer),
6889 win_x and win_y are the pointer's position in it
6890 (XTC did this the last time through), and
6891 parent_x and parent_y are the pointer's position in win's parent.
6892 (They are what win_x and win_y were when win was child.
6893 If win is the root window, it has no parent, and
6894 parent_{x,y} are invalid, but that's okay, because we'll
6895 never use them in that case.) */
6896
6897 /* Is win one of our frames? */
19126e11 6898 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
69388238 6899 }
58769bee 6900
2d7fc7e8
RS
6901 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
6902 f1 = 0;
6903
e99db5a1 6904 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
2d7fc7e8 6905
ab648270 6906 /* If not, is it one of our scroll bars? */
a135645a 6907 if (! f1)
12ba150f 6908 {
ab648270 6909 struct scroll_bar *bar = x_window_to_scroll_bar (win);
12ba150f
JB
6910
6911 if (bar)
6912 {
a135645a 6913 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
6914 win_x = parent_x;
6915 win_y = parent_y;
6916 }
6917 }
90e65f07 6918
8bcee03e 6919 if (f1 == 0 && insist > 0)
1cf412ec
RS
6920 f1 = selected_frame;
6921
a135645a 6922 if (f1)
12ba150f 6923 {
06a2c219
GM
6924 /* Ok, we found a frame. Store all the values.
6925 last_mouse_glyph is a rectangle used to reduce the
6926 generation of mouse events. To not miss any motion
6927 events, we must divide the frame into rectangles of the
6928 size of the smallest character that could be displayed
6929 on it, i.e. into the same rectangles that matrices on
6930 the frame are divided into. */
6931
6932#if OLD_REDISPLAY_CODE
2b5c9e71 6933 int ignore1, ignore2;
2b5c9e71 6934 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
334208b7 6935 &last_mouse_glyph,
1cf412ec
RS
6936 FRAME_X_DISPLAY_INFO (f1)->grabbed
6937 || insist);
06a2c219
GM
6938#else
6939 {
6940 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
6941 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
6942 int x = win_x;
6943 int y = win_y;
6944
6945 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
6946 round down even for negative values. */
6947 if (x < 0)
6948 x -= width - 1;
6949 if (y < 0)
6950 y -= height - 1;
6951
6952 last_mouse_glyph.width = width;
6953 last_mouse_glyph.height = height;
6954 last_mouse_glyph.x = (x + width - 1) / width * width;
6955 last_mouse_glyph.y = (y + height - 1) / height * height;
6956 }
6957#endif
12ba150f
JB
6958
6959 *bar_window = Qnil;
6960 *part = 0;
334208b7 6961 *fp = f1;
e0c1aef2
KH
6962 XSETINT (*x, win_x);
6963 XSETINT (*y, win_y);
12ba150f
JB
6964 *time = last_mouse_movement_time;
6965 }
6966 }
6967 }
90e65f07
JB
6968
6969 UNBLOCK_INPUT;
6970}
f451eb13 6971
06a2c219
GM
6972
6973DEFUN ("xt-process-timeouts", Fxt_process_timeouts, Sxt_process_timeouts,
6974 0, 0, 0,
6975 "Arrange for Xt timeout callbacks to be called.")
6976 ()
6977{
6978#ifdef USE_X_TOOLKIT
6979 BLOCK_INPUT;
6980 while (XtAppPending (Xt_app_con) & XtIMTimer)
6981 XtAppProcessEvent (Xt_app_con, XtIMTimer);
6982 UNBLOCK_INPUT;
6983#endif /* USE_X_TOOLKIT */
6984
6985 return Qnil;
6986}
6987
6988
6989\f
6990/* Scroll bar support. */
6991
6992/* Given an X window ID, find the struct scroll_bar which manages it.
6993 This can be called in GC, so we have to make sure to strip off mark
6994 bits. */
6995static struct scroll_bar *
6996x_window_to_scroll_bar (window_id)
6997 Window window_id;
6998{
6999 Lisp_Object tail;
7000
7001 for (tail = Vframe_list;
7002 XGCTYPE (tail) == Lisp_Cons;
7003 tail = XCONS (tail)->cdr)
7004 {
7005 Lisp_Object frame, bar, condemned;
7006
7007 frame = XCONS (tail)->car;
7008 /* All elements of Vframe_list should be frames. */
7009 if (! GC_FRAMEP (frame))
7010 abort ();
7011
7012 /* Scan this frame's scroll bar list for a scroll bar with the
7013 right window ID. */
7014 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7015 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7016 /* This trick allows us to search both the ordinary and
7017 condemned scroll bar lists with one loop. */
7018 ! GC_NILP (bar) || (bar = condemned,
7019 condemned = Qnil,
7020 ! GC_NILP (bar));
7021 bar = XSCROLL_BAR (bar)->next)
7022 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7023 return XSCROLL_BAR (bar);
7024 }
7025
7026 return 0;
7027}
7028
7029
7030\f
7031/************************************************************************
7032 Toolkit scroll bars
7033 ************************************************************************/
7034
7035#if USE_TOOLKIT_SCROLL_BARS
7036
7037static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7038static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7039static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7040 struct scroll_bar *));
7041static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7042 int, int, int));
7043
7044
7045/* Id of action hook installed for scroll bars. */
7046
7047static XtActionHookId action_hook_id;
7048
7049/* Lisp window being scrolled. Set when starting to interact with
7050 a toolkit scroll bar, reset to nil when ending the interaction. */
7051
7052static Lisp_Object window_being_scrolled;
7053
7054/* Last scroll bar part sent in xm_scroll_callback. */
7055
7056static int last_scroll_bar_part;
7057
7058
7059/* Action hook installed via XtAppAddActionHook when toolkit scroll
7060 bars are used.. The hoos is responsible for detecting when
7061 the user ends an interaction with the scroll bar, and generates
7062 a `end-scroll' scroll_bar_click' event if so. */
7063
7064static void
7065xt_action_hook (widget, client_data, action_name, event, params,
7066 num_params)
7067 Widget widget;
7068 XtPointer client_data;
7069 String action_name;
7070 XEvent *event;
7071 String *params;
7072 Cardinal *num_params;
7073{
7074 int scroll_bar_p;
7075 char *end_action;
7076
7077#ifdef USE_MOTIF
7078 scroll_bar_p = XmIsScrollBar (widget);
7079 end_action = "Release";
7080#elif defined HAVE_XAW3D
7081 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7082 end_action = "EndScroll";
7083#else
7084#error unknown scroll bar toolkit
7085#endif /* HAVE_XAW3D */
7086
7087 /* Although LessTif uses XtTimeouts like Xaw3d, the timer hack to
7088 let Xt timeouts be processed doesn't work. */
7089 if (scroll_bar_p
7090 && strcmp (action_name, end_action) == 0
7091 && WINDOWP (window_being_scrolled))
7092 {
7093 struct window *w;
7094
7095 x_send_scroll_bar_event (window_being_scrolled,
7096 scroll_bar_end_scroll, 0, 0);
7097 w = XWINDOW (window_being_scrolled);
7098 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7099 window_being_scrolled = Qnil;
7100 last_scroll_bar_part = -1;
7101 }
7102}
7103
7104
7105/* Send a client message with message type Xatom_Scrollbar for a
7106 scroll action to the frame of WINDOW. PART is a value identifying
7107 the part of the scroll bar that was clicked on. PORTION is the
7108 amount to scroll of a whole of WHOLE. */
7109
7110static void
7111x_send_scroll_bar_event (window, part, portion, whole)
7112 Lisp_Object window;
7113 int part, portion, whole;
7114{
7115 XEvent event;
7116 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
7117 struct frame *f = XFRAME (XWINDOW (window)->frame);
7118
7119 /* Construct a ClientMessage event to send to the frame. */
7120 ev->type = ClientMessage;
7121 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7122 ev->display = FRAME_X_DISPLAY (f);
7123 ev->window = FRAME_X_WINDOW (f);
7124 ev->format = 32;
7125 ev->data.l[0] = (long) window;
7126 ev->data.l[1] = (long) part;
7127 ev->data.l[2] = (long) 0;
7128 ev->data.l[3] = (long) portion;
7129 ev->data.l[4] = (long) whole;
7130
7131 /* Setting the event mask to zero means that the message will
7132 be sent to the client that created the window, and if that
7133 window no longer exists, no event will be sent. */
7134 BLOCK_INPUT;
7135 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7136 UNBLOCK_INPUT;
7137}
7138
7139
7140/* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7141 in *IEVENT. */
7142
7143static void
7144x_scroll_bar_to_input_event (event, ievent)
7145 XEvent *event;
7146 struct input_event *ievent;
7147{
7148 XClientMessageEvent *ev = (XClientMessageEvent *) event;
7149 Lisp_Object window = (Lisp_Object) ev->data.l[0];
7150 struct frame *f = XFRAME (XWINDOW (window)->frame);
7151
7152 ievent->kind = scroll_bar_click;
7153 ievent->frame_or_window = window;
7154 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7155 ievent->part = ev->data.l[1];
7156 ievent->code = ev->data.l[2];
7157 ievent->x = make_number ((int) ev->data.l[3]);
7158 ievent->y = make_number ((int) ev->data.l[4]);
7159 ievent->modifiers = 0;
7160}
7161
7162
7163#ifdef USE_MOTIF
7164
7165/* Minimum and maximum values used for Motif scroll bars. */
7166
7167#define XM_SB_MIN 1
7168#define XM_SB_MAX 10000000
7169#define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7170
7171
7172/* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7173 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7174 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7175
7176static void
7177xm_scroll_callback (widget, client_data, call_data)
7178 Widget widget;
7179 XtPointer client_data, call_data;
7180{
7181 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7182 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7183 double percent;
7184 int part = -1, whole = 0, portion = 0;
7185
7186 switch (cs->reason)
7187 {
7188 case XmCR_DECREMENT:
7189 bar->dragging = Qnil;
7190 part = scroll_bar_up_arrow;
7191 break;
7192
7193 case XmCR_INCREMENT:
7194 bar->dragging = Qnil;
7195 part = scroll_bar_down_arrow;
7196 break;
7197
7198 case XmCR_PAGE_DECREMENT:
7199 bar->dragging = Qnil;
7200 part = scroll_bar_above_handle;
7201 break;
7202
7203 case XmCR_PAGE_INCREMENT:
7204 bar->dragging = Qnil;
7205 part = scroll_bar_below_handle;
7206 break;
7207
7208 case XmCR_TO_TOP:
7209 bar->dragging = Qnil;
7210 part = scroll_bar_to_top;
7211 break;
7212
7213 case XmCR_TO_BOTTOM:
7214 bar->dragging = Qnil;
7215 part = scroll_bar_to_bottom;
7216 break;
7217
7218 case XmCR_DRAG:
7219 {
7220 int slider_size;
7221 int dragging_down_p = (INTEGERP (bar->dragging)
7222 && XINT (bar->dragging) <= cs->value);
7223
7224 /* Get the slider size. */
7225 BLOCK_INPUT;
7226 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7227 UNBLOCK_INPUT;
7228
7229 /* At the max position of the scroll bar, do a line-wise
7230 movement. Without doing anything, the LessTif scroll bar
7231 calls us with the same cs->value again and again. If we
7232 want to make sure that we can reach the end of the buffer,
7233 we have to do something.
7234
7235 Implementation note: setting bar->dragging always to
7236 cs->value gives a smoother movement at the max position.
7237 Setting it to nil when doing line-wise movement gives
7238 a better slider behavior. */
7239
7240 if (cs->value + slider_size == XM_SB_MAX
7241 || (dragging_down_p
7242 && last_scroll_bar_part == scroll_bar_down_arrow))
7243 {
7244 part = scroll_bar_down_arrow;
7245 bar->dragging = Qnil;
7246 }
7247 else
7248 {
7249 whole = XM_SB_RANGE;
7250 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7251 part = scroll_bar_handle;
7252 bar->dragging = make_number (cs->value);
7253 }
7254 }
7255 break;
7256
7257 case XmCR_VALUE_CHANGED:
7258 break;
7259 };
7260
7261 if (part >= 0)
7262 {
7263 window_being_scrolled = bar->window;
7264 last_scroll_bar_part = part;
7265 x_send_scroll_bar_event (bar->window, part, portion, whole);
7266 }
7267}
7268
7269
7270#else /* not USE_MOTIF, i.e. XAW3D. */
7271
7272
7273/* Xaw3d scroll bar callback. Invoked when the thumb is dragged.
7274 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7275 scroll bar struct. CALL_DATA is a pointer to a float saying where
7276 the thumb is. */
7277
7278static void
7279xaw3d_jump_callback (widget, client_data, call_data)
7280 Widget widget;
7281 XtPointer client_data, call_data;
7282{
7283 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7284 float top = *(float *) call_data;
7285 float shown;
7286 int whole, portion;
7287 int dragging_down_p, part;
7288 double epsilon = 0.01;
7289
7290 /* Get the size of the thumb, a value between 0 and 1. */
7291 BLOCK_INPUT;
7292 XtVaGetValues (widget, XtNshown, &shown, NULL);
7293 UNBLOCK_INPUT;
7294
7295 whole = 10000000;
7296 portion = shown < 1 ? top * whole : 0;
7297 dragging_down_p = (INTEGERP (bar->dragging)
7298 && XINT (bar->dragging) < portion);
7299
7300 if (shown < 1
7301 && (abs (top + shown - 1) < epsilon
7302 || (dragging_down_p
7303 && last_scroll_bar_part == scroll_bar_down_arrow)))
7304 part = scroll_bar_down_arrow;
7305 else
7306 part = scroll_bar_handle;
7307
7308 window_being_scrolled = bar->window;
7309 bar->dragging = make_number (portion);
7310 last_scroll_bar_part = part;
7311 x_send_scroll_bar_event (bar->window, part, portion, whole);
7312}
7313
7314
7315/* Xaw3d scroll bar callback. Invoked for incremental scrolling.,
7316 i.e. line or page up or down. WIDGET is the Xaw3d scroll bar
7317 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
7318 the scroll bar. CALL_DATA is an integer specifying the action that
7319 has taken place. It's magnitude is in the range 0..height of the
7320 scroll bar. Negative values mean scroll towards buffer start.
7321 Values < height of scroll bar mean line-wise movement. */
7322
7323static void
7324xaw3d_scroll_callback (widget, client_data, call_data)
7325 Widget widget;
7326 XtPointer client_data, call_data;
7327{
7328 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7329 int position = (int) call_data;
7330 Dimension height;
7331 int part;
7332
7333 /* Get the height of the scroll bar. */
7334 BLOCK_INPUT;
7335 XtVaGetValues (widget, XtNheight, &height, NULL);
7336 UNBLOCK_INPUT;
7337
7338 if (position < 0)
7339 {
7340 if (abs (position) < height)
7341 part = scroll_bar_up_arrow;
7342 else
7343 part = scroll_bar_above_handle;
7344 }
7345 else
7346 {
7347 if (abs (position) < height)
7348 part = scroll_bar_down_arrow;
7349 else
7350 part = scroll_bar_below_handle;
7351 }
7352
7353 window_being_scrolled = bar->window;
7354 bar->dragging = Qnil;
7355 last_scroll_bar_part = part;
7356 x_send_scroll_bar_event (bar->window, part, 0, 0);
7357}
7358
7359
7360#endif /* not USE_MOTIF */
7361
7362
7363/* Create the widget for scroll bar BAR on frame F. Record the widget
7364 and X window of the scroll bar in BAR. */
7365
7366static void
7367x_create_toolkit_scroll_bar (f, bar)
7368 struct frame *f;
7369 struct scroll_bar *bar;
7370{
7371 Window xwindow;
7372 Widget widget;
7373 Arg av[20];
7374 int ac = 0;
7375 char *scroll_bar_name = "verticalScrollBar";
7376 unsigned long pixel;
7377
7378 BLOCK_INPUT;
7379
7380#ifdef USE_MOTIF
7381 /* LessTif 0.85, problems:
7382
7383 1. When the mouse if over the scroll bar, the scroll bar will
7384 get keyboard events. I didn't find a way to turn this off.
7385
7386 2. Do we have to explicitly set the cursor to get an arrow
7387 cursor (see below)? */
7388
7389 /* Set resources. Create the widget. */
7390 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7391 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
7392 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
7393 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
7394 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
7395 XtSetArg (av[ac], XmNincrement, 1); ++ac;
7396 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
7397
7398 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7399 if (pixel != -1)
7400 {
7401 XtSetArg (av[ac], XmNforeground, pixel);
7402 ++ac;
7403 }
7404
7405 pixel = f->output_data.x->scroll_bar_background_pixel;
7406 if (pixel != -1)
7407 {
7408 XtSetArg (av[ac], XmNbackground, pixel);
7409 ++ac;
7410 }
7411
7412 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
7413 scroll_bar_name, av, ac);
7414
7415 /* Add one callback for everything that can happen. */
7416 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
7417 (XtPointer) bar);
7418 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
7419 (XtPointer) bar);
7420 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
7421 (XtPointer) bar);
7422 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
7423 (XtPointer) bar);
7424 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
7425 (XtPointer) bar);
7426 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
7427 (XtPointer) bar);
7428 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
7429 (XtPointer) bar);
7430
7431 /* Realize the widget. Only after that is the X window created. */
7432 XtRealizeWidget (widget);
7433
7434 /* Set the cursor to an arrow. I didn't find a resource to do that.
7435 And I'm wondering why it hasn't an arrow cursor by default. */
7436 XDefineCursor (XtDisplay (widget), XtWindow (widget),
7437 f->output_data.x->nontext_cursor);
7438
7439#elif defined HAVE_XAW3D
7440
7441 /* Set resources. Create the widget. The background of the
7442 Xaw3d scroll bar widget is a little bit light for my taste.
7443 We don't alter it here to let users change it according
7444 to their taste with `emacs*verticalScrollBar.background: xxx'. */
7445 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7446 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
7447 XtSetArg (av[ac], XtNcursorName, "left_ptr"); ++ac;
7448 XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac;
7449
7450 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7451 if (pixel != -1)
7452 {
7453 XtSetArg (av[ac], XtNforeground, pixel);
7454 ++ac;
7455 }
7456
7457 pixel = f->output_data.x->scroll_bar_background_pixel;
7458 if (pixel != -1)
7459 {
7460 XtSetArg (av[ac], XtNbackground, pixel);
7461 ++ac;
7462 }
7463
7464 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
7465 f->output_data.x->edit_widget, av, ac);
7466
7467 /* Define callbacks. */
7468 XtAddCallback (widget, XtNjumpProc, xaw3d_jump_callback, (XtPointer) bar);
7469 XtAddCallback (widget, XtNscrollProc, xaw3d_scroll_callback,
7470 (XtPointer) bar);
7471
7472 /* Realize the widget. Only after that is the X window created. */
7473 XtRealizeWidget (widget);
7474
7475#endif /* HAVE_XAW3D */
7476
7477 /* Install an action hook that let's us detect when the user
7478 finishes interacting with a scroll bar. */
7479 if (action_hook_id == 0)
7480 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
7481
7482 /* Remember X window and widget in the scroll bar vector. */
7483 SET_SCROLL_BAR_X_WIDGET (bar, widget);
7484 xwindow = XtWindow (widget);
7485 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
7486
7487 UNBLOCK_INPUT;
7488}
7489
7490
7491/* Set the thumb size and position of scroll bar BAR. We are currently
7492 displaying PORTION out of a whole WHOLE, and our position POSITION. */
7493
7494static void
7495x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
7496 struct scroll_bar *bar;
7497 int portion, position, whole;
f451eb13 7498{
06a2c219
GM
7499 float top, shown;
7500 Arg av[2];
7501 Widget widget = SCROLL_BAR_X_WIDGET (bar);
f451eb13 7502
06a2c219
GM
7503 if (whole == 0)
7504 top = 0, shown = 1;
7505 else
f451eb13 7506 {
06a2c219
GM
7507 top = (float) position / whole;
7508 shown = (float) portion / whole;
7509 }
f451eb13 7510
06a2c219 7511 BLOCK_INPUT;
f451eb13 7512
06a2c219
GM
7513#ifdef USE_MOTIF
7514 {
7515 int size, value;
7516 Boolean arrow1_selected, arrow2_selected;
7517 unsigned char flags;
7518 XmScrollBarWidget sb;
7519
7520 /* Slider size. Must be in the range [1 .. MAX - MIN] where NAX
7521 is the scroll bar's maximum and MIN is the scroll bar's minimum
7522 value. */
7523 size = shown * XM_SB_RANGE;
7524 size = min (size, XM_SB_RANGE);
7525 size = max (size, 1);
7526
7527 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
7528 value = top * XM_SB_RANGE;
7529 value = min (value, XM_SB_MAX - size);
7530 value = max (value, XM_SB_MIN);
7531
7532 /* LessTif: Calling XmScrollBarSetValues after an increment or
7533 decrement turns off auto-repeat LessTif-internally. This can
7534 be seen in ScrollBar.c which resets Arrow1Selected and
7535 Arrow2Selected. It also sets internal flags so that LessTif
7536 believes the mouse is in the slider. We either have to change
7537 our code, or work around that by accessing private data. */
7538
7539 sb = (XmScrollBarWidget) widget;
7540 arrow1_selected = sb->scrollBar.arrow1_selected;
7541 arrow2_selected = sb->scrollBar.arrow2_selected;
7542 flags = sb->scrollBar.flags;
7543
7544 if (NILP (bar->dragging))
7545 XmScrollBarSetValues (widget, value, size, 0, 0, False);
7546 else if (last_scroll_bar_part == scroll_bar_down_arrow)
7547 /* This has the negative side effect that the slider value is
7548 not would it would be if we scrolled here using line-wise or
7549 page-wise movement. */
7550 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
7551 else
7552 {
7553 /* If currently dragging, only update the slider size.
7554 This reduces flicker effects. */
7555 int old_value, old_size, increment, page_increment;
7556
7557 XmScrollBarGetValues (widget, &old_value, &old_size,
7558 &increment, &page_increment);
7559 XmScrollBarSetValues (widget, old_value,
7560 min (size, XM_SB_RANGE - old_value),
7561 0, 0, False);
7562 }
7563
7564 sb->scrollBar.arrow1_selected = arrow1_selected;
7565 sb->scrollBar.arrow2_selected = arrow2_selected;
7566 sb->scrollBar.flags = flags;
7567 }
7568#elif defined HAVE_XAW3D
7569 {
7570 /* Restrict to [0 1]. */
7571 top = max (0, min (1, top));
7572 shown = max (0, min (1, shown));
7573
7574 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
7575 check that your system's configuration file contains a define
7576 for `NARROWPROTO'. See s/freebsd.h for an example. */
7577 if (NILP (bar->dragging))
eb393530
GM
7578 {
7579 float old_top, old_shown;
7580 XtVaGetValues (widget, XtNtopOfThumb, &old_top, XtNshown, &old_shown,
7581 NULL);
7582 if (top != old_top || shown != old_shown)
7583 XawScrollbarSetThumb (widget, top, shown);
7584 }
06a2c219
GM
7585 else
7586 {
7587 ScrollbarWidget sb = (ScrollbarWidget) widget;
7588 int scroll_mode = sb->scrollbar.scroll_mode;
f451eb13 7589
06a2c219
GM
7590 sb->scrollbar.scroll_mode = 0;
7591
7592 if (last_scroll_bar_part == scroll_bar_down_arrow)
7593 XawScrollbarSetThumb (widget, top, 1 - top);
7594 else
7595 {
7596 float old_top;
7597 XtVaGetValues (widget, XtNtopOfThumb, &old_top, NULL);
7598 XawScrollbarSetThumb (widget, old_top, min (shown, 1 - old_top));
7599 }
7600
7601 sb->scrollbar.scroll_mode = scroll_mode;
7602 }
7603 }
7604#endif /* HAVE_XAW3D */
7605
7606 UNBLOCK_INPUT;
f451eb13
JB
7607}
7608
06a2c219
GM
7609#endif /* USE_TOOLKIT_SCROLL_BARS */
7610
7611
7612\f
7613/************************************************************************
7614 Scroll bars, general
7615 ************************************************************************/
7616
7617/* Create a scroll bar and return the scroll bar vector for it. W is
7618 the Emacs window on which to create the scroll bar. TOP, LEFT,
7619 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
7620 scroll bar. */
7621
ab648270 7622static struct scroll_bar *
06a2c219
GM
7623x_scroll_bar_create (w, top, left, width, height)
7624 struct window *w;
f451eb13
JB
7625 int top, left, width, height;
7626{
06a2c219
GM
7627 struct frame *f = XFRAME (w->frame);
7628#ifdef USE_X_TOOLKIT
7629 Arg av[10];
7630#endif
7631 int ac = 0;
7632 Window window;
334208b7
RS
7633 struct scroll_bar *bar
7634 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
f451eb13
JB
7635
7636 BLOCK_INPUT;
7637
06a2c219
GM
7638#if USE_TOOLKIT_SCROLL_BARS
7639 x_create_toolkit_scroll_bar (f, bar);
7640#else /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
7641 {
7642 XSetWindowAttributes a;
7643 unsigned long mask;
06a2c219
GM
7644
7645 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
7646 if (a.background_pixel == -1)
7647 a.background_pixel = f->output_data.x->background_pixel;
7648
12ba150f 7649 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9a572e2a 7650 | ButtonMotionMask | PointerMotionHintMask
12ba150f 7651 | ExposureMask);
7a13e894 7652 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
f451eb13 7653
dbc4e1c1 7654 mask = (CWBackPixel | CWEventMask | CWCursor);
f451eb13 7655
06a2c219
GM
7656 /* Clear the area of W that will serve as a scroll bar. This is
7657 for the case that a window has been split horizontally. In
7658 this case, no clear_frame is generated to reduce flickering. */
7659 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7660 left, top, width,
7661 window_box_height (w), False);
7662
7663 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7664 /* Position and size of scroll bar. */
7665 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7666 top,
7667 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
7668 height,
7669 /* Border width, depth, class, and visual. */
7670 0,
7671 CopyFromParent,
7672 CopyFromParent,
7673 CopyFromParent,
7674 /* Attributes. */
7675 mask, &a);
7676 SET_SCROLL_BAR_X_WINDOW (bar, window);
f451eb13 7677 }
06a2c219 7678#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 7679
06a2c219 7680 XSETWINDOW (bar->window, w);
e0c1aef2
KH
7681 XSETINT (bar->top, top);
7682 XSETINT (bar->left, left);
7683 XSETINT (bar->width, width);
7684 XSETINT (bar->height, height);
7685 XSETINT (bar->start, 0);
7686 XSETINT (bar->end, 0);
12ba150f 7687 bar->dragging = Qnil;
f451eb13
JB
7688
7689 /* Add bar to its frame's list of scroll bars. */
334208b7 7690 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 7691 bar->prev = Qnil;
334208b7 7692 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
06a2c219 7693 if (!NILP (bar->next))
e0c1aef2 7694 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13 7695
06a2c219
GM
7696 /* Map the window/widget. */
7697#if USE_TOOLKIT_SCROLL_BARS
7698 XtMapWidget (SCROLL_BAR_X_WIDGET (bar));
7699 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
7700 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7701 top,
7702 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
7703 height, 0);
7704#else /* not USE_TOOLKIT_SCROLL_BARS */
7f9c7f94 7705 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
06a2c219 7706#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
7707
7708 UNBLOCK_INPUT;
12ba150f 7709 return bar;
f451eb13
JB
7710}
7711
06a2c219 7712
12ba150f 7713/* Draw BAR's handle in the proper position.
06a2c219 7714
12ba150f
JB
7715 If the handle is already drawn from START to END, don't bother
7716 redrawing it, unless REBUILD is non-zero; in that case, always
7717 redraw it. (REBUILD is handy for drawing the handle after expose
58769bee 7718 events.)
12ba150f
JB
7719
7720 Normally, we want to constrain the start and end of the handle to
06a2c219
GM
7721 fit inside its rectangle, but if the user is dragging the scroll
7722 bar handle, we want to let them drag it down all the way, so that
7723 the bar's top is as far down as it goes; otherwise, there's no way
7724 to move to the very end of the buffer. */
7725
f451eb13 7726static void
ab648270
JB
7727x_scroll_bar_set_handle (bar, start, end, rebuild)
7728 struct scroll_bar *bar;
f451eb13 7729 int start, end;
12ba150f 7730 int rebuild;
f451eb13 7731{
06a2c219 7732#ifndef USE_TOOLKIT_SCROLL_BARS
12ba150f 7733 int dragging = ! NILP (bar->dragging);
ab648270 7734 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 7735 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 7736 GC gc = f->output_data.x->normal_gc;
12ba150f
JB
7737
7738 /* If the display is already accurate, do nothing. */
7739 if (! rebuild
7740 && start == XINT (bar->start)
7741 && end == XINT (bar->end))
7742 return;
7743
f451eb13
JB
7744 BLOCK_INPUT;
7745
7746 {
d9cdbb3d
RS
7747 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
7748 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
7749 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
f451eb13
JB
7750
7751 /* Make sure the values are reasonable, and try to preserve
7752 the distance between start and end. */
12ba150f
JB
7753 {
7754 int length = end - start;
7755
7756 if (start < 0)
7757 start = 0;
7758 else if (start > top_range)
7759 start = top_range;
7760 end = start + length;
7761
7762 if (end < start)
7763 end = start;
7764 else if (end > top_range && ! dragging)
7765 end = top_range;
7766 }
f451eb13 7767
ab648270 7768 /* Store the adjusted setting in the scroll bar. */
e0c1aef2
KH
7769 XSETINT (bar->start, start);
7770 XSETINT (bar->end, end);
f451eb13 7771
12ba150f
JB
7772 /* Clip the end position, just for display. */
7773 if (end > top_range)
7774 end = top_range;
f451eb13 7775
ab648270 7776 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
12ba150f
JB
7777 below top positions, to make sure the handle is always at least
7778 that many pixels tall. */
ab648270 7779 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
f451eb13 7780
12ba150f
JB
7781 /* Draw the empty space above the handle. Note that we can't clear
7782 zero-height areas; that means "clear to end of window." */
7783 if (0 < start)
334208b7 7784 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 7785
12ba150f 7786 /* x, y, width, height, and exposures. */
ab648270
JB
7787 VERTICAL_SCROLL_BAR_LEFT_BORDER,
7788 VERTICAL_SCROLL_BAR_TOP_BORDER,
12ba150f
JB
7789 inside_width, start,
7790 False);
f451eb13 7791
06a2c219
GM
7792 /* Change to proper foreground color if one is specified. */
7793 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
7794 XSetForeground (FRAME_X_DISPLAY (f), gc,
7795 f->output_data.x->scroll_bar_foreground_pixel);
7796
12ba150f 7797 /* Draw the handle itself. */
334208b7 7798 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13 7799
12ba150f 7800 /* x, y, width, height */
ab648270
JB
7801 VERTICAL_SCROLL_BAR_LEFT_BORDER,
7802 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
12ba150f 7803 inside_width, end - start);
f451eb13 7804
06a2c219
GM
7805 /* Restore the foreground color of the GC if we changed it above. */
7806 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
7807 XSetForeground (FRAME_X_DISPLAY (f), gc,
7808 f->output_data.x->foreground_pixel);
f451eb13 7809
12ba150f
JB
7810 /* Draw the empty space below the handle. Note that we can't
7811 clear zero-height areas; that means "clear to end of window." */
7812 if (end < inside_height)
334208b7 7813 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 7814
12ba150f 7815 /* x, y, width, height, and exposures. */
ab648270
JB
7816 VERTICAL_SCROLL_BAR_LEFT_BORDER,
7817 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
12ba150f
JB
7818 inside_width, inside_height - end,
7819 False);
f451eb13 7820
f451eb13
JB
7821 }
7822
f451eb13 7823 UNBLOCK_INPUT;
06a2c219 7824#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
7825}
7826
f451eb13 7827
06a2c219
GM
7828/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
7829 nil. */
58769bee 7830
12ba150f 7831static void
ab648270
JB
7832x_scroll_bar_remove (bar)
7833 struct scroll_bar *bar;
12ba150f
JB
7834{
7835 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7836
7837 BLOCK_INPUT;
7838
06a2c219
GM
7839#if USE_TOOLKIT_SCROLL_BARS
7840 XtDestroyWidget (SCROLL_BAR_X_WIDGET (bar));
7841#else /* not USE_TOOLKIT_SCROLL_BARS */
334208b7 7842 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
06a2c219
GM
7843#endif /* not USE_TOOLKIT_SCROLL_BARS */
7844
ab648270
JB
7845 /* Disassociate this scroll bar from its window. */
7846 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
12ba150f
JB
7847
7848 UNBLOCK_INPUT;
7849}
7850
06a2c219 7851
12ba150f
JB
7852/* Set the handle of the vertical scroll bar for WINDOW to indicate
7853 that we are displaying PORTION characters out of a total of WHOLE
ab648270 7854 characters, starting at POSITION. If WINDOW has no scroll bar,
12ba150f 7855 create one. */
06a2c219 7856
12ba150f 7857static void
06a2c219
GM
7858XTset_vertical_scroll_bar (w, portion, whole, position)
7859 struct window *w;
f451eb13
JB
7860 int portion, whole, position;
7861{
06a2c219 7862 struct frame *f = XFRAME (w->frame);
ab648270 7863 struct scroll_bar *bar;
06a2c219
GM
7864 int pixel_top, pixel_left, pixel_width, pixel_height;
7865 int window_x, window_y, window_width, window_height;
7866 int scroll_bar_area_width;
7867
7868 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
7869
7870 /* Where should this scroll bar be, pixel-wise? */
7871 pixel_top = window_y;
7872 pixel_height = window_height;
7873
7874 /* The width of the scroll bar itself. */
7875 pixel_width = (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
7876 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7877 : (FRAME_SCROLL_BAR_COLS (f)
7878 * FONT_WIDTH (FRAME_FONT (f))));
7879
7880 /* The width on the screen reserved for the scroll bar plus maybe
7881 some empty room at both sides of the scroll bar. */
7882 scroll_bar_area_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
7883
7884 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
7885 pixel_left = (window_x
7886 + window_width
7887 + FRAME_FLAGS_AREA_WIDTH (f)
7888 + scroll_bar_area_width
7889 - pixel_width + 1);
7890 else
7891 pixel_left = (window_x
7892 - FRAME_FLAGS_AREA_WIDTH (f)
7893 - scroll_bar_area_width);
12ba150f 7894
ab648270 7895 /* Does the scroll bar exist yet? */
06a2c219
GM
7896 if (NILP (w->vertical_scroll_bar))
7897 bar = x_scroll_bar_create (w, pixel_top, pixel_left, pixel_width,
7898 pixel_height);
f451eb13 7899 else
12ba150f
JB
7900 {
7901 /* It may just need to be moved and resized. */
06a2c219
GM
7902 unsigned int mask = 0;
7903
7904 bar = XSCROLL_BAR (w->vertical_scroll_bar);
7905
7906 BLOCK_INPUT;
7907
7908 if (pixel_left != XINT (bar->left))
7909 mask |= CWX;
7910 if (pixel_top != XINT (bar->top))
7911 mask |= CWY;
7912 if (pixel_width != XINT (bar->width))
7913 mask |= CWWidth;
7914 if (pixel_height != XINT (bar->height))
7915 mask |= CWHeight;
7916
7917#ifdef USE_TOOLKIT_SCROLL_BARS
fe6f39d9
GM
7918
7919 /* Since toolkit scroll bars are smaller than the space reserved
7920 for them on the frame, we have to clear "under" them. */
7921 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7922 pixel_left, pixel_top,
7923 pixel_width + 1, pixel_height, False);
06a2c219
GM
7924
7925 /* Move/size the scroll bar widget. */
7926 if (mask)
7927 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
7928 pixel_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7929 pixel_top,
7930 pixel_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
7931 pixel_height, 0);
7932
7933#else /* not USE_TOOLKIT_SCROLL_BARS */
7934
7935 /* Clear areas not covered by the scroll bar. This makes sure a
7936 previous mode line display is cleared after C-x 2 C-x 1, for
7937 example. Non-toolkit scroll bars are as wide as the area
7938 reserved for scroll bars - trim at both sides. */
7939 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7940 pixel_left, pixel_top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7941 pixel_height, False);
7942 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7943 (pixel_left
7944 + pixel_width
7945 - VERTICAL_SCROLL_BAR_WIDTH_TRIM),
7946 pixel_top,
7947 VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7948 pixel_height, False);
7949
7950 /* Move/size the scroll bar window. */
7951 if (mask)
7952 {
7953 XWindowChanges wc;
7954
7955 wc.x = pixel_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
7956 wc.y = pixel_top;
7957 wc.width = pixel_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
7958 wc.height = pixel_height;
7959 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
7960 mask, &wc);
7961 }
7962
7963#endif /* not USE_TOOLKIT_SCROLL_BARS */
7964
7965 /* Remember new settings. */
7966 XSETINT (bar->left, pixel_left);
7967 XSETINT (bar->top, pixel_top);
7968 XSETINT (bar->width, pixel_width);
7969 XSETINT (bar->height, pixel_height);
7970
7971 UNBLOCK_INPUT;
12ba150f 7972 }
f451eb13 7973
06a2c219
GM
7974#if USE_TOOLKIT_SCROLL_BARS
7975 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
7976#else /* not USE_TOOLKIT_SCROLL_BARS */
ab648270 7977 /* Set the scroll bar's current state, unless we're currently being
f451eb13 7978 dragged. */
12ba150f 7979 if (NILP (bar->dragging))
f451eb13 7980 {
d9cdbb3d 7981 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, pixel_height);
f451eb13 7982
12ba150f 7983 if (whole == 0)
ab648270 7984 x_scroll_bar_set_handle (bar, 0, top_range, 0);
12ba150f
JB
7985 else
7986 {
43f868f5
JB
7987 int start = ((double) position * top_range) / whole;
7988 int end = ((double) (position + portion) * top_range) / whole;
ab648270 7989 x_scroll_bar_set_handle (bar, start, end, 0);
12ba150f 7990 }
f451eb13 7991 }
06a2c219 7992#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 7993
06a2c219 7994 XSETVECTOR (w->vertical_scroll_bar, bar);
f451eb13
JB
7995}
7996
12ba150f 7997
f451eb13 7998/* The following three hooks are used when we're doing a thorough
ab648270 7999 redisplay of the frame. We don't explicitly know which scroll bars
f451eb13 8000 are going to be deleted, because keeping track of when windows go
12ba150f
JB
8001 away is a real pain - "Can you say set-window-configuration, boys
8002 and girls?" Instead, we just assert at the beginning of redisplay
ab648270 8003 that *all* scroll bars are to be removed, and then save a scroll bar
12ba150f 8004 from the fiery pit when we actually redisplay its window. */
f451eb13 8005
ab648270
JB
8006/* Arrange for all scroll bars on FRAME to be removed at the next call
8007 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
06a2c219
GM
8008 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8009
58769bee 8010static void
ab648270 8011XTcondemn_scroll_bars (frame)
f451eb13
JB
8012 FRAME_PTR frame;
8013{
f9e24cb9
RS
8014 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8015 while (! NILP (FRAME_SCROLL_BARS (frame)))
8016 {
8017 Lisp_Object bar;
8018 bar = FRAME_SCROLL_BARS (frame);
8019 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8020 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8021 XSCROLL_BAR (bar)->prev = Qnil;
8022 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8023 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8024 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8025 }
f451eb13
JB
8026}
8027
06a2c219 8028/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
12ba150f 8029 Note that WINDOW isn't necessarily condemned at all. */
f451eb13 8030static void
ab648270 8031XTredeem_scroll_bar (window)
12ba150f 8032 struct window *window;
f451eb13 8033{
ab648270 8034 struct scroll_bar *bar;
12ba150f 8035
ab648270
JB
8036 /* We can't redeem this window's scroll bar if it doesn't have one. */
8037 if (NILP (window->vertical_scroll_bar))
12ba150f
JB
8038 abort ();
8039
ab648270 8040 bar = XSCROLL_BAR (window->vertical_scroll_bar);
12ba150f
JB
8041
8042 /* Unlink it from the condemned list. */
8043 {
8044 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
8045
8046 if (NILP (bar->prev))
8047 {
8048 /* If the prev pointer is nil, it must be the first in one of
8049 the lists. */
ab648270 8050 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
12ba150f
JB
8051 /* It's not condemned. Everything's fine. */
8052 return;
ab648270
JB
8053 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8054 window->vertical_scroll_bar))
8055 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
12ba150f
JB
8056 else
8057 /* If its prev pointer is nil, it must be at the front of
8058 one or the other! */
8059 abort ();
8060 }
8061 else
ab648270 8062 XSCROLL_BAR (bar->prev)->next = bar->next;
12ba150f
JB
8063
8064 if (! NILP (bar->next))
ab648270 8065 XSCROLL_BAR (bar->next)->prev = bar->prev;
12ba150f 8066
ab648270 8067 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 8068 bar->prev = Qnil;
e0c1aef2 8069 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
12ba150f 8070 if (! NILP (bar->next))
e0c1aef2 8071 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
12ba150f 8072 }
f451eb13
JB
8073}
8074
ab648270
JB
8075/* Remove all scroll bars on FRAME that haven't been saved since the
8076 last call to `*condemn_scroll_bars_hook'. */
06a2c219 8077
f451eb13 8078static void
ab648270 8079XTjudge_scroll_bars (f)
12ba150f 8080 FRAME_PTR f;
f451eb13 8081{
12ba150f 8082 Lisp_Object bar, next;
f451eb13 8083
ab648270 8084 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
cf7cb199
JB
8085
8086 /* Clear out the condemned list now so we won't try to process any
ab648270
JB
8087 more events on the hapless scroll bars. */
8088 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
cf7cb199
JB
8089
8090 for (; ! NILP (bar); bar = next)
f451eb13 8091 {
ab648270 8092 struct scroll_bar *b = XSCROLL_BAR (bar);
12ba150f 8093
ab648270 8094 x_scroll_bar_remove (b);
12ba150f
JB
8095
8096 next = b->next;
8097 b->next = b->prev = Qnil;
f451eb13 8098 }
12ba150f 8099
ab648270 8100 /* Now there should be no references to the condemned scroll bars,
12ba150f 8101 and they should get garbage-collected. */
f451eb13
JB
8102}
8103
8104
06a2c219
GM
8105/* Handle an Expose or GraphicsExpose event on a scroll bar. This
8106 is a no-op when using toolkit scroll bars.
ab648270
JB
8107
8108 This may be called from a signal handler, so we have to ignore GC
8109 mark bits. */
06a2c219 8110
f451eb13 8111static void
ab648270
JB
8112x_scroll_bar_expose (bar, event)
8113 struct scroll_bar *bar;
f451eb13
JB
8114 XEvent *event;
8115{
06a2c219
GM
8116#ifndef USE_TOOLKIT_SCROLL_BARS
8117
ab648270 8118 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8119 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8120 GC gc = f->output_data.x->normal_gc;
3cbd2e0b 8121 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
12ba150f 8122
f451eb13
JB
8123 BLOCK_INPUT;
8124
ab648270 8125 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
f451eb13 8126
06a2c219 8127 /* Draw a one-pixel border just inside the edges of the scroll bar. */
334208b7 8128 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13
JB
8129
8130 /* x, y, width, height */
d9cdbb3d 8131 0, 0,
3cbd2e0b 8132 XINT (bar->width) - 1 - width_trim - width_trim,
d9cdbb3d
RS
8133 XINT (bar->height) - 1);
8134
f451eb13 8135 UNBLOCK_INPUT;
06a2c219
GM
8136
8137#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8138}
8139
ab648270
JB
8140/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8141 is set to something other than no_event, it is enqueued.
8142
8143 This may be called from a signal handler, so we have to ignore GC
8144 mark bits. */
06a2c219 8145
f451eb13 8146static void
ab648270
JB
8147x_scroll_bar_handle_click (bar, event, emacs_event)
8148 struct scroll_bar *bar;
f451eb13
JB
8149 XEvent *event;
8150 struct input_event *emacs_event;
8151{
0299d313 8152 if (! GC_WINDOWP (bar->window))
12ba150f
JB
8153 abort ();
8154
ab648270 8155 emacs_event->kind = scroll_bar_click;
69388238 8156 emacs_event->code = event->xbutton.button - Button1;
0299d313
RS
8157 emacs_event->modifiers
8158 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8159 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8160 event->xbutton.state)
8161 | (event->type == ButtonRelease
8162 ? up_modifier
8163 : down_modifier));
12ba150f 8164 emacs_event->frame_or_window = bar->window;
f451eb13 8165 emacs_event->timestamp = event->xbutton.time;
12ba150f 8166 {
06a2c219 8167#if 0
d9cdbb3d 8168 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
0299d313 8169 int internal_height
d9cdbb3d 8170 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8171#endif
0299d313 8172 int top_range
d9cdbb3d 8173 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ab648270 8174 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
12ba150f
JB
8175
8176 if (y < 0) y = 0;
8177 if (y > top_range) y = top_range;
8178
8179 if (y < XINT (bar->start))
ab648270
JB
8180 emacs_event->part = scroll_bar_above_handle;
8181 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8182 emacs_event->part = scroll_bar_handle;
12ba150f 8183 else
ab648270 8184 emacs_event->part = scroll_bar_below_handle;
929787e1
JB
8185
8186 /* Just because the user has clicked on the handle doesn't mean
5116f055
JB
8187 they want to drag it. Lisp code needs to be able to decide
8188 whether or not we're dragging. */
929787e1 8189#if 0
12ba150f
JB
8190 /* If the user has just clicked on the handle, record where they're
8191 holding it. */
8192 if (event->type == ButtonPress
ab648270 8193 && emacs_event->part == scroll_bar_handle)
e0c1aef2 8194 XSETINT (bar->dragging, y - XINT (bar->start));
929787e1 8195#endif
12ba150f
JB
8196
8197 /* If the user has released the handle, set it to its final position. */
8198 if (event->type == ButtonRelease
8199 && ! NILP (bar->dragging))
8200 {
8201 int new_start = y - XINT (bar->dragging);
8202 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
f451eb13 8203
ab648270 8204 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
12ba150f
JB
8205 bar->dragging = Qnil;
8206 }
f451eb13 8207
5116f055
JB
8208 /* Same deal here as the other #if 0. */
8209#if 0
58769bee 8210 /* Clicks on the handle are always reported as occurring at the top of
12ba150f 8211 the handle. */
ab648270 8212 if (emacs_event->part == scroll_bar_handle)
12ba150f
JB
8213 emacs_event->x = bar->start;
8214 else
e0c1aef2 8215 XSETINT (emacs_event->x, y);
5116f055 8216#else
e0c1aef2 8217 XSETINT (emacs_event->x, y);
5116f055 8218#endif
f451eb13 8219
e0c1aef2 8220 XSETINT (emacs_event->y, top_range);
12ba150f
JB
8221 }
8222}
f451eb13 8223
ab648270
JB
8224/* Handle some mouse motion while someone is dragging the scroll bar.
8225
8226 This may be called from a signal handler, so we have to ignore GC
8227 mark bits. */
06a2c219 8228
f451eb13 8229static void
ab648270
JB
8230x_scroll_bar_note_movement (bar, event)
8231 struct scroll_bar *bar;
f451eb13
JB
8232 XEvent *event;
8233{
39d8bb4d
KH
8234 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8235
f451eb13
JB
8236 last_mouse_movement_time = event->xmotion.time;
8237
39d8bb4d 8238 f->mouse_moved = 1;
e0c1aef2 8239 XSETVECTOR (last_mouse_scroll_bar, bar);
f451eb13
JB
8240
8241 /* If we're dragging the bar, display it. */
ab648270 8242 if (! GC_NILP (bar->dragging))
f451eb13
JB
8243 {
8244 /* Where should the handle be now? */
12ba150f 8245 int new_start = event->xmotion.y - XINT (bar->dragging);
f451eb13 8246
12ba150f 8247 if (new_start != XINT (bar->start))
f451eb13 8248 {
12ba150f 8249 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
58769bee 8250
ab648270 8251 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
f451eb13
JB
8252 }
8253 }
f451eb13
JB
8254}
8255
12ba150f 8256/* Return information to the user about the current position of the mouse
ab648270 8257 on the scroll bar. */
06a2c219 8258
12ba150f 8259static void
334208b7
RS
8260x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8261 FRAME_PTR *fp;
12ba150f 8262 Lisp_Object *bar_window;
ab648270 8263 enum scroll_bar_part *part;
12ba150f
JB
8264 Lisp_Object *x, *y;
8265 unsigned long *time;
8266{
ab648270 8267 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
334208b7
RS
8268 Window w = SCROLL_BAR_X_WINDOW (bar);
8269 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f 8270 int win_x, win_y;
559cb2fb
JB
8271 Window dummy_window;
8272 int dummy_coord;
8273 unsigned int dummy_mask;
12ba150f 8274
cf7cb199
JB
8275 BLOCK_INPUT;
8276
ab648270 8277 /* Get the mouse's position relative to the scroll bar window, and
12ba150f 8278 report that. */
334208b7 8279 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
12ba150f 8280
559cb2fb
JB
8281 /* Root, child, root x and root y. */
8282 &dummy_window, &dummy_window,
8283 &dummy_coord, &dummy_coord,
12ba150f 8284
559cb2fb
JB
8285 /* Position relative to scroll bar. */
8286 &win_x, &win_y,
12ba150f 8287
559cb2fb
JB
8288 /* Mouse buttons and modifier keys. */
8289 &dummy_mask))
7a13e894 8290 ;
559cb2fb
JB
8291 else
8292 {
06a2c219 8293#if 0
559cb2fb 8294 int inside_height
d9cdbb3d 8295 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8296#endif
559cb2fb 8297 int top_range
d9cdbb3d 8298 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
559cb2fb
JB
8299
8300 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
8301
8302 if (! NILP (bar->dragging))
8303 win_y -= XINT (bar->dragging);
8304
8305 if (win_y < 0)
8306 win_y = 0;
8307 if (win_y > top_range)
8308 win_y = top_range;
8309
334208b7 8310 *fp = f;
7a13e894 8311 *bar_window = bar->window;
559cb2fb
JB
8312
8313 if (! NILP (bar->dragging))
8314 *part = scroll_bar_handle;
8315 else if (win_y < XINT (bar->start))
8316 *part = scroll_bar_above_handle;
8317 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8318 *part = scroll_bar_handle;
8319 else
8320 *part = scroll_bar_below_handle;
12ba150f 8321
e0c1aef2
KH
8322 XSETINT (*x, win_y);
8323 XSETINT (*y, top_range);
12ba150f 8324
39d8bb4d 8325 f->mouse_moved = 0;
559cb2fb
JB
8326 last_mouse_scroll_bar = Qnil;
8327 }
12ba150f 8328
559cb2fb 8329 *time = last_mouse_movement_time;
cf7cb199 8330
cf7cb199 8331 UNBLOCK_INPUT;
12ba150f
JB
8332}
8333
f451eb13 8334
dbc4e1c1 8335/* The screen has been cleared so we may have changed foreground or
ab648270
JB
8336 background colors, and the scroll bars may need to be redrawn.
8337 Clear out the scroll bars, and ask for expose events, so we can
dbc4e1c1
JB
8338 redraw them. */
8339
dfcf069d 8340void
ab648270 8341x_scroll_bar_clear (f)
dbc4e1c1
JB
8342 FRAME_PTR f;
8343{
06a2c219 8344#ifndef USE_TOOLKIT_SCROLL_BARS
dbc4e1c1
JB
8345 Lisp_Object bar;
8346
b80c363e
RS
8347 /* We can have scroll bars even if this is 0,
8348 if we just turned off scroll bar mode.
8349 But in that case we should not clear them. */
8350 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8351 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
8352 bar = XSCROLL_BAR (bar)->next)
8353 XClearArea (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
8354 0, 0, 0, 0, True);
06a2c219 8355#endif /* not USE_TOOLKIT_SCROLL_BARS */
dbc4e1c1
JB
8356}
8357
06a2c219 8358/* This processes Expose events from the menu-bar specific X event
19126e11 8359 loop in xmenu.c. This allows to redisplay the frame if necessary
06a2c219 8360 when handling menu-bar or pop-up items. */
3afe33e7 8361
06a2c219 8362int
3afe33e7
RS
8363process_expose_from_menu (event)
8364 XEvent event;
8365{
8366 FRAME_PTR f;
19126e11 8367 struct x_display_info *dpyinfo;
06a2c219 8368 int frame_exposed_p = 0;
3afe33e7 8369
f94397b5
KH
8370 BLOCK_INPUT;
8371
19126e11
KH
8372 dpyinfo = x_display_info_for_display (event.xexpose.display);
8373 f = x_window_to_frame (dpyinfo, event.xexpose.window);
3afe33e7
RS
8374 if (f)
8375 {
8376 if (f->async_visible == 0)
8377 {
8378 f->async_visible = 1;
8379 f->async_iconified = 0;
06c488fd 8380 f->output_data.x->has_been_visible = 1;
3afe33e7
RS
8381 SET_FRAME_GARBAGED (f);
8382 }
8383 else
8384 {
06a2c219
GM
8385 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
8386 event.xexpose.x, event.xexpose.y,
8387 event.xexpose.width, event.xexpose.height);
8388 frame_exposed_p = 1;
3afe33e7
RS
8389 }
8390 }
8391 else
8392 {
8393 struct scroll_bar *bar
8394 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 8395
3afe33e7
RS
8396 if (bar)
8397 x_scroll_bar_expose (bar, &event);
8398 }
f94397b5
KH
8399
8400 UNBLOCK_INPUT;
06a2c219 8401 return frame_exposed_p;
3afe33e7 8402}
09756a85
RS
8403\f
8404/* Define a queue to save up SelectionRequest events for later handling. */
8405
8406struct selection_event_queue
8407 {
8408 XEvent event;
8409 struct selection_event_queue *next;
8410 };
8411
8412static struct selection_event_queue *queue;
8413
8414/* Nonzero means queue up certain events--don't process them yet. */
06a2c219 8415
09756a85
RS
8416static int x_queue_selection_requests;
8417
8418/* Queue up an X event *EVENT, to be processed later. */
dbc4e1c1 8419
09756a85 8420static void
334208b7
RS
8421x_queue_event (f, event)
8422 FRAME_PTR f;
09756a85
RS
8423 XEvent *event;
8424{
8425 struct selection_event_queue *queue_tmp
06a2c219 8426 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
09756a85 8427
58769bee 8428 if (queue_tmp != NULL)
09756a85
RS
8429 {
8430 queue_tmp->event = *event;
8431 queue_tmp->next = queue;
8432 queue = queue_tmp;
8433 }
8434}
8435
8436/* Take all the queued events and put them back
8437 so that they get processed afresh. */
8438
8439static void
db3906fd
RS
8440x_unqueue_events (display)
8441 Display *display;
09756a85 8442{
58769bee 8443 while (queue != NULL)
09756a85
RS
8444 {
8445 struct selection_event_queue *queue_tmp = queue;
db3906fd 8446 XPutBackEvent (display, &queue_tmp->event);
09756a85 8447 queue = queue_tmp->next;
06a2c219 8448 xfree ((char *)queue_tmp);
09756a85
RS
8449 }
8450}
8451
8452/* Start queuing SelectionRequest events. */
8453
8454void
db3906fd
RS
8455x_start_queuing_selection_requests (display)
8456 Display *display;
09756a85
RS
8457{
8458 x_queue_selection_requests++;
8459}
8460
8461/* Stop queuing SelectionRequest events. */
8462
8463void
db3906fd
RS
8464x_stop_queuing_selection_requests (display)
8465 Display *display;
09756a85
RS
8466{
8467 x_queue_selection_requests--;
db3906fd 8468 x_unqueue_events (display);
09756a85 8469}
f451eb13
JB
8470\f
8471/* The main X event-reading loop - XTread_socket. */
dc6f92b8 8472
06a2c219 8473/* Time stamp of enter window event. This is only used by XTread_socket,
dc6f92b8
JB
8474 but we have to put it out here, since static variables within functions
8475 sometimes don't work. */
06a2c219 8476
dc6f92b8
JB
8477static Time enter_timestamp;
8478
11edeb03 8479/* This holds the state XLookupString needs to implement dead keys
58769bee 8480 and other tricks known as "compose processing". _X Window System_
11edeb03
JB
8481 says that a portable program can't use this, but Stephen Gildea assures
8482 me that letting the compiler initialize it to zeros will work okay.
8483
8484 This must be defined outside of XTread_socket, for the same reasons
06a2c219
GM
8485 given for enter_time stamp, above. */
8486
11edeb03
JB
8487static XComposeStatus compose_status;
8488
10e6549c
RS
8489/* Record the last 100 characters stored
8490 to help debug the loss-of-chars-during-GC problem. */
06a2c219 8491
2224b905
RS
8492static int temp_index;
8493static short temp_buffer[100];
10e6549c 8494
7a13e894
RS
8495/* Set this to nonzero to fake an "X I/O error"
8496 on a particular display. */
06a2c219 8497
7a13e894
RS
8498struct x_display_info *XTread_socket_fake_io_error;
8499
2224b905
RS
8500/* When we find no input here, we occasionally do a no-op command
8501 to verify that the X server is still running and we can still talk with it.
8502 We try all the open displays, one by one.
8503 This variable is used for cycling thru the displays. */
06a2c219 8504
2224b905
RS
8505static struct x_display_info *next_noop_dpyinfo;
8506
06a2c219
GM
8507#define SET_SAVED_MENU_EVENT(size) \
8508 do \
8509 { \
8510 if (f->output_data.x->saved_menu_event == 0) \
8511 f->output_data.x->saved_menu_event \
8512 = (XEvent *) xmalloc (sizeof (XEvent)); \
8513 bcopy (&event, f->output_data.x->saved_menu_event, size); \
8514 if (numchars >= 1) \
8515 { \
8516 bufp->kind = menu_bar_activate_event; \
8517 XSETFRAME (bufp->frame_or_window, f); \
8518 bufp++; \
8519 count++; \
8520 numchars--; \
8521 } \
8522 } \
8523 while (0)
8524
8805890a 8525#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
06a2c219 8526#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8805890a 8527
dc6f92b8
JB
8528/* Read events coming from the X server.
8529 This routine is called by the SIGIO handler.
8530 We return as soon as there are no more events to be read.
8531
8532 Events representing keys are stored in buffer BUFP,
8533 which can hold up to NUMCHARS characters.
8534 We return the number of characters stored into the buffer,
8535 thus pretending to be `read'.
8536
dc6f92b8
JB
8537 EXPECTED is nonzero if the caller knows input is available. */
8538
7c5283e4 8539int
f66868ba 8540XTread_socket (sd, bufp, numchars, expected)
dc6f92b8 8541 register int sd;
8805890a
KH
8542 /* register */ struct input_event *bufp;
8543 /* register */ int numchars;
dc6f92b8
JB
8544 int expected;
8545{
8546 int count = 0;
8547 int nbytes = 0;
dc6f92b8 8548 XEvent event;
f676886a 8549 struct frame *f;
66f55a9d 8550 int event_found = 0;
334208b7 8551 struct x_display_info *dpyinfo;
6c183ba5
RS
8552#ifdef HAVE_X_I18N
8553 Status status_return;
8554#endif
dc6f92b8 8555
9ac0d9e0 8556 if (interrupt_input_blocked)
dc6f92b8 8557 {
9ac0d9e0 8558 interrupt_input_pending = 1;
dc6f92b8
JB
8559 return -1;
8560 }
8561
9ac0d9e0 8562 interrupt_input_pending = 0;
dc6f92b8 8563 BLOCK_INPUT;
c0a04927
RS
8564
8565 /* So people can tell when we have read the available input. */
8566 input_signal_count++;
8567
dc6f92b8 8568 if (numchars <= 0)
06a2c219 8569 abort (); /* Don't think this happens. */
dc6f92b8 8570
7a13e894
RS
8571 /* Find the display we are supposed to read input for.
8572 It's the one communicating on descriptor SD. */
8573 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
8574 {
8575#if 0 /* This ought to be unnecessary; let's verify it. */
dc6f92b8 8576#ifdef FIOSNBIO
7a13e894
RS
8577 /* If available, Xlib uses FIOSNBIO to make the socket
8578 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
e6cbea31 8579 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
06a2c219 8580 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7a13e894 8581 fcntl (dpyinfo->connection, F_SETFL, 0);
c118dd06 8582#endif /* ! defined (FIOSNBIO) */
7a13e894 8583#endif
dc6f92b8 8584
7a13e894
RS
8585#if 0 /* This code can't be made to work, with multiple displays,
8586 and appears not to be used on any system any more.
8587 Also keyboard.c doesn't turn O_NDELAY on and off
8588 for X connections. */
dc6f92b8
JB
8589#ifndef SIGIO
8590#ifndef HAVE_SELECT
7a13e894
RS
8591 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
8592 {
8593 extern int read_alarm_should_throw;
8594 read_alarm_should_throw = 1;
8595 XPeekEvent (dpyinfo->display, &event);
8596 read_alarm_should_throw = 0;
8597 }
c118dd06
JB
8598#endif /* HAVE_SELECT */
8599#endif /* SIGIO */
7a13e894 8600#endif
dc6f92b8 8601
7a13e894
RS
8602 /* For debugging, this gives a way to fake an I/O error. */
8603 if (dpyinfo == XTread_socket_fake_io_error)
8604 {
8605 XTread_socket_fake_io_error = 0;
8606 x_io_error_quitter (dpyinfo->display);
8607 }
dc6f92b8 8608
06a2c219 8609 while (XPending (dpyinfo->display))
dc6f92b8 8610 {
7a13e894 8611 XNextEvent (dpyinfo->display, &event);
06a2c219
GM
8612
8613 if (display_busy_cursor_p)
8614 {
8615 /* Setting inhibit_busy_cursor to 2 inhibits busy-cursor
8616 display until the next X event is read and we come
8617 here again. Setting it to 1 inhibits busy-cursor
8618 display for direct commands. */
8619 if (event.type == MotionNotify
8620 || event.type == EnterNotify
8621 || (dpyinfo->grabbed
8622 && event.type != ButtonRelease))
8623 inhibit_busy_cursor = 2;
8624 else
8625 inhibit_busy_cursor = 1;
8626 }
8627
531483fb 8628#ifdef HAVE_X_I18N
d1bc4182
RS
8629 {
8630 struct frame *f1 = x_any_window_to_frame (dpyinfo,
c99babf2 8631 event.xclient.window);
d1bc4182
RS
8632 /* The necessity of the following line took me
8633 a full work-day to decipher from the docs!! */
8634 if (f1 != 0 && FRAME_XIC (f1) && XFilterEvent (&event, None))
8635 break;
8636 }
0cd6403b 8637#endif
7a13e894
RS
8638 event_found = 1;
8639
8640 switch (event.type)
8641 {
8642 case ClientMessage:
c047688c 8643 {
7a13e894
RS
8644 if (event.xclient.message_type
8645 == dpyinfo->Xatom_wm_protocols
8646 && event.xclient.format == 32)
c047688c 8647 {
7a13e894
RS
8648 if (event.xclient.data.l[0]
8649 == dpyinfo->Xatom_wm_take_focus)
c047688c 8650 {
8c1a6a84
RS
8651 /* Use x_any_window_to_frame because this
8652 could be the shell widget window
8653 if the frame has no title bar. */
8654 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6c183ba5
RS
8655#ifdef HAVE_X_I18N
8656 /* Not quite sure this is needed -pd */
8c1a6a84 8657 if (f && FRAME_XIC (f))
6c183ba5
RS
8658 XSetICFocus (FRAME_XIC (f));
8659#endif
bf7253f4
RS
8660 /* Since we set WM_TAKE_FOCUS, we must call
8661 XSetInputFocus explicitly. But not if f is null,
8662 since that might be an event for a deleted frame. */
7a13e894 8663 if (f)
bf7253f4
RS
8664 {
8665 Display *d = event.xclient.display;
8666 /* Catch and ignore errors, in case window has been
8667 iconified by a window manager such as GWM. */
8668 int count = x_catch_errors (d);
8669 XSetInputFocus (d, event.xclient.window,
8670 RevertToPointerRoot,
8671 event.xclient.data.l[1]);
8672 /* This is needed to detect the error
8673 if there is an error. */
8674 XSync (d, False);
8675 x_uncatch_errors (d, count);
8676 }
7a13e894 8677 /* Not certain about handling scroll bars here */
c047688c 8678 }
7a13e894
RS
8679 else if (event.xclient.data.l[0]
8680 == dpyinfo->Xatom_wm_save_yourself)
8681 {
8682 /* Save state modify the WM_COMMAND property to
06a2c219 8683 something which can reinstate us. This notifies
7a13e894
RS
8684 the session manager, who's looking for such a
8685 PropertyNotify. Can restart processing when
06a2c219 8686 a keyboard or mouse event arrives. */
7a13e894
RS
8687 if (numchars > 0)
8688 {
19126e11
KH
8689 f = x_top_window_to_frame (dpyinfo,
8690 event.xclient.window);
7a13e894
RS
8691
8692 /* This is just so we only give real data once
8693 for a single Emacs process. */
8694 if (f == selected_frame)
8695 XSetCommand (FRAME_X_DISPLAY (f),
8696 event.xclient.window,
8697 initial_argv, initial_argc);
f000f5c5 8698 else if (f)
7a13e894
RS
8699 XSetCommand (FRAME_X_DISPLAY (f),
8700 event.xclient.window,
8701 0, 0);
8702 }
8703 }
8704 else if (event.xclient.data.l[0]
8705 == dpyinfo->Xatom_wm_delete_window)
1fb20991 8706 {
19126e11
KH
8707 struct frame *f
8708 = x_any_window_to_frame (dpyinfo,
8709 event.xclient.window);
1fb20991 8710
7a13e894
RS
8711 if (f)
8712 {
8713 if (numchars == 0)
8714 abort ();
1fb20991 8715
7a13e894
RS
8716 bufp->kind = delete_window_event;
8717 XSETFRAME (bufp->frame_or_window, f);
8718 bufp++;
8719
8720 count += 1;
8721 numchars -= 1;
8722 }
1fb20991 8723 }
c047688c 8724 }
7a13e894
RS
8725 else if (event.xclient.message_type
8726 == dpyinfo->Xatom_wm_configure_denied)
8727 {
8728 }
8729 else if (event.xclient.message_type
8730 == dpyinfo->Xatom_wm_window_moved)
8731 {
8732 int new_x, new_y;
19126e11
KH
8733 struct frame *f
8734 = x_window_to_frame (dpyinfo, event.xclient.window);
58769bee 8735
7a13e894
RS
8736 new_x = event.xclient.data.s[0];
8737 new_y = event.xclient.data.s[1];
1fb20991 8738
7a13e894
RS
8739 if (f)
8740 {
7556890b
RS
8741 f->output_data.x->left_pos = new_x;
8742 f->output_data.x->top_pos = new_y;
7a13e894 8743 }
1fb20991 8744 }
0fdff6bb 8745#ifdef HACK_EDITRES
7a13e894
RS
8746 else if (event.xclient.message_type
8747 == dpyinfo->Xatom_editres)
8748 {
19126e11
KH
8749 struct frame *f
8750 = x_any_window_to_frame (dpyinfo, event.xclient.window);
7556890b 8751 _XEditResCheckMessages (f->output_data.x->widget, NULL,
19126e11 8752 &event, NULL);
7a13e894 8753 }
0fdff6bb 8754#endif /* HACK_EDITRES */
06a2c219
GM
8755 else if ((event.xclient.message_type
8756 == dpyinfo->Xatom_DONE)
8757 || (event.xclient.message_type
8758 == dpyinfo->Xatom_PAGE))
8759 {
8760 /* Ghostview job completed. Kill it. We could
8761 reply with "Next" if we received "Page", but we
8762 currently never do because we are interested in
8763 images, only, which should have 1 page. */
8764 Window gs_window = (Window) event.xclient.data.l[0];
8765 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
8766 struct frame *f
8767 = x_window_to_frame (dpyinfo, event.xclient.window);
8768 x_kill_gs_process (pixmap, f);
8769 expose_frame (f, 0, 0, 0, 0);
8770 }
8771#ifdef USE_TOOLKIT_SCROLL_BARS
8772 /* Scroll bar callbacks send a ClientMessage from which
8773 we construct an input_event. */
8774 else if (event.xclient.message_type
8775 == dpyinfo->Xatom_Scrollbar)
8776 {
8777 x_scroll_bar_to_input_event (&event, bufp);
8778 ++bufp, ++count, --numchars;
8779 goto out;
8780 }
8781#endif /* USE_TOOLKIT_SCROLL_BARS */
8782 else
8783 goto OTHER;
7a13e894
RS
8784 }
8785 break;
dc6f92b8 8786
7a13e894 8787 case SelectionNotify:
3afe33e7 8788#ifdef USE_X_TOOLKIT
19126e11 8789 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
7a13e894 8790 goto OTHER;
3afe33e7 8791#endif /* not USE_X_TOOLKIT */
dfcf069d 8792 x_handle_selection_notify (&event.xselection);
7a13e894 8793 break;
d56a553a 8794
06a2c219 8795 case SelectionClear: /* Someone has grabbed ownership. */
3afe33e7 8796#ifdef USE_X_TOOLKIT
19126e11 8797 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
7a13e894 8798 goto OTHER;
3afe33e7 8799#endif /* USE_X_TOOLKIT */
7a13e894
RS
8800 {
8801 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
d56a553a 8802
7a13e894
RS
8803 if (numchars == 0)
8804 abort ();
d56a553a 8805
7a13e894
RS
8806 bufp->kind = selection_clear_event;
8807 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
8808 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
8809 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 8810 bufp->frame_or_window = Qnil;
7a13e894 8811 bufp++;
d56a553a 8812
7a13e894
RS
8813 count += 1;
8814 numchars -= 1;
8815 }
8816 break;
dc6f92b8 8817
06a2c219 8818 case SelectionRequest: /* Someone wants our selection. */
3afe33e7 8819#ifdef USE_X_TOOLKIT
19126e11 8820 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
7a13e894 8821 goto OTHER;
3afe33e7 8822#endif /* USE_X_TOOLKIT */
7a13e894 8823 if (x_queue_selection_requests)
19126e11 8824 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
7a13e894
RS
8825 &event);
8826 else
8827 {
8828 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
dc6f92b8 8829
7a13e894
RS
8830 if (numchars == 0)
8831 abort ();
8832
8833 bufp->kind = selection_request_event;
8834 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
8835 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
8836 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
8837 SELECTION_EVENT_TARGET (bufp) = eventp->target;
8838 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
8839 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 8840 bufp->frame_or_window = Qnil;
7a13e894
RS
8841 bufp++;
8842
8843 count += 1;
8844 numchars -= 1;
8845 }
8846 break;
8847
8848 case PropertyNotify:
3afe33e7 8849#ifdef USE_X_TOOLKIT
19126e11 8850 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
7a13e894 8851 goto OTHER;
3afe33e7 8852#endif /* not USE_X_TOOLKIT */
dfcf069d 8853 x_handle_property_notify (&event.xproperty);
7a13e894 8854 break;
dc6f92b8 8855
7a13e894 8856 case ReparentNotify:
19126e11 8857 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
7a13e894
RS
8858 if (f)
8859 {
8860 int x, y;
7556890b 8861 f->output_data.x->parent_desc = event.xreparent.parent;
7a13e894 8862 x_real_positions (f, &x, &y);
7556890b
RS
8863 f->output_data.x->left_pos = x;
8864 f->output_data.x->top_pos = y;
7a13e894
RS
8865 }
8866 break;
3bd330d4 8867
7a13e894 8868 case Expose:
19126e11 8869 f = x_window_to_frame (dpyinfo, event.xexpose.window);
7a13e894 8870 if (f)
dc6f92b8 8871 {
7a13e894
RS
8872 if (f->async_visible == 0)
8873 {
8874 f->async_visible = 1;
8875 f->async_iconified = 0;
06c488fd 8876 f->output_data.x->has_been_visible = 1;
7a13e894
RS
8877 SET_FRAME_GARBAGED (f);
8878 }
8879 else
06a2c219
GM
8880 expose_frame (x_window_to_frame (dpyinfo,
8881 event.xexpose.window),
8882 event.xexpose.x, event.xexpose.y,
8883 event.xexpose.width, event.xexpose.height);
dc6f92b8
JB
8884 }
8885 else
7a13e894 8886 {
06a2c219
GM
8887#ifdef USE_TOOLKIT_SCROLL_BARS
8888 /* Dispatch event to the widget. */
8889 goto OTHER;
8890#else /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
8891 struct scroll_bar *bar
8892 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 8893
7a13e894
RS
8894 if (bar)
8895 x_scroll_bar_expose (bar, &event);
3afe33e7 8896#ifdef USE_X_TOOLKIT
7a13e894
RS
8897 else
8898 goto OTHER;
3afe33e7 8899#endif /* USE_X_TOOLKIT */
06a2c219 8900#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
8901 }
8902 break;
dc6f92b8 8903
7a13e894
RS
8904 case GraphicsExpose: /* This occurs when an XCopyArea's
8905 source area was obscured or not
8906 available.*/
19126e11 8907 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
7a13e894
RS
8908 if (f)
8909 {
06a2c219
GM
8910 expose_frame (f,
8911 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
8912 event.xgraphicsexpose.width,
8913 event.xgraphicsexpose.height);
7a13e894 8914 }
3afe33e7 8915#ifdef USE_X_TOOLKIT
7a13e894
RS
8916 else
8917 goto OTHER;
3afe33e7 8918#endif /* USE_X_TOOLKIT */
7a13e894 8919 break;
dc6f92b8 8920
7a13e894 8921 case NoExpose: /* This occurs when an XCopyArea's
06a2c219
GM
8922 source area was completely
8923 available */
7a13e894 8924 break;
dc6f92b8 8925
7a13e894 8926 case UnmapNotify:
06a2c219
GM
8927 /* Redo the mouse-highlight after the tooltip has gone. */
8928 if (event.xmap.window == tip_window)
8929 {
8930 tip_window = 0;
8931 redo_mouse_highlight ();
8932 }
8933
91ea2a7a 8934 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
7a13e894
RS
8935 if (f) /* F may no longer exist if
8936 the frame was deleted. */
8937 {
8938 /* While a frame is unmapped, display generation is
8939 disabled; you don't want to spend time updating a
8940 display that won't ever be seen. */
8941 f->async_visible = 0;
8942 /* We can't distinguish, from the event, whether the window
8943 has become iconified or invisible. So assume, if it
8944 was previously visible, than now it is iconified.
1aa6072f
RS
8945 But x_make_frame_invisible clears both
8946 the visible flag and the iconified flag;
8947 and that way, we know the window is not iconified now. */
7a13e894 8948 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
1aa6072f
RS
8949 {
8950 f->async_iconified = 1;
bddd097c 8951
1aa6072f
RS
8952 bufp->kind = iconify_event;
8953 XSETFRAME (bufp->frame_or_window, f);
8954 bufp++;
8955 count++;
8956 numchars--;
8957 }
7a13e894 8958 }
7a13e894 8959 goto OTHER;
dc6f92b8 8960
7a13e894 8961 case MapNotify:
06a2c219
GM
8962 if (event.xmap.window == tip_window)
8963 /* The tooltip has been drawn already. Avoid
8964 the SET_FRAME_GARBAGED below. */
8965 goto OTHER;
8966
8967 /* We use x_top_window_to_frame because map events can
8968 come for sub-windows and they don't mean that the
8969 frame is visible. */
19126e11 8970 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
7a13e894
RS
8971 if (f)
8972 {
8973 f->async_visible = 1;
8974 f->async_iconified = 0;
06c488fd 8975 f->output_data.x->has_been_visible = 1;
dc6f92b8 8976
7a13e894
RS
8977 /* wait_reading_process_input will notice this and update
8978 the frame's display structures. */
8979 SET_FRAME_GARBAGED (f);
bddd097c 8980
d806e720
RS
8981 if (f->iconified)
8982 {
8983 bufp->kind = deiconify_event;
8984 XSETFRAME (bufp->frame_or_window, f);
8985 bufp++;
8986 count++;
8987 numchars--;
8988 }
e73ec6fa 8989 else if (! NILP (Vframe_list)
8c29f6bf 8990 && ! NILP (XCONS (Vframe_list)->cdr))
78aa2ba5
KH
8991 /* Force a redisplay sooner or later
8992 to update the frame titles
8993 in case this is the second frame. */
8994 record_asynch_buffer_change ();
7a13e894 8995 }
7a13e894 8996 goto OTHER;
dc6f92b8 8997
7a13e894 8998 case KeyPress:
19126e11 8999 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
f451eb13 9000
06a2c219
GM
9001#ifdef USE_MOTIF
9002 /* I couldn't find a way to prevent LessTif scroll bars
9003 from consuming key events. */
9004 if (f == 0)
9005 {
9006 Widget widget = XtWindowToWidget (dpyinfo->display,
9007 event.xkey.window);
9008 if (widget && XmIsScrollBar (widget))
9009 {
9010 widget = XtParent (widget);
9011 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9012 }
9013 }
9014#endif /* USE_MOTIF */
9015
7a13e894
RS
9016 if (f != 0)
9017 {
9018 KeySym keysym, orig_keysym;
9019 /* al%imercury@uunet.uu.net says that making this 81 instead of
9020 80 fixed a bug whereby meta chars made his Emacs hang. */
9021 unsigned char copy_buffer[81];
9022 int modifiers;
64bb1782 9023
7a13e894
RS
9024 event.xkey.state
9025 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9026 extra_keyboard_modifiers);
9027 modifiers = event.xkey.state;
3a2712f9 9028
7a13e894 9029 /* This will have to go some day... */
752a043f 9030
7a13e894
RS
9031 /* make_lispy_event turns chars into control chars.
9032 Don't do it here because XLookupString is too eager. */
9033 event.xkey.state &= ~ControlMask;
5d46f928
RS
9034 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9035 | dpyinfo->super_mod_mask
9036 | dpyinfo->hyper_mod_mask
9037 | dpyinfo->alt_mod_mask);
9038
1cf4a0d1
RS
9039 /* In case Meta is ComposeCharacter,
9040 clear its status. According to Markus Ehrnsperger
9041 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9042 this enables ComposeCharacter to work whether or
9043 not it is combined with Meta. */
9044 if (modifiers & dpyinfo->meta_mod_mask)
9045 bzero (&compose_status, sizeof (compose_status));
9046
6c183ba5
RS
9047#ifdef HAVE_X_I18N
9048 if (FRAME_XIC (f))
9049 {
5d7cc324
RS
9050 /* The necessity of the following line took me
9051 a full work-day to decipher from the docs!! */
9052 if (XFilterEvent (&event, None))
9053 break;
6c183ba5
RS
9054 nbytes = XmbLookupString (FRAME_XIC (f),
9055 &event.xkey, copy_buffer,
9056 80, &keysym,
9057 &status_return);
1decb680
PE
9058 if (status_return == XLookupNone)
9059 break;
9060 else if (status_return == XLookupChars)
9061 keysym = NoSymbol;
9062 else if (status_return != XLookupKeySym
9063 && status_return != XLookupBoth)
9064 abort ();
6c183ba5
RS
9065 }
9066 else
9067 nbytes = XLookupString (&event.xkey, copy_buffer,
9068 80, &keysym, &compose_status);
9069#else
0299d313
RS
9070 nbytes = XLookupString (&event.xkey, copy_buffer,
9071 80, &keysym, &compose_status);
6c183ba5 9072#endif
dc6f92b8 9073
7a13e894 9074 orig_keysym = keysym;
55123275 9075
7a13e894
RS
9076 if (numchars > 1)
9077 {
9078 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9079 || keysym == XK_Delete
1097aea0 9080#ifdef XK_ISO_Left_Tab
441affdb 9081 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
1097aea0 9082#endif
852bff8f 9083 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
7a13e894
RS
9084 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9085 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
c34790e0 9086#ifdef HPUX
7a13e894
RS
9087 /* This recognizes the "extended function keys".
9088 It seems there's no cleaner way.
9089 Test IsModifierKey to avoid handling mode_switch
9090 incorrectly. */
9091 || ((unsigned) (keysym) >= XK_Select
9092 && (unsigned)(keysym) < XK_KP_Space)
69388238
RS
9093#endif
9094#ifdef XK_dead_circumflex
7a13e894 9095 || orig_keysym == XK_dead_circumflex
69388238
RS
9096#endif
9097#ifdef XK_dead_grave
7a13e894 9098 || orig_keysym == XK_dead_grave
69388238
RS
9099#endif
9100#ifdef XK_dead_tilde
7a13e894 9101 || orig_keysym == XK_dead_tilde
69388238
RS
9102#endif
9103#ifdef XK_dead_diaeresis
7a13e894 9104 || orig_keysym == XK_dead_diaeresis
69388238
RS
9105#endif
9106#ifdef XK_dead_macron
7a13e894 9107 || orig_keysym == XK_dead_macron
69388238
RS
9108#endif
9109#ifdef XK_dead_degree
7a13e894 9110 || orig_keysym == XK_dead_degree
69388238
RS
9111#endif
9112#ifdef XK_dead_acute
7a13e894 9113 || orig_keysym == XK_dead_acute
69388238
RS
9114#endif
9115#ifdef XK_dead_cedilla
7a13e894 9116 || orig_keysym == XK_dead_cedilla
69388238
RS
9117#endif
9118#ifdef XK_dead_breve
7a13e894 9119 || orig_keysym == XK_dead_breve
69388238
RS
9120#endif
9121#ifdef XK_dead_ogonek
7a13e894 9122 || orig_keysym == XK_dead_ogonek
69388238
RS
9123#endif
9124#ifdef XK_dead_caron
7a13e894 9125 || orig_keysym == XK_dead_caron
69388238
RS
9126#endif
9127#ifdef XK_dead_doubleacute
7a13e894 9128 || orig_keysym == XK_dead_doubleacute
69388238
RS
9129#endif
9130#ifdef XK_dead_abovedot
7a13e894 9131 || orig_keysym == XK_dead_abovedot
c34790e0 9132#endif
7a13e894
RS
9133 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9134 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9135 /* Any "vendor-specific" key is ok. */
9136 || (orig_keysym & (1 << 28)))
9137 && ! (IsModifierKey (orig_keysym)
7719aa06
RS
9138#ifndef HAVE_X11R5
9139#ifdef XK_Mode_switch
7a13e894 9140 || ((unsigned)(orig_keysym) == XK_Mode_switch)
7719aa06
RS
9141#endif
9142#ifdef XK_Num_Lock
7a13e894 9143 || ((unsigned)(orig_keysym) == XK_Num_Lock)
7719aa06
RS
9144#endif
9145#endif /* not HAVE_X11R5 */
7a13e894 9146 ))
dc6f92b8 9147 {
10e6549c
RS
9148 if (temp_index == sizeof temp_buffer / sizeof (short))
9149 temp_index = 0;
7a13e894
RS
9150 temp_buffer[temp_index++] = keysym;
9151 bufp->kind = non_ascii_keystroke;
9152 bufp->code = keysym;
e0c1aef2 9153 XSETFRAME (bufp->frame_or_window, f);
334208b7
RS
9154 bufp->modifiers
9155 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9156 modifiers);
1113d9db 9157 bufp->timestamp = event.xkey.time;
dc6f92b8 9158 bufp++;
7a13e894
RS
9159 count++;
9160 numchars--;
06a2c219
GM
9161
9162 if (display_busy_cursor_p)
9163 if (keysym != XK_Return || minibuf_level == 0)
9164 inhibit_busy_cursor = 2;
dc6f92b8 9165 }
7a13e894
RS
9166 else if (numchars > nbytes)
9167 {
9168 register int i;
9169
9170 for (i = 0; i < nbytes; i++)
9171 {
9172 if (temp_index == sizeof temp_buffer / sizeof (short))
9173 temp_index = 0;
9174 temp_buffer[temp_index++] = copy_buffer[i];
9175 bufp->kind = ascii_keystroke;
9176 bufp->code = copy_buffer[i];
9177 XSETFRAME (bufp->frame_or_window, f);
9178 bufp->modifiers
9179 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9180 modifiers);
9181 bufp->timestamp = event.xkey.time;
9182 bufp++;
9183 }
9184
9185 count += nbytes;
9186 numchars -= nbytes;
1decb680
PE
9187
9188 if (keysym == NoSymbol)
9189 break;
7a13e894
RS
9190 }
9191 else
9192 abort ();
dc6f92b8 9193 }
10e6549c
RS
9194 else
9195 abort ();
dc6f92b8 9196 }
717ca130 9197 goto OTHER;
f451eb13 9198
7a13e894 9199 /* Here's a possible interpretation of the whole
06a2c219
GM
9200 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
9201 you get a FocusIn event, you have to get a FocusOut
9202 event before you relinquish the focus. If you
9203 haven't received a FocusIn event, then a mere
9204 LeaveNotify is enough to free you. */
f451eb13 9205
7a13e894 9206 case EnterNotify:
06a2c219
GM
9207 {
9208 int from_menu_bar_p = 0;
9209
9210 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
9211
9212#ifdef LESSTIF_VERSION
9213 /* When clicking outside of a menu bar popup to close
9214 it, we get a FocusIn/ EnterNotify sequence of
9215 events. The flag event.xcrossing.focus is not set
9216 in the EnterNotify event of that sequence because
9217 the focus is in the menu bar,
9218 event.xcrossing.window is the frame's X window.
9219 Unconditionally setting the focus frame to null in
9220 this case is not the right thing, because no event
9221 follows that could set the focus frame to the right
9222 value.
9223
9224 This could be a LessTif bug, but I wasn't able to
9225 reproduce the behavior in a simple test program.
9226
9227 (gerd, LessTif 0.88.1). */
9228
9229 if (!event.xcrossing.focus
9230 && f
9231 && f->output_data.x->menubar_widget)
9232 {
9233 Window focus;
9234 int revert;
9235
9236 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9237 if (focus == XtWindow (f->output_data.x->menubar_widget))
9238 from_menu_bar_p = 1;
9239 }
9240#endif /* LESSTIF_VERSION */
6d4238f3 9241
06a2c219
GM
9242 if (event.xcrossing.focus || from_menu_bar_p)
9243 {
9244 /* Avoid nasty pop/raise loops. */
9245 if (f && (!(f->auto_raise)
9246 || !(f->auto_lower)
9247 || (event.xcrossing.time - enter_timestamp) > 500))
9248 {
9249 x_new_focus_frame (dpyinfo, f);
9250 enter_timestamp = event.xcrossing.time;
9251 }
9252 }
9253 else if (f == dpyinfo->x_focus_frame)
9254 x_new_focus_frame (dpyinfo, 0);
9255
9256 /* EnterNotify counts as mouse movement,
9257 so update things that depend on mouse position. */
9258 if (f && !f->output_data.x->busy_p)
9259 note_mouse_movement (f, &event.xmotion);
9260 goto OTHER;
9261 }
dc6f92b8 9262
7a13e894 9263 case FocusIn:
19126e11 9264 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 9265 if (event.xfocus.detail != NotifyPointer)
0f941935 9266 dpyinfo->x_focus_event_frame = f;
7a13e894 9267 if (f)
0f941935 9268 x_new_focus_frame (dpyinfo, f);
f9e24cb9 9269
6c183ba5
RS
9270#ifdef HAVE_X_I18N
9271 if (f && FRAME_XIC (f))
9272 XSetICFocus (FRAME_XIC (f));
9273#endif
9274
7a13e894 9275 goto OTHER;
10c5e63d 9276
7a13e894 9277 case LeaveNotify:
19126e11 9278 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
7a13e894 9279 if (f)
10c5e63d 9280 {
06a2c219
GM
9281 Lisp_Object frame;
9282 int from_menu_bar_p = 0;
9283
7a13e894 9284 if (f == dpyinfo->mouse_face_mouse_frame)
06a2c219
GM
9285 {
9286 /* If we move outside the frame, then we're
9287 certainly no longer on any text in the frame. */
9288 clear_mouse_face (dpyinfo);
9289 dpyinfo->mouse_face_mouse_frame = 0;
9290 }
9291
9292 /* Generate a nil HELP_EVENT to cancel a help-echo.
9293 Do it only if there's something to cancel.
9294 Otherwise, the startup message is cleared when
9295 the mouse leaves the frame. */
9296 if (any_help_event_p)
9297 {
9298 XSETFRAME (frame, f);
9299 bufp->kind = HELP_EVENT;
9300 bufp->frame_or_window = Fcons (frame, Qnil);
9301 ++bufp, ++count, --numchars;
9302 }
7a13e894 9303
06a2c219
GM
9304#ifdef LESSTIF_VERSION
9305 /* Please see the comment at the start of the
9306 EnterNotify case. */
9307 if (!event.xcrossing.focus
9308 && f->output_data.x->menubar_widget)
9309 {
9310 Window focus;
9311 int revert;
9312 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9313 if (focus == XtWindow (f->output_data.x->menubar_widget))
9314 from_menu_bar_p = 1;
9315 }
9316#endif /* LESSTIF_VERSION */
9317
9318 if (event.xcrossing.focus || from_menu_bar_p)
0f941935 9319 x_mouse_leave (dpyinfo);
10c5e63d 9320 else
7a13e894 9321 {
0f941935
KH
9322 if (f == dpyinfo->x_focus_event_frame)
9323 dpyinfo->x_focus_event_frame = 0;
9324 if (f == dpyinfo->x_focus_frame)
9325 x_new_focus_frame (dpyinfo, 0);
7a13e894 9326 }
10c5e63d 9327 }
7a13e894 9328 goto OTHER;
dc6f92b8 9329
7a13e894 9330 case FocusOut:
19126e11 9331 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 9332 if (event.xfocus.detail != NotifyPointer
0f941935
KH
9333 && f == dpyinfo->x_focus_event_frame)
9334 dpyinfo->x_focus_event_frame = 0;
9335 if (f && f == dpyinfo->x_focus_frame)
9336 x_new_focus_frame (dpyinfo, 0);
f9e24cb9 9337
6c183ba5
RS
9338#ifdef HAVE_X_I18N
9339 if (f && FRAME_XIC (f))
9340 XUnsetICFocus (FRAME_XIC (f));
9341#endif
9342
7a13e894 9343 goto OTHER;
dc6f92b8 9344
7a13e894 9345 case MotionNotify:
dc6f92b8 9346 {
06a2c219
GM
9347 previous_help_echo = help_echo;
9348 help_echo = Qnil;
9349
7a13e894
RS
9350 if (dpyinfo->grabbed && last_mouse_frame
9351 && FRAME_LIVE_P (last_mouse_frame))
9352 f = last_mouse_frame;
9353 else
19126e11 9354 f = x_window_to_frame (dpyinfo, event.xmotion.window);
06a2c219 9355
7a13e894
RS
9356 if (f)
9357 note_mouse_movement (f, &event.xmotion);
9358 else
9359 {
06a2c219 9360#ifndef USE_X_TOOLKIT
7a13e894
RS
9361 struct scroll_bar *bar
9362 = x_window_to_scroll_bar (event.xmotion.window);
f451eb13 9363
7a13e894
RS
9364 if (bar)
9365 x_scroll_bar_note_movement (bar, &event);
06a2c219 9366#endif /* USE_X_TOOLKIT */
b8009dd1 9367
06a2c219
GM
9368 /* If we move outside the frame, then we're
9369 certainly no longer on any text in the frame. */
7a13e894
RS
9370 clear_mouse_face (dpyinfo);
9371 }
06a2c219
GM
9372
9373 /* If the contents of the global variable help_echo
9374 has changed, generate a HELP_EVENT. */
9375 if (STRINGP (help_echo)
9376 || STRINGP (previous_help_echo))
9377 {
9378 Lisp_Object frame;
9379
9380 if (f)
9381 XSETFRAME (frame, f);
9382 else
9383 frame = Qnil;
9384
9385 any_help_event_p = 1;
9386 bufp->kind = HELP_EVENT;
9387 bufp->frame_or_window = Fcons (frame, help_echo);
9388 ++bufp, ++count, --numchars;
9389 }
9390
9391 goto OTHER;
dc6f92b8 9392 }
dc6f92b8 9393
7a13e894 9394 case ConfigureNotify:
9829ddba
RS
9395 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
9396 if (f)
af395ec1 9397 {
bf1b7b30
KH
9398 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
9399 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
06a2c219 9400
2d7fc7e8
RS
9401#ifndef USE_X_TOOLKIT
9402 /* In the toolkit version, change_frame_size
9403 is called by the code that handles resizing
9404 of the EmacsFrame widget. */
7a13e894 9405
7a13e894
RS
9406 /* Even if the number of character rows and columns has
9407 not changed, the font size may have changed, so we need
9408 to check the pixel dimensions as well. */
9409 if (columns != f->width
9410 || rows != f->height
7556890b
RS
9411 || event.xconfigure.width != f->output_data.x->pixel_width
9412 || event.xconfigure.height != f->output_data.x->pixel_height)
7a13e894
RS
9413 {
9414 change_frame_size (f, rows, columns, 0, 1);
9415 SET_FRAME_GARBAGED (f);
e687d06e 9416 cancel_mouse_face (f);
7a13e894 9417 }
2d7fc7e8 9418#endif
af395ec1 9419
7556890b
RS
9420 f->output_data.x->pixel_width = event.xconfigure.width;
9421 f->output_data.x->pixel_height = event.xconfigure.height;
7a13e894
RS
9422
9423 /* What we have now is the position of Emacs's own window.
9424 Convert that to the position of the window manager window. */
dcb07ae9
RS
9425 x_real_positions (f, &f->output_data.x->left_pos,
9426 &f->output_data.x->top_pos);
9427
9428 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
9429 {
9430 /* Since the WM decorations come below top_pos now,
9431 we must put them below top_pos in the future. */
9432 f->output_data.x->win_gravity = NorthWestGravity;
9433 x_wm_set_size_hint (f, (long) 0, 0);
9434 }
8f08dc93
KH
9435#ifdef USE_MOTIF
9436 /* Some window managers pass (0,0) as the location of
9437 the window, and the Motif event handler stores it
9438 in the emacs widget, which messes up Motif menus. */
9439 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
9440 {
9441 event.xconfigure.x = f->output_data.x->widget->core.x;
9442 event.xconfigure.y = f->output_data.x->widget->core.y;
9443 }
06a2c219 9444#endif /* USE_MOTIF */
7a13e894 9445 }
2d7fc7e8 9446 goto OTHER;
dc6f92b8 9447
7a13e894
RS
9448 case ButtonPress:
9449 case ButtonRelease:
9450 {
9451 /* If we decide we want to generate an event to be seen
9452 by the rest of Emacs, we put it here. */
9453 struct input_event emacs_event;
06a2c219
GM
9454 int toolbar_p = 0;
9455
7a13e894 9456 emacs_event.kind = no_event;
7a13e894 9457 bzero (&compose_status, sizeof (compose_status));
9b07615b 9458
06a2c219
GM
9459 if (dpyinfo->grabbed
9460 && last_mouse_frame
9f67f20b
RS
9461 && FRAME_LIVE_P (last_mouse_frame))
9462 f = last_mouse_frame;
9463 else
2224b905 9464 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9f67f20b 9465
06a2c219
GM
9466 if (f)
9467 {
9468 /* Is this in the toolbar? */
9469 if (WINDOWP (f->toolbar_window)
9470 && XFASTINT (XWINDOW (f->toolbar_window)->height))
9471 {
9472 Lisp_Object window;
9473 int p, x, y;
9474
9475 x = event.xbutton.x;
9476 y = event.xbutton.y;
9477
9478 /* Set x and y. */
9479 window = window_from_coordinates (f, x, y, &p, 1);
9480 if (EQ (window, f->toolbar_window))
9481 {
9482 x_handle_toolbar_click (f, &event.xbutton);
9483 toolbar_p = 1;
9484 }
9485 }
9486
9487 if (!toolbar_p)
9488 if (!dpyinfo->x_focus_frame
9489 || f == dpyinfo->x_focus_frame)
9490 construct_mouse_click (&emacs_event, &event, f);
7a13e894
RS
9491 }
9492 else
9493 {
06a2c219 9494#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
9495 struct scroll_bar *bar
9496 = x_window_to_scroll_bar (event.xbutton.window);
f451eb13 9497
7a13e894
RS
9498 if (bar)
9499 x_scroll_bar_handle_click (bar, &event, &emacs_event);
06a2c219 9500#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9501 }
9502
9503 if (event.type == ButtonPress)
9504 {
9505 dpyinfo->grabbed |= (1 << event.xbutton.button);
9506 last_mouse_frame = f;
edad46f6
KH
9507 /* Ignore any mouse motion that happened
9508 before this event; any subsequent mouse-movement
9509 Emacs events should reflect only motion after
9510 the ButtonPress. */
a00e91cd
KH
9511 if (f != 0)
9512 f->mouse_moved = 0;
06a2c219
GM
9513
9514 if (!toolbar_p)
9515 last_toolbar_item = -1;
9516 if (display_busy_cursor_p)
9517 inhibit_busy_cursor = 2;
7a13e894 9518 }
3afe33e7
RS
9519 else
9520 {
7a13e894 9521 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
3afe33e7 9522 }
23faf38f 9523
7a13e894
RS
9524 if (numchars >= 1 && emacs_event.kind != no_event)
9525 {
9526 bcopy (&emacs_event, bufp, sizeof (struct input_event));
9527 bufp++;
9528 count++;
9529 numchars--;
9530 }
3afe33e7
RS
9531
9532#ifdef USE_X_TOOLKIT
2224b905
RS
9533 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
9534 /* For a down-event in the menu bar,
9535 don't pass it to Xt right now.
9536 Instead, save it away
9537 and we will pass it to Xt from kbd_buffer_get_event.
9538 That way, we can run some Lisp code first. */
91375f8f
RS
9539 if (f && event.type == ButtonPress
9540 /* Verify the event is really within the menu bar
9541 and not just sent to it due to grabbing. */
9542 && event.xbutton.x >= 0
9543 && event.xbutton.x < f->output_data.x->pixel_width
9544 && event.xbutton.y >= 0
9545 && event.xbutton.y < f->output_data.x->menubar_height
9546 && event.xbutton.same_screen)
2224b905 9547 {
8805890a 9548 SET_SAVED_BUTTON_EVENT;
2237cac9
RS
9549 XSETFRAME (last_mouse_press_frame, f);
9550 }
9551 else if (event.type == ButtonPress)
9552 {
9553 last_mouse_press_frame = Qnil;
30e671c3 9554 goto OTHER;
ce89ef46 9555 }
06a2c219 9556
2237cac9
RS
9557#ifdef USE_MOTIF /* This should do not harm for Lucid,
9558 but I am trying to be cautious. */
ce89ef46
RS
9559 else if (event.type == ButtonRelease)
9560 {
2237cac9 9561 if (!NILP (last_mouse_press_frame))
f10ded1c 9562 {
2237cac9
RS
9563 f = XFRAME (last_mouse_press_frame);
9564 if (f->output_data.x)
06a2c219 9565 SET_SAVED_BUTTON_EVENT;
f10ded1c 9566 }
06a2c219 9567 else
30e671c3 9568 goto OTHER;
2224b905 9569 }
2237cac9 9570#endif /* USE_MOTIF */
2224b905
RS
9571 else
9572 goto OTHER;
3afe33e7 9573#endif /* USE_X_TOOLKIT */
7a13e894
RS
9574 }
9575 break;
dc6f92b8 9576
7a13e894 9577 case CirculateNotify:
06a2c219
GM
9578 goto OTHER;
9579
7a13e894 9580 case CirculateRequest:
06a2c219
GM
9581 goto OTHER;
9582
9583 case VisibilityNotify:
9584 goto OTHER;
dc6f92b8 9585
7a13e894
RS
9586 case MappingNotify:
9587 /* Someone has changed the keyboard mapping - update the
9588 local cache. */
9589 switch (event.xmapping.request)
9590 {
9591 case MappingModifier:
9592 x_find_modifier_meanings (dpyinfo);
9593 /* This is meant to fall through. */
9594 case MappingKeyboard:
9595 XRefreshKeyboardMapping (&event.xmapping);
9596 }
7a13e894 9597 goto OTHER;
dc6f92b8 9598
7a13e894 9599 default:
7a13e894 9600 OTHER:
717ca130 9601#ifdef USE_X_TOOLKIT
7a13e894
RS
9602 BLOCK_INPUT;
9603 XtDispatchEvent (&event);
9604 UNBLOCK_INPUT;
3afe33e7 9605#endif /* USE_X_TOOLKIT */
7a13e894
RS
9606 break;
9607 }
dc6f92b8
JB
9608 }
9609 }
9610
06a2c219
GM
9611 out:;
9612
9a5196d0
RS
9613 /* On some systems, an X bug causes Emacs to get no more events
9614 when the window is destroyed. Detect that. (1994.) */
58769bee 9615 if (! event_found)
ef2a22d0 9616 {
ef2a22d0
RS
9617 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
9618 One XNOOP in 100 loops will make Emacs terminate.
9619 B. Bretthauer, 1994 */
9620 x_noop_count++;
58769bee 9621 if (x_noop_count >= 100)
ef2a22d0
RS
9622 {
9623 x_noop_count=0;
2224b905
RS
9624
9625 if (next_noop_dpyinfo == 0)
9626 next_noop_dpyinfo = x_display_list;
9627
9628 XNoOp (next_noop_dpyinfo->display);
9629
9630 /* Each time we get here, cycle through the displays now open. */
9631 next_noop_dpyinfo = next_noop_dpyinfo->next;
ef2a22d0
RS
9632 }
9633 }
502add23 9634
06a2c219 9635 /* If the focus was just given to an auto-raising frame,
0134a210 9636 raise it now. */
7a13e894 9637 /* ??? This ought to be able to handle more than one such frame. */
0134a210
RS
9638 if (pending_autoraise_frame)
9639 {
9640 x_raise_frame (pending_autoraise_frame);
9641 pending_autoraise_frame = 0;
9642 }
0134a210 9643
dc6f92b8
JB
9644 UNBLOCK_INPUT;
9645 return count;
9646}
06a2c219
GM
9647
9648
9649
dc6f92b8 9650\f
06a2c219
GM
9651/***********************************************************************
9652 Text Cursor
9653 ***********************************************************************/
9654
9655/* Note if the text cursor of window W has been overwritten by a
9656 drawing operation that outputs N glyphs starting at HPOS in the
9657 line given by output_cursor.vpos. N < 0 means all the rest of the
9658 line after HPOS has been written. */
9659
9660static void
9661note_overwritten_text_cursor (w, hpos, n)
9662 struct window *w;
9663 int hpos, n;
9664{
9665 if (updated_area == TEXT_AREA
9666 && output_cursor.vpos == w->phys_cursor.vpos
9667 && hpos <= w->phys_cursor.hpos
9668 && (n < 0
9669 || hpos + n > w->phys_cursor.hpos))
9670 w->phys_cursor_on_p = 0;
9671}
f451eb13
JB
9672
9673
06a2c219
GM
9674/* Set clipping for output in glyph row ROW. W is the window in which
9675 we operate. GC is the graphics context to set clipping in.
9676 WHOLE_LINE_P non-zero means include the areas used for truncation
9677 mark display and alike in the clipping rectangle.
9678
9679 ROW may be a text row or, e.g., a mode line. Text rows must be
9680 clipped to the interior of the window dedicated to text display,
9681 mode lines must be clipped to the whole window. */
dc6f92b8
JB
9682
9683static void
06a2c219
GM
9684x_clip_to_row (w, row, gc, whole_line_p)
9685 struct window *w;
9686 struct glyph_row *row;
9687 GC gc;
9688 int whole_line_p;
dc6f92b8 9689{
06a2c219
GM
9690 struct frame *f = XFRAME (WINDOW_FRAME (w));
9691 XRectangle clip_rect;
9692 int window_x, window_y, window_width, window_height;
dc6f92b8 9693
06a2c219 9694 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5c1aae96 9695
06a2c219
GM
9696 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
9697 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
9698 clip_rect.y = max (clip_rect.y, window_y);
9699 clip_rect.width = window_width;
9700 clip_rect.height = row->visible_height;
5c1aae96 9701
06a2c219
GM
9702 /* If clipping to the whole line, including trunc marks, extend
9703 the rectangle to the left and increase its width. */
9704 if (whole_line_p)
9705 {
9706 clip_rect.x -= FRAME_X_FLAGS_AREA_WIDTH (f);
9707 clip_rect.width += 2 * FRAME_X_FLAGS_AREA_WIDTH (f);
9708 }
5c1aae96 9709
06a2c219 9710 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
dc6f92b8
JB
9711}
9712
06a2c219
GM
9713
9714/* Draw a hollow box cursor on window W in glyph row ROW. */
dc6f92b8
JB
9715
9716static void
06a2c219
GM
9717x_draw_hollow_cursor (w, row)
9718 struct window *w;
9719 struct glyph_row *row;
dc6f92b8 9720{
06a2c219
GM
9721 struct frame *f = XFRAME (WINDOW_FRAME (w));
9722 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9723 Display *dpy = FRAME_X_DISPLAY (f);
9724 int x, y, wd, h;
9725 XGCValues xgcv;
9726 struct glyph *cursor_glyph;
9727 GC gc;
9728
9729 /* Compute frame-relative coordinates from window-relative
9730 coordinates. */
9731 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9732 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
9733 + row->ascent - w->phys_cursor_ascent);
9734 h = row->height - 1;
9735
9736 /* Get the glyph the cursor is on. If we can't tell because
9737 the current matrix is invalid or such, give up. */
9738 cursor_glyph = get_phys_cursor_glyph (w);
9739 if (cursor_glyph == NULL)
dc6f92b8
JB
9740 return;
9741
06a2c219
GM
9742 /* Compute the width of the rectangle to draw. If on a stretch
9743 glyph, and `x-stretch-block-cursor' is nil, don't draw a
9744 rectangle as wide as the glyph, but use a canonical character
9745 width instead. */
9746 wd = cursor_glyph->pixel_width - 1;
9747 if (cursor_glyph->type == STRETCH_GLYPH
9748 && !x_stretch_cursor_p)
9749 wd = min (CANON_X_UNIT (f), wd);
9750
9751 /* The foreground of cursor_gc is typically the same as the normal
9752 background color, which can cause the cursor box to be invisible. */
9753 xgcv.foreground = f->output_data.x->cursor_pixel;
9754 if (dpyinfo->scratch_cursor_gc)
9755 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
9756 else
9757 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
9758 GCForeground, &xgcv);
9759 gc = dpyinfo->scratch_cursor_gc;
9760
9761 /* Set clipping, draw the rectangle, and reset clipping again. */
9762 x_clip_to_row (w, row, gc, 0);
9763 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
9764 XSetClipMask (dpy, gc, None);
dc6f92b8
JB
9765}
9766
06a2c219
GM
9767
9768/* Draw a bar cursor on window W in glyph row ROW.
9769
9770 Implementation note: One would like to draw a bar cursor with an
9771 angle equal to the one given by the font property XA_ITALIC_ANGLE.
9772 Unfortunately, I didn't find a font yet that has this property set.
9773 --gerd. */
dc6f92b8
JB
9774
9775static void
06a2c219
GM
9776x_draw_bar_cursor (w, row)
9777 struct window *w;
9778 struct glyph_row *row;
dc6f92b8 9779{
06a2c219
GM
9780 /* If cursor hpos is out of bounds, don't draw garbage. This can
9781 happen in mini-buffer windows when switching between echo area
9782 glyphs and mini-buffer. */
9783 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
9784 {
9785 struct frame *f = XFRAME (w->frame);
9786 struct glyph *cursor_glyph;
9787 GC gc;
9788 int x;
9789 unsigned long mask;
9790 XGCValues xgcv;
9791 Display *dpy;
9792 Window window;
9793
9794 cursor_glyph = get_phys_cursor_glyph (w);
9795 if (cursor_glyph == NULL)
9796 return;
9797
9798 xgcv.background = f->output_data.x->cursor_pixel;
9799 xgcv.foreground = f->output_data.x->cursor_pixel;
9800 xgcv.graphics_exposures = 0;
9801 mask = GCForeground | GCBackground | GCGraphicsExposures;
9802 dpy = FRAME_X_DISPLAY (f);
9803 window = FRAME_X_WINDOW (f);
9804 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
9805
9806 if (gc)
9807 XChangeGC (dpy, gc, mask, &xgcv);
9808 else
9809 {
9810 gc = XCreateGC (dpy, window, mask, &xgcv);
9811 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
9812 }
9813
9814 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9815 x_clip_to_row (w, row, gc, 0);
9816 XFillRectangle (dpy, window, gc,
9817 x,
9818 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
9819 min (cursor_glyph->pixel_width,
9820 f->output_data.x->cursor_width),
9821 row->height);
9822 XSetClipMask (dpy, gc, None);
9823 }
dc6f92b8
JB
9824}
9825
06a2c219
GM
9826
9827/* Clear the cursor of window W to background color, and mark the
9828 cursor as not shown. This is used when the text where the cursor
9829 is is about to be rewritten. */
9830
dc6f92b8 9831static void
06a2c219
GM
9832x_clear_cursor (w)
9833 struct window *w;
dc6f92b8 9834{
06a2c219
GM
9835 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
9836 x_update_window_cursor (w, 0);
9837}
90e65f07 9838
dbc4e1c1 9839
06a2c219
GM
9840/* Draw the cursor glyph of window W in glyph row ROW. See the
9841 comment of x_draw_glyphs for the meaning of HL. */
dbc4e1c1 9842
06a2c219
GM
9843static void
9844x_draw_phys_cursor_glyph (w, row, hl)
9845 struct window *w;
9846 struct glyph_row *row;
9847 enum draw_glyphs_face hl;
9848{
9849 /* If cursor hpos is out of bounds, don't draw garbage. This can
9850 happen in mini-buffer windows when switching between echo area
9851 glyphs and mini-buffer. */
9852 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
66ac4b0e
GM
9853 {
9854 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
9855 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
9856 hl, 0, 0, 0);
9857
9858 /* When we erase the cursor, and ROW is overlapped by other
9859 rows, make sure that these overlapping parts of other rows
9860 are redrawn. */
9861 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
9862 {
9863 if (row > w->current_matrix->rows
9864 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
9865 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
9866
9867 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
9868 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
9869 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
9870 }
9871 }
06a2c219 9872}
dbc4e1c1 9873
eea6af04 9874
06a2c219 9875/* Erase the image of a cursor of window W from the screen. */
eea6af04 9876
06a2c219
GM
9877static void
9878x_erase_phys_cursor (w)
9879 struct window *w;
9880{
9881 struct frame *f = XFRAME (w->frame);
9882 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9883 int hpos = w->phys_cursor.hpos;
9884 int vpos = w->phys_cursor.vpos;
9885 int mouse_face_here_p = 0;
9886 struct glyph_matrix *active_glyphs = w->current_matrix;
9887 struct glyph_row *cursor_row;
9888 struct glyph *cursor_glyph;
9889 enum draw_glyphs_face hl;
9890
9891 /* No cursor displayed or row invalidated => nothing to do on the
9892 screen. */
9893 if (w->phys_cursor_type == NO_CURSOR)
9894 goto mark_cursor_off;
9895
9896 /* VPOS >= active_glyphs->nrows means that window has been resized.
9897 Don't bother to erase the cursor. */
9898 if (vpos >= active_glyphs->nrows)
9899 goto mark_cursor_off;
9900
9901 /* If row containing cursor is marked invalid, there is nothing we
9902 can do. */
9903 cursor_row = MATRIX_ROW (active_glyphs, vpos);
9904 if (!cursor_row->enabled_p)
9905 goto mark_cursor_off;
9906
9907 /* This can happen when the new row is shorter than the old one.
9908 In this case, either x_draw_glyphs or clear_end_of_line
9909 should have cleared the cursor. Note that we wouldn't be
9910 able to erase the cursor in this case because we don't have a
9911 cursor glyph at hand. */
9912 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
9913 goto mark_cursor_off;
9914
9915 /* If the cursor is in the mouse face area, redisplay that when
9916 we clear the cursor. */
9917 if (w == XWINDOW (dpyinfo->mouse_face_window)
9918 && (vpos > dpyinfo->mouse_face_beg_row
9919 || (vpos == dpyinfo->mouse_face_beg_row
9920 && hpos >= dpyinfo->mouse_face_beg_col))
9921 && (vpos < dpyinfo->mouse_face_end_row
9922 || (vpos == dpyinfo->mouse_face_end_row
9923 && hpos < dpyinfo->mouse_face_end_col))
9924 /* Don't redraw the cursor's spot in mouse face if it is at the
9925 end of a line (on a newline). The cursor appears there, but
9926 mouse highlighting does not. */
9927 && cursor_row->used[TEXT_AREA] > hpos)
9928 mouse_face_here_p = 1;
9929
9930 /* Maybe clear the display under the cursor. */
9931 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
9932 {
9933 int x;
9934 int top_line_height = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
dbc4e1c1 9935
06a2c219
GM
9936 cursor_glyph = get_phys_cursor_glyph (w);
9937 if (cursor_glyph == NULL)
9938 goto mark_cursor_off;
dbc4e1c1 9939
06a2c219
GM
9940 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
9941
9942 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9943 x,
9944 WINDOW_TO_FRAME_PIXEL_Y (w, max (top_line_height,
9945 cursor_row->y)),
9946 cursor_glyph->pixel_width,
9947 cursor_row->visible_height,
9948 False);
dbc4e1c1 9949 }
06a2c219
GM
9950
9951 /* Erase the cursor by redrawing the character underneath it. */
9952 if (mouse_face_here_p)
9953 hl = DRAW_MOUSE_FACE;
9954 else if (cursor_row->inverse_p)
9955 hl = DRAW_INVERSE_VIDEO;
9956 else
9957 hl = DRAW_NORMAL_TEXT;
9958 x_draw_phys_cursor_glyph (w, cursor_row, hl);
dbc4e1c1 9959
06a2c219
GM
9960 mark_cursor_off:
9961 w->phys_cursor_on_p = 0;
9962 w->phys_cursor_type = NO_CURSOR;
dbc4e1c1
JB
9963}
9964
9965
06a2c219
GM
9966/* Display or clear cursor of window W. If ON is zero, clear the
9967 cursor. If it is non-zero, display the cursor. If ON is nonzero,
9968 where to put the cursor is specified by HPOS, VPOS, X and Y. */
dbc4e1c1 9969
06a2c219
GM
9970void
9971x_display_and_set_cursor (w, on, hpos, vpos, x, y)
9972 struct window *w;
9973 int on, hpos, vpos, x, y;
dbc4e1c1 9974{
06a2c219
GM
9975 struct frame *f = XFRAME (w->frame);
9976 int new_cursor_type;
9977 struct glyph_matrix *current_glyphs;
9978 struct glyph_row *glyph_row;
9979 struct glyph *glyph;
dbc4e1c1 9980
49d838ea 9981 /* This is pointless on invisible frames, and dangerous on garbaged
06a2c219
GM
9982 windows and frames; in the latter case, the frame or window may
9983 be in the midst of changing its size, and x and y may be off the
9984 window. */
9985 if (! FRAME_VISIBLE_P (f)
9986 || FRAME_GARBAGED_P (f)
9987 || vpos >= w->current_matrix->nrows
9988 || hpos >= w->current_matrix->matrix_w)
dc6f92b8
JB
9989 return;
9990
9991 /* If cursor is off and we want it off, return quickly. */
06a2c219 9992 if (!on && !w->phys_cursor_on_p)
dc6f92b8
JB
9993 return;
9994
06a2c219
GM
9995 current_glyphs = w->current_matrix;
9996 glyph_row = MATRIX_ROW (current_glyphs, vpos);
9997 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
9998
9999 /* If cursor row is not enabled, we don't really know where to
10000 display the cursor. */
10001 if (!glyph_row->enabled_p)
10002 {
10003 w->phys_cursor_on_p = 0;
10004 return;
10005 }
10006
10007 xassert (interrupt_input_blocked);
10008
10009 /* Set new_cursor_type to the cursor we want to be displayed. In a
10010 mini-buffer window, we want the cursor only to appear if we are
10011 reading input from this window. For the selected window, we want
10012 the cursor type given by the frame parameter. If explicitly
10013 marked off, draw no cursor. In all other cases, we want a hollow
10014 box cursor. */
9b4a7047
GM
10015 if (cursor_in_echo_area
10016 && FRAME_HAS_MINIBUF_P (f)
10017 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
06a2c219 10018 {
9b4a7047
GM
10019 if (w == XWINDOW (echo_area_window))
10020 new_cursor_type = FRAME_DESIRED_CURSOR (f);
06a2c219
GM
10021 else
10022 new_cursor_type = HOLLOW_BOX_CURSOR;
10023 }
06a2c219 10024 else
9b4a7047
GM
10025 {
10026 if (w != XWINDOW (selected_window)
10027 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
10028 {
10029 if (MINI_WINDOW_P (w))
10030 new_cursor_type = NO_CURSOR;
10031 else
10032 new_cursor_type = HOLLOW_BOX_CURSOR;
10033 }
10034 else if (w->cursor_off_p)
10035 new_cursor_type = NO_CURSOR;
10036 else
10037 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10038 }
06a2c219
GM
10039
10040 /* If cursor is currently being shown and we don't want it to be or
10041 it is in the wrong place, or the cursor type is not what we want,
dc6f92b8 10042 erase it. */
06a2c219 10043 if (w->phys_cursor_on_p
dc6f92b8 10044 && (!on
06a2c219
GM
10045 || w->phys_cursor.x != x
10046 || w->phys_cursor.y != y
10047 || new_cursor_type != w->phys_cursor_type))
10048 x_erase_phys_cursor (w);
10049
10050 /* If the cursor is now invisible and we want it to be visible,
10051 display it. */
10052 if (on && !w->phys_cursor_on_p)
10053 {
10054 w->phys_cursor_ascent = glyph_row->ascent;
10055 w->phys_cursor_height = glyph_row->height;
10056
10057 /* Set phys_cursor_.* before x_draw_.* is called because some
10058 of them may need the information. */
10059 w->phys_cursor.x = x;
10060 w->phys_cursor.y = glyph_row->y;
10061 w->phys_cursor.hpos = hpos;
10062 w->phys_cursor.vpos = vpos;
10063 w->phys_cursor_type = new_cursor_type;
10064 w->phys_cursor_on_p = 1;
10065
10066 switch (new_cursor_type)
dc6f92b8 10067 {
06a2c219
GM
10068 case HOLLOW_BOX_CURSOR:
10069 x_draw_hollow_cursor (w, glyph_row);
10070 break;
10071
10072 case FILLED_BOX_CURSOR:
10073 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10074 break;
10075
10076 case BAR_CURSOR:
10077 x_draw_bar_cursor (w, glyph_row);
10078 break;
10079
10080 case NO_CURSOR:
10081 break;
dc6f92b8 10082
06a2c219
GM
10083 default:
10084 abort ();
10085 }
dc6f92b8
JB
10086 }
10087
06a2c219 10088#ifndef XFlush
f676886a 10089 if (updating_frame != f)
334208b7 10090 XFlush (FRAME_X_DISPLAY (f));
06a2c219 10091#endif
dc6f92b8
JB
10092}
10093
06a2c219
GM
10094
10095/* Display the cursor on window W, or clear it. X and Y are window
10096 relative pixel coordinates. HPOS and VPOS are glyph matrix
10097 positions. If W is not the selected window, display a hollow
10098 cursor. ON non-zero means display the cursor at X, Y which
10099 correspond to HPOS, VPOS, otherwise it is cleared. */
5d46f928 10100
dfcf069d 10101void
06a2c219
GM
10102x_display_cursor (w, on, hpos, vpos, x, y)
10103 struct window *w;
10104 int on, hpos, vpos, x, y;
dc6f92b8 10105{
f94397b5 10106 BLOCK_INPUT;
06a2c219 10107 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
5d46f928
RS
10108 UNBLOCK_INPUT;
10109}
10110
06a2c219
GM
10111
10112/* Display the cursor on window W, or clear it, according to ON_P.
5d46f928
RS
10113 Don't change the cursor's position. */
10114
dfcf069d 10115void
06a2c219 10116x_update_cursor (f, on_p)
5d46f928 10117 struct frame *f;
5d46f928 10118{
06a2c219
GM
10119 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
10120}
10121
10122
10123/* Call x_update_window_cursor with parameter ON_P on all leaf windows
10124 in the window tree rooted at W. */
10125
10126static void
10127x_update_cursor_in_window_tree (w, on_p)
10128 struct window *w;
10129 int on_p;
10130{
10131 while (w)
10132 {
10133 if (!NILP (w->hchild))
10134 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10135 else if (!NILP (w->vchild))
10136 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10137 else
10138 x_update_window_cursor (w, on_p);
10139
10140 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10141 }
10142}
5d46f928 10143
f94397b5 10144
06a2c219
GM
10145/* Switch the display of W's cursor on or off, according to the value
10146 of ON. */
10147
10148static void
10149x_update_window_cursor (w, on)
10150 struct window *w;
10151 int on;
10152{
10153 BLOCK_INPUT;
10154 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
10155 w->phys_cursor.x, w->phys_cursor.y);
f94397b5 10156 UNBLOCK_INPUT;
dc6f92b8 10157}
06a2c219
GM
10158
10159
10160
dc6f92b8
JB
10161\f
10162/* Icons. */
10163
f676886a 10164/* Refresh bitmap kitchen sink icon for frame F
06a2c219 10165 when we get an expose event for it. */
dc6f92b8 10166
dfcf069d 10167void
f676886a
JB
10168refreshicon (f)
10169 struct frame *f;
dc6f92b8 10170{
06a2c219 10171 /* Normally, the window manager handles this function. */
dc6f92b8
JB
10172}
10173
dbc4e1c1 10174/* Make the x-window of frame F use the gnu icon bitmap. */
dc6f92b8
JB
10175
10176int
990ba854 10177x_bitmap_icon (f, file)
f676886a 10178 struct frame *f;
990ba854 10179 Lisp_Object file;
dc6f92b8 10180{
06a2c219 10181 int bitmap_id;
dc6f92b8 10182
c118dd06 10183 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
10184 return 1;
10185
990ba854 10186 /* Free up our existing icon bitmap if any. */
7556890b
RS
10187 if (f->output_data.x->icon_bitmap > 0)
10188 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10189 f->output_data.x->icon_bitmap = 0;
990ba854
RS
10190
10191 if (STRINGP (file))
7f2ae036
RS
10192 bitmap_id = x_create_bitmap_from_file (f, file);
10193 else
10194 {
990ba854 10195 /* Create the GNU bitmap if necessary. */
5bf01b68 10196 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
334208b7
RS
10197 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
10198 = x_create_bitmap_from_data (f, gnu_bits,
10199 gnu_width, gnu_height);
990ba854
RS
10200
10201 /* The first time we create the GNU bitmap,
06a2c219 10202 this increments the ref-count one extra time.
990ba854
RS
10203 As a result, the GNU bitmap is never freed.
10204 That way, we don't have to worry about allocating it again. */
334208b7 10205 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
990ba854 10206
334208b7 10207 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7f2ae036
RS
10208 }
10209
10210 x_wm_set_icon_pixmap (f, bitmap_id);
7556890b 10211 f->output_data.x->icon_bitmap = bitmap_id;
dc6f92b8
JB
10212
10213 return 0;
10214}
10215
10216
1be2d067
KH
10217/* Make the x-window of frame F use a rectangle with text.
10218 Use ICON_NAME as the text. */
dc6f92b8
JB
10219
10220int
f676886a
JB
10221x_text_icon (f, icon_name)
10222 struct frame *f;
dc6f92b8
JB
10223 char *icon_name;
10224{
c118dd06 10225 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
10226 return 1;
10227
1be2d067
KH
10228#ifdef HAVE_X11R4
10229 {
10230 XTextProperty text;
10231 text.value = (unsigned char *) icon_name;
10232 text.encoding = XA_STRING;
10233 text.format = 8;
10234 text.nitems = strlen (icon_name);
10235#ifdef USE_X_TOOLKIT
7556890b 10236 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
1be2d067
KH
10237 &text);
10238#else /* not USE_X_TOOLKIT */
10239 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
10240#endif /* not USE_X_TOOLKIT */
10241 }
10242#else /* not HAVE_X11R4 */
10243 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
10244#endif /* not HAVE_X11R4 */
58769bee 10245
7556890b
RS
10246 if (f->output_data.x->icon_bitmap > 0)
10247 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10248 f->output_data.x->icon_bitmap = 0;
b1c884c3 10249 x_wm_set_icon_pixmap (f, 0);
dc6f92b8
JB
10250
10251 return 0;
10252}
10253\f
e99db5a1
RS
10254#define X_ERROR_MESSAGE_SIZE 200
10255
10256/* If non-nil, this should be a string.
10257 It means catch X errors and store the error message in this string. */
10258
10259static Lisp_Object x_error_message_string;
10260
10261/* An X error handler which stores the error message in
10262 x_error_message_string. This is called from x_error_handler if
10263 x_catch_errors is in effect. */
10264
06a2c219 10265static void
e99db5a1
RS
10266x_error_catcher (display, error)
10267 Display *display;
10268 XErrorEvent *error;
10269{
10270 XGetErrorText (display, error->error_code,
10271 XSTRING (x_error_message_string)->data,
10272 X_ERROR_MESSAGE_SIZE);
10273}
10274
10275/* Begin trapping X errors for display DPY. Actually we trap X errors
10276 for all displays, but DPY should be the display you are actually
10277 operating on.
10278
10279 After calling this function, X protocol errors no longer cause
10280 Emacs to exit; instead, they are recorded in the string
10281 stored in x_error_message_string.
10282
10283 Calling x_check_errors signals an Emacs error if an X error has
10284 occurred since the last call to x_catch_errors or x_check_errors.
10285
10286 Calling x_uncatch_errors resumes the normal error handling. */
10287
10288void x_check_errors ();
10289static Lisp_Object x_catch_errors_unwind ();
10290
10291int
10292x_catch_errors (dpy)
10293 Display *dpy;
10294{
10295 int count = specpdl_ptr - specpdl;
10296
10297 /* Make sure any errors from previous requests have been dealt with. */
10298 XSync (dpy, False);
10299
10300 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
10301
10302 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
10303 XSTRING (x_error_message_string)->data[0] = 0;
10304
10305 return count;
10306}
10307
10308/* Unbind the binding that we made to check for X errors. */
10309
10310static Lisp_Object
10311x_catch_errors_unwind (old_val)
10312 Lisp_Object old_val;
10313{
10314 x_error_message_string = old_val;
10315 return Qnil;
10316}
10317
10318/* If any X protocol errors have arrived since the last call to
10319 x_catch_errors or x_check_errors, signal an Emacs error using
10320 sprintf (a buffer, FORMAT, the x error message text) as the text. */
10321
10322void
10323x_check_errors (dpy, format)
10324 Display *dpy;
10325 char *format;
10326{
10327 /* Make sure to catch any errors incurred so far. */
10328 XSync (dpy, False);
10329
10330 if (XSTRING (x_error_message_string)->data[0])
10331 error (format, XSTRING (x_error_message_string)->data);
10332}
10333
9829ddba
RS
10334/* Nonzero if we had any X protocol errors
10335 since we did x_catch_errors on DPY. */
e99db5a1
RS
10336
10337int
10338x_had_errors_p (dpy)
10339 Display *dpy;
10340{
10341 /* Make sure to catch any errors incurred so far. */
10342 XSync (dpy, False);
10343
10344 return XSTRING (x_error_message_string)->data[0] != 0;
10345}
10346
9829ddba
RS
10347/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
10348
06a2c219 10349void
9829ddba
RS
10350x_clear_errors (dpy)
10351 Display *dpy;
10352{
10353 XSTRING (x_error_message_string)->data[0] = 0;
10354}
10355
e99db5a1
RS
10356/* Stop catching X protocol errors and let them make Emacs die.
10357 DPY should be the display that was passed to x_catch_errors.
10358 COUNT should be the value that was returned by
10359 the corresponding call to x_catch_errors. */
10360
10361void
10362x_uncatch_errors (dpy, count)
10363 Display *dpy;
10364 int count;
10365{
10366 unbind_to (count, Qnil);
10367}
10368
10369#if 0
10370static unsigned int x_wire_count;
10371x_trace_wire ()
10372{
10373 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
10374}
10375#endif /* ! 0 */
10376
10377\f
10378/* Handle SIGPIPE, which can happen when the connection to a server
10379 simply goes away. SIGPIPE is handled by x_connection_signal.
10380 Don't need to do anything, because the write which caused the
10381 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
06a2c219 10382 which will do the appropriate cleanup for us. */
e99db5a1
RS
10383
10384static SIGTYPE
10385x_connection_signal (signalnum) /* If we don't have an argument, */
06a2c219 10386 int signalnum; /* some compilers complain in signal calls. */
e99db5a1
RS
10387{
10388#ifdef USG
10389 /* USG systems forget handlers when they are used;
10390 must reestablish each time */
10391 signal (signalnum, x_connection_signal);
10392#endif /* USG */
10393}
10394\f
4746118a
JB
10395/* Handling X errors. */
10396
7a13e894 10397/* Handle the loss of connection to display DISPLAY. */
16bd92ea 10398
4746118a 10399static SIGTYPE
7a13e894
RS
10400x_connection_closed (display, error_message)
10401 Display *display;
10402 char *error_message;
4746118a 10403{
7a13e894
RS
10404 struct x_display_info *dpyinfo = x_display_info_for_display (display);
10405 Lisp_Object frame, tail;
10406
6186a4a0
RS
10407 /* Indicate that this display is dead. */
10408
f613a4c8 10409#ifdef USE_X_TOOLKIT
adabc3a9 10410 XtCloseDisplay (display);
f613a4c8 10411#endif
adabc3a9 10412
6186a4a0
RS
10413 dpyinfo->display = 0;
10414
06a2c219 10415 /* First delete frames whose mini-buffers are on frames
7a13e894
RS
10416 that are on the dead display. */
10417 FOR_EACH_FRAME (tail, frame)
10418 {
10419 Lisp_Object minibuf_frame;
10420 minibuf_frame
10421 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
f48f33ca
RS
10422 if (FRAME_X_P (XFRAME (frame))
10423 && FRAME_X_P (XFRAME (minibuf_frame))
10424 && ! EQ (frame, minibuf_frame)
10425 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7a13e894
RS
10426 Fdelete_frame (frame, Qt);
10427 }
10428
10429 /* Now delete all remaining frames on the dead display.
06a2c219 10430 We are now sure none of these is used as the mini-buffer
7a13e894
RS
10431 for another frame that we need to delete. */
10432 FOR_EACH_FRAME (tail, frame)
f48f33ca
RS
10433 if (FRAME_X_P (XFRAME (frame))
10434 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
07a7096a
KH
10435 {
10436 /* Set this to t so that Fdelete_frame won't get confused
10437 trying to find a replacement. */
10438 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
10439 Fdelete_frame (frame, Qt);
10440 }
7a13e894 10441
482a1bd2
KH
10442 if (dpyinfo)
10443 x_delete_display (dpyinfo);
7a13e894
RS
10444
10445 if (x_display_list == 0)
10446 {
f8d07b62 10447 fprintf (stderr, "%s\n", error_message);
7a13e894
RS
10448 shut_down_emacs (0, 0, Qnil);
10449 exit (70);
10450 }
12ba150f 10451
7a13e894
RS
10452 /* Ordinary stack unwind doesn't deal with these. */
10453#ifdef SIGIO
10454 sigunblock (sigmask (SIGIO));
10455#endif
10456 sigunblock (sigmask (SIGALRM));
10457 TOTALLY_UNBLOCK_INPUT;
10458
aa4d9a9e 10459 clear_waiting_for_input ();
7a13e894 10460 error ("%s", error_message);
4746118a
JB
10461}
10462
7a13e894
RS
10463/* This is the usual handler for X protocol errors.
10464 It kills all frames on the display that we got the error for.
10465 If that was the only one, it prints an error message and kills Emacs. */
10466
06a2c219 10467static void
c118dd06
JB
10468x_error_quitter (display, error)
10469 Display *display;
10470 XErrorEvent *error;
10471{
7a13e894 10472 char buf[256], buf1[356];
dc6f92b8 10473
58769bee 10474 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 10475 original error handler. */
dc6f92b8 10476
c118dd06 10477 XGetErrorText (display, error->error_code, buf, sizeof (buf));
0a0fdc70 10478 sprintf (buf1, "X protocol error: %s on protocol request %d",
c118dd06 10479 buf, error->request_code);
7a13e894 10480 x_connection_closed (display, buf1);
dc6f92b8
JB
10481}
10482
e99db5a1
RS
10483/* This is the first-level handler for X protocol errors.
10484 It calls x_error_quitter or x_error_catcher. */
7a13e894 10485
8922af5f 10486static int
e99db5a1 10487x_error_handler (display, error)
8922af5f 10488 Display *display;
e99db5a1 10489 XErrorEvent *error;
8922af5f 10490{
e99db5a1
RS
10491 if (! NILP (x_error_message_string))
10492 x_error_catcher (display, error);
10493 else
10494 x_error_quitter (display, error);
06a2c219 10495 return 0;
f9e24cb9 10496}
c118dd06 10497
e99db5a1
RS
10498/* This is the handler for X IO errors, always.
10499 It kills all frames on the display that we lost touch with.
10500 If that was the only one, it prints an error message and kills Emacs. */
7a13e894 10501
c118dd06 10502static int
e99db5a1 10503x_io_error_quitter (display)
c118dd06 10504 Display *display;
c118dd06 10505{
e99db5a1 10506 char buf[256];
dc6f92b8 10507
e99db5a1
RS
10508 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
10509 x_connection_closed (display, buf);
06a2c219 10510 return 0;
dc6f92b8 10511}
dc6f92b8 10512\f
f451eb13
JB
10513/* Changing the font of the frame. */
10514
76bcdf39
RS
10515/* Give frame F the font named FONTNAME as its default font, and
10516 return the full name of that font. FONTNAME may be a wildcard
10517 pattern; in that case, we choose some font that fits the pattern.
10518 The return value shows which font we chose. */
10519
b5cf7a0e 10520Lisp_Object
f676886a
JB
10521x_new_font (f, fontname)
10522 struct frame *f;
dc6f92b8
JB
10523 register char *fontname;
10524{
dc43ef94
KH
10525 struct font_info *fontp
10526 = fs_load_font (f, FRAME_X_FONT_TABLE (f), CHARSET_ASCII, fontname, -1);
dc6f92b8 10527
dc43ef94
KH
10528 if (!fontp)
10529 return Qnil;
2224a5fc 10530
dc43ef94
KH
10531 f->output_data.x->font = (XFontStruct *) (fontp->font);
10532 f->output_data.x->font_baseline
10533 = (f->output_data.x->font->ascent + fontp->baseline_offset);
10534 f->output_data.x->fontset = -1;
10535
b2cad826
KH
10536 /* Compute the scroll bar width in character columns. */
10537 if (f->scroll_bar_pixel_width > 0)
10538 {
7556890b 10539 int wid = FONT_WIDTH (f->output_data.x->font);
b2cad826
KH
10540 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
10541 }
10542 else
4e61bddf
RS
10543 {
10544 int wid = FONT_WIDTH (f->output_data.x->font);
10545 f->scroll_bar_cols = (14 + wid - 1) / wid;
10546 }
b2cad826 10547
f676886a 10548 /* Now make the frame display the given font. */
c118dd06 10549 if (FRAME_X_WINDOW (f) != 0)
dc6f92b8 10550 {
7556890b
RS
10551 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
10552 f->output_data.x->font->fid);
10553 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
10554 f->output_data.x->font->fid);
10555 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
10556 f->output_data.x->font->fid);
f676886a 10557
a27f9f86 10558 frame_update_line_height (f);
0134a210 10559 x_set_window_size (f, 0, f->width, f->height);
dc6f92b8 10560 }
a27f9f86
RS
10561 else
10562 /* If we are setting a new frame's font for the first time,
10563 there are no faces yet, so this font's height is the line height. */
7556890b 10564 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
dc6f92b8 10565
dc43ef94
KH
10566 return build_string (fontp->full_name);
10567}
10568
10569/* Give frame F the fontset named FONTSETNAME as its default font, and
10570 return the full name of that fontset. FONTSETNAME may be a wildcard
b5210ea7
KH
10571 pattern; in that case, we choose some fontset that fits the pattern.
10572 The return value shows which fontset we chose. */
b5cf7a0e 10573
dc43ef94
KH
10574Lisp_Object
10575x_new_fontset (f, fontsetname)
10576 struct frame *f;
10577 char *fontsetname;
10578{
10579 int fontset = fs_query_fontset (f, fontsetname);
10580 struct fontset_info *fontsetp;
10581 Lisp_Object result;
b5cf7a0e 10582
dc43ef94
KH
10583 if (fontset < 0)
10584 return Qnil;
b5cf7a0e 10585
2da424f1
KH
10586 if (f->output_data.x->fontset == fontset)
10587 /* This fontset is already set in frame F. There's nothing more
10588 to do. */
10589 return build_string (fontsetname);
10590
dc43ef94
KH
10591 fontsetp = FRAME_FONTSET_DATA (f)->fontset_table[fontset];
10592
10593 if (!fontsetp->fontname[CHARSET_ASCII])
10594 /* This fontset doesn't contain ASCII font. */
10595 return Qnil;
10596
10597 result = x_new_font (f, fontsetp->fontname[CHARSET_ASCII]);
10598
10599 if (!STRINGP (result))
10600 /* Can't load ASCII font. */
10601 return Qnil;
10602
10603 /* Since x_new_font doesn't update any fontset information, do it now. */
10604 f->output_data.x->fontset = fontset;
2da424f1 10605 FS_LOAD_FONT (f, FRAME_X_FONT_TABLE (f),
715a159b 10606 CHARSET_ASCII, fontsetp->fontname[CHARSET_ASCII], fontset);
dc43ef94
KH
10607
10608 return build_string (fontsetname);
dc6f92b8 10609}
dc6f92b8 10610\f
2e365682
RS
10611/* Calculate the absolute position in frame F
10612 from its current recorded position values and gravity. */
10613
dfcf069d 10614void
43bca5d5 10615x_calc_absolute_position (f)
f676886a 10616 struct frame *f;
dc6f92b8 10617{
06a2c219 10618 Window child;
6dba1858 10619 int win_x = 0, win_y = 0;
7556890b 10620 int flags = f->output_data.x->size_hint_flags;
c81412a0
KH
10621 int this_window;
10622
9829ddba
RS
10623 /* We have nothing to do if the current position
10624 is already for the top-left corner. */
10625 if (! ((flags & XNegative) || (flags & YNegative)))
10626 return;
10627
c81412a0 10628#ifdef USE_X_TOOLKIT
7556890b 10629 this_window = XtWindow (f->output_data.x->widget);
c81412a0
KH
10630#else
10631 this_window = FRAME_X_WINDOW (f);
10632#endif
6dba1858
RS
10633
10634 /* Find the position of the outside upper-left corner of
9829ddba
RS
10635 the inner window, with respect to the outer window.
10636 But do this only if we will need the results. */
7556890b 10637 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6dba1858 10638 {
9829ddba
RS
10639 int count;
10640
6dba1858 10641 BLOCK_INPUT;
9829ddba
RS
10642 count = x_catch_errors (FRAME_X_DISPLAY (f));
10643 while (1)
10644 {
10645 x_clear_errors (FRAME_X_DISPLAY (f));
10646 XTranslateCoordinates (FRAME_X_DISPLAY (f),
10647
10648 /* From-window, to-window. */
10649 this_window,
10650 f->output_data.x->parent_desc,
10651
10652 /* From-position, to-position. */
10653 0, 0, &win_x, &win_y,
10654
10655 /* Child of win. */
10656 &child);
10657 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
10658 {
10659 Window newroot, newparent = 0xdeadbeef;
10660 Window *newchildren;
10661 int nchildren;
10662
10663 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
10664 &newparent, &newchildren, &nchildren))
10665 break;
58769bee 10666
7c3c78a3 10667 XFree ((char *) newchildren);
6dba1858 10668
9829ddba
RS
10669 f->output_data.x->parent_desc = newparent;
10670 }
10671 else
10672 break;
10673 }
6dba1858 10674
9829ddba 10675 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
6dba1858
RS
10676 UNBLOCK_INPUT;
10677 }
10678
10679 /* Treat negative positions as relative to the leftmost bottommost
10680 position that fits on the screen. */
20f55f9a 10681 if (flags & XNegative)
7556890b 10682 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
2e365682
RS
10683 - 2 * f->output_data.x->border_width - win_x
10684 - PIXEL_WIDTH (f)
10685 + f->output_data.x->left_pos);
dc6f92b8 10686
20f55f9a 10687 if (flags & YNegative)
06a2c219
GM
10688 {
10689 int menubar_height = 0;
10690
10691#ifdef USE_X_TOOLKIT
10692 if (f->output_data.x->menubar_widget)
10693 menubar_height
10694 = (f->output_data.x->menubar_widget->core.height
10695 + f->output_data.x->menubar_widget->core.border_width);
10696#endif
10697
10698 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
10699 - 2 * f->output_data.x->border_width
10700 - win_y
10701 - PIXEL_HEIGHT (f)
10702 - menubar_height
10703 + f->output_data.x->top_pos);
10704 }
2e365682 10705
3a35ab44
RS
10706 /* The left_pos and top_pos
10707 are now relative to the top and left screen edges,
10708 so the flags should correspond. */
7556890b 10709 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
dc6f92b8
JB
10710}
10711
3a35ab44
RS
10712/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
10713 to really change the position, and 0 when calling from
10714 x_make_frame_visible (in that case, XOFF and YOFF are the current
aa3ff7c9
KH
10715 position values). It is -1 when calling from x_set_frame_parameters,
10716 which means, do adjust for borders but don't change the gravity. */
3a35ab44 10717
dfcf069d 10718void
dc05a16b 10719x_set_offset (f, xoff, yoff, change_gravity)
f676886a 10720 struct frame *f;
dc6f92b8 10721 register int xoff, yoff;
dc05a16b 10722 int change_gravity;
dc6f92b8 10723{
4a4cbdd5
KH
10724 int modified_top, modified_left;
10725
aa3ff7c9 10726 if (change_gravity > 0)
3a35ab44 10727 {
7556890b
RS
10728 f->output_data.x->top_pos = yoff;
10729 f->output_data.x->left_pos = xoff;
10730 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
3a35ab44 10731 if (xoff < 0)
7556890b 10732 f->output_data.x->size_hint_flags |= XNegative;
3a35ab44 10733 if (yoff < 0)
7556890b
RS
10734 f->output_data.x->size_hint_flags |= YNegative;
10735 f->output_data.x->win_gravity = NorthWestGravity;
3a35ab44 10736 }
43bca5d5 10737 x_calc_absolute_position (f);
dc6f92b8
JB
10738
10739 BLOCK_INPUT;
c32cdd9a 10740 x_wm_set_size_hint (f, (long) 0, 0);
3a35ab44 10741
7556890b
RS
10742 modified_left = f->output_data.x->left_pos;
10743 modified_top = f->output_data.x->top_pos;
e73ec6fa
RS
10744#if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
10745 this seems to be unnecessary and incorrect. rms, 4/17/97. */
10746 /* It is a mystery why we need to add the border_width here
10747 when the frame is already visible, but experiment says we do. */
aa3ff7c9 10748 if (change_gravity != 0)
4a4cbdd5 10749 {
7556890b
RS
10750 modified_left += f->output_data.x->border_width;
10751 modified_top += f->output_data.x->border_width;
4a4cbdd5 10752 }
e73ec6fa 10753#endif
4a4cbdd5 10754
3afe33e7 10755#ifdef USE_X_TOOLKIT
7556890b 10756 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
4a4cbdd5 10757 modified_left, modified_top);
3afe33e7 10758#else /* not USE_X_TOOLKIT */
334208b7 10759 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4a4cbdd5 10760 modified_left, modified_top);
3afe33e7 10761#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
10762 UNBLOCK_INPUT;
10763}
10764
bc20ebbf
FP
10765/* Call this to change the size of frame F's x-window.
10766 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
10767 for this size change and subsequent size changes.
10768 Otherwise we leave the window gravity unchanged. */
dc6f92b8 10769
dfcf069d 10770void
bc20ebbf 10771x_set_window_size (f, change_gravity, cols, rows)
f676886a 10772 struct frame *f;
bc20ebbf 10773 int change_gravity;
b1c884c3 10774 int cols, rows;
dc6f92b8 10775{
06a2c219 10776#ifndef USE_X_TOOLKIT
dc6f92b8 10777 int pixelwidth, pixelheight;
06a2c219 10778#endif
dc6f92b8 10779
80fd1fe2 10780 BLOCK_INPUT;
aee9a898
RS
10781
10782#ifdef USE_X_TOOLKIT
3a20653d
RS
10783 {
10784 /* The x and y position of the widget is clobbered by the
10785 call to XtSetValues within EmacsFrameSetCharSize.
10786 This is a real kludge, but I don't understand Xt so I can't
10787 figure out a correct fix. Can anyone else tell me? -- rms. */
7556890b
RS
10788 int xpos = f->output_data.x->widget->core.x;
10789 int ypos = f->output_data.x->widget->core.y;
10790 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
10791 f->output_data.x->widget->core.x = xpos;
10792 f->output_data.x->widget->core.y = ypos;
3a20653d 10793 }
80fd1fe2
FP
10794
10795#else /* not USE_X_TOOLKIT */
10796
b1c884c3 10797 check_frame_size (f, &rows, &cols);
7556890b 10798 f->output_data.x->vertical_scroll_bar_extra
b2cad826
KH
10799 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
10800 ? 0
10801 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
480407eb 10802 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7556890b 10803 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
06a2c219
GM
10804 f->output_data.x->flags_areas_extra
10805 = 2 * FRAME_FLAGS_AREA_WIDTH (f);
f451eb13
JB
10806 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
10807 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
dc6f92b8 10808
7556890b 10809 f->output_data.x->win_gravity = NorthWestGravity;
c32cdd9a 10810 x_wm_set_size_hint (f, (long) 0, 0);
6ccf47d1 10811
334208b7
RS
10812 XSync (FRAME_X_DISPLAY (f), False);
10813 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10814 pixelwidth, pixelheight);
b1c884c3
JB
10815
10816 /* Now, strictly speaking, we can't be sure that this is accurate,
10817 but the window manager will get around to dealing with the size
10818 change request eventually, and we'll hear how it went when the
dbc4e1c1
JB
10819 ConfigureNotify event gets here.
10820
10821 We could just not bother storing any of this information here,
10822 and let the ConfigureNotify event set everything up, but that
fddd5ceb 10823 might be kind of confusing to the Lisp code, since size changes
dbc4e1c1 10824 wouldn't be reported in the frame parameters until some random
fddd5ceb
RS
10825 point in the future when the ConfigureNotify event arrives.
10826
10827 We pass 1 for DELAY since we can't run Lisp code inside of
10828 a BLOCK_INPUT. */
10829 change_frame_size (f, rows, cols, 0, 1);
b1c884c3
JB
10830 PIXEL_WIDTH (f) = pixelwidth;
10831 PIXEL_HEIGHT (f) = pixelheight;
10832
aee9a898
RS
10833 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
10834 receive in the ConfigureNotify event; if we get what we asked
10835 for, then the event won't cause the screen to become garbaged, so
10836 we have to make sure to do it here. */
10837 SET_FRAME_GARBAGED (f);
10838
10839 XFlush (FRAME_X_DISPLAY (f));
10840
10841#endif /* not USE_X_TOOLKIT */
10842
4d73d038 10843 /* If cursor was outside the new size, mark it as off. */
06a2c219 10844 mark_window_cursors_off (XWINDOW (f->root_window));
4d73d038 10845
aee9a898
RS
10846 /* Clear out any recollection of where the mouse highlighting was,
10847 since it might be in a place that's outside the new frame size.
10848 Actually checking whether it is outside is a pain in the neck,
10849 so don't try--just let the highlighting be done afresh with new size. */
e687d06e 10850 cancel_mouse_face (f);
dbc4e1c1 10851
dc6f92b8
JB
10852 UNBLOCK_INPUT;
10853}
dc6f92b8 10854\f
d047c4eb 10855/* Mouse warping. */
dc6f92b8 10856
9b378208 10857void
f676886a
JB
10858x_set_mouse_position (f, x, y)
10859 struct frame *f;
dc6f92b8
JB
10860 int x, y;
10861{
10862 int pix_x, pix_y;
10863
7556890b
RS
10864 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
10865 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
f451eb13
JB
10866
10867 if (pix_x < 0) pix_x = 0;
10868 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
10869
10870 if (pix_y < 0) pix_y = 0;
10871 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
dc6f92b8
JB
10872
10873 BLOCK_INPUT;
dc6f92b8 10874
334208b7
RS
10875 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
10876 0, 0, 0, 0, pix_x, pix_y);
dc6f92b8
JB
10877 UNBLOCK_INPUT;
10878}
10879
9b378208
RS
10880/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
10881
10882void
10883x_set_mouse_pixel_position (f, pix_x, pix_y)
10884 struct frame *f;
10885 int pix_x, pix_y;
10886{
10887 BLOCK_INPUT;
10888
334208b7
RS
10889 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
10890 0, 0, 0, 0, pix_x, pix_y);
9b378208
RS
10891 UNBLOCK_INPUT;
10892}
d047c4eb
KH
10893\f
10894/* focus shifting, raising and lowering. */
9b378208 10895
dfcf069d 10896void
f676886a
JB
10897x_focus_on_frame (f)
10898 struct frame *f;
dc6f92b8 10899{
1fb20991 10900#if 0 /* This proves to be unpleasant. */
f676886a 10901 x_raise_frame (f);
1fb20991 10902#endif
6d4238f3
JB
10903#if 0
10904 /* I don't think that the ICCCM allows programs to do things like this
10905 without the interaction of the window manager. Whatever you end up
f676886a 10906 doing with this code, do it to x_unfocus_frame too. */
334208b7 10907 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dc6f92b8 10908 RevertToPointerRoot, CurrentTime);
c118dd06 10909#endif /* ! 0 */
dc6f92b8
JB
10910}
10911
dfcf069d 10912void
f676886a
JB
10913x_unfocus_frame (f)
10914 struct frame *f;
dc6f92b8 10915{
6d4238f3 10916#if 0
f676886a 10917 /* Look at the remarks in x_focus_on_frame. */
0f941935 10918 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
334208b7 10919 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
dc6f92b8 10920 RevertToPointerRoot, CurrentTime);
c118dd06 10921#endif /* ! 0 */
dc6f92b8
JB
10922}
10923
f676886a 10924/* Raise frame F. */
dc6f92b8 10925
dfcf069d 10926void
f676886a
JB
10927x_raise_frame (f)
10928 struct frame *f;
dc6f92b8 10929{
3a88c238 10930 if (f->async_visible)
dc6f92b8
JB
10931 {
10932 BLOCK_INPUT;
3afe33e7 10933#ifdef USE_X_TOOLKIT
7556890b 10934 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 10935#else /* not USE_X_TOOLKIT */
334208b7 10936 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 10937#endif /* not USE_X_TOOLKIT */
334208b7 10938 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
10939 UNBLOCK_INPUT;
10940 }
10941}
10942
f676886a 10943/* Lower frame F. */
dc6f92b8 10944
dfcf069d 10945void
f676886a
JB
10946x_lower_frame (f)
10947 struct frame *f;
dc6f92b8 10948{
3a88c238 10949 if (f->async_visible)
dc6f92b8
JB
10950 {
10951 BLOCK_INPUT;
3afe33e7 10952#ifdef USE_X_TOOLKIT
7556890b 10953 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 10954#else /* not USE_X_TOOLKIT */
334208b7 10955 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 10956#endif /* not USE_X_TOOLKIT */
334208b7 10957 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
10958 UNBLOCK_INPUT;
10959 }
10960}
10961
dbc4e1c1 10962static void
6b0442dc 10963XTframe_raise_lower (f, raise_flag)
dbc4e1c1 10964 FRAME_PTR f;
6b0442dc 10965 int raise_flag;
dbc4e1c1 10966{
6b0442dc 10967 if (raise_flag)
dbc4e1c1
JB
10968 x_raise_frame (f);
10969 else
10970 x_lower_frame (f);
10971}
d047c4eb
KH
10972\f
10973/* Change of visibility. */
dc6f92b8 10974
9382638d
KH
10975/* This tries to wait until the frame is really visible.
10976 However, if the window manager asks the user where to position
10977 the frame, this will return before the user finishes doing that.
10978 The frame will not actually be visible at that time,
10979 but it will become visible later when the window manager
10980 finishes with it. */
10981
dfcf069d 10982void
f676886a
JB
10983x_make_frame_visible (f)
10984 struct frame *f;
dc6f92b8 10985{
990ba854 10986 Lisp_Object type;
1aa6072f 10987 int original_top, original_left;
dc6f92b8 10988
dc6f92b8 10989 BLOCK_INPUT;
dc6f92b8 10990
990ba854
RS
10991 type = x_icon_type (f);
10992 if (!NILP (type))
10993 x_bitmap_icon (f, type);
bdcd49ba 10994
f676886a 10995 if (! FRAME_VISIBLE_P (f))
90e65f07 10996 {
1aa6072f
RS
10997 /* We test FRAME_GARBAGED_P here to make sure we don't
10998 call x_set_offset a second time
10999 if we get to x_make_frame_visible a second time
11000 before the window gets really visible. */
11001 if (! FRAME_ICONIFIED_P (f)
11002 && ! f->output_data.x->asked_for_visible)
11003 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
11004
11005 f->output_data.x->asked_for_visible = 1;
11006
90e65f07 11007 if (! EQ (Vx_no_window_manager, Qt))
f676886a 11008 x_wm_set_window_state (f, NormalState);
3afe33e7 11009#ifdef USE_X_TOOLKIT
d7a38a2e 11010 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 11011 XtMapWidget (f->output_data.x->widget);
3afe33e7 11012#else /* not USE_X_TOOLKIT */
7f9c7f94 11013 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11014#endif /* not USE_X_TOOLKIT */
0134a210
RS
11015#if 0 /* This seems to bring back scroll bars in the wrong places
11016 if the window configuration has changed. They seem
11017 to come back ok without this. */
ab648270 11018 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
334208b7 11019 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
0134a210 11020#endif
90e65f07 11021 }
dc6f92b8 11022
334208b7 11023 XFlush (FRAME_X_DISPLAY (f));
90e65f07 11024
0dacf791
RS
11025 /* Synchronize to ensure Emacs knows the frame is visible
11026 before we do anything else. We do this loop with input not blocked
11027 so that incoming events are handled. */
11028 {
11029 Lisp_Object frame;
c0a04927 11030 int count = input_signal_count;
28c01ffe
RS
11031 /* This must be before UNBLOCK_INPUT
11032 since events that arrive in response to the actions above
11033 will set it when they are handled. */
11034 int previously_visible = f->output_data.x->has_been_visible;
1aa6072f
RS
11035
11036 original_left = f->output_data.x->left_pos;
11037 original_top = f->output_data.x->top_pos;
c0a04927
RS
11038
11039 /* This must come after we set COUNT. */
11040 UNBLOCK_INPUT;
11041
2745e6c4 11042 /* We unblock here so that arriving X events are processed. */
1aa6072f 11043
dcb07ae9
RS
11044 /* Now move the window back to where it was "supposed to be".
11045 But don't do it if the gravity is negative.
11046 When the gravity is negative, this uses a position
28c01ffe
RS
11047 that is 3 pixels too low. Perhaps that's really the border width.
11048
11049 Don't do this if the window has never been visible before,
11050 because the window manager may choose the position
11051 and we don't want to override it. */
1aa6072f 11052
4d3f5d9a 11053 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
06c488fd 11054 && f->output_data.x->win_gravity == NorthWestGravity
28c01ffe 11055 && previously_visible)
1aa6072f 11056 {
2745e6c4
RS
11057 Drawable rootw;
11058 int x, y;
11059 unsigned int width, height, border, depth;
06a2c219 11060
1aa6072f 11061 BLOCK_INPUT;
9829ddba 11062
06a2c219
GM
11063 /* On some window managers (such as FVWM) moving an existing
11064 window, even to the same place, causes the window manager
11065 to introduce an offset. This can cause the window to move
11066 to an unexpected location. Check the geometry (a little
11067 slow here) and then verify that the window is in the right
11068 place. If the window is not in the right place, move it
11069 there, and take the potential window manager hit. */
2745e6c4
RS
11070 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11071 &rootw, &x, &y, &width, &height, &border, &depth);
11072
11073 if (original_left != x || original_top != y)
11074 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11075 original_left, original_top);
11076
1aa6072f
RS
11077 UNBLOCK_INPUT;
11078 }
9829ddba 11079
e0c1aef2 11080 XSETFRAME (frame, f);
c0a04927
RS
11081
11082 while (1)
2a6cf806 11083 {
334208b7 11084 x_sync (f);
c0a04927
RS
11085 /* Once we have handled input events,
11086 we should have received the MapNotify if one is coming.
11087 So if we have not got it yet, stop looping.
11088 Some window managers make their own decisions
11089 about visibility. */
11090 if (input_signal_count != count)
11091 break;
c12a7cbd 11092 /* Machines that do polling rather than SIGIO have been observed
23cf7c60
KH
11093 to go into a busy-wait here. So we'll fake an alarm signal
11094 to let the handler know that there's something to be read.
11095 We used to raise a real alarm, but it seems that the handler
11096 isn't always enabled here. This is probably a bug. */
8b2f8d4e 11097 if (input_polling_used ())
3b2fa4e6
RS
11098 {
11099 /* It could be confusing if a real alarm arrives while processing
11100 the fake one. Turn it off and let the handler reset it. */
11101 alarm (0);
5e8e68ce 11102 input_poll_signal (0);
3b2fa4e6 11103 }
c0a04927
RS
11104 /* Once we have handled input events,
11105 we should have received the MapNotify if one is coming.
11106 So if we have not got it yet, stop looping.
11107 Some window managers make their own decisions
11108 about visibility. */
11109 if (input_signal_count != count)
11110 break;
2a6cf806 11111 }
0dacf791
RS
11112 FRAME_SAMPLE_VISIBILITY (f);
11113 }
dc6f92b8
JB
11114}
11115
06a2c219 11116/* Change from mapped state to withdrawn state. */
dc6f92b8 11117
d047c4eb
KH
11118/* Make the frame visible (mapped and not iconified). */
11119
dfcf069d 11120void
f676886a
JB
11121x_make_frame_invisible (f)
11122 struct frame *f;
dc6f92b8 11123{
546e6d5b
RS
11124 Window window;
11125
11126#ifdef USE_X_TOOLKIT
11127 /* Use the frame's outermost window, not the one we normally draw on. */
7556890b 11128 window = XtWindow (f->output_data.x->widget);
546e6d5b
RS
11129#else /* not USE_X_TOOLKIT */
11130 window = FRAME_X_WINDOW (f);
11131#endif /* not USE_X_TOOLKIT */
dc6f92b8 11132
9319ae23 11133 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
11134 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11135 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 11136
5627c40e 11137#if 0/* This might add unreliability; I don't trust it -- rms. */
9319ae23 11138 if (! f->async_visible && ! f->async_iconified)
dc6f92b8 11139 return;
5627c40e 11140#endif
dc6f92b8
JB
11141
11142 BLOCK_INPUT;
c118dd06 11143
af31d76f
RS
11144 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
11145 that the current position of the window is user-specified, rather than
11146 program-specified, so that when the window is mapped again, it will be
11147 placed at the same location, without forcing the user to position it
11148 by hand again (they have already done that once for this window.) */
c32cdd9a 11149 x_wm_set_size_hint (f, (long) 0, 1);
af31d76f 11150
c118dd06
JB
11151#ifdef HAVE_X11R4
11152
334208b7
RS
11153 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
11154 DefaultScreen (FRAME_X_DISPLAY (f))))
c118dd06
JB
11155 {
11156 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11157 error ("Can't notify window manager of window withdrawal");
c118dd06 11158 }
c118dd06 11159#else /* ! defined (HAVE_X11R4) */
16bd92ea 11160
c118dd06 11161 /* Tell the window manager what we're going to do. */
dc6f92b8
JB
11162 if (! EQ (Vx_no_window_manager, Qt))
11163 {
16bd92ea 11164 XEvent unmap;
dc6f92b8 11165
16bd92ea 11166 unmap.xunmap.type = UnmapNotify;
546e6d5b 11167 unmap.xunmap.window = window;
334208b7 11168 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
16bd92ea 11169 unmap.xunmap.from_configure = False;
334208b7
RS
11170 if (! XSendEvent (FRAME_X_DISPLAY (f),
11171 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea 11172 False,
06a2c219 11173 SubstructureRedirectMaskSubstructureNotifyMask,
16bd92ea
JB
11174 &unmap))
11175 {
11176 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11177 error ("Can't notify window manager of withdrawal");
16bd92ea 11178 }
dc6f92b8
JB
11179 }
11180
16bd92ea 11181 /* Unmap the window ourselves. Cheeky! */
334208b7 11182 XUnmapWindow (FRAME_X_DISPLAY (f), window);
c118dd06 11183#endif /* ! defined (HAVE_X11R4) */
dc6f92b8 11184
5627c40e
RS
11185 /* We can't distinguish this from iconification
11186 just by the event that we get from the server.
11187 So we can't win using the usual strategy of letting
11188 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
11189 and synchronize with the server to make sure we agree. */
11190 f->visible = 0;
11191 FRAME_ICONIFIED_P (f) = 0;
11192 f->async_visible = 0;
11193 f->async_iconified = 0;
11194
334208b7 11195 x_sync (f);
5627c40e 11196
dc6f92b8
JB
11197 UNBLOCK_INPUT;
11198}
11199
06a2c219 11200/* Change window state from mapped to iconified. */
dc6f92b8 11201
dfcf069d 11202void
f676886a
JB
11203x_iconify_frame (f)
11204 struct frame *f;
dc6f92b8 11205{
3afe33e7 11206 int result;
990ba854 11207 Lisp_Object type;
dc6f92b8 11208
9319ae23 11209 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
11210 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11211 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 11212
3a88c238 11213 if (f->async_iconified)
dc6f92b8
JB
11214 return;
11215
3afe33e7 11216 BLOCK_INPUT;
546e6d5b 11217
9af3143a
RS
11218 FRAME_SAMPLE_VISIBILITY (f);
11219
990ba854
RS
11220 type = x_icon_type (f);
11221 if (!NILP (type))
11222 x_bitmap_icon (f, type);
bdcd49ba
RS
11223
11224#ifdef USE_X_TOOLKIT
11225
546e6d5b
RS
11226 if (! FRAME_VISIBLE_P (f))
11227 {
11228 if (! EQ (Vx_no_window_manager, Qt))
11229 x_wm_set_window_state (f, IconicState);
11230 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 11231 XtMapWidget (f->output_data.x->widget);
9cf30a30
RS
11232 /* The server won't give us any event to indicate
11233 that an invisible frame was changed to an icon,
11234 so we have to record it here. */
11235 f->iconified = 1;
1e6bc770 11236 f->visible = 1;
9cf30a30 11237 f->async_iconified = 1;
1e6bc770 11238 f->async_visible = 0;
546e6d5b
RS
11239 UNBLOCK_INPUT;
11240 return;
11241 }
11242
334208b7 11243 result = XIconifyWindow (FRAME_X_DISPLAY (f),
7556890b 11244 XtWindow (f->output_data.x->widget),
334208b7 11245 DefaultScreen (FRAME_X_DISPLAY (f)));
3afe33e7
RS
11246 UNBLOCK_INPUT;
11247
11248 if (!result)
546e6d5b 11249 error ("Can't notify window manager of iconification");
3afe33e7
RS
11250
11251 f->async_iconified = 1;
1e6bc770
RS
11252 f->async_visible = 0;
11253
8c002a25
KH
11254
11255 BLOCK_INPUT;
334208b7 11256 XFlush (FRAME_X_DISPLAY (f));
8c002a25 11257 UNBLOCK_INPUT;
3afe33e7
RS
11258#else /* not USE_X_TOOLKIT */
11259
fd13dbb2
RS
11260 /* Make sure the X server knows where the window should be positioned,
11261 in case the user deiconifies with the window manager. */
11262 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
7556890b 11263 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
fd13dbb2 11264
16bd92ea
JB
11265 /* Since we don't know which revision of X we're running, we'll use both
11266 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
11267
11268 /* X11R4: send a ClientMessage to the window manager using the
11269 WM_CHANGE_STATE type. */
11270 {
11271 XEvent message;
58769bee 11272
c118dd06 11273 message.xclient.window = FRAME_X_WINDOW (f);
16bd92ea 11274 message.xclient.type = ClientMessage;
334208b7 11275 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
16bd92ea
JB
11276 message.xclient.format = 32;
11277 message.xclient.data.l[0] = IconicState;
11278
334208b7
RS
11279 if (! XSendEvent (FRAME_X_DISPLAY (f),
11280 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea
JB
11281 False,
11282 SubstructureRedirectMask | SubstructureNotifyMask,
11283 &message))
dc6f92b8
JB
11284 {
11285 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11286 error ("Can't notify window manager of iconification");
dc6f92b8 11287 }
16bd92ea 11288 }
dc6f92b8 11289
58769bee 11290 /* X11R3: set the initial_state field of the window manager hints to
16bd92ea
JB
11291 IconicState. */
11292 x_wm_set_window_state (f, IconicState);
dc6f92b8 11293
a9c00105
RS
11294 if (!FRAME_VISIBLE_P (f))
11295 {
11296 /* If the frame was withdrawn, before, we must map it. */
7f9c7f94 11297 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
a9c00105
RS
11298 }
11299
3a88c238 11300 f->async_iconified = 1;
1e6bc770 11301 f->async_visible = 0;
dc6f92b8 11302
334208b7 11303 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 11304 UNBLOCK_INPUT;
8c002a25 11305#endif /* not USE_X_TOOLKIT */
dc6f92b8 11306}
d047c4eb 11307\f
c0ff3fab 11308/* Destroy the X window of frame F. */
dc6f92b8 11309
dfcf069d 11310void
c0ff3fab 11311x_destroy_window (f)
f676886a 11312 struct frame *f;
dc6f92b8 11313{
7f9c7f94
RS
11314 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11315
dc6f92b8 11316 BLOCK_INPUT;
c0ff3fab 11317
6186a4a0
RS
11318 /* If a display connection is dead, don't try sending more
11319 commands to the X server. */
11320 if (dpyinfo->display != 0)
11321 {
11322 if (f->output_data.x->icon_desc != 0)
11323 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
31f41daf
RS
11324#ifdef HAVE_X_I18N
11325 if (FRAME_XIM (f))
11326 {
11327 XDestroyIC (FRAME_XIC (f));
408be661
RS
11328#if ! defined (SOLARIS2) || defined (HAVE_X11R6)
11329 /* This line causes crashes on Solaris with Openwin,
11330 due to an apparent bug in XCloseIM.
11331 X11R6 seems not to have the bug. */
31f41daf 11332 XCloseIM (FRAME_XIM (f));
a9978dd8 11333#endif
31f41daf
RS
11334 }
11335#endif
6186a4a0 11336 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->window_desc);
3afe33e7 11337#ifdef USE_X_TOOLKIT
06a2c219
GM
11338 if (f->output_data.x->widget)
11339 XtDestroyWidget (f->output_data.x->widget);
6186a4a0 11340 free_frame_menubar (f);
3afe33e7
RS
11341#endif /* USE_X_TOOLKIT */
11342
6186a4a0
RS
11343 free_frame_faces (f);
11344 XFlush (FRAME_X_DISPLAY (f));
11345 }
dc6f92b8 11346
df89d8a4 11347 if (f->output_data.x->saved_menu_event)
06a2c219 11348 xfree (f->output_data.x->saved_menu_event);
0c49ce2f 11349
7556890b
RS
11350 xfree (f->output_data.x);
11351 f->output_data.x = 0;
0f941935
KH
11352 if (f == dpyinfo->x_focus_frame)
11353 dpyinfo->x_focus_frame = 0;
11354 if (f == dpyinfo->x_focus_event_frame)
11355 dpyinfo->x_focus_event_frame = 0;
11356 if (f == dpyinfo->x_highlight_frame)
11357 dpyinfo->x_highlight_frame = 0;
c0ff3fab 11358
7f9c7f94
RS
11359 dpyinfo->reference_count--;
11360
11361 if (f == dpyinfo->mouse_face_mouse_frame)
dc05a16b 11362 {
7f9c7f94
RS
11363 dpyinfo->mouse_face_beg_row
11364 = dpyinfo->mouse_face_beg_col = -1;
11365 dpyinfo->mouse_face_end_row
11366 = dpyinfo->mouse_face_end_col = -1;
11367 dpyinfo->mouse_face_window = Qnil;
21323706
RS
11368 dpyinfo->mouse_face_deferred_gc = 0;
11369 dpyinfo->mouse_face_mouse_frame = 0;
dc05a16b 11370 }
0134a210 11371
c0ff3fab 11372 UNBLOCK_INPUT;
dc6f92b8
JB
11373}
11374\f
f451eb13
JB
11375/* Setting window manager hints. */
11376
af31d76f
RS
11377/* Set the normal size hints for the window manager, for frame F.
11378 FLAGS is the flags word to use--or 0 meaning preserve the flags
11379 that the window now has.
11380 If USER_POSITION is nonzero, we set the USPosition
11381 flag (this is useful when FLAGS is 0). */
6dba1858 11382
dfcf069d 11383void
af31d76f 11384x_wm_set_size_hint (f, flags, user_position)
f676886a 11385 struct frame *f;
af31d76f
RS
11386 long flags;
11387 int user_position;
dc6f92b8
JB
11388{
11389 XSizeHints size_hints;
3afe33e7
RS
11390
11391#ifdef USE_X_TOOLKIT
7e4f2521
FP
11392 Arg al[2];
11393 int ac = 0;
11394 Dimension widget_width, widget_height;
7556890b 11395 Window window = XtWindow (f->output_data.x->widget);
3afe33e7 11396#else /* not USE_X_TOOLKIT */
c118dd06 11397 Window window = FRAME_X_WINDOW (f);
3afe33e7 11398#endif /* not USE_X_TOOLKIT */
dc6f92b8 11399
b72a58fd
RS
11400 /* Setting PMaxSize caused various problems. */
11401 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
dc6f92b8 11402
7556890b
RS
11403 size_hints.x = f->output_data.x->left_pos;
11404 size_hints.y = f->output_data.x->top_pos;
7553a6b7 11405
7e4f2521
FP
11406#ifdef USE_X_TOOLKIT
11407 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
11408 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
7556890b 11409 XtGetValues (f->output_data.x->widget, al, ac);
7e4f2521
FP
11410 size_hints.height = widget_height;
11411 size_hints.width = widget_width;
11412#else /* not USE_X_TOOLKIT */
f676886a
JB
11413 size_hints.height = PIXEL_HEIGHT (f);
11414 size_hints.width = PIXEL_WIDTH (f);
7e4f2521 11415#endif /* not USE_X_TOOLKIT */
7553a6b7 11416
7556890b
RS
11417 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
11418 size_hints.height_inc = f->output_data.x->line_height;
334208b7
RS
11419 size_hints.max_width
11420 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
11421 size_hints.max_height
11422 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
0134a210 11423
d067ea8b
KH
11424 /* Calculate the base and minimum sizes.
11425
11426 (When we use the X toolkit, we don't do it here.
11427 Instead we copy the values that the widgets are using, below.) */
11428#ifndef USE_X_TOOLKIT
b1c884c3 11429 {
b0342f17 11430 int base_width, base_height;
0134a210 11431 int min_rows = 0, min_cols = 0;
b0342f17 11432
f451eb13
JB
11433 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
11434 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
b0342f17 11435
0134a210 11436 check_frame_size (f, &min_rows, &min_cols);
b0342f17 11437
0134a210
RS
11438 /* The window manager uses the base width hints to calculate the
11439 current number of rows and columns in the frame while
11440 resizing; min_width and min_height aren't useful for this
11441 purpose, since they might not give the dimensions for a
11442 zero-row, zero-column frame.
58769bee 11443
0134a210
RS
11444 We use the base_width and base_height members if we have
11445 them; otherwise, we set the min_width and min_height members
11446 to the size for a zero x zero frame. */
b0342f17
JB
11447
11448#ifdef HAVE_X11R4
0134a210
RS
11449 size_hints.flags |= PBaseSize;
11450 size_hints.base_width = base_width;
11451 size_hints.base_height = base_height;
11452 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
11453 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
b0342f17 11454#else
0134a210
RS
11455 size_hints.min_width = base_width;
11456 size_hints.min_height = base_height;
b0342f17 11457#endif
b1c884c3 11458 }
dc6f92b8 11459
d067ea8b 11460 /* If we don't need the old flags, we don't need the old hint at all. */
af31d76f 11461 if (flags)
dc6f92b8 11462 {
d067ea8b
KH
11463 size_hints.flags |= flags;
11464 goto no_read;
11465 }
11466#endif /* not USE_X_TOOLKIT */
11467
11468 {
11469 XSizeHints hints; /* Sometimes I hate X Windows... */
11470 long supplied_return;
11471 int value;
af31d76f
RS
11472
11473#ifdef HAVE_X11R4
d067ea8b
KH
11474 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
11475 &supplied_return);
af31d76f 11476#else
d067ea8b 11477 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
af31d76f 11478#endif
58769bee 11479
d067ea8b
KH
11480#ifdef USE_X_TOOLKIT
11481 size_hints.base_height = hints.base_height;
11482 size_hints.base_width = hints.base_width;
11483 size_hints.min_height = hints.min_height;
11484 size_hints.min_width = hints.min_width;
11485#endif
11486
11487 if (flags)
11488 size_hints.flags |= flags;
11489 else
11490 {
11491 if (value == 0)
11492 hints.flags = 0;
11493 if (hints.flags & PSize)
11494 size_hints.flags |= PSize;
11495 if (hints.flags & PPosition)
11496 size_hints.flags |= PPosition;
11497 if (hints.flags & USPosition)
11498 size_hints.flags |= USPosition;
11499 if (hints.flags & USSize)
11500 size_hints.flags |= USSize;
11501 }
11502 }
11503
06a2c219 11504#ifndef USE_X_TOOLKIT
d067ea8b 11505 no_read:
06a2c219 11506#endif
0134a210 11507
af31d76f 11508#ifdef PWinGravity
7556890b 11509 size_hints.win_gravity = f->output_data.x->win_gravity;
af31d76f 11510 size_hints.flags |= PWinGravity;
dc05a16b 11511
af31d76f 11512 if (user_position)
6dba1858 11513 {
af31d76f
RS
11514 size_hints.flags &= ~ PPosition;
11515 size_hints.flags |= USPosition;
6dba1858 11516 }
2554751d 11517#endif /* PWinGravity */
6dba1858 11518
b0342f17 11519#ifdef HAVE_X11R4
334208b7 11520 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 11521#else
334208b7 11522 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 11523#endif
dc6f92b8
JB
11524}
11525
11526/* Used for IconicState or NormalState */
06a2c219 11527
dfcf069d 11528void
f676886a
JB
11529x_wm_set_window_state (f, state)
11530 struct frame *f;
dc6f92b8
JB
11531 int state;
11532{
3afe33e7 11533#ifdef USE_X_TOOLKIT
546e6d5b
RS
11534 Arg al[1];
11535
11536 XtSetArg (al[0], XtNinitialState, state);
7556890b 11537 XtSetValues (f->output_data.x->widget, al, 1);
3afe33e7 11538#else /* not USE_X_TOOLKIT */
c118dd06 11539 Window window = FRAME_X_WINDOW (f);
dc6f92b8 11540
7556890b
RS
11541 f->output_data.x->wm_hints.flags |= StateHint;
11542 f->output_data.x->wm_hints.initial_state = state;
b1c884c3 11543
7556890b 11544 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
546e6d5b 11545#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11546}
11547
dfcf069d 11548void
7f2ae036 11549x_wm_set_icon_pixmap (f, pixmap_id)
f676886a 11550 struct frame *f;
7f2ae036 11551 int pixmap_id;
dc6f92b8 11552{
d2bd6bc4
RS
11553 Pixmap icon_pixmap;
11554
06a2c219 11555#ifndef USE_X_TOOLKIT
c118dd06 11556 Window window = FRAME_X_WINDOW (f);
75231bad 11557#endif
dc6f92b8 11558
7f2ae036 11559 if (pixmap_id > 0)
dbc4e1c1 11560 {
d2bd6bc4 11561 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
7556890b 11562 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
dbc4e1c1
JB
11563 }
11564 else
68568555
RS
11565 {
11566 /* It seems there is no way to turn off use of an icon pixmap.
11567 The following line does it, only if no icon has yet been created,
11568 for some window managers. But with mwm it crashes.
11569 Some people say it should clear the IconPixmapHint bit in this case,
11570 but that doesn't work, and the X consortium said it isn't the
11571 right thing at all. Since there is no way to win,
11572 best to explicitly give up. */
11573#if 0
11574 f->output_data.x->wm_hints.icon_pixmap = None;
11575#else
11576 return;
11577#endif
11578 }
b1c884c3 11579
d2bd6bc4
RS
11580#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
11581
11582 {
11583 Arg al[1];
11584 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
11585 XtSetValues (f->output_data.x->widget, al, 1);
11586 }
11587
11588#else /* not USE_X_TOOLKIT */
11589
7556890b
RS
11590 f->output_data.x->wm_hints.flags |= IconPixmapHint;
11591 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
d2bd6bc4
RS
11592
11593#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11594}
11595
dfcf069d 11596void
f676886a
JB
11597x_wm_set_icon_position (f, icon_x, icon_y)
11598 struct frame *f;
dc6f92b8
JB
11599 int icon_x, icon_y;
11600{
75231bad 11601#ifdef USE_X_TOOLKIT
7556890b 11602 Window window = XtWindow (f->output_data.x->widget);
75231bad 11603#else
c118dd06 11604 Window window = FRAME_X_WINDOW (f);
75231bad 11605#endif
dc6f92b8 11606
7556890b
RS
11607 f->output_data.x->wm_hints.flags |= IconPositionHint;
11608 f->output_data.x->wm_hints.icon_x = icon_x;
11609 f->output_data.x->wm_hints.icon_y = icon_y;
b1c884c3 11610
7556890b 11611 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
dc6f92b8
JB
11612}
11613
11614\f
06a2c219
GM
11615/***********************************************************************
11616 Fonts
11617 ***********************************************************************/
dc43ef94
KH
11618
11619/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
06a2c219 11620
dc43ef94
KH
11621struct font_info *
11622x_get_font_info (f, font_idx)
11623 FRAME_PTR f;
11624 int font_idx;
11625{
11626 return (FRAME_X_FONT_TABLE (f) + font_idx);
11627}
11628
11629
11630/* Return a list of names of available fonts matching PATTERN on frame
11631 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
11632 to be listed. Frame F NULL means we have not yet created any
11633 frame on X, and consult the first display in x_display_list.
11634 MAXNAMES sets a limit on how many fonts to match. */
11635
11636Lisp_Object
11637x_list_fonts (f, pattern, size, maxnames)
11638 FRAME_PTR f;
11639 Lisp_Object pattern;
11640 int size;
11641 int maxnames;
11642{
06a2c219
GM
11643 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
11644 Lisp_Object tem, second_best;
dc43ef94 11645 Display *dpy = f != NULL ? FRAME_X_DISPLAY (f) : x_display_list->display;
09c6077f 11646 int try_XLoadQueryFont = 0;
53ca4657 11647 int count;
dc43ef94 11648
6b0efe73 11649 patterns = Fassoc (pattern, Valternate_fontname_alist);
2da424f1
KH
11650 if (NILP (patterns))
11651 patterns = Fcons (pattern, Qnil);
81ba44e5 11652
09c6077f
KH
11653 if (maxnames == 1 && !size)
11654 /* We can return any single font matching PATTERN. */
11655 try_XLoadQueryFont = 1;
9a32686f 11656
2da424f1 11657 for (; CONSP (patterns); patterns = XCONS (patterns)->cdr)
dc43ef94 11658 {
dc43ef94
KH
11659 int num_fonts;
11660 char **names;
11661
2da424f1 11662 pattern = XCONS (patterns)->car;
536f4067
RS
11663 /* See if we cached the result for this particular query.
11664 The cache is an alist of the form:
11665 (((PATTERN . MAXNAMES) (FONTNAME . WIDTH) ...) ...)
11666 */
b5210ea7
KH
11667 if (f && (tem = XCONS (FRAME_X_DISPLAY_INFO (f)->name_list_element)->cdr,
11668 key = Fcons (pattern, make_number (maxnames)),
11669 !NILP (list = Fassoc (key, tem))))
11670 {
11671 list = Fcdr_safe (list);
11672 /* We have a cashed list. Don't have to get the list again. */
11673 goto label_cached;
11674 }
11675
11676 /* At first, put PATTERN in the cache. */
09c6077f 11677
dc43ef94 11678 BLOCK_INPUT;
17d85edc
KH
11679 count = x_catch_errors (dpy);
11680
09c6077f
KH
11681 if (try_XLoadQueryFont)
11682 {
11683 XFontStruct *font;
11684 unsigned long value;
11685
11686 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
17d85edc
KH
11687 if (x_had_errors_p (dpy))
11688 {
11689 /* This error is perhaps due to insufficient memory on X
11690 server. Let's just ignore it. */
11691 font = NULL;
11692 x_clear_errors (dpy);
11693 }
11694
09c6077f
KH
11695 if (font
11696 && XGetFontProperty (font, XA_FONT, &value))
11697 {
11698 char *name = (char *) XGetAtomName (dpy, (Atom) value);
11699 int len = strlen (name);
01c752b5 11700 char *tmp;
09c6077f 11701
6f6512e8
KH
11702 /* If DXPC (a Differential X Protocol Compressor)
11703 Ver.3.7 is running, XGetAtomName will return null
11704 string. We must avoid such a name. */
11705 if (len == 0)
11706 try_XLoadQueryFont = 0;
11707 else
11708 {
11709 num_fonts = 1;
11710 names = (char **) alloca (sizeof (char *));
11711 /* Some systems only allow alloca assigned to a
11712 simple var. */
11713 tmp = (char *) alloca (len + 1); names[0] = tmp;
11714 bcopy (name, names[0], len + 1);
11715 XFree (name);
11716 }
09c6077f
KH
11717 }
11718 else
11719 try_XLoadQueryFont = 0;
a083fd23
RS
11720
11721 if (font)
11722 XFreeFont (dpy, font);
09c6077f
KH
11723 }
11724
11725 if (!try_XLoadQueryFont)
17d85edc
KH
11726 {
11727 /* We try at least 10 fonts because XListFonts will return
11728 auto-scaled fonts at the head. */
11729 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
11730 &num_fonts);
11731 if (x_had_errors_p (dpy))
11732 {
11733 /* This error is perhaps due to insufficient memory on X
11734 server. Let's just ignore it. */
11735 names = NULL;
11736 x_clear_errors (dpy);
11737 }
11738 }
11739
11740 x_uncatch_errors (dpy, count);
dc43ef94
KH
11741 UNBLOCK_INPUT;
11742
11743 if (names)
11744 {
11745 int i;
dc43ef94
KH
11746
11747 /* Make a list of all the fonts we got back.
11748 Store that in the font cache for the display. */
11749 for (i = 0; i < num_fonts; i++)
11750 {
06a2c219 11751 int width = 0;
dc43ef94 11752 char *p = names[i];
06a2c219
GM
11753 int average_width = -1, dashes = 0;
11754
dc43ef94 11755 /* Count the number of dashes in NAMES[I]. If there are
b5210ea7
KH
11756 14 dashes, and the field value following 12th dash
11757 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
11758 is usually too ugly to be used for editing. Let's
11759 ignore it. */
dc43ef94
KH
11760 while (*p)
11761 if (*p++ == '-')
11762 {
11763 dashes++;
11764 if (dashes == 7) /* PIXEL_SIZE field */
11765 width = atoi (p);
11766 else if (dashes == 12) /* AVERAGE_WIDTH field */
11767 average_width = atoi (p);
11768 }
11769 if (dashes < 14 || average_width != 0)
11770 {
11771 tem = build_string (names[i]);
11772 if (NILP (Fassoc (tem, list)))
11773 {
11774 if (STRINGP (Vx_pixel_size_width_font_regexp)
f39db7ea
RS
11775 && ((fast_c_string_match_ignore_case
11776 (Vx_pixel_size_width_font_regexp, names[i]))
dc43ef94
KH
11777 >= 0))
11778 /* We can set the value of PIXEL_SIZE to the
b5210ea7 11779 width of this font. */
dc43ef94
KH
11780 list = Fcons (Fcons (tem, make_number (width)), list);
11781 else
11782 /* For the moment, width is not known. */
11783 list = Fcons (Fcons (tem, Qnil), list);
11784 }
11785 }
11786 }
09c6077f
KH
11787 if (!try_XLoadQueryFont)
11788 XFreeFontNames (names);
dc43ef94
KH
11789 }
11790
b5210ea7 11791 /* Now store the result in the cache. */
dc43ef94
KH
11792 if (f != NULL)
11793 XCONS (FRAME_X_DISPLAY_INFO (f)->name_list_element)->cdr
11794 = Fcons (Fcons (key, list),
11795 XCONS (FRAME_X_DISPLAY_INFO (f)->name_list_element)->cdr);
dc43ef94 11796
b5210ea7
KH
11797 label_cached:
11798 if (NILP (list)) continue; /* Try the remaining alternatives. */
dc43ef94 11799
b5210ea7
KH
11800 newlist = second_best = Qnil;
11801 /* Make a list of the fonts that have the right width. */
11802 for (; CONSP (list); list = XCONS (list)->cdr)
11803 {
536f4067
RS
11804 int found_size;
11805
b5210ea7 11806 tem = XCONS (list)->car;
dc43ef94 11807
b5210ea7
KH
11808 if (!CONSP (tem) || NILP (XCONS (tem)->car))
11809 continue;
11810 if (!size)
11811 {
11812 newlist = Fcons (XCONS (tem)->car, newlist);
11813 continue;
11814 }
dc43ef94 11815
dc43ef94
KH
11816 if (!INTEGERP (XCONS (tem)->cdr))
11817 {
b5210ea7
KH
11818 /* Since we have not yet known the size of this font, we
11819 must try slow function call XLoadQueryFont. */
dc43ef94
KH
11820 XFontStruct *thisinfo;
11821
11822 BLOCK_INPUT;
17d85edc 11823 count = x_catch_errors (dpy);
dc43ef94
KH
11824 thisinfo = XLoadQueryFont (dpy,
11825 XSTRING (XCONS (tem)->car)->data);
17d85edc
KH
11826 if (x_had_errors_p (dpy))
11827 {
11828 /* This error is perhaps due to insufficient memory on X
11829 server. Let's just ignore it. */
11830 thisinfo = NULL;
11831 x_clear_errors (dpy);
11832 }
11833 x_uncatch_errors (dpy, count);
dc43ef94
KH
11834 UNBLOCK_INPUT;
11835
11836 if (thisinfo)
11837 {
536f4067
RS
11838 XCONS (tem)->cdr
11839 = (thisinfo->min_bounds.width == 0
11840 ? make_number (0)
11841 : make_number (thisinfo->max_bounds.width));
dc43ef94
KH
11842 XFreeFont (dpy, thisinfo);
11843 }
11844 else
b5210ea7 11845 /* For unknown reason, the previous call of XListFont had
06a2c219 11846 returned a font which can't be opened. Record the size
b5210ea7 11847 as 0 not to try to open it again. */
dc43ef94
KH
11848 XCONS (tem)->cdr = make_number (0);
11849 }
536f4067
RS
11850
11851 found_size = XINT (XCONS (tem)->cdr);
11852 if (found_size == size)
b5210ea7 11853 newlist = Fcons (XCONS (tem)->car, newlist);
536f4067 11854 else if (found_size > 0)
b5210ea7 11855 {
536f4067 11856 if (NILP (second_best))
b5210ea7 11857 second_best = tem;
536f4067
RS
11858 else if (found_size < size)
11859 {
11860 if (XINT (XCONS (second_best)->cdr) > size
11861 || XINT (XCONS (second_best)->cdr) < found_size)
11862 second_best = tem;
11863 }
11864 else
11865 {
11866 if (XINT (XCONS (second_best)->cdr) > size
11867 && XINT (XCONS (second_best)->cdr) > found_size)
11868 second_best = tem;
11869 }
b5210ea7
KH
11870 }
11871 }
11872 if (!NILP (newlist))
11873 break;
11874 else if (!NILP (second_best))
11875 {
11876 newlist = Fcons (XCONS (second_best)->car, Qnil);
11877 break;
dc43ef94 11878 }
dc43ef94
KH
11879 }
11880
11881 return newlist;
11882}
11883
06a2c219
GM
11884
11885#if GLYPH_DEBUG
11886
11887/* Check that FONT is valid on frame F. It is if it can be found in F's
11888 font table. */
11889
11890static void
11891x_check_font (f, font)
11892 struct frame *f;
11893 XFontStruct *font;
11894{
11895 int i;
11896 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11897
11898 xassert (font != NULL);
11899
11900 for (i = 0; i < dpyinfo->n_fonts; i++)
11901 if (dpyinfo->font_table[i].name
11902 && font == dpyinfo->font_table[i].font)
11903 break;
11904
11905 xassert (i < dpyinfo->n_fonts);
11906}
11907
11908#endif /* GLYPH_DEBUG != 0 */
11909
11910/* Set *W to the minimum width, *H to the minimum font height of FONT.
11911 Note: There are (broken) X fonts out there with invalid XFontStruct
11912 min_bounds contents. For example, handa@etl.go.jp reports that
11913 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
11914 have font->min_bounds.width == 0. */
11915
11916static INLINE void
11917x_font_min_bounds (font, w, h)
11918 XFontStruct *font;
11919 int *w, *h;
11920{
11921 *h = FONT_HEIGHT (font);
11922 *w = font->min_bounds.width;
11923
11924 /* Try to handle the case where FONT->min_bounds has invalid
11925 contents. Since the only font known to have invalid min_bounds
11926 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
11927 if (*w <= 0)
11928 *w = font->max_bounds.width;
11929}
11930
11931
11932/* Compute the smallest character width and smallest font height over
11933 all fonts available on frame F. Set the members smallest_char_width
11934 and smallest_font_height in F's x_display_info structure to
11935 the values computed. Value is non-zero if smallest_font_height or
11936 smallest_char_width become smaller than they were before. */
11937
11938static int
11939x_compute_min_glyph_bounds (f)
11940 struct frame *f;
11941{
11942 int i;
11943 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11944 XFontStruct *font;
11945 int old_width = dpyinfo->smallest_char_width;
11946 int old_height = dpyinfo->smallest_font_height;
11947
11948 dpyinfo->smallest_font_height = 100000;
11949 dpyinfo->smallest_char_width = 100000;
11950
11951 for (i = 0; i < dpyinfo->n_fonts; ++i)
11952 if (dpyinfo->font_table[i].name)
11953 {
11954 struct font_info *fontp = dpyinfo->font_table + i;
11955 int w, h;
11956
11957 font = (XFontStruct *) fontp->font;
11958 xassert (font != (XFontStruct *) ~0);
11959 x_font_min_bounds (font, &w, &h);
11960
11961 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
11962 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
11963 }
11964
11965 xassert (dpyinfo->smallest_char_width > 0
11966 && dpyinfo->smallest_font_height > 0);
11967
11968 return (dpyinfo->n_fonts == 1
11969 || dpyinfo->smallest_char_width < old_width
11970 || dpyinfo->smallest_font_height < old_height);
11971}
11972
11973
dc43ef94
KH
11974/* Load font named FONTNAME of the size SIZE for frame F, and return a
11975 pointer to the structure font_info while allocating it dynamically.
11976 If SIZE is 0, load any size of font.
11977 If loading is failed, return NULL. */
11978
11979struct font_info *
11980x_load_font (f, fontname, size)
11981 struct frame *f;
11982 register char *fontname;
11983 int size;
11984{
11985 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11986 Lisp_Object font_names;
d645aaa4 11987 int count;
dc43ef94
KH
11988
11989 /* Get a list of all the fonts that match this name. Once we
11990 have a list of matching fonts, we compare them against the fonts
11991 we already have by comparing names. */
09c6077f 11992 font_names = x_list_fonts (f, build_string (fontname), size, 1);
dc43ef94
KH
11993
11994 if (!NILP (font_names))
11995 {
11996 Lisp_Object tail;
11997 int i;
11998
11999 for (i = 0; i < dpyinfo->n_fonts; i++)
12000 for (tail = font_names; CONSP (tail); tail = XCONS (tail)->cdr)
06a2c219
GM
12001 if (dpyinfo->font_table[i].name
12002 && (!strcmp (dpyinfo->font_table[i].name,
12003 XSTRING (XCONS (tail)->car)->data)
12004 || !strcmp (dpyinfo->font_table[i].full_name,
12005 XSTRING (XCONS (tail)->car)->data)))
dc43ef94
KH
12006 return (dpyinfo->font_table + i);
12007 }
12008
12009 /* Load the font and add it to the table. */
12010 {
12011 char *full_name;
12012 XFontStruct *font;
12013 struct font_info *fontp;
12014 unsigned long value;
06a2c219 12015 int i;
dc43ef94 12016
2da424f1
KH
12017 /* If we have found fonts by x_list_font, load one of them. If
12018 not, we still try to load a font by the name given as FONTNAME
12019 because XListFonts (called in x_list_font) of some X server has
12020 a bug of not finding a font even if the font surely exists and
12021 is loadable by XLoadQueryFont. */
e1d6d5b9 12022 if (size > 0 && !NILP (font_names))
f613a4c8 12023 fontname = (char *) XSTRING (XCONS (font_names)->car)->data;
dc43ef94
KH
12024
12025 BLOCK_INPUT;
d645aaa4 12026 count = x_catch_errors (FRAME_X_DISPLAY (f));
dc43ef94 12027 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
d645aaa4
KH
12028 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12029 {
12030 /* This error is perhaps due to insufficient memory on X
12031 server. Let's just ignore it. */
12032 font = NULL;
12033 x_clear_errors (FRAME_X_DISPLAY (f));
12034 }
12035 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
dc43ef94 12036 UNBLOCK_INPUT;
b5210ea7 12037 if (!font)
dc43ef94
KH
12038 return NULL;
12039
06a2c219
GM
12040 /* Find a free slot in the font table. */
12041 for (i = 0; i < dpyinfo->n_fonts; ++i)
12042 if (dpyinfo->font_table[i].name == NULL)
12043 break;
12044
12045 /* If no free slot found, maybe enlarge the font table. */
12046 if (i == dpyinfo->n_fonts
12047 && dpyinfo->n_fonts == dpyinfo->font_table_size)
dc43ef94 12048 {
06a2c219
GM
12049 int sz;
12050 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
12051 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
dc43ef94 12052 dpyinfo->font_table
06a2c219 12053 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
dc43ef94
KH
12054 }
12055
06a2c219
GM
12056 fontp = dpyinfo->font_table + i;
12057 if (i == dpyinfo->n_fonts)
12058 ++dpyinfo->n_fonts;
dc43ef94
KH
12059
12060 /* Now fill in the slots of *FONTP. */
12061 BLOCK_INPUT;
12062 fontp->font = font;
06a2c219 12063 fontp->font_idx = i;
dc43ef94
KH
12064 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
12065 bcopy (fontname, fontp->name, strlen (fontname) + 1);
12066
12067 /* Try to get the full name of FONT. Put it in FULL_NAME. */
12068 full_name = 0;
12069 if (XGetFontProperty (font, XA_FONT, &value))
12070 {
12071 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
12072 char *p = name;
12073 int dashes = 0;
12074
12075 /* Count the number of dashes in the "full name".
12076 If it is too few, this isn't really the font's full name,
12077 so don't use it.
12078 In X11R4, the fonts did not come with their canonical names
12079 stored in them. */
12080 while (*p)
12081 {
12082 if (*p == '-')
12083 dashes++;
12084 p++;
12085 }
12086
12087 if (dashes >= 13)
12088 {
12089 full_name = (char *) xmalloc (p - name + 1);
12090 bcopy (name, full_name, p - name + 1);
12091 }
12092
12093 XFree (name);
12094 }
12095
12096 if (full_name != 0)
12097 fontp->full_name = full_name;
12098 else
12099 fontp->full_name = fontp->name;
12100
12101 fontp->size = font->max_bounds.width;
d5749adb
KH
12102 fontp->height = FONT_HEIGHT (font);
12103 {
12104 /* For some font, ascent and descent in max_bounds field is
12105 larger than the above value. */
12106 int max_height = font->max_bounds.ascent + font->max_bounds.descent;
12107 if (max_height > fontp->height)
74848a96 12108 fontp->height = max_height;
d5749adb 12109 }
dc43ef94 12110
2da424f1
KH
12111 if (NILP (font_names))
12112 {
12113 /* We come here because of a bug of XListFonts mentioned at
12114 the head of this block. Let's store this information in
12115 the cache for x_list_fonts. */
12116 Lisp_Object lispy_name = build_string (fontname);
12117 Lisp_Object lispy_full_name = build_string (fontp->full_name);
12118
12119 XCONS (dpyinfo->name_list_element)->cdr
12120 = Fcons (Fcons (Fcons (lispy_name, make_number (256)),
12121 Fcons (Fcons (lispy_full_name,
12122 make_number (fontp->size)),
12123 Qnil)),
12124 XCONS (dpyinfo->name_list_element)->cdr);
12125 if (full_name)
12126 XCONS (dpyinfo->name_list_element)->cdr
12127 = Fcons (Fcons (Fcons (lispy_full_name, make_number (256)),
12128 Fcons (Fcons (lispy_full_name,
12129 make_number (fontp->size)),
12130 Qnil)),
12131 XCONS (dpyinfo->name_list_element)->cdr);
12132 }
12133
dc43ef94
KH
12134 /* The slot `encoding' specifies how to map a character
12135 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
ce667c3e 12136 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF, 0:0x2020..0x7F7F,
8ff102bd
RS
12137 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF,
12138 0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF, or
12139 2:0xA020..0xFF7F). For the moment, we don't know which charset
06a2c219 12140 uses this font. So, we set information in fontp->encoding[1]
8ff102bd
RS
12141 which is never used by any charset. If mapping can't be
12142 decided, set FONT_ENCODING_NOT_DECIDED. */
dc43ef94
KH
12143 fontp->encoding[1]
12144 = (font->max_byte1 == 0
12145 /* 1-byte font */
12146 ? (font->min_char_or_byte2 < 0x80
12147 ? (font->max_char_or_byte2 < 0x80
12148 ? 0 /* 0x20..0x7F */
8ff102bd 12149 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
dc43ef94
KH
12150 : 1) /* 0xA0..0xFF */
12151 /* 2-byte font */
12152 : (font->min_byte1 < 0x80
12153 ? (font->max_byte1 < 0x80
12154 ? (font->min_char_or_byte2 < 0x80
12155 ? (font->max_char_or_byte2 < 0x80
12156 ? 0 /* 0x2020..0x7F7F */
8ff102bd 12157 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
dc43ef94 12158 : 3) /* 0x20A0..0x7FFF */
8ff102bd 12159 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
dc43ef94
KH
12160 : (font->min_char_or_byte2 < 0x80
12161 ? (font->max_char_or_byte2 < 0x80
12162 ? 2 /* 0xA020..0xFF7F */
8ff102bd 12163 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
dc43ef94
KH
12164 : 1))); /* 0xA0A0..0xFFFF */
12165
12166 fontp->baseline_offset
12167 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
12168 ? (long) value : 0);
12169 fontp->relative_compose
12170 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
12171 ? (long) value : 0);
f78798df
KH
12172 fontp->default_ascent
12173 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
12174 ? (long) value : 0);
dc43ef94 12175
06a2c219
GM
12176 /* Set global flag fonts_changed_p to non-zero if the font loaded
12177 has a character with a smaller width than any other character
12178 before, or if the font loaded has a smalle>r height than any
12179 other font loaded before. If this happens, it will make a
12180 glyph matrix reallocation necessary. */
12181 fonts_changed_p = x_compute_min_glyph_bounds (f);
dc43ef94 12182 UNBLOCK_INPUT;
dc43ef94
KH
12183 return fontp;
12184 }
12185}
12186
06a2c219
GM
12187
12188/* Return a pointer to struct font_info of a font named FONTNAME for
12189 frame F. If no such font is loaded, return NULL. */
12190
dc43ef94
KH
12191struct font_info *
12192x_query_font (f, fontname)
12193 struct frame *f;
12194 register char *fontname;
12195{
12196 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12197 int i;
12198
12199 for (i = 0; i < dpyinfo->n_fonts; i++)
06a2c219
GM
12200 if (dpyinfo->font_table[i].name
12201 && (!strcmp (dpyinfo->font_table[i].name, fontname)
12202 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
dc43ef94
KH
12203 return (dpyinfo->font_table + i);
12204 return NULL;
12205}
12206
06a2c219
GM
12207
12208/* Find a CCL program for a font specified by FONTP, and set the member
a6582676
KH
12209 `encoder' of the structure. */
12210
12211void
12212x_find_ccl_program (fontp)
12213 struct font_info *fontp;
12214{
a42f54e6 12215 Lisp_Object list, elt;
a6582676
KH
12216
12217 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCONS (list)->cdr)
12218 {
12219 elt = XCONS (list)->car;
12220 if (CONSP (elt)
12221 && STRINGP (XCONS (elt)->car)
12222 && (fast_c_string_match_ignore_case (XCONS (elt)->car, fontp->name)
12223 >= 0))
a42f54e6
KH
12224 break;
12225 }
12226 if (! NILP (list))
12227 {
d27f8ca7
KH
12228 struct ccl_program *ccl
12229 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
a42f54e6
KH
12230
12231 if (setup_ccl_program (ccl, XCONS (elt)->cdr) < 0)
12232 xfree (ccl);
12233 else
12234 fontp->font_encoder = ccl;
a6582676
KH
12235 }
12236}
12237
06a2c219 12238
dc43ef94 12239\f
06a2c219
GM
12240/***********************************************************************
12241 Initialization
12242 ***********************************************************************/
f451eb13 12243
3afe33e7
RS
12244#ifdef USE_X_TOOLKIT
12245static XrmOptionDescRec emacs_options[] = {
12246 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
12247 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
12248
12249 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
12250 XrmoptionSepArg, NULL},
12251 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
12252
12253 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12254 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12255 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12256 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12257 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12258 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
12259 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
12260};
12261#endif /* USE_X_TOOLKIT */
12262
7a13e894
RS
12263static int x_initialized;
12264
29b38361
KH
12265#ifdef MULTI_KBOARD
12266/* Test whether two display-name strings agree up to the dot that separates
12267 the screen number from the server number. */
12268static int
12269same_x_server (name1, name2)
12270 char *name1, *name2;
12271{
12272 int seen_colon = 0;
cf591cc1
RS
12273 unsigned char *system_name = XSTRING (Vsystem_name)->data;
12274 int system_name_length = strlen (system_name);
12275 int length_until_period = 0;
12276
12277 while (system_name[length_until_period] != 0
12278 && system_name[length_until_period] != '.')
12279 length_until_period++;
12280
12281 /* Treat `unix' like an empty host name. */
12282 if (! strncmp (name1, "unix:", 5))
12283 name1 += 4;
12284 if (! strncmp (name2, "unix:", 5))
12285 name2 += 4;
12286 /* Treat this host's name like an empty host name. */
12287 if (! strncmp (name1, system_name, system_name_length)
12288 && name1[system_name_length] == ':')
12289 name1 += system_name_length;
12290 if (! strncmp (name2, system_name, system_name_length)
12291 && name2[system_name_length] == ':')
12292 name2 += system_name_length;
12293 /* Treat this host's domainless name like an empty host name. */
12294 if (! strncmp (name1, system_name, length_until_period)
12295 && name1[length_until_period] == ':')
12296 name1 += length_until_period;
12297 if (! strncmp (name2, system_name, length_until_period)
12298 && name2[length_until_period] == ':')
12299 name2 += length_until_period;
12300
29b38361
KH
12301 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
12302 {
12303 if (*name1 == ':')
12304 seen_colon++;
12305 if (seen_colon && *name1 == '.')
12306 return 1;
12307 }
12308 return (seen_colon
12309 && (*name1 == '.' || *name1 == '\0')
12310 && (*name2 == '.' || *name2 == '\0'));
12311}
12312#endif
12313
71f8198a
PE
12314#if defined (HAVE_X_I18N) || (defined (USE_X_TOOLKIT) && defined (HAVE_X11XTR6))
12315/* Recover from setlocale (LC_ALL, ""). */
12316static void
12317fixup_locale ()
12318{
12319 /* Currently we require strerror to use the "C" locale,
12320 since we don't yet support decoding its string result. */
12321#ifdef LC_MESSAGES
12322 setlocale (LC_MESSAGES, "C");
12323#endif
12324
12325 /* The Emacs Lisp reader needs LC_NUMERIC to be "C",
12326 so that numbers are read and printed properly for Emacs Lisp. */
12327 setlocale (LC_NUMERIC, "C");
12328
12329 /* Currently we require strftime to use the "C" locale,
12330 since we don't yet support encoding its format argument,
12331 or decoding its string result. */
12332 setlocale (LC_TIME, "C");
12333}
12334#endif
12335
334208b7 12336struct x_display_info *
1f8255f2 12337x_term_init (display_name, xrm_option, resource_name)
334208b7 12338 Lisp_Object display_name;
1f8255f2
RS
12339 char *xrm_option;
12340 char *resource_name;
dc6f92b8 12341{
334208b7 12342 int connection;
7a13e894 12343 Display *dpy;
334208b7
RS
12344 struct x_display_info *dpyinfo;
12345 XrmDatabase xrdb;
12346
60439948
KH
12347 BLOCK_INPUT;
12348
7a13e894
RS
12349 if (!x_initialized)
12350 {
12351 x_initialize ();
12352 x_initialized = 1;
12353 }
dc6f92b8 12354
6c183ba5 12355#ifdef HAVE_X_I18N
6186a4a0 12356 setlocale (LC_ALL, "");
71f8198a 12357 fixup_locale ();
6c183ba5
RS
12358#endif
12359
3afe33e7 12360#ifdef USE_X_TOOLKIT
2d7fc7e8
RS
12361 /* weiner@footloose.sps.mot.com reports that this causes
12362 errors with X11R5:
12363 X protocol error: BadAtom (invalid Atom parameter)
12364 on protocol request 18skiloaf.
12365 So let's not use it until R6. */
12366#ifdef HAVE_X11XTR6
bdcd49ba
RS
12367 XtSetLanguageProc (NULL, NULL, NULL);
12368#endif
12369
7f9c7f94
RS
12370 {
12371 int argc = 0;
12372 char *argv[3];
12373
12374 argv[0] = "";
12375 argc = 1;
12376 if (xrm_option)
12377 {
12378 argv[argc++] = "-xrm";
12379 argv[argc++] = xrm_option;
12380 }
12381 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
12382 resource_name, EMACS_CLASS,
12383 emacs_options, XtNumber (emacs_options),
12384 &argc, argv);
39d8bb4d
KH
12385
12386#ifdef HAVE_X11XTR6
10537cb1 12387 /* I think this is to compensate for XtSetLanguageProc. */
71f8198a 12388 fixup_locale ();
39d8bb4d 12389#endif
7f9c7f94 12390 }
3afe33e7
RS
12391
12392#else /* not USE_X_TOOLKIT */
bdcd49ba
RS
12393#ifdef HAVE_X11R5
12394 XSetLocaleModifiers ("");
12395#endif
7a13e894 12396 dpy = XOpenDisplay (XSTRING (display_name)->data);
3afe33e7 12397#endif /* not USE_X_TOOLKIT */
334208b7 12398
7a13e894
RS
12399 /* Detect failure. */
12400 if (dpy == 0)
60439948
KH
12401 {
12402 UNBLOCK_INPUT;
12403 return 0;
12404 }
7a13e894
RS
12405
12406 /* We have definitely succeeded. Record the new connection. */
12407
12408 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
12409
29b38361
KH
12410#ifdef MULTI_KBOARD
12411 {
12412 struct x_display_info *share;
12413 Lisp_Object tail;
12414
12415 for (share = x_display_list, tail = x_display_name_list; share;
12416 share = share->next, tail = XCONS (tail)->cdr)
12417 if (same_x_server (XSTRING (XCONS (XCONS (tail)->car)->car)->data,
12418 XSTRING (display_name)->data))
12419 break;
12420 if (share)
12421 dpyinfo->kboard = share->kboard;
12422 else
12423 {
12424 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
12425 init_kboard (dpyinfo->kboard);
59e755be
KH
12426 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
12427 {
12428 char *vendor = ServerVendor (dpy);
12429 dpyinfo->kboard->Vsystem_key_alist
12430 = call1 (Qvendor_specific_keysyms,
12431 build_string (vendor ? vendor : ""));
12432 }
12433
29b38361
KH
12434 dpyinfo->kboard->next_kboard = all_kboards;
12435 all_kboards = dpyinfo->kboard;
0ad5446c
KH
12436 /* Don't let the initial kboard remain current longer than necessary.
12437 That would cause problems if a file loaded on startup tries to
06a2c219 12438 prompt in the mini-buffer. */
0ad5446c
KH
12439 if (current_kboard == initial_kboard)
12440 current_kboard = dpyinfo->kboard;
29b38361
KH
12441 }
12442 dpyinfo->kboard->reference_count++;
12443 }
b9737ad3
KH
12444#endif
12445
7a13e894
RS
12446 /* Put this display on the chain. */
12447 dpyinfo->next = x_display_list;
12448 x_display_list = dpyinfo;
12449
12450 /* Put it on x_display_name_list as well, to keep them parallel. */
12451 x_display_name_list = Fcons (Fcons (display_name, Qnil),
12452 x_display_name_list);
12453 dpyinfo->name_list_element = XCONS (x_display_name_list)->car;
12454
12455 dpyinfo->display = dpy;
dc6f92b8 12456
dc6f92b8 12457#if 0
7a13e894 12458 XSetAfterFunction (x_current_display, x_trace_wire);
c118dd06 12459#endif /* ! 0 */
7a13e894
RS
12460
12461 dpyinfo->x_id_name
fc932ac6
RS
12462 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
12463 + STRING_BYTES (XSTRING (Vsystem_name))
7a13e894
RS
12464 + 2);
12465 sprintf (dpyinfo->x_id_name, "%s@%s",
12466 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
28430d3c
JB
12467
12468 /* Figure out which modifier bits mean what. */
334208b7 12469 x_find_modifier_meanings (dpyinfo);
f451eb13 12470
ab648270 12471 /* Get the scroll bar cursor. */
7a13e894 12472 dpyinfo->vertical_scroll_bar_cursor
334208b7 12473 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
f451eb13 12474
334208b7
RS
12475 xrdb = x_load_resources (dpyinfo->display, xrm_option,
12476 resource_name, EMACS_CLASS);
12477#ifdef HAVE_XRMSETDATABASE
12478 XrmSetDatabase (dpyinfo->display, xrdb);
12479#else
12480 dpyinfo->display->db = xrdb;
12481#endif
547d9db8 12482 /* Put the rdb where we can find it in a way that works on
7a13e894
RS
12483 all versions. */
12484 dpyinfo->xrdb = xrdb;
334208b7
RS
12485
12486 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
12487 DefaultScreen (dpyinfo->display));
12488 dpyinfo->visual = select_visual (dpyinfo->display, dpyinfo->screen,
12489 &dpyinfo->n_planes);
12490 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
12491 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
12492 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
12493 dpyinfo->grabbed = 0;
12494 dpyinfo->reference_count = 0;
12495 dpyinfo->icon_bitmap_id = -1;
06a2c219 12496 dpyinfo->font_table = NULL;
7a13e894
RS
12497 dpyinfo->n_fonts = 0;
12498 dpyinfo->font_table_size = 0;
12499 dpyinfo->bitmaps = 0;
12500 dpyinfo->bitmaps_size = 0;
12501 dpyinfo->bitmaps_last = 0;
12502 dpyinfo->scratch_cursor_gc = 0;
12503 dpyinfo->mouse_face_mouse_frame = 0;
12504 dpyinfo->mouse_face_deferred_gc = 0;
12505 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
12506 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
06a2c219 12507 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
7a13e894
RS
12508 dpyinfo->mouse_face_window = Qnil;
12509 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
12510 dpyinfo->mouse_face_defer = 0;
0f941935
KH
12511 dpyinfo->x_focus_frame = 0;
12512 dpyinfo->x_focus_event_frame = 0;
12513 dpyinfo->x_highlight_frame = 0;
06a2c219 12514 dpyinfo->image_cache = make_image_cache ();
334208b7 12515
06a2c219
GM
12516 {
12517 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
12518 double pixels = DisplayHeight (dpyinfo->display, screen_number);
12519 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
12520 dpyinfo->resy = pixels * 25.4 / mm;
12521 pixels = DisplayWidth (dpyinfo->display, screen_number);
12522 mm = DisplayWidthMM (dpyinfo->display, screen_number);
12523 dpyinfo->resx = pixels * 25.4 / mm;
12524 }
12525
334208b7
RS
12526 dpyinfo->Xatom_wm_protocols
12527 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
12528 dpyinfo->Xatom_wm_take_focus
12529 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
12530 dpyinfo->Xatom_wm_save_yourself
12531 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
12532 dpyinfo->Xatom_wm_delete_window
12533 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
12534 dpyinfo->Xatom_wm_change_state
12535 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
12536 dpyinfo->Xatom_wm_configure_denied
12537 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
12538 dpyinfo->Xatom_wm_window_moved
12539 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
12540 dpyinfo->Xatom_editres
12541 = XInternAtom (dpyinfo->display, "Editres", False);
12542 dpyinfo->Xatom_CLIPBOARD
12543 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
12544 dpyinfo->Xatom_TIMESTAMP
12545 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
12546 dpyinfo->Xatom_TEXT
12547 = XInternAtom (dpyinfo->display, "TEXT", False);
dc43ef94
KH
12548 dpyinfo->Xatom_COMPOUND_TEXT
12549 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
334208b7
RS
12550 dpyinfo->Xatom_DELETE
12551 = XInternAtom (dpyinfo->display, "DELETE", False);
12552 dpyinfo->Xatom_MULTIPLE
12553 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
12554 dpyinfo->Xatom_INCR
12555 = XInternAtom (dpyinfo->display, "INCR", False);
12556 dpyinfo->Xatom_EMACS_TMP
12557 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
12558 dpyinfo->Xatom_TARGETS
12559 = XInternAtom (dpyinfo->display, "TARGETS", False);
12560 dpyinfo->Xatom_NULL
12561 = XInternAtom (dpyinfo->display, "NULL", False);
12562 dpyinfo->Xatom_ATOM_PAIR
12563 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
dc43ef94
KH
12564 /* For properties of font. */
12565 dpyinfo->Xatom_PIXEL_SIZE
12566 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
12567 dpyinfo->Xatom_MULE_BASELINE_OFFSET
12568 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
12569 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
12570 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
f78798df
KH
12571 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
12572 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
334208b7 12573
06a2c219
GM
12574 /* Ghostscript support. */
12575 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
12576 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
12577
12578 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
12579 False);
12580
547d9db8
KH
12581 dpyinfo->cut_buffers_initialized = 0;
12582
334208b7
RS
12583 connection = ConnectionNumber (dpyinfo->display);
12584 dpyinfo->connection = connection;
12585
dc43ef94 12586 {
5d7cc324
RS
12587 char null_bits[1];
12588
12589 null_bits[0] = 0x00;
dc43ef94
KH
12590
12591 dpyinfo->null_pixel
12592 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
12593 null_bits, 1, 1, (long) 0, (long) 0,
12594 1);
12595 }
12596
06a2c219
GM
12597 {
12598 extern int gray_bitmap_width, gray_bitmap_height;
12599 extern unsigned char *gray_bitmap_bits;
12600 dpyinfo->gray
12601 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
12602 gray_bitmap_bits,
12603 gray_bitmap_width, gray_bitmap_height,
12604 (unsigned long) 1, (unsigned long) 0, 1);
12605 }
12606
87485d6f
MW
12607#ifdef subprocesses
12608 /* This is only needed for distinguishing keyboard and process input. */
334208b7 12609 if (connection != 0)
7a13e894 12610 add_keyboard_wait_descriptor (connection);
87485d6f 12611#endif
6d4238f3 12612
041b69ac 12613#ifndef F_SETOWN_BUG
dc6f92b8 12614#ifdef F_SETOWN
dc6f92b8 12615#ifdef F_SETOWN_SOCK_NEG
61c3ce62 12616 /* stdin is a socket here */
334208b7 12617 fcntl (connection, F_SETOWN, -getpid ());
c118dd06 12618#else /* ! defined (F_SETOWN_SOCK_NEG) */
334208b7 12619 fcntl (connection, F_SETOWN, getpid ());
c118dd06
JB
12620#endif /* ! defined (F_SETOWN_SOCK_NEG) */
12621#endif /* ! defined (F_SETOWN) */
041b69ac 12622#endif /* F_SETOWN_BUG */
dc6f92b8
JB
12623
12624#ifdef SIGIO
eee20f6a
KH
12625 if (interrupt_input)
12626 init_sigio (connection);
c118dd06 12627#endif /* ! defined (SIGIO) */
dc6f92b8 12628
51b592fb 12629#ifdef USE_LUCID
f8c39f51 12630#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
51b592fb
RS
12631 /* Make sure that we have a valid font for dialog boxes
12632 so that Xt does not crash. */
12633 {
12634 Display *dpy = dpyinfo->display;
12635 XrmValue d, fr, to;
12636 Font font;
e99db5a1 12637 int count;
51b592fb
RS
12638
12639 d.addr = (XPointer)&dpy;
12640 d.size = sizeof (Display *);
12641 fr.addr = XtDefaultFont;
12642 fr.size = sizeof (XtDefaultFont);
12643 to.size = sizeof (Font *);
12644 to.addr = (XPointer)&font;
e99db5a1 12645 count = x_catch_errors (dpy);
51b592fb
RS
12646 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
12647 abort ();
12648 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
12649 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
e99db5a1 12650 x_uncatch_errors (dpy, count);
51b592fb
RS
12651 }
12652#endif
f8c39f51 12653#endif
51b592fb 12654
60439948
KH
12655 UNBLOCK_INPUT;
12656
7a13e894
RS
12657 return dpyinfo;
12658}
12659\f
12660/* Get rid of display DPYINFO, assuming all frames are already gone,
12661 and without sending any more commands to the X server. */
dc6f92b8 12662
7a13e894
RS
12663void
12664x_delete_display (dpyinfo)
12665 struct x_display_info *dpyinfo;
12666{
12667 delete_keyboard_wait_descriptor (dpyinfo->connection);
12668
12669 /* Discard this display from x_display_name_list and x_display_list.
12670 We can't use Fdelq because that can quit. */
12671 if (! NILP (x_display_name_list)
12672 && EQ (XCONS (x_display_name_list)->car, dpyinfo->name_list_element))
12673 x_display_name_list = XCONS (x_display_name_list)->cdr;
12674 else
12675 {
12676 Lisp_Object tail;
12677
12678 tail = x_display_name_list;
12679 while (CONSP (tail) && CONSP (XCONS (tail)->cdr))
12680 {
12681 if (EQ (XCONS (XCONS (tail)->cdr)->car,
12682 dpyinfo->name_list_element))
12683 {
12684 XCONS (tail)->cdr = XCONS (XCONS (tail)->cdr)->cdr;
12685 break;
12686 }
12687 tail = XCONS (tail)->cdr;
12688 }
12689 }
12690
12691 if (x_display_list == dpyinfo)
12692 x_display_list = dpyinfo->next;
7f9c7f94
RS
12693 else
12694 {
12695 struct x_display_info *tail;
7a13e894 12696
7f9c7f94
RS
12697 for (tail = x_display_list; tail; tail = tail->next)
12698 if (tail->next == dpyinfo)
12699 tail->next = tail->next->next;
12700 }
7a13e894 12701
0d777288
RS
12702#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
12703#ifndef AIX /* On AIX, XCloseDisplay calls this. */
7f9c7f94
RS
12704 XrmDestroyDatabase (dpyinfo->xrdb);
12705#endif
0d777288 12706#endif
29b38361
KH
12707#ifdef MULTI_KBOARD
12708 if (--dpyinfo->kboard->reference_count == 0)
39f79001 12709 delete_kboard (dpyinfo->kboard);
b9737ad3
KH
12710#endif
12711 xfree (dpyinfo->font_table);
12712 xfree (dpyinfo->x_id_name);
12713 xfree (dpyinfo);
7a13e894
RS
12714}
12715\f
12716/* Set up use of X before we make the first connection. */
12717
06a2c219
GM
12718static struct redisplay_interface x_redisplay_interface =
12719{
12720 x_produce_glyphs,
12721 x_write_glyphs,
12722 x_insert_glyphs,
12723 x_clear_end_of_line,
12724 x_scroll_run,
12725 x_after_update_window_line,
12726 x_update_window_begin,
12727 x_update_window_end,
12728 XTcursor_to,
12729 x_flush,
66ac4b0e
GM
12730 x_get_glyph_overhangs,
12731 x_fix_overlapping_area
06a2c219
GM
12732};
12733
dfcf069d 12734void
7a13e894
RS
12735x_initialize ()
12736{
06a2c219
GM
12737 rif = &x_redisplay_interface;
12738
12739 clear_frame_hook = x_clear_frame;
12740 ins_del_lines_hook = x_ins_del_lines;
12741 change_line_highlight_hook = x_change_line_highlight;
12742 delete_glyphs_hook = x_delete_glyphs;
dc6f92b8
JB
12743 ring_bell_hook = XTring_bell;
12744 reset_terminal_modes_hook = XTreset_terminal_modes;
12745 set_terminal_modes_hook = XTset_terminal_modes;
06a2c219
GM
12746 update_begin_hook = x_update_begin;
12747 update_end_hook = x_update_end;
dc6f92b8
JB
12748 set_terminal_window_hook = XTset_terminal_window;
12749 read_socket_hook = XTread_socket;
b8009dd1 12750 frame_up_to_date_hook = XTframe_up_to_date;
dc6f92b8 12751 reassert_line_highlight_hook = XTreassert_line_highlight;
90e65f07 12752 mouse_position_hook = XTmouse_position;
f451eb13 12753 frame_rehighlight_hook = XTframe_rehighlight;
dbc4e1c1 12754 frame_raise_lower_hook = XTframe_raise_lower;
ab648270
JB
12755 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
12756 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
12757 redeem_scroll_bar_hook = XTredeem_scroll_bar;
12758 judge_scroll_bars_hook = XTjudge_scroll_bars;
06a2c219 12759 estimate_mode_line_height_hook = x_estimate_mode_line_height;
58769bee 12760
f676886a 12761 scroll_region_ok = 1; /* we'll scroll partial frames */
dc6f92b8
JB
12762 char_ins_del_ok = 0; /* just as fast to write the line */
12763 line_ins_del_ok = 1; /* we'll just blt 'em */
12764 fast_clear_end_of_line = 1; /* X does this well */
58769bee 12765 memory_below_frame = 0; /* we don't remember what scrolls
dc6f92b8
JB
12766 off the bottom */
12767 baud_rate = 19200;
12768
7a13e894 12769 x_noop_count = 0;
06a2c219
GM
12770 last_toolbar_item = -1;
12771 any_help_event_p = 0;
12772
b30b24cb
RS
12773 /* Try to use interrupt input; if we can't, then start polling. */
12774 Fset_input_mode (Qt, Qnil, Qt, Qnil);
12775
7f9c7f94
RS
12776#ifdef USE_X_TOOLKIT
12777 XtToolkitInitialize ();
12778 Xt_app_con = XtCreateApplicationContext ();
665881ad 12779 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
7f9c7f94
RS
12780#endif
12781
58769bee 12782 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 12783 original error handler. */
e99db5a1 12784 XSetErrorHandler (x_error_handler);
334208b7 12785 XSetIOErrorHandler (x_io_error_quitter);
dc6f92b8 12786
06a2c219 12787 /* Disable Window Change signals; they are handled by X events. */
dc6f92b8
JB
12788#ifdef SIGWINCH
12789 signal (SIGWINCH, SIG_DFL);
c118dd06 12790#endif /* ! defined (SIGWINCH) */
dc6f92b8 12791
92e2441b 12792 signal (SIGPIPE, x_connection_signal);
dc6f92b8 12793}
55123275 12794
06a2c219 12795
55123275
JB
12796void
12797syms_of_xterm ()
12798{
e99db5a1
RS
12799 staticpro (&x_error_message_string);
12800 x_error_message_string = Qnil;
12801
7a13e894
RS
12802 staticpro (&x_display_name_list);
12803 x_display_name_list = Qnil;
334208b7 12804
ab648270 12805 staticpro (&last_mouse_scroll_bar);
e53cb100 12806 last_mouse_scroll_bar = Qnil;
59e755be
KH
12807
12808 staticpro (&Qvendor_specific_keysyms);
12809 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
2237cac9
RS
12810
12811 staticpro (&last_mouse_press_frame);
12812 last_mouse_press_frame = Qnil;
06a2c219
GM
12813
12814 staticpro (&help_echo);
12815 help_echo = Qnil;
12816 staticpro (&previous_help_echo);
12817 previous_help_echo = Qnil;
12818
12819 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
12820 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
12821For example, if a block cursor is over a tab, it will be drawn as\n\
12822wide as that tab on the display.");
12823 x_stretch_cursor_p = 0;
12824
12825 DEFVAR_BOOL ("x-toolkit-scroll-bars-p", &x_toolkit_scroll_bars_p,
12826 "If not nil, Emacs uses toolkit scroll bars.");
12827#if USE_TOOLKIT_SCROLL_BARS
12828 x_toolkit_scroll_bars_p = 1;
12829#else
12830 x_toolkit_scroll_bars_p = 0;
12831#endif
12832
12833 defsubr (&Sxt_process_timeouts);
12834 staticpro (&last_mouse_motion_frame);
12835 last_mouse_motion_frame = Qnil;
55123275 12836}
6cf0ae86
RS
12837
12838#endif /* not HAVE_X_WINDOWS */
06a2c219 12839