(gen_help_event, kbd_buffer_store_help_event):
[bpt/emacs.git] / src / xterm.c
CommitLineData
dc6f92b8 1/* X Communication module for terminals which understand the X protocol.
1c7e22fd 2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000
06a2c219 3 Free Software Foundation, Inc.
dc6f92b8
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
4746118a 9the Free Software Foundation; either version 2, or (at your option)
dc6f92b8
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
dc6f92b8 21
06a2c219 22/* New display code by Gerd Moellmann <gerd@gnu.org>. */
3afe33e7
RS
23/* Xt features made by Fred Pierresteguy. */
24
68c45bf0
PE
25#include <config.h>
26
039440c4 27/* On 4.3 these lose if they come after xterm.h. */
039440c4 28/* Putting these at the beginning seems to be standard for other .c files. */
039440c4
RS
29#include <signal.h>
30
4846819e
RS
31#include <stdio.h>
32
dc6f92b8
JB
33#ifdef HAVE_X_WINDOWS
34
35#include "lisp.h"
9ac0d9e0 36#include "blockinput.h"
dc6f92b8 37
ae79c227
AS
38/* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40#include "syssignal.h"
41
dc6f92b8
JB
42/* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44#include "xterm.h"
f451eb13 45#include <X11/cursorfont.h>
dc6f92b8
JB
46
47/* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49#ifndef makedev
50#include <sys/types.h>
c118dd06 51#endif /* makedev */
dc6f92b8 52
6df54671 53#ifdef BSD_SYSTEM
dc6f92b8 54#include <sys/ioctl.h>
6df54671 55#endif /* ! defined (BSD_SYSTEM) */
dc6f92b8 56
2d368234 57#include "systty.h"
3a2712f9 58#include "systime.h"
dc6f92b8 59
b8009dd1 60#ifndef INCLUDED_FCNTL
dc6f92b8 61#include <fcntl.h>
b8009dd1 62#endif
dc6f92b8
JB
63#include <ctype.h>
64#include <errno.h>
65#include <setjmp.h>
66#include <sys/stat.h>
a0a7635f
RS
67/* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
68/* #include <sys/param.h> */
dc6f92b8 69
dc43ef94
KH
70#include "charset.h"
71#include "ccl.h"
7a13e894 72#include "frame.h"
dc6f92b8 73#include "dispextern.h"
ee569018 74#include "fontset.h"
dc6f92b8
JB
75#include "termhooks.h"
76#include "termopts.h"
77#include "termchar.h"
78#if 0
79#include "sink.h"
80#include "sinkmask.h"
c118dd06 81#endif /* ! 0 */
dc6f92b8 82#include "gnu.h"
dc6f92b8 83#include "disptab.h"
dc6f92b8 84#include "buffer.h"
f451eb13 85#include "window.h"
3b2fa4e6 86#include "keyboard.h"
bde7c500 87#include "intervals.h"
dfcf069d 88#include "process.h"
bffcfca9 89#include "atimer.h"
dc6f92b8 90
d2bd6bc4
RS
91#ifdef USE_X_TOOLKIT
92#include <X11/Shell.h>
93#endif
94
06a2c219
GM
95#ifdef HAVE_SYS_TIME_H
96#include <sys/time.h>
97#endif
98#ifdef HAVE_UNISTD_H
99#include <unistd.h>
100#endif
101
3afe33e7 102#ifdef USE_X_TOOLKIT
06a2c219 103
9d7e2e3e 104extern void free_frame_menubar ();
2224b905 105extern FRAME_PTR x_menubar_window_to_frame ();
06a2c219 106
0fdff6bb
RS
107#if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
108#define HACK_EDITRES
109extern void _XEditResCheckMessages ();
110#endif /* not NO_EDITRES */
06a2c219
GM
111
112/* Include toolkit specific headers for the scroll bar widget. */
113
114#ifdef USE_TOOLKIT_SCROLL_BARS
115#if defined USE_MOTIF
116#include <Xm/Xm.h> /* for LESSTIF_VERSION */
117#include <Xm/ScrollBar.h>
118#include <Xm/ScrollBarP.h>
ec18280f
SM
119#else /* !USE_MOTIF i.e. use Xaw */
120
121#ifdef HAVE_XAW3D
06a2c219 122#include <X11/Xaw3d/Simple.h>
06a2c219
GM
123#include <X11/Xaw3d/Scrollbar.h>
124#define ARROW_SCROLLBAR
125#include <X11/Xaw3d/ScrollbarP.h>
ec18280f
SM
126#else /* !HAVE_XAW3D */
127#include <X11/Xaw/Simple.h>
128#include <X11/Xaw/Scrollbar.h>
129#endif /* !HAVE_XAW3D */
130#ifndef XtNpickTop
131#define XtNpickTop "pickTop"
132#endif /* !XtNpickTop */
133#endif /* !USE_MOTIF */
06a2c219
GM
134#endif /* USE_TOOLKIT_SCROLL_BARS */
135
3afe33e7
RS
136#endif /* USE_X_TOOLKIT */
137
b849c413
RS
138#ifndef USE_X_TOOLKIT
139#define x_any_window_to_frame x_window_to_frame
5627c40e 140#define x_top_window_to_frame x_window_to_frame
b849c413
RS
141#endif
142
546e6d5b 143#ifdef USE_X_TOOLKIT
d067ea8b 144#include "widget.h"
546e6d5b
RS
145#ifndef XtNinitialState
146#define XtNinitialState "initialState"
147#endif
148#endif
149
80528801
KH
150#ifdef SOLARIS2
151/* memmove will be defined as a macro in Xfuncs.h unless
152 <string.h> is included beforehand. The declaration for memmove in
153 <string.h> will cause a syntax error when Xfuncs.h later includes it. */
154#include <string.h>
155#endif
156
e4b68333 157#ifndef min
06a2c219 158#define min(a,b) ((a) < (b) ? (a) : (b))
e4b68333
RS
159#endif
160#ifndef max
06a2c219
GM
161#define max(a,b) ((a) > (b) ? (a) : (b))
162#endif
163
164#define abs(x) ((x) < 0 ? -(x) : (x))
165
166#define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
167
168\f
169/* Bitmaps for truncated lines. */
170
171enum bitmap_type
172{
173 NO_BITMAP,
174 LEFT_TRUNCATION_BITMAP,
175 RIGHT_TRUNCATION_BITMAP,
176 OVERLAY_ARROW_BITMAP,
177 CONTINUED_LINE_BITMAP,
178 CONTINUATION_LINE_BITMAP,
179 ZV_LINE_BITMAP
180};
181
182/* Bitmap drawn to indicate lines not displaying text if
183 `indicate-empty-lines' is non-nil. */
184
185#define zv_width 8
186#define zv_height 8
187static unsigned char zv_bits[] = {
188 0x00, 0x00, 0x1e, 0x1e, 0x1e, 0x1e, 0x00, 0x00};
189
190/* An arrow like this: `<-'. */
191
192#define left_width 8
193#define left_height 8
194static unsigned char left_bits[] = {
195 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
196
110859fc
GM
197/* Right truncation arrow bitmap `->'. */
198
199#define right_width 8
200#define right_height 8
201static unsigned char right_bits[] = {
202 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
203
06a2c219
GM
204/* Marker for continued lines. */
205
206#define continued_width 8
207#define continued_height 8
208static unsigned char continued_bits[] = {
110859fc
GM
209 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
210
211/* Marker for continuation lines. */
06a2c219
GM
212
213#define continuation_width 8
214#define continuation_height 8
215static unsigned char continuation_bits[] = {
110859fc 216 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
06a2c219 217
110859fc 218/* Overlay arrow bitmap. */
06a2c219 219
110859fc
GM
220#if 0
221/* A bomb. */
06a2c219
GM
222#define ov_width 8
223#define ov_height 8
224static unsigned char ov_bits[] = {
225 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
06a2c219 226#else
110859fc 227/* A triangular arrow. */
06a2c219
GM
228#define ov_width 8
229#define ov_height 8
230static unsigned char ov_bits[] = {
110859fc
GM
231 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
232
e4b68333 233#endif
06a2c219
GM
234
235extern Lisp_Object Qhelp_echo;
236
69388238 237\f
06a2c219
GM
238/* Non-zero means Emacs uses toolkit scroll bars. */
239
240int x_toolkit_scroll_bars_p;
241
242/* If a string, XTread_socket generates an event to display that string.
243 (The display is done in read_char.) */
244
245static Lisp_Object help_echo;
246
247/* Temporary variable for XTread_socket. */
248
249static Lisp_Object previous_help_echo;
250
251/* Non-zero means that a HELP_EVENT has been generated since Emacs
252 start. */
253
254static int any_help_event_p;
255
256/* Non-zero means draw block and hollow cursor as wide as the glyph
257 under it. For example, if a block cursor is over a tab, it will be
258 drawn as wide as that tab on the display. */
259
260int x_stretch_cursor_p;
261
262/* This is a chain of structures for all the X displays currently in
263 use. */
264
334208b7 265struct x_display_info *x_display_list;
dc6f92b8 266
06a2c219
GM
267/* This is a list of cons cells, each of the form (NAME
268 . FONT-LIST-CACHE), one for each element of x_display_list and in
269 the same order. NAME is the name of the frame. FONT-LIST-CACHE
270 records previous values returned by x-list-fonts. */
271
7a13e894 272Lisp_Object x_display_name_list;
f451eb13 273
987d2ad1 274/* Frame being updated by update_frame. This is declared in term.c.
06a2c219
GM
275 This is set by update_begin and looked at by all the XT functions.
276 It is zero while not inside an update. In that case, the XT
277 functions assume that `selected_frame' is the frame to apply to. */
278
d0386f2a 279extern struct frame *updating_frame;
dc6f92b8 280
dfcf069d 281extern int waiting_for_input;
0e81d8cd 282
06a2c219
GM
283/* This is a frame waiting to be auto-raised, within XTread_socket. */
284
0134a210
RS
285struct frame *pending_autoraise_frame;
286
7f9c7f94
RS
287#ifdef USE_X_TOOLKIT
288/* The application context for Xt use. */
289XtAppContext Xt_app_con;
06a2c219
GM
290static String Xt_default_resources[] = {0};
291#endif /* USE_X_TOOLKIT */
665881ad 292
06a2c219
GM
293/* Nominal cursor position -- where to draw output.
294 HPOS and VPOS are window relative glyph matrix coordinates.
295 X and Y are window relative pixel coordinates. */
dc6f92b8 296
06a2c219 297struct cursor_pos output_cursor;
dc6f92b8 298
bffcfca9
GM
299/* Non-zero means user is interacting with a toolkit scroll bar. */
300
301static int toolkit_scroll_bar_interaction;
dc6f92b8 302
69388238
RS
303/* Mouse movement.
304
06a2c219 305 Formerly, we used PointerMotionHintMask (in standard_event_mask)
f5bb65ec
RS
306 so that we would have to call XQueryPointer after each MotionNotify
307 event to ask for another such event. However, this made mouse tracking
308 slow, and there was a bug that made it eventually stop.
309
310 Simply asking for MotionNotify all the time seems to work better.
311
69388238
RS
312 In order to avoid asking for motion events and then throwing most
313 of them away or busy-polling the server for mouse positions, we ask
314 the server for pointer motion hints. This means that we get only
315 one event per group of mouse movements. "Groups" are delimited by
316 other kinds of events (focus changes and button clicks, for
317 example), or by XQueryPointer calls; when one of these happens, we
318 get another MotionNotify event the next time the mouse moves. This
319 is at least as efficient as getting motion events when mouse
320 tracking is on, and I suspect only negligibly worse when tracking
f5bb65ec 321 is off. */
69388238
RS
322
323/* Where the mouse was last time we reported a mouse event. */
69388238 324
06a2c219
GM
325FRAME_PTR last_mouse_frame;
326static XRectangle last_mouse_glyph;
2237cac9
RS
327static Lisp_Object last_mouse_press_frame;
328
69388238
RS
329/* The scroll bar in which the last X motion event occurred.
330
06a2c219
GM
331 If the last X motion event occurred in a scroll bar, we set this so
332 XTmouse_position can know whether to report a scroll bar motion or
69388238
RS
333 an ordinary motion.
334
06a2c219
GM
335 If the last X motion event didn't occur in a scroll bar, we set
336 this to Qnil, to tell XTmouse_position to return an ordinary motion
337 event. */
338
69388238
RS
339static Lisp_Object last_mouse_scroll_bar;
340
69388238
RS
341/* This is a hack. We would really prefer that XTmouse_position would
342 return the time associated with the position it returns, but there
06a2c219 343 doesn't seem to be any way to wrest the time-stamp from the server
69388238
RS
344 along with the position query. So, we just keep track of the time
345 of the last movement we received, and return that in hopes that
346 it's somewhat accurate. */
06a2c219 347
69388238
RS
348static Time last_mouse_movement_time;
349
06a2c219
GM
350/* Incremented by XTread_socket whenever it really tries to read
351 events. */
352
c0a04927
RS
353#ifdef __STDC__
354static int volatile input_signal_count;
355#else
356static int input_signal_count;
357#endif
358
7a13e894 359/* Used locally within XTread_socket. */
06a2c219 360
7a13e894 361static int x_noop_count;
dc6f92b8 362
7a13e894 363/* Initial values of argv and argc. */
06a2c219 364
7a13e894
RS
365extern char **initial_argv;
366extern int initial_argc;
dc6f92b8 367
7a13e894 368extern Lisp_Object Vcommand_line_args, Vsystem_name;
dc6f92b8 369
06a2c219 370/* Tells if a window manager is present or not. */
7a13e894
RS
371
372extern Lisp_Object Vx_no_window_manager;
dc6f92b8 373
c2df547c 374extern Lisp_Object Qface, Qmouse_face;
b8009dd1 375
dc6f92b8
JB
376extern int errno;
377
dfeccd2d 378/* A mask of extra modifier bits to put into every keyboard char. */
06a2c219 379
64bb1782
RS
380extern int extra_keyboard_modifiers;
381
59e755be
KH
382static Lisp_Object Qvendor_specific_keysyms;
383
334208b7 384extern XrmDatabase x_load_resources ();
c32cdd9a
KH
385extern Lisp_Object x_icon_type ();
386
7a13e894 387
06a2c219
GM
388/* Enumeration for overriding/changing the face to use for drawing
389 glyphs in x_draw_glyphs. */
390
391enum draw_glyphs_face
392{
393 DRAW_NORMAL_TEXT,
394 DRAW_INVERSE_VIDEO,
395 DRAW_CURSOR,
396 DRAW_MOUSE_FACE,
397 DRAW_IMAGE_RAISED,
398 DRAW_IMAGE_SUNKEN
399};
400
71b8321e 401static void x_update_window_end P_ ((struct window *, int, int));
06a2c219
GM
402static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
403void x_delete_display P_ ((struct x_display_info *));
404static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
405 unsigned));
406static int fast_find_position P_ ((struct window *, int, int *, int *,
407 int *, int *));
408static void set_output_cursor P_ ((struct cursor_pos *));
409static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
410 int *, int *, int *));
411static void note_mode_line_highlight P_ ((struct window *, int, int));
06a2c219 412static void note_mouse_highlight P_ ((struct frame *, int, int));
9ea173e8
GM
413static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
414static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
06a2c219
GM
415static void show_mouse_face P_ ((struct x_display_info *,
416 enum draw_glyphs_face));
417static int x_io_error_quitter P_ ((Display *));
418int x_catch_errors P_ ((Display *));
419void x_uncatch_errors P_ ((Display *, int));
420void x_lower_frame P_ ((struct frame *));
421void x_scroll_bar_clear P_ ((struct frame *));
422int x_had_errors_p P_ ((Display *));
423void x_wm_set_size_hint P_ ((struct frame *, long, int));
424void x_raise_frame P_ ((struct frame *));
425void x_set_window_size P_ ((struct frame *, int, int, int));
426void x_wm_set_window_state P_ ((struct frame *, int));
427void x_wm_set_icon_pixmap P_ ((struct frame *, int));
428void x_initialize P_ ((void));
429static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
430static int x_compute_min_glyph_bounds P_ ((struct frame *));
431static void x_draw_phys_cursor_glyph P_ ((struct window *,
432 struct glyph_row *,
433 enum draw_glyphs_face));
434static void x_update_end P_ ((struct frame *));
435static void XTframe_up_to_date P_ ((struct frame *));
436static void XTreassert_line_highlight P_ ((int, int));
437static void x_change_line_highlight P_ ((int, int, int, int));
438static void XTset_terminal_modes P_ ((void));
439static void XTreset_terminal_modes P_ ((void));
440static void XTcursor_to P_ ((int, int, int, int));
441static void x_write_glyphs P_ ((struct glyph *, int));
442static void x_clear_end_of_line P_ ((int));
443static void x_clear_frame P_ ((void));
444static void x_clear_cursor P_ ((struct window *));
445static void frame_highlight P_ ((struct frame *));
446static void frame_unhighlight P_ ((struct frame *));
447static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
448static void XTframe_rehighlight P_ ((struct frame *));
449static void x_frame_rehighlight P_ ((struct x_display_info *));
450static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
f02d8aa0 451static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
06a2c219
GM
452static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
453 XRectangle *));
454static void expose_frame P_ ((struct frame *, int, int, int, int));
455static void expose_window_tree P_ ((struct window *, XRectangle *));
456static void expose_window P_ ((struct window *, XRectangle *));
457static void expose_area P_ ((struct window *, struct glyph_row *,
458 XRectangle *, enum glyph_row_area));
459static void expose_line P_ ((struct window *, struct glyph_row *,
460 XRectangle *));
461static void x_update_cursor_in_window_tree P_ ((struct window *, int));
462static void x_update_window_cursor P_ ((struct window *, int));
463static void x_erase_phys_cursor P_ ((struct window *));
464void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
465static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
466 enum bitmap_type));
467
468static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
469 GC, int));
470static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
471static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
472static void note_overwritten_text_cursor P_ ((struct window *, int, int));
473static void x_flush P_ ((struct frame *f));
474
475
476/* Flush display of frame F, or of all frames if F is null. */
477
478static void
479x_flush (f)
480 struct frame *f;
481{
482 BLOCK_INPUT;
483 if (f == NULL)
484 {
485 Lisp_Object rest, frame;
486 FOR_EACH_FRAME (rest, frame)
487 x_flush (XFRAME (frame));
488 }
489 else if (FRAME_X_P (f))
490 XFlush (FRAME_X_DISPLAY (f));
491 UNBLOCK_INPUT;
492}
493
dc6f92b8 494
06a2c219
GM
495/* Remove calls to XFlush by defining XFlush to an empty replacement.
496 Calls to XFlush should be unnecessary because the X output buffer
497 is flushed automatically as needed by calls to XPending,
498 XNextEvent, or XWindowEvent according to the XFlush man page.
499 XTread_socket calls XPending. Removing XFlush improves
500 performance. */
501
502#define XFlush(DISPLAY) (void) 0
b8009dd1 503
334208b7 504\f
06a2c219
GM
505/***********************************************************************
506 Debugging
507 ***********************************************************************/
508
9382638d 509#if 0
06a2c219
GM
510
511/* This is a function useful for recording debugging information about
512 the sequence of occurrences in this file. */
9382638d
KH
513
514struct record
515{
516 char *locus;
517 int type;
518};
519
520struct record event_record[100];
521
522int event_record_index;
523
524record_event (locus, type)
525 char *locus;
526 int type;
527{
528 if (event_record_index == sizeof (event_record) / sizeof (struct record))
529 event_record_index = 0;
530
531 event_record[event_record_index].locus = locus;
532 event_record[event_record_index].type = type;
533 event_record_index++;
534}
535
536#endif /* 0 */
06a2c219
GM
537
538
9382638d 539\f
334208b7
RS
540/* Return the struct x_display_info corresponding to DPY. */
541
542struct x_display_info *
543x_display_info_for_display (dpy)
544 Display *dpy;
545{
546 struct x_display_info *dpyinfo;
547
548 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
549 if (dpyinfo->display == dpy)
550 return dpyinfo;
16bd92ea 551
334208b7
RS
552 return 0;
553}
f451eb13 554
06a2c219
GM
555
556\f
557/***********************************************************************
558 Starting and ending an update
559 ***********************************************************************/
560
561/* Start an update of frame F. This function is installed as a hook
562 for update_begin, i.e. it is called when update_begin is called.
563 This function is called prior to calls to x_update_window_begin for
564 each window being updated. Currently, there is nothing to do here
565 because all interesting stuff is done on a window basis. */
dc6f92b8 566
dfcf069d 567static void
06a2c219 568x_update_begin (f)
f676886a 569 struct frame *f;
58769bee 570{
06a2c219
GM
571 /* Nothing to do. */
572}
dc6f92b8 573
dc6f92b8 574
06a2c219
GM
575/* Start update of window W. Set the global variable updated_window
576 to the window being updated and set output_cursor to the cursor
577 position of W. */
dc6f92b8 578
06a2c219
GM
579static void
580x_update_window_begin (w)
581 struct window *w;
582{
583 struct frame *f = XFRAME (WINDOW_FRAME (w));
584 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
585
586 updated_window = w;
587 set_output_cursor (&w->cursor);
b8009dd1 588
06a2c219 589 BLOCK_INPUT;
d1bc4182 590
06a2c219 591 if (f == display_info->mouse_face_mouse_frame)
b8009dd1 592 {
514e4681 593 /* Don't do highlighting for mouse motion during the update. */
06a2c219 594 display_info->mouse_face_defer = 1;
37c2c98b 595
06a2c219
GM
596 /* If F needs to be redrawn, simply forget about any prior mouse
597 highlighting. */
9f67f20b 598 if (FRAME_GARBAGED_P (f))
06a2c219
GM
599 display_info->mouse_face_window = Qnil;
600
64f26cf5
GM
601#if 0 /* Rows in a current matrix containing glyphs in mouse-face have
602 their mouse_face_p flag set, which means that they are always
603 unequal to rows in a desired matrix which never have that
604 flag set. So, rows containing mouse-face glyphs are never
605 scrolled, and we don't have to switch the mouse highlight off
606 here to prevent it from being scrolled. */
607
06a2c219
GM
608 /* Can we tell that this update does not affect the window
609 where the mouse highlight is? If so, no need to turn off.
610 Likewise, don't do anything if the frame is garbaged;
611 in that case, the frame's current matrix that we would use
612 is all wrong, and we will redisplay that line anyway. */
613 if (!NILP (display_info->mouse_face_window)
614 && w == XWINDOW (display_info->mouse_face_window))
514e4681 615 {
06a2c219 616 int i;
514e4681 617
06a2c219
GM
618 for (i = 0; i < w->desired_matrix->nrows; ++i)
619 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
514e4681
RS
620 break;
621
06a2c219
GM
622 if (i < w->desired_matrix->nrows)
623 clear_mouse_face (display_info);
514e4681 624 }
64f26cf5 625#endif /* 0 */
b8009dd1 626 }
6ccf47d1 627
dc6f92b8
JB
628 UNBLOCK_INPUT;
629}
630
06a2c219
GM
631
632/* Draw a vertical window border to the right of window W if W doesn't
633 have vertical scroll bars. */
634
dfcf069d 635static void
06a2c219
GM
636x_draw_vertical_border (w)
637 struct window *w;
58769bee 638{
06a2c219
GM
639 struct frame *f = XFRAME (WINDOW_FRAME (w));
640
641 /* Redraw borders between horizontally adjacent windows. Don't
642 do it for frames with vertical scroll bars because either the
643 right scroll bar of a window, or the left scroll bar of its
644 neighbor will suffice as a border. */
645 if (!WINDOW_RIGHTMOST_P (w)
646 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
647 {
648 int x0, x1, y0, y1;
dc6f92b8 649
06a2c219 650 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
110859fc 651 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
652 y1 -= 1;
653
654 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
655 f->output_data.x->normal_gc, x1, y0, x1, y1);
656 }
657}
658
659
71b8321e
GM
660/* End update of window W (which is equal to updated_window).
661
662 Draw vertical borders between horizontally adjacent windows, and
663 display W's cursor if CURSOR_ON_P is non-zero.
664
665 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
666 glyphs in mouse-face were overwritten. In that case we have to
667 make sure that the mouse-highlight is properly redrawn.
668
669 W may be a menu bar pseudo-window in case we don't have X toolkit
670 support. Such windows don't have a cursor, so don't display it
671 here. */
06a2c219
GM
672
673static void
71b8321e 674x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
06a2c219 675 struct window *w;
71b8321e 676 int cursor_on_p, mouse_face_overwritten_p;
06a2c219
GM
677{
678 if (!w->pseudo_window_p)
679 {
71b8321e
GM
680 struct x_display_info *dpyinfo
681 = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
682
06a2c219 683 BLOCK_INPUT;
71b8321e
GM
684
685 /* If a row with mouse-face was overwritten, arrange for
686 XTframe_up_to_date to redisplay the mouse highlight. */
687 if (mouse_face_overwritten_p)
688 {
689 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
690 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
691 dpyinfo->mouse_face_window = Qnil;
692 }
693
06a2c219
GM
694 if (cursor_on_p)
695 x_display_and_set_cursor (w, 1, output_cursor.hpos,
696 output_cursor.vpos,
697 output_cursor.x, output_cursor.y);
71b8321e 698
06a2c219
GM
699 x_draw_vertical_border (w);
700 UNBLOCK_INPUT;
701 }
702
703 updated_window = NULL;
704}
dc6f92b8 705
dc6f92b8 706
06a2c219
GM
707/* End update of frame F. This function is installed as a hook in
708 update_end. */
709
710static void
711x_update_end (f)
712 struct frame *f;
713{
714 /* Mouse highlight may be displayed again. */
aa8bff2e 715 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
b8009dd1 716
06a2c219 717 BLOCK_INPUT;
334208b7 718 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
719 UNBLOCK_INPUT;
720}
b8009dd1 721
06a2c219
GM
722
723/* This function is called from various places in xdisp.c whenever a
724 complete update has been performed. The global variable
725 updated_window is not available here. */
b8009dd1 726
dfcf069d 727static void
b8009dd1 728XTframe_up_to_date (f)
06a2c219 729 struct frame *f;
b8009dd1 730{
06a2c219 731 if (FRAME_X_P (f))
514e4681 732 {
06a2c219 733 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
71b8321e 734
06a2c219
GM
735 if (dpyinfo->mouse_face_deferred_gc
736 || f == dpyinfo->mouse_face_mouse_frame)
737 {
738 BLOCK_INPUT;
739 if (dpyinfo->mouse_face_mouse_frame)
740 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
741 dpyinfo->mouse_face_mouse_x,
742 dpyinfo->mouse_face_mouse_y);
743 dpyinfo->mouse_face_deferred_gc = 0;
744 UNBLOCK_INPUT;
745 }
514e4681 746 }
b8009dd1 747}
06a2c219
GM
748
749
750/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
751 arrow bitmaps, or clear the areas where they would be displayed
752 before DESIRED_ROW is made current. The window being updated is
753 found in updated_window. This function It is called from
754 update_window_line only if it is known that there are differences
755 between bitmaps to be drawn between current row and DESIRED_ROW. */
756
757static void
758x_after_update_window_line (desired_row)
759 struct glyph_row *desired_row;
760{
761 struct window *w = updated_window;
762
763 xassert (w);
764
765 if (!desired_row->mode_line_p && !w->pseudo_window_p)
766 {
767 BLOCK_INPUT;
768 x_draw_row_bitmaps (w, desired_row);
769
770 /* When a window has disappeared, make sure that no rest of
771 full-width rows stays visible in the internal border. */
772 if (windows_or_buffers_changed)
773 {
774 struct frame *f = XFRAME (w->frame);
775 int width = FRAME_INTERNAL_BORDER_WIDTH (f);
776 int height = desired_row->visible_height;
110859fc
GM
777 int x = (window_box_right (w, -1)
778 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
06a2c219
GM
779 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
780
781 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
782 x, y, width, height, False);
783 }
784
785 UNBLOCK_INPUT;
786 }
787}
788
789
790/* Draw the bitmap WHICH in one of the areas to the left or right of
791 window W. ROW is the glyph row for which to display the bitmap; it
792 determines the vertical position at which the bitmap has to be
793 drawn. */
794
795static void
796x_draw_bitmap (w, row, which)
797 struct window *w;
798 struct glyph_row *row;
799 enum bitmap_type which;
800{
801 struct frame *f = XFRAME (WINDOW_FRAME (w));
802 Display *display = FRAME_X_DISPLAY (f);
803 Window window = FRAME_X_WINDOW (f);
804 int x, y, wd, h, dy;
805 unsigned char *bits;
806 Pixmap pixmap;
807 GC gc = f->output_data.x->normal_gc;
808 struct face *face;
809 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
810
811 /* Must clip because of partially visible lines. */
812 x_clip_to_row (w, row, gc, 1);
813
814 switch (which)
815 {
816 case LEFT_TRUNCATION_BITMAP:
817 wd = left_width;
818 h = left_height;
819 bits = left_bits;
820 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
821 - wd
110859fc 822 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
823 break;
824
825 case OVERLAY_ARROW_BITMAP:
826 wd = left_width;
827 h = left_height;
828 bits = ov_bits;
829 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
830 - wd
110859fc 831 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
832 break;
833
834 case RIGHT_TRUNCATION_BITMAP:
835 wd = right_width;
836 h = right_height;
837 bits = right_bits;
838 x = window_box_right (w, -1);
110859fc 839 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
840 break;
841
842 case CONTINUED_LINE_BITMAP:
843 wd = right_width;
844 h = right_height;
845 bits = continued_bits;
846 x = window_box_right (w, -1);
110859fc 847 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
848 break;
849
850 case CONTINUATION_LINE_BITMAP:
851 wd = continuation_width;
852 h = continuation_height;
853 bits = continuation_bits;
854 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
855 - wd
110859fc 856 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
857 break;
858
859 case ZV_LINE_BITMAP:
860 wd = zv_width;
861 h = zv_height;
862 bits = zv_bits;
863 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
864 - wd
110859fc 865 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
866 break;
867
868 default:
869 abort ();
870 }
871
872 /* Convert to frame coordinates. Set dy to the offset in the row to
873 start drawing the bitmap. */
874 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
875 dy = (row->height - h) / 2;
876
877 /* Draw the bitmap. I believe these small pixmaps can be cached
878 by the server. */
879 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
880 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
881 face->foreground,
882 face->background, depth);
883 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
884 XFreePixmap (display, pixmap);
885 XSetClipMask (display, gc, None);
886}
887
888
889/* Draw flags bitmaps for glyph row ROW on window W. Call this
890 function with input blocked. */
891
892static void
893x_draw_row_bitmaps (w, row)
894 struct window *w;
895 struct glyph_row *row;
896{
897 struct frame *f = XFRAME (w->frame);
898 enum bitmap_type bitmap;
899 struct face *face;
045dee35 900 int header_line_height = -1;
06a2c219
GM
901
902 xassert (interrupt_input_blocked);
903
904 /* If row is completely invisible, because of vscrolling, we
905 don't have to draw anything. */
906 if (row->visible_height <= 0)
907 return;
908
909 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
910 PREPARE_FACE_FOR_DISPLAY (f, face);
911
912 /* Decide which bitmap to draw at the left side. */
913 if (row->overlay_arrow_p)
914 bitmap = OVERLAY_ARROW_BITMAP;
915 else if (row->truncated_on_left_p)
916 bitmap = LEFT_TRUNCATION_BITMAP;
917 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
918 bitmap = CONTINUATION_LINE_BITMAP;
919 else if (row->indicate_empty_line_p)
920 bitmap = ZV_LINE_BITMAP;
921 else
922 bitmap = NO_BITMAP;
923
924 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
925 the flags area. */
926 if (bitmap == NO_BITMAP
110859fc 927 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
928 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
929 {
930 /* If W has a vertical border to its left, don't draw over it. */
931 int border = ((XFASTINT (w->left) > 0
932 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
933 ? 1 : 0);
934 int left = window_box_left (w, -1);
935
045dee35
GM
936 if (header_line_height < 0)
937 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
938
939 /* In case the same realized face is used for bitmap areas and
940 for something displayed in the text (e.g. face `region' on
941 mono-displays, the fill style may have been changed to
942 FillSolid in x_draw_glyph_string_background. */
943 if (face->stipple)
944 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
945 else
946 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
947
06a2c219
GM
948 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
949 face->gc,
950 (left
110859fc 951 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219 952 + border),
045dee35 953 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 954 row->y)),
110859fc 955 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
06a2c219 956 row->visible_height);
dcd08bfb
GM
957 if (!face->stipple)
958 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
959 }
960
961 /* Draw the left bitmap. */
962 if (bitmap != NO_BITMAP)
963 x_draw_bitmap (w, row, bitmap);
964
965 /* Decide which bitmap to draw at the right side. */
966 if (row->truncated_on_right_p)
967 bitmap = RIGHT_TRUNCATION_BITMAP;
968 else if (row->continued_p)
969 bitmap = CONTINUED_LINE_BITMAP;
970 else
971 bitmap = NO_BITMAP;
972
973 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
974 the flags area. */
975 if (bitmap == NO_BITMAP
110859fc 976 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
977 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
978 {
979 int right = window_box_right (w, -1);
980
045dee35
GM
981 if (header_line_height < 0)
982 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
983
984 /* In case the same realized face is used for bitmap areas and
985 for something displayed in the text (e.g. face `region' on
986 mono-displays, the fill style may have been changed to
987 FillSolid in x_draw_glyph_string_background. */
988 if (face->stipple)
989 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
990 else
991 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
06a2c219
GM
992 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
993 face->gc,
994 right,
045dee35 995 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 996 row->y)),
110859fc 997 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
06a2c219 998 row->visible_height);
dcd08bfb
GM
999 if (!face->stipple)
1000 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
1001 }
1002
1003 /* Draw the right bitmap. */
1004 if (bitmap != NO_BITMAP)
1005 x_draw_bitmap (w, row, bitmap);
1006}
1007
dc6f92b8 1008\f
06a2c219
GM
1009/***********************************************************************
1010 Line Highlighting
1011 ***********************************************************************/
dc6f92b8 1012
06a2c219
GM
1013/* External interface to control of standout mode. Not used for X
1014 frames. Aborts when called. */
1015
1016static void
dc6f92b8
JB
1017XTreassert_line_highlight (new, vpos)
1018 int new, vpos;
1019{
06a2c219 1020 abort ();
dc6f92b8
JB
1021}
1022
06a2c219
GM
1023
1024/* Call this when about to modify line at position VPOS and change
1025 whether it is highlighted. Not used for X frames. Aborts when
1026 called. */
dc6f92b8 1027
dfcf069d 1028static void
06a2c219
GM
1029x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1030 int new_highlight, vpos, y, first_unused_hpos;
dc6f92b8 1031{
06a2c219 1032 abort ();
dc6f92b8
JB
1033}
1034
06a2c219
GM
1035
1036/* This is called when starting Emacs and when restarting after
1037 suspend. When starting Emacs, no X window is mapped. And nothing
1038 must be done to Emacs's own window if it is suspended (though that
1039 rarely happens). */
dc6f92b8 1040
dfcf069d 1041static void
dc6f92b8
JB
1042XTset_terminal_modes ()
1043{
1044}
1045
06a2c219
GM
1046/* This is called when exiting or suspending Emacs. Exiting will make
1047 the X-windows go away, and suspending requires no action. */
dc6f92b8 1048
dfcf069d 1049static void
dc6f92b8
JB
1050XTreset_terminal_modes ()
1051{
dc6f92b8 1052}
06a2c219
GM
1053
1054
dc6f92b8 1055\f
06a2c219
GM
1056/***********************************************************************
1057 Output Cursor
1058 ***********************************************************************/
1059
1060/* Set the global variable output_cursor to CURSOR. All cursor
1061 positions are relative to updated_window. */
dc6f92b8 1062
dfcf069d 1063static void
06a2c219
GM
1064set_output_cursor (cursor)
1065 struct cursor_pos *cursor;
dc6f92b8 1066{
06a2c219
GM
1067 output_cursor.hpos = cursor->hpos;
1068 output_cursor.vpos = cursor->vpos;
1069 output_cursor.x = cursor->x;
1070 output_cursor.y = cursor->y;
1071}
1072
1073
1074/* Set a nominal cursor position.
dc6f92b8 1075
06a2c219
GM
1076 HPOS and VPOS are column/row positions in a window glyph matrix. X
1077 and Y are window text area relative pixel positions.
1078
1079 If this is done during an update, updated_window will contain the
1080 window that is being updated and the position is the future output
1081 cursor position for that window. If updated_window is null, use
1082 selected_window and display the cursor at the given position. */
1083
1084static void
1085XTcursor_to (vpos, hpos, y, x)
1086 int vpos, hpos, y, x;
1087{
1088 struct window *w;
1089
1090 /* If updated_window is not set, work on selected_window. */
1091 if (updated_window)
1092 w = updated_window;
1093 else
1094 w = XWINDOW (selected_window);
dbcb258a 1095
06a2c219
GM
1096 /* Set the output cursor. */
1097 output_cursor.hpos = hpos;
1098 output_cursor.vpos = vpos;
1099 output_cursor.x = x;
1100 output_cursor.y = y;
dc6f92b8 1101
06a2c219
GM
1102 /* If not called as part of an update, really display the cursor.
1103 This will also set the cursor position of W. */
1104 if (updated_window == NULL)
dc6f92b8
JB
1105 {
1106 BLOCK_INPUT;
06a2c219 1107 x_display_cursor (w, 1, hpos, vpos, x, y);
b86bd3dd 1108 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
dc6f92b8
JB
1109 UNBLOCK_INPUT;
1110 }
1111}
dc43ef94 1112
06a2c219
GM
1113
1114\f
1115/***********************************************************************
1116 Display Iterator
1117 ***********************************************************************/
1118
1119/* Function prototypes of this page. */
1120
1121static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1122 struct glyph *,
ee569018
KH
1123 XChar2b *,
1124 int *));
06a2c219
GM
1125static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1126 int, XChar2b *, int));
1127static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1128static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1129static void x_append_glyph P_ ((struct it *));
b4192550 1130static void x_append_composite_glyph P_ ((struct it *));
06a2c219
GM
1131static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1132 int, int, double));
1133static void x_produce_glyphs P_ ((struct it *));
06a2c219 1134static void x_produce_image_glyph P_ ((struct it *it));
ee569018
KH
1135
1136
1137/* Return a pointer to per-char metric information in FONT of a
1138 character pointed by B which is a pointer to an XChar2b. */
1139
1140#define PER_CHAR_METRIC(font, b) \
1141 ((font)->per_char \
1142 ? ((font)->per_char + (b)->byte2 - (font)->min_char_or_byte2 \
1143 + (((font)->min_byte1 || (font)->max_byte1) \
1144 ? (((b)->byte1 - (font)->min_byte1) \
1145 * ((font)->max_char_or_byte2 - (font)->min_char_or_byte2 + 1)) \
1146 : 0)) \
1147 : &((font)->max_bounds))
dc43ef94 1148
dc6f92b8 1149
e2ef8ee6
GM
1150/* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1151 is not contained in the font. */
dc43ef94 1152
06a2c219 1153static INLINE XCharStruct *
ee569018 1154x_per_char_metric (font, char2b)
06a2c219
GM
1155 XFontStruct *font;
1156 XChar2b *char2b;
1157{
1158 /* The result metric information. */
1159 XCharStruct *pcm = NULL;
dc6f92b8 1160
06a2c219 1161 xassert (font && char2b);
dc6f92b8 1162
06a2c219 1163 if (font->per_char != NULL)
dc6f92b8 1164 {
06a2c219 1165 if (font->min_byte1 == 0 && font->max_byte1 == 0)
dc43ef94 1166 {
06a2c219
GM
1167 /* min_char_or_byte2 specifies the linear character index
1168 corresponding to the first element of the per_char array,
1169 max_char_or_byte2 is the index of the last character. A
1170 character with non-zero CHAR2B->byte1 is not in the font.
1171 A character with byte2 less than min_char_or_byte2 or
1172 greater max_char_or_byte2 is not in the font. */
1173 if (char2b->byte1 == 0
1174 && char2b->byte2 >= font->min_char_or_byte2
1175 && char2b->byte2 <= font->max_char_or_byte2)
1176 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
dc43ef94 1177 }
06a2c219 1178 else
dc6f92b8 1179 {
06a2c219
GM
1180 /* If either min_byte1 or max_byte1 are nonzero, both
1181 min_char_or_byte2 and max_char_or_byte2 are less than
1182 256, and the 2-byte character index values corresponding
1183 to the per_char array element N (counting from 0) are:
1184
1185 byte1 = N/D + min_byte1
1186 byte2 = N\D + min_char_or_byte2
1187
1188 where:
1189
1190 D = max_char_or_byte2 - min_char_or_byte2 + 1
1191 / = integer division
1192 \ = integer modulus */
1193 if (char2b->byte1 >= font->min_byte1
1194 && char2b->byte1 <= font->max_byte1
1195 && char2b->byte2 >= font->min_char_or_byte2
1196 && char2b->byte2 <= font->max_char_or_byte2)
1197 {
1198 pcm = (font->per_char
1199 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1200 * (char2b->byte1 - font->min_byte1))
1201 + (char2b->byte2 - font->min_char_or_byte2));
1202 }
dc6f92b8 1203 }
06a2c219
GM
1204 }
1205 else
1206 {
1207 /* If the per_char pointer is null, all glyphs between the first
1208 and last character indexes inclusive have the same
1209 information, as given by both min_bounds and max_bounds. */
1210 if (char2b->byte2 >= font->min_char_or_byte2
1211 && char2b->byte2 <= font->max_char_or_byte2)
1212 pcm = &font->max_bounds;
1213 }
dc6f92b8 1214
ee569018 1215 return ((pcm == NULL
3e71d8f2 1216 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
ee569018 1217 ? NULL : pcm);
06a2c219 1218}
b73b6aaf 1219
57b03282 1220
06a2c219
GM
1221/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1222 the two-byte form of C. Encoding is returned in *CHAR2B. */
dc43ef94 1223
06a2c219
GM
1224static INLINE void
1225x_encode_char (c, char2b, font_info)
1226 int c;
1227 XChar2b *char2b;
1228 struct font_info *font_info;
1229{
1230 int charset = CHAR_CHARSET (c);
1231 XFontStruct *font = font_info->font;
1232
1233 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1234 This may be either a program in a special encoder language or a
1235 fixed encoding. */
1236 if (font_info->font_encoder)
1237 {
1238 /* It's a program. */
1239 struct ccl_program *ccl = font_info->font_encoder;
1240
1241 if (CHARSET_DIMENSION (charset) == 1)
1242 {
1243 ccl->reg[0] = charset;
1244 ccl->reg[1] = char2b->byte2;
1245 }
1246 else
1247 {
1248 ccl->reg[0] = charset;
1249 ccl->reg[1] = char2b->byte1;
1250 ccl->reg[2] = char2b->byte2;
1251 }
1252
1253 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1254
1255 /* We assume that MSBs are appropriately set/reset by CCL
1256 program. */
1257 if (font->max_byte1 == 0) /* 1-byte font */
ee569018 1258 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
06a2c219
GM
1259 else
1260 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1261 }
1262 else if (font_info->encoding[charset])
1263 {
1264 /* Fixed encoding scheme. See fontset.h for the meaning of the
1265 encoding numbers. */
1266 int enc = font_info->encoding[charset];
1267
1268 if ((enc == 1 || enc == 2)
1269 && CHARSET_DIMENSION (charset) == 2)
1270 char2b->byte1 |= 0x80;
1271
1272 if (enc == 1 || enc == 3)
1273 char2b->byte2 |= 0x80;
1274 }
1275}
1276
1277
1278/* Get face and two-byte form of character C in face FACE_ID on frame
1279 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1280 means we want to display multibyte text. Value is a pointer to a
1281 realized face that is ready for display. */
1282
1283static INLINE struct face *
1284x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1285 struct frame *f;
1286 int c, face_id;
1287 XChar2b *char2b;
1288 int multibyte_p;
1289{
1290 struct face *face = FACE_FROM_ID (f, face_id);
1291
1292 if (!multibyte_p)
1293 {
1294 /* Unibyte case. We don't have to encode, but we have to make
1295 sure to use a face suitable for unibyte. */
1296 char2b->byte1 = 0;
1297 char2b->byte2 = c;
ee569018
KH
1298 face_id = FACE_FOR_CHAR (f, face, c);
1299 face = FACE_FROM_ID (f, face_id);
06a2c219
GM
1300 }
1301 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1302 {
1303 /* Case of ASCII in a face known to fit ASCII. */
1304 char2b->byte1 = 0;
1305 char2b->byte2 = c;
1306 }
1307 else
1308 {
1309 int c1, c2, charset;
1310
1311 /* Split characters into bytes. If c2 is -1 afterwards, C is
1312 really a one-byte character so that byte1 is zero. */
1313 SPLIT_CHAR (c, charset, c1, c2);
1314 if (c2 > 0)
1315 char2b->byte1 = c1, char2b->byte2 = c2;
1316 else
1317 char2b->byte1 = 0, char2b->byte2 = c1;
1318
06a2c219 1319 /* Maybe encode the character in *CHAR2B. */
ee569018 1320 if (face->font != NULL)
06a2c219
GM
1321 {
1322 struct font_info *font_info
1323 = FONT_INFO_FROM_ID (f, face->font_info_id);
1324 if (font_info)
ee569018 1325 x_encode_char (c, char2b, font_info);
06a2c219
GM
1326 }
1327 }
1328
1329 /* Make sure X resources of the face are allocated. */
1330 xassert (face != NULL);
1331 PREPARE_FACE_FOR_DISPLAY (f, face);
1332
1333 return face;
1334}
1335
1336
1337/* Get face and two-byte form of character glyph GLYPH on frame F.
43d120d8 1338 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
06a2c219
GM
1339 a pointer to a realized face that is ready for display. */
1340
1341static INLINE struct face *
ee569018 1342x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
06a2c219
GM
1343 struct frame *f;
1344 struct glyph *glyph;
1345 XChar2b *char2b;
ee569018 1346 int *two_byte_p;
06a2c219
GM
1347{
1348 struct face *face;
1349
1350 xassert (glyph->type == CHAR_GLYPH);
43d120d8 1351 face = FACE_FROM_ID (f, glyph->face_id);
06a2c219 1352
ee569018
KH
1353 if (two_byte_p)
1354 *two_byte_p = 0;
1355
06a2c219
GM
1356 if (!glyph->multibyte_p)
1357 {
1358 /* Unibyte case. We don't have to encode, but we have to make
1359 sure to use a face suitable for unibyte. */
1360 char2b->byte1 = 0;
43d120d8 1361 char2b->byte2 = glyph->u.ch;
06a2c219 1362 }
43d120d8
KH
1363 else if (glyph->u.ch < 128
1364 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
06a2c219
GM
1365 {
1366 /* Case of ASCII in a face known to fit ASCII. */
1367 char2b->byte1 = 0;
43d120d8 1368 char2b->byte2 = glyph->u.ch;
06a2c219
GM
1369 }
1370 else
1371 {
1372 int c1, c2, charset;
1373
1374 /* Split characters into bytes. If c2 is -1 afterwards, C is
1375 really a one-byte character so that byte1 is zero. */
43d120d8 1376 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
06a2c219
GM
1377 if (c2 > 0)
1378 char2b->byte1 = c1, char2b->byte2 = c2;
1379 else
1380 char2b->byte1 = 0, char2b->byte2 = c1;
1381
1382 /* Maybe encode the character in *CHAR2B. */
1383 if (charset != CHARSET_ASCII)
1384 {
1385 struct font_info *font_info
1386 = FONT_INFO_FROM_ID (f, face->font_info_id);
1387 if (font_info)
1388 {
43d120d8 1389 x_encode_char (glyph->u.ch, char2b, font_info);
ee569018
KH
1390 if (two_byte_p)
1391 *two_byte_p
1392 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
06a2c219
GM
1393 }
1394 }
1395 }
1396
1397 /* Make sure X resources of the face are allocated. */
1398 xassert (face != NULL);
1399 PREPARE_FACE_FOR_DISPLAY (f, face);
1400 return face;
1401}
1402
1403
1404/* Store one glyph for IT->char_to_display in IT->glyph_row.
1405 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1406
1407static INLINE void
1408x_append_glyph (it)
1409 struct it *it;
1410{
1411 struct glyph *glyph;
1412 enum glyph_row_area area = it->area;
1413
1414 xassert (it->glyph_row);
1415 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1416
1417 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1418 if (glyph < it->glyph_row->glyphs[area + 1])
1419 {
06a2c219
GM
1420 glyph->charpos = CHARPOS (it->position);
1421 glyph->object = it->object;
88d75730 1422 glyph->pixel_width = it->pixel_width;
06a2c219 1423 glyph->voffset = it->voffset;
88d75730 1424 glyph->type = CHAR_GLYPH;
06a2c219 1425 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1426 glyph->left_box_line_p = it->start_of_box_run_p;
1427 glyph->right_box_line_p = it->end_of_box_run_p;
66ac4b0e
GM
1428 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1429 || it->phys_descent > it->descent);
88d75730 1430 glyph->padding_p = 0;
ee569018 1431 glyph->glyph_not_available_p = it->glyph_not_available_p;
88d75730
GM
1432 glyph->face_id = it->face_id;
1433 glyph->u.ch = it->char_to_display;
06a2c219
GM
1434 ++it->glyph_row->used[area];
1435 }
1436}
1437
b4192550
KH
1438/* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1439 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1440
1441static INLINE void
1442x_append_composite_glyph (it)
1443 struct it *it;
1444{
1445 struct glyph *glyph;
1446 enum glyph_row_area area = it->area;
1447
1448 xassert (it->glyph_row);
1449
1450 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1451 if (glyph < it->glyph_row->glyphs[area + 1])
1452 {
b4192550
KH
1453 glyph->charpos = CHARPOS (it->position);
1454 glyph->object = it->object;
88d75730 1455 glyph->pixel_width = it->pixel_width;
b4192550 1456 glyph->voffset = it->voffset;
88d75730 1457 glyph->type = COMPOSITE_GLYPH;
b4192550 1458 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1459 glyph->left_box_line_p = it->start_of_box_run_p;
1460 glyph->right_box_line_p = it->end_of_box_run_p;
b4192550
KH
1461 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1462 || it->phys_descent > it->descent);
88d75730
GM
1463 glyph->padding_p = 0;
1464 glyph->glyph_not_available_p = 0;
1465 glyph->face_id = it->face_id;
1466 glyph->u.cmp_id = it->cmp_id;
b4192550
KH
1467 ++it->glyph_row->used[area];
1468 }
1469}
1470
06a2c219
GM
1471
1472/* Change IT->ascent and IT->height according to the setting of
1473 IT->voffset. */
1474
1475static INLINE void
1476take_vertical_position_into_account (it)
1477 struct it *it;
1478{
1479 if (it->voffset)
1480 {
1481 if (it->voffset < 0)
1482 /* Increase the ascent so that we can display the text higher
1483 in the line. */
1484 it->ascent += abs (it->voffset);
1485 else
1486 /* Increase the descent so that we can display the text lower
1487 in the line. */
1488 it->descent += it->voffset;
1489 }
1490}
1491
1492
1493/* Produce glyphs/get display metrics for the image IT is loaded with.
1494 See the description of struct display_iterator in dispextern.h for
1495 an overview of struct display_iterator. */
1496
1497static void
1498x_produce_image_glyph (it)
1499 struct it *it;
1500{
1501 struct image *img;
1502 struct face *face;
1503
1504 xassert (it->what == IT_IMAGE);
1505
1506 face = FACE_FROM_ID (it->f, it->face_id);
1507 img = IMAGE_FROM_ID (it->f, it->image_id);
1508 xassert (img);
1509
1510 /* Make sure X resources of the face and image are loaded. */
1511 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1512 prepare_image_for_display (it->f, img);
1513
95af8492 1514 it->ascent = it->phys_ascent = image_ascent (img, face);
66ac4b0e 1515 it->descent = it->phys_descent = img->height + 2 * img->margin - it->ascent;
06a2c219
GM
1516 it->pixel_width = img->width + 2 * img->margin;
1517
1518 it->nglyphs = 1;
1519
1520 if (face->box != FACE_NO_BOX)
1521 {
1522 it->ascent += face->box_line_width;
1523 it->descent += face->box_line_width;
1524
1525 if (it->start_of_box_run_p)
1526 it->pixel_width += face->box_line_width;
1527 if (it->end_of_box_run_p)
1528 it->pixel_width += face->box_line_width;
1529 }
1530
1531 take_vertical_position_into_account (it);
1532
1533 if (it->glyph_row)
1534 {
1535 struct glyph *glyph;
1536 enum glyph_row_area area = it->area;
1537
1538 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1539 if (glyph < it->glyph_row->glyphs[area + 1])
1540 {
06a2c219
GM
1541 glyph->charpos = CHARPOS (it->position);
1542 glyph->object = it->object;
88d75730 1543 glyph->pixel_width = it->pixel_width;
06a2c219 1544 glyph->voffset = it->voffset;
88d75730 1545 glyph->type = IMAGE_GLYPH;
06a2c219 1546 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1547 glyph->left_box_line_p = it->start_of_box_run_p;
1548 glyph->right_box_line_p = it->end_of_box_run_p;
1549 glyph->overlaps_vertically_p = 0;
1550 glyph->padding_p = 0;
1551 glyph->glyph_not_available_p = 0;
1552 glyph->face_id = it->face_id;
1553 glyph->u.img_id = img->id;
06a2c219
GM
1554 ++it->glyph_row->used[area];
1555 }
1556 }
1557}
1558
1559
1560/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1561 of the glyph, WIDTH and HEIGHT are the width and height of the
1562 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1563 ascent of the glyph (0 <= ASCENT <= 1). */
1564
1565static void
1566x_append_stretch_glyph (it, object, width, height, ascent)
1567 struct it *it;
1568 Lisp_Object object;
1569 int width, height;
1570 double ascent;
1571{
1572 struct glyph *glyph;
1573 enum glyph_row_area area = it->area;
1574
1575 xassert (ascent >= 0 && ascent <= 1);
1576
1577 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1578 if (glyph < it->glyph_row->glyphs[area + 1])
1579 {
06a2c219
GM
1580 glyph->charpos = CHARPOS (it->position);
1581 glyph->object = object;
88d75730 1582 glyph->pixel_width = width;
06a2c219 1583 glyph->voffset = it->voffset;
88d75730 1584 glyph->type = STRETCH_GLYPH;
06a2c219 1585 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1586 glyph->left_box_line_p = it->start_of_box_run_p;
1587 glyph->right_box_line_p = it->end_of_box_run_p;
1588 glyph->overlaps_vertically_p = 0;
1589 glyph->padding_p = 0;
1590 glyph->glyph_not_available_p = 0;
1591 glyph->face_id = it->face_id;
1592 glyph->u.stretch.ascent = height * ascent;
1593 glyph->u.stretch.height = height;
06a2c219
GM
1594 ++it->glyph_row->used[area];
1595 }
1596}
1597
1598
1599/* Produce a stretch glyph for iterator IT. IT->object is the value
1600 of the glyph property displayed. The value must be a list
1601 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1602 being recognized:
1603
1604 1. `:width WIDTH' specifies that the space should be WIDTH *
1605 canonical char width wide. WIDTH may be an integer or floating
1606 point number.
1607
1608 2. `:relative-width FACTOR' specifies that the width of the stretch
1609 should be computed from the width of the first character having the
1610 `glyph' property, and should be FACTOR times that width.
1611
1612 3. `:align-to HPOS' specifies that the space should be wide enough
1613 to reach HPOS, a value in canonical character units.
1614
1615 Exactly one of the above pairs must be present.
1616
1617 4. `:height HEIGHT' specifies that the height of the stretch produced
1618 should be HEIGHT, measured in canonical character units.
1619
1620 5. `:relative-height FACTOR' specifies that the height of the the
1621 stretch should be FACTOR times the height of the characters having
1622 the glyph property.
1623
1624 Either none or exactly one of 4 or 5 must be present.
1625
1626 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1627 of the stretch should be used for the ascent of the stretch.
1628 ASCENT must be in the range 0 <= ASCENT <= 100. */
1629
1630#define NUMVAL(X) \
1631 ((INTEGERP (X) || FLOATP (X)) \
1632 ? XFLOATINT (X) \
1633 : - 1)
1634
1635
1636static void
1637x_produce_stretch_glyph (it)
1638 struct it *it;
1639{
1640 /* (space :width WIDTH :height HEIGHT. */
3e71d8f2
GM
1641#if GLYPH_DEBUG
1642 extern Lisp_Object Qspace;
1643#endif
1644 extern Lisp_Object QCwidth, QCheight, QCascent;
06a2c219
GM
1645 extern Lisp_Object QCrelative_width, QCrelative_height;
1646 extern Lisp_Object QCalign_to;
1647 Lisp_Object prop, plist;
1648 double width = 0, height = 0, ascent = 0;
1649 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1650 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1651
1652 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1653
1654 /* List should start with `space'. */
1655 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1656 plist = XCDR (it->object);
1657
1658 /* Compute the width of the stretch. */
1659 if (prop = Fplist_get (plist, QCwidth),
1660 NUMVAL (prop) > 0)
1661 /* Absolute width `:width WIDTH' specified and valid. */
1662 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1663 else if (prop = Fplist_get (plist, QCrelative_width),
1664 NUMVAL (prop) > 0)
1665 {
1666 /* Relative width `:relative-width FACTOR' specified and valid.
1667 Compute the width of the characters having the `glyph'
1668 property. */
1669 struct it it2;
1670 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1671
1672 it2 = *it;
1673 if (it->multibyte_p)
1674 {
1675 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1676 - IT_BYTEPOS (*it));
1677 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1678 }
1679 else
1680 it2.c = *p, it2.len = 1;
1681
1682 it2.glyph_row = NULL;
1683 it2.what = IT_CHARACTER;
1684 x_produce_glyphs (&it2);
1685 width = NUMVAL (prop) * it2.pixel_width;
1686 }
1687 else if (prop = Fplist_get (plist, QCalign_to),
1688 NUMVAL (prop) > 0)
1689 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1690 else
1691 /* Nothing specified -> width defaults to canonical char width. */
1692 width = CANON_X_UNIT (it->f);
1693
1694 /* Compute height. */
1695 if (prop = Fplist_get (plist, QCheight),
1696 NUMVAL (prop) > 0)
1697 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1698 else if (prop = Fplist_get (plist, QCrelative_height),
1699 NUMVAL (prop) > 0)
1700 height = FONT_HEIGHT (font) * NUMVAL (prop);
1701 else
1702 height = FONT_HEIGHT (font);
1703
1704 /* Compute percentage of height used for ascent. If
1705 `:ascent ASCENT' is present and valid, use that. Otherwise,
1706 derive the ascent from the font in use. */
1707 if (prop = Fplist_get (plist, QCascent),
1708 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1709 ascent = NUMVAL (prop) / 100.0;
1710 else
1711 ascent = (double) font->ascent / FONT_HEIGHT (font);
1712
1713 if (width <= 0)
1714 width = 1;
1715 if (height <= 0)
1716 height = 1;
1717
1718 if (it->glyph_row)
1719 {
1720 Lisp_Object object = it->stack[it->sp - 1].string;
1721 if (!STRINGP (object))
1722 object = it->w->buffer;
1723 x_append_stretch_glyph (it, object, width, height, ascent);
1724 }
1725
1726 it->pixel_width = width;
66ac4b0e
GM
1727 it->ascent = it->phys_ascent = height * ascent;
1728 it->descent = it->phys_descent = height - it->ascent;
06a2c219
GM
1729 it->nglyphs = 1;
1730
1731 if (face->box != FACE_NO_BOX)
1732 {
1733 it->ascent += face->box_line_width;
1734 it->descent += face->box_line_width;
1735
1736 if (it->start_of_box_run_p)
1737 it->pixel_width += face->box_line_width;
1738 if (it->end_of_box_run_p)
1739 it->pixel_width += face->box_line_width;
1740 }
1741
1742 take_vertical_position_into_account (it);
1743}
1744
b4192550
KH
1745/* Return proper value to be used as baseline offset of font that has
1746 ASCENT and DESCENT to draw characters by the font at the vertical
1747 center of the line of frame F.
1748
1749 Here, out task is to find the value of BOFF in the following figure;
1750
1751 -------------------------+-----------+-
1752 -+-+---------+-+ | |
1753 | | | | | |
1754 | | | | F_ASCENT F_HEIGHT
1755 | | | ASCENT | |
1756 HEIGHT | | | | |
1757 | | |-|-+------+-----------|------- baseline
1758 | | | | BOFF | |
1759 | |---------|-+-+ | |
1760 | | | DESCENT | |
1761 -+-+---------+-+ F_DESCENT |
1762 -------------------------+-----------+-
1763
1764 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1765 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1766 DESCENT = FONT->descent
1767 HEIGHT = FONT_HEIGHT (FONT)
1768 F_DESCENT = (F->output_data.x->font->descent
1769 - F->output_data.x->baseline_offset)
1770 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1771*/
1772
1773#define VCENTER_BASELINE_OFFSET(FONT, F) \
1774 ((FONT)->descent \
1775 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT))) / 2 \
1776 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
06a2c219
GM
1777
1778/* Produce glyphs/get display metrics for the display element IT is
1779 loaded with. See the description of struct display_iterator in
1780 dispextern.h for an overview of struct display_iterator. */
1781
1782static void
1783x_produce_glyphs (it)
1784 struct it *it;
1785{
ee569018
KH
1786 it->glyph_not_available_p = 0;
1787
06a2c219
GM
1788 if (it->what == IT_CHARACTER)
1789 {
1790 XChar2b char2b;
1791 XFontStruct *font;
ee569018 1792 struct face *face = FACE_FROM_ID (it->f, it->face_id);
06a2c219 1793 XCharStruct *pcm;
06a2c219 1794 int font_not_found_p;
b4192550
KH
1795 struct font_info *font_info;
1796 int boff; /* baseline offset */
06a2c219 1797
ee569018
KH
1798 /* Maybe translate single-byte characters to multibyte, or the
1799 other way. */
06a2c219 1800 it->char_to_display = it->c;
ee569018 1801 if (!ASCII_BYTE_P (it->c))
06a2c219 1802 {
ee569018
KH
1803 if (unibyte_display_via_language_environment
1804 && SINGLE_BYTE_CHAR_P (it->c)
1805 && (it->c >= 0240
1806 || !NILP (Vnonascii_translation_table)))
1807 {
1808 it->char_to_display = unibyte_char_to_multibyte (it->c);
1809 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1810 face = FACE_FROM_ID (it->f, it->face_id);
1811 }
1812 else if (!SINGLE_BYTE_CHAR_P (it->c)
1813 && !it->multibyte_p)
1814 {
1815 it->char_to_display = multibyte_char_to_unibyte (it->c, Qnil);
1816 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1817 face = FACE_FROM_ID (it->f, it->face_id);
1818 }
06a2c219
GM
1819 }
1820
ee569018
KH
1821 /* Get font to use. Encode IT->char_to_display. */
1822 x_get_char_face_and_encoding (it->f, it->char_to_display,
1823 it->face_id, &char2b,
1824 it->multibyte_p);
06a2c219
GM
1825 font = face->font;
1826
1827 /* When no suitable font found, use the default font. */
1828 font_not_found_p = font == NULL;
1829 if (font_not_found_p)
b4192550
KH
1830 {
1831 font = FRAME_FONT (it->f);
1832 boff = it->f->output_data.x->baseline_offset;
1833 font_info = NULL;
1834 }
1835 else
1836 {
1837 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1838 boff = font_info->baseline_offset;
1839 if (font_info->vertical_centering)
1840 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1841 }
06a2c219
GM
1842
1843 if (it->char_to_display >= ' '
1844 && (!it->multibyte_p || it->char_to_display < 128))
1845 {
1846 /* Either unibyte or ASCII. */
1847 int stretched_p;
1848
1849 it->nglyphs = 1;
06a2c219
GM
1850
1851 pcm = x_per_char_metric (font, &char2b);
b4192550
KH
1852 it->ascent = font->ascent + boff;
1853 it->descent = font->descent - boff;
474848ac
GM
1854
1855 if (pcm)
1856 {
1857 it->phys_ascent = pcm->ascent + boff;
1858 it->phys_descent = pcm->descent - boff;
1859 it->pixel_width = pcm->width;
1860 }
1861 else
1862 {
1863 it->glyph_not_available_p = 1;
1864 it->phys_ascent = font->ascent + boff;
1865 it->phys_descent = font->descent - boff;
1866 it->pixel_width = FONT_WIDTH (font);
1867 }
06a2c219
GM
1868
1869 /* If this is a space inside a region of text with
1870 `space-width' property, change its width. */
1871 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1872 if (stretched_p)
1873 it->pixel_width *= XFLOATINT (it->space_width);
1874
1875 /* If face has a box, add the box thickness to the character
1876 height. If character has a box line to the left and/or
1877 right, add the box line width to the character's width. */
1878 if (face->box != FACE_NO_BOX)
1879 {
1880 int thick = face->box_line_width;
1881
1882 it->ascent += thick;
1883 it->descent += thick;
1884
1885 if (it->start_of_box_run_p)
1886 it->pixel_width += thick;
1887 if (it->end_of_box_run_p)
1888 it->pixel_width += thick;
1889 }
1890
1891 /* If face has an overline, add the height of the overline
1892 (1 pixel) and a 1 pixel margin to the character height. */
1893 if (face->overline_p)
1894 it->ascent += 2;
1895
1896 take_vertical_position_into_account (it);
1897
1898 /* If we have to actually produce glyphs, do it. */
1899 if (it->glyph_row)
1900 {
1901 if (stretched_p)
1902 {
1903 /* Translate a space with a `space-width' property
1904 into a stretch glyph. */
1905 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1906 x_append_stretch_glyph (it, it->object, it->pixel_width,
1907 it->ascent + it->descent, ascent);
1908 }
1909 else
1910 x_append_glyph (it);
1911
1912 /* If characters with lbearing or rbearing are displayed
1913 in this line, record that fact in a flag of the
1914 glyph row. This is used to optimize X output code. */
1c7e22fd 1915 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
06a2c219
GM
1916 it->glyph_row->contains_overlapping_glyphs_p = 1;
1917 }
1918 }
1919 else if (it->char_to_display == '\n')
1920 {
1921 /* A newline has no width but we need the height of the line. */
1922 it->pixel_width = 0;
1923 it->nglyphs = 0;
b4192550
KH
1924 it->ascent = it->phys_ascent = font->ascent + boff;
1925 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1926
1927 if (face->box != FACE_NO_BOX)
1928 {
1929 int thick = face->box_line_width;
1930 it->ascent += thick;
1931 it->descent += thick;
1932 }
1933 }
1934 else if (it->char_to_display == '\t')
1935 {
1936 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
d365f5bb 1937 int x = it->current_x + it->continuation_lines_width;
06a2c219
GM
1938 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1939
1940 it->pixel_width = next_tab_x - x;
1941 it->nglyphs = 1;
b4192550
KH
1942 it->ascent = it->phys_ascent = font->ascent + boff;
1943 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1944
1945 if (it->glyph_row)
1946 {
1947 double ascent = (double) it->ascent / (it->ascent + it->descent);
1948 x_append_stretch_glyph (it, it->object, it->pixel_width,
1949 it->ascent + it->descent, ascent);
1950 }
1951 }
1952 else
1953 {
1954 /* A multi-byte character. Assume that the display width of the
1955 character is the width of the character multiplied by the
b4192550 1956 width of the font. */
06a2c219 1957
b4192550
KH
1958 /* If we found a font, this font should give us the right
1959 metrics. If we didn't find a font, use the frame's
1960 default font and calculate the width of the character
1961 from the charset width; this is what old redisplay code
1962 did. */
1963 pcm = x_per_char_metric (font, &char2b);
ee569018
KH
1964 if (font_not_found_p || !pcm)
1965 {
1966 int charset = CHAR_CHARSET (it->char_to_display);
1967
1968 it->glyph_not_available_p = 1;
1969 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
1970 * CHARSET_WIDTH (charset));
1971 it->phys_ascent = font->ascent + boff;
1972 it->phys_descent = font->descent - boff;
1973 }
1974 else
1975 {
1976 it->pixel_width = pcm->width;
1977 it->phys_ascent = pcm->ascent + boff;
1978 it->phys_descent = pcm->descent - boff;
1979 if (it->glyph_row
1980 && (pcm->lbearing < 0
1981 || pcm->rbearing > pcm->width))
1982 it->glyph_row->contains_overlapping_glyphs_p = 1;
1983 }
b4192550
KH
1984 it->nglyphs = 1;
1985 it->ascent = font->ascent + boff;
1986 it->descent = font->descent - boff;
06a2c219
GM
1987 if (face->box != FACE_NO_BOX)
1988 {
1989 int thick = face->box_line_width;
1990 it->ascent += thick;
1991 it->descent += thick;
1992
1993 if (it->start_of_box_run_p)
1994 it->pixel_width += thick;
1995 if (it->end_of_box_run_p)
1996 it->pixel_width += thick;
1997 }
1998
1999 /* If face has an overline, add the height of the overline
2000 (1 pixel) and a 1 pixel margin to the character height. */
2001 if (face->overline_p)
2002 it->ascent += 2;
2003
2004 take_vertical_position_into_account (it);
2005
2006 if (it->glyph_row)
2007 x_append_glyph (it);
2008 }
2009 }
b4192550
KH
2010 else if (it->what == IT_COMPOSITION)
2011 {
2012 /* Note: A composition is represented as one glyph in the
2013 glyph matrix. There are no padding glyphs. */
2014 XChar2b char2b;
2015 XFontStruct *font;
ee569018 2016 struct face *face = FACE_FROM_ID (it->f, it->face_id);
b4192550
KH
2017 XCharStruct *pcm;
2018 int font_not_found_p;
2019 struct font_info *font_info;
2020 int boff; /* baseline offset */
2021 struct composition *cmp = composition_table[it->cmp_id];
2022
2023 /* Maybe translate single-byte characters to multibyte. */
2024 it->char_to_display = it->c;
2025 if (unibyte_display_via_language_environment
2026 && SINGLE_BYTE_CHAR_P (it->c)
2027 && (it->c >= 0240
2028 || (it->c >= 0200
2029 && !NILP (Vnonascii_translation_table))))
2030 {
2031 it->char_to_display = unibyte_char_to_multibyte (it->c);
b4192550
KH
2032 }
2033
2034 /* Get face and font to use. Encode IT->char_to_display. */
ee569018
KH
2035 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2036 face = FACE_FROM_ID (it->f, it->face_id);
2037 x_get_char_face_and_encoding (it->f, it->char_to_display,
2038 it->face_id, &char2b, it->multibyte_p);
b4192550
KH
2039 font = face->font;
2040
2041 /* When no suitable font found, use the default font. */
2042 font_not_found_p = font == NULL;
2043 if (font_not_found_p)
2044 {
2045 font = FRAME_FONT (it->f);
2046 boff = it->f->output_data.x->baseline_offset;
2047 font_info = NULL;
2048 }
2049 else
2050 {
2051 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2052 boff = font_info->baseline_offset;
2053 if (font_info->vertical_centering)
2054 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2055 }
2056
2057 /* There are no padding glyphs, so there is only one glyph to
2058 produce for the composition. Important is that pixel_width,
2059 ascent and descent are the values of what is drawn by
2060 draw_glyphs (i.e. the values of the overall glyphs composed). */
2061 it->nglyphs = 1;
2062
2063 /* If we have not yet calculated pixel size data of glyphs of
2064 the composition for the current face font, calculate them
2065 now. Theoretically, we have to check all fonts for the
2066 glyphs, but that requires much time and memory space. So,
2067 here we check only the font of the first glyph. This leads
2068 to incorrect display very rarely, and C-l (recenter) can
2069 correct the display anyway. */
2070 if (cmp->font != (void *) font)
2071 {
2072 /* Ascent and descent of the font of the first character of
2073 this composition (adjusted by baseline offset). Ascent
2074 and descent of overall glyphs should not be less than
2075 them respectively. */
2076 int font_ascent = font->ascent + boff;
2077 int font_descent = font->descent - boff;
2078 /* Bounding box of the overall glyphs. */
2079 int leftmost, rightmost, lowest, highest;
329bed06 2080 int i, width, ascent, descent;
b4192550
KH
2081
2082 cmp->font = (void *) font;
2083
2084 /* Initialize the bounding box. */
2085 pcm = x_per_char_metric (font, &char2b);
329bed06
GM
2086 if (pcm)
2087 {
2088 width = pcm->width;
2089 ascent = pcm->ascent;
2090 descent = pcm->descent;
2091 }
2092 else
2093 {
2094 width = FONT_WIDTH (font);
2095 ascent = font->ascent;
2096 descent = font->descent;
2097 }
2098
2099 rightmost = width;
2100 lowest = - descent + boff;
2101 highest = ascent + boff;
b4192550 2102 leftmost = 0;
329bed06 2103
b4192550
KH
2104 if (font_info
2105 && font_info->default_ascent
2106 && CHAR_TABLE_P (Vuse_default_ascent)
2107 && !NILP (Faref (Vuse_default_ascent,
2108 make_number (it->char_to_display))))
2109 highest = font_info->default_ascent + boff;
2110
2111 /* Draw the first glyph at the normal position. It may be
2112 shifted to right later if some other glyphs are drawn at
2113 the left. */
2114 cmp->offsets[0] = 0;
2115 cmp->offsets[1] = boff;
2116
2117 /* Set cmp->offsets for the remaining glyphs. */
2118 for (i = 1; i < cmp->glyph_len; i++)
2119 {
2120 int left, right, btm, top;
2121 int ch = COMPOSITION_GLYPH (cmp, i);
ee569018
KH
2122 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2123
2124 face = FACE_FROM_ID (it->f, face_id);
2125 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2126 it->multibyte_p);
b4192550
KH
2127 font = face->font;
2128 if (font == NULL)
2129 {
2130 font = FRAME_FONT (it->f);
2131 boff = it->f->output_data.x->baseline_offset;
2132 font_info = NULL;
2133 }
2134 else
2135 {
2136 font_info
2137 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2138 boff = font_info->baseline_offset;
2139 if (font_info->vertical_centering)
2140 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2141 }
2142
2143 pcm = x_per_char_metric (font, &char2b);
329bed06
GM
2144 if (pcm)
2145 {
2146 width = pcm->width;
2147 ascent = pcm->ascent;
2148 descent = pcm->descent;
2149 }
2150 else
2151 {
2152 width = FONT_WIDTH (font);
2153 ascent = font->ascent;
2154 descent = font->descent;
2155 }
b4192550
KH
2156
2157 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2158 {
2159 /* Relative composition with or without
2160 alternate chars. */
329bed06
GM
2161 left = (leftmost + rightmost - width) / 2;
2162 btm = - descent + boff;
b4192550
KH
2163 if (font_info && font_info->relative_compose
2164 && (! CHAR_TABLE_P (Vignore_relative_composition)
2165 || NILP (Faref (Vignore_relative_composition,
2166 make_number (ch)))))
2167 {
2168
329bed06 2169 if (- descent >= font_info->relative_compose)
b4192550
KH
2170 /* One extra pixel between two glyphs. */
2171 btm = highest + 1;
329bed06 2172 else if (ascent <= 0)
b4192550 2173 /* One extra pixel between two glyphs. */
329bed06 2174 btm = lowest - 1 - ascent - descent;
b4192550
KH
2175 }
2176 }
2177 else
2178 {
2179 /* A composition rule is specified by an integer
2180 value that encodes global and new reference
2181 points (GREF and NREF). GREF and NREF are
2182 specified by numbers as below:
2183
2184 0---1---2 -- ascent
2185 | |
2186 | |
2187 | |
2188 9--10--11 -- center
2189 | |
2190 ---3---4---5--- baseline
2191 | |
2192 6---7---8 -- descent
2193 */
2194 int rule = COMPOSITION_RULE (cmp, i);
2195 int gref, nref, grefx, grefy, nrefx, nrefy;
2196
2197 COMPOSITION_DECODE_RULE (rule, gref, nref);
2198 grefx = gref % 3, nrefx = nref % 3;
2199 grefy = gref / 3, nrefy = nref / 3;
2200
2201 left = (leftmost
2202 + grefx * (rightmost - leftmost) / 2
329bed06 2203 - nrefx * width / 2);
b4192550
KH
2204 btm = ((grefy == 0 ? highest
2205 : grefy == 1 ? 0
2206 : grefy == 2 ? lowest
2207 : (highest + lowest) / 2)
329bed06
GM
2208 - (nrefy == 0 ? ascent + descent
2209 : nrefy == 1 ? descent - boff
b4192550 2210 : nrefy == 2 ? 0
329bed06 2211 : (ascent + descent) / 2));
b4192550
KH
2212 }
2213
2214 cmp->offsets[i * 2] = left;
329bed06 2215 cmp->offsets[i * 2 + 1] = btm + descent;
b4192550
KH
2216
2217 /* Update the bounding box of the overall glyphs. */
329bed06
GM
2218 right = left + width;
2219 top = btm + descent + ascent;
b4192550
KH
2220 if (left < leftmost)
2221 leftmost = left;
2222 if (right > rightmost)
2223 rightmost = right;
2224 if (top > highest)
2225 highest = top;
2226 if (btm < lowest)
2227 lowest = btm;
2228 }
2229
2230 /* If there are glyphs whose x-offsets are negative,
2231 shift all glyphs to the right and make all x-offsets
2232 non-negative. */
2233 if (leftmost < 0)
2234 {
2235 for (i = 0; i < cmp->glyph_len; i++)
2236 cmp->offsets[i * 2] -= leftmost;
2237 rightmost -= leftmost;
2238 }
2239
2240 cmp->pixel_width = rightmost;
2241 cmp->ascent = highest;
2242 cmp->descent = - lowest;
2243 if (cmp->ascent < font_ascent)
2244 cmp->ascent = font_ascent;
2245 if (cmp->descent < font_descent)
2246 cmp->descent = font_descent;
2247 }
2248
2249 it->pixel_width = cmp->pixel_width;
2250 it->ascent = it->phys_ascent = cmp->ascent;
2251 it->descent = it->phys_descent = cmp->descent;
2252
2253 if (face->box != FACE_NO_BOX)
2254 {
2255 int thick = face->box_line_width;
2256 it->ascent += thick;
2257 it->descent += thick;
2258
2259 if (it->start_of_box_run_p)
2260 it->pixel_width += thick;
2261 if (it->end_of_box_run_p)
2262 it->pixel_width += thick;
2263 }
2264
2265 /* If face has an overline, add the height of the overline
2266 (1 pixel) and a 1 pixel margin to the character height. */
2267 if (face->overline_p)
2268 it->ascent += 2;
2269
2270 take_vertical_position_into_account (it);
2271
2272 if (it->glyph_row)
2273 x_append_composite_glyph (it);
2274 }
06a2c219
GM
2275 else if (it->what == IT_IMAGE)
2276 x_produce_image_glyph (it);
2277 else if (it->what == IT_STRETCH)
2278 x_produce_stretch_glyph (it);
2279
3017fdd1
GM
2280 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2281 because this isn't true for images with `:ascent 100'. */
2282 xassert (it->ascent >= 0 && it->descent >= 0);
06a2c219
GM
2283 if (it->area == TEXT_AREA)
2284 it->current_x += it->pixel_width;
66ac4b0e 2285
d365f5bb
GM
2286 it->descent += it->extra_line_spacing;
2287
06a2c219
GM
2288 it->max_ascent = max (it->max_ascent, it->ascent);
2289 it->max_descent = max (it->max_descent, it->descent);
66ac4b0e
GM
2290 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2291 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
06a2c219
GM
2292}
2293
2294
2295/* Estimate the pixel height of the mode or top line on frame F.
2296 FACE_ID specifies what line's height to estimate. */
2297
2298int
2299x_estimate_mode_line_height (f, face_id)
2300 struct frame *f;
2301 enum face_id face_id;
2302{
2303 int height = 1;
2304
2305 /* This function is called so early when Emacs starts that the face
2306 cache and mode line face are not yet initialized. */
2307 if (FRAME_FACE_CACHE (f))
2308 {
2309 struct face *face = FACE_FROM_ID (f, face_id);
2310 if (face)
2311 height = FONT_HEIGHT (face->font) + 2 * face->box_line_width;
2312 }
2313
2314 return height;
2315}
2316
2317\f
2318/***********************************************************************
2319 Glyph display
2320 ***********************************************************************/
2321
2322/* A sequence of glyphs to be drawn in the same face.
2323
2324 This data structure is not really completely X specific, so it
2325 could possibly, at least partially, be useful for other systems. It
2326 is currently not part of the external redisplay interface because
2327 it's not clear what other systems will need. */
2328
2329struct glyph_string
2330{
2331 /* X-origin of the string. */
2332 int x;
2333
2334 /* Y-origin and y-position of the base line of this string. */
2335 int y, ybase;
2336
2337 /* The width of the string, not including a face extension. */
2338 int width;
2339
2340 /* The width of the string, including a face extension. */
2341 int background_width;
2342
2343 /* The height of this string. This is the height of the line this
2344 string is drawn in, and can be different from the height of the
2345 font the string is drawn in. */
2346 int height;
2347
2348 /* Number of pixels this string overwrites in front of its x-origin.
2349 This number is zero if the string has an lbearing >= 0; it is
2350 -lbearing, if the string has an lbearing < 0. */
2351 int left_overhang;
2352
2353 /* Number of pixels this string overwrites past its right-most
2354 nominal x-position, i.e. x + width. Zero if the string's
2355 rbearing is <= its nominal width, rbearing - width otherwise. */
2356 int right_overhang;
2357
2358 /* The frame on which the glyph string is drawn. */
2359 struct frame *f;
2360
2361 /* The window on which the glyph string is drawn. */
2362 struct window *w;
2363
2364 /* X display and window for convenience. */
2365 Display *display;
2366 Window window;
2367
2368 /* The glyph row for which this string was built. It determines the
2369 y-origin and height of the string. */
2370 struct glyph_row *row;
2371
2372 /* The area within row. */
2373 enum glyph_row_area area;
2374
2375 /* Characters to be drawn, and number of characters. */
2376 XChar2b *char2b;
2377 int nchars;
2378
06a2c219
GM
2379 /* A face-override for drawing cursors, mouse face and similar. */
2380 enum draw_glyphs_face hl;
2381
2382 /* Face in which this string is to be drawn. */
2383 struct face *face;
2384
2385 /* Font in which this string is to be drawn. */
2386 XFontStruct *font;
2387
2388 /* Font info for this string. */
2389 struct font_info *font_info;
2390
b4192550
KH
2391 /* Non-null means this string describes (part of) a composition.
2392 All characters from char2b are drawn composed. */
2393 struct composition *cmp;
06a2c219
GM
2394
2395 /* Index of this glyph string's first character in the glyph
b4192550
KH
2396 definition of CMP. If this is zero, this glyph string describes
2397 the first character of a composition. */
06a2c219
GM
2398 int gidx;
2399
2400 /* 1 means this glyph strings face has to be drawn to the right end
2401 of the window's drawing area. */
2402 unsigned extends_to_end_of_line_p : 1;
2403
2404 /* 1 means the background of this string has been drawn. */
2405 unsigned background_filled_p : 1;
2406
2407 /* 1 means glyph string must be drawn with 16-bit functions. */
2408 unsigned two_byte_p : 1;
2409
2410 /* 1 means that the original font determined for drawing this glyph
2411 string could not be loaded. The member `font' has been set to
2412 the frame's default font in this case. */
2413 unsigned font_not_found_p : 1;
2414
2415 /* 1 means that the face in which this glyph string is drawn has a
2416 stipple pattern. */
2417 unsigned stippled_p : 1;
2418
66ac4b0e
GM
2419 /* 1 means only the foreground of this glyph string must be drawn,
2420 and we should use the physical height of the line this glyph
2421 string appears in as clip rect. */
2422 unsigned for_overlaps_p : 1;
2423
06a2c219
GM
2424 /* The GC to use for drawing this glyph string. */
2425 GC gc;
2426
2427 /* A pointer to the first glyph in the string. This glyph
2428 corresponds to char2b[0]. Needed to draw rectangles if
2429 font_not_found_p is 1. */
2430 struct glyph *first_glyph;
2431
2432 /* Image, if any. */
2433 struct image *img;
2434
2435 struct glyph_string *next, *prev;
2436};
2437
2438
5c187dee 2439#if 0
06a2c219
GM
2440
2441static void
2442x_dump_glyph_string (s)
2443 struct glyph_string *s;
2444{
2445 fprintf (stderr, "glyph string\n");
2446 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2447 s->x, s->y, s->width, s->height);
2448 fprintf (stderr, " ybase = %d\n", s->ybase);
2449 fprintf (stderr, " hl = %d\n", s->hl);
2450 fprintf (stderr, " left overhang = %d, right = %d\n",
2451 s->left_overhang, s->right_overhang);
2452 fprintf (stderr, " nchars = %d\n", s->nchars);
2453 fprintf (stderr, " extends to end of line = %d\n",
2454 s->extends_to_end_of_line_p);
2455 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2456 fprintf (stderr, " bg width = %d\n", s->background_width);
2457}
2458
2459#endif /* GLYPH_DEBUG */
2460
2461
2462
2463static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2464 struct glyph_string **,
2465 struct glyph_string *,
2466 struct glyph_string *));
2467static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2468 struct glyph_string **,
2469 struct glyph_string *,
2470 struct glyph_string *));
2471static void x_append_glyph_string P_ ((struct glyph_string **,
2472 struct glyph_string **,
2473 struct glyph_string *));
2474static int x_left_overwritten P_ ((struct glyph_string *));
2475static int x_left_overwriting P_ ((struct glyph_string *));
2476static int x_right_overwritten P_ ((struct glyph_string *));
2477static int x_right_overwriting P_ ((struct glyph_string *));
66ac4b0e
GM
2478static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2479 int));
06a2c219
GM
2480static void x_init_glyph_string P_ ((struct glyph_string *,
2481 XChar2b *, struct window *,
2482 struct glyph_row *,
2483 enum glyph_row_area, int,
2484 enum draw_glyphs_face));
2485static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2486 enum glyph_row_area, int, int,
66ac4b0e 2487 enum draw_glyphs_face, int *, int *, int));
06a2c219
GM
2488static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2489static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2490static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2491 int));
2492static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
b4192550 2493static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
06a2c219
GM
2494static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2495static void x_draw_glyph_string P_ ((struct glyph_string *));
2496static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2497static void x_set_cursor_gc P_ ((struct glyph_string *));
2498static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2499static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2500static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2501 int *, int *));
2502static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2503static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
68c45bf0 2504 unsigned long *, double, int));
06a2c219 2505static void x_setup_relief_color P_ ((struct frame *, struct relief *,
68c45bf0 2506 double, int, unsigned long));
06a2c219
GM
2507static void x_setup_relief_colors P_ ((struct glyph_string *));
2508static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2509static void x_draw_image_relief P_ ((struct glyph_string *));
2510static void x_draw_image_foreground P_ ((struct glyph_string *));
2511static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2512static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2513static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2514 int, int, int));
2515static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2516 int, int, int, int, XRectangle *));
2517static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2518 int, int, int, XRectangle *));
66ac4b0e
GM
2519static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2520 enum glyph_row_area));
209f68d9
GM
2521static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2522 struct glyph_row *,
2523 enum glyph_row_area, int, int));
06a2c219 2524
163dcff3
GM
2525#if GLYPH_DEBUG
2526static void x_check_font P_ ((struct frame *, XFontStruct *));
2527#endif
2528
06a2c219 2529
06a2c219
GM
2530/* Append the list of glyph strings with head H and tail T to the list
2531 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2532
2533static INLINE void
2534x_append_glyph_string_lists (head, tail, h, t)
2535 struct glyph_string **head, **tail;
2536 struct glyph_string *h, *t;
2537{
2538 if (h)
2539 {
2540 if (*head)
2541 (*tail)->next = h;
2542 else
2543 *head = h;
2544 h->prev = *tail;
2545 *tail = t;
2546 }
2547}
2548
2549
2550/* Prepend the list of glyph strings with head H and tail T to the
2551 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2552 result. */
2553
2554static INLINE void
2555x_prepend_glyph_string_lists (head, tail, h, t)
2556 struct glyph_string **head, **tail;
2557 struct glyph_string *h, *t;
2558{
2559 if (h)
2560 {
2561 if (*head)
2562 (*head)->prev = t;
2563 else
2564 *tail = t;
2565 t->next = *head;
2566 *head = h;
2567 }
2568}
2569
2570
2571/* Append glyph string S to the list with head *HEAD and tail *TAIL.
2572 Set *HEAD and *TAIL to the resulting list. */
2573
2574static INLINE void
2575x_append_glyph_string (head, tail, s)
2576 struct glyph_string **head, **tail;
2577 struct glyph_string *s;
2578{
2579 s->next = s->prev = NULL;
2580 x_append_glyph_string_lists (head, tail, s, s);
2581}
2582
2583
2584/* Set S->gc to a suitable GC for drawing glyph string S in cursor
2585 face. */
2586
2587static void
2588x_set_cursor_gc (s)
2589 struct glyph_string *s;
2590{
2591 if (s->font == FRAME_FONT (s->f)
2592 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2593 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
b4192550 2594 && !s->cmp)
06a2c219
GM
2595 s->gc = s->f->output_data.x->cursor_gc;
2596 else
2597 {
2598 /* Cursor on non-default face: must merge. */
2599 XGCValues xgcv;
2600 unsigned long mask;
2601
2602 xgcv.background = s->f->output_data.x->cursor_pixel;
2603 xgcv.foreground = s->face->background;
2604
2605 /* If the glyph would be invisible, try a different foreground. */
2606 if (xgcv.foreground == xgcv.background)
2607 xgcv.foreground = s->face->foreground;
2608 if (xgcv.foreground == xgcv.background)
2609 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2610 if (xgcv.foreground == xgcv.background)
2611 xgcv.foreground = s->face->foreground;
2612
2613 /* Make sure the cursor is distinct from text in this face. */
2614 if (xgcv.background == s->face->background
2615 && xgcv.foreground == s->face->foreground)
2616 {
2617 xgcv.background = s->face->foreground;
2618 xgcv.foreground = s->face->background;
2619 }
2620
2621 IF_DEBUG (x_check_font (s->f, s->font));
2622 xgcv.font = s->font->fid;
2623 xgcv.graphics_exposures = False;
2624 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2625
2626 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2627 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2628 mask, &xgcv);
2629 else
2630 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2631 = XCreateGC (s->display, s->window, mask, &xgcv);
2632
2633 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2634 }
2635}
2636
2637
2638/* Set up S->gc of glyph string S for drawing text in mouse face. */
2639
2640static void
2641x_set_mouse_face_gc (s)
2642 struct glyph_string *s;
2643{
2644 int face_id;
ee569018 2645 struct face *face;
06a2c219
GM
2646
2647 /* What face has to be used for the mouse face? */
2648 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
ee569018 2649 face = FACE_FROM_ID (s->f, face_id);
033e3e18
GM
2650 if (s->first_glyph->type == CHAR_GLYPH)
2651 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2652 else
2653 face_id = FACE_FOR_CHAR (s->f, face, 0);
06a2c219
GM
2654 s->face = FACE_FROM_ID (s->f, face_id);
2655 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2656
2657 /* If font in this face is same as S->font, use it. */
2658 if (s->font == s->face->font)
2659 s->gc = s->face->gc;
2660 else
2661 {
2662 /* Otherwise construct scratch_cursor_gc with values from FACE
2663 but font FONT. */
2664 XGCValues xgcv;
2665 unsigned long mask;
2666
2667 xgcv.background = s->face->background;
2668 xgcv.foreground = s->face->foreground;
2669 IF_DEBUG (x_check_font (s->f, s->font));
2670 xgcv.font = s->font->fid;
2671 xgcv.graphics_exposures = False;
2672 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2673
2674 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2675 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2676 mask, &xgcv);
2677 else
2678 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2679 = XCreateGC (s->display, s->window, mask, &xgcv);
2680
2681 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2682 }
2683
2684 xassert (s->gc != 0);
2685}
2686
2687
2688/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2689 Faces to use in the mode line have already been computed when the
2690 matrix was built, so there isn't much to do, here. */
2691
2692static INLINE void
2693x_set_mode_line_face_gc (s)
2694 struct glyph_string *s;
2695{
2696 s->gc = s->face->gc;
06a2c219
GM
2697}
2698
2699
2700/* Set S->gc of glyph string S for drawing that glyph string. Set
2701 S->stippled_p to a non-zero value if the face of S has a stipple
2702 pattern. */
2703
2704static INLINE void
2705x_set_glyph_string_gc (s)
2706 struct glyph_string *s;
2707{
209f68d9
GM
2708 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2709
06a2c219
GM
2710 if (s->hl == DRAW_NORMAL_TEXT)
2711 {
2712 s->gc = s->face->gc;
2713 s->stippled_p = s->face->stipple != 0;
2714 }
2715 else if (s->hl == DRAW_INVERSE_VIDEO)
2716 {
2717 x_set_mode_line_face_gc (s);
2718 s->stippled_p = s->face->stipple != 0;
2719 }
2720 else if (s->hl == DRAW_CURSOR)
2721 {
2722 x_set_cursor_gc (s);
2723 s->stippled_p = 0;
2724 }
2725 else if (s->hl == DRAW_MOUSE_FACE)
2726 {
2727 x_set_mouse_face_gc (s);
2728 s->stippled_p = s->face->stipple != 0;
2729 }
2730 else if (s->hl == DRAW_IMAGE_RAISED
2731 || s->hl == DRAW_IMAGE_SUNKEN)
2732 {
2733 s->gc = s->face->gc;
2734 s->stippled_p = s->face->stipple != 0;
2735 }
2736 else
2737 {
2738 s->gc = s->face->gc;
2739 s->stippled_p = s->face->stipple != 0;
2740 }
2741
2742 /* GC must have been set. */
2743 xassert (s->gc != 0);
2744}
2745
2746
2747/* Return in *R the clipping rectangle for glyph string S. */
2748
2749static void
2750x_get_glyph_string_clip_rect (s, r)
2751 struct glyph_string *s;
2752 XRectangle *r;
2753{
2754 if (s->row->full_width_p)
2755 {
2756 /* Draw full-width. X coordinates are relative to S->w->left. */
1da3fd71
GM
2757 int canon_x = CANON_X_UNIT (s->f);
2758
2759 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2760 r->width = XFASTINT (s->w->width) * canon_x;
06a2c219
GM
2761
2762 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2763 {
1da3fd71 2764 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
06a2c219
GM
2765 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2766 r->x -= width;
2767 }
2768
b9432a85 2769 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
1da3fd71 2770
06a2c219
GM
2771 /* Unless displaying a mode or menu bar line, which are always
2772 fully visible, clip to the visible part of the row. */
2773 if (s->w->pseudo_window_p)
2774 r->height = s->row->visible_height;
2775 else
2776 r->height = s->height;
2777 }
2778 else
2779 {
2780 /* This is a text line that may be partially visible. */
2781 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2782 r->width = window_box_width (s->w, s->area);
2783 r->height = s->row->visible_height;
2784 }
2785
2786 /* Don't use S->y for clipping because it doesn't take partially
2787 visible lines into account. For example, it can be negative for
2788 partially visible lines at the top of a window. */
2789 if (!s->row->full_width_p
2790 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
045dee35 2791 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
06a2c219
GM
2792 else
2793 r->y = max (0, s->row->y);
06a2c219 2794
9ea173e8 2795 /* If drawing a tool-bar window, draw it over the internal border
06a2c219 2796 at the top of the window. */
9ea173e8 2797 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219 2798 r->y -= s->f->output_data.x->internal_border_width;
66ac4b0e
GM
2799
2800 /* If S draws overlapping rows, it's sufficient to use the top and
2801 bottom of the window for clipping because this glyph string
2802 intentionally draws over other lines. */
2803 if (s->for_overlaps_p)
2804 {
045dee35 2805 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
66ac4b0e
GM
2806 r->height = window_text_bottom_y (s->w) - r->y;
2807 }
2808
2809 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
06a2c219
GM
2810}
2811
2812
2813/* Set clipping for output of glyph string S. S may be part of a mode
2814 line or menu if we don't have X toolkit support. */
2815
2816static INLINE void
2817x_set_glyph_string_clipping (s)
2818 struct glyph_string *s;
2819{
2820 XRectangle r;
2821 x_get_glyph_string_clip_rect (s, &r);
2822 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2823}
2824
2825
2826/* Compute left and right overhang of glyph string S. If S is a glyph
b4192550 2827 string for a composition, assume overhangs don't exist. */
06a2c219
GM
2828
2829static INLINE void
2830x_compute_glyph_string_overhangs (s)
2831 struct glyph_string *s;
2832{
b4192550 2833 if (s->cmp == NULL
06a2c219
GM
2834 && s->first_glyph->type == CHAR_GLYPH)
2835 {
2836 XCharStruct cs;
2837 int direction, font_ascent, font_descent;
2838 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2839 &font_ascent, &font_descent, &cs);
2840 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2841 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2842 }
2843}
2844
2845
2846/* Compute overhangs and x-positions for glyph string S and its
2847 predecessors, or successors. X is the starting x-position for S.
2848 BACKWARD_P non-zero means process predecessors. */
2849
2850static void
2851x_compute_overhangs_and_x (s, x, backward_p)
2852 struct glyph_string *s;
2853 int x;
2854 int backward_p;
2855{
2856 if (backward_p)
2857 {
2858 while (s)
2859 {
2860 x_compute_glyph_string_overhangs (s);
2861 x -= s->width;
2862 s->x = x;
2863 s = s->prev;
2864 }
2865 }
2866 else
2867 {
2868 while (s)
2869 {
2870 x_compute_glyph_string_overhangs (s);
2871 s->x = x;
2872 x += s->width;
2873 s = s->next;
2874 }
2875 }
2876}
2877
2878
2879/* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
b4192550
KH
2880 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2881 assumed to be zero. */
06a2c219
GM
2882
2883static void
2884x_get_glyph_overhangs (glyph, f, left, right)
2885 struct glyph *glyph;
2886 struct frame *f;
2887 int *left, *right;
2888{
06a2c219
GM
2889 *left = *right = 0;
2890
b4192550 2891 if (glyph->type == CHAR_GLYPH)
06a2c219
GM
2892 {
2893 XFontStruct *font;
2894 struct face *face;
2895 struct font_info *font_info;
2896 XChar2b char2b;
ee569018
KH
2897 XCharStruct *pcm;
2898
2899 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
06a2c219
GM
2900 font = face->font;
2901 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
ee569018
KH
2902 if (font
2903 && (pcm = x_per_char_metric (font, &char2b)))
06a2c219 2904 {
06a2c219
GM
2905 if (pcm->rbearing > pcm->width)
2906 *right = pcm->rbearing - pcm->width;
2907 if (pcm->lbearing < 0)
2908 *left = -pcm->lbearing;
2909 }
2910 }
2911}
2912
2913
2914/* Return the index of the first glyph preceding glyph string S that
2915 is overwritten by S because of S's left overhang. Value is -1
2916 if no glyphs are overwritten. */
2917
2918static int
2919x_left_overwritten (s)
2920 struct glyph_string *s;
2921{
2922 int k;
2923
2924 if (s->left_overhang)
2925 {
2926 int x = 0, i;
2927 struct glyph *glyphs = s->row->glyphs[s->area];
2928 int first = s->first_glyph - glyphs;
2929
2930 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2931 x -= glyphs[i].pixel_width;
2932
2933 k = i + 1;
2934 }
2935 else
2936 k = -1;
2937
2938 return k;
2939}
2940
2941
2942/* Return the index of the first glyph preceding glyph string S that
2943 is overwriting S because of its right overhang. Value is -1 if no
2944 glyph in front of S overwrites S. */
2945
2946static int
2947x_left_overwriting (s)
2948 struct glyph_string *s;
2949{
2950 int i, k, x;
2951 struct glyph *glyphs = s->row->glyphs[s->area];
2952 int first = s->first_glyph - glyphs;
2953
2954 k = -1;
2955 x = 0;
2956 for (i = first - 1; i >= 0; --i)
2957 {
2958 int left, right;
2959 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2960 if (x + right > 0)
2961 k = i;
2962 x -= glyphs[i].pixel_width;
2963 }
2964
2965 return k;
2966}
2967
2968
2969/* Return the index of the last glyph following glyph string S that is
2970 not overwritten by S because of S's right overhang. Value is -1 if
2971 no such glyph is found. */
2972
2973static int
2974x_right_overwritten (s)
2975 struct glyph_string *s;
2976{
2977 int k = -1;
2978
2979 if (s->right_overhang)
2980 {
2981 int x = 0, i;
2982 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 2983 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
2984 int end = s->row->used[s->area];
2985
2986 for (i = first; i < end && s->right_overhang > x; ++i)
2987 x += glyphs[i].pixel_width;
2988
2989 k = i;
2990 }
2991
2992 return k;
2993}
2994
2995
2996/* Return the index of the last glyph following glyph string S that
2997 overwrites S because of its left overhang. Value is negative
2998 if no such glyph is found. */
2999
3000static int
3001x_right_overwriting (s)
3002 struct glyph_string *s;
3003{
3004 int i, k, x;
3005 int end = s->row->used[s->area];
3006 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 3007 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
3008
3009 k = -1;
3010 x = 0;
3011 for (i = first; i < end; ++i)
3012 {
3013 int left, right;
3014 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3015 if (x - left < 0)
3016 k = i;
3017 x += glyphs[i].pixel_width;
3018 }
3019
3020 return k;
3021}
3022
3023
3024/* Fill rectangle X, Y, W, H with background color of glyph string S. */
3025
3026static INLINE void
3027x_clear_glyph_string_rect (s, x, y, w, h)
3028 struct glyph_string *s;
3029 int x, y, w, h;
3030{
3031 XGCValues xgcv;
3032 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3033 XSetForeground (s->display, s->gc, xgcv.background);
3034 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3035 XSetForeground (s->display, s->gc, xgcv.foreground);
3036}
3037
3038
3039/* Draw the background of glyph_string S. If S->background_filled_p
3040 is non-zero don't draw it. FORCE_P non-zero means draw the
3041 background even if it wouldn't be drawn normally. This is used
b4192550
KH
3042 when a string preceding S draws into the background of S, or S
3043 contains the first component of a composition. */
06a2c219
GM
3044
3045static void
3046x_draw_glyph_string_background (s, force_p)
3047 struct glyph_string *s;
3048 int force_p;
3049{
3050 /* Nothing to do if background has already been drawn or if it
3051 shouldn't be drawn in the first place. */
3052 if (!s->background_filled_p)
3053 {
b4192550 3054 if (s->stippled_p)
06a2c219
GM
3055 {
3056 /* Fill background with a stipple pattern. */
3057 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3058 XFillRectangle (s->display, s->window, s->gc, s->x,
3059 s->y + s->face->box_line_width,
3060 s->background_width,
3061 s->height - 2 * s->face->box_line_width);
3062 XSetFillStyle (s->display, s->gc, FillSolid);
3063 s->background_filled_p = 1;
3064 }
3065 else if (FONT_HEIGHT (s->font) < s->height - 2 * s->face->box_line_width
3066 || s->font_not_found_p
3067 || s->extends_to_end_of_line_p
06a2c219
GM
3068 || force_p)
3069 {
3070 x_clear_glyph_string_rect (s, s->x, s->y + s->face->box_line_width,
3071 s->background_width,
3072 s->height - 2 * s->face->box_line_width);
3073 s->background_filled_p = 1;
3074 }
3075 }
3076}
3077
3078
3079/* Draw the foreground of glyph string S. */
3080
3081static void
3082x_draw_glyph_string_foreground (s)
3083 struct glyph_string *s;
3084{
3085 int i, x;
3086
3087 /* If first glyph of S has a left box line, start drawing the text
3088 of S to the right of that box line. */
3089 if (s->face->box != FACE_NO_BOX
3090 && s->first_glyph->left_box_line_p)
3091 x = s->x + s->face->box_line_width;
3092 else
3093 x = s->x;
3094
b4192550
KH
3095 /* Draw characters of S as rectangles if S's font could not be
3096 loaded. */
3097 if (s->font_not_found_p)
06a2c219 3098 {
b4192550 3099 for (i = 0; i < s->nchars; ++i)
06a2c219 3100 {
b4192550
KH
3101 struct glyph *g = s->first_glyph + i;
3102 XDrawRectangle (s->display, s->window,
3103 s->gc, x, s->y, g->pixel_width - 1,
3104 s->height - 1);
3105 x += g->pixel_width;
06a2c219
GM
3106 }
3107 }
3108 else
3109 {
b4192550
KH
3110 char *char1b = (char *) s->char2b;
3111 int boff = s->font_info->baseline_offset;
06a2c219 3112
b4192550
KH
3113 if (s->font_info->vertical_centering)
3114 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3115
3116 /* If we can use 8-bit functions, condense S->char2b. */
3117 if (!s->two_byte_p)
3118 for (i = 0; i < s->nchars; ++i)
3119 char1b[i] = s->char2b[i].byte2;
3120
3121 /* Draw text with XDrawString if background has already been
3122 filled. Otherwise, use XDrawImageString. (Note that
3123 XDrawImageString is usually faster than XDrawString.) Always
3124 use XDrawImageString when drawing the cursor so that there is
3125 no chance that characters under a box cursor are invisible. */
3126 if (s->for_overlaps_p
3127 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3128 {
3129 /* Draw characters with 16-bit or 8-bit functions. */
3130 if (s->two_byte_p)
3131 XDrawString16 (s->display, s->window, s->gc, x,
3132 s->ybase - boff, s->char2b, s->nchars);
3133 else
3134 XDrawString (s->display, s->window, s->gc, x,
3135 s->ybase - boff, char1b, s->nchars);
3136 }
06a2c219
GM
3137 else
3138 {
b4192550
KH
3139 if (s->two_byte_p)
3140 XDrawImageString16 (s->display, s->window, s->gc, x,
3141 s->ybase - boff, s->char2b, s->nchars);
06a2c219 3142 else
b4192550
KH
3143 XDrawImageString (s->display, s->window, s->gc, x,
3144 s->ybase - boff, char1b, s->nchars);
3145 }
3146 }
3147}
06a2c219 3148
b4192550 3149/* Draw the foreground of composite glyph string S. */
06a2c219 3150
b4192550
KH
3151static void
3152x_draw_composite_glyph_string_foreground (s)
3153 struct glyph_string *s;
3154{
3155 int i, x;
06a2c219 3156
b4192550
KH
3157 /* If first glyph of S has a left box line, start drawing the text
3158 of S to the right of that box line. */
3159 if (s->face->box != FACE_NO_BOX
3160 && s->first_glyph->left_box_line_p)
3161 x = s->x + s->face->box_line_width;
3162 else
3163 x = s->x;
06a2c219 3164
b4192550
KH
3165 /* S is a glyph string for a composition. S->gidx is the index of
3166 the first character drawn for glyphs of this composition.
3167 S->gidx == 0 means we are drawing the very first character of
3168 this composition. */
06a2c219 3169
b4192550
KH
3170 /* Draw a rectangle for the composition if the font for the very
3171 first character of the composition could not be loaded. */
3172 if (s->font_not_found_p)
3173 {
3174 if (s->gidx == 0)
3175 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3176 s->width - 1, s->height - 1);
3177 }
3178 else
3179 {
3180 for (i = 0; i < s->nchars; i++, ++s->gidx)
3181 XDrawString16 (s->display, s->window, s->gc,
3182 x + s->cmp->offsets[s->gidx * 2],
3183 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3184 s->char2b + i, 1);
06a2c219
GM
3185 }
3186}
3187
3188
80c32bcc
GM
3189#ifdef USE_X_TOOLKIT
3190
3e71d8f2 3191static struct frame *x_frame_of_widget P_ ((Widget));
80c32bcc 3192
3e71d8f2
GM
3193
3194/* Return the frame on which widget WIDGET is used.. Abort if frame
3195 cannot be determined. */
3196
e851c833 3197static struct frame *
3e71d8f2 3198x_frame_of_widget (widget)
80c32bcc 3199 Widget widget;
80c32bcc 3200{
80c32bcc 3201 struct x_display_info *dpyinfo;
5c187dee 3202 Lisp_Object tail;
3e71d8f2
GM
3203 struct frame *f;
3204
80c32bcc
GM
3205 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3206
3207 /* Find the top-level shell of the widget. Note that this function
3208 can be called when the widget is not yet realized, so XtWindow
3209 (widget) == 0. That's the reason we can't simply use
3210 x_any_window_to_frame. */
3211 while (!XtIsTopLevelShell (widget))
3212 widget = XtParent (widget);
3213
3214 /* Look for a frame with that top-level widget. Allocate the color
3215 on that frame to get the right gamma correction value. */
3216 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3217 if (GC_FRAMEP (XCAR (tail))
3218 && (f = XFRAME (XCAR (tail)),
3219 (f->output_data.nothing != 1
3220 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3221 && f->output_data.x->widget == widget)
3e71d8f2 3222 return f;
80c32bcc
GM
3223
3224 abort ();
3225}
3226
3e71d8f2
GM
3227
3228/* Allocate the color COLOR->pixel on the screen and display of
3229 widget WIDGET in colormap CMAP. If an exact match cannot be
3230 allocated, try the nearest color available. Value is non-zero
3231 if successful. This is called from lwlib. */
3232
3233int
3234x_alloc_nearest_color_for_widget (widget, cmap, color)
3235 Widget widget;
3236 Colormap cmap;
3237 XColor *color;
3238{
3239 struct frame *f = x_frame_of_widget (widget);
3240 return x_alloc_nearest_color (f, cmap, color);
3241}
3242
3243
80c32bcc
GM
3244#endif /* USE_X_TOOLKIT */
3245
3246
06a2c219
GM
3247/* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3248 CMAP. If an exact match can't be allocated, try the nearest color
3249 available. Value is non-zero if successful. Set *COLOR to the
3250 color allocated. */
3251
3252int
80c32bcc
GM
3253x_alloc_nearest_color (f, cmap, color)
3254 struct frame *f;
06a2c219
GM
3255 Colormap cmap;
3256 XColor *color;
3257{
80c32bcc
GM
3258 Display *display = FRAME_X_DISPLAY (f);
3259 Screen *screen = FRAME_X_SCREEN (f);
3260 int rc;
3261
3262 gamma_correct (f, color);
3263 rc = XAllocColor (display, cmap, color);
06a2c219
GM
3264 if (rc == 0)
3265 {
3266 /* If we got to this point, the colormap is full, so we're going
3267 to try to get the next closest color. The algorithm used is
3268 a least-squares matching, which is what X uses for closest
3269 color matching with StaticColor visuals. */
3270 int nearest, i;
3271 unsigned long nearest_delta = ~0;
3272 int ncells = XDisplayCells (display, XScreenNumberOfScreen (screen));
3273 XColor *cells = (XColor *) alloca (ncells * sizeof *cells);
3274
3275 for (i = 0; i < ncells; ++i)
3276 cells[i].pixel = i;
3277 XQueryColors (display, cmap, cells, ncells);
3278
3279 for (nearest = i = 0; i < ncells; ++i)
3280 {
3281 long dred = (color->red >> 8) - (cells[i].red >> 8);
3282 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3283 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3284 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3285
3286 if (delta < nearest_delta)
3287 {
3288 nearest = i;
3289 nearest_delta = delta;
3290 }
3291 }
3292
3293 color->red = cells[nearest].red;
3294 color->green = cells[nearest].green;
3295 color->blue = cells[nearest].blue;
3296 rc = XAllocColor (display, cmap, color);
3297 }
3298
d9c545da
GM
3299#ifdef DEBUG_X_COLORS
3300 if (rc)
3301 register_color (color->pixel);
3302#endif /* DEBUG_X_COLORS */
3303
06a2c219
GM
3304 return rc;
3305}
3306
3307
d9c545da
GM
3308/* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3309 It's necessary to do this instead of just using PIXEL directly to
3310 get color reference counts right. */
3311
3312unsigned long
3313x_copy_color (f, pixel)
3314 struct frame *f;
3315 unsigned long pixel;
3316{
3317 XColor color;
3318
3319 color.pixel = pixel;
3320 BLOCK_INPUT;
3321 XQueryColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3322 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3323 UNBLOCK_INPUT;
3324#ifdef DEBUG_X_COLORS
3325 register_color (pixel);
3326#endif
3327 return color.pixel;
3328}
3329
3330
3e71d8f2
GM
3331/* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3332 It's necessary to do this instead of just using PIXEL directly to
3333 get color reference counts right. */
3334
3335unsigned long
3336x_copy_dpy_color (dpy, cmap, pixel)
3337 Display *dpy;
3338 Colormap cmap;
3339 unsigned long pixel;
3340{
3341 XColor color;
3342
3343 color.pixel = pixel;
3344 BLOCK_INPUT;
3345 XQueryColor (dpy, cmap, &color);
3346 XAllocColor (dpy, cmap, &color);
3347 UNBLOCK_INPUT;
3348#ifdef DEBUG_X_COLORS
3349 register_color (pixel);
3350#endif
3351 return color.pixel;
3352}
3353
3354
06a2c219
GM
3355/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3356 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3357 If this produces the same color as PIXEL, try a color where all RGB
3358 values have DELTA added. Return the allocated color in *PIXEL.
3359 DISPLAY is the X display, CMAP is the colormap to operate on.
3360 Value is non-zero if successful. */
3361
3362static int
3363x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3364 struct frame *f;
3365 Display *display;
3366 Colormap cmap;
3367 unsigned long *pixel;
68c45bf0 3368 double factor;
06a2c219
GM
3369 int delta;
3370{
3371 XColor color, new;
3372 int success_p;
3373
3374 /* Get RGB color values. */
3375 color.pixel = *pixel;
3376 XQueryColor (display, cmap, &color);
3377
3378 /* Change RGB values by specified FACTOR. Avoid overflow! */
3379 xassert (factor >= 0);
3380 new.red = min (0xffff, factor * color.red);
3381 new.green = min (0xffff, factor * color.green);
3382 new.blue = min (0xffff, factor * color.blue);
3383
3384 /* Try to allocate the color. */
80c32bcc 3385 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3386 if (success_p)
3387 {
3388 if (new.pixel == *pixel)
3389 {
3390 /* If we end up with the same color as before, try adding
3391 delta to the RGB values. */
0d605c67 3392 x_free_colors (f, &new.pixel, 1);
06a2c219
GM
3393
3394 new.red = min (0xffff, delta + color.red);
3395 new.green = min (0xffff, delta + color.green);
3396 new.blue = min (0xffff, delta + color.blue);
80c32bcc 3397 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3398 }
3399 else
3400 success_p = 1;
3401 *pixel = new.pixel;
3402 }
3403
3404 return success_p;
3405}
3406
3407
3408/* Set up the foreground color for drawing relief lines of glyph
3409 string S. RELIEF is a pointer to a struct relief containing the GC
3410 with which lines will be drawn. Use a color that is FACTOR or
3411 DELTA lighter or darker than the relief's background which is found
3412 in S->f->output_data.x->relief_background. If such a color cannot
3413 be allocated, use DEFAULT_PIXEL, instead. */
3414
3415static void
3416x_setup_relief_color (f, relief, factor, delta, default_pixel)
3417 struct frame *f;
3418 struct relief *relief;
68c45bf0 3419 double factor;
06a2c219
GM
3420 int delta;
3421 unsigned long default_pixel;
3422{
3423 XGCValues xgcv;
3424 struct x_output *di = f->output_data.x;
3425 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3426 unsigned long pixel;
3427 unsigned long background = di->relief_background;
43bd1b2b 3428 Colormap cmap = FRAME_X_COLORMAP (f);
dcd08bfb
GM
3429 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3430 Display *dpy = FRAME_X_DISPLAY (f);
06a2c219
GM
3431
3432 xgcv.graphics_exposures = False;
3433 xgcv.line_width = 1;
3434
3435 /* Free previously allocated color. The color cell will be reused
3436 when it has been freed as many times as it was allocated, so this
3437 doesn't affect faces using the same colors. */
3438 if (relief->gc
3439 && relief->allocated_p)
3440 {
0d605c67 3441 x_free_colors (f, &relief->pixel, 1);
06a2c219
GM
3442 relief->allocated_p = 0;
3443 }
3444
3445 /* Allocate new color. */
3446 xgcv.foreground = default_pixel;
3447 pixel = background;
dcd08bfb
GM
3448 if (dpyinfo->n_planes != 1
3449 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
06a2c219
GM
3450 {
3451 relief->allocated_p = 1;
3452 xgcv.foreground = relief->pixel = pixel;
3453 }
3454
3455 if (relief->gc == 0)
3456 {
dcd08bfb 3457 xgcv.stipple = dpyinfo->gray;
06a2c219 3458 mask |= GCStipple;
dcd08bfb 3459 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
06a2c219
GM
3460 }
3461 else
dcd08bfb 3462 XChangeGC (dpy, relief->gc, mask, &xgcv);
06a2c219
GM
3463}
3464
3465
3466/* Set up colors for the relief lines around glyph string S. */
3467
3468static void
3469x_setup_relief_colors (s)
3470 struct glyph_string *s;
3471{
3472 struct x_output *di = s->f->output_data.x;
3473 unsigned long color;
3474
3475 if (s->face->use_box_color_for_shadows_p)
3476 color = s->face->box_color;
3477 else
3478 {
3479 XGCValues xgcv;
3480
3481 /* Get the background color of the face. */
3482 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3483 color = xgcv.background;
3484 }
3485
3486 if (di->white_relief.gc == 0
3487 || color != di->relief_background)
3488 {
3489 di->relief_background = color;
3490 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3491 WHITE_PIX_DEFAULT (s->f));
3492 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3493 BLACK_PIX_DEFAULT (s->f));
3494 }
3495}
3496
3497
3498/* Draw a relief on frame F inside the rectangle given by LEFT_X,
3499 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3500 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3501 relief. LEFT_P non-zero means draw a relief on the left side of
3502 the rectangle. RIGHT_P non-zero means draw a relief on the right
3503 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3504 when drawing. */
3505
3506static void
3507x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3508 raised_p, left_p, right_p, clip_rect)
3509 struct frame *f;
3510 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3511 XRectangle *clip_rect;
3512{
3513 int i;
3514 GC gc;
3515
3516 if (raised_p)
3517 gc = f->output_data.x->white_relief.gc;
3518 else
3519 gc = f->output_data.x->black_relief.gc;
3520 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3521
3522 /* Top. */
3523 for (i = 0; i < width; ++i)
3524 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3525 left_x + i * left_p, top_y + i,
3526 right_x + 1 - i * right_p, top_y + i);
3527
3528 /* Left. */
3529 if (left_p)
3530 for (i = 0; i < width; ++i)
3531 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3532 left_x + i, top_y + i, left_x + i, bottom_y - i);
3533
3534 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3535 if (raised_p)
3536 gc = f->output_data.x->black_relief.gc;
3537 else
3538 gc = f->output_data.x->white_relief.gc;
3539 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3540
3541 /* Bottom. */
3542 for (i = 0; i < width; ++i)
3543 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3544 left_x + i * left_p, bottom_y - i,
3545 right_x + 1 - i * right_p, bottom_y - i);
3546
3547 /* Right. */
3548 if (right_p)
3549 for (i = 0; i < width; ++i)
3550 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3551 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3552
3553 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3554}
3555
3556
3557/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3558 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3559 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3560 left side of the rectangle. RIGHT_P non-zero means draw a line
3561 on the right side of the rectangle. CLIP_RECT is the clipping
3562 rectangle to use when drawing. */
3563
3564static void
3565x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3566 left_p, right_p, clip_rect)
3567 struct glyph_string *s;
3568 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3569 XRectangle *clip_rect;
3570{
3571 XGCValues xgcv;
3572
3573 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3574 XSetForeground (s->display, s->gc, s->face->box_color);
3575 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3576
3577 /* Top. */
3578 XFillRectangle (s->display, s->window, s->gc,
3579 left_x, top_y, right_x - left_x, width);
3580
3581 /* Left. */
3582 if (left_p)
3583 XFillRectangle (s->display, s->window, s->gc,
3584 left_x, top_y, width, bottom_y - top_y);
3585
3586 /* Bottom. */
3587 XFillRectangle (s->display, s->window, s->gc,
3588 left_x, bottom_y - width, right_x - left_x, width);
3589
3590 /* Right. */
3591 if (right_p)
3592 XFillRectangle (s->display, s->window, s->gc,
3593 right_x - width, top_y, width, bottom_y - top_y);
3594
3595 XSetForeground (s->display, s->gc, xgcv.foreground);
3596 XSetClipMask (s->display, s->gc, None);
3597}
3598
3599
3600/* Draw a box around glyph string S. */
3601
3602static void
3603x_draw_glyph_string_box (s)
3604 struct glyph_string *s;
3605{
3606 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3607 int left_p, right_p;
3608 struct glyph *last_glyph;
3609 XRectangle clip_rect;
3610
3611 last_x = window_box_right (s->w, s->area);
3612 if (s->row->full_width_p
3613 && !s->w->pseudo_window_p)
3614 {
110859fc 3615 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
06a2c219
GM
3616 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3617 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3618 }
3619
3620 /* The glyph that may have a right box line. */
b4192550 3621 last_glyph = (s->cmp || s->img
06a2c219
GM
3622 ? s->first_glyph
3623 : s->first_glyph + s->nchars - 1);
3624
3625 width = s->face->box_line_width;
3626 raised_p = s->face->box == FACE_RAISED_BOX;
3627 left_x = s->x;
3628 right_x = ((s->row->full_width_p
1da3fd71 3629 ? last_x - 1
a7aeb2de 3630 : min (last_x, s->x + s->background_width) - 1));
06a2c219
GM
3631 top_y = s->y;
3632 bottom_y = top_y + s->height - 1;
3633
3634 left_p = (s->first_glyph->left_box_line_p
3635 || (s->hl == DRAW_MOUSE_FACE
3636 && (s->prev == NULL
3637 || s->prev->hl != s->hl)));
3638 right_p = (last_glyph->right_box_line_p
3639 || (s->hl == DRAW_MOUSE_FACE
3640 && (s->next == NULL
3641 || s->next->hl != s->hl)));
3642
3643 x_get_glyph_string_clip_rect (s, &clip_rect);
3644
3645 if (s->face->box == FACE_SIMPLE_BOX)
3646 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3647 left_p, right_p, &clip_rect);
3648 else
3649 {
3650 x_setup_relief_colors (s);
3651 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3652 width, raised_p, left_p, right_p, &clip_rect);
3653 }
3654}
3655
3656
3657/* Draw foreground of image glyph string S. */
3658
3659static void
3660x_draw_image_foreground (s)
3661 struct glyph_string *s;
3662{
3663 int x;
95af8492 3664 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
3665
3666 /* If first glyph of S has a left box line, start drawing it to the
3667 right of that line. */
3668 if (s->face->box != FACE_NO_BOX
3669 && s->first_glyph->left_box_line_p)
3670 x = s->x + s->face->box_line_width;
3671 else
3672 x = s->x;
3673
3674 /* If there is a margin around the image, adjust x- and y-position
3675 by that margin. */
3676 if (s->img->margin)
3677 {
3678 x += s->img->margin;
3679 y += s->img->margin;
3680 }
3681
3682 if (s->img->pixmap)
3683 {
3684 if (s->img->mask)
3685 {
3686 /* We can't set both a clip mask and use XSetClipRectangles
3687 because the latter also sets a clip mask. We also can't
3688 trust on the shape extension to be available
3689 (XShapeCombineRegion). So, compute the rectangle to draw
3690 manually. */
3691 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3692 | GCFunction);
3693 XGCValues xgcv;
3694 XRectangle clip_rect, image_rect, r;
3695
3696 xgcv.clip_mask = s->img->mask;
3697 xgcv.clip_x_origin = x;
3698 xgcv.clip_y_origin = y;
3699 xgcv.function = GXcopy;
3700 XChangeGC (s->display, s->gc, mask, &xgcv);
3701
3702 x_get_glyph_string_clip_rect (s, &clip_rect);
3703 image_rect.x = x;
3704 image_rect.y = y;
3705 image_rect.width = s->img->width;
3706 image_rect.height = s->img->height;
3707 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3708 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3709 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3710 }
3711 else
3712 {
3713 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3714 0, 0, s->img->width, s->img->height, x, y);
3715
3716 /* When the image has a mask, we can expect that at
3717 least part of a mouse highlight or a block cursor will
3718 be visible. If the image doesn't have a mask, make
3719 a block cursor visible by drawing a rectangle around
3720 the image. I believe it's looking better if we do
3721 nothing here for mouse-face. */
3722 if (s->hl == DRAW_CURSOR)
3723 XDrawRectangle (s->display, s->window, s->gc, x, y,
3724 s->img->width - 1, s->img->height - 1);
3725 }
3726 }
3727 else
3728 /* Draw a rectangle if image could not be loaded. */
3729 XDrawRectangle (s->display, s->window, s->gc, x, y,
3730 s->img->width - 1, s->img->height - 1);
3731}
3732
3733
3734/* Draw a relief around the image glyph string S. */
3735
3736static void
3737x_draw_image_relief (s)
3738 struct glyph_string *s;
3739{
3740 int x0, y0, x1, y1, thick, raised_p;
3741 XRectangle r;
3742 int x;
95af8492 3743 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
3744
3745 /* If first glyph of S has a left box line, start drawing it to the
3746 right of that line. */
3747 if (s->face->box != FACE_NO_BOX
3748 && s->first_glyph->left_box_line_p)
3749 x = s->x + s->face->box_line_width;
3750 else
3751 x = s->x;
3752
3753 /* If there is a margin around the image, adjust x- and y-position
3754 by that margin. */
3755 if (s->img->margin)
3756 {
3757 x += s->img->margin;
3758 y += s->img->margin;
3759 }
3760
3761 if (s->hl == DRAW_IMAGE_SUNKEN
3762 || s->hl == DRAW_IMAGE_RAISED)
3763 {
9ea173e8 3764 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
06a2c219
GM
3765 raised_p = s->hl == DRAW_IMAGE_RAISED;
3766 }
3767 else
3768 {
3769 thick = abs (s->img->relief);
3770 raised_p = s->img->relief > 0;
3771 }
3772
3773 x0 = x - thick;
3774 y0 = y - thick;
3775 x1 = x + s->img->width + thick - 1;
3776 y1 = y + s->img->height + thick - 1;
3777
3778 x_setup_relief_colors (s);
3779 x_get_glyph_string_clip_rect (s, &r);
3780 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3781}
3782
3783
3784/* Draw the foreground of image glyph string S to PIXMAP. */
3785
3786static void
3787x_draw_image_foreground_1 (s, pixmap)
3788 struct glyph_string *s;
3789 Pixmap pixmap;
3790{
3791 int x;
95af8492 3792 int y = s->ybase - s->y - image_ascent (s->img, s->face);
06a2c219
GM
3793
3794 /* If first glyph of S has a left box line, start drawing it to the
3795 right of that line. */
3796 if (s->face->box != FACE_NO_BOX
3797 && s->first_glyph->left_box_line_p)
3798 x = s->face->box_line_width;
3799 else
3800 x = 0;
3801
3802 /* If there is a margin around the image, adjust x- and y-position
3803 by that margin. */
3804 if (s->img->margin)
3805 {
3806 x += s->img->margin;
3807 y += s->img->margin;
3808 }
dc43ef94 3809
06a2c219
GM
3810 if (s->img->pixmap)
3811 {
3812 if (s->img->mask)
3813 {
3814 /* We can't set both a clip mask and use XSetClipRectangles
3815 because the latter also sets a clip mask. We also can't
3816 trust on the shape extension to be available
3817 (XShapeCombineRegion). So, compute the rectangle to draw
3818 manually. */
3819 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3820 | GCFunction);
3821 XGCValues xgcv;
3822
3823 xgcv.clip_mask = s->img->mask;
3824 xgcv.clip_x_origin = x;
3825 xgcv.clip_y_origin = y;
3826 xgcv.function = GXcopy;
3827 XChangeGC (s->display, s->gc, mask, &xgcv);
3828
3829 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3830 0, 0, s->img->width, s->img->height, x, y);
3831 XSetClipMask (s->display, s->gc, None);
3832 }
3833 else
3834 {
3835 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3836 0, 0, s->img->width, s->img->height, x, y);
3837
3838 /* When the image has a mask, we can expect that at
3839 least part of a mouse highlight or a block cursor will
3840 be visible. If the image doesn't have a mask, make
3841 a block cursor visible by drawing a rectangle around
3842 the image. I believe it's looking better if we do
3843 nothing here for mouse-face. */
3844 if (s->hl == DRAW_CURSOR)
3845 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3846 s->img->width - 1, s->img->height - 1);
3847 }
3848 }
3849 else
3850 /* Draw a rectangle if image could not be loaded. */
3851 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3852 s->img->width - 1, s->img->height - 1);
3853}
dc43ef94 3854
990ba854 3855
06a2c219
GM
3856/* Draw part of the background of glyph string S. X, Y, W, and H
3857 give the rectangle to draw. */
a9a5b0a5 3858
06a2c219
GM
3859static void
3860x_draw_glyph_string_bg_rect (s, x, y, w, h)
3861 struct glyph_string *s;
3862 int x, y, w, h;
3863{
3864 if (s->stippled_p)
3865 {
3866 /* Fill background with a stipple pattern. */
3867 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3868 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3869 XSetFillStyle (s->display, s->gc, FillSolid);
3870 }
3871 else
3872 x_clear_glyph_string_rect (s, x, y, w, h);
3873}
07e34cb0 3874
b5210ea7 3875
06a2c219 3876/* Draw image glyph string S.
dc43ef94 3877
06a2c219
GM
3878 s->y
3879 s->x +-------------------------
3880 | s->face->box
3881 |
3882 | +-------------------------
3883 | | s->img->margin
3884 | |
3885 | | +-------------------
3886 | | | the image
dc43ef94 3887
06a2c219 3888 */
dc43ef94 3889
06a2c219
GM
3890static void
3891x_draw_image_glyph_string (s)
3892 struct glyph_string *s;
3893{
3894 int x, y;
3895 int box_line_width = s->face->box_line_width;
3896 int margin = s->img->margin;
3897 int height;
3898 Pixmap pixmap = None;
3899
3900 height = s->height - 2 * box_line_width;
3901
3902 /* Fill background with face under the image. Do it only if row is
3903 taller than image or if image has a clip mask to reduce
3904 flickering. */
3905 s->stippled_p = s->face->stipple != 0;
3906 if (height > s->img->height
3907 || margin
3908 || s->img->mask
3909 || s->img->pixmap == 0
3910 || s->width != s->background_width)
3911 {
3912 if (box_line_width && s->first_glyph->left_box_line_p)
3913 x = s->x + box_line_width;
3914 else
3915 x = s->x;
3916
3917 y = s->y + box_line_width;
3918
3919 if (s->img->mask)
3920 {
3921 /* Create a pixmap as large as the glyph string Fill it with
3922 the background color. Copy the image to it, using its
3923 mask. Copy the temporary pixmap to the display. */
3924 Screen *screen = FRAME_X_SCREEN (s->f);
3925 int depth = DefaultDepthOfScreen (screen);
3926
3927 /* Create a pixmap as large as the glyph string. */
3928 pixmap = XCreatePixmap (s->display, s->window,
3929 s->background_width,
3930 s->height, depth);
3931
3932 /* Don't clip in the following because we're working on the
3933 pixmap. */
3934 XSetClipMask (s->display, s->gc, None);
3935
3936 /* Fill the pixmap with the background color/stipple. */
3937 if (s->stippled_p)
3938 {
3939 /* Fill background with a stipple pattern. */
3940 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3941 XFillRectangle (s->display, pixmap, s->gc,
3942 0, 0, s->background_width, s->height);
3943 XSetFillStyle (s->display, s->gc, FillSolid);
3944 }
3945 else
3946 {
3947 XGCValues xgcv;
3948 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3949 &xgcv);
3950 XSetForeground (s->display, s->gc, xgcv.background);
3951 XFillRectangle (s->display, pixmap, s->gc,
3952 0, 0, s->background_width, s->height);
3953 XSetForeground (s->display, s->gc, xgcv.foreground);
3954 }
3955 }
3956 else
3957 /* Implementation idea: Is it possible to construct a mask?
3958 We could look at the color at the margins of the image, and
3959 say that this color is probably the background color of the
3960 image. */
3961 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
3962
3963 s->background_filled_p = 1;
3964 }
dc43ef94 3965
06a2c219
GM
3966 /* Draw the foreground. */
3967 if (pixmap != None)
3968 {
3969 x_draw_image_foreground_1 (s, pixmap);
3970 x_set_glyph_string_clipping (s);
3971 XCopyArea (s->display, pixmap, s->window, s->gc,
3972 0, 0, s->background_width, s->height, s->x, s->y);
3973 XFreePixmap (s->display, pixmap);
3974 }
3975 else
3976 x_draw_image_foreground (s);
b5210ea7 3977
06a2c219
GM
3978 /* If we must draw a relief around the image, do it. */
3979 if (s->img->relief
3980 || s->hl == DRAW_IMAGE_RAISED
3981 || s->hl == DRAW_IMAGE_SUNKEN)
3982 x_draw_image_relief (s);
3983}
8c1a6a84 3984
990ba854 3985
06a2c219 3986/* Draw stretch glyph string S. */
dc43ef94 3987
06a2c219
GM
3988static void
3989x_draw_stretch_glyph_string (s)
3990 struct glyph_string *s;
3991{
3992 xassert (s->first_glyph->type == STRETCH_GLYPH);
3993 s->stippled_p = s->face->stipple != 0;
990ba854 3994
06a2c219
GM
3995 if (s->hl == DRAW_CURSOR
3996 && !x_stretch_cursor_p)
3997 {
3998 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
3999 as wide as the stretch glyph. */
4000 int width = min (CANON_X_UNIT (s->f), s->background_width);
990ba854 4001
06a2c219
GM
4002 /* Draw cursor. */
4003 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
0cdd0c9f 4004
06a2c219
GM
4005 /* Clear rest using the GC of the original non-cursor face. */
4006 if (width < s->background_width)
4007 {
4008 GC gc = s->face->gc;
4009 int x = s->x + width, y = s->y;
4010 int w = s->background_width - width, h = s->height;
4011 XRectangle r;
dc43ef94 4012
06a2c219
GM
4013 x_get_glyph_string_clip_rect (s, &r);
4014 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
97210f4e 4015
06a2c219
GM
4016 if (s->face->stipple)
4017 {
4018 /* Fill background with a stipple pattern. */
4019 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4020 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4021 XSetFillStyle (s->display, gc, FillSolid);
4022 }
4023 else
4024 {
4025 XGCValues xgcv;
4026 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4027 XSetForeground (s->display, gc, xgcv.background);
4028 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4029 XSetForeground (s->display, gc, xgcv.foreground);
4030 }
4031 }
4032 }
4033 else
4034 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4035 s->height);
4036
4037 s->background_filled_p = 1;
4038}
4039
4040
4041/* Draw glyph string S. */
4042
4043static void
4044x_draw_glyph_string (s)
4045 struct glyph_string *s;
4046{
4047 /* If S draws into the background of its successor, draw the
4048 background of the successor first so that S can draw into it.
4049 This makes S->next use XDrawString instead of XDrawImageString. */
66ac4b0e 4050 if (s->next && s->right_overhang && !s->for_overlaps_p)
06a2c219
GM
4051 {
4052 xassert (s->next->img == NULL);
4053 x_set_glyph_string_gc (s->next);
4054 x_set_glyph_string_clipping (s->next);
4055 x_draw_glyph_string_background (s->next, 1);
4056 }
97210f4e 4057
06a2c219
GM
4058 /* Set up S->gc, set clipping and draw S. */
4059 x_set_glyph_string_gc (s);
4060 x_set_glyph_string_clipping (s);
4061
4062 switch (s->first_glyph->type)
4063 {
4064 case IMAGE_GLYPH:
4065 x_draw_image_glyph_string (s);
4066 break;
4067
4068 case STRETCH_GLYPH:
4069 x_draw_stretch_glyph_string (s);
4070 break;
4071
4072 case CHAR_GLYPH:
66ac4b0e
GM
4073 if (s->for_overlaps_p)
4074 s->background_filled_p = 1;
4075 else
4076 x_draw_glyph_string_background (s, 0);
06a2c219
GM
4077 x_draw_glyph_string_foreground (s);
4078 break;
4079
b4192550
KH
4080 case COMPOSITE_GLYPH:
4081 if (s->for_overlaps_p || s->gidx > 0)
4082 s->background_filled_p = 1;
4083 else
4084 x_draw_glyph_string_background (s, 1);
4085 x_draw_composite_glyph_string_foreground (s);
4086 break;
4087
06a2c219
GM
4088 default:
4089 abort ();
4090 }
4091
66ac4b0e 4092 if (!s->for_overlaps_p)
06a2c219 4093 {
66ac4b0e
GM
4094 /* Draw underline. */
4095 if (s->face->underline_p)
4096 {
4097 unsigned long dy, h;
06a2c219 4098
66ac4b0e
GM
4099 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4100 h = 1;
4101 if (!XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &dy))
4102 dy = s->height - h;
06a2c219 4103
66ac4b0e
GM
4104 if (s->face->underline_defaulted_p)
4105 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4106 s->width, h);
4107 else
4108 {
4109 XGCValues xgcv;
4110 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4111 XSetForeground (s->display, s->gc, s->face->underline_color);
4112 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4113 s->width, h);
4114 XSetForeground (s->display, s->gc, xgcv.foreground);
4115 }
dc6f92b8 4116 }
07e34cb0 4117
66ac4b0e
GM
4118 /* Draw overline. */
4119 if (s->face->overline_p)
06a2c219 4120 {
66ac4b0e
GM
4121 unsigned long dy = 0, h = 1;
4122
4123 if (s->face->overline_color_defaulted_p)
4124 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4125 s->width, h);
4126 else
4127 {
4128 XGCValues xgcv;
4129 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4130 XSetForeground (s->display, s->gc, s->face->overline_color);
4131 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4132 s->width, h);
4133 XSetForeground (s->display, s->gc, xgcv.foreground);
4134 }
06a2c219 4135 }
06a2c219 4136
66ac4b0e
GM
4137 /* Draw strike-through. */
4138 if (s->face->strike_through_p)
06a2c219 4139 {
66ac4b0e
GM
4140 unsigned long h = 1;
4141 unsigned long dy = (s->height - h) / 2;
4142
4143 if (s->face->strike_through_color_defaulted_p)
4144 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4145 s->width, h);
4146 else
4147 {
4148 XGCValues xgcv;
4149 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4150 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4151 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4152 s->width, h);
4153 XSetForeground (s->display, s->gc, xgcv.foreground);
4154 }
06a2c219 4155 }
06a2c219 4156
66ac4b0e
GM
4157 /* Draw relief. */
4158 if (s->face->box != FACE_NO_BOX)
4159 x_draw_glyph_string_box (s);
4160 }
06a2c219
GM
4161
4162 /* Reset clipping. */
4163 XSetClipMask (s->display, s->gc, None);
dc6f92b8 4164}
07e34cb0 4165
06a2c219 4166
b4192550
KH
4167static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4168 struct face **, int));
06a2c219 4169
06a2c219 4170
209f68d9
GM
4171/* Fill glyph string S with composition components specified by S->cmp.
4172
b4192550
KH
4173 FACES is an array of faces for all components of this composition.
4174 S->gidx is the index of the first component for S.
4175 OVERLAPS_P non-zero means S should draw the foreground only, and
209f68d9 4176 use its physical height for clipping.
06a2c219 4177
b4192550 4178 Value is the index of a component not in S. */
07e34cb0 4179
b4192550
KH
4180static int
4181x_fill_composite_glyph_string (s, faces, overlaps_p)
06a2c219 4182 struct glyph_string *s;
b4192550 4183 struct face **faces;
66ac4b0e 4184 int overlaps_p;
07e34cb0 4185{
b4192550 4186 int i;
06a2c219 4187
b4192550 4188 xassert (s);
06a2c219 4189
b4192550 4190 s->for_overlaps_p = overlaps_p;
06a2c219 4191
b4192550
KH
4192 s->face = faces[s->gidx];
4193 s->font = s->face->font;
4194 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
06a2c219 4195
b4192550
KH
4196 /* For all glyphs of this composition, starting at the offset
4197 S->gidx, until we reach the end of the definition or encounter a
4198 glyph that requires the different face, add it to S. */
4199 ++s->nchars;
4200 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4201 ++s->nchars;
06a2c219 4202
b4192550
KH
4203 /* All glyph strings for the same composition has the same width,
4204 i.e. the width set for the first component of the composition. */
06a2c219 4205
06a2c219
GM
4206 s->width = s->first_glyph->pixel_width;
4207
4208 /* If the specified font could not be loaded, use the frame's
4209 default font, but record the fact that we couldn't load it in
4210 the glyph string so that we can draw rectangles for the
4211 characters of the glyph string. */
4212 if (s->font == NULL)
4213 {
4214 s->font_not_found_p = 1;
4215 s->font = FRAME_FONT (s->f);
4216 }
4217
4218 /* Adjust base line for subscript/superscript text. */
4219 s->ybase += s->first_glyph->voffset;
4220
4221 xassert (s->face && s->face->gc);
4222
4223 /* This glyph string must always be drawn with 16-bit functions. */
4224 s->two_byte_p = 1;
b4192550
KH
4225
4226 return s->gidx + s->nchars;
06a2c219
GM
4227}
4228
4229
209f68d9
GM
4230/* Fill glyph string S from a sequence of character glyphs.
4231
06a2c219 4232 FACE_ID is the face id of the string. START is the index of the
66ac4b0e
GM
4233 first glyph to consider, END is the index of the last + 1.
4234 OVERLAPS_P non-zero means S should draw the foreground only, and
209f68d9 4235 use its physical height for clipping.
66ac4b0e
GM
4236
4237 Value is the index of the first glyph not in S. */
06a2c219
GM
4238
4239static int
66ac4b0e 4240x_fill_glyph_string (s, face_id, start, end, overlaps_p)
06a2c219
GM
4241 struct glyph_string *s;
4242 int face_id;
66ac4b0e 4243 int start, end, overlaps_p;
06a2c219
GM
4244{
4245 struct glyph *glyph, *last;
4246 int voffset;
ee569018 4247 int glyph_not_available_p;
06a2c219 4248
06a2c219
GM
4249 xassert (s->f == XFRAME (s->w->frame));
4250 xassert (s->nchars == 0);
4251 xassert (start >= 0 && end > start);
4252
66ac4b0e 4253 s->for_overlaps_p = overlaps_p,
06a2c219
GM
4254 glyph = s->row->glyphs[s->area] + start;
4255 last = s->row->glyphs[s->area] + end;
4256 voffset = glyph->voffset;
4257
ee569018
KH
4258 glyph_not_available_p = glyph->glyph_not_available_p;
4259
06a2c219
GM
4260 while (glyph < last
4261 && glyph->type == CHAR_GLYPH
4262 && glyph->voffset == voffset
ee569018
KH
4263 /* Same face id implies same font, nowadays. */
4264 && glyph->face_id == face_id
4265 && glyph->glyph_not_available_p == glyph_not_available_p)
06a2c219 4266 {
ee569018
KH
4267 int two_byte_p;
4268
06a2c219 4269 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
ee569018
KH
4270 s->char2b + s->nchars,
4271 &two_byte_p);
4272 s->two_byte_p = two_byte_p;
06a2c219
GM
4273 ++s->nchars;
4274 xassert (s->nchars <= end - start);
4275 s->width += glyph->pixel_width;
4276 ++glyph;
4277 }
4278
4279 s->font = s->face->font;
4280 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4281
4282 /* If the specified font could not be loaded, use the frame's font,
4283 but record the fact that we couldn't load it in
4284 S->font_not_found_p so that we can draw rectangles for the
4285 characters of the glyph string. */
ee569018 4286 if (s->font == NULL || glyph_not_available_p)
06a2c219
GM
4287 {
4288 s->font_not_found_p = 1;
4289 s->font = FRAME_FONT (s->f);
4290 }
4291
4292 /* Adjust base line for subscript/superscript text. */
4293 s->ybase += voffset;
66ac4b0e 4294
06a2c219
GM
4295 xassert (s->face && s->face->gc);
4296 return glyph - s->row->glyphs[s->area];
07e34cb0 4297}
dc6f92b8 4298
06a2c219
GM
4299
4300/* Fill glyph string S from image glyph S->first_glyph. */
dc6f92b8 4301
dfcf069d 4302static void
06a2c219
GM
4303x_fill_image_glyph_string (s)
4304 struct glyph_string *s;
4305{
4306 xassert (s->first_glyph->type == IMAGE_GLYPH);
43d120d8 4307 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
06a2c219 4308 xassert (s->img);
43d120d8 4309 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
06a2c219
GM
4310 s->font = s->face->font;
4311 s->width = s->first_glyph->pixel_width;
4312
4313 /* Adjust base line for subscript/superscript text. */
4314 s->ybase += s->first_glyph->voffset;
4315}
4316
4317
209f68d9 4318/* Fill glyph string S from a sequence of stretch glyphs.
06a2c219 4319
209f68d9
GM
4320 ROW is the glyph row in which the glyphs are found, AREA is the
4321 area within the row. START is the index of the first glyph to
4322 consider, END is the index of the last + 1.
4323
4324 Value is the index of the first glyph not in S. */
4325
4326static int
4327x_fill_stretch_glyph_string (s, row, area, start, end)
06a2c219 4328 struct glyph_string *s;
209f68d9
GM
4329 struct glyph_row *row;
4330 enum glyph_row_area area;
4331 int start, end;
06a2c219 4332{
209f68d9
GM
4333 struct glyph *glyph, *last;
4334 int voffset, face_id;
4335
06a2c219 4336 xassert (s->first_glyph->type == STRETCH_GLYPH);
209f68d9
GM
4337
4338 glyph = s->row->glyphs[s->area] + start;
4339 last = s->row->glyphs[s->area] + end;
4340 face_id = glyph->face_id;
4341 s->face = FACE_FROM_ID (s->f, face_id);
06a2c219 4342 s->font = s->face->font;
209f68d9
GM
4343 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4344 s->width = glyph->pixel_width;
4345 voffset = glyph->voffset;
4346
4347 for (++glyph;
4348 (glyph < last
4349 && glyph->type == STRETCH_GLYPH
4350 && glyph->voffset == voffset
4351 && glyph->face_id == face_id);
4352 ++glyph)
4353 s->width += glyph->pixel_width;
06a2c219
GM
4354
4355 /* Adjust base line for subscript/superscript text. */
209f68d9
GM
4356 s->ybase += voffset;
4357
4358 xassert (s->face && s->face->gc);
4359 return glyph - s->row->glyphs[s->area];
06a2c219
GM
4360}
4361
4362
4363/* Initialize glyph string S. CHAR2B is a suitably allocated vector
4364 of XChar2b structures for S; it can't be allocated in
4365 x_init_glyph_string because it must be allocated via `alloca'. W
4366 is the window on which S is drawn. ROW and AREA are the glyph row
4367 and area within the row from which S is constructed. START is the
4368 index of the first glyph structure covered by S. HL is a
4369 face-override for drawing S. */
4370
4371static void
4372x_init_glyph_string (s, char2b, w, row, area, start, hl)
4373 struct glyph_string *s;
4374 XChar2b *char2b;
4375 struct window *w;
4376 struct glyph_row *row;
4377 enum glyph_row_area area;
4378 int start;
4379 enum draw_glyphs_face hl;
4380{
4381 bzero (s, sizeof *s);
4382 s->w = w;
4383 s->f = XFRAME (w->frame);
4384 s->display = FRAME_X_DISPLAY (s->f);
4385 s->window = FRAME_X_WINDOW (s->f);
4386 s->char2b = char2b;
4387 s->hl = hl;
4388 s->row = row;
4389 s->area = area;
4390 s->first_glyph = row->glyphs[area] + start;
4391 s->height = row->height;
4392 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4393
9ea173e8
GM
4394 /* Display the internal border below the tool-bar window. */
4395 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219
GM
4396 s->y -= s->f->output_data.x->internal_border_width;
4397
4398 s->ybase = s->y + row->ascent;
4399}
4400
4401
4402/* Set background width of glyph string S. START is the index of the
4403 first glyph following S. LAST_X is the right-most x-position + 1
4404 in the drawing area. */
4405
4406static INLINE void
4407x_set_glyph_string_background_width (s, start, last_x)
4408 struct glyph_string *s;
4409 int start;
4410 int last_x;
4411{
4412 /* If the face of this glyph string has to be drawn to the end of
4413 the drawing area, set S->extends_to_end_of_line_p. */
4414 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4415
4416 if (start == s->row->used[s->area]
4417 && s->hl == DRAW_NORMAL_TEXT
4418 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4419 || s->face->background != default_face->background
4420 || s->face->stipple != default_face->stipple))
4421 s->extends_to_end_of_line_p = 1;
4422
4423 /* If S extends its face to the end of the line, set its
4424 background_width to the distance to the right edge of the drawing
4425 area. */
4426 if (s->extends_to_end_of_line_p)
1da3fd71 4427 s->background_width = last_x - s->x + 1;
06a2c219
GM
4428 else
4429 s->background_width = s->width;
4430}
4431
4432
4433/* Add a glyph string for a stretch glyph to the list of strings
4434 between HEAD and TAIL. START is the index of the stretch glyph in
4435 row area AREA of glyph row ROW. END is the index of the last glyph
4436 in that glyph row area. X is the current output position assigned
4437 to the new glyph string constructed. HL overrides that face of the
4438 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4439 is the right-most x-position of the drawing area. */
4440
8abee2e1
DL
4441/* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4442 and below -- keep them on one line. */
4443#define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4444 do \
4445 { \
4446 s = (struct glyph_string *) alloca (sizeof *s); \
4447 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
209f68d9 4448 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
06a2c219 4449 x_append_glyph_string (&HEAD, &TAIL, s); \
06a2c219
GM
4450 s->x = (X); \
4451 } \
4452 while (0)
4453
4454
4455/* Add a glyph string for an image glyph to the list of strings
4456 between HEAD and TAIL. START is the index of the image glyph in
4457 row area AREA of glyph row ROW. END is the index of the last glyph
4458 in that glyph row area. X is the current output position assigned
4459 to the new glyph string constructed. HL overrides that face of the
4460 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4461 is the right-most x-position of the drawing area. */
4462
8abee2e1 4463#define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4464 do \
4465 { \
4466 s = (struct glyph_string *) alloca (sizeof *s); \
4467 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4468 x_fill_image_glyph_string (s); \
4469 x_append_glyph_string (&HEAD, &TAIL, s); \
4470 ++START; \
4471 s->x = (X); \
4472 } \
4473 while (0)
4474
4475
4476/* Add a glyph string for a sequence of character glyphs to the list
4477 of strings between HEAD and TAIL. START is the index of the first
4478 glyph in row area AREA of glyph row ROW that is part of the new
4479 glyph string. END is the index of the last glyph in that glyph row
4480 area. X is the current output position assigned to the new glyph
4481 string constructed. HL overrides that face of the glyph; e.g. it
4482 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4483 right-most x-position of the drawing area. */
4484
8abee2e1 4485#define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4486 do \
4487 { \
3e71d8f2 4488 int c, face_id; \
06a2c219
GM
4489 XChar2b *char2b; \
4490 \
43d120d8 4491 c = (ROW)->glyphs[AREA][START].u.ch; \
43d120d8 4492 face_id = (ROW)->glyphs[AREA][START].face_id; \
06a2c219 4493 \
b4192550
KH
4494 s = (struct glyph_string *) alloca (sizeof *s); \
4495 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4496 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4497 x_append_glyph_string (&HEAD, &TAIL, s); \
b4192550
KH
4498 s->x = (X); \
4499 START = x_fill_glyph_string (s, face_id, START, END, \
66ac4b0e 4500 OVERLAPS_P); \
06a2c219
GM
4501 } \
4502 while (0)
4503
4504
b4192550
KH
4505/* Add a glyph string for a composite sequence to the list of strings
4506 between HEAD and TAIL. START is the index of the first glyph in
4507 row area AREA of glyph row ROW that is part of the new glyph
4508 string. END is the index of the last glyph in that glyph row area.
4509 X is the current output position assigned to the new glyph string
4510 constructed. HL overrides that face of the glyph; e.g. it is
4511 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4512 x-position of the drawing area. */
4513
6c27ec25 4514#define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
b4192550 4515 do { \
43d120d8
KH
4516 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4517 int face_id = (ROW)->glyphs[AREA][START].face_id; \
ee569018 4518 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
b4192550
KH
4519 struct composition *cmp = composition_table[cmp_id]; \
4520 int glyph_len = cmp->glyph_len; \
4521 XChar2b *char2b; \
4522 struct face **faces; \
4523 struct glyph_string *first_s = NULL; \
4524 int n; \
4525 \
ee569018 4526 base_face = base_face->ascii_face; \
b4192550
KH
4527 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4528 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4529 /* At first, fill in `char2b' and `faces'. */ \
4530 for (n = 0; n < glyph_len; n++) \
4531 { \
43d120d8 4532 int c = COMPOSITION_GLYPH (cmp, n); \
ee569018
KH
4533 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4534 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4535 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4536 this_face_id, char2b + n, 1); \
b4192550
KH
4537 } \
4538 \
4539 /* Make glyph_strings for each glyph sequence that is drawable by \
4540 the same face, and append them to HEAD/TAIL. */ \
4541 for (n = 0; n < cmp->glyph_len;) \
4542 { \
4543 s = (struct glyph_string *) alloca (sizeof *s); \
4544 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4545 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4546 s->cmp = cmp; \
4547 s->gidx = n; \
b4192550
KH
4548 s->x = (X); \
4549 \
4550 if (n == 0) \
4551 first_s = s; \
4552 \
4553 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4554 } \
4555 \
4556 ++START; \
4557 s = first_s; \
4558 } while (0)
4559
4560
06a2c219
GM
4561/* Build a list of glyph strings between HEAD and TAIL for the glyphs
4562 of AREA of glyph row ROW on window W between indices START and END.
4563 HL overrides the face for drawing glyph strings, e.g. it is
4564 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4565 x-positions of the drawing area.
4566
4567 This is an ugly monster macro construct because we must use alloca
4568 to allocate glyph strings (because x_draw_glyphs can be called
4569 asynchronously). */
4570
8abee2e1 4571#define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4572 do \
4573 { \
4574 HEAD = TAIL = NULL; \
4575 while (START < END) \
4576 { \
4577 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4578 switch (first_glyph->type) \
4579 { \
4580 case CHAR_GLYPH: \
4581 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
66ac4b0e
GM
4582 TAIL, HL, X, LAST_X, \
4583 OVERLAPS_P); \
06a2c219
GM
4584 break; \
4585 \
b4192550
KH
4586 case COMPOSITE_GLYPH: \
4587 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4588 HEAD, TAIL, HL, X, LAST_X,\
4589 OVERLAPS_P); \
4590 break; \
4591 \
06a2c219
GM
4592 case STRETCH_GLYPH: \
4593 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4594 HEAD, TAIL, HL, X, LAST_X); \
4595 break; \
4596 \
4597 case IMAGE_GLYPH: \
4598 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4599 TAIL, HL, X, LAST_X); \
4600 break; \
4601 \
4602 default: \
4603 abort (); \
4604 } \
4605 \
4606 x_set_glyph_string_background_width (s, START, LAST_X); \
4607 (X) += s->width; \
4608 } \
4609 } \
4610 while (0)
4611
4612
4613/* Draw glyphs between START and END in AREA of ROW on window W,
4614 starting at x-position X. X is relative to AREA in W. HL is a
4615 face-override with the following meaning:
4616
4617 DRAW_NORMAL_TEXT draw normally
4618 DRAW_CURSOR draw in cursor face
4619 DRAW_MOUSE_FACE draw in mouse face.
4620 DRAW_INVERSE_VIDEO draw in mode line face
4621 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4622 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4623
4624 If REAL_START is non-null, return in *REAL_START the real starting
4625 position for display. This can be different from START in case
4626 overlapping glyphs must be displayed. If REAL_END is non-null,
4627 return in *REAL_END the real end position for display. This can be
4628 different from END in case overlapping glyphs must be displayed.
4629
66ac4b0e
GM
4630 If OVERLAPS_P is non-zero, draw only the foreground of characters
4631 and clip to the physical height of ROW.
4632
06a2c219
GM
4633 Value is the x-position reached, relative to AREA of W. */
4634
4635static int
66ac4b0e
GM
4636x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4637 overlaps_p)
06a2c219
GM
4638 struct window *w;
4639 int x;
4640 struct glyph_row *row;
4641 enum glyph_row_area area;
4642 int start, end;
4643 enum draw_glyphs_face hl;
4644 int *real_start, *real_end;
66ac4b0e 4645 int overlaps_p;
dc6f92b8 4646{
06a2c219
GM
4647 struct glyph_string *head, *tail;
4648 struct glyph_string *s;
4649 int last_x, area_width;
4650 int x_reached;
4651 int i, j;
4652
4653 /* Let's rather be paranoid than getting a SEGV. */
4654 start = max (0, start);
4655 end = min (end, row->used[area]);
4656 if (real_start)
4657 *real_start = start;
4658 if (real_end)
4659 *real_end = end;
4660
4661 /* Translate X to frame coordinates. Set last_x to the right
4662 end of the drawing area. */
4663 if (row->full_width_p)
4664 {
4665 /* X is relative to the left edge of W, without scroll bars
4666 or flag areas. */
4667 struct frame *f = XFRAME (w->frame);
110859fc 4668 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
06a2c219 4669 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
dc6f92b8 4670
06a2c219
GM
4671 x += window_left_x;
4672 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4673 last_x = window_left_x + area_width;
4674
4675 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4676 {
110859fc 4677 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
06a2c219
GM
4678 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4679 last_x += width;
4680 else
4681 x -= width;
4682 }
dc6f92b8 4683
b9432a85
GM
4684 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4685 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
06a2c219
GM
4686 }
4687 else
dc6f92b8 4688 {
06a2c219
GM
4689 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4690 area_width = window_box_width (w, area);
4691 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
dc6f92b8
JB
4692 }
4693
06a2c219
GM
4694 /* Build a doubly-linked list of glyph_string structures between
4695 head and tail from what we have to draw. Note that the macro
4696 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4697 the reason we use a separate variable `i'. */
4698 i = start;
66ac4b0e
GM
4699 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4700 overlaps_p);
06a2c219
GM
4701 if (tail)
4702 x_reached = tail->x + tail->background_width;
4703 else
4704 x_reached = x;
90e65f07 4705
06a2c219
GM
4706 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4707 the row, redraw some glyphs in front or following the glyph
4708 strings built above. */
66ac4b0e 4709 if (!overlaps_p && row->contains_overlapping_glyphs_p)
06a2c219
GM
4710 {
4711 int dummy_x = 0;
4712 struct glyph_string *h, *t;
4713
4714 /* Compute overhangs for all glyph strings. */
4715 for (s = head; s; s = s->next)
4716 x_compute_glyph_string_overhangs (s);
4717
4718 /* Prepend glyph strings for glyphs in front of the first glyph
4719 string that are overwritten because of the first glyph
4720 string's left overhang. The background of all strings
4721 prepended must be drawn because the first glyph string
4722 draws over it. */
4723 i = x_left_overwritten (head);
4724 if (i >= 0)
4725 {
4726 j = i;
4727 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
66ac4b0e
GM
4728 DRAW_NORMAL_TEXT, dummy_x, last_x,
4729 overlaps_p);
06a2c219
GM
4730 start = i;
4731 if (real_start)
4732 *real_start = start;
4733 x_compute_overhangs_and_x (t, head->x, 1);
4734 x_prepend_glyph_string_lists (&head, &tail, h, t);
4735 }
58769bee 4736
06a2c219
GM
4737 /* Prepend glyph strings for glyphs in front of the first glyph
4738 string that overwrite that glyph string because of their
4739 right overhang. For these strings, only the foreground must
4740 be drawn, because it draws over the glyph string at `head'.
4741 The background must not be drawn because this would overwrite
4742 right overhangs of preceding glyphs for which no glyph
4743 strings exist. */
4744 i = x_left_overwriting (head);
4745 if (i >= 0)
4746 {
4747 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
66ac4b0e
GM
4748 DRAW_NORMAL_TEXT, dummy_x, last_x,
4749 overlaps_p);
06a2c219
GM
4750 for (s = h; s; s = s->next)
4751 s->background_filled_p = 1;
4752 if (real_start)
4753 *real_start = i;
4754 x_compute_overhangs_and_x (t, head->x, 1);
4755 x_prepend_glyph_string_lists (&head, &tail, h, t);
4756 }
dbcb258a 4757
06a2c219
GM
4758 /* Append glyphs strings for glyphs following the last glyph
4759 string tail that are overwritten by tail. The background of
4760 these strings has to be drawn because tail's foreground draws
4761 over it. */
4762 i = x_right_overwritten (tail);
4763 if (i >= 0)
4764 {
4765 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4766 DRAW_NORMAL_TEXT, x, last_x,
4767 overlaps_p);
06a2c219
GM
4768 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4769 x_append_glyph_string_lists (&head, &tail, h, t);
4770 if (real_end)
4771 *real_end = i;
4772 }
dc6f92b8 4773
06a2c219
GM
4774 /* Append glyph strings for glyphs following the last glyph
4775 string tail that overwrite tail. The foreground of such
4776 glyphs has to be drawn because it writes into the background
4777 of tail. The background must not be drawn because it could
4778 paint over the foreground of following glyphs. */
4779 i = x_right_overwriting (tail);
4780 if (i >= 0)
4781 {
4782 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4783 DRAW_NORMAL_TEXT, x, last_x,
4784 overlaps_p);
06a2c219
GM
4785 for (s = h; s; s = s->next)
4786 s->background_filled_p = 1;
4787 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4788 x_append_glyph_string_lists (&head, &tail, h, t);
4789 if (real_end)
4790 *real_end = i;
4791 }
4792 }
58769bee 4793
06a2c219
GM
4794 /* Draw all strings. */
4795 for (s = head; s; s = s->next)
4796 x_draw_glyph_string (s);
dc6f92b8 4797
06a2c219
GM
4798 /* Value is the x-position up to which drawn, relative to AREA of W.
4799 This doesn't include parts drawn because of overhangs. */
4800 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4801 if (!row->full_width_p)
4802 {
4803 if (area > LEFT_MARGIN_AREA)
4804 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4805 if (area > TEXT_AREA)
4806 x_reached -= window_box_width (w, TEXT_AREA);
4807 }
4808 return x_reached;
4809}
dc6f92b8 4810
dc6f92b8 4811
66ac4b0e
GM
4812/* Fix the display of area AREA of overlapping row ROW in window W. */
4813
4814static void
4815x_fix_overlapping_area (w, row, area)
4816 struct window *w;
4817 struct glyph_row *row;
4818 enum glyph_row_area area;
4819{
4820 int i, x;
4821
4822 BLOCK_INPUT;
4823
4824 if (area == LEFT_MARGIN_AREA)
4825 x = 0;
4826 else if (area == TEXT_AREA)
4827 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
4828 else
4829 x = (window_box_width (w, LEFT_MARGIN_AREA)
4830 + window_box_width (w, TEXT_AREA));
4831
4832 for (i = 0; i < row->used[area];)
4833 {
4834 if (row->glyphs[area][i].overlaps_vertically_p)
4835 {
4836 int start = i, start_x = x;
4837
4838 do
4839 {
4840 x += row->glyphs[area][i].pixel_width;
4841 ++i;
4842 }
4843 while (i < row->used[area]
4844 && row->glyphs[area][i].overlaps_vertically_p);
4845
4846 x_draw_glyphs (w, start_x, row, area, start, i,
4847 (row->inverse_p
4848 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4849 NULL, NULL, 1);
4850 }
4851 else
4852 {
4853 x += row->glyphs[area][i].pixel_width;
4854 ++i;
4855 }
4856 }
4857
4858 UNBLOCK_INPUT;
4859}
4860
4861
06a2c219
GM
4862/* Output LEN glyphs starting at START at the nominal cursor position.
4863 Advance the nominal cursor over the text. The global variable
4864 updated_window contains the window being updated, updated_row is
4865 the glyph row being updated, and updated_area is the area of that
4866 row being updated. */
dc6f92b8 4867
06a2c219
GM
4868static void
4869x_write_glyphs (start, len)
4870 struct glyph *start;
4871 int len;
4872{
4873 int x, hpos, real_start, real_end;
d9cdbb3d 4874
06a2c219 4875 xassert (updated_window && updated_row);
dc6f92b8 4876 BLOCK_INPUT;
06a2c219
GM
4877
4878 /* Write glyphs. */
dc6f92b8 4879
06a2c219
GM
4880 hpos = start - updated_row->glyphs[updated_area];
4881 x = x_draw_glyphs (updated_window, output_cursor.x,
4882 updated_row, updated_area,
4883 hpos, hpos + len,
4884 (updated_row->inverse_p
4885 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
66ac4b0e 4886 &real_start, &real_end, 0);
b30ec466 4887
06a2c219
GM
4888 /* If we drew over the cursor, note that it is not visible any more. */
4889 note_overwritten_text_cursor (updated_window, real_start,
4890 real_end - real_start);
dc6f92b8
JB
4891
4892 UNBLOCK_INPUT;
06a2c219
GM
4893
4894 /* Advance the output cursor. */
4895 output_cursor.hpos += len;
4896 output_cursor.x = x;
dc6f92b8
JB
4897}
4898
0cdd0c9f 4899
06a2c219 4900/* Insert LEN glyphs from START at the nominal cursor position. */
0cdd0c9f 4901
06a2c219
GM
4902static void
4903x_insert_glyphs (start, len)
4904 struct glyph *start;
4905 register int len;
4906{
4907 struct frame *f;
4908 struct window *w;
4909 int line_height, shift_by_width, shifted_region_width;
4910 struct glyph_row *row;
4911 struct glyph *glyph;
4912 int frame_x, frame_y, hpos, real_start, real_end;
58769bee 4913
06a2c219 4914 xassert (updated_window && updated_row);
0cdd0c9f 4915 BLOCK_INPUT;
06a2c219
GM
4916 w = updated_window;
4917 f = XFRAME (WINDOW_FRAME (w));
4918
4919 /* Get the height of the line we are in. */
4920 row = updated_row;
4921 line_height = row->height;
4922
4923 /* Get the width of the glyphs to insert. */
4924 shift_by_width = 0;
4925 for (glyph = start; glyph < start + len; ++glyph)
4926 shift_by_width += glyph->pixel_width;
4927
4928 /* Get the width of the region to shift right. */
4929 shifted_region_width = (window_box_width (w, updated_area)
4930 - output_cursor.x
4931 - shift_by_width);
4932
4933 /* Shift right. */
4934 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
4935 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
4936 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
4937 f->output_data.x->normal_gc,
4938 frame_x, frame_y,
4939 shifted_region_width, line_height,
4940 frame_x + shift_by_width, frame_y);
4941
4942 /* Write the glyphs. */
4943 hpos = start - row->glyphs[updated_area];
4944 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
66ac4b0e 4945 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
06a2c219
GM
4946 note_overwritten_text_cursor (w, real_start, real_end - real_start);
4947
4948 /* Advance the output cursor. */
4949 output_cursor.hpos += len;
4950 output_cursor.x += shift_by_width;
0cdd0c9f
RS
4951 UNBLOCK_INPUT;
4952}
0cdd0c9f 4953
0cdd0c9f 4954
06a2c219
GM
4955/* Delete N glyphs at the nominal cursor position. Not implemented
4956 for X frames. */
c83febd7
RS
4957
4958static void
06a2c219
GM
4959x_delete_glyphs (n)
4960 register int n;
c83febd7 4961{
06a2c219 4962 abort ();
c83febd7
RS
4963}
4964
0cdd0c9f 4965
06a2c219
GM
4966/* Erase the current text line from the nominal cursor position
4967 (inclusive) to pixel column TO_X (exclusive). The idea is that
4968 everything from TO_X onward is already erased.
4969
4970 TO_X is a pixel position relative to updated_area of
4971 updated_window. TO_X == -1 means clear to the end of this area. */
dc6f92b8 4972
06a2c219
GM
4973static void
4974x_clear_end_of_line (to_x)
4975 int to_x;
4976{
4977 struct frame *f;
4978 struct window *w = updated_window;
4979 int max_x, min_y, max_y;
4980 int from_x, from_y, to_y;
4981
4982 xassert (updated_window && updated_row);
4983 f = XFRAME (w->frame);
4984
4985 if (updated_row->full_width_p)
4986 {
4987 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
4988 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
4989 && !w->pseudo_window_p)
4990 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
0cdd0c9f 4991 }
06a2c219
GM
4992 else
4993 max_x = window_box_width (w, updated_area);
4994 max_y = window_text_bottom_y (w);
dc6f92b8 4995
06a2c219
GM
4996 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
4997 of window. For TO_X > 0, truncate to end of drawing area. */
4998 if (to_x == 0)
4999 return;
5000 else if (to_x < 0)
5001 to_x = max_x;
5002 else
5003 to_x = min (to_x, max_x);
dbc4e1c1 5004
06a2c219
GM
5005 to_y = min (max_y, output_cursor.y + updated_row->height);
5006
5007 /* Notice if the cursor will be cleared by this operation. */
5008 if (!updated_row->full_width_p)
5009 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
dbc4e1c1 5010
06a2c219
GM
5011 from_x = output_cursor.x;
5012
5013 /* Translate to frame coordinates. */
5014 if (updated_row->full_width_p)
5015 {
5016 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5017 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5018 }
0cdd0c9f
RS
5019 else
5020 {
06a2c219
GM
5021 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5022 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5023 }
5024
045dee35 5025 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
06a2c219
GM
5026 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5027 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5028
5029 /* Prevent inadvertently clearing to end of the X window. */
5030 if (to_x > from_x && to_y > from_y)
5031 {
5032 BLOCK_INPUT;
5033 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5034 from_x, from_y, to_x - from_x, to_y - from_y,
5035 False);
5036 UNBLOCK_INPUT;
0cdd0c9f 5037 }
0cdd0c9f 5038}
dbc4e1c1 5039
0cdd0c9f 5040
06a2c219 5041/* Clear entire frame. If updating_frame is non-null, clear that
b86bd3dd 5042 frame. Otherwise clear the selected frame. */
06a2c219
GM
5043
5044static void
5045x_clear_frame ()
0cdd0c9f 5046{
06a2c219 5047 struct frame *f;
0cdd0c9f 5048
06a2c219
GM
5049 if (updating_frame)
5050 f = updating_frame;
0cdd0c9f 5051 else
b86bd3dd 5052 f = SELECTED_FRAME ();
58769bee 5053
06a2c219
GM
5054 /* Clearing the frame will erase any cursor, so mark them all as no
5055 longer visible. */
5056 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5057 output_cursor.hpos = output_cursor.vpos = 0;
5058 output_cursor.x = -1;
5059
5060 /* We don't set the output cursor here because there will always
5061 follow an explicit cursor_to. */
5062 BLOCK_INPUT;
5063 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5064
5065 /* We have to clear the scroll bars, too. If we have changed
5066 colors or something like that, then they should be notified. */
5067 x_scroll_bar_clear (f);
0cdd0c9f 5068
06a2c219
GM
5069 XFlush (FRAME_X_DISPLAY (f));
5070 UNBLOCK_INPUT;
dc6f92b8 5071}
06a2c219
GM
5072
5073
dc6f92b8 5074\f
dbc4e1c1
JB
5075/* Invert the middle quarter of the frame for .15 sec. */
5076
06a2c219
GM
5077/* We use the select system call to do the waiting, so we have to make
5078 sure it's available. If it isn't, we just won't do visual bells. */
5079
dbc4e1c1
JB
5080#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5081
06a2c219
GM
5082
5083/* Subtract the `struct timeval' values X and Y, storing the result in
5084 *RESULT. Return 1 if the difference is negative, otherwise 0. */
dbc4e1c1
JB
5085
5086static int
5087timeval_subtract (result, x, y)
5088 struct timeval *result, x, y;
5089{
06a2c219
GM
5090 /* Perform the carry for the later subtraction by updating y. This
5091 is safer because on some systems the tv_sec member is unsigned. */
dbc4e1c1
JB
5092 if (x.tv_usec < y.tv_usec)
5093 {
5094 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5095 y.tv_usec -= 1000000 * nsec;
5096 y.tv_sec += nsec;
5097 }
06a2c219 5098
dbc4e1c1
JB
5099 if (x.tv_usec - y.tv_usec > 1000000)
5100 {
5101 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5102 y.tv_usec += 1000000 * nsec;
5103 y.tv_sec -= nsec;
5104 }
5105
06a2c219
GM
5106 /* Compute the time remaining to wait. tv_usec is certainly
5107 positive. */
dbc4e1c1
JB
5108 result->tv_sec = x.tv_sec - y.tv_sec;
5109 result->tv_usec = x.tv_usec - y.tv_usec;
5110
06a2c219
GM
5111 /* Return indication of whether the result should be considered
5112 negative. */
dbc4e1c1
JB
5113 return x.tv_sec < y.tv_sec;
5114}
dc6f92b8 5115
dfcf069d 5116void
f676886a
JB
5117XTflash (f)
5118 struct frame *f;
dc6f92b8 5119{
dbc4e1c1 5120 BLOCK_INPUT;
dc6f92b8 5121
dbc4e1c1
JB
5122 {
5123 GC gc;
dc6f92b8 5124
06a2c219
GM
5125 /* Create a GC that will use the GXxor function to flip foreground
5126 pixels into background pixels. */
dbc4e1c1
JB
5127 {
5128 XGCValues values;
dc6f92b8 5129
dbc4e1c1 5130 values.function = GXxor;
7556890b
RS
5131 values.foreground = (f->output_data.x->foreground_pixel
5132 ^ f->output_data.x->background_pixel);
58769bee 5133
334208b7 5134 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dbc4e1c1
JB
5135 GCFunction | GCForeground, &values);
5136 }
dc6f92b8 5137
dbc4e1c1 5138 {
e84e14c3
RS
5139 /* Get the height not including a menu bar widget. */
5140 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5141 /* Height of each line to flash. */
5142 int flash_height = FRAME_LINE_HEIGHT (f);
5143 /* These will be the left and right margins of the rectangles. */
5144 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5145 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5146
5147 int width;
5148
5149 /* Don't flash the area between a scroll bar and the frame
5150 edge it is next to. */
5151 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5152 {
5153 case vertical_scroll_bar_left:
5154 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5155 break;
5156
5157 case vertical_scroll_bar_right:
5158 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5159 break;
06a2c219
GM
5160
5161 default:
5162 break;
e84e14c3
RS
5163 }
5164
5165 width = flash_right - flash_left;
5166
5167 /* If window is tall, flash top and bottom line. */
5168 if (height > 3 * FRAME_LINE_HEIGHT (f))
5169 {
5170 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5171 flash_left,
5172 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5173 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5174 width, flash_height);
5175 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5176 flash_left,
5177 (height - flash_height
5178 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5179 width, flash_height);
5180 }
5181 else
5182 /* If it is short, flash it all. */
5183 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5184 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5185 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
dc6f92b8 5186
06a2c219 5187 x_flush (f);
dc6f92b8 5188
dbc4e1c1 5189 {
06a2c219 5190 struct timeval wakeup;
dc6f92b8 5191
66c30ea1 5192 EMACS_GET_TIME (wakeup);
dc6f92b8 5193
dbc4e1c1
JB
5194 /* Compute time to wait until, propagating carry from usecs. */
5195 wakeup.tv_usec += 150000;
5196 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5197 wakeup.tv_usec %= 1000000;
5198
5199 /* Keep waiting until past the time wakeup. */
5200 while (1)
5201 {
5202 struct timeval timeout;
5203
66c30ea1 5204 EMACS_GET_TIME (timeout);
dbc4e1c1
JB
5205
5206 /* In effect, timeout = wakeup - timeout.
5207 Break if result would be negative. */
5208 if (timeval_subtract (&timeout, wakeup, timeout))
5209 break;
5210
5211 /* Try to wait that long--but we might wake up sooner. */
c32cdd9a 5212 select (0, NULL, NULL, NULL, &timeout);
dbc4e1c1
JB
5213 }
5214 }
58769bee 5215
e84e14c3
RS
5216 /* If window is tall, flash top and bottom line. */
5217 if (height > 3 * FRAME_LINE_HEIGHT (f))
5218 {
5219 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5220 flash_left,
5221 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5222 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5223 width, flash_height);
5224 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5225 flash_left,
5226 (height - flash_height
5227 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5228 width, flash_height);
5229 }
5230 else
5231 /* If it is short, flash it all. */
5232 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5233 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5234 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5235
334208b7 5236 XFreeGC (FRAME_X_DISPLAY (f), gc);
06a2c219 5237 x_flush (f);
dc6f92b8 5238 }
dbc4e1c1
JB
5239 }
5240
5241 UNBLOCK_INPUT;
dc6f92b8
JB
5242}
5243
06a2c219 5244#endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
dbc4e1c1
JB
5245
5246
dc6f92b8
JB
5247/* Make audible bell. */
5248
dfcf069d 5249void
dc6f92b8
JB
5250XTring_bell ()
5251{
b86bd3dd
GM
5252 struct frame *f = SELECTED_FRAME ();
5253
5254 if (FRAME_X_DISPLAY (f))
5255 {
dbc4e1c1 5256#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
b86bd3dd
GM
5257 if (visible_bell)
5258 XTflash (f);
5259 else
dbc4e1c1 5260#endif
b86bd3dd
GM
5261 {
5262 BLOCK_INPUT;
5263 XBell (FRAME_X_DISPLAY (f), 0);
5264 XFlush (FRAME_X_DISPLAY (f));
5265 UNBLOCK_INPUT;
5266 }
dc6f92b8
JB
5267 }
5268}
06a2c219 5269
dc6f92b8 5270\f
06a2c219
GM
5271/* Specify how many text lines, from the top of the window,
5272 should be affected by insert-lines and delete-lines operations.
5273 This, and those operations, are used only within an update
5274 that is bounded by calls to x_update_begin and x_update_end. */
dc6f92b8 5275
dfcf069d 5276static void
06a2c219
GM
5277XTset_terminal_window (n)
5278 register int n;
dc6f92b8 5279{
06a2c219 5280 /* This function intentionally left blank. */
dc6f92b8
JB
5281}
5282
06a2c219
GM
5283
5284\f
5285/***********************************************************************
5286 Line Dance
5287 ***********************************************************************/
5288
5289/* Perform an insert-lines or delete-lines operation, inserting N
5290 lines or deleting -N lines at vertical position VPOS. */
5291
dfcf069d 5292static void
06a2c219
GM
5293x_ins_del_lines (vpos, n)
5294 int vpos, n;
dc6f92b8
JB
5295{
5296 abort ();
5297}
06a2c219
GM
5298
5299
5300/* Scroll part of the display as described by RUN. */
dc6f92b8 5301
dfcf069d 5302static void
06a2c219
GM
5303x_scroll_run (w, run)
5304 struct window *w;
5305 struct run *run;
dc6f92b8 5306{
06a2c219
GM
5307 struct frame *f = XFRAME (w->frame);
5308 int x, y, width, height, from_y, to_y, bottom_y;
5309
5310 /* Get frame-relative bounding box of the text display area of W,
5311 without mode lines. Include in this box the flags areas to the
5312 left and right of W. */
5313 window_box (w, -1, &x, &y, &width, &height);
110859fc
GM
5314 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5315 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
5316
5317 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5318 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5319 bottom_y = y + height;
dc6f92b8 5320
06a2c219
GM
5321 if (to_y < from_y)
5322 {
5323 /* Scrolling up. Make sure we don't copy part of the mode
5324 line at the bottom. */
5325 if (from_y + run->height > bottom_y)
5326 height = bottom_y - from_y;
5327 else
5328 height = run->height;
5329 }
dc6f92b8 5330 else
06a2c219
GM
5331 {
5332 /* Scolling down. Make sure we don't copy over the mode line.
5333 at the bottom. */
5334 if (to_y + run->height > bottom_y)
5335 height = bottom_y - to_y;
5336 else
5337 height = run->height;
5338 }
7a13e894 5339
06a2c219
GM
5340 BLOCK_INPUT;
5341
5342 /* Cursor off. Will be switched on again in x_update_window_end. */
5343 updated_window = w;
5344 x_clear_cursor (w);
5345
5346 XCopyArea (FRAME_X_DISPLAY (f),
5347 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5348 f->output_data.x->normal_gc,
5349 x, from_y,
5350 width, height,
5351 x, to_y);
5352
5353 UNBLOCK_INPUT;
5354}
dc6f92b8 5355
dc6f92b8 5356
06a2c219
GM
5357\f
5358/***********************************************************************
5359 Exposure Events
5360 ***********************************************************************/
5361
5362/* Redisplay an exposed area of frame F. X and Y are the upper-left
5363 corner of the exposed rectangle. W and H are width and height of
5364 the exposed area. All are pixel values. W or H zero means redraw
5365 the entire frame. */
dc6f92b8 5366
06a2c219
GM
5367static void
5368expose_frame (f, x, y, w, h)
5369 struct frame *f;
5370 int x, y, w, h;
dc6f92b8 5371{
06a2c219 5372 XRectangle r;
dc6f92b8 5373
06a2c219 5374 TRACE ((stderr, "expose_frame "));
dc6f92b8 5375
06a2c219
GM
5376 /* No need to redraw if frame will be redrawn soon. */
5377 if (FRAME_GARBAGED_P (f))
dc6f92b8 5378 {
06a2c219
GM
5379 TRACE ((stderr, " garbaged\n"));
5380 return;
5381 }
5382
5383 /* If basic faces haven't been realized yet, there is no point in
5384 trying to redraw anything. This can happen when we get an expose
5385 event while Emacs is starting, e.g. by moving another window. */
5386 if (FRAME_FACE_CACHE (f) == NULL
5387 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5388 {
5389 TRACE ((stderr, " no faces\n"));
5390 return;
58769bee 5391 }
06a2c219
GM
5392
5393 if (w == 0 || h == 0)
58769bee 5394 {
06a2c219
GM
5395 r.x = r.y = 0;
5396 r.width = CANON_X_UNIT (f) * f->width;
5397 r.height = CANON_Y_UNIT (f) * f->height;
dc6f92b8
JB
5398 }
5399 else
5400 {
06a2c219
GM
5401 r.x = x;
5402 r.y = y;
5403 r.width = w;
5404 r.height = h;
5405 }
5406
5407 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5408 expose_window_tree (XWINDOW (f->root_window), &r);
5409
9ea173e8 5410 if (WINDOWP (f->tool_bar_window))
06a2c219 5411 {
9ea173e8 5412 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
5413 XRectangle window_rect;
5414 XRectangle intersection_rect;
5415 int window_x, window_y, window_width, window_height;
5416
5417
5418 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5419 window_rect.x = window_x;
5420 window_rect.y = window_y;
5421 window_rect.width = window_width;
5422 window_rect.height = window_height;
5423
5424 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5425 expose_window (w, &intersection_rect);
5426 }
5427
5428#ifndef USE_X_TOOLKIT
5429 if (WINDOWP (f->menu_bar_window))
5430 {
5431 struct window *w = XWINDOW (f->menu_bar_window);
5432 XRectangle window_rect;
5433 XRectangle intersection_rect;
5434 int window_x, window_y, window_width, window_height;
5435
5436
5437 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5438 window_rect.x = window_x;
5439 window_rect.y = window_y;
5440 window_rect.width = window_width;
5441 window_rect.height = window_height;
5442
5443 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5444 expose_window (w, &intersection_rect);
dc6f92b8 5445 }
06a2c219 5446#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
5447}
5448
06a2c219
GM
5449
5450/* Redraw (parts) of all windows in the window tree rooted at W that
5451 intersect R. R contains frame pixel coordinates. */
5452
58769bee 5453static void
06a2c219
GM
5454expose_window_tree (w, r)
5455 struct window *w;
5456 XRectangle *r;
dc6f92b8 5457{
06a2c219
GM
5458 while (w)
5459 {
5460 if (!NILP (w->hchild))
5461 expose_window_tree (XWINDOW (w->hchild), r);
5462 else if (!NILP (w->vchild))
5463 expose_window_tree (XWINDOW (w->vchild), r);
5464 else
5465 {
5466 XRectangle window_rect;
5467 XRectangle intersection_rect;
5468 struct frame *f = XFRAME (w->frame);
5469 int window_x, window_y, window_width, window_height;
5470
5471 /* Frame-relative pixel rectangle of W. */
5472 window_box (w, -1, &window_x, &window_y, &window_width,
5473 &window_height);
5474 window_rect.x
5475 = (window_x
110859fc 5476 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
714dc26c 5477 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
06a2c219
GM
5478 window_rect.y = window_y;
5479 window_rect.width
5480 = (window_width
110859fc 5481 + FRAME_X_FLAGS_AREA_WIDTH (f)
06a2c219
GM
5482 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5483 window_rect.height
5484 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5485
5486 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5487 expose_window (w, &intersection_rect);
5488 }
58769bee 5489
06a2c219
GM
5490 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5491 }
5492}
58769bee 5493
dc6f92b8 5494
06a2c219
GM
5495/* Redraw the part of glyph row area AREA of glyph row ROW on window W
5496 which intersects rectangle R. R is in window-relative coordinates. */
5497
5498static void
5499expose_area (w, row, r, area)
5500 struct window *w;
5501 struct glyph_row *row;
5502 XRectangle *r;
5503 enum glyph_row_area area;
5504{
5505 int x;
5506 struct glyph *first = row->glyphs[area];
5507 struct glyph *end = row->glyphs[area] + row->used[area];
5508 struct glyph *last;
5509 int first_x;
5510
5511 /* Set x to the window-relative start position for drawing glyphs of
5512 AREA. The first glyph of the text area can be partially visible.
5513 The first glyphs of other areas cannot. */
5514 if (area == LEFT_MARGIN_AREA)
5515 x = 0;
5516 else if (area == TEXT_AREA)
5517 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5518 else
5519 x = (window_box_width (w, LEFT_MARGIN_AREA)
5520 + window_box_width (w, TEXT_AREA));
5521
6fb13182
GM
5522 if (area == TEXT_AREA && row->fill_line_p)
5523 /* If row extends face to end of line write the whole line. */
5524 x_draw_glyphs (w, x, row, area,
5525 0, row->used[area],
06a2c219 5526 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5527 NULL, NULL, 0);
6fb13182
GM
5528 else
5529 {
5530 /* Find the first glyph that must be redrawn. */
5531 while (first < end
5532 && x + first->pixel_width < r->x)
5533 {
5534 x += first->pixel_width;
5535 ++first;
5536 }
5537
5538 /* Find the last one. */
5539 last = first;
5540 first_x = x;
5541 while (last < end
5542 && x < r->x + r->width)
5543 {
5544 x += last->pixel_width;
5545 ++last;
5546 }
5547
5548 /* Repaint. */
5549 if (last > first)
5550 x_draw_glyphs (w, first_x, row, area,
5551 first - row->glyphs[area],
5552 last - row->glyphs[area],
5553 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5554 NULL, NULL, 0);
5555 }
06a2c219
GM
5556}
5557
58769bee 5558
06a2c219
GM
5559/* Redraw the parts of the glyph row ROW on window W intersecting
5560 rectangle R. R is in window-relative coordinates. */
dc6f92b8 5561
06a2c219
GM
5562static void
5563expose_line (w, row, r)
5564 struct window *w;
5565 struct glyph_row *row;
5566 XRectangle *r;
5567{
5568 xassert (row->enabled_p);
5569
5570 if (row->mode_line_p || w->pseudo_window_p)
5571 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5572 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5573 NULL, NULL, 0);
06a2c219
GM
5574 else
5575 {
5576 if (row->used[LEFT_MARGIN_AREA])
5577 expose_area (w, row, r, LEFT_MARGIN_AREA);
5578 if (row->used[TEXT_AREA])
5579 expose_area (w, row, r, TEXT_AREA);
5580 if (row->used[RIGHT_MARGIN_AREA])
5581 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5582 x_draw_row_bitmaps (w, row);
5583 }
5584}
dc6f92b8 5585
58769bee 5586
06a2c219
GM
5587/* Return non-zero if W's cursor intersects rectangle R. */
5588
5589static int
5590x_phys_cursor_in_rect_p (w, r)
5591 struct window *w;
5592 XRectangle *r;
5593{
5594 XRectangle cr, result;
5595 struct glyph *cursor_glyph;
5596
5597 cursor_glyph = get_phys_cursor_glyph (w);
5598 if (cursor_glyph)
5599 {
5600 cr.x = w->phys_cursor.x;
5601 cr.y = w->phys_cursor.y;
5602 cr.width = cursor_glyph->pixel_width;
5603 cr.height = w->phys_cursor_height;
5604 return x_intersect_rectangles (&cr, r, &result);
5605 }
5606 else
5607 return 0;
dc6f92b8 5608}
dc6f92b8 5609
06a2c219
GM
5610
5611/* Redraw a rectangle of window W. R is a rectangle in window
5612 relative coordinates. Call this function with input blocked. */
dc6f92b8
JB
5613
5614static void
06a2c219
GM
5615expose_window (w, r)
5616 struct window *w;
5617 XRectangle *r;
dc6f92b8 5618{
06a2c219
GM
5619 struct glyph_row *row;
5620 int y;
5621 int yb = window_text_bottom_y (w);
5622 int cursor_cleared_p;
dc6f92b8 5623
80c32bcc
GM
5624 /* If window is not yet fully initialized, do nothing. This can
5625 happen when toolkit scroll bars are used and a window is split.
5626 Reconfiguring the scroll bar will generate an expose for a newly
5627 created window. */
5628 if (w->current_matrix == NULL)
5629 return;
5630
06a2c219
GM
5631 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5632 r->x, r->y, r->width, r->height));
dc6f92b8 5633
06a2c219
GM
5634 /* Convert to window coordinates. */
5635 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5636 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
dc6f92b8 5637
06a2c219
GM
5638 /* Turn off the cursor. */
5639 if (!w->pseudo_window_p
5640 && x_phys_cursor_in_rect_p (w, r))
5641 {
5642 x_clear_cursor (w);
5643 cursor_cleared_p = 1;
5644 }
5645 else
5646 cursor_cleared_p = 0;
5647
5648 /* Find the first row intersecting the rectangle R. */
5649 row = w->current_matrix->rows;
5650 y = 0;
5651 while (row->enabled_p
5652 && y < yb
5653 && y + row->height < r->y)
5654 {
5655 y += row->height;
5656 ++row;
5657 }
5658
dc6f92b8 5659 /* Display the text in the rectangle, one text line at a time. */
06a2c219
GM
5660 while (row->enabled_p
5661 && y < yb
5662 && y < r->y + r->height)
5663 {
5664 expose_line (w, row, r);
5665 y += row->height;
5666 ++row;
5667 }
5668
5669 /* Display the mode line if there is one. */
5670 if (WINDOW_WANTS_MODELINE_P (w)
5671 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5672 row->enabled_p)
5673 && row->y < r->y + r->height)
5674 expose_line (w, row, r);
dc6f92b8 5675
06a2c219 5676 if (!w->pseudo_window_p)
dc6f92b8 5677 {
06a2c219
GM
5678 /* Draw border between windows. */
5679 x_draw_vertical_border (w);
5680
5681 /* Turn the cursor on again. */
5682 if (cursor_cleared_p)
5683 x_update_window_cursor (w, 1);
5684 }
5685}
dc6f92b8 5686
dc6f92b8 5687
06a2c219
GM
5688/* Determine the intersection of two rectangles R1 and R2. Return
5689 the intersection in *RESULT. Value is non-zero if RESULT is not
5690 empty. */
5691
5692static int
5693x_intersect_rectangles (r1, r2, result)
5694 XRectangle *r1, *r2, *result;
5695{
5696 XRectangle *left, *right;
5697 XRectangle *upper, *lower;
5698 int intersection_p = 0;
5699
5700 /* Rearrange so that R1 is the left-most rectangle. */
5701 if (r1->x < r2->x)
5702 left = r1, right = r2;
5703 else
5704 left = r2, right = r1;
5705
5706 /* X0 of the intersection is right.x0, if this is inside R1,
5707 otherwise there is no intersection. */
5708 if (right->x <= left->x + left->width)
5709 {
5710 result->x = right->x;
5711
5712 /* The right end of the intersection is the minimum of the
5713 the right ends of left and right. */
5714 result->width = (min (left->x + left->width, right->x + right->width)
5715 - result->x);
5716
5717 /* Same game for Y. */
5718 if (r1->y < r2->y)
5719 upper = r1, lower = r2;
5720 else
5721 upper = r2, lower = r1;
5722
5723 /* The upper end of the intersection is lower.y0, if this is inside
5724 of upper. Otherwise, there is no intersection. */
5725 if (lower->y <= upper->y + upper->height)
dc43ef94 5726 {
06a2c219
GM
5727 result->y = lower->y;
5728
5729 /* The lower end of the intersection is the minimum of the lower
5730 ends of upper and lower. */
5731 result->height = (min (lower->y + lower->height,
5732 upper->y + upper->height)
5733 - result->y);
5734 intersection_p = 1;
dc43ef94 5735 }
dc6f92b8
JB
5736 }
5737
06a2c219 5738 return intersection_p;
dc6f92b8 5739}
06a2c219
GM
5740
5741
5742
5743
dc6f92b8 5744\f
dc6f92b8 5745static void
334208b7
RS
5746frame_highlight (f)
5747 struct frame *f;
dc6f92b8 5748{
b3e1e05c
JB
5749 /* We used to only do this if Vx_no_window_manager was non-nil, but
5750 the ICCCM (section 4.1.6) says that the window's border pixmap
5751 and border pixel are window attributes which are "private to the
5752 client", so we can always change it to whatever we want. */
5753 BLOCK_INPUT;
334208b7 5754 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5755 f->output_data.x->border_pixel);
b3e1e05c 5756 UNBLOCK_INPUT;
5d46f928 5757 x_update_cursor (f, 1);
dc6f92b8
JB
5758}
5759
5760static void
334208b7
RS
5761frame_unhighlight (f)
5762 struct frame *f;
dc6f92b8 5763{
b3e1e05c
JB
5764 /* We used to only do this if Vx_no_window_manager was non-nil, but
5765 the ICCCM (section 4.1.6) says that the window's border pixmap
5766 and border pixel are window attributes which are "private to the
5767 client", so we can always change it to whatever we want. */
5768 BLOCK_INPUT;
334208b7 5769 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5770 f->output_data.x->border_tile);
b3e1e05c 5771 UNBLOCK_INPUT;
5d46f928 5772 x_update_cursor (f, 1);
dc6f92b8 5773}
dc6f92b8 5774
f676886a
JB
5775/* The focus has changed. Update the frames as necessary to reflect
5776 the new situation. Note that we can't change the selected frame
c5acd733 5777 here, because the Lisp code we are interrupting might become confused.
eb8c3be9 5778 Each event gets marked with the frame in which it occurred, so the
c5acd733 5779 Lisp code can tell when the switch took place by examining the events. */
dc6f92b8 5780
6d4238f3 5781static void
0f941935
KH
5782x_new_focus_frame (dpyinfo, frame)
5783 struct x_display_info *dpyinfo;
f676886a 5784 struct frame *frame;
dc6f92b8 5785{
0f941935 5786 struct frame *old_focus = dpyinfo->x_focus_frame;
dc6f92b8 5787
0f941935 5788 if (frame != dpyinfo->x_focus_frame)
dc6f92b8 5789 {
58769bee 5790 /* Set this before calling other routines, so that they see
f676886a 5791 the correct value of x_focus_frame. */
0f941935 5792 dpyinfo->x_focus_frame = frame;
6d4238f3
JB
5793
5794 if (old_focus && old_focus->auto_lower)
f676886a 5795 x_lower_frame (old_focus);
dc6f92b8
JB
5796
5797#if 0
f676886a 5798 selected_frame = frame;
e0c1aef2
KH
5799 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
5800 selected_frame);
f676886a
JB
5801 Fselect_window (selected_frame->selected_window);
5802 choose_minibuf_frame ();
c118dd06 5803#endif /* ! 0 */
dc6f92b8 5804
0f941935
KH
5805 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
5806 pending_autoraise_frame = dpyinfo->x_focus_frame;
0134a210
RS
5807 else
5808 pending_autoraise_frame = 0;
6d4238f3 5809 }
dc6f92b8 5810
0f941935 5811 x_frame_rehighlight (dpyinfo);
6d4238f3
JB
5812}
5813
37c2c98b
RS
5814/* Handle an event saying the mouse has moved out of an Emacs frame. */
5815
5816void
0f941935
KH
5817x_mouse_leave (dpyinfo)
5818 struct x_display_info *dpyinfo;
37c2c98b 5819{
0f941935 5820 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
37c2c98b 5821}
6d4238f3 5822
f451eb13
JB
5823/* The focus has changed, or we have redirected a frame's focus to
5824 another frame (this happens when a frame uses a surrogate
06a2c219 5825 mini-buffer frame). Shift the highlight as appropriate.
0f941935
KH
5826
5827 The FRAME argument doesn't necessarily have anything to do with which
06a2c219 5828 frame is being highlighted or un-highlighted; we only use it to find
0f941935 5829 the appropriate X display info. */
06a2c219 5830
6d4238f3 5831static void
0f941935
KH
5832XTframe_rehighlight (frame)
5833 struct frame *frame;
6d4238f3 5834{
0f941935
KH
5835 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
5836}
6d4238f3 5837
0f941935
KH
5838static void
5839x_frame_rehighlight (dpyinfo)
5840 struct x_display_info *dpyinfo;
5841{
5842 struct frame *old_highlight = dpyinfo->x_highlight_frame;
5843
5844 if (dpyinfo->x_focus_frame)
6d4238f3 5845 {
0f941935
KH
5846 dpyinfo->x_highlight_frame
5847 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
5848 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
5849 : dpyinfo->x_focus_frame);
5850 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
f451eb13 5851 {
0f941935
KH
5852 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
5853 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
f451eb13 5854 }
dc6f92b8 5855 }
6d4238f3 5856 else
0f941935 5857 dpyinfo->x_highlight_frame = 0;
dc6f92b8 5858
0f941935 5859 if (dpyinfo->x_highlight_frame != old_highlight)
6d4238f3
JB
5860 {
5861 if (old_highlight)
f676886a 5862 frame_unhighlight (old_highlight);
0f941935
KH
5863 if (dpyinfo->x_highlight_frame)
5864 frame_highlight (dpyinfo->x_highlight_frame);
6d4238f3 5865 }
dc6f92b8 5866}
06a2c219
GM
5867
5868
dc6f92b8 5869\f
06a2c219 5870/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
dc6f92b8 5871
28430d3c
JB
5872/* Initialize mode_switch_bit and modifier_meaning. */
5873static void
334208b7
RS
5874x_find_modifier_meanings (dpyinfo)
5875 struct x_display_info *dpyinfo;
28430d3c 5876{
f689eb05 5877 int min_code, max_code;
28430d3c
JB
5878 KeySym *syms;
5879 int syms_per_code;
5880 XModifierKeymap *mods;
5881
334208b7
RS
5882 dpyinfo->meta_mod_mask = 0;
5883 dpyinfo->shift_lock_mask = 0;
5884 dpyinfo->alt_mod_mask = 0;
5885 dpyinfo->super_mod_mask = 0;
5886 dpyinfo->hyper_mod_mask = 0;
58769bee 5887
9658a521 5888#ifdef HAVE_X11R4
334208b7 5889 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
9658a521 5890#else
4a60f8c5
RS
5891 min_code = dpyinfo->display->min_keycode;
5892 max_code = dpyinfo->display->max_keycode;
9658a521
JB
5893#endif
5894
334208b7 5895 syms = XGetKeyboardMapping (dpyinfo->display,
28430d3c
JB
5896 min_code, max_code - min_code + 1,
5897 &syms_per_code);
334208b7 5898 mods = XGetModifierMapping (dpyinfo->display);
28430d3c 5899
58769bee 5900 /* Scan the modifier table to see which modifier bits the Meta and
11edeb03 5901 Alt keysyms are on. */
28430d3c 5902 {
06a2c219 5903 int row, col; /* The row and column in the modifier table. */
28430d3c
JB
5904
5905 for (row = 3; row < 8; row++)
5906 for (col = 0; col < mods->max_keypermod; col++)
5907 {
0299d313
RS
5908 KeyCode code
5909 = mods->modifiermap[(row * mods->max_keypermod) + col];
28430d3c 5910
af92970c
KH
5911 /* Zeroes are used for filler. Skip them. */
5912 if (code == 0)
5913 continue;
5914
28430d3c
JB
5915 /* Are any of this keycode's keysyms a meta key? */
5916 {
5917 int code_col;
5918
5919 for (code_col = 0; code_col < syms_per_code; code_col++)
5920 {
f689eb05 5921 int sym = syms[((code - min_code) * syms_per_code) + code_col];
28430d3c 5922
f689eb05 5923 switch (sym)
28430d3c 5924 {
f689eb05
JB
5925 case XK_Meta_L:
5926 case XK_Meta_R:
334208b7 5927 dpyinfo->meta_mod_mask |= (1 << row);
28430d3c 5928 break;
f689eb05
JB
5929
5930 case XK_Alt_L:
5931 case XK_Alt_R:
334208b7 5932 dpyinfo->alt_mod_mask |= (1 << row);
a3c44b14
RS
5933 break;
5934
5935 case XK_Hyper_L:
5936 case XK_Hyper_R:
334208b7 5937 dpyinfo->hyper_mod_mask |= (1 << row);
a3c44b14
RS
5938 break;
5939
5940 case XK_Super_L:
5941 case XK_Super_R:
334208b7 5942 dpyinfo->super_mod_mask |= (1 << row);
f689eb05 5943 break;
11edeb03
JB
5944
5945 case XK_Shift_Lock:
5946 /* Ignore this if it's not on the lock modifier. */
5947 if ((1 << row) == LockMask)
334208b7 5948 dpyinfo->shift_lock_mask = LockMask;
11edeb03 5949 break;
28430d3c
JB
5950 }
5951 }
5952 }
5953 }
5954 }
5955
f689eb05 5956 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
334208b7 5957 if (! dpyinfo->meta_mod_mask)
a3c44b14 5958 {
334208b7
RS
5959 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
5960 dpyinfo->alt_mod_mask = 0;
a3c44b14 5961 }
f689eb05 5962
148c4b70
RS
5963 /* If some keys are both alt and meta,
5964 make them just meta, not alt. */
334208b7 5965 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
148c4b70 5966 {
334208b7 5967 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
148c4b70 5968 }
58769bee 5969
28430d3c 5970 XFree ((char *) syms);
f689eb05 5971 XFreeModifiermap (mods);
28430d3c
JB
5972}
5973
dfeccd2d
JB
5974/* Convert between the modifier bits X uses and the modifier bits
5975 Emacs uses. */
06a2c219 5976
7c5283e4 5977static unsigned int
334208b7
RS
5978x_x_to_emacs_modifiers (dpyinfo, state)
5979 struct x_display_info *dpyinfo;
dc6f92b8
JB
5980 unsigned int state;
5981{
334208b7
RS
5982 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
5983 | ((state & ControlMask) ? ctrl_modifier : 0)
5984 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
5985 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
5986 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
5987 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
dc6f92b8
JB
5988}
5989
dfeccd2d 5990static unsigned int
334208b7
RS
5991x_emacs_to_x_modifiers (dpyinfo, state)
5992 struct x_display_info *dpyinfo;
dfeccd2d
JB
5993 unsigned int state;
5994{
334208b7
RS
5995 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
5996 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
5997 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
5998 | ((state & shift_modifier) ? ShiftMask : 0)
5999 | ((state & ctrl_modifier) ? ControlMask : 0)
6000 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
dfeccd2d 6001}
d047c4eb
KH
6002
6003/* Convert a keysym to its name. */
6004
6005char *
6006x_get_keysym_name (keysym)
6007 KeySym keysym;
6008{
6009 char *value;
6010
6011 BLOCK_INPUT;
6012 value = XKeysymToString (keysym);
6013 UNBLOCK_INPUT;
6014
6015 return value;
6016}
06a2c219
GM
6017
6018
e4571a43
JB
6019\f
6020/* Mouse clicks and mouse movement. Rah. */
e4571a43 6021
06a2c219
GM
6022/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6023 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6024 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6025 not force the value into range. */
69388238 6026
c8dba240 6027void
69388238 6028pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
e4571a43 6029 FRAME_PTR f;
69388238 6030 register int pix_x, pix_y;
e4571a43
JB
6031 register int *x, *y;
6032 XRectangle *bounds;
69388238 6033 int noclip;
e4571a43 6034{
06a2c219 6035 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
69388238
RS
6036 even for negative values. */
6037 if (pix_x < 0)
7556890b 6038 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
69388238 6039 if (pix_y < 0)
7556890b 6040 pix_y -= (f)->output_data.x->line_height - 1;
69388238 6041
e4571a43
JB
6042 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6043 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6044
6045 if (bounds)
6046 {
7556890b
RS
6047 bounds->width = FONT_WIDTH (f->output_data.x->font);
6048 bounds->height = f->output_data.x->line_height;
e4571a43
JB
6049 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6050 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6051 }
6052
69388238
RS
6053 if (!noclip)
6054 {
6055 if (pix_x < 0)
6056 pix_x = 0;
3cbd2e0b
RS
6057 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6058 pix_x = FRAME_WINDOW_WIDTH (f);
69388238
RS
6059
6060 if (pix_y < 0)
6061 pix_y = 0;
6062 else if (pix_y > f->height)
6063 pix_y = f->height;
6064 }
e4571a43
JB
6065
6066 *x = pix_x;
6067 *y = pix_y;
6068}
6069
06a2c219
GM
6070
6071/* Given HPOS/VPOS in the current matrix of W, return corresponding
6072 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6073 can't tell the positions because W's display is not up to date,
6074 return 0. */
6075
6076int
6077glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6078 struct window *w;
6079 int hpos, vpos;
6080 int *frame_x, *frame_y;
2b5c9e71 6081{
06a2c219
GM
6082 int success_p;
6083
6084 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6085 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6086
6087 if (display_completed)
6088 {
6089 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6090 struct glyph *glyph = row->glyphs[TEXT_AREA];
6091 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6092
6093 *frame_y = row->y;
6094 *frame_x = row->x;
6095 while (glyph < end)
6096 {
6097 *frame_x += glyph->pixel_width;
6098 ++glyph;
6099 }
6100
6101 success_p = 1;
6102 }
6103 else
6104 {
6105 *frame_y = *frame_x = 0;
6106 success_p = 0;
6107 }
6108
6109 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6110 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6111 return success_p;
2b5c9e71
RS
6112}
6113
06a2c219 6114
dc6f92b8
JB
6115/* Prepare a mouse-event in *RESULT for placement in the input queue.
6116
6117 If the event is a button press, then note that we have grabbed
f451eb13 6118 the mouse. */
dc6f92b8
JB
6119
6120static Lisp_Object
f451eb13 6121construct_mouse_click (result, event, f)
dc6f92b8
JB
6122 struct input_event *result;
6123 XButtonEvent *event;
f676886a 6124 struct frame *f;
dc6f92b8 6125{
f451eb13 6126 /* Make the event type no_event; we'll change that when we decide
dc6f92b8 6127 otherwise. */
f451eb13 6128 result->kind = mouse_click;
69388238 6129 result->code = event->button - Button1;
1113d9db 6130 result->timestamp = event->time;
334208b7
RS
6131 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6132 event->state)
f689eb05 6133 | (event->type == ButtonRelease
58769bee 6134 ? up_modifier
f689eb05 6135 : down_modifier));
dc6f92b8 6136
06a2c219
GM
6137 XSETINT (result->x, event->x);
6138 XSETINT (result->y, event->y);
6139 XSETFRAME (result->frame_or_window, f);
0f8aabe9 6140 result->arg = Qnil;
06a2c219 6141 return Qnil;
dc6f92b8 6142}
b849c413 6143
69388238 6144\f
90e65f07
JB
6145/* Function to report a mouse movement to the mainstream Emacs code.
6146 The input handler calls this.
6147
6148 We have received a mouse movement event, which is given in *event.
6149 If the mouse is over a different glyph than it was last time, tell
6150 the mainstream emacs code by setting mouse_moved. If not, ask for
6151 another motion event, so we can check again the next time it moves. */
b8009dd1 6152
06a2c219
GM
6153static XMotionEvent last_mouse_motion_event;
6154static Lisp_Object last_mouse_motion_frame;
6155
90e65f07 6156static void
12ba150f 6157note_mouse_movement (frame, event)
f676886a 6158 FRAME_PTR frame;
90e65f07 6159 XMotionEvent *event;
90e65f07 6160{
e5d77022 6161 last_mouse_movement_time = event->time;
06a2c219
GM
6162 last_mouse_motion_event = *event;
6163 XSETFRAME (last_mouse_motion_frame, frame);
e5d77022 6164
27f338af
RS
6165 if (event->window != FRAME_X_WINDOW (frame))
6166 {
39d8bb4d 6167 frame->mouse_moved = 1;
27f338af 6168 last_mouse_scroll_bar = Qnil;
27f338af 6169 note_mouse_highlight (frame, -1, -1);
27f338af
RS
6170 }
6171
90e65f07 6172 /* Has the mouse moved off the glyph it was on at the last sighting? */
27f338af
RS
6173 else if (event->x < last_mouse_glyph.x
6174 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6175 || event->y < last_mouse_glyph.y
6176 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
12ba150f 6177 {
39d8bb4d 6178 frame->mouse_moved = 1;
ab648270 6179 last_mouse_scroll_bar = Qnil;
b8009dd1 6180 note_mouse_highlight (frame, event->x, event->y);
90e65f07
JB
6181 }
6182}
6183
bf1c0ba1 6184/* This is used for debugging, to turn off note_mouse_highlight. */
bf1c0ba1 6185
06a2c219
GM
6186 int disable_mouse_highlight;
6187
6188
6189\f
6190/************************************************************************
6191 Mouse Face
6192 ************************************************************************/
6193
6194/* Find the glyph under window-relative coordinates X/Y in window W.
6195 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6196 strings. Return in *HPOS and *VPOS the row and column number of
6197 the glyph found. Return in *AREA the glyph area containing X.
6198 Value is a pointer to the glyph found or null if X/Y is not on
6199 text, or we can't tell because W's current matrix is not up to
6200 date. */
6201
6202static struct glyph *
6203x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6204 struct window *w;
6205 int x, y;
6206 int *hpos, *vpos, *area;
6207{
6208 struct glyph *glyph, *end;
3e71d8f2 6209 struct glyph_row *row = NULL;
06a2c219
GM
6210 int x0, i, left_area_width;
6211
6212 /* Find row containing Y. Give up if some row is not enabled. */
6213 for (i = 0; i < w->current_matrix->nrows; ++i)
6214 {
6215 row = MATRIX_ROW (w->current_matrix, i);
6216 if (!row->enabled_p)
6217 return NULL;
6218 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6219 break;
6220 }
6221
6222 *vpos = i;
6223 *hpos = 0;
6224
6225 /* Give up if Y is not in the window. */
6226 if (i == w->current_matrix->nrows)
6227 return NULL;
6228
6229 /* Get the glyph area containing X. */
6230 if (w->pseudo_window_p)
6231 {
6232 *area = TEXT_AREA;
6233 x0 = 0;
6234 }
6235 else
6236 {
6237 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6238 if (x < left_area_width)
6239 {
6240 *area = LEFT_MARGIN_AREA;
6241 x0 = 0;
6242 }
6243 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6244 {
6245 *area = TEXT_AREA;
6246 x0 = row->x + left_area_width;
6247 }
6248 else
6249 {
6250 *area = RIGHT_MARGIN_AREA;
6251 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6252 }
6253 }
6254
6255 /* Find glyph containing X. */
6256 glyph = row->glyphs[*area];
6257 end = glyph + row->used[*area];
6258 while (glyph < end)
6259 {
6260 if (x < x0 + glyph->pixel_width)
6261 {
6262 if (w->pseudo_window_p)
6263 break;
6264 else if (BUFFERP (glyph->object))
6265 break;
6266 }
6267
6268 x0 += glyph->pixel_width;
6269 ++glyph;
6270 }
6271
6272 if (glyph == end)
6273 return NULL;
6274
6275 *hpos = glyph - row->glyphs[*area];
6276 return glyph;
6277}
6278
6279
6280/* Convert frame-relative x/y to coordinates relative to window W.
6281 Takes pseudo-windows into account. */
6282
6283static void
6284frame_to_window_pixel_xy (w, x, y)
6285 struct window *w;
6286 int *x, *y;
6287{
6288 if (w->pseudo_window_p)
6289 {
6290 /* A pseudo-window is always full-width, and starts at the
6291 left edge of the frame, plus a frame border. */
6292 struct frame *f = XFRAME (w->frame);
6293 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6294 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6295 }
6296 else
6297 {
6298 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6299 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6300 }
6301}
6302
6303
6304/* Take proper action when mouse has moved to the mode or top line of
6305 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6306 mode line. X is relative to the start of the text display area of
6307 W, so the width of bitmap areas and scroll bars must be subtracted
6308 to get a position relative to the start of the mode line. */
6309
6310static void
6311note_mode_line_highlight (w, x, mode_line_p)
6312 struct window *w;
6313 int x, mode_line_p;
6314{
6315 struct frame *f = XFRAME (w->frame);
6316 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6317 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6318 struct glyph_row *row;
6319
6320 if (mode_line_p)
6321 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6322 else
045dee35 6323 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
06a2c219
GM
6324
6325 if (row->enabled_p)
6326 {
6327 struct glyph *glyph, *end;
6328 Lisp_Object help, map;
6329 int x0;
6330
6331 /* Find the glyph under X. */
6332 glyph = row->glyphs[TEXT_AREA];
6333 end = glyph + row->used[TEXT_AREA];
6334 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
110859fc 6335 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
06a2c219
GM
6336 while (glyph < end
6337 && x >= x0 + glyph->pixel_width)
6338 {
6339 x0 += glyph->pixel_width;
6340 ++glyph;
6341 }
6342
6343 if (glyph < end
6344 && STRINGP (glyph->object)
6345 && XSTRING (glyph->object)->intervals
6346 && glyph->charpos >= 0
6347 && glyph->charpos < XSTRING (glyph->object)->size)
6348 {
6349 /* If we're on a string with `help-echo' text property,
6350 arrange for the help to be displayed. This is done by
6351 setting the global variable help_echo to the help string. */
6352 help = Fget_text_property (make_number (glyph->charpos),
6353 Qhelp_echo, glyph->object);
b7e80413 6354 if (!NILP (help))
06a2c219
GM
6355 help_echo = help;
6356
6357 /* Change the mouse pointer according to what is under X/Y. */
6358 map = Fget_text_property (make_number (glyph->charpos),
6359 Qlocal_map, glyph->object);
6360 if (!NILP (Fkeymapp (map)))
6361 cursor = f->output_data.x->nontext_cursor;
6362 }
6363 }
6364
6365 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6366}
6367
6368
6369/* Take proper action when the mouse has moved to position X, Y on
6370 frame F as regards highlighting characters that have mouse-face
6371 properties. Also de-highlighting chars where the mouse was before.
27f338af 6372 X and Y can be negative or out of range. */
b8009dd1
RS
6373
6374static void
6375note_mouse_highlight (f, x, y)
06a2c219 6376 struct frame *f;
c32cdd9a 6377 int x, y;
b8009dd1 6378{
06a2c219
GM
6379 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6380 int portion;
b8009dd1
RS
6381 Lisp_Object window;
6382 struct window *w;
6383
06a2c219
GM
6384 /* When a menu is active, don't highlight because this looks odd. */
6385#ifdef USE_X_TOOLKIT
6386 if (popup_activated ())
6387 return;
6388#endif
6389
04fff9c0
GM
6390 if (disable_mouse_highlight
6391 || !f->glyphs_initialized_p)
bf1c0ba1
RS
6392 return;
6393
06a2c219
GM
6394 dpyinfo->mouse_face_mouse_x = x;
6395 dpyinfo->mouse_face_mouse_y = y;
6396 dpyinfo->mouse_face_mouse_frame = f;
b8009dd1 6397
06a2c219 6398 if (dpyinfo->mouse_face_defer)
b8009dd1
RS
6399 return;
6400
514e4681
RS
6401 if (gc_in_progress)
6402 {
06a2c219 6403 dpyinfo->mouse_face_deferred_gc = 1;
514e4681
RS
6404 return;
6405 }
6406
b8009dd1 6407 /* Which window is that in? */
06a2c219 6408 window = window_from_coordinates (f, x, y, &portion, 1);
b8009dd1
RS
6409
6410 /* If we were displaying active text in another window, clear that. */
06a2c219
GM
6411 if (! EQ (window, dpyinfo->mouse_face_window))
6412 clear_mouse_face (dpyinfo);
6413
6414 /* Not on a window -> return. */
6415 if (!WINDOWP (window))
6416 return;
6417
6418 /* Convert to window-relative pixel coordinates. */
6419 w = XWINDOW (window);
6420 frame_to_window_pixel_xy (w, &x, &y);
6421
9ea173e8 6422 /* Handle tool-bar window differently since it doesn't display a
06a2c219 6423 buffer. */
9ea173e8 6424 if (EQ (window, f->tool_bar_window))
06a2c219 6425 {
9ea173e8 6426 note_tool_bar_highlight (f, x, y);
06a2c219
GM
6427 return;
6428 }
6429
6430 if (portion == 1 || portion == 3)
6431 {
6432 /* Mouse is on the mode or top line. */
6433 note_mode_line_highlight (w, x, portion == 1);
6434 return;
6435 }
6436 else
6437 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6438 f->output_data.x->text_cursor);
b8009dd1 6439
0cdd0c9f
RS
6440 /* Are we in a window whose display is up to date?
6441 And verify the buffer's text has not changed. */
06a2c219
GM
6442 if (/* Within text portion of the window. */
6443 portion == 0
0cdd0c9f 6444 && EQ (w->window_end_valid, w->buffer)
26459b28
KH
6445 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6446 && (XFASTINT (w->last_overlay_modified)
6447 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
b8009dd1 6448 {
06a2c219
GM
6449 int hpos, vpos, pos, i, area;
6450 struct glyph *glyph;
b8009dd1 6451
06a2c219
GM
6452 /* Find the glyph under X/Y. */
6453 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6454
6455 /* Clear mouse face if X/Y not over text. */
6456 if (glyph == NULL
6457 || area != TEXT_AREA
6458 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
b8009dd1 6459 {
06a2c219
GM
6460 clear_mouse_face (dpyinfo);
6461 return;
6462 }
6463
6464 pos = glyph->charpos;
6465 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6466
6467 /* Check for mouse-face and help-echo. */
6468 {
6469 Lisp_Object mouse_face, overlay, position;
6470 Lisp_Object *overlay_vec;
6471 int len, noverlays;
6472 struct buffer *obuf;
6473 int obegv, ozv;
6474
6475 /* If we get an out-of-range value, return now; avoid an error. */
6476 if (pos > BUF_Z (XBUFFER (w->buffer)))
6477 return;
6478
6479 /* Make the window's buffer temporarily current for
6480 overlays_at and compute_char_face. */
6481 obuf = current_buffer;
6482 current_buffer = XBUFFER (w->buffer);
6483 obegv = BEGV;
6484 ozv = ZV;
6485 BEGV = BEG;
6486 ZV = Z;
6487
6488 /* Is this char mouse-active or does it have help-echo? */
6489 XSETINT (position, pos);
6490
6491 /* Put all the overlays we want in a vector in overlay_vec.
6492 Store the length in len. If there are more than 10, make
6493 enough space for all, and try again. */
6494 len = 10;
6495 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6496 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6497 if (noverlays > len)
6498 {
6499 len = noverlays;
6500 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6501 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6502 }
6503
6504 noverlays = sort_overlays (overlay_vec, noverlays, w);
6505
6506 /* Check mouse-face highlighting. */
6507 if (! (EQ (window, dpyinfo->mouse_face_window)
6508 && vpos >= dpyinfo->mouse_face_beg_row
6509 && vpos <= dpyinfo->mouse_face_end_row
6510 && (vpos > dpyinfo->mouse_face_beg_row
6511 || hpos >= dpyinfo->mouse_face_beg_col)
6512 && (vpos < dpyinfo->mouse_face_end_row
6513 || hpos < dpyinfo->mouse_face_end_col
6514 || dpyinfo->mouse_face_past_end)))
6515 {
6516 /* Clear the display of the old active region, if any. */
6517 clear_mouse_face (dpyinfo);
6518
6519 /* Find the highest priority overlay that has a mouse-face prop. */
6520 overlay = Qnil;
6521 for (i = 0; i < noverlays; i++)
6522 {
6523 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6524 if (!NILP (mouse_face))
6525 {
6526 overlay = overlay_vec[i];
6527 break;
6528 }
6529 }
6530
6531 /* If no overlay applies, get a text property. */
6532 if (NILP (overlay))
6533 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6534
6535 /* Handle the overlay case. */
6536 if (! NILP (overlay))
6537 {
6538 /* Find the range of text around this char that
6539 should be active. */
6540 Lisp_Object before, after;
6541 int ignore;
6542
6543 before = Foverlay_start (overlay);
6544 after = Foverlay_end (overlay);
6545 /* Record this as the current active region. */
6546 fast_find_position (w, XFASTINT (before),
6547 &dpyinfo->mouse_face_beg_col,
6548 &dpyinfo->mouse_face_beg_row,
6549 &dpyinfo->mouse_face_beg_x,
6550 &dpyinfo->mouse_face_beg_y);
6551 dpyinfo->mouse_face_past_end
6552 = !fast_find_position (w, XFASTINT (after),
6553 &dpyinfo->mouse_face_end_col,
6554 &dpyinfo->mouse_face_end_row,
6555 &dpyinfo->mouse_face_end_x,
6556 &dpyinfo->mouse_face_end_y);
6557 dpyinfo->mouse_face_window = window;
6558 dpyinfo->mouse_face_face_id
6559 = face_at_buffer_position (w, pos, 0, 0,
6560 &ignore, pos + 1, 1);
6561
6562 /* Display it as active. */
6563 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6564 }
6565 /* Handle the text property case. */
6566 else if (! NILP (mouse_face))
6567 {
6568 /* Find the range of text around this char that
6569 should be active. */
6570 Lisp_Object before, after, beginning, end;
6571 int ignore;
6572
6573 beginning = Fmarker_position (w->start);
6574 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6575 - XFASTINT (w->window_end_pos)));
6576 before
6577 = Fprevious_single_property_change (make_number (pos + 1),
6578 Qmouse_face,
6579 w->buffer, beginning);
6580 after
6581 = Fnext_single_property_change (position, Qmouse_face,
6582 w->buffer, end);
6583 /* Record this as the current active region. */
6584 fast_find_position (w, XFASTINT (before),
6585 &dpyinfo->mouse_face_beg_col,
6586 &dpyinfo->mouse_face_beg_row,
6587 &dpyinfo->mouse_face_beg_x,
6588 &dpyinfo->mouse_face_beg_y);
6589 dpyinfo->mouse_face_past_end
6590 = !fast_find_position (w, XFASTINT (after),
6591 &dpyinfo->mouse_face_end_col,
6592 &dpyinfo->mouse_face_end_row,
6593 &dpyinfo->mouse_face_end_x,
6594 &dpyinfo->mouse_face_end_y);
6595 dpyinfo->mouse_face_window = window;
6596 dpyinfo->mouse_face_face_id
6597 = face_at_buffer_position (w, pos, 0, 0,
6598 &ignore, pos + 1, 1);
6599
6600 /* Display it as active. */
6601 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6602 }
6603 }
6604
6605 /* Look for a `help-echo' property. */
6606 {
6607 Lisp_Object help;
6608
6609 /* Check overlays first. */
6610 help = Qnil;
b7e80413 6611 for (i = 0; i < noverlays && NILP (help); ++i)
06a2c219
GM
6612 help = Foverlay_get (overlay_vec[i], Qhelp_echo);
6613
6614 /* Try text properties. */
b7e80413 6615 if (NILP (help)
06a2c219
GM
6616 && ((STRINGP (glyph->object)
6617 && glyph->charpos >= 0
6618 && glyph->charpos < XSTRING (glyph->object)->size)
6619 || (BUFFERP (glyph->object)
6620 && glyph->charpos >= BEGV
6621 && glyph->charpos < ZV)))
6622 help = Fget_text_property (make_number (glyph->charpos),
6623 Qhelp_echo, glyph->object);
6624
b7e80413 6625 if (!NILP (help))
06a2c219
GM
6626 help_echo = help;
6627 }
6628
6629 BEGV = obegv;
6630 ZV = ozv;
6631 current_buffer = obuf;
6632 }
6633 }
6634}
6635
6636static void
6637redo_mouse_highlight ()
6638{
6639 if (!NILP (last_mouse_motion_frame)
6640 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6641 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6642 last_mouse_motion_event.x,
6643 last_mouse_motion_event.y);
6644}
6645
6646
6647\f
6648/***********************************************************************
9ea173e8 6649 Tool-bars
06a2c219
GM
6650 ***********************************************************************/
6651
9ea173e8
GM
6652static int x_tool_bar_item P_ ((struct frame *, int, int,
6653 struct glyph **, int *, int *, int *));
06a2c219 6654
9ea173e8 6655/* Tool-bar item index of the item on which a mouse button was pressed
06a2c219
GM
6656 or -1. */
6657
9ea173e8 6658static int last_tool_bar_item;
06a2c219
GM
6659
6660
9ea173e8
GM
6661/* Get information about the tool-bar item at position X/Y on frame F.
6662 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6663 the current matrix of the tool-bar window of F, or NULL if not
6664 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
6665 item in F->current_tool_bar_items. Value is
06a2c219 6666
9ea173e8 6667 -1 if X/Y is not on a tool-bar item
06a2c219
GM
6668 0 if X/Y is on the same item that was highlighted before.
6669 1 otherwise. */
6670
6671static int
9ea173e8 6672x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
06a2c219
GM
6673 struct frame *f;
6674 int x, y;
6675 struct glyph **glyph;
6676 int *hpos, *vpos, *prop_idx;
6677{
6678 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6679 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6680 int area;
6681
6682 /* Find the glyph under X/Y. */
6683 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6684 if (*glyph == NULL)
6685 return -1;
6686
9ea173e8
GM
6687 /* Get the start of this tool-bar item's properties in
6688 f->current_tool_bar_items. */
6689 if (!tool_bar_item_info (f, *glyph, prop_idx))
06a2c219
GM
6690 return -1;
6691
6692 /* Is mouse on the highlighted item? */
9ea173e8 6693 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
06a2c219
GM
6694 && *vpos >= dpyinfo->mouse_face_beg_row
6695 && *vpos <= dpyinfo->mouse_face_end_row
6696 && (*vpos > dpyinfo->mouse_face_beg_row
6697 || *hpos >= dpyinfo->mouse_face_beg_col)
6698 && (*vpos < dpyinfo->mouse_face_end_row
6699 || *hpos < dpyinfo->mouse_face_end_col
6700 || dpyinfo->mouse_face_past_end))
6701 return 0;
6702
6703 return 1;
6704}
6705
6706
9ea173e8 6707/* Handle mouse button event on the tool-bar of frame F, at
06a2c219
GM
6708 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6709 or ButtonRelase. */
6710
6711static void
9ea173e8 6712x_handle_tool_bar_click (f, button_event)
06a2c219
GM
6713 struct frame *f;
6714 XButtonEvent *button_event;
6715{
6716 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6717 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6718 int hpos, vpos, prop_idx;
6719 struct glyph *glyph;
6720 Lisp_Object enabled_p;
6721 int x = button_event->x;
6722 int y = button_event->y;
6723
9ea173e8 6724 /* If not on the highlighted tool-bar item, return. */
06a2c219 6725 frame_to_window_pixel_xy (w, &x, &y);
9ea173e8 6726 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
06a2c219
GM
6727 return;
6728
6729 /* If item is disabled, do nothing. */
9ea173e8
GM
6730 enabled_p = (XVECTOR (f->current_tool_bar_items)
6731 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
06a2c219
GM
6732 if (NILP (enabled_p))
6733 return;
6734
6735 if (button_event->type == ButtonPress)
6736 {
6737 /* Show item in pressed state. */
6738 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6739 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9ea173e8 6740 last_tool_bar_item = prop_idx;
06a2c219
GM
6741 }
6742 else
6743 {
6744 Lisp_Object key, frame;
6745 struct input_event event;
6746
6747 /* Show item in released state. */
6748 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6749 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6750
9ea173e8
GM
6751 key = (XVECTOR (f->current_tool_bar_items)
6752 ->contents[prop_idx + TOOL_BAR_ITEM_KEY]);
06a2c219
GM
6753
6754 XSETFRAME (frame, f);
9ea173e8 6755 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
6756 event.frame_or_window = frame;
6757 event.arg = frame;
06a2c219
GM
6758 kbd_buffer_store_event (&event);
6759
9ea173e8 6760 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
6761 event.frame_or_window = frame;
6762 event.arg = key;
06a2c219
GM
6763 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6764 button_event->state);
6765 kbd_buffer_store_event (&event);
9ea173e8 6766 last_tool_bar_item = -1;
06a2c219
GM
6767 }
6768}
6769
6770
9ea173e8
GM
6771/* Possibly highlight a tool-bar item on frame F when mouse moves to
6772 tool-bar window-relative coordinates X/Y. Called from
06a2c219
GM
6773 note_mouse_highlight. */
6774
6775static void
9ea173e8 6776note_tool_bar_highlight (f, x, y)
06a2c219
GM
6777 struct frame *f;
6778 int x, y;
6779{
9ea173e8 6780 Lisp_Object window = f->tool_bar_window;
06a2c219
GM
6781 struct window *w = XWINDOW (window);
6782 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6783 int hpos, vpos;
6784 struct glyph *glyph;
6785 struct glyph_row *row;
5c187dee 6786 int i;
06a2c219
GM
6787 Lisp_Object enabled_p;
6788 int prop_idx;
6789 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
5c187dee 6790 int mouse_down_p, rc;
06a2c219
GM
6791
6792 /* Function note_mouse_highlight is called with negative x(y
6793 values when mouse moves outside of the frame. */
6794 if (x <= 0 || y <= 0)
6795 {
6796 clear_mouse_face (dpyinfo);
6797 return;
6798 }
6799
9ea173e8 6800 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
06a2c219
GM
6801 if (rc < 0)
6802 {
9ea173e8 6803 /* Not on tool-bar item. */
06a2c219
GM
6804 clear_mouse_face (dpyinfo);
6805 return;
6806 }
6807 else if (rc == 0)
9ea173e8 6808 /* On same tool-bar item as before. */
06a2c219 6809 goto set_help_echo;
b8009dd1 6810
06a2c219
GM
6811 clear_mouse_face (dpyinfo);
6812
9ea173e8 6813 /* Mouse is down, but on different tool-bar item? */
06a2c219
GM
6814 mouse_down_p = (dpyinfo->grabbed
6815 && f == last_mouse_frame
6816 && FRAME_LIVE_P (f));
6817 if (mouse_down_p
9ea173e8 6818 && last_tool_bar_item != prop_idx)
06a2c219
GM
6819 return;
6820
6821 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
6822 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
6823
9ea173e8
GM
6824 /* If tool-bar item is not enabled, don't highlight it. */
6825 enabled_p = (XVECTOR (f->current_tool_bar_items)
6826 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
06a2c219
GM
6827 if (!NILP (enabled_p))
6828 {
6829 /* Compute the x-position of the glyph. In front and past the
6830 image is a space. We include this is the highlighted area. */
6831 row = MATRIX_ROW (w->current_matrix, vpos);
6832 for (i = x = 0; i < hpos; ++i)
6833 x += row->glyphs[TEXT_AREA][i].pixel_width;
6834
6835 /* Record this as the current active region. */
6836 dpyinfo->mouse_face_beg_col = hpos;
6837 dpyinfo->mouse_face_beg_row = vpos;
6838 dpyinfo->mouse_face_beg_x = x;
6839 dpyinfo->mouse_face_beg_y = row->y;
6840 dpyinfo->mouse_face_past_end = 0;
6841
6842 dpyinfo->mouse_face_end_col = hpos + 1;
6843 dpyinfo->mouse_face_end_row = vpos;
6844 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
6845 dpyinfo->mouse_face_end_y = row->y;
6846 dpyinfo->mouse_face_window = window;
9ea173e8 6847 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
06a2c219
GM
6848
6849 /* Display it as active. */
6850 show_mouse_face (dpyinfo, draw);
6851 dpyinfo->mouse_face_image_state = draw;
b8009dd1 6852 }
06a2c219
GM
6853
6854 set_help_echo:
6855
9ea173e8 6856 /* Set help_echo to a help string.to display for this tool-bar item.
06a2c219 6857 XTread_socket does the rest. */
9ea173e8
GM
6858 help_echo = (XVECTOR (f->current_tool_bar_items)
6859 ->contents[prop_idx + TOOL_BAR_ITEM_HELP]);
b7e80413 6860 if (NILP (help_echo))
9ea173e8
GM
6861 help_echo = (XVECTOR (f->current_tool_bar_items)
6862 ->contents[prop_idx + TOOL_BAR_ITEM_CAPTION]);
b8009dd1 6863}
4d73d038 6864
06a2c219
GM
6865
6866\f
6867/* Find the glyph matrix position of buffer position POS in window W.
6868 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
6869 current glyphs must be up to date. If POS is above window start
6870 return (0, 0, 0, 0). If POS is after end of W, return end of
6871 last line in W. */
b8009dd1
RS
6872
6873static int
06a2c219
GM
6874fast_find_position (w, pos, hpos, vpos, x, y)
6875 struct window *w;
b8009dd1 6876 int pos;
06a2c219 6877 int *hpos, *vpos, *x, *y;
b8009dd1 6878{
b8009dd1 6879 int i;
bf1c0ba1 6880 int lastcol;
06a2c219
GM
6881 int maybe_next_line_p = 0;
6882 int line_start_position;
6883 int yb = window_text_bottom_y (w);
6884 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
6885 struct glyph_row *best_row = row;
6886 int row_vpos = 0, best_row_vpos = 0;
6887 int current_x;
6888
6889 while (row->y < yb)
b8009dd1 6890 {
06a2c219
GM
6891 if (row->used[TEXT_AREA])
6892 line_start_position = row->glyphs[TEXT_AREA]->charpos;
6893 else
6894 line_start_position = 0;
6895
6896 if (line_start_position > pos)
b8009dd1 6897 break;
77b68646
RS
6898 /* If the position sought is the end of the buffer,
6899 don't include the blank lines at the bottom of the window. */
06a2c219
GM
6900 else if (line_start_position == pos
6901 && pos == BUF_ZV (XBUFFER (w->buffer)))
77b68646 6902 {
06a2c219 6903 maybe_next_line_p = 1;
77b68646
RS
6904 break;
6905 }
06a2c219
GM
6906 else if (line_start_position > 0)
6907 {
6908 best_row = row;
6909 best_row_vpos = row_vpos;
6910 }
4b0bb6f3
GM
6911
6912 if (row->y + row->height >= yb)
6913 break;
06a2c219
GM
6914
6915 ++row;
6916 ++row_vpos;
b8009dd1 6917 }
06a2c219
GM
6918
6919 /* Find the right column within BEST_ROW. */
6920 lastcol = 0;
6921 current_x = best_row->x;
6922 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
bf1c0ba1 6923 {
06a2c219
GM
6924 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
6925 int charpos;
6926
6927 charpos = glyph->charpos;
6928 if (charpos == pos)
bf1c0ba1 6929 {
06a2c219
GM
6930 *hpos = i;
6931 *vpos = best_row_vpos;
6932 *x = current_x;
6933 *y = best_row->y;
bf1c0ba1
RS
6934 return 1;
6935 }
06a2c219 6936 else if (charpos > pos)
4d73d038 6937 break;
06a2c219
GM
6938 else if (charpos > 0)
6939 lastcol = i;
6940
6941 current_x += glyph->pixel_width;
bf1c0ba1 6942 }
b8009dd1 6943
77b68646
RS
6944 /* If we're looking for the end of the buffer,
6945 and we didn't find it in the line we scanned,
6946 use the start of the following line. */
06a2c219 6947 if (maybe_next_line_p)
77b68646 6948 {
06a2c219
GM
6949 ++best_row;
6950 ++best_row_vpos;
6951 lastcol = 0;
6952 current_x = best_row->x;
77b68646
RS
6953 }
6954
06a2c219
GM
6955 *vpos = best_row_vpos;
6956 *hpos = lastcol + 1;
6957 *x = current_x;
6958 *y = best_row->y;
b8009dd1
RS
6959 return 0;
6960}
6961
06a2c219 6962
b8009dd1
RS
6963/* Display the active region described by mouse_face_*
6964 in its mouse-face if HL > 0, in its normal face if HL = 0. */
6965
6966static void
06a2c219 6967show_mouse_face (dpyinfo, draw)
7a13e894 6968 struct x_display_info *dpyinfo;
06a2c219 6969 enum draw_glyphs_face draw;
b8009dd1 6970{
7a13e894 6971 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
06a2c219 6972 struct frame *f = XFRAME (WINDOW_FRAME (w));
b8009dd1 6973 int i;
06a2c219
GM
6974 int cursor_off_p = 0;
6975 struct cursor_pos saved_cursor;
6976
6977 saved_cursor = output_cursor;
6978
6979 /* If window is in the process of being destroyed, don't bother
6980 to do anything. */
6981 if (w->current_matrix == NULL)
6982 goto set_x_cursor;
6983
6984 /* Recognize when we are called to operate on rows that don't exist
6985 anymore. This can happen when a window is split. */
6986 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
6987 goto set_x_cursor;
6988
6989 set_output_cursor (&w->phys_cursor);
6990
6991 /* Note that mouse_face_beg_row etc. are window relative. */
6992 for (i = dpyinfo->mouse_face_beg_row;
6993 i <= dpyinfo->mouse_face_end_row;
6994 i++)
6995 {
6996 int start_hpos, end_hpos, start_x;
6997 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
6998
6999 /* Don't do anything if row doesn't have valid contents. */
7000 if (!row->enabled_p)
7001 continue;
7002
7003 /* For all but the first row, the highlight starts at column 0. */
7004 if (i == dpyinfo->mouse_face_beg_row)
7005 {
7006 start_hpos = dpyinfo->mouse_face_beg_col;
7007 start_x = dpyinfo->mouse_face_beg_x;
7008 }
7009 else
7010 {
7011 start_hpos = 0;
7012 start_x = 0;
7013 }
7014
7015 if (i == dpyinfo->mouse_face_end_row)
7016 end_hpos = dpyinfo->mouse_face_end_col;
7017 else
7018 end_hpos = row->used[TEXT_AREA];
7019
7020 /* If the cursor's in the text we are about to rewrite, turn the
7021 cursor off. */
7022 if (!w->pseudo_window_p
7023 && i == output_cursor.vpos
7024 && output_cursor.hpos >= start_hpos - 1
7025 && output_cursor.hpos <= end_hpos)
514e4681 7026 {
06a2c219
GM
7027 x_update_window_cursor (w, 0);
7028 cursor_off_p = 1;
514e4681 7029 }
b8009dd1 7030
06a2c219 7031 if (end_hpos > start_hpos)
64f26cf5
GM
7032 {
7033 row->mouse_face_p = draw == DRAW_MOUSE_FACE;
7034 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7035 start_hpos, end_hpos, draw, NULL, NULL, 0);
7036 }
b8009dd1
RS
7037 }
7038
514e4681 7039 /* If we turned the cursor off, turn it back on. */
06a2c219
GM
7040 if (cursor_off_p)
7041 x_display_cursor (w, 1,
7042 output_cursor.hpos, output_cursor.vpos,
7043 output_cursor.x, output_cursor.y);
2729a2b5 7044
06a2c219 7045 output_cursor = saved_cursor;
fb3b7de5 7046
06a2c219
GM
7047 set_x_cursor:
7048
7049 /* Change the mouse cursor. */
7050 if (draw == DRAW_NORMAL_TEXT)
7051 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7052 f->output_data.x->text_cursor);
7053 else if (draw == DRAW_MOUSE_FACE)
334208b7 7054 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 7055 f->output_data.x->cross_cursor);
27ead1d5 7056 else
334208b7 7057 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
06a2c219 7058 f->output_data.x->nontext_cursor);
b8009dd1
RS
7059}
7060
7061/* Clear out the mouse-highlighted active region.
06a2c219 7062 Redraw it un-highlighted first. */
b8009dd1 7063
06a2c219 7064void
7a13e894
RS
7065clear_mouse_face (dpyinfo)
7066 struct x_display_info *dpyinfo;
b8009dd1 7067{
06a2c219
GM
7068 if (tip_frame)
7069 return;
7070
7a13e894 7071 if (! NILP (dpyinfo->mouse_face_window))
06a2c219 7072 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
b8009dd1 7073
7a13e894
RS
7074 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7075 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7076 dpyinfo->mouse_face_window = Qnil;
b8009dd1 7077}
e687d06e 7078
71b8321e
GM
7079
7080/* Clear any mouse-face on window W. This function is part of the
7081 redisplay interface, and is called from try_window_id and similar
7082 functions to ensure the mouse-highlight is off. */
7083
7084static void
7085x_clear_mouse_face (w)
7086 struct window *w;
7087{
7088 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7089 Lisp_Object window;
7090
7091 XSETWINDOW (window, w);
7092 if (EQ (window, dpyinfo->mouse_face_window))
7093 clear_mouse_face (dpyinfo);
7094}
7095
7096
e687d06e
RS
7097/* Just discard the mouse face information for frame F, if any.
7098 This is used when the size of F is changed. */
7099
dfcf069d 7100void
e687d06e
RS
7101cancel_mouse_face (f)
7102 FRAME_PTR f;
7103{
7104 Lisp_Object window;
7105 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7106
7107 window = dpyinfo->mouse_face_window;
7108 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7109 {
7110 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7111 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7112 dpyinfo->mouse_face_window = Qnil;
7113 }
7114}
b8009dd1 7115\f
ab648270
JB
7116static struct scroll_bar *x_window_to_scroll_bar ();
7117static void x_scroll_bar_report_motion ();
12ba150f 7118
90e65f07 7119/* Return the current position of the mouse.
2d7fc7e8 7120 *fp should be a frame which indicates which display to ask about.
90e65f07 7121
2d7fc7e8 7122 If the mouse movement started in a scroll bar, set *fp, *bar_window,
ab648270 7123 and *part to the frame, window, and scroll bar part that the mouse
12ba150f 7124 is over. Set *x and *y to the portion and whole of the mouse's
ab648270 7125 position on the scroll bar.
12ba150f 7126
2d7fc7e8 7127 If the mouse movement started elsewhere, set *fp to the frame the
12ba150f
JB
7128 mouse is on, *bar_window to nil, and *x and *y to the character cell
7129 the mouse is over.
7130
06a2c219 7131 Set *time to the server time-stamp for the time at which the mouse
12ba150f
JB
7132 was at this position.
7133
a135645a
RS
7134 Don't store anything if we don't have a valid set of values to report.
7135
90e65f07 7136 This clears the mouse_moved flag, so we can wait for the next mouse
f5bb65ec 7137 movement. */
90e65f07
JB
7138
7139static void
1cf412ec 7140XTmouse_position (fp, insist, bar_window, part, x, y, time)
334208b7 7141 FRAME_PTR *fp;
1cf412ec 7142 int insist;
12ba150f 7143 Lisp_Object *bar_window;
ab648270 7144 enum scroll_bar_part *part;
90e65f07 7145 Lisp_Object *x, *y;
e5d77022 7146 unsigned long *time;
90e65f07 7147{
a135645a
RS
7148 FRAME_PTR f1;
7149
90e65f07
JB
7150 BLOCK_INPUT;
7151
8bcee03e 7152 if (! NILP (last_mouse_scroll_bar) && insist == 0)
334208b7 7153 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
90e65f07
JB
7154 else
7155 {
12ba150f
JB
7156 Window root;
7157 int root_x, root_y;
90e65f07 7158
12ba150f
JB
7159 Window dummy_window;
7160 int dummy;
7161
39d8bb4d
KH
7162 Lisp_Object frame, tail;
7163
7164 /* Clear the mouse-moved flag for every frame on this display. */
7165 FOR_EACH_FRAME (tail, frame)
7166 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7167 XFRAME (frame)->mouse_moved = 0;
7168
ab648270 7169 last_mouse_scroll_bar = Qnil;
12ba150f
JB
7170
7171 /* Figure out which root window we're on. */
334208b7
RS
7172 XQueryPointer (FRAME_X_DISPLAY (*fp),
7173 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
12ba150f
JB
7174
7175 /* The root window which contains the pointer. */
7176 &root,
7177
7178 /* Trash which we can't trust if the pointer is on
7179 a different screen. */
7180 &dummy_window,
7181
7182 /* The position on that root window. */
58769bee 7183 &root_x, &root_y,
12ba150f
JB
7184
7185 /* More trash we can't trust. */
7186 &dummy, &dummy,
7187
7188 /* Modifier keys and pointer buttons, about which
7189 we don't care. */
7190 (unsigned int *) &dummy);
7191
7192 /* Now we have a position on the root; find the innermost window
7193 containing the pointer. */
7194 {
7195 Window win, child;
7196 int win_x, win_y;
06a2c219 7197 int parent_x = 0, parent_y = 0;
e99db5a1 7198 int count;
12ba150f
JB
7199
7200 win = root;
69388238 7201
2d7fc7e8
RS
7202 /* XTranslateCoordinates can get errors if the window
7203 structure is changing at the same time this function
7204 is running. So at least we must not crash from them. */
7205
e99db5a1 7206 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
2d7fc7e8 7207
334208b7 7208 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
23faf38f 7209 && FRAME_LIVE_P (last_mouse_frame))
12ba150f 7210 {
69388238
RS
7211 /* If mouse was grabbed on a frame, give coords for that frame
7212 even if the mouse is now outside it. */
334208b7 7213 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
69388238 7214
12ba150f 7215 /* From-window, to-window. */
69388238 7216 root, FRAME_X_WINDOW (last_mouse_frame),
12ba150f
JB
7217
7218 /* From-position, to-position. */
7219 root_x, root_y, &win_x, &win_y,
7220
7221 /* Child of win. */
7222 &child);
69388238
RS
7223 f1 = last_mouse_frame;
7224 }
7225 else
7226 {
7227 while (1)
7228 {
334208b7 7229 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
12ba150f 7230
69388238
RS
7231 /* From-window, to-window. */
7232 root, win,
12ba150f 7233
69388238
RS
7234 /* From-position, to-position. */
7235 root_x, root_y, &win_x, &win_y,
7236
7237 /* Child of win. */
7238 &child);
7239
9af3143a 7240 if (child == None || child == win)
69388238
RS
7241 break;
7242
7243 win = child;
7244 parent_x = win_x;
7245 parent_y = win_y;
7246 }
12ba150f 7247
69388238
RS
7248 /* Now we know that:
7249 win is the innermost window containing the pointer
7250 (XTC says it has no child containing the pointer),
7251 win_x and win_y are the pointer's position in it
7252 (XTC did this the last time through), and
7253 parent_x and parent_y are the pointer's position in win's parent.
7254 (They are what win_x and win_y were when win was child.
7255 If win is the root window, it has no parent, and
7256 parent_{x,y} are invalid, but that's okay, because we'll
7257 never use them in that case.) */
7258
7259 /* Is win one of our frames? */
19126e11 7260 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
69388238 7261 }
58769bee 7262
2d7fc7e8
RS
7263 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7264 f1 = 0;
7265
e99db5a1 7266 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
2d7fc7e8 7267
ab648270 7268 /* If not, is it one of our scroll bars? */
a135645a 7269 if (! f1)
12ba150f 7270 {
ab648270 7271 struct scroll_bar *bar = x_window_to_scroll_bar (win);
12ba150f
JB
7272
7273 if (bar)
7274 {
a135645a 7275 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
7276 win_x = parent_x;
7277 win_y = parent_y;
7278 }
7279 }
90e65f07 7280
8bcee03e 7281 if (f1 == 0 && insist > 0)
b86bd3dd 7282 f1 = SELECTED_FRAME ();
1cf412ec 7283
a135645a 7284 if (f1)
12ba150f 7285 {
06a2c219
GM
7286 /* Ok, we found a frame. Store all the values.
7287 last_mouse_glyph is a rectangle used to reduce the
7288 generation of mouse events. To not miss any motion
7289 events, we must divide the frame into rectangles of the
7290 size of the smallest character that could be displayed
7291 on it, i.e. into the same rectangles that matrices on
7292 the frame are divided into. */
7293
7294#if OLD_REDISPLAY_CODE
2b5c9e71 7295 int ignore1, ignore2;
2b5c9e71 7296 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
334208b7 7297 &last_mouse_glyph,
1cf412ec
RS
7298 FRAME_X_DISPLAY_INFO (f1)->grabbed
7299 || insist);
06a2c219
GM
7300#else
7301 {
7302 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7303 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7304 int x = win_x;
7305 int y = win_y;
7306
7307 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7308 round down even for negative values. */
7309 if (x < 0)
7310 x -= width - 1;
7311 if (y < 0)
7312 y -= height - 1;
7313
7314 last_mouse_glyph.width = width;
7315 last_mouse_glyph.height = height;
7316 last_mouse_glyph.x = (x + width - 1) / width * width;
7317 last_mouse_glyph.y = (y + height - 1) / height * height;
7318 }
7319#endif
12ba150f
JB
7320
7321 *bar_window = Qnil;
7322 *part = 0;
334208b7 7323 *fp = f1;
e0c1aef2
KH
7324 XSETINT (*x, win_x);
7325 XSETINT (*y, win_y);
12ba150f
JB
7326 *time = last_mouse_movement_time;
7327 }
7328 }
7329 }
90e65f07
JB
7330
7331 UNBLOCK_INPUT;
7332}
f451eb13 7333
06a2c219 7334
06a2c219 7335#ifdef USE_X_TOOLKIT
bffcfca9
GM
7336
7337/* Atimer callback function for TIMER. Called every 0.1s to process
7338 Xt timeouts, if needed. We must avoid calling XtAppPending as
7339 much as possible because that function does an implicit XFlush
7340 that slows us down. */
7341
7342static void
7343x_process_timeouts (timer)
7344 struct atimer *timer;
7345{
7346 if (toolkit_scroll_bar_interaction || popup_activated_flag)
7347 {
7348 BLOCK_INPUT;
7349 while (XtAppPending (Xt_app_con) & XtIMTimer)
7350 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7351 UNBLOCK_INPUT;
7352 }
06a2c219
GM
7353}
7354
bffcfca9 7355#endif /* USE_X_TOOLKIT */
06a2c219
GM
7356
7357\f
7358/* Scroll bar support. */
7359
7360/* Given an X window ID, find the struct scroll_bar which manages it.
7361 This can be called in GC, so we have to make sure to strip off mark
7362 bits. */
bffcfca9 7363
06a2c219
GM
7364static struct scroll_bar *
7365x_window_to_scroll_bar (window_id)
7366 Window window_id;
7367{
7368 Lisp_Object tail;
7369
7370 for (tail = Vframe_list;
7371 XGCTYPE (tail) == Lisp_Cons;
8e713be6 7372 tail = XCDR (tail))
06a2c219
GM
7373 {
7374 Lisp_Object frame, bar, condemned;
7375
8e713be6 7376 frame = XCAR (tail);
06a2c219
GM
7377 /* All elements of Vframe_list should be frames. */
7378 if (! GC_FRAMEP (frame))
7379 abort ();
7380
7381 /* Scan this frame's scroll bar list for a scroll bar with the
7382 right window ID. */
7383 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7384 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7385 /* This trick allows us to search both the ordinary and
7386 condemned scroll bar lists with one loop. */
7387 ! GC_NILP (bar) || (bar = condemned,
7388 condemned = Qnil,
7389 ! GC_NILP (bar));
7390 bar = XSCROLL_BAR (bar)->next)
7391 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7392 return XSCROLL_BAR (bar);
7393 }
7394
7395 return 0;
7396}
7397
7398
7399\f
7400/************************************************************************
7401 Toolkit scroll bars
7402 ************************************************************************/
7403
7404#if USE_TOOLKIT_SCROLL_BARS
7405
7406static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7407static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7408static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7409 struct scroll_bar *));
7410static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7411 int, int, int));
7412
7413
7414/* Id of action hook installed for scroll bars. */
7415
7416static XtActionHookId action_hook_id;
7417
7418/* Lisp window being scrolled. Set when starting to interact with
7419 a toolkit scroll bar, reset to nil when ending the interaction. */
7420
7421static Lisp_Object window_being_scrolled;
7422
7423/* Last scroll bar part sent in xm_scroll_callback. */
7424
7425static int last_scroll_bar_part;
7426
ec18280f
SM
7427/* Whether this is an Xaw with arrow-scrollbars. This should imply
7428 that movements of 1/20 of the screen size are mapped to up/down. */
7429
7430static Boolean xaw3d_arrow_scroll;
7431
7432/* Whether the drag scrolling maintains the mouse at the top of the
7433 thumb. If not, resizing the thumb needs to be done more carefully
7434 to avoid jerkyness. */
7435
7436static Boolean xaw3d_pick_top;
7437
06a2c219
GM
7438
7439/* Action hook installed via XtAppAddActionHook when toolkit scroll
ec18280f 7440 bars are used.. The hook is responsible for detecting when
06a2c219
GM
7441 the user ends an interaction with the scroll bar, and generates
7442 a `end-scroll' scroll_bar_click' event if so. */
7443
7444static void
7445xt_action_hook (widget, client_data, action_name, event, params,
7446 num_params)
7447 Widget widget;
7448 XtPointer client_data;
7449 String action_name;
7450 XEvent *event;
7451 String *params;
7452 Cardinal *num_params;
7453{
7454 int scroll_bar_p;
7455 char *end_action;
7456
7457#ifdef USE_MOTIF
7458 scroll_bar_p = XmIsScrollBar (widget);
7459 end_action = "Release";
ec18280f 7460#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
7461 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7462 end_action = "EndScroll";
ec18280f 7463#endif /* USE_MOTIF */
06a2c219 7464
06a2c219
GM
7465 if (scroll_bar_p
7466 && strcmp (action_name, end_action) == 0
7467 && WINDOWP (window_being_scrolled))
7468 {
7469 struct window *w;
7470
7471 x_send_scroll_bar_event (window_being_scrolled,
7472 scroll_bar_end_scroll, 0, 0);
7473 w = XWINDOW (window_being_scrolled);
7474 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7475 window_being_scrolled = Qnil;
7476 last_scroll_bar_part = -1;
bffcfca9
GM
7477
7478 /* Xt timeouts no longer needed. */
7479 toolkit_scroll_bar_interaction = 0;
06a2c219
GM
7480 }
7481}
7482
7483
7484/* Send a client message with message type Xatom_Scrollbar for a
7485 scroll action to the frame of WINDOW. PART is a value identifying
7486 the part of the scroll bar that was clicked on. PORTION is the
7487 amount to scroll of a whole of WHOLE. */
7488
7489static void
7490x_send_scroll_bar_event (window, part, portion, whole)
7491 Lisp_Object window;
7492 int part, portion, whole;
7493{
7494 XEvent event;
7495 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
7496 struct frame *f = XFRAME (XWINDOW (window)->frame);
7497
7498 /* Construct a ClientMessage event to send to the frame. */
7499 ev->type = ClientMessage;
7500 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7501 ev->display = FRAME_X_DISPLAY (f);
7502 ev->window = FRAME_X_WINDOW (f);
7503 ev->format = 32;
52e386c2 7504 ev->data.l[0] = (long) XFASTINT (window);
06a2c219
GM
7505 ev->data.l[1] = (long) part;
7506 ev->data.l[2] = (long) 0;
7507 ev->data.l[3] = (long) portion;
7508 ev->data.l[4] = (long) whole;
7509
bffcfca9
GM
7510 /* Make Xt timeouts work while the scroll bar is active. */
7511 toolkit_scroll_bar_interaction = 1;
7512
06a2c219
GM
7513 /* Setting the event mask to zero means that the message will
7514 be sent to the client that created the window, and if that
7515 window no longer exists, no event will be sent. */
7516 BLOCK_INPUT;
7517 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7518 UNBLOCK_INPUT;
7519}
7520
7521
7522/* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7523 in *IEVENT. */
7524
7525static void
7526x_scroll_bar_to_input_event (event, ievent)
7527 XEvent *event;
7528 struct input_event *ievent;
7529{
7530 XClientMessageEvent *ev = (XClientMessageEvent *) event;
52e386c2
KR
7531 Lisp_Object window;
7532 struct frame *f;
7533
7534 XSETFASTINT (window, ev->data.l[0]);
7535 f = XFRAME (XWINDOW (window)->frame);
06a2c219
GM
7536
7537 ievent->kind = scroll_bar_click;
7538 ievent->frame_or_window = window;
0f8aabe9 7539 ievent->arg = Qnil;
06a2c219
GM
7540 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7541 ievent->part = ev->data.l[1];
7542 ievent->code = ev->data.l[2];
7543 ievent->x = make_number ((int) ev->data.l[3]);
7544 ievent->y = make_number ((int) ev->data.l[4]);
7545 ievent->modifiers = 0;
7546}
7547
7548
7549#ifdef USE_MOTIF
7550
7551/* Minimum and maximum values used for Motif scroll bars. */
7552
7553#define XM_SB_MIN 1
7554#define XM_SB_MAX 10000000
7555#define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7556
7557
7558/* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7559 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7560 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7561
7562static void
7563xm_scroll_callback (widget, client_data, call_data)
7564 Widget widget;
7565 XtPointer client_data, call_data;
7566{
7567 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7568 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7569 double percent;
7570 int part = -1, whole = 0, portion = 0;
7571
7572 switch (cs->reason)
7573 {
7574 case XmCR_DECREMENT:
7575 bar->dragging = Qnil;
7576 part = scroll_bar_up_arrow;
7577 break;
7578
7579 case XmCR_INCREMENT:
7580 bar->dragging = Qnil;
7581 part = scroll_bar_down_arrow;
7582 break;
7583
7584 case XmCR_PAGE_DECREMENT:
7585 bar->dragging = Qnil;
7586 part = scroll_bar_above_handle;
7587 break;
7588
7589 case XmCR_PAGE_INCREMENT:
7590 bar->dragging = Qnil;
7591 part = scroll_bar_below_handle;
7592 break;
7593
7594 case XmCR_TO_TOP:
7595 bar->dragging = Qnil;
7596 part = scroll_bar_to_top;
7597 break;
7598
7599 case XmCR_TO_BOTTOM:
7600 bar->dragging = Qnil;
7601 part = scroll_bar_to_bottom;
7602 break;
7603
7604 case XmCR_DRAG:
7605 {
7606 int slider_size;
7607 int dragging_down_p = (INTEGERP (bar->dragging)
7608 && XINT (bar->dragging) <= cs->value);
7609
7610 /* Get the slider size. */
7611 BLOCK_INPUT;
7612 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7613 UNBLOCK_INPUT;
7614
7615 /* At the max position of the scroll bar, do a line-wise
7616 movement. Without doing anything, the LessTif scroll bar
7617 calls us with the same cs->value again and again. If we
7618 want to make sure that we can reach the end of the buffer,
7619 we have to do something.
7620
7621 Implementation note: setting bar->dragging always to
7622 cs->value gives a smoother movement at the max position.
7623 Setting it to nil when doing line-wise movement gives
7624 a better slider behavior. */
7625
7626 if (cs->value + slider_size == XM_SB_MAX
7627 || (dragging_down_p
7628 && last_scroll_bar_part == scroll_bar_down_arrow))
7629 {
7630 part = scroll_bar_down_arrow;
7631 bar->dragging = Qnil;
7632 }
7633 else
7634 {
7635 whole = XM_SB_RANGE;
7636 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7637 part = scroll_bar_handle;
7638 bar->dragging = make_number (cs->value);
7639 }
7640 }
7641 break;
7642
7643 case XmCR_VALUE_CHANGED:
7644 break;
7645 };
7646
7647 if (part >= 0)
7648 {
7649 window_being_scrolled = bar->window;
7650 last_scroll_bar_part = part;
7651 x_send_scroll_bar_event (bar->window, part, portion, whole);
7652 }
7653}
7654
7655
ec18280f 7656#else /* !USE_MOTIF, i.e. Xaw. */
06a2c219
GM
7657
7658
ec18280f 7659/* Xaw scroll bar callback. Invoked when the thumb is dragged.
06a2c219
GM
7660 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7661 scroll bar struct. CALL_DATA is a pointer to a float saying where
7662 the thumb is. */
7663
7664static void
ec18280f 7665xaw_jump_callback (widget, client_data, call_data)
06a2c219
GM
7666 Widget widget;
7667 XtPointer client_data, call_data;
7668{
7669 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7670 float top = *(float *) call_data;
7671 float shown;
ec18280f
SM
7672 int whole, portion, height;
7673 int part;
06a2c219
GM
7674
7675 /* Get the size of the thumb, a value between 0 and 1. */
7676 BLOCK_INPUT;
ec18280f 7677 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
06a2c219
GM
7678 UNBLOCK_INPUT;
7679
7680 whole = 10000000;
7681 portion = shown < 1 ? top * whole : 0;
06a2c219 7682
ec18280f
SM
7683 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
7684 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
7685 the bottom, so we force the scrolling whenever we see that we're
7686 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
7687 we try to ensure that we always stay two pixels away from the
7688 bottom). */
06a2c219
GM
7689 part = scroll_bar_down_arrow;
7690 else
7691 part = scroll_bar_handle;
7692
7693 window_being_scrolled = bar->window;
7694 bar->dragging = make_number (portion);
7695 last_scroll_bar_part = part;
7696 x_send_scroll_bar_event (bar->window, part, portion, whole);
7697}
7698
7699
ec18280f
SM
7700/* Xaw scroll bar callback. Invoked for incremental scrolling.,
7701 i.e. line or page up or down. WIDGET is the Xaw scroll bar
06a2c219
GM
7702 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
7703 the scroll bar. CALL_DATA is an integer specifying the action that
7704 has taken place. It's magnitude is in the range 0..height of the
7705 scroll bar. Negative values mean scroll towards buffer start.
7706 Values < height of scroll bar mean line-wise movement. */
7707
7708static void
ec18280f 7709xaw_scroll_callback (widget, client_data, call_data)
06a2c219
GM
7710 Widget widget;
7711 XtPointer client_data, call_data;
7712{
7713 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7714 int position = (int) call_data;
7715 Dimension height;
7716 int part;
7717
7718 /* Get the height of the scroll bar. */
7719 BLOCK_INPUT;
7720 XtVaGetValues (widget, XtNheight, &height, NULL);
7721 UNBLOCK_INPUT;
7722
ec18280f
SM
7723 if (abs (position) >= height)
7724 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
7725
7726 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
7727 it maps line-movement to call_data = max(5, height/20). */
7728 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
7729 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
06a2c219 7730 else
ec18280f 7731 part = scroll_bar_move_ratio;
06a2c219
GM
7732
7733 window_being_scrolled = bar->window;
7734 bar->dragging = Qnil;
7735 last_scroll_bar_part = part;
ec18280f 7736 x_send_scroll_bar_event (bar->window, part, position, height);
06a2c219
GM
7737}
7738
7739
7740#endif /* not USE_MOTIF */
7741
7742
7743/* Create the widget for scroll bar BAR on frame F. Record the widget
7744 and X window of the scroll bar in BAR. */
7745
7746static void
7747x_create_toolkit_scroll_bar (f, bar)
7748 struct frame *f;
7749 struct scroll_bar *bar;
7750{
7751 Window xwindow;
7752 Widget widget;
7753 Arg av[20];
7754 int ac = 0;
7755 char *scroll_bar_name = "verticalScrollBar";
7756 unsigned long pixel;
7757
7758 BLOCK_INPUT;
7759
7760#ifdef USE_MOTIF
7761 /* LessTif 0.85, problems:
7762
7763 1. When the mouse if over the scroll bar, the scroll bar will
7764 get keyboard events. I didn't find a way to turn this off.
7765
7766 2. Do we have to explicitly set the cursor to get an arrow
7767 cursor (see below)? */
7768
7769 /* Set resources. Create the widget. */
7770 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7771 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
7772 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
7773 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
7774 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
7775 XtSetArg (av[ac], XmNincrement, 1); ++ac;
7776 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
7777
7778 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7779 if (pixel != -1)
7780 {
7781 XtSetArg (av[ac], XmNforeground, pixel);
7782 ++ac;
7783 }
7784
7785 pixel = f->output_data.x->scroll_bar_background_pixel;
7786 if (pixel != -1)
7787 {
7788 XtSetArg (av[ac], XmNbackground, pixel);
7789 ++ac;
7790 }
7791
7792 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
7793 scroll_bar_name, av, ac);
7794
7795 /* Add one callback for everything that can happen. */
7796 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
7797 (XtPointer) bar);
7798 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
7799 (XtPointer) bar);
7800 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
7801 (XtPointer) bar);
7802 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
7803 (XtPointer) bar);
7804 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
7805 (XtPointer) bar);
7806 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
7807 (XtPointer) bar);
7808 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
7809 (XtPointer) bar);
7810
7811 /* Realize the widget. Only after that is the X window created. */
7812 XtRealizeWidget (widget);
7813
7814 /* Set the cursor to an arrow. I didn't find a resource to do that.
7815 And I'm wondering why it hasn't an arrow cursor by default. */
7816 XDefineCursor (XtDisplay (widget), XtWindow (widget),
7817 f->output_data.x->nontext_cursor);
7818
ec18280f 7819#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
7820
7821 /* Set resources. Create the widget. The background of the
7822 Xaw3d scroll bar widget is a little bit light for my taste.
7823 We don't alter it here to let users change it according
7824 to their taste with `emacs*verticalScrollBar.background: xxx'. */
7825 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7826 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
ec18280f
SM
7827 /* For smoother scrolling with Xaw3d -sm */
7828 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
7829 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
06a2c219
GM
7830
7831 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7832 if (pixel != -1)
7833 {
7834 XtSetArg (av[ac], XtNforeground, pixel);
7835 ++ac;
7836 }
7837
7838 pixel = f->output_data.x->scroll_bar_background_pixel;
7839 if (pixel != -1)
7840 {
7841 XtSetArg (av[ac], XtNbackground, pixel);
7842 ++ac;
7843 }
7844
7845 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
7846 f->output_data.x->edit_widget, av, ac);
ec18280f
SM
7847
7848 {
7849 char *initial = "";
7850 char *val = initial;
7851 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
7852 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
7853 if (val == initial)
7854 { /* ARROW_SCROLL */
7855 xaw3d_arrow_scroll = True;
7856 /* Isn't that just a personal preference ? -sm */
7857 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
7858 }
7859 }
06a2c219
GM
7860
7861 /* Define callbacks. */
ec18280f
SM
7862 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
7863 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
06a2c219
GM
7864 (XtPointer) bar);
7865
7866 /* Realize the widget. Only after that is the X window created. */
7867 XtRealizeWidget (widget);
7868
ec18280f 7869#endif /* !USE_MOTIF */
06a2c219
GM
7870
7871 /* Install an action hook that let's us detect when the user
7872 finishes interacting with a scroll bar. */
7873 if (action_hook_id == 0)
7874 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
7875
7876 /* Remember X window and widget in the scroll bar vector. */
7877 SET_SCROLL_BAR_X_WIDGET (bar, widget);
7878 xwindow = XtWindow (widget);
7879 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
7880
7881 UNBLOCK_INPUT;
7882}
7883
7884
7885/* Set the thumb size and position of scroll bar BAR. We are currently
7886 displaying PORTION out of a whole WHOLE, and our position POSITION. */
7887
7888static void
7889x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
7890 struct scroll_bar *bar;
7891 int portion, position, whole;
f451eb13 7892{
06a2c219 7893 float top, shown;
06a2c219 7894 Widget widget = SCROLL_BAR_X_WIDGET (bar);
f451eb13 7895
06a2c219
GM
7896 if (whole == 0)
7897 top = 0, shown = 1;
7898 else
f451eb13 7899 {
06a2c219
GM
7900 top = (float) position / whole;
7901 shown = (float) portion / whole;
7902 }
f451eb13 7903
06a2c219 7904 BLOCK_INPUT;
f451eb13 7905
06a2c219
GM
7906#ifdef USE_MOTIF
7907 {
7908 int size, value;
7909 Boolean arrow1_selected, arrow2_selected;
7910 unsigned char flags;
7911 XmScrollBarWidget sb;
7912
ec18280f 7913 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
06a2c219
GM
7914 is the scroll bar's maximum and MIN is the scroll bar's minimum
7915 value. */
7916 size = shown * XM_SB_RANGE;
7917 size = min (size, XM_SB_RANGE);
7918 size = max (size, 1);
7919
7920 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
7921 value = top * XM_SB_RANGE;
7922 value = min (value, XM_SB_MAX - size);
7923 value = max (value, XM_SB_MIN);
7924
7925 /* LessTif: Calling XmScrollBarSetValues after an increment or
7926 decrement turns off auto-repeat LessTif-internally. This can
7927 be seen in ScrollBar.c which resets Arrow1Selected and
7928 Arrow2Selected. It also sets internal flags so that LessTif
7929 believes the mouse is in the slider. We either have to change
7930 our code, or work around that by accessing private data. */
7931
7932 sb = (XmScrollBarWidget) widget;
7933 arrow1_selected = sb->scrollBar.arrow1_selected;
7934 arrow2_selected = sb->scrollBar.arrow2_selected;
7935 flags = sb->scrollBar.flags;
7936
7937 if (NILP (bar->dragging))
7938 XmScrollBarSetValues (widget, value, size, 0, 0, False);
7939 else if (last_scroll_bar_part == scroll_bar_down_arrow)
7940 /* This has the negative side effect that the slider value is
ec18280f 7941 not what it would be if we scrolled here using line-wise or
06a2c219
GM
7942 page-wise movement. */
7943 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
7944 else
7945 {
7946 /* If currently dragging, only update the slider size.
7947 This reduces flicker effects. */
7948 int old_value, old_size, increment, page_increment;
7949
7950 XmScrollBarGetValues (widget, &old_value, &old_size,
7951 &increment, &page_increment);
7952 XmScrollBarSetValues (widget, old_value,
7953 min (size, XM_SB_RANGE - old_value),
7954 0, 0, False);
7955 }
7956
7957 sb->scrollBar.arrow1_selected = arrow1_selected;
7958 sb->scrollBar.arrow2_selected = arrow2_selected;
7959 sb->scrollBar.flags = flags;
7960 }
ec18280f 7961#else /* !USE_MOTIF i.e. use Xaw */
06a2c219 7962 {
ec18280f
SM
7963 float old_top, old_shown;
7964 Dimension height;
7965 XtVaGetValues (widget,
7966 XtNtopOfThumb, &old_top,
7967 XtNshown, &old_shown,
7968 XtNheight, &height,
7969 NULL);
7970
7971 /* Massage the top+shown values. */
7972 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
7973 top = max (0, min (1, top));
7974 else
7975 top = old_top;
7976 /* Keep two pixels available for moving the thumb down. */
7977 shown = max (0, min (1 - top - (2.0 / height), shown));
06a2c219
GM
7978
7979 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
7980 check that your system's configuration file contains a define
7981 for `NARROWPROTO'. See s/freebsd.h for an example. */
ec18280f 7982 if (top != old_top || shown != old_shown)
eb393530 7983 {
ec18280f 7984 if (NILP (bar->dragging))
eb393530 7985 XawScrollbarSetThumb (widget, top, shown);
06a2c219
GM
7986 else
7987 {
ec18280f
SM
7988#ifdef HAVE_XAW3D
7989 ScrollbarWidget sb = (ScrollbarWidget) widget;
3e71d8f2 7990 int scroll_mode = 0;
ec18280f
SM
7991
7992 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
7993 if (xaw3d_arrow_scroll)
7994 {
7995 /* Xaw3d stupidly ignores resize requests while dragging
7996 so we have to make it believe it's not in dragging mode. */
7997 scroll_mode = sb->scrollbar.scroll_mode;
7998 if (scroll_mode == 2)
7999 sb->scrollbar.scroll_mode = 0;
8000 }
8001#endif
8002 /* Try to make the scrolling a tad smoother. */
8003 if (!xaw3d_pick_top)
8004 shown = min (shown, old_shown);
8005
8006 XawScrollbarSetThumb (widget, top, shown);
8007
8008#ifdef HAVE_XAW3D
8009 if (xaw3d_arrow_scroll && scroll_mode == 2)
8010 sb->scrollbar.scroll_mode = scroll_mode;
8011#endif
06a2c219 8012 }
06a2c219
GM
8013 }
8014 }
ec18280f 8015#endif /* !USE_MOTIF */
06a2c219
GM
8016
8017 UNBLOCK_INPUT;
f451eb13
JB
8018}
8019
06a2c219
GM
8020#endif /* USE_TOOLKIT_SCROLL_BARS */
8021
8022
8023\f
8024/************************************************************************
8025 Scroll bars, general
8026 ************************************************************************/
8027
8028/* Create a scroll bar and return the scroll bar vector for it. W is
8029 the Emacs window on which to create the scroll bar. TOP, LEFT,
8030 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8031 scroll bar. */
8032
ab648270 8033static struct scroll_bar *
06a2c219
GM
8034x_scroll_bar_create (w, top, left, width, height)
8035 struct window *w;
f451eb13
JB
8036 int top, left, width, height;
8037{
06a2c219 8038 struct frame *f = XFRAME (w->frame);
334208b7
RS
8039 struct scroll_bar *bar
8040 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
f451eb13
JB
8041
8042 BLOCK_INPUT;
8043
06a2c219
GM
8044#if USE_TOOLKIT_SCROLL_BARS
8045 x_create_toolkit_scroll_bar (f, bar);
8046#else /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8047 {
8048 XSetWindowAttributes a;
8049 unsigned long mask;
5c187dee 8050 Window window;
06a2c219
GM
8051
8052 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8053 if (a.background_pixel == -1)
8054 a.background_pixel = f->output_data.x->background_pixel;
8055
12ba150f 8056 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9a572e2a 8057 | ButtonMotionMask | PointerMotionHintMask
12ba150f 8058 | ExposureMask);
7a13e894 8059 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
f451eb13 8060
dbc4e1c1 8061 mask = (CWBackPixel | CWEventMask | CWCursor);
f451eb13 8062
06a2c219
GM
8063 /* Clear the area of W that will serve as a scroll bar. This is
8064 for the case that a window has been split horizontally. In
8065 this case, no clear_frame is generated to reduce flickering. */
8066 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8067 left, top, width,
8068 window_box_height (w), False);
8069
8070 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8071 /* Position and size of scroll bar. */
8072 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8073 top,
8074 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8075 height,
8076 /* Border width, depth, class, and visual. */
8077 0,
8078 CopyFromParent,
8079 CopyFromParent,
8080 CopyFromParent,
8081 /* Attributes. */
8082 mask, &a);
8083 SET_SCROLL_BAR_X_WINDOW (bar, window);
f451eb13 8084 }
06a2c219 8085#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 8086
06a2c219 8087 XSETWINDOW (bar->window, w);
e0c1aef2
KH
8088 XSETINT (bar->top, top);
8089 XSETINT (bar->left, left);
8090 XSETINT (bar->width, width);
8091 XSETINT (bar->height, height);
8092 XSETINT (bar->start, 0);
8093 XSETINT (bar->end, 0);
12ba150f 8094 bar->dragging = Qnil;
f451eb13
JB
8095
8096 /* Add bar to its frame's list of scroll bars. */
334208b7 8097 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 8098 bar->prev = Qnil;
334208b7 8099 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
06a2c219 8100 if (!NILP (bar->next))
e0c1aef2 8101 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13 8102
06a2c219
GM
8103 /* Map the window/widget. */
8104#if USE_TOOLKIT_SCROLL_BARS
8105 XtMapWidget (SCROLL_BAR_X_WIDGET (bar));
8106 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
8107 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8108 top,
8109 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8110 height, 0);
8111#else /* not USE_TOOLKIT_SCROLL_BARS */
7f9c7f94 8112 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
06a2c219 8113#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8114
8115 UNBLOCK_INPUT;
12ba150f 8116 return bar;
f451eb13
JB
8117}
8118
06a2c219 8119
12ba150f 8120/* Draw BAR's handle in the proper position.
06a2c219 8121
12ba150f
JB
8122 If the handle is already drawn from START to END, don't bother
8123 redrawing it, unless REBUILD is non-zero; in that case, always
8124 redraw it. (REBUILD is handy for drawing the handle after expose
58769bee 8125 events.)
12ba150f
JB
8126
8127 Normally, we want to constrain the start and end of the handle to
06a2c219
GM
8128 fit inside its rectangle, but if the user is dragging the scroll
8129 bar handle, we want to let them drag it down all the way, so that
8130 the bar's top is as far down as it goes; otherwise, there's no way
8131 to move to the very end of the buffer. */
8132
5c187dee
GM
8133#ifndef USE_TOOLKIT_SCROLL_BARS
8134
f451eb13 8135static void
ab648270
JB
8136x_scroll_bar_set_handle (bar, start, end, rebuild)
8137 struct scroll_bar *bar;
f451eb13 8138 int start, end;
12ba150f 8139 int rebuild;
f451eb13 8140{
12ba150f 8141 int dragging = ! NILP (bar->dragging);
ab648270 8142 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8143 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8144 GC gc = f->output_data.x->normal_gc;
12ba150f
JB
8145
8146 /* If the display is already accurate, do nothing. */
8147 if (! rebuild
8148 && start == XINT (bar->start)
8149 && end == XINT (bar->end))
8150 return;
8151
f451eb13
JB
8152 BLOCK_INPUT;
8153
8154 {
d9cdbb3d
RS
8155 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8156 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8157 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
f451eb13
JB
8158
8159 /* Make sure the values are reasonable, and try to preserve
8160 the distance between start and end. */
12ba150f
JB
8161 {
8162 int length = end - start;
8163
8164 if (start < 0)
8165 start = 0;
8166 else if (start > top_range)
8167 start = top_range;
8168 end = start + length;
8169
8170 if (end < start)
8171 end = start;
8172 else if (end > top_range && ! dragging)
8173 end = top_range;
8174 }
f451eb13 8175
ab648270 8176 /* Store the adjusted setting in the scroll bar. */
e0c1aef2
KH
8177 XSETINT (bar->start, start);
8178 XSETINT (bar->end, end);
f451eb13 8179
12ba150f
JB
8180 /* Clip the end position, just for display. */
8181 if (end > top_range)
8182 end = top_range;
f451eb13 8183
ab648270 8184 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
12ba150f
JB
8185 below top positions, to make sure the handle is always at least
8186 that many pixels tall. */
ab648270 8187 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
f451eb13 8188
12ba150f
JB
8189 /* Draw the empty space above the handle. Note that we can't clear
8190 zero-height areas; that means "clear to end of window." */
8191 if (0 < start)
334208b7 8192 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 8193
12ba150f 8194 /* x, y, width, height, and exposures. */
ab648270
JB
8195 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8196 VERTICAL_SCROLL_BAR_TOP_BORDER,
12ba150f
JB
8197 inside_width, start,
8198 False);
f451eb13 8199
06a2c219
GM
8200 /* Change to proper foreground color if one is specified. */
8201 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8202 XSetForeground (FRAME_X_DISPLAY (f), gc,
8203 f->output_data.x->scroll_bar_foreground_pixel);
8204
12ba150f 8205 /* Draw the handle itself. */
334208b7 8206 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13 8207
12ba150f 8208 /* x, y, width, height */
ab648270
JB
8209 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8210 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
12ba150f 8211 inside_width, end - start);
f451eb13 8212
06a2c219
GM
8213 /* Restore the foreground color of the GC if we changed it above. */
8214 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8215 XSetForeground (FRAME_X_DISPLAY (f), gc,
8216 f->output_data.x->foreground_pixel);
f451eb13 8217
12ba150f
JB
8218 /* Draw the empty space below the handle. Note that we can't
8219 clear zero-height areas; that means "clear to end of window." */
8220 if (end < inside_height)
334208b7 8221 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 8222
12ba150f 8223 /* x, y, width, height, and exposures. */
ab648270
JB
8224 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8225 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
12ba150f
JB
8226 inside_width, inside_height - end,
8227 False);
f451eb13 8228
f451eb13
JB
8229 }
8230
f451eb13
JB
8231 UNBLOCK_INPUT;
8232}
8233
5c187dee 8234#endif /* !USE_TOOLKIT_SCROLL_BARS */
f451eb13 8235
06a2c219
GM
8236/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8237 nil. */
58769bee 8238
12ba150f 8239static void
ab648270
JB
8240x_scroll_bar_remove (bar)
8241 struct scroll_bar *bar;
12ba150f 8242{
12ba150f
JB
8243 BLOCK_INPUT;
8244
06a2c219
GM
8245#if USE_TOOLKIT_SCROLL_BARS
8246 XtDestroyWidget (SCROLL_BAR_X_WIDGET (bar));
8247#else /* not USE_TOOLKIT_SCROLL_BARS */
5c187dee
GM
8248 {
8249 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8250 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8251 }
06a2c219
GM
8252#endif /* not USE_TOOLKIT_SCROLL_BARS */
8253
ab648270
JB
8254 /* Disassociate this scroll bar from its window. */
8255 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
12ba150f
JB
8256
8257 UNBLOCK_INPUT;
8258}
8259
06a2c219 8260
12ba150f
JB
8261/* Set the handle of the vertical scroll bar for WINDOW to indicate
8262 that we are displaying PORTION characters out of a total of WHOLE
ab648270 8263 characters, starting at POSITION. If WINDOW has no scroll bar,
12ba150f 8264 create one. */
06a2c219 8265
12ba150f 8266static void
06a2c219
GM
8267XTset_vertical_scroll_bar (w, portion, whole, position)
8268 struct window *w;
f451eb13
JB
8269 int portion, whole, position;
8270{
06a2c219 8271 struct frame *f = XFRAME (w->frame);
ab648270 8272 struct scroll_bar *bar;
3c6ede7b 8273 int top, height, left, sb_left, width, sb_width;
06a2c219 8274 int window_x, window_y, window_width, window_height;
06a2c219 8275
3c6ede7b 8276 /* Get window dimensions. */
06a2c219 8277 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
3c6ede7b
GM
8278 top = window_y;
8279 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8280 height = window_height;
06a2c219 8281
3c6ede7b 8282 /* Compute the left edge of the scroll bar area. */
06a2c219 8283 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
3c6ede7b
GM
8284 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8285 else
8286 left = XFASTINT (w->left);
8287 left *= CANON_X_UNIT (f);
8288 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8289
8290 /* Compute the width of the scroll bar which might be less than
8291 the width of the area reserved for the scroll bar. */
8292 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8293 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
06a2c219 8294 else
3c6ede7b 8295 sb_width = width;
12ba150f 8296
3c6ede7b
GM
8297 /* Compute the left edge of the scroll bar. */
8298#ifdef USE_TOOLKIT_SCROLL_BARS
8299 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8300 sb_left = left + width - sb_width - (width - sb_width) / 2;
8301 else
8302 sb_left = left + (width - sb_width) / 2;
8303#else
8304 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8305 sb_left = left + width - sb_width;
8306 else
8307 sb_left = left;
8308#endif
8309
ab648270 8310 /* Does the scroll bar exist yet? */
06a2c219 8311 if (NILP (w->vertical_scroll_bar))
3c6ede7b 8312 {
80c32bcc 8313 BLOCK_INPUT;
3c6ede7b
GM
8314 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8315 left, top, width, height, False);
80c32bcc 8316 UNBLOCK_INPUT;
3c6ede7b
GM
8317 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8318 }
f451eb13 8319 else
12ba150f
JB
8320 {
8321 /* It may just need to be moved and resized. */
06a2c219
GM
8322 unsigned int mask = 0;
8323
8324 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8325
8326 BLOCK_INPUT;
8327
3c6ede7b 8328 if (sb_left != XINT (bar->left))
06a2c219 8329 mask |= CWX;
3c6ede7b 8330 if (top != XINT (bar->top))
06a2c219 8331 mask |= CWY;
3c6ede7b 8332 if (sb_width != XINT (bar->width))
06a2c219 8333 mask |= CWWidth;
3c6ede7b 8334 if (height != XINT (bar->height))
06a2c219
GM
8335 mask |= CWHeight;
8336
8337#ifdef USE_TOOLKIT_SCROLL_BARS
fe6f39d9
GM
8338
8339 /* Since toolkit scroll bars are smaller than the space reserved
8340 for them on the frame, we have to clear "under" them. */
8341 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3c6ede7b 8342 left, top, width, height, False);
06a2c219
GM
8343
8344 /* Move/size the scroll bar widget. */
8345 if (mask)
8346 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
3c6ede7b
GM
8347 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8348 top,
8349 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8350 height, 0);
06a2c219
GM
8351
8352#else /* not USE_TOOLKIT_SCROLL_BARS */
8353
e1f6572f
RS
8354 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8355 {
8356 /* Clear areas not covered by the scroll bar. This makes sure a
8357 previous mode line display is cleared after C-x 2 C-x 1, for
8358 example. Non-toolkit scroll bars are as wide as the area
8359 reserved for scroll bars - trim at both sides. */
8360 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8361 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8362 height, False);
8363 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8364 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8365 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8366 height, False);
8367 }
06a2c219
GM
8368
8369 /* Move/size the scroll bar window. */
8370 if (mask)
8371 {
8372 XWindowChanges wc;
8373
3c6ede7b
GM
8374 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8375 wc.y = top;
8376 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8377 wc.height = height;
06a2c219
GM
8378 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8379 mask, &wc);
8380 }
8381
8382#endif /* not USE_TOOLKIT_SCROLL_BARS */
8383
8384 /* Remember new settings. */
3c6ede7b
GM
8385 XSETINT (bar->left, sb_left);
8386 XSETINT (bar->top, top);
8387 XSETINT (bar->width, sb_width);
8388 XSETINT (bar->height, height);
06a2c219
GM
8389
8390 UNBLOCK_INPUT;
12ba150f 8391 }
f451eb13 8392
06a2c219
GM
8393#if USE_TOOLKIT_SCROLL_BARS
8394 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8395#else /* not USE_TOOLKIT_SCROLL_BARS */
ab648270 8396 /* Set the scroll bar's current state, unless we're currently being
f451eb13 8397 dragged. */
12ba150f 8398 if (NILP (bar->dragging))
f451eb13 8399 {
92857db0 8400 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
f451eb13 8401
12ba150f 8402 if (whole == 0)
ab648270 8403 x_scroll_bar_set_handle (bar, 0, top_range, 0);
12ba150f
JB
8404 else
8405 {
43f868f5
JB
8406 int start = ((double) position * top_range) / whole;
8407 int end = ((double) (position + portion) * top_range) / whole;
ab648270 8408 x_scroll_bar_set_handle (bar, start, end, 0);
12ba150f 8409 }
f451eb13 8410 }
06a2c219 8411#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 8412
06a2c219 8413 XSETVECTOR (w->vertical_scroll_bar, bar);
f451eb13
JB
8414}
8415
12ba150f 8416
f451eb13 8417/* The following three hooks are used when we're doing a thorough
ab648270 8418 redisplay of the frame. We don't explicitly know which scroll bars
f451eb13 8419 are going to be deleted, because keeping track of when windows go
12ba150f
JB
8420 away is a real pain - "Can you say set-window-configuration, boys
8421 and girls?" Instead, we just assert at the beginning of redisplay
ab648270 8422 that *all* scroll bars are to be removed, and then save a scroll bar
12ba150f 8423 from the fiery pit when we actually redisplay its window. */
f451eb13 8424
ab648270
JB
8425/* Arrange for all scroll bars on FRAME to be removed at the next call
8426 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
06a2c219
GM
8427 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8428
58769bee 8429static void
ab648270 8430XTcondemn_scroll_bars (frame)
f451eb13
JB
8431 FRAME_PTR frame;
8432{
f9e24cb9
RS
8433 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8434 while (! NILP (FRAME_SCROLL_BARS (frame)))
8435 {
8436 Lisp_Object bar;
8437 bar = FRAME_SCROLL_BARS (frame);
8438 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8439 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8440 XSCROLL_BAR (bar)->prev = Qnil;
8441 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8442 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8443 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8444 }
f451eb13
JB
8445}
8446
06a2c219 8447/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
12ba150f 8448 Note that WINDOW isn't necessarily condemned at all. */
f451eb13 8449static void
ab648270 8450XTredeem_scroll_bar (window)
12ba150f 8451 struct window *window;
f451eb13 8452{
ab648270 8453 struct scroll_bar *bar;
12ba150f 8454
ab648270
JB
8455 /* We can't redeem this window's scroll bar if it doesn't have one. */
8456 if (NILP (window->vertical_scroll_bar))
12ba150f
JB
8457 abort ();
8458
ab648270 8459 bar = XSCROLL_BAR (window->vertical_scroll_bar);
12ba150f
JB
8460
8461 /* Unlink it from the condemned list. */
8462 {
8463 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
8464
8465 if (NILP (bar->prev))
8466 {
8467 /* If the prev pointer is nil, it must be the first in one of
8468 the lists. */
ab648270 8469 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
12ba150f
JB
8470 /* It's not condemned. Everything's fine. */
8471 return;
ab648270
JB
8472 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8473 window->vertical_scroll_bar))
8474 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
12ba150f
JB
8475 else
8476 /* If its prev pointer is nil, it must be at the front of
8477 one or the other! */
8478 abort ();
8479 }
8480 else
ab648270 8481 XSCROLL_BAR (bar->prev)->next = bar->next;
12ba150f
JB
8482
8483 if (! NILP (bar->next))
ab648270 8484 XSCROLL_BAR (bar->next)->prev = bar->prev;
12ba150f 8485
ab648270 8486 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 8487 bar->prev = Qnil;
e0c1aef2 8488 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
12ba150f 8489 if (! NILP (bar->next))
e0c1aef2 8490 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
12ba150f 8491 }
f451eb13
JB
8492}
8493
ab648270
JB
8494/* Remove all scroll bars on FRAME that haven't been saved since the
8495 last call to `*condemn_scroll_bars_hook'. */
06a2c219 8496
f451eb13 8497static void
ab648270 8498XTjudge_scroll_bars (f)
12ba150f 8499 FRAME_PTR f;
f451eb13 8500{
12ba150f 8501 Lisp_Object bar, next;
f451eb13 8502
ab648270 8503 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
cf7cb199
JB
8504
8505 /* Clear out the condemned list now so we won't try to process any
ab648270
JB
8506 more events on the hapless scroll bars. */
8507 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
cf7cb199
JB
8508
8509 for (; ! NILP (bar); bar = next)
f451eb13 8510 {
ab648270 8511 struct scroll_bar *b = XSCROLL_BAR (bar);
12ba150f 8512
ab648270 8513 x_scroll_bar_remove (b);
12ba150f
JB
8514
8515 next = b->next;
8516 b->next = b->prev = Qnil;
f451eb13 8517 }
12ba150f 8518
ab648270 8519 /* Now there should be no references to the condemned scroll bars,
12ba150f 8520 and they should get garbage-collected. */
f451eb13
JB
8521}
8522
8523
06a2c219
GM
8524/* Handle an Expose or GraphicsExpose event on a scroll bar. This
8525 is a no-op when using toolkit scroll bars.
ab648270
JB
8526
8527 This may be called from a signal handler, so we have to ignore GC
8528 mark bits. */
06a2c219 8529
f451eb13 8530static void
ab648270
JB
8531x_scroll_bar_expose (bar, event)
8532 struct scroll_bar *bar;
f451eb13
JB
8533 XEvent *event;
8534{
06a2c219
GM
8535#ifndef USE_TOOLKIT_SCROLL_BARS
8536
ab648270 8537 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8538 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8539 GC gc = f->output_data.x->normal_gc;
3cbd2e0b 8540 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
12ba150f 8541
f451eb13
JB
8542 BLOCK_INPUT;
8543
ab648270 8544 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
f451eb13 8545
06a2c219 8546 /* Draw a one-pixel border just inside the edges of the scroll bar. */
334208b7 8547 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13
JB
8548
8549 /* x, y, width, height */
d9cdbb3d 8550 0, 0,
3cbd2e0b 8551 XINT (bar->width) - 1 - width_trim - width_trim,
d9cdbb3d
RS
8552 XINT (bar->height) - 1);
8553
f451eb13 8554 UNBLOCK_INPUT;
06a2c219
GM
8555
8556#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8557}
8558
ab648270
JB
8559/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8560 is set to something other than no_event, it is enqueued.
8561
8562 This may be called from a signal handler, so we have to ignore GC
8563 mark bits. */
06a2c219 8564
5c187dee
GM
8565#ifndef USE_TOOLKIT_SCROLL_BARS
8566
f451eb13 8567static void
ab648270
JB
8568x_scroll_bar_handle_click (bar, event, emacs_event)
8569 struct scroll_bar *bar;
f451eb13
JB
8570 XEvent *event;
8571 struct input_event *emacs_event;
8572{
0299d313 8573 if (! GC_WINDOWP (bar->window))
12ba150f
JB
8574 abort ();
8575
ab648270 8576 emacs_event->kind = scroll_bar_click;
69388238 8577 emacs_event->code = event->xbutton.button - Button1;
0299d313
RS
8578 emacs_event->modifiers
8579 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8580 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8581 event->xbutton.state)
8582 | (event->type == ButtonRelease
8583 ? up_modifier
8584 : down_modifier));
12ba150f 8585 emacs_event->frame_or_window = bar->window;
0f8aabe9 8586 emacs_event->arg = Qnil;
f451eb13 8587 emacs_event->timestamp = event->xbutton.time;
12ba150f 8588 {
06a2c219 8589#if 0
d9cdbb3d 8590 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
0299d313 8591 int internal_height
d9cdbb3d 8592 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8593#endif
0299d313 8594 int top_range
d9cdbb3d 8595 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ab648270 8596 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
12ba150f
JB
8597
8598 if (y < 0) y = 0;
8599 if (y > top_range) y = top_range;
8600
8601 if (y < XINT (bar->start))
ab648270
JB
8602 emacs_event->part = scroll_bar_above_handle;
8603 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8604 emacs_event->part = scroll_bar_handle;
12ba150f 8605 else
ab648270 8606 emacs_event->part = scroll_bar_below_handle;
929787e1
JB
8607
8608 /* Just because the user has clicked on the handle doesn't mean
5116f055
JB
8609 they want to drag it. Lisp code needs to be able to decide
8610 whether or not we're dragging. */
929787e1 8611#if 0
12ba150f
JB
8612 /* If the user has just clicked on the handle, record where they're
8613 holding it. */
8614 if (event->type == ButtonPress
ab648270 8615 && emacs_event->part == scroll_bar_handle)
e0c1aef2 8616 XSETINT (bar->dragging, y - XINT (bar->start));
929787e1 8617#endif
12ba150f
JB
8618
8619 /* If the user has released the handle, set it to its final position. */
8620 if (event->type == ButtonRelease
8621 && ! NILP (bar->dragging))
8622 {
8623 int new_start = y - XINT (bar->dragging);
8624 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
f451eb13 8625
ab648270 8626 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
12ba150f
JB
8627 bar->dragging = Qnil;
8628 }
f451eb13 8629
5116f055
JB
8630 /* Same deal here as the other #if 0. */
8631#if 0
58769bee 8632 /* Clicks on the handle are always reported as occurring at the top of
12ba150f 8633 the handle. */
ab648270 8634 if (emacs_event->part == scroll_bar_handle)
12ba150f
JB
8635 emacs_event->x = bar->start;
8636 else
e0c1aef2 8637 XSETINT (emacs_event->x, y);
5116f055 8638#else
e0c1aef2 8639 XSETINT (emacs_event->x, y);
5116f055 8640#endif
f451eb13 8641
e0c1aef2 8642 XSETINT (emacs_event->y, top_range);
12ba150f
JB
8643 }
8644}
f451eb13 8645
ab648270
JB
8646/* Handle some mouse motion while someone is dragging the scroll bar.
8647
8648 This may be called from a signal handler, so we have to ignore GC
8649 mark bits. */
06a2c219 8650
f451eb13 8651static void
ab648270
JB
8652x_scroll_bar_note_movement (bar, event)
8653 struct scroll_bar *bar;
f451eb13
JB
8654 XEvent *event;
8655{
39d8bb4d
KH
8656 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8657
f451eb13
JB
8658 last_mouse_movement_time = event->xmotion.time;
8659
39d8bb4d 8660 f->mouse_moved = 1;
e0c1aef2 8661 XSETVECTOR (last_mouse_scroll_bar, bar);
f451eb13
JB
8662
8663 /* If we're dragging the bar, display it. */
ab648270 8664 if (! GC_NILP (bar->dragging))
f451eb13
JB
8665 {
8666 /* Where should the handle be now? */
12ba150f 8667 int new_start = event->xmotion.y - XINT (bar->dragging);
f451eb13 8668
12ba150f 8669 if (new_start != XINT (bar->start))
f451eb13 8670 {
12ba150f 8671 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
58769bee 8672
ab648270 8673 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
f451eb13
JB
8674 }
8675 }
f451eb13
JB
8676}
8677
5c187dee
GM
8678#endif /* !USE_TOOLKIT_SCROLL_BARS */
8679
12ba150f 8680/* Return information to the user about the current position of the mouse
ab648270 8681 on the scroll bar. */
06a2c219 8682
12ba150f 8683static void
334208b7
RS
8684x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8685 FRAME_PTR *fp;
12ba150f 8686 Lisp_Object *bar_window;
ab648270 8687 enum scroll_bar_part *part;
12ba150f
JB
8688 Lisp_Object *x, *y;
8689 unsigned long *time;
8690{
ab648270 8691 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
334208b7
RS
8692 Window w = SCROLL_BAR_X_WINDOW (bar);
8693 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f 8694 int win_x, win_y;
559cb2fb
JB
8695 Window dummy_window;
8696 int dummy_coord;
8697 unsigned int dummy_mask;
12ba150f 8698
cf7cb199
JB
8699 BLOCK_INPUT;
8700
ab648270 8701 /* Get the mouse's position relative to the scroll bar window, and
12ba150f 8702 report that. */
334208b7 8703 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
12ba150f 8704
559cb2fb
JB
8705 /* Root, child, root x and root y. */
8706 &dummy_window, &dummy_window,
8707 &dummy_coord, &dummy_coord,
12ba150f 8708
559cb2fb
JB
8709 /* Position relative to scroll bar. */
8710 &win_x, &win_y,
12ba150f 8711
559cb2fb
JB
8712 /* Mouse buttons and modifier keys. */
8713 &dummy_mask))
7a13e894 8714 ;
559cb2fb
JB
8715 else
8716 {
06a2c219 8717#if 0
559cb2fb 8718 int inside_height
d9cdbb3d 8719 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8720#endif
559cb2fb 8721 int top_range
d9cdbb3d 8722 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
559cb2fb
JB
8723
8724 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
8725
8726 if (! NILP (bar->dragging))
8727 win_y -= XINT (bar->dragging);
8728
8729 if (win_y < 0)
8730 win_y = 0;
8731 if (win_y > top_range)
8732 win_y = top_range;
8733
334208b7 8734 *fp = f;
7a13e894 8735 *bar_window = bar->window;
559cb2fb
JB
8736
8737 if (! NILP (bar->dragging))
8738 *part = scroll_bar_handle;
8739 else if (win_y < XINT (bar->start))
8740 *part = scroll_bar_above_handle;
8741 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8742 *part = scroll_bar_handle;
8743 else
8744 *part = scroll_bar_below_handle;
12ba150f 8745
e0c1aef2
KH
8746 XSETINT (*x, win_y);
8747 XSETINT (*y, top_range);
12ba150f 8748
39d8bb4d 8749 f->mouse_moved = 0;
559cb2fb
JB
8750 last_mouse_scroll_bar = Qnil;
8751 }
12ba150f 8752
559cb2fb 8753 *time = last_mouse_movement_time;
cf7cb199 8754
cf7cb199 8755 UNBLOCK_INPUT;
12ba150f
JB
8756}
8757
f451eb13 8758
dbc4e1c1 8759/* The screen has been cleared so we may have changed foreground or
ab648270
JB
8760 background colors, and the scroll bars may need to be redrawn.
8761 Clear out the scroll bars, and ask for expose events, so we can
dbc4e1c1
JB
8762 redraw them. */
8763
dfcf069d 8764void
ab648270 8765x_scroll_bar_clear (f)
dbc4e1c1
JB
8766 FRAME_PTR f;
8767{
06a2c219 8768#ifndef USE_TOOLKIT_SCROLL_BARS
dbc4e1c1
JB
8769 Lisp_Object bar;
8770
b80c363e
RS
8771 /* We can have scroll bars even if this is 0,
8772 if we just turned off scroll bar mode.
8773 But in that case we should not clear them. */
8774 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8775 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
8776 bar = XSCROLL_BAR (bar)->next)
8777 XClearArea (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
8778 0, 0, 0, 0, True);
06a2c219 8779#endif /* not USE_TOOLKIT_SCROLL_BARS */
dbc4e1c1
JB
8780}
8781
06a2c219 8782/* This processes Expose events from the menu-bar specific X event
19126e11 8783 loop in xmenu.c. This allows to redisplay the frame if necessary
06a2c219 8784 when handling menu-bar or pop-up items. */
3afe33e7 8785
06a2c219 8786int
3afe33e7
RS
8787process_expose_from_menu (event)
8788 XEvent event;
8789{
8790 FRAME_PTR f;
19126e11 8791 struct x_display_info *dpyinfo;
06a2c219 8792 int frame_exposed_p = 0;
3afe33e7 8793
f94397b5
KH
8794 BLOCK_INPUT;
8795
19126e11
KH
8796 dpyinfo = x_display_info_for_display (event.xexpose.display);
8797 f = x_window_to_frame (dpyinfo, event.xexpose.window);
3afe33e7
RS
8798 if (f)
8799 {
8800 if (f->async_visible == 0)
8801 {
8802 f->async_visible = 1;
8803 f->async_iconified = 0;
06c488fd 8804 f->output_data.x->has_been_visible = 1;
3afe33e7
RS
8805 SET_FRAME_GARBAGED (f);
8806 }
8807 else
8808 {
06a2c219
GM
8809 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
8810 event.xexpose.x, event.xexpose.y,
8811 event.xexpose.width, event.xexpose.height);
8812 frame_exposed_p = 1;
3afe33e7
RS
8813 }
8814 }
8815 else
8816 {
8817 struct scroll_bar *bar
8818 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 8819
3afe33e7
RS
8820 if (bar)
8821 x_scroll_bar_expose (bar, &event);
8822 }
f94397b5
KH
8823
8824 UNBLOCK_INPUT;
06a2c219 8825 return frame_exposed_p;
3afe33e7 8826}
09756a85
RS
8827\f
8828/* Define a queue to save up SelectionRequest events for later handling. */
8829
8830struct selection_event_queue
8831 {
8832 XEvent event;
8833 struct selection_event_queue *next;
8834 };
8835
8836static struct selection_event_queue *queue;
8837
8838/* Nonzero means queue up certain events--don't process them yet. */
06a2c219 8839
09756a85
RS
8840static int x_queue_selection_requests;
8841
8842/* Queue up an X event *EVENT, to be processed later. */
dbc4e1c1 8843
09756a85 8844static void
334208b7
RS
8845x_queue_event (f, event)
8846 FRAME_PTR f;
09756a85
RS
8847 XEvent *event;
8848{
8849 struct selection_event_queue *queue_tmp
06a2c219 8850 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
09756a85 8851
58769bee 8852 if (queue_tmp != NULL)
09756a85
RS
8853 {
8854 queue_tmp->event = *event;
8855 queue_tmp->next = queue;
8856 queue = queue_tmp;
8857 }
8858}
8859
8860/* Take all the queued events and put them back
8861 so that they get processed afresh. */
8862
8863static void
db3906fd
RS
8864x_unqueue_events (display)
8865 Display *display;
09756a85 8866{
58769bee 8867 while (queue != NULL)
09756a85
RS
8868 {
8869 struct selection_event_queue *queue_tmp = queue;
db3906fd 8870 XPutBackEvent (display, &queue_tmp->event);
09756a85 8871 queue = queue_tmp->next;
06a2c219 8872 xfree ((char *)queue_tmp);
09756a85
RS
8873 }
8874}
8875
8876/* Start queuing SelectionRequest events. */
8877
8878void
db3906fd
RS
8879x_start_queuing_selection_requests (display)
8880 Display *display;
09756a85
RS
8881{
8882 x_queue_selection_requests++;
8883}
8884
8885/* Stop queuing SelectionRequest events. */
8886
8887void
db3906fd
RS
8888x_stop_queuing_selection_requests (display)
8889 Display *display;
09756a85
RS
8890{
8891 x_queue_selection_requests--;
db3906fd 8892 x_unqueue_events (display);
09756a85 8893}
f451eb13
JB
8894\f
8895/* The main X event-reading loop - XTread_socket. */
dc6f92b8 8896
06a2c219 8897/* Time stamp of enter window event. This is only used by XTread_socket,
dc6f92b8
JB
8898 but we have to put it out here, since static variables within functions
8899 sometimes don't work. */
06a2c219 8900
dc6f92b8
JB
8901static Time enter_timestamp;
8902
11edeb03 8903/* This holds the state XLookupString needs to implement dead keys
58769bee 8904 and other tricks known as "compose processing". _X Window System_
11edeb03
JB
8905 says that a portable program can't use this, but Stephen Gildea assures
8906 me that letting the compiler initialize it to zeros will work okay.
8907
8908 This must be defined outside of XTread_socket, for the same reasons
06a2c219
GM
8909 given for enter_time stamp, above. */
8910
11edeb03
JB
8911static XComposeStatus compose_status;
8912
10e6549c
RS
8913/* Record the last 100 characters stored
8914 to help debug the loss-of-chars-during-GC problem. */
06a2c219 8915
2224b905
RS
8916static int temp_index;
8917static short temp_buffer[100];
10e6549c 8918
7a13e894
RS
8919/* Set this to nonzero to fake an "X I/O error"
8920 on a particular display. */
06a2c219 8921
7a13e894
RS
8922struct x_display_info *XTread_socket_fake_io_error;
8923
2224b905
RS
8924/* When we find no input here, we occasionally do a no-op command
8925 to verify that the X server is still running and we can still talk with it.
8926 We try all the open displays, one by one.
8927 This variable is used for cycling thru the displays. */
06a2c219 8928
2224b905
RS
8929static struct x_display_info *next_noop_dpyinfo;
8930
06a2c219
GM
8931#define SET_SAVED_MENU_EVENT(size) \
8932 do \
8933 { \
8934 if (f->output_data.x->saved_menu_event == 0) \
8935 f->output_data.x->saved_menu_event \
8936 = (XEvent *) xmalloc (sizeof (XEvent)); \
8937 bcopy (&event, f->output_data.x->saved_menu_event, size); \
8938 if (numchars >= 1) \
8939 { \
8940 bufp->kind = menu_bar_activate_event; \
8941 XSETFRAME (bufp->frame_or_window, f); \
0f8aabe9 8942 bufp->arg = Qnil; \
06a2c219
GM
8943 bufp++; \
8944 count++; \
8945 numchars--; \
8946 } \
8947 } \
8948 while (0)
8949
8805890a 8950#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
06a2c219 8951#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8805890a 8952
dc6f92b8
JB
8953/* Read events coming from the X server.
8954 This routine is called by the SIGIO handler.
8955 We return as soon as there are no more events to be read.
8956
8957 Events representing keys are stored in buffer BUFP,
8958 which can hold up to NUMCHARS characters.
8959 We return the number of characters stored into the buffer,
8960 thus pretending to be `read'.
8961
dc6f92b8
JB
8962 EXPECTED is nonzero if the caller knows input is available. */
8963
7c5283e4 8964int
f66868ba 8965XTread_socket (sd, bufp, numchars, expected)
dc6f92b8 8966 register int sd;
8805890a
KH
8967 /* register */ struct input_event *bufp;
8968 /* register */ int numchars;
dc6f92b8
JB
8969 int expected;
8970{
8971 int count = 0;
8972 int nbytes = 0;
dc6f92b8 8973 XEvent event;
f676886a 8974 struct frame *f;
66f55a9d 8975 int event_found = 0;
334208b7 8976 struct x_display_info *dpyinfo;
dc6f92b8 8977
9ac0d9e0 8978 if (interrupt_input_blocked)
dc6f92b8 8979 {
9ac0d9e0 8980 interrupt_input_pending = 1;
dc6f92b8
JB
8981 return -1;
8982 }
8983
9ac0d9e0 8984 interrupt_input_pending = 0;
dc6f92b8 8985 BLOCK_INPUT;
c0a04927
RS
8986
8987 /* So people can tell when we have read the available input. */
8988 input_signal_count++;
8989
dc6f92b8 8990 if (numchars <= 0)
06a2c219 8991 abort (); /* Don't think this happens. */
dc6f92b8 8992
bde5503b
GM
8993 ++handling_signal;
8994
7a13e894
RS
8995 /* Find the display we are supposed to read input for.
8996 It's the one communicating on descriptor SD. */
8997 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
8998 {
8999#if 0 /* This ought to be unnecessary; let's verify it. */
dc6f92b8 9000#ifdef FIOSNBIO
7a13e894
RS
9001 /* If available, Xlib uses FIOSNBIO to make the socket
9002 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
e6cbea31 9003 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
06a2c219 9004 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7a13e894 9005 fcntl (dpyinfo->connection, F_SETFL, 0);
c118dd06 9006#endif /* ! defined (FIOSNBIO) */
7a13e894 9007#endif
dc6f92b8 9008
7a13e894
RS
9009#if 0 /* This code can't be made to work, with multiple displays,
9010 and appears not to be used on any system any more.
9011 Also keyboard.c doesn't turn O_NDELAY on and off
9012 for X connections. */
dc6f92b8
JB
9013#ifndef SIGIO
9014#ifndef HAVE_SELECT
7a13e894
RS
9015 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9016 {
9017 extern int read_alarm_should_throw;
9018 read_alarm_should_throw = 1;
9019 XPeekEvent (dpyinfo->display, &event);
9020 read_alarm_should_throw = 0;
9021 }
c118dd06
JB
9022#endif /* HAVE_SELECT */
9023#endif /* SIGIO */
7a13e894 9024#endif
dc6f92b8 9025
7a13e894
RS
9026 /* For debugging, this gives a way to fake an I/O error. */
9027 if (dpyinfo == XTread_socket_fake_io_error)
9028 {
9029 XTread_socket_fake_io_error = 0;
9030 x_io_error_quitter (dpyinfo->display);
9031 }
dc6f92b8 9032
06a2c219 9033 while (XPending (dpyinfo->display))
dc6f92b8 9034 {
7a13e894 9035 XNextEvent (dpyinfo->display, &event);
06a2c219 9036
531483fb 9037#ifdef HAVE_X_I18N
d1bc4182 9038 {
f2be1146
GM
9039 /* Filter events for the current X input method.
9040 XFilterEvent returns non-zero if the input method has
9041 consumed the event. We pass the frame's X window to
9042 XFilterEvent because that's the one for which the IC
9043 was created. */
f5d11644
GM
9044 struct frame *f1 = x_any_window_to_frame (dpyinfo,
9045 event.xclient.window);
9046 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
d1bc4182
RS
9047 break;
9048 }
0cd6403b 9049#endif
7a13e894
RS
9050 event_found = 1;
9051
9052 switch (event.type)
9053 {
9054 case ClientMessage:
c047688c 9055 {
7a13e894
RS
9056 if (event.xclient.message_type
9057 == dpyinfo->Xatom_wm_protocols
9058 && event.xclient.format == 32)
c047688c 9059 {
7a13e894
RS
9060 if (event.xclient.data.l[0]
9061 == dpyinfo->Xatom_wm_take_focus)
c047688c 9062 {
8c1a6a84
RS
9063 /* Use x_any_window_to_frame because this
9064 could be the shell widget window
9065 if the frame has no title bar. */
9066 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6c183ba5
RS
9067#ifdef HAVE_X_I18N
9068 /* Not quite sure this is needed -pd */
8c1a6a84 9069 if (f && FRAME_XIC (f))
6c183ba5
RS
9070 XSetICFocus (FRAME_XIC (f));
9071#endif
f1da8f06
GM
9072#if 0 /* Emacs sets WM hints whose `input' field is `true'. This
9073 instructs the WM to set the input focus automatically for
9074 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
9075 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
9076 it has set the focus. So, XSetInputFocus below is not
9077 needed.
9078
9079 The call to XSetInputFocus below has also caused trouble. In
9080 cases where the XSetInputFocus done by the WM and the one
9081 below are temporally close (on a fast machine), the call
9082 below can generate additional FocusIn events which confuse
9083 Emacs. */
9084
bf7253f4
RS
9085 /* Since we set WM_TAKE_FOCUS, we must call
9086 XSetInputFocus explicitly. But not if f is null,
9087 since that might be an event for a deleted frame. */
7a13e894 9088 if (f)
bf7253f4
RS
9089 {
9090 Display *d = event.xclient.display;
9091 /* Catch and ignore errors, in case window has been
9092 iconified by a window manager such as GWM. */
9093 int count = x_catch_errors (d);
9094 XSetInputFocus (d, event.xclient.window,
e1f6572f
RS
9095 /* The ICCCM says this is
9096 the only valid choice. */
9097 RevertToParent,
bf7253f4
RS
9098 event.xclient.data.l[1]);
9099 /* This is needed to detect the error
9100 if there is an error. */
9101 XSync (d, False);
9102 x_uncatch_errors (d, count);
9103 }
7a13e894 9104 /* Not certain about handling scroll bars here */
f1da8f06 9105#endif /* 0 */
c047688c 9106 }
7a13e894
RS
9107 else if (event.xclient.data.l[0]
9108 == dpyinfo->Xatom_wm_save_yourself)
9109 {
9110 /* Save state modify the WM_COMMAND property to
06a2c219 9111 something which can reinstate us. This notifies
7a13e894
RS
9112 the session manager, who's looking for such a
9113 PropertyNotify. Can restart processing when
06a2c219 9114 a keyboard or mouse event arrives. */
7a13e894
RS
9115 if (numchars > 0)
9116 {
19126e11
KH
9117 f = x_top_window_to_frame (dpyinfo,
9118 event.xclient.window);
7a13e894
RS
9119
9120 /* This is just so we only give real data once
9121 for a single Emacs process. */
b86bd3dd 9122 if (f == SELECTED_FRAME ())
7a13e894
RS
9123 XSetCommand (FRAME_X_DISPLAY (f),
9124 event.xclient.window,
9125 initial_argv, initial_argc);
f000f5c5 9126 else if (f)
7a13e894
RS
9127 XSetCommand (FRAME_X_DISPLAY (f),
9128 event.xclient.window,
9129 0, 0);
9130 }
9131 }
9132 else if (event.xclient.data.l[0]
9133 == dpyinfo->Xatom_wm_delete_window)
1fb20991 9134 {
19126e11
KH
9135 struct frame *f
9136 = x_any_window_to_frame (dpyinfo,
9137 event.xclient.window);
1fb20991 9138
7a13e894
RS
9139 if (f)
9140 {
9141 if (numchars == 0)
9142 abort ();
1fb20991 9143
7a13e894
RS
9144 bufp->kind = delete_window_event;
9145 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9146 bufp->arg = Qnil;
7a13e894
RS
9147 bufp++;
9148
9149 count += 1;
9150 numchars -= 1;
9151 }
1fb20991 9152 }
c047688c 9153 }
7a13e894
RS
9154 else if (event.xclient.message_type
9155 == dpyinfo->Xatom_wm_configure_denied)
9156 {
9157 }
9158 else if (event.xclient.message_type
9159 == dpyinfo->Xatom_wm_window_moved)
9160 {
9161 int new_x, new_y;
19126e11
KH
9162 struct frame *f
9163 = x_window_to_frame (dpyinfo, event.xclient.window);
58769bee 9164
7a13e894
RS
9165 new_x = event.xclient.data.s[0];
9166 new_y = event.xclient.data.s[1];
1fb20991 9167
7a13e894
RS
9168 if (f)
9169 {
7556890b
RS
9170 f->output_data.x->left_pos = new_x;
9171 f->output_data.x->top_pos = new_y;
7a13e894 9172 }
1fb20991 9173 }
0fdff6bb 9174#ifdef HACK_EDITRES
7a13e894
RS
9175 else if (event.xclient.message_type
9176 == dpyinfo->Xatom_editres)
9177 {
19126e11
KH
9178 struct frame *f
9179 = x_any_window_to_frame (dpyinfo, event.xclient.window);
7556890b 9180 _XEditResCheckMessages (f->output_data.x->widget, NULL,
19126e11 9181 &event, NULL);
7a13e894 9182 }
0fdff6bb 9183#endif /* HACK_EDITRES */
06a2c219
GM
9184 else if ((event.xclient.message_type
9185 == dpyinfo->Xatom_DONE)
9186 || (event.xclient.message_type
9187 == dpyinfo->Xatom_PAGE))
9188 {
9189 /* Ghostview job completed. Kill it. We could
9190 reply with "Next" if we received "Page", but we
9191 currently never do because we are interested in
9192 images, only, which should have 1 page. */
06a2c219
GM
9193 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9194 struct frame *f
9195 = x_window_to_frame (dpyinfo, event.xclient.window);
9196 x_kill_gs_process (pixmap, f);
9197 expose_frame (f, 0, 0, 0, 0);
9198 }
9199#ifdef USE_TOOLKIT_SCROLL_BARS
9200 /* Scroll bar callbacks send a ClientMessage from which
9201 we construct an input_event. */
9202 else if (event.xclient.message_type
9203 == dpyinfo->Xatom_Scrollbar)
9204 {
9205 x_scroll_bar_to_input_event (&event, bufp);
9206 ++bufp, ++count, --numchars;
9207 goto out;
9208 }
9209#endif /* USE_TOOLKIT_SCROLL_BARS */
9210 else
9211 goto OTHER;
7a13e894
RS
9212 }
9213 break;
dc6f92b8 9214
7a13e894 9215 case SelectionNotify:
3afe33e7 9216#ifdef USE_X_TOOLKIT
19126e11 9217 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
7a13e894 9218 goto OTHER;
3afe33e7 9219#endif /* not USE_X_TOOLKIT */
dfcf069d 9220 x_handle_selection_notify (&event.xselection);
7a13e894 9221 break;
d56a553a 9222
06a2c219 9223 case SelectionClear: /* Someone has grabbed ownership. */
3afe33e7 9224#ifdef USE_X_TOOLKIT
19126e11 9225 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
7a13e894 9226 goto OTHER;
3afe33e7 9227#endif /* USE_X_TOOLKIT */
7a13e894
RS
9228 {
9229 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
d56a553a 9230
7a13e894
RS
9231 if (numchars == 0)
9232 abort ();
d56a553a 9233
7a13e894
RS
9234 bufp->kind = selection_clear_event;
9235 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9236 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9237 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9238 bufp->frame_or_window = Qnil;
0f8aabe9 9239 bufp->arg = Qnil;
7a13e894 9240 bufp++;
d56a553a 9241
7a13e894
RS
9242 count += 1;
9243 numchars -= 1;
9244 }
9245 break;
dc6f92b8 9246
06a2c219 9247 case SelectionRequest: /* Someone wants our selection. */
3afe33e7 9248#ifdef USE_X_TOOLKIT
19126e11 9249 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
7a13e894 9250 goto OTHER;
3afe33e7 9251#endif /* USE_X_TOOLKIT */
7a13e894 9252 if (x_queue_selection_requests)
19126e11 9253 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
7a13e894
RS
9254 &event);
9255 else
9256 {
9257 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
dc6f92b8 9258
7a13e894
RS
9259 if (numchars == 0)
9260 abort ();
9261
9262 bufp->kind = selection_request_event;
9263 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9264 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9265 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9266 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9267 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9268 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9269 bufp->frame_or_window = Qnil;
0f8aabe9 9270 bufp->arg = Qnil;
7a13e894
RS
9271 bufp++;
9272
9273 count += 1;
9274 numchars -= 1;
9275 }
9276 break;
9277
9278 case PropertyNotify:
3afe33e7 9279#ifdef USE_X_TOOLKIT
19126e11 9280 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
7a13e894 9281 goto OTHER;
3afe33e7 9282#endif /* not USE_X_TOOLKIT */
dfcf069d 9283 x_handle_property_notify (&event.xproperty);
7a13e894 9284 break;
dc6f92b8 9285
7a13e894 9286 case ReparentNotify:
19126e11 9287 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
7a13e894
RS
9288 if (f)
9289 {
9290 int x, y;
7556890b 9291 f->output_data.x->parent_desc = event.xreparent.parent;
7a13e894 9292 x_real_positions (f, &x, &y);
7556890b
RS
9293 f->output_data.x->left_pos = x;
9294 f->output_data.x->top_pos = y;
7a13e894
RS
9295 }
9296 break;
3bd330d4 9297
7a13e894 9298 case Expose:
19126e11 9299 f = x_window_to_frame (dpyinfo, event.xexpose.window);
7a13e894 9300 if (f)
dc6f92b8 9301 {
7a13e894
RS
9302 if (f->async_visible == 0)
9303 {
9304 f->async_visible = 1;
9305 f->async_iconified = 0;
06c488fd 9306 f->output_data.x->has_been_visible = 1;
7a13e894
RS
9307 SET_FRAME_GARBAGED (f);
9308 }
9309 else
06a2c219
GM
9310 expose_frame (x_window_to_frame (dpyinfo,
9311 event.xexpose.window),
9312 event.xexpose.x, event.xexpose.y,
9313 event.xexpose.width, event.xexpose.height);
dc6f92b8
JB
9314 }
9315 else
7a13e894 9316 {
06a2c219
GM
9317#ifdef USE_TOOLKIT_SCROLL_BARS
9318 /* Dispatch event to the widget. */
9319 goto OTHER;
9320#else /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9321 struct scroll_bar *bar
9322 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 9323
7a13e894
RS
9324 if (bar)
9325 x_scroll_bar_expose (bar, &event);
3afe33e7 9326#ifdef USE_X_TOOLKIT
7a13e894
RS
9327 else
9328 goto OTHER;
3afe33e7 9329#endif /* USE_X_TOOLKIT */
06a2c219 9330#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9331 }
9332 break;
dc6f92b8 9333
7a13e894
RS
9334 case GraphicsExpose: /* This occurs when an XCopyArea's
9335 source area was obscured or not
9336 available.*/
19126e11 9337 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
7a13e894
RS
9338 if (f)
9339 {
06a2c219
GM
9340 expose_frame (f,
9341 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9342 event.xgraphicsexpose.width,
9343 event.xgraphicsexpose.height);
7a13e894 9344 }
3afe33e7 9345#ifdef USE_X_TOOLKIT
7a13e894
RS
9346 else
9347 goto OTHER;
3afe33e7 9348#endif /* USE_X_TOOLKIT */
7a13e894 9349 break;
dc6f92b8 9350
7a13e894 9351 case NoExpose: /* This occurs when an XCopyArea's
06a2c219
GM
9352 source area was completely
9353 available */
7a13e894 9354 break;
dc6f92b8 9355
7a13e894 9356 case UnmapNotify:
06a2c219
GM
9357 /* Redo the mouse-highlight after the tooltip has gone. */
9358 if (event.xmap.window == tip_window)
9359 {
9360 tip_window = 0;
9361 redo_mouse_highlight ();
9362 }
9363
91ea2a7a 9364 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
7a13e894
RS
9365 if (f) /* F may no longer exist if
9366 the frame was deleted. */
9367 {
9368 /* While a frame is unmapped, display generation is
9369 disabled; you don't want to spend time updating a
9370 display that won't ever be seen. */
9371 f->async_visible = 0;
9372 /* We can't distinguish, from the event, whether the window
9373 has become iconified or invisible. So assume, if it
9374 was previously visible, than now it is iconified.
1aa6072f
RS
9375 But x_make_frame_invisible clears both
9376 the visible flag and the iconified flag;
9377 and that way, we know the window is not iconified now. */
7a13e894 9378 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
1aa6072f
RS
9379 {
9380 f->async_iconified = 1;
bddd097c 9381
1aa6072f
RS
9382 bufp->kind = iconify_event;
9383 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9384 bufp->arg = Qnil;
1aa6072f
RS
9385 bufp++;
9386 count++;
9387 numchars--;
9388 }
7a13e894 9389 }
7a13e894 9390 goto OTHER;
dc6f92b8 9391
7a13e894 9392 case MapNotify:
06a2c219
GM
9393 if (event.xmap.window == tip_window)
9394 /* The tooltip has been drawn already. Avoid
9395 the SET_FRAME_GARBAGED below. */
9396 goto OTHER;
9397
9398 /* We use x_top_window_to_frame because map events can
9399 come for sub-windows and they don't mean that the
9400 frame is visible. */
19126e11 9401 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
7a13e894
RS
9402 if (f)
9403 {
9404 f->async_visible = 1;
9405 f->async_iconified = 0;
06c488fd 9406 f->output_data.x->has_been_visible = 1;
dc6f92b8 9407
7a13e894
RS
9408 /* wait_reading_process_input will notice this and update
9409 the frame's display structures. */
9410 SET_FRAME_GARBAGED (f);
bddd097c 9411
d806e720
RS
9412 if (f->iconified)
9413 {
9414 bufp->kind = deiconify_event;
9415 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9416 bufp->arg = Qnil;
d806e720
RS
9417 bufp++;
9418 count++;
9419 numchars--;
9420 }
e73ec6fa 9421 else if (! NILP (Vframe_list)
8e713be6 9422 && ! NILP (XCDR (Vframe_list)))
78aa2ba5
KH
9423 /* Force a redisplay sooner or later
9424 to update the frame titles
9425 in case this is the second frame. */
9426 record_asynch_buffer_change ();
7a13e894 9427 }
7a13e894 9428 goto OTHER;
dc6f92b8 9429
7a13e894 9430 case KeyPress:
19126e11 9431 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
f451eb13 9432
06a2c219
GM
9433#ifdef USE_MOTIF
9434 /* I couldn't find a way to prevent LessTif scroll bars
9435 from consuming key events. */
9436 if (f == 0)
9437 {
9438 Widget widget = XtWindowToWidget (dpyinfo->display,
9439 event.xkey.window);
9440 if (widget && XmIsScrollBar (widget))
9441 {
9442 widget = XtParent (widget);
9443 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9444 }
9445 }
9446#endif /* USE_MOTIF */
9447
7a13e894
RS
9448 if (f != 0)
9449 {
9450 KeySym keysym, orig_keysym;
9451 /* al%imercury@uunet.uu.net says that making this 81 instead of
9452 80 fixed a bug whereby meta chars made his Emacs hang. */
9453 unsigned char copy_buffer[81];
9454 int modifiers;
64bb1782 9455
7a13e894
RS
9456 event.xkey.state
9457 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9458 extra_keyboard_modifiers);
9459 modifiers = event.xkey.state;
3a2712f9 9460
7a13e894 9461 /* This will have to go some day... */
752a043f 9462
7a13e894
RS
9463 /* make_lispy_event turns chars into control chars.
9464 Don't do it here because XLookupString is too eager. */
9465 event.xkey.state &= ~ControlMask;
5d46f928
RS
9466 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9467 | dpyinfo->super_mod_mask
9468 | dpyinfo->hyper_mod_mask
9469 | dpyinfo->alt_mod_mask);
9470
1cf4a0d1
RS
9471 /* In case Meta is ComposeCharacter,
9472 clear its status. According to Markus Ehrnsperger
9473 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9474 this enables ComposeCharacter to work whether or
9475 not it is combined with Meta. */
9476 if (modifiers & dpyinfo->meta_mod_mask)
9477 bzero (&compose_status, sizeof (compose_status));
9478
6c183ba5
RS
9479#ifdef HAVE_X_I18N
9480 if (FRAME_XIC (f))
9481 {
f5d11644
GM
9482 unsigned char *copy_bufptr = copy_buffer;
9483 int copy_bufsiz = sizeof (copy_buffer);
9484 Status status_return;
9485
6c183ba5 9486 nbytes = XmbLookupString (FRAME_XIC (f),
f5d11644
GM
9487 &event.xkey, copy_bufptr,
9488 copy_bufsiz, &keysym,
6c183ba5 9489 &status_return);
f5d11644
GM
9490 if (status_return == XBufferOverflow)
9491 {
9492 copy_bufsiz = nbytes + 1;
9493 copy_bufptr = (char *) alloca (copy_bufsiz);
9494 nbytes = XmbLookupString (FRAME_XIC (f),
9495 &event.xkey, copy_bufptr,
9496 copy_bufsiz, &keysym,
9497 &status_return);
9498 }
9499
1decb680
PE
9500 if (status_return == XLookupNone)
9501 break;
9502 else if (status_return == XLookupChars)
fdd9d55e
GM
9503 {
9504 keysym = NoSymbol;
9505 modifiers = 0;
9506 }
1decb680
PE
9507 else if (status_return != XLookupKeySym
9508 && status_return != XLookupBoth)
9509 abort ();
6c183ba5
RS
9510 }
9511 else
9512 nbytes = XLookupString (&event.xkey, copy_buffer,
9513 80, &keysym, &compose_status);
9514#else
0299d313
RS
9515 nbytes = XLookupString (&event.xkey, copy_buffer,
9516 80, &keysym, &compose_status);
6c183ba5 9517#endif
dc6f92b8 9518
7a13e894 9519 orig_keysym = keysym;
55123275 9520
7a13e894
RS
9521 if (numchars > 1)
9522 {
9523 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9524 || keysym == XK_Delete
1097aea0 9525#ifdef XK_ISO_Left_Tab
441affdb 9526 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
1097aea0 9527#endif
852bff8f 9528 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
7a13e894
RS
9529 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9530 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
c34790e0 9531#ifdef HPUX
7a13e894
RS
9532 /* This recognizes the "extended function keys".
9533 It seems there's no cleaner way.
9534 Test IsModifierKey to avoid handling mode_switch
9535 incorrectly. */
9536 || ((unsigned) (keysym) >= XK_Select
9537 && (unsigned)(keysym) < XK_KP_Space)
69388238
RS
9538#endif
9539#ifdef XK_dead_circumflex
7a13e894 9540 || orig_keysym == XK_dead_circumflex
69388238
RS
9541#endif
9542#ifdef XK_dead_grave
7a13e894 9543 || orig_keysym == XK_dead_grave
69388238
RS
9544#endif
9545#ifdef XK_dead_tilde
7a13e894 9546 || orig_keysym == XK_dead_tilde
69388238
RS
9547#endif
9548#ifdef XK_dead_diaeresis
7a13e894 9549 || orig_keysym == XK_dead_diaeresis
69388238
RS
9550#endif
9551#ifdef XK_dead_macron
7a13e894 9552 || orig_keysym == XK_dead_macron
69388238
RS
9553#endif
9554#ifdef XK_dead_degree
7a13e894 9555 || orig_keysym == XK_dead_degree
69388238
RS
9556#endif
9557#ifdef XK_dead_acute
7a13e894 9558 || orig_keysym == XK_dead_acute
69388238
RS
9559#endif
9560#ifdef XK_dead_cedilla
7a13e894 9561 || orig_keysym == XK_dead_cedilla
69388238
RS
9562#endif
9563#ifdef XK_dead_breve
7a13e894 9564 || orig_keysym == XK_dead_breve
69388238
RS
9565#endif
9566#ifdef XK_dead_ogonek
7a13e894 9567 || orig_keysym == XK_dead_ogonek
69388238
RS
9568#endif
9569#ifdef XK_dead_caron
7a13e894 9570 || orig_keysym == XK_dead_caron
69388238
RS
9571#endif
9572#ifdef XK_dead_doubleacute
7a13e894 9573 || orig_keysym == XK_dead_doubleacute
69388238
RS
9574#endif
9575#ifdef XK_dead_abovedot
7a13e894 9576 || orig_keysym == XK_dead_abovedot
c34790e0 9577#endif
7a13e894
RS
9578 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9579 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9580 /* Any "vendor-specific" key is ok. */
9581 || (orig_keysym & (1 << 28)))
9582 && ! (IsModifierKey (orig_keysym)
7719aa06
RS
9583#ifndef HAVE_X11R5
9584#ifdef XK_Mode_switch
7a13e894 9585 || ((unsigned)(orig_keysym) == XK_Mode_switch)
7719aa06
RS
9586#endif
9587#ifdef XK_Num_Lock
7a13e894 9588 || ((unsigned)(orig_keysym) == XK_Num_Lock)
7719aa06
RS
9589#endif
9590#endif /* not HAVE_X11R5 */
7a13e894 9591 ))
dc6f92b8 9592 {
10e6549c
RS
9593 if (temp_index == sizeof temp_buffer / sizeof (short))
9594 temp_index = 0;
7a13e894
RS
9595 temp_buffer[temp_index++] = keysym;
9596 bufp->kind = non_ascii_keystroke;
9597 bufp->code = keysym;
e0c1aef2 9598 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9599 bufp->arg = Qnil;
334208b7
RS
9600 bufp->modifiers
9601 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9602 modifiers);
1113d9db 9603 bufp->timestamp = event.xkey.time;
dc6f92b8 9604 bufp++;
7a13e894
RS
9605 count++;
9606 numchars--;
dc6f92b8 9607 }
7a13e894
RS
9608 else if (numchars > nbytes)
9609 {
9610 register int i;
9611
9612 for (i = 0; i < nbytes; i++)
9613 {
9614 if (temp_index == sizeof temp_buffer / sizeof (short))
9615 temp_index = 0;
9616 temp_buffer[temp_index++] = copy_buffer[i];
9617 bufp->kind = ascii_keystroke;
9618 bufp->code = copy_buffer[i];
9619 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9620 bufp->arg = Qnil;
7a13e894
RS
9621 bufp->modifiers
9622 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9623 modifiers);
9624 bufp->timestamp = event.xkey.time;
9625 bufp++;
9626 }
9627
9628 count += nbytes;
9629 numchars -= nbytes;
1decb680
PE
9630
9631 if (keysym == NoSymbol)
9632 break;
7a13e894
RS
9633 }
9634 else
9635 abort ();
dc6f92b8 9636 }
10e6549c
RS
9637 else
9638 abort ();
dc6f92b8 9639 }
59ddecde
GM
9640#ifdef HAVE_X_I18N
9641 /* Don't dispatch this event since XtDispatchEvent calls
9642 XFilterEvent, and two calls in a row may freeze the
9643 client. */
9644 break;
9645#else
717ca130 9646 goto OTHER;
59ddecde 9647#endif
f451eb13 9648
f5d11644 9649 case KeyRelease:
59ddecde
GM
9650#ifdef HAVE_X_I18N
9651 /* Don't dispatch this event since XtDispatchEvent calls
9652 XFilterEvent, and two calls in a row may freeze the
9653 client. */
9654 break;
9655#else
f5d11644 9656 goto OTHER;
59ddecde 9657#endif
f5d11644 9658
7a13e894 9659 /* Here's a possible interpretation of the whole
06a2c219
GM
9660 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
9661 you get a FocusIn event, you have to get a FocusOut
9662 event before you relinquish the focus. If you
9663 haven't received a FocusIn event, then a mere
9664 LeaveNotify is enough to free you. */
f451eb13 9665
7a13e894 9666 case EnterNotify:
06a2c219
GM
9667 {
9668 int from_menu_bar_p = 0;
9669
9670 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
9671
9672#ifdef LESSTIF_VERSION
9673 /* When clicking outside of a menu bar popup to close
9674 it, we get a FocusIn/ EnterNotify sequence of
9675 events. The flag event.xcrossing.focus is not set
9676 in the EnterNotify event of that sequence because
9677 the focus is in the menu bar,
9678 event.xcrossing.window is the frame's X window.
9679 Unconditionally setting the focus frame to null in
9680 this case is not the right thing, because no event
9681 follows that could set the focus frame to the right
9682 value.
9683
9684 This could be a LessTif bug, but I wasn't able to
9685 reproduce the behavior in a simple test program.
9686
9687 (gerd, LessTif 0.88.1). */
9688
9689 if (!event.xcrossing.focus
9690 && f
9691 && f->output_data.x->menubar_widget)
9692 {
9693 Window focus;
9694 int revert;
9695
9696 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9697 if (focus == XtWindow (f->output_data.x->menubar_widget))
9698 from_menu_bar_p = 1;
9699 }
9700#endif /* LESSTIF_VERSION */
6d4238f3 9701
06a2c219
GM
9702 if (event.xcrossing.focus || from_menu_bar_p)
9703 {
9704 /* Avoid nasty pop/raise loops. */
9705 if (f && (!(f->auto_raise)
9706 || !(f->auto_lower)
9707 || (event.xcrossing.time - enter_timestamp) > 500))
9708 {
9709 x_new_focus_frame (dpyinfo, f);
9710 enter_timestamp = event.xcrossing.time;
9711 }
9712 }
9713 else if (f == dpyinfo->x_focus_frame)
9714 x_new_focus_frame (dpyinfo, 0);
9715
9716 /* EnterNotify counts as mouse movement,
9717 so update things that depend on mouse position. */
9718 if (f && !f->output_data.x->busy_p)
9719 note_mouse_movement (f, &event.xmotion);
9720 goto OTHER;
9721 }
dc6f92b8 9722
7a13e894 9723 case FocusIn:
19126e11 9724 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 9725 if (event.xfocus.detail != NotifyPointer)
0f941935 9726 dpyinfo->x_focus_event_frame = f;
7a13e894 9727 if (f)
eb72635f
GM
9728 {
9729 x_new_focus_frame (dpyinfo, f);
9730
9731 /* Don't stop displaying the initial startup message
9732 for a switch-frame event we don't need. */
9733 if (GC_NILP (Vterminal_frame)
9734 && GC_CONSP (Vframe_list)
9735 && !GC_NILP (XCDR (Vframe_list)))
9736 {
9737 bufp->kind = FOCUS_IN_EVENT;
9738 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9739 bufp->arg = Qnil;
eb72635f
GM
9740 ++bufp, ++count, --numchars;
9741 }
9742 }
f9e24cb9 9743
6c183ba5
RS
9744#ifdef HAVE_X_I18N
9745 if (f && FRAME_XIC (f))
9746 XSetICFocus (FRAME_XIC (f));
9747#endif
9748
7a13e894 9749 goto OTHER;
10c5e63d 9750
7a13e894 9751 case LeaveNotify:
19126e11 9752 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
7a13e894 9753 if (f)
10c5e63d 9754 {
06a2c219
GM
9755 Lisp_Object frame;
9756 int from_menu_bar_p = 0;
9757
7a13e894 9758 if (f == dpyinfo->mouse_face_mouse_frame)
06a2c219
GM
9759 {
9760 /* If we move outside the frame, then we're
9761 certainly no longer on any text in the frame. */
9762 clear_mouse_face (dpyinfo);
9763 dpyinfo->mouse_face_mouse_frame = 0;
9764 }
9765
9766 /* Generate a nil HELP_EVENT to cancel a help-echo.
9767 Do it only if there's something to cancel.
9768 Otherwise, the startup message is cleared when
9769 the mouse leaves the frame. */
9770 if (any_help_event_p)
9771 {
9772 XSETFRAME (frame, f);
9773 bufp->kind = HELP_EVENT;
0f1a9b23
GM
9774 bufp->frame_or_window = frame;
9775 bufp->arg = Qnil;
06a2c219
GM
9776 ++bufp, ++count, --numchars;
9777 }
7a13e894 9778
06a2c219
GM
9779#ifdef LESSTIF_VERSION
9780 /* Please see the comment at the start of the
9781 EnterNotify case. */
9782 if (!event.xcrossing.focus
9783 && f->output_data.x->menubar_widget)
9784 {
9785 Window focus;
9786 int revert;
9787 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9788 if (focus == XtWindow (f->output_data.x->menubar_widget))
9789 from_menu_bar_p = 1;
9790 }
9791#endif /* LESSTIF_VERSION */
9792
9793 if (event.xcrossing.focus || from_menu_bar_p)
0f941935 9794 x_mouse_leave (dpyinfo);
10c5e63d 9795 else
7a13e894 9796 {
0f941935
KH
9797 if (f == dpyinfo->x_focus_event_frame)
9798 dpyinfo->x_focus_event_frame = 0;
9799 if (f == dpyinfo->x_focus_frame)
9800 x_new_focus_frame (dpyinfo, 0);
7a13e894 9801 }
10c5e63d 9802 }
7a13e894 9803 goto OTHER;
dc6f92b8 9804
7a13e894 9805 case FocusOut:
19126e11 9806 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 9807 if (event.xfocus.detail != NotifyPointer
0f941935
KH
9808 && f == dpyinfo->x_focus_event_frame)
9809 dpyinfo->x_focus_event_frame = 0;
9810 if (f && f == dpyinfo->x_focus_frame)
9811 x_new_focus_frame (dpyinfo, 0);
f9e24cb9 9812
6c183ba5
RS
9813#ifdef HAVE_X_I18N
9814 if (f && FRAME_XIC (f))
9815 XUnsetICFocus (FRAME_XIC (f));
9816#endif
9817
7a13e894 9818 goto OTHER;
dc6f92b8 9819
7a13e894 9820 case MotionNotify:
dc6f92b8 9821 {
06a2c219
GM
9822 previous_help_echo = help_echo;
9823 help_echo = Qnil;
9824
7a13e894
RS
9825 if (dpyinfo->grabbed && last_mouse_frame
9826 && FRAME_LIVE_P (last_mouse_frame))
9827 f = last_mouse_frame;
9828 else
19126e11 9829 f = x_window_to_frame (dpyinfo, event.xmotion.window);
06a2c219 9830
7a13e894
RS
9831 if (f)
9832 note_mouse_movement (f, &event.xmotion);
9833 else
9834 {
e88b3c50 9835#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
9836 struct scroll_bar *bar
9837 = x_window_to_scroll_bar (event.xmotion.window);
f451eb13 9838
7a13e894
RS
9839 if (bar)
9840 x_scroll_bar_note_movement (bar, &event);
e88b3c50 9841#endif /* USE_TOOLKIT_SCROLL_BARS */
b8009dd1 9842
06a2c219
GM
9843 /* If we move outside the frame, then we're
9844 certainly no longer on any text in the frame. */
7a13e894
RS
9845 clear_mouse_face (dpyinfo);
9846 }
06a2c219
GM
9847
9848 /* If the contents of the global variable help_echo
9849 has changed, generate a HELP_EVENT. */
b7e80413
SM
9850 if (!NILP (help_echo)
9851 || !NILP (previous_help_echo))
06a2c219
GM
9852 {
9853 Lisp_Object frame;
9854
9855 if (f)
9856 XSETFRAME (frame, f);
9857 else
9858 frame = Qnil;
9859
9860 any_help_event_p = 1;
9861 bufp->kind = HELP_EVENT;
0f1a9b23
GM
9862 bufp->frame_or_window = frame;
9863 bufp->arg = help_echo;
06a2c219
GM
9864 ++bufp, ++count, --numchars;
9865 }
9866
9867 goto OTHER;
dc6f92b8 9868 }
dc6f92b8 9869
7a13e894 9870 case ConfigureNotify:
9829ddba
RS
9871 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
9872 if (f)
af395ec1 9873 {
5c187dee 9874#ifndef USE_X_TOOLKIT
bf1b7b30
KH
9875 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
9876 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
5c187dee 9877
2d7fc7e8
RS
9878 /* In the toolkit version, change_frame_size
9879 is called by the code that handles resizing
9880 of the EmacsFrame widget. */
7a13e894 9881
7a13e894
RS
9882 /* Even if the number of character rows and columns has
9883 not changed, the font size may have changed, so we need
9884 to check the pixel dimensions as well. */
9885 if (columns != f->width
9886 || rows != f->height
7556890b
RS
9887 || event.xconfigure.width != f->output_data.x->pixel_width
9888 || event.xconfigure.height != f->output_data.x->pixel_height)
7a13e894 9889 {
7d1e984f 9890 change_frame_size (f, rows, columns, 0, 1, 0);
7a13e894 9891 SET_FRAME_GARBAGED (f);
e687d06e 9892 cancel_mouse_face (f);
7a13e894 9893 }
2d7fc7e8 9894#endif
af395ec1 9895
7556890b
RS
9896 f->output_data.x->pixel_width = event.xconfigure.width;
9897 f->output_data.x->pixel_height = event.xconfigure.height;
7a13e894
RS
9898
9899 /* What we have now is the position of Emacs's own window.
9900 Convert that to the position of the window manager window. */
dcb07ae9
RS
9901 x_real_positions (f, &f->output_data.x->left_pos,
9902 &f->output_data.x->top_pos);
9903
f5d11644
GM
9904#ifdef HAVE_X_I18N
9905 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
9906 xic_set_statusarea (f);
9907#endif
9908
dcb07ae9
RS
9909 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
9910 {
9911 /* Since the WM decorations come below top_pos now,
9912 we must put them below top_pos in the future. */
9913 f->output_data.x->win_gravity = NorthWestGravity;
9914 x_wm_set_size_hint (f, (long) 0, 0);
9915 }
8f08dc93
KH
9916#ifdef USE_MOTIF
9917 /* Some window managers pass (0,0) as the location of
9918 the window, and the Motif event handler stores it
9919 in the emacs widget, which messes up Motif menus. */
9920 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
9921 {
9922 event.xconfigure.x = f->output_data.x->widget->core.x;
9923 event.xconfigure.y = f->output_data.x->widget->core.y;
9924 }
06a2c219 9925#endif /* USE_MOTIF */
7a13e894 9926 }
2d7fc7e8 9927 goto OTHER;
dc6f92b8 9928
7a13e894
RS
9929 case ButtonPress:
9930 case ButtonRelease:
9931 {
9932 /* If we decide we want to generate an event to be seen
9933 by the rest of Emacs, we put it here. */
9934 struct input_event emacs_event;
9ea173e8 9935 int tool_bar_p = 0;
06a2c219 9936
7a13e894 9937 emacs_event.kind = no_event;
7a13e894 9938 bzero (&compose_status, sizeof (compose_status));
9b07615b 9939
06a2c219
GM
9940 if (dpyinfo->grabbed
9941 && last_mouse_frame
9f67f20b
RS
9942 && FRAME_LIVE_P (last_mouse_frame))
9943 f = last_mouse_frame;
9944 else
2224b905 9945 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9f67f20b 9946
06a2c219
GM
9947 if (f)
9948 {
9ea173e8
GM
9949 /* Is this in the tool-bar? */
9950 if (WINDOWP (f->tool_bar_window)
9951 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
06a2c219
GM
9952 {
9953 Lisp_Object window;
9954 int p, x, y;
9955
9956 x = event.xbutton.x;
9957 y = event.xbutton.y;
9958
9959 /* Set x and y. */
9960 window = window_from_coordinates (f, x, y, &p, 1);
9ea173e8 9961 if (EQ (window, f->tool_bar_window))
06a2c219 9962 {
9ea173e8
GM
9963 x_handle_tool_bar_click (f, &event.xbutton);
9964 tool_bar_p = 1;
06a2c219
GM
9965 }
9966 }
9967
9ea173e8 9968 if (!tool_bar_p)
06a2c219
GM
9969 if (!dpyinfo->x_focus_frame
9970 || f == dpyinfo->x_focus_frame)
9971 construct_mouse_click (&emacs_event, &event, f);
7a13e894
RS
9972 }
9973 else
9974 {
06a2c219 9975#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
9976 struct scroll_bar *bar
9977 = x_window_to_scroll_bar (event.xbutton.window);
f451eb13 9978
7a13e894
RS
9979 if (bar)
9980 x_scroll_bar_handle_click (bar, &event, &emacs_event);
06a2c219 9981#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9982 }
9983
9984 if (event.type == ButtonPress)
9985 {
9986 dpyinfo->grabbed |= (1 << event.xbutton.button);
9987 last_mouse_frame = f;
edad46f6
KH
9988 /* Ignore any mouse motion that happened
9989 before this event; any subsequent mouse-movement
9990 Emacs events should reflect only motion after
9991 the ButtonPress. */
a00e91cd
KH
9992 if (f != 0)
9993 f->mouse_moved = 0;
06a2c219 9994
9ea173e8
GM
9995 if (!tool_bar_p)
9996 last_tool_bar_item = -1;
7a13e894 9997 }
3afe33e7
RS
9998 else
9999 {
7a13e894 10000 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
3afe33e7 10001 }
23faf38f 10002
7a13e894
RS
10003 if (numchars >= 1 && emacs_event.kind != no_event)
10004 {
10005 bcopy (&emacs_event, bufp, sizeof (struct input_event));
10006 bufp++;
10007 count++;
10008 numchars--;
10009 }
3afe33e7
RS
10010
10011#ifdef USE_X_TOOLKIT
2224b905
RS
10012 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
10013 /* For a down-event in the menu bar,
10014 don't pass it to Xt right now.
10015 Instead, save it away
10016 and we will pass it to Xt from kbd_buffer_get_event.
10017 That way, we can run some Lisp code first. */
91375f8f
RS
10018 if (f && event.type == ButtonPress
10019 /* Verify the event is really within the menu bar
10020 and not just sent to it due to grabbing. */
10021 && event.xbutton.x >= 0
10022 && event.xbutton.x < f->output_data.x->pixel_width
10023 && event.xbutton.y >= 0
10024 && event.xbutton.y < f->output_data.x->menubar_height
10025 && event.xbutton.same_screen)
2224b905 10026 {
8805890a 10027 SET_SAVED_BUTTON_EVENT;
2237cac9
RS
10028 XSETFRAME (last_mouse_press_frame, f);
10029 }
10030 else if (event.type == ButtonPress)
10031 {
10032 last_mouse_press_frame = Qnil;
30e671c3 10033 goto OTHER;
ce89ef46 10034 }
06a2c219 10035
2237cac9
RS
10036#ifdef USE_MOTIF /* This should do not harm for Lucid,
10037 but I am trying to be cautious. */
ce89ef46
RS
10038 else if (event.type == ButtonRelease)
10039 {
2237cac9 10040 if (!NILP (last_mouse_press_frame))
f10ded1c 10041 {
2237cac9
RS
10042 f = XFRAME (last_mouse_press_frame);
10043 if (f->output_data.x)
06a2c219 10044 SET_SAVED_BUTTON_EVENT;
f10ded1c 10045 }
06a2c219 10046 else
30e671c3 10047 goto OTHER;
2224b905 10048 }
2237cac9 10049#endif /* USE_MOTIF */
2224b905
RS
10050 else
10051 goto OTHER;
3afe33e7 10052#endif /* USE_X_TOOLKIT */
7a13e894
RS
10053 }
10054 break;
dc6f92b8 10055
7a13e894 10056 case CirculateNotify:
06a2c219
GM
10057 goto OTHER;
10058
7a13e894 10059 case CirculateRequest:
06a2c219
GM
10060 goto OTHER;
10061
10062 case VisibilityNotify:
10063 goto OTHER;
dc6f92b8 10064
7a13e894
RS
10065 case MappingNotify:
10066 /* Someone has changed the keyboard mapping - update the
10067 local cache. */
10068 switch (event.xmapping.request)
10069 {
10070 case MappingModifier:
10071 x_find_modifier_meanings (dpyinfo);
10072 /* This is meant to fall through. */
10073 case MappingKeyboard:
10074 XRefreshKeyboardMapping (&event.xmapping);
10075 }
7a13e894 10076 goto OTHER;
dc6f92b8 10077
7a13e894 10078 default:
7a13e894 10079 OTHER:
717ca130 10080#ifdef USE_X_TOOLKIT
7a13e894
RS
10081 BLOCK_INPUT;
10082 XtDispatchEvent (&event);
10083 UNBLOCK_INPUT;
3afe33e7 10084#endif /* USE_X_TOOLKIT */
7a13e894
RS
10085 break;
10086 }
dc6f92b8
JB
10087 }
10088 }
10089
06a2c219
GM
10090 out:;
10091
9a5196d0
RS
10092 /* On some systems, an X bug causes Emacs to get no more events
10093 when the window is destroyed. Detect that. (1994.) */
58769bee 10094 if (! event_found)
ef2a22d0 10095 {
ef2a22d0
RS
10096 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
10097 One XNOOP in 100 loops will make Emacs terminate.
10098 B. Bretthauer, 1994 */
10099 x_noop_count++;
58769bee 10100 if (x_noop_count >= 100)
ef2a22d0
RS
10101 {
10102 x_noop_count=0;
2224b905
RS
10103
10104 if (next_noop_dpyinfo == 0)
10105 next_noop_dpyinfo = x_display_list;
10106
10107 XNoOp (next_noop_dpyinfo->display);
10108
10109 /* Each time we get here, cycle through the displays now open. */
10110 next_noop_dpyinfo = next_noop_dpyinfo->next;
ef2a22d0
RS
10111 }
10112 }
502add23 10113
06a2c219 10114 /* If the focus was just given to an auto-raising frame,
0134a210 10115 raise it now. */
7a13e894 10116 /* ??? This ought to be able to handle more than one such frame. */
0134a210
RS
10117 if (pending_autoraise_frame)
10118 {
10119 x_raise_frame (pending_autoraise_frame);
10120 pending_autoraise_frame = 0;
10121 }
0134a210 10122
dc6f92b8 10123 UNBLOCK_INPUT;
bde5503b 10124 --handling_signal;
dc6f92b8
JB
10125 return count;
10126}
06a2c219
GM
10127
10128
10129
dc6f92b8 10130\f
06a2c219
GM
10131/***********************************************************************
10132 Text Cursor
10133 ***********************************************************************/
10134
10135/* Note if the text cursor of window W has been overwritten by a
10136 drawing operation that outputs N glyphs starting at HPOS in the
10137 line given by output_cursor.vpos. N < 0 means all the rest of the
10138 line after HPOS has been written. */
10139
10140static void
10141note_overwritten_text_cursor (w, hpos, n)
10142 struct window *w;
10143 int hpos, n;
10144{
10145 if (updated_area == TEXT_AREA
10146 && output_cursor.vpos == w->phys_cursor.vpos
10147 && hpos <= w->phys_cursor.hpos
10148 && (n < 0
10149 || hpos + n > w->phys_cursor.hpos))
10150 w->phys_cursor_on_p = 0;
10151}
f451eb13
JB
10152
10153
06a2c219
GM
10154/* Set clipping for output in glyph row ROW. W is the window in which
10155 we operate. GC is the graphics context to set clipping in.
10156 WHOLE_LINE_P non-zero means include the areas used for truncation
10157 mark display and alike in the clipping rectangle.
10158
10159 ROW may be a text row or, e.g., a mode line. Text rows must be
10160 clipped to the interior of the window dedicated to text display,
10161 mode lines must be clipped to the whole window. */
dc6f92b8
JB
10162
10163static void
06a2c219
GM
10164x_clip_to_row (w, row, gc, whole_line_p)
10165 struct window *w;
10166 struct glyph_row *row;
10167 GC gc;
10168 int whole_line_p;
dc6f92b8 10169{
06a2c219
GM
10170 struct frame *f = XFRAME (WINDOW_FRAME (w));
10171 XRectangle clip_rect;
10172 int window_x, window_y, window_width, window_height;
dc6f92b8 10173
06a2c219 10174 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5c1aae96 10175
06a2c219
GM
10176 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10177 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10178 clip_rect.y = max (clip_rect.y, window_y);
10179 clip_rect.width = window_width;
10180 clip_rect.height = row->visible_height;
5c1aae96 10181
06a2c219
GM
10182 /* If clipping to the whole line, including trunc marks, extend
10183 the rectangle to the left and increase its width. */
10184 if (whole_line_p)
10185 {
110859fc
GM
10186 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10187 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
06a2c219 10188 }
5c1aae96 10189
06a2c219 10190 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
dc6f92b8
JB
10191}
10192
06a2c219
GM
10193
10194/* Draw a hollow box cursor on window W in glyph row ROW. */
dc6f92b8
JB
10195
10196static void
06a2c219
GM
10197x_draw_hollow_cursor (w, row)
10198 struct window *w;
10199 struct glyph_row *row;
dc6f92b8 10200{
06a2c219
GM
10201 struct frame *f = XFRAME (WINDOW_FRAME (w));
10202 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10203 Display *dpy = FRAME_X_DISPLAY (f);
10204 int x, y, wd, h;
10205 XGCValues xgcv;
10206 struct glyph *cursor_glyph;
10207 GC gc;
10208
10209 /* Compute frame-relative coordinates from window-relative
10210 coordinates. */
10211 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10212 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10213 + row->ascent - w->phys_cursor_ascent);
10214 h = row->height - 1;
10215
10216 /* Get the glyph the cursor is on. If we can't tell because
10217 the current matrix is invalid or such, give up. */
10218 cursor_glyph = get_phys_cursor_glyph (w);
10219 if (cursor_glyph == NULL)
dc6f92b8
JB
10220 return;
10221
06a2c219
GM
10222 /* Compute the width of the rectangle to draw. If on a stretch
10223 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10224 rectangle as wide as the glyph, but use a canonical character
10225 width instead. */
10226 wd = cursor_glyph->pixel_width - 1;
10227 if (cursor_glyph->type == STRETCH_GLYPH
10228 && !x_stretch_cursor_p)
10229 wd = min (CANON_X_UNIT (f), wd);
10230
10231 /* The foreground of cursor_gc is typically the same as the normal
10232 background color, which can cause the cursor box to be invisible. */
10233 xgcv.foreground = f->output_data.x->cursor_pixel;
10234 if (dpyinfo->scratch_cursor_gc)
10235 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10236 else
10237 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10238 GCForeground, &xgcv);
10239 gc = dpyinfo->scratch_cursor_gc;
10240
10241 /* Set clipping, draw the rectangle, and reset clipping again. */
10242 x_clip_to_row (w, row, gc, 0);
10243 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10244 XSetClipMask (dpy, gc, None);
dc6f92b8
JB
10245}
10246
06a2c219
GM
10247
10248/* Draw a bar cursor on window W in glyph row ROW.
10249
10250 Implementation note: One would like to draw a bar cursor with an
10251 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10252 Unfortunately, I didn't find a font yet that has this property set.
10253 --gerd. */
dc6f92b8
JB
10254
10255static void
f02d8aa0 10256x_draw_bar_cursor (w, row, width)
06a2c219
GM
10257 struct window *w;
10258 struct glyph_row *row;
f02d8aa0 10259 int width;
dc6f92b8 10260{
06a2c219
GM
10261 /* If cursor hpos is out of bounds, don't draw garbage. This can
10262 happen in mini-buffer windows when switching between echo area
10263 glyphs and mini-buffer. */
10264 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
10265 {
10266 struct frame *f = XFRAME (w->frame);
10267 struct glyph *cursor_glyph;
10268 GC gc;
10269 int x;
10270 unsigned long mask;
10271 XGCValues xgcv;
10272 Display *dpy;
10273 Window window;
10274
10275 cursor_glyph = get_phys_cursor_glyph (w);
10276 if (cursor_glyph == NULL)
10277 return;
10278
10279 xgcv.background = f->output_data.x->cursor_pixel;
10280 xgcv.foreground = f->output_data.x->cursor_pixel;
10281 xgcv.graphics_exposures = 0;
10282 mask = GCForeground | GCBackground | GCGraphicsExposures;
10283 dpy = FRAME_X_DISPLAY (f);
10284 window = FRAME_X_WINDOW (f);
10285 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
10286
10287 if (gc)
10288 XChangeGC (dpy, gc, mask, &xgcv);
10289 else
10290 {
10291 gc = XCreateGC (dpy, window, mask, &xgcv);
10292 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10293 }
10294
f02d8aa0
GM
10295 if (width < 0)
10296 width = f->output_data.x->cursor_width;
10297
06a2c219
GM
10298 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10299 x_clip_to_row (w, row, gc, 0);
10300 XFillRectangle (dpy, window, gc,
10301 x,
10302 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
f02d8aa0 10303 min (cursor_glyph->pixel_width, width),
06a2c219
GM
10304 row->height);
10305 XSetClipMask (dpy, gc, None);
10306 }
dc6f92b8
JB
10307}
10308
06a2c219
GM
10309
10310/* Clear the cursor of window W to background color, and mark the
10311 cursor as not shown. This is used when the text where the cursor
10312 is is about to be rewritten. */
10313
dc6f92b8 10314static void
06a2c219
GM
10315x_clear_cursor (w)
10316 struct window *w;
dc6f92b8 10317{
06a2c219
GM
10318 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10319 x_update_window_cursor (w, 0);
10320}
90e65f07 10321
dbc4e1c1 10322
06a2c219
GM
10323/* Draw the cursor glyph of window W in glyph row ROW. See the
10324 comment of x_draw_glyphs for the meaning of HL. */
dbc4e1c1 10325
06a2c219
GM
10326static void
10327x_draw_phys_cursor_glyph (w, row, hl)
10328 struct window *w;
10329 struct glyph_row *row;
10330 enum draw_glyphs_face hl;
10331{
10332 /* If cursor hpos is out of bounds, don't draw garbage. This can
10333 happen in mini-buffer windows when switching between echo area
10334 glyphs and mini-buffer. */
10335 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
66ac4b0e
GM
10336 {
10337 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10338 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10339 hl, 0, 0, 0);
10340
10341 /* When we erase the cursor, and ROW is overlapped by other
10342 rows, make sure that these overlapping parts of other rows
10343 are redrawn. */
10344 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10345 {
10346 if (row > w->current_matrix->rows
10347 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10348 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10349
10350 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10351 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10352 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10353 }
10354 }
06a2c219 10355}
dbc4e1c1 10356
eea6af04 10357
06a2c219 10358/* Erase the image of a cursor of window W from the screen. */
eea6af04 10359
06a2c219
GM
10360static void
10361x_erase_phys_cursor (w)
10362 struct window *w;
10363{
10364 struct frame *f = XFRAME (w->frame);
10365 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10366 int hpos = w->phys_cursor.hpos;
10367 int vpos = w->phys_cursor.vpos;
10368 int mouse_face_here_p = 0;
10369 struct glyph_matrix *active_glyphs = w->current_matrix;
10370 struct glyph_row *cursor_row;
10371 struct glyph *cursor_glyph;
10372 enum draw_glyphs_face hl;
10373
10374 /* No cursor displayed or row invalidated => nothing to do on the
10375 screen. */
10376 if (w->phys_cursor_type == NO_CURSOR)
10377 goto mark_cursor_off;
10378
10379 /* VPOS >= active_glyphs->nrows means that window has been resized.
10380 Don't bother to erase the cursor. */
10381 if (vpos >= active_glyphs->nrows)
10382 goto mark_cursor_off;
10383
10384 /* If row containing cursor is marked invalid, there is nothing we
10385 can do. */
10386 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10387 if (!cursor_row->enabled_p)
10388 goto mark_cursor_off;
10389
10390 /* This can happen when the new row is shorter than the old one.
10391 In this case, either x_draw_glyphs or clear_end_of_line
10392 should have cleared the cursor. Note that we wouldn't be
10393 able to erase the cursor in this case because we don't have a
10394 cursor glyph at hand. */
10395 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10396 goto mark_cursor_off;
10397
10398 /* If the cursor is in the mouse face area, redisplay that when
10399 we clear the cursor. */
8801a864
KR
10400 if (! NILP (dpyinfo->mouse_face_window)
10401 && w == XWINDOW (dpyinfo->mouse_face_window)
06a2c219
GM
10402 && (vpos > dpyinfo->mouse_face_beg_row
10403 || (vpos == dpyinfo->mouse_face_beg_row
10404 && hpos >= dpyinfo->mouse_face_beg_col))
10405 && (vpos < dpyinfo->mouse_face_end_row
10406 || (vpos == dpyinfo->mouse_face_end_row
10407 && hpos < dpyinfo->mouse_face_end_col))
10408 /* Don't redraw the cursor's spot in mouse face if it is at the
10409 end of a line (on a newline). The cursor appears there, but
10410 mouse highlighting does not. */
10411 && cursor_row->used[TEXT_AREA] > hpos)
10412 mouse_face_here_p = 1;
10413
10414 /* Maybe clear the display under the cursor. */
10415 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10416 {
10417 int x;
045dee35 10418 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dbc4e1c1 10419
06a2c219
GM
10420 cursor_glyph = get_phys_cursor_glyph (w);
10421 if (cursor_glyph == NULL)
10422 goto mark_cursor_off;
dbc4e1c1 10423
06a2c219
GM
10424 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10425
10426 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10427 x,
045dee35 10428 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219
GM
10429 cursor_row->y)),
10430 cursor_glyph->pixel_width,
10431 cursor_row->visible_height,
10432 False);
dbc4e1c1 10433 }
06a2c219
GM
10434
10435 /* Erase the cursor by redrawing the character underneath it. */
10436 if (mouse_face_here_p)
10437 hl = DRAW_MOUSE_FACE;
10438 else if (cursor_row->inverse_p)
10439 hl = DRAW_INVERSE_VIDEO;
10440 else
10441 hl = DRAW_NORMAL_TEXT;
10442 x_draw_phys_cursor_glyph (w, cursor_row, hl);
dbc4e1c1 10443
06a2c219
GM
10444 mark_cursor_off:
10445 w->phys_cursor_on_p = 0;
10446 w->phys_cursor_type = NO_CURSOR;
dbc4e1c1
JB
10447}
10448
10449
06a2c219
GM
10450/* Display or clear cursor of window W. If ON is zero, clear the
10451 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10452 where to put the cursor is specified by HPOS, VPOS, X and Y. */
dbc4e1c1 10453
06a2c219
GM
10454void
10455x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10456 struct window *w;
10457 int on, hpos, vpos, x, y;
dbc4e1c1 10458{
06a2c219
GM
10459 struct frame *f = XFRAME (w->frame);
10460 int new_cursor_type;
f02d8aa0 10461 int new_cursor_width;
06a2c219
GM
10462 struct glyph_matrix *current_glyphs;
10463 struct glyph_row *glyph_row;
10464 struct glyph *glyph;
dbc4e1c1 10465
49d838ea 10466 /* This is pointless on invisible frames, and dangerous on garbaged
06a2c219
GM
10467 windows and frames; in the latter case, the frame or window may
10468 be in the midst of changing its size, and x and y may be off the
10469 window. */
10470 if (! FRAME_VISIBLE_P (f)
10471 || FRAME_GARBAGED_P (f)
10472 || vpos >= w->current_matrix->nrows
10473 || hpos >= w->current_matrix->matrix_w)
dc6f92b8
JB
10474 return;
10475
10476 /* If cursor is off and we want it off, return quickly. */
06a2c219 10477 if (!on && !w->phys_cursor_on_p)
dc6f92b8
JB
10478 return;
10479
06a2c219
GM
10480 current_glyphs = w->current_matrix;
10481 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10482 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10483
10484 /* If cursor row is not enabled, we don't really know where to
10485 display the cursor. */
10486 if (!glyph_row->enabled_p)
10487 {
10488 w->phys_cursor_on_p = 0;
10489 return;
10490 }
10491
10492 xassert (interrupt_input_blocked);
10493
10494 /* Set new_cursor_type to the cursor we want to be displayed. In a
10495 mini-buffer window, we want the cursor only to appear if we are
10496 reading input from this window. For the selected window, we want
10497 the cursor type given by the frame parameter. If explicitly
10498 marked off, draw no cursor. In all other cases, we want a hollow
10499 box cursor. */
f02d8aa0 10500 new_cursor_width = -1;
9b4a7047
GM
10501 if (cursor_in_echo_area
10502 && FRAME_HAS_MINIBUF_P (f)
10503 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
06a2c219 10504 {
9b4a7047
GM
10505 if (w == XWINDOW (echo_area_window))
10506 new_cursor_type = FRAME_DESIRED_CURSOR (f);
06a2c219
GM
10507 else
10508 new_cursor_type = HOLLOW_BOX_CURSOR;
10509 }
06a2c219 10510 else
9b4a7047
GM
10511 {
10512 if (w != XWINDOW (selected_window)
10513 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
10514 {
e55a0b79
GM
10515 extern int cursor_in_non_selected_windows;
10516
10517 if (MINI_WINDOW_P (w) || !cursor_in_non_selected_windows)
9b4a7047
GM
10518 new_cursor_type = NO_CURSOR;
10519 else
10520 new_cursor_type = HOLLOW_BOX_CURSOR;
10521 }
10522 else if (w->cursor_off_p)
10523 new_cursor_type = NO_CURSOR;
10524 else
f02d8aa0
GM
10525 {
10526 struct buffer *b = XBUFFER (w->buffer);
10527
10528 if (EQ (b->cursor_type, Qt))
10529 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10530 else
10531 new_cursor_type = x_specified_cursor_type (b->cursor_type,
10532 &new_cursor_width);
10533 }
9b4a7047 10534 }
06a2c219
GM
10535
10536 /* If cursor is currently being shown and we don't want it to be or
10537 it is in the wrong place, or the cursor type is not what we want,
dc6f92b8 10538 erase it. */
06a2c219 10539 if (w->phys_cursor_on_p
dc6f92b8 10540 && (!on
06a2c219
GM
10541 || w->phys_cursor.x != x
10542 || w->phys_cursor.y != y
10543 || new_cursor_type != w->phys_cursor_type))
10544 x_erase_phys_cursor (w);
10545
10546 /* If the cursor is now invisible and we want it to be visible,
10547 display it. */
10548 if (on && !w->phys_cursor_on_p)
10549 {
10550 w->phys_cursor_ascent = glyph_row->ascent;
10551 w->phys_cursor_height = glyph_row->height;
10552
10553 /* Set phys_cursor_.* before x_draw_.* is called because some
10554 of them may need the information. */
10555 w->phys_cursor.x = x;
10556 w->phys_cursor.y = glyph_row->y;
10557 w->phys_cursor.hpos = hpos;
10558 w->phys_cursor.vpos = vpos;
10559 w->phys_cursor_type = new_cursor_type;
10560 w->phys_cursor_on_p = 1;
10561
10562 switch (new_cursor_type)
dc6f92b8 10563 {
06a2c219
GM
10564 case HOLLOW_BOX_CURSOR:
10565 x_draw_hollow_cursor (w, glyph_row);
10566 break;
10567
10568 case FILLED_BOX_CURSOR:
10569 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10570 break;
10571
10572 case BAR_CURSOR:
f02d8aa0 10573 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
06a2c219
GM
10574 break;
10575
10576 case NO_CURSOR:
10577 break;
dc6f92b8 10578
06a2c219
GM
10579 default:
10580 abort ();
10581 }
59ddecde
GM
10582
10583#ifdef HAVE_X_I18N
10584 if (w == XWINDOW (f->selected_window))
10585 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
10586 xic_set_preeditarea (w, x, y);
10587#endif
dc6f92b8
JB
10588 }
10589
06a2c219 10590#ifndef XFlush
f676886a 10591 if (updating_frame != f)
334208b7 10592 XFlush (FRAME_X_DISPLAY (f));
06a2c219 10593#endif
dc6f92b8
JB
10594}
10595
06a2c219
GM
10596
10597/* Display the cursor on window W, or clear it. X and Y are window
10598 relative pixel coordinates. HPOS and VPOS are glyph matrix
10599 positions. If W is not the selected window, display a hollow
10600 cursor. ON non-zero means display the cursor at X, Y which
10601 correspond to HPOS, VPOS, otherwise it is cleared. */
5d46f928 10602
dfcf069d 10603void
06a2c219
GM
10604x_display_cursor (w, on, hpos, vpos, x, y)
10605 struct window *w;
10606 int on, hpos, vpos, x, y;
dc6f92b8 10607{
f94397b5 10608 BLOCK_INPUT;
06a2c219 10609 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
5d46f928
RS
10610 UNBLOCK_INPUT;
10611}
10612
06a2c219
GM
10613
10614/* Display the cursor on window W, or clear it, according to ON_P.
5d46f928
RS
10615 Don't change the cursor's position. */
10616
dfcf069d 10617void
06a2c219 10618x_update_cursor (f, on_p)
5d46f928 10619 struct frame *f;
5d46f928 10620{
06a2c219
GM
10621 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
10622}
10623
10624
10625/* Call x_update_window_cursor with parameter ON_P on all leaf windows
10626 in the window tree rooted at W. */
10627
10628static void
10629x_update_cursor_in_window_tree (w, on_p)
10630 struct window *w;
10631 int on_p;
10632{
10633 while (w)
10634 {
10635 if (!NILP (w->hchild))
10636 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10637 else if (!NILP (w->vchild))
10638 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10639 else
10640 x_update_window_cursor (w, on_p);
10641
10642 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10643 }
10644}
5d46f928 10645
f94397b5 10646
06a2c219
GM
10647/* Switch the display of W's cursor on or off, according to the value
10648 of ON. */
10649
10650static void
10651x_update_window_cursor (w, on)
10652 struct window *w;
10653 int on;
10654{
16b5d424
GM
10655 /* Don't update cursor in windows whose frame is in the process
10656 of being deleted. */
10657 if (w->current_matrix)
10658 {
10659 BLOCK_INPUT;
10660 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
10661 w->phys_cursor.x, w->phys_cursor.y);
10662 UNBLOCK_INPUT;
10663 }
dc6f92b8 10664}
06a2c219
GM
10665
10666
10667
dc6f92b8
JB
10668\f
10669/* Icons. */
10670
f676886a 10671/* Refresh bitmap kitchen sink icon for frame F
06a2c219 10672 when we get an expose event for it. */
dc6f92b8 10673
dfcf069d 10674void
f676886a
JB
10675refreshicon (f)
10676 struct frame *f;
dc6f92b8 10677{
06a2c219 10678 /* Normally, the window manager handles this function. */
dc6f92b8
JB
10679}
10680
dbc4e1c1 10681/* Make the x-window of frame F use the gnu icon bitmap. */
dc6f92b8
JB
10682
10683int
990ba854 10684x_bitmap_icon (f, file)
f676886a 10685 struct frame *f;
990ba854 10686 Lisp_Object file;
dc6f92b8 10687{
06a2c219 10688 int bitmap_id;
dc6f92b8 10689
c118dd06 10690 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
10691 return 1;
10692
990ba854 10693 /* Free up our existing icon bitmap if any. */
7556890b
RS
10694 if (f->output_data.x->icon_bitmap > 0)
10695 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10696 f->output_data.x->icon_bitmap = 0;
990ba854
RS
10697
10698 if (STRINGP (file))
7f2ae036
RS
10699 bitmap_id = x_create_bitmap_from_file (f, file);
10700 else
10701 {
990ba854 10702 /* Create the GNU bitmap if necessary. */
5bf01b68 10703 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
334208b7
RS
10704 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
10705 = x_create_bitmap_from_data (f, gnu_bits,
10706 gnu_width, gnu_height);
990ba854
RS
10707
10708 /* The first time we create the GNU bitmap,
06a2c219 10709 this increments the ref-count one extra time.
990ba854
RS
10710 As a result, the GNU bitmap is never freed.
10711 That way, we don't have to worry about allocating it again. */
334208b7 10712 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
990ba854 10713
334208b7 10714 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7f2ae036
RS
10715 }
10716
10717 x_wm_set_icon_pixmap (f, bitmap_id);
7556890b 10718 f->output_data.x->icon_bitmap = bitmap_id;
dc6f92b8
JB
10719
10720 return 0;
10721}
10722
10723
1be2d067
KH
10724/* Make the x-window of frame F use a rectangle with text.
10725 Use ICON_NAME as the text. */
dc6f92b8
JB
10726
10727int
f676886a
JB
10728x_text_icon (f, icon_name)
10729 struct frame *f;
dc6f92b8
JB
10730 char *icon_name;
10731{
c118dd06 10732 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
10733 return 1;
10734
1be2d067
KH
10735#ifdef HAVE_X11R4
10736 {
10737 XTextProperty text;
10738 text.value = (unsigned char *) icon_name;
10739 text.encoding = XA_STRING;
10740 text.format = 8;
10741 text.nitems = strlen (icon_name);
10742#ifdef USE_X_TOOLKIT
7556890b 10743 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
1be2d067
KH
10744 &text);
10745#else /* not USE_X_TOOLKIT */
10746 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
10747#endif /* not USE_X_TOOLKIT */
10748 }
10749#else /* not HAVE_X11R4 */
10750 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
10751#endif /* not HAVE_X11R4 */
58769bee 10752
7556890b
RS
10753 if (f->output_data.x->icon_bitmap > 0)
10754 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10755 f->output_data.x->icon_bitmap = 0;
b1c884c3 10756 x_wm_set_icon_pixmap (f, 0);
dc6f92b8
JB
10757
10758 return 0;
10759}
10760\f
e99db5a1
RS
10761#define X_ERROR_MESSAGE_SIZE 200
10762
10763/* If non-nil, this should be a string.
10764 It means catch X errors and store the error message in this string. */
10765
10766static Lisp_Object x_error_message_string;
10767
10768/* An X error handler which stores the error message in
10769 x_error_message_string. This is called from x_error_handler if
10770 x_catch_errors is in effect. */
10771
06a2c219 10772static void
e99db5a1
RS
10773x_error_catcher (display, error)
10774 Display *display;
10775 XErrorEvent *error;
10776{
10777 XGetErrorText (display, error->error_code,
10778 XSTRING (x_error_message_string)->data,
10779 X_ERROR_MESSAGE_SIZE);
10780}
10781
10782/* Begin trapping X errors for display DPY. Actually we trap X errors
10783 for all displays, but DPY should be the display you are actually
10784 operating on.
10785
10786 After calling this function, X protocol errors no longer cause
10787 Emacs to exit; instead, they are recorded in the string
10788 stored in x_error_message_string.
10789
10790 Calling x_check_errors signals an Emacs error if an X error has
10791 occurred since the last call to x_catch_errors or x_check_errors.
10792
10793 Calling x_uncatch_errors resumes the normal error handling. */
10794
10795void x_check_errors ();
10796static Lisp_Object x_catch_errors_unwind ();
10797
10798int
10799x_catch_errors (dpy)
10800 Display *dpy;
10801{
10802 int count = specpdl_ptr - specpdl;
10803
10804 /* Make sure any errors from previous requests have been dealt with. */
10805 XSync (dpy, False);
10806
10807 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
10808
10809 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
10810 XSTRING (x_error_message_string)->data[0] = 0;
10811
10812 return count;
10813}
10814
10815/* Unbind the binding that we made to check for X errors. */
10816
10817static Lisp_Object
10818x_catch_errors_unwind (old_val)
10819 Lisp_Object old_val;
10820{
10821 x_error_message_string = old_val;
10822 return Qnil;
10823}
10824
10825/* If any X protocol errors have arrived since the last call to
10826 x_catch_errors or x_check_errors, signal an Emacs error using
10827 sprintf (a buffer, FORMAT, the x error message text) as the text. */
10828
10829void
10830x_check_errors (dpy, format)
10831 Display *dpy;
10832 char *format;
10833{
10834 /* Make sure to catch any errors incurred so far. */
10835 XSync (dpy, False);
10836
10837 if (XSTRING (x_error_message_string)->data[0])
10838 error (format, XSTRING (x_error_message_string)->data);
10839}
10840
9829ddba
RS
10841/* Nonzero if we had any X protocol errors
10842 since we did x_catch_errors on DPY. */
e99db5a1
RS
10843
10844int
10845x_had_errors_p (dpy)
10846 Display *dpy;
10847{
10848 /* Make sure to catch any errors incurred so far. */
10849 XSync (dpy, False);
10850
10851 return XSTRING (x_error_message_string)->data[0] != 0;
10852}
10853
9829ddba
RS
10854/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
10855
06a2c219 10856void
9829ddba
RS
10857x_clear_errors (dpy)
10858 Display *dpy;
10859{
10860 XSTRING (x_error_message_string)->data[0] = 0;
10861}
10862
e99db5a1
RS
10863/* Stop catching X protocol errors and let them make Emacs die.
10864 DPY should be the display that was passed to x_catch_errors.
10865 COUNT should be the value that was returned by
10866 the corresponding call to x_catch_errors. */
10867
10868void
10869x_uncatch_errors (dpy, count)
10870 Display *dpy;
10871 int count;
10872{
10873 unbind_to (count, Qnil);
10874}
10875
10876#if 0
10877static unsigned int x_wire_count;
10878x_trace_wire ()
10879{
10880 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
10881}
10882#endif /* ! 0 */
10883
10884\f
10885/* Handle SIGPIPE, which can happen when the connection to a server
10886 simply goes away. SIGPIPE is handled by x_connection_signal.
10887 Don't need to do anything, because the write which caused the
10888 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
06a2c219 10889 which will do the appropriate cleanup for us. */
e99db5a1
RS
10890
10891static SIGTYPE
10892x_connection_signal (signalnum) /* If we don't have an argument, */
06a2c219 10893 int signalnum; /* some compilers complain in signal calls. */
e99db5a1
RS
10894{
10895#ifdef USG
10896 /* USG systems forget handlers when they are used;
10897 must reestablish each time */
10898 signal (signalnum, x_connection_signal);
10899#endif /* USG */
10900}
10901\f
4746118a
JB
10902/* Handling X errors. */
10903
7a13e894 10904/* Handle the loss of connection to display DISPLAY. */
16bd92ea 10905
4746118a 10906static SIGTYPE
7a13e894
RS
10907x_connection_closed (display, error_message)
10908 Display *display;
10909 char *error_message;
4746118a 10910{
7a13e894
RS
10911 struct x_display_info *dpyinfo = x_display_info_for_display (display);
10912 Lisp_Object frame, tail;
10913
6186a4a0
RS
10914 /* Indicate that this display is dead. */
10915
2e465cdd 10916#if 0 /* Closing the display caused a bus error on OpenWindows. */
f613a4c8 10917#ifdef USE_X_TOOLKIT
adabc3a9 10918 XtCloseDisplay (display);
2e465cdd 10919#endif
f613a4c8 10920#endif
adabc3a9 10921
9e80b57d
KR
10922 if (dpyinfo)
10923 dpyinfo->display = 0;
6186a4a0 10924
06a2c219 10925 /* First delete frames whose mini-buffers are on frames
7a13e894
RS
10926 that are on the dead display. */
10927 FOR_EACH_FRAME (tail, frame)
10928 {
10929 Lisp_Object minibuf_frame;
10930 minibuf_frame
10931 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
f48f33ca
RS
10932 if (FRAME_X_P (XFRAME (frame))
10933 && FRAME_X_P (XFRAME (minibuf_frame))
10934 && ! EQ (frame, minibuf_frame)
10935 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7a13e894
RS
10936 Fdelete_frame (frame, Qt);
10937 }
10938
10939 /* Now delete all remaining frames on the dead display.
06a2c219 10940 We are now sure none of these is used as the mini-buffer
7a13e894
RS
10941 for another frame that we need to delete. */
10942 FOR_EACH_FRAME (tail, frame)
f48f33ca
RS
10943 if (FRAME_X_P (XFRAME (frame))
10944 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
07a7096a
KH
10945 {
10946 /* Set this to t so that Fdelete_frame won't get confused
10947 trying to find a replacement. */
10948 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
10949 Fdelete_frame (frame, Qt);
10950 }
7a13e894 10951
482a1bd2
KH
10952 if (dpyinfo)
10953 x_delete_display (dpyinfo);
7a13e894
RS
10954
10955 if (x_display_list == 0)
10956 {
f8d07b62 10957 fprintf (stderr, "%s\n", error_message);
7a13e894
RS
10958 shut_down_emacs (0, 0, Qnil);
10959 exit (70);
10960 }
12ba150f 10961
7a13e894
RS
10962 /* Ordinary stack unwind doesn't deal with these. */
10963#ifdef SIGIO
10964 sigunblock (sigmask (SIGIO));
10965#endif
10966 sigunblock (sigmask (SIGALRM));
10967 TOTALLY_UNBLOCK_INPUT;
10968
aa4d9a9e 10969 clear_waiting_for_input ();
7a13e894 10970 error ("%s", error_message);
4746118a
JB
10971}
10972
7a13e894
RS
10973/* This is the usual handler for X protocol errors.
10974 It kills all frames on the display that we got the error for.
10975 If that was the only one, it prints an error message and kills Emacs. */
10976
06a2c219 10977static void
c118dd06
JB
10978x_error_quitter (display, error)
10979 Display *display;
10980 XErrorEvent *error;
10981{
7a13e894 10982 char buf[256], buf1[356];
dc6f92b8 10983
58769bee 10984 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 10985 original error handler. */
dc6f92b8 10986
c118dd06 10987 XGetErrorText (display, error->error_code, buf, sizeof (buf));
0a0fdc70 10988 sprintf (buf1, "X protocol error: %s on protocol request %d",
c118dd06 10989 buf, error->request_code);
7a13e894 10990 x_connection_closed (display, buf1);
dc6f92b8
JB
10991}
10992
e99db5a1
RS
10993/* This is the first-level handler for X protocol errors.
10994 It calls x_error_quitter or x_error_catcher. */
7a13e894 10995
8922af5f 10996static int
e99db5a1 10997x_error_handler (display, error)
8922af5f 10998 Display *display;
e99db5a1 10999 XErrorEvent *error;
8922af5f 11000{
e99db5a1
RS
11001 if (! NILP (x_error_message_string))
11002 x_error_catcher (display, error);
11003 else
11004 x_error_quitter (display, error);
06a2c219 11005 return 0;
f9e24cb9 11006}
c118dd06 11007
e99db5a1
RS
11008/* This is the handler for X IO errors, always.
11009 It kills all frames on the display that we lost touch with.
11010 If that was the only one, it prints an error message and kills Emacs. */
7a13e894 11011
c118dd06 11012static int
e99db5a1 11013x_io_error_quitter (display)
c118dd06 11014 Display *display;
c118dd06 11015{
e99db5a1 11016 char buf[256];
dc6f92b8 11017
e99db5a1
RS
11018 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
11019 x_connection_closed (display, buf);
06a2c219 11020 return 0;
dc6f92b8 11021}
dc6f92b8 11022\f
f451eb13
JB
11023/* Changing the font of the frame. */
11024
76bcdf39
RS
11025/* Give frame F the font named FONTNAME as its default font, and
11026 return the full name of that font. FONTNAME may be a wildcard
11027 pattern; in that case, we choose some font that fits the pattern.
11028 The return value shows which font we chose. */
11029
b5cf7a0e 11030Lisp_Object
f676886a
JB
11031x_new_font (f, fontname)
11032 struct frame *f;
dc6f92b8
JB
11033 register char *fontname;
11034{
dc43ef94 11035 struct font_info *fontp
ee569018 11036 = FS_LOAD_FONT (f, 0, fontname, -1);
dc6f92b8 11037
dc43ef94
KH
11038 if (!fontp)
11039 return Qnil;
2224a5fc 11040
dc43ef94 11041 f->output_data.x->font = (XFontStruct *) (fontp->font);
b4192550 11042 f->output_data.x->baseline_offset = fontp->baseline_offset;
dc43ef94
KH
11043 f->output_data.x->fontset = -1;
11044
b2cad826
KH
11045 /* Compute the scroll bar width in character columns. */
11046 if (f->scroll_bar_pixel_width > 0)
11047 {
7556890b 11048 int wid = FONT_WIDTH (f->output_data.x->font);
b2cad826
KH
11049 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
11050 }
11051 else
4e61bddf
RS
11052 {
11053 int wid = FONT_WIDTH (f->output_data.x->font);
11054 f->scroll_bar_cols = (14 + wid - 1) / wid;
11055 }
b2cad826 11056
f676886a 11057 /* Now make the frame display the given font. */
c118dd06 11058 if (FRAME_X_WINDOW (f) != 0)
dc6f92b8 11059 {
7556890b
RS
11060 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
11061 f->output_data.x->font->fid);
11062 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
11063 f->output_data.x->font->fid);
11064 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
11065 f->output_data.x->font->fid);
f676886a 11066
a27f9f86 11067 frame_update_line_height (f);
0134a210 11068 x_set_window_size (f, 0, f->width, f->height);
dc6f92b8 11069 }
a27f9f86
RS
11070 else
11071 /* If we are setting a new frame's font for the first time,
11072 there are no faces yet, so this font's height is the line height. */
7556890b 11073 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
dc6f92b8 11074
dc43ef94
KH
11075 return build_string (fontp->full_name);
11076}
11077
11078/* Give frame F the fontset named FONTSETNAME as its default font, and
11079 return the full name of that fontset. FONTSETNAME may be a wildcard
b5210ea7
KH
11080 pattern; in that case, we choose some fontset that fits the pattern.
11081 The return value shows which fontset we chose. */
b5cf7a0e 11082
dc43ef94
KH
11083Lisp_Object
11084x_new_fontset (f, fontsetname)
11085 struct frame *f;
11086 char *fontsetname;
11087{
ee569018 11088 int fontset = fs_query_fontset (build_string (fontsetname), 0);
dc43ef94 11089 Lisp_Object result;
b5cf7a0e 11090
dc43ef94
KH
11091 if (fontset < 0)
11092 return Qnil;
b5cf7a0e 11093
2da424f1
KH
11094 if (f->output_data.x->fontset == fontset)
11095 /* This fontset is already set in frame F. There's nothing more
11096 to do. */
ee569018 11097 return fontset_name (fontset);
dc43ef94 11098
ee569018 11099 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
dc43ef94
KH
11100
11101 if (!STRINGP (result))
11102 /* Can't load ASCII font. */
11103 return Qnil;
11104
11105 /* Since x_new_font doesn't update any fontset information, do it now. */
11106 f->output_data.x->fontset = fontset;
dc43ef94 11107
f5d11644
GM
11108#ifdef HAVE_X_I18N
11109 if (FRAME_XIC (f)
11110 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
ee569018 11111 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
f5d11644
GM
11112#endif
11113
dc43ef94 11114 return build_string (fontsetname);
dc6f92b8 11115}
f5d11644
GM
11116
11117\f
11118/***********************************************************************
11119 X Input Methods
11120 ***********************************************************************/
11121
11122#ifdef HAVE_X_I18N
11123
11124#ifdef HAVE_X11R6
11125
11126/* XIM destroy callback function, which is called whenever the
11127 connection to input method XIM dies. CLIENT_DATA contains a
11128 pointer to the x_display_info structure corresponding to XIM. */
11129
11130static void
11131xim_destroy_callback (xim, client_data, call_data)
11132 XIM xim;
11133 XPointer client_data;
11134 XPointer call_data;
11135{
11136 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
11137 Lisp_Object frame, tail;
11138
11139 BLOCK_INPUT;
11140
11141 /* No need to call XDestroyIC.. */
11142 FOR_EACH_FRAME (tail, frame)
11143 {
11144 struct frame *f = XFRAME (frame);
11145 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
11146 {
11147 FRAME_XIC (f) = NULL;
11148 if (FRAME_XIC_FONTSET (f))
11149 {
11150 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
11151 FRAME_XIC_FONTSET (f) = NULL;
11152 }
11153 }
11154 }
11155
11156 /* No need to call XCloseIM. */
11157 dpyinfo->xim = NULL;
11158 XFree (dpyinfo->xim_styles);
11159 UNBLOCK_INPUT;
11160}
11161
11162#endif /* HAVE_X11R6 */
11163
11164/* Open the connection to the XIM server on display DPYINFO.
11165 RESOURCE_NAME is the resource name Emacs uses. */
11166
11167static void
11168xim_open_dpy (dpyinfo, resource_name)
11169 struct x_display_info *dpyinfo;
11170 char *resource_name;
11171{
287f7dd6 11172#ifdef USE_XIM
f5d11644
GM
11173 XIM xim;
11174
11175 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
11176 dpyinfo->xim = xim;
11177
11178 if (xim)
11179 {
f5d11644
GM
11180#ifdef HAVE_X11R6
11181 XIMCallback destroy;
11182#endif
11183
11184 /* Get supported styles and XIM values. */
11185 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
11186
11187#ifdef HAVE_X11R6
11188 destroy.callback = xim_destroy_callback;
11189 destroy.client_data = (XPointer)dpyinfo;
68642df6 11190 /* This isn't prptotyped in OSF 5.0. */
f5d11644
GM
11191 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
11192#endif
11193 }
287f7dd6
GM
11194
11195#else /* not USE_XIM */
11196 dpyinfo->xim = NULL;
11197#endif /* not USE_XIM */
f5d11644
GM
11198}
11199
11200
b9de836c 11201#ifdef HAVE_X11R6_XIM
f5d11644
GM
11202
11203struct xim_inst_t
11204{
11205 struct x_display_info *dpyinfo;
11206 char *resource_name;
11207};
11208
11209/* XIM instantiate callback function, which is called whenever an XIM
11210 server is available. DISPLAY is teh display of the XIM.
11211 CLIENT_DATA contains a pointer to an xim_inst_t structure created
11212 when the callback was registered. */
11213
11214static void
11215xim_instantiate_callback (display, client_data, call_data)
11216 Display *display;
11217 XPointer client_data;
11218 XPointer call_data;
11219{
11220 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
11221 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
11222
11223 /* We don't support multiple XIM connections. */
11224 if (dpyinfo->xim)
11225 return;
11226
11227 xim_open_dpy (dpyinfo, xim_inst->resource_name);
11228
11229 /* Create XIC for the existing frames on the same display, as long
11230 as they have no XIC. */
11231 if (dpyinfo->xim && dpyinfo->reference_count > 0)
11232 {
11233 Lisp_Object tail, frame;
11234
11235 BLOCK_INPUT;
11236 FOR_EACH_FRAME (tail, frame)
11237 {
11238 struct frame *f = XFRAME (frame);
11239
11240 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
11241 if (FRAME_XIC (f) == NULL)
11242 {
11243 create_frame_xic (f);
11244 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
11245 xic_set_statusarea (f);
11246 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
11247 {
11248 struct window *w = XWINDOW (f->selected_window);
11249 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
11250 }
11251 }
11252 }
11253
11254 UNBLOCK_INPUT;
11255 }
11256}
11257
b9de836c 11258#endif /* HAVE_X11R6_XIM */
f5d11644
GM
11259
11260
11261/* Open a connection to the XIM server on display DPYINFO.
11262 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
11263 connection only at the first time. On X11R6, open the connection
11264 in the XIM instantiate callback function. */
11265
11266static void
11267xim_initialize (dpyinfo, resource_name)
11268 struct x_display_info *dpyinfo;
11269 char *resource_name;
11270{
287f7dd6 11271#ifdef USE_XIM
b9de836c 11272#ifdef HAVE_X11R6_XIM
f5d11644
GM
11273 struct xim_inst_t *xim_inst;
11274 int len;
11275
11276 dpyinfo->xim = NULL;
11277 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
11278 xim_inst->dpyinfo = dpyinfo;
11279 len = strlen (resource_name);
11280 xim_inst->resource_name = (char *) xmalloc (len + 1);
11281 bcopy (resource_name, xim_inst->resource_name, len + 1);
11282 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11283 resource_name, EMACS_CLASS,
11284 xim_instantiate_callback,
2ebb2f8b
DL
11285 /* Fixme: This is XPointer in
11286 XFree86 but (XPointer *) on
11287 Tru64, at least. */
11288 (XPointer) xim_inst);
b9de836c 11289#else /* not HAVE_X11R6_XIM */
f5d11644
GM
11290 dpyinfo->xim = NULL;
11291 xim_open_dpy (dpyinfo, resource_name);
b9de836c 11292#endif /* not HAVE_X11R6_XIM */
287f7dd6
GM
11293
11294#else /* not USE_XIM */
11295 dpyinfo->xim = NULL;
11296#endif /* not USE_XIM */
f5d11644
GM
11297}
11298
11299
11300/* Close the connection to the XIM server on display DPYINFO. */
11301
11302static void
11303xim_close_dpy (dpyinfo)
11304 struct x_display_info *dpyinfo;
11305{
287f7dd6 11306#ifdef USE_XIM
b9de836c 11307#ifdef HAVE_X11R6_XIM
f5d11644
GM
11308 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11309 NULL, EMACS_CLASS,
11310 xim_instantiate_callback, NULL);
b9de836c 11311#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
11312 XCloseIM (dpyinfo->xim);
11313 dpyinfo->xim = NULL;
11314 XFree (dpyinfo->xim_styles);
287f7dd6 11315#endif /* USE_XIM */
f5d11644
GM
11316}
11317
b9de836c 11318#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
11319
11320
dc6f92b8 11321\f
2e365682
RS
11322/* Calculate the absolute position in frame F
11323 from its current recorded position values and gravity. */
11324
dfcf069d 11325void
43bca5d5 11326x_calc_absolute_position (f)
f676886a 11327 struct frame *f;
dc6f92b8 11328{
06a2c219 11329 Window child;
6dba1858 11330 int win_x = 0, win_y = 0;
7556890b 11331 int flags = f->output_data.x->size_hint_flags;
c81412a0
KH
11332 int this_window;
11333
9829ddba
RS
11334 /* We have nothing to do if the current position
11335 is already for the top-left corner. */
11336 if (! ((flags & XNegative) || (flags & YNegative)))
11337 return;
11338
c81412a0 11339#ifdef USE_X_TOOLKIT
7556890b 11340 this_window = XtWindow (f->output_data.x->widget);
c81412a0
KH
11341#else
11342 this_window = FRAME_X_WINDOW (f);
11343#endif
6dba1858
RS
11344
11345 /* Find the position of the outside upper-left corner of
9829ddba
RS
11346 the inner window, with respect to the outer window.
11347 But do this only if we will need the results. */
7556890b 11348 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6dba1858 11349 {
9829ddba
RS
11350 int count;
11351
6dba1858 11352 BLOCK_INPUT;
9829ddba
RS
11353 count = x_catch_errors (FRAME_X_DISPLAY (f));
11354 while (1)
11355 {
11356 x_clear_errors (FRAME_X_DISPLAY (f));
11357 XTranslateCoordinates (FRAME_X_DISPLAY (f),
11358
11359 /* From-window, to-window. */
11360 this_window,
11361 f->output_data.x->parent_desc,
11362
11363 /* From-position, to-position. */
11364 0, 0, &win_x, &win_y,
11365
11366 /* Child of win. */
11367 &child);
11368 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
11369 {
11370 Window newroot, newparent = 0xdeadbeef;
11371 Window *newchildren;
2ebb2f8b 11372 unsigned int nchildren;
9829ddba
RS
11373
11374 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
11375 &newparent, &newchildren, &nchildren))
11376 break;
58769bee 11377
7c3c78a3 11378 XFree ((char *) newchildren);
6dba1858 11379
9829ddba
RS
11380 f->output_data.x->parent_desc = newparent;
11381 }
11382 else
11383 break;
11384 }
6dba1858 11385
9829ddba 11386 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
6dba1858
RS
11387 UNBLOCK_INPUT;
11388 }
11389
11390 /* Treat negative positions as relative to the leftmost bottommost
11391 position that fits on the screen. */
20f55f9a 11392 if (flags & XNegative)
7556890b 11393 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
2e365682
RS
11394 - 2 * f->output_data.x->border_width - win_x
11395 - PIXEL_WIDTH (f)
11396 + f->output_data.x->left_pos);
dc6f92b8 11397
20f55f9a 11398 if (flags & YNegative)
06a2c219
GM
11399 {
11400 int menubar_height = 0;
11401
11402#ifdef USE_X_TOOLKIT
11403 if (f->output_data.x->menubar_widget)
11404 menubar_height
11405 = (f->output_data.x->menubar_widget->core.height
11406 + f->output_data.x->menubar_widget->core.border_width);
11407#endif
11408
11409 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
11410 - 2 * f->output_data.x->border_width
11411 - win_y
11412 - PIXEL_HEIGHT (f)
11413 - menubar_height
11414 + f->output_data.x->top_pos);
11415 }
2e365682 11416
3a35ab44
RS
11417 /* The left_pos and top_pos
11418 are now relative to the top and left screen edges,
11419 so the flags should correspond. */
7556890b 11420 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
dc6f92b8
JB
11421}
11422
3a35ab44
RS
11423/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
11424 to really change the position, and 0 when calling from
11425 x_make_frame_visible (in that case, XOFF and YOFF are the current
aa3ff7c9
KH
11426 position values). It is -1 when calling from x_set_frame_parameters,
11427 which means, do adjust for borders but don't change the gravity. */
3a35ab44 11428
dfcf069d 11429void
dc05a16b 11430x_set_offset (f, xoff, yoff, change_gravity)
f676886a 11431 struct frame *f;
dc6f92b8 11432 register int xoff, yoff;
dc05a16b 11433 int change_gravity;
dc6f92b8 11434{
4a4cbdd5
KH
11435 int modified_top, modified_left;
11436
aa3ff7c9 11437 if (change_gravity > 0)
3a35ab44 11438 {
7556890b
RS
11439 f->output_data.x->top_pos = yoff;
11440 f->output_data.x->left_pos = xoff;
11441 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
3a35ab44 11442 if (xoff < 0)
7556890b 11443 f->output_data.x->size_hint_flags |= XNegative;
3a35ab44 11444 if (yoff < 0)
7556890b
RS
11445 f->output_data.x->size_hint_flags |= YNegative;
11446 f->output_data.x->win_gravity = NorthWestGravity;
3a35ab44 11447 }
43bca5d5 11448 x_calc_absolute_position (f);
dc6f92b8
JB
11449
11450 BLOCK_INPUT;
c32cdd9a 11451 x_wm_set_size_hint (f, (long) 0, 0);
3a35ab44 11452
7556890b
RS
11453 modified_left = f->output_data.x->left_pos;
11454 modified_top = f->output_data.x->top_pos;
e73ec6fa
RS
11455#if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
11456 this seems to be unnecessary and incorrect. rms, 4/17/97. */
11457 /* It is a mystery why we need to add the border_width here
11458 when the frame is already visible, but experiment says we do. */
aa3ff7c9 11459 if (change_gravity != 0)
4a4cbdd5 11460 {
7556890b
RS
11461 modified_left += f->output_data.x->border_width;
11462 modified_top += f->output_data.x->border_width;
4a4cbdd5 11463 }
e73ec6fa 11464#endif
4a4cbdd5 11465
3afe33e7 11466#ifdef USE_X_TOOLKIT
7556890b 11467 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
4a4cbdd5 11468 modified_left, modified_top);
3afe33e7 11469#else /* not USE_X_TOOLKIT */
334208b7 11470 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4a4cbdd5 11471 modified_left, modified_top);
3afe33e7 11472#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11473 UNBLOCK_INPUT;
11474}
11475
bc20ebbf
FP
11476/* Call this to change the size of frame F's x-window.
11477 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
11478 for this size change and subsequent size changes.
11479 Otherwise we leave the window gravity unchanged. */
dc6f92b8 11480
dfcf069d 11481void
bc20ebbf 11482x_set_window_size (f, change_gravity, cols, rows)
f676886a 11483 struct frame *f;
bc20ebbf 11484 int change_gravity;
b1c884c3 11485 int cols, rows;
dc6f92b8 11486{
06a2c219 11487#ifndef USE_X_TOOLKIT
dc6f92b8 11488 int pixelwidth, pixelheight;
06a2c219 11489#endif
dc6f92b8 11490
80fd1fe2 11491 BLOCK_INPUT;
aee9a898
RS
11492
11493#ifdef USE_X_TOOLKIT
3a20653d
RS
11494 {
11495 /* The x and y position of the widget is clobbered by the
11496 call to XtSetValues within EmacsFrameSetCharSize.
11497 This is a real kludge, but I don't understand Xt so I can't
11498 figure out a correct fix. Can anyone else tell me? -- rms. */
7556890b
RS
11499 int xpos = f->output_data.x->widget->core.x;
11500 int ypos = f->output_data.x->widget->core.y;
11501 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
11502 f->output_data.x->widget->core.x = xpos;
11503 f->output_data.x->widget->core.y = ypos;
3a20653d 11504 }
80fd1fe2
FP
11505
11506#else /* not USE_X_TOOLKIT */
11507
b1c884c3 11508 check_frame_size (f, &rows, &cols);
7556890b 11509 f->output_data.x->vertical_scroll_bar_extra
b2cad826
KH
11510 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
11511 ? 0
11512 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
480407eb 11513 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7556890b 11514 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
06a2c219 11515 f->output_data.x->flags_areas_extra
110859fc 11516 = FRAME_FLAGS_AREA_WIDTH (f);
f451eb13
JB
11517 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
11518 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
dc6f92b8 11519
7556890b 11520 f->output_data.x->win_gravity = NorthWestGravity;
c32cdd9a 11521 x_wm_set_size_hint (f, (long) 0, 0);
6ccf47d1 11522
334208b7
RS
11523 XSync (FRAME_X_DISPLAY (f), False);
11524 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11525 pixelwidth, pixelheight);
b1c884c3
JB
11526
11527 /* Now, strictly speaking, we can't be sure that this is accurate,
11528 but the window manager will get around to dealing with the size
11529 change request eventually, and we'll hear how it went when the
dbc4e1c1
JB
11530 ConfigureNotify event gets here.
11531
11532 We could just not bother storing any of this information here,
11533 and let the ConfigureNotify event set everything up, but that
fddd5ceb 11534 might be kind of confusing to the Lisp code, since size changes
dbc4e1c1 11535 wouldn't be reported in the frame parameters until some random
fddd5ceb
RS
11536 point in the future when the ConfigureNotify event arrives.
11537
11538 We pass 1 for DELAY since we can't run Lisp code inside of
11539 a BLOCK_INPUT. */
7d1e984f 11540 change_frame_size (f, rows, cols, 0, 1, 0);
b1c884c3
JB
11541 PIXEL_WIDTH (f) = pixelwidth;
11542 PIXEL_HEIGHT (f) = pixelheight;
11543
aee9a898
RS
11544 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11545 receive in the ConfigureNotify event; if we get what we asked
11546 for, then the event won't cause the screen to become garbaged, so
11547 we have to make sure to do it here. */
11548 SET_FRAME_GARBAGED (f);
11549
11550 XFlush (FRAME_X_DISPLAY (f));
11551
11552#endif /* not USE_X_TOOLKIT */
11553
4d73d038 11554 /* If cursor was outside the new size, mark it as off. */
06a2c219 11555 mark_window_cursors_off (XWINDOW (f->root_window));
4d73d038 11556
aee9a898
RS
11557 /* Clear out any recollection of where the mouse highlighting was,
11558 since it might be in a place that's outside the new frame size.
11559 Actually checking whether it is outside is a pain in the neck,
11560 so don't try--just let the highlighting be done afresh with new size. */
e687d06e 11561 cancel_mouse_face (f);
dbc4e1c1 11562
dc6f92b8
JB
11563 UNBLOCK_INPUT;
11564}
dc6f92b8 11565\f
d047c4eb 11566/* Mouse warping. */
dc6f92b8 11567
9b378208 11568void
f676886a
JB
11569x_set_mouse_position (f, x, y)
11570 struct frame *f;
dc6f92b8
JB
11571 int x, y;
11572{
11573 int pix_x, pix_y;
11574
7556890b
RS
11575 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
11576 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
f451eb13
JB
11577
11578 if (pix_x < 0) pix_x = 0;
11579 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
11580
11581 if (pix_y < 0) pix_y = 0;
11582 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
dc6f92b8
JB
11583
11584 BLOCK_INPUT;
dc6f92b8 11585
334208b7
RS
11586 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11587 0, 0, 0, 0, pix_x, pix_y);
dc6f92b8
JB
11588 UNBLOCK_INPUT;
11589}
11590
9b378208
RS
11591/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11592
11593void
11594x_set_mouse_pixel_position (f, pix_x, pix_y)
11595 struct frame *f;
11596 int pix_x, pix_y;
11597{
11598 BLOCK_INPUT;
11599
334208b7
RS
11600 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11601 0, 0, 0, 0, pix_x, pix_y);
9b378208
RS
11602 UNBLOCK_INPUT;
11603}
d047c4eb
KH
11604\f
11605/* focus shifting, raising and lowering. */
9b378208 11606
dfcf069d 11607void
f676886a
JB
11608x_focus_on_frame (f)
11609 struct frame *f;
dc6f92b8 11610{
1fb20991 11611#if 0 /* This proves to be unpleasant. */
f676886a 11612 x_raise_frame (f);
1fb20991 11613#endif
6d4238f3
JB
11614#if 0
11615 /* I don't think that the ICCCM allows programs to do things like this
11616 without the interaction of the window manager. Whatever you end up
f676886a 11617 doing with this code, do it to x_unfocus_frame too. */
334208b7 11618 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dc6f92b8 11619 RevertToPointerRoot, CurrentTime);
c118dd06 11620#endif /* ! 0 */
dc6f92b8
JB
11621}
11622
dfcf069d 11623void
f676886a
JB
11624x_unfocus_frame (f)
11625 struct frame *f;
dc6f92b8 11626{
6d4238f3 11627#if 0
f676886a 11628 /* Look at the remarks in x_focus_on_frame. */
0f941935 11629 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
334208b7 11630 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
dc6f92b8 11631 RevertToPointerRoot, CurrentTime);
c118dd06 11632#endif /* ! 0 */
dc6f92b8
JB
11633}
11634
f676886a 11635/* Raise frame F. */
dc6f92b8 11636
dfcf069d 11637void
f676886a
JB
11638x_raise_frame (f)
11639 struct frame *f;
dc6f92b8 11640{
3a88c238 11641 if (f->async_visible)
dc6f92b8
JB
11642 {
11643 BLOCK_INPUT;
3afe33e7 11644#ifdef USE_X_TOOLKIT
7556890b 11645 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 11646#else /* not USE_X_TOOLKIT */
334208b7 11647 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11648#endif /* not USE_X_TOOLKIT */
334208b7 11649 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
11650 UNBLOCK_INPUT;
11651 }
11652}
11653
f676886a 11654/* Lower frame F. */
dc6f92b8 11655
dfcf069d 11656void
f676886a
JB
11657x_lower_frame (f)
11658 struct frame *f;
dc6f92b8 11659{
3a88c238 11660 if (f->async_visible)
dc6f92b8
JB
11661 {
11662 BLOCK_INPUT;
3afe33e7 11663#ifdef USE_X_TOOLKIT
7556890b 11664 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 11665#else /* not USE_X_TOOLKIT */
334208b7 11666 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11667#endif /* not USE_X_TOOLKIT */
334208b7 11668 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
11669 UNBLOCK_INPUT;
11670 }
11671}
11672
dbc4e1c1 11673static void
6b0442dc 11674XTframe_raise_lower (f, raise_flag)
dbc4e1c1 11675 FRAME_PTR f;
6b0442dc 11676 int raise_flag;
dbc4e1c1 11677{
6b0442dc 11678 if (raise_flag)
dbc4e1c1
JB
11679 x_raise_frame (f);
11680 else
11681 x_lower_frame (f);
11682}
d047c4eb
KH
11683\f
11684/* Change of visibility. */
dc6f92b8 11685
9382638d
KH
11686/* This tries to wait until the frame is really visible.
11687 However, if the window manager asks the user where to position
11688 the frame, this will return before the user finishes doing that.
11689 The frame will not actually be visible at that time,
11690 but it will become visible later when the window manager
11691 finishes with it. */
11692
dfcf069d 11693void
f676886a
JB
11694x_make_frame_visible (f)
11695 struct frame *f;
dc6f92b8 11696{
990ba854 11697 Lisp_Object type;
1aa6072f 11698 int original_top, original_left;
dc6f92b8 11699
dc6f92b8 11700 BLOCK_INPUT;
dc6f92b8 11701
990ba854
RS
11702 type = x_icon_type (f);
11703 if (!NILP (type))
11704 x_bitmap_icon (f, type);
bdcd49ba 11705
f676886a 11706 if (! FRAME_VISIBLE_P (f))
90e65f07 11707 {
1aa6072f
RS
11708 /* We test FRAME_GARBAGED_P here to make sure we don't
11709 call x_set_offset a second time
11710 if we get to x_make_frame_visible a second time
11711 before the window gets really visible. */
11712 if (! FRAME_ICONIFIED_P (f)
11713 && ! f->output_data.x->asked_for_visible)
11714 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
11715
11716 f->output_data.x->asked_for_visible = 1;
11717
90e65f07 11718 if (! EQ (Vx_no_window_manager, Qt))
f676886a 11719 x_wm_set_window_state (f, NormalState);
3afe33e7 11720#ifdef USE_X_TOOLKIT
d7a38a2e 11721 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 11722 XtMapWidget (f->output_data.x->widget);
3afe33e7 11723#else /* not USE_X_TOOLKIT */
7f9c7f94 11724 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11725#endif /* not USE_X_TOOLKIT */
0134a210
RS
11726#if 0 /* This seems to bring back scroll bars in the wrong places
11727 if the window configuration has changed. They seem
11728 to come back ok without this. */
ab648270 11729 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
334208b7 11730 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
0134a210 11731#endif
90e65f07 11732 }
dc6f92b8 11733
334208b7 11734 XFlush (FRAME_X_DISPLAY (f));
90e65f07 11735
0dacf791
RS
11736 /* Synchronize to ensure Emacs knows the frame is visible
11737 before we do anything else. We do this loop with input not blocked
11738 so that incoming events are handled. */
11739 {
11740 Lisp_Object frame;
12ce2351 11741 int count;
28c01ffe
RS
11742 /* This must be before UNBLOCK_INPUT
11743 since events that arrive in response to the actions above
11744 will set it when they are handled. */
11745 int previously_visible = f->output_data.x->has_been_visible;
1aa6072f
RS
11746
11747 original_left = f->output_data.x->left_pos;
11748 original_top = f->output_data.x->top_pos;
c0a04927
RS
11749
11750 /* This must come after we set COUNT. */
11751 UNBLOCK_INPUT;
11752
2745e6c4 11753 /* We unblock here so that arriving X events are processed. */
1aa6072f 11754
dcb07ae9
RS
11755 /* Now move the window back to where it was "supposed to be".
11756 But don't do it if the gravity is negative.
11757 When the gravity is negative, this uses a position
28c01ffe
RS
11758 that is 3 pixels too low. Perhaps that's really the border width.
11759
11760 Don't do this if the window has never been visible before,
11761 because the window manager may choose the position
11762 and we don't want to override it. */
1aa6072f 11763
4d3f5d9a 11764 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
06c488fd 11765 && f->output_data.x->win_gravity == NorthWestGravity
28c01ffe 11766 && previously_visible)
1aa6072f 11767 {
2745e6c4
RS
11768 Drawable rootw;
11769 int x, y;
11770 unsigned int width, height, border, depth;
06a2c219 11771
1aa6072f 11772 BLOCK_INPUT;
9829ddba 11773
06a2c219
GM
11774 /* On some window managers (such as FVWM) moving an existing
11775 window, even to the same place, causes the window manager
11776 to introduce an offset. This can cause the window to move
11777 to an unexpected location. Check the geometry (a little
11778 slow here) and then verify that the window is in the right
11779 place. If the window is not in the right place, move it
11780 there, and take the potential window manager hit. */
2745e6c4
RS
11781 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11782 &rootw, &x, &y, &width, &height, &border, &depth);
11783
11784 if (original_left != x || original_top != y)
11785 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11786 original_left, original_top);
11787
1aa6072f
RS
11788 UNBLOCK_INPUT;
11789 }
9829ddba 11790
e0c1aef2 11791 XSETFRAME (frame, f);
c0a04927 11792
12ce2351
GM
11793 /* Wait until the frame is visible. Process X events until a
11794 MapNotify event has been seen, or until we think we won't get a
11795 MapNotify at all.. */
11796 for (count = input_signal_count + 10;
11797 input_signal_count < count && !FRAME_VISIBLE_P (f);)
2a6cf806 11798 {
12ce2351 11799 /* Force processing of queued events. */
334208b7 11800 x_sync (f);
12ce2351
GM
11801
11802 /* Machines that do polling rather than SIGIO have been
11803 observed to go into a busy-wait here. So we'll fake an
11804 alarm signal to let the handler know that there's something
11805 to be read. We used to raise a real alarm, but it seems
11806 that the handler isn't always enabled here. This is
11807 probably a bug. */
8b2f8d4e 11808 if (input_polling_used ())
3b2fa4e6 11809 {
12ce2351
GM
11810 /* It could be confusing if a real alarm arrives while
11811 processing the fake one. Turn it off and let the
11812 handler reset it. */
3e71d8f2 11813 extern void poll_for_input_1 P_ ((void));
bffcfca9
GM
11814 int old_poll_suppress_count = poll_suppress_count;
11815 poll_suppress_count = 1;
11816 poll_for_input_1 ();
11817 poll_suppress_count = old_poll_suppress_count;
3b2fa4e6 11818 }
12ce2351
GM
11819
11820 /* See if a MapNotify event has been processed. */
11821 FRAME_SAMPLE_VISIBILITY (f);
2a6cf806 11822 }
0dacf791 11823 }
dc6f92b8
JB
11824}
11825
06a2c219 11826/* Change from mapped state to withdrawn state. */
dc6f92b8 11827
d047c4eb
KH
11828/* Make the frame visible (mapped and not iconified). */
11829
dfcf069d 11830void
f676886a
JB
11831x_make_frame_invisible (f)
11832 struct frame *f;
dc6f92b8 11833{
546e6d5b
RS
11834 Window window;
11835
11836#ifdef USE_X_TOOLKIT
11837 /* Use the frame's outermost window, not the one we normally draw on. */
7556890b 11838 window = XtWindow (f->output_data.x->widget);
546e6d5b
RS
11839#else /* not USE_X_TOOLKIT */
11840 window = FRAME_X_WINDOW (f);
11841#endif /* not USE_X_TOOLKIT */
dc6f92b8 11842
9319ae23 11843 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
11844 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11845 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 11846
5627c40e 11847#if 0/* This might add unreliability; I don't trust it -- rms. */
9319ae23 11848 if (! f->async_visible && ! f->async_iconified)
dc6f92b8 11849 return;
5627c40e 11850#endif
dc6f92b8
JB
11851
11852 BLOCK_INPUT;
c118dd06 11853
af31d76f
RS
11854 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
11855 that the current position of the window is user-specified, rather than
11856 program-specified, so that when the window is mapped again, it will be
11857 placed at the same location, without forcing the user to position it
11858 by hand again (they have already done that once for this window.) */
c32cdd9a 11859 x_wm_set_size_hint (f, (long) 0, 1);
af31d76f 11860
c118dd06
JB
11861#ifdef HAVE_X11R4
11862
334208b7
RS
11863 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
11864 DefaultScreen (FRAME_X_DISPLAY (f))))
c118dd06
JB
11865 {
11866 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11867 error ("Can't notify window manager of window withdrawal");
c118dd06 11868 }
c118dd06 11869#else /* ! defined (HAVE_X11R4) */
16bd92ea 11870
c118dd06 11871 /* Tell the window manager what we're going to do. */
dc6f92b8
JB
11872 if (! EQ (Vx_no_window_manager, Qt))
11873 {
16bd92ea 11874 XEvent unmap;
dc6f92b8 11875
16bd92ea 11876 unmap.xunmap.type = UnmapNotify;
546e6d5b 11877 unmap.xunmap.window = window;
334208b7 11878 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
16bd92ea 11879 unmap.xunmap.from_configure = False;
334208b7
RS
11880 if (! XSendEvent (FRAME_X_DISPLAY (f),
11881 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea 11882 False,
06a2c219 11883 SubstructureRedirectMaskSubstructureNotifyMask,
16bd92ea
JB
11884 &unmap))
11885 {
11886 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11887 error ("Can't notify window manager of withdrawal");
16bd92ea 11888 }
dc6f92b8
JB
11889 }
11890
16bd92ea 11891 /* Unmap the window ourselves. Cheeky! */
334208b7 11892 XUnmapWindow (FRAME_X_DISPLAY (f), window);
c118dd06 11893#endif /* ! defined (HAVE_X11R4) */
dc6f92b8 11894
5627c40e
RS
11895 /* We can't distinguish this from iconification
11896 just by the event that we get from the server.
11897 So we can't win using the usual strategy of letting
11898 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
11899 and synchronize with the server to make sure we agree. */
11900 f->visible = 0;
11901 FRAME_ICONIFIED_P (f) = 0;
11902 f->async_visible = 0;
11903 f->async_iconified = 0;
11904
334208b7 11905 x_sync (f);
5627c40e 11906
dc6f92b8
JB
11907 UNBLOCK_INPUT;
11908}
11909
06a2c219 11910/* Change window state from mapped to iconified. */
dc6f92b8 11911
dfcf069d 11912void
f676886a
JB
11913x_iconify_frame (f)
11914 struct frame *f;
dc6f92b8 11915{
3afe33e7 11916 int result;
990ba854 11917 Lisp_Object type;
dc6f92b8 11918
9319ae23 11919 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
11920 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11921 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 11922
3a88c238 11923 if (f->async_iconified)
dc6f92b8
JB
11924 return;
11925
3afe33e7 11926 BLOCK_INPUT;
546e6d5b 11927
9af3143a
RS
11928 FRAME_SAMPLE_VISIBILITY (f);
11929
990ba854
RS
11930 type = x_icon_type (f);
11931 if (!NILP (type))
11932 x_bitmap_icon (f, type);
bdcd49ba
RS
11933
11934#ifdef USE_X_TOOLKIT
11935
546e6d5b
RS
11936 if (! FRAME_VISIBLE_P (f))
11937 {
11938 if (! EQ (Vx_no_window_manager, Qt))
11939 x_wm_set_window_state (f, IconicState);
11940 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 11941 XtMapWidget (f->output_data.x->widget);
9cf30a30
RS
11942 /* The server won't give us any event to indicate
11943 that an invisible frame was changed to an icon,
11944 so we have to record it here. */
11945 f->iconified = 1;
1e6bc770 11946 f->visible = 1;
9cf30a30 11947 f->async_iconified = 1;
1e6bc770 11948 f->async_visible = 0;
546e6d5b
RS
11949 UNBLOCK_INPUT;
11950 return;
11951 }
11952
334208b7 11953 result = XIconifyWindow (FRAME_X_DISPLAY (f),
7556890b 11954 XtWindow (f->output_data.x->widget),
334208b7 11955 DefaultScreen (FRAME_X_DISPLAY (f)));
3afe33e7
RS
11956 UNBLOCK_INPUT;
11957
11958 if (!result)
546e6d5b 11959 error ("Can't notify window manager of iconification");
3afe33e7
RS
11960
11961 f->async_iconified = 1;
1e6bc770
RS
11962 f->async_visible = 0;
11963
8c002a25
KH
11964
11965 BLOCK_INPUT;
334208b7 11966 XFlush (FRAME_X_DISPLAY (f));
8c002a25 11967 UNBLOCK_INPUT;
3afe33e7
RS
11968#else /* not USE_X_TOOLKIT */
11969
fd13dbb2
RS
11970 /* Make sure the X server knows where the window should be positioned,
11971 in case the user deiconifies with the window manager. */
11972 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
7556890b 11973 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
fd13dbb2 11974
16bd92ea
JB
11975 /* Since we don't know which revision of X we're running, we'll use both
11976 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
11977
11978 /* X11R4: send a ClientMessage to the window manager using the
11979 WM_CHANGE_STATE type. */
11980 {
11981 XEvent message;
58769bee 11982
c118dd06 11983 message.xclient.window = FRAME_X_WINDOW (f);
16bd92ea 11984 message.xclient.type = ClientMessage;
334208b7 11985 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
16bd92ea
JB
11986 message.xclient.format = 32;
11987 message.xclient.data.l[0] = IconicState;
11988
334208b7
RS
11989 if (! XSendEvent (FRAME_X_DISPLAY (f),
11990 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea
JB
11991 False,
11992 SubstructureRedirectMask | SubstructureNotifyMask,
11993 &message))
dc6f92b8
JB
11994 {
11995 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11996 error ("Can't notify window manager of iconification");
dc6f92b8 11997 }
16bd92ea 11998 }
dc6f92b8 11999
58769bee 12000 /* X11R3: set the initial_state field of the window manager hints to
16bd92ea
JB
12001 IconicState. */
12002 x_wm_set_window_state (f, IconicState);
dc6f92b8 12003
a9c00105
RS
12004 if (!FRAME_VISIBLE_P (f))
12005 {
12006 /* If the frame was withdrawn, before, we must map it. */
7f9c7f94 12007 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
a9c00105
RS
12008 }
12009
3a88c238 12010 f->async_iconified = 1;
1e6bc770 12011 f->async_visible = 0;
dc6f92b8 12012
334208b7 12013 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 12014 UNBLOCK_INPUT;
8c002a25 12015#endif /* not USE_X_TOOLKIT */
dc6f92b8 12016}
d047c4eb 12017\f
c0ff3fab 12018/* Destroy the X window of frame F. */
dc6f92b8 12019
dfcf069d 12020void
c0ff3fab 12021x_destroy_window (f)
f676886a 12022 struct frame *f;
dc6f92b8 12023{
7f9c7f94
RS
12024 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12025
dc6f92b8 12026 BLOCK_INPUT;
c0ff3fab 12027
6186a4a0
RS
12028 /* If a display connection is dead, don't try sending more
12029 commands to the X server. */
12030 if (dpyinfo->display != 0)
12031 {
12032 if (f->output_data.x->icon_desc != 0)
12033 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
31f41daf 12034#ifdef HAVE_X_I18N
f5d11644
GM
12035 if (FRAME_XIC (f))
12036 free_frame_xic (f);
31f41daf 12037#endif
6186a4a0 12038 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->window_desc);
3afe33e7 12039#ifdef USE_X_TOOLKIT
06a2c219
GM
12040 if (f->output_data.x->widget)
12041 XtDestroyWidget (f->output_data.x->widget);
6186a4a0 12042 free_frame_menubar (f);
3afe33e7
RS
12043#endif /* USE_X_TOOLKIT */
12044
3e71d8f2
GM
12045 unload_color (f, f->output_data.x->foreground_pixel);
12046 unload_color (f, f->output_data.x->background_pixel);
12047 unload_color (f, f->output_data.x->cursor_pixel);
12048 unload_color (f, f->output_data.x->cursor_foreground_pixel);
12049 unload_color (f, f->output_data.x->border_pixel);
12050 unload_color (f, f->output_data.x->mouse_pixel);
12051 if (f->output_data.x->scroll_bar_background_pixel != -1)
12052 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
12053 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
12054 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
12055 if (f->output_data.x->white_relief.allocated_p)
12056 unload_color (f, f->output_data.x->white_relief.pixel);
12057 if (f->output_data.x->black_relief.allocated_p)
12058 unload_color (f, f->output_data.x->black_relief.pixel);
12059
6186a4a0
RS
12060 free_frame_faces (f);
12061 XFlush (FRAME_X_DISPLAY (f));
12062 }
dc6f92b8 12063
df89d8a4 12064 if (f->output_data.x->saved_menu_event)
06a2c219 12065 xfree (f->output_data.x->saved_menu_event);
0c49ce2f 12066
7556890b
RS
12067 xfree (f->output_data.x);
12068 f->output_data.x = 0;
0f941935
KH
12069 if (f == dpyinfo->x_focus_frame)
12070 dpyinfo->x_focus_frame = 0;
12071 if (f == dpyinfo->x_focus_event_frame)
12072 dpyinfo->x_focus_event_frame = 0;
12073 if (f == dpyinfo->x_highlight_frame)
12074 dpyinfo->x_highlight_frame = 0;
c0ff3fab 12075
7f9c7f94
RS
12076 dpyinfo->reference_count--;
12077
12078 if (f == dpyinfo->mouse_face_mouse_frame)
dc05a16b 12079 {
7f9c7f94
RS
12080 dpyinfo->mouse_face_beg_row
12081 = dpyinfo->mouse_face_beg_col = -1;
12082 dpyinfo->mouse_face_end_row
12083 = dpyinfo->mouse_face_end_col = -1;
12084 dpyinfo->mouse_face_window = Qnil;
21323706
RS
12085 dpyinfo->mouse_face_deferred_gc = 0;
12086 dpyinfo->mouse_face_mouse_frame = 0;
dc05a16b 12087 }
0134a210 12088
c0ff3fab 12089 UNBLOCK_INPUT;
dc6f92b8
JB
12090}
12091\f
f451eb13
JB
12092/* Setting window manager hints. */
12093
af31d76f
RS
12094/* Set the normal size hints for the window manager, for frame F.
12095 FLAGS is the flags word to use--or 0 meaning preserve the flags
12096 that the window now has.
12097 If USER_POSITION is nonzero, we set the USPosition
12098 flag (this is useful when FLAGS is 0). */
6dba1858 12099
dfcf069d 12100void
af31d76f 12101x_wm_set_size_hint (f, flags, user_position)
f676886a 12102 struct frame *f;
af31d76f
RS
12103 long flags;
12104 int user_position;
dc6f92b8
JB
12105{
12106 XSizeHints size_hints;
3afe33e7
RS
12107
12108#ifdef USE_X_TOOLKIT
7e4f2521
FP
12109 Arg al[2];
12110 int ac = 0;
12111 Dimension widget_width, widget_height;
7556890b 12112 Window window = XtWindow (f->output_data.x->widget);
3afe33e7 12113#else /* not USE_X_TOOLKIT */
c118dd06 12114 Window window = FRAME_X_WINDOW (f);
3afe33e7 12115#endif /* not USE_X_TOOLKIT */
dc6f92b8 12116
b72a58fd
RS
12117 /* Setting PMaxSize caused various problems. */
12118 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
dc6f92b8 12119
7556890b
RS
12120 size_hints.x = f->output_data.x->left_pos;
12121 size_hints.y = f->output_data.x->top_pos;
7553a6b7 12122
7e4f2521
FP
12123#ifdef USE_X_TOOLKIT
12124 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
12125 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
7556890b 12126 XtGetValues (f->output_data.x->widget, al, ac);
7e4f2521
FP
12127 size_hints.height = widget_height;
12128 size_hints.width = widget_width;
12129#else /* not USE_X_TOOLKIT */
f676886a
JB
12130 size_hints.height = PIXEL_HEIGHT (f);
12131 size_hints.width = PIXEL_WIDTH (f);
7e4f2521 12132#endif /* not USE_X_TOOLKIT */
7553a6b7 12133
7556890b
RS
12134 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
12135 size_hints.height_inc = f->output_data.x->line_height;
334208b7
RS
12136 size_hints.max_width
12137 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
12138 size_hints.max_height
12139 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
0134a210 12140
d067ea8b
KH
12141 /* Calculate the base and minimum sizes.
12142
12143 (When we use the X toolkit, we don't do it here.
12144 Instead we copy the values that the widgets are using, below.) */
12145#ifndef USE_X_TOOLKIT
b1c884c3 12146 {
b0342f17 12147 int base_width, base_height;
0134a210 12148 int min_rows = 0, min_cols = 0;
b0342f17 12149
f451eb13
JB
12150 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
12151 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
b0342f17 12152
0134a210 12153 check_frame_size (f, &min_rows, &min_cols);
b0342f17 12154
0134a210
RS
12155 /* The window manager uses the base width hints to calculate the
12156 current number of rows and columns in the frame while
12157 resizing; min_width and min_height aren't useful for this
12158 purpose, since they might not give the dimensions for a
12159 zero-row, zero-column frame.
58769bee 12160
0134a210
RS
12161 We use the base_width and base_height members if we have
12162 them; otherwise, we set the min_width and min_height members
12163 to the size for a zero x zero frame. */
b0342f17
JB
12164
12165#ifdef HAVE_X11R4
0134a210
RS
12166 size_hints.flags |= PBaseSize;
12167 size_hints.base_width = base_width;
12168 size_hints.base_height = base_height;
12169 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
12170 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
b0342f17 12171#else
0134a210
RS
12172 size_hints.min_width = base_width;
12173 size_hints.min_height = base_height;
b0342f17 12174#endif
b1c884c3 12175 }
dc6f92b8 12176
d067ea8b 12177 /* If we don't need the old flags, we don't need the old hint at all. */
af31d76f 12178 if (flags)
dc6f92b8 12179 {
d067ea8b
KH
12180 size_hints.flags |= flags;
12181 goto no_read;
12182 }
12183#endif /* not USE_X_TOOLKIT */
12184
12185 {
12186 XSizeHints hints; /* Sometimes I hate X Windows... */
12187 long supplied_return;
12188 int value;
af31d76f
RS
12189
12190#ifdef HAVE_X11R4
d067ea8b
KH
12191 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
12192 &supplied_return);
af31d76f 12193#else
d067ea8b 12194 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
af31d76f 12195#endif
58769bee 12196
d067ea8b
KH
12197#ifdef USE_X_TOOLKIT
12198 size_hints.base_height = hints.base_height;
12199 size_hints.base_width = hints.base_width;
12200 size_hints.min_height = hints.min_height;
12201 size_hints.min_width = hints.min_width;
12202#endif
12203
12204 if (flags)
12205 size_hints.flags |= flags;
12206 else
12207 {
12208 if (value == 0)
12209 hints.flags = 0;
12210 if (hints.flags & PSize)
12211 size_hints.flags |= PSize;
12212 if (hints.flags & PPosition)
12213 size_hints.flags |= PPosition;
12214 if (hints.flags & USPosition)
12215 size_hints.flags |= USPosition;
12216 if (hints.flags & USSize)
12217 size_hints.flags |= USSize;
12218 }
12219 }
12220
06a2c219 12221#ifndef USE_X_TOOLKIT
d067ea8b 12222 no_read:
06a2c219 12223#endif
0134a210 12224
af31d76f 12225#ifdef PWinGravity
7556890b 12226 size_hints.win_gravity = f->output_data.x->win_gravity;
af31d76f 12227 size_hints.flags |= PWinGravity;
dc05a16b 12228
af31d76f 12229 if (user_position)
6dba1858 12230 {
af31d76f
RS
12231 size_hints.flags &= ~ PPosition;
12232 size_hints.flags |= USPosition;
6dba1858 12233 }
2554751d 12234#endif /* PWinGravity */
6dba1858 12235
b0342f17 12236#ifdef HAVE_X11R4
334208b7 12237 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 12238#else
334208b7 12239 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 12240#endif
dc6f92b8
JB
12241}
12242
12243/* Used for IconicState or NormalState */
06a2c219 12244
dfcf069d 12245void
f676886a
JB
12246x_wm_set_window_state (f, state)
12247 struct frame *f;
dc6f92b8
JB
12248 int state;
12249{
3afe33e7 12250#ifdef USE_X_TOOLKIT
546e6d5b
RS
12251 Arg al[1];
12252
12253 XtSetArg (al[0], XtNinitialState, state);
7556890b 12254 XtSetValues (f->output_data.x->widget, al, 1);
3afe33e7 12255#else /* not USE_X_TOOLKIT */
c118dd06 12256 Window window = FRAME_X_WINDOW (f);
dc6f92b8 12257
7556890b
RS
12258 f->output_data.x->wm_hints.flags |= StateHint;
12259 f->output_data.x->wm_hints.initial_state = state;
b1c884c3 12260
7556890b 12261 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
546e6d5b 12262#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12263}
12264
dfcf069d 12265void
7f2ae036 12266x_wm_set_icon_pixmap (f, pixmap_id)
f676886a 12267 struct frame *f;
7f2ae036 12268 int pixmap_id;
dc6f92b8 12269{
d2bd6bc4
RS
12270 Pixmap icon_pixmap;
12271
06a2c219 12272#ifndef USE_X_TOOLKIT
c118dd06 12273 Window window = FRAME_X_WINDOW (f);
75231bad 12274#endif
dc6f92b8 12275
7f2ae036 12276 if (pixmap_id > 0)
dbc4e1c1 12277 {
d2bd6bc4 12278 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
7556890b 12279 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
dbc4e1c1
JB
12280 }
12281 else
68568555
RS
12282 {
12283 /* It seems there is no way to turn off use of an icon pixmap.
12284 The following line does it, only if no icon has yet been created,
12285 for some window managers. But with mwm it crashes.
12286 Some people say it should clear the IconPixmapHint bit in this case,
12287 but that doesn't work, and the X consortium said it isn't the
12288 right thing at all. Since there is no way to win,
12289 best to explicitly give up. */
12290#if 0
12291 f->output_data.x->wm_hints.icon_pixmap = None;
12292#else
12293 return;
12294#endif
12295 }
b1c884c3 12296
d2bd6bc4
RS
12297#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
12298
12299 {
12300 Arg al[1];
12301 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12302 XtSetValues (f->output_data.x->widget, al, 1);
12303 }
12304
12305#else /* not USE_X_TOOLKIT */
12306
7556890b
RS
12307 f->output_data.x->wm_hints.flags |= IconPixmapHint;
12308 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
d2bd6bc4
RS
12309
12310#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12311}
12312
dfcf069d 12313void
f676886a
JB
12314x_wm_set_icon_position (f, icon_x, icon_y)
12315 struct frame *f;
dc6f92b8
JB
12316 int icon_x, icon_y;
12317{
75231bad 12318#ifdef USE_X_TOOLKIT
7556890b 12319 Window window = XtWindow (f->output_data.x->widget);
75231bad 12320#else
c118dd06 12321 Window window = FRAME_X_WINDOW (f);
75231bad 12322#endif
dc6f92b8 12323
7556890b
RS
12324 f->output_data.x->wm_hints.flags |= IconPositionHint;
12325 f->output_data.x->wm_hints.icon_x = icon_x;
12326 f->output_data.x->wm_hints.icon_y = icon_y;
b1c884c3 12327
7556890b 12328 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
dc6f92b8
JB
12329}
12330
12331\f
06a2c219
GM
12332/***********************************************************************
12333 Fonts
12334 ***********************************************************************/
dc43ef94
KH
12335
12336/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
06a2c219 12337
dc43ef94
KH
12338struct font_info *
12339x_get_font_info (f, font_idx)
12340 FRAME_PTR f;
12341 int font_idx;
12342{
12343 return (FRAME_X_FONT_TABLE (f) + font_idx);
12344}
12345
12346
12347/* Return a list of names of available fonts matching PATTERN on frame
12348 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
12349 to be listed. Frame F NULL means we have not yet created any
12350 frame on X, and consult the first display in x_display_list.
12351 MAXNAMES sets a limit on how many fonts to match. */
12352
12353Lisp_Object
12354x_list_fonts (f, pattern, size, maxnames)
12355 FRAME_PTR f;
12356 Lisp_Object pattern;
12357 int size;
12358 int maxnames;
12359{
06a2c219
GM
12360 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
12361 Lisp_Object tem, second_best;
dc43ef94 12362 Display *dpy = f != NULL ? FRAME_X_DISPLAY (f) : x_display_list->display;
09c6077f 12363 int try_XLoadQueryFont = 0;
53ca4657 12364 int count;
dc43ef94 12365
6b0efe73 12366 patterns = Fassoc (pattern, Valternate_fontname_alist);
2da424f1
KH
12367 if (NILP (patterns))
12368 patterns = Fcons (pattern, Qnil);
81ba44e5 12369
09c6077f
KH
12370 if (maxnames == 1 && !size)
12371 /* We can return any single font matching PATTERN. */
12372 try_XLoadQueryFont = 1;
9a32686f 12373
8e713be6 12374 for (; CONSP (patterns); patterns = XCDR (patterns))
dc43ef94 12375 {
dc43ef94 12376 int num_fonts;
3e71d8f2 12377 char **names = NULL;
dc43ef94 12378
8e713be6 12379 pattern = XCAR (patterns);
536f4067
RS
12380 /* See if we cached the result for this particular query.
12381 The cache is an alist of the form:
12382 (((PATTERN . MAXNAMES) (FONTNAME . WIDTH) ...) ...)
12383 */
8e713be6 12384 if (f && (tem = XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element),
b5210ea7
KH
12385 key = Fcons (pattern, make_number (maxnames)),
12386 !NILP (list = Fassoc (key, tem))))
12387 {
12388 list = Fcdr_safe (list);
12389 /* We have a cashed list. Don't have to get the list again. */
12390 goto label_cached;
12391 }
12392
12393 /* At first, put PATTERN in the cache. */
09c6077f 12394
dc43ef94 12395 BLOCK_INPUT;
17d85edc
KH
12396 count = x_catch_errors (dpy);
12397
09c6077f
KH
12398 if (try_XLoadQueryFont)
12399 {
12400 XFontStruct *font;
12401 unsigned long value;
12402
12403 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
17d85edc
KH
12404 if (x_had_errors_p (dpy))
12405 {
12406 /* This error is perhaps due to insufficient memory on X
12407 server. Let's just ignore it. */
12408 font = NULL;
12409 x_clear_errors (dpy);
12410 }
12411
09c6077f
KH
12412 if (font
12413 && XGetFontProperty (font, XA_FONT, &value))
12414 {
12415 char *name = (char *) XGetAtomName (dpy, (Atom) value);
12416 int len = strlen (name);
01c752b5 12417 char *tmp;
09c6077f 12418
6f6512e8
KH
12419 /* If DXPC (a Differential X Protocol Compressor)
12420 Ver.3.7 is running, XGetAtomName will return null
12421 string. We must avoid such a name. */
12422 if (len == 0)
12423 try_XLoadQueryFont = 0;
12424 else
12425 {
12426 num_fonts = 1;
12427 names = (char **) alloca (sizeof (char *));
12428 /* Some systems only allow alloca assigned to a
12429 simple var. */
12430 tmp = (char *) alloca (len + 1); names[0] = tmp;
12431 bcopy (name, names[0], len + 1);
12432 XFree (name);
12433 }
09c6077f
KH
12434 }
12435 else
12436 try_XLoadQueryFont = 0;
a083fd23
RS
12437
12438 if (font)
12439 XFreeFont (dpy, font);
09c6077f
KH
12440 }
12441
12442 if (!try_XLoadQueryFont)
17d85edc
KH
12443 {
12444 /* We try at least 10 fonts because XListFonts will return
12445 auto-scaled fonts at the head. */
12446 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
12447 &num_fonts);
12448 if (x_had_errors_p (dpy))
12449 {
12450 /* This error is perhaps due to insufficient memory on X
12451 server. Let's just ignore it. */
12452 names = NULL;
12453 x_clear_errors (dpy);
12454 }
12455 }
12456
12457 x_uncatch_errors (dpy, count);
dc43ef94
KH
12458 UNBLOCK_INPUT;
12459
12460 if (names)
12461 {
12462 int i;
dc43ef94
KH
12463
12464 /* Make a list of all the fonts we got back.
12465 Store that in the font cache for the display. */
12466 for (i = 0; i < num_fonts; i++)
12467 {
06a2c219 12468 int width = 0;
dc43ef94 12469 char *p = names[i];
06a2c219
GM
12470 int average_width = -1, dashes = 0;
12471
dc43ef94 12472 /* Count the number of dashes in NAMES[I]. If there are
b5210ea7
KH
12473 14 dashes, and the field value following 12th dash
12474 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
12475 is usually too ugly to be used for editing. Let's
12476 ignore it. */
dc43ef94
KH
12477 while (*p)
12478 if (*p++ == '-')
12479 {
12480 dashes++;
12481 if (dashes == 7) /* PIXEL_SIZE field */
12482 width = atoi (p);
12483 else if (dashes == 12) /* AVERAGE_WIDTH field */
12484 average_width = atoi (p);
12485 }
12486 if (dashes < 14 || average_width != 0)
12487 {
12488 tem = build_string (names[i]);
12489 if (NILP (Fassoc (tem, list)))
12490 {
12491 if (STRINGP (Vx_pixel_size_width_font_regexp)
f39db7ea
RS
12492 && ((fast_c_string_match_ignore_case
12493 (Vx_pixel_size_width_font_regexp, names[i]))
dc43ef94
KH
12494 >= 0))
12495 /* We can set the value of PIXEL_SIZE to the
b5210ea7 12496 width of this font. */
dc43ef94
KH
12497 list = Fcons (Fcons (tem, make_number (width)), list);
12498 else
12499 /* For the moment, width is not known. */
12500 list = Fcons (Fcons (tem, Qnil), list);
12501 }
12502 }
12503 }
09c6077f
KH
12504 if (!try_XLoadQueryFont)
12505 XFreeFontNames (names);
dc43ef94
KH
12506 }
12507
b5210ea7 12508 /* Now store the result in the cache. */
dc43ef94 12509 if (f != NULL)
8e713be6 12510 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element)
dc43ef94 12511 = Fcons (Fcons (key, list),
8e713be6 12512 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
dc43ef94 12513
b5210ea7
KH
12514 label_cached:
12515 if (NILP (list)) continue; /* Try the remaining alternatives. */
dc43ef94 12516
b5210ea7
KH
12517 newlist = second_best = Qnil;
12518 /* Make a list of the fonts that have the right width. */
8e713be6 12519 for (; CONSP (list); list = XCDR (list))
b5210ea7 12520 {
536f4067
RS
12521 int found_size;
12522
8e713be6 12523 tem = XCAR (list);
dc43ef94 12524
8e713be6 12525 if (!CONSP (tem) || NILP (XCAR (tem)))
b5210ea7
KH
12526 continue;
12527 if (!size)
12528 {
8e713be6 12529 newlist = Fcons (XCAR (tem), newlist);
b5210ea7
KH
12530 continue;
12531 }
dc43ef94 12532
8e713be6 12533 if (!INTEGERP (XCDR (tem)))
dc43ef94 12534 {
b5210ea7
KH
12535 /* Since we have not yet known the size of this font, we
12536 must try slow function call XLoadQueryFont. */
dc43ef94
KH
12537 XFontStruct *thisinfo;
12538
12539 BLOCK_INPUT;
17d85edc 12540 count = x_catch_errors (dpy);
dc43ef94 12541 thisinfo = XLoadQueryFont (dpy,
8e713be6 12542 XSTRING (XCAR (tem))->data);
17d85edc
KH
12543 if (x_had_errors_p (dpy))
12544 {
12545 /* This error is perhaps due to insufficient memory on X
12546 server. Let's just ignore it. */
12547 thisinfo = NULL;
12548 x_clear_errors (dpy);
12549 }
12550 x_uncatch_errors (dpy, count);
dc43ef94
KH
12551 UNBLOCK_INPUT;
12552
12553 if (thisinfo)
12554 {
8e713be6 12555 XCDR (tem)
536f4067
RS
12556 = (thisinfo->min_bounds.width == 0
12557 ? make_number (0)
12558 : make_number (thisinfo->max_bounds.width));
dc43ef94
KH
12559 XFreeFont (dpy, thisinfo);
12560 }
12561 else
b5210ea7 12562 /* For unknown reason, the previous call of XListFont had
06a2c219 12563 returned a font which can't be opened. Record the size
b5210ea7 12564 as 0 not to try to open it again. */
8e713be6 12565 XCDR (tem) = make_number (0);
dc43ef94 12566 }
536f4067 12567
8e713be6 12568 found_size = XINT (XCDR (tem));
536f4067 12569 if (found_size == size)
8e713be6 12570 newlist = Fcons (XCAR (tem), newlist);
536f4067 12571 else if (found_size > 0)
b5210ea7 12572 {
536f4067 12573 if (NILP (second_best))
b5210ea7 12574 second_best = tem;
536f4067
RS
12575 else if (found_size < size)
12576 {
8e713be6
KR
12577 if (XINT (XCDR (second_best)) > size
12578 || XINT (XCDR (second_best)) < found_size)
536f4067
RS
12579 second_best = tem;
12580 }
12581 else
12582 {
8e713be6
KR
12583 if (XINT (XCDR (second_best)) > size
12584 && XINT (XCDR (second_best)) > found_size)
536f4067
RS
12585 second_best = tem;
12586 }
b5210ea7
KH
12587 }
12588 }
12589 if (!NILP (newlist))
12590 break;
12591 else if (!NILP (second_best))
12592 {
8e713be6 12593 newlist = Fcons (XCAR (second_best), Qnil);
b5210ea7 12594 break;
dc43ef94 12595 }
dc43ef94
KH
12596 }
12597
12598 return newlist;
12599}
12600
06a2c219
GM
12601
12602#if GLYPH_DEBUG
12603
12604/* Check that FONT is valid on frame F. It is if it can be found in F's
12605 font table. */
12606
12607static void
12608x_check_font (f, font)
12609 struct frame *f;
12610 XFontStruct *font;
12611{
12612 int i;
12613 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12614
12615 xassert (font != NULL);
12616
12617 for (i = 0; i < dpyinfo->n_fonts; i++)
12618 if (dpyinfo->font_table[i].name
12619 && font == dpyinfo->font_table[i].font)
12620 break;
12621
12622 xassert (i < dpyinfo->n_fonts);
12623}
12624
12625#endif /* GLYPH_DEBUG != 0 */
12626
12627/* Set *W to the minimum width, *H to the minimum font height of FONT.
12628 Note: There are (broken) X fonts out there with invalid XFontStruct
12629 min_bounds contents. For example, handa@etl.go.jp reports that
12630 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
12631 have font->min_bounds.width == 0. */
12632
12633static INLINE void
12634x_font_min_bounds (font, w, h)
12635 XFontStruct *font;
12636 int *w, *h;
12637{
12638 *h = FONT_HEIGHT (font);
12639 *w = font->min_bounds.width;
12640
12641 /* Try to handle the case where FONT->min_bounds has invalid
12642 contents. Since the only font known to have invalid min_bounds
12643 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
12644 if (*w <= 0)
12645 *w = font->max_bounds.width;
12646}
12647
12648
12649/* Compute the smallest character width and smallest font height over
12650 all fonts available on frame F. Set the members smallest_char_width
12651 and smallest_font_height in F's x_display_info structure to
12652 the values computed. Value is non-zero if smallest_font_height or
12653 smallest_char_width become smaller than they were before. */
12654
12655static int
12656x_compute_min_glyph_bounds (f)
12657 struct frame *f;
12658{
12659 int i;
12660 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12661 XFontStruct *font;
12662 int old_width = dpyinfo->smallest_char_width;
12663 int old_height = dpyinfo->smallest_font_height;
12664
12665 dpyinfo->smallest_font_height = 100000;
12666 dpyinfo->smallest_char_width = 100000;
12667
12668 for (i = 0; i < dpyinfo->n_fonts; ++i)
12669 if (dpyinfo->font_table[i].name)
12670 {
12671 struct font_info *fontp = dpyinfo->font_table + i;
12672 int w, h;
12673
12674 font = (XFontStruct *) fontp->font;
12675 xassert (font != (XFontStruct *) ~0);
12676 x_font_min_bounds (font, &w, &h);
12677
12678 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
12679 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
12680 }
12681
12682 xassert (dpyinfo->smallest_char_width > 0
12683 && dpyinfo->smallest_font_height > 0);
12684
12685 return (dpyinfo->n_fonts == 1
12686 || dpyinfo->smallest_char_width < old_width
12687 || dpyinfo->smallest_font_height < old_height);
12688}
12689
12690
dc43ef94
KH
12691/* Load font named FONTNAME of the size SIZE for frame F, and return a
12692 pointer to the structure font_info while allocating it dynamically.
12693 If SIZE is 0, load any size of font.
12694 If loading is failed, return NULL. */
12695
12696struct font_info *
12697x_load_font (f, fontname, size)
12698 struct frame *f;
12699 register char *fontname;
12700 int size;
12701{
12702 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12703 Lisp_Object font_names;
d645aaa4 12704 int count;
dc43ef94
KH
12705
12706 /* Get a list of all the fonts that match this name. Once we
12707 have a list of matching fonts, we compare them against the fonts
12708 we already have by comparing names. */
09c6077f 12709 font_names = x_list_fonts (f, build_string (fontname), size, 1);
dc43ef94
KH
12710
12711 if (!NILP (font_names))
12712 {
12713 Lisp_Object tail;
12714 int i;
12715
12716 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 12717 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
06a2c219
GM
12718 if (dpyinfo->font_table[i].name
12719 && (!strcmp (dpyinfo->font_table[i].name,
8e713be6 12720 XSTRING (XCAR (tail))->data)
06a2c219 12721 || !strcmp (dpyinfo->font_table[i].full_name,
8e713be6 12722 XSTRING (XCAR (tail))->data)))
dc43ef94
KH
12723 return (dpyinfo->font_table + i);
12724 }
12725
12726 /* Load the font and add it to the table. */
12727 {
12728 char *full_name;
12729 XFontStruct *font;
12730 struct font_info *fontp;
12731 unsigned long value;
06a2c219 12732 int i;
dc43ef94 12733
2da424f1
KH
12734 /* If we have found fonts by x_list_font, load one of them. If
12735 not, we still try to load a font by the name given as FONTNAME
12736 because XListFonts (called in x_list_font) of some X server has
12737 a bug of not finding a font even if the font surely exists and
12738 is loadable by XLoadQueryFont. */
e1d6d5b9 12739 if (size > 0 && !NILP (font_names))
8e713be6 12740 fontname = (char *) XSTRING (XCAR (font_names))->data;
dc43ef94
KH
12741
12742 BLOCK_INPUT;
d645aaa4 12743 count = x_catch_errors (FRAME_X_DISPLAY (f));
dc43ef94 12744 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
d645aaa4
KH
12745 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12746 {
12747 /* This error is perhaps due to insufficient memory on X
12748 server. Let's just ignore it. */
12749 font = NULL;
12750 x_clear_errors (FRAME_X_DISPLAY (f));
12751 }
12752 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
dc43ef94 12753 UNBLOCK_INPUT;
b5210ea7 12754 if (!font)
dc43ef94
KH
12755 return NULL;
12756
06a2c219
GM
12757 /* Find a free slot in the font table. */
12758 for (i = 0; i < dpyinfo->n_fonts; ++i)
12759 if (dpyinfo->font_table[i].name == NULL)
12760 break;
12761
12762 /* If no free slot found, maybe enlarge the font table. */
12763 if (i == dpyinfo->n_fonts
12764 && dpyinfo->n_fonts == dpyinfo->font_table_size)
dc43ef94 12765 {
06a2c219
GM
12766 int sz;
12767 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
12768 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
dc43ef94 12769 dpyinfo->font_table
06a2c219 12770 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
dc43ef94
KH
12771 }
12772
06a2c219
GM
12773 fontp = dpyinfo->font_table + i;
12774 if (i == dpyinfo->n_fonts)
12775 ++dpyinfo->n_fonts;
dc43ef94
KH
12776
12777 /* Now fill in the slots of *FONTP. */
12778 BLOCK_INPUT;
12779 fontp->font = font;
06a2c219 12780 fontp->font_idx = i;
dc43ef94
KH
12781 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
12782 bcopy (fontname, fontp->name, strlen (fontname) + 1);
12783
12784 /* Try to get the full name of FONT. Put it in FULL_NAME. */
12785 full_name = 0;
12786 if (XGetFontProperty (font, XA_FONT, &value))
12787 {
12788 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
12789 char *p = name;
12790 int dashes = 0;
12791
12792 /* Count the number of dashes in the "full name".
12793 If it is too few, this isn't really the font's full name,
12794 so don't use it.
12795 In X11R4, the fonts did not come with their canonical names
12796 stored in them. */
12797 while (*p)
12798 {
12799 if (*p == '-')
12800 dashes++;
12801 p++;
12802 }
12803
12804 if (dashes >= 13)
12805 {
12806 full_name = (char *) xmalloc (p - name + 1);
12807 bcopy (name, full_name, p - name + 1);
12808 }
12809
12810 XFree (name);
12811 }
12812
12813 if (full_name != 0)
12814 fontp->full_name = full_name;
12815 else
12816 fontp->full_name = fontp->name;
12817
12818 fontp->size = font->max_bounds.width;
d5749adb
KH
12819 fontp->height = FONT_HEIGHT (font);
12820 {
12821 /* For some font, ascent and descent in max_bounds field is
12822 larger than the above value. */
12823 int max_height = font->max_bounds.ascent + font->max_bounds.descent;
12824 if (max_height > fontp->height)
74848a96 12825 fontp->height = max_height;
d5749adb 12826 }
dc43ef94 12827
2da424f1
KH
12828 if (NILP (font_names))
12829 {
12830 /* We come here because of a bug of XListFonts mentioned at
12831 the head of this block. Let's store this information in
12832 the cache for x_list_fonts. */
12833 Lisp_Object lispy_name = build_string (fontname);
12834 Lisp_Object lispy_full_name = build_string (fontp->full_name);
12835
8e713be6 12836 XCDR (dpyinfo->name_list_element)
2da424f1
KH
12837 = Fcons (Fcons (Fcons (lispy_name, make_number (256)),
12838 Fcons (Fcons (lispy_full_name,
12839 make_number (fontp->size)),
12840 Qnil)),
8e713be6 12841 XCDR (dpyinfo->name_list_element));
2da424f1 12842 if (full_name)
8e713be6 12843 XCDR (dpyinfo->name_list_element)
2da424f1
KH
12844 = Fcons (Fcons (Fcons (lispy_full_name, make_number (256)),
12845 Fcons (Fcons (lispy_full_name,
12846 make_number (fontp->size)),
12847 Qnil)),
8e713be6 12848 XCDR (dpyinfo->name_list_element));
2da424f1
KH
12849 }
12850
dc43ef94
KH
12851 /* The slot `encoding' specifies how to map a character
12852 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
ee569018
KH
12853 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
12854 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
8ff102bd 12855 2:0xA020..0xFF7F). For the moment, we don't know which charset
06a2c219 12856 uses this font. So, we set information in fontp->encoding[1]
8ff102bd
RS
12857 which is never used by any charset. If mapping can't be
12858 decided, set FONT_ENCODING_NOT_DECIDED. */
dc43ef94
KH
12859 fontp->encoding[1]
12860 = (font->max_byte1 == 0
12861 /* 1-byte font */
12862 ? (font->min_char_or_byte2 < 0x80
12863 ? (font->max_char_or_byte2 < 0x80
12864 ? 0 /* 0x20..0x7F */
8ff102bd 12865 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
dc43ef94
KH
12866 : 1) /* 0xA0..0xFF */
12867 /* 2-byte font */
12868 : (font->min_byte1 < 0x80
12869 ? (font->max_byte1 < 0x80
12870 ? (font->min_char_or_byte2 < 0x80
12871 ? (font->max_char_or_byte2 < 0x80
12872 ? 0 /* 0x2020..0x7F7F */
8ff102bd 12873 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
dc43ef94 12874 : 3) /* 0x20A0..0x7FFF */
8ff102bd 12875 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
dc43ef94
KH
12876 : (font->min_char_or_byte2 < 0x80
12877 ? (font->max_char_or_byte2 < 0x80
12878 ? 2 /* 0xA020..0xFF7F */
8ff102bd 12879 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
dc43ef94
KH
12880 : 1))); /* 0xA0A0..0xFFFF */
12881
12882 fontp->baseline_offset
12883 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
12884 ? (long) value : 0);
12885 fontp->relative_compose
12886 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
12887 ? (long) value : 0);
f78798df
KH
12888 fontp->default_ascent
12889 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
12890 ? (long) value : 0);
dc43ef94 12891
06a2c219
GM
12892 /* Set global flag fonts_changed_p to non-zero if the font loaded
12893 has a character with a smaller width than any other character
12894 before, or if the font loaded has a smalle>r height than any
12895 other font loaded before. If this happens, it will make a
12896 glyph matrix reallocation necessary. */
12897 fonts_changed_p = x_compute_min_glyph_bounds (f);
dc43ef94 12898 UNBLOCK_INPUT;
dc43ef94
KH
12899 return fontp;
12900 }
12901}
12902
06a2c219
GM
12903
12904/* Return a pointer to struct font_info of a font named FONTNAME for
12905 frame F. If no such font is loaded, return NULL. */
12906
dc43ef94
KH
12907struct font_info *
12908x_query_font (f, fontname)
12909 struct frame *f;
12910 register char *fontname;
12911{
12912 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12913 int i;
12914
12915 for (i = 0; i < dpyinfo->n_fonts; i++)
06a2c219
GM
12916 if (dpyinfo->font_table[i].name
12917 && (!strcmp (dpyinfo->font_table[i].name, fontname)
12918 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
dc43ef94
KH
12919 return (dpyinfo->font_table + i);
12920 return NULL;
12921}
12922
06a2c219
GM
12923
12924/* Find a CCL program for a font specified by FONTP, and set the member
a6582676
KH
12925 `encoder' of the structure. */
12926
12927void
12928x_find_ccl_program (fontp)
12929 struct font_info *fontp;
12930{
a42f54e6 12931 Lisp_Object list, elt;
a6582676 12932
8e713be6 12933 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
a6582676 12934 {
8e713be6 12935 elt = XCAR (list);
a6582676 12936 if (CONSP (elt)
8e713be6
KR
12937 && STRINGP (XCAR (elt))
12938 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
a6582676 12939 >= 0))
a42f54e6
KH
12940 break;
12941 }
12942 if (! NILP (list))
12943 {
d27f8ca7
KH
12944 struct ccl_program *ccl
12945 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
a42f54e6 12946
8e713be6 12947 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
a42f54e6
KH
12948 xfree (ccl);
12949 else
12950 fontp->font_encoder = ccl;
a6582676
KH
12951 }
12952}
12953
06a2c219 12954
dc43ef94 12955\f
06a2c219
GM
12956/***********************************************************************
12957 Initialization
12958 ***********************************************************************/
f451eb13 12959
3afe33e7
RS
12960#ifdef USE_X_TOOLKIT
12961static XrmOptionDescRec emacs_options[] = {
12962 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
12963 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
12964
12965 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
12966 XrmoptionSepArg, NULL},
12967 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
12968
12969 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12970 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12971 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12972 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12973 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12974 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
12975 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
12976};
12977#endif /* USE_X_TOOLKIT */
12978
7a13e894
RS
12979static int x_initialized;
12980
29b38361
KH
12981#ifdef MULTI_KBOARD
12982/* Test whether two display-name strings agree up to the dot that separates
12983 the screen number from the server number. */
12984static int
12985same_x_server (name1, name2)
12986 char *name1, *name2;
12987{
12988 int seen_colon = 0;
cf591cc1
RS
12989 unsigned char *system_name = XSTRING (Vsystem_name)->data;
12990 int system_name_length = strlen (system_name);
12991 int length_until_period = 0;
12992
12993 while (system_name[length_until_period] != 0
12994 && system_name[length_until_period] != '.')
12995 length_until_period++;
12996
12997 /* Treat `unix' like an empty host name. */
12998 if (! strncmp (name1, "unix:", 5))
12999 name1 += 4;
13000 if (! strncmp (name2, "unix:", 5))
13001 name2 += 4;
13002 /* Treat this host's name like an empty host name. */
13003 if (! strncmp (name1, system_name, system_name_length)
13004 && name1[system_name_length] == ':')
13005 name1 += system_name_length;
13006 if (! strncmp (name2, system_name, system_name_length)
13007 && name2[system_name_length] == ':')
13008 name2 += system_name_length;
13009 /* Treat this host's domainless name like an empty host name. */
13010 if (! strncmp (name1, system_name, length_until_period)
13011 && name1[length_until_period] == ':')
13012 name1 += length_until_period;
13013 if (! strncmp (name2, system_name, length_until_period)
13014 && name2[length_until_period] == ':')
13015 name2 += length_until_period;
13016
29b38361
KH
13017 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
13018 {
13019 if (*name1 == ':')
13020 seen_colon++;
13021 if (seen_colon && *name1 == '.')
13022 return 1;
13023 }
13024 return (seen_colon
13025 && (*name1 == '.' || *name1 == '\0')
13026 && (*name2 == '.' || *name2 == '\0'));
13027}
13028#endif
13029
334208b7 13030struct x_display_info *
1f8255f2 13031x_term_init (display_name, xrm_option, resource_name)
334208b7 13032 Lisp_Object display_name;
1f8255f2
RS
13033 char *xrm_option;
13034 char *resource_name;
dc6f92b8 13035{
334208b7 13036 int connection;
7a13e894 13037 Display *dpy;
334208b7
RS
13038 struct x_display_info *dpyinfo;
13039 XrmDatabase xrdb;
13040
60439948
KH
13041 BLOCK_INPUT;
13042
7a13e894
RS
13043 if (!x_initialized)
13044 {
13045 x_initialize ();
13046 x_initialized = 1;
13047 }
dc6f92b8 13048
3afe33e7 13049#ifdef USE_X_TOOLKIT
2d7fc7e8
RS
13050 /* weiner@footloose.sps.mot.com reports that this causes
13051 errors with X11R5:
13052 X protocol error: BadAtom (invalid Atom parameter)
13053 on protocol request 18skiloaf.
13054 So let's not use it until R6. */
13055#ifdef HAVE_X11XTR6
bdcd49ba
RS
13056 XtSetLanguageProc (NULL, NULL, NULL);
13057#endif
13058
7f9c7f94
RS
13059 {
13060 int argc = 0;
13061 char *argv[3];
13062
13063 argv[0] = "";
13064 argc = 1;
13065 if (xrm_option)
13066 {
13067 argv[argc++] = "-xrm";
13068 argv[argc++] = xrm_option;
13069 }
13070 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
13071 resource_name, EMACS_CLASS,
13072 emacs_options, XtNumber (emacs_options),
13073 &argc, argv);
39d8bb4d
KH
13074
13075#ifdef HAVE_X11XTR6
10537cb1 13076 /* I think this is to compensate for XtSetLanguageProc. */
71f8198a 13077 fixup_locale ();
39d8bb4d 13078#endif
7f9c7f94 13079 }
3afe33e7
RS
13080
13081#else /* not USE_X_TOOLKIT */
bdcd49ba
RS
13082#ifdef HAVE_X11R5
13083 XSetLocaleModifiers ("");
13084#endif
7a13e894 13085 dpy = XOpenDisplay (XSTRING (display_name)->data);
3afe33e7 13086#endif /* not USE_X_TOOLKIT */
334208b7 13087
7a13e894
RS
13088 /* Detect failure. */
13089 if (dpy == 0)
60439948
KH
13090 {
13091 UNBLOCK_INPUT;
13092 return 0;
13093 }
7a13e894
RS
13094
13095 /* We have definitely succeeded. Record the new connection. */
13096
13097 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
13098
29b38361
KH
13099#ifdef MULTI_KBOARD
13100 {
13101 struct x_display_info *share;
13102 Lisp_Object tail;
13103
13104 for (share = x_display_list, tail = x_display_name_list; share;
8e713be6
KR
13105 share = share->next, tail = XCDR (tail))
13106 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
29b38361
KH
13107 XSTRING (display_name)->data))
13108 break;
13109 if (share)
13110 dpyinfo->kboard = share->kboard;
13111 else
13112 {
13113 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
13114 init_kboard (dpyinfo->kboard);
59e755be
KH
13115 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
13116 {
13117 char *vendor = ServerVendor (dpy);
9b6ed9f3 13118 UNBLOCK_INPUT;
59e755be
KH
13119 dpyinfo->kboard->Vsystem_key_alist
13120 = call1 (Qvendor_specific_keysyms,
13121 build_string (vendor ? vendor : ""));
9b6ed9f3 13122 BLOCK_INPUT;
59e755be
KH
13123 }
13124
29b38361
KH
13125 dpyinfo->kboard->next_kboard = all_kboards;
13126 all_kboards = dpyinfo->kboard;
0ad5446c
KH
13127 /* Don't let the initial kboard remain current longer than necessary.
13128 That would cause problems if a file loaded on startup tries to
06a2c219 13129 prompt in the mini-buffer. */
0ad5446c
KH
13130 if (current_kboard == initial_kboard)
13131 current_kboard = dpyinfo->kboard;
29b38361
KH
13132 }
13133 dpyinfo->kboard->reference_count++;
13134 }
b9737ad3
KH
13135#endif
13136
7a13e894
RS
13137 /* Put this display on the chain. */
13138 dpyinfo->next = x_display_list;
13139 x_display_list = dpyinfo;
13140
13141 /* Put it on x_display_name_list as well, to keep them parallel. */
13142 x_display_name_list = Fcons (Fcons (display_name, Qnil),
13143 x_display_name_list);
8e713be6 13144 dpyinfo->name_list_element = XCAR (x_display_name_list);
7a13e894
RS
13145
13146 dpyinfo->display = dpy;
dc6f92b8 13147
dc6f92b8 13148#if 0
7a13e894 13149 XSetAfterFunction (x_current_display, x_trace_wire);
c118dd06 13150#endif /* ! 0 */
7a13e894
RS
13151
13152 dpyinfo->x_id_name
fc932ac6
RS
13153 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
13154 + STRING_BYTES (XSTRING (Vsystem_name))
7a13e894
RS
13155 + 2);
13156 sprintf (dpyinfo->x_id_name, "%s@%s",
13157 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
28430d3c
JB
13158
13159 /* Figure out which modifier bits mean what. */
334208b7 13160 x_find_modifier_meanings (dpyinfo);
f451eb13 13161
ab648270 13162 /* Get the scroll bar cursor. */
7a13e894 13163 dpyinfo->vertical_scroll_bar_cursor
334208b7 13164 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
f451eb13 13165
334208b7
RS
13166 xrdb = x_load_resources (dpyinfo->display, xrm_option,
13167 resource_name, EMACS_CLASS);
13168#ifdef HAVE_XRMSETDATABASE
13169 XrmSetDatabase (dpyinfo->display, xrdb);
13170#else
13171 dpyinfo->display->db = xrdb;
13172#endif
547d9db8 13173 /* Put the rdb where we can find it in a way that works on
7a13e894
RS
13174 all versions. */
13175 dpyinfo->xrdb = xrdb;
334208b7
RS
13176
13177 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
13178 DefaultScreen (dpyinfo->display));
5ff67d81 13179 select_visual (dpyinfo);
43bd1b2b 13180 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
334208b7
RS
13181 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
13182 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
13183 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
13184 dpyinfo->grabbed = 0;
13185 dpyinfo->reference_count = 0;
13186 dpyinfo->icon_bitmap_id = -1;
06a2c219 13187 dpyinfo->font_table = NULL;
7a13e894
RS
13188 dpyinfo->n_fonts = 0;
13189 dpyinfo->font_table_size = 0;
13190 dpyinfo->bitmaps = 0;
13191 dpyinfo->bitmaps_size = 0;
13192 dpyinfo->bitmaps_last = 0;
13193 dpyinfo->scratch_cursor_gc = 0;
13194 dpyinfo->mouse_face_mouse_frame = 0;
13195 dpyinfo->mouse_face_deferred_gc = 0;
13196 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
13197 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
06a2c219 13198 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
7a13e894
RS
13199 dpyinfo->mouse_face_window = Qnil;
13200 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
13201 dpyinfo->mouse_face_defer = 0;
0f941935
KH
13202 dpyinfo->x_focus_frame = 0;
13203 dpyinfo->x_focus_event_frame = 0;
13204 dpyinfo->x_highlight_frame = 0;
06a2c219 13205 dpyinfo->image_cache = make_image_cache ();
334208b7 13206
43bd1b2b 13207 /* See if a private colormap is requested. */
5ff67d81
GM
13208 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
13209 {
13210 if (dpyinfo->visual->class == PseudoColor)
13211 {
13212 Lisp_Object value;
13213 value = display_x_get_resource (dpyinfo,
13214 build_string ("privateColormap"),
13215 build_string ("PrivateColormap"),
13216 Qnil, Qnil);
13217 if (STRINGP (value)
13218 && (!strcmp (XSTRING (value)->data, "true")
13219 || !strcmp (XSTRING (value)->data, "on")))
13220 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
13221 }
43bd1b2b 13222 }
5ff67d81
GM
13223 else
13224 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
13225 dpyinfo->visual, AllocNone);
43bd1b2b 13226
06a2c219
GM
13227 {
13228 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
13229 double pixels = DisplayHeight (dpyinfo->display, screen_number);
13230 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
13231 dpyinfo->resy = pixels * 25.4 / mm;
13232 pixels = DisplayWidth (dpyinfo->display, screen_number);
13233 mm = DisplayWidthMM (dpyinfo->display, screen_number);
13234 dpyinfo->resx = pixels * 25.4 / mm;
13235 }
13236
334208b7
RS
13237 dpyinfo->Xatom_wm_protocols
13238 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
13239 dpyinfo->Xatom_wm_take_focus
13240 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
13241 dpyinfo->Xatom_wm_save_yourself
13242 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
13243 dpyinfo->Xatom_wm_delete_window
13244 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
13245 dpyinfo->Xatom_wm_change_state
13246 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
13247 dpyinfo->Xatom_wm_configure_denied
13248 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
13249 dpyinfo->Xatom_wm_window_moved
13250 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
13251 dpyinfo->Xatom_editres
13252 = XInternAtom (dpyinfo->display, "Editres", False);
13253 dpyinfo->Xatom_CLIPBOARD
13254 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
13255 dpyinfo->Xatom_TIMESTAMP
13256 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
13257 dpyinfo->Xatom_TEXT
13258 = XInternAtom (dpyinfo->display, "TEXT", False);
dc43ef94
KH
13259 dpyinfo->Xatom_COMPOUND_TEXT
13260 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
334208b7
RS
13261 dpyinfo->Xatom_DELETE
13262 = XInternAtom (dpyinfo->display, "DELETE", False);
13263 dpyinfo->Xatom_MULTIPLE
13264 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
13265 dpyinfo->Xatom_INCR
13266 = XInternAtom (dpyinfo->display, "INCR", False);
13267 dpyinfo->Xatom_EMACS_TMP
13268 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
13269 dpyinfo->Xatom_TARGETS
13270 = XInternAtom (dpyinfo->display, "TARGETS", False);
13271 dpyinfo->Xatom_NULL
13272 = XInternAtom (dpyinfo->display, "NULL", False);
13273 dpyinfo->Xatom_ATOM_PAIR
13274 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
dc43ef94
KH
13275 /* For properties of font. */
13276 dpyinfo->Xatom_PIXEL_SIZE
13277 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
13278 dpyinfo->Xatom_MULE_BASELINE_OFFSET
13279 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
13280 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
13281 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
f78798df
KH
13282 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
13283 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
334208b7 13284
06a2c219
GM
13285 /* Ghostscript support. */
13286 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
13287 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
13288
13289 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
13290 False);
13291
547d9db8
KH
13292 dpyinfo->cut_buffers_initialized = 0;
13293
334208b7
RS
13294 connection = ConnectionNumber (dpyinfo->display);
13295 dpyinfo->connection = connection;
13296
dc43ef94 13297 {
5d7cc324
RS
13298 char null_bits[1];
13299
13300 null_bits[0] = 0x00;
dc43ef94
KH
13301
13302 dpyinfo->null_pixel
13303 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13304 null_bits, 1, 1, (long) 0, (long) 0,
13305 1);
13306 }
13307
06a2c219
GM
13308 {
13309 extern int gray_bitmap_width, gray_bitmap_height;
13310 extern unsigned char *gray_bitmap_bits;
13311 dpyinfo->gray
13312 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13313 gray_bitmap_bits,
13314 gray_bitmap_width, gray_bitmap_height,
13315 (unsigned long) 1, (unsigned long) 0, 1);
13316 }
13317
f5d11644
GM
13318#ifdef HAVE_X_I18N
13319 xim_initialize (dpyinfo, resource_name);
13320#endif
13321
87485d6f
MW
13322#ifdef subprocesses
13323 /* This is only needed for distinguishing keyboard and process input. */
334208b7 13324 if (connection != 0)
7a13e894 13325 add_keyboard_wait_descriptor (connection);
87485d6f 13326#endif
6d4238f3 13327
041b69ac 13328#ifndef F_SETOWN_BUG
dc6f92b8 13329#ifdef F_SETOWN
dc6f92b8 13330#ifdef F_SETOWN_SOCK_NEG
61c3ce62 13331 /* stdin is a socket here */
334208b7 13332 fcntl (connection, F_SETOWN, -getpid ());
c118dd06 13333#else /* ! defined (F_SETOWN_SOCK_NEG) */
334208b7 13334 fcntl (connection, F_SETOWN, getpid ());
c118dd06
JB
13335#endif /* ! defined (F_SETOWN_SOCK_NEG) */
13336#endif /* ! defined (F_SETOWN) */
041b69ac 13337#endif /* F_SETOWN_BUG */
dc6f92b8
JB
13338
13339#ifdef SIGIO
eee20f6a
KH
13340 if (interrupt_input)
13341 init_sigio (connection);
c118dd06 13342#endif /* ! defined (SIGIO) */
dc6f92b8 13343
51b592fb 13344#ifdef USE_LUCID
f8c39f51 13345#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
51b592fb
RS
13346 /* Make sure that we have a valid font for dialog boxes
13347 so that Xt does not crash. */
13348 {
13349 Display *dpy = dpyinfo->display;
13350 XrmValue d, fr, to;
13351 Font font;
e99db5a1 13352 int count;
51b592fb
RS
13353
13354 d.addr = (XPointer)&dpy;
13355 d.size = sizeof (Display *);
13356 fr.addr = XtDefaultFont;
13357 fr.size = sizeof (XtDefaultFont);
13358 to.size = sizeof (Font *);
13359 to.addr = (XPointer)&font;
e99db5a1 13360 count = x_catch_errors (dpy);
51b592fb
RS
13361 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
13362 abort ();
13363 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
13364 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
e99db5a1 13365 x_uncatch_errors (dpy, count);
51b592fb
RS
13366 }
13367#endif
f8c39f51 13368#endif
51b592fb 13369
34e23e5a
GM
13370 /* See if we should run in synchronous mode. This is useful
13371 for debugging X code. */
13372 {
13373 Lisp_Object value;
13374 value = display_x_get_resource (dpyinfo,
13375 build_string ("synchronous"),
13376 build_string ("Synchronous"),
13377 Qnil, Qnil);
13378 if (STRINGP (value)
13379 && (!strcmp (XSTRING (value)->data, "true")
13380 || !strcmp (XSTRING (value)->data, "on")))
13381 XSynchronize (dpyinfo->display, True);
13382 }
13383
60439948
KH
13384 UNBLOCK_INPUT;
13385
7a13e894
RS
13386 return dpyinfo;
13387}
13388\f
13389/* Get rid of display DPYINFO, assuming all frames are already gone,
13390 and without sending any more commands to the X server. */
dc6f92b8 13391
7a13e894
RS
13392void
13393x_delete_display (dpyinfo)
13394 struct x_display_info *dpyinfo;
13395{
13396 delete_keyboard_wait_descriptor (dpyinfo->connection);
13397
13398 /* Discard this display from x_display_name_list and x_display_list.
13399 We can't use Fdelq because that can quit. */
13400 if (! NILP (x_display_name_list)
8e713be6
KR
13401 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
13402 x_display_name_list = XCDR (x_display_name_list);
7a13e894
RS
13403 else
13404 {
13405 Lisp_Object tail;
13406
13407 tail = x_display_name_list;
8e713be6 13408 while (CONSP (tail) && CONSP (XCDR (tail)))
7a13e894 13409 {
bffcfca9 13410 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
7a13e894 13411 {
8e713be6 13412 XCDR (tail) = XCDR (XCDR (tail));
7a13e894
RS
13413 break;
13414 }
8e713be6 13415 tail = XCDR (tail);
7a13e894
RS
13416 }
13417 }
13418
9bda743f
GM
13419 if (next_noop_dpyinfo == dpyinfo)
13420 next_noop_dpyinfo = dpyinfo->next;
13421
7a13e894
RS
13422 if (x_display_list == dpyinfo)
13423 x_display_list = dpyinfo->next;
7f9c7f94
RS
13424 else
13425 {
13426 struct x_display_info *tail;
7a13e894 13427
7f9c7f94
RS
13428 for (tail = x_display_list; tail; tail = tail->next)
13429 if (tail->next == dpyinfo)
13430 tail->next = tail->next->next;
13431 }
7a13e894 13432
0d777288
RS
13433#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
13434#ifndef AIX /* On AIX, XCloseDisplay calls this. */
7f9c7f94
RS
13435 XrmDestroyDatabase (dpyinfo->xrdb);
13436#endif
0d777288 13437#endif
29b38361
KH
13438#ifdef MULTI_KBOARD
13439 if (--dpyinfo->kboard->reference_count == 0)
39f79001 13440 delete_kboard (dpyinfo->kboard);
b9737ad3 13441#endif
f5d11644
GM
13442#ifdef HAVE_X_I18N
13443 if (dpyinfo->xim)
13444 xim_close_dpy (dpyinfo);
13445#endif
13446
b9737ad3
KH
13447 xfree (dpyinfo->font_table);
13448 xfree (dpyinfo->x_id_name);
13449 xfree (dpyinfo);
7a13e894
RS
13450}
13451\f
13452/* Set up use of X before we make the first connection. */
13453
06a2c219
GM
13454static struct redisplay_interface x_redisplay_interface =
13455{
13456 x_produce_glyphs,
13457 x_write_glyphs,
13458 x_insert_glyphs,
13459 x_clear_end_of_line,
13460 x_scroll_run,
13461 x_after_update_window_line,
13462 x_update_window_begin,
13463 x_update_window_end,
13464 XTcursor_to,
13465 x_flush,
71b8321e 13466 x_clear_mouse_face,
66ac4b0e
GM
13467 x_get_glyph_overhangs,
13468 x_fix_overlapping_area
06a2c219
GM
13469};
13470
dfcf069d 13471void
7a13e894
RS
13472x_initialize ()
13473{
06a2c219
GM
13474 rif = &x_redisplay_interface;
13475
13476 clear_frame_hook = x_clear_frame;
13477 ins_del_lines_hook = x_ins_del_lines;
13478 change_line_highlight_hook = x_change_line_highlight;
13479 delete_glyphs_hook = x_delete_glyphs;
dc6f92b8
JB
13480 ring_bell_hook = XTring_bell;
13481 reset_terminal_modes_hook = XTreset_terminal_modes;
13482 set_terminal_modes_hook = XTset_terminal_modes;
06a2c219
GM
13483 update_begin_hook = x_update_begin;
13484 update_end_hook = x_update_end;
dc6f92b8
JB
13485 set_terminal_window_hook = XTset_terminal_window;
13486 read_socket_hook = XTread_socket;
b8009dd1 13487 frame_up_to_date_hook = XTframe_up_to_date;
dc6f92b8 13488 reassert_line_highlight_hook = XTreassert_line_highlight;
90e65f07 13489 mouse_position_hook = XTmouse_position;
f451eb13 13490 frame_rehighlight_hook = XTframe_rehighlight;
dbc4e1c1 13491 frame_raise_lower_hook = XTframe_raise_lower;
ab648270
JB
13492 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13493 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13494 redeem_scroll_bar_hook = XTredeem_scroll_bar;
13495 judge_scroll_bars_hook = XTjudge_scroll_bars;
06a2c219 13496 estimate_mode_line_height_hook = x_estimate_mode_line_height;
58769bee 13497
f676886a 13498 scroll_region_ok = 1; /* we'll scroll partial frames */
dc6f92b8
JB
13499 char_ins_del_ok = 0; /* just as fast to write the line */
13500 line_ins_del_ok = 1; /* we'll just blt 'em */
13501 fast_clear_end_of_line = 1; /* X does this well */
58769bee 13502 memory_below_frame = 0; /* we don't remember what scrolls
dc6f92b8
JB
13503 off the bottom */
13504 baud_rate = 19200;
13505
7a13e894 13506 x_noop_count = 0;
9ea173e8 13507 last_tool_bar_item = -1;
06a2c219
GM
13508 any_help_event_p = 0;
13509
b30b24cb
RS
13510 /* Try to use interrupt input; if we can't, then start polling. */
13511 Fset_input_mode (Qt, Qnil, Qt, Qnil);
13512
7f9c7f94
RS
13513#ifdef USE_X_TOOLKIT
13514 XtToolkitInitialize ();
13515 Xt_app_con = XtCreateApplicationContext ();
665881ad 13516 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
bffcfca9
GM
13517
13518 /* Install an asynchronous timer that processes Xt timeout events
13519 every 0.1s. This is necessary because some widget sets use
13520 timeouts internally, for example the LessTif menu bar, or the
13521 Xaw3d scroll bar. When Xt timouts aren't processed, these
13522 widgets don't behave normally. */
13523 {
13524 EMACS_TIME interval;
13525 EMACS_SET_SECS_USECS (interval, 0, 100000);
13526 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
13527 }
db74249b 13528#endif
bffcfca9 13529
db74249b 13530#if USE_TOOLKIT_SCROLL_BARS
ec18280f
SM
13531 xaw3d_arrow_scroll = False;
13532 xaw3d_pick_top = True;
7f9c7f94
RS
13533#endif
13534
58769bee 13535 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 13536 original error handler. */
e99db5a1 13537 XSetErrorHandler (x_error_handler);
334208b7 13538 XSetIOErrorHandler (x_io_error_quitter);
dc6f92b8 13539
06a2c219 13540 /* Disable Window Change signals; they are handled by X events. */
dc6f92b8
JB
13541#ifdef SIGWINCH
13542 signal (SIGWINCH, SIG_DFL);
c118dd06 13543#endif /* ! defined (SIGWINCH) */
dc6f92b8 13544
92e2441b 13545 signal (SIGPIPE, x_connection_signal);
dc6f92b8 13546}
55123275 13547
06a2c219 13548
55123275
JB
13549void
13550syms_of_xterm ()
13551{
e99db5a1
RS
13552 staticpro (&x_error_message_string);
13553 x_error_message_string = Qnil;
13554
7a13e894
RS
13555 staticpro (&x_display_name_list);
13556 x_display_name_list = Qnil;
334208b7 13557
ab648270 13558 staticpro (&last_mouse_scroll_bar);
e53cb100 13559 last_mouse_scroll_bar = Qnil;
59e755be
KH
13560
13561 staticpro (&Qvendor_specific_keysyms);
13562 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
2237cac9
RS
13563
13564 staticpro (&last_mouse_press_frame);
13565 last_mouse_press_frame = Qnil;
06a2c219
GM
13566
13567 staticpro (&help_echo);
13568 help_echo = Qnil;
13569 staticpro (&previous_help_echo);
13570 previous_help_echo = Qnil;
13571
13572 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
13573 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
13574For example, if a block cursor is over a tab, it will be drawn as\n\
13575wide as that tab on the display.");
13576 x_stretch_cursor_p = 0;
13577
13578 DEFVAR_BOOL ("x-toolkit-scroll-bars-p", &x_toolkit_scroll_bars_p,
13579 "If not nil, Emacs uses toolkit scroll bars.");
13580#if USE_TOOLKIT_SCROLL_BARS
13581 x_toolkit_scroll_bars_p = 1;
13582#else
13583 x_toolkit_scroll_bars_p = 0;
13584#endif
13585
06a2c219
GM
13586 staticpro (&last_mouse_motion_frame);
13587 last_mouse_motion_frame = Qnil;
55123275 13588}
6cf0ae86
RS
13589
13590#endif /* not HAVE_X_WINDOWS */
06a2c219 13591