Update to reftex 4.9
[bpt/emacs.git] / src / xterm.c
CommitLineData
dc6f92b8 1/* X Communication module for terminals which understand the X protocol.
06a2c219
GM
2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999
3 Free Software Foundation, Inc.
dc6f92b8
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
4746118a 9the Free Software Foundation; either version 2, or (at your option)
dc6f92b8
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
dc6f92b8 21
06a2c219 22/* New display code by Gerd Moellmann <gerd@gnu.org>. */
3afe33e7
RS
23/* Xt features made by Fred Pierresteguy. */
24
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 46
16bd92ea 47#ifndef USG
dc6f92b8
JB
48/* Load sys/types.h if not already loaded.
49 In some systems loading it twice is suicidal. */
50#ifndef makedev
51#include <sys/types.h>
c118dd06
JB
52#endif /* makedev */
53#endif /* USG */
dc6f92b8 54
6df54671 55#ifdef BSD_SYSTEM
dc6f92b8 56#include <sys/ioctl.h>
6df54671 57#endif /* ! defined (BSD_SYSTEM) */
dc6f92b8 58
2d368234 59#include "systty.h"
3a2712f9 60#include "systime.h"
dc6f92b8 61
b8009dd1 62#ifndef INCLUDED_FCNTL
dc6f92b8 63#include <fcntl.h>
b8009dd1 64#endif
dc6f92b8
JB
65#include <ctype.h>
66#include <errno.h>
67#include <setjmp.h>
68#include <sys/stat.h>
a0a7635f
RS
69/* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
70/* #include <sys/param.h> */
dc6f92b8 71
dc43ef94
KH
72#include "charset.h"
73#include "ccl.h"
7a13e894 74#include "frame.h"
a1dfb88a 75#include "fontset.h"
dc6f92b8
JB
76#include "dispextern.h"
77#include "termhooks.h"
78#include "termopts.h"
79#include "termchar.h"
80#if 0
81#include "sink.h"
82#include "sinkmask.h"
c118dd06 83#endif /* ! 0 */
dc6f92b8 84#include "gnu.h"
dc6f92b8 85#include "disptab.h"
dc6f92b8 86#include "buffer.h"
f451eb13 87#include "window.h"
3b2fa4e6 88#include "keyboard.h"
bde7c500 89#include "intervals.h"
dfcf069d 90#include "process.h"
dc6f92b8 91
d2bd6bc4
RS
92#ifdef USE_X_TOOLKIT
93#include <X11/Shell.h>
94#endif
95
06a2c219
GM
96#include <sys/types.h>
97#ifdef HAVE_SYS_TIME_H
98#include <sys/time.h>
99#endif
100#ifdef HAVE_UNISTD_H
101#include <unistd.h>
102#endif
103
3afe33e7 104#ifdef USE_X_TOOLKIT
06a2c219 105
9d7e2e3e 106extern void free_frame_menubar ();
2224b905 107extern FRAME_PTR x_menubar_window_to_frame ();
06a2c219 108
0fdff6bb
RS
109#if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
110#define HACK_EDITRES
111extern void _XEditResCheckMessages ();
112#endif /* not NO_EDITRES */
06a2c219
GM
113
114/* Include toolkit specific headers for the scroll bar widget. */
115
116#ifdef USE_TOOLKIT_SCROLL_BARS
117#if defined USE_MOTIF
118#include <Xm/Xm.h> /* for LESSTIF_VERSION */
119#include <Xm/ScrollBar.h>
120#include <Xm/ScrollBarP.h>
ec18280f
SM
121#else /* !USE_MOTIF i.e. use Xaw */
122
123#ifdef HAVE_XAW3D
06a2c219 124#include <X11/Xaw3d/Simple.h>
06a2c219
GM
125#include <X11/Xaw3d/Scrollbar.h>
126#define ARROW_SCROLLBAR
127#include <X11/Xaw3d/ScrollbarP.h>
ec18280f
SM
128#else /* !HAVE_XAW3D */
129#include <X11/Xaw/Simple.h>
130#include <X11/Xaw/Scrollbar.h>
131#endif /* !HAVE_XAW3D */
132#ifndef XtNpickTop
133#define XtNpickTop "pickTop"
134#endif /* !XtNpickTop */
135#endif /* !USE_MOTIF */
06a2c219
GM
136#endif /* USE_TOOLKIT_SCROLL_BARS */
137
3afe33e7
RS
138#endif /* USE_X_TOOLKIT */
139
b849c413
RS
140#ifndef USE_X_TOOLKIT
141#define x_any_window_to_frame x_window_to_frame
5627c40e 142#define x_top_window_to_frame x_window_to_frame
b849c413
RS
143#endif
144
546e6d5b 145#ifdef USE_X_TOOLKIT
d067ea8b 146#include "widget.h"
546e6d5b
RS
147#ifndef XtNinitialState
148#define XtNinitialState "initialState"
149#endif
150#endif
151
80528801
KH
152#ifdef SOLARIS2
153/* memmove will be defined as a macro in Xfuncs.h unless
154 <string.h> is included beforehand. The declaration for memmove in
155 <string.h> will cause a syntax error when Xfuncs.h later includes it. */
156#include <string.h>
157#endif
158
e4b68333 159#ifndef min
06a2c219 160#define min(a,b) ((a) < (b) ? (a) : (b))
e4b68333
RS
161#endif
162#ifndef max
06a2c219
GM
163#define max(a,b) ((a) > (b) ? (a) : (b))
164#endif
165
166#define abs(x) ((x) < 0 ? -(x) : (x))
167
168#define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
169
170\f
171/* Bitmaps for truncated lines. */
172
173enum bitmap_type
174{
175 NO_BITMAP,
176 LEFT_TRUNCATION_BITMAP,
177 RIGHT_TRUNCATION_BITMAP,
178 OVERLAY_ARROW_BITMAP,
179 CONTINUED_LINE_BITMAP,
180 CONTINUATION_LINE_BITMAP,
181 ZV_LINE_BITMAP
182};
183
184/* Bitmap drawn to indicate lines not displaying text if
185 `indicate-empty-lines' is non-nil. */
186
187#define zv_width 8
188#define zv_height 8
189static unsigned char zv_bits[] = {
190 0x00, 0x00, 0x1e, 0x1e, 0x1e, 0x1e, 0x00, 0x00};
191
192/* An arrow like this: `<-'. */
193
194#define left_width 8
195#define left_height 8
196static unsigned char left_bits[] = {
197 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
198
110859fc
GM
199/* Right truncation arrow bitmap `->'. */
200
201#define right_width 8
202#define right_height 8
203static unsigned char right_bits[] = {
204 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
205
06a2c219
GM
206/* Marker for continued lines. */
207
208#define continued_width 8
209#define continued_height 8
210static unsigned char continued_bits[] = {
110859fc
GM
211 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
212
213/* Marker for continuation lines. */
06a2c219
GM
214
215#define continuation_width 8
216#define continuation_height 8
217static unsigned char continuation_bits[] = {
110859fc 218 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
06a2c219 219
110859fc 220/* Overlay arrow bitmap. */
06a2c219 221
110859fc
GM
222#if 0
223/* A bomb. */
06a2c219
GM
224#define ov_width 8
225#define ov_height 8
226static unsigned char ov_bits[] = {
227 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
06a2c219 228#else
110859fc 229/* A triangular arrow. */
06a2c219
GM
230#define ov_width 8
231#define ov_height 8
232static unsigned char ov_bits[] = {
110859fc
GM
233 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
234
e4b68333 235#endif
06a2c219
GM
236
237extern Lisp_Object Qhelp_echo;
238
69388238 239\f
06a2c219
GM
240/* Non-zero means Emacs uses toolkit scroll bars. */
241
242int x_toolkit_scroll_bars_p;
243
244/* If a string, XTread_socket generates an event to display that string.
245 (The display is done in read_char.) */
246
247static Lisp_Object help_echo;
248
249/* Temporary variable for XTread_socket. */
250
251static Lisp_Object previous_help_echo;
252
253/* Non-zero means that a HELP_EVENT has been generated since Emacs
254 start. */
255
256static int any_help_event_p;
257
258/* Non-zero means draw block and hollow cursor as wide as the glyph
259 under it. For example, if a block cursor is over a tab, it will be
260 drawn as wide as that tab on the display. */
261
262int x_stretch_cursor_p;
263
264/* This is a chain of structures for all the X displays currently in
265 use. */
266
334208b7 267struct x_display_info *x_display_list;
dc6f92b8 268
06a2c219
GM
269/* This is a list of cons cells, each of the form (NAME
270 . FONT-LIST-CACHE), one for each element of x_display_list and in
271 the same order. NAME is the name of the frame. FONT-LIST-CACHE
272 records previous values returned by x-list-fonts. */
273
7a13e894 274Lisp_Object x_display_name_list;
f451eb13 275
987d2ad1 276/* Frame being updated by update_frame. This is declared in term.c.
06a2c219
GM
277 This is set by update_begin and looked at by all the XT functions.
278 It is zero while not inside an update. In that case, the XT
279 functions assume that `selected_frame' is the frame to apply to. */
280
d0386f2a 281extern struct frame *updating_frame;
dc6f92b8 282
dfcf069d 283extern int waiting_for_input;
0e81d8cd 284
06a2c219
GM
285/* This is a frame waiting to be auto-raised, within XTread_socket. */
286
0134a210
RS
287struct frame *pending_autoraise_frame;
288
7f9c7f94
RS
289#ifdef USE_X_TOOLKIT
290/* The application context for Xt use. */
291XtAppContext Xt_app_con;
06a2c219
GM
292static String Xt_default_resources[] = {0};
293#endif /* USE_X_TOOLKIT */
665881ad 294
06a2c219
GM
295/* Nominal cursor position -- where to draw output.
296 HPOS and VPOS are window relative glyph matrix coordinates.
297 X and Y are window relative pixel coordinates. */
dc6f92b8 298
06a2c219 299struct cursor_pos output_cursor;
dc6f92b8 300
dc6f92b8 301
69388238
RS
302/* Mouse movement.
303
06a2c219 304 Formerly, we used PointerMotionHintMask (in standard_event_mask)
f5bb65ec
RS
305 so that we would have to call XQueryPointer after each MotionNotify
306 event to ask for another such event. However, this made mouse tracking
307 slow, and there was a bug that made it eventually stop.
308
309 Simply asking for MotionNotify all the time seems to work better.
310
69388238
RS
311 In order to avoid asking for motion events and then throwing most
312 of them away or busy-polling the server for mouse positions, we ask
313 the server for pointer motion hints. This means that we get only
314 one event per group of mouse movements. "Groups" are delimited by
315 other kinds of events (focus changes and button clicks, for
316 example), or by XQueryPointer calls; when one of these happens, we
317 get another MotionNotify event the next time the mouse moves. This
318 is at least as efficient as getting motion events when mouse
319 tracking is on, and I suspect only negligibly worse when tracking
f5bb65ec 320 is off. */
69388238
RS
321
322/* Where the mouse was last time we reported a mouse event. */
69388238 323
06a2c219
GM
324FRAME_PTR last_mouse_frame;
325static XRectangle last_mouse_glyph;
2237cac9
RS
326static Lisp_Object last_mouse_press_frame;
327
69388238
RS
328/* The scroll bar in which the last X motion event occurred.
329
06a2c219
GM
330 If the last X motion event occurred in a scroll bar, we set this so
331 XTmouse_position can know whether to report a scroll bar motion or
69388238
RS
332 an ordinary motion.
333
06a2c219
GM
334 If the last X motion event didn't occur in a scroll bar, we set
335 this to Qnil, to tell XTmouse_position to return an ordinary motion
336 event. */
337
69388238
RS
338static Lisp_Object last_mouse_scroll_bar;
339
69388238
RS
340/* This is a hack. We would really prefer that XTmouse_position would
341 return the time associated with the position it returns, but there
06a2c219 342 doesn't seem to be any way to wrest the time-stamp from the server
69388238
RS
343 along with the position query. So, we just keep track of the time
344 of the last movement we received, and return that in hopes that
345 it's somewhat accurate. */
06a2c219 346
69388238
RS
347static Time last_mouse_movement_time;
348
06a2c219
GM
349/* Incremented by XTread_socket whenever it really tries to read
350 events. */
351
c0a04927
RS
352#ifdef __STDC__
353static int volatile input_signal_count;
354#else
355static int input_signal_count;
356#endif
357
7a13e894 358/* Used locally within XTread_socket. */
06a2c219 359
7a13e894 360static int x_noop_count;
dc6f92b8 361
7a13e894 362/* Initial values of argv and argc. */
06a2c219 363
7a13e894
RS
364extern char **initial_argv;
365extern int initial_argc;
dc6f92b8 366
7a13e894 367extern Lisp_Object Vcommand_line_args, Vsystem_name;
dc6f92b8 368
06a2c219 369/* Tells if a window manager is present or not. */
7a13e894
RS
370
371extern Lisp_Object Vx_no_window_manager;
dc6f92b8 372
c2df547c 373extern Lisp_Object Qface, Qmouse_face;
b8009dd1 374
dc6f92b8
JB
375extern int errno;
376
dfeccd2d 377/* A mask of extra modifier bits to put into every keyboard char. */
06a2c219 378
64bb1782
RS
379extern int extra_keyboard_modifiers;
380
59e755be
KH
381static Lisp_Object Qvendor_specific_keysyms;
382
334208b7 383extern XrmDatabase x_load_resources ();
c32cdd9a
KH
384extern Lisp_Object x_icon_type ();
385
7a13e894 386
06a2c219
GM
387/* Enumeration for overriding/changing the face to use for drawing
388 glyphs in x_draw_glyphs. */
389
390enum draw_glyphs_face
391{
392 DRAW_NORMAL_TEXT,
393 DRAW_INVERSE_VIDEO,
394 DRAW_CURSOR,
395 DRAW_MOUSE_FACE,
396 DRAW_IMAGE_RAISED,
397 DRAW_IMAGE_SUNKEN
398};
399
400static void x_update_window_end P_ ((struct window *, int));
401static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
402void x_delete_display P_ ((struct x_display_info *));
403static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
404 unsigned));
405static int fast_find_position P_ ((struct window *, int, int *, int *,
406 int *, int *));
407static void set_output_cursor P_ ((struct cursor_pos *));
408static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
409 int *, int *, int *));
410static void note_mode_line_highlight P_ ((struct window *, int, int));
411static void x_check_font P_ ((struct frame *, XFontStruct *));
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 *));
451static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *));
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
601 /* Can we tell that this update does not affect the window
602 where the mouse highlight is? If so, no need to turn off.
603 Likewise, don't do anything if the frame is garbaged;
604 in that case, the frame's current matrix that we would use
605 is all wrong, and we will redisplay that line anyway. */
606 if (!NILP (display_info->mouse_face_window)
607 && w == XWINDOW (display_info->mouse_face_window))
514e4681 608 {
06a2c219 609 int i;
514e4681 610
06a2c219
GM
611 for (i = 0; i < w->desired_matrix->nrows; ++i)
612 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
514e4681
RS
613 break;
614
06a2c219
GM
615 if (i < w->desired_matrix->nrows)
616 clear_mouse_face (display_info);
514e4681 617 }
b8009dd1 618 }
6ccf47d1 619
dc6f92b8
JB
620 UNBLOCK_INPUT;
621}
622
06a2c219
GM
623
624/* Draw a vertical window border to the right of window W if W doesn't
625 have vertical scroll bars. */
626
dfcf069d 627static void
06a2c219
GM
628x_draw_vertical_border (w)
629 struct window *w;
58769bee 630{
06a2c219
GM
631 struct frame *f = XFRAME (WINDOW_FRAME (w));
632
633 /* Redraw borders between horizontally adjacent windows. Don't
634 do it for frames with vertical scroll bars because either the
635 right scroll bar of a window, or the left scroll bar of its
636 neighbor will suffice as a border. */
637 if (!WINDOW_RIGHTMOST_P (w)
638 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
639 {
640 int x0, x1, y0, y1;
dc6f92b8 641
06a2c219 642 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
110859fc 643 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
644 y1 -= 1;
645
646 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
647 f->output_data.x->normal_gc, x1, y0, x1, y1);
648 }
649}
650
651
652/* End update of window W (which is equal to updated_window). Draw
653 vertical borders between horizontally adjacent windows, and display
654 W's cursor if CURSOR_ON_P is non-zero. W may be a menu bar
655 pseudo-window in case we don't have X toolkit support. Such
656 windows don't have a cursor, so don't display it here. */
657
658static void
659x_update_window_end (w, cursor_on_p)
660 struct window *w;
661 int cursor_on_p;
662{
663 if (!w->pseudo_window_p)
664 {
665 BLOCK_INPUT;
666 if (cursor_on_p)
667 x_display_and_set_cursor (w, 1, output_cursor.hpos,
668 output_cursor.vpos,
669 output_cursor.x, output_cursor.y);
670 x_draw_vertical_border (w);
671 UNBLOCK_INPUT;
672 }
673
674 updated_window = NULL;
675}
dc6f92b8 676
dc6f92b8 677
06a2c219
GM
678/* End update of frame F. This function is installed as a hook in
679 update_end. */
680
681static void
682x_update_end (f)
683 struct frame *f;
684{
685 /* Mouse highlight may be displayed again. */
aa8bff2e 686 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
b8009dd1 687
06a2c219 688 BLOCK_INPUT;
334208b7 689 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
690 UNBLOCK_INPUT;
691}
b8009dd1 692
06a2c219
GM
693
694/* This function is called from various places in xdisp.c whenever a
695 complete update has been performed. The global variable
696 updated_window is not available here. */
b8009dd1 697
dfcf069d 698static void
b8009dd1 699XTframe_up_to_date (f)
06a2c219 700 struct frame *f;
b8009dd1 701{
06a2c219 702 if (FRAME_X_P (f))
514e4681 703 {
06a2c219
GM
704 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
705 if (dpyinfo->mouse_face_deferred_gc
706 || f == dpyinfo->mouse_face_mouse_frame)
707 {
708 BLOCK_INPUT;
709 if (dpyinfo->mouse_face_mouse_frame)
710 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
711 dpyinfo->mouse_face_mouse_x,
712 dpyinfo->mouse_face_mouse_y);
713 dpyinfo->mouse_face_deferred_gc = 0;
714 UNBLOCK_INPUT;
715 }
514e4681 716 }
b8009dd1 717}
06a2c219
GM
718
719
720/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
721 arrow bitmaps, or clear the areas where they would be displayed
722 before DESIRED_ROW is made current. The window being updated is
723 found in updated_window. This function It is called from
724 update_window_line only if it is known that there are differences
725 between bitmaps to be drawn between current row and DESIRED_ROW. */
726
727static void
728x_after_update_window_line (desired_row)
729 struct glyph_row *desired_row;
730{
731 struct window *w = updated_window;
732
733 xassert (w);
734
735 if (!desired_row->mode_line_p && !w->pseudo_window_p)
736 {
737 BLOCK_INPUT;
738 x_draw_row_bitmaps (w, desired_row);
739
740 /* When a window has disappeared, make sure that no rest of
741 full-width rows stays visible in the internal border. */
742 if (windows_or_buffers_changed)
743 {
744 struct frame *f = XFRAME (w->frame);
745 int width = FRAME_INTERNAL_BORDER_WIDTH (f);
746 int height = desired_row->visible_height;
110859fc
GM
747 int x = (window_box_right (w, -1)
748 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
06a2c219
GM
749 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
750
751 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
752 x, y, width, height, False);
753 }
754
755 UNBLOCK_INPUT;
756 }
757}
758
759
760/* Draw the bitmap WHICH in one of the areas to the left or right of
761 window W. ROW is the glyph row for which to display the bitmap; it
762 determines the vertical position at which the bitmap has to be
763 drawn. */
764
765static void
766x_draw_bitmap (w, row, which)
767 struct window *w;
768 struct glyph_row *row;
769 enum bitmap_type which;
770{
771 struct frame *f = XFRAME (WINDOW_FRAME (w));
772 Display *display = FRAME_X_DISPLAY (f);
773 Window window = FRAME_X_WINDOW (f);
774 int x, y, wd, h, dy;
775 unsigned char *bits;
776 Pixmap pixmap;
777 GC gc = f->output_data.x->normal_gc;
778 struct face *face;
779 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
780
781 /* Must clip because of partially visible lines. */
782 x_clip_to_row (w, row, gc, 1);
783
784 switch (which)
785 {
786 case LEFT_TRUNCATION_BITMAP:
787 wd = left_width;
788 h = left_height;
789 bits = left_bits;
790 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
791 - wd
110859fc 792 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
793 break;
794
795 case OVERLAY_ARROW_BITMAP:
796 wd = left_width;
797 h = left_height;
798 bits = ov_bits;
799 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
800 - wd
110859fc 801 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
802 break;
803
804 case RIGHT_TRUNCATION_BITMAP:
805 wd = right_width;
806 h = right_height;
807 bits = right_bits;
808 x = window_box_right (w, -1);
110859fc 809 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
810 break;
811
812 case CONTINUED_LINE_BITMAP:
813 wd = right_width;
814 h = right_height;
815 bits = continued_bits;
816 x = window_box_right (w, -1);
110859fc 817 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
818 break;
819
820 case CONTINUATION_LINE_BITMAP:
821 wd = continuation_width;
822 h = continuation_height;
823 bits = continuation_bits;
824 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
825 - wd
110859fc 826 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
827 break;
828
829 case ZV_LINE_BITMAP:
830 wd = zv_width;
831 h = zv_height;
832 bits = zv_bits;
833 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
834 - wd
110859fc 835 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
836 break;
837
838 default:
839 abort ();
840 }
841
842 /* Convert to frame coordinates. Set dy to the offset in the row to
843 start drawing the bitmap. */
844 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
845 dy = (row->height - h) / 2;
846
847 /* Draw the bitmap. I believe these small pixmaps can be cached
848 by the server. */
849 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
850 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
851 face->foreground,
852 face->background, depth);
853 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
854 XFreePixmap (display, pixmap);
855 XSetClipMask (display, gc, None);
856}
857
858
859/* Draw flags bitmaps for glyph row ROW on window W. Call this
860 function with input blocked. */
861
862static void
863x_draw_row_bitmaps (w, row)
864 struct window *w;
865 struct glyph_row *row;
866{
867 struct frame *f = XFRAME (w->frame);
868 enum bitmap_type bitmap;
869 struct face *face;
045dee35 870 int header_line_height = -1;
06a2c219
GM
871
872 xassert (interrupt_input_blocked);
873
874 /* If row is completely invisible, because of vscrolling, we
875 don't have to draw anything. */
876 if (row->visible_height <= 0)
877 return;
878
879 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
880 PREPARE_FACE_FOR_DISPLAY (f, face);
881
882 /* Decide which bitmap to draw at the left side. */
883 if (row->overlay_arrow_p)
884 bitmap = OVERLAY_ARROW_BITMAP;
885 else if (row->truncated_on_left_p)
886 bitmap = LEFT_TRUNCATION_BITMAP;
887 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
888 bitmap = CONTINUATION_LINE_BITMAP;
889 else if (row->indicate_empty_line_p)
890 bitmap = ZV_LINE_BITMAP;
891 else
892 bitmap = NO_BITMAP;
893
894 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
895 the flags area. */
896 if (bitmap == NO_BITMAP
110859fc 897 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
898 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
899 {
900 /* If W has a vertical border to its left, don't draw over it. */
901 int border = ((XFASTINT (w->left) > 0
902 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
903 ? 1 : 0);
904 int left = window_box_left (w, -1);
905
045dee35
GM
906 if (header_line_height < 0)
907 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
908
909 /* In case the same realized face is used for bitmap areas and
910 for something displayed in the text (e.g. face `region' on
911 mono-displays, the fill style may have been changed to
912 FillSolid in x_draw_glyph_string_background. */
913 if (face->stipple)
914 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
915 else
916 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
917
06a2c219
GM
918 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
919 face->gc,
920 (left
110859fc 921 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219 922 + border),
045dee35 923 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 924 row->y)),
110859fc 925 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
06a2c219 926 row->visible_height);
dcd08bfb
GM
927 if (!face->stipple)
928 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
929 }
930
931 /* Draw the left bitmap. */
932 if (bitmap != NO_BITMAP)
933 x_draw_bitmap (w, row, bitmap);
934
935 /* Decide which bitmap to draw at the right side. */
936 if (row->truncated_on_right_p)
937 bitmap = RIGHT_TRUNCATION_BITMAP;
938 else if (row->continued_p)
939 bitmap = CONTINUED_LINE_BITMAP;
940 else
941 bitmap = NO_BITMAP;
942
943 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
944 the flags area. */
945 if (bitmap == NO_BITMAP
110859fc 946 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
947 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
948 {
949 int right = window_box_right (w, -1);
950
045dee35
GM
951 if (header_line_height < 0)
952 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
953
954 /* In case the same realized face is used for bitmap areas and
955 for something displayed in the text (e.g. face `region' on
956 mono-displays, the fill style may have been changed to
957 FillSolid in x_draw_glyph_string_background. */
958 if (face->stipple)
959 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
960 else
961 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
06a2c219
GM
962 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
963 face->gc,
964 right,
045dee35 965 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 966 row->y)),
110859fc 967 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
06a2c219 968 row->visible_height);
dcd08bfb
GM
969 if (!face->stipple)
970 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
971 }
972
973 /* Draw the right bitmap. */
974 if (bitmap != NO_BITMAP)
975 x_draw_bitmap (w, row, bitmap);
976}
977
dc6f92b8 978\f
06a2c219
GM
979/***********************************************************************
980 Line Highlighting
981 ***********************************************************************/
dc6f92b8 982
06a2c219
GM
983/* External interface to control of standout mode. Not used for X
984 frames. Aborts when called. */
985
986static void
dc6f92b8
JB
987XTreassert_line_highlight (new, vpos)
988 int new, vpos;
989{
06a2c219 990 abort ();
dc6f92b8
JB
991}
992
06a2c219
GM
993
994/* Call this when about to modify line at position VPOS and change
995 whether it is highlighted. Not used for X frames. Aborts when
996 called. */
dc6f92b8 997
dfcf069d 998static void
06a2c219
GM
999x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1000 int new_highlight, vpos, y, first_unused_hpos;
dc6f92b8 1001{
06a2c219 1002 abort ();
dc6f92b8
JB
1003}
1004
06a2c219
GM
1005
1006/* This is called when starting Emacs and when restarting after
1007 suspend. When starting Emacs, no X window is mapped. And nothing
1008 must be done to Emacs's own window if it is suspended (though that
1009 rarely happens). */
dc6f92b8 1010
dfcf069d 1011static void
dc6f92b8
JB
1012XTset_terminal_modes ()
1013{
1014}
1015
06a2c219
GM
1016/* This is called when exiting or suspending Emacs. Exiting will make
1017 the X-windows go away, and suspending requires no action. */
dc6f92b8 1018
dfcf069d 1019static void
dc6f92b8
JB
1020XTreset_terminal_modes ()
1021{
dc6f92b8 1022}
06a2c219
GM
1023
1024
dc6f92b8 1025\f
06a2c219
GM
1026/***********************************************************************
1027 Output Cursor
1028 ***********************************************************************/
1029
1030/* Set the global variable output_cursor to CURSOR. All cursor
1031 positions are relative to updated_window. */
dc6f92b8 1032
dfcf069d 1033static void
06a2c219
GM
1034set_output_cursor (cursor)
1035 struct cursor_pos *cursor;
dc6f92b8 1036{
06a2c219
GM
1037 output_cursor.hpos = cursor->hpos;
1038 output_cursor.vpos = cursor->vpos;
1039 output_cursor.x = cursor->x;
1040 output_cursor.y = cursor->y;
1041}
1042
1043
1044/* Set a nominal cursor position.
dc6f92b8 1045
06a2c219
GM
1046 HPOS and VPOS are column/row positions in a window glyph matrix. X
1047 and Y are window text area relative pixel positions.
1048
1049 If this is done during an update, updated_window will contain the
1050 window that is being updated and the position is the future output
1051 cursor position for that window. If updated_window is null, use
1052 selected_window and display the cursor at the given position. */
1053
1054static void
1055XTcursor_to (vpos, hpos, y, x)
1056 int vpos, hpos, y, x;
1057{
1058 struct window *w;
1059
1060 /* If updated_window is not set, work on selected_window. */
1061 if (updated_window)
1062 w = updated_window;
1063 else
1064 w = XWINDOW (selected_window);
dbcb258a 1065
06a2c219
GM
1066 /* Set the output cursor. */
1067 output_cursor.hpos = hpos;
1068 output_cursor.vpos = vpos;
1069 output_cursor.x = x;
1070 output_cursor.y = y;
dc6f92b8 1071
06a2c219
GM
1072 /* If not called as part of an update, really display the cursor.
1073 This will also set the cursor position of W. */
1074 if (updated_window == NULL)
dc6f92b8
JB
1075 {
1076 BLOCK_INPUT;
06a2c219 1077 x_display_cursor (w, 1, hpos, vpos, x, y);
b86bd3dd 1078 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
dc6f92b8
JB
1079 UNBLOCK_INPUT;
1080 }
1081}
dc43ef94 1082
06a2c219
GM
1083
1084\f
1085/***********************************************************************
1086 Display Iterator
1087 ***********************************************************************/
1088
1089/* Function prototypes of this page. */
1090
1091static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1092 struct glyph *,
1093 XChar2b *));
1094static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1095 int, XChar2b *, int));
1096static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1097static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1098static void x_append_glyph P_ ((struct it *));
b4192550 1099static void x_append_composite_glyph P_ ((struct it *));
06a2c219
GM
1100static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1101 int, int, double));
1102static void x_produce_glyphs P_ ((struct it *));
06a2c219
GM
1103static void x_produce_image_glyph P_ ((struct it *it));
1104
1105
1106/* Return a pointer to per-char metric information in FONT of a
1107 character pointed by B which is a pointer to an XChar2b. */
dc43ef94
KH
1108
1109#define PER_CHAR_METRIC(font, b) \
1110 ((font)->per_char \
1111 ? ((font)->per_char + (b)->byte2 - (font)->min_char_or_byte2 \
1112 + (((font)->min_byte1 || (font)->max_byte1) \
1113 ? (((b)->byte1 - (font)->min_byte1) \
1114 * ((font)->max_char_or_byte2 - (font)->min_char_or_byte2 + 1)) \
1115 : 0)) \
1116 : &((font)->max_bounds))
1117
dc6f92b8 1118
06a2c219
GM
1119/* Get metrics of character CHAR2B in FONT. Value is always non-null.
1120 If CHAR2B is not contained in FONT, the font's default character
1121 metric is returned. */
dc43ef94 1122
06a2c219
GM
1123static INLINE XCharStruct *
1124x_per_char_metric (font, char2b)
1125 XFontStruct *font;
1126 XChar2b *char2b;
1127{
1128 /* The result metric information. */
1129 XCharStruct *pcm = NULL;
dc6f92b8 1130
06a2c219 1131 xassert (font && char2b);
dc6f92b8 1132
06a2c219 1133 if (font->per_char != NULL)
dc6f92b8 1134 {
06a2c219 1135 if (font->min_byte1 == 0 && font->max_byte1 == 0)
dc43ef94 1136 {
06a2c219
GM
1137 /* min_char_or_byte2 specifies the linear character index
1138 corresponding to the first element of the per_char array,
1139 max_char_or_byte2 is the index of the last character. A
1140 character with non-zero CHAR2B->byte1 is not in the font.
1141 A character with byte2 less than min_char_or_byte2 or
1142 greater max_char_or_byte2 is not in the font. */
1143 if (char2b->byte1 == 0
1144 && char2b->byte2 >= font->min_char_or_byte2
1145 && char2b->byte2 <= font->max_char_or_byte2)
1146 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
dc43ef94 1147 }
06a2c219 1148 else
dc6f92b8 1149 {
06a2c219
GM
1150 /* If either min_byte1 or max_byte1 are nonzero, both
1151 min_char_or_byte2 and max_char_or_byte2 are less than
1152 256, and the 2-byte character index values corresponding
1153 to the per_char array element N (counting from 0) are:
1154
1155 byte1 = N/D + min_byte1
1156 byte2 = N\D + min_char_or_byte2
1157
1158 where:
1159
1160 D = max_char_or_byte2 - min_char_or_byte2 + 1
1161 / = integer division
1162 \ = integer modulus */
1163 if (char2b->byte1 >= font->min_byte1
1164 && char2b->byte1 <= font->max_byte1
1165 && char2b->byte2 >= font->min_char_or_byte2
1166 && char2b->byte2 <= font->max_char_or_byte2)
1167 {
1168 pcm = (font->per_char
1169 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1170 * (char2b->byte1 - font->min_byte1))
1171 + (char2b->byte2 - font->min_char_or_byte2));
1172 }
dc6f92b8 1173 }
06a2c219
GM
1174 }
1175 else
1176 {
1177 /* If the per_char pointer is null, all glyphs between the first
1178 and last character indexes inclusive have the same
1179 information, as given by both min_bounds and max_bounds. */
1180 if (char2b->byte2 >= font->min_char_or_byte2
1181 && char2b->byte2 <= font->max_char_or_byte2)
1182 pcm = &font->max_bounds;
1183 }
dc6f92b8 1184
4a4dc352 1185
06a2c219
GM
1186 if (pcm == NULL || pcm->width == 0)
1187 {
1188 /* Character not contained in the font. FONT->default_char
1189 gives the character that will be printed. FONT->default_char
1190 is a 16-bit character code with byte1 in the most significant
1191 byte and byte2 in the least significant byte. */
1192 XChar2b default_char;
1193 default_char.byte1 = (font->default_char >> BITS_PER_CHAR) & 0xff;
1194 default_char.byte2 = font->default_char & 0xff;
1195
1196 /* Avoid an endless recursion if FONT->default_char itself
1197 hasn't per char metrics. handa@etl.go.jp reports that some
1198 fonts have this problem. */
1199 if (default_char.byte1 != char2b->byte1
1200 || default_char.byte2 != char2b->byte2)
1201 pcm = x_per_char_metric (font, &default_char);
1202 else
1203 pcm = &font->max_bounds;
1204 }
1205
1206 return pcm;
1207}
b73b6aaf 1208
57b03282 1209
06a2c219
GM
1210/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1211 the two-byte form of C. Encoding is returned in *CHAR2B. */
dc43ef94 1212
06a2c219
GM
1213static INLINE void
1214x_encode_char (c, char2b, font_info)
1215 int c;
1216 XChar2b *char2b;
1217 struct font_info *font_info;
1218{
1219 int charset = CHAR_CHARSET (c);
1220 XFontStruct *font = font_info->font;
1221
1222 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1223 This may be either a program in a special encoder language or a
1224 fixed encoding. */
1225 if (font_info->font_encoder)
1226 {
1227 /* It's a program. */
1228 struct ccl_program *ccl = font_info->font_encoder;
1229
1230 if (CHARSET_DIMENSION (charset) == 1)
1231 {
1232 ccl->reg[0] = charset;
1233 ccl->reg[1] = char2b->byte2;
1234 }
1235 else
1236 {
1237 ccl->reg[0] = charset;
1238 ccl->reg[1] = char2b->byte1;
1239 ccl->reg[2] = char2b->byte2;
1240 }
1241
1242 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1243
1244 /* We assume that MSBs are appropriately set/reset by CCL
1245 program. */
1246 if (font->max_byte1 == 0) /* 1-byte font */
1247 char2b->byte2 = ccl->reg[1];
1248 else
1249 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1250 }
1251 else if (font_info->encoding[charset])
1252 {
1253 /* Fixed encoding scheme. See fontset.h for the meaning of the
1254 encoding numbers. */
1255 int enc = font_info->encoding[charset];
1256
1257 if ((enc == 1 || enc == 2)
1258 && CHARSET_DIMENSION (charset) == 2)
1259 char2b->byte1 |= 0x80;
1260
1261 if (enc == 1 || enc == 3)
1262 char2b->byte2 |= 0x80;
1263 }
1264}
1265
1266
1267/* Get face and two-byte form of character C in face FACE_ID on frame
1268 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1269 means we want to display multibyte text. Value is a pointer to a
1270 realized face that is ready for display. */
1271
1272static INLINE struct face *
1273x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1274 struct frame *f;
1275 int c, face_id;
1276 XChar2b *char2b;
1277 int multibyte_p;
1278{
1279 struct face *face = FACE_FROM_ID (f, face_id);
1280
1281 if (!multibyte_p)
1282 {
1283 /* Unibyte case. We don't have to encode, but we have to make
1284 sure to use a face suitable for unibyte. */
1285 char2b->byte1 = 0;
1286 char2b->byte2 = c;
1287
1288 if (!FACE_SUITABLE_FOR_CHARSET_P (face, -1))
1289 {
1290 face_id = FACE_FOR_CHARSET (f, face_id, -1);
1291 face = FACE_FROM_ID (f, face_id);
1292 }
1293 }
1294 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1295 {
1296 /* Case of ASCII in a face known to fit ASCII. */
1297 char2b->byte1 = 0;
1298 char2b->byte2 = c;
1299 }
1300 else
1301 {
1302 int c1, c2, charset;
1303
1304 /* Split characters into bytes. If c2 is -1 afterwards, C is
1305 really a one-byte character so that byte1 is zero. */
1306 SPLIT_CHAR (c, charset, c1, c2);
1307 if (c2 > 0)
1308 char2b->byte1 = c1, char2b->byte2 = c2;
1309 else
1310 char2b->byte1 = 0, char2b->byte2 = c1;
1311
1312 /* Get the face for displaying C. If `face' is not suitable for
1313 charset, get the one that fits. (This can happen for the
b4192550
KH
1314 translations of a composition where the glyph
1315 specifies a face for the first component, but the other
1316 components have a different charset.) */
06a2c219
GM
1317 if (!FACE_SUITABLE_FOR_CHARSET_P (face, charset))
1318 {
1319 face_id = FACE_FOR_CHARSET (f, face_id, charset);
1320 face = FACE_FROM_ID (f, face_id);
1321 }
1322
1323 /* Maybe encode the character in *CHAR2B. */
1324 if (charset != CHARSET_ASCII)
1325 {
1326 struct font_info *font_info
1327 = FONT_INFO_FROM_ID (f, face->font_info_id);
1328 if (font_info)
1329 {
1330 x_encode_char (c, char2b, font_info);
1331 if (charset == charset_latin_iso8859_1)
1332 {
1333 xassert (((XFontStruct *) font_info->font)->max_char_or_byte2
1334 >= 0x80);
1335 char2b->byte2 |= 0x80;
1336 }
1337 }
1338 }
1339 }
1340
1341 /* Make sure X resources of the face are allocated. */
1342 xassert (face != NULL);
1343 PREPARE_FACE_FOR_DISPLAY (f, face);
1344
1345 return face;
1346}
1347
1348
1349/* Get face and two-byte form of character glyph GLYPH on frame F.
1350 The encoding of GLYPH->u.ch.code is returned in *CHAR2B. Value is
1351 a pointer to a realized face that is ready for display. */
1352
1353static INLINE struct face *
1354x_get_glyph_face_and_encoding (f, glyph, char2b)
1355 struct frame *f;
1356 struct glyph *glyph;
1357 XChar2b *char2b;
1358{
1359 struct face *face;
1360
1361 xassert (glyph->type == CHAR_GLYPH);
1362 face = FACE_FROM_ID (f, glyph->u.ch.face_id);
1363
1364 if (!glyph->multibyte_p)
1365 {
1366 /* Unibyte case. We don't have to encode, but we have to make
1367 sure to use a face suitable for unibyte. */
1368 char2b->byte1 = 0;
1369 char2b->byte2 = glyph->u.ch.code;
1370 }
1371 else if (glyph->u.ch.code < 128
1372 && glyph->u.ch.face_id < BASIC_FACE_ID_SENTINEL)
1373 {
1374 /* Case of ASCII in a face known to fit ASCII. */
1375 char2b->byte1 = 0;
1376 char2b->byte2 = glyph->u.ch.code;
1377 }
1378 else
1379 {
1380 int c1, c2, charset;
1381
1382 /* Split characters into bytes. If c2 is -1 afterwards, C is
1383 really a one-byte character so that byte1 is zero. */
1384 SPLIT_CHAR (glyph->u.ch.code, charset, c1, c2);
1385 if (c2 > 0)
1386 char2b->byte1 = c1, char2b->byte2 = c2;
1387 else
1388 char2b->byte1 = 0, char2b->byte2 = c1;
1389
1390 /* Maybe encode the character in *CHAR2B. */
1391 if (charset != CHARSET_ASCII)
1392 {
1393 struct font_info *font_info
1394 = FONT_INFO_FROM_ID (f, face->font_info_id);
1395 if (font_info)
1396 {
1397 x_encode_char (glyph->u.ch.code, char2b, font_info);
1398 if (charset == charset_latin_iso8859_1)
1399 char2b->byte2 |= 0x80;
1400 }
1401 }
1402 }
1403
1404 /* Make sure X resources of the face are allocated. */
1405 xassert (face != NULL);
1406 PREPARE_FACE_FOR_DISPLAY (f, face);
1407 return face;
1408}
1409
1410
1411/* Store one glyph for IT->char_to_display in IT->glyph_row.
1412 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1413
1414static INLINE void
1415x_append_glyph (it)
1416 struct it *it;
1417{
1418 struct glyph *glyph;
1419 enum glyph_row_area area = it->area;
1420
1421 xassert (it->glyph_row);
1422 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1423
1424 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1425 if (glyph < it->glyph_row->glyphs[area + 1])
1426 {
1427 /* Play it safe. If sub-structures of the glyph are not all the
1428 same size, it otherwise be that some bits stay set. This
1429 would prevent a comparison with GLYPH_EQUAL_P. */
1430 glyph->u.val = 0;
1431
1432 glyph->type = CHAR_GLYPH;
1433 glyph->pixel_width = it->pixel_width;
1434 glyph->u.ch.code = it->char_to_display;
1435 glyph->u.ch.face_id = it->face_id;
1436 glyph->charpos = CHARPOS (it->position);
1437 glyph->object = it->object;
1438 glyph->left_box_line_p = it->start_of_box_run_p;
1439 glyph->right_box_line_p = it->end_of_box_run_p;
1440 glyph->voffset = it->voffset;
1441 glyph->multibyte_p = it->multibyte_p;
66ac4b0e
GM
1442 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1443 || it->phys_descent > it->descent);
06a2c219
GM
1444 ++it->glyph_row->used[area];
1445 }
1446}
1447
b4192550
KH
1448/* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1449 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1450
1451static INLINE void
1452x_append_composite_glyph (it)
1453 struct it *it;
1454{
1455 struct glyph *glyph;
1456 enum glyph_row_area area = it->area;
1457
1458 xassert (it->glyph_row);
1459
1460 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1461 if (glyph < it->glyph_row->glyphs[area + 1])
1462 {
1463 /* Play it safe. If sub-structures of the glyph are not all the
1464 same size, it otherwise be that some bits stay set. This
1465 would prevent a comparison with GLYPH_EQUAL_P. */
1466 glyph->u.val = 0;
1467
1468 glyph->type = COMPOSITE_GLYPH;
1469 glyph->pixel_width = it->pixel_width;
1470 glyph->u.cmp.id = it->cmp_id;
1471 glyph->u.cmp.face_id = it->face_id;
1472 glyph->charpos = CHARPOS (it->position);
1473 glyph->object = it->object;
1474 glyph->left_box_line_p = it->start_of_box_run_p;
1475 glyph->right_box_line_p = it->end_of_box_run_p;
1476 glyph->voffset = it->voffset;
1477 glyph->multibyte_p = it->multibyte_p;
1478 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1479 || it->phys_descent > it->descent);
1480 ++it->glyph_row->used[area];
1481 }
1482}
1483
06a2c219
GM
1484
1485/* Change IT->ascent and IT->height according to the setting of
1486 IT->voffset. */
1487
1488static INLINE void
1489take_vertical_position_into_account (it)
1490 struct it *it;
1491{
1492 if (it->voffset)
1493 {
1494 if (it->voffset < 0)
1495 /* Increase the ascent so that we can display the text higher
1496 in the line. */
1497 it->ascent += abs (it->voffset);
1498 else
1499 /* Increase the descent so that we can display the text lower
1500 in the line. */
1501 it->descent += it->voffset;
1502 }
1503}
1504
1505
1506/* Produce glyphs/get display metrics for the image IT is loaded with.
1507 See the description of struct display_iterator in dispextern.h for
1508 an overview of struct display_iterator. */
1509
1510static void
1511x_produce_image_glyph (it)
1512 struct it *it;
1513{
1514 struct image *img;
1515 struct face *face;
1516
1517 xassert (it->what == IT_IMAGE);
1518
1519 face = FACE_FROM_ID (it->f, it->face_id);
1520 img = IMAGE_FROM_ID (it->f, it->image_id);
1521 xassert (img);
1522
1523 /* Make sure X resources of the face and image are loaded. */
1524 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1525 prepare_image_for_display (it->f, img);
1526
66ac4b0e
GM
1527 it->ascent = it->phys_ascent = IMAGE_ASCENT (img);
1528 it->descent = it->phys_descent = img->height + 2 * img->margin - it->ascent;
06a2c219
GM
1529 it->pixel_width = img->width + 2 * img->margin;
1530
1531 it->nglyphs = 1;
1532
1533 if (face->box != FACE_NO_BOX)
1534 {
1535 it->ascent += face->box_line_width;
1536 it->descent += face->box_line_width;
1537
1538 if (it->start_of_box_run_p)
1539 it->pixel_width += face->box_line_width;
1540 if (it->end_of_box_run_p)
1541 it->pixel_width += face->box_line_width;
1542 }
1543
1544 take_vertical_position_into_account (it);
1545
1546 if (it->glyph_row)
1547 {
1548 struct glyph *glyph;
1549 enum glyph_row_area area = it->area;
1550
1551 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1552 if (glyph < it->glyph_row->glyphs[area + 1])
1553 {
1554 glyph->type = IMAGE_GLYPH;
1555 glyph->u.img.id = img->id;
1556 glyph->u.img.face_id = it->face_id;
1557 glyph->pixel_width = it->pixel_width;
1558 glyph->charpos = CHARPOS (it->position);
1559 glyph->object = it->object;
1560 glyph->left_box_line_p = it->start_of_box_run_p;
1561 glyph->right_box_line_p = it->end_of_box_run_p;
1562 glyph->voffset = it->voffset;
1563 glyph->multibyte_p = it->multibyte_p;
1564 ++it->glyph_row->used[area];
1565 }
1566 }
1567}
1568
1569
1570/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1571 of the glyph, WIDTH and HEIGHT are the width and height of the
1572 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1573 ascent of the glyph (0 <= ASCENT <= 1). */
1574
1575static void
1576x_append_stretch_glyph (it, object, width, height, ascent)
1577 struct it *it;
1578 Lisp_Object object;
1579 int width, height;
1580 double ascent;
1581{
1582 struct glyph *glyph;
1583 enum glyph_row_area area = it->area;
1584
1585 xassert (ascent >= 0 && ascent <= 1);
1586
1587 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1588 if (glyph < it->glyph_row->glyphs[area + 1])
1589 {
1590 glyph->type = STRETCH_GLYPH;
1591 glyph->u.stretch.ascent = height * ascent;
1592 glyph->u.stretch.height = height;
1593 glyph->u.stretch.face_id = it->face_id;
1594 glyph->pixel_width = width;
1595 glyph->charpos = CHARPOS (it->position);
1596 glyph->object = object;
1597 glyph->left_box_line_p = it->start_of_box_run_p;
1598 glyph->right_box_line_p = it->end_of_box_run_p;
1599 glyph->voffset = it->voffset;
1600 glyph->multibyte_p = it->multibyte_p;
1601 ++it->glyph_row->used[area];
1602 }
1603}
1604
1605
1606/* Produce a stretch glyph for iterator IT. IT->object is the value
1607 of the glyph property displayed. The value must be a list
1608 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1609 being recognized:
1610
1611 1. `:width WIDTH' specifies that the space should be WIDTH *
1612 canonical char width wide. WIDTH may be an integer or floating
1613 point number.
1614
1615 2. `:relative-width FACTOR' specifies that the width of the stretch
1616 should be computed from the width of the first character having the
1617 `glyph' property, and should be FACTOR times that width.
1618
1619 3. `:align-to HPOS' specifies that the space should be wide enough
1620 to reach HPOS, a value in canonical character units.
1621
1622 Exactly one of the above pairs must be present.
1623
1624 4. `:height HEIGHT' specifies that the height of the stretch produced
1625 should be HEIGHT, measured in canonical character units.
1626
1627 5. `:relative-height FACTOR' specifies that the height of the the
1628 stretch should be FACTOR times the height of the characters having
1629 the glyph property.
1630
1631 Either none or exactly one of 4 or 5 must be present.
1632
1633 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1634 of the stretch should be used for the ascent of the stretch.
1635 ASCENT must be in the range 0 <= ASCENT <= 100. */
1636
1637#define NUMVAL(X) \
1638 ((INTEGERP (X) || FLOATP (X)) \
1639 ? XFLOATINT (X) \
1640 : - 1)
1641
1642
1643static void
1644x_produce_stretch_glyph (it)
1645 struct it *it;
1646{
1647 /* (space :width WIDTH :height HEIGHT. */
1648 extern Lisp_Object QCwidth, QCheight, QCascent, Qspace;
1649 extern Lisp_Object QCrelative_width, QCrelative_height;
1650 extern Lisp_Object QCalign_to;
1651 Lisp_Object prop, plist;
1652 double width = 0, height = 0, ascent = 0;
1653 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1654 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1655
1656 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1657
1658 /* List should start with `space'. */
1659 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1660 plist = XCDR (it->object);
1661
1662 /* Compute the width of the stretch. */
1663 if (prop = Fplist_get (plist, QCwidth),
1664 NUMVAL (prop) > 0)
1665 /* Absolute width `:width WIDTH' specified and valid. */
1666 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1667 else if (prop = Fplist_get (plist, QCrelative_width),
1668 NUMVAL (prop) > 0)
1669 {
1670 /* Relative width `:relative-width FACTOR' specified and valid.
1671 Compute the width of the characters having the `glyph'
1672 property. */
1673 struct it it2;
1674 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1675
1676 it2 = *it;
1677 if (it->multibyte_p)
1678 {
1679 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1680 - IT_BYTEPOS (*it));
1681 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1682 }
1683 else
1684 it2.c = *p, it2.len = 1;
1685
1686 it2.glyph_row = NULL;
1687 it2.what = IT_CHARACTER;
1688 x_produce_glyphs (&it2);
1689 width = NUMVAL (prop) * it2.pixel_width;
1690 }
1691 else if (prop = Fplist_get (plist, QCalign_to),
1692 NUMVAL (prop) > 0)
1693 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1694 else
1695 /* Nothing specified -> width defaults to canonical char width. */
1696 width = CANON_X_UNIT (it->f);
1697
1698 /* Compute height. */
1699 if (prop = Fplist_get (plist, QCheight),
1700 NUMVAL (prop) > 0)
1701 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1702 else if (prop = Fplist_get (plist, QCrelative_height),
1703 NUMVAL (prop) > 0)
1704 height = FONT_HEIGHT (font) * NUMVAL (prop);
1705 else
1706 height = FONT_HEIGHT (font);
1707
1708 /* Compute percentage of height used for ascent. If
1709 `:ascent ASCENT' is present and valid, use that. Otherwise,
1710 derive the ascent from the font in use. */
1711 if (prop = Fplist_get (plist, QCascent),
1712 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1713 ascent = NUMVAL (prop) / 100.0;
1714 else
1715 ascent = (double) font->ascent / FONT_HEIGHT (font);
1716
1717 if (width <= 0)
1718 width = 1;
1719 if (height <= 0)
1720 height = 1;
1721
1722 if (it->glyph_row)
1723 {
1724 Lisp_Object object = it->stack[it->sp - 1].string;
1725 if (!STRINGP (object))
1726 object = it->w->buffer;
1727 x_append_stretch_glyph (it, object, width, height, ascent);
1728 }
1729
1730 it->pixel_width = width;
66ac4b0e
GM
1731 it->ascent = it->phys_ascent = height * ascent;
1732 it->descent = it->phys_descent = height - it->ascent;
06a2c219
GM
1733 it->nglyphs = 1;
1734
1735 if (face->box != FACE_NO_BOX)
1736 {
1737 it->ascent += face->box_line_width;
1738 it->descent += face->box_line_width;
1739
1740 if (it->start_of_box_run_p)
1741 it->pixel_width += face->box_line_width;
1742 if (it->end_of_box_run_p)
1743 it->pixel_width += face->box_line_width;
1744 }
1745
1746 take_vertical_position_into_account (it);
1747}
1748
b4192550
KH
1749/* Return proper value to be used as baseline offset of font that has
1750 ASCENT and DESCENT to draw characters by the font at the vertical
1751 center of the line of frame F.
1752
1753 Here, out task is to find the value of BOFF in the following figure;
1754
1755 -------------------------+-----------+-
1756 -+-+---------+-+ | |
1757 | | | | | |
1758 | | | | F_ASCENT F_HEIGHT
1759 | | | ASCENT | |
1760 HEIGHT | | | | |
1761 | | |-|-+------+-----------|------- baseline
1762 | | | | BOFF | |
1763 | |---------|-+-+ | |
1764 | | | DESCENT | |
1765 -+-+---------+-+ F_DESCENT |
1766 -------------------------+-----------+-
1767
1768 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1769 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1770 DESCENT = FONT->descent
1771 HEIGHT = FONT_HEIGHT (FONT)
1772 F_DESCENT = (F->output_data.x->font->descent
1773 - F->output_data.x->baseline_offset)
1774 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1775*/
1776
1777#define VCENTER_BASELINE_OFFSET(FONT, F) \
1778 ((FONT)->descent \
1779 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT))) / 2 \
1780 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
06a2c219
GM
1781
1782/* Produce glyphs/get display metrics for the display element IT is
1783 loaded with. See the description of struct display_iterator in
1784 dispextern.h for an overview of struct display_iterator. */
1785
1786static void
1787x_produce_glyphs (it)
1788 struct it *it;
1789{
1790 if (it->what == IT_CHARACTER)
1791 {
1792 XChar2b char2b;
1793 XFontStruct *font;
1794 struct face *face;
1795 XCharStruct *pcm;
06a2c219 1796 int font_not_found_p;
b4192550
KH
1797 struct font_info *font_info;
1798 int boff; /* baseline offset */
06a2c219
GM
1799
1800 /* Maybe translate single-byte characters to multibyte. */
1801 it->char_to_display = it->c;
1802 if (unibyte_display_via_language_environment
1803 && SINGLE_BYTE_CHAR_P (it->c)
1804 && (it->c >= 0240
1805 || (it->c >= 0200
1806 && !NILP (Vnonascii_translation_table))))
1807 {
1808 it->char_to_display = unibyte_char_to_multibyte (it->c);
1809 it->charset = CHAR_CHARSET (it->char_to_display);
1810 }
1811
1812 /* Get face and font to use. Encode IT->char_to_display. */
1813 face = x_get_char_face_and_encoding (it->f, it->char_to_display,
1814 it->face_id, &char2b,
1815 it->multibyte_p);
1816 font = face->font;
1817
1818 /* When no suitable font found, use the default font. */
1819 font_not_found_p = font == NULL;
1820 if (font_not_found_p)
b4192550
KH
1821 {
1822 font = FRAME_FONT (it->f);
1823 boff = it->f->output_data.x->baseline_offset;
1824 font_info = NULL;
1825 }
1826 else
1827 {
1828 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1829 boff = font_info->baseline_offset;
1830 if (font_info->vertical_centering)
1831 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1832 }
06a2c219
GM
1833
1834 if (it->char_to_display >= ' '
1835 && (!it->multibyte_p || it->char_to_display < 128))
1836 {
1837 /* Either unibyte or ASCII. */
1838 int stretched_p;
1839
1840 it->nglyphs = 1;
06a2c219
GM
1841
1842 pcm = x_per_char_metric (font, &char2b);
b4192550
KH
1843 it->ascent = font->ascent + boff;
1844 it->descent = font->descent - boff;
1845 it->phys_ascent = pcm->ascent + boff;
1846 it->phys_descent = pcm->descent - boff;
06a2c219
GM
1847 it->pixel_width = pcm->width;
1848
1849 /* If this is a space inside a region of text with
1850 `space-width' property, change its width. */
1851 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1852 if (stretched_p)
1853 it->pixel_width *= XFLOATINT (it->space_width);
1854
1855 /* If face has a box, add the box thickness to the character
1856 height. If character has a box line to the left and/or
1857 right, add the box line width to the character's width. */
1858 if (face->box != FACE_NO_BOX)
1859 {
1860 int thick = face->box_line_width;
1861
1862 it->ascent += thick;
1863 it->descent += thick;
1864
1865 if (it->start_of_box_run_p)
1866 it->pixel_width += thick;
1867 if (it->end_of_box_run_p)
1868 it->pixel_width += thick;
1869 }
1870
1871 /* If face has an overline, add the height of the overline
1872 (1 pixel) and a 1 pixel margin to the character height. */
1873 if (face->overline_p)
1874 it->ascent += 2;
1875
1876 take_vertical_position_into_account (it);
1877
1878 /* If we have to actually produce glyphs, do it. */
1879 if (it->glyph_row)
1880 {
1881 if (stretched_p)
1882 {
1883 /* Translate a space with a `space-width' property
1884 into a stretch glyph. */
1885 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1886 x_append_stretch_glyph (it, it->object, it->pixel_width,
1887 it->ascent + it->descent, ascent);
1888 }
1889 else
1890 x_append_glyph (it);
1891
1892 /* If characters with lbearing or rbearing are displayed
1893 in this line, record that fact in a flag of the
1894 glyph row. This is used to optimize X output code. */
1895 if (pcm->lbearing < 0
1896 || pcm->rbearing > pcm->width)
1897 it->glyph_row->contains_overlapping_glyphs_p = 1;
1898 }
1899 }
1900 else if (it->char_to_display == '\n')
1901 {
1902 /* A newline has no width but we need the height of the line. */
1903 it->pixel_width = 0;
1904 it->nglyphs = 0;
b4192550
KH
1905 it->ascent = it->phys_ascent = font->ascent + boff;
1906 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1907
1908 if (face->box != FACE_NO_BOX)
1909 {
1910 int thick = face->box_line_width;
1911 it->ascent += thick;
1912 it->descent += thick;
1913 }
1914 }
1915 else if (it->char_to_display == '\t')
1916 {
1917 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1918 int x = (it->current_x
1919 - it->prompt_width
1920 + it->continuation_lines_width);
1921 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1922
1923 it->pixel_width = next_tab_x - x;
1924 it->nglyphs = 1;
b4192550
KH
1925 it->ascent = it->phys_ascent = font->ascent + boff;
1926 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1927
1928 if (it->glyph_row)
1929 {
1930 double ascent = (double) it->ascent / (it->ascent + it->descent);
1931 x_append_stretch_glyph (it, it->object, it->pixel_width,
1932 it->ascent + it->descent, ascent);
1933 }
1934 }
1935 else
1936 {
1937 /* A multi-byte character. Assume that the display width of the
1938 character is the width of the character multiplied by the
b4192550 1939 width of the font. */
06a2c219 1940
b4192550
KH
1941 /* If we found a font, this font should give us the right
1942 metrics. If we didn't find a font, use the frame's
1943 default font and calculate the width of the character
1944 from the charset width; this is what old redisplay code
1945 did. */
1946 pcm = x_per_char_metric (font, &char2b);
1947 it->pixel_width = pcm->width;
1948 if (font_not_found_p)
1949 it->pixel_width *= CHARSET_WIDTH (it->charset);
1950 it->nglyphs = 1;
1951 it->ascent = font->ascent + boff;
1952 it->descent = font->descent - boff;
1953 it->phys_ascent = pcm->ascent + boff;
1954 it->phys_descent = pcm->descent - boff;
1955 if (it->glyph_row
1956 && (pcm->lbearing < 0
1957 || pcm->rbearing > pcm->width))
1958 it->glyph_row->contains_overlapping_glyphs_p = 1;
06a2c219
GM
1959
1960 if (face->box != FACE_NO_BOX)
1961 {
1962 int thick = face->box_line_width;
1963 it->ascent += thick;
1964 it->descent += thick;
1965
1966 if (it->start_of_box_run_p)
1967 it->pixel_width += thick;
1968 if (it->end_of_box_run_p)
1969 it->pixel_width += thick;
1970 }
1971
1972 /* If face has an overline, add the height of the overline
1973 (1 pixel) and a 1 pixel margin to the character height. */
1974 if (face->overline_p)
1975 it->ascent += 2;
1976
1977 take_vertical_position_into_account (it);
1978
1979 if (it->glyph_row)
1980 x_append_glyph (it);
1981 }
1982 }
b4192550
KH
1983 else if (it->what == IT_COMPOSITION)
1984 {
1985 /* Note: A composition is represented as one glyph in the
1986 glyph matrix. There are no padding glyphs. */
1987 XChar2b char2b;
1988 XFontStruct *font;
1989 struct face *face;
1990 XCharStruct *pcm;
1991 int font_not_found_p;
1992 struct font_info *font_info;
1993 int boff; /* baseline offset */
1994 struct composition *cmp = composition_table[it->cmp_id];
1995
1996 /* Maybe translate single-byte characters to multibyte. */
1997 it->char_to_display = it->c;
1998 if (unibyte_display_via_language_environment
1999 && SINGLE_BYTE_CHAR_P (it->c)
2000 && (it->c >= 0240
2001 || (it->c >= 0200
2002 && !NILP (Vnonascii_translation_table))))
2003 {
2004 it->char_to_display = unibyte_char_to_multibyte (it->c);
2005 it->charset = CHAR_CHARSET (it->char_to_display);
2006 }
2007
2008 /* Get face and font to use. Encode IT->char_to_display. */
2009 face = x_get_char_face_and_encoding (it->f, it->char_to_display,
2010 it->face_id, &char2b,
2011 it->multibyte_p);
2012 font = face->font;
2013
2014 /* When no suitable font found, use the default font. */
2015 font_not_found_p = font == NULL;
2016 if (font_not_found_p)
2017 {
2018 font = FRAME_FONT (it->f);
2019 boff = it->f->output_data.x->baseline_offset;
2020 font_info = NULL;
2021 }
2022 else
2023 {
2024 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2025 boff = font_info->baseline_offset;
2026 if (font_info->vertical_centering)
2027 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2028 }
2029
2030 /* There are no padding glyphs, so there is only one glyph to
2031 produce for the composition. Important is that pixel_width,
2032 ascent and descent are the values of what is drawn by
2033 draw_glyphs (i.e. the values of the overall glyphs composed). */
2034 it->nglyphs = 1;
2035
2036 /* If we have not yet calculated pixel size data of glyphs of
2037 the composition for the current face font, calculate them
2038 now. Theoretically, we have to check all fonts for the
2039 glyphs, but that requires much time and memory space. So,
2040 here we check only the font of the first glyph. This leads
2041 to incorrect display very rarely, and C-l (recenter) can
2042 correct the display anyway. */
2043 if (cmp->font != (void *) font)
2044 {
2045 /* Ascent and descent of the font of the first character of
2046 this composition (adjusted by baseline offset). Ascent
2047 and descent of overall glyphs should not be less than
2048 them respectively. */
2049 int font_ascent = font->ascent + boff;
2050 int font_descent = font->descent - boff;
2051 /* Bounding box of the overall glyphs. */
2052 int leftmost, rightmost, lowest, highest;
2053 int i;
2054
2055 cmp->font = (void *) font;
2056
2057 /* Initialize the bounding box. */
2058 pcm = x_per_char_metric (font, &char2b);
2059 leftmost = 0;
2060 rightmost = pcm->width;
2061 lowest = - pcm->descent + boff;
2062 highest = pcm->ascent + boff;
2063 if (font_info
2064 && font_info->default_ascent
2065 && CHAR_TABLE_P (Vuse_default_ascent)
2066 && !NILP (Faref (Vuse_default_ascent,
2067 make_number (it->char_to_display))))
2068 highest = font_info->default_ascent + boff;
2069
2070 /* Draw the first glyph at the normal position. It may be
2071 shifted to right later if some other glyphs are drawn at
2072 the left. */
2073 cmp->offsets[0] = 0;
2074 cmp->offsets[1] = boff;
2075
2076 /* Set cmp->offsets for the remaining glyphs. */
2077 for (i = 1; i < cmp->glyph_len; i++)
2078 {
2079 int left, right, btm, top;
2080 int ch = COMPOSITION_GLYPH (cmp, i);
2081
2082 face = x_get_char_face_and_encoding (it->f, ch,
2083 it->face_id, &char2b,
2084 it->multibyte_p);
2085 font = face->font;
2086 if (font == NULL)
2087 {
2088 font = FRAME_FONT (it->f);
2089 boff = it->f->output_data.x->baseline_offset;
2090 font_info = NULL;
2091 }
2092 else
2093 {
2094 font_info
2095 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2096 boff = font_info->baseline_offset;
2097 if (font_info->vertical_centering)
2098 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2099 }
2100
2101 pcm = x_per_char_metric (font, &char2b);
2102
2103 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2104 {
2105 /* Relative composition with or without
2106 alternate chars. */
2107 left = (leftmost + rightmost - pcm->width) / 2;
2108 btm = - pcm->descent + boff;
2109 if (font_info && font_info->relative_compose
2110 && (! CHAR_TABLE_P (Vignore_relative_composition)
2111 || NILP (Faref (Vignore_relative_composition,
2112 make_number (ch)))))
2113 {
2114
2115 if (- pcm->descent
2116 >= font_info->relative_compose)
2117 /* One extra pixel between two glyphs. */
2118 btm = highest + 1;
2119 else if (pcm->ascent <= 0)
2120 /* One extra pixel between two glyphs. */
2121 btm = lowest - 1 - pcm->ascent - pcm->descent;
2122 }
2123 }
2124 else
2125 {
2126 /* A composition rule is specified by an integer
2127 value that encodes global and new reference
2128 points (GREF and NREF). GREF and NREF are
2129 specified by numbers as below:
2130
2131 0---1---2 -- ascent
2132 | |
2133 | |
2134 | |
2135 9--10--11 -- center
2136 | |
2137 ---3---4---5--- baseline
2138 | |
2139 6---7---8 -- descent
2140 */
2141 int rule = COMPOSITION_RULE (cmp, i);
2142 int gref, nref, grefx, grefy, nrefx, nrefy;
2143
2144 COMPOSITION_DECODE_RULE (rule, gref, nref);
2145 grefx = gref % 3, nrefx = nref % 3;
2146 grefy = gref / 3, nrefy = nref / 3;
2147
2148 left = (leftmost
2149 + grefx * (rightmost - leftmost) / 2
2150 - nrefx * pcm->width / 2);
2151 btm = ((grefy == 0 ? highest
2152 : grefy == 1 ? 0
2153 : grefy == 2 ? lowest
2154 : (highest + lowest) / 2)
2155 - (nrefy == 0 ? pcm->ascent + pcm->descent
2156 : nrefy == 1 ? pcm->descent - boff
2157 : nrefy == 2 ? 0
2158 : (pcm->ascent + pcm->descent) / 2));
2159 }
2160
2161 cmp->offsets[i * 2] = left;
2162 cmp->offsets[i * 2 + 1] = btm + pcm->descent;
2163
2164 /* Update the bounding box of the overall glyphs. */
2165 right = left + pcm->width;
2166 top = btm + pcm->descent + pcm->ascent;
2167 if (left < leftmost)
2168 leftmost = left;
2169 if (right > rightmost)
2170 rightmost = right;
2171 if (top > highest)
2172 highest = top;
2173 if (btm < lowest)
2174 lowest = btm;
2175 }
2176
2177 /* If there are glyphs whose x-offsets are negative,
2178 shift all glyphs to the right and make all x-offsets
2179 non-negative. */
2180 if (leftmost < 0)
2181 {
2182 for (i = 0; i < cmp->glyph_len; i++)
2183 cmp->offsets[i * 2] -= leftmost;
2184 rightmost -= leftmost;
2185 }
2186
2187 cmp->pixel_width = rightmost;
2188 cmp->ascent = highest;
2189 cmp->descent = - lowest;
2190 if (cmp->ascent < font_ascent)
2191 cmp->ascent = font_ascent;
2192 if (cmp->descent < font_descent)
2193 cmp->descent = font_descent;
2194 }
2195
2196 it->pixel_width = cmp->pixel_width;
2197 it->ascent = it->phys_ascent = cmp->ascent;
2198 it->descent = it->phys_descent = cmp->descent;
2199
2200 if (face->box != FACE_NO_BOX)
2201 {
2202 int thick = face->box_line_width;
2203 it->ascent += thick;
2204 it->descent += thick;
2205
2206 if (it->start_of_box_run_p)
2207 it->pixel_width += thick;
2208 if (it->end_of_box_run_p)
2209 it->pixel_width += thick;
2210 }
2211
2212 /* If face has an overline, add the height of the overline
2213 (1 pixel) and a 1 pixel margin to the character height. */
2214 if (face->overline_p)
2215 it->ascent += 2;
2216
2217 take_vertical_position_into_account (it);
2218
2219 if (it->glyph_row)
2220 x_append_composite_glyph (it);
2221 }
06a2c219
GM
2222 else if (it->what == IT_IMAGE)
2223 x_produce_image_glyph (it);
2224 else if (it->what == IT_STRETCH)
2225 x_produce_stretch_glyph (it);
2226
2227 /* Accumulate dimensions. */
2228 xassert (it->ascent >= 0 && it->descent > 0);
2229 if (it->area == TEXT_AREA)
2230 it->current_x += it->pixel_width;
66ac4b0e 2231
06a2c219
GM
2232 it->max_ascent = max (it->max_ascent, it->ascent);
2233 it->max_descent = max (it->max_descent, it->descent);
66ac4b0e
GM
2234 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2235 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
06a2c219
GM
2236}
2237
2238
2239/* Estimate the pixel height of the mode or top line on frame F.
2240 FACE_ID specifies what line's height to estimate. */
2241
2242int
2243x_estimate_mode_line_height (f, face_id)
2244 struct frame *f;
2245 enum face_id face_id;
2246{
2247 int height = 1;
2248
2249 /* This function is called so early when Emacs starts that the face
2250 cache and mode line face are not yet initialized. */
2251 if (FRAME_FACE_CACHE (f))
2252 {
2253 struct face *face = FACE_FROM_ID (f, face_id);
2254 if (face)
2255 height = FONT_HEIGHT (face->font) + 2 * face->box_line_width;
2256 }
2257
2258 return height;
2259}
2260
2261\f
2262/***********************************************************************
2263 Glyph display
2264 ***********************************************************************/
2265
2266/* A sequence of glyphs to be drawn in the same face.
2267
2268 This data structure is not really completely X specific, so it
2269 could possibly, at least partially, be useful for other systems. It
2270 is currently not part of the external redisplay interface because
2271 it's not clear what other systems will need. */
2272
2273struct glyph_string
2274{
2275 /* X-origin of the string. */
2276 int x;
2277
2278 /* Y-origin and y-position of the base line of this string. */
2279 int y, ybase;
2280
2281 /* The width of the string, not including a face extension. */
2282 int width;
2283
2284 /* The width of the string, including a face extension. */
2285 int background_width;
2286
2287 /* The height of this string. This is the height of the line this
2288 string is drawn in, and can be different from the height of the
2289 font the string is drawn in. */
2290 int height;
2291
2292 /* Number of pixels this string overwrites in front of its x-origin.
2293 This number is zero if the string has an lbearing >= 0; it is
2294 -lbearing, if the string has an lbearing < 0. */
2295 int left_overhang;
2296
2297 /* Number of pixels this string overwrites past its right-most
2298 nominal x-position, i.e. x + width. Zero if the string's
2299 rbearing is <= its nominal width, rbearing - width otherwise. */
2300 int right_overhang;
2301
2302 /* The frame on which the glyph string is drawn. */
2303 struct frame *f;
2304
2305 /* The window on which the glyph string is drawn. */
2306 struct window *w;
2307
2308 /* X display and window for convenience. */
2309 Display *display;
2310 Window window;
2311
2312 /* The glyph row for which this string was built. It determines the
2313 y-origin and height of the string. */
2314 struct glyph_row *row;
2315
2316 /* The area within row. */
2317 enum glyph_row_area area;
2318
2319 /* Characters to be drawn, and number of characters. */
2320 XChar2b *char2b;
2321 int nchars;
2322
2323 /* Character set of this glyph string. */
2324 int charset;
2325
2326 /* A face-override for drawing cursors, mouse face and similar. */
2327 enum draw_glyphs_face hl;
2328
2329 /* Face in which this string is to be drawn. */
2330 struct face *face;
2331
2332 /* Font in which this string is to be drawn. */
2333 XFontStruct *font;
2334
2335 /* Font info for this string. */
2336 struct font_info *font_info;
2337
b4192550
KH
2338 /* Non-null means this string describes (part of) a composition.
2339 All characters from char2b are drawn composed. */
2340 struct composition *cmp;
06a2c219
GM
2341
2342 /* Index of this glyph string's first character in the glyph
b4192550
KH
2343 definition of CMP. If this is zero, this glyph string describes
2344 the first character of a composition. */
06a2c219
GM
2345 int gidx;
2346
2347 /* 1 means this glyph strings face has to be drawn to the right end
2348 of the window's drawing area. */
2349 unsigned extends_to_end_of_line_p : 1;
2350
2351 /* 1 means the background of this string has been drawn. */
2352 unsigned background_filled_p : 1;
2353
2354 /* 1 means glyph string must be drawn with 16-bit functions. */
2355 unsigned two_byte_p : 1;
2356
2357 /* 1 means that the original font determined for drawing this glyph
2358 string could not be loaded. The member `font' has been set to
2359 the frame's default font in this case. */
2360 unsigned font_not_found_p : 1;
2361
2362 /* 1 means that the face in which this glyph string is drawn has a
2363 stipple pattern. */
2364 unsigned stippled_p : 1;
2365
66ac4b0e
GM
2366 /* 1 means only the foreground of this glyph string must be drawn,
2367 and we should use the physical height of the line this glyph
2368 string appears in as clip rect. */
2369 unsigned for_overlaps_p : 1;
2370
06a2c219
GM
2371 /* The GC to use for drawing this glyph string. */
2372 GC gc;
2373
2374 /* A pointer to the first glyph in the string. This glyph
2375 corresponds to char2b[0]. Needed to draw rectangles if
2376 font_not_found_p is 1. */
2377 struct glyph *first_glyph;
2378
2379 /* Image, if any. */
2380 struct image *img;
2381
2382 struct glyph_string *next, *prev;
2383};
2384
2385
5c187dee 2386#if 0
06a2c219
GM
2387
2388static void
2389x_dump_glyph_string (s)
2390 struct glyph_string *s;
2391{
2392 fprintf (stderr, "glyph string\n");
2393 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2394 s->x, s->y, s->width, s->height);
2395 fprintf (stderr, " ybase = %d\n", s->ybase);
2396 fprintf (stderr, " hl = %d\n", s->hl);
2397 fprintf (stderr, " left overhang = %d, right = %d\n",
2398 s->left_overhang, s->right_overhang);
2399 fprintf (stderr, " nchars = %d\n", s->nchars);
2400 fprintf (stderr, " extends to end of line = %d\n",
2401 s->extends_to_end_of_line_p);
2402 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2403 fprintf (stderr, " bg width = %d\n", s->background_width);
2404}
2405
2406#endif /* GLYPH_DEBUG */
2407
2408
2409
2410static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2411 struct glyph_string **,
2412 struct glyph_string *,
2413 struct glyph_string *));
2414static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2415 struct glyph_string **,
2416 struct glyph_string *,
2417 struct glyph_string *));
2418static void x_append_glyph_string P_ ((struct glyph_string **,
2419 struct glyph_string **,
2420 struct glyph_string *));
2421static int x_left_overwritten P_ ((struct glyph_string *));
2422static int x_left_overwriting P_ ((struct glyph_string *));
2423static int x_right_overwritten P_ ((struct glyph_string *));
2424static int x_right_overwriting P_ ((struct glyph_string *));
66ac4b0e
GM
2425static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2426 int));
06a2c219
GM
2427static void x_init_glyph_string P_ ((struct glyph_string *,
2428 XChar2b *, struct window *,
2429 struct glyph_row *,
2430 enum glyph_row_area, int,
2431 enum draw_glyphs_face));
2432static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2433 enum glyph_row_area, int, int,
66ac4b0e 2434 enum draw_glyphs_face, int *, int *, int));
06a2c219
GM
2435static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2436static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2437static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2438 int));
2439static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
b4192550 2440static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
06a2c219
GM
2441static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2442static void x_draw_glyph_string P_ ((struct glyph_string *));
2443static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2444static void x_set_cursor_gc P_ ((struct glyph_string *));
2445static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2446static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2447static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2448 int *, int *));
2449static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2450static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
68c45bf0 2451 unsigned long *, double, int));
06a2c219 2452static void x_setup_relief_color P_ ((struct frame *, struct relief *,
68c45bf0 2453 double, int, unsigned long));
06a2c219
GM
2454static void x_setup_relief_colors P_ ((struct glyph_string *));
2455static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2456static void x_draw_image_relief P_ ((struct glyph_string *));
2457static void x_draw_image_foreground P_ ((struct glyph_string *));
2458static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2459static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2460static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2461 int, int, int));
2462static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2463 int, int, int, int, XRectangle *));
2464static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2465 int, int, int, XRectangle *));
66ac4b0e
GM
2466static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2467 enum glyph_row_area));
06a2c219
GM
2468
2469
06a2c219
GM
2470/* Append the list of glyph strings with head H and tail T to the list
2471 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2472
2473static INLINE void
2474x_append_glyph_string_lists (head, tail, h, t)
2475 struct glyph_string **head, **tail;
2476 struct glyph_string *h, *t;
2477{
2478 if (h)
2479 {
2480 if (*head)
2481 (*tail)->next = h;
2482 else
2483 *head = h;
2484 h->prev = *tail;
2485 *tail = t;
2486 }
2487}
2488
2489
2490/* Prepend the list of glyph strings with head H and tail T to the
2491 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2492 result. */
2493
2494static INLINE void
2495x_prepend_glyph_string_lists (head, tail, h, t)
2496 struct glyph_string **head, **tail;
2497 struct glyph_string *h, *t;
2498{
2499 if (h)
2500 {
2501 if (*head)
2502 (*head)->prev = t;
2503 else
2504 *tail = t;
2505 t->next = *head;
2506 *head = h;
2507 }
2508}
2509
2510
2511/* Append glyph string S to the list with head *HEAD and tail *TAIL.
2512 Set *HEAD and *TAIL to the resulting list. */
2513
2514static INLINE void
2515x_append_glyph_string (head, tail, s)
2516 struct glyph_string **head, **tail;
2517 struct glyph_string *s;
2518{
2519 s->next = s->prev = NULL;
2520 x_append_glyph_string_lists (head, tail, s, s);
2521}
2522
2523
2524/* Set S->gc to a suitable GC for drawing glyph string S in cursor
2525 face. */
2526
2527static void
2528x_set_cursor_gc (s)
2529 struct glyph_string *s;
2530{
2531 if (s->font == FRAME_FONT (s->f)
2532 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2533 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
b4192550 2534 && !s->cmp)
06a2c219
GM
2535 s->gc = s->f->output_data.x->cursor_gc;
2536 else
2537 {
2538 /* Cursor on non-default face: must merge. */
2539 XGCValues xgcv;
2540 unsigned long mask;
2541
2542 xgcv.background = s->f->output_data.x->cursor_pixel;
2543 xgcv.foreground = s->face->background;
2544
2545 /* If the glyph would be invisible, try a different foreground. */
2546 if (xgcv.foreground == xgcv.background)
2547 xgcv.foreground = s->face->foreground;
2548 if (xgcv.foreground == xgcv.background)
2549 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2550 if (xgcv.foreground == xgcv.background)
2551 xgcv.foreground = s->face->foreground;
2552
2553 /* Make sure the cursor is distinct from text in this face. */
2554 if (xgcv.background == s->face->background
2555 && xgcv.foreground == s->face->foreground)
2556 {
2557 xgcv.background = s->face->foreground;
2558 xgcv.foreground = s->face->background;
2559 }
2560
2561 IF_DEBUG (x_check_font (s->f, s->font));
2562 xgcv.font = s->font->fid;
2563 xgcv.graphics_exposures = False;
2564 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2565
2566 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2567 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2568 mask, &xgcv);
2569 else
2570 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2571 = XCreateGC (s->display, s->window, mask, &xgcv);
2572
2573 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2574 }
2575}
2576
2577
2578/* Set up S->gc of glyph string S for drawing text in mouse face. */
2579
2580static void
2581x_set_mouse_face_gc (s)
2582 struct glyph_string *s;
2583{
2584 int face_id;
2585
2586 /* What face has to be used for the mouse face? */
2587 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2588 face_id = FACE_FOR_CHARSET (s->f, face_id, s->charset);
2589 s->face = FACE_FROM_ID (s->f, face_id);
2590 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2591
2592 /* If font in this face is same as S->font, use it. */
2593 if (s->font == s->face->font)
2594 s->gc = s->face->gc;
2595 else
2596 {
2597 /* Otherwise construct scratch_cursor_gc with values from FACE
2598 but font FONT. */
2599 XGCValues xgcv;
2600 unsigned long mask;
2601
2602 xgcv.background = s->face->background;
2603 xgcv.foreground = s->face->foreground;
2604 IF_DEBUG (x_check_font (s->f, s->font));
2605 xgcv.font = s->font->fid;
2606 xgcv.graphics_exposures = False;
2607 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2608
2609 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2610 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2611 mask, &xgcv);
2612 else
2613 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2614 = XCreateGC (s->display, s->window, mask, &xgcv);
2615
2616 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2617 }
2618
2619 xassert (s->gc != 0);
2620}
2621
2622
2623/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2624 Faces to use in the mode line have already been computed when the
2625 matrix was built, so there isn't much to do, here. */
2626
2627static INLINE void
2628x_set_mode_line_face_gc (s)
2629 struct glyph_string *s;
2630{
2631 s->gc = s->face->gc;
2632 xassert (s->gc != 0);
2633}
2634
2635
2636/* Set S->gc of glyph string S for drawing that glyph string. Set
2637 S->stippled_p to a non-zero value if the face of S has a stipple
2638 pattern. */
2639
2640static INLINE void
2641x_set_glyph_string_gc (s)
2642 struct glyph_string *s;
2643{
2644 if (s->hl == DRAW_NORMAL_TEXT)
2645 {
2646 s->gc = s->face->gc;
2647 s->stippled_p = s->face->stipple != 0;
2648 }
2649 else if (s->hl == DRAW_INVERSE_VIDEO)
2650 {
2651 x_set_mode_line_face_gc (s);
2652 s->stippled_p = s->face->stipple != 0;
2653 }
2654 else if (s->hl == DRAW_CURSOR)
2655 {
2656 x_set_cursor_gc (s);
2657 s->stippled_p = 0;
2658 }
2659 else if (s->hl == DRAW_MOUSE_FACE)
2660 {
2661 x_set_mouse_face_gc (s);
2662 s->stippled_p = s->face->stipple != 0;
2663 }
2664 else if (s->hl == DRAW_IMAGE_RAISED
2665 || s->hl == DRAW_IMAGE_SUNKEN)
2666 {
2667 s->gc = s->face->gc;
2668 s->stippled_p = s->face->stipple != 0;
2669 }
2670 else
2671 {
2672 s->gc = s->face->gc;
2673 s->stippled_p = s->face->stipple != 0;
2674 }
2675
2676 /* GC must have been set. */
2677 xassert (s->gc != 0);
2678}
2679
2680
2681/* Return in *R the clipping rectangle for glyph string S. */
2682
2683static void
2684x_get_glyph_string_clip_rect (s, r)
2685 struct glyph_string *s;
2686 XRectangle *r;
2687{
2688 if (s->row->full_width_p)
2689 {
2690 /* Draw full-width. X coordinates are relative to S->w->left. */
1da3fd71
GM
2691 int canon_x = CANON_X_UNIT (s->f);
2692
2693 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2694 r->width = XFASTINT (s->w->width) * canon_x;
06a2c219
GM
2695
2696 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2697 {
1da3fd71 2698 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
06a2c219
GM
2699 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2700 r->x -= width;
2701 }
2702
b9432a85 2703 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
1da3fd71 2704
06a2c219
GM
2705 /* Unless displaying a mode or menu bar line, which are always
2706 fully visible, clip to the visible part of the row. */
2707 if (s->w->pseudo_window_p)
2708 r->height = s->row->visible_height;
2709 else
2710 r->height = s->height;
2711 }
2712 else
2713 {
2714 /* This is a text line that may be partially visible. */
2715 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2716 r->width = window_box_width (s->w, s->area);
2717 r->height = s->row->visible_height;
2718 }
2719
2720 /* Don't use S->y for clipping because it doesn't take partially
2721 visible lines into account. For example, it can be negative for
2722 partially visible lines at the top of a window. */
2723 if (!s->row->full_width_p
2724 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
045dee35 2725 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
06a2c219
GM
2726 else
2727 r->y = max (0, s->row->y);
06a2c219 2728
9ea173e8 2729 /* If drawing a tool-bar window, draw it over the internal border
06a2c219 2730 at the top of the window. */
9ea173e8 2731 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219 2732 r->y -= s->f->output_data.x->internal_border_width;
66ac4b0e
GM
2733
2734 /* If S draws overlapping rows, it's sufficient to use the top and
2735 bottom of the window for clipping because this glyph string
2736 intentionally draws over other lines. */
2737 if (s->for_overlaps_p)
2738 {
045dee35 2739 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
66ac4b0e
GM
2740 r->height = window_text_bottom_y (s->w) - r->y;
2741 }
2742
2743 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
06a2c219
GM
2744}
2745
2746
2747/* Set clipping for output of glyph string S. S may be part of a mode
2748 line or menu if we don't have X toolkit support. */
2749
2750static INLINE void
2751x_set_glyph_string_clipping (s)
2752 struct glyph_string *s;
2753{
2754 XRectangle r;
2755 x_get_glyph_string_clip_rect (s, &r);
2756 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2757}
2758
2759
2760/* Compute left and right overhang of glyph string S. If S is a glyph
b4192550 2761 string for a composition, assume overhangs don't exist. */
06a2c219
GM
2762
2763static INLINE void
2764x_compute_glyph_string_overhangs (s)
2765 struct glyph_string *s;
2766{
b4192550 2767 if (s->cmp == NULL
06a2c219
GM
2768 && s->first_glyph->type == CHAR_GLYPH)
2769 {
2770 XCharStruct cs;
2771 int direction, font_ascent, font_descent;
2772 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2773 &font_ascent, &font_descent, &cs);
2774 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2775 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2776 }
2777}
2778
2779
2780/* Compute overhangs and x-positions for glyph string S and its
2781 predecessors, or successors. X is the starting x-position for S.
2782 BACKWARD_P non-zero means process predecessors. */
2783
2784static void
2785x_compute_overhangs_and_x (s, x, backward_p)
2786 struct glyph_string *s;
2787 int x;
2788 int backward_p;
2789{
2790 if (backward_p)
2791 {
2792 while (s)
2793 {
2794 x_compute_glyph_string_overhangs (s);
2795 x -= s->width;
2796 s->x = x;
2797 s = s->prev;
2798 }
2799 }
2800 else
2801 {
2802 while (s)
2803 {
2804 x_compute_glyph_string_overhangs (s);
2805 s->x = x;
2806 x += s->width;
2807 s = s->next;
2808 }
2809 }
2810}
2811
2812
2813/* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
b4192550
KH
2814 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2815 assumed to be zero. */
06a2c219
GM
2816
2817static void
2818x_get_glyph_overhangs (glyph, f, left, right)
2819 struct glyph *glyph;
2820 struct frame *f;
2821 int *left, *right;
2822{
2823 int c;
2824
2825 *left = *right = 0;
2826
b4192550 2827 if (glyph->type == CHAR_GLYPH)
06a2c219
GM
2828 {
2829 XFontStruct *font;
2830 struct face *face;
2831 struct font_info *font_info;
2832 XChar2b char2b;
2833
2834 face = x_get_glyph_face_and_encoding (f, glyph, &char2b);
2835 font = face->font;
2836 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2837 if (font)
2838 {
2839 XCharStruct *pcm = x_per_char_metric (font, &char2b);
2840
2841 if (pcm->rbearing > pcm->width)
2842 *right = pcm->rbearing - pcm->width;
2843 if (pcm->lbearing < 0)
2844 *left = -pcm->lbearing;
2845 }
2846 }
2847}
2848
2849
2850/* Return the index of the first glyph preceding glyph string S that
2851 is overwritten by S because of S's left overhang. Value is -1
2852 if no glyphs are overwritten. */
2853
2854static int
2855x_left_overwritten (s)
2856 struct glyph_string *s;
2857{
2858 int k;
2859
2860 if (s->left_overhang)
2861 {
2862 int x = 0, i;
2863 struct glyph *glyphs = s->row->glyphs[s->area];
2864 int first = s->first_glyph - glyphs;
2865
2866 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2867 x -= glyphs[i].pixel_width;
2868
2869 k = i + 1;
2870 }
2871 else
2872 k = -1;
2873
2874 return k;
2875}
2876
2877
2878/* Return the index of the first glyph preceding glyph string S that
2879 is overwriting S because of its right overhang. Value is -1 if no
2880 glyph in front of S overwrites S. */
2881
2882static int
2883x_left_overwriting (s)
2884 struct glyph_string *s;
2885{
2886 int i, k, x;
2887 struct glyph *glyphs = s->row->glyphs[s->area];
2888 int first = s->first_glyph - glyphs;
2889
2890 k = -1;
2891 x = 0;
2892 for (i = first - 1; i >= 0; --i)
2893 {
2894 int left, right;
2895 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2896 if (x + right > 0)
2897 k = i;
2898 x -= glyphs[i].pixel_width;
2899 }
2900
2901 return k;
2902}
2903
2904
2905/* Return the index of the last glyph following glyph string S that is
2906 not overwritten by S because of S's right overhang. Value is -1 if
2907 no such glyph is found. */
2908
2909static int
2910x_right_overwritten (s)
2911 struct glyph_string *s;
2912{
2913 int k = -1;
2914
2915 if (s->right_overhang)
2916 {
2917 int x = 0, i;
2918 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 2919 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
2920 int end = s->row->used[s->area];
2921
2922 for (i = first; i < end && s->right_overhang > x; ++i)
2923 x += glyphs[i].pixel_width;
2924
2925 k = i;
2926 }
2927
2928 return k;
2929}
2930
2931
2932/* Return the index of the last glyph following glyph string S that
2933 overwrites S because of its left overhang. Value is negative
2934 if no such glyph is found. */
2935
2936static int
2937x_right_overwriting (s)
2938 struct glyph_string *s;
2939{
2940 int i, k, x;
2941 int end = s->row->used[s->area];
2942 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 2943 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
2944
2945 k = -1;
2946 x = 0;
2947 for (i = first; i < end; ++i)
2948 {
2949 int left, right;
2950 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2951 if (x - left < 0)
2952 k = i;
2953 x += glyphs[i].pixel_width;
2954 }
2955
2956 return k;
2957}
2958
2959
2960/* Fill rectangle X, Y, W, H with background color of glyph string S. */
2961
2962static INLINE void
2963x_clear_glyph_string_rect (s, x, y, w, h)
2964 struct glyph_string *s;
2965 int x, y, w, h;
2966{
2967 XGCValues xgcv;
2968 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
2969 XSetForeground (s->display, s->gc, xgcv.background);
2970 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2971 XSetForeground (s->display, s->gc, xgcv.foreground);
2972}
2973
2974
2975/* Draw the background of glyph_string S. If S->background_filled_p
2976 is non-zero don't draw it. FORCE_P non-zero means draw the
2977 background even if it wouldn't be drawn normally. This is used
b4192550
KH
2978 when a string preceding S draws into the background of S, or S
2979 contains the first component of a composition. */
06a2c219
GM
2980
2981static void
2982x_draw_glyph_string_background (s, force_p)
2983 struct glyph_string *s;
2984 int force_p;
2985{
2986 /* Nothing to do if background has already been drawn or if it
2987 shouldn't be drawn in the first place. */
2988 if (!s->background_filled_p)
2989 {
b4192550 2990 if (s->stippled_p)
06a2c219
GM
2991 {
2992 /* Fill background with a stipple pattern. */
2993 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2994 XFillRectangle (s->display, s->window, s->gc, s->x,
2995 s->y + s->face->box_line_width,
2996 s->background_width,
2997 s->height - 2 * s->face->box_line_width);
2998 XSetFillStyle (s->display, s->gc, FillSolid);
2999 s->background_filled_p = 1;
3000 }
3001 else if (FONT_HEIGHT (s->font) < s->height - 2 * s->face->box_line_width
3002 || s->font_not_found_p
3003 || s->extends_to_end_of_line_p
06a2c219
GM
3004 || force_p)
3005 {
3006 x_clear_glyph_string_rect (s, s->x, s->y + s->face->box_line_width,
3007 s->background_width,
3008 s->height - 2 * s->face->box_line_width);
3009 s->background_filled_p = 1;
3010 }
3011 }
3012}
3013
3014
3015/* Draw the foreground of glyph string S. */
3016
3017static void
3018x_draw_glyph_string_foreground (s)
3019 struct glyph_string *s;
3020{
3021 int i, x;
3022
3023 /* If first glyph of S has a left box line, start drawing the text
3024 of S to the right of that box line. */
3025 if (s->face->box != FACE_NO_BOX
3026 && s->first_glyph->left_box_line_p)
3027 x = s->x + s->face->box_line_width;
3028 else
3029 x = s->x;
3030
b4192550
KH
3031 /* Draw characters of S as rectangles if S's font could not be
3032 loaded. */
3033 if (s->font_not_found_p)
06a2c219 3034 {
b4192550 3035 for (i = 0; i < s->nchars; ++i)
06a2c219 3036 {
b4192550
KH
3037 struct glyph *g = s->first_glyph + i;
3038 XDrawRectangle (s->display, s->window,
3039 s->gc, x, s->y, g->pixel_width - 1,
3040 s->height - 1);
3041 x += g->pixel_width;
06a2c219
GM
3042 }
3043 }
3044 else
3045 {
b4192550
KH
3046 char *char1b = (char *) s->char2b;
3047 int boff = s->font_info->baseline_offset;
06a2c219 3048
b4192550
KH
3049 if (s->font_info->vertical_centering)
3050 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3051
3052 /* If we can use 8-bit functions, condense S->char2b. */
3053 if (!s->two_byte_p)
3054 for (i = 0; i < s->nchars; ++i)
3055 char1b[i] = s->char2b[i].byte2;
3056
3057 /* Draw text with XDrawString if background has already been
3058 filled. Otherwise, use XDrawImageString. (Note that
3059 XDrawImageString is usually faster than XDrawString.) Always
3060 use XDrawImageString when drawing the cursor so that there is
3061 no chance that characters under a box cursor are invisible. */
3062 if (s->for_overlaps_p
3063 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3064 {
3065 /* Draw characters with 16-bit or 8-bit functions. */
3066 if (s->two_byte_p)
3067 XDrawString16 (s->display, s->window, s->gc, x,
3068 s->ybase - boff, s->char2b, s->nchars);
3069 else
3070 XDrawString (s->display, s->window, s->gc, x,
3071 s->ybase - boff, char1b, s->nchars);
3072 }
06a2c219
GM
3073 else
3074 {
b4192550
KH
3075 if (s->two_byte_p)
3076 XDrawImageString16 (s->display, s->window, s->gc, x,
3077 s->ybase - boff, s->char2b, s->nchars);
06a2c219 3078 else
b4192550
KH
3079 XDrawImageString (s->display, s->window, s->gc, x,
3080 s->ybase - boff, char1b, s->nchars);
3081 }
3082 }
3083}
06a2c219 3084
b4192550 3085/* Draw the foreground of composite glyph string S. */
06a2c219 3086
b4192550
KH
3087static void
3088x_draw_composite_glyph_string_foreground (s)
3089 struct glyph_string *s;
3090{
3091 int i, x;
06a2c219 3092
b4192550
KH
3093 /* If first glyph of S has a left box line, start drawing the text
3094 of S to the right of that box line. */
3095 if (s->face->box != FACE_NO_BOX
3096 && s->first_glyph->left_box_line_p)
3097 x = s->x + s->face->box_line_width;
3098 else
3099 x = s->x;
06a2c219 3100
b4192550
KH
3101 /* S is a glyph string for a composition. S->gidx is the index of
3102 the first character drawn for glyphs of this composition.
3103 S->gidx == 0 means we are drawing the very first character of
3104 this composition. */
06a2c219 3105
b4192550
KH
3106 /* Draw a rectangle for the composition if the font for the very
3107 first character of the composition could not be loaded. */
3108 if (s->font_not_found_p)
3109 {
3110 if (s->gidx == 0)
3111 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3112 s->width - 1, s->height - 1);
3113 }
3114 else
3115 {
3116 for (i = 0; i < s->nchars; i++, ++s->gidx)
3117 XDrawString16 (s->display, s->window, s->gc,
3118 x + s->cmp->offsets[s->gidx * 2],
3119 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3120 s->char2b + i, 1);
06a2c219
GM
3121 }
3122}
3123
3124
80c32bcc
GM
3125#ifdef USE_X_TOOLKIT
3126
3127/* Allocate the color COLOR->pixel on the screen and display of
3128 widget WIDGET in colormap CMAP. If an exact match cannot be
3129 allocated, try the nearest color available. Value is non-zero
3130 if successful. This is called from lwlib. */
3131
3132int
3133x_alloc_nearest_color_for_widget (widget, cmap, color)
3134 Widget widget;
3135 Colormap cmap;
3136 XColor *color;
3137{
3138 struct frame *f;
3139 struct x_display_info *dpyinfo;
5c187dee 3140 Lisp_Object tail;
80c32bcc
GM
3141
3142 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3143
3144 /* Find the top-level shell of the widget. Note that this function
3145 can be called when the widget is not yet realized, so XtWindow
3146 (widget) == 0. That's the reason we can't simply use
3147 x_any_window_to_frame. */
3148 while (!XtIsTopLevelShell (widget))
3149 widget = XtParent (widget);
3150
3151 /* Look for a frame with that top-level widget. Allocate the color
3152 on that frame to get the right gamma correction value. */
3153 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3154 if (GC_FRAMEP (XCAR (tail))
3155 && (f = XFRAME (XCAR (tail)),
3156 (f->output_data.nothing != 1
3157 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3158 && f->output_data.x->widget == widget)
3159 return x_alloc_nearest_color (f, cmap, color);
3160
3161 abort ();
3162}
3163
3164#endif /* USE_X_TOOLKIT */
3165
3166
06a2c219
GM
3167/* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3168 CMAP. If an exact match can't be allocated, try the nearest color
3169 available. Value is non-zero if successful. Set *COLOR to the
3170 color allocated. */
3171
3172int
80c32bcc
GM
3173x_alloc_nearest_color (f, cmap, color)
3174 struct frame *f;
06a2c219
GM
3175 Colormap cmap;
3176 XColor *color;
3177{
80c32bcc
GM
3178 Display *display = FRAME_X_DISPLAY (f);
3179 Screen *screen = FRAME_X_SCREEN (f);
3180 int rc;
3181
3182 gamma_correct (f, color);
3183 rc = XAllocColor (display, cmap, color);
06a2c219
GM
3184 if (rc == 0)
3185 {
3186 /* If we got to this point, the colormap is full, so we're going
3187 to try to get the next closest color. The algorithm used is
3188 a least-squares matching, which is what X uses for closest
3189 color matching with StaticColor visuals. */
3190 int nearest, i;
3191 unsigned long nearest_delta = ~0;
3192 int ncells = XDisplayCells (display, XScreenNumberOfScreen (screen));
3193 XColor *cells = (XColor *) alloca (ncells * sizeof *cells);
3194
3195 for (i = 0; i < ncells; ++i)
3196 cells[i].pixel = i;
3197 XQueryColors (display, cmap, cells, ncells);
3198
3199 for (nearest = i = 0; i < ncells; ++i)
3200 {
3201 long dred = (color->red >> 8) - (cells[i].red >> 8);
3202 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3203 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3204 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3205
3206 if (delta < nearest_delta)
3207 {
3208 nearest = i;
3209 nearest_delta = delta;
3210 }
3211 }
3212
3213 color->red = cells[nearest].red;
3214 color->green = cells[nearest].green;
3215 color->blue = cells[nearest].blue;
3216 rc = XAllocColor (display, cmap, color);
3217 }
3218
3219 return rc;
3220}
3221
3222
3223/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3224 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3225 If this produces the same color as PIXEL, try a color where all RGB
3226 values have DELTA added. Return the allocated color in *PIXEL.
3227 DISPLAY is the X display, CMAP is the colormap to operate on.
3228 Value is non-zero if successful. */
3229
3230static int
3231x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3232 struct frame *f;
3233 Display *display;
3234 Colormap cmap;
3235 unsigned long *pixel;
68c45bf0 3236 double factor;
06a2c219
GM
3237 int delta;
3238{
3239 XColor color, new;
3240 int success_p;
3241
3242 /* Get RGB color values. */
3243 color.pixel = *pixel;
3244 XQueryColor (display, cmap, &color);
3245
3246 /* Change RGB values by specified FACTOR. Avoid overflow! */
3247 xassert (factor >= 0);
3248 new.red = min (0xffff, factor * color.red);
3249 new.green = min (0xffff, factor * color.green);
3250 new.blue = min (0xffff, factor * color.blue);
3251
3252 /* Try to allocate the color. */
80c32bcc 3253 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3254 if (success_p)
3255 {
3256 if (new.pixel == *pixel)
3257 {
3258 /* If we end up with the same color as before, try adding
3259 delta to the RGB values. */
3260 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
3261
3262 /* If display has an immutable color map, freeing colors is
3263 not necessary and some servers don't allow it. So don't
3264 do it. */
3265 if (class != StaticColor
3266 && class != StaticGray
3267 && class != TrueColor)
3268 XFreeColors (display, cmap, &new.pixel, 1, 0);
3269
3270 new.red = min (0xffff, delta + color.red);
3271 new.green = min (0xffff, delta + color.green);
3272 new.blue = min (0xffff, delta + color.blue);
80c32bcc 3273 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3274 }
3275 else
3276 success_p = 1;
3277 *pixel = new.pixel;
3278 }
3279
3280 return success_p;
3281}
3282
3283
3284/* Set up the foreground color for drawing relief lines of glyph
3285 string S. RELIEF is a pointer to a struct relief containing the GC
3286 with which lines will be drawn. Use a color that is FACTOR or
3287 DELTA lighter or darker than the relief's background which is found
3288 in S->f->output_data.x->relief_background. If such a color cannot
3289 be allocated, use DEFAULT_PIXEL, instead. */
3290
3291static void
3292x_setup_relief_color (f, relief, factor, delta, default_pixel)
3293 struct frame *f;
3294 struct relief *relief;
68c45bf0 3295 double factor;
06a2c219
GM
3296 int delta;
3297 unsigned long default_pixel;
3298{
3299 XGCValues xgcv;
3300 struct x_output *di = f->output_data.x;
3301 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3302 unsigned long pixel;
3303 unsigned long background = di->relief_background;
3304 Colormap cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
dcd08bfb
GM
3305 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3306 Display *dpy = FRAME_X_DISPLAY (f);
06a2c219
GM
3307
3308 xgcv.graphics_exposures = False;
3309 xgcv.line_width = 1;
3310
3311 /* Free previously allocated color. The color cell will be reused
3312 when it has been freed as many times as it was allocated, so this
3313 doesn't affect faces using the same colors. */
3314 if (relief->gc
3315 && relief->allocated_p)
3316 {
3317 /* If display has an immutable color map, freeing colors is not
3318 necessary and some servers don't allow it. So don't do it. */
dcd08bfb 3319 int class = dpyinfo->visual->class;
06a2c219
GM
3320 if (class != StaticColor
3321 && class != StaticGray
3322 && class != TrueColor)
dcd08bfb 3323 XFreeColors (dpy, cmap, &relief->pixel, 1, 0);
06a2c219
GM
3324 relief->allocated_p = 0;
3325 }
3326
3327 /* Allocate new color. */
3328 xgcv.foreground = default_pixel;
3329 pixel = background;
dcd08bfb
GM
3330 if (dpyinfo->n_planes != 1
3331 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
06a2c219
GM
3332 {
3333 relief->allocated_p = 1;
3334 xgcv.foreground = relief->pixel = pixel;
3335 }
3336
3337 if (relief->gc == 0)
3338 {
dcd08bfb 3339 xgcv.stipple = dpyinfo->gray;
06a2c219 3340 mask |= GCStipple;
dcd08bfb 3341 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
06a2c219
GM
3342 }
3343 else
dcd08bfb 3344 XChangeGC (dpy, relief->gc, mask, &xgcv);
06a2c219
GM
3345}
3346
3347
3348/* Set up colors for the relief lines around glyph string S. */
3349
3350static void
3351x_setup_relief_colors (s)
3352 struct glyph_string *s;
3353{
3354 struct x_output *di = s->f->output_data.x;
3355 unsigned long color;
3356
3357 if (s->face->use_box_color_for_shadows_p)
3358 color = s->face->box_color;
3359 else
3360 {
3361 XGCValues xgcv;
3362
3363 /* Get the background color of the face. */
3364 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3365 color = xgcv.background;
3366 }
3367
3368 if (di->white_relief.gc == 0
3369 || color != di->relief_background)
3370 {
3371 di->relief_background = color;
3372 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3373 WHITE_PIX_DEFAULT (s->f));
3374 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3375 BLACK_PIX_DEFAULT (s->f));
3376 }
3377}
3378
3379
3380/* Draw a relief on frame F inside the rectangle given by LEFT_X,
3381 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3382 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3383 relief. LEFT_P non-zero means draw a relief on the left side of
3384 the rectangle. RIGHT_P non-zero means draw a relief on the right
3385 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3386 when drawing. */
3387
3388static void
3389x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3390 raised_p, left_p, right_p, clip_rect)
3391 struct frame *f;
3392 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3393 XRectangle *clip_rect;
3394{
3395 int i;
3396 GC gc;
3397
3398 if (raised_p)
3399 gc = f->output_data.x->white_relief.gc;
3400 else
3401 gc = f->output_data.x->black_relief.gc;
3402 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3403
3404 /* Top. */
3405 for (i = 0; i < width; ++i)
3406 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3407 left_x + i * left_p, top_y + i,
3408 right_x + 1 - i * right_p, top_y + i);
3409
3410 /* Left. */
3411 if (left_p)
3412 for (i = 0; i < width; ++i)
3413 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3414 left_x + i, top_y + i, left_x + i, bottom_y - i);
3415
3416 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3417 if (raised_p)
3418 gc = f->output_data.x->black_relief.gc;
3419 else
3420 gc = f->output_data.x->white_relief.gc;
3421 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3422
3423 /* Bottom. */
3424 for (i = 0; i < width; ++i)
3425 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3426 left_x + i * left_p, bottom_y - i,
3427 right_x + 1 - i * right_p, bottom_y - i);
3428
3429 /* Right. */
3430 if (right_p)
3431 for (i = 0; i < width; ++i)
3432 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3433 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3434
3435 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3436}
3437
3438
3439/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3440 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3441 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3442 left side of the rectangle. RIGHT_P non-zero means draw a line
3443 on the right side of the rectangle. CLIP_RECT is the clipping
3444 rectangle to use when drawing. */
3445
3446static void
3447x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3448 left_p, right_p, clip_rect)
3449 struct glyph_string *s;
3450 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3451 XRectangle *clip_rect;
3452{
3453 XGCValues xgcv;
3454
3455 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3456 XSetForeground (s->display, s->gc, s->face->box_color);
3457 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3458
3459 /* Top. */
3460 XFillRectangle (s->display, s->window, s->gc,
3461 left_x, top_y, right_x - left_x, width);
3462
3463 /* Left. */
3464 if (left_p)
3465 XFillRectangle (s->display, s->window, s->gc,
3466 left_x, top_y, width, bottom_y - top_y);
3467
3468 /* Bottom. */
3469 XFillRectangle (s->display, s->window, s->gc,
3470 left_x, bottom_y - width, right_x - left_x, width);
3471
3472 /* Right. */
3473 if (right_p)
3474 XFillRectangle (s->display, s->window, s->gc,
3475 right_x - width, top_y, width, bottom_y - top_y);
3476
3477 XSetForeground (s->display, s->gc, xgcv.foreground);
3478 XSetClipMask (s->display, s->gc, None);
3479}
3480
3481
3482/* Draw a box around glyph string S. */
3483
3484static void
3485x_draw_glyph_string_box (s)
3486 struct glyph_string *s;
3487{
3488 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3489 int left_p, right_p;
3490 struct glyph *last_glyph;
3491 XRectangle clip_rect;
3492
3493 last_x = window_box_right (s->w, s->area);
3494 if (s->row->full_width_p
3495 && !s->w->pseudo_window_p)
3496 {
110859fc 3497 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
06a2c219
GM
3498 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3499 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3500 }
3501
3502 /* The glyph that may have a right box line. */
b4192550 3503 last_glyph = (s->cmp || s->img
06a2c219
GM
3504 ? s->first_glyph
3505 : s->first_glyph + s->nchars - 1);
3506
3507 width = s->face->box_line_width;
3508 raised_p = s->face->box == FACE_RAISED_BOX;
3509 left_x = s->x;
3510 right_x = ((s->row->full_width_p
1da3fd71 3511 ? last_x - 1
a7aeb2de 3512 : min (last_x, s->x + s->background_width) - 1));
06a2c219
GM
3513 top_y = s->y;
3514 bottom_y = top_y + s->height - 1;
3515
3516 left_p = (s->first_glyph->left_box_line_p
3517 || (s->hl == DRAW_MOUSE_FACE
3518 && (s->prev == NULL
3519 || s->prev->hl != s->hl)));
3520 right_p = (last_glyph->right_box_line_p
3521 || (s->hl == DRAW_MOUSE_FACE
3522 && (s->next == NULL
3523 || s->next->hl != s->hl)));
3524
3525 x_get_glyph_string_clip_rect (s, &clip_rect);
3526
3527 if (s->face->box == FACE_SIMPLE_BOX)
3528 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3529 left_p, right_p, &clip_rect);
3530 else
3531 {
3532 x_setup_relief_colors (s);
3533 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3534 width, raised_p, left_p, right_p, &clip_rect);
3535 }
3536}
3537
3538
3539/* Draw foreground of image glyph string S. */
3540
3541static void
3542x_draw_image_foreground (s)
3543 struct glyph_string *s;
3544{
3545 int x;
3546 int y = s->ybase - IMAGE_ASCENT (s->img);
3547
3548 /* If first glyph of S has a left box line, start drawing it to the
3549 right of that line. */
3550 if (s->face->box != FACE_NO_BOX
3551 && s->first_glyph->left_box_line_p)
3552 x = s->x + s->face->box_line_width;
3553 else
3554 x = s->x;
3555
3556 /* If there is a margin around the image, adjust x- and y-position
3557 by that margin. */
3558 if (s->img->margin)
3559 {
3560 x += s->img->margin;
3561 y += s->img->margin;
3562 }
3563
3564 if (s->img->pixmap)
3565 {
3566 if (s->img->mask)
3567 {
3568 /* We can't set both a clip mask and use XSetClipRectangles
3569 because the latter also sets a clip mask. We also can't
3570 trust on the shape extension to be available
3571 (XShapeCombineRegion). So, compute the rectangle to draw
3572 manually. */
3573 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3574 | GCFunction);
3575 XGCValues xgcv;
3576 XRectangle clip_rect, image_rect, r;
3577
3578 xgcv.clip_mask = s->img->mask;
3579 xgcv.clip_x_origin = x;
3580 xgcv.clip_y_origin = y;
3581 xgcv.function = GXcopy;
3582 XChangeGC (s->display, s->gc, mask, &xgcv);
3583
3584 x_get_glyph_string_clip_rect (s, &clip_rect);
3585 image_rect.x = x;
3586 image_rect.y = y;
3587 image_rect.width = s->img->width;
3588 image_rect.height = s->img->height;
3589 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3590 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3591 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3592 }
3593 else
3594 {
3595 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3596 0, 0, s->img->width, s->img->height, x, y);
3597
3598 /* When the image has a mask, we can expect that at
3599 least part of a mouse highlight or a block cursor will
3600 be visible. If the image doesn't have a mask, make
3601 a block cursor visible by drawing a rectangle around
3602 the image. I believe it's looking better if we do
3603 nothing here for mouse-face. */
3604 if (s->hl == DRAW_CURSOR)
3605 XDrawRectangle (s->display, s->window, s->gc, x, y,
3606 s->img->width - 1, s->img->height - 1);
3607 }
3608 }
3609 else
3610 /* Draw a rectangle if image could not be loaded. */
3611 XDrawRectangle (s->display, s->window, s->gc, x, y,
3612 s->img->width - 1, s->img->height - 1);
3613}
3614
3615
3616/* Draw a relief around the image glyph string S. */
3617
3618static void
3619x_draw_image_relief (s)
3620 struct glyph_string *s;
3621{
3622 int x0, y0, x1, y1, thick, raised_p;
3623 XRectangle r;
3624 int x;
3625 int y = s->ybase - IMAGE_ASCENT (s->img);
3626
3627 /* If first glyph of S has a left box line, start drawing it to the
3628 right of that line. */
3629 if (s->face->box != FACE_NO_BOX
3630 && s->first_glyph->left_box_line_p)
3631 x = s->x + s->face->box_line_width;
3632 else
3633 x = s->x;
3634
3635 /* If there is a margin around the image, adjust x- and y-position
3636 by that margin. */
3637 if (s->img->margin)
3638 {
3639 x += s->img->margin;
3640 y += s->img->margin;
3641 }
3642
3643 if (s->hl == DRAW_IMAGE_SUNKEN
3644 || s->hl == DRAW_IMAGE_RAISED)
3645 {
9ea173e8 3646 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
06a2c219
GM
3647 raised_p = s->hl == DRAW_IMAGE_RAISED;
3648 }
3649 else
3650 {
3651 thick = abs (s->img->relief);
3652 raised_p = s->img->relief > 0;
3653 }
3654
3655 x0 = x - thick;
3656 y0 = y - thick;
3657 x1 = x + s->img->width + thick - 1;
3658 y1 = y + s->img->height + thick - 1;
3659
3660 x_setup_relief_colors (s);
3661 x_get_glyph_string_clip_rect (s, &r);
3662 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3663}
3664
3665
3666/* Draw the foreground of image glyph string S to PIXMAP. */
3667
3668static void
3669x_draw_image_foreground_1 (s, pixmap)
3670 struct glyph_string *s;
3671 Pixmap pixmap;
3672{
3673 int x;
3674 int y = s->ybase - s->y - IMAGE_ASCENT (s->img);
3675
3676 /* If first glyph of S has a left box line, start drawing it to the
3677 right of that line. */
3678 if (s->face->box != FACE_NO_BOX
3679 && s->first_glyph->left_box_line_p)
3680 x = s->face->box_line_width;
3681 else
3682 x = 0;
3683
3684 /* If there is a margin around the image, adjust x- and y-position
3685 by that margin. */
3686 if (s->img->margin)
3687 {
3688 x += s->img->margin;
3689 y += s->img->margin;
3690 }
dc43ef94 3691
06a2c219
GM
3692 if (s->img->pixmap)
3693 {
3694 if (s->img->mask)
3695 {
3696 /* We can't set both a clip mask and use XSetClipRectangles
3697 because the latter also sets a clip mask. We also can't
3698 trust on the shape extension to be available
3699 (XShapeCombineRegion). So, compute the rectangle to draw
3700 manually. */
3701 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3702 | GCFunction);
3703 XGCValues xgcv;
3704
3705 xgcv.clip_mask = s->img->mask;
3706 xgcv.clip_x_origin = x;
3707 xgcv.clip_y_origin = y;
3708 xgcv.function = GXcopy;
3709 XChangeGC (s->display, s->gc, mask, &xgcv);
3710
3711 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3712 0, 0, s->img->width, s->img->height, x, y);
3713 XSetClipMask (s->display, s->gc, None);
3714 }
3715 else
3716 {
3717 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3718 0, 0, s->img->width, s->img->height, x, y);
3719
3720 /* When the image has a mask, we can expect that at
3721 least part of a mouse highlight or a block cursor will
3722 be visible. If the image doesn't have a mask, make
3723 a block cursor visible by drawing a rectangle around
3724 the image. I believe it's looking better if we do
3725 nothing here for mouse-face. */
3726 if (s->hl == DRAW_CURSOR)
3727 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3728 s->img->width - 1, s->img->height - 1);
3729 }
3730 }
3731 else
3732 /* Draw a rectangle if image could not be loaded. */
3733 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3734 s->img->width - 1, s->img->height - 1);
3735}
dc43ef94 3736
990ba854 3737
06a2c219
GM
3738/* Draw part of the background of glyph string S. X, Y, W, and H
3739 give the rectangle to draw. */
a9a5b0a5 3740
06a2c219
GM
3741static void
3742x_draw_glyph_string_bg_rect (s, x, y, w, h)
3743 struct glyph_string *s;
3744 int x, y, w, h;
3745{
3746 if (s->stippled_p)
3747 {
3748 /* Fill background with a stipple pattern. */
3749 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3750 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3751 XSetFillStyle (s->display, s->gc, FillSolid);
3752 }
3753 else
3754 x_clear_glyph_string_rect (s, x, y, w, h);
3755}
07e34cb0 3756
b5210ea7 3757
06a2c219 3758/* Draw image glyph string S.
dc43ef94 3759
06a2c219
GM
3760 s->y
3761 s->x +-------------------------
3762 | s->face->box
3763 |
3764 | +-------------------------
3765 | | s->img->margin
3766 | |
3767 | | +-------------------
3768 | | | the image
dc43ef94 3769
06a2c219 3770 */
dc43ef94 3771
06a2c219
GM
3772static void
3773x_draw_image_glyph_string (s)
3774 struct glyph_string *s;
3775{
3776 int x, y;
3777 int box_line_width = s->face->box_line_width;
3778 int margin = s->img->margin;
3779 int height;
3780 Pixmap pixmap = None;
3781
3782 height = s->height - 2 * box_line_width;
3783
3784 /* Fill background with face under the image. Do it only if row is
3785 taller than image or if image has a clip mask to reduce
3786 flickering. */
3787 s->stippled_p = s->face->stipple != 0;
3788 if (height > s->img->height
3789 || margin
3790 || s->img->mask
3791 || s->img->pixmap == 0
3792 || s->width != s->background_width)
3793 {
3794 if (box_line_width && s->first_glyph->left_box_line_p)
3795 x = s->x + box_line_width;
3796 else
3797 x = s->x;
3798
3799 y = s->y + box_line_width;
3800
3801 if (s->img->mask)
3802 {
3803 /* Create a pixmap as large as the glyph string Fill it with
3804 the background color. Copy the image to it, using its
3805 mask. Copy the temporary pixmap to the display. */
3806 Screen *screen = FRAME_X_SCREEN (s->f);
3807 int depth = DefaultDepthOfScreen (screen);
3808
3809 /* Create a pixmap as large as the glyph string. */
3810 pixmap = XCreatePixmap (s->display, s->window,
3811 s->background_width,
3812 s->height, depth);
3813
3814 /* Don't clip in the following because we're working on the
3815 pixmap. */
3816 XSetClipMask (s->display, s->gc, None);
3817
3818 /* Fill the pixmap with the background color/stipple. */
3819 if (s->stippled_p)
3820 {
3821 /* Fill background with a stipple pattern. */
3822 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3823 XFillRectangle (s->display, pixmap, s->gc,
3824 0, 0, s->background_width, s->height);
3825 XSetFillStyle (s->display, s->gc, FillSolid);
3826 }
3827 else
3828 {
3829 XGCValues xgcv;
3830 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3831 &xgcv);
3832 XSetForeground (s->display, s->gc, xgcv.background);
3833 XFillRectangle (s->display, pixmap, s->gc,
3834 0, 0, s->background_width, s->height);
3835 XSetForeground (s->display, s->gc, xgcv.foreground);
3836 }
3837 }
3838 else
3839 /* Implementation idea: Is it possible to construct a mask?
3840 We could look at the color at the margins of the image, and
3841 say that this color is probably the background color of the
3842 image. */
3843 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
3844
3845 s->background_filled_p = 1;
3846 }
dc43ef94 3847
06a2c219
GM
3848 /* Draw the foreground. */
3849 if (pixmap != None)
3850 {
3851 x_draw_image_foreground_1 (s, pixmap);
3852 x_set_glyph_string_clipping (s);
3853 XCopyArea (s->display, pixmap, s->window, s->gc,
3854 0, 0, s->background_width, s->height, s->x, s->y);
3855 XFreePixmap (s->display, pixmap);
3856 }
3857 else
3858 x_draw_image_foreground (s);
b5210ea7 3859
06a2c219
GM
3860 /* If we must draw a relief around the image, do it. */
3861 if (s->img->relief
3862 || s->hl == DRAW_IMAGE_RAISED
3863 || s->hl == DRAW_IMAGE_SUNKEN)
3864 x_draw_image_relief (s);
3865}
8c1a6a84 3866
990ba854 3867
06a2c219 3868/* Draw stretch glyph string S. */
dc43ef94 3869
06a2c219
GM
3870static void
3871x_draw_stretch_glyph_string (s)
3872 struct glyph_string *s;
3873{
3874 xassert (s->first_glyph->type == STRETCH_GLYPH);
3875 s->stippled_p = s->face->stipple != 0;
990ba854 3876
06a2c219
GM
3877 if (s->hl == DRAW_CURSOR
3878 && !x_stretch_cursor_p)
3879 {
3880 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
3881 as wide as the stretch glyph. */
3882 int width = min (CANON_X_UNIT (s->f), s->background_width);
990ba854 3883
06a2c219
GM
3884 /* Draw cursor. */
3885 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
0cdd0c9f 3886
06a2c219
GM
3887 /* Clear rest using the GC of the original non-cursor face. */
3888 if (width < s->background_width)
3889 {
3890 GC gc = s->face->gc;
3891 int x = s->x + width, y = s->y;
3892 int w = s->background_width - width, h = s->height;
3893 XRectangle r;
dc43ef94 3894
06a2c219
GM
3895 x_get_glyph_string_clip_rect (s, &r);
3896 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
97210f4e 3897
06a2c219
GM
3898 if (s->face->stipple)
3899 {
3900 /* Fill background with a stipple pattern. */
3901 XSetFillStyle (s->display, gc, FillOpaqueStippled);
3902 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3903 XSetFillStyle (s->display, gc, FillSolid);
3904 }
3905 else
3906 {
3907 XGCValues xgcv;
3908 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
3909 XSetForeground (s->display, gc, xgcv.background);
3910 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3911 XSetForeground (s->display, gc, xgcv.foreground);
3912 }
3913 }
3914 }
3915 else
3916 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
3917 s->height);
3918
3919 s->background_filled_p = 1;
3920}
3921
3922
3923/* Draw glyph string S. */
3924
3925static void
3926x_draw_glyph_string (s)
3927 struct glyph_string *s;
3928{
3929 /* If S draws into the background of its successor, draw the
3930 background of the successor first so that S can draw into it.
3931 This makes S->next use XDrawString instead of XDrawImageString. */
66ac4b0e 3932 if (s->next && s->right_overhang && !s->for_overlaps_p)
06a2c219
GM
3933 {
3934 xassert (s->next->img == NULL);
3935 x_set_glyph_string_gc (s->next);
3936 x_set_glyph_string_clipping (s->next);
3937 x_draw_glyph_string_background (s->next, 1);
3938 }
97210f4e 3939
06a2c219
GM
3940 /* Set up S->gc, set clipping and draw S. */
3941 x_set_glyph_string_gc (s);
3942 x_set_glyph_string_clipping (s);
3943
3944 switch (s->first_glyph->type)
3945 {
3946 case IMAGE_GLYPH:
3947 x_draw_image_glyph_string (s);
3948 break;
3949
3950 case STRETCH_GLYPH:
3951 x_draw_stretch_glyph_string (s);
3952 break;
3953
3954 case CHAR_GLYPH:
66ac4b0e
GM
3955 if (s->for_overlaps_p)
3956 s->background_filled_p = 1;
3957 else
3958 x_draw_glyph_string_background (s, 0);
06a2c219
GM
3959 x_draw_glyph_string_foreground (s);
3960 break;
3961
b4192550
KH
3962 case COMPOSITE_GLYPH:
3963 if (s->for_overlaps_p || s->gidx > 0)
3964 s->background_filled_p = 1;
3965 else
3966 x_draw_glyph_string_background (s, 1);
3967 x_draw_composite_glyph_string_foreground (s);
3968 break;
3969
06a2c219
GM
3970 default:
3971 abort ();
3972 }
3973
66ac4b0e 3974 if (!s->for_overlaps_p)
06a2c219 3975 {
66ac4b0e
GM
3976 /* Draw underline. */
3977 if (s->face->underline_p)
3978 {
3979 unsigned long dy, h;
06a2c219 3980
66ac4b0e
GM
3981 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
3982 h = 1;
3983 if (!XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &dy))
3984 dy = s->height - h;
06a2c219 3985
66ac4b0e
GM
3986 if (s->face->underline_defaulted_p)
3987 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3988 s->width, h);
3989 else
3990 {
3991 XGCValues xgcv;
3992 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3993 XSetForeground (s->display, s->gc, s->face->underline_color);
3994 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3995 s->width, h);
3996 XSetForeground (s->display, s->gc, xgcv.foreground);
3997 }
dc6f92b8 3998 }
07e34cb0 3999
66ac4b0e
GM
4000 /* Draw overline. */
4001 if (s->face->overline_p)
06a2c219 4002 {
66ac4b0e
GM
4003 unsigned long dy = 0, h = 1;
4004
4005 if (s->face->overline_color_defaulted_p)
4006 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4007 s->width, h);
4008 else
4009 {
4010 XGCValues xgcv;
4011 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4012 XSetForeground (s->display, s->gc, s->face->overline_color);
4013 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4014 s->width, h);
4015 XSetForeground (s->display, s->gc, xgcv.foreground);
4016 }
06a2c219 4017 }
06a2c219 4018
66ac4b0e
GM
4019 /* Draw strike-through. */
4020 if (s->face->strike_through_p)
06a2c219 4021 {
66ac4b0e
GM
4022 unsigned long h = 1;
4023 unsigned long dy = (s->height - h) / 2;
4024
4025 if (s->face->strike_through_color_defaulted_p)
4026 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4027 s->width, h);
4028 else
4029 {
4030 XGCValues xgcv;
4031 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4032 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4033 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4034 s->width, h);
4035 XSetForeground (s->display, s->gc, xgcv.foreground);
4036 }
06a2c219 4037 }
06a2c219 4038
66ac4b0e
GM
4039 /* Draw relief. */
4040 if (s->face->box != FACE_NO_BOX)
4041 x_draw_glyph_string_box (s);
4042 }
06a2c219
GM
4043
4044 /* Reset clipping. */
4045 XSetClipMask (s->display, s->gc, None);
dc6f92b8 4046}
07e34cb0 4047
06a2c219 4048
b4192550
KH
4049static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4050 struct face **, int));
06a2c219 4051
06a2c219 4052
b4192550
KH
4053/* Load glyph string S with a composition components specified by S->cmp.
4054 FACES is an array of faces for all components of this composition.
4055 S->gidx is the index of the first component for S.
4056 OVERLAPS_P non-zero means S should draw the foreground only, and
4057 use its lines physical height for clipping.
06a2c219 4058
b4192550 4059 Value is the index of a component not in S. */
07e34cb0 4060
b4192550
KH
4061static int
4062x_fill_composite_glyph_string (s, faces, overlaps_p)
06a2c219 4063 struct glyph_string *s;
b4192550 4064 struct face **faces;
66ac4b0e 4065 int overlaps_p;
07e34cb0 4066{
b4192550 4067 int i;
06a2c219 4068
b4192550 4069 xassert (s);
06a2c219 4070
b4192550 4071 s->for_overlaps_p = overlaps_p;
06a2c219 4072
b4192550
KH
4073 s->face = faces[s->gidx];
4074 s->font = s->face->font;
4075 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
06a2c219 4076
b4192550
KH
4077 /* For all glyphs of this composition, starting at the offset
4078 S->gidx, until we reach the end of the definition or encounter a
4079 glyph that requires the different face, add it to S. */
4080 ++s->nchars;
4081 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4082 ++s->nchars;
06a2c219 4083
b4192550
KH
4084 /* All glyph strings for the same composition has the same width,
4085 i.e. the width set for the first component of the composition. */
06a2c219 4086
06a2c219
GM
4087 s->width = s->first_glyph->pixel_width;
4088
4089 /* If the specified font could not be loaded, use the frame's
4090 default font, but record the fact that we couldn't load it in
4091 the glyph string so that we can draw rectangles for the
4092 characters of the glyph string. */
4093 if (s->font == NULL)
4094 {
4095 s->font_not_found_p = 1;
4096 s->font = FRAME_FONT (s->f);
4097 }
4098
4099 /* Adjust base line for subscript/superscript text. */
4100 s->ybase += s->first_glyph->voffset;
4101
4102 xassert (s->face && s->face->gc);
4103
4104 /* This glyph string must always be drawn with 16-bit functions. */
4105 s->two_byte_p = 1;
b4192550
KH
4106
4107 return s->gidx + s->nchars;
06a2c219
GM
4108}
4109
4110
b4192550 4111/* Load glyph string S with a sequence characters.
06a2c219 4112 FACE_ID is the face id of the string. START is the index of the
66ac4b0e
GM
4113 first glyph to consider, END is the index of the last + 1.
4114 OVERLAPS_P non-zero means S should draw the foreground only, and
4115 use its lines physical height for clipping.
4116
4117 Value is the index of the first glyph not in S. */
06a2c219
GM
4118
4119static int
66ac4b0e 4120x_fill_glyph_string (s, face_id, start, end, overlaps_p)
06a2c219
GM
4121 struct glyph_string *s;
4122 int face_id;
66ac4b0e 4123 int start, end, overlaps_p;
06a2c219
GM
4124{
4125 struct glyph *glyph, *last;
4126 int voffset;
4127
06a2c219
GM
4128 xassert (s->f == XFRAME (s->w->frame));
4129 xassert (s->nchars == 0);
4130 xassert (start >= 0 && end > start);
4131
66ac4b0e 4132 s->for_overlaps_p = overlaps_p,
06a2c219
GM
4133 glyph = s->row->glyphs[s->area] + start;
4134 last = s->row->glyphs[s->area] + end;
4135 voffset = glyph->voffset;
4136
4137 while (glyph < last
4138 && glyph->type == CHAR_GLYPH
4139 && glyph->voffset == voffset
4140 /* Same face id implies same charset, nowadays. */
4141 && glyph->u.ch.face_id == face_id)
4142 {
4143 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4144 s->char2b + s->nchars);
4145 if (s->char2b[s->nchars].byte2 != 0)
4146 s->two_byte_p = 1;
4147
4148 ++s->nchars;
4149 xassert (s->nchars <= end - start);
4150 s->width += glyph->pixel_width;
4151 ++glyph;
4152 }
4153
4154 s->font = s->face->font;
4155 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4156
4157 /* If the specified font could not be loaded, use the frame's font,
4158 but record the fact that we couldn't load it in
4159 S->font_not_found_p so that we can draw rectangles for the
4160 characters of the glyph string. */
4161 if (s->font == NULL)
4162 {
4163 s->font_not_found_p = 1;
4164 s->font = FRAME_FONT (s->f);
4165 }
4166
4167 /* Adjust base line for subscript/superscript text. */
4168 s->ybase += voffset;
66ac4b0e 4169
06a2c219
GM
4170 xassert (s->face && s->face->gc);
4171 return glyph - s->row->glyphs[s->area];
07e34cb0 4172}
dc6f92b8 4173
06a2c219
GM
4174
4175/* Fill glyph string S from image glyph S->first_glyph. */
dc6f92b8 4176
dfcf069d 4177static void
06a2c219
GM
4178x_fill_image_glyph_string (s)
4179 struct glyph_string *s;
4180{
4181 xassert (s->first_glyph->type == IMAGE_GLYPH);
4182 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img.id);
4183 xassert (s->img);
4184 s->face = FACE_FROM_ID (s->f, s->first_glyph->u.img.face_id);
4185 s->font = s->face->font;
4186 s->width = s->first_glyph->pixel_width;
4187
4188 /* Adjust base line for subscript/superscript text. */
4189 s->ybase += s->first_glyph->voffset;
4190}
4191
4192
4193/* Fill glyph string S from stretch glyph S->first_glyph. */
4194
4195static void
4196x_fill_stretch_glyph_string (s)
4197 struct glyph_string *s;
4198{
4199 xassert (s->first_glyph->type == STRETCH_GLYPH);
4200 s->face = FACE_FROM_ID (s->f, s->first_glyph->u.stretch.face_id);
4201 s->font = s->face->font;
4202 s->width = s->first_glyph->pixel_width;
4203
4204 /* Adjust base line for subscript/superscript text. */
4205 s->ybase += s->first_glyph->voffset;
4206}
4207
4208
4209/* Initialize glyph string S. CHAR2B is a suitably allocated vector
4210 of XChar2b structures for S; it can't be allocated in
4211 x_init_glyph_string because it must be allocated via `alloca'. W
4212 is the window on which S is drawn. ROW and AREA are the glyph row
4213 and area within the row from which S is constructed. START is the
4214 index of the first glyph structure covered by S. HL is a
4215 face-override for drawing S. */
4216
4217static void
4218x_init_glyph_string (s, char2b, w, row, area, start, hl)
4219 struct glyph_string *s;
4220 XChar2b *char2b;
4221 struct window *w;
4222 struct glyph_row *row;
4223 enum glyph_row_area area;
4224 int start;
4225 enum draw_glyphs_face hl;
4226{
4227 bzero (s, sizeof *s);
4228 s->w = w;
4229 s->f = XFRAME (w->frame);
4230 s->display = FRAME_X_DISPLAY (s->f);
4231 s->window = FRAME_X_WINDOW (s->f);
4232 s->char2b = char2b;
4233 s->hl = hl;
4234 s->row = row;
4235 s->area = area;
4236 s->first_glyph = row->glyphs[area] + start;
4237 s->height = row->height;
4238 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4239
9ea173e8
GM
4240 /* Display the internal border below the tool-bar window. */
4241 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219
GM
4242 s->y -= s->f->output_data.x->internal_border_width;
4243
4244 s->ybase = s->y + row->ascent;
4245}
4246
4247
4248/* Set background width of glyph string S. START is the index of the
4249 first glyph following S. LAST_X is the right-most x-position + 1
4250 in the drawing area. */
4251
4252static INLINE void
4253x_set_glyph_string_background_width (s, start, last_x)
4254 struct glyph_string *s;
4255 int start;
4256 int last_x;
4257{
4258 /* If the face of this glyph string has to be drawn to the end of
4259 the drawing area, set S->extends_to_end_of_line_p. */
4260 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4261
4262 if (start == s->row->used[s->area]
4263 && s->hl == DRAW_NORMAL_TEXT
4264 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4265 || s->face->background != default_face->background
4266 || s->face->stipple != default_face->stipple))
4267 s->extends_to_end_of_line_p = 1;
4268
4269 /* If S extends its face to the end of the line, set its
4270 background_width to the distance to the right edge of the drawing
4271 area. */
4272 if (s->extends_to_end_of_line_p)
1da3fd71 4273 s->background_width = last_x - s->x + 1;
06a2c219
GM
4274 else
4275 s->background_width = s->width;
4276}
4277
4278
4279/* Add a glyph string for a stretch glyph to the list of strings
4280 between HEAD and TAIL. START is the index of the stretch glyph in
4281 row area AREA of glyph row ROW. END is the index of the last glyph
4282 in that glyph row area. X is the current output position assigned
4283 to the new glyph string constructed. HL overrides that face of the
4284 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4285 is the right-most x-position of the drawing area. */
4286
8abee2e1
DL
4287/* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4288 and below -- keep them on one line. */
4289#define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4290 do \
4291 { \
4292 s = (struct glyph_string *) alloca (sizeof *s); \
4293 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4294 x_fill_stretch_glyph_string (s); \
4295 x_append_glyph_string (&HEAD, &TAIL, s); \
4296 ++START; \
4297 s->x = (X); \
4298 } \
4299 while (0)
4300
4301
4302/* Add a glyph string for an image glyph to the list of strings
4303 between HEAD and TAIL. START is the index of the image glyph in
4304 row area AREA of glyph row ROW. END is the index of the last glyph
4305 in that glyph row area. X is the current output position assigned
4306 to the new glyph string constructed. HL overrides that face of the
4307 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4308 is the right-most x-position of the drawing area. */
4309
8abee2e1 4310#define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4311 do \
4312 { \
4313 s = (struct glyph_string *) alloca (sizeof *s); \
4314 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4315 x_fill_image_glyph_string (s); \
4316 x_append_glyph_string (&HEAD, &TAIL, s); \
4317 ++START; \
4318 s->x = (X); \
4319 } \
4320 while (0)
4321
4322
4323/* Add a glyph string for a sequence of character glyphs to the list
4324 of strings between HEAD and TAIL. START is the index of the first
4325 glyph in row area AREA of glyph row ROW that is part of the new
4326 glyph string. END is the index of the last glyph in that glyph row
4327 area. X is the current output position assigned to the new glyph
4328 string constructed. HL overrides that face of the glyph; e.g. it
4329 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4330 right-most x-position of the drawing area. */
4331
8abee2e1 4332#define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4333 do \
4334 { \
4335 int c, charset, face_id; \
4336 XChar2b *char2b; \
4337 \
4338 c = (ROW)->glyphs[AREA][START].u.ch.code; \
4339 charset = CHAR_CHARSET (c); \
4340 face_id = (ROW)->glyphs[AREA][START].u.ch.face_id; \
4341 \
b4192550
KH
4342 s = (struct glyph_string *) alloca (sizeof *s); \
4343 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4344 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4345 x_append_glyph_string (&HEAD, &TAIL, s); \
4346 s->charset = charset; \
4347 s->x = (X); \
4348 START = x_fill_glyph_string (s, face_id, START, END, \
66ac4b0e 4349 OVERLAPS_P); \
06a2c219
GM
4350 } \
4351 while (0)
4352
4353
b4192550
KH
4354/* Add a glyph string for a composite sequence to the list of strings
4355 between HEAD and TAIL. START is the index of the first glyph in
4356 row area AREA of glyph row ROW that is part of the new glyph
4357 string. END is the index of the last glyph in that glyph row area.
4358 X is the current output position assigned to the new glyph string
4359 constructed. HL overrides that face of the glyph; e.g. it is
4360 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4361 x-position of the drawing area. */
4362
4363#define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, \
4364 TAIL, HL, X, LAST_X, OVERLAPS_P) \
4365 do { \
4366 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp.id; \
4367 int face_id = (ROW)->glyphs[AREA][START].u.cmp.face_id; \
4368 struct composition *cmp = composition_table[cmp_id]; \
4369 int glyph_len = cmp->glyph_len; \
4370 XChar2b *char2b; \
4371 struct face **faces; \
4372 struct glyph_string *first_s = NULL; \
4373 int n; \
4374 \
4375 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4376 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4377 /* At first, fill in `char2b' and `faces'. */ \
4378 for (n = 0; n < glyph_len; n++) \
4379 { \
4380 int c = FAST_GLYPH_CHAR (COMPOSITION_GLYPH (cmp, n)); \
4381 faces[n] = x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4382 face_id, char2b + n, 1); \
4383 } \
4384 \
4385 /* Make glyph_strings for each glyph sequence that is drawable by \
4386 the same face, and append them to HEAD/TAIL. */ \
4387 for (n = 0; n < cmp->glyph_len;) \
4388 { \
4389 s = (struct glyph_string *) alloca (sizeof *s); \
4390 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4391 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4392 s->cmp = cmp; \
4393 s->gidx = n; \
4394 s->charset = 0; \
4395 s->x = (X); \
4396 \
4397 if (n == 0) \
4398 first_s = s; \
4399 \
4400 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4401 } \
4402 \
4403 ++START; \
4404 s = first_s; \
4405 } while (0)
4406
4407
06a2c219
GM
4408/* Build a list of glyph strings between HEAD and TAIL for the glyphs
4409 of AREA of glyph row ROW on window W between indices START and END.
4410 HL overrides the face for drawing glyph strings, e.g. it is
4411 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4412 x-positions of the drawing area.
4413
4414 This is an ugly monster macro construct because we must use alloca
4415 to allocate glyph strings (because x_draw_glyphs can be called
4416 asynchronously). */
4417
8abee2e1 4418#define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4419 do \
4420 { \
4421 HEAD = TAIL = NULL; \
4422 while (START < END) \
4423 { \
4424 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4425 switch (first_glyph->type) \
4426 { \
4427 case CHAR_GLYPH: \
4428 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
66ac4b0e
GM
4429 TAIL, HL, X, LAST_X, \
4430 OVERLAPS_P); \
06a2c219
GM
4431 break; \
4432 \
b4192550
KH
4433 case COMPOSITE_GLYPH: \
4434 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4435 HEAD, TAIL, HL, X, LAST_X,\
4436 OVERLAPS_P); \
4437 break; \
4438 \
06a2c219
GM
4439 case STRETCH_GLYPH: \
4440 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4441 HEAD, TAIL, HL, X, LAST_X); \
4442 break; \
4443 \
4444 case IMAGE_GLYPH: \
4445 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4446 TAIL, HL, X, LAST_X); \
4447 break; \
4448 \
4449 default: \
4450 abort (); \
4451 } \
4452 \
4453 x_set_glyph_string_background_width (s, START, LAST_X); \
4454 (X) += s->width; \
4455 } \
4456 } \
4457 while (0)
4458
4459
4460/* Draw glyphs between START and END in AREA of ROW on window W,
4461 starting at x-position X. X is relative to AREA in W. HL is a
4462 face-override with the following meaning:
4463
4464 DRAW_NORMAL_TEXT draw normally
4465 DRAW_CURSOR draw in cursor face
4466 DRAW_MOUSE_FACE draw in mouse face.
4467 DRAW_INVERSE_VIDEO draw in mode line face
4468 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4469 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4470
4471 If REAL_START is non-null, return in *REAL_START the real starting
4472 position for display. This can be different from START in case
4473 overlapping glyphs must be displayed. If REAL_END is non-null,
4474 return in *REAL_END the real end position for display. This can be
4475 different from END in case overlapping glyphs must be displayed.
4476
66ac4b0e
GM
4477 If OVERLAPS_P is non-zero, draw only the foreground of characters
4478 and clip to the physical height of ROW.
4479
06a2c219
GM
4480 Value is the x-position reached, relative to AREA of W. */
4481
4482static int
66ac4b0e
GM
4483x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4484 overlaps_p)
06a2c219
GM
4485 struct window *w;
4486 int x;
4487 struct glyph_row *row;
4488 enum glyph_row_area area;
4489 int start, end;
4490 enum draw_glyphs_face hl;
4491 int *real_start, *real_end;
66ac4b0e 4492 int overlaps_p;
dc6f92b8 4493{
06a2c219
GM
4494 struct glyph_string *head, *tail;
4495 struct glyph_string *s;
4496 int last_x, area_width;
4497 int x_reached;
4498 int i, j;
4499
4500 /* Let's rather be paranoid than getting a SEGV. */
4501 start = max (0, start);
4502 end = min (end, row->used[area]);
4503 if (real_start)
4504 *real_start = start;
4505 if (real_end)
4506 *real_end = end;
4507
4508 /* Translate X to frame coordinates. Set last_x to the right
4509 end of the drawing area. */
4510 if (row->full_width_p)
4511 {
4512 /* X is relative to the left edge of W, without scroll bars
4513 or flag areas. */
4514 struct frame *f = XFRAME (w->frame);
110859fc 4515 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
06a2c219 4516 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
dc6f92b8 4517
06a2c219
GM
4518 x += window_left_x;
4519 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4520 last_x = window_left_x + area_width;
4521
4522 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4523 {
110859fc 4524 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
06a2c219
GM
4525 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4526 last_x += width;
4527 else
4528 x -= width;
4529 }
dc6f92b8 4530
b9432a85
GM
4531 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4532 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
06a2c219
GM
4533 }
4534 else
dc6f92b8 4535 {
06a2c219
GM
4536 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4537 area_width = window_box_width (w, area);
4538 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
dc6f92b8
JB
4539 }
4540
06a2c219
GM
4541 /* Build a doubly-linked list of glyph_string structures between
4542 head and tail from what we have to draw. Note that the macro
4543 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4544 the reason we use a separate variable `i'. */
4545 i = start;
66ac4b0e
GM
4546 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4547 overlaps_p);
06a2c219
GM
4548 if (tail)
4549 x_reached = tail->x + tail->background_width;
4550 else
4551 x_reached = x;
90e65f07 4552
06a2c219
GM
4553 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4554 the row, redraw some glyphs in front or following the glyph
4555 strings built above. */
66ac4b0e 4556 if (!overlaps_p && row->contains_overlapping_glyphs_p)
06a2c219
GM
4557 {
4558 int dummy_x = 0;
4559 struct glyph_string *h, *t;
4560
4561 /* Compute overhangs for all glyph strings. */
4562 for (s = head; s; s = s->next)
4563 x_compute_glyph_string_overhangs (s);
4564
4565 /* Prepend glyph strings for glyphs in front of the first glyph
4566 string that are overwritten because of the first glyph
4567 string's left overhang. The background of all strings
4568 prepended must be drawn because the first glyph string
4569 draws over it. */
4570 i = x_left_overwritten (head);
4571 if (i >= 0)
4572 {
4573 j = i;
4574 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
66ac4b0e
GM
4575 DRAW_NORMAL_TEXT, dummy_x, last_x,
4576 overlaps_p);
06a2c219
GM
4577 start = i;
4578 if (real_start)
4579 *real_start = start;
4580 x_compute_overhangs_and_x (t, head->x, 1);
4581 x_prepend_glyph_string_lists (&head, &tail, h, t);
4582 }
58769bee 4583
06a2c219
GM
4584 /* Prepend glyph strings for glyphs in front of the first glyph
4585 string that overwrite that glyph string because of their
4586 right overhang. For these strings, only the foreground must
4587 be drawn, because it draws over the glyph string at `head'.
4588 The background must not be drawn because this would overwrite
4589 right overhangs of preceding glyphs for which no glyph
4590 strings exist. */
4591 i = x_left_overwriting (head);
4592 if (i >= 0)
4593 {
4594 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
66ac4b0e
GM
4595 DRAW_NORMAL_TEXT, dummy_x, last_x,
4596 overlaps_p);
06a2c219
GM
4597 for (s = h; s; s = s->next)
4598 s->background_filled_p = 1;
4599 if (real_start)
4600 *real_start = i;
4601 x_compute_overhangs_and_x (t, head->x, 1);
4602 x_prepend_glyph_string_lists (&head, &tail, h, t);
4603 }
dbcb258a 4604
06a2c219
GM
4605 /* Append glyphs strings for glyphs following the last glyph
4606 string tail that are overwritten by tail. The background of
4607 these strings has to be drawn because tail's foreground draws
4608 over it. */
4609 i = x_right_overwritten (tail);
4610 if (i >= 0)
4611 {
4612 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4613 DRAW_NORMAL_TEXT, x, last_x,
4614 overlaps_p);
06a2c219
GM
4615 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4616 x_append_glyph_string_lists (&head, &tail, h, t);
4617 if (real_end)
4618 *real_end = i;
4619 }
dc6f92b8 4620
06a2c219
GM
4621 /* Append glyph strings for glyphs following the last glyph
4622 string tail that overwrite tail. The foreground of such
4623 glyphs has to be drawn because it writes into the background
4624 of tail. The background must not be drawn because it could
4625 paint over the foreground of following glyphs. */
4626 i = x_right_overwriting (tail);
4627 if (i >= 0)
4628 {
4629 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4630 DRAW_NORMAL_TEXT, x, last_x,
4631 overlaps_p);
06a2c219
GM
4632 for (s = h; s; s = s->next)
4633 s->background_filled_p = 1;
4634 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4635 x_append_glyph_string_lists (&head, &tail, h, t);
4636 if (real_end)
4637 *real_end = i;
4638 }
4639 }
58769bee 4640
06a2c219
GM
4641 /* Draw all strings. */
4642 for (s = head; s; s = s->next)
4643 x_draw_glyph_string (s);
dc6f92b8 4644
06a2c219
GM
4645 /* Value is the x-position up to which drawn, relative to AREA of W.
4646 This doesn't include parts drawn because of overhangs. */
4647 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4648 if (!row->full_width_p)
4649 {
4650 if (area > LEFT_MARGIN_AREA)
4651 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4652 if (area > TEXT_AREA)
4653 x_reached -= window_box_width (w, TEXT_AREA);
4654 }
4655 return x_reached;
4656}
dc6f92b8 4657
dc6f92b8 4658
66ac4b0e
GM
4659/* Fix the display of area AREA of overlapping row ROW in window W. */
4660
4661static void
4662x_fix_overlapping_area (w, row, area)
4663 struct window *w;
4664 struct glyph_row *row;
4665 enum glyph_row_area area;
4666{
4667 int i, x;
4668
4669 BLOCK_INPUT;
4670
4671 if (area == LEFT_MARGIN_AREA)
4672 x = 0;
4673 else if (area == TEXT_AREA)
4674 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
4675 else
4676 x = (window_box_width (w, LEFT_MARGIN_AREA)
4677 + window_box_width (w, TEXT_AREA));
4678
4679 for (i = 0; i < row->used[area];)
4680 {
4681 if (row->glyphs[area][i].overlaps_vertically_p)
4682 {
4683 int start = i, start_x = x;
4684
4685 do
4686 {
4687 x += row->glyphs[area][i].pixel_width;
4688 ++i;
4689 }
4690 while (i < row->used[area]
4691 && row->glyphs[area][i].overlaps_vertically_p);
4692
4693 x_draw_glyphs (w, start_x, row, area, start, i,
4694 (row->inverse_p
4695 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4696 NULL, NULL, 1);
4697 }
4698 else
4699 {
4700 x += row->glyphs[area][i].pixel_width;
4701 ++i;
4702 }
4703 }
4704
4705 UNBLOCK_INPUT;
4706}
4707
4708
06a2c219
GM
4709/* Output LEN glyphs starting at START at the nominal cursor position.
4710 Advance the nominal cursor over the text. The global variable
4711 updated_window contains the window being updated, updated_row is
4712 the glyph row being updated, and updated_area is the area of that
4713 row being updated. */
dc6f92b8 4714
06a2c219
GM
4715static void
4716x_write_glyphs (start, len)
4717 struct glyph *start;
4718 int len;
4719{
4720 int x, hpos, real_start, real_end;
d9cdbb3d 4721
06a2c219 4722 xassert (updated_window && updated_row);
dc6f92b8 4723 BLOCK_INPUT;
06a2c219
GM
4724
4725 /* Write glyphs. */
dc6f92b8 4726
06a2c219
GM
4727 hpos = start - updated_row->glyphs[updated_area];
4728 x = x_draw_glyphs (updated_window, output_cursor.x,
4729 updated_row, updated_area,
4730 hpos, hpos + len,
4731 (updated_row->inverse_p
4732 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
66ac4b0e 4733 &real_start, &real_end, 0);
b30ec466 4734
06a2c219
GM
4735 /* If we drew over the cursor, note that it is not visible any more. */
4736 note_overwritten_text_cursor (updated_window, real_start,
4737 real_end - real_start);
dc6f92b8
JB
4738
4739 UNBLOCK_INPUT;
06a2c219
GM
4740
4741 /* Advance the output cursor. */
4742 output_cursor.hpos += len;
4743 output_cursor.x = x;
dc6f92b8
JB
4744}
4745
0cdd0c9f 4746
06a2c219 4747/* Insert LEN glyphs from START at the nominal cursor position. */
0cdd0c9f 4748
06a2c219
GM
4749static void
4750x_insert_glyphs (start, len)
4751 struct glyph *start;
4752 register int len;
4753{
4754 struct frame *f;
4755 struct window *w;
4756 int line_height, shift_by_width, shifted_region_width;
4757 struct glyph_row *row;
4758 struct glyph *glyph;
4759 int frame_x, frame_y, hpos, real_start, real_end;
58769bee 4760
06a2c219 4761 xassert (updated_window && updated_row);
0cdd0c9f 4762 BLOCK_INPUT;
06a2c219
GM
4763 w = updated_window;
4764 f = XFRAME (WINDOW_FRAME (w));
4765
4766 /* Get the height of the line we are in. */
4767 row = updated_row;
4768 line_height = row->height;
4769
4770 /* Get the width of the glyphs to insert. */
4771 shift_by_width = 0;
4772 for (glyph = start; glyph < start + len; ++glyph)
4773 shift_by_width += glyph->pixel_width;
4774
4775 /* Get the width of the region to shift right. */
4776 shifted_region_width = (window_box_width (w, updated_area)
4777 - output_cursor.x
4778 - shift_by_width);
4779
4780 /* Shift right. */
4781 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
4782 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
4783 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
4784 f->output_data.x->normal_gc,
4785 frame_x, frame_y,
4786 shifted_region_width, line_height,
4787 frame_x + shift_by_width, frame_y);
4788
4789 /* Write the glyphs. */
4790 hpos = start - row->glyphs[updated_area];
4791 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
66ac4b0e 4792 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
06a2c219
GM
4793 note_overwritten_text_cursor (w, real_start, real_end - real_start);
4794
4795 /* Advance the output cursor. */
4796 output_cursor.hpos += len;
4797 output_cursor.x += shift_by_width;
0cdd0c9f
RS
4798 UNBLOCK_INPUT;
4799}
0cdd0c9f 4800
0cdd0c9f 4801
06a2c219
GM
4802/* Delete N glyphs at the nominal cursor position. Not implemented
4803 for X frames. */
c83febd7
RS
4804
4805static void
06a2c219
GM
4806x_delete_glyphs (n)
4807 register int n;
c83febd7 4808{
06a2c219 4809 abort ();
c83febd7
RS
4810}
4811
0cdd0c9f 4812
06a2c219
GM
4813/* Erase the current text line from the nominal cursor position
4814 (inclusive) to pixel column TO_X (exclusive). The idea is that
4815 everything from TO_X onward is already erased.
4816
4817 TO_X is a pixel position relative to updated_area of
4818 updated_window. TO_X == -1 means clear to the end of this area. */
dc6f92b8 4819
06a2c219
GM
4820static void
4821x_clear_end_of_line (to_x)
4822 int to_x;
4823{
4824 struct frame *f;
4825 struct window *w = updated_window;
4826 int max_x, min_y, max_y;
4827 int from_x, from_y, to_y;
4828
4829 xassert (updated_window && updated_row);
4830 f = XFRAME (w->frame);
4831
4832 if (updated_row->full_width_p)
4833 {
4834 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
4835 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
4836 && !w->pseudo_window_p)
4837 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
0cdd0c9f 4838 }
06a2c219
GM
4839 else
4840 max_x = window_box_width (w, updated_area);
4841 max_y = window_text_bottom_y (w);
dc6f92b8 4842
06a2c219
GM
4843 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
4844 of window. For TO_X > 0, truncate to end of drawing area. */
4845 if (to_x == 0)
4846 return;
4847 else if (to_x < 0)
4848 to_x = max_x;
4849 else
4850 to_x = min (to_x, max_x);
dbc4e1c1 4851
06a2c219
GM
4852 to_y = min (max_y, output_cursor.y + updated_row->height);
4853
4854 /* Notice if the cursor will be cleared by this operation. */
4855 if (!updated_row->full_width_p)
4856 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
dbc4e1c1 4857
06a2c219
GM
4858 from_x = output_cursor.x;
4859
4860 /* Translate to frame coordinates. */
4861 if (updated_row->full_width_p)
4862 {
4863 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
4864 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
4865 }
0cdd0c9f
RS
4866 else
4867 {
06a2c219
GM
4868 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
4869 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
4870 }
4871
045dee35 4872 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
06a2c219
GM
4873 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
4874 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
4875
4876 /* Prevent inadvertently clearing to end of the X window. */
4877 if (to_x > from_x && to_y > from_y)
4878 {
4879 BLOCK_INPUT;
4880 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4881 from_x, from_y, to_x - from_x, to_y - from_y,
4882 False);
4883 UNBLOCK_INPUT;
0cdd0c9f 4884 }
0cdd0c9f 4885}
dbc4e1c1 4886
0cdd0c9f 4887
06a2c219 4888/* Clear entire frame. If updating_frame is non-null, clear that
b86bd3dd 4889 frame. Otherwise clear the selected frame. */
06a2c219
GM
4890
4891static void
4892x_clear_frame ()
0cdd0c9f 4893{
06a2c219 4894 struct frame *f;
0cdd0c9f 4895
06a2c219
GM
4896 if (updating_frame)
4897 f = updating_frame;
0cdd0c9f 4898 else
b86bd3dd 4899 f = SELECTED_FRAME ();
58769bee 4900
06a2c219
GM
4901 /* Clearing the frame will erase any cursor, so mark them all as no
4902 longer visible. */
4903 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
4904 output_cursor.hpos = output_cursor.vpos = 0;
4905 output_cursor.x = -1;
4906
4907 /* We don't set the output cursor here because there will always
4908 follow an explicit cursor_to. */
4909 BLOCK_INPUT;
4910 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
4911
4912 /* We have to clear the scroll bars, too. If we have changed
4913 colors or something like that, then they should be notified. */
4914 x_scroll_bar_clear (f);
0cdd0c9f 4915
06a2c219
GM
4916 XFlush (FRAME_X_DISPLAY (f));
4917 UNBLOCK_INPUT;
dc6f92b8 4918}
06a2c219
GM
4919
4920
dc6f92b8 4921\f
dbc4e1c1
JB
4922/* Invert the middle quarter of the frame for .15 sec. */
4923
06a2c219
GM
4924/* We use the select system call to do the waiting, so we have to make
4925 sure it's available. If it isn't, we just won't do visual bells. */
4926
dbc4e1c1
JB
4927#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
4928
06a2c219
GM
4929
4930/* Subtract the `struct timeval' values X and Y, storing the result in
4931 *RESULT. Return 1 if the difference is negative, otherwise 0. */
dbc4e1c1
JB
4932
4933static int
4934timeval_subtract (result, x, y)
4935 struct timeval *result, x, y;
4936{
06a2c219
GM
4937 /* Perform the carry for the later subtraction by updating y. This
4938 is safer because on some systems the tv_sec member is unsigned. */
dbc4e1c1
JB
4939 if (x.tv_usec < y.tv_usec)
4940 {
4941 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
4942 y.tv_usec -= 1000000 * nsec;
4943 y.tv_sec += nsec;
4944 }
06a2c219 4945
dbc4e1c1
JB
4946 if (x.tv_usec - y.tv_usec > 1000000)
4947 {
4948 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
4949 y.tv_usec += 1000000 * nsec;
4950 y.tv_sec -= nsec;
4951 }
4952
06a2c219
GM
4953 /* Compute the time remaining to wait. tv_usec is certainly
4954 positive. */
dbc4e1c1
JB
4955 result->tv_sec = x.tv_sec - y.tv_sec;
4956 result->tv_usec = x.tv_usec - y.tv_usec;
4957
06a2c219
GM
4958 /* Return indication of whether the result should be considered
4959 negative. */
dbc4e1c1
JB
4960 return x.tv_sec < y.tv_sec;
4961}
dc6f92b8 4962
dfcf069d 4963void
f676886a
JB
4964XTflash (f)
4965 struct frame *f;
dc6f92b8 4966{
dbc4e1c1 4967 BLOCK_INPUT;
dc6f92b8 4968
dbc4e1c1
JB
4969 {
4970 GC gc;
dc6f92b8 4971
06a2c219
GM
4972 /* Create a GC that will use the GXxor function to flip foreground
4973 pixels into background pixels. */
dbc4e1c1
JB
4974 {
4975 XGCValues values;
dc6f92b8 4976
dbc4e1c1 4977 values.function = GXxor;
7556890b
RS
4978 values.foreground = (f->output_data.x->foreground_pixel
4979 ^ f->output_data.x->background_pixel);
58769bee 4980
334208b7 4981 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dbc4e1c1
JB
4982 GCFunction | GCForeground, &values);
4983 }
dc6f92b8 4984
dbc4e1c1 4985 {
e84e14c3
RS
4986 /* Get the height not including a menu bar widget. */
4987 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
4988 /* Height of each line to flash. */
4989 int flash_height = FRAME_LINE_HEIGHT (f);
4990 /* These will be the left and right margins of the rectangles. */
4991 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
4992 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
4993
4994 int width;
4995
4996 /* Don't flash the area between a scroll bar and the frame
4997 edge it is next to. */
4998 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
4999 {
5000 case vertical_scroll_bar_left:
5001 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5002 break;
5003
5004 case vertical_scroll_bar_right:
5005 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5006 break;
06a2c219
GM
5007
5008 default:
5009 break;
e84e14c3
RS
5010 }
5011
5012 width = flash_right - flash_left;
5013
5014 /* If window is tall, flash top and bottom line. */
5015 if (height > 3 * FRAME_LINE_HEIGHT (f))
5016 {
5017 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5018 flash_left,
5019 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5020 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5021 width, flash_height);
5022 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5023 flash_left,
5024 (height - flash_height
5025 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5026 width, flash_height);
5027 }
5028 else
5029 /* If it is short, flash it all. */
5030 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5031 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5032 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
dc6f92b8 5033
06a2c219 5034 x_flush (f);
dc6f92b8 5035
dbc4e1c1 5036 {
06a2c219 5037 struct timeval wakeup;
dc6f92b8 5038
66c30ea1 5039 EMACS_GET_TIME (wakeup);
dc6f92b8 5040
dbc4e1c1
JB
5041 /* Compute time to wait until, propagating carry from usecs. */
5042 wakeup.tv_usec += 150000;
5043 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5044 wakeup.tv_usec %= 1000000;
5045
5046 /* Keep waiting until past the time wakeup. */
5047 while (1)
5048 {
5049 struct timeval timeout;
5050
66c30ea1 5051 EMACS_GET_TIME (timeout);
dbc4e1c1
JB
5052
5053 /* In effect, timeout = wakeup - timeout.
5054 Break if result would be negative. */
5055 if (timeval_subtract (&timeout, wakeup, timeout))
5056 break;
5057
5058 /* Try to wait that long--but we might wake up sooner. */
c32cdd9a 5059 select (0, NULL, NULL, NULL, &timeout);
dbc4e1c1
JB
5060 }
5061 }
58769bee 5062
e84e14c3
RS
5063 /* If window is tall, flash top and bottom line. */
5064 if (height > 3 * FRAME_LINE_HEIGHT (f))
5065 {
5066 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5067 flash_left,
5068 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5069 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5070 width, flash_height);
5071 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5072 flash_left,
5073 (height - flash_height
5074 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5075 width, flash_height);
5076 }
5077 else
5078 /* If it is short, flash it all. */
5079 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5080 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5081 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5082
334208b7 5083 XFreeGC (FRAME_X_DISPLAY (f), gc);
06a2c219 5084 x_flush (f);
dc6f92b8 5085 }
dbc4e1c1
JB
5086 }
5087
5088 UNBLOCK_INPUT;
dc6f92b8
JB
5089}
5090
06a2c219 5091#endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
dbc4e1c1
JB
5092
5093
dc6f92b8
JB
5094/* Make audible bell. */
5095
dfcf069d 5096void
dc6f92b8
JB
5097XTring_bell ()
5098{
b86bd3dd
GM
5099 struct frame *f = SELECTED_FRAME ();
5100
5101 if (FRAME_X_DISPLAY (f))
5102 {
dbc4e1c1 5103#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
b86bd3dd
GM
5104 if (visible_bell)
5105 XTflash (f);
5106 else
dbc4e1c1 5107#endif
b86bd3dd
GM
5108 {
5109 BLOCK_INPUT;
5110 XBell (FRAME_X_DISPLAY (f), 0);
5111 XFlush (FRAME_X_DISPLAY (f));
5112 UNBLOCK_INPUT;
5113 }
dc6f92b8
JB
5114 }
5115}
06a2c219 5116
dc6f92b8 5117\f
06a2c219
GM
5118/* Specify how many text lines, from the top of the window,
5119 should be affected by insert-lines and delete-lines operations.
5120 This, and those operations, are used only within an update
5121 that is bounded by calls to x_update_begin and x_update_end. */
dc6f92b8 5122
dfcf069d 5123static void
06a2c219
GM
5124XTset_terminal_window (n)
5125 register int n;
dc6f92b8 5126{
06a2c219 5127 /* This function intentionally left blank. */
dc6f92b8
JB
5128}
5129
06a2c219
GM
5130
5131\f
5132/***********************************************************************
5133 Line Dance
5134 ***********************************************************************/
5135
5136/* Perform an insert-lines or delete-lines operation, inserting N
5137 lines or deleting -N lines at vertical position VPOS. */
5138
dfcf069d 5139static void
06a2c219
GM
5140x_ins_del_lines (vpos, n)
5141 int vpos, n;
dc6f92b8
JB
5142{
5143 abort ();
5144}
06a2c219
GM
5145
5146
5147/* Scroll part of the display as described by RUN. */
dc6f92b8 5148
dfcf069d 5149static void
06a2c219
GM
5150x_scroll_run (w, run)
5151 struct window *w;
5152 struct run *run;
dc6f92b8 5153{
06a2c219
GM
5154 struct frame *f = XFRAME (w->frame);
5155 int x, y, width, height, from_y, to_y, bottom_y;
5156
5157 /* Get frame-relative bounding box of the text display area of W,
5158 without mode lines. Include in this box the flags areas to the
5159 left and right of W. */
5160 window_box (w, -1, &x, &y, &width, &height);
110859fc
GM
5161 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5162 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
5163
5164 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5165 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5166 bottom_y = y + height;
dc6f92b8 5167
06a2c219
GM
5168 if (to_y < from_y)
5169 {
5170 /* Scrolling up. Make sure we don't copy part of the mode
5171 line at the bottom. */
5172 if (from_y + run->height > bottom_y)
5173 height = bottom_y - from_y;
5174 else
5175 height = run->height;
5176 }
dc6f92b8 5177 else
06a2c219
GM
5178 {
5179 /* Scolling down. Make sure we don't copy over the mode line.
5180 at the bottom. */
5181 if (to_y + run->height > bottom_y)
5182 height = bottom_y - to_y;
5183 else
5184 height = run->height;
5185 }
7a13e894 5186
06a2c219
GM
5187 BLOCK_INPUT;
5188
5189 /* Cursor off. Will be switched on again in x_update_window_end. */
5190 updated_window = w;
5191 x_clear_cursor (w);
5192
5193 XCopyArea (FRAME_X_DISPLAY (f),
5194 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5195 f->output_data.x->normal_gc,
5196 x, from_y,
5197 width, height,
5198 x, to_y);
5199
5200 UNBLOCK_INPUT;
5201}
dc6f92b8 5202
dc6f92b8 5203
06a2c219
GM
5204\f
5205/***********************************************************************
5206 Exposure Events
5207 ***********************************************************************/
5208
5209/* Redisplay an exposed area of frame F. X and Y are the upper-left
5210 corner of the exposed rectangle. W and H are width and height of
5211 the exposed area. All are pixel values. W or H zero means redraw
5212 the entire frame. */
dc6f92b8 5213
06a2c219
GM
5214static void
5215expose_frame (f, x, y, w, h)
5216 struct frame *f;
5217 int x, y, w, h;
dc6f92b8 5218{
06a2c219 5219 XRectangle r;
dc6f92b8 5220
06a2c219 5221 TRACE ((stderr, "expose_frame "));
dc6f92b8 5222
06a2c219
GM
5223 /* No need to redraw if frame will be redrawn soon. */
5224 if (FRAME_GARBAGED_P (f))
dc6f92b8 5225 {
06a2c219
GM
5226 TRACE ((stderr, " garbaged\n"));
5227 return;
5228 }
5229
5230 /* If basic faces haven't been realized yet, there is no point in
5231 trying to redraw anything. This can happen when we get an expose
5232 event while Emacs is starting, e.g. by moving another window. */
5233 if (FRAME_FACE_CACHE (f) == NULL
5234 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5235 {
5236 TRACE ((stderr, " no faces\n"));
5237 return;
58769bee 5238 }
06a2c219
GM
5239
5240 if (w == 0 || h == 0)
58769bee 5241 {
06a2c219
GM
5242 r.x = r.y = 0;
5243 r.width = CANON_X_UNIT (f) * f->width;
5244 r.height = CANON_Y_UNIT (f) * f->height;
dc6f92b8
JB
5245 }
5246 else
5247 {
06a2c219
GM
5248 r.x = x;
5249 r.y = y;
5250 r.width = w;
5251 r.height = h;
5252 }
5253
5254 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5255 expose_window_tree (XWINDOW (f->root_window), &r);
5256
9ea173e8 5257 if (WINDOWP (f->tool_bar_window))
06a2c219 5258 {
9ea173e8 5259 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
5260 XRectangle window_rect;
5261 XRectangle intersection_rect;
5262 int window_x, window_y, window_width, window_height;
5263
5264
5265 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5266 window_rect.x = window_x;
5267 window_rect.y = window_y;
5268 window_rect.width = window_width;
5269 window_rect.height = window_height;
5270
5271 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5272 expose_window (w, &intersection_rect);
5273 }
5274
5275#ifndef USE_X_TOOLKIT
5276 if (WINDOWP (f->menu_bar_window))
5277 {
5278 struct window *w = XWINDOW (f->menu_bar_window);
5279 XRectangle window_rect;
5280 XRectangle intersection_rect;
5281 int window_x, window_y, window_width, window_height;
5282
5283
5284 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5285 window_rect.x = window_x;
5286 window_rect.y = window_y;
5287 window_rect.width = window_width;
5288 window_rect.height = window_height;
5289
5290 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5291 expose_window (w, &intersection_rect);
dc6f92b8 5292 }
06a2c219 5293#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
5294}
5295
06a2c219
GM
5296
5297/* Redraw (parts) of all windows in the window tree rooted at W that
5298 intersect R. R contains frame pixel coordinates. */
5299
58769bee 5300static void
06a2c219
GM
5301expose_window_tree (w, r)
5302 struct window *w;
5303 XRectangle *r;
dc6f92b8 5304{
06a2c219
GM
5305 while (w)
5306 {
5307 if (!NILP (w->hchild))
5308 expose_window_tree (XWINDOW (w->hchild), r);
5309 else if (!NILP (w->vchild))
5310 expose_window_tree (XWINDOW (w->vchild), r);
5311 else
5312 {
5313 XRectangle window_rect;
5314 XRectangle intersection_rect;
5315 struct frame *f = XFRAME (w->frame);
5316 int window_x, window_y, window_width, window_height;
5317
5318 /* Frame-relative pixel rectangle of W. */
5319 window_box (w, -1, &window_x, &window_y, &window_width,
5320 &window_height);
5321 window_rect.x
5322 = (window_x
110859fc 5323 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
714dc26c 5324 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
06a2c219
GM
5325 window_rect.y = window_y;
5326 window_rect.width
5327 = (window_width
110859fc 5328 + FRAME_X_FLAGS_AREA_WIDTH (f)
06a2c219
GM
5329 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5330 window_rect.height
5331 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5332
5333 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5334 expose_window (w, &intersection_rect);
5335 }
58769bee 5336
06a2c219
GM
5337 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5338 }
5339}
58769bee 5340
dc6f92b8 5341
06a2c219
GM
5342/* Redraw the part of glyph row area AREA of glyph row ROW on window W
5343 which intersects rectangle R. R is in window-relative coordinates. */
5344
5345static void
5346expose_area (w, row, r, area)
5347 struct window *w;
5348 struct glyph_row *row;
5349 XRectangle *r;
5350 enum glyph_row_area area;
5351{
5352 int x;
5353 struct glyph *first = row->glyphs[area];
5354 struct glyph *end = row->glyphs[area] + row->used[area];
5355 struct glyph *last;
5356 int first_x;
5357
5358 /* Set x to the window-relative start position for drawing glyphs of
5359 AREA. The first glyph of the text area can be partially visible.
5360 The first glyphs of other areas cannot. */
5361 if (area == LEFT_MARGIN_AREA)
5362 x = 0;
5363 else if (area == TEXT_AREA)
5364 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5365 else
5366 x = (window_box_width (w, LEFT_MARGIN_AREA)
5367 + window_box_width (w, TEXT_AREA));
5368
6fb13182
GM
5369 if (area == TEXT_AREA && row->fill_line_p)
5370 /* If row extends face to end of line write the whole line. */
5371 x_draw_glyphs (w, x, row, area,
5372 0, row->used[area],
06a2c219 5373 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5374 NULL, NULL, 0);
6fb13182
GM
5375 else
5376 {
5377 /* Find the first glyph that must be redrawn. */
5378 while (first < end
5379 && x + first->pixel_width < r->x)
5380 {
5381 x += first->pixel_width;
5382 ++first;
5383 }
5384
5385 /* Find the last one. */
5386 last = first;
5387 first_x = x;
5388 while (last < end
5389 && x < r->x + r->width)
5390 {
5391 x += last->pixel_width;
5392 ++last;
5393 }
5394
5395 /* Repaint. */
5396 if (last > first)
5397 x_draw_glyphs (w, first_x, row, area,
5398 first - row->glyphs[area],
5399 last - row->glyphs[area],
5400 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5401 NULL, NULL, 0);
5402 }
06a2c219
GM
5403}
5404
58769bee 5405
06a2c219
GM
5406/* Redraw the parts of the glyph row ROW on window W intersecting
5407 rectangle R. R is in window-relative coordinates. */
dc6f92b8 5408
06a2c219
GM
5409static void
5410expose_line (w, row, r)
5411 struct window *w;
5412 struct glyph_row *row;
5413 XRectangle *r;
5414{
5415 xassert (row->enabled_p);
5416
5417 if (row->mode_line_p || w->pseudo_window_p)
5418 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5419 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5420 NULL, NULL, 0);
06a2c219
GM
5421 else
5422 {
5423 if (row->used[LEFT_MARGIN_AREA])
5424 expose_area (w, row, r, LEFT_MARGIN_AREA);
5425 if (row->used[TEXT_AREA])
5426 expose_area (w, row, r, TEXT_AREA);
5427 if (row->used[RIGHT_MARGIN_AREA])
5428 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5429 x_draw_row_bitmaps (w, row);
5430 }
5431}
dc6f92b8 5432
58769bee 5433
06a2c219
GM
5434/* Return non-zero if W's cursor intersects rectangle R. */
5435
5436static int
5437x_phys_cursor_in_rect_p (w, r)
5438 struct window *w;
5439 XRectangle *r;
5440{
5441 XRectangle cr, result;
5442 struct glyph *cursor_glyph;
5443
5444 cursor_glyph = get_phys_cursor_glyph (w);
5445 if (cursor_glyph)
5446 {
5447 cr.x = w->phys_cursor.x;
5448 cr.y = w->phys_cursor.y;
5449 cr.width = cursor_glyph->pixel_width;
5450 cr.height = w->phys_cursor_height;
5451 return x_intersect_rectangles (&cr, r, &result);
5452 }
5453 else
5454 return 0;
dc6f92b8 5455}
dc6f92b8 5456
06a2c219
GM
5457
5458/* Redraw a rectangle of window W. R is a rectangle in window
5459 relative coordinates. Call this function with input blocked. */
dc6f92b8
JB
5460
5461static void
06a2c219
GM
5462expose_window (w, r)
5463 struct window *w;
5464 XRectangle *r;
dc6f92b8 5465{
06a2c219
GM
5466 struct glyph_row *row;
5467 int y;
5468 int yb = window_text_bottom_y (w);
5469 int cursor_cleared_p;
dc6f92b8 5470
80c32bcc
GM
5471 /* If window is not yet fully initialized, do nothing. This can
5472 happen when toolkit scroll bars are used and a window is split.
5473 Reconfiguring the scroll bar will generate an expose for a newly
5474 created window. */
5475 if (w->current_matrix == NULL)
5476 return;
5477
06a2c219
GM
5478 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5479 r->x, r->y, r->width, r->height));
dc6f92b8 5480
06a2c219
GM
5481 /* Convert to window coordinates. */
5482 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5483 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
dc6f92b8 5484
06a2c219
GM
5485 /* Turn off the cursor. */
5486 if (!w->pseudo_window_p
5487 && x_phys_cursor_in_rect_p (w, r))
5488 {
5489 x_clear_cursor (w);
5490 cursor_cleared_p = 1;
5491 }
5492 else
5493 cursor_cleared_p = 0;
5494
5495 /* Find the first row intersecting the rectangle R. */
5496 row = w->current_matrix->rows;
5497 y = 0;
5498 while (row->enabled_p
5499 && y < yb
5500 && y + row->height < r->y)
5501 {
5502 y += row->height;
5503 ++row;
5504 }
5505
dc6f92b8 5506 /* Display the text in the rectangle, one text line at a time. */
06a2c219
GM
5507 while (row->enabled_p
5508 && y < yb
5509 && y < r->y + r->height)
5510 {
5511 expose_line (w, row, r);
5512 y += row->height;
5513 ++row;
5514 }
5515
5516 /* Display the mode line if there is one. */
5517 if (WINDOW_WANTS_MODELINE_P (w)
5518 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5519 row->enabled_p)
5520 && row->y < r->y + r->height)
5521 expose_line (w, row, r);
dc6f92b8 5522
06a2c219 5523 if (!w->pseudo_window_p)
dc6f92b8 5524 {
06a2c219
GM
5525 /* Draw border between windows. */
5526 x_draw_vertical_border (w);
5527
5528 /* Turn the cursor on again. */
5529 if (cursor_cleared_p)
5530 x_update_window_cursor (w, 1);
5531 }
5532}
dc6f92b8 5533
dc6f92b8 5534
06a2c219
GM
5535/* Determine the intersection of two rectangles R1 and R2. Return
5536 the intersection in *RESULT. Value is non-zero if RESULT is not
5537 empty. */
5538
5539static int
5540x_intersect_rectangles (r1, r2, result)
5541 XRectangle *r1, *r2, *result;
5542{
5543 XRectangle *left, *right;
5544 XRectangle *upper, *lower;
5545 int intersection_p = 0;
5546
5547 /* Rearrange so that R1 is the left-most rectangle. */
5548 if (r1->x < r2->x)
5549 left = r1, right = r2;
5550 else
5551 left = r2, right = r1;
5552
5553 /* X0 of the intersection is right.x0, if this is inside R1,
5554 otherwise there is no intersection. */
5555 if (right->x <= left->x + left->width)
5556 {
5557 result->x = right->x;
5558
5559 /* The right end of the intersection is the minimum of the
5560 the right ends of left and right. */
5561 result->width = (min (left->x + left->width, right->x + right->width)
5562 - result->x);
5563
5564 /* Same game for Y. */
5565 if (r1->y < r2->y)
5566 upper = r1, lower = r2;
5567 else
5568 upper = r2, lower = r1;
5569
5570 /* The upper end of the intersection is lower.y0, if this is inside
5571 of upper. Otherwise, there is no intersection. */
5572 if (lower->y <= upper->y + upper->height)
dc43ef94 5573 {
06a2c219
GM
5574 result->y = lower->y;
5575
5576 /* The lower end of the intersection is the minimum of the lower
5577 ends of upper and lower. */
5578 result->height = (min (lower->y + lower->height,
5579 upper->y + upper->height)
5580 - result->y);
5581 intersection_p = 1;
dc43ef94 5582 }
dc6f92b8
JB
5583 }
5584
06a2c219 5585 return intersection_p;
dc6f92b8 5586}
06a2c219
GM
5587
5588
5589
5590
dc6f92b8 5591\f
dc6f92b8 5592static void
334208b7
RS
5593frame_highlight (f)
5594 struct frame *f;
dc6f92b8 5595{
b3e1e05c
JB
5596 /* We used to only do this if Vx_no_window_manager was non-nil, but
5597 the ICCCM (section 4.1.6) says that the window's border pixmap
5598 and border pixel are window attributes which are "private to the
5599 client", so we can always change it to whatever we want. */
5600 BLOCK_INPUT;
334208b7 5601 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5602 f->output_data.x->border_pixel);
b3e1e05c 5603 UNBLOCK_INPUT;
5d46f928 5604 x_update_cursor (f, 1);
dc6f92b8
JB
5605}
5606
5607static void
334208b7
RS
5608frame_unhighlight (f)
5609 struct frame *f;
dc6f92b8 5610{
b3e1e05c
JB
5611 /* We used to only do this if Vx_no_window_manager was non-nil, but
5612 the ICCCM (section 4.1.6) says that the window's border pixmap
5613 and border pixel are window attributes which are "private to the
5614 client", so we can always change it to whatever we want. */
5615 BLOCK_INPUT;
334208b7 5616 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5617 f->output_data.x->border_tile);
b3e1e05c 5618 UNBLOCK_INPUT;
5d46f928 5619 x_update_cursor (f, 1);
dc6f92b8 5620}
dc6f92b8 5621
f676886a
JB
5622/* The focus has changed. Update the frames as necessary to reflect
5623 the new situation. Note that we can't change the selected frame
c5acd733 5624 here, because the Lisp code we are interrupting might become confused.
eb8c3be9 5625 Each event gets marked with the frame in which it occurred, so the
c5acd733 5626 Lisp code can tell when the switch took place by examining the events. */
dc6f92b8 5627
6d4238f3 5628static void
0f941935
KH
5629x_new_focus_frame (dpyinfo, frame)
5630 struct x_display_info *dpyinfo;
f676886a 5631 struct frame *frame;
dc6f92b8 5632{
0f941935 5633 struct frame *old_focus = dpyinfo->x_focus_frame;
dc6f92b8 5634
0f941935 5635 if (frame != dpyinfo->x_focus_frame)
dc6f92b8 5636 {
58769bee 5637 /* Set this before calling other routines, so that they see
f676886a 5638 the correct value of x_focus_frame. */
0f941935 5639 dpyinfo->x_focus_frame = frame;
6d4238f3
JB
5640
5641 if (old_focus && old_focus->auto_lower)
f676886a 5642 x_lower_frame (old_focus);
dc6f92b8
JB
5643
5644#if 0
f676886a 5645 selected_frame = frame;
e0c1aef2
KH
5646 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
5647 selected_frame);
f676886a
JB
5648 Fselect_window (selected_frame->selected_window);
5649 choose_minibuf_frame ();
c118dd06 5650#endif /* ! 0 */
dc6f92b8 5651
0f941935
KH
5652 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
5653 pending_autoraise_frame = dpyinfo->x_focus_frame;
0134a210
RS
5654 else
5655 pending_autoraise_frame = 0;
6d4238f3 5656 }
dc6f92b8 5657
0f941935 5658 x_frame_rehighlight (dpyinfo);
6d4238f3
JB
5659}
5660
37c2c98b
RS
5661/* Handle an event saying the mouse has moved out of an Emacs frame. */
5662
5663void
0f941935
KH
5664x_mouse_leave (dpyinfo)
5665 struct x_display_info *dpyinfo;
37c2c98b 5666{
0f941935 5667 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
37c2c98b 5668}
6d4238f3 5669
f451eb13
JB
5670/* The focus has changed, or we have redirected a frame's focus to
5671 another frame (this happens when a frame uses a surrogate
06a2c219 5672 mini-buffer frame). Shift the highlight as appropriate.
0f941935
KH
5673
5674 The FRAME argument doesn't necessarily have anything to do with which
06a2c219 5675 frame is being highlighted or un-highlighted; we only use it to find
0f941935 5676 the appropriate X display info. */
06a2c219 5677
6d4238f3 5678static void
0f941935
KH
5679XTframe_rehighlight (frame)
5680 struct frame *frame;
6d4238f3 5681{
0f941935
KH
5682 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
5683}
6d4238f3 5684
0f941935
KH
5685static void
5686x_frame_rehighlight (dpyinfo)
5687 struct x_display_info *dpyinfo;
5688{
5689 struct frame *old_highlight = dpyinfo->x_highlight_frame;
5690
5691 if (dpyinfo->x_focus_frame)
6d4238f3 5692 {
0f941935
KH
5693 dpyinfo->x_highlight_frame
5694 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
5695 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
5696 : dpyinfo->x_focus_frame);
5697 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
f451eb13 5698 {
0f941935
KH
5699 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
5700 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
f451eb13 5701 }
dc6f92b8 5702 }
6d4238f3 5703 else
0f941935 5704 dpyinfo->x_highlight_frame = 0;
dc6f92b8 5705
0f941935 5706 if (dpyinfo->x_highlight_frame != old_highlight)
6d4238f3
JB
5707 {
5708 if (old_highlight)
f676886a 5709 frame_unhighlight (old_highlight);
0f941935
KH
5710 if (dpyinfo->x_highlight_frame)
5711 frame_highlight (dpyinfo->x_highlight_frame);
6d4238f3 5712 }
dc6f92b8 5713}
06a2c219
GM
5714
5715
dc6f92b8 5716\f
06a2c219 5717/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
dc6f92b8 5718
28430d3c
JB
5719/* Initialize mode_switch_bit and modifier_meaning. */
5720static void
334208b7
RS
5721x_find_modifier_meanings (dpyinfo)
5722 struct x_display_info *dpyinfo;
28430d3c 5723{
f689eb05 5724 int min_code, max_code;
28430d3c
JB
5725 KeySym *syms;
5726 int syms_per_code;
5727 XModifierKeymap *mods;
5728
334208b7
RS
5729 dpyinfo->meta_mod_mask = 0;
5730 dpyinfo->shift_lock_mask = 0;
5731 dpyinfo->alt_mod_mask = 0;
5732 dpyinfo->super_mod_mask = 0;
5733 dpyinfo->hyper_mod_mask = 0;
58769bee 5734
9658a521 5735#ifdef HAVE_X11R4
334208b7 5736 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
9658a521 5737#else
4a60f8c5
RS
5738 min_code = dpyinfo->display->min_keycode;
5739 max_code = dpyinfo->display->max_keycode;
9658a521
JB
5740#endif
5741
334208b7 5742 syms = XGetKeyboardMapping (dpyinfo->display,
28430d3c
JB
5743 min_code, max_code - min_code + 1,
5744 &syms_per_code);
334208b7 5745 mods = XGetModifierMapping (dpyinfo->display);
28430d3c 5746
58769bee 5747 /* Scan the modifier table to see which modifier bits the Meta and
11edeb03 5748 Alt keysyms are on. */
28430d3c 5749 {
06a2c219 5750 int row, col; /* The row and column in the modifier table. */
28430d3c
JB
5751
5752 for (row = 3; row < 8; row++)
5753 for (col = 0; col < mods->max_keypermod; col++)
5754 {
0299d313
RS
5755 KeyCode code
5756 = mods->modifiermap[(row * mods->max_keypermod) + col];
28430d3c 5757
af92970c
KH
5758 /* Zeroes are used for filler. Skip them. */
5759 if (code == 0)
5760 continue;
5761
28430d3c
JB
5762 /* Are any of this keycode's keysyms a meta key? */
5763 {
5764 int code_col;
5765
5766 for (code_col = 0; code_col < syms_per_code; code_col++)
5767 {
f689eb05 5768 int sym = syms[((code - min_code) * syms_per_code) + code_col];
28430d3c 5769
f689eb05 5770 switch (sym)
28430d3c 5771 {
f689eb05
JB
5772 case XK_Meta_L:
5773 case XK_Meta_R:
334208b7 5774 dpyinfo->meta_mod_mask |= (1 << row);
28430d3c 5775 break;
f689eb05
JB
5776
5777 case XK_Alt_L:
5778 case XK_Alt_R:
334208b7 5779 dpyinfo->alt_mod_mask |= (1 << row);
a3c44b14
RS
5780 break;
5781
5782 case XK_Hyper_L:
5783 case XK_Hyper_R:
334208b7 5784 dpyinfo->hyper_mod_mask |= (1 << row);
a3c44b14
RS
5785 break;
5786
5787 case XK_Super_L:
5788 case XK_Super_R:
334208b7 5789 dpyinfo->super_mod_mask |= (1 << row);
f689eb05 5790 break;
11edeb03
JB
5791
5792 case XK_Shift_Lock:
5793 /* Ignore this if it's not on the lock modifier. */
5794 if ((1 << row) == LockMask)
334208b7 5795 dpyinfo->shift_lock_mask = LockMask;
11edeb03 5796 break;
28430d3c
JB
5797 }
5798 }
5799 }
5800 }
5801 }
5802
f689eb05 5803 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
334208b7 5804 if (! dpyinfo->meta_mod_mask)
a3c44b14 5805 {
334208b7
RS
5806 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
5807 dpyinfo->alt_mod_mask = 0;
a3c44b14 5808 }
f689eb05 5809
148c4b70
RS
5810 /* If some keys are both alt and meta,
5811 make them just meta, not alt. */
334208b7 5812 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
148c4b70 5813 {
334208b7 5814 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
148c4b70 5815 }
58769bee 5816
28430d3c 5817 XFree ((char *) syms);
f689eb05 5818 XFreeModifiermap (mods);
28430d3c
JB
5819}
5820
dfeccd2d
JB
5821/* Convert between the modifier bits X uses and the modifier bits
5822 Emacs uses. */
06a2c219 5823
7c5283e4 5824static unsigned int
334208b7
RS
5825x_x_to_emacs_modifiers (dpyinfo, state)
5826 struct x_display_info *dpyinfo;
dc6f92b8
JB
5827 unsigned int state;
5828{
334208b7
RS
5829 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
5830 | ((state & ControlMask) ? ctrl_modifier : 0)
5831 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
5832 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
5833 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
5834 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
dc6f92b8
JB
5835}
5836
dfeccd2d 5837static unsigned int
334208b7
RS
5838x_emacs_to_x_modifiers (dpyinfo, state)
5839 struct x_display_info *dpyinfo;
dfeccd2d
JB
5840 unsigned int state;
5841{
334208b7
RS
5842 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
5843 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
5844 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
5845 | ((state & shift_modifier) ? ShiftMask : 0)
5846 | ((state & ctrl_modifier) ? ControlMask : 0)
5847 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
dfeccd2d 5848}
d047c4eb
KH
5849
5850/* Convert a keysym to its name. */
5851
5852char *
5853x_get_keysym_name (keysym)
5854 KeySym keysym;
5855{
5856 char *value;
5857
5858 BLOCK_INPUT;
5859 value = XKeysymToString (keysym);
5860 UNBLOCK_INPUT;
5861
5862 return value;
5863}
06a2c219
GM
5864
5865
e4571a43
JB
5866\f
5867/* Mouse clicks and mouse movement. Rah. */
e4571a43 5868
06a2c219
GM
5869/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
5870 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
5871 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
5872 not force the value into range. */
69388238 5873
c8dba240 5874void
69388238 5875pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
e4571a43 5876 FRAME_PTR f;
69388238 5877 register int pix_x, pix_y;
e4571a43
JB
5878 register int *x, *y;
5879 XRectangle *bounds;
69388238 5880 int noclip;
e4571a43 5881{
06a2c219 5882 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
69388238
RS
5883 even for negative values. */
5884 if (pix_x < 0)
7556890b 5885 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
69388238 5886 if (pix_y < 0)
7556890b 5887 pix_y -= (f)->output_data.x->line_height - 1;
69388238 5888
e4571a43
JB
5889 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
5890 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
5891
5892 if (bounds)
5893 {
7556890b
RS
5894 bounds->width = FONT_WIDTH (f->output_data.x->font);
5895 bounds->height = f->output_data.x->line_height;
e4571a43
JB
5896 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
5897 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
5898 }
5899
69388238
RS
5900 if (!noclip)
5901 {
5902 if (pix_x < 0)
5903 pix_x = 0;
3cbd2e0b
RS
5904 else if (pix_x > FRAME_WINDOW_WIDTH (f))
5905 pix_x = FRAME_WINDOW_WIDTH (f);
69388238
RS
5906
5907 if (pix_y < 0)
5908 pix_y = 0;
5909 else if (pix_y > f->height)
5910 pix_y = f->height;
5911 }
e4571a43
JB
5912
5913 *x = pix_x;
5914 *y = pix_y;
5915}
5916
06a2c219
GM
5917
5918/* Given HPOS/VPOS in the current matrix of W, return corresponding
5919 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
5920 can't tell the positions because W's display is not up to date,
5921 return 0. */
5922
5923int
5924glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
5925 struct window *w;
5926 int hpos, vpos;
5927 int *frame_x, *frame_y;
2b5c9e71 5928{
06a2c219
GM
5929 int success_p;
5930
5931 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
5932 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
5933
5934 if (display_completed)
5935 {
5936 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
5937 struct glyph *glyph = row->glyphs[TEXT_AREA];
5938 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
5939
5940 *frame_y = row->y;
5941 *frame_x = row->x;
5942 while (glyph < end)
5943 {
5944 *frame_x += glyph->pixel_width;
5945 ++glyph;
5946 }
5947
5948 success_p = 1;
5949 }
5950 else
5951 {
5952 *frame_y = *frame_x = 0;
5953 success_p = 0;
5954 }
5955
5956 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
5957 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
5958 return success_p;
2b5c9e71
RS
5959}
5960
06a2c219 5961
dc6f92b8
JB
5962/* Prepare a mouse-event in *RESULT for placement in the input queue.
5963
5964 If the event is a button press, then note that we have grabbed
f451eb13 5965 the mouse. */
dc6f92b8
JB
5966
5967static Lisp_Object
f451eb13 5968construct_mouse_click (result, event, f)
dc6f92b8
JB
5969 struct input_event *result;
5970 XButtonEvent *event;
f676886a 5971 struct frame *f;
dc6f92b8 5972{
f451eb13 5973 /* Make the event type no_event; we'll change that when we decide
dc6f92b8 5974 otherwise. */
f451eb13 5975 result->kind = mouse_click;
69388238 5976 result->code = event->button - Button1;
1113d9db 5977 result->timestamp = event->time;
334208b7
RS
5978 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
5979 event->state)
f689eb05 5980 | (event->type == ButtonRelease
58769bee 5981 ? up_modifier
f689eb05 5982 : down_modifier));
dc6f92b8 5983
06a2c219
GM
5984 XSETINT (result->x, event->x);
5985 XSETINT (result->y, event->y);
5986 XSETFRAME (result->frame_or_window, f);
5987 return Qnil;
dc6f92b8 5988}
b849c413 5989
06a2c219
GM
5990#if 0 /* This function isn't called. --gerd */
5991
b849c413
RS
5992/* Prepare a menu-event in *RESULT for placement in the input queue. */
5993
5994static Lisp_Object
5995construct_menu_click (result, event, f)
5996 struct input_event *result;
5997 XButtonEvent *event;
5998 struct frame *f;
5999{
6000 /* Make the event type no_event; we'll change that when we decide
6001 otherwise. */
6002 result->kind = mouse_click;
26459b28 6003 result->code = event->button - Button1;
b849c413 6004 result->timestamp = event->time;
334208b7
RS
6005 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6006 event->state)
b849c413 6007 | (event->type == ButtonRelease
58769bee 6008 ? up_modifier
b849c413
RS
6009 : down_modifier));
6010
e0c1aef2
KH
6011 XSETINT (result->x, event->x);
6012 XSETINT (result->y, -1);
6013 XSETFRAME (result->frame_or_window, f);
b849c413 6014}
06a2c219
GM
6015
6016#endif /* 0 */
6017
69388238 6018\f
90e65f07
JB
6019/* Function to report a mouse movement to the mainstream Emacs code.
6020 The input handler calls this.
6021
6022 We have received a mouse movement event, which is given in *event.
6023 If the mouse is over a different glyph than it was last time, tell
6024 the mainstream emacs code by setting mouse_moved. If not, ask for
6025 another motion event, so we can check again the next time it moves. */
b8009dd1 6026
06a2c219
GM
6027static XMotionEvent last_mouse_motion_event;
6028static Lisp_Object last_mouse_motion_frame;
6029
90e65f07 6030static void
12ba150f 6031note_mouse_movement (frame, event)
f676886a 6032 FRAME_PTR frame;
90e65f07 6033 XMotionEvent *event;
90e65f07 6034{
e5d77022 6035 last_mouse_movement_time = event->time;
06a2c219
GM
6036 last_mouse_motion_event = *event;
6037 XSETFRAME (last_mouse_motion_frame, frame);
e5d77022 6038
27f338af
RS
6039 if (event->window != FRAME_X_WINDOW (frame))
6040 {
39d8bb4d 6041 frame->mouse_moved = 1;
27f338af 6042 last_mouse_scroll_bar = Qnil;
27f338af 6043 note_mouse_highlight (frame, -1, -1);
27f338af
RS
6044 }
6045
90e65f07 6046 /* Has the mouse moved off the glyph it was on at the last sighting? */
27f338af
RS
6047 else if (event->x < last_mouse_glyph.x
6048 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6049 || event->y < last_mouse_glyph.y
6050 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
12ba150f 6051 {
39d8bb4d 6052 frame->mouse_moved = 1;
ab648270 6053 last_mouse_scroll_bar = Qnil;
b8009dd1 6054 note_mouse_highlight (frame, event->x, event->y);
90e65f07
JB
6055 }
6056}
6057
bf1c0ba1 6058/* This is used for debugging, to turn off note_mouse_highlight. */
bf1c0ba1 6059
06a2c219
GM
6060 int disable_mouse_highlight;
6061
6062
6063\f
6064/************************************************************************
6065 Mouse Face
6066 ************************************************************************/
6067
6068/* Find the glyph under window-relative coordinates X/Y in window W.
6069 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6070 strings. Return in *HPOS and *VPOS the row and column number of
6071 the glyph found. Return in *AREA the glyph area containing X.
6072 Value is a pointer to the glyph found or null if X/Y is not on
6073 text, or we can't tell because W's current matrix is not up to
6074 date. */
6075
6076static struct glyph *
6077x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6078 struct window *w;
6079 int x, y;
6080 int *hpos, *vpos, *area;
6081{
6082 struct glyph *glyph, *end;
6083 struct glyph_row *row;
6084 int x0, i, left_area_width;
6085
6086 /* Find row containing Y. Give up if some row is not enabled. */
6087 for (i = 0; i < w->current_matrix->nrows; ++i)
6088 {
6089 row = MATRIX_ROW (w->current_matrix, i);
6090 if (!row->enabled_p)
6091 return NULL;
6092 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6093 break;
6094 }
6095
6096 *vpos = i;
6097 *hpos = 0;
6098
6099 /* Give up if Y is not in the window. */
6100 if (i == w->current_matrix->nrows)
6101 return NULL;
6102
6103 /* Get the glyph area containing X. */
6104 if (w->pseudo_window_p)
6105 {
6106 *area = TEXT_AREA;
6107 x0 = 0;
6108 }
6109 else
6110 {
6111 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6112 if (x < left_area_width)
6113 {
6114 *area = LEFT_MARGIN_AREA;
6115 x0 = 0;
6116 }
6117 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6118 {
6119 *area = TEXT_AREA;
6120 x0 = row->x + left_area_width;
6121 }
6122 else
6123 {
6124 *area = RIGHT_MARGIN_AREA;
6125 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6126 }
6127 }
6128
6129 /* Find glyph containing X. */
6130 glyph = row->glyphs[*area];
6131 end = glyph + row->used[*area];
6132 while (glyph < end)
6133 {
6134 if (x < x0 + glyph->pixel_width)
6135 {
6136 if (w->pseudo_window_p)
6137 break;
6138 else if (BUFFERP (glyph->object))
6139 break;
6140 }
6141
6142 x0 += glyph->pixel_width;
6143 ++glyph;
6144 }
6145
6146 if (glyph == end)
6147 return NULL;
6148
6149 *hpos = glyph - row->glyphs[*area];
6150 return glyph;
6151}
6152
6153
6154/* Convert frame-relative x/y to coordinates relative to window W.
6155 Takes pseudo-windows into account. */
6156
6157static void
6158frame_to_window_pixel_xy (w, x, y)
6159 struct window *w;
6160 int *x, *y;
6161{
6162 if (w->pseudo_window_p)
6163 {
6164 /* A pseudo-window is always full-width, and starts at the
6165 left edge of the frame, plus a frame border. */
6166 struct frame *f = XFRAME (w->frame);
6167 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6168 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6169 }
6170 else
6171 {
6172 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6173 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6174 }
6175}
6176
6177
6178/* Take proper action when mouse has moved to the mode or top line of
6179 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6180 mode line. X is relative to the start of the text display area of
6181 W, so the width of bitmap areas and scroll bars must be subtracted
6182 to get a position relative to the start of the mode line. */
6183
6184static void
6185note_mode_line_highlight (w, x, mode_line_p)
6186 struct window *w;
6187 int x, mode_line_p;
6188{
6189 struct frame *f = XFRAME (w->frame);
6190 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6191 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6192 struct glyph_row *row;
6193
6194 if (mode_line_p)
6195 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6196 else
045dee35 6197 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
06a2c219
GM
6198
6199 if (row->enabled_p)
6200 {
6201 struct glyph *glyph, *end;
6202 Lisp_Object help, map;
6203 int x0;
6204
6205 /* Find the glyph under X. */
6206 glyph = row->glyphs[TEXT_AREA];
6207 end = glyph + row->used[TEXT_AREA];
6208 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
110859fc 6209 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
06a2c219
GM
6210 while (glyph < end
6211 && x >= x0 + glyph->pixel_width)
6212 {
6213 x0 += glyph->pixel_width;
6214 ++glyph;
6215 }
6216
6217 if (glyph < end
6218 && STRINGP (glyph->object)
6219 && XSTRING (glyph->object)->intervals
6220 && glyph->charpos >= 0
6221 && glyph->charpos < XSTRING (glyph->object)->size)
6222 {
6223 /* If we're on a string with `help-echo' text property,
6224 arrange for the help to be displayed. This is done by
6225 setting the global variable help_echo to the help string. */
6226 help = Fget_text_property (make_number (glyph->charpos),
6227 Qhelp_echo, glyph->object);
6228 if (STRINGP (help))
6229 help_echo = help;
6230
6231 /* Change the mouse pointer according to what is under X/Y. */
6232 map = Fget_text_property (make_number (glyph->charpos),
6233 Qlocal_map, glyph->object);
6234 if (!NILP (Fkeymapp (map)))
6235 cursor = f->output_data.x->nontext_cursor;
6236 }
6237 }
6238
6239 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6240}
6241
6242
6243/* Take proper action when the mouse has moved to position X, Y on
6244 frame F as regards highlighting characters that have mouse-face
6245 properties. Also de-highlighting chars where the mouse was before.
27f338af 6246 X and Y can be negative or out of range. */
b8009dd1
RS
6247
6248static void
6249note_mouse_highlight (f, x, y)
06a2c219 6250 struct frame *f;
c32cdd9a 6251 int x, y;
b8009dd1 6252{
06a2c219
GM
6253 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6254 int portion;
b8009dd1
RS
6255 Lisp_Object window;
6256 struct window *w;
6257
06a2c219
GM
6258 /* When a menu is active, don't highlight because this looks odd. */
6259#ifdef USE_X_TOOLKIT
6260 if (popup_activated ())
6261 return;
6262#endif
6263
bf1c0ba1
RS
6264 if (disable_mouse_highlight)
6265 return;
6266
06a2c219
GM
6267 dpyinfo->mouse_face_mouse_x = x;
6268 dpyinfo->mouse_face_mouse_y = y;
6269 dpyinfo->mouse_face_mouse_frame = f;
b8009dd1 6270
06a2c219 6271 if (dpyinfo->mouse_face_defer)
b8009dd1
RS
6272 return;
6273
514e4681
RS
6274 if (gc_in_progress)
6275 {
06a2c219 6276 dpyinfo->mouse_face_deferred_gc = 1;
514e4681
RS
6277 return;
6278 }
6279
b8009dd1 6280 /* Which window is that in? */
06a2c219 6281 window = window_from_coordinates (f, x, y, &portion, 1);
b8009dd1
RS
6282
6283 /* If we were displaying active text in another window, clear that. */
06a2c219
GM
6284 if (! EQ (window, dpyinfo->mouse_face_window))
6285 clear_mouse_face (dpyinfo);
6286
6287 /* Not on a window -> return. */
6288 if (!WINDOWP (window))
6289 return;
6290
6291 /* Convert to window-relative pixel coordinates. */
6292 w = XWINDOW (window);
6293 frame_to_window_pixel_xy (w, &x, &y);
6294
9ea173e8 6295 /* Handle tool-bar window differently since it doesn't display a
06a2c219 6296 buffer. */
9ea173e8 6297 if (EQ (window, f->tool_bar_window))
06a2c219 6298 {
9ea173e8 6299 note_tool_bar_highlight (f, x, y);
06a2c219
GM
6300 return;
6301 }
6302
6303 if (portion == 1 || portion == 3)
6304 {
6305 /* Mouse is on the mode or top line. */
6306 note_mode_line_highlight (w, x, portion == 1);
6307 return;
6308 }
6309 else
6310 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6311 f->output_data.x->text_cursor);
b8009dd1 6312
0cdd0c9f
RS
6313 /* Are we in a window whose display is up to date?
6314 And verify the buffer's text has not changed. */
06a2c219
GM
6315 if (/* Within text portion of the window. */
6316 portion == 0
0cdd0c9f 6317 && EQ (w->window_end_valid, w->buffer)
26459b28
KH
6318 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6319 && (XFASTINT (w->last_overlay_modified)
6320 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
b8009dd1 6321 {
06a2c219
GM
6322 int hpos, vpos, pos, i, area;
6323 struct glyph *glyph;
b8009dd1 6324
06a2c219
GM
6325 /* Find the glyph under X/Y. */
6326 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6327
6328 /* Clear mouse face if X/Y not over text. */
6329 if (glyph == NULL
6330 || area != TEXT_AREA
6331 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
b8009dd1 6332 {
06a2c219
GM
6333 clear_mouse_face (dpyinfo);
6334 return;
6335 }
6336
6337 pos = glyph->charpos;
6338 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6339
6340 /* Check for mouse-face and help-echo. */
6341 {
6342 Lisp_Object mouse_face, overlay, position;
6343 Lisp_Object *overlay_vec;
6344 int len, noverlays;
6345 struct buffer *obuf;
6346 int obegv, ozv;
6347
6348 /* If we get an out-of-range value, return now; avoid an error. */
6349 if (pos > BUF_Z (XBUFFER (w->buffer)))
6350 return;
6351
6352 /* Make the window's buffer temporarily current for
6353 overlays_at and compute_char_face. */
6354 obuf = current_buffer;
6355 current_buffer = XBUFFER (w->buffer);
6356 obegv = BEGV;
6357 ozv = ZV;
6358 BEGV = BEG;
6359 ZV = Z;
6360
6361 /* Is this char mouse-active or does it have help-echo? */
6362 XSETINT (position, pos);
6363
6364 /* Put all the overlays we want in a vector in overlay_vec.
6365 Store the length in len. If there are more than 10, make
6366 enough space for all, and try again. */
6367 len = 10;
6368 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6369 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6370 if (noverlays > len)
6371 {
6372 len = noverlays;
6373 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6374 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6375 }
6376
6377 noverlays = sort_overlays (overlay_vec, noverlays, w);
6378
6379 /* Check mouse-face highlighting. */
6380 if (! (EQ (window, dpyinfo->mouse_face_window)
6381 && vpos >= dpyinfo->mouse_face_beg_row
6382 && vpos <= dpyinfo->mouse_face_end_row
6383 && (vpos > dpyinfo->mouse_face_beg_row
6384 || hpos >= dpyinfo->mouse_face_beg_col)
6385 && (vpos < dpyinfo->mouse_face_end_row
6386 || hpos < dpyinfo->mouse_face_end_col
6387 || dpyinfo->mouse_face_past_end)))
6388 {
6389 /* Clear the display of the old active region, if any. */
6390 clear_mouse_face (dpyinfo);
6391
6392 /* Find the highest priority overlay that has a mouse-face prop. */
6393 overlay = Qnil;
6394 for (i = 0; i < noverlays; i++)
6395 {
6396 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6397 if (!NILP (mouse_face))
6398 {
6399 overlay = overlay_vec[i];
6400 break;
6401 }
6402 }
6403
6404 /* If no overlay applies, get a text property. */
6405 if (NILP (overlay))
6406 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6407
6408 /* Handle the overlay case. */
6409 if (! NILP (overlay))
6410 {
6411 /* Find the range of text around this char that
6412 should be active. */
6413 Lisp_Object before, after;
6414 int ignore;
6415
6416 before = Foverlay_start (overlay);
6417 after = Foverlay_end (overlay);
6418 /* Record this as the current active region. */
6419 fast_find_position (w, XFASTINT (before),
6420 &dpyinfo->mouse_face_beg_col,
6421 &dpyinfo->mouse_face_beg_row,
6422 &dpyinfo->mouse_face_beg_x,
6423 &dpyinfo->mouse_face_beg_y);
6424 dpyinfo->mouse_face_past_end
6425 = !fast_find_position (w, XFASTINT (after),
6426 &dpyinfo->mouse_face_end_col,
6427 &dpyinfo->mouse_face_end_row,
6428 &dpyinfo->mouse_face_end_x,
6429 &dpyinfo->mouse_face_end_y);
6430 dpyinfo->mouse_face_window = window;
6431 dpyinfo->mouse_face_face_id
6432 = face_at_buffer_position (w, pos, 0, 0,
6433 &ignore, pos + 1, 1);
6434
6435 /* Display it as active. */
6436 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6437 }
6438 /* Handle the text property case. */
6439 else if (! NILP (mouse_face))
6440 {
6441 /* Find the range of text around this char that
6442 should be active. */
6443 Lisp_Object before, after, beginning, end;
6444 int ignore;
6445
6446 beginning = Fmarker_position (w->start);
6447 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6448 - XFASTINT (w->window_end_pos)));
6449 before
6450 = Fprevious_single_property_change (make_number (pos + 1),
6451 Qmouse_face,
6452 w->buffer, beginning);
6453 after
6454 = Fnext_single_property_change (position, Qmouse_face,
6455 w->buffer, end);
6456 /* Record this as the current active region. */
6457 fast_find_position (w, XFASTINT (before),
6458 &dpyinfo->mouse_face_beg_col,
6459 &dpyinfo->mouse_face_beg_row,
6460 &dpyinfo->mouse_face_beg_x,
6461 &dpyinfo->mouse_face_beg_y);
6462 dpyinfo->mouse_face_past_end
6463 = !fast_find_position (w, XFASTINT (after),
6464 &dpyinfo->mouse_face_end_col,
6465 &dpyinfo->mouse_face_end_row,
6466 &dpyinfo->mouse_face_end_x,
6467 &dpyinfo->mouse_face_end_y);
6468 dpyinfo->mouse_face_window = window;
6469 dpyinfo->mouse_face_face_id
6470 = face_at_buffer_position (w, pos, 0, 0,
6471 &ignore, pos + 1, 1);
6472
6473 /* Display it as active. */
6474 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6475 }
6476 }
6477
6478 /* Look for a `help-echo' property. */
6479 {
6480 Lisp_Object help;
6481
6482 /* Check overlays first. */
6483 help = Qnil;
6484 for (i = 0; i < noverlays && !STRINGP (help); ++i)
6485 help = Foverlay_get (overlay_vec[i], Qhelp_echo);
6486
6487 /* Try text properties. */
6488 if (!STRINGP (help)
6489 && ((STRINGP (glyph->object)
6490 && glyph->charpos >= 0
6491 && glyph->charpos < XSTRING (glyph->object)->size)
6492 || (BUFFERP (glyph->object)
6493 && glyph->charpos >= BEGV
6494 && glyph->charpos < ZV)))
6495 help = Fget_text_property (make_number (glyph->charpos),
6496 Qhelp_echo, glyph->object);
6497
6498 if (STRINGP (help))
6499 help_echo = help;
6500 }
6501
6502 BEGV = obegv;
6503 ZV = ozv;
6504 current_buffer = obuf;
6505 }
6506 }
6507}
6508
6509static void
6510redo_mouse_highlight ()
6511{
6512 if (!NILP (last_mouse_motion_frame)
6513 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6514 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6515 last_mouse_motion_event.x,
6516 last_mouse_motion_event.y);
6517}
6518
6519
6520\f
6521/***********************************************************************
9ea173e8 6522 Tool-bars
06a2c219
GM
6523 ***********************************************************************/
6524
9ea173e8
GM
6525static int x_tool_bar_item P_ ((struct frame *, int, int,
6526 struct glyph **, int *, int *, int *));
06a2c219 6527
9ea173e8 6528/* Tool-bar item index of the item on which a mouse button was pressed
06a2c219
GM
6529 or -1. */
6530
9ea173e8 6531static int last_tool_bar_item;
06a2c219
GM
6532
6533
9ea173e8
GM
6534/* Get information about the tool-bar item at position X/Y on frame F.
6535 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6536 the current matrix of the tool-bar window of F, or NULL if not
6537 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
6538 item in F->current_tool_bar_items. Value is
06a2c219 6539
9ea173e8 6540 -1 if X/Y is not on a tool-bar item
06a2c219
GM
6541 0 if X/Y is on the same item that was highlighted before.
6542 1 otherwise. */
6543
6544static int
9ea173e8 6545x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
06a2c219
GM
6546 struct frame *f;
6547 int x, y;
6548 struct glyph **glyph;
6549 int *hpos, *vpos, *prop_idx;
6550{
6551 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6552 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6553 int area;
6554
6555 /* Find the glyph under X/Y. */
6556 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6557 if (*glyph == NULL)
6558 return -1;
6559
9ea173e8
GM
6560 /* Get the start of this tool-bar item's properties in
6561 f->current_tool_bar_items. */
6562 if (!tool_bar_item_info (f, *glyph, prop_idx))
06a2c219
GM
6563 return -1;
6564
6565 /* Is mouse on the highlighted item? */
9ea173e8 6566 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
06a2c219
GM
6567 && *vpos >= dpyinfo->mouse_face_beg_row
6568 && *vpos <= dpyinfo->mouse_face_end_row
6569 && (*vpos > dpyinfo->mouse_face_beg_row
6570 || *hpos >= dpyinfo->mouse_face_beg_col)
6571 && (*vpos < dpyinfo->mouse_face_end_row
6572 || *hpos < dpyinfo->mouse_face_end_col
6573 || dpyinfo->mouse_face_past_end))
6574 return 0;
6575
6576 return 1;
6577}
6578
6579
9ea173e8 6580/* Handle mouse button event on the tool-bar of frame F, at
06a2c219
GM
6581 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6582 or ButtonRelase. */
6583
6584static void
9ea173e8 6585x_handle_tool_bar_click (f, button_event)
06a2c219
GM
6586 struct frame *f;
6587 XButtonEvent *button_event;
6588{
6589 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6590 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6591 int hpos, vpos, prop_idx;
6592 struct glyph *glyph;
6593 Lisp_Object enabled_p;
6594 int x = button_event->x;
6595 int y = button_event->y;
6596
9ea173e8 6597 /* If not on the highlighted tool-bar item, return. */
06a2c219 6598 frame_to_window_pixel_xy (w, &x, &y);
9ea173e8 6599 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
06a2c219
GM
6600 return;
6601
6602 /* If item is disabled, do nothing. */
9ea173e8
GM
6603 enabled_p = (XVECTOR (f->current_tool_bar_items)
6604 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
06a2c219
GM
6605 if (NILP (enabled_p))
6606 return;
6607
6608 if (button_event->type == ButtonPress)
6609 {
6610 /* Show item in pressed state. */
6611 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6612 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9ea173e8 6613 last_tool_bar_item = prop_idx;
06a2c219
GM
6614 }
6615 else
6616 {
6617 Lisp_Object key, frame;
6618 struct input_event event;
6619
6620 /* Show item in released state. */
6621 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6622 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6623
9ea173e8
GM
6624 key = (XVECTOR (f->current_tool_bar_items)
6625 ->contents[prop_idx + TOOL_BAR_ITEM_KEY]);
06a2c219
GM
6626
6627 XSETFRAME (frame, f);
9ea173e8
GM
6628 event.kind = TOOL_BAR_EVENT;
6629 event.frame_or_window = Fcons (frame, Fcons (Qtool_bar, Qnil));
06a2c219
GM
6630 kbd_buffer_store_event (&event);
6631
9ea173e8 6632 event.kind = TOOL_BAR_EVENT;
06a2c219
GM
6633 event.frame_or_window = Fcons (frame, key);
6634 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6635 button_event->state);
6636 kbd_buffer_store_event (&event);
9ea173e8 6637 last_tool_bar_item = -1;
06a2c219
GM
6638 }
6639}
6640
6641
9ea173e8
GM
6642/* Possibly highlight a tool-bar item on frame F when mouse moves to
6643 tool-bar window-relative coordinates X/Y. Called from
06a2c219
GM
6644 note_mouse_highlight. */
6645
6646static void
9ea173e8 6647note_tool_bar_highlight (f, x, y)
06a2c219
GM
6648 struct frame *f;
6649 int x, y;
6650{
9ea173e8 6651 Lisp_Object window = f->tool_bar_window;
06a2c219
GM
6652 struct window *w = XWINDOW (window);
6653 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6654 int hpos, vpos;
6655 struct glyph *glyph;
6656 struct glyph_row *row;
5c187dee 6657 int i;
06a2c219
GM
6658 Lisp_Object enabled_p;
6659 int prop_idx;
6660 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
5c187dee 6661 int mouse_down_p, rc;
06a2c219
GM
6662
6663 /* Function note_mouse_highlight is called with negative x(y
6664 values when mouse moves outside of the frame. */
6665 if (x <= 0 || y <= 0)
6666 {
6667 clear_mouse_face (dpyinfo);
6668 return;
6669 }
6670
9ea173e8 6671 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
06a2c219
GM
6672 if (rc < 0)
6673 {
9ea173e8 6674 /* Not on tool-bar item. */
06a2c219
GM
6675 clear_mouse_face (dpyinfo);
6676 return;
6677 }
6678 else if (rc == 0)
9ea173e8 6679 /* On same tool-bar item as before. */
06a2c219 6680 goto set_help_echo;
b8009dd1 6681
06a2c219
GM
6682 clear_mouse_face (dpyinfo);
6683
9ea173e8 6684 /* Mouse is down, but on different tool-bar item? */
06a2c219
GM
6685 mouse_down_p = (dpyinfo->grabbed
6686 && f == last_mouse_frame
6687 && FRAME_LIVE_P (f));
6688 if (mouse_down_p
9ea173e8 6689 && last_tool_bar_item != prop_idx)
06a2c219
GM
6690 return;
6691
6692 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
6693 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
6694
9ea173e8
GM
6695 /* If tool-bar item is not enabled, don't highlight it. */
6696 enabled_p = (XVECTOR (f->current_tool_bar_items)
6697 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
06a2c219
GM
6698 if (!NILP (enabled_p))
6699 {
6700 /* Compute the x-position of the glyph. In front and past the
6701 image is a space. We include this is the highlighted area. */
6702 row = MATRIX_ROW (w->current_matrix, vpos);
6703 for (i = x = 0; i < hpos; ++i)
6704 x += row->glyphs[TEXT_AREA][i].pixel_width;
6705
6706 /* Record this as the current active region. */
6707 dpyinfo->mouse_face_beg_col = hpos;
6708 dpyinfo->mouse_face_beg_row = vpos;
6709 dpyinfo->mouse_face_beg_x = x;
6710 dpyinfo->mouse_face_beg_y = row->y;
6711 dpyinfo->mouse_face_past_end = 0;
6712
6713 dpyinfo->mouse_face_end_col = hpos + 1;
6714 dpyinfo->mouse_face_end_row = vpos;
6715 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
6716 dpyinfo->mouse_face_end_y = row->y;
6717 dpyinfo->mouse_face_window = window;
9ea173e8 6718 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
06a2c219
GM
6719
6720 /* Display it as active. */
6721 show_mouse_face (dpyinfo, draw);
6722 dpyinfo->mouse_face_image_state = draw;
b8009dd1 6723 }
06a2c219
GM
6724
6725 set_help_echo:
6726
9ea173e8 6727 /* Set help_echo to a help string.to display for this tool-bar item.
06a2c219 6728 XTread_socket does the rest. */
9ea173e8
GM
6729 help_echo = (XVECTOR (f->current_tool_bar_items)
6730 ->contents[prop_idx + TOOL_BAR_ITEM_HELP]);
06a2c219 6731 if (!STRINGP (help_echo))
9ea173e8
GM
6732 help_echo = (XVECTOR (f->current_tool_bar_items)
6733 ->contents[prop_idx + TOOL_BAR_ITEM_CAPTION]);
b8009dd1 6734}
4d73d038 6735
06a2c219
GM
6736
6737\f
6738/* Find the glyph matrix position of buffer position POS in window W.
6739 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
6740 current glyphs must be up to date. If POS is above window start
6741 return (0, 0, 0, 0). If POS is after end of W, return end of
6742 last line in W. */
b8009dd1
RS
6743
6744static int
06a2c219
GM
6745fast_find_position (w, pos, hpos, vpos, x, y)
6746 struct window *w;
b8009dd1 6747 int pos;
06a2c219 6748 int *hpos, *vpos, *x, *y;
b8009dd1 6749{
b8009dd1 6750 int i;
bf1c0ba1 6751 int lastcol;
06a2c219
GM
6752 int maybe_next_line_p = 0;
6753 int line_start_position;
6754 int yb = window_text_bottom_y (w);
6755 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
6756 struct glyph_row *best_row = row;
6757 int row_vpos = 0, best_row_vpos = 0;
6758 int current_x;
6759
6760 while (row->y < yb)
b8009dd1 6761 {
06a2c219
GM
6762 if (row->used[TEXT_AREA])
6763 line_start_position = row->glyphs[TEXT_AREA]->charpos;
6764 else
6765 line_start_position = 0;
6766
6767 if (line_start_position > pos)
b8009dd1 6768 break;
77b68646
RS
6769 /* If the position sought is the end of the buffer,
6770 don't include the blank lines at the bottom of the window. */
06a2c219
GM
6771 else if (line_start_position == pos
6772 && pos == BUF_ZV (XBUFFER (w->buffer)))
77b68646 6773 {
06a2c219 6774 maybe_next_line_p = 1;
77b68646
RS
6775 break;
6776 }
06a2c219
GM
6777 else if (line_start_position > 0)
6778 {
6779 best_row = row;
6780 best_row_vpos = row_vpos;
6781 }
6782
6783 ++row;
6784 ++row_vpos;
b8009dd1 6785 }
06a2c219
GM
6786
6787 /* Find the right column within BEST_ROW. */
6788 lastcol = 0;
6789 current_x = best_row->x;
6790 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
bf1c0ba1 6791 {
06a2c219
GM
6792 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
6793 int charpos;
6794
6795 charpos = glyph->charpos;
6796 if (charpos == pos)
bf1c0ba1 6797 {
06a2c219
GM
6798 *hpos = i;
6799 *vpos = best_row_vpos;
6800 *x = current_x;
6801 *y = best_row->y;
bf1c0ba1
RS
6802 return 1;
6803 }
06a2c219 6804 else if (charpos > pos)
4d73d038 6805 break;
06a2c219
GM
6806 else if (charpos > 0)
6807 lastcol = i;
6808
6809 current_x += glyph->pixel_width;
bf1c0ba1 6810 }
b8009dd1 6811
77b68646
RS
6812 /* If we're looking for the end of the buffer,
6813 and we didn't find it in the line we scanned,
6814 use the start of the following line. */
06a2c219 6815 if (maybe_next_line_p)
77b68646 6816 {
06a2c219
GM
6817 ++best_row;
6818 ++best_row_vpos;
6819 lastcol = 0;
6820 current_x = best_row->x;
77b68646
RS
6821 }
6822
06a2c219
GM
6823 *vpos = best_row_vpos;
6824 *hpos = lastcol + 1;
6825 *x = current_x;
6826 *y = best_row->y;
b8009dd1
RS
6827 return 0;
6828}
6829
06a2c219 6830
b8009dd1
RS
6831/* Display the active region described by mouse_face_*
6832 in its mouse-face if HL > 0, in its normal face if HL = 0. */
6833
6834static void
06a2c219 6835show_mouse_face (dpyinfo, draw)
7a13e894 6836 struct x_display_info *dpyinfo;
06a2c219 6837 enum draw_glyphs_face draw;
b8009dd1 6838{
7a13e894 6839 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
06a2c219 6840 struct frame *f = XFRAME (WINDOW_FRAME (w));
b8009dd1 6841 int i;
06a2c219
GM
6842 int cursor_off_p = 0;
6843 struct cursor_pos saved_cursor;
6844
6845 saved_cursor = output_cursor;
6846
6847 /* If window is in the process of being destroyed, don't bother
6848 to do anything. */
6849 if (w->current_matrix == NULL)
6850 goto set_x_cursor;
6851
6852 /* Recognize when we are called to operate on rows that don't exist
6853 anymore. This can happen when a window is split. */
6854 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
6855 goto set_x_cursor;
6856
6857 set_output_cursor (&w->phys_cursor);
6858
6859 /* Note that mouse_face_beg_row etc. are window relative. */
6860 for (i = dpyinfo->mouse_face_beg_row;
6861 i <= dpyinfo->mouse_face_end_row;
6862 i++)
6863 {
6864 int start_hpos, end_hpos, start_x;
6865 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
6866
6867 /* Don't do anything if row doesn't have valid contents. */
6868 if (!row->enabled_p)
6869 continue;
6870
6871 /* For all but the first row, the highlight starts at column 0. */
6872 if (i == dpyinfo->mouse_face_beg_row)
6873 {
6874 start_hpos = dpyinfo->mouse_face_beg_col;
6875 start_x = dpyinfo->mouse_face_beg_x;
6876 }
6877 else
6878 {
6879 start_hpos = 0;
6880 start_x = 0;
6881 }
6882
6883 if (i == dpyinfo->mouse_face_end_row)
6884 end_hpos = dpyinfo->mouse_face_end_col;
6885 else
6886 end_hpos = row->used[TEXT_AREA];
6887
6888 /* If the cursor's in the text we are about to rewrite, turn the
6889 cursor off. */
6890 if (!w->pseudo_window_p
6891 && i == output_cursor.vpos
6892 && output_cursor.hpos >= start_hpos - 1
6893 && output_cursor.hpos <= end_hpos)
514e4681 6894 {
06a2c219
GM
6895 x_update_window_cursor (w, 0);
6896 cursor_off_p = 1;
514e4681 6897 }
b8009dd1 6898
06a2c219 6899 if (end_hpos > start_hpos)
54a91a0f 6900 x_draw_glyphs (w, start_x, row, TEXT_AREA,
66ac4b0e 6901 start_hpos, end_hpos, draw, NULL, NULL, 0);
b8009dd1
RS
6902 }
6903
514e4681 6904 /* If we turned the cursor off, turn it back on. */
06a2c219
GM
6905 if (cursor_off_p)
6906 x_display_cursor (w, 1,
6907 output_cursor.hpos, output_cursor.vpos,
6908 output_cursor.x, output_cursor.y);
2729a2b5 6909
06a2c219 6910 output_cursor = saved_cursor;
fb3b7de5 6911
06a2c219
GM
6912 set_x_cursor:
6913
6914 /* Change the mouse cursor. */
6915 if (draw == DRAW_NORMAL_TEXT)
6916 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6917 f->output_data.x->text_cursor);
6918 else if (draw == DRAW_MOUSE_FACE)
334208b7 6919 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 6920 f->output_data.x->cross_cursor);
27ead1d5 6921 else
334208b7 6922 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
06a2c219 6923 f->output_data.x->nontext_cursor);
b8009dd1
RS
6924}
6925
6926/* Clear out the mouse-highlighted active region.
06a2c219 6927 Redraw it un-highlighted first. */
b8009dd1 6928
06a2c219 6929void
7a13e894
RS
6930clear_mouse_face (dpyinfo)
6931 struct x_display_info *dpyinfo;
b8009dd1 6932{
06a2c219
GM
6933 if (tip_frame)
6934 return;
6935
7a13e894 6936 if (! NILP (dpyinfo->mouse_face_window))
06a2c219 6937 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
b8009dd1 6938
7a13e894
RS
6939 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
6940 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
6941 dpyinfo->mouse_face_window = Qnil;
b8009dd1 6942}
e687d06e
RS
6943
6944/* Just discard the mouse face information for frame F, if any.
6945 This is used when the size of F is changed. */
6946
dfcf069d 6947void
e687d06e
RS
6948cancel_mouse_face (f)
6949 FRAME_PTR f;
6950{
6951 Lisp_Object window;
6952 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6953
6954 window = dpyinfo->mouse_face_window;
6955 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
6956 {
6957 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
6958 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
6959 dpyinfo->mouse_face_window = Qnil;
6960 }
6961}
b8009dd1 6962\f
ab648270
JB
6963static struct scroll_bar *x_window_to_scroll_bar ();
6964static void x_scroll_bar_report_motion ();
12ba150f 6965
90e65f07 6966/* Return the current position of the mouse.
2d7fc7e8 6967 *fp should be a frame which indicates which display to ask about.
90e65f07 6968
2d7fc7e8 6969 If the mouse movement started in a scroll bar, set *fp, *bar_window,
ab648270 6970 and *part to the frame, window, and scroll bar part that the mouse
12ba150f 6971 is over. Set *x and *y to the portion and whole of the mouse's
ab648270 6972 position on the scroll bar.
12ba150f 6973
2d7fc7e8 6974 If the mouse movement started elsewhere, set *fp to the frame the
12ba150f
JB
6975 mouse is on, *bar_window to nil, and *x and *y to the character cell
6976 the mouse is over.
6977
06a2c219 6978 Set *time to the server time-stamp for the time at which the mouse
12ba150f
JB
6979 was at this position.
6980
a135645a
RS
6981 Don't store anything if we don't have a valid set of values to report.
6982
90e65f07 6983 This clears the mouse_moved flag, so we can wait for the next mouse
f5bb65ec 6984 movement. */
90e65f07
JB
6985
6986static void
1cf412ec 6987XTmouse_position (fp, insist, bar_window, part, x, y, time)
334208b7 6988 FRAME_PTR *fp;
1cf412ec 6989 int insist;
12ba150f 6990 Lisp_Object *bar_window;
ab648270 6991 enum scroll_bar_part *part;
90e65f07 6992 Lisp_Object *x, *y;
e5d77022 6993 unsigned long *time;
90e65f07 6994{
a135645a
RS
6995 FRAME_PTR f1;
6996
90e65f07
JB
6997 BLOCK_INPUT;
6998
8bcee03e 6999 if (! NILP (last_mouse_scroll_bar) && insist == 0)
334208b7 7000 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
90e65f07
JB
7001 else
7002 {
12ba150f
JB
7003 Window root;
7004 int root_x, root_y;
90e65f07 7005
12ba150f
JB
7006 Window dummy_window;
7007 int dummy;
7008
39d8bb4d
KH
7009 Lisp_Object frame, tail;
7010
7011 /* Clear the mouse-moved flag for every frame on this display. */
7012 FOR_EACH_FRAME (tail, frame)
7013 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7014 XFRAME (frame)->mouse_moved = 0;
7015
ab648270 7016 last_mouse_scroll_bar = Qnil;
12ba150f
JB
7017
7018 /* Figure out which root window we're on. */
334208b7
RS
7019 XQueryPointer (FRAME_X_DISPLAY (*fp),
7020 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
12ba150f
JB
7021
7022 /* The root window which contains the pointer. */
7023 &root,
7024
7025 /* Trash which we can't trust if the pointer is on
7026 a different screen. */
7027 &dummy_window,
7028
7029 /* The position on that root window. */
58769bee 7030 &root_x, &root_y,
12ba150f
JB
7031
7032 /* More trash we can't trust. */
7033 &dummy, &dummy,
7034
7035 /* Modifier keys and pointer buttons, about which
7036 we don't care. */
7037 (unsigned int *) &dummy);
7038
7039 /* Now we have a position on the root; find the innermost window
7040 containing the pointer. */
7041 {
7042 Window win, child;
7043 int win_x, win_y;
06a2c219 7044 int parent_x = 0, parent_y = 0;
e99db5a1 7045 int count;
12ba150f
JB
7046
7047 win = root;
69388238 7048
2d7fc7e8
RS
7049 /* XTranslateCoordinates can get errors if the window
7050 structure is changing at the same time this function
7051 is running. So at least we must not crash from them. */
7052
e99db5a1 7053 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
2d7fc7e8 7054
334208b7 7055 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
23faf38f 7056 && FRAME_LIVE_P (last_mouse_frame))
12ba150f 7057 {
69388238
RS
7058 /* If mouse was grabbed on a frame, give coords for that frame
7059 even if the mouse is now outside it. */
334208b7 7060 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
69388238 7061
12ba150f 7062 /* From-window, to-window. */
69388238 7063 root, FRAME_X_WINDOW (last_mouse_frame),
12ba150f
JB
7064
7065 /* From-position, to-position. */
7066 root_x, root_y, &win_x, &win_y,
7067
7068 /* Child of win. */
7069 &child);
69388238
RS
7070 f1 = last_mouse_frame;
7071 }
7072 else
7073 {
7074 while (1)
7075 {
334208b7 7076 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
12ba150f 7077
69388238
RS
7078 /* From-window, to-window. */
7079 root, win,
12ba150f 7080
69388238
RS
7081 /* From-position, to-position. */
7082 root_x, root_y, &win_x, &win_y,
7083
7084 /* Child of win. */
7085 &child);
7086
9af3143a 7087 if (child == None || child == win)
69388238
RS
7088 break;
7089
7090 win = child;
7091 parent_x = win_x;
7092 parent_y = win_y;
7093 }
12ba150f 7094
69388238
RS
7095 /* Now we know that:
7096 win is the innermost window containing the pointer
7097 (XTC says it has no child containing the pointer),
7098 win_x and win_y are the pointer's position in it
7099 (XTC did this the last time through), and
7100 parent_x and parent_y are the pointer's position in win's parent.
7101 (They are what win_x and win_y were when win was child.
7102 If win is the root window, it has no parent, and
7103 parent_{x,y} are invalid, but that's okay, because we'll
7104 never use them in that case.) */
7105
7106 /* Is win one of our frames? */
19126e11 7107 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
69388238 7108 }
58769bee 7109
2d7fc7e8
RS
7110 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7111 f1 = 0;
7112
e99db5a1 7113 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
2d7fc7e8 7114
ab648270 7115 /* If not, is it one of our scroll bars? */
a135645a 7116 if (! f1)
12ba150f 7117 {
ab648270 7118 struct scroll_bar *bar = x_window_to_scroll_bar (win);
12ba150f
JB
7119
7120 if (bar)
7121 {
a135645a 7122 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
7123 win_x = parent_x;
7124 win_y = parent_y;
7125 }
7126 }
90e65f07 7127
8bcee03e 7128 if (f1 == 0 && insist > 0)
b86bd3dd 7129 f1 = SELECTED_FRAME ();
1cf412ec 7130
a135645a 7131 if (f1)
12ba150f 7132 {
06a2c219
GM
7133 /* Ok, we found a frame. Store all the values.
7134 last_mouse_glyph is a rectangle used to reduce the
7135 generation of mouse events. To not miss any motion
7136 events, we must divide the frame into rectangles of the
7137 size of the smallest character that could be displayed
7138 on it, i.e. into the same rectangles that matrices on
7139 the frame are divided into. */
7140
7141#if OLD_REDISPLAY_CODE
2b5c9e71 7142 int ignore1, ignore2;
2b5c9e71 7143 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
334208b7 7144 &last_mouse_glyph,
1cf412ec
RS
7145 FRAME_X_DISPLAY_INFO (f1)->grabbed
7146 || insist);
06a2c219
GM
7147#else
7148 {
7149 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7150 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7151 int x = win_x;
7152 int y = win_y;
7153
7154 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7155 round down even for negative values. */
7156 if (x < 0)
7157 x -= width - 1;
7158 if (y < 0)
7159 y -= height - 1;
7160
7161 last_mouse_glyph.width = width;
7162 last_mouse_glyph.height = height;
7163 last_mouse_glyph.x = (x + width - 1) / width * width;
7164 last_mouse_glyph.y = (y + height - 1) / height * height;
7165 }
7166#endif
12ba150f
JB
7167
7168 *bar_window = Qnil;
7169 *part = 0;
334208b7 7170 *fp = f1;
e0c1aef2
KH
7171 XSETINT (*x, win_x);
7172 XSETINT (*y, win_y);
12ba150f
JB
7173 *time = last_mouse_movement_time;
7174 }
7175 }
7176 }
90e65f07
JB
7177
7178 UNBLOCK_INPUT;
7179}
f451eb13 7180
06a2c219
GM
7181
7182DEFUN ("xt-process-timeouts", Fxt_process_timeouts, Sxt_process_timeouts,
7183 0, 0, 0,
7184 "Arrange for Xt timeout callbacks to be called.")
7185 ()
7186{
7187#ifdef USE_X_TOOLKIT
7188 BLOCK_INPUT;
7189 while (XtAppPending (Xt_app_con) & XtIMTimer)
7190 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7191 UNBLOCK_INPUT;
7192#endif /* USE_X_TOOLKIT */
7193
7194 return Qnil;
7195}
7196
7197
7198\f
7199/* Scroll bar support. */
7200
7201/* Given an X window ID, find the struct scroll_bar which manages it.
7202 This can be called in GC, so we have to make sure to strip off mark
7203 bits. */
7204static struct scroll_bar *
7205x_window_to_scroll_bar (window_id)
7206 Window window_id;
7207{
7208 Lisp_Object tail;
7209
7210 for (tail = Vframe_list;
7211 XGCTYPE (tail) == Lisp_Cons;
8e713be6 7212 tail = XCDR (tail))
06a2c219
GM
7213 {
7214 Lisp_Object frame, bar, condemned;
7215
8e713be6 7216 frame = XCAR (tail);
06a2c219
GM
7217 /* All elements of Vframe_list should be frames. */
7218 if (! GC_FRAMEP (frame))
7219 abort ();
7220
7221 /* Scan this frame's scroll bar list for a scroll bar with the
7222 right window ID. */
7223 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7224 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7225 /* This trick allows us to search both the ordinary and
7226 condemned scroll bar lists with one loop. */
7227 ! GC_NILP (bar) || (bar = condemned,
7228 condemned = Qnil,
7229 ! GC_NILP (bar));
7230 bar = XSCROLL_BAR (bar)->next)
7231 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7232 return XSCROLL_BAR (bar);
7233 }
7234
7235 return 0;
7236}
7237
7238
7239\f
7240/************************************************************************
7241 Toolkit scroll bars
7242 ************************************************************************/
7243
7244#if USE_TOOLKIT_SCROLL_BARS
7245
7246static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7247static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7248static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7249 struct scroll_bar *));
7250static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7251 int, int, int));
7252
7253
7254/* Id of action hook installed for scroll bars. */
7255
7256static XtActionHookId action_hook_id;
7257
7258/* Lisp window being scrolled. Set when starting to interact with
7259 a toolkit scroll bar, reset to nil when ending the interaction. */
7260
7261static Lisp_Object window_being_scrolled;
7262
7263/* Last scroll bar part sent in xm_scroll_callback. */
7264
7265static int last_scroll_bar_part;
7266
ec18280f
SM
7267/* Whether this is an Xaw with arrow-scrollbars. This should imply
7268 that movements of 1/20 of the screen size are mapped to up/down. */
7269
7270static Boolean xaw3d_arrow_scroll;
7271
7272/* Whether the drag scrolling maintains the mouse at the top of the
7273 thumb. If not, resizing the thumb needs to be done more carefully
7274 to avoid jerkyness. */
7275
7276static Boolean xaw3d_pick_top;
7277
06a2c219
GM
7278
7279/* Action hook installed via XtAppAddActionHook when toolkit scroll
ec18280f 7280 bars are used.. The hook is responsible for detecting when
06a2c219
GM
7281 the user ends an interaction with the scroll bar, and generates
7282 a `end-scroll' scroll_bar_click' event if so. */
7283
7284static void
7285xt_action_hook (widget, client_data, action_name, event, params,
7286 num_params)
7287 Widget widget;
7288 XtPointer client_data;
7289 String action_name;
7290 XEvent *event;
7291 String *params;
7292 Cardinal *num_params;
7293{
7294 int scroll_bar_p;
7295 char *end_action;
7296
7297#ifdef USE_MOTIF
7298 scroll_bar_p = XmIsScrollBar (widget);
7299 end_action = "Release";
ec18280f 7300#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
7301 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7302 end_action = "EndScroll";
ec18280f 7303#endif /* USE_MOTIF */
06a2c219
GM
7304
7305 /* Although LessTif uses XtTimeouts like Xaw3d, the timer hack to
7306 let Xt timeouts be processed doesn't work. */
7307 if (scroll_bar_p
7308 && strcmp (action_name, end_action) == 0
7309 && WINDOWP (window_being_scrolled))
7310 {
7311 struct window *w;
7312
7313 x_send_scroll_bar_event (window_being_scrolled,
7314 scroll_bar_end_scroll, 0, 0);
7315 w = XWINDOW (window_being_scrolled);
7316 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7317 window_being_scrolled = Qnil;
7318 last_scroll_bar_part = -1;
7319 }
7320}
7321
7322
7323/* Send a client message with message type Xatom_Scrollbar for a
7324 scroll action to the frame of WINDOW. PART is a value identifying
7325 the part of the scroll bar that was clicked on. PORTION is the
7326 amount to scroll of a whole of WHOLE. */
7327
7328static void
7329x_send_scroll_bar_event (window, part, portion, whole)
7330 Lisp_Object window;
7331 int part, portion, whole;
7332{
7333 XEvent event;
7334 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
7335 struct frame *f = XFRAME (XWINDOW (window)->frame);
7336
7337 /* Construct a ClientMessage event to send to the frame. */
7338 ev->type = ClientMessage;
7339 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7340 ev->display = FRAME_X_DISPLAY (f);
7341 ev->window = FRAME_X_WINDOW (f);
7342 ev->format = 32;
7343 ev->data.l[0] = (long) window;
7344 ev->data.l[1] = (long) part;
7345 ev->data.l[2] = (long) 0;
7346 ev->data.l[3] = (long) portion;
7347 ev->data.l[4] = (long) whole;
7348
7349 /* Setting the event mask to zero means that the message will
7350 be sent to the client that created the window, and if that
7351 window no longer exists, no event will be sent. */
7352 BLOCK_INPUT;
7353 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7354 UNBLOCK_INPUT;
7355}
7356
7357
7358/* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7359 in *IEVENT. */
7360
7361static void
7362x_scroll_bar_to_input_event (event, ievent)
7363 XEvent *event;
7364 struct input_event *ievent;
7365{
7366 XClientMessageEvent *ev = (XClientMessageEvent *) event;
7367 Lisp_Object window = (Lisp_Object) ev->data.l[0];
7368 struct frame *f = XFRAME (XWINDOW (window)->frame);
7369
7370 ievent->kind = scroll_bar_click;
7371 ievent->frame_or_window = window;
7372 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7373 ievent->part = ev->data.l[1];
7374 ievent->code = ev->data.l[2];
7375 ievent->x = make_number ((int) ev->data.l[3]);
7376 ievent->y = make_number ((int) ev->data.l[4]);
7377 ievent->modifiers = 0;
7378}
7379
7380
7381#ifdef USE_MOTIF
7382
7383/* Minimum and maximum values used for Motif scroll bars. */
7384
7385#define XM_SB_MIN 1
7386#define XM_SB_MAX 10000000
7387#define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7388
7389
7390/* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7391 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7392 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7393
7394static void
7395xm_scroll_callback (widget, client_data, call_data)
7396 Widget widget;
7397 XtPointer client_data, call_data;
7398{
7399 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7400 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7401 double percent;
7402 int part = -1, whole = 0, portion = 0;
7403
7404 switch (cs->reason)
7405 {
7406 case XmCR_DECREMENT:
7407 bar->dragging = Qnil;
7408 part = scroll_bar_up_arrow;
7409 break;
7410
7411 case XmCR_INCREMENT:
7412 bar->dragging = Qnil;
7413 part = scroll_bar_down_arrow;
7414 break;
7415
7416 case XmCR_PAGE_DECREMENT:
7417 bar->dragging = Qnil;
7418 part = scroll_bar_above_handle;
7419 break;
7420
7421 case XmCR_PAGE_INCREMENT:
7422 bar->dragging = Qnil;
7423 part = scroll_bar_below_handle;
7424 break;
7425
7426 case XmCR_TO_TOP:
7427 bar->dragging = Qnil;
7428 part = scroll_bar_to_top;
7429 break;
7430
7431 case XmCR_TO_BOTTOM:
7432 bar->dragging = Qnil;
7433 part = scroll_bar_to_bottom;
7434 break;
7435
7436 case XmCR_DRAG:
7437 {
7438 int slider_size;
7439 int dragging_down_p = (INTEGERP (bar->dragging)
7440 && XINT (bar->dragging) <= cs->value);
7441
7442 /* Get the slider size. */
7443 BLOCK_INPUT;
7444 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7445 UNBLOCK_INPUT;
7446
7447 /* At the max position of the scroll bar, do a line-wise
7448 movement. Without doing anything, the LessTif scroll bar
7449 calls us with the same cs->value again and again. If we
7450 want to make sure that we can reach the end of the buffer,
7451 we have to do something.
7452
7453 Implementation note: setting bar->dragging always to
7454 cs->value gives a smoother movement at the max position.
7455 Setting it to nil when doing line-wise movement gives
7456 a better slider behavior. */
7457
7458 if (cs->value + slider_size == XM_SB_MAX
7459 || (dragging_down_p
7460 && last_scroll_bar_part == scroll_bar_down_arrow))
7461 {
7462 part = scroll_bar_down_arrow;
7463 bar->dragging = Qnil;
7464 }
7465 else
7466 {
7467 whole = XM_SB_RANGE;
7468 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7469 part = scroll_bar_handle;
7470 bar->dragging = make_number (cs->value);
7471 }
7472 }
7473 break;
7474
7475 case XmCR_VALUE_CHANGED:
7476 break;
7477 };
7478
7479 if (part >= 0)
7480 {
7481 window_being_scrolled = bar->window;
7482 last_scroll_bar_part = part;
7483 x_send_scroll_bar_event (bar->window, part, portion, whole);
7484 }
7485}
7486
7487
ec18280f 7488#else /* !USE_MOTIF, i.e. Xaw. */
06a2c219
GM
7489
7490
ec18280f 7491/* Xaw scroll bar callback. Invoked when the thumb is dragged.
06a2c219
GM
7492 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7493 scroll bar struct. CALL_DATA is a pointer to a float saying where
7494 the thumb is. */
7495
7496static void
ec18280f 7497xaw_jump_callback (widget, client_data, call_data)
06a2c219
GM
7498 Widget widget;
7499 XtPointer client_data, call_data;
7500{
7501 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7502 float top = *(float *) call_data;
7503 float shown;
ec18280f
SM
7504 int whole, portion, height;
7505 int part;
06a2c219
GM
7506
7507 /* Get the size of the thumb, a value between 0 and 1. */
7508 BLOCK_INPUT;
ec18280f 7509 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
06a2c219
GM
7510 UNBLOCK_INPUT;
7511
7512 whole = 10000000;
7513 portion = shown < 1 ? top * whole : 0;
06a2c219 7514
ec18280f
SM
7515 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
7516 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
7517 the bottom, so we force the scrolling whenever we see that we're
7518 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
7519 we try to ensure that we always stay two pixels away from the
7520 bottom). */
06a2c219
GM
7521 part = scroll_bar_down_arrow;
7522 else
7523 part = scroll_bar_handle;
7524
7525 window_being_scrolled = bar->window;
7526 bar->dragging = make_number (portion);
7527 last_scroll_bar_part = part;
7528 x_send_scroll_bar_event (bar->window, part, portion, whole);
7529}
7530
7531
ec18280f
SM
7532/* Xaw scroll bar callback. Invoked for incremental scrolling.,
7533 i.e. line or page up or down. WIDGET is the Xaw scroll bar
06a2c219
GM
7534 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
7535 the scroll bar. CALL_DATA is an integer specifying the action that
7536 has taken place. It's magnitude is in the range 0..height of the
7537 scroll bar. Negative values mean scroll towards buffer start.
7538 Values < height of scroll bar mean line-wise movement. */
7539
7540static void
ec18280f 7541xaw_scroll_callback (widget, client_data, call_data)
06a2c219
GM
7542 Widget widget;
7543 XtPointer client_data, call_data;
7544{
7545 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7546 int position = (int) call_data;
7547 Dimension height;
7548 int part;
7549
7550 /* Get the height of the scroll bar. */
7551 BLOCK_INPUT;
7552 XtVaGetValues (widget, XtNheight, &height, NULL);
7553 UNBLOCK_INPUT;
7554
ec18280f
SM
7555 if (abs (position) >= height)
7556 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
7557
7558 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
7559 it maps line-movement to call_data = max(5, height/20). */
7560 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
7561 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
06a2c219 7562 else
ec18280f 7563 part = scroll_bar_move_ratio;
06a2c219
GM
7564
7565 window_being_scrolled = bar->window;
7566 bar->dragging = Qnil;
7567 last_scroll_bar_part = part;
ec18280f 7568 x_send_scroll_bar_event (bar->window, part, position, height);
06a2c219
GM
7569}
7570
7571
7572#endif /* not USE_MOTIF */
7573
7574
7575/* Create the widget for scroll bar BAR on frame F. Record the widget
7576 and X window of the scroll bar in BAR. */
7577
7578static void
7579x_create_toolkit_scroll_bar (f, bar)
7580 struct frame *f;
7581 struct scroll_bar *bar;
7582{
7583 Window xwindow;
7584 Widget widget;
7585 Arg av[20];
7586 int ac = 0;
7587 char *scroll_bar_name = "verticalScrollBar";
7588 unsigned long pixel;
7589
7590 BLOCK_INPUT;
7591
7592#ifdef USE_MOTIF
7593 /* LessTif 0.85, problems:
7594
7595 1. When the mouse if over the scroll bar, the scroll bar will
7596 get keyboard events. I didn't find a way to turn this off.
7597
7598 2. Do we have to explicitly set the cursor to get an arrow
7599 cursor (see below)? */
7600
7601 /* Set resources. Create the widget. */
7602 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7603 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
7604 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
7605 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
7606 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
7607 XtSetArg (av[ac], XmNincrement, 1); ++ac;
7608 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
7609
7610 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7611 if (pixel != -1)
7612 {
7613 XtSetArg (av[ac], XmNforeground, pixel);
7614 ++ac;
7615 }
7616
7617 pixel = f->output_data.x->scroll_bar_background_pixel;
7618 if (pixel != -1)
7619 {
7620 XtSetArg (av[ac], XmNbackground, pixel);
7621 ++ac;
7622 }
7623
7624 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
7625 scroll_bar_name, av, ac);
7626
7627 /* Add one callback for everything that can happen. */
7628 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
7629 (XtPointer) bar);
7630 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
7631 (XtPointer) bar);
7632 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
7633 (XtPointer) bar);
7634 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
7635 (XtPointer) bar);
7636 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
7637 (XtPointer) bar);
7638 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
7639 (XtPointer) bar);
7640 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
7641 (XtPointer) bar);
7642
7643 /* Realize the widget. Only after that is the X window created. */
7644 XtRealizeWidget (widget);
7645
7646 /* Set the cursor to an arrow. I didn't find a resource to do that.
7647 And I'm wondering why it hasn't an arrow cursor by default. */
7648 XDefineCursor (XtDisplay (widget), XtWindow (widget),
7649 f->output_data.x->nontext_cursor);
7650
ec18280f 7651#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
7652
7653 /* Set resources. Create the widget. The background of the
7654 Xaw3d scroll bar widget is a little bit light for my taste.
7655 We don't alter it here to let users change it according
7656 to their taste with `emacs*verticalScrollBar.background: xxx'. */
7657 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7658 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
ec18280f
SM
7659 /* For smoother scrolling with Xaw3d -sm */
7660 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
7661 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
06a2c219
GM
7662
7663 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7664 if (pixel != -1)
7665 {
7666 XtSetArg (av[ac], XtNforeground, pixel);
7667 ++ac;
7668 }
7669
7670 pixel = f->output_data.x->scroll_bar_background_pixel;
7671 if (pixel != -1)
7672 {
7673 XtSetArg (av[ac], XtNbackground, pixel);
7674 ++ac;
7675 }
7676
7677 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
7678 f->output_data.x->edit_widget, av, ac);
ec18280f
SM
7679
7680 {
7681 char *initial = "";
7682 char *val = initial;
7683 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
7684 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
7685 if (val == initial)
7686 { /* ARROW_SCROLL */
7687 xaw3d_arrow_scroll = True;
7688 /* Isn't that just a personal preference ? -sm */
7689 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
7690 }
7691 }
06a2c219
GM
7692
7693 /* Define callbacks. */
ec18280f
SM
7694 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
7695 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
06a2c219
GM
7696 (XtPointer) bar);
7697
7698 /* Realize the widget. Only after that is the X window created. */
7699 XtRealizeWidget (widget);
7700
ec18280f 7701#endif /* !USE_MOTIF */
06a2c219
GM
7702
7703 /* Install an action hook that let's us detect when the user
7704 finishes interacting with a scroll bar. */
7705 if (action_hook_id == 0)
7706 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
7707
7708 /* Remember X window and widget in the scroll bar vector. */
7709 SET_SCROLL_BAR_X_WIDGET (bar, widget);
7710 xwindow = XtWindow (widget);
7711 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
7712
7713 UNBLOCK_INPUT;
7714}
7715
7716
7717/* Set the thumb size and position of scroll bar BAR. We are currently
7718 displaying PORTION out of a whole WHOLE, and our position POSITION. */
7719
7720static void
7721x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
7722 struct scroll_bar *bar;
7723 int portion, position, whole;
f451eb13 7724{
06a2c219 7725 float top, shown;
06a2c219 7726 Widget widget = SCROLL_BAR_X_WIDGET (bar);
f451eb13 7727
06a2c219
GM
7728 if (whole == 0)
7729 top = 0, shown = 1;
7730 else
f451eb13 7731 {
06a2c219
GM
7732 top = (float) position / whole;
7733 shown = (float) portion / whole;
7734 }
f451eb13 7735
06a2c219 7736 BLOCK_INPUT;
f451eb13 7737
06a2c219
GM
7738#ifdef USE_MOTIF
7739 {
7740 int size, value;
7741 Boolean arrow1_selected, arrow2_selected;
7742 unsigned char flags;
7743 XmScrollBarWidget sb;
7744
ec18280f 7745 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
06a2c219
GM
7746 is the scroll bar's maximum and MIN is the scroll bar's minimum
7747 value. */
7748 size = shown * XM_SB_RANGE;
7749 size = min (size, XM_SB_RANGE);
7750 size = max (size, 1);
7751
7752 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
7753 value = top * XM_SB_RANGE;
7754 value = min (value, XM_SB_MAX - size);
7755 value = max (value, XM_SB_MIN);
7756
7757 /* LessTif: Calling XmScrollBarSetValues after an increment or
7758 decrement turns off auto-repeat LessTif-internally. This can
7759 be seen in ScrollBar.c which resets Arrow1Selected and
7760 Arrow2Selected. It also sets internal flags so that LessTif
7761 believes the mouse is in the slider. We either have to change
7762 our code, or work around that by accessing private data. */
7763
7764 sb = (XmScrollBarWidget) widget;
7765 arrow1_selected = sb->scrollBar.arrow1_selected;
7766 arrow2_selected = sb->scrollBar.arrow2_selected;
7767 flags = sb->scrollBar.flags;
7768
7769 if (NILP (bar->dragging))
7770 XmScrollBarSetValues (widget, value, size, 0, 0, False);
7771 else if (last_scroll_bar_part == scroll_bar_down_arrow)
7772 /* This has the negative side effect that the slider value is
ec18280f 7773 not what it would be if we scrolled here using line-wise or
06a2c219
GM
7774 page-wise movement. */
7775 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
7776 else
7777 {
7778 /* If currently dragging, only update the slider size.
7779 This reduces flicker effects. */
7780 int old_value, old_size, increment, page_increment;
7781
7782 XmScrollBarGetValues (widget, &old_value, &old_size,
7783 &increment, &page_increment);
7784 XmScrollBarSetValues (widget, old_value,
7785 min (size, XM_SB_RANGE - old_value),
7786 0, 0, False);
7787 }
7788
7789 sb->scrollBar.arrow1_selected = arrow1_selected;
7790 sb->scrollBar.arrow2_selected = arrow2_selected;
7791 sb->scrollBar.flags = flags;
7792 }
ec18280f 7793#else /* !USE_MOTIF i.e. use Xaw */
06a2c219 7794 {
ec18280f
SM
7795 float old_top, old_shown;
7796 Dimension height;
7797 XtVaGetValues (widget,
7798 XtNtopOfThumb, &old_top,
7799 XtNshown, &old_shown,
7800 XtNheight, &height,
7801 NULL);
7802
7803 /* Massage the top+shown values. */
7804 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
7805 top = max (0, min (1, top));
7806 else
7807 top = old_top;
7808 /* Keep two pixels available for moving the thumb down. */
7809 shown = max (0, min (1 - top - (2.0 / height), shown));
06a2c219
GM
7810
7811 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
7812 check that your system's configuration file contains a define
7813 for `NARROWPROTO'. See s/freebsd.h for an example. */
ec18280f 7814 if (top != old_top || shown != old_shown)
eb393530 7815 {
ec18280f 7816 if (NILP (bar->dragging))
eb393530 7817 XawScrollbarSetThumb (widget, top, shown);
06a2c219
GM
7818 else
7819 {
ec18280f
SM
7820#ifdef HAVE_XAW3D
7821 ScrollbarWidget sb = (ScrollbarWidget) widget;
7822 int scroll_mode;
7823
7824 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
7825 if (xaw3d_arrow_scroll)
7826 {
7827 /* Xaw3d stupidly ignores resize requests while dragging
7828 so we have to make it believe it's not in dragging mode. */
7829 scroll_mode = sb->scrollbar.scroll_mode;
7830 if (scroll_mode == 2)
7831 sb->scrollbar.scroll_mode = 0;
7832 }
7833#endif
7834 /* Try to make the scrolling a tad smoother. */
7835 if (!xaw3d_pick_top)
7836 shown = min (shown, old_shown);
7837
7838 XawScrollbarSetThumb (widget, top, shown);
7839
7840#ifdef HAVE_XAW3D
7841 if (xaw3d_arrow_scroll && scroll_mode == 2)
7842 sb->scrollbar.scroll_mode = scroll_mode;
7843#endif
06a2c219 7844 }
06a2c219
GM
7845 }
7846 }
ec18280f 7847#endif /* !USE_MOTIF */
06a2c219
GM
7848
7849 UNBLOCK_INPUT;
f451eb13
JB
7850}
7851
06a2c219
GM
7852#endif /* USE_TOOLKIT_SCROLL_BARS */
7853
7854
7855\f
7856/************************************************************************
7857 Scroll bars, general
7858 ************************************************************************/
7859
7860/* Create a scroll bar and return the scroll bar vector for it. W is
7861 the Emacs window on which to create the scroll bar. TOP, LEFT,
7862 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
7863 scroll bar. */
7864
ab648270 7865static struct scroll_bar *
06a2c219
GM
7866x_scroll_bar_create (w, top, left, width, height)
7867 struct window *w;
f451eb13
JB
7868 int top, left, width, height;
7869{
06a2c219 7870 struct frame *f = XFRAME (w->frame);
334208b7
RS
7871 struct scroll_bar *bar
7872 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
f451eb13
JB
7873
7874 BLOCK_INPUT;
7875
06a2c219
GM
7876#if USE_TOOLKIT_SCROLL_BARS
7877 x_create_toolkit_scroll_bar (f, bar);
7878#else /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
7879 {
7880 XSetWindowAttributes a;
7881 unsigned long mask;
5c187dee 7882 Window window;
06a2c219
GM
7883
7884 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
7885 if (a.background_pixel == -1)
7886 a.background_pixel = f->output_data.x->background_pixel;
7887
12ba150f 7888 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9a572e2a 7889 | ButtonMotionMask | PointerMotionHintMask
12ba150f 7890 | ExposureMask);
7a13e894 7891 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
f451eb13 7892
dbc4e1c1 7893 mask = (CWBackPixel | CWEventMask | CWCursor);
f451eb13 7894
06a2c219
GM
7895 /* Clear the area of W that will serve as a scroll bar. This is
7896 for the case that a window has been split horizontally. In
7897 this case, no clear_frame is generated to reduce flickering. */
7898 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7899 left, top, width,
7900 window_box_height (w), False);
7901
7902 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7903 /* Position and size of scroll bar. */
7904 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7905 top,
7906 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
7907 height,
7908 /* Border width, depth, class, and visual. */
7909 0,
7910 CopyFromParent,
7911 CopyFromParent,
7912 CopyFromParent,
7913 /* Attributes. */
7914 mask, &a);
7915 SET_SCROLL_BAR_X_WINDOW (bar, window);
f451eb13 7916 }
06a2c219 7917#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 7918
06a2c219 7919 XSETWINDOW (bar->window, w);
e0c1aef2
KH
7920 XSETINT (bar->top, top);
7921 XSETINT (bar->left, left);
7922 XSETINT (bar->width, width);
7923 XSETINT (bar->height, height);
7924 XSETINT (bar->start, 0);
7925 XSETINT (bar->end, 0);
12ba150f 7926 bar->dragging = Qnil;
f451eb13
JB
7927
7928 /* Add bar to its frame's list of scroll bars. */
334208b7 7929 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 7930 bar->prev = Qnil;
334208b7 7931 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
06a2c219 7932 if (!NILP (bar->next))
e0c1aef2 7933 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13 7934
06a2c219
GM
7935 /* Map the window/widget. */
7936#if USE_TOOLKIT_SCROLL_BARS
7937 XtMapWidget (SCROLL_BAR_X_WIDGET (bar));
7938 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
7939 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7940 top,
7941 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
7942 height, 0);
7943#else /* not USE_TOOLKIT_SCROLL_BARS */
7f9c7f94 7944 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
06a2c219 7945#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
7946
7947 UNBLOCK_INPUT;
12ba150f 7948 return bar;
f451eb13
JB
7949}
7950
06a2c219 7951
12ba150f 7952/* Draw BAR's handle in the proper position.
06a2c219 7953
12ba150f
JB
7954 If the handle is already drawn from START to END, don't bother
7955 redrawing it, unless REBUILD is non-zero; in that case, always
7956 redraw it. (REBUILD is handy for drawing the handle after expose
58769bee 7957 events.)
12ba150f
JB
7958
7959 Normally, we want to constrain the start and end of the handle to
06a2c219
GM
7960 fit inside its rectangle, but if the user is dragging the scroll
7961 bar handle, we want to let them drag it down all the way, so that
7962 the bar's top is as far down as it goes; otherwise, there's no way
7963 to move to the very end of the buffer. */
7964
5c187dee
GM
7965#ifndef USE_TOOLKIT_SCROLL_BARS
7966
f451eb13 7967static void
ab648270
JB
7968x_scroll_bar_set_handle (bar, start, end, rebuild)
7969 struct scroll_bar *bar;
f451eb13 7970 int start, end;
12ba150f 7971 int rebuild;
f451eb13 7972{
12ba150f 7973 int dragging = ! NILP (bar->dragging);
ab648270 7974 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 7975 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 7976 GC gc = f->output_data.x->normal_gc;
12ba150f
JB
7977
7978 /* If the display is already accurate, do nothing. */
7979 if (! rebuild
7980 && start == XINT (bar->start)
7981 && end == XINT (bar->end))
7982 return;
7983
f451eb13
JB
7984 BLOCK_INPUT;
7985
7986 {
d9cdbb3d
RS
7987 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
7988 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
7989 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
f451eb13
JB
7990
7991 /* Make sure the values are reasonable, and try to preserve
7992 the distance between start and end. */
12ba150f
JB
7993 {
7994 int length = end - start;
7995
7996 if (start < 0)
7997 start = 0;
7998 else if (start > top_range)
7999 start = top_range;
8000 end = start + length;
8001
8002 if (end < start)
8003 end = start;
8004 else if (end > top_range && ! dragging)
8005 end = top_range;
8006 }
f451eb13 8007
ab648270 8008 /* Store the adjusted setting in the scroll bar. */
e0c1aef2
KH
8009 XSETINT (bar->start, start);
8010 XSETINT (bar->end, end);
f451eb13 8011
12ba150f
JB
8012 /* Clip the end position, just for display. */
8013 if (end > top_range)
8014 end = top_range;
f451eb13 8015
ab648270 8016 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
12ba150f
JB
8017 below top positions, to make sure the handle is always at least
8018 that many pixels tall. */
ab648270 8019 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
f451eb13 8020
12ba150f
JB
8021 /* Draw the empty space above the handle. Note that we can't clear
8022 zero-height areas; that means "clear to end of window." */
8023 if (0 < start)
334208b7 8024 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 8025
12ba150f 8026 /* x, y, width, height, and exposures. */
ab648270
JB
8027 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8028 VERTICAL_SCROLL_BAR_TOP_BORDER,
12ba150f
JB
8029 inside_width, start,
8030 False);
f451eb13 8031
06a2c219
GM
8032 /* Change to proper foreground color if one is specified. */
8033 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8034 XSetForeground (FRAME_X_DISPLAY (f), gc,
8035 f->output_data.x->scroll_bar_foreground_pixel);
8036
12ba150f 8037 /* Draw the handle itself. */
334208b7 8038 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13 8039
12ba150f 8040 /* x, y, width, height */
ab648270
JB
8041 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8042 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
12ba150f 8043 inside_width, end - start);
f451eb13 8044
06a2c219
GM
8045 /* Restore the foreground color of the GC if we changed it above. */
8046 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8047 XSetForeground (FRAME_X_DISPLAY (f), gc,
8048 f->output_data.x->foreground_pixel);
f451eb13 8049
12ba150f
JB
8050 /* Draw the empty space below the handle. Note that we can't
8051 clear zero-height areas; that means "clear to end of window." */
8052 if (end < inside_height)
334208b7 8053 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 8054
12ba150f 8055 /* x, y, width, height, and exposures. */
ab648270
JB
8056 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8057 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
12ba150f
JB
8058 inside_width, inside_height - end,
8059 False);
f451eb13 8060
f451eb13
JB
8061 }
8062
f451eb13
JB
8063 UNBLOCK_INPUT;
8064}
8065
5c187dee 8066#endif /* !USE_TOOLKIT_SCROLL_BARS */
f451eb13 8067
06a2c219
GM
8068/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8069 nil. */
58769bee 8070
12ba150f 8071static void
ab648270
JB
8072x_scroll_bar_remove (bar)
8073 struct scroll_bar *bar;
12ba150f 8074{
12ba150f
JB
8075 BLOCK_INPUT;
8076
06a2c219
GM
8077#if USE_TOOLKIT_SCROLL_BARS
8078 XtDestroyWidget (SCROLL_BAR_X_WIDGET (bar));
8079#else /* not USE_TOOLKIT_SCROLL_BARS */
5c187dee
GM
8080 {
8081 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8082 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8083 }
06a2c219
GM
8084#endif /* not USE_TOOLKIT_SCROLL_BARS */
8085
ab648270
JB
8086 /* Disassociate this scroll bar from its window. */
8087 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
12ba150f
JB
8088
8089 UNBLOCK_INPUT;
8090}
8091
06a2c219 8092
12ba150f
JB
8093/* Set the handle of the vertical scroll bar for WINDOW to indicate
8094 that we are displaying PORTION characters out of a total of WHOLE
ab648270 8095 characters, starting at POSITION. If WINDOW has no scroll bar,
12ba150f 8096 create one. */
06a2c219 8097
12ba150f 8098static void
06a2c219
GM
8099XTset_vertical_scroll_bar (w, portion, whole, position)
8100 struct window *w;
f451eb13
JB
8101 int portion, whole, position;
8102{
06a2c219 8103 struct frame *f = XFRAME (w->frame);
ab648270 8104 struct scroll_bar *bar;
3c6ede7b 8105 int top, height, left, sb_left, width, sb_width;
06a2c219 8106 int window_x, window_y, window_width, window_height;
06a2c219 8107
3c6ede7b 8108 /* Get window dimensions. */
06a2c219 8109 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
3c6ede7b
GM
8110 top = window_y;
8111 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8112 height = window_height;
06a2c219 8113
3c6ede7b 8114 /* Compute the left edge of the scroll bar area. */
06a2c219 8115 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
3c6ede7b
GM
8116 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8117 else
8118 left = XFASTINT (w->left);
8119 left *= CANON_X_UNIT (f);
8120 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8121
8122 /* Compute the width of the scroll bar which might be less than
8123 the width of the area reserved for the scroll bar. */
8124 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8125 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
06a2c219 8126 else
3c6ede7b 8127 sb_width = width;
12ba150f 8128
3c6ede7b
GM
8129 /* Compute the left edge of the scroll bar. */
8130#ifdef USE_TOOLKIT_SCROLL_BARS
8131 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8132 sb_left = left + width - sb_width - (width - sb_width) / 2;
8133 else
8134 sb_left = left + (width - sb_width) / 2;
8135#else
8136 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8137 sb_left = left + width - sb_width;
8138 else
8139 sb_left = left;
8140#endif
8141
ab648270 8142 /* Does the scroll bar exist yet? */
06a2c219 8143 if (NILP (w->vertical_scroll_bar))
3c6ede7b 8144 {
80c32bcc 8145 BLOCK_INPUT;
3c6ede7b
GM
8146 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8147 left, top, width, height, False);
80c32bcc 8148 UNBLOCK_INPUT;
3c6ede7b
GM
8149 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8150 }
f451eb13 8151 else
12ba150f
JB
8152 {
8153 /* It may just need to be moved and resized. */
06a2c219
GM
8154 unsigned int mask = 0;
8155
8156 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8157
8158 BLOCK_INPUT;
8159
3c6ede7b 8160 if (sb_left != XINT (bar->left))
06a2c219 8161 mask |= CWX;
3c6ede7b 8162 if (top != XINT (bar->top))
06a2c219 8163 mask |= CWY;
3c6ede7b 8164 if (sb_width != XINT (bar->width))
06a2c219 8165 mask |= CWWidth;
3c6ede7b 8166 if (height != XINT (bar->height))
06a2c219
GM
8167 mask |= CWHeight;
8168
8169#ifdef USE_TOOLKIT_SCROLL_BARS
fe6f39d9
GM
8170
8171 /* Since toolkit scroll bars are smaller than the space reserved
8172 for them on the frame, we have to clear "under" them. */
8173 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3c6ede7b 8174 left, top, width, height, False);
06a2c219
GM
8175
8176 /* Move/size the scroll bar widget. */
8177 if (mask)
8178 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
3c6ede7b
GM
8179 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8180 top,
8181 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8182 height, 0);
06a2c219
GM
8183
8184#else /* not USE_TOOLKIT_SCROLL_BARS */
8185
e1f6572f
RS
8186 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8187 {
8188 /* Clear areas not covered by the scroll bar. This makes sure a
8189 previous mode line display is cleared after C-x 2 C-x 1, for
8190 example. Non-toolkit scroll bars are as wide as the area
8191 reserved for scroll bars - trim at both sides. */
8192 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8193 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8194 height, False);
8195 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8196 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8197 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8198 height, False);
8199 }
06a2c219
GM
8200
8201 /* Move/size the scroll bar window. */
8202 if (mask)
8203 {
8204 XWindowChanges wc;
8205
3c6ede7b
GM
8206 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8207 wc.y = top;
8208 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8209 wc.height = height;
06a2c219
GM
8210 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8211 mask, &wc);
8212 }
8213
8214#endif /* not USE_TOOLKIT_SCROLL_BARS */
8215
8216 /* Remember new settings. */
3c6ede7b
GM
8217 XSETINT (bar->left, sb_left);
8218 XSETINT (bar->top, top);
8219 XSETINT (bar->width, sb_width);
8220 XSETINT (bar->height, height);
06a2c219
GM
8221
8222 UNBLOCK_INPUT;
12ba150f 8223 }
f451eb13 8224
06a2c219
GM
8225#if USE_TOOLKIT_SCROLL_BARS
8226 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8227#else /* not USE_TOOLKIT_SCROLL_BARS */
ab648270 8228 /* Set the scroll bar's current state, unless we're currently being
f451eb13 8229 dragged. */
12ba150f 8230 if (NILP (bar->dragging))
f451eb13 8231 {
92857db0 8232 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
f451eb13 8233
12ba150f 8234 if (whole == 0)
ab648270 8235 x_scroll_bar_set_handle (bar, 0, top_range, 0);
12ba150f
JB
8236 else
8237 {
43f868f5
JB
8238 int start = ((double) position * top_range) / whole;
8239 int end = ((double) (position + portion) * top_range) / whole;
ab648270 8240 x_scroll_bar_set_handle (bar, start, end, 0);
12ba150f 8241 }
f451eb13 8242 }
06a2c219 8243#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 8244
06a2c219 8245 XSETVECTOR (w->vertical_scroll_bar, bar);
f451eb13
JB
8246}
8247
12ba150f 8248
f451eb13 8249/* The following three hooks are used when we're doing a thorough
ab648270 8250 redisplay of the frame. We don't explicitly know which scroll bars
f451eb13 8251 are going to be deleted, because keeping track of when windows go
12ba150f
JB
8252 away is a real pain - "Can you say set-window-configuration, boys
8253 and girls?" Instead, we just assert at the beginning of redisplay
ab648270 8254 that *all* scroll bars are to be removed, and then save a scroll bar
12ba150f 8255 from the fiery pit when we actually redisplay its window. */
f451eb13 8256
ab648270
JB
8257/* Arrange for all scroll bars on FRAME to be removed at the next call
8258 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
06a2c219
GM
8259 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8260
58769bee 8261static void
ab648270 8262XTcondemn_scroll_bars (frame)
f451eb13
JB
8263 FRAME_PTR frame;
8264{
f9e24cb9
RS
8265 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8266 while (! NILP (FRAME_SCROLL_BARS (frame)))
8267 {
8268 Lisp_Object bar;
8269 bar = FRAME_SCROLL_BARS (frame);
8270 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8271 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8272 XSCROLL_BAR (bar)->prev = Qnil;
8273 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8274 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8275 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8276 }
f451eb13
JB
8277}
8278
06a2c219 8279/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
12ba150f 8280 Note that WINDOW isn't necessarily condemned at all. */
f451eb13 8281static void
ab648270 8282XTredeem_scroll_bar (window)
12ba150f 8283 struct window *window;
f451eb13 8284{
ab648270 8285 struct scroll_bar *bar;
12ba150f 8286
ab648270
JB
8287 /* We can't redeem this window's scroll bar if it doesn't have one. */
8288 if (NILP (window->vertical_scroll_bar))
12ba150f
JB
8289 abort ();
8290
ab648270 8291 bar = XSCROLL_BAR (window->vertical_scroll_bar);
12ba150f
JB
8292
8293 /* Unlink it from the condemned list. */
8294 {
8295 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
8296
8297 if (NILP (bar->prev))
8298 {
8299 /* If the prev pointer is nil, it must be the first in one of
8300 the lists. */
ab648270 8301 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
12ba150f
JB
8302 /* It's not condemned. Everything's fine. */
8303 return;
ab648270
JB
8304 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8305 window->vertical_scroll_bar))
8306 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
12ba150f
JB
8307 else
8308 /* If its prev pointer is nil, it must be at the front of
8309 one or the other! */
8310 abort ();
8311 }
8312 else
ab648270 8313 XSCROLL_BAR (bar->prev)->next = bar->next;
12ba150f
JB
8314
8315 if (! NILP (bar->next))
ab648270 8316 XSCROLL_BAR (bar->next)->prev = bar->prev;
12ba150f 8317
ab648270 8318 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 8319 bar->prev = Qnil;
e0c1aef2 8320 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
12ba150f 8321 if (! NILP (bar->next))
e0c1aef2 8322 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
12ba150f 8323 }
f451eb13
JB
8324}
8325
ab648270
JB
8326/* Remove all scroll bars on FRAME that haven't been saved since the
8327 last call to `*condemn_scroll_bars_hook'. */
06a2c219 8328
f451eb13 8329static void
ab648270 8330XTjudge_scroll_bars (f)
12ba150f 8331 FRAME_PTR f;
f451eb13 8332{
12ba150f 8333 Lisp_Object bar, next;
f451eb13 8334
ab648270 8335 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
cf7cb199
JB
8336
8337 /* Clear out the condemned list now so we won't try to process any
ab648270
JB
8338 more events on the hapless scroll bars. */
8339 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
cf7cb199
JB
8340
8341 for (; ! NILP (bar); bar = next)
f451eb13 8342 {
ab648270 8343 struct scroll_bar *b = XSCROLL_BAR (bar);
12ba150f 8344
ab648270 8345 x_scroll_bar_remove (b);
12ba150f
JB
8346
8347 next = b->next;
8348 b->next = b->prev = Qnil;
f451eb13 8349 }
12ba150f 8350
ab648270 8351 /* Now there should be no references to the condemned scroll bars,
12ba150f 8352 and they should get garbage-collected. */
f451eb13
JB
8353}
8354
8355
06a2c219
GM
8356/* Handle an Expose or GraphicsExpose event on a scroll bar. This
8357 is a no-op when using toolkit scroll bars.
ab648270
JB
8358
8359 This may be called from a signal handler, so we have to ignore GC
8360 mark bits. */
06a2c219 8361
f451eb13 8362static void
ab648270
JB
8363x_scroll_bar_expose (bar, event)
8364 struct scroll_bar *bar;
f451eb13
JB
8365 XEvent *event;
8366{
06a2c219
GM
8367#ifndef USE_TOOLKIT_SCROLL_BARS
8368
ab648270 8369 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8370 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8371 GC gc = f->output_data.x->normal_gc;
3cbd2e0b 8372 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
12ba150f 8373
f451eb13
JB
8374 BLOCK_INPUT;
8375
ab648270 8376 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
f451eb13 8377
06a2c219 8378 /* Draw a one-pixel border just inside the edges of the scroll bar. */
334208b7 8379 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13
JB
8380
8381 /* x, y, width, height */
d9cdbb3d 8382 0, 0,
3cbd2e0b 8383 XINT (bar->width) - 1 - width_trim - width_trim,
d9cdbb3d
RS
8384 XINT (bar->height) - 1);
8385
f451eb13 8386 UNBLOCK_INPUT;
06a2c219
GM
8387
8388#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8389}
8390
ab648270
JB
8391/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8392 is set to something other than no_event, it is enqueued.
8393
8394 This may be called from a signal handler, so we have to ignore GC
8395 mark bits. */
06a2c219 8396
5c187dee
GM
8397#ifndef USE_TOOLKIT_SCROLL_BARS
8398
f451eb13 8399static void
ab648270
JB
8400x_scroll_bar_handle_click (bar, event, emacs_event)
8401 struct scroll_bar *bar;
f451eb13
JB
8402 XEvent *event;
8403 struct input_event *emacs_event;
8404{
0299d313 8405 if (! GC_WINDOWP (bar->window))
12ba150f
JB
8406 abort ();
8407
ab648270 8408 emacs_event->kind = scroll_bar_click;
69388238 8409 emacs_event->code = event->xbutton.button - Button1;
0299d313
RS
8410 emacs_event->modifiers
8411 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8412 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8413 event->xbutton.state)
8414 | (event->type == ButtonRelease
8415 ? up_modifier
8416 : down_modifier));
12ba150f 8417 emacs_event->frame_or_window = bar->window;
f451eb13 8418 emacs_event->timestamp = event->xbutton.time;
12ba150f 8419 {
06a2c219 8420#if 0
d9cdbb3d 8421 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
0299d313 8422 int internal_height
d9cdbb3d 8423 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8424#endif
0299d313 8425 int top_range
d9cdbb3d 8426 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ab648270 8427 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
12ba150f
JB
8428
8429 if (y < 0) y = 0;
8430 if (y > top_range) y = top_range;
8431
8432 if (y < XINT (bar->start))
ab648270
JB
8433 emacs_event->part = scroll_bar_above_handle;
8434 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8435 emacs_event->part = scroll_bar_handle;
12ba150f 8436 else
ab648270 8437 emacs_event->part = scroll_bar_below_handle;
929787e1
JB
8438
8439 /* Just because the user has clicked on the handle doesn't mean
5116f055
JB
8440 they want to drag it. Lisp code needs to be able to decide
8441 whether or not we're dragging. */
929787e1 8442#if 0
12ba150f
JB
8443 /* If the user has just clicked on the handle, record where they're
8444 holding it. */
8445 if (event->type == ButtonPress
ab648270 8446 && emacs_event->part == scroll_bar_handle)
e0c1aef2 8447 XSETINT (bar->dragging, y - XINT (bar->start));
929787e1 8448#endif
12ba150f
JB
8449
8450 /* If the user has released the handle, set it to its final position. */
8451 if (event->type == ButtonRelease
8452 && ! NILP (bar->dragging))
8453 {
8454 int new_start = y - XINT (bar->dragging);
8455 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
f451eb13 8456
ab648270 8457 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
12ba150f
JB
8458 bar->dragging = Qnil;
8459 }
f451eb13 8460
5116f055
JB
8461 /* Same deal here as the other #if 0. */
8462#if 0
58769bee 8463 /* Clicks on the handle are always reported as occurring at the top of
12ba150f 8464 the handle. */
ab648270 8465 if (emacs_event->part == scroll_bar_handle)
12ba150f
JB
8466 emacs_event->x = bar->start;
8467 else
e0c1aef2 8468 XSETINT (emacs_event->x, y);
5116f055 8469#else
e0c1aef2 8470 XSETINT (emacs_event->x, y);
5116f055 8471#endif
f451eb13 8472
e0c1aef2 8473 XSETINT (emacs_event->y, top_range);
12ba150f
JB
8474 }
8475}
f451eb13 8476
ab648270
JB
8477/* Handle some mouse motion while someone is dragging the scroll bar.
8478
8479 This may be called from a signal handler, so we have to ignore GC
8480 mark bits. */
06a2c219 8481
f451eb13 8482static void
ab648270
JB
8483x_scroll_bar_note_movement (bar, event)
8484 struct scroll_bar *bar;
f451eb13
JB
8485 XEvent *event;
8486{
39d8bb4d
KH
8487 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8488
f451eb13
JB
8489 last_mouse_movement_time = event->xmotion.time;
8490
39d8bb4d 8491 f->mouse_moved = 1;
e0c1aef2 8492 XSETVECTOR (last_mouse_scroll_bar, bar);
f451eb13
JB
8493
8494 /* If we're dragging the bar, display it. */
ab648270 8495 if (! GC_NILP (bar->dragging))
f451eb13
JB
8496 {
8497 /* Where should the handle be now? */
12ba150f 8498 int new_start = event->xmotion.y - XINT (bar->dragging);
f451eb13 8499
12ba150f 8500 if (new_start != XINT (bar->start))
f451eb13 8501 {
12ba150f 8502 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
58769bee 8503
ab648270 8504 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
f451eb13
JB
8505 }
8506 }
f451eb13
JB
8507}
8508
5c187dee
GM
8509#endif /* !USE_TOOLKIT_SCROLL_BARS */
8510
12ba150f 8511/* Return information to the user about the current position of the mouse
ab648270 8512 on the scroll bar. */
06a2c219 8513
12ba150f 8514static void
334208b7
RS
8515x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8516 FRAME_PTR *fp;
12ba150f 8517 Lisp_Object *bar_window;
ab648270 8518 enum scroll_bar_part *part;
12ba150f
JB
8519 Lisp_Object *x, *y;
8520 unsigned long *time;
8521{
ab648270 8522 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
334208b7
RS
8523 Window w = SCROLL_BAR_X_WINDOW (bar);
8524 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f 8525 int win_x, win_y;
559cb2fb
JB
8526 Window dummy_window;
8527 int dummy_coord;
8528 unsigned int dummy_mask;
12ba150f 8529
cf7cb199
JB
8530 BLOCK_INPUT;
8531
ab648270 8532 /* Get the mouse's position relative to the scroll bar window, and
12ba150f 8533 report that. */
334208b7 8534 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
12ba150f 8535
559cb2fb
JB
8536 /* Root, child, root x and root y. */
8537 &dummy_window, &dummy_window,
8538 &dummy_coord, &dummy_coord,
12ba150f 8539
559cb2fb
JB
8540 /* Position relative to scroll bar. */
8541 &win_x, &win_y,
12ba150f 8542
559cb2fb
JB
8543 /* Mouse buttons and modifier keys. */
8544 &dummy_mask))
7a13e894 8545 ;
559cb2fb
JB
8546 else
8547 {
06a2c219 8548#if 0
559cb2fb 8549 int inside_height
d9cdbb3d 8550 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8551#endif
559cb2fb 8552 int top_range
d9cdbb3d 8553 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
559cb2fb
JB
8554
8555 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
8556
8557 if (! NILP (bar->dragging))
8558 win_y -= XINT (bar->dragging);
8559
8560 if (win_y < 0)
8561 win_y = 0;
8562 if (win_y > top_range)
8563 win_y = top_range;
8564
334208b7 8565 *fp = f;
7a13e894 8566 *bar_window = bar->window;
559cb2fb
JB
8567
8568 if (! NILP (bar->dragging))
8569 *part = scroll_bar_handle;
8570 else if (win_y < XINT (bar->start))
8571 *part = scroll_bar_above_handle;
8572 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8573 *part = scroll_bar_handle;
8574 else
8575 *part = scroll_bar_below_handle;
12ba150f 8576
e0c1aef2
KH
8577 XSETINT (*x, win_y);
8578 XSETINT (*y, top_range);
12ba150f 8579
39d8bb4d 8580 f->mouse_moved = 0;
559cb2fb
JB
8581 last_mouse_scroll_bar = Qnil;
8582 }
12ba150f 8583
559cb2fb 8584 *time = last_mouse_movement_time;
cf7cb199 8585
cf7cb199 8586 UNBLOCK_INPUT;
12ba150f
JB
8587}
8588
f451eb13 8589
dbc4e1c1 8590/* The screen has been cleared so we may have changed foreground or
ab648270
JB
8591 background colors, and the scroll bars may need to be redrawn.
8592 Clear out the scroll bars, and ask for expose events, so we can
dbc4e1c1
JB
8593 redraw them. */
8594
dfcf069d 8595void
ab648270 8596x_scroll_bar_clear (f)
dbc4e1c1
JB
8597 FRAME_PTR f;
8598{
06a2c219 8599#ifndef USE_TOOLKIT_SCROLL_BARS
dbc4e1c1
JB
8600 Lisp_Object bar;
8601
b80c363e
RS
8602 /* We can have scroll bars even if this is 0,
8603 if we just turned off scroll bar mode.
8604 But in that case we should not clear them. */
8605 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8606 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
8607 bar = XSCROLL_BAR (bar)->next)
8608 XClearArea (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
8609 0, 0, 0, 0, True);
06a2c219 8610#endif /* not USE_TOOLKIT_SCROLL_BARS */
dbc4e1c1
JB
8611}
8612
06a2c219 8613/* This processes Expose events from the menu-bar specific X event
19126e11 8614 loop in xmenu.c. This allows to redisplay the frame if necessary
06a2c219 8615 when handling menu-bar or pop-up items. */
3afe33e7 8616
06a2c219 8617int
3afe33e7
RS
8618process_expose_from_menu (event)
8619 XEvent event;
8620{
8621 FRAME_PTR f;
19126e11 8622 struct x_display_info *dpyinfo;
06a2c219 8623 int frame_exposed_p = 0;
3afe33e7 8624
f94397b5
KH
8625 BLOCK_INPUT;
8626
19126e11
KH
8627 dpyinfo = x_display_info_for_display (event.xexpose.display);
8628 f = x_window_to_frame (dpyinfo, event.xexpose.window);
3afe33e7
RS
8629 if (f)
8630 {
8631 if (f->async_visible == 0)
8632 {
8633 f->async_visible = 1;
8634 f->async_iconified = 0;
06c488fd 8635 f->output_data.x->has_been_visible = 1;
3afe33e7
RS
8636 SET_FRAME_GARBAGED (f);
8637 }
8638 else
8639 {
06a2c219
GM
8640 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
8641 event.xexpose.x, event.xexpose.y,
8642 event.xexpose.width, event.xexpose.height);
8643 frame_exposed_p = 1;
3afe33e7
RS
8644 }
8645 }
8646 else
8647 {
8648 struct scroll_bar *bar
8649 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 8650
3afe33e7
RS
8651 if (bar)
8652 x_scroll_bar_expose (bar, &event);
8653 }
f94397b5
KH
8654
8655 UNBLOCK_INPUT;
06a2c219 8656 return frame_exposed_p;
3afe33e7 8657}
09756a85
RS
8658\f
8659/* Define a queue to save up SelectionRequest events for later handling. */
8660
8661struct selection_event_queue
8662 {
8663 XEvent event;
8664 struct selection_event_queue *next;
8665 };
8666
8667static struct selection_event_queue *queue;
8668
8669/* Nonzero means queue up certain events--don't process them yet. */
06a2c219 8670
09756a85
RS
8671static int x_queue_selection_requests;
8672
8673/* Queue up an X event *EVENT, to be processed later. */
dbc4e1c1 8674
09756a85 8675static void
334208b7
RS
8676x_queue_event (f, event)
8677 FRAME_PTR f;
09756a85
RS
8678 XEvent *event;
8679{
8680 struct selection_event_queue *queue_tmp
06a2c219 8681 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
09756a85 8682
58769bee 8683 if (queue_tmp != NULL)
09756a85
RS
8684 {
8685 queue_tmp->event = *event;
8686 queue_tmp->next = queue;
8687 queue = queue_tmp;
8688 }
8689}
8690
8691/* Take all the queued events and put them back
8692 so that they get processed afresh. */
8693
8694static void
db3906fd
RS
8695x_unqueue_events (display)
8696 Display *display;
09756a85 8697{
58769bee 8698 while (queue != NULL)
09756a85
RS
8699 {
8700 struct selection_event_queue *queue_tmp = queue;
db3906fd 8701 XPutBackEvent (display, &queue_tmp->event);
09756a85 8702 queue = queue_tmp->next;
06a2c219 8703 xfree ((char *)queue_tmp);
09756a85
RS
8704 }
8705}
8706
8707/* Start queuing SelectionRequest events. */
8708
8709void
db3906fd
RS
8710x_start_queuing_selection_requests (display)
8711 Display *display;
09756a85
RS
8712{
8713 x_queue_selection_requests++;
8714}
8715
8716/* Stop queuing SelectionRequest events. */
8717
8718void
db3906fd
RS
8719x_stop_queuing_selection_requests (display)
8720 Display *display;
09756a85
RS
8721{
8722 x_queue_selection_requests--;
db3906fd 8723 x_unqueue_events (display);
09756a85 8724}
f451eb13
JB
8725\f
8726/* The main X event-reading loop - XTread_socket. */
dc6f92b8 8727
06a2c219 8728/* Time stamp of enter window event. This is only used by XTread_socket,
dc6f92b8
JB
8729 but we have to put it out here, since static variables within functions
8730 sometimes don't work. */
06a2c219 8731
dc6f92b8
JB
8732static Time enter_timestamp;
8733
11edeb03 8734/* This holds the state XLookupString needs to implement dead keys
58769bee 8735 and other tricks known as "compose processing". _X Window System_
11edeb03
JB
8736 says that a portable program can't use this, but Stephen Gildea assures
8737 me that letting the compiler initialize it to zeros will work okay.
8738
8739 This must be defined outside of XTread_socket, for the same reasons
06a2c219
GM
8740 given for enter_time stamp, above. */
8741
11edeb03
JB
8742static XComposeStatus compose_status;
8743
10e6549c
RS
8744/* Record the last 100 characters stored
8745 to help debug the loss-of-chars-during-GC problem. */
06a2c219 8746
2224b905
RS
8747static int temp_index;
8748static short temp_buffer[100];
10e6549c 8749
7a13e894
RS
8750/* Set this to nonzero to fake an "X I/O error"
8751 on a particular display. */
06a2c219 8752
7a13e894
RS
8753struct x_display_info *XTread_socket_fake_io_error;
8754
2224b905
RS
8755/* When we find no input here, we occasionally do a no-op command
8756 to verify that the X server is still running and we can still talk with it.
8757 We try all the open displays, one by one.
8758 This variable is used for cycling thru the displays. */
06a2c219 8759
2224b905
RS
8760static struct x_display_info *next_noop_dpyinfo;
8761
06a2c219
GM
8762#define SET_SAVED_MENU_EVENT(size) \
8763 do \
8764 { \
8765 if (f->output_data.x->saved_menu_event == 0) \
8766 f->output_data.x->saved_menu_event \
8767 = (XEvent *) xmalloc (sizeof (XEvent)); \
8768 bcopy (&event, f->output_data.x->saved_menu_event, size); \
8769 if (numchars >= 1) \
8770 { \
8771 bufp->kind = menu_bar_activate_event; \
8772 XSETFRAME (bufp->frame_or_window, f); \
8773 bufp++; \
8774 count++; \
8775 numchars--; \
8776 } \
8777 } \
8778 while (0)
8779
8805890a 8780#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
06a2c219 8781#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8805890a 8782
dc6f92b8
JB
8783/* Read events coming from the X server.
8784 This routine is called by the SIGIO handler.
8785 We return as soon as there are no more events to be read.
8786
8787 Events representing keys are stored in buffer BUFP,
8788 which can hold up to NUMCHARS characters.
8789 We return the number of characters stored into the buffer,
8790 thus pretending to be `read'.
8791
dc6f92b8
JB
8792 EXPECTED is nonzero if the caller knows input is available. */
8793
7c5283e4 8794int
f66868ba 8795XTread_socket (sd, bufp, numchars, expected)
dc6f92b8 8796 register int sd;
8805890a
KH
8797 /* register */ struct input_event *bufp;
8798 /* register */ int numchars;
dc6f92b8
JB
8799 int expected;
8800{
8801 int count = 0;
8802 int nbytes = 0;
dc6f92b8 8803 XEvent event;
f676886a 8804 struct frame *f;
66f55a9d 8805 int event_found = 0;
334208b7 8806 struct x_display_info *dpyinfo;
6c183ba5
RS
8807#ifdef HAVE_X_I18N
8808 Status status_return;
8809#endif
dc6f92b8 8810
9ac0d9e0 8811 if (interrupt_input_blocked)
dc6f92b8 8812 {
9ac0d9e0 8813 interrupt_input_pending = 1;
dc6f92b8
JB
8814 return -1;
8815 }
8816
9ac0d9e0 8817 interrupt_input_pending = 0;
dc6f92b8 8818 BLOCK_INPUT;
c0a04927
RS
8819
8820 /* So people can tell when we have read the available input. */
8821 input_signal_count++;
8822
dc6f92b8 8823 if (numchars <= 0)
06a2c219 8824 abort (); /* Don't think this happens. */
dc6f92b8 8825
7a13e894
RS
8826 /* Find the display we are supposed to read input for.
8827 It's the one communicating on descriptor SD. */
8828 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
8829 {
8830#if 0 /* This ought to be unnecessary; let's verify it. */
dc6f92b8 8831#ifdef FIOSNBIO
7a13e894
RS
8832 /* If available, Xlib uses FIOSNBIO to make the socket
8833 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
e6cbea31 8834 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
06a2c219 8835 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7a13e894 8836 fcntl (dpyinfo->connection, F_SETFL, 0);
c118dd06 8837#endif /* ! defined (FIOSNBIO) */
7a13e894 8838#endif
dc6f92b8 8839
7a13e894
RS
8840#if 0 /* This code can't be made to work, with multiple displays,
8841 and appears not to be used on any system any more.
8842 Also keyboard.c doesn't turn O_NDELAY on and off
8843 for X connections. */
dc6f92b8
JB
8844#ifndef SIGIO
8845#ifndef HAVE_SELECT
7a13e894
RS
8846 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
8847 {
8848 extern int read_alarm_should_throw;
8849 read_alarm_should_throw = 1;
8850 XPeekEvent (dpyinfo->display, &event);
8851 read_alarm_should_throw = 0;
8852 }
c118dd06
JB
8853#endif /* HAVE_SELECT */
8854#endif /* SIGIO */
7a13e894 8855#endif
dc6f92b8 8856
7a13e894
RS
8857 /* For debugging, this gives a way to fake an I/O error. */
8858 if (dpyinfo == XTread_socket_fake_io_error)
8859 {
8860 XTread_socket_fake_io_error = 0;
8861 x_io_error_quitter (dpyinfo->display);
8862 }
dc6f92b8 8863
06a2c219 8864 while (XPending (dpyinfo->display))
dc6f92b8 8865 {
7a13e894 8866 XNextEvent (dpyinfo->display, &event);
06a2c219
GM
8867
8868 if (display_busy_cursor_p)
8869 {
8870 /* Setting inhibit_busy_cursor to 2 inhibits busy-cursor
8871 display until the next X event is read and we come
8872 here again. Setting it to 1 inhibits busy-cursor
8873 display for direct commands. */
8874 if (event.type == MotionNotify
8875 || event.type == EnterNotify
8876 || (dpyinfo->grabbed
8877 && event.type != ButtonRelease))
8878 inhibit_busy_cursor = 2;
8879 else
8880 inhibit_busy_cursor = 1;
8881 }
8882
531483fb 8883#ifdef HAVE_X_I18N
d1bc4182
RS
8884 {
8885 struct frame *f1 = x_any_window_to_frame (dpyinfo,
c99babf2 8886 event.xclient.window);
d1bc4182
RS
8887 /* The necessity of the following line took me
8888 a full work-day to decipher from the docs!! */
8889 if (f1 != 0 && FRAME_XIC (f1) && XFilterEvent (&event, None))
8890 break;
8891 }
0cd6403b 8892#endif
7a13e894
RS
8893 event_found = 1;
8894
8895 switch (event.type)
8896 {
8897 case ClientMessage:
c047688c 8898 {
7a13e894
RS
8899 if (event.xclient.message_type
8900 == dpyinfo->Xatom_wm_protocols
8901 && event.xclient.format == 32)
c047688c 8902 {
7a13e894
RS
8903 if (event.xclient.data.l[0]
8904 == dpyinfo->Xatom_wm_take_focus)
c047688c 8905 {
8c1a6a84
RS
8906 /* Use x_any_window_to_frame because this
8907 could be the shell widget window
8908 if the frame has no title bar. */
8909 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6c183ba5
RS
8910#ifdef HAVE_X_I18N
8911 /* Not quite sure this is needed -pd */
8c1a6a84 8912 if (f && FRAME_XIC (f))
6c183ba5
RS
8913 XSetICFocus (FRAME_XIC (f));
8914#endif
f1da8f06
GM
8915#if 0 /* Emacs sets WM hints whose `input' field is `true'. This
8916 instructs the WM to set the input focus automatically for
8917 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
8918 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
8919 it has set the focus. So, XSetInputFocus below is not
8920 needed.
8921
8922 The call to XSetInputFocus below has also caused trouble. In
8923 cases where the XSetInputFocus done by the WM and the one
8924 below are temporally close (on a fast machine), the call
8925 below can generate additional FocusIn events which confuse
8926 Emacs. */
8927
bf7253f4
RS
8928 /* Since we set WM_TAKE_FOCUS, we must call
8929 XSetInputFocus explicitly. But not if f is null,
8930 since that might be an event for a deleted frame. */
7a13e894 8931 if (f)
bf7253f4
RS
8932 {
8933 Display *d = event.xclient.display;
8934 /* Catch and ignore errors, in case window has been
8935 iconified by a window manager such as GWM. */
8936 int count = x_catch_errors (d);
8937 XSetInputFocus (d, event.xclient.window,
e1f6572f
RS
8938 /* The ICCCM says this is
8939 the only valid choice. */
8940 RevertToParent,
bf7253f4
RS
8941 event.xclient.data.l[1]);
8942 /* This is needed to detect the error
8943 if there is an error. */
8944 XSync (d, False);
8945 x_uncatch_errors (d, count);
8946 }
7a13e894 8947 /* Not certain about handling scroll bars here */
f1da8f06 8948#endif /* 0 */
c047688c 8949 }
7a13e894
RS
8950 else if (event.xclient.data.l[0]
8951 == dpyinfo->Xatom_wm_save_yourself)
8952 {
8953 /* Save state modify the WM_COMMAND property to
06a2c219 8954 something which can reinstate us. This notifies
7a13e894
RS
8955 the session manager, who's looking for such a
8956 PropertyNotify. Can restart processing when
06a2c219 8957 a keyboard or mouse event arrives. */
7a13e894
RS
8958 if (numchars > 0)
8959 {
19126e11
KH
8960 f = x_top_window_to_frame (dpyinfo,
8961 event.xclient.window);
7a13e894
RS
8962
8963 /* This is just so we only give real data once
8964 for a single Emacs process. */
b86bd3dd 8965 if (f == SELECTED_FRAME ())
7a13e894
RS
8966 XSetCommand (FRAME_X_DISPLAY (f),
8967 event.xclient.window,
8968 initial_argv, initial_argc);
f000f5c5 8969 else if (f)
7a13e894
RS
8970 XSetCommand (FRAME_X_DISPLAY (f),
8971 event.xclient.window,
8972 0, 0);
8973 }
8974 }
8975 else if (event.xclient.data.l[0]
8976 == dpyinfo->Xatom_wm_delete_window)
1fb20991 8977 {
19126e11
KH
8978 struct frame *f
8979 = x_any_window_to_frame (dpyinfo,
8980 event.xclient.window);
1fb20991 8981
7a13e894
RS
8982 if (f)
8983 {
8984 if (numchars == 0)
8985 abort ();
1fb20991 8986
7a13e894
RS
8987 bufp->kind = delete_window_event;
8988 XSETFRAME (bufp->frame_or_window, f);
8989 bufp++;
8990
8991 count += 1;
8992 numchars -= 1;
8993 }
1fb20991 8994 }
c047688c 8995 }
7a13e894
RS
8996 else if (event.xclient.message_type
8997 == dpyinfo->Xatom_wm_configure_denied)
8998 {
8999 }
9000 else if (event.xclient.message_type
9001 == dpyinfo->Xatom_wm_window_moved)
9002 {
9003 int new_x, new_y;
19126e11
KH
9004 struct frame *f
9005 = x_window_to_frame (dpyinfo, event.xclient.window);
58769bee 9006
7a13e894
RS
9007 new_x = event.xclient.data.s[0];
9008 new_y = event.xclient.data.s[1];
1fb20991 9009
7a13e894
RS
9010 if (f)
9011 {
7556890b
RS
9012 f->output_data.x->left_pos = new_x;
9013 f->output_data.x->top_pos = new_y;
7a13e894 9014 }
1fb20991 9015 }
0fdff6bb 9016#ifdef HACK_EDITRES
7a13e894
RS
9017 else if (event.xclient.message_type
9018 == dpyinfo->Xatom_editres)
9019 {
19126e11
KH
9020 struct frame *f
9021 = x_any_window_to_frame (dpyinfo, event.xclient.window);
7556890b 9022 _XEditResCheckMessages (f->output_data.x->widget, NULL,
19126e11 9023 &event, NULL);
7a13e894 9024 }
0fdff6bb 9025#endif /* HACK_EDITRES */
06a2c219
GM
9026 else if ((event.xclient.message_type
9027 == dpyinfo->Xatom_DONE)
9028 || (event.xclient.message_type
9029 == dpyinfo->Xatom_PAGE))
9030 {
9031 /* Ghostview job completed. Kill it. We could
9032 reply with "Next" if we received "Page", but we
9033 currently never do because we are interested in
9034 images, only, which should have 1 page. */
06a2c219
GM
9035 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9036 struct frame *f
9037 = x_window_to_frame (dpyinfo, event.xclient.window);
9038 x_kill_gs_process (pixmap, f);
9039 expose_frame (f, 0, 0, 0, 0);
9040 }
9041#ifdef USE_TOOLKIT_SCROLL_BARS
9042 /* Scroll bar callbacks send a ClientMessage from which
9043 we construct an input_event. */
9044 else if (event.xclient.message_type
9045 == dpyinfo->Xatom_Scrollbar)
9046 {
f9632fc8
GM
9047 if (display_busy_cursor_p)
9048 inhibit_busy_cursor = 2;
06a2c219
GM
9049 x_scroll_bar_to_input_event (&event, bufp);
9050 ++bufp, ++count, --numchars;
9051 goto out;
9052 }
9053#endif /* USE_TOOLKIT_SCROLL_BARS */
9054 else
9055 goto OTHER;
7a13e894
RS
9056 }
9057 break;
dc6f92b8 9058
7a13e894 9059 case SelectionNotify:
3afe33e7 9060#ifdef USE_X_TOOLKIT
19126e11 9061 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
7a13e894 9062 goto OTHER;
3afe33e7 9063#endif /* not USE_X_TOOLKIT */
dfcf069d 9064 x_handle_selection_notify (&event.xselection);
7a13e894 9065 break;
d56a553a 9066
06a2c219 9067 case SelectionClear: /* Someone has grabbed ownership. */
3afe33e7 9068#ifdef USE_X_TOOLKIT
19126e11 9069 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
7a13e894 9070 goto OTHER;
3afe33e7 9071#endif /* USE_X_TOOLKIT */
7a13e894
RS
9072 {
9073 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
d56a553a 9074
7a13e894
RS
9075 if (numchars == 0)
9076 abort ();
d56a553a 9077
7a13e894
RS
9078 bufp->kind = selection_clear_event;
9079 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9080 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9081 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9082 bufp->frame_or_window = Qnil;
7a13e894 9083 bufp++;
d56a553a 9084
7a13e894
RS
9085 count += 1;
9086 numchars -= 1;
9087 }
9088 break;
dc6f92b8 9089
06a2c219 9090 case SelectionRequest: /* Someone wants our selection. */
3afe33e7 9091#ifdef USE_X_TOOLKIT
19126e11 9092 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
7a13e894 9093 goto OTHER;
3afe33e7 9094#endif /* USE_X_TOOLKIT */
7a13e894 9095 if (x_queue_selection_requests)
19126e11 9096 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
7a13e894
RS
9097 &event);
9098 else
9099 {
9100 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
dc6f92b8 9101
7a13e894
RS
9102 if (numchars == 0)
9103 abort ();
9104
9105 bufp->kind = selection_request_event;
9106 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9107 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9108 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9109 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9110 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9111 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9112 bufp->frame_or_window = Qnil;
7a13e894
RS
9113 bufp++;
9114
9115 count += 1;
9116 numchars -= 1;
9117 }
9118 break;
9119
9120 case PropertyNotify:
3afe33e7 9121#ifdef USE_X_TOOLKIT
19126e11 9122 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
7a13e894 9123 goto OTHER;
3afe33e7 9124#endif /* not USE_X_TOOLKIT */
dfcf069d 9125 x_handle_property_notify (&event.xproperty);
7a13e894 9126 break;
dc6f92b8 9127
7a13e894 9128 case ReparentNotify:
19126e11 9129 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
7a13e894
RS
9130 if (f)
9131 {
9132 int x, y;
7556890b 9133 f->output_data.x->parent_desc = event.xreparent.parent;
7a13e894 9134 x_real_positions (f, &x, &y);
7556890b
RS
9135 f->output_data.x->left_pos = x;
9136 f->output_data.x->top_pos = y;
7a13e894
RS
9137 }
9138 break;
3bd330d4 9139
7a13e894 9140 case Expose:
19126e11 9141 f = x_window_to_frame (dpyinfo, event.xexpose.window);
7a13e894 9142 if (f)
dc6f92b8 9143 {
7a13e894
RS
9144 if (f->async_visible == 0)
9145 {
9146 f->async_visible = 1;
9147 f->async_iconified = 0;
06c488fd 9148 f->output_data.x->has_been_visible = 1;
7a13e894
RS
9149 SET_FRAME_GARBAGED (f);
9150 }
9151 else
06a2c219
GM
9152 expose_frame (x_window_to_frame (dpyinfo,
9153 event.xexpose.window),
9154 event.xexpose.x, event.xexpose.y,
9155 event.xexpose.width, event.xexpose.height);
dc6f92b8
JB
9156 }
9157 else
7a13e894 9158 {
06a2c219
GM
9159#ifdef USE_TOOLKIT_SCROLL_BARS
9160 /* Dispatch event to the widget. */
9161 goto OTHER;
9162#else /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9163 struct scroll_bar *bar
9164 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 9165
7a13e894
RS
9166 if (bar)
9167 x_scroll_bar_expose (bar, &event);
3afe33e7 9168#ifdef USE_X_TOOLKIT
7a13e894
RS
9169 else
9170 goto OTHER;
3afe33e7 9171#endif /* USE_X_TOOLKIT */
06a2c219 9172#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9173 }
9174 break;
dc6f92b8 9175
7a13e894
RS
9176 case GraphicsExpose: /* This occurs when an XCopyArea's
9177 source area was obscured or not
9178 available.*/
19126e11 9179 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
7a13e894
RS
9180 if (f)
9181 {
06a2c219
GM
9182 expose_frame (f,
9183 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9184 event.xgraphicsexpose.width,
9185 event.xgraphicsexpose.height);
7a13e894 9186 }
3afe33e7 9187#ifdef USE_X_TOOLKIT
7a13e894
RS
9188 else
9189 goto OTHER;
3afe33e7 9190#endif /* USE_X_TOOLKIT */
7a13e894 9191 break;
dc6f92b8 9192
7a13e894 9193 case NoExpose: /* This occurs when an XCopyArea's
06a2c219
GM
9194 source area was completely
9195 available */
7a13e894 9196 break;
dc6f92b8 9197
7a13e894 9198 case UnmapNotify:
06a2c219
GM
9199 /* Redo the mouse-highlight after the tooltip has gone. */
9200 if (event.xmap.window == tip_window)
9201 {
9202 tip_window = 0;
9203 redo_mouse_highlight ();
9204 }
9205
91ea2a7a 9206 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
7a13e894
RS
9207 if (f) /* F may no longer exist if
9208 the frame was deleted. */
9209 {
9210 /* While a frame is unmapped, display generation is
9211 disabled; you don't want to spend time updating a
9212 display that won't ever be seen. */
9213 f->async_visible = 0;
9214 /* We can't distinguish, from the event, whether the window
9215 has become iconified or invisible. So assume, if it
9216 was previously visible, than now it is iconified.
1aa6072f
RS
9217 But x_make_frame_invisible clears both
9218 the visible flag and the iconified flag;
9219 and that way, we know the window is not iconified now. */
7a13e894 9220 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
1aa6072f
RS
9221 {
9222 f->async_iconified = 1;
bddd097c 9223
1aa6072f
RS
9224 bufp->kind = iconify_event;
9225 XSETFRAME (bufp->frame_or_window, f);
9226 bufp++;
9227 count++;
9228 numchars--;
9229 }
7a13e894 9230 }
7a13e894 9231 goto OTHER;
dc6f92b8 9232
7a13e894 9233 case MapNotify:
06a2c219
GM
9234 if (event.xmap.window == tip_window)
9235 /* The tooltip has been drawn already. Avoid
9236 the SET_FRAME_GARBAGED below. */
9237 goto OTHER;
9238
9239 /* We use x_top_window_to_frame because map events can
9240 come for sub-windows and they don't mean that the
9241 frame is visible. */
19126e11 9242 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
7a13e894
RS
9243 if (f)
9244 {
9245 f->async_visible = 1;
9246 f->async_iconified = 0;
06c488fd 9247 f->output_data.x->has_been_visible = 1;
dc6f92b8 9248
7a13e894
RS
9249 /* wait_reading_process_input will notice this and update
9250 the frame's display structures. */
9251 SET_FRAME_GARBAGED (f);
bddd097c 9252
d806e720
RS
9253 if (f->iconified)
9254 {
9255 bufp->kind = deiconify_event;
9256 XSETFRAME (bufp->frame_or_window, f);
9257 bufp++;
9258 count++;
9259 numchars--;
9260 }
e73ec6fa 9261 else if (! NILP (Vframe_list)
8e713be6 9262 && ! NILP (XCDR (Vframe_list)))
78aa2ba5
KH
9263 /* Force a redisplay sooner or later
9264 to update the frame titles
9265 in case this is the second frame. */
9266 record_asynch_buffer_change ();
7a13e894 9267 }
7a13e894 9268 goto OTHER;
dc6f92b8 9269
7a13e894 9270 case KeyPress:
19126e11 9271 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
f451eb13 9272
06a2c219
GM
9273#ifdef USE_MOTIF
9274 /* I couldn't find a way to prevent LessTif scroll bars
9275 from consuming key events. */
9276 if (f == 0)
9277 {
9278 Widget widget = XtWindowToWidget (dpyinfo->display,
9279 event.xkey.window);
9280 if (widget && XmIsScrollBar (widget))
9281 {
9282 widget = XtParent (widget);
9283 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9284 }
9285 }
9286#endif /* USE_MOTIF */
9287
7a13e894
RS
9288 if (f != 0)
9289 {
9290 KeySym keysym, orig_keysym;
9291 /* al%imercury@uunet.uu.net says that making this 81 instead of
9292 80 fixed a bug whereby meta chars made his Emacs hang. */
9293 unsigned char copy_buffer[81];
9294 int modifiers;
64bb1782 9295
7a13e894
RS
9296 event.xkey.state
9297 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9298 extra_keyboard_modifiers);
9299 modifiers = event.xkey.state;
3a2712f9 9300
7a13e894 9301 /* This will have to go some day... */
752a043f 9302
7a13e894
RS
9303 /* make_lispy_event turns chars into control chars.
9304 Don't do it here because XLookupString is too eager. */
9305 event.xkey.state &= ~ControlMask;
5d46f928
RS
9306 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9307 | dpyinfo->super_mod_mask
9308 | dpyinfo->hyper_mod_mask
9309 | dpyinfo->alt_mod_mask);
9310
1cf4a0d1
RS
9311 /* In case Meta is ComposeCharacter,
9312 clear its status. According to Markus Ehrnsperger
9313 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9314 this enables ComposeCharacter to work whether or
9315 not it is combined with Meta. */
9316 if (modifiers & dpyinfo->meta_mod_mask)
9317 bzero (&compose_status, sizeof (compose_status));
9318
6c183ba5
RS
9319#ifdef HAVE_X_I18N
9320 if (FRAME_XIC (f))
9321 {
5d7cc324
RS
9322 /* The necessity of the following line took me
9323 a full work-day to decipher from the docs!! */
9324 if (XFilterEvent (&event, None))
9325 break;
6c183ba5
RS
9326 nbytes = XmbLookupString (FRAME_XIC (f),
9327 &event.xkey, copy_buffer,
9328 80, &keysym,
9329 &status_return);
1decb680
PE
9330 if (status_return == XLookupNone)
9331 break;
9332 else if (status_return == XLookupChars)
9333 keysym = NoSymbol;
9334 else if (status_return != XLookupKeySym
9335 && status_return != XLookupBoth)
9336 abort ();
6c183ba5
RS
9337 }
9338 else
9339 nbytes = XLookupString (&event.xkey, copy_buffer,
9340 80, &keysym, &compose_status);
9341#else
0299d313
RS
9342 nbytes = XLookupString (&event.xkey, copy_buffer,
9343 80, &keysym, &compose_status);
6c183ba5 9344#endif
dc6f92b8 9345
7a13e894 9346 orig_keysym = keysym;
55123275 9347
7a13e894
RS
9348 if (numchars > 1)
9349 {
9350 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9351 || keysym == XK_Delete
1097aea0 9352#ifdef XK_ISO_Left_Tab
441affdb 9353 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
1097aea0 9354#endif
852bff8f 9355 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
7a13e894
RS
9356 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9357 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
c34790e0 9358#ifdef HPUX
7a13e894
RS
9359 /* This recognizes the "extended function keys".
9360 It seems there's no cleaner way.
9361 Test IsModifierKey to avoid handling mode_switch
9362 incorrectly. */
9363 || ((unsigned) (keysym) >= XK_Select
9364 && (unsigned)(keysym) < XK_KP_Space)
69388238
RS
9365#endif
9366#ifdef XK_dead_circumflex
7a13e894 9367 || orig_keysym == XK_dead_circumflex
69388238
RS
9368#endif
9369#ifdef XK_dead_grave
7a13e894 9370 || orig_keysym == XK_dead_grave
69388238
RS
9371#endif
9372#ifdef XK_dead_tilde
7a13e894 9373 || orig_keysym == XK_dead_tilde
69388238
RS
9374#endif
9375#ifdef XK_dead_diaeresis
7a13e894 9376 || orig_keysym == XK_dead_diaeresis
69388238
RS
9377#endif
9378#ifdef XK_dead_macron
7a13e894 9379 || orig_keysym == XK_dead_macron
69388238
RS
9380#endif
9381#ifdef XK_dead_degree
7a13e894 9382 || orig_keysym == XK_dead_degree
69388238
RS
9383#endif
9384#ifdef XK_dead_acute
7a13e894 9385 || orig_keysym == XK_dead_acute
69388238
RS
9386#endif
9387#ifdef XK_dead_cedilla
7a13e894 9388 || orig_keysym == XK_dead_cedilla
69388238
RS
9389#endif
9390#ifdef XK_dead_breve
7a13e894 9391 || orig_keysym == XK_dead_breve
69388238
RS
9392#endif
9393#ifdef XK_dead_ogonek
7a13e894 9394 || orig_keysym == XK_dead_ogonek
69388238
RS
9395#endif
9396#ifdef XK_dead_caron
7a13e894 9397 || orig_keysym == XK_dead_caron
69388238
RS
9398#endif
9399#ifdef XK_dead_doubleacute
7a13e894 9400 || orig_keysym == XK_dead_doubleacute
69388238
RS
9401#endif
9402#ifdef XK_dead_abovedot
7a13e894 9403 || orig_keysym == XK_dead_abovedot
c34790e0 9404#endif
7a13e894
RS
9405 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9406 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9407 /* Any "vendor-specific" key is ok. */
9408 || (orig_keysym & (1 << 28)))
9409 && ! (IsModifierKey (orig_keysym)
7719aa06
RS
9410#ifndef HAVE_X11R5
9411#ifdef XK_Mode_switch
7a13e894 9412 || ((unsigned)(orig_keysym) == XK_Mode_switch)
7719aa06
RS
9413#endif
9414#ifdef XK_Num_Lock
7a13e894 9415 || ((unsigned)(orig_keysym) == XK_Num_Lock)
7719aa06
RS
9416#endif
9417#endif /* not HAVE_X11R5 */
7a13e894 9418 ))
dc6f92b8 9419 {
10e6549c
RS
9420 if (temp_index == sizeof temp_buffer / sizeof (short))
9421 temp_index = 0;
7a13e894
RS
9422 temp_buffer[temp_index++] = keysym;
9423 bufp->kind = non_ascii_keystroke;
9424 bufp->code = keysym;
e0c1aef2 9425 XSETFRAME (bufp->frame_or_window, f);
334208b7
RS
9426 bufp->modifiers
9427 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9428 modifiers);
1113d9db 9429 bufp->timestamp = event.xkey.time;
dc6f92b8 9430 bufp++;
7a13e894
RS
9431 count++;
9432 numchars--;
06a2c219
GM
9433
9434 if (display_busy_cursor_p)
9435 if (keysym != XK_Return || minibuf_level == 0)
9436 inhibit_busy_cursor = 2;
dc6f92b8 9437 }
7a13e894
RS
9438 else if (numchars > nbytes)
9439 {
9440 register int i;
9441
9442 for (i = 0; i < nbytes; i++)
9443 {
9444 if (temp_index == sizeof temp_buffer / sizeof (short))
9445 temp_index = 0;
9446 temp_buffer[temp_index++] = copy_buffer[i];
9447 bufp->kind = ascii_keystroke;
9448 bufp->code = copy_buffer[i];
9449 XSETFRAME (bufp->frame_or_window, f);
9450 bufp->modifiers
9451 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9452 modifiers);
9453 bufp->timestamp = event.xkey.time;
9454 bufp++;
9455 }
9456
9457 count += nbytes;
9458 numchars -= nbytes;
1decb680
PE
9459
9460 if (keysym == NoSymbol)
9461 break;
7a13e894
RS
9462 }
9463 else
9464 abort ();
dc6f92b8 9465 }
10e6549c
RS
9466 else
9467 abort ();
dc6f92b8 9468 }
717ca130 9469 goto OTHER;
f451eb13 9470
7a13e894 9471 /* Here's a possible interpretation of the whole
06a2c219
GM
9472 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
9473 you get a FocusIn event, you have to get a FocusOut
9474 event before you relinquish the focus. If you
9475 haven't received a FocusIn event, then a mere
9476 LeaveNotify is enough to free you. */
f451eb13 9477
7a13e894 9478 case EnterNotify:
06a2c219
GM
9479 {
9480 int from_menu_bar_p = 0;
9481
9482 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
9483
9484#ifdef LESSTIF_VERSION
9485 /* When clicking outside of a menu bar popup to close
9486 it, we get a FocusIn/ EnterNotify sequence of
9487 events. The flag event.xcrossing.focus is not set
9488 in the EnterNotify event of that sequence because
9489 the focus is in the menu bar,
9490 event.xcrossing.window is the frame's X window.
9491 Unconditionally setting the focus frame to null in
9492 this case is not the right thing, because no event
9493 follows that could set the focus frame to the right
9494 value.
9495
9496 This could be a LessTif bug, but I wasn't able to
9497 reproduce the behavior in a simple test program.
9498
9499 (gerd, LessTif 0.88.1). */
9500
9501 if (!event.xcrossing.focus
9502 && f
9503 && f->output_data.x->menubar_widget)
9504 {
9505 Window focus;
9506 int revert;
9507
9508 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9509 if (focus == XtWindow (f->output_data.x->menubar_widget))
9510 from_menu_bar_p = 1;
9511 }
9512#endif /* LESSTIF_VERSION */
6d4238f3 9513
06a2c219
GM
9514 if (event.xcrossing.focus || from_menu_bar_p)
9515 {
9516 /* Avoid nasty pop/raise loops. */
9517 if (f && (!(f->auto_raise)
9518 || !(f->auto_lower)
9519 || (event.xcrossing.time - enter_timestamp) > 500))
9520 {
9521 x_new_focus_frame (dpyinfo, f);
9522 enter_timestamp = event.xcrossing.time;
9523 }
9524 }
9525 else if (f == dpyinfo->x_focus_frame)
9526 x_new_focus_frame (dpyinfo, 0);
9527
9528 /* EnterNotify counts as mouse movement,
9529 so update things that depend on mouse position. */
9530 if (f && !f->output_data.x->busy_p)
9531 note_mouse_movement (f, &event.xmotion);
9532 goto OTHER;
9533 }
dc6f92b8 9534
7a13e894 9535 case FocusIn:
19126e11 9536 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 9537 if (event.xfocus.detail != NotifyPointer)
0f941935 9538 dpyinfo->x_focus_event_frame = f;
7a13e894 9539 if (f)
0f941935 9540 x_new_focus_frame (dpyinfo, f);
f9e24cb9 9541
6c183ba5
RS
9542#ifdef HAVE_X_I18N
9543 if (f && FRAME_XIC (f))
9544 XSetICFocus (FRAME_XIC (f));
9545#endif
9546
7a13e894 9547 goto OTHER;
10c5e63d 9548
7a13e894 9549 case LeaveNotify:
19126e11 9550 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
7a13e894 9551 if (f)
10c5e63d 9552 {
06a2c219
GM
9553 Lisp_Object frame;
9554 int from_menu_bar_p = 0;
9555
7a13e894 9556 if (f == dpyinfo->mouse_face_mouse_frame)
06a2c219
GM
9557 {
9558 /* If we move outside the frame, then we're
9559 certainly no longer on any text in the frame. */
9560 clear_mouse_face (dpyinfo);
9561 dpyinfo->mouse_face_mouse_frame = 0;
9562 }
9563
9564 /* Generate a nil HELP_EVENT to cancel a help-echo.
9565 Do it only if there's something to cancel.
9566 Otherwise, the startup message is cleared when
9567 the mouse leaves the frame. */
9568 if (any_help_event_p)
9569 {
9570 XSETFRAME (frame, f);
9571 bufp->kind = HELP_EVENT;
9572 bufp->frame_or_window = Fcons (frame, Qnil);
9573 ++bufp, ++count, --numchars;
9574 }
7a13e894 9575
06a2c219
GM
9576#ifdef LESSTIF_VERSION
9577 /* Please see the comment at the start of the
9578 EnterNotify case. */
9579 if (!event.xcrossing.focus
9580 && f->output_data.x->menubar_widget)
9581 {
9582 Window focus;
9583 int revert;
9584 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9585 if (focus == XtWindow (f->output_data.x->menubar_widget))
9586 from_menu_bar_p = 1;
9587 }
9588#endif /* LESSTIF_VERSION */
9589
9590 if (event.xcrossing.focus || from_menu_bar_p)
0f941935 9591 x_mouse_leave (dpyinfo);
10c5e63d 9592 else
7a13e894 9593 {
0f941935
KH
9594 if (f == dpyinfo->x_focus_event_frame)
9595 dpyinfo->x_focus_event_frame = 0;
9596 if (f == dpyinfo->x_focus_frame)
9597 x_new_focus_frame (dpyinfo, 0);
7a13e894 9598 }
10c5e63d 9599 }
7a13e894 9600 goto OTHER;
dc6f92b8 9601
7a13e894 9602 case FocusOut:
19126e11 9603 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 9604 if (event.xfocus.detail != NotifyPointer
0f941935
KH
9605 && f == dpyinfo->x_focus_event_frame)
9606 dpyinfo->x_focus_event_frame = 0;
9607 if (f && f == dpyinfo->x_focus_frame)
9608 x_new_focus_frame (dpyinfo, 0);
f9e24cb9 9609
6c183ba5
RS
9610#ifdef HAVE_X_I18N
9611 if (f && FRAME_XIC (f))
9612 XUnsetICFocus (FRAME_XIC (f));
9613#endif
9614
7a13e894 9615 goto OTHER;
dc6f92b8 9616
7a13e894 9617 case MotionNotify:
dc6f92b8 9618 {
06a2c219
GM
9619 previous_help_echo = help_echo;
9620 help_echo = Qnil;
9621
7a13e894
RS
9622 if (dpyinfo->grabbed && last_mouse_frame
9623 && FRAME_LIVE_P (last_mouse_frame))
9624 f = last_mouse_frame;
9625 else
19126e11 9626 f = x_window_to_frame (dpyinfo, event.xmotion.window);
06a2c219 9627
7a13e894
RS
9628 if (f)
9629 note_mouse_movement (f, &event.xmotion);
9630 else
9631 {
e88b3c50 9632#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
9633 struct scroll_bar *bar
9634 = x_window_to_scroll_bar (event.xmotion.window);
f451eb13 9635
7a13e894
RS
9636 if (bar)
9637 x_scroll_bar_note_movement (bar, &event);
e88b3c50 9638#endif /* USE_TOOLKIT_SCROLL_BARS */
b8009dd1 9639
06a2c219
GM
9640 /* If we move outside the frame, then we're
9641 certainly no longer on any text in the frame. */
7a13e894
RS
9642 clear_mouse_face (dpyinfo);
9643 }
06a2c219
GM
9644
9645 /* If the contents of the global variable help_echo
9646 has changed, generate a HELP_EVENT. */
9647 if (STRINGP (help_echo)
9648 || STRINGP (previous_help_echo))
9649 {
9650 Lisp_Object frame;
9651
9652 if (f)
9653 XSETFRAME (frame, f);
9654 else
9655 frame = Qnil;
9656
9657 any_help_event_p = 1;
9658 bufp->kind = HELP_EVENT;
9659 bufp->frame_or_window = Fcons (frame, help_echo);
9660 ++bufp, ++count, --numchars;
9661 }
9662
9663 goto OTHER;
dc6f92b8 9664 }
dc6f92b8 9665
7a13e894 9666 case ConfigureNotify:
9829ddba
RS
9667 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
9668 if (f)
af395ec1 9669 {
5c187dee 9670#ifndef USE_X_TOOLKIT
bf1b7b30
KH
9671 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
9672 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
5c187dee 9673
2d7fc7e8
RS
9674 /* In the toolkit version, change_frame_size
9675 is called by the code that handles resizing
9676 of the EmacsFrame widget. */
7a13e894 9677
7a13e894
RS
9678 /* Even if the number of character rows and columns has
9679 not changed, the font size may have changed, so we need
9680 to check the pixel dimensions as well. */
9681 if (columns != f->width
9682 || rows != f->height
7556890b
RS
9683 || event.xconfigure.width != f->output_data.x->pixel_width
9684 || event.xconfigure.height != f->output_data.x->pixel_height)
7a13e894 9685 {
7d1e984f 9686 change_frame_size (f, rows, columns, 0, 1, 0);
7a13e894 9687 SET_FRAME_GARBAGED (f);
e687d06e 9688 cancel_mouse_face (f);
7a13e894 9689 }
2d7fc7e8 9690#endif
af395ec1 9691
7556890b
RS
9692 f->output_data.x->pixel_width = event.xconfigure.width;
9693 f->output_data.x->pixel_height = event.xconfigure.height;
7a13e894
RS
9694
9695 /* What we have now is the position of Emacs's own window.
9696 Convert that to the position of the window manager window. */
dcb07ae9
RS
9697 x_real_positions (f, &f->output_data.x->left_pos,
9698 &f->output_data.x->top_pos);
9699
9700 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
9701 {
9702 /* Since the WM decorations come below top_pos now,
9703 we must put them below top_pos in the future. */
9704 f->output_data.x->win_gravity = NorthWestGravity;
9705 x_wm_set_size_hint (f, (long) 0, 0);
9706 }
8f08dc93
KH
9707#ifdef USE_MOTIF
9708 /* Some window managers pass (0,0) as the location of
9709 the window, and the Motif event handler stores it
9710 in the emacs widget, which messes up Motif menus. */
9711 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
9712 {
9713 event.xconfigure.x = f->output_data.x->widget->core.x;
9714 event.xconfigure.y = f->output_data.x->widget->core.y;
9715 }
06a2c219 9716#endif /* USE_MOTIF */
7a13e894 9717 }
2d7fc7e8 9718 goto OTHER;
dc6f92b8 9719
7a13e894
RS
9720 case ButtonPress:
9721 case ButtonRelease:
9722 {
9723 /* If we decide we want to generate an event to be seen
9724 by the rest of Emacs, we put it here. */
9725 struct input_event emacs_event;
9ea173e8 9726 int tool_bar_p = 0;
06a2c219 9727
7a13e894 9728 emacs_event.kind = no_event;
7a13e894 9729 bzero (&compose_status, sizeof (compose_status));
9b07615b 9730
06a2c219
GM
9731 if (dpyinfo->grabbed
9732 && last_mouse_frame
9f67f20b
RS
9733 && FRAME_LIVE_P (last_mouse_frame))
9734 f = last_mouse_frame;
9735 else
2224b905 9736 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9f67f20b 9737
06a2c219
GM
9738 if (f)
9739 {
9ea173e8
GM
9740 /* Is this in the tool-bar? */
9741 if (WINDOWP (f->tool_bar_window)
9742 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
06a2c219
GM
9743 {
9744 Lisp_Object window;
9745 int p, x, y;
9746
9747 x = event.xbutton.x;
9748 y = event.xbutton.y;
9749
9750 /* Set x and y. */
9751 window = window_from_coordinates (f, x, y, &p, 1);
9ea173e8 9752 if (EQ (window, f->tool_bar_window))
06a2c219 9753 {
9ea173e8
GM
9754 x_handle_tool_bar_click (f, &event.xbutton);
9755 tool_bar_p = 1;
06a2c219
GM
9756 }
9757 }
9758
9ea173e8 9759 if (!tool_bar_p)
06a2c219
GM
9760 if (!dpyinfo->x_focus_frame
9761 || f == dpyinfo->x_focus_frame)
9762 construct_mouse_click (&emacs_event, &event, f);
7a13e894
RS
9763 }
9764 else
9765 {
06a2c219 9766#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
9767 struct scroll_bar *bar
9768 = x_window_to_scroll_bar (event.xbutton.window);
f451eb13 9769
7a13e894
RS
9770 if (bar)
9771 x_scroll_bar_handle_click (bar, &event, &emacs_event);
06a2c219 9772#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9773 }
9774
9775 if (event.type == ButtonPress)
9776 {
9777 dpyinfo->grabbed |= (1 << event.xbutton.button);
9778 last_mouse_frame = f;
edad46f6
KH
9779 /* Ignore any mouse motion that happened
9780 before this event; any subsequent mouse-movement
9781 Emacs events should reflect only motion after
9782 the ButtonPress. */
a00e91cd
KH
9783 if (f != 0)
9784 f->mouse_moved = 0;
06a2c219 9785
9ea173e8
GM
9786 if (!tool_bar_p)
9787 last_tool_bar_item = -1;
06a2c219
GM
9788 if (display_busy_cursor_p)
9789 inhibit_busy_cursor = 2;
7a13e894 9790 }
3afe33e7
RS
9791 else
9792 {
7a13e894 9793 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
3afe33e7 9794 }
23faf38f 9795
7a13e894
RS
9796 if (numchars >= 1 && emacs_event.kind != no_event)
9797 {
9798 bcopy (&emacs_event, bufp, sizeof (struct input_event));
9799 bufp++;
9800 count++;
9801 numchars--;
9802 }
3afe33e7
RS
9803
9804#ifdef USE_X_TOOLKIT
2224b905
RS
9805 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
9806 /* For a down-event in the menu bar,
9807 don't pass it to Xt right now.
9808 Instead, save it away
9809 and we will pass it to Xt from kbd_buffer_get_event.
9810 That way, we can run some Lisp code first. */
91375f8f
RS
9811 if (f && event.type == ButtonPress
9812 /* Verify the event is really within the menu bar
9813 and not just sent to it due to grabbing. */
9814 && event.xbutton.x >= 0
9815 && event.xbutton.x < f->output_data.x->pixel_width
9816 && event.xbutton.y >= 0
9817 && event.xbutton.y < f->output_data.x->menubar_height
9818 && event.xbutton.same_screen)
2224b905 9819 {
8805890a 9820 SET_SAVED_BUTTON_EVENT;
2237cac9
RS
9821 XSETFRAME (last_mouse_press_frame, f);
9822 }
9823 else if (event.type == ButtonPress)
9824 {
9825 last_mouse_press_frame = Qnil;
30e671c3 9826 goto OTHER;
ce89ef46 9827 }
06a2c219 9828
2237cac9
RS
9829#ifdef USE_MOTIF /* This should do not harm for Lucid,
9830 but I am trying to be cautious. */
ce89ef46
RS
9831 else if (event.type == ButtonRelease)
9832 {
2237cac9 9833 if (!NILP (last_mouse_press_frame))
f10ded1c 9834 {
2237cac9
RS
9835 f = XFRAME (last_mouse_press_frame);
9836 if (f->output_data.x)
06a2c219 9837 SET_SAVED_BUTTON_EVENT;
f10ded1c 9838 }
06a2c219 9839 else
30e671c3 9840 goto OTHER;
2224b905 9841 }
2237cac9 9842#endif /* USE_MOTIF */
2224b905
RS
9843 else
9844 goto OTHER;
3afe33e7 9845#endif /* USE_X_TOOLKIT */
7a13e894
RS
9846 }
9847 break;
dc6f92b8 9848
7a13e894 9849 case CirculateNotify:
06a2c219
GM
9850 goto OTHER;
9851
7a13e894 9852 case CirculateRequest:
06a2c219
GM
9853 goto OTHER;
9854
9855 case VisibilityNotify:
9856 goto OTHER;
dc6f92b8 9857
7a13e894
RS
9858 case MappingNotify:
9859 /* Someone has changed the keyboard mapping - update the
9860 local cache. */
9861 switch (event.xmapping.request)
9862 {
9863 case MappingModifier:
9864 x_find_modifier_meanings (dpyinfo);
9865 /* This is meant to fall through. */
9866 case MappingKeyboard:
9867 XRefreshKeyboardMapping (&event.xmapping);
9868 }
7a13e894 9869 goto OTHER;
dc6f92b8 9870
7a13e894 9871 default:
7a13e894 9872 OTHER:
717ca130 9873#ifdef USE_X_TOOLKIT
7a13e894
RS
9874 BLOCK_INPUT;
9875 XtDispatchEvent (&event);
9876 UNBLOCK_INPUT;
3afe33e7 9877#endif /* USE_X_TOOLKIT */
7a13e894
RS
9878 break;
9879 }
dc6f92b8
JB
9880 }
9881 }
9882
06a2c219
GM
9883 out:;
9884
9a5196d0
RS
9885 /* On some systems, an X bug causes Emacs to get no more events
9886 when the window is destroyed. Detect that. (1994.) */
58769bee 9887 if (! event_found)
ef2a22d0 9888 {
ef2a22d0
RS
9889 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
9890 One XNOOP in 100 loops will make Emacs terminate.
9891 B. Bretthauer, 1994 */
9892 x_noop_count++;
58769bee 9893 if (x_noop_count >= 100)
ef2a22d0
RS
9894 {
9895 x_noop_count=0;
2224b905
RS
9896
9897 if (next_noop_dpyinfo == 0)
9898 next_noop_dpyinfo = x_display_list;
9899
9900 XNoOp (next_noop_dpyinfo->display);
9901
9902 /* Each time we get here, cycle through the displays now open. */
9903 next_noop_dpyinfo = next_noop_dpyinfo->next;
ef2a22d0
RS
9904 }
9905 }
502add23 9906
06a2c219 9907 /* If the focus was just given to an auto-raising frame,
0134a210 9908 raise it now. */
7a13e894 9909 /* ??? This ought to be able to handle more than one such frame. */
0134a210
RS
9910 if (pending_autoraise_frame)
9911 {
9912 x_raise_frame (pending_autoraise_frame);
9913 pending_autoraise_frame = 0;
9914 }
0134a210 9915
dc6f92b8
JB
9916 UNBLOCK_INPUT;
9917 return count;
9918}
06a2c219
GM
9919
9920
9921
dc6f92b8 9922\f
06a2c219
GM
9923/***********************************************************************
9924 Text Cursor
9925 ***********************************************************************/
9926
9927/* Note if the text cursor of window W has been overwritten by a
9928 drawing operation that outputs N glyphs starting at HPOS in the
9929 line given by output_cursor.vpos. N < 0 means all the rest of the
9930 line after HPOS has been written. */
9931
9932static void
9933note_overwritten_text_cursor (w, hpos, n)
9934 struct window *w;
9935 int hpos, n;
9936{
9937 if (updated_area == TEXT_AREA
9938 && output_cursor.vpos == w->phys_cursor.vpos
9939 && hpos <= w->phys_cursor.hpos
9940 && (n < 0
9941 || hpos + n > w->phys_cursor.hpos))
9942 w->phys_cursor_on_p = 0;
9943}
f451eb13
JB
9944
9945
06a2c219
GM
9946/* Set clipping for output in glyph row ROW. W is the window in which
9947 we operate. GC is the graphics context to set clipping in.
9948 WHOLE_LINE_P non-zero means include the areas used for truncation
9949 mark display and alike in the clipping rectangle.
9950
9951 ROW may be a text row or, e.g., a mode line. Text rows must be
9952 clipped to the interior of the window dedicated to text display,
9953 mode lines must be clipped to the whole window. */
dc6f92b8
JB
9954
9955static void
06a2c219
GM
9956x_clip_to_row (w, row, gc, whole_line_p)
9957 struct window *w;
9958 struct glyph_row *row;
9959 GC gc;
9960 int whole_line_p;
dc6f92b8 9961{
06a2c219
GM
9962 struct frame *f = XFRAME (WINDOW_FRAME (w));
9963 XRectangle clip_rect;
9964 int window_x, window_y, window_width, window_height;
dc6f92b8 9965
06a2c219 9966 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5c1aae96 9967
06a2c219
GM
9968 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
9969 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
9970 clip_rect.y = max (clip_rect.y, window_y);
9971 clip_rect.width = window_width;
9972 clip_rect.height = row->visible_height;
5c1aae96 9973
06a2c219
GM
9974 /* If clipping to the whole line, including trunc marks, extend
9975 the rectangle to the left and increase its width. */
9976 if (whole_line_p)
9977 {
110859fc
GM
9978 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
9979 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
06a2c219 9980 }
5c1aae96 9981
06a2c219 9982 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
dc6f92b8
JB
9983}
9984
06a2c219
GM
9985
9986/* Draw a hollow box cursor on window W in glyph row ROW. */
dc6f92b8
JB
9987
9988static void
06a2c219
GM
9989x_draw_hollow_cursor (w, row)
9990 struct window *w;
9991 struct glyph_row *row;
dc6f92b8 9992{
06a2c219
GM
9993 struct frame *f = XFRAME (WINDOW_FRAME (w));
9994 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9995 Display *dpy = FRAME_X_DISPLAY (f);
9996 int x, y, wd, h;
9997 XGCValues xgcv;
9998 struct glyph *cursor_glyph;
9999 GC gc;
10000
10001 /* Compute frame-relative coordinates from window-relative
10002 coordinates. */
10003 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10004 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10005 + row->ascent - w->phys_cursor_ascent);
10006 h = row->height - 1;
10007
10008 /* Get the glyph the cursor is on. If we can't tell because
10009 the current matrix is invalid or such, give up. */
10010 cursor_glyph = get_phys_cursor_glyph (w);
10011 if (cursor_glyph == NULL)
dc6f92b8
JB
10012 return;
10013
06a2c219
GM
10014 /* Compute the width of the rectangle to draw. If on a stretch
10015 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10016 rectangle as wide as the glyph, but use a canonical character
10017 width instead. */
10018 wd = cursor_glyph->pixel_width - 1;
10019 if (cursor_glyph->type == STRETCH_GLYPH
10020 && !x_stretch_cursor_p)
10021 wd = min (CANON_X_UNIT (f), wd);
10022
10023 /* The foreground of cursor_gc is typically the same as the normal
10024 background color, which can cause the cursor box to be invisible. */
10025 xgcv.foreground = f->output_data.x->cursor_pixel;
10026 if (dpyinfo->scratch_cursor_gc)
10027 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10028 else
10029 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10030 GCForeground, &xgcv);
10031 gc = dpyinfo->scratch_cursor_gc;
10032
10033 /* Set clipping, draw the rectangle, and reset clipping again. */
10034 x_clip_to_row (w, row, gc, 0);
10035 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10036 XSetClipMask (dpy, gc, None);
dc6f92b8
JB
10037}
10038
06a2c219
GM
10039
10040/* Draw a bar cursor on window W in glyph row ROW.
10041
10042 Implementation note: One would like to draw a bar cursor with an
10043 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10044 Unfortunately, I didn't find a font yet that has this property set.
10045 --gerd. */
dc6f92b8
JB
10046
10047static void
06a2c219
GM
10048x_draw_bar_cursor (w, row)
10049 struct window *w;
10050 struct glyph_row *row;
dc6f92b8 10051{
06a2c219
GM
10052 /* If cursor hpos is out of bounds, don't draw garbage. This can
10053 happen in mini-buffer windows when switching between echo area
10054 glyphs and mini-buffer. */
10055 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
10056 {
10057 struct frame *f = XFRAME (w->frame);
10058 struct glyph *cursor_glyph;
10059 GC gc;
10060 int x;
10061 unsigned long mask;
10062 XGCValues xgcv;
10063 Display *dpy;
10064 Window window;
10065
10066 cursor_glyph = get_phys_cursor_glyph (w);
10067 if (cursor_glyph == NULL)
10068 return;
10069
10070 xgcv.background = f->output_data.x->cursor_pixel;
10071 xgcv.foreground = f->output_data.x->cursor_pixel;
10072 xgcv.graphics_exposures = 0;
10073 mask = GCForeground | GCBackground | GCGraphicsExposures;
10074 dpy = FRAME_X_DISPLAY (f);
10075 window = FRAME_X_WINDOW (f);
10076 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
10077
10078 if (gc)
10079 XChangeGC (dpy, gc, mask, &xgcv);
10080 else
10081 {
10082 gc = XCreateGC (dpy, window, mask, &xgcv);
10083 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10084 }
10085
10086 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10087 x_clip_to_row (w, row, gc, 0);
10088 XFillRectangle (dpy, window, gc,
10089 x,
10090 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
10091 min (cursor_glyph->pixel_width,
10092 f->output_data.x->cursor_width),
10093 row->height);
10094 XSetClipMask (dpy, gc, None);
10095 }
dc6f92b8
JB
10096}
10097
06a2c219
GM
10098
10099/* Clear the cursor of window W to background color, and mark the
10100 cursor as not shown. This is used when the text where the cursor
10101 is is about to be rewritten. */
10102
dc6f92b8 10103static void
06a2c219
GM
10104x_clear_cursor (w)
10105 struct window *w;
dc6f92b8 10106{
06a2c219
GM
10107 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10108 x_update_window_cursor (w, 0);
10109}
90e65f07 10110
dbc4e1c1 10111
06a2c219
GM
10112/* Draw the cursor glyph of window W in glyph row ROW. See the
10113 comment of x_draw_glyphs for the meaning of HL. */
dbc4e1c1 10114
06a2c219
GM
10115static void
10116x_draw_phys_cursor_glyph (w, row, hl)
10117 struct window *w;
10118 struct glyph_row *row;
10119 enum draw_glyphs_face hl;
10120{
10121 /* If cursor hpos is out of bounds, don't draw garbage. This can
10122 happen in mini-buffer windows when switching between echo area
10123 glyphs and mini-buffer. */
10124 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
66ac4b0e
GM
10125 {
10126 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10127 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10128 hl, 0, 0, 0);
10129
10130 /* When we erase the cursor, and ROW is overlapped by other
10131 rows, make sure that these overlapping parts of other rows
10132 are redrawn. */
10133 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10134 {
10135 if (row > w->current_matrix->rows
10136 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10137 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10138
10139 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10140 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10141 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10142 }
10143 }
06a2c219 10144}
dbc4e1c1 10145
eea6af04 10146
06a2c219 10147/* Erase the image of a cursor of window W from the screen. */
eea6af04 10148
06a2c219
GM
10149static void
10150x_erase_phys_cursor (w)
10151 struct window *w;
10152{
10153 struct frame *f = XFRAME (w->frame);
10154 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10155 int hpos = w->phys_cursor.hpos;
10156 int vpos = w->phys_cursor.vpos;
10157 int mouse_face_here_p = 0;
10158 struct glyph_matrix *active_glyphs = w->current_matrix;
10159 struct glyph_row *cursor_row;
10160 struct glyph *cursor_glyph;
10161 enum draw_glyphs_face hl;
10162
10163 /* No cursor displayed or row invalidated => nothing to do on the
10164 screen. */
10165 if (w->phys_cursor_type == NO_CURSOR)
10166 goto mark_cursor_off;
10167
10168 /* VPOS >= active_glyphs->nrows means that window has been resized.
10169 Don't bother to erase the cursor. */
10170 if (vpos >= active_glyphs->nrows)
10171 goto mark_cursor_off;
10172
10173 /* If row containing cursor is marked invalid, there is nothing we
10174 can do. */
10175 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10176 if (!cursor_row->enabled_p)
10177 goto mark_cursor_off;
10178
10179 /* This can happen when the new row is shorter than the old one.
10180 In this case, either x_draw_glyphs or clear_end_of_line
10181 should have cleared the cursor. Note that we wouldn't be
10182 able to erase the cursor in this case because we don't have a
10183 cursor glyph at hand. */
10184 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10185 goto mark_cursor_off;
10186
10187 /* If the cursor is in the mouse face area, redisplay that when
10188 we clear the cursor. */
10189 if (w == XWINDOW (dpyinfo->mouse_face_window)
10190 && (vpos > dpyinfo->mouse_face_beg_row
10191 || (vpos == dpyinfo->mouse_face_beg_row
10192 && hpos >= dpyinfo->mouse_face_beg_col))
10193 && (vpos < dpyinfo->mouse_face_end_row
10194 || (vpos == dpyinfo->mouse_face_end_row
10195 && hpos < dpyinfo->mouse_face_end_col))
10196 /* Don't redraw the cursor's spot in mouse face if it is at the
10197 end of a line (on a newline). The cursor appears there, but
10198 mouse highlighting does not. */
10199 && cursor_row->used[TEXT_AREA] > hpos)
10200 mouse_face_here_p = 1;
10201
10202 /* Maybe clear the display under the cursor. */
10203 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10204 {
10205 int x;
045dee35 10206 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dbc4e1c1 10207
06a2c219
GM
10208 cursor_glyph = get_phys_cursor_glyph (w);
10209 if (cursor_glyph == NULL)
10210 goto mark_cursor_off;
dbc4e1c1 10211
06a2c219
GM
10212 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10213
10214 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10215 x,
045dee35 10216 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219
GM
10217 cursor_row->y)),
10218 cursor_glyph->pixel_width,
10219 cursor_row->visible_height,
10220 False);
dbc4e1c1 10221 }
06a2c219
GM
10222
10223 /* Erase the cursor by redrawing the character underneath it. */
10224 if (mouse_face_here_p)
10225 hl = DRAW_MOUSE_FACE;
10226 else if (cursor_row->inverse_p)
10227 hl = DRAW_INVERSE_VIDEO;
10228 else
10229 hl = DRAW_NORMAL_TEXT;
10230 x_draw_phys_cursor_glyph (w, cursor_row, hl);
dbc4e1c1 10231
06a2c219
GM
10232 mark_cursor_off:
10233 w->phys_cursor_on_p = 0;
10234 w->phys_cursor_type = NO_CURSOR;
dbc4e1c1
JB
10235}
10236
10237
06a2c219
GM
10238/* Display or clear cursor of window W. If ON is zero, clear the
10239 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10240 where to put the cursor is specified by HPOS, VPOS, X and Y. */
dbc4e1c1 10241
06a2c219
GM
10242void
10243x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10244 struct window *w;
10245 int on, hpos, vpos, x, y;
dbc4e1c1 10246{
06a2c219
GM
10247 struct frame *f = XFRAME (w->frame);
10248 int new_cursor_type;
10249 struct glyph_matrix *current_glyphs;
10250 struct glyph_row *glyph_row;
10251 struct glyph *glyph;
dbc4e1c1 10252
49d838ea 10253 /* This is pointless on invisible frames, and dangerous on garbaged
06a2c219
GM
10254 windows and frames; in the latter case, the frame or window may
10255 be in the midst of changing its size, and x and y may be off the
10256 window. */
10257 if (! FRAME_VISIBLE_P (f)
10258 || FRAME_GARBAGED_P (f)
10259 || vpos >= w->current_matrix->nrows
10260 || hpos >= w->current_matrix->matrix_w)
dc6f92b8
JB
10261 return;
10262
10263 /* If cursor is off and we want it off, return quickly. */
06a2c219 10264 if (!on && !w->phys_cursor_on_p)
dc6f92b8
JB
10265 return;
10266
06a2c219
GM
10267 current_glyphs = w->current_matrix;
10268 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10269 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10270
10271 /* If cursor row is not enabled, we don't really know where to
10272 display the cursor. */
10273 if (!glyph_row->enabled_p)
10274 {
10275 w->phys_cursor_on_p = 0;
10276 return;
10277 }
10278
10279 xassert (interrupt_input_blocked);
10280
10281 /* Set new_cursor_type to the cursor we want to be displayed. In a
10282 mini-buffer window, we want the cursor only to appear if we are
10283 reading input from this window. For the selected window, we want
10284 the cursor type given by the frame parameter. If explicitly
10285 marked off, draw no cursor. In all other cases, we want a hollow
10286 box cursor. */
9b4a7047
GM
10287 if (cursor_in_echo_area
10288 && FRAME_HAS_MINIBUF_P (f)
10289 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
06a2c219 10290 {
9b4a7047
GM
10291 if (w == XWINDOW (echo_area_window))
10292 new_cursor_type = FRAME_DESIRED_CURSOR (f);
06a2c219
GM
10293 else
10294 new_cursor_type = HOLLOW_BOX_CURSOR;
10295 }
06a2c219 10296 else
9b4a7047
GM
10297 {
10298 if (w != XWINDOW (selected_window)
10299 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
10300 {
10301 if (MINI_WINDOW_P (w))
10302 new_cursor_type = NO_CURSOR;
10303 else
10304 new_cursor_type = HOLLOW_BOX_CURSOR;
10305 }
10306 else if (w->cursor_off_p)
10307 new_cursor_type = NO_CURSOR;
10308 else
10309 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10310 }
06a2c219
GM
10311
10312 /* If cursor is currently being shown and we don't want it to be or
10313 it is in the wrong place, or the cursor type is not what we want,
dc6f92b8 10314 erase it. */
06a2c219 10315 if (w->phys_cursor_on_p
dc6f92b8 10316 && (!on
06a2c219
GM
10317 || w->phys_cursor.x != x
10318 || w->phys_cursor.y != y
10319 || new_cursor_type != w->phys_cursor_type))
10320 x_erase_phys_cursor (w);
10321
10322 /* If the cursor is now invisible and we want it to be visible,
10323 display it. */
10324 if (on && !w->phys_cursor_on_p)
10325 {
10326 w->phys_cursor_ascent = glyph_row->ascent;
10327 w->phys_cursor_height = glyph_row->height;
10328
10329 /* Set phys_cursor_.* before x_draw_.* is called because some
10330 of them may need the information. */
10331 w->phys_cursor.x = x;
10332 w->phys_cursor.y = glyph_row->y;
10333 w->phys_cursor.hpos = hpos;
10334 w->phys_cursor.vpos = vpos;
10335 w->phys_cursor_type = new_cursor_type;
10336 w->phys_cursor_on_p = 1;
10337
10338 switch (new_cursor_type)
dc6f92b8 10339 {
06a2c219
GM
10340 case HOLLOW_BOX_CURSOR:
10341 x_draw_hollow_cursor (w, glyph_row);
10342 break;
10343
10344 case FILLED_BOX_CURSOR:
10345 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10346 break;
10347
10348 case BAR_CURSOR:
10349 x_draw_bar_cursor (w, glyph_row);
10350 break;
10351
10352 case NO_CURSOR:
10353 break;
dc6f92b8 10354
06a2c219
GM
10355 default:
10356 abort ();
10357 }
dc6f92b8
JB
10358 }
10359
06a2c219 10360#ifndef XFlush
f676886a 10361 if (updating_frame != f)
334208b7 10362 XFlush (FRAME_X_DISPLAY (f));
06a2c219 10363#endif
dc6f92b8
JB
10364}
10365
06a2c219
GM
10366
10367/* Display the cursor on window W, or clear it. X and Y are window
10368 relative pixel coordinates. HPOS and VPOS are glyph matrix
10369 positions. If W is not the selected window, display a hollow
10370 cursor. ON non-zero means display the cursor at X, Y which
10371 correspond to HPOS, VPOS, otherwise it is cleared. */
5d46f928 10372
dfcf069d 10373void
06a2c219
GM
10374x_display_cursor (w, on, hpos, vpos, x, y)
10375 struct window *w;
10376 int on, hpos, vpos, x, y;
dc6f92b8 10377{
f94397b5 10378 BLOCK_INPUT;
06a2c219 10379 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
5d46f928
RS
10380 UNBLOCK_INPUT;
10381}
10382
06a2c219
GM
10383
10384/* Display the cursor on window W, or clear it, according to ON_P.
5d46f928
RS
10385 Don't change the cursor's position. */
10386
dfcf069d 10387void
06a2c219 10388x_update_cursor (f, on_p)
5d46f928 10389 struct frame *f;
5d46f928 10390{
06a2c219
GM
10391 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
10392}
10393
10394
10395/* Call x_update_window_cursor with parameter ON_P on all leaf windows
10396 in the window tree rooted at W. */
10397
10398static void
10399x_update_cursor_in_window_tree (w, on_p)
10400 struct window *w;
10401 int on_p;
10402{
10403 while (w)
10404 {
10405 if (!NILP (w->hchild))
10406 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10407 else if (!NILP (w->vchild))
10408 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10409 else
10410 x_update_window_cursor (w, on_p);
10411
10412 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10413 }
10414}
5d46f928 10415
f94397b5 10416
06a2c219
GM
10417/* Switch the display of W's cursor on or off, according to the value
10418 of ON. */
10419
10420static void
10421x_update_window_cursor (w, on)
10422 struct window *w;
10423 int on;
10424{
10425 BLOCK_INPUT;
10426 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
10427 w->phys_cursor.x, w->phys_cursor.y);
f94397b5 10428 UNBLOCK_INPUT;
dc6f92b8 10429}
06a2c219
GM
10430
10431
10432
dc6f92b8
JB
10433\f
10434/* Icons. */
10435
f676886a 10436/* Refresh bitmap kitchen sink icon for frame F
06a2c219 10437 when we get an expose event for it. */
dc6f92b8 10438
dfcf069d 10439void
f676886a
JB
10440refreshicon (f)
10441 struct frame *f;
dc6f92b8 10442{
06a2c219 10443 /* Normally, the window manager handles this function. */
dc6f92b8
JB
10444}
10445
dbc4e1c1 10446/* Make the x-window of frame F use the gnu icon bitmap. */
dc6f92b8
JB
10447
10448int
990ba854 10449x_bitmap_icon (f, file)
f676886a 10450 struct frame *f;
990ba854 10451 Lisp_Object file;
dc6f92b8 10452{
06a2c219 10453 int bitmap_id;
dc6f92b8 10454
c118dd06 10455 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
10456 return 1;
10457
990ba854 10458 /* Free up our existing icon bitmap if any. */
7556890b
RS
10459 if (f->output_data.x->icon_bitmap > 0)
10460 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10461 f->output_data.x->icon_bitmap = 0;
990ba854
RS
10462
10463 if (STRINGP (file))
7f2ae036
RS
10464 bitmap_id = x_create_bitmap_from_file (f, file);
10465 else
10466 {
990ba854 10467 /* Create the GNU bitmap if necessary. */
5bf01b68 10468 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
334208b7
RS
10469 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
10470 = x_create_bitmap_from_data (f, gnu_bits,
10471 gnu_width, gnu_height);
990ba854
RS
10472
10473 /* The first time we create the GNU bitmap,
06a2c219 10474 this increments the ref-count one extra time.
990ba854
RS
10475 As a result, the GNU bitmap is never freed.
10476 That way, we don't have to worry about allocating it again. */
334208b7 10477 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
990ba854 10478
334208b7 10479 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7f2ae036
RS
10480 }
10481
10482 x_wm_set_icon_pixmap (f, bitmap_id);
7556890b 10483 f->output_data.x->icon_bitmap = bitmap_id;
dc6f92b8
JB
10484
10485 return 0;
10486}
10487
10488
1be2d067
KH
10489/* Make the x-window of frame F use a rectangle with text.
10490 Use ICON_NAME as the text. */
dc6f92b8
JB
10491
10492int
f676886a
JB
10493x_text_icon (f, icon_name)
10494 struct frame *f;
dc6f92b8
JB
10495 char *icon_name;
10496{
c118dd06 10497 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
10498 return 1;
10499
1be2d067
KH
10500#ifdef HAVE_X11R4
10501 {
10502 XTextProperty text;
10503 text.value = (unsigned char *) icon_name;
10504 text.encoding = XA_STRING;
10505 text.format = 8;
10506 text.nitems = strlen (icon_name);
10507#ifdef USE_X_TOOLKIT
7556890b 10508 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
1be2d067
KH
10509 &text);
10510#else /* not USE_X_TOOLKIT */
10511 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
10512#endif /* not USE_X_TOOLKIT */
10513 }
10514#else /* not HAVE_X11R4 */
10515 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
10516#endif /* not HAVE_X11R4 */
58769bee 10517
7556890b
RS
10518 if (f->output_data.x->icon_bitmap > 0)
10519 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10520 f->output_data.x->icon_bitmap = 0;
b1c884c3 10521 x_wm_set_icon_pixmap (f, 0);
dc6f92b8
JB
10522
10523 return 0;
10524}
10525\f
e99db5a1
RS
10526#define X_ERROR_MESSAGE_SIZE 200
10527
10528/* If non-nil, this should be a string.
10529 It means catch X errors and store the error message in this string. */
10530
10531static Lisp_Object x_error_message_string;
10532
10533/* An X error handler which stores the error message in
10534 x_error_message_string. This is called from x_error_handler if
10535 x_catch_errors is in effect. */
10536
06a2c219 10537static void
e99db5a1
RS
10538x_error_catcher (display, error)
10539 Display *display;
10540 XErrorEvent *error;
10541{
10542 XGetErrorText (display, error->error_code,
10543 XSTRING (x_error_message_string)->data,
10544 X_ERROR_MESSAGE_SIZE);
10545}
10546
10547/* Begin trapping X errors for display DPY. Actually we trap X errors
10548 for all displays, but DPY should be the display you are actually
10549 operating on.
10550
10551 After calling this function, X protocol errors no longer cause
10552 Emacs to exit; instead, they are recorded in the string
10553 stored in x_error_message_string.
10554
10555 Calling x_check_errors signals an Emacs error if an X error has
10556 occurred since the last call to x_catch_errors or x_check_errors.
10557
10558 Calling x_uncatch_errors resumes the normal error handling. */
10559
10560void x_check_errors ();
10561static Lisp_Object x_catch_errors_unwind ();
10562
10563int
10564x_catch_errors (dpy)
10565 Display *dpy;
10566{
10567 int count = specpdl_ptr - specpdl;
10568
10569 /* Make sure any errors from previous requests have been dealt with. */
10570 XSync (dpy, False);
10571
10572 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
10573
10574 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
10575 XSTRING (x_error_message_string)->data[0] = 0;
10576
10577 return count;
10578}
10579
10580/* Unbind the binding that we made to check for X errors. */
10581
10582static Lisp_Object
10583x_catch_errors_unwind (old_val)
10584 Lisp_Object old_val;
10585{
10586 x_error_message_string = old_val;
10587 return Qnil;
10588}
10589
10590/* If any X protocol errors have arrived since the last call to
10591 x_catch_errors or x_check_errors, signal an Emacs error using
10592 sprintf (a buffer, FORMAT, the x error message text) as the text. */
10593
10594void
10595x_check_errors (dpy, format)
10596 Display *dpy;
10597 char *format;
10598{
10599 /* Make sure to catch any errors incurred so far. */
10600 XSync (dpy, False);
10601
10602 if (XSTRING (x_error_message_string)->data[0])
10603 error (format, XSTRING (x_error_message_string)->data);
10604}
10605
9829ddba
RS
10606/* Nonzero if we had any X protocol errors
10607 since we did x_catch_errors on DPY. */
e99db5a1
RS
10608
10609int
10610x_had_errors_p (dpy)
10611 Display *dpy;
10612{
10613 /* Make sure to catch any errors incurred so far. */
10614 XSync (dpy, False);
10615
10616 return XSTRING (x_error_message_string)->data[0] != 0;
10617}
10618
9829ddba
RS
10619/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
10620
06a2c219 10621void
9829ddba
RS
10622x_clear_errors (dpy)
10623 Display *dpy;
10624{
10625 XSTRING (x_error_message_string)->data[0] = 0;
10626}
10627
e99db5a1
RS
10628/* Stop catching X protocol errors and let them make Emacs die.
10629 DPY should be the display that was passed to x_catch_errors.
10630 COUNT should be the value that was returned by
10631 the corresponding call to x_catch_errors. */
10632
10633void
10634x_uncatch_errors (dpy, count)
10635 Display *dpy;
10636 int count;
10637{
10638 unbind_to (count, Qnil);
10639}
10640
10641#if 0
10642static unsigned int x_wire_count;
10643x_trace_wire ()
10644{
10645 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
10646}
10647#endif /* ! 0 */
10648
10649\f
10650/* Handle SIGPIPE, which can happen when the connection to a server
10651 simply goes away. SIGPIPE is handled by x_connection_signal.
10652 Don't need to do anything, because the write which caused the
10653 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
06a2c219 10654 which will do the appropriate cleanup for us. */
e99db5a1
RS
10655
10656static SIGTYPE
10657x_connection_signal (signalnum) /* If we don't have an argument, */
06a2c219 10658 int signalnum; /* some compilers complain in signal calls. */
e99db5a1
RS
10659{
10660#ifdef USG
10661 /* USG systems forget handlers when they are used;
10662 must reestablish each time */
10663 signal (signalnum, x_connection_signal);
10664#endif /* USG */
10665}
10666\f
4746118a
JB
10667/* Handling X errors. */
10668
7a13e894 10669/* Handle the loss of connection to display DISPLAY. */
16bd92ea 10670
4746118a 10671static SIGTYPE
7a13e894
RS
10672x_connection_closed (display, error_message)
10673 Display *display;
10674 char *error_message;
4746118a 10675{
7a13e894
RS
10676 struct x_display_info *dpyinfo = x_display_info_for_display (display);
10677 Lisp_Object frame, tail;
10678
6186a4a0
RS
10679 /* Indicate that this display is dead. */
10680
f613a4c8 10681#ifdef USE_X_TOOLKIT
adabc3a9 10682 XtCloseDisplay (display);
f613a4c8 10683#endif
adabc3a9 10684
6186a4a0
RS
10685 dpyinfo->display = 0;
10686
06a2c219 10687 /* First delete frames whose mini-buffers are on frames
7a13e894
RS
10688 that are on the dead display. */
10689 FOR_EACH_FRAME (tail, frame)
10690 {
10691 Lisp_Object minibuf_frame;
10692 minibuf_frame
10693 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
f48f33ca
RS
10694 if (FRAME_X_P (XFRAME (frame))
10695 && FRAME_X_P (XFRAME (minibuf_frame))
10696 && ! EQ (frame, minibuf_frame)
10697 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7a13e894
RS
10698 Fdelete_frame (frame, Qt);
10699 }
10700
10701 /* Now delete all remaining frames on the dead display.
06a2c219 10702 We are now sure none of these is used as the mini-buffer
7a13e894
RS
10703 for another frame that we need to delete. */
10704 FOR_EACH_FRAME (tail, frame)
f48f33ca
RS
10705 if (FRAME_X_P (XFRAME (frame))
10706 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
07a7096a
KH
10707 {
10708 /* Set this to t so that Fdelete_frame won't get confused
10709 trying to find a replacement. */
10710 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
10711 Fdelete_frame (frame, Qt);
10712 }
7a13e894 10713
482a1bd2
KH
10714 if (dpyinfo)
10715 x_delete_display (dpyinfo);
7a13e894
RS
10716
10717 if (x_display_list == 0)
10718 {
f8d07b62 10719 fprintf (stderr, "%s\n", error_message);
7a13e894
RS
10720 shut_down_emacs (0, 0, Qnil);
10721 exit (70);
10722 }
12ba150f 10723
7a13e894
RS
10724 /* Ordinary stack unwind doesn't deal with these. */
10725#ifdef SIGIO
10726 sigunblock (sigmask (SIGIO));
10727#endif
10728 sigunblock (sigmask (SIGALRM));
10729 TOTALLY_UNBLOCK_INPUT;
10730
aa4d9a9e 10731 clear_waiting_for_input ();
7a13e894 10732 error ("%s", error_message);
4746118a
JB
10733}
10734
7a13e894
RS
10735/* This is the usual handler for X protocol errors.
10736 It kills all frames on the display that we got the error for.
10737 If that was the only one, it prints an error message and kills Emacs. */
10738
06a2c219 10739static void
c118dd06
JB
10740x_error_quitter (display, error)
10741 Display *display;
10742 XErrorEvent *error;
10743{
7a13e894 10744 char buf[256], buf1[356];
dc6f92b8 10745
58769bee 10746 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 10747 original error handler. */
dc6f92b8 10748
c118dd06 10749 XGetErrorText (display, error->error_code, buf, sizeof (buf));
0a0fdc70 10750 sprintf (buf1, "X protocol error: %s on protocol request %d",
c118dd06 10751 buf, error->request_code);
7a13e894 10752 x_connection_closed (display, buf1);
dc6f92b8
JB
10753}
10754
e99db5a1
RS
10755/* This is the first-level handler for X protocol errors.
10756 It calls x_error_quitter or x_error_catcher. */
7a13e894 10757
8922af5f 10758static int
e99db5a1 10759x_error_handler (display, error)
8922af5f 10760 Display *display;
e99db5a1 10761 XErrorEvent *error;
8922af5f 10762{
e99db5a1
RS
10763 if (! NILP (x_error_message_string))
10764 x_error_catcher (display, error);
10765 else
10766 x_error_quitter (display, error);
06a2c219 10767 return 0;
f9e24cb9 10768}
c118dd06 10769
e99db5a1
RS
10770/* This is the handler for X IO errors, always.
10771 It kills all frames on the display that we lost touch with.
10772 If that was the only one, it prints an error message and kills Emacs. */
7a13e894 10773
c118dd06 10774static int
e99db5a1 10775x_io_error_quitter (display)
c118dd06 10776 Display *display;
c118dd06 10777{
e99db5a1 10778 char buf[256];
dc6f92b8 10779
e99db5a1
RS
10780 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
10781 x_connection_closed (display, buf);
06a2c219 10782 return 0;
dc6f92b8 10783}
dc6f92b8 10784\f
f451eb13
JB
10785/* Changing the font of the frame. */
10786
76bcdf39
RS
10787/* Give frame F the font named FONTNAME as its default font, and
10788 return the full name of that font. FONTNAME may be a wildcard
10789 pattern; in that case, we choose some font that fits the pattern.
10790 The return value shows which font we chose. */
10791
b5cf7a0e 10792Lisp_Object
f676886a
JB
10793x_new_font (f, fontname)
10794 struct frame *f;
dc6f92b8
JB
10795 register char *fontname;
10796{
dc43ef94
KH
10797 struct font_info *fontp
10798 = fs_load_font (f, FRAME_X_FONT_TABLE (f), CHARSET_ASCII, fontname, -1);
dc6f92b8 10799
dc43ef94
KH
10800 if (!fontp)
10801 return Qnil;
2224a5fc 10802
dc43ef94 10803 f->output_data.x->font = (XFontStruct *) (fontp->font);
b4192550 10804 f->output_data.x->baseline_offset = fontp->baseline_offset;
dc43ef94
KH
10805 f->output_data.x->fontset = -1;
10806
b2cad826
KH
10807 /* Compute the scroll bar width in character columns. */
10808 if (f->scroll_bar_pixel_width > 0)
10809 {
7556890b 10810 int wid = FONT_WIDTH (f->output_data.x->font);
b2cad826
KH
10811 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
10812 }
10813 else
4e61bddf
RS
10814 {
10815 int wid = FONT_WIDTH (f->output_data.x->font);
10816 f->scroll_bar_cols = (14 + wid - 1) / wid;
10817 }
b2cad826 10818
f676886a 10819 /* Now make the frame display the given font. */
c118dd06 10820 if (FRAME_X_WINDOW (f) != 0)
dc6f92b8 10821 {
7556890b
RS
10822 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
10823 f->output_data.x->font->fid);
10824 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
10825 f->output_data.x->font->fid);
10826 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
10827 f->output_data.x->font->fid);
f676886a 10828
a27f9f86 10829 frame_update_line_height (f);
0134a210 10830 x_set_window_size (f, 0, f->width, f->height);
dc6f92b8 10831 }
a27f9f86
RS
10832 else
10833 /* If we are setting a new frame's font for the first time,
10834 there are no faces yet, so this font's height is the line height. */
7556890b 10835 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
dc6f92b8 10836
dc43ef94
KH
10837 return build_string (fontp->full_name);
10838}
10839
10840/* Give frame F the fontset named FONTSETNAME as its default font, and
10841 return the full name of that fontset. FONTSETNAME may be a wildcard
b5210ea7
KH
10842 pattern; in that case, we choose some fontset that fits the pattern.
10843 The return value shows which fontset we chose. */
b5cf7a0e 10844
dc43ef94
KH
10845Lisp_Object
10846x_new_fontset (f, fontsetname)
10847 struct frame *f;
10848 char *fontsetname;
10849{
10850 int fontset = fs_query_fontset (f, fontsetname);
10851 struct fontset_info *fontsetp;
10852 Lisp_Object result;
b5cf7a0e 10853
dc43ef94
KH
10854 if (fontset < 0)
10855 return Qnil;
b5cf7a0e 10856
2da424f1
KH
10857 if (f->output_data.x->fontset == fontset)
10858 /* This fontset is already set in frame F. There's nothing more
10859 to do. */
10860 return build_string (fontsetname);
10861
dc43ef94
KH
10862 fontsetp = FRAME_FONTSET_DATA (f)->fontset_table[fontset];
10863
10864 if (!fontsetp->fontname[CHARSET_ASCII])
10865 /* This fontset doesn't contain ASCII font. */
10866 return Qnil;
10867
10868 result = x_new_font (f, fontsetp->fontname[CHARSET_ASCII]);
10869
10870 if (!STRINGP (result))
10871 /* Can't load ASCII font. */
10872 return Qnil;
10873
10874 /* Since x_new_font doesn't update any fontset information, do it now. */
10875 f->output_data.x->fontset = fontset;
2da424f1 10876 FS_LOAD_FONT (f, FRAME_X_FONT_TABLE (f),
715a159b 10877 CHARSET_ASCII, fontsetp->fontname[CHARSET_ASCII], fontset);
dc43ef94
KH
10878
10879 return build_string (fontsetname);
dc6f92b8 10880}
dc6f92b8 10881\f
2e365682
RS
10882/* Calculate the absolute position in frame F
10883 from its current recorded position values and gravity. */
10884
dfcf069d 10885void
43bca5d5 10886x_calc_absolute_position (f)
f676886a 10887 struct frame *f;
dc6f92b8 10888{
06a2c219 10889 Window child;
6dba1858 10890 int win_x = 0, win_y = 0;
7556890b 10891 int flags = f->output_data.x->size_hint_flags;
c81412a0
KH
10892 int this_window;
10893
9829ddba
RS
10894 /* We have nothing to do if the current position
10895 is already for the top-left corner. */
10896 if (! ((flags & XNegative) || (flags & YNegative)))
10897 return;
10898
c81412a0 10899#ifdef USE_X_TOOLKIT
7556890b 10900 this_window = XtWindow (f->output_data.x->widget);
c81412a0
KH
10901#else
10902 this_window = FRAME_X_WINDOW (f);
10903#endif
6dba1858
RS
10904
10905 /* Find the position of the outside upper-left corner of
9829ddba
RS
10906 the inner window, with respect to the outer window.
10907 But do this only if we will need the results. */
7556890b 10908 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6dba1858 10909 {
9829ddba
RS
10910 int count;
10911
6dba1858 10912 BLOCK_INPUT;
9829ddba
RS
10913 count = x_catch_errors (FRAME_X_DISPLAY (f));
10914 while (1)
10915 {
10916 x_clear_errors (FRAME_X_DISPLAY (f));
10917 XTranslateCoordinates (FRAME_X_DISPLAY (f),
10918
10919 /* From-window, to-window. */
10920 this_window,
10921 f->output_data.x->parent_desc,
10922
10923 /* From-position, to-position. */
10924 0, 0, &win_x, &win_y,
10925
10926 /* Child of win. */
10927 &child);
10928 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
10929 {
10930 Window newroot, newparent = 0xdeadbeef;
10931 Window *newchildren;
10932 int nchildren;
10933
10934 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
10935 &newparent, &newchildren, &nchildren))
10936 break;
58769bee 10937
7c3c78a3 10938 XFree ((char *) newchildren);
6dba1858 10939
9829ddba
RS
10940 f->output_data.x->parent_desc = newparent;
10941 }
10942 else
10943 break;
10944 }
6dba1858 10945
9829ddba 10946 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
6dba1858
RS
10947 UNBLOCK_INPUT;
10948 }
10949
10950 /* Treat negative positions as relative to the leftmost bottommost
10951 position that fits on the screen. */
20f55f9a 10952 if (flags & XNegative)
7556890b 10953 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
2e365682
RS
10954 - 2 * f->output_data.x->border_width - win_x
10955 - PIXEL_WIDTH (f)
10956 + f->output_data.x->left_pos);
dc6f92b8 10957
20f55f9a 10958 if (flags & YNegative)
06a2c219
GM
10959 {
10960 int menubar_height = 0;
10961
10962#ifdef USE_X_TOOLKIT
10963 if (f->output_data.x->menubar_widget)
10964 menubar_height
10965 = (f->output_data.x->menubar_widget->core.height
10966 + f->output_data.x->menubar_widget->core.border_width);
10967#endif
10968
10969 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
10970 - 2 * f->output_data.x->border_width
10971 - win_y
10972 - PIXEL_HEIGHT (f)
10973 - menubar_height
10974 + f->output_data.x->top_pos);
10975 }
2e365682 10976
3a35ab44
RS
10977 /* The left_pos and top_pos
10978 are now relative to the top and left screen edges,
10979 so the flags should correspond. */
7556890b 10980 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
dc6f92b8
JB
10981}
10982
3a35ab44
RS
10983/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
10984 to really change the position, and 0 when calling from
10985 x_make_frame_visible (in that case, XOFF and YOFF are the current
aa3ff7c9
KH
10986 position values). It is -1 when calling from x_set_frame_parameters,
10987 which means, do adjust for borders but don't change the gravity. */
3a35ab44 10988
dfcf069d 10989void
dc05a16b 10990x_set_offset (f, xoff, yoff, change_gravity)
f676886a 10991 struct frame *f;
dc6f92b8 10992 register int xoff, yoff;
dc05a16b 10993 int change_gravity;
dc6f92b8 10994{
4a4cbdd5
KH
10995 int modified_top, modified_left;
10996
aa3ff7c9 10997 if (change_gravity > 0)
3a35ab44 10998 {
7556890b
RS
10999 f->output_data.x->top_pos = yoff;
11000 f->output_data.x->left_pos = xoff;
11001 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
3a35ab44 11002 if (xoff < 0)
7556890b 11003 f->output_data.x->size_hint_flags |= XNegative;
3a35ab44 11004 if (yoff < 0)
7556890b
RS
11005 f->output_data.x->size_hint_flags |= YNegative;
11006 f->output_data.x->win_gravity = NorthWestGravity;
3a35ab44 11007 }
43bca5d5 11008 x_calc_absolute_position (f);
dc6f92b8
JB
11009
11010 BLOCK_INPUT;
c32cdd9a 11011 x_wm_set_size_hint (f, (long) 0, 0);
3a35ab44 11012
7556890b
RS
11013 modified_left = f->output_data.x->left_pos;
11014 modified_top = f->output_data.x->top_pos;
e73ec6fa
RS
11015#if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
11016 this seems to be unnecessary and incorrect. rms, 4/17/97. */
11017 /* It is a mystery why we need to add the border_width here
11018 when the frame is already visible, but experiment says we do. */
aa3ff7c9 11019 if (change_gravity != 0)
4a4cbdd5 11020 {
7556890b
RS
11021 modified_left += f->output_data.x->border_width;
11022 modified_top += f->output_data.x->border_width;
4a4cbdd5 11023 }
e73ec6fa 11024#endif
4a4cbdd5 11025
3afe33e7 11026#ifdef USE_X_TOOLKIT
7556890b 11027 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
4a4cbdd5 11028 modified_left, modified_top);
3afe33e7 11029#else /* not USE_X_TOOLKIT */
334208b7 11030 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4a4cbdd5 11031 modified_left, modified_top);
3afe33e7 11032#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11033 UNBLOCK_INPUT;
11034}
11035
bc20ebbf
FP
11036/* Call this to change the size of frame F's x-window.
11037 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
11038 for this size change and subsequent size changes.
11039 Otherwise we leave the window gravity unchanged. */
dc6f92b8 11040
dfcf069d 11041void
bc20ebbf 11042x_set_window_size (f, change_gravity, cols, rows)
f676886a 11043 struct frame *f;
bc20ebbf 11044 int change_gravity;
b1c884c3 11045 int cols, rows;
dc6f92b8 11046{
06a2c219 11047#ifndef USE_X_TOOLKIT
dc6f92b8 11048 int pixelwidth, pixelheight;
06a2c219 11049#endif
dc6f92b8 11050
80fd1fe2 11051 BLOCK_INPUT;
aee9a898
RS
11052
11053#ifdef USE_X_TOOLKIT
3a20653d
RS
11054 {
11055 /* The x and y position of the widget is clobbered by the
11056 call to XtSetValues within EmacsFrameSetCharSize.
11057 This is a real kludge, but I don't understand Xt so I can't
11058 figure out a correct fix. Can anyone else tell me? -- rms. */
7556890b
RS
11059 int xpos = f->output_data.x->widget->core.x;
11060 int ypos = f->output_data.x->widget->core.y;
11061 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
11062 f->output_data.x->widget->core.x = xpos;
11063 f->output_data.x->widget->core.y = ypos;
3a20653d 11064 }
80fd1fe2
FP
11065
11066#else /* not USE_X_TOOLKIT */
11067
b1c884c3 11068 check_frame_size (f, &rows, &cols);
7556890b 11069 f->output_data.x->vertical_scroll_bar_extra
b2cad826
KH
11070 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
11071 ? 0
11072 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
480407eb 11073 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7556890b 11074 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
06a2c219 11075 f->output_data.x->flags_areas_extra
110859fc 11076 = FRAME_FLAGS_AREA_WIDTH (f);
f451eb13
JB
11077 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
11078 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
dc6f92b8 11079
7556890b 11080 f->output_data.x->win_gravity = NorthWestGravity;
c32cdd9a 11081 x_wm_set_size_hint (f, (long) 0, 0);
6ccf47d1 11082
334208b7
RS
11083 XSync (FRAME_X_DISPLAY (f), False);
11084 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11085 pixelwidth, pixelheight);
b1c884c3
JB
11086
11087 /* Now, strictly speaking, we can't be sure that this is accurate,
11088 but the window manager will get around to dealing with the size
11089 change request eventually, and we'll hear how it went when the
dbc4e1c1
JB
11090 ConfigureNotify event gets here.
11091
11092 We could just not bother storing any of this information here,
11093 and let the ConfigureNotify event set everything up, but that
fddd5ceb 11094 might be kind of confusing to the Lisp code, since size changes
dbc4e1c1 11095 wouldn't be reported in the frame parameters until some random
fddd5ceb
RS
11096 point in the future when the ConfigureNotify event arrives.
11097
11098 We pass 1 for DELAY since we can't run Lisp code inside of
11099 a BLOCK_INPUT. */
7d1e984f 11100 change_frame_size (f, rows, cols, 0, 1, 0);
b1c884c3
JB
11101 PIXEL_WIDTH (f) = pixelwidth;
11102 PIXEL_HEIGHT (f) = pixelheight;
11103
aee9a898
RS
11104 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11105 receive in the ConfigureNotify event; if we get what we asked
11106 for, then the event won't cause the screen to become garbaged, so
11107 we have to make sure to do it here. */
11108 SET_FRAME_GARBAGED (f);
11109
11110 XFlush (FRAME_X_DISPLAY (f));
11111
11112#endif /* not USE_X_TOOLKIT */
11113
4d73d038 11114 /* If cursor was outside the new size, mark it as off. */
06a2c219 11115 mark_window_cursors_off (XWINDOW (f->root_window));
4d73d038 11116
aee9a898
RS
11117 /* Clear out any recollection of where the mouse highlighting was,
11118 since it might be in a place that's outside the new frame size.
11119 Actually checking whether it is outside is a pain in the neck,
11120 so don't try--just let the highlighting be done afresh with new size. */
e687d06e 11121 cancel_mouse_face (f);
dbc4e1c1 11122
dc6f92b8
JB
11123 UNBLOCK_INPUT;
11124}
dc6f92b8 11125\f
d047c4eb 11126/* Mouse warping. */
dc6f92b8 11127
9b378208 11128void
f676886a
JB
11129x_set_mouse_position (f, x, y)
11130 struct frame *f;
dc6f92b8
JB
11131 int x, y;
11132{
11133 int pix_x, pix_y;
11134
7556890b
RS
11135 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
11136 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
f451eb13
JB
11137
11138 if (pix_x < 0) pix_x = 0;
11139 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
11140
11141 if (pix_y < 0) pix_y = 0;
11142 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
dc6f92b8
JB
11143
11144 BLOCK_INPUT;
dc6f92b8 11145
334208b7
RS
11146 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11147 0, 0, 0, 0, pix_x, pix_y);
dc6f92b8
JB
11148 UNBLOCK_INPUT;
11149}
11150
9b378208
RS
11151/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11152
11153void
11154x_set_mouse_pixel_position (f, pix_x, pix_y)
11155 struct frame *f;
11156 int pix_x, pix_y;
11157{
11158 BLOCK_INPUT;
11159
334208b7
RS
11160 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11161 0, 0, 0, 0, pix_x, pix_y);
9b378208
RS
11162 UNBLOCK_INPUT;
11163}
d047c4eb
KH
11164\f
11165/* focus shifting, raising and lowering. */
9b378208 11166
dfcf069d 11167void
f676886a
JB
11168x_focus_on_frame (f)
11169 struct frame *f;
dc6f92b8 11170{
1fb20991 11171#if 0 /* This proves to be unpleasant. */
f676886a 11172 x_raise_frame (f);
1fb20991 11173#endif
6d4238f3
JB
11174#if 0
11175 /* I don't think that the ICCCM allows programs to do things like this
11176 without the interaction of the window manager. Whatever you end up
f676886a 11177 doing with this code, do it to x_unfocus_frame too. */
334208b7 11178 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dc6f92b8 11179 RevertToPointerRoot, CurrentTime);
c118dd06 11180#endif /* ! 0 */
dc6f92b8
JB
11181}
11182
dfcf069d 11183void
f676886a
JB
11184x_unfocus_frame (f)
11185 struct frame *f;
dc6f92b8 11186{
6d4238f3 11187#if 0
f676886a 11188 /* Look at the remarks in x_focus_on_frame. */
0f941935 11189 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
334208b7 11190 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
dc6f92b8 11191 RevertToPointerRoot, CurrentTime);
c118dd06 11192#endif /* ! 0 */
dc6f92b8
JB
11193}
11194
f676886a 11195/* Raise frame F. */
dc6f92b8 11196
dfcf069d 11197void
f676886a
JB
11198x_raise_frame (f)
11199 struct frame *f;
dc6f92b8 11200{
3a88c238 11201 if (f->async_visible)
dc6f92b8
JB
11202 {
11203 BLOCK_INPUT;
3afe33e7 11204#ifdef USE_X_TOOLKIT
7556890b 11205 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 11206#else /* not USE_X_TOOLKIT */
334208b7 11207 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11208#endif /* not USE_X_TOOLKIT */
334208b7 11209 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
11210 UNBLOCK_INPUT;
11211 }
11212}
11213
f676886a 11214/* Lower frame F. */
dc6f92b8 11215
dfcf069d 11216void
f676886a
JB
11217x_lower_frame (f)
11218 struct frame *f;
dc6f92b8 11219{
3a88c238 11220 if (f->async_visible)
dc6f92b8
JB
11221 {
11222 BLOCK_INPUT;
3afe33e7 11223#ifdef USE_X_TOOLKIT
7556890b 11224 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 11225#else /* not USE_X_TOOLKIT */
334208b7 11226 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11227#endif /* not USE_X_TOOLKIT */
334208b7 11228 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
11229 UNBLOCK_INPUT;
11230 }
11231}
11232
dbc4e1c1 11233static void
6b0442dc 11234XTframe_raise_lower (f, raise_flag)
dbc4e1c1 11235 FRAME_PTR f;
6b0442dc 11236 int raise_flag;
dbc4e1c1 11237{
6b0442dc 11238 if (raise_flag)
dbc4e1c1
JB
11239 x_raise_frame (f);
11240 else
11241 x_lower_frame (f);
11242}
d047c4eb
KH
11243\f
11244/* Change of visibility. */
dc6f92b8 11245
9382638d
KH
11246/* This tries to wait until the frame is really visible.
11247 However, if the window manager asks the user where to position
11248 the frame, this will return before the user finishes doing that.
11249 The frame will not actually be visible at that time,
11250 but it will become visible later when the window manager
11251 finishes with it. */
11252
dfcf069d 11253void
f676886a
JB
11254x_make_frame_visible (f)
11255 struct frame *f;
dc6f92b8 11256{
990ba854 11257 Lisp_Object type;
1aa6072f 11258 int original_top, original_left;
dc6f92b8 11259
dc6f92b8 11260 BLOCK_INPUT;
dc6f92b8 11261
990ba854
RS
11262 type = x_icon_type (f);
11263 if (!NILP (type))
11264 x_bitmap_icon (f, type);
bdcd49ba 11265
f676886a 11266 if (! FRAME_VISIBLE_P (f))
90e65f07 11267 {
1aa6072f
RS
11268 /* We test FRAME_GARBAGED_P here to make sure we don't
11269 call x_set_offset a second time
11270 if we get to x_make_frame_visible a second time
11271 before the window gets really visible. */
11272 if (! FRAME_ICONIFIED_P (f)
11273 && ! f->output_data.x->asked_for_visible)
11274 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
11275
11276 f->output_data.x->asked_for_visible = 1;
11277
90e65f07 11278 if (! EQ (Vx_no_window_manager, Qt))
f676886a 11279 x_wm_set_window_state (f, NormalState);
3afe33e7 11280#ifdef USE_X_TOOLKIT
d7a38a2e 11281 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 11282 XtMapWidget (f->output_data.x->widget);
3afe33e7 11283#else /* not USE_X_TOOLKIT */
7f9c7f94 11284 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11285#endif /* not USE_X_TOOLKIT */
0134a210
RS
11286#if 0 /* This seems to bring back scroll bars in the wrong places
11287 if the window configuration has changed. They seem
11288 to come back ok without this. */
ab648270 11289 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
334208b7 11290 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
0134a210 11291#endif
90e65f07 11292 }
dc6f92b8 11293
334208b7 11294 XFlush (FRAME_X_DISPLAY (f));
90e65f07 11295
0dacf791
RS
11296 /* Synchronize to ensure Emacs knows the frame is visible
11297 before we do anything else. We do this loop with input not blocked
11298 so that incoming events are handled. */
11299 {
11300 Lisp_Object frame;
c0a04927 11301 int count = input_signal_count;
28c01ffe
RS
11302 /* This must be before UNBLOCK_INPUT
11303 since events that arrive in response to the actions above
11304 will set it when they are handled. */
11305 int previously_visible = f->output_data.x->has_been_visible;
1aa6072f
RS
11306
11307 original_left = f->output_data.x->left_pos;
11308 original_top = f->output_data.x->top_pos;
c0a04927
RS
11309
11310 /* This must come after we set COUNT. */
11311 UNBLOCK_INPUT;
11312
2745e6c4 11313 /* We unblock here so that arriving X events are processed. */
1aa6072f 11314
dcb07ae9
RS
11315 /* Now move the window back to where it was "supposed to be".
11316 But don't do it if the gravity is negative.
11317 When the gravity is negative, this uses a position
28c01ffe
RS
11318 that is 3 pixels too low. Perhaps that's really the border width.
11319
11320 Don't do this if the window has never been visible before,
11321 because the window manager may choose the position
11322 and we don't want to override it. */
1aa6072f 11323
4d3f5d9a 11324 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
06c488fd 11325 && f->output_data.x->win_gravity == NorthWestGravity
28c01ffe 11326 && previously_visible)
1aa6072f 11327 {
2745e6c4
RS
11328 Drawable rootw;
11329 int x, y;
11330 unsigned int width, height, border, depth;
06a2c219 11331
1aa6072f 11332 BLOCK_INPUT;
9829ddba 11333
06a2c219
GM
11334 /* On some window managers (such as FVWM) moving an existing
11335 window, even to the same place, causes the window manager
11336 to introduce an offset. This can cause the window to move
11337 to an unexpected location. Check the geometry (a little
11338 slow here) and then verify that the window is in the right
11339 place. If the window is not in the right place, move it
11340 there, and take the potential window manager hit. */
2745e6c4
RS
11341 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11342 &rootw, &x, &y, &width, &height, &border, &depth);
11343
11344 if (original_left != x || original_top != y)
11345 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11346 original_left, original_top);
11347
1aa6072f
RS
11348 UNBLOCK_INPUT;
11349 }
9829ddba 11350
e0c1aef2 11351 XSETFRAME (frame, f);
c0a04927
RS
11352
11353 while (1)
2a6cf806 11354 {
334208b7 11355 x_sync (f);
c0a04927
RS
11356 /* Once we have handled input events,
11357 we should have received the MapNotify if one is coming.
11358 So if we have not got it yet, stop looping.
11359 Some window managers make their own decisions
11360 about visibility. */
11361 if (input_signal_count != count)
11362 break;
c12a7cbd 11363 /* Machines that do polling rather than SIGIO have been observed
23cf7c60
KH
11364 to go into a busy-wait here. So we'll fake an alarm signal
11365 to let the handler know that there's something to be read.
11366 We used to raise a real alarm, but it seems that the handler
11367 isn't always enabled here. This is probably a bug. */
8b2f8d4e 11368 if (input_polling_used ())
3b2fa4e6
RS
11369 {
11370 /* It could be confusing if a real alarm arrives while processing
11371 the fake one. Turn it off and let the handler reset it. */
11372 alarm (0);
5e8e68ce 11373 input_poll_signal (0);
3b2fa4e6 11374 }
c0a04927
RS
11375 /* Once we have handled input events,
11376 we should have received the MapNotify if one is coming.
11377 So if we have not got it yet, stop looping.
11378 Some window managers make their own decisions
11379 about visibility. */
11380 if (input_signal_count != count)
11381 break;
2a6cf806 11382 }
0dacf791
RS
11383 FRAME_SAMPLE_VISIBILITY (f);
11384 }
dc6f92b8
JB
11385}
11386
06a2c219 11387/* Change from mapped state to withdrawn state. */
dc6f92b8 11388
d047c4eb
KH
11389/* Make the frame visible (mapped and not iconified). */
11390
dfcf069d 11391void
f676886a
JB
11392x_make_frame_invisible (f)
11393 struct frame *f;
dc6f92b8 11394{
546e6d5b
RS
11395 Window window;
11396
11397#ifdef USE_X_TOOLKIT
11398 /* Use the frame's outermost window, not the one we normally draw on. */
7556890b 11399 window = XtWindow (f->output_data.x->widget);
546e6d5b
RS
11400#else /* not USE_X_TOOLKIT */
11401 window = FRAME_X_WINDOW (f);
11402#endif /* not USE_X_TOOLKIT */
dc6f92b8 11403
9319ae23 11404 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
11405 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11406 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 11407
5627c40e 11408#if 0/* This might add unreliability; I don't trust it -- rms. */
9319ae23 11409 if (! f->async_visible && ! f->async_iconified)
dc6f92b8 11410 return;
5627c40e 11411#endif
dc6f92b8
JB
11412
11413 BLOCK_INPUT;
c118dd06 11414
af31d76f
RS
11415 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
11416 that the current position of the window is user-specified, rather than
11417 program-specified, so that when the window is mapped again, it will be
11418 placed at the same location, without forcing the user to position it
11419 by hand again (they have already done that once for this window.) */
c32cdd9a 11420 x_wm_set_size_hint (f, (long) 0, 1);
af31d76f 11421
c118dd06
JB
11422#ifdef HAVE_X11R4
11423
334208b7
RS
11424 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
11425 DefaultScreen (FRAME_X_DISPLAY (f))))
c118dd06
JB
11426 {
11427 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11428 error ("Can't notify window manager of window withdrawal");
c118dd06 11429 }
c118dd06 11430#else /* ! defined (HAVE_X11R4) */
16bd92ea 11431
c118dd06 11432 /* Tell the window manager what we're going to do. */
dc6f92b8
JB
11433 if (! EQ (Vx_no_window_manager, Qt))
11434 {
16bd92ea 11435 XEvent unmap;
dc6f92b8 11436
16bd92ea 11437 unmap.xunmap.type = UnmapNotify;
546e6d5b 11438 unmap.xunmap.window = window;
334208b7 11439 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
16bd92ea 11440 unmap.xunmap.from_configure = False;
334208b7
RS
11441 if (! XSendEvent (FRAME_X_DISPLAY (f),
11442 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea 11443 False,
06a2c219 11444 SubstructureRedirectMaskSubstructureNotifyMask,
16bd92ea
JB
11445 &unmap))
11446 {
11447 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11448 error ("Can't notify window manager of withdrawal");
16bd92ea 11449 }
dc6f92b8
JB
11450 }
11451
16bd92ea 11452 /* Unmap the window ourselves. Cheeky! */
334208b7 11453 XUnmapWindow (FRAME_X_DISPLAY (f), window);
c118dd06 11454#endif /* ! defined (HAVE_X11R4) */
dc6f92b8 11455
5627c40e
RS
11456 /* We can't distinguish this from iconification
11457 just by the event that we get from the server.
11458 So we can't win using the usual strategy of letting
11459 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
11460 and synchronize with the server to make sure we agree. */
11461 f->visible = 0;
11462 FRAME_ICONIFIED_P (f) = 0;
11463 f->async_visible = 0;
11464 f->async_iconified = 0;
11465
334208b7 11466 x_sync (f);
5627c40e 11467
dc6f92b8
JB
11468 UNBLOCK_INPUT;
11469}
11470
06a2c219 11471/* Change window state from mapped to iconified. */
dc6f92b8 11472
dfcf069d 11473void
f676886a
JB
11474x_iconify_frame (f)
11475 struct frame *f;
dc6f92b8 11476{
3afe33e7 11477 int result;
990ba854 11478 Lisp_Object type;
dc6f92b8 11479
9319ae23 11480 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
11481 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11482 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 11483
3a88c238 11484 if (f->async_iconified)
dc6f92b8
JB
11485 return;
11486
3afe33e7 11487 BLOCK_INPUT;
546e6d5b 11488
9af3143a
RS
11489 FRAME_SAMPLE_VISIBILITY (f);
11490
990ba854
RS
11491 type = x_icon_type (f);
11492 if (!NILP (type))
11493 x_bitmap_icon (f, type);
bdcd49ba
RS
11494
11495#ifdef USE_X_TOOLKIT
11496
546e6d5b
RS
11497 if (! FRAME_VISIBLE_P (f))
11498 {
11499 if (! EQ (Vx_no_window_manager, Qt))
11500 x_wm_set_window_state (f, IconicState);
11501 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 11502 XtMapWidget (f->output_data.x->widget);
9cf30a30
RS
11503 /* The server won't give us any event to indicate
11504 that an invisible frame was changed to an icon,
11505 so we have to record it here. */
11506 f->iconified = 1;
1e6bc770 11507 f->visible = 1;
9cf30a30 11508 f->async_iconified = 1;
1e6bc770 11509 f->async_visible = 0;
546e6d5b
RS
11510 UNBLOCK_INPUT;
11511 return;
11512 }
11513
334208b7 11514 result = XIconifyWindow (FRAME_X_DISPLAY (f),
7556890b 11515 XtWindow (f->output_data.x->widget),
334208b7 11516 DefaultScreen (FRAME_X_DISPLAY (f)));
3afe33e7
RS
11517 UNBLOCK_INPUT;
11518
11519 if (!result)
546e6d5b 11520 error ("Can't notify window manager of iconification");
3afe33e7
RS
11521
11522 f->async_iconified = 1;
1e6bc770
RS
11523 f->async_visible = 0;
11524
8c002a25
KH
11525
11526 BLOCK_INPUT;
334208b7 11527 XFlush (FRAME_X_DISPLAY (f));
8c002a25 11528 UNBLOCK_INPUT;
3afe33e7
RS
11529#else /* not USE_X_TOOLKIT */
11530
fd13dbb2
RS
11531 /* Make sure the X server knows where the window should be positioned,
11532 in case the user deiconifies with the window manager. */
11533 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
7556890b 11534 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
fd13dbb2 11535
16bd92ea
JB
11536 /* Since we don't know which revision of X we're running, we'll use both
11537 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
11538
11539 /* X11R4: send a ClientMessage to the window manager using the
11540 WM_CHANGE_STATE type. */
11541 {
11542 XEvent message;
58769bee 11543
c118dd06 11544 message.xclient.window = FRAME_X_WINDOW (f);
16bd92ea 11545 message.xclient.type = ClientMessage;
334208b7 11546 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
16bd92ea
JB
11547 message.xclient.format = 32;
11548 message.xclient.data.l[0] = IconicState;
11549
334208b7
RS
11550 if (! XSendEvent (FRAME_X_DISPLAY (f),
11551 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea
JB
11552 False,
11553 SubstructureRedirectMask | SubstructureNotifyMask,
11554 &message))
dc6f92b8
JB
11555 {
11556 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11557 error ("Can't notify window manager of iconification");
dc6f92b8 11558 }
16bd92ea 11559 }
dc6f92b8 11560
58769bee 11561 /* X11R3: set the initial_state field of the window manager hints to
16bd92ea
JB
11562 IconicState. */
11563 x_wm_set_window_state (f, IconicState);
dc6f92b8 11564
a9c00105
RS
11565 if (!FRAME_VISIBLE_P (f))
11566 {
11567 /* If the frame was withdrawn, before, we must map it. */
7f9c7f94 11568 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
a9c00105
RS
11569 }
11570
3a88c238 11571 f->async_iconified = 1;
1e6bc770 11572 f->async_visible = 0;
dc6f92b8 11573
334208b7 11574 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 11575 UNBLOCK_INPUT;
8c002a25 11576#endif /* not USE_X_TOOLKIT */
dc6f92b8 11577}
d047c4eb 11578\f
c0ff3fab 11579/* Destroy the X window of frame F. */
dc6f92b8 11580
dfcf069d 11581void
c0ff3fab 11582x_destroy_window (f)
f676886a 11583 struct frame *f;
dc6f92b8 11584{
7f9c7f94
RS
11585 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11586
dc6f92b8 11587 BLOCK_INPUT;
c0ff3fab 11588
6186a4a0
RS
11589 /* If a display connection is dead, don't try sending more
11590 commands to the X server. */
11591 if (dpyinfo->display != 0)
11592 {
11593 if (f->output_data.x->icon_desc != 0)
11594 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
31f41daf
RS
11595#ifdef HAVE_X_I18N
11596 if (FRAME_XIM (f))
11597 {
11598 XDestroyIC (FRAME_XIC (f));
408be661
RS
11599#if ! defined (SOLARIS2) || defined (HAVE_X11R6)
11600 /* This line causes crashes on Solaris with Openwin,
11601 due to an apparent bug in XCloseIM.
11602 X11R6 seems not to have the bug. */
31f41daf 11603 XCloseIM (FRAME_XIM (f));
a9978dd8 11604#endif
31f41daf
RS
11605 }
11606#endif
6186a4a0 11607 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->window_desc);
3afe33e7 11608#ifdef USE_X_TOOLKIT
06a2c219
GM
11609 if (f->output_data.x->widget)
11610 XtDestroyWidget (f->output_data.x->widget);
6186a4a0 11611 free_frame_menubar (f);
3afe33e7
RS
11612#endif /* USE_X_TOOLKIT */
11613
6186a4a0
RS
11614 free_frame_faces (f);
11615 XFlush (FRAME_X_DISPLAY (f));
11616 }
dc6f92b8 11617
df89d8a4 11618 if (f->output_data.x->saved_menu_event)
06a2c219 11619 xfree (f->output_data.x->saved_menu_event);
0c49ce2f 11620
7556890b
RS
11621 xfree (f->output_data.x);
11622 f->output_data.x = 0;
0f941935
KH
11623 if (f == dpyinfo->x_focus_frame)
11624 dpyinfo->x_focus_frame = 0;
11625 if (f == dpyinfo->x_focus_event_frame)
11626 dpyinfo->x_focus_event_frame = 0;
11627 if (f == dpyinfo->x_highlight_frame)
11628 dpyinfo->x_highlight_frame = 0;
c0ff3fab 11629
7f9c7f94
RS
11630 dpyinfo->reference_count--;
11631
11632 if (f == dpyinfo->mouse_face_mouse_frame)
dc05a16b 11633 {
7f9c7f94
RS
11634 dpyinfo->mouse_face_beg_row
11635 = dpyinfo->mouse_face_beg_col = -1;
11636 dpyinfo->mouse_face_end_row
11637 = dpyinfo->mouse_face_end_col = -1;
11638 dpyinfo->mouse_face_window = Qnil;
21323706
RS
11639 dpyinfo->mouse_face_deferred_gc = 0;
11640 dpyinfo->mouse_face_mouse_frame = 0;
dc05a16b 11641 }
0134a210 11642
c0ff3fab 11643 UNBLOCK_INPUT;
dc6f92b8
JB
11644}
11645\f
f451eb13
JB
11646/* Setting window manager hints. */
11647
af31d76f
RS
11648/* Set the normal size hints for the window manager, for frame F.
11649 FLAGS is the flags word to use--or 0 meaning preserve the flags
11650 that the window now has.
11651 If USER_POSITION is nonzero, we set the USPosition
11652 flag (this is useful when FLAGS is 0). */
6dba1858 11653
dfcf069d 11654void
af31d76f 11655x_wm_set_size_hint (f, flags, user_position)
f676886a 11656 struct frame *f;
af31d76f
RS
11657 long flags;
11658 int user_position;
dc6f92b8
JB
11659{
11660 XSizeHints size_hints;
3afe33e7
RS
11661
11662#ifdef USE_X_TOOLKIT
7e4f2521
FP
11663 Arg al[2];
11664 int ac = 0;
11665 Dimension widget_width, widget_height;
7556890b 11666 Window window = XtWindow (f->output_data.x->widget);
3afe33e7 11667#else /* not USE_X_TOOLKIT */
c118dd06 11668 Window window = FRAME_X_WINDOW (f);
3afe33e7 11669#endif /* not USE_X_TOOLKIT */
dc6f92b8 11670
b72a58fd
RS
11671 /* Setting PMaxSize caused various problems. */
11672 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
dc6f92b8 11673
7556890b
RS
11674 size_hints.x = f->output_data.x->left_pos;
11675 size_hints.y = f->output_data.x->top_pos;
7553a6b7 11676
7e4f2521
FP
11677#ifdef USE_X_TOOLKIT
11678 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
11679 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
7556890b 11680 XtGetValues (f->output_data.x->widget, al, ac);
7e4f2521
FP
11681 size_hints.height = widget_height;
11682 size_hints.width = widget_width;
11683#else /* not USE_X_TOOLKIT */
f676886a
JB
11684 size_hints.height = PIXEL_HEIGHT (f);
11685 size_hints.width = PIXEL_WIDTH (f);
7e4f2521 11686#endif /* not USE_X_TOOLKIT */
7553a6b7 11687
7556890b
RS
11688 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
11689 size_hints.height_inc = f->output_data.x->line_height;
334208b7
RS
11690 size_hints.max_width
11691 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
11692 size_hints.max_height
11693 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
0134a210 11694
d067ea8b
KH
11695 /* Calculate the base and minimum sizes.
11696
11697 (When we use the X toolkit, we don't do it here.
11698 Instead we copy the values that the widgets are using, below.) */
11699#ifndef USE_X_TOOLKIT
b1c884c3 11700 {
b0342f17 11701 int base_width, base_height;
0134a210 11702 int min_rows = 0, min_cols = 0;
b0342f17 11703
f451eb13
JB
11704 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
11705 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
b0342f17 11706
0134a210 11707 check_frame_size (f, &min_rows, &min_cols);
b0342f17 11708
0134a210
RS
11709 /* The window manager uses the base width hints to calculate the
11710 current number of rows and columns in the frame while
11711 resizing; min_width and min_height aren't useful for this
11712 purpose, since they might not give the dimensions for a
11713 zero-row, zero-column frame.
58769bee 11714
0134a210
RS
11715 We use the base_width and base_height members if we have
11716 them; otherwise, we set the min_width and min_height members
11717 to the size for a zero x zero frame. */
b0342f17
JB
11718
11719#ifdef HAVE_X11R4
0134a210
RS
11720 size_hints.flags |= PBaseSize;
11721 size_hints.base_width = base_width;
11722 size_hints.base_height = base_height;
11723 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
11724 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
b0342f17 11725#else
0134a210
RS
11726 size_hints.min_width = base_width;
11727 size_hints.min_height = base_height;
b0342f17 11728#endif
b1c884c3 11729 }
dc6f92b8 11730
d067ea8b 11731 /* If we don't need the old flags, we don't need the old hint at all. */
af31d76f 11732 if (flags)
dc6f92b8 11733 {
d067ea8b
KH
11734 size_hints.flags |= flags;
11735 goto no_read;
11736 }
11737#endif /* not USE_X_TOOLKIT */
11738
11739 {
11740 XSizeHints hints; /* Sometimes I hate X Windows... */
11741 long supplied_return;
11742 int value;
af31d76f
RS
11743
11744#ifdef HAVE_X11R4
d067ea8b
KH
11745 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
11746 &supplied_return);
af31d76f 11747#else
d067ea8b 11748 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
af31d76f 11749#endif
58769bee 11750
d067ea8b
KH
11751#ifdef USE_X_TOOLKIT
11752 size_hints.base_height = hints.base_height;
11753 size_hints.base_width = hints.base_width;
11754 size_hints.min_height = hints.min_height;
11755 size_hints.min_width = hints.min_width;
11756#endif
11757
11758 if (flags)
11759 size_hints.flags |= flags;
11760 else
11761 {
11762 if (value == 0)
11763 hints.flags = 0;
11764 if (hints.flags & PSize)
11765 size_hints.flags |= PSize;
11766 if (hints.flags & PPosition)
11767 size_hints.flags |= PPosition;
11768 if (hints.flags & USPosition)
11769 size_hints.flags |= USPosition;
11770 if (hints.flags & USSize)
11771 size_hints.flags |= USSize;
11772 }
11773 }
11774
06a2c219 11775#ifndef USE_X_TOOLKIT
d067ea8b 11776 no_read:
06a2c219 11777#endif
0134a210 11778
af31d76f 11779#ifdef PWinGravity
7556890b 11780 size_hints.win_gravity = f->output_data.x->win_gravity;
af31d76f 11781 size_hints.flags |= PWinGravity;
dc05a16b 11782
af31d76f 11783 if (user_position)
6dba1858 11784 {
af31d76f
RS
11785 size_hints.flags &= ~ PPosition;
11786 size_hints.flags |= USPosition;
6dba1858 11787 }
2554751d 11788#endif /* PWinGravity */
6dba1858 11789
b0342f17 11790#ifdef HAVE_X11R4
334208b7 11791 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 11792#else
334208b7 11793 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 11794#endif
dc6f92b8
JB
11795}
11796
11797/* Used for IconicState or NormalState */
06a2c219 11798
dfcf069d 11799void
f676886a
JB
11800x_wm_set_window_state (f, state)
11801 struct frame *f;
dc6f92b8
JB
11802 int state;
11803{
3afe33e7 11804#ifdef USE_X_TOOLKIT
546e6d5b
RS
11805 Arg al[1];
11806
11807 XtSetArg (al[0], XtNinitialState, state);
7556890b 11808 XtSetValues (f->output_data.x->widget, al, 1);
3afe33e7 11809#else /* not USE_X_TOOLKIT */
c118dd06 11810 Window window = FRAME_X_WINDOW (f);
dc6f92b8 11811
7556890b
RS
11812 f->output_data.x->wm_hints.flags |= StateHint;
11813 f->output_data.x->wm_hints.initial_state = state;
b1c884c3 11814
7556890b 11815 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
546e6d5b 11816#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11817}
11818
dfcf069d 11819void
7f2ae036 11820x_wm_set_icon_pixmap (f, pixmap_id)
f676886a 11821 struct frame *f;
7f2ae036 11822 int pixmap_id;
dc6f92b8 11823{
d2bd6bc4
RS
11824 Pixmap icon_pixmap;
11825
06a2c219 11826#ifndef USE_X_TOOLKIT
c118dd06 11827 Window window = FRAME_X_WINDOW (f);
75231bad 11828#endif
dc6f92b8 11829
7f2ae036 11830 if (pixmap_id > 0)
dbc4e1c1 11831 {
d2bd6bc4 11832 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
7556890b 11833 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
dbc4e1c1
JB
11834 }
11835 else
68568555
RS
11836 {
11837 /* It seems there is no way to turn off use of an icon pixmap.
11838 The following line does it, only if no icon has yet been created,
11839 for some window managers. But with mwm it crashes.
11840 Some people say it should clear the IconPixmapHint bit in this case,
11841 but that doesn't work, and the X consortium said it isn't the
11842 right thing at all. Since there is no way to win,
11843 best to explicitly give up. */
11844#if 0
11845 f->output_data.x->wm_hints.icon_pixmap = None;
11846#else
11847 return;
11848#endif
11849 }
b1c884c3 11850
d2bd6bc4
RS
11851#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
11852
11853 {
11854 Arg al[1];
11855 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
11856 XtSetValues (f->output_data.x->widget, al, 1);
11857 }
11858
11859#else /* not USE_X_TOOLKIT */
11860
7556890b
RS
11861 f->output_data.x->wm_hints.flags |= IconPixmapHint;
11862 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
d2bd6bc4
RS
11863
11864#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11865}
11866
dfcf069d 11867void
f676886a
JB
11868x_wm_set_icon_position (f, icon_x, icon_y)
11869 struct frame *f;
dc6f92b8
JB
11870 int icon_x, icon_y;
11871{
75231bad 11872#ifdef USE_X_TOOLKIT
7556890b 11873 Window window = XtWindow (f->output_data.x->widget);
75231bad 11874#else
c118dd06 11875 Window window = FRAME_X_WINDOW (f);
75231bad 11876#endif
dc6f92b8 11877
7556890b
RS
11878 f->output_data.x->wm_hints.flags |= IconPositionHint;
11879 f->output_data.x->wm_hints.icon_x = icon_x;
11880 f->output_data.x->wm_hints.icon_y = icon_y;
b1c884c3 11881
7556890b 11882 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
dc6f92b8
JB
11883}
11884
11885\f
06a2c219
GM
11886/***********************************************************************
11887 Fonts
11888 ***********************************************************************/
dc43ef94
KH
11889
11890/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
06a2c219 11891
dc43ef94
KH
11892struct font_info *
11893x_get_font_info (f, font_idx)
11894 FRAME_PTR f;
11895 int font_idx;
11896{
11897 return (FRAME_X_FONT_TABLE (f) + font_idx);
11898}
11899
11900
11901/* Return a list of names of available fonts matching PATTERN on frame
11902 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
11903 to be listed. Frame F NULL means we have not yet created any
11904 frame on X, and consult the first display in x_display_list.
11905 MAXNAMES sets a limit on how many fonts to match. */
11906
11907Lisp_Object
11908x_list_fonts (f, pattern, size, maxnames)
11909 FRAME_PTR f;
11910 Lisp_Object pattern;
11911 int size;
11912 int maxnames;
11913{
06a2c219
GM
11914 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
11915 Lisp_Object tem, second_best;
dc43ef94 11916 Display *dpy = f != NULL ? FRAME_X_DISPLAY (f) : x_display_list->display;
09c6077f 11917 int try_XLoadQueryFont = 0;
53ca4657 11918 int count;
dc43ef94 11919
6b0efe73 11920 patterns = Fassoc (pattern, Valternate_fontname_alist);
2da424f1
KH
11921 if (NILP (patterns))
11922 patterns = Fcons (pattern, Qnil);
81ba44e5 11923
09c6077f
KH
11924 if (maxnames == 1 && !size)
11925 /* We can return any single font matching PATTERN. */
11926 try_XLoadQueryFont = 1;
9a32686f 11927
8e713be6 11928 for (; CONSP (patterns); patterns = XCDR (patterns))
dc43ef94 11929 {
dc43ef94
KH
11930 int num_fonts;
11931 char **names;
11932
8e713be6 11933 pattern = XCAR (patterns);
536f4067
RS
11934 /* See if we cached the result for this particular query.
11935 The cache is an alist of the form:
11936 (((PATTERN . MAXNAMES) (FONTNAME . WIDTH) ...) ...)
11937 */
8e713be6 11938 if (f && (tem = XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element),
b5210ea7
KH
11939 key = Fcons (pattern, make_number (maxnames)),
11940 !NILP (list = Fassoc (key, tem))))
11941 {
11942 list = Fcdr_safe (list);
11943 /* We have a cashed list. Don't have to get the list again. */
11944 goto label_cached;
11945 }
11946
11947 /* At first, put PATTERN in the cache. */
09c6077f 11948
dc43ef94 11949 BLOCK_INPUT;
17d85edc
KH
11950 count = x_catch_errors (dpy);
11951
09c6077f
KH
11952 if (try_XLoadQueryFont)
11953 {
11954 XFontStruct *font;
11955 unsigned long value;
11956
11957 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
17d85edc
KH
11958 if (x_had_errors_p (dpy))
11959 {
11960 /* This error is perhaps due to insufficient memory on X
11961 server. Let's just ignore it. */
11962 font = NULL;
11963 x_clear_errors (dpy);
11964 }
11965
09c6077f
KH
11966 if (font
11967 && XGetFontProperty (font, XA_FONT, &value))
11968 {
11969 char *name = (char *) XGetAtomName (dpy, (Atom) value);
11970 int len = strlen (name);
01c752b5 11971 char *tmp;
09c6077f 11972
6f6512e8
KH
11973 /* If DXPC (a Differential X Protocol Compressor)
11974 Ver.3.7 is running, XGetAtomName will return null
11975 string. We must avoid such a name. */
11976 if (len == 0)
11977 try_XLoadQueryFont = 0;
11978 else
11979 {
11980 num_fonts = 1;
11981 names = (char **) alloca (sizeof (char *));
11982 /* Some systems only allow alloca assigned to a
11983 simple var. */
11984 tmp = (char *) alloca (len + 1); names[0] = tmp;
11985 bcopy (name, names[0], len + 1);
11986 XFree (name);
11987 }
09c6077f
KH
11988 }
11989 else
11990 try_XLoadQueryFont = 0;
a083fd23
RS
11991
11992 if (font)
11993 XFreeFont (dpy, font);
09c6077f
KH
11994 }
11995
11996 if (!try_XLoadQueryFont)
17d85edc
KH
11997 {
11998 /* We try at least 10 fonts because XListFonts will return
11999 auto-scaled fonts at the head. */
12000 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
12001 &num_fonts);
12002 if (x_had_errors_p (dpy))
12003 {
12004 /* This error is perhaps due to insufficient memory on X
12005 server. Let's just ignore it. */
12006 names = NULL;
12007 x_clear_errors (dpy);
12008 }
12009 }
12010
12011 x_uncatch_errors (dpy, count);
dc43ef94
KH
12012 UNBLOCK_INPUT;
12013
12014 if (names)
12015 {
12016 int i;
dc43ef94
KH
12017
12018 /* Make a list of all the fonts we got back.
12019 Store that in the font cache for the display. */
12020 for (i = 0; i < num_fonts; i++)
12021 {
06a2c219 12022 int width = 0;
dc43ef94 12023 char *p = names[i];
06a2c219
GM
12024 int average_width = -1, dashes = 0;
12025
dc43ef94 12026 /* Count the number of dashes in NAMES[I]. If there are
b5210ea7
KH
12027 14 dashes, and the field value following 12th dash
12028 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
12029 is usually too ugly to be used for editing. Let's
12030 ignore it. */
dc43ef94
KH
12031 while (*p)
12032 if (*p++ == '-')
12033 {
12034 dashes++;
12035 if (dashes == 7) /* PIXEL_SIZE field */
12036 width = atoi (p);
12037 else if (dashes == 12) /* AVERAGE_WIDTH field */
12038 average_width = atoi (p);
12039 }
12040 if (dashes < 14 || average_width != 0)
12041 {
12042 tem = build_string (names[i]);
12043 if (NILP (Fassoc (tem, list)))
12044 {
12045 if (STRINGP (Vx_pixel_size_width_font_regexp)
f39db7ea
RS
12046 && ((fast_c_string_match_ignore_case
12047 (Vx_pixel_size_width_font_regexp, names[i]))
dc43ef94
KH
12048 >= 0))
12049 /* We can set the value of PIXEL_SIZE to the
b5210ea7 12050 width of this font. */
dc43ef94
KH
12051 list = Fcons (Fcons (tem, make_number (width)), list);
12052 else
12053 /* For the moment, width is not known. */
12054 list = Fcons (Fcons (tem, Qnil), list);
12055 }
12056 }
12057 }
09c6077f
KH
12058 if (!try_XLoadQueryFont)
12059 XFreeFontNames (names);
dc43ef94
KH
12060 }
12061
b5210ea7 12062 /* Now store the result in the cache. */
dc43ef94 12063 if (f != NULL)
8e713be6 12064 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element)
dc43ef94 12065 = Fcons (Fcons (key, list),
8e713be6 12066 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
dc43ef94 12067
b5210ea7
KH
12068 label_cached:
12069 if (NILP (list)) continue; /* Try the remaining alternatives. */
dc43ef94 12070
b5210ea7
KH
12071 newlist = second_best = Qnil;
12072 /* Make a list of the fonts that have the right width. */
8e713be6 12073 for (; CONSP (list); list = XCDR (list))
b5210ea7 12074 {
536f4067
RS
12075 int found_size;
12076
8e713be6 12077 tem = XCAR (list);
dc43ef94 12078
8e713be6 12079 if (!CONSP (tem) || NILP (XCAR (tem)))
b5210ea7
KH
12080 continue;
12081 if (!size)
12082 {
8e713be6 12083 newlist = Fcons (XCAR (tem), newlist);
b5210ea7
KH
12084 continue;
12085 }
dc43ef94 12086
8e713be6 12087 if (!INTEGERP (XCDR (tem)))
dc43ef94 12088 {
b5210ea7
KH
12089 /* Since we have not yet known the size of this font, we
12090 must try slow function call XLoadQueryFont. */
dc43ef94
KH
12091 XFontStruct *thisinfo;
12092
12093 BLOCK_INPUT;
17d85edc 12094 count = x_catch_errors (dpy);
dc43ef94 12095 thisinfo = XLoadQueryFont (dpy,
8e713be6 12096 XSTRING (XCAR (tem))->data);
17d85edc
KH
12097 if (x_had_errors_p (dpy))
12098 {
12099 /* This error is perhaps due to insufficient memory on X
12100 server. Let's just ignore it. */
12101 thisinfo = NULL;
12102 x_clear_errors (dpy);
12103 }
12104 x_uncatch_errors (dpy, count);
dc43ef94
KH
12105 UNBLOCK_INPUT;
12106
12107 if (thisinfo)
12108 {
8e713be6 12109 XCDR (tem)
536f4067
RS
12110 = (thisinfo->min_bounds.width == 0
12111 ? make_number (0)
12112 : make_number (thisinfo->max_bounds.width));
dc43ef94
KH
12113 XFreeFont (dpy, thisinfo);
12114 }
12115 else
b5210ea7 12116 /* For unknown reason, the previous call of XListFont had
06a2c219 12117 returned a font which can't be opened. Record the size
b5210ea7 12118 as 0 not to try to open it again. */
8e713be6 12119 XCDR (tem) = make_number (0);
dc43ef94 12120 }
536f4067 12121
8e713be6 12122 found_size = XINT (XCDR (tem));
536f4067 12123 if (found_size == size)
8e713be6 12124 newlist = Fcons (XCAR (tem), newlist);
536f4067 12125 else if (found_size > 0)
b5210ea7 12126 {
536f4067 12127 if (NILP (second_best))
b5210ea7 12128 second_best = tem;
536f4067
RS
12129 else if (found_size < size)
12130 {
8e713be6
KR
12131 if (XINT (XCDR (second_best)) > size
12132 || XINT (XCDR (second_best)) < found_size)
536f4067
RS
12133 second_best = tem;
12134 }
12135 else
12136 {
8e713be6
KR
12137 if (XINT (XCDR (second_best)) > size
12138 && XINT (XCDR (second_best)) > found_size)
536f4067
RS
12139 second_best = tem;
12140 }
b5210ea7
KH
12141 }
12142 }
12143 if (!NILP (newlist))
12144 break;
12145 else if (!NILP (second_best))
12146 {
8e713be6 12147 newlist = Fcons (XCAR (second_best), Qnil);
b5210ea7 12148 break;
dc43ef94 12149 }
dc43ef94
KH
12150 }
12151
12152 return newlist;
12153}
12154
06a2c219
GM
12155
12156#if GLYPH_DEBUG
12157
12158/* Check that FONT is valid on frame F. It is if it can be found in F's
12159 font table. */
12160
12161static void
12162x_check_font (f, font)
12163 struct frame *f;
12164 XFontStruct *font;
12165{
12166 int i;
12167 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12168
12169 xassert (font != NULL);
12170
12171 for (i = 0; i < dpyinfo->n_fonts; i++)
12172 if (dpyinfo->font_table[i].name
12173 && font == dpyinfo->font_table[i].font)
12174 break;
12175
12176 xassert (i < dpyinfo->n_fonts);
12177}
12178
12179#endif /* GLYPH_DEBUG != 0 */
12180
12181/* Set *W to the minimum width, *H to the minimum font height of FONT.
12182 Note: There are (broken) X fonts out there with invalid XFontStruct
12183 min_bounds contents. For example, handa@etl.go.jp reports that
12184 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
12185 have font->min_bounds.width == 0. */
12186
12187static INLINE void
12188x_font_min_bounds (font, w, h)
12189 XFontStruct *font;
12190 int *w, *h;
12191{
12192 *h = FONT_HEIGHT (font);
12193 *w = font->min_bounds.width;
12194
12195 /* Try to handle the case where FONT->min_bounds has invalid
12196 contents. Since the only font known to have invalid min_bounds
12197 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
12198 if (*w <= 0)
12199 *w = font->max_bounds.width;
12200}
12201
12202
12203/* Compute the smallest character width and smallest font height over
12204 all fonts available on frame F. Set the members smallest_char_width
12205 and smallest_font_height in F's x_display_info structure to
12206 the values computed. Value is non-zero if smallest_font_height or
12207 smallest_char_width become smaller than they were before. */
12208
12209static int
12210x_compute_min_glyph_bounds (f)
12211 struct frame *f;
12212{
12213 int i;
12214 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12215 XFontStruct *font;
12216 int old_width = dpyinfo->smallest_char_width;
12217 int old_height = dpyinfo->smallest_font_height;
12218
12219 dpyinfo->smallest_font_height = 100000;
12220 dpyinfo->smallest_char_width = 100000;
12221
12222 for (i = 0; i < dpyinfo->n_fonts; ++i)
12223 if (dpyinfo->font_table[i].name)
12224 {
12225 struct font_info *fontp = dpyinfo->font_table + i;
12226 int w, h;
12227
12228 font = (XFontStruct *) fontp->font;
12229 xassert (font != (XFontStruct *) ~0);
12230 x_font_min_bounds (font, &w, &h);
12231
12232 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
12233 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
12234 }
12235
12236 xassert (dpyinfo->smallest_char_width > 0
12237 && dpyinfo->smallest_font_height > 0);
12238
12239 return (dpyinfo->n_fonts == 1
12240 || dpyinfo->smallest_char_width < old_width
12241 || dpyinfo->smallest_font_height < old_height);
12242}
12243
12244
dc43ef94
KH
12245/* Load font named FONTNAME of the size SIZE for frame F, and return a
12246 pointer to the structure font_info while allocating it dynamically.
12247 If SIZE is 0, load any size of font.
12248 If loading is failed, return NULL. */
12249
12250struct font_info *
12251x_load_font (f, fontname, size)
12252 struct frame *f;
12253 register char *fontname;
12254 int size;
12255{
12256 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12257 Lisp_Object font_names;
d645aaa4 12258 int count;
dc43ef94
KH
12259
12260 /* Get a list of all the fonts that match this name. Once we
12261 have a list of matching fonts, we compare them against the fonts
12262 we already have by comparing names. */
09c6077f 12263 font_names = x_list_fonts (f, build_string (fontname), size, 1);
dc43ef94
KH
12264
12265 if (!NILP (font_names))
12266 {
12267 Lisp_Object tail;
12268 int i;
12269
12270 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 12271 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
06a2c219
GM
12272 if (dpyinfo->font_table[i].name
12273 && (!strcmp (dpyinfo->font_table[i].name,
8e713be6 12274 XSTRING (XCAR (tail))->data)
06a2c219 12275 || !strcmp (dpyinfo->font_table[i].full_name,
8e713be6 12276 XSTRING (XCAR (tail))->data)))
dc43ef94
KH
12277 return (dpyinfo->font_table + i);
12278 }
12279
12280 /* Load the font and add it to the table. */
12281 {
12282 char *full_name;
12283 XFontStruct *font;
12284 struct font_info *fontp;
12285 unsigned long value;
06a2c219 12286 int i;
dc43ef94 12287
2da424f1
KH
12288 /* If we have found fonts by x_list_font, load one of them. If
12289 not, we still try to load a font by the name given as FONTNAME
12290 because XListFonts (called in x_list_font) of some X server has
12291 a bug of not finding a font even if the font surely exists and
12292 is loadable by XLoadQueryFont. */
e1d6d5b9 12293 if (size > 0 && !NILP (font_names))
8e713be6 12294 fontname = (char *) XSTRING (XCAR (font_names))->data;
dc43ef94
KH
12295
12296 BLOCK_INPUT;
d645aaa4 12297 count = x_catch_errors (FRAME_X_DISPLAY (f));
dc43ef94 12298 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
d645aaa4
KH
12299 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12300 {
12301 /* This error is perhaps due to insufficient memory on X
12302 server. Let's just ignore it. */
12303 font = NULL;
12304 x_clear_errors (FRAME_X_DISPLAY (f));
12305 }
12306 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
dc43ef94 12307 UNBLOCK_INPUT;
b5210ea7 12308 if (!font)
dc43ef94
KH
12309 return NULL;
12310
06a2c219
GM
12311 /* Find a free slot in the font table. */
12312 for (i = 0; i < dpyinfo->n_fonts; ++i)
12313 if (dpyinfo->font_table[i].name == NULL)
12314 break;
12315
12316 /* If no free slot found, maybe enlarge the font table. */
12317 if (i == dpyinfo->n_fonts
12318 && dpyinfo->n_fonts == dpyinfo->font_table_size)
dc43ef94 12319 {
06a2c219
GM
12320 int sz;
12321 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
12322 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
dc43ef94 12323 dpyinfo->font_table
06a2c219 12324 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
dc43ef94
KH
12325 }
12326
06a2c219
GM
12327 fontp = dpyinfo->font_table + i;
12328 if (i == dpyinfo->n_fonts)
12329 ++dpyinfo->n_fonts;
dc43ef94
KH
12330
12331 /* Now fill in the slots of *FONTP. */
12332 BLOCK_INPUT;
12333 fontp->font = font;
06a2c219 12334 fontp->font_idx = i;
dc43ef94
KH
12335 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
12336 bcopy (fontname, fontp->name, strlen (fontname) + 1);
12337
12338 /* Try to get the full name of FONT. Put it in FULL_NAME. */
12339 full_name = 0;
12340 if (XGetFontProperty (font, XA_FONT, &value))
12341 {
12342 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
12343 char *p = name;
12344 int dashes = 0;
12345
12346 /* Count the number of dashes in the "full name".
12347 If it is too few, this isn't really the font's full name,
12348 so don't use it.
12349 In X11R4, the fonts did not come with their canonical names
12350 stored in them. */
12351 while (*p)
12352 {
12353 if (*p == '-')
12354 dashes++;
12355 p++;
12356 }
12357
12358 if (dashes >= 13)
12359 {
12360 full_name = (char *) xmalloc (p - name + 1);
12361 bcopy (name, full_name, p - name + 1);
12362 }
12363
12364 XFree (name);
12365 }
12366
12367 if (full_name != 0)
12368 fontp->full_name = full_name;
12369 else
12370 fontp->full_name = fontp->name;
12371
12372 fontp->size = font->max_bounds.width;
d5749adb
KH
12373 fontp->height = FONT_HEIGHT (font);
12374 {
12375 /* For some font, ascent and descent in max_bounds field is
12376 larger than the above value. */
12377 int max_height = font->max_bounds.ascent + font->max_bounds.descent;
12378 if (max_height > fontp->height)
74848a96 12379 fontp->height = max_height;
d5749adb 12380 }
dc43ef94 12381
2da424f1
KH
12382 if (NILP (font_names))
12383 {
12384 /* We come here because of a bug of XListFonts mentioned at
12385 the head of this block. Let's store this information in
12386 the cache for x_list_fonts. */
12387 Lisp_Object lispy_name = build_string (fontname);
12388 Lisp_Object lispy_full_name = build_string (fontp->full_name);
12389
8e713be6 12390 XCDR (dpyinfo->name_list_element)
2da424f1
KH
12391 = Fcons (Fcons (Fcons (lispy_name, make_number (256)),
12392 Fcons (Fcons (lispy_full_name,
12393 make_number (fontp->size)),
12394 Qnil)),
8e713be6 12395 XCDR (dpyinfo->name_list_element));
2da424f1 12396 if (full_name)
8e713be6 12397 XCDR (dpyinfo->name_list_element)
2da424f1
KH
12398 = Fcons (Fcons (Fcons (lispy_full_name, make_number (256)),
12399 Fcons (Fcons (lispy_full_name,
12400 make_number (fontp->size)),
12401 Qnil)),
8e713be6 12402 XCDR (dpyinfo->name_list_element));
2da424f1
KH
12403 }
12404
dc43ef94
KH
12405 /* The slot `encoding' specifies how to map a character
12406 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
ce667c3e 12407 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF, 0:0x2020..0x7F7F,
8ff102bd
RS
12408 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF,
12409 0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF, or
12410 2:0xA020..0xFF7F). For the moment, we don't know which charset
06a2c219 12411 uses this font. So, we set information in fontp->encoding[1]
8ff102bd
RS
12412 which is never used by any charset. If mapping can't be
12413 decided, set FONT_ENCODING_NOT_DECIDED. */
dc43ef94
KH
12414 fontp->encoding[1]
12415 = (font->max_byte1 == 0
12416 /* 1-byte font */
12417 ? (font->min_char_or_byte2 < 0x80
12418 ? (font->max_char_or_byte2 < 0x80
12419 ? 0 /* 0x20..0x7F */
8ff102bd 12420 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
dc43ef94
KH
12421 : 1) /* 0xA0..0xFF */
12422 /* 2-byte font */
12423 : (font->min_byte1 < 0x80
12424 ? (font->max_byte1 < 0x80
12425 ? (font->min_char_or_byte2 < 0x80
12426 ? (font->max_char_or_byte2 < 0x80
12427 ? 0 /* 0x2020..0x7F7F */
8ff102bd 12428 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
dc43ef94 12429 : 3) /* 0x20A0..0x7FFF */
8ff102bd 12430 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
dc43ef94
KH
12431 : (font->min_char_or_byte2 < 0x80
12432 ? (font->max_char_or_byte2 < 0x80
12433 ? 2 /* 0xA020..0xFF7F */
8ff102bd 12434 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
dc43ef94
KH
12435 : 1))); /* 0xA0A0..0xFFFF */
12436
12437 fontp->baseline_offset
12438 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
12439 ? (long) value : 0);
12440 fontp->relative_compose
12441 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
12442 ? (long) value : 0);
f78798df
KH
12443 fontp->default_ascent
12444 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
12445 ? (long) value : 0);
dc43ef94 12446
06a2c219
GM
12447 /* Set global flag fonts_changed_p to non-zero if the font loaded
12448 has a character with a smaller width than any other character
12449 before, or if the font loaded has a smalle>r height than any
12450 other font loaded before. If this happens, it will make a
12451 glyph matrix reallocation necessary. */
12452 fonts_changed_p = x_compute_min_glyph_bounds (f);
dc43ef94 12453 UNBLOCK_INPUT;
dc43ef94
KH
12454 return fontp;
12455 }
12456}
12457
06a2c219
GM
12458
12459/* Return a pointer to struct font_info of a font named FONTNAME for
12460 frame F. If no such font is loaded, return NULL. */
12461
dc43ef94
KH
12462struct font_info *
12463x_query_font (f, fontname)
12464 struct frame *f;
12465 register char *fontname;
12466{
12467 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12468 int i;
12469
12470 for (i = 0; i < dpyinfo->n_fonts; i++)
06a2c219
GM
12471 if (dpyinfo->font_table[i].name
12472 && (!strcmp (dpyinfo->font_table[i].name, fontname)
12473 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
dc43ef94
KH
12474 return (dpyinfo->font_table + i);
12475 return NULL;
12476}
12477
06a2c219
GM
12478
12479/* Find a CCL program for a font specified by FONTP, and set the member
a6582676
KH
12480 `encoder' of the structure. */
12481
12482void
12483x_find_ccl_program (fontp)
12484 struct font_info *fontp;
12485{
a42f54e6 12486 Lisp_Object list, elt;
a6582676 12487
8e713be6 12488 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
a6582676 12489 {
8e713be6 12490 elt = XCAR (list);
a6582676 12491 if (CONSP (elt)
8e713be6
KR
12492 && STRINGP (XCAR (elt))
12493 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
a6582676 12494 >= 0))
a42f54e6
KH
12495 break;
12496 }
12497 if (! NILP (list))
12498 {
d27f8ca7
KH
12499 struct ccl_program *ccl
12500 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
a42f54e6 12501
8e713be6 12502 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
a42f54e6
KH
12503 xfree (ccl);
12504 else
12505 fontp->font_encoder = ccl;
a6582676
KH
12506 }
12507}
12508
06a2c219 12509
dc43ef94 12510\f
06a2c219
GM
12511/***********************************************************************
12512 Initialization
12513 ***********************************************************************/
f451eb13 12514
3afe33e7
RS
12515#ifdef USE_X_TOOLKIT
12516static XrmOptionDescRec emacs_options[] = {
12517 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
12518 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
12519
12520 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
12521 XrmoptionSepArg, NULL},
12522 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
12523
12524 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12525 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12526 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12527 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12528 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12529 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
12530 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
12531};
12532#endif /* USE_X_TOOLKIT */
12533
7a13e894
RS
12534static int x_initialized;
12535
29b38361
KH
12536#ifdef MULTI_KBOARD
12537/* Test whether two display-name strings agree up to the dot that separates
12538 the screen number from the server number. */
12539static int
12540same_x_server (name1, name2)
12541 char *name1, *name2;
12542{
12543 int seen_colon = 0;
cf591cc1
RS
12544 unsigned char *system_name = XSTRING (Vsystem_name)->data;
12545 int system_name_length = strlen (system_name);
12546 int length_until_period = 0;
12547
12548 while (system_name[length_until_period] != 0
12549 && system_name[length_until_period] != '.')
12550 length_until_period++;
12551
12552 /* Treat `unix' like an empty host name. */
12553 if (! strncmp (name1, "unix:", 5))
12554 name1 += 4;
12555 if (! strncmp (name2, "unix:", 5))
12556 name2 += 4;
12557 /* Treat this host's name like an empty host name. */
12558 if (! strncmp (name1, system_name, system_name_length)
12559 && name1[system_name_length] == ':')
12560 name1 += system_name_length;
12561 if (! strncmp (name2, system_name, system_name_length)
12562 && name2[system_name_length] == ':')
12563 name2 += system_name_length;
12564 /* Treat this host's domainless name like an empty host name. */
12565 if (! strncmp (name1, system_name, length_until_period)
12566 && name1[length_until_period] == ':')
12567 name1 += length_until_period;
12568 if (! strncmp (name2, system_name, length_until_period)
12569 && name2[length_until_period] == ':')
12570 name2 += length_until_period;
12571
29b38361
KH
12572 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
12573 {
12574 if (*name1 == ':')
12575 seen_colon++;
12576 if (seen_colon && *name1 == '.')
12577 return 1;
12578 }
12579 return (seen_colon
12580 && (*name1 == '.' || *name1 == '\0')
12581 && (*name2 == '.' || *name2 == '\0'));
12582}
12583#endif
12584
334208b7 12585struct x_display_info *
1f8255f2 12586x_term_init (display_name, xrm_option, resource_name)
334208b7 12587 Lisp_Object display_name;
1f8255f2
RS
12588 char *xrm_option;
12589 char *resource_name;
dc6f92b8 12590{
334208b7 12591 int connection;
7a13e894 12592 Display *dpy;
334208b7
RS
12593 struct x_display_info *dpyinfo;
12594 XrmDatabase xrdb;
12595
60439948
KH
12596 BLOCK_INPUT;
12597
7a13e894
RS
12598 if (!x_initialized)
12599 {
12600 x_initialize ();
12601 x_initialized = 1;
12602 }
dc6f92b8 12603
3afe33e7 12604#ifdef USE_X_TOOLKIT
2d7fc7e8
RS
12605 /* weiner@footloose.sps.mot.com reports that this causes
12606 errors with X11R5:
12607 X protocol error: BadAtom (invalid Atom parameter)
12608 on protocol request 18skiloaf.
12609 So let's not use it until R6. */
12610#ifdef HAVE_X11XTR6
bdcd49ba
RS
12611 XtSetLanguageProc (NULL, NULL, NULL);
12612#endif
12613
7f9c7f94
RS
12614 {
12615 int argc = 0;
12616 char *argv[3];
12617
12618 argv[0] = "";
12619 argc = 1;
12620 if (xrm_option)
12621 {
12622 argv[argc++] = "-xrm";
12623 argv[argc++] = xrm_option;
12624 }
12625 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
12626 resource_name, EMACS_CLASS,
12627 emacs_options, XtNumber (emacs_options),
12628 &argc, argv);
39d8bb4d
KH
12629
12630#ifdef HAVE_X11XTR6
10537cb1 12631 /* I think this is to compensate for XtSetLanguageProc. */
71f8198a 12632 fixup_locale ();
39d8bb4d 12633#endif
7f9c7f94 12634 }
3afe33e7
RS
12635
12636#else /* not USE_X_TOOLKIT */
bdcd49ba
RS
12637#ifdef HAVE_X11R5
12638 XSetLocaleModifiers ("");
12639#endif
7a13e894 12640 dpy = XOpenDisplay (XSTRING (display_name)->data);
3afe33e7 12641#endif /* not USE_X_TOOLKIT */
334208b7 12642
7a13e894
RS
12643 /* Detect failure. */
12644 if (dpy == 0)
60439948
KH
12645 {
12646 UNBLOCK_INPUT;
12647 return 0;
12648 }
7a13e894
RS
12649
12650 /* We have definitely succeeded. Record the new connection. */
12651
12652 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
12653
29b38361
KH
12654#ifdef MULTI_KBOARD
12655 {
12656 struct x_display_info *share;
12657 Lisp_Object tail;
12658
12659 for (share = x_display_list, tail = x_display_name_list; share;
8e713be6
KR
12660 share = share->next, tail = XCDR (tail))
12661 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
29b38361
KH
12662 XSTRING (display_name)->data))
12663 break;
12664 if (share)
12665 dpyinfo->kboard = share->kboard;
12666 else
12667 {
12668 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
12669 init_kboard (dpyinfo->kboard);
59e755be
KH
12670 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
12671 {
12672 char *vendor = ServerVendor (dpy);
12673 dpyinfo->kboard->Vsystem_key_alist
12674 = call1 (Qvendor_specific_keysyms,
12675 build_string (vendor ? vendor : ""));
12676 }
12677
29b38361
KH
12678 dpyinfo->kboard->next_kboard = all_kboards;
12679 all_kboards = dpyinfo->kboard;
0ad5446c
KH
12680 /* Don't let the initial kboard remain current longer than necessary.
12681 That would cause problems if a file loaded on startup tries to
06a2c219 12682 prompt in the mini-buffer. */
0ad5446c
KH
12683 if (current_kboard == initial_kboard)
12684 current_kboard = dpyinfo->kboard;
29b38361
KH
12685 }
12686 dpyinfo->kboard->reference_count++;
12687 }
b9737ad3
KH
12688#endif
12689
7a13e894
RS
12690 /* Put this display on the chain. */
12691 dpyinfo->next = x_display_list;
12692 x_display_list = dpyinfo;
12693
12694 /* Put it on x_display_name_list as well, to keep them parallel. */
12695 x_display_name_list = Fcons (Fcons (display_name, Qnil),
12696 x_display_name_list);
8e713be6 12697 dpyinfo->name_list_element = XCAR (x_display_name_list);
7a13e894
RS
12698
12699 dpyinfo->display = dpy;
dc6f92b8 12700
dc6f92b8 12701#if 0
7a13e894 12702 XSetAfterFunction (x_current_display, x_trace_wire);
c118dd06 12703#endif /* ! 0 */
7a13e894
RS
12704
12705 dpyinfo->x_id_name
fc932ac6
RS
12706 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
12707 + STRING_BYTES (XSTRING (Vsystem_name))
7a13e894
RS
12708 + 2);
12709 sprintf (dpyinfo->x_id_name, "%s@%s",
12710 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
28430d3c
JB
12711
12712 /* Figure out which modifier bits mean what. */
334208b7 12713 x_find_modifier_meanings (dpyinfo);
f451eb13 12714
ab648270 12715 /* Get the scroll bar cursor. */
7a13e894 12716 dpyinfo->vertical_scroll_bar_cursor
334208b7 12717 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
f451eb13 12718
334208b7
RS
12719 xrdb = x_load_resources (dpyinfo->display, xrm_option,
12720 resource_name, EMACS_CLASS);
12721#ifdef HAVE_XRMSETDATABASE
12722 XrmSetDatabase (dpyinfo->display, xrdb);
12723#else
12724 dpyinfo->display->db = xrdb;
12725#endif
547d9db8 12726 /* Put the rdb where we can find it in a way that works on
7a13e894
RS
12727 all versions. */
12728 dpyinfo->xrdb = xrdb;
334208b7
RS
12729
12730 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
12731 DefaultScreen (dpyinfo->display));
12732 dpyinfo->visual = select_visual (dpyinfo->display, dpyinfo->screen,
12733 &dpyinfo->n_planes);
12734 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
12735 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
12736 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
12737 dpyinfo->grabbed = 0;
12738 dpyinfo->reference_count = 0;
12739 dpyinfo->icon_bitmap_id = -1;
06a2c219 12740 dpyinfo->font_table = NULL;
7a13e894
RS
12741 dpyinfo->n_fonts = 0;
12742 dpyinfo->font_table_size = 0;
12743 dpyinfo->bitmaps = 0;
12744 dpyinfo->bitmaps_size = 0;
12745 dpyinfo->bitmaps_last = 0;
12746 dpyinfo->scratch_cursor_gc = 0;
12747 dpyinfo->mouse_face_mouse_frame = 0;
12748 dpyinfo->mouse_face_deferred_gc = 0;
12749 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
12750 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
06a2c219 12751 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
7a13e894
RS
12752 dpyinfo->mouse_face_window = Qnil;
12753 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
12754 dpyinfo->mouse_face_defer = 0;
0f941935
KH
12755 dpyinfo->x_focus_frame = 0;
12756 dpyinfo->x_focus_event_frame = 0;
12757 dpyinfo->x_highlight_frame = 0;
06a2c219 12758 dpyinfo->image_cache = make_image_cache ();
334208b7 12759
06a2c219
GM
12760 {
12761 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
12762 double pixels = DisplayHeight (dpyinfo->display, screen_number);
12763 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
12764 dpyinfo->resy = pixels * 25.4 / mm;
12765 pixels = DisplayWidth (dpyinfo->display, screen_number);
12766 mm = DisplayWidthMM (dpyinfo->display, screen_number);
12767 dpyinfo->resx = pixels * 25.4 / mm;
12768 }
12769
334208b7
RS
12770 dpyinfo->Xatom_wm_protocols
12771 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
12772 dpyinfo->Xatom_wm_take_focus
12773 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
12774 dpyinfo->Xatom_wm_save_yourself
12775 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
12776 dpyinfo->Xatom_wm_delete_window
12777 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
12778 dpyinfo->Xatom_wm_change_state
12779 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
12780 dpyinfo->Xatom_wm_configure_denied
12781 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
12782 dpyinfo->Xatom_wm_window_moved
12783 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
12784 dpyinfo->Xatom_editres
12785 = XInternAtom (dpyinfo->display, "Editres", False);
12786 dpyinfo->Xatom_CLIPBOARD
12787 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
12788 dpyinfo->Xatom_TIMESTAMP
12789 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
12790 dpyinfo->Xatom_TEXT
12791 = XInternAtom (dpyinfo->display, "TEXT", False);
dc43ef94
KH
12792 dpyinfo->Xatom_COMPOUND_TEXT
12793 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
334208b7
RS
12794 dpyinfo->Xatom_DELETE
12795 = XInternAtom (dpyinfo->display, "DELETE", False);
12796 dpyinfo->Xatom_MULTIPLE
12797 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
12798 dpyinfo->Xatom_INCR
12799 = XInternAtom (dpyinfo->display, "INCR", False);
12800 dpyinfo->Xatom_EMACS_TMP
12801 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
12802 dpyinfo->Xatom_TARGETS
12803 = XInternAtom (dpyinfo->display, "TARGETS", False);
12804 dpyinfo->Xatom_NULL
12805 = XInternAtom (dpyinfo->display, "NULL", False);
12806 dpyinfo->Xatom_ATOM_PAIR
12807 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
dc43ef94
KH
12808 /* For properties of font. */
12809 dpyinfo->Xatom_PIXEL_SIZE
12810 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
12811 dpyinfo->Xatom_MULE_BASELINE_OFFSET
12812 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
12813 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
12814 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
f78798df
KH
12815 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
12816 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
334208b7 12817
06a2c219
GM
12818 /* Ghostscript support. */
12819 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
12820 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
12821
12822 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
12823 False);
12824
547d9db8
KH
12825 dpyinfo->cut_buffers_initialized = 0;
12826
334208b7
RS
12827 connection = ConnectionNumber (dpyinfo->display);
12828 dpyinfo->connection = connection;
12829
dc43ef94 12830 {
5d7cc324
RS
12831 char null_bits[1];
12832
12833 null_bits[0] = 0x00;
dc43ef94
KH
12834
12835 dpyinfo->null_pixel
12836 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
12837 null_bits, 1, 1, (long) 0, (long) 0,
12838 1);
12839 }
12840
06a2c219
GM
12841 {
12842 extern int gray_bitmap_width, gray_bitmap_height;
12843 extern unsigned char *gray_bitmap_bits;
12844 dpyinfo->gray
12845 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
12846 gray_bitmap_bits,
12847 gray_bitmap_width, gray_bitmap_height,
12848 (unsigned long) 1, (unsigned long) 0, 1);
12849 }
12850
87485d6f
MW
12851#ifdef subprocesses
12852 /* This is only needed for distinguishing keyboard and process input. */
334208b7 12853 if (connection != 0)
7a13e894 12854 add_keyboard_wait_descriptor (connection);
87485d6f 12855#endif
6d4238f3 12856
041b69ac 12857#ifndef F_SETOWN_BUG
dc6f92b8 12858#ifdef F_SETOWN
dc6f92b8 12859#ifdef F_SETOWN_SOCK_NEG
61c3ce62 12860 /* stdin is a socket here */
334208b7 12861 fcntl (connection, F_SETOWN, -getpid ());
c118dd06 12862#else /* ! defined (F_SETOWN_SOCK_NEG) */
334208b7 12863 fcntl (connection, F_SETOWN, getpid ());
c118dd06
JB
12864#endif /* ! defined (F_SETOWN_SOCK_NEG) */
12865#endif /* ! defined (F_SETOWN) */
041b69ac 12866#endif /* F_SETOWN_BUG */
dc6f92b8
JB
12867
12868#ifdef SIGIO
eee20f6a
KH
12869 if (interrupt_input)
12870 init_sigio (connection);
c118dd06 12871#endif /* ! defined (SIGIO) */
dc6f92b8 12872
51b592fb 12873#ifdef USE_LUCID
f8c39f51 12874#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
51b592fb
RS
12875 /* Make sure that we have a valid font for dialog boxes
12876 so that Xt does not crash. */
12877 {
12878 Display *dpy = dpyinfo->display;
12879 XrmValue d, fr, to;
12880 Font font;
e99db5a1 12881 int count;
51b592fb
RS
12882
12883 d.addr = (XPointer)&dpy;
12884 d.size = sizeof (Display *);
12885 fr.addr = XtDefaultFont;
12886 fr.size = sizeof (XtDefaultFont);
12887 to.size = sizeof (Font *);
12888 to.addr = (XPointer)&font;
e99db5a1 12889 count = x_catch_errors (dpy);
51b592fb
RS
12890 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
12891 abort ();
12892 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
12893 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
e99db5a1 12894 x_uncatch_errors (dpy, count);
51b592fb
RS
12895 }
12896#endif
f8c39f51 12897#endif
51b592fb 12898
60439948
KH
12899 UNBLOCK_INPUT;
12900
7a13e894
RS
12901 return dpyinfo;
12902}
12903\f
12904/* Get rid of display DPYINFO, assuming all frames are already gone,
12905 and without sending any more commands to the X server. */
dc6f92b8 12906
7a13e894
RS
12907void
12908x_delete_display (dpyinfo)
12909 struct x_display_info *dpyinfo;
12910{
12911 delete_keyboard_wait_descriptor (dpyinfo->connection);
12912
12913 /* Discard this display from x_display_name_list and x_display_list.
12914 We can't use Fdelq because that can quit. */
12915 if (! NILP (x_display_name_list)
8e713be6
KR
12916 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
12917 x_display_name_list = XCDR (x_display_name_list);
7a13e894
RS
12918 else
12919 {
12920 Lisp_Object tail;
12921
12922 tail = x_display_name_list;
8e713be6 12923 while (CONSP (tail) && CONSP (XCDR (tail)))
7a13e894 12924 {
8e713be6 12925 if (EQ (XCAR (XCDR (tail)),
7a13e894
RS
12926 dpyinfo->name_list_element))
12927 {
8e713be6 12928 XCDR (tail) = XCDR (XCDR (tail));
7a13e894
RS
12929 break;
12930 }
8e713be6 12931 tail = XCDR (tail);
7a13e894
RS
12932 }
12933 }
12934
12935 if (x_display_list == dpyinfo)
12936 x_display_list = dpyinfo->next;
7f9c7f94
RS
12937 else
12938 {
12939 struct x_display_info *tail;
7a13e894 12940
7f9c7f94
RS
12941 for (tail = x_display_list; tail; tail = tail->next)
12942 if (tail->next == dpyinfo)
12943 tail->next = tail->next->next;
12944 }
7a13e894 12945
0d777288
RS
12946#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
12947#ifndef AIX /* On AIX, XCloseDisplay calls this. */
7f9c7f94
RS
12948 XrmDestroyDatabase (dpyinfo->xrdb);
12949#endif
0d777288 12950#endif
29b38361
KH
12951#ifdef MULTI_KBOARD
12952 if (--dpyinfo->kboard->reference_count == 0)
39f79001 12953 delete_kboard (dpyinfo->kboard);
b9737ad3
KH
12954#endif
12955 xfree (dpyinfo->font_table);
12956 xfree (dpyinfo->x_id_name);
12957 xfree (dpyinfo);
7a13e894
RS
12958}
12959\f
12960/* Set up use of X before we make the first connection. */
12961
06a2c219
GM
12962static struct redisplay_interface x_redisplay_interface =
12963{
12964 x_produce_glyphs,
12965 x_write_glyphs,
12966 x_insert_glyphs,
12967 x_clear_end_of_line,
12968 x_scroll_run,
12969 x_after_update_window_line,
12970 x_update_window_begin,
12971 x_update_window_end,
12972 XTcursor_to,
12973 x_flush,
66ac4b0e
GM
12974 x_get_glyph_overhangs,
12975 x_fix_overlapping_area
06a2c219
GM
12976};
12977
dfcf069d 12978void
7a13e894
RS
12979x_initialize ()
12980{
06a2c219
GM
12981 rif = &x_redisplay_interface;
12982
12983 clear_frame_hook = x_clear_frame;
12984 ins_del_lines_hook = x_ins_del_lines;
12985 change_line_highlight_hook = x_change_line_highlight;
12986 delete_glyphs_hook = x_delete_glyphs;
dc6f92b8
JB
12987 ring_bell_hook = XTring_bell;
12988 reset_terminal_modes_hook = XTreset_terminal_modes;
12989 set_terminal_modes_hook = XTset_terminal_modes;
06a2c219
GM
12990 update_begin_hook = x_update_begin;
12991 update_end_hook = x_update_end;
dc6f92b8
JB
12992 set_terminal_window_hook = XTset_terminal_window;
12993 read_socket_hook = XTread_socket;
b8009dd1 12994 frame_up_to_date_hook = XTframe_up_to_date;
dc6f92b8 12995 reassert_line_highlight_hook = XTreassert_line_highlight;
90e65f07 12996 mouse_position_hook = XTmouse_position;
f451eb13 12997 frame_rehighlight_hook = XTframe_rehighlight;
dbc4e1c1 12998 frame_raise_lower_hook = XTframe_raise_lower;
ab648270
JB
12999 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13000 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13001 redeem_scroll_bar_hook = XTredeem_scroll_bar;
13002 judge_scroll_bars_hook = XTjudge_scroll_bars;
06a2c219 13003 estimate_mode_line_height_hook = x_estimate_mode_line_height;
58769bee 13004
f676886a 13005 scroll_region_ok = 1; /* we'll scroll partial frames */
dc6f92b8
JB
13006 char_ins_del_ok = 0; /* just as fast to write the line */
13007 line_ins_del_ok = 1; /* we'll just blt 'em */
13008 fast_clear_end_of_line = 1; /* X does this well */
58769bee 13009 memory_below_frame = 0; /* we don't remember what scrolls
dc6f92b8
JB
13010 off the bottom */
13011 baud_rate = 19200;
13012
7a13e894 13013 x_noop_count = 0;
9ea173e8 13014 last_tool_bar_item = -1;
06a2c219
GM
13015 any_help_event_p = 0;
13016
b30b24cb
RS
13017 /* Try to use interrupt input; if we can't, then start polling. */
13018 Fset_input_mode (Qt, Qnil, Qt, Qnil);
13019
7f9c7f94
RS
13020#ifdef USE_X_TOOLKIT
13021 XtToolkitInitialize ();
13022 Xt_app_con = XtCreateApplicationContext ();
665881ad 13023 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
db74249b
SM
13024#endif
13025#if USE_TOOLKIT_SCROLL_BARS
ec18280f
SM
13026 xaw3d_arrow_scroll = False;
13027 xaw3d_pick_top = True;
7f9c7f94
RS
13028#endif
13029
58769bee 13030 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 13031 original error handler. */
e99db5a1 13032 XSetErrorHandler (x_error_handler);
334208b7 13033 XSetIOErrorHandler (x_io_error_quitter);
dc6f92b8 13034
06a2c219 13035 /* Disable Window Change signals; they are handled by X events. */
dc6f92b8
JB
13036#ifdef SIGWINCH
13037 signal (SIGWINCH, SIG_DFL);
c118dd06 13038#endif /* ! defined (SIGWINCH) */
dc6f92b8 13039
92e2441b 13040 signal (SIGPIPE, x_connection_signal);
dc6f92b8 13041}
55123275 13042
06a2c219 13043
55123275
JB
13044void
13045syms_of_xterm ()
13046{
e99db5a1
RS
13047 staticpro (&x_error_message_string);
13048 x_error_message_string = Qnil;
13049
7a13e894
RS
13050 staticpro (&x_display_name_list);
13051 x_display_name_list = Qnil;
334208b7 13052
ab648270 13053 staticpro (&last_mouse_scroll_bar);
e53cb100 13054 last_mouse_scroll_bar = Qnil;
59e755be
KH
13055
13056 staticpro (&Qvendor_specific_keysyms);
13057 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
2237cac9
RS
13058
13059 staticpro (&last_mouse_press_frame);
13060 last_mouse_press_frame = Qnil;
06a2c219
GM
13061
13062 staticpro (&help_echo);
13063 help_echo = Qnil;
13064 staticpro (&previous_help_echo);
13065 previous_help_echo = Qnil;
13066
13067 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
13068 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
13069For example, if a block cursor is over a tab, it will be drawn as\n\
13070wide as that tab on the display.");
13071 x_stretch_cursor_p = 0;
13072
13073 DEFVAR_BOOL ("x-toolkit-scroll-bars-p", &x_toolkit_scroll_bars_p,
13074 "If not nil, Emacs uses toolkit scroll bars.");
13075#if USE_TOOLKIT_SCROLL_BARS
13076 x_toolkit_scroll_bars_p = 1;
13077#else
13078 x_toolkit_scroll_bars_p = 0;
13079#endif
13080
13081 defsubr (&Sxt_process_timeouts);
13082 staticpro (&last_mouse_motion_frame);
13083 last_mouse_motion_frame = Qnil;
55123275 13084}
6cf0ae86
RS
13085
13086#endif /* not HAVE_X_WINDOWS */
06a2c219 13087