(comint-output-filter): Doc fixes & misc code cleanup.
[bpt/emacs.git] / src / xterm.c
CommitLineData
dc6f92b8 1/* X Communication module for terminals which understand the X protocol.
1c7e22fd 2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000
06a2c219 3 Free Software Foundation, Inc.
dc6f92b8
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
4746118a 9the Free Software Foundation; either version 2, or (at your option)
dc6f92b8
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
dc6f92b8 21
06a2c219 22/* New display code by Gerd Moellmann <gerd@gnu.org>. */
3afe33e7
RS
23/* Xt features made by Fred Pierresteguy. */
24
68c45bf0
PE
25#include <config.h>
26
039440c4 27/* On 4.3 these lose if they come after xterm.h. */
039440c4 28/* Putting these at the beginning seems to be standard for other .c files. */
039440c4
RS
29#include <signal.h>
30
4846819e
RS
31#include <stdio.h>
32
dc6f92b8
JB
33#ifdef HAVE_X_WINDOWS
34
35#include "lisp.h"
9ac0d9e0 36#include "blockinput.h"
dc6f92b8 37
ae79c227
AS
38/* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40#include "syssignal.h"
41
dc6f92b8
JB
42/* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44#include "xterm.h"
f451eb13 45#include <X11/cursorfont.h>
dc6f92b8
JB
46
47/* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49#ifndef makedev
50#include <sys/types.h>
c118dd06 51#endif /* makedev */
dc6f92b8 52
6df54671 53#ifdef BSD_SYSTEM
dc6f92b8 54#include <sys/ioctl.h>
6df54671 55#endif /* ! defined (BSD_SYSTEM) */
dc6f92b8 56
2d368234 57#include "systty.h"
3a2712f9 58#include "systime.h"
dc6f92b8 59
b8009dd1 60#ifndef INCLUDED_FCNTL
dc6f92b8 61#include <fcntl.h>
b8009dd1 62#endif
dc6f92b8
JB
63#include <ctype.h>
64#include <errno.h>
65#include <setjmp.h>
66#include <sys/stat.h>
a0a7635f
RS
67/* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
68/* #include <sys/param.h> */
dc6f92b8 69
dc43ef94
KH
70#include "charset.h"
71#include "ccl.h"
7a13e894 72#include "frame.h"
dc6f92b8 73#include "dispextern.h"
ee569018 74#include "fontset.h"
dc6f92b8
JB
75#include "termhooks.h"
76#include "termopts.h"
77#include "termchar.h"
78#if 0
79#include "sink.h"
80#include "sinkmask.h"
c118dd06 81#endif /* ! 0 */
dc6f92b8 82#include "gnu.h"
dc6f92b8 83#include "disptab.h"
dc6f92b8 84#include "buffer.h"
f451eb13 85#include "window.h"
3b2fa4e6 86#include "keyboard.h"
bde7c500 87#include "intervals.h"
dfcf069d 88#include "process.h"
bffcfca9 89#include "atimer.h"
dc6f92b8 90
d2bd6bc4
RS
91#ifdef USE_X_TOOLKIT
92#include <X11/Shell.h>
93#endif
94
06a2c219
GM
95#ifdef HAVE_SYS_TIME_H
96#include <sys/time.h>
97#endif
98#ifdef HAVE_UNISTD_H
99#include <unistd.h>
100#endif
101
3afe33e7 102#ifdef USE_X_TOOLKIT
06a2c219 103
952291d9
GM
104extern void free_frame_menubar P_ ((struct frame *));
105extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
106 int));
06a2c219 107
0fdff6bb
RS
108#if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
109#define HACK_EDITRES
110extern void _XEditResCheckMessages ();
111#endif /* not NO_EDITRES */
06a2c219
GM
112
113/* Include toolkit specific headers for the scroll bar widget. */
114
115#ifdef USE_TOOLKIT_SCROLL_BARS
116#if defined USE_MOTIF
117#include <Xm/Xm.h> /* for LESSTIF_VERSION */
118#include <Xm/ScrollBar.h>
119#include <Xm/ScrollBarP.h>
ec18280f
SM
120#else /* !USE_MOTIF i.e. use Xaw */
121
122#ifdef HAVE_XAW3D
06a2c219 123#include <X11/Xaw3d/Simple.h>
06a2c219
GM
124#include <X11/Xaw3d/Scrollbar.h>
125#define ARROW_SCROLLBAR
126#include <X11/Xaw3d/ScrollbarP.h>
ec18280f
SM
127#else /* !HAVE_XAW3D */
128#include <X11/Xaw/Simple.h>
129#include <X11/Xaw/Scrollbar.h>
130#endif /* !HAVE_XAW3D */
131#ifndef XtNpickTop
132#define XtNpickTop "pickTop"
133#endif /* !XtNpickTop */
134#endif /* !USE_MOTIF */
06a2c219
GM
135#endif /* USE_TOOLKIT_SCROLL_BARS */
136
3afe33e7
RS
137#endif /* USE_X_TOOLKIT */
138
b849c413
RS
139#ifndef USE_X_TOOLKIT
140#define x_any_window_to_frame x_window_to_frame
5627c40e 141#define x_top_window_to_frame x_window_to_frame
b849c413
RS
142#endif
143
546e6d5b 144#ifdef USE_X_TOOLKIT
d067ea8b 145#include "widget.h"
546e6d5b
RS
146#ifndef XtNinitialState
147#define XtNinitialState "initialState"
148#endif
149#endif
150
80528801
KH
151#ifdef SOLARIS2
152/* memmove will be defined as a macro in Xfuncs.h unless
153 <string.h> is included beforehand. The declaration for memmove in
154 <string.h> will cause a syntax error when Xfuncs.h later includes it. */
155#include <string.h>
156#endif
157
e4b68333 158#ifndef min
06a2c219 159#define min(a,b) ((a) < (b) ? (a) : (b))
e4b68333
RS
160#endif
161#ifndef max
06a2c219
GM
162#define max(a,b) ((a) > (b) ? (a) : (b))
163#endif
164
165#define abs(x) ((x) < 0 ? -(x) : (x))
166
167#define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
168
169\f
170/* Bitmaps for truncated lines. */
171
172enum bitmap_type
173{
174 NO_BITMAP,
175 LEFT_TRUNCATION_BITMAP,
176 RIGHT_TRUNCATION_BITMAP,
177 OVERLAY_ARROW_BITMAP,
178 CONTINUED_LINE_BITMAP,
179 CONTINUATION_LINE_BITMAP,
180 ZV_LINE_BITMAP
181};
182
183/* Bitmap drawn to indicate lines not displaying text if
184 `indicate-empty-lines' is non-nil. */
185
186#define zv_width 8
187#define zv_height 8
188static unsigned char zv_bits[] = {
189 0x00, 0x00, 0x1e, 0x1e, 0x1e, 0x1e, 0x00, 0x00};
190
191/* An arrow like this: `<-'. */
192
193#define left_width 8
194#define left_height 8
195static unsigned char left_bits[] = {
196 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
197
110859fc
GM
198/* Right truncation arrow bitmap `->'. */
199
200#define right_width 8
201#define right_height 8
202static unsigned char right_bits[] = {
203 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
204
06a2c219
GM
205/* Marker for continued lines. */
206
207#define continued_width 8
208#define continued_height 8
209static unsigned char continued_bits[] = {
110859fc
GM
210 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
211
212/* Marker for continuation lines. */
06a2c219
GM
213
214#define continuation_width 8
215#define continuation_height 8
216static unsigned char continuation_bits[] = {
110859fc 217 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
06a2c219 218
110859fc 219/* Overlay arrow bitmap. */
06a2c219 220
110859fc
GM
221#if 0
222/* A bomb. */
06a2c219
GM
223#define ov_width 8
224#define ov_height 8
225static unsigned char ov_bits[] = {
226 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
06a2c219 227#else
110859fc 228/* A triangular arrow. */
06a2c219
GM
229#define ov_width 8
230#define ov_height 8
231static unsigned char ov_bits[] = {
110859fc
GM
232 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
233
e4b68333 234#endif
06a2c219
GM
235
236extern Lisp_Object Qhelp_echo;
237
69388238 238\f
06a2c219
GM
239/* Non-zero means Emacs uses toolkit scroll bars. */
240
241int x_toolkit_scroll_bars_p;
242
243/* If a string, XTread_socket generates an event to display that string.
244 (The display is done in read_char.) */
245
246static Lisp_Object help_echo;
7cea38bc 247static Lisp_Object help_echo_window;
be010514
GM
248static Lisp_Object help_echo_object;
249static int help_echo_pos;
06a2c219
GM
250
251/* Temporary variable for XTread_socket. */
252
253static Lisp_Object previous_help_echo;
254
255/* Non-zero means that a HELP_EVENT has been generated since Emacs
256 start. */
257
258static int any_help_event_p;
259
260/* Non-zero means draw block and hollow cursor as wide as the glyph
261 under it. For example, if a block cursor is over a tab, it will be
262 drawn as wide as that tab on the display. */
263
264int x_stretch_cursor_p;
265
266/* This is a chain of structures for all the X displays currently in
267 use. */
268
334208b7 269struct x_display_info *x_display_list;
dc6f92b8 270
06a2c219
GM
271/* This is a list of cons cells, each of the form (NAME
272 . FONT-LIST-CACHE), one for each element of x_display_list and in
273 the same order. NAME is the name of the frame. FONT-LIST-CACHE
274 records previous values returned by x-list-fonts. */
275
7a13e894 276Lisp_Object x_display_name_list;
f451eb13 277
987d2ad1 278/* Frame being updated by update_frame. This is declared in term.c.
06a2c219
GM
279 This is set by update_begin and looked at by all the XT functions.
280 It is zero while not inside an update. In that case, the XT
281 functions assume that `selected_frame' is the frame to apply to. */
282
d0386f2a 283extern struct frame *updating_frame;
dc6f92b8 284
dfcf069d 285extern int waiting_for_input;
0e81d8cd 286
06a2c219
GM
287/* This is a frame waiting to be auto-raised, within XTread_socket. */
288
0134a210
RS
289struct frame *pending_autoraise_frame;
290
7f9c7f94
RS
291#ifdef USE_X_TOOLKIT
292/* The application context for Xt use. */
293XtAppContext Xt_app_con;
06a2c219
GM
294static String Xt_default_resources[] = {0};
295#endif /* USE_X_TOOLKIT */
665881ad 296
06a2c219
GM
297/* Nominal cursor position -- where to draw output.
298 HPOS and VPOS are window relative glyph matrix coordinates.
299 X and Y are window relative pixel coordinates. */
dc6f92b8 300
06a2c219 301struct cursor_pos output_cursor;
dc6f92b8 302
bffcfca9
GM
303/* Non-zero means user is interacting with a toolkit scroll bar. */
304
305static int toolkit_scroll_bar_interaction;
dc6f92b8 306
69388238
RS
307/* Mouse movement.
308
06a2c219 309 Formerly, we used PointerMotionHintMask (in standard_event_mask)
f5bb65ec
RS
310 so that we would have to call XQueryPointer after each MotionNotify
311 event to ask for another such event. However, this made mouse tracking
312 slow, and there was a bug that made it eventually stop.
313
314 Simply asking for MotionNotify all the time seems to work better.
315
69388238
RS
316 In order to avoid asking for motion events and then throwing most
317 of them away or busy-polling the server for mouse positions, we ask
318 the server for pointer motion hints. This means that we get only
319 one event per group of mouse movements. "Groups" are delimited by
320 other kinds of events (focus changes and button clicks, for
321 example), or by XQueryPointer calls; when one of these happens, we
322 get another MotionNotify event the next time the mouse moves. This
323 is at least as efficient as getting motion events when mouse
324 tracking is on, and I suspect only negligibly worse when tracking
f5bb65ec 325 is off. */
69388238
RS
326
327/* Where the mouse was last time we reported a mouse event. */
69388238 328
06a2c219
GM
329FRAME_PTR last_mouse_frame;
330static XRectangle last_mouse_glyph;
2237cac9
RS
331static Lisp_Object last_mouse_press_frame;
332
69388238
RS
333/* The scroll bar in which the last X motion event occurred.
334
06a2c219
GM
335 If the last X motion event occurred in a scroll bar, we set this so
336 XTmouse_position can know whether to report a scroll bar motion or
69388238
RS
337 an ordinary motion.
338
06a2c219
GM
339 If the last X motion event didn't occur in a scroll bar, we set
340 this to Qnil, to tell XTmouse_position to return an ordinary motion
341 event. */
342
69388238
RS
343static Lisp_Object last_mouse_scroll_bar;
344
69388238
RS
345/* This is a hack. We would really prefer that XTmouse_position would
346 return the time associated with the position it returns, but there
06a2c219 347 doesn't seem to be any way to wrest the time-stamp from the server
69388238
RS
348 along with the position query. So, we just keep track of the time
349 of the last movement we received, and return that in hopes that
350 it's somewhat accurate. */
06a2c219 351
69388238
RS
352static Time last_mouse_movement_time;
353
06a2c219
GM
354/* Incremented by XTread_socket whenever it really tries to read
355 events. */
356
c0a04927
RS
357#ifdef __STDC__
358static int volatile input_signal_count;
359#else
360static int input_signal_count;
361#endif
362
7a13e894 363/* Used locally within XTread_socket. */
06a2c219 364
7a13e894 365static int x_noop_count;
dc6f92b8 366
7a13e894 367/* Initial values of argv and argc. */
06a2c219 368
7a13e894
RS
369extern char **initial_argv;
370extern int initial_argc;
dc6f92b8 371
7a13e894 372extern Lisp_Object Vcommand_line_args, Vsystem_name;
dc6f92b8 373
06a2c219 374/* Tells if a window manager is present or not. */
7a13e894
RS
375
376extern Lisp_Object Vx_no_window_manager;
dc6f92b8 377
c2df547c 378extern Lisp_Object Qface, Qmouse_face;
b8009dd1 379
dc6f92b8
JB
380extern int errno;
381
dfeccd2d 382/* A mask of extra modifier bits to put into every keyboard char. */
06a2c219 383
64bb1782
RS
384extern int extra_keyboard_modifiers;
385
59e755be
KH
386static Lisp_Object Qvendor_specific_keysyms;
387
952291d9
GM
388extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
389extern Lisp_Object x_icon_type P_ ((struct frame *));
c32cdd9a 390
7a13e894 391
06a2c219
GM
392/* Enumeration for overriding/changing the face to use for drawing
393 glyphs in x_draw_glyphs. */
394
395enum draw_glyphs_face
396{
397 DRAW_NORMAL_TEXT,
398 DRAW_INVERSE_VIDEO,
399 DRAW_CURSOR,
400 DRAW_MOUSE_FACE,
401 DRAW_IMAGE_RAISED,
402 DRAW_IMAGE_SUNKEN
403};
404
71b8321e 405static void x_update_window_end P_ ((struct window *, int, int));
06a2c219
GM
406static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
407void x_delete_display P_ ((struct x_display_info *));
408static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
409 unsigned));
410static int fast_find_position P_ ((struct window *, int, int *, int *,
411 int *, int *));
412static void set_output_cursor P_ ((struct cursor_pos *));
413static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
414 int *, int *, int *));
415static void note_mode_line_highlight P_ ((struct window *, int, int));
06a2c219 416static void note_mouse_highlight P_ ((struct frame *, int, int));
9ea173e8
GM
417static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
418static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
06a2c219
GM
419static void show_mouse_face P_ ((struct x_display_info *,
420 enum draw_glyphs_face));
421static int x_io_error_quitter P_ ((Display *));
422int x_catch_errors P_ ((Display *));
423void x_uncatch_errors P_ ((Display *, int));
424void x_lower_frame P_ ((struct frame *));
425void x_scroll_bar_clear P_ ((struct frame *));
426int x_had_errors_p P_ ((Display *));
427void x_wm_set_size_hint P_ ((struct frame *, long, int));
428void x_raise_frame P_ ((struct frame *));
429void x_set_window_size P_ ((struct frame *, int, int, int));
430void x_wm_set_window_state P_ ((struct frame *, int));
431void x_wm_set_icon_pixmap P_ ((struct frame *, int));
432void x_initialize P_ ((void));
433static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
434static int x_compute_min_glyph_bounds P_ ((struct frame *));
435static void x_draw_phys_cursor_glyph P_ ((struct window *,
436 struct glyph_row *,
437 enum draw_glyphs_face));
438static void x_update_end P_ ((struct frame *));
439static void XTframe_up_to_date P_ ((struct frame *));
440static void XTreassert_line_highlight P_ ((int, int));
441static void x_change_line_highlight P_ ((int, int, int, int));
442static void XTset_terminal_modes P_ ((void));
443static void XTreset_terminal_modes P_ ((void));
444static void XTcursor_to P_ ((int, int, int, int));
445static void x_write_glyphs P_ ((struct glyph *, int));
446static void x_clear_end_of_line P_ ((int));
447static void x_clear_frame P_ ((void));
448static void x_clear_cursor P_ ((struct window *));
449static void frame_highlight P_ ((struct frame *));
450static void frame_unhighlight P_ ((struct frame *));
451static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
452static void XTframe_rehighlight P_ ((struct frame *));
453static void x_frame_rehighlight P_ ((struct x_display_info *));
454static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
f02d8aa0 455static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
06a2c219
GM
456static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
457 XRectangle *));
458static void expose_frame P_ ((struct frame *, int, int, int, int));
459static void expose_window_tree P_ ((struct window *, XRectangle *));
460static void expose_window P_ ((struct window *, XRectangle *));
461static void expose_area P_ ((struct window *, struct glyph_row *,
462 XRectangle *, enum glyph_row_area));
463static void expose_line P_ ((struct window *, struct glyph_row *,
464 XRectangle *));
465static void x_update_cursor_in_window_tree P_ ((struct window *, int));
466static void x_update_window_cursor P_ ((struct window *, int));
467static void x_erase_phys_cursor P_ ((struct window *));
468void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
469static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
470 enum bitmap_type));
471
472static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
473 GC, int));
474static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
475static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
476static void note_overwritten_text_cursor P_ ((struct window *, int, int));
477static void x_flush P_ ((struct frame *f));
952291d9
GM
478static void x_update_begin P_ ((struct frame *));
479static void x_update_window_begin P_ ((struct window *));
480static void x_draw_vertical_border P_ ((struct window *));
481static void x_after_update_window_line P_ ((struct glyph_row *));
482static INLINE void take_vertical_position_into_account P_ ((struct it *));
483static void x_produce_stretch_glyph P_ ((struct it *));
484
06a2c219
GM
485
486/* Flush display of frame F, or of all frames if F is null. */
487
488static void
489x_flush (f)
490 struct frame *f;
491{
492 BLOCK_INPUT;
493 if (f == NULL)
494 {
495 Lisp_Object rest, frame;
496 FOR_EACH_FRAME (rest, frame)
497 x_flush (XFRAME (frame));
498 }
499 else if (FRAME_X_P (f))
500 XFlush (FRAME_X_DISPLAY (f));
501 UNBLOCK_INPUT;
502}
503
dc6f92b8 504
06a2c219
GM
505/* Remove calls to XFlush by defining XFlush to an empty replacement.
506 Calls to XFlush should be unnecessary because the X output buffer
507 is flushed automatically as needed by calls to XPending,
508 XNextEvent, or XWindowEvent according to the XFlush man page.
509 XTread_socket calls XPending. Removing XFlush improves
510 performance. */
511
512#define XFlush(DISPLAY) (void) 0
b8009dd1 513
334208b7 514\f
06a2c219
GM
515/***********************************************************************
516 Debugging
517 ***********************************************************************/
518
9382638d 519#if 0
06a2c219
GM
520
521/* This is a function useful for recording debugging information about
522 the sequence of occurrences in this file. */
9382638d
KH
523
524struct record
525{
526 char *locus;
527 int type;
528};
529
530struct record event_record[100];
531
532int event_record_index;
533
534record_event (locus, type)
535 char *locus;
536 int type;
537{
538 if (event_record_index == sizeof (event_record) / sizeof (struct record))
539 event_record_index = 0;
540
541 event_record[event_record_index].locus = locus;
542 event_record[event_record_index].type = type;
543 event_record_index++;
544}
545
546#endif /* 0 */
06a2c219
GM
547
548
9382638d 549\f
334208b7
RS
550/* Return the struct x_display_info corresponding to DPY. */
551
552struct x_display_info *
553x_display_info_for_display (dpy)
554 Display *dpy;
555{
556 struct x_display_info *dpyinfo;
557
558 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
559 if (dpyinfo->display == dpy)
560 return dpyinfo;
16bd92ea 561
334208b7
RS
562 return 0;
563}
f451eb13 564
06a2c219
GM
565
566\f
567/***********************************************************************
568 Starting and ending an update
569 ***********************************************************************/
570
571/* Start an update of frame F. This function is installed as a hook
572 for update_begin, i.e. it is called when update_begin is called.
573 This function is called prior to calls to x_update_window_begin for
574 each window being updated. Currently, there is nothing to do here
575 because all interesting stuff is done on a window basis. */
dc6f92b8 576
dfcf069d 577static void
06a2c219 578x_update_begin (f)
f676886a 579 struct frame *f;
58769bee 580{
06a2c219
GM
581 /* Nothing to do. */
582}
dc6f92b8 583
dc6f92b8 584
06a2c219
GM
585/* Start update of window W. Set the global variable updated_window
586 to the window being updated and set output_cursor to the cursor
587 position of W. */
dc6f92b8 588
06a2c219
GM
589static void
590x_update_window_begin (w)
591 struct window *w;
592{
593 struct frame *f = XFRAME (WINDOW_FRAME (w));
594 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
595
596 updated_window = w;
597 set_output_cursor (&w->cursor);
b8009dd1 598
06a2c219 599 BLOCK_INPUT;
d1bc4182 600
06a2c219 601 if (f == display_info->mouse_face_mouse_frame)
b8009dd1 602 {
514e4681 603 /* Don't do highlighting for mouse motion during the update. */
06a2c219 604 display_info->mouse_face_defer = 1;
37c2c98b 605
06a2c219
GM
606 /* If F needs to be redrawn, simply forget about any prior mouse
607 highlighting. */
9f67f20b 608 if (FRAME_GARBAGED_P (f))
06a2c219
GM
609 display_info->mouse_face_window = Qnil;
610
64f26cf5
GM
611#if 0 /* Rows in a current matrix containing glyphs in mouse-face have
612 their mouse_face_p flag set, which means that they are always
613 unequal to rows in a desired matrix which never have that
614 flag set. So, rows containing mouse-face glyphs are never
615 scrolled, and we don't have to switch the mouse highlight off
616 here to prevent it from being scrolled. */
617
06a2c219
GM
618 /* Can we tell that this update does not affect the window
619 where the mouse highlight is? If so, no need to turn off.
620 Likewise, don't do anything if the frame is garbaged;
621 in that case, the frame's current matrix that we would use
622 is all wrong, and we will redisplay that line anyway. */
623 if (!NILP (display_info->mouse_face_window)
624 && w == XWINDOW (display_info->mouse_face_window))
514e4681 625 {
06a2c219 626 int i;
514e4681 627
06a2c219
GM
628 for (i = 0; i < w->desired_matrix->nrows; ++i)
629 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
514e4681
RS
630 break;
631
06a2c219
GM
632 if (i < w->desired_matrix->nrows)
633 clear_mouse_face (display_info);
514e4681 634 }
64f26cf5 635#endif /* 0 */
b8009dd1 636 }
6ccf47d1 637
dc6f92b8
JB
638 UNBLOCK_INPUT;
639}
640
06a2c219
GM
641
642/* Draw a vertical window border to the right of window W if W doesn't
643 have vertical scroll bars. */
644
dfcf069d 645static void
06a2c219
GM
646x_draw_vertical_border (w)
647 struct window *w;
58769bee 648{
06a2c219
GM
649 struct frame *f = XFRAME (WINDOW_FRAME (w));
650
651 /* Redraw borders between horizontally adjacent windows. Don't
652 do it for frames with vertical scroll bars because either the
653 right scroll bar of a window, or the left scroll bar of its
654 neighbor will suffice as a border. */
655 if (!WINDOW_RIGHTMOST_P (w)
656 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
657 {
658 int x0, x1, y0, y1;
dc6f92b8 659
06a2c219 660 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
110859fc 661 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
662 y1 -= 1;
663
664 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
665 f->output_data.x->normal_gc, x1, y0, x1, y1);
666 }
667}
668
669
71b8321e
GM
670/* End update of window W (which is equal to updated_window).
671
672 Draw vertical borders between horizontally adjacent windows, and
673 display W's cursor if CURSOR_ON_P is non-zero.
674
675 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
676 glyphs in mouse-face were overwritten. In that case we have to
677 make sure that the mouse-highlight is properly redrawn.
678
679 W may be a menu bar pseudo-window in case we don't have X toolkit
680 support. Such windows don't have a cursor, so don't display it
681 here. */
06a2c219
GM
682
683static void
71b8321e 684x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
06a2c219 685 struct window *w;
71b8321e 686 int cursor_on_p, mouse_face_overwritten_p;
06a2c219
GM
687{
688 if (!w->pseudo_window_p)
689 {
71b8321e
GM
690 struct x_display_info *dpyinfo
691 = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
692
06a2c219 693 BLOCK_INPUT;
71b8321e
GM
694
695 /* If a row with mouse-face was overwritten, arrange for
696 XTframe_up_to_date to redisplay the mouse highlight. */
697 if (mouse_face_overwritten_p)
698 {
699 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
700 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
701 dpyinfo->mouse_face_window = Qnil;
702 }
703
06a2c219
GM
704 if (cursor_on_p)
705 x_display_and_set_cursor (w, 1, output_cursor.hpos,
706 output_cursor.vpos,
707 output_cursor.x, output_cursor.y);
71b8321e 708
06a2c219
GM
709 x_draw_vertical_border (w);
710 UNBLOCK_INPUT;
711 }
712
713 updated_window = NULL;
714}
dc6f92b8 715
dc6f92b8 716
06a2c219
GM
717/* End update of frame F. This function is installed as a hook in
718 update_end. */
719
720static void
721x_update_end (f)
722 struct frame *f;
723{
724 /* Mouse highlight may be displayed again. */
aa8bff2e 725 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
b8009dd1 726
06a2c219 727 BLOCK_INPUT;
334208b7 728 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
729 UNBLOCK_INPUT;
730}
b8009dd1 731
06a2c219
GM
732
733/* This function is called from various places in xdisp.c whenever a
734 complete update has been performed. The global variable
735 updated_window is not available here. */
b8009dd1 736
dfcf069d 737static void
b8009dd1 738XTframe_up_to_date (f)
06a2c219 739 struct frame *f;
b8009dd1 740{
06a2c219 741 if (FRAME_X_P (f))
514e4681 742 {
06a2c219 743 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
71b8321e 744
06a2c219
GM
745 if (dpyinfo->mouse_face_deferred_gc
746 || f == dpyinfo->mouse_face_mouse_frame)
747 {
748 BLOCK_INPUT;
749 if (dpyinfo->mouse_face_mouse_frame)
750 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
751 dpyinfo->mouse_face_mouse_x,
752 dpyinfo->mouse_face_mouse_y);
753 dpyinfo->mouse_face_deferred_gc = 0;
754 UNBLOCK_INPUT;
755 }
514e4681 756 }
b8009dd1 757}
06a2c219
GM
758
759
760/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
761 arrow bitmaps, or clear the areas where they would be displayed
762 before DESIRED_ROW is made current. The window being updated is
763 found in updated_window. This function It is called from
764 update_window_line only if it is known that there are differences
765 between bitmaps to be drawn between current row and DESIRED_ROW. */
766
767static void
768x_after_update_window_line (desired_row)
769 struct glyph_row *desired_row;
770{
771 struct window *w = updated_window;
772
773 xassert (w);
774
775 if (!desired_row->mode_line_p && !w->pseudo_window_p)
776 {
777 BLOCK_INPUT;
778 x_draw_row_bitmaps (w, desired_row);
779
780 /* When a window has disappeared, make sure that no rest of
781 full-width rows stays visible in the internal border. */
782 if (windows_or_buffers_changed)
783 {
784 struct frame *f = XFRAME (w->frame);
785 int width = FRAME_INTERNAL_BORDER_WIDTH (f);
786 int height = desired_row->visible_height;
110859fc
GM
787 int x = (window_box_right (w, -1)
788 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
06a2c219
GM
789 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
790
791 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
792 x, y, width, height, False);
793 }
794
795 UNBLOCK_INPUT;
796 }
797}
798
799
800/* Draw the bitmap WHICH in one of the areas to the left or right of
801 window W. ROW is the glyph row for which to display the bitmap; it
802 determines the vertical position at which the bitmap has to be
803 drawn. */
804
805static void
806x_draw_bitmap (w, row, which)
807 struct window *w;
808 struct glyph_row *row;
809 enum bitmap_type which;
810{
811 struct frame *f = XFRAME (WINDOW_FRAME (w));
812 Display *display = FRAME_X_DISPLAY (f);
813 Window window = FRAME_X_WINDOW (f);
814 int x, y, wd, h, dy;
815 unsigned char *bits;
816 Pixmap pixmap;
817 GC gc = f->output_data.x->normal_gc;
818 struct face *face;
819 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
820
821 /* Must clip because of partially visible lines. */
822 x_clip_to_row (w, row, gc, 1);
823
824 switch (which)
825 {
826 case LEFT_TRUNCATION_BITMAP:
827 wd = left_width;
828 h = left_height;
829 bits = left_bits;
830 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
831 - wd
110859fc 832 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
833 break;
834
835 case OVERLAY_ARROW_BITMAP:
836 wd = left_width;
837 h = left_height;
838 bits = ov_bits;
839 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
840 - wd
110859fc 841 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
842 break;
843
844 case RIGHT_TRUNCATION_BITMAP:
845 wd = right_width;
846 h = right_height;
847 bits = right_bits;
848 x = window_box_right (w, -1);
110859fc 849 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
850 break;
851
852 case CONTINUED_LINE_BITMAP:
853 wd = right_width;
854 h = right_height;
855 bits = continued_bits;
856 x = window_box_right (w, -1);
110859fc 857 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
858 break;
859
860 case CONTINUATION_LINE_BITMAP:
861 wd = continuation_width;
862 h = continuation_height;
863 bits = continuation_bits;
864 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
865 - wd
110859fc 866 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
867 break;
868
869 case ZV_LINE_BITMAP:
870 wd = zv_width;
871 h = zv_height;
872 bits = zv_bits;
873 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
874 - wd
110859fc 875 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
876 break;
877
878 default:
879 abort ();
880 }
881
882 /* Convert to frame coordinates. Set dy to the offset in the row to
883 start drawing the bitmap. */
884 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
885 dy = (row->height - h) / 2;
886
887 /* Draw the bitmap. I believe these small pixmaps can be cached
888 by the server. */
889 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
890 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
891 face->foreground,
892 face->background, depth);
893 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
894 XFreePixmap (display, pixmap);
895 XSetClipMask (display, gc, None);
896}
897
898
899/* Draw flags bitmaps for glyph row ROW on window W. Call this
900 function with input blocked. */
901
902static void
903x_draw_row_bitmaps (w, row)
904 struct window *w;
905 struct glyph_row *row;
906{
907 struct frame *f = XFRAME (w->frame);
908 enum bitmap_type bitmap;
909 struct face *face;
045dee35 910 int header_line_height = -1;
06a2c219
GM
911
912 xassert (interrupt_input_blocked);
913
914 /* If row is completely invisible, because of vscrolling, we
915 don't have to draw anything. */
916 if (row->visible_height <= 0)
917 return;
918
919 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
920 PREPARE_FACE_FOR_DISPLAY (f, face);
921
922 /* Decide which bitmap to draw at the left side. */
923 if (row->overlay_arrow_p)
924 bitmap = OVERLAY_ARROW_BITMAP;
925 else if (row->truncated_on_left_p)
926 bitmap = LEFT_TRUNCATION_BITMAP;
927 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
928 bitmap = CONTINUATION_LINE_BITMAP;
929 else if (row->indicate_empty_line_p)
930 bitmap = ZV_LINE_BITMAP;
931 else
932 bitmap = NO_BITMAP;
933
934 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
935 the flags area. */
936 if (bitmap == NO_BITMAP
110859fc 937 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
938 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
939 {
940 /* If W has a vertical border to its left, don't draw over it. */
941 int border = ((XFASTINT (w->left) > 0
942 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
943 ? 1 : 0);
944 int left = window_box_left (w, -1);
945
045dee35
GM
946 if (header_line_height < 0)
947 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
948
949 /* In case the same realized face is used for bitmap areas and
950 for something displayed in the text (e.g. face `region' on
951 mono-displays, the fill style may have been changed to
952 FillSolid in x_draw_glyph_string_background. */
953 if (face->stipple)
954 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
955 else
956 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
957
06a2c219
GM
958 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
959 face->gc,
960 (left
110859fc 961 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219 962 + border),
045dee35 963 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 964 row->y)),
110859fc 965 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
06a2c219 966 row->visible_height);
dcd08bfb
GM
967 if (!face->stipple)
968 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
969 }
970
971 /* Draw the left bitmap. */
972 if (bitmap != NO_BITMAP)
973 x_draw_bitmap (w, row, bitmap);
974
975 /* Decide which bitmap to draw at the right side. */
976 if (row->truncated_on_right_p)
977 bitmap = RIGHT_TRUNCATION_BITMAP;
978 else if (row->continued_p)
979 bitmap = CONTINUED_LINE_BITMAP;
980 else
981 bitmap = NO_BITMAP;
982
983 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
984 the flags area. */
985 if (bitmap == NO_BITMAP
110859fc 986 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
987 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
988 {
989 int right = window_box_right (w, -1);
990
045dee35
GM
991 if (header_line_height < 0)
992 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
993
994 /* In case the same realized face is used for bitmap areas and
995 for something displayed in the text (e.g. face `region' on
996 mono-displays, the fill style may have been changed to
997 FillSolid in x_draw_glyph_string_background. */
998 if (face->stipple)
999 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
1000 else
1001 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
06a2c219
GM
1002 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1003 face->gc,
1004 right,
045dee35 1005 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 1006 row->y)),
110859fc 1007 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
06a2c219 1008 row->visible_height);
dcd08bfb
GM
1009 if (!face->stipple)
1010 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
1011 }
1012
1013 /* Draw the right bitmap. */
1014 if (bitmap != NO_BITMAP)
1015 x_draw_bitmap (w, row, bitmap);
1016}
1017
dc6f92b8 1018\f
06a2c219
GM
1019/***********************************************************************
1020 Line Highlighting
1021 ***********************************************************************/
dc6f92b8 1022
06a2c219
GM
1023/* External interface to control of standout mode. Not used for X
1024 frames. Aborts when called. */
1025
1026static void
dc6f92b8
JB
1027XTreassert_line_highlight (new, vpos)
1028 int new, vpos;
1029{
06a2c219 1030 abort ();
dc6f92b8
JB
1031}
1032
06a2c219
GM
1033
1034/* Call this when about to modify line at position VPOS and change
1035 whether it is highlighted. Not used for X frames. Aborts when
1036 called. */
dc6f92b8 1037
dfcf069d 1038static void
06a2c219
GM
1039x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1040 int new_highlight, vpos, y, first_unused_hpos;
dc6f92b8 1041{
06a2c219 1042 abort ();
dc6f92b8
JB
1043}
1044
06a2c219
GM
1045
1046/* This is called when starting Emacs and when restarting after
1047 suspend. When starting Emacs, no X window is mapped. And nothing
1048 must be done to Emacs's own window if it is suspended (though that
1049 rarely happens). */
dc6f92b8 1050
dfcf069d 1051static void
dc6f92b8
JB
1052XTset_terminal_modes ()
1053{
1054}
1055
06a2c219
GM
1056/* This is called when exiting or suspending Emacs. Exiting will make
1057 the X-windows go away, and suspending requires no action. */
dc6f92b8 1058
dfcf069d 1059static void
dc6f92b8
JB
1060XTreset_terminal_modes ()
1061{
dc6f92b8 1062}
06a2c219
GM
1063
1064
dc6f92b8 1065\f
06a2c219
GM
1066/***********************************************************************
1067 Output Cursor
1068 ***********************************************************************/
1069
1070/* Set the global variable output_cursor to CURSOR. All cursor
1071 positions are relative to updated_window. */
dc6f92b8 1072
dfcf069d 1073static void
06a2c219
GM
1074set_output_cursor (cursor)
1075 struct cursor_pos *cursor;
dc6f92b8 1076{
06a2c219
GM
1077 output_cursor.hpos = cursor->hpos;
1078 output_cursor.vpos = cursor->vpos;
1079 output_cursor.x = cursor->x;
1080 output_cursor.y = cursor->y;
1081}
1082
1083
1084/* Set a nominal cursor position.
dc6f92b8 1085
06a2c219
GM
1086 HPOS and VPOS are column/row positions in a window glyph matrix. X
1087 and Y are window text area relative pixel positions.
1088
1089 If this is done during an update, updated_window will contain the
1090 window that is being updated and the position is the future output
1091 cursor position for that window. If updated_window is null, use
1092 selected_window and display the cursor at the given position. */
1093
1094static void
1095XTcursor_to (vpos, hpos, y, x)
1096 int vpos, hpos, y, x;
1097{
1098 struct window *w;
1099
1100 /* If updated_window is not set, work on selected_window. */
1101 if (updated_window)
1102 w = updated_window;
1103 else
1104 w = XWINDOW (selected_window);
dbcb258a 1105
06a2c219
GM
1106 /* Set the output cursor. */
1107 output_cursor.hpos = hpos;
1108 output_cursor.vpos = vpos;
1109 output_cursor.x = x;
1110 output_cursor.y = y;
dc6f92b8 1111
06a2c219
GM
1112 /* If not called as part of an update, really display the cursor.
1113 This will also set the cursor position of W. */
1114 if (updated_window == NULL)
dc6f92b8
JB
1115 {
1116 BLOCK_INPUT;
06a2c219 1117 x_display_cursor (w, 1, hpos, vpos, x, y);
b86bd3dd 1118 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
dc6f92b8
JB
1119 UNBLOCK_INPUT;
1120 }
1121}
dc43ef94 1122
06a2c219
GM
1123
1124\f
1125/***********************************************************************
1126 Display Iterator
1127 ***********************************************************************/
1128
1129/* Function prototypes of this page. */
1130
1131static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1132 struct glyph *,
ee569018
KH
1133 XChar2b *,
1134 int *));
06a2c219
GM
1135static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1136 int, XChar2b *, int));
1137static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1138static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1139static void x_append_glyph P_ ((struct it *));
b4192550 1140static void x_append_composite_glyph P_ ((struct it *));
06a2c219
GM
1141static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1142 int, int, double));
1143static void x_produce_glyphs P_ ((struct it *));
06a2c219 1144static void x_produce_image_glyph P_ ((struct it *it));
ee569018
KH
1145
1146
1147/* Return a pointer to per-char metric information in FONT of a
1148 character pointed by B which is a pointer to an XChar2b. */
1149
1150#define PER_CHAR_METRIC(font, b) \
1151 ((font)->per_char \
1152 ? ((font)->per_char + (b)->byte2 - (font)->min_char_or_byte2 \
1153 + (((font)->min_byte1 || (font)->max_byte1) \
1154 ? (((b)->byte1 - (font)->min_byte1) \
1155 * ((font)->max_char_or_byte2 - (font)->min_char_or_byte2 + 1)) \
1156 : 0)) \
1157 : &((font)->max_bounds))
dc43ef94 1158
dc6f92b8 1159
e2ef8ee6
GM
1160/* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1161 is not contained in the font. */
dc43ef94 1162
06a2c219 1163static INLINE XCharStruct *
ee569018 1164x_per_char_metric (font, char2b)
06a2c219
GM
1165 XFontStruct *font;
1166 XChar2b *char2b;
1167{
1168 /* The result metric information. */
1169 XCharStruct *pcm = NULL;
dc6f92b8 1170
06a2c219 1171 xassert (font && char2b);
dc6f92b8 1172
06a2c219 1173 if (font->per_char != NULL)
dc6f92b8 1174 {
06a2c219 1175 if (font->min_byte1 == 0 && font->max_byte1 == 0)
dc43ef94 1176 {
06a2c219
GM
1177 /* min_char_or_byte2 specifies the linear character index
1178 corresponding to the first element of the per_char array,
1179 max_char_or_byte2 is the index of the last character. A
1180 character with non-zero CHAR2B->byte1 is not in the font.
1181 A character with byte2 less than min_char_or_byte2 or
1182 greater max_char_or_byte2 is not in the font. */
1183 if (char2b->byte1 == 0
1184 && char2b->byte2 >= font->min_char_or_byte2
1185 && char2b->byte2 <= font->max_char_or_byte2)
1186 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
dc43ef94 1187 }
06a2c219 1188 else
dc6f92b8 1189 {
06a2c219
GM
1190 /* If either min_byte1 or max_byte1 are nonzero, both
1191 min_char_or_byte2 and max_char_or_byte2 are less than
1192 256, and the 2-byte character index values corresponding
1193 to the per_char array element N (counting from 0) are:
1194
1195 byte1 = N/D + min_byte1
1196 byte2 = N\D + min_char_or_byte2
1197
1198 where:
1199
1200 D = max_char_or_byte2 - min_char_or_byte2 + 1
1201 / = integer division
1202 \ = integer modulus */
1203 if (char2b->byte1 >= font->min_byte1
1204 && char2b->byte1 <= font->max_byte1
1205 && char2b->byte2 >= font->min_char_or_byte2
1206 && char2b->byte2 <= font->max_char_or_byte2)
1207 {
1208 pcm = (font->per_char
1209 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1210 * (char2b->byte1 - font->min_byte1))
1211 + (char2b->byte2 - font->min_char_or_byte2));
1212 }
dc6f92b8 1213 }
06a2c219
GM
1214 }
1215 else
1216 {
1217 /* If the per_char pointer is null, all glyphs between the first
1218 and last character indexes inclusive have the same
1219 information, as given by both min_bounds and max_bounds. */
1220 if (char2b->byte2 >= font->min_char_or_byte2
1221 && char2b->byte2 <= font->max_char_or_byte2)
1222 pcm = &font->max_bounds;
1223 }
dc6f92b8 1224
ee569018 1225 return ((pcm == NULL
3e71d8f2 1226 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
ee569018 1227 ? NULL : pcm);
06a2c219 1228}
b73b6aaf 1229
57b03282 1230
06a2c219
GM
1231/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1232 the two-byte form of C. Encoding is returned in *CHAR2B. */
dc43ef94 1233
06a2c219
GM
1234static INLINE void
1235x_encode_char (c, char2b, font_info)
1236 int c;
1237 XChar2b *char2b;
1238 struct font_info *font_info;
1239{
1240 int charset = CHAR_CHARSET (c);
1241 XFontStruct *font = font_info->font;
1242
1243 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1244 This may be either a program in a special encoder language or a
1245 fixed encoding. */
1246 if (font_info->font_encoder)
1247 {
1248 /* It's a program. */
1249 struct ccl_program *ccl = font_info->font_encoder;
1250
1251 if (CHARSET_DIMENSION (charset) == 1)
1252 {
1253 ccl->reg[0] = charset;
1254 ccl->reg[1] = char2b->byte2;
1255 }
1256 else
1257 {
1258 ccl->reg[0] = charset;
1259 ccl->reg[1] = char2b->byte1;
1260 ccl->reg[2] = char2b->byte2;
1261 }
1262
1263 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1264
1265 /* We assume that MSBs are appropriately set/reset by CCL
1266 program. */
1267 if (font->max_byte1 == 0) /* 1-byte font */
ee569018 1268 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
06a2c219
GM
1269 else
1270 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1271 }
1272 else if (font_info->encoding[charset])
1273 {
1274 /* Fixed encoding scheme. See fontset.h for the meaning of the
1275 encoding numbers. */
1276 int enc = font_info->encoding[charset];
1277
1278 if ((enc == 1 || enc == 2)
1279 && CHARSET_DIMENSION (charset) == 2)
1280 char2b->byte1 |= 0x80;
1281
1282 if (enc == 1 || enc == 3)
1283 char2b->byte2 |= 0x80;
1284 }
1285}
1286
1287
1288/* Get face and two-byte form of character C in face FACE_ID on frame
1289 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1290 means we want to display multibyte text. Value is a pointer to a
1291 realized face that is ready for display. */
1292
1293static INLINE struct face *
1294x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1295 struct frame *f;
1296 int c, face_id;
1297 XChar2b *char2b;
1298 int multibyte_p;
1299{
1300 struct face *face = FACE_FROM_ID (f, face_id);
1301
1302 if (!multibyte_p)
1303 {
1304 /* Unibyte case. We don't have to encode, but we have to make
1305 sure to use a face suitable for unibyte. */
1306 char2b->byte1 = 0;
1307 char2b->byte2 = c;
ee569018
KH
1308 face_id = FACE_FOR_CHAR (f, face, c);
1309 face = FACE_FROM_ID (f, face_id);
06a2c219
GM
1310 }
1311 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1312 {
1313 /* Case of ASCII in a face known to fit ASCII. */
1314 char2b->byte1 = 0;
1315 char2b->byte2 = c;
1316 }
1317 else
1318 {
1319 int c1, c2, charset;
1320
1321 /* Split characters into bytes. If c2 is -1 afterwards, C is
1322 really a one-byte character so that byte1 is zero. */
1323 SPLIT_CHAR (c, charset, c1, c2);
1324 if (c2 > 0)
1325 char2b->byte1 = c1, char2b->byte2 = c2;
1326 else
1327 char2b->byte1 = 0, char2b->byte2 = c1;
1328
06a2c219 1329 /* Maybe encode the character in *CHAR2B. */
ee569018 1330 if (face->font != NULL)
06a2c219
GM
1331 {
1332 struct font_info *font_info
1333 = FONT_INFO_FROM_ID (f, face->font_info_id);
1334 if (font_info)
ee569018 1335 x_encode_char (c, char2b, font_info);
06a2c219
GM
1336 }
1337 }
1338
1339 /* Make sure X resources of the face are allocated. */
1340 xassert (face != NULL);
1341 PREPARE_FACE_FOR_DISPLAY (f, face);
1342
1343 return face;
1344}
1345
1346
1347/* Get face and two-byte form of character glyph GLYPH on frame F.
43d120d8 1348 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
06a2c219
GM
1349 a pointer to a realized face that is ready for display. */
1350
1351static INLINE struct face *
ee569018 1352x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
06a2c219
GM
1353 struct frame *f;
1354 struct glyph *glyph;
1355 XChar2b *char2b;
ee569018 1356 int *two_byte_p;
06a2c219
GM
1357{
1358 struct face *face;
1359
1360 xassert (glyph->type == CHAR_GLYPH);
43d120d8 1361 face = FACE_FROM_ID (f, glyph->face_id);
06a2c219 1362
ee569018
KH
1363 if (two_byte_p)
1364 *two_byte_p = 0;
1365
06a2c219
GM
1366 if (!glyph->multibyte_p)
1367 {
1368 /* Unibyte case. We don't have to encode, but we have to make
1369 sure to use a face suitable for unibyte. */
1370 char2b->byte1 = 0;
43d120d8 1371 char2b->byte2 = glyph->u.ch;
06a2c219 1372 }
43d120d8
KH
1373 else if (glyph->u.ch < 128
1374 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
06a2c219
GM
1375 {
1376 /* Case of ASCII in a face known to fit ASCII. */
1377 char2b->byte1 = 0;
43d120d8 1378 char2b->byte2 = glyph->u.ch;
06a2c219
GM
1379 }
1380 else
1381 {
1382 int c1, c2, charset;
1383
1384 /* Split characters into bytes. If c2 is -1 afterwards, C is
1385 really a one-byte character so that byte1 is zero. */
43d120d8 1386 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
06a2c219
GM
1387 if (c2 > 0)
1388 char2b->byte1 = c1, char2b->byte2 = c2;
1389 else
1390 char2b->byte1 = 0, char2b->byte2 = c1;
1391
1392 /* Maybe encode the character in *CHAR2B. */
1393 if (charset != CHARSET_ASCII)
1394 {
1395 struct font_info *font_info
1396 = FONT_INFO_FROM_ID (f, face->font_info_id);
1397 if (font_info)
1398 {
43d120d8 1399 x_encode_char (glyph->u.ch, char2b, font_info);
ee569018
KH
1400 if (two_byte_p)
1401 *two_byte_p
1402 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
06a2c219
GM
1403 }
1404 }
1405 }
1406
1407 /* Make sure X resources of the face are allocated. */
1408 xassert (face != NULL);
1409 PREPARE_FACE_FOR_DISPLAY (f, face);
1410 return face;
1411}
1412
1413
1414/* Store one glyph for IT->char_to_display in IT->glyph_row.
1415 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1416
1417static INLINE void
1418x_append_glyph (it)
1419 struct it *it;
1420{
1421 struct glyph *glyph;
1422 enum glyph_row_area area = it->area;
1423
1424 xassert (it->glyph_row);
1425 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1426
1427 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1428 if (glyph < it->glyph_row->glyphs[area + 1])
1429 {
06a2c219
GM
1430 glyph->charpos = CHARPOS (it->position);
1431 glyph->object = it->object;
88d75730 1432 glyph->pixel_width = it->pixel_width;
06a2c219 1433 glyph->voffset = it->voffset;
88d75730 1434 glyph->type = CHAR_GLYPH;
06a2c219 1435 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1436 glyph->left_box_line_p = it->start_of_box_run_p;
1437 glyph->right_box_line_p = it->end_of_box_run_p;
66ac4b0e
GM
1438 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1439 || it->phys_descent > it->descent);
88d75730 1440 glyph->padding_p = 0;
ee569018 1441 glyph->glyph_not_available_p = it->glyph_not_available_p;
88d75730
GM
1442 glyph->face_id = it->face_id;
1443 glyph->u.ch = it->char_to_display;
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 {
b4192550
KH
1463 glyph->charpos = CHARPOS (it->position);
1464 glyph->object = it->object;
88d75730 1465 glyph->pixel_width = it->pixel_width;
b4192550 1466 glyph->voffset = it->voffset;
88d75730 1467 glyph->type = COMPOSITE_GLYPH;
b4192550 1468 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1469 glyph->left_box_line_p = it->start_of_box_run_p;
1470 glyph->right_box_line_p = it->end_of_box_run_p;
b4192550
KH
1471 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1472 || it->phys_descent > it->descent);
88d75730
GM
1473 glyph->padding_p = 0;
1474 glyph->glyph_not_available_p = 0;
1475 glyph->face_id = it->face_id;
1476 glyph->u.cmp_id = it->cmp_id;
b4192550
KH
1477 ++it->glyph_row->used[area];
1478 }
1479}
1480
06a2c219
GM
1481
1482/* Change IT->ascent and IT->height according to the setting of
1483 IT->voffset. */
1484
1485static INLINE void
1486take_vertical_position_into_account (it)
1487 struct it *it;
1488{
1489 if (it->voffset)
1490 {
1491 if (it->voffset < 0)
1492 /* Increase the ascent so that we can display the text higher
1493 in the line. */
1494 it->ascent += abs (it->voffset);
1495 else
1496 /* Increase the descent so that we can display the text lower
1497 in the line. */
1498 it->descent += it->voffset;
1499 }
1500}
1501
1502
1503/* Produce glyphs/get display metrics for the image IT is loaded with.
1504 See the description of struct display_iterator in dispextern.h for
1505 an overview of struct display_iterator. */
1506
1507static void
1508x_produce_image_glyph (it)
1509 struct it *it;
1510{
1511 struct image *img;
1512 struct face *face;
1513
1514 xassert (it->what == IT_IMAGE);
1515
1516 face = FACE_FROM_ID (it->f, it->face_id);
1517 img = IMAGE_FROM_ID (it->f, it->image_id);
1518 xassert (img);
1519
1520 /* Make sure X resources of the face and image are loaded. */
1521 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1522 prepare_image_for_display (it->f, img);
1523
95af8492 1524 it->ascent = it->phys_ascent = image_ascent (img, face);
66ac4b0e 1525 it->descent = it->phys_descent = img->height + 2 * img->margin - it->ascent;
06a2c219
GM
1526 it->pixel_width = img->width + 2 * img->margin;
1527
1528 it->nglyphs = 1;
1529
1530 if (face->box != FACE_NO_BOX)
1531 {
1532 it->ascent += face->box_line_width;
1533 it->descent += face->box_line_width;
1534
1535 if (it->start_of_box_run_p)
1536 it->pixel_width += face->box_line_width;
1537 if (it->end_of_box_run_p)
1538 it->pixel_width += face->box_line_width;
1539 }
1540
1541 take_vertical_position_into_account (it);
1542
1543 if (it->glyph_row)
1544 {
1545 struct glyph *glyph;
1546 enum glyph_row_area area = it->area;
1547
1548 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1549 if (glyph < it->glyph_row->glyphs[area + 1])
1550 {
06a2c219
GM
1551 glyph->charpos = CHARPOS (it->position);
1552 glyph->object = it->object;
88d75730 1553 glyph->pixel_width = it->pixel_width;
06a2c219 1554 glyph->voffset = it->voffset;
88d75730 1555 glyph->type = IMAGE_GLYPH;
06a2c219 1556 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1557 glyph->left_box_line_p = it->start_of_box_run_p;
1558 glyph->right_box_line_p = it->end_of_box_run_p;
1559 glyph->overlaps_vertically_p = 0;
1560 glyph->padding_p = 0;
1561 glyph->glyph_not_available_p = 0;
1562 glyph->face_id = it->face_id;
1563 glyph->u.img_id = img->id;
06a2c219
GM
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 {
06a2c219
GM
1590 glyph->charpos = CHARPOS (it->position);
1591 glyph->object = object;
88d75730 1592 glyph->pixel_width = width;
06a2c219 1593 glyph->voffset = it->voffset;
88d75730 1594 glyph->type = STRETCH_GLYPH;
06a2c219 1595 glyph->multibyte_p = it->multibyte_p;
88d75730
GM
1596 glyph->left_box_line_p = it->start_of_box_run_p;
1597 glyph->right_box_line_p = it->end_of_box_run_p;
1598 glyph->overlaps_vertically_p = 0;
1599 glyph->padding_p = 0;
1600 glyph->glyph_not_available_p = 0;
1601 glyph->face_id = it->face_id;
1602 glyph->u.stretch.ascent = height * ascent;
1603 glyph->u.stretch.height = height;
06a2c219
GM
1604 ++it->glyph_row->used[area];
1605 }
1606}
1607
1608
1609/* Produce a stretch glyph for iterator IT. IT->object is the value
1610 of the glyph property displayed. The value must be a list
1611 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1612 being recognized:
1613
1614 1. `:width WIDTH' specifies that the space should be WIDTH *
1615 canonical char width wide. WIDTH may be an integer or floating
1616 point number.
1617
1618 2. `:relative-width FACTOR' specifies that the width of the stretch
1619 should be computed from the width of the first character having the
1620 `glyph' property, and should be FACTOR times that width.
1621
1622 3. `:align-to HPOS' specifies that the space should be wide enough
1623 to reach HPOS, a value in canonical character units.
1624
1625 Exactly one of the above pairs must be present.
1626
1627 4. `:height HEIGHT' specifies that the height of the stretch produced
1628 should be HEIGHT, measured in canonical character units.
1629
1630 5. `:relative-height FACTOR' specifies that the height of the the
1631 stretch should be FACTOR times the height of the characters having
1632 the glyph property.
1633
1634 Either none or exactly one of 4 or 5 must be present.
1635
1636 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1637 of the stretch should be used for the ascent of the stretch.
1638 ASCENT must be in the range 0 <= ASCENT <= 100. */
1639
1640#define NUMVAL(X) \
1641 ((INTEGERP (X) || FLOATP (X)) \
1642 ? XFLOATINT (X) \
1643 : - 1)
1644
1645
1646static void
1647x_produce_stretch_glyph (it)
1648 struct it *it;
1649{
1650 /* (space :width WIDTH :height HEIGHT. */
3e71d8f2
GM
1651#if GLYPH_DEBUG
1652 extern Lisp_Object Qspace;
1653#endif
1654 extern Lisp_Object QCwidth, QCheight, QCascent;
06a2c219
GM
1655 extern Lisp_Object QCrelative_width, QCrelative_height;
1656 extern Lisp_Object QCalign_to;
1657 Lisp_Object prop, plist;
1658 double width = 0, height = 0, ascent = 0;
1659 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1660 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1661
1662 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1663
1664 /* List should start with `space'. */
1665 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1666 plist = XCDR (it->object);
1667
1668 /* Compute the width of the stretch. */
1669 if (prop = Fplist_get (plist, QCwidth),
1670 NUMVAL (prop) > 0)
1671 /* Absolute width `:width WIDTH' specified and valid. */
1672 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1673 else if (prop = Fplist_get (plist, QCrelative_width),
1674 NUMVAL (prop) > 0)
1675 {
1676 /* Relative width `:relative-width FACTOR' specified and valid.
1677 Compute the width of the characters having the `glyph'
1678 property. */
1679 struct it it2;
1680 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1681
1682 it2 = *it;
1683 if (it->multibyte_p)
1684 {
1685 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1686 - IT_BYTEPOS (*it));
1687 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1688 }
1689 else
1690 it2.c = *p, it2.len = 1;
1691
1692 it2.glyph_row = NULL;
1693 it2.what = IT_CHARACTER;
1694 x_produce_glyphs (&it2);
1695 width = NUMVAL (prop) * it2.pixel_width;
1696 }
1697 else if (prop = Fplist_get (plist, QCalign_to),
1698 NUMVAL (prop) > 0)
1699 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1700 else
1701 /* Nothing specified -> width defaults to canonical char width. */
1702 width = CANON_X_UNIT (it->f);
1703
1704 /* Compute height. */
1705 if (prop = Fplist_get (plist, QCheight),
1706 NUMVAL (prop) > 0)
1707 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1708 else if (prop = Fplist_get (plist, QCrelative_height),
1709 NUMVAL (prop) > 0)
1710 height = FONT_HEIGHT (font) * NUMVAL (prop);
1711 else
1712 height = FONT_HEIGHT (font);
1713
1714 /* Compute percentage of height used for ascent. If
1715 `:ascent ASCENT' is present and valid, use that. Otherwise,
1716 derive the ascent from the font in use. */
1717 if (prop = Fplist_get (plist, QCascent),
1718 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1719 ascent = NUMVAL (prop) / 100.0;
1720 else
1721 ascent = (double) font->ascent / FONT_HEIGHT (font);
1722
1723 if (width <= 0)
1724 width = 1;
1725 if (height <= 0)
1726 height = 1;
1727
1728 if (it->glyph_row)
1729 {
1730 Lisp_Object object = it->stack[it->sp - 1].string;
1731 if (!STRINGP (object))
1732 object = it->w->buffer;
1733 x_append_stretch_glyph (it, object, width, height, ascent);
1734 }
1735
1736 it->pixel_width = width;
66ac4b0e
GM
1737 it->ascent = it->phys_ascent = height * ascent;
1738 it->descent = it->phys_descent = height - it->ascent;
06a2c219
GM
1739 it->nglyphs = 1;
1740
1741 if (face->box != FACE_NO_BOX)
1742 {
1743 it->ascent += face->box_line_width;
1744 it->descent += face->box_line_width;
1745
1746 if (it->start_of_box_run_p)
1747 it->pixel_width += face->box_line_width;
1748 if (it->end_of_box_run_p)
1749 it->pixel_width += face->box_line_width;
1750 }
1751
1752 take_vertical_position_into_account (it);
1753}
1754
b4192550
KH
1755/* Return proper value to be used as baseline offset of font that has
1756 ASCENT and DESCENT to draw characters by the font at the vertical
1757 center of the line of frame F.
1758
1759 Here, out task is to find the value of BOFF in the following figure;
1760
1761 -------------------------+-----------+-
1762 -+-+---------+-+ | |
1763 | | | | | |
1764 | | | | F_ASCENT F_HEIGHT
1765 | | | ASCENT | |
1766 HEIGHT | | | | |
1767 | | |-|-+------+-----------|------- baseline
1768 | | | | BOFF | |
1769 | |---------|-+-+ | |
1770 | | | DESCENT | |
1771 -+-+---------+-+ F_DESCENT |
1772 -------------------------+-----------+-
1773
1774 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1775 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1776 DESCENT = FONT->descent
1777 HEIGHT = FONT_HEIGHT (FONT)
1778 F_DESCENT = (F->output_data.x->font->descent
1779 - F->output_data.x->baseline_offset)
1780 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1781*/
1782
1783#define VCENTER_BASELINE_OFFSET(FONT, F) \
1784 ((FONT)->descent \
1785 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT))) / 2 \
1786 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
06a2c219
GM
1787
1788/* Produce glyphs/get display metrics for the display element IT is
1789 loaded with. See the description of struct display_iterator in
1790 dispextern.h for an overview of struct display_iterator. */
1791
1792static void
1793x_produce_glyphs (it)
1794 struct it *it;
1795{
ee569018
KH
1796 it->glyph_not_available_p = 0;
1797
06a2c219
GM
1798 if (it->what == IT_CHARACTER)
1799 {
1800 XChar2b char2b;
1801 XFontStruct *font;
ee569018 1802 struct face *face = FACE_FROM_ID (it->f, it->face_id);
06a2c219 1803 XCharStruct *pcm;
06a2c219 1804 int font_not_found_p;
b4192550
KH
1805 struct font_info *font_info;
1806 int boff; /* baseline offset */
06a2c219 1807
ee569018
KH
1808 /* Maybe translate single-byte characters to multibyte, or the
1809 other way. */
06a2c219 1810 it->char_to_display = it->c;
ee569018 1811 if (!ASCII_BYTE_P (it->c))
06a2c219 1812 {
ee569018
KH
1813 if (unibyte_display_via_language_environment
1814 && SINGLE_BYTE_CHAR_P (it->c)
1815 && (it->c >= 0240
1816 || !NILP (Vnonascii_translation_table)))
1817 {
1818 it->char_to_display = unibyte_char_to_multibyte (it->c);
1819 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1820 face = FACE_FROM_ID (it->f, it->face_id);
1821 }
1822 else if (!SINGLE_BYTE_CHAR_P (it->c)
1823 && !it->multibyte_p)
1824 {
1825 it->char_to_display = multibyte_char_to_unibyte (it->c, Qnil);
1826 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1827 face = FACE_FROM_ID (it->f, it->face_id);
1828 }
06a2c219
GM
1829 }
1830
ee569018
KH
1831 /* Get font to use. Encode IT->char_to_display. */
1832 x_get_char_face_and_encoding (it->f, it->char_to_display,
1833 it->face_id, &char2b,
1834 it->multibyte_p);
06a2c219
GM
1835 font = face->font;
1836
1837 /* When no suitable font found, use the default font. */
1838 font_not_found_p = font == NULL;
1839 if (font_not_found_p)
b4192550
KH
1840 {
1841 font = FRAME_FONT (it->f);
1842 boff = it->f->output_data.x->baseline_offset;
1843 font_info = NULL;
1844 }
1845 else
1846 {
1847 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1848 boff = font_info->baseline_offset;
1849 if (font_info->vertical_centering)
1850 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1851 }
06a2c219
GM
1852
1853 if (it->char_to_display >= ' '
1854 && (!it->multibyte_p || it->char_to_display < 128))
1855 {
1856 /* Either unibyte or ASCII. */
1857 int stretched_p;
1858
1859 it->nglyphs = 1;
06a2c219
GM
1860
1861 pcm = x_per_char_metric (font, &char2b);
b4192550
KH
1862 it->ascent = font->ascent + boff;
1863 it->descent = font->descent - boff;
474848ac
GM
1864
1865 if (pcm)
1866 {
1867 it->phys_ascent = pcm->ascent + boff;
1868 it->phys_descent = pcm->descent - boff;
1869 it->pixel_width = pcm->width;
1870 }
1871 else
1872 {
1873 it->glyph_not_available_p = 1;
1874 it->phys_ascent = font->ascent + boff;
1875 it->phys_descent = font->descent - boff;
1876 it->pixel_width = FONT_WIDTH (font);
1877 }
06a2c219
GM
1878
1879 /* If this is a space inside a region of text with
1880 `space-width' property, change its width. */
1881 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1882 if (stretched_p)
1883 it->pixel_width *= XFLOATINT (it->space_width);
1884
1885 /* If face has a box, add the box thickness to the character
1886 height. If character has a box line to the left and/or
1887 right, add the box line width to the character's width. */
1888 if (face->box != FACE_NO_BOX)
1889 {
1890 int thick = face->box_line_width;
1891
1892 it->ascent += thick;
1893 it->descent += thick;
1894
1895 if (it->start_of_box_run_p)
1896 it->pixel_width += thick;
1897 if (it->end_of_box_run_p)
1898 it->pixel_width += thick;
1899 }
1900
1901 /* If face has an overline, add the height of the overline
1902 (1 pixel) and a 1 pixel margin to the character height. */
1903 if (face->overline_p)
1904 it->ascent += 2;
1905
1906 take_vertical_position_into_account (it);
1907
1908 /* If we have to actually produce glyphs, do it. */
1909 if (it->glyph_row)
1910 {
1911 if (stretched_p)
1912 {
1913 /* Translate a space with a `space-width' property
1914 into a stretch glyph. */
1915 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1916 x_append_stretch_glyph (it, it->object, it->pixel_width,
1917 it->ascent + it->descent, ascent);
1918 }
1919 else
1920 x_append_glyph (it);
1921
1922 /* If characters with lbearing or rbearing are displayed
1923 in this line, record that fact in a flag of the
1924 glyph row. This is used to optimize X output code. */
1c7e22fd 1925 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
06a2c219
GM
1926 it->glyph_row->contains_overlapping_glyphs_p = 1;
1927 }
1928 }
1929 else if (it->char_to_display == '\n')
1930 {
1931 /* A newline has no width but we need the height of the line. */
1932 it->pixel_width = 0;
1933 it->nglyphs = 0;
b4192550
KH
1934 it->ascent = it->phys_ascent = font->ascent + boff;
1935 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1936
1937 if (face->box != FACE_NO_BOX)
1938 {
1939 int thick = face->box_line_width;
1940 it->ascent += thick;
1941 it->descent += thick;
1942 }
1943 }
1944 else if (it->char_to_display == '\t')
1945 {
1946 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
d365f5bb 1947 int x = it->current_x + it->continuation_lines_width;
06a2c219
GM
1948 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1949
1950 it->pixel_width = next_tab_x - x;
1951 it->nglyphs = 1;
b4192550
KH
1952 it->ascent = it->phys_ascent = font->ascent + boff;
1953 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1954
1955 if (it->glyph_row)
1956 {
1957 double ascent = (double) it->ascent / (it->ascent + it->descent);
1958 x_append_stretch_glyph (it, it->object, it->pixel_width,
1959 it->ascent + it->descent, ascent);
1960 }
1961 }
1962 else
1963 {
1964 /* A multi-byte character. Assume that the display width of the
1965 character is the width of the character multiplied by the
b4192550 1966 width of the font. */
06a2c219 1967
b4192550
KH
1968 /* If we found a font, this font should give us the right
1969 metrics. If we didn't find a font, use the frame's
1970 default font and calculate the width of the character
1971 from the charset width; this is what old redisplay code
1972 did. */
1973 pcm = x_per_char_metric (font, &char2b);
ee569018
KH
1974 if (font_not_found_p || !pcm)
1975 {
1976 int charset = CHAR_CHARSET (it->char_to_display);
1977
1978 it->glyph_not_available_p = 1;
1979 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
1980 * CHARSET_WIDTH (charset));
1981 it->phys_ascent = font->ascent + boff;
1982 it->phys_descent = font->descent - boff;
1983 }
1984 else
1985 {
1986 it->pixel_width = pcm->width;
1987 it->phys_ascent = pcm->ascent + boff;
1988 it->phys_descent = pcm->descent - boff;
1989 if (it->glyph_row
1990 && (pcm->lbearing < 0
1991 || pcm->rbearing > pcm->width))
1992 it->glyph_row->contains_overlapping_glyphs_p = 1;
1993 }
b4192550
KH
1994 it->nglyphs = 1;
1995 it->ascent = font->ascent + boff;
1996 it->descent = font->descent - boff;
06a2c219
GM
1997 if (face->box != FACE_NO_BOX)
1998 {
1999 int thick = face->box_line_width;
2000 it->ascent += thick;
2001 it->descent += thick;
2002
2003 if (it->start_of_box_run_p)
2004 it->pixel_width += thick;
2005 if (it->end_of_box_run_p)
2006 it->pixel_width += thick;
2007 }
2008
2009 /* If face has an overline, add the height of the overline
2010 (1 pixel) and a 1 pixel margin to the character height. */
2011 if (face->overline_p)
2012 it->ascent += 2;
2013
2014 take_vertical_position_into_account (it);
2015
2016 if (it->glyph_row)
2017 x_append_glyph (it);
2018 }
2019 }
b4192550
KH
2020 else if (it->what == IT_COMPOSITION)
2021 {
2022 /* Note: A composition is represented as one glyph in the
2023 glyph matrix. There are no padding glyphs. */
2024 XChar2b char2b;
2025 XFontStruct *font;
ee569018 2026 struct face *face = FACE_FROM_ID (it->f, it->face_id);
b4192550
KH
2027 XCharStruct *pcm;
2028 int font_not_found_p;
2029 struct font_info *font_info;
2030 int boff; /* baseline offset */
2031 struct composition *cmp = composition_table[it->cmp_id];
2032
2033 /* Maybe translate single-byte characters to multibyte. */
2034 it->char_to_display = it->c;
2035 if (unibyte_display_via_language_environment
2036 && SINGLE_BYTE_CHAR_P (it->c)
2037 && (it->c >= 0240
2038 || (it->c >= 0200
2039 && !NILP (Vnonascii_translation_table))))
2040 {
2041 it->char_to_display = unibyte_char_to_multibyte (it->c);
b4192550
KH
2042 }
2043
2044 /* Get face and font to use. Encode IT->char_to_display. */
ee569018
KH
2045 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2046 face = FACE_FROM_ID (it->f, it->face_id);
2047 x_get_char_face_and_encoding (it->f, it->char_to_display,
2048 it->face_id, &char2b, it->multibyte_p);
b4192550
KH
2049 font = face->font;
2050
2051 /* When no suitable font found, use the default font. */
2052 font_not_found_p = font == NULL;
2053 if (font_not_found_p)
2054 {
2055 font = FRAME_FONT (it->f);
2056 boff = it->f->output_data.x->baseline_offset;
2057 font_info = NULL;
2058 }
2059 else
2060 {
2061 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2062 boff = font_info->baseline_offset;
2063 if (font_info->vertical_centering)
2064 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2065 }
2066
2067 /* There are no padding glyphs, so there is only one glyph to
2068 produce for the composition. Important is that pixel_width,
2069 ascent and descent are the values of what is drawn by
2070 draw_glyphs (i.e. the values of the overall glyphs composed). */
2071 it->nglyphs = 1;
2072
2073 /* If we have not yet calculated pixel size data of glyphs of
2074 the composition for the current face font, calculate them
2075 now. Theoretically, we have to check all fonts for the
2076 glyphs, but that requires much time and memory space. So,
2077 here we check only the font of the first glyph. This leads
2078 to incorrect display very rarely, and C-l (recenter) can
2079 correct the display anyway. */
2080 if (cmp->font != (void *) font)
2081 {
2082 /* Ascent and descent of the font of the first character of
2083 this composition (adjusted by baseline offset). Ascent
2084 and descent of overall glyphs should not be less than
2085 them respectively. */
2086 int font_ascent = font->ascent + boff;
2087 int font_descent = font->descent - boff;
2088 /* Bounding box of the overall glyphs. */
2089 int leftmost, rightmost, lowest, highest;
329bed06 2090 int i, width, ascent, descent;
b4192550
KH
2091
2092 cmp->font = (void *) font;
2093
2094 /* Initialize the bounding box. */
2095 pcm = x_per_char_metric (font, &char2b);
329bed06
GM
2096 if (pcm)
2097 {
2098 width = pcm->width;
2099 ascent = pcm->ascent;
2100 descent = pcm->descent;
2101 }
2102 else
2103 {
2104 width = FONT_WIDTH (font);
2105 ascent = font->ascent;
2106 descent = font->descent;
2107 }
2108
2109 rightmost = width;
2110 lowest = - descent + boff;
2111 highest = ascent + boff;
b4192550 2112 leftmost = 0;
329bed06 2113
b4192550
KH
2114 if (font_info
2115 && font_info->default_ascent
2116 && CHAR_TABLE_P (Vuse_default_ascent)
2117 && !NILP (Faref (Vuse_default_ascent,
2118 make_number (it->char_to_display))))
2119 highest = font_info->default_ascent + boff;
2120
2121 /* Draw the first glyph at the normal position. It may be
2122 shifted to right later if some other glyphs are drawn at
2123 the left. */
2124 cmp->offsets[0] = 0;
2125 cmp->offsets[1] = boff;
2126
2127 /* Set cmp->offsets for the remaining glyphs. */
2128 for (i = 1; i < cmp->glyph_len; i++)
2129 {
2130 int left, right, btm, top;
2131 int ch = COMPOSITION_GLYPH (cmp, i);
ee569018
KH
2132 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2133
2134 face = FACE_FROM_ID (it->f, face_id);
2135 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2136 it->multibyte_p);
b4192550
KH
2137 font = face->font;
2138 if (font == NULL)
2139 {
2140 font = FRAME_FONT (it->f);
2141 boff = it->f->output_data.x->baseline_offset;
2142 font_info = NULL;
2143 }
2144 else
2145 {
2146 font_info
2147 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2148 boff = font_info->baseline_offset;
2149 if (font_info->vertical_centering)
2150 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2151 }
2152
2153 pcm = x_per_char_metric (font, &char2b);
329bed06
GM
2154 if (pcm)
2155 {
2156 width = pcm->width;
2157 ascent = pcm->ascent;
2158 descent = pcm->descent;
2159 }
2160 else
2161 {
2162 width = FONT_WIDTH (font);
2163 ascent = font->ascent;
2164 descent = font->descent;
2165 }
b4192550
KH
2166
2167 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2168 {
2169 /* Relative composition with or without
2170 alternate chars. */
329bed06
GM
2171 left = (leftmost + rightmost - width) / 2;
2172 btm = - descent + boff;
b4192550
KH
2173 if (font_info && font_info->relative_compose
2174 && (! CHAR_TABLE_P (Vignore_relative_composition)
2175 || NILP (Faref (Vignore_relative_composition,
2176 make_number (ch)))))
2177 {
2178
329bed06 2179 if (- descent >= font_info->relative_compose)
b4192550
KH
2180 /* One extra pixel between two glyphs. */
2181 btm = highest + 1;
329bed06 2182 else if (ascent <= 0)
b4192550 2183 /* One extra pixel between two glyphs. */
329bed06 2184 btm = lowest - 1 - ascent - descent;
b4192550
KH
2185 }
2186 }
2187 else
2188 {
2189 /* A composition rule is specified by an integer
2190 value that encodes global and new reference
2191 points (GREF and NREF). GREF and NREF are
2192 specified by numbers as below:
2193
2194 0---1---2 -- ascent
2195 | |
2196 | |
2197 | |
2198 9--10--11 -- center
2199 | |
2200 ---3---4---5--- baseline
2201 | |
2202 6---7---8 -- descent
2203 */
2204 int rule = COMPOSITION_RULE (cmp, i);
2205 int gref, nref, grefx, grefy, nrefx, nrefy;
2206
2207 COMPOSITION_DECODE_RULE (rule, gref, nref);
2208 grefx = gref % 3, nrefx = nref % 3;
2209 grefy = gref / 3, nrefy = nref / 3;
2210
2211 left = (leftmost
2212 + grefx * (rightmost - leftmost) / 2
329bed06 2213 - nrefx * width / 2);
b4192550
KH
2214 btm = ((grefy == 0 ? highest
2215 : grefy == 1 ? 0
2216 : grefy == 2 ? lowest
2217 : (highest + lowest) / 2)
329bed06
GM
2218 - (nrefy == 0 ? ascent + descent
2219 : nrefy == 1 ? descent - boff
b4192550 2220 : nrefy == 2 ? 0
329bed06 2221 : (ascent + descent) / 2));
b4192550
KH
2222 }
2223
2224 cmp->offsets[i * 2] = left;
329bed06 2225 cmp->offsets[i * 2 + 1] = btm + descent;
b4192550
KH
2226
2227 /* Update the bounding box of the overall glyphs. */
329bed06
GM
2228 right = left + width;
2229 top = btm + descent + ascent;
b4192550
KH
2230 if (left < leftmost)
2231 leftmost = left;
2232 if (right > rightmost)
2233 rightmost = right;
2234 if (top > highest)
2235 highest = top;
2236 if (btm < lowest)
2237 lowest = btm;
2238 }
2239
2240 /* If there are glyphs whose x-offsets are negative,
2241 shift all glyphs to the right and make all x-offsets
2242 non-negative. */
2243 if (leftmost < 0)
2244 {
2245 for (i = 0; i < cmp->glyph_len; i++)
2246 cmp->offsets[i * 2] -= leftmost;
2247 rightmost -= leftmost;
2248 }
2249
2250 cmp->pixel_width = rightmost;
2251 cmp->ascent = highest;
2252 cmp->descent = - lowest;
2253 if (cmp->ascent < font_ascent)
2254 cmp->ascent = font_ascent;
2255 if (cmp->descent < font_descent)
2256 cmp->descent = font_descent;
2257 }
2258
2259 it->pixel_width = cmp->pixel_width;
2260 it->ascent = it->phys_ascent = cmp->ascent;
2261 it->descent = it->phys_descent = cmp->descent;
2262
2263 if (face->box != FACE_NO_BOX)
2264 {
2265 int thick = face->box_line_width;
2266 it->ascent += thick;
2267 it->descent += thick;
2268
2269 if (it->start_of_box_run_p)
2270 it->pixel_width += thick;
2271 if (it->end_of_box_run_p)
2272 it->pixel_width += thick;
2273 }
2274
2275 /* If face has an overline, add the height of the overline
2276 (1 pixel) and a 1 pixel margin to the character height. */
2277 if (face->overline_p)
2278 it->ascent += 2;
2279
2280 take_vertical_position_into_account (it);
2281
2282 if (it->glyph_row)
2283 x_append_composite_glyph (it);
2284 }
06a2c219
GM
2285 else if (it->what == IT_IMAGE)
2286 x_produce_image_glyph (it);
2287 else if (it->what == IT_STRETCH)
2288 x_produce_stretch_glyph (it);
2289
3017fdd1
GM
2290 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2291 because this isn't true for images with `:ascent 100'. */
2292 xassert (it->ascent >= 0 && it->descent >= 0);
06a2c219
GM
2293 if (it->area == TEXT_AREA)
2294 it->current_x += it->pixel_width;
66ac4b0e 2295
d365f5bb
GM
2296 it->descent += it->extra_line_spacing;
2297
06a2c219
GM
2298 it->max_ascent = max (it->max_ascent, it->ascent);
2299 it->max_descent = max (it->max_descent, it->descent);
66ac4b0e
GM
2300 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2301 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
06a2c219
GM
2302}
2303
2304
2305/* Estimate the pixel height of the mode or top line on frame F.
2306 FACE_ID specifies what line's height to estimate. */
2307
2308int
2309x_estimate_mode_line_height (f, face_id)
2310 struct frame *f;
2311 enum face_id face_id;
2312{
2313 int height = 1;
2314
2315 /* This function is called so early when Emacs starts that the face
2316 cache and mode line face are not yet initialized. */
2317 if (FRAME_FACE_CACHE (f))
2318 {
2319 struct face *face = FACE_FROM_ID (f, face_id);
2320 if (face)
2321 height = FONT_HEIGHT (face->font) + 2 * face->box_line_width;
2322 }
2323
2324 return height;
2325}
2326
2327\f
2328/***********************************************************************
2329 Glyph display
2330 ***********************************************************************/
2331
2332/* A sequence of glyphs to be drawn in the same face.
2333
2334 This data structure is not really completely X specific, so it
2335 could possibly, at least partially, be useful for other systems. It
2336 is currently not part of the external redisplay interface because
2337 it's not clear what other systems will need. */
2338
2339struct glyph_string
2340{
2341 /* X-origin of the string. */
2342 int x;
2343
2344 /* Y-origin and y-position of the base line of this string. */
2345 int y, ybase;
2346
2347 /* The width of the string, not including a face extension. */
2348 int width;
2349
2350 /* The width of the string, including a face extension. */
2351 int background_width;
2352
2353 /* The height of this string. This is the height of the line this
2354 string is drawn in, and can be different from the height of the
2355 font the string is drawn in. */
2356 int height;
2357
2358 /* Number of pixels this string overwrites in front of its x-origin.
2359 This number is zero if the string has an lbearing >= 0; it is
2360 -lbearing, if the string has an lbearing < 0. */
2361 int left_overhang;
2362
2363 /* Number of pixels this string overwrites past its right-most
2364 nominal x-position, i.e. x + width. Zero if the string's
2365 rbearing is <= its nominal width, rbearing - width otherwise. */
2366 int right_overhang;
2367
2368 /* The frame on which the glyph string is drawn. */
2369 struct frame *f;
2370
2371 /* The window on which the glyph string is drawn. */
2372 struct window *w;
2373
2374 /* X display and window for convenience. */
2375 Display *display;
2376 Window window;
2377
2378 /* The glyph row for which this string was built. It determines the
2379 y-origin and height of the string. */
2380 struct glyph_row *row;
2381
2382 /* The area within row. */
2383 enum glyph_row_area area;
2384
2385 /* Characters to be drawn, and number of characters. */
2386 XChar2b *char2b;
2387 int nchars;
2388
06a2c219
GM
2389 /* A face-override for drawing cursors, mouse face and similar. */
2390 enum draw_glyphs_face hl;
2391
2392 /* Face in which this string is to be drawn. */
2393 struct face *face;
2394
2395 /* Font in which this string is to be drawn. */
2396 XFontStruct *font;
2397
2398 /* Font info for this string. */
2399 struct font_info *font_info;
2400
b4192550
KH
2401 /* Non-null means this string describes (part of) a composition.
2402 All characters from char2b are drawn composed. */
2403 struct composition *cmp;
06a2c219
GM
2404
2405 /* Index of this glyph string's first character in the glyph
b4192550
KH
2406 definition of CMP. If this is zero, this glyph string describes
2407 the first character of a composition. */
06a2c219
GM
2408 int gidx;
2409
2410 /* 1 means this glyph strings face has to be drawn to the right end
2411 of the window's drawing area. */
2412 unsigned extends_to_end_of_line_p : 1;
2413
2414 /* 1 means the background of this string has been drawn. */
2415 unsigned background_filled_p : 1;
2416
2417 /* 1 means glyph string must be drawn with 16-bit functions. */
2418 unsigned two_byte_p : 1;
2419
2420 /* 1 means that the original font determined for drawing this glyph
2421 string could not be loaded. The member `font' has been set to
2422 the frame's default font in this case. */
2423 unsigned font_not_found_p : 1;
2424
2425 /* 1 means that the face in which this glyph string is drawn has a
2426 stipple pattern. */
2427 unsigned stippled_p : 1;
2428
66ac4b0e
GM
2429 /* 1 means only the foreground of this glyph string must be drawn,
2430 and we should use the physical height of the line this glyph
2431 string appears in as clip rect. */
2432 unsigned for_overlaps_p : 1;
2433
06a2c219
GM
2434 /* The GC to use for drawing this glyph string. */
2435 GC gc;
2436
2437 /* A pointer to the first glyph in the string. This glyph
2438 corresponds to char2b[0]. Needed to draw rectangles if
2439 font_not_found_p is 1. */
2440 struct glyph *first_glyph;
2441
2442 /* Image, if any. */
2443 struct image *img;
2444
2445 struct glyph_string *next, *prev;
2446};
2447
2448
5c187dee 2449#if 0
06a2c219
GM
2450
2451static void
2452x_dump_glyph_string (s)
2453 struct glyph_string *s;
2454{
2455 fprintf (stderr, "glyph string\n");
2456 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2457 s->x, s->y, s->width, s->height);
2458 fprintf (stderr, " ybase = %d\n", s->ybase);
2459 fprintf (stderr, " hl = %d\n", s->hl);
2460 fprintf (stderr, " left overhang = %d, right = %d\n",
2461 s->left_overhang, s->right_overhang);
2462 fprintf (stderr, " nchars = %d\n", s->nchars);
2463 fprintf (stderr, " extends to end of line = %d\n",
2464 s->extends_to_end_of_line_p);
2465 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2466 fprintf (stderr, " bg width = %d\n", s->background_width);
2467}
2468
2469#endif /* GLYPH_DEBUG */
2470
2471
2472
2473static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2474 struct glyph_string **,
2475 struct glyph_string *,
2476 struct glyph_string *));
2477static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2478 struct glyph_string **,
2479 struct glyph_string *,
2480 struct glyph_string *));
2481static void x_append_glyph_string P_ ((struct glyph_string **,
2482 struct glyph_string **,
2483 struct glyph_string *));
2484static int x_left_overwritten P_ ((struct glyph_string *));
2485static int x_left_overwriting P_ ((struct glyph_string *));
2486static int x_right_overwritten P_ ((struct glyph_string *));
2487static int x_right_overwriting P_ ((struct glyph_string *));
66ac4b0e
GM
2488static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2489 int));
06a2c219
GM
2490static void x_init_glyph_string P_ ((struct glyph_string *,
2491 XChar2b *, struct window *,
2492 struct glyph_row *,
2493 enum glyph_row_area, int,
2494 enum draw_glyphs_face));
2495static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2496 enum glyph_row_area, int, int,
66ac4b0e 2497 enum draw_glyphs_face, int *, int *, int));
06a2c219
GM
2498static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2499static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2500static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2501 int));
2502static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
b4192550 2503static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
06a2c219
GM
2504static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2505static void x_draw_glyph_string P_ ((struct glyph_string *));
2506static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2507static void x_set_cursor_gc P_ ((struct glyph_string *));
2508static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2509static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2510static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2511 int *, int *));
2512static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2513static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
68c45bf0 2514 unsigned long *, double, int));
06a2c219 2515static void x_setup_relief_color P_ ((struct frame *, struct relief *,
68c45bf0 2516 double, int, unsigned long));
06a2c219
GM
2517static void x_setup_relief_colors P_ ((struct glyph_string *));
2518static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2519static void x_draw_image_relief P_ ((struct glyph_string *));
2520static void x_draw_image_foreground P_ ((struct glyph_string *));
2521static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2522static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2523static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2524 int, int, int));
2525static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2526 int, int, int, int, XRectangle *));
2527static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2528 int, int, int, XRectangle *));
66ac4b0e
GM
2529static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2530 enum glyph_row_area));
209f68d9
GM
2531static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2532 struct glyph_row *,
2533 enum glyph_row_area, int, int));
06a2c219 2534
163dcff3
GM
2535#if GLYPH_DEBUG
2536static void x_check_font P_ ((struct frame *, XFontStruct *));
2537#endif
2538
06a2c219 2539
06a2c219
GM
2540/* Append the list of glyph strings with head H and tail T to the list
2541 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2542
2543static INLINE void
2544x_append_glyph_string_lists (head, tail, h, t)
2545 struct glyph_string **head, **tail;
2546 struct glyph_string *h, *t;
2547{
2548 if (h)
2549 {
2550 if (*head)
2551 (*tail)->next = h;
2552 else
2553 *head = h;
2554 h->prev = *tail;
2555 *tail = t;
2556 }
2557}
2558
2559
2560/* Prepend the list of glyph strings with head H and tail T to the
2561 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2562 result. */
2563
2564static INLINE void
2565x_prepend_glyph_string_lists (head, tail, h, t)
2566 struct glyph_string **head, **tail;
2567 struct glyph_string *h, *t;
2568{
2569 if (h)
2570 {
2571 if (*head)
2572 (*head)->prev = t;
2573 else
2574 *tail = t;
2575 t->next = *head;
2576 *head = h;
2577 }
2578}
2579
2580
2581/* Append glyph string S to the list with head *HEAD and tail *TAIL.
2582 Set *HEAD and *TAIL to the resulting list. */
2583
2584static INLINE void
2585x_append_glyph_string (head, tail, s)
2586 struct glyph_string **head, **tail;
2587 struct glyph_string *s;
2588{
2589 s->next = s->prev = NULL;
2590 x_append_glyph_string_lists (head, tail, s, s);
2591}
2592
2593
2594/* Set S->gc to a suitable GC for drawing glyph string S in cursor
2595 face. */
2596
2597static void
2598x_set_cursor_gc (s)
2599 struct glyph_string *s;
2600{
2601 if (s->font == FRAME_FONT (s->f)
2602 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2603 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
b4192550 2604 && !s->cmp)
06a2c219
GM
2605 s->gc = s->f->output_data.x->cursor_gc;
2606 else
2607 {
2608 /* Cursor on non-default face: must merge. */
2609 XGCValues xgcv;
2610 unsigned long mask;
2611
2612 xgcv.background = s->f->output_data.x->cursor_pixel;
2613 xgcv.foreground = s->face->background;
2614
2615 /* If the glyph would be invisible, try a different foreground. */
2616 if (xgcv.foreground == xgcv.background)
2617 xgcv.foreground = s->face->foreground;
2618 if (xgcv.foreground == xgcv.background)
2619 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2620 if (xgcv.foreground == xgcv.background)
2621 xgcv.foreground = s->face->foreground;
2622
2623 /* Make sure the cursor is distinct from text in this face. */
2624 if (xgcv.background == s->face->background
2625 && xgcv.foreground == s->face->foreground)
2626 {
2627 xgcv.background = s->face->foreground;
2628 xgcv.foreground = s->face->background;
2629 }
2630
2631 IF_DEBUG (x_check_font (s->f, s->font));
2632 xgcv.font = s->font->fid;
2633 xgcv.graphics_exposures = False;
2634 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2635
2636 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2637 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2638 mask, &xgcv);
2639 else
2640 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2641 = XCreateGC (s->display, s->window, mask, &xgcv);
2642
2643 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2644 }
2645}
2646
2647
2648/* Set up S->gc of glyph string S for drawing text in mouse face. */
2649
2650static void
2651x_set_mouse_face_gc (s)
2652 struct glyph_string *s;
2653{
2654 int face_id;
ee569018 2655 struct face *face;
06a2c219
GM
2656
2657 /* What face has to be used for the mouse face? */
2658 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
ee569018 2659 face = FACE_FROM_ID (s->f, face_id);
033e3e18
GM
2660 if (s->first_glyph->type == CHAR_GLYPH)
2661 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2662 else
2663 face_id = FACE_FOR_CHAR (s->f, face, 0);
06a2c219
GM
2664 s->face = FACE_FROM_ID (s->f, face_id);
2665 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2666
2667 /* If font in this face is same as S->font, use it. */
2668 if (s->font == s->face->font)
2669 s->gc = s->face->gc;
2670 else
2671 {
2672 /* Otherwise construct scratch_cursor_gc with values from FACE
2673 but font FONT. */
2674 XGCValues xgcv;
2675 unsigned long mask;
2676
2677 xgcv.background = s->face->background;
2678 xgcv.foreground = s->face->foreground;
2679 IF_DEBUG (x_check_font (s->f, s->font));
2680 xgcv.font = s->font->fid;
2681 xgcv.graphics_exposures = False;
2682 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2683
2684 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2685 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2686 mask, &xgcv);
2687 else
2688 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2689 = XCreateGC (s->display, s->window, mask, &xgcv);
2690
2691 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2692 }
2693
2694 xassert (s->gc != 0);
2695}
2696
2697
2698/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2699 Faces to use in the mode line have already been computed when the
2700 matrix was built, so there isn't much to do, here. */
2701
2702static INLINE void
2703x_set_mode_line_face_gc (s)
2704 struct glyph_string *s;
2705{
2706 s->gc = s->face->gc;
06a2c219
GM
2707}
2708
2709
2710/* Set S->gc of glyph string S for drawing that glyph string. Set
2711 S->stippled_p to a non-zero value if the face of S has a stipple
2712 pattern. */
2713
2714static INLINE void
2715x_set_glyph_string_gc (s)
2716 struct glyph_string *s;
2717{
209f68d9
GM
2718 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2719
06a2c219
GM
2720 if (s->hl == DRAW_NORMAL_TEXT)
2721 {
2722 s->gc = s->face->gc;
2723 s->stippled_p = s->face->stipple != 0;
2724 }
2725 else if (s->hl == DRAW_INVERSE_VIDEO)
2726 {
2727 x_set_mode_line_face_gc (s);
2728 s->stippled_p = s->face->stipple != 0;
2729 }
2730 else if (s->hl == DRAW_CURSOR)
2731 {
2732 x_set_cursor_gc (s);
2733 s->stippled_p = 0;
2734 }
2735 else if (s->hl == DRAW_MOUSE_FACE)
2736 {
2737 x_set_mouse_face_gc (s);
2738 s->stippled_p = s->face->stipple != 0;
2739 }
2740 else if (s->hl == DRAW_IMAGE_RAISED
2741 || s->hl == DRAW_IMAGE_SUNKEN)
2742 {
2743 s->gc = s->face->gc;
2744 s->stippled_p = s->face->stipple != 0;
2745 }
2746 else
2747 {
2748 s->gc = s->face->gc;
2749 s->stippled_p = s->face->stipple != 0;
2750 }
2751
2752 /* GC must have been set. */
2753 xassert (s->gc != 0);
2754}
2755
2756
2757/* Return in *R the clipping rectangle for glyph string S. */
2758
2759static void
2760x_get_glyph_string_clip_rect (s, r)
2761 struct glyph_string *s;
2762 XRectangle *r;
2763{
2764 if (s->row->full_width_p)
2765 {
2766 /* Draw full-width. X coordinates are relative to S->w->left. */
1da3fd71
GM
2767 int canon_x = CANON_X_UNIT (s->f);
2768
2769 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2770 r->width = XFASTINT (s->w->width) * canon_x;
06a2c219
GM
2771
2772 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2773 {
1da3fd71 2774 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
06a2c219
GM
2775 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2776 r->x -= width;
2777 }
2778
b9432a85 2779 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
1da3fd71 2780
06a2c219
GM
2781 /* Unless displaying a mode or menu bar line, which are always
2782 fully visible, clip to the visible part of the row. */
2783 if (s->w->pseudo_window_p)
2784 r->height = s->row->visible_height;
2785 else
2786 r->height = s->height;
2787 }
2788 else
2789 {
2790 /* This is a text line that may be partially visible. */
2791 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2792 r->width = window_box_width (s->w, s->area);
2793 r->height = s->row->visible_height;
2794 }
2795
2796 /* Don't use S->y for clipping because it doesn't take partially
2797 visible lines into account. For example, it can be negative for
2798 partially visible lines at the top of a window. */
2799 if (!s->row->full_width_p
2800 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
045dee35 2801 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
06a2c219
GM
2802 else
2803 r->y = max (0, s->row->y);
06a2c219 2804
9ea173e8 2805 /* If drawing a tool-bar window, draw it over the internal border
06a2c219 2806 at the top of the window. */
9ea173e8 2807 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219 2808 r->y -= s->f->output_data.x->internal_border_width;
66ac4b0e
GM
2809
2810 /* If S draws overlapping rows, it's sufficient to use the top and
2811 bottom of the window for clipping because this glyph string
2812 intentionally draws over other lines. */
2813 if (s->for_overlaps_p)
2814 {
045dee35 2815 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
66ac4b0e
GM
2816 r->height = window_text_bottom_y (s->w) - r->y;
2817 }
2818
2819 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
06a2c219
GM
2820}
2821
2822
2823/* Set clipping for output of glyph string S. S may be part of a mode
2824 line or menu if we don't have X toolkit support. */
2825
2826static INLINE void
2827x_set_glyph_string_clipping (s)
2828 struct glyph_string *s;
2829{
2830 XRectangle r;
2831 x_get_glyph_string_clip_rect (s, &r);
2832 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2833}
2834
2835
2836/* Compute left and right overhang of glyph string S. If S is a glyph
b4192550 2837 string for a composition, assume overhangs don't exist. */
06a2c219
GM
2838
2839static INLINE void
2840x_compute_glyph_string_overhangs (s)
2841 struct glyph_string *s;
2842{
b4192550 2843 if (s->cmp == NULL
06a2c219
GM
2844 && s->first_glyph->type == CHAR_GLYPH)
2845 {
2846 XCharStruct cs;
2847 int direction, font_ascent, font_descent;
2848 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2849 &font_ascent, &font_descent, &cs);
2850 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2851 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2852 }
2853}
2854
2855
2856/* Compute overhangs and x-positions for glyph string S and its
2857 predecessors, or successors. X is the starting x-position for S.
2858 BACKWARD_P non-zero means process predecessors. */
2859
2860static void
2861x_compute_overhangs_and_x (s, x, backward_p)
2862 struct glyph_string *s;
2863 int x;
2864 int backward_p;
2865{
2866 if (backward_p)
2867 {
2868 while (s)
2869 {
2870 x_compute_glyph_string_overhangs (s);
2871 x -= s->width;
2872 s->x = x;
2873 s = s->prev;
2874 }
2875 }
2876 else
2877 {
2878 while (s)
2879 {
2880 x_compute_glyph_string_overhangs (s);
2881 s->x = x;
2882 x += s->width;
2883 s = s->next;
2884 }
2885 }
2886}
2887
2888
2889/* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
b4192550
KH
2890 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2891 assumed to be zero. */
06a2c219
GM
2892
2893static void
2894x_get_glyph_overhangs (glyph, f, left, right)
2895 struct glyph *glyph;
2896 struct frame *f;
2897 int *left, *right;
2898{
06a2c219
GM
2899 *left = *right = 0;
2900
b4192550 2901 if (glyph->type == CHAR_GLYPH)
06a2c219
GM
2902 {
2903 XFontStruct *font;
2904 struct face *face;
2905 struct font_info *font_info;
2906 XChar2b char2b;
ee569018
KH
2907 XCharStruct *pcm;
2908
2909 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
06a2c219
GM
2910 font = face->font;
2911 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
ee569018
KH
2912 if (font
2913 && (pcm = x_per_char_metric (font, &char2b)))
06a2c219 2914 {
06a2c219
GM
2915 if (pcm->rbearing > pcm->width)
2916 *right = pcm->rbearing - pcm->width;
2917 if (pcm->lbearing < 0)
2918 *left = -pcm->lbearing;
2919 }
2920 }
2921}
2922
2923
2924/* Return the index of the first glyph preceding glyph string S that
2925 is overwritten by S because of S's left overhang. Value is -1
2926 if no glyphs are overwritten. */
2927
2928static int
2929x_left_overwritten (s)
2930 struct glyph_string *s;
2931{
2932 int k;
2933
2934 if (s->left_overhang)
2935 {
2936 int x = 0, i;
2937 struct glyph *glyphs = s->row->glyphs[s->area];
2938 int first = s->first_glyph - glyphs;
2939
2940 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2941 x -= glyphs[i].pixel_width;
2942
2943 k = i + 1;
2944 }
2945 else
2946 k = -1;
2947
2948 return k;
2949}
2950
2951
2952/* Return the index of the first glyph preceding glyph string S that
2953 is overwriting S because of its right overhang. Value is -1 if no
2954 glyph in front of S overwrites S. */
2955
2956static int
2957x_left_overwriting (s)
2958 struct glyph_string *s;
2959{
2960 int i, k, x;
2961 struct glyph *glyphs = s->row->glyphs[s->area];
2962 int first = s->first_glyph - glyphs;
2963
2964 k = -1;
2965 x = 0;
2966 for (i = first - 1; i >= 0; --i)
2967 {
2968 int left, right;
2969 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2970 if (x + right > 0)
2971 k = i;
2972 x -= glyphs[i].pixel_width;
2973 }
2974
2975 return k;
2976}
2977
2978
2979/* Return the index of the last glyph following glyph string S that is
2980 not overwritten by S because of S's right overhang. Value is -1 if
2981 no such glyph is found. */
2982
2983static int
2984x_right_overwritten (s)
2985 struct glyph_string *s;
2986{
2987 int k = -1;
2988
2989 if (s->right_overhang)
2990 {
2991 int x = 0, i;
2992 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 2993 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
2994 int end = s->row->used[s->area];
2995
2996 for (i = first; i < end && s->right_overhang > x; ++i)
2997 x += glyphs[i].pixel_width;
2998
2999 k = i;
3000 }
3001
3002 return k;
3003}
3004
3005
3006/* Return the index of the last glyph following glyph string S that
3007 overwrites S because of its left overhang. Value is negative
3008 if no such glyph is found. */
3009
3010static int
3011x_right_overwriting (s)
3012 struct glyph_string *s;
3013{
3014 int i, k, x;
3015 int end = s->row->used[s->area];
3016 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 3017 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
3018
3019 k = -1;
3020 x = 0;
3021 for (i = first; i < end; ++i)
3022 {
3023 int left, right;
3024 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3025 if (x - left < 0)
3026 k = i;
3027 x += glyphs[i].pixel_width;
3028 }
3029
3030 return k;
3031}
3032
3033
3034/* Fill rectangle X, Y, W, H with background color of glyph string S. */
3035
3036static INLINE void
3037x_clear_glyph_string_rect (s, x, y, w, h)
3038 struct glyph_string *s;
3039 int x, y, w, h;
3040{
3041 XGCValues xgcv;
3042 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3043 XSetForeground (s->display, s->gc, xgcv.background);
3044 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3045 XSetForeground (s->display, s->gc, xgcv.foreground);
3046}
3047
3048
3049/* Draw the background of glyph_string S. If S->background_filled_p
3050 is non-zero don't draw it. FORCE_P non-zero means draw the
3051 background even if it wouldn't be drawn normally. This is used
b4192550
KH
3052 when a string preceding S draws into the background of S, or S
3053 contains the first component of a composition. */
06a2c219
GM
3054
3055static void
3056x_draw_glyph_string_background (s, force_p)
3057 struct glyph_string *s;
3058 int force_p;
3059{
3060 /* Nothing to do if background has already been drawn or if it
3061 shouldn't be drawn in the first place. */
3062 if (!s->background_filled_p)
3063 {
b4192550 3064 if (s->stippled_p)
06a2c219
GM
3065 {
3066 /* Fill background with a stipple pattern. */
3067 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3068 XFillRectangle (s->display, s->window, s->gc, s->x,
3069 s->y + s->face->box_line_width,
3070 s->background_width,
3071 s->height - 2 * s->face->box_line_width);
3072 XSetFillStyle (s->display, s->gc, FillSolid);
3073 s->background_filled_p = 1;
3074 }
3075 else if (FONT_HEIGHT (s->font) < s->height - 2 * s->face->box_line_width
3076 || s->font_not_found_p
3077 || s->extends_to_end_of_line_p
06a2c219
GM
3078 || force_p)
3079 {
3080 x_clear_glyph_string_rect (s, s->x, s->y + s->face->box_line_width,
3081 s->background_width,
3082 s->height - 2 * s->face->box_line_width);
3083 s->background_filled_p = 1;
3084 }
3085 }
3086}
3087
3088
3089/* Draw the foreground of glyph string S. */
3090
3091static void
3092x_draw_glyph_string_foreground (s)
3093 struct glyph_string *s;
3094{
3095 int i, x;
3096
3097 /* If first glyph of S has a left box line, start drawing the text
3098 of S to the right of that box line. */
3099 if (s->face->box != FACE_NO_BOX
3100 && s->first_glyph->left_box_line_p)
3101 x = s->x + s->face->box_line_width;
3102 else
3103 x = s->x;
3104
b4192550
KH
3105 /* Draw characters of S as rectangles if S's font could not be
3106 loaded. */
3107 if (s->font_not_found_p)
06a2c219 3108 {
b4192550 3109 for (i = 0; i < s->nchars; ++i)
06a2c219 3110 {
b4192550
KH
3111 struct glyph *g = s->first_glyph + i;
3112 XDrawRectangle (s->display, s->window,
3113 s->gc, x, s->y, g->pixel_width - 1,
3114 s->height - 1);
3115 x += g->pixel_width;
06a2c219
GM
3116 }
3117 }
3118 else
3119 {
b4192550
KH
3120 char *char1b = (char *) s->char2b;
3121 int boff = s->font_info->baseline_offset;
06a2c219 3122
b4192550
KH
3123 if (s->font_info->vertical_centering)
3124 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3125
3126 /* If we can use 8-bit functions, condense S->char2b. */
3127 if (!s->two_byte_p)
3128 for (i = 0; i < s->nchars; ++i)
3129 char1b[i] = s->char2b[i].byte2;
3130
3131 /* Draw text with XDrawString if background has already been
3132 filled. Otherwise, use XDrawImageString. (Note that
3133 XDrawImageString is usually faster than XDrawString.) Always
3134 use XDrawImageString when drawing the cursor so that there is
3135 no chance that characters under a box cursor are invisible. */
3136 if (s->for_overlaps_p
3137 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3138 {
3139 /* Draw characters with 16-bit or 8-bit functions. */
3140 if (s->two_byte_p)
3141 XDrawString16 (s->display, s->window, s->gc, x,
3142 s->ybase - boff, s->char2b, s->nchars);
3143 else
3144 XDrawString (s->display, s->window, s->gc, x,
3145 s->ybase - boff, char1b, s->nchars);
3146 }
06a2c219
GM
3147 else
3148 {
b4192550
KH
3149 if (s->two_byte_p)
3150 XDrawImageString16 (s->display, s->window, s->gc, x,
3151 s->ybase - boff, s->char2b, s->nchars);
06a2c219 3152 else
b4192550
KH
3153 XDrawImageString (s->display, s->window, s->gc, x,
3154 s->ybase - boff, char1b, s->nchars);
3155 }
3156 }
3157}
06a2c219 3158
b4192550 3159/* Draw the foreground of composite glyph string S. */
06a2c219 3160
b4192550
KH
3161static void
3162x_draw_composite_glyph_string_foreground (s)
3163 struct glyph_string *s;
3164{
3165 int i, x;
06a2c219 3166
b4192550
KH
3167 /* If first glyph of S has a left box line, start drawing the text
3168 of S to the right of that box line. */
3169 if (s->face->box != FACE_NO_BOX
3170 && s->first_glyph->left_box_line_p)
3171 x = s->x + s->face->box_line_width;
3172 else
3173 x = s->x;
06a2c219 3174
b4192550
KH
3175 /* S is a glyph string for a composition. S->gidx is the index of
3176 the first character drawn for glyphs of this composition.
3177 S->gidx == 0 means we are drawing the very first character of
3178 this composition. */
06a2c219 3179
b4192550
KH
3180 /* Draw a rectangle for the composition if the font for the very
3181 first character of the composition could not be loaded. */
3182 if (s->font_not_found_p)
3183 {
3184 if (s->gidx == 0)
3185 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3186 s->width - 1, s->height - 1);
3187 }
3188 else
3189 {
3190 for (i = 0; i < s->nchars; i++, ++s->gidx)
3191 XDrawString16 (s->display, s->window, s->gc,
3192 x + s->cmp->offsets[s->gidx * 2],
3193 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3194 s->char2b + i, 1);
06a2c219
GM
3195 }
3196}
3197
3198
80c32bcc
GM
3199#ifdef USE_X_TOOLKIT
3200
3e71d8f2 3201static struct frame *x_frame_of_widget P_ ((Widget));
80c32bcc 3202
3e71d8f2
GM
3203
3204/* Return the frame on which widget WIDGET is used.. Abort if frame
3205 cannot be determined. */
3206
e851c833 3207static struct frame *
3e71d8f2 3208x_frame_of_widget (widget)
80c32bcc 3209 Widget widget;
80c32bcc 3210{
80c32bcc 3211 struct x_display_info *dpyinfo;
5c187dee 3212 Lisp_Object tail;
3e71d8f2
GM
3213 struct frame *f;
3214
80c32bcc
GM
3215 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3216
3217 /* Find the top-level shell of the widget. Note that this function
3218 can be called when the widget is not yet realized, so XtWindow
3219 (widget) == 0. That's the reason we can't simply use
3220 x_any_window_to_frame. */
3221 while (!XtIsTopLevelShell (widget))
3222 widget = XtParent (widget);
3223
3224 /* Look for a frame with that top-level widget. Allocate the color
3225 on that frame to get the right gamma correction value. */
3226 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3227 if (GC_FRAMEP (XCAR (tail))
3228 && (f = XFRAME (XCAR (tail)),
3229 (f->output_data.nothing != 1
3230 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3231 && f->output_data.x->widget == widget)
3e71d8f2 3232 return f;
80c32bcc
GM
3233
3234 abort ();
3235}
3236
3e71d8f2
GM
3237
3238/* Allocate the color COLOR->pixel on the screen and display of
3239 widget WIDGET in colormap CMAP. If an exact match cannot be
3240 allocated, try the nearest color available. Value is non-zero
3241 if successful. This is called from lwlib. */
3242
3243int
3244x_alloc_nearest_color_for_widget (widget, cmap, color)
3245 Widget widget;
3246 Colormap cmap;
3247 XColor *color;
3248{
3249 struct frame *f = x_frame_of_widget (widget);
3250 return x_alloc_nearest_color (f, cmap, color);
3251}
3252
3253
80c32bcc
GM
3254#endif /* USE_X_TOOLKIT */
3255
3256
06a2c219
GM
3257/* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3258 CMAP. If an exact match can't be allocated, try the nearest color
3259 available. Value is non-zero if successful. Set *COLOR to the
3260 color allocated. */
3261
3262int
80c32bcc
GM
3263x_alloc_nearest_color (f, cmap, color)
3264 struct frame *f;
06a2c219
GM
3265 Colormap cmap;
3266 XColor *color;
3267{
80c32bcc
GM
3268 Display *display = FRAME_X_DISPLAY (f);
3269 Screen *screen = FRAME_X_SCREEN (f);
3270 int rc;
3271
3272 gamma_correct (f, color);
3273 rc = XAllocColor (display, cmap, color);
06a2c219
GM
3274 if (rc == 0)
3275 {
3276 /* If we got to this point, the colormap is full, so we're going
3277 to try to get the next closest color. The algorithm used is
3278 a least-squares matching, which is what X uses for closest
3279 color matching with StaticColor visuals. */
3280 int nearest, i;
3281 unsigned long nearest_delta = ~0;
3282 int ncells = XDisplayCells (display, XScreenNumberOfScreen (screen));
3283 XColor *cells = (XColor *) alloca (ncells * sizeof *cells);
3284
3285 for (i = 0; i < ncells; ++i)
3286 cells[i].pixel = i;
3287 XQueryColors (display, cmap, cells, ncells);
3288
3289 for (nearest = i = 0; i < ncells; ++i)
3290 {
3291 long dred = (color->red >> 8) - (cells[i].red >> 8);
3292 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3293 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3294 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3295
3296 if (delta < nearest_delta)
3297 {
3298 nearest = i;
3299 nearest_delta = delta;
3300 }
3301 }
3302
3303 color->red = cells[nearest].red;
3304 color->green = cells[nearest].green;
3305 color->blue = cells[nearest].blue;
3306 rc = XAllocColor (display, cmap, color);
3307 }
3308
d9c545da
GM
3309#ifdef DEBUG_X_COLORS
3310 if (rc)
3311 register_color (color->pixel);
3312#endif /* DEBUG_X_COLORS */
3313
06a2c219
GM
3314 return rc;
3315}
3316
3317
d9c545da
GM
3318/* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3319 It's necessary to do this instead of just using PIXEL directly to
3320 get color reference counts right. */
3321
3322unsigned long
3323x_copy_color (f, pixel)
3324 struct frame *f;
3325 unsigned long pixel;
3326{
3327 XColor color;
3328
3329 color.pixel = pixel;
3330 BLOCK_INPUT;
3331 XQueryColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3332 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3333 UNBLOCK_INPUT;
3334#ifdef DEBUG_X_COLORS
3335 register_color (pixel);
3336#endif
3337 return color.pixel;
3338}
3339
3340
3e71d8f2
GM
3341/* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3342 It's necessary to do this instead of just using PIXEL directly to
3343 get color reference counts right. */
3344
3345unsigned long
3346x_copy_dpy_color (dpy, cmap, pixel)
3347 Display *dpy;
3348 Colormap cmap;
3349 unsigned long pixel;
3350{
3351 XColor color;
3352
3353 color.pixel = pixel;
3354 BLOCK_INPUT;
3355 XQueryColor (dpy, cmap, &color);
3356 XAllocColor (dpy, cmap, &color);
3357 UNBLOCK_INPUT;
3358#ifdef DEBUG_X_COLORS
3359 register_color (pixel);
3360#endif
3361 return color.pixel;
3362}
3363
3364
06a2c219
GM
3365/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3366 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3367 If this produces the same color as PIXEL, try a color where all RGB
3368 values have DELTA added. Return the allocated color in *PIXEL.
3369 DISPLAY is the X display, CMAP is the colormap to operate on.
3370 Value is non-zero if successful. */
3371
3372static int
3373x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3374 struct frame *f;
3375 Display *display;
3376 Colormap cmap;
3377 unsigned long *pixel;
68c45bf0 3378 double factor;
06a2c219
GM
3379 int delta;
3380{
3381 XColor color, new;
3382 int success_p;
3383
3384 /* Get RGB color values. */
3385 color.pixel = *pixel;
3386 XQueryColor (display, cmap, &color);
3387
3388 /* Change RGB values by specified FACTOR. Avoid overflow! */
3389 xassert (factor >= 0);
3390 new.red = min (0xffff, factor * color.red);
3391 new.green = min (0xffff, factor * color.green);
3392 new.blue = min (0xffff, factor * color.blue);
3393
3394 /* Try to allocate the color. */
80c32bcc 3395 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3396 if (success_p)
3397 {
3398 if (new.pixel == *pixel)
3399 {
3400 /* If we end up with the same color as before, try adding
3401 delta to the RGB values. */
0d605c67 3402 x_free_colors (f, &new.pixel, 1);
06a2c219
GM
3403
3404 new.red = min (0xffff, delta + color.red);
3405 new.green = min (0xffff, delta + color.green);
3406 new.blue = min (0xffff, delta + color.blue);
80c32bcc 3407 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3408 }
3409 else
3410 success_p = 1;
3411 *pixel = new.pixel;
3412 }
3413
3414 return success_p;
3415}
3416
3417
3418/* Set up the foreground color for drawing relief lines of glyph
3419 string S. RELIEF is a pointer to a struct relief containing the GC
3420 with which lines will be drawn. Use a color that is FACTOR or
3421 DELTA lighter or darker than the relief's background which is found
3422 in S->f->output_data.x->relief_background. If such a color cannot
3423 be allocated, use DEFAULT_PIXEL, instead. */
3424
3425static void
3426x_setup_relief_color (f, relief, factor, delta, default_pixel)
3427 struct frame *f;
3428 struct relief *relief;
68c45bf0 3429 double factor;
06a2c219
GM
3430 int delta;
3431 unsigned long default_pixel;
3432{
3433 XGCValues xgcv;
3434 struct x_output *di = f->output_data.x;
3435 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3436 unsigned long pixel;
3437 unsigned long background = di->relief_background;
43bd1b2b 3438 Colormap cmap = FRAME_X_COLORMAP (f);
dcd08bfb
GM
3439 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3440 Display *dpy = FRAME_X_DISPLAY (f);
06a2c219
GM
3441
3442 xgcv.graphics_exposures = False;
3443 xgcv.line_width = 1;
3444
3445 /* Free previously allocated color. The color cell will be reused
3446 when it has been freed as many times as it was allocated, so this
3447 doesn't affect faces using the same colors. */
3448 if (relief->gc
3449 && relief->allocated_p)
3450 {
0d605c67 3451 x_free_colors (f, &relief->pixel, 1);
06a2c219
GM
3452 relief->allocated_p = 0;
3453 }
3454
3455 /* Allocate new color. */
3456 xgcv.foreground = default_pixel;
3457 pixel = background;
dcd08bfb
GM
3458 if (dpyinfo->n_planes != 1
3459 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
06a2c219
GM
3460 {
3461 relief->allocated_p = 1;
3462 xgcv.foreground = relief->pixel = pixel;
3463 }
3464
3465 if (relief->gc == 0)
3466 {
dcd08bfb 3467 xgcv.stipple = dpyinfo->gray;
06a2c219 3468 mask |= GCStipple;
dcd08bfb 3469 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
06a2c219
GM
3470 }
3471 else
dcd08bfb 3472 XChangeGC (dpy, relief->gc, mask, &xgcv);
06a2c219
GM
3473}
3474
3475
3476/* Set up colors for the relief lines around glyph string S. */
3477
3478static void
3479x_setup_relief_colors (s)
3480 struct glyph_string *s;
3481{
3482 struct x_output *di = s->f->output_data.x;
3483 unsigned long color;
3484
3485 if (s->face->use_box_color_for_shadows_p)
3486 color = s->face->box_color;
3487 else
3488 {
3489 XGCValues xgcv;
3490
3491 /* Get the background color of the face. */
3492 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3493 color = xgcv.background;
3494 }
3495
3496 if (di->white_relief.gc == 0
3497 || color != di->relief_background)
3498 {
3499 di->relief_background = color;
3500 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3501 WHITE_PIX_DEFAULT (s->f));
3502 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3503 BLACK_PIX_DEFAULT (s->f));
3504 }
3505}
3506
3507
3508/* Draw a relief on frame F inside the rectangle given by LEFT_X,
3509 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3510 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3511 relief. LEFT_P non-zero means draw a relief on the left side of
3512 the rectangle. RIGHT_P non-zero means draw a relief on the right
3513 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3514 when drawing. */
3515
3516static void
3517x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3518 raised_p, left_p, right_p, clip_rect)
3519 struct frame *f;
3520 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3521 XRectangle *clip_rect;
3522{
3523 int i;
3524 GC gc;
3525
3526 if (raised_p)
3527 gc = f->output_data.x->white_relief.gc;
3528 else
3529 gc = f->output_data.x->black_relief.gc;
3530 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3531
3532 /* Top. */
3533 for (i = 0; i < width; ++i)
3534 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3535 left_x + i * left_p, top_y + i,
3536 right_x + 1 - i * right_p, top_y + i);
3537
3538 /* Left. */
3539 if (left_p)
3540 for (i = 0; i < width; ++i)
3541 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3542 left_x + i, top_y + i, left_x + i, bottom_y - i);
3543
3544 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3545 if (raised_p)
3546 gc = f->output_data.x->black_relief.gc;
3547 else
3548 gc = f->output_data.x->white_relief.gc;
3549 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3550
3551 /* Bottom. */
3552 for (i = 0; i < width; ++i)
3553 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3554 left_x + i * left_p, bottom_y - i,
3555 right_x + 1 - i * right_p, bottom_y - i);
3556
3557 /* Right. */
3558 if (right_p)
3559 for (i = 0; i < width; ++i)
3560 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3561 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3562
3563 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3564}
3565
3566
3567/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3568 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3569 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3570 left side of the rectangle. RIGHT_P non-zero means draw a line
3571 on the right side of the rectangle. CLIP_RECT is the clipping
3572 rectangle to use when drawing. */
3573
3574static void
3575x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3576 left_p, right_p, clip_rect)
3577 struct glyph_string *s;
3578 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3579 XRectangle *clip_rect;
3580{
3581 XGCValues xgcv;
3582
3583 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3584 XSetForeground (s->display, s->gc, s->face->box_color);
3585 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3586
3587 /* Top. */
3588 XFillRectangle (s->display, s->window, s->gc,
3589 left_x, top_y, right_x - left_x, width);
3590
3591 /* Left. */
3592 if (left_p)
3593 XFillRectangle (s->display, s->window, s->gc,
3594 left_x, top_y, width, bottom_y - top_y);
3595
3596 /* Bottom. */
3597 XFillRectangle (s->display, s->window, s->gc,
3598 left_x, bottom_y - width, right_x - left_x, width);
3599
3600 /* Right. */
3601 if (right_p)
3602 XFillRectangle (s->display, s->window, s->gc,
3603 right_x - width, top_y, width, bottom_y - top_y);
3604
3605 XSetForeground (s->display, s->gc, xgcv.foreground);
3606 XSetClipMask (s->display, s->gc, None);
3607}
3608
3609
3610/* Draw a box around glyph string S. */
3611
3612static void
3613x_draw_glyph_string_box (s)
3614 struct glyph_string *s;
3615{
3616 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3617 int left_p, right_p;
3618 struct glyph *last_glyph;
3619 XRectangle clip_rect;
3620
3621 last_x = window_box_right (s->w, s->area);
3622 if (s->row->full_width_p
3623 && !s->w->pseudo_window_p)
3624 {
110859fc 3625 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
06a2c219
GM
3626 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3627 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3628 }
3629
3630 /* The glyph that may have a right box line. */
b4192550 3631 last_glyph = (s->cmp || s->img
06a2c219
GM
3632 ? s->first_glyph
3633 : s->first_glyph + s->nchars - 1);
3634
3635 width = s->face->box_line_width;
3636 raised_p = s->face->box == FACE_RAISED_BOX;
3637 left_x = s->x;
3638 right_x = ((s->row->full_width_p
1da3fd71 3639 ? last_x - 1
a7aeb2de 3640 : min (last_x, s->x + s->background_width) - 1));
06a2c219
GM
3641 top_y = s->y;
3642 bottom_y = top_y + s->height - 1;
3643
3644 left_p = (s->first_glyph->left_box_line_p
3645 || (s->hl == DRAW_MOUSE_FACE
3646 && (s->prev == NULL
3647 || s->prev->hl != s->hl)));
3648 right_p = (last_glyph->right_box_line_p
3649 || (s->hl == DRAW_MOUSE_FACE
3650 && (s->next == NULL
3651 || s->next->hl != s->hl)));
3652
3653 x_get_glyph_string_clip_rect (s, &clip_rect);
3654
3655 if (s->face->box == FACE_SIMPLE_BOX)
3656 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3657 left_p, right_p, &clip_rect);
3658 else
3659 {
3660 x_setup_relief_colors (s);
3661 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3662 width, raised_p, left_p, right_p, &clip_rect);
3663 }
3664}
3665
3666
3667/* Draw foreground of image glyph string S. */
3668
3669static void
3670x_draw_image_foreground (s)
3671 struct glyph_string *s;
3672{
3673 int x;
95af8492 3674 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
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->x + s->face->box_line_width;
3681 else
3682 x = s->x;
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 }
3691
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 XRectangle clip_rect, image_rect, r;
3705
3706 xgcv.clip_mask = s->img->mask;
3707 xgcv.clip_x_origin = x;
3708 xgcv.clip_y_origin = y;
3709 xgcv.function = GXcopy;
3710 XChangeGC (s->display, s->gc, mask, &xgcv);
3711
3712 x_get_glyph_string_clip_rect (s, &clip_rect);
3713 image_rect.x = x;
3714 image_rect.y = y;
3715 image_rect.width = s->img->width;
3716 image_rect.height = s->img->height;
3717 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3718 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3719 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3720 }
3721 else
3722 {
49ad1d99
GM
3723 unsigned long mask = GCClipXOrigin | GCClipYOrigin | GCFunction;
3724 XGCValues xgcv;
3725 XRectangle clip_rect, image_rect, r;
3726
3727 x_get_glyph_string_clip_rect (s, &clip_rect);
3728 image_rect.x = x;
3729 image_rect.y = y;
3730 image_rect.width = s->img->width;
3731 image_rect.height = s->img->height;
3732 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3733 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3734 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
06a2c219
GM
3735
3736 /* When the image has a mask, we can expect that at
3737 least part of a mouse highlight or a block cursor will
3738 be visible. If the image doesn't have a mask, make
3739 a block cursor visible by drawing a rectangle around
3740 the image. I believe it's looking better if we do
3741 nothing here for mouse-face. */
3742 if (s->hl == DRAW_CURSOR)
3743 XDrawRectangle (s->display, s->window, s->gc, x, y,
3744 s->img->width - 1, s->img->height - 1);
3745 }
3746 }
3747 else
3748 /* Draw a rectangle if image could not be loaded. */
3749 XDrawRectangle (s->display, s->window, s->gc, x, y,
3750 s->img->width - 1, s->img->height - 1);
3751}
3752
3753
3754/* Draw a relief around the image glyph string S. */
3755
3756static void
3757x_draw_image_relief (s)
3758 struct glyph_string *s;
3759{
3760 int x0, y0, x1, y1, thick, raised_p;
3761 XRectangle r;
3762 int x;
95af8492 3763 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
3764
3765 /* If first glyph of S has a left box line, start drawing it to the
3766 right of that line. */
3767 if (s->face->box != FACE_NO_BOX
3768 && s->first_glyph->left_box_line_p)
3769 x = s->x + s->face->box_line_width;
3770 else
3771 x = s->x;
3772
3773 /* If there is a margin around the image, adjust x- and y-position
3774 by that margin. */
3775 if (s->img->margin)
3776 {
3777 x += s->img->margin;
3778 y += s->img->margin;
3779 }
3780
3781 if (s->hl == DRAW_IMAGE_SUNKEN
3782 || s->hl == DRAW_IMAGE_RAISED)
3783 {
9ea173e8 3784 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
06a2c219
GM
3785 raised_p = s->hl == DRAW_IMAGE_RAISED;
3786 }
3787 else
3788 {
3789 thick = abs (s->img->relief);
3790 raised_p = s->img->relief > 0;
3791 }
3792
3793 x0 = x - thick;
3794 y0 = y - thick;
3795 x1 = x + s->img->width + thick - 1;
3796 y1 = y + s->img->height + thick - 1;
3797
3798 x_setup_relief_colors (s);
3799 x_get_glyph_string_clip_rect (s, &r);
3800 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3801}
3802
3803
3804/* Draw the foreground of image glyph string S to PIXMAP. */
3805
3806static void
3807x_draw_image_foreground_1 (s, pixmap)
3808 struct glyph_string *s;
3809 Pixmap pixmap;
3810{
3811 int x;
95af8492 3812 int y = s->ybase - s->y - image_ascent (s->img, s->face);
06a2c219
GM
3813
3814 /* If first glyph of S has a left box line, start drawing it to the
3815 right of that line. */
3816 if (s->face->box != FACE_NO_BOX
3817 && s->first_glyph->left_box_line_p)
3818 x = s->face->box_line_width;
3819 else
3820 x = 0;
3821
3822 /* If there is a margin around the image, adjust x- and y-position
3823 by that margin. */
3824 if (s->img->margin)
3825 {
3826 x += s->img->margin;
3827 y += s->img->margin;
3828 }
dc43ef94 3829
06a2c219
GM
3830 if (s->img->pixmap)
3831 {
3832 if (s->img->mask)
3833 {
3834 /* We can't set both a clip mask and use XSetClipRectangles
3835 because the latter also sets a clip mask. We also can't
3836 trust on the shape extension to be available
3837 (XShapeCombineRegion). So, compute the rectangle to draw
3838 manually. */
3839 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3840 | GCFunction);
3841 XGCValues xgcv;
3842
3843 xgcv.clip_mask = s->img->mask;
3844 xgcv.clip_x_origin = x;
3845 xgcv.clip_y_origin = y;
3846 xgcv.function = GXcopy;
3847 XChangeGC (s->display, s->gc, mask, &xgcv);
3848
3849 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3850 0, 0, s->img->width, s->img->height, x, y);
3851 XSetClipMask (s->display, s->gc, None);
3852 }
3853 else
3854 {
3855 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3856 0, 0, s->img->width, s->img->height, x, y);
3857
3858 /* When the image has a mask, we can expect that at
3859 least part of a mouse highlight or a block cursor will
3860 be visible. If the image doesn't have a mask, make
3861 a block cursor visible by drawing a rectangle around
3862 the image. I believe it's looking better if we do
3863 nothing here for mouse-face. */
3864 if (s->hl == DRAW_CURSOR)
3865 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3866 s->img->width - 1, s->img->height - 1);
3867 }
3868 }
3869 else
3870 /* Draw a rectangle if image could not be loaded. */
3871 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3872 s->img->width - 1, s->img->height - 1);
3873}
dc43ef94 3874
990ba854 3875
06a2c219
GM
3876/* Draw part of the background of glyph string S. X, Y, W, and H
3877 give the rectangle to draw. */
a9a5b0a5 3878
06a2c219
GM
3879static void
3880x_draw_glyph_string_bg_rect (s, x, y, w, h)
3881 struct glyph_string *s;
3882 int x, y, w, h;
3883{
3884 if (s->stippled_p)
3885 {
3886 /* Fill background with a stipple pattern. */
3887 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3888 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3889 XSetFillStyle (s->display, s->gc, FillSolid);
3890 }
3891 else
3892 x_clear_glyph_string_rect (s, x, y, w, h);
3893}
07e34cb0 3894
b5210ea7 3895
06a2c219 3896/* Draw image glyph string S.
dc43ef94 3897
06a2c219
GM
3898 s->y
3899 s->x +-------------------------
3900 | s->face->box
3901 |
3902 | +-------------------------
3903 | | s->img->margin
3904 | |
3905 | | +-------------------
3906 | | | the image
dc43ef94 3907
06a2c219 3908 */
dc43ef94 3909
06a2c219
GM
3910static void
3911x_draw_image_glyph_string (s)
3912 struct glyph_string *s;
3913{
3914 int x, y;
3915 int box_line_width = s->face->box_line_width;
3916 int margin = s->img->margin;
3917 int height;
3918 Pixmap pixmap = None;
3919
3920 height = s->height - 2 * box_line_width;
3921
3922 /* Fill background with face under the image. Do it only if row is
3923 taller than image or if image has a clip mask to reduce
3924 flickering. */
3925 s->stippled_p = s->face->stipple != 0;
3926 if (height > s->img->height
3927 || margin
3928 || s->img->mask
3929 || s->img->pixmap == 0
3930 || s->width != s->background_width)
3931 {
3932 if (box_line_width && s->first_glyph->left_box_line_p)
3933 x = s->x + box_line_width;
3934 else
3935 x = s->x;
3936
3937 y = s->y + box_line_width;
3938
3939 if (s->img->mask)
3940 {
3941 /* Create a pixmap as large as the glyph string Fill it with
3942 the background color. Copy the image to it, using its
3943 mask. Copy the temporary pixmap to the display. */
3944 Screen *screen = FRAME_X_SCREEN (s->f);
3945 int depth = DefaultDepthOfScreen (screen);
3946
3947 /* Create a pixmap as large as the glyph string. */
3948 pixmap = XCreatePixmap (s->display, s->window,
3949 s->background_width,
3950 s->height, depth);
3951
3952 /* Don't clip in the following because we're working on the
3953 pixmap. */
3954 XSetClipMask (s->display, s->gc, None);
3955
3956 /* Fill the pixmap with the background color/stipple. */
3957 if (s->stippled_p)
3958 {
3959 /* Fill background with a stipple pattern. */
3960 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3961 XFillRectangle (s->display, pixmap, s->gc,
3962 0, 0, s->background_width, s->height);
3963 XSetFillStyle (s->display, s->gc, FillSolid);
3964 }
3965 else
3966 {
3967 XGCValues xgcv;
3968 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3969 &xgcv);
3970 XSetForeground (s->display, s->gc, xgcv.background);
3971 XFillRectangle (s->display, pixmap, s->gc,
3972 0, 0, s->background_width, s->height);
3973 XSetForeground (s->display, s->gc, xgcv.foreground);
3974 }
3975 }
3976 else
3977 /* Implementation idea: Is it possible to construct a mask?
3978 We could look at the color at the margins of the image, and
3979 say that this color is probably the background color of the
3980 image. */
3981 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
3982
3983 s->background_filled_p = 1;
3984 }
dc43ef94 3985
06a2c219
GM
3986 /* Draw the foreground. */
3987 if (pixmap != None)
3988 {
3989 x_draw_image_foreground_1 (s, pixmap);
3990 x_set_glyph_string_clipping (s);
3991 XCopyArea (s->display, pixmap, s->window, s->gc,
3992 0, 0, s->background_width, s->height, s->x, s->y);
3993 XFreePixmap (s->display, pixmap);
3994 }
3995 else
3996 x_draw_image_foreground (s);
b5210ea7 3997
06a2c219
GM
3998 /* If we must draw a relief around the image, do it. */
3999 if (s->img->relief
4000 || s->hl == DRAW_IMAGE_RAISED
4001 || s->hl == DRAW_IMAGE_SUNKEN)
4002 x_draw_image_relief (s);
4003}
8c1a6a84 4004
990ba854 4005
06a2c219 4006/* Draw stretch glyph string S. */
dc43ef94 4007
06a2c219
GM
4008static void
4009x_draw_stretch_glyph_string (s)
4010 struct glyph_string *s;
4011{
4012 xassert (s->first_glyph->type == STRETCH_GLYPH);
4013 s->stippled_p = s->face->stipple != 0;
990ba854 4014
06a2c219
GM
4015 if (s->hl == DRAW_CURSOR
4016 && !x_stretch_cursor_p)
4017 {
4018 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4019 as wide as the stretch glyph. */
4020 int width = min (CANON_X_UNIT (s->f), s->background_width);
990ba854 4021
06a2c219
GM
4022 /* Draw cursor. */
4023 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
0cdd0c9f 4024
06a2c219
GM
4025 /* Clear rest using the GC of the original non-cursor face. */
4026 if (width < s->background_width)
4027 {
4028 GC gc = s->face->gc;
4029 int x = s->x + width, y = s->y;
4030 int w = s->background_width - width, h = s->height;
4031 XRectangle r;
dc43ef94 4032
06a2c219
GM
4033 x_get_glyph_string_clip_rect (s, &r);
4034 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
97210f4e 4035
06a2c219
GM
4036 if (s->face->stipple)
4037 {
4038 /* Fill background with a stipple pattern. */
4039 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4040 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4041 XSetFillStyle (s->display, gc, FillSolid);
4042 }
4043 else
4044 {
4045 XGCValues xgcv;
4046 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4047 XSetForeground (s->display, gc, xgcv.background);
4048 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4049 XSetForeground (s->display, gc, xgcv.foreground);
4050 }
4051 }
4052 }
4053 else
4054 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4055 s->height);
4056
4057 s->background_filled_p = 1;
4058}
4059
4060
4061/* Draw glyph string S. */
4062
4063static void
4064x_draw_glyph_string (s)
4065 struct glyph_string *s;
4066{
4067 /* If S draws into the background of its successor, draw the
4068 background of the successor first so that S can draw into it.
4069 This makes S->next use XDrawString instead of XDrawImageString. */
66ac4b0e 4070 if (s->next && s->right_overhang && !s->for_overlaps_p)
06a2c219
GM
4071 {
4072 xassert (s->next->img == NULL);
4073 x_set_glyph_string_gc (s->next);
4074 x_set_glyph_string_clipping (s->next);
4075 x_draw_glyph_string_background (s->next, 1);
4076 }
97210f4e 4077
06a2c219
GM
4078 /* Set up S->gc, set clipping and draw S. */
4079 x_set_glyph_string_gc (s);
4080 x_set_glyph_string_clipping (s);
4081
4082 switch (s->first_glyph->type)
4083 {
4084 case IMAGE_GLYPH:
4085 x_draw_image_glyph_string (s);
4086 break;
4087
4088 case STRETCH_GLYPH:
4089 x_draw_stretch_glyph_string (s);
4090 break;
4091
4092 case CHAR_GLYPH:
66ac4b0e
GM
4093 if (s->for_overlaps_p)
4094 s->background_filled_p = 1;
4095 else
4096 x_draw_glyph_string_background (s, 0);
06a2c219
GM
4097 x_draw_glyph_string_foreground (s);
4098 break;
4099
b4192550
KH
4100 case COMPOSITE_GLYPH:
4101 if (s->for_overlaps_p || s->gidx > 0)
4102 s->background_filled_p = 1;
4103 else
4104 x_draw_glyph_string_background (s, 1);
4105 x_draw_composite_glyph_string_foreground (s);
4106 break;
4107
06a2c219
GM
4108 default:
4109 abort ();
4110 }
4111
66ac4b0e 4112 if (!s->for_overlaps_p)
06a2c219 4113 {
66ac4b0e
GM
4114 /* Draw underline. */
4115 if (s->face->underline_p)
4116 {
4117 unsigned long dy, h;
06a2c219 4118
66ac4b0e
GM
4119 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4120 h = 1;
4121 if (!XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &dy))
4122 dy = s->height - h;
06a2c219 4123
66ac4b0e
GM
4124 if (s->face->underline_defaulted_p)
4125 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4126 s->width, h);
4127 else
4128 {
4129 XGCValues xgcv;
4130 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4131 XSetForeground (s->display, s->gc, s->face->underline_color);
4132 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4133 s->width, h);
4134 XSetForeground (s->display, s->gc, xgcv.foreground);
4135 }
dc6f92b8 4136 }
07e34cb0 4137
66ac4b0e
GM
4138 /* Draw overline. */
4139 if (s->face->overline_p)
06a2c219 4140 {
66ac4b0e
GM
4141 unsigned long dy = 0, h = 1;
4142
4143 if (s->face->overline_color_defaulted_p)
4144 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4145 s->width, h);
4146 else
4147 {
4148 XGCValues xgcv;
4149 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4150 XSetForeground (s->display, s->gc, s->face->overline_color);
4151 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4152 s->width, h);
4153 XSetForeground (s->display, s->gc, xgcv.foreground);
4154 }
06a2c219 4155 }
06a2c219 4156
66ac4b0e
GM
4157 /* Draw strike-through. */
4158 if (s->face->strike_through_p)
06a2c219 4159 {
66ac4b0e
GM
4160 unsigned long h = 1;
4161 unsigned long dy = (s->height - h) / 2;
4162
4163 if (s->face->strike_through_color_defaulted_p)
4164 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4165 s->width, h);
4166 else
4167 {
4168 XGCValues xgcv;
4169 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4170 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4171 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4172 s->width, h);
4173 XSetForeground (s->display, s->gc, xgcv.foreground);
4174 }
06a2c219 4175 }
06a2c219 4176
66ac4b0e
GM
4177 /* Draw relief. */
4178 if (s->face->box != FACE_NO_BOX)
4179 x_draw_glyph_string_box (s);
4180 }
06a2c219
GM
4181
4182 /* Reset clipping. */
4183 XSetClipMask (s->display, s->gc, None);
dc6f92b8 4184}
07e34cb0 4185
06a2c219 4186
b4192550
KH
4187static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4188 struct face **, int));
06a2c219 4189
06a2c219 4190
209f68d9
GM
4191/* Fill glyph string S with composition components specified by S->cmp.
4192
b4192550
KH
4193 FACES is an array of faces for all components of this composition.
4194 S->gidx is the index of the first component for S.
4195 OVERLAPS_P non-zero means S should draw the foreground only, and
209f68d9 4196 use its physical height for clipping.
06a2c219 4197
b4192550 4198 Value is the index of a component not in S. */
07e34cb0 4199
b4192550
KH
4200static int
4201x_fill_composite_glyph_string (s, faces, overlaps_p)
06a2c219 4202 struct glyph_string *s;
b4192550 4203 struct face **faces;
66ac4b0e 4204 int overlaps_p;
07e34cb0 4205{
b4192550 4206 int i;
06a2c219 4207
b4192550 4208 xassert (s);
06a2c219 4209
b4192550 4210 s->for_overlaps_p = overlaps_p;
06a2c219 4211
b4192550
KH
4212 s->face = faces[s->gidx];
4213 s->font = s->face->font;
4214 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
06a2c219 4215
b4192550
KH
4216 /* For all glyphs of this composition, starting at the offset
4217 S->gidx, until we reach the end of the definition or encounter a
4218 glyph that requires the different face, add it to S. */
4219 ++s->nchars;
4220 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4221 ++s->nchars;
06a2c219 4222
b4192550
KH
4223 /* All glyph strings for the same composition has the same width,
4224 i.e. the width set for the first component of the composition. */
06a2c219 4225
06a2c219
GM
4226 s->width = s->first_glyph->pixel_width;
4227
4228 /* If the specified font could not be loaded, use the frame's
4229 default font, but record the fact that we couldn't load it in
4230 the glyph string so that we can draw rectangles for the
4231 characters of the glyph string. */
4232 if (s->font == NULL)
4233 {
4234 s->font_not_found_p = 1;
4235 s->font = FRAME_FONT (s->f);
4236 }
4237
4238 /* Adjust base line for subscript/superscript text. */
4239 s->ybase += s->first_glyph->voffset;
4240
4241 xassert (s->face && s->face->gc);
4242
4243 /* This glyph string must always be drawn with 16-bit functions. */
4244 s->two_byte_p = 1;
b4192550
KH
4245
4246 return s->gidx + s->nchars;
06a2c219
GM
4247}
4248
4249
209f68d9
GM
4250/* Fill glyph string S from a sequence of character glyphs.
4251
06a2c219 4252 FACE_ID is the face id of the string. START is the index of the
66ac4b0e
GM
4253 first glyph to consider, END is the index of the last + 1.
4254 OVERLAPS_P non-zero means S should draw the foreground only, and
209f68d9 4255 use its physical height for clipping.
66ac4b0e
GM
4256
4257 Value is the index of the first glyph not in S. */
06a2c219
GM
4258
4259static int
66ac4b0e 4260x_fill_glyph_string (s, face_id, start, end, overlaps_p)
06a2c219
GM
4261 struct glyph_string *s;
4262 int face_id;
66ac4b0e 4263 int start, end, overlaps_p;
06a2c219
GM
4264{
4265 struct glyph *glyph, *last;
4266 int voffset;
ee569018 4267 int glyph_not_available_p;
06a2c219 4268
06a2c219
GM
4269 xassert (s->f == XFRAME (s->w->frame));
4270 xassert (s->nchars == 0);
4271 xassert (start >= 0 && end > start);
4272
66ac4b0e 4273 s->for_overlaps_p = overlaps_p,
06a2c219
GM
4274 glyph = s->row->glyphs[s->area] + start;
4275 last = s->row->glyphs[s->area] + end;
4276 voffset = glyph->voffset;
4277
ee569018
KH
4278 glyph_not_available_p = glyph->glyph_not_available_p;
4279
06a2c219
GM
4280 while (glyph < last
4281 && glyph->type == CHAR_GLYPH
4282 && glyph->voffset == voffset
ee569018
KH
4283 /* Same face id implies same font, nowadays. */
4284 && glyph->face_id == face_id
4285 && glyph->glyph_not_available_p == glyph_not_available_p)
06a2c219 4286 {
ee569018
KH
4287 int two_byte_p;
4288
06a2c219 4289 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
ee569018
KH
4290 s->char2b + s->nchars,
4291 &two_byte_p);
4292 s->two_byte_p = two_byte_p;
06a2c219
GM
4293 ++s->nchars;
4294 xassert (s->nchars <= end - start);
4295 s->width += glyph->pixel_width;
4296 ++glyph;
4297 }
4298
4299 s->font = s->face->font;
4300 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4301
4302 /* If the specified font could not be loaded, use the frame's font,
4303 but record the fact that we couldn't load it in
4304 S->font_not_found_p so that we can draw rectangles for the
4305 characters of the glyph string. */
ee569018 4306 if (s->font == NULL || glyph_not_available_p)
06a2c219
GM
4307 {
4308 s->font_not_found_p = 1;
4309 s->font = FRAME_FONT (s->f);
4310 }
4311
4312 /* Adjust base line for subscript/superscript text. */
4313 s->ybase += voffset;
66ac4b0e 4314
06a2c219
GM
4315 xassert (s->face && s->face->gc);
4316 return glyph - s->row->glyphs[s->area];
07e34cb0 4317}
dc6f92b8 4318
06a2c219
GM
4319
4320/* Fill glyph string S from image glyph S->first_glyph. */
dc6f92b8 4321
dfcf069d 4322static void
06a2c219
GM
4323x_fill_image_glyph_string (s)
4324 struct glyph_string *s;
4325{
4326 xassert (s->first_glyph->type == IMAGE_GLYPH);
43d120d8 4327 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
06a2c219 4328 xassert (s->img);
43d120d8 4329 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
06a2c219
GM
4330 s->font = s->face->font;
4331 s->width = s->first_glyph->pixel_width;
4332
4333 /* Adjust base line for subscript/superscript text. */
4334 s->ybase += s->first_glyph->voffset;
4335}
4336
4337
209f68d9 4338/* Fill glyph string S from a sequence of stretch glyphs.
06a2c219 4339
209f68d9
GM
4340 ROW is the glyph row in which the glyphs are found, AREA is the
4341 area within the row. START is the index of the first glyph to
4342 consider, END is the index of the last + 1.
4343
4344 Value is the index of the first glyph not in S. */
4345
4346static int
4347x_fill_stretch_glyph_string (s, row, area, start, end)
06a2c219 4348 struct glyph_string *s;
209f68d9
GM
4349 struct glyph_row *row;
4350 enum glyph_row_area area;
4351 int start, end;
06a2c219 4352{
209f68d9
GM
4353 struct glyph *glyph, *last;
4354 int voffset, face_id;
4355
06a2c219 4356 xassert (s->first_glyph->type == STRETCH_GLYPH);
209f68d9
GM
4357
4358 glyph = s->row->glyphs[s->area] + start;
4359 last = s->row->glyphs[s->area] + end;
4360 face_id = glyph->face_id;
4361 s->face = FACE_FROM_ID (s->f, face_id);
06a2c219 4362 s->font = s->face->font;
209f68d9
GM
4363 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4364 s->width = glyph->pixel_width;
4365 voffset = glyph->voffset;
4366
4367 for (++glyph;
4368 (glyph < last
4369 && glyph->type == STRETCH_GLYPH
4370 && glyph->voffset == voffset
4371 && glyph->face_id == face_id);
4372 ++glyph)
4373 s->width += glyph->pixel_width;
06a2c219
GM
4374
4375 /* Adjust base line for subscript/superscript text. */
209f68d9
GM
4376 s->ybase += voffset;
4377
4378 xassert (s->face && s->face->gc);
4379 return glyph - s->row->glyphs[s->area];
06a2c219
GM
4380}
4381
4382
4383/* Initialize glyph string S. CHAR2B is a suitably allocated vector
4384 of XChar2b structures for S; it can't be allocated in
4385 x_init_glyph_string because it must be allocated via `alloca'. W
4386 is the window on which S is drawn. ROW and AREA are the glyph row
4387 and area within the row from which S is constructed. START is the
4388 index of the first glyph structure covered by S. HL is a
4389 face-override for drawing S. */
4390
4391static void
4392x_init_glyph_string (s, char2b, w, row, area, start, hl)
4393 struct glyph_string *s;
4394 XChar2b *char2b;
4395 struct window *w;
4396 struct glyph_row *row;
4397 enum glyph_row_area area;
4398 int start;
4399 enum draw_glyphs_face hl;
4400{
4401 bzero (s, sizeof *s);
4402 s->w = w;
4403 s->f = XFRAME (w->frame);
4404 s->display = FRAME_X_DISPLAY (s->f);
4405 s->window = FRAME_X_WINDOW (s->f);
4406 s->char2b = char2b;
4407 s->hl = hl;
4408 s->row = row;
4409 s->area = area;
4410 s->first_glyph = row->glyphs[area] + start;
4411 s->height = row->height;
4412 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4413
9ea173e8
GM
4414 /* Display the internal border below the tool-bar window. */
4415 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219
GM
4416 s->y -= s->f->output_data.x->internal_border_width;
4417
4418 s->ybase = s->y + row->ascent;
4419}
4420
4421
4422/* Set background width of glyph string S. START is the index of the
4423 first glyph following S. LAST_X is the right-most x-position + 1
4424 in the drawing area. */
4425
4426static INLINE void
4427x_set_glyph_string_background_width (s, start, last_x)
4428 struct glyph_string *s;
4429 int start;
4430 int last_x;
4431{
4432 /* If the face of this glyph string has to be drawn to the end of
4433 the drawing area, set S->extends_to_end_of_line_p. */
4434 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4435
4436 if (start == s->row->used[s->area]
4437 && s->hl == DRAW_NORMAL_TEXT
4438 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4439 || s->face->background != default_face->background
4440 || s->face->stipple != default_face->stipple))
4441 s->extends_to_end_of_line_p = 1;
4442
4443 /* If S extends its face to the end of the line, set its
4444 background_width to the distance to the right edge of the drawing
4445 area. */
4446 if (s->extends_to_end_of_line_p)
1da3fd71 4447 s->background_width = last_x - s->x + 1;
06a2c219
GM
4448 else
4449 s->background_width = s->width;
4450}
4451
4452
4453/* Add a glyph string for a stretch glyph to the list of strings
4454 between HEAD and TAIL. START is the index of the stretch glyph in
4455 row area AREA of glyph row ROW. END is the index of the last glyph
4456 in that glyph row area. X is the current output position assigned
4457 to the new glyph string constructed. HL overrides that face of the
4458 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4459 is the right-most x-position of the drawing area. */
4460
8abee2e1
DL
4461/* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4462 and below -- keep them on one line. */
4463#define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4464 do \
4465 { \
4466 s = (struct glyph_string *) alloca (sizeof *s); \
4467 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
209f68d9 4468 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
06a2c219 4469 x_append_glyph_string (&HEAD, &TAIL, s); \
06a2c219
GM
4470 s->x = (X); \
4471 } \
4472 while (0)
4473
4474
4475/* Add a glyph string for an image glyph to the list of strings
4476 between HEAD and TAIL. START is the index of the image glyph in
4477 row area AREA of glyph row ROW. END is the index of the last glyph
4478 in that glyph row area. X is the current output position assigned
4479 to the new glyph string constructed. HL overrides that face of the
4480 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4481 is the right-most x-position of the drawing area. */
4482
8abee2e1 4483#define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4484 do \
4485 { \
4486 s = (struct glyph_string *) alloca (sizeof *s); \
4487 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4488 x_fill_image_glyph_string (s); \
4489 x_append_glyph_string (&HEAD, &TAIL, s); \
4490 ++START; \
4491 s->x = (X); \
4492 } \
4493 while (0)
4494
4495
4496/* Add a glyph string for a sequence of character glyphs to the list
4497 of strings between HEAD and TAIL. START is the index of the first
4498 glyph in row area AREA of glyph row ROW that is part of the new
4499 glyph string. END is the index of the last glyph in that glyph row
4500 area. X is the current output position assigned to the new glyph
4501 string constructed. HL overrides that face of the glyph; e.g. it
4502 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4503 right-most x-position of the drawing area. */
4504
8abee2e1 4505#define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4506 do \
4507 { \
3e71d8f2 4508 int c, face_id; \
06a2c219
GM
4509 XChar2b *char2b; \
4510 \
43d120d8 4511 c = (ROW)->glyphs[AREA][START].u.ch; \
43d120d8 4512 face_id = (ROW)->glyphs[AREA][START].face_id; \
06a2c219 4513 \
b4192550
KH
4514 s = (struct glyph_string *) alloca (sizeof *s); \
4515 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4516 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4517 x_append_glyph_string (&HEAD, &TAIL, s); \
b4192550
KH
4518 s->x = (X); \
4519 START = x_fill_glyph_string (s, face_id, START, END, \
66ac4b0e 4520 OVERLAPS_P); \
06a2c219
GM
4521 } \
4522 while (0)
4523
4524
b4192550
KH
4525/* Add a glyph string for a composite sequence to the list of strings
4526 between HEAD and TAIL. START is the index of the first glyph in
4527 row area AREA of glyph row ROW that is part of the new glyph
4528 string. END is the index of the last glyph in that glyph row area.
4529 X is the current output position assigned to the new glyph string
4530 constructed. HL overrides that face of the glyph; e.g. it is
4531 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4532 x-position of the drawing area. */
4533
6c27ec25 4534#define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
b4192550 4535 do { \
43d120d8
KH
4536 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4537 int face_id = (ROW)->glyphs[AREA][START].face_id; \
ee569018 4538 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
b4192550
KH
4539 struct composition *cmp = composition_table[cmp_id]; \
4540 int glyph_len = cmp->glyph_len; \
4541 XChar2b *char2b; \
4542 struct face **faces; \
4543 struct glyph_string *first_s = NULL; \
4544 int n; \
4545 \
ee569018 4546 base_face = base_face->ascii_face; \
b4192550
KH
4547 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4548 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4549 /* At first, fill in `char2b' and `faces'. */ \
4550 for (n = 0; n < glyph_len; n++) \
4551 { \
43d120d8 4552 int c = COMPOSITION_GLYPH (cmp, n); \
ee569018
KH
4553 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4554 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4555 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4556 this_face_id, char2b + n, 1); \
b4192550
KH
4557 } \
4558 \
4559 /* Make glyph_strings for each glyph sequence that is drawable by \
4560 the same face, and append them to HEAD/TAIL. */ \
4561 for (n = 0; n < cmp->glyph_len;) \
4562 { \
4563 s = (struct glyph_string *) alloca (sizeof *s); \
4564 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4565 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4566 s->cmp = cmp; \
4567 s->gidx = n; \
b4192550
KH
4568 s->x = (X); \
4569 \
4570 if (n == 0) \
4571 first_s = s; \
4572 \
4573 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4574 } \
4575 \
4576 ++START; \
4577 s = first_s; \
4578 } while (0)
4579
4580
06a2c219
GM
4581/* Build a list of glyph strings between HEAD and TAIL for the glyphs
4582 of AREA of glyph row ROW on window W between indices START and END.
4583 HL overrides the face for drawing glyph strings, e.g. it is
4584 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4585 x-positions of the drawing area.
4586
4587 This is an ugly monster macro construct because we must use alloca
4588 to allocate glyph strings (because x_draw_glyphs can be called
4589 asynchronously). */
4590
8abee2e1 4591#define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4592 do \
4593 { \
4594 HEAD = TAIL = NULL; \
4595 while (START < END) \
4596 { \
4597 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4598 switch (first_glyph->type) \
4599 { \
4600 case CHAR_GLYPH: \
4601 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
66ac4b0e
GM
4602 TAIL, HL, X, LAST_X, \
4603 OVERLAPS_P); \
06a2c219
GM
4604 break; \
4605 \
b4192550
KH
4606 case COMPOSITE_GLYPH: \
4607 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4608 HEAD, TAIL, HL, X, LAST_X,\
4609 OVERLAPS_P); \
4610 break; \
4611 \
06a2c219
GM
4612 case STRETCH_GLYPH: \
4613 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4614 HEAD, TAIL, HL, X, LAST_X); \
4615 break; \
4616 \
4617 case IMAGE_GLYPH: \
4618 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4619 TAIL, HL, X, LAST_X); \
4620 break; \
4621 \
4622 default: \
4623 abort (); \
4624 } \
4625 \
4626 x_set_glyph_string_background_width (s, START, LAST_X); \
4627 (X) += s->width; \
4628 } \
4629 } \
4630 while (0)
4631
4632
4633/* Draw glyphs between START and END in AREA of ROW on window W,
4634 starting at x-position X. X is relative to AREA in W. HL is a
4635 face-override with the following meaning:
4636
4637 DRAW_NORMAL_TEXT draw normally
4638 DRAW_CURSOR draw in cursor face
4639 DRAW_MOUSE_FACE draw in mouse face.
4640 DRAW_INVERSE_VIDEO draw in mode line face
4641 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4642 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4643
4644 If REAL_START is non-null, return in *REAL_START the real starting
4645 position for display. This can be different from START in case
4646 overlapping glyphs must be displayed. If REAL_END is non-null,
4647 return in *REAL_END the real end position for display. This can be
4648 different from END in case overlapping glyphs must be displayed.
4649
66ac4b0e
GM
4650 If OVERLAPS_P is non-zero, draw only the foreground of characters
4651 and clip to the physical height of ROW.
4652
06a2c219
GM
4653 Value is the x-position reached, relative to AREA of W. */
4654
4655static int
66ac4b0e
GM
4656x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4657 overlaps_p)
06a2c219
GM
4658 struct window *w;
4659 int x;
4660 struct glyph_row *row;
4661 enum glyph_row_area area;
4662 int start, end;
4663 enum draw_glyphs_face hl;
4664 int *real_start, *real_end;
66ac4b0e 4665 int overlaps_p;
dc6f92b8 4666{
06a2c219
GM
4667 struct glyph_string *head, *tail;
4668 struct glyph_string *s;
4669 int last_x, area_width;
4670 int x_reached;
4671 int i, j;
4672
4673 /* Let's rather be paranoid than getting a SEGV. */
4674 start = max (0, start);
4675 end = min (end, row->used[area]);
4676 if (real_start)
4677 *real_start = start;
4678 if (real_end)
4679 *real_end = end;
4680
4681 /* Translate X to frame coordinates. Set last_x to the right
4682 end of the drawing area. */
4683 if (row->full_width_p)
4684 {
4685 /* X is relative to the left edge of W, without scroll bars
4686 or flag areas. */
4687 struct frame *f = XFRAME (w->frame);
110859fc 4688 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
06a2c219 4689 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
dc6f92b8 4690
06a2c219
GM
4691 x += window_left_x;
4692 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4693 last_x = window_left_x + area_width;
4694
4695 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4696 {
110859fc 4697 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
06a2c219
GM
4698 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4699 last_x += width;
4700 else
4701 x -= width;
4702 }
dc6f92b8 4703
b9432a85
GM
4704 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4705 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
06a2c219
GM
4706 }
4707 else
dc6f92b8 4708 {
06a2c219
GM
4709 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4710 area_width = window_box_width (w, area);
4711 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
dc6f92b8
JB
4712 }
4713
06a2c219
GM
4714 /* Build a doubly-linked list of glyph_string structures between
4715 head and tail from what we have to draw. Note that the macro
4716 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4717 the reason we use a separate variable `i'. */
4718 i = start;
66ac4b0e
GM
4719 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4720 overlaps_p);
06a2c219
GM
4721 if (tail)
4722 x_reached = tail->x + tail->background_width;
4723 else
4724 x_reached = x;
90e65f07 4725
06a2c219
GM
4726 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4727 the row, redraw some glyphs in front or following the glyph
4728 strings built above. */
66ac4b0e 4729 if (!overlaps_p && row->contains_overlapping_glyphs_p)
06a2c219
GM
4730 {
4731 int dummy_x = 0;
4732 struct glyph_string *h, *t;
4733
4734 /* Compute overhangs for all glyph strings. */
4735 for (s = head; s; s = s->next)
4736 x_compute_glyph_string_overhangs (s);
4737
4738 /* Prepend glyph strings for glyphs in front of the first glyph
4739 string that are overwritten because of the first glyph
4740 string's left overhang. The background of all strings
4741 prepended must be drawn because the first glyph string
4742 draws over it. */
4743 i = x_left_overwritten (head);
4744 if (i >= 0)
4745 {
4746 j = i;
4747 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
66ac4b0e
GM
4748 DRAW_NORMAL_TEXT, dummy_x, last_x,
4749 overlaps_p);
06a2c219
GM
4750 start = i;
4751 if (real_start)
4752 *real_start = start;
4753 x_compute_overhangs_and_x (t, head->x, 1);
4754 x_prepend_glyph_string_lists (&head, &tail, h, t);
4755 }
58769bee 4756
06a2c219
GM
4757 /* Prepend glyph strings for glyphs in front of the first glyph
4758 string that overwrite that glyph string because of their
4759 right overhang. For these strings, only the foreground must
4760 be drawn, because it draws over the glyph string at `head'.
4761 The background must not be drawn because this would overwrite
4762 right overhangs of preceding glyphs for which no glyph
4763 strings exist. */
4764 i = x_left_overwriting (head);
4765 if (i >= 0)
4766 {
4767 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
66ac4b0e
GM
4768 DRAW_NORMAL_TEXT, dummy_x, last_x,
4769 overlaps_p);
06a2c219
GM
4770 for (s = h; s; s = s->next)
4771 s->background_filled_p = 1;
4772 if (real_start)
4773 *real_start = i;
4774 x_compute_overhangs_and_x (t, head->x, 1);
4775 x_prepend_glyph_string_lists (&head, &tail, h, t);
4776 }
dbcb258a 4777
06a2c219
GM
4778 /* Append glyphs strings for glyphs following the last glyph
4779 string tail that are overwritten by tail. The background of
4780 these strings has to be drawn because tail's foreground draws
4781 over it. */
4782 i = x_right_overwritten (tail);
4783 if (i >= 0)
4784 {
4785 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4786 DRAW_NORMAL_TEXT, x, last_x,
4787 overlaps_p);
06a2c219
GM
4788 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4789 x_append_glyph_string_lists (&head, &tail, h, t);
4790 if (real_end)
4791 *real_end = i;
4792 }
dc6f92b8 4793
06a2c219
GM
4794 /* Append glyph strings for glyphs following the last glyph
4795 string tail that overwrite tail. The foreground of such
4796 glyphs has to be drawn because it writes into the background
4797 of tail. The background must not be drawn because it could
4798 paint over the foreground of following glyphs. */
4799 i = x_right_overwriting (tail);
4800 if (i >= 0)
4801 {
4802 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4803 DRAW_NORMAL_TEXT, x, last_x,
4804 overlaps_p);
06a2c219
GM
4805 for (s = h; s; s = s->next)
4806 s->background_filled_p = 1;
4807 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4808 x_append_glyph_string_lists (&head, &tail, h, t);
4809 if (real_end)
4810 *real_end = i;
4811 }
4812 }
58769bee 4813
06a2c219
GM
4814 /* Draw all strings. */
4815 for (s = head; s; s = s->next)
4816 x_draw_glyph_string (s);
dc6f92b8 4817
06a2c219
GM
4818 /* Value is the x-position up to which drawn, relative to AREA of W.
4819 This doesn't include parts drawn because of overhangs. */
4820 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4821 if (!row->full_width_p)
4822 {
4823 if (area > LEFT_MARGIN_AREA)
4824 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4825 if (area > TEXT_AREA)
4826 x_reached -= window_box_width (w, TEXT_AREA);
4827 }
4828 return x_reached;
4829}
dc6f92b8 4830
dc6f92b8 4831
66ac4b0e
GM
4832/* Fix the display of area AREA of overlapping row ROW in window W. */
4833
4834static void
4835x_fix_overlapping_area (w, row, area)
4836 struct window *w;
4837 struct glyph_row *row;
4838 enum glyph_row_area area;
4839{
4840 int i, x;
4841
4842 BLOCK_INPUT;
4843
4844 if (area == LEFT_MARGIN_AREA)
4845 x = 0;
4846 else if (area == TEXT_AREA)
4847 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
4848 else
4849 x = (window_box_width (w, LEFT_MARGIN_AREA)
4850 + window_box_width (w, TEXT_AREA));
4851
4852 for (i = 0; i < row->used[area];)
4853 {
4854 if (row->glyphs[area][i].overlaps_vertically_p)
4855 {
4856 int start = i, start_x = x;
4857
4858 do
4859 {
4860 x += row->glyphs[area][i].pixel_width;
4861 ++i;
4862 }
4863 while (i < row->used[area]
4864 && row->glyphs[area][i].overlaps_vertically_p);
4865
4866 x_draw_glyphs (w, start_x, row, area, start, i,
4867 (row->inverse_p
4868 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4869 NULL, NULL, 1);
4870 }
4871 else
4872 {
4873 x += row->glyphs[area][i].pixel_width;
4874 ++i;
4875 }
4876 }
4877
4878 UNBLOCK_INPUT;
4879}
4880
4881
06a2c219
GM
4882/* Output LEN glyphs starting at START at the nominal cursor position.
4883 Advance the nominal cursor over the text. The global variable
4884 updated_window contains the window being updated, updated_row is
4885 the glyph row being updated, and updated_area is the area of that
4886 row being updated. */
dc6f92b8 4887
06a2c219
GM
4888static void
4889x_write_glyphs (start, len)
4890 struct glyph *start;
4891 int len;
4892{
4893 int x, hpos, real_start, real_end;
d9cdbb3d 4894
06a2c219 4895 xassert (updated_window && updated_row);
dc6f92b8 4896 BLOCK_INPUT;
06a2c219
GM
4897
4898 /* Write glyphs. */
dc6f92b8 4899
06a2c219
GM
4900 hpos = start - updated_row->glyphs[updated_area];
4901 x = x_draw_glyphs (updated_window, output_cursor.x,
4902 updated_row, updated_area,
4903 hpos, hpos + len,
4904 (updated_row->inverse_p
4905 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
66ac4b0e 4906 &real_start, &real_end, 0);
b30ec466 4907
06a2c219
GM
4908 /* If we drew over the cursor, note that it is not visible any more. */
4909 note_overwritten_text_cursor (updated_window, real_start,
4910 real_end - real_start);
dc6f92b8
JB
4911
4912 UNBLOCK_INPUT;
06a2c219
GM
4913
4914 /* Advance the output cursor. */
4915 output_cursor.hpos += len;
4916 output_cursor.x = x;
dc6f92b8
JB
4917}
4918
0cdd0c9f 4919
06a2c219 4920/* Insert LEN glyphs from START at the nominal cursor position. */
0cdd0c9f 4921
06a2c219
GM
4922static void
4923x_insert_glyphs (start, len)
4924 struct glyph *start;
4925 register int len;
4926{
4927 struct frame *f;
4928 struct window *w;
4929 int line_height, shift_by_width, shifted_region_width;
4930 struct glyph_row *row;
4931 struct glyph *glyph;
4932 int frame_x, frame_y, hpos, real_start, real_end;
58769bee 4933
06a2c219 4934 xassert (updated_window && updated_row);
0cdd0c9f 4935 BLOCK_INPUT;
06a2c219
GM
4936 w = updated_window;
4937 f = XFRAME (WINDOW_FRAME (w));
4938
4939 /* Get the height of the line we are in. */
4940 row = updated_row;
4941 line_height = row->height;
4942
4943 /* Get the width of the glyphs to insert. */
4944 shift_by_width = 0;
4945 for (glyph = start; glyph < start + len; ++glyph)
4946 shift_by_width += glyph->pixel_width;
4947
4948 /* Get the width of the region to shift right. */
4949 shifted_region_width = (window_box_width (w, updated_area)
4950 - output_cursor.x
4951 - shift_by_width);
4952
4953 /* Shift right. */
4954 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
4955 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
4956 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
4957 f->output_data.x->normal_gc,
4958 frame_x, frame_y,
4959 shifted_region_width, line_height,
4960 frame_x + shift_by_width, frame_y);
4961
4962 /* Write the glyphs. */
4963 hpos = start - row->glyphs[updated_area];
4964 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
66ac4b0e 4965 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
06a2c219
GM
4966 note_overwritten_text_cursor (w, real_start, real_end - real_start);
4967
4968 /* Advance the output cursor. */
4969 output_cursor.hpos += len;
4970 output_cursor.x += shift_by_width;
0cdd0c9f
RS
4971 UNBLOCK_INPUT;
4972}
0cdd0c9f 4973
0cdd0c9f 4974
06a2c219
GM
4975/* Delete N glyphs at the nominal cursor position. Not implemented
4976 for X frames. */
c83febd7
RS
4977
4978static void
06a2c219
GM
4979x_delete_glyphs (n)
4980 register int n;
c83febd7 4981{
06a2c219 4982 abort ();
c83febd7
RS
4983}
4984
0cdd0c9f 4985
06a2c219
GM
4986/* Erase the current text line from the nominal cursor position
4987 (inclusive) to pixel column TO_X (exclusive). The idea is that
4988 everything from TO_X onward is already erased.
4989
4990 TO_X is a pixel position relative to updated_area of
4991 updated_window. TO_X == -1 means clear to the end of this area. */
dc6f92b8 4992
06a2c219
GM
4993static void
4994x_clear_end_of_line (to_x)
4995 int to_x;
4996{
4997 struct frame *f;
4998 struct window *w = updated_window;
4999 int max_x, min_y, max_y;
5000 int from_x, from_y, to_y;
5001
5002 xassert (updated_window && updated_row);
5003 f = XFRAME (w->frame);
5004
5005 if (updated_row->full_width_p)
5006 {
5007 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5008 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5009 && !w->pseudo_window_p)
5010 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
0cdd0c9f 5011 }
06a2c219
GM
5012 else
5013 max_x = window_box_width (w, updated_area);
5014 max_y = window_text_bottom_y (w);
dc6f92b8 5015
06a2c219
GM
5016 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5017 of window. For TO_X > 0, truncate to end of drawing area. */
5018 if (to_x == 0)
5019 return;
5020 else if (to_x < 0)
5021 to_x = max_x;
5022 else
5023 to_x = min (to_x, max_x);
dbc4e1c1 5024
06a2c219
GM
5025 to_y = min (max_y, output_cursor.y + updated_row->height);
5026
5027 /* Notice if the cursor will be cleared by this operation. */
5028 if (!updated_row->full_width_p)
5029 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
dbc4e1c1 5030
06a2c219
GM
5031 from_x = output_cursor.x;
5032
5033 /* Translate to frame coordinates. */
5034 if (updated_row->full_width_p)
5035 {
5036 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5037 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5038 }
0cdd0c9f
RS
5039 else
5040 {
06a2c219
GM
5041 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5042 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5043 }
5044
045dee35 5045 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
06a2c219
GM
5046 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5047 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5048
5049 /* Prevent inadvertently clearing to end of the X window. */
5050 if (to_x > from_x && to_y > from_y)
5051 {
5052 BLOCK_INPUT;
5053 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5054 from_x, from_y, to_x - from_x, to_y - from_y,
5055 False);
5056 UNBLOCK_INPUT;
0cdd0c9f 5057 }
0cdd0c9f 5058}
dbc4e1c1 5059
0cdd0c9f 5060
06a2c219 5061/* Clear entire frame. If updating_frame is non-null, clear that
b86bd3dd 5062 frame. Otherwise clear the selected frame. */
06a2c219
GM
5063
5064static void
5065x_clear_frame ()
0cdd0c9f 5066{
06a2c219 5067 struct frame *f;
0cdd0c9f 5068
06a2c219
GM
5069 if (updating_frame)
5070 f = updating_frame;
0cdd0c9f 5071 else
b86bd3dd 5072 f = SELECTED_FRAME ();
58769bee 5073
06a2c219
GM
5074 /* Clearing the frame will erase any cursor, so mark them all as no
5075 longer visible. */
5076 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5077 output_cursor.hpos = output_cursor.vpos = 0;
5078 output_cursor.x = -1;
5079
5080 /* We don't set the output cursor here because there will always
5081 follow an explicit cursor_to. */
5082 BLOCK_INPUT;
5083 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5084
5085 /* We have to clear the scroll bars, too. If we have changed
5086 colors or something like that, then they should be notified. */
5087 x_scroll_bar_clear (f);
0cdd0c9f 5088
06a2c219
GM
5089 XFlush (FRAME_X_DISPLAY (f));
5090 UNBLOCK_INPUT;
dc6f92b8 5091}
06a2c219
GM
5092
5093
dc6f92b8 5094\f
dbc4e1c1
JB
5095/* Invert the middle quarter of the frame for .15 sec. */
5096
06a2c219
GM
5097/* We use the select system call to do the waiting, so we have to make
5098 sure it's available. If it isn't, we just won't do visual bells. */
5099
dbc4e1c1
JB
5100#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5101
06a2c219
GM
5102
5103/* Subtract the `struct timeval' values X and Y, storing the result in
5104 *RESULT. Return 1 if the difference is negative, otherwise 0. */
dbc4e1c1
JB
5105
5106static int
5107timeval_subtract (result, x, y)
5108 struct timeval *result, x, y;
5109{
06a2c219
GM
5110 /* Perform the carry for the later subtraction by updating y. This
5111 is safer because on some systems the tv_sec member is unsigned. */
dbc4e1c1
JB
5112 if (x.tv_usec < y.tv_usec)
5113 {
5114 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5115 y.tv_usec -= 1000000 * nsec;
5116 y.tv_sec += nsec;
5117 }
06a2c219 5118
dbc4e1c1
JB
5119 if (x.tv_usec - y.tv_usec > 1000000)
5120 {
5121 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5122 y.tv_usec += 1000000 * nsec;
5123 y.tv_sec -= nsec;
5124 }
5125
06a2c219
GM
5126 /* Compute the time remaining to wait. tv_usec is certainly
5127 positive. */
dbc4e1c1
JB
5128 result->tv_sec = x.tv_sec - y.tv_sec;
5129 result->tv_usec = x.tv_usec - y.tv_usec;
5130
06a2c219
GM
5131 /* Return indication of whether the result should be considered
5132 negative. */
dbc4e1c1
JB
5133 return x.tv_sec < y.tv_sec;
5134}
dc6f92b8 5135
dfcf069d 5136void
f676886a
JB
5137XTflash (f)
5138 struct frame *f;
dc6f92b8 5139{
dbc4e1c1 5140 BLOCK_INPUT;
dc6f92b8 5141
dbc4e1c1
JB
5142 {
5143 GC gc;
dc6f92b8 5144
06a2c219
GM
5145 /* Create a GC that will use the GXxor function to flip foreground
5146 pixels into background pixels. */
dbc4e1c1
JB
5147 {
5148 XGCValues values;
dc6f92b8 5149
dbc4e1c1 5150 values.function = GXxor;
7556890b
RS
5151 values.foreground = (f->output_data.x->foreground_pixel
5152 ^ f->output_data.x->background_pixel);
58769bee 5153
334208b7 5154 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dbc4e1c1
JB
5155 GCFunction | GCForeground, &values);
5156 }
dc6f92b8 5157
dbc4e1c1 5158 {
e84e14c3
RS
5159 /* Get the height not including a menu bar widget. */
5160 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5161 /* Height of each line to flash. */
5162 int flash_height = FRAME_LINE_HEIGHT (f);
5163 /* These will be the left and right margins of the rectangles. */
5164 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5165 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5166
5167 int width;
5168
5169 /* Don't flash the area between a scroll bar and the frame
5170 edge it is next to. */
5171 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5172 {
5173 case vertical_scroll_bar_left:
5174 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5175 break;
5176
5177 case vertical_scroll_bar_right:
5178 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5179 break;
06a2c219
GM
5180
5181 default:
5182 break;
e84e14c3
RS
5183 }
5184
5185 width = flash_right - flash_left;
5186
5187 /* If window is tall, flash top and bottom line. */
5188 if (height > 3 * FRAME_LINE_HEIGHT (f))
5189 {
5190 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5191 flash_left,
5192 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5193 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5194 width, flash_height);
5195 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5196 flash_left,
5197 (height - flash_height
5198 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5199 width, flash_height);
5200 }
5201 else
5202 /* If it is short, flash it all. */
5203 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5204 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5205 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
dc6f92b8 5206
06a2c219 5207 x_flush (f);
dc6f92b8 5208
dbc4e1c1 5209 {
06a2c219 5210 struct timeval wakeup;
dc6f92b8 5211
66c30ea1 5212 EMACS_GET_TIME (wakeup);
dc6f92b8 5213
dbc4e1c1
JB
5214 /* Compute time to wait until, propagating carry from usecs. */
5215 wakeup.tv_usec += 150000;
5216 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5217 wakeup.tv_usec %= 1000000;
5218
5219 /* Keep waiting until past the time wakeup. */
5220 while (1)
5221 {
5222 struct timeval timeout;
5223
66c30ea1 5224 EMACS_GET_TIME (timeout);
dbc4e1c1
JB
5225
5226 /* In effect, timeout = wakeup - timeout.
5227 Break if result would be negative. */
5228 if (timeval_subtract (&timeout, wakeup, timeout))
5229 break;
5230
5231 /* Try to wait that long--but we might wake up sooner. */
c32cdd9a 5232 select (0, NULL, NULL, NULL, &timeout);
dbc4e1c1
JB
5233 }
5234 }
58769bee 5235
e84e14c3
RS
5236 /* If window is tall, flash top and bottom line. */
5237 if (height > 3 * FRAME_LINE_HEIGHT (f))
5238 {
5239 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5240 flash_left,
5241 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5242 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5243 width, flash_height);
5244 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5245 flash_left,
5246 (height - flash_height
5247 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5248 width, flash_height);
5249 }
5250 else
5251 /* If it is short, flash it all. */
5252 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5253 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5254 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5255
334208b7 5256 XFreeGC (FRAME_X_DISPLAY (f), gc);
06a2c219 5257 x_flush (f);
dc6f92b8 5258 }
dbc4e1c1
JB
5259 }
5260
5261 UNBLOCK_INPUT;
dc6f92b8
JB
5262}
5263
06a2c219 5264#endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
dbc4e1c1
JB
5265
5266
dc6f92b8
JB
5267/* Make audible bell. */
5268
dfcf069d 5269void
dc6f92b8
JB
5270XTring_bell ()
5271{
b86bd3dd
GM
5272 struct frame *f = SELECTED_FRAME ();
5273
5274 if (FRAME_X_DISPLAY (f))
5275 {
dbc4e1c1 5276#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
b86bd3dd
GM
5277 if (visible_bell)
5278 XTflash (f);
5279 else
dbc4e1c1 5280#endif
b86bd3dd
GM
5281 {
5282 BLOCK_INPUT;
5283 XBell (FRAME_X_DISPLAY (f), 0);
5284 XFlush (FRAME_X_DISPLAY (f));
5285 UNBLOCK_INPUT;
5286 }
dc6f92b8
JB
5287 }
5288}
06a2c219 5289
dc6f92b8 5290\f
06a2c219
GM
5291/* Specify how many text lines, from the top of the window,
5292 should be affected by insert-lines and delete-lines operations.
5293 This, and those operations, are used only within an update
5294 that is bounded by calls to x_update_begin and x_update_end. */
dc6f92b8 5295
dfcf069d 5296static void
06a2c219
GM
5297XTset_terminal_window (n)
5298 register int n;
dc6f92b8 5299{
06a2c219 5300 /* This function intentionally left blank. */
dc6f92b8
JB
5301}
5302
06a2c219
GM
5303
5304\f
5305/***********************************************************************
5306 Line Dance
5307 ***********************************************************************/
5308
5309/* Perform an insert-lines or delete-lines operation, inserting N
5310 lines or deleting -N lines at vertical position VPOS. */
5311
dfcf069d 5312static void
06a2c219
GM
5313x_ins_del_lines (vpos, n)
5314 int vpos, n;
dc6f92b8
JB
5315{
5316 abort ();
5317}
06a2c219
GM
5318
5319
5320/* Scroll part of the display as described by RUN. */
dc6f92b8 5321
dfcf069d 5322static void
06a2c219
GM
5323x_scroll_run (w, run)
5324 struct window *w;
5325 struct run *run;
dc6f92b8 5326{
06a2c219
GM
5327 struct frame *f = XFRAME (w->frame);
5328 int x, y, width, height, from_y, to_y, bottom_y;
5329
5330 /* Get frame-relative bounding box of the text display area of W,
5331 without mode lines. Include in this box the flags areas to the
5332 left and right of W. */
5333 window_box (w, -1, &x, &y, &width, &height);
110859fc
GM
5334 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5335 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
5336
5337 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5338 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5339 bottom_y = y + height;
dc6f92b8 5340
06a2c219
GM
5341 if (to_y < from_y)
5342 {
5343 /* Scrolling up. Make sure we don't copy part of the mode
5344 line at the bottom. */
5345 if (from_y + run->height > bottom_y)
5346 height = bottom_y - from_y;
5347 else
5348 height = run->height;
5349 }
dc6f92b8 5350 else
06a2c219
GM
5351 {
5352 /* Scolling down. Make sure we don't copy over the mode line.
5353 at the bottom. */
5354 if (to_y + run->height > bottom_y)
5355 height = bottom_y - to_y;
5356 else
5357 height = run->height;
5358 }
7a13e894 5359
06a2c219
GM
5360 BLOCK_INPUT;
5361
5362 /* Cursor off. Will be switched on again in x_update_window_end. */
5363 updated_window = w;
5364 x_clear_cursor (w);
5365
5366 XCopyArea (FRAME_X_DISPLAY (f),
5367 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5368 f->output_data.x->normal_gc,
5369 x, from_y,
5370 width, height,
5371 x, to_y);
5372
5373 UNBLOCK_INPUT;
5374}
dc6f92b8 5375
dc6f92b8 5376
06a2c219
GM
5377\f
5378/***********************************************************************
5379 Exposure Events
5380 ***********************************************************************/
5381
5382/* Redisplay an exposed area of frame F. X and Y are the upper-left
5383 corner of the exposed rectangle. W and H are width and height of
5384 the exposed area. All are pixel values. W or H zero means redraw
5385 the entire frame. */
dc6f92b8 5386
06a2c219
GM
5387static void
5388expose_frame (f, x, y, w, h)
5389 struct frame *f;
5390 int x, y, w, h;
dc6f92b8 5391{
06a2c219 5392 XRectangle r;
dc6f92b8 5393
06a2c219 5394 TRACE ((stderr, "expose_frame "));
dc6f92b8 5395
06a2c219
GM
5396 /* No need to redraw if frame will be redrawn soon. */
5397 if (FRAME_GARBAGED_P (f))
dc6f92b8 5398 {
06a2c219
GM
5399 TRACE ((stderr, " garbaged\n"));
5400 return;
5401 }
5402
5403 /* If basic faces haven't been realized yet, there is no point in
5404 trying to redraw anything. This can happen when we get an expose
5405 event while Emacs is starting, e.g. by moving another window. */
5406 if (FRAME_FACE_CACHE (f) == NULL
5407 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5408 {
5409 TRACE ((stderr, " no faces\n"));
5410 return;
58769bee 5411 }
06a2c219
GM
5412
5413 if (w == 0 || h == 0)
58769bee 5414 {
06a2c219
GM
5415 r.x = r.y = 0;
5416 r.width = CANON_X_UNIT (f) * f->width;
5417 r.height = CANON_Y_UNIT (f) * f->height;
dc6f92b8
JB
5418 }
5419 else
5420 {
06a2c219
GM
5421 r.x = x;
5422 r.y = y;
5423 r.width = w;
5424 r.height = h;
5425 }
5426
5427 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5428 expose_window_tree (XWINDOW (f->root_window), &r);
5429
9ea173e8 5430 if (WINDOWP (f->tool_bar_window))
06a2c219 5431 {
9ea173e8 5432 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
5433 XRectangle window_rect;
5434 XRectangle intersection_rect;
5435 int window_x, window_y, window_width, window_height;
5436
5437
5438 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5439 window_rect.x = window_x;
5440 window_rect.y = window_y;
5441 window_rect.width = window_width;
5442 window_rect.height = window_height;
5443
5444 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5445 expose_window (w, &intersection_rect);
5446 }
5447
5448#ifndef USE_X_TOOLKIT
5449 if (WINDOWP (f->menu_bar_window))
5450 {
5451 struct window *w = XWINDOW (f->menu_bar_window);
5452 XRectangle window_rect;
5453 XRectangle intersection_rect;
5454 int window_x, window_y, window_width, window_height;
5455
5456
5457 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5458 window_rect.x = window_x;
5459 window_rect.y = window_y;
5460 window_rect.width = window_width;
5461 window_rect.height = window_height;
5462
5463 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5464 expose_window (w, &intersection_rect);
dc6f92b8 5465 }
06a2c219 5466#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
5467}
5468
06a2c219
GM
5469
5470/* Redraw (parts) of all windows in the window tree rooted at W that
5471 intersect R. R contains frame pixel coordinates. */
5472
58769bee 5473static void
06a2c219
GM
5474expose_window_tree (w, r)
5475 struct window *w;
5476 XRectangle *r;
dc6f92b8 5477{
06a2c219
GM
5478 while (w)
5479 {
5480 if (!NILP (w->hchild))
5481 expose_window_tree (XWINDOW (w->hchild), r);
5482 else if (!NILP (w->vchild))
5483 expose_window_tree (XWINDOW (w->vchild), r);
5484 else
5485 {
5486 XRectangle window_rect;
5487 XRectangle intersection_rect;
5488 struct frame *f = XFRAME (w->frame);
5489 int window_x, window_y, window_width, window_height;
5490
5491 /* Frame-relative pixel rectangle of W. */
5492 window_box (w, -1, &window_x, &window_y, &window_width,
5493 &window_height);
5494 window_rect.x
5495 = (window_x
110859fc 5496 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
714dc26c 5497 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
06a2c219
GM
5498 window_rect.y = window_y;
5499 window_rect.width
5500 = (window_width
110859fc 5501 + FRAME_X_FLAGS_AREA_WIDTH (f)
06a2c219
GM
5502 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5503 window_rect.height
5504 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5505
5506 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5507 expose_window (w, &intersection_rect);
5508 }
58769bee 5509
06a2c219
GM
5510 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5511 }
5512}
58769bee 5513
dc6f92b8 5514
06a2c219
GM
5515/* Redraw the part of glyph row area AREA of glyph row ROW on window W
5516 which intersects rectangle R. R is in window-relative coordinates. */
5517
5518static void
5519expose_area (w, row, r, area)
5520 struct window *w;
5521 struct glyph_row *row;
5522 XRectangle *r;
5523 enum glyph_row_area area;
5524{
5525 int x;
5526 struct glyph *first = row->glyphs[area];
5527 struct glyph *end = row->glyphs[area] + row->used[area];
5528 struct glyph *last;
5529 int first_x;
5530
5531 /* Set x to the window-relative start position for drawing glyphs of
5532 AREA. The first glyph of the text area can be partially visible.
5533 The first glyphs of other areas cannot. */
5534 if (area == LEFT_MARGIN_AREA)
5535 x = 0;
5536 else if (area == TEXT_AREA)
5537 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5538 else
5539 x = (window_box_width (w, LEFT_MARGIN_AREA)
5540 + window_box_width (w, TEXT_AREA));
5541
6fb13182
GM
5542 if (area == TEXT_AREA && row->fill_line_p)
5543 /* If row extends face to end of line write the whole line. */
5544 x_draw_glyphs (w, x, row, area,
5545 0, row->used[area],
06a2c219 5546 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5547 NULL, NULL, 0);
6fb13182
GM
5548 else
5549 {
5550 /* Find the first glyph that must be redrawn. */
5551 while (first < end
5552 && x + first->pixel_width < r->x)
5553 {
5554 x += first->pixel_width;
5555 ++first;
5556 }
5557
5558 /* Find the last one. */
5559 last = first;
5560 first_x = x;
5561 while (last < end
5562 && x < r->x + r->width)
5563 {
5564 x += last->pixel_width;
5565 ++last;
5566 }
5567
5568 /* Repaint. */
5569 if (last > first)
5570 x_draw_glyphs (w, first_x, row, area,
5571 first - row->glyphs[area],
5572 last - row->glyphs[area],
5573 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5574 NULL, NULL, 0);
5575 }
06a2c219
GM
5576}
5577
58769bee 5578
06a2c219
GM
5579/* Redraw the parts of the glyph row ROW on window W intersecting
5580 rectangle R. R is in window-relative coordinates. */
dc6f92b8 5581
06a2c219
GM
5582static void
5583expose_line (w, row, r)
5584 struct window *w;
5585 struct glyph_row *row;
5586 XRectangle *r;
5587{
5588 xassert (row->enabled_p);
5589
5590 if (row->mode_line_p || w->pseudo_window_p)
5591 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5592 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5593 NULL, NULL, 0);
06a2c219
GM
5594 else
5595 {
5596 if (row->used[LEFT_MARGIN_AREA])
5597 expose_area (w, row, r, LEFT_MARGIN_AREA);
5598 if (row->used[TEXT_AREA])
5599 expose_area (w, row, r, TEXT_AREA);
5600 if (row->used[RIGHT_MARGIN_AREA])
5601 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5602 x_draw_row_bitmaps (w, row);
5603 }
5604}
dc6f92b8 5605
58769bee 5606
06a2c219
GM
5607/* Return non-zero if W's cursor intersects rectangle R. */
5608
5609static int
5610x_phys_cursor_in_rect_p (w, r)
5611 struct window *w;
5612 XRectangle *r;
5613{
5614 XRectangle cr, result;
5615 struct glyph *cursor_glyph;
5616
5617 cursor_glyph = get_phys_cursor_glyph (w);
5618 if (cursor_glyph)
5619 {
5620 cr.x = w->phys_cursor.x;
5621 cr.y = w->phys_cursor.y;
5622 cr.width = cursor_glyph->pixel_width;
5623 cr.height = w->phys_cursor_height;
5624 return x_intersect_rectangles (&cr, r, &result);
5625 }
5626 else
5627 return 0;
dc6f92b8 5628}
dc6f92b8 5629
06a2c219
GM
5630
5631/* Redraw a rectangle of window W. R is a rectangle in window
5632 relative coordinates. Call this function with input blocked. */
dc6f92b8
JB
5633
5634static void
06a2c219
GM
5635expose_window (w, r)
5636 struct window *w;
5637 XRectangle *r;
dc6f92b8 5638{
06a2c219
GM
5639 struct glyph_row *row;
5640 int y;
5641 int yb = window_text_bottom_y (w);
5642 int cursor_cleared_p;
dc6f92b8 5643
80c32bcc
GM
5644 /* If window is not yet fully initialized, do nothing. This can
5645 happen when toolkit scroll bars are used and a window is split.
5646 Reconfiguring the scroll bar will generate an expose for a newly
5647 created window. */
5648 if (w->current_matrix == NULL)
5649 return;
5650
06a2c219
GM
5651 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5652 r->x, r->y, r->width, r->height));
dc6f92b8 5653
06a2c219
GM
5654 /* Convert to window coordinates. */
5655 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5656 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
dc6f92b8 5657
06a2c219
GM
5658 /* Turn off the cursor. */
5659 if (!w->pseudo_window_p
5660 && x_phys_cursor_in_rect_p (w, r))
5661 {
5662 x_clear_cursor (w);
5663 cursor_cleared_p = 1;
5664 }
5665 else
5666 cursor_cleared_p = 0;
5667
5668 /* Find the first row intersecting the rectangle R. */
5669 row = w->current_matrix->rows;
5670 y = 0;
5671 while (row->enabled_p
5672 && y < yb
5673 && y + row->height < r->y)
5674 {
5675 y += row->height;
5676 ++row;
5677 }
5678
dc6f92b8 5679 /* Display the text in the rectangle, one text line at a time. */
06a2c219
GM
5680 while (row->enabled_p
5681 && y < yb
5682 && y < r->y + r->height)
5683 {
5684 expose_line (w, row, r);
5685 y += row->height;
5686 ++row;
5687 }
5688
5689 /* Display the mode line if there is one. */
5690 if (WINDOW_WANTS_MODELINE_P (w)
5691 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5692 row->enabled_p)
5693 && row->y < r->y + r->height)
5694 expose_line (w, row, r);
dc6f92b8 5695
06a2c219 5696 if (!w->pseudo_window_p)
dc6f92b8 5697 {
06a2c219
GM
5698 /* Draw border between windows. */
5699 x_draw_vertical_border (w);
5700
5701 /* Turn the cursor on again. */
5702 if (cursor_cleared_p)
5703 x_update_window_cursor (w, 1);
5704 }
5705}
dc6f92b8 5706
dc6f92b8 5707
06a2c219
GM
5708/* Determine the intersection of two rectangles R1 and R2. Return
5709 the intersection in *RESULT. Value is non-zero if RESULT is not
5710 empty. */
5711
5712static int
5713x_intersect_rectangles (r1, r2, result)
5714 XRectangle *r1, *r2, *result;
5715{
5716 XRectangle *left, *right;
5717 XRectangle *upper, *lower;
5718 int intersection_p = 0;
5719
5720 /* Rearrange so that R1 is the left-most rectangle. */
5721 if (r1->x < r2->x)
5722 left = r1, right = r2;
5723 else
5724 left = r2, right = r1;
5725
5726 /* X0 of the intersection is right.x0, if this is inside R1,
5727 otherwise there is no intersection. */
5728 if (right->x <= left->x + left->width)
5729 {
5730 result->x = right->x;
5731
5732 /* The right end of the intersection is the minimum of the
5733 the right ends of left and right. */
5734 result->width = (min (left->x + left->width, right->x + right->width)
5735 - result->x);
5736
5737 /* Same game for Y. */
5738 if (r1->y < r2->y)
5739 upper = r1, lower = r2;
5740 else
5741 upper = r2, lower = r1;
5742
5743 /* The upper end of the intersection is lower.y0, if this is inside
5744 of upper. Otherwise, there is no intersection. */
5745 if (lower->y <= upper->y + upper->height)
dc43ef94 5746 {
06a2c219
GM
5747 result->y = lower->y;
5748
5749 /* The lower end of the intersection is the minimum of the lower
5750 ends of upper and lower. */
5751 result->height = (min (lower->y + lower->height,
5752 upper->y + upper->height)
5753 - result->y);
5754 intersection_p = 1;
dc43ef94 5755 }
dc6f92b8
JB
5756 }
5757
06a2c219 5758 return intersection_p;
dc6f92b8 5759}
06a2c219
GM
5760
5761
5762
5763
dc6f92b8 5764\f
dc6f92b8 5765static void
334208b7
RS
5766frame_highlight (f)
5767 struct frame *f;
dc6f92b8 5768{
b3e1e05c
JB
5769 /* We used to only do this if Vx_no_window_manager was non-nil, but
5770 the ICCCM (section 4.1.6) says that the window's border pixmap
5771 and border pixel are window attributes which are "private to the
5772 client", so we can always change it to whatever we want. */
5773 BLOCK_INPUT;
334208b7 5774 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5775 f->output_data.x->border_pixel);
b3e1e05c 5776 UNBLOCK_INPUT;
5d46f928 5777 x_update_cursor (f, 1);
dc6f92b8
JB
5778}
5779
5780static void
334208b7
RS
5781frame_unhighlight (f)
5782 struct frame *f;
dc6f92b8 5783{
b3e1e05c
JB
5784 /* We used to only do this if Vx_no_window_manager was non-nil, but
5785 the ICCCM (section 4.1.6) says that the window's border pixmap
5786 and border pixel are window attributes which are "private to the
5787 client", so we can always change it to whatever we want. */
5788 BLOCK_INPUT;
334208b7 5789 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5790 f->output_data.x->border_tile);
b3e1e05c 5791 UNBLOCK_INPUT;
5d46f928 5792 x_update_cursor (f, 1);
dc6f92b8 5793}
dc6f92b8 5794
f676886a
JB
5795/* The focus has changed. Update the frames as necessary to reflect
5796 the new situation. Note that we can't change the selected frame
c5acd733 5797 here, because the Lisp code we are interrupting might become confused.
eb8c3be9 5798 Each event gets marked with the frame in which it occurred, so the
c5acd733 5799 Lisp code can tell when the switch took place by examining the events. */
dc6f92b8 5800
6d4238f3 5801static void
0f941935
KH
5802x_new_focus_frame (dpyinfo, frame)
5803 struct x_display_info *dpyinfo;
f676886a 5804 struct frame *frame;
dc6f92b8 5805{
0f941935 5806 struct frame *old_focus = dpyinfo->x_focus_frame;
dc6f92b8 5807
0f941935 5808 if (frame != dpyinfo->x_focus_frame)
dc6f92b8 5809 {
58769bee 5810 /* Set this before calling other routines, so that they see
f676886a 5811 the correct value of x_focus_frame. */
0f941935 5812 dpyinfo->x_focus_frame = frame;
6d4238f3
JB
5813
5814 if (old_focus && old_focus->auto_lower)
f676886a 5815 x_lower_frame (old_focus);
dc6f92b8
JB
5816
5817#if 0
f676886a 5818 selected_frame = frame;
e0c1aef2
KH
5819 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
5820 selected_frame);
f676886a
JB
5821 Fselect_window (selected_frame->selected_window);
5822 choose_minibuf_frame ();
c118dd06 5823#endif /* ! 0 */
dc6f92b8 5824
0f941935
KH
5825 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
5826 pending_autoraise_frame = dpyinfo->x_focus_frame;
0134a210
RS
5827 else
5828 pending_autoraise_frame = 0;
6d4238f3 5829 }
dc6f92b8 5830
0f941935 5831 x_frame_rehighlight (dpyinfo);
6d4238f3
JB
5832}
5833
37c2c98b
RS
5834/* Handle an event saying the mouse has moved out of an Emacs frame. */
5835
5836void
0f941935
KH
5837x_mouse_leave (dpyinfo)
5838 struct x_display_info *dpyinfo;
37c2c98b 5839{
0f941935 5840 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
37c2c98b 5841}
6d4238f3 5842
f451eb13
JB
5843/* The focus has changed, or we have redirected a frame's focus to
5844 another frame (this happens when a frame uses a surrogate
06a2c219 5845 mini-buffer frame). Shift the highlight as appropriate.
0f941935
KH
5846
5847 The FRAME argument doesn't necessarily have anything to do with which
06a2c219 5848 frame is being highlighted or un-highlighted; we only use it to find
0f941935 5849 the appropriate X display info. */
06a2c219 5850
6d4238f3 5851static void
0f941935
KH
5852XTframe_rehighlight (frame)
5853 struct frame *frame;
6d4238f3 5854{
0f941935
KH
5855 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
5856}
6d4238f3 5857
0f941935
KH
5858static void
5859x_frame_rehighlight (dpyinfo)
5860 struct x_display_info *dpyinfo;
5861{
5862 struct frame *old_highlight = dpyinfo->x_highlight_frame;
5863
5864 if (dpyinfo->x_focus_frame)
6d4238f3 5865 {
0f941935
KH
5866 dpyinfo->x_highlight_frame
5867 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
5868 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
5869 : dpyinfo->x_focus_frame);
5870 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
f451eb13 5871 {
0f941935
KH
5872 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
5873 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
f451eb13 5874 }
dc6f92b8 5875 }
6d4238f3 5876 else
0f941935 5877 dpyinfo->x_highlight_frame = 0;
dc6f92b8 5878
0f941935 5879 if (dpyinfo->x_highlight_frame != old_highlight)
6d4238f3
JB
5880 {
5881 if (old_highlight)
f676886a 5882 frame_unhighlight (old_highlight);
0f941935
KH
5883 if (dpyinfo->x_highlight_frame)
5884 frame_highlight (dpyinfo->x_highlight_frame);
6d4238f3 5885 }
dc6f92b8 5886}
06a2c219
GM
5887
5888
dc6f92b8 5889\f
06a2c219 5890/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
dc6f92b8 5891
28430d3c
JB
5892/* Initialize mode_switch_bit and modifier_meaning. */
5893static void
334208b7
RS
5894x_find_modifier_meanings (dpyinfo)
5895 struct x_display_info *dpyinfo;
28430d3c 5896{
f689eb05 5897 int min_code, max_code;
28430d3c
JB
5898 KeySym *syms;
5899 int syms_per_code;
5900 XModifierKeymap *mods;
5901
334208b7
RS
5902 dpyinfo->meta_mod_mask = 0;
5903 dpyinfo->shift_lock_mask = 0;
5904 dpyinfo->alt_mod_mask = 0;
5905 dpyinfo->super_mod_mask = 0;
5906 dpyinfo->hyper_mod_mask = 0;
58769bee 5907
9658a521 5908#ifdef HAVE_X11R4
334208b7 5909 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
9658a521 5910#else
4a60f8c5
RS
5911 min_code = dpyinfo->display->min_keycode;
5912 max_code = dpyinfo->display->max_keycode;
9658a521
JB
5913#endif
5914
334208b7 5915 syms = XGetKeyboardMapping (dpyinfo->display,
28430d3c
JB
5916 min_code, max_code - min_code + 1,
5917 &syms_per_code);
334208b7 5918 mods = XGetModifierMapping (dpyinfo->display);
28430d3c 5919
58769bee 5920 /* Scan the modifier table to see which modifier bits the Meta and
11edeb03 5921 Alt keysyms are on. */
28430d3c 5922 {
06a2c219 5923 int row, col; /* The row and column in the modifier table. */
28430d3c
JB
5924
5925 for (row = 3; row < 8; row++)
5926 for (col = 0; col < mods->max_keypermod; col++)
5927 {
0299d313
RS
5928 KeyCode code
5929 = mods->modifiermap[(row * mods->max_keypermod) + col];
28430d3c 5930
af92970c
KH
5931 /* Zeroes are used for filler. Skip them. */
5932 if (code == 0)
5933 continue;
5934
28430d3c
JB
5935 /* Are any of this keycode's keysyms a meta key? */
5936 {
5937 int code_col;
5938
5939 for (code_col = 0; code_col < syms_per_code; code_col++)
5940 {
f689eb05 5941 int sym = syms[((code - min_code) * syms_per_code) + code_col];
28430d3c 5942
f689eb05 5943 switch (sym)
28430d3c 5944 {
f689eb05
JB
5945 case XK_Meta_L:
5946 case XK_Meta_R:
334208b7 5947 dpyinfo->meta_mod_mask |= (1 << row);
28430d3c 5948 break;
f689eb05
JB
5949
5950 case XK_Alt_L:
5951 case XK_Alt_R:
334208b7 5952 dpyinfo->alt_mod_mask |= (1 << row);
a3c44b14
RS
5953 break;
5954
5955 case XK_Hyper_L:
5956 case XK_Hyper_R:
334208b7 5957 dpyinfo->hyper_mod_mask |= (1 << row);
a3c44b14
RS
5958 break;
5959
5960 case XK_Super_L:
5961 case XK_Super_R:
334208b7 5962 dpyinfo->super_mod_mask |= (1 << row);
f689eb05 5963 break;
11edeb03
JB
5964
5965 case XK_Shift_Lock:
5966 /* Ignore this if it's not on the lock modifier. */
5967 if ((1 << row) == LockMask)
334208b7 5968 dpyinfo->shift_lock_mask = LockMask;
11edeb03 5969 break;
28430d3c
JB
5970 }
5971 }
5972 }
5973 }
5974 }
5975
f689eb05 5976 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
334208b7 5977 if (! dpyinfo->meta_mod_mask)
a3c44b14 5978 {
334208b7
RS
5979 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
5980 dpyinfo->alt_mod_mask = 0;
a3c44b14 5981 }
f689eb05 5982
148c4b70
RS
5983 /* If some keys are both alt and meta,
5984 make them just meta, not alt. */
334208b7 5985 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
148c4b70 5986 {
334208b7 5987 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
148c4b70 5988 }
58769bee 5989
28430d3c 5990 XFree ((char *) syms);
f689eb05 5991 XFreeModifiermap (mods);
28430d3c
JB
5992}
5993
dfeccd2d
JB
5994/* Convert between the modifier bits X uses and the modifier bits
5995 Emacs uses. */
06a2c219 5996
7c5283e4 5997static unsigned int
334208b7
RS
5998x_x_to_emacs_modifiers (dpyinfo, state)
5999 struct x_display_info *dpyinfo;
dc6f92b8
JB
6000 unsigned int state;
6001{
334208b7
RS
6002 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6003 | ((state & ControlMask) ? ctrl_modifier : 0)
6004 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
6005 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
6006 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
6007 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
dc6f92b8
JB
6008}
6009
dfeccd2d 6010static unsigned int
334208b7
RS
6011x_emacs_to_x_modifiers (dpyinfo, state)
6012 struct x_display_info *dpyinfo;
dfeccd2d
JB
6013 unsigned int state;
6014{
334208b7
RS
6015 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
6016 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
6017 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
6018 | ((state & shift_modifier) ? ShiftMask : 0)
6019 | ((state & ctrl_modifier) ? ControlMask : 0)
6020 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
dfeccd2d 6021}
d047c4eb
KH
6022
6023/* Convert a keysym to its name. */
6024
6025char *
6026x_get_keysym_name (keysym)
6027 KeySym keysym;
6028{
6029 char *value;
6030
6031 BLOCK_INPUT;
6032 value = XKeysymToString (keysym);
6033 UNBLOCK_INPUT;
6034
6035 return value;
6036}
06a2c219
GM
6037
6038
e4571a43
JB
6039\f
6040/* Mouse clicks and mouse movement. Rah. */
e4571a43 6041
06a2c219
GM
6042/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6043 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6044 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6045 not force the value into range. */
69388238 6046
c8dba240 6047void
69388238 6048pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
e4571a43 6049 FRAME_PTR f;
69388238 6050 register int pix_x, pix_y;
e4571a43
JB
6051 register int *x, *y;
6052 XRectangle *bounds;
69388238 6053 int noclip;
e4571a43 6054{
06a2c219 6055 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
69388238
RS
6056 even for negative values. */
6057 if (pix_x < 0)
7556890b 6058 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
69388238 6059 if (pix_y < 0)
7556890b 6060 pix_y -= (f)->output_data.x->line_height - 1;
69388238 6061
e4571a43
JB
6062 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6063 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6064
6065 if (bounds)
6066 {
7556890b
RS
6067 bounds->width = FONT_WIDTH (f->output_data.x->font);
6068 bounds->height = f->output_data.x->line_height;
e4571a43
JB
6069 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6070 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6071 }
6072
69388238
RS
6073 if (!noclip)
6074 {
6075 if (pix_x < 0)
6076 pix_x = 0;
3cbd2e0b
RS
6077 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6078 pix_x = FRAME_WINDOW_WIDTH (f);
69388238
RS
6079
6080 if (pix_y < 0)
6081 pix_y = 0;
6082 else if (pix_y > f->height)
6083 pix_y = f->height;
6084 }
e4571a43
JB
6085
6086 *x = pix_x;
6087 *y = pix_y;
6088}
6089
06a2c219
GM
6090
6091/* Given HPOS/VPOS in the current matrix of W, return corresponding
6092 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6093 can't tell the positions because W's display is not up to date,
6094 return 0. */
6095
6096int
6097glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6098 struct window *w;
6099 int hpos, vpos;
6100 int *frame_x, *frame_y;
2b5c9e71 6101{
06a2c219
GM
6102 int success_p;
6103
6104 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6105 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6106
6107 if (display_completed)
6108 {
6109 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6110 struct glyph *glyph = row->glyphs[TEXT_AREA];
6111 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6112
6113 *frame_y = row->y;
6114 *frame_x = row->x;
6115 while (glyph < end)
6116 {
6117 *frame_x += glyph->pixel_width;
6118 ++glyph;
6119 }
6120
6121 success_p = 1;
6122 }
6123 else
6124 {
6125 *frame_y = *frame_x = 0;
6126 success_p = 0;
6127 }
6128
6129 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6130 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6131 return success_p;
2b5c9e71
RS
6132}
6133
06a2c219 6134
dc6f92b8
JB
6135/* Prepare a mouse-event in *RESULT for placement in the input queue.
6136
6137 If the event is a button press, then note that we have grabbed
f451eb13 6138 the mouse. */
dc6f92b8
JB
6139
6140static Lisp_Object
f451eb13 6141construct_mouse_click (result, event, f)
dc6f92b8
JB
6142 struct input_event *result;
6143 XButtonEvent *event;
f676886a 6144 struct frame *f;
dc6f92b8 6145{
f451eb13 6146 /* Make the event type no_event; we'll change that when we decide
dc6f92b8 6147 otherwise. */
f451eb13 6148 result->kind = mouse_click;
69388238 6149 result->code = event->button - Button1;
1113d9db 6150 result->timestamp = event->time;
334208b7
RS
6151 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6152 event->state)
f689eb05 6153 | (event->type == ButtonRelease
58769bee 6154 ? up_modifier
f689eb05 6155 : down_modifier));
dc6f92b8 6156
06a2c219
GM
6157 XSETINT (result->x, event->x);
6158 XSETINT (result->y, event->y);
6159 XSETFRAME (result->frame_or_window, f);
0f8aabe9 6160 result->arg = Qnil;
06a2c219 6161 return Qnil;
dc6f92b8 6162}
b849c413 6163
69388238 6164\f
90e65f07
JB
6165/* Function to report a mouse movement to the mainstream Emacs code.
6166 The input handler calls this.
6167
6168 We have received a mouse movement event, which is given in *event.
6169 If the mouse is over a different glyph than it was last time, tell
6170 the mainstream emacs code by setting mouse_moved. If not, ask for
6171 another motion event, so we can check again the next time it moves. */
b8009dd1 6172
06a2c219
GM
6173static XMotionEvent last_mouse_motion_event;
6174static Lisp_Object last_mouse_motion_frame;
6175
90e65f07 6176static void
12ba150f 6177note_mouse_movement (frame, event)
f676886a 6178 FRAME_PTR frame;
90e65f07 6179 XMotionEvent *event;
90e65f07 6180{
e5d77022 6181 last_mouse_movement_time = event->time;
06a2c219
GM
6182 last_mouse_motion_event = *event;
6183 XSETFRAME (last_mouse_motion_frame, frame);
e5d77022 6184
27f338af
RS
6185 if (event->window != FRAME_X_WINDOW (frame))
6186 {
39d8bb4d 6187 frame->mouse_moved = 1;
27f338af 6188 last_mouse_scroll_bar = Qnil;
27f338af 6189 note_mouse_highlight (frame, -1, -1);
27f338af
RS
6190 }
6191
90e65f07 6192 /* Has the mouse moved off the glyph it was on at the last sighting? */
27f338af
RS
6193 else if (event->x < last_mouse_glyph.x
6194 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6195 || event->y < last_mouse_glyph.y
6196 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
12ba150f 6197 {
39d8bb4d 6198 frame->mouse_moved = 1;
ab648270 6199 last_mouse_scroll_bar = Qnil;
b8009dd1 6200 note_mouse_highlight (frame, event->x, event->y);
90e65f07
JB
6201 }
6202}
6203
bf1c0ba1 6204/* This is used for debugging, to turn off note_mouse_highlight. */
bf1c0ba1 6205
06a2c219
GM
6206 int disable_mouse_highlight;
6207
6208
6209\f
6210/************************************************************************
6211 Mouse Face
6212 ************************************************************************/
6213
6214/* Find the glyph under window-relative coordinates X/Y in window W.
6215 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6216 strings. Return in *HPOS and *VPOS the row and column number of
6217 the glyph found. Return in *AREA the glyph area containing X.
6218 Value is a pointer to the glyph found or null if X/Y is not on
6219 text, or we can't tell because W's current matrix is not up to
6220 date. */
6221
6222static struct glyph *
6223x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6224 struct window *w;
6225 int x, y;
6226 int *hpos, *vpos, *area;
6227{
6228 struct glyph *glyph, *end;
3e71d8f2 6229 struct glyph_row *row = NULL;
06a2c219
GM
6230 int x0, i, left_area_width;
6231
6232 /* Find row containing Y. Give up if some row is not enabled. */
6233 for (i = 0; i < w->current_matrix->nrows; ++i)
6234 {
6235 row = MATRIX_ROW (w->current_matrix, i);
6236 if (!row->enabled_p)
6237 return NULL;
6238 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6239 break;
6240 }
6241
6242 *vpos = i;
6243 *hpos = 0;
6244
6245 /* Give up if Y is not in the window. */
6246 if (i == w->current_matrix->nrows)
6247 return NULL;
6248
6249 /* Get the glyph area containing X. */
6250 if (w->pseudo_window_p)
6251 {
6252 *area = TEXT_AREA;
6253 x0 = 0;
6254 }
6255 else
6256 {
6257 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6258 if (x < left_area_width)
6259 {
6260 *area = LEFT_MARGIN_AREA;
6261 x0 = 0;
6262 }
6263 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6264 {
6265 *area = TEXT_AREA;
6266 x0 = row->x + left_area_width;
6267 }
6268 else
6269 {
6270 *area = RIGHT_MARGIN_AREA;
6271 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6272 }
6273 }
6274
6275 /* Find glyph containing X. */
6276 glyph = row->glyphs[*area];
6277 end = glyph + row->used[*area];
6278 while (glyph < end)
6279 {
6280 if (x < x0 + glyph->pixel_width)
6281 {
6282 if (w->pseudo_window_p)
6283 break;
6284 else if (BUFFERP (glyph->object))
6285 break;
6286 }
6287
6288 x0 += glyph->pixel_width;
6289 ++glyph;
6290 }
6291
6292 if (glyph == end)
6293 return NULL;
6294
6295 *hpos = glyph - row->glyphs[*area];
6296 return glyph;
6297}
6298
6299
6300/* Convert frame-relative x/y to coordinates relative to window W.
6301 Takes pseudo-windows into account. */
6302
6303static void
6304frame_to_window_pixel_xy (w, x, y)
6305 struct window *w;
6306 int *x, *y;
6307{
6308 if (w->pseudo_window_p)
6309 {
6310 /* A pseudo-window is always full-width, and starts at the
6311 left edge of the frame, plus a frame border. */
6312 struct frame *f = XFRAME (w->frame);
6313 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6314 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6315 }
6316 else
6317 {
6318 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6319 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6320 }
6321}
6322
6323
6324/* Take proper action when mouse has moved to the mode or top line of
6325 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6326 mode line. X is relative to the start of the text display area of
6327 W, so the width of bitmap areas and scroll bars must be subtracted
6328 to get a position relative to the start of the mode line. */
6329
6330static void
6331note_mode_line_highlight (w, x, mode_line_p)
6332 struct window *w;
6333 int x, mode_line_p;
6334{
6335 struct frame *f = XFRAME (w->frame);
6336 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6337 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6338 struct glyph_row *row;
6339
6340 if (mode_line_p)
6341 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6342 else
045dee35 6343 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
06a2c219
GM
6344
6345 if (row->enabled_p)
6346 {
6347 struct glyph *glyph, *end;
6348 Lisp_Object help, map;
6349 int x0;
6350
6351 /* Find the glyph under X. */
6352 glyph = row->glyphs[TEXT_AREA];
6353 end = glyph + row->used[TEXT_AREA];
6354 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
110859fc 6355 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
06a2c219
GM
6356 while (glyph < end
6357 && x >= x0 + glyph->pixel_width)
6358 {
6359 x0 += glyph->pixel_width;
6360 ++glyph;
6361 }
6362
6363 if (glyph < end
6364 && STRINGP (glyph->object)
6365 && XSTRING (glyph->object)->intervals
6366 && glyph->charpos >= 0
6367 && glyph->charpos < XSTRING (glyph->object)->size)
6368 {
6369 /* If we're on a string with `help-echo' text property,
6370 arrange for the help to be displayed. This is done by
6371 setting the global variable help_echo to the help string. */
6372 help = Fget_text_property (make_number (glyph->charpos),
6373 Qhelp_echo, glyph->object);
b7e80413 6374 if (!NILP (help))
be010514
GM
6375 {
6376 help_echo = help;
7cea38bc 6377 XSETWINDOW (help_echo_window, w);
be010514
GM
6378 help_echo_object = glyph->object;
6379 help_echo_pos = glyph->charpos;
6380 }
06a2c219
GM
6381
6382 /* Change the mouse pointer according to what is under X/Y. */
6383 map = Fget_text_property (make_number (glyph->charpos),
6384 Qlocal_map, glyph->object);
6385 if (!NILP (Fkeymapp (map)))
6386 cursor = f->output_data.x->nontext_cursor;
be010514
GM
6387 else
6388 {
6389 map = Fget_text_property (make_number (glyph->charpos),
6390 Qkeymap, glyph->object);
6391 if (!NILP (Fkeymapp (map)))
6392 cursor = f->output_data.x->nontext_cursor;
6393 }
06a2c219
GM
6394 }
6395 }
6396
6397 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6398}
6399
6400
6401/* Take proper action when the mouse has moved to position X, Y on
6402 frame F as regards highlighting characters that have mouse-face
6403 properties. Also de-highlighting chars where the mouse was before.
27f338af 6404 X and Y can be negative or out of range. */
b8009dd1
RS
6405
6406static void
6407note_mouse_highlight (f, x, y)
06a2c219 6408 struct frame *f;
c32cdd9a 6409 int x, y;
b8009dd1 6410{
06a2c219
GM
6411 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6412 int portion;
b8009dd1
RS
6413 Lisp_Object window;
6414 struct window *w;
6415
06a2c219
GM
6416 /* When a menu is active, don't highlight because this looks odd. */
6417#ifdef USE_X_TOOLKIT
6418 if (popup_activated ())
6419 return;
6420#endif
6421
04fff9c0
GM
6422 if (disable_mouse_highlight
6423 || !f->glyphs_initialized_p)
bf1c0ba1
RS
6424 return;
6425
06a2c219
GM
6426 dpyinfo->mouse_face_mouse_x = x;
6427 dpyinfo->mouse_face_mouse_y = y;
6428 dpyinfo->mouse_face_mouse_frame = f;
b8009dd1 6429
06a2c219 6430 if (dpyinfo->mouse_face_defer)
b8009dd1
RS
6431 return;
6432
514e4681
RS
6433 if (gc_in_progress)
6434 {
06a2c219 6435 dpyinfo->mouse_face_deferred_gc = 1;
514e4681
RS
6436 return;
6437 }
6438
b8009dd1 6439 /* Which window is that in? */
06a2c219 6440 window = window_from_coordinates (f, x, y, &portion, 1);
b8009dd1
RS
6441
6442 /* If we were displaying active text in another window, clear that. */
06a2c219
GM
6443 if (! EQ (window, dpyinfo->mouse_face_window))
6444 clear_mouse_face (dpyinfo);
6445
6446 /* Not on a window -> return. */
6447 if (!WINDOWP (window))
6448 return;
6449
6450 /* Convert to window-relative pixel coordinates. */
6451 w = XWINDOW (window);
6452 frame_to_window_pixel_xy (w, &x, &y);
6453
9ea173e8 6454 /* Handle tool-bar window differently since it doesn't display a
06a2c219 6455 buffer. */
9ea173e8 6456 if (EQ (window, f->tool_bar_window))
06a2c219 6457 {
9ea173e8 6458 note_tool_bar_highlight (f, x, y);
06a2c219
GM
6459 return;
6460 }
6461
6462 if (portion == 1 || portion == 3)
6463 {
6464 /* Mouse is on the mode or top line. */
6465 note_mode_line_highlight (w, x, portion == 1);
6466 return;
6467 }
6468 else
6469 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6470 f->output_data.x->text_cursor);
b8009dd1 6471
0cdd0c9f
RS
6472 /* Are we in a window whose display is up to date?
6473 And verify the buffer's text has not changed. */
06a2c219
GM
6474 if (/* Within text portion of the window. */
6475 portion == 0
0cdd0c9f 6476 && EQ (w->window_end_valid, w->buffer)
26459b28
KH
6477 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6478 && (XFASTINT (w->last_overlay_modified)
6479 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
b8009dd1 6480 {
06a2c219
GM
6481 int hpos, vpos, pos, i, area;
6482 struct glyph *glyph;
b8009dd1 6483
06a2c219
GM
6484 /* Find the glyph under X/Y. */
6485 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6486
6487 /* Clear mouse face if X/Y not over text. */
6488 if (glyph == NULL
6489 || area != TEXT_AREA
6490 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
b8009dd1 6491 {
06a2c219
GM
6492 clear_mouse_face (dpyinfo);
6493 return;
6494 }
6495
6496 pos = glyph->charpos;
6497 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6498
6499 /* Check for mouse-face and help-echo. */
6500 {
6501 Lisp_Object mouse_face, overlay, position;
6502 Lisp_Object *overlay_vec;
6503 int len, noverlays;
6504 struct buffer *obuf;
6505 int obegv, ozv;
6506
6507 /* If we get an out-of-range value, return now; avoid an error. */
6508 if (pos > BUF_Z (XBUFFER (w->buffer)))
6509 return;
6510
6511 /* Make the window's buffer temporarily current for
6512 overlays_at and compute_char_face. */
6513 obuf = current_buffer;
6514 current_buffer = XBUFFER (w->buffer);
6515 obegv = BEGV;
6516 ozv = ZV;
6517 BEGV = BEG;
6518 ZV = Z;
6519
6520 /* Is this char mouse-active or does it have help-echo? */
6521 XSETINT (position, pos);
6522
6523 /* Put all the overlays we want in a vector in overlay_vec.
6524 Store the length in len. If there are more than 10, make
6525 enough space for all, and try again. */
6526 len = 10;
6527 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
bc592036 6528 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
06a2c219
GM
6529 if (noverlays > len)
6530 {
6531 len = noverlays;
6532 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
bc592036 6533 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
06a2c219 6534 }
f8349001
GM
6535
6536 /* Sort overlays into increasing priority order. */
06a2c219
GM
6537 noverlays = sort_overlays (overlay_vec, noverlays, w);
6538
6539 /* Check mouse-face highlighting. */
6540 if (! (EQ (window, dpyinfo->mouse_face_window)
6541 && vpos >= dpyinfo->mouse_face_beg_row
6542 && vpos <= dpyinfo->mouse_face_end_row
6543 && (vpos > dpyinfo->mouse_face_beg_row
6544 || hpos >= dpyinfo->mouse_face_beg_col)
6545 && (vpos < dpyinfo->mouse_face_end_row
6546 || hpos < dpyinfo->mouse_face_end_col
6547 || dpyinfo->mouse_face_past_end)))
6548 {
6549 /* Clear the display of the old active region, if any. */
6550 clear_mouse_face (dpyinfo);
6551
6552 /* Find the highest priority overlay that has a mouse-face prop. */
6553 overlay = Qnil;
f8349001 6554 for (i = noverlays - 1; i >= 0; --i)
06a2c219
GM
6555 {
6556 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6557 if (!NILP (mouse_face))
6558 {
6559 overlay = overlay_vec[i];
6560 break;
6561 }
6562 }
6563
6564 /* If no overlay applies, get a text property. */
6565 if (NILP (overlay))
6566 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6567
6568 /* Handle the overlay case. */
6569 if (! NILP (overlay))
6570 {
6571 /* Find the range of text around this char that
6572 should be active. */
6573 Lisp_Object before, after;
6574 int ignore;
6575
6576 before = Foverlay_start (overlay);
6577 after = Foverlay_end (overlay);
6578 /* Record this as the current active region. */
6579 fast_find_position (w, XFASTINT (before),
6580 &dpyinfo->mouse_face_beg_col,
6581 &dpyinfo->mouse_face_beg_row,
6582 &dpyinfo->mouse_face_beg_x,
6583 &dpyinfo->mouse_face_beg_y);
6584 dpyinfo->mouse_face_past_end
6585 = !fast_find_position (w, XFASTINT (after),
6586 &dpyinfo->mouse_face_end_col,
6587 &dpyinfo->mouse_face_end_row,
6588 &dpyinfo->mouse_face_end_x,
6589 &dpyinfo->mouse_face_end_y);
6590 dpyinfo->mouse_face_window = window;
6591 dpyinfo->mouse_face_face_id
6592 = face_at_buffer_position (w, pos, 0, 0,
6593 &ignore, pos + 1, 1);
6594
6595 /* Display it as active. */
6596 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6597 }
6598 /* Handle the text property case. */
6599 else if (! NILP (mouse_face))
6600 {
6601 /* Find the range of text around this char that
6602 should be active. */
6603 Lisp_Object before, after, beginning, end;
6604 int ignore;
6605
6606 beginning = Fmarker_position (w->start);
6607 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6608 - XFASTINT (w->window_end_pos)));
6609 before
6610 = Fprevious_single_property_change (make_number (pos + 1),
6611 Qmouse_face,
6612 w->buffer, beginning);
6613 after
6614 = Fnext_single_property_change (position, Qmouse_face,
6615 w->buffer, end);
6616 /* Record this as the current active region. */
6617 fast_find_position (w, XFASTINT (before),
6618 &dpyinfo->mouse_face_beg_col,
6619 &dpyinfo->mouse_face_beg_row,
6620 &dpyinfo->mouse_face_beg_x,
6621 &dpyinfo->mouse_face_beg_y);
6622 dpyinfo->mouse_face_past_end
6623 = !fast_find_position (w, XFASTINT (after),
6624 &dpyinfo->mouse_face_end_col,
6625 &dpyinfo->mouse_face_end_row,
6626 &dpyinfo->mouse_face_end_x,
6627 &dpyinfo->mouse_face_end_y);
6628 dpyinfo->mouse_face_window = window;
6629 dpyinfo->mouse_face_face_id
6630 = face_at_buffer_position (w, pos, 0, 0,
6631 &ignore, pos + 1, 1);
6632
6633 /* Display it as active. */
6634 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6635 }
6636 }
6637
6638 /* Look for a `help-echo' property. */
6639 {
743934db 6640 Lisp_Object help, overlay;
06a2c219
GM
6641
6642 /* Check overlays first. */
6643 help = Qnil;
f8349001 6644 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
743934db
GM
6645 {
6646 overlay = overlay_vec[i];
6647 help = Foverlay_get (overlay, Qhelp_echo);
6648 }
be010514
GM
6649
6650 if (!NILP (help))
6651 {
6652 help_echo = help;
7cea38bc 6653 help_echo_window = window;
743934db 6654 help_echo_object = overlay;
be010514
GM
6655 help_echo_pos = pos;
6656 }
6657 else
6658 {
6659 /* Try text properties. */
6660 if ((STRINGP (glyph->object)
06a2c219
GM
6661 && glyph->charpos >= 0
6662 && glyph->charpos < XSTRING (glyph->object)->size)
6663 || (BUFFERP (glyph->object)
6664 && glyph->charpos >= BEGV
be010514
GM
6665 && glyph->charpos < ZV))
6666 help = Fget_text_property (make_number (glyph->charpos),
6667 Qhelp_echo, glyph->object);
06a2c219 6668
be010514
GM
6669 if (!NILP (help))
6670 {
6671 help_echo = help;
7cea38bc 6672 help_echo_window = window;
be010514
GM
6673 help_echo_object = glyph->object;
6674 help_echo_pos = glyph->charpos;
6675 }
6676 }
06a2c219
GM
6677 }
6678
6679 BEGV = obegv;
6680 ZV = ozv;
6681 current_buffer = obuf;
6682 }
6683 }
6684}
6685
6686static void
6687redo_mouse_highlight ()
6688{
6689 if (!NILP (last_mouse_motion_frame)
6690 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6691 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6692 last_mouse_motion_event.x,
6693 last_mouse_motion_event.y);
6694}
6695
6696
6697\f
6698/***********************************************************************
9ea173e8 6699 Tool-bars
06a2c219
GM
6700 ***********************************************************************/
6701
9ea173e8
GM
6702static int x_tool_bar_item P_ ((struct frame *, int, int,
6703 struct glyph **, int *, int *, int *));
06a2c219 6704
9ea173e8 6705/* Tool-bar item index of the item on which a mouse button was pressed
06a2c219
GM
6706 or -1. */
6707
9ea173e8 6708static int last_tool_bar_item;
06a2c219
GM
6709
6710
9ea173e8
GM
6711/* Get information about the tool-bar item at position X/Y on frame F.
6712 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6713 the current matrix of the tool-bar window of F, or NULL if not
6714 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
6715 item in F->current_tool_bar_items. Value is
06a2c219 6716
9ea173e8 6717 -1 if X/Y is not on a tool-bar item
06a2c219
GM
6718 0 if X/Y is on the same item that was highlighted before.
6719 1 otherwise. */
6720
6721static int
9ea173e8 6722x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
06a2c219
GM
6723 struct frame *f;
6724 int x, y;
6725 struct glyph **glyph;
6726 int *hpos, *vpos, *prop_idx;
6727{
6728 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6729 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6730 int area;
6731
6732 /* Find the glyph under X/Y. */
6733 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6734 if (*glyph == NULL)
6735 return -1;
6736
9ea173e8
GM
6737 /* Get the start of this tool-bar item's properties in
6738 f->current_tool_bar_items. */
6739 if (!tool_bar_item_info (f, *glyph, prop_idx))
06a2c219
GM
6740 return -1;
6741
6742 /* Is mouse on the highlighted item? */
9ea173e8 6743 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
06a2c219
GM
6744 && *vpos >= dpyinfo->mouse_face_beg_row
6745 && *vpos <= dpyinfo->mouse_face_end_row
6746 && (*vpos > dpyinfo->mouse_face_beg_row
6747 || *hpos >= dpyinfo->mouse_face_beg_col)
6748 && (*vpos < dpyinfo->mouse_face_end_row
6749 || *hpos < dpyinfo->mouse_face_end_col
6750 || dpyinfo->mouse_face_past_end))
6751 return 0;
6752
6753 return 1;
6754}
6755
6756
9ea173e8 6757/* Handle mouse button event on the tool-bar of frame F, at
06a2c219
GM
6758 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6759 or ButtonRelase. */
6760
6761static void
9ea173e8 6762x_handle_tool_bar_click (f, button_event)
06a2c219
GM
6763 struct frame *f;
6764 XButtonEvent *button_event;
6765{
6766 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6767 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6768 int hpos, vpos, prop_idx;
6769 struct glyph *glyph;
6770 Lisp_Object enabled_p;
6771 int x = button_event->x;
6772 int y = button_event->y;
6773
9ea173e8 6774 /* If not on the highlighted tool-bar item, return. */
06a2c219 6775 frame_to_window_pixel_xy (w, &x, &y);
9ea173e8 6776 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
06a2c219
GM
6777 return;
6778
6779 /* If item is disabled, do nothing. */
9ea173e8
GM
6780 enabled_p = (XVECTOR (f->current_tool_bar_items)
6781 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
06a2c219
GM
6782 if (NILP (enabled_p))
6783 return;
6784
6785 if (button_event->type == ButtonPress)
6786 {
6787 /* Show item in pressed state. */
6788 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6789 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9ea173e8 6790 last_tool_bar_item = prop_idx;
06a2c219
GM
6791 }
6792 else
6793 {
6794 Lisp_Object key, frame;
6795 struct input_event event;
6796
6797 /* Show item in released state. */
6798 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6799 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6800
9ea173e8
GM
6801 key = (XVECTOR (f->current_tool_bar_items)
6802 ->contents[prop_idx + TOOL_BAR_ITEM_KEY]);
06a2c219
GM
6803
6804 XSETFRAME (frame, f);
9ea173e8 6805 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
6806 event.frame_or_window = frame;
6807 event.arg = frame;
06a2c219
GM
6808 kbd_buffer_store_event (&event);
6809
9ea173e8 6810 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
6811 event.frame_or_window = frame;
6812 event.arg = key;
06a2c219
GM
6813 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6814 button_event->state);
6815 kbd_buffer_store_event (&event);
9ea173e8 6816 last_tool_bar_item = -1;
06a2c219
GM
6817 }
6818}
6819
6820
9ea173e8
GM
6821/* Possibly highlight a tool-bar item on frame F when mouse moves to
6822 tool-bar window-relative coordinates X/Y. Called from
06a2c219
GM
6823 note_mouse_highlight. */
6824
6825static void
9ea173e8 6826note_tool_bar_highlight (f, x, y)
06a2c219
GM
6827 struct frame *f;
6828 int x, y;
6829{
9ea173e8 6830 Lisp_Object window = f->tool_bar_window;
06a2c219
GM
6831 struct window *w = XWINDOW (window);
6832 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6833 int hpos, vpos;
6834 struct glyph *glyph;
6835 struct glyph_row *row;
5c187dee 6836 int i;
06a2c219
GM
6837 Lisp_Object enabled_p;
6838 int prop_idx;
6839 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
5c187dee 6840 int mouse_down_p, rc;
06a2c219
GM
6841
6842 /* Function note_mouse_highlight is called with negative x(y
6843 values when mouse moves outside of the frame. */
6844 if (x <= 0 || y <= 0)
6845 {
6846 clear_mouse_face (dpyinfo);
6847 return;
6848 }
6849
9ea173e8 6850 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
06a2c219
GM
6851 if (rc < 0)
6852 {
9ea173e8 6853 /* Not on tool-bar item. */
06a2c219
GM
6854 clear_mouse_face (dpyinfo);
6855 return;
6856 }
6857 else if (rc == 0)
9ea173e8 6858 /* On same tool-bar item as before. */
06a2c219 6859 goto set_help_echo;
b8009dd1 6860
06a2c219
GM
6861 clear_mouse_face (dpyinfo);
6862
9ea173e8 6863 /* Mouse is down, but on different tool-bar item? */
06a2c219
GM
6864 mouse_down_p = (dpyinfo->grabbed
6865 && f == last_mouse_frame
6866 && FRAME_LIVE_P (f));
6867 if (mouse_down_p
9ea173e8 6868 && last_tool_bar_item != prop_idx)
06a2c219
GM
6869 return;
6870
6871 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
6872 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
6873
9ea173e8
GM
6874 /* If tool-bar item is not enabled, don't highlight it. */
6875 enabled_p = (XVECTOR (f->current_tool_bar_items)
6876 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
06a2c219
GM
6877 if (!NILP (enabled_p))
6878 {
6879 /* Compute the x-position of the glyph. In front and past the
6880 image is a space. We include this is the highlighted area. */
6881 row = MATRIX_ROW (w->current_matrix, vpos);
6882 for (i = x = 0; i < hpos; ++i)
6883 x += row->glyphs[TEXT_AREA][i].pixel_width;
6884
6885 /* Record this as the current active region. */
6886 dpyinfo->mouse_face_beg_col = hpos;
6887 dpyinfo->mouse_face_beg_row = vpos;
6888 dpyinfo->mouse_face_beg_x = x;
6889 dpyinfo->mouse_face_beg_y = row->y;
6890 dpyinfo->mouse_face_past_end = 0;
6891
6892 dpyinfo->mouse_face_end_col = hpos + 1;
6893 dpyinfo->mouse_face_end_row = vpos;
6894 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
6895 dpyinfo->mouse_face_end_y = row->y;
6896 dpyinfo->mouse_face_window = window;
9ea173e8 6897 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
06a2c219
GM
6898
6899 /* Display it as active. */
6900 show_mouse_face (dpyinfo, draw);
6901 dpyinfo->mouse_face_image_state = draw;
b8009dd1 6902 }
06a2c219
GM
6903
6904 set_help_echo:
6905
9ea173e8 6906 /* Set help_echo to a help string.to display for this tool-bar item.
06a2c219 6907 XTread_socket does the rest. */
7cea38bc 6908 help_echo_object = help_echo_window = Qnil;
be010514 6909 help_echo_pos = -1;
9ea173e8
GM
6910 help_echo = (XVECTOR (f->current_tool_bar_items)
6911 ->contents[prop_idx + TOOL_BAR_ITEM_HELP]);
b7e80413 6912 if (NILP (help_echo))
9ea173e8
GM
6913 help_echo = (XVECTOR (f->current_tool_bar_items)
6914 ->contents[prop_idx + TOOL_BAR_ITEM_CAPTION]);
b8009dd1 6915}
4d73d038 6916
06a2c219
GM
6917
6918\f
6919/* Find the glyph matrix position of buffer position POS in window W.
6920 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
6921 current glyphs must be up to date. If POS is above window start
6922 return (0, 0, 0, 0). If POS is after end of W, return end of
6923 last line in W. */
b8009dd1
RS
6924
6925static int
06a2c219
GM
6926fast_find_position (w, pos, hpos, vpos, x, y)
6927 struct window *w;
b8009dd1 6928 int pos;
06a2c219 6929 int *hpos, *vpos, *x, *y;
b8009dd1 6930{
b8009dd1 6931 int i;
bf1c0ba1 6932 int lastcol;
06a2c219
GM
6933 int maybe_next_line_p = 0;
6934 int line_start_position;
6935 int yb = window_text_bottom_y (w);
6936 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
6937 struct glyph_row *best_row = row;
6938 int row_vpos = 0, best_row_vpos = 0;
6939 int current_x;
6940
6941 while (row->y < yb)
b8009dd1 6942 {
06a2c219
GM
6943 if (row->used[TEXT_AREA])
6944 line_start_position = row->glyphs[TEXT_AREA]->charpos;
6945 else
6946 line_start_position = 0;
6947
6948 if (line_start_position > pos)
b8009dd1 6949 break;
77b68646
RS
6950 /* If the position sought is the end of the buffer,
6951 don't include the blank lines at the bottom of the window. */
06a2c219
GM
6952 else if (line_start_position == pos
6953 && pos == BUF_ZV (XBUFFER (w->buffer)))
77b68646 6954 {
06a2c219 6955 maybe_next_line_p = 1;
77b68646
RS
6956 break;
6957 }
06a2c219
GM
6958 else if (line_start_position > 0)
6959 {
6960 best_row = row;
6961 best_row_vpos = row_vpos;
6962 }
4b0bb6f3
GM
6963
6964 if (row->y + row->height >= yb)
6965 break;
06a2c219
GM
6966
6967 ++row;
6968 ++row_vpos;
b8009dd1 6969 }
06a2c219
GM
6970
6971 /* Find the right column within BEST_ROW. */
6972 lastcol = 0;
6973 current_x = best_row->x;
6974 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
bf1c0ba1 6975 {
06a2c219
GM
6976 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
6977 int charpos;
6978
6979 charpos = glyph->charpos;
6980 if (charpos == pos)
bf1c0ba1 6981 {
06a2c219
GM
6982 *hpos = i;
6983 *vpos = best_row_vpos;
6984 *x = current_x;
6985 *y = best_row->y;
bf1c0ba1
RS
6986 return 1;
6987 }
06a2c219 6988 else if (charpos > pos)
4d73d038 6989 break;
06a2c219
GM
6990 else if (charpos > 0)
6991 lastcol = i;
6992
6993 current_x += glyph->pixel_width;
bf1c0ba1 6994 }
b8009dd1 6995
77b68646
RS
6996 /* If we're looking for the end of the buffer,
6997 and we didn't find it in the line we scanned,
6998 use the start of the following line. */
06a2c219 6999 if (maybe_next_line_p)
77b68646 7000 {
06a2c219
GM
7001 ++best_row;
7002 ++best_row_vpos;
7003 lastcol = 0;
7004 current_x = best_row->x;
77b68646
RS
7005 }
7006
06a2c219
GM
7007 *vpos = best_row_vpos;
7008 *hpos = lastcol + 1;
7009 *x = current_x;
7010 *y = best_row->y;
b8009dd1
RS
7011 return 0;
7012}
7013
06a2c219 7014
b8009dd1
RS
7015/* Display the active region described by mouse_face_*
7016 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7017
7018static void
06a2c219 7019show_mouse_face (dpyinfo, draw)
7a13e894 7020 struct x_display_info *dpyinfo;
06a2c219 7021 enum draw_glyphs_face draw;
b8009dd1 7022{
7a13e894 7023 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
06a2c219 7024 struct frame *f = XFRAME (WINDOW_FRAME (w));
b8009dd1 7025 int i;
06a2c219
GM
7026 int cursor_off_p = 0;
7027 struct cursor_pos saved_cursor;
7028
7029 saved_cursor = output_cursor;
7030
7031 /* If window is in the process of being destroyed, don't bother
7032 to do anything. */
7033 if (w->current_matrix == NULL)
7034 goto set_x_cursor;
7035
7036 /* Recognize when we are called to operate on rows that don't exist
7037 anymore. This can happen when a window is split. */
7038 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
7039 goto set_x_cursor;
7040
7041 set_output_cursor (&w->phys_cursor);
7042
7043 /* Note that mouse_face_beg_row etc. are window relative. */
7044 for (i = dpyinfo->mouse_face_beg_row;
7045 i <= dpyinfo->mouse_face_end_row;
7046 i++)
7047 {
7048 int start_hpos, end_hpos, start_x;
7049 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
7050
7051 /* Don't do anything if row doesn't have valid contents. */
7052 if (!row->enabled_p)
7053 continue;
7054
7055 /* For all but the first row, the highlight starts at column 0. */
7056 if (i == dpyinfo->mouse_face_beg_row)
7057 {
7058 start_hpos = dpyinfo->mouse_face_beg_col;
7059 start_x = dpyinfo->mouse_face_beg_x;
7060 }
7061 else
7062 {
7063 start_hpos = 0;
7064 start_x = 0;
7065 }
7066
7067 if (i == dpyinfo->mouse_face_end_row)
7068 end_hpos = dpyinfo->mouse_face_end_col;
7069 else
7070 end_hpos = row->used[TEXT_AREA];
7071
7072 /* If the cursor's in the text we are about to rewrite, turn the
7073 cursor off. */
7074 if (!w->pseudo_window_p
7075 && i == output_cursor.vpos
7076 && output_cursor.hpos >= start_hpos - 1
7077 && output_cursor.hpos <= end_hpos)
514e4681 7078 {
06a2c219
GM
7079 x_update_window_cursor (w, 0);
7080 cursor_off_p = 1;
514e4681 7081 }
b8009dd1 7082
06a2c219 7083 if (end_hpos > start_hpos)
64f26cf5
GM
7084 {
7085 row->mouse_face_p = draw == DRAW_MOUSE_FACE;
7086 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7087 start_hpos, end_hpos, draw, NULL, NULL, 0);
7088 }
b8009dd1
RS
7089 }
7090
514e4681 7091 /* If we turned the cursor off, turn it back on. */
06a2c219
GM
7092 if (cursor_off_p)
7093 x_display_cursor (w, 1,
7094 output_cursor.hpos, output_cursor.vpos,
7095 output_cursor.x, output_cursor.y);
2729a2b5 7096
06a2c219 7097 output_cursor = saved_cursor;
fb3b7de5 7098
06a2c219
GM
7099 set_x_cursor:
7100
7101 /* Change the mouse cursor. */
7102 if (draw == DRAW_NORMAL_TEXT)
7103 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7104 f->output_data.x->text_cursor);
7105 else if (draw == DRAW_MOUSE_FACE)
334208b7 7106 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 7107 f->output_data.x->cross_cursor);
27ead1d5 7108 else
334208b7 7109 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
06a2c219 7110 f->output_data.x->nontext_cursor);
b8009dd1
RS
7111}
7112
7113/* Clear out the mouse-highlighted active region.
06a2c219 7114 Redraw it un-highlighted first. */
b8009dd1 7115
06a2c219 7116void
7a13e894
RS
7117clear_mouse_face (dpyinfo)
7118 struct x_display_info *dpyinfo;
b8009dd1 7119{
06a2c219
GM
7120 if (tip_frame)
7121 return;
7122
7a13e894 7123 if (! NILP (dpyinfo->mouse_face_window))
06a2c219 7124 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
b8009dd1 7125
7a13e894
RS
7126 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7127 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7128 dpyinfo->mouse_face_window = Qnil;
b8009dd1 7129}
e687d06e 7130
71b8321e
GM
7131
7132/* Clear any mouse-face on window W. This function is part of the
7133 redisplay interface, and is called from try_window_id and similar
7134 functions to ensure the mouse-highlight is off. */
7135
7136static void
7137x_clear_mouse_face (w)
7138 struct window *w;
7139{
7140 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7141 Lisp_Object window;
7142
7143 XSETWINDOW (window, w);
7144 if (EQ (window, dpyinfo->mouse_face_window))
7145 clear_mouse_face (dpyinfo);
7146}
7147
7148
e687d06e
RS
7149/* Just discard the mouse face information for frame F, if any.
7150 This is used when the size of F is changed. */
7151
dfcf069d 7152void
e687d06e
RS
7153cancel_mouse_face (f)
7154 FRAME_PTR f;
7155{
7156 Lisp_Object window;
7157 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7158
7159 window = dpyinfo->mouse_face_window;
7160 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7161 {
7162 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7163 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7164 dpyinfo->mouse_face_window = Qnil;
7165 }
7166}
b8009dd1 7167\f
ab648270
JB
7168static struct scroll_bar *x_window_to_scroll_bar ();
7169static void x_scroll_bar_report_motion ();
12ba150f 7170
90e65f07 7171/* Return the current position of the mouse.
2d7fc7e8 7172 *fp should be a frame which indicates which display to ask about.
90e65f07 7173
2d7fc7e8 7174 If the mouse movement started in a scroll bar, set *fp, *bar_window,
ab648270 7175 and *part to the frame, window, and scroll bar part that the mouse
12ba150f 7176 is over. Set *x and *y to the portion and whole of the mouse's
ab648270 7177 position on the scroll bar.
12ba150f 7178
2d7fc7e8 7179 If the mouse movement started elsewhere, set *fp to the frame the
12ba150f
JB
7180 mouse is on, *bar_window to nil, and *x and *y to the character cell
7181 the mouse is over.
7182
06a2c219 7183 Set *time to the server time-stamp for the time at which the mouse
12ba150f
JB
7184 was at this position.
7185
a135645a
RS
7186 Don't store anything if we don't have a valid set of values to report.
7187
90e65f07 7188 This clears the mouse_moved flag, so we can wait for the next mouse
f5bb65ec 7189 movement. */
90e65f07
JB
7190
7191static void
1cf412ec 7192XTmouse_position (fp, insist, bar_window, part, x, y, time)
334208b7 7193 FRAME_PTR *fp;
1cf412ec 7194 int insist;
12ba150f 7195 Lisp_Object *bar_window;
ab648270 7196 enum scroll_bar_part *part;
90e65f07 7197 Lisp_Object *x, *y;
e5d77022 7198 unsigned long *time;
90e65f07 7199{
a135645a
RS
7200 FRAME_PTR f1;
7201
90e65f07
JB
7202 BLOCK_INPUT;
7203
8bcee03e 7204 if (! NILP (last_mouse_scroll_bar) && insist == 0)
334208b7 7205 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
90e65f07
JB
7206 else
7207 {
12ba150f
JB
7208 Window root;
7209 int root_x, root_y;
90e65f07 7210
12ba150f
JB
7211 Window dummy_window;
7212 int dummy;
7213
39d8bb4d
KH
7214 Lisp_Object frame, tail;
7215
7216 /* Clear the mouse-moved flag for every frame on this display. */
7217 FOR_EACH_FRAME (tail, frame)
7218 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7219 XFRAME (frame)->mouse_moved = 0;
7220
ab648270 7221 last_mouse_scroll_bar = Qnil;
12ba150f
JB
7222
7223 /* Figure out which root window we're on. */
334208b7
RS
7224 XQueryPointer (FRAME_X_DISPLAY (*fp),
7225 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
12ba150f
JB
7226
7227 /* The root window which contains the pointer. */
7228 &root,
7229
7230 /* Trash which we can't trust if the pointer is on
7231 a different screen. */
7232 &dummy_window,
7233
7234 /* The position on that root window. */
58769bee 7235 &root_x, &root_y,
12ba150f
JB
7236
7237 /* More trash we can't trust. */
7238 &dummy, &dummy,
7239
7240 /* Modifier keys and pointer buttons, about which
7241 we don't care. */
7242 (unsigned int *) &dummy);
7243
7244 /* Now we have a position on the root; find the innermost window
7245 containing the pointer. */
7246 {
7247 Window win, child;
7248 int win_x, win_y;
06a2c219 7249 int parent_x = 0, parent_y = 0;
e99db5a1 7250 int count;
12ba150f
JB
7251
7252 win = root;
69388238 7253
2d7fc7e8
RS
7254 /* XTranslateCoordinates can get errors if the window
7255 structure is changing at the same time this function
7256 is running. So at least we must not crash from them. */
7257
e99db5a1 7258 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
2d7fc7e8 7259
334208b7 7260 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
23faf38f 7261 && FRAME_LIVE_P (last_mouse_frame))
12ba150f 7262 {
69388238
RS
7263 /* If mouse was grabbed on a frame, give coords for that frame
7264 even if the mouse is now outside it. */
334208b7 7265 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
69388238 7266
12ba150f 7267 /* From-window, to-window. */
69388238 7268 root, FRAME_X_WINDOW (last_mouse_frame),
12ba150f
JB
7269
7270 /* From-position, to-position. */
7271 root_x, root_y, &win_x, &win_y,
7272
7273 /* Child of win. */
7274 &child);
69388238
RS
7275 f1 = last_mouse_frame;
7276 }
7277 else
7278 {
7279 while (1)
7280 {
334208b7 7281 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
12ba150f 7282
69388238
RS
7283 /* From-window, to-window. */
7284 root, win,
12ba150f 7285
69388238
RS
7286 /* From-position, to-position. */
7287 root_x, root_y, &win_x, &win_y,
7288
7289 /* Child of win. */
7290 &child);
7291
9af3143a 7292 if (child == None || child == win)
69388238
RS
7293 break;
7294
7295 win = child;
7296 parent_x = win_x;
7297 parent_y = win_y;
7298 }
12ba150f 7299
69388238
RS
7300 /* Now we know that:
7301 win is the innermost window containing the pointer
7302 (XTC says it has no child containing the pointer),
7303 win_x and win_y are the pointer's position in it
7304 (XTC did this the last time through), and
7305 parent_x and parent_y are the pointer's position in win's parent.
7306 (They are what win_x and win_y were when win was child.
7307 If win is the root window, it has no parent, and
7308 parent_{x,y} are invalid, but that's okay, because we'll
7309 never use them in that case.) */
7310
7311 /* Is win one of our frames? */
19126e11 7312 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
69388238 7313 }
58769bee 7314
2d7fc7e8
RS
7315 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7316 f1 = 0;
7317
e99db5a1 7318 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
2d7fc7e8 7319
ab648270 7320 /* If not, is it one of our scroll bars? */
a135645a 7321 if (! f1)
12ba150f 7322 {
ab648270 7323 struct scroll_bar *bar = x_window_to_scroll_bar (win);
12ba150f
JB
7324
7325 if (bar)
7326 {
a135645a 7327 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
7328 win_x = parent_x;
7329 win_y = parent_y;
7330 }
7331 }
90e65f07 7332
8bcee03e 7333 if (f1 == 0 && insist > 0)
b86bd3dd 7334 f1 = SELECTED_FRAME ();
1cf412ec 7335
a135645a 7336 if (f1)
12ba150f 7337 {
06a2c219
GM
7338 /* Ok, we found a frame. Store all the values.
7339 last_mouse_glyph is a rectangle used to reduce the
7340 generation of mouse events. To not miss any motion
7341 events, we must divide the frame into rectangles of the
7342 size of the smallest character that could be displayed
7343 on it, i.e. into the same rectangles that matrices on
7344 the frame are divided into. */
7345
7346#if OLD_REDISPLAY_CODE
2b5c9e71 7347 int ignore1, ignore2;
2b5c9e71 7348 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
334208b7 7349 &last_mouse_glyph,
1cf412ec
RS
7350 FRAME_X_DISPLAY_INFO (f1)->grabbed
7351 || insist);
06a2c219
GM
7352#else
7353 {
7354 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7355 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7356 int x = win_x;
7357 int y = win_y;
7358
7359 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7360 round down even for negative values. */
7361 if (x < 0)
7362 x -= width - 1;
7363 if (y < 0)
7364 y -= height - 1;
7365
7366 last_mouse_glyph.width = width;
7367 last_mouse_glyph.height = height;
7368 last_mouse_glyph.x = (x + width - 1) / width * width;
7369 last_mouse_glyph.y = (y + height - 1) / height * height;
7370 }
7371#endif
12ba150f
JB
7372
7373 *bar_window = Qnil;
7374 *part = 0;
334208b7 7375 *fp = f1;
e0c1aef2
KH
7376 XSETINT (*x, win_x);
7377 XSETINT (*y, win_y);
12ba150f
JB
7378 *time = last_mouse_movement_time;
7379 }
7380 }
7381 }
90e65f07
JB
7382
7383 UNBLOCK_INPUT;
7384}
f451eb13 7385
06a2c219 7386
06a2c219 7387#ifdef USE_X_TOOLKIT
bffcfca9
GM
7388
7389/* Atimer callback function for TIMER. Called every 0.1s to process
7390 Xt timeouts, if needed. We must avoid calling XtAppPending as
7391 much as possible because that function does an implicit XFlush
7392 that slows us down. */
7393
7394static void
7395x_process_timeouts (timer)
7396 struct atimer *timer;
7397{
7398 if (toolkit_scroll_bar_interaction || popup_activated_flag)
7399 {
7400 BLOCK_INPUT;
7401 while (XtAppPending (Xt_app_con) & XtIMTimer)
7402 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7403 UNBLOCK_INPUT;
7404 }
06a2c219
GM
7405}
7406
bffcfca9 7407#endif /* USE_X_TOOLKIT */
06a2c219
GM
7408
7409\f
7410/* Scroll bar support. */
7411
7412/* Given an X window ID, find the struct scroll_bar which manages it.
7413 This can be called in GC, so we have to make sure to strip off mark
7414 bits. */
bffcfca9 7415
06a2c219
GM
7416static struct scroll_bar *
7417x_window_to_scroll_bar (window_id)
7418 Window window_id;
7419{
7420 Lisp_Object tail;
7421
7422 for (tail = Vframe_list;
7423 XGCTYPE (tail) == Lisp_Cons;
8e713be6 7424 tail = XCDR (tail))
06a2c219
GM
7425 {
7426 Lisp_Object frame, bar, condemned;
7427
8e713be6 7428 frame = XCAR (tail);
06a2c219
GM
7429 /* All elements of Vframe_list should be frames. */
7430 if (! GC_FRAMEP (frame))
7431 abort ();
7432
7433 /* Scan this frame's scroll bar list for a scroll bar with the
7434 right window ID. */
7435 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7436 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7437 /* This trick allows us to search both the ordinary and
7438 condemned scroll bar lists with one loop. */
7439 ! GC_NILP (bar) || (bar = condemned,
7440 condemned = Qnil,
7441 ! GC_NILP (bar));
7442 bar = XSCROLL_BAR (bar)->next)
7443 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7444 return XSCROLL_BAR (bar);
7445 }
7446
7447 return 0;
7448}
7449
7450
7451\f
7452/************************************************************************
7453 Toolkit scroll bars
7454 ************************************************************************/
7455
7456#if USE_TOOLKIT_SCROLL_BARS
7457
7458static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7459static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7460static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7461 struct scroll_bar *));
7462static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7463 int, int, int));
7464
7465
7466/* Id of action hook installed for scroll bars. */
7467
7468static XtActionHookId action_hook_id;
7469
7470/* Lisp window being scrolled. Set when starting to interact with
7471 a toolkit scroll bar, reset to nil when ending the interaction. */
7472
7473static Lisp_Object window_being_scrolled;
7474
7475/* Last scroll bar part sent in xm_scroll_callback. */
7476
7477static int last_scroll_bar_part;
7478
ec18280f
SM
7479/* Whether this is an Xaw with arrow-scrollbars. This should imply
7480 that movements of 1/20 of the screen size are mapped to up/down. */
7481
7482static Boolean xaw3d_arrow_scroll;
7483
7484/* Whether the drag scrolling maintains the mouse at the top of the
7485 thumb. If not, resizing the thumb needs to be done more carefully
7486 to avoid jerkyness. */
7487
7488static Boolean xaw3d_pick_top;
7489
06a2c219
GM
7490
7491/* Action hook installed via XtAppAddActionHook when toolkit scroll
ec18280f 7492 bars are used.. The hook is responsible for detecting when
06a2c219
GM
7493 the user ends an interaction with the scroll bar, and generates
7494 a `end-scroll' scroll_bar_click' event if so. */
7495
7496static void
7497xt_action_hook (widget, client_data, action_name, event, params,
7498 num_params)
7499 Widget widget;
7500 XtPointer client_data;
7501 String action_name;
7502 XEvent *event;
7503 String *params;
7504 Cardinal *num_params;
7505{
7506 int scroll_bar_p;
7507 char *end_action;
7508
7509#ifdef USE_MOTIF
7510 scroll_bar_p = XmIsScrollBar (widget);
7511 end_action = "Release";
ec18280f 7512#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
7513 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7514 end_action = "EndScroll";
ec18280f 7515#endif /* USE_MOTIF */
06a2c219 7516
06a2c219
GM
7517 if (scroll_bar_p
7518 && strcmp (action_name, end_action) == 0
7519 && WINDOWP (window_being_scrolled))
7520 {
7521 struct window *w;
7522
7523 x_send_scroll_bar_event (window_being_scrolled,
7524 scroll_bar_end_scroll, 0, 0);
7525 w = XWINDOW (window_being_scrolled);
7526 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7527 window_being_scrolled = Qnil;
7528 last_scroll_bar_part = -1;
bffcfca9
GM
7529
7530 /* Xt timeouts no longer needed. */
7531 toolkit_scroll_bar_interaction = 0;
06a2c219
GM
7532 }
7533}
7534
7535
7536/* Send a client message with message type Xatom_Scrollbar for a
7537 scroll action to the frame of WINDOW. PART is a value identifying
7538 the part of the scroll bar that was clicked on. PORTION is the
7539 amount to scroll of a whole of WHOLE. */
7540
7541static void
7542x_send_scroll_bar_event (window, part, portion, whole)
7543 Lisp_Object window;
7544 int part, portion, whole;
7545{
7546 XEvent event;
7547 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
7548 struct frame *f = XFRAME (XWINDOW (window)->frame);
7549
7550 /* Construct a ClientMessage event to send to the frame. */
7551 ev->type = ClientMessage;
7552 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7553 ev->display = FRAME_X_DISPLAY (f);
7554 ev->window = FRAME_X_WINDOW (f);
7555 ev->format = 32;
52e386c2 7556 ev->data.l[0] = (long) XFASTINT (window);
06a2c219
GM
7557 ev->data.l[1] = (long) part;
7558 ev->data.l[2] = (long) 0;
7559 ev->data.l[3] = (long) portion;
7560 ev->data.l[4] = (long) whole;
7561
bffcfca9
GM
7562 /* Make Xt timeouts work while the scroll bar is active. */
7563 toolkit_scroll_bar_interaction = 1;
7564
06a2c219
GM
7565 /* Setting the event mask to zero means that the message will
7566 be sent to the client that created the window, and if that
7567 window no longer exists, no event will be sent. */
7568 BLOCK_INPUT;
7569 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7570 UNBLOCK_INPUT;
7571}
7572
7573
7574/* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7575 in *IEVENT. */
7576
7577static void
7578x_scroll_bar_to_input_event (event, ievent)
7579 XEvent *event;
7580 struct input_event *ievent;
7581{
7582 XClientMessageEvent *ev = (XClientMessageEvent *) event;
52e386c2
KR
7583 Lisp_Object window;
7584 struct frame *f;
7585
7586 XSETFASTINT (window, ev->data.l[0]);
7587 f = XFRAME (XWINDOW (window)->frame);
06a2c219
GM
7588
7589 ievent->kind = scroll_bar_click;
7590 ievent->frame_or_window = window;
0f8aabe9 7591 ievent->arg = Qnil;
06a2c219
GM
7592 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7593 ievent->part = ev->data.l[1];
7594 ievent->code = ev->data.l[2];
7595 ievent->x = make_number ((int) ev->data.l[3]);
7596 ievent->y = make_number ((int) ev->data.l[4]);
7597 ievent->modifiers = 0;
7598}
7599
7600
7601#ifdef USE_MOTIF
7602
7603/* Minimum and maximum values used for Motif scroll bars. */
7604
7605#define XM_SB_MIN 1
7606#define XM_SB_MAX 10000000
7607#define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7608
7609
7610/* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7611 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7612 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7613
7614static void
7615xm_scroll_callback (widget, client_data, call_data)
7616 Widget widget;
7617 XtPointer client_data, call_data;
7618{
7619 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7620 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7621 double percent;
7622 int part = -1, whole = 0, portion = 0;
7623
7624 switch (cs->reason)
7625 {
7626 case XmCR_DECREMENT:
7627 bar->dragging = Qnil;
7628 part = scroll_bar_up_arrow;
7629 break;
7630
7631 case XmCR_INCREMENT:
7632 bar->dragging = Qnil;
7633 part = scroll_bar_down_arrow;
7634 break;
7635
7636 case XmCR_PAGE_DECREMENT:
7637 bar->dragging = Qnil;
7638 part = scroll_bar_above_handle;
7639 break;
7640
7641 case XmCR_PAGE_INCREMENT:
7642 bar->dragging = Qnil;
7643 part = scroll_bar_below_handle;
7644 break;
7645
7646 case XmCR_TO_TOP:
7647 bar->dragging = Qnil;
7648 part = scroll_bar_to_top;
7649 break;
7650
7651 case XmCR_TO_BOTTOM:
7652 bar->dragging = Qnil;
7653 part = scroll_bar_to_bottom;
7654 break;
7655
7656 case XmCR_DRAG:
7657 {
7658 int slider_size;
7659 int dragging_down_p = (INTEGERP (bar->dragging)
7660 && XINT (bar->dragging) <= cs->value);
7661
7662 /* Get the slider size. */
7663 BLOCK_INPUT;
7664 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7665 UNBLOCK_INPUT;
7666
7667 /* At the max position of the scroll bar, do a line-wise
7668 movement. Without doing anything, the LessTif scroll bar
7669 calls us with the same cs->value again and again. If we
7670 want to make sure that we can reach the end of the buffer,
7671 we have to do something.
7672
7673 Implementation note: setting bar->dragging always to
7674 cs->value gives a smoother movement at the max position.
7675 Setting it to nil when doing line-wise movement gives
7676 a better slider behavior. */
7677
7678 if (cs->value + slider_size == XM_SB_MAX
7679 || (dragging_down_p
7680 && last_scroll_bar_part == scroll_bar_down_arrow))
7681 {
7682 part = scroll_bar_down_arrow;
7683 bar->dragging = Qnil;
7684 }
7685 else
7686 {
7687 whole = XM_SB_RANGE;
7688 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7689 part = scroll_bar_handle;
7690 bar->dragging = make_number (cs->value);
7691 }
7692 }
7693 break;
7694
7695 case XmCR_VALUE_CHANGED:
7696 break;
7697 };
7698
7699 if (part >= 0)
7700 {
7701 window_being_scrolled = bar->window;
7702 last_scroll_bar_part = part;
7703 x_send_scroll_bar_event (bar->window, part, portion, whole);
7704 }
7705}
7706
7707
ec18280f 7708#else /* !USE_MOTIF, i.e. Xaw. */
06a2c219
GM
7709
7710
ec18280f 7711/* Xaw scroll bar callback. Invoked when the thumb is dragged.
06a2c219
GM
7712 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7713 scroll bar struct. CALL_DATA is a pointer to a float saying where
7714 the thumb is. */
7715
7716static void
ec18280f 7717xaw_jump_callback (widget, client_data, call_data)
06a2c219
GM
7718 Widget widget;
7719 XtPointer client_data, call_data;
7720{
7721 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7722 float top = *(float *) call_data;
7723 float shown;
ec18280f
SM
7724 int whole, portion, height;
7725 int part;
06a2c219
GM
7726
7727 /* Get the size of the thumb, a value between 0 and 1. */
7728 BLOCK_INPUT;
ec18280f 7729 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
06a2c219
GM
7730 UNBLOCK_INPUT;
7731
7732 whole = 10000000;
7733 portion = shown < 1 ? top * whole : 0;
06a2c219 7734
ec18280f
SM
7735 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
7736 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
7737 the bottom, so we force the scrolling whenever we see that we're
7738 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
7739 we try to ensure that we always stay two pixels away from the
7740 bottom). */
06a2c219
GM
7741 part = scroll_bar_down_arrow;
7742 else
7743 part = scroll_bar_handle;
7744
7745 window_being_scrolled = bar->window;
7746 bar->dragging = make_number (portion);
7747 last_scroll_bar_part = part;
7748 x_send_scroll_bar_event (bar->window, part, portion, whole);
7749}
7750
7751
ec18280f
SM
7752/* Xaw scroll bar callback. Invoked for incremental scrolling.,
7753 i.e. line or page up or down. WIDGET is the Xaw scroll bar
06a2c219
GM
7754 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
7755 the scroll bar. CALL_DATA is an integer specifying the action that
7756 has taken place. It's magnitude is in the range 0..height of the
7757 scroll bar. Negative values mean scroll towards buffer start.
7758 Values < height of scroll bar mean line-wise movement. */
7759
7760static void
ec18280f 7761xaw_scroll_callback (widget, client_data, call_data)
06a2c219
GM
7762 Widget widget;
7763 XtPointer client_data, call_data;
7764{
7765 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7766 int position = (int) call_data;
7767 Dimension height;
7768 int part;
7769
7770 /* Get the height of the scroll bar. */
7771 BLOCK_INPUT;
7772 XtVaGetValues (widget, XtNheight, &height, NULL);
7773 UNBLOCK_INPUT;
7774
ec18280f
SM
7775 if (abs (position) >= height)
7776 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
7777
7778 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
7779 it maps line-movement to call_data = max(5, height/20). */
7780 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
7781 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
06a2c219 7782 else
ec18280f 7783 part = scroll_bar_move_ratio;
06a2c219
GM
7784
7785 window_being_scrolled = bar->window;
7786 bar->dragging = Qnil;
7787 last_scroll_bar_part = part;
ec18280f 7788 x_send_scroll_bar_event (bar->window, part, position, height);
06a2c219
GM
7789}
7790
7791
7792#endif /* not USE_MOTIF */
7793
7794
7795/* Create the widget for scroll bar BAR on frame F. Record the widget
7796 and X window of the scroll bar in BAR. */
7797
7798static void
7799x_create_toolkit_scroll_bar (f, bar)
7800 struct frame *f;
7801 struct scroll_bar *bar;
7802{
7803 Window xwindow;
7804 Widget widget;
7805 Arg av[20];
7806 int ac = 0;
7807 char *scroll_bar_name = "verticalScrollBar";
7808 unsigned long pixel;
7809
7810 BLOCK_INPUT;
7811
7812#ifdef USE_MOTIF
7813 /* LessTif 0.85, problems:
7814
7815 1. When the mouse if over the scroll bar, the scroll bar will
7816 get keyboard events. I didn't find a way to turn this off.
7817
7818 2. Do we have to explicitly set the cursor to get an arrow
7819 cursor (see below)? */
7820
7821 /* Set resources. Create the widget. */
7822 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7823 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
7824 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
7825 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
7826 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
7827 XtSetArg (av[ac], XmNincrement, 1); ++ac;
7828 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
7829
7830 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7831 if (pixel != -1)
7832 {
7833 XtSetArg (av[ac], XmNforeground, pixel);
7834 ++ac;
7835 }
7836
7837 pixel = f->output_data.x->scroll_bar_background_pixel;
7838 if (pixel != -1)
7839 {
7840 XtSetArg (av[ac], XmNbackground, pixel);
7841 ++ac;
7842 }
7843
7844 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
7845 scroll_bar_name, av, ac);
7846
7847 /* Add one callback for everything that can happen. */
7848 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
7849 (XtPointer) bar);
7850 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
7851 (XtPointer) bar);
7852 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
7853 (XtPointer) bar);
7854 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
7855 (XtPointer) bar);
7856 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
7857 (XtPointer) bar);
7858 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
7859 (XtPointer) bar);
7860 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
7861 (XtPointer) bar);
7862
7863 /* Realize the widget. Only after that is the X window created. */
7864 XtRealizeWidget (widget);
7865
7866 /* Set the cursor to an arrow. I didn't find a resource to do that.
7867 And I'm wondering why it hasn't an arrow cursor by default. */
7868 XDefineCursor (XtDisplay (widget), XtWindow (widget),
7869 f->output_data.x->nontext_cursor);
7870
ec18280f 7871#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
7872
7873 /* Set resources. Create the widget. The background of the
7874 Xaw3d scroll bar widget is a little bit light for my taste.
7875 We don't alter it here to let users change it according
7876 to their taste with `emacs*verticalScrollBar.background: xxx'. */
7877 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7878 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
ec18280f
SM
7879 /* For smoother scrolling with Xaw3d -sm */
7880 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
7881 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
06a2c219
GM
7882
7883 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7884 if (pixel != -1)
7885 {
7886 XtSetArg (av[ac], XtNforeground, pixel);
7887 ++ac;
7888 }
7889
7890 pixel = f->output_data.x->scroll_bar_background_pixel;
7891 if (pixel != -1)
7892 {
7893 XtSetArg (av[ac], XtNbackground, pixel);
7894 ++ac;
7895 }
7896
7897 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
7898 f->output_data.x->edit_widget, av, ac);
ec18280f
SM
7899
7900 {
7901 char *initial = "";
7902 char *val = initial;
7903 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
7904 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
7905 if (val == initial)
7906 { /* ARROW_SCROLL */
7907 xaw3d_arrow_scroll = True;
7908 /* Isn't that just a personal preference ? -sm */
7909 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
7910 }
7911 }
06a2c219
GM
7912
7913 /* Define callbacks. */
ec18280f
SM
7914 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
7915 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
06a2c219
GM
7916 (XtPointer) bar);
7917
7918 /* Realize the widget. Only after that is the X window created. */
7919 XtRealizeWidget (widget);
7920
ec18280f 7921#endif /* !USE_MOTIF */
06a2c219
GM
7922
7923 /* Install an action hook that let's us detect when the user
7924 finishes interacting with a scroll bar. */
7925 if (action_hook_id == 0)
7926 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
7927
7928 /* Remember X window and widget in the scroll bar vector. */
7929 SET_SCROLL_BAR_X_WIDGET (bar, widget);
7930 xwindow = XtWindow (widget);
7931 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
7932
7933 UNBLOCK_INPUT;
7934}
7935
7936
7937/* Set the thumb size and position of scroll bar BAR. We are currently
7938 displaying PORTION out of a whole WHOLE, and our position POSITION. */
7939
7940static void
7941x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
7942 struct scroll_bar *bar;
7943 int portion, position, whole;
f451eb13 7944{
06a2c219 7945 float top, shown;
06a2c219 7946 Widget widget = SCROLL_BAR_X_WIDGET (bar);
f451eb13 7947
06a2c219
GM
7948 if (whole == 0)
7949 top = 0, shown = 1;
7950 else
f451eb13 7951 {
06a2c219
GM
7952 top = (float) position / whole;
7953 shown = (float) portion / whole;
7954 }
f451eb13 7955
06a2c219 7956 BLOCK_INPUT;
f451eb13 7957
06a2c219
GM
7958#ifdef USE_MOTIF
7959 {
7960 int size, value;
7961 Boolean arrow1_selected, arrow2_selected;
7962 unsigned char flags;
7963 XmScrollBarWidget sb;
7964
ec18280f 7965 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
06a2c219
GM
7966 is the scroll bar's maximum and MIN is the scroll bar's minimum
7967 value. */
7968 size = shown * XM_SB_RANGE;
7969 size = min (size, XM_SB_RANGE);
7970 size = max (size, 1);
7971
7972 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
7973 value = top * XM_SB_RANGE;
7974 value = min (value, XM_SB_MAX - size);
7975 value = max (value, XM_SB_MIN);
7976
7977 /* LessTif: Calling XmScrollBarSetValues after an increment or
7978 decrement turns off auto-repeat LessTif-internally. This can
7979 be seen in ScrollBar.c which resets Arrow1Selected and
7980 Arrow2Selected. It also sets internal flags so that LessTif
7981 believes the mouse is in the slider. We either have to change
7982 our code, or work around that by accessing private data. */
7983
7984 sb = (XmScrollBarWidget) widget;
7985 arrow1_selected = sb->scrollBar.arrow1_selected;
7986 arrow2_selected = sb->scrollBar.arrow2_selected;
7987 flags = sb->scrollBar.flags;
7988
7989 if (NILP (bar->dragging))
7990 XmScrollBarSetValues (widget, value, size, 0, 0, False);
7991 else if (last_scroll_bar_part == scroll_bar_down_arrow)
7992 /* This has the negative side effect that the slider value is
ec18280f 7993 not what it would be if we scrolled here using line-wise or
06a2c219
GM
7994 page-wise movement. */
7995 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
7996 else
7997 {
7998 /* If currently dragging, only update the slider size.
7999 This reduces flicker effects. */
8000 int old_value, old_size, increment, page_increment;
8001
8002 XmScrollBarGetValues (widget, &old_value, &old_size,
8003 &increment, &page_increment);
8004 XmScrollBarSetValues (widget, old_value,
8005 min (size, XM_SB_RANGE - old_value),
8006 0, 0, False);
8007 }
8008
8009 sb->scrollBar.arrow1_selected = arrow1_selected;
8010 sb->scrollBar.arrow2_selected = arrow2_selected;
8011 sb->scrollBar.flags = flags;
8012 }
ec18280f 8013#else /* !USE_MOTIF i.e. use Xaw */
06a2c219 8014 {
ec18280f
SM
8015 float old_top, old_shown;
8016 Dimension height;
8017 XtVaGetValues (widget,
8018 XtNtopOfThumb, &old_top,
8019 XtNshown, &old_shown,
8020 XtNheight, &height,
8021 NULL);
8022
8023 /* Massage the top+shown values. */
8024 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8025 top = max (0, min (1, top));
8026 else
8027 top = old_top;
8028 /* Keep two pixels available for moving the thumb down. */
8029 shown = max (0, min (1 - top - (2.0 / height), shown));
06a2c219
GM
8030
8031 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8032 check that your system's configuration file contains a define
8033 for `NARROWPROTO'. See s/freebsd.h for an example. */
ec18280f 8034 if (top != old_top || shown != old_shown)
eb393530 8035 {
ec18280f 8036 if (NILP (bar->dragging))
eb393530 8037 XawScrollbarSetThumb (widget, top, shown);
06a2c219
GM
8038 else
8039 {
ec18280f
SM
8040#ifdef HAVE_XAW3D
8041 ScrollbarWidget sb = (ScrollbarWidget) widget;
3e71d8f2 8042 int scroll_mode = 0;
ec18280f
SM
8043
8044 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8045 if (xaw3d_arrow_scroll)
8046 {
8047 /* Xaw3d stupidly ignores resize requests while dragging
8048 so we have to make it believe it's not in dragging mode. */
8049 scroll_mode = sb->scrollbar.scroll_mode;
8050 if (scroll_mode == 2)
8051 sb->scrollbar.scroll_mode = 0;
8052 }
8053#endif
8054 /* Try to make the scrolling a tad smoother. */
8055 if (!xaw3d_pick_top)
8056 shown = min (shown, old_shown);
8057
8058 XawScrollbarSetThumb (widget, top, shown);
8059
8060#ifdef HAVE_XAW3D
8061 if (xaw3d_arrow_scroll && scroll_mode == 2)
8062 sb->scrollbar.scroll_mode = scroll_mode;
8063#endif
06a2c219 8064 }
06a2c219
GM
8065 }
8066 }
ec18280f 8067#endif /* !USE_MOTIF */
06a2c219
GM
8068
8069 UNBLOCK_INPUT;
f451eb13
JB
8070}
8071
06a2c219
GM
8072#endif /* USE_TOOLKIT_SCROLL_BARS */
8073
8074
8075\f
8076/************************************************************************
8077 Scroll bars, general
8078 ************************************************************************/
8079
8080/* Create a scroll bar and return the scroll bar vector for it. W is
8081 the Emacs window on which to create the scroll bar. TOP, LEFT,
8082 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8083 scroll bar. */
8084
ab648270 8085static struct scroll_bar *
06a2c219
GM
8086x_scroll_bar_create (w, top, left, width, height)
8087 struct window *w;
f451eb13
JB
8088 int top, left, width, height;
8089{
06a2c219 8090 struct frame *f = XFRAME (w->frame);
334208b7
RS
8091 struct scroll_bar *bar
8092 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
f451eb13
JB
8093
8094 BLOCK_INPUT;
8095
06a2c219
GM
8096#if USE_TOOLKIT_SCROLL_BARS
8097 x_create_toolkit_scroll_bar (f, bar);
8098#else /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8099 {
8100 XSetWindowAttributes a;
8101 unsigned long mask;
5c187dee 8102 Window window;
06a2c219
GM
8103
8104 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8105 if (a.background_pixel == -1)
8106 a.background_pixel = f->output_data.x->background_pixel;
8107
12ba150f 8108 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9a572e2a 8109 | ButtonMotionMask | PointerMotionHintMask
12ba150f 8110 | ExposureMask);
7a13e894 8111 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
f451eb13 8112
dbc4e1c1 8113 mask = (CWBackPixel | CWEventMask | CWCursor);
f451eb13 8114
06a2c219
GM
8115 /* Clear the area of W that will serve as a scroll bar. This is
8116 for the case that a window has been split horizontally. In
8117 this case, no clear_frame is generated to reduce flickering. */
8118 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8119 left, top, width,
8120 window_box_height (w), False);
8121
8122 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8123 /* Position and size of scroll bar. */
8124 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8125 top,
8126 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8127 height,
8128 /* Border width, depth, class, and visual. */
8129 0,
8130 CopyFromParent,
8131 CopyFromParent,
8132 CopyFromParent,
8133 /* Attributes. */
8134 mask, &a);
8135 SET_SCROLL_BAR_X_WINDOW (bar, window);
f451eb13 8136 }
06a2c219 8137#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 8138
06a2c219 8139 XSETWINDOW (bar->window, w);
e0c1aef2
KH
8140 XSETINT (bar->top, top);
8141 XSETINT (bar->left, left);
8142 XSETINT (bar->width, width);
8143 XSETINT (bar->height, height);
8144 XSETINT (bar->start, 0);
8145 XSETINT (bar->end, 0);
12ba150f 8146 bar->dragging = Qnil;
f451eb13
JB
8147
8148 /* Add bar to its frame's list of scroll bars. */
334208b7 8149 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 8150 bar->prev = Qnil;
334208b7 8151 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
06a2c219 8152 if (!NILP (bar->next))
e0c1aef2 8153 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13 8154
06a2c219
GM
8155 /* Map the window/widget. */
8156#if USE_TOOLKIT_SCROLL_BARS
8157 XtMapWidget (SCROLL_BAR_X_WIDGET (bar));
8158 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
8159 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8160 top,
8161 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8162 height, 0);
8163#else /* not USE_TOOLKIT_SCROLL_BARS */
7f9c7f94 8164 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
06a2c219 8165#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8166
8167 UNBLOCK_INPUT;
12ba150f 8168 return bar;
f451eb13
JB
8169}
8170
06a2c219 8171
12ba150f 8172/* Draw BAR's handle in the proper position.
06a2c219 8173
12ba150f
JB
8174 If the handle is already drawn from START to END, don't bother
8175 redrawing it, unless REBUILD is non-zero; in that case, always
8176 redraw it. (REBUILD is handy for drawing the handle after expose
58769bee 8177 events.)
12ba150f
JB
8178
8179 Normally, we want to constrain the start and end of the handle to
06a2c219
GM
8180 fit inside its rectangle, but if the user is dragging the scroll
8181 bar handle, we want to let them drag it down all the way, so that
8182 the bar's top is as far down as it goes; otherwise, there's no way
8183 to move to the very end of the buffer. */
8184
5c187dee
GM
8185#ifndef USE_TOOLKIT_SCROLL_BARS
8186
f451eb13 8187static void
ab648270
JB
8188x_scroll_bar_set_handle (bar, start, end, rebuild)
8189 struct scroll_bar *bar;
f451eb13 8190 int start, end;
12ba150f 8191 int rebuild;
f451eb13 8192{
12ba150f 8193 int dragging = ! NILP (bar->dragging);
ab648270 8194 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8195 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8196 GC gc = f->output_data.x->normal_gc;
12ba150f
JB
8197
8198 /* If the display is already accurate, do nothing. */
8199 if (! rebuild
8200 && start == XINT (bar->start)
8201 && end == XINT (bar->end))
8202 return;
8203
f451eb13
JB
8204 BLOCK_INPUT;
8205
8206 {
d9cdbb3d
RS
8207 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8208 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8209 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
f451eb13
JB
8210
8211 /* Make sure the values are reasonable, and try to preserve
8212 the distance between start and end. */
12ba150f
JB
8213 {
8214 int length = end - start;
8215
8216 if (start < 0)
8217 start = 0;
8218 else if (start > top_range)
8219 start = top_range;
8220 end = start + length;
8221
8222 if (end < start)
8223 end = start;
8224 else if (end > top_range && ! dragging)
8225 end = top_range;
8226 }
f451eb13 8227
ab648270 8228 /* Store the adjusted setting in the scroll bar. */
e0c1aef2
KH
8229 XSETINT (bar->start, start);
8230 XSETINT (bar->end, end);
f451eb13 8231
12ba150f
JB
8232 /* Clip the end position, just for display. */
8233 if (end > top_range)
8234 end = top_range;
f451eb13 8235
ab648270 8236 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
12ba150f
JB
8237 below top positions, to make sure the handle is always at least
8238 that many pixels tall. */
ab648270 8239 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
f451eb13 8240
12ba150f
JB
8241 /* Draw the empty space above the handle. Note that we can't clear
8242 zero-height areas; that means "clear to end of window." */
8243 if (0 < start)
334208b7 8244 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 8245
12ba150f 8246 /* x, y, width, height, and exposures. */
ab648270
JB
8247 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8248 VERTICAL_SCROLL_BAR_TOP_BORDER,
12ba150f
JB
8249 inside_width, start,
8250 False);
f451eb13 8251
06a2c219
GM
8252 /* Change to proper foreground color if one is specified. */
8253 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8254 XSetForeground (FRAME_X_DISPLAY (f), gc,
8255 f->output_data.x->scroll_bar_foreground_pixel);
8256
12ba150f 8257 /* Draw the handle itself. */
334208b7 8258 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13 8259
12ba150f 8260 /* x, y, width, height */
ab648270
JB
8261 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8262 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
12ba150f 8263 inside_width, end - start);
f451eb13 8264
06a2c219
GM
8265 /* Restore the foreground color of the GC if we changed it above. */
8266 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8267 XSetForeground (FRAME_X_DISPLAY (f), gc,
8268 f->output_data.x->foreground_pixel);
f451eb13 8269
12ba150f
JB
8270 /* Draw the empty space below the handle. Note that we can't
8271 clear zero-height areas; that means "clear to end of window." */
8272 if (end < inside_height)
334208b7 8273 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 8274
12ba150f 8275 /* x, y, width, height, and exposures. */
ab648270
JB
8276 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8277 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
12ba150f
JB
8278 inside_width, inside_height - end,
8279 False);
f451eb13 8280
f451eb13
JB
8281 }
8282
f451eb13
JB
8283 UNBLOCK_INPUT;
8284}
8285
5c187dee 8286#endif /* !USE_TOOLKIT_SCROLL_BARS */
f451eb13 8287
06a2c219
GM
8288/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8289 nil. */
58769bee 8290
12ba150f 8291static void
ab648270
JB
8292x_scroll_bar_remove (bar)
8293 struct scroll_bar *bar;
12ba150f 8294{
12ba150f
JB
8295 BLOCK_INPUT;
8296
06a2c219
GM
8297#if USE_TOOLKIT_SCROLL_BARS
8298 XtDestroyWidget (SCROLL_BAR_X_WIDGET (bar));
8299#else /* not USE_TOOLKIT_SCROLL_BARS */
5c187dee
GM
8300 {
8301 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8302 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8303 }
06a2c219
GM
8304#endif /* not USE_TOOLKIT_SCROLL_BARS */
8305
ab648270
JB
8306 /* Disassociate this scroll bar from its window. */
8307 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
12ba150f
JB
8308
8309 UNBLOCK_INPUT;
8310}
8311
06a2c219 8312
12ba150f
JB
8313/* Set the handle of the vertical scroll bar for WINDOW to indicate
8314 that we are displaying PORTION characters out of a total of WHOLE
ab648270 8315 characters, starting at POSITION. If WINDOW has no scroll bar,
12ba150f 8316 create one. */
06a2c219 8317
12ba150f 8318static void
06a2c219
GM
8319XTset_vertical_scroll_bar (w, portion, whole, position)
8320 struct window *w;
f451eb13
JB
8321 int portion, whole, position;
8322{
06a2c219 8323 struct frame *f = XFRAME (w->frame);
ab648270 8324 struct scroll_bar *bar;
3c6ede7b 8325 int top, height, left, sb_left, width, sb_width;
06a2c219 8326 int window_x, window_y, window_width, window_height;
06a2c219 8327
3c6ede7b 8328 /* Get window dimensions. */
06a2c219 8329 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
3c6ede7b
GM
8330 top = window_y;
8331 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8332 height = window_height;
06a2c219 8333
3c6ede7b 8334 /* Compute the left edge of the scroll bar area. */
06a2c219 8335 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
3c6ede7b
GM
8336 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8337 else
8338 left = XFASTINT (w->left);
8339 left *= CANON_X_UNIT (f);
8340 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8341
8342 /* Compute the width of the scroll bar which might be less than
8343 the width of the area reserved for the scroll bar. */
8344 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8345 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
06a2c219 8346 else
3c6ede7b 8347 sb_width = width;
12ba150f 8348
3c6ede7b
GM
8349 /* Compute the left edge of the scroll bar. */
8350#ifdef USE_TOOLKIT_SCROLL_BARS
8351 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8352 sb_left = left + width - sb_width - (width - sb_width) / 2;
8353 else
8354 sb_left = left + (width - sb_width) / 2;
8355#else
8356 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8357 sb_left = left + width - sb_width;
8358 else
8359 sb_left = left;
8360#endif
8361
ab648270 8362 /* Does the scroll bar exist yet? */
06a2c219 8363 if (NILP (w->vertical_scroll_bar))
3c6ede7b 8364 {
80c32bcc 8365 BLOCK_INPUT;
3c6ede7b
GM
8366 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8367 left, top, width, height, False);
80c32bcc 8368 UNBLOCK_INPUT;
3c6ede7b
GM
8369 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8370 }
f451eb13 8371 else
12ba150f
JB
8372 {
8373 /* It may just need to be moved and resized. */
06a2c219
GM
8374 unsigned int mask = 0;
8375
8376 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8377
8378 BLOCK_INPUT;
8379
3c6ede7b 8380 if (sb_left != XINT (bar->left))
06a2c219 8381 mask |= CWX;
3c6ede7b 8382 if (top != XINT (bar->top))
06a2c219 8383 mask |= CWY;
3c6ede7b 8384 if (sb_width != XINT (bar->width))
06a2c219 8385 mask |= CWWidth;
3c6ede7b 8386 if (height != XINT (bar->height))
06a2c219
GM
8387 mask |= CWHeight;
8388
8389#ifdef USE_TOOLKIT_SCROLL_BARS
fe6f39d9
GM
8390
8391 /* Since toolkit scroll bars are smaller than the space reserved
8392 for them on the frame, we have to clear "under" them. */
8393 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3c6ede7b 8394 left, top, width, height, False);
06a2c219
GM
8395
8396 /* Move/size the scroll bar widget. */
8397 if (mask)
8398 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
3c6ede7b
GM
8399 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8400 top,
8401 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8402 height, 0);
06a2c219
GM
8403
8404#else /* not USE_TOOLKIT_SCROLL_BARS */
8405
e1f6572f
RS
8406 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8407 {
8408 /* Clear areas not covered by the scroll bar. This makes sure a
8409 previous mode line display is cleared after C-x 2 C-x 1, for
8410 example. Non-toolkit scroll bars are as wide as the area
8411 reserved for scroll bars - trim at both sides. */
8412 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8413 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8414 height, False);
8415 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8416 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8417 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8418 height, False);
8419 }
06a2c219
GM
8420
8421 /* Move/size the scroll bar window. */
8422 if (mask)
8423 {
8424 XWindowChanges wc;
8425
3c6ede7b
GM
8426 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8427 wc.y = top;
8428 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8429 wc.height = height;
06a2c219
GM
8430 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8431 mask, &wc);
8432 }
8433
8434#endif /* not USE_TOOLKIT_SCROLL_BARS */
8435
8436 /* Remember new settings. */
3c6ede7b
GM
8437 XSETINT (bar->left, sb_left);
8438 XSETINT (bar->top, top);
8439 XSETINT (bar->width, sb_width);
8440 XSETINT (bar->height, height);
06a2c219
GM
8441
8442 UNBLOCK_INPUT;
12ba150f 8443 }
f451eb13 8444
06a2c219
GM
8445#if USE_TOOLKIT_SCROLL_BARS
8446 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8447#else /* not USE_TOOLKIT_SCROLL_BARS */
ab648270 8448 /* Set the scroll bar's current state, unless we're currently being
f451eb13 8449 dragged. */
12ba150f 8450 if (NILP (bar->dragging))
f451eb13 8451 {
92857db0 8452 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
f451eb13 8453
12ba150f 8454 if (whole == 0)
ab648270 8455 x_scroll_bar_set_handle (bar, 0, top_range, 0);
12ba150f
JB
8456 else
8457 {
43f868f5
JB
8458 int start = ((double) position * top_range) / whole;
8459 int end = ((double) (position + portion) * top_range) / whole;
ab648270 8460 x_scroll_bar_set_handle (bar, start, end, 0);
12ba150f 8461 }
f451eb13 8462 }
06a2c219 8463#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 8464
06a2c219 8465 XSETVECTOR (w->vertical_scroll_bar, bar);
f451eb13
JB
8466}
8467
12ba150f 8468
f451eb13 8469/* The following three hooks are used when we're doing a thorough
ab648270 8470 redisplay of the frame. We don't explicitly know which scroll bars
f451eb13 8471 are going to be deleted, because keeping track of when windows go
12ba150f
JB
8472 away is a real pain - "Can you say set-window-configuration, boys
8473 and girls?" Instead, we just assert at the beginning of redisplay
ab648270 8474 that *all* scroll bars are to be removed, and then save a scroll bar
12ba150f 8475 from the fiery pit when we actually redisplay its window. */
f451eb13 8476
ab648270
JB
8477/* Arrange for all scroll bars on FRAME to be removed at the next call
8478 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
06a2c219
GM
8479 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8480
58769bee 8481static void
ab648270 8482XTcondemn_scroll_bars (frame)
f451eb13
JB
8483 FRAME_PTR frame;
8484{
f9e24cb9
RS
8485 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8486 while (! NILP (FRAME_SCROLL_BARS (frame)))
8487 {
8488 Lisp_Object bar;
8489 bar = FRAME_SCROLL_BARS (frame);
8490 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8491 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8492 XSCROLL_BAR (bar)->prev = Qnil;
8493 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8494 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8495 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8496 }
f451eb13
JB
8497}
8498
06a2c219 8499/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
12ba150f 8500 Note that WINDOW isn't necessarily condemned at all. */
f451eb13 8501static void
ab648270 8502XTredeem_scroll_bar (window)
12ba150f 8503 struct window *window;
f451eb13 8504{
ab648270 8505 struct scroll_bar *bar;
12ba150f 8506
ab648270
JB
8507 /* We can't redeem this window's scroll bar if it doesn't have one. */
8508 if (NILP (window->vertical_scroll_bar))
12ba150f
JB
8509 abort ();
8510
ab648270 8511 bar = XSCROLL_BAR (window->vertical_scroll_bar);
12ba150f
JB
8512
8513 /* Unlink it from the condemned list. */
8514 {
8515 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
8516
8517 if (NILP (bar->prev))
8518 {
8519 /* If the prev pointer is nil, it must be the first in one of
8520 the lists. */
ab648270 8521 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
12ba150f
JB
8522 /* It's not condemned. Everything's fine. */
8523 return;
ab648270
JB
8524 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8525 window->vertical_scroll_bar))
8526 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
12ba150f
JB
8527 else
8528 /* If its prev pointer is nil, it must be at the front of
8529 one or the other! */
8530 abort ();
8531 }
8532 else
ab648270 8533 XSCROLL_BAR (bar->prev)->next = bar->next;
12ba150f
JB
8534
8535 if (! NILP (bar->next))
ab648270 8536 XSCROLL_BAR (bar->next)->prev = bar->prev;
12ba150f 8537
ab648270 8538 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 8539 bar->prev = Qnil;
e0c1aef2 8540 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
12ba150f 8541 if (! NILP (bar->next))
e0c1aef2 8542 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
12ba150f 8543 }
f451eb13
JB
8544}
8545
ab648270
JB
8546/* Remove all scroll bars on FRAME that haven't been saved since the
8547 last call to `*condemn_scroll_bars_hook'. */
06a2c219 8548
f451eb13 8549static void
ab648270 8550XTjudge_scroll_bars (f)
12ba150f 8551 FRAME_PTR f;
f451eb13 8552{
12ba150f 8553 Lisp_Object bar, next;
f451eb13 8554
ab648270 8555 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
cf7cb199
JB
8556
8557 /* Clear out the condemned list now so we won't try to process any
ab648270
JB
8558 more events on the hapless scroll bars. */
8559 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
cf7cb199
JB
8560
8561 for (; ! NILP (bar); bar = next)
f451eb13 8562 {
ab648270 8563 struct scroll_bar *b = XSCROLL_BAR (bar);
12ba150f 8564
ab648270 8565 x_scroll_bar_remove (b);
12ba150f
JB
8566
8567 next = b->next;
8568 b->next = b->prev = Qnil;
f451eb13 8569 }
12ba150f 8570
ab648270 8571 /* Now there should be no references to the condemned scroll bars,
12ba150f 8572 and they should get garbage-collected. */
f451eb13
JB
8573}
8574
8575
06a2c219
GM
8576/* Handle an Expose or GraphicsExpose event on a scroll bar. This
8577 is a no-op when using toolkit scroll bars.
ab648270
JB
8578
8579 This may be called from a signal handler, so we have to ignore GC
8580 mark bits. */
06a2c219 8581
f451eb13 8582static void
ab648270
JB
8583x_scroll_bar_expose (bar, event)
8584 struct scroll_bar *bar;
f451eb13
JB
8585 XEvent *event;
8586{
06a2c219
GM
8587#ifndef USE_TOOLKIT_SCROLL_BARS
8588
ab648270 8589 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8590 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8591 GC gc = f->output_data.x->normal_gc;
3cbd2e0b 8592 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
12ba150f 8593
f451eb13
JB
8594 BLOCK_INPUT;
8595
ab648270 8596 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
f451eb13 8597
06a2c219 8598 /* Draw a one-pixel border just inside the edges of the scroll bar. */
334208b7 8599 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13
JB
8600
8601 /* x, y, width, height */
d9cdbb3d 8602 0, 0,
3cbd2e0b 8603 XINT (bar->width) - 1 - width_trim - width_trim,
d9cdbb3d
RS
8604 XINT (bar->height) - 1);
8605
f451eb13 8606 UNBLOCK_INPUT;
06a2c219
GM
8607
8608#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8609}
8610
ab648270
JB
8611/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8612 is set to something other than no_event, it is enqueued.
8613
8614 This may be called from a signal handler, so we have to ignore GC
8615 mark bits. */
06a2c219 8616
5c187dee
GM
8617#ifndef USE_TOOLKIT_SCROLL_BARS
8618
f451eb13 8619static void
ab648270
JB
8620x_scroll_bar_handle_click (bar, event, emacs_event)
8621 struct scroll_bar *bar;
f451eb13
JB
8622 XEvent *event;
8623 struct input_event *emacs_event;
8624{
0299d313 8625 if (! GC_WINDOWP (bar->window))
12ba150f
JB
8626 abort ();
8627
ab648270 8628 emacs_event->kind = scroll_bar_click;
69388238 8629 emacs_event->code = event->xbutton.button - Button1;
0299d313
RS
8630 emacs_event->modifiers
8631 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8632 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8633 event->xbutton.state)
8634 | (event->type == ButtonRelease
8635 ? up_modifier
8636 : down_modifier));
12ba150f 8637 emacs_event->frame_or_window = bar->window;
0f8aabe9 8638 emacs_event->arg = Qnil;
f451eb13 8639 emacs_event->timestamp = event->xbutton.time;
12ba150f 8640 {
06a2c219 8641#if 0
d9cdbb3d 8642 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
0299d313 8643 int internal_height
d9cdbb3d 8644 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8645#endif
0299d313 8646 int top_range
d9cdbb3d 8647 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ab648270 8648 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
12ba150f
JB
8649
8650 if (y < 0) y = 0;
8651 if (y > top_range) y = top_range;
8652
8653 if (y < XINT (bar->start))
ab648270
JB
8654 emacs_event->part = scroll_bar_above_handle;
8655 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8656 emacs_event->part = scroll_bar_handle;
12ba150f 8657 else
ab648270 8658 emacs_event->part = scroll_bar_below_handle;
929787e1
JB
8659
8660 /* Just because the user has clicked on the handle doesn't mean
5116f055
JB
8661 they want to drag it. Lisp code needs to be able to decide
8662 whether or not we're dragging. */
929787e1 8663#if 0
12ba150f
JB
8664 /* If the user has just clicked on the handle, record where they're
8665 holding it. */
8666 if (event->type == ButtonPress
ab648270 8667 && emacs_event->part == scroll_bar_handle)
e0c1aef2 8668 XSETINT (bar->dragging, y - XINT (bar->start));
929787e1 8669#endif
12ba150f
JB
8670
8671 /* If the user has released the handle, set it to its final position. */
8672 if (event->type == ButtonRelease
8673 && ! NILP (bar->dragging))
8674 {
8675 int new_start = y - XINT (bar->dragging);
8676 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
f451eb13 8677
ab648270 8678 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
12ba150f
JB
8679 bar->dragging = Qnil;
8680 }
f451eb13 8681
5116f055
JB
8682 /* Same deal here as the other #if 0. */
8683#if 0
58769bee 8684 /* Clicks on the handle are always reported as occurring at the top of
12ba150f 8685 the handle. */
ab648270 8686 if (emacs_event->part == scroll_bar_handle)
12ba150f
JB
8687 emacs_event->x = bar->start;
8688 else
e0c1aef2 8689 XSETINT (emacs_event->x, y);
5116f055 8690#else
e0c1aef2 8691 XSETINT (emacs_event->x, y);
5116f055 8692#endif
f451eb13 8693
e0c1aef2 8694 XSETINT (emacs_event->y, top_range);
12ba150f
JB
8695 }
8696}
f451eb13 8697
ab648270
JB
8698/* Handle some mouse motion while someone is dragging the scroll bar.
8699
8700 This may be called from a signal handler, so we have to ignore GC
8701 mark bits. */
06a2c219 8702
f451eb13 8703static void
ab648270
JB
8704x_scroll_bar_note_movement (bar, event)
8705 struct scroll_bar *bar;
f451eb13
JB
8706 XEvent *event;
8707{
39d8bb4d
KH
8708 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8709
f451eb13
JB
8710 last_mouse_movement_time = event->xmotion.time;
8711
39d8bb4d 8712 f->mouse_moved = 1;
e0c1aef2 8713 XSETVECTOR (last_mouse_scroll_bar, bar);
f451eb13
JB
8714
8715 /* If we're dragging the bar, display it. */
ab648270 8716 if (! GC_NILP (bar->dragging))
f451eb13
JB
8717 {
8718 /* Where should the handle be now? */
12ba150f 8719 int new_start = event->xmotion.y - XINT (bar->dragging);
f451eb13 8720
12ba150f 8721 if (new_start != XINT (bar->start))
f451eb13 8722 {
12ba150f 8723 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
58769bee 8724
ab648270 8725 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
f451eb13
JB
8726 }
8727 }
f451eb13
JB
8728}
8729
5c187dee
GM
8730#endif /* !USE_TOOLKIT_SCROLL_BARS */
8731
12ba150f 8732/* Return information to the user about the current position of the mouse
ab648270 8733 on the scroll bar. */
06a2c219 8734
12ba150f 8735static void
334208b7
RS
8736x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8737 FRAME_PTR *fp;
12ba150f 8738 Lisp_Object *bar_window;
ab648270 8739 enum scroll_bar_part *part;
12ba150f
JB
8740 Lisp_Object *x, *y;
8741 unsigned long *time;
8742{
ab648270 8743 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
334208b7
RS
8744 Window w = SCROLL_BAR_X_WINDOW (bar);
8745 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f 8746 int win_x, win_y;
559cb2fb
JB
8747 Window dummy_window;
8748 int dummy_coord;
8749 unsigned int dummy_mask;
12ba150f 8750
cf7cb199
JB
8751 BLOCK_INPUT;
8752
ab648270 8753 /* Get the mouse's position relative to the scroll bar window, and
12ba150f 8754 report that. */
334208b7 8755 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
12ba150f 8756
559cb2fb
JB
8757 /* Root, child, root x and root y. */
8758 &dummy_window, &dummy_window,
8759 &dummy_coord, &dummy_coord,
12ba150f 8760
559cb2fb
JB
8761 /* Position relative to scroll bar. */
8762 &win_x, &win_y,
12ba150f 8763
559cb2fb
JB
8764 /* Mouse buttons and modifier keys. */
8765 &dummy_mask))
7a13e894 8766 ;
559cb2fb
JB
8767 else
8768 {
06a2c219 8769#if 0
559cb2fb 8770 int inside_height
d9cdbb3d 8771 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8772#endif
559cb2fb 8773 int top_range
d9cdbb3d 8774 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
559cb2fb
JB
8775
8776 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
8777
8778 if (! NILP (bar->dragging))
8779 win_y -= XINT (bar->dragging);
8780
8781 if (win_y < 0)
8782 win_y = 0;
8783 if (win_y > top_range)
8784 win_y = top_range;
8785
334208b7 8786 *fp = f;
7a13e894 8787 *bar_window = bar->window;
559cb2fb
JB
8788
8789 if (! NILP (bar->dragging))
8790 *part = scroll_bar_handle;
8791 else if (win_y < XINT (bar->start))
8792 *part = scroll_bar_above_handle;
8793 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8794 *part = scroll_bar_handle;
8795 else
8796 *part = scroll_bar_below_handle;
12ba150f 8797
e0c1aef2
KH
8798 XSETINT (*x, win_y);
8799 XSETINT (*y, top_range);
12ba150f 8800
39d8bb4d 8801 f->mouse_moved = 0;
559cb2fb
JB
8802 last_mouse_scroll_bar = Qnil;
8803 }
12ba150f 8804
559cb2fb 8805 *time = last_mouse_movement_time;
cf7cb199 8806
cf7cb199 8807 UNBLOCK_INPUT;
12ba150f
JB
8808}
8809
f451eb13 8810
dbc4e1c1 8811/* The screen has been cleared so we may have changed foreground or
ab648270
JB
8812 background colors, and the scroll bars may need to be redrawn.
8813 Clear out the scroll bars, and ask for expose events, so we can
dbc4e1c1
JB
8814 redraw them. */
8815
dfcf069d 8816void
ab648270 8817x_scroll_bar_clear (f)
dbc4e1c1
JB
8818 FRAME_PTR f;
8819{
06a2c219 8820#ifndef USE_TOOLKIT_SCROLL_BARS
dbc4e1c1
JB
8821 Lisp_Object bar;
8822
b80c363e
RS
8823 /* We can have scroll bars even if this is 0,
8824 if we just turned off scroll bar mode.
8825 But in that case we should not clear them. */
8826 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8827 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
8828 bar = XSCROLL_BAR (bar)->next)
8829 XClearArea (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
8830 0, 0, 0, 0, True);
06a2c219 8831#endif /* not USE_TOOLKIT_SCROLL_BARS */
dbc4e1c1
JB
8832}
8833
06a2c219 8834/* This processes Expose events from the menu-bar specific X event
19126e11 8835 loop in xmenu.c. This allows to redisplay the frame if necessary
06a2c219 8836 when handling menu-bar or pop-up items. */
3afe33e7 8837
06a2c219 8838int
3afe33e7
RS
8839process_expose_from_menu (event)
8840 XEvent event;
8841{
8842 FRAME_PTR f;
19126e11 8843 struct x_display_info *dpyinfo;
06a2c219 8844 int frame_exposed_p = 0;
3afe33e7 8845
f94397b5
KH
8846 BLOCK_INPUT;
8847
19126e11
KH
8848 dpyinfo = x_display_info_for_display (event.xexpose.display);
8849 f = x_window_to_frame (dpyinfo, event.xexpose.window);
3afe33e7
RS
8850 if (f)
8851 {
8852 if (f->async_visible == 0)
8853 {
8854 f->async_visible = 1;
8855 f->async_iconified = 0;
06c488fd 8856 f->output_data.x->has_been_visible = 1;
3afe33e7
RS
8857 SET_FRAME_GARBAGED (f);
8858 }
8859 else
8860 {
06a2c219
GM
8861 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
8862 event.xexpose.x, event.xexpose.y,
8863 event.xexpose.width, event.xexpose.height);
8864 frame_exposed_p = 1;
3afe33e7
RS
8865 }
8866 }
8867 else
8868 {
8869 struct scroll_bar *bar
8870 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 8871
3afe33e7
RS
8872 if (bar)
8873 x_scroll_bar_expose (bar, &event);
8874 }
f94397b5
KH
8875
8876 UNBLOCK_INPUT;
06a2c219 8877 return frame_exposed_p;
3afe33e7 8878}
09756a85
RS
8879\f
8880/* Define a queue to save up SelectionRequest events for later handling. */
8881
8882struct selection_event_queue
8883 {
8884 XEvent event;
8885 struct selection_event_queue *next;
8886 };
8887
8888static struct selection_event_queue *queue;
8889
8890/* Nonzero means queue up certain events--don't process them yet. */
06a2c219 8891
09756a85
RS
8892static int x_queue_selection_requests;
8893
8894/* Queue up an X event *EVENT, to be processed later. */
dbc4e1c1 8895
09756a85 8896static void
334208b7
RS
8897x_queue_event (f, event)
8898 FRAME_PTR f;
09756a85
RS
8899 XEvent *event;
8900{
8901 struct selection_event_queue *queue_tmp
06a2c219 8902 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
09756a85 8903
58769bee 8904 if (queue_tmp != NULL)
09756a85
RS
8905 {
8906 queue_tmp->event = *event;
8907 queue_tmp->next = queue;
8908 queue = queue_tmp;
8909 }
8910}
8911
8912/* Take all the queued events and put them back
8913 so that they get processed afresh. */
8914
8915static void
db3906fd
RS
8916x_unqueue_events (display)
8917 Display *display;
09756a85 8918{
58769bee 8919 while (queue != NULL)
09756a85
RS
8920 {
8921 struct selection_event_queue *queue_tmp = queue;
db3906fd 8922 XPutBackEvent (display, &queue_tmp->event);
09756a85 8923 queue = queue_tmp->next;
06a2c219 8924 xfree ((char *)queue_tmp);
09756a85
RS
8925 }
8926}
8927
8928/* Start queuing SelectionRequest events. */
8929
8930void
db3906fd
RS
8931x_start_queuing_selection_requests (display)
8932 Display *display;
09756a85
RS
8933{
8934 x_queue_selection_requests++;
8935}
8936
8937/* Stop queuing SelectionRequest events. */
8938
8939void
db3906fd
RS
8940x_stop_queuing_selection_requests (display)
8941 Display *display;
09756a85
RS
8942{
8943 x_queue_selection_requests--;
db3906fd 8944 x_unqueue_events (display);
09756a85 8945}
f451eb13
JB
8946\f
8947/* The main X event-reading loop - XTread_socket. */
dc6f92b8 8948
06a2c219 8949/* Time stamp of enter window event. This is only used by XTread_socket,
dc6f92b8
JB
8950 but we have to put it out here, since static variables within functions
8951 sometimes don't work. */
06a2c219 8952
dc6f92b8
JB
8953static Time enter_timestamp;
8954
11edeb03 8955/* This holds the state XLookupString needs to implement dead keys
58769bee 8956 and other tricks known as "compose processing". _X Window System_
11edeb03
JB
8957 says that a portable program can't use this, but Stephen Gildea assures
8958 me that letting the compiler initialize it to zeros will work okay.
8959
8960 This must be defined outside of XTread_socket, for the same reasons
06a2c219
GM
8961 given for enter_time stamp, above. */
8962
11edeb03
JB
8963static XComposeStatus compose_status;
8964
10e6549c
RS
8965/* Record the last 100 characters stored
8966 to help debug the loss-of-chars-during-GC problem. */
06a2c219 8967
2224b905
RS
8968static int temp_index;
8969static short temp_buffer[100];
10e6549c 8970
7a13e894
RS
8971/* Set this to nonzero to fake an "X I/O error"
8972 on a particular display. */
06a2c219 8973
7a13e894
RS
8974struct x_display_info *XTread_socket_fake_io_error;
8975
2224b905
RS
8976/* When we find no input here, we occasionally do a no-op command
8977 to verify that the X server is still running and we can still talk with it.
8978 We try all the open displays, one by one.
8979 This variable is used for cycling thru the displays. */
06a2c219 8980
2224b905
RS
8981static struct x_display_info *next_noop_dpyinfo;
8982
06a2c219
GM
8983#define SET_SAVED_MENU_EVENT(size) \
8984 do \
8985 { \
8986 if (f->output_data.x->saved_menu_event == 0) \
8987 f->output_data.x->saved_menu_event \
8988 = (XEvent *) xmalloc (sizeof (XEvent)); \
8989 bcopy (&event, f->output_data.x->saved_menu_event, size); \
8990 if (numchars >= 1) \
8991 { \
8992 bufp->kind = menu_bar_activate_event; \
8993 XSETFRAME (bufp->frame_or_window, f); \
0f8aabe9 8994 bufp->arg = Qnil; \
06a2c219
GM
8995 bufp++; \
8996 count++; \
8997 numchars--; \
8998 } \
8999 } \
9000 while (0)
9001
8805890a 9002#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
06a2c219 9003#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8805890a 9004
dc6f92b8
JB
9005/* Read events coming from the X server.
9006 This routine is called by the SIGIO handler.
9007 We return as soon as there are no more events to be read.
9008
9009 Events representing keys are stored in buffer BUFP,
9010 which can hold up to NUMCHARS characters.
9011 We return the number of characters stored into the buffer,
9012 thus pretending to be `read'.
9013
dc6f92b8
JB
9014 EXPECTED is nonzero if the caller knows input is available. */
9015
7c5283e4 9016int
f66868ba 9017XTread_socket (sd, bufp, numchars, expected)
dc6f92b8 9018 register int sd;
8805890a
KH
9019 /* register */ struct input_event *bufp;
9020 /* register */ int numchars;
dc6f92b8
JB
9021 int expected;
9022{
9023 int count = 0;
9024 int nbytes = 0;
dc6f92b8 9025 XEvent event;
f676886a 9026 struct frame *f;
66f55a9d 9027 int event_found = 0;
334208b7 9028 struct x_display_info *dpyinfo;
dc6f92b8 9029
9ac0d9e0 9030 if (interrupt_input_blocked)
dc6f92b8 9031 {
9ac0d9e0 9032 interrupt_input_pending = 1;
dc6f92b8
JB
9033 return -1;
9034 }
9035
9ac0d9e0 9036 interrupt_input_pending = 0;
dc6f92b8 9037 BLOCK_INPUT;
c0a04927
RS
9038
9039 /* So people can tell when we have read the available input. */
9040 input_signal_count++;
9041
dc6f92b8 9042 if (numchars <= 0)
06a2c219 9043 abort (); /* Don't think this happens. */
dc6f92b8 9044
bde5503b
GM
9045 ++handling_signal;
9046
7a13e894
RS
9047 /* Find the display we are supposed to read input for.
9048 It's the one communicating on descriptor SD. */
9049 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9050 {
9051#if 0 /* This ought to be unnecessary; let's verify it. */
dc6f92b8 9052#ifdef FIOSNBIO
7a13e894
RS
9053 /* If available, Xlib uses FIOSNBIO to make the socket
9054 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
e6cbea31 9055 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
06a2c219 9056 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7a13e894 9057 fcntl (dpyinfo->connection, F_SETFL, 0);
c118dd06 9058#endif /* ! defined (FIOSNBIO) */
7a13e894 9059#endif
dc6f92b8 9060
7a13e894
RS
9061#if 0 /* This code can't be made to work, with multiple displays,
9062 and appears not to be used on any system any more.
9063 Also keyboard.c doesn't turn O_NDELAY on and off
9064 for X connections. */
dc6f92b8
JB
9065#ifndef SIGIO
9066#ifndef HAVE_SELECT
7a13e894
RS
9067 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9068 {
9069 extern int read_alarm_should_throw;
9070 read_alarm_should_throw = 1;
9071 XPeekEvent (dpyinfo->display, &event);
9072 read_alarm_should_throw = 0;
9073 }
c118dd06
JB
9074#endif /* HAVE_SELECT */
9075#endif /* SIGIO */
7a13e894 9076#endif
dc6f92b8 9077
7a13e894
RS
9078 /* For debugging, this gives a way to fake an I/O error. */
9079 if (dpyinfo == XTread_socket_fake_io_error)
9080 {
9081 XTread_socket_fake_io_error = 0;
9082 x_io_error_quitter (dpyinfo->display);
9083 }
dc6f92b8 9084
06a2c219 9085 while (XPending (dpyinfo->display))
dc6f92b8 9086 {
7a13e894 9087 XNextEvent (dpyinfo->display, &event);
06a2c219 9088
531483fb 9089#ifdef HAVE_X_I18N
d1bc4182 9090 {
f2be1146
GM
9091 /* Filter events for the current X input method.
9092 XFilterEvent returns non-zero if the input method has
9093 consumed the event. We pass the frame's X window to
9094 XFilterEvent because that's the one for which the IC
9095 was created. */
f5d11644
GM
9096 struct frame *f1 = x_any_window_to_frame (dpyinfo,
9097 event.xclient.window);
9098 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
d1bc4182
RS
9099 break;
9100 }
0cd6403b 9101#endif
7a13e894
RS
9102 event_found = 1;
9103
9104 switch (event.type)
9105 {
9106 case ClientMessage:
c047688c 9107 {
7a13e894
RS
9108 if (event.xclient.message_type
9109 == dpyinfo->Xatom_wm_protocols
9110 && event.xclient.format == 32)
c047688c 9111 {
7a13e894
RS
9112 if (event.xclient.data.l[0]
9113 == dpyinfo->Xatom_wm_take_focus)
c047688c 9114 {
8c1a6a84
RS
9115 /* Use x_any_window_to_frame because this
9116 could be the shell widget window
9117 if the frame has no title bar. */
9118 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6c183ba5
RS
9119#ifdef HAVE_X_I18N
9120 /* Not quite sure this is needed -pd */
8c1a6a84 9121 if (f && FRAME_XIC (f))
6c183ba5
RS
9122 XSetICFocus (FRAME_XIC (f));
9123#endif
f1da8f06
GM
9124#if 0 /* Emacs sets WM hints whose `input' field is `true'. This
9125 instructs the WM to set the input focus automatically for
9126 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
9127 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
9128 it has set the focus. So, XSetInputFocus below is not
9129 needed.
9130
9131 The call to XSetInputFocus below has also caused trouble. In
9132 cases where the XSetInputFocus done by the WM and the one
9133 below are temporally close (on a fast machine), the call
9134 below can generate additional FocusIn events which confuse
9135 Emacs. */
9136
bf7253f4
RS
9137 /* Since we set WM_TAKE_FOCUS, we must call
9138 XSetInputFocus explicitly. But not if f is null,
9139 since that might be an event for a deleted frame. */
7a13e894 9140 if (f)
bf7253f4
RS
9141 {
9142 Display *d = event.xclient.display;
9143 /* Catch and ignore errors, in case window has been
9144 iconified by a window manager such as GWM. */
9145 int count = x_catch_errors (d);
9146 XSetInputFocus (d, event.xclient.window,
e1f6572f
RS
9147 /* The ICCCM says this is
9148 the only valid choice. */
9149 RevertToParent,
bf7253f4
RS
9150 event.xclient.data.l[1]);
9151 /* This is needed to detect the error
9152 if there is an error. */
9153 XSync (d, False);
9154 x_uncatch_errors (d, count);
9155 }
7a13e894 9156 /* Not certain about handling scroll bars here */
f1da8f06 9157#endif /* 0 */
c047688c 9158 }
7a13e894
RS
9159 else if (event.xclient.data.l[0]
9160 == dpyinfo->Xatom_wm_save_yourself)
9161 {
9162 /* Save state modify the WM_COMMAND property to
06a2c219 9163 something which can reinstate us. This notifies
7a13e894
RS
9164 the session manager, who's looking for such a
9165 PropertyNotify. Can restart processing when
06a2c219 9166 a keyboard or mouse event arrives. */
7a13e894
RS
9167 if (numchars > 0)
9168 {
19126e11
KH
9169 f = x_top_window_to_frame (dpyinfo,
9170 event.xclient.window);
7a13e894
RS
9171
9172 /* This is just so we only give real data once
9173 for a single Emacs process. */
b86bd3dd 9174 if (f == SELECTED_FRAME ())
7a13e894
RS
9175 XSetCommand (FRAME_X_DISPLAY (f),
9176 event.xclient.window,
9177 initial_argv, initial_argc);
f000f5c5 9178 else if (f)
7a13e894
RS
9179 XSetCommand (FRAME_X_DISPLAY (f),
9180 event.xclient.window,
9181 0, 0);
9182 }
9183 }
9184 else if (event.xclient.data.l[0]
9185 == dpyinfo->Xatom_wm_delete_window)
1fb20991 9186 {
19126e11
KH
9187 struct frame *f
9188 = x_any_window_to_frame (dpyinfo,
9189 event.xclient.window);
1fb20991 9190
7a13e894
RS
9191 if (f)
9192 {
9193 if (numchars == 0)
9194 abort ();
1fb20991 9195
7a13e894
RS
9196 bufp->kind = delete_window_event;
9197 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9198 bufp->arg = Qnil;
7a13e894
RS
9199 bufp++;
9200
9201 count += 1;
9202 numchars -= 1;
9203 }
1fb20991 9204 }
c047688c 9205 }
7a13e894
RS
9206 else if (event.xclient.message_type
9207 == dpyinfo->Xatom_wm_configure_denied)
9208 {
9209 }
9210 else if (event.xclient.message_type
9211 == dpyinfo->Xatom_wm_window_moved)
9212 {
9213 int new_x, new_y;
19126e11
KH
9214 struct frame *f
9215 = x_window_to_frame (dpyinfo, event.xclient.window);
58769bee 9216
7a13e894
RS
9217 new_x = event.xclient.data.s[0];
9218 new_y = event.xclient.data.s[1];
1fb20991 9219
7a13e894
RS
9220 if (f)
9221 {
7556890b
RS
9222 f->output_data.x->left_pos = new_x;
9223 f->output_data.x->top_pos = new_y;
7a13e894 9224 }
1fb20991 9225 }
0fdff6bb 9226#ifdef HACK_EDITRES
7a13e894
RS
9227 else if (event.xclient.message_type
9228 == dpyinfo->Xatom_editres)
9229 {
19126e11
KH
9230 struct frame *f
9231 = x_any_window_to_frame (dpyinfo, event.xclient.window);
7556890b 9232 _XEditResCheckMessages (f->output_data.x->widget, NULL,
19126e11 9233 &event, NULL);
7a13e894 9234 }
0fdff6bb 9235#endif /* HACK_EDITRES */
06a2c219
GM
9236 else if ((event.xclient.message_type
9237 == dpyinfo->Xatom_DONE)
9238 || (event.xclient.message_type
9239 == dpyinfo->Xatom_PAGE))
9240 {
9241 /* Ghostview job completed. Kill it. We could
9242 reply with "Next" if we received "Page", but we
9243 currently never do because we are interested in
9244 images, only, which should have 1 page. */
06a2c219
GM
9245 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9246 struct frame *f
9247 = x_window_to_frame (dpyinfo, event.xclient.window);
9248 x_kill_gs_process (pixmap, f);
9249 expose_frame (f, 0, 0, 0, 0);
9250 }
9251#ifdef USE_TOOLKIT_SCROLL_BARS
9252 /* Scroll bar callbacks send a ClientMessage from which
9253 we construct an input_event. */
9254 else if (event.xclient.message_type
9255 == dpyinfo->Xatom_Scrollbar)
9256 {
9257 x_scroll_bar_to_input_event (&event, bufp);
9258 ++bufp, ++count, --numchars;
9259 goto out;
9260 }
9261#endif /* USE_TOOLKIT_SCROLL_BARS */
9262 else
9263 goto OTHER;
7a13e894
RS
9264 }
9265 break;
dc6f92b8 9266
7a13e894 9267 case SelectionNotify:
3afe33e7 9268#ifdef USE_X_TOOLKIT
19126e11 9269 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
7a13e894 9270 goto OTHER;
3afe33e7 9271#endif /* not USE_X_TOOLKIT */
dfcf069d 9272 x_handle_selection_notify (&event.xselection);
7a13e894 9273 break;
d56a553a 9274
06a2c219 9275 case SelectionClear: /* Someone has grabbed ownership. */
3afe33e7 9276#ifdef USE_X_TOOLKIT
19126e11 9277 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
7a13e894 9278 goto OTHER;
3afe33e7 9279#endif /* USE_X_TOOLKIT */
7a13e894
RS
9280 {
9281 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
d56a553a 9282
7a13e894
RS
9283 if (numchars == 0)
9284 abort ();
d56a553a 9285
7a13e894
RS
9286 bufp->kind = selection_clear_event;
9287 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9288 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9289 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9290 bufp->frame_or_window = Qnil;
0f8aabe9 9291 bufp->arg = Qnil;
7a13e894 9292 bufp++;
d56a553a 9293
7a13e894
RS
9294 count += 1;
9295 numchars -= 1;
9296 }
9297 break;
dc6f92b8 9298
06a2c219 9299 case SelectionRequest: /* Someone wants our selection. */
3afe33e7 9300#ifdef USE_X_TOOLKIT
19126e11 9301 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
7a13e894 9302 goto OTHER;
3afe33e7 9303#endif /* USE_X_TOOLKIT */
7a13e894 9304 if (x_queue_selection_requests)
19126e11 9305 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
7a13e894
RS
9306 &event);
9307 else
9308 {
9309 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
dc6f92b8 9310
7a13e894
RS
9311 if (numchars == 0)
9312 abort ();
9313
9314 bufp->kind = selection_request_event;
9315 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9316 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9317 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9318 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9319 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9320 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9321 bufp->frame_or_window = Qnil;
0f8aabe9 9322 bufp->arg = Qnil;
7a13e894
RS
9323 bufp++;
9324
9325 count += 1;
9326 numchars -= 1;
9327 }
9328 break;
9329
9330 case PropertyNotify:
3afe33e7 9331#ifdef USE_X_TOOLKIT
19126e11 9332 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
7a13e894 9333 goto OTHER;
3afe33e7 9334#endif /* not USE_X_TOOLKIT */
dfcf069d 9335 x_handle_property_notify (&event.xproperty);
7a13e894 9336 break;
dc6f92b8 9337
7a13e894 9338 case ReparentNotify:
19126e11 9339 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
7a13e894
RS
9340 if (f)
9341 {
9342 int x, y;
7556890b 9343 f->output_data.x->parent_desc = event.xreparent.parent;
7a13e894 9344 x_real_positions (f, &x, &y);
7556890b
RS
9345 f->output_data.x->left_pos = x;
9346 f->output_data.x->top_pos = y;
7a13e894
RS
9347 }
9348 break;
3bd330d4 9349
7a13e894 9350 case Expose:
19126e11 9351 f = x_window_to_frame (dpyinfo, event.xexpose.window);
7a13e894 9352 if (f)
dc6f92b8 9353 {
7a13e894
RS
9354 if (f->async_visible == 0)
9355 {
9356 f->async_visible = 1;
9357 f->async_iconified = 0;
06c488fd 9358 f->output_data.x->has_been_visible = 1;
7a13e894
RS
9359 SET_FRAME_GARBAGED (f);
9360 }
9361 else
06a2c219
GM
9362 expose_frame (x_window_to_frame (dpyinfo,
9363 event.xexpose.window),
9364 event.xexpose.x, event.xexpose.y,
9365 event.xexpose.width, event.xexpose.height);
dc6f92b8
JB
9366 }
9367 else
7a13e894 9368 {
06a2c219
GM
9369#ifdef USE_TOOLKIT_SCROLL_BARS
9370 /* Dispatch event to the widget. */
9371 goto OTHER;
9372#else /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9373 struct scroll_bar *bar
9374 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 9375
7a13e894
RS
9376 if (bar)
9377 x_scroll_bar_expose (bar, &event);
3afe33e7 9378#ifdef USE_X_TOOLKIT
7a13e894
RS
9379 else
9380 goto OTHER;
3afe33e7 9381#endif /* USE_X_TOOLKIT */
06a2c219 9382#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9383 }
9384 break;
dc6f92b8 9385
7a13e894
RS
9386 case GraphicsExpose: /* This occurs when an XCopyArea's
9387 source area was obscured or not
9388 available.*/
19126e11 9389 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
7a13e894
RS
9390 if (f)
9391 {
06a2c219
GM
9392 expose_frame (f,
9393 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9394 event.xgraphicsexpose.width,
9395 event.xgraphicsexpose.height);
7a13e894 9396 }
3afe33e7 9397#ifdef USE_X_TOOLKIT
7a13e894
RS
9398 else
9399 goto OTHER;
3afe33e7 9400#endif /* USE_X_TOOLKIT */
7a13e894 9401 break;
dc6f92b8 9402
7a13e894 9403 case NoExpose: /* This occurs when an XCopyArea's
06a2c219
GM
9404 source area was completely
9405 available */
7a13e894 9406 break;
dc6f92b8 9407
7a13e894 9408 case UnmapNotify:
06a2c219
GM
9409 /* Redo the mouse-highlight after the tooltip has gone. */
9410 if (event.xmap.window == tip_window)
9411 {
9412 tip_window = 0;
9413 redo_mouse_highlight ();
9414 }
9415
91ea2a7a 9416 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
7a13e894
RS
9417 if (f) /* F may no longer exist if
9418 the frame was deleted. */
9419 {
9420 /* While a frame is unmapped, display generation is
9421 disabled; you don't want to spend time updating a
9422 display that won't ever be seen. */
9423 f->async_visible = 0;
9424 /* We can't distinguish, from the event, whether the window
9425 has become iconified or invisible. So assume, if it
9426 was previously visible, than now it is iconified.
1aa6072f
RS
9427 But x_make_frame_invisible clears both
9428 the visible flag and the iconified flag;
9429 and that way, we know the window is not iconified now. */
7a13e894 9430 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
1aa6072f
RS
9431 {
9432 f->async_iconified = 1;
bddd097c 9433
1aa6072f
RS
9434 bufp->kind = iconify_event;
9435 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9436 bufp->arg = Qnil;
1aa6072f
RS
9437 bufp++;
9438 count++;
9439 numchars--;
9440 }
7a13e894 9441 }
7a13e894 9442 goto OTHER;
dc6f92b8 9443
7a13e894 9444 case MapNotify:
06a2c219
GM
9445 if (event.xmap.window == tip_window)
9446 /* The tooltip has been drawn already. Avoid
9447 the SET_FRAME_GARBAGED below. */
9448 goto OTHER;
9449
9450 /* We use x_top_window_to_frame because map events can
9451 come for sub-windows and they don't mean that the
9452 frame is visible. */
19126e11 9453 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
7a13e894
RS
9454 if (f)
9455 {
9456 f->async_visible = 1;
9457 f->async_iconified = 0;
06c488fd 9458 f->output_data.x->has_been_visible = 1;
dc6f92b8 9459
7a13e894
RS
9460 /* wait_reading_process_input will notice this and update
9461 the frame's display structures. */
9462 SET_FRAME_GARBAGED (f);
bddd097c 9463
d806e720
RS
9464 if (f->iconified)
9465 {
9466 bufp->kind = deiconify_event;
9467 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9468 bufp->arg = Qnil;
d806e720
RS
9469 bufp++;
9470 count++;
9471 numchars--;
9472 }
e73ec6fa 9473 else if (! NILP (Vframe_list)
8e713be6 9474 && ! NILP (XCDR (Vframe_list)))
78aa2ba5
KH
9475 /* Force a redisplay sooner or later
9476 to update the frame titles
9477 in case this is the second frame. */
9478 record_asynch_buffer_change ();
7a13e894 9479 }
7a13e894 9480 goto OTHER;
dc6f92b8 9481
7a13e894 9482 case KeyPress:
19126e11 9483 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
f451eb13 9484
06a2c219
GM
9485#ifdef USE_MOTIF
9486 /* I couldn't find a way to prevent LessTif scroll bars
9487 from consuming key events. */
9488 if (f == 0)
9489 {
9490 Widget widget = XtWindowToWidget (dpyinfo->display,
9491 event.xkey.window);
9492 if (widget && XmIsScrollBar (widget))
9493 {
9494 widget = XtParent (widget);
9495 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9496 }
9497 }
9498#endif /* USE_MOTIF */
9499
7a13e894
RS
9500 if (f != 0)
9501 {
9502 KeySym keysym, orig_keysym;
9503 /* al%imercury@uunet.uu.net says that making this 81 instead of
9504 80 fixed a bug whereby meta chars made his Emacs hang. */
9505 unsigned char copy_buffer[81];
9506 int modifiers;
64bb1782 9507
7a13e894
RS
9508 event.xkey.state
9509 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9510 extra_keyboard_modifiers);
9511 modifiers = event.xkey.state;
3a2712f9 9512
7a13e894 9513 /* This will have to go some day... */
752a043f 9514
7a13e894
RS
9515 /* make_lispy_event turns chars into control chars.
9516 Don't do it here because XLookupString is too eager. */
9517 event.xkey.state &= ~ControlMask;
5d46f928
RS
9518 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9519 | dpyinfo->super_mod_mask
9520 | dpyinfo->hyper_mod_mask
9521 | dpyinfo->alt_mod_mask);
9522
1cf4a0d1
RS
9523 /* In case Meta is ComposeCharacter,
9524 clear its status. According to Markus Ehrnsperger
9525 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9526 this enables ComposeCharacter to work whether or
9527 not it is combined with Meta. */
9528 if (modifiers & dpyinfo->meta_mod_mask)
9529 bzero (&compose_status, sizeof (compose_status));
9530
6c183ba5
RS
9531#ifdef HAVE_X_I18N
9532 if (FRAME_XIC (f))
9533 {
f5d11644
GM
9534 unsigned char *copy_bufptr = copy_buffer;
9535 int copy_bufsiz = sizeof (copy_buffer);
9536 Status status_return;
9537
6c183ba5 9538 nbytes = XmbLookupString (FRAME_XIC (f),
f5d11644
GM
9539 &event.xkey, copy_bufptr,
9540 copy_bufsiz, &keysym,
6c183ba5 9541 &status_return);
f5d11644
GM
9542 if (status_return == XBufferOverflow)
9543 {
9544 copy_bufsiz = nbytes + 1;
9545 copy_bufptr = (char *) alloca (copy_bufsiz);
9546 nbytes = XmbLookupString (FRAME_XIC (f),
9547 &event.xkey, copy_bufptr,
9548 copy_bufsiz, &keysym,
9549 &status_return);
9550 }
9551
1decb680
PE
9552 if (status_return == XLookupNone)
9553 break;
9554 else if (status_return == XLookupChars)
fdd9d55e
GM
9555 {
9556 keysym = NoSymbol;
9557 modifiers = 0;
9558 }
1decb680
PE
9559 else if (status_return != XLookupKeySym
9560 && status_return != XLookupBoth)
9561 abort ();
6c183ba5
RS
9562 }
9563 else
9564 nbytes = XLookupString (&event.xkey, copy_buffer,
9565 80, &keysym, &compose_status);
9566#else
0299d313
RS
9567 nbytes = XLookupString (&event.xkey, copy_buffer,
9568 80, &keysym, &compose_status);
6c183ba5 9569#endif
dc6f92b8 9570
7a13e894 9571 orig_keysym = keysym;
55123275 9572
7a13e894
RS
9573 if (numchars > 1)
9574 {
9575 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9576 || keysym == XK_Delete
1097aea0 9577#ifdef XK_ISO_Left_Tab
441affdb 9578 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
1097aea0 9579#endif
852bff8f 9580 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
7a13e894
RS
9581 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9582 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
c34790e0 9583#ifdef HPUX
7a13e894
RS
9584 /* This recognizes the "extended function keys".
9585 It seems there's no cleaner way.
9586 Test IsModifierKey to avoid handling mode_switch
9587 incorrectly. */
9588 || ((unsigned) (keysym) >= XK_Select
9589 && (unsigned)(keysym) < XK_KP_Space)
69388238
RS
9590#endif
9591#ifdef XK_dead_circumflex
7a13e894 9592 || orig_keysym == XK_dead_circumflex
69388238
RS
9593#endif
9594#ifdef XK_dead_grave
7a13e894 9595 || orig_keysym == XK_dead_grave
69388238
RS
9596#endif
9597#ifdef XK_dead_tilde
7a13e894 9598 || orig_keysym == XK_dead_tilde
69388238
RS
9599#endif
9600#ifdef XK_dead_diaeresis
7a13e894 9601 || orig_keysym == XK_dead_diaeresis
69388238
RS
9602#endif
9603#ifdef XK_dead_macron
7a13e894 9604 || orig_keysym == XK_dead_macron
69388238
RS
9605#endif
9606#ifdef XK_dead_degree
7a13e894 9607 || orig_keysym == XK_dead_degree
69388238
RS
9608#endif
9609#ifdef XK_dead_acute
7a13e894 9610 || orig_keysym == XK_dead_acute
69388238
RS
9611#endif
9612#ifdef XK_dead_cedilla
7a13e894 9613 || orig_keysym == XK_dead_cedilla
69388238
RS
9614#endif
9615#ifdef XK_dead_breve
7a13e894 9616 || orig_keysym == XK_dead_breve
69388238
RS
9617#endif
9618#ifdef XK_dead_ogonek
7a13e894 9619 || orig_keysym == XK_dead_ogonek
69388238
RS
9620#endif
9621#ifdef XK_dead_caron
7a13e894 9622 || orig_keysym == XK_dead_caron
69388238
RS
9623#endif
9624#ifdef XK_dead_doubleacute
7a13e894 9625 || orig_keysym == XK_dead_doubleacute
69388238
RS
9626#endif
9627#ifdef XK_dead_abovedot
7a13e894 9628 || orig_keysym == XK_dead_abovedot
c34790e0 9629#endif
7a13e894
RS
9630 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9631 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9632 /* Any "vendor-specific" key is ok. */
9633 || (orig_keysym & (1 << 28)))
9634 && ! (IsModifierKey (orig_keysym)
7719aa06
RS
9635#ifndef HAVE_X11R5
9636#ifdef XK_Mode_switch
7a13e894 9637 || ((unsigned)(orig_keysym) == XK_Mode_switch)
7719aa06
RS
9638#endif
9639#ifdef XK_Num_Lock
7a13e894 9640 || ((unsigned)(orig_keysym) == XK_Num_Lock)
7719aa06
RS
9641#endif
9642#endif /* not HAVE_X11R5 */
7a13e894 9643 ))
dc6f92b8 9644 {
10e6549c
RS
9645 if (temp_index == sizeof temp_buffer / sizeof (short))
9646 temp_index = 0;
7a13e894
RS
9647 temp_buffer[temp_index++] = keysym;
9648 bufp->kind = non_ascii_keystroke;
9649 bufp->code = keysym;
e0c1aef2 9650 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9651 bufp->arg = Qnil;
334208b7
RS
9652 bufp->modifiers
9653 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9654 modifiers);
1113d9db 9655 bufp->timestamp = event.xkey.time;
dc6f92b8 9656 bufp++;
7a13e894
RS
9657 count++;
9658 numchars--;
dc6f92b8 9659 }
7a13e894
RS
9660 else if (numchars > nbytes)
9661 {
9662 register int i;
9663
9664 for (i = 0; i < nbytes; i++)
9665 {
9666 if (temp_index == sizeof temp_buffer / sizeof (short))
9667 temp_index = 0;
9668 temp_buffer[temp_index++] = copy_buffer[i];
9669 bufp->kind = ascii_keystroke;
9670 bufp->code = copy_buffer[i];
9671 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9672 bufp->arg = Qnil;
7a13e894
RS
9673 bufp->modifiers
9674 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9675 modifiers);
9676 bufp->timestamp = event.xkey.time;
9677 bufp++;
9678 }
9679
9680 count += nbytes;
9681 numchars -= nbytes;
1decb680
PE
9682
9683 if (keysym == NoSymbol)
9684 break;
7a13e894
RS
9685 }
9686 else
9687 abort ();
dc6f92b8 9688 }
10e6549c
RS
9689 else
9690 abort ();
dc6f92b8 9691 }
59ddecde
GM
9692#ifdef HAVE_X_I18N
9693 /* Don't dispatch this event since XtDispatchEvent calls
9694 XFilterEvent, and two calls in a row may freeze the
9695 client. */
9696 break;
9697#else
717ca130 9698 goto OTHER;
59ddecde 9699#endif
f451eb13 9700
f5d11644 9701 case KeyRelease:
59ddecde
GM
9702#ifdef HAVE_X_I18N
9703 /* Don't dispatch this event since XtDispatchEvent calls
9704 XFilterEvent, and two calls in a row may freeze the
9705 client. */
9706 break;
9707#else
f5d11644 9708 goto OTHER;
59ddecde 9709#endif
f5d11644 9710
7a13e894 9711 /* Here's a possible interpretation of the whole
06a2c219
GM
9712 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
9713 you get a FocusIn event, you have to get a FocusOut
9714 event before you relinquish the focus. If you
9715 haven't received a FocusIn event, then a mere
9716 LeaveNotify is enough to free you. */
f451eb13 9717
7a13e894 9718 case EnterNotify:
06a2c219
GM
9719 {
9720 int from_menu_bar_p = 0;
9721
9722 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
9723
9724#ifdef LESSTIF_VERSION
9725 /* When clicking outside of a menu bar popup to close
9726 it, we get a FocusIn/ EnterNotify sequence of
9727 events. The flag event.xcrossing.focus is not set
9728 in the EnterNotify event of that sequence because
9729 the focus is in the menu bar,
9730 event.xcrossing.window is the frame's X window.
9731 Unconditionally setting the focus frame to null in
9732 this case is not the right thing, because no event
9733 follows that could set the focus frame to the right
9734 value.
9735
9736 This could be a LessTif bug, but I wasn't able to
9737 reproduce the behavior in a simple test program.
9738
9739 (gerd, LessTif 0.88.1). */
9740
9741 if (!event.xcrossing.focus
9742 && f
9743 && f->output_data.x->menubar_widget)
9744 {
9745 Window focus;
9746 int revert;
9747
9748 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9749 if (focus == XtWindow (f->output_data.x->menubar_widget))
9750 from_menu_bar_p = 1;
9751 }
9752#endif /* LESSTIF_VERSION */
6d4238f3 9753
06a2c219
GM
9754 if (event.xcrossing.focus || from_menu_bar_p)
9755 {
9756 /* Avoid nasty pop/raise loops. */
9757 if (f && (!(f->auto_raise)
9758 || !(f->auto_lower)
9759 || (event.xcrossing.time - enter_timestamp) > 500))
9760 {
9761 x_new_focus_frame (dpyinfo, f);
9762 enter_timestamp = event.xcrossing.time;
9763 }
9764 }
9765 else if (f == dpyinfo->x_focus_frame)
9766 x_new_focus_frame (dpyinfo, 0);
9767
9768 /* EnterNotify counts as mouse movement,
9769 so update things that depend on mouse position. */
9770 if (f && !f->output_data.x->busy_p)
9771 note_mouse_movement (f, &event.xmotion);
9772 goto OTHER;
9773 }
dc6f92b8 9774
7a13e894 9775 case FocusIn:
19126e11 9776 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 9777 if (event.xfocus.detail != NotifyPointer)
0f941935 9778 dpyinfo->x_focus_event_frame = f;
7a13e894 9779 if (f)
eb72635f
GM
9780 {
9781 x_new_focus_frame (dpyinfo, f);
9782
9783 /* Don't stop displaying the initial startup message
9784 for a switch-frame event we don't need. */
9785 if (GC_NILP (Vterminal_frame)
9786 && GC_CONSP (Vframe_list)
9787 && !GC_NILP (XCDR (Vframe_list)))
9788 {
9789 bufp->kind = FOCUS_IN_EVENT;
9790 XSETFRAME (bufp->frame_or_window, f);
0f8aabe9 9791 bufp->arg = Qnil;
eb72635f
GM
9792 ++bufp, ++count, --numchars;
9793 }
9794 }
f9e24cb9 9795
6c183ba5
RS
9796#ifdef HAVE_X_I18N
9797 if (f && FRAME_XIC (f))
9798 XSetICFocus (FRAME_XIC (f));
9799#endif
9800
7a13e894 9801 goto OTHER;
10c5e63d 9802
7a13e894 9803 case LeaveNotify:
19126e11 9804 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
7a13e894 9805 if (f)
10c5e63d 9806 {
06a2c219
GM
9807 Lisp_Object frame;
9808 int from_menu_bar_p = 0;
9809
7a13e894 9810 if (f == dpyinfo->mouse_face_mouse_frame)
06a2c219
GM
9811 {
9812 /* If we move outside the frame, then we're
9813 certainly no longer on any text in the frame. */
9814 clear_mouse_face (dpyinfo);
9815 dpyinfo->mouse_face_mouse_frame = 0;
9816 }
9817
9818 /* Generate a nil HELP_EVENT to cancel a help-echo.
9819 Do it only if there's something to cancel.
9820 Otherwise, the startup message is cleared when
9821 the mouse leaves the frame. */
9822 if (any_help_event_p)
9823 {
be010514
GM
9824 Lisp_Object frame;
9825 int n;
9826
06a2c219 9827 XSETFRAME (frame, f);
7cea38bc 9828 n = gen_help_event (bufp, Qnil, frame, Qnil, Qnil, 0);
be010514 9829 bufp += n, count += n, numchars -= n;
06a2c219 9830 }
7a13e894 9831
06a2c219
GM
9832#ifdef LESSTIF_VERSION
9833 /* Please see the comment at the start of the
9834 EnterNotify case. */
9835 if (!event.xcrossing.focus
9836 && f->output_data.x->menubar_widget)
9837 {
9838 Window focus;
9839 int revert;
9840 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9841 if (focus == XtWindow (f->output_data.x->menubar_widget))
9842 from_menu_bar_p = 1;
9843 }
9844#endif /* LESSTIF_VERSION */
9845
9846 if (event.xcrossing.focus || from_menu_bar_p)
0f941935 9847 x_mouse_leave (dpyinfo);
10c5e63d 9848 else
7a13e894 9849 {
0f941935
KH
9850 if (f == dpyinfo->x_focus_event_frame)
9851 dpyinfo->x_focus_event_frame = 0;
9852 if (f == dpyinfo->x_focus_frame)
9853 x_new_focus_frame (dpyinfo, 0);
7a13e894 9854 }
10c5e63d 9855 }
7a13e894 9856 goto OTHER;
dc6f92b8 9857
7a13e894 9858 case FocusOut:
19126e11 9859 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 9860 if (event.xfocus.detail != NotifyPointer
0f941935
KH
9861 && f == dpyinfo->x_focus_event_frame)
9862 dpyinfo->x_focus_event_frame = 0;
9863 if (f && f == dpyinfo->x_focus_frame)
9864 x_new_focus_frame (dpyinfo, 0);
f9e24cb9 9865
6c183ba5
RS
9866#ifdef HAVE_X_I18N
9867 if (f && FRAME_XIC (f))
9868 XUnsetICFocus (FRAME_XIC (f));
9869#endif
9870
7a13e894 9871 goto OTHER;
dc6f92b8 9872
7a13e894 9873 case MotionNotify:
dc6f92b8 9874 {
06a2c219 9875 previous_help_echo = help_echo;
7cea38bc 9876 help_echo = help_echo_object = help_echo_window = Qnil;
be010514 9877 help_echo_pos = -1;
06a2c219 9878
7a13e894
RS
9879 if (dpyinfo->grabbed && last_mouse_frame
9880 && FRAME_LIVE_P (last_mouse_frame))
9881 f = last_mouse_frame;
9882 else
19126e11 9883 f = x_window_to_frame (dpyinfo, event.xmotion.window);
06a2c219 9884
7a13e894
RS
9885 if (f)
9886 note_mouse_movement (f, &event.xmotion);
9887 else
9888 {
e88b3c50 9889#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
9890 struct scroll_bar *bar
9891 = x_window_to_scroll_bar (event.xmotion.window);
f451eb13 9892
7a13e894
RS
9893 if (bar)
9894 x_scroll_bar_note_movement (bar, &event);
e88b3c50 9895#endif /* USE_TOOLKIT_SCROLL_BARS */
b8009dd1 9896
06a2c219
GM
9897 /* If we move outside the frame, then we're
9898 certainly no longer on any text in the frame. */
7a13e894
RS
9899 clear_mouse_face (dpyinfo);
9900 }
06a2c219
GM
9901
9902 /* If the contents of the global variable help_echo
9903 has changed, generate a HELP_EVENT. */
b7e80413
SM
9904 if (!NILP (help_echo)
9905 || !NILP (previous_help_echo))
06a2c219
GM
9906 {
9907 Lisp_Object frame;
be010514 9908 int n;
06a2c219
GM
9909
9910 if (f)
9911 XSETFRAME (frame, f);
9912 else
9913 frame = Qnil;
9914
9915 any_help_event_p = 1;
be010514 9916 n = gen_help_event (bufp, help_echo, frame,
7cea38bc
GM
9917 help_echo_window, help_echo_object,
9918 help_echo_pos);
be010514 9919 bufp += n, count += n, numchars -= n;
06a2c219
GM
9920 }
9921
9922 goto OTHER;
dc6f92b8 9923 }
dc6f92b8 9924
7a13e894 9925 case ConfigureNotify:
9829ddba
RS
9926 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
9927 if (f)
af395ec1 9928 {
5c187dee 9929#ifndef USE_X_TOOLKIT
bf1b7b30
KH
9930 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
9931 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
5c187dee 9932
2d7fc7e8
RS
9933 /* In the toolkit version, change_frame_size
9934 is called by the code that handles resizing
9935 of the EmacsFrame widget. */
7a13e894 9936
7a13e894
RS
9937 /* Even if the number of character rows and columns has
9938 not changed, the font size may have changed, so we need
9939 to check the pixel dimensions as well. */
9940 if (columns != f->width
9941 || rows != f->height
7556890b
RS
9942 || event.xconfigure.width != f->output_data.x->pixel_width
9943 || event.xconfigure.height != f->output_data.x->pixel_height)
7a13e894 9944 {
7d1e984f 9945 change_frame_size (f, rows, columns, 0, 1, 0);
7a13e894 9946 SET_FRAME_GARBAGED (f);
e687d06e 9947 cancel_mouse_face (f);
7a13e894 9948 }
2d7fc7e8 9949#endif
af395ec1 9950
7556890b
RS
9951 f->output_data.x->pixel_width = event.xconfigure.width;
9952 f->output_data.x->pixel_height = event.xconfigure.height;
7a13e894
RS
9953
9954 /* What we have now is the position of Emacs's own window.
9955 Convert that to the position of the window manager window. */
dcb07ae9
RS
9956 x_real_positions (f, &f->output_data.x->left_pos,
9957 &f->output_data.x->top_pos);
9958
f5d11644
GM
9959#ifdef HAVE_X_I18N
9960 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
9961 xic_set_statusarea (f);
9962#endif
9963
dcb07ae9
RS
9964 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
9965 {
9966 /* Since the WM decorations come below top_pos now,
9967 we must put them below top_pos in the future. */
9968 f->output_data.x->win_gravity = NorthWestGravity;
9969 x_wm_set_size_hint (f, (long) 0, 0);
9970 }
8f08dc93
KH
9971#ifdef USE_MOTIF
9972 /* Some window managers pass (0,0) as the location of
9973 the window, and the Motif event handler stores it
9974 in the emacs widget, which messes up Motif menus. */
9975 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
9976 {
9977 event.xconfigure.x = f->output_data.x->widget->core.x;
9978 event.xconfigure.y = f->output_data.x->widget->core.y;
9979 }
06a2c219 9980#endif /* USE_MOTIF */
7a13e894 9981 }
2d7fc7e8 9982 goto OTHER;
dc6f92b8 9983
7a13e894
RS
9984 case ButtonPress:
9985 case ButtonRelease:
9986 {
9987 /* If we decide we want to generate an event to be seen
9988 by the rest of Emacs, we put it here. */
9989 struct input_event emacs_event;
9ea173e8 9990 int tool_bar_p = 0;
06a2c219 9991
7a13e894 9992 emacs_event.kind = no_event;
7a13e894 9993 bzero (&compose_status, sizeof (compose_status));
9b07615b 9994
06a2c219
GM
9995 if (dpyinfo->grabbed
9996 && last_mouse_frame
9f67f20b
RS
9997 && FRAME_LIVE_P (last_mouse_frame))
9998 f = last_mouse_frame;
9999 else
2224b905 10000 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9f67f20b 10001
06a2c219
GM
10002 if (f)
10003 {
9ea173e8
GM
10004 /* Is this in the tool-bar? */
10005 if (WINDOWP (f->tool_bar_window)
10006 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
06a2c219
GM
10007 {
10008 Lisp_Object window;
10009 int p, x, y;
10010
10011 x = event.xbutton.x;
10012 y = event.xbutton.y;
10013
10014 /* Set x and y. */
10015 window = window_from_coordinates (f, x, y, &p, 1);
9ea173e8 10016 if (EQ (window, f->tool_bar_window))
06a2c219 10017 {
9ea173e8
GM
10018 x_handle_tool_bar_click (f, &event.xbutton);
10019 tool_bar_p = 1;
06a2c219
GM
10020 }
10021 }
10022
9ea173e8 10023 if (!tool_bar_p)
06a2c219
GM
10024 if (!dpyinfo->x_focus_frame
10025 || f == dpyinfo->x_focus_frame)
10026 construct_mouse_click (&emacs_event, &event, f);
7a13e894
RS
10027 }
10028 else
10029 {
06a2c219 10030#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
10031 struct scroll_bar *bar
10032 = x_window_to_scroll_bar (event.xbutton.window);
f451eb13 10033
7a13e894
RS
10034 if (bar)
10035 x_scroll_bar_handle_click (bar, &event, &emacs_event);
06a2c219 10036#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
10037 }
10038
10039 if (event.type == ButtonPress)
10040 {
10041 dpyinfo->grabbed |= (1 << event.xbutton.button);
10042 last_mouse_frame = f;
edad46f6
KH
10043 /* Ignore any mouse motion that happened
10044 before this event; any subsequent mouse-movement
10045 Emacs events should reflect only motion after
10046 the ButtonPress. */
a00e91cd
KH
10047 if (f != 0)
10048 f->mouse_moved = 0;
06a2c219 10049
9ea173e8
GM
10050 if (!tool_bar_p)
10051 last_tool_bar_item = -1;
7a13e894 10052 }
3afe33e7
RS
10053 else
10054 {
7a13e894 10055 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
3afe33e7 10056 }
23faf38f 10057
7a13e894
RS
10058 if (numchars >= 1 && emacs_event.kind != no_event)
10059 {
10060 bcopy (&emacs_event, bufp, sizeof (struct input_event));
10061 bufp++;
10062 count++;
10063 numchars--;
10064 }
3afe33e7
RS
10065
10066#ifdef USE_X_TOOLKIT
2224b905
RS
10067 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
10068 /* For a down-event in the menu bar,
10069 don't pass it to Xt right now.
10070 Instead, save it away
10071 and we will pass it to Xt from kbd_buffer_get_event.
10072 That way, we can run some Lisp code first. */
91375f8f
RS
10073 if (f && event.type == ButtonPress
10074 /* Verify the event is really within the menu bar
10075 and not just sent to it due to grabbing. */
10076 && event.xbutton.x >= 0
10077 && event.xbutton.x < f->output_data.x->pixel_width
10078 && event.xbutton.y >= 0
10079 && event.xbutton.y < f->output_data.x->menubar_height
10080 && event.xbutton.same_screen)
2224b905 10081 {
8805890a 10082 SET_SAVED_BUTTON_EVENT;
2237cac9
RS
10083 XSETFRAME (last_mouse_press_frame, f);
10084 }
10085 else if (event.type == ButtonPress)
10086 {
10087 last_mouse_press_frame = Qnil;
30e671c3 10088 goto OTHER;
ce89ef46 10089 }
06a2c219 10090
2237cac9
RS
10091#ifdef USE_MOTIF /* This should do not harm for Lucid,
10092 but I am trying to be cautious. */
ce89ef46
RS
10093 else if (event.type == ButtonRelease)
10094 {
2237cac9 10095 if (!NILP (last_mouse_press_frame))
f10ded1c 10096 {
2237cac9
RS
10097 f = XFRAME (last_mouse_press_frame);
10098 if (f->output_data.x)
06a2c219 10099 SET_SAVED_BUTTON_EVENT;
f10ded1c 10100 }
06a2c219 10101 else
30e671c3 10102 goto OTHER;
2224b905 10103 }
2237cac9 10104#endif /* USE_MOTIF */
2224b905
RS
10105 else
10106 goto OTHER;
3afe33e7 10107#endif /* USE_X_TOOLKIT */
7a13e894
RS
10108 }
10109 break;
dc6f92b8 10110
7a13e894 10111 case CirculateNotify:
06a2c219
GM
10112 goto OTHER;
10113
7a13e894 10114 case CirculateRequest:
06a2c219
GM
10115 goto OTHER;
10116
10117 case VisibilityNotify:
10118 goto OTHER;
dc6f92b8 10119
7a13e894
RS
10120 case MappingNotify:
10121 /* Someone has changed the keyboard mapping - update the
10122 local cache. */
10123 switch (event.xmapping.request)
10124 {
10125 case MappingModifier:
10126 x_find_modifier_meanings (dpyinfo);
10127 /* This is meant to fall through. */
10128 case MappingKeyboard:
10129 XRefreshKeyboardMapping (&event.xmapping);
10130 }
7a13e894 10131 goto OTHER;
dc6f92b8 10132
7a13e894 10133 default:
7a13e894 10134 OTHER:
717ca130 10135#ifdef USE_X_TOOLKIT
7a13e894
RS
10136 BLOCK_INPUT;
10137 XtDispatchEvent (&event);
10138 UNBLOCK_INPUT;
3afe33e7 10139#endif /* USE_X_TOOLKIT */
7a13e894
RS
10140 break;
10141 }
dc6f92b8
JB
10142 }
10143 }
10144
06a2c219
GM
10145 out:;
10146
9a5196d0
RS
10147 /* On some systems, an X bug causes Emacs to get no more events
10148 when the window is destroyed. Detect that. (1994.) */
58769bee 10149 if (! event_found)
ef2a22d0 10150 {
ef2a22d0
RS
10151 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
10152 One XNOOP in 100 loops will make Emacs terminate.
10153 B. Bretthauer, 1994 */
10154 x_noop_count++;
58769bee 10155 if (x_noop_count >= 100)
ef2a22d0
RS
10156 {
10157 x_noop_count=0;
2224b905
RS
10158
10159 if (next_noop_dpyinfo == 0)
10160 next_noop_dpyinfo = x_display_list;
10161
10162 XNoOp (next_noop_dpyinfo->display);
10163
10164 /* Each time we get here, cycle through the displays now open. */
10165 next_noop_dpyinfo = next_noop_dpyinfo->next;
ef2a22d0
RS
10166 }
10167 }
502add23 10168
06a2c219 10169 /* If the focus was just given to an auto-raising frame,
0134a210 10170 raise it now. */
7a13e894 10171 /* ??? This ought to be able to handle more than one such frame. */
0134a210
RS
10172 if (pending_autoraise_frame)
10173 {
10174 x_raise_frame (pending_autoraise_frame);
10175 pending_autoraise_frame = 0;
10176 }
0134a210 10177
dc6f92b8 10178 UNBLOCK_INPUT;
bde5503b 10179 --handling_signal;
dc6f92b8
JB
10180 return count;
10181}
06a2c219
GM
10182
10183
10184
dc6f92b8 10185\f
06a2c219
GM
10186/***********************************************************************
10187 Text Cursor
10188 ***********************************************************************/
10189
10190/* Note if the text cursor of window W has been overwritten by a
10191 drawing operation that outputs N glyphs starting at HPOS in the
10192 line given by output_cursor.vpos. N < 0 means all the rest of the
10193 line after HPOS has been written. */
10194
10195static void
10196note_overwritten_text_cursor (w, hpos, n)
10197 struct window *w;
10198 int hpos, n;
10199{
10200 if (updated_area == TEXT_AREA
10201 && output_cursor.vpos == w->phys_cursor.vpos
10202 && hpos <= w->phys_cursor.hpos
10203 && (n < 0
10204 || hpos + n > w->phys_cursor.hpos))
10205 w->phys_cursor_on_p = 0;
10206}
f451eb13
JB
10207
10208
06a2c219
GM
10209/* Set clipping for output in glyph row ROW. W is the window in which
10210 we operate. GC is the graphics context to set clipping in.
10211 WHOLE_LINE_P non-zero means include the areas used for truncation
10212 mark display and alike in the clipping rectangle.
10213
10214 ROW may be a text row or, e.g., a mode line. Text rows must be
10215 clipped to the interior of the window dedicated to text display,
10216 mode lines must be clipped to the whole window. */
dc6f92b8
JB
10217
10218static void
06a2c219
GM
10219x_clip_to_row (w, row, gc, whole_line_p)
10220 struct window *w;
10221 struct glyph_row *row;
10222 GC gc;
10223 int whole_line_p;
dc6f92b8 10224{
06a2c219
GM
10225 struct frame *f = XFRAME (WINDOW_FRAME (w));
10226 XRectangle clip_rect;
10227 int window_x, window_y, window_width, window_height;
dc6f92b8 10228
06a2c219 10229 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5c1aae96 10230
06a2c219
GM
10231 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10232 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10233 clip_rect.y = max (clip_rect.y, window_y);
10234 clip_rect.width = window_width;
10235 clip_rect.height = row->visible_height;
5c1aae96 10236
06a2c219
GM
10237 /* If clipping to the whole line, including trunc marks, extend
10238 the rectangle to the left and increase its width. */
10239 if (whole_line_p)
10240 {
110859fc
GM
10241 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10242 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
06a2c219 10243 }
5c1aae96 10244
06a2c219 10245 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
dc6f92b8
JB
10246}
10247
06a2c219
GM
10248
10249/* Draw a hollow box cursor on window W in glyph row ROW. */
dc6f92b8
JB
10250
10251static void
06a2c219
GM
10252x_draw_hollow_cursor (w, row)
10253 struct window *w;
10254 struct glyph_row *row;
dc6f92b8 10255{
06a2c219
GM
10256 struct frame *f = XFRAME (WINDOW_FRAME (w));
10257 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10258 Display *dpy = FRAME_X_DISPLAY (f);
10259 int x, y, wd, h;
10260 XGCValues xgcv;
10261 struct glyph *cursor_glyph;
10262 GC gc;
10263
10264 /* Compute frame-relative coordinates from window-relative
10265 coordinates. */
10266 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10267 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10268 + row->ascent - w->phys_cursor_ascent);
10269 h = row->height - 1;
10270
10271 /* Get the glyph the cursor is on. If we can't tell because
10272 the current matrix is invalid or such, give up. */
10273 cursor_glyph = get_phys_cursor_glyph (w);
10274 if (cursor_glyph == NULL)
dc6f92b8
JB
10275 return;
10276
06a2c219
GM
10277 /* Compute the width of the rectangle to draw. If on a stretch
10278 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10279 rectangle as wide as the glyph, but use a canonical character
10280 width instead. */
10281 wd = cursor_glyph->pixel_width - 1;
10282 if (cursor_glyph->type == STRETCH_GLYPH
10283 && !x_stretch_cursor_p)
10284 wd = min (CANON_X_UNIT (f), wd);
10285
10286 /* The foreground of cursor_gc is typically the same as the normal
10287 background color, which can cause the cursor box to be invisible. */
10288 xgcv.foreground = f->output_data.x->cursor_pixel;
10289 if (dpyinfo->scratch_cursor_gc)
10290 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10291 else
10292 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10293 GCForeground, &xgcv);
10294 gc = dpyinfo->scratch_cursor_gc;
10295
10296 /* Set clipping, draw the rectangle, and reset clipping again. */
10297 x_clip_to_row (w, row, gc, 0);
10298 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10299 XSetClipMask (dpy, gc, None);
dc6f92b8
JB
10300}
10301
06a2c219
GM
10302
10303/* Draw a bar cursor on window W in glyph row ROW.
10304
10305 Implementation note: One would like to draw a bar cursor with an
10306 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10307 Unfortunately, I didn't find a font yet that has this property set.
10308 --gerd. */
dc6f92b8
JB
10309
10310static void
f02d8aa0 10311x_draw_bar_cursor (w, row, width)
06a2c219
GM
10312 struct window *w;
10313 struct glyph_row *row;
f02d8aa0 10314 int width;
dc6f92b8 10315{
06a2c219
GM
10316 /* If cursor hpos is out of bounds, don't draw garbage. This can
10317 happen in mini-buffer windows when switching between echo area
10318 glyphs and mini-buffer. */
10319 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
10320 {
10321 struct frame *f = XFRAME (w->frame);
10322 struct glyph *cursor_glyph;
10323 GC gc;
10324 int x;
10325 unsigned long mask;
10326 XGCValues xgcv;
10327 Display *dpy;
10328 Window window;
10329
10330 cursor_glyph = get_phys_cursor_glyph (w);
10331 if (cursor_glyph == NULL)
10332 return;
10333
10334 xgcv.background = f->output_data.x->cursor_pixel;
10335 xgcv.foreground = f->output_data.x->cursor_pixel;
10336 xgcv.graphics_exposures = 0;
10337 mask = GCForeground | GCBackground | GCGraphicsExposures;
10338 dpy = FRAME_X_DISPLAY (f);
10339 window = FRAME_X_WINDOW (f);
10340 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
10341
10342 if (gc)
10343 XChangeGC (dpy, gc, mask, &xgcv);
10344 else
10345 {
10346 gc = XCreateGC (dpy, window, mask, &xgcv);
10347 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10348 }
10349
f02d8aa0
GM
10350 if (width < 0)
10351 width = f->output_data.x->cursor_width;
10352
06a2c219
GM
10353 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10354 x_clip_to_row (w, row, gc, 0);
10355 XFillRectangle (dpy, window, gc,
10356 x,
10357 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
f02d8aa0 10358 min (cursor_glyph->pixel_width, width),
06a2c219
GM
10359 row->height);
10360 XSetClipMask (dpy, gc, None);
10361 }
dc6f92b8
JB
10362}
10363
06a2c219
GM
10364
10365/* Clear the cursor of window W to background color, and mark the
10366 cursor as not shown. This is used when the text where the cursor
10367 is is about to be rewritten. */
10368
dc6f92b8 10369static void
06a2c219
GM
10370x_clear_cursor (w)
10371 struct window *w;
dc6f92b8 10372{
06a2c219
GM
10373 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10374 x_update_window_cursor (w, 0);
10375}
90e65f07 10376
dbc4e1c1 10377
06a2c219
GM
10378/* Draw the cursor glyph of window W in glyph row ROW. See the
10379 comment of x_draw_glyphs for the meaning of HL. */
dbc4e1c1 10380
06a2c219
GM
10381static void
10382x_draw_phys_cursor_glyph (w, row, hl)
10383 struct window *w;
10384 struct glyph_row *row;
10385 enum draw_glyphs_face hl;
10386{
10387 /* If cursor hpos is out of bounds, don't draw garbage. This can
10388 happen in mini-buffer windows when switching between echo area
10389 glyphs and mini-buffer. */
10390 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
66ac4b0e
GM
10391 {
10392 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10393 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10394 hl, 0, 0, 0);
10395
10396 /* When we erase the cursor, and ROW is overlapped by other
10397 rows, make sure that these overlapping parts of other rows
10398 are redrawn. */
10399 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10400 {
10401 if (row > w->current_matrix->rows
10402 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10403 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10404
10405 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10406 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10407 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10408 }
10409 }
06a2c219 10410}
dbc4e1c1 10411
eea6af04 10412
06a2c219 10413/* Erase the image of a cursor of window W from the screen. */
eea6af04 10414
06a2c219
GM
10415static void
10416x_erase_phys_cursor (w)
10417 struct window *w;
10418{
10419 struct frame *f = XFRAME (w->frame);
10420 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10421 int hpos = w->phys_cursor.hpos;
10422 int vpos = w->phys_cursor.vpos;
10423 int mouse_face_here_p = 0;
10424 struct glyph_matrix *active_glyphs = w->current_matrix;
10425 struct glyph_row *cursor_row;
10426 struct glyph *cursor_glyph;
10427 enum draw_glyphs_face hl;
10428
10429 /* No cursor displayed or row invalidated => nothing to do on the
10430 screen. */
10431 if (w->phys_cursor_type == NO_CURSOR)
10432 goto mark_cursor_off;
10433
10434 /* VPOS >= active_glyphs->nrows means that window has been resized.
10435 Don't bother to erase the cursor. */
10436 if (vpos >= active_glyphs->nrows)
10437 goto mark_cursor_off;
10438
10439 /* If row containing cursor is marked invalid, there is nothing we
10440 can do. */
10441 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10442 if (!cursor_row->enabled_p)
10443 goto mark_cursor_off;
10444
10445 /* This can happen when the new row is shorter than the old one.
10446 In this case, either x_draw_glyphs or clear_end_of_line
10447 should have cleared the cursor. Note that we wouldn't be
10448 able to erase the cursor in this case because we don't have a
10449 cursor glyph at hand. */
10450 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10451 goto mark_cursor_off;
10452
10453 /* If the cursor is in the mouse face area, redisplay that when
10454 we clear the cursor. */
8801a864
KR
10455 if (! NILP (dpyinfo->mouse_face_window)
10456 && w == XWINDOW (dpyinfo->mouse_face_window)
06a2c219
GM
10457 && (vpos > dpyinfo->mouse_face_beg_row
10458 || (vpos == dpyinfo->mouse_face_beg_row
10459 && hpos >= dpyinfo->mouse_face_beg_col))
10460 && (vpos < dpyinfo->mouse_face_end_row
10461 || (vpos == dpyinfo->mouse_face_end_row
10462 && hpos < dpyinfo->mouse_face_end_col))
10463 /* Don't redraw the cursor's spot in mouse face if it is at the
10464 end of a line (on a newline). The cursor appears there, but
10465 mouse highlighting does not. */
10466 && cursor_row->used[TEXT_AREA] > hpos)
10467 mouse_face_here_p = 1;
10468
10469 /* Maybe clear the display under the cursor. */
10470 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10471 {
10472 int x;
045dee35 10473 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dbc4e1c1 10474
06a2c219
GM
10475 cursor_glyph = get_phys_cursor_glyph (w);
10476 if (cursor_glyph == NULL)
10477 goto mark_cursor_off;
dbc4e1c1 10478
06a2c219
GM
10479 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10480
10481 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10482 x,
045dee35 10483 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219
GM
10484 cursor_row->y)),
10485 cursor_glyph->pixel_width,
10486 cursor_row->visible_height,
10487 False);
dbc4e1c1 10488 }
06a2c219
GM
10489
10490 /* Erase the cursor by redrawing the character underneath it. */
10491 if (mouse_face_here_p)
10492 hl = DRAW_MOUSE_FACE;
10493 else if (cursor_row->inverse_p)
10494 hl = DRAW_INVERSE_VIDEO;
10495 else
10496 hl = DRAW_NORMAL_TEXT;
10497 x_draw_phys_cursor_glyph (w, cursor_row, hl);
dbc4e1c1 10498
06a2c219
GM
10499 mark_cursor_off:
10500 w->phys_cursor_on_p = 0;
10501 w->phys_cursor_type = NO_CURSOR;
dbc4e1c1
JB
10502}
10503
10504
06a2c219
GM
10505/* Display or clear cursor of window W. If ON is zero, clear the
10506 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10507 where to put the cursor is specified by HPOS, VPOS, X and Y. */
dbc4e1c1 10508
06a2c219
GM
10509void
10510x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10511 struct window *w;
10512 int on, hpos, vpos, x, y;
dbc4e1c1 10513{
06a2c219
GM
10514 struct frame *f = XFRAME (w->frame);
10515 int new_cursor_type;
f02d8aa0 10516 int new_cursor_width;
06a2c219
GM
10517 struct glyph_matrix *current_glyphs;
10518 struct glyph_row *glyph_row;
10519 struct glyph *glyph;
dbc4e1c1 10520
49d838ea 10521 /* This is pointless on invisible frames, and dangerous on garbaged
06a2c219
GM
10522 windows and frames; in the latter case, the frame or window may
10523 be in the midst of changing its size, and x and y may be off the
10524 window. */
10525 if (! FRAME_VISIBLE_P (f)
10526 || FRAME_GARBAGED_P (f)
10527 || vpos >= w->current_matrix->nrows
10528 || hpos >= w->current_matrix->matrix_w)
dc6f92b8
JB
10529 return;
10530
10531 /* If cursor is off and we want it off, return quickly. */
06a2c219 10532 if (!on && !w->phys_cursor_on_p)
dc6f92b8
JB
10533 return;
10534
06a2c219
GM
10535 current_glyphs = w->current_matrix;
10536 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10537 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10538
10539 /* If cursor row is not enabled, we don't really know where to
10540 display the cursor. */
10541 if (!glyph_row->enabled_p)
10542 {
10543 w->phys_cursor_on_p = 0;
10544 return;
10545 }
10546
10547 xassert (interrupt_input_blocked);
10548
10549 /* Set new_cursor_type to the cursor we want to be displayed. In a
10550 mini-buffer window, we want the cursor only to appear if we are
10551 reading input from this window. For the selected window, we want
10552 the cursor type given by the frame parameter. If explicitly
10553 marked off, draw no cursor. In all other cases, we want a hollow
10554 box cursor. */
f02d8aa0 10555 new_cursor_width = -1;
9b4a7047
GM
10556 if (cursor_in_echo_area
10557 && FRAME_HAS_MINIBUF_P (f)
10558 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
06a2c219 10559 {
9b4a7047
GM
10560 if (w == XWINDOW (echo_area_window))
10561 new_cursor_type = FRAME_DESIRED_CURSOR (f);
06a2c219
GM
10562 else
10563 new_cursor_type = HOLLOW_BOX_CURSOR;
10564 }
06a2c219 10565 else
9b4a7047
GM
10566 {
10567 if (w != XWINDOW (selected_window)
10568 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
10569 {
e55a0b79
GM
10570 extern int cursor_in_non_selected_windows;
10571
10572 if (MINI_WINDOW_P (w) || !cursor_in_non_selected_windows)
9b4a7047
GM
10573 new_cursor_type = NO_CURSOR;
10574 else
10575 new_cursor_type = HOLLOW_BOX_CURSOR;
10576 }
10577 else if (w->cursor_off_p)
10578 new_cursor_type = NO_CURSOR;
10579 else
f02d8aa0
GM
10580 {
10581 struct buffer *b = XBUFFER (w->buffer);
10582
10583 if (EQ (b->cursor_type, Qt))
10584 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10585 else
10586 new_cursor_type = x_specified_cursor_type (b->cursor_type,
10587 &new_cursor_width);
10588 }
9b4a7047 10589 }
06a2c219
GM
10590
10591 /* If cursor is currently being shown and we don't want it to be or
10592 it is in the wrong place, or the cursor type is not what we want,
dc6f92b8 10593 erase it. */
06a2c219 10594 if (w->phys_cursor_on_p
dc6f92b8 10595 && (!on
06a2c219
GM
10596 || w->phys_cursor.x != x
10597 || w->phys_cursor.y != y
10598 || new_cursor_type != w->phys_cursor_type))
10599 x_erase_phys_cursor (w);
10600
10601 /* If the cursor is now invisible and we want it to be visible,
10602 display it. */
10603 if (on && !w->phys_cursor_on_p)
10604 {
10605 w->phys_cursor_ascent = glyph_row->ascent;
10606 w->phys_cursor_height = glyph_row->height;
10607
10608 /* Set phys_cursor_.* before x_draw_.* is called because some
10609 of them may need the information. */
10610 w->phys_cursor.x = x;
10611 w->phys_cursor.y = glyph_row->y;
10612 w->phys_cursor.hpos = hpos;
10613 w->phys_cursor.vpos = vpos;
10614 w->phys_cursor_type = new_cursor_type;
10615 w->phys_cursor_on_p = 1;
10616
10617 switch (new_cursor_type)
dc6f92b8 10618 {
06a2c219
GM
10619 case HOLLOW_BOX_CURSOR:
10620 x_draw_hollow_cursor (w, glyph_row);
10621 break;
10622
10623 case FILLED_BOX_CURSOR:
10624 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10625 break;
10626
10627 case BAR_CURSOR:
f02d8aa0 10628 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
06a2c219
GM
10629 break;
10630
10631 case NO_CURSOR:
10632 break;
dc6f92b8 10633
06a2c219
GM
10634 default:
10635 abort ();
10636 }
59ddecde
GM
10637
10638#ifdef HAVE_X_I18N
10639 if (w == XWINDOW (f->selected_window))
10640 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
10641 xic_set_preeditarea (w, x, y);
10642#endif
dc6f92b8
JB
10643 }
10644
06a2c219 10645#ifndef XFlush
f676886a 10646 if (updating_frame != f)
334208b7 10647 XFlush (FRAME_X_DISPLAY (f));
06a2c219 10648#endif
dc6f92b8
JB
10649}
10650
06a2c219
GM
10651
10652/* Display the cursor on window W, or clear it. X and Y are window
10653 relative pixel coordinates. HPOS and VPOS are glyph matrix
10654 positions. If W is not the selected window, display a hollow
10655 cursor. ON non-zero means display the cursor at X, Y which
10656 correspond to HPOS, VPOS, otherwise it is cleared. */
5d46f928 10657
dfcf069d 10658void
06a2c219
GM
10659x_display_cursor (w, on, hpos, vpos, x, y)
10660 struct window *w;
10661 int on, hpos, vpos, x, y;
dc6f92b8 10662{
f94397b5 10663 BLOCK_INPUT;
06a2c219 10664 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
5d46f928
RS
10665 UNBLOCK_INPUT;
10666}
10667
06a2c219
GM
10668
10669/* Display the cursor on window W, or clear it, according to ON_P.
5d46f928
RS
10670 Don't change the cursor's position. */
10671
dfcf069d 10672void
06a2c219 10673x_update_cursor (f, on_p)
5d46f928 10674 struct frame *f;
5d46f928 10675{
06a2c219
GM
10676 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
10677}
10678
10679
10680/* Call x_update_window_cursor with parameter ON_P on all leaf windows
10681 in the window tree rooted at W. */
10682
10683static void
10684x_update_cursor_in_window_tree (w, on_p)
10685 struct window *w;
10686 int on_p;
10687{
10688 while (w)
10689 {
10690 if (!NILP (w->hchild))
10691 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10692 else if (!NILP (w->vchild))
10693 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10694 else
10695 x_update_window_cursor (w, on_p);
10696
10697 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10698 }
10699}
5d46f928 10700
f94397b5 10701
06a2c219
GM
10702/* Switch the display of W's cursor on or off, according to the value
10703 of ON. */
10704
10705static void
10706x_update_window_cursor (w, on)
10707 struct window *w;
10708 int on;
10709{
16b5d424
GM
10710 /* Don't update cursor in windows whose frame is in the process
10711 of being deleted. */
10712 if (w->current_matrix)
10713 {
10714 BLOCK_INPUT;
10715 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
10716 w->phys_cursor.x, w->phys_cursor.y);
10717 UNBLOCK_INPUT;
10718 }
dc6f92b8 10719}
06a2c219
GM
10720
10721
10722
dc6f92b8
JB
10723\f
10724/* Icons. */
10725
f676886a 10726/* Refresh bitmap kitchen sink icon for frame F
06a2c219 10727 when we get an expose event for it. */
dc6f92b8 10728
dfcf069d 10729void
f676886a
JB
10730refreshicon (f)
10731 struct frame *f;
dc6f92b8 10732{
06a2c219 10733 /* Normally, the window manager handles this function. */
dc6f92b8
JB
10734}
10735
dbc4e1c1 10736/* Make the x-window of frame F use the gnu icon bitmap. */
dc6f92b8
JB
10737
10738int
990ba854 10739x_bitmap_icon (f, file)
f676886a 10740 struct frame *f;
990ba854 10741 Lisp_Object file;
dc6f92b8 10742{
06a2c219 10743 int bitmap_id;
dc6f92b8 10744
c118dd06 10745 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
10746 return 1;
10747
990ba854 10748 /* Free up our existing icon bitmap if any. */
7556890b
RS
10749 if (f->output_data.x->icon_bitmap > 0)
10750 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10751 f->output_data.x->icon_bitmap = 0;
990ba854
RS
10752
10753 if (STRINGP (file))
7f2ae036
RS
10754 bitmap_id = x_create_bitmap_from_file (f, file);
10755 else
10756 {
990ba854 10757 /* Create the GNU bitmap if necessary. */
5bf01b68 10758 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
334208b7
RS
10759 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
10760 = x_create_bitmap_from_data (f, gnu_bits,
10761 gnu_width, gnu_height);
990ba854
RS
10762
10763 /* The first time we create the GNU bitmap,
06a2c219 10764 this increments the ref-count one extra time.
990ba854
RS
10765 As a result, the GNU bitmap is never freed.
10766 That way, we don't have to worry about allocating it again. */
334208b7 10767 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
990ba854 10768
334208b7 10769 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7f2ae036
RS
10770 }
10771
10772 x_wm_set_icon_pixmap (f, bitmap_id);
7556890b 10773 f->output_data.x->icon_bitmap = bitmap_id;
dc6f92b8
JB
10774
10775 return 0;
10776}
10777
10778
1be2d067
KH
10779/* Make the x-window of frame F use a rectangle with text.
10780 Use ICON_NAME as the text. */
dc6f92b8
JB
10781
10782int
f676886a
JB
10783x_text_icon (f, icon_name)
10784 struct frame *f;
dc6f92b8
JB
10785 char *icon_name;
10786{
c118dd06 10787 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
10788 return 1;
10789
1be2d067
KH
10790#ifdef HAVE_X11R4
10791 {
10792 XTextProperty text;
10793 text.value = (unsigned char *) icon_name;
10794 text.encoding = XA_STRING;
10795 text.format = 8;
10796 text.nitems = strlen (icon_name);
10797#ifdef USE_X_TOOLKIT
7556890b 10798 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
1be2d067
KH
10799 &text);
10800#else /* not USE_X_TOOLKIT */
10801 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
10802#endif /* not USE_X_TOOLKIT */
10803 }
10804#else /* not HAVE_X11R4 */
10805 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
10806#endif /* not HAVE_X11R4 */
58769bee 10807
7556890b
RS
10808 if (f->output_data.x->icon_bitmap > 0)
10809 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10810 f->output_data.x->icon_bitmap = 0;
b1c884c3 10811 x_wm_set_icon_pixmap (f, 0);
dc6f92b8
JB
10812
10813 return 0;
10814}
10815\f
e99db5a1
RS
10816#define X_ERROR_MESSAGE_SIZE 200
10817
10818/* If non-nil, this should be a string.
10819 It means catch X errors and store the error message in this string. */
10820
10821static Lisp_Object x_error_message_string;
10822
10823/* An X error handler which stores the error message in
10824 x_error_message_string. This is called from x_error_handler if
10825 x_catch_errors is in effect. */
10826
06a2c219 10827static void
e99db5a1
RS
10828x_error_catcher (display, error)
10829 Display *display;
10830 XErrorEvent *error;
10831{
10832 XGetErrorText (display, error->error_code,
10833 XSTRING (x_error_message_string)->data,
10834 X_ERROR_MESSAGE_SIZE);
10835}
10836
10837/* Begin trapping X errors for display DPY. Actually we trap X errors
10838 for all displays, but DPY should be the display you are actually
10839 operating on.
10840
10841 After calling this function, X protocol errors no longer cause
10842 Emacs to exit; instead, they are recorded in the string
10843 stored in x_error_message_string.
10844
10845 Calling x_check_errors signals an Emacs error if an X error has
10846 occurred since the last call to x_catch_errors or x_check_errors.
10847
10848 Calling x_uncatch_errors resumes the normal error handling. */
10849
10850void x_check_errors ();
10851static Lisp_Object x_catch_errors_unwind ();
10852
10853int
10854x_catch_errors (dpy)
10855 Display *dpy;
10856{
10857 int count = specpdl_ptr - specpdl;
10858
10859 /* Make sure any errors from previous requests have been dealt with. */
10860 XSync (dpy, False);
10861
10862 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
10863
10864 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
10865 XSTRING (x_error_message_string)->data[0] = 0;
10866
10867 return count;
10868}
10869
10870/* Unbind the binding that we made to check for X errors. */
10871
10872static Lisp_Object
10873x_catch_errors_unwind (old_val)
10874 Lisp_Object old_val;
10875{
10876 x_error_message_string = old_val;
10877 return Qnil;
10878}
10879
10880/* If any X protocol errors have arrived since the last call to
10881 x_catch_errors or x_check_errors, signal an Emacs error using
10882 sprintf (a buffer, FORMAT, the x error message text) as the text. */
10883
10884void
10885x_check_errors (dpy, format)
10886 Display *dpy;
10887 char *format;
10888{
10889 /* Make sure to catch any errors incurred so far. */
10890 XSync (dpy, False);
10891
10892 if (XSTRING (x_error_message_string)->data[0])
10893 error (format, XSTRING (x_error_message_string)->data);
10894}
10895
9829ddba
RS
10896/* Nonzero if we had any X protocol errors
10897 since we did x_catch_errors on DPY. */
e99db5a1
RS
10898
10899int
10900x_had_errors_p (dpy)
10901 Display *dpy;
10902{
10903 /* Make sure to catch any errors incurred so far. */
10904 XSync (dpy, False);
10905
10906 return XSTRING (x_error_message_string)->data[0] != 0;
10907}
10908
9829ddba
RS
10909/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
10910
06a2c219 10911void
9829ddba
RS
10912x_clear_errors (dpy)
10913 Display *dpy;
10914{
10915 XSTRING (x_error_message_string)->data[0] = 0;
10916}
10917
e99db5a1
RS
10918/* Stop catching X protocol errors and let them make Emacs die.
10919 DPY should be the display that was passed to x_catch_errors.
10920 COUNT should be the value that was returned by
10921 the corresponding call to x_catch_errors. */
10922
10923void
10924x_uncatch_errors (dpy, count)
10925 Display *dpy;
10926 int count;
10927{
10928 unbind_to (count, Qnil);
10929}
10930
10931#if 0
10932static unsigned int x_wire_count;
10933x_trace_wire ()
10934{
10935 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
10936}
10937#endif /* ! 0 */
10938
10939\f
10940/* Handle SIGPIPE, which can happen when the connection to a server
10941 simply goes away. SIGPIPE is handled by x_connection_signal.
10942 Don't need to do anything, because the write which caused the
10943 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
06a2c219 10944 which will do the appropriate cleanup for us. */
e99db5a1
RS
10945
10946static SIGTYPE
10947x_connection_signal (signalnum) /* If we don't have an argument, */
06a2c219 10948 int signalnum; /* some compilers complain in signal calls. */
e99db5a1
RS
10949{
10950#ifdef USG
10951 /* USG systems forget handlers when they are used;
10952 must reestablish each time */
10953 signal (signalnum, x_connection_signal);
10954#endif /* USG */
10955}
10956\f
4746118a
JB
10957/* Handling X errors. */
10958
7a13e894 10959/* Handle the loss of connection to display DISPLAY. */
16bd92ea 10960
4746118a 10961static SIGTYPE
7a13e894
RS
10962x_connection_closed (display, error_message)
10963 Display *display;
10964 char *error_message;
4746118a 10965{
7a13e894
RS
10966 struct x_display_info *dpyinfo = x_display_info_for_display (display);
10967 Lisp_Object frame, tail;
10968
6186a4a0
RS
10969 /* Indicate that this display is dead. */
10970
2e465cdd 10971#if 0 /* Closing the display caused a bus error on OpenWindows. */
f613a4c8 10972#ifdef USE_X_TOOLKIT
adabc3a9 10973 XtCloseDisplay (display);
2e465cdd 10974#endif
f613a4c8 10975#endif
adabc3a9 10976
9e80b57d
KR
10977 if (dpyinfo)
10978 dpyinfo->display = 0;
6186a4a0 10979
06a2c219 10980 /* First delete frames whose mini-buffers are on frames
7a13e894
RS
10981 that are on the dead display. */
10982 FOR_EACH_FRAME (tail, frame)
10983 {
10984 Lisp_Object minibuf_frame;
10985 minibuf_frame
10986 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
f48f33ca
RS
10987 if (FRAME_X_P (XFRAME (frame))
10988 && FRAME_X_P (XFRAME (minibuf_frame))
10989 && ! EQ (frame, minibuf_frame)
10990 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7a13e894
RS
10991 Fdelete_frame (frame, Qt);
10992 }
10993
10994 /* Now delete all remaining frames on the dead display.
06a2c219 10995 We are now sure none of these is used as the mini-buffer
7a13e894
RS
10996 for another frame that we need to delete. */
10997 FOR_EACH_FRAME (tail, frame)
f48f33ca
RS
10998 if (FRAME_X_P (XFRAME (frame))
10999 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
07a7096a
KH
11000 {
11001 /* Set this to t so that Fdelete_frame won't get confused
11002 trying to find a replacement. */
11003 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
11004 Fdelete_frame (frame, Qt);
11005 }
7a13e894 11006
482a1bd2
KH
11007 if (dpyinfo)
11008 x_delete_display (dpyinfo);
7a13e894
RS
11009
11010 if (x_display_list == 0)
11011 {
f8d07b62 11012 fprintf (stderr, "%s\n", error_message);
7a13e894
RS
11013 shut_down_emacs (0, 0, Qnil);
11014 exit (70);
11015 }
12ba150f 11016
7a13e894
RS
11017 /* Ordinary stack unwind doesn't deal with these. */
11018#ifdef SIGIO
11019 sigunblock (sigmask (SIGIO));
11020#endif
11021 sigunblock (sigmask (SIGALRM));
11022 TOTALLY_UNBLOCK_INPUT;
11023
aa4d9a9e 11024 clear_waiting_for_input ();
7a13e894 11025 error ("%s", error_message);
4746118a
JB
11026}
11027
7a13e894
RS
11028/* This is the usual handler for X protocol errors.
11029 It kills all frames on the display that we got the error for.
11030 If that was the only one, it prints an error message and kills Emacs. */
11031
06a2c219 11032static void
c118dd06
JB
11033x_error_quitter (display, error)
11034 Display *display;
11035 XErrorEvent *error;
11036{
7a13e894 11037 char buf[256], buf1[356];
dc6f92b8 11038
58769bee 11039 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 11040 original error handler. */
dc6f92b8 11041
c118dd06 11042 XGetErrorText (display, error->error_code, buf, sizeof (buf));
0a0fdc70 11043 sprintf (buf1, "X protocol error: %s on protocol request %d",
c118dd06 11044 buf, error->request_code);
7a13e894 11045 x_connection_closed (display, buf1);
dc6f92b8
JB
11046}
11047
e99db5a1
RS
11048/* This is the first-level handler for X protocol errors.
11049 It calls x_error_quitter or x_error_catcher. */
7a13e894 11050
8922af5f 11051static int
e99db5a1 11052x_error_handler (display, error)
8922af5f 11053 Display *display;
e99db5a1 11054 XErrorEvent *error;
8922af5f 11055{
e99db5a1
RS
11056 if (! NILP (x_error_message_string))
11057 x_error_catcher (display, error);
11058 else
11059 x_error_quitter (display, error);
06a2c219 11060 return 0;
f9e24cb9 11061}
c118dd06 11062
e99db5a1
RS
11063/* This is the handler for X IO errors, always.
11064 It kills all frames on the display that we lost touch with.
11065 If that was the only one, it prints an error message and kills Emacs. */
7a13e894 11066
c118dd06 11067static int
e99db5a1 11068x_io_error_quitter (display)
c118dd06 11069 Display *display;
c118dd06 11070{
e99db5a1 11071 char buf[256];
dc6f92b8 11072
e99db5a1
RS
11073 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
11074 x_connection_closed (display, buf);
06a2c219 11075 return 0;
dc6f92b8 11076}
dc6f92b8 11077\f
f451eb13
JB
11078/* Changing the font of the frame. */
11079
76bcdf39
RS
11080/* Give frame F the font named FONTNAME as its default font, and
11081 return the full name of that font. FONTNAME may be a wildcard
11082 pattern; in that case, we choose some font that fits the pattern.
11083 The return value shows which font we chose. */
11084
b5cf7a0e 11085Lisp_Object
f676886a
JB
11086x_new_font (f, fontname)
11087 struct frame *f;
dc6f92b8
JB
11088 register char *fontname;
11089{
dc43ef94 11090 struct font_info *fontp
ee569018 11091 = FS_LOAD_FONT (f, 0, fontname, -1);
dc6f92b8 11092
dc43ef94
KH
11093 if (!fontp)
11094 return Qnil;
2224a5fc 11095
dc43ef94 11096 f->output_data.x->font = (XFontStruct *) (fontp->font);
b4192550 11097 f->output_data.x->baseline_offset = fontp->baseline_offset;
dc43ef94
KH
11098 f->output_data.x->fontset = -1;
11099
b2cad826
KH
11100 /* Compute the scroll bar width in character columns. */
11101 if (f->scroll_bar_pixel_width > 0)
11102 {
7556890b 11103 int wid = FONT_WIDTH (f->output_data.x->font);
b2cad826
KH
11104 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
11105 }
11106 else
4e61bddf
RS
11107 {
11108 int wid = FONT_WIDTH (f->output_data.x->font);
11109 f->scroll_bar_cols = (14 + wid - 1) / wid;
11110 }
b2cad826 11111
f676886a 11112 /* Now make the frame display the given font. */
c118dd06 11113 if (FRAME_X_WINDOW (f) != 0)
dc6f92b8 11114 {
7556890b
RS
11115 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
11116 f->output_data.x->font->fid);
11117 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
11118 f->output_data.x->font->fid);
11119 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
11120 f->output_data.x->font->fid);
f676886a 11121
a27f9f86 11122 frame_update_line_height (f);
0134a210 11123 x_set_window_size (f, 0, f->width, f->height);
dc6f92b8 11124 }
a27f9f86
RS
11125 else
11126 /* If we are setting a new frame's font for the first time,
11127 there are no faces yet, so this font's height is the line height. */
7556890b 11128 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
dc6f92b8 11129
dc43ef94
KH
11130 return build_string (fontp->full_name);
11131}
11132
11133/* Give frame F the fontset named FONTSETNAME as its default font, and
11134 return the full name of that fontset. FONTSETNAME may be a wildcard
b5210ea7
KH
11135 pattern; in that case, we choose some fontset that fits the pattern.
11136 The return value shows which fontset we chose. */
b5cf7a0e 11137
dc43ef94
KH
11138Lisp_Object
11139x_new_fontset (f, fontsetname)
11140 struct frame *f;
11141 char *fontsetname;
11142{
ee569018 11143 int fontset = fs_query_fontset (build_string (fontsetname), 0);
dc43ef94 11144 Lisp_Object result;
b5cf7a0e 11145
dc43ef94
KH
11146 if (fontset < 0)
11147 return Qnil;
b5cf7a0e 11148
2da424f1
KH
11149 if (f->output_data.x->fontset == fontset)
11150 /* This fontset is already set in frame F. There's nothing more
11151 to do. */
ee569018 11152 return fontset_name (fontset);
dc43ef94 11153
ee569018 11154 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
dc43ef94
KH
11155
11156 if (!STRINGP (result))
11157 /* Can't load ASCII font. */
11158 return Qnil;
11159
11160 /* Since x_new_font doesn't update any fontset information, do it now. */
11161 f->output_data.x->fontset = fontset;
dc43ef94 11162
f5d11644
GM
11163#ifdef HAVE_X_I18N
11164 if (FRAME_XIC (f)
11165 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
ee569018 11166 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
f5d11644
GM
11167#endif
11168
dc43ef94 11169 return build_string (fontsetname);
dc6f92b8 11170}
f5d11644
GM
11171
11172\f
11173/***********************************************************************
11174 X Input Methods
11175 ***********************************************************************/
11176
11177#ifdef HAVE_X_I18N
11178
11179#ifdef HAVE_X11R6
11180
11181/* XIM destroy callback function, which is called whenever the
11182 connection to input method XIM dies. CLIENT_DATA contains a
11183 pointer to the x_display_info structure corresponding to XIM. */
11184
11185static void
11186xim_destroy_callback (xim, client_data, call_data)
11187 XIM xim;
11188 XPointer client_data;
11189 XPointer call_data;
11190{
11191 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
11192 Lisp_Object frame, tail;
11193
11194 BLOCK_INPUT;
11195
11196 /* No need to call XDestroyIC.. */
11197 FOR_EACH_FRAME (tail, frame)
11198 {
11199 struct frame *f = XFRAME (frame);
11200 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
11201 {
11202 FRAME_XIC (f) = NULL;
11203 if (FRAME_XIC_FONTSET (f))
11204 {
11205 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
11206 FRAME_XIC_FONTSET (f) = NULL;
11207 }
11208 }
11209 }
11210
11211 /* No need to call XCloseIM. */
11212 dpyinfo->xim = NULL;
11213 XFree (dpyinfo->xim_styles);
11214 UNBLOCK_INPUT;
11215}
11216
11217#endif /* HAVE_X11R6 */
11218
11219/* Open the connection to the XIM server on display DPYINFO.
11220 RESOURCE_NAME is the resource name Emacs uses. */
11221
11222static void
11223xim_open_dpy (dpyinfo, resource_name)
11224 struct x_display_info *dpyinfo;
11225 char *resource_name;
11226{
287f7dd6 11227#ifdef USE_XIM
f5d11644
GM
11228 XIM xim;
11229
11230 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
11231 dpyinfo->xim = xim;
11232
11233 if (xim)
11234 {
f5d11644
GM
11235#ifdef HAVE_X11R6
11236 XIMCallback destroy;
11237#endif
11238
11239 /* Get supported styles and XIM values. */
11240 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
11241
11242#ifdef HAVE_X11R6
11243 destroy.callback = xim_destroy_callback;
11244 destroy.client_data = (XPointer)dpyinfo;
68642df6 11245 /* This isn't prptotyped in OSF 5.0. */
f5d11644
GM
11246 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
11247#endif
11248 }
287f7dd6
GM
11249
11250#else /* not USE_XIM */
11251 dpyinfo->xim = NULL;
11252#endif /* not USE_XIM */
f5d11644
GM
11253}
11254
11255
b9de836c 11256#ifdef HAVE_X11R6_XIM
f5d11644
GM
11257
11258struct xim_inst_t
11259{
11260 struct x_display_info *dpyinfo;
11261 char *resource_name;
11262};
11263
11264/* XIM instantiate callback function, which is called whenever an XIM
11265 server is available. DISPLAY is teh display of the XIM.
11266 CLIENT_DATA contains a pointer to an xim_inst_t structure created
11267 when the callback was registered. */
11268
11269static void
11270xim_instantiate_callback (display, client_data, call_data)
11271 Display *display;
11272 XPointer client_data;
11273 XPointer call_data;
11274{
11275 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
11276 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
11277
11278 /* We don't support multiple XIM connections. */
11279 if (dpyinfo->xim)
11280 return;
11281
11282 xim_open_dpy (dpyinfo, xim_inst->resource_name);
11283
11284 /* Create XIC for the existing frames on the same display, as long
11285 as they have no XIC. */
11286 if (dpyinfo->xim && dpyinfo->reference_count > 0)
11287 {
11288 Lisp_Object tail, frame;
11289
11290 BLOCK_INPUT;
11291 FOR_EACH_FRAME (tail, frame)
11292 {
11293 struct frame *f = XFRAME (frame);
11294
11295 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
11296 if (FRAME_XIC (f) == NULL)
11297 {
11298 create_frame_xic (f);
11299 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
11300 xic_set_statusarea (f);
11301 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
11302 {
11303 struct window *w = XWINDOW (f->selected_window);
11304 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
11305 }
11306 }
11307 }
11308
11309 UNBLOCK_INPUT;
11310 }
11311}
11312
b9de836c 11313#endif /* HAVE_X11R6_XIM */
f5d11644
GM
11314
11315
11316/* Open a connection to the XIM server on display DPYINFO.
11317 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
11318 connection only at the first time. On X11R6, open the connection
11319 in the XIM instantiate callback function. */
11320
11321static void
11322xim_initialize (dpyinfo, resource_name)
11323 struct x_display_info *dpyinfo;
11324 char *resource_name;
11325{
287f7dd6 11326#ifdef USE_XIM
b9de836c 11327#ifdef HAVE_X11R6_XIM
f5d11644
GM
11328 struct xim_inst_t *xim_inst;
11329 int len;
11330
11331 dpyinfo->xim = NULL;
11332 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
11333 xim_inst->dpyinfo = dpyinfo;
11334 len = strlen (resource_name);
11335 xim_inst->resource_name = (char *) xmalloc (len + 1);
11336 bcopy (resource_name, xim_inst->resource_name, len + 1);
11337 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11338 resource_name, EMACS_CLASS,
11339 xim_instantiate_callback,
2ebb2f8b
DL
11340 /* Fixme: This is XPointer in
11341 XFree86 but (XPointer *) on
11342 Tru64, at least. */
11343 (XPointer) xim_inst);
b9de836c 11344#else /* not HAVE_X11R6_XIM */
f5d11644
GM
11345 dpyinfo->xim = NULL;
11346 xim_open_dpy (dpyinfo, resource_name);
b9de836c 11347#endif /* not HAVE_X11R6_XIM */
287f7dd6
GM
11348
11349#else /* not USE_XIM */
11350 dpyinfo->xim = NULL;
11351#endif /* not USE_XIM */
f5d11644
GM
11352}
11353
11354
11355/* Close the connection to the XIM server on display DPYINFO. */
11356
11357static void
11358xim_close_dpy (dpyinfo)
11359 struct x_display_info *dpyinfo;
11360{
287f7dd6 11361#ifdef USE_XIM
b9de836c 11362#ifdef HAVE_X11R6_XIM
f5d11644
GM
11363 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11364 NULL, EMACS_CLASS,
11365 xim_instantiate_callback, NULL);
b9de836c 11366#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
11367 XCloseIM (dpyinfo->xim);
11368 dpyinfo->xim = NULL;
11369 XFree (dpyinfo->xim_styles);
287f7dd6 11370#endif /* USE_XIM */
f5d11644
GM
11371}
11372
b9de836c 11373#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
11374
11375
dc6f92b8 11376\f
2e365682
RS
11377/* Calculate the absolute position in frame F
11378 from its current recorded position values and gravity. */
11379
dfcf069d 11380void
43bca5d5 11381x_calc_absolute_position (f)
f676886a 11382 struct frame *f;
dc6f92b8 11383{
06a2c219 11384 Window child;
6dba1858 11385 int win_x = 0, win_y = 0;
7556890b 11386 int flags = f->output_data.x->size_hint_flags;
c81412a0
KH
11387 int this_window;
11388
9829ddba
RS
11389 /* We have nothing to do if the current position
11390 is already for the top-left corner. */
11391 if (! ((flags & XNegative) || (flags & YNegative)))
11392 return;
11393
c81412a0 11394#ifdef USE_X_TOOLKIT
7556890b 11395 this_window = XtWindow (f->output_data.x->widget);
c81412a0
KH
11396#else
11397 this_window = FRAME_X_WINDOW (f);
11398#endif
6dba1858
RS
11399
11400 /* Find the position of the outside upper-left corner of
9829ddba
RS
11401 the inner window, with respect to the outer window.
11402 But do this only if we will need the results. */
7556890b 11403 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6dba1858 11404 {
9829ddba
RS
11405 int count;
11406
6dba1858 11407 BLOCK_INPUT;
9829ddba
RS
11408 count = x_catch_errors (FRAME_X_DISPLAY (f));
11409 while (1)
11410 {
11411 x_clear_errors (FRAME_X_DISPLAY (f));
11412 XTranslateCoordinates (FRAME_X_DISPLAY (f),
11413
11414 /* From-window, to-window. */
11415 this_window,
11416 f->output_data.x->parent_desc,
11417
11418 /* From-position, to-position. */
11419 0, 0, &win_x, &win_y,
11420
11421 /* Child of win. */
11422 &child);
11423 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
11424 {
11425 Window newroot, newparent = 0xdeadbeef;
11426 Window *newchildren;
2ebb2f8b 11427 unsigned int nchildren;
9829ddba
RS
11428
11429 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
11430 &newparent, &newchildren, &nchildren))
11431 break;
58769bee 11432
7c3c78a3 11433 XFree ((char *) newchildren);
6dba1858 11434
9829ddba
RS
11435 f->output_data.x->parent_desc = newparent;
11436 }
11437 else
11438 break;
11439 }
6dba1858 11440
9829ddba 11441 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
6dba1858
RS
11442 UNBLOCK_INPUT;
11443 }
11444
11445 /* Treat negative positions as relative to the leftmost bottommost
11446 position that fits on the screen. */
20f55f9a 11447 if (flags & XNegative)
7556890b 11448 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
2e365682
RS
11449 - 2 * f->output_data.x->border_width - win_x
11450 - PIXEL_WIDTH (f)
11451 + f->output_data.x->left_pos);
dc6f92b8 11452
20f55f9a 11453 if (flags & YNegative)
06a2c219
GM
11454 {
11455 int menubar_height = 0;
11456
11457#ifdef USE_X_TOOLKIT
11458 if (f->output_data.x->menubar_widget)
11459 menubar_height
11460 = (f->output_data.x->menubar_widget->core.height
11461 + f->output_data.x->menubar_widget->core.border_width);
11462#endif
11463
11464 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
11465 - 2 * f->output_data.x->border_width
11466 - win_y
11467 - PIXEL_HEIGHT (f)
11468 - menubar_height
11469 + f->output_data.x->top_pos);
11470 }
2e365682 11471
3a35ab44
RS
11472 /* The left_pos and top_pos
11473 are now relative to the top and left screen edges,
11474 so the flags should correspond. */
7556890b 11475 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
dc6f92b8
JB
11476}
11477
3a35ab44
RS
11478/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
11479 to really change the position, and 0 when calling from
11480 x_make_frame_visible (in that case, XOFF and YOFF are the current
aa3ff7c9
KH
11481 position values). It is -1 when calling from x_set_frame_parameters,
11482 which means, do adjust for borders but don't change the gravity. */
3a35ab44 11483
dfcf069d 11484void
dc05a16b 11485x_set_offset (f, xoff, yoff, change_gravity)
f676886a 11486 struct frame *f;
dc6f92b8 11487 register int xoff, yoff;
dc05a16b 11488 int change_gravity;
dc6f92b8 11489{
4a4cbdd5
KH
11490 int modified_top, modified_left;
11491
aa3ff7c9 11492 if (change_gravity > 0)
3a35ab44 11493 {
7556890b
RS
11494 f->output_data.x->top_pos = yoff;
11495 f->output_data.x->left_pos = xoff;
11496 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
3a35ab44 11497 if (xoff < 0)
7556890b 11498 f->output_data.x->size_hint_flags |= XNegative;
3a35ab44 11499 if (yoff < 0)
7556890b
RS
11500 f->output_data.x->size_hint_flags |= YNegative;
11501 f->output_data.x->win_gravity = NorthWestGravity;
3a35ab44 11502 }
43bca5d5 11503 x_calc_absolute_position (f);
dc6f92b8
JB
11504
11505 BLOCK_INPUT;
c32cdd9a 11506 x_wm_set_size_hint (f, (long) 0, 0);
3a35ab44 11507
7556890b
RS
11508 modified_left = f->output_data.x->left_pos;
11509 modified_top = f->output_data.x->top_pos;
e73ec6fa
RS
11510#if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
11511 this seems to be unnecessary and incorrect. rms, 4/17/97. */
11512 /* It is a mystery why we need to add the border_width here
11513 when the frame is already visible, but experiment says we do. */
aa3ff7c9 11514 if (change_gravity != 0)
4a4cbdd5 11515 {
7556890b
RS
11516 modified_left += f->output_data.x->border_width;
11517 modified_top += f->output_data.x->border_width;
4a4cbdd5 11518 }
e73ec6fa 11519#endif
4a4cbdd5 11520
3afe33e7 11521#ifdef USE_X_TOOLKIT
7556890b 11522 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
4a4cbdd5 11523 modified_left, modified_top);
3afe33e7 11524#else /* not USE_X_TOOLKIT */
334208b7 11525 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4a4cbdd5 11526 modified_left, modified_top);
3afe33e7 11527#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11528 UNBLOCK_INPUT;
11529}
11530
bc20ebbf
FP
11531/* Call this to change the size of frame F's x-window.
11532 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
11533 for this size change and subsequent size changes.
11534 Otherwise we leave the window gravity unchanged. */
dc6f92b8 11535
dfcf069d 11536void
bc20ebbf 11537x_set_window_size (f, change_gravity, cols, rows)
f676886a 11538 struct frame *f;
bc20ebbf 11539 int change_gravity;
b1c884c3 11540 int cols, rows;
dc6f92b8 11541{
06a2c219 11542#ifndef USE_X_TOOLKIT
dc6f92b8 11543 int pixelwidth, pixelheight;
06a2c219 11544#endif
dc6f92b8 11545
80fd1fe2 11546 BLOCK_INPUT;
aee9a898
RS
11547
11548#ifdef USE_X_TOOLKIT
3a20653d
RS
11549 {
11550 /* The x and y position of the widget is clobbered by the
11551 call to XtSetValues within EmacsFrameSetCharSize.
11552 This is a real kludge, but I don't understand Xt so I can't
11553 figure out a correct fix. Can anyone else tell me? -- rms. */
7556890b
RS
11554 int xpos = f->output_data.x->widget->core.x;
11555 int ypos = f->output_data.x->widget->core.y;
11556 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
11557 f->output_data.x->widget->core.x = xpos;
11558 f->output_data.x->widget->core.y = ypos;
3a20653d 11559 }
80fd1fe2
FP
11560
11561#else /* not USE_X_TOOLKIT */
11562
b1c884c3 11563 check_frame_size (f, &rows, &cols);
7556890b 11564 f->output_data.x->vertical_scroll_bar_extra
b2cad826
KH
11565 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
11566 ? 0
11567 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
480407eb 11568 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7556890b 11569 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
06a2c219 11570 f->output_data.x->flags_areas_extra
110859fc 11571 = FRAME_FLAGS_AREA_WIDTH (f);
f451eb13
JB
11572 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
11573 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
dc6f92b8 11574
7556890b 11575 f->output_data.x->win_gravity = NorthWestGravity;
c32cdd9a 11576 x_wm_set_size_hint (f, (long) 0, 0);
6ccf47d1 11577
334208b7
RS
11578 XSync (FRAME_X_DISPLAY (f), False);
11579 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11580 pixelwidth, pixelheight);
b1c884c3
JB
11581
11582 /* Now, strictly speaking, we can't be sure that this is accurate,
11583 but the window manager will get around to dealing with the size
11584 change request eventually, and we'll hear how it went when the
dbc4e1c1
JB
11585 ConfigureNotify event gets here.
11586
11587 We could just not bother storing any of this information here,
11588 and let the ConfigureNotify event set everything up, but that
fddd5ceb 11589 might be kind of confusing to the Lisp code, since size changes
dbc4e1c1 11590 wouldn't be reported in the frame parameters until some random
fddd5ceb
RS
11591 point in the future when the ConfigureNotify event arrives.
11592
11593 We pass 1 for DELAY since we can't run Lisp code inside of
11594 a BLOCK_INPUT. */
7d1e984f 11595 change_frame_size (f, rows, cols, 0, 1, 0);
b1c884c3
JB
11596 PIXEL_WIDTH (f) = pixelwidth;
11597 PIXEL_HEIGHT (f) = pixelheight;
11598
aee9a898
RS
11599 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11600 receive in the ConfigureNotify event; if we get what we asked
11601 for, then the event won't cause the screen to become garbaged, so
11602 we have to make sure to do it here. */
11603 SET_FRAME_GARBAGED (f);
11604
11605 XFlush (FRAME_X_DISPLAY (f));
11606
11607#endif /* not USE_X_TOOLKIT */
11608
4d73d038 11609 /* If cursor was outside the new size, mark it as off. */
06a2c219 11610 mark_window_cursors_off (XWINDOW (f->root_window));
4d73d038 11611
aee9a898
RS
11612 /* Clear out any recollection of where the mouse highlighting was,
11613 since it might be in a place that's outside the new frame size.
11614 Actually checking whether it is outside is a pain in the neck,
11615 so don't try--just let the highlighting be done afresh with new size. */
e687d06e 11616 cancel_mouse_face (f);
dbc4e1c1 11617
dc6f92b8
JB
11618 UNBLOCK_INPUT;
11619}
dc6f92b8 11620\f
d047c4eb 11621/* Mouse warping. */
dc6f92b8 11622
9b378208 11623void
f676886a
JB
11624x_set_mouse_position (f, x, y)
11625 struct frame *f;
dc6f92b8
JB
11626 int x, y;
11627{
11628 int pix_x, pix_y;
11629
7556890b
RS
11630 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
11631 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
f451eb13
JB
11632
11633 if (pix_x < 0) pix_x = 0;
11634 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
11635
11636 if (pix_y < 0) pix_y = 0;
11637 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
dc6f92b8
JB
11638
11639 BLOCK_INPUT;
dc6f92b8 11640
334208b7
RS
11641 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11642 0, 0, 0, 0, pix_x, pix_y);
dc6f92b8
JB
11643 UNBLOCK_INPUT;
11644}
11645
9b378208
RS
11646/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11647
11648void
11649x_set_mouse_pixel_position (f, pix_x, pix_y)
11650 struct frame *f;
11651 int pix_x, pix_y;
11652{
11653 BLOCK_INPUT;
11654
334208b7
RS
11655 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11656 0, 0, 0, 0, pix_x, pix_y);
9b378208
RS
11657 UNBLOCK_INPUT;
11658}
d047c4eb
KH
11659\f
11660/* focus shifting, raising and lowering. */
9b378208 11661
dfcf069d 11662void
f676886a
JB
11663x_focus_on_frame (f)
11664 struct frame *f;
dc6f92b8 11665{
1fb20991 11666#if 0 /* This proves to be unpleasant. */
f676886a 11667 x_raise_frame (f);
1fb20991 11668#endif
6d4238f3
JB
11669#if 0
11670 /* I don't think that the ICCCM allows programs to do things like this
11671 without the interaction of the window manager. Whatever you end up
f676886a 11672 doing with this code, do it to x_unfocus_frame too. */
334208b7 11673 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dc6f92b8 11674 RevertToPointerRoot, CurrentTime);
c118dd06 11675#endif /* ! 0 */
dc6f92b8
JB
11676}
11677
dfcf069d 11678void
f676886a
JB
11679x_unfocus_frame (f)
11680 struct frame *f;
dc6f92b8 11681{
6d4238f3 11682#if 0
f676886a 11683 /* Look at the remarks in x_focus_on_frame. */
0f941935 11684 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
334208b7 11685 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
dc6f92b8 11686 RevertToPointerRoot, CurrentTime);
c118dd06 11687#endif /* ! 0 */
dc6f92b8
JB
11688}
11689
f676886a 11690/* Raise frame F. */
dc6f92b8 11691
dfcf069d 11692void
f676886a
JB
11693x_raise_frame (f)
11694 struct frame *f;
dc6f92b8 11695{
3a88c238 11696 if (f->async_visible)
dc6f92b8
JB
11697 {
11698 BLOCK_INPUT;
3afe33e7 11699#ifdef USE_X_TOOLKIT
7556890b 11700 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 11701#else /* not USE_X_TOOLKIT */
334208b7 11702 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11703#endif /* not USE_X_TOOLKIT */
334208b7 11704 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
11705 UNBLOCK_INPUT;
11706 }
11707}
11708
f676886a 11709/* Lower frame F. */
dc6f92b8 11710
dfcf069d 11711void
f676886a
JB
11712x_lower_frame (f)
11713 struct frame *f;
dc6f92b8 11714{
3a88c238 11715 if (f->async_visible)
dc6f92b8
JB
11716 {
11717 BLOCK_INPUT;
3afe33e7 11718#ifdef USE_X_TOOLKIT
7556890b 11719 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 11720#else /* not USE_X_TOOLKIT */
334208b7 11721 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11722#endif /* not USE_X_TOOLKIT */
334208b7 11723 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
11724 UNBLOCK_INPUT;
11725 }
11726}
11727
dbc4e1c1 11728static void
6b0442dc 11729XTframe_raise_lower (f, raise_flag)
dbc4e1c1 11730 FRAME_PTR f;
6b0442dc 11731 int raise_flag;
dbc4e1c1 11732{
6b0442dc 11733 if (raise_flag)
dbc4e1c1
JB
11734 x_raise_frame (f);
11735 else
11736 x_lower_frame (f);
11737}
d047c4eb
KH
11738\f
11739/* Change of visibility. */
dc6f92b8 11740
9382638d
KH
11741/* This tries to wait until the frame is really visible.
11742 However, if the window manager asks the user where to position
11743 the frame, this will return before the user finishes doing that.
11744 The frame will not actually be visible at that time,
11745 but it will become visible later when the window manager
11746 finishes with it. */
11747
dfcf069d 11748void
f676886a
JB
11749x_make_frame_visible (f)
11750 struct frame *f;
dc6f92b8 11751{
990ba854 11752 Lisp_Object type;
1aa6072f 11753 int original_top, original_left;
dc6f92b8 11754
dc6f92b8 11755 BLOCK_INPUT;
dc6f92b8 11756
990ba854
RS
11757 type = x_icon_type (f);
11758 if (!NILP (type))
11759 x_bitmap_icon (f, type);
bdcd49ba 11760
f676886a 11761 if (! FRAME_VISIBLE_P (f))
90e65f07 11762 {
1aa6072f
RS
11763 /* We test FRAME_GARBAGED_P here to make sure we don't
11764 call x_set_offset a second time
11765 if we get to x_make_frame_visible a second time
11766 before the window gets really visible. */
11767 if (! FRAME_ICONIFIED_P (f)
11768 && ! f->output_data.x->asked_for_visible)
11769 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
11770
11771 f->output_data.x->asked_for_visible = 1;
11772
90e65f07 11773 if (! EQ (Vx_no_window_manager, Qt))
f676886a 11774 x_wm_set_window_state (f, NormalState);
3afe33e7 11775#ifdef USE_X_TOOLKIT
d7a38a2e 11776 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 11777 XtMapWidget (f->output_data.x->widget);
3afe33e7 11778#else /* not USE_X_TOOLKIT */
7f9c7f94 11779 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11780#endif /* not USE_X_TOOLKIT */
0134a210
RS
11781#if 0 /* This seems to bring back scroll bars in the wrong places
11782 if the window configuration has changed. They seem
11783 to come back ok without this. */
ab648270 11784 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
334208b7 11785 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
0134a210 11786#endif
90e65f07 11787 }
dc6f92b8 11788
334208b7 11789 XFlush (FRAME_X_DISPLAY (f));
90e65f07 11790
0dacf791
RS
11791 /* Synchronize to ensure Emacs knows the frame is visible
11792 before we do anything else. We do this loop with input not blocked
11793 so that incoming events are handled. */
11794 {
11795 Lisp_Object frame;
12ce2351 11796 int count;
28c01ffe
RS
11797 /* This must be before UNBLOCK_INPUT
11798 since events that arrive in response to the actions above
11799 will set it when they are handled. */
11800 int previously_visible = f->output_data.x->has_been_visible;
1aa6072f
RS
11801
11802 original_left = f->output_data.x->left_pos;
11803 original_top = f->output_data.x->top_pos;
c0a04927
RS
11804
11805 /* This must come after we set COUNT. */
11806 UNBLOCK_INPUT;
11807
2745e6c4 11808 /* We unblock here so that arriving X events are processed. */
1aa6072f 11809
dcb07ae9
RS
11810 /* Now move the window back to where it was "supposed to be".
11811 But don't do it if the gravity is negative.
11812 When the gravity is negative, this uses a position
28c01ffe
RS
11813 that is 3 pixels too low. Perhaps that's really the border width.
11814
11815 Don't do this if the window has never been visible before,
11816 because the window manager may choose the position
11817 and we don't want to override it. */
1aa6072f 11818
4d3f5d9a 11819 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
06c488fd 11820 && f->output_data.x->win_gravity == NorthWestGravity
28c01ffe 11821 && previously_visible)
1aa6072f 11822 {
2745e6c4
RS
11823 Drawable rootw;
11824 int x, y;
11825 unsigned int width, height, border, depth;
06a2c219 11826
1aa6072f 11827 BLOCK_INPUT;
9829ddba 11828
06a2c219
GM
11829 /* On some window managers (such as FVWM) moving an existing
11830 window, even to the same place, causes the window manager
11831 to introduce an offset. This can cause the window to move
11832 to an unexpected location. Check the geometry (a little
11833 slow here) and then verify that the window is in the right
11834 place. If the window is not in the right place, move it
11835 there, and take the potential window manager hit. */
2745e6c4
RS
11836 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11837 &rootw, &x, &y, &width, &height, &border, &depth);
11838
11839 if (original_left != x || original_top != y)
11840 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11841 original_left, original_top);
11842
1aa6072f
RS
11843 UNBLOCK_INPUT;
11844 }
9829ddba 11845
e0c1aef2 11846 XSETFRAME (frame, f);
c0a04927 11847
12ce2351
GM
11848 /* Wait until the frame is visible. Process X events until a
11849 MapNotify event has been seen, or until we think we won't get a
11850 MapNotify at all.. */
11851 for (count = input_signal_count + 10;
11852 input_signal_count < count && !FRAME_VISIBLE_P (f);)
2a6cf806 11853 {
12ce2351 11854 /* Force processing of queued events. */
334208b7 11855 x_sync (f);
12ce2351
GM
11856
11857 /* Machines that do polling rather than SIGIO have been
11858 observed to go into a busy-wait here. So we'll fake an
11859 alarm signal to let the handler know that there's something
11860 to be read. We used to raise a real alarm, but it seems
11861 that the handler isn't always enabled here. This is
11862 probably a bug. */
8b2f8d4e 11863 if (input_polling_used ())
3b2fa4e6 11864 {
12ce2351
GM
11865 /* It could be confusing if a real alarm arrives while
11866 processing the fake one. Turn it off and let the
11867 handler reset it. */
3e71d8f2 11868 extern void poll_for_input_1 P_ ((void));
bffcfca9
GM
11869 int old_poll_suppress_count = poll_suppress_count;
11870 poll_suppress_count = 1;
11871 poll_for_input_1 ();
11872 poll_suppress_count = old_poll_suppress_count;
3b2fa4e6 11873 }
12ce2351
GM
11874
11875 /* See if a MapNotify event has been processed. */
11876 FRAME_SAMPLE_VISIBILITY (f);
2a6cf806 11877 }
0dacf791 11878 }
dc6f92b8
JB
11879}
11880
06a2c219 11881/* Change from mapped state to withdrawn state. */
dc6f92b8 11882
d047c4eb
KH
11883/* Make the frame visible (mapped and not iconified). */
11884
dfcf069d 11885void
f676886a
JB
11886x_make_frame_invisible (f)
11887 struct frame *f;
dc6f92b8 11888{
546e6d5b
RS
11889 Window window;
11890
11891#ifdef USE_X_TOOLKIT
11892 /* Use the frame's outermost window, not the one we normally draw on. */
7556890b 11893 window = XtWindow (f->output_data.x->widget);
546e6d5b
RS
11894#else /* not USE_X_TOOLKIT */
11895 window = FRAME_X_WINDOW (f);
11896#endif /* not USE_X_TOOLKIT */
dc6f92b8 11897
9319ae23 11898 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
11899 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11900 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 11901
5627c40e 11902#if 0/* This might add unreliability; I don't trust it -- rms. */
9319ae23 11903 if (! f->async_visible && ! f->async_iconified)
dc6f92b8 11904 return;
5627c40e 11905#endif
dc6f92b8
JB
11906
11907 BLOCK_INPUT;
c118dd06 11908
af31d76f
RS
11909 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
11910 that the current position of the window is user-specified, rather than
11911 program-specified, so that when the window is mapped again, it will be
11912 placed at the same location, without forcing the user to position it
11913 by hand again (they have already done that once for this window.) */
c32cdd9a 11914 x_wm_set_size_hint (f, (long) 0, 1);
af31d76f 11915
c118dd06
JB
11916#ifdef HAVE_X11R4
11917
334208b7
RS
11918 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
11919 DefaultScreen (FRAME_X_DISPLAY (f))))
c118dd06
JB
11920 {
11921 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11922 error ("Can't notify window manager of window withdrawal");
c118dd06 11923 }
c118dd06 11924#else /* ! defined (HAVE_X11R4) */
16bd92ea 11925
c118dd06 11926 /* Tell the window manager what we're going to do. */
dc6f92b8
JB
11927 if (! EQ (Vx_no_window_manager, Qt))
11928 {
16bd92ea 11929 XEvent unmap;
dc6f92b8 11930
16bd92ea 11931 unmap.xunmap.type = UnmapNotify;
546e6d5b 11932 unmap.xunmap.window = window;
334208b7 11933 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
16bd92ea 11934 unmap.xunmap.from_configure = False;
334208b7
RS
11935 if (! XSendEvent (FRAME_X_DISPLAY (f),
11936 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea 11937 False,
06a2c219 11938 SubstructureRedirectMaskSubstructureNotifyMask,
16bd92ea
JB
11939 &unmap))
11940 {
11941 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11942 error ("Can't notify window manager of withdrawal");
16bd92ea 11943 }
dc6f92b8
JB
11944 }
11945
16bd92ea 11946 /* Unmap the window ourselves. Cheeky! */
334208b7 11947 XUnmapWindow (FRAME_X_DISPLAY (f), window);
c118dd06 11948#endif /* ! defined (HAVE_X11R4) */
dc6f92b8 11949
5627c40e
RS
11950 /* We can't distinguish this from iconification
11951 just by the event that we get from the server.
11952 So we can't win using the usual strategy of letting
11953 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
11954 and synchronize with the server to make sure we agree. */
11955 f->visible = 0;
11956 FRAME_ICONIFIED_P (f) = 0;
11957 f->async_visible = 0;
11958 f->async_iconified = 0;
11959
334208b7 11960 x_sync (f);
5627c40e 11961
dc6f92b8
JB
11962 UNBLOCK_INPUT;
11963}
11964
06a2c219 11965/* Change window state from mapped to iconified. */
dc6f92b8 11966
dfcf069d 11967void
f676886a
JB
11968x_iconify_frame (f)
11969 struct frame *f;
dc6f92b8 11970{
3afe33e7 11971 int result;
990ba854 11972 Lisp_Object type;
dc6f92b8 11973
9319ae23 11974 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
11975 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11976 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 11977
3a88c238 11978 if (f->async_iconified)
dc6f92b8
JB
11979 return;
11980
3afe33e7 11981 BLOCK_INPUT;
546e6d5b 11982
9af3143a
RS
11983 FRAME_SAMPLE_VISIBILITY (f);
11984
990ba854
RS
11985 type = x_icon_type (f);
11986 if (!NILP (type))
11987 x_bitmap_icon (f, type);
bdcd49ba
RS
11988
11989#ifdef USE_X_TOOLKIT
11990
546e6d5b
RS
11991 if (! FRAME_VISIBLE_P (f))
11992 {
11993 if (! EQ (Vx_no_window_manager, Qt))
11994 x_wm_set_window_state (f, IconicState);
11995 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 11996 XtMapWidget (f->output_data.x->widget);
9cf30a30
RS
11997 /* The server won't give us any event to indicate
11998 that an invisible frame was changed to an icon,
11999 so we have to record it here. */
12000 f->iconified = 1;
1e6bc770 12001 f->visible = 1;
9cf30a30 12002 f->async_iconified = 1;
1e6bc770 12003 f->async_visible = 0;
546e6d5b
RS
12004 UNBLOCK_INPUT;
12005 return;
12006 }
12007
334208b7 12008 result = XIconifyWindow (FRAME_X_DISPLAY (f),
7556890b 12009 XtWindow (f->output_data.x->widget),
334208b7 12010 DefaultScreen (FRAME_X_DISPLAY (f)));
3afe33e7
RS
12011 UNBLOCK_INPUT;
12012
12013 if (!result)
546e6d5b 12014 error ("Can't notify window manager of iconification");
3afe33e7
RS
12015
12016 f->async_iconified = 1;
1e6bc770
RS
12017 f->async_visible = 0;
12018
8c002a25
KH
12019
12020 BLOCK_INPUT;
334208b7 12021 XFlush (FRAME_X_DISPLAY (f));
8c002a25 12022 UNBLOCK_INPUT;
3afe33e7
RS
12023#else /* not USE_X_TOOLKIT */
12024
fd13dbb2
RS
12025 /* Make sure the X server knows where the window should be positioned,
12026 in case the user deiconifies with the window manager. */
12027 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
7556890b 12028 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
fd13dbb2 12029
16bd92ea
JB
12030 /* Since we don't know which revision of X we're running, we'll use both
12031 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
12032
12033 /* X11R4: send a ClientMessage to the window manager using the
12034 WM_CHANGE_STATE type. */
12035 {
12036 XEvent message;
58769bee 12037
c118dd06 12038 message.xclient.window = FRAME_X_WINDOW (f);
16bd92ea 12039 message.xclient.type = ClientMessage;
334208b7 12040 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
16bd92ea
JB
12041 message.xclient.format = 32;
12042 message.xclient.data.l[0] = IconicState;
12043
334208b7
RS
12044 if (! XSendEvent (FRAME_X_DISPLAY (f),
12045 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea
JB
12046 False,
12047 SubstructureRedirectMask | SubstructureNotifyMask,
12048 &message))
dc6f92b8
JB
12049 {
12050 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 12051 error ("Can't notify window manager of iconification");
dc6f92b8 12052 }
16bd92ea 12053 }
dc6f92b8 12054
58769bee 12055 /* X11R3: set the initial_state field of the window manager hints to
16bd92ea
JB
12056 IconicState. */
12057 x_wm_set_window_state (f, IconicState);
dc6f92b8 12058
a9c00105
RS
12059 if (!FRAME_VISIBLE_P (f))
12060 {
12061 /* If the frame was withdrawn, before, we must map it. */
7f9c7f94 12062 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
a9c00105
RS
12063 }
12064
3a88c238 12065 f->async_iconified = 1;
1e6bc770 12066 f->async_visible = 0;
dc6f92b8 12067
334208b7 12068 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 12069 UNBLOCK_INPUT;
8c002a25 12070#endif /* not USE_X_TOOLKIT */
dc6f92b8 12071}
d047c4eb 12072\f
c0ff3fab 12073/* Destroy the X window of frame F. */
dc6f92b8 12074
dfcf069d 12075void
c0ff3fab 12076x_destroy_window (f)
f676886a 12077 struct frame *f;
dc6f92b8 12078{
7f9c7f94
RS
12079 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12080
dc6f92b8 12081 BLOCK_INPUT;
c0ff3fab 12082
6186a4a0
RS
12083 /* If a display connection is dead, don't try sending more
12084 commands to the X server. */
12085 if (dpyinfo->display != 0)
12086 {
12087 if (f->output_data.x->icon_desc != 0)
12088 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
31f41daf 12089#ifdef HAVE_X_I18N
f5d11644
GM
12090 if (FRAME_XIC (f))
12091 free_frame_xic (f);
31f41daf 12092#endif
6186a4a0 12093 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->window_desc);
3afe33e7 12094#ifdef USE_X_TOOLKIT
06a2c219
GM
12095 if (f->output_data.x->widget)
12096 XtDestroyWidget (f->output_data.x->widget);
6186a4a0 12097 free_frame_menubar (f);
3afe33e7
RS
12098#endif /* USE_X_TOOLKIT */
12099
3e71d8f2
GM
12100 unload_color (f, f->output_data.x->foreground_pixel);
12101 unload_color (f, f->output_data.x->background_pixel);
12102 unload_color (f, f->output_data.x->cursor_pixel);
12103 unload_color (f, f->output_data.x->cursor_foreground_pixel);
12104 unload_color (f, f->output_data.x->border_pixel);
12105 unload_color (f, f->output_data.x->mouse_pixel);
12106 if (f->output_data.x->scroll_bar_background_pixel != -1)
12107 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
12108 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
12109 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
12110 if (f->output_data.x->white_relief.allocated_p)
12111 unload_color (f, f->output_data.x->white_relief.pixel);
12112 if (f->output_data.x->black_relief.allocated_p)
12113 unload_color (f, f->output_data.x->black_relief.pixel);
12114
6186a4a0
RS
12115 free_frame_faces (f);
12116 XFlush (FRAME_X_DISPLAY (f));
12117 }
dc6f92b8 12118
df89d8a4 12119 if (f->output_data.x->saved_menu_event)
06a2c219 12120 xfree (f->output_data.x->saved_menu_event);
0c49ce2f 12121
7556890b
RS
12122 xfree (f->output_data.x);
12123 f->output_data.x = 0;
0f941935
KH
12124 if (f == dpyinfo->x_focus_frame)
12125 dpyinfo->x_focus_frame = 0;
12126 if (f == dpyinfo->x_focus_event_frame)
12127 dpyinfo->x_focus_event_frame = 0;
12128 if (f == dpyinfo->x_highlight_frame)
12129 dpyinfo->x_highlight_frame = 0;
c0ff3fab 12130
7f9c7f94
RS
12131 dpyinfo->reference_count--;
12132
12133 if (f == dpyinfo->mouse_face_mouse_frame)
dc05a16b 12134 {
7f9c7f94
RS
12135 dpyinfo->mouse_face_beg_row
12136 = dpyinfo->mouse_face_beg_col = -1;
12137 dpyinfo->mouse_face_end_row
12138 = dpyinfo->mouse_face_end_col = -1;
12139 dpyinfo->mouse_face_window = Qnil;
21323706
RS
12140 dpyinfo->mouse_face_deferred_gc = 0;
12141 dpyinfo->mouse_face_mouse_frame = 0;
dc05a16b 12142 }
0134a210 12143
c0ff3fab 12144 UNBLOCK_INPUT;
dc6f92b8
JB
12145}
12146\f
f451eb13
JB
12147/* Setting window manager hints. */
12148
af31d76f
RS
12149/* Set the normal size hints for the window manager, for frame F.
12150 FLAGS is the flags word to use--or 0 meaning preserve the flags
12151 that the window now has.
12152 If USER_POSITION is nonzero, we set the USPosition
12153 flag (this is useful when FLAGS is 0). */
6dba1858 12154
dfcf069d 12155void
af31d76f 12156x_wm_set_size_hint (f, flags, user_position)
f676886a 12157 struct frame *f;
af31d76f
RS
12158 long flags;
12159 int user_position;
dc6f92b8
JB
12160{
12161 XSizeHints size_hints;
3afe33e7
RS
12162
12163#ifdef USE_X_TOOLKIT
7e4f2521
FP
12164 Arg al[2];
12165 int ac = 0;
12166 Dimension widget_width, widget_height;
7556890b 12167 Window window = XtWindow (f->output_data.x->widget);
3afe33e7 12168#else /* not USE_X_TOOLKIT */
c118dd06 12169 Window window = FRAME_X_WINDOW (f);
3afe33e7 12170#endif /* not USE_X_TOOLKIT */
dc6f92b8 12171
b72a58fd
RS
12172 /* Setting PMaxSize caused various problems. */
12173 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
dc6f92b8 12174
7556890b
RS
12175 size_hints.x = f->output_data.x->left_pos;
12176 size_hints.y = f->output_data.x->top_pos;
7553a6b7 12177
7e4f2521
FP
12178#ifdef USE_X_TOOLKIT
12179 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
12180 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
7556890b 12181 XtGetValues (f->output_data.x->widget, al, ac);
7e4f2521
FP
12182 size_hints.height = widget_height;
12183 size_hints.width = widget_width;
12184#else /* not USE_X_TOOLKIT */
f676886a
JB
12185 size_hints.height = PIXEL_HEIGHT (f);
12186 size_hints.width = PIXEL_WIDTH (f);
7e4f2521 12187#endif /* not USE_X_TOOLKIT */
7553a6b7 12188
7556890b
RS
12189 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
12190 size_hints.height_inc = f->output_data.x->line_height;
334208b7
RS
12191 size_hints.max_width
12192 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
12193 size_hints.max_height
12194 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
0134a210 12195
d067ea8b
KH
12196 /* Calculate the base and minimum sizes.
12197
12198 (When we use the X toolkit, we don't do it here.
12199 Instead we copy the values that the widgets are using, below.) */
12200#ifndef USE_X_TOOLKIT
b1c884c3 12201 {
b0342f17 12202 int base_width, base_height;
0134a210 12203 int min_rows = 0, min_cols = 0;
b0342f17 12204
f451eb13
JB
12205 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
12206 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
b0342f17 12207
0134a210 12208 check_frame_size (f, &min_rows, &min_cols);
b0342f17 12209
0134a210
RS
12210 /* The window manager uses the base width hints to calculate the
12211 current number of rows and columns in the frame while
12212 resizing; min_width and min_height aren't useful for this
12213 purpose, since they might not give the dimensions for a
12214 zero-row, zero-column frame.
58769bee 12215
0134a210
RS
12216 We use the base_width and base_height members if we have
12217 them; otherwise, we set the min_width and min_height members
12218 to the size for a zero x zero frame. */
b0342f17
JB
12219
12220#ifdef HAVE_X11R4
0134a210
RS
12221 size_hints.flags |= PBaseSize;
12222 size_hints.base_width = base_width;
12223 size_hints.base_height = base_height;
12224 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
12225 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
b0342f17 12226#else
0134a210
RS
12227 size_hints.min_width = base_width;
12228 size_hints.min_height = base_height;
b0342f17 12229#endif
b1c884c3 12230 }
dc6f92b8 12231
d067ea8b 12232 /* If we don't need the old flags, we don't need the old hint at all. */
af31d76f 12233 if (flags)
dc6f92b8 12234 {
d067ea8b
KH
12235 size_hints.flags |= flags;
12236 goto no_read;
12237 }
12238#endif /* not USE_X_TOOLKIT */
12239
12240 {
12241 XSizeHints hints; /* Sometimes I hate X Windows... */
12242 long supplied_return;
12243 int value;
af31d76f
RS
12244
12245#ifdef HAVE_X11R4
d067ea8b
KH
12246 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
12247 &supplied_return);
af31d76f 12248#else
d067ea8b 12249 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
af31d76f 12250#endif
58769bee 12251
d067ea8b
KH
12252#ifdef USE_X_TOOLKIT
12253 size_hints.base_height = hints.base_height;
12254 size_hints.base_width = hints.base_width;
12255 size_hints.min_height = hints.min_height;
12256 size_hints.min_width = hints.min_width;
12257#endif
12258
12259 if (flags)
12260 size_hints.flags |= flags;
12261 else
12262 {
12263 if (value == 0)
12264 hints.flags = 0;
12265 if (hints.flags & PSize)
12266 size_hints.flags |= PSize;
12267 if (hints.flags & PPosition)
12268 size_hints.flags |= PPosition;
12269 if (hints.flags & USPosition)
12270 size_hints.flags |= USPosition;
12271 if (hints.flags & USSize)
12272 size_hints.flags |= USSize;
12273 }
12274 }
12275
06a2c219 12276#ifndef USE_X_TOOLKIT
d067ea8b 12277 no_read:
06a2c219 12278#endif
0134a210 12279
af31d76f 12280#ifdef PWinGravity
7556890b 12281 size_hints.win_gravity = f->output_data.x->win_gravity;
af31d76f 12282 size_hints.flags |= PWinGravity;
dc05a16b 12283
af31d76f 12284 if (user_position)
6dba1858 12285 {
af31d76f
RS
12286 size_hints.flags &= ~ PPosition;
12287 size_hints.flags |= USPosition;
6dba1858 12288 }
2554751d 12289#endif /* PWinGravity */
6dba1858 12290
b0342f17 12291#ifdef HAVE_X11R4
334208b7 12292 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 12293#else
334208b7 12294 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 12295#endif
dc6f92b8
JB
12296}
12297
12298/* Used for IconicState or NormalState */
06a2c219 12299
dfcf069d 12300void
f676886a
JB
12301x_wm_set_window_state (f, state)
12302 struct frame *f;
dc6f92b8
JB
12303 int state;
12304{
3afe33e7 12305#ifdef USE_X_TOOLKIT
546e6d5b
RS
12306 Arg al[1];
12307
12308 XtSetArg (al[0], XtNinitialState, state);
7556890b 12309 XtSetValues (f->output_data.x->widget, al, 1);
3afe33e7 12310#else /* not USE_X_TOOLKIT */
c118dd06 12311 Window window = FRAME_X_WINDOW (f);
dc6f92b8 12312
7556890b
RS
12313 f->output_data.x->wm_hints.flags |= StateHint;
12314 f->output_data.x->wm_hints.initial_state = state;
b1c884c3 12315
7556890b 12316 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
546e6d5b 12317#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12318}
12319
dfcf069d 12320void
7f2ae036 12321x_wm_set_icon_pixmap (f, pixmap_id)
f676886a 12322 struct frame *f;
7f2ae036 12323 int pixmap_id;
dc6f92b8 12324{
d2bd6bc4
RS
12325 Pixmap icon_pixmap;
12326
06a2c219 12327#ifndef USE_X_TOOLKIT
c118dd06 12328 Window window = FRAME_X_WINDOW (f);
75231bad 12329#endif
dc6f92b8 12330
7f2ae036 12331 if (pixmap_id > 0)
dbc4e1c1 12332 {
d2bd6bc4 12333 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
7556890b 12334 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
dbc4e1c1
JB
12335 }
12336 else
68568555
RS
12337 {
12338 /* It seems there is no way to turn off use of an icon pixmap.
12339 The following line does it, only if no icon has yet been created,
12340 for some window managers. But with mwm it crashes.
12341 Some people say it should clear the IconPixmapHint bit in this case,
12342 but that doesn't work, and the X consortium said it isn't the
12343 right thing at all. Since there is no way to win,
12344 best to explicitly give up. */
12345#if 0
12346 f->output_data.x->wm_hints.icon_pixmap = None;
12347#else
12348 return;
12349#endif
12350 }
b1c884c3 12351
d2bd6bc4
RS
12352#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
12353
12354 {
12355 Arg al[1];
12356 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12357 XtSetValues (f->output_data.x->widget, al, 1);
12358 }
12359
12360#else /* not USE_X_TOOLKIT */
12361
7556890b
RS
12362 f->output_data.x->wm_hints.flags |= IconPixmapHint;
12363 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
d2bd6bc4
RS
12364
12365#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12366}
12367
dfcf069d 12368void
f676886a
JB
12369x_wm_set_icon_position (f, icon_x, icon_y)
12370 struct frame *f;
dc6f92b8
JB
12371 int icon_x, icon_y;
12372{
75231bad 12373#ifdef USE_X_TOOLKIT
7556890b 12374 Window window = XtWindow (f->output_data.x->widget);
75231bad 12375#else
c118dd06 12376 Window window = FRAME_X_WINDOW (f);
75231bad 12377#endif
dc6f92b8 12378
7556890b
RS
12379 f->output_data.x->wm_hints.flags |= IconPositionHint;
12380 f->output_data.x->wm_hints.icon_x = icon_x;
12381 f->output_data.x->wm_hints.icon_y = icon_y;
b1c884c3 12382
7556890b 12383 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
dc6f92b8
JB
12384}
12385
12386\f
06a2c219
GM
12387/***********************************************************************
12388 Fonts
12389 ***********************************************************************/
dc43ef94
KH
12390
12391/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
06a2c219 12392
dc43ef94
KH
12393struct font_info *
12394x_get_font_info (f, font_idx)
12395 FRAME_PTR f;
12396 int font_idx;
12397{
12398 return (FRAME_X_FONT_TABLE (f) + font_idx);
12399}
12400
12401
12402/* Return a list of names of available fonts matching PATTERN on frame
12403 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
12404 to be listed. Frame F NULL means we have not yet created any
12405 frame on X, and consult the first display in x_display_list.
12406 MAXNAMES sets a limit on how many fonts to match. */
12407
12408Lisp_Object
12409x_list_fonts (f, pattern, size, maxnames)
12410 FRAME_PTR f;
12411 Lisp_Object pattern;
12412 int size;
12413 int maxnames;
12414{
06a2c219
GM
12415 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
12416 Lisp_Object tem, second_best;
dc43ef94 12417 Display *dpy = f != NULL ? FRAME_X_DISPLAY (f) : x_display_list->display;
09c6077f 12418 int try_XLoadQueryFont = 0;
53ca4657 12419 int count;
dc43ef94 12420
6b0efe73 12421 patterns = Fassoc (pattern, Valternate_fontname_alist);
2da424f1
KH
12422 if (NILP (patterns))
12423 patterns = Fcons (pattern, Qnil);
81ba44e5 12424
09c6077f
KH
12425 if (maxnames == 1 && !size)
12426 /* We can return any single font matching PATTERN. */
12427 try_XLoadQueryFont = 1;
9a32686f 12428
8e713be6 12429 for (; CONSP (patterns); patterns = XCDR (patterns))
dc43ef94 12430 {
dc43ef94 12431 int num_fonts;
3e71d8f2 12432 char **names = NULL;
dc43ef94 12433
8e713be6 12434 pattern = XCAR (patterns);
536f4067
RS
12435 /* See if we cached the result for this particular query.
12436 The cache is an alist of the form:
12437 (((PATTERN . MAXNAMES) (FONTNAME . WIDTH) ...) ...)
12438 */
8e713be6 12439 if (f && (tem = XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element),
b5210ea7
KH
12440 key = Fcons (pattern, make_number (maxnames)),
12441 !NILP (list = Fassoc (key, tem))))
12442 {
12443 list = Fcdr_safe (list);
12444 /* We have a cashed list. Don't have to get the list again. */
12445 goto label_cached;
12446 }
12447
12448 /* At first, put PATTERN in the cache. */
09c6077f 12449
dc43ef94 12450 BLOCK_INPUT;
17d85edc
KH
12451 count = x_catch_errors (dpy);
12452
09c6077f
KH
12453 if (try_XLoadQueryFont)
12454 {
12455 XFontStruct *font;
12456 unsigned long value;
12457
12458 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
17d85edc
KH
12459 if (x_had_errors_p (dpy))
12460 {
12461 /* This error is perhaps due to insufficient memory on X
12462 server. Let's just ignore it. */
12463 font = NULL;
12464 x_clear_errors (dpy);
12465 }
12466
09c6077f
KH
12467 if (font
12468 && XGetFontProperty (font, XA_FONT, &value))
12469 {
12470 char *name = (char *) XGetAtomName (dpy, (Atom) value);
12471 int len = strlen (name);
01c752b5 12472 char *tmp;
09c6077f 12473
6f6512e8
KH
12474 /* If DXPC (a Differential X Protocol Compressor)
12475 Ver.3.7 is running, XGetAtomName will return null
12476 string. We must avoid such a name. */
12477 if (len == 0)
12478 try_XLoadQueryFont = 0;
12479 else
12480 {
12481 num_fonts = 1;
12482 names = (char **) alloca (sizeof (char *));
12483 /* Some systems only allow alloca assigned to a
12484 simple var. */
12485 tmp = (char *) alloca (len + 1); names[0] = tmp;
12486 bcopy (name, names[0], len + 1);
12487 XFree (name);
12488 }
09c6077f
KH
12489 }
12490 else
12491 try_XLoadQueryFont = 0;
a083fd23
RS
12492
12493 if (font)
12494 XFreeFont (dpy, font);
09c6077f
KH
12495 }
12496
12497 if (!try_XLoadQueryFont)
17d85edc
KH
12498 {
12499 /* We try at least 10 fonts because XListFonts will return
12500 auto-scaled fonts at the head. */
12501 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
12502 &num_fonts);
12503 if (x_had_errors_p (dpy))
12504 {
12505 /* This error is perhaps due to insufficient memory on X
12506 server. Let's just ignore it. */
12507 names = NULL;
12508 x_clear_errors (dpy);
12509 }
12510 }
12511
12512 x_uncatch_errors (dpy, count);
dc43ef94
KH
12513 UNBLOCK_INPUT;
12514
12515 if (names)
12516 {
12517 int i;
dc43ef94
KH
12518
12519 /* Make a list of all the fonts we got back.
12520 Store that in the font cache for the display. */
12521 for (i = 0; i < num_fonts; i++)
12522 {
06a2c219 12523 int width = 0;
dc43ef94 12524 char *p = names[i];
06a2c219
GM
12525 int average_width = -1, dashes = 0;
12526
dc43ef94 12527 /* Count the number of dashes in NAMES[I]. If there are
b5210ea7
KH
12528 14 dashes, and the field value following 12th dash
12529 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
12530 is usually too ugly to be used for editing. Let's
12531 ignore it. */
dc43ef94
KH
12532 while (*p)
12533 if (*p++ == '-')
12534 {
12535 dashes++;
12536 if (dashes == 7) /* PIXEL_SIZE field */
12537 width = atoi (p);
12538 else if (dashes == 12) /* AVERAGE_WIDTH field */
12539 average_width = atoi (p);
12540 }
12541 if (dashes < 14 || average_width != 0)
12542 {
12543 tem = build_string (names[i]);
12544 if (NILP (Fassoc (tem, list)))
12545 {
12546 if (STRINGP (Vx_pixel_size_width_font_regexp)
f39db7ea
RS
12547 && ((fast_c_string_match_ignore_case
12548 (Vx_pixel_size_width_font_regexp, names[i]))
dc43ef94
KH
12549 >= 0))
12550 /* We can set the value of PIXEL_SIZE to the
b5210ea7 12551 width of this font. */
dc43ef94
KH
12552 list = Fcons (Fcons (tem, make_number (width)), list);
12553 else
12554 /* For the moment, width is not known. */
12555 list = Fcons (Fcons (tem, Qnil), list);
12556 }
12557 }
12558 }
09c6077f
KH
12559 if (!try_XLoadQueryFont)
12560 XFreeFontNames (names);
dc43ef94
KH
12561 }
12562
b5210ea7 12563 /* Now store the result in the cache. */
dc43ef94 12564 if (f != NULL)
8e713be6 12565 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element)
dc43ef94 12566 = Fcons (Fcons (key, list),
8e713be6 12567 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
dc43ef94 12568
b5210ea7
KH
12569 label_cached:
12570 if (NILP (list)) continue; /* Try the remaining alternatives. */
dc43ef94 12571
b5210ea7
KH
12572 newlist = second_best = Qnil;
12573 /* Make a list of the fonts that have the right width. */
8e713be6 12574 for (; CONSP (list); list = XCDR (list))
b5210ea7 12575 {
536f4067
RS
12576 int found_size;
12577
8e713be6 12578 tem = XCAR (list);
dc43ef94 12579
8e713be6 12580 if (!CONSP (tem) || NILP (XCAR (tem)))
b5210ea7
KH
12581 continue;
12582 if (!size)
12583 {
8e713be6 12584 newlist = Fcons (XCAR (tem), newlist);
b5210ea7
KH
12585 continue;
12586 }
dc43ef94 12587
8e713be6 12588 if (!INTEGERP (XCDR (tem)))
dc43ef94 12589 {
b5210ea7
KH
12590 /* Since we have not yet known the size of this font, we
12591 must try slow function call XLoadQueryFont. */
dc43ef94
KH
12592 XFontStruct *thisinfo;
12593
12594 BLOCK_INPUT;
17d85edc 12595 count = x_catch_errors (dpy);
dc43ef94 12596 thisinfo = XLoadQueryFont (dpy,
8e713be6 12597 XSTRING (XCAR (tem))->data);
17d85edc
KH
12598 if (x_had_errors_p (dpy))
12599 {
12600 /* This error is perhaps due to insufficient memory on X
12601 server. Let's just ignore it. */
12602 thisinfo = NULL;
12603 x_clear_errors (dpy);
12604 }
12605 x_uncatch_errors (dpy, count);
dc43ef94
KH
12606 UNBLOCK_INPUT;
12607
12608 if (thisinfo)
12609 {
8e713be6 12610 XCDR (tem)
536f4067
RS
12611 = (thisinfo->min_bounds.width == 0
12612 ? make_number (0)
12613 : make_number (thisinfo->max_bounds.width));
dc43ef94
KH
12614 XFreeFont (dpy, thisinfo);
12615 }
12616 else
b5210ea7 12617 /* For unknown reason, the previous call of XListFont had
06a2c219 12618 returned a font which can't be opened. Record the size
b5210ea7 12619 as 0 not to try to open it again. */
8e713be6 12620 XCDR (tem) = make_number (0);
dc43ef94 12621 }
536f4067 12622
8e713be6 12623 found_size = XINT (XCDR (tem));
536f4067 12624 if (found_size == size)
8e713be6 12625 newlist = Fcons (XCAR (tem), newlist);
536f4067 12626 else if (found_size > 0)
b5210ea7 12627 {
536f4067 12628 if (NILP (second_best))
b5210ea7 12629 second_best = tem;
536f4067
RS
12630 else if (found_size < size)
12631 {
8e713be6
KR
12632 if (XINT (XCDR (second_best)) > size
12633 || XINT (XCDR (second_best)) < found_size)
536f4067
RS
12634 second_best = tem;
12635 }
12636 else
12637 {
8e713be6
KR
12638 if (XINT (XCDR (second_best)) > size
12639 && XINT (XCDR (second_best)) > found_size)
536f4067
RS
12640 second_best = tem;
12641 }
b5210ea7
KH
12642 }
12643 }
12644 if (!NILP (newlist))
12645 break;
12646 else if (!NILP (second_best))
12647 {
8e713be6 12648 newlist = Fcons (XCAR (second_best), Qnil);
b5210ea7 12649 break;
dc43ef94 12650 }
dc43ef94
KH
12651 }
12652
12653 return newlist;
12654}
12655
06a2c219
GM
12656
12657#if GLYPH_DEBUG
12658
12659/* Check that FONT is valid on frame F. It is if it can be found in F's
12660 font table. */
12661
12662static void
12663x_check_font (f, font)
12664 struct frame *f;
12665 XFontStruct *font;
12666{
12667 int i;
12668 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12669
12670 xassert (font != NULL);
12671
12672 for (i = 0; i < dpyinfo->n_fonts; i++)
12673 if (dpyinfo->font_table[i].name
12674 && font == dpyinfo->font_table[i].font)
12675 break;
12676
12677 xassert (i < dpyinfo->n_fonts);
12678}
12679
12680#endif /* GLYPH_DEBUG != 0 */
12681
12682/* Set *W to the minimum width, *H to the minimum font height of FONT.
12683 Note: There are (broken) X fonts out there with invalid XFontStruct
12684 min_bounds contents. For example, handa@etl.go.jp reports that
12685 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
12686 have font->min_bounds.width == 0. */
12687
12688static INLINE void
12689x_font_min_bounds (font, w, h)
12690 XFontStruct *font;
12691 int *w, *h;
12692{
12693 *h = FONT_HEIGHT (font);
12694 *w = font->min_bounds.width;
12695
12696 /* Try to handle the case where FONT->min_bounds has invalid
12697 contents. Since the only font known to have invalid min_bounds
12698 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
12699 if (*w <= 0)
12700 *w = font->max_bounds.width;
12701}
12702
12703
12704/* Compute the smallest character width and smallest font height over
12705 all fonts available on frame F. Set the members smallest_char_width
12706 and smallest_font_height in F's x_display_info structure to
12707 the values computed. Value is non-zero if smallest_font_height or
12708 smallest_char_width become smaller than they were before. */
12709
12710static int
12711x_compute_min_glyph_bounds (f)
12712 struct frame *f;
12713{
12714 int i;
12715 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12716 XFontStruct *font;
12717 int old_width = dpyinfo->smallest_char_width;
12718 int old_height = dpyinfo->smallest_font_height;
12719
12720 dpyinfo->smallest_font_height = 100000;
12721 dpyinfo->smallest_char_width = 100000;
12722
12723 for (i = 0; i < dpyinfo->n_fonts; ++i)
12724 if (dpyinfo->font_table[i].name)
12725 {
12726 struct font_info *fontp = dpyinfo->font_table + i;
12727 int w, h;
12728
12729 font = (XFontStruct *) fontp->font;
12730 xassert (font != (XFontStruct *) ~0);
12731 x_font_min_bounds (font, &w, &h);
12732
12733 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
12734 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
12735 }
12736
12737 xassert (dpyinfo->smallest_char_width > 0
12738 && dpyinfo->smallest_font_height > 0);
12739
12740 return (dpyinfo->n_fonts == 1
12741 || dpyinfo->smallest_char_width < old_width
12742 || dpyinfo->smallest_font_height < old_height);
12743}
12744
12745
dc43ef94
KH
12746/* Load font named FONTNAME of the size SIZE for frame F, and return a
12747 pointer to the structure font_info while allocating it dynamically.
12748 If SIZE is 0, load any size of font.
12749 If loading is failed, return NULL. */
12750
12751struct font_info *
12752x_load_font (f, fontname, size)
12753 struct frame *f;
12754 register char *fontname;
12755 int size;
12756{
12757 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12758 Lisp_Object font_names;
d645aaa4 12759 int count;
dc43ef94
KH
12760
12761 /* Get a list of all the fonts that match this name. Once we
12762 have a list of matching fonts, we compare them against the fonts
12763 we already have by comparing names. */
09c6077f 12764 font_names = x_list_fonts (f, build_string (fontname), size, 1);
dc43ef94
KH
12765
12766 if (!NILP (font_names))
12767 {
12768 Lisp_Object tail;
12769 int i;
12770
12771 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 12772 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
06a2c219
GM
12773 if (dpyinfo->font_table[i].name
12774 && (!strcmp (dpyinfo->font_table[i].name,
8e713be6 12775 XSTRING (XCAR (tail))->data)
06a2c219 12776 || !strcmp (dpyinfo->font_table[i].full_name,
8e713be6 12777 XSTRING (XCAR (tail))->data)))
dc43ef94
KH
12778 return (dpyinfo->font_table + i);
12779 }
12780
12781 /* Load the font and add it to the table. */
12782 {
12783 char *full_name;
12784 XFontStruct *font;
12785 struct font_info *fontp;
12786 unsigned long value;
06a2c219 12787 int i;
dc43ef94 12788
2da424f1
KH
12789 /* If we have found fonts by x_list_font, load one of them. If
12790 not, we still try to load a font by the name given as FONTNAME
12791 because XListFonts (called in x_list_font) of some X server has
12792 a bug of not finding a font even if the font surely exists and
12793 is loadable by XLoadQueryFont. */
e1d6d5b9 12794 if (size > 0 && !NILP (font_names))
8e713be6 12795 fontname = (char *) XSTRING (XCAR (font_names))->data;
dc43ef94
KH
12796
12797 BLOCK_INPUT;
d645aaa4 12798 count = x_catch_errors (FRAME_X_DISPLAY (f));
dc43ef94 12799 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
d645aaa4
KH
12800 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12801 {
12802 /* This error is perhaps due to insufficient memory on X
12803 server. Let's just ignore it. */
12804 font = NULL;
12805 x_clear_errors (FRAME_X_DISPLAY (f));
12806 }
12807 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
dc43ef94 12808 UNBLOCK_INPUT;
b5210ea7 12809 if (!font)
dc43ef94
KH
12810 return NULL;
12811
06a2c219
GM
12812 /* Find a free slot in the font table. */
12813 for (i = 0; i < dpyinfo->n_fonts; ++i)
12814 if (dpyinfo->font_table[i].name == NULL)
12815 break;
12816
12817 /* If no free slot found, maybe enlarge the font table. */
12818 if (i == dpyinfo->n_fonts
12819 && dpyinfo->n_fonts == dpyinfo->font_table_size)
dc43ef94 12820 {
06a2c219
GM
12821 int sz;
12822 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
12823 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
dc43ef94 12824 dpyinfo->font_table
06a2c219 12825 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
dc43ef94
KH
12826 }
12827
06a2c219
GM
12828 fontp = dpyinfo->font_table + i;
12829 if (i == dpyinfo->n_fonts)
12830 ++dpyinfo->n_fonts;
dc43ef94
KH
12831
12832 /* Now fill in the slots of *FONTP. */
12833 BLOCK_INPUT;
12834 fontp->font = font;
06a2c219 12835 fontp->font_idx = i;
dc43ef94
KH
12836 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
12837 bcopy (fontname, fontp->name, strlen (fontname) + 1);
12838
12839 /* Try to get the full name of FONT. Put it in FULL_NAME. */
12840 full_name = 0;
12841 if (XGetFontProperty (font, XA_FONT, &value))
12842 {
12843 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
12844 char *p = name;
12845 int dashes = 0;
12846
12847 /* Count the number of dashes in the "full name".
12848 If it is too few, this isn't really the font's full name,
12849 so don't use it.
12850 In X11R4, the fonts did not come with their canonical names
12851 stored in them. */
12852 while (*p)
12853 {
12854 if (*p == '-')
12855 dashes++;
12856 p++;
12857 }
12858
12859 if (dashes >= 13)
12860 {
12861 full_name = (char *) xmalloc (p - name + 1);
12862 bcopy (name, full_name, p - name + 1);
12863 }
12864
12865 XFree (name);
12866 }
12867
12868 if (full_name != 0)
12869 fontp->full_name = full_name;
12870 else
12871 fontp->full_name = fontp->name;
12872
12873 fontp->size = font->max_bounds.width;
d5749adb
KH
12874 fontp->height = FONT_HEIGHT (font);
12875 {
12876 /* For some font, ascent and descent in max_bounds field is
12877 larger than the above value. */
12878 int max_height = font->max_bounds.ascent + font->max_bounds.descent;
12879 if (max_height > fontp->height)
74848a96 12880 fontp->height = max_height;
d5749adb 12881 }
dc43ef94 12882
2da424f1
KH
12883 if (NILP (font_names))
12884 {
12885 /* We come here because of a bug of XListFonts mentioned at
12886 the head of this block. Let's store this information in
12887 the cache for x_list_fonts. */
12888 Lisp_Object lispy_name = build_string (fontname);
12889 Lisp_Object lispy_full_name = build_string (fontp->full_name);
12890
8e713be6 12891 XCDR (dpyinfo->name_list_element)
2da424f1
KH
12892 = Fcons (Fcons (Fcons (lispy_name, make_number (256)),
12893 Fcons (Fcons (lispy_full_name,
12894 make_number (fontp->size)),
12895 Qnil)),
8e713be6 12896 XCDR (dpyinfo->name_list_element));
2da424f1 12897 if (full_name)
8e713be6 12898 XCDR (dpyinfo->name_list_element)
2da424f1
KH
12899 = Fcons (Fcons (Fcons (lispy_full_name, make_number (256)),
12900 Fcons (Fcons (lispy_full_name,
12901 make_number (fontp->size)),
12902 Qnil)),
8e713be6 12903 XCDR (dpyinfo->name_list_element));
2da424f1
KH
12904 }
12905
dc43ef94
KH
12906 /* The slot `encoding' specifies how to map a character
12907 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
ee569018
KH
12908 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
12909 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
8ff102bd 12910 2:0xA020..0xFF7F). For the moment, we don't know which charset
06a2c219 12911 uses this font. So, we set information in fontp->encoding[1]
8ff102bd
RS
12912 which is never used by any charset. If mapping can't be
12913 decided, set FONT_ENCODING_NOT_DECIDED. */
dc43ef94
KH
12914 fontp->encoding[1]
12915 = (font->max_byte1 == 0
12916 /* 1-byte font */
12917 ? (font->min_char_or_byte2 < 0x80
12918 ? (font->max_char_or_byte2 < 0x80
12919 ? 0 /* 0x20..0x7F */
8ff102bd 12920 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
dc43ef94
KH
12921 : 1) /* 0xA0..0xFF */
12922 /* 2-byte font */
12923 : (font->min_byte1 < 0x80
12924 ? (font->max_byte1 < 0x80
12925 ? (font->min_char_or_byte2 < 0x80
12926 ? (font->max_char_or_byte2 < 0x80
12927 ? 0 /* 0x2020..0x7F7F */
8ff102bd 12928 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
dc43ef94 12929 : 3) /* 0x20A0..0x7FFF */
8ff102bd 12930 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
dc43ef94
KH
12931 : (font->min_char_or_byte2 < 0x80
12932 ? (font->max_char_or_byte2 < 0x80
12933 ? 2 /* 0xA020..0xFF7F */
8ff102bd 12934 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
dc43ef94
KH
12935 : 1))); /* 0xA0A0..0xFFFF */
12936
12937 fontp->baseline_offset
12938 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
12939 ? (long) value : 0);
12940 fontp->relative_compose
12941 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
12942 ? (long) value : 0);
f78798df
KH
12943 fontp->default_ascent
12944 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
12945 ? (long) value : 0);
dc43ef94 12946
06a2c219
GM
12947 /* Set global flag fonts_changed_p to non-zero if the font loaded
12948 has a character with a smaller width than any other character
12949 before, or if the font loaded has a smalle>r height than any
12950 other font loaded before. If this happens, it will make a
12951 glyph matrix reallocation necessary. */
12952 fonts_changed_p = x_compute_min_glyph_bounds (f);
dc43ef94 12953 UNBLOCK_INPUT;
dc43ef94
KH
12954 return fontp;
12955 }
12956}
12957
06a2c219
GM
12958
12959/* Return a pointer to struct font_info of a font named FONTNAME for
12960 frame F. If no such font is loaded, return NULL. */
12961
dc43ef94
KH
12962struct font_info *
12963x_query_font (f, fontname)
12964 struct frame *f;
12965 register char *fontname;
12966{
12967 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12968 int i;
12969
12970 for (i = 0; i < dpyinfo->n_fonts; i++)
06a2c219
GM
12971 if (dpyinfo->font_table[i].name
12972 && (!strcmp (dpyinfo->font_table[i].name, fontname)
12973 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
dc43ef94
KH
12974 return (dpyinfo->font_table + i);
12975 return NULL;
12976}
12977
06a2c219
GM
12978
12979/* Find a CCL program for a font specified by FONTP, and set the member
a6582676
KH
12980 `encoder' of the structure. */
12981
12982void
12983x_find_ccl_program (fontp)
12984 struct font_info *fontp;
12985{
a42f54e6 12986 Lisp_Object list, elt;
a6582676 12987
8e713be6 12988 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
a6582676 12989 {
8e713be6 12990 elt = XCAR (list);
a6582676 12991 if (CONSP (elt)
8e713be6
KR
12992 && STRINGP (XCAR (elt))
12993 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
a6582676 12994 >= 0))
a42f54e6
KH
12995 break;
12996 }
12997 if (! NILP (list))
12998 {
d27f8ca7
KH
12999 struct ccl_program *ccl
13000 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
a42f54e6 13001
8e713be6 13002 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
a42f54e6
KH
13003 xfree (ccl);
13004 else
13005 fontp->font_encoder = ccl;
a6582676
KH
13006 }
13007}
13008
06a2c219 13009
dc43ef94 13010\f
06a2c219
GM
13011/***********************************************************************
13012 Initialization
13013 ***********************************************************************/
f451eb13 13014
3afe33e7
RS
13015#ifdef USE_X_TOOLKIT
13016static XrmOptionDescRec emacs_options[] = {
13017 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
13018 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
13019
13020 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
13021 XrmoptionSepArg, NULL},
13022 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
13023
13024 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13025 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13026 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13027 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13028 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13029 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
13030 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
13031};
13032#endif /* USE_X_TOOLKIT */
13033
7a13e894
RS
13034static int x_initialized;
13035
29b38361
KH
13036#ifdef MULTI_KBOARD
13037/* Test whether two display-name strings agree up to the dot that separates
13038 the screen number from the server number. */
13039static int
13040same_x_server (name1, name2)
13041 char *name1, *name2;
13042{
13043 int seen_colon = 0;
cf591cc1
RS
13044 unsigned char *system_name = XSTRING (Vsystem_name)->data;
13045 int system_name_length = strlen (system_name);
13046 int length_until_period = 0;
13047
13048 while (system_name[length_until_period] != 0
13049 && system_name[length_until_period] != '.')
13050 length_until_period++;
13051
13052 /* Treat `unix' like an empty host name. */
13053 if (! strncmp (name1, "unix:", 5))
13054 name1 += 4;
13055 if (! strncmp (name2, "unix:", 5))
13056 name2 += 4;
13057 /* Treat this host's name like an empty host name. */
13058 if (! strncmp (name1, system_name, system_name_length)
13059 && name1[system_name_length] == ':')
13060 name1 += system_name_length;
13061 if (! strncmp (name2, system_name, system_name_length)
13062 && name2[system_name_length] == ':')
13063 name2 += system_name_length;
13064 /* Treat this host's domainless name like an empty host name. */
13065 if (! strncmp (name1, system_name, length_until_period)
13066 && name1[length_until_period] == ':')
13067 name1 += length_until_period;
13068 if (! strncmp (name2, system_name, length_until_period)
13069 && name2[length_until_period] == ':')
13070 name2 += length_until_period;
13071
29b38361
KH
13072 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
13073 {
13074 if (*name1 == ':')
13075 seen_colon++;
13076 if (seen_colon && *name1 == '.')
13077 return 1;
13078 }
13079 return (seen_colon
13080 && (*name1 == '.' || *name1 == '\0')
13081 && (*name2 == '.' || *name2 == '\0'));
13082}
13083#endif
13084
334208b7 13085struct x_display_info *
1f8255f2 13086x_term_init (display_name, xrm_option, resource_name)
334208b7 13087 Lisp_Object display_name;
1f8255f2
RS
13088 char *xrm_option;
13089 char *resource_name;
dc6f92b8 13090{
334208b7 13091 int connection;
7a13e894 13092 Display *dpy;
334208b7
RS
13093 struct x_display_info *dpyinfo;
13094 XrmDatabase xrdb;
13095
60439948
KH
13096 BLOCK_INPUT;
13097
7a13e894
RS
13098 if (!x_initialized)
13099 {
13100 x_initialize ();
13101 x_initialized = 1;
13102 }
dc6f92b8 13103
3afe33e7 13104#ifdef USE_X_TOOLKIT
2d7fc7e8
RS
13105 /* weiner@footloose.sps.mot.com reports that this causes
13106 errors with X11R5:
13107 X protocol error: BadAtom (invalid Atom parameter)
13108 on protocol request 18skiloaf.
13109 So let's not use it until R6. */
13110#ifdef HAVE_X11XTR6
bdcd49ba
RS
13111 XtSetLanguageProc (NULL, NULL, NULL);
13112#endif
13113
7f9c7f94
RS
13114 {
13115 int argc = 0;
13116 char *argv[3];
13117
13118 argv[0] = "";
13119 argc = 1;
13120 if (xrm_option)
13121 {
13122 argv[argc++] = "-xrm";
13123 argv[argc++] = xrm_option;
13124 }
13125 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
13126 resource_name, EMACS_CLASS,
13127 emacs_options, XtNumber (emacs_options),
13128 &argc, argv);
39d8bb4d
KH
13129
13130#ifdef HAVE_X11XTR6
10537cb1 13131 /* I think this is to compensate for XtSetLanguageProc. */
71f8198a 13132 fixup_locale ();
39d8bb4d 13133#endif
7f9c7f94 13134 }
3afe33e7
RS
13135
13136#else /* not USE_X_TOOLKIT */
bdcd49ba
RS
13137#ifdef HAVE_X11R5
13138 XSetLocaleModifiers ("");
13139#endif
7a13e894 13140 dpy = XOpenDisplay (XSTRING (display_name)->data);
3afe33e7 13141#endif /* not USE_X_TOOLKIT */
334208b7 13142
7a13e894
RS
13143 /* Detect failure. */
13144 if (dpy == 0)
60439948
KH
13145 {
13146 UNBLOCK_INPUT;
13147 return 0;
13148 }
7a13e894
RS
13149
13150 /* We have definitely succeeded. Record the new connection. */
13151
13152 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
13153
29b38361
KH
13154#ifdef MULTI_KBOARD
13155 {
13156 struct x_display_info *share;
13157 Lisp_Object tail;
13158
13159 for (share = x_display_list, tail = x_display_name_list; share;
8e713be6
KR
13160 share = share->next, tail = XCDR (tail))
13161 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
29b38361
KH
13162 XSTRING (display_name)->data))
13163 break;
13164 if (share)
13165 dpyinfo->kboard = share->kboard;
13166 else
13167 {
13168 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
13169 init_kboard (dpyinfo->kboard);
59e755be
KH
13170 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
13171 {
13172 char *vendor = ServerVendor (dpy);
9b6ed9f3 13173 UNBLOCK_INPUT;
59e755be
KH
13174 dpyinfo->kboard->Vsystem_key_alist
13175 = call1 (Qvendor_specific_keysyms,
13176 build_string (vendor ? vendor : ""));
9b6ed9f3 13177 BLOCK_INPUT;
59e755be
KH
13178 }
13179
29b38361
KH
13180 dpyinfo->kboard->next_kboard = all_kboards;
13181 all_kboards = dpyinfo->kboard;
0ad5446c
KH
13182 /* Don't let the initial kboard remain current longer than necessary.
13183 That would cause problems if a file loaded on startup tries to
06a2c219 13184 prompt in the mini-buffer. */
0ad5446c
KH
13185 if (current_kboard == initial_kboard)
13186 current_kboard = dpyinfo->kboard;
29b38361
KH
13187 }
13188 dpyinfo->kboard->reference_count++;
13189 }
b9737ad3
KH
13190#endif
13191
7a13e894
RS
13192 /* Put this display on the chain. */
13193 dpyinfo->next = x_display_list;
13194 x_display_list = dpyinfo;
13195
13196 /* Put it on x_display_name_list as well, to keep them parallel. */
13197 x_display_name_list = Fcons (Fcons (display_name, Qnil),
13198 x_display_name_list);
8e713be6 13199 dpyinfo->name_list_element = XCAR (x_display_name_list);
7a13e894
RS
13200
13201 dpyinfo->display = dpy;
dc6f92b8 13202
dc6f92b8 13203#if 0
7a13e894 13204 XSetAfterFunction (x_current_display, x_trace_wire);
c118dd06 13205#endif /* ! 0 */
7a13e894
RS
13206
13207 dpyinfo->x_id_name
fc932ac6
RS
13208 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
13209 + STRING_BYTES (XSTRING (Vsystem_name))
7a13e894
RS
13210 + 2);
13211 sprintf (dpyinfo->x_id_name, "%s@%s",
13212 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
28430d3c
JB
13213
13214 /* Figure out which modifier bits mean what. */
334208b7 13215 x_find_modifier_meanings (dpyinfo);
f451eb13 13216
ab648270 13217 /* Get the scroll bar cursor. */
7a13e894 13218 dpyinfo->vertical_scroll_bar_cursor
334208b7 13219 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
f451eb13 13220
334208b7
RS
13221 xrdb = x_load_resources (dpyinfo->display, xrm_option,
13222 resource_name, EMACS_CLASS);
13223#ifdef HAVE_XRMSETDATABASE
13224 XrmSetDatabase (dpyinfo->display, xrdb);
13225#else
13226 dpyinfo->display->db = xrdb;
13227#endif
547d9db8 13228 /* Put the rdb where we can find it in a way that works on
7a13e894
RS
13229 all versions. */
13230 dpyinfo->xrdb = xrdb;
334208b7
RS
13231
13232 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
13233 DefaultScreen (dpyinfo->display));
5ff67d81 13234 select_visual (dpyinfo);
43bd1b2b 13235 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
334208b7
RS
13236 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
13237 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
13238 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
13239 dpyinfo->grabbed = 0;
13240 dpyinfo->reference_count = 0;
13241 dpyinfo->icon_bitmap_id = -1;
06a2c219 13242 dpyinfo->font_table = NULL;
7a13e894
RS
13243 dpyinfo->n_fonts = 0;
13244 dpyinfo->font_table_size = 0;
13245 dpyinfo->bitmaps = 0;
13246 dpyinfo->bitmaps_size = 0;
13247 dpyinfo->bitmaps_last = 0;
13248 dpyinfo->scratch_cursor_gc = 0;
13249 dpyinfo->mouse_face_mouse_frame = 0;
13250 dpyinfo->mouse_face_deferred_gc = 0;
13251 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
13252 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
06a2c219 13253 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
7a13e894
RS
13254 dpyinfo->mouse_face_window = Qnil;
13255 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
13256 dpyinfo->mouse_face_defer = 0;
0f941935
KH
13257 dpyinfo->x_focus_frame = 0;
13258 dpyinfo->x_focus_event_frame = 0;
13259 dpyinfo->x_highlight_frame = 0;
06a2c219 13260 dpyinfo->image_cache = make_image_cache ();
334208b7 13261
43bd1b2b 13262 /* See if a private colormap is requested. */
5ff67d81
GM
13263 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
13264 {
13265 if (dpyinfo->visual->class == PseudoColor)
13266 {
13267 Lisp_Object value;
13268 value = display_x_get_resource (dpyinfo,
13269 build_string ("privateColormap"),
13270 build_string ("PrivateColormap"),
13271 Qnil, Qnil);
13272 if (STRINGP (value)
13273 && (!strcmp (XSTRING (value)->data, "true")
13274 || !strcmp (XSTRING (value)->data, "on")))
13275 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
13276 }
43bd1b2b 13277 }
5ff67d81
GM
13278 else
13279 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
13280 dpyinfo->visual, AllocNone);
43bd1b2b 13281
06a2c219
GM
13282 {
13283 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
13284 double pixels = DisplayHeight (dpyinfo->display, screen_number);
13285 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
13286 dpyinfo->resy = pixels * 25.4 / mm;
13287 pixels = DisplayWidth (dpyinfo->display, screen_number);
13288 mm = DisplayWidthMM (dpyinfo->display, screen_number);
13289 dpyinfo->resx = pixels * 25.4 / mm;
13290 }
13291
334208b7
RS
13292 dpyinfo->Xatom_wm_protocols
13293 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
13294 dpyinfo->Xatom_wm_take_focus
13295 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
13296 dpyinfo->Xatom_wm_save_yourself
13297 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
13298 dpyinfo->Xatom_wm_delete_window
13299 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
13300 dpyinfo->Xatom_wm_change_state
13301 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
13302 dpyinfo->Xatom_wm_configure_denied
13303 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
13304 dpyinfo->Xatom_wm_window_moved
13305 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
13306 dpyinfo->Xatom_editres
13307 = XInternAtom (dpyinfo->display, "Editres", False);
13308 dpyinfo->Xatom_CLIPBOARD
13309 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
13310 dpyinfo->Xatom_TIMESTAMP
13311 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
13312 dpyinfo->Xatom_TEXT
13313 = XInternAtom (dpyinfo->display, "TEXT", False);
dc43ef94
KH
13314 dpyinfo->Xatom_COMPOUND_TEXT
13315 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
334208b7
RS
13316 dpyinfo->Xatom_DELETE
13317 = XInternAtom (dpyinfo->display, "DELETE", False);
13318 dpyinfo->Xatom_MULTIPLE
13319 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
13320 dpyinfo->Xatom_INCR
13321 = XInternAtom (dpyinfo->display, "INCR", False);
13322 dpyinfo->Xatom_EMACS_TMP
13323 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
13324 dpyinfo->Xatom_TARGETS
13325 = XInternAtom (dpyinfo->display, "TARGETS", False);
13326 dpyinfo->Xatom_NULL
13327 = XInternAtom (dpyinfo->display, "NULL", False);
13328 dpyinfo->Xatom_ATOM_PAIR
13329 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
dc43ef94
KH
13330 /* For properties of font. */
13331 dpyinfo->Xatom_PIXEL_SIZE
13332 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
13333 dpyinfo->Xatom_MULE_BASELINE_OFFSET
13334 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
13335 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
13336 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
f78798df
KH
13337 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
13338 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
334208b7 13339
06a2c219
GM
13340 /* Ghostscript support. */
13341 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
13342 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
13343
13344 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
13345 False);
13346
547d9db8
KH
13347 dpyinfo->cut_buffers_initialized = 0;
13348
334208b7
RS
13349 connection = ConnectionNumber (dpyinfo->display);
13350 dpyinfo->connection = connection;
13351
dc43ef94 13352 {
5d7cc324
RS
13353 char null_bits[1];
13354
13355 null_bits[0] = 0x00;
dc43ef94
KH
13356
13357 dpyinfo->null_pixel
13358 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13359 null_bits, 1, 1, (long) 0, (long) 0,
13360 1);
13361 }
13362
06a2c219
GM
13363 {
13364 extern int gray_bitmap_width, gray_bitmap_height;
13365 extern unsigned char *gray_bitmap_bits;
13366 dpyinfo->gray
13367 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13368 gray_bitmap_bits,
13369 gray_bitmap_width, gray_bitmap_height,
13370 (unsigned long) 1, (unsigned long) 0, 1);
13371 }
13372
f5d11644
GM
13373#ifdef HAVE_X_I18N
13374 xim_initialize (dpyinfo, resource_name);
13375#endif
13376
87485d6f
MW
13377#ifdef subprocesses
13378 /* This is only needed for distinguishing keyboard and process input. */
334208b7 13379 if (connection != 0)
7a13e894 13380 add_keyboard_wait_descriptor (connection);
87485d6f 13381#endif
6d4238f3 13382
041b69ac 13383#ifndef F_SETOWN_BUG
dc6f92b8 13384#ifdef F_SETOWN
dc6f92b8 13385#ifdef F_SETOWN_SOCK_NEG
61c3ce62 13386 /* stdin is a socket here */
334208b7 13387 fcntl (connection, F_SETOWN, -getpid ());
c118dd06 13388#else /* ! defined (F_SETOWN_SOCK_NEG) */
334208b7 13389 fcntl (connection, F_SETOWN, getpid ());
c118dd06
JB
13390#endif /* ! defined (F_SETOWN_SOCK_NEG) */
13391#endif /* ! defined (F_SETOWN) */
041b69ac 13392#endif /* F_SETOWN_BUG */
dc6f92b8
JB
13393
13394#ifdef SIGIO
eee20f6a
KH
13395 if (interrupt_input)
13396 init_sigio (connection);
c118dd06 13397#endif /* ! defined (SIGIO) */
dc6f92b8 13398
51b592fb 13399#ifdef USE_LUCID
f8c39f51 13400#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
51b592fb
RS
13401 /* Make sure that we have a valid font for dialog boxes
13402 so that Xt does not crash. */
13403 {
13404 Display *dpy = dpyinfo->display;
13405 XrmValue d, fr, to;
13406 Font font;
e99db5a1 13407 int count;
51b592fb
RS
13408
13409 d.addr = (XPointer)&dpy;
13410 d.size = sizeof (Display *);
13411 fr.addr = XtDefaultFont;
13412 fr.size = sizeof (XtDefaultFont);
13413 to.size = sizeof (Font *);
13414 to.addr = (XPointer)&font;
e99db5a1 13415 count = x_catch_errors (dpy);
51b592fb
RS
13416 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
13417 abort ();
13418 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
13419 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
e99db5a1 13420 x_uncatch_errors (dpy, count);
51b592fb
RS
13421 }
13422#endif
f8c39f51 13423#endif
51b592fb 13424
34e23e5a
GM
13425 /* See if we should run in synchronous mode. This is useful
13426 for debugging X code. */
13427 {
13428 Lisp_Object value;
13429 value = display_x_get_resource (dpyinfo,
13430 build_string ("synchronous"),
13431 build_string ("Synchronous"),
13432 Qnil, Qnil);
13433 if (STRINGP (value)
13434 && (!strcmp (XSTRING (value)->data, "true")
13435 || !strcmp (XSTRING (value)->data, "on")))
13436 XSynchronize (dpyinfo->display, True);
13437 }
13438
60439948
KH
13439 UNBLOCK_INPUT;
13440
7a13e894
RS
13441 return dpyinfo;
13442}
13443\f
13444/* Get rid of display DPYINFO, assuming all frames are already gone,
13445 and without sending any more commands to the X server. */
dc6f92b8 13446
7a13e894
RS
13447void
13448x_delete_display (dpyinfo)
13449 struct x_display_info *dpyinfo;
13450{
13451 delete_keyboard_wait_descriptor (dpyinfo->connection);
13452
13453 /* Discard this display from x_display_name_list and x_display_list.
13454 We can't use Fdelq because that can quit. */
13455 if (! NILP (x_display_name_list)
8e713be6
KR
13456 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
13457 x_display_name_list = XCDR (x_display_name_list);
7a13e894
RS
13458 else
13459 {
13460 Lisp_Object tail;
13461
13462 tail = x_display_name_list;
8e713be6 13463 while (CONSP (tail) && CONSP (XCDR (tail)))
7a13e894 13464 {
bffcfca9 13465 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
7a13e894 13466 {
8e713be6 13467 XCDR (tail) = XCDR (XCDR (tail));
7a13e894
RS
13468 break;
13469 }
8e713be6 13470 tail = XCDR (tail);
7a13e894
RS
13471 }
13472 }
13473
9bda743f
GM
13474 if (next_noop_dpyinfo == dpyinfo)
13475 next_noop_dpyinfo = dpyinfo->next;
13476
7a13e894
RS
13477 if (x_display_list == dpyinfo)
13478 x_display_list = dpyinfo->next;
7f9c7f94
RS
13479 else
13480 {
13481 struct x_display_info *tail;
7a13e894 13482
7f9c7f94
RS
13483 for (tail = x_display_list; tail; tail = tail->next)
13484 if (tail->next == dpyinfo)
13485 tail->next = tail->next->next;
13486 }
7a13e894 13487
0d777288
RS
13488#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
13489#ifndef AIX /* On AIX, XCloseDisplay calls this. */
7f9c7f94
RS
13490 XrmDestroyDatabase (dpyinfo->xrdb);
13491#endif
0d777288 13492#endif
29b38361
KH
13493#ifdef MULTI_KBOARD
13494 if (--dpyinfo->kboard->reference_count == 0)
39f79001 13495 delete_kboard (dpyinfo->kboard);
b9737ad3 13496#endif
f5d11644
GM
13497#ifdef HAVE_X_I18N
13498 if (dpyinfo->xim)
13499 xim_close_dpy (dpyinfo);
13500#endif
13501
b9737ad3
KH
13502 xfree (dpyinfo->font_table);
13503 xfree (dpyinfo->x_id_name);
13504 xfree (dpyinfo);
7a13e894
RS
13505}
13506\f
13507/* Set up use of X before we make the first connection. */
13508
06a2c219
GM
13509static struct redisplay_interface x_redisplay_interface =
13510{
13511 x_produce_glyphs,
13512 x_write_glyphs,
13513 x_insert_glyphs,
13514 x_clear_end_of_line,
13515 x_scroll_run,
13516 x_after_update_window_line,
13517 x_update_window_begin,
13518 x_update_window_end,
13519 XTcursor_to,
13520 x_flush,
71b8321e 13521 x_clear_mouse_face,
66ac4b0e
GM
13522 x_get_glyph_overhangs,
13523 x_fix_overlapping_area
06a2c219
GM
13524};
13525
dfcf069d 13526void
7a13e894
RS
13527x_initialize ()
13528{
06a2c219
GM
13529 rif = &x_redisplay_interface;
13530
13531 clear_frame_hook = x_clear_frame;
13532 ins_del_lines_hook = x_ins_del_lines;
13533 change_line_highlight_hook = x_change_line_highlight;
13534 delete_glyphs_hook = x_delete_glyphs;
dc6f92b8
JB
13535 ring_bell_hook = XTring_bell;
13536 reset_terminal_modes_hook = XTreset_terminal_modes;
13537 set_terminal_modes_hook = XTset_terminal_modes;
06a2c219
GM
13538 update_begin_hook = x_update_begin;
13539 update_end_hook = x_update_end;
dc6f92b8
JB
13540 set_terminal_window_hook = XTset_terminal_window;
13541 read_socket_hook = XTread_socket;
b8009dd1 13542 frame_up_to_date_hook = XTframe_up_to_date;
dc6f92b8 13543 reassert_line_highlight_hook = XTreassert_line_highlight;
90e65f07 13544 mouse_position_hook = XTmouse_position;
f451eb13 13545 frame_rehighlight_hook = XTframe_rehighlight;
dbc4e1c1 13546 frame_raise_lower_hook = XTframe_raise_lower;
ab648270
JB
13547 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13548 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13549 redeem_scroll_bar_hook = XTredeem_scroll_bar;
13550 judge_scroll_bars_hook = XTjudge_scroll_bars;
06a2c219 13551 estimate_mode_line_height_hook = x_estimate_mode_line_height;
58769bee 13552
f676886a 13553 scroll_region_ok = 1; /* we'll scroll partial frames */
dc6f92b8
JB
13554 char_ins_del_ok = 0; /* just as fast to write the line */
13555 line_ins_del_ok = 1; /* we'll just blt 'em */
13556 fast_clear_end_of_line = 1; /* X does this well */
58769bee 13557 memory_below_frame = 0; /* we don't remember what scrolls
dc6f92b8
JB
13558 off the bottom */
13559 baud_rate = 19200;
13560
7a13e894 13561 x_noop_count = 0;
9ea173e8 13562 last_tool_bar_item = -1;
06a2c219
GM
13563 any_help_event_p = 0;
13564
b30b24cb
RS
13565 /* Try to use interrupt input; if we can't, then start polling. */
13566 Fset_input_mode (Qt, Qnil, Qt, Qnil);
13567
7f9c7f94
RS
13568#ifdef USE_X_TOOLKIT
13569 XtToolkitInitialize ();
13570 Xt_app_con = XtCreateApplicationContext ();
665881ad 13571 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
bffcfca9
GM
13572
13573 /* Install an asynchronous timer that processes Xt timeout events
13574 every 0.1s. This is necessary because some widget sets use
13575 timeouts internally, for example the LessTif menu bar, or the
13576 Xaw3d scroll bar. When Xt timouts aren't processed, these
13577 widgets don't behave normally. */
13578 {
13579 EMACS_TIME interval;
13580 EMACS_SET_SECS_USECS (interval, 0, 100000);
13581 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
13582 }
db74249b 13583#endif
bffcfca9 13584
db74249b 13585#if USE_TOOLKIT_SCROLL_BARS
ec18280f
SM
13586 xaw3d_arrow_scroll = False;
13587 xaw3d_pick_top = True;
7f9c7f94
RS
13588#endif
13589
58769bee 13590 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 13591 original error handler. */
e99db5a1 13592 XSetErrorHandler (x_error_handler);
334208b7 13593 XSetIOErrorHandler (x_io_error_quitter);
dc6f92b8 13594
06a2c219 13595 /* Disable Window Change signals; they are handled by X events. */
dc6f92b8
JB
13596#ifdef SIGWINCH
13597 signal (SIGWINCH, SIG_DFL);
c118dd06 13598#endif /* ! defined (SIGWINCH) */
dc6f92b8 13599
92e2441b 13600 signal (SIGPIPE, x_connection_signal);
dc6f92b8 13601}
55123275 13602
06a2c219 13603
55123275
JB
13604void
13605syms_of_xterm ()
13606{
e99db5a1
RS
13607 staticpro (&x_error_message_string);
13608 x_error_message_string = Qnil;
13609
7a13e894
RS
13610 staticpro (&x_display_name_list);
13611 x_display_name_list = Qnil;
334208b7 13612
ab648270 13613 staticpro (&last_mouse_scroll_bar);
e53cb100 13614 last_mouse_scroll_bar = Qnil;
59e755be
KH
13615
13616 staticpro (&Qvendor_specific_keysyms);
13617 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
2237cac9
RS
13618
13619 staticpro (&last_mouse_press_frame);
13620 last_mouse_press_frame = Qnil;
06a2c219 13621
06a2c219 13622 help_echo = Qnil;
be010514
GM
13623 staticpro (&help_echo);
13624 help_echo_object = Qnil;
13625 staticpro (&help_echo_object);
7cea38bc
GM
13626 help_echo_window = Qnil;
13627 staticpro (&help_echo_window);
06a2c219 13628 previous_help_echo = Qnil;
be010514
GM
13629 staticpro (&previous_help_echo);
13630 help_echo_pos = -1;
06a2c219
GM
13631
13632 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
13633 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
13634For example, if a block cursor is over a tab, it will be drawn as\n\
13635wide as that tab on the display.");
13636 x_stretch_cursor_p = 0;
13637
13638 DEFVAR_BOOL ("x-toolkit-scroll-bars-p", &x_toolkit_scroll_bars_p,
13639 "If not nil, Emacs uses toolkit scroll bars.");
13640#if USE_TOOLKIT_SCROLL_BARS
13641 x_toolkit_scroll_bars_p = 1;
13642#else
13643 x_toolkit_scroll_bars_p = 0;
13644#endif
13645
06a2c219
GM
13646 staticpro (&last_mouse_motion_frame);
13647 last_mouse_motion_frame = Qnil;
55123275 13648}
6cf0ae86
RS
13649
13650#endif /* not HAVE_X_WINDOWS */
06a2c219 13651