(setup_coding_system): Get compiled CCL code by just
[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);
909 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
910 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
911 face->gc,
912 (left
913 - FRAME_X_FLAGS_AREA_WIDTH (f)
914 + border),
915 WINDOW_TO_FRAME_PIXEL_Y (w, max (top_line_height,
916 row->y)),
917 FRAME_X_FLAGS_AREA_WIDTH (f) - border,
918 row->visible_height);
919 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
920 }
921
922 /* Draw the left bitmap. */
923 if (bitmap != NO_BITMAP)
924 x_draw_bitmap (w, row, bitmap);
925
926 /* Decide which bitmap to draw at the right side. */
927 if (row->truncated_on_right_p)
928 bitmap = RIGHT_TRUNCATION_BITMAP;
929 else if (row->continued_p)
930 bitmap = CONTINUED_LINE_BITMAP;
931 else
932 bitmap = NO_BITMAP;
933
934 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
935 the flags area. */
936 if (bitmap == NO_BITMAP
937 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_FLAGS_AREA_WIDTH (f)
938 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
939 {
940 int right = window_box_right (w, -1);
941
942 if (top_line_height < 0)
943 top_line_height = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
944 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
945 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
946 face->gc,
947 right,
948 WINDOW_TO_FRAME_PIXEL_Y (w, max (top_line_height,
949 row->y)),
950 FRAME_X_FLAGS_AREA_WIDTH (f),
951 row->visible_height);
952 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
953 }
954
955 /* Draw the right bitmap. */
956 if (bitmap != NO_BITMAP)
957 x_draw_bitmap (w, row, bitmap);
958}
959
dc6f92b8 960\f
06a2c219
GM
961/***********************************************************************
962 Line Highlighting
963 ***********************************************************************/
dc6f92b8 964
06a2c219
GM
965/* External interface to control of standout mode. Not used for X
966 frames. Aborts when called. */
967
968static void
dc6f92b8
JB
969XTreassert_line_highlight (new, vpos)
970 int new, vpos;
971{
06a2c219 972 abort ();
dc6f92b8
JB
973}
974
06a2c219
GM
975
976/* Call this when about to modify line at position VPOS and change
977 whether it is highlighted. Not used for X frames. Aborts when
978 called. */
dc6f92b8 979
dfcf069d 980static void
06a2c219
GM
981x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
982 int new_highlight, vpos, y, first_unused_hpos;
dc6f92b8 983{
06a2c219 984 abort ();
dc6f92b8
JB
985}
986
06a2c219
GM
987
988/* This is called when starting Emacs and when restarting after
989 suspend. When starting Emacs, no X window is mapped. And nothing
990 must be done to Emacs's own window if it is suspended (though that
991 rarely happens). */
dc6f92b8 992
dfcf069d 993static void
dc6f92b8
JB
994XTset_terminal_modes ()
995{
996}
997
06a2c219
GM
998/* This is called when exiting or suspending Emacs. Exiting will make
999 the X-windows go away, and suspending requires no action. */
dc6f92b8 1000
dfcf069d 1001static void
dc6f92b8
JB
1002XTreset_terminal_modes ()
1003{
dc6f92b8 1004}
06a2c219
GM
1005
1006
dc6f92b8 1007\f
06a2c219
GM
1008/***********************************************************************
1009 Output Cursor
1010 ***********************************************************************/
1011
1012/* Set the global variable output_cursor to CURSOR. All cursor
1013 positions are relative to updated_window. */
dc6f92b8 1014
dfcf069d 1015static void
06a2c219
GM
1016set_output_cursor (cursor)
1017 struct cursor_pos *cursor;
dc6f92b8 1018{
06a2c219
GM
1019 output_cursor.hpos = cursor->hpos;
1020 output_cursor.vpos = cursor->vpos;
1021 output_cursor.x = cursor->x;
1022 output_cursor.y = cursor->y;
1023}
1024
1025
1026/* Set a nominal cursor position.
dc6f92b8 1027
06a2c219
GM
1028 HPOS and VPOS are column/row positions in a window glyph matrix. X
1029 and Y are window text area relative pixel positions.
1030
1031 If this is done during an update, updated_window will contain the
1032 window that is being updated and the position is the future output
1033 cursor position for that window. If updated_window is null, use
1034 selected_window and display the cursor at the given position. */
1035
1036static void
1037XTcursor_to (vpos, hpos, y, x)
1038 int vpos, hpos, y, x;
1039{
1040 struct window *w;
1041
1042 /* If updated_window is not set, work on selected_window. */
1043 if (updated_window)
1044 w = updated_window;
1045 else
1046 w = XWINDOW (selected_window);
dbcb258a 1047
06a2c219
GM
1048 /* Set the output cursor. */
1049 output_cursor.hpos = hpos;
1050 output_cursor.vpos = vpos;
1051 output_cursor.x = x;
1052 output_cursor.y = y;
dc6f92b8 1053
06a2c219
GM
1054 /* If not called as part of an update, really display the cursor.
1055 This will also set the cursor position of W. */
1056 if (updated_window == NULL)
dc6f92b8
JB
1057 {
1058 BLOCK_INPUT;
06a2c219 1059 x_display_cursor (w, 1, hpos, vpos, x, y);
334208b7 1060 XFlush (FRAME_X_DISPLAY (selected_frame));
dc6f92b8
JB
1061 UNBLOCK_INPUT;
1062 }
1063}
dc43ef94 1064
06a2c219
GM
1065
1066\f
1067/***********************************************************************
1068 Display Iterator
1069 ***********************************************************************/
1070
1071/* Function prototypes of this page. */
1072
1073static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1074 struct glyph *,
1075 XChar2b *));
1076static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1077 int, XChar2b *, int));
1078static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1079static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1080static void x_append_glyph P_ ((struct it *));
1081static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1082 int, int, double));
1083static void x_produce_glyphs P_ ((struct it *));
1084static void x_produce_special_glyphs P_ ((struct it *, enum display_element_type));
1085static void x_produce_image_glyph P_ ((struct it *it));
1086
1087
1088/* Return a pointer to per-char metric information in FONT of a
1089 character pointed by B which is a pointer to an XChar2b. */
dc43ef94
KH
1090
1091#define PER_CHAR_METRIC(font, b) \
1092 ((font)->per_char \
1093 ? ((font)->per_char + (b)->byte2 - (font)->min_char_or_byte2 \
1094 + (((font)->min_byte1 || (font)->max_byte1) \
1095 ? (((b)->byte1 - (font)->min_byte1) \
1096 * ((font)->max_char_or_byte2 - (font)->min_char_or_byte2 + 1)) \
1097 : 0)) \
1098 : &((font)->max_bounds))
1099
dc6f92b8 1100
06a2c219
GM
1101/* Get metrics of character CHAR2B in FONT. Value is always non-null.
1102 If CHAR2B is not contained in FONT, the font's default character
1103 metric is returned. */
dc43ef94 1104
06a2c219
GM
1105static INLINE XCharStruct *
1106x_per_char_metric (font, char2b)
1107 XFontStruct *font;
1108 XChar2b *char2b;
1109{
1110 /* The result metric information. */
1111 XCharStruct *pcm = NULL;
dc6f92b8 1112
06a2c219 1113 xassert (font && char2b);
dc6f92b8 1114
06a2c219 1115 if (font->per_char != NULL)
dc6f92b8 1116 {
06a2c219 1117 if (font->min_byte1 == 0 && font->max_byte1 == 0)
dc43ef94 1118 {
06a2c219
GM
1119 /* min_char_or_byte2 specifies the linear character index
1120 corresponding to the first element of the per_char array,
1121 max_char_or_byte2 is the index of the last character. A
1122 character with non-zero CHAR2B->byte1 is not in the font.
1123 A character with byte2 less than min_char_or_byte2 or
1124 greater max_char_or_byte2 is not in the font. */
1125 if (char2b->byte1 == 0
1126 && char2b->byte2 >= font->min_char_or_byte2
1127 && char2b->byte2 <= font->max_char_or_byte2)
1128 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
dc43ef94 1129 }
06a2c219 1130 else
dc6f92b8 1131 {
06a2c219
GM
1132 /* If either min_byte1 or max_byte1 are nonzero, both
1133 min_char_or_byte2 and max_char_or_byte2 are less than
1134 256, and the 2-byte character index values corresponding
1135 to the per_char array element N (counting from 0) are:
1136
1137 byte1 = N/D + min_byte1
1138 byte2 = N\D + min_char_or_byte2
1139
1140 where:
1141
1142 D = max_char_or_byte2 - min_char_or_byte2 + 1
1143 / = integer division
1144 \ = integer modulus */
1145 if (char2b->byte1 >= font->min_byte1
1146 && char2b->byte1 <= font->max_byte1
1147 && char2b->byte2 >= font->min_char_or_byte2
1148 && char2b->byte2 <= font->max_char_or_byte2)
1149 {
1150 pcm = (font->per_char
1151 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1152 * (char2b->byte1 - font->min_byte1))
1153 + (char2b->byte2 - font->min_char_or_byte2));
1154 }
dc6f92b8 1155 }
06a2c219
GM
1156 }
1157 else
1158 {
1159 /* If the per_char pointer is null, all glyphs between the first
1160 and last character indexes inclusive have the same
1161 information, as given by both min_bounds and max_bounds. */
1162 if (char2b->byte2 >= font->min_char_or_byte2
1163 && char2b->byte2 <= font->max_char_or_byte2)
1164 pcm = &font->max_bounds;
1165 }
dc6f92b8 1166
4a4dc352 1167
06a2c219
GM
1168 if (pcm == NULL || pcm->width == 0)
1169 {
1170 /* Character not contained in the font. FONT->default_char
1171 gives the character that will be printed. FONT->default_char
1172 is a 16-bit character code with byte1 in the most significant
1173 byte and byte2 in the least significant byte. */
1174 XChar2b default_char;
1175 default_char.byte1 = (font->default_char >> BITS_PER_CHAR) & 0xff;
1176 default_char.byte2 = font->default_char & 0xff;
1177
1178 /* Avoid an endless recursion if FONT->default_char itself
1179 hasn't per char metrics. handa@etl.go.jp reports that some
1180 fonts have this problem. */
1181 if (default_char.byte1 != char2b->byte1
1182 || default_char.byte2 != char2b->byte2)
1183 pcm = x_per_char_metric (font, &default_char);
1184 else
1185 pcm = &font->max_bounds;
1186 }
1187
1188 return pcm;
1189}
b73b6aaf 1190
57b03282 1191
06a2c219
GM
1192/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1193 the two-byte form of C. Encoding is returned in *CHAR2B. */
dc43ef94 1194
06a2c219
GM
1195static INLINE void
1196x_encode_char (c, char2b, font_info)
1197 int c;
1198 XChar2b *char2b;
1199 struct font_info *font_info;
1200{
1201 int charset = CHAR_CHARSET (c);
1202 XFontStruct *font = font_info->font;
1203
1204 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1205 This may be either a program in a special encoder language or a
1206 fixed encoding. */
1207 if (font_info->font_encoder)
1208 {
1209 /* It's a program. */
1210 struct ccl_program *ccl = font_info->font_encoder;
1211
1212 if (CHARSET_DIMENSION (charset) == 1)
1213 {
1214 ccl->reg[0] = charset;
1215 ccl->reg[1] = char2b->byte2;
1216 }
1217 else
1218 {
1219 ccl->reg[0] = charset;
1220 ccl->reg[1] = char2b->byte1;
1221 ccl->reg[2] = char2b->byte2;
1222 }
1223
1224 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1225
1226 /* We assume that MSBs are appropriately set/reset by CCL
1227 program. */
1228 if (font->max_byte1 == 0) /* 1-byte font */
1229 char2b->byte2 = ccl->reg[1];
1230 else
1231 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1232 }
1233 else if (font_info->encoding[charset])
1234 {
1235 /* Fixed encoding scheme. See fontset.h for the meaning of the
1236 encoding numbers. */
1237 int enc = font_info->encoding[charset];
1238
1239 if ((enc == 1 || enc == 2)
1240 && CHARSET_DIMENSION (charset) == 2)
1241 char2b->byte1 |= 0x80;
1242
1243 if (enc == 1 || enc == 3)
1244 char2b->byte2 |= 0x80;
1245 }
1246}
1247
1248
1249/* Get face and two-byte form of character C in face FACE_ID on frame
1250 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1251 means we want to display multibyte text. Value is a pointer to a
1252 realized face that is ready for display. */
1253
1254static INLINE struct face *
1255x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1256 struct frame *f;
1257 int c, face_id;
1258 XChar2b *char2b;
1259 int multibyte_p;
1260{
1261 struct face *face = FACE_FROM_ID (f, face_id);
1262
1263 if (!multibyte_p)
1264 {
1265 /* Unibyte case. We don't have to encode, but we have to make
1266 sure to use a face suitable for unibyte. */
1267 char2b->byte1 = 0;
1268 char2b->byte2 = c;
1269
1270 if (!FACE_SUITABLE_FOR_CHARSET_P (face, -1))
1271 {
1272 face_id = FACE_FOR_CHARSET (f, face_id, -1);
1273 face = FACE_FROM_ID (f, face_id);
1274 }
1275 }
1276 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1277 {
1278 /* Case of ASCII in a face known to fit ASCII. */
1279 char2b->byte1 = 0;
1280 char2b->byte2 = c;
1281 }
1282 else
1283 {
1284 int c1, c2, charset;
1285
1286 /* Split characters into bytes. If c2 is -1 afterwards, C is
1287 really a one-byte character so that byte1 is zero. */
1288 SPLIT_CHAR (c, charset, c1, c2);
1289 if (c2 > 0)
1290 char2b->byte1 = c1, char2b->byte2 = c2;
1291 else
1292 char2b->byte1 = 0, char2b->byte2 = c1;
1293
1294 /* Get the face for displaying C. If `face' is not suitable for
1295 charset, get the one that fits. (This can happen for the
1296 translations of composite characters where the glyph
1297 specifies a face for ASCII, but translations have a different
1298 charset.) */
1299 if (!FACE_SUITABLE_FOR_CHARSET_P (face, charset))
1300 {
1301 face_id = FACE_FOR_CHARSET (f, face_id, charset);
1302 face = FACE_FROM_ID (f, face_id);
1303 }
1304
1305 /* Maybe encode the character in *CHAR2B. */
1306 if (charset != CHARSET_ASCII)
1307 {
1308 struct font_info *font_info
1309 = FONT_INFO_FROM_ID (f, face->font_info_id);
1310 if (font_info)
1311 {
1312 x_encode_char (c, char2b, font_info);
1313 if (charset == charset_latin_iso8859_1)
1314 {
1315 xassert (((XFontStruct *) font_info->font)->max_char_or_byte2
1316 >= 0x80);
1317 char2b->byte2 |= 0x80;
1318 }
1319 }
1320 }
1321 }
1322
1323 /* Make sure X resources of the face are allocated. */
1324 xassert (face != NULL);
1325 PREPARE_FACE_FOR_DISPLAY (f, face);
1326
1327 return face;
1328}
1329
1330
1331/* Get face and two-byte form of character glyph GLYPH on frame F.
1332 The encoding of GLYPH->u.ch.code is returned in *CHAR2B. Value is
1333 a pointer to a realized face that is ready for display. */
1334
1335static INLINE struct face *
1336x_get_glyph_face_and_encoding (f, glyph, char2b)
1337 struct frame *f;
1338 struct glyph *glyph;
1339 XChar2b *char2b;
1340{
1341 struct face *face;
1342
1343 xassert (glyph->type == CHAR_GLYPH);
1344 face = FACE_FROM_ID (f, glyph->u.ch.face_id);
1345
1346 if (!glyph->multibyte_p)
1347 {
1348 /* Unibyte case. We don't have to encode, but we have to make
1349 sure to use a face suitable for unibyte. */
1350 char2b->byte1 = 0;
1351 char2b->byte2 = glyph->u.ch.code;
1352 }
1353 else if (glyph->u.ch.code < 128
1354 && glyph->u.ch.face_id < BASIC_FACE_ID_SENTINEL)
1355 {
1356 /* Case of ASCII in a face known to fit ASCII. */
1357 char2b->byte1 = 0;
1358 char2b->byte2 = glyph->u.ch.code;
1359 }
1360 else
1361 {
1362 int c1, c2, charset;
1363
1364 /* Split characters into bytes. If c2 is -1 afterwards, C is
1365 really a one-byte character so that byte1 is zero. */
1366 SPLIT_CHAR (glyph->u.ch.code, charset, c1, c2);
1367 if (c2 > 0)
1368 char2b->byte1 = c1, char2b->byte2 = c2;
1369 else
1370 char2b->byte1 = 0, char2b->byte2 = c1;
1371
1372 /* Maybe encode the character in *CHAR2B. */
1373 if (charset != CHARSET_ASCII)
1374 {
1375 struct font_info *font_info
1376 = FONT_INFO_FROM_ID (f, face->font_info_id);
1377 if (font_info)
1378 {
1379 x_encode_char (glyph->u.ch.code, char2b, font_info);
1380 if (charset == charset_latin_iso8859_1)
1381 char2b->byte2 |= 0x80;
1382 }
1383 }
1384 }
1385
1386 /* Make sure X resources of the face are allocated. */
1387 xassert (face != NULL);
1388 PREPARE_FACE_FOR_DISPLAY (f, face);
1389 return face;
1390}
1391
1392
1393/* Store one glyph for IT->char_to_display in IT->glyph_row.
1394 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1395
1396static INLINE void
1397x_append_glyph (it)
1398 struct it *it;
1399{
1400 struct glyph *glyph;
1401 enum glyph_row_area area = it->area;
1402
1403 xassert (it->glyph_row);
1404 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1405
1406 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1407 if (glyph < it->glyph_row->glyphs[area + 1])
1408 {
1409 /* Play it safe. If sub-structures of the glyph are not all the
1410 same size, it otherwise be that some bits stay set. This
1411 would prevent a comparison with GLYPH_EQUAL_P. */
1412 glyph->u.val = 0;
1413
1414 glyph->type = CHAR_GLYPH;
1415 glyph->pixel_width = it->pixel_width;
1416 glyph->u.ch.code = it->char_to_display;
1417 glyph->u.ch.face_id = it->face_id;
1418 glyph->charpos = CHARPOS (it->position);
1419 glyph->object = it->object;
1420 glyph->left_box_line_p = it->start_of_box_run_p;
1421 glyph->right_box_line_p = it->end_of_box_run_p;
1422 glyph->voffset = it->voffset;
1423 glyph->multibyte_p = it->multibyte_p;
1424 ++it->glyph_row->used[area];
1425 }
1426}
1427
1428
1429/* Change IT->ascent and IT->height according to the setting of
1430 IT->voffset. */
1431
1432static INLINE void
1433take_vertical_position_into_account (it)
1434 struct it *it;
1435{
1436 if (it->voffset)
1437 {
1438 if (it->voffset < 0)
1439 /* Increase the ascent so that we can display the text higher
1440 in the line. */
1441 it->ascent += abs (it->voffset);
1442 else
1443 /* Increase the descent so that we can display the text lower
1444 in the line. */
1445 it->descent += it->voffset;
1446 }
1447}
1448
1449
1450/* Produce glyphs/get display metrics for the image IT is loaded with.
1451 See the description of struct display_iterator in dispextern.h for
1452 an overview of struct display_iterator. */
1453
1454static void
1455x_produce_image_glyph (it)
1456 struct it *it;
1457{
1458 struct image *img;
1459 struct face *face;
1460
1461 xassert (it->what == IT_IMAGE);
1462
1463 face = FACE_FROM_ID (it->f, it->face_id);
1464 img = IMAGE_FROM_ID (it->f, it->image_id);
1465 xassert (img);
1466
1467 /* Make sure X resources of the face and image are loaded. */
1468 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1469 prepare_image_for_display (it->f, img);
1470
1471 it->ascent = IMAGE_ASCENT (img);
1472 it->descent = img->height + 2 * img->margin - it->ascent;
1473 it->pixel_width = img->width + 2 * img->margin;
1474
1475 it->nglyphs = 1;
1476
1477 if (face->box != FACE_NO_BOX)
1478 {
1479 it->ascent += face->box_line_width;
1480 it->descent += face->box_line_width;
1481
1482 if (it->start_of_box_run_p)
1483 it->pixel_width += face->box_line_width;
1484 if (it->end_of_box_run_p)
1485 it->pixel_width += face->box_line_width;
1486 }
1487
1488 take_vertical_position_into_account (it);
1489
1490 if (it->glyph_row)
1491 {
1492 struct glyph *glyph;
1493 enum glyph_row_area area = it->area;
1494
1495 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1496 if (glyph < it->glyph_row->glyphs[area + 1])
1497 {
1498 glyph->type = IMAGE_GLYPH;
1499 glyph->u.img.id = img->id;
1500 glyph->u.img.face_id = it->face_id;
1501 glyph->pixel_width = it->pixel_width;
1502 glyph->charpos = CHARPOS (it->position);
1503 glyph->object = it->object;
1504 glyph->left_box_line_p = it->start_of_box_run_p;
1505 glyph->right_box_line_p = it->end_of_box_run_p;
1506 glyph->voffset = it->voffset;
1507 glyph->multibyte_p = it->multibyte_p;
1508 ++it->glyph_row->used[area];
1509 }
1510 }
1511}
1512
1513
1514/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1515 of the glyph, WIDTH and HEIGHT are the width and height of the
1516 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1517 ascent of the glyph (0 <= ASCENT <= 1). */
1518
1519static void
1520x_append_stretch_glyph (it, object, width, height, ascent)
1521 struct it *it;
1522 Lisp_Object object;
1523 int width, height;
1524 double ascent;
1525{
1526 struct glyph *glyph;
1527 enum glyph_row_area area = it->area;
1528
1529 xassert (ascent >= 0 && ascent <= 1);
1530
1531 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1532 if (glyph < it->glyph_row->glyphs[area + 1])
1533 {
1534 glyph->type = STRETCH_GLYPH;
1535 glyph->u.stretch.ascent = height * ascent;
1536 glyph->u.stretch.height = height;
1537 glyph->u.stretch.face_id = it->face_id;
1538 glyph->pixel_width = width;
1539 glyph->charpos = CHARPOS (it->position);
1540 glyph->object = object;
1541 glyph->left_box_line_p = it->start_of_box_run_p;
1542 glyph->right_box_line_p = it->end_of_box_run_p;
1543 glyph->voffset = it->voffset;
1544 glyph->multibyte_p = it->multibyte_p;
1545 ++it->glyph_row->used[area];
1546 }
1547}
1548
1549
1550/* Produce a stretch glyph for iterator IT. IT->object is the value
1551 of the glyph property displayed. The value must be a list
1552 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1553 being recognized:
1554
1555 1. `:width WIDTH' specifies that the space should be WIDTH *
1556 canonical char width wide. WIDTH may be an integer or floating
1557 point number.
1558
1559 2. `:relative-width FACTOR' specifies that the width of the stretch
1560 should be computed from the width of the first character having the
1561 `glyph' property, and should be FACTOR times that width.
1562
1563 3. `:align-to HPOS' specifies that the space should be wide enough
1564 to reach HPOS, a value in canonical character units.
1565
1566 Exactly one of the above pairs must be present.
1567
1568 4. `:height HEIGHT' specifies that the height of the stretch produced
1569 should be HEIGHT, measured in canonical character units.
1570
1571 5. `:relative-height FACTOR' specifies that the height of the the
1572 stretch should be FACTOR times the height of the characters having
1573 the glyph property.
1574
1575 Either none or exactly one of 4 or 5 must be present.
1576
1577 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1578 of the stretch should be used for the ascent of the stretch.
1579 ASCENT must be in the range 0 <= ASCENT <= 100. */
1580
1581#define NUMVAL(X) \
1582 ((INTEGERP (X) || FLOATP (X)) \
1583 ? XFLOATINT (X) \
1584 : - 1)
1585
1586
1587static void
1588x_produce_stretch_glyph (it)
1589 struct it *it;
1590{
1591 /* (space :width WIDTH :height HEIGHT. */
1592 extern Lisp_Object QCwidth, QCheight, QCascent, Qspace;
1593 extern Lisp_Object QCrelative_width, QCrelative_height;
1594 extern Lisp_Object QCalign_to;
1595 Lisp_Object prop, plist;
1596 double width = 0, height = 0, ascent = 0;
1597 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1598 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1599
1600 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1601
1602 /* List should start with `space'. */
1603 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1604 plist = XCDR (it->object);
1605
1606 /* Compute the width of the stretch. */
1607 if (prop = Fplist_get (plist, QCwidth),
1608 NUMVAL (prop) > 0)
1609 /* Absolute width `:width WIDTH' specified and valid. */
1610 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1611 else if (prop = Fplist_get (plist, QCrelative_width),
1612 NUMVAL (prop) > 0)
1613 {
1614 /* Relative width `:relative-width FACTOR' specified and valid.
1615 Compute the width of the characters having the `glyph'
1616 property. */
1617 struct it it2;
1618 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1619
1620 it2 = *it;
1621 if (it->multibyte_p)
1622 {
1623 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1624 - IT_BYTEPOS (*it));
1625 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1626 }
1627 else
1628 it2.c = *p, it2.len = 1;
1629
1630 it2.glyph_row = NULL;
1631 it2.what = IT_CHARACTER;
1632 x_produce_glyphs (&it2);
1633 width = NUMVAL (prop) * it2.pixel_width;
1634 }
1635 else if (prop = Fplist_get (plist, QCalign_to),
1636 NUMVAL (prop) > 0)
1637 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1638 else
1639 /* Nothing specified -> width defaults to canonical char width. */
1640 width = CANON_X_UNIT (it->f);
1641
1642 /* Compute height. */
1643 if (prop = Fplist_get (plist, QCheight),
1644 NUMVAL (prop) > 0)
1645 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1646 else if (prop = Fplist_get (plist, QCrelative_height),
1647 NUMVAL (prop) > 0)
1648 height = FONT_HEIGHT (font) * NUMVAL (prop);
1649 else
1650 height = FONT_HEIGHT (font);
1651
1652 /* Compute percentage of height used for ascent. If
1653 `:ascent ASCENT' is present and valid, use that. Otherwise,
1654 derive the ascent from the font in use. */
1655 if (prop = Fplist_get (plist, QCascent),
1656 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1657 ascent = NUMVAL (prop) / 100.0;
1658 else
1659 ascent = (double) font->ascent / FONT_HEIGHT (font);
1660
1661 if (width <= 0)
1662 width = 1;
1663 if (height <= 0)
1664 height = 1;
1665
1666 if (it->glyph_row)
1667 {
1668 Lisp_Object object = it->stack[it->sp - 1].string;
1669 if (!STRINGP (object))
1670 object = it->w->buffer;
1671 x_append_stretch_glyph (it, object, width, height, ascent);
1672 }
1673
1674 it->pixel_width = width;
1675 it->ascent = height * ascent;
1676 it->descent = height - it->ascent;
1677 it->nglyphs = 1;
1678
1679 if (face->box != FACE_NO_BOX)
1680 {
1681 it->ascent += face->box_line_width;
1682 it->descent += face->box_line_width;
1683
1684 if (it->start_of_box_run_p)
1685 it->pixel_width += face->box_line_width;
1686 if (it->end_of_box_run_p)
1687 it->pixel_width += face->box_line_width;
1688 }
1689
1690 take_vertical_position_into_account (it);
1691}
1692
1693
1694/* Produce glyphs/get display metrics for the display element IT is
1695 loaded with. See the description of struct display_iterator in
1696 dispextern.h for an overview of struct display_iterator. */
1697
1698static void
1699x_produce_glyphs (it)
1700 struct it *it;
1701{
1702 if (it->what == IT_CHARACTER)
1703 {
1704 XChar2b char2b;
1705 XFontStruct *font;
1706 struct face *face;
1707 XCharStruct *pcm;
1708 struct it ci;
1709 int font_not_found_p;
1710 int c;
1711
1712 /* Maybe translate single-byte characters to multibyte. */
1713 it->char_to_display = it->c;
1714 if (unibyte_display_via_language_environment
1715 && SINGLE_BYTE_CHAR_P (it->c)
1716 && (it->c >= 0240
1717 || (it->c >= 0200
1718 && !NILP (Vnonascii_translation_table))))
1719 {
1720 it->char_to_display = unibyte_char_to_multibyte (it->c);
1721 it->charset = CHAR_CHARSET (it->char_to_display);
1722 }
1723
1724 /* Get face and font to use. Encode IT->char_to_display. */
1725 face = x_get_char_face_and_encoding (it->f, it->char_to_display,
1726 it->face_id, &char2b,
1727 it->multibyte_p);
1728 font = face->font;
1729
1730 /* When no suitable font found, use the default font. */
1731 font_not_found_p = font == NULL;
1732 if (font_not_found_p)
1733 font = FRAME_FONT (it->f);
1734
1735 if (it->char_to_display >= ' '
1736 && (!it->multibyte_p || it->char_to_display < 128))
1737 {
1738 /* Either unibyte or ASCII. */
1739 int stretched_p;
1740
1741 it->nglyphs = 1;
1742 it->ascent = font->ascent;
1743 it->descent = font->descent;
1744
1745 pcm = x_per_char_metric (font, &char2b);
1746 it->pixel_width = pcm->width;
1747
1748 /* If this is a space inside a region of text with
1749 `space-width' property, change its width. */
1750 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1751 if (stretched_p)
1752 it->pixel_width *= XFLOATINT (it->space_width);
1753
1754 /* If face has a box, add the box thickness to the character
1755 height. If character has a box line to the left and/or
1756 right, add the box line width to the character's width. */
1757 if (face->box != FACE_NO_BOX)
1758 {
1759 int thick = face->box_line_width;
1760
1761 it->ascent += thick;
1762 it->descent += thick;
1763
1764 if (it->start_of_box_run_p)
1765 it->pixel_width += thick;
1766 if (it->end_of_box_run_p)
1767 it->pixel_width += thick;
1768 }
1769
1770 /* If face has an overline, add the height of the overline
1771 (1 pixel) and a 1 pixel margin to the character height. */
1772 if (face->overline_p)
1773 it->ascent += 2;
1774
1775 take_vertical_position_into_account (it);
1776
1777 /* If we have to actually produce glyphs, do it. */
1778 if (it->glyph_row)
1779 {
1780 if (stretched_p)
1781 {
1782 /* Translate a space with a `space-width' property
1783 into a stretch glyph. */
1784 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1785 x_append_stretch_glyph (it, it->object, it->pixel_width,
1786 it->ascent + it->descent, ascent);
1787 }
1788 else
1789 x_append_glyph (it);
1790
1791 /* If characters with lbearing or rbearing are displayed
1792 in this line, record that fact in a flag of the
1793 glyph row. This is used to optimize X output code. */
1794 if (pcm->lbearing < 0
1795 || pcm->rbearing > pcm->width)
1796 it->glyph_row->contains_overlapping_glyphs_p = 1;
1797 }
1798 }
1799 else if (it->char_to_display == '\n')
1800 {
1801 /* A newline has no width but we need the height of the line. */
1802 it->pixel_width = 0;
1803 it->nglyphs = 0;
1804 it->ascent = font->ascent;
1805 it->descent = font->descent;
1806
1807 if (face->box != FACE_NO_BOX)
1808 {
1809 int thick = face->box_line_width;
1810 it->ascent += thick;
1811 it->descent += thick;
1812 }
1813 }
1814 else if (it->char_to_display == '\t')
1815 {
1816 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1817 int x = (it->current_x
1818 - it->prompt_width
1819 + it->continuation_lines_width);
1820 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1821
1822 it->pixel_width = next_tab_x - x;
1823 it->nglyphs = 1;
1824 it->ascent = font->ascent;
1825 it->descent = font->descent;
1826
1827 if (it->glyph_row)
1828 {
1829 double ascent = (double) it->ascent / (it->ascent + it->descent);
1830 x_append_stretch_glyph (it, it->object, it->pixel_width,
1831 it->ascent + it->descent, ascent);
1832 }
1833 }
1834 else
1835 {
1836 /* A multi-byte character. Assume that the display width of the
1837 character is the width of the character multiplied by the
1838 width of the font. There has to be better support for
1839 variable sizes in cmpchar_info to do anything better than
1840 that.
1841
1842 Note: composite characters are represented as one glyph in
1843 the glyph matrix. There are no padding glyphs. */
1844 if (it->charset == CHARSET_COMPOSITION)
1845 {
1846 struct cmpchar_info *cmpcharp;
1847 int idx;
1848
1849 idx = COMPOSITE_CHAR_ID (it->char_to_display);
1850 cmpcharp = cmpchar_table[idx];
1851 it->pixel_width = font->max_bounds.width * cmpcharp->width;
1852
1853 /* There are no padding glyphs, so there is only one glyph
1854 to produce for the composite char. Important is that
1855 pixel_width, ascent and descent are the values of what is
1856 drawn by draw_glyphs. */
1857 it->nglyphs = 1;
1858
1859 /* These settings may not be correct. We must have more
1860 information in cmpcharp to do the correct setting. */
1861 it->ascent = font->ascent;
1862 it->descent = font->descent;
1863 }
1864 else
1865 {
1866 /* If we found a font, this font should give us the right
1867 metrics. If we didn't find a font, use the frame's
1868 default font and calculate the width of the character
1869 from the charset width; this is what old redisplay code
1870 did. */
1871 pcm = x_per_char_metric (font, &char2b);
1872 it->pixel_width = pcm->width;
1873 if (font_not_found_p)
1874 it->pixel_width *= CHARSET_WIDTH (it->charset);
1875 it->nglyphs = 1;
1876 it->ascent = font->ascent;
1877 it->descent = font->descent;
1878 if (it->glyph_row
1879 && (pcm->lbearing < 0
1880 || pcm->rbearing > pcm->width))
1881 it->glyph_row->contains_overlapping_glyphs_p = 1;
1882 }
1883
1884 if (face->box != FACE_NO_BOX)
1885 {
1886 int thick = face->box_line_width;
1887 it->ascent += thick;
1888 it->descent += thick;
1889
1890 if (it->start_of_box_run_p)
1891 it->pixel_width += thick;
1892 if (it->end_of_box_run_p)
1893 it->pixel_width += thick;
1894 }
1895
1896 /* If face has an overline, add the height of the overline
1897 (1 pixel) and a 1 pixel margin to the character height. */
1898 if (face->overline_p)
1899 it->ascent += 2;
1900
1901 take_vertical_position_into_account (it);
1902
1903 if (it->glyph_row)
1904 x_append_glyph (it);
1905 }
1906 }
1907 else if (it->what == IT_IMAGE)
1908 x_produce_image_glyph (it);
1909 else if (it->what == IT_STRETCH)
1910 x_produce_stretch_glyph (it);
1911
1912 /* Accumulate dimensions. */
1913 xassert (it->ascent >= 0 && it->descent > 0);
1914 if (it->area == TEXT_AREA)
1915 it->current_x += it->pixel_width;
1916 it->max_ascent = max (it->max_ascent, it->ascent);
1917 it->max_descent = max (it->max_descent, it->descent);
1918}
1919
1920
1921/* Estimate the pixel height of the mode or top line on frame F.
1922 FACE_ID specifies what line's height to estimate. */
1923
1924int
1925x_estimate_mode_line_height (f, face_id)
1926 struct frame *f;
1927 enum face_id face_id;
1928{
1929 int height = 1;
1930
1931 /* This function is called so early when Emacs starts that the face
1932 cache and mode line face are not yet initialized. */
1933 if (FRAME_FACE_CACHE (f))
1934 {
1935 struct face *face = FACE_FROM_ID (f, face_id);
1936 if (face)
1937 height = FONT_HEIGHT (face->font) + 2 * face->box_line_width;
1938 }
1939
1940 return height;
1941}
1942
1943\f
1944/***********************************************************************
1945 Glyph display
1946 ***********************************************************************/
1947
1948/* A sequence of glyphs to be drawn in the same face.
1949
1950 This data structure is not really completely X specific, so it
1951 could possibly, at least partially, be useful for other systems. It
1952 is currently not part of the external redisplay interface because
1953 it's not clear what other systems will need. */
1954
1955struct glyph_string
1956{
1957 /* X-origin of the string. */
1958 int x;
1959
1960 /* Y-origin and y-position of the base line of this string. */
1961 int y, ybase;
1962
1963 /* The width of the string, not including a face extension. */
1964 int width;
1965
1966 /* The width of the string, including a face extension. */
1967 int background_width;
1968
1969 /* The height of this string. This is the height of the line this
1970 string is drawn in, and can be different from the height of the
1971 font the string is drawn in. */
1972 int height;
1973
1974 /* Number of pixels this string overwrites in front of its x-origin.
1975 This number is zero if the string has an lbearing >= 0; it is
1976 -lbearing, if the string has an lbearing < 0. */
1977 int left_overhang;
1978
1979 /* Number of pixels this string overwrites past its right-most
1980 nominal x-position, i.e. x + width. Zero if the string's
1981 rbearing is <= its nominal width, rbearing - width otherwise. */
1982 int right_overhang;
1983
1984 /* The frame on which the glyph string is drawn. */
1985 struct frame *f;
1986
1987 /* The window on which the glyph string is drawn. */
1988 struct window *w;
1989
1990 /* X display and window for convenience. */
1991 Display *display;
1992 Window window;
1993
1994 /* The glyph row for which this string was built. It determines the
1995 y-origin and height of the string. */
1996 struct glyph_row *row;
1997
1998 /* The area within row. */
1999 enum glyph_row_area area;
2000
2001 /* Characters to be drawn, and number of characters. */
2002 XChar2b *char2b;
2003 int nchars;
2004
2005 /* Character set of this glyph string. */
2006 int charset;
2007
2008 /* A face-override for drawing cursors, mouse face and similar. */
2009 enum draw_glyphs_face hl;
2010
2011 /* Face in which this string is to be drawn. */
2012 struct face *face;
2013
2014 /* Font in which this string is to be drawn. */
2015 XFontStruct *font;
2016
2017 /* Font info for this string. */
2018 struct font_info *font_info;
2019
2020 /* Non-null means this string describes (part of) a composite
2021 character. All characters from char2b are drawn at the same
2022 x-origin in that case. */
2023 struct cmpchar_info *cmpcharp;
2024
2025 /* Index of this glyph string's first character in the glyph
2026 definition of cmpcharp. If this is zero, this glyph string
2027 describes the first character of a composite character. */
2028 int gidx;
2029
2030 /* 1 means this glyph strings face has to be drawn to the right end
2031 of the window's drawing area. */
2032 unsigned extends_to_end_of_line_p : 1;
2033
2034 /* 1 means the background of this string has been drawn. */
2035 unsigned background_filled_p : 1;
2036
2037 /* 1 means glyph string must be drawn with 16-bit functions. */
2038 unsigned two_byte_p : 1;
2039
2040 /* 1 means that the original font determined for drawing this glyph
2041 string could not be loaded. The member `font' has been set to
2042 the frame's default font in this case. */
2043 unsigned font_not_found_p : 1;
2044
2045 /* 1 means that the face in which this glyph string is drawn has a
2046 stipple pattern. */
2047 unsigned stippled_p : 1;
2048
2049 /* The GC to use for drawing this glyph string. */
2050 GC gc;
2051
2052 /* A pointer to the first glyph in the string. This glyph
2053 corresponds to char2b[0]. Needed to draw rectangles if
2054 font_not_found_p is 1. */
2055 struct glyph *first_glyph;
2056
2057 /* Image, if any. */
2058 struct image *img;
2059
2060 struct glyph_string *next, *prev;
2061};
2062
2063
2064#if GLYPH_DEBUG
2065
2066static void
2067x_dump_glyph_string (s)
2068 struct glyph_string *s;
2069{
2070 fprintf (stderr, "glyph string\n");
2071 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2072 s->x, s->y, s->width, s->height);
2073 fprintf (stderr, " ybase = %d\n", s->ybase);
2074 fprintf (stderr, " hl = %d\n", s->hl);
2075 fprintf (stderr, " left overhang = %d, right = %d\n",
2076 s->left_overhang, s->right_overhang);
2077 fprintf (stderr, " nchars = %d\n", s->nchars);
2078 fprintf (stderr, " extends to end of line = %d\n",
2079 s->extends_to_end_of_line_p);
2080 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2081 fprintf (stderr, " bg width = %d\n", s->background_width);
2082}
2083
2084#endif /* GLYPH_DEBUG */
2085
2086
2087
2088static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2089 struct glyph_string **,
2090 struct glyph_string *,
2091 struct glyph_string *));
2092static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2093 struct glyph_string **,
2094 struct glyph_string *,
2095 struct glyph_string *));
2096static void x_append_glyph_string P_ ((struct glyph_string **,
2097 struct glyph_string **,
2098 struct glyph_string *));
2099static int x_left_overwritten P_ ((struct glyph_string *));
2100static int x_left_overwriting P_ ((struct glyph_string *));
2101static int x_right_overwritten P_ ((struct glyph_string *));
2102static int x_right_overwriting P_ ((struct glyph_string *));
2103static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int));
2104static void x_init_glyph_string P_ ((struct glyph_string *,
2105 XChar2b *, struct window *,
2106 struct glyph_row *,
2107 enum glyph_row_area, int,
2108 enum draw_glyphs_face));
2109static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2110 enum glyph_row_area, int, int,
2111 enum draw_glyphs_face, int *, int *));
2112static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2113static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2114static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2115 int));
2116static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2117static void x_draw_glyph_string_underline P_ ((struct glyph_string *));
2118static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2119static void x_draw_glyph_string P_ ((struct glyph_string *));
2120static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2121static void x_set_cursor_gc P_ ((struct glyph_string *));
2122static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2123static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2124static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2125 int *, int *));
2126static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2127static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2128 unsigned long *, float, int));
2129static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2130 float, int, unsigned long));
2131static void x_setup_relief_colors P_ ((struct glyph_string *));
2132static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2133static void x_draw_image_relief P_ ((struct glyph_string *));
2134static void x_draw_image_foreground P_ ((struct glyph_string *));
2135static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2136static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2137static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2138 int, int, int));
2139static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2140 int, int, int, int, XRectangle *));
2141static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2142 int, int, int, XRectangle *));
2143
2144
2145
2146/* Append the list of glyph strings with head H and tail T to the list
2147 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2148
2149static INLINE void
2150x_append_glyph_string_lists (head, tail, h, t)
2151 struct glyph_string **head, **tail;
2152 struct glyph_string *h, *t;
2153{
2154 if (h)
2155 {
2156 if (*head)
2157 (*tail)->next = h;
2158 else
2159 *head = h;
2160 h->prev = *tail;
2161 *tail = t;
2162 }
2163}
2164
2165
2166/* Prepend the list of glyph strings with head H and tail T to the
2167 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2168 result. */
2169
2170static INLINE void
2171x_prepend_glyph_string_lists (head, tail, h, t)
2172 struct glyph_string **head, **tail;
2173 struct glyph_string *h, *t;
2174{
2175 if (h)
2176 {
2177 if (*head)
2178 (*head)->prev = t;
2179 else
2180 *tail = t;
2181 t->next = *head;
2182 *head = h;
2183 }
2184}
2185
2186
2187/* Append glyph string S to the list with head *HEAD and tail *TAIL.
2188 Set *HEAD and *TAIL to the resulting list. */
2189
2190static INLINE void
2191x_append_glyph_string (head, tail, s)
2192 struct glyph_string **head, **tail;
2193 struct glyph_string *s;
2194{
2195 s->next = s->prev = NULL;
2196 x_append_glyph_string_lists (head, tail, s, s);
2197}
2198
2199
2200/* Set S->gc to a suitable GC for drawing glyph string S in cursor
2201 face. */
2202
2203static void
2204x_set_cursor_gc (s)
2205 struct glyph_string *s;
2206{
2207 if (s->font == FRAME_FONT (s->f)
2208 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2209 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2210 && !s->cmpcharp)
2211 s->gc = s->f->output_data.x->cursor_gc;
2212 else
2213 {
2214 /* Cursor on non-default face: must merge. */
2215 XGCValues xgcv;
2216 unsigned long mask;
2217
2218 xgcv.background = s->f->output_data.x->cursor_pixel;
2219 xgcv.foreground = s->face->background;
2220
2221 /* If the glyph would be invisible, try a different foreground. */
2222 if (xgcv.foreground == xgcv.background)
2223 xgcv.foreground = s->face->foreground;
2224 if (xgcv.foreground == xgcv.background)
2225 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2226 if (xgcv.foreground == xgcv.background)
2227 xgcv.foreground = s->face->foreground;
2228
2229 /* Make sure the cursor is distinct from text in this face. */
2230 if (xgcv.background == s->face->background
2231 && xgcv.foreground == s->face->foreground)
2232 {
2233 xgcv.background = s->face->foreground;
2234 xgcv.foreground = s->face->background;
2235 }
2236
2237 IF_DEBUG (x_check_font (s->f, s->font));
2238 xgcv.font = s->font->fid;
2239 xgcv.graphics_exposures = False;
2240 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2241
2242 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2243 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2244 mask, &xgcv);
2245 else
2246 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2247 = XCreateGC (s->display, s->window, mask, &xgcv);
2248
2249 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2250 }
2251}
2252
2253
2254/* Set up S->gc of glyph string S for drawing text in mouse face. */
2255
2256static void
2257x_set_mouse_face_gc (s)
2258 struct glyph_string *s;
2259{
2260 int face_id;
2261
2262 /* What face has to be used for the mouse face? */
2263 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2264 face_id = FACE_FOR_CHARSET (s->f, face_id, s->charset);
2265 s->face = FACE_FROM_ID (s->f, face_id);
2266 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2267
2268 /* If font in this face is same as S->font, use it. */
2269 if (s->font == s->face->font)
2270 s->gc = s->face->gc;
2271 else
2272 {
2273 /* Otherwise construct scratch_cursor_gc with values from FACE
2274 but font FONT. */
2275 XGCValues xgcv;
2276 unsigned long mask;
2277
2278 xgcv.background = s->face->background;
2279 xgcv.foreground = s->face->foreground;
2280 IF_DEBUG (x_check_font (s->f, s->font));
2281 xgcv.font = s->font->fid;
2282 xgcv.graphics_exposures = False;
2283 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2284
2285 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2286 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2287 mask, &xgcv);
2288 else
2289 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2290 = XCreateGC (s->display, s->window, mask, &xgcv);
2291
2292 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2293 }
2294
2295 xassert (s->gc != 0);
2296}
2297
2298
2299/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2300 Faces to use in the mode line have already been computed when the
2301 matrix was built, so there isn't much to do, here. */
2302
2303static INLINE void
2304x_set_mode_line_face_gc (s)
2305 struct glyph_string *s;
2306{
2307 s->gc = s->face->gc;
2308 xassert (s->gc != 0);
2309}
2310
2311
2312/* Set S->gc of glyph string S for drawing that glyph string. Set
2313 S->stippled_p to a non-zero value if the face of S has a stipple
2314 pattern. */
2315
2316static INLINE void
2317x_set_glyph_string_gc (s)
2318 struct glyph_string *s;
2319{
2320 if (s->hl == DRAW_NORMAL_TEXT)
2321 {
2322 s->gc = s->face->gc;
2323 s->stippled_p = s->face->stipple != 0;
2324 }
2325 else if (s->hl == DRAW_INVERSE_VIDEO)
2326 {
2327 x_set_mode_line_face_gc (s);
2328 s->stippled_p = s->face->stipple != 0;
2329 }
2330 else if (s->hl == DRAW_CURSOR)
2331 {
2332 x_set_cursor_gc (s);
2333 s->stippled_p = 0;
2334 }
2335 else if (s->hl == DRAW_MOUSE_FACE)
2336 {
2337 x_set_mouse_face_gc (s);
2338 s->stippled_p = s->face->stipple != 0;
2339 }
2340 else if (s->hl == DRAW_IMAGE_RAISED
2341 || s->hl == DRAW_IMAGE_SUNKEN)
2342 {
2343 s->gc = s->face->gc;
2344 s->stippled_p = s->face->stipple != 0;
2345 }
2346 else
2347 {
2348 s->gc = s->face->gc;
2349 s->stippled_p = s->face->stipple != 0;
2350 }
2351
2352 /* GC must have been set. */
2353 xassert (s->gc != 0);
2354}
2355
2356
2357/* Return in *R the clipping rectangle for glyph string S. */
2358
2359static void
2360x_get_glyph_string_clip_rect (s, r)
2361 struct glyph_string *s;
2362 XRectangle *r;
2363{
2364 if (s->row->full_width_p)
2365 {
2366 /* Draw full-width. X coordinates are relative to S->w->left. */
2367 r->x = WINDOW_LEFT_MARGIN (s->w) * CANON_X_UNIT (s->f);
2368 r->width = XFASTINT (s->w->width) * CANON_X_UNIT (s->f);
2369
2370 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2371 {
2372 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
2373 r->width += width;
2374 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2375 r->x -= width;
2376 }
2377
2378 /* Unless displaying a mode or menu bar line, which are always
2379 fully visible, clip to the visible part of the row. */
2380 if (s->w->pseudo_window_p)
2381 r->height = s->row->visible_height;
2382 else
2383 r->height = s->height;
2384 }
2385 else
2386 {
2387 /* This is a text line that may be partially visible. */
2388 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2389 r->width = window_box_width (s->w, s->area);
2390 r->height = s->row->visible_height;
2391 }
2392
2393 /* Don't use S->y for clipping because it doesn't take partially
2394 visible lines into account. For example, it can be negative for
2395 partially visible lines at the top of a window. */
2396 if (!s->row->full_width_p
2397 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2398 r->y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (s->w);
2399 else
2400 r->y = max (0, s->row->y);
2401 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2402
2403 /* If drawing a toolbar window, draw it over the internal border
2404 at the top of the window. */
2405 if (s->w == XWINDOW (s->f->toolbar_window))
2406 r->y -= s->f->output_data.x->internal_border_width;
2407}
2408
2409
2410/* Set clipping for output of glyph string S. S may be part of a mode
2411 line or menu if we don't have X toolkit support. */
2412
2413static INLINE void
2414x_set_glyph_string_clipping (s)
2415 struct glyph_string *s;
2416{
2417 XRectangle r;
2418 x_get_glyph_string_clip_rect (s, &r);
2419 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2420}
2421
2422
2423/* Compute left and right overhang of glyph string S. If S is a glyph
2424 string for a composite character, assume overhangs don't exist. */
2425
2426static INLINE void
2427x_compute_glyph_string_overhangs (s)
2428 struct glyph_string *s;
2429{
2430 if (s->cmpcharp == NULL
2431 && s->first_glyph->type == CHAR_GLYPH)
2432 {
2433 XCharStruct cs;
2434 int direction, font_ascent, font_descent;
2435 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2436 &font_ascent, &font_descent, &cs);
2437 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2438 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2439 }
2440}
2441
2442
2443/* Compute overhangs and x-positions for glyph string S and its
2444 predecessors, or successors. X is the starting x-position for S.
2445 BACKWARD_P non-zero means process predecessors. */
2446
2447static void
2448x_compute_overhangs_and_x (s, x, backward_p)
2449 struct glyph_string *s;
2450 int x;
2451 int backward_p;
2452{
2453 if (backward_p)
2454 {
2455 while (s)
2456 {
2457 x_compute_glyph_string_overhangs (s);
2458 x -= s->width;
2459 s->x = x;
2460 s = s->prev;
2461 }
2462 }
2463 else
2464 {
2465 while (s)
2466 {
2467 x_compute_glyph_string_overhangs (s);
2468 s->x = x;
2469 x += s->width;
2470 s = s->next;
2471 }
2472 }
2473}
2474
2475
2476/* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2477 frame F. Overhangs of glyphs other than type CHAR_GLYPH or of
2478 character glyphs for composite characters are assumed to be zero. */
2479
2480static void
2481x_get_glyph_overhangs (glyph, f, left, right)
2482 struct glyph *glyph;
2483 struct frame *f;
2484 int *left, *right;
2485{
2486 int c;
2487
2488 *left = *right = 0;
2489
2490 if (glyph->type == CHAR_GLYPH
2491 && (c = glyph->u.ch.code,
2492 CHAR_CHARSET (c) != CHARSET_COMPOSITION))
2493 {
2494 XFontStruct *font;
2495 struct face *face;
2496 struct font_info *font_info;
2497 XChar2b char2b;
2498
2499 face = x_get_glyph_face_and_encoding (f, glyph, &char2b);
2500 font = face->font;
2501 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2502 if (font)
2503 {
2504 XCharStruct *pcm = x_per_char_metric (font, &char2b);
2505
2506 if (pcm->rbearing > pcm->width)
2507 *right = pcm->rbearing - pcm->width;
2508 if (pcm->lbearing < 0)
2509 *left = -pcm->lbearing;
2510 }
2511 }
2512}
2513
2514
2515/* Return the index of the first glyph preceding glyph string S that
2516 is overwritten by S because of S's left overhang. Value is -1
2517 if no glyphs are overwritten. */
2518
2519static int
2520x_left_overwritten (s)
2521 struct glyph_string *s;
2522{
2523 int k;
2524
2525 if (s->left_overhang)
2526 {
2527 int x = 0, i;
2528 struct glyph *glyphs = s->row->glyphs[s->area];
2529 int first = s->first_glyph - glyphs;
2530
2531 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2532 x -= glyphs[i].pixel_width;
2533
2534 k = i + 1;
2535 }
2536 else
2537 k = -1;
2538
2539 return k;
2540}
2541
2542
2543/* Return the index of the first glyph preceding glyph string S that
2544 is overwriting S because of its right overhang. Value is -1 if no
2545 glyph in front of S overwrites S. */
2546
2547static int
2548x_left_overwriting (s)
2549 struct glyph_string *s;
2550{
2551 int i, k, x;
2552 struct glyph *glyphs = s->row->glyphs[s->area];
2553 int first = s->first_glyph - glyphs;
2554
2555 k = -1;
2556 x = 0;
2557 for (i = first - 1; i >= 0; --i)
2558 {
2559 int left, right;
2560 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2561 if (x + right > 0)
2562 k = i;
2563 x -= glyphs[i].pixel_width;
2564 }
2565
2566 return k;
2567}
2568
2569
2570/* Return the index of the last glyph following glyph string S that is
2571 not overwritten by S because of S's right overhang. Value is -1 if
2572 no such glyph is found. */
2573
2574static int
2575x_right_overwritten (s)
2576 struct glyph_string *s;
2577{
2578 int k = -1;
2579
2580 if (s->right_overhang)
2581 {
2582 int x = 0, i;
2583 struct glyph *glyphs = s->row->glyphs[s->area];
2584 int first = (s->first_glyph - glyphs) + (s->cmpcharp ? 1 : s->nchars);
2585 int end = s->row->used[s->area];
2586
2587 for (i = first; i < end && s->right_overhang > x; ++i)
2588 x += glyphs[i].pixel_width;
2589
2590 k = i;
2591 }
2592
2593 return k;
2594}
2595
2596
2597/* Return the index of the last glyph following glyph string S that
2598 overwrites S because of its left overhang. Value is negative
2599 if no such glyph is found. */
2600
2601static int
2602x_right_overwriting (s)
2603 struct glyph_string *s;
2604{
2605 int i, k, x;
2606 int end = s->row->used[s->area];
2607 struct glyph *glyphs = s->row->glyphs[s->area];
2608 int first = (s->first_glyph - glyphs) + (s->cmpcharp ? 1 : s->nchars);
2609
2610 k = -1;
2611 x = 0;
2612 for (i = first; i < end; ++i)
2613 {
2614 int left, right;
2615 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2616 if (x - left < 0)
2617 k = i;
2618 x += glyphs[i].pixel_width;
2619 }
2620
2621 return k;
2622}
2623
2624
2625/* Fill rectangle X, Y, W, H with background color of glyph string S. */
2626
2627static INLINE void
2628x_clear_glyph_string_rect (s, x, y, w, h)
2629 struct glyph_string *s;
2630 int x, y, w, h;
2631{
2632 XGCValues xgcv;
2633 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
2634 XSetForeground (s->display, s->gc, xgcv.background);
2635 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2636 XSetForeground (s->display, s->gc, xgcv.foreground);
2637}
2638
2639
2640/* Draw the background of glyph_string S. If S->background_filled_p
2641 is non-zero don't draw it. FORCE_P non-zero means draw the
2642 background even if it wouldn't be drawn normally. This is used
2643 when a string preceding S draws into the background of S. */
2644
2645static void
2646x_draw_glyph_string_background (s, force_p)
2647 struct glyph_string *s;
2648 int force_p;
2649{
2650 /* Nothing to do if background has already been drawn or if it
2651 shouldn't be drawn in the first place. */
2652 if (!s->background_filled_p)
2653 {
2654 if (s->cmpcharp
2655 && s->gidx > 0
2656 && !s->font_not_found_p
2657 && !s->extends_to_end_of_line_p)
2658 {
2659 /* Don't draw background for glyphs of a composite
2660 characters, except for the first one. */
2661 s->background_filled_p = 1;
2662 }
2663 else if (s->stippled_p)
2664 {
2665 /* Fill background with a stipple pattern. */
2666 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2667 XFillRectangle (s->display, s->window, s->gc, s->x,
2668 s->y + s->face->box_line_width,
2669 s->background_width,
2670 s->height - 2 * s->face->box_line_width);
2671 XSetFillStyle (s->display, s->gc, FillSolid);
2672 s->background_filled_p = 1;
2673 }
2674 else if (FONT_HEIGHT (s->font) < s->height - 2 * s->face->box_line_width
2675 || s->font_not_found_p
2676 || s->extends_to_end_of_line_p
2677 || s->cmpcharp
2678 || force_p)
2679 {
2680 x_clear_glyph_string_rect (s, s->x, s->y + s->face->box_line_width,
2681 s->background_width,
2682 s->height - 2 * s->face->box_line_width);
2683 s->background_filled_p = 1;
2684 }
2685 }
2686}
2687
2688
2689/* Draw the foreground of glyph string S. */
2690
2691static void
2692x_draw_glyph_string_foreground (s)
2693 struct glyph_string *s;
2694{
2695 int i, x;
2696
2697 /* If first glyph of S has a left box line, start drawing the text
2698 of S to the right of that box line. */
2699 if (s->face->box != FACE_NO_BOX
2700 && s->first_glyph->left_box_line_p)
2701 x = s->x + s->face->box_line_width;
2702 else
2703 x = s->x;
2704
2705 if (s->cmpcharp == NULL)
2706 {
2707 /* Not a composite character. Draw characters of S as
2708 rectangles if S's font could not be loaded. */
2709 if (s->font_not_found_p)
2710 {
2711 for (i = 0; i < s->nchars; ++i)
2712 {
2713 struct glyph *g = s->first_glyph + i;
2714 XDrawRectangle (s->display, s->window,
2715 s->gc, x, s->y, g->pixel_width - 1,
2716 s->height - 1);
2717 x += g->pixel_width;
2718 }
2719 }
2720 else
2721 {
2722 char *char1b = (char *) s->char2b;
2723
2724 /* If we can use 8-bit functions, condense S->char2b. */
2725 if (!s->two_byte_p)
2726 for (i = 0; i < s->nchars; ++i)
2727 char1b[i] = s->char2b[i].byte2;
2728
2729 /* Draw text with XDrawString if background has already been
2730 filled. Otherwise, use XDrawImageString. (Note that
2731 XDrawImageString is usually faster than XDrawString.)
2732 Always use XDrawImageString when drawing the cursor so
2733 that there is no chance that characters under a box
2734 cursor are invisible. */
2735 if (s->background_filled_p && s->hl != DRAW_CURSOR)
2736 {
2737 /* Draw characters with 16-bit or 8-bit functions. */
2738 if (s->two_byte_p)
2739 XDrawString16 (s->display, s->window, s->gc, x, s->ybase,
2740 s->char2b, s->nchars);
2741 else
2742 XDrawString (s->display, s->window, s->gc, x, s->ybase,
2743 char1b, s->nchars);
2744 }
2745 else
2746 {
2747 if (s->two_byte_p)
2748 XDrawImageString16 (s->display, s->window, s->gc,
2749 x, s->ybase, s->char2b, s->nchars);
2750 else
2751 XDrawImageString (s->display, s->window, s->gc,
2752 x, s->ybase, char1b, s->nchars);
2753 }
2754 }
2755 }
2756 else
2757 {
2758 /* S is a glyph string for a composite character. S->gidx is the
2759 index of the first character drawn in the vector
2760 S->cmpcharp->glyph. S->gidx == 0 means we are drawing the
2761 very first component character of a composite char. */
2762
2763 /* Draw a single rectangle for the composite character if S's
2764 font could not be loaded. */
2765 if (s->font_not_found_p && s->gidx == 0)
2766 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
2767 s->width - 1, s->height - 1);
2768 else
2769 {
2770 XCharStruct *pcm;
2771 int relative_compose, default_ascent, i;
2772 int highest = 0, lowest = 0;
2773
2774 /* The value of font_info my be null if we couldn't find it
2775 in x_get_char_face_and_encoding. */
2776 if (s->cmpcharp->cmp_rule == NULL && s->font_info)
2777 {
2778 relative_compose = s->font_info->relative_compose;
2779 default_ascent = s->font_info->default_ascent;
2780 }
2781 else
2782 relative_compose = default_ascent = 0;
2783
2784 if ((s->cmpcharp->cmp_rule || relative_compose)
2785 && s->gidx == 0)
2786 {
2787 /* This is the first character. Initialize variables.
2788 Highest is the highest position of glyphs ever
2789 written, lowest the lowest position. */
2790 int x_offset = 0;
2791 int first_ch = s->first_glyph->u.ch.code;
2792
2793 if (default_ascent
2794 && CHAR_TABLE_P (Vuse_default_ascent)
2795 && !NILP (Faref (Vuse_default_ascent, first_ch)))
2796 {
2797 highest = default_ascent;
2798 lowest = 0;
2799 }
2800 else
2801 {
2802 pcm = PER_CHAR_METRIC (s->font, s->char2b);
2803 highest = pcm->ascent + 1;
2804 lowest = - pcm->descent;
2805 }
2806
2807 if (s->cmpcharp->cmp_rule)
2808 x_offset = (s->cmpcharp->col_offset[0]
2809 * FONT_WIDTH (s->f->output_data.x->font));
2810
2811 /* Draw the first character at the normal position. */
2812 XDrawString16 (s->display, s->window, s->gc,
2813 x + x_offset,
2814 s->ybase, s->char2b, 1);
2815 i = 1;
2816 ++s->gidx;
2817 }
2818 else
2819 i = 0;
2820
2821 for (; i < s->nchars; i++, ++s->gidx)
2822 {
2823 int x_offset = 0, y_offset = 0;
2824
2825 if (relative_compose)
2826 {
2827 pcm = PER_CHAR_METRIC (s->font, s->char2b + i);
2828 if (NILP (Vignore_relative_composition)
2829 || NILP (Faref (Vignore_relative_composition,
2830 make_number (s->cmpcharp->glyph[s->gidx]))))
2831 {
2832 if (- pcm->descent >= relative_compose)
2833 {
2834 /* Draw above the current glyphs. */
2835 y_offset = highest + pcm->descent;
2836 highest += pcm->ascent + pcm->descent;
2837 }
2838 else if (pcm->ascent <= 0)
2839 {
2840 /* Draw beneath the current glyphs. */
2841 y_offset = lowest - pcm->ascent;
2842 lowest -= pcm->ascent + pcm->descent;
2843 }
2844 }
2845 else
2846 {
2847 /* Draw the glyph at normal position. If
2848 it sticks out of HIGHEST or LOWEST,
2849 update them appropriately. */
2850 if (pcm->ascent > highest)
2851 highest = pcm->ascent;
2852 else if (- pcm->descent < lowest)
2853 lowest = - pcm->descent;
2854 }
2855 }
2856 else if (s->cmpcharp->cmp_rule)
2857 {
2858 int gref = (s->cmpcharp->cmp_rule[s->gidx] - 0xA0) / 9;
2859 int nref = (s->cmpcharp->cmp_rule[s->gidx] - 0xA0) % 9;
2860 int bottom, top;
2861
2862 /* Re-encode GREF and NREF so that they specify
2863 only Y-axis information:
2864 0:top, 1:base, 2:bottom, 3:center */
2865 gref = gref / 3 + (gref == 4) * 2;
2866 nref = nref / 3 + (nref == 4) * 2;
2867
2868 pcm = PER_CHAR_METRIC (s->font, s->char2b + i);
2869 bottom = ((gref == 0 ? highest : gref == 1 ? 0
2870 : gref == 2 ? lowest
2871 : (highest + lowest) / 2)
2872 - (nref == 0 ? pcm->ascent + pcm->descent
2873 : nref == 1 ? pcm->descent : nref == 2 ? 0
2874 : (pcm->ascent + pcm->descent) / 2));
2875 top = bottom + (pcm->ascent + pcm->descent);
2876 if (top > highest)
2877 highest = top;
2878 if (bottom < lowest)
2879 lowest = bottom;
2880 y_offset = bottom + pcm->descent;
2881 x_offset = (s->cmpcharp->col_offset[s->gidx]
2882 * FONT_WIDTH (FRAME_FONT (s->f)));
2883 }
2884
2885 XDrawString16 (s->display, s->window, s->gc,
2886 x + x_offset, s->ybase - y_offset,
2887 s->char2b + i, 1);
2888 }
2889 }
2890 }
2891}
2892
2893
2894/* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
2895 CMAP. If an exact match can't be allocated, try the nearest color
2896 available. Value is non-zero if successful. Set *COLOR to the
2897 color allocated. */
2898
2899int
2900x_alloc_nearest_color (display, screen, cmap, color)
2901 Display *display;
2902 Screen *screen;
2903 Colormap cmap;
2904 XColor *color;
2905{
2906 int rc = XAllocColor (display, cmap, color);
2907 if (rc == 0)
2908 {
2909 /* If we got to this point, the colormap is full, so we're going
2910 to try to get the next closest color. The algorithm used is
2911 a least-squares matching, which is what X uses for closest
2912 color matching with StaticColor visuals. */
2913 int nearest, i;
2914 unsigned long nearest_delta = ~0;
2915 int ncells = XDisplayCells (display, XScreenNumberOfScreen (screen));
2916 XColor *cells = (XColor *) alloca (ncells * sizeof *cells);
2917
2918 for (i = 0; i < ncells; ++i)
2919 cells[i].pixel = i;
2920 XQueryColors (display, cmap, cells, ncells);
2921
2922 for (nearest = i = 0; i < ncells; ++i)
2923 {
2924 long dred = (color->red >> 8) - (cells[i].red >> 8);
2925 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
2926 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
2927 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
2928
2929 if (delta < nearest_delta)
2930 {
2931 nearest = i;
2932 nearest_delta = delta;
2933 }
2934 }
2935
2936 color->red = cells[nearest].red;
2937 color->green = cells[nearest].green;
2938 color->blue = cells[nearest].blue;
2939 rc = XAllocColor (display, cmap, color);
2940 }
2941
2942 return rc;
2943}
2944
2945
2946/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
2947 or DELTA. Try a color with RGB values multiplied by FACTOR first.
2948 If this produces the same color as PIXEL, try a color where all RGB
2949 values have DELTA added. Return the allocated color in *PIXEL.
2950 DISPLAY is the X display, CMAP is the colormap to operate on.
2951 Value is non-zero if successful. */
2952
2953static int
2954x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
2955 struct frame *f;
2956 Display *display;
2957 Colormap cmap;
2958 unsigned long *pixel;
2959 float factor;
2960 int delta;
2961{
2962 XColor color, new;
2963 int success_p;
2964
2965 /* Get RGB color values. */
2966 color.pixel = *pixel;
2967 XQueryColor (display, cmap, &color);
2968
2969 /* Change RGB values by specified FACTOR. Avoid overflow! */
2970 xassert (factor >= 0);
2971 new.red = min (0xffff, factor * color.red);
2972 new.green = min (0xffff, factor * color.green);
2973 new.blue = min (0xffff, factor * color.blue);
2974
2975 /* Try to allocate the color. */
2976 success_p = x_alloc_nearest_color (display, FRAME_X_SCREEN (f), cmap, &new);
2977 if (success_p)
2978 {
2979 if (new.pixel == *pixel)
2980 {
2981 /* If we end up with the same color as before, try adding
2982 delta to the RGB values. */
2983 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
2984
2985 /* If display has an immutable color map, freeing colors is
2986 not necessary and some servers don't allow it. So don't
2987 do it. */
2988 if (class != StaticColor
2989 && class != StaticGray
2990 && class != TrueColor)
2991 XFreeColors (display, cmap, &new.pixel, 1, 0);
2992
2993 new.red = min (0xffff, delta + color.red);
2994 new.green = min (0xffff, delta + color.green);
2995 new.blue = min (0xffff, delta + color.blue);
2996 success_p = x_alloc_nearest_color (display, FRAME_X_SCREEN (f),
2997 cmap, &new);
2998 }
2999 else
3000 success_p = 1;
3001 *pixel = new.pixel;
3002 }
3003
3004 return success_p;
3005}
3006
3007
3008/* Set up the foreground color for drawing relief lines of glyph
3009 string S. RELIEF is a pointer to a struct relief containing the GC
3010 with which lines will be drawn. Use a color that is FACTOR or
3011 DELTA lighter or darker than the relief's background which is found
3012 in S->f->output_data.x->relief_background. If such a color cannot
3013 be allocated, use DEFAULT_PIXEL, instead. */
3014
3015static void
3016x_setup_relief_color (f, relief, factor, delta, default_pixel)
3017 struct frame *f;
3018 struct relief *relief;
3019 float factor;
3020 int delta;
3021 unsigned long default_pixel;
3022{
3023 XGCValues xgcv;
3024 struct x_output *di = f->output_data.x;
3025 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3026 unsigned long pixel;
3027 unsigned long background = di->relief_background;
3028 Colormap cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
3029
3030 xgcv.graphics_exposures = False;
3031 xgcv.line_width = 1;
3032
3033 /* Free previously allocated color. The color cell will be reused
3034 when it has been freed as many times as it was allocated, so this
3035 doesn't affect faces using the same colors. */
3036 if (relief->gc
3037 && relief->allocated_p)
3038 {
3039 /* If display has an immutable color map, freeing colors is not
3040 necessary and some servers don't allow it. So don't do it. */
3041 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
3042 if (class != StaticColor
3043 && class != StaticGray
3044 && class != TrueColor)
3045 XFreeColors (FRAME_X_DISPLAY (f), cmap, &relief->pixel, 1, 0);
3046 relief->allocated_p = 0;
3047 }
3048
3049 /* Allocate new color. */
3050 xgcv.foreground = default_pixel;
3051 pixel = background;
3052 if (x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), cmap, &pixel,
3053 factor, delta))
3054 {
3055 relief->allocated_p = 1;
3056 xgcv.foreground = relief->pixel = pixel;
3057 }
3058
3059 if (relief->gc == 0)
3060 {
3061 xgcv.stipple = FRAME_X_DISPLAY_INFO (f)->gray;
3062 mask |= GCStipple;
3063 relief->gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3064 mask, &xgcv);
3065 }
3066 else
3067 XChangeGC (FRAME_X_DISPLAY (f), relief->gc, mask, &xgcv);
3068}
3069
3070
3071/* Set up colors for the relief lines around glyph string S. */
3072
3073static void
3074x_setup_relief_colors (s)
3075 struct glyph_string *s;
3076{
3077 struct x_output *di = s->f->output_data.x;
3078 unsigned long color;
3079
3080 if (s->face->use_box_color_for_shadows_p)
3081 color = s->face->box_color;
3082 else
3083 {
3084 XGCValues xgcv;
3085
3086 /* Get the background color of the face. */
3087 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3088 color = xgcv.background;
3089 }
3090
3091 if (di->white_relief.gc == 0
3092 || color != di->relief_background)
3093 {
3094 di->relief_background = color;
3095 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3096 WHITE_PIX_DEFAULT (s->f));
3097 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3098 BLACK_PIX_DEFAULT (s->f));
3099 }
3100}
3101
3102
3103/* Draw a relief on frame F inside the rectangle given by LEFT_X,
3104 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3105 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3106 relief. LEFT_P non-zero means draw a relief on the left side of
3107 the rectangle. RIGHT_P non-zero means draw a relief on the right
3108 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3109 when drawing. */
3110
3111static void
3112x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3113 raised_p, left_p, right_p, clip_rect)
3114 struct frame *f;
3115 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3116 XRectangle *clip_rect;
3117{
3118 int i;
3119 GC gc;
3120
3121 if (raised_p)
3122 gc = f->output_data.x->white_relief.gc;
3123 else
3124 gc = f->output_data.x->black_relief.gc;
3125 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3126
3127 /* Top. */
3128 for (i = 0; i < width; ++i)
3129 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3130 left_x + i * left_p, top_y + i,
3131 right_x + 1 - i * right_p, top_y + i);
3132
3133 /* Left. */
3134 if (left_p)
3135 for (i = 0; i < width; ++i)
3136 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3137 left_x + i, top_y + i, left_x + i, bottom_y - i);
3138
3139 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3140 if (raised_p)
3141 gc = f->output_data.x->black_relief.gc;
3142 else
3143 gc = f->output_data.x->white_relief.gc;
3144 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3145
3146 /* Bottom. */
3147 for (i = 0; i < width; ++i)
3148 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3149 left_x + i * left_p, bottom_y - i,
3150 right_x + 1 - i * right_p, bottom_y - i);
3151
3152 /* Right. */
3153 if (right_p)
3154 for (i = 0; i < width; ++i)
3155 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3156 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3157
3158 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3159}
3160
3161
3162/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3163 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3164 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3165 left side of the rectangle. RIGHT_P non-zero means draw a line
3166 on the right side of the rectangle. CLIP_RECT is the clipping
3167 rectangle to use when drawing. */
3168
3169static void
3170x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3171 left_p, right_p, clip_rect)
3172 struct glyph_string *s;
3173 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3174 XRectangle *clip_rect;
3175{
3176 XGCValues xgcv;
3177
3178 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3179 XSetForeground (s->display, s->gc, s->face->box_color);
3180 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3181
3182 /* Top. */
3183 XFillRectangle (s->display, s->window, s->gc,
3184 left_x, top_y, right_x - left_x, width);
3185
3186 /* Left. */
3187 if (left_p)
3188 XFillRectangle (s->display, s->window, s->gc,
3189 left_x, top_y, width, bottom_y - top_y);
3190
3191 /* Bottom. */
3192 XFillRectangle (s->display, s->window, s->gc,
3193 left_x, bottom_y - width, right_x - left_x, width);
3194
3195 /* Right. */
3196 if (right_p)
3197 XFillRectangle (s->display, s->window, s->gc,
3198 right_x - width, top_y, width, bottom_y - top_y);
3199
3200 XSetForeground (s->display, s->gc, xgcv.foreground);
3201 XSetClipMask (s->display, s->gc, None);
3202}
3203
3204
3205/* Draw a box around glyph string S. */
3206
3207static void
3208x_draw_glyph_string_box (s)
3209 struct glyph_string *s;
3210{
3211 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3212 int left_p, right_p;
3213 struct glyph *last_glyph;
3214 XRectangle clip_rect;
3215
3216 last_x = window_box_right (s->w, s->area);
3217 if (s->row->full_width_p
3218 && !s->w->pseudo_window_p)
3219 {
3220 last_x += FRAME_X_FLAGS_AREA_WIDTH (s->f);
3221 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3222 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3223 }
3224
3225 /* The glyph that may have a right box line. */
3226 last_glyph = (s->cmpcharp || s->img
3227 ? s->first_glyph
3228 : s->first_glyph + s->nchars - 1);
3229
3230 width = s->face->box_line_width;
3231 raised_p = s->face->box == FACE_RAISED_BOX;
3232 left_x = s->x;
3233 right_x = ((s->row->full_width_p
3234 ? last_x
3235 : min (last_x, s->x + s->width) - 1));
3236 top_y = s->y;
3237 bottom_y = top_y + s->height - 1;
3238
3239 left_p = (s->first_glyph->left_box_line_p
3240 || (s->hl == DRAW_MOUSE_FACE
3241 && (s->prev == NULL
3242 || s->prev->hl != s->hl)));
3243 right_p = (last_glyph->right_box_line_p
3244 || (s->hl == DRAW_MOUSE_FACE
3245 && (s->next == NULL
3246 || s->next->hl != s->hl)));
3247
3248 x_get_glyph_string_clip_rect (s, &clip_rect);
3249
3250 if (s->face->box == FACE_SIMPLE_BOX)
3251 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3252 left_p, right_p, &clip_rect);
3253 else
3254 {
3255 x_setup_relief_colors (s);
3256 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3257 width, raised_p, left_p, right_p, &clip_rect);
3258 }
3259}
3260
3261
3262/* Draw foreground of image glyph string S. */
3263
3264static void
3265x_draw_image_foreground (s)
3266 struct glyph_string *s;
3267{
3268 int x;
3269 int y = s->ybase - IMAGE_ASCENT (s->img);
3270
3271 /* If first glyph of S has a left box line, start drawing it to the
3272 right of that line. */
3273 if (s->face->box != FACE_NO_BOX
3274 && s->first_glyph->left_box_line_p)
3275 x = s->x + s->face->box_line_width;
3276 else
3277 x = s->x;
3278
3279 /* If there is a margin around the image, adjust x- and y-position
3280 by that margin. */
3281 if (s->img->margin)
3282 {
3283 x += s->img->margin;
3284 y += s->img->margin;
3285 }
3286
3287 if (s->img->pixmap)
3288 {
3289 if (s->img->mask)
3290 {
3291 /* We can't set both a clip mask and use XSetClipRectangles
3292 because the latter also sets a clip mask. We also can't
3293 trust on the shape extension to be available
3294 (XShapeCombineRegion). So, compute the rectangle to draw
3295 manually. */
3296 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3297 | GCFunction);
3298 XGCValues xgcv;
3299 XRectangle clip_rect, image_rect, r;
3300
3301 xgcv.clip_mask = s->img->mask;
3302 xgcv.clip_x_origin = x;
3303 xgcv.clip_y_origin = y;
3304 xgcv.function = GXcopy;
3305 XChangeGC (s->display, s->gc, mask, &xgcv);
3306
3307 x_get_glyph_string_clip_rect (s, &clip_rect);
3308 image_rect.x = x;
3309 image_rect.y = y;
3310 image_rect.width = s->img->width;
3311 image_rect.height = s->img->height;
3312 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3313 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3314 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3315 }
3316 else
3317 {
3318 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3319 0, 0, s->img->width, s->img->height, x, y);
3320
3321 /* When the image has a mask, we can expect that at
3322 least part of a mouse highlight or a block cursor will
3323 be visible. If the image doesn't have a mask, make
3324 a block cursor visible by drawing a rectangle around
3325 the image. I believe it's looking better if we do
3326 nothing here for mouse-face. */
3327 if (s->hl == DRAW_CURSOR)
3328 XDrawRectangle (s->display, s->window, s->gc, x, y,
3329 s->img->width - 1, s->img->height - 1);
3330 }
3331 }
3332 else
3333 /* Draw a rectangle if image could not be loaded. */
3334 XDrawRectangle (s->display, s->window, s->gc, x, y,
3335 s->img->width - 1, s->img->height - 1);
3336}
3337
3338
3339/* Draw a relief around the image glyph string S. */
3340
3341static void
3342x_draw_image_relief (s)
3343 struct glyph_string *s;
3344{
3345 int x0, y0, x1, y1, thick, raised_p;
3346 XRectangle r;
3347 int x;
3348 int y = s->ybase - IMAGE_ASCENT (s->img);
3349
3350 /* If first glyph of S has a left box line, start drawing it to the
3351 right of that line. */
3352 if (s->face->box != FACE_NO_BOX
3353 && s->first_glyph->left_box_line_p)
3354 x = s->x + s->face->box_line_width;
3355 else
3356 x = s->x;
3357
3358 /* If there is a margin around the image, adjust x- and y-position
3359 by that margin. */
3360 if (s->img->margin)
3361 {
3362 x += s->img->margin;
3363 y += s->img->margin;
3364 }
3365
3366 if (s->hl == DRAW_IMAGE_SUNKEN
3367 || s->hl == DRAW_IMAGE_RAISED)
3368 {
3369 thick = toolbar_button_relief > 0 ? toolbar_button_relief : 3;
3370 raised_p = s->hl == DRAW_IMAGE_RAISED;
3371 }
3372 else
3373 {
3374 thick = abs (s->img->relief);
3375 raised_p = s->img->relief > 0;
3376 }
3377
3378 x0 = x - thick;
3379 y0 = y - thick;
3380 x1 = x + s->img->width + thick - 1;
3381 y1 = y + s->img->height + thick - 1;
3382
3383 x_setup_relief_colors (s);
3384 x_get_glyph_string_clip_rect (s, &r);
3385 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3386}
3387
3388
3389/* Draw the foreground of image glyph string S to PIXMAP. */
3390
3391static void
3392x_draw_image_foreground_1 (s, pixmap)
3393 struct glyph_string *s;
3394 Pixmap pixmap;
3395{
3396 int x;
3397 int y = s->ybase - s->y - IMAGE_ASCENT (s->img);
3398
3399 /* If first glyph of S has a left box line, start drawing it to the
3400 right of that line. */
3401 if (s->face->box != FACE_NO_BOX
3402 && s->first_glyph->left_box_line_p)
3403 x = s->face->box_line_width;
3404 else
3405 x = 0;
3406
3407 /* If there is a margin around the image, adjust x- and y-position
3408 by that margin. */
3409 if (s->img->margin)
3410 {
3411 x += s->img->margin;
3412 y += s->img->margin;
3413 }
dc43ef94 3414
06a2c219
GM
3415 if (s->img->pixmap)
3416 {
3417 if (s->img->mask)
3418 {
3419 /* We can't set both a clip mask and use XSetClipRectangles
3420 because the latter also sets a clip mask. We also can't
3421 trust on the shape extension to be available
3422 (XShapeCombineRegion). So, compute the rectangle to draw
3423 manually. */
3424 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3425 | GCFunction);
3426 XGCValues xgcv;
3427
3428 xgcv.clip_mask = s->img->mask;
3429 xgcv.clip_x_origin = x;
3430 xgcv.clip_y_origin = y;
3431 xgcv.function = GXcopy;
3432 XChangeGC (s->display, s->gc, mask, &xgcv);
3433
3434 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3435 0, 0, s->img->width, s->img->height, x, y);
3436 XSetClipMask (s->display, s->gc, None);
3437 }
3438 else
3439 {
3440 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3441 0, 0, s->img->width, s->img->height, x, y);
3442
3443 /* When the image has a mask, we can expect that at
3444 least part of a mouse highlight or a block cursor will
3445 be visible. If the image doesn't have a mask, make
3446 a block cursor visible by drawing a rectangle around
3447 the image. I believe it's looking better if we do
3448 nothing here for mouse-face. */
3449 if (s->hl == DRAW_CURSOR)
3450 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3451 s->img->width - 1, s->img->height - 1);
3452 }
3453 }
3454 else
3455 /* Draw a rectangle if image could not be loaded. */
3456 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3457 s->img->width - 1, s->img->height - 1);
3458}
dc43ef94 3459
990ba854 3460
06a2c219
GM
3461/* Draw part of the background of glyph string S. X, Y, W, and H
3462 give the rectangle to draw. */
a9a5b0a5 3463
06a2c219
GM
3464static void
3465x_draw_glyph_string_bg_rect (s, x, y, w, h)
3466 struct glyph_string *s;
3467 int x, y, w, h;
3468{
3469 if (s->stippled_p)
3470 {
3471 /* Fill background with a stipple pattern. */
3472 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3473 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3474 XSetFillStyle (s->display, s->gc, FillSolid);
3475 }
3476 else
3477 x_clear_glyph_string_rect (s, x, y, w, h);
3478}
07e34cb0 3479
b5210ea7 3480
06a2c219 3481/* Draw image glyph string S.
dc43ef94 3482
06a2c219
GM
3483 s->y
3484 s->x +-------------------------
3485 | s->face->box
3486 |
3487 | +-------------------------
3488 | | s->img->margin
3489 | |
3490 | | +-------------------
3491 | | | the image
dc43ef94 3492
06a2c219 3493 */
dc43ef94 3494
06a2c219
GM
3495static void
3496x_draw_image_glyph_string (s)
3497 struct glyph_string *s;
3498{
3499 int x, y;
3500 int box_line_width = s->face->box_line_width;
3501 int margin = s->img->margin;
3502 int height;
3503 Pixmap pixmap = None;
3504
3505 height = s->height - 2 * box_line_width;
3506
3507 /* Fill background with face under the image. Do it only if row is
3508 taller than image or if image has a clip mask to reduce
3509 flickering. */
3510 s->stippled_p = s->face->stipple != 0;
3511 if (height > s->img->height
3512 || margin
3513 || s->img->mask
3514 || s->img->pixmap == 0
3515 || s->width != s->background_width)
3516 {
3517 if (box_line_width && s->first_glyph->left_box_line_p)
3518 x = s->x + box_line_width;
3519 else
3520 x = s->x;
3521
3522 y = s->y + box_line_width;
3523
3524 if (s->img->mask)
3525 {
3526 /* Create a pixmap as large as the glyph string Fill it with
3527 the background color. Copy the image to it, using its
3528 mask. Copy the temporary pixmap to the display. */
3529 Screen *screen = FRAME_X_SCREEN (s->f);
3530 int depth = DefaultDepthOfScreen (screen);
3531
3532 /* Create a pixmap as large as the glyph string. */
3533 pixmap = XCreatePixmap (s->display, s->window,
3534 s->background_width,
3535 s->height, depth);
3536
3537 /* Don't clip in the following because we're working on the
3538 pixmap. */
3539 XSetClipMask (s->display, s->gc, None);
3540
3541 /* Fill the pixmap with the background color/stipple. */
3542 if (s->stippled_p)
3543 {
3544 /* Fill background with a stipple pattern. */
3545 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3546 XFillRectangle (s->display, pixmap, s->gc,
3547 0, 0, s->background_width, s->height);
3548 XSetFillStyle (s->display, s->gc, FillSolid);
3549 }
3550 else
3551 {
3552 XGCValues xgcv;
3553 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3554 &xgcv);
3555 XSetForeground (s->display, s->gc, xgcv.background);
3556 XFillRectangle (s->display, pixmap, s->gc,
3557 0, 0, s->background_width, s->height);
3558 XSetForeground (s->display, s->gc, xgcv.foreground);
3559 }
3560 }
3561 else
3562 /* Implementation idea: Is it possible to construct a mask?
3563 We could look at the color at the margins of the image, and
3564 say that this color is probably the background color of the
3565 image. */
3566 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
3567
3568 s->background_filled_p = 1;
3569 }
dc43ef94 3570
06a2c219
GM
3571 /* Draw the foreground. */
3572 if (pixmap != None)
3573 {
3574 x_draw_image_foreground_1 (s, pixmap);
3575 x_set_glyph_string_clipping (s);
3576 XCopyArea (s->display, pixmap, s->window, s->gc,
3577 0, 0, s->background_width, s->height, s->x, s->y);
3578 XFreePixmap (s->display, pixmap);
3579 }
3580 else
3581 x_draw_image_foreground (s);
b5210ea7 3582
06a2c219
GM
3583 /* If we must draw a relief around the image, do it. */
3584 if (s->img->relief
3585 || s->hl == DRAW_IMAGE_RAISED
3586 || s->hl == DRAW_IMAGE_SUNKEN)
3587 x_draw_image_relief (s);
3588}
8c1a6a84 3589
990ba854 3590
06a2c219 3591/* Draw stretch glyph string S. */
dc43ef94 3592
06a2c219
GM
3593static void
3594x_draw_stretch_glyph_string (s)
3595 struct glyph_string *s;
3596{
3597 xassert (s->first_glyph->type == STRETCH_GLYPH);
3598 s->stippled_p = s->face->stipple != 0;
990ba854 3599
06a2c219
GM
3600 if (s->hl == DRAW_CURSOR
3601 && !x_stretch_cursor_p)
3602 {
3603 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
3604 as wide as the stretch glyph. */
3605 int width = min (CANON_X_UNIT (s->f), s->background_width);
990ba854 3606
06a2c219
GM
3607 /* Draw cursor. */
3608 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
0cdd0c9f 3609
06a2c219
GM
3610 /* Clear rest using the GC of the original non-cursor face. */
3611 if (width < s->background_width)
3612 {
3613 GC gc = s->face->gc;
3614 int x = s->x + width, y = s->y;
3615 int w = s->background_width - width, h = s->height;
3616 XRectangle r;
dc43ef94 3617
06a2c219
GM
3618 x_get_glyph_string_clip_rect (s, &r);
3619 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
97210f4e 3620
06a2c219
GM
3621 if (s->face->stipple)
3622 {
3623 /* Fill background with a stipple pattern. */
3624 XSetFillStyle (s->display, gc, FillOpaqueStippled);
3625 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3626 XSetFillStyle (s->display, gc, FillSolid);
3627 }
3628 else
3629 {
3630 XGCValues xgcv;
3631 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
3632 XSetForeground (s->display, gc, xgcv.background);
3633 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3634 XSetForeground (s->display, gc, xgcv.foreground);
3635 }
3636 }
3637 }
3638 else
3639 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
3640 s->height);
3641
3642 s->background_filled_p = 1;
3643}
3644
3645
3646/* Draw glyph string S. */
3647
3648static void
3649x_draw_glyph_string (s)
3650 struct glyph_string *s;
3651{
3652 /* If S draws into the background of its successor, draw the
3653 background of the successor first so that S can draw into it.
3654 This makes S->next use XDrawString instead of XDrawImageString. */
3655 if (s->next && s->right_overhang)
3656 {
3657 xassert (s->next->img == NULL);
3658 x_set_glyph_string_gc (s->next);
3659 x_set_glyph_string_clipping (s->next);
3660 x_draw_glyph_string_background (s->next, 1);
3661 }
97210f4e 3662
06a2c219
GM
3663 /* Set up S->gc, set clipping and draw S. */
3664 x_set_glyph_string_gc (s);
3665 x_set_glyph_string_clipping (s);
3666
3667 switch (s->first_glyph->type)
3668 {
3669 case IMAGE_GLYPH:
3670 x_draw_image_glyph_string (s);
3671 break;
3672
3673 case STRETCH_GLYPH:
3674 x_draw_stretch_glyph_string (s);
3675 break;
3676
3677 case CHAR_GLYPH:
3678 x_draw_glyph_string_background (s, 0);
3679 x_draw_glyph_string_foreground (s);
3680 break;
3681
3682 default:
3683 abort ();
3684 }
3685
3686 /* Draw underline. */
3687 if (s->face->underline_p)
3688 {
3689 unsigned long dy, h;
3690
3691 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
3692 h = 1;
3693 if (!XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &dy))
3694 dy = s->height - h;
3695
3696 if (s->face->underline_defaulted_p)
3697 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3698 s->width, h);
3699 else
dc6f92b8 3700 {
06a2c219
GM
3701 XGCValues xgcv;
3702 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3703 XSetForeground (s->display, s->gc, s->face->underline_color);
3704 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3705 s->width, h);
3706 XSetForeground (s->display, s->gc, xgcv.foreground);
dc6f92b8 3707 }
06a2c219 3708 }
07e34cb0 3709
06a2c219
GM
3710 /* Draw overline. */
3711 if (s->face->overline_p)
3712 {
3713 unsigned long dy = 0, h = 1;
3714
3715 if (s->face->overline_color_defaulted_p)
3716 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3717 s->width, h);
3718 else
3719 {
3720 XGCValues xgcv;
3721 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3722 XSetForeground (s->display, s->gc, s->face->overline_color);
3723 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3724 s->width, h);
3725 XSetForeground (s->display, s->gc, xgcv.foreground);
3726 }
dc6f92b8 3727 }
06a2c219
GM
3728
3729 /* Draw strike-through. */
3730 if (s->face->strike_through_p)
3731 {
3732 unsigned long h = 1;
3733 unsigned long dy = (s->height - h) / 2;
dc43ef94 3734
06a2c219
GM
3735 if (s->face->strike_through_color_defaulted_p)
3736 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3737 s->width, h);
3738 else
3739 {
3740 XGCValues xgcv;
3741 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3742 XSetForeground (s->display, s->gc, s->face->strike_through_color);
3743 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3744 s->width, h);
3745 XSetForeground (s->display, s->gc, xgcv.foreground);
3746 }
3747 }
3748
3749 /* Draw relief. */
3750 if (s->face->box != FACE_NO_BOX)
3751 x_draw_glyph_string_box (s);
3752
3753 /* Reset clipping. */
3754 XSetClipMask (s->display, s->gc, None);
dc6f92b8 3755}
07e34cb0 3756
06a2c219
GM
3757
3758/* A work-list entry used during the construction of glyph_string
3759 structures for a composite character. */
3760
3761struct work
3762{
3763 /* Pointer to composite char info defining has the composite
3764 character is drawn. */
3765 struct cmpchar_info *cmpcharp;
3766
3767 /* Start index in compcharp->glyph[]. */
3768 int gidx;
3769
3770 /* Next in stack. */
3771 struct work *next;
3772};
3773
3774
3775static void x_fill_composite_glyph_string P_ ((struct glyph_string *,
3776 int, struct work **,
3777 struct work **));
3778
3779
3780/* Load glyph string S with information from the top of *STACK for a
3781 composite character. FACE_ID is the id of the face in which S is
3782 drawn. *NEW is a pointer to a struct work not on the stack, that
3783 can be used if this function needs to push a new structure on the
3784 stack. If it uses it, *NEW is set to null. */
07e34cb0
JB
3785
3786static void
06a2c219
GM
3787x_fill_composite_glyph_string (s, face_id, stack, new)
3788 struct glyph_string *s;
3789 int face_id;
3790 struct work **stack, **new;
07e34cb0 3791{
06a2c219
GM
3792 int i, c;
3793 struct work *work;
3794
3795 xassert (s && *new && *stack);
3796
3797 /* Pop the work stack. */
3798 work = *stack;
3799 *stack = work->next;
3800
3801 /* For all glyphs of cmpcharp->glyph, starting at the offset
3802 work->offset, until we reach the end of the definition or
3803 encounter another composite char, get the font and face to use,
3804 and add it to S. */
3805 for (i = work->gidx; i < work->cmpcharp->glyph_len; ++i)
3806 {
3807 c = FAST_GLYPH_CHAR (work->cmpcharp->glyph[i]);
3808 if (CHAR_CHARSET (c) == CHARSET_COMPOSITION)
3809 break;
3810 s->face = x_get_char_face_and_encoding (s->f, c, face_id,
3811 s->char2b + s->nchars, 1);
3812 s->font = s->face->font;
3813 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
3814 ++s->nchars;
3815 }
3816
3817 /* If we find another composite char in the glyph definition of
3818 work->cmpcharp, put back the rest of the glyphs on the work
3819 stack, and make a new entry for the composite char. */
3820 if (i < work->cmpcharp->glyph_len)
3821 {
3822 /* Push back an unprocessed rest of this glyph spec. */
3823 if (i < work->cmpcharp->glyph_len - 1)
3824 {
3825 work->gidx = i + 1;
3826 work->next = *stack;
3827 *stack = work;
3828 work = *new;
3829 *new = NULL;
3830 }
3831
3832 /* Make an entry for the composite char on the work stack. */
3833 work->cmpcharp = cmpchar_table[COMPOSITE_CHAR_ID (c)];
3834 work->gidx = 0;
3835 work->next = *stack;
3836 *stack = work;
3837 }
3838
3839 /* The width of this glyph string equals the width of the first
3840 glyph. All characters are drawn at the same x-position. */
3841 s->width = s->first_glyph->pixel_width;
3842
3843 /* If the specified font could not be loaded, use the frame's
3844 default font, but record the fact that we couldn't load it in
3845 the glyph string so that we can draw rectangles for the
3846 characters of the glyph string. */
3847 if (s->font == NULL)
3848 {
3849 s->font_not_found_p = 1;
3850 s->font = FRAME_FONT (s->f);
3851 }
3852
3853 /* Adjust base line for subscript/superscript text. */
3854 s->ybase += s->first_glyph->voffset;
3855
3856 xassert (s->face && s->face->gc);
3857
3858 /* This glyph string must always be drawn with 16-bit functions. */
3859 s->two_byte_p = 1;
3860}
3861
3862
3863/* Load glyph string S with a sequence of non-composite characters.
3864 FACE_ID is the face id of the string. START is the index of the
3865 first glyph to consider, END is the index of the last + 1. Value
3866 is the index of the first glyph not in S. */
3867
3868static int
3869x_fill_glyph_string (s, face_id, start, end)
3870 struct glyph_string *s;
3871 int face_id;
3872 int start, end;
3873{
3874 struct glyph *glyph, *last;
3875 int voffset;
3876
3877 xassert (s->charset != CHARSET_COMPOSITION);
3878 xassert (s->f == XFRAME (s->w->frame));
3879 xassert (s->nchars == 0);
3880 xassert (start >= 0 && end > start);
3881
3882 glyph = s->row->glyphs[s->area] + start;
3883 last = s->row->glyphs[s->area] + end;
3884 voffset = glyph->voffset;
3885
3886 while (glyph < last
3887 && glyph->type == CHAR_GLYPH
3888 && glyph->voffset == voffset
3889 /* Same face id implies same charset, nowadays. */
3890 && glyph->u.ch.face_id == face_id)
3891 {
3892 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
3893 s->char2b + s->nchars);
3894 if (s->char2b[s->nchars].byte2 != 0)
3895 s->two_byte_p = 1;
3896
3897 ++s->nchars;
3898 xassert (s->nchars <= end - start);
3899 s->width += glyph->pixel_width;
3900 ++glyph;
3901 }
3902
3903 s->font = s->face->font;
3904 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
3905
3906 /* If the specified font could not be loaded, use the frame's font,
3907 but record the fact that we couldn't load it in
3908 S->font_not_found_p 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 += voffset;
3918
3919 xassert (s->face && s->face->gc);
3920 return glyph - s->row->glyphs[s->area];
07e34cb0 3921}
dc6f92b8 3922
06a2c219
GM
3923
3924/* Fill glyph string S from image glyph S->first_glyph. */
dc6f92b8 3925
dfcf069d 3926static void
06a2c219
GM
3927x_fill_image_glyph_string (s)
3928 struct glyph_string *s;
3929{
3930 xassert (s->first_glyph->type == IMAGE_GLYPH);
3931 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img.id);
3932 xassert (s->img);
3933 s->face = FACE_FROM_ID (s->f, s->first_glyph->u.img.face_id);
3934 s->font = s->face->font;
3935 s->width = s->first_glyph->pixel_width;
3936
3937 /* Adjust base line for subscript/superscript text. */
3938 s->ybase += s->first_glyph->voffset;
3939}
3940
3941
3942/* Fill glyph string S from stretch glyph S->first_glyph. */
3943
3944static void
3945x_fill_stretch_glyph_string (s)
3946 struct glyph_string *s;
3947{
3948 xassert (s->first_glyph->type == STRETCH_GLYPH);
3949 s->face = FACE_FROM_ID (s->f, s->first_glyph->u.stretch.face_id);
3950 s->font = s->face->font;
3951 s->width = s->first_glyph->pixel_width;
3952
3953 /* Adjust base line for subscript/superscript text. */
3954 s->ybase += s->first_glyph->voffset;
3955}
3956
3957
3958/* Initialize glyph string S. CHAR2B is a suitably allocated vector
3959 of XChar2b structures for S; it can't be allocated in
3960 x_init_glyph_string because it must be allocated via `alloca'. W
3961 is the window on which S is drawn. ROW and AREA are the glyph row
3962 and area within the row from which S is constructed. START is the
3963 index of the first glyph structure covered by S. HL is a
3964 face-override for drawing S. */
3965
3966static void
3967x_init_glyph_string (s, char2b, w, row, area, start, hl)
3968 struct glyph_string *s;
3969 XChar2b *char2b;
3970 struct window *w;
3971 struct glyph_row *row;
3972 enum glyph_row_area area;
3973 int start;
3974 enum draw_glyphs_face hl;
3975{
3976 bzero (s, sizeof *s);
3977 s->w = w;
3978 s->f = XFRAME (w->frame);
3979 s->display = FRAME_X_DISPLAY (s->f);
3980 s->window = FRAME_X_WINDOW (s->f);
3981 s->char2b = char2b;
3982 s->hl = hl;
3983 s->row = row;
3984 s->area = area;
3985 s->first_glyph = row->glyphs[area] + start;
3986 s->height = row->height;
3987 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
3988
3989 /* Display the internal border below the toolbar window. */
3990 if (s->w == XWINDOW (s->f->toolbar_window))
3991 s->y -= s->f->output_data.x->internal_border_width;
3992
3993 s->ybase = s->y + row->ascent;
3994}
3995
3996
3997/* Set background width of glyph string S. START is the index of the
3998 first glyph following S. LAST_X is the right-most x-position + 1
3999 in the drawing area. */
4000
4001static INLINE void
4002x_set_glyph_string_background_width (s, start, last_x)
4003 struct glyph_string *s;
4004 int start;
4005 int last_x;
4006{
4007 /* If the face of this glyph string has to be drawn to the end of
4008 the drawing area, set S->extends_to_end_of_line_p. */
4009 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4010
4011 if (start == s->row->used[s->area]
4012 && s->hl == DRAW_NORMAL_TEXT
4013 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4014 || s->face->background != default_face->background
4015 || s->face->stipple != default_face->stipple))
4016 s->extends_to_end_of_line_p = 1;
4017
4018 /* If S extends its face to the end of the line, set its
4019 background_width to the distance to the right edge of the drawing
4020 area. */
4021 if (s->extends_to_end_of_line_p)
4022 s->background_width = last_x - s->x;
4023 else
4024 s->background_width = s->width;
4025}
4026
4027
4028/* Add a glyph string for a stretch glyph to the list of strings
4029 between HEAD and TAIL. START is the index of the stretch glyph in
4030 row area AREA of glyph row ROW. END is the index of the last glyph
4031 in that glyph row area. X is the current output position assigned
4032 to the new glyph string constructed. HL overrides that face of the
4033 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4034 is the right-most x-position of the drawing area. */
4035
4036#define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, \
4037 HL, X, LAST_X) \
4038 do \
4039 { \
4040 s = (struct glyph_string *) alloca (sizeof *s); \
4041 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4042 x_fill_stretch_glyph_string (s); \
4043 x_append_glyph_string (&HEAD, &TAIL, s); \
4044 ++START; \
4045 s->x = (X); \
4046 } \
4047 while (0)
4048
4049
4050/* Add a glyph string for an image glyph to the list of strings
4051 between HEAD and TAIL. START is the index of the image glyph in
4052 row area AREA of glyph row ROW. END is the index of the last glyph
4053 in that glyph row area. X is the current output position assigned
4054 to the new glyph string constructed. HL overrides that face of the
4055 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4056 is the right-most x-position of the drawing area. */
4057
4058#define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, \
4059 HL, X, LAST_X) \
4060 do \
4061 { \
4062 s = (struct glyph_string *) alloca (sizeof *s); \
4063 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4064 x_fill_image_glyph_string (s); \
4065 x_append_glyph_string (&HEAD, &TAIL, s); \
4066 ++START; \
4067 s->x = (X); \
4068 } \
4069 while (0)
4070
4071
4072/* Add a glyph string for a sequence of character glyphs to the list
4073 of strings between HEAD and TAIL. START is the index of the first
4074 glyph in row area AREA of glyph row ROW that is part of the new
4075 glyph string. END is the index of the last glyph in that glyph row
4076 area. X is the current output position assigned to the new glyph
4077 string constructed. HL overrides that face of the glyph; e.g. it
4078 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4079 right-most x-position of the drawing area. */
4080
4081#define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, \
4082 X, LAST_X) \
4083 do \
4084 { \
4085 int c, charset, face_id; \
4086 XChar2b *char2b; \
4087 \
4088 c = (ROW)->glyphs[AREA][START].u.ch.code; \
4089 charset = CHAR_CHARSET (c); \
4090 face_id = (ROW)->glyphs[AREA][START].u.ch.face_id; \
4091 \
4092 if (charset == CHARSET_COMPOSITION) \
4093 { \
4094 struct work *stack, *work, *new = NULL; \
4095 int n = 0; \
4096 struct glyph_string *first_s = NULL; \
4097 \
4098 /* Push an initial entry for character c on the stack. */ \
4099 stack = NULL; \
4100 work = (struct work *) alloca (sizeof *work); \
4101 work->cmpcharp = cmpchar_table[COMPOSITE_CHAR_ID (c)]; \
4102 work->gidx = 0; \
4103 work->next = stack; \
4104 stack = work; \
4105 \
4106 /* While the stack is not empty, append glyph_strings \
4107 to head/tail for glyphs to draw. */ \
4108 while (stack) \
4109 { \
4110 s = (struct glyph_string *) alloca (sizeof *s); \
4111 char2b = (XChar2b *) alloca (stack->cmpcharp->glyph_len \
4112 * sizeof (XChar2b)); \
4113 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4114 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4115 s->cmpcharp = stack->cmpcharp; \
4116 s->gidx = stack->gidx; \
4117 s->charset = charset; \
4118 s->x = (X); \
4119 \
4120 if (n == 0) \
4121 { \
4122 /* Don't draw the background except for the \
4123 first glyph string. */ \
4124 s->background_filled_p = n > 0; \
4125 first_s = s; \
4126 } \
4127 ++n; \
4128 \
4129 if (new == NULL) \
4130 new = (struct work *) alloca (sizeof *new); \
4131 x_fill_composite_glyph_string (s, face_id, &stack, \
4132 &new); \
4133 } \
4134 \
4135 ++START; \
4136 s = first_s; \
4137 } \
4138 else \
4139 { \
4140 s = (struct glyph_string *) alloca (sizeof *s); \
4141 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4142 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4143 x_append_glyph_string (&HEAD, &TAIL, s); \
4144 s->charset = charset; \
4145 s->x = (X); \
4146 START = x_fill_glyph_string (s, face_id, START, END); \
4147 } \
4148 } \
4149 while (0)
4150
4151
4152/* Build a list of glyph strings between HEAD and TAIL for the glyphs
4153 of AREA of glyph row ROW on window W between indices START and END.
4154 HL overrides the face for drawing glyph strings, e.g. it is
4155 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4156 x-positions of the drawing area.
4157
4158 This is an ugly monster macro construct because we must use alloca
4159 to allocate glyph strings (because x_draw_glyphs can be called
4160 asynchronously). */
4161
4162#define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, \
4163 X, LAST_X) \
4164 do \
4165 { \
4166 HEAD = TAIL = NULL; \
4167 while (START < END) \
4168 { \
4169 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4170 switch (first_glyph->type) \
4171 { \
4172 case CHAR_GLYPH: \
4173 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
4174 TAIL, HL, X, LAST_X); \
4175 break; \
4176 \
4177 case STRETCH_GLYPH: \
4178 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4179 HEAD, TAIL, HL, X, LAST_X); \
4180 break; \
4181 \
4182 case IMAGE_GLYPH: \
4183 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4184 TAIL, HL, X, LAST_X); \
4185 break; \
4186 \
4187 default: \
4188 abort (); \
4189 } \
4190 \
4191 x_set_glyph_string_background_width (s, START, LAST_X); \
4192 (X) += s->width; \
4193 } \
4194 } \
4195 while (0)
4196
4197
4198/* Draw glyphs between START and END in AREA of ROW on window W,
4199 starting at x-position X. X is relative to AREA in W. HL is a
4200 face-override with the following meaning:
4201
4202 DRAW_NORMAL_TEXT draw normally
4203 DRAW_CURSOR draw in cursor face
4204 DRAW_MOUSE_FACE draw in mouse face.
4205 DRAW_INVERSE_VIDEO draw in mode line face
4206 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4207 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4208
4209 If REAL_START is non-null, return in *REAL_START the real starting
4210 position for display. This can be different from START in case
4211 overlapping glyphs must be displayed. If REAL_END is non-null,
4212 return in *REAL_END the real end position for display. This can be
4213 different from END in case overlapping glyphs must be displayed.
4214
4215 Value is the x-position reached, relative to AREA of W. */
4216
4217static int
4218x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end)
4219 struct window *w;
4220 int x;
4221 struct glyph_row *row;
4222 enum glyph_row_area area;
4223 int start, end;
4224 enum draw_glyphs_face hl;
4225 int *real_start, *real_end;
dc6f92b8 4226{
06a2c219
GM
4227 struct glyph_string *head, *tail;
4228 struct glyph_string *s;
4229 int last_x, area_width;
4230 int x_reached;
4231 int i, j;
4232
4233 /* Let's rather be paranoid than getting a SEGV. */
4234 start = max (0, start);
4235 end = min (end, row->used[area]);
4236 if (real_start)
4237 *real_start = start;
4238 if (real_end)
4239 *real_end = end;
4240
4241 /* Translate X to frame coordinates. Set last_x to the right
4242 end of the drawing area. */
4243 if (row->full_width_p)
4244 {
4245 /* X is relative to the left edge of W, without scroll bars
4246 or flag areas. */
4247 struct frame *f = XFRAME (w->frame);
4248 int width = FRAME_FLAGS_AREA_WIDTH (f);
4249 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
dc6f92b8 4250
06a2c219
GM
4251 x += window_left_x;
4252 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4253 last_x = window_left_x + area_width;
4254
4255 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4256 {
4257 width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4258 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4259 last_x += width;
4260 else
4261 x -= width;
4262 }
dc6f92b8 4263
06a2c219
GM
4264 /* If row should extend over internal borders, adjust x and
4265 width accordingly. */
4266 if (row->internal_border_p)
4267 {
4268 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4269 width -= 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
4270 }
4271 }
4272 else
dc6f92b8 4273 {
06a2c219
GM
4274 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4275 area_width = window_box_width (w, area);
4276 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
dc6f92b8
JB
4277 }
4278
06a2c219
GM
4279 /* Build a doubly-linked list of glyph_string structures between
4280 head and tail from what we have to draw. Note that the macro
4281 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4282 the reason we use a separate variable `i'. */
4283 i = start;
4284 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x);
4285 if (tail)
4286 x_reached = tail->x + tail->background_width;
4287 else
4288 x_reached = x;
90e65f07 4289
06a2c219
GM
4290 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4291 the row, redraw some glyphs in front or following the glyph
4292 strings built above. */
4293 if (row->contains_overlapping_glyphs_p)
4294 {
4295 int dummy_x = 0;
4296 struct glyph_string *h, *t;
4297
4298 /* Compute overhangs for all glyph strings. */
4299 for (s = head; s; s = s->next)
4300 x_compute_glyph_string_overhangs (s);
4301
4302 /* Prepend glyph strings for glyphs in front of the first glyph
4303 string that are overwritten because of the first glyph
4304 string's left overhang. The background of all strings
4305 prepended must be drawn because the first glyph string
4306 draws over it. */
4307 i = x_left_overwritten (head);
4308 if (i >= 0)
4309 {
4310 j = i;
4311 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
4312 DRAW_NORMAL_TEXT, dummy_x, last_x);
4313 start = i;
4314 if (real_start)
4315 *real_start = start;
4316 x_compute_overhangs_and_x (t, head->x, 1);
4317 x_prepend_glyph_string_lists (&head, &tail, h, t);
4318 }
58769bee 4319
06a2c219
GM
4320 /* Prepend glyph strings for glyphs in front of the first glyph
4321 string that overwrite that glyph string because of their
4322 right overhang. For these strings, only the foreground must
4323 be drawn, because it draws over the glyph string at `head'.
4324 The background must not be drawn because this would overwrite
4325 right overhangs of preceding glyphs for which no glyph
4326 strings exist. */
4327 i = x_left_overwriting (head);
4328 if (i >= 0)
4329 {
4330 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
4331 DRAW_NORMAL_TEXT, dummy_x, last_x);
4332 for (s = h; s; s = s->next)
4333 s->background_filled_p = 1;
4334 if (real_start)
4335 *real_start = i;
4336 x_compute_overhangs_and_x (t, head->x, 1);
4337 x_prepend_glyph_string_lists (&head, &tail, h, t);
4338 }
dbcb258a 4339
06a2c219
GM
4340 /* Append glyphs strings for glyphs following the last glyph
4341 string tail that are overwritten by tail. The background of
4342 these strings has to be drawn because tail's foreground draws
4343 over it. */
4344 i = x_right_overwritten (tail);
4345 if (i >= 0)
4346 {
4347 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4348 DRAW_NORMAL_TEXT, x, last_x);
4349 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4350 x_append_glyph_string_lists (&head, &tail, h, t);
4351 if (real_end)
4352 *real_end = i;
4353 }
dc6f92b8 4354
06a2c219
GM
4355 /* Append glyph strings for glyphs following the last glyph
4356 string tail that overwrite tail. The foreground of such
4357 glyphs has to be drawn because it writes into the background
4358 of tail. The background must not be drawn because it could
4359 paint over the foreground of following glyphs. */
4360 i = x_right_overwriting (tail);
4361 if (i >= 0)
4362 {
4363 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4364 DRAW_NORMAL_TEXT, x, last_x);
4365 for (s = h; s; s = s->next)
4366 s->background_filled_p = 1;
4367 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4368 x_append_glyph_string_lists (&head, &tail, h, t);
4369 if (real_end)
4370 *real_end = i;
4371 }
4372 }
58769bee 4373
06a2c219
GM
4374 /* Draw all strings. */
4375 for (s = head; s; s = s->next)
4376 x_draw_glyph_string (s);
dc6f92b8 4377
06a2c219
GM
4378 /* Value is the x-position up to which drawn, relative to AREA of W.
4379 This doesn't include parts drawn because of overhangs. */
4380 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4381 if (!row->full_width_p)
4382 {
4383 if (area > LEFT_MARGIN_AREA)
4384 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4385 if (area > TEXT_AREA)
4386 x_reached -= window_box_width (w, TEXT_AREA);
4387 }
4388 return x_reached;
4389}
dc6f92b8 4390
dc6f92b8 4391
06a2c219
GM
4392/* Output LEN glyphs starting at START at the nominal cursor position.
4393 Advance the nominal cursor over the text. The global variable
4394 updated_window contains the window being updated, updated_row is
4395 the glyph row being updated, and updated_area is the area of that
4396 row being updated. */
dc6f92b8 4397
06a2c219
GM
4398static void
4399x_write_glyphs (start, len)
4400 struct glyph *start;
4401 int len;
4402{
4403 int x, hpos, real_start, real_end;
d9cdbb3d 4404
06a2c219 4405 xassert (updated_window && updated_row);
dc6f92b8 4406 BLOCK_INPUT;
06a2c219
GM
4407
4408 /* Write glyphs. */
dc6f92b8 4409
06a2c219
GM
4410 hpos = start - updated_row->glyphs[updated_area];
4411 x = x_draw_glyphs (updated_window, output_cursor.x,
4412 updated_row, updated_area,
4413 hpos, hpos + len,
4414 (updated_row->inverse_p
4415 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4416 &real_start, &real_end);
b30ec466 4417
06a2c219
GM
4418 /* If we drew over the cursor, note that it is not visible any more. */
4419 note_overwritten_text_cursor (updated_window, real_start,
4420 real_end - real_start);
dc6f92b8
JB
4421
4422 UNBLOCK_INPUT;
06a2c219
GM
4423
4424 /* Advance the output cursor. */
4425 output_cursor.hpos += len;
4426 output_cursor.x = x;
dc6f92b8
JB
4427}
4428
0cdd0c9f 4429
06a2c219 4430/* Insert LEN glyphs from START at the nominal cursor position. */
0cdd0c9f 4431
06a2c219
GM
4432static void
4433x_insert_glyphs (start, len)
4434 struct glyph *start;
4435 register int len;
4436{
4437 struct frame *f;
4438 struct window *w;
4439 int line_height, shift_by_width, shifted_region_width;
4440 struct glyph_row *row;
4441 struct glyph *glyph;
4442 int frame_x, frame_y, hpos, real_start, real_end;
58769bee 4443
06a2c219 4444 xassert (updated_window && updated_row);
0cdd0c9f 4445 BLOCK_INPUT;
06a2c219
GM
4446 w = updated_window;
4447 f = XFRAME (WINDOW_FRAME (w));
4448
4449 /* Get the height of the line we are in. */
4450 row = updated_row;
4451 line_height = row->height;
4452
4453 /* Get the width of the glyphs to insert. */
4454 shift_by_width = 0;
4455 for (glyph = start; glyph < start + len; ++glyph)
4456 shift_by_width += glyph->pixel_width;
4457
4458 /* Get the width of the region to shift right. */
4459 shifted_region_width = (window_box_width (w, updated_area)
4460 - output_cursor.x
4461 - shift_by_width);
4462
4463 /* Shift right. */
4464 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
4465 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
4466 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
4467 f->output_data.x->normal_gc,
4468 frame_x, frame_y,
4469 shifted_region_width, line_height,
4470 frame_x + shift_by_width, frame_y);
4471
4472 /* Write the glyphs. */
4473 hpos = start - row->glyphs[updated_area];
4474 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
4475 DRAW_NORMAL_TEXT, &real_start, &real_end);
4476 note_overwritten_text_cursor (w, real_start, real_end - real_start);
4477
4478 /* Advance the output cursor. */
4479 output_cursor.hpos += len;
4480 output_cursor.x += shift_by_width;
0cdd0c9f
RS
4481 UNBLOCK_INPUT;
4482}
0cdd0c9f 4483
0cdd0c9f 4484
06a2c219
GM
4485/* Delete N glyphs at the nominal cursor position. Not implemented
4486 for X frames. */
c83febd7
RS
4487
4488static void
06a2c219
GM
4489x_delete_glyphs (n)
4490 register int n;
c83febd7 4491{
06a2c219 4492 abort ();
c83febd7
RS
4493}
4494
0cdd0c9f 4495
06a2c219
GM
4496/* Erase the current text line from the nominal cursor position
4497 (inclusive) to pixel column TO_X (exclusive). The idea is that
4498 everything from TO_X onward is already erased.
4499
4500 TO_X is a pixel position relative to updated_area of
4501 updated_window. TO_X == -1 means clear to the end of this area. */
dc6f92b8 4502
06a2c219
GM
4503static void
4504x_clear_end_of_line (to_x)
4505 int to_x;
4506{
4507 struct frame *f;
4508 struct window *w = updated_window;
4509 int max_x, min_y, max_y;
4510 int from_x, from_y, to_y;
4511
4512 xassert (updated_window && updated_row);
4513 f = XFRAME (w->frame);
4514
4515 if (updated_row->full_width_p)
4516 {
4517 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
4518 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
4519 && !w->pseudo_window_p)
4520 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
0cdd0c9f 4521 }
06a2c219
GM
4522 else
4523 max_x = window_box_width (w, updated_area);
4524 max_y = window_text_bottom_y (w);
dc6f92b8 4525
06a2c219
GM
4526 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
4527 of window. For TO_X > 0, truncate to end of drawing area. */
4528 if (to_x == 0)
4529 return;
4530 else if (to_x < 0)
4531 to_x = max_x;
4532 else
4533 to_x = min (to_x, max_x);
dbc4e1c1 4534
06a2c219
GM
4535 to_y = min (max_y, output_cursor.y + updated_row->height);
4536
4537 /* Notice if the cursor will be cleared by this operation. */
4538 if (!updated_row->full_width_p)
4539 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
dbc4e1c1 4540
06a2c219
GM
4541 from_x = output_cursor.x;
4542
4543 /* Translate to frame coordinates. */
4544 if (updated_row->full_width_p)
4545 {
4546 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
4547 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
4548 }
0cdd0c9f
RS
4549 else
4550 {
06a2c219
GM
4551 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
4552 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
4553 }
4554
4555 min_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
4556 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
4557 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
4558
4559 /* Prevent inadvertently clearing to end of the X window. */
4560 if (to_x > from_x && to_y > from_y)
4561 {
4562 BLOCK_INPUT;
4563 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4564 from_x, from_y, to_x - from_x, to_y - from_y,
4565 False);
4566 UNBLOCK_INPUT;
0cdd0c9f 4567 }
0cdd0c9f 4568}
dbc4e1c1 4569
0cdd0c9f 4570
06a2c219
GM
4571/* Clear entire frame. If updating_frame is non-null, clear that
4572 frame. Otherwise clear selected_frame. */
4573
4574static void
4575x_clear_frame ()
0cdd0c9f 4576{
06a2c219 4577 struct frame *f;
0cdd0c9f 4578
06a2c219
GM
4579 if (updating_frame)
4580 f = updating_frame;
0cdd0c9f 4581 else
06a2c219 4582 f = selected_frame;
58769bee 4583
06a2c219
GM
4584 /* Clearing the frame will erase any cursor, so mark them all as no
4585 longer visible. */
4586 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
4587 output_cursor.hpos = output_cursor.vpos = 0;
4588 output_cursor.x = -1;
4589
4590 /* We don't set the output cursor here because there will always
4591 follow an explicit cursor_to. */
4592 BLOCK_INPUT;
4593 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
4594
4595 /* We have to clear the scroll bars, too. If we have changed
4596 colors or something like that, then they should be notified. */
4597 x_scroll_bar_clear (f);
0cdd0c9f 4598
06a2c219
GM
4599 XFlush (FRAME_X_DISPLAY (f));
4600 UNBLOCK_INPUT;
dc6f92b8 4601}
06a2c219
GM
4602
4603
dc6f92b8 4604\f
dbc4e1c1
JB
4605/* Invert the middle quarter of the frame for .15 sec. */
4606
06a2c219
GM
4607/* We use the select system call to do the waiting, so we have to make
4608 sure it's available. If it isn't, we just won't do visual bells. */
4609
dbc4e1c1
JB
4610#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
4611
06a2c219
GM
4612
4613/* Subtract the `struct timeval' values X and Y, storing the result in
4614 *RESULT. Return 1 if the difference is negative, otherwise 0. */
dbc4e1c1
JB
4615
4616static int
4617timeval_subtract (result, x, y)
4618 struct timeval *result, x, y;
4619{
06a2c219
GM
4620 /* Perform the carry for the later subtraction by updating y. This
4621 is safer because on some systems the tv_sec member is unsigned. */
dbc4e1c1
JB
4622 if (x.tv_usec < y.tv_usec)
4623 {
4624 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
4625 y.tv_usec -= 1000000 * nsec;
4626 y.tv_sec += nsec;
4627 }
06a2c219 4628
dbc4e1c1
JB
4629 if (x.tv_usec - y.tv_usec > 1000000)
4630 {
4631 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
4632 y.tv_usec += 1000000 * nsec;
4633 y.tv_sec -= nsec;
4634 }
4635
06a2c219
GM
4636 /* Compute the time remaining to wait. tv_usec is certainly
4637 positive. */
dbc4e1c1
JB
4638 result->tv_sec = x.tv_sec - y.tv_sec;
4639 result->tv_usec = x.tv_usec - y.tv_usec;
4640
06a2c219
GM
4641 /* Return indication of whether the result should be considered
4642 negative. */
dbc4e1c1
JB
4643 return x.tv_sec < y.tv_sec;
4644}
dc6f92b8 4645
dfcf069d 4646void
f676886a
JB
4647XTflash (f)
4648 struct frame *f;
dc6f92b8 4649{
dbc4e1c1 4650 BLOCK_INPUT;
dc6f92b8 4651
dbc4e1c1
JB
4652 {
4653 GC gc;
dc6f92b8 4654
06a2c219
GM
4655 /* Create a GC that will use the GXxor function to flip foreground
4656 pixels into background pixels. */
dbc4e1c1
JB
4657 {
4658 XGCValues values;
dc6f92b8 4659
dbc4e1c1 4660 values.function = GXxor;
7556890b
RS
4661 values.foreground = (f->output_data.x->foreground_pixel
4662 ^ f->output_data.x->background_pixel);
58769bee 4663
334208b7 4664 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dbc4e1c1
JB
4665 GCFunction | GCForeground, &values);
4666 }
dc6f92b8 4667
dbc4e1c1 4668 {
e84e14c3
RS
4669 /* Get the height not including a menu bar widget. */
4670 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
4671 /* Height of each line to flash. */
4672 int flash_height = FRAME_LINE_HEIGHT (f);
4673 /* These will be the left and right margins of the rectangles. */
4674 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
4675 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
4676
4677 int width;
4678
4679 /* Don't flash the area between a scroll bar and the frame
4680 edge it is next to. */
4681 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
4682 {
4683 case vertical_scroll_bar_left:
4684 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
4685 break;
4686
4687 case vertical_scroll_bar_right:
4688 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
4689 break;
06a2c219
GM
4690
4691 default:
4692 break;
e84e14c3
RS
4693 }
4694
4695 width = flash_right - flash_left;
4696
4697 /* If window is tall, flash top and bottom line. */
4698 if (height > 3 * FRAME_LINE_HEIGHT (f))
4699 {
4700 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
4701 flash_left,
4702 (FRAME_INTERNAL_BORDER_WIDTH (f)
4703 + FRAME_TOOLBAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
4704 width, flash_height);
4705 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4706 flash_left,
4707 (height - flash_height
4708 - FRAME_INTERNAL_BORDER_WIDTH (f)),
4709 width, flash_height);
4710 }
4711 else
4712 /* If it is short, flash it all. */
4713 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4714 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
4715 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
dc6f92b8 4716
06a2c219 4717 x_flush (f);
dc6f92b8 4718
dbc4e1c1 4719 {
06a2c219 4720 struct timeval wakeup;
dc6f92b8 4721
66c30ea1 4722 EMACS_GET_TIME (wakeup);
dc6f92b8 4723
dbc4e1c1
JB
4724 /* Compute time to wait until, propagating carry from usecs. */
4725 wakeup.tv_usec += 150000;
4726 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
4727 wakeup.tv_usec %= 1000000;
4728
4729 /* Keep waiting until past the time wakeup. */
4730 while (1)
4731 {
4732 struct timeval timeout;
4733
66c30ea1 4734 EMACS_GET_TIME (timeout);
dbc4e1c1
JB
4735
4736 /* In effect, timeout = wakeup - timeout.
4737 Break if result would be negative. */
4738 if (timeval_subtract (&timeout, wakeup, timeout))
4739 break;
4740
4741 /* Try to wait that long--but we might wake up sooner. */
c32cdd9a 4742 select (0, NULL, NULL, NULL, &timeout);
dbc4e1c1
JB
4743 }
4744 }
58769bee 4745
e84e14c3
RS
4746 /* If window is tall, flash top and bottom line. */
4747 if (height > 3 * FRAME_LINE_HEIGHT (f))
4748 {
4749 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
4750 flash_left,
4751 (FRAME_INTERNAL_BORDER_WIDTH (f)
4752 + FRAME_TOOLBAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
4753 width, flash_height);
4754 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4755 flash_left,
4756 (height - flash_height
4757 - FRAME_INTERNAL_BORDER_WIDTH (f)),
4758 width, flash_height);
4759 }
4760 else
4761 /* If it is short, flash it all. */
4762 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4763 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
4764 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
4765
334208b7 4766 XFreeGC (FRAME_X_DISPLAY (f), gc);
06a2c219 4767 x_flush (f);
dc6f92b8 4768 }
dbc4e1c1
JB
4769 }
4770
4771 UNBLOCK_INPUT;
dc6f92b8
JB
4772}
4773
06a2c219 4774#endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
dbc4e1c1
JB
4775
4776
dc6f92b8
JB
4777/* Make audible bell. */
4778
334208b7 4779#define XRINGBELL XBell (FRAME_X_DISPLAY (selected_frame), 0)
dc6f92b8 4780
dfcf069d 4781void
dc6f92b8
JB
4782XTring_bell ()
4783{
334208b7 4784 if (FRAME_X_DISPLAY (selected_frame) == 0)
5a6ef893
RS
4785 return;
4786
dbc4e1c1 4787#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
dc6f92b8 4788 if (visible_bell)
f676886a 4789 XTflash (selected_frame);
dc6f92b8 4790 else
dbc4e1c1 4791#endif
dc6f92b8
JB
4792 {
4793 BLOCK_INPUT;
4794 XRINGBELL;
334208b7 4795 XFlush (FRAME_X_DISPLAY (selected_frame));
dc6f92b8
JB
4796 UNBLOCK_INPUT;
4797 }
4798}
06a2c219 4799
dc6f92b8 4800\f
06a2c219
GM
4801/* Specify how many text lines, from the top of the window,
4802 should be affected by insert-lines and delete-lines operations.
4803 This, and those operations, are used only within an update
4804 that is bounded by calls to x_update_begin and x_update_end. */
dc6f92b8 4805
dfcf069d 4806static void
06a2c219
GM
4807XTset_terminal_window (n)
4808 register int n;
dc6f92b8 4809{
06a2c219 4810 /* This function intentionally left blank. */
dc6f92b8
JB
4811}
4812
06a2c219
GM
4813
4814\f
4815/***********************************************************************
4816 Line Dance
4817 ***********************************************************************/
4818
4819/* Perform an insert-lines or delete-lines operation, inserting N
4820 lines or deleting -N lines at vertical position VPOS. */
4821
dfcf069d 4822static void
06a2c219
GM
4823x_ins_del_lines (vpos, n)
4824 int vpos, n;
dc6f92b8
JB
4825{
4826 abort ();
4827}
06a2c219
GM
4828
4829
4830/* Scroll part of the display as described by RUN. */
dc6f92b8 4831
dfcf069d 4832static void
06a2c219
GM
4833x_scroll_run (w, run)
4834 struct window *w;
4835 struct run *run;
dc6f92b8 4836{
06a2c219
GM
4837 struct frame *f = XFRAME (w->frame);
4838 int x, y, width, height, from_y, to_y, bottom_y;
4839
4840 /* Get frame-relative bounding box of the text display area of W,
4841 without mode lines. Include in this box the flags areas to the
4842 left and right of W. */
4843 window_box (w, -1, &x, &y, &width, &height);
4844 width += 2 * FRAME_X_FLAGS_AREA_WIDTH (f);
4845 x -= FRAME_X_FLAGS_AREA_WIDTH (f);
4846
4847 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
4848 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
4849 bottom_y = y + height;
dc6f92b8 4850
06a2c219
GM
4851 if (to_y < from_y)
4852 {
4853 /* Scrolling up. Make sure we don't copy part of the mode
4854 line at the bottom. */
4855 if (from_y + run->height > bottom_y)
4856 height = bottom_y - from_y;
4857 else
4858 height = run->height;
4859 }
dc6f92b8 4860 else
06a2c219
GM
4861 {
4862 /* Scolling down. Make sure we don't copy over the mode line.
4863 at the bottom. */
4864 if (to_y + run->height > bottom_y)
4865 height = bottom_y - to_y;
4866 else
4867 height = run->height;
4868 }
7a13e894 4869
06a2c219
GM
4870 BLOCK_INPUT;
4871
4872 /* Cursor off. Will be switched on again in x_update_window_end. */
4873 updated_window = w;
4874 x_clear_cursor (w);
4875
4876 XCopyArea (FRAME_X_DISPLAY (f),
4877 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
4878 f->output_data.x->normal_gc,
4879 x, from_y,
4880 width, height,
4881 x, to_y);
4882
4883 UNBLOCK_INPUT;
4884}
dc6f92b8 4885
dc6f92b8 4886
06a2c219
GM
4887\f
4888/***********************************************************************
4889 Exposure Events
4890 ***********************************************************************/
4891
4892/* Redisplay an exposed area of frame F. X and Y are the upper-left
4893 corner of the exposed rectangle. W and H are width and height of
4894 the exposed area. All are pixel values. W or H zero means redraw
4895 the entire frame. */
dc6f92b8 4896
06a2c219
GM
4897static void
4898expose_frame (f, x, y, w, h)
4899 struct frame *f;
4900 int x, y, w, h;
dc6f92b8 4901{
06a2c219 4902 XRectangle r;
dc6f92b8 4903
06a2c219 4904 TRACE ((stderr, "expose_frame "));
dc6f92b8 4905
06a2c219
GM
4906 /* No need to redraw if frame will be redrawn soon. */
4907 if (FRAME_GARBAGED_P (f))
dc6f92b8 4908 {
06a2c219
GM
4909 TRACE ((stderr, " garbaged\n"));
4910 return;
4911 }
4912
4913 /* If basic faces haven't been realized yet, there is no point in
4914 trying to redraw anything. This can happen when we get an expose
4915 event while Emacs is starting, e.g. by moving another window. */
4916 if (FRAME_FACE_CACHE (f) == NULL
4917 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
4918 {
4919 TRACE ((stderr, " no faces\n"));
4920 return;
58769bee 4921 }
06a2c219
GM
4922
4923 if (w == 0 || h == 0)
58769bee 4924 {
06a2c219
GM
4925 r.x = r.y = 0;
4926 r.width = CANON_X_UNIT (f) * f->width;
4927 r.height = CANON_Y_UNIT (f) * f->height;
dc6f92b8
JB
4928 }
4929 else
4930 {
06a2c219
GM
4931 r.x = x;
4932 r.y = y;
4933 r.width = w;
4934 r.height = h;
4935 }
4936
4937 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
4938 expose_window_tree (XWINDOW (f->root_window), &r);
4939
4940 if (WINDOWP (f->toolbar_window))
4941 {
4942 struct window *w = XWINDOW (f->toolbar_window);
4943 XRectangle window_rect;
4944 XRectangle intersection_rect;
4945 int window_x, window_y, window_width, window_height;
4946
4947
4948 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
4949 window_rect.x = window_x;
4950 window_rect.y = window_y;
4951 window_rect.width = window_width;
4952 window_rect.height = window_height;
4953
4954 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
4955 expose_window (w, &intersection_rect);
4956 }
4957
4958#ifndef USE_X_TOOLKIT
4959 if (WINDOWP (f->menu_bar_window))
4960 {
4961 struct window *w = XWINDOW (f->menu_bar_window);
4962 XRectangle window_rect;
4963 XRectangle intersection_rect;
4964 int window_x, window_y, window_width, window_height;
4965
4966
4967 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
4968 window_rect.x = window_x;
4969 window_rect.y = window_y;
4970 window_rect.width = window_width;
4971 window_rect.height = window_height;
4972
4973 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
4974 expose_window (w, &intersection_rect);
dc6f92b8 4975 }
06a2c219 4976#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
4977}
4978
06a2c219
GM
4979
4980/* Redraw (parts) of all windows in the window tree rooted at W that
4981 intersect R. R contains frame pixel coordinates. */
4982
58769bee 4983static void
06a2c219
GM
4984expose_window_tree (w, r)
4985 struct window *w;
4986 XRectangle *r;
dc6f92b8 4987{
06a2c219
GM
4988 while (w)
4989 {
4990 if (!NILP (w->hchild))
4991 expose_window_tree (XWINDOW (w->hchild), r);
4992 else if (!NILP (w->vchild))
4993 expose_window_tree (XWINDOW (w->vchild), r);
4994 else
4995 {
4996 XRectangle window_rect;
4997 XRectangle intersection_rect;
4998 struct frame *f = XFRAME (w->frame);
4999 int window_x, window_y, window_width, window_height;
5000
5001 /* Frame-relative pixel rectangle of W. */
5002 window_box (w, -1, &window_x, &window_y, &window_width,
5003 &window_height);
5004 window_rect.x
5005 = (window_x
5006 - FRAME_X_FLAGS_AREA_WIDTH (f)
5007 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_Y_UNIT (f));
5008 window_rect.y = window_y;
5009 window_rect.width
5010 = (window_width
5011 + 2 * FRAME_X_FLAGS_AREA_WIDTH (f)
5012 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5013 window_rect.height
5014 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5015
5016 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5017 expose_window (w, &intersection_rect);
5018 }
58769bee 5019
06a2c219
GM
5020 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5021 }
5022}
58769bee 5023
dc6f92b8 5024
06a2c219
GM
5025/* Redraw the part of glyph row area AREA of glyph row ROW on window W
5026 which intersects rectangle R. R is in window-relative coordinates. */
5027
5028static void
5029expose_area (w, row, r, area)
5030 struct window *w;
5031 struct glyph_row *row;
5032 XRectangle *r;
5033 enum glyph_row_area area;
5034{
5035 int x;
5036 struct glyph *first = row->glyphs[area];
5037 struct glyph *end = row->glyphs[area] + row->used[area];
5038 struct glyph *last;
5039 int first_x;
5040
5041 /* Set x to the window-relative start position for drawing glyphs of
5042 AREA. The first glyph of the text area can be partially visible.
5043 The first glyphs of other areas cannot. */
5044 if (area == LEFT_MARGIN_AREA)
5045 x = 0;
5046 else if (area == TEXT_AREA)
5047 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5048 else
5049 x = (window_box_width (w, LEFT_MARGIN_AREA)
5050 + window_box_width (w, TEXT_AREA));
5051
5052 /* Find the first glyph that must be redrawn. */
5053 while (first < end
5054 && x + first->pixel_width < r->x)
5055 {
5056 x += first->pixel_width;
5057 ++first;
5058 }
5059
5060 /* Find the last one. */
5061 last = first;
5062 first_x = x;
5063 while (last < end
5064 && x < r->x + r->width)
5065 {
5066 x += last->pixel_width;
5067 ++last;
5068 }
5069
5070 /* Repaint. */
5071 if (last > first)
5072 x_draw_glyphs (w, first_x, row, area,
5073 first - row->glyphs[area],
5074 last - row->glyphs[area],
5075 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5076 NULL, NULL);
5077}
5078
58769bee 5079
06a2c219
GM
5080/* Redraw the parts of the glyph row ROW on window W intersecting
5081 rectangle R. R is in window-relative coordinates. */
dc6f92b8 5082
06a2c219
GM
5083static void
5084expose_line (w, row, r)
5085 struct window *w;
5086 struct glyph_row *row;
5087 XRectangle *r;
5088{
5089 xassert (row->enabled_p);
5090
5091 if (row->mode_line_p || w->pseudo_window_p)
5092 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5093 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5094 NULL, NULL);
5095 else
5096 {
5097 if (row->used[LEFT_MARGIN_AREA])
5098 expose_area (w, row, r, LEFT_MARGIN_AREA);
5099 if (row->used[TEXT_AREA])
5100 expose_area (w, row, r, TEXT_AREA);
5101 if (row->used[RIGHT_MARGIN_AREA])
5102 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5103 x_draw_row_bitmaps (w, row);
5104 }
5105}
dc6f92b8 5106
58769bee 5107
06a2c219
GM
5108/* Return non-zero if W's cursor intersects rectangle R. */
5109
5110static int
5111x_phys_cursor_in_rect_p (w, r)
5112 struct window *w;
5113 XRectangle *r;
5114{
5115 XRectangle cr, result;
5116 struct glyph *cursor_glyph;
5117
5118 cursor_glyph = get_phys_cursor_glyph (w);
5119 if (cursor_glyph)
5120 {
5121 cr.x = w->phys_cursor.x;
5122 cr.y = w->phys_cursor.y;
5123 cr.width = cursor_glyph->pixel_width;
5124 cr.height = w->phys_cursor_height;
5125 return x_intersect_rectangles (&cr, r, &result);
5126 }
5127 else
5128 return 0;
dc6f92b8 5129}
dc6f92b8 5130
06a2c219
GM
5131
5132/* Redraw a rectangle of window W. R is a rectangle in window
5133 relative coordinates. Call this function with input blocked. */
dc6f92b8
JB
5134
5135static void
06a2c219
GM
5136expose_window (w, r)
5137 struct window *w;
5138 XRectangle *r;
dc6f92b8 5139{
06a2c219
GM
5140 struct glyph_row *row;
5141 int y;
5142 int yb = window_text_bottom_y (w);
5143 int cursor_cleared_p;
dc6f92b8 5144
06a2c219
GM
5145 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5146 r->x, r->y, r->width, r->height));
dc6f92b8 5147
06a2c219
GM
5148 /* Convert to window coordinates. */
5149 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5150 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
dc6f92b8 5151
06a2c219
GM
5152 /* Turn off the cursor. */
5153 if (!w->pseudo_window_p
5154 && x_phys_cursor_in_rect_p (w, r))
5155 {
5156 x_clear_cursor (w);
5157 cursor_cleared_p = 1;
5158 }
5159 else
5160 cursor_cleared_p = 0;
5161
5162 /* Find the first row intersecting the rectangle R. */
5163 row = w->current_matrix->rows;
5164 y = 0;
5165 while (row->enabled_p
5166 && y < yb
5167 && y + row->height < r->y)
5168 {
5169 y += row->height;
5170 ++row;
5171 }
5172
dc6f92b8 5173 /* Display the text in the rectangle, one text line at a time. */
06a2c219
GM
5174 while (row->enabled_p
5175 && y < yb
5176 && y < r->y + r->height)
5177 {
5178 expose_line (w, row, r);
5179 y += row->height;
5180 ++row;
5181 }
5182
5183 /* Display the mode line if there is one. */
5184 if (WINDOW_WANTS_MODELINE_P (w)
5185 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5186 row->enabled_p)
5187 && row->y < r->y + r->height)
5188 expose_line (w, row, r);
dc6f92b8 5189
06a2c219 5190 if (!w->pseudo_window_p)
dc6f92b8 5191 {
06a2c219
GM
5192 /* Draw border between windows. */
5193 x_draw_vertical_border (w);
5194
5195 /* Turn the cursor on again. */
5196 if (cursor_cleared_p)
5197 x_update_window_cursor (w, 1);
5198 }
5199}
dc6f92b8 5200
dc6f92b8 5201
06a2c219
GM
5202/* Determine the intersection of two rectangles R1 and R2. Return
5203 the intersection in *RESULT. Value is non-zero if RESULT is not
5204 empty. */
5205
5206static int
5207x_intersect_rectangles (r1, r2, result)
5208 XRectangle *r1, *r2, *result;
5209{
5210 XRectangle *left, *right;
5211 XRectangle *upper, *lower;
5212 int intersection_p = 0;
5213
5214 /* Rearrange so that R1 is the left-most rectangle. */
5215 if (r1->x < r2->x)
5216 left = r1, right = r2;
5217 else
5218 left = r2, right = r1;
5219
5220 /* X0 of the intersection is right.x0, if this is inside R1,
5221 otherwise there is no intersection. */
5222 if (right->x <= left->x + left->width)
5223 {
5224 result->x = right->x;
5225
5226 /* The right end of the intersection is the minimum of the
5227 the right ends of left and right. */
5228 result->width = (min (left->x + left->width, right->x + right->width)
5229 - result->x);
5230
5231 /* Same game for Y. */
5232 if (r1->y < r2->y)
5233 upper = r1, lower = r2;
5234 else
5235 upper = r2, lower = r1;
5236
5237 /* The upper end of the intersection is lower.y0, if this is inside
5238 of upper. Otherwise, there is no intersection. */
5239 if (lower->y <= upper->y + upper->height)
dc43ef94 5240 {
06a2c219
GM
5241 result->y = lower->y;
5242
5243 /* The lower end of the intersection is the minimum of the lower
5244 ends of upper and lower. */
5245 result->height = (min (lower->y + lower->height,
5246 upper->y + upper->height)
5247 - result->y);
5248 intersection_p = 1;
dc43ef94 5249 }
dc6f92b8
JB
5250 }
5251
06a2c219 5252 return intersection_p;
dc6f92b8 5253}
06a2c219
GM
5254
5255
5256
5257
dc6f92b8 5258\f
dc6f92b8 5259static void
334208b7
RS
5260frame_highlight (f)
5261 struct frame *f;
dc6f92b8 5262{
b3e1e05c
JB
5263 /* We used to only do this if Vx_no_window_manager was non-nil, but
5264 the ICCCM (section 4.1.6) says that the window's border pixmap
5265 and border pixel are window attributes which are "private to the
5266 client", so we can always change it to whatever we want. */
5267 BLOCK_INPUT;
334208b7 5268 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5269 f->output_data.x->border_pixel);
b3e1e05c 5270 UNBLOCK_INPUT;
5d46f928 5271 x_update_cursor (f, 1);
dc6f92b8
JB
5272}
5273
5274static void
334208b7
RS
5275frame_unhighlight (f)
5276 struct frame *f;
dc6f92b8 5277{
b3e1e05c
JB
5278 /* We used to only do this if Vx_no_window_manager was non-nil, but
5279 the ICCCM (section 4.1.6) says that the window's border pixmap
5280 and border pixel are window attributes which are "private to the
5281 client", so we can always change it to whatever we want. */
5282 BLOCK_INPUT;
334208b7 5283 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5284 f->output_data.x->border_tile);
b3e1e05c 5285 UNBLOCK_INPUT;
5d46f928 5286 x_update_cursor (f, 1);
dc6f92b8 5287}
dc6f92b8 5288
f676886a
JB
5289/* The focus has changed. Update the frames as necessary to reflect
5290 the new situation. Note that we can't change the selected frame
c5acd733 5291 here, because the Lisp code we are interrupting might become confused.
eb8c3be9 5292 Each event gets marked with the frame in which it occurred, so the
c5acd733 5293 Lisp code can tell when the switch took place by examining the events. */
dc6f92b8 5294
6d4238f3 5295static void
0f941935
KH
5296x_new_focus_frame (dpyinfo, frame)
5297 struct x_display_info *dpyinfo;
f676886a 5298 struct frame *frame;
dc6f92b8 5299{
0f941935 5300 struct frame *old_focus = dpyinfo->x_focus_frame;
dc6f92b8 5301
0f941935 5302 if (frame != dpyinfo->x_focus_frame)
dc6f92b8 5303 {
58769bee 5304 /* Set this before calling other routines, so that they see
f676886a 5305 the correct value of x_focus_frame. */
0f941935 5306 dpyinfo->x_focus_frame = frame;
6d4238f3
JB
5307
5308 if (old_focus && old_focus->auto_lower)
f676886a 5309 x_lower_frame (old_focus);
dc6f92b8
JB
5310
5311#if 0
f676886a 5312 selected_frame = frame;
e0c1aef2
KH
5313 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
5314 selected_frame);
f676886a
JB
5315 Fselect_window (selected_frame->selected_window);
5316 choose_minibuf_frame ();
c118dd06 5317#endif /* ! 0 */
dc6f92b8 5318
0f941935
KH
5319 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
5320 pending_autoraise_frame = dpyinfo->x_focus_frame;
0134a210
RS
5321 else
5322 pending_autoraise_frame = 0;
6d4238f3 5323 }
dc6f92b8 5324
0f941935 5325 x_frame_rehighlight (dpyinfo);
6d4238f3
JB
5326}
5327
37c2c98b
RS
5328/* Handle an event saying the mouse has moved out of an Emacs frame. */
5329
5330void
0f941935
KH
5331x_mouse_leave (dpyinfo)
5332 struct x_display_info *dpyinfo;
37c2c98b 5333{
0f941935 5334 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
37c2c98b 5335}
6d4238f3 5336
f451eb13
JB
5337/* The focus has changed, or we have redirected a frame's focus to
5338 another frame (this happens when a frame uses a surrogate
06a2c219 5339 mini-buffer frame). Shift the highlight as appropriate.
0f941935
KH
5340
5341 The FRAME argument doesn't necessarily have anything to do with which
06a2c219 5342 frame is being highlighted or un-highlighted; we only use it to find
0f941935 5343 the appropriate X display info. */
06a2c219 5344
6d4238f3 5345static void
0f941935
KH
5346XTframe_rehighlight (frame)
5347 struct frame *frame;
6d4238f3 5348{
0f941935
KH
5349 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
5350}
6d4238f3 5351
0f941935
KH
5352static void
5353x_frame_rehighlight (dpyinfo)
5354 struct x_display_info *dpyinfo;
5355{
5356 struct frame *old_highlight = dpyinfo->x_highlight_frame;
5357
5358 if (dpyinfo->x_focus_frame)
6d4238f3 5359 {
0f941935
KH
5360 dpyinfo->x_highlight_frame
5361 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
5362 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
5363 : dpyinfo->x_focus_frame);
5364 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
f451eb13 5365 {
0f941935
KH
5366 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
5367 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
f451eb13 5368 }
dc6f92b8 5369 }
6d4238f3 5370 else
0f941935 5371 dpyinfo->x_highlight_frame = 0;
dc6f92b8 5372
0f941935 5373 if (dpyinfo->x_highlight_frame != old_highlight)
6d4238f3
JB
5374 {
5375 if (old_highlight)
f676886a 5376 frame_unhighlight (old_highlight);
0f941935
KH
5377 if (dpyinfo->x_highlight_frame)
5378 frame_highlight (dpyinfo->x_highlight_frame);
6d4238f3 5379 }
dc6f92b8 5380}
06a2c219
GM
5381
5382
dc6f92b8 5383\f
06a2c219 5384/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
dc6f92b8 5385
28430d3c
JB
5386/* Initialize mode_switch_bit and modifier_meaning. */
5387static void
334208b7
RS
5388x_find_modifier_meanings (dpyinfo)
5389 struct x_display_info *dpyinfo;
28430d3c 5390{
f689eb05 5391 int min_code, max_code;
28430d3c
JB
5392 KeySym *syms;
5393 int syms_per_code;
5394 XModifierKeymap *mods;
5395
334208b7
RS
5396 dpyinfo->meta_mod_mask = 0;
5397 dpyinfo->shift_lock_mask = 0;
5398 dpyinfo->alt_mod_mask = 0;
5399 dpyinfo->super_mod_mask = 0;
5400 dpyinfo->hyper_mod_mask = 0;
58769bee 5401
9658a521 5402#ifdef HAVE_X11R4
334208b7 5403 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
9658a521 5404#else
4a60f8c5
RS
5405 min_code = dpyinfo->display->min_keycode;
5406 max_code = dpyinfo->display->max_keycode;
9658a521
JB
5407#endif
5408
334208b7 5409 syms = XGetKeyboardMapping (dpyinfo->display,
28430d3c
JB
5410 min_code, max_code - min_code + 1,
5411 &syms_per_code);
334208b7 5412 mods = XGetModifierMapping (dpyinfo->display);
28430d3c 5413
58769bee 5414 /* Scan the modifier table to see which modifier bits the Meta and
11edeb03 5415 Alt keysyms are on. */
28430d3c 5416 {
06a2c219 5417 int row, col; /* The row and column in the modifier table. */
28430d3c
JB
5418
5419 for (row = 3; row < 8; row++)
5420 for (col = 0; col < mods->max_keypermod; col++)
5421 {
0299d313
RS
5422 KeyCode code
5423 = mods->modifiermap[(row * mods->max_keypermod) + col];
28430d3c 5424
af92970c
KH
5425 /* Zeroes are used for filler. Skip them. */
5426 if (code == 0)
5427 continue;
5428
28430d3c
JB
5429 /* Are any of this keycode's keysyms a meta key? */
5430 {
5431 int code_col;
5432
5433 for (code_col = 0; code_col < syms_per_code; code_col++)
5434 {
f689eb05 5435 int sym = syms[((code - min_code) * syms_per_code) + code_col];
28430d3c 5436
f689eb05 5437 switch (sym)
28430d3c 5438 {
f689eb05
JB
5439 case XK_Meta_L:
5440 case XK_Meta_R:
334208b7 5441 dpyinfo->meta_mod_mask |= (1 << row);
28430d3c 5442 break;
f689eb05
JB
5443
5444 case XK_Alt_L:
5445 case XK_Alt_R:
334208b7 5446 dpyinfo->alt_mod_mask |= (1 << row);
a3c44b14
RS
5447 break;
5448
5449 case XK_Hyper_L:
5450 case XK_Hyper_R:
334208b7 5451 dpyinfo->hyper_mod_mask |= (1 << row);
a3c44b14
RS
5452 break;
5453
5454 case XK_Super_L:
5455 case XK_Super_R:
334208b7 5456 dpyinfo->super_mod_mask |= (1 << row);
f689eb05 5457 break;
11edeb03
JB
5458
5459 case XK_Shift_Lock:
5460 /* Ignore this if it's not on the lock modifier. */
5461 if ((1 << row) == LockMask)
334208b7 5462 dpyinfo->shift_lock_mask = LockMask;
11edeb03 5463 break;
28430d3c
JB
5464 }
5465 }
5466 }
5467 }
5468 }
5469
f689eb05 5470 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
334208b7 5471 if (! dpyinfo->meta_mod_mask)
a3c44b14 5472 {
334208b7
RS
5473 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
5474 dpyinfo->alt_mod_mask = 0;
a3c44b14 5475 }
f689eb05 5476
148c4b70
RS
5477 /* If some keys are both alt and meta,
5478 make them just meta, not alt. */
334208b7 5479 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
148c4b70 5480 {
334208b7 5481 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
148c4b70 5482 }
58769bee 5483
28430d3c 5484 XFree ((char *) syms);
f689eb05 5485 XFreeModifiermap (mods);
28430d3c
JB
5486}
5487
dfeccd2d
JB
5488/* Convert between the modifier bits X uses and the modifier bits
5489 Emacs uses. */
06a2c219 5490
7c5283e4 5491static unsigned int
334208b7
RS
5492x_x_to_emacs_modifiers (dpyinfo, state)
5493 struct x_display_info *dpyinfo;
dc6f92b8
JB
5494 unsigned int state;
5495{
334208b7
RS
5496 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
5497 | ((state & ControlMask) ? ctrl_modifier : 0)
5498 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
5499 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
5500 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
5501 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
dc6f92b8
JB
5502}
5503
dfeccd2d 5504static unsigned int
334208b7
RS
5505x_emacs_to_x_modifiers (dpyinfo, state)
5506 struct x_display_info *dpyinfo;
dfeccd2d
JB
5507 unsigned int state;
5508{
334208b7
RS
5509 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
5510 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
5511 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
5512 | ((state & shift_modifier) ? ShiftMask : 0)
5513 | ((state & ctrl_modifier) ? ControlMask : 0)
5514 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
dfeccd2d 5515}
d047c4eb
KH
5516
5517/* Convert a keysym to its name. */
5518
5519char *
5520x_get_keysym_name (keysym)
5521 KeySym keysym;
5522{
5523 char *value;
5524
5525 BLOCK_INPUT;
5526 value = XKeysymToString (keysym);
5527 UNBLOCK_INPUT;
5528
5529 return value;
5530}
06a2c219
GM
5531
5532
e4571a43
JB
5533\f
5534/* Mouse clicks and mouse movement. Rah. */
e4571a43 5535
06a2c219
GM
5536/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
5537 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
5538 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
5539 not force the value into range. */
69388238 5540
c8dba240 5541void
69388238 5542pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
e4571a43 5543 FRAME_PTR f;
69388238 5544 register int pix_x, pix_y;
e4571a43
JB
5545 register int *x, *y;
5546 XRectangle *bounds;
69388238 5547 int noclip;
e4571a43 5548{
06a2c219 5549 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
69388238
RS
5550 even for negative values. */
5551 if (pix_x < 0)
7556890b 5552 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
69388238 5553 if (pix_y < 0)
7556890b 5554 pix_y -= (f)->output_data.x->line_height - 1;
69388238 5555
e4571a43
JB
5556 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
5557 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
5558
5559 if (bounds)
5560 {
7556890b
RS
5561 bounds->width = FONT_WIDTH (f->output_data.x->font);
5562 bounds->height = f->output_data.x->line_height;
e4571a43
JB
5563 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
5564 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
5565 }
5566
69388238
RS
5567 if (!noclip)
5568 {
5569 if (pix_x < 0)
5570 pix_x = 0;
3cbd2e0b
RS
5571 else if (pix_x > FRAME_WINDOW_WIDTH (f))
5572 pix_x = FRAME_WINDOW_WIDTH (f);
69388238
RS
5573
5574 if (pix_y < 0)
5575 pix_y = 0;
5576 else if (pix_y > f->height)
5577 pix_y = f->height;
5578 }
e4571a43
JB
5579
5580 *x = pix_x;
5581 *y = pix_y;
5582}
5583
06a2c219
GM
5584
5585/* Given HPOS/VPOS in the current matrix of W, return corresponding
5586 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
5587 can't tell the positions because W's display is not up to date,
5588 return 0. */
5589
5590int
5591glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
5592 struct window *w;
5593 int hpos, vpos;
5594 int *frame_x, *frame_y;
2b5c9e71 5595{
06a2c219
GM
5596 int success_p;
5597
5598 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
5599 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
5600
5601 if (display_completed)
5602 {
5603 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
5604 struct glyph *glyph = row->glyphs[TEXT_AREA];
5605 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
5606
5607 *frame_y = row->y;
5608 *frame_x = row->x;
5609 while (glyph < end)
5610 {
5611 *frame_x += glyph->pixel_width;
5612 ++glyph;
5613 }
5614
5615 success_p = 1;
5616 }
5617 else
5618 {
5619 *frame_y = *frame_x = 0;
5620 success_p = 0;
5621 }
5622
5623 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
5624 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
5625 return success_p;
2b5c9e71
RS
5626}
5627
06a2c219 5628
dc6f92b8
JB
5629/* Prepare a mouse-event in *RESULT for placement in the input queue.
5630
5631 If the event is a button press, then note that we have grabbed
f451eb13 5632 the mouse. */
dc6f92b8
JB
5633
5634static Lisp_Object
f451eb13 5635construct_mouse_click (result, event, f)
dc6f92b8
JB
5636 struct input_event *result;
5637 XButtonEvent *event;
f676886a 5638 struct frame *f;
dc6f92b8 5639{
f451eb13 5640 /* Make the event type no_event; we'll change that when we decide
dc6f92b8 5641 otherwise. */
f451eb13 5642 result->kind = mouse_click;
69388238 5643 result->code = event->button - Button1;
1113d9db 5644 result->timestamp = event->time;
334208b7
RS
5645 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
5646 event->state)
f689eb05 5647 | (event->type == ButtonRelease
58769bee 5648 ? up_modifier
f689eb05 5649 : down_modifier));
dc6f92b8 5650
06a2c219
GM
5651 XSETINT (result->x, event->x);
5652 XSETINT (result->y, event->y);
5653 XSETFRAME (result->frame_or_window, f);
5654 return Qnil;
dc6f92b8 5655}
b849c413 5656
06a2c219
GM
5657#if 0 /* This function isn't called. --gerd */
5658
b849c413
RS
5659/* Prepare a menu-event in *RESULT for placement in the input queue. */
5660
5661static Lisp_Object
5662construct_menu_click (result, event, f)
5663 struct input_event *result;
5664 XButtonEvent *event;
5665 struct frame *f;
5666{
5667 /* Make the event type no_event; we'll change that when we decide
5668 otherwise. */
5669 result->kind = mouse_click;
26459b28 5670 result->code = event->button - Button1;
b849c413 5671 result->timestamp = event->time;
334208b7
RS
5672 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
5673 event->state)
b849c413 5674 | (event->type == ButtonRelease
58769bee 5675 ? up_modifier
b849c413
RS
5676 : down_modifier));
5677
e0c1aef2
KH
5678 XSETINT (result->x, event->x);
5679 XSETINT (result->y, -1);
5680 XSETFRAME (result->frame_or_window, f);
b849c413 5681}
06a2c219
GM
5682
5683#endif /* 0 */
5684
69388238 5685\f
90e65f07
JB
5686/* Function to report a mouse movement to the mainstream Emacs code.
5687 The input handler calls this.
5688
5689 We have received a mouse movement event, which is given in *event.
5690 If the mouse is over a different glyph than it was last time, tell
5691 the mainstream emacs code by setting mouse_moved. If not, ask for
5692 another motion event, so we can check again the next time it moves. */
b8009dd1 5693
06a2c219
GM
5694static XMotionEvent last_mouse_motion_event;
5695static Lisp_Object last_mouse_motion_frame;
5696
90e65f07 5697static void
12ba150f 5698note_mouse_movement (frame, event)
f676886a 5699 FRAME_PTR frame;
90e65f07 5700 XMotionEvent *event;
90e65f07 5701{
e5d77022 5702 last_mouse_movement_time = event->time;
06a2c219
GM
5703 last_mouse_motion_event = *event;
5704 XSETFRAME (last_mouse_motion_frame, frame);
e5d77022 5705
27f338af
RS
5706 if (event->window != FRAME_X_WINDOW (frame))
5707 {
39d8bb4d 5708 frame->mouse_moved = 1;
27f338af 5709 last_mouse_scroll_bar = Qnil;
27f338af 5710 note_mouse_highlight (frame, -1, -1);
27f338af
RS
5711 }
5712
90e65f07 5713 /* Has the mouse moved off the glyph it was on at the last sighting? */
27f338af
RS
5714 else if (event->x < last_mouse_glyph.x
5715 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
5716 || event->y < last_mouse_glyph.y
5717 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
12ba150f 5718 {
39d8bb4d 5719 frame->mouse_moved = 1;
ab648270 5720 last_mouse_scroll_bar = Qnil;
b8009dd1 5721 note_mouse_highlight (frame, event->x, event->y);
90e65f07
JB
5722 }
5723}
5724
bf1c0ba1 5725/* This is used for debugging, to turn off note_mouse_highlight. */
bf1c0ba1 5726
06a2c219
GM
5727 int disable_mouse_highlight;
5728
5729
5730\f
5731/************************************************************************
5732 Mouse Face
5733 ************************************************************************/
5734
5735/* Find the glyph under window-relative coordinates X/Y in window W.
5736 Consider only glyphs from buffer text, i.e. no glyphs from overlay
5737 strings. Return in *HPOS and *VPOS the row and column number of
5738 the glyph found. Return in *AREA the glyph area containing X.
5739 Value is a pointer to the glyph found or null if X/Y is not on
5740 text, or we can't tell because W's current matrix is not up to
5741 date. */
5742
5743static struct glyph *
5744x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
5745 struct window *w;
5746 int x, y;
5747 int *hpos, *vpos, *area;
5748{
5749 struct glyph *glyph, *end;
5750 struct glyph_row *row;
5751 int x0, i, left_area_width;
5752
5753 /* Find row containing Y. Give up if some row is not enabled. */
5754 for (i = 0; i < w->current_matrix->nrows; ++i)
5755 {
5756 row = MATRIX_ROW (w->current_matrix, i);
5757 if (!row->enabled_p)
5758 return NULL;
5759 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
5760 break;
5761 }
5762
5763 *vpos = i;
5764 *hpos = 0;
5765
5766 /* Give up if Y is not in the window. */
5767 if (i == w->current_matrix->nrows)
5768 return NULL;
5769
5770 /* Get the glyph area containing X. */
5771 if (w->pseudo_window_p)
5772 {
5773 *area = TEXT_AREA;
5774 x0 = 0;
5775 }
5776 else
5777 {
5778 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
5779 if (x < left_area_width)
5780 {
5781 *area = LEFT_MARGIN_AREA;
5782 x0 = 0;
5783 }
5784 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
5785 {
5786 *area = TEXT_AREA;
5787 x0 = row->x + left_area_width;
5788 }
5789 else
5790 {
5791 *area = RIGHT_MARGIN_AREA;
5792 x0 = left_area_width + window_box_width (w, TEXT_AREA);
5793 }
5794 }
5795
5796 /* Find glyph containing X. */
5797 glyph = row->glyphs[*area];
5798 end = glyph + row->used[*area];
5799 while (glyph < end)
5800 {
5801 if (x < x0 + glyph->pixel_width)
5802 {
5803 if (w->pseudo_window_p)
5804 break;
5805 else if (BUFFERP (glyph->object))
5806 break;
5807 }
5808
5809 x0 += glyph->pixel_width;
5810 ++glyph;
5811 }
5812
5813 if (glyph == end)
5814 return NULL;
5815
5816 *hpos = glyph - row->glyphs[*area];
5817 return glyph;
5818}
5819
5820
5821/* Convert frame-relative x/y to coordinates relative to window W.
5822 Takes pseudo-windows into account. */
5823
5824static void
5825frame_to_window_pixel_xy (w, x, y)
5826 struct window *w;
5827 int *x, *y;
5828{
5829 if (w->pseudo_window_p)
5830 {
5831 /* A pseudo-window is always full-width, and starts at the
5832 left edge of the frame, plus a frame border. */
5833 struct frame *f = XFRAME (w->frame);
5834 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
5835 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
5836 }
5837 else
5838 {
5839 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
5840 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
5841 }
5842}
5843
5844
5845/* Take proper action when mouse has moved to the mode or top line of
5846 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
5847 mode line. X is relative to the start of the text display area of
5848 W, so the width of bitmap areas and scroll bars must be subtracted
5849 to get a position relative to the start of the mode line. */
5850
5851static void
5852note_mode_line_highlight (w, x, mode_line_p)
5853 struct window *w;
5854 int x, mode_line_p;
5855{
5856 struct frame *f = XFRAME (w->frame);
5857 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
5858 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
5859 struct glyph_row *row;
5860
5861 if (mode_line_p)
5862 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
5863 else
5864 row = MATRIX_TOP_LINE_ROW (w->current_matrix);
5865
5866 if (row->enabled_p)
5867 {
5868 struct glyph *glyph, *end;
5869 Lisp_Object help, map;
5870 int x0;
5871
5872 /* Find the glyph under X. */
5873 glyph = row->glyphs[TEXT_AREA];
5874 end = glyph + row->used[TEXT_AREA];
5875 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
5876 + FRAME_X_FLAGS_AREA_WIDTH (f));
5877 while (glyph < end
5878 && x >= x0 + glyph->pixel_width)
5879 {
5880 x0 += glyph->pixel_width;
5881 ++glyph;
5882 }
5883
5884 if (glyph < end
5885 && STRINGP (glyph->object)
5886 && XSTRING (glyph->object)->intervals
5887 && glyph->charpos >= 0
5888 && glyph->charpos < XSTRING (glyph->object)->size)
5889 {
5890 /* If we're on a string with `help-echo' text property,
5891 arrange for the help to be displayed. This is done by
5892 setting the global variable help_echo to the help string. */
5893 help = Fget_text_property (make_number (glyph->charpos),
5894 Qhelp_echo, glyph->object);
5895 if (STRINGP (help))
5896 help_echo = help;
5897
5898 /* Change the mouse pointer according to what is under X/Y. */
5899 map = Fget_text_property (make_number (glyph->charpos),
5900 Qlocal_map, glyph->object);
5901 if (!NILP (Fkeymapp (map)))
5902 cursor = f->output_data.x->nontext_cursor;
5903 }
5904 }
5905
5906 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
5907}
5908
5909
5910/* Take proper action when the mouse has moved to position X, Y on
5911 frame F as regards highlighting characters that have mouse-face
5912 properties. Also de-highlighting chars where the mouse was before.
27f338af 5913 X and Y can be negative or out of range. */
b8009dd1
RS
5914
5915static void
5916note_mouse_highlight (f, x, y)
06a2c219 5917 struct frame *f;
c32cdd9a 5918 int x, y;
b8009dd1 5919{
06a2c219
GM
5920 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
5921 int portion;
b8009dd1
RS
5922 Lisp_Object window;
5923 struct window *w;
5924
06a2c219
GM
5925 /* When a menu is active, don't highlight because this looks odd. */
5926#ifdef USE_X_TOOLKIT
5927 if (popup_activated ())
5928 return;
5929#endif
5930
bf1c0ba1
RS
5931 if (disable_mouse_highlight)
5932 return;
5933
06a2c219
GM
5934 dpyinfo->mouse_face_mouse_x = x;
5935 dpyinfo->mouse_face_mouse_y = y;
5936 dpyinfo->mouse_face_mouse_frame = f;
b8009dd1 5937
06a2c219 5938 if (dpyinfo->mouse_face_defer)
b8009dd1
RS
5939 return;
5940
514e4681
RS
5941 if (gc_in_progress)
5942 {
06a2c219 5943 dpyinfo->mouse_face_deferred_gc = 1;
514e4681
RS
5944 return;
5945 }
5946
b8009dd1 5947 /* Which window is that in? */
06a2c219 5948 window = window_from_coordinates (f, x, y, &portion, 1);
b8009dd1
RS
5949
5950 /* If we were displaying active text in another window, clear that. */
06a2c219
GM
5951 if (! EQ (window, dpyinfo->mouse_face_window))
5952 clear_mouse_face (dpyinfo);
5953
5954 /* Not on a window -> return. */
5955 if (!WINDOWP (window))
5956 return;
5957
5958 /* Convert to window-relative pixel coordinates. */
5959 w = XWINDOW (window);
5960 frame_to_window_pixel_xy (w, &x, &y);
5961
5962 /* Handle toolbar window differently since it doesn't display a
5963 buffer. */
5964 if (EQ (window, f->toolbar_window))
5965 {
5966 note_toolbar_highlight (f, x, y);
5967 return;
5968 }
5969
5970 if (portion == 1 || portion == 3)
5971 {
5972 /* Mouse is on the mode or top line. */
5973 note_mode_line_highlight (w, x, portion == 1);
5974 return;
5975 }
5976 else
5977 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5978 f->output_data.x->text_cursor);
b8009dd1 5979
0cdd0c9f
RS
5980 /* Are we in a window whose display is up to date?
5981 And verify the buffer's text has not changed. */
06a2c219
GM
5982 if (/* Within text portion of the window. */
5983 portion == 0
0cdd0c9f 5984 && EQ (w->window_end_valid, w->buffer)
26459b28
KH
5985 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
5986 && (XFASTINT (w->last_overlay_modified)
5987 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
b8009dd1 5988 {
06a2c219
GM
5989 int hpos, vpos, pos, i, area;
5990 struct glyph *glyph;
b8009dd1 5991
06a2c219
GM
5992 /* Find the glyph under X/Y. */
5993 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
5994
5995 /* Clear mouse face if X/Y not over text. */
5996 if (glyph == NULL
5997 || area != TEXT_AREA
5998 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
b8009dd1 5999 {
06a2c219
GM
6000 clear_mouse_face (dpyinfo);
6001 return;
6002 }
6003
6004 pos = glyph->charpos;
6005 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6006
6007 /* Check for mouse-face and help-echo. */
6008 {
6009 Lisp_Object mouse_face, overlay, position;
6010 Lisp_Object *overlay_vec;
6011 int len, noverlays;
6012 struct buffer *obuf;
6013 int obegv, ozv;
6014
6015 /* If we get an out-of-range value, return now; avoid an error. */
6016 if (pos > BUF_Z (XBUFFER (w->buffer)))
6017 return;
6018
6019 /* Make the window's buffer temporarily current for
6020 overlays_at and compute_char_face. */
6021 obuf = current_buffer;
6022 current_buffer = XBUFFER (w->buffer);
6023 obegv = BEGV;
6024 ozv = ZV;
6025 BEGV = BEG;
6026 ZV = Z;
6027
6028 /* Is this char mouse-active or does it have help-echo? */
6029 XSETINT (position, pos);
6030
6031 /* Put all the overlays we want in a vector in overlay_vec.
6032 Store the length in len. If there are more than 10, make
6033 enough space for all, and try again. */
6034 len = 10;
6035 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6036 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6037 if (noverlays > len)
6038 {
6039 len = noverlays;
6040 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6041 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6042 }
6043
6044 noverlays = sort_overlays (overlay_vec, noverlays, w);
6045
6046 /* Check mouse-face highlighting. */
6047 if (! (EQ (window, dpyinfo->mouse_face_window)
6048 && vpos >= dpyinfo->mouse_face_beg_row
6049 && vpos <= dpyinfo->mouse_face_end_row
6050 && (vpos > dpyinfo->mouse_face_beg_row
6051 || hpos >= dpyinfo->mouse_face_beg_col)
6052 && (vpos < dpyinfo->mouse_face_end_row
6053 || hpos < dpyinfo->mouse_face_end_col
6054 || dpyinfo->mouse_face_past_end)))
6055 {
6056 /* Clear the display of the old active region, if any. */
6057 clear_mouse_face (dpyinfo);
6058
6059 /* Find the highest priority overlay that has a mouse-face prop. */
6060 overlay = Qnil;
6061 for (i = 0; i < noverlays; i++)
6062 {
6063 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6064 if (!NILP (mouse_face))
6065 {
6066 overlay = overlay_vec[i];
6067 break;
6068 }
6069 }
6070
6071 /* If no overlay applies, get a text property. */
6072 if (NILP (overlay))
6073 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6074
6075 /* Handle the overlay case. */
6076 if (! NILP (overlay))
6077 {
6078 /* Find the range of text around this char that
6079 should be active. */
6080 Lisp_Object before, after;
6081 int ignore;
6082
6083 before = Foverlay_start (overlay);
6084 after = Foverlay_end (overlay);
6085 /* Record this as the current active region. */
6086 fast_find_position (w, XFASTINT (before),
6087 &dpyinfo->mouse_face_beg_col,
6088 &dpyinfo->mouse_face_beg_row,
6089 &dpyinfo->mouse_face_beg_x,
6090 &dpyinfo->mouse_face_beg_y);
6091 dpyinfo->mouse_face_past_end
6092 = !fast_find_position (w, XFASTINT (after),
6093 &dpyinfo->mouse_face_end_col,
6094 &dpyinfo->mouse_face_end_row,
6095 &dpyinfo->mouse_face_end_x,
6096 &dpyinfo->mouse_face_end_y);
6097 dpyinfo->mouse_face_window = window;
6098 dpyinfo->mouse_face_face_id
6099 = face_at_buffer_position (w, pos, 0, 0,
6100 &ignore, pos + 1, 1);
6101
6102 /* Display it as active. */
6103 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6104 }
6105 /* Handle the text property case. */
6106 else if (! NILP (mouse_face))
6107 {
6108 /* Find the range of text around this char that
6109 should be active. */
6110 Lisp_Object before, after, beginning, end;
6111 int ignore;
6112
6113 beginning = Fmarker_position (w->start);
6114 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6115 - XFASTINT (w->window_end_pos)));
6116 before
6117 = Fprevious_single_property_change (make_number (pos + 1),
6118 Qmouse_face,
6119 w->buffer, beginning);
6120 after
6121 = Fnext_single_property_change (position, Qmouse_face,
6122 w->buffer, end);
6123 /* Record this as the current active region. */
6124 fast_find_position (w, XFASTINT (before),
6125 &dpyinfo->mouse_face_beg_col,
6126 &dpyinfo->mouse_face_beg_row,
6127 &dpyinfo->mouse_face_beg_x,
6128 &dpyinfo->mouse_face_beg_y);
6129 dpyinfo->mouse_face_past_end
6130 = !fast_find_position (w, XFASTINT (after),
6131 &dpyinfo->mouse_face_end_col,
6132 &dpyinfo->mouse_face_end_row,
6133 &dpyinfo->mouse_face_end_x,
6134 &dpyinfo->mouse_face_end_y);
6135 dpyinfo->mouse_face_window = window;
6136 dpyinfo->mouse_face_face_id
6137 = face_at_buffer_position (w, pos, 0, 0,
6138 &ignore, pos + 1, 1);
6139
6140 /* Display it as active. */
6141 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6142 }
6143 }
6144
6145 /* Look for a `help-echo' property. */
6146 {
6147 Lisp_Object help;
6148
6149 /* Check overlays first. */
6150 help = Qnil;
6151 for (i = 0; i < noverlays && !STRINGP (help); ++i)
6152 help = Foverlay_get (overlay_vec[i], Qhelp_echo);
6153
6154 /* Try text properties. */
6155 if (!STRINGP (help)
6156 && ((STRINGP (glyph->object)
6157 && glyph->charpos >= 0
6158 && glyph->charpos < XSTRING (glyph->object)->size)
6159 || (BUFFERP (glyph->object)
6160 && glyph->charpos >= BEGV
6161 && glyph->charpos < ZV)))
6162 help = Fget_text_property (make_number (glyph->charpos),
6163 Qhelp_echo, glyph->object);
6164
6165 if (STRINGP (help))
6166 help_echo = help;
6167 }
6168
6169 BEGV = obegv;
6170 ZV = ozv;
6171 current_buffer = obuf;
6172 }
6173 }
6174}
6175
6176static void
6177redo_mouse_highlight ()
6178{
6179 if (!NILP (last_mouse_motion_frame)
6180 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6181 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6182 last_mouse_motion_event.x,
6183 last_mouse_motion_event.y);
6184}
6185
6186
6187\f
6188/***********************************************************************
6189 Toolbars
6190 ***********************************************************************/
6191
6192static int x_toolbar_item P_ ((struct frame *, int, int,
6193 struct glyph **, int *, int *, int *));
6194
6195/* Toolbar item index of the item on which a mouse button was pressed
6196 or -1. */
6197
6198static int last_toolbar_item;
6199
6200
6201/* Get information about the toolbar item at position X/Y on frame F.
6202 Return in *GLYPH a pointer to the glyph of the toolbar item in
6203 the current matrix of the toolbar window of F, or NULL if not
6204 on a toolbar item. Return in *PROP_IDX the index of the toolbar
6205 item in F->current_toolbar_items. Value is
6206
6207 -1 if X/Y is not on a toolbar item
6208 0 if X/Y is on the same item that was highlighted before.
6209 1 otherwise. */
6210
6211static int
6212x_toolbar_item (f, x, y, glyph, hpos, vpos, prop_idx)
6213 struct frame *f;
6214 int x, y;
6215 struct glyph **glyph;
6216 int *hpos, *vpos, *prop_idx;
6217{
6218 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6219 struct window *w = XWINDOW (f->toolbar_window);
6220 int area;
6221
6222 /* Find the glyph under X/Y. */
6223 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6224 if (*glyph == NULL)
6225 return -1;
6226
6227 /* Get the start of this toolbar item's properties in
6228 f->current_toolbar_items. */
6229 if (!toolbar_item_info (f, *glyph, prop_idx))
6230 return -1;
6231
6232 /* Is mouse on the highlighted item? */
6233 if (EQ (f->toolbar_window, dpyinfo->mouse_face_window)
6234 && *vpos >= dpyinfo->mouse_face_beg_row
6235 && *vpos <= dpyinfo->mouse_face_end_row
6236 && (*vpos > dpyinfo->mouse_face_beg_row
6237 || *hpos >= dpyinfo->mouse_face_beg_col)
6238 && (*vpos < dpyinfo->mouse_face_end_row
6239 || *hpos < dpyinfo->mouse_face_end_col
6240 || dpyinfo->mouse_face_past_end))
6241 return 0;
6242
6243 return 1;
6244}
6245
6246
6247/* Handle mouse button event on the toolbar of frame F, at
6248 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6249 or ButtonRelase. */
6250
6251static void
6252x_handle_toolbar_click (f, button_event)
6253 struct frame *f;
6254 XButtonEvent *button_event;
6255{
6256 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6257 struct window *w = XWINDOW (f->toolbar_window);
6258 int hpos, vpos, prop_idx;
6259 struct glyph *glyph;
6260 Lisp_Object enabled_p;
6261 int x = button_event->x;
6262 int y = button_event->y;
6263
6264 /* If not on the highlighted toolbar item, return. */
6265 frame_to_window_pixel_xy (w, &x, &y);
6266 if (x_toolbar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
6267 return;
6268
6269 /* If item is disabled, do nothing. */
6270 enabled_p = (XVECTOR (f->current_toolbar_items)
6271 ->contents[prop_idx + TOOLBAR_ITEM_ENABLED_P]);
6272 if (NILP (enabled_p))
6273 return;
6274
6275 if (button_event->type == ButtonPress)
6276 {
6277 /* Show item in pressed state. */
6278 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6279 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
6280 last_toolbar_item = prop_idx;
6281 }
6282 else
6283 {
6284 Lisp_Object key, frame;
6285 struct input_event event;
6286
6287 /* Show item in released state. */
6288 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6289 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6290
6291 key = (XVECTOR (f->current_toolbar_items)
6292 ->contents[prop_idx + TOOLBAR_ITEM_KEY]);
6293
6294 XSETFRAME (frame, f);
6295 event.kind = TOOLBAR_EVENT;
6296 event.frame_or_window = Fcons (frame, Fcons (Qtoolbar, Qnil));
6297 kbd_buffer_store_event (&event);
6298
6299 event.kind = TOOLBAR_EVENT;
6300 event.frame_or_window = Fcons (frame, key);
6301 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6302 button_event->state);
6303 kbd_buffer_store_event (&event);
6304 last_toolbar_item = -1;
6305 }
6306}
6307
6308
6309/* Possibly highlight a toolbar item on frame F when mouse moves to
6310 toolbar window-relative coordinates X/Y. Called from
6311 note_mouse_highlight. */
6312
6313static void
6314note_toolbar_highlight (f, x, y)
6315 struct frame *f;
6316 int x, y;
6317{
6318 Lisp_Object window = f->toolbar_window;
6319 struct window *w = XWINDOW (window);
6320 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6321 int hpos, vpos;
6322 struct glyph *glyph;
6323 struct glyph_row *row;
6324 int i, j, area;
6325 Lisp_Object enabled_p;
6326 int prop_idx;
6327 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
6328 int on_highlight_p, mouse_down_p, rc;
6329
6330 /* Function note_mouse_highlight is called with negative x(y
6331 values when mouse moves outside of the frame. */
6332 if (x <= 0 || y <= 0)
6333 {
6334 clear_mouse_face (dpyinfo);
6335 return;
6336 }
6337
6338 rc = x_toolbar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
6339 if (rc < 0)
6340 {
6341 /* Not on toolbar item. */
6342 clear_mouse_face (dpyinfo);
6343 return;
6344 }
6345 else if (rc == 0)
6346 /* On same toolbar item as before. */
6347 goto set_help_echo;
b8009dd1 6348
06a2c219
GM
6349 clear_mouse_face (dpyinfo);
6350
6351 /* Mouse is down, but on different toolbar item? */
6352 mouse_down_p = (dpyinfo->grabbed
6353 && f == last_mouse_frame
6354 && FRAME_LIVE_P (f));
6355 if (mouse_down_p
6356 && last_toolbar_item != prop_idx)
6357 return;
6358
6359 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
6360 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
6361
6362 /* If toolbar item is not enabled, don't highlight it. */
6363 enabled_p = (XVECTOR (f->current_toolbar_items)
6364 ->contents[prop_idx + TOOLBAR_ITEM_ENABLED_P]);
6365 if (!NILP (enabled_p))
6366 {
6367 /* Compute the x-position of the glyph. In front and past the
6368 image is a space. We include this is the highlighted area. */
6369 row = MATRIX_ROW (w->current_matrix, vpos);
6370 for (i = x = 0; i < hpos; ++i)
6371 x += row->glyphs[TEXT_AREA][i].pixel_width;
6372
6373 /* Record this as the current active region. */
6374 dpyinfo->mouse_face_beg_col = hpos;
6375 dpyinfo->mouse_face_beg_row = vpos;
6376 dpyinfo->mouse_face_beg_x = x;
6377 dpyinfo->mouse_face_beg_y = row->y;
6378 dpyinfo->mouse_face_past_end = 0;
6379
6380 dpyinfo->mouse_face_end_col = hpos + 1;
6381 dpyinfo->mouse_face_end_row = vpos;
6382 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
6383 dpyinfo->mouse_face_end_y = row->y;
6384 dpyinfo->mouse_face_window = window;
6385 dpyinfo->mouse_face_face_id = TOOLBAR_FACE_ID;
6386
6387 /* Display it as active. */
6388 show_mouse_face (dpyinfo, draw);
6389 dpyinfo->mouse_face_image_state = draw;
b8009dd1 6390 }
06a2c219
GM
6391
6392 set_help_echo:
6393
6394 /* Set help_echo to a help string.to display for this toolbar item.
6395 XTread_socket does the rest. */
6396 help_echo = (XVECTOR (f->current_toolbar_items)
6397 ->contents[prop_idx + TOOLBAR_ITEM_HELP]);
6398 if (!STRINGP (help_echo))
6399 help_echo = (XVECTOR (f->current_toolbar_items)
6400 ->contents[prop_idx + TOOLBAR_ITEM_CAPTION]);
b8009dd1 6401}
4d73d038 6402
06a2c219
GM
6403
6404\f
6405/* Find the glyph matrix position of buffer position POS in window W.
6406 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
6407 current glyphs must be up to date. If POS is above window start
6408 return (0, 0, 0, 0). If POS is after end of W, return end of
6409 last line in W. */
b8009dd1
RS
6410
6411static int
06a2c219
GM
6412fast_find_position (w, pos, hpos, vpos, x, y)
6413 struct window *w;
b8009dd1 6414 int pos;
06a2c219 6415 int *hpos, *vpos, *x, *y;
b8009dd1 6416{
b8009dd1 6417 int i;
bf1c0ba1 6418 int lastcol;
06a2c219
GM
6419 int maybe_next_line_p = 0;
6420 int line_start_position;
6421 int yb = window_text_bottom_y (w);
6422 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
6423 struct glyph_row *best_row = row;
6424 int row_vpos = 0, best_row_vpos = 0;
6425 int current_x;
6426
6427 while (row->y < yb)
b8009dd1 6428 {
06a2c219
GM
6429 if (row->used[TEXT_AREA])
6430 line_start_position = row->glyphs[TEXT_AREA]->charpos;
6431 else
6432 line_start_position = 0;
6433
6434 if (line_start_position > pos)
b8009dd1 6435 break;
77b68646
RS
6436 /* If the position sought is the end of the buffer,
6437 don't include the blank lines at the bottom of the window. */
06a2c219
GM
6438 else if (line_start_position == pos
6439 && pos == BUF_ZV (XBUFFER (w->buffer)))
77b68646 6440 {
06a2c219 6441 maybe_next_line_p = 1;
77b68646
RS
6442 break;
6443 }
06a2c219
GM
6444 else if (line_start_position > 0)
6445 {
6446 best_row = row;
6447 best_row_vpos = row_vpos;
6448 }
6449
6450 ++row;
6451 ++row_vpos;
b8009dd1 6452 }
06a2c219
GM
6453
6454 /* Find the right column within BEST_ROW. */
6455 lastcol = 0;
6456 current_x = best_row->x;
6457 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
bf1c0ba1 6458 {
06a2c219
GM
6459 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
6460 int charpos;
6461
6462 charpos = glyph->charpos;
6463 if (charpos == pos)
bf1c0ba1 6464 {
06a2c219
GM
6465 *hpos = i;
6466 *vpos = best_row_vpos;
6467 *x = current_x;
6468 *y = best_row->y;
bf1c0ba1
RS
6469 return 1;
6470 }
06a2c219 6471 else if (charpos > pos)
4d73d038 6472 break;
06a2c219
GM
6473 else if (charpos > 0)
6474 lastcol = i;
6475
6476 current_x += glyph->pixel_width;
bf1c0ba1 6477 }
b8009dd1 6478
77b68646
RS
6479 /* If we're looking for the end of the buffer,
6480 and we didn't find it in the line we scanned,
6481 use the start of the following line. */
06a2c219 6482 if (maybe_next_line_p)
77b68646 6483 {
06a2c219
GM
6484 ++best_row;
6485 ++best_row_vpos;
6486 lastcol = 0;
6487 current_x = best_row->x;
77b68646
RS
6488 }
6489
06a2c219
GM
6490 *vpos = best_row_vpos;
6491 *hpos = lastcol + 1;
6492 *x = current_x;
6493 *y = best_row->y;
b8009dd1
RS
6494 return 0;
6495}
6496
06a2c219 6497
b8009dd1
RS
6498/* Display the active region described by mouse_face_*
6499 in its mouse-face if HL > 0, in its normal face if HL = 0. */
6500
6501static void
06a2c219 6502show_mouse_face (dpyinfo, draw)
7a13e894 6503 struct x_display_info *dpyinfo;
06a2c219 6504 enum draw_glyphs_face draw;
b8009dd1 6505{
7a13e894 6506 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
06a2c219 6507 struct frame *f = XFRAME (WINDOW_FRAME (w));
b8009dd1 6508 int i;
06a2c219
GM
6509 int cursor_off_p = 0;
6510 struct cursor_pos saved_cursor;
6511
6512 saved_cursor = output_cursor;
6513
6514 /* If window is in the process of being destroyed, don't bother
6515 to do anything. */
6516 if (w->current_matrix == NULL)
6517 goto set_x_cursor;
6518
6519 /* Recognize when we are called to operate on rows that don't exist
6520 anymore. This can happen when a window is split. */
6521 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
6522 goto set_x_cursor;
6523
6524 set_output_cursor (&w->phys_cursor);
6525
6526 /* Note that mouse_face_beg_row etc. are window relative. */
6527 for (i = dpyinfo->mouse_face_beg_row;
6528 i <= dpyinfo->mouse_face_end_row;
6529 i++)
6530 {
6531 int start_hpos, end_hpos, start_x;
6532 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
6533
6534 /* Don't do anything if row doesn't have valid contents. */
6535 if (!row->enabled_p)
6536 continue;
6537
6538 /* For all but the first row, the highlight starts at column 0. */
6539 if (i == dpyinfo->mouse_face_beg_row)
6540 {
6541 start_hpos = dpyinfo->mouse_face_beg_col;
6542 start_x = dpyinfo->mouse_face_beg_x;
6543 }
6544 else
6545 {
6546 start_hpos = 0;
6547 start_x = 0;
6548 }
6549
6550 if (i == dpyinfo->mouse_face_end_row)
6551 end_hpos = dpyinfo->mouse_face_end_col;
6552 else
6553 end_hpos = row->used[TEXT_AREA];
6554
6555 /* If the cursor's in the text we are about to rewrite, turn the
6556 cursor off. */
6557 if (!w->pseudo_window_p
6558 && i == output_cursor.vpos
6559 && output_cursor.hpos >= start_hpos - 1
6560 && output_cursor.hpos <= end_hpos)
514e4681 6561 {
06a2c219
GM
6562 x_update_window_cursor (w, 0);
6563 cursor_off_p = 1;
514e4681 6564 }
b8009dd1 6565
06a2c219
GM
6566 if (end_hpos > start_hpos)
6567 x_draw_glyphs (w, start_x, row, updated_area,
6568 start_hpos, end_hpos, draw, NULL, NULL);
b8009dd1
RS
6569 }
6570
514e4681 6571 /* If we turned the cursor off, turn it back on. */
06a2c219
GM
6572 if (cursor_off_p)
6573 x_display_cursor (w, 1,
6574 output_cursor.hpos, output_cursor.vpos,
6575 output_cursor.x, output_cursor.y);
2729a2b5 6576
06a2c219 6577 output_cursor = saved_cursor;
fb3b7de5 6578
06a2c219
GM
6579 set_x_cursor:
6580
6581 /* Change the mouse cursor. */
6582 if (draw == DRAW_NORMAL_TEXT)
6583 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6584 f->output_data.x->text_cursor);
6585 else if (draw == DRAW_MOUSE_FACE)
334208b7 6586 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 6587 f->output_data.x->cross_cursor);
27ead1d5 6588 else
334208b7 6589 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
06a2c219 6590 f->output_data.x->nontext_cursor);
b8009dd1
RS
6591}
6592
6593/* Clear out the mouse-highlighted active region.
06a2c219 6594 Redraw it un-highlighted first. */
b8009dd1 6595
06a2c219 6596void
7a13e894
RS
6597clear_mouse_face (dpyinfo)
6598 struct x_display_info *dpyinfo;
b8009dd1 6599{
06a2c219
GM
6600 if (tip_frame)
6601 return;
6602
7a13e894 6603 if (! NILP (dpyinfo->mouse_face_window))
06a2c219 6604 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
b8009dd1 6605
7a13e894
RS
6606 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
6607 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
6608 dpyinfo->mouse_face_window = Qnil;
b8009dd1 6609}
e687d06e
RS
6610
6611/* Just discard the mouse face information for frame F, if any.
6612 This is used when the size of F is changed. */
6613
dfcf069d 6614void
e687d06e
RS
6615cancel_mouse_face (f)
6616 FRAME_PTR f;
6617{
6618 Lisp_Object window;
6619 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6620
6621 window = dpyinfo->mouse_face_window;
6622 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
6623 {
6624 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
6625 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
6626 dpyinfo->mouse_face_window = Qnil;
6627 }
6628}
b8009dd1 6629\f
ab648270
JB
6630static struct scroll_bar *x_window_to_scroll_bar ();
6631static void x_scroll_bar_report_motion ();
12ba150f 6632
90e65f07 6633/* Return the current position of the mouse.
2d7fc7e8 6634 *fp should be a frame which indicates which display to ask about.
90e65f07 6635
2d7fc7e8 6636 If the mouse movement started in a scroll bar, set *fp, *bar_window,
ab648270 6637 and *part to the frame, window, and scroll bar part that the mouse
12ba150f 6638 is over. Set *x and *y to the portion and whole of the mouse's
ab648270 6639 position on the scroll bar.
12ba150f 6640
2d7fc7e8 6641 If the mouse movement started elsewhere, set *fp to the frame the
12ba150f
JB
6642 mouse is on, *bar_window to nil, and *x and *y to the character cell
6643 the mouse is over.
6644
06a2c219 6645 Set *time to the server time-stamp for the time at which the mouse
12ba150f
JB
6646 was at this position.
6647
a135645a
RS
6648 Don't store anything if we don't have a valid set of values to report.
6649
90e65f07 6650 This clears the mouse_moved flag, so we can wait for the next mouse
f5bb65ec 6651 movement. */
90e65f07
JB
6652
6653static void
1cf412ec 6654XTmouse_position (fp, insist, bar_window, part, x, y, time)
334208b7 6655 FRAME_PTR *fp;
1cf412ec 6656 int insist;
12ba150f 6657 Lisp_Object *bar_window;
ab648270 6658 enum scroll_bar_part *part;
90e65f07 6659 Lisp_Object *x, *y;
e5d77022 6660 unsigned long *time;
90e65f07 6661{
a135645a
RS
6662 FRAME_PTR f1;
6663
90e65f07
JB
6664 BLOCK_INPUT;
6665
8bcee03e 6666 if (! NILP (last_mouse_scroll_bar) && insist == 0)
334208b7 6667 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
90e65f07
JB
6668 else
6669 {
12ba150f
JB
6670 Window root;
6671 int root_x, root_y;
90e65f07 6672
12ba150f
JB
6673 Window dummy_window;
6674 int dummy;
6675
39d8bb4d
KH
6676 Lisp_Object frame, tail;
6677
6678 /* Clear the mouse-moved flag for every frame on this display. */
6679 FOR_EACH_FRAME (tail, frame)
6680 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
6681 XFRAME (frame)->mouse_moved = 0;
6682
ab648270 6683 last_mouse_scroll_bar = Qnil;
12ba150f
JB
6684
6685 /* Figure out which root window we're on. */
334208b7
RS
6686 XQueryPointer (FRAME_X_DISPLAY (*fp),
6687 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
12ba150f
JB
6688
6689 /* The root window which contains the pointer. */
6690 &root,
6691
6692 /* Trash which we can't trust if the pointer is on
6693 a different screen. */
6694 &dummy_window,
6695
6696 /* The position on that root window. */
58769bee 6697 &root_x, &root_y,
12ba150f
JB
6698
6699 /* More trash we can't trust. */
6700 &dummy, &dummy,
6701
6702 /* Modifier keys and pointer buttons, about which
6703 we don't care. */
6704 (unsigned int *) &dummy);
6705
6706 /* Now we have a position on the root; find the innermost window
6707 containing the pointer. */
6708 {
6709 Window win, child;
6710 int win_x, win_y;
06a2c219 6711 int parent_x = 0, parent_y = 0;
e99db5a1 6712 int count;
12ba150f
JB
6713
6714 win = root;
69388238 6715
2d7fc7e8
RS
6716 /* XTranslateCoordinates can get errors if the window
6717 structure is changing at the same time this function
6718 is running. So at least we must not crash from them. */
6719
e99db5a1 6720 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
2d7fc7e8 6721
334208b7 6722 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
23faf38f 6723 && FRAME_LIVE_P (last_mouse_frame))
12ba150f 6724 {
69388238
RS
6725 /* If mouse was grabbed on a frame, give coords for that frame
6726 even if the mouse is now outside it. */
334208b7 6727 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
69388238 6728
12ba150f 6729 /* From-window, to-window. */
69388238 6730 root, FRAME_X_WINDOW (last_mouse_frame),
12ba150f
JB
6731
6732 /* From-position, to-position. */
6733 root_x, root_y, &win_x, &win_y,
6734
6735 /* Child of win. */
6736 &child);
69388238
RS
6737 f1 = last_mouse_frame;
6738 }
6739 else
6740 {
6741 while (1)
6742 {
334208b7 6743 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
12ba150f 6744
69388238
RS
6745 /* From-window, to-window. */
6746 root, win,
12ba150f 6747
69388238
RS
6748 /* From-position, to-position. */
6749 root_x, root_y, &win_x, &win_y,
6750
6751 /* Child of win. */
6752 &child);
6753
9af3143a 6754 if (child == None || child == win)
69388238
RS
6755 break;
6756
6757 win = child;
6758 parent_x = win_x;
6759 parent_y = win_y;
6760 }
12ba150f 6761
69388238
RS
6762 /* Now we know that:
6763 win is the innermost window containing the pointer
6764 (XTC says it has no child containing the pointer),
6765 win_x and win_y are the pointer's position in it
6766 (XTC did this the last time through), and
6767 parent_x and parent_y are the pointer's position in win's parent.
6768 (They are what win_x and win_y were when win was child.
6769 If win is the root window, it has no parent, and
6770 parent_{x,y} are invalid, but that's okay, because we'll
6771 never use them in that case.) */
6772
6773 /* Is win one of our frames? */
19126e11 6774 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
69388238 6775 }
58769bee 6776
2d7fc7e8
RS
6777 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
6778 f1 = 0;
6779
e99db5a1 6780 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
2d7fc7e8 6781
ab648270 6782 /* If not, is it one of our scroll bars? */
a135645a 6783 if (! f1)
12ba150f 6784 {
ab648270 6785 struct scroll_bar *bar = x_window_to_scroll_bar (win);
12ba150f
JB
6786
6787 if (bar)
6788 {
a135645a 6789 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
6790 win_x = parent_x;
6791 win_y = parent_y;
6792 }
6793 }
90e65f07 6794
8bcee03e 6795 if (f1 == 0 && insist > 0)
1cf412ec
RS
6796 f1 = selected_frame;
6797
a135645a 6798 if (f1)
12ba150f 6799 {
06a2c219
GM
6800 /* Ok, we found a frame. Store all the values.
6801 last_mouse_glyph is a rectangle used to reduce the
6802 generation of mouse events. To not miss any motion
6803 events, we must divide the frame into rectangles of the
6804 size of the smallest character that could be displayed
6805 on it, i.e. into the same rectangles that matrices on
6806 the frame are divided into. */
6807
6808#if OLD_REDISPLAY_CODE
2b5c9e71 6809 int ignore1, ignore2;
2b5c9e71 6810 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
334208b7 6811 &last_mouse_glyph,
1cf412ec
RS
6812 FRAME_X_DISPLAY_INFO (f1)->grabbed
6813 || insist);
06a2c219
GM
6814#else
6815 {
6816 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
6817 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
6818 int x = win_x;
6819 int y = win_y;
6820
6821 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
6822 round down even for negative values. */
6823 if (x < 0)
6824 x -= width - 1;
6825 if (y < 0)
6826 y -= height - 1;
6827
6828 last_mouse_glyph.width = width;
6829 last_mouse_glyph.height = height;
6830 last_mouse_glyph.x = (x + width - 1) / width * width;
6831 last_mouse_glyph.y = (y + height - 1) / height * height;
6832 }
6833#endif
12ba150f
JB
6834
6835 *bar_window = Qnil;
6836 *part = 0;
334208b7 6837 *fp = f1;
e0c1aef2
KH
6838 XSETINT (*x, win_x);
6839 XSETINT (*y, win_y);
12ba150f
JB
6840 *time = last_mouse_movement_time;
6841 }
6842 }
6843 }
90e65f07
JB
6844
6845 UNBLOCK_INPUT;
6846}
f451eb13 6847
06a2c219
GM
6848
6849DEFUN ("xt-process-timeouts", Fxt_process_timeouts, Sxt_process_timeouts,
6850 0, 0, 0,
6851 "Arrange for Xt timeout callbacks to be called.")
6852 ()
6853{
6854#ifdef USE_X_TOOLKIT
6855 BLOCK_INPUT;
6856 while (XtAppPending (Xt_app_con) & XtIMTimer)
6857 XtAppProcessEvent (Xt_app_con, XtIMTimer);
6858 UNBLOCK_INPUT;
6859#endif /* USE_X_TOOLKIT */
6860
6861 return Qnil;
6862}
6863
6864
6865\f
6866/* Scroll bar support. */
6867
6868/* Given an X window ID, find the struct scroll_bar which manages it.
6869 This can be called in GC, so we have to make sure to strip off mark
6870 bits. */
6871static struct scroll_bar *
6872x_window_to_scroll_bar (window_id)
6873 Window window_id;
6874{
6875 Lisp_Object tail;
6876
6877 for (tail = Vframe_list;
6878 XGCTYPE (tail) == Lisp_Cons;
6879 tail = XCONS (tail)->cdr)
6880 {
6881 Lisp_Object frame, bar, condemned;
6882
6883 frame = XCONS (tail)->car;
6884 /* All elements of Vframe_list should be frames. */
6885 if (! GC_FRAMEP (frame))
6886 abort ();
6887
6888 /* Scan this frame's scroll bar list for a scroll bar with the
6889 right window ID. */
6890 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
6891 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
6892 /* This trick allows us to search both the ordinary and
6893 condemned scroll bar lists with one loop. */
6894 ! GC_NILP (bar) || (bar = condemned,
6895 condemned = Qnil,
6896 ! GC_NILP (bar));
6897 bar = XSCROLL_BAR (bar)->next)
6898 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
6899 return XSCROLL_BAR (bar);
6900 }
6901
6902 return 0;
6903}
6904
6905
6906\f
6907/************************************************************************
6908 Toolkit scroll bars
6909 ************************************************************************/
6910
6911#if USE_TOOLKIT_SCROLL_BARS
6912
6913static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
6914static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
6915static void x_create_toolkit_scroll_bar P_ ((struct frame *,
6916 struct scroll_bar *));
6917static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
6918 int, int, int));
6919
6920
6921/* Id of action hook installed for scroll bars. */
6922
6923static XtActionHookId action_hook_id;
6924
6925/* Lisp window being scrolled. Set when starting to interact with
6926 a toolkit scroll bar, reset to nil when ending the interaction. */
6927
6928static Lisp_Object window_being_scrolled;
6929
6930/* Last scroll bar part sent in xm_scroll_callback. */
6931
6932static int last_scroll_bar_part;
6933
6934
6935/* Action hook installed via XtAppAddActionHook when toolkit scroll
6936 bars are used.. The hoos is responsible for detecting when
6937 the user ends an interaction with the scroll bar, and generates
6938 a `end-scroll' scroll_bar_click' event if so. */
6939
6940static void
6941xt_action_hook (widget, client_data, action_name, event, params,
6942 num_params)
6943 Widget widget;
6944 XtPointer client_data;
6945 String action_name;
6946 XEvent *event;
6947 String *params;
6948 Cardinal *num_params;
6949{
6950 int scroll_bar_p;
6951 char *end_action;
6952
6953#ifdef USE_MOTIF
6954 scroll_bar_p = XmIsScrollBar (widget);
6955 end_action = "Release";
6956#elif defined HAVE_XAW3D
6957 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
6958 end_action = "EndScroll";
6959#else
6960#error unknown scroll bar toolkit
6961#endif /* HAVE_XAW3D */
6962
6963 /* Although LessTif uses XtTimeouts like Xaw3d, the timer hack to
6964 let Xt timeouts be processed doesn't work. */
6965 if (scroll_bar_p
6966 && strcmp (action_name, end_action) == 0
6967 && WINDOWP (window_being_scrolled))
6968 {
6969 struct window *w;
6970
6971 x_send_scroll_bar_event (window_being_scrolled,
6972 scroll_bar_end_scroll, 0, 0);
6973 w = XWINDOW (window_being_scrolled);
6974 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
6975 window_being_scrolled = Qnil;
6976 last_scroll_bar_part = -1;
6977 }
6978}
6979
6980
6981/* Send a client message with message type Xatom_Scrollbar for a
6982 scroll action to the frame of WINDOW. PART is a value identifying
6983 the part of the scroll bar that was clicked on. PORTION is the
6984 amount to scroll of a whole of WHOLE. */
6985
6986static void
6987x_send_scroll_bar_event (window, part, portion, whole)
6988 Lisp_Object window;
6989 int part, portion, whole;
6990{
6991 XEvent event;
6992 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
6993 struct frame *f = XFRAME (XWINDOW (window)->frame);
6994
6995 /* Construct a ClientMessage event to send to the frame. */
6996 ev->type = ClientMessage;
6997 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
6998 ev->display = FRAME_X_DISPLAY (f);
6999 ev->window = FRAME_X_WINDOW (f);
7000 ev->format = 32;
7001 ev->data.l[0] = (long) window;
7002 ev->data.l[1] = (long) part;
7003 ev->data.l[2] = (long) 0;
7004 ev->data.l[3] = (long) portion;
7005 ev->data.l[4] = (long) whole;
7006
7007 /* Setting the event mask to zero means that the message will
7008 be sent to the client that created the window, and if that
7009 window no longer exists, no event will be sent. */
7010 BLOCK_INPUT;
7011 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7012 UNBLOCK_INPUT;
7013}
7014
7015
7016/* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7017 in *IEVENT. */
7018
7019static void
7020x_scroll_bar_to_input_event (event, ievent)
7021 XEvent *event;
7022 struct input_event *ievent;
7023{
7024 XClientMessageEvent *ev = (XClientMessageEvent *) event;
7025 Lisp_Object window = (Lisp_Object) ev->data.l[0];
7026 struct frame *f = XFRAME (XWINDOW (window)->frame);
7027
7028 ievent->kind = scroll_bar_click;
7029 ievent->frame_or_window = window;
7030 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7031 ievent->part = ev->data.l[1];
7032 ievent->code = ev->data.l[2];
7033 ievent->x = make_number ((int) ev->data.l[3]);
7034 ievent->y = make_number ((int) ev->data.l[4]);
7035 ievent->modifiers = 0;
7036}
7037
7038
7039#ifdef USE_MOTIF
7040
7041/* Minimum and maximum values used for Motif scroll bars. */
7042
7043#define XM_SB_MIN 1
7044#define XM_SB_MAX 10000000
7045#define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7046
7047
7048/* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7049 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7050 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7051
7052static void
7053xm_scroll_callback (widget, client_data, call_data)
7054 Widget widget;
7055 XtPointer client_data, call_data;
7056{
7057 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7058 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7059 double percent;
7060 int part = -1, whole = 0, portion = 0;
7061
7062 switch (cs->reason)
7063 {
7064 case XmCR_DECREMENT:
7065 bar->dragging = Qnil;
7066 part = scroll_bar_up_arrow;
7067 break;
7068
7069 case XmCR_INCREMENT:
7070 bar->dragging = Qnil;
7071 part = scroll_bar_down_arrow;
7072 break;
7073
7074 case XmCR_PAGE_DECREMENT:
7075 bar->dragging = Qnil;
7076 part = scroll_bar_above_handle;
7077 break;
7078
7079 case XmCR_PAGE_INCREMENT:
7080 bar->dragging = Qnil;
7081 part = scroll_bar_below_handle;
7082 break;
7083
7084 case XmCR_TO_TOP:
7085 bar->dragging = Qnil;
7086 part = scroll_bar_to_top;
7087 break;
7088
7089 case XmCR_TO_BOTTOM:
7090 bar->dragging = Qnil;
7091 part = scroll_bar_to_bottom;
7092 break;
7093
7094 case XmCR_DRAG:
7095 {
7096 int slider_size;
7097 int dragging_down_p = (INTEGERP (bar->dragging)
7098 && XINT (bar->dragging) <= cs->value);
7099
7100 /* Get the slider size. */
7101 BLOCK_INPUT;
7102 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7103 UNBLOCK_INPUT;
7104
7105 /* At the max position of the scroll bar, do a line-wise
7106 movement. Without doing anything, the LessTif scroll bar
7107 calls us with the same cs->value again and again. If we
7108 want to make sure that we can reach the end of the buffer,
7109 we have to do something.
7110
7111 Implementation note: setting bar->dragging always to
7112 cs->value gives a smoother movement at the max position.
7113 Setting it to nil when doing line-wise movement gives
7114 a better slider behavior. */
7115
7116 if (cs->value + slider_size == XM_SB_MAX
7117 || (dragging_down_p
7118 && last_scroll_bar_part == scroll_bar_down_arrow))
7119 {
7120 part = scroll_bar_down_arrow;
7121 bar->dragging = Qnil;
7122 }
7123 else
7124 {
7125 whole = XM_SB_RANGE;
7126 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7127 part = scroll_bar_handle;
7128 bar->dragging = make_number (cs->value);
7129 }
7130 }
7131 break;
7132
7133 case XmCR_VALUE_CHANGED:
7134 break;
7135 };
7136
7137 if (part >= 0)
7138 {
7139 window_being_scrolled = bar->window;
7140 last_scroll_bar_part = part;
7141 x_send_scroll_bar_event (bar->window, part, portion, whole);
7142 }
7143}
7144
7145
7146#else /* not USE_MOTIF, i.e. XAW3D. */
7147
7148
7149/* Xaw3d scroll bar callback. Invoked when the thumb is dragged.
7150 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7151 scroll bar struct. CALL_DATA is a pointer to a float saying where
7152 the thumb is. */
7153
7154static void
7155xaw3d_jump_callback (widget, client_data, call_data)
7156 Widget widget;
7157 XtPointer client_data, call_data;
7158{
7159 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7160 float top = *(float *) call_data;
7161 float shown;
7162 int whole, portion;
7163 int dragging_down_p, part;
7164 double epsilon = 0.01;
7165
7166 /* Get the size of the thumb, a value between 0 and 1. */
7167 BLOCK_INPUT;
7168 XtVaGetValues (widget, XtNshown, &shown, NULL);
7169 UNBLOCK_INPUT;
7170
7171 whole = 10000000;
7172 portion = shown < 1 ? top * whole : 0;
7173 dragging_down_p = (INTEGERP (bar->dragging)
7174 && XINT (bar->dragging) < portion);
7175
7176 if (shown < 1
7177 && (abs (top + shown - 1) < epsilon
7178 || (dragging_down_p
7179 && last_scroll_bar_part == scroll_bar_down_arrow)))
7180 part = scroll_bar_down_arrow;
7181 else
7182 part = scroll_bar_handle;
7183
7184 window_being_scrolled = bar->window;
7185 bar->dragging = make_number (portion);
7186 last_scroll_bar_part = part;
7187 x_send_scroll_bar_event (bar->window, part, portion, whole);
7188}
7189
7190
7191/* Xaw3d scroll bar callback. Invoked for incremental scrolling.,
7192 i.e. line or page up or down. WIDGET is the Xaw3d scroll bar
7193 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
7194 the scroll bar. CALL_DATA is an integer specifying the action that
7195 has taken place. It's magnitude is in the range 0..height of the
7196 scroll bar. Negative values mean scroll towards buffer start.
7197 Values < height of scroll bar mean line-wise movement. */
7198
7199static void
7200xaw3d_scroll_callback (widget, client_data, call_data)
7201 Widget widget;
7202 XtPointer client_data, call_data;
7203{
7204 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7205 int position = (int) call_data;
7206 Dimension height;
7207 int part;
7208
7209 /* Get the height of the scroll bar. */
7210 BLOCK_INPUT;
7211 XtVaGetValues (widget, XtNheight, &height, NULL);
7212 UNBLOCK_INPUT;
7213
7214 if (position < 0)
7215 {
7216 if (abs (position) < height)
7217 part = scroll_bar_up_arrow;
7218 else
7219 part = scroll_bar_above_handle;
7220 }
7221 else
7222 {
7223 if (abs (position) < height)
7224 part = scroll_bar_down_arrow;
7225 else
7226 part = scroll_bar_below_handle;
7227 }
7228
7229 window_being_scrolled = bar->window;
7230 bar->dragging = Qnil;
7231 last_scroll_bar_part = part;
7232 x_send_scroll_bar_event (bar->window, part, 0, 0);
7233}
7234
7235
7236#endif /* not USE_MOTIF */
7237
7238
7239/* Create the widget for scroll bar BAR on frame F. Record the widget
7240 and X window of the scroll bar in BAR. */
7241
7242static void
7243x_create_toolkit_scroll_bar (f, bar)
7244 struct frame *f;
7245 struct scroll_bar *bar;
7246{
7247 Window xwindow;
7248 Widget widget;
7249 Arg av[20];
7250 int ac = 0;
7251 char *scroll_bar_name = "verticalScrollBar";
7252 unsigned long pixel;
7253
7254 BLOCK_INPUT;
7255
7256#ifdef USE_MOTIF
7257 /* LessTif 0.85, problems:
7258
7259 1. When the mouse if over the scroll bar, the scroll bar will
7260 get keyboard events. I didn't find a way to turn this off.
7261
7262 2. Do we have to explicitly set the cursor to get an arrow
7263 cursor (see below)? */
7264
7265 /* Set resources. Create the widget. */
7266 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7267 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
7268 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
7269 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
7270 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
7271 XtSetArg (av[ac], XmNincrement, 1); ++ac;
7272 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
7273
7274 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7275 if (pixel != -1)
7276 {
7277 XtSetArg (av[ac], XmNforeground, pixel);
7278 ++ac;
7279 }
7280
7281 pixel = f->output_data.x->scroll_bar_background_pixel;
7282 if (pixel != -1)
7283 {
7284 XtSetArg (av[ac], XmNbackground, pixel);
7285 ++ac;
7286 }
7287
7288 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
7289 scroll_bar_name, av, ac);
7290
7291 /* Add one callback for everything that can happen. */
7292 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
7293 (XtPointer) bar);
7294 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
7295 (XtPointer) bar);
7296 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
7297 (XtPointer) bar);
7298 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
7299 (XtPointer) bar);
7300 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
7301 (XtPointer) bar);
7302 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
7303 (XtPointer) bar);
7304 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
7305 (XtPointer) bar);
7306
7307 /* Realize the widget. Only after that is the X window created. */
7308 XtRealizeWidget (widget);
7309
7310 /* Set the cursor to an arrow. I didn't find a resource to do that.
7311 And I'm wondering why it hasn't an arrow cursor by default. */
7312 XDefineCursor (XtDisplay (widget), XtWindow (widget),
7313 f->output_data.x->nontext_cursor);
7314
7315#elif defined HAVE_XAW3D
7316
7317 /* Set resources. Create the widget. The background of the
7318 Xaw3d scroll bar widget is a little bit light for my taste.
7319 We don't alter it here to let users change it according
7320 to their taste with `emacs*verticalScrollBar.background: xxx'. */
7321 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7322 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
7323 XtSetArg (av[ac], XtNcursorName, "left_ptr"); ++ac;
7324 XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac;
7325
7326 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7327 if (pixel != -1)
7328 {
7329 XtSetArg (av[ac], XtNforeground, pixel);
7330 ++ac;
7331 }
7332
7333 pixel = f->output_data.x->scroll_bar_background_pixel;
7334 if (pixel != -1)
7335 {
7336 XtSetArg (av[ac], XtNbackground, pixel);
7337 ++ac;
7338 }
7339
7340 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
7341 f->output_data.x->edit_widget, av, ac);
7342
7343 /* Define callbacks. */
7344 XtAddCallback (widget, XtNjumpProc, xaw3d_jump_callback, (XtPointer) bar);
7345 XtAddCallback (widget, XtNscrollProc, xaw3d_scroll_callback,
7346 (XtPointer) bar);
7347
7348 /* Realize the widget. Only after that is the X window created. */
7349 XtRealizeWidget (widget);
7350
7351#endif /* HAVE_XAW3D */
7352
7353 /* Install an action hook that let's us detect when the user
7354 finishes interacting with a scroll bar. */
7355 if (action_hook_id == 0)
7356 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
7357
7358 /* Remember X window and widget in the scroll bar vector. */
7359 SET_SCROLL_BAR_X_WIDGET (bar, widget);
7360 xwindow = XtWindow (widget);
7361 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
7362
7363 UNBLOCK_INPUT;
7364}
7365
7366
7367/* Set the thumb size and position of scroll bar BAR. We are currently
7368 displaying PORTION out of a whole WHOLE, and our position POSITION. */
7369
7370static void
7371x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
7372 struct scroll_bar *bar;
7373 int portion, position, whole;
f451eb13 7374{
06a2c219
GM
7375 float top, shown;
7376 Arg av[2];
7377 Widget widget = SCROLL_BAR_X_WIDGET (bar);
f451eb13 7378
06a2c219
GM
7379 if (whole == 0)
7380 top = 0, shown = 1;
7381 else
f451eb13 7382 {
06a2c219
GM
7383 top = (float) position / whole;
7384 shown = (float) portion / whole;
7385 }
f451eb13 7386
06a2c219 7387 BLOCK_INPUT;
f451eb13 7388
06a2c219
GM
7389#ifdef USE_MOTIF
7390 {
7391 int size, value;
7392 Boolean arrow1_selected, arrow2_selected;
7393 unsigned char flags;
7394 XmScrollBarWidget sb;
7395
7396 /* Slider size. Must be in the range [1 .. MAX - MIN] where NAX
7397 is the scroll bar's maximum and MIN is the scroll bar's minimum
7398 value. */
7399 size = shown * XM_SB_RANGE;
7400 size = min (size, XM_SB_RANGE);
7401 size = max (size, 1);
7402
7403 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
7404 value = top * XM_SB_RANGE;
7405 value = min (value, XM_SB_MAX - size);
7406 value = max (value, XM_SB_MIN);
7407
7408 /* LessTif: Calling XmScrollBarSetValues after an increment or
7409 decrement turns off auto-repeat LessTif-internally. This can
7410 be seen in ScrollBar.c which resets Arrow1Selected and
7411 Arrow2Selected. It also sets internal flags so that LessTif
7412 believes the mouse is in the slider. We either have to change
7413 our code, or work around that by accessing private data. */
7414
7415 sb = (XmScrollBarWidget) widget;
7416 arrow1_selected = sb->scrollBar.arrow1_selected;
7417 arrow2_selected = sb->scrollBar.arrow2_selected;
7418 flags = sb->scrollBar.flags;
7419
7420 if (NILP (bar->dragging))
7421 XmScrollBarSetValues (widget, value, size, 0, 0, False);
7422 else if (last_scroll_bar_part == scroll_bar_down_arrow)
7423 /* This has the negative side effect that the slider value is
7424 not would it would be if we scrolled here using line-wise or
7425 page-wise movement. */
7426 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
7427 else
7428 {
7429 /* If currently dragging, only update the slider size.
7430 This reduces flicker effects. */
7431 int old_value, old_size, increment, page_increment;
7432
7433 XmScrollBarGetValues (widget, &old_value, &old_size,
7434 &increment, &page_increment);
7435 XmScrollBarSetValues (widget, old_value,
7436 min (size, XM_SB_RANGE - old_value),
7437 0, 0, False);
7438 }
7439
7440 sb->scrollBar.arrow1_selected = arrow1_selected;
7441 sb->scrollBar.arrow2_selected = arrow2_selected;
7442 sb->scrollBar.flags = flags;
7443 }
7444#elif defined HAVE_XAW3D
7445 {
7446 /* Restrict to [0 1]. */
7447 top = max (0, min (1, top));
7448 shown = max (0, min (1, shown));
7449
7450 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
7451 check that your system's configuration file contains a define
7452 for `NARROWPROTO'. See s/freebsd.h for an example. */
7453 if (NILP (bar->dragging))
7454 XawScrollbarSetThumb (widget, top, shown);
7455 else
7456 {
7457 ScrollbarWidget sb = (ScrollbarWidget) widget;
7458 int scroll_mode = sb->scrollbar.scroll_mode;
f451eb13 7459
06a2c219
GM
7460 sb->scrollbar.scroll_mode = 0;
7461
7462 if (last_scroll_bar_part == scroll_bar_down_arrow)
7463 XawScrollbarSetThumb (widget, top, 1 - top);
7464 else
7465 {
7466 float old_top;
7467 XtVaGetValues (widget, XtNtopOfThumb, &old_top, NULL);
7468 XawScrollbarSetThumb (widget, old_top, min (shown, 1 - old_top));
7469 }
7470
7471 sb->scrollbar.scroll_mode = scroll_mode;
7472 }
7473 }
7474#endif /* HAVE_XAW3D */
7475
7476 UNBLOCK_INPUT;
f451eb13
JB
7477}
7478
06a2c219
GM
7479#endif /* USE_TOOLKIT_SCROLL_BARS */
7480
7481
7482\f
7483/************************************************************************
7484 Scroll bars, general
7485 ************************************************************************/
7486
7487/* Create a scroll bar and return the scroll bar vector for it. W is
7488 the Emacs window on which to create the scroll bar. TOP, LEFT,
7489 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
7490 scroll bar. */
7491
ab648270 7492static struct scroll_bar *
06a2c219
GM
7493x_scroll_bar_create (w, top, left, width, height)
7494 struct window *w;
f451eb13
JB
7495 int top, left, width, height;
7496{
06a2c219
GM
7497 struct frame *f = XFRAME (w->frame);
7498#ifdef USE_X_TOOLKIT
7499 Arg av[10];
7500#endif
7501 int ac = 0;
7502 Window window;
334208b7
RS
7503 struct scroll_bar *bar
7504 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
f451eb13
JB
7505
7506 BLOCK_INPUT;
7507
06a2c219
GM
7508#if USE_TOOLKIT_SCROLL_BARS
7509 x_create_toolkit_scroll_bar (f, bar);
7510#else /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
7511 {
7512 XSetWindowAttributes a;
7513 unsigned long mask;
06a2c219
GM
7514
7515 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
7516 if (a.background_pixel == -1)
7517 a.background_pixel = f->output_data.x->background_pixel;
7518
12ba150f 7519 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9a572e2a 7520 | ButtonMotionMask | PointerMotionHintMask
12ba150f 7521 | ExposureMask);
7a13e894 7522 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
f451eb13 7523
dbc4e1c1 7524 mask = (CWBackPixel | CWEventMask | CWCursor);
f451eb13 7525
06a2c219
GM
7526 /* Clear the area of W that will serve as a scroll bar. This is
7527 for the case that a window has been split horizontally. In
7528 this case, no clear_frame is generated to reduce flickering. */
7529 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7530 left, top, width,
7531 window_box_height (w), False);
7532
7533 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7534 /* Position and size of scroll bar. */
7535 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7536 top,
7537 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
7538 height,
7539 /* Border width, depth, class, and visual. */
7540 0,
7541 CopyFromParent,
7542 CopyFromParent,
7543 CopyFromParent,
7544 /* Attributes. */
7545 mask, &a);
7546 SET_SCROLL_BAR_X_WINDOW (bar, window);
f451eb13 7547 }
06a2c219 7548#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 7549
06a2c219 7550 XSETWINDOW (bar->window, w);
e0c1aef2
KH
7551 XSETINT (bar->top, top);
7552 XSETINT (bar->left, left);
7553 XSETINT (bar->width, width);
7554 XSETINT (bar->height, height);
7555 XSETINT (bar->start, 0);
7556 XSETINT (bar->end, 0);
12ba150f 7557 bar->dragging = Qnil;
f451eb13
JB
7558
7559 /* Add bar to its frame's list of scroll bars. */
334208b7 7560 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 7561 bar->prev = Qnil;
334208b7 7562 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
06a2c219 7563 if (!NILP (bar->next))
e0c1aef2 7564 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13 7565
06a2c219
GM
7566 /* Map the window/widget. */
7567#if USE_TOOLKIT_SCROLL_BARS
7568 XtMapWidget (SCROLL_BAR_X_WIDGET (bar));
7569 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
7570 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7571 top,
7572 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
7573 height, 0);
7574#else /* not USE_TOOLKIT_SCROLL_BARS */
7f9c7f94 7575 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
06a2c219 7576#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
7577
7578 UNBLOCK_INPUT;
12ba150f 7579 return bar;
f451eb13
JB
7580}
7581
06a2c219 7582
12ba150f 7583/* Draw BAR's handle in the proper position.
06a2c219 7584
12ba150f
JB
7585 If the handle is already drawn from START to END, don't bother
7586 redrawing it, unless REBUILD is non-zero; in that case, always
7587 redraw it. (REBUILD is handy for drawing the handle after expose
58769bee 7588 events.)
12ba150f
JB
7589
7590 Normally, we want to constrain the start and end of the handle to
06a2c219
GM
7591 fit inside its rectangle, but if the user is dragging the scroll
7592 bar handle, we want to let them drag it down all the way, so that
7593 the bar's top is as far down as it goes; otherwise, there's no way
7594 to move to the very end of the buffer. */
7595
f451eb13 7596static void
ab648270
JB
7597x_scroll_bar_set_handle (bar, start, end, rebuild)
7598 struct scroll_bar *bar;
f451eb13 7599 int start, end;
12ba150f 7600 int rebuild;
f451eb13 7601{
06a2c219 7602#ifndef USE_TOOLKIT_SCROLL_BARS
12ba150f 7603 int dragging = ! NILP (bar->dragging);
ab648270 7604 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 7605 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 7606 GC gc = f->output_data.x->normal_gc;
12ba150f
JB
7607
7608 /* If the display is already accurate, do nothing. */
7609 if (! rebuild
7610 && start == XINT (bar->start)
7611 && end == XINT (bar->end))
7612 return;
7613
f451eb13
JB
7614 BLOCK_INPUT;
7615
7616 {
d9cdbb3d
RS
7617 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
7618 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
7619 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
f451eb13
JB
7620
7621 /* Make sure the values are reasonable, and try to preserve
7622 the distance between start and end. */
12ba150f
JB
7623 {
7624 int length = end - start;
7625
7626 if (start < 0)
7627 start = 0;
7628 else if (start > top_range)
7629 start = top_range;
7630 end = start + length;
7631
7632 if (end < start)
7633 end = start;
7634 else if (end > top_range && ! dragging)
7635 end = top_range;
7636 }
f451eb13 7637
ab648270 7638 /* Store the adjusted setting in the scroll bar. */
e0c1aef2
KH
7639 XSETINT (bar->start, start);
7640 XSETINT (bar->end, end);
f451eb13 7641
12ba150f
JB
7642 /* Clip the end position, just for display. */
7643 if (end > top_range)
7644 end = top_range;
f451eb13 7645
ab648270 7646 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
12ba150f
JB
7647 below top positions, to make sure the handle is always at least
7648 that many pixels tall. */
ab648270 7649 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
f451eb13 7650
12ba150f
JB
7651 /* Draw the empty space above the handle. Note that we can't clear
7652 zero-height areas; that means "clear to end of window." */
7653 if (0 < start)
334208b7 7654 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 7655
12ba150f 7656 /* x, y, width, height, and exposures. */
ab648270
JB
7657 VERTICAL_SCROLL_BAR_LEFT_BORDER,
7658 VERTICAL_SCROLL_BAR_TOP_BORDER,
12ba150f
JB
7659 inside_width, start,
7660 False);
f451eb13 7661
06a2c219
GM
7662 /* Change to proper foreground color if one is specified. */
7663 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
7664 XSetForeground (FRAME_X_DISPLAY (f), gc,
7665 f->output_data.x->scroll_bar_foreground_pixel);
7666
12ba150f 7667 /* Draw the handle itself. */
334208b7 7668 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13 7669
12ba150f 7670 /* x, y, width, height */
ab648270
JB
7671 VERTICAL_SCROLL_BAR_LEFT_BORDER,
7672 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
12ba150f 7673 inside_width, end - start);
f451eb13 7674
06a2c219
GM
7675 /* Restore the foreground color of the GC if we changed it above. */
7676 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
7677 XSetForeground (FRAME_X_DISPLAY (f), gc,
7678 f->output_data.x->foreground_pixel);
f451eb13 7679
12ba150f
JB
7680 /* Draw the empty space below the handle. Note that we can't
7681 clear zero-height areas; that means "clear to end of window." */
7682 if (end < inside_height)
334208b7 7683 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 7684
12ba150f 7685 /* x, y, width, height, and exposures. */
ab648270
JB
7686 VERTICAL_SCROLL_BAR_LEFT_BORDER,
7687 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
12ba150f
JB
7688 inside_width, inside_height - end,
7689 False);
f451eb13 7690
f451eb13
JB
7691 }
7692
f451eb13 7693 UNBLOCK_INPUT;
06a2c219 7694#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
7695}
7696
f451eb13 7697
06a2c219
GM
7698/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
7699 nil. */
58769bee 7700
12ba150f 7701static void
ab648270
JB
7702x_scroll_bar_remove (bar)
7703 struct scroll_bar *bar;
12ba150f
JB
7704{
7705 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7706
7707 BLOCK_INPUT;
7708
06a2c219
GM
7709#if USE_TOOLKIT_SCROLL_BARS
7710 XtDestroyWidget (SCROLL_BAR_X_WIDGET (bar));
7711#else /* not USE_TOOLKIT_SCROLL_BARS */
334208b7 7712 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
06a2c219
GM
7713#endif /* not USE_TOOLKIT_SCROLL_BARS */
7714
ab648270
JB
7715 /* Disassociate this scroll bar from its window. */
7716 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
12ba150f
JB
7717
7718 UNBLOCK_INPUT;
7719}
7720
06a2c219 7721
12ba150f
JB
7722/* Set the handle of the vertical scroll bar for WINDOW to indicate
7723 that we are displaying PORTION characters out of a total of WHOLE
ab648270 7724 characters, starting at POSITION. If WINDOW has no scroll bar,
12ba150f 7725 create one. */
06a2c219 7726
12ba150f 7727static void
06a2c219
GM
7728XTset_vertical_scroll_bar (w, portion, whole, position)
7729 struct window *w;
f451eb13
JB
7730 int portion, whole, position;
7731{
06a2c219 7732 struct frame *f = XFRAME (w->frame);
ab648270 7733 struct scroll_bar *bar;
06a2c219
GM
7734 int pixel_top, pixel_left, pixel_width, pixel_height;
7735 int window_x, window_y, window_width, window_height;
7736 int scroll_bar_area_width;
7737
7738 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
7739
7740 /* Where should this scroll bar be, pixel-wise? */
7741 pixel_top = window_y;
7742 pixel_height = window_height;
7743
7744 /* The width of the scroll bar itself. */
7745 pixel_width = (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
7746 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7747 : (FRAME_SCROLL_BAR_COLS (f)
7748 * FONT_WIDTH (FRAME_FONT (f))));
7749
7750 /* The width on the screen reserved for the scroll bar plus maybe
7751 some empty room at both sides of the scroll bar. */
7752 scroll_bar_area_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
7753
7754 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
7755 pixel_left = (window_x
7756 + window_width
7757 + FRAME_FLAGS_AREA_WIDTH (f)
7758 + scroll_bar_area_width
7759 - pixel_width + 1);
7760 else
7761 pixel_left = (window_x
7762 - FRAME_FLAGS_AREA_WIDTH (f)
7763 - scroll_bar_area_width);
12ba150f 7764
ab648270 7765 /* Does the scroll bar exist yet? */
06a2c219
GM
7766 if (NILP (w->vertical_scroll_bar))
7767 bar = x_scroll_bar_create (w, pixel_top, pixel_left, pixel_width,
7768 pixel_height);
f451eb13 7769 else
12ba150f
JB
7770 {
7771 /* It may just need to be moved and resized. */
06a2c219
GM
7772 unsigned int mask = 0;
7773
7774 bar = XSCROLL_BAR (w->vertical_scroll_bar);
7775
7776 BLOCK_INPUT;
7777
7778 if (pixel_left != XINT (bar->left))
7779 mask |= CWX;
7780 if (pixel_top != XINT (bar->top))
7781 mask |= CWY;
7782 if (pixel_width != XINT (bar->width))
7783 mask |= CWWidth;
7784 if (pixel_height != XINT (bar->height))
7785 mask |= CWHeight;
7786
7787#ifdef USE_TOOLKIT_SCROLL_BARS
7788
7789 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
7790 {
7791 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7792 pixel_left + pixel_width - scroll_bar_area_width,
7793 pixel_top,
7794 (scroll_bar_area_width
7795 - pixel_width
7796 + VERTICAL_SCROLL_BAR_WIDTH_TRIM),
7797 pixel_height, False);
7798 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7799 (pixel_left
7800 + pixel_width
7801 - VERTICAL_SCROLL_BAR_WIDTH_TRIM),
7802 pixel_top,
7803 VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7804 pixel_height, False);
7805 }
7806 else
7807 {
7808 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7809 pixel_left, pixel_top,
7810 VERTICAL_SCROLL_BAR_WIDTH_TRIM, pixel_height, False);
7811 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7812 (pixel_left
7813 + pixel_width
7814 - VERTICAL_SCROLL_BAR_WIDTH_TRIM),
7815 pixel_top,
7816 (scroll_bar_area_width
7817 - pixel_width
7818 + VERTICAL_SCROLL_BAR_WIDTH_TRIM),
7819 pixel_height, False);
7820 }
7821
7822 /* Move/size the scroll bar widget. */
7823 if (mask)
7824 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
7825 pixel_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7826 pixel_top,
7827 pixel_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
7828 pixel_height, 0);
7829
7830#else /* not USE_TOOLKIT_SCROLL_BARS */
7831
7832 /* Clear areas not covered by the scroll bar. This makes sure a
7833 previous mode line display is cleared after C-x 2 C-x 1, for
7834 example. Non-toolkit scroll bars are as wide as the area
7835 reserved for scroll bars - trim at both sides. */
7836 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7837 pixel_left, pixel_top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7838 pixel_height, False);
7839 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7840 (pixel_left
7841 + pixel_width
7842 - VERTICAL_SCROLL_BAR_WIDTH_TRIM),
7843 pixel_top,
7844 VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7845 pixel_height, False);
7846
7847 /* Move/size the scroll bar window. */
7848 if (mask)
7849 {
7850 XWindowChanges wc;
7851
7852 wc.x = pixel_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
7853 wc.y = pixel_top;
7854 wc.width = pixel_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
7855 wc.height = pixel_height;
7856 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
7857 mask, &wc);
7858 }
7859
7860#endif /* not USE_TOOLKIT_SCROLL_BARS */
7861
7862 /* Remember new settings. */
7863 XSETINT (bar->left, pixel_left);
7864 XSETINT (bar->top, pixel_top);
7865 XSETINT (bar->width, pixel_width);
7866 XSETINT (bar->height, pixel_height);
7867
7868 UNBLOCK_INPUT;
12ba150f 7869 }
f451eb13 7870
06a2c219
GM
7871#if USE_TOOLKIT_SCROLL_BARS
7872 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
7873#else /* not USE_TOOLKIT_SCROLL_BARS */
ab648270 7874 /* Set the scroll bar's current state, unless we're currently being
f451eb13 7875 dragged. */
12ba150f 7876 if (NILP (bar->dragging))
f451eb13 7877 {
d9cdbb3d 7878 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, pixel_height);
f451eb13 7879
12ba150f 7880 if (whole == 0)
ab648270 7881 x_scroll_bar_set_handle (bar, 0, top_range, 0);
12ba150f
JB
7882 else
7883 {
43f868f5
JB
7884 int start = ((double) position * top_range) / whole;
7885 int end = ((double) (position + portion) * top_range) / whole;
ab648270 7886 x_scroll_bar_set_handle (bar, start, end, 0);
12ba150f 7887 }
f451eb13 7888 }
06a2c219 7889#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 7890
06a2c219 7891 XSETVECTOR (w->vertical_scroll_bar, bar);
f451eb13
JB
7892}
7893
12ba150f 7894
f451eb13 7895/* The following three hooks are used when we're doing a thorough
ab648270 7896 redisplay of the frame. We don't explicitly know which scroll bars
f451eb13 7897 are going to be deleted, because keeping track of when windows go
12ba150f
JB
7898 away is a real pain - "Can you say set-window-configuration, boys
7899 and girls?" Instead, we just assert at the beginning of redisplay
ab648270 7900 that *all* scroll bars are to be removed, and then save a scroll bar
12ba150f 7901 from the fiery pit when we actually redisplay its window. */
f451eb13 7902
ab648270
JB
7903/* Arrange for all scroll bars on FRAME to be removed at the next call
7904 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
06a2c219
GM
7905 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
7906
58769bee 7907static void
ab648270 7908XTcondemn_scroll_bars (frame)
f451eb13
JB
7909 FRAME_PTR frame;
7910{
f9e24cb9
RS
7911 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
7912 while (! NILP (FRAME_SCROLL_BARS (frame)))
7913 {
7914 Lisp_Object bar;
7915 bar = FRAME_SCROLL_BARS (frame);
7916 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
7917 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
7918 XSCROLL_BAR (bar)->prev = Qnil;
7919 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
7920 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
7921 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
7922 }
f451eb13
JB
7923}
7924
06a2c219 7925/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
12ba150f 7926 Note that WINDOW isn't necessarily condemned at all. */
f451eb13 7927static void
ab648270 7928XTredeem_scroll_bar (window)
12ba150f 7929 struct window *window;
f451eb13 7930{
ab648270 7931 struct scroll_bar *bar;
12ba150f 7932
ab648270
JB
7933 /* We can't redeem this window's scroll bar if it doesn't have one. */
7934 if (NILP (window->vertical_scroll_bar))
12ba150f
JB
7935 abort ();
7936
ab648270 7937 bar = XSCROLL_BAR (window->vertical_scroll_bar);
12ba150f
JB
7938
7939 /* Unlink it from the condemned list. */
7940 {
7941 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
7942
7943 if (NILP (bar->prev))
7944 {
7945 /* If the prev pointer is nil, it must be the first in one of
7946 the lists. */
ab648270 7947 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
12ba150f
JB
7948 /* It's not condemned. Everything's fine. */
7949 return;
ab648270
JB
7950 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
7951 window->vertical_scroll_bar))
7952 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
12ba150f
JB
7953 else
7954 /* If its prev pointer is nil, it must be at the front of
7955 one or the other! */
7956 abort ();
7957 }
7958 else
ab648270 7959 XSCROLL_BAR (bar->prev)->next = bar->next;
12ba150f
JB
7960
7961 if (! NILP (bar->next))
ab648270 7962 XSCROLL_BAR (bar->next)->prev = bar->prev;
12ba150f 7963
ab648270 7964 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 7965 bar->prev = Qnil;
e0c1aef2 7966 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
12ba150f 7967 if (! NILP (bar->next))
e0c1aef2 7968 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
12ba150f 7969 }
f451eb13
JB
7970}
7971
ab648270
JB
7972/* Remove all scroll bars on FRAME that haven't been saved since the
7973 last call to `*condemn_scroll_bars_hook'. */
06a2c219 7974
f451eb13 7975static void
ab648270 7976XTjudge_scroll_bars (f)
12ba150f 7977 FRAME_PTR f;
f451eb13 7978{
12ba150f 7979 Lisp_Object bar, next;
f451eb13 7980
ab648270 7981 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
cf7cb199
JB
7982
7983 /* Clear out the condemned list now so we won't try to process any
ab648270
JB
7984 more events on the hapless scroll bars. */
7985 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
cf7cb199
JB
7986
7987 for (; ! NILP (bar); bar = next)
f451eb13 7988 {
ab648270 7989 struct scroll_bar *b = XSCROLL_BAR (bar);
12ba150f 7990
ab648270 7991 x_scroll_bar_remove (b);
12ba150f
JB
7992
7993 next = b->next;
7994 b->next = b->prev = Qnil;
f451eb13 7995 }
12ba150f 7996
ab648270 7997 /* Now there should be no references to the condemned scroll bars,
12ba150f 7998 and they should get garbage-collected. */
f451eb13
JB
7999}
8000
8001
06a2c219
GM
8002/* Handle an Expose or GraphicsExpose event on a scroll bar. This
8003 is a no-op when using toolkit scroll bars.
ab648270
JB
8004
8005 This may be called from a signal handler, so we have to ignore GC
8006 mark bits. */
06a2c219 8007
f451eb13 8008static void
ab648270
JB
8009x_scroll_bar_expose (bar, event)
8010 struct scroll_bar *bar;
f451eb13
JB
8011 XEvent *event;
8012{
06a2c219
GM
8013#ifndef USE_TOOLKIT_SCROLL_BARS
8014
ab648270 8015 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8016 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8017 GC gc = f->output_data.x->normal_gc;
3cbd2e0b 8018 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
12ba150f 8019
f451eb13
JB
8020 BLOCK_INPUT;
8021
ab648270 8022 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
f451eb13 8023
06a2c219 8024 /* Draw a one-pixel border just inside the edges of the scroll bar. */
334208b7 8025 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13
JB
8026
8027 /* x, y, width, height */
d9cdbb3d 8028 0, 0,
3cbd2e0b 8029 XINT (bar->width) - 1 - width_trim - width_trim,
d9cdbb3d
RS
8030 XINT (bar->height) - 1);
8031
f451eb13 8032 UNBLOCK_INPUT;
06a2c219
GM
8033
8034#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8035}
8036
ab648270
JB
8037/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8038 is set to something other than no_event, it is enqueued.
8039
8040 This may be called from a signal handler, so we have to ignore GC
8041 mark bits. */
06a2c219 8042
f451eb13 8043static void
ab648270
JB
8044x_scroll_bar_handle_click (bar, event, emacs_event)
8045 struct scroll_bar *bar;
f451eb13
JB
8046 XEvent *event;
8047 struct input_event *emacs_event;
8048{
0299d313 8049 if (! GC_WINDOWP (bar->window))
12ba150f
JB
8050 abort ();
8051
ab648270 8052 emacs_event->kind = scroll_bar_click;
69388238 8053 emacs_event->code = event->xbutton.button - Button1;
0299d313
RS
8054 emacs_event->modifiers
8055 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8056 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8057 event->xbutton.state)
8058 | (event->type == ButtonRelease
8059 ? up_modifier
8060 : down_modifier));
12ba150f 8061 emacs_event->frame_or_window = bar->window;
f451eb13 8062 emacs_event->timestamp = event->xbutton.time;
12ba150f 8063 {
06a2c219 8064#if 0
d9cdbb3d 8065 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
0299d313 8066 int internal_height
d9cdbb3d 8067 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8068#endif
0299d313 8069 int top_range
d9cdbb3d 8070 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ab648270 8071 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
12ba150f
JB
8072
8073 if (y < 0) y = 0;
8074 if (y > top_range) y = top_range;
8075
8076 if (y < XINT (bar->start))
ab648270
JB
8077 emacs_event->part = scroll_bar_above_handle;
8078 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8079 emacs_event->part = scroll_bar_handle;
12ba150f 8080 else
ab648270 8081 emacs_event->part = scroll_bar_below_handle;
929787e1
JB
8082
8083 /* Just because the user has clicked on the handle doesn't mean
5116f055
JB
8084 they want to drag it. Lisp code needs to be able to decide
8085 whether or not we're dragging. */
929787e1 8086#if 0
12ba150f
JB
8087 /* If the user has just clicked on the handle, record where they're
8088 holding it. */
8089 if (event->type == ButtonPress
ab648270 8090 && emacs_event->part == scroll_bar_handle)
e0c1aef2 8091 XSETINT (bar->dragging, y - XINT (bar->start));
929787e1 8092#endif
12ba150f
JB
8093
8094 /* If the user has released the handle, set it to its final position. */
8095 if (event->type == ButtonRelease
8096 && ! NILP (bar->dragging))
8097 {
8098 int new_start = y - XINT (bar->dragging);
8099 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
f451eb13 8100
ab648270 8101 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
12ba150f
JB
8102 bar->dragging = Qnil;
8103 }
f451eb13 8104
5116f055
JB
8105 /* Same deal here as the other #if 0. */
8106#if 0
58769bee 8107 /* Clicks on the handle are always reported as occurring at the top of
12ba150f 8108 the handle. */
ab648270 8109 if (emacs_event->part == scroll_bar_handle)
12ba150f
JB
8110 emacs_event->x = bar->start;
8111 else
e0c1aef2 8112 XSETINT (emacs_event->x, y);
5116f055 8113#else
e0c1aef2 8114 XSETINT (emacs_event->x, y);
5116f055 8115#endif
f451eb13 8116
e0c1aef2 8117 XSETINT (emacs_event->y, top_range);
12ba150f
JB
8118 }
8119}
f451eb13 8120
ab648270
JB
8121/* Handle some mouse motion while someone is dragging the scroll bar.
8122
8123 This may be called from a signal handler, so we have to ignore GC
8124 mark bits. */
06a2c219 8125
f451eb13 8126static void
ab648270
JB
8127x_scroll_bar_note_movement (bar, event)
8128 struct scroll_bar *bar;
f451eb13
JB
8129 XEvent *event;
8130{
39d8bb4d
KH
8131 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8132
f451eb13
JB
8133 last_mouse_movement_time = event->xmotion.time;
8134
39d8bb4d 8135 f->mouse_moved = 1;
e0c1aef2 8136 XSETVECTOR (last_mouse_scroll_bar, bar);
f451eb13
JB
8137
8138 /* If we're dragging the bar, display it. */
ab648270 8139 if (! GC_NILP (bar->dragging))
f451eb13
JB
8140 {
8141 /* Where should the handle be now? */
12ba150f 8142 int new_start = event->xmotion.y - XINT (bar->dragging);
f451eb13 8143
12ba150f 8144 if (new_start != XINT (bar->start))
f451eb13 8145 {
12ba150f 8146 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
58769bee 8147
ab648270 8148 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
f451eb13
JB
8149 }
8150 }
f451eb13
JB
8151}
8152
12ba150f 8153/* Return information to the user about the current position of the mouse
ab648270 8154 on the scroll bar. */
06a2c219 8155
12ba150f 8156static void
334208b7
RS
8157x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8158 FRAME_PTR *fp;
12ba150f 8159 Lisp_Object *bar_window;
ab648270 8160 enum scroll_bar_part *part;
12ba150f
JB
8161 Lisp_Object *x, *y;
8162 unsigned long *time;
8163{
ab648270 8164 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
334208b7
RS
8165 Window w = SCROLL_BAR_X_WINDOW (bar);
8166 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f 8167 int win_x, win_y;
559cb2fb
JB
8168 Window dummy_window;
8169 int dummy_coord;
8170 unsigned int dummy_mask;
12ba150f 8171
cf7cb199
JB
8172 BLOCK_INPUT;
8173
ab648270 8174 /* Get the mouse's position relative to the scroll bar window, and
12ba150f 8175 report that. */
334208b7 8176 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
12ba150f 8177
559cb2fb
JB
8178 /* Root, child, root x and root y. */
8179 &dummy_window, &dummy_window,
8180 &dummy_coord, &dummy_coord,
12ba150f 8181
559cb2fb
JB
8182 /* Position relative to scroll bar. */
8183 &win_x, &win_y,
12ba150f 8184
559cb2fb
JB
8185 /* Mouse buttons and modifier keys. */
8186 &dummy_mask))
7a13e894 8187 ;
559cb2fb
JB
8188 else
8189 {
06a2c219 8190#if 0
559cb2fb 8191 int inside_height
d9cdbb3d 8192 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8193#endif
559cb2fb 8194 int top_range
d9cdbb3d 8195 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
559cb2fb
JB
8196
8197 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
8198
8199 if (! NILP (bar->dragging))
8200 win_y -= XINT (bar->dragging);
8201
8202 if (win_y < 0)
8203 win_y = 0;
8204 if (win_y > top_range)
8205 win_y = top_range;
8206
334208b7 8207 *fp = f;
7a13e894 8208 *bar_window = bar->window;
559cb2fb
JB
8209
8210 if (! NILP (bar->dragging))
8211 *part = scroll_bar_handle;
8212 else if (win_y < XINT (bar->start))
8213 *part = scroll_bar_above_handle;
8214 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8215 *part = scroll_bar_handle;
8216 else
8217 *part = scroll_bar_below_handle;
12ba150f 8218
e0c1aef2
KH
8219 XSETINT (*x, win_y);
8220 XSETINT (*y, top_range);
12ba150f 8221
39d8bb4d 8222 f->mouse_moved = 0;
559cb2fb
JB
8223 last_mouse_scroll_bar = Qnil;
8224 }
12ba150f 8225
559cb2fb 8226 *time = last_mouse_movement_time;
cf7cb199 8227
cf7cb199 8228 UNBLOCK_INPUT;
12ba150f
JB
8229}
8230
f451eb13 8231
dbc4e1c1 8232/* The screen has been cleared so we may have changed foreground or
ab648270
JB
8233 background colors, and the scroll bars may need to be redrawn.
8234 Clear out the scroll bars, and ask for expose events, so we can
dbc4e1c1
JB
8235 redraw them. */
8236
dfcf069d 8237void
ab648270 8238x_scroll_bar_clear (f)
dbc4e1c1
JB
8239 FRAME_PTR f;
8240{
06a2c219 8241#ifndef USE_TOOLKIT_SCROLL_BARS
dbc4e1c1
JB
8242 Lisp_Object bar;
8243
b80c363e
RS
8244 /* We can have scroll bars even if this is 0,
8245 if we just turned off scroll bar mode.
8246 But in that case we should not clear them. */
8247 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8248 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
8249 bar = XSCROLL_BAR (bar)->next)
8250 XClearArea (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
8251 0, 0, 0, 0, True);
06a2c219 8252#endif /* not USE_TOOLKIT_SCROLL_BARS */
dbc4e1c1
JB
8253}
8254
06a2c219 8255/* This processes Expose events from the menu-bar specific X event
19126e11 8256 loop in xmenu.c. This allows to redisplay the frame if necessary
06a2c219 8257 when handling menu-bar or pop-up items. */
3afe33e7 8258
06a2c219 8259int
3afe33e7
RS
8260process_expose_from_menu (event)
8261 XEvent event;
8262{
8263 FRAME_PTR f;
19126e11 8264 struct x_display_info *dpyinfo;
06a2c219 8265 int frame_exposed_p = 0;
3afe33e7 8266
f94397b5
KH
8267 BLOCK_INPUT;
8268
19126e11
KH
8269 dpyinfo = x_display_info_for_display (event.xexpose.display);
8270 f = x_window_to_frame (dpyinfo, event.xexpose.window);
3afe33e7
RS
8271 if (f)
8272 {
8273 if (f->async_visible == 0)
8274 {
8275 f->async_visible = 1;
8276 f->async_iconified = 0;
06c488fd 8277 f->output_data.x->has_been_visible = 1;
3afe33e7
RS
8278 SET_FRAME_GARBAGED (f);
8279 }
8280 else
8281 {
06a2c219
GM
8282 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
8283 event.xexpose.x, event.xexpose.y,
8284 event.xexpose.width, event.xexpose.height);
8285 frame_exposed_p = 1;
3afe33e7
RS
8286 }
8287 }
8288 else
8289 {
8290 struct scroll_bar *bar
8291 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 8292
3afe33e7
RS
8293 if (bar)
8294 x_scroll_bar_expose (bar, &event);
8295 }
f94397b5
KH
8296
8297 UNBLOCK_INPUT;
06a2c219 8298 return frame_exposed_p;
3afe33e7 8299}
09756a85
RS
8300\f
8301/* Define a queue to save up SelectionRequest events for later handling. */
8302
8303struct selection_event_queue
8304 {
8305 XEvent event;
8306 struct selection_event_queue *next;
8307 };
8308
8309static struct selection_event_queue *queue;
8310
8311/* Nonzero means queue up certain events--don't process them yet. */
06a2c219 8312
09756a85
RS
8313static int x_queue_selection_requests;
8314
8315/* Queue up an X event *EVENT, to be processed later. */
dbc4e1c1 8316
09756a85 8317static void
334208b7
RS
8318x_queue_event (f, event)
8319 FRAME_PTR f;
09756a85
RS
8320 XEvent *event;
8321{
8322 struct selection_event_queue *queue_tmp
06a2c219 8323 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
09756a85 8324
58769bee 8325 if (queue_tmp != NULL)
09756a85
RS
8326 {
8327 queue_tmp->event = *event;
8328 queue_tmp->next = queue;
8329 queue = queue_tmp;
8330 }
8331}
8332
8333/* Take all the queued events and put them back
8334 so that they get processed afresh. */
8335
8336static void
db3906fd
RS
8337x_unqueue_events (display)
8338 Display *display;
09756a85 8339{
58769bee 8340 while (queue != NULL)
09756a85
RS
8341 {
8342 struct selection_event_queue *queue_tmp = queue;
db3906fd 8343 XPutBackEvent (display, &queue_tmp->event);
09756a85 8344 queue = queue_tmp->next;
06a2c219 8345 xfree ((char *)queue_tmp);
09756a85
RS
8346 }
8347}
8348
8349/* Start queuing SelectionRequest events. */
8350
8351void
db3906fd
RS
8352x_start_queuing_selection_requests (display)
8353 Display *display;
09756a85
RS
8354{
8355 x_queue_selection_requests++;
8356}
8357
8358/* Stop queuing SelectionRequest events. */
8359
8360void
db3906fd
RS
8361x_stop_queuing_selection_requests (display)
8362 Display *display;
09756a85
RS
8363{
8364 x_queue_selection_requests--;
db3906fd 8365 x_unqueue_events (display);
09756a85 8366}
f451eb13
JB
8367\f
8368/* The main X event-reading loop - XTread_socket. */
dc6f92b8 8369
06a2c219 8370/* Time stamp of enter window event. This is only used by XTread_socket,
dc6f92b8
JB
8371 but we have to put it out here, since static variables within functions
8372 sometimes don't work. */
06a2c219 8373
dc6f92b8
JB
8374static Time enter_timestamp;
8375
11edeb03 8376/* This holds the state XLookupString needs to implement dead keys
58769bee 8377 and other tricks known as "compose processing". _X Window System_
11edeb03
JB
8378 says that a portable program can't use this, but Stephen Gildea assures
8379 me that letting the compiler initialize it to zeros will work okay.
8380
8381 This must be defined outside of XTread_socket, for the same reasons
06a2c219
GM
8382 given for enter_time stamp, above. */
8383
11edeb03
JB
8384static XComposeStatus compose_status;
8385
10e6549c
RS
8386/* Record the last 100 characters stored
8387 to help debug the loss-of-chars-during-GC problem. */
06a2c219 8388
2224b905
RS
8389static int temp_index;
8390static short temp_buffer[100];
10e6549c 8391
7a13e894
RS
8392/* Set this to nonzero to fake an "X I/O error"
8393 on a particular display. */
06a2c219 8394
7a13e894
RS
8395struct x_display_info *XTread_socket_fake_io_error;
8396
2224b905
RS
8397/* When we find no input here, we occasionally do a no-op command
8398 to verify that the X server is still running and we can still talk with it.
8399 We try all the open displays, one by one.
8400 This variable is used for cycling thru the displays. */
06a2c219 8401
2224b905
RS
8402static struct x_display_info *next_noop_dpyinfo;
8403
06a2c219
GM
8404#define SET_SAVED_MENU_EVENT(size) \
8405 do \
8406 { \
8407 if (f->output_data.x->saved_menu_event == 0) \
8408 f->output_data.x->saved_menu_event \
8409 = (XEvent *) xmalloc (sizeof (XEvent)); \
8410 bcopy (&event, f->output_data.x->saved_menu_event, size); \
8411 if (numchars >= 1) \
8412 { \
8413 bufp->kind = menu_bar_activate_event; \
8414 XSETFRAME (bufp->frame_or_window, f); \
8415 bufp++; \
8416 count++; \
8417 numchars--; \
8418 } \
8419 } \
8420 while (0)
8421
8805890a 8422#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
06a2c219 8423#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8805890a 8424
dc6f92b8
JB
8425/* Read events coming from the X server.
8426 This routine is called by the SIGIO handler.
8427 We return as soon as there are no more events to be read.
8428
8429 Events representing keys are stored in buffer BUFP,
8430 which can hold up to NUMCHARS characters.
8431 We return the number of characters stored into the buffer,
8432 thus pretending to be `read'.
8433
dc6f92b8
JB
8434 EXPECTED is nonzero if the caller knows input is available. */
8435
7c5283e4 8436int
f66868ba 8437XTread_socket (sd, bufp, numchars, expected)
dc6f92b8 8438 register int sd;
8805890a
KH
8439 /* register */ struct input_event *bufp;
8440 /* register */ int numchars;
dc6f92b8
JB
8441 int expected;
8442{
8443 int count = 0;
8444 int nbytes = 0;
dc6f92b8 8445 XEvent event;
f676886a 8446 struct frame *f;
66f55a9d 8447 int event_found = 0;
334208b7 8448 struct x_display_info *dpyinfo;
6c183ba5
RS
8449#ifdef HAVE_X_I18N
8450 Status status_return;
8451#endif
dc6f92b8 8452
9ac0d9e0 8453 if (interrupt_input_blocked)
dc6f92b8 8454 {
9ac0d9e0 8455 interrupt_input_pending = 1;
dc6f92b8
JB
8456 return -1;
8457 }
8458
9ac0d9e0 8459 interrupt_input_pending = 0;
dc6f92b8 8460 BLOCK_INPUT;
c0a04927
RS
8461
8462 /* So people can tell when we have read the available input. */
8463 input_signal_count++;
8464
dc6f92b8 8465 if (numchars <= 0)
06a2c219 8466 abort (); /* Don't think this happens. */
dc6f92b8 8467
7a13e894
RS
8468 /* Find the display we are supposed to read input for.
8469 It's the one communicating on descriptor SD. */
8470 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
8471 {
8472#if 0 /* This ought to be unnecessary; let's verify it. */
dc6f92b8 8473#ifdef FIOSNBIO
7a13e894
RS
8474 /* If available, Xlib uses FIOSNBIO to make the socket
8475 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
e6cbea31 8476 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
06a2c219 8477 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7a13e894 8478 fcntl (dpyinfo->connection, F_SETFL, 0);
c118dd06 8479#endif /* ! defined (FIOSNBIO) */
7a13e894 8480#endif
dc6f92b8 8481
7a13e894
RS
8482#if 0 /* This code can't be made to work, with multiple displays,
8483 and appears not to be used on any system any more.
8484 Also keyboard.c doesn't turn O_NDELAY on and off
8485 for X connections. */
dc6f92b8
JB
8486#ifndef SIGIO
8487#ifndef HAVE_SELECT
7a13e894
RS
8488 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
8489 {
8490 extern int read_alarm_should_throw;
8491 read_alarm_should_throw = 1;
8492 XPeekEvent (dpyinfo->display, &event);
8493 read_alarm_should_throw = 0;
8494 }
c118dd06
JB
8495#endif /* HAVE_SELECT */
8496#endif /* SIGIO */
7a13e894 8497#endif
dc6f92b8 8498
7a13e894
RS
8499 /* For debugging, this gives a way to fake an I/O error. */
8500 if (dpyinfo == XTread_socket_fake_io_error)
8501 {
8502 XTread_socket_fake_io_error = 0;
8503 x_io_error_quitter (dpyinfo->display);
8504 }
dc6f92b8 8505
06a2c219 8506 while (XPending (dpyinfo->display))
dc6f92b8 8507 {
7a13e894 8508 XNextEvent (dpyinfo->display, &event);
06a2c219
GM
8509
8510 if (display_busy_cursor_p)
8511 {
8512 /* Setting inhibit_busy_cursor to 2 inhibits busy-cursor
8513 display until the next X event is read and we come
8514 here again. Setting it to 1 inhibits busy-cursor
8515 display for direct commands. */
8516 if (event.type == MotionNotify
8517 || event.type == EnterNotify
8518 || (dpyinfo->grabbed
8519 && event.type != ButtonRelease))
8520 inhibit_busy_cursor = 2;
8521 else
8522 inhibit_busy_cursor = 1;
8523 }
8524
531483fb 8525#ifdef HAVE_X_I18N
d1bc4182
RS
8526 {
8527 struct frame *f1 = x_any_window_to_frame (dpyinfo,
c99babf2 8528 event.xclient.window);
d1bc4182
RS
8529 /* The necessity of the following line took me
8530 a full work-day to decipher from the docs!! */
8531 if (f1 != 0 && FRAME_XIC (f1) && XFilterEvent (&event, None))
8532 break;
8533 }
0cd6403b 8534#endif
7a13e894
RS
8535 event_found = 1;
8536
8537 switch (event.type)
8538 {
8539 case ClientMessage:
c047688c 8540 {
7a13e894
RS
8541 if (event.xclient.message_type
8542 == dpyinfo->Xatom_wm_protocols
8543 && event.xclient.format == 32)
c047688c 8544 {
7a13e894
RS
8545 if (event.xclient.data.l[0]
8546 == dpyinfo->Xatom_wm_take_focus)
c047688c 8547 {
8c1a6a84
RS
8548 /* Use x_any_window_to_frame because this
8549 could be the shell widget window
8550 if the frame has no title bar. */
8551 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6c183ba5
RS
8552#ifdef HAVE_X_I18N
8553 /* Not quite sure this is needed -pd */
8c1a6a84 8554 if (f && FRAME_XIC (f))
6c183ba5
RS
8555 XSetICFocus (FRAME_XIC (f));
8556#endif
bf7253f4
RS
8557 /* Since we set WM_TAKE_FOCUS, we must call
8558 XSetInputFocus explicitly. But not if f is null,
8559 since that might be an event for a deleted frame. */
7a13e894 8560 if (f)
bf7253f4
RS
8561 {
8562 Display *d = event.xclient.display;
8563 /* Catch and ignore errors, in case window has been
8564 iconified by a window manager such as GWM. */
8565 int count = x_catch_errors (d);
8566 XSetInputFocus (d, event.xclient.window,
8567 RevertToPointerRoot,
8568 event.xclient.data.l[1]);
8569 /* This is needed to detect the error
8570 if there is an error. */
8571 XSync (d, False);
8572 x_uncatch_errors (d, count);
8573 }
7a13e894 8574 /* Not certain about handling scroll bars here */
c047688c 8575 }
7a13e894
RS
8576 else if (event.xclient.data.l[0]
8577 == dpyinfo->Xatom_wm_save_yourself)
8578 {
8579 /* Save state modify the WM_COMMAND property to
06a2c219 8580 something which can reinstate us. This notifies
7a13e894
RS
8581 the session manager, who's looking for such a
8582 PropertyNotify. Can restart processing when
06a2c219 8583 a keyboard or mouse event arrives. */
7a13e894
RS
8584 if (numchars > 0)
8585 {
19126e11
KH
8586 f = x_top_window_to_frame (dpyinfo,
8587 event.xclient.window);
7a13e894
RS
8588
8589 /* This is just so we only give real data once
8590 for a single Emacs process. */
8591 if (f == selected_frame)
8592 XSetCommand (FRAME_X_DISPLAY (f),
8593 event.xclient.window,
8594 initial_argv, initial_argc);
f000f5c5 8595 else if (f)
7a13e894
RS
8596 XSetCommand (FRAME_X_DISPLAY (f),
8597 event.xclient.window,
8598 0, 0);
8599 }
8600 }
8601 else if (event.xclient.data.l[0]
8602 == dpyinfo->Xatom_wm_delete_window)
1fb20991 8603 {
19126e11
KH
8604 struct frame *f
8605 = x_any_window_to_frame (dpyinfo,
8606 event.xclient.window);
1fb20991 8607
7a13e894
RS
8608 if (f)
8609 {
8610 if (numchars == 0)
8611 abort ();
1fb20991 8612
7a13e894
RS
8613 bufp->kind = delete_window_event;
8614 XSETFRAME (bufp->frame_or_window, f);
8615 bufp++;
8616
8617 count += 1;
8618 numchars -= 1;
8619 }
1fb20991 8620 }
c047688c 8621 }
7a13e894
RS
8622 else if (event.xclient.message_type
8623 == dpyinfo->Xatom_wm_configure_denied)
8624 {
8625 }
8626 else if (event.xclient.message_type
8627 == dpyinfo->Xatom_wm_window_moved)
8628 {
8629 int new_x, new_y;
19126e11
KH
8630 struct frame *f
8631 = x_window_to_frame (dpyinfo, event.xclient.window);
58769bee 8632
7a13e894
RS
8633 new_x = event.xclient.data.s[0];
8634 new_y = event.xclient.data.s[1];
1fb20991 8635
7a13e894
RS
8636 if (f)
8637 {
7556890b
RS
8638 f->output_data.x->left_pos = new_x;
8639 f->output_data.x->top_pos = new_y;
7a13e894 8640 }
1fb20991 8641 }
0fdff6bb 8642#ifdef HACK_EDITRES
7a13e894
RS
8643 else if (event.xclient.message_type
8644 == dpyinfo->Xatom_editres)
8645 {
19126e11
KH
8646 struct frame *f
8647 = x_any_window_to_frame (dpyinfo, event.xclient.window);
7556890b 8648 _XEditResCheckMessages (f->output_data.x->widget, NULL,
19126e11 8649 &event, NULL);
7a13e894 8650 }
0fdff6bb 8651#endif /* HACK_EDITRES */
06a2c219
GM
8652 else if ((event.xclient.message_type
8653 == dpyinfo->Xatom_DONE)
8654 || (event.xclient.message_type
8655 == dpyinfo->Xatom_PAGE))
8656 {
8657 /* Ghostview job completed. Kill it. We could
8658 reply with "Next" if we received "Page", but we
8659 currently never do because we are interested in
8660 images, only, which should have 1 page. */
8661 Window gs_window = (Window) event.xclient.data.l[0];
8662 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
8663 struct frame *f
8664 = x_window_to_frame (dpyinfo, event.xclient.window);
8665 x_kill_gs_process (pixmap, f);
8666 expose_frame (f, 0, 0, 0, 0);
8667 }
8668#ifdef USE_TOOLKIT_SCROLL_BARS
8669 /* Scroll bar callbacks send a ClientMessage from which
8670 we construct an input_event. */
8671 else if (event.xclient.message_type
8672 == dpyinfo->Xatom_Scrollbar)
8673 {
8674 x_scroll_bar_to_input_event (&event, bufp);
8675 ++bufp, ++count, --numchars;
8676 goto out;
8677 }
8678#endif /* USE_TOOLKIT_SCROLL_BARS */
8679 else
8680 goto OTHER;
7a13e894
RS
8681 }
8682 break;
dc6f92b8 8683
7a13e894 8684 case SelectionNotify:
3afe33e7 8685#ifdef USE_X_TOOLKIT
19126e11 8686 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
7a13e894 8687 goto OTHER;
3afe33e7 8688#endif /* not USE_X_TOOLKIT */
dfcf069d 8689 x_handle_selection_notify (&event.xselection);
7a13e894 8690 break;
d56a553a 8691
06a2c219 8692 case SelectionClear: /* Someone has grabbed ownership. */
3afe33e7 8693#ifdef USE_X_TOOLKIT
19126e11 8694 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
7a13e894 8695 goto OTHER;
3afe33e7 8696#endif /* USE_X_TOOLKIT */
7a13e894
RS
8697 {
8698 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
d56a553a 8699
7a13e894
RS
8700 if (numchars == 0)
8701 abort ();
d56a553a 8702
7a13e894
RS
8703 bufp->kind = selection_clear_event;
8704 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
8705 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
8706 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 8707 bufp->frame_or_window = Qnil;
7a13e894 8708 bufp++;
d56a553a 8709
7a13e894
RS
8710 count += 1;
8711 numchars -= 1;
8712 }
8713 break;
dc6f92b8 8714
06a2c219 8715 case SelectionRequest: /* Someone wants our selection. */
3afe33e7 8716#ifdef USE_X_TOOLKIT
19126e11 8717 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
7a13e894 8718 goto OTHER;
3afe33e7 8719#endif /* USE_X_TOOLKIT */
7a13e894 8720 if (x_queue_selection_requests)
19126e11 8721 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
7a13e894
RS
8722 &event);
8723 else
8724 {
8725 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
dc6f92b8 8726
7a13e894
RS
8727 if (numchars == 0)
8728 abort ();
8729
8730 bufp->kind = selection_request_event;
8731 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
8732 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
8733 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
8734 SELECTION_EVENT_TARGET (bufp) = eventp->target;
8735 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
8736 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 8737 bufp->frame_or_window = Qnil;
7a13e894
RS
8738 bufp++;
8739
8740 count += 1;
8741 numchars -= 1;
8742 }
8743 break;
8744
8745 case PropertyNotify:
3afe33e7 8746#ifdef USE_X_TOOLKIT
19126e11 8747 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
7a13e894 8748 goto OTHER;
3afe33e7 8749#endif /* not USE_X_TOOLKIT */
dfcf069d 8750 x_handle_property_notify (&event.xproperty);
7a13e894 8751 break;
dc6f92b8 8752
7a13e894 8753 case ReparentNotify:
19126e11 8754 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
7a13e894
RS
8755 if (f)
8756 {
8757 int x, y;
7556890b 8758 f->output_data.x->parent_desc = event.xreparent.parent;
7a13e894 8759 x_real_positions (f, &x, &y);
7556890b
RS
8760 f->output_data.x->left_pos = x;
8761 f->output_data.x->top_pos = y;
7a13e894
RS
8762 }
8763 break;
3bd330d4 8764
7a13e894 8765 case Expose:
19126e11 8766 f = x_window_to_frame (dpyinfo, event.xexpose.window);
7a13e894 8767 if (f)
dc6f92b8 8768 {
7a13e894
RS
8769 if (f->async_visible == 0)
8770 {
8771 f->async_visible = 1;
8772 f->async_iconified = 0;
06c488fd 8773 f->output_data.x->has_been_visible = 1;
7a13e894
RS
8774 SET_FRAME_GARBAGED (f);
8775 }
8776 else
06a2c219
GM
8777 expose_frame (x_window_to_frame (dpyinfo,
8778 event.xexpose.window),
8779 event.xexpose.x, event.xexpose.y,
8780 event.xexpose.width, event.xexpose.height);
dc6f92b8
JB
8781 }
8782 else
7a13e894 8783 {
06a2c219
GM
8784#ifdef USE_TOOLKIT_SCROLL_BARS
8785 /* Dispatch event to the widget. */
8786 goto OTHER;
8787#else /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
8788 struct scroll_bar *bar
8789 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 8790
7a13e894
RS
8791 if (bar)
8792 x_scroll_bar_expose (bar, &event);
3afe33e7 8793#ifdef USE_X_TOOLKIT
7a13e894
RS
8794 else
8795 goto OTHER;
3afe33e7 8796#endif /* USE_X_TOOLKIT */
06a2c219 8797#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
8798 }
8799 break;
dc6f92b8 8800
7a13e894
RS
8801 case GraphicsExpose: /* This occurs when an XCopyArea's
8802 source area was obscured or not
8803 available.*/
19126e11 8804 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
7a13e894
RS
8805 if (f)
8806 {
06a2c219
GM
8807 expose_frame (f,
8808 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
8809 event.xgraphicsexpose.width,
8810 event.xgraphicsexpose.height);
7a13e894 8811 }
3afe33e7 8812#ifdef USE_X_TOOLKIT
7a13e894
RS
8813 else
8814 goto OTHER;
3afe33e7 8815#endif /* USE_X_TOOLKIT */
7a13e894 8816 break;
dc6f92b8 8817
7a13e894 8818 case NoExpose: /* This occurs when an XCopyArea's
06a2c219
GM
8819 source area was completely
8820 available */
7a13e894 8821 break;
dc6f92b8 8822
7a13e894 8823 case UnmapNotify:
06a2c219
GM
8824 /* Redo the mouse-highlight after the tooltip has gone. */
8825 if (event.xmap.window == tip_window)
8826 {
8827 tip_window = 0;
8828 redo_mouse_highlight ();
8829 }
8830
91ea2a7a 8831 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
7a13e894
RS
8832 if (f) /* F may no longer exist if
8833 the frame was deleted. */
8834 {
8835 /* While a frame is unmapped, display generation is
8836 disabled; you don't want to spend time updating a
8837 display that won't ever be seen. */
8838 f->async_visible = 0;
8839 /* We can't distinguish, from the event, whether the window
8840 has become iconified or invisible. So assume, if it
8841 was previously visible, than now it is iconified.
1aa6072f
RS
8842 But x_make_frame_invisible clears both
8843 the visible flag and the iconified flag;
8844 and that way, we know the window is not iconified now. */
7a13e894 8845 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
1aa6072f
RS
8846 {
8847 f->async_iconified = 1;
bddd097c 8848
1aa6072f
RS
8849 bufp->kind = iconify_event;
8850 XSETFRAME (bufp->frame_or_window, f);
8851 bufp++;
8852 count++;
8853 numchars--;
8854 }
7a13e894 8855 }
7a13e894 8856 goto OTHER;
dc6f92b8 8857
7a13e894 8858 case MapNotify:
06a2c219
GM
8859 if (event.xmap.window == tip_window)
8860 /* The tooltip has been drawn already. Avoid
8861 the SET_FRAME_GARBAGED below. */
8862 goto OTHER;
8863
8864 /* We use x_top_window_to_frame because map events can
8865 come for sub-windows and they don't mean that the
8866 frame is visible. */
19126e11 8867 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
7a13e894
RS
8868 if (f)
8869 {
8870 f->async_visible = 1;
8871 f->async_iconified = 0;
06c488fd 8872 f->output_data.x->has_been_visible = 1;
dc6f92b8 8873
7a13e894
RS
8874 /* wait_reading_process_input will notice this and update
8875 the frame's display structures. */
8876 SET_FRAME_GARBAGED (f);
bddd097c 8877
d806e720
RS
8878 if (f->iconified)
8879 {
8880 bufp->kind = deiconify_event;
8881 XSETFRAME (bufp->frame_or_window, f);
8882 bufp++;
8883 count++;
8884 numchars--;
8885 }
e73ec6fa 8886 else if (! NILP (Vframe_list)
8c29f6bf 8887 && ! NILP (XCONS (Vframe_list)->cdr))
78aa2ba5
KH
8888 /* Force a redisplay sooner or later
8889 to update the frame titles
8890 in case this is the second frame. */
8891 record_asynch_buffer_change ();
7a13e894 8892 }
7a13e894 8893 goto OTHER;
dc6f92b8 8894
7a13e894 8895 case KeyPress:
19126e11 8896 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
f451eb13 8897
06a2c219
GM
8898#ifdef USE_MOTIF
8899 /* I couldn't find a way to prevent LessTif scroll bars
8900 from consuming key events. */
8901 if (f == 0)
8902 {
8903 Widget widget = XtWindowToWidget (dpyinfo->display,
8904 event.xkey.window);
8905 if (widget && XmIsScrollBar (widget))
8906 {
8907 widget = XtParent (widget);
8908 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
8909 }
8910 }
8911#endif /* USE_MOTIF */
8912
7a13e894
RS
8913 if (f != 0)
8914 {
8915 KeySym keysym, orig_keysym;
8916 /* al%imercury@uunet.uu.net says that making this 81 instead of
8917 80 fixed a bug whereby meta chars made his Emacs hang. */
8918 unsigned char copy_buffer[81];
8919 int modifiers;
64bb1782 8920
7a13e894
RS
8921 event.xkey.state
8922 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
8923 extra_keyboard_modifiers);
8924 modifiers = event.xkey.state;
3a2712f9 8925
7a13e894 8926 /* This will have to go some day... */
752a043f 8927
7a13e894
RS
8928 /* make_lispy_event turns chars into control chars.
8929 Don't do it here because XLookupString is too eager. */
8930 event.xkey.state &= ~ControlMask;
5d46f928
RS
8931 event.xkey.state &= ~(dpyinfo->meta_mod_mask
8932 | dpyinfo->super_mod_mask
8933 | dpyinfo->hyper_mod_mask
8934 | dpyinfo->alt_mod_mask);
8935
1cf4a0d1
RS
8936 /* In case Meta is ComposeCharacter,
8937 clear its status. According to Markus Ehrnsperger
8938 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
8939 this enables ComposeCharacter to work whether or
8940 not it is combined with Meta. */
8941 if (modifiers & dpyinfo->meta_mod_mask)
8942 bzero (&compose_status, sizeof (compose_status));
8943
6c183ba5
RS
8944#ifdef HAVE_X_I18N
8945 if (FRAME_XIC (f))
8946 {
5d7cc324
RS
8947 /* The necessity of the following line took me
8948 a full work-day to decipher from the docs!! */
8949 if (XFilterEvent (&event, None))
8950 break;
6c183ba5
RS
8951 nbytes = XmbLookupString (FRAME_XIC (f),
8952 &event.xkey, copy_buffer,
8953 80, &keysym,
8954 &status_return);
1decb680
PE
8955 if (status_return == XLookupNone)
8956 break;
8957 else if (status_return == XLookupChars)
8958 keysym = NoSymbol;
8959 else if (status_return != XLookupKeySym
8960 && status_return != XLookupBoth)
8961 abort ();
6c183ba5
RS
8962 }
8963 else
8964 nbytes = XLookupString (&event.xkey, copy_buffer,
8965 80, &keysym, &compose_status);
8966#else
0299d313
RS
8967 nbytes = XLookupString (&event.xkey, copy_buffer,
8968 80, &keysym, &compose_status);
6c183ba5 8969#endif
dc6f92b8 8970
7a13e894 8971 orig_keysym = keysym;
55123275 8972
7a13e894
RS
8973 if (numchars > 1)
8974 {
8975 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
8976 || keysym == XK_Delete
1097aea0 8977#ifdef XK_ISO_Left_Tab
441affdb 8978 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
1097aea0 8979#endif
852bff8f 8980 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
7a13e894
RS
8981 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
8982 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
c34790e0 8983#ifdef HPUX
7a13e894
RS
8984 /* This recognizes the "extended function keys".
8985 It seems there's no cleaner way.
8986 Test IsModifierKey to avoid handling mode_switch
8987 incorrectly. */
8988 || ((unsigned) (keysym) >= XK_Select
8989 && (unsigned)(keysym) < XK_KP_Space)
69388238
RS
8990#endif
8991#ifdef XK_dead_circumflex
7a13e894 8992 || orig_keysym == XK_dead_circumflex
69388238
RS
8993#endif
8994#ifdef XK_dead_grave
7a13e894 8995 || orig_keysym == XK_dead_grave
69388238
RS
8996#endif
8997#ifdef XK_dead_tilde
7a13e894 8998 || orig_keysym == XK_dead_tilde
69388238
RS
8999#endif
9000#ifdef XK_dead_diaeresis
7a13e894 9001 || orig_keysym == XK_dead_diaeresis
69388238
RS
9002#endif
9003#ifdef XK_dead_macron
7a13e894 9004 || orig_keysym == XK_dead_macron
69388238
RS
9005#endif
9006#ifdef XK_dead_degree
7a13e894 9007 || orig_keysym == XK_dead_degree
69388238
RS
9008#endif
9009#ifdef XK_dead_acute
7a13e894 9010 || orig_keysym == XK_dead_acute
69388238
RS
9011#endif
9012#ifdef XK_dead_cedilla
7a13e894 9013 || orig_keysym == XK_dead_cedilla
69388238
RS
9014#endif
9015#ifdef XK_dead_breve
7a13e894 9016 || orig_keysym == XK_dead_breve
69388238
RS
9017#endif
9018#ifdef XK_dead_ogonek
7a13e894 9019 || orig_keysym == XK_dead_ogonek
69388238
RS
9020#endif
9021#ifdef XK_dead_caron
7a13e894 9022 || orig_keysym == XK_dead_caron
69388238
RS
9023#endif
9024#ifdef XK_dead_doubleacute
7a13e894 9025 || orig_keysym == XK_dead_doubleacute
69388238
RS
9026#endif
9027#ifdef XK_dead_abovedot
7a13e894 9028 || orig_keysym == XK_dead_abovedot
c34790e0 9029#endif
7a13e894
RS
9030 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9031 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9032 /* Any "vendor-specific" key is ok. */
9033 || (orig_keysym & (1 << 28)))
9034 && ! (IsModifierKey (orig_keysym)
7719aa06
RS
9035#ifndef HAVE_X11R5
9036#ifdef XK_Mode_switch
7a13e894 9037 || ((unsigned)(orig_keysym) == XK_Mode_switch)
7719aa06
RS
9038#endif
9039#ifdef XK_Num_Lock
7a13e894 9040 || ((unsigned)(orig_keysym) == XK_Num_Lock)
7719aa06
RS
9041#endif
9042#endif /* not HAVE_X11R5 */
7a13e894 9043 ))
dc6f92b8 9044 {
10e6549c
RS
9045 if (temp_index == sizeof temp_buffer / sizeof (short))
9046 temp_index = 0;
7a13e894
RS
9047 temp_buffer[temp_index++] = keysym;
9048 bufp->kind = non_ascii_keystroke;
9049 bufp->code = keysym;
e0c1aef2 9050 XSETFRAME (bufp->frame_or_window, f);
334208b7
RS
9051 bufp->modifiers
9052 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9053 modifiers);
1113d9db 9054 bufp->timestamp = event.xkey.time;
dc6f92b8 9055 bufp++;
7a13e894
RS
9056 count++;
9057 numchars--;
06a2c219
GM
9058
9059 if (display_busy_cursor_p)
9060 if (keysym != XK_Return || minibuf_level == 0)
9061 inhibit_busy_cursor = 2;
dc6f92b8 9062 }
7a13e894
RS
9063 else if (numchars > nbytes)
9064 {
9065 register int i;
9066
9067 for (i = 0; i < nbytes; i++)
9068 {
9069 if (temp_index == sizeof temp_buffer / sizeof (short))
9070 temp_index = 0;
9071 temp_buffer[temp_index++] = copy_buffer[i];
9072 bufp->kind = ascii_keystroke;
9073 bufp->code = copy_buffer[i];
9074 XSETFRAME (bufp->frame_or_window, f);
9075 bufp->modifiers
9076 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9077 modifiers);
9078 bufp->timestamp = event.xkey.time;
9079 bufp++;
9080 }
9081
9082 count += nbytes;
9083 numchars -= nbytes;
1decb680
PE
9084
9085 if (keysym == NoSymbol)
9086 break;
7a13e894
RS
9087 }
9088 else
9089 abort ();
dc6f92b8 9090 }
10e6549c
RS
9091 else
9092 abort ();
dc6f92b8 9093 }
717ca130 9094 goto OTHER;
f451eb13 9095
7a13e894 9096 /* Here's a possible interpretation of the whole
06a2c219
GM
9097 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
9098 you get a FocusIn event, you have to get a FocusOut
9099 event before you relinquish the focus. If you
9100 haven't received a FocusIn event, then a mere
9101 LeaveNotify is enough to free you. */
f451eb13 9102
7a13e894 9103 case EnterNotify:
06a2c219
GM
9104 {
9105 int from_menu_bar_p = 0;
9106
9107 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
9108
9109#ifdef LESSTIF_VERSION
9110 /* When clicking outside of a menu bar popup to close
9111 it, we get a FocusIn/ EnterNotify sequence of
9112 events. The flag event.xcrossing.focus is not set
9113 in the EnterNotify event of that sequence because
9114 the focus is in the menu bar,
9115 event.xcrossing.window is the frame's X window.
9116 Unconditionally setting the focus frame to null in
9117 this case is not the right thing, because no event
9118 follows that could set the focus frame to the right
9119 value.
9120
9121 This could be a LessTif bug, but I wasn't able to
9122 reproduce the behavior in a simple test program.
9123
9124 (gerd, LessTif 0.88.1). */
9125
9126 if (!event.xcrossing.focus
9127 && f
9128 && f->output_data.x->menubar_widget)
9129 {
9130 Window focus;
9131 int revert;
9132
9133 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9134 if (focus == XtWindow (f->output_data.x->menubar_widget))
9135 from_menu_bar_p = 1;
9136 }
9137#endif /* LESSTIF_VERSION */
6d4238f3 9138
06a2c219
GM
9139 if (event.xcrossing.focus || from_menu_bar_p)
9140 {
9141 /* Avoid nasty pop/raise loops. */
9142 if (f && (!(f->auto_raise)
9143 || !(f->auto_lower)
9144 || (event.xcrossing.time - enter_timestamp) > 500))
9145 {
9146 x_new_focus_frame (dpyinfo, f);
9147 enter_timestamp = event.xcrossing.time;
9148 }
9149 }
9150 else if (f == dpyinfo->x_focus_frame)
9151 x_new_focus_frame (dpyinfo, 0);
9152
9153 /* EnterNotify counts as mouse movement,
9154 so update things that depend on mouse position. */
9155 if (f && !f->output_data.x->busy_p)
9156 note_mouse_movement (f, &event.xmotion);
9157 goto OTHER;
9158 }
dc6f92b8 9159
7a13e894 9160 case FocusIn:
19126e11 9161 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 9162 if (event.xfocus.detail != NotifyPointer)
0f941935 9163 dpyinfo->x_focus_event_frame = f;
7a13e894 9164 if (f)
0f941935 9165 x_new_focus_frame (dpyinfo, f);
f9e24cb9 9166
6c183ba5
RS
9167#ifdef HAVE_X_I18N
9168 if (f && FRAME_XIC (f))
9169 XSetICFocus (FRAME_XIC (f));
9170#endif
9171
7a13e894 9172 goto OTHER;
10c5e63d 9173
7a13e894 9174 case LeaveNotify:
19126e11 9175 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
7a13e894 9176 if (f)
10c5e63d 9177 {
06a2c219
GM
9178 Lisp_Object frame;
9179 int from_menu_bar_p = 0;
9180
7a13e894 9181 if (f == dpyinfo->mouse_face_mouse_frame)
06a2c219
GM
9182 {
9183 /* If we move outside the frame, then we're
9184 certainly no longer on any text in the frame. */
9185 clear_mouse_face (dpyinfo);
9186 dpyinfo->mouse_face_mouse_frame = 0;
9187 }
9188
9189 /* Generate a nil HELP_EVENT to cancel a help-echo.
9190 Do it only if there's something to cancel.
9191 Otherwise, the startup message is cleared when
9192 the mouse leaves the frame. */
9193 if (any_help_event_p)
9194 {
9195 XSETFRAME (frame, f);
9196 bufp->kind = HELP_EVENT;
9197 bufp->frame_or_window = Fcons (frame, Qnil);
9198 ++bufp, ++count, --numchars;
9199 }
7a13e894 9200
06a2c219
GM
9201#ifdef LESSTIF_VERSION
9202 /* Please see the comment at the start of the
9203 EnterNotify case. */
9204 if (!event.xcrossing.focus
9205 && f->output_data.x->menubar_widget)
9206 {
9207 Window focus;
9208 int revert;
9209 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9210 if (focus == XtWindow (f->output_data.x->menubar_widget))
9211 from_menu_bar_p = 1;
9212 }
9213#endif /* LESSTIF_VERSION */
9214
9215 if (event.xcrossing.focus || from_menu_bar_p)
0f941935 9216 x_mouse_leave (dpyinfo);
10c5e63d 9217 else
7a13e894 9218 {
0f941935
KH
9219 if (f == dpyinfo->x_focus_event_frame)
9220 dpyinfo->x_focus_event_frame = 0;
9221 if (f == dpyinfo->x_focus_frame)
9222 x_new_focus_frame (dpyinfo, 0);
7a13e894 9223 }
10c5e63d 9224 }
7a13e894 9225 goto OTHER;
dc6f92b8 9226
7a13e894 9227 case FocusOut:
19126e11 9228 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 9229 if (event.xfocus.detail != NotifyPointer
0f941935
KH
9230 && f == dpyinfo->x_focus_event_frame)
9231 dpyinfo->x_focus_event_frame = 0;
9232 if (f && f == dpyinfo->x_focus_frame)
9233 x_new_focus_frame (dpyinfo, 0);
f9e24cb9 9234
6c183ba5
RS
9235#ifdef HAVE_X_I18N
9236 if (f && FRAME_XIC (f))
9237 XUnsetICFocus (FRAME_XIC (f));
9238#endif
9239
7a13e894 9240 goto OTHER;
dc6f92b8 9241
7a13e894 9242 case MotionNotify:
dc6f92b8 9243 {
06a2c219
GM
9244 previous_help_echo = help_echo;
9245 help_echo = Qnil;
9246
7a13e894
RS
9247 if (dpyinfo->grabbed && last_mouse_frame
9248 && FRAME_LIVE_P (last_mouse_frame))
9249 f = last_mouse_frame;
9250 else
19126e11 9251 f = x_window_to_frame (dpyinfo, event.xmotion.window);
06a2c219 9252
7a13e894
RS
9253 if (f)
9254 note_mouse_movement (f, &event.xmotion);
9255 else
9256 {
06a2c219 9257#ifndef USE_X_TOOLKIT
7a13e894
RS
9258 struct scroll_bar *bar
9259 = x_window_to_scroll_bar (event.xmotion.window);
f451eb13 9260
7a13e894
RS
9261 if (bar)
9262 x_scroll_bar_note_movement (bar, &event);
06a2c219 9263#endif /* USE_X_TOOLKIT */
b8009dd1 9264
06a2c219
GM
9265 /* If we move outside the frame, then we're
9266 certainly no longer on any text in the frame. */
7a13e894
RS
9267 clear_mouse_face (dpyinfo);
9268 }
06a2c219
GM
9269
9270 /* If the contents of the global variable help_echo
9271 has changed, generate a HELP_EVENT. */
9272 if (STRINGP (help_echo)
9273 || STRINGP (previous_help_echo))
9274 {
9275 Lisp_Object frame;
9276
9277 if (f)
9278 XSETFRAME (frame, f);
9279 else
9280 frame = Qnil;
9281
9282 any_help_event_p = 1;
9283 bufp->kind = HELP_EVENT;
9284 bufp->frame_or_window = Fcons (frame, help_echo);
9285 ++bufp, ++count, --numchars;
9286 }
9287
9288 goto OTHER;
dc6f92b8 9289 }
dc6f92b8 9290
7a13e894 9291 case ConfigureNotify:
9829ddba
RS
9292 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
9293 if (f)
af395ec1 9294 {
bf1b7b30
KH
9295 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
9296 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
06a2c219 9297
2d7fc7e8
RS
9298#ifndef USE_X_TOOLKIT
9299 /* In the toolkit version, change_frame_size
9300 is called by the code that handles resizing
9301 of the EmacsFrame widget. */
7a13e894 9302
7a13e894
RS
9303 /* Even if the number of character rows and columns has
9304 not changed, the font size may have changed, so we need
9305 to check the pixel dimensions as well. */
9306 if (columns != f->width
9307 || rows != f->height
7556890b
RS
9308 || event.xconfigure.width != f->output_data.x->pixel_width
9309 || event.xconfigure.height != f->output_data.x->pixel_height)
7a13e894
RS
9310 {
9311 change_frame_size (f, rows, columns, 0, 1);
9312 SET_FRAME_GARBAGED (f);
e687d06e 9313 cancel_mouse_face (f);
7a13e894 9314 }
2d7fc7e8 9315#endif
af395ec1 9316
7556890b
RS
9317 f->output_data.x->pixel_width = event.xconfigure.width;
9318 f->output_data.x->pixel_height = event.xconfigure.height;
7a13e894
RS
9319
9320 /* What we have now is the position of Emacs's own window.
9321 Convert that to the position of the window manager window. */
dcb07ae9
RS
9322 x_real_positions (f, &f->output_data.x->left_pos,
9323 &f->output_data.x->top_pos);
9324
9325 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
9326 {
9327 /* Since the WM decorations come below top_pos now,
9328 we must put them below top_pos in the future. */
9329 f->output_data.x->win_gravity = NorthWestGravity;
9330 x_wm_set_size_hint (f, (long) 0, 0);
9331 }
8f08dc93
KH
9332#ifdef USE_MOTIF
9333 /* Some window managers pass (0,0) as the location of
9334 the window, and the Motif event handler stores it
9335 in the emacs widget, which messes up Motif menus. */
9336 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
9337 {
9338 event.xconfigure.x = f->output_data.x->widget->core.x;
9339 event.xconfigure.y = f->output_data.x->widget->core.y;
9340 }
06a2c219 9341#endif /* USE_MOTIF */
7a13e894 9342 }
2d7fc7e8 9343 goto OTHER;
dc6f92b8 9344
7a13e894
RS
9345 case ButtonPress:
9346 case ButtonRelease:
9347 {
9348 /* If we decide we want to generate an event to be seen
9349 by the rest of Emacs, we put it here. */
9350 struct input_event emacs_event;
06a2c219
GM
9351 int toolbar_p = 0;
9352
7a13e894 9353 emacs_event.kind = no_event;
7a13e894 9354 bzero (&compose_status, sizeof (compose_status));
9b07615b 9355
06a2c219
GM
9356 if (dpyinfo->grabbed
9357 && last_mouse_frame
9f67f20b
RS
9358 && FRAME_LIVE_P (last_mouse_frame))
9359 f = last_mouse_frame;
9360 else
2224b905 9361 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9f67f20b 9362
06a2c219
GM
9363 if (f)
9364 {
9365 /* Is this in the toolbar? */
9366 if (WINDOWP (f->toolbar_window)
9367 && XFASTINT (XWINDOW (f->toolbar_window)->height))
9368 {
9369 Lisp_Object window;
9370 int p, x, y;
9371
9372 x = event.xbutton.x;
9373 y = event.xbutton.y;
9374
9375 /* Set x and y. */
9376 window = window_from_coordinates (f, x, y, &p, 1);
9377 if (EQ (window, f->toolbar_window))
9378 {
9379 x_handle_toolbar_click (f, &event.xbutton);
9380 toolbar_p = 1;
9381 }
9382 }
9383
9384 if (!toolbar_p)
9385 if (!dpyinfo->x_focus_frame
9386 || f == dpyinfo->x_focus_frame)
9387 construct_mouse_click (&emacs_event, &event, f);
7a13e894
RS
9388 }
9389 else
9390 {
06a2c219 9391#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
9392 struct scroll_bar *bar
9393 = x_window_to_scroll_bar (event.xbutton.window);
f451eb13 9394
7a13e894
RS
9395 if (bar)
9396 x_scroll_bar_handle_click (bar, &event, &emacs_event);
06a2c219 9397#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9398 }
9399
9400 if (event.type == ButtonPress)
9401 {
9402 dpyinfo->grabbed |= (1 << event.xbutton.button);
9403 last_mouse_frame = f;
edad46f6
KH
9404 /* Ignore any mouse motion that happened
9405 before this event; any subsequent mouse-movement
9406 Emacs events should reflect only motion after
9407 the ButtonPress. */
a00e91cd
KH
9408 if (f != 0)
9409 f->mouse_moved = 0;
06a2c219
GM
9410
9411 if (!toolbar_p)
9412 last_toolbar_item = -1;
9413 if (display_busy_cursor_p)
9414 inhibit_busy_cursor = 2;
7a13e894 9415 }
3afe33e7
RS
9416 else
9417 {
7a13e894 9418 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
3afe33e7 9419 }
23faf38f 9420
7a13e894
RS
9421 if (numchars >= 1 && emacs_event.kind != no_event)
9422 {
9423 bcopy (&emacs_event, bufp, sizeof (struct input_event));
9424 bufp++;
9425 count++;
9426 numchars--;
9427 }
3afe33e7
RS
9428
9429#ifdef USE_X_TOOLKIT
2224b905
RS
9430 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
9431 /* For a down-event in the menu bar,
9432 don't pass it to Xt right now.
9433 Instead, save it away
9434 and we will pass it to Xt from kbd_buffer_get_event.
9435 That way, we can run some Lisp code first. */
91375f8f
RS
9436 if (f && event.type == ButtonPress
9437 /* Verify the event is really within the menu bar
9438 and not just sent to it due to grabbing. */
9439 && event.xbutton.x >= 0
9440 && event.xbutton.x < f->output_data.x->pixel_width
9441 && event.xbutton.y >= 0
9442 && event.xbutton.y < f->output_data.x->menubar_height
9443 && event.xbutton.same_screen)
2224b905 9444 {
8805890a 9445 SET_SAVED_BUTTON_EVENT;
2237cac9
RS
9446 XSETFRAME (last_mouse_press_frame, f);
9447 }
9448 else if (event.type == ButtonPress)
9449 {
9450 last_mouse_press_frame = Qnil;
30e671c3 9451 goto OTHER;
ce89ef46 9452 }
06a2c219 9453
2237cac9
RS
9454#ifdef USE_MOTIF /* This should do not harm for Lucid,
9455 but I am trying to be cautious. */
ce89ef46
RS
9456 else if (event.type == ButtonRelease)
9457 {
2237cac9 9458 if (!NILP (last_mouse_press_frame))
f10ded1c 9459 {
2237cac9
RS
9460 f = XFRAME (last_mouse_press_frame);
9461 if (f->output_data.x)
06a2c219 9462 SET_SAVED_BUTTON_EVENT;
f10ded1c 9463 }
06a2c219 9464 else
30e671c3 9465 goto OTHER;
2224b905 9466 }
2237cac9 9467#endif /* USE_MOTIF */
2224b905
RS
9468 else
9469 goto OTHER;
3afe33e7 9470#endif /* USE_X_TOOLKIT */
7a13e894
RS
9471 }
9472 break;
dc6f92b8 9473
7a13e894 9474 case CirculateNotify:
06a2c219
GM
9475 goto OTHER;
9476
7a13e894 9477 case CirculateRequest:
06a2c219
GM
9478 goto OTHER;
9479
9480 case VisibilityNotify:
9481 goto OTHER;
dc6f92b8 9482
7a13e894
RS
9483 case MappingNotify:
9484 /* Someone has changed the keyboard mapping - update the
9485 local cache. */
9486 switch (event.xmapping.request)
9487 {
9488 case MappingModifier:
9489 x_find_modifier_meanings (dpyinfo);
9490 /* This is meant to fall through. */
9491 case MappingKeyboard:
9492 XRefreshKeyboardMapping (&event.xmapping);
9493 }
7a13e894 9494 goto OTHER;
dc6f92b8 9495
7a13e894 9496 default:
7a13e894 9497 OTHER:
717ca130 9498#ifdef USE_X_TOOLKIT
7a13e894
RS
9499 BLOCK_INPUT;
9500 XtDispatchEvent (&event);
9501 UNBLOCK_INPUT;
3afe33e7 9502#endif /* USE_X_TOOLKIT */
7a13e894
RS
9503 break;
9504 }
dc6f92b8
JB
9505 }
9506 }
9507
06a2c219
GM
9508 out:;
9509
9a5196d0
RS
9510 /* On some systems, an X bug causes Emacs to get no more events
9511 when the window is destroyed. Detect that. (1994.) */
58769bee 9512 if (! event_found)
ef2a22d0 9513 {
ef2a22d0
RS
9514 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
9515 One XNOOP in 100 loops will make Emacs terminate.
9516 B. Bretthauer, 1994 */
9517 x_noop_count++;
58769bee 9518 if (x_noop_count >= 100)
ef2a22d0
RS
9519 {
9520 x_noop_count=0;
2224b905
RS
9521
9522 if (next_noop_dpyinfo == 0)
9523 next_noop_dpyinfo = x_display_list;
9524
9525 XNoOp (next_noop_dpyinfo->display);
9526
9527 /* Each time we get here, cycle through the displays now open. */
9528 next_noop_dpyinfo = next_noop_dpyinfo->next;
ef2a22d0
RS
9529 }
9530 }
502add23 9531
06a2c219 9532 /* If the focus was just given to an auto-raising frame,
0134a210 9533 raise it now. */
7a13e894 9534 /* ??? This ought to be able to handle more than one such frame. */
0134a210
RS
9535 if (pending_autoraise_frame)
9536 {
9537 x_raise_frame (pending_autoraise_frame);
9538 pending_autoraise_frame = 0;
9539 }
0134a210 9540
dc6f92b8
JB
9541 UNBLOCK_INPUT;
9542 return count;
9543}
06a2c219
GM
9544
9545
9546
dc6f92b8 9547\f
06a2c219
GM
9548/***********************************************************************
9549 Text Cursor
9550 ***********************************************************************/
9551
9552/* Note if the text cursor of window W has been overwritten by a
9553 drawing operation that outputs N glyphs starting at HPOS in the
9554 line given by output_cursor.vpos. N < 0 means all the rest of the
9555 line after HPOS has been written. */
9556
9557static void
9558note_overwritten_text_cursor (w, hpos, n)
9559 struct window *w;
9560 int hpos, n;
9561{
9562 if (updated_area == TEXT_AREA
9563 && output_cursor.vpos == w->phys_cursor.vpos
9564 && hpos <= w->phys_cursor.hpos
9565 && (n < 0
9566 || hpos + n > w->phys_cursor.hpos))
9567 w->phys_cursor_on_p = 0;
9568}
f451eb13
JB
9569
9570
06a2c219
GM
9571/* Set clipping for output in glyph row ROW. W is the window in which
9572 we operate. GC is the graphics context to set clipping in.
9573 WHOLE_LINE_P non-zero means include the areas used for truncation
9574 mark display and alike in the clipping rectangle.
9575
9576 ROW may be a text row or, e.g., a mode line. Text rows must be
9577 clipped to the interior of the window dedicated to text display,
9578 mode lines must be clipped to the whole window. */
dc6f92b8
JB
9579
9580static void
06a2c219
GM
9581x_clip_to_row (w, row, gc, whole_line_p)
9582 struct window *w;
9583 struct glyph_row *row;
9584 GC gc;
9585 int whole_line_p;
dc6f92b8 9586{
06a2c219
GM
9587 struct frame *f = XFRAME (WINDOW_FRAME (w));
9588 XRectangle clip_rect;
9589 int window_x, window_y, window_width, window_height;
dc6f92b8 9590
06a2c219 9591 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5c1aae96 9592
06a2c219
GM
9593 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
9594 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
9595 clip_rect.y = max (clip_rect.y, window_y);
9596 clip_rect.width = window_width;
9597 clip_rect.height = row->visible_height;
5c1aae96 9598
06a2c219
GM
9599 /* If clipping to the whole line, including trunc marks, extend
9600 the rectangle to the left and increase its width. */
9601 if (whole_line_p)
9602 {
9603 clip_rect.x -= FRAME_X_FLAGS_AREA_WIDTH (f);
9604 clip_rect.width += 2 * FRAME_X_FLAGS_AREA_WIDTH (f);
9605 }
5c1aae96 9606
06a2c219 9607 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
dc6f92b8
JB
9608}
9609
06a2c219
GM
9610
9611/* Draw a hollow box cursor on window W in glyph row ROW. */
dc6f92b8
JB
9612
9613static void
06a2c219
GM
9614x_draw_hollow_cursor (w, row)
9615 struct window *w;
9616 struct glyph_row *row;
dc6f92b8 9617{
06a2c219
GM
9618 struct frame *f = XFRAME (WINDOW_FRAME (w));
9619 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9620 Display *dpy = FRAME_X_DISPLAY (f);
9621 int x, y, wd, h;
9622 XGCValues xgcv;
9623 struct glyph *cursor_glyph;
9624 GC gc;
9625
9626 /* Compute frame-relative coordinates from window-relative
9627 coordinates. */
9628 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9629 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
9630 + row->ascent - w->phys_cursor_ascent);
9631 h = row->height - 1;
9632
9633 /* Get the glyph the cursor is on. If we can't tell because
9634 the current matrix is invalid or such, give up. */
9635 cursor_glyph = get_phys_cursor_glyph (w);
9636 if (cursor_glyph == NULL)
dc6f92b8
JB
9637 return;
9638
06a2c219
GM
9639 /* Compute the width of the rectangle to draw. If on a stretch
9640 glyph, and `x-stretch-block-cursor' is nil, don't draw a
9641 rectangle as wide as the glyph, but use a canonical character
9642 width instead. */
9643 wd = cursor_glyph->pixel_width - 1;
9644 if (cursor_glyph->type == STRETCH_GLYPH
9645 && !x_stretch_cursor_p)
9646 wd = min (CANON_X_UNIT (f), wd);
9647
9648 /* The foreground of cursor_gc is typically the same as the normal
9649 background color, which can cause the cursor box to be invisible. */
9650 xgcv.foreground = f->output_data.x->cursor_pixel;
9651 if (dpyinfo->scratch_cursor_gc)
9652 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
9653 else
9654 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
9655 GCForeground, &xgcv);
9656 gc = dpyinfo->scratch_cursor_gc;
9657
9658 /* Set clipping, draw the rectangle, and reset clipping again. */
9659 x_clip_to_row (w, row, gc, 0);
9660 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
9661 XSetClipMask (dpy, gc, None);
dc6f92b8
JB
9662}
9663
06a2c219
GM
9664
9665/* Draw a bar cursor on window W in glyph row ROW.
9666
9667 Implementation note: One would like to draw a bar cursor with an
9668 angle equal to the one given by the font property XA_ITALIC_ANGLE.
9669 Unfortunately, I didn't find a font yet that has this property set.
9670 --gerd. */
dc6f92b8
JB
9671
9672static void
06a2c219
GM
9673x_draw_bar_cursor (w, row)
9674 struct window *w;
9675 struct glyph_row *row;
dc6f92b8 9676{
06a2c219
GM
9677 /* If cursor hpos is out of bounds, don't draw garbage. This can
9678 happen in mini-buffer windows when switching between echo area
9679 glyphs and mini-buffer. */
9680 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
9681 {
9682 struct frame *f = XFRAME (w->frame);
9683 struct glyph *cursor_glyph;
9684 GC gc;
9685 int x;
9686 unsigned long mask;
9687 XGCValues xgcv;
9688 Display *dpy;
9689 Window window;
9690
9691 cursor_glyph = get_phys_cursor_glyph (w);
9692 if (cursor_glyph == NULL)
9693 return;
9694
9695 xgcv.background = f->output_data.x->cursor_pixel;
9696 xgcv.foreground = f->output_data.x->cursor_pixel;
9697 xgcv.graphics_exposures = 0;
9698 mask = GCForeground | GCBackground | GCGraphicsExposures;
9699 dpy = FRAME_X_DISPLAY (f);
9700 window = FRAME_X_WINDOW (f);
9701 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
9702
9703 if (gc)
9704 XChangeGC (dpy, gc, mask, &xgcv);
9705 else
9706 {
9707 gc = XCreateGC (dpy, window, mask, &xgcv);
9708 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
9709 }
9710
9711 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9712 x_clip_to_row (w, row, gc, 0);
9713 XFillRectangle (dpy, window, gc,
9714 x,
9715 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
9716 min (cursor_glyph->pixel_width,
9717 f->output_data.x->cursor_width),
9718 row->height);
9719 XSetClipMask (dpy, gc, None);
9720 }
dc6f92b8
JB
9721}
9722
06a2c219
GM
9723
9724/* Clear the cursor of window W to background color, and mark the
9725 cursor as not shown. This is used when the text where the cursor
9726 is is about to be rewritten. */
9727
dc6f92b8 9728static void
06a2c219
GM
9729x_clear_cursor (w)
9730 struct window *w;
dc6f92b8 9731{
06a2c219
GM
9732 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
9733 x_update_window_cursor (w, 0);
9734}
90e65f07 9735
dbc4e1c1 9736
06a2c219
GM
9737/* Draw the cursor glyph of window W in glyph row ROW. See the
9738 comment of x_draw_glyphs for the meaning of HL. */
dbc4e1c1 9739
06a2c219
GM
9740static void
9741x_draw_phys_cursor_glyph (w, row, hl)
9742 struct window *w;
9743 struct glyph_row *row;
9744 enum draw_glyphs_face hl;
9745{
9746 /* If cursor hpos is out of bounds, don't draw garbage. This can
9747 happen in mini-buffer windows when switching between echo area
9748 glyphs and mini-buffer. */
9749 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
9750 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
9751 w->phys_cursor.hpos, w->phys_cursor.hpos + 1, hl, 0, 0);
9752}
dbc4e1c1 9753
eea6af04 9754
06a2c219 9755/* Erase the image of a cursor of window W from the screen. */
eea6af04 9756
06a2c219
GM
9757static void
9758x_erase_phys_cursor (w)
9759 struct window *w;
9760{
9761 struct frame *f = XFRAME (w->frame);
9762 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9763 int hpos = w->phys_cursor.hpos;
9764 int vpos = w->phys_cursor.vpos;
9765 int mouse_face_here_p = 0;
9766 struct glyph_matrix *active_glyphs = w->current_matrix;
9767 struct glyph_row *cursor_row;
9768 struct glyph *cursor_glyph;
9769 enum draw_glyphs_face hl;
9770
9771 /* No cursor displayed or row invalidated => nothing to do on the
9772 screen. */
9773 if (w->phys_cursor_type == NO_CURSOR)
9774 goto mark_cursor_off;
9775
9776 /* VPOS >= active_glyphs->nrows means that window has been resized.
9777 Don't bother to erase the cursor. */
9778 if (vpos >= active_glyphs->nrows)
9779 goto mark_cursor_off;
9780
9781 /* If row containing cursor is marked invalid, there is nothing we
9782 can do. */
9783 cursor_row = MATRIX_ROW (active_glyphs, vpos);
9784 if (!cursor_row->enabled_p)
9785 goto mark_cursor_off;
9786
9787 /* This can happen when the new row is shorter than the old one.
9788 In this case, either x_draw_glyphs or clear_end_of_line
9789 should have cleared the cursor. Note that we wouldn't be
9790 able to erase the cursor in this case because we don't have a
9791 cursor glyph at hand. */
9792 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
9793 goto mark_cursor_off;
9794
9795 /* If the cursor is in the mouse face area, redisplay that when
9796 we clear the cursor. */
9797 if (w == XWINDOW (dpyinfo->mouse_face_window)
9798 && (vpos > dpyinfo->mouse_face_beg_row
9799 || (vpos == dpyinfo->mouse_face_beg_row
9800 && hpos >= dpyinfo->mouse_face_beg_col))
9801 && (vpos < dpyinfo->mouse_face_end_row
9802 || (vpos == dpyinfo->mouse_face_end_row
9803 && hpos < dpyinfo->mouse_face_end_col))
9804 /* Don't redraw the cursor's spot in mouse face if it is at the
9805 end of a line (on a newline). The cursor appears there, but
9806 mouse highlighting does not. */
9807 && cursor_row->used[TEXT_AREA] > hpos)
9808 mouse_face_here_p = 1;
9809
9810 /* Maybe clear the display under the cursor. */
9811 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
9812 {
9813 int x;
9814 int top_line_height = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
dbc4e1c1 9815
06a2c219
GM
9816 cursor_glyph = get_phys_cursor_glyph (w);
9817 if (cursor_glyph == NULL)
9818 goto mark_cursor_off;
dbc4e1c1 9819
06a2c219
GM
9820 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
9821
9822 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9823 x,
9824 WINDOW_TO_FRAME_PIXEL_Y (w, max (top_line_height,
9825 cursor_row->y)),
9826 cursor_glyph->pixel_width,
9827 cursor_row->visible_height,
9828 False);
dbc4e1c1 9829 }
06a2c219
GM
9830
9831 /* Erase the cursor by redrawing the character underneath it. */
9832 if (mouse_face_here_p)
9833 hl = DRAW_MOUSE_FACE;
9834 else if (cursor_row->inverse_p)
9835 hl = DRAW_INVERSE_VIDEO;
9836 else
9837 hl = DRAW_NORMAL_TEXT;
9838 x_draw_phys_cursor_glyph (w, cursor_row, hl);
dbc4e1c1 9839
06a2c219
GM
9840 mark_cursor_off:
9841 w->phys_cursor_on_p = 0;
9842 w->phys_cursor_type = NO_CURSOR;
dbc4e1c1
JB
9843}
9844
9845
06a2c219
GM
9846/* Display or clear cursor of window W. If ON is zero, clear the
9847 cursor. If it is non-zero, display the cursor. If ON is nonzero,
9848 where to put the cursor is specified by HPOS, VPOS, X and Y. */
dbc4e1c1 9849
06a2c219
GM
9850void
9851x_display_and_set_cursor (w, on, hpos, vpos, x, y)
9852 struct window *w;
9853 int on, hpos, vpos, x, y;
dbc4e1c1 9854{
06a2c219
GM
9855 struct frame *f = XFRAME (w->frame);
9856 int new_cursor_type;
9857 struct glyph_matrix *current_glyphs;
9858 struct glyph_row *glyph_row;
9859 struct glyph *glyph;
dbc4e1c1 9860
49d838ea 9861 /* This is pointless on invisible frames, and dangerous on garbaged
06a2c219
GM
9862 windows and frames; in the latter case, the frame or window may
9863 be in the midst of changing its size, and x and y may be off the
9864 window. */
9865 if (! FRAME_VISIBLE_P (f)
9866 || FRAME_GARBAGED_P (f)
9867 || vpos >= w->current_matrix->nrows
9868 || hpos >= w->current_matrix->matrix_w)
dc6f92b8
JB
9869 return;
9870
9871 /* If cursor is off and we want it off, return quickly. */
06a2c219 9872 if (!on && !w->phys_cursor_on_p)
dc6f92b8
JB
9873 return;
9874
06a2c219
GM
9875 current_glyphs = w->current_matrix;
9876 glyph_row = MATRIX_ROW (current_glyphs, vpos);
9877 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
9878
9879 /* If cursor row is not enabled, we don't really know where to
9880 display the cursor. */
9881 if (!glyph_row->enabled_p)
9882 {
9883 w->phys_cursor_on_p = 0;
9884 return;
9885 }
9886
9887 xassert (interrupt_input_blocked);
9888
9889 /* Set new_cursor_type to the cursor we want to be displayed. In a
9890 mini-buffer window, we want the cursor only to appear if we are
9891 reading input from this window. For the selected window, we want
9892 the cursor type given by the frame parameter. If explicitly
9893 marked off, draw no cursor. In all other cases, we want a hollow
9894 box cursor. */
9895 if (w != XWINDOW (selected_window)
9896 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
9897 {
9898 if (MINI_WINDOW_P (w))
9899 new_cursor_type = NO_CURSOR;
9900 else
9901 new_cursor_type = HOLLOW_BOX_CURSOR;
9902 }
9903 else if (w->cursor_off_p)
9904 new_cursor_type = NO_CURSOR;
9905 else
9906 new_cursor_type = FRAME_DESIRED_CURSOR (f);
9907
9908 /* If cursor is currently being shown and we don't want it to be or
9909 it is in the wrong place, or the cursor type is not what we want,
dc6f92b8 9910 erase it. */
06a2c219 9911 if (w->phys_cursor_on_p
dc6f92b8 9912 && (!on
06a2c219
GM
9913 || w->phys_cursor.x != x
9914 || w->phys_cursor.y != y
9915 || new_cursor_type != w->phys_cursor_type))
9916 x_erase_phys_cursor (w);
9917
9918 /* If the cursor is now invisible and we want it to be visible,
9919 display it. */
9920 if (on && !w->phys_cursor_on_p)
9921 {
9922 w->phys_cursor_ascent = glyph_row->ascent;
9923 w->phys_cursor_height = glyph_row->height;
9924
9925 /* Set phys_cursor_.* before x_draw_.* is called because some
9926 of them may need the information. */
9927 w->phys_cursor.x = x;
9928 w->phys_cursor.y = glyph_row->y;
9929 w->phys_cursor.hpos = hpos;
9930 w->phys_cursor.vpos = vpos;
9931 w->phys_cursor_type = new_cursor_type;
9932 w->phys_cursor_on_p = 1;
9933
9934 switch (new_cursor_type)
dc6f92b8 9935 {
06a2c219
GM
9936 case HOLLOW_BOX_CURSOR:
9937 x_draw_hollow_cursor (w, glyph_row);
9938 break;
9939
9940 case FILLED_BOX_CURSOR:
9941 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
9942 break;
9943
9944 case BAR_CURSOR:
9945 x_draw_bar_cursor (w, glyph_row);
9946 break;
9947
9948 case NO_CURSOR:
9949 break;
dc6f92b8 9950
06a2c219
GM
9951 default:
9952 abort ();
9953 }
dc6f92b8
JB
9954 }
9955
06a2c219 9956#ifndef XFlush
f676886a 9957 if (updating_frame != f)
334208b7 9958 XFlush (FRAME_X_DISPLAY (f));
06a2c219 9959#endif
dc6f92b8
JB
9960}
9961
06a2c219
GM
9962
9963/* Display the cursor on window W, or clear it. X and Y are window
9964 relative pixel coordinates. HPOS and VPOS are glyph matrix
9965 positions. If W is not the selected window, display a hollow
9966 cursor. ON non-zero means display the cursor at X, Y which
9967 correspond to HPOS, VPOS, otherwise it is cleared. */
5d46f928 9968
dfcf069d 9969void
06a2c219
GM
9970x_display_cursor (w, on, hpos, vpos, x, y)
9971 struct window *w;
9972 int on, hpos, vpos, x, y;
dc6f92b8 9973{
f94397b5 9974 BLOCK_INPUT;
06a2c219 9975 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
5d46f928
RS
9976 UNBLOCK_INPUT;
9977}
9978
06a2c219
GM
9979
9980/* Display the cursor on window W, or clear it, according to ON_P.
5d46f928
RS
9981 Don't change the cursor's position. */
9982
dfcf069d 9983void
06a2c219 9984x_update_cursor (f, on_p)
5d46f928 9985 struct frame *f;
5d46f928 9986{
06a2c219
GM
9987 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
9988}
9989
9990
9991/* Call x_update_window_cursor with parameter ON_P on all leaf windows
9992 in the window tree rooted at W. */
9993
9994static void
9995x_update_cursor_in_window_tree (w, on_p)
9996 struct window *w;
9997 int on_p;
9998{
9999 while (w)
10000 {
10001 if (!NILP (w->hchild))
10002 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10003 else if (!NILP (w->vchild))
10004 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10005 else
10006 x_update_window_cursor (w, on_p);
10007
10008 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10009 }
10010}
5d46f928 10011
f94397b5 10012
06a2c219
GM
10013/* Switch the display of W's cursor on or off, according to the value
10014 of ON. */
10015
10016static void
10017x_update_window_cursor (w, on)
10018 struct window *w;
10019 int on;
10020{
10021 BLOCK_INPUT;
10022 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
10023 w->phys_cursor.x, w->phys_cursor.y);
f94397b5 10024 UNBLOCK_INPUT;
dc6f92b8 10025}
06a2c219
GM
10026
10027
10028
dc6f92b8
JB
10029\f
10030/* Icons. */
10031
f676886a 10032/* Refresh bitmap kitchen sink icon for frame F
06a2c219 10033 when we get an expose event for it. */
dc6f92b8 10034
dfcf069d 10035void
f676886a
JB
10036refreshicon (f)
10037 struct frame *f;
dc6f92b8 10038{
06a2c219 10039 /* Normally, the window manager handles this function. */
dc6f92b8
JB
10040}
10041
dbc4e1c1 10042/* Make the x-window of frame F use the gnu icon bitmap. */
dc6f92b8
JB
10043
10044int
990ba854 10045x_bitmap_icon (f, file)
f676886a 10046 struct frame *f;
990ba854 10047 Lisp_Object file;
dc6f92b8 10048{
06a2c219 10049 int bitmap_id;
dc6f92b8 10050
c118dd06 10051 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
10052 return 1;
10053
990ba854 10054 /* Free up our existing icon bitmap if any. */
7556890b
RS
10055 if (f->output_data.x->icon_bitmap > 0)
10056 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10057 f->output_data.x->icon_bitmap = 0;
990ba854
RS
10058
10059 if (STRINGP (file))
7f2ae036
RS
10060 bitmap_id = x_create_bitmap_from_file (f, file);
10061 else
10062 {
990ba854 10063 /* Create the GNU bitmap if necessary. */
5bf01b68 10064 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
334208b7
RS
10065 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
10066 = x_create_bitmap_from_data (f, gnu_bits,
10067 gnu_width, gnu_height);
990ba854
RS
10068
10069 /* The first time we create the GNU bitmap,
06a2c219 10070 this increments the ref-count one extra time.
990ba854
RS
10071 As a result, the GNU bitmap is never freed.
10072 That way, we don't have to worry about allocating it again. */
334208b7 10073 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
990ba854 10074
334208b7 10075 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7f2ae036
RS
10076 }
10077
10078 x_wm_set_icon_pixmap (f, bitmap_id);
7556890b 10079 f->output_data.x->icon_bitmap = bitmap_id;
dc6f92b8
JB
10080
10081 return 0;
10082}
10083
10084
1be2d067
KH
10085/* Make the x-window of frame F use a rectangle with text.
10086 Use ICON_NAME as the text. */
dc6f92b8
JB
10087
10088int
f676886a
JB
10089x_text_icon (f, icon_name)
10090 struct frame *f;
dc6f92b8
JB
10091 char *icon_name;
10092{
c118dd06 10093 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
10094 return 1;
10095
1be2d067
KH
10096#ifdef HAVE_X11R4
10097 {
10098 XTextProperty text;
10099 text.value = (unsigned char *) icon_name;
10100 text.encoding = XA_STRING;
10101 text.format = 8;
10102 text.nitems = strlen (icon_name);
10103#ifdef USE_X_TOOLKIT
7556890b 10104 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
1be2d067
KH
10105 &text);
10106#else /* not USE_X_TOOLKIT */
10107 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
10108#endif /* not USE_X_TOOLKIT */
10109 }
10110#else /* not HAVE_X11R4 */
10111 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
10112#endif /* not HAVE_X11R4 */
58769bee 10113
7556890b
RS
10114 if (f->output_data.x->icon_bitmap > 0)
10115 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10116 f->output_data.x->icon_bitmap = 0;
b1c884c3 10117 x_wm_set_icon_pixmap (f, 0);
dc6f92b8
JB
10118
10119 return 0;
10120}
10121\f
e99db5a1
RS
10122#define X_ERROR_MESSAGE_SIZE 200
10123
10124/* If non-nil, this should be a string.
10125 It means catch X errors and store the error message in this string. */
10126
10127static Lisp_Object x_error_message_string;
10128
10129/* An X error handler which stores the error message in
10130 x_error_message_string. This is called from x_error_handler if
10131 x_catch_errors is in effect. */
10132
06a2c219 10133static void
e99db5a1
RS
10134x_error_catcher (display, error)
10135 Display *display;
10136 XErrorEvent *error;
10137{
10138 XGetErrorText (display, error->error_code,
10139 XSTRING (x_error_message_string)->data,
10140 X_ERROR_MESSAGE_SIZE);
10141}
10142
10143/* Begin trapping X errors for display DPY. Actually we trap X errors
10144 for all displays, but DPY should be the display you are actually
10145 operating on.
10146
10147 After calling this function, X protocol errors no longer cause
10148 Emacs to exit; instead, they are recorded in the string
10149 stored in x_error_message_string.
10150
10151 Calling x_check_errors signals an Emacs error if an X error has
10152 occurred since the last call to x_catch_errors or x_check_errors.
10153
10154 Calling x_uncatch_errors resumes the normal error handling. */
10155
10156void x_check_errors ();
10157static Lisp_Object x_catch_errors_unwind ();
10158
10159int
10160x_catch_errors (dpy)
10161 Display *dpy;
10162{
10163 int count = specpdl_ptr - specpdl;
10164
10165 /* Make sure any errors from previous requests have been dealt with. */
10166 XSync (dpy, False);
10167
10168 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
10169
10170 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
10171 XSTRING (x_error_message_string)->data[0] = 0;
10172
10173 return count;
10174}
10175
10176/* Unbind the binding that we made to check for X errors. */
10177
10178static Lisp_Object
10179x_catch_errors_unwind (old_val)
10180 Lisp_Object old_val;
10181{
10182 x_error_message_string = old_val;
10183 return Qnil;
10184}
10185
10186/* If any X protocol errors have arrived since the last call to
10187 x_catch_errors or x_check_errors, signal an Emacs error using
10188 sprintf (a buffer, FORMAT, the x error message text) as the text. */
10189
10190void
10191x_check_errors (dpy, format)
10192 Display *dpy;
10193 char *format;
10194{
10195 /* Make sure to catch any errors incurred so far. */
10196 XSync (dpy, False);
10197
10198 if (XSTRING (x_error_message_string)->data[0])
10199 error (format, XSTRING (x_error_message_string)->data);
10200}
10201
9829ddba
RS
10202/* Nonzero if we had any X protocol errors
10203 since we did x_catch_errors on DPY. */
e99db5a1
RS
10204
10205int
10206x_had_errors_p (dpy)
10207 Display *dpy;
10208{
10209 /* Make sure to catch any errors incurred so far. */
10210 XSync (dpy, False);
10211
10212 return XSTRING (x_error_message_string)->data[0] != 0;
10213}
10214
9829ddba
RS
10215/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
10216
06a2c219 10217void
9829ddba
RS
10218x_clear_errors (dpy)
10219 Display *dpy;
10220{
10221 XSTRING (x_error_message_string)->data[0] = 0;
10222}
10223
e99db5a1
RS
10224/* Stop catching X protocol errors and let them make Emacs die.
10225 DPY should be the display that was passed to x_catch_errors.
10226 COUNT should be the value that was returned by
10227 the corresponding call to x_catch_errors. */
10228
10229void
10230x_uncatch_errors (dpy, count)
10231 Display *dpy;
10232 int count;
10233{
10234 unbind_to (count, Qnil);
10235}
10236
10237#if 0
10238static unsigned int x_wire_count;
10239x_trace_wire ()
10240{
10241 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
10242}
10243#endif /* ! 0 */
10244
10245\f
10246/* Handle SIGPIPE, which can happen when the connection to a server
10247 simply goes away. SIGPIPE is handled by x_connection_signal.
10248 Don't need to do anything, because the write which caused the
10249 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
06a2c219 10250 which will do the appropriate cleanup for us. */
e99db5a1
RS
10251
10252static SIGTYPE
10253x_connection_signal (signalnum) /* If we don't have an argument, */
06a2c219 10254 int signalnum; /* some compilers complain in signal calls. */
e99db5a1
RS
10255{
10256#ifdef USG
10257 /* USG systems forget handlers when they are used;
10258 must reestablish each time */
10259 signal (signalnum, x_connection_signal);
10260#endif /* USG */
10261}
10262\f
4746118a
JB
10263/* Handling X errors. */
10264
7a13e894 10265/* Handle the loss of connection to display DISPLAY. */
16bd92ea 10266
4746118a 10267static SIGTYPE
7a13e894
RS
10268x_connection_closed (display, error_message)
10269 Display *display;
10270 char *error_message;
4746118a 10271{
7a13e894
RS
10272 struct x_display_info *dpyinfo = x_display_info_for_display (display);
10273 Lisp_Object frame, tail;
10274
6186a4a0
RS
10275 /* Indicate that this display is dead. */
10276
f613a4c8 10277#ifdef USE_X_TOOLKIT
adabc3a9 10278 XtCloseDisplay (display);
f613a4c8 10279#endif
adabc3a9 10280
6186a4a0
RS
10281 dpyinfo->display = 0;
10282
06a2c219 10283 /* First delete frames whose mini-buffers are on frames
7a13e894
RS
10284 that are on the dead display. */
10285 FOR_EACH_FRAME (tail, frame)
10286 {
10287 Lisp_Object minibuf_frame;
10288 minibuf_frame
10289 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
f48f33ca
RS
10290 if (FRAME_X_P (XFRAME (frame))
10291 && FRAME_X_P (XFRAME (minibuf_frame))
10292 && ! EQ (frame, minibuf_frame)
10293 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7a13e894
RS
10294 Fdelete_frame (frame, Qt);
10295 }
10296
10297 /* Now delete all remaining frames on the dead display.
06a2c219 10298 We are now sure none of these is used as the mini-buffer
7a13e894
RS
10299 for another frame that we need to delete. */
10300 FOR_EACH_FRAME (tail, frame)
f48f33ca
RS
10301 if (FRAME_X_P (XFRAME (frame))
10302 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
07a7096a
KH
10303 {
10304 /* Set this to t so that Fdelete_frame won't get confused
10305 trying to find a replacement. */
10306 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
10307 Fdelete_frame (frame, Qt);
10308 }
7a13e894 10309
482a1bd2
KH
10310 if (dpyinfo)
10311 x_delete_display (dpyinfo);
7a13e894
RS
10312
10313 if (x_display_list == 0)
10314 {
f8d07b62 10315 fprintf (stderr, "%s\n", error_message);
7a13e894
RS
10316 shut_down_emacs (0, 0, Qnil);
10317 exit (70);
10318 }
12ba150f 10319
7a13e894
RS
10320 /* Ordinary stack unwind doesn't deal with these. */
10321#ifdef SIGIO
10322 sigunblock (sigmask (SIGIO));
10323#endif
10324 sigunblock (sigmask (SIGALRM));
10325 TOTALLY_UNBLOCK_INPUT;
10326
aa4d9a9e 10327 clear_waiting_for_input ();
7a13e894 10328 error ("%s", error_message);
4746118a
JB
10329}
10330
7a13e894
RS
10331/* This is the usual handler for X protocol errors.
10332 It kills all frames on the display that we got the error for.
10333 If that was the only one, it prints an error message and kills Emacs. */
10334
06a2c219 10335static void
c118dd06
JB
10336x_error_quitter (display, error)
10337 Display *display;
10338 XErrorEvent *error;
10339{
7a13e894 10340 char buf[256], buf1[356];
dc6f92b8 10341
58769bee 10342 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 10343 original error handler. */
dc6f92b8 10344
c118dd06 10345 XGetErrorText (display, error->error_code, buf, sizeof (buf));
0a0fdc70 10346 sprintf (buf1, "X protocol error: %s on protocol request %d",
c118dd06 10347 buf, error->request_code);
7a13e894 10348 x_connection_closed (display, buf1);
dc6f92b8
JB
10349}
10350
e99db5a1
RS
10351/* This is the first-level handler for X protocol errors.
10352 It calls x_error_quitter or x_error_catcher. */
7a13e894 10353
8922af5f 10354static int
e99db5a1 10355x_error_handler (display, error)
8922af5f 10356 Display *display;
e99db5a1 10357 XErrorEvent *error;
8922af5f 10358{
e99db5a1
RS
10359 if (! NILP (x_error_message_string))
10360 x_error_catcher (display, error);
10361 else
10362 x_error_quitter (display, error);
06a2c219 10363 return 0;
f9e24cb9 10364}
c118dd06 10365
e99db5a1
RS
10366/* This is the handler for X IO errors, always.
10367 It kills all frames on the display that we lost touch with.
10368 If that was the only one, it prints an error message and kills Emacs. */
7a13e894 10369
c118dd06 10370static int
e99db5a1 10371x_io_error_quitter (display)
c118dd06 10372 Display *display;
c118dd06 10373{
e99db5a1 10374 char buf[256];
dc6f92b8 10375
e99db5a1
RS
10376 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
10377 x_connection_closed (display, buf);
06a2c219 10378 return 0;
dc6f92b8 10379}
dc6f92b8 10380\f
f451eb13
JB
10381/* Changing the font of the frame. */
10382
76bcdf39
RS
10383/* Give frame F the font named FONTNAME as its default font, and
10384 return the full name of that font. FONTNAME may be a wildcard
10385 pattern; in that case, we choose some font that fits the pattern.
10386 The return value shows which font we chose. */
10387
b5cf7a0e 10388Lisp_Object
f676886a
JB
10389x_new_font (f, fontname)
10390 struct frame *f;
dc6f92b8
JB
10391 register char *fontname;
10392{
dc43ef94
KH
10393 struct font_info *fontp
10394 = fs_load_font (f, FRAME_X_FONT_TABLE (f), CHARSET_ASCII, fontname, -1);
dc6f92b8 10395
dc43ef94
KH
10396 if (!fontp)
10397 return Qnil;
2224a5fc 10398
dc43ef94
KH
10399 f->output_data.x->font = (XFontStruct *) (fontp->font);
10400 f->output_data.x->font_baseline
10401 = (f->output_data.x->font->ascent + fontp->baseline_offset);
10402 f->output_data.x->fontset = -1;
10403
b2cad826
KH
10404 /* Compute the scroll bar width in character columns. */
10405 if (f->scroll_bar_pixel_width > 0)
10406 {
7556890b 10407 int wid = FONT_WIDTH (f->output_data.x->font);
b2cad826
KH
10408 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
10409 }
10410 else
4e61bddf
RS
10411 {
10412 int wid = FONT_WIDTH (f->output_data.x->font);
10413 f->scroll_bar_cols = (14 + wid - 1) / wid;
10414 }
b2cad826 10415
f676886a 10416 /* Now make the frame display the given font. */
c118dd06 10417 if (FRAME_X_WINDOW (f) != 0)
dc6f92b8 10418 {
7556890b
RS
10419 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
10420 f->output_data.x->font->fid);
10421 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
10422 f->output_data.x->font->fid);
10423 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
10424 f->output_data.x->font->fid);
f676886a 10425
a27f9f86 10426 frame_update_line_height (f);
0134a210 10427 x_set_window_size (f, 0, f->width, f->height);
dc6f92b8 10428 }
a27f9f86
RS
10429 else
10430 /* If we are setting a new frame's font for the first time,
10431 there are no faces yet, so this font's height is the line height. */
7556890b 10432 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
dc6f92b8 10433
dc43ef94
KH
10434 return build_string (fontp->full_name);
10435}
10436
10437/* Give frame F the fontset named FONTSETNAME as its default font, and
10438 return the full name of that fontset. FONTSETNAME may be a wildcard
b5210ea7
KH
10439 pattern; in that case, we choose some fontset that fits the pattern.
10440 The return value shows which fontset we chose. */
b5cf7a0e 10441
dc43ef94
KH
10442Lisp_Object
10443x_new_fontset (f, fontsetname)
10444 struct frame *f;
10445 char *fontsetname;
10446{
10447 int fontset = fs_query_fontset (f, fontsetname);
10448 struct fontset_info *fontsetp;
10449 Lisp_Object result;
b5cf7a0e 10450
dc43ef94
KH
10451 if (fontset < 0)
10452 return Qnil;
b5cf7a0e 10453
2da424f1
KH
10454 if (f->output_data.x->fontset == fontset)
10455 /* This fontset is already set in frame F. There's nothing more
10456 to do. */
10457 return build_string (fontsetname);
10458
dc43ef94
KH
10459 fontsetp = FRAME_FONTSET_DATA (f)->fontset_table[fontset];
10460
10461 if (!fontsetp->fontname[CHARSET_ASCII])
10462 /* This fontset doesn't contain ASCII font. */
10463 return Qnil;
10464
10465 result = x_new_font (f, fontsetp->fontname[CHARSET_ASCII]);
10466
10467 if (!STRINGP (result))
10468 /* Can't load ASCII font. */
10469 return Qnil;
10470
10471 /* Since x_new_font doesn't update any fontset information, do it now. */
10472 f->output_data.x->fontset = fontset;
2da424f1 10473 FS_LOAD_FONT (f, FRAME_X_FONT_TABLE (f),
715a159b 10474 CHARSET_ASCII, fontsetp->fontname[CHARSET_ASCII], fontset);
dc43ef94
KH
10475
10476 return build_string (fontsetname);
dc6f92b8 10477}
dc6f92b8 10478\f
2e365682
RS
10479/* Calculate the absolute position in frame F
10480 from its current recorded position values and gravity. */
10481
dfcf069d 10482void
43bca5d5 10483x_calc_absolute_position (f)
f676886a 10484 struct frame *f;
dc6f92b8 10485{
06a2c219 10486 Window child;
6dba1858 10487 int win_x = 0, win_y = 0;
7556890b 10488 int flags = f->output_data.x->size_hint_flags;
c81412a0
KH
10489 int this_window;
10490
9829ddba
RS
10491 /* We have nothing to do if the current position
10492 is already for the top-left corner. */
10493 if (! ((flags & XNegative) || (flags & YNegative)))
10494 return;
10495
c81412a0 10496#ifdef USE_X_TOOLKIT
7556890b 10497 this_window = XtWindow (f->output_data.x->widget);
c81412a0
KH
10498#else
10499 this_window = FRAME_X_WINDOW (f);
10500#endif
6dba1858
RS
10501
10502 /* Find the position of the outside upper-left corner of
9829ddba
RS
10503 the inner window, with respect to the outer window.
10504 But do this only if we will need the results. */
7556890b 10505 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6dba1858 10506 {
9829ddba
RS
10507 int count;
10508
6dba1858 10509 BLOCK_INPUT;
9829ddba
RS
10510 count = x_catch_errors (FRAME_X_DISPLAY (f));
10511 while (1)
10512 {
10513 x_clear_errors (FRAME_X_DISPLAY (f));
10514 XTranslateCoordinates (FRAME_X_DISPLAY (f),
10515
10516 /* From-window, to-window. */
10517 this_window,
10518 f->output_data.x->parent_desc,
10519
10520 /* From-position, to-position. */
10521 0, 0, &win_x, &win_y,
10522
10523 /* Child of win. */
10524 &child);
10525 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
10526 {
10527 Window newroot, newparent = 0xdeadbeef;
10528 Window *newchildren;
10529 int nchildren;
10530
10531 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
10532 &newparent, &newchildren, &nchildren))
10533 break;
58769bee 10534
7c3c78a3 10535 XFree ((char *) newchildren);
6dba1858 10536
9829ddba
RS
10537 f->output_data.x->parent_desc = newparent;
10538 }
10539 else
10540 break;
10541 }
6dba1858 10542
9829ddba 10543 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
6dba1858
RS
10544 UNBLOCK_INPUT;
10545 }
10546
10547 /* Treat negative positions as relative to the leftmost bottommost
10548 position that fits on the screen. */
20f55f9a 10549 if (flags & XNegative)
7556890b 10550 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
2e365682
RS
10551 - 2 * f->output_data.x->border_width - win_x
10552 - PIXEL_WIDTH (f)
10553 + f->output_data.x->left_pos);
dc6f92b8 10554
20f55f9a 10555 if (flags & YNegative)
06a2c219
GM
10556 {
10557 int menubar_height = 0;
10558
10559#ifdef USE_X_TOOLKIT
10560 if (f->output_data.x->menubar_widget)
10561 menubar_height
10562 = (f->output_data.x->menubar_widget->core.height
10563 + f->output_data.x->menubar_widget->core.border_width);
10564#endif
10565
10566 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
10567 - 2 * f->output_data.x->border_width
10568 - win_y
10569 - PIXEL_HEIGHT (f)
10570 - menubar_height
10571 + f->output_data.x->top_pos);
10572 }
2e365682 10573
3a35ab44
RS
10574 /* The left_pos and top_pos
10575 are now relative to the top and left screen edges,
10576 so the flags should correspond. */
7556890b 10577 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
dc6f92b8
JB
10578}
10579
3a35ab44
RS
10580/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
10581 to really change the position, and 0 when calling from
10582 x_make_frame_visible (in that case, XOFF and YOFF are the current
aa3ff7c9
KH
10583 position values). It is -1 when calling from x_set_frame_parameters,
10584 which means, do adjust for borders but don't change the gravity. */
3a35ab44 10585
dfcf069d 10586void
dc05a16b 10587x_set_offset (f, xoff, yoff, change_gravity)
f676886a 10588 struct frame *f;
dc6f92b8 10589 register int xoff, yoff;
dc05a16b 10590 int change_gravity;
dc6f92b8 10591{
4a4cbdd5
KH
10592 int modified_top, modified_left;
10593
aa3ff7c9 10594 if (change_gravity > 0)
3a35ab44 10595 {
7556890b
RS
10596 f->output_data.x->top_pos = yoff;
10597 f->output_data.x->left_pos = xoff;
10598 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
3a35ab44 10599 if (xoff < 0)
7556890b 10600 f->output_data.x->size_hint_flags |= XNegative;
3a35ab44 10601 if (yoff < 0)
7556890b
RS
10602 f->output_data.x->size_hint_flags |= YNegative;
10603 f->output_data.x->win_gravity = NorthWestGravity;
3a35ab44 10604 }
43bca5d5 10605 x_calc_absolute_position (f);
dc6f92b8
JB
10606
10607 BLOCK_INPUT;
c32cdd9a 10608 x_wm_set_size_hint (f, (long) 0, 0);
3a35ab44 10609
7556890b
RS
10610 modified_left = f->output_data.x->left_pos;
10611 modified_top = f->output_data.x->top_pos;
e73ec6fa
RS
10612#if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
10613 this seems to be unnecessary and incorrect. rms, 4/17/97. */
10614 /* It is a mystery why we need to add the border_width here
10615 when the frame is already visible, but experiment says we do. */
aa3ff7c9 10616 if (change_gravity != 0)
4a4cbdd5 10617 {
7556890b
RS
10618 modified_left += f->output_data.x->border_width;
10619 modified_top += f->output_data.x->border_width;
4a4cbdd5 10620 }
e73ec6fa 10621#endif
4a4cbdd5 10622
3afe33e7 10623#ifdef USE_X_TOOLKIT
7556890b 10624 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
4a4cbdd5 10625 modified_left, modified_top);
3afe33e7 10626#else /* not USE_X_TOOLKIT */
334208b7 10627 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4a4cbdd5 10628 modified_left, modified_top);
3afe33e7 10629#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
10630 UNBLOCK_INPUT;
10631}
10632
bc20ebbf
FP
10633/* Call this to change the size of frame F's x-window.
10634 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
10635 for this size change and subsequent size changes.
10636 Otherwise we leave the window gravity unchanged. */
dc6f92b8 10637
dfcf069d 10638void
bc20ebbf 10639x_set_window_size (f, change_gravity, cols, rows)
f676886a 10640 struct frame *f;
bc20ebbf 10641 int change_gravity;
b1c884c3 10642 int cols, rows;
dc6f92b8 10643{
06a2c219 10644#ifndef USE_X_TOOLKIT
dc6f92b8 10645 int pixelwidth, pixelheight;
06a2c219 10646#endif
dc6f92b8 10647
80fd1fe2 10648 BLOCK_INPUT;
aee9a898
RS
10649
10650#ifdef USE_X_TOOLKIT
3a20653d
RS
10651 {
10652 /* The x and y position of the widget is clobbered by the
10653 call to XtSetValues within EmacsFrameSetCharSize.
10654 This is a real kludge, but I don't understand Xt so I can't
10655 figure out a correct fix. Can anyone else tell me? -- rms. */
7556890b
RS
10656 int xpos = f->output_data.x->widget->core.x;
10657 int ypos = f->output_data.x->widget->core.y;
10658 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
10659 f->output_data.x->widget->core.x = xpos;
10660 f->output_data.x->widget->core.y = ypos;
3a20653d 10661 }
80fd1fe2
FP
10662
10663#else /* not USE_X_TOOLKIT */
10664
b1c884c3 10665 check_frame_size (f, &rows, &cols);
7556890b 10666 f->output_data.x->vertical_scroll_bar_extra
b2cad826
KH
10667 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
10668 ? 0
10669 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
480407eb 10670 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7556890b 10671 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
06a2c219
GM
10672 f->output_data.x->flags_areas_extra
10673 = 2 * FRAME_FLAGS_AREA_WIDTH (f);
f451eb13
JB
10674 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
10675 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
dc6f92b8 10676
7556890b 10677 f->output_data.x->win_gravity = NorthWestGravity;
c32cdd9a 10678 x_wm_set_size_hint (f, (long) 0, 0);
6ccf47d1 10679
334208b7
RS
10680 XSync (FRAME_X_DISPLAY (f), False);
10681 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10682 pixelwidth, pixelheight);
b1c884c3
JB
10683
10684 /* Now, strictly speaking, we can't be sure that this is accurate,
10685 but the window manager will get around to dealing with the size
10686 change request eventually, and we'll hear how it went when the
dbc4e1c1
JB
10687 ConfigureNotify event gets here.
10688
10689 We could just not bother storing any of this information here,
10690 and let the ConfigureNotify event set everything up, but that
10691 might be kind of confusing to the lisp code, since size changes
10692 wouldn't be reported in the frame parameters until some random
10693 point in the future when the ConfigureNotify event arrives. */
8922af5f 10694 change_frame_size (f, rows, cols, 0, 0);
b1c884c3
JB
10695 PIXEL_WIDTH (f) = pixelwidth;
10696 PIXEL_HEIGHT (f) = pixelheight;
10697
aee9a898
RS
10698 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
10699 receive in the ConfigureNotify event; if we get what we asked
10700 for, then the event won't cause the screen to become garbaged, so
10701 we have to make sure to do it here. */
10702 SET_FRAME_GARBAGED (f);
10703
10704 XFlush (FRAME_X_DISPLAY (f));
10705
10706#endif /* not USE_X_TOOLKIT */
10707
4d73d038 10708 /* If cursor was outside the new size, mark it as off. */
06a2c219 10709 mark_window_cursors_off (XWINDOW (f->root_window));
4d73d038 10710
aee9a898
RS
10711 /* Clear out any recollection of where the mouse highlighting was,
10712 since it might be in a place that's outside the new frame size.
10713 Actually checking whether it is outside is a pain in the neck,
10714 so don't try--just let the highlighting be done afresh with new size. */
e687d06e 10715 cancel_mouse_face (f);
dbc4e1c1 10716
dc6f92b8
JB
10717 UNBLOCK_INPUT;
10718}
dc6f92b8 10719\f
d047c4eb 10720/* Mouse warping. */
dc6f92b8 10721
9b378208 10722void
f676886a
JB
10723x_set_mouse_position (f, x, y)
10724 struct frame *f;
dc6f92b8
JB
10725 int x, y;
10726{
10727 int pix_x, pix_y;
10728
7556890b
RS
10729 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
10730 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
f451eb13
JB
10731
10732 if (pix_x < 0) pix_x = 0;
10733 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
10734
10735 if (pix_y < 0) pix_y = 0;
10736 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
dc6f92b8
JB
10737
10738 BLOCK_INPUT;
dc6f92b8 10739
334208b7
RS
10740 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
10741 0, 0, 0, 0, pix_x, pix_y);
dc6f92b8
JB
10742 UNBLOCK_INPUT;
10743}
10744
9b378208
RS
10745/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
10746
10747void
10748x_set_mouse_pixel_position (f, pix_x, pix_y)
10749 struct frame *f;
10750 int pix_x, pix_y;
10751{
10752 BLOCK_INPUT;
10753
334208b7
RS
10754 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
10755 0, 0, 0, 0, pix_x, pix_y);
9b378208
RS
10756 UNBLOCK_INPUT;
10757}
d047c4eb
KH
10758\f
10759/* focus shifting, raising and lowering. */
9b378208 10760
dfcf069d 10761void
f676886a
JB
10762x_focus_on_frame (f)
10763 struct frame *f;
dc6f92b8 10764{
1fb20991 10765#if 0 /* This proves to be unpleasant. */
f676886a 10766 x_raise_frame (f);
1fb20991 10767#endif
6d4238f3
JB
10768#if 0
10769 /* I don't think that the ICCCM allows programs to do things like this
10770 without the interaction of the window manager. Whatever you end up
f676886a 10771 doing with this code, do it to x_unfocus_frame too. */
334208b7 10772 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dc6f92b8 10773 RevertToPointerRoot, CurrentTime);
c118dd06 10774#endif /* ! 0 */
dc6f92b8
JB
10775}
10776
dfcf069d 10777void
f676886a
JB
10778x_unfocus_frame (f)
10779 struct frame *f;
dc6f92b8 10780{
6d4238f3 10781#if 0
f676886a 10782 /* Look at the remarks in x_focus_on_frame. */
0f941935 10783 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
334208b7 10784 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
dc6f92b8 10785 RevertToPointerRoot, CurrentTime);
c118dd06 10786#endif /* ! 0 */
dc6f92b8
JB
10787}
10788
f676886a 10789/* Raise frame F. */
dc6f92b8 10790
dfcf069d 10791void
f676886a
JB
10792x_raise_frame (f)
10793 struct frame *f;
dc6f92b8 10794{
3a88c238 10795 if (f->async_visible)
dc6f92b8
JB
10796 {
10797 BLOCK_INPUT;
3afe33e7 10798#ifdef USE_X_TOOLKIT
7556890b 10799 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 10800#else /* not USE_X_TOOLKIT */
334208b7 10801 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 10802#endif /* not USE_X_TOOLKIT */
334208b7 10803 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
10804 UNBLOCK_INPUT;
10805 }
10806}
10807
f676886a 10808/* Lower frame F. */
dc6f92b8 10809
dfcf069d 10810void
f676886a
JB
10811x_lower_frame (f)
10812 struct frame *f;
dc6f92b8 10813{
3a88c238 10814 if (f->async_visible)
dc6f92b8
JB
10815 {
10816 BLOCK_INPUT;
3afe33e7 10817#ifdef USE_X_TOOLKIT
7556890b 10818 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 10819#else /* not USE_X_TOOLKIT */
334208b7 10820 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 10821#endif /* not USE_X_TOOLKIT */
334208b7 10822 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
10823 UNBLOCK_INPUT;
10824 }
10825}
10826
dbc4e1c1 10827static void
6b0442dc 10828XTframe_raise_lower (f, raise_flag)
dbc4e1c1 10829 FRAME_PTR f;
6b0442dc 10830 int raise_flag;
dbc4e1c1 10831{
6b0442dc 10832 if (raise_flag)
dbc4e1c1
JB
10833 x_raise_frame (f);
10834 else
10835 x_lower_frame (f);
10836}
d047c4eb
KH
10837\f
10838/* Change of visibility. */
dc6f92b8 10839
9382638d
KH
10840/* This tries to wait until the frame is really visible.
10841 However, if the window manager asks the user where to position
10842 the frame, this will return before the user finishes doing that.
10843 The frame will not actually be visible at that time,
10844 but it will become visible later when the window manager
10845 finishes with it. */
10846
dfcf069d 10847void
f676886a
JB
10848x_make_frame_visible (f)
10849 struct frame *f;
dc6f92b8 10850{
990ba854 10851 Lisp_Object type;
1aa6072f 10852 int original_top, original_left;
dc6f92b8 10853
dc6f92b8 10854 BLOCK_INPUT;
dc6f92b8 10855
990ba854
RS
10856 type = x_icon_type (f);
10857 if (!NILP (type))
10858 x_bitmap_icon (f, type);
bdcd49ba 10859
f676886a 10860 if (! FRAME_VISIBLE_P (f))
90e65f07 10861 {
1aa6072f
RS
10862 /* We test FRAME_GARBAGED_P here to make sure we don't
10863 call x_set_offset a second time
10864 if we get to x_make_frame_visible a second time
10865 before the window gets really visible. */
10866 if (! FRAME_ICONIFIED_P (f)
10867 && ! f->output_data.x->asked_for_visible)
10868 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
10869
10870 f->output_data.x->asked_for_visible = 1;
10871
90e65f07 10872 if (! EQ (Vx_no_window_manager, Qt))
f676886a 10873 x_wm_set_window_state (f, NormalState);
3afe33e7 10874#ifdef USE_X_TOOLKIT
d7a38a2e 10875 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 10876 XtMapWidget (f->output_data.x->widget);
3afe33e7 10877#else /* not USE_X_TOOLKIT */
7f9c7f94 10878 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 10879#endif /* not USE_X_TOOLKIT */
0134a210
RS
10880#if 0 /* This seems to bring back scroll bars in the wrong places
10881 if the window configuration has changed. They seem
10882 to come back ok without this. */
ab648270 10883 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
334208b7 10884 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
0134a210 10885#endif
90e65f07 10886 }
dc6f92b8 10887
334208b7 10888 XFlush (FRAME_X_DISPLAY (f));
90e65f07 10889
0dacf791
RS
10890 /* Synchronize to ensure Emacs knows the frame is visible
10891 before we do anything else. We do this loop with input not blocked
10892 so that incoming events are handled. */
10893 {
10894 Lisp_Object frame;
c0a04927 10895 int count = input_signal_count;
28c01ffe
RS
10896 /* This must be before UNBLOCK_INPUT
10897 since events that arrive in response to the actions above
10898 will set it when they are handled. */
10899 int previously_visible = f->output_data.x->has_been_visible;
1aa6072f
RS
10900
10901 original_left = f->output_data.x->left_pos;
10902 original_top = f->output_data.x->top_pos;
c0a04927
RS
10903
10904 /* This must come after we set COUNT. */
10905 UNBLOCK_INPUT;
10906
2745e6c4 10907 /* We unblock here so that arriving X events are processed. */
1aa6072f 10908
dcb07ae9
RS
10909 /* Now move the window back to where it was "supposed to be".
10910 But don't do it if the gravity is negative.
10911 When the gravity is negative, this uses a position
28c01ffe
RS
10912 that is 3 pixels too low. Perhaps that's really the border width.
10913
10914 Don't do this if the window has never been visible before,
10915 because the window manager may choose the position
10916 and we don't want to override it. */
1aa6072f 10917
4d3f5d9a 10918 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
06c488fd 10919 && f->output_data.x->win_gravity == NorthWestGravity
28c01ffe 10920 && previously_visible)
1aa6072f 10921 {
2745e6c4
RS
10922 Drawable rootw;
10923 int x, y;
10924 unsigned int width, height, border, depth;
06a2c219 10925
1aa6072f 10926 BLOCK_INPUT;
9829ddba 10927
06a2c219
GM
10928 /* On some window managers (such as FVWM) moving an existing
10929 window, even to the same place, causes the window manager
10930 to introduce an offset. This can cause the window to move
10931 to an unexpected location. Check the geometry (a little
10932 slow here) and then verify that the window is in the right
10933 place. If the window is not in the right place, move it
10934 there, and take the potential window manager hit. */
2745e6c4
RS
10935 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10936 &rootw, &x, &y, &width, &height, &border, &depth);
10937
10938 if (original_left != x || original_top != y)
10939 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10940 original_left, original_top);
10941
1aa6072f
RS
10942 UNBLOCK_INPUT;
10943 }
9829ddba 10944
e0c1aef2 10945 XSETFRAME (frame, f);
c0a04927
RS
10946
10947 while (1)
2a6cf806 10948 {
334208b7 10949 x_sync (f);
c0a04927
RS
10950 /* Once we have handled input events,
10951 we should have received the MapNotify if one is coming.
10952 So if we have not got it yet, stop looping.
10953 Some window managers make their own decisions
10954 about visibility. */
10955 if (input_signal_count != count)
10956 break;
c12a7cbd 10957 /* Machines that do polling rather than SIGIO have been observed
23cf7c60
KH
10958 to go into a busy-wait here. So we'll fake an alarm signal
10959 to let the handler know that there's something to be read.
10960 We used to raise a real alarm, but it seems that the handler
10961 isn't always enabled here. This is probably a bug. */
8b2f8d4e 10962 if (input_polling_used ())
3b2fa4e6
RS
10963 {
10964 /* It could be confusing if a real alarm arrives while processing
10965 the fake one. Turn it off and let the handler reset it. */
10966 alarm (0);
5e8e68ce 10967 input_poll_signal (0);
3b2fa4e6 10968 }
c0a04927
RS
10969 /* Once we have handled input events,
10970 we should have received the MapNotify if one is coming.
10971 So if we have not got it yet, stop looping.
10972 Some window managers make their own decisions
10973 about visibility. */
10974 if (input_signal_count != count)
10975 break;
2a6cf806 10976 }
0dacf791
RS
10977 FRAME_SAMPLE_VISIBILITY (f);
10978 }
dc6f92b8
JB
10979}
10980
06a2c219 10981/* Change from mapped state to withdrawn state. */
dc6f92b8 10982
d047c4eb
KH
10983/* Make the frame visible (mapped and not iconified). */
10984
dfcf069d 10985void
f676886a
JB
10986x_make_frame_invisible (f)
10987 struct frame *f;
dc6f92b8 10988{
546e6d5b
RS
10989 Window window;
10990
10991#ifdef USE_X_TOOLKIT
10992 /* Use the frame's outermost window, not the one we normally draw on. */
7556890b 10993 window = XtWindow (f->output_data.x->widget);
546e6d5b
RS
10994#else /* not USE_X_TOOLKIT */
10995 window = FRAME_X_WINDOW (f);
10996#endif /* not USE_X_TOOLKIT */
dc6f92b8 10997
9319ae23 10998 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
10999 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11000 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 11001
5627c40e 11002#if 0/* This might add unreliability; I don't trust it -- rms. */
9319ae23 11003 if (! f->async_visible && ! f->async_iconified)
dc6f92b8 11004 return;
5627c40e 11005#endif
dc6f92b8
JB
11006
11007 BLOCK_INPUT;
c118dd06 11008
af31d76f
RS
11009 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
11010 that the current position of the window is user-specified, rather than
11011 program-specified, so that when the window is mapped again, it will be
11012 placed at the same location, without forcing the user to position it
11013 by hand again (they have already done that once for this window.) */
c32cdd9a 11014 x_wm_set_size_hint (f, (long) 0, 1);
af31d76f 11015
c118dd06
JB
11016#ifdef HAVE_X11R4
11017
334208b7
RS
11018 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
11019 DefaultScreen (FRAME_X_DISPLAY (f))))
c118dd06
JB
11020 {
11021 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11022 error ("Can't notify window manager of window withdrawal");
c118dd06 11023 }
c118dd06 11024#else /* ! defined (HAVE_X11R4) */
16bd92ea 11025
c118dd06 11026 /* Tell the window manager what we're going to do. */
dc6f92b8
JB
11027 if (! EQ (Vx_no_window_manager, Qt))
11028 {
16bd92ea 11029 XEvent unmap;
dc6f92b8 11030
16bd92ea 11031 unmap.xunmap.type = UnmapNotify;
546e6d5b 11032 unmap.xunmap.window = window;
334208b7 11033 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
16bd92ea 11034 unmap.xunmap.from_configure = False;
334208b7
RS
11035 if (! XSendEvent (FRAME_X_DISPLAY (f),
11036 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea 11037 False,
06a2c219 11038 SubstructureRedirectMaskSubstructureNotifyMask,
16bd92ea
JB
11039 &unmap))
11040 {
11041 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11042 error ("Can't notify window manager of withdrawal");
16bd92ea 11043 }
dc6f92b8
JB
11044 }
11045
16bd92ea 11046 /* Unmap the window ourselves. Cheeky! */
334208b7 11047 XUnmapWindow (FRAME_X_DISPLAY (f), window);
c118dd06 11048#endif /* ! defined (HAVE_X11R4) */
dc6f92b8 11049
5627c40e
RS
11050 /* We can't distinguish this from iconification
11051 just by the event that we get from the server.
11052 So we can't win using the usual strategy of letting
11053 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
11054 and synchronize with the server to make sure we agree. */
11055 f->visible = 0;
11056 FRAME_ICONIFIED_P (f) = 0;
11057 f->async_visible = 0;
11058 f->async_iconified = 0;
11059
334208b7 11060 x_sync (f);
5627c40e 11061
dc6f92b8
JB
11062 UNBLOCK_INPUT;
11063}
11064
06a2c219 11065/* Change window state from mapped to iconified. */
dc6f92b8 11066
dfcf069d 11067void
f676886a
JB
11068x_iconify_frame (f)
11069 struct frame *f;
dc6f92b8 11070{
3afe33e7 11071 int result;
990ba854 11072 Lisp_Object type;
dc6f92b8 11073
9319ae23 11074 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
11075 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11076 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 11077
3a88c238 11078 if (f->async_iconified)
dc6f92b8
JB
11079 return;
11080
3afe33e7 11081 BLOCK_INPUT;
546e6d5b 11082
9af3143a
RS
11083 FRAME_SAMPLE_VISIBILITY (f);
11084
990ba854
RS
11085 type = x_icon_type (f);
11086 if (!NILP (type))
11087 x_bitmap_icon (f, type);
bdcd49ba
RS
11088
11089#ifdef USE_X_TOOLKIT
11090
546e6d5b
RS
11091 if (! FRAME_VISIBLE_P (f))
11092 {
11093 if (! EQ (Vx_no_window_manager, Qt))
11094 x_wm_set_window_state (f, IconicState);
11095 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 11096 XtMapWidget (f->output_data.x->widget);
9cf30a30
RS
11097 /* The server won't give us any event to indicate
11098 that an invisible frame was changed to an icon,
11099 so we have to record it here. */
11100 f->iconified = 1;
1e6bc770 11101 f->visible = 1;
9cf30a30 11102 f->async_iconified = 1;
1e6bc770 11103 f->async_visible = 0;
546e6d5b
RS
11104 UNBLOCK_INPUT;
11105 return;
11106 }
11107
334208b7 11108 result = XIconifyWindow (FRAME_X_DISPLAY (f),
7556890b 11109 XtWindow (f->output_data.x->widget),
334208b7 11110 DefaultScreen (FRAME_X_DISPLAY (f)));
3afe33e7
RS
11111 UNBLOCK_INPUT;
11112
11113 if (!result)
546e6d5b 11114 error ("Can't notify window manager of iconification");
3afe33e7
RS
11115
11116 f->async_iconified = 1;
1e6bc770
RS
11117 f->async_visible = 0;
11118
8c002a25
KH
11119
11120 BLOCK_INPUT;
334208b7 11121 XFlush (FRAME_X_DISPLAY (f));
8c002a25 11122 UNBLOCK_INPUT;
3afe33e7
RS
11123#else /* not USE_X_TOOLKIT */
11124
fd13dbb2
RS
11125 /* Make sure the X server knows where the window should be positioned,
11126 in case the user deiconifies with the window manager. */
11127 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
7556890b 11128 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
fd13dbb2 11129
16bd92ea
JB
11130 /* Since we don't know which revision of X we're running, we'll use both
11131 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
11132
11133 /* X11R4: send a ClientMessage to the window manager using the
11134 WM_CHANGE_STATE type. */
11135 {
11136 XEvent message;
58769bee 11137
c118dd06 11138 message.xclient.window = FRAME_X_WINDOW (f);
16bd92ea 11139 message.xclient.type = ClientMessage;
334208b7 11140 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
16bd92ea
JB
11141 message.xclient.format = 32;
11142 message.xclient.data.l[0] = IconicState;
11143
334208b7
RS
11144 if (! XSendEvent (FRAME_X_DISPLAY (f),
11145 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea
JB
11146 False,
11147 SubstructureRedirectMask | SubstructureNotifyMask,
11148 &message))
dc6f92b8
JB
11149 {
11150 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11151 error ("Can't notify window manager of iconification");
dc6f92b8 11152 }
16bd92ea 11153 }
dc6f92b8 11154
58769bee 11155 /* X11R3: set the initial_state field of the window manager hints to
16bd92ea
JB
11156 IconicState. */
11157 x_wm_set_window_state (f, IconicState);
dc6f92b8 11158
a9c00105
RS
11159 if (!FRAME_VISIBLE_P (f))
11160 {
11161 /* If the frame was withdrawn, before, we must map it. */
7f9c7f94 11162 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
a9c00105
RS
11163 }
11164
3a88c238 11165 f->async_iconified = 1;
1e6bc770 11166 f->async_visible = 0;
dc6f92b8 11167
334208b7 11168 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 11169 UNBLOCK_INPUT;
8c002a25 11170#endif /* not USE_X_TOOLKIT */
dc6f92b8 11171}
d047c4eb 11172\f
c0ff3fab 11173/* Destroy the X window of frame F. */
dc6f92b8 11174
dfcf069d 11175void
c0ff3fab 11176x_destroy_window (f)
f676886a 11177 struct frame *f;
dc6f92b8 11178{
7f9c7f94
RS
11179 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11180
dc6f92b8 11181 BLOCK_INPUT;
c0ff3fab 11182
6186a4a0
RS
11183 /* If a display connection is dead, don't try sending more
11184 commands to the X server. */
11185 if (dpyinfo->display != 0)
11186 {
11187 if (f->output_data.x->icon_desc != 0)
11188 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
31f41daf
RS
11189#ifdef HAVE_X_I18N
11190 if (FRAME_XIM (f))
11191 {
11192 XDestroyIC (FRAME_XIC (f));
408be661
RS
11193#if ! defined (SOLARIS2) || defined (HAVE_X11R6)
11194 /* This line causes crashes on Solaris with Openwin,
11195 due to an apparent bug in XCloseIM.
11196 X11R6 seems not to have the bug. */
31f41daf 11197 XCloseIM (FRAME_XIM (f));
a9978dd8 11198#endif
31f41daf
RS
11199 }
11200#endif
6186a4a0 11201 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->window_desc);
3afe33e7 11202#ifdef USE_X_TOOLKIT
06a2c219
GM
11203 if (f->output_data.x->widget)
11204 XtDestroyWidget (f->output_data.x->widget);
6186a4a0 11205 free_frame_menubar (f);
3afe33e7
RS
11206#endif /* USE_X_TOOLKIT */
11207
6186a4a0
RS
11208 free_frame_faces (f);
11209 XFlush (FRAME_X_DISPLAY (f));
11210 }
dc6f92b8 11211
df89d8a4 11212 if (f->output_data.x->saved_menu_event)
06a2c219 11213 xfree (f->output_data.x->saved_menu_event);
0c49ce2f 11214
7556890b
RS
11215 xfree (f->output_data.x);
11216 f->output_data.x = 0;
0f941935
KH
11217 if (f == dpyinfo->x_focus_frame)
11218 dpyinfo->x_focus_frame = 0;
11219 if (f == dpyinfo->x_focus_event_frame)
11220 dpyinfo->x_focus_event_frame = 0;
11221 if (f == dpyinfo->x_highlight_frame)
11222 dpyinfo->x_highlight_frame = 0;
c0ff3fab 11223
7f9c7f94
RS
11224 dpyinfo->reference_count--;
11225
11226 if (f == dpyinfo->mouse_face_mouse_frame)
dc05a16b 11227 {
7f9c7f94
RS
11228 dpyinfo->mouse_face_beg_row
11229 = dpyinfo->mouse_face_beg_col = -1;
11230 dpyinfo->mouse_face_end_row
11231 = dpyinfo->mouse_face_end_col = -1;
11232 dpyinfo->mouse_face_window = Qnil;
21323706
RS
11233 dpyinfo->mouse_face_deferred_gc = 0;
11234 dpyinfo->mouse_face_mouse_frame = 0;
dc05a16b 11235 }
0134a210 11236
c0ff3fab 11237 UNBLOCK_INPUT;
dc6f92b8
JB
11238}
11239\f
f451eb13
JB
11240/* Setting window manager hints. */
11241
af31d76f
RS
11242/* Set the normal size hints for the window manager, for frame F.
11243 FLAGS is the flags word to use--or 0 meaning preserve the flags
11244 that the window now has.
11245 If USER_POSITION is nonzero, we set the USPosition
11246 flag (this is useful when FLAGS is 0). */
6dba1858 11247
dfcf069d 11248void
af31d76f 11249x_wm_set_size_hint (f, flags, user_position)
f676886a 11250 struct frame *f;
af31d76f
RS
11251 long flags;
11252 int user_position;
dc6f92b8
JB
11253{
11254 XSizeHints size_hints;
3afe33e7
RS
11255
11256#ifdef USE_X_TOOLKIT
7e4f2521
FP
11257 Arg al[2];
11258 int ac = 0;
11259 Dimension widget_width, widget_height;
7556890b 11260 Window window = XtWindow (f->output_data.x->widget);
3afe33e7 11261#else /* not USE_X_TOOLKIT */
c118dd06 11262 Window window = FRAME_X_WINDOW (f);
3afe33e7 11263#endif /* not USE_X_TOOLKIT */
dc6f92b8 11264
b72a58fd
RS
11265 /* Setting PMaxSize caused various problems. */
11266 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
dc6f92b8 11267
7556890b
RS
11268 size_hints.x = f->output_data.x->left_pos;
11269 size_hints.y = f->output_data.x->top_pos;
7553a6b7 11270
7e4f2521
FP
11271#ifdef USE_X_TOOLKIT
11272 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
11273 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
7556890b 11274 XtGetValues (f->output_data.x->widget, al, ac);
7e4f2521
FP
11275 size_hints.height = widget_height;
11276 size_hints.width = widget_width;
11277#else /* not USE_X_TOOLKIT */
f676886a
JB
11278 size_hints.height = PIXEL_HEIGHT (f);
11279 size_hints.width = PIXEL_WIDTH (f);
7e4f2521 11280#endif /* not USE_X_TOOLKIT */
7553a6b7 11281
7556890b
RS
11282 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
11283 size_hints.height_inc = f->output_data.x->line_height;
334208b7
RS
11284 size_hints.max_width
11285 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
11286 size_hints.max_height
11287 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
0134a210 11288
d067ea8b
KH
11289 /* Calculate the base and minimum sizes.
11290
11291 (When we use the X toolkit, we don't do it here.
11292 Instead we copy the values that the widgets are using, below.) */
11293#ifndef USE_X_TOOLKIT
b1c884c3 11294 {
b0342f17 11295 int base_width, base_height;
0134a210 11296 int min_rows = 0, min_cols = 0;
b0342f17 11297
f451eb13
JB
11298 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
11299 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
b0342f17 11300
0134a210 11301 check_frame_size (f, &min_rows, &min_cols);
b0342f17 11302
0134a210
RS
11303 /* The window manager uses the base width hints to calculate the
11304 current number of rows and columns in the frame while
11305 resizing; min_width and min_height aren't useful for this
11306 purpose, since they might not give the dimensions for a
11307 zero-row, zero-column frame.
58769bee 11308
0134a210
RS
11309 We use the base_width and base_height members if we have
11310 them; otherwise, we set the min_width and min_height members
11311 to the size for a zero x zero frame. */
b0342f17
JB
11312
11313#ifdef HAVE_X11R4
0134a210
RS
11314 size_hints.flags |= PBaseSize;
11315 size_hints.base_width = base_width;
11316 size_hints.base_height = base_height;
11317 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
11318 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
b0342f17 11319#else
0134a210
RS
11320 size_hints.min_width = base_width;
11321 size_hints.min_height = base_height;
b0342f17 11322#endif
b1c884c3 11323 }
dc6f92b8 11324
d067ea8b 11325 /* If we don't need the old flags, we don't need the old hint at all. */
af31d76f 11326 if (flags)
dc6f92b8 11327 {
d067ea8b
KH
11328 size_hints.flags |= flags;
11329 goto no_read;
11330 }
11331#endif /* not USE_X_TOOLKIT */
11332
11333 {
11334 XSizeHints hints; /* Sometimes I hate X Windows... */
11335 long supplied_return;
11336 int value;
af31d76f
RS
11337
11338#ifdef HAVE_X11R4
d067ea8b
KH
11339 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
11340 &supplied_return);
af31d76f 11341#else
d067ea8b 11342 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
af31d76f 11343#endif
58769bee 11344
d067ea8b
KH
11345#ifdef USE_X_TOOLKIT
11346 size_hints.base_height = hints.base_height;
11347 size_hints.base_width = hints.base_width;
11348 size_hints.min_height = hints.min_height;
11349 size_hints.min_width = hints.min_width;
11350#endif
11351
11352 if (flags)
11353 size_hints.flags |= flags;
11354 else
11355 {
11356 if (value == 0)
11357 hints.flags = 0;
11358 if (hints.flags & PSize)
11359 size_hints.flags |= PSize;
11360 if (hints.flags & PPosition)
11361 size_hints.flags |= PPosition;
11362 if (hints.flags & USPosition)
11363 size_hints.flags |= USPosition;
11364 if (hints.flags & USSize)
11365 size_hints.flags |= USSize;
11366 }
11367 }
11368
06a2c219 11369#ifndef USE_X_TOOLKIT
d067ea8b 11370 no_read:
06a2c219 11371#endif
0134a210 11372
af31d76f 11373#ifdef PWinGravity
7556890b 11374 size_hints.win_gravity = f->output_data.x->win_gravity;
af31d76f 11375 size_hints.flags |= PWinGravity;
dc05a16b 11376
af31d76f 11377 if (user_position)
6dba1858 11378 {
af31d76f
RS
11379 size_hints.flags &= ~ PPosition;
11380 size_hints.flags |= USPosition;
6dba1858 11381 }
2554751d 11382#endif /* PWinGravity */
6dba1858 11383
b0342f17 11384#ifdef HAVE_X11R4
334208b7 11385 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 11386#else
334208b7 11387 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 11388#endif
dc6f92b8
JB
11389}
11390
11391/* Used for IconicState or NormalState */
06a2c219 11392
dfcf069d 11393void
f676886a
JB
11394x_wm_set_window_state (f, state)
11395 struct frame *f;
dc6f92b8
JB
11396 int state;
11397{
3afe33e7 11398#ifdef USE_X_TOOLKIT
546e6d5b
RS
11399 Arg al[1];
11400
11401 XtSetArg (al[0], XtNinitialState, state);
7556890b 11402 XtSetValues (f->output_data.x->widget, al, 1);
3afe33e7 11403#else /* not USE_X_TOOLKIT */
c118dd06 11404 Window window = FRAME_X_WINDOW (f);
dc6f92b8 11405
7556890b
RS
11406 f->output_data.x->wm_hints.flags |= StateHint;
11407 f->output_data.x->wm_hints.initial_state = state;
b1c884c3 11408
7556890b 11409 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
546e6d5b 11410#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11411}
11412
dfcf069d 11413void
7f2ae036 11414x_wm_set_icon_pixmap (f, pixmap_id)
f676886a 11415 struct frame *f;
7f2ae036 11416 int pixmap_id;
dc6f92b8 11417{
d2bd6bc4
RS
11418 Pixmap icon_pixmap;
11419
06a2c219 11420#ifndef USE_X_TOOLKIT
c118dd06 11421 Window window = FRAME_X_WINDOW (f);
75231bad 11422#endif
dc6f92b8 11423
7f2ae036 11424 if (pixmap_id > 0)
dbc4e1c1 11425 {
d2bd6bc4 11426 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
7556890b 11427 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
dbc4e1c1
JB
11428 }
11429 else
68568555
RS
11430 {
11431 /* It seems there is no way to turn off use of an icon pixmap.
11432 The following line does it, only if no icon has yet been created,
11433 for some window managers. But with mwm it crashes.
11434 Some people say it should clear the IconPixmapHint bit in this case,
11435 but that doesn't work, and the X consortium said it isn't the
11436 right thing at all. Since there is no way to win,
11437 best to explicitly give up. */
11438#if 0
11439 f->output_data.x->wm_hints.icon_pixmap = None;
11440#else
11441 return;
11442#endif
11443 }
b1c884c3 11444
d2bd6bc4
RS
11445#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
11446
11447 {
11448 Arg al[1];
11449 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
11450 XtSetValues (f->output_data.x->widget, al, 1);
11451 }
11452
11453#else /* not USE_X_TOOLKIT */
11454
7556890b
RS
11455 f->output_data.x->wm_hints.flags |= IconPixmapHint;
11456 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
d2bd6bc4
RS
11457
11458#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11459}
11460
dfcf069d 11461void
f676886a
JB
11462x_wm_set_icon_position (f, icon_x, icon_y)
11463 struct frame *f;
dc6f92b8
JB
11464 int icon_x, icon_y;
11465{
75231bad 11466#ifdef USE_X_TOOLKIT
7556890b 11467 Window window = XtWindow (f->output_data.x->widget);
75231bad 11468#else
c118dd06 11469 Window window = FRAME_X_WINDOW (f);
75231bad 11470#endif
dc6f92b8 11471
7556890b
RS
11472 f->output_data.x->wm_hints.flags |= IconPositionHint;
11473 f->output_data.x->wm_hints.icon_x = icon_x;
11474 f->output_data.x->wm_hints.icon_y = icon_y;
b1c884c3 11475
7556890b 11476 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
dc6f92b8
JB
11477}
11478
11479\f
06a2c219
GM
11480/***********************************************************************
11481 Fonts
11482 ***********************************************************************/
dc43ef94
KH
11483
11484/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
06a2c219 11485
dc43ef94
KH
11486struct font_info *
11487x_get_font_info (f, font_idx)
11488 FRAME_PTR f;
11489 int font_idx;
11490{
11491 return (FRAME_X_FONT_TABLE (f) + font_idx);
11492}
11493
11494
11495/* Return a list of names of available fonts matching PATTERN on frame
11496 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
11497 to be listed. Frame F NULL means we have not yet created any
11498 frame on X, and consult the first display in x_display_list.
11499 MAXNAMES sets a limit on how many fonts to match. */
11500
11501Lisp_Object
11502x_list_fonts (f, pattern, size, maxnames)
11503 FRAME_PTR f;
11504 Lisp_Object pattern;
11505 int size;
11506 int maxnames;
11507{
06a2c219
GM
11508 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
11509 Lisp_Object tem, second_best;
dc43ef94 11510 Display *dpy = f != NULL ? FRAME_X_DISPLAY (f) : x_display_list->display;
09c6077f 11511 int try_XLoadQueryFont = 0;
53ca4657 11512 int count;
dc43ef94 11513
6b0efe73 11514 patterns = Fassoc (pattern, Valternate_fontname_alist);
2da424f1
KH
11515 if (NILP (patterns))
11516 patterns = Fcons (pattern, Qnil);
81ba44e5 11517
09c6077f
KH
11518 if (maxnames == 1 && !size)
11519 /* We can return any single font matching PATTERN. */
11520 try_XLoadQueryFont = 1;
9a32686f 11521
2da424f1 11522 for (; CONSP (patterns); patterns = XCONS (patterns)->cdr)
dc43ef94 11523 {
dc43ef94
KH
11524 int num_fonts;
11525 char **names;
11526
2da424f1 11527 pattern = XCONS (patterns)->car;
536f4067
RS
11528 /* See if we cached the result for this particular query.
11529 The cache is an alist of the form:
11530 (((PATTERN . MAXNAMES) (FONTNAME . WIDTH) ...) ...)
11531 */
b5210ea7
KH
11532 if (f && (tem = XCONS (FRAME_X_DISPLAY_INFO (f)->name_list_element)->cdr,
11533 key = Fcons (pattern, make_number (maxnames)),
11534 !NILP (list = Fassoc (key, tem))))
11535 {
11536 list = Fcdr_safe (list);
11537 /* We have a cashed list. Don't have to get the list again. */
11538 goto label_cached;
11539 }
11540
11541 /* At first, put PATTERN in the cache. */
09c6077f 11542
dc43ef94 11543 BLOCK_INPUT;
17d85edc
KH
11544 count = x_catch_errors (dpy);
11545
09c6077f
KH
11546 if (try_XLoadQueryFont)
11547 {
11548 XFontStruct *font;
11549 unsigned long value;
11550
11551 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
17d85edc
KH
11552 if (x_had_errors_p (dpy))
11553 {
11554 /* This error is perhaps due to insufficient memory on X
11555 server. Let's just ignore it. */
11556 font = NULL;
11557 x_clear_errors (dpy);
11558 }
11559
09c6077f
KH
11560 if (font
11561 && XGetFontProperty (font, XA_FONT, &value))
11562 {
11563 char *name = (char *) XGetAtomName (dpy, (Atom) value);
11564 int len = strlen (name);
01c752b5 11565 char *tmp;
09c6077f 11566
6f6512e8
KH
11567 /* If DXPC (a Differential X Protocol Compressor)
11568 Ver.3.7 is running, XGetAtomName will return null
11569 string. We must avoid such a name. */
11570 if (len == 0)
11571 try_XLoadQueryFont = 0;
11572 else
11573 {
11574 num_fonts = 1;
11575 names = (char **) alloca (sizeof (char *));
11576 /* Some systems only allow alloca assigned to a
11577 simple var. */
11578 tmp = (char *) alloca (len + 1); names[0] = tmp;
11579 bcopy (name, names[0], len + 1);
11580 XFree (name);
11581 }
09c6077f
KH
11582 }
11583 else
11584 try_XLoadQueryFont = 0;
a083fd23
RS
11585
11586 if (font)
11587 XFreeFont (dpy, font);
09c6077f
KH
11588 }
11589
11590 if (!try_XLoadQueryFont)
17d85edc
KH
11591 {
11592 /* We try at least 10 fonts because XListFonts will return
11593 auto-scaled fonts at the head. */
11594 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
11595 &num_fonts);
11596 if (x_had_errors_p (dpy))
11597 {
11598 /* This error is perhaps due to insufficient memory on X
11599 server. Let's just ignore it. */
11600 names = NULL;
11601 x_clear_errors (dpy);
11602 }
11603 }
11604
11605 x_uncatch_errors (dpy, count);
dc43ef94
KH
11606 UNBLOCK_INPUT;
11607
11608 if (names)
11609 {
11610 int i;
dc43ef94
KH
11611
11612 /* Make a list of all the fonts we got back.
11613 Store that in the font cache for the display. */
11614 for (i = 0; i < num_fonts; i++)
11615 {
06a2c219 11616 int width = 0;
dc43ef94 11617 char *p = names[i];
06a2c219
GM
11618 int average_width = -1, dashes = 0;
11619
dc43ef94 11620 /* Count the number of dashes in NAMES[I]. If there are
b5210ea7
KH
11621 14 dashes, and the field value following 12th dash
11622 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
11623 is usually too ugly to be used for editing. Let's
11624 ignore it. */
dc43ef94
KH
11625 while (*p)
11626 if (*p++ == '-')
11627 {
11628 dashes++;
11629 if (dashes == 7) /* PIXEL_SIZE field */
11630 width = atoi (p);
11631 else if (dashes == 12) /* AVERAGE_WIDTH field */
11632 average_width = atoi (p);
11633 }
11634 if (dashes < 14 || average_width != 0)
11635 {
11636 tem = build_string (names[i]);
11637 if (NILP (Fassoc (tem, list)))
11638 {
11639 if (STRINGP (Vx_pixel_size_width_font_regexp)
f39db7ea
RS
11640 && ((fast_c_string_match_ignore_case
11641 (Vx_pixel_size_width_font_regexp, names[i]))
dc43ef94
KH
11642 >= 0))
11643 /* We can set the value of PIXEL_SIZE to the
b5210ea7 11644 width of this font. */
dc43ef94
KH
11645 list = Fcons (Fcons (tem, make_number (width)), list);
11646 else
11647 /* For the moment, width is not known. */
11648 list = Fcons (Fcons (tem, Qnil), list);
11649 }
11650 }
11651 }
09c6077f
KH
11652 if (!try_XLoadQueryFont)
11653 XFreeFontNames (names);
dc43ef94
KH
11654 }
11655
b5210ea7 11656 /* Now store the result in the cache. */
dc43ef94
KH
11657 if (f != NULL)
11658 XCONS (FRAME_X_DISPLAY_INFO (f)->name_list_element)->cdr
11659 = Fcons (Fcons (key, list),
11660 XCONS (FRAME_X_DISPLAY_INFO (f)->name_list_element)->cdr);
dc43ef94 11661
b5210ea7
KH
11662 label_cached:
11663 if (NILP (list)) continue; /* Try the remaining alternatives. */
dc43ef94 11664
b5210ea7
KH
11665 newlist = second_best = Qnil;
11666 /* Make a list of the fonts that have the right width. */
11667 for (; CONSP (list); list = XCONS (list)->cdr)
11668 {
536f4067
RS
11669 int found_size;
11670
b5210ea7 11671 tem = XCONS (list)->car;
dc43ef94 11672
b5210ea7
KH
11673 if (!CONSP (tem) || NILP (XCONS (tem)->car))
11674 continue;
11675 if (!size)
11676 {
11677 newlist = Fcons (XCONS (tem)->car, newlist);
11678 continue;
11679 }
dc43ef94 11680
dc43ef94
KH
11681 if (!INTEGERP (XCONS (tem)->cdr))
11682 {
b5210ea7
KH
11683 /* Since we have not yet known the size of this font, we
11684 must try slow function call XLoadQueryFont. */
dc43ef94
KH
11685 XFontStruct *thisinfo;
11686
11687 BLOCK_INPUT;
17d85edc 11688 count = x_catch_errors (dpy);
dc43ef94
KH
11689 thisinfo = XLoadQueryFont (dpy,
11690 XSTRING (XCONS (tem)->car)->data);
17d85edc
KH
11691 if (x_had_errors_p (dpy))
11692 {
11693 /* This error is perhaps due to insufficient memory on X
11694 server. Let's just ignore it. */
11695 thisinfo = NULL;
11696 x_clear_errors (dpy);
11697 }
11698 x_uncatch_errors (dpy, count);
dc43ef94
KH
11699 UNBLOCK_INPUT;
11700
11701 if (thisinfo)
11702 {
536f4067
RS
11703 XCONS (tem)->cdr
11704 = (thisinfo->min_bounds.width == 0
11705 ? make_number (0)
11706 : make_number (thisinfo->max_bounds.width));
dc43ef94
KH
11707 XFreeFont (dpy, thisinfo);
11708 }
11709 else
b5210ea7 11710 /* For unknown reason, the previous call of XListFont had
06a2c219 11711 returned a font which can't be opened. Record the size
b5210ea7 11712 as 0 not to try to open it again. */
dc43ef94
KH
11713 XCONS (tem)->cdr = make_number (0);
11714 }
536f4067
RS
11715
11716 found_size = XINT (XCONS (tem)->cdr);
11717 if (found_size == size)
b5210ea7 11718 newlist = Fcons (XCONS (tem)->car, newlist);
536f4067 11719 else if (found_size > 0)
b5210ea7 11720 {
536f4067 11721 if (NILP (second_best))
b5210ea7 11722 second_best = tem;
536f4067
RS
11723 else if (found_size < size)
11724 {
11725 if (XINT (XCONS (second_best)->cdr) > size
11726 || XINT (XCONS (second_best)->cdr) < found_size)
11727 second_best = tem;
11728 }
11729 else
11730 {
11731 if (XINT (XCONS (second_best)->cdr) > size
11732 && XINT (XCONS (second_best)->cdr) > found_size)
11733 second_best = tem;
11734 }
b5210ea7
KH
11735 }
11736 }
11737 if (!NILP (newlist))
11738 break;
11739 else if (!NILP (second_best))
11740 {
11741 newlist = Fcons (XCONS (second_best)->car, Qnil);
11742 break;
dc43ef94 11743 }
dc43ef94
KH
11744 }
11745
11746 return newlist;
11747}
11748
06a2c219
GM
11749
11750#if GLYPH_DEBUG
11751
11752/* Check that FONT is valid on frame F. It is if it can be found in F's
11753 font table. */
11754
11755static void
11756x_check_font (f, font)
11757 struct frame *f;
11758 XFontStruct *font;
11759{
11760 int i;
11761 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11762
11763 xassert (font != NULL);
11764
11765 for (i = 0; i < dpyinfo->n_fonts; i++)
11766 if (dpyinfo->font_table[i].name
11767 && font == dpyinfo->font_table[i].font)
11768 break;
11769
11770 xassert (i < dpyinfo->n_fonts);
11771}
11772
11773#endif /* GLYPH_DEBUG != 0 */
11774
11775/* Set *W to the minimum width, *H to the minimum font height of FONT.
11776 Note: There are (broken) X fonts out there with invalid XFontStruct
11777 min_bounds contents. For example, handa@etl.go.jp reports that
11778 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
11779 have font->min_bounds.width == 0. */
11780
11781static INLINE void
11782x_font_min_bounds (font, w, h)
11783 XFontStruct *font;
11784 int *w, *h;
11785{
11786 *h = FONT_HEIGHT (font);
11787 *w = font->min_bounds.width;
11788
11789 /* Try to handle the case where FONT->min_bounds has invalid
11790 contents. Since the only font known to have invalid min_bounds
11791 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
11792 if (*w <= 0)
11793 *w = font->max_bounds.width;
11794}
11795
11796
11797/* Compute the smallest character width and smallest font height over
11798 all fonts available on frame F. Set the members smallest_char_width
11799 and smallest_font_height in F's x_display_info structure to
11800 the values computed. Value is non-zero if smallest_font_height or
11801 smallest_char_width become smaller than they were before. */
11802
11803static int
11804x_compute_min_glyph_bounds (f)
11805 struct frame *f;
11806{
11807 int i;
11808 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11809 XFontStruct *font;
11810 int old_width = dpyinfo->smallest_char_width;
11811 int old_height = dpyinfo->smallest_font_height;
11812
11813 dpyinfo->smallest_font_height = 100000;
11814 dpyinfo->smallest_char_width = 100000;
11815
11816 for (i = 0; i < dpyinfo->n_fonts; ++i)
11817 if (dpyinfo->font_table[i].name)
11818 {
11819 struct font_info *fontp = dpyinfo->font_table + i;
11820 int w, h;
11821
11822 font = (XFontStruct *) fontp->font;
11823 xassert (font != (XFontStruct *) ~0);
11824 x_font_min_bounds (font, &w, &h);
11825
11826 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
11827 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
11828 }
11829
11830 xassert (dpyinfo->smallest_char_width > 0
11831 && dpyinfo->smallest_font_height > 0);
11832
11833 return (dpyinfo->n_fonts == 1
11834 || dpyinfo->smallest_char_width < old_width
11835 || dpyinfo->smallest_font_height < old_height);
11836}
11837
11838
dc43ef94
KH
11839/* Load font named FONTNAME of the size SIZE for frame F, and return a
11840 pointer to the structure font_info while allocating it dynamically.
11841 If SIZE is 0, load any size of font.
11842 If loading is failed, return NULL. */
11843
11844struct font_info *
11845x_load_font (f, fontname, size)
11846 struct frame *f;
11847 register char *fontname;
11848 int size;
11849{
11850 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11851 Lisp_Object font_names;
d645aaa4 11852 int count;
dc43ef94
KH
11853
11854 /* Get a list of all the fonts that match this name. Once we
11855 have a list of matching fonts, we compare them against the fonts
11856 we already have by comparing names. */
09c6077f 11857 font_names = x_list_fonts (f, build_string (fontname), size, 1);
dc43ef94
KH
11858
11859 if (!NILP (font_names))
11860 {
11861 Lisp_Object tail;
11862 int i;
11863
11864 for (i = 0; i < dpyinfo->n_fonts; i++)
11865 for (tail = font_names; CONSP (tail); tail = XCONS (tail)->cdr)
06a2c219
GM
11866 if (dpyinfo->font_table[i].name
11867 && (!strcmp (dpyinfo->font_table[i].name,
11868 XSTRING (XCONS (tail)->car)->data)
11869 || !strcmp (dpyinfo->font_table[i].full_name,
11870 XSTRING (XCONS (tail)->car)->data)))
dc43ef94
KH
11871 return (dpyinfo->font_table + i);
11872 }
11873
11874 /* Load the font and add it to the table. */
11875 {
11876 char *full_name;
11877 XFontStruct *font;
11878 struct font_info *fontp;
11879 unsigned long value;
06a2c219 11880 int i;
dc43ef94 11881
2da424f1
KH
11882 /* If we have found fonts by x_list_font, load one of them. If
11883 not, we still try to load a font by the name given as FONTNAME
11884 because XListFonts (called in x_list_font) of some X server has
11885 a bug of not finding a font even if the font surely exists and
11886 is loadable by XLoadQueryFont. */
e1d6d5b9 11887 if (size > 0 && !NILP (font_names))
f613a4c8 11888 fontname = (char *) XSTRING (XCONS (font_names)->car)->data;
dc43ef94
KH
11889
11890 BLOCK_INPUT;
d645aaa4 11891 count = x_catch_errors (FRAME_X_DISPLAY (f));
dc43ef94 11892 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
d645aaa4
KH
11893 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
11894 {
11895 /* This error is perhaps due to insufficient memory on X
11896 server. Let's just ignore it. */
11897 font = NULL;
11898 x_clear_errors (FRAME_X_DISPLAY (f));
11899 }
11900 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
dc43ef94 11901 UNBLOCK_INPUT;
b5210ea7 11902 if (!font)
dc43ef94
KH
11903 return NULL;
11904
06a2c219
GM
11905 /* Find a free slot in the font table. */
11906 for (i = 0; i < dpyinfo->n_fonts; ++i)
11907 if (dpyinfo->font_table[i].name == NULL)
11908 break;
11909
11910 /* If no free slot found, maybe enlarge the font table. */
11911 if (i == dpyinfo->n_fonts
11912 && dpyinfo->n_fonts == dpyinfo->font_table_size)
dc43ef94 11913 {
06a2c219
GM
11914 int sz;
11915 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
11916 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
dc43ef94 11917 dpyinfo->font_table
06a2c219 11918 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
dc43ef94
KH
11919 }
11920
06a2c219
GM
11921 fontp = dpyinfo->font_table + i;
11922 if (i == dpyinfo->n_fonts)
11923 ++dpyinfo->n_fonts;
dc43ef94
KH
11924
11925 /* Now fill in the slots of *FONTP. */
11926 BLOCK_INPUT;
11927 fontp->font = font;
06a2c219 11928 fontp->font_idx = i;
dc43ef94
KH
11929 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
11930 bcopy (fontname, fontp->name, strlen (fontname) + 1);
11931
11932 /* Try to get the full name of FONT. Put it in FULL_NAME. */
11933 full_name = 0;
11934 if (XGetFontProperty (font, XA_FONT, &value))
11935 {
11936 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
11937 char *p = name;
11938 int dashes = 0;
11939
11940 /* Count the number of dashes in the "full name".
11941 If it is too few, this isn't really the font's full name,
11942 so don't use it.
11943 In X11R4, the fonts did not come with their canonical names
11944 stored in them. */
11945 while (*p)
11946 {
11947 if (*p == '-')
11948 dashes++;
11949 p++;
11950 }
11951
11952 if (dashes >= 13)
11953 {
11954 full_name = (char *) xmalloc (p - name + 1);
11955 bcopy (name, full_name, p - name + 1);
11956 }
11957
11958 XFree (name);
11959 }
11960
11961 if (full_name != 0)
11962 fontp->full_name = full_name;
11963 else
11964 fontp->full_name = fontp->name;
11965
11966 fontp->size = font->max_bounds.width;
d5749adb
KH
11967 fontp->height = FONT_HEIGHT (font);
11968 {
11969 /* For some font, ascent and descent in max_bounds field is
11970 larger than the above value. */
11971 int max_height = font->max_bounds.ascent + font->max_bounds.descent;
11972 if (max_height > fontp->height)
74848a96 11973 fontp->height = max_height;
d5749adb 11974 }
dc43ef94 11975
2da424f1
KH
11976 if (NILP (font_names))
11977 {
11978 /* We come here because of a bug of XListFonts mentioned at
11979 the head of this block. Let's store this information in
11980 the cache for x_list_fonts. */
11981 Lisp_Object lispy_name = build_string (fontname);
11982 Lisp_Object lispy_full_name = build_string (fontp->full_name);
11983
11984 XCONS (dpyinfo->name_list_element)->cdr
11985 = Fcons (Fcons (Fcons (lispy_name, make_number (256)),
11986 Fcons (Fcons (lispy_full_name,
11987 make_number (fontp->size)),
11988 Qnil)),
11989 XCONS (dpyinfo->name_list_element)->cdr);
11990 if (full_name)
11991 XCONS (dpyinfo->name_list_element)->cdr
11992 = Fcons (Fcons (Fcons (lispy_full_name, make_number (256)),
11993 Fcons (Fcons (lispy_full_name,
11994 make_number (fontp->size)),
11995 Qnil)),
11996 XCONS (dpyinfo->name_list_element)->cdr);
11997 }
11998
dc43ef94
KH
11999 /* The slot `encoding' specifies how to map a character
12000 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
ce667c3e 12001 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF, 0:0x2020..0x7F7F,
8ff102bd
RS
12002 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF,
12003 0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF, or
12004 2:0xA020..0xFF7F). For the moment, we don't know which charset
06a2c219 12005 uses this font. So, we set information in fontp->encoding[1]
8ff102bd
RS
12006 which is never used by any charset. If mapping can't be
12007 decided, set FONT_ENCODING_NOT_DECIDED. */
dc43ef94
KH
12008 fontp->encoding[1]
12009 = (font->max_byte1 == 0
12010 /* 1-byte font */
12011 ? (font->min_char_or_byte2 < 0x80
12012 ? (font->max_char_or_byte2 < 0x80
12013 ? 0 /* 0x20..0x7F */
8ff102bd 12014 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
dc43ef94
KH
12015 : 1) /* 0xA0..0xFF */
12016 /* 2-byte font */
12017 : (font->min_byte1 < 0x80
12018 ? (font->max_byte1 < 0x80
12019 ? (font->min_char_or_byte2 < 0x80
12020 ? (font->max_char_or_byte2 < 0x80
12021 ? 0 /* 0x2020..0x7F7F */
8ff102bd 12022 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
dc43ef94 12023 : 3) /* 0x20A0..0x7FFF */
8ff102bd 12024 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
dc43ef94
KH
12025 : (font->min_char_or_byte2 < 0x80
12026 ? (font->max_char_or_byte2 < 0x80
12027 ? 2 /* 0xA020..0xFF7F */
8ff102bd 12028 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
dc43ef94
KH
12029 : 1))); /* 0xA0A0..0xFFFF */
12030
12031 fontp->baseline_offset
12032 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
12033 ? (long) value : 0);
12034 fontp->relative_compose
12035 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
12036 ? (long) value : 0);
f78798df
KH
12037 fontp->default_ascent
12038 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
12039 ? (long) value : 0);
dc43ef94 12040
06a2c219
GM
12041 /* Set global flag fonts_changed_p to non-zero if the font loaded
12042 has a character with a smaller width than any other character
12043 before, or if the font loaded has a smalle>r height than any
12044 other font loaded before. If this happens, it will make a
12045 glyph matrix reallocation necessary. */
12046 fonts_changed_p = x_compute_min_glyph_bounds (f);
dc43ef94 12047 UNBLOCK_INPUT;
dc43ef94
KH
12048 return fontp;
12049 }
12050}
12051
06a2c219
GM
12052
12053/* Return a pointer to struct font_info of a font named FONTNAME for
12054 frame F. If no such font is loaded, return NULL. */
12055
dc43ef94
KH
12056struct font_info *
12057x_query_font (f, fontname)
12058 struct frame *f;
12059 register char *fontname;
12060{
12061 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12062 int i;
12063
12064 for (i = 0; i < dpyinfo->n_fonts; i++)
06a2c219
GM
12065 if (dpyinfo->font_table[i].name
12066 && (!strcmp (dpyinfo->font_table[i].name, fontname)
12067 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
dc43ef94
KH
12068 return (dpyinfo->font_table + i);
12069 return NULL;
12070}
12071
06a2c219
GM
12072
12073/* Find a CCL program for a font specified by FONTP, and set the member
a6582676
KH
12074 `encoder' of the structure. */
12075
12076void
12077x_find_ccl_program (fontp)
12078 struct font_info *fontp;
12079{
12080 extern Lisp_Object Vfont_ccl_encoder_alist, Vccl_program_table;
12081 extern Lisp_Object Qccl_program_idx;
12082 extern Lisp_Object resolve_symbol_ccl_program ();
12083 Lisp_Object list, elt, ccl_prog, ccl_id;
12084
12085 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCONS (list)->cdr)
12086 {
12087 elt = XCONS (list)->car;
12088 if (CONSP (elt)
12089 && STRINGP (XCONS (elt)->car)
12090 && (fast_c_string_match_ignore_case (XCONS (elt)->car, fontp->name)
12091 >= 0))
12092 {
12093 if (SYMBOLP (XCONS (elt)->cdr) &&
12094 (!NILP (ccl_id = Fget (XCONS (elt)->cdr, Qccl_program_idx))))
12095 {
12096 ccl_prog = XVECTOR (Vccl_program_table)->contents[XUINT (ccl_id)];
12097 if (!CONSP (ccl_prog)) continue;
12098 ccl_prog = XCONS (ccl_prog)->cdr;
12099 }
12100 else
12101 {
12102 ccl_prog = XCONS (elt)->cdr;
12103 if (!VECTORP (ccl_prog)) continue;
12104 }
12105
12106 fontp->font_encoder
12107 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
12108 setup_ccl_program (fontp->font_encoder,
12109 resolve_symbol_ccl_program (ccl_prog));
12110 break;
12111 }
12112 }
12113}
12114
06a2c219 12115
dc43ef94 12116\f
06a2c219
GM
12117/***********************************************************************
12118 Initialization
12119 ***********************************************************************/
f451eb13 12120
3afe33e7
RS
12121#ifdef USE_X_TOOLKIT
12122static XrmOptionDescRec emacs_options[] = {
12123 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
12124 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
12125
12126 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
12127 XrmoptionSepArg, NULL},
12128 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
12129
12130 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12131 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12132 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12133 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12134 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12135 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
12136 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
12137};
12138#endif /* USE_X_TOOLKIT */
12139
7a13e894
RS
12140static int x_initialized;
12141
29b38361
KH
12142#ifdef MULTI_KBOARD
12143/* Test whether two display-name strings agree up to the dot that separates
12144 the screen number from the server number. */
12145static int
12146same_x_server (name1, name2)
12147 char *name1, *name2;
12148{
12149 int seen_colon = 0;
cf591cc1
RS
12150 unsigned char *system_name = XSTRING (Vsystem_name)->data;
12151 int system_name_length = strlen (system_name);
12152 int length_until_period = 0;
12153
12154 while (system_name[length_until_period] != 0
12155 && system_name[length_until_period] != '.')
12156 length_until_period++;
12157
12158 /* Treat `unix' like an empty host name. */
12159 if (! strncmp (name1, "unix:", 5))
12160 name1 += 4;
12161 if (! strncmp (name2, "unix:", 5))
12162 name2 += 4;
12163 /* Treat this host's name like an empty host name. */
12164 if (! strncmp (name1, system_name, system_name_length)
12165 && name1[system_name_length] == ':')
12166 name1 += system_name_length;
12167 if (! strncmp (name2, system_name, system_name_length)
12168 && name2[system_name_length] == ':')
12169 name2 += system_name_length;
12170 /* Treat this host's domainless name like an empty host name. */
12171 if (! strncmp (name1, system_name, length_until_period)
12172 && name1[length_until_period] == ':')
12173 name1 += length_until_period;
12174 if (! strncmp (name2, system_name, length_until_period)
12175 && name2[length_until_period] == ':')
12176 name2 += length_until_period;
12177
29b38361
KH
12178 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
12179 {
12180 if (*name1 == ':')
12181 seen_colon++;
12182 if (seen_colon && *name1 == '.')
12183 return 1;
12184 }
12185 return (seen_colon
12186 && (*name1 == '.' || *name1 == '\0')
12187 && (*name2 == '.' || *name2 == '\0'));
12188}
12189#endif
12190
71f8198a
PE
12191#if defined (HAVE_X_I18N) || (defined (USE_X_TOOLKIT) && defined (HAVE_X11XTR6))
12192/* Recover from setlocale (LC_ALL, ""). */
12193static void
12194fixup_locale ()
12195{
12196 /* Currently we require strerror to use the "C" locale,
12197 since we don't yet support decoding its string result. */
12198#ifdef LC_MESSAGES
12199 setlocale (LC_MESSAGES, "C");
12200#endif
12201
12202 /* The Emacs Lisp reader needs LC_NUMERIC to be "C",
12203 so that numbers are read and printed properly for Emacs Lisp. */
12204 setlocale (LC_NUMERIC, "C");
12205
12206 /* Currently we require strftime to use the "C" locale,
12207 since we don't yet support encoding its format argument,
12208 or decoding its string result. */
12209 setlocale (LC_TIME, "C");
12210}
12211#endif
12212
334208b7 12213struct x_display_info *
1f8255f2 12214x_term_init (display_name, xrm_option, resource_name)
334208b7 12215 Lisp_Object display_name;
1f8255f2
RS
12216 char *xrm_option;
12217 char *resource_name;
dc6f92b8 12218{
334208b7 12219 int connection;
7a13e894 12220 Display *dpy;
334208b7
RS
12221 struct x_display_info *dpyinfo;
12222 XrmDatabase xrdb;
12223
60439948
KH
12224 BLOCK_INPUT;
12225
7a13e894
RS
12226 if (!x_initialized)
12227 {
12228 x_initialize ();
12229 x_initialized = 1;
12230 }
dc6f92b8 12231
6c183ba5 12232#ifdef HAVE_X_I18N
6186a4a0 12233 setlocale (LC_ALL, "");
71f8198a 12234 fixup_locale ();
6c183ba5
RS
12235#endif
12236
3afe33e7 12237#ifdef USE_X_TOOLKIT
2d7fc7e8
RS
12238 /* weiner@footloose.sps.mot.com reports that this causes
12239 errors with X11R5:
12240 X protocol error: BadAtom (invalid Atom parameter)
12241 on protocol request 18skiloaf.
12242 So let's not use it until R6. */
12243#ifdef HAVE_X11XTR6
bdcd49ba
RS
12244 XtSetLanguageProc (NULL, NULL, NULL);
12245#endif
12246
7f9c7f94
RS
12247 {
12248 int argc = 0;
12249 char *argv[3];
12250
12251 argv[0] = "";
12252 argc = 1;
12253 if (xrm_option)
12254 {
12255 argv[argc++] = "-xrm";
12256 argv[argc++] = xrm_option;
12257 }
12258 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
12259 resource_name, EMACS_CLASS,
12260 emacs_options, XtNumber (emacs_options),
12261 &argc, argv);
39d8bb4d
KH
12262
12263#ifdef HAVE_X11XTR6
10537cb1 12264 /* I think this is to compensate for XtSetLanguageProc. */
71f8198a 12265 fixup_locale ();
39d8bb4d 12266#endif
7f9c7f94 12267 }
3afe33e7
RS
12268
12269#else /* not USE_X_TOOLKIT */
bdcd49ba
RS
12270#ifdef HAVE_X11R5
12271 XSetLocaleModifiers ("");
12272#endif
7a13e894 12273 dpy = XOpenDisplay (XSTRING (display_name)->data);
3afe33e7 12274#endif /* not USE_X_TOOLKIT */
334208b7 12275
7a13e894
RS
12276 /* Detect failure. */
12277 if (dpy == 0)
60439948
KH
12278 {
12279 UNBLOCK_INPUT;
12280 return 0;
12281 }
7a13e894
RS
12282
12283 /* We have definitely succeeded. Record the new connection. */
12284
12285 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
12286
29b38361
KH
12287#ifdef MULTI_KBOARD
12288 {
12289 struct x_display_info *share;
12290 Lisp_Object tail;
12291
12292 for (share = x_display_list, tail = x_display_name_list; share;
12293 share = share->next, tail = XCONS (tail)->cdr)
12294 if (same_x_server (XSTRING (XCONS (XCONS (tail)->car)->car)->data,
12295 XSTRING (display_name)->data))
12296 break;
12297 if (share)
12298 dpyinfo->kboard = share->kboard;
12299 else
12300 {
12301 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
12302 init_kboard (dpyinfo->kboard);
59e755be
KH
12303 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
12304 {
12305 char *vendor = ServerVendor (dpy);
12306 dpyinfo->kboard->Vsystem_key_alist
12307 = call1 (Qvendor_specific_keysyms,
12308 build_string (vendor ? vendor : ""));
12309 }
12310
29b38361
KH
12311 dpyinfo->kboard->next_kboard = all_kboards;
12312 all_kboards = dpyinfo->kboard;
0ad5446c
KH
12313 /* Don't let the initial kboard remain current longer than necessary.
12314 That would cause problems if a file loaded on startup tries to
06a2c219 12315 prompt in the mini-buffer. */
0ad5446c
KH
12316 if (current_kboard == initial_kboard)
12317 current_kboard = dpyinfo->kboard;
29b38361
KH
12318 }
12319 dpyinfo->kboard->reference_count++;
12320 }
b9737ad3
KH
12321#endif
12322
7a13e894
RS
12323 /* Put this display on the chain. */
12324 dpyinfo->next = x_display_list;
12325 x_display_list = dpyinfo;
12326
12327 /* Put it on x_display_name_list as well, to keep them parallel. */
12328 x_display_name_list = Fcons (Fcons (display_name, Qnil),
12329 x_display_name_list);
12330 dpyinfo->name_list_element = XCONS (x_display_name_list)->car;
12331
12332 dpyinfo->display = dpy;
dc6f92b8 12333
dc6f92b8 12334#if 0
7a13e894 12335 XSetAfterFunction (x_current_display, x_trace_wire);
c118dd06 12336#endif /* ! 0 */
7a13e894
RS
12337
12338 dpyinfo->x_id_name
fc932ac6
RS
12339 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
12340 + STRING_BYTES (XSTRING (Vsystem_name))
7a13e894
RS
12341 + 2);
12342 sprintf (dpyinfo->x_id_name, "%s@%s",
12343 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
28430d3c
JB
12344
12345 /* Figure out which modifier bits mean what. */
334208b7 12346 x_find_modifier_meanings (dpyinfo);
f451eb13 12347
ab648270 12348 /* Get the scroll bar cursor. */
7a13e894 12349 dpyinfo->vertical_scroll_bar_cursor
334208b7 12350 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
f451eb13 12351
334208b7
RS
12352 xrdb = x_load_resources (dpyinfo->display, xrm_option,
12353 resource_name, EMACS_CLASS);
12354#ifdef HAVE_XRMSETDATABASE
12355 XrmSetDatabase (dpyinfo->display, xrdb);
12356#else
12357 dpyinfo->display->db = xrdb;
12358#endif
547d9db8 12359 /* Put the rdb where we can find it in a way that works on
7a13e894
RS
12360 all versions. */
12361 dpyinfo->xrdb = xrdb;
334208b7
RS
12362
12363 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
12364 DefaultScreen (dpyinfo->display));
12365 dpyinfo->visual = select_visual (dpyinfo->display, dpyinfo->screen,
12366 &dpyinfo->n_planes);
12367 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
12368 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
12369 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
12370 dpyinfo->grabbed = 0;
12371 dpyinfo->reference_count = 0;
12372 dpyinfo->icon_bitmap_id = -1;
06a2c219 12373 dpyinfo->font_table = NULL;
7a13e894
RS
12374 dpyinfo->n_fonts = 0;
12375 dpyinfo->font_table_size = 0;
12376 dpyinfo->bitmaps = 0;
12377 dpyinfo->bitmaps_size = 0;
12378 dpyinfo->bitmaps_last = 0;
12379 dpyinfo->scratch_cursor_gc = 0;
12380 dpyinfo->mouse_face_mouse_frame = 0;
12381 dpyinfo->mouse_face_deferred_gc = 0;
12382 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
12383 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
06a2c219 12384 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
7a13e894
RS
12385 dpyinfo->mouse_face_window = Qnil;
12386 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
12387 dpyinfo->mouse_face_defer = 0;
0f941935
KH
12388 dpyinfo->x_focus_frame = 0;
12389 dpyinfo->x_focus_event_frame = 0;
12390 dpyinfo->x_highlight_frame = 0;
06a2c219 12391 dpyinfo->image_cache = make_image_cache ();
334208b7 12392
06a2c219
GM
12393 {
12394 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
12395 double pixels = DisplayHeight (dpyinfo->display, screen_number);
12396 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
12397 dpyinfo->resy = pixels * 25.4 / mm;
12398 pixels = DisplayWidth (dpyinfo->display, screen_number);
12399 mm = DisplayWidthMM (dpyinfo->display, screen_number);
12400 dpyinfo->resx = pixels * 25.4 / mm;
12401 }
12402
334208b7
RS
12403 dpyinfo->Xatom_wm_protocols
12404 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
12405 dpyinfo->Xatom_wm_take_focus
12406 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
12407 dpyinfo->Xatom_wm_save_yourself
12408 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
12409 dpyinfo->Xatom_wm_delete_window
12410 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
12411 dpyinfo->Xatom_wm_change_state
12412 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
12413 dpyinfo->Xatom_wm_configure_denied
12414 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
12415 dpyinfo->Xatom_wm_window_moved
12416 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
12417 dpyinfo->Xatom_editres
12418 = XInternAtom (dpyinfo->display, "Editres", False);
12419 dpyinfo->Xatom_CLIPBOARD
12420 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
12421 dpyinfo->Xatom_TIMESTAMP
12422 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
12423 dpyinfo->Xatom_TEXT
12424 = XInternAtom (dpyinfo->display, "TEXT", False);
dc43ef94
KH
12425 dpyinfo->Xatom_COMPOUND_TEXT
12426 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
334208b7
RS
12427 dpyinfo->Xatom_DELETE
12428 = XInternAtom (dpyinfo->display, "DELETE", False);
12429 dpyinfo->Xatom_MULTIPLE
12430 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
12431 dpyinfo->Xatom_INCR
12432 = XInternAtom (dpyinfo->display, "INCR", False);
12433 dpyinfo->Xatom_EMACS_TMP
12434 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
12435 dpyinfo->Xatom_TARGETS
12436 = XInternAtom (dpyinfo->display, "TARGETS", False);
12437 dpyinfo->Xatom_NULL
12438 = XInternAtom (dpyinfo->display, "NULL", False);
12439 dpyinfo->Xatom_ATOM_PAIR
12440 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
dc43ef94
KH
12441 /* For properties of font. */
12442 dpyinfo->Xatom_PIXEL_SIZE
12443 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
12444 dpyinfo->Xatom_MULE_BASELINE_OFFSET
12445 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
12446 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
12447 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
f78798df
KH
12448 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
12449 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
334208b7 12450
06a2c219
GM
12451 /* Ghostscript support. */
12452 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
12453 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
12454
12455 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
12456 False);
12457
547d9db8
KH
12458 dpyinfo->cut_buffers_initialized = 0;
12459
334208b7
RS
12460 connection = ConnectionNumber (dpyinfo->display);
12461 dpyinfo->connection = connection;
12462
dc43ef94 12463 {
5d7cc324
RS
12464 char null_bits[1];
12465
12466 null_bits[0] = 0x00;
dc43ef94
KH
12467
12468 dpyinfo->null_pixel
12469 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
12470 null_bits, 1, 1, (long) 0, (long) 0,
12471 1);
12472 }
12473
06a2c219
GM
12474 {
12475 extern int gray_bitmap_width, gray_bitmap_height;
12476 extern unsigned char *gray_bitmap_bits;
12477 dpyinfo->gray
12478 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
12479 gray_bitmap_bits,
12480 gray_bitmap_width, gray_bitmap_height,
12481 (unsigned long) 1, (unsigned long) 0, 1);
12482 }
12483
87485d6f
MW
12484#ifdef subprocesses
12485 /* This is only needed for distinguishing keyboard and process input. */
334208b7 12486 if (connection != 0)
7a13e894 12487 add_keyboard_wait_descriptor (connection);
87485d6f 12488#endif
6d4238f3 12489
041b69ac 12490#ifndef F_SETOWN_BUG
dc6f92b8 12491#ifdef F_SETOWN
dc6f92b8 12492#ifdef F_SETOWN_SOCK_NEG
61c3ce62 12493 /* stdin is a socket here */
334208b7 12494 fcntl (connection, F_SETOWN, -getpid ());
c118dd06 12495#else /* ! defined (F_SETOWN_SOCK_NEG) */
334208b7 12496 fcntl (connection, F_SETOWN, getpid ());
c118dd06
JB
12497#endif /* ! defined (F_SETOWN_SOCK_NEG) */
12498#endif /* ! defined (F_SETOWN) */
041b69ac 12499#endif /* F_SETOWN_BUG */
dc6f92b8
JB
12500
12501#ifdef SIGIO
eee20f6a
KH
12502 if (interrupt_input)
12503 init_sigio (connection);
c118dd06 12504#endif /* ! defined (SIGIO) */
dc6f92b8 12505
51b592fb 12506#ifdef USE_LUCID
f8c39f51 12507#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
51b592fb
RS
12508 /* Make sure that we have a valid font for dialog boxes
12509 so that Xt does not crash. */
12510 {
12511 Display *dpy = dpyinfo->display;
12512 XrmValue d, fr, to;
12513 Font font;
e99db5a1 12514 int count;
51b592fb
RS
12515
12516 d.addr = (XPointer)&dpy;
12517 d.size = sizeof (Display *);
12518 fr.addr = XtDefaultFont;
12519 fr.size = sizeof (XtDefaultFont);
12520 to.size = sizeof (Font *);
12521 to.addr = (XPointer)&font;
e99db5a1 12522 count = x_catch_errors (dpy);
51b592fb
RS
12523 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
12524 abort ();
12525 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
12526 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
e99db5a1 12527 x_uncatch_errors (dpy, count);
51b592fb
RS
12528 }
12529#endif
f8c39f51 12530#endif
51b592fb 12531
60439948
KH
12532 UNBLOCK_INPUT;
12533
7a13e894
RS
12534 return dpyinfo;
12535}
12536\f
12537/* Get rid of display DPYINFO, assuming all frames are already gone,
12538 and without sending any more commands to the X server. */
dc6f92b8 12539
7a13e894
RS
12540void
12541x_delete_display (dpyinfo)
12542 struct x_display_info *dpyinfo;
12543{
12544 delete_keyboard_wait_descriptor (dpyinfo->connection);
12545
12546 /* Discard this display from x_display_name_list and x_display_list.
12547 We can't use Fdelq because that can quit. */
12548 if (! NILP (x_display_name_list)
12549 && EQ (XCONS (x_display_name_list)->car, dpyinfo->name_list_element))
12550 x_display_name_list = XCONS (x_display_name_list)->cdr;
12551 else
12552 {
12553 Lisp_Object tail;
12554
12555 tail = x_display_name_list;
12556 while (CONSP (tail) && CONSP (XCONS (tail)->cdr))
12557 {
12558 if (EQ (XCONS (XCONS (tail)->cdr)->car,
12559 dpyinfo->name_list_element))
12560 {
12561 XCONS (tail)->cdr = XCONS (XCONS (tail)->cdr)->cdr;
12562 break;
12563 }
12564 tail = XCONS (tail)->cdr;
12565 }
12566 }
12567
12568 if (x_display_list == dpyinfo)
12569 x_display_list = dpyinfo->next;
7f9c7f94
RS
12570 else
12571 {
12572 struct x_display_info *tail;
7a13e894 12573
7f9c7f94
RS
12574 for (tail = x_display_list; tail; tail = tail->next)
12575 if (tail->next == dpyinfo)
12576 tail->next = tail->next->next;
12577 }
7a13e894 12578
0d777288
RS
12579#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
12580#ifndef AIX /* On AIX, XCloseDisplay calls this. */
7f9c7f94
RS
12581 XrmDestroyDatabase (dpyinfo->xrdb);
12582#endif
0d777288 12583#endif
29b38361
KH
12584#ifdef MULTI_KBOARD
12585 if (--dpyinfo->kboard->reference_count == 0)
39f79001 12586 delete_kboard (dpyinfo->kboard);
b9737ad3
KH
12587#endif
12588 xfree (dpyinfo->font_table);
12589 xfree (dpyinfo->x_id_name);
12590 xfree (dpyinfo);
7a13e894
RS
12591}
12592\f
12593/* Set up use of X before we make the first connection. */
12594
06a2c219
GM
12595static struct redisplay_interface x_redisplay_interface =
12596{
12597 x_produce_glyphs,
12598 x_write_glyphs,
12599 x_insert_glyphs,
12600 x_clear_end_of_line,
12601 x_scroll_run,
12602 x_after_update_window_line,
12603 x_update_window_begin,
12604 x_update_window_end,
12605 XTcursor_to,
12606 x_flush,
12607 x_get_glyph_overhangs
12608};
12609
dfcf069d 12610void
7a13e894
RS
12611x_initialize ()
12612{
06a2c219
GM
12613 rif = &x_redisplay_interface;
12614
12615 clear_frame_hook = x_clear_frame;
12616 ins_del_lines_hook = x_ins_del_lines;
12617 change_line_highlight_hook = x_change_line_highlight;
12618 delete_glyphs_hook = x_delete_glyphs;
dc6f92b8
JB
12619 ring_bell_hook = XTring_bell;
12620 reset_terminal_modes_hook = XTreset_terminal_modes;
12621 set_terminal_modes_hook = XTset_terminal_modes;
06a2c219
GM
12622 update_begin_hook = x_update_begin;
12623 update_end_hook = x_update_end;
dc6f92b8
JB
12624 set_terminal_window_hook = XTset_terminal_window;
12625 read_socket_hook = XTread_socket;
b8009dd1 12626 frame_up_to_date_hook = XTframe_up_to_date;
dc6f92b8 12627 reassert_line_highlight_hook = XTreassert_line_highlight;
90e65f07 12628 mouse_position_hook = XTmouse_position;
f451eb13 12629 frame_rehighlight_hook = XTframe_rehighlight;
dbc4e1c1 12630 frame_raise_lower_hook = XTframe_raise_lower;
ab648270
JB
12631 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
12632 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
12633 redeem_scroll_bar_hook = XTredeem_scroll_bar;
12634 judge_scroll_bars_hook = XTjudge_scroll_bars;
06a2c219 12635 estimate_mode_line_height_hook = x_estimate_mode_line_height;
58769bee 12636
f676886a 12637 scroll_region_ok = 1; /* we'll scroll partial frames */
dc6f92b8
JB
12638 char_ins_del_ok = 0; /* just as fast to write the line */
12639 line_ins_del_ok = 1; /* we'll just blt 'em */
12640 fast_clear_end_of_line = 1; /* X does this well */
58769bee 12641 memory_below_frame = 0; /* we don't remember what scrolls
dc6f92b8
JB
12642 off the bottom */
12643 baud_rate = 19200;
12644
7a13e894 12645 x_noop_count = 0;
06a2c219
GM
12646 last_toolbar_item = -1;
12647 any_help_event_p = 0;
12648
b30b24cb
RS
12649 /* Try to use interrupt input; if we can't, then start polling. */
12650 Fset_input_mode (Qt, Qnil, Qt, Qnil);
12651
7f9c7f94
RS
12652#ifdef USE_X_TOOLKIT
12653 XtToolkitInitialize ();
12654 Xt_app_con = XtCreateApplicationContext ();
665881ad 12655 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
7f9c7f94
RS
12656#endif
12657
58769bee 12658 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 12659 original error handler. */
e99db5a1 12660 XSetErrorHandler (x_error_handler);
334208b7 12661 XSetIOErrorHandler (x_io_error_quitter);
dc6f92b8 12662
06a2c219 12663 /* Disable Window Change signals; they are handled by X events. */
dc6f92b8
JB
12664#ifdef SIGWINCH
12665 signal (SIGWINCH, SIG_DFL);
c118dd06 12666#endif /* ! defined (SIGWINCH) */
dc6f92b8 12667
92e2441b 12668 signal (SIGPIPE, x_connection_signal);
dc6f92b8 12669}
55123275 12670
06a2c219 12671
55123275
JB
12672void
12673syms_of_xterm ()
12674{
e99db5a1
RS
12675 staticpro (&x_error_message_string);
12676 x_error_message_string = Qnil;
12677
7a13e894
RS
12678 staticpro (&x_display_name_list);
12679 x_display_name_list = Qnil;
334208b7 12680
ab648270 12681 staticpro (&last_mouse_scroll_bar);
e53cb100 12682 last_mouse_scroll_bar = Qnil;
59e755be
KH
12683
12684 staticpro (&Qvendor_specific_keysyms);
12685 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
2237cac9
RS
12686
12687 staticpro (&last_mouse_press_frame);
12688 last_mouse_press_frame = Qnil;
06a2c219
GM
12689
12690 staticpro (&help_echo);
12691 help_echo = Qnil;
12692 staticpro (&previous_help_echo);
12693 previous_help_echo = Qnil;
12694
12695 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
12696 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
12697For example, if a block cursor is over a tab, it will be drawn as\n\
12698wide as that tab on the display.");
12699 x_stretch_cursor_p = 0;
12700
12701 DEFVAR_BOOL ("x-toolkit-scroll-bars-p", &x_toolkit_scroll_bars_p,
12702 "If not nil, Emacs uses toolkit scroll bars.");
12703#if USE_TOOLKIT_SCROLL_BARS
12704 x_toolkit_scroll_bars_p = 1;
12705#else
12706 x_toolkit_scroll_bars_p = 0;
12707#endif
12708
12709 defsubr (&Sxt_process_timeouts);
12710 staticpro (&last_mouse_motion_frame);
12711 last_mouse_motion_frame = Qnil;
55123275 12712}
6cf0ae86
RS
12713
12714#endif /* not HAVE_X_WINDOWS */
06a2c219 12715