(mark_object) [GC_CHECK_MARKED_OBJECTS]: Check that no
[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 46
16bd92ea 47#ifndef USG
dc6f92b8
JB
48/* Load sys/types.h if not already loaded.
49 In some systems loading it twice is suicidal. */
50#ifndef makedev
51#include <sys/types.h>
c118dd06
JB
52#endif /* makedev */
53#endif /* USG */
dc6f92b8 54
6df54671 55#ifdef BSD_SYSTEM
dc6f92b8 56#include <sys/ioctl.h>
6df54671 57#endif /* ! defined (BSD_SYSTEM) */
dc6f92b8 58
2d368234 59#include "systty.h"
3a2712f9 60#include "systime.h"
dc6f92b8 61
b8009dd1 62#ifndef INCLUDED_FCNTL
dc6f92b8 63#include <fcntl.h>
b8009dd1 64#endif
dc6f92b8
JB
65#include <ctype.h>
66#include <errno.h>
67#include <setjmp.h>
68#include <sys/stat.h>
a0a7635f
RS
69/* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
70/* #include <sys/param.h> */
dc6f92b8 71
dc43ef94
KH
72#include "charset.h"
73#include "ccl.h"
7a13e894 74#include "frame.h"
dc6f92b8 75#include "dispextern.h"
ee569018 76#include "fontset.h"
dc6f92b8
JB
77#include "termhooks.h"
78#include "termopts.h"
79#include "termchar.h"
80#if 0
81#include "sink.h"
82#include "sinkmask.h"
c118dd06 83#endif /* ! 0 */
dc6f92b8 84#include "gnu.h"
dc6f92b8 85#include "disptab.h"
dc6f92b8 86#include "buffer.h"
f451eb13 87#include "window.h"
3b2fa4e6 88#include "keyboard.h"
bde7c500 89#include "intervals.h"
dfcf069d 90#include "process.h"
bffcfca9 91#include "atimer.h"
dc6f92b8 92
d2bd6bc4
RS
93#ifdef USE_X_TOOLKIT
94#include <X11/Shell.h>
95#endif
96
06a2c219
GM
97#include <sys/types.h>
98#ifdef HAVE_SYS_TIME_H
99#include <sys/time.h>
100#endif
101#ifdef HAVE_UNISTD_H
102#include <unistd.h>
103#endif
104
3afe33e7 105#ifdef USE_X_TOOLKIT
06a2c219 106
9d7e2e3e 107extern void free_frame_menubar ();
2224b905 108extern FRAME_PTR x_menubar_window_to_frame ();
06a2c219 109
0fdff6bb
RS
110#if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
111#define HACK_EDITRES
112extern void _XEditResCheckMessages ();
113#endif /* not NO_EDITRES */
06a2c219
GM
114
115/* Include toolkit specific headers for the scroll bar widget. */
116
117#ifdef USE_TOOLKIT_SCROLL_BARS
118#if defined USE_MOTIF
119#include <Xm/Xm.h> /* for LESSTIF_VERSION */
120#include <Xm/ScrollBar.h>
121#include <Xm/ScrollBarP.h>
ec18280f
SM
122#else /* !USE_MOTIF i.e. use Xaw */
123
124#ifdef HAVE_XAW3D
06a2c219 125#include <X11/Xaw3d/Simple.h>
06a2c219
GM
126#include <X11/Xaw3d/Scrollbar.h>
127#define ARROW_SCROLLBAR
128#include <X11/Xaw3d/ScrollbarP.h>
ec18280f
SM
129#else /* !HAVE_XAW3D */
130#include <X11/Xaw/Simple.h>
131#include <X11/Xaw/Scrollbar.h>
132#endif /* !HAVE_XAW3D */
133#ifndef XtNpickTop
134#define XtNpickTop "pickTop"
135#endif /* !XtNpickTop */
136#endif /* !USE_MOTIF */
06a2c219
GM
137#endif /* USE_TOOLKIT_SCROLL_BARS */
138
3afe33e7
RS
139#endif /* USE_X_TOOLKIT */
140
b849c413
RS
141#ifndef USE_X_TOOLKIT
142#define x_any_window_to_frame x_window_to_frame
5627c40e 143#define x_top_window_to_frame x_window_to_frame
b849c413
RS
144#endif
145
546e6d5b 146#ifdef USE_X_TOOLKIT
d067ea8b 147#include "widget.h"
546e6d5b
RS
148#ifndef XtNinitialState
149#define XtNinitialState "initialState"
150#endif
151#endif
152
80528801
KH
153#ifdef SOLARIS2
154/* memmove will be defined as a macro in Xfuncs.h unless
155 <string.h> is included beforehand. The declaration for memmove in
156 <string.h> will cause a syntax error when Xfuncs.h later includes it. */
157#include <string.h>
158#endif
159
e4b68333 160#ifndef min
06a2c219 161#define min(a,b) ((a) < (b) ? (a) : (b))
e4b68333
RS
162#endif
163#ifndef max
06a2c219
GM
164#define max(a,b) ((a) > (b) ? (a) : (b))
165#endif
166
167#define abs(x) ((x) < 0 ? -(x) : (x))
168
169#define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
170
171\f
172/* Bitmaps for truncated lines. */
173
174enum bitmap_type
175{
176 NO_BITMAP,
177 LEFT_TRUNCATION_BITMAP,
178 RIGHT_TRUNCATION_BITMAP,
179 OVERLAY_ARROW_BITMAP,
180 CONTINUED_LINE_BITMAP,
181 CONTINUATION_LINE_BITMAP,
182 ZV_LINE_BITMAP
183};
184
185/* Bitmap drawn to indicate lines not displaying text if
186 `indicate-empty-lines' is non-nil. */
187
188#define zv_width 8
189#define zv_height 8
190static unsigned char zv_bits[] = {
191 0x00, 0x00, 0x1e, 0x1e, 0x1e, 0x1e, 0x00, 0x00};
192
193/* An arrow like this: `<-'. */
194
195#define left_width 8
196#define left_height 8
197static unsigned char left_bits[] = {
198 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
199
110859fc
GM
200/* Right truncation arrow bitmap `->'. */
201
202#define right_width 8
203#define right_height 8
204static unsigned char right_bits[] = {
205 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
206
06a2c219
GM
207/* Marker for continued lines. */
208
209#define continued_width 8
210#define continued_height 8
211static unsigned char continued_bits[] = {
110859fc
GM
212 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
213
214/* Marker for continuation lines. */
06a2c219
GM
215
216#define continuation_width 8
217#define continuation_height 8
218static unsigned char continuation_bits[] = {
110859fc 219 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
06a2c219 220
110859fc 221/* Overlay arrow bitmap. */
06a2c219 222
110859fc
GM
223#if 0
224/* A bomb. */
06a2c219
GM
225#define ov_width 8
226#define ov_height 8
227static unsigned char ov_bits[] = {
228 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
06a2c219 229#else
110859fc 230/* A triangular arrow. */
06a2c219
GM
231#define ov_width 8
232#define ov_height 8
233static unsigned char ov_bits[] = {
110859fc
GM
234 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
235
e4b68333 236#endif
06a2c219
GM
237
238extern Lisp_Object Qhelp_echo;
239
69388238 240\f
06a2c219
GM
241/* Non-zero means Emacs uses toolkit scroll bars. */
242
243int x_toolkit_scroll_bars_p;
244
245/* If a string, XTread_socket generates an event to display that string.
246 (The display is done in read_char.) */
247
248static Lisp_Object help_echo;
249
250/* Temporary variable for XTread_socket. */
251
252static Lisp_Object previous_help_echo;
253
254/* Non-zero means that a HELP_EVENT has been generated since Emacs
255 start. */
256
257static int any_help_event_p;
258
259/* Non-zero means draw block and hollow cursor as wide as the glyph
260 under it. For example, if a block cursor is over a tab, it will be
261 drawn as wide as that tab on the display. */
262
263int x_stretch_cursor_p;
264
265/* This is a chain of structures for all the X displays currently in
266 use. */
267
334208b7 268struct x_display_info *x_display_list;
dc6f92b8 269
06a2c219
GM
270/* This is a list of cons cells, each of the form (NAME
271 . FONT-LIST-CACHE), one for each element of x_display_list and in
272 the same order. NAME is the name of the frame. FONT-LIST-CACHE
273 records previous values returned by x-list-fonts. */
274
7a13e894 275Lisp_Object x_display_name_list;
f451eb13 276
987d2ad1 277/* Frame being updated by update_frame. This is declared in term.c.
06a2c219
GM
278 This is set by update_begin and looked at by all the XT functions.
279 It is zero while not inside an update. In that case, the XT
280 functions assume that `selected_frame' is the frame to apply to. */
281
d0386f2a 282extern struct frame *updating_frame;
dc6f92b8 283
dfcf069d 284extern int waiting_for_input;
0e81d8cd 285
06a2c219
GM
286/* This is a frame waiting to be auto-raised, within XTread_socket. */
287
0134a210
RS
288struct frame *pending_autoraise_frame;
289
7f9c7f94
RS
290#ifdef USE_X_TOOLKIT
291/* The application context for Xt use. */
292XtAppContext Xt_app_con;
06a2c219
GM
293static String Xt_default_resources[] = {0};
294#endif /* USE_X_TOOLKIT */
665881ad 295
06a2c219
GM
296/* Nominal cursor position -- where to draw output.
297 HPOS and VPOS are window relative glyph matrix coordinates.
298 X and Y are window relative pixel coordinates. */
dc6f92b8 299
06a2c219 300struct cursor_pos output_cursor;
dc6f92b8 301
bffcfca9
GM
302/* Non-zero means user is interacting with a toolkit scroll bar. */
303
304static int toolkit_scroll_bar_interaction;
dc6f92b8 305
69388238
RS
306/* Mouse movement.
307
06a2c219 308 Formerly, we used PointerMotionHintMask (in standard_event_mask)
f5bb65ec
RS
309 so that we would have to call XQueryPointer after each MotionNotify
310 event to ask for another such event. However, this made mouse tracking
311 slow, and there was a bug that made it eventually stop.
312
313 Simply asking for MotionNotify all the time seems to work better.
314
69388238
RS
315 In order to avoid asking for motion events and then throwing most
316 of them away or busy-polling the server for mouse positions, we ask
317 the server for pointer motion hints. This means that we get only
318 one event per group of mouse movements. "Groups" are delimited by
319 other kinds of events (focus changes and button clicks, for
320 example), or by XQueryPointer calls; when one of these happens, we
321 get another MotionNotify event the next time the mouse moves. This
322 is at least as efficient as getting motion events when mouse
323 tracking is on, and I suspect only negligibly worse when tracking
f5bb65ec 324 is off. */
69388238
RS
325
326/* Where the mouse was last time we reported a mouse event. */
69388238 327
06a2c219
GM
328FRAME_PTR last_mouse_frame;
329static XRectangle last_mouse_glyph;
2237cac9
RS
330static Lisp_Object last_mouse_press_frame;
331
69388238
RS
332/* The scroll bar in which the last X motion event occurred.
333
06a2c219
GM
334 If the last X motion event occurred in a scroll bar, we set this so
335 XTmouse_position can know whether to report a scroll bar motion or
69388238
RS
336 an ordinary motion.
337
06a2c219
GM
338 If the last X motion event didn't occur in a scroll bar, we set
339 this to Qnil, to tell XTmouse_position to return an ordinary motion
340 event. */
341
69388238
RS
342static Lisp_Object last_mouse_scroll_bar;
343
69388238
RS
344/* This is a hack. We would really prefer that XTmouse_position would
345 return the time associated with the position it returns, but there
06a2c219 346 doesn't seem to be any way to wrest the time-stamp from the server
69388238
RS
347 along with the position query. So, we just keep track of the time
348 of the last movement we received, and return that in hopes that
349 it's somewhat accurate. */
06a2c219 350
69388238
RS
351static Time last_mouse_movement_time;
352
06a2c219
GM
353/* Incremented by XTread_socket whenever it really tries to read
354 events. */
355
c0a04927
RS
356#ifdef __STDC__
357static int volatile input_signal_count;
358#else
359static int input_signal_count;
360#endif
361
7a13e894 362/* Used locally within XTread_socket. */
06a2c219 363
7a13e894 364static int x_noop_count;
dc6f92b8 365
7a13e894 366/* Initial values of argv and argc. */
06a2c219 367
7a13e894
RS
368extern char **initial_argv;
369extern int initial_argc;
dc6f92b8 370
7a13e894 371extern Lisp_Object Vcommand_line_args, Vsystem_name;
dc6f92b8 372
06a2c219 373/* Tells if a window manager is present or not. */
7a13e894
RS
374
375extern Lisp_Object Vx_no_window_manager;
dc6f92b8 376
c2df547c 377extern Lisp_Object Qface, Qmouse_face;
b8009dd1 378
dc6f92b8
JB
379extern int errno;
380
dfeccd2d 381/* A mask of extra modifier bits to put into every keyboard char. */
06a2c219 382
64bb1782
RS
383extern int extra_keyboard_modifiers;
384
59e755be
KH
385static Lisp_Object Qvendor_specific_keysyms;
386
334208b7 387extern XrmDatabase x_load_resources ();
c32cdd9a
KH
388extern Lisp_Object x_icon_type ();
389
7a13e894 390
06a2c219
GM
391/* Enumeration for overriding/changing the face to use for drawing
392 glyphs in x_draw_glyphs. */
393
394enum draw_glyphs_face
395{
396 DRAW_NORMAL_TEXT,
397 DRAW_INVERSE_VIDEO,
398 DRAW_CURSOR,
399 DRAW_MOUSE_FACE,
400 DRAW_IMAGE_RAISED,
401 DRAW_IMAGE_SUNKEN
402};
403
404static void x_update_window_end P_ ((struct window *, int));
405static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
406void x_delete_display P_ ((struct x_display_info *));
407static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
408 unsigned));
409static int fast_find_position P_ ((struct window *, int, int *, int *,
410 int *, int *));
411static void set_output_cursor P_ ((struct cursor_pos *));
412static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
413 int *, int *, int *));
414static void note_mode_line_highlight P_ ((struct window *, int, int));
06a2c219 415static void note_mouse_highlight P_ ((struct frame *, int, int));
9ea173e8
GM
416static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
417static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
06a2c219
GM
418static void show_mouse_face P_ ((struct x_display_info *,
419 enum draw_glyphs_face));
420static int x_io_error_quitter P_ ((Display *));
421int x_catch_errors P_ ((Display *));
422void x_uncatch_errors P_ ((Display *, int));
423void x_lower_frame P_ ((struct frame *));
424void x_scroll_bar_clear P_ ((struct frame *));
425int x_had_errors_p P_ ((Display *));
426void x_wm_set_size_hint P_ ((struct frame *, long, int));
427void x_raise_frame P_ ((struct frame *));
428void x_set_window_size P_ ((struct frame *, int, int, int));
429void x_wm_set_window_state P_ ((struct frame *, int));
430void x_wm_set_icon_pixmap P_ ((struct frame *, int));
431void x_initialize P_ ((void));
432static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
433static int x_compute_min_glyph_bounds P_ ((struct frame *));
434static void x_draw_phys_cursor_glyph P_ ((struct window *,
435 struct glyph_row *,
436 enum draw_glyphs_face));
437static void x_update_end P_ ((struct frame *));
438static void XTframe_up_to_date P_ ((struct frame *));
439static void XTreassert_line_highlight P_ ((int, int));
440static void x_change_line_highlight P_ ((int, int, int, int));
441static void XTset_terminal_modes P_ ((void));
442static void XTreset_terminal_modes P_ ((void));
443static void XTcursor_to P_ ((int, int, int, int));
444static void x_write_glyphs P_ ((struct glyph *, int));
445static void x_clear_end_of_line P_ ((int));
446static void x_clear_frame P_ ((void));
447static void x_clear_cursor P_ ((struct window *));
448static void frame_highlight P_ ((struct frame *));
449static void frame_unhighlight P_ ((struct frame *));
450static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
451static void XTframe_rehighlight P_ ((struct frame *));
452static void x_frame_rehighlight P_ ((struct x_display_info *));
453static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
f02d8aa0 454static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
06a2c219
GM
455static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
456 XRectangle *));
457static void expose_frame P_ ((struct frame *, int, int, int, int));
458static void expose_window_tree P_ ((struct window *, XRectangle *));
459static void expose_window P_ ((struct window *, XRectangle *));
460static void expose_area P_ ((struct window *, struct glyph_row *,
461 XRectangle *, enum glyph_row_area));
462static void expose_line P_ ((struct window *, struct glyph_row *,
463 XRectangle *));
464static void x_update_cursor_in_window_tree P_ ((struct window *, int));
465static void x_update_window_cursor P_ ((struct window *, int));
466static void x_erase_phys_cursor P_ ((struct window *));
467void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
468static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
469 enum bitmap_type));
470
471static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
472 GC, int));
473static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
474static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
475static void note_overwritten_text_cursor P_ ((struct window *, int, int));
476static void x_flush P_ ((struct frame *f));
477
478
479/* Flush display of frame F, or of all frames if F is null. */
480
481static void
482x_flush (f)
483 struct frame *f;
484{
485 BLOCK_INPUT;
486 if (f == NULL)
487 {
488 Lisp_Object rest, frame;
489 FOR_EACH_FRAME (rest, frame)
490 x_flush (XFRAME (frame));
491 }
492 else if (FRAME_X_P (f))
493 XFlush (FRAME_X_DISPLAY (f));
494 UNBLOCK_INPUT;
495}
496
dc6f92b8 497
06a2c219
GM
498/* Remove calls to XFlush by defining XFlush to an empty replacement.
499 Calls to XFlush should be unnecessary because the X output buffer
500 is flushed automatically as needed by calls to XPending,
501 XNextEvent, or XWindowEvent according to the XFlush man page.
502 XTread_socket calls XPending. Removing XFlush improves
503 performance. */
504
505#define XFlush(DISPLAY) (void) 0
b8009dd1 506
334208b7 507\f
06a2c219
GM
508/***********************************************************************
509 Debugging
510 ***********************************************************************/
511
9382638d 512#if 0
06a2c219
GM
513
514/* This is a function useful for recording debugging information about
515 the sequence of occurrences in this file. */
9382638d
KH
516
517struct record
518{
519 char *locus;
520 int type;
521};
522
523struct record event_record[100];
524
525int event_record_index;
526
527record_event (locus, type)
528 char *locus;
529 int type;
530{
531 if (event_record_index == sizeof (event_record) / sizeof (struct record))
532 event_record_index = 0;
533
534 event_record[event_record_index].locus = locus;
535 event_record[event_record_index].type = type;
536 event_record_index++;
537}
538
539#endif /* 0 */
06a2c219
GM
540
541
9382638d 542\f
334208b7
RS
543/* Return the struct x_display_info corresponding to DPY. */
544
545struct x_display_info *
546x_display_info_for_display (dpy)
547 Display *dpy;
548{
549 struct x_display_info *dpyinfo;
550
551 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
552 if (dpyinfo->display == dpy)
553 return dpyinfo;
16bd92ea 554
334208b7
RS
555 return 0;
556}
f451eb13 557
06a2c219
GM
558
559\f
560/***********************************************************************
561 Starting and ending an update
562 ***********************************************************************/
563
564/* Start an update of frame F. This function is installed as a hook
565 for update_begin, i.e. it is called when update_begin is called.
566 This function is called prior to calls to x_update_window_begin for
567 each window being updated. Currently, there is nothing to do here
568 because all interesting stuff is done on a window basis. */
dc6f92b8 569
dfcf069d 570static void
06a2c219 571x_update_begin (f)
f676886a 572 struct frame *f;
58769bee 573{
06a2c219
GM
574 /* Nothing to do. */
575}
dc6f92b8 576
dc6f92b8 577
06a2c219
GM
578/* Start update of window W. Set the global variable updated_window
579 to the window being updated and set output_cursor to the cursor
580 position of W. */
dc6f92b8 581
06a2c219
GM
582static void
583x_update_window_begin (w)
584 struct window *w;
585{
586 struct frame *f = XFRAME (WINDOW_FRAME (w));
587 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
588
589 updated_window = w;
590 set_output_cursor (&w->cursor);
b8009dd1 591
06a2c219 592 BLOCK_INPUT;
d1bc4182 593
06a2c219 594 if (f == display_info->mouse_face_mouse_frame)
b8009dd1 595 {
514e4681 596 /* Don't do highlighting for mouse motion during the update. */
06a2c219 597 display_info->mouse_face_defer = 1;
37c2c98b 598
06a2c219
GM
599 /* If F needs to be redrawn, simply forget about any prior mouse
600 highlighting. */
9f67f20b 601 if (FRAME_GARBAGED_P (f))
06a2c219
GM
602 display_info->mouse_face_window = Qnil;
603
604 /* Can we tell that this update does not affect the window
605 where the mouse highlight is? If so, no need to turn off.
606 Likewise, don't do anything if the frame is garbaged;
607 in that case, the frame's current matrix that we would use
608 is all wrong, and we will redisplay that line anyway. */
609 if (!NILP (display_info->mouse_face_window)
610 && w == XWINDOW (display_info->mouse_face_window))
514e4681 611 {
06a2c219 612 int i;
514e4681 613
06a2c219
GM
614 for (i = 0; i < w->desired_matrix->nrows; ++i)
615 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
514e4681
RS
616 break;
617
06a2c219
GM
618 if (i < w->desired_matrix->nrows)
619 clear_mouse_face (display_info);
514e4681 620 }
b8009dd1 621 }
6ccf47d1 622
dc6f92b8
JB
623 UNBLOCK_INPUT;
624}
625
06a2c219
GM
626
627/* Draw a vertical window border to the right of window W if W doesn't
628 have vertical scroll bars. */
629
dfcf069d 630static void
06a2c219
GM
631x_draw_vertical_border (w)
632 struct window *w;
58769bee 633{
06a2c219
GM
634 struct frame *f = XFRAME (WINDOW_FRAME (w));
635
636 /* Redraw borders between horizontally adjacent windows. Don't
637 do it for frames with vertical scroll bars because either the
638 right scroll bar of a window, or the left scroll bar of its
639 neighbor will suffice as a border. */
640 if (!WINDOW_RIGHTMOST_P (w)
641 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
642 {
643 int x0, x1, y0, y1;
dc6f92b8 644
06a2c219 645 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
110859fc 646 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
647 y1 -= 1;
648
649 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
650 f->output_data.x->normal_gc, x1, y0, x1, y1);
651 }
652}
653
654
655/* End update of window W (which is equal to updated_window). Draw
656 vertical borders between horizontally adjacent windows, and display
657 W's cursor if CURSOR_ON_P is non-zero. W may be a menu bar
658 pseudo-window in case we don't have X toolkit support. Such
659 windows don't have a cursor, so don't display it here. */
660
661static void
662x_update_window_end (w, cursor_on_p)
663 struct window *w;
664 int cursor_on_p;
665{
666 if (!w->pseudo_window_p)
667 {
668 BLOCK_INPUT;
669 if (cursor_on_p)
670 x_display_and_set_cursor (w, 1, output_cursor.hpos,
671 output_cursor.vpos,
672 output_cursor.x, output_cursor.y);
673 x_draw_vertical_border (w);
674 UNBLOCK_INPUT;
675 }
676
677 updated_window = NULL;
678}
dc6f92b8 679
dc6f92b8 680
06a2c219
GM
681/* End update of frame F. This function is installed as a hook in
682 update_end. */
683
684static void
685x_update_end (f)
686 struct frame *f;
687{
688 /* Mouse highlight may be displayed again. */
aa8bff2e 689 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
b8009dd1 690
06a2c219 691 BLOCK_INPUT;
334208b7 692 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
693 UNBLOCK_INPUT;
694}
b8009dd1 695
06a2c219
GM
696
697/* This function is called from various places in xdisp.c whenever a
698 complete update has been performed. The global variable
699 updated_window is not available here. */
b8009dd1 700
dfcf069d 701static void
b8009dd1 702XTframe_up_to_date (f)
06a2c219 703 struct frame *f;
b8009dd1 704{
06a2c219 705 if (FRAME_X_P (f))
514e4681 706 {
06a2c219
GM
707 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
708 if (dpyinfo->mouse_face_deferred_gc
709 || f == dpyinfo->mouse_face_mouse_frame)
710 {
711 BLOCK_INPUT;
712 if (dpyinfo->mouse_face_mouse_frame)
713 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
714 dpyinfo->mouse_face_mouse_x,
715 dpyinfo->mouse_face_mouse_y);
716 dpyinfo->mouse_face_deferred_gc = 0;
717 UNBLOCK_INPUT;
718 }
514e4681 719 }
b8009dd1 720}
06a2c219
GM
721
722
723/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
724 arrow bitmaps, or clear the areas where they would be displayed
725 before DESIRED_ROW is made current. The window being updated is
726 found in updated_window. This function It is called from
727 update_window_line only if it is known that there are differences
728 between bitmaps to be drawn between current row and DESIRED_ROW. */
729
730static void
731x_after_update_window_line (desired_row)
732 struct glyph_row *desired_row;
733{
734 struct window *w = updated_window;
735
736 xassert (w);
737
738 if (!desired_row->mode_line_p && !w->pseudo_window_p)
739 {
740 BLOCK_INPUT;
741 x_draw_row_bitmaps (w, desired_row);
742
743 /* When a window has disappeared, make sure that no rest of
744 full-width rows stays visible in the internal border. */
745 if (windows_or_buffers_changed)
746 {
747 struct frame *f = XFRAME (w->frame);
748 int width = FRAME_INTERNAL_BORDER_WIDTH (f);
749 int height = desired_row->visible_height;
110859fc
GM
750 int x = (window_box_right (w, -1)
751 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
06a2c219
GM
752 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
753
754 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
755 x, y, width, height, False);
756 }
757
758 UNBLOCK_INPUT;
759 }
760}
761
762
763/* Draw the bitmap WHICH in one of the areas to the left or right of
764 window W. ROW is the glyph row for which to display the bitmap; it
765 determines the vertical position at which the bitmap has to be
766 drawn. */
767
768static void
769x_draw_bitmap (w, row, which)
770 struct window *w;
771 struct glyph_row *row;
772 enum bitmap_type which;
773{
774 struct frame *f = XFRAME (WINDOW_FRAME (w));
775 Display *display = FRAME_X_DISPLAY (f);
776 Window window = FRAME_X_WINDOW (f);
777 int x, y, wd, h, dy;
778 unsigned char *bits;
779 Pixmap pixmap;
780 GC gc = f->output_data.x->normal_gc;
781 struct face *face;
782 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
783
784 /* Must clip because of partially visible lines. */
785 x_clip_to_row (w, row, gc, 1);
786
787 switch (which)
788 {
789 case LEFT_TRUNCATION_BITMAP:
790 wd = left_width;
791 h = left_height;
792 bits = left_bits;
793 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
794 - wd
110859fc 795 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
796 break;
797
798 case OVERLAY_ARROW_BITMAP:
799 wd = left_width;
800 h = left_height;
801 bits = ov_bits;
802 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
803 - wd
110859fc 804 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
805 break;
806
807 case RIGHT_TRUNCATION_BITMAP:
808 wd = right_width;
809 h = right_height;
810 bits = right_bits;
811 x = window_box_right (w, -1);
110859fc 812 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
813 break;
814
815 case CONTINUED_LINE_BITMAP:
816 wd = right_width;
817 h = right_height;
818 bits = continued_bits;
819 x = window_box_right (w, -1);
110859fc 820 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
06a2c219
GM
821 break;
822
823 case CONTINUATION_LINE_BITMAP:
824 wd = continuation_width;
825 h = continuation_height;
826 bits = continuation_bits;
827 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
828 - wd
110859fc 829 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
830 break;
831
832 case ZV_LINE_BITMAP:
833 wd = zv_width;
834 h = zv_height;
835 bits = zv_bits;
836 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
837 - wd
110859fc 838 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
06a2c219
GM
839 break;
840
841 default:
842 abort ();
843 }
844
845 /* Convert to frame coordinates. Set dy to the offset in the row to
846 start drawing the bitmap. */
847 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
848 dy = (row->height - h) / 2;
849
850 /* Draw the bitmap. I believe these small pixmaps can be cached
851 by the server. */
852 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
853 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
854 face->foreground,
855 face->background, depth);
856 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
857 XFreePixmap (display, pixmap);
858 XSetClipMask (display, gc, None);
859}
860
861
862/* Draw flags bitmaps for glyph row ROW on window W. Call this
863 function with input blocked. */
864
865static void
866x_draw_row_bitmaps (w, row)
867 struct window *w;
868 struct glyph_row *row;
869{
870 struct frame *f = XFRAME (w->frame);
871 enum bitmap_type bitmap;
872 struct face *face;
045dee35 873 int header_line_height = -1;
06a2c219
GM
874
875 xassert (interrupt_input_blocked);
876
877 /* If row is completely invisible, because of vscrolling, we
878 don't have to draw anything. */
879 if (row->visible_height <= 0)
880 return;
881
882 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
883 PREPARE_FACE_FOR_DISPLAY (f, face);
884
885 /* Decide which bitmap to draw at the left side. */
886 if (row->overlay_arrow_p)
887 bitmap = OVERLAY_ARROW_BITMAP;
888 else if (row->truncated_on_left_p)
889 bitmap = LEFT_TRUNCATION_BITMAP;
890 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
891 bitmap = CONTINUATION_LINE_BITMAP;
892 else if (row->indicate_empty_line_p)
893 bitmap = ZV_LINE_BITMAP;
894 else
895 bitmap = NO_BITMAP;
896
897 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
898 the flags area. */
899 if (bitmap == NO_BITMAP
110859fc 900 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
901 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
902 {
903 /* If W has a vertical border to its left, don't draw over it. */
904 int border = ((XFASTINT (w->left) > 0
905 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
906 ? 1 : 0);
907 int left = window_box_left (w, -1);
908
045dee35
GM
909 if (header_line_height < 0)
910 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
911
912 /* In case the same realized face is used for bitmap areas and
913 for something displayed in the text (e.g. face `region' on
914 mono-displays, the fill style may have been changed to
915 FillSolid in x_draw_glyph_string_background. */
916 if (face->stipple)
917 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
918 else
919 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
920
06a2c219
GM
921 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
922 face->gc,
923 (left
110859fc 924 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
06a2c219 925 + border),
045dee35 926 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 927 row->y)),
110859fc 928 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
06a2c219 929 row->visible_height);
dcd08bfb
GM
930 if (!face->stipple)
931 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
932 }
933
934 /* Draw the left bitmap. */
935 if (bitmap != NO_BITMAP)
936 x_draw_bitmap (w, row, bitmap);
937
938 /* Decide which bitmap to draw at the right side. */
939 if (row->truncated_on_right_p)
940 bitmap = RIGHT_TRUNCATION_BITMAP;
941 else if (row->continued_p)
942 bitmap = CONTINUED_LINE_BITMAP;
943 else
944 bitmap = NO_BITMAP;
945
946 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
947 the flags area. */
948 if (bitmap == NO_BITMAP
110859fc 949 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
06a2c219
GM
950 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
951 {
952 int right = window_box_right (w, -1);
953
045dee35
GM
954 if (header_line_height < 0)
955 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dcd08bfb
GM
956
957 /* In case the same realized face is used for bitmap areas and
958 for something displayed in the text (e.g. face `region' on
959 mono-displays, the fill style may have been changed to
960 FillSolid in x_draw_glyph_string_background. */
961 if (face->stipple)
962 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
963 else
964 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
06a2c219
GM
965 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
966 face->gc,
967 right,
045dee35 968 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219 969 row->y)),
110859fc 970 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
06a2c219 971 row->visible_height);
dcd08bfb
GM
972 if (!face->stipple)
973 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
06a2c219
GM
974 }
975
976 /* Draw the right bitmap. */
977 if (bitmap != NO_BITMAP)
978 x_draw_bitmap (w, row, bitmap);
979}
980
dc6f92b8 981\f
06a2c219
GM
982/***********************************************************************
983 Line Highlighting
984 ***********************************************************************/
dc6f92b8 985
06a2c219
GM
986/* External interface to control of standout mode. Not used for X
987 frames. Aborts when called. */
988
989static void
dc6f92b8
JB
990XTreassert_line_highlight (new, vpos)
991 int new, vpos;
992{
06a2c219 993 abort ();
dc6f92b8
JB
994}
995
06a2c219
GM
996
997/* Call this when about to modify line at position VPOS and change
998 whether it is highlighted. Not used for X frames. Aborts when
999 called. */
dc6f92b8 1000
dfcf069d 1001static void
06a2c219
GM
1002x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1003 int new_highlight, vpos, y, first_unused_hpos;
dc6f92b8 1004{
06a2c219 1005 abort ();
dc6f92b8
JB
1006}
1007
06a2c219
GM
1008
1009/* This is called when starting Emacs and when restarting after
1010 suspend. When starting Emacs, no X window is mapped. And nothing
1011 must be done to Emacs's own window if it is suspended (though that
1012 rarely happens). */
dc6f92b8 1013
dfcf069d 1014static void
dc6f92b8
JB
1015XTset_terminal_modes ()
1016{
1017}
1018
06a2c219
GM
1019/* This is called when exiting or suspending Emacs. Exiting will make
1020 the X-windows go away, and suspending requires no action. */
dc6f92b8 1021
dfcf069d 1022static void
dc6f92b8
JB
1023XTreset_terminal_modes ()
1024{
dc6f92b8 1025}
06a2c219
GM
1026
1027
dc6f92b8 1028\f
06a2c219
GM
1029/***********************************************************************
1030 Output Cursor
1031 ***********************************************************************/
1032
1033/* Set the global variable output_cursor to CURSOR. All cursor
1034 positions are relative to updated_window. */
dc6f92b8 1035
dfcf069d 1036static void
06a2c219
GM
1037set_output_cursor (cursor)
1038 struct cursor_pos *cursor;
dc6f92b8 1039{
06a2c219
GM
1040 output_cursor.hpos = cursor->hpos;
1041 output_cursor.vpos = cursor->vpos;
1042 output_cursor.x = cursor->x;
1043 output_cursor.y = cursor->y;
1044}
1045
1046
1047/* Set a nominal cursor position.
dc6f92b8 1048
06a2c219
GM
1049 HPOS and VPOS are column/row positions in a window glyph matrix. X
1050 and Y are window text area relative pixel positions.
1051
1052 If this is done during an update, updated_window will contain the
1053 window that is being updated and the position is the future output
1054 cursor position for that window. If updated_window is null, use
1055 selected_window and display the cursor at the given position. */
1056
1057static void
1058XTcursor_to (vpos, hpos, y, x)
1059 int vpos, hpos, y, x;
1060{
1061 struct window *w;
1062
1063 /* If updated_window is not set, work on selected_window. */
1064 if (updated_window)
1065 w = updated_window;
1066 else
1067 w = XWINDOW (selected_window);
dbcb258a 1068
06a2c219
GM
1069 /* Set the output cursor. */
1070 output_cursor.hpos = hpos;
1071 output_cursor.vpos = vpos;
1072 output_cursor.x = x;
1073 output_cursor.y = y;
dc6f92b8 1074
06a2c219
GM
1075 /* If not called as part of an update, really display the cursor.
1076 This will also set the cursor position of W. */
1077 if (updated_window == NULL)
dc6f92b8
JB
1078 {
1079 BLOCK_INPUT;
06a2c219 1080 x_display_cursor (w, 1, hpos, vpos, x, y);
b86bd3dd 1081 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
dc6f92b8
JB
1082 UNBLOCK_INPUT;
1083 }
1084}
dc43ef94 1085
06a2c219
GM
1086
1087\f
1088/***********************************************************************
1089 Display Iterator
1090 ***********************************************************************/
1091
1092/* Function prototypes of this page. */
1093
1094static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1095 struct glyph *,
ee569018
KH
1096 XChar2b *,
1097 int *));
06a2c219
GM
1098static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1099 int, XChar2b *, int));
1100static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1101static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1102static void x_append_glyph P_ ((struct it *));
b4192550 1103static void x_append_composite_glyph P_ ((struct it *));
06a2c219
GM
1104static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1105 int, int, double));
1106static void x_produce_glyphs P_ ((struct it *));
06a2c219 1107static void x_produce_image_glyph P_ ((struct it *it));
ee569018
KH
1108
1109
1110/* Return a pointer to per-char metric information in FONT of a
1111 character pointed by B which is a pointer to an XChar2b. */
1112
1113#define PER_CHAR_METRIC(font, b) \
1114 ((font)->per_char \
1115 ? ((font)->per_char + (b)->byte2 - (font)->min_char_or_byte2 \
1116 + (((font)->min_byte1 || (font)->max_byte1) \
1117 ? (((b)->byte1 - (font)->min_byte1) \
1118 * ((font)->max_char_or_byte2 - (font)->min_char_or_byte2 + 1)) \
1119 : 0)) \
1120 : &((font)->max_bounds))
dc43ef94 1121
dc6f92b8 1122
e2ef8ee6
GM
1123/* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1124 is not contained in the font. */
dc43ef94 1125
06a2c219 1126static INLINE XCharStruct *
ee569018 1127x_per_char_metric (font, char2b)
06a2c219
GM
1128 XFontStruct *font;
1129 XChar2b *char2b;
1130{
1131 /* The result metric information. */
1132 XCharStruct *pcm = NULL;
dc6f92b8 1133
06a2c219 1134 xassert (font && char2b);
dc6f92b8 1135
06a2c219 1136 if (font->per_char != NULL)
dc6f92b8 1137 {
06a2c219 1138 if (font->min_byte1 == 0 && font->max_byte1 == 0)
dc43ef94 1139 {
06a2c219
GM
1140 /* min_char_or_byte2 specifies the linear character index
1141 corresponding to the first element of the per_char array,
1142 max_char_or_byte2 is the index of the last character. A
1143 character with non-zero CHAR2B->byte1 is not in the font.
1144 A character with byte2 less than min_char_or_byte2 or
1145 greater max_char_or_byte2 is not in the font. */
1146 if (char2b->byte1 == 0
1147 && char2b->byte2 >= font->min_char_or_byte2
1148 && char2b->byte2 <= font->max_char_or_byte2)
1149 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
dc43ef94 1150 }
06a2c219 1151 else
dc6f92b8 1152 {
06a2c219
GM
1153 /* If either min_byte1 or max_byte1 are nonzero, both
1154 min_char_or_byte2 and max_char_or_byte2 are less than
1155 256, and the 2-byte character index values corresponding
1156 to the per_char array element N (counting from 0) are:
1157
1158 byte1 = N/D + min_byte1
1159 byte2 = N\D + min_char_or_byte2
1160
1161 where:
1162
1163 D = max_char_or_byte2 - min_char_or_byte2 + 1
1164 / = integer division
1165 \ = integer modulus */
1166 if (char2b->byte1 >= font->min_byte1
1167 && char2b->byte1 <= font->max_byte1
1168 && char2b->byte2 >= font->min_char_or_byte2
1169 && char2b->byte2 <= font->max_char_or_byte2)
1170 {
1171 pcm = (font->per_char
1172 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1173 * (char2b->byte1 - font->min_byte1))
1174 + (char2b->byte2 - font->min_char_or_byte2));
1175 }
dc6f92b8 1176 }
06a2c219
GM
1177 }
1178 else
1179 {
1180 /* If the per_char pointer is null, all glyphs between the first
1181 and last character indexes inclusive have the same
1182 information, as given by both min_bounds and max_bounds. */
1183 if (char2b->byte2 >= font->min_char_or_byte2
1184 && char2b->byte2 <= font->max_char_or_byte2)
1185 pcm = &font->max_bounds;
1186 }
dc6f92b8 1187
ee569018 1188 return ((pcm == NULL
3e71d8f2 1189 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
ee569018 1190 ? NULL : pcm);
06a2c219 1191}
b73b6aaf 1192
57b03282 1193
06a2c219
GM
1194/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1195 the two-byte form of C. Encoding is returned in *CHAR2B. */
dc43ef94 1196
06a2c219
GM
1197static INLINE void
1198x_encode_char (c, char2b, font_info)
1199 int c;
1200 XChar2b *char2b;
1201 struct font_info *font_info;
1202{
1203 int charset = CHAR_CHARSET (c);
1204 XFontStruct *font = font_info->font;
1205
1206 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1207 This may be either a program in a special encoder language or a
1208 fixed encoding. */
1209 if (font_info->font_encoder)
1210 {
1211 /* It's a program. */
1212 struct ccl_program *ccl = font_info->font_encoder;
1213
1214 if (CHARSET_DIMENSION (charset) == 1)
1215 {
1216 ccl->reg[0] = charset;
1217 ccl->reg[1] = char2b->byte2;
1218 }
1219 else
1220 {
1221 ccl->reg[0] = charset;
1222 ccl->reg[1] = char2b->byte1;
1223 ccl->reg[2] = char2b->byte2;
1224 }
1225
1226 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1227
1228 /* We assume that MSBs are appropriately set/reset by CCL
1229 program. */
1230 if (font->max_byte1 == 0) /* 1-byte font */
ee569018 1231 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
06a2c219
GM
1232 else
1233 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1234 }
1235 else if (font_info->encoding[charset])
1236 {
1237 /* Fixed encoding scheme. See fontset.h for the meaning of the
1238 encoding numbers. */
1239 int enc = font_info->encoding[charset];
1240
1241 if ((enc == 1 || enc == 2)
1242 && CHARSET_DIMENSION (charset) == 2)
1243 char2b->byte1 |= 0x80;
1244
1245 if (enc == 1 || enc == 3)
1246 char2b->byte2 |= 0x80;
1247 }
1248}
1249
1250
1251/* Get face and two-byte form of character C in face FACE_ID on frame
1252 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1253 means we want to display multibyte text. Value is a pointer to a
1254 realized face that is ready for display. */
1255
1256static INLINE struct face *
1257x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1258 struct frame *f;
1259 int c, face_id;
1260 XChar2b *char2b;
1261 int multibyte_p;
1262{
1263 struct face *face = FACE_FROM_ID (f, face_id);
1264
1265 if (!multibyte_p)
1266 {
1267 /* Unibyte case. We don't have to encode, but we have to make
1268 sure to use a face suitable for unibyte. */
1269 char2b->byte1 = 0;
1270 char2b->byte2 = c;
ee569018
KH
1271 face_id = FACE_FOR_CHAR (f, face, c);
1272 face = FACE_FROM_ID (f, face_id);
06a2c219
GM
1273 }
1274 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1275 {
1276 /* Case of ASCII in a face known to fit ASCII. */
1277 char2b->byte1 = 0;
1278 char2b->byte2 = c;
1279 }
1280 else
1281 {
1282 int c1, c2, charset;
1283
1284 /* Split characters into bytes. If c2 is -1 afterwards, C is
1285 really a one-byte character so that byte1 is zero. */
1286 SPLIT_CHAR (c, charset, c1, c2);
1287 if (c2 > 0)
1288 char2b->byte1 = c1, char2b->byte2 = c2;
1289 else
1290 char2b->byte1 = 0, char2b->byte2 = c1;
1291
06a2c219 1292 /* Maybe encode the character in *CHAR2B. */
ee569018 1293 if (face->font != NULL)
06a2c219
GM
1294 {
1295 struct font_info *font_info
1296 = FONT_INFO_FROM_ID (f, face->font_info_id);
1297 if (font_info)
ee569018 1298 x_encode_char (c, char2b, font_info);
06a2c219
GM
1299 }
1300 }
1301
1302 /* Make sure X resources of the face are allocated. */
1303 xassert (face != NULL);
1304 PREPARE_FACE_FOR_DISPLAY (f, face);
1305
1306 return face;
1307}
1308
1309
1310/* Get face and two-byte form of character glyph GLYPH on frame F.
43d120d8 1311 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
06a2c219
GM
1312 a pointer to a realized face that is ready for display. */
1313
1314static INLINE struct face *
ee569018 1315x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
06a2c219
GM
1316 struct frame *f;
1317 struct glyph *glyph;
1318 XChar2b *char2b;
ee569018 1319 int *two_byte_p;
06a2c219
GM
1320{
1321 struct face *face;
1322
1323 xassert (glyph->type == CHAR_GLYPH);
43d120d8 1324 face = FACE_FROM_ID (f, glyph->face_id);
06a2c219 1325
ee569018
KH
1326 if (two_byte_p)
1327 *two_byte_p = 0;
1328
06a2c219
GM
1329 if (!glyph->multibyte_p)
1330 {
1331 /* Unibyte case. We don't have to encode, but we have to make
1332 sure to use a face suitable for unibyte. */
1333 char2b->byte1 = 0;
43d120d8 1334 char2b->byte2 = glyph->u.ch;
06a2c219 1335 }
43d120d8
KH
1336 else if (glyph->u.ch < 128
1337 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
06a2c219
GM
1338 {
1339 /* Case of ASCII in a face known to fit ASCII. */
1340 char2b->byte1 = 0;
43d120d8 1341 char2b->byte2 = glyph->u.ch;
06a2c219
GM
1342 }
1343 else
1344 {
1345 int c1, c2, charset;
1346
1347 /* Split characters into bytes. If c2 is -1 afterwards, C is
1348 really a one-byte character so that byte1 is zero. */
43d120d8 1349 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
06a2c219
GM
1350 if (c2 > 0)
1351 char2b->byte1 = c1, char2b->byte2 = c2;
1352 else
1353 char2b->byte1 = 0, char2b->byte2 = c1;
1354
1355 /* Maybe encode the character in *CHAR2B. */
1356 if (charset != CHARSET_ASCII)
1357 {
1358 struct font_info *font_info
1359 = FONT_INFO_FROM_ID (f, face->font_info_id);
1360 if (font_info)
1361 {
43d120d8 1362 x_encode_char (glyph->u.ch, char2b, font_info);
ee569018
KH
1363 if (two_byte_p)
1364 *two_byte_p
1365 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
06a2c219
GM
1366 }
1367 }
1368 }
1369
1370 /* Make sure X resources of the face are allocated. */
1371 xassert (face != NULL);
1372 PREPARE_FACE_FOR_DISPLAY (f, face);
1373 return face;
1374}
1375
1376
1377/* Store one glyph for IT->char_to_display in IT->glyph_row.
1378 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1379
1380static INLINE void
1381x_append_glyph (it)
1382 struct it *it;
1383{
1384 struct glyph *glyph;
1385 enum glyph_row_area area = it->area;
1386
1387 xassert (it->glyph_row);
1388 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1389
1390 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1391 if (glyph < it->glyph_row->glyphs[area + 1])
1392 {
1393 /* Play it safe. If sub-structures of the glyph are not all the
1394 same size, it otherwise be that some bits stay set. This
1395 would prevent a comparison with GLYPH_EQUAL_P. */
1396 glyph->u.val = 0;
1397
1398 glyph->type = CHAR_GLYPH;
1399 glyph->pixel_width = it->pixel_width;
43d120d8
KH
1400 glyph->u.ch = it->char_to_display;
1401 glyph->face_id = it->face_id;
06a2c219
GM
1402 glyph->charpos = CHARPOS (it->position);
1403 glyph->object = it->object;
1404 glyph->left_box_line_p = it->start_of_box_run_p;
1405 glyph->right_box_line_p = it->end_of_box_run_p;
1406 glyph->voffset = it->voffset;
1407 glyph->multibyte_p = it->multibyte_p;
66ac4b0e
GM
1408 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1409 || it->phys_descent > it->descent);
ee569018 1410 glyph->glyph_not_available_p = it->glyph_not_available_p;
06a2c219
GM
1411 ++it->glyph_row->used[area];
1412 }
1413}
1414
b4192550
KH
1415/* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1416 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1417
1418static INLINE void
1419x_append_composite_glyph (it)
1420 struct it *it;
1421{
1422 struct glyph *glyph;
1423 enum glyph_row_area area = it->area;
1424
1425 xassert (it->glyph_row);
1426
1427 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1428 if (glyph < it->glyph_row->glyphs[area + 1])
1429 {
1430 /* Play it safe. If sub-structures of the glyph are not all the
1431 same size, it otherwise be that some bits stay set. This
1432 would prevent a comparison with GLYPH_EQUAL_P. */
1433 glyph->u.val = 0;
1434
1435 glyph->type = COMPOSITE_GLYPH;
1436 glyph->pixel_width = it->pixel_width;
43d120d8
KH
1437 glyph->u.cmp_id = it->cmp_id;
1438 glyph->face_id = it->face_id;
b4192550
KH
1439 glyph->charpos = CHARPOS (it->position);
1440 glyph->object = it->object;
1441 glyph->left_box_line_p = it->start_of_box_run_p;
1442 glyph->right_box_line_p = it->end_of_box_run_p;
1443 glyph->voffset = it->voffset;
1444 glyph->multibyte_p = it->multibyte_p;
1445 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1446 || it->phys_descent > it->descent);
1447 ++it->glyph_row->used[area];
1448 }
1449}
1450
06a2c219
GM
1451
1452/* Change IT->ascent and IT->height according to the setting of
1453 IT->voffset. */
1454
1455static INLINE void
1456take_vertical_position_into_account (it)
1457 struct it *it;
1458{
1459 if (it->voffset)
1460 {
1461 if (it->voffset < 0)
1462 /* Increase the ascent so that we can display the text higher
1463 in the line. */
1464 it->ascent += abs (it->voffset);
1465 else
1466 /* Increase the descent so that we can display the text lower
1467 in the line. */
1468 it->descent += it->voffset;
1469 }
1470}
1471
1472
1473/* Produce glyphs/get display metrics for the image IT is loaded with.
1474 See the description of struct display_iterator in dispextern.h for
1475 an overview of struct display_iterator. */
1476
1477static void
1478x_produce_image_glyph (it)
1479 struct it *it;
1480{
1481 struct image *img;
1482 struct face *face;
1483
1484 xassert (it->what == IT_IMAGE);
1485
1486 face = FACE_FROM_ID (it->f, it->face_id);
1487 img = IMAGE_FROM_ID (it->f, it->image_id);
1488 xassert (img);
1489
1490 /* Make sure X resources of the face and image are loaded. */
1491 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1492 prepare_image_for_display (it->f, img);
1493
95af8492 1494 it->ascent = it->phys_ascent = image_ascent (img, face);
66ac4b0e 1495 it->descent = it->phys_descent = img->height + 2 * img->margin - it->ascent;
06a2c219
GM
1496 it->pixel_width = img->width + 2 * img->margin;
1497
1498 it->nglyphs = 1;
1499
1500 if (face->box != FACE_NO_BOX)
1501 {
1502 it->ascent += face->box_line_width;
1503 it->descent += face->box_line_width;
1504
1505 if (it->start_of_box_run_p)
1506 it->pixel_width += face->box_line_width;
1507 if (it->end_of_box_run_p)
1508 it->pixel_width += face->box_line_width;
1509 }
1510
1511 take_vertical_position_into_account (it);
1512
1513 if (it->glyph_row)
1514 {
1515 struct glyph *glyph;
1516 enum glyph_row_area area = it->area;
1517
1518 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1519 if (glyph < it->glyph_row->glyphs[area + 1])
1520 {
1521 glyph->type = IMAGE_GLYPH;
43d120d8
KH
1522 glyph->u.img_id = img->id;
1523 glyph->face_id = it->face_id;
06a2c219
GM
1524 glyph->pixel_width = it->pixel_width;
1525 glyph->charpos = CHARPOS (it->position);
1526 glyph->object = it->object;
1527 glyph->left_box_line_p = it->start_of_box_run_p;
1528 glyph->right_box_line_p = it->end_of_box_run_p;
1529 glyph->voffset = it->voffset;
1530 glyph->multibyte_p = it->multibyte_p;
1531 ++it->glyph_row->used[area];
1532 }
1533 }
1534}
1535
1536
1537/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1538 of the glyph, WIDTH and HEIGHT are the width and height of the
1539 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1540 ascent of the glyph (0 <= ASCENT <= 1). */
1541
1542static void
1543x_append_stretch_glyph (it, object, width, height, ascent)
1544 struct it *it;
1545 Lisp_Object object;
1546 int width, height;
1547 double ascent;
1548{
1549 struct glyph *glyph;
1550 enum glyph_row_area area = it->area;
1551
1552 xassert (ascent >= 0 && ascent <= 1);
1553
1554 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1555 if (glyph < it->glyph_row->glyphs[area + 1])
1556 {
1557 glyph->type = STRETCH_GLYPH;
1558 glyph->u.stretch.ascent = height * ascent;
1559 glyph->u.stretch.height = height;
43d120d8 1560 glyph->face_id = it->face_id;
06a2c219
GM
1561 glyph->pixel_width = width;
1562 glyph->charpos = CHARPOS (it->position);
1563 glyph->object = object;
1564 glyph->left_box_line_p = it->start_of_box_run_p;
1565 glyph->right_box_line_p = it->end_of_box_run_p;
1566 glyph->voffset = it->voffset;
1567 glyph->multibyte_p = it->multibyte_p;
1568 ++it->glyph_row->used[area];
1569 }
1570}
1571
1572
1573/* Produce a stretch glyph for iterator IT. IT->object is the value
1574 of the glyph property displayed. The value must be a list
1575 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1576 being recognized:
1577
1578 1. `:width WIDTH' specifies that the space should be WIDTH *
1579 canonical char width wide. WIDTH may be an integer or floating
1580 point number.
1581
1582 2. `:relative-width FACTOR' specifies that the width of the stretch
1583 should be computed from the width of the first character having the
1584 `glyph' property, and should be FACTOR times that width.
1585
1586 3. `:align-to HPOS' specifies that the space should be wide enough
1587 to reach HPOS, a value in canonical character units.
1588
1589 Exactly one of the above pairs must be present.
1590
1591 4. `:height HEIGHT' specifies that the height of the stretch produced
1592 should be HEIGHT, measured in canonical character units.
1593
1594 5. `:relative-height FACTOR' specifies that the height of the the
1595 stretch should be FACTOR times the height of the characters having
1596 the glyph property.
1597
1598 Either none or exactly one of 4 or 5 must be present.
1599
1600 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1601 of the stretch should be used for the ascent of the stretch.
1602 ASCENT must be in the range 0 <= ASCENT <= 100. */
1603
1604#define NUMVAL(X) \
1605 ((INTEGERP (X) || FLOATP (X)) \
1606 ? XFLOATINT (X) \
1607 : - 1)
1608
1609
1610static void
1611x_produce_stretch_glyph (it)
1612 struct it *it;
1613{
1614 /* (space :width WIDTH :height HEIGHT. */
3e71d8f2
GM
1615#if GLYPH_DEBUG
1616 extern Lisp_Object Qspace;
1617#endif
1618 extern Lisp_Object QCwidth, QCheight, QCascent;
06a2c219
GM
1619 extern Lisp_Object QCrelative_width, QCrelative_height;
1620 extern Lisp_Object QCalign_to;
1621 Lisp_Object prop, plist;
1622 double width = 0, height = 0, ascent = 0;
1623 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1624 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1625
1626 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1627
1628 /* List should start with `space'. */
1629 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1630 plist = XCDR (it->object);
1631
1632 /* Compute the width of the stretch. */
1633 if (prop = Fplist_get (plist, QCwidth),
1634 NUMVAL (prop) > 0)
1635 /* Absolute width `:width WIDTH' specified and valid. */
1636 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1637 else if (prop = Fplist_get (plist, QCrelative_width),
1638 NUMVAL (prop) > 0)
1639 {
1640 /* Relative width `:relative-width FACTOR' specified and valid.
1641 Compute the width of the characters having the `glyph'
1642 property. */
1643 struct it it2;
1644 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1645
1646 it2 = *it;
1647 if (it->multibyte_p)
1648 {
1649 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1650 - IT_BYTEPOS (*it));
1651 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1652 }
1653 else
1654 it2.c = *p, it2.len = 1;
1655
1656 it2.glyph_row = NULL;
1657 it2.what = IT_CHARACTER;
1658 x_produce_glyphs (&it2);
1659 width = NUMVAL (prop) * it2.pixel_width;
1660 }
1661 else if (prop = Fplist_get (plist, QCalign_to),
1662 NUMVAL (prop) > 0)
1663 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1664 else
1665 /* Nothing specified -> width defaults to canonical char width. */
1666 width = CANON_X_UNIT (it->f);
1667
1668 /* Compute height. */
1669 if (prop = Fplist_get (plist, QCheight),
1670 NUMVAL (prop) > 0)
1671 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1672 else if (prop = Fplist_get (plist, QCrelative_height),
1673 NUMVAL (prop) > 0)
1674 height = FONT_HEIGHT (font) * NUMVAL (prop);
1675 else
1676 height = FONT_HEIGHT (font);
1677
1678 /* Compute percentage of height used for ascent. If
1679 `:ascent ASCENT' is present and valid, use that. Otherwise,
1680 derive the ascent from the font in use. */
1681 if (prop = Fplist_get (plist, QCascent),
1682 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1683 ascent = NUMVAL (prop) / 100.0;
1684 else
1685 ascent = (double) font->ascent / FONT_HEIGHT (font);
1686
1687 if (width <= 0)
1688 width = 1;
1689 if (height <= 0)
1690 height = 1;
1691
1692 if (it->glyph_row)
1693 {
1694 Lisp_Object object = it->stack[it->sp - 1].string;
1695 if (!STRINGP (object))
1696 object = it->w->buffer;
1697 x_append_stretch_glyph (it, object, width, height, ascent);
1698 }
1699
1700 it->pixel_width = width;
66ac4b0e
GM
1701 it->ascent = it->phys_ascent = height * ascent;
1702 it->descent = it->phys_descent = height - it->ascent;
06a2c219
GM
1703 it->nglyphs = 1;
1704
1705 if (face->box != FACE_NO_BOX)
1706 {
1707 it->ascent += face->box_line_width;
1708 it->descent += face->box_line_width;
1709
1710 if (it->start_of_box_run_p)
1711 it->pixel_width += face->box_line_width;
1712 if (it->end_of_box_run_p)
1713 it->pixel_width += face->box_line_width;
1714 }
1715
1716 take_vertical_position_into_account (it);
1717}
1718
b4192550
KH
1719/* Return proper value to be used as baseline offset of font that has
1720 ASCENT and DESCENT to draw characters by the font at the vertical
1721 center of the line of frame F.
1722
1723 Here, out task is to find the value of BOFF in the following figure;
1724
1725 -------------------------+-----------+-
1726 -+-+---------+-+ | |
1727 | | | | | |
1728 | | | | F_ASCENT F_HEIGHT
1729 | | | ASCENT | |
1730 HEIGHT | | | | |
1731 | | |-|-+------+-----------|------- baseline
1732 | | | | BOFF | |
1733 | |---------|-+-+ | |
1734 | | | DESCENT | |
1735 -+-+---------+-+ F_DESCENT |
1736 -------------------------+-----------+-
1737
1738 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1739 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1740 DESCENT = FONT->descent
1741 HEIGHT = FONT_HEIGHT (FONT)
1742 F_DESCENT = (F->output_data.x->font->descent
1743 - F->output_data.x->baseline_offset)
1744 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1745*/
1746
1747#define VCENTER_BASELINE_OFFSET(FONT, F) \
1748 ((FONT)->descent \
1749 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT))) / 2 \
1750 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
06a2c219
GM
1751
1752/* Produce glyphs/get display metrics for the display element IT is
1753 loaded with. See the description of struct display_iterator in
1754 dispextern.h for an overview of struct display_iterator. */
1755
1756static void
1757x_produce_glyphs (it)
1758 struct it *it;
1759{
ee569018
KH
1760 it->glyph_not_available_p = 0;
1761
06a2c219
GM
1762 if (it->what == IT_CHARACTER)
1763 {
1764 XChar2b char2b;
1765 XFontStruct *font;
ee569018 1766 struct face *face = FACE_FROM_ID (it->f, it->face_id);
06a2c219 1767 XCharStruct *pcm;
06a2c219 1768 int font_not_found_p;
b4192550
KH
1769 struct font_info *font_info;
1770 int boff; /* baseline offset */
06a2c219 1771
ee569018
KH
1772 /* Maybe translate single-byte characters to multibyte, or the
1773 other way. */
06a2c219 1774 it->char_to_display = it->c;
ee569018 1775 if (!ASCII_BYTE_P (it->c))
06a2c219 1776 {
ee569018
KH
1777 if (unibyte_display_via_language_environment
1778 && SINGLE_BYTE_CHAR_P (it->c)
1779 && (it->c >= 0240
1780 || !NILP (Vnonascii_translation_table)))
1781 {
1782 it->char_to_display = unibyte_char_to_multibyte (it->c);
1783 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1784 face = FACE_FROM_ID (it->f, it->face_id);
1785 }
1786 else if (!SINGLE_BYTE_CHAR_P (it->c)
1787 && !it->multibyte_p)
1788 {
1789 it->char_to_display = multibyte_char_to_unibyte (it->c, Qnil);
1790 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1791 face = FACE_FROM_ID (it->f, it->face_id);
1792 }
06a2c219
GM
1793 }
1794
ee569018
KH
1795 /* Get font to use. Encode IT->char_to_display. */
1796 x_get_char_face_and_encoding (it->f, it->char_to_display,
1797 it->face_id, &char2b,
1798 it->multibyte_p);
06a2c219
GM
1799 font = face->font;
1800
1801 /* When no suitable font found, use the default font. */
1802 font_not_found_p = font == NULL;
1803 if (font_not_found_p)
b4192550
KH
1804 {
1805 font = FRAME_FONT (it->f);
1806 boff = it->f->output_data.x->baseline_offset;
1807 font_info = NULL;
1808 }
1809 else
1810 {
1811 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1812 boff = font_info->baseline_offset;
1813 if (font_info->vertical_centering)
1814 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1815 }
06a2c219
GM
1816
1817 if (it->char_to_display >= ' '
1818 && (!it->multibyte_p || it->char_to_display < 128))
1819 {
1820 /* Either unibyte or ASCII. */
1821 int stretched_p;
1822
1823 it->nglyphs = 1;
06a2c219
GM
1824
1825 pcm = x_per_char_metric (font, &char2b);
b4192550
KH
1826 it->ascent = font->ascent + boff;
1827 it->descent = font->descent - boff;
474848ac
GM
1828
1829 if (pcm)
1830 {
1831 it->phys_ascent = pcm->ascent + boff;
1832 it->phys_descent = pcm->descent - boff;
1833 it->pixel_width = pcm->width;
1834 }
1835 else
1836 {
1837 it->glyph_not_available_p = 1;
1838 it->phys_ascent = font->ascent + boff;
1839 it->phys_descent = font->descent - boff;
1840 it->pixel_width = FONT_WIDTH (font);
1841 }
06a2c219
GM
1842
1843 /* If this is a space inside a region of text with
1844 `space-width' property, change its width. */
1845 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1846 if (stretched_p)
1847 it->pixel_width *= XFLOATINT (it->space_width);
1848
1849 /* If face has a box, add the box thickness to the character
1850 height. If character has a box line to the left and/or
1851 right, add the box line width to the character's width. */
1852 if (face->box != FACE_NO_BOX)
1853 {
1854 int thick = face->box_line_width;
1855
1856 it->ascent += thick;
1857 it->descent += thick;
1858
1859 if (it->start_of_box_run_p)
1860 it->pixel_width += thick;
1861 if (it->end_of_box_run_p)
1862 it->pixel_width += thick;
1863 }
1864
1865 /* If face has an overline, add the height of the overline
1866 (1 pixel) and a 1 pixel margin to the character height. */
1867 if (face->overline_p)
1868 it->ascent += 2;
1869
1870 take_vertical_position_into_account (it);
1871
1872 /* If we have to actually produce glyphs, do it. */
1873 if (it->glyph_row)
1874 {
1875 if (stretched_p)
1876 {
1877 /* Translate a space with a `space-width' property
1878 into a stretch glyph. */
1879 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1880 x_append_stretch_glyph (it, it->object, it->pixel_width,
1881 it->ascent + it->descent, ascent);
1882 }
1883 else
1884 x_append_glyph (it);
1885
1886 /* If characters with lbearing or rbearing are displayed
1887 in this line, record that fact in a flag of the
1888 glyph row. This is used to optimize X output code. */
1c7e22fd 1889 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
06a2c219
GM
1890 it->glyph_row->contains_overlapping_glyphs_p = 1;
1891 }
1892 }
1893 else if (it->char_to_display == '\n')
1894 {
1895 /* A newline has no width but we need the height of the line. */
1896 it->pixel_width = 0;
1897 it->nglyphs = 0;
b4192550
KH
1898 it->ascent = it->phys_ascent = font->ascent + boff;
1899 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1900
1901 if (face->box != FACE_NO_BOX)
1902 {
1903 int thick = face->box_line_width;
1904 it->ascent += thick;
1905 it->descent += thick;
1906 }
1907 }
1908 else if (it->char_to_display == '\t')
1909 {
1910 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
d365f5bb 1911 int x = it->current_x + it->continuation_lines_width;
06a2c219
GM
1912 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1913
1914 it->pixel_width = next_tab_x - x;
1915 it->nglyphs = 1;
b4192550
KH
1916 it->ascent = it->phys_ascent = font->ascent + boff;
1917 it->descent = it->phys_descent = font->descent - boff;
06a2c219
GM
1918
1919 if (it->glyph_row)
1920 {
1921 double ascent = (double) it->ascent / (it->ascent + it->descent);
1922 x_append_stretch_glyph (it, it->object, it->pixel_width,
1923 it->ascent + it->descent, ascent);
1924 }
1925 }
1926 else
1927 {
1928 /* A multi-byte character. Assume that the display width of the
1929 character is the width of the character multiplied by the
b4192550 1930 width of the font. */
06a2c219 1931
b4192550
KH
1932 /* If we found a font, this font should give us the right
1933 metrics. If we didn't find a font, use the frame's
1934 default font and calculate the width of the character
1935 from the charset width; this is what old redisplay code
1936 did. */
1937 pcm = x_per_char_metric (font, &char2b);
ee569018
KH
1938 if (font_not_found_p || !pcm)
1939 {
1940 int charset = CHAR_CHARSET (it->char_to_display);
1941
1942 it->glyph_not_available_p = 1;
1943 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
1944 * CHARSET_WIDTH (charset));
1945 it->phys_ascent = font->ascent + boff;
1946 it->phys_descent = font->descent - boff;
1947 }
1948 else
1949 {
1950 it->pixel_width = pcm->width;
1951 it->phys_ascent = pcm->ascent + boff;
1952 it->phys_descent = pcm->descent - boff;
1953 if (it->glyph_row
1954 && (pcm->lbearing < 0
1955 || pcm->rbearing > pcm->width))
1956 it->glyph_row->contains_overlapping_glyphs_p = 1;
1957 }
b4192550
KH
1958 it->nglyphs = 1;
1959 it->ascent = font->ascent + boff;
1960 it->descent = font->descent - boff;
06a2c219
GM
1961 if (face->box != FACE_NO_BOX)
1962 {
1963 int thick = face->box_line_width;
1964 it->ascent += thick;
1965 it->descent += thick;
1966
1967 if (it->start_of_box_run_p)
1968 it->pixel_width += thick;
1969 if (it->end_of_box_run_p)
1970 it->pixel_width += thick;
1971 }
1972
1973 /* If face has an overline, add the height of the overline
1974 (1 pixel) and a 1 pixel margin to the character height. */
1975 if (face->overline_p)
1976 it->ascent += 2;
1977
1978 take_vertical_position_into_account (it);
1979
1980 if (it->glyph_row)
1981 x_append_glyph (it);
1982 }
1983 }
b4192550
KH
1984 else if (it->what == IT_COMPOSITION)
1985 {
1986 /* Note: A composition is represented as one glyph in the
1987 glyph matrix. There are no padding glyphs. */
1988 XChar2b char2b;
1989 XFontStruct *font;
ee569018 1990 struct face *face = FACE_FROM_ID (it->f, it->face_id);
b4192550
KH
1991 XCharStruct *pcm;
1992 int font_not_found_p;
1993 struct font_info *font_info;
1994 int boff; /* baseline offset */
1995 struct composition *cmp = composition_table[it->cmp_id];
1996
1997 /* Maybe translate single-byte characters to multibyte. */
1998 it->char_to_display = it->c;
1999 if (unibyte_display_via_language_environment
2000 && SINGLE_BYTE_CHAR_P (it->c)
2001 && (it->c >= 0240
2002 || (it->c >= 0200
2003 && !NILP (Vnonascii_translation_table))))
2004 {
2005 it->char_to_display = unibyte_char_to_multibyte (it->c);
b4192550
KH
2006 }
2007
2008 /* Get face and font to use. Encode IT->char_to_display. */
ee569018
KH
2009 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2010 face = FACE_FROM_ID (it->f, it->face_id);
2011 x_get_char_face_and_encoding (it->f, it->char_to_display,
2012 it->face_id, &char2b, it->multibyte_p);
b4192550
KH
2013 font = face->font;
2014
2015 /* When no suitable font found, use the default font. */
2016 font_not_found_p = font == NULL;
2017 if (font_not_found_p)
2018 {
2019 font = FRAME_FONT (it->f);
2020 boff = it->f->output_data.x->baseline_offset;
2021 font_info = NULL;
2022 }
2023 else
2024 {
2025 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2026 boff = font_info->baseline_offset;
2027 if (font_info->vertical_centering)
2028 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2029 }
2030
2031 /* There are no padding glyphs, so there is only one glyph to
2032 produce for the composition. Important is that pixel_width,
2033 ascent and descent are the values of what is drawn by
2034 draw_glyphs (i.e. the values of the overall glyphs composed). */
2035 it->nglyphs = 1;
2036
2037 /* If we have not yet calculated pixel size data of glyphs of
2038 the composition for the current face font, calculate them
2039 now. Theoretically, we have to check all fonts for the
2040 glyphs, but that requires much time and memory space. So,
2041 here we check only the font of the first glyph. This leads
2042 to incorrect display very rarely, and C-l (recenter) can
2043 correct the display anyway. */
2044 if (cmp->font != (void *) font)
2045 {
2046 /* Ascent and descent of the font of the first character of
2047 this composition (adjusted by baseline offset). Ascent
2048 and descent of overall glyphs should not be less than
2049 them respectively. */
2050 int font_ascent = font->ascent + boff;
2051 int font_descent = font->descent - boff;
2052 /* Bounding box of the overall glyphs. */
2053 int leftmost, rightmost, lowest, highest;
329bed06 2054 int i, width, ascent, descent;
b4192550
KH
2055
2056 cmp->font = (void *) font;
2057
2058 /* Initialize the bounding box. */
2059 pcm = x_per_char_metric (font, &char2b);
329bed06
GM
2060 if (pcm)
2061 {
2062 width = pcm->width;
2063 ascent = pcm->ascent;
2064 descent = pcm->descent;
2065 }
2066 else
2067 {
2068 width = FONT_WIDTH (font);
2069 ascent = font->ascent;
2070 descent = font->descent;
2071 }
2072
2073 rightmost = width;
2074 lowest = - descent + boff;
2075 highest = ascent + boff;
b4192550 2076 leftmost = 0;
329bed06 2077
b4192550
KH
2078 if (font_info
2079 && font_info->default_ascent
2080 && CHAR_TABLE_P (Vuse_default_ascent)
2081 && !NILP (Faref (Vuse_default_ascent,
2082 make_number (it->char_to_display))))
2083 highest = font_info->default_ascent + boff;
2084
2085 /* Draw the first glyph at the normal position. It may be
2086 shifted to right later if some other glyphs are drawn at
2087 the left. */
2088 cmp->offsets[0] = 0;
2089 cmp->offsets[1] = boff;
2090
2091 /* Set cmp->offsets for the remaining glyphs. */
2092 for (i = 1; i < cmp->glyph_len; i++)
2093 {
2094 int left, right, btm, top;
2095 int ch = COMPOSITION_GLYPH (cmp, i);
ee569018
KH
2096 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2097
2098 face = FACE_FROM_ID (it->f, face_id);
2099 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2100 it->multibyte_p);
b4192550
KH
2101 font = face->font;
2102 if (font == NULL)
2103 {
2104 font = FRAME_FONT (it->f);
2105 boff = it->f->output_data.x->baseline_offset;
2106 font_info = NULL;
2107 }
2108 else
2109 {
2110 font_info
2111 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2112 boff = font_info->baseline_offset;
2113 if (font_info->vertical_centering)
2114 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2115 }
2116
2117 pcm = x_per_char_metric (font, &char2b);
329bed06
GM
2118 if (pcm)
2119 {
2120 width = pcm->width;
2121 ascent = pcm->ascent;
2122 descent = pcm->descent;
2123 }
2124 else
2125 {
2126 width = FONT_WIDTH (font);
2127 ascent = font->ascent;
2128 descent = font->descent;
2129 }
b4192550
KH
2130
2131 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2132 {
2133 /* Relative composition with or without
2134 alternate chars. */
329bed06
GM
2135 left = (leftmost + rightmost - width) / 2;
2136 btm = - descent + boff;
b4192550
KH
2137 if (font_info && font_info->relative_compose
2138 && (! CHAR_TABLE_P (Vignore_relative_composition)
2139 || NILP (Faref (Vignore_relative_composition,
2140 make_number (ch)))))
2141 {
2142
329bed06 2143 if (- descent >= font_info->relative_compose)
b4192550
KH
2144 /* One extra pixel between two glyphs. */
2145 btm = highest + 1;
329bed06 2146 else if (ascent <= 0)
b4192550 2147 /* One extra pixel between two glyphs. */
329bed06 2148 btm = lowest - 1 - ascent - descent;
b4192550
KH
2149 }
2150 }
2151 else
2152 {
2153 /* A composition rule is specified by an integer
2154 value that encodes global and new reference
2155 points (GREF and NREF). GREF and NREF are
2156 specified by numbers as below:
2157
2158 0---1---2 -- ascent
2159 | |
2160 | |
2161 | |
2162 9--10--11 -- center
2163 | |
2164 ---3---4---5--- baseline
2165 | |
2166 6---7---8 -- descent
2167 */
2168 int rule = COMPOSITION_RULE (cmp, i);
2169 int gref, nref, grefx, grefy, nrefx, nrefy;
2170
2171 COMPOSITION_DECODE_RULE (rule, gref, nref);
2172 grefx = gref % 3, nrefx = nref % 3;
2173 grefy = gref / 3, nrefy = nref / 3;
2174
2175 left = (leftmost
2176 + grefx * (rightmost - leftmost) / 2
329bed06 2177 - nrefx * width / 2);
b4192550
KH
2178 btm = ((grefy == 0 ? highest
2179 : grefy == 1 ? 0
2180 : grefy == 2 ? lowest
2181 : (highest + lowest) / 2)
329bed06
GM
2182 - (nrefy == 0 ? ascent + descent
2183 : nrefy == 1 ? descent - boff
b4192550 2184 : nrefy == 2 ? 0
329bed06 2185 : (ascent + descent) / 2));
b4192550
KH
2186 }
2187
2188 cmp->offsets[i * 2] = left;
329bed06 2189 cmp->offsets[i * 2 + 1] = btm + descent;
b4192550
KH
2190
2191 /* Update the bounding box of the overall glyphs. */
329bed06
GM
2192 right = left + width;
2193 top = btm + descent + ascent;
b4192550
KH
2194 if (left < leftmost)
2195 leftmost = left;
2196 if (right > rightmost)
2197 rightmost = right;
2198 if (top > highest)
2199 highest = top;
2200 if (btm < lowest)
2201 lowest = btm;
2202 }
2203
2204 /* If there are glyphs whose x-offsets are negative,
2205 shift all glyphs to the right and make all x-offsets
2206 non-negative. */
2207 if (leftmost < 0)
2208 {
2209 for (i = 0; i < cmp->glyph_len; i++)
2210 cmp->offsets[i * 2] -= leftmost;
2211 rightmost -= leftmost;
2212 }
2213
2214 cmp->pixel_width = rightmost;
2215 cmp->ascent = highest;
2216 cmp->descent = - lowest;
2217 if (cmp->ascent < font_ascent)
2218 cmp->ascent = font_ascent;
2219 if (cmp->descent < font_descent)
2220 cmp->descent = font_descent;
2221 }
2222
2223 it->pixel_width = cmp->pixel_width;
2224 it->ascent = it->phys_ascent = cmp->ascent;
2225 it->descent = it->phys_descent = cmp->descent;
2226
2227 if (face->box != FACE_NO_BOX)
2228 {
2229 int thick = face->box_line_width;
2230 it->ascent += thick;
2231 it->descent += thick;
2232
2233 if (it->start_of_box_run_p)
2234 it->pixel_width += thick;
2235 if (it->end_of_box_run_p)
2236 it->pixel_width += thick;
2237 }
2238
2239 /* If face has an overline, add the height of the overline
2240 (1 pixel) and a 1 pixel margin to the character height. */
2241 if (face->overline_p)
2242 it->ascent += 2;
2243
2244 take_vertical_position_into_account (it);
2245
2246 if (it->glyph_row)
2247 x_append_composite_glyph (it);
2248 }
06a2c219
GM
2249 else if (it->what == IT_IMAGE)
2250 x_produce_image_glyph (it);
2251 else if (it->what == IT_STRETCH)
2252 x_produce_stretch_glyph (it);
2253
3017fdd1
GM
2254 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2255 because this isn't true for images with `:ascent 100'. */
2256 xassert (it->ascent >= 0 && it->descent >= 0);
06a2c219
GM
2257 if (it->area == TEXT_AREA)
2258 it->current_x += it->pixel_width;
66ac4b0e 2259
d365f5bb
GM
2260 it->descent += it->extra_line_spacing;
2261
06a2c219
GM
2262 it->max_ascent = max (it->max_ascent, it->ascent);
2263 it->max_descent = max (it->max_descent, it->descent);
66ac4b0e
GM
2264 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2265 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
06a2c219
GM
2266}
2267
2268
2269/* Estimate the pixel height of the mode or top line on frame F.
2270 FACE_ID specifies what line's height to estimate. */
2271
2272int
2273x_estimate_mode_line_height (f, face_id)
2274 struct frame *f;
2275 enum face_id face_id;
2276{
2277 int height = 1;
2278
2279 /* This function is called so early when Emacs starts that the face
2280 cache and mode line face are not yet initialized. */
2281 if (FRAME_FACE_CACHE (f))
2282 {
2283 struct face *face = FACE_FROM_ID (f, face_id);
2284 if (face)
2285 height = FONT_HEIGHT (face->font) + 2 * face->box_line_width;
2286 }
2287
2288 return height;
2289}
2290
2291\f
2292/***********************************************************************
2293 Glyph display
2294 ***********************************************************************/
2295
2296/* A sequence of glyphs to be drawn in the same face.
2297
2298 This data structure is not really completely X specific, so it
2299 could possibly, at least partially, be useful for other systems. It
2300 is currently not part of the external redisplay interface because
2301 it's not clear what other systems will need. */
2302
2303struct glyph_string
2304{
2305 /* X-origin of the string. */
2306 int x;
2307
2308 /* Y-origin and y-position of the base line of this string. */
2309 int y, ybase;
2310
2311 /* The width of the string, not including a face extension. */
2312 int width;
2313
2314 /* The width of the string, including a face extension. */
2315 int background_width;
2316
2317 /* The height of this string. This is the height of the line this
2318 string is drawn in, and can be different from the height of the
2319 font the string is drawn in. */
2320 int height;
2321
2322 /* Number of pixels this string overwrites in front of its x-origin.
2323 This number is zero if the string has an lbearing >= 0; it is
2324 -lbearing, if the string has an lbearing < 0. */
2325 int left_overhang;
2326
2327 /* Number of pixels this string overwrites past its right-most
2328 nominal x-position, i.e. x + width. Zero if the string's
2329 rbearing is <= its nominal width, rbearing - width otherwise. */
2330 int right_overhang;
2331
2332 /* The frame on which the glyph string is drawn. */
2333 struct frame *f;
2334
2335 /* The window on which the glyph string is drawn. */
2336 struct window *w;
2337
2338 /* X display and window for convenience. */
2339 Display *display;
2340 Window window;
2341
2342 /* The glyph row for which this string was built. It determines the
2343 y-origin and height of the string. */
2344 struct glyph_row *row;
2345
2346 /* The area within row. */
2347 enum glyph_row_area area;
2348
2349 /* Characters to be drawn, and number of characters. */
2350 XChar2b *char2b;
2351 int nchars;
2352
06a2c219
GM
2353 /* A face-override for drawing cursors, mouse face and similar. */
2354 enum draw_glyphs_face hl;
2355
2356 /* Face in which this string is to be drawn. */
2357 struct face *face;
2358
2359 /* Font in which this string is to be drawn. */
2360 XFontStruct *font;
2361
2362 /* Font info for this string. */
2363 struct font_info *font_info;
2364
b4192550
KH
2365 /* Non-null means this string describes (part of) a composition.
2366 All characters from char2b are drawn composed. */
2367 struct composition *cmp;
06a2c219
GM
2368
2369 /* Index of this glyph string's first character in the glyph
b4192550
KH
2370 definition of CMP. If this is zero, this glyph string describes
2371 the first character of a composition. */
06a2c219
GM
2372 int gidx;
2373
2374 /* 1 means this glyph strings face has to be drawn to the right end
2375 of the window's drawing area. */
2376 unsigned extends_to_end_of_line_p : 1;
2377
2378 /* 1 means the background of this string has been drawn. */
2379 unsigned background_filled_p : 1;
2380
2381 /* 1 means glyph string must be drawn with 16-bit functions. */
2382 unsigned two_byte_p : 1;
2383
2384 /* 1 means that the original font determined for drawing this glyph
2385 string could not be loaded. The member `font' has been set to
2386 the frame's default font in this case. */
2387 unsigned font_not_found_p : 1;
2388
2389 /* 1 means that the face in which this glyph string is drawn has a
2390 stipple pattern. */
2391 unsigned stippled_p : 1;
2392
66ac4b0e
GM
2393 /* 1 means only the foreground of this glyph string must be drawn,
2394 and we should use the physical height of the line this glyph
2395 string appears in as clip rect. */
2396 unsigned for_overlaps_p : 1;
2397
06a2c219
GM
2398 /* The GC to use for drawing this glyph string. */
2399 GC gc;
2400
2401 /* A pointer to the first glyph in the string. This glyph
2402 corresponds to char2b[0]. Needed to draw rectangles if
2403 font_not_found_p is 1. */
2404 struct glyph *first_glyph;
2405
2406 /* Image, if any. */
2407 struct image *img;
2408
2409 struct glyph_string *next, *prev;
2410};
2411
2412
5c187dee 2413#if 0
06a2c219
GM
2414
2415static void
2416x_dump_glyph_string (s)
2417 struct glyph_string *s;
2418{
2419 fprintf (stderr, "glyph string\n");
2420 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2421 s->x, s->y, s->width, s->height);
2422 fprintf (stderr, " ybase = %d\n", s->ybase);
2423 fprintf (stderr, " hl = %d\n", s->hl);
2424 fprintf (stderr, " left overhang = %d, right = %d\n",
2425 s->left_overhang, s->right_overhang);
2426 fprintf (stderr, " nchars = %d\n", s->nchars);
2427 fprintf (stderr, " extends to end of line = %d\n",
2428 s->extends_to_end_of_line_p);
2429 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2430 fprintf (stderr, " bg width = %d\n", s->background_width);
2431}
2432
2433#endif /* GLYPH_DEBUG */
2434
2435
2436
2437static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2438 struct glyph_string **,
2439 struct glyph_string *,
2440 struct glyph_string *));
2441static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2442 struct glyph_string **,
2443 struct glyph_string *,
2444 struct glyph_string *));
2445static void x_append_glyph_string P_ ((struct glyph_string **,
2446 struct glyph_string **,
2447 struct glyph_string *));
2448static int x_left_overwritten P_ ((struct glyph_string *));
2449static int x_left_overwriting P_ ((struct glyph_string *));
2450static int x_right_overwritten P_ ((struct glyph_string *));
2451static int x_right_overwriting P_ ((struct glyph_string *));
66ac4b0e
GM
2452static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2453 int));
06a2c219
GM
2454static void x_init_glyph_string P_ ((struct glyph_string *,
2455 XChar2b *, struct window *,
2456 struct glyph_row *,
2457 enum glyph_row_area, int,
2458 enum draw_glyphs_face));
2459static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2460 enum glyph_row_area, int, int,
66ac4b0e 2461 enum draw_glyphs_face, int *, int *, int));
06a2c219
GM
2462static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2463static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2464static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2465 int));
2466static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
b4192550 2467static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
06a2c219
GM
2468static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2469static void x_draw_glyph_string P_ ((struct glyph_string *));
2470static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2471static void x_set_cursor_gc P_ ((struct glyph_string *));
2472static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2473static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2474static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2475 int *, int *));
2476static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2477static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
68c45bf0 2478 unsigned long *, double, int));
06a2c219 2479static void x_setup_relief_color P_ ((struct frame *, struct relief *,
68c45bf0 2480 double, int, unsigned long));
06a2c219
GM
2481static void x_setup_relief_colors P_ ((struct glyph_string *));
2482static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2483static void x_draw_image_relief P_ ((struct glyph_string *));
2484static void x_draw_image_foreground P_ ((struct glyph_string *));
2485static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2486static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2487static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2488 int, int, int));
2489static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2490 int, int, int, int, XRectangle *));
2491static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2492 int, int, int, XRectangle *));
66ac4b0e
GM
2493static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2494 enum glyph_row_area));
06a2c219 2495
163dcff3
GM
2496#if GLYPH_DEBUG
2497static void x_check_font P_ ((struct frame *, XFontStruct *));
2498#endif
2499
06a2c219 2500
06a2c219
GM
2501/* Append the list of glyph strings with head H and tail T to the list
2502 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2503
2504static INLINE void
2505x_append_glyph_string_lists (head, tail, h, t)
2506 struct glyph_string **head, **tail;
2507 struct glyph_string *h, *t;
2508{
2509 if (h)
2510 {
2511 if (*head)
2512 (*tail)->next = h;
2513 else
2514 *head = h;
2515 h->prev = *tail;
2516 *tail = t;
2517 }
2518}
2519
2520
2521/* Prepend the list of glyph strings with head H and tail T to the
2522 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2523 result. */
2524
2525static INLINE void
2526x_prepend_glyph_string_lists (head, tail, h, t)
2527 struct glyph_string **head, **tail;
2528 struct glyph_string *h, *t;
2529{
2530 if (h)
2531 {
2532 if (*head)
2533 (*head)->prev = t;
2534 else
2535 *tail = t;
2536 t->next = *head;
2537 *head = h;
2538 }
2539}
2540
2541
2542/* Append glyph string S to the list with head *HEAD and tail *TAIL.
2543 Set *HEAD and *TAIL to the resulting list. */
2544
2545static INLINE void
2546x_append_glyph_string (head, tail, s)
2547 struct glyph_string **head, **tail;
2548 struct glyph_string *s;
2549{
2550 s->next = s->prev = NULL;
2551 x_append_glyph_string_lists (head, tail, s, s);
2552}
2553
2554
2555/* Set S->gc to a suitable GC for drawing glyph string S in cursor
2556 face. */
2557
2558static void
2559x_set_cursor_gc (s)
2560 struct glyph_string *s;
2561{
2562 if (s->font == FRAME_FONT (s->f)
2563 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2564 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
b4192550 2565 && !s->cmp)
06a2c219
GM
2566 s->gc = s->f->output_data.x->cursor_gc;
2567 else
2568 {
2569 /* Cursor on non-default face: must merge. */
2570 XGCValues xgcv;
2571 unsigned long mask;
2572
2573 xgcv.background = s->f->output_data.x->cursor_pixel;
2574 xgcv.foreground = s->face->background;
2575
2576 /* If the glyph would be invisible, try a different foreground. */
2577 if (xgcv.foreground == xgcv.background)
2578 xgcv.foreground = s->face->foreground;
2579 if (xgcv.foreground == xgcv.background)
2580 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2581 if (xgcv.foreground == xgcv.background)
2582 xgcv.foreground = s->face->foreground;
2583
2584 /* Make sure the cursor is distinct from text in this face. */
2585 if (xgcv.background == s->face->background
2586 && xgcv.foreground == s->face->foreground)
2587 {
2588 xgcv.background = s->face->foreground;
2589 xgcv.foreground = s->face->background;
2590 }
2591
2592 IF_DEBUG (x_check_font (s->f, s->font));
2593 xgcv.font = s->font->fid;
2594 xgcv.graphics_exposures = False;
2595 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2596
2597 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2598 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2599 mask, &xgcv);
2600 else
2601 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2602 = XCreateGC (s->display, s->window, mask, &xgcv);
2603
2604 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2605 }
2606}
2607
2608
2609/* Set up S->gc of glyph string S for drawing text in mouse face. */
2610
2611static void
2612x_set_mouse_face_gc (s)
2613 struct glyph_string *s;
2614{
2615 int face_id;
ee569018 2616 struct face *face;
06a2c219
GM
2617
2618 /* What face has to be used for the mouse face? */
2619 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
ee569018 2620 face = FACE_FROM_ID (s->f, face_id);
033e3e18
GM
2621 if (s->first_glyph->type == CHAR_GLYPH)
2622 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2623 else
2624 face_id = FACE_FOR_CHAR (s->f, face, 0);
06a2c219
GM
2625 s->face = FACE_FROM_ID (s->f, face_id);
2626 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2627
2628 /* If font in this face is same as S->font, use it. */
2629 if (s->font == s->face->font)
2630 s->gc = s->face->gc;
2631 else
2632 {
2633 /* Otherwise construct scratch_cursor_gc with values from FACE
2634 but font FONT. */
2635 XGCValues xgcv;
2636 unsigned long mask;
2637
2638 xgcv.background = s->face->background;
2639 xgcv.foreground = s->face->foreground;
2640 IF_DEBUG (x_check_font (s->f, s->font));
2641 xgcv.font = s->font->fid;
2642 xgcv.graphics_exposures = False;
2643 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2644
2645 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2646 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2647 mask, &xgcv);
2648 else
2649 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2650 = XCreateGC (s->display, s->window, mask, &xgcv);
2651
2652 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2653 }
2654
2655 xassert (s->gc != 0);
2656}
2657
2658
2659/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2660 Faces to use in the mode line have already been computed when the
2661 matrix was built, so there isn't much to do, here. */
2662
2663static INLINE void
2664x_set_mode_line_face_gc (s)
2665 struct glyph_string *s;
2666{
2667 s->gc = s->face->gc;
2668 xassert (s->gc != 0);
2669}
2670
2671
2672/* Set S->gc of glyph string S for drawing that glyph string. Set
2673 S->stippled_p to a non-zero value if the face of S has a stipple
2674 pattern. */
2675
2676static INLINE void
2677x_set_glyph_string_gc (s)
2678 struct glyph_string *s;
2679{
2680 if (s->hl == DRAW_NORMAL_TEXT)
2681 {
2682 s->gc = s->face->gc;
2683 s->stippled_p = s->face->stipple != 0;
2684 }
2685 else if (s->hl == DRAW_INVERSE_VIDEO)
2686 {
2687 x_set_mode_line_face_gc (s);
2688 s->stippled_p = s->face->stipple != 0;
2689 }
2690 else if (s->hl == DRAW_CURSOR)
2691 {
2692 x_set_cursor_gc (s);
2693 s->stippled_p = 0;
2694 }
2695 else if (s->hl == DRAW_MOUSE_FACE)
2696 {
2697 x_set_mouse_face_gc (s);
2698 s->stippled_p = s->face->stipple != 0;
2699 }
2700 else if (s->hl == DRAW_IMAGE_RAISED
2701 || s->hl == DRAW_IMAGE_SUNKEN)
2702 {
2703 s->gc = s->face->gc;
2704 s->stippled_p = s->face->stipple != 0;
2705 }
2706 else
2707 {
2708 s->gc = s->face->gc;
2709 s->stippled_p = s->face->stipple != 0;
2710 }
2711
2712 /* GC must have been set. */
2713 xassert (s->gc != 0);
2714}
2715
2716
2717/* Return in *R the clipping rectangle for glyph string S. */
2718
2719static void
2720x_get_glyph_string_clip_rect (s, r)
2721 struct glyph_string *s;
2722 XRectangle *r;
2723{
2724 if (s->row->full_width_p)
2725 {
2726 /* Draw full-width. X coordinates are relative to S->w->left. */
1da3fd71
GM
2727 int canon_x = CANON_X_UNIT (s->f);
2728
2729 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2730 r->width = XFASTINT (s->w->width) * canon_x;
06a2c219
GM
2731
2732 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2733 {
1da3fd71 2734 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
06a2c219
GM
2735 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2736 r->x -= width;
2737 }
2738
b9432a85 2739 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
1da3fd71 2740
06a2c219
GM
2741 /* Unless displaying a mode or menu bar line, which are always
2742 fully visible, clip to the visible part of the row. */
2743 if (s->w->pseudo_window_p)
2744 r->height = s->row->visible_height;
2745 else
2746 r->height = s->height;
2747 }
2748 else
2749 {
2750 /* This is a text line that may be partially visible. */
2751 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2752 r->width = window_box_width (s->w, s->area);
2753 r->height = s->row->visible_height;
2754 }
2755
2756 /* Don't use S->y for clipping because it doesn't take partially
2757 visible lines into account. For example, it can be negative for
2758 partially visible lines at the top of a window. */
2759 if (!s->row->full_width_p
2760 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
045dee35 2761 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
06a2c219
GM
2762 else
2763 r->y = max (0, s->row->y);
06a2c219 2764
9ea173e8 2765 /* If drawing a tool-bar window, draw it over the internal border
06a2c219 2766 at the top of the window. */
9ea173e8 2767 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219 2768 r->y -= s->f->output_data.x->internal_border_width;
66ac4b0e
GM
2769
2770 /* If S draws overlapping rows, it's sufficient to use the top and
2771 bottom of the window for clipping because this glyph string
2772 intentionally draws over other lines. */
2773 if (s->for_overlaps_p)
2774 {
045dee35 2775 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
66ac4b0e
GM
2776 r->height = window_text_bottom_y (s->w) - r->y;
2777 }
2778
2779 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
06a2c219
GM
2780}
2781
2782
2783/* Set clipping for output of glyph string S. S may be part of a mode
2784 line or menu if we don't have X toolkit support. */
2785
2786static INLINE void
2787x_set_glyph_string_clipping (s)
2788 struct glyph_string *s;
2789{
2790 XRectangle r;
2791 x_get_glyph_string_clip_rect (s, &r);
2792 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2793}
2794
2795
2796/* Compute left and right overhang of glyph string S. If S is a glyph
b4192550 2797 string for a composition, assume overhangs don't exist. */
06a2c219
GM
2798
2799static INLINE void
2800x_compute_glyph_string_overhangs (s)
2801 struct glyph_string *s;
2802{
b4192550 2803 if (s->cmp == NULL
06a2c219
GM
2804 && s->first_glyph->type == CHAR_GLYPH)
2805 {
2806 XCharStruct cs;
2807 int direction, font_ascent, font_descent;
2808 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2809 &font_ascent, &font_descent, &cs);
2810 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2811 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2812 }
2813}
2814
2815
2816/* Compute overhangs and x-positions for glyph string S and its
2817 predecessors, or successors. X is the starting x-position for S.
2818 BACKWARD_P non-zero means process predecessors. */
2819
2820static void
2821x_compute_overhangs_and_x (s, x, backward_p)
2822 struct glyph_string *s;
2823 int x;
2824 int backward_p;
2825{
2826 if (backward_p)
2827 {
2828 while (s)
2829 {
2830 x_compute_glyph_string_overhangs (s);
2831 x -= s->width;
2832 s->x = x;
2833 s = s->prev;
2834 }
2835 }
2836 else
2837 {
2838 while (s)
2839 {
2840 x_compute_glyph_string_overhangs (s);
2841 s->x = x;
2842 x += s->width;
2843 s = s->next;
2844 }
2845 }
2846}
2847
2848
2849/* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
b4192550
KH
2850 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2851 assumed to be zero. */
06a2c219
GM
2852
2853static void
2854x_get_glyph_overhangs (glyph, f, left, right)
2855 struct glyph *glyph;
2856 struct frame *f;
2857 int *left, *right;
2858{
06a2c219
GM
2859 *left = *right = 0;
2860
b4192550 2861 if (glyph->type == CHAR_GLYPH)
06a2c219
GM
2862 {
2863 XFontStruct *font;
2864 struct face *face;
2865 struct font_info *font_info;
2866 XChar2b char2b;
ee569018
KH
2867 XCharStruct *pcm;
2868
2869 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
06a2c219
GM
2870 font = face->font;
2871 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
ee569018
KH
2872 if (font
2873 && (pcm = x_per_char_metric (font, &char2b)))
06a2c219 2874 {
06a2c219
GM
2875 if (pcm->rbearing > pcm->width)
2876 *right = pcm->rbearing - pcm->width;
2877 if (pcm->lbearing < 0)
2878 *left = -pcm->lbearing;
2879 }
2880 }
2881}
2882
2883
2884/* Return the index of the first glyph preceding glyph string S that
2885 is overwritten by S because of S's left overhang. Value is -1
2886 if no glyphs are overwritten. */
2887
2888static int
2889x_left_overwritten (s)
2890 struct glyph_string *s;
2891{
2892 int k;
2893
2894 if (s->left_overhang)
2895 {
2896 int x = 0, i;
2897 struct glyph *glyphs = s->row->glyphs[s->area];
2898 int first = s->first_glyph - glyphs;
2899
2900 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2901 x -= glyphs[i].pixel_width;
2902
2903 k = i + 1;
2904 }
2905 else
2906 k = -1;
2907
2908 return k;
2909}
2910
2911
2912/* Return the index of the first glyph preceding glyph string S that
2913 is overwriting S because of its right overhang. Value is -1 if no
2914 glyph in front of S overwrites S. */
2915
2916static int
2917x_left_overwriting (s)
2918 struct glyph_string *s;
2919{
2920 int i, k, x;
2921 struct glyph *glyphs = s->row->glyphs[s->area];
2922 int first = s->first_glyph - glyphs;
2923
2924 k = -1;
2925 x = 0;
2926 for (i = first - 1; i >= 0; --i)
2927 {
2928 int left, right;
2929 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2930 if (x + right > 0)
2931 k = i;
2932 x -= glyphs[i].pixel_width;
2933 }
2934
2935 return k;
2936}
2937
2938
2939/* Return the index of the last glyph following glyph string S that is
2940 not overwritten by S because of S's right overhang. Value is -1 if
2941 no such glyph is found. */
2942
2943static int
2944x_right_overwritten (s)
2945 struct glyph_string *s;
2946{
2947 int k = -1;
2948
2949 if (s->right_overhang)
2950 {
2951 int x = 0, i;
2952 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 2953 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
2954 int end = s->row->used[s->area];
2955
2956 for (i = first; i < end && s->right_overhang > x; ++i)
2957 x += glyphs[i].pixel_width;
2958
2959 k = i;
2960 }
2961
2962 return k;
2963}
2964
2965
2966/* Return the index of the last glyph following glyph string S that
2967 overwrites S because of its left overhang. Value is negative
2968 if no such glyph is found. */
2969
2970static int
2971x_right_overwriting (s)
2972 struct glyph_string *s;
2973{
2974 int i, k, x;
2975 int end = s->row->used[s->area];
2976 struct glyph *glyphs = s->row->glyphs[s->area];
b4192550 2977 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
06a2c219
GM
2978
2979 k = -1;
2980 x = 0;
2981 for (i = first; i < end; ++i)
2982 {
2983 int left, right;
2984 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2985 if (x - left < 0)
2986 k = i;
2987 x += glyphs[i].pixel_width;
2988 }
2989
2990 return k;
2991}
2992
2993
2994/* Fill rectangle X, Y, W, H with background color of glyph string S. */
2995
2996static INLINE void
2997x_clear_glyph_string_rect (s, x, y, w, h)
2998 struct glyph_string *s;
2999 int x, y, w, h;
3000{
3001 XGCValues xgcv;
3002 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3003 XSetForeground (s->display, s->gc, xgcv.background);
3004 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3005 XSetForeground (s->display, s->gc, xgcv.foreground);
3006}
3007
3008
3009/* Draw the background of glyph_string S. If S->background_filled_p
3010 is non-zero don't draw it. FORCE_P non-zero means draw the
3011 background even if it wouldn't be drawn normally. This is used
b4192550
KH
3012 when a string preceding S draws into the background of S, or S
3013 contains the first component of a composition. */
06a2c219
GM
3014
3015static void
3016x_draw_glyph_string_background (s, force_p)
3017 struct glyph_string *s;
3018 int force_p;
3019{
3020 /* Nothing to do if background has already been drawn or if it
3021 shouldn't be drawn in the first place. */
3022 if (!s->background_filled_p)
3023 {
b4192550 3024 if (s->stippled_p)
06a2c219
GM
3025 {
3026 /* Fill background with a stipple pattern. */
3027 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3028 XFillRectangle (s->display, s->window, s->gc, s->x,
3029 s->y + s->face->box_line_width,
3030 s->background_width,
3031 s->height - 2 * s->face->box_line_width);
3032 XSetFillStyle (s->display, s->gc, FillSolid);
3033 s->background_filled_p = 1;
3034 }
3035 else if (FONT_HEIGHT (s->font) < s->height - 2 * s->face->box_line_width
3036 || s->font_not_found_p
3037 || s->extends_to_end_of_line_p
06a2c219
GM
3038 || force_p)
3039 {
3040 x_clear_glyph_string_rect (s, s->x, s->y + s->face->box_line_width,
3041 s->background_width,
3042 s->height - 2 * s->face->box_line_width);
3043 s->background_filled_p = 1;
3044 }
3045 }
3046}
3047
3048
3049/* Draw the foreground of glyph string S. */
3050
3051static void
3052x_draw_glyph_string_foreground (s)
3053 struct glyph_string *s;
3054{
3055 int i, x;
3056
3057 /* If first glyph of S has a left box line, start drawing the text
3058 of S to the right of that box line. */
3059 if (s->face->box != FACE_NO_BOX
3060 && s->first_glyph->left_box_line_p)
3061 x = s->x + s->face->box_line_width;
3062 else
3063 x = s->x;
3064
b4192550
KH
3065 /* Draw characters of S as rectangles if S's font could not be
3066 loaded. */
3067 if (s->font_not_found_p)
06a2c219 3068 {
b4192550 3069 for (i = 0; i < s->nchars; ++i)
06a2c219 3070 {
b4192550
KH
3071 struct glyph *g = s->first_glyph + i;
3072 XDrawRectangle (s->display, s->window,
3073 s->gc, x, s->y, g->pixel_width - 1,
3074 s->height - 1);
3075 x += g->pixel_width;
06a2c219
GM
3076 }
3077 }
3078 else
3079 {
b4192550
KH
3080 char *char1b = (char *) s->char2b;
3081 int boff = s->font_info->baseline_offset;
06a2c219 3082
b4192550
KH
3083 if (s->font_info->vertical_centering)
3084 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3085
3086 /* If we can use 8-bit functions, condense S->char2b. */
3087 if (!s->two_byte_p)
3088 for (i = 0; i < s->nchars; ++i)
3089 char1b[i] = s->char2b[i].byte2;
3090
3091 /* Draw text with XDrawString if background has already been
3092 filled. Otherwise, use XDrawImageString. (Note that
3093 XDrawImageString is usually faster than XDrawString.) Always
3094 use XDrawImageString when drawing the cursor so that there is
3095 no chance that characters under a box cursor are invisible. */
3096 if (s->for_overlaps_p
3097 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3098 {
3099 /* Draw characters with 16-bit or 8-bit functions. */
3100 if (s->two_byte_p)
3101 XDrawString16 (s->display, s->window, s->gc, x,
3102 s->ybase - boff, s->char2b, s->nchars);
3103 else
3104 XDrawString (s->display, s->window, s->gc, x,
3105 s->ybase - boff, char1b, s->nchars);
3106 }
06a2c219
GM
3107 else
3108 {
b4192550
KH
3109 if (s->two_byte_p)
3110 XDrawImageString16 (s->display, s->window, s->gc, x,
3111 s->ybase - boff, s->char2b, s->nchars);
06a2c219 3112 else
b4192550
KH
3113 XDrawImageString (s->display, s->window, s->gc, x,
3114 s->ybase - boff, char1b, s->nchars);
3115 }
3116 }
3117}
06a2c219 3118
b4192550 3119/* Draw the foreground of composite glyph string S. */
06a2c219 3120
b4192550
KH
3121static void
3122x_draw_composite_glyph_string_foreground (s)
3123 struct glyph_string *s;
3124{
3125 int i, x;
06a2c219 3126
b4192550
KH
3127 /* If first glyph of S has a left box line, start drawing the text
3128 of S to the right of that box line. */
3129 if (s->face->box != FACE_NO_BOX
3130 && s->first_glyph->left_box_line_p)
3131 x = s->x + s->face->box_line_width;
3132 else
3133 x = s->x;
06a2c219 3134
b4192550
KH
3135 /* S is a glyph string for a composition. S->gidx is the index of
3136 the first character drawn for glyphs of this composition.
3137 S->gidx == 0 means we are drawing the very first character of
3138 this composition. */
06a2c219 3139
b4192550
KH
3140 /* Draw a rectangle for the composition if the font for the very
3141 first character of the composition could not be loaded. */
3142 if (s->font_not_found_p)
3143 {
3144 if (s->gidx == 0)
3145 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3146 s->width - 1, s->height - 1);
3147 }
3148 else
3149 {
3150 for (i = 0; i < s->nchars; i++, ++s->gidx)
3151 XDrawString16 (s->display, s->window, s->gc,
3152 x + s->cmp->offsets[s->gidx * 2],
3153 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3154 s->char2b + i, 1);
06a2c219
GM
3155 }
3156}
3157
3158
80c32bcc
GM
3159#ifdef USE_X_TOOLKIT
3160
3e71d8f2 3161static struct frame *x_frame_of_widget P_ ((Widget));
80c32bcc 3162
3e71d8f2
GM
3163
3164/* Return the frame on which widget WIDGET is used.. Abort if frame
3165 cannot be determined. */
3166
3167struct frame *
3168x_frame_of_widget (widget)
80c32bcc 3169 Widget widget;
80c32bcc 3170{
80c32bcc 3171 struct x_display_info *dpyinfo;
5c187dee 3172 Lisp_Object tail;
3e71d8f2
GM
3173 struct frame *f;
3174
80c32bcc
GM
3175 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3176
3177 /* Find the top-level shell of the widget. Note that this function
3178 can be called when the widget is not yet realized, so XtWindow
3179 (widget) == 0. That's the reason we can't simply use
3180 x_any_window_to_frame. */
3181 while (!XtIsTopLevelShell (widget))
3182 widget = XtParent (widget);
3183
3184 /* Look for a frame with that top-level widget. Allocate the color
3185 on that frame to get the right gamma correction value. */
3186 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3187 if (GC_FRAMEP (XCAR (tail))
3188 && (f = XFRAME (XCAR (tail)),
3189 (f->output_data.nothing != 1
3190 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3191 && f->output_data.x->widget == widget)
3e71d8f2 3192 return f;
80c32bcc
GM
3193
3194 abort ();
3195}
3196
3e71d8f2
GM
3197
3198/* Allocate the color COLOR->pixel on the screen and display of
3199 widget WIDGET in colormap CMAP. If an exact match cannot be
3200 allocated, try the nearest color available. Value is non-zero
3201 if successful. This is called from lwlib. */
3202
3203int
3204x_alloc_nearest_color_for_widget (widget, cmap, color)
3205 Widget widget;
3206 Colormap cmap;
3207 XColor *color;
3208{
3209 struct frame *f = x_frame_of_widget (widget);
3210 return x_alloc_nearest_color (f, cmap, color);
3211}
3212
3213
80c32bcc
GM
3214#endif /* USE_X_TOOLKIT */
3215
3216
06a2c219
GM
3217/* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3218 CMAP. If an exact match can't be allocated, try the nearest color
3219 available. Value is non-zero if successful. Set *COLOR to the
3220 color allocated. */
3221
3222int
80c32bcc
GM
3223x_alloc_nearest_color (f, cmap, color)
3224 struct frame *f;
06a2c219
GM
3225 Colormap cmap;
3226 XColor *color;
3227{
80c32bcc
GM
3228 Display *display = FRAME_X_DISPLAY (f);
3229 Screen *screen = FRAME_X_SCREEN (f);
3230 int rc;
3231
3232 gamma_correct (f, color);
3233 rc = XAllocColor (display, cmap, color);
06a2c219
GM
3234 if (rc == 0)
3235 {
3236 /* If we got to this point, the colormap is full, so we're going
3237 to try to get the next closest color. The algorithm used is
3238 a least-squares matching, which is what X uses for closest
3239 color matching with StaticColor visuals. */
3240 int nearest, i;
3241 unsigned long nearest_delta = ~0;
3242 int ncells = XDisplayCells (display, XScreenNumberOfScreen (screen));
3243 XColor *cells = (XColor *) alloca (ncells * sizeof *cells);
3244
3245 for (i = 0; i < ncells; ++i)
3246 cells[i].pixel = i;
3247 XQueryColors (display, cmap, cells, ncells);
3248
3249 for (nearest = i = 0; i < ncells; ++i)
3250 {
3251 long dred = (color->red >> 8) - (cells[i].red >> 8);
3252 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3253 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3254 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3255
3256 if (delta < nearest_delta)
3257 {
3258 nearest = i;
3259 nearest_delta = delta;
3260 }
3261 }
3262
3263 color->red = cells[nearest].red;
3264 color->green = cells[nearest].green;
3265 color->blue = cells[nearest].blue;
3266 rc = XAllocColor (display, cmap, color);
3267 }
3268
d9c545da
GM
3269#ifdef DEBUG_X_COLORS
3270 if (rc)
3271 register_color (color->pixel);
3272#endif /* DEBUG_X_COLORS */
3273
06a2c219
GM
3274 return rc;
3275}
3276
3277
d9c545da
GM
3278/* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3279 It's necessary to do this instead of just using PIXEL directly to
3280 get color reference counts right. */
3281
3282unsigned long
3283x_copy_color (f, pixel)
3284 struct frame *f;
3285 unsigned long pixel;
3286{
3287 XColor color;
3288
3289 color.pixel = pixel;
3290 BLOCK_INPUT;
3291 XQueryColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3292 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3293 UNBLOCK_INPUT;
3294#ifdef DEBUG_X_COLORS
3295 register_color (pixel);
3296#endif
3297 return color.pixel;
3298}
3299
3300
3e71d8f2
GM
3301/* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3302 It's necessary to do this instead of just using PIXEL directly to
3303 get color reference counts right. */
3304
3305unsigned long
3306x_copy_dpy_color (dpy, cmap, pixel)
3307 Display *dpy;
3308 Colormap cmap;
3309 unsigned long pixel;
3310{
3311 XColor color;
3312
3313 color.pixel = pixel;
3314 BLOCK_INPUT;
3315 XQueryColor (dpy, cmap, &color);
3316 XAllocColor (dpy, cmap, &color);
3317 UNBLOCK_INPUT;
3318#ifdef DEBUG_X_COLORS
3319 register_color (pixel);
3320#endif
3321 return color.pixel;
3322}
3323
3324
06a2c219
GM
3325/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3326 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3327 If this produces the same color as PIXEL, try a color where all RGB
3328 values have DELTA added. Return the allocated color in *PIXEL.
3329 DISPLAY is the X display, CMAP is the colormap to operate on.
3330 Value is non-zero if successful. */
3331
3332static int
3333x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3334 struct frame *f;
3335 Display *display;
3336 Colormap cmap;
3337 unsigned long *pixel;
68c45bf0 3338 double factor;
06a2c219
GM
3339 int delta;
3340{
3341 XColor color, new;
3342 int success_p;
3343
3344 /* Get RGB color values. */
3345 color.pixel = *pixel;
3346 XQueryColor (display, cmap, &color);
3347
3348 /* Change RGB values by specified FACTOR. Avoid overflow! */
3349 xassert (factor >= 0);
3350 new.red = min (0xffff, factor * color.red);
3351 new.green = min (0xffff, factor * color.green);
3352 new.blue = min (0xffff, factor * color.blue);
3353
3354 /* Try to allocate the color. */
80c32bcc 3355 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3356 if (success_p)
3357 {
3358 if (new.pixel == *pixel)
3359 {
3360 /* If we end up with the same color as before, try adding
3361 delta to the RGB values. */
0d605c67 3362 x_free_colors (f, &new.pixel, 1);
06a2c219
GM
3363
3364 new.red = min (0xffff, delta + color.red);
3365 new.green = min (0xffff, delta + color.green);
3366 new.blue = min (0xffff, delta + color.blue);
80c32bcc 3367 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
3368 }
3369 else
3370 success_p = 1;
3371 *pixel = new.pixel;
3372 }
3373
3374 return success_p;
3375}
3376
3377
3378/* Set up the foreground color for drawing relief lines of glyph
3379 string S. RELIEF is a pointer to a struct relief containing the GC
3380 with which lines will be drawn. Use a color that is FACTOR or
3381 DELTA lighter or darker than the relief's background which is found
3382 in S->f->output_data.x->relief_background. If such a color cannot
3383 be allocated, use DEFAULT_PIXEL, instead. */
3384
3385static void
3386x_setup_relief_color (f, relief, factor, delta, default_pixel)
3387 struct frame *f;
3388 struct relief *relief;
68c45bf0 3389 double factor;
06a2c219
GM
3390 int delta;
3391 unsigned long default_pixel;
3392{
3393 XGCValues xgcv;
3394 struct x_output *di = f->output_data.x;
3395 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3396 unsigned long pixel;
3397 unsigned long background = di->relief_background;
43bd1b2b 3398 Colormap cmap = FRAME_X_COLORMAP (f);
dcd08bfb
GM
3399 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3400 Display *dpy = FRAME_X_DISPLAY (f);
06a2c219
GM
3401
3402 xgcv.graphics_exposures = False;
3403 xgcv.line_width = 1;
3404
3405 /* Free previously allocated color. The color cell will be reused
3406 when it has been freed as many times as it was allocated, so this
3407 doesn't affect faces using the same colors. */
3408 if (relief->gc
3409 && relief->allocated_p)
3410 {
0d605c67 3411 x_free_colors (f, &relief->pixel, 1);
06a2c219
GM
3412 relief->allocated_p = 0;
3413 }
3414
3415 /* Allocate new color. */
3416 xgcv.foreground = default_pixel;
3417 pixel = background;
dcd08bfb
GM
3418 if (dpyinfo->n_planes != 1
3419 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
06a2c219
GM
3420 {
3421 relief->allocated_p = 1;
3422 xgcv.foreground = relief->pixel = pixel;
3423 }
3424
3425 if (relief->gc == 0)
3426 {
dcd08bfb 3427 xgcv.stipple = dpyinfo->gray;
06a2c219 3428 mask |= GCStipple;
dcd08bfb 3429 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
06a2c219
GM
3430 }
3431 else
dcd08bfb 3432 XChangeGC (dpy, relief->gc, mask, &xgcv);
06a2c219
GM
3433}
3434
3435
3436/* Set up colors for the relief lines around glyph string S. */
3437
3438static void
3439x_setup_relief_colors (s)
3440 struct glyph_string *s;
3441{
3442 struct x_output *di = s->f->output_data.x;
3443 unsigned long color;
3444
3445 if (s->face->use_box_color_for_shadows_p)
3446 color = s->face->box_color;
3447 else
3448 {
3449 XGCValues xgcv;
3450
3451 /* Get the background color of the face. */
3452 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3453 color = xgcv.background;
3454 }
3455
3456 if (di->white_relief.gc == 0
3457 || color != di->relief_background)
3458 {
3459 di->relief_background = color;
3460 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3461 WHITE_PIX_DEFAULT (s->f));
3462 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3463 BLACK_PIX_DEFAULT (s->f));
3464 }
3465}
3466
3467
3468/* Draw a relief on frame F inside the rectangle given by LEFT_X,
3469 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3470 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3471 relief. LEFT_P non-zero means draw a relief on the left side of
3472 the rectangle. RIGHT_P non-zero means draw a relief on the right
3473 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3474 when drawing. */
3475
3476static void
3477x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3478 raised_p, left_p, right_p, clip_rect)
3479 struct frame *f;
3480 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3481 XRectangle *clip_rect;
3482{
3483 int i;
3484 GC gc;
3485
3486 if (raised_p)
3487 gc = f->output_data.x->white_relief.gc;
3488 else
3489 gc = f->output_data.x->black_relief.gc;
3490 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3491
3492 /* Top. */
3493 for (i = 0; i < width; ++i)
3494 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3495 left_x + i * left_p, top_y + i,
3496 right_x + 1 - i * right_p, top_y + i);
3497
3498 /* Left. */
3499 if (left_p)
3500 for (i = 0; i < width; ++i)
3501 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3502 left_x + i, top_y + i, left_x + i, bottom_y - i);
3503
3504 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3505 if (raised_p)
3506 gc = f->output_data.x->black_relief.gc;
3507 else
3508 gc = f->output_data.x->white_relief.gc;
3509 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3510
3511 /* Bottom. */
3512 for (i = 0; i < width; ++i)
3513 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3514 left_x + i * left_p, bottom_y - i,
3515 right_x + 1 - i * right_p, bottom_y - i);
3516
3517 /* Right. */
3518 if (right_p)
3519 for (i = 0; i < width; ++i)
3520 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3521 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3522
3523 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3524}
3525
3526
3527/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3528 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3529 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3530 left side of the rectangle. RIGHT_P non-zero means draw a line
3531 on the right side of the rectangle. CLIP_RECT is the clipping
3532 rectangle to use when drawing. */
3533
3534static void
3535x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3536 left_p, right_p, clip_rect)
3537 struct glyph_string *s;
3538 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3539 XRectangle *clip_rect;
3540{
3541 XGCValues xgcv;
3542
3543 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3544 XSetForeground (s->display, s->gc, s->face->box_color);
3545 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3546
3547 /* Top. */
3548 XFillRectangle (s->display, s->window, s->gc,
3549 left_x, top_y, right_x - left_x, width);
3550
3551 /* Left. */
3552 if (left_p)
3553 XFillRectangle (s->display, s->window, s->gc,
3554 left_x, top_y, width, bottom_y - top_y);
3555
3556 /* Bottom. */
3557 XFillRectangle (s->display, s->window, s->gc,
3558 left_x, bottom_y - width, right_x - left_x, width);
3559
3560 /* Right. */
3561 if (right_p)
3562 XFillRectangle (s->display, s->window, s->gc,
3563 right_x - width, top_y, width, bottom_y - top_y);
3564
3565 XSetForeground (s->display, s->gc, xgcv.foreground);
3566 XSetClipMask (s->display, s->gc, None);
3567}
3568
3569
3570/* Draw a box around glyph string S. */
3571
3572static void
3573x_draw_glyph_string_box (s)
3574 struct glyph_string *s;
3575{
3576 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3577 int left_p, right_p;
3578 struct glyph *last_glyph;
3579 XRectangle clip_rect;
3580
3581 last_x = window_box_right (s->w, s->area);
3582 if (s->row->full_width_p
3583 && !s->w->pseudo_window_p)
3584 {
110859fc 3585 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
06a2c219
GM
3586 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3587 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3588 }
3589
3590 /* The glyph that may have a right box line. */
b4192550 3591 last_glyph = (s->cmp || s->img
06a2c219
GM
3592 ? s->first_glyph
3593 : s->first_glyph + s->nchars - 1);
3594
3595 width = s->face->box_line_width;
3596 raised_p = s->face->box == FACE_RAISED_BOX;
3597 left_x = s->x;
3598 right_x = ((s->row->full_width_p
1da3fd71 3599 ? last_x - 1
a7aeb2de 3600 : min (last_x, s->x + s->background_width) - 1));
06a2c219
GM
3601 top_y = s->y;
3602 bottom_y = top_y + s->height - 1;
3603
3604 left_p = (s->first_glyph->left_box_line_p
3605 || (s->hl == DRAW_MOUSE_FACE
3606 && (s->prev == NULL
3607 || s->prev->hl != s->hl)));
3608 right_p = (last_glyph->right_box_line_p
3609 || (s->hl == DRAW_MOUSE_FACE
3610 && (s->next == NULL
3611 || s->next->hl != s->hl)));
3612
3613 x_get_glyph_string_clip_rect (s, &clip_rect);
3614
3615 if (s->face->box == FACE_SIMPLE_BOX)
3616 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3617 left_p, right_p, &clip_rect);
3618 else
3619 {
3620 x_setup_relief_colors (s);
3621 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3622 width, raised_p, left_p, right_p, &clip_rect);
3623 }
3624}
3625
3626
3627/* Draw foreground of image glyph string S. */
3628
3629static void
3630x_draw_image_foreground (s)
3631 struct glyph_string *s;
3632{
3633 int x;
95af8492 3634 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
3635
3636 /* If first glyph of S has a left box line, start drawing it to the
3637 right of that line. */
3638 if (s->face->box != FACE_NO_BOX
3639 && s->first_glyph->left_box_line_p)
3640 x = s->x + s->face->box_line_width;
3641 else
3642 x = s->x;
3643
3644 /* If there is a margin around the image, adjust x- and y-position
3645 by that margin. */
3646 if (s->img->margin)
3647 {
3648 x += s->img->margin;
3649 y += s->img->margin;
3650 }
3651
3652 if (s->img->pixmap)
3653 {
3654 if (s->img->mask)
3655 {
3656 /* We can't set both a clip mask and use XSetClipRectangles
3657 because the latter also sets a clip mask. We also can't
3658 trust on the shape extension to be available
3659 (XShapeCombineRegion). So, compute the rectangle to draw
3660 manually. */
3661 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3662 | GCFunction);
3663 XGCValues xgcv;
3664 XRectangle clip_rect, image_rect, r;
3665
3666 xgcv.clip_mask = s->img->mask;
3667 xgcv.clip_x_origin = x;
3668 xgcv.clip_y_origin = y;
3669 xgcv.function = GXcopy;
3670 XChangeGC (s->display, s->gc, mask, &xgcv);
3671
3672 x_get_glyph_string_clip_rect (s, &clip_rect);
3673 image_rect.x = x;
3674 image_rect.y = y;
3675 image_rect.width = s->img->width;
3676 image_rect.height = s->img->height;
3677 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3678 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3679 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3680 }
3681 else
3682 {
3683 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3684 0, 0, s->img->width, s->img->height, x, y);
3685
3686 /* When the image has a mask, we can expect that at
3687 least part of a mouse highlight or a block cursor will
3688 be visible. If the image doesn't have a mask, make
3689 a block cursor visible by drawing a rectangle around
3690 the image. I believe it's looking better if we do
3691 nothing here for mouse-face. */
3692 if (s->hl == DRAW_CURSOR)
3693 XDrawRectangle (s->display, s->window, s->gc, x, y,
3694 s->img->width - 1, s->img->height - 1);
3695 }
3696 }
3697 else
3698 /* Draw a rectangle if image could not be loaded. */
3699 XDrawRectangle (s->display, s->window, s->gc, x, y,
3700 s->img->width - 1, s->img->height - 1);
3701}
3702
3703
3704/* Draw a relief around the image glyph string S. */
3705
3706static void
3707x_draw_image_relief (s)
3708 struct glyph_string *s;
3709{
3710 int x0, y0, x1, y1, thick, raised_p;
3711 XRectangle r;
3712 int x;
95af8492 3713 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
3714
3715 /* If first glyph of S has a left box line, start drawing it to the
3716 right of that line. */
3717 if (s->face->box != FACE_NO_BOX
3718 && s->first_glyph->left_box_line_p)
3719 x = s->x + s->face->box_line_width;
3720 else
3721 x = s->x;
3722
3723 /* If there is a margin around the image, adjust x- and y-position
3724 by that margin. */
3725 if (s->img->margin)
3726 {
3727 x += s->img->margin;
3728 y += s->img->margin;
3729 }
3730
3731 if (s->hl == DRAW_IMAGE_SUNKEN
3732 || s->hl == DRAW_IMAGE_RAISED)
3733 {
9ea173e8 3734 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
06a2c219
GM
3735 raised_p = s->hl == DRAW_IMAGE_RAISED;
3736 }
3737 else
3738 {
3739 thick = abs (s->img->relief);
3740 raised_p = s->img->relief > 0;
3741 }
3742
3743 x0 = x - thick;
3744 y0 = y - thick;
3745 x1 = x + s->img->width + thick - 1;
3746 y1 = y + s->img->height + thick - 1;
3747
3748 x_setup_relief_colors (s);
3749 x_get_glyph_string_clip_rect (s, &r);
3750 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3751}
3752
3753
3754/* Draw the foreground of image glyph string S to PIXMAP. */
3755
3756static void
3757x_draw_image_foreground_1 (s, pixmap)
3758 struct glyph_string *s;
3759 Pixmap pixmap;
3760{
3761 int x;
95af8492 3762 int y = s->ybase - s->y - image_ascent (s->img, s->face);
06a2c219
GM
3763
3764 /* If first glyph of S has a left box line, start drawing it to the
3765 right of that line. */
3766 if (s->face->box != FACE_NO_BOX
3767 && s->first_glyph->left_box_line_p)
3768 x = s->face->box_line_width;
3769 else
3770 x = 0;
3771
3772 /* If there is a margin around the image, adjust x- and y-position
3773 by that margin. */
3774 if (s->img->margin)
3775 {
3776 x += s->img->margin;
3777 y += s->img->margin;
3778 }
dc43ef94 3779
06a2c219
GM
3780 if (s->img->pixmap)
3781 {
3782 if (s->img->mask)
3783 {
3784 /* We can't set both a clip mask and use XSetClipRectangles
3785 because the latter also sets a clip mask. We also can't
3786 trust on the shape extension to be available
3787 (XShapeCombineRegion). So, compute the rectangle to draw
3788 manually. */
3789 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3790 | GCFunction);
3791 XGCValues xgcv;
3792
3793 xgcv.clip_mask = s->img->mask;
3794 xgcv.clip_x_origin = x;
3795 xgcv.clip_y_origin = y;
3796 xgcv.function = GXcopy;
3797 XChangeGC (s->display, s->gc, mask, &xgcv);
3798
3799 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3800 0, 0, s->img->width, s->img->height, x, y);
3801 XSetClipMask (s->display, s->gc, None);
3802 }
3803 else
3804 {
3805 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3806 0, 0, s->img->width, s->img->height, x, y);
3807
3808 /* When the image has a mask, we can expect that at
3809 least part of a mouse highlight or a block cursor will
3810 be visible. If the image doesn't have a mask, make
3811 a block cursor visible by drawing a rectangle around
3812 the image. I believe it's looking better if we do
3813 nothing here for mouse-face. */
3814 if (s->hl == DRAW_CURSOR)
3815 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3816 s->img->width - 1, s->img->height - 1);
3817 }
3818 }
3819 else
3820 /* Draw a rectangle if image could not be loaded. */
3821 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3822 s->img->width - 1, s->img->height - 1);
3823}
dc43ef94 3824
990ba854 3825
06a2c219
GM
3826/* Draw part of the background of glyph string S. X, Y, W, and H
3827 give the rectangle to draw. */
a9a5b0a5 3828
06a2c219
GM
3829static void
3830x_draw_glyph_string_bg_rect (s, x, y, w, h)
3831 struct glyph_string *s;
3832 int x, y, w, h;
3833{
3834 if (s->stippled_p)
3835 {
3836 /* Fill background with a stipple pattern. */
3837 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3838 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3839 XSetFillStyle (s->display, s->gc, FillSolid);
3840 }
3841 else
3842 x_clear_glyph_string_rect (s, x, y, w, h);
3843}
07e34cb0 3844
b5210ea7 3845
06a2c219 3846/* Draw image glyph string S.
dc43ef94 3847
06a2c219
GM
3848 s->y
3849 s->x +-------------------------
3850 | s->face->box
3851 |
3852 | +-------------------------
3853 | | s->img->margin
3854 | |
3855 | | +-------------------
3856 | | | the image
dc43ef94 3857
06a2c219 3858 */
dc43ef94 3859
06a2c219
GM
3860static void
3861x_draw_image_glyph_string (s)
3862 struct glyph_string *s;
3863{
3864 int x, y;
3865 int box_line_width = s->face->box_line_width;
3866 int margin = s->img->margin;
3867 int height;
3868 Pixmap pixmap = None;
3869
3870 height = s->height - 2 * box_line_width;
3871
3872 /* Fill background with face under the image. Do it only if row is
3873 taller than image or if image has a clip mask to reduce
3874 flickering. */
3875 s->stippled_p = s->face->stipple != 0;
3876 if (height > s->img->height
3877 || margin
3878 || s->img->mask
3879 || s->img->pixmap == 0
3880 || s->width != s->background_width)
3881 {
3882 if (box_line_width && s->first_glyph->left_box_line_p)
3883 x = s->x + box_line_width;
3884 else
3885 x = s->x;
3886
3887 y = s->y + box_line_width;
3888
3889 if (s->img->mask)
3890 {
3891 /* Create a pixmap as large as the glyph string Fill it with
3892 the background color. Copy the image to it, using its
3893 mask. Copy the temporary pixmap to the display. */
3894 Screen *screen = FRAME_X_SCREEN (s->f);
3895 int depth = DefaultDepthOfScreen (screen);
3896
3897 /* Create a pixmap as large as the glyph string. */
3898 pixmap = XCreatePixmap (s->display, s->window,
3899 s->background_width,
3900 s->height, depth);
3901
3902 /* Don't clip in the following because we're working on the
3903 pixmap. */
3904 XSetClipMask (s->display, s->gc, None);
3905
3906 /* Fill the pixmap with the background color/stipple. */
3907 if (s->stippled_p)
3908 {
3909 /* Fill background with a stipple pattern. */
3910 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3911 XFillRectangle (s->display, pixmap, s->gc,
3912 0, 0, s->background_width, s->height);
3913 XSetFillStyle (s->display, s->gc, FillSolid);
3914 }
3915 else
3916 {
3917 XGCValues xgcv;
3918 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3919 &xgcv);
3920 XSetForeground (s->display, s->gc, xgcv.background);
3921 XFillRectangle (s->display, pixmap, s->gc,
3922 0, 0, s->background_width, s->height);
3923 XSetForeground (s->display, s->gc, xgcv.foreground);
3924 }
3925 }
3926 else
3927 /* Implementation idea: Is it possible to construct a mask?
3928 We could look at the color at the margins of the image, and
3929 say that this color is probably the background color of the
3930 image. */
3931 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
3932
3933 s->background_filled_p = 1;
3934 }
dc43ef94 3935
06a2c219
GM
3936 /* Draw the foreground. */
3937 if (pixmap != None)
3938 {
3939 x_draw_image_foreground_1 (s, pixmap);
3940 x_set_glyph_string_clipping (s);
3941 XCopyArea (s->display, pixmap, s->window, s->gc,
3942 0, 0, s->background_width, s->height, s->x, s->y);
3943 XFreePixmap (s->display, pixmap);
3944 }
3945 else
3946 x_draw_image_foreground (s);
b5210ea7 3947
06a2c219
GM
3948 /* If we must draw a relief around the image, do it. */
3949 if (s->img->relief
3950 || s->hl == DRAW_IMAGE_RAISED
3951 || s->hl == DRAW_IMAGE_SUNKEN)
3952 x_draw_image_relief (s);
3953}
8c1a6a84 3954
990ba854 3955
06a2c219 3956/* Draw stretch glyph string S. */
dc43ef94 3957
06a2c219
GM
3958static void
3959x_draw_stretch_glyph_string (s)
3960 struct glyph_string *s;
3961{
3962 xassert (s->first_glyph->type == STRETCH_GLYPH);
3963 s->stippled_p = s->face->stipple != 0;
990ba854 3964
06a2c219
GM
3965 if (s->hl == DRAW_CURSOR
3966 && !x_stretch_cursor_p)
3967 {
3968 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
3969 as wide as the stretch glyph. */
3970 int width = min (CANON_X_UNIT (s->f), s->background_width);
990ba854 3971
06a2c219
GM
3972 /* Draw cursor. */
3973 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
0cdd0c9f 3974
06a2c219
GM
3975 /* Clear rest using the GC of the original non-cursor face. */
3976 if (width < s->background_width)
3977 {
3978 GC gc = s->face->gc;
3979 int x = s->x + width, y = s->y;
3980 int w = s->background_width - width, h = s->height;
3981 XRectangle r;
dc43ef94 3982
06a2c219
GM
3983 x_get_glyph_string_clip_rect (s, &r);
3984 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
97210f4e 3985
06a2c219
GM
3986 if (s->face->stipple)
3987 {
3988 /* Fill background with a stipple pattern. */
3989 XSetFillStyle (s->display, gc, FillOpaqueStippled);
3990 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3991 XSetFillStyle (s->display, gc, FillSolid);
3992 }
3993 else
3994 {
3995 XGCValues xgcv;
3996 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
3997 XSetForeground (s->display, gc, xgcv.background);
3998 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3999 XSetForeground (s->display, gc, xgcv.foreground);
4000 }
4001 }
4002 }
4003 else
4004 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4005 s->height);
4006
4007 s->background_filled_p = 1;
4008}
4009
4010
4011/* Draw glyph string S. */
4012
4013static void
4014x_draw_glyph_string (s)
4015 struct glyph_string *s;
4016{
4017 /* If S draws into the background of its successor, draw the
4018 background of the successor first so that S can draw into it.
4019 This makes S->next use XDrawString instead of XDrawImageString. */
66ac4b0e 4020 if (s->next && s->right_overhang && !s->for_overlaps_p)
06a2c219
GM
4021 {
4022 xassert (s->next->img == NULL);
4023 x_set_glyph_string_gc (s->next);
4024 x_set_glyph_string_clipping (s->next);
4025 x_draw_glyph_string_background (s->next, 1);
4026 }
97210f4e 4027
06a2c219
GM
4028 /* Set up S->gc, set clipping and draw S. */
4029 x_set_glyph_string_gc (s);
4030 x_set_glyph_string_clipping (s);
4031
4032 switch (s->first_glyph->type)
4033 {
4034 case IMAGE_GLYPH:
4035 x_draw_image_glyph_string (s);
4036 break;
4037
4038 case STRETCH_GLYPH:
4039 x_draw_stretch_glyph_string (s);
4040 break;
4041
4042 case CHAR_GLYPH:
66ac4b0e
GM
4043 if (s->for_overlaps_p)
4044 s->background_filled_p = 1;
4045 else
4046 x_draw_glyph_string_background (s, 0);
06a2c219
GM
4047 x_draw_glyph_string_foreground (s);
4048 break;
4049
b4192550
KH
4050 case COMPOSITE_GLYPH:
4051 if (s->for_overlaps_p || s->gidx > 0)
4052 s->background_filled_p = 1;
4053 else
4054 x_draw_glyph_string_background (s, 1);
4055 x_draw_composite_glyph_string_foreground (s);
4056 break;
4057
06a2c219
GM
4058 default:
4059 abort ();
4060 }
4061
66ac4b0e 4062 if (!s->for_overlaps_p)
06a2c219 4063 {
66ac4b0e
GM
4064 /* Draw underline. */
4065 if (s->face->underline_p)
4066 {
4067 unsigned long dy, h;
06a2c219 4068
66ac4b0e
GM
4069 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4070 h = 1;
4071 if (!XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &dy))
4072 dy = s->height - h;
06a2c219 4073
66ac4b0e
GM
4074 if (s->face->underline_defaulted_p)
4075 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4076 s->width, h);
4077 else
4078 {
4079 XGCValues xgcv;
4080 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4081 XSetForeground (s->display, s->gc, s->face->underline_color);
4082 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4083 s->width, h);
4084 XSetForeground (s->display, s->gc, xgcv.foreground);
4085 }
dc6f92b8 4086 }
07e34cb0 4087
66ac4b0e
GM
4088 /* Draw overline. */
4089 if (s->face->overline_p)
06a2c219 4090 {
66ac4b0e
GM
4091 unsigned long dy = 0, h = 1;
4092
4093 if (s->face->overline_color_defaulted_p)
4094 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4095 s->width, h);
4096 else
4097 {
4098 XGCValues xgcv;
4099 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4100 XSetForeground (s->display, s->gc, s->face->overline_color);
4101 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4102 s->width, h);
4103 XSetForeground (s->display, s->gc, xgcv.foreground);
4104 }
06a2c219 4105 }
06a2c219 4106
66ac4b0e
GM
4107 /* Draw strike-through. */
4108 if (s->face->strike_through_p)
06a2c219 4109 {
66ac4b0e
GM
4110 unsigned long h = 1;
4111 unsigned long dy = (s->height - h) / 2;
4112
4113 if (s->face->strike_through_color_defaulted_p)
4114 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4115 s->width, h);
4116 else
4117 {
4118 XGCValues xgcv;
4119 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4120 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4121 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4122 s->width, h);
4123 XSetForeground (s->display, s->gc, xgcv.foreground);
4124 }
06a2c219 4125 }
06a2c219 4126
66ac4b0e
GM
4127 /* Draw relief. */
4128 if (s->face->box != FACE_NO_BOX)
4129 x_draw_glyph_string_box (s);
4130 }
06a2c219
GM
4131
4132 /* Reset clipping. */
4133 XSetClipMask (s->display, s->gc, None);
dc6f92b8 4134}
07e34cb0 4135
06a2c219 4136
b4192550
KH
4137static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4138 struct face **, int));
06a2c219 4139
06a2c219 4140
b4192550
KH
4141/* Load glyph string S with a composition components specified by S->cmp.
4142 FACES is an array of faces for all components of this composition.
4143 S->gidx is the index of the first component for S.
4144 OVERLAPS_P non-zero means S should draw the foreground only, and
4145 use its lines physical height for clipping.
06a2c219 4146
b4192550 4147 Value is the index of a component not in S. */
07e34cb0 4148
b4192550
KH
4149static int
4150x_fill_composite_glyph_string (s, faces, overlaps_p)
06a2c219 4151 struct glyph_string *s;
b4192550 4152 struct face **faces;
66ac4b0e 4153 int overlaps_p;
07e34cb0 4154{
b4192550 4155 int i;
06a2c219 4156
b4192550 4157 xassert (s);
06a2c219 4158
b4192550 4159 s->for_overlaps_p = overlaps_p;
06a2c219 4160
b4192550
KH
4161 s->face = faces[s->gidx];
4162 s->font = s->face->font;
4163 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
06a2c219 4164
b4192550
KH
4165 /* For all glyphs of this composition, starting at the offset
4166 S->gidx, until we reach the end of the definition or encounter a
4167 glyph that requires the different face, add it to S. */
4168 ++s->nchars;
4169 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4170 ++s->nchars;
06a2c219 4171
b4192550
KH
4172 /* All glyph strings for the same composition has the same width,
4173 i.e. the width set for the first component of the composition. */
06a2c219 4174
06a2c219
GM
4175 s->width = s->first_glyph->pixel_width;
4176
4177 /* If the specified font could not be loaded, use the frame's
4178 default font, but record the fact that we couldn't load it in
4179 the glyph string so that we can draw rectangles for the
4180 characters of the glyph string. */
4181 if (s->font == NULL)
4182 {
4183 s->font_not_found_p = 1;
4184 s->font = FRAME_FONT (s->f);
4185 }
4186
4187 /* Adjust base line for subscript/superscript text. */
4188 s->ybase += s->first_glyph->voffset;
4189
4190 xassert (s->face && s->face->gc);
4191
4192 /* This glyph string must always be drawn with 16-bit functions. */
4193 s->two_byte_p = 1;
b4192550
KH
4194
4195 return s->gidx + s->nchars;
06a2c219
GM
4196}
4197
4198
b4192550 4199/* Load glyph string S with a sequence characters.
06a2c219 4200 FACE_ID is the face id of the string. START is the index of the
66ac4b0e
GM
4201 first glyph to consider, END is the index of the last + 1.
4202 OVERLAPS_P non-zero means S should draw the foreground only, and
4203 use its lines physical height for clipping.
4204
4205 Value is the index of the first glyph not in S. */
06a2c219
GM
4206
4207static int
66ac4b0e 4208x_fill_glyph_string (s, face_id, start, end, overlaps_p)
06a2c219
GM
4209 struct glyph_string *s;
4210 int face_id;
66ac4b0e 4211 int start, end, overlaps_p;
06a2c219
GM
4212{
4213 struct glyph *glyph, *last;
4214 int voffset;
ee569018 4215 int glyph_not_available_p;
06a2c219 4216
06a2c219
GM
4217 xassert (s->f == XFRAME (s->w->frame));
4218 xassert (s->nchars == 0);
4219 xassert (start >= 0 && end > start);
4220
66ac4b0e 4221 s->for_overlaps_p = overlaps_p,
06a2c219
GM
4222 glyph = s->row->glyphs[s->area] + start;
4223 last = s->row->glyphs[s->area] + end;
4224 voffset = glyph->voffset;
4225
ee569018
KH
4226 glyph_not_available_p = glyph->glyph_not_available_p;
4227
06a2c219
GM
4228 while (glyph < last
4229 && glyph->type == CHAR_GLYPH
4230 && glyph->voffset == voffset
ee569018
KH
4231 /* Same face id implies same font, nowadays. */
4232 && glyph->face_id == face_id
4233 && glyph->glyph_not_available_p == glyph_not_available_p)
06a2c219 4234 {
ee569018
KH
4235 int two_byte_p;
4236
06a2c219 4237 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
ee569018
KH
4238 s->char2b + s->nchars,
4239 &two_byte_p);
4240 s->two_byte_p = two_byte_p;
06a2c219
GM
4241 ++s->nchars;
4242 xassert (s->nchars <= end - start);
4243 s->width += glyph->pixel_width;
4244 ++glyph;
4245 }
4246
4247 s->font = s->face->font;
4248 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4249
4250 /* If the specified font could not be loaded, use the frame's font,
4251 but record the fact that we couldn't load it in
4252 S->font_not_found_p so that we can draw rectangles for the
4253 characters of the glyph string. */
ee569018 4254 if (s->font == NULL || glyph_not_available_p)
06a2c219
GM
4255 {
4256 s->font_not_found_p = 1;
4257 s->font = FRAME_FONT (s->f);
4258 }
4259
4260 /* Adjust base line for subscript/superscript text. */
4261 s->ybase += voffset;
66ac4b0e 4262
06a2c219
GM
4263 xassert (s->face && s->face->gc);
4264 return glyph - s->row->glyphs[s->area];
07e34cb0 4265}
dc6f92b8 4266
06a2c219
GM
4267
4268/* Fill glyph string S from image glyph S->first_glyph. */
dc6f92b8 4269
dfcf069d 4270static void
06a2c219
GM
4271x_fill_image_glyph_string (s)
4272 struct glyph_string *s;
4273{
4274 xassert (s->first_glyph->type == IMAGE_GLYPH);
43d120d8 4275 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
06a2c219 4276 xassert (s->img);
43d120d8 4277 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
06a2c219
GM
4278 s->font = s->face->font;
4279 s->width = s->first_glyph->pixel_width;
4280
4281 /* Adjust base line for subscript/superscript text. */
4282 s->ybase += s->first_glyph->voffset;
4283}
4284
4285
4286/* Fill glyph string S from stretch glyph S->first_glyph. */
4287
4288static void
4289x_fill_stretch_glyph_string (s)
4290 struct glyph_string *s;
4291{
4292 xassert (s->first_glyph->type == STRETCH_GLYPH);
43d120d8 4293 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
06a2c219
GM
4294 s->font = s->face->font;
4295 s->width = s->first_glyph->pixel_width;
4296
4297 /* Adjust base line for subscript/superscript text. */
4298 s->ybase += s->first_glyph->voffset;
4299}
4300
4301
4302/* Initialize glyph string S. CHAR2B is a suitably allocated vector
4303 of XChar2b structures for S; it can't be allocated in
4304 x_init_glyph_string because it must be allocated via `alloca'. W
4305 is the window on which S is drawn. ROW and AREA are the glyph row
4306 and area within the row from which S is constructed. START is the
4307 index of the first glyph structure covered by S. HL is a
4308 face-override for drawing S. */
4309
4310static void
4311x_init_glyph_string (s, char2b, w, row, area, start, hl)
4312 struct glyph_string *s;
4313 XChar2b *char2b;
4314 struct window *w;
4315 struct glyph_row *row;
4316 enum glyph_row_area area;
4317 int start;
4318 enum draw_glyphs_face hl;
4319{
4320 bzero (s, sizeof *s);
4321 s->w = w;
4322 s->f = XFRAME (w->frame);
4323 s->display = FRAME_X_DISPLAY (s->f);
4324 s->window = FRAME_X_WINDOW (s->f);
4325 s->char2b = char2b;
4326 s->hl = hl;
4327 s->row = row;
4328 s->area = area;
4329 s->first_glyph = row->glyphs[area] + start;
4330 s->height = row->height;
4331 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4332
9ea173e8
GM
4333 /* Display the internal border below the tool-bar window. */
4334 if (s->w == XWINDOW (s->f->tool_bar_window))
06a2c219
GM
4335 s->y -= s->f->output_data.x->internal_border_width;
4336
4337 s->ybase = s->y + row->ascent;
4338}
4339
4340
4341/* Set background width of glyph string S. START is the index of the
4342 first glyph following S. LAST_X is the right-most x-position + 1
4343 in the drawing area. */
4344
4345static INLINE void
4346x_set_glyph_string_background_width (s, start, last_x)
4347 struct glyph_string *s;
4348 int start;
4349 int last_x;
4350{
4351 /* If the face of this glyph string has to be drawn to the end of
4352 the drawing area, set S->extends_to_end_of_line_p. */
4353 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4354
4355 if (start == s->row->used[s->area]
4356 && s->hl == DRAW_NORMAL_TEXT
4357 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4358 || s->face->background != default_face->background
4359 || s->face->stipple != default_face->stipple))
4360 s->extends_to_end_of_line_p = 1;
4361
4362 /* If S extends its face to the end of the line, set its
4363 background_width to the distance to the right edge of the drawing
4364 area. */
4365 if (s->extends_to_end_of_line_p)
1da3fd71 4366 s->background_width = last_x - s->x + 1;
06a2c219
GM
4367 else
4368 s->background_width = s->width;
4369}
4370
4371
4372/* Add a glyph string for a stretch glyph to the list of strings
4373 between HEAD and TAIL. START is the index of the stretch glyph in
4374 row area AREA of glyph row ROW. END is the index of the last glyph
4375 in that glyph row area. X is the current output position assigned
4376 to the new glyph string constructed. HL overrides that face of the
4377 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4378 is the right-most x-position of the drawing area. */
4379
8abee2e1
DL
4380/* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4381 and below -- keep them on one line. */
4382#define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4383 do \
4384 { \
4385 s = (struct glyph_string *) alloca (sizeof *s); \
4386 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4387 x_fill_stretch_glyph_string (s); \
4388 x_append_glyph_string (&HEAD, &TAIL, s); \
4389 ++START; \
4390 s->x = (X); \
4391 } \
4392 while (0)
4393
4394
4395/* Add a glyph string for an image glyph to the list of strings
4396 between HEAD and TAIL. START is the index of the image glyph in
4397 row area AREA of glyph row ROW. END is the index of the last glyph
4398 in that glyph row area. X is the current output position assigned
4399 to the new glyph string constructed. HL overrides that face of the
4400 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4401 is the right-most x-position of the drawing area. */
4402
8abee2e1 4403#define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
06a2c219
GM
4404 do \
4405 { \
4406 s = (struct glyph_string *) alloca (sizeof *s); \
4407 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4408 x_fill_image_glyph_string (s); \
4409 x_append_glyph_string (&HEAD, &TAIL, s); \
4410 ++START; \
4411 s->x = (X); \
4412 } \
4413 while (0)
4414
4415
4416/* Add a glyph string for a sequence of character glyphs to the list
4417 of strings between HEAD and TAIL. START is the index of the first
4418 glyph in row area AREA of glyph row ROW that is part of the new
4419 glyph string. END is the index of the last glyph in that glyph row
4420 area. X is the current output position assigned to the new glyph
4421 string constructed. HL overrides that face of the glyph; e.g. it
4422 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4423 right-most x-position of the drawing area. */
4424
8abee2e1 4425#define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4426 do \
4427 { \
3e71d8f2 4428 int c, face_id; \
06a2c219
GM
4429 XChar2b *char2b; \
4430 \
43d120d8 4431 c = (ROW)->glyphs[AREA][START].u.ch; \
43d120d8 4432 face_id = (ROW)->glyphs[AREA][START].face_id; \
06a2c219 4433 \
b4192550
KH
4434 s = (struct glyph_string *) alloca (sizeof *s); \
4435 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4436 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4437 x_append_glyph_string (&HEAD, &TAIL, s); \
b4192550
KH
4438 s->x = (X); \
4439 START = x_fill_glyph_string (s, face_id, START, END, \
66ac4b0e 4440 OVERLAPS_P); \
06a2c219
GM
4441 } \
4442 while (0)
4443
4444
b4192550
KH
4445/* Add a glyph string for a composite sequence to the list of strings
4446 between HEAD and TAIL. START is the index of the first glyph in
4447 row area AREA of glyph row ROW that is part of the new glyph
4448 string. END is the index of the last glyph in that glyph row area.
4449 X is the current output position assigned to the new glyph string
4450 constructed. HL overrides that face of the glyph; e.g. it is
4451 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4452 x-position of the drawing area. */
4453
6c27ec25 4454#define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
b4192550 4455 do { \
43d120d8
KH
4456 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4457 int face_id = (ROW)->glyphs[AREA][START].face_id; \
ee569018 4458 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
b4192550
KH
4459 struct composition *cmp = composition_table[cmp_id]; \
4460 int glyph_len = cmp->glyph_len; \
4461 XChar2b *char2b; \
4462 struct face **faces; \
4463 struct glyph_string *first_s = NULL; \
4464 int n; \
4465 \
ee569018 4466 base_face = base_face->ascii_face; \
b4192550
KH
4467 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4468 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4469 /* At first, fill in `char2b' and `faces'. */ \
4470 for (n = 0; n < glyph_len; n++) \
4471 { \
43d120d8 4472 int c = COMPOSITION_GLYPH (cmp, n); \
ee569018
KH
4473 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4474 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4475 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4476 this_face_id, char2b + n, 1); \
b4192550
KH
4477 } \
4478 \
4479 /* Make glyph_strings for each glyph sequence that is drawable by \
4480 the same face, and append them to HEAD/TAIL. */ \
4481 for (n = 0; n < cmp->glyph_len;) \
4482 { \
4483 s = (struct glyph_string *) alloca (sizeof *s); \
4484 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4485 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4486 s->cmp = cmp; \
4487 s->gidx = n; \
b4192550
KH
4488 s->x = (X); \
4489 \
4490 if (n == 0) \
4491 first_s = s; \
4492 \
4493 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4494 } \
4495 \
4496 ++START; \
4497 s = first_s; \
4498 } while (0)
4499
4500
06a2c219
GM
4501/* Build a list of glyph strings between HEAD and TAIL for the glyphs
4502 of AREA of glyph row ROW on window W between indices START and END.
4503 HL overrides the face for drawing glyph strings, e.g. it is
4504 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4505 x-positions of the drawing area.
4506
4507 This is an ugly monster macro construct because we must use alloca
4508 to allocate glyph strings (because x_draw_glyphs can be called
4509 asynchronously). */
4510
8abee2e1 4511#define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
06a2c219
GM
4512 do \
4513 { \
4514 HEAD = TAIL = NULL; \
4515 while (START < END) \
4516 { \
4517 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4518 switch (first_glyph->type) \
4519 { \
4520 case CHAR_GLYPH: \
4521 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
66ac4b0e
GM
4522 TAIL, HL, X, LAST_X, \
4523 OVERLAPS_P); \
06a2c219
GM
4524 break; \
4525 \
b4192550
KH
4526 case COMPOSITE_GLYPH: \
4527 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4528 HEAD, TAIL, HL, X, LAST_X,\
4529 OVERLAPS_P); \
4530 break; \
4531 \
06a2c219
GM
4532 case STRETCH_GLYPH: \
4533 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4534 HEAD, TAIL, HL, X, LAST_X); \
4535 break; \
4536 \
4537 case IMAGE_GLYPH: \
4538 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4539 TAIL, HL, X, LAST_X); \
4540 break; \
4541 \
4542 default: \
4543 abort (); \
4544 } \
4545 \
4546 x_set_glyph_string_background_width (s, START, LAST_X); \
4547 (X) += s->width; \
4548 } \
4549 } \
4550 while (0)
4551
4552
4553/* Draw glyphs between START and END in AREA of ROW on window W,
4554 starting at x-position X. X is relative to AREA in W. HL is a
4555 face-override with the following meaning:
4556
4557 DRAW_NORMAL_TEXT draw normally
4558 DRAW_CURSOR draw in cursor face
4559 DRAW_MOUSE_FACE draw in mouse face.
4560 DRAW_INVERSE_VIDEO draw in mode line face
4561 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4562 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4563
4564 If REAL_START is non-null, return in *REAL_START the real starting
4565 position for display. This can be different from START in case
4566 overlapping glyphs must be displayed. If REAL_END is non-null,
4567 return in *REAL_END the real end position for display. This can be
4568 different from END in case overlapping glyphs must be displayed.
4569
66ac4b0e
GM
4570 If OVERLAPS_P is non-zero, draw only the foreground of characters
4571 and clip to the physical height of ROW.
4572
06a2c219
GM
4573 Value is the x-position reached, relative to AREA of W. */
4574
4575static int
66ac4b0e
GM
4576x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4577 overlaps_p)
06a2c219
GM
4578 struct window *w;
4579 int x;
4580 struct glyph_row *row;
4581 enum glyph_row_area area;
4582 int start, end;
4583 enum draw_glyphs_face hl;
4584 int *real_start, *real_end;
66ac4b0e 4585 int overlaps_p;
dc6f92b8 4586{
06a2c219
GM
4587 struct glyph_string *head, *tail;
4588 struct glyph_string *s;
4589 int last_x, area_width;
4590 int x_reached;
4591 int i, j;
4592
4593 /* Let's rather be paranoid than getting a SEGV. */
4594 start = max (0, start);
4595 end = min (end, row->used[area]);
4596 if (real_start)
4597 *real_start = start;
4598 if (real_end)
4599 *real_end = end;
4600
4601 /* Translate X to frame coordinates. Set last_x to the right
4602 end of the drawing area. */
4603 if (row->full_width_p)
4604 {
4605 /* X is relative to the left edge of W, without scroll bars
4606 or flag areas. */
4607 struct frame *f = XFRAME (w->frame);
110859fc 4608 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
06a2c219 4609 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
dc6f92b8 4610
06a2c219
GM
4611 x += window_left_x;
4612 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4613 last_x = window_left_x + area_width;
4614
4615 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4616 {
110859fc 4617 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
06a2c219
GM
4618 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4619 last_x += width;
4620 else
4621 x -= width;
4622 }
dc6f92b8 4623
b9432a85
GM
4624 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4625 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
06a2c219
GM
4626 }
4627 else
dc6f92b8 4628 {
06a2c219
GM
4629 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4630 area_width = window_box_width (w, area);
4631 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
dc6f92b8
JB
4632 }
4633
06a2c219
GM
4634 /* Build a doubly-linked list of glyph_string structures between
4635 head and tail from what we have to draw. Note that the macro
4636 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4637 the reason we use a separate variable `i'. */
4638 i = start;
66ac4b0e
GM
4639 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4640 overlaps_p);
06a2c219
GM
4641 if (tail)
4642 x_reached = tail->x + tail->background_width;
4643 else
4644 x_reached = x;
90e65f07 4645
06a2c219
GM
4646 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4647 the row, redraw some glyphs in front or following the glyph
4648 strings built above. */
66ac4b0e 4649 if (!overlaps_p && row->contains_overlapping_glyphs_p)
06a2c219
GM
4650 {
4651 int dummy_x = 0;
4652 struct glyph_string *h, *t;
4653
4654 /* Compute overhangs for all glyph strings. */
4655 for (s = head; s; s = s->next)
4656 x_compute_glyph_string_overhangs (s);
4657
4658 /* Prepend glyph strings for glyphs in front of the first glyph
4659 string that are overwritten because of the first glyph
4660 string's left overhang. The background of all strings
4661 prepended must be drawn because the first glyph string
4662 draws over it. */
4663 i = x_left_overwritten (head);
4664 if (i >= 0)
4665 {
4666 j = i;
4667 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
66ac4b0e
GM
4668 DRAW_NORMAL_TEXT, dummy_x, last_x,
4669 overlaps_p);
06a2c219
GM
4670 start = i;
4671 if (real_start)
4672 *real_start = start;
4673 x_compute_overhangs_and_x (t, head->x, 1);
4674 x_prepend_glyph_string_lists (&head, &tail, h, t);
4675 }
58769bee 4676
06a2c219
GM
4677 /* Prepend glyph strings for glyphs in front of the first glyph
4678 string that overwrite that glyph string because of their
4679 right overhang. For these strings, only the foreground must
4680 be drawn, because it draws over the glyph string at `head'.
4681 The background must not be drawn because this would overwrite
4682 right overhangs of preceding glyphs for which no glyph
4683 strings exist. */
4684 i = x_left_overwriting (head);
4685 if (i >= 0)
4686 {
4687 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
66ac4b0e
GM
4688 DRAW_NORMAL_TEXT, dummy_x, last_x,
4689 overlaps_p);
06a2c219
GM
4690 for (s = h; s; s = s->next)
4691 s->background_filled_p = 1;
4692 if (real_start)
4693 *real_start = i;
4694 x_compute_overhangs_and_x (t, head->x, 1);
4695 x_prepend_glyph_string_lists (&head, &tail, h, t);
4696 }
dbcb258a 4697
06a2c219
GM
4698 /* Append glyphs strings for glyphs following the last glyph
4699 string tail that are overwritten by tail. The background of
4700 these strings has to be drawn because tail's foreground draws
4701 over it. */
4702 i = x_right_overwritten (tail);
4703 if (i >= 0)
4704 {
4705 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4706 DRAW_NORMAL_TEXT, x, last_x,
4707 overlaps_p);
06a2c219
GM
4708 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4709 x_append_glyph_string_lists (&head, &tail, h, t);
4710 if (real_end)
4711 *real_end = i;
4712 }
dc6f92b8 4713
06a2c219
GM
4714 /* Append glyph strings for glyphs following the last glyph
4715 string tail that overwrite tail. The foreground of such
4716 glyphs has to be drawn because it writes into the background
4717 of tail. The background must not be drawn because it could
4718 paint over the foreground of following glyphs. */
4719 i = x_right_overwriting (tail);
4720 if (i >= 0)
4721 {
4722 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
66ac4b0e
GM
4723 DRAW_NORMAL_TEXT, x, last_x,
4724 overlaps_p);
06a2c219
GM
4725 for (s = h; s; s = s->next)
4726 s->background_filled_p = 1;
4727 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4728 x_append_glyph_string_lists (&head, &tail, h, t);
4729 if (real_end)
4730 *real_end = i;
4731 }
4732 }
58769bee 4733
06a2c219
GM
4734 /* Draw all strings. */
4735 for (s = head; s; s = s->next)
4736 x_draw_glyph_string (s);
dc6f92b8 4737
06a2c219
GM
4738 /* Value is the x-position up to which drawn, relative to AREA of W.
4739 This doesn't include parts drawn because of overhangs. */
4740 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4741 if (!row->full_width_p)
4742 {
4743 if (area > LEFT_MARGIN_AREA)
4744 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4745 if (area > TEXT_AREA)
4746 x_reached -= window_box_width (w, TEXT_AREA);
4747 }
4748 return x_reached;
4749}
dc6f92b8 4750
dc6f92b8 4751
66ac4b0e
GM
4752/* Fix the display of area AREA of overlapping row ROW in window W. */
4753
4754static void
4755x_fix_overlapping_area (w, row, area)
4756 struct window *w;
4757 struct glyph_row *row;
4758 enum glyph_row_area area;
4759{
4760 int i, x;
4761
4762 BLOCK_INPUT;
4763
4764 if (area == LEFT_MARGIN_AREA)
4765 x = 0;
4766 else if (area == TEXT_AREA)
4767 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
4768 else
4769 x = (window_box_width (w, LEFT_MARGIN_AREA)
4770 + window_box_width (w, TEXT_AREA));
4771
4772 for (i = 0; i < row->used[area];)
4773 {
4774 if (row->glyphs[area][i].overlaps_vertically_p)
4775 {
4776 int start = i, start_x = x;
4777
4778 do
4779 {
4780 x += row->glyphs[area][i].pixel_width;
4781 ++i;
4782 }
4783 while (i < row->used[area]
4784 && row->glyphs[area][i].overlaps_vertically_p);
4785
4786 x_draw_glyphs (w, start_x, row, area, start, i,
4787 (row->inverse_p
4788 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4789 NULL, NULL, 1);
4790 }
4791 else
4792 {
4793 x += row->glyphs[area][i].pixel_width;
4794 ++i;
4795 }
4796 }
4797
4798 UNBLOCK_INPUT;
4799}
4800
4801
06a2c219
GM
4802/* Output LEN glyphs starting at START at the nominal cursor position.
4803 Advance the nominal cursor over the text. The global variable
4804 updated_window contains the window being updated, updated_row is
4805 the glyph row being updated, and updated_area is the area of that
4806 row being updated. */
dc6f92b8 4807
06a2c219
GM
4808static void
4809x_write_glyphs (start, len)
4810 struct glyph *start;
4811 int len;
4812{
4813 int x, hpos, real_start, real_end;
d9cdbb3d 4814
06a2c219 4815 xassert (updated_window && updated_row);
dc6f92b8 4816 BLOCK_INPUT;
06a2c219
GM
4817
4818 /* Write glyphs. */
dc6f92b8 4819
06a2c219
GM
4820 hpos = start - updated_row->glyphs[updated_area];
4821 x = x_draw_glyphs (updated_window, output_cursor.x,
4822 updated_row, updated_area,
4823 hpos, hpos + len,
4824 (updated_row->inverse_p
4825 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
66ac4b0e 4826 &real_start, &real_end, 0);
b30ec466 4827
06a2c219
GM
4828 /* If we drew over the cursor, note that it is not visible any more. */
4829 note_overwritten_text_cursor (updated_window, real_start,
4830 real_end - real_start);
dc6f92b8
JB
4831
4832 UNBLOCK_INPUT;
06a2c219
GM
4833
4834 /* Advance the output cursor. */
4835 output_cursor.hpos += len;
4836 output_cursor.x = x;
dc6f92b8
JB
4837}
4838
0cdd0c9f 4839
06a2c219 4840/* Insert LEN glyphs from START at the nominal cursor position. */
0cdd0c9f 4841
06a2c219
GM
4842static void
4843x_insert_glyphs (start, len)
4844 struct glyph *start;
4845 register int len;
4846{
4847 struct frame *f;
4848 struct window *w;
4849 int line_height, shift_by_width, shifted_region_width;
4850 struct glyph_row *row;
4851 struct glyph *glyph;
4852 int frame_x, frame_y, hpos, real_start, real_end;
58769bee 4853
06a2c219 4854 xassert (updated_window && updated_row);
0cdd0c9f 4855 BLOCK_INPUT;
06a2c219
GM
4856 w = updated_window;
4857 f = XFRAME (WINDOW_FRAME (w));
4858
4859 /* Get the height of the line we are in. */
4860 row = updated_row;
4861 line_height = row->height;
4862
4863 /* Get the width of the glyphs to insert. */
4864 shift_by_width = 0;
4865 for (glyph = start; glyph < start + len; ++glyph)
4866 shift_by_width += glyph->pixel_width;
4867
4868 /* Get the width of the region to shift right. */
4869 shifted_region_width = (window_box_width (w, updated_area)
4870 - output_cursor.x
4871 - shift_by_width);
4872
4873 /* Shift right. */
4874 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
4875 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
4876 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
4877 f->output_data.x->normal_gc,
4878 frame_x, frame_y,
4879 shifted_region_width, line_height,
4880 frame_x + shift_by_width, frame_y);
4881
4882 /* Write the glyphs. */
4883 hpos = start - row->glyphs[updated_area];
4884 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
66ac4b0e 4885 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
06a2c219
GM
4886 note_overwritten_text_cursor (w, real_start, real_end - real_start);
4887
4888 /* Advance the output cursor. */
4889 output_cursor.hpos += len;
4890 output_cursor.x += shift_by_width;
0cdd0c9f
RS
4891 UNBLOCK_INPUT;
4892}
0cdd0c9f 4893
0cdd0c9f 4894
06a2c219
GM
4895/* Delete N glyphs at the nominal cursor position. Not implemented
4896 for X frames. */
c83febd7
RS
4897
4898static void
06a2c219
GM
4899x_delete_glyphs (n)
4900 register int n;
c83febd7 4901{
06a2c219 4902 abort ();
c83febd7
RS
4903}
4904
0cdd0c9f 4905
06a2c219
GM
4906/* Erase the current text line from the nominal cursor position
4907 (inclusive) to pixel column TO_X (exclusive). The idea is that
4908 everything from TO_X onward is already erased.
4909
4910 TO_X is a pixel position relative to updated_area of
4911 updated_window. TO_X == -1 means clear to the end of this area. */
dc6f92b8 4912
06a2c219
GM
4913static void
4914x_clear_end_of_line (to_x)
4915 int to_x;
4916{
4917 struct frame *f;
4918 struct window *w = updated_window;
4919 int max_x, min_y, max_y;
4920 int from_x, from_y, to_y;
4921
4922 xassert (updated_window && updated_row);
4923 f = XFRAME (w->frame);
4924
4925 if (updated_row->full_width_p)
4926 {
4927 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
4928 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
4929 && !w->pseudo_window_p)
4930 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
0cdd0c9f 4931 }
06a2c219
GM
4932 else
4933 max_x = window_box_width (w, updated_area);
4934 max_y = window_text_bottom_y (w);
dc6f92b8 4935
06a2c219
GM
4936 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
4937 of window. For TO_X > 0, truncate to end of drawing area. */
4938 if (to_x == 0)
4939 return;
4940 else if (to_x < 0)
4941 to_x = max_x;
4942 else
4943 to_x = min (to_x, max_x);
dbc4e1c1 4944
06a2c219
GM
4945 to_y = min (max_y, output_cursor.y + updated_row->height);
4946
4947 /* Notice if the cursor will be cleared by this operation. */
4948 if (!updated_row->full_width_p)
4949 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
dbc4e1c1 4950
06a2c219
GM
4951 from_x = output_cursor.x;
4952
4953 /* Translate to frame coordinates. */
4954 if (updated_row->full_width_p)
4955 {
4956 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
4957 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
4958 }
0cdd0c9f
RS
4959 else
4960 {
06a2c219
GM
4961 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
4962 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
4963 }
4964
045dee35 4965 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
06a2c219
GM
4966 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
4967 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
4968
4969 /* Prevent inadvertently clearing to end of the X window. */
4970 if (to_x > from_x && to_y > from_y)
4971 {
4972 BLOCK_INPUT;
4973 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4974 from_x, from_y, to_x - from_x, to_y - from_y,
4975 False);
4976 UNBLOCK_INPUT;
0cdd0c9f 4977 }
0cdd0c9f 4978}
dbc4e1c1 4979
0cdd0c9f 4980
06a2c219 4981/* Clear entire frame. If updating_frame is non-null, clear that
b86bd3dd 4982 frame. Otherwise clear the selected frame. */
06a2c219
GM
4983
4984static void
4985x_clear_frame ()
0cdd0c9f 4986{
06a2c219 4987 struct frame *f;
0cdd0c9f 4988
06a2c219
GM
4989 if (updating_frame)
4990 f = updating_frame;
0cdd0c9f 4991 else
b86bd3dd 4992 f = SELECTED_FRAME ();
58769bee 4993
06a2c219
GM
4994 /* Clearing the frame will erase any cursor, so mark them all as no
4995 longer visible. */
4996 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
4997 output_cursor.hpos = output_cursor.vpos = 0;
4998 output_cursor.x = -1;
4999
5000 /* We don't set the output cursor here because there will always
5001 follow an explicit cursor_to. */
5002 BLOCK_INPUT;
5003 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5004
5005 /* We have to clear the scroll bars, too. If we have changed
5006 colors or something like that, then they should be notified. */
5007 x_scroll_bar_clear (f);
0cdd0c9f 5008
06a2c219
GM
5009 XFlush (FRAME_X_DISPLAY (f));
5010 UNBLOCK_INPUT;
dc6f92b8 5011}
06a2c219
GM
5012
5013
dc6f92b8 5014\f
dbc4e1c1
JB
5015/* Invert the middle quarter of the frame for .15 sec. */
5016
06a2c219
GM
5017/* We use the select system call to do the waiting, so we have to make
5018 sure it's available. If it isn't, we just won't do visual bells. */
5019
dbc4e1c1
JB
5020#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5021
06a2c219
GM
5022
5023/* Subtract the `struct timeval' values X and Y, storing the result in
5024 *RESULT. Return 1 if the difference is negative, otherwise 0. */
dbc4e1c1
JB
5025
5026static int
5027timeval_subtract (result, x, y)
5028 struct timeval *result, x, y;
5029{
06a2c219
GM
5030 /* Perform the carry for the later subtraction by updating y. This
5031 is safer because on some systems the tv_sec member is unsigned. */
dbc4e1c1
JB
5032 if (x.tv_usec < y.tv_usec)
5033 {
5034 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5035 y.tv_usec -= 1000000 * nsec;
5036 y.tv_sec += nsec;
5037 }
06a2c219 5038
dbc4e1c1
JB
5039 if (x.tv_usec - y.tv_usec > 1000000)
5040 {
5041 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5042 y.tv_usec += 1000000 * nsec;
5043 y.tv_sec -= nsec;
5044 }
5045
06a2c219
GM
5046 /* Compute the time remaining to wait. tv_usec is certainly
5047 positive. */
dbc4e1c1
JB
5048 result->tv_sec = x.tv_sec - y.tv_sec;
5049 result->tv_usec = x.tv_usec - y.tv_usec;
5050
06a2c219
GM
5051 /* Return indication of whether the result should be considered
5052 negative. */
dbc4e1c1
JB
5053 return x.tv_sec < y.tv_sec;
5054}
dc6f92b8 5055
dfcf069d 5056void
f676886a
JB
5057XTflash (f)
5058 struct frame *f;
dc6f92b8 5059{
dbc4e1c1 5060 BLOCK_INPUT;
dc6f92b8 5061
dbc4e1c1
JB
5062 {
5063 GC gc;
dc6f92b8 5064
06a2c219
GM
5065 /* Create a GC that will use the GXxor function to flip foreground
5066 pixels into background pixels. */
dbc4e1c1
JB
5067 {
5068 XGCValues values;
dc6f92b8 5069
dbc4e1c1 5070 values.function = GXxor;
7556890b
RS
5071 values.foreground = (f->output_data.x->foreground_pixel
5072 ^ f->output_data.x->background_pixel);
58769bee 5073
334208b7 5074 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dbc4e1c1
JB
5075 GCFunction | GCForeground, &values);
5076 }
dc6f92b8 5077
dbc4e1c1 5078 {
e84e14c3
RS
5079 /* Get the height not including a menu bar widget. */
5080 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5081 /* Height of each line to flash. */
5082 int flash_height = FRAME_LINE_HEIGHT (f);
5083 /* These will be the left and right margins of the rectangles. */
5084 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5085 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5086
5087 int width;
5088
5089 /* Don't flash the area between a scroll bar and the frame
5090 edge it is next to. */
5091 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5092 {
5093 case vertical_scroll_bar_left:
5094 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5095 break;
5096
5097 case vertical_scroll_bar_right:
5098 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5099 break;
06a2c219
GM
5100
5101 default:
5102 break;
e84e14c3
RS
5103 }
5104
5105 width = flash_right - flash_left;
5106
5107 /* If window is tall, flash top and bottom line. */
5108 if (height > 3 * FRAME_LINE_HEIGHT (f))
5109 {
5110 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5111 flash_left,
5112 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5113 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5114 width, flash_height);
5115 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5116 flash_left,
5117 (height - flash_height
5118 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5119 width, flash_height);
5120 }
5121 else
5122 /* If it is short, flash it all. */
5123 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5124 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5125 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
dc6f92b8 5126
06a2c219 5127 x_flush (f);
dc6f92b8 5128
dbc4e1c1 5129 {
06a2c219 5130 struct timeval wakeup;
dc6f92b8 5131
66c30ea1 5132 EMACS_GET_TIME (wakeup);
dc6f92b8 5133
dbc4e1c1
JB
5134 /* Compute time to wait until, propagating carry from usecs. */
5135 wakeup.tv_usec += 150000;
5136 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5137 wakeup.tv_usec %= 1000000;
5138
5139 /* Keep waiting until past the time wakeup. */
5140 while (1)
5141 {
5142 struct timeval timeout;
5143
66c30ea1 5144 EMACS_GET_TIME (timeout);
dbc4e1c1
JB
5145
5146 /* In effect, timeout = wakeup - timeout.
5147 Break if result would be negative. */
5148 if (timeval_subtract (&timeout, wakeup, timeout))
5149 break;
5150
5151 /* Try to wait that long--but we might wake up sooner. */
c32cdd9a 5152 select (0, NULL, NULL, NULL, &timeout);
dbc4e1c1
JB
5153 }
5154 }
58769bee 5155
e84e14c3
RS
5156 /* If window is tall, flash top and bottom line. */
5157 if (height > 3 * FRAME_LINE_HEIGHT (f))
5158 {
5159 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
5160 flash_left,
5161 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 5162 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
5163 width, flash_height);
5164 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5165 flash_left,
5166 (height - flash_height
5167 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5168 width, flash_height);
5169 }
5170 else
5171 /* If it is short, flash it all. */
5172 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5173 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5174 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5175
334208b7 5176 XFreeGC (FRAME_X_DISPLAY (f), gc);
06a2c219 5177 x_flush (f);
dc6f92b8 5178 }
dbc4e1c1
JB
5179 }
5180
5181 UNBLOCK_INPUT;
dc6f92b8
JB
5182}
5183
06a2c219 5184#endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
dbc4e1c1
JB
5185
5186
dc6f92b8
JB
5187/* Make audible bell. */
5188
dfcf069d 5189void
dc6f92b8
JB
5190XTring_bell ()
5191{
b86bd3dd
GM
5192 struct frame *f = SELECTED_FRAME ();
5193
5194 if (FRAME_X_DISPLAY (f))
5195 {
dbc4e1c1 5196#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
b86bd3dd
GM
5197 if (visible_bell)
5198 XTflash (f);
5199 else
dbc4e1c1 5200#endif
b86bd3dd
GM
5201 {
5202 BLOCK_INPUT;
5203 XBell (FRAME_X_DISPLAY (f), 0);
5204 XFlush (FRAME_X_DISPLAY (f));
5205 UNBLOCK_INPUT;
5206 }
dc6f92b8
JB
5207 }
5208}
06a2c219 5209
dc6f92b8 5210\f
06a2c219
GM
5211/* Specify how many text lines, from the top of the window,
5212 should be affected by insert-lines and delete-lines operations.
5213 This, and those operations, are used only within an update
5214 that is bounded by calls to x_update_begin and x_update_end. */
dc6f92b8 5215
dfcf069d 5216static void
06a2c219
GM
5217XTset_terminal_window (n)
5218 register int n;
dc6f92b8 5219{
06a2c219 5220 /* This function intentionally left blank. */
dc6f92b8
JB
5221}
5222
06a2c219
GM
5223
5224\f
5225/***********************************************************************
5226 Line Dance
5227 ***********************************************************************/
5228
5229/* Perform an insert-lines or delete-lines operation, inserting N
5230 lines or deleting -N lines at vertical position VPOS. */
5231
dfcf069d 5232static void
06a2c219
GM
5233x_ins_del_lines (vpos, n)
5234 int vpos, n;
dc6f92b8
JB
5235{
5236 abort ();
5237}
06a2c219
GM
5238
5239
5240/* Scroll part of the display as described by RUN. */
dc6f92b8 5241
dfcf069d 5242static void
06a2c219
GM
5243x_scroll_run (w, run)
5244 struct window *w;
5245 struct run *run;
dc6f92b8 5246{
06a2c219
GM
5247 struct frame *f = XFRAME (w->frame);
5248 int x, y, width, height, from_y, to_y, bottom_y;
5249
5250 /* Get frame-relative bounding box of the text display area of W,
5251 without mode lines. Include in this box the flags areas to the
5252 left and right of W. */
5253 window_box (w, -1, &x, &y, &width, &height);
110859fc
GM
5254 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5255 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
06a2c219
GM
5256
5257 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5258 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5259 bottom_y = y + height;
dc6f92b8 5260
06a2c219
GM
5261 if (to_y < from_y)
5262 {
5263 /* Scrolling up. Make sure we don't copy part of the mode
5264 line at the bottom. */
5265 if (from_y + run->height > bottom_y)
5266 height = bottom_y - from_y;
5267 else
5268 height = run->height;
5269 }
dc6f92b8 5270 else
06a2c219
GM
5271 {
5272 /* Scolling down. Make sure we don't copy over the mode line.
5273 at the bottom. */
5274 if (to_y + run->height > bottom_y)
5275 height = bottom_y - to_y;
5276 else
5277 height = run->height;
5278 }
7a13e894 5279
06a2c219
GM
5280 BLOCK_INPUT;
5281
5282 /* Cursor off. Will be switched on again in x_update_window_end. */
5283 updated_window = w;
5284 x_clear_cursor (w);
5285
5286 XCopyArea (FRAME_X_DISPLAY (f),
5287 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5288 f->output_data.x->normal_gc,
5289 x, from_y,
5290 width, height,
5291 x, to_y);
5292
5293 UNBLOCK_INPUT;
5294}
dc6f92b8 5295
dc6f92b8 5296
06a2c219
GM
5297\f
5298/***********************************************************************
5299 Exposure Events
5300 ***********************************************************************/
5301
5302/* Redisplay an exposed area of frame F. X and Y are the upper-left
5303 corner of the exposed rectangle. W and H are width and height of
5304 the exposed area. All are pixel values. W or H zero means redraw
5305 the entire frame. */
dc6f92b8 5306
06a2c219
GM
5307static void
5308expose_frame (f, x, y, w, h)
5309 struct frame *f;
5310 int x, y, w, h;
dc6f92b8 5311{
06a2c219 5312 XRectangle r;
dc6f92b8 5313
06a2c219 5314 TRACE ((stderr, "expose_frame "));
dc6f92b8 5315
06a2c219
GM
5316 /* No need to redraw if frame will be redrawn soon. */
5317 if (FRAME_GARBAGED_P (f))
dc6f92b8 5318 {
06a2c219
GM
5319 TRACE ((stderr, " garbaged\n"));
5320 return;
5321 }
5322
5323 /* If basic faces haven't been realized yet, there is no point in
5324 trying to redraw anything. This can happen when we get an expose
5325 event while Emacs is starting, e.g. by moving another window. */
5326 if (FRAME_FACE_CACHE (f) == NULL
5327 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5328 {
5329 TRACE ((stderr, " no faces\n"));
5330 return;
58769bee 5331 }
06a2c219
GM
5332
5333 if (w == 0 || h == 0)
58769bee 5334 {
06a2c219
GM
5335 r.x = r.y = 0;
5336 r.width = CANON_X_UNIT (f) * f->width;
5337 r.height = CANON_Y_UNIT (f) * f->height;
dc6f92b8
JB
5338 }
5339 else
5340 {
06a2c219
GM
5341 r.x = x;
5342 r.y = y;
5343 r.width = w;
5344 r.height = h;
5345 }
5346
5347 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5348 expose_window_tree (XWINDOW (f->root_window), &r);
5349
9ea173e8 5350 if (WINDOWP (f->tool_bar_window))
06a2c219 5351 {
9ea173e8 5352 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
5353 XRectangle window_rect;
5354 XRectangle intersection_rect;
5355 int window_x, window_y, window_width, window_height;
5356
5357
5358 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5359 window_rect.x = window_x;
5360 window_rect.y = window_y;
5361 window_rect.width = window_width;
5362 window_rect.height = window_height;
5363
5364 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5365 expose_window (w, &intersection_rect);
5366 }
5367
5368#ifndef USE_X_TOOLKIT
5369 if (WINDOWP (f->menu_bar_window))
5370 {
5371 struct window *w = XWINDOW (f->menu_bar_window);
5372 XRectangle window_rect;
5373 XRectangle intersection_rect;
5374 int window_x, window_y, window_width, window_height;
5375
5376
5377 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5378 window_rect.x = window_x;
5379 window_rect.y = window_y;
5380 window_rect.width = window_width;
5381 window_rect.height = window_height;
5382
5383 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5384 expose_window (w, &intersection_rect);
dc6f92b8 5385 }
06a2c219 5386#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
5387}
5388
06a2c219
GM
5389
5390/* Redraw (parts) of all windows in the window tree rooted at W that
5391 intersect R. R contains frame pixel coordinates. */
5392
58769bee 5393static void
06a2c219
GM
5394expose_window_tree (w, r)
5395 struct window *w;
5396 XRectangle *r;
dc6f92b8 5397{
06a2c219
GM
5398 while (w)
5399 {
5400 if (!NILP (w->hchild))
5401 expose_window_tree (XWINDOW (w->hchild), r);
5402 else if (!NILP (w->vchild))
5403 expose_window_tree (XWINDOW (w->vchild), r);
5404 else
5405 {
5406 XRectangle window_rect;
5407 XRectangle intersection_rect;
5408 struct frame *f = XFRAME (w->frame);
5409 int window_x, window_y, window_width, window_height;
5410
5411 /* Frame-relative pixel rectangle of W. */
5412 window_box (w, -1, &window_x, &window_y, &window_width,
5413 &window_height);
5414 window_rect.x
5415 = (window_x
110859fc 5416 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
714dc26c 5417 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
06a2c219
GM
5418 window_rect.y = window_y;
5419 window_rect.width
5420 = (window_width
110859fc 5421 + FRAME_X_FLAGS_AREA_WIDTH (f)
06a2c219
GM
5422 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5423 window_rect.height
5424 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5425
5426 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5427 expose_window (w, &intersection_rect);
5428 }
58769bee 5429
06a2c219
GM
5430 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5431 }
5432}
58769bee 5433
dc6f92b8 5434
06a2c219
GM
5435/* Redraw the part of glyph row area AREA of glyph row ROW on window W
5436 which intersects rectangle R. R is in window-relative coordinates. */
5437
5438static void
5439expose_area (w, row, r, area)
5440 struct window *w;
5441 struct glyph_row *row;
5442 XRectangle *r;
5443 enum glyph_row_area area;
5444{
5445 int x;
5446 struct glyph *first = row->glyphs[area];
5447 struct glyph *end = row->glyphs[area] + row->used[area];
5448 struct glyph *last;
5449 int first_x;
5450
5451 /* Set x to the window-relative start position for drawing glyphs of
5452 AREA. The first glyph of the text area can be partially visible.
5453 The first glyphs of other areas cannot. */
5454 if (area == LEFT_MARGIN_AREA)
5455 x = 0;
5456 else if (area == TEXT_AREA)
5457 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5458 else
5459 x = (window_box_width (w, LEFT_MARGIN_AREA)
5460 + window_box_width (w, TEXT_AREA));
5461
6fb13182
GM
5462 if (area == TEXT_AREA && row->fill_line_p)
5463 /* If row extends face to end of line write the whole line. */
5464 x_draw_glyphs (w, x, row, area,
5465 0, row->used[area],
06a2c219 5466 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5467 NULL, NULL, 0);
6fb13182
GM
5468 else
5469 {
5470 /* Find the first glyph that must be redrawn. */
5471 while (first < end
5472 && x + first->pixel_width < r->x)
5473 {
5474 x += first->pixel_width;
5475 ++first;
5476 }
5477
5478 /* Find the last one. */
5479 last = first;
5480 first_x = x;
5481 while (last < end
5482 && x < r->x + r->width)
5483 {
5484 x += last->pixel_width;
5485 ++last;
5486 }
5487
5488 /* Repaint. */
5489 if (last > first)
5490 x_draw_glyphs (w, first_x, row, area,
5491 first - row->glyphs[area],
5492 last - row->glyphs[area],
5493 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5494 NULL, NULL, 0);
5495 }
06a2c219
GM
5496}
5497
58769bee 5498
06a2c219
GM
5499/* Redraw the parts of the glyph row ROW on window W intersecting
5500 rectangle R. R is in window-relative coordinates. */
dc6f92b8 5501
06a2c219
GM
5502static void
5503expose_line (w, row, r)
5504 struct window *w;
5505 struct glyph_row *row;
5506 XRectangle *r;
5507{
5508 xassert (row->enabled_p);
5509
5510 if (row->mode_line_p || w->pseudo_window_p)
5511 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5512 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
66ac4b0e 5513 NULL, NULL, 0);
06a2c219
GM
5514 else
5515 {
5516 if (row->used[LEFT_MARGIN_AREA])
5517 expose_area (w, row, r, LEFT_MARGIN_AREA);
5518 if (row->used[TEXT_AREA])
5519 expose_area (w, row, r, TEXT_AREA);
5520 if (row->used[RIGHT_MARGIN_AREA])
5521 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5522 x_draw_row_bitmaps (w, row);
5523 }
5524}
dc6f92b8 5525
58769bee 5526
06a2c219
GM
5527/* Return non-zero if W's cursor intersects rectangle R. */
5528
5529static int
5530x_phys_cursor_in_rect_p (w, r)
5531 struct window *w;
5532 XRectangle *r;
5533{
5534 XRectangle cr, result;
5535 struct glyph *cursor_glyph;
5536
5537 cursor_glyph = get_phys_cursor_glyph (w);
5538 if (cursor_glyph)
5539 {
5540 cr.x = w->phys_cursor.x;
5541 cr.y = w->phys_cursor.y;
5542 cr.width = cursor_glyph->pixel_width;
5543 cr.height = w->phys_cursor_height;
5544 return x_intersect_rectangles (&cr, r, &result);
5545 }
5546 else
5547 return 0;
dc6f92b8 5548}
dc6f92b8 5549
06a2c219
GM
5550
5551/* Redraw a rectangle of window W. R is a rectangle in window
5552 relative coordinates. Call this function with input blocked. */
dc6f92b8
JB
5553
5554static void
06a2c219
GM
5555expose_window (w, r)
5556 struct window *w;
5557 XRectangle *r;
dc6f92b8 5558{
06a2c219
GM
5559 struct glyph_row *row;
5560 int y;
5561 int yb = window_text_bottom_y (w);
5562 int cursor_cleared_p;
dc6f92b8 5563
80c32bcc
GM
5564 /* If window is not yet fully initialized, do nothing. This can
5565 happen when toolkit scroll bars are used and a window is split.
5566 Reconfiguring the scroll bar will generate an expose for a newly
5567 created window. */
5568 if (w->current_matrix == NULL)
5569 return;
5570
06a2c219
GM
5571 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5572 r->x, r->y, r->width, r->height));
dc6f92b8 5573
06a2c219
GM
5574 /* Convert to window coordinates. */
5575 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5576 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
dc6f92b8 5577
06a2c219
GM
5578 /* Turn off the cursor. */
5579 if (!w->pseudo_window_p
5580 && x_phys_cursor_in_rect_p (w, r))
5581 {
5582 x_clear_cursor (w);
5583 cursor_cleared_p = 1;
5584 }
5585 else
5586 cursor_cleared_p = 0;
5587
5588 /* Find the first row intersecting the rectangle R. */
5589 row = w->current_matrix->rows;
5590 y = 0;
5591 while (row->enabled_p
5592 && y < yb
5593 && y + row->height < r->y)
5594 {
5595 y += row->height;
5596 ++row;
5597 }
5598
dc6f92b8 5599 /* Display the text in the rectangle, one text line at a time. */
06a2c219
GM
5600 while (row->enabled_p
5601 && y < yb
5602 && y < r->y + r->height)
5603 {
5604 expose_line (w, row, r);
5605 y += row->height;
5606 ++row;
5607 }
5608
5609 /* Display the mode line if there is one. */
5610 if (WINDOW_WANTS_MODELINE_P (w)
5611 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5612 row->enabled_p)
5613 && row->y < r->y + r->height)
5614 expose_line (w, row, r);
dc6f92b8 5615
06a2c219 5616 if (!w->pseudo_window_p)
dc6f92b8 5617 {
06a2c219
GM
5618 /* Draw border between windows. */
5619 x_draw_vertical_border (w);
5620
5621 /* Turn the cursor on again. */
5622 if (cursor_cleared_p)
5623 x_update_window_cursor (w, 1);
5624 }
5625}
dc6f92b8 5626
dc6f92b8 5627
06a2c219
GM
5628/* Determine the intersection of two rectangles R1 and R2. Return
5629 the intersection in *RESULT. Value is non-zero if RESULT is not
5630 empty. */
5631
5632static int
5633x_intersect_rectangles (r1, r2, result)
5634 XRectangle *r1, *r2, *result;
5635{
5636 XRectangle *left, *right;
5637 XRectangle *upper, *lower;
5638 int intersection_p = 0;
5639
5640 /* Rearrange so that R1 is the left-most rectangle. */
5641 if (r1->x < r2->x)
5642 left = r1, right = r2;
5643 else
5644 left = r2, right = r1;
5645
5646 /* X0 of the intersection is right.x0, if this is inside R1,
5647 otherwise there is no intersection. */
5648 if (right->x <= left->x + left->width)
5649 {
5650 result->x = right->x;
5651
5652 /* The right end of the intersection is the minimum of the
5653 the right ends of left and right. */
5654 result->width = (min (left->x + left->width, right->x + right->width)
5655 - result->x);
5656
5657 /* Same game for Y. */
5658 if (r1->y < r2->y)
5659 upper = r1, lower = r2;
5660 else
5661 upper = r2, lower = r1;
5662
5663 /* The upper end of the intersection is lower.y0, if this is inside
5664 of upper. Otherwise, there is no intersection. */
5665 if (lower->y <= upper->y + upper->height)
dc43ef94 5666 {
06a2c219
GM
5667 result->y = lower->y;
5668
5669 /* The lower end of the intersection is the minimum of the lower
5670 ends of upper and lower. */
5671 result->height = (min (lower->y + lower->height,
5672 upper->y + upper->height)
5673 - result->y);
5674 intersection_p = 1;
dc43ef94 5675 }
dc6f92b8
JB
5676 }
5677
06a2c219 5678 return intersection_p;
dc6f92b8 5679}
06a2c219
GM
5680
5681
5682
5683
dc6f92b8 5684\f
dc6f92b8 5685static void
334208b7
RS
5686frame_highlight (f)
5687 struct frame *f;
dc6f92b8 5688{
b3e1e05c
JB
5689 /* We used to only do this if Vx_no_window_manager was non-nil, but
5690 the ICCCM (section 4.1.6) says that the window's border pixmap
5691 and border pixel are window attributes which are "private to the
5692 client", so we can always change it to whatever we want. */
5693 BLOCK_INPUT;
334208b7 5694 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5695 f->output_data.x->border_pixel);
b3e1e05c 5696 UNBLOCK_INPUT;
5d46f928 5697 x_update_cursor (f, 1);
dc6f92b8
JB
5698}
5699
5700static void
334208b7
RS
5701frame_unhighlight (f)
5702 struct frame *f;
dc6f92b8 5703{
b3e1e05c
JB
5704 /* We used to only do this if Vx_no_window_manager was non-nil, but
5705 the ICCCM (section 4.1.6) says that the window's border pixmap
5706 and border pixel are window attributes which are "private to the
5707 client", so we can always change it to whatever we want. */
5708 BLOCK_INPUT;
334208b7 5709 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5710 f->output_data.x->border_tile);
b3e1e05c 5711 UNBLOCK_INPUT;
5d46f928 5712 x_update_cursor (f, 1);
dc6f92b8 5713}
dc6f92b8 5714
f676886a
JB
5715/* The focus has changed. Update the frames as necessary to reflect
5716 the new situation. Note that we can't change the selected frame
c5acd733 5717 here, because the Lisp code we are interrupting might become confused.
eb8c3be9 5718 Each event gets marked with the frame in which it occurred, so the
c5acd733 5719 Lisp code can tell when the switch took place by examining the events. */
dc6f92b8 5720
6d4238f3 5721static void
0f941935
KH
5722x_new_focus_frame (dpyinfo, frame)
5723 struct x_display_info *dpyinfo;
f676886a 5724 struct frame *frame;
dc6f92b8 5725{
0f941935 5726 struct frame *old_focus = dpyinfo->x_focus_frame;
dc6f92b8 5727
0f941935 5728 if (frame != dpyinfo->x_focus_frame)
dc6f92b8 5729 {
58769bee 5730 /* Set this before calling other routines, so that they see
f676886a 5731 the correct value of x_focus_frame. */
0f941935 5732 dpyinfo->x_focus_frame = frame;
6d4238f3
JB
5733
5734 if (old_focus && old_focus->auto_lower)
f676886a 5735 x_lower_frame (old_focus);
dc6f92b8
JB
5736
5737#if 0
f676886a 5738 selected_frame = frame;
e0c1aef2
KH
5739 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
5740 selected_frame);
f676886a
JB
5741 Fselect_window (selected_frame->selected_window);
5742 choose_minibuf_frame ();
c118dd06 5743#endif /* ! 0 */
dc6f92b8 5744
0f941935
KH
5745 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
5746 pending_autoraise_frame = dpyinfo->x_focus_frame;
0134a210
RS
5747 else
5748 pending_autoraise_frame = 0;
6d4238f3 5749 }
dc6f92b8 5750
0f941935 5751 x_frame_rehighlight (dpyinfo);
6d4238f3
JB
5752}
5753
37c2c98b
RS
5754/* Handle an event saying the mouse has moved out of an Emacs frame. */
5755
5756void
0f941935
KH
5757x_mouse_leave (dpyinfo)
5758 struct x_display_info *dpyinfo;
37c2c98b 5759{
0f941935 5760 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
37c2c98b 5761}
6d4238f3 5762
f451eb13
JB
5763/* The focus has changed, or we have redirected a frame's focus to
5764 another frame (this happens when a frame uses a surrogate
06a2c219 5765 mini-buffer frame). Shift the highlight as appropriate.
0f941935
KH
5766
5767 The FRAME argument doesn't necessarily have anything to do with which
06a2c219 5768 frame is being highlighted or un-highlighted; we only use it to find
0f941935 5769 the appropriate X display info. */
06a2c219 5770
6d4238f3 5771static void
0f941935
KH
5772XTframe_rehighlight (frame)
5773 struct frame *frame;
6d4238f3 5774{
0f941935
KH
5775 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
5776}
6d4238f3 5777
0f941935
KH
5778static void
5779x_frame_rehighlight (dpyinfo)
5780 struct x_display_info *dpyinfo;
5781{
5782 struct frame *old_highlight = dpyinfo->x_highlight_frame;
5783
5784 if (dpyinfo->x_focus_frame)
6d4238f3 5785 {
0f941935
KH
5786 dpyinfo->x_highlight_frame
5787 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
5788 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
5789 : dpyinfo->x_focus_frame);
5790 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
f451eb13 5791 {
0f941935
KH
5792 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
5793 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
f451eb13 5794 }
dc6f92b8 5795 }
6d4238f3 5796 else
0f941935 5797 dpyinfo->x_highlight_frame = 0;
dc6f92b8 5798
0f941935 5799 if (dpyinfo->x_highlight_frame != old_highlight)
6d4238f3
JB
5800 {
5801 if (old_highlight)
f676886a 5802 frame_unhighlight (old_highlight);
0f941935
KH
5803 if (dpyinfo->x_highlight_frame)
5804 frame_highlight (dpyinfo->x_highlight_frame);
6d4238f3 5805 }
dc6f92b8 5806}
06a2c219
GM
5807
5808
dc6f92b8 5809\f
06a2c219 5810/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
dc6f92b8 5811
28430d3c
JB
5812/* Initialize mode_switch_bit and modifier_meaning. */
5813static void
334208b7
RS
5814x_find_modifier_meanings (dpyinfo)
5815 struct x_display_info *dpyinfo;
28430d3c 5816{
f689eb05 5817 int min_code, max_code;
28430d3c
JB
5818 KeySym *syms;
5819 int syms_per_code;
5820 XModifierKeymap *mods;
5821
334208b7
RS
5822 dpyinfo->meta_mod_mask = 0;
5823 dpyinfo->shift_lock_mask = 0;
5824 dpyinfo->alt_mod_mask = 0;
5825 dpyinfo->super_mod_mask = 0;
5826 dpyinfo->hyper_mod_mask = 0;
58769bee 5827
9658a521 5828#ifdef HAVE_X11R4
334208b7 5829 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
9658a521 5830#else
4a60f8c5
RS
5831 min_code = dpyinfo->display->min_keycode;
5832 max_code = dpyinfo->display->max_keycode;
9658a521
JB
5833#endif
5834
334208b7 5835 syms = XGetKeyboardMapping (dpyinfo->display,
28430d3c
JB
5836 min_code, max_code - min_code + 1,
5837 &syms_per_code);
334208b7 5838 mods = XGetModifierMapping (dpyinfo->display);
28430d3c 5839
58769bee 5840 /* Scan the modifier table to see which modifier bits the Meta and
11edeb03 5841 Alt keysyms are on. */
28430d3c 5842 {
06a2c219 5843 int row, col; /* The row and column in the modifier table. */
28430d3c
JB
5844
5845 for (row = 3; row < 8; row++)
5846 for (col = 0; col < mods->max_keypermod; col++)
5847 {
0299d313
RS
5848 KeyCode code
5849 = mods->modifiermap[(row * mods->max_keypermod) + col];
28430d3c 5850
af92970c
KH
5851 /* Zeroes are used for filler. Skip them. */
5852 if (code == 0)
5853 continue;
5854
28430d3c
JB
5855 /* Are any of this keycode's keysyms a meta key? */
5856 {
5857 int code_col;
5858
5859 for (code_col = 0; code_col < syms_per_code; code_col++)
5860 {
f689eb05 5861 int sym = syms[((code - min_code) * syms_per_code) + code_col];
28430d3c 5862
f689eb05 5863 switch (sym)
28430d3c 5864 {
f689eb05
JB
5865 case XK_Meta_L:
5866 case XK_Meta_R:
334208b7 5867 dpyinfo->meta_mod_mask |= (1 << row);
28430d3c 5868 break;
f689eb05
JB
5869
5870 case XK_Alt_L:
5871 case XK_Alt_R:
334208b7 5872 dpyinfo->alt_mod_mask |= (1 << row);
a3c44b14
RS
5873 break;
5874
5875 case XK_Hyper_L:
5876 case XK_Hyper_R:
334208b7 5877 dpyinfo->hyper_mod_mask |= (1 << row);
a3c44b14
RS
5878 break;
5879
5880 case XK_Super_L:
5881 case XK_Super_R:
334208b7 5882 dpyinfo->super_mod_mask |= (1 << row);
f689eb05 5883 break;
11edeb03
JB
5884
5885 case XK_Shift_Lock:
5886 /* Ignore this if it's not on the lock modifier. */
5887 if ((1 << row) == LockMask)
334208b7 5888 dpyinfo->shift_lock_mask = LockMask;
11edeb03 5889 break;
28430d3c
JB
5890 }
5891 }
5892 }
5893 }
5894 }
5895
f689eb05 5896 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
334208b7 5897 if (! dpyinfo->meta_mod_mask)
a3c44b14 5898 {
334208b7
RS
5899 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
5900 dpyinfo->alt_mod_mask = 0;
a3c44b14 5901 }
f689eb05 5902
148c4b70
RS
5903 /* If some keys are both alt and meta,
5904 make them just meta, not alt. */
334208b7 5905 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
148c4b70 5906 {
334208b7 5907 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
148c4b70 5908 }
58769bee 5909
28430d3c 5910 XFree ((char *) syms);
f689eb05 5911 XFreeModifiermap (mods);
28430d3c
JB
5912}
5913
dfeccd2d
JB
5914/* Convert between the modifier bits X uses and the modifier bits
5915 Emacs uses. */
06a2c219 5916
7c5283e4 5917static unsigned int
334208b7
RS
5918x_x_to_emacs_modifiers (dpyinfo, state)
5919 struct x_display_info *dpyinfo;
dc6f92b8
JB
5920 unsigned int state;
5921{
334208b7
RS
5922 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
5923 | ((state & ControlMask) ? ctrl_modifier : 0)
5924 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
5925 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
5926 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
5927 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
dc6f92b8
JB
5928}
5929
dfeccd2d 5930static unsigned int
334208b7
RS
5931x_emacs_to_x_modifiers (dpyinfo, state)
5932 struct x_display_info *dpyinfo;
dfeccd2d
JB
5933 unsigned int state;
5934{
334208b7
RS
5935 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
5936 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
5937 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
5938 | ((state & shift_modifier) ? ShiftMask : 0)
5939 | ((state & ctrl_modifier) ? ControlMask : 0)
5940 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
dfeccd2d 5941}
d047c4eb
KH
5942
5943/* Convert a keysym to its name. */
5944
5945char *
5946x_get_keysym_name (keysym)
5947 KeySym keysym;
5948{
5949 char *value;
5950
5951 BLOCK_INPUT;
5952 value = XKeysymToString (keysym);
5953 UNBLOCK_INPUT;
5954
5955 return value;
5956}
06a2c219
GM
5957
5958
e4571a43
JB
5959\f
5960/* Mouse clicks and mouse movement. Rah. */
e4571a43 5961
06a2c219
GM
5962/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
5963 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
5964 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
5965 not force the value into range. */
69388238 5966
c8dba240 5967void
69388238 5968pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
e4571a43 5969 FRAME_PTR f;
69388238 5970 register int pix_x, pix_y;
e4571a43
JB
5971 register int *x, *y;
5972 XRectangle *bounds;
69388238 5973 int noclip;
e4571a43 5974{
06a2c219 5975 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
69388238
RS
5976 even for negative values. */
5977 if (pix_x < 0)
7556890b 5978 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
69388238 5979 if (pix_y < 0)
7556890b 5980 pix_y -= (f)->output_data.x->line_height - 1;
69388238 5981
e4571a43
JB
5982 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
5983 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
5984
5985 if (bounds)
5986 {
7556890b
RS
5987 bounds->width = FONT_WIDTH (f->output_data.x->font);
5988 bounds->height = f->output_data.x->line_height;
e4571a43
JB
5989 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
5990 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
5991 }
5992
69388238
RS
5993 if (!noclip)
5994 {
5995 if (pix_x < 0)
5996 pix_x = 0;
3cbd2e0b
RS
5997 else if (pix_x > FRAME_WINDOW_WIDTH (f))
5998 pix_x = FRAME_WINDOW_WIDTH (f);
69388238
RS
5999
6000 if (pix_y < 0)
6001 pix_y = 0;
6002 else if (pix_y > f->height)
6003 pix_y = f->height;
6004 }
e4571a43
JB
6005
6006 *x = pix_x;
6007 *y = pix_y;
6008}
6009
06a2c219
GM
6010
6011/* Given HPOS/VPOS in the current matrix of W, return corresponding
6012 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6013 can't tell the positions because W's display is not up to date,
6014 return 0. */
6015
6016int
6017glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6018 struct window *w;
6019 int hpos, vpos;
6020 int *frame_x, *frame_y;
2b5c9e71 6021{
06a2c219
GM
6022 int success_p;
6023
6024 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6025 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6026
6027 if (display_completed)
6028 {
6029 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6030 struct glyph *glyph = row->glyphs[TEXT_AREA];
6031 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6032
6033 *frame_y = row->y;
6034 *frame_x = row->x;
6035 while (glyph < end)
6036 {
6037 *frame_x += glyph->pixel_width;
6038 ++glyph;
6039 }
6040
6041 success_p = 1;
6042 }
6043 else
6044 {
6045 *frame_y = *frame_x = 0;
6046 success_p = 0;
6047 }
6048
6049 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6050 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6051 return success_p;
2b5c9e71
RS
6052}
6053
06a2c219 6054
dc6f92b8
JB
6055/* Prepare a mouse-event in *RESULT for placement in the input queue.
6056
6057 If the event is a button press, then note that we have grabbed
f451eb13 6058 the mouse. */
dc6f92b8
JB
6059
6060static Lisp_Object
f451eb13 6061construct_mouse_click (result, event, f)
dc6f92b8
JB
6062 struct input_event *result;
6063 XButtonEvent *event;
f676886a 6064 struct frame *f;
dc6f92b8 6065{
f451eb13 6066 /* Make the event type no_event; we'll change that when we decide
dc6f92b8 6067 otherwise. */
f451eb13 6068 result->kind = mouse_click;
69388238 6069 result->code = event->button - Button1;
1113d9db 6070 result->timestamp = event->time;
334208b7
RS
6071 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6072 event->state)
f689eb05 6073 | (event->type == ButtonRelease
58769bee 6074 ? up_modifier
f689eb05 6075 : down_modifier));
dc6f92b8 6076
06a2c219
GM
6077 XSETINT (result->x, event->x);
6078 XSETINT (result->y, event->y);
6079 XSETFRAME (result->frame_or_window, f);
6080 return Qnil;
dc6f92b8 6081}
b849c413 6082
06a2c219
GM
6083#if 0 /* This function isn't called. --gerd */
6084
b849c413
RS
6085/* Prepare a menu-event in *RESULT for placement in the input queue. */
6086
6087static Lisp_Object
6088construct_menu_click (result, event, f)
6089 struct input_event *result;
6090 XButtonEvent *event;
6091 struct frame *f;
6092{
6093 /* Make the event type no_event; we'll change that when we decide
6094 otherwise. */
6095 result->kind = mouse_click;
26459b28 6096 result->code = event->button - Button1;
b849c413 6097 result->timestamp = event->time;
334208b7
RS
6098 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6099 event->state)
b849c413 6100 | (event->type == ButtonRelease
58769bee 6101 ? up_modifier
b849c413
RS
6102 : down_modifier));
6103
e0c1aef2
KH
6104 XSETINT (result->x, event->x);
6105 XSETINT (result->y, -1);
6106 XSETFRAME (result->frame_or_window, f);
b849c413 6107}
06a2c219
GM
6108
6109#endif /* 0 */
6110
69388238 6111\f
90e65f07
JB
6112/* Function to report a mouse movement to the mainstream Emacs code.
6113 The input handler calls this.
6114
6115 We have received a mouse movement event, which is given in *event.
6116 If the mouse is over a different glyph than it was last time, tell
6117 the mainstream emacs code by setting mouse_moved. If not, ask for
6118 another motion event, so we can check again the next time it moves. */
b8009dd1 6119
06a2c219
GM
6120static XMotionEvent last_mouse_motion_event;
6121static Lisp_Object last_mouse_motion_frame;
6122
90e65f07 6123static void
12ba150f 6124note_mouse_movement (frame, event)
f676886a 6125 FRAME_PTR frame;
90e65f07 6126 XMotionEvent *event;
90e65f07 6127{
e5d77022 6128 last_mouse_movement_time = event->time;
06a2c219
GM
6129 last_mouse_motion_event = *event;
6130 XSETFRAME (last_mouse_motion_frame, frame);
e5d77022 6131
27f338af
RS
6132 if (event->window != FRAME_X_WINDOW (frame))
6133 {
39d8bb4d 6134 frame->mouse_moved = 1;
27f338af 6135 last_mouse_scroll_bar = Qnil;
27f338af 6136 note_mouse_highlight (frame, -1, -1);
27f338af
RS
6137 }
6138
90e65f07 6139 /* Has the mouse moved off the glyph it was on at the last sighting? */
27f338af
RS
6140 else if (event->x < last_mouse_glyph.x
6141 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6142 || event->y < last_mouse_glyph.y
6143 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
12ba150f 6144 {
39d8bb4d 6145 frame->mouse_moved = 1;
ab648270 6146 last_mouse_scroll_bar = Qnil;
b8009dd1 6147 note_mouse_highlight (frame, event->x, event->y);
90e65f07
JB
6148 }
6149}
6150
bf1c0ba1 6151/* This is used for debugging, to turn off note_mouse_highlight. */
bf1c0ba1 6152
06a2c219
GM
6153 int disable_mouse_highlight;
6154
6155
6156\f
6157/************************************************************************
6158 Mouse Face
6159 ************************************************************************/
6160
6161/* Find the glyph under window-relative coordinates X/Y in window W.
6162 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6163 strings. Return in *HPOS and *VPOS the row and column number of
6164 the glyph found. Return in *AREA the glyph area containing X.
6165 Value is a pointer to the glyph found or null if X/Y is not on
6166 text, or we can't tell because W's current matrix is not up to
6167 date. */
6168
6169static struct glyph *
6170x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6171 struct window *w;
6172 int x, y;
6173 int *hpos, *vpos, *area;
6174{
6175 struct glyph *glyph, *end;
3e71d8f2 6176 struct glyph_row *row = NULL;
06a2c219
GM
6177 int x0, i, left_area_width;
6178
6179 /* Find row containing Y. Give up if some row is not enabled. */
6180 for (i = 0; i < w->current_matrix->nrows; ++i)
6181 {
6182 row = MATRIX_ROW (w->current_matrix, i);
6183 if (!row->enabled_p)
6184 return NULL;
6185 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6186 break;
6187 }
6188
6189 *vpos = i;
6190 *hpos = 0;
6191
6192 /* Give up if Y is not in the window. */
6193 if (i == w->current_matrix->nrows)
6194 return NULL;
6195
6196 /* Get the glyph area containing X. */
6197 if (w->pseudo_window_p)
6198 {
6199 *area = TEXT_AREA;
6200 x0 = 0;
6201 }
6202 else
6203 {
6204 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6205 if (x < left_area_width)
6206 {
6207 *area = LEFT_MARGIN_AREA;
6208 x0 = 0;
6209 }
6210 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6211 {
6212 *area = TEXT_AREA;
6213 x0 = row->x + left_area_width;
6214 }
6215 else
6216 {
6217 *area = RIGHT_MARGIN_AREA;
6218 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6219 }
6220 }
6221
6222 /* Find glyph containing X. */
6223 glyph = row->glyphs[*area];
6224 end = glyph + row->used[*area];
6225 while (glyph < end)
6226 {
6227 if (x < x0 + glyph->pixel_width)
6228 {
6229 if (w->pseudo_window_p)
6230 break;
6231 else if (BUFFERP (glyph->object))
6232 break;
6233 }
6234
6235 x0 += glyph->pixel_width;
6236 ++glyph;
6237 }
6238
6239 if (glyph == end)
6240 return NULL;
6241
6242 *hpos = glyph - row->glyphs[*area];
6243 return glyph;
6244}
6245
6246
6247/* Convert frame-relative x/y to coordinates relative to window W.
6248 Takes pseudo-windows into account. */
6249
6250static void
6251frame_to_window_pixel_xy (w, x, y)
6252 struct window *w;
6253 int *x, *y;
6254{
6255 if (w->pseudo_window_p)
6256 {
6257 /* A pseudo-window is always full-width, and starts at the
6258 left edge of the frame, plus a frame border. */
6259 struct frame *f = XFRAME (w->frame);
6260 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6261 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6262 }
6263 else
6264 {
6265 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6266 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6267 }
6268}
6269
6270
6271/* Take proper action when mouse has moved to the mode or top line of
6272 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6273 mode line. X is relative to the start of the text display area of
6274 W, so the width of bitmap areas and scroll bars must be subtracted
6275 to get a position relative to the start of the mode line. */
6276
6277static void
6278note_mode_line_highlight (w, x, mode_line_p)
6279 struct window *w;
6280 int x, mode_line_p;
6281{
6282 struct frame *f = XFRAME (w->frame);
6283 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6284 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6285 struct glyph_row *row;
6286
6287 if (mode_line_p)
6288 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6289 else
045dee35 6290 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
06a2c219
GM
6291
6292 if (row->enabled_p)
6293 {
6294 struct glyph *glyph, *end;
6295 Lisp_Object help, map;
6296 int x0;
6297
6298 /* Find the glyph under X. */
6299 glyph = row->glyphs[TEXT_AREA];
6300 end = glyph + row->used[TEXT_AREA];
6301 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
110859fc 6302 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
06a2c219
GM
6303 while (glyph < end
6304 && x >= x0 + glyph->pixel_width)
6305 {
6306 x0 += glyph->pixel_width;
6307 ++glyph;
6308 }
6309
6310 if (glyph < end
6311 && STRINGP (glyph->object)
6312 && XSTRING (glyph->object)->intervals
6313 && glyph->charpos >= 0
6314 && glyph->charpos < XSTRING (glyph->object)->size)
6315 {
6316 /* If we're on a string with `help-echo' text property,
6317 arrange for the help to be displayed. This is done by
6318 setting the global variable help_echo to the help string. */
6319 help = Fget_text_property (make_number (glyph->charpos),
6320 Qhelp_echo, glyph->object);
6321 if (STRINGP (help))
6322 help_echo = help;
6323
6324 /* Change the mouse pointer according to what is under X/Y. */
6325 map = Fget_text_property (make_number (glyph->charpos),
6326 Qlocal_map, glyph->object);
6327 if (!NILP (Fkeymapp (map)))
6328 cursor = f->output_data.x->nontext_cursor;
6329 }
6330 }
6331
6332 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6333}
6334
6335
6336/* Take proper action when the mouse has moved to position X, Y on
6337 frame F as regards highlighting characters that have mouse-face
6338 properties. Also de-highlighting chars where the mouse was before.
27f338af 6339 X and Y can be negative or out of range. */
b8009dd1
RS
6340
6341static void
6342note_mouse_highlight (f, x, y)
06a2c219 6343 struct frame *f;
c32cdd9a 6344 int x, y;
b8009dd1 6345{
06a2c219
GM
6346 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6347 int portion;
b8009dd1
RS
6348 Lisp_Object window;
6349 struct window *w;
6350
06a2c219
GM
6351 /* When a menu is active, don't highlight because this looks odd. */
6352#ifdef USE_X_TOOLKIT
6353 if (popup_activated ())
6354 return;
6355#endif
6356
04fff9c0
GM
6357 if (disable_mouse_highlight
6358 || !f->glyphs_initialized_p)
bf1c0ba1
RS
6359 return;
6360
06a2c219
GM
6361 dpyinfo->mouse_face_mouse_x = x;
6362 dpyinfo->mouse_face_mouse_y = y;
6363 dpyinfo->mouse_face_mouse_frame = f;
b8009dd1 6364
06a2c219 6365 if (dpyinfo->mouse_face_defer)
b8009dd1
RS
6366 return;
6367
514e4681
RS
6368 if (gc_in_progress)
6369 {
06a2c219 6370 dpyinfo->mouse_face_deferred_gc = 1;
514e4681
RS
6371 return;
6372 }
6373
b8009dd1 6374 /* Which window is that in? */
06a2c219 6375 window = window_from_coordinates (f, x, y, &portion, 1);
b8009dd1
RS
6376
6377 /* If we were displaying active text in another window, clear that. */
06a2c219
GM
6378 if (! EQ (window, dpyinfo->mouse_face_window))
6379 clear_mouse_face (dpyinfo);
6380
6381 /* Not on a window -> return. */
6382 if (!WINDOWP (window))
6383 return;
6384
6385 /* Convert to window-relative pixel coordinates. */
6386 w = XWINDOW (window);
6387 frame_to_window_pixel_xy (w, &x, &y);
6388
9ea173e8 6389 /* Handle tool-bar window differently since it doesn't display a
06a2c219 6390 buffer. */
9ea173e8 6391 if (EQ (window, f->tool_bar_window))
06a2c219 6392 {
9ea173e8 6393 note_tool_bar_highlight (f, x, y);
06a2c219
GM
6394 return;
6395 }
6396
6397 if (portion == 1 || portion == 3)
6398 {
6399 /* Mouse is on the mode or top line. */
6400 note_mode_line_highlight (w, x, portion == 1);
6401 return;
6402 }
6403 else
6404 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6405 f->output_data.x->text_cursor);
b8009dd1 6406
0cdd0c9f
RS
6407 /* Are we in a window whose display is up to date?
6408 And verify the buffer's text has not changed. */
06a2c219
GM
6409 if (/* Within text portion of the window. */
6410 portion == 0
0cdd0c9f 6411 && EQ (w->window_end_valid, w->buffer)
26459b28
KH
6412 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6413 && (XFASTINT (w->last_overlay_modified)
6414 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
b8009dd1 6415 {
06a2c219
GM
6416 int hpos, vpos, pos, i, area;
6417 struct glyph *glyph;
b8009dd1 6418
06a2c219
GM
6419 /* Find the glyph under X/Y. */
6420 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6421
6422 /* Clear mouse face if X/Y not over text. */
6423 if (glyph == NULL
6424 || area != TEXT_AREA
6425 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
b8009dd1 6426 {
06a2c219
GM
6427 clear_mouse_face (dpyinfo);
6428 return;
6429 }
6430
6431 pos = glyph->charpos;
6432 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6433
6434 /* Check for mouse-face and help-echo. */
6435 {
6436 Lisp_Object mouse_face, overlay, position;
6437 Lisp_Object *overlay_vec;
6438 int len, noverlays;
6439 struct buffer *obuf;
6440 int obegv, ozv;
6441
6442 /* If we get an out-of-range value, return now; avoid an error. */
6443 if (pos > BUF_Z (XBUFFER (w->buffer)))
6444 return;
6445
6446 /* Make the window's buffer temporarily current for
6447 overlays_at and compute_char_face. */
6448 obuf = current_buffer;
6449 current_buffer = XBUFFER (w->buffer);
6450 obegv = BEGV;
6451 ozv = ZV;
6452 BEGV = BEG;
6453 ZV = Z;
6454
6455 /* Is this char mouse-active or does it have help-echo? */
6456 XSETINT (position, pos);
6457
6458 /* Put all the overlays we want in a vector in overlay_vec.
6459 Store the length in len. If there are more than 10, make
6460 enough space for all, and try again. */
6461 len = 10;
6462 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6463 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6464 if (noverlays > len)
6465 {
6466 len = noverlays;
6467 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6468 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6469 }
6470
6471 noverlays = sort_overlays (overlay_vec, noverlays, w);
6472
6473 /* Check mouse-face highlighting. */
6474 if (! (EQ (window, dpyinfo->mouse_face_window)
6475 && vpos >= dpyinfo->mouse_face_beg_row
6476 && vpos <= dpyinfo->mouse_face_end_row
6477 && (vpos > dpyinfo->mouse_face_beg_row
6478 || hpos >= dpyinfo->mouse_face_beg_col)
6479 && (vpos < dpyinfo->mouse_face_end_row
6480 || hpos < dpyinfo->mouse_face_end_col
6481 || dpyinfo->mouse_face_past_end)))
6482 {
6483 /* Clear the display of the old active region, if any. */
6484 clear_mouse_face (dpyinfo);
6485
6486 /* Find the highest priority overlay that has a mouse-face prop. */
6487 overlay = Qnil;
6488 for (i = 0; i < noverlays; i++)
6489 {
6490 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6491 if (!NILP (mouse_face))
6492 {
6493 overlay = overlay_vec[i];
6494 break;
6495 }
6496 }
6497
6498 /* If no overlay applies, get a text property. */
6499 if (NILP (overlay))
6500 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6501
6502 /* Handle the overlay case. */
6503 if (! NILP (overlay))
6504 {
6505 /* Find the range of text around this char that
6506 should be active. */
6507 Lisp_Object before, after;
6508 int ignore;
6509
6510 before = Foverlay_start (overlay);
6511 after = Foverlay_end (overlay);
6512 /* Record this as the current active region. */
6513 fast_find_position (w, XFASTINT (before),
6514 &dpyinfo->mouse_face_beg_col,
6515 &dpyinfo->mouse_face_beg_row,
6516 &dpyinfo->mouse_face_beg_x,
6517 &dpyinfo->mouse_face_beg_y);
6518 dpyinfo->mouse_face_past_end
6519 = !fast_find_position (w, XFASTINT (after),
6520 &dpyinfo->mouse_face_end_col,
6521 &dpyinfo->mouse_face_end_row,
6522 &dpyinfo->mouse_face_end_x,
6523 &dpyinfo->mouse_face_end_y);
6524 dpyinfo->mouse_face_window = window;
6525 dpyinfo->mouse_face_face_id
6526 = face_at_buffer_position (w, pos, 0, 0,
6527 &ignore, pos + 1, 1);
6528
6529 /* Display it as active. */
6530 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6531 }
6532 /* Handle the text property case. */
6533 else if (! NILP (mouse_face))
6534 {
6535 /* Find the range of text around this char that
6536 should be active. */
6537 Lisp_Object before, after, beginning, end;
6538 int ignore;
6539
6540 beginning = Fmarker_position (w->start);
6541 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6542 - XFASTINT (w->window_end_pos)));
6543 before
6544 = Fprevious_single_property_change (make_number (pos + 1),
6545 Qmouse_face,
6546 w->buffer, beginning);
6547 after
6548 = Fnext_single_property_change (position, Qmouse_face,
6549 w->buffer, end);
6550 /* Record this as the current active region. */
6551 fast_find_position (w, XFASTINT (before),
6552 &dpyinfo->mouse_face_beg_col,
6553 &dpyinfo->mouse_face_beg_row,
6554 &dpyinfo->mouse_face_beg_x,
6555 &dpyinfo->mouse_face_beg_y);
6556 dpyinfo->mouse_face_past_end
6557 = !fast_find_position (w, XFASTINT (after),
6558 &dpyinfo->mouse_face_end_col,
6559 &dpyinfo->mouse_face_end_row,
6560 &dpyinfo->mouse_face_end_x,
6561 &dpyinfo->mouse_face_end_y);
6562 dpyinfo->mouse_face_window = window;
6563 dpyinfo->mouse_face_face_id
6564 = face_at_buffer_position (w, pos, 0, 0,
6565 &ignore, pos + 1, 1);
6566
6567 /* Display it as active. */
6568 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6569 }
6570 }
6571
6572 /* Look for a `help-echo' property. */
6573 {
6574 Lisp_Object help;
6575
6576 /* Check overlays first. */
6577 help = Qnil;
6578 for (i = 0; i < noverlays && !STRINGP (help); ++i)
6579 help = Foverlay_get (overlay_vec[i], Qhelp_echo);
6580
6581 /* Try text properties. */
6582 if (!STRINGP (help)
6583 && ((STRINGP (glyph->object)
6584 && glyph->charpos >= 0
6585 && glyph->charpos < XSTRING (glyph->object)->size)
6586 || (BUFFERP (glyph->object)
6587 && glyph->charpos >= BEGV
6588 && glyph->charpos < ZV)))
6589 help = Fget_text_property (make_number (glyph->charpos),
6590 Qhelp_echo, glyph->object);
6591
6592 if (STRINGP (help))
6593 help_echo = help;
6594 }
6595
6596 BEGV = obegv;
6597 ZV = ozv;
6598 current_buffer = obuf;
6599 }
6600 }
6601}
6602
6603static void
6604redo_mouse_highlight ()
6605{
6606 if (!NILP (last_mouse_motion_frame)
6607 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6608 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6609 last_mouse_motion_event.x,
6610 last_mouse_motion_event.y);
6611}
6612
6613
6614\f
6615/***********************************************************************
9ea173e8 6616 Tool-bars
06a2c219
GM
6617 ***********************************************************************/
6618
9ea173e8
GM
6619static int x_tool_bar_item P_ ((struct frame *, int, int,
6620 struct glyph **, int *, int *, int *));
06a2c219 6621
9ea173e8 6622/* Tool-bar item index of the item on which a mouse button was pressed
06a2c219
GM
6623 or -1. */
6624
9ea173e8 6625static int last_tool_bar_item;
06a2c219
GM
6626
6627
9ea173e8
GM
6628/* Get information about the tool-bar item at position X/Y on frame F.
6629 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6630 the current matrix of the tool-bar window of F, or NULL if not
6631 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
6632 item in F->current_tool_bar_items. Value is
06a2c219 6633
9ea173e8 6634 -1 if X/Y is not on a tool-bar item
06a2c219
GM
6635 0 if X/Y is on the same item that was highlighted before.
6636 1 otherwise. */
6637
6638static int
9ea173e8 6639x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
06a2c219
GM
6640 struct frame *f;
6641 int x, y;
6642 struct glyph **glyph;
6643 int *hpos, *vpos, *prop_idx;
6644{
6645 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6646 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6647 int area;
6648
6649 /* Find the glyph under X/Y. */
6650 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6651 if (*glyph == NULL)
6652 return -1;
6653
9ea173e8
GM
6654 /* Get the start of this tool-bar item's properties in
6655 f->current_tool_bar_items. */
6656 if (!tool_bar_item_info (f, *glyph, prop_idx))
06a2c219
GM
6657 return -1;
6658
6659 /* Is mouse on the highlighted item? */
9ea173e8 6660 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
06a2c219
GM
6661 && *vpos >= dpyinfo->mouse_face_beg_row
6662 && *vpos <= dpyinfo->mouse_face_end_row
6663 && (*vpos > dpyinfo->mouse_face_beg_row
6664 || *hpos >= dpyinfo->mouse_face_beg_col)
6665 && (*vpos < dpyinfo->mouse_face_end_row
6666 || *hpos < dpyinfo->mouse_face_end_col
6667 || dpyinfo->mouse_face_past_end))
6668 return 0;
6669
6670 return 1;
6671}
6672
6673
9ea173e8 6674/* Handle mouse button event on the tool-bar of frame F, at
06a2c219
GM
6675 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6676 or ButtonRelase. */
6677
6678static void
9ea173e8 6679x_handle_tool_bar_click (f, button_event)
06a2c219
GM
6680 struct frame *f;
6681 XButtonEvent *button_event;
6682{
6683 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 6684 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
6685 int hpos, vpos, prop_idx;
6686 struct glyph *glyph;
6687 Lisp_Object enabled_p;
6688 int x = button_event->x;
6689 int y = button_event->y;
6690
9ea173e8 6691 /* If not on the highlighted tool-bar item, return. */
06a2c219 6692 frame_to_window_pixel_xy (w, &x, &y);
9ea173e8 6693 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
06a2c219
GM
6694 return;
6695
6696 /* If item is disabled, do nothing. */
9ea173e8
GM
6697 enabled_p = (XVECTOR (f->current_tool_bar_items)
6698 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
06a2c219
GM
6699 if (NILP (enabled_p))
6700 return;
6701
6702 if (button_event->type == ButtonPress)
6703 {
6704 /* Show item in pressed state. */
6705 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6706 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9ea173e8 6707 last_tool_bar_item = prop_idx;
06a2c219
GM
6708 }
6709 else
6710 {
6711 Lisp_Object key, frame;
6712 struct input_event event;
6713
6714 /* Show item in released state. */
6715 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6716 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6717
9ea173e8
GM
6718 key = (XVECTOR (f->current_tool_bar_items)
6719 ->contents[prop_idx + TOOL_BAR_ITEM_KEY]);
06a2c219
GM
6720
6721 XSETFRAME (frame, f);
9ea173e8
GM
6722 event.kind = TOOL_BAR_EVENT;
6723 event.frame_or_window = Fcons (frame, Fcons (Qtool_bar, Qnil));
06a2c219
GM
6724 kbd_buffer_store_event (&event);
6725
9ea173e8 6726 event.kind = TOOL_BAR_EVENT;
06a2c219
GM
6727 event.frame_or_window = Fcons (frame, key);
6728 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6729 button_event->state);
6730 kbd_buffer_store_event (&event);
9ea173e8 6731 last_tool_bar_item = -1;
06a2c219
GM
6732 }
6733}
6734
6735
9ea173e8
GM
6736/* Possibly highlight a tool-bar item on frame F when mouse moves to
6737 tool-bar window-relative coordinates X/Y. Called from
06a2c219
GM
6738 note_mouse_highlight. */
6739
6740static void
9ea173e8 6741note_tool_bar_highlight (f, x, y)
06a2c219
GM
6742 struct frame *f;
6743 int x, y;
6744{
9ea173e8 6745 Lisp_Object window = f->tool_bar_window;
06a2c219
GM
6746 struct window *w = XWINDOW (window);
6747 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6748 int hpos, vpos;
6749 struct glyph *glyph;
6750 struct glyph_row *row;
5c187dee 6751 int i;
06a2c219
GM
6752 Lisp_Object enabled_p;
6753 int prop_idx;
6754 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
5c187dee 6755 int mouse_down_p, rc;
06a2c219
GM
6756
6757 /* Function note_mouse_highlight is called with negative x(y
6758 values when mouse moves outside of the frame. */
6759 if (x <= 0 || y <= 0)
6760 {
6761 clear_mouse_face (dpyinfo);
6762 return;
6763 }
6764
9ea173e8 6765 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
06a2c219
GM
6766 if (rc < 0)
6767 {
9ea173e8 6768 /* Not on tool-bar item. */
06a2c219
GM
6769 clear_mouse_face (dpyinfo);
6770 return;
6771 }
6772 else if (rc == 0)
9ea173e8 6773 /* On same tool-bar item as before. */
06a2c219 6774 goto set_help_echo;
b8009dd1 6775
06a2c219
GM
6776 clear_mouse_face (dpyinfo);
6777
9ea173e8 6778 /* Mouse is down, but on different tool-bar item? */
06a2c219
GM
6779 mouse_down_p = (dpyinfo->grabbed
6780 && f == last_mouse_frame
6781 && FRAME_LIVE_P (f));
6782 if (mouse_down_p
9ea173e8 6783 && last_tool_bar_item != prop_idx)
06a2c219
GM
6784 return;
6785
6786 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
6787 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
6788
9ea173e8
GM
6789 /* If tool-bar item is not enabled, don't highlight it. */
6790 enabled_p = (XVECTOR (f->current_tool_bar_items)
6791 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
06a2c219
GM
6792 if (!NILP (enabled_p))
6793 {
6794 /* Compute the x-position of the glyph. In front and past the
6795 image is a space. We include this is the highlighted area. */
6796 row = MATRIX_ROW (w->current_matrix, vpos);
6797 for (i = x = 0; i < hpos; ++i)
6798 x += row->glyphs[TEXT_AREA][i].pixel_width;
6799
6800 /* Record this as the current active region. */
6801 dpyinfo->mouse_face_beg_col = hpos;
6802 dpyinfo->mouse_face_beg_row = vpos;
6803 dpyinfo->mouse_face_beg_x = x;
6804 dpyinfo->mouse_face_beg_y = row->y;
6805 dpyinfo->mouse_face_past_end = 0;
6806
6807 dpyinfo->mouse_face_end_col = hpos + 1;
6808 dpyinfo->mouse_face_end_row = vpos;
6809 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
6810 dpyinfo->mouse_face_end_y = row->y;
6811 dpyinfo->mouse_face_window = window;
9ea173e8 6812 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
06a2c219
GM
6813
6814 /* Display it as active. */
6815 show_mouse_face (dpyinfo, draw);
6816 dpyinfo->mouse_face_image_state = draw;
b8009dd1 6817 }
06a2c219
GM
6818
6819 set_help_echo:
6820
9ea173e8 6821 /* Set help_echo to a help string.to display for this tool-bar item.
06a2c219 6822 XTread_socket does the rest. */
9ea173e8
GM
6823 help_echo = (XVECTOR (f->current_tool_bar_items)
6824 ->contents[prop_idx + TOOL_BAR_ITEM_HELP]);
06a2c219 6825 if (!STRINGP (help_echo))
9ea173e8
GM
6826 help_echo = (XVECTOR (f->current_tool_bar_items)
6827 ->contents[prop_idx + TOOL_BAR_ITEM_CAPTION]);
b8009dd1 6828}
4d73d038 6829
06a2c219
GM
6830
6831\f
6832/* Find the glyph matrix position of buffer position POS in window W.
6833 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
6834 current glyphs must be up to date. If POS is above window start
6835 return (0, 0, 0, 0). If POS is after end of W, return end of
6836 last line in W. */
b8009dd1
RS
6837
6838static int
06a2c219
GM
6839fast_find_position (w, pos, hpos, vpos, x, y)
6840 struct window *w;
b8009dd1 6841 int pos;
06a2c219 6842 int *hpos, *vpos, *x, *y;
b8009dd1 6843{
b8009dd1 6844 int i;
bf1c0ba1 6845 int lastcol;
06a2c219
GM
6846 int maybe_next_line_p = 0;
6847 int line_start_position;
6848 int yb = window_text_bottom_y (w);
6849 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
6850 struct glyph_row *best_row = row;
6851 int row_vpos = 0, best_row_vpos = 0;
6852 int current_x;
6853
6854 while (row->y < yb)
b8009dd1 6855 {
06a2c219
GM
6856 if (row->used[TEXT_AREA])
6857 line_start_position = row->glyphs[TEXT_AREA]->charpos;
6858 else
6859 line_start_position = 0;
6860
6861 if (line_start_position > pos)
b8009dd1 6862 break;
77b68646
RS
6863 /* If the position sought is the end of the buffer,
6864 don't include the blank lines at the bottom of the window. */
06a2c219
GM
6865 else if (line_start_position == pos
6866 && pos == BUF_ZV (XBUFFER (w->buffer)))
77b68646 6867 {
06a2c219 6868 maybe_next_line_p = 1;
77b68646
RS
6869 break;
6870 }
06a2c219
GM
6871 else if (line_start_position > 0)
6872 {
6873 best_row = row;
6874 best_row_vpos = row_vpos;
6875 }
4b0bb6f3
GM
6876
6877 if (row->y + row->height >= yb)
6878 break;
06a2c219
GM
6879
6880 ++row;
6881 ++row_vpos;
b8009dd1 6882 }
06a2c219
GM
6883
6884 /* Find the right column within BEST_ROW. */
6885 lastcol = 0;
6886 current_x = best_row->x;
6887 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
bf1c0ba1 6888 {
06a2c219
GM
6889 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
6890 int charpos;
6891
6892 charpos = glyph->charpos;
6893 if (charpos == pos)
bf1c0ba1 6894 {
06a2c219
GM
6895 *hpos = i;
6896 *vpos = best_row_vpos;
6897 *x = current_x;
6898 *y = best_row->y;
bf1c0ba1
RS
6899 return 1;
6900 }
06a2c219 6901 else if (charpos > pos)
4d73d038 6902 break;
06a2c219
GM
6903 else if (charpos > 0)
6904 lastcol = i;
6905
6906 current_x += glyph->pixel_width;
bf1c0ba1 6907 }
b8009dd1 6908
77b68646
RS
6909 /* If we're looking for the end of the buffer,
6910 and we didn't find it in the line we scanned,
6911 use the start of the following line. */
06a2c219 6912 if (maybe_next_line_p)
77b68646 6913 {
06a2c219
GM
6914 ++best_row;
6915 ++best_row_vpos;
6916 lastcol = 0;
6917 current_x = best_row->x;
77b68646
RS
6918 }
6919
06a2c219
GM
6920 *vpos = best_row_vpos;
6921 *hpos = lastcol + 1;
6922 *x = current_x;
6923 *y = best_row->y;
b8009dd1
RS
6924 return 0;
6925}
6926
06a2c219 6927
b8009dd1
RS
6928/* Display the active region described by mouse_face_*
6929 in its mouse-face if HL > 0, in its normal face if HL = 0. */
6930
6931static void
06a2c219 6932show_mouse_face (dpyinfo, draw)
7a13e894 6933 struct x_display_info *dpyinfo;
06a2c219 6934 enum draw_glyphs_face draw;
b8009dd1 6935{
7a13e894 6936 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
06a2c219 6937 struct frame *f = XFRAME (WINDOW_FRAME (w));
b8009dd1 6938 int i;
06a2c219
GM
6939 int cursor_off_p = 0;
6940 struct cursor_pos saved_cursor;
6941
6942 saved_cursor = output_cursor;
6943
6944 /* If window is in the process of being destroyed, don't bother
6945 to do anything. */
6946 if (w->current_matrix == NULL)
6947 goto set_x_cursor;
6948
6949 /* Recognize when we are called to operate on rows that don't exist
6950 anymore. This can happen when a window is split. */
6951 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
6952 goto set_x_cursor;
6953
6954 set_output_cursor (&w->phys_cursor);
6955
6956 /* Note that mouse_face_beg_row etc. are window relative. */
6957 for (i = dpyinfo->mouse_face_beg_row;
6958 i <= dpyinfo->mouse_face_end_row;
6959 i++)
6960 {
6961 int start_hpos, end_hpos, start_x;
6962 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
6963
6964 /* Don't do anything if row doesn't have valid contents. */
6965 if (!row->enabled_p)
6966 continue;
6967
6968 /* For all but the first row, the highlight starts at column 0. */
6969 if (i == dpyinfo->mouse_face_beg_row)
6970 {
6971 start_hpos = dpyinfo->mouse_face_beg_col;
6972 start_x = dpyinfo->mouse_face_beg_x;
6973 }
6974 else
6975 {
6976 start_hpos = 0;
6977 start_x = 0;
6978 }
6979
6980 if (i == dpyinfo->mouse_face_end_row)
6981 end_hpos = dpyinfo->mouse_face_end_col;
6982 else
6983 end_hpos = row->used[TEXT_AREA];
6984
6985 /* If the cursor's in the text we are about to rewrite, turn the
6986 cursor off. */
6987 if (!w->pseudo_window_p
6988 && i == output_cursor.vpos
6989 && output_cursor.hpos >= start_hpos - 1
6990 && output_cursor.hpos <= end_hpos)
514e4681 6991 {
06a2c219
GM
6992 x_update_window_cursor (w, 0);
6993 cursor_off_p = 1;
514e4681 6994 }
b8009dd1 6995
06a2c219 6996 if (end_hpos > start_hpos)
54a91a0f 6997 x_draw_glyphs (w, start_x, row, TEXT_AREA,
66ac4b0e 6998 start_hpos, end_hpos, draw, NULL, NULL, 0);
b8009dd1
RS
6999 }
7000
514e4681 7001 /* If we turned the cursor off, turn it back on. */
06a2c219
GM
7002 if (cursor_off_p)
7003 x_display_cursor (w, 1,
7004 output_cursor.hpos, output_cursor.vpos,
7005 output_cursor.x, output_cursor.y);
2729a2b5 7006
06a2c219 7007 output_cursor = saved_cursor;
fb3b7de5 7008
06a2c219
GM
7009 set_x_cursor:
7010
7011 /* Change the mouse cursor. */
7012 if (draw == DRAW_NORMAL_TEXT)
7013 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7014 f->output_data.x->text_cursor);
7015 else if (draw == DRAW_MOUSE_FACE)
334208b7 7016 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 7017 f->output_data.x->cross_cursor);
27ead1d5 7018 else
334208b7 7019 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
06a2c219 7020 f->output_data.x->nontext_cursor);
b8009dd1
RS
7021}
7022
7023/* Clear out the mouse-highlighted active region.
06a2c219 7024 Redraw it un-highlighted first. */
b8009dd1 7025
06a2c219 7026void
7a13e894
RS
7027clear_mouse_face (dpyinfo)
7028 struct x_display_info *dpyinfo;
b8009dd1 7029{
06a2c219
GM
7030 if (tip_frame)
7031 return;
7032
7a13e894 7033 if (! NILP (dpyinfo->mouse_face_window))
06a2c219 7034 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
b8009dd1 7035
7a13e894
RS
7036 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7037 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7038 dpyinfo->mouse_face_window = Qnil;
b8009dd1 7039}
e687d06e
RS
7040
7041/* Just discard the mouse face information for frame F, if any.
7042 This is used when the size of F is changed. */
7043
dfcf069d 7044void
e687d06e
RS
7045cancel_mouse_face (f)
7046 FRAME_PTR f;
7047{
7048 Lisp_Object window;
7049 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7050
7051 window = dpyinfo->mouse_face_window;
7052 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7053 {
7054 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7055 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7056 dpyinfo->mouse_face_window = Qnil;
7057 }
7058}
b8009dd1 7059\f
ab648270
JB
7060static struct scroll_bar *x_window_to_scroll_bar ();
7061static void x_scroll_bar_report_motion ();
12ba150f 7062
90e65f07 7063/* Return the current position of the mouse.
2d7fc7e8 7064 *fp should be a frame which indicates which display to ask about.
90e65f07 7065
2d7fc7e8 7066 If the mouse movement started in a scroll bar, set *fp, *bar_window,
ab648270 7067 and *part to the frame, window, and scroll bar part that the mouse
12ba150f 7068 is over. Set *x and *y to the portion and whole of the mouse's
ab648270 7069 position on the scroll bar.
12ba150f 7070
2d7fc7e8 7071 If the mouse movement started elsewhere, set *fp to the frame the
12ba150f
JB
7072 mouse is on, *bar_window to nil, and *x and *y to the character cell
7073 the mouse is over.
7074
06a2c219 7075 Set *time to the server time-stamp for the time at which the mouse
12ba150f
JB
7076 was at this position.
7077
a135645a
RS
7078 Don't store anything if we don't have a valid set of values to report.
7079
90e65f07 7080 This clears the mouse_moved flag, so we can wait for the next mouse
f5bb65ec 7081 movement. */
90e65f07
JB
7082
7083static void
1cf412ec 7084XTmouse_position (fp, insist, bar_window, part, x, y, time)
334208b7 7085 FRAME_PTR *fp;
1cf412ec 7086 int insist;
12ba150f 7087 Lisp_Object *bar_window;
ab648270 7088 enum scroll_bar_part *part;
90e65f07 7089 Lisp_Object *x, *y;
e5d77022 7090 unsigned long *time;
90e65f07 7091{
a135645a
RS
7092 FRAME_PTR f1;
7093
90e65f07
JB
7094 BLOCK_INPUT;
7095
8bcee03e 7096 if (! NILP (last_mouse_scroll_bar) && insist == 0)
334208b7 7097 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
90e65f07
JB
7098 else
7099 {
12ba150f
JB
7100 Window root;
7101 int root_x, root_y;
90e65f07 7102
12ba150f
JB
7103 Window dummy_window;
7104 int dummy;
7105
39d8bb4d
KH
7106 Lisp_Object frame, tail;
7107
7108 /* Clear the mouse-moved flag for every frame on this display. */
7109 FOR_EACH_FRAME (tail, frame)
7110 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7111 XFRAME (frame)->mouse_moved = 0;
7112
ab648270 7113 last_mouse_scroll_bar = Qnil;
12ba150f
JB
7114
7115 /* Figure out which root window we're on. */
334208b7
RS
7116 XQueryPointer (FRAME_X_DISPLAY (*fp),
7117 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
12ba150f
JB
7118
7119 /* The root window which contains the pointer. */
7120 &root,
7121
7122 /* Trash which we can't trust if the pointer is on
7123 a different screen. */
7124 &dummy_window,
7125
7126 /* The position on that root window. */
58769bee 7127 &root_x, &root_y,
12ba150f
JB
7128
7129 /* More trash we can't trust. */
7130 &dummy, &dummy,
7131
7132 /* Modifier keys and pointer buttons, about which
7133 we don't care. */
7134 (unsigned int *) &dummy);
7135
7136 /* Now we have a position on the root; find the innermost window
7137 containing the pointer. */
7138 {
7139 Window win, child;
7140 int win_x, win_y;
06a2c219 7141 int parent_x = 0, parent_y = 0;
e99db5a1 7142 int count;
12ba150f
JB
7143
7144 win = root;
69388238 7145
2d7fc7e8
RS
7146 /* XTranslateCoordinates can get errors if the window
7147 structure is changing at the same time this function
7148 is running. So at least we must not crash from them. */
7149
e99db5a1 7150 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
2d7fc7e8 7151
334208b7 7152 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
23faf38f 7153 && FRAME_LIVE_P (last_mouse_frame))
12ba150f 7154 {
69388238
RS
7155 /* If mouse was grabbed on a frame, give coords for that frame
7156 even if the mouse is now outside it. */
334208b7 7157 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
69388238 7158
12ba150f 7159 /* From-window, to-window. */
69388238 7160 root, FRAME_X_WINDOW (last_mouse_frame),
12ba150f
JB
7161
7162 /* From-position, to-position. */
7163 root_x, root_y, &win_x, &win_y,
7164
7165 /* Child of win. */
7166 &child);
69388238
RS
7167 f1 = last_mouse_frame;
7168 }
7169 else
7170 {
7171 while (1)
7172 {
334208b7 7173 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
12ba150f 7174
69388238
RS
7175 /* From-window, to-window. */
7176 root, win,
12ba150f 7177
69388238
RS
7178 /* From-position, to-position. */
7179 root_x, root_y, &win_x, &win_y,
7180
7181 /* Child of win. */
7182 &child);
7183
9af3143a 7184 if (child == None || child == win)
69388238
RS
7185 break;
7186
7187 win = child;
7188 parent_x = win_x;
7189 parent_y = win_y;
7190 }
12ba150f 7191
69388238
RS
7192 /* Now we know that:
7193 win is the innermost window containing the pointer
7194 (XTC says it has no child containing the pointer),
7195 win_x and win_y are the pointer's position in it
7196 (XTC did this the last time through), and
7197 parent_x and parent_y are the pointer's position in win's parent.
7198 (They are what win_x and win_y were when win was child.
7199 If win is the root window, it has no parent, and
7200 parent_{x,y} are invalid, but that's okay, because we'll
7201 never use them in that case.) */
7202
7203 /* Is win one of our frames? */
19126e11 7204 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
69388238 7205 }
58769bee 7206
2d7fc7e8
RS
7207 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7208 f1 = 0;
7209
e99db5a1 7210 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
2d7fc7e8 7211
ab648270 7212 /* If not, is it one of our scroll bars? */
a135645a 7213 if (! f1)
12ba150f 7214 {
ab648270 7215 struct scroll_bar *bar = x_window_to_scroll_bar (win);
12ba150f
JB
7216
7217 if (bar)
7218 {
a135645a 7219 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
7220 win_x = parent_x;
7221 win_y = parent_y;
7222 }
7223 }
90e65f07 7224
8bcee03e 7225 if (f1 == 0 && insist > 0)
b86bd3dd 7226 f1 = SELECTED_FRAME ();
1cf412ec 7227
a135645a 7228 if (f1)
12ba150f 7229 {
06a2c219
GM
7230 /* Ok, we found a frame. Store all the values.
7231 last_mouse_glyph is a rectangle used to reduce the
7232 generation of mouse events. To not miss any motion
7233 events, we must divide the frame into rectangles of the
7234 size of the smallest character that could be displayed
7235 on it, i.e. into the same rectangles that matrices on
7236 the frame are divided into. */
7237
7238#if OLD_REDISPLAY_CODE
2b5c9e71 7239 int ignore1, ignore2;
2b5c9e71 7240 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
334208b7 7241 &last_mouse_glyph,
1cf412ec
RS
7242 FRAME_X_DISPLAY_INFO (f1)->grabbed
7243 || insist);
06a2c219
GM
7244#else
7245 {
7246 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7247 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7248 int x = win_x;
7249 int y = win_y;
7250
7251 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7252 round down even for negative values. */
7253 if (x < 0)
7254 x -= width - 1;
7255 if (y < 0)
7256 y -= height - 1;
7257
7258 last_mouse_glyph.width = width;
7259 last_mouse_glyph.height = height;
7260 last_mouse_glyph.x = (x + width - 1) / width * width;
7261 last_mouse_glyph.y = (y + height - 1) / height * height;
7262 }
7263#endif
12ba150f
JB
7264
7265 *bar_window = Qnil;
7266 *part = 0;
334208b7 7267 *fp = f1;
e0c1aef2
KH
7268 XSETINT (*x, win_x);
7269 XSETINT (*y, win_y);
12ba150f
JB
7270 *time = last_mouse_movement_time;
7271 }
7272 }
7273 }
90e65f07
JB
7274
7275 UNBLOCK_INPUT;
7276}
f451eb13 7277
06a2c219 7278
06a2c219 7279#ifdef USE_X_TOOLKIT
bffcfca9
GM
7280
7281/* Atimer callback function for TIMER. Called every 0.1s to process
7282 Xt timeouts, if needed. We must avoid calling XtAppPending as
7283 much as possible because that function does an implicit XFlush
7284 that slows us down. */
7285
7286static void
7287x_process_timeouts (timer)
7288 struct atimer *timer;
7289{
7290 if (toolkit_scroll_bar_interaction || popup_activated_flag)
7291 {
7292 BLOCK_INPUT;
7293 while (XtAppPending (Xt_app_con) & XtIMTimer)
7294 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7295 UNBLOCK_INPUT;
7296 }
06a2c219
GM
7297}
7298
bffcfca9 7299#endif /* USE_X_TOOLKIT */
06a2c219
GM
7300
7301\f
7302/* Scroll bar support. */
7303
7304/* Given an X window ID, find the struct scroll_bar which manages it.
7305 This can be called in GC, so we have to make sure to strip off mark
7306 bits. */
bffcfca9 7307
06a2c219
GM
7308static struct scroll_bar *
7309x_window_to_scroll_bar (window_id)
7310 Window window_id;
7311{
7312 Lisp_Object tail;
7313
7314 for (tail = Vframe_list;
7315 XGCTYPE (tail) == Lisp_Cons;
8e713be6 7316 tail = XCDR (tail))
06a2c219
GM
7317 {
7318 Lisp_Object frame, bar, condemned;
7319
8e713be6 7320 frame = XCAR (tail);
06a2c219
GM
7321 /* All elements of Vframe_list should be frames. */
7322 if (! GC_FRAMEP (frame))
7323 abort ();
7324
7325 /* Scan this frame's scroll bar list for a scroll bar with the
7326 right window ID. */
7327 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7328 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7329 /* This trick allows us to search both the ordinary and
7330 condemned scroll bar lists with one loop. */
7331 ! GC_NILP (bar) || (bar = condemned,
7332 condemned = Qnil,
7333 ! GC_NILP (bar));
7334 bar = XSCROLL_BAR (bar)->next)
7335 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7336 return XSCROLL_BAR (bar);
7337 }
7338
7339 return 0;
7340}
7341
7342
7343\f
7344/************************************************************************
7345 Toolkit scroll bars
7346 ************************************************************************/
7347
7348#if USE_TOOLKIT_SCROLL_BARS
7349
7350static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7351static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7352static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7353 struct scroll_bar *));
7354static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7355 int, int, int));
7356
7357
7358/* Id of action hook installed for scroll bars. */
7359
7360static XtActionHookId action_hook_id;
7361
7362/* Lisp window being scrolled. Set when starting to interact with
7363 a toolkit scroll bar, reset to nil when ending the interaction. */
7364
7365static Lisp_Object window_being_scrolled;
7366
7367/* Last scroll bar part sent in xm_scroll_callback. */
7368
7369static int last_scroll_bar_part;
7370
ec18280f
SM
7371/* Whether this is an Xaw with arrow-scrollbars. This should imply
7372 that movements of 1/20 of the screen size are mapped to up/down. */
7373
7374static Boolean xaw3d_arrow_scroll;
7375
7376/* Whether the drag scrolling maintains the mouse at the top of the
7377 thumb. If not, resizing the thumb needs to be done more carefully
7378 to avoid jerkyness. */
7379
7380static Boolean xaw3d_pick_top;
7381
06a2c219
GM
7382
7383/* Action hook installed via XtAppAddActionHook when toolkit scroll
ec18280f 7384 bars are used.. The hook is responsible for detecting when
06a2c219
GM
7385 the user ends an interaction with the scroll bar, and generates
7386 a `end-scroll' scroll_bar_click' event if so. */
7387
7388static void
7389xt_action_hook (widget, client_data, action_name, event, params,
7390 num_params)
7391 Widget widget;
7392 XtPointer client_data;
7393 String action_name;
7394 XEvent *event;
7395 String *params;
7396 Cardinal *num_params;
7397{
7398 int scroll_bar_p;
7399 char *end_action;
7400
7401#ifdef USE_MOTIF
7402 scroll_bar_p = XmIsScrollBar (widget);
7403 end_action = "Release";
ec18280f 7404#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
7405 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7406 end_action = "EndScroll";
ec18280f 7407#endif /* USE_MOTIF */
06a2c219 7408
06a2c219
GM
7409 if (scroll_bar_p
7410 && strcmp (action_name, end_action) == 0
7411 && WINDOWP (window_being_scrolled))
7412 {
7413 struct window *w;
7414
7415 x_send_scroll_bar_event (window_being_scrolled,
7416 scroll_bar_end_scroll, 0, 0);
7417 w = XWINDOW (window_being_scrolled);
7418 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7419 window_being_scrolled = Qnil;
7420 last_scroll_bar_part = -1;
bffcfca9
GM
7421
7422 /* Xt timeouts no longer needed. */
7423 toolkit_scroll_bar_interaction = 0;
06a2c219
GM
7424 }
7425}
7426
7427
7428/* Send a client message with message type Xatom_Scrollbar for a
7429 scroll action to the frame of WINDOW. PART is a value identifying
7430 the part of the scroll bar that was clicked on. PORTION is the
7431 amount to scroll of a whole of WHOLE. */
7432
7433static void
7434x_send_scroll_bar_event (window, part, portion, whole)
7435 Lisp_Object window;
7436 int part, portion, whole;
7437{
7438 XEvent event;
7439 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
7440 struct frame *f = XFRAME (XWINDOW (window)->frame);
7441
7442 /* Construct a ClientMessage event to send to the frame. */
7443 ev->type = ClientMessage;
7444 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7445 ev->display = FRAME_X_DISPLAY (f);
7446 ev->window = FRAME_X_WINDOW (f);
7447 ev->format = 32;
52e386c2 7448 ev->data.l[0] = (long) XFASTINT (window);
06a2c219
GM
7449 ev->data.l[1] = (long) part;
7450 ev->data.l[2] = (long) 0;
7451 ev->data.l[3] = (long) portion;
7452 ev->data.l[4] = (long) whole;
7453
bffcfca9
GM
7454 /* Make Xt timeouts work while the scroll bar is active. */
7455 toolkit_scroll_bar_interaction = 1;
7456
06a2c219
GM
7457 /* Setting the event mask to zero means that the message will
7458 be sent to the client that created the window, and if that
7459 window no longer exists, no event will be sent. */
7460 BLOCK_INPUT;
7461 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7462 UNBLOCK_INPUT;
7463}
7464
7465
7466/* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7467 in *IEVENT. */
7468
7469static void
7470x_scroll_bar_to_input_event (event, ievent)
7471 XEvent *event;
7472 struct input_event *ievent;
7473{
7474 XClientMessageEvent *ev = (XClientMessageEvent *) event;
52e386c2
KR
7475 Lisp_Object window;
7476 struct frame *f;
7477
7478 XSETFASTINT (window, ev->data.l[0]);
7479 f = XFRAME (XWINDOW (window)->frame);
06a2c219
GM
7480
7481 ievent->kind = scroll_bar_click;
7482 ievent->frame_or_window = window;
7483 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7484 ievent->part = ev->data.l[1];
7485 ievent->code = ev->data.l[2];
7486 ievent->x = make_number ((int) ev->data.l[3]);
7487 ievent->y = make_number ((int) ev->data.l[4]);
7488 ievent->modifiers = 0;
7489}
7490
7491
7492#ifdef USE_MOTIF
7493
7494/* Minimum and maximum values used for Motif scroll bars. */
7495
7496#define XM_SB_MIN 1
7497#define XM_SB_MAX 10000000
7498#define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7499
7500
7501/* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7502 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7503 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7504
7505static void
7506xm_scroll_callback (widget, client_data, call_data)
7507 Widget widget;
7508 XtPointer client_data, call_data;
7509{
7510 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7511 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7512 double percent;
7513 int part = -1, whole = 0, portion = 0;
7514
7515 switch (cs->reason)
7516 {
7517 case XmCR_DECREMENT:
7518 bar->dragging = Qnil;
7519 part = scroll_bar_up_arrow;
7520 break;
7521
7522 case XmCR_INCREMENT:
7523 bar->dragging = Qnil;
7524 part = scroll_bar_down_arrow;
7525 break;
7526
7527 case XmCR_PAGE_DECREMENT:
7528 bar->dragging = Qnil;
7529 part = scroll_bar_above_handle;
7530 break;
7531
7532 case XmCR_PAGE_INCREMENT:
7533 bar->dragging = Qnil;
7534 part = scroll_bar_below_handle;
7535 break;
7536
7537 case XmCR_TO_TOP:
7538 bar->dragging = Qnil;
7539 part = scroll_bar_to_top;
7540 break;
7541
7542 case XmCR_TO_BOTTOM:
7543 bar->dragging = Qnil;
7544 part = scroll_bar_to_bottom;
7545 break;
7546
7547 case XmCR_DRAG:
7548 {
7549 int slider_size;
7550 int dragging_down_p = (INTEGERP (bar->dragging)
7551 && XINT (bar->dragging) <= cs->value);
7552
7553 /* Get the slider size. */
7554 BLOCK_INPUT;
7555 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7556 UNBLOCK_INPUT;
7557
7558 /* At the max position of the scroll bar, do a line-wise
7559 movement. Without doing anything, the LessTif scroll bar
7560 calls us with the same cs->value again and again. If we
7561 want to make sure that we can reach the end of the buffer,
7562 we have to do something.
7563
7564 Implementation note: setting bar->dragging always to
7565 cs->value gives a smoother movement at the max position.
7566 Setting it to nil when doing line-wise movement gives
7567 a better slider behavior. */
7568
7569 if (cs->value + slider_size == XM_SB_MAX
7570 || (dragging_down_p
7571 && last_scroll_bar_part == scroll_bar_down_arrow))
7572 {
7573 part = scroll_bar_down_arrow;
7574 bar->dragging = Qnil;
7575 }
7576 else
7577 {
7578 whole = XM_SB_RANGE;
7579 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7580 part = scroll_bar_handle;
7581 bar->dragging = make_number (cs->value);
7582 }
7583 }
7584 break;
7585
7586 case XmCR_VALUE_CHANGED:
7587 break;
7588 };
7589
7590 if (part >= 0)
7591 {
7592 window_being_scrolled = bar->window;
7593 last_scroll_bar_part = part;
7594 x_send_scroll_bar_event (bar->window, part, portion, whole);
7595 }
7596}
7597
7598
ec18280f 7599#else /* !USE_MOTIF, i.e. Xaw. */
06a2c219
GM
7600
7601
ec18280f 7602/* Xaw scroll bar callback. Invoked when the thumb is dragged.
06a2c219
GM
7603 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7604 scroll bar struct. CALL_DATA is a pointer to a float saying where
7605 the thumb is. */
7606
7607static void
ec18280f 7608xaw_jump_callback (widget, client_data, call_data)
06a2c219
GM
7609 Widget widget;
7610 XtPointer client_data, call_data;
7611{
7612 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7613 float top = *(float *) call_data;
7614 float shown;
ec18280f
SM
7615 int whole, portion, height;
7616 int part;
06a2c219
GM
7617
7618 /* Get the size of the thumb, a value between 0 and 1. */
7619 BLOCK_INPUT;
ec18280f 7620 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
06a2c219
GM
7621 UNBLOCK_INPUT;
7622
7623 whole = 10000000;
7624 portion = shown < 1 ? top * whole : 0;
06a2c219 7625
ec18280f
SM
7626 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
7627 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
7628 the bottom, so we force the scrolling whenever we see that we're
7629 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
7630 we try to ensure that we always stay two pixels away from the
7631 bottom). */
06a2c219
GM
7632 part = scroll_bar_down_arrow;
7633 else
7634 part = scroll_bar_handle;
7635
7636 window_being_scrolled = bar->window;
7637 bar->dragging = make_number (portion);
7638 last_scroll_bar_part = part;
7639 x_send_scroll_bar_event (bar->window, part, portion, whole);
7640}
7641
7642
ec18280f
SM
7643/* Xaw scroll bar callback. Invoked for incremental scrolling.,
7644 i.e. line or page up or down. WIDGET is the Xaw scroll bar
06a2c219
GM
7645 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
7646 the scroll bar. CALL_DATA is an integer specifying the action that
7647 has taken place. It's magnitude is in the range 0..height of the
7648 scroll bar. Negative values mean scroll towards buffer start.
7649 Values < height of scroll bar mean line-wise movement. */
7650
7651static void
ec18280f 7652xaw_scroll_callback (widget, client_data, call_data)
06a2c219
GM
7653 Widget widget;
7654 XtPointer client_data, call_data;
7655{
7656 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7657 int position = (int) call_data;
7658 Dimension height;
7659 int part;
7660
7661 /* Get the height of the scroll bar. */
7662 BLOCK_INPUT;
7663 XtVaGetValues (widget, XtNheight, &height, NULL);
7664 UNBLOCK_INPUT;
7665
ec18280f
SM
7666 if (abs (position) >= height)
7667 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
7668
7669 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
7670 it maps line-movement to call_data = max(5, height/20). */
7671 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
7672 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
06a2c219 7673 else
ec18280f 7674 part = scroll_bar_move_ratio;
06a2c219
GM
7675
7676 window_being_scrolled = bar->window;
7677 bar->dragging = Qnil;
7678 last_scroll_bar_part = part;
ec18280f 7679 x_send_scroll_bar_event (bar->window, part, position, height);
06a2c219
GM
7680}
7681
7682
7683#endif /* not USE_MOTIF */
7684
7685
7686/* Create the widget for scroll bar BAR on frame F. Record the widget
7687 and X window of the scroll bar in BAR. */
7688
7689static void
7690x_create_toolkit_scroll_bar (f, bar)
7691 struct frame *f;
7692 struct scroll_bar *bar;
7693{
7694 Window xwindow;
7695 Widget widget;
7696 Arg av[20];
7697 int ac = 0;
7698 char *scroll_bar_name = "verticalScrollBar";
7699 unsigned long pixel;
7700
7701 BLOCK_INPUT;
7702
7703#ifdef USE_MOTIF
7704 /* LessTif 0.85, problems:
7705
7706 1. When the mouse if over the scroll bar, the scroll bar will
7707 get keyboard events. I didn't find a way to turn this off.
7708
7709 2. Do we have to explicitly set the cursor to get an arrow
7710 cursor (see below)? */
7711
7712 /* Set resources. Create the widget. */
7713 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7714 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
7715 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
7716 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
7717 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
7718 XtSetArg (av[ac], XmNincrement, 1); ++ac;
7719 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
7720
7721 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7722 if (pixel != -1)
7723 {
7724 XtSetArg (av[ac], XmNforeground, pixel);
7725 ++ac;
7726 }
7727
7728 pixel = f->output_data.x->scroll_bar_background_pixel;
7729 if (pixel != -1)
7730 {
7731 XtSetArg (av[ac], XmNbackground, pixel);
7732 ++ac;
7733 }
7734
7735 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
7736 scroll_bar_name, av, ac);
7737
7738 /* Add one callback for everything that can happen. */
7739 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
7740 (XtPointer) bar);
7741 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
7742 (XtPointer) bar);
7743 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
7744 (XtPointer) bar);
7745 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
7746 (XtPointer) bar);
7747 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
7748 (XtPointer) bar);
7749 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
7750 (XtPointer) bar);
7751 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
7752 (XtPointer) bar);
7753
7754 /* Realize the widget. Only after that is the X window created. */
7755 XtRealizeWidget (widget);
7756
7757 /* Set the cursor to an arrow. I didn't find a resource to do that.
7758 And I'm wondering why it hasn't an arrow cursor by default. */
7759 XDefineCursor (XtDisplay (widget), XtWindow (widget),
7760 f->output_data.x->nontext_cursor);
7761
ec18280f 7762#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
7763
7764 /* Set resources. Create the widget. The background of the
7765 Xaw3d scroll bar widget is a little bit light for my taste.
7766 We don't alter it here to let users change it according
7767 to their taste with `emacs*verticalScrollBar.background: xxx'. */
7768 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7769 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
ec18280f
SM
7770 /* For smoother scrolling with Xaw3d -sm */
7771 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
7772 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
06a2c219
GM
7773
7774 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7775 if (pixel != -1)
7776 {
7777 XtSetArg (av[ac], XtNforeground, pixel);
7778 ++ac;
7779 }
7780
7781 pixel = f->output_data.x->scroll_bar_background_pixel;
7782 if (pixel != -1)
7783 {
7784 XtSetArg (av[ac], XtNbackground, pixel);
7785 ++ac;
7786 }
7787
7788 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
7789 f->output_data.x->edit_widget, av, ac);
ec18280f
SM
7790
7791 {
7792 char *initial = "";
7793 char *val = initial;
7794 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
7795 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
7796 if (val == initial)
7797 { /* ARROW_SCROLL */
7798 xaw3d_arrow_scroll = True;
7799 /* Isn't that just a personal preference ? -sm */
7800 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
7801 }
7802 }
06a2c219
GM
7803
7804 /* Define callbacks. */
ec18280f
SM
7805 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
7806 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
06a2c219
GM
7807 (XtPointer) bar);
7808
7809 /* Realize the widget. Only after that is the X window created. */
7810 XtRealizeWidget (widget);
7811
ec18280f 7812#endif /* !USE_MOTIF */
06a2c219
GM
7813
7814 /* Install an action hook that let's us detect when the user
7815 finishes interacting with a scroll bar. */
7816 if (action_hook_id == 0)
7817 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
7818
7819 /* Remember X window and widget in the scroll bar vector. */
7820 SET_SCROLL_BAR_X_WIDGET (bar, widget);
7821 xwindow = XtWindow (widget);
7822 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
7823
7824 UNBLOCK_INPUT;
7825}
7826
7827
7828/* Set the thumb size and position of scroll bar BAR. We are currently
7829 displaying PORTION out of a whole WHOLE, and our position POSITION. */
7830
7831static void
7832x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
7833 struct scroll_bar *bar;
7834 int portion, position, whole;
f451eb13 7835{
06a2c219 7836 float top, shown;
06a2c219 7837 Widget widget = SCROLL_BAR_X_WIDGET (bar);
f451eb13 7838
06a2c219
GM
7839 if (whole == 0)
7840 top = 0, shown = 1;
7841 else
f451eb13 7842 {
06a2c219
GM
7843 top = (float) position / whole;
7844 shown = (float) portion / whole;
7845 }
f451eb13 7846
06a2c219 7847 BLOCK_INPUT;
f451eb13 7848
06a2c219
GM
7849#ifdef USE_MOTIF
7850 {
7851 int size, value;
7852 Boolean arrow1_selected, arrow2_selected;
7853 unsigned char flags;
7854 XmScrollBarWidget sb;
7855
ec18280f 7856 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
06a2c219
GM
7857 is the scroll bar's maximum and MIN is the scroll bar's minimum
7858 value. */
7859 size = shown * XM_SB_RANGE;
7860 size = min (size, XM_SB_RANGE);
7861 size = max (size, 1);
7862
7863 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
7864 value = top * XM_SB_RANGE;
7865 value = min (value, XM_SB_MAX - size);
7866 value = max (value, XM_SB_MIN);
7867
7868 /* LessTif: Calling XmScrollBarSetValues after an increment or
7869 decrement turns off auto-repeat LessTif-internally. This can
7870 be seen in ScrollBar.c which resets Arrow1Selected and
7871 Arrow2Selected. It also sets internal flags so that LessTif
7872 believes the mouse is in the slider. We either have to change
7873 our code, or work around that by accessing private data. */
7874
7875 sb = (XmScrollBarWidget) widget;
7876 arrow1_selected = sb->scrollBar.arrow1_selected;
7877 arrow2_selected = sb->scrollBar.arrow2_selected;
7878 flags = sb->scrollBar.flags;
7879
7880 if (NILP (bar->dragging))
7881 XmScrollBarSetValues (widget, value, size, 0, 0, False);
7882 else if (last_scroll_bar_part == scroll_bar_down_arrow)
7883 /* This has the negative side effect that the slider value is
ec18280f 7884 not what it would be if we scrolled here using line-wise or
06a2c219
GM
7885 page-wise movement. */
7886 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
7887 else
7888 {
7889 /* If currently dragging, only update the slider size.
7890 This reduces flicker effects. */
7891 int old_value, old_size, increment, page_increment;
7892
7893 XmScrollBarGetValues (widget, &old_value, &old_size,
7894 &increment, &page_increment);
7895 XmScrollBarSetValues (widget, old_value,
7896 min (size, XM_SB_RANGE - old_value),
7897 0, 0, False);
7898 }
7899
7900 sb->scrollBar.arrow1_selected = arrow1_selected;
7901 sb->scrollBar.arrow2_selected = arrow2_selected;
7902 sb->scrollBar.flags = flags;
7903 }
ec18280f 7904#else /* !USE_MOTIF i.e. use Xaw */
06a2c219 7905 {
ec18280f
SM
7906 float old_top, old_shown;
7907 Dimension height;
7908 XtVaGetValues (widget,
7909 XtNtopOfThumb, &old_top,
7910 XtNshown, &old_shown,
7911 XtNheight, &height,
7912 NULL);
7913
7914 /* Massage the top+shown values. */
7915 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
7916 top = max (0, min (1, top));
7917 else
7918 top = old_top;
7919 /* Keep two pixels available for moving the thumb down. */
7920 shown = max (0, min (1 - top - (2.0 / height), shown));
06a2c219
GM
7921
7922 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
7923 check that your system's configuration file contains a define
7924 for `NARROWPROTO'. See s/freebsd.h for an example. */
ec18280f 7925 if (top != old_top || shown != old_shown)
eb393530 7926 {
ec18280f 7927 if (NILP (bar->dragging))
eb393530 7928 XawScrollbarSetThumb (widget, top, shown);
06a2c219
GM
7929 else
7930 {
ec18280f
SM
7931#ifdef HAVE_XAW3D
7932 ScrollbarWidget sb = (ScrollbarWidget) widget;
3e71d8f2 7933 int scroll_mode = 0;
ec18280f
SM
7934
7935 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
7936 if (xaw3d_arrow_scroll)
7937 {
7938 /* Xaw3d stupidly ignores resize requests while dragging
7939 so we have to make it believe it's not in dragging mode. */
7940 scroll_mode = sb->scrollbar.scroll_mode;
7941 if (scroll_mode == 2)
7942 sb->scrollbar.scroll_mode = 0;
7943 }
7944#endif
7945 /* Try to make the scrolling a tad smoother. */
7946 if (!xaw3d_pick_top)
7947 shown = min (shown, old_shown);
7948
7949 XawScrollbarSetThumb (widget, top, shown);
7950
7951#ifdef HAVE_XAW3D
7952 if (xaw3d_arrow_scroll && scroll_mode == 2)
7953 sb->scrollbar.scroll_mode = scroll_mode;
7954#endif
06a2c219 7955 }
06a2c219
GM
7956 }
7957 }
ec18280f 7958#endif /* !USE_MOTIF */
06a2c219
GM
7959
7960 UNBLOCK_INPUT;
f451eb13
JB
7961}
7962
06a2c219
GM
7963#endif /* USE_TOOLKIT_SCROLL_BARS */
7964
7965
7966\f
7967/************************************************************************
7968 Scroll bars, general
7969 ************************************************************************/
7970
7971/* Create a scroll bar and return the scroll bar vector for it. W is
7972 the Emacs window on which to create the scroll bar. TOP, LEFT,
7973 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
7974 scroll bar. */
7975
ab648270 7976static struct scroll_bar *
06a2c219
GM
7977x_scroll_bar_create (w, top, left, width, height)
7978 struct window *w;
f451eb13
JB
7979 int top, left, width, height;
7980{
06a2c219 7981 struct frame *f = XFRAME (w->frame);
334208b7
RS
7982 struct scroll_bar *bar
7983 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
f451eb13
JB
7984
7985 BLOCK_INPUT;
7986
06a2c219
GM
7987#if USE_TOOLKIT_SCROLL_BARS
7988 x_create_toolkit_scroll_bar (f, bar);
7989#else /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
7990 {
7991 XSetWindowAttributes a;
7992 unsigned long mask;
5c187dee 7993 Window window;
06a2c219
GM
7994
7995 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
7996 if (a.background_pixel == -1)
7997 a.background_pixel = f->output_data.x->background_pixel;
7998
12ba150f 7999 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9a572e2a 8000 | ButtonMotionMask | PointerMotionHintMask
12ba150f 8001 | ExposureMask);
7a13e894 8002 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
f451eb13 8003
dbc4e1c1 8004 mask = (CWBackPixel | CWEventMask | CWCursor);
f451eb13 8005
06a2c219
GM
8006 /* Clear the area of W that will serve as a scroll bar. This is
8007 for the case that a window has been split horizontally. In
8008 this case, no clear_frame is generated to reduce flickering. */
8009 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8010 left, top, width,
8011 window_box_height (w), False);
8012
8013 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8014 /* Position and size of scroll bar. */
8015 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8016 top,
8017 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8018 height,
8019 /* Border width, depth, class, and visual. */
8020 0,
8021 CopyFromParent,
8022 CopyFromParent,
8023 CopyFromParent,
8024 /* Attributes. */
8025 mask, &a);
8026 SET_SCROLL_BAR_X_WINDOW (bar, window);
f451eb13 8027 }
06a2c219 8028#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 8029
06a2c219 8030 XSETWINDOW (bar->window, w);
e0c1aef2
KH
8031 XSETINT (bar->top, top);
8032 XSETINT (bar->left, left);
8033 XSETINT (bar->width, width);
8034 XSETINT (bar->height, height);
8035 XSETINT (bar->start, 0);
8036 XSETINT (bar->end, 0);
12ba150f 8037 bar->dragging = Qnil;
f451eb13
JB
8038
8039 /* Add bar to its frame's list of scroll bars. */
334208b7 8040 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 8041 bar->prev = Qnil;
334208b7 8042 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
06a2c219 8043 if (!NILP (bar->next))
e0c1aef2 8044 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13 8045
06a2c219
GM
8046 /* Map the window/widget. */
8047#if USE_TOOLKIT_SCROLL_BARS
8048 XtMapWidget (SCROLL_BAR_X_WIDGET (bar));
8049 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
8050 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8051 top,
8052 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8053 height, 0);
8054#else /* not USE_TOOLKIT_SCROLL_BARS */
7f9c7f94 8055 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
06a2c219 8056#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8057
8058 UNBLOCK_INPUT;
12ba150f 8059 return bar;
f451eb13
JB
8060}
8061
06a2c219 8062
12ba150f 8063/* Draw BAR's handle in the proper position.
06a2c219 8064
12ba150f
JB
8065 If the handle is already drawn from START to END, don't bother
8066 redrawing it, unless REBUILD is non-zero; in that case, always
8067 redraw it. (REBUILD is handy for drawing the handle after expose
58769bee 8068 events.)
12ba150f
JB
8069
8070 Normally, we want to constrain the start and end of the handle to
06a2c219
GM
8071 fit inside its rectangle, but if the user is dragging the scroll
8072 bar handle, we want to let them drag it down all the way, so that
8073 the bar's top is as far down as it goes; otherwise, there's no way
8074 to move to the very end of the buffer. */
8075
5c187dee
GM
8076#ifndef USE_TOOLKIT_SCROLL_BARS
8077
f451eb13 8078static void
ab648270
JB
8079x_scroll_bar_set_handle (bar, start, end, rebuild)
8080 struct scroll_bar *bar;
f451eb13 8081 int start, end;
12ba150f 8082 int rebuild;
f451eb13 8083{
12ba150f 8084 int dragging = ! NILP (bar->dragging);
ab648270 8085 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8086 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8087 GC gc = f->output_data.x->normal_gc;
12ba150f
JB
8088
8089 /* If the display is already accurate, do nothing. */
8090 if (! rebuild
8091 && start == XINT (bar->start)
8092 && end == XINT (bar->end))
8093 return;
8094
f451eb13
JB
8095 BLOCK_INPUT;
8096
8097 {
d9cdbb3d
RS
8098 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8099 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8100 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
f451eb13
JB
8101
8102 /* Make sure the values are reasonable, and try to preserve
8103 the distance between start and end. */
12ba150f
JB
8104 {
8105 int length = end - start;
8106
8107 if (start < 0)
8108 start = 0;
8109 else if (start > top_range)
8110 start = top_range;
8111 end = start + length;
8112
8113 if (end < start)
8114 end = start;
8115 else if (end > top_range && ! dragging)
8116 end = top_range;
8117 }
f451eb13 8118
ab648270 8119 /* Store the adjusted setting in the scroll bar. */
e0c1aef2
KH
8120 XSETINT (bar->start, start);
8121 XSETINT (bar->end, end);
f451eb13 8122
12ba150f
JB
8123 /* Clip the end position, just for display. */
8124 if (end > top_range)
8125 end = top_range;
f451eb13 8126
ab648270 8127 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
12ba150f
JB
8128 below top positions, to make sure the handle is always at least
8129 that many pixels tall. */
ab648270 8130 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
f451eb13 8131
12ba150f
JB
8132 /* Draw the empty space above the handle. Note that we can't clear
8133 zero-height areas; that means "clear to end of window." */
8134 if (0 < start)
334208b7 8135 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 8136
12ba150f 8137 /* x, y, width, height, and exposures. */
ab648270
JB
8138 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8139 VERTICAL_SCROLL_BAR_TOP_BORDER,
12ba150f
JB
8140 inside_width, start,
8141 False);
f451eb13 8142
06a2c219
GM
8143 /* Change to proper foreground color if one is specified. */
8144 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8145 XSetForeground (FRAME_X_DISPLAY (f), gc,
8146 f->output_data.x->scroll_bar_foreground_pixel);
8147
12ba150f 8148 /* Draw the handle itself. */
334208b7 8149 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13 8150
12ba150f 8151 /* x, y, width, height */
ab648270
JB
8152 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8153 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
12ba150f 8154 inside_width, end - start);
f451eb13 8155
06a2c219
GM
8156 /* Restore the foreground color of the GC if we changed it above. */
8157 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8158 XSetForeground (FRAME_X_DISPLAY (f), gc,
8159 f->output_data.x->foreground_pixel);
f451eb13 8160
12ba150f
JB
8161 /* Draw the empty space below the handle. Note that we can't
8162 clear zero-height areas; that means "clear to end of window." */
8163 if (end < inside_height)
334208b7 8164 XClearArea (FRAME_X_DISPLAY (f), w,
f451eb13 8165
12ba150f 8166 /* x, y, width, height, and exposures. */
ab648270
JB
8167 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8168 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
12ba150f
JB
8169 inside_width, inside_height - end,
8170 False);
f451eb13 8171
f451eb13
JB
8172 }
8173
f451eb13
JB
8174 UNBLOCK_INPUT;
8175}
8176
5c187dee 8177#endif /* !USE_TOOLKIT_SCROLL_BARS */
f451eb13 8178
06a2c219
GM
8179/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8180 nil. */
58769bee 8181
12ba150f 8182static void
ab648270
JB
8183x_scroll_bar_remove (bar)
8184 struct scroll_bar *bar;
12ba150f 8185{
12ba150f
JB
8186 BLOCK_INPUT;
8187
06a2c219
GM
8188#if USE_TOOLKIT_SCROLL_BARS
8189 XtDestroyWidget (SCROLL_BAR_X_WIDGET (bar));
8190#else /* not USE_TOOLKIT_SCROLL_BARS */
5c187dee
GM
8191 {
8192 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8193 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8194 }
06a2c219
GM
8195#endif /* not USE_TOOLKIT_SCROLL_BARS */
8196
ab648270
JB
8197 /* Disassociate this scroll bar from its window. */
8198 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
12ba150f
JB
8199
8200 UNBLOCK_INPUT;
8201}
8202
06a2c219 8203
12ba150f
JB
8204/* Set the handle of the vertical scroll bar for WINDOW to indicate
8205 that we are displaying PORTION characters out of a total of WHOLE
ab648270 8206 characters, starting at POSITION. If WINDOW has no scroll bar,
12ba150f 8207 create one. */
06a2c219 8208
12ba150f 8209static void
06a2c219
GM
8210XTset_vertical_scroll_bar (w, portion, whole, position)
8211 struct window *w;
f451eb13
JB
8212 int portion, whole, position;
8213{
06a2c219 8214 struct frame *f = XFRAME (w->frame);
ab648270 8215 struct scroll_bar *bar;
3c6ede7b 8216 int top, height, left, sb_left, width, sb_width;
06a2c219 8217 int window_x, window_y, window_width, window_height;
06a2c219 8218
3c6ede7b 8219 /* Get window dimensions. */
06a2c219 8220 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
3c6ede7b
GM
8221 top = window_y;
8222 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8223 height = window_height;
06a2c219 8224
3c6ede7b 8225 /* Compute the left edge of the scroll bar area. */
06a2c219 8226 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
3c6ede7b
GM
8227 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8228 else
8229 left = XFASTINT (w->left);
8230 left *= CANON_X_UNIT (f);
8231 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8232
8233 /* Compute the width of the scroll bar which might be less than
8234 the width of the area reserved for the scroll bar. */
8235 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8236 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
06a2c219 8237 else
3c6ede7b 8238 sb_width = width;
12ba150f 8239
3c6ede7b
GM
8240 /* Compute the left edge of the scroll bar. */
8241#ifdef USE_TOOLKIT_SCROLL_BARS
8242 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8243 sb_left = left + width - sb_width - (width - sb_width) / 2;
8244 else
8245 sb_left = left + (width - sb_width) / 2;
8246#else
8247 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8248 sb_left = left + width - sb_width;
8249 else
8250 sb_left = left;
8251#endif
8252
ab648270 8253 /* Does the scroll bar exist yet? */
06a2c219 8254 if (NILP (w->vertical_scroll_bar))
3c6ede7b 8255 {
80c32bcc 8256 BLOCK_INPUT;
3c6ede7b
GM
8257 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8258 left, top, width, height, False);
80c32bcc 8259 UNBLOCK_INPUT;
3c6ede7b
GM
8260 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8261 }
f451eb13 8262 else
12ba150f
JB
8263 {
8264 /* It may just need to be moved and resized. */
06a2c219
GM
8265 unsigned int mask = 0;
8266
8267 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8268
8269 BLOCK_INPUT;
8270
3c6ede7b 8271 if (sb_left != XINT (bar->left))
06a2c219 8272 mask |= CWX;
3c6ede7b 8273 if (top != XINT (bar->top))
06a2c219 8274 mask |= CWY;
3c6ede7b 8275 if (sb_width != XINT (bar->width))
06a2c219 8276 mask |= CWWidth;
3c6ede7b 8277 if (height != XINT (bar->height))
06a2c219
GM
8278 mask |= CWHeight;
8279
8280#ifdef USE_TOOLKIT_SCROLL_BARS
fe6f39d9
GM
8281
8282 /* Since toolkit scroll bars are smaller than the space reserved
8283 for them on the frame, we have to clear "under" them. */
8284 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3c6ede7b 8285 left, top, width, height, False);
06a2c219
GM
8286
8287 /* Move/size the scroll bar widget. */
8288 if (mask)
8289 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
3c6ede7b
GM
8290 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8291 top,
8292 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8293 height, 0);
06a2c219
GM
8294
8295#else /* not USE_TOOLKIT_SCROLL_BARS */
8296
e1f6572f
RS
8297 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8298 {
8299 /* Clear areas not covered by the scroll bar. This makes sure a
8300 previous mode line display is cleared after C-x 2 C-x 1, for
8301 example. Non-toolkit scroll bars are as wide as the area
8302 reserved for scroll bars - trim at both sides. */
8303 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8304 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8305 height, False);
8306 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8307 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8308 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8309 height, False);
8310 }
06a2c219
GM
8311
8312 /* Move/size the scroll bar window. */
8313 if (mask)
8314 {
8315 XWindowChanges wc;
8316
3c6ede7b
GM
8317 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8318 wc.y = top;
8319 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8320 wc.height = height;
06a2c219
GM
8321 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8322 mask, &wc);
8323 }
8324
8325#endif /* not USE_TOOLKIT_SCROLL_BARS */
8326
8327 /* Remember new settings. */
3c6ede7b
GM
8328 XSETINT (bar->left, sb_left);
8329 XSETINT (bar->top, top);
8330 XSETINT (bar->width, sb_width);
8331 XSETINT (bar->height, height);
06a2c219
GM
8332
8333 UNBLOCK_INPUT;
12ba150f 8334 }
f451eb13 8335
06a2c219
GM
8336#if USE_TOOLKIT_SCROLL_BARS
8337 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8338#else /* not USE_TOOLKIT_SCROLL_BARS */
ab648270 8339 /* Set the scroll bar's current state, unless we're currently being
f451eb13 8340 dragged. */
12ba150f 8341 if (NILP (bar->dragging))
f451eb13 8342 {
92857db0 8343 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
f451eb13 8344
12ba150f 8345 if (whole == 0)
ab648270 8346 x_scroll_bar_set_handle (bar, 0, top_range, 0);
12ba150f
JB
8347 else
8348 {
43f868f5
JB
8349 int start = ((double) position * top_range) / whole;
8350 int end = ((double) (position + portion) * top_range) / whole;
ab648270 8351 x_scroll_bar_set_handle (bar, start, end, 0);
12ba150f 8352 }
f451eb13 8353 }
06a2c219 8354#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 8355
06a2c219 8356 XSETVECTOR (w->vertical_scroll_bar, bar);
f451eb13
JB
8357}
8358
12ba150f 8359
f451eb13 8360/* The following three hooks are used when we're doing a thorough
ab648270 8361 redisplay of the frame. We don't explicitly know which scroll bars
f451eb13 8362 are going to be deleted, because keeping track of when windows go
12ba150f
JB
8363 away is a real pain - "Can you say set-window-configuration, boys
8364 and girls?" Instead, we just assert at the beginning of redisplay
ab648270 8365 that *all* scroll bars are to be removed, and then save a scroll bar
12ba150f 8366 from the fiery pit when we actually redisplay its window. */
f451eb13 8367
ab648270
JB
8368/* Arrange for all scroll bars on FRAME to be removed at the next call
8369 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
06a2c219
GM
8370 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8371
58769bee 8372static void
ab648270 8373XTcondemn_scroll_bars (frame)
f451eb13
JB
8374 FRAME_PTR frame;
8375{
f9e24cb9
RS
8376 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8377 while (! NILP (FRAME_SCROLL_BARS (frame)))
8378 {
8379 Lisp_Object bar;
8380 bar = FRAME_SCROLL_BARS (frame);
8381 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8382 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8383 XSCROLL_BAR (bar)->prev = Qnil;
8384 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8385 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8386 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8387 }
f451eb13
JB
8388}
8389
06a2c219 8390/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
12ba150f 8391 Note that WINDOW isn't necessarily condemned at all. */
f451eb13 8392static void
ab648270 8393XTredeem_scroll_bar (window)
12ba150f 8394 struct window *window;
f451eb13 8395{
ab648270 8396 struct scroll_bar *bar;
12ba150f 8397
ab648270
JB
8398 /* We can't redeem this window's scroll bar if it doesn't have one. */
8399 if (NILP (window->vertical_scroll_bar))
12ba150f
JB
8400 abort ();
8401
ab648270 8402 bar = XSCROLL_BAR (window->vertical_scroll_bar);
12ba150f
JB
8403
8404 /* Unlink it from the condemned list. */
8405 {
8406 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
8407
8408 if (NILP (bar->prev))
8409 {
8410 /* If the prev pointer is nil, it must be the first in one of
8411 the lists. */
ab648270 8412 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
12ba150f
JB
8413 /* It's not condemned. Everything's fine. */
8414 return;
ab648270
JB
8415 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8416 window->vertical_scroll_bar))
8417 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
12ba150f
JB
8418 else
8419 /* If its prev pointer is nil, it must be at the front of
8420 one or the other! */
8421 abort ();
8422 }
8423 else
ab648270 8424 XSCROLL_BAR (bar->prev)->next = bar->next;
12ba150f
JB
8425
8426 if (! NILP (bar->next))
ab648270 8427 XSCROLL_BAR (bar->next)->prev = bar->prev;
12ba150f 8428
ab648270 8429 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 8430 bar->prev = Qnil;
e0c1aef2 8431 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
12ba150f 8432 if (! NILP (bar->next))
e0c1aef2 8433 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
12ba150f 8434 }
f451eb13
JB
8435}
8436
ab648270
JB
8437/* Remove all scroll bars on FRAME that haven't been saved since the
8438 last call to `*condemn_scroll_bars_hook'. */
06a2c219 8439
f451eb13 8440static void
ab648270 8441XTjudge_scroll_bars (f)
12ba150f 8442 FRAME_PTR f;
f451eb13 8443{
12ba150f 8444 Lisp_Object bar, next;
f451eb13 8445
ab648270 8446 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
cf7cb199
JB
8447
8448 /* Clear out the condemned list now so we won't try to process any
ab648270
JB
8449 more events on the hapless scroll bars. */
8450 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
cf7cb199
JB
8451
8452 for (; ! NILP (bar); bar = next)
f451eb13 8453 {
ab648270 8454 struct scroll_bar *b = XSCROLL_BAR (bar);
12ba150f 8455
ab648270 8456 x_scroll_bar_remove (b);
12ba150f
JB
8457
8458 next = b->next;
8459 b->next = b->prev = Qnil;
f451eb13 8460 }
12ba150f 8461
ab648270 8462 /* Now there should be no references to the condemned scroll bars,
12ba150f 8463 and they should get garbage-collected. */
f451eb13
JB
8464}
8465
8466
06a2c219
GM
8467/* Handle an Expose or GraphicsExpose event on a scroll bar. This
8468 is a no-op when using toolkit scroll bars.
ab648270
JB
8469
8470 This may be called from a signal handler, so we have to ignore GC
8471 mark bits. */
06a2c219 8472
f451eb13 8473static void
ab648270
JB
8474x_scroll_bar_expose (bar, event)
8475 struct scroll_bar *bar;
f451eb13
JB
8476 XEvent *event;
8477{
06a2c219
GM
8478#ifndef USE_TOOLKIT_SCROLL_BARS
8479
ab648270 8480 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 8481 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 8482 GC gc = f->output_data.x->normal_gc;
3cbd2e0b 8483 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
12ba150f 8484
f451eb13
JB
8485 BLOCK_INPUT;
8486
ab648270 8487 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
f451eb13 8488
06a2c219 8489 /* Draw a one-pixel border just inside the edges of the scroll bar. */
334208b7 8490 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13
JB
8491
8492 /* x, y, width, height */
d9cdbb3d 8493 0, 0,
3cbd2e0b 8494 XINT (bar->width) - 1 - width_trim - width_trim,
d9cdbb3d
RS
8495 XINT (bar->height) - 1);
8496
f451eb13 8497 UNBLOCK_INPUT;
06a2c219
GM
8498
8499#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
8500}
8501
ab648270
JB
8502/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8503 is set to something other than no_event, it is enqueued.
8504
8505 This may be called from a signal handler, so we have to ignore GC
8506 mark bits. */
06a2c219 8507
5c187dee
GM
8508#ifndef USE_TOOLKIT_SCROLL_BARS
8509
f451eb13 8510static void
ab648270
JB
8511x_scroll_bar_handle_click (bar, event, emacs_event)
8512 struct scroll_bar *bar;
f451eb13
JB
8513 XEvent *event;
8514 struct input_event *emacs_event;
8515{
0299d313 8516 if (! GC_WINDOWP (bar->window))
12ba150f
JB
8517 abort ();
8518
ab648270 8519 emacs_event->kind = scroll_bar_click;
69388238 8520 emacs_event->code = event->xbutton.button - Button1;
0299d313
RS
8521 emacs_event->modifiers
8522 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8523 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8524 event->xbutton.state)
8525 | (event->type == ButtonRelease
8526 ? up_modifier
8527 : down_modifier));
12ba150f 8528 emacs_event->frame_or_window = bar->window;
f451eb13 8529 emacs_event->timestamp = event->xbutton.time;
12ba150f 8530 {
06a2c219 8531#if 0
d9cdbb3d 8532 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
0299d313 8533 int internal_height
d9cdbb3d 8534 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8535#endif
0299d313 8536 int top_range
d9cdbb3d 8537 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ab648270 8538 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
12ba150f
JB
8539
8540 if (y < 0) y = 0;
8541 if (y > top_range) y = top_range;
8542
8543 if (y < XINT (bar->start))
ab648270
JB
8544 emacs_event->part = scroll_bar_above_handle;
8545 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8546 emacs_event->part = scroll_bar_handle;
12ba150f 8547 else
ab648270 8548 emacs_event->part = scroll_bar_below_handle;
929787e1
JB
8549
8550 /* Just because the user has clicked on the handle doesn't mean
5116f055
JB
8551 they want to drag it. Lisp code needs to be able to decide
8552 whether or not we're dragging. */
929787e1 8553#if 0
12ba150f
JB
8554 /* If the user has just clicked on the handle, record where they're
8555 holding it. */
8556 if (event->type == ButtonPress
ab648270 8557 && emacs_event->part == scroll_bar_handle)
e0c1aef2 8558 XSETINT (bar->dragging, y - XINT (bar->start));
929787e1 8559#endif
12ba150f
JB
8560
8561 /* If the user has released the handle, set it to its final position. */
8562 if (event->type == ButtonRelease
8563 && ! NILP (bar->dragging))
8564 {
8565 int new_start = y - XINT (bar->dragging);
8566 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
f451eb13 8567
ab648270 8568 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
12ba150f
JB
8569 bar->dragging = Qnil;
8570 }
f451eb13 8571
5116f055
JB
8572 /* Same deal here as the other #if 0. */
8573#if 0
58769bee 8574 /* Clicks on the handle are always reported as occurring at the top of
12ba150f 8575 the handle. */
ab648270 8576 if (emacs_event->part == scroll_bar_handle)
12ba150f
JB
8577 emacs_event->x = bar->start;
8578 else
e0c1aef2 8579 XSETINT (emacs_event->x, y);
5116f055 8580#else
e0c1aef2 8581 XSETINT (emacs_event->x, y);
5116f055 8582#endif
f451eb13 8583
e0c1aef2 8584 XSETINT (emacs_event->y, top_range);
12ba150f
JB
8585 }
8586}
f451eb13 8587
ab648270
JB
8588/* Handle some mouse motion while someone is dragging the scroll bar.
8589
8590 This may be called from a signal handler, so we have to ignore GC
8591 mark bits. */
06a2c219 8592
f451eb13 8593static void
ab648270
JB
8594x_scroll_bar_note_movement (bar, event)
8595 struct scroll_bar *bar;
f451eb13
JB
8596 XEvent *event;
8597{
39d8bb4d
KH
8598 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8599
f451eb13
JB
8600 last_mouse_movement_time = event->xmotion.time;
8601
39d8bb4d 8602 f->mouse_moved = 1;
e0c1aef2 8603 XSETVECTOR (last_mouse_scroll_bar, bar);
f451eb13
JB
8604
8605 /* If we're dragging the bar, display it. */
ab648270 8606 if (! GC_NILP (bar->dragging))
f451eb13
JB
8607 {
8608 /* Where should the handle be now? */
12ba150f 8609 int new_start = event->xmotion.y - XINT (bar->dragging);
f451eb13 8610
12ba150f 8611 if (new_start != XINT (bar->start))
f451eb13 8612 {
12ba150f 8613 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
58769bee 8614
ab648270 8615 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
f451eb13
JB
8616 }
8617 }
f451eb13
JB
8618}
8619
5c187dee
GM
8620#endif /* !USE_TOOLKIT_SCROLL_BARS */
8621
12ba150f 8622/* Return information to the user about the current position of the mouse
ab648270 8623 on the scroll bar. */
06a2c219 8624
12ba150f 8625static void
334208b7
RS
8626x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8627 FRAME_PTR *fp;
12ba150f 8628 Lisp_Object *bar_window;
ab648270 8629 enum scroll_bar_part *part;
12ba150f
JB
8630 Lisp_Object *x, *y;
8631 unsigned long *time;
8632{
ab648270 8633 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
334208b7
RS
8634 Window w = SCROLL_BAR_X_WINDOW (bar);
8635 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f 8636 int win_x, win_y;
559cb2fb
JB
8637 Window dummy_window;
8638 int dummy_coord;
8639 unsigned int dummy_mask;
12ba150f 8640
cf7cb199
JB
8641 BLOCK_INPUT;
8642
ab648270 8643 /* Get the mouse's position relative to the scroll bar window, and
12ba150f 8644 report that. */
334208b7 8645 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
12ba150f 8646
559cb2fb
JB
8647 /* Root, child, root x and root y. */
8648 &dummy_window, &dummy_window,
8649 &dummy_coord, &dummy_coord,
12ba150f 8650
559cb2fb
JB
8651 /* Position relative to scroll bar. */
8652 &win_x, &win_y,
12ba150f 8653
559cb2fb
JB
8654 /* Mouse buttons and modifier keys. */
8655 &dummy_mask))
7a13e894 8656 ;
559cb2fb
JB
8657 else
8658 {
06a2c219 8659#if 0
559cb2fb 8660 int inside_height
d9cdbb3d 8661 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 8662#endif
559cb2fb 8663 int top_range
d9cdbb3d 8664 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
559cb2fb
JB
8665
8666 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
8667
8668 if (! NILP (bar->dragging))
8669 win_y -= XINT (bar->dragging);
8670
8671 if (win_y < 0)
8672 win_y = 0;
8673 if (win_y > top_range)
8674 win_y = top_range;
8675
334208b7 8676 *fp = f;
7a13e894 8677 *bar_window = bar->window;
559cb2fb
JB
8678
8679 if (! NILP (bar->dragging))
8680 *part = scroll_bar_handle;
8681 else if (win_y < XINT (bar->start))
8682 *part = scroll_bar_above_handle;
8683 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8684 *part = scroll_bar_handle;
8685 else
8686 *part = scroll_bar_below_handle;
12ba150f 8687
e0c1aef2
KH
8688 XSETINT (*x, win_y);
8689 XSETINT (*y, top_range);
12ba150f 8690
39d8bb4d 8691 f->mouse_moved = 0;
559cb2fb
JB
8692 last_mouse_scroll_bar = Qnil;
8693 }
12ba150f 8694
559cb2fb 8695 *time = last_mouse_movement_time;
cf7cb199 8696
cf7cb199 8697 UNBLOCK_INPUT;
12ba150f
JB
8698}
8699
f451eb13 8700
dbc4e1c1 8701/* The screen has been cleared so we may have changed foreground or
ab648270
JB
8702 background colors, and the scroll bars may need to be redrawn.
8703 Clear out the scroll bars, and ask for expose events, so we can
dbc4e1c1
JB
8704 redraw them. */
8705
dfcf069d 8706void
ab648270 8707x_scroll_bar_clear (f)
dbc4e1c1
JB
8708 FRAME_PTR f;
8709{
06a2c219 8710#ifndef USE_TOOLKIT_SCROLL_BARS
dbc4e1c1
JB
8711 Lisp_Object bar;
8712
b80c363e
RS
8713 /* We can have scroll bars even if this is 0,
8714 if we just turned off scroll bar mode.
8715 But in that case we should not clear them. */
8716 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8717 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
8718 bar = XSCROLL_BAR (bar)->next)
8719 XClearArea (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
8720 0, 0, 0, 0, True);
06a2c219 8721#endif /* not USE_TOOLKIT_SCROLL_BARS */
dbc4e1c1
JB
8722}
8723
06a2c219 8724/* This processes Expose events from the menu-bar specific X event
19126e11 8725 loop in xmenu.c. This allows to redisplay the frame if necessary
06a2c219 8726 when handling menu-bar or pop-up items. */
3afe33e7 8727
06a2c219 8728int
3afe33e7
RS
8729process_expose_from_menu (event)
8730 XEvent event;
8731{
8732 FRAME_PTR f;
19126e11 8733 struct x_display_info *dpyinfo;
06a2c219 8734 int frame_exposed_p = 0;
3afe33e7 8735
f94397b5
KH
8736 BLOCK_INPUT;
8737
19126e11
KH
8738 dpyinfo = x_display_info_for_display (event.xexpose.display);
8739 f = x_window_to_frame (dpyinfo, event.xexpose.window);
3afe33e7
RS
8740 if (f)
8741 {
8742 if (f->async_visible == 0)
8743 {
8744 f->async_visible = 1;
8745 f->async_iconified = 0;
06c488fd 8746 f->output_data.x->has_been_visible = 1;
3afe33e7
RS
8747 SET_FRAME_GARBAGED (f);
8748 }
8749 else
8750 {
06a2c219
GM
8751 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
8752 event.xexpose.x, event.xexpose.y,
8753 event.xexpose.width, event.xexpose.height);
8754 frame_exposed_p = 1;
3afe33e7
RS
8755 }
8756 }
8757 else
8758 {
8759 struct scroll_bar *bar
8760 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 8761
3afe33e7
RS
8762 if (bar)
8763 x_scroll_bar_expose (bar, &event);
8764 }
f94397b5
KH
8765
8766 UNBLOCK_INPUT;
06a2c219 8767 return frame_exposed_p;
3afe33e7 8768}
09756a85
RS
8769\f
8770/* Define a queue to save up SelectionRequest events for later handling. */
8771
8772struct selection_event_queue
8773 {
8774 XEvent event;
8775 struct selection_event_queue *next;
8776 };
8777
8778static struct selection_event_queue *queue;
8779
8780/* Nonzero means queue up certain events--don't process them yet. */
06a2c219 8781
09756a85
RS
8782static int x_queue_selection_requests;
8783
8784/* Queue up an X event *EVENT, to be processed later. */
dbc4e1c1 8785
09756a85 8786static void
334208b7
RS
8787x_queue_event (f, event)
8788 FRAME_PTR f;
09756a85
RS
8789 XEvent *event;
8790{
8791 struct selection_event_queue *queue_tmp
06a2c219 8792 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
09756a85 8793
58769bee 8794 if (queue_tmp != NULL)
09756a85
RS
8795 {
8796 queue_tmp->event = *event;
8797 queue_tmp->next = queue;
8798 queue = queue_tmp;
8799 }
8800}
8801
8802/* Take all the queued events and put them back
8803 so that they get processed afresh. */
8804
8805static void
db3906fd
RS
8806x_unqueue_events (display)
8807 Display *display;
09756a85 8808{
58769bee 8809 while (queue != NULL)
09756a85
RS
8810 {
8811 struct selection_event_queue *queue_tmp = queue;
db3906fd 8812 XPutBackEvent (display, &queue_tmp->event);
09756a85 8813 queue = queue_tmp->next;
06a2c219 8814 xfree ((char *)queue_tmp);
09756a85
RS
8815 }
8816}
8817
8818/* Start queuing SelectionRequest events. */
8819
8820void
db3906fd
RS
8821x_start_queuing_selection_requests (display)
8822 Display *display;
09756a85
RS
8823{
8824 x_queue_selection_requests++;
8825}
8826
8827/* Stop queuing SelectionRequest events. */
8828
8829void
db3906fd
RS
8830x_stop_queuing_selection_requests (display)
8831 Display *display;
09756a85
RS
8832{
8833 x_queue_selection_requests--;
db3906fd 8834 x_unqueue_events (display);
09756a85 8835}
f451eb13
JB
8836\f
8837/* The main X event-reading loop - XTread_socket. */
dc6f92b8 8838
06a2c219 8839/* Time stamp of enter window event. This is only used by XTread_socket,
dc6f92b8
JB
8840 but we have to put it out here, since static variables within functions
8841 sometimes don't work. */
06a2c219 8842
dc6f92b8
JB
8843static Time enter_timestamp;
8844
11edeb03 8845/* This holds the state XLookupString needs to implement dead keys
58769bee 8846 and other tricks known as "compose processing". _X Window System_
11edeb03
JB
8847 says that a portable program can't use this, but Stephen Gildea assures
8848 me that letting the compiler initialize it to zeros will work okay.
8849
8850 This must be defined outside of XTread_socket, for the same reasons
06a2c219
GM
8851 given for enter_time stamp, above. */
8852
11edeb03
JB
8853static XComposeStatus compose_status;
8854
10e6549c
RS
8855/* Record the last 100 characters stored
8856 to help debug the loss-of-chars-during-GC problem. */
06a2c219 8857
2224b905
RS
8858static int temp_index;
8859static short temp_buffer[100];
10e6549c 8860
7a13e894
RS
8861/* Set this to nonzero to fake an "X I/O error"
8862 on a particular display. */
06a2c219 8863
7a13e894
RS
8864struct x_display_info *XTread_socket_fake_io_error;
8865
2224b905
RS
8866/* When we find no input here, we occasionally do a no-op command
8867 to verify that the X server is still running and we can still talk with it.
8868 We try all the open displays, one by one.
8869 This variable is used for cycling thru the displays. */
06a2c219 8870
2224b905
RS
8871static struct x_display_info *next_noop_dpyinfo;
8872
06a2c219
GM
8873#define SET_SAVED_MENU_EVENT(size) \
8874 do \
8875 { \
8876 if (f->output_data.x->saved_menu_event == 0) \
8877 f->output_data.x->saved_menu_event \
8878 = (XEvent *) xmalloc (sizeof (XEvent)); \
8879 bcopy (&event, f->output_data.x->saved_menu_event, size); \
8880 if (numchars >= 1) \
8881 { \
8882 bufp->kind = menu_bar_activate_event; \
8883 XSETFRAME (bufp->frame_or_window, f); \
8884 bufp++; \
8885 count++; \
8886 numchars--; \
8887 } \
8888 } \
8889 while (0)
8890
8805890a 8891#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
06a2c219 8892#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8805890a 8893
dc6f92b8
JB
8894/* Read events coming from the X server.
8895 This routine is called by the SIGIO handler.
8896 We return as soon as there are no more events to be read.
8897
8898 Events representing keys are stored in buffer BUFP,
8899 which can hold up to NUMCHARS characters.
8900 We return the number of characters stored into the buffer,
8901 thus pretending to be `read'.
8902
dc6f92b8
JB
8903 EXPECTED is nonzero if the caller knows input is available. */
8904
7c5283e4 8905int
f66868ba 8906XTread_socket (sd, bufp, numchars, expected)
dc6f92b8 8907 register int sd;
8805890a
KH
8908 /* register */ struct input_event *bufp;
8909 /* register */ int numchars;
dc6f92b8
JB
8910 int expected;
8911{
8912 int count = 0;
8913 int nbytes = 0;
dc6f92b8 8914 XEvent event;
f676886a 8915 struct frame *f;
66f55a9d 8916 int event_found = 0;
334208b7 8917 struct x_display_info *dpyinfo;
dc6f92b8 8918
9ac0d9e0 8919 if (interrupt_input_blocked)
dc6f92b8 8920 {
9ac0d9e0 8921 interrupt_input_pending = 1;
dc6f92b8
JB
8922 return -1;
8923 }
8924
9ac0d9e0 8925 interrupt_input_pending = 0;
dc6f92b8 8926 BLOCK_INPUT;
c0a04927
RS
8927
8928 /* So people can tell when we have read the available input. */
8929 input_signal_count++;
8930
dc6f92b8 8931 if (numchars <= 0)
06a2c219 8932 abort (); /* Don't think this happens. */
dc6f92b8 8933
7a13e894
RS
8934 /* Find the display we are supposed to read input for.
8935 It's the one communicating on descriptor SD. */
8936 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
8937 {
8938#if 0 /* This ought to be unnecessary; let's verify it. */
dc6f92b8 8939#ifdef FIOSNBIO
7a13e894
RS
8940 /* If available, Xlib uses FIOSNBIO to make the socket
8941 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
e6cbea31 8942 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
06a2c219 8943 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7a13e894 8944 fcntl (dpyinfo->connection, F_SETFL, 0);
c118dd06 8945#endif /* ! defined (FIOSNBIO) */
7a13e894 8946#endif
dc6f92b8 8947
7a13e894
RS
8948#if 0 /* This code can't be made to work, with multiple displays,
8949 and appears not to be used on any system any more.
8950 Also keyboard.c doesn't turn O_NDELAY on and off
8951 for X connections. */
dc6f92b8
JB
8952#ifndef SIGIO
8953#ifndef HAVE_SELECT
7a13e894
RS
8954 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
8955 {
8956 extern int read_alarm_should_throw;
8957 read_alarm_should_throw = 1;
8958 XPeekEvent (dpyinfo->display, &event);
8959 read_alarm_should_throw = 0;
8960 }
c118dd06
JB
8961#endif /* HAVE_SELECT */
8962#endif /* SIGIO */
7a13e894 8963#endif
dc6f92b8 8964
7a13e894
RS
8965 /* For debugging, this gives a way to fake an I/O error. */
8966 if (dpyinfo == XTread_socket_fake_io_error)
8967 {
8968 XTread_socket_fake_io_error = 0;
8969 x_io_error_quitter (dpyinfo->display);
8970 }
dc6f92b8 8971
06a2c219 8972 while (XPending (dpyinfo->display))
dc6f92b8 8973 {
7a13e894 8974 XNextEvent (dpyinfo->display, &event);
06a2c219 8975
531483fb 8976#ifdef HAVE_X_I18N
d1bc4182 8977 {
f2be1146
GM
8978 /* Filter events for the current X input method.
8979 XFilterEvent returns non-zero if the input method has
8980 consumed the event. We pass the frame's X window to
8981 XFilterEvent because that's the one for which the IC
8982 was created. */
f5d11644
GM
8983 struct frame *f1 = x_any_window_to_frame (dpyinfo,
8984 event.xclient.window);
8985 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
d1bc4182
RS
8986 break;
8987 }
0cd6403b 8988#endif
7a13e894
RS
8989 event_found = 1;
8990
8991 switch (event.type)
8992 {
8993 case ClientMessage:
c047688c 8994 {
7a13e894
RS
8995 if (event.xclient.message_type
8996 == dpyinfo->Xatom_wm_protocols
8997 && event.xclient.format == 32)
c047688c 8998 {
7a13e894
RS
8999 if (event.xclient.data.l[0]
9000 == dpyinfo->Xatom_wm_take_focus)
c047688c 9001 {
8c1a6a84
RS
9002 /* Use x_any_window_to_frame because this
9003 could be the shell widget window
9004 if the frame has no title bar. */
9005 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6c183ba5
RS
9006#ifdef HAVE_X_I18N
9007 /* Not quite sure this is needed -pd */
8c1a6a84 9008 if (f && FRAME_XIC (f))
6c183ba5
RS
9009 XSetICFocus (FRAME_XIC (f));
9010#endif
f1da8f06
GM
9011#if 0 /* Emacs sets WM hints whose `input' field is `true'. This
9012 instructs the WM to set the input focus automatically for
9013 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
9014 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
9015 it has set the focus. So, XSetInputFocus below is not
9016 needed.
9017
9018 The call to XSetInputFocus below has also caused trouble. In
9019 cases where the XSetInputFocus done by the WM and the one
9020 below are temporally close (on a fast machine), the call
9021 below can generate additional FocusIn events which confuse
9022 Emacs. */
9023
bf7253f4
RS
9024 /* Since we set WM_TAKE_FOCUS, we must call
9025 XSetInputFocus explicitly. But not if f is null,
9026 since that might be an event for a deleted frame. */
7a13e894 9027 if (f)
bf7253f4
RS
9028 {
9029 Display *d = event.xclient.display;
9030 /* Catch and ignore errors, in case window has been
9031 iconified by a window manager such as GWM. */
9032 int count = x_catch_errors (d);
9033 XSetInputFocus (d, event.xclient.window,
e1f6572f
RS
9034 /* The ICCCM says this is
9035 the only valid choice. */
9036 RevertToParent,
bf7253f4
RS
9037 event.xclient.data.l[1]);
9038 /* This is needed to detect the error
9039 if there is an error. */
9040 XSync (d, False);
9041 x_uncatch_errors (d, count);
9042 }
7a13e894 9043 /* Not certain about handling scroll bars here */
f1da8f06 9044#endif /* 0 */
c047688c 9045 }
7a13e894
RS
9046 else if (event.xclient.data.l[0]
9047 == dpyinfo->Xatom_wm_save_yourself)
9048 {
9049 /* Save state modify the WM_COMMAND property to
06a2c219 9050 something which can reinstate us. This notifies
7a13e894
RS
9051 the session manager, who's looking for such a
9052 PropertyNotify. Can restart processing when
06a2c219 9053 a keyboard or mouse event arrives. */
7a13e894
RS
9054 if (numchars > 0)
9055 {
19126e11
KH
9056 f = x_top_window_to_frame (dpyinfo,
9057 event.xclient.window);
7a13e894
RS
9058
9059 /* This is just so we only give real data once
9060 for a single Emacs process. */
b86bd3dd 9061 if (f == SELECTED_FRAME ())
7a13e894
RS
9062 XSetCommand (FRAME_X_DISPLAY (f),
9063 event.xclient.window,
9064 initial_argv, initial_argc);
f000f5c5 9065 else if (f)
7a13e894
RS
9066 XSetCommand (FRAME_X_DISPLAY (f),
9067 event.xclient.window,
9068 0, 0);
9069 }
9070 }
9071 else if (event.xclient.data.l[0]
9072 == dpyinfo->Xatom_wm_delete_window)
1fb20991 9073 {
19126e11
KH
9074 struct frame *f
9075 = x_any_window_to_frame (dpyinfo,
9076 event.xclient.window);
1fb20991 9077
7a13e894
RS
9078 if (f)
9079 {
9080 if (numchars == 0)
9081 abort ();
1fb20991 9082
7a13e894
RS
9083 bufp->kind = delete_window_event;
9084 XSETFRAME (bufp->frame_or_window, f);
9085 bufp++;
9086
9087 count += 1;
9088 numchars -= 1;
9089 }
1fb20991 9090 }
c047688c 9091 }
7a13e894
RS
9092 else if (event.xclient.message_type
9093 == dpyinfo->Xatom_wm_configure_denied)
9094 {
9095 }
9096 else if (event.xclient.message_type
9097 == dpyinfo->Xatom_wm_window_moved)
9098 {
9099 int new_x, new_y;
19126e11
KH
9100 struct frame *f
9101 = x_window_to_frame (dpyinfo, event.xclient.window);
58769bee 9102
7a13e894
RS
9103 new_x = event.xclient.data.s[0];
9104 new_y = event.xclient.data.s[1];
1fb20991 9105
7a13e894
RS
9106 if (f)
9107 {
7556890b
RS
9108 f->output_data.x->left_pos = new_x;
9109 f->output_data.x->top_pos = new_y;
7a13e894 9110 }
1fb20991 9111 }
0fdff6bb 9112#ifdef HACK_EDITRES
7a13e894
RS
9113 else if (event.xclient.message_type
9114 == dpyinfo->Xatom_editres)
9115 {
19126e11
KH
9116 struct frame *f
9117 = x_any_window_to_frame (dpyinfo, event.xclient.window);
7556890b 9118 _XEditResCheckMessages (f->output_data.x->widget, NULL,
19126e11 9119 &event, NULL);
7a13e894 9120 }
0fdff6bb 9121#endif /* HACK_EDITRES */
06a2c219
GM
9122 else if ((event.xclient.message_type
9123 == dpyinfo->Xatom_DONE)
9124 || (event.xclient.message_type
9125 == dpyinfo->Xatom_PAGE))
9126 {
9127 /* Ghostview job completed. Kill it. We could
9128 reply with "Next" if we received "Page", but we
9129 currently never do because we are interested in
9130 images, only, which should have 1 page. */
06a2c219
GM
9131 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9132 struct frame *f
9133 = x_window_to_frame (dpyinfo, event.xclient.window);
9134 x_kill_gs_process (pixmap, f);
9135 expose_frame (f, 0, 0, 0, 0);
9136 }
9137#ifdef USE_TOOLKIT_SCROLL_BARS
9138 /* Scroll bar callbacks send a ClientMessage from which
9139 we construct an input_event. */
9140 else if (event.xclient.message_type
9141 == dpyinfo->Xatom_Scrollbar)
9142 {
9143 x_scroll_bar_to_input_event (&event, bufp);
9144 ++bufp, ++count, --numchars;
9145 goto out;
9146 }
9147#endif /* USE_TOOLKIT_SCROLL_BARS */
9148 else
9149 goto OTHER;
7a13e894
RS
9150 }
9151 break;
dc6f92b8 9152
7a13e894 9153 case SelectionNotify:
3afe33e7 9154#ifdef USE_X_TOOLKIT
19126e11 9155 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
7a13e894 9156 goto OTHER;
3afe33e7 9157#endif /* not USE_X_TOOLKIT */
dfcf069d 9158 x_handle_selection_notify (&event.xselection);
7a13e894 9159 break;
d56a553a 9160
06a2c219 9161 case SelectionClear: /* Someone has grabbed ownership. */
3afe33e7 9162#ifdef USE_X_TOOLKIT
19126e11 9163 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
7a13e894 9164 goto OTHER;
3afe33e7 9165#endif /* USE_X_TOOLKIT */
7a13e894
RS
9166 {
9167 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
d56a553a 9168
7a13e894
RS
9169 if (numchars == 0)
9170 abort ();
d56a553a 9171
7a13e894
RS
9172 bufp->kind = selection_clear_event;
9173 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9174 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9175 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9176 bufp->frame_or_window = Qnil;
7a13e894 9177 bufp++;
d56a553a 9178
7a13e894
RS
9179 count += 1;
9180 numchars -= 1;
9181 }
9182 break;
dc6f92b8 9183
06a2c219 9184 case SelectionRequest: /* Someone wants our selection. */
3afe33e7 9185#ifdef USE_X_TOOLKIT
19126e11 9186 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
7a13e894 9187 goto OTHER;
3afe33e7 9188#endif /* USE_X_TOOLKIT */
7a13e894 9189 if (x_queue_selection_requests)
19126e11 9190 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
7a13e894
RS
9191 &event);
9192 else
9193 {
9194 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
dc6f92b8 9195
7a13e894
RS
9196 if (numchars == 0)
9197 abort ();
9198
9199 bufp->kind = selection_request_event;
9200 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9201 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9202 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9203 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9204 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9205 SELECTION_EVENT_TIME (bufp) = eventp->time;
e6cbea31 9206 bufp->frame_or_window = Qnil;
7a13e894
RS
9207 bufp++;
9208
9209 count += 1;
9210 numchars -= 1;
9211 }
9212 break;
9213
9214 case PropertyNotify:
3afe33e7 9215#ifdef USE_X_TOOLKIT
19126e11 9216 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
7a13e894 9217 goto OTHER;
3afe33e7 9218#endif /* not USE_X_TOOLKIT */
dfcf069d 9219 x_handle_property_notify (&event.xproperty);
7a13e894 9220 break;
dc6f92b8 9221
7a13e894 9222 case ReparentNotify:
19126e11 9223 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
7a13e894
RS
9224 if (f)
9225 {
9226 int x, y;
7556890b 9227 f->output_data.x->parent_desc = event.xreparent.parent;
7a13e894 9228 x_real_positions (f, &x, &y);
7556890b
RS
9229 f->output_data.x->left_pos = x;
9230 f->output_data.x->top_pos = y;
7a13e894
RS
9231 }
9232 break;
3bd330d4 9233
7a13e894 9234 case Expose:
19126e11 9235 f = x_window_to_frame (dpyinfo, event.xexpose.window);
7a13e894 9236 if (f)
dc6f92b8 9237 {
7a13e894
RS
9238 if (f->async_visible == 0)
9239 {
9240 f->async_visible = 1;
9241 f->async_iconified = 0;
06c488fd 9242 f->output_data.x->has_been_visible = 1;
7a13e894
RS
9243 SET_FRAME_GARBAGED (f);
9244 }
9245 else
06a2c219
GM
9246 expose_frame (x_window_to_frame (dpyinfo,
9247 event.xexpose.window),
9248 event.xexpose.x, event.xexpose.y,
9249 event.xexpose.width, event.xexpose.height);
dc6f92b8
JB
9250 }
9251 else
7a13e894 9252 {
06a2c219
GM
9253#ifdef USE_TOOLKIT_SCROLL_BARS
9254 /* Dispatch event to the widget. */
9255 goto OTHER;
9256#else /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9257 struct scroll_bar *bar
9258 = x_window_to_scroll_bar (event.xexpose.window);
58769bee 9259
7a13e894
RS
9260 if (bar)
9261 x_scroll_bar_expose (bar, &event);
3afe33e7 9262#ifdef USE_X_TOOLKIT
7a13e894
RS
9263 else
9264 goto OTHER;
3afe33e7 9265#endif /* USE_X_TOOLKIT */
06a2c219 9266#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9267 }
9268 break;
dc6f92b8 9269
7a13e894
RS
9270 case GraphicsExpose: /* This occurs when an XCopyArea's
9271 source area was obscured or not
9272 available.*/
19126e11 9273 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
7a13e894
RS
9274 if (f)
9275 {
06a2c219
GM
9276 expose_frame (f,
9277 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9278 event.xgraphicsexpose.width,
9279 event.xgraphicsexpose.height);
7a13e894 9280 }
3afe33e7 9281#ifdef USE_X_TOOLKIT
7a13e894
RS
9282 else
9283 goto OTHER;
3afe33e7 9284#endif /* USE_X_TOOLKIT */
7a13e894 9285 break;
dc6f92b8 9286
7a13e894 9287 case NoExpose: /* This occurs when an XCopyArea's
06a2c219
GM
9288 source area was completely
9289 available */
7a13e894 9290 break;
dc6f92b8 9291
7a13e894 9292 case UnmapNotify:
06a2c219
GM
9293 /* Redo the mouse-highlight after the tooltip has gone. */
9294 if (event.xmap.window == tip_window)
9295 {
9296 tip_window = 0;
9297 redo_mouse_highlight ();
9298 }
9299
91ea2a7a 9300 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
7a13e894
RS
9301 if (f) /* F may no longer exist if
9302 the frame was deleted. */
9303 {
9304 /* While a frame is unmapped, display generation is
9305 disabled; you don't want to spend time updating a
9306 display that won't ever be seen. */
9307 f->async_visible = 0;
9308 /* We can't distinguish, from the event, whether the window
9309 has become iconified or invisible. So assume, if it
9310 was previously visible, than now it is iconified.
1aa6072f
RS
9311 But x_make_frame_invisible clears both
9312 the visible flag and the iconified flag;
9313 and that way, we know the window is not iconified now. */
7a13e894 9314 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
1aa6072f
RS
9315 {
9316 f->async_iconified = 1;
bddd097c 9317
1aa6072f
RS
9318 bufp->kind = iconify_event;
9319 XSETFRAME (bufp->frame_or_window, f);
9320 bufp++;
9321 count++;
9322 numchars--;
9323 }
7a13e894 9324 }
7a13e894 9325 goto OTHER;
dc6f92b8 9326
7a13e894 9327 case MapNotify:
06a2c219
GM
9328 if (event.xmap.window == tip_window)
9329 /* The tooltip has been drawn already. Avoid
9330 the SET_FRAME_GARBAGED below. */
9331 goto OTHER;
9332
9333 /* We use x_top_window_to_frame because map events can
9334 come for sub-windows and they don't mean that the
9335 frame is visible. */
19126e11 9336 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
7a13e894
RS
9337 if (f)
9338 {
9339 f->async_visible = 1;
9340 f->async_iconified = 0;
06c488fd 9341 f->output_data.x->has_been_visible = 1;
dc6f92b8 9342
7a13e894
RS
9343 /* wait_reading_process_input will notice this and update
9344 the frame's display structures. */
9345 SET_FRAME_GARBAGED (f);
bddd097c 9346
d806e720
RS
9347 if (f->iconified)
9348 {
9349 bufp->kind = deiconify_event;
9350 XSETFRAME (bufp->frame_or_window, f);
9351 bufp++;
9352 count++;
9353 numchars--;
9354 }
e73ec6fa 9355 else if (! NILP (Vframe_list)
8e713be6 9356 && ! NILP (XCDR (Vframe_list)))
78aa2ba5
KH
9357 /* Force a redisplay sooner or later
9358 to update the frame titles
9359 in case this is the second frame. */
9360 record_asynch_buffer_change ();
7a13e894 9361 }
7a13e894 9362 goto OTHER;
dc6f92b8 9363
7a13e894 9364 case KeyPress:
19126e11 9365 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
f451eb13 9366
06a2c219
GM
9367#ifdef USE_MOTIF
9368 /* I couldn't find a way to prevent LessTif scroll bars
9369 from consuming key events. */
9370 if (f == 0)
9371 {
9372 Widget widget = XtWindowToWidget (dpyinfo->display,
9373 event.xkey.window);
9374 if (widget && XmIsScrollBar (widget))
9375 {
9376 widget = XtParent (widget);
9377 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9378 }
9379 }
9380#endif /* USE_MOTIF */
9381
7a13e894
RS
9382 if (f != 0)
9383 {
9384 KeySym keysym, orig_keysym;
9385 /* al%imercury@uunet.uu.net says that making this 81 instead of
9386 80 fixed a bug whereby meta chars made his Emacs hang. */
9387 unsigned char copy_buffer[81];
9388 int modifiers;
64bb1782 9389
7a13e894
RS
9390 event.xkey.state
9391 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9392 extra_keyboard_modifiers);
9393 modifiers = event.xkey.state;
3a2712f9 9394
7a13e894 9395 /* This will have to go some day... */
752a043f 9396
7a13e894
RS
9397 /* make_lispy_event turns chars into control chars.
9398 Don't do it here because XLookupString is too eager. */
9399 event.xkey.state &= ~ControlMask;
5d46f928
RS
9400 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9401 | dpyinfo->super_mod_mask
9402 | dpyinfo->hyper_mod_mask
9403 | dpyinfo->alt_mod_mask);
9404
1cf4a0d1
RS
9405 /* In case Meta is ComposeCharacter,
9406 clear its status. According to Markus Ehrnsperger
9407 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9408 this enables ComposeCharacter to work whether or
9409 not it is combined with Meta. */
9410 if (modifiers & dpyinfo->meta_mod_mask)
9411 bzero (&compose_status, sizeof (compose_status));
9412
6c183ba5
RS
9413#ifdef HAVE_X_I18N
9414 if (FRAME_XIC (f))
9415 {
f5d11644
GM
9416 unsigned char *copy_bufptr = copy_buffer;
9417 int copy_bufsiz = sizeof (copy_buffer);
9418 Status status_return;
9419
6c183ba5 9420 nbytes = XmbLookupString (FRAME_XIC (f),
f5d11644
GM
9421 &event.xkey, copy_bufptr,
9422 copy_bufsiz, &keysym,
6c183ba5 9423 &status_return);
f5d11644
GM
9424 if (status_return == XBufferOverflow)
9425 {
9426 copy_bufsiz = nbytes + 1;
9427 copy_bufptr = (char *) alloca (copy_bufsiz);
9428 nbytes = XmbLookupString (FRAME_XIC (f),
9429 &event.xkey, copy_bufptr,
9430 copy_bufsiz, &keysym,
9431 &status_return);
9432 }
9433
1decb680
PE
9434 if (status_return == XLookupNone)
9435 break;
9436 else if (status_return == XLookupChars)
9437 keysym = NoSymbol;
9438 else if (status_return != XLookupKeySym
9439 && status_return != XLookupBoth)
9440 abort ();
6c183ba5
RS
9441 }
9442 else
9443 nbytes = XLookupString (&event.xkey, copy_buffer,
9444 80, &keysym, &compose_status);
9445#else
0299d313
RS
9446 nbytes = XLookupString (&event.xkey, copy_buffer,
9447 80, &keysym, &compose_status);
6c183ba5 9448#endif
dc6f92b8 9449
7a13e894 9450 orig_keysym = keysym;
55123275 9451
7a13e894
RS
9452 if (numchars > 1)
9453 {
9454 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9455 || keysym == XK_Delete
1097aea0 9456#ifdef XK_ISO_Left_Tab
441affdb 9457 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
1097aea0 9458#endif
852bff8f 9459 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
7a13e894
RS
9460 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9461 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
c34790e0 9462#ifdef HPUX
7a13e894
RS
9463 /* This recognizes the "extended function keys".
9464 It seems there's no cleaner way.
9465 Test IsModifierKey to avoid handling mode_switch
9466 incorrectly. */
9467 || ((unsigned) (keysym) >= XK_Select
9468 && (unsigned)(keysym) < XK_KP_Space)
69388238
RS
9469#endif
9470#ifdef XK_dead_circumflex
7a13e894 9471 || orig_keysym == XK_dead_circumflex
69388238
RS
9472#endif
9473#ifdef XK_dead_grave
7a13e894 9474 || orig_keysym == XK_dead_grave
69388238
RS
9475#endif
9476#ifdef XK_dead_tilde
7a13e894 9477 || orig_keysym == XK_dead_tilde
69388238
RS
9478#endif
9479#ifdef XK_dead_diaeresis
7a13e894 9480 || orig_keysym == XK_dead_diaeresis
69388238
RS
9481#endif
9482#ifdef XK_dead_macron
7a13e894 9483 || orig_keysym == XK_dead_macron
69388238
RS
9484#endif
9485#ifdef XK_dead_degree
7a13e894 9486 || orig_keysym == XK_dead_degree
69388238
RS
9487#endif
9488#ifdef XK_dead_acute
7a13e894 9489 || orig_keysym == XK_dead_acute
69388238
RS
9490#endif
9491#ifdef XK_dead_cedilla
7a13e894 9492 || orig_keysym == XK_dead_cedilla
69388238
RS
9493#endif
9494#ifdef XK_dead_breve
7a13e894 9495 || orig_keysym == XK_dead_breve
69388238
RS
9496#endif
9497#ifdef XK_dead_ogonek
7a13e894 9498 || orig_keysym == XK_dead_ogonek
69388238
RS
9499#endif
9500#ifdef XK_dead_caron
7a13e894 9501 || orig_keysym == XK_dead_caron
69388238
RS
9502#endif
9503#ifdef XK_dead_doubleacute
7a13e894 9504 || orig_keysym == XK_dead_doubleacute
69388238
RS
9505#endif
9506#ifdef XK_dead_abovedot
7a13e894 9507 || orig_keysym == XK_dead_abovedot
c34790e0 9508#endif
7a13e894
RS
9509 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9510 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9511 /* Any "vendor-specific" key is ok. */
9512 || (orig_keysym & (1 << 28)))
9513 && ! (IsModifierKey (orig_keysym)
7719aa06
RS
9514#ifndef HAVE_X11R5
9515#ifdef XK_Mode_switch
7a13e894 9516 || ((unsigned)(orig_keysym) == XK_Mode_switch)
7719aa06
RS
9517#endif
9518#ifdef XK_Num_Lock
7a13e894 9519 || ((unsigned)(orig_keysym) == XK_Num_Lock)
7719aa06
RS
9520#endif
9521#endif /* not HAVE_X11R5 */
7a13e894 9522 ))
dc6f92b8 9523 {
10e6549c
RS
9524 if (temp_index == sizeof temp_buffer / sizeof (short))
9525 temp_index = 0;
7a13e894
RS
9526 temp_buffer[temp_index++] = keysym;
9527 bufp->kind = non_ascii_keystroke;
9528 bufp->code = keysym;
e0c1aef2 9529 XSETFRAME (bufp->frame_or_window, f);
334208b7
RS
9530 bufp->modifiers
9531 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9532 modifiers);
1113d9db 9533 bufp->timestamp = event.xkey.time;
dc6f92b8 9534 bufp++;
7a13e894
RS
9535 count++;
9536 numchars--;
dc6f92b8 9537 }
7a13e894
RS
9538 else if (numchars > nbytes)
9539 {
9540 register int i;
9541
9542 for (i = 0; i < nbytes; i++)
9543 {
9544 if (temp_index == sizeof temp_buffer / sizeof (short))
9545 temp_index = 0;
9546 temp_buffer[temp_index++] = copy_buffer[i];
9547 bufp->kind = ascii_keystroke;
9548 bufp->code = copy_buffer[i];
9549 XSETFRAME (bufp->frame_or_window, f);
9550 bufp->modifiers
9551 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9552 modifiers);
9553 bufp->timestamp = event.xkey.time;
9554 bufp++;
9555 }
9556
9557 count += nbytes;
9558 numchars -= nbytes;
1decb680
PE
9559
9560 if (keysym == NoSymbol)
9561 break;
7a13e894
RS
9562 }
9563 else
9564 abort ();
dc6f92b8 9565 }
10e6549c
RS
9566 else
9567 abort ();
dc6f92b8 9568 }
59ddecde
GM
9569#ifdef HAVE_X_I18N
9570 /* Don't dispatch this event since XtDispatchEvent calls
9571 XFilterEvent, and two calls in a row may freeze the
9572 client. */
9573 break;
9574#else
717ca130 9575 goto OTHER;
59ddecde 9576#endif
f451eb13 9577
f5d11644 9578 case KeyRelease:
59ddecde
GM
9579#ifdef HAVE_X_I18N
9580 /* Don't dispatch this event since XtDispatchEvent calls
9581 XFilterEvent, and two calls in a row may freeze the
9582 client. */
9583 break;
9584#else
f5d11644 9585 goto OTHER;
59ddecde 9586#endif
f5d11644 9587
7a13e894 9588 /* Here's a possible interpretation of the whole
06a2c219
GM
9589 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
9590 you get a FocusIn event, you have to get a FocusOut
9591 event before you relinquish the focus. If you
9592 haven't received a FocusIn event, then a mere
9593 LeaveNotify is enough to free you. */
f451eb13 9594
7a13e894 9595 case EnterNotify:
06a2c219
GM
9596 {
9597 int from_menu_bar_p = 0;
9598
9599 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
9600
9601#ifdef LESSTIF_VERSION
9602 /* When clicking outside of a menu bar popup to close
9603 it, we get a FocusIn/ EnterNotify sequence of
9604 events. The flag event.xcrossing.focus is not set
9605 in the EnterNotify event of that sequence because
9606 the focus is in the menu bar,
9607 event.xcrossing.window is the frame's X window.
9608 Unconditionally setting the focus frame to null in
9609 this case is not the right thing, because no event
9610 follows that could set the focus frame to the right
9611 value.
9612
9613 This could be a LessTif bug, but I wasn't able to
9614 reproduce the behavior in a simple test program.
9615
9616 (gerd, LessTif 0.88.1). */
9617
9618 if (!event.xcrossing.focus
9619 && f
9620 && f->output_data.x->menubar_widget)
9621 {
9622 Window focus;
9623 int revert;
9624
9625 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9626 if (focus == XtWindow (f->output_data.x->menubar_widget))
9627 from_menu_bar_p = 1;
9628 }
9629#endif /* LESSTIF_VERSION */
6d4238f3 9630
06a2c219
GM
9631 if (event.xcrossing.focus || from_menu_bar_p)
9632 {
9633 /* Avoid nasty pop/raise loops. */
9634 if (f && (!(f->auto_raise)
9635 || !(f->auto_lower)
9636 || (event.xcrossing.time - enter_timestamp) > 500))
9637 {
9638 x_new_focus_frame (dpyinfo, f);
9639 enter_timestamp = event.xcrossing.time;
9640 }
9641 }
9642 else if (f == dpyinfo->x_focus_frame)
9643 x_new_focus_frame (dpyinfo, 0);
9644
9645 /* EnterNotify counts as mouse movement,
9646 so update things that depend on mouse position. */
9647 if (f && !f->output_data.x->busy_p)
9648 note_mouse_movement (f, &event.xmotion);
9649 goto OTHER;
9650 }
dc6f92b8 9651
7a13e894 9652 case FocusIn:
19126e11 9653 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 9654 if (event.xfocus.detail != NotifyPointer)
0f941935 9655 dpyinfo->x_focus_event_frame = f;
7a13e894 9656 if (f)
eb72635f
GM
9657 {
9658 x_new_focus_frame (dpyinfo, f);
9659
9660 /* Don't stop displaying the initial startup message
9661 for a switch-frame event we don't need. */
9662 if (GC_NILP (Vterminal_frame)
9663 && GC_CONSP (Vframe_list)
9664 && !GC_NILP (XCDR (Vframe_list)))
9665 {
9666 bufp->kind = FOCUS_IN_EVENT;
9667 XSETFRAME (bufp->frame_or_window, f);
9668 ++bufp, ++count, --numchars;
9669 }
9670 }
f9e24cb9 9671
6c183ba5
RS
9672#ifdef HAVE_X_I18N
9673 if (f && FRAME_XIC (f))
9674 XSetICFocus (FRAME_XIC (f));
9675#endif
9676
7a13e894 9677 goto OTHER;
10c5e63d 9678
7a13e894 9679 case LeaveNotify:
19126e11 9680 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
7a13e894 9681 if (f)
10c5e63d 9682 {
06a2c219
GM
9683 Lisp_Object frame;
9684 int from_menu_bar_p = 0;
9685
7a13e894 9686 if (f == dpyinfo->mouse_face_mouse_frame)
06a2c219
GM
9687 {
9688 /* If we move outside the frame, then we're
9689 certainly no longer on any text in the frame. */
9690 clear_mouse_face (dpyinfo);
9691 dpyinfo->mouse_face_mouse_frame = 0;
9692 }
9693
9694 /* Generate a nil HELP_EVENT to cancel a help-echo.
9695 Do it only if there's something to cancel.
9696 Otherwise, the startup message is cleared when
9697 the mouse leaves the frame. */
9698 if (any_help_event_p)
9699 {
9700 XSETFRAME (frame, f);
9701 bufp->kind = HELP_EVENT;
9702 bufp->frame_or_window = Fcons (frame, Qnil);
9703 ++bufp, ++count, --numchars;
9704 }
7a13e894 9705
06a2c219
GM
9706#ifdef LESSTIF_VERSION
9707 /* Please see the comment at the start of the
9708 EnterNotify case. */
9709 if (!event.xcrossing.focus
9710 && f->output_data.x->menubar_widget)
9711 {
9712 Window focus;
9713 int revert;
9714 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9715 if (focus == XtWindow (f->output_data.x->menubar_widget))
9716 from_menu_bar_p = 1;
9717 }
9718#endif /* LESSTIF_VERSION */
9719
9720 if (event.xcrossing.focus || from_menu_bar_p)
0f941935 9721 x_mouse_leave (dpyinfo);
10c5e63d 9722 else
7a13e894 9723 {
0f941935
KH
9724 if (f == dpyinfo->x_focus_event_frame)
9725 dpyinfo->x_focus_event_frame = 0;
9726 if (f == dpyinfo->x_focus_frame)
9727 x_new_focus_frame (dpyinfo, 0);
7a13e894 9728 }
10c5e63d 9729 }
7a13e894 9730 goto OTHER;
dc6f92b8 9731
7a13e894 9732 case FocusOut:
19126e11 9733 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
7a13e894 9734 if (event.xfocus.detail != NotifyPointer
0f941935
KH
9735 && f == dpyinfo->x_focus_event_frame)
9736 dpyinfo->x_focus_event_frame = 0;
9737 if (f && f == dpyinfo->x_focus_frame)
9738 x_new_focus_frame (dpyinfo, 0);
f9e24cb9 9739
6c183ba5
RS
9740#ifdef HAVE_X_I18N
9741 if (f && FRAME_XIC (f))
9742 XUnsetICFocus (FRAME_XIC (f));
9743#endif
9744
7a13e894 9745 goto OTHER;
dc6f92b8 9746
7a13e894 9747 case MotionNotify:
dc6f92b8 9748 {
06a2c219
GM
9749 previous_help_echo = help_echo;
9750 help_echo = Qnil;
9751
7a13e894
RS
9752 if (dpyinfo->grabbed && last_mouse_frame
9753 && FRAME_LIVE_P (last_mouse_frame))
9754 f = last_mouse_frame;
9755 else
19126e11 9756 f = x_window_to_frame (dpyinfo, event.xmotion.window);
06a2c219 9757
7a13e894
RS
9758 if (f)
9759 note_mouse_movement (f, &event.xmotion);
9760 else
9761 {
e88b3c50 9762#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
9763 struct scroll_bar *bar
9764 = x_window_to_scroll_bar (event.xmotion.window);
f451eb13 9765
7a13e894
RS
9766 if (bar)
9767 x_scroll_bar_note_movement (bar, &event);
e88b3c50 9768#endif /* USE_TOOLKIT_SCROLL_BARS */
b8009dd1 9769
06a2c219
GM
9770 /* If we move outside the frame, then we're
9771 certainly no longer on any text in the frame. */
7a13e894
RS
9772 clear_mouse_face (dpyinfo);
9773 }
06a2c219
GM
9774
9775 /* If the contents of the global variable help_echo
9776 has changed, generate a HELP_EVENT. */
9777 if (STRINGP (help_echo)
9778 || STRINGP (previous_help_echo))
9779 {
9780 Lisp_Object frame;
9781
9782 if (f)
9783 XSETFRAME (frame, f);
9784 else
9785 frame = Qnil;
9786
9787 any_help_event_p = 1;
9788 bufp->kind = HELP_EVENT;
9789 bufp->frame_or_window = Fcons (frame, help_echo);
9790 ++bufp, ++count, --numchars;
9791 }
9792
9793 goto OTHER;
dc6f92b8 9794 }
dc6f92b8 9795
7a13e894 9796 case ConfigureNotify:
9829ddba
RS
9797 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
9798 if (f)
af395ec1 9799 {
5c187dee 9800#ifndef USE_X_TOOLKIT
bf1b7b30
KH
9801 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
9802 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
5c187dee 9803
2d7fc7e8
RS
9804 /* In the toolkit version, change_frame_size
9805 is called by the code that handles resizing
9806 of the EmacsFrame widget. */
7a13e894 9807
7a13e894
RS
9808 /* Even if the number of character rows and columns has
9809 not changed, the font size may have changed, so we need
9810 to check the pixel dimensions as well. */
9811 if (columns != f->width
9812 || rows != f->height
7556890b
RS
9813 || event.xconfigure.width != f->output_data.x->pixel_width
9814 || event.xconfigure.height != f->output_data.x->pixel_height)
7a13e894 9815 {
7d1e984f 9816 change_frame_size (f, rows, columns, 0, 1, 0);
7a13e894 9817 SET_FRAME_GARBAGED (f);
e687d06e 9818 cancel_mouse_face (f);
7a13e894 9819 }
2d7fc7e8 9820#endif
af395ec1 9821
7556890b
RS
9822 f->output_data.x->pixel_width = event.xconfigure.width;
9823 f->output_data.x->pixel_height = event.xconfigure.height;
7a13e894
RS
9824
9825 /* What we have now is the position of Emacs's own window.
9826 Convert that to the position of the window manager window. */
dcb07ae9
RS
9827 x_real_positions (f, &f->output_data.x->left_pos,
9828 &f->output_data.x->top_pos);
9829
f5d11644
GM
9830#ifdef HAVE_X_I18N
9831 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
9832 xic_set_statusarea (f);
9833#endif
9834
dcb07ae9
RS
9835 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
9836 {
9837 /* Since the WM decorations come below top_pos now,
9838 we must put them below top_pos in the future. */
9839 f->output_data.x->win_gravity = NorthWestGravity;
9840 x_wm_set_size_hint (f, (long) 0, 0);
9841 }
8f08dc93
KH
9842#ifdef USE_MOTIF
9843 /* Some window managers pass (0,0) as the location of
9844 the window, and the Motif event handler stores it
9845 in the emacs widget, which messes up Motif menus. */
9846 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
9847 {
9848 event.xconfigure.x = f->output_data.x->widget->core.x;
9849 event.xconfigure.y = f->output_data.x->widget->core.y;
9850 }
06a2c219 9851#endif /* USE_MOTIF */
7a13e894 9852 }
2d7fc7e8 9853 goto OTHER;
dc6f92b8 9854
7a13e894
RS
9855 case ButtonPress:
9856 case ButtonRelease:
9857 {
9858 /* If we decide we want to generate an event to be seen
9859 by the rest of Emacs, we put it here. */
9860 struct input_event emacs_event;
9ea173e8 9861 int tool_bar_p = 0;
06a2c219 9862
7a13e894 9863 emacs_event.kind = no_event;
7a13e894 9864 bzero (&compose_status, sizeof (compose_status));
9b07615b 9865
06a2c219
GM
9866 if (dpyinfo->grabbed
9867 && last_mouse_frame
9f67f20b
RS
9868 && FRAME_LIVE_P (last_mouse_frame))
9869 f = last_mouse_frame;
9870 else
2224b905 9871 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9f67f20b 9872
06a2c219
GM
9873 if (f)
9874 {
9ea173e8
GM
9875 /* Is this in the tool-bar? */
9876 if (WINDOWP (f->tool_bar_window)
9877 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
06a2c219
GM
9878 {
9879 Lisp_Object window;
9880 int p, x, y;
9881
9882 x = event.xbutton.x;
9883 y = event.xbutton.y;
9884
9885 /* Set x and y. */
9886 window = window_from_coordinates (f, x, y, &p, 1);
9ea173e8 9887 if (EQ (window, f->tool_bar_window))
06a2c219 9888 {
9ea173e8
GM
9889 x_handle_tool_bar_click (f, &event.xbutton);
9890 tool_bar_p = 1;
06a2c219
GM
9891 }
9892 }
9893
9ea173e8 9894 if (!tool_bar_p)
06a2c219
GM
9895 if (!dpyinfo->x_focus_frame
9896 || f == dpyinfo->x_focus_frame)
9897 construct_mouse_click (&emacs_event, &event, f);
7a13e894
RS
9898 }
9899 else
9900 {
06a2c219 9901#ifndef USE_TOOLKIT_SCROLL_BARS
7a13e894
RS
9902 struct scroll_bar *bar
9903 = x_window_to_scroll_bar (event.xbutton.window);
f451eb13 9904
7a13e894
RS
9905 if (bar)
9906 x_scroll_bar_handle_click (bar, &event, &emacs_event);
06a2c219 9907#endif /* not USE_TOOLKIT_SCROLL_BARS */
7a13e894
RS
9908 }
9909
9910 if (event.type == ButtonPress)
9911 {
9912 dpyinfo->grabbed |= (1 << event.xbutton.button);
9913 last_mouse_frame = f;
edad46f6
KH
9914 /* Ignore any mouse motion that happened
9915 before this event; any subsequent mouse-movement
9916 Emacs events should reflect only motion after
9917 the ButtonPress. */
a00e91cd
KH
9918 if (f != 0)
9919 f->mouse_moved = 0;
06a2c219 9920
9ea173e8
GM
9921 if (!tool_bar_p)
9922 last_tool_bar_item = -1;
7a13e894 9923 }
3afe33e7
RS
9924 else
9925 {
7a13e894 9926 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
3afe33e7 9927 }
23faf38f 9928
7a13e894
RS
9929 if (numchars >= 1 && emacs_event.kind != no_event)
9930 {
9931 bcopy (&emacs_event, bufp, sizeof (struct input_event));
9932 bufp++;
9933 count++;
9934 numchars--;
9935 }
3afe33e7
RS
9936
9937#ifdef USE_X_TOOLKIT
2224b905
RS
9938 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
9939 /* For a down-event in the menu bar,
9940 don't pass it to Xt right now.
9941 Instead, save it away
9942 and we will pass it to Xt from kbd_buffer_get_event.
9943 That way, we can run some Lisp code first. */
91375f8f
RS
9944 if (f && event.type == ButtonPress
9945 /* Verify the event is really within the menu bar
9946 and not just sent to it due to grabbing. */
9947 && event.xbutton.x >= 0
9948 && event.xbutton.x < f->output_data.x->pixel_width
9949 && event.xbutton.y >= 0
9950 && event.xbutton.y < f->output_data.x->menubar_height
9951 && event.xbutton.same_screen)
2224b905 9952 {
8805890a 9953 SET_SAVED_BUTTON_EVENT;
2237cac9
RS
9954 XSETFRAME (last_mouse_press_frame, f);
9955 }
9956 else if (event.type == ButtonPress)
9957 {
9958 last_mouse_press_frame = Qnil;
30e671c3 9959 goto OTHER;
ce89ef46 9960 }
06a2c219 9961
2237cac9
RS
9962#ifdef USE_MOTIF /* This should do not harm for Lucid,
9963 but I am trying to be cautious. */
ce89ef46
RS
9964 else if (event.type == ButtonRelease)
9965 {
2237cac9 9966 if (!NILP (last_mouse_press_frame))
f10ded1c 9967 {
2237cac9
RS
9968 f = XFRAME (last_mouse_press_frame);
9969 if (f->output_data.x)
06a2c219 9970 SET_SAVED_BUTTON_EVENT;
f10ded1c 9971 }
06a2c219 9972 else
30e671c3 9973 goto OTHER;
2224b905 9974 }
2237cac9 9975#endif /* USE_MOTIF */
2224b905
RS
9976 else
9977 goto OTHER;
3afe33e7 9978#endif /* USE_X_TOOLKIT */
7a13e894
RS
9979 }
9980 break;
dc6f92b8 9981
7a13e894 9982 case CirculateNotify:
06a2c219
GM
9983 goto OTHER;
9984
7a13e894 9985 case CirculateRequest:
06a2c219
GM
9986 goto OTHER;
9987
9988 case VisibilityNotify:
9989 goto OTHER;
dc6f92b8 9990
7a13e894
RS
9991 case MappingNotify:
9992 /* Someone has changed the keyboard mapping - update the
9993 local cache. */
9994 switch (event.xmapping.request)
9995 {
9996 case MappingModifier:
9997 x_find_modifier_meanings (dpyinfo);
9998 /* This is meant to fall through. */
9999 case MappingKeyboard:
10000 XRefreshKeyboardMapping (&event.xmapping);
10001 }
7a13e894 10002 goto OTHER;
dc6f92b8 10003
7a13e894 10004 default:
7a13e894 10005 OTHER:
717ca130 10006#ifdef USE_X_TOOLKIT
7a13e894
RS
10007 BLOCK_INPUT;
10008 XtDispatchEvent (&event);
10009 UNBLOCK_INPUT;
3afe33e7 10010#endif /* USE_X_TOOLKIT */
7a13e894
RS
10011 break;
10012 }
dc6f92b8
JB
10013 }
10014 }
10015
06a2c219
GM
10016 out:;
10017
9a5196d0
RS
10018 /* On some systems, an X bug causes Emacs to get no more events
10019 when the window is destroyed. Detect that. (1994.) */
58769bee 10020 if (! event_found)
ef2a22d0 10021 {
ef2a22d0
RS
10022 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
10023 One XNOOP in 100 loops will make Emacs terminate.
10024 B. Bretthauer, 1994 */
10025 x_noop_count++;
58769bee 10026 if (x_noop_count >= 100)
ef2a22d0
RS
10027 {
10028 x_noop_count=0;
2224b905
RS
10029
10030 if (next_noop_dpyinfo == 0)
10031 next_noop_dpyinfo = x_display_list;
10032
10033 XNoOp (next_noop_dpyinfo->display);
10034
10035 /* Each time we get here, cycle through the displays now open. */
10036 next_noop_dpyinfo = next_noop_dpyinfo->next;
ef2a22d0
RS
10037 }
10038 }
502add23 10039
06a2c219 10040 /* If the focus was just given to an auto-raising frame,
0134a210 10041 raise it now. */
7a13e894 10042 /* ??? This ought to be able to handle more than one such frame. */
0134a210
RS
10043 if (pending_autoraise_frame)
10044 {
10045 x_raise_frame (pending_autoraise_frame);
10046 pending_autoraise_frame = 0;
10047 }
0134a210 10048
dc6f92b8
JB
10049 UNBLOCK_INPUT;
10050 return count;
10051}
06a2c219
GM
10052
10053
10054
dc6f92b8 10055\f
06a2c219
GM
10056/***********************************************************************
10057 Text Cursor
10058 ***********************************************************************/
10059
10060/* Note if the text cursor of window W has been overwritten by a
10061 drawing operation that outputs N glyphs starting at HPOS in the
10062 line given by output_cursor.vpos. N < 0 means all the rest of the
10063 line after HPOS has been written. */
10064
10065static void
10066note_overwritten_text_cursor (w, hpos, n)
10067 struct window *w;
10068 int hpos, n;
10069{
10070 if (updated_area == TEXT_AREA
10071 && output_cursor.vpos == w->phys_cursor.vpos
10072 && hpos <= w->phys_cursor.hpos
10073 && (n < 0
10074 || hpos + n > w->phys_cursor.hpos))
10075 w->phys_cursor_on_p = 0;
10076}
f451eb13
JB
10077
10078
06a2c219
GM
10079/* Set clipping for output in glyph row ROW. W is the window in which
10080 we operate. GC is the graphics context to set clipping in.
10081 WHOLE_LINE_P non-zero means include the areas used for truncation
10082 mark display and alike in the clipping rectangle.
10083
10084 ROW may be a text row or, e.g., a mode line. Text rows must be
10085 clipped to the interior of the window dedicated to text display,
10086 mode lines must be clipped to the whole window. */
dc6f92b8
JB
10087
10088static void
06a2c219
GM
10089x_clip_to_row (w, row, gc, whole_line_p)
10090 struct window *w;
10091 struct glyph_row *row;
10092 GC gc;
10093 int whole_line_p;
dc6f92b8 10094{
06a2c219
GM
10095 struct frame *f = XFRAME (WINDOW_FRAME (w));
10096 XRectangle clip_rect;
10097 int window_x, window_y, window_width, window_height;
dc6f92b8 10098
06a2c219 10099 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5c1aae96 10100
06a2c219
GM
10101 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10102 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10103 clip_rect.y = max (clip_rect.y, window_y);
10104 clip_rect.width = window_width;
10105 clip_rect.height = row->visible_height;
5c1aae96 10106
06a2c219
GM
10107 /* If clipping to the whole line, including trunc marks, extend
10108 the rectangle to the left and increase its width. */
10109 if (whole_line_p)
10110 {
110859fc
GM
10111 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10112 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
06a2c219 10113 }
5c1aae96 10114
06a2c219 10115 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
dc6f92b8
JB
10116}
10117
06a2c219
GM
10118
10119/* Draw a hollow box cursor on window W in glyph row ROW. */
dc6f92b8
JB
10120
10121static void
06a2c219
GM
10122x_draw_hollow_cursor (w, row)
10123 struct window *w;
10124 struct glyph_row *row;
dc6f92b8 10125{
06a2c219
GM
10126 struct frame *f = XFRAME (WINDOW_FRAME (w));
10127 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10128 Display *dpy = FRAME_X_DISPLAY (f);
10129 int x, y, wd, h;
10130 XGCValues xgcv;
10131 struct glyph *cursor_glyph;
10132 GC gc;
10133
10134 /* Compute frame-relative coordinates from window-relative
10135 coordinates. */
10136 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10137 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10138 + row->ascent - w->phys_cursor_ascent);
10139 h = row->height - 1;
10140
10141 /* Get the glyph the cursor is on. If we can't tell because
10142 the current matrix is invalid or such, give up. */
10143 cursor_glyph = get_phys_cursor_glyph (w);
10144 if (cursor_glyph == NULL)
dc6f92b8
JB
10145 return;
10146
06a2c219
GM
10147 /* Compute the width of the rectangle to draw. If on a stretch
10148 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10149 rectangle as wide as the glyph, but use a canonical character
10150 width instead. */
10151 wd = cursor_glyph->pixel_width - 1;
10152 if (cursor_glyph->type == STRETCH_GLYPH
10153 && !x_stretch_cursor_p)
10154 wd = min (CANON_X_UNIT (f), wd);
10155
10156 /* The foreground of cursor_gc is typically the same as the normal
10157 background color, which can cause the cursor box to be invisible. */
10158 xgcv.foreground = f->output_data.x->cursor_pixel;
10159 if (dpyinfo->scratch_cursor_gc)
10160 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10161 else
10162 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10163 GCForeground, &xgcv);
10164 gc = dpyinfo->scratch_cursor_gc;
10165
10166 /* Set clipping, draw the rectangle, and reset clipping again. */
10167 x_clip_to_row (w, row, gc, 0);
10168 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10169 XSetClipMask (dpy, gc, None);
dc6f92b8
JB
10170}
10171
06a2c219
GM
10172
10173/* Draw a bar cursor on window W in glyph row ROW.
10174
10175 Implementation note: One would like to draw a bar cursor with an
10176 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10177 Unfortunately, I didn't find a font yet that has this property set.
10178 --gerd. */
dc6f92b8
JB
10179
10180static void
f02d8aa0 10181x_draw_bar_cursor (w, row, width)
06a2c219
GM
10182 struct window *w;
10183 struct glyph_row *row;
f02d8aa0 10184 int width;
dc6f92b8 10185{
06a2c219
GM
10186 /* If cursor hpos is out of bounds, don't draw garbage. This can
10187 happen in mini-buffer windows when switching between echo area
10188 glyphs and mini-buffer. */
10189 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
10190 {
10191 struct frame *f = XFRAME (w->frame);
10192 struct glyph *cursor_glyph;
10193 GC gc;
10194 int x;
10195 unsigned long mask;
10196 XGCValues xgcv;
10197 Display *dpy;
10198 Window window;
10199
10200 cursor_glyph = get_phys_cursor_glyph (w);
10201 if (cursor_glyph == NULL)
10202 return;
10203
10204 xgcv.background = f->output_data.x->cursor_pixel;
10205 xgcv.foreground = f->output_data.x->cursor_pixel;
10206 xgcv.graphics_exposures = 0;
10207 mask = GCForeground | GCBackground | GCGraphicsExposures;
10208 dpy = FRAME_X_DISPLAY (f);
10209 window = FRAME_X_WINDOW (f);
10210 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
10211
10212 if (gc)
10213 XChangeGC (dpy, gc, mask, &xgcv);
10214 else
10215 {
10216 gc = XCreateGC (dpy, window, mask, &xgcv);
10217 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10218 }
10219
f02d8aa0
GM
10220 if (width < 0)
10221 width = f->output_data.x->cursor_width;
10222
06a2c219
GM
10223 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10224 x_clip_to_row (w, row, gc, 0);
10225 XFillRectangle (dpy, window, gc,
10226 x,
10227 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
f02d8aa0 10228 min (cursor_glyph->pixel_width, width),
06a2c219
GM
10229 row->height);
10230 XSetClipMask (dpy, gc, None);
10231 }
dc6f92b8
JB
10232}
10233
06a2c219
GM
10234
10235/* Clear the cursor of window W to background color, and mark the
10236 cursor as not shown. This is used when the text where the cursor
10237 is is about to be rewritten. */
10238
dc6f92b8 10239static void
06a2c219
GM
10240x_clear_cursor (w)
10241 struct window *w;
dc6f92b8 10242{
06a2c219
GM
10243 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10244 x_update_window_cursor (w, 0);
10245}
90e65f07 10246
dbc4e1c1 10247
06a2c219
GM
10248/* Draw the cursor glyph of window W in glyph row ROW. See the
10249 comment of x_draw_glyphs for the meaning of HL. */
dbc4e1c1 10250
06a2c219
GM
10251static void
10252x_draw_phys_cursor_glyph (w, row, hl)
10253 struct window *w;
10254 struct glyph_row *row;
10255 enum draw_glyphs_face hl;
10256{
10257 /* If cursor hpos is out of bounds, don't draw garbage. This can
10258 happen in mini-buffer windows when switching between echo area
10259 glyphs and mini-buffer. */
10260 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
66ac4b0e
GM
10261 {
10262 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10263 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10264 hl, 0, 0, 0);
10265
10266 /* When we erase the cursor, and ROW is overlapped by other
10267 rows, make sure that these overlapping parts of other rows
10268 are redrawn. */
10269 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10270 {
10271 if (row > w->current_matrix->rows
10272 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10273 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10274
10275 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10276 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10277 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10278 }
10279 }
06a2c219 10280}
dbc4e1c1 10281
eea6af04 10282
06a2c219 10283/* Erase the image of a cursor of window W from the screen. */
eea6af04 10284
06a2c219
GM
10285static void
10286x_erase_phys_cursor (w)
10287 struct window *w;
10288{
10289 struct frame *f = XFRAME (w->frame);
10290 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10291 int hpos = w->phys_cursor.hpos;
10292 int vpos = w->phys_cursor.vpos;
10293 int mouse_face_here_p = 0;
10294 struct glyph_matrix *active_glyphs = w->current_matrix;
10295 struct glyph_row *cursor_row;
10296 struct glyph *cursor_glyph;
10297 enum draw_glyphs_face hl;
10298
10299 /* No cursor displayed or row invalidated => nothing to do on the
10300 screen. */
10301 if (w->phys_cursor_type == NO_CURSOR)
10302 goto mark_cursor_off;
10303
10304 /* VPOS >= active_glyphs->nrows means that window has been resized.
10305 Don't bother to erase the cursor. */
10306 if (vpos >= active_glyphs->nrows)
10307 goto mark_cursor_off;
10308
10309 /* If row containing cursor is marked invalid, there is nothing we
10310 can do. */
10311 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10312 if (!cursor_row->enabled_p)
10313 goto mark_cursor_off;
10314
10315 /* This can happen when the new row is shorter than the old one.
10316 In this case, either x_draw_glyphs or clear_end_of_line
10317 should have cleared the cursor. Note that we wouldn't be
10318 able to erase the cursor in this case because we don't have a
10319 cursor glyph at hand. */
10320 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10321 goto mark_cursor_off;
10322
10323 /* If the cursor is in the mouse face area, redisplay that when
10324 we clear the cursor. */
8801a864
KR
10325 if (! NILP (dpyinfo->mouse_face_window)
10326 && w == XWINDOW (dpyinfo->mouse_face_window)
06a2c219
GM
10327 && (vpos > dpyinfo->mouse_face_beg_row
10328 || (vpos == dpyinfo->mouse_face_beg_row
10329 && hpos >= dpyinfo->mouse_face_beg_col))
10330 && (vpos < dpyinfo->mouse_face_end_row
10331 || (vpos == dpyinfo->mouse_face_end_row
10332 && hpos < dpyinfo->mouse_face_end_col))
10333 /* Don't redraw the cursor's spot in mouse face if it is at the
10334 end of a line (on a newline). The cursor appears there, but
10335 mouse highlighting does not. */
10336 && cursor_row->used[TEXT_AREA] > hpos)
10337 mouse_face_here_p = 1;
10338
10339 /* Maybe clear the display under the cursor. */
10340 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10341 {
10342 int x;
045dee35 10343 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dbc4e1c1 10344
06a2c219
GM
10345 cursor_glyph = get_phys_cursor_glyph (w);
10346 if (cursor_glyph == NULL)
10347 goto mark_cursor_off;
dbc4e1c1 10348
06a2c219
GM
10349 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10350
10351 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10352 x,
045dee35 10353 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
06a2c219
GM
10354 cursor_row->y)),
10355 cursor_glyph->pixel_width,
10356 cursor_row->visible_height,
10357 False);
dbc4e1c1 10358 }
06a2c219
GM
10359
10360 /* Erase the cursor by redrawing the character underneath it. */
10361 if (mouse_face_here_p)
10362 hl = DRAW_MOUSE_FACE;
10363 else if (cursor_row->inverse_p)
10364 hl = DRAW_INVERSE_VIDEO;
10365 else
10366 hl = DRAW_NORMAL_TEXT;
10367 x_draw_phys_cursor_glyph (w, cursor_row, hl);
dbc4e1c1 10368
06a2c219
GM
10369 mark_cursor_off:
10370 w->phys_cursor_on_p = 0;
10371 w->phys_cursor_type = NO_CURSOR;
dbc4e1c1
JB
10372}
10373
10374
06a2c219
GM
10375/* Display or clear cursor of window W. If ON is zero, clear the
10376 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10377 where to put the cursor is specified by HPOS, VPOS, X and Y. */
dbc4e1c1 10378
06a2c219
GM
10379void
10380x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10381 struct window *w;
10382 int on, hpos, vpos, x, y;
dbc4e1c1 10383{
06a2c219
GM
10384 struct frame *f = XFRAME (w->frame);
10385 int new_cursor_type;
f02d8aa0 10386 int new_cursor_width;
06a2c219
GM
10387 struct glyph_matrix *current_glyphs;
10388 struct glyph_row *glyph_row;
10389 struct glyph *glyph;
dbc4e1c1 10390
49d838ea 10391 /* This is pointless on invisible frames, and dangerous on garbaged
06a2c219
GM
10392 windows and frames; in the latter case, the frame or window may
10393 be in the midst of changing its size, and x and y may be off the
10394 window. */
10395 if (! FRAME_VISIBLE_P (f)
10396 || FRAME_GARBAGED_P (f)
10397 || vpos >= w->current_matrix->nrows
10398 || hpos >= w->current_matrix->matrix_w)
dc6f92b8
JB
10399 return;
10400
10401 /* If cursor is off and we want it off, return quickly. */
06a2c219 10402 if (!on && !w->phys_cursor_on_p)
dc6f92b8
JB
10403 return;
10404
06a2c219
GM
10405 current_glyphs = w->current_matrix;
10406 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10407 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10408
10409 /* If cursor row is not enabled, we don't really know where to
10410 display the cursor. */
10411 if (!glyph_row->enabled_p)
10412 {
10413 w->phys_cursor_on_p = 0;
10414 return;
10415 }
10416
10417 xassert (interrupt_input_blocked);
10418
10419 /* Set new_cursor_type to the cursor we want to be displayed. In a
10420 mini-buffer window, we want the cursor only to appear if we are
10421 reading input from this window. For the selected window, we want
10422 the cursor type given by the frame parameter. If explicitly
10423 marked off, draw no cursor. In all other cases, we want a hollow
10424 box cursor. */
f02d8aa0 10425 new_cursor_width = -1;
9b4a7047
GM
10426 if (cursor_in_echo_area
10427 && FRAME_HAS_MINIBUF_P (f)
10428 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
06a2c219 10429 {
9b4a7047
GM
10430 if (w == XWINDOW (echo_area_window))
10431 new_cursor_type = FRAME_DESIRED_CURSOR (f);
06a2c219
GM
10432 else
10433 new_cursor_type = HOLLOW_BOX_CURSOR;
10434 }
06a2c219 10435 else
9b4a7047
GM
10436 {
10437 if (w != XWINDOW (selected_window)
10438 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
10439 {
e55a0b79
GM
10440 extern int cursor_in_non_selected_windows;
10441
10442 if (MINI_WINDOW_P (w) || !cursor_in_non_selected_windows)
9b4a7047
GM
10443 new_cursor_type = NO_CURSOR;
10444 else
10445 new_cursor_type = HOLLOW_BOX_CURSOR;
10446 }
10447 else if (w->cursor_off_p)
10448 new_cursor_type = NO_CURSOR;
10449 else
f02d8aa0
GM
10450 {
10451 struct buffer *b = XBUFFER (w->buffer);
10452
10453 if (EQ (b->cursor_type, Qt))
10454 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10455 else
10456 new_cursor_type = x_specified_cursor_type (b->cursor_type,
10457 &new_cursor_width);
10458 }
9b4a7047 10459 }
06a2c219
GM
10460
10461 /* If cursor is currently being shown and we don't want it to be or
10462 it is in the wrong place, or the cursor type is not what we want,
dc6f92b8 10463 erase it. */
06a2c219 10464 if (w->phys_cursor_on_p
dc6f92b8 10465 && (!on
06a2c219
GM
10466 || w->phys_cursor.x != x
10467 || w->phys_cursor.y != y
10468 || new_cursor_type != w->phys_cursor_type))
10469 x_erase_phys_cursor (w);
10470
10471 /* If the cursor is now invisible and we want it to be visible,
10472 display it. */
10473 if (on && !w->phys_cursor_on_p)
10474 {
10475 w->phys_cursor_ascent = glyph_row->ascent;
10476 w->phys_cursor_height = glyph_row->height;
10477
10478 /* Set phys_cursor_.* before x_draw_.* is called because some
10479 of them may need the information. */
10480 w->phys_cursor.x = x;
10481 w->phys_cursor.y = glyph_row->y;
10482 w->phys_cursor.hpos = hpos;
10483 w->phys_cursor.vpos = vpos;
10484 w->phys_cursor_type = new_cursor_type;
10485 w->phys_cursor_on_p = 1;
10486
10487 switch (new_cursor_type)
dc6f92b8 10488 {
06a2c219
GM
10489 case HOLLOW_BOX_CURSOR:
10490 x_draw_hollow_cursor (w, glyph_row);
10491 break;
10492
10493 case FILLED_BOX_CURSOR:
10494 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10495 break;
10496
10497 case BAR_CURSOR:
f02d8aa0 10498 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
06a2c219
GM
10499 break;
10500
10501 case NO_CURSOR:
10502 break;
dc6f92b8 10503
06a2c219
GM
10504 default:
10505 abort ();
10506 }
59ddecde
GM
10507
10508#ifdef HAVE_X_I18N
10509 if (w == XWINDOW (f->selected_window))
10510 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
10511 xic_set_preeditarea (w, x, y);
10512#endif
dc6f92b8
JB
10513 }
10514
06a2c219 10515#ifndef XFlush
f676886a 10516 if (updating_frame != f)
334208b7 10517 XFlush (FRAME_X_DISPLAY (f));
06a2c219 10518#endif
dc6f92b8
JB
10519}
10520
06a2c219
GM
10521
10522/* Display the cursor on window W, or clear it. X and Y are window
10523 relative pixel coordinates. HPOS and VPOS are glyph matrix
10524 positions. If W is not the selected window, display a hollow
10525 cursor. ON non-zero means display the cursor at X, Y which
10526 correspond to HPOS, VPOS, otherwise it is cleared. */
5d46f928 10527
dfcf069d 10528void
06a2c219
GM
10529x_display_cursor (w, on, hpos, vpos, x, y)
10530 struct window *w;
10531 int on, hpos, vpos, x, y;
dc6f92b8 10532{
f94397b5 10533 BLOCK_INPUT;
06a2c219 10534 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
5d46f928
RS
10535 UNBLOCK_INPUT;
10536}
10537
06a2c219
GM
10538
10539/* Display the cursor on window W, or clear it, according to ON_P.
5d46f928
RS
10540 Don't change the cursor's position. */
10541
dfcf069d 10542void
06a2c219 10543x_update_cursor (f, on_p)
5d46f928 10544 struct frame *f;
5d46f928 10545{
06a2c219
GM
10546 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
10547}
10548
10549
10550/* Call x_update_window_cursor with parameter ON_P on all leaf windows
10551 in the window tree rooted at W. */
10552
10553static void
10554x_update_cursor_in_window_tree (w, on_p)
10555 struct window *w;
10556 int on_p;
10557{
10558 while (w)
10559 {
10560 if (!NILP (w->hchild))
10561 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10562 else if (!NILP (w->vchild))
10563 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10564 else
10565 x_update_window_cursor (w, on_p);
10566
10567 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10568 }
10569}
5d46f928 10570
f94397b5 10571
06a2c219
GM
10572/* Switch the display of W's cursor on or off, according to the value
10573 of ON. */
10574
10575static void
10576x_update_window_cursor (w, on)
10577 struct window *w;
10578 int on;
10579{
16b5d424
GM
10580 /* Don't update cursor in windows whose frame is in the process
10581 of being deleted. */
10582 if (w->current_matrix)
10583 {
10584 BLOCK_INPUT;
10585 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
10586 w->phys_cursor.x, w->phys_cursor.y);
10587 UNBLOCK_INPUT;
10588 }
dc6f92b8 10589}
06a2c219
GM
10590
10591
10592
dc6f92b8
JB
10593\f
10594/* Icons. */
10595
f676886a 10596/* Refresh bitmap kitchen sink icon for frame F
06a2c219 10597 when we get an expose event for it. */
dc6f92b8 10598
dfcf069d 10599void
f676886a
JB
10600refreshicon (f)
10601 struct frame *f;
dc6f92b8 10602{
06a2c219 10603 /* Normally, the window manager handles this function. */
dc6f92b8
JB
10604}
10605
dbc4e1c1 10606/* Make the x-window of frame F use the gnu icon bitmap. */
dc6f92b8
JB
10607
10608int
990ba854 10609x_bitmap_icon (f, file)
f676886a 10610 struct frame *f;
990ba854 10611 Lisp_Object file;
dc6f92b8 10612{
06a2c219 10613 int bitmap_id;
dc6f92b8 10614
c118dd06 10615 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
10616 return 1;
10617
990ba854 10618 /* Free up our existing icon bitmap if any. */
7556890b
RS
10619 if (f->output_data.x->icon_bitmap > 0)
10620 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10621 f->output_data.x->icon_bitmap = 0;
990ba854
RS
10622
10623 if (STRINGP (file))
7f2ae036
RS
10624 bitmap_id = x_create_bitmap_from_file (f, file);
10625 else
10626 {
990ba854 10627 /* Create the GNU bitmap if necessary. */
5bf01b68 10628 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
334208b7
RS
10629 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
10630 = x_create_bitmap_from_data (f, gnu_bits,
10631 gnu_width, gnu_height);
990ba854
RS
10632
10633 /* The first time we create the GNU bitmap,
06a2c219 10634 this increments the ref-count one extra time.
990ba854
RS
10635 As a result, the GNU bitmap is never freed.
10636 That way, we don't have to worry about allocating it again. */
334208b7 10637 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
990ba854 10638
334208b7 10639 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7f2ae036
RS
10640 }
10641
10642 x_wm_set_icon_pixmap (f, bitmap_id);
7556890b 10643 f->output_data.x->icon_bitmap = bitmap_id;
dc6f92b8
JB
10644
10645 return 0;
10646}
10647
10648
1be2d067
KH
10649/* Make the x-window of frame F use a rectangle with text.
10650 Use ICON_NAME as the text. */
dc6f92b8
JB
10651
10652int
f676886a
JB
10653x_text_icon (f, icon_name)
10654 struct frame *f;
dc6f92b8
JB
10655 char *icon_name;
10656{
c118dd06 10657 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
10658 return 1;
10659
1be2d067
KH
10660#ifdef HAVE_X11R4
10661 {
10662 XTextProperty text;
10663 text.value = (unsigned char *) icon_name;
10664 text.encoding = XA_STRING;
10665 text.format = 8;
10666 text.nitems = strlen (icon_name);
10667#ifdef USE_X_TOOLKIT
7556890b 10668 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
1be2d067
KH
10669 &text);
10670#else /* not USE_X_TOOLKIT */
10671 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
10672#endif /* not USE_X_TOOLKIT */
10673 }
10674#else /* not HAVE_X11R4 */
10675 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
10676#endif /* not HAVE_X11R4 */
58769bee 10677
7556890b
RS
10678 if (f->output_data.x->icon_bitmap > 0)
10679 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10680 f->output_data.x->icon_bitmap = 0;
b1c884c3 10681 x_wm_set_icon_pixmap (f, 0);
dc6f92b8
JB
10682
10683 return 0;
10684}
10685\f
e99db5a1
RS
10686#define X_ERROR_MESSAGE_SIZE 200
10687
10688/* If non-nil, this should be a string.
10689 It means catch X errors and store the error message in this string. */
10690
10691static Lisp_Object x_error_message_string;
10692
10693/* An X error handler which stores the error message in
10694 x_error_message_string. This is called from x_error_handler if
10695 x_catch_errors is in effect. */
10696
06a2c219 10697static void
e99db5a1
RS
10698x_error_catcher (display, error)
10699 Display *display;
10700 XErrorEvent *error;
10701{
10702 XGetErrorText (display, error->error_code,
10703 XSTRING (x_error_message_string)->data,
10704 X_ERROR_MESSAGE_SIZE);
10705}
10706
10707/* Begin trapping X errors for display DPY. Actually we trap X errors
10708 for all displays, but DPY should be the display you are actually
10709 operating on.
10710
10711 After calling this function, X protocol errors no longer cause
10712 Emacs to exit; instead, they are recorded in the string
10713 stored in x_error_message_string.
10714
10715 Calling x_check_errors signals an Emacs error if an X error has
10716 occurred since the last call to x_catch_errors or x_check_errors.
10717
10718 Calling x_uncatch_errors resumes the normal error handling. */
10719
10720void x_check_errors ();
10721static Lisp_Object x_catch_errors_unwind ();
10722
10723int
10724x_catch_errors (dpy)
10725 Display *dpy;
10726{
10727 int count = specpdl_ptr - specpdl;
10728
10729 /* Make sure any errors from previous requests have been dealt with. */
10730 XSync (dpy, False);
10731
10732 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
10733
10734 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
10735 XSTRING (x_error_message_string)->data[0] = 0;
10736
10737 return count;
10738}
10739
10740/* Unbind the binding that we made to check for X errors. */
10741
10742static Lisp_Object
10743x_catch_errors_unwind (old_val)
10744 Lisp_Object old_val;
10745{
10746 x_error_message_string = old_val;
10747 return Qnil;
10748}
10749
10750/* If any X protocol errors have arrived since the last call to
10751 x_catch_errors or x_check_errors, signal an Emacs error using
10752 sprintf (a buffer, FORMAT, the x error message text) as the text. */
10753
10754void
10755x_check_errors (dpy, format)
10756 Display *dpy;
10757 char *format;
10758{
10759 /* Make sure to catch any errors incurred so far. */
10760 XSync (dpy, False);
10761
10762 if (XSTRING (x_error_message_string)->data[0])
10763 error (format, XSTRING (x_error_message_string)->data);
10764}
10765
9829ddba
RS
10766/* Nonzero if we had any X protocol errors
10767 since we did x_catch_errors on DPY. */
e99db5a1
RS
10768
10769int
10770x_had_errors_p (dpy)
10771 Display *dpy;
10772{
10773 /* Make sure to catch any errors incurred so far. */
10774 XSync (dpy, False);
10775
10776 return XSTRING (x_error_message_string)->data[0] != 0;
10777}
10778
9829ddba
RS
10779/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
10780
06a2c219 10781void
9829ddba
RS
10782x_clear_errors (dpy)
10783 Display *dpy;
10784{
10785 XSTRING (x_error_message_string)->data[0] = 0;
10786}
10787
e99db5a1
RS
10788/* Stop catching X protocol errors and let them make Emacs die.
10789 DPY should be the display that was passed to x_catch_errors.
10790 COUNT should be the value that was returned by
10791 the corresponding call to x_catch_errors. */
10792
10793void
10794x_uncatch_errors (dpy, count)
10795 Display *dpy;
10796 int count;
10797{
10798 unbind_to (count, Qnil);
10799}
10800
10801#if 0
10802static unsigned int x_wire_count;
10803x_trace_wire ()
10804{
10805 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
10806}
10807#endif /* ! 0 */
10808
10809\f
10810/* Handle SIGPIPE, which can happen when the connection to a server
10811 simply goes away. SIGPIPE is handled by x_connection_signal.
10812 Don't need to do anything, because the write which caused the
10813 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
06a2c219 10814 which will do the appropriate cleanup for us. */
e99db5a1
RS
10815
10816static SIGTYPE
10817x_connection_signal (signalnum) /* If we don't have an argument, */
06a2c219 10818 int signalnum; /* some compilers complain in signal calls. */
e99db5a1
RS
10819{
10820#ifdef USG
10821 /* USG systems forget handlers when they are used;
10822 must reestablish each time */
10823 signal (signalnum, x_connection_signal);
10824#endif /* USG */
10825}
10826\f
4746118a
JB
10827/* Handling X errors. */
10828
7a13e894 10829/* Handle the loss of connection to display DISPLAY. */
16bd92ea 10830
4746118a 10831static SIGTYPE
7a13e894
RS
10832x_connection_closed (display, error_message)
10833 Display *display;
10834 char *error_message;
4746118a 10835{
7a13e894
RS
10836 struct x_display_info *dpyinfo = x_display_info_for_display (display);
10837 Lisp_Object frame, tail;
10838
6186a4a0
RS
10839 /* Indicate that this display is dead. */
10840
2e465cdd 10841#if 0 /* Closing the display caused a bus error on OpenWindows. */
f613a4c8 10842#ifdef USE_X_TOOLKIT
adabc3a9 10843 XtCloseDisplay (display);
2e465cdd 10844#endif
f613a4c8 10845#endif
adabc3a9 10846
9e80b57d
KR
10847 if (dpyinfo)
10848 dpyinfo->display = 0;
6186a4a0 10849
06a2c219 10850 /* First delete frames whose mini-buffers are on frames
7a13e894
RS
10851 that are on the dead display. */
10852 FOR_EACH_FRAME (tail, frame)
10853 {
10854 Lisp_Object minibuf_frame;
10855 minibuf_frame
10856 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
f48f33ca
RS
10857 if (FRAME_X_P (XFRAME (frame))
10858 && FRAME_X_P (XFRAME (minibuf_frame))
10859 && ! EQ (frame, minibuf_frame)
10860 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7a13e894
RS
10861 Fdelete_frame (frame, Qt);
10862 }
10863
10864 /* Now delete all remaining frames on the dead display.
06a2c219 10865 We are now sure none of these is used as the mini-buffer
7a13e894
RS
10866 for another frame that we need to delete. */
10867 FOR_EACH_FRAME (tail, frame)
f48f33ca
RS
10868 if (FRAME_X_P (XFRAME (frame))
10869 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
07a7096a
KH
10870 {
10871 /* Set this to t so that Fdelete_frame won't get confused
10872 trying to find a replacement. */
10873 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
10874 Fdelete_frame (frame, Qt);
10875 }
7a13e894 10876
482a1bd2
KH
10877 if (dpyinfo)
10878 x_delete_display (dpyinfo);
7a13e894
RS
10879
10880 if (x_display_list == 0)
10881 {
f8d07b62 10882 fprintf (stderr, "%s\n", error_message);
7a13e894
RS
10883 shut_down_emacs (0, 0, Qnil);
10884 exit (70);
10885 }
12ba150f 10886
7a13e894
RS
10887 /* Ordinary stack unwind doesn't deal with these. */
10888#ifdef SIGIO
10889 sigunblock (sigmask (SIGIO));
10890#endif
10891 sigunblock (sigmask (SIGALRM));
10892 TOTALLY_UNBLOCK_INPUT;
10893
aa4d9a9e 10894 clear_waiting_for_input ();
7a13e894 10895 error ("%s", error_message);
4746118a
JB
10896}
10897
7a13e894
RS
10898/* This is the usual handler for X protocol errors.
10899 It kills all frames on the display that we got the error for.
10900 If that was the only one, it prints an error message and kills Emacs. */
10901
06a2c219 10902static void
c118dd06
JB
10903x_error_quitter (display, error)
10904 Display *display;
10905 XErrorEvent *error;
10906{
7a13e894 10907 char buf[256], buf1[356];
dc6f92b8 10908
58769bee 10909 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 10910 original error handler. */
dc6f92b8 10911
c118dd06 10912 XGetErrorText (display, error->error_code, buf, sizeof (buf));
0a0fdc70 10913 sprintf (buf1, "X protocol error: %s on protocol request %d",
c118dd06 10914 buf, error->request_code);
7a13e894 10915 x_connection_closed (display, buf1);
dc6f92b8
JB
10916}
10917
e99db5a1
RS
10918/* This is the first-level handler for X protocol errors.
10919 It calls x_error_quitter or x_error_catcher. */
7a13e894 10920
8922af5f 10921static int
e99db5a1 10922x_error_handler (display, error)
8922af5f 10923 Display *display;
e99db5a1 10924 XErrorEvent *error;
8922af5f 10925{
e99db5a1
RS
10926 if (! NILP (x_error_message_string))
10927 x_error_catcher (display, error);
10928 else
10929 x_error_quitter (display, error);
06a2c219 10930 return 0;
f9e24cb9 10931}
c118dd06 10932
e99db5a1
RS
10933/* This is the handler for X IO errors, always.
10934 It kills all frames on the display that we lost touch with.
10935 If that was the only one, it prints an error message and kills Emacs. */
7a13e894 10936
c118dd06 10937static int
e99db5a1 10938x_io_error_quitter (display)
c118dd06 10939 Display *display;
c118dd06 10940{
e99db5a1 10941 char buf[256];
dc6f92b8 10942
e99db5a1
RS
10943 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
10944 x_connection_closed (display, buf);
06a2c219 10945 return 0;
dc6f92b8 10946}
dc6f92b8 10947\f
f451eb13
JB
10948/* Changing the font of the frame. */
10949
76bcdf39
RS
10950/* Give frame F the font named FONTNAME as its default font, and
10951 return the full name of that font. FONTNAME may be a wildcard
10952 pattern; in that case, we choose some font that fits the pattern.
10953 The return value shows which font we chose. */
10954
b5cf7a0e 10955Lisp_Object
f676886a
JB
10956x_new_font (f, fontname)
10957 struct frame *f;
dc6f92b8
JB
10958 register char *fontname;
10959{
dc43ef94 10960 struct font_info *fontp
ee569018 10961 = FS_LOAD_FONT (f, 0, fontname, -1);
dc6f92b8 10962
dc43ef94
KH
10963 if (!fontp)
10964 return Qnil;
2224a5fc 10965
dc43ef94 10966 f->output_data.x->font = (XFontStruct *) (fontp->font);
b4192550 10967 f->output_data.x->baseline_offset = fontp->baseline_offset;
dc43ef94
KH
10968 f->output_data.x->fontset = -1;
10969
b2cad826
KH
10970 /* Compute the scroll bar width in character columns. */
10971 if (f->scroll_bar_pixel_width > 0)
10972 {
7556890b 10973 int wid = FONT_WIDTH (f->output_data.x->font);
b2cad826
KH
10974 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
10975 }
10976 else
4e61bddf
RS
10977 {
10978 int wid = FONT_WIDTH (f->output_data.x->font);
10979 f->scroll_bar_cols = (14 + wid - 1) / wid;
10980 }
b2cad826 10981
f676886a 10982 /* Now make the frame display the given font. */
c118dd06 10983 if (FRAME_X_WINDOW (f) != 0)
dc6f92b8 10984 {
7556890b
RS
10985 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
10986 f->output_data.x->font->fid);
10987 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
10988 f->output_data.x->font->fid);
10989 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
10990 f->output_data.x->font->fid);
f676886a 10991
a27f9f86 10992 frame_update_line_height (f);
0134a210 10993 x_set_window_size (f, 0, f->width, f->height);
dc6f92b8 10994 }
a27f9f86
RS
10995 else
10996 /* If we are setting a new frame's font for the first time,
10997 there are no faces yet, so this font's height is the line height. */
7556890b 10998 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
dc6f92b8 10999
dc43ef94
KH
11000 return build_string (fontp->full_name);
11001}
11002
11003/* Give frame F the fontset named FONTSETNAME as its default font, and
11004 return the full name of that fontset. FONTSETNAME may be a wildcard
b5210ea7
KH
11005 pattern; in that case, we choose some fontset that fits the pattern.
11006 The return value shows which fontset we chose. */
b5cf7a0e 11007
dc43ef94
KH
11008Lisp_Object
11009x_new_fontset (f, fontsetname)
11010 struct frame *f;
11011 char *fontsetname;
11012{
ee569018 11013 int fontset = fs_query_fontset (build_string (fontsetname), 0);
dc43ef94 11014 Lisp_Object result;
b5cf7a0e 11015
dc43ef94
KH
11016 if (fontset < 0)
11017 return Qnil;
b5cf7a0e 11018
2da424f1
KH
11019 if (f->output_data.x->fontset == fontset)
11020 /* This fontset is already set in frame F. There's nothing more
11021 to do. */
ee569018 11022 return fontset_name (fontset);
dc43ef94 11023
ee569018 11024 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
dc43ef94
KH
11025
11026 if (!STRINGP (result))
11027 /* Can't load ASCII font. */
11028 return Qnil;
11029
11030 /* Since x_new_font doesn't update any fontset information, do it now. */
11031 f->output_data.x->fontset = fontset;
dc43ef94 11032
f5d11644
GM
11033#ifdef HAVE_X_I18N
11034 if (FRAME_XIC (f)
11035 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
ee569018 11036 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
f5d11644
GM
11037#endif
11038
dc43ef94 11039 return build_string (fontsetname);
dc6f92b8 11040}
f5d11644
GM
11041
11042\f
11043/***********************************************************************
11044 X Input Methods
11045 ***********************************************************************/
11046
11047#ifdef HAVE_X_I18N
11048
11049#ifdef HAVE_X11R6
11050
11051/* XIM destroy callback function, which is called whenever the
11052 connection to input method XIM dies. CLIENT_DATA contains a
11053 pointer to the x_display_info structure corresponding to XIM. */
11054
11055static void
11056xim_destroy_callback (xim, client_data, call_data)
11057 XIM xim;
11058 XPointer client_data;
11059 XPointer call_data;
11060{
11061 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
11062 Lisp_Object frame, tail;
11063
11064 BLOCK_INPUT;
11065
11066 /* No need to call XDestroyIC.. */
11067 FOR_EACH_FRAME (tail, frame)
11068 {
11069 struct frame *f = XFRAME (frame);
11070 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
11071 {
11072 FRAME_XIC (f) = NULL;
11073 if (FRAME_XIC_FONTSET (f))
11074 {
11075 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
11076 FRAME_XIC_FONTSET (f) = NULL;
11077 }
11078 }
11079 }
11080
11081 /* No need to call XCloseIM. */
11082 dpyinfo->xim = NULL;
11083 XFree (dpyinfo->xim_styles);
11084 UNBLOCK_INPUT;
11085}
11086
11087#endif /* HAVE_X11R6 */
11088
11089/* Open the connection to the XIM server on display DPYINFO.
11090 RESOURCE_NAME is the resource name Emacs uses. */
11091
11092static void
11093xim_open_dpy (dpyinfo, resource_name)
11094 struct x_display_info *dpyinfo;
11095 char *resource_name;
11096{
11097 XIM xim;
11098
11099 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
11100 dpyinfo->xim = xim;
11101
11102 if (xim)
11103 {
f5d11644
GM
11104#ifdef HAVE_X11R6
11105 XIMCallback destroy;
11106#endif
11107
11108 /* Get supported styles and XIM values. */
11109 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
11110
11111#ifdef HAVE_X11R6
11112 destroy.callback = xim_destroy_callback;
11113 destroy.client_data = (XPointer)dpyinfo;
11114 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
11115#endif
11116 }
11117}
11118
11119
b9de836c 11120#ifdef HAVE_X11R6_XIM
f5d11644
GM
11121
11122struct xim_inst_t
11123{
11124 struct x_display_info *dpyinfo;
11125 char *resource_name;
11126};
11127
11128/* XIM instantiate callback function, which is called whenever an XIM
11129 server is available. DISPLAY is teh display of the XIM.
11130 CLIENT_DATA contains a pointer to an xim_inst_t structure created
11131 when the callback was registered. */
11132
11133static void
11134xim_instantiate_callback (display, client_data, call_data)
11135 Display *display;
11136 XPointer client_data;
11137 XPointer call_data;
11138{
11139 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
11140 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
11141
11142 /* We don't support multiple XIM connections. */
11143 if (dpyinfo->xim)
11144 return;
11145
11146 xim_open_dpy (dpyinfo, xim_inst->resource_name);
11147
11148 /* Create XIC for the existing frames on the same display, as long
11149 as they have no XIC. */
11150 if (dpyinfo->xim && dpyinfo->reference_count > 0)
11151 {
11152 Lisp_Object tail, frame;
11153
11154 BLOCK_INPUT;
11155 FOR_EACH_FRAME (tail, frame)
11156 {
11157 struct frame *f = XFRAME (frame);
11158
11159 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
11160 if (FRAME_XIC (f) == NULL)
11161 {
11162 create_frame_xic (f);
11163 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
11164 xic_set_statusarea (f);
11165 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
11166 {
11167 struct window *w = XWINDOW (f->selected_window);
11168 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
11169 }
11170 }
11171 }
11172
11173 UNBLOCK_INPUT;
11174 }
11175}
11176
b9de836c 11177#endif /* HAVE_X11R6_XIM */
f5d11644
GM
11178
11179
11180/* Open a connection to the XIM server on display DPYINFO.
11181 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
11182 connection only at the first time. On X11R6, open the connection
11183 in the XIM instantiate callback function. */
11184
11185static void
11186xim_initialize (dpyinfo, resource_name)
11187 struct x_display_info *dpyinfo;
11188 char *resource_name;
11189{
b9de836c 11190#ifdef HAVE_X11R6_XIM
f5d11644
GM
11191 struct xim_inst_t *xim_inst;
11192 int len;
11193
11194 dpyinfo->xim = NULL;
11195 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
11196 xim_inst->dpyinfo = dpyinfo;
11197 len = strlen (resource_name);
11198 xim_inst->resource_name = (char *) xmalloc (len + 1);
11199 bcopy (resource_name, xim_inst->resource_name, len + 1);
11200 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11201 resource_name, EMACS_CLASS,
11202 xim_instantiate_callback,
2ebb2f8b
DL
11203 /* Fixme: This is XPointer in
11204 XFree86 but (XPointer *) on
11205 Tru64, at least. */
11206 (XPointer) xim_inst);
b9de836c 11207#else /* not HAVE_X11R6_XIM */
f5d11644
GM
11208 dpyinfo->xim = NULL;
11209 xim_open_dpy (dpyinfo, resource_name);
b9de836c 11210#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
11211}
11212
11213
11214/* Close the connection to the XIM server on display DPYINFO. */
11215
11216static void
11217xim_close_dpy (dpyinfo)
11218 struct x_display_info *dpyinfo;
11219{
b9de836c 11220#ifdef HAVE_X11R6_XIM
f5d11644
GM
11221 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11222 NULL, EMACS_CLASS,
11223 xim_instantiate_callback, NULL);
b9de836c 11224#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
11225 XCloseIM (dpyinfo->xim);
11226 dpyinfo->xim = NULL;
11227 XFree (dpyinfo->xim_styles);
11228}
11229
b9de836c 11230#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
11231
11232
dc6f92b8 11233\f
2e365682
RS
11234/* Calculate the absolute position in frame F
11235 from its current recorded position values and gravity. */
11236
dfcf069d 11237void
43bca5d5 11238x_calc_absolute_position (f)
f676886a 11239 struct frame *f;
dc6f92b8 11240{
06a2c219 11241 Window child;
6dba1858 11242 int win_x = 0, win_y = 0;
7556890b 11243 int flags = f->output_data.x->size_hint_flags;
c81412a0
KH
11244 int this_window;
11245
9829ddba
RS
11246 /* We have nothing to do if the current position
11247 is already for the top-left corner. */
11248 if (! ((flags & XNegative) || (flags & YNegative)))
11249 return;
11250
c81412a0 11251#ifdef USE_X_TOOLKIT
7556890b 11252 this_window = XtWindow (f->output_data.x->widget);
c81412a0
KH
11253#else
11254 this_window = FRAME_X_WINDOW (f);
11255#endif
6dba1858
RS
11256
11257 /* Find the position of the outside upper-left corner of
9829ddba
RS
11258 the inner window, with respect to the outer window.
11259 But do this only if we will need the results. */
7556890b 11260 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6dba1858 11261 {
9829ddba
RS
11262 int count;
11263
6dba1858 11264 BLOCK_INPUT;
9829ddba
RS
11265 count = x_catch_errors (FRAME_X_DISPLAY (f));
11266 while (1)
11267 {
11268 x_clear_errors (FRAME_X_DISPLAY (f));
11269 XTranslateCoordinates (FRAME_X_DISPLAY (f),
11270
11271 /* From-window, to-window. */
11272 this_window,
11273 f->output_data.x->parent_desc,
11274
11275 /* From-position, to-position. */
11276 0, 0, &win_x, &win_y,
11277
11278 /* Child of win. */
11279 &child);
11280 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
11281 {
11282 Window newroot, newparent = 0xdeadbeef;
11283 Window *newchildren;
2ebb2f8b 11284 unsigned int nchildren;
9829ddba
RS
11285
11286 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
11287 &newparent, &newchildren, &nchildren))
11288 break;
58769bee 11289
7c3c78a3 11290 XFree ((char *) newchildren);
6dba1858 11291
9829ddba
RS
11292 f->output_data.x->parent_desc = newparent;
11293 }
11294 else
11295 break;
11296 }
6dba1858 11297
9829ddba 11298 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
6dba1858
RS
11299 UNBLOCK_INPUT;
11300 }
11301
11302 /* Treat negative positions as relative to the leftmost bottommost
11303 position that fits on the screen. */
20f55f9a 11304 if (flags & XNegative)
7556890b 11305 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
2e365682
RS
11306 - 2 * f->output_data.x->border_width - win_x
11307 - PIXEL_WIDTH (f)
11308 + f->output_data.x->left_pos);
dc6f92b8 11309
20f55f9a 11310 if (flags & YNegative)
06a2c219
GM
11311 {
11312 int menubar_height = 0;
11313
11314#ifdef USE_X_TOOLKIT
11315 if (f->output_data.x->menubar_widget)
11316 menubar_height
11317 = (f->output_data.x->menubar_widget->core.height
11318 + f->output_data.x->menubar_widget->core.border_width);
11319#endif
11320
11321 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
11322 - 2 * f->output_data.x->border_width
11323 - win_y
11324 - PIXEL_HEIGHT (f)
11325 - menubar_height
11326 + f->output_data.x->top_pos);
11327 }
2e365682 11328
3a35ab44
RS
11329 /* The left_pos and top_pos
11330 are now relative to the top and left screen edges,
11331 so the flags should correspond. */
7556890b 11332 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
dc6f92b8
JB
11333}
11334
3a35ab44
RS
11335/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
11336 to really change the position, and 0 when calling from
11337 x_make_frame_visible (in that case, XOFF and YOFF are the current
aa3ff7c9
KH
11338 position values). It is -1 when calling from x_set_frame_parameters,
11339 which means, do adjust for borders but don't change the gravity. */
3a35ab44 11340
dfcf069d 11341void
dc05a16b 11342x_set_offset (f, xoff, yoff, change_gravity)
f676886a 11343 struct frame *f;
dc6f92b8 11344 register int xoff, yoff;
dc05a16b 11345 int change_gravity;
dc6f92b8 11346{
4a4cbdd5
KH
11347 int modified_top, modified_left;
11348
aa3ff7c9 11349 if (change_gravity > 0)
3a35ab44 11350 {
7556890b
RS
11351 f->output_data.x->top_pos = yoff;
11352 f->output_data.x->left_pos = xoff;
11353 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
3a35ab44 11354 if (xoff < 0)
7556890b 11355 f->output_data.x->size_hint_flags |= XNegative;
3a35ab44 11356 if (yoff < 0)
7556890b
RS
11357 f->output_data.x->size_hint_flags |= YNegative;
11358 f->output_data.x->win_gravity = NorthWestGravity;
3a35ab44 11359 }
43bca5d5 11360 x_calc_absolute_position (f);
dc6f92b8
JB
11361
11362 BLOCK_INPUT;
c32cdd9a 11363 x_wm_set_size_hint (f, (long) 0, 0);
3a35ab44 11364
7556890b
RS
11365 modified_left = f->output_data.x->left_pos;
11366 modified_top = f->output_data.x->top_pos;
e73ec6fa
RS
11367#if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
11368 this seems to be unnecessary and incorrect. rms, 4/17/97. */
11369 /* It is a mystery why we need to add the border_width here
11370 when the frame is already visible, but experiment says we do. */
aa3ff7c9 11371 if (change_gravity != 0)
4a4cbdd5 11372 {
7556890b
RS
11373 modified_left += f->output_data.x->border_width;
11374 modified_top += f->output_data.x->border_width;
4a4cbdd5 11375 }
e73ec6fa 11376#endif
4a4cbdd5 11377
3afe33e7 11378#ifdef USE_X_TOOLKIT
7556890b 11379 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
4a4cbdd5 11380 modified_left, modified_top);
3afe33e7 11381#else /* not USE_X_TOOLKIT */
334208b7 11382 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4a4cbdd5 11383 modified_left, modified_top);
3afe33e7 11384#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11385 UNBLOCK_INPUT;
11386}
11387
bc20ebbf
FP
11388/* Call this to change the size of frame F's x-window.
11389 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
11390 for this size change and subsequent size changes.
11391 Otherwise we leave the window gravity unchanged. */
dc6f92b8 11392
dfcf069d 11393void
bc20ebbf 11394x_set_window_size (f, change_gravity, cols, rows)
f676886a 11395 struct frame *f;
bc20ebbf 11396 int change_gravity;
b1c884c3 11397 int cols, rows;
dc6f92b8 11398{
06a2c219 11399#ifndef USE_X_TOOLKIT
dc6f92b8 11400 int pixelwidth, pixelheight;
06a2c219 11401#endif
dc6f92b8 11402
80fd1fe2 11403 BLOCK_INPUT;
aee9a898
RS
11404
11405#ifdef USE_X_TOOLKIT
3a20653d
RS
11406 {
11407 /* The x and y position of the widget is clobbered by the
11408 call to XtSetValues within EmacsFrameSetCharSize.
11409 This is a real kludge, but I don't understand Xt so I can't
11410 figure out a correct fix. Can anyone else tell me? -- rms. */
7556890b
RS
11411 int xpos = f->output_data.x->widget->core.x;
11412 int ypos = f->output_data.x->widget->core.y;
11413 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
11414 f->output_data.x->widget->core.x = xpos;
11415 f->output_data.x->widget->core.y = ypos;
3a20653d 11416 }
80fd1fe2
FP
11417
11418#else /* not USE_X_TOOLKIT */
11419
b1c884c3 11420 check_frame_size (f, &rows, &cols);
7556890b 11421 f->output_data.x->vertical_scroll_bar_extra
b2cad826
KH
11422 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
11423 ? 0
11424 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
480407eb 11425 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7556890b 11426 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
06a2c219 11427 f->output_data.x->flags_areas_extra
110859fc 11428 = FRAME_FLAGS_AREA_WIDTH (f);
f451eb13
JB
11429 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
11430 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
dc6f92b8 11431
7556890b 11432 f->output_data.x->win_gravity = NorthWestGravity;
c32cdd9a 11433 x_wm_set_size_hint (f, (long) 0, 0);
6ccf47d1 11434
334208b7
RS
11435 XSync (FRAME_X_DISPLAY (f), False);
11436 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11437 pixelwidth, pixelheight);
b1c884c3
JB
11438
11439 /* Now, strictly speaking, we can't be sure that this is accurate,
11440 but the window manager will get around to dealing with the size
11441 change request eventually, and we'll hear how it went when the
dbc4e1c1
JB
11442 ConfigureNotify event gets here.
11443
11444 We could just not bother storing any of this information here,
11445 and let the ConfigureNotify event set everything up, but that
fddd5ceb 11446 might be kind of confusing to the Lisp code, since size changes
dbc4e1c1 11447 wouldn't be reported in the frame parameters until some random
fddd5ceb
RS
11448 point in the future when the ConfigureNotify event arrives.
11449
11450 We pass 1 for DELAY since we can't run Lisp code inside of
11451 a BLOCK_INPUT. */
7d1e984f 11452 change_frame_size (f, rows, cols, 0, 1, 0);
b1c884c3
JB
11453 PIXEL_WIDTH (f) = pixelwidth;
11454 PIXEL_HEIGHT (f) = pixelheight;
11455
aee9a898
RS
11456 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11457 receive in the ConfigureNotify event; if we get what we asked
11458 for, then the event won't cause the screen to become garbaged, so
11459 we have to make sure to do it here. */
11460 SET_FRAME_GARBAGED (f);
11461
11462 XFlush (FRAME_X_DISPLAY (f));
11463
11464#endif /* not USE_X_TOOLKIT */
11465
4d73d038 11466 /* If cursor was outside the new size, mark it as off. */
06a2c219 11467 mark_window_cursors_off (XWINDOW (f->root_window));
4d73d038 11468
aee9a898
RS
11469 /* Clear out any recollection of where the mouse highlighting was,
11470 since it might be in a place that's outside the new frame size.
11471 Actually checking whether it is outside is a pain in the neck,
11472 so don't try--just let the highlighting be done afresh with new size. */
e687d06e 11473 cancel_mouse_face (f);
dbc4e1c1 11474
dc6f92b8
JB
11475 UNBLOCK_INPUT;
11476}
dc6f92b8 11477\f
d047c4eb 11478/* Mouse warping. */
dc6f92b8 11479
9b378208 11480void
f676886a
JB
11481x_set_mouse_position (f, x, y)
11482 struct frame *f;
dc6f92b8
JB
11483 int x, y;
11484{
11485 int pix_x, pix_y;
11486
7556890b
RS
11487 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
11488 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
f451eb13
JB
11489
11490 if (pix_x < 0) pix_x = 0;
11491 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
11492
11493 if (pix_y < 0) pix_y = 0;
11494 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
dc6f92b8
JB
11495
11496 BLOCK_INPUT;
dc6f92b8 11497
334208b7
RS
11498 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11499 0, 0, 0, 0, pix_x, pix_y);
dc6f92b8
JB
11500 UNBLOCK_INPUT;
11501}
11502
9b378208
RS
11503/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11504
11505void
11506x_set_mouse_pixel_position (f, pix_x, pix_y)
11507 struct frame *f;
11508 int pix_x, pix_y;
11509{
11510 BLOCK_INPUT;
11511
334208b7
RS
11512 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11513 0, 0, 0, 0, pix_x, pix_y);
9b378208
RS
11514 UNBLOCK_INPUT;
11515}
d047c4eb
KH
11516\f
11517/* focus shifting, raising and lowering. */
9b378208 11518
dfcf069d 11519void
f676886a
JB
11520x_focus_on_frame (f)
11521 struct frame *f;
dc6f92b8 11522{
1fb20991 11523#if 0 /* This proves to be unpleasant. */
f676886a 11524 x_raise_frame (f);
1fb20991 11525#endif
6d4238f3
JB
11526#if 0
11527 /* I don't think that the ICCCM allows programs to do things like this
11528 without the interaction of the window manager. Whatever you end up
f676886a 11529 doing with this code, do it to x_unfocus_frame too. */
334208b7 11530 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dc6f92b8 11531 RevertToPointerRoot, CurrentTime);
c118dd06 11532#endif /* ! 0 */
dc6f92b8
JB
11533}
11534
dfcf069d 11535void
f676886a
JB
11536x_unfocus_frame (f)
11537 struct frame *f;
dc6f92b8 11538{
6d4238f3 11539#if 0
f676886a 11540 /* Look at the remarks in x_focus_on_frame. */
0f941935 11541 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
334208b7 11542 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
dc6f92b8 11543 RevertToPointerRoot, CurrentTime);
c118dd06 11544#endif /* ! 0 */
dc6f92b8
JB
11545}
11546
f676886a 11547/* Raise frame F. */
dc6f92b8 11548
dfcf069d 11549void
f676886a
JB
11550x_raise_frame (f)
11551 struct frame *f;
dc6f92b8 11552{
3a88c238 11553 if (f->async_visible)
dc6f92b8
JB
11554 {
11555 BLOCK_INPUT;
3afe33e7 11556#ifdef USE_X_TOOLKIT
7556890b 11557 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 11558#else /* not USE_X_TOOLKIT */
334208b7 11559 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11560#endif /* not USE_X_TOOLKIT */
334208b7 11561 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
11562 UNBLOCK_INPUT;
11563 }
11564}
11565
f676886a 11566/* Lower frame F. */
dc6f92b8 11567
dfcf069d 11568void
f676886a
JB
11569x_lower_frame (f)
11570 struct frame *f;
dc6f92b8 11571{
3a88c238 11572 if (f->async_visible)
dc6f92b8
JB
11573 {
11574 BLOCK_INPUT;
3afe33e7 11575#ifdef USE_X_TOOLKIT
7556890b 11576 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
3afe33e7 11577#else /* not USE_X_TOOLKIT */
334208b7 11578 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11579#endif /* not USE_X_TOOLKIT */
334208b7 11580 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
11581 UNBLOCK_INPUT;
11582 }
11583}
11584
dbc4e1c1 11585static void
6b0442dc 11586XTframe_raise_lower (f, raise_flag)
dbc4e1c1 11587 FRAME_PTR f;
6b0442dc 11588 int raise_flag;
dbc4e1c1 11589{
6b0442dc 11590 if (raise_flag)
dbc4e1c1
JB
11591 x_raise_frame (f);
11592 else
11593 x_lower_frame (f);
11594}
d047c4eb
KH
11595\f
11596/* Change of visibility. */
dc6f92b8 11597
9382638d
KH
11598/* This tries to wait until the frame is really visible.
11599 However, if the window manager asks the user where to position
11600 the frame, this will return before the user finishes doing that.
11601 The frame will not actually be visible at that time,
11602 but it will become visible later when the window manager
11603 finishes with it. */
11604
dfcf069d 11605void
f676886a
JB
11606x_make_frame_visible (f)
11607 struct frame *f;
dc6f92b8 11608{
990ba854 11609 Lisp_Object type;
1aa6072f 11610 int original_top, original_left;
dc6f92b8 11611
dc6f92b8 11612 BLOCK_INPUT;
dc6f92b8 11613
990ba854
RS
11614 type = x_icon_type (f);
11615 if (!NILP (type))
11616 x_bitmap_icon (f, type);
bdcd49ba 11617
f676886a 11618 if (! FRAME_VISIBLE_P (f))
90e65f07 11619 {
1aa6072f
RS
11620 /* We test FRAME_GARBAGED_P here to make sure we don't
11621 call x_set_offset a second time
11622 if we get to x_make_frame_visible a second time
11623 before the window gets really visible. */
11624 if (! FRAME_ICONIFIED_P (f)
11625 && ! f->output_data.x->asked_for_visible)
11626 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
11627
11628 f->output_data.x->asked_for_visible = 1;
11629
90e65f07 11630 if (! EQ (Vx_no_window_manager, Qt))
f676886a 11631 x_wm_set_window_state (f, NormalState);
3afe33e7 11632#ifdef USE_X_TOOLKIT
d7a38a2e 11633 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 11634 XtMapWidget (f->output_data.x->widget);
3afe33e7 11635#else /* not USE_X_TOOLKIT */
7f9c7f94 11636 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3afe33e7 11637#endif /* not USE_X_TOOLKIT */
0134a210
RS
11638#if 0 /* This seems to bring back scroll bars in the wrong places
11639 if the window configuration has changed. They seem
11640 to come back ok without this. */
ab648270 11641 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
334208b7 11642 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
0134a210 11643#endif
90e65f07 11644 }
dc6f92b8 11645
334208b7 11646 XFlush (FRAME_X_DISPLAY (f));
90e65f07 11647
0dacf791
RS
11648 /* Synchronize to ensure Emacs knows the frame is visible
11649 before we do anything else. We do this loop with input not blocked
11650 so that incoming events are handled. */
11651 {
11652 Lisp_Object frame;
12ce2351 11653 int count;
28c01ffe
RS
11654 /* This must be before UNBLOCK_INPUT
11655 since events that arrive in response to the actions above
11656 will set it when they are handled. */
11657 int previously_visible = f->output_data.x->has_been_visible;
1aa6072f
RS
11658
11659 original_left = f->output_data.x->left_pos;
11660 original_top = f->output_data.x->top_pos;
c0a04927
RS
11661
11662 /* This must come after we set COUNT. */
11663 UNBLOCK_INPUT;
11664
2745e6c4 11665 /* We unblock here so that arriving X events are processed. */
1aa6072f 11666
dcb07ae9
RS
11667 /* Now move the window back to where it was "supposed to be".
11668 But don't do it if the gravity is negative.
11669 When the gravity is negative, this uses a position
28c01ffe
RS
11670 that is 3 pixels too low. Perhaps that's really the border width.
11671
11672 Don't do this if the window has never been visible before,
11673 because the window manager may choose the position
11674 and we don't want to override it. */
1aa6072f 11675
4d3f5d9a 11676 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
06c488fd 11677 && f->output_data.x->win_gravity == NorthWestGravity
28c01ffe 11678 && previously_visible)
1aa6072f 11679 {
2745e6c4
RS
11680 Drawable rootw;
11681 int x, y;
11682 unsigned int width, height, border, depth;
06a2c219 11683
1aa6072f 11684 BLOCK_INPUT;
9829ddba 11685
06a2c219
GM
11686 /* On some window managers (such as FVWM) moving an existing
11687 window, even to the same place, causes the window manager
11688 to introduce an offset. This can cause the window to move
11689 to an unexpected location. Check the geometry (a little
11690 slow here) and then verify that the window is in the right
11691 place. If the window is not in the right place, move it
11692 there, and take the potential window manager hit. */
2745e6c4
RS
11693 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11694 &rootw, &x, &y, &width, &height, &border, &depth);
11695
11696 if (original_left != x || original_top != y)
11697 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11698 original_left, original_top);
11699
1aa6072f
RS
11700 UNBLOCK_INPUT;
11701 }
9829ddba 11702
e0c1aef2 11703 XSETFRAME (frame, f);
c0a04927 11704
12ce2351
GM
11705 /* Wait until the frame is visible. Process X events until a
11706 MapNotify event has been seen, or until we think we won't get a
11707 MapNotify at all.. */
11708 for (count = input_signal_count + 10;
11709 input_signal_count < count && !FRAME_VISIBLE_P (f);)
2a6cf806 11710 {
12ce2351 11711 /* Force processing of queued events. */
334208b7 11712 x_sync (f);
12ce2351
GM
11713
11714 /* Machines that do polling rather than SIGIO have been
11715 observed to go into a busy-wait here. So we'll fake an
11716 alarm signal to let the handler know that there's something
11717 to be read. We used to raise a real alarm, but it seems
11718 that the handler isn't always enabled here. This is
11719 probably a bug. */
8b2f8d4e 11720 if (input_polling_used ())
3b2fa4e6 11721 {
12ce2351
GM
11722 /* It could be confusing if a real alarm arrives while
11723 processing the fake one. Turn it off and let the
11724 handler reset it. */
3e71d8f2 11725 extern void poll_for_input_1 P_ ((void));
bffcfca9
GM
11726 int old_poll_suppress_count = poll_suppress_count;
11727 poll_suppress_count = 1;
11728 poll_for_input_1 ();
11729 poll_suppress_count = old_poll_suppress_count;
3b2fa4e6 11730 }
12ce2351
GM
11731
11732 /* See if a MapNotify event has been processed. */
11733 FRAME_SAMPLE_VISIBILITY (f);
2a6cf806 11734 }
0dacf791 11735 }
dc6f92b8
JB
11736}
11737
06a2c219 11738/* Change from mapped state to withdrawn state. */
dc6f92b8 11739
d047c4eb
KH
11740/* Make the frame visible (mapped and not iconified). */
11741
dfcf069d 11742void
f676886a
JB
11743x_make_frame_invisible (f)
11744 struct frame *f;
dc6f92b8 11745{
546e6d5b
RS
11746 Window window;
11747
11748#ifdef USE_X_TOOLKIT
11749 /* Use the frame's outermost window, not the one we normally draw on. */
7556890b 11750 window = XtWindow (f->output_data.x->widget);
546e6d5b
RS
11751#else /* not USE_X_TOOLKIT */
11752 window = FRAME_X_WINDOW (f);
11753#endif /* not USE_X_TOOLKIT */
dc6f92b8 11754
9319ae23 11755 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
11756 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11757 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 11758
5627c40e 11759#if 0/* This might add unreliability; I don't trust it -- rms. */
9319ae23 11760 if (! f->async_visible && ! f->async_iconified)
dc6f92b8 11761 return;
5627c40e 11762#endif
dc6f92b8
JB
11763
11764 BLOCK_INPUT;
c118dd06 11765
af31d76f
RS
11766 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
11767 that the current position of the window is user-specified, rather than
11768 program-specified, so that when the window is mapped again, it will be
11769 placed at the same location, without forcing the user to position it
11770 by hand again (they have already done that once for this window.) */
c32cdd9a 11771 x_wm_set_size_hint (f, (long) 0, 1);
af31d76f 11772
c118dd06
JB
11773#ifdef HAVE_X11R4
11774
334208b7
RS
11775 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
11776 DefaultScreen (FRAME_X_DISPLAY (f))))
c118dd06
JB
11777 {
11778 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11779 error ("Can't notify window manager of window withdrawal");
c118dd06 11780 }
c118dd06 11781#else /* ! defined (HAVE_X11R4) */
16bd92ea 11782
c118dd06 11783 /* Tell the window manager what we're going to do. */
dc6f92b8
JB
11784 if (! EQ (Vx_no_window_manager, Qt))
11785 {
16bd92ea 11786 XEvent unmap;
dc6f92b8 11787
16bd92ea 11788 unmap.xunmap.type = UnmapNotify;
546e6d5b 11789 unmap.xunmap.window = window;
334208b7 11790 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
16bd92ea 11791 unmap.xunmap.from_configure = False;
334208b7
RS
11792 if (! XSendEvent (FRAME_X_DISPLAY (f),
11793 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea 11794 False,
06a2c219 11795 SubstructureRedirectMaskSubstructureNotifyMask,
16bd92ea
JB
11796 &unmap))
11797 {
11798 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11799 error ("Can't notify window manager of withdrawal");
16bd92ea 11800 }
dc6f92b8
JB
11801 }
11802
16bd92ea 11803 /* Unmap the window ourselves. Cheeky! */
334208b7 11804 XUnmapWindow (FRAME_X_DISPLAY (f), window);
c118dd06 11805#endif /* ! defined (HAVE_X11R4) */
dc6f92b8 11806
5627c40e
RS
11807 /* We can't distinguish this from iconification
11808 just by the event that we get from the server.
11809 So we can't win using the usual strategy of letting
11810 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
11811 and synchronize with the server to make sure we agree. */
11812 f->visible = 0;
11813 FRAME_ICONIFIED_P (f) = 0;
11814 f->async_visible = 0;
11815 f->async_iconified = 0;
11816
334208b7 11817 x_sync (f);
5627c40e 11818
dc6f92b8
JB
11819 UNBLOCK_INPUT;
11820}
11821
06a2c219 11822/* Change window state from mapped to iconified. */
dc6f92b8 11823
dfcf069d 11824void
f676886a
JB
11825x_iconify_frame (f)
11826 struct frame *f;
dc6f92b8 11827{
3afe33e7 11828 int result;
990ba854 11829 Lisp_Object type;
dc6f92b8 11830
9319ae23 11831 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
11832 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11833 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 11834
3a88c238 11835 if (f->async_iconified)
dc6f92b8
JB
11836 return;
11837
3afe33e7 11838 BLOCK_INPUT;
546e6d5b 11839
9af3143a
RS
11840 FRAME_SAMPLE_VISIBILITY (f);
11841
990ba854
RS
11842 type = x_icon_type (f);
11843 if (!NILP (type))
11844 x_bitmap_icon (f, type);
bdcd49ba
RS
11845
11846#ifdef USE_X_TOOLKIT
11847
546e6d5b
RS
11848 if (! FRAME_VISIBLE_P (f))
11849 {
11850 if (! EQ (Vx_no_window_manager, Qt))
11851 x_wm_set_window_state (f, IconicState);
11852 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 11853 XtMapWidget (f->output_data.x->widget);
9cf30a30
RS
11854 /* The server won't give us any event to indicate
11855 that an invisible frame was changed to an icon,
11856 so we have to record it here. */
11857 f->iconified = 1;
1e6bc770 11858 f->visible = 1;
9cf30a30 11859 f->async_iconified = 1;
1e6bc770 11860 f->async_visible = 0;
546e6d5b
RS
11861 UNBLOCK_INPUT;
11862 return;
11863 }
11864
334208b7 11865 result = XIconifyWindow (FRAME_X_DISPLAY (f),
7556890b 11866 XtWindow (f->output_data.x->widget),
334208b7 11867 DefaultScreen (FRAME_X_DISPLAY (f)));
3afe33e7
RS
11868 UNBLOCK_INPUT;
11869
11870 if (!result)
546e6d5b 11871 error ("Can't notify window manager of iconification");
3afe33e7
RS
11872
11873 f->async_iconified = 1;
1e6bc770
RS
11874 f->async_visible = 0;
11875
8c002a25
KH
11876
11877 BLOCK_INPUT;
334208b7 11878 XFlush (FRAME_X_DISPLAY (f));
8c002a25 11879 UNBLOCK_INPUT;
3afe33e7
RS
11880#else /* not USE_X_TOOLKIT */
11881
fd13dbb2
RS
11882 /* Make sure the X server knows where the window should be positioned,
11883 in case the user deiconifies with the window manager. */
11884 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
7556890b 11885 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
fd13dbb2 11886
16bd92ea
JB
11887 /* Since we don't know which revision of X we're running, we'll use both
11888 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
11889
11890 /* X11R4: send a ClientMessage to the window manager using the
11891 WM_CHANGE_STATE type. */
11892 {
11893 XEvent message;
58769bee 11894
c118dd06 11895 message.xclient.window = FRAME_X_WINDOW (f);
16bd92ea 11896 message.xclient.type = ClientMessage;
334208b7 11897 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
16bd92ea
JB
11898 message.xclient.format = 32;
11899 message.xclient.data.l[0] = IconicState;
11900
334208b7
RS
11901 if (! XSendEvent (FRAME_X_DISPLAY (f),
11902 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea
JB
11903 False,
11904 SubstructureRedirectMask | SubstructureNotifyMask,
11905 &message))
dc6f92b8
JB
11906 {
11907 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11908 error ("Can't notify window manager of iconification");
dc6f92b8 11909 }
16bd92ea 11910 }
dc6f92b8 11911
58769bee 11912 /* X11R3: set the initial_state field of the window manager hints to
16bd92ea
JB
11913 IconicState. */
11914 x_wm_set_window_state (f, IconicState);
dc6f92b8 11915
a9c00105
RS
11916 if (!FRAME_VISIBLE_P (f))
11917 {
11918 /* If the frame was withdrawn, before, we must map it. */
7f9c7f94 11919 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
a9c00105
RS
11920 }
11921
3a88c238 11922 f->async_iconified = 1;
1e6bc770 11923 f->async_visible = 0;
dc6f92b8 11924
334208b7 11925 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 11926 UNBLOCK_INPUT;
8c002a25 11927#endif /* not USE_X_TOOLKIT */
dc6f92b8 11928}
d047c4eb 11929\f
c0ff3fab 11930/* Destroy the X window of frame F. */
dc6f92b8 11931
dfcf069d 11932void
c0ff3fab 11933x_destroy_window (f)
f676886a 11934 struct frame *f;
dc6f92b8 11935{
7f9c7f94
RS
11936 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11937
dc6f92b8 11938 BLOCK_INPUT;
c0ff3fab 11939
6186a4a0
RS
11940 /* If a display connection is dead, don't try sending more
11941 commands to the X server. */
11942 if (dpyinfo->display != 0)
11943 {
11944 if (f->output_data.x->icon_desc != 0)
11945 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
31f41daf 11946#ifdef HAVE_X_I18N
f5d11644
GM
11947 if (FRAME_XIC (f))
11948 free_frame_xic (f);
31f41daf 11949#endif
6186a4a0 11950 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->window_desc);
3afe33e7 11951#ifdef USE_X_TOOLKIT
06a2c219
GM
11952 if (f->output_data.x->widget)
11953 XtDestroyWidget (f->output_data.x->widget);
6186a4a0 11954 free_frame_menubar (f);
3afe33e7
RS
11955#endif /* USE_X_TOOLKIT */
11956
3e71d8f2
GM
11957 unload_color (f, f->output_data.x->foreground_pixel);
11958 unload_color (f, f->output_data.x->background_pixel);
11959 unload_color (f, f->output_data.x->cursor_pixel);
11960 unload_color (f, f->output_data.x->cursor_foreground_pixel);
11961 unload_color (f, f->output_data.x->border_pixel);
11962 unload_color (f, f->output_data.x->mouse_pixel);
11963 if (f->output_data.x->scroll_bar_background_pixel != -1)
11964 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
11965 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
11966 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
11967 if (f->output_data.x->white_relief.allocated_p)
11968 unload_color (f, f->output_data.x->white_relief.pixel);
11969 if (f->output_data.x->black_relief.allocated_p)
11970 unload_color (f, f->output_data.x->black_relief.pixel);
11971
6186a4a0
RS
11972 free_frame_faces (f);
11973 XFlush (FRAME_X_DISPLAY (f));
11974 }
dc6f92b8 11975
df89d8a4 11976 if (f->output_data.x->saved_menu_event)
06a2c219 11977 xfree (f->output_data.x->saved_menu_event);
0c49ce2f 11978
7556890b
RS
11979 xfree (f->output_data.x);
11980 f->output_data.x = 0;
0f941935
KH
11981 if (f == dpyinfo->x_focus_frame)
11982 dpyinfo->x_focus_frame = 0;
11983 if (f == dpyinfo->x_focus_event_frame)
11984 dpyinfo->x_focus_event_frame = 0;
11985 if (f == dpyinfo->x_highlight_frame)
11986 dpyinfo->x_highlight_frame = 0;
c0ff3fab 11987
7f9c7f94
RS
11988 dpyinfo->reference_count--;
11989
11990 if (f == dpyinfo->mouse_face_mouse_frame)
dc05a16b 11991 {
7f9c7f94
RS
11992 dpyinfo->mouse_face_beg_row
11993 = dpyinfo->mouse_face_beg_col = -1;
11994 dpyinfo->mouse_face_end_row
11995 = dpyinfo->mouse_face_end_col = -1;
11996 dpyinfo->mouse_face_window = Qnil;
21323706
RS
11997 dpyinfo->mouse_face_deferred_gc = 0;
11998 dpyinfo->mouse_face_mouse_frame = 0;
dc05a16b 11999 }
0134a210 12000
c0ff3fab 12001 UNBLOCK_INPUT;
dc6f92b8
JB
12002}
12003\f
f451eb13
JB
12004/* Setting window manager hints. */
12005
af31d76f
RS
12006/* Set the normal size hints for the window manager, for frame F.
12007 FLAGS is the flags word to use--or 0 meaning preserve the flags
12008 that the window now has.
12009 If USER_POSITION is nonzero, we set the USPosition
12010 flag (this is useful when FLAGS is 0). */
6dba1858 12011
dfcf069d 12012void
af31d76f 12013x_wm_set_size_hint (f, flags, user_position)
f676886a 12014 struct frame *f;
af31d76f
RS
12015 long flags;
12016 int user_position;
dc6f92b8
JB
12017{
12018 XSizeHints size_hints;
3afe33e7
RS
12019
12020#ifdef USE_X_TOOLKIT
7e4f2521
FP
12021 Arg al[2];
12022 int ac = 0;
12023 Dimension widget_width, widget_height;
7556890b 12024 Window window = XtWindow (f->output_data.x->widget);
3afe33e7 12025#else /* not USE_X_TOOLKIT */
c118dd06 12026 Window window = FRAME_X_WINDOW (f);
3afe33e7 12027#endif /* not USE_X_TOOLKIT */
dc6f92b8 12028
b72a58fd
RS
12029 /* Setting PMaxSize caused various problems. */
12030 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
dc6f92b8 12031
7556890b
RS
12032 size_hints.x = f->output_data.x->left_pos;
12033 size_hints.y = f->output_data.x->top_pos;
7553a6b7 12034
7e4f2521
FP
12035#ifdef USE_X_TOOLKIT
12036 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
12037 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
7556890b 12038 XtGetValues (f->output_data.x->widget, al, ac);
7e4f2521
FP
12039 size_hints.height = widget_height;
12040 size_hints.width = widget_width;
12041#else /* not USE_X_TOOLKIT */
f676886a
JB
12042 size_hints.height = PIXEL_HEIGHT (f);
12043 size_hints.width = PIXEL_WIDTH (f);
7e4f2521 12044#endif /* not USE_X_TOOLKIT */
7553a6b7 12045
7556890b
RS
12046 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
12047 size_hints.height_inc = f->output_data.x->line_height;
334208b7
RS
12048 size_hints.max_width
12049 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
12050 size_hints.max_height
12051 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
0134a210 12052
d067ea8b
KH
12053 /* Calculate the base and minimum sizes.
12054
12055 (When we use the X toolkit, we don't do it here.
12056 Instead we copy the values that the widgets are using, below.) */
12057#ifndef USE_X_TOOLKIT
b1c884c3 12058 {
b0342f17 12059 int base_width, base_height;
0134a210 12060 int min_rows = 0, min_cols = 0;
b0342f17 12061
f451eb13
JB
12062 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
12063 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
b0342f17 12064
0134a210 12065 check_frame_size (f, &min_rows, &min_cols);
b0342f17 12066
0134a210
RS
12067 /* The window manager uses the base width hints to calculate the
12068 current number of rows and columns in the frame while
12069 resizing; min_width and min_height aren't useful for this
12070 purpose, since they might not give the dimensions for a
12071 zero-row, zero-column frame.
58769bee 12072
0134a210
RS
12073 We use the base_width and base_height members if we have
12074 them; otherwise, we set the min_width and min_height members
12075 to the size for a zero x zero frame. */
b0342f17
JB
12076
12077#ifdef HAVE_X11R4
0134a210
RS
12078 size_hints.flags |= PBaseSize;
12079 size_hints.base_width = base_width;
12080 size_hints.base_height = base_height;
12081 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
12082 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
b0342f17 12083#else
0134a210
RS
12084 size_hints.min_width = base_width;
12085 size_hints.min_height = base_height;
b0342f17 12086#endif
b1c884c3 12087 }
dc6f92b8 12088
d067ea8b 12089 /* If we don't need the old flags, we don't need the old hint at all. */
af31d76f 12090 if (flags)
dc6f92b8 12091 {
d067ea8b
KH
12092 size_hints.flags |= flags;
12093 goto no_read;
12094 }
12095#endif /* not USE_X_TOOLKIT */
12096
12097 {
12098 XSizeHints hints; /* Sometimes I hate X Windows... */
12099 long supplied_return;
12100 int value;
af31d76f
RS
12101
12102#ifdef HAVE_X11R4
d067ea8b
KH
12103 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
12104 &supplied_return);
af31d76f 12105#else
d067ea8b 12106 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
af31d76f 12107#endif
58769bee 12108
d067ea8b
KH
12109#ifdef USE_X_TOOLKIT
12110 size_hints.base_height = hints.base_height;
12111 size_hints.base_width = hints.base_width;
12112 size_hints.min_height = hints.min_height;
12113 size_hints.min_width = hints.min_width;
12114#endif
12115
12116 if (flags)
12117 size_hints.flags |= flags;
12118 else
12119 {
12120 if (value == 0)
12121 hints.flags = 0;
12122 if (hints.flags & PSize)
12123 size_hints.flags |= PSize;
12124 if (hints.flags & PPosition)
12125 size_hints.flags |= PPosition;
12126 if (hints.flags & USPosition)
12127 size_hints.flags |= USPosition;
12128 if (hints.flags & USSize)
12129 size_hints.flags |= USSize;
12130 }
12131 }
12132
06a2c219 12133#ifndef USE_X_TOOLKIT
d067ea8b 12134 no_read:
06a2c219 12135#endif
0134a210 12136
af31d76f 12137#ifdef PWinGravity
7556890b 12138 size_hints.win_gravity = f->output_data.x->win_gravity;
af31d76f 12139 size_hints.flags |= PWinGravity;
dc05a16b 12140
af31d76f 12141 if (user_position)
6dba1858 12142 {
af31d76f
RS
12143 size_hints.flags &= ~ PPosition;
12144 size_hints.flags |= USPosition;
6dba1858 12145 }
2554751d 12146#endif /* PWinGravity */
6dba1858 12147
b0342f17 12148#ifdef HAVE_X11R4
334208b7 12149 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 12150#else
334208b7 12151 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 12152#endif
dc6f92b8
JB
12153}
12154
12155/* Used for IconicState or NormalState */
06a2c219 12156
dfcf069d 12157void
f676886a
JB
12158x_wm_set_window_state (f, state)
12159 struct frame *f;
dc6f92b8
JB
12160 int state;
12161{
3afe33e7 12162#ifdef USE_X_TOOLKIT
546e6d5b
RS
12163 Arg al[1];
12164
12165 XtSetArg (al[0], XtNinitialState, state);
7556890b 12166 XtSetValues (f->output_data.x->widget, al, 1);
3afe33e7 12167#else /* not USE_X_TOOLKIT */
c118dd06 12168 Window window = FRAME_X_WINDOW (f);
dc6f92b8 12169
7556890b
RS
12170 f->output_data.x->wm_hints.flags |= StateHint;
12171 f->output_data.x->wm_hints.initial_state = state;
b1c884c3 12172
7556890b 12173 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
546e6d5b 12174#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12175}
12176
dfcf069d 12177void
7f2ae036 12178x_wm_set_icon_pixmap (f, pixmap_id)
f676886a 12179 struct frame *f;
7f2ae036 12180 int pixmap_id;
dc6f92b8 12181{
d2bd6bc4
RS
12182 Pixmap icon_pixmap;
12183
06a2c219 12184#ifndef USE_X_TOOLKIT
c118dd06 12185 Window window = FRAME_X_WINDOW (f);
75231bad 12186#endif
dc6f92b8 12187
7f2ae036 12188 if (pixmap_id > 0)
dbc4e1c1 12189 {
d2bd6bc4 12190 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
7556890b 12191 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
dbc4e1c1
JB
12192 }
12193 else
68568555
RS
12194 {
12195 /* It seems there is no way to turn off use of an icon pixmap.
12196 The following line does it, only if no icon has yet been created,
12197 for some window managers. But with mwm it crashes.
12198 Some people say it should clear the IconPixmapHint bit in this case,
12199 but that doesn't work, and the X consortium said it isn't the
12200 right thing at all. Since there is no way to win,
12201 best to explicitly give up. */
12202#if 0
12203 f->output_data.x->wm_hints.icon_pixmap = None;
12204#else
12205 return;
12206#endif
12207 }
b1c884c3 12208
d2bd6bc4
RS
12209#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
12210
12211 {
12212 Arg al[1];
12213 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12214 XtSetValues (f->output_data.x->widget, al, 1);
12215 }
12216
12217#else /* not USE_X_TOOLKIT */
12218
7556890b
RS
12219 f->output_data.x->wm_hints.flags |= IconPixmapHint;
12220 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
d2bd6bc4
RS
12221
12222#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
12223}
12224
dfcf069d 12225void
f676886a
JB
12226x_wm_set_icon_position (f, icon_x, icon_y)
12227 struct frame *f;
dc6f92b8
JB
12228 int icon_x, icon_y;
12229{
75231bad 12230#ifdef USE_X_TOOLKIT
7556890b 12231 Window window = XtWindow (f->output_data.x->widget);
75231bad 12232#else
c118dd06 12233 Window window = FRAME_X_WINDOW (f);
75231bad 12234#endif
dc6f92b8 12235
7556890b
RS
12236 f->output_data.x->wm_hints.flags |= IconPositionHint;
12237 f->output_data.x->wm_hints.icon_x = icon_x;
12238 f->output_data.x->wm_hints.icon_y = icon_y;
b1c884c3 12239
7556890b 12240 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
dc6f92b8
JB
12241}
12242
12243\f
06a2c219
GM
12244/***********************************************************************
12245 Fonts
12246 ***********************************************************************/
dc43ef94
KH
12247
12248/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
06a2c219 12249
dc43ef94
KH
12250struct font_info *
12251x_get_font_info (f, font_idx)
12252 FRAME_PTR f;
12253 int font_idx;
12254{
12255 return (FRAME_X_FONT_TABLE (f) + font_idx);
12256}
12257
12258
12259/* Return a list of names of available fonts matching PATTERN on frame
12260 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
12261 to be listed. Frame F NULL means we have not yet created any
12262 frame on X, and consult the first display in x_display_list.
12263 MAXNAMES sets a limit on how many fonts to match. */
12264
12265Lisp_Object
12266x_list_fonts (f, pattern, size, maxnames)
12267 FRAME_PTR f;
12268 Lisp_Object pattern;
12269 int size;
12270 int maxnames;
12271{
06a2c219
GM
12272 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
12273 Lisp_Object tem, second_best;
dc43ef94 12274 Display *dpy = f != NULL ? FRAME_X_DISPLAY (f) : x_display_list->display;
09c6077f 12275 int try_XLoadQueryFont = 0;
53ca4657 12276 int count;
dc43ef94 12277
6b0efe73 12278 patterns = Fassoc (pattern, Valternate_fontname_alist);
2da424f1
KH
12279 if (NILP (patterns))
12280 patterns = Fcons (pattern, Qnil);
81ba44e5 12281
09c6077f
KH
12282 if (maxnames == 1 && !size)
12283 /* We can return any single font matching PATTERN. */
12284 try_XLoadQueryFont = 1;
9a32686f 12285
8e713be6 12286 for (; CONSP (patterns); patterns = XCDR (patterns))
dc43ef94 12287 {
dc43ef94 12288 int num_fonts;
3e71d8f2 12289 char **names = NULL;
dc43ef94 12290
8e713be6 12291 pattern = XCAR (patterns);
536f4067
RS
12292 /* See if we cached the result for this particular query.
12293 The cache is an alist of the form:
12294 (((PATTERN . MAXNAMES) (FONTNAME . WIDTH) ...) ...)
12295 */
8e713be6 12296 if (f && (tem = XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element),
b5210ea7
KH
12297 key = Fcons (pattern, make_number (maxnames)),
12298 !NILP (list = Fassoc (key, tem))))
12299 {
12300 list = Fcdr_safe (list);
12301 /* We have a cashed list. Don't have to get the list again. */
12302 goto label_cached;
12303 }
12304
12305 /* At first, put PATTERN in the cache. */
09c6077f 12306
dc43ef94 12307 BLOCK_INPUT;
17d85edc
KH
12308 count = x_catch_errors (dpy);
12309
09c6077f
KH
12310 if (try_XLoadQueryFont)
12311 {
12312 XFontStruct *font;
12313 unsigned long value;
12314
12315 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
17d85edc
KH
12316 if (x_had_errors_p (dpy))
12317 {
12318 /* This error is perhaps due to insufficient memory on X
12319 server. Let's just ignore it. */
12320 font = NULL;
12321 x_clear_errors (dpy);
12322 }
12323
09c6077f
KH
12324 if (font
12325 && XGetFontProperty (font, XA_FONT, &value))
12326 {
12327 char *name = (char *) XGetAtomName (dpy, (Atom) value);
12328 int len = strlen (name);
01c752b5 12329 char *tmp;
09c6077f 12330
6f6512e8
KH
12331 /* If DXPC (a Differential X Protocol Compressor)
12332 Ver.3.7 is running, XGetAtomName will return null
12333 string. We must avoid such a name. */
12334 if (len == 0)
12335 try_XLoadQueryFont = 0;
12336 else
12337 {
12338 num_fonts = 1;
12339 names = (char **) alloca (sizeof (char *));
12340 /* Some systems only allow alloca assigned to a
12341 simple var. */
12342 tmp = (char *) alloca (len + 1); names[0] = tmp;
12343 bcopy (name, names[0], len + 1);
12344 XFree (name);
12345 }
09c6077f
KH
12346 }
12347 else
12348 try_XLoadQueryFont = 0;
a083fd23
RS
12349
12350 if (font)
12351 XFreeFont (dpy, font);
09c6077f
KH
12352 }
12353
12354 if (!try_XLoadQueryFont)
17d85edc
KH
12355 {
12356 /* We try at least 10 fonts because XListFonts will return
12357 auto-scaled fonts at the head. */
12358 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
12359 &num_fonts);
12360 if (x_had_errors_p (dpy))
12361 {
12362 /* This error is perhaps due to insufficient memory on X
12363 server. Let's just ignore it. */
12364 names = NULL;
12365 x_clear_errors (dpy);
12366 }
12367 }
12368
12369 x_uncatch_errors (dpy, count);
dc43ef94
KH
12370 UNBLOCK_INPUT;
12371
12372 if (names)
12373 {
12374 int i;
dc43ef94
KH
12375
12376 /* Make a list of all the fonts we got back.
12377 Store that in the font cache for the display. */
12378 for (i = 0; i < num_fonts; i++)
12379 {
06a2c219 12380 int width = 0;
dc43ef94 12381 char *p = names[i];
06a2c219
GM
12382 int average_width = -1, dashes = 0;
12383
dc43ef94 12384 /* Count the number of dashes in NAMES[I]. If there are
b5210ea7
KH
12385 14 dashes, and the field value following 12th dash
12386 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
12387 is usually too ugly to be used for editing. Let's
12388 ignore it. */
dc43ef94
KH
12389 while (*p)
12390 if (*p++ == '-')
12391 {
12392 dashes++;
12393 if (dashes == 7) /* PIXEL_SIZE field */
12394 width = atoi (p);
12395 else if (dashes == 12) /* AVERAGE_WIDTH field */
12396 average_width = atoi (p);
12397 }
12398 if (dashes < 14 || average_width != 0)
12399 {
12400 tem = build_string (names[i]);
12401 if (NILP (Fassoc (tem, list)))
12402 {
12403 if (STRINGP (Vx_pixel_size_width_font_regexp)
f39db7ea
RS
12404 && ((fast_c_string_match_ignore_case
12405 (Vx_pixel_size_width_font_regexp, names[i]))
dc43ef94
KH
12406 >= 0))
12407 /* We can set the value of PIXEL_SIZE to the
b5210ea7 12408 width of this font. */
dc43ef94
KH
12409 list = Fcons (Fcons (tem, make_number (width)), list);
12410 else
12411 /* For the moment, width is not known. */
12412 list = Fcons (Fcons (tem, Qnil), list);
12413 }
12414 }
12415 }
09c6077f
KH
12416 if (!try_XLoadQueryFont)
12417 XFreeFontNames (names);
dc43ef94
KH
12418 }
12419
b5210ea7 12420 /* Now store the result in the cache. */
dc43ef94 12421 if (f != NULL)
8e713be6 12422 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element)
dc43ef94 12423 = Fcons (Fcons (key, list),
8e713be6 12424 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
dc43ef94 12425
b5210ea7
KH
12426 label_cached:
12427 if (NILP (list)) continue; /* Try the remaining alternatives. */
dc43ef94 12428
b5210ea7
KH
12429 newlist = second_best = Qnil;
12430 /* Make a list of the fonts that have the right width. */
8e713be6 12431 for (; CONSP (list); list = XCDR (list))
b5210ea7 12432 {
536f4067
RS
12433 int found_size;
12434
8e713be6 12435 tem = XCAR (list);
dc43ef94 12436
8e713be6 12437 if (!CONSP (tem) || NILP (XCAR (tem)))
b5210ea7
KH
12438 continue;
12439 if (!size)
12440 {
8e713be6 12441 newlist = Fcons (XCAR (tem), newlist);
b5210ea7
KH
12442 continue;
12443 }
dc43ef94 12444
8e713be6 12445 if (!INTEGERP (XCDR (tem)))
dc43ef94 12446 {
b5210ea7
KH
12447 /* Since we have not yet known the size of this font, we
12448 must try slow function call XLoadQueryFont. */
dc43ef94
KH
12449 XFontStruct *thisinfo;
12450
12451 BLOCK_INPUT;
17d85edc 12452 count = x_catch_errors (dpy);
dc43ef94 12453 thisinfo = XLoadQueryFont (dpy,
8e713be6 12454 XSTRING (XCAR (tem))->data);
17d85edc
KH
12455 if (x_had_errors_p (dpy))
12456 {
12457 /* This error is perhaps due to insufficient memory on X
12458 server. Let's just ignore it. */
12459 thisinfo = NULL;
12460 x_clear_errors (dpy);
12461 }
12462 x_uncatch_errors (dpy, count);
dc43ef94
KH
12463 UNBLOCK_INPUT;
12464
12465 if (thisinfo)
12466 {
8e713be6 12467 XCDR (tem)
536f4067
RS
12468 = (thisinfo->min_bounds.width == 0
12469 ? make_number (0)
12470 : make_number (thisinfo->max_bounds.width));
dc43ef94
KH
12471 XFreeFont (dpy, thisinfo);
12472 }
12473 else
b5210ea7 12474 /* For unknown reason, the previous call of XListFont had
06a2c219 12475 returned a font which can't be opened. Record the size
b5210ea7 12476 as 0 not to try to open it again. */
8e713be6 12477 XCDR (tem) = make_number (0);
dc43ef94 12478 }
536f4067 12479
8e713be6 12480 found_size = XINT (XCDR (tem));
536f4067 12481 if (found_size == size)
8e713be6 12482 newlist = Fcons (XCAR (tem), newlist);
536f4067 12483 else if (found_size > 0)
b5210ea7 12484 {
536f4067 12485 if (NILP (second_best))
b5210ea7 12486 second_best = tem;
536f4067
RS
12487 else if (found_size < size)
12488 {
8e713be6
KR
12489 if (XINT (XCDR (second_best)) > size
12490 || XINT (XCDR (second_best)) < found_size)
536f4067
RS
12491 second_best = tem;
12492 }
12493 else
12494 {
8e713be6
KR
12495 if (XINT (XCDR (second_best)) > size
12496 && XINT (XCDR (second_best)) > found_size)
536f4067
RS
12497 second_best = tem;
12498 }
b5210ea7
KH
12499 }
12500 }
12501 if (!NILP (newlist))
12502 break;
12503 else if (!NILP (second_best))
12504 {
8e713be6 12505 newlist = Fcons (XCAR (second_best), Qnil);
b5210ea7 12506 break;
dc43ef94 12507 }
dc43ef94
KH
12508 }
12509
12510 return newlist;
12511}
12512
06a2c219
GM
12513
12514#if GLYPH_DEBUG
12515
12516/* Check that FONT is valid on frame F. It is if it can be found in F's
12517 font table. */
12518
12519static void
12520x_check_font (f, font)
12521 struct frame *f;
12522 XFontStruct *font;
12523{
12524 int i;
12525 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12526
12527 xassert (font != NULL);
12528
12529 for (i = 0; i < dpyinfo->n_fonts; i++)
12530 if (dpyinfo->font_table[i].name
12531 && font == dpyinfo->font_table[i].font)
12532 break;
12533
12534 xassert (i < dpyinfo->n_fonts);
12535}
12536
12537#endif /* GLYPH_DEBUG != 0 */
12538
12539/* Set *W to the minimum width, *H to the minimum font height of FONT.
12540 Note: There are (broken) X fonts out there with invalid XFontStruct
12541 min_bounds contents. For example, handa@etl.go.jp reports that
12542 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
12543 have font->min_bounds.width == 0. */
12544
12545static INLINE void
12546x_font_min_bounds (font, w, h)
12547 XFontStruct *font;
12548 int *w, *h;
12549{
12550 *h = FONT_HEIGHT (font);
12551 *w = font->min_bounds.width;
12552
12553 /* Try to handle the case where FONT->min_bounds has invalid
12554 contents. Since the only font known to have invalid min_bounds
12555 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
12556 if (*w <= 0)
12557 *w = font->max_bounds.width;
12558}
12559
12560
12561/* Compute the smallest character width and smallest font height over
12562 all fonts available on frame F. Set the members smallest_char_width
12563 and smallest_font_height in F's x_display_info structure to
12564 the values computed. Value is non-zero if smallest_font_height or
12565 smallest_char_width become smaller than they were before. */
12566
12567static int
12568x_compute_min_glyph_bounds (f)
12569 struct frame *f;
12570{
12571 int i;
12572 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12573 XFontStruct *font;
12574 int old_width = dpyinfo->smallest_char_width;
12575 int old_height = dpyinfo->smallest_font_height;
12576
12577 dpyinfo->smallest_font_height = 100000;
12578 dpyinfo->smallest_char_width = 100000;
12579
12580 for (i = 0; i < dpyinfo->n_fonts; ++i)
12581 if (dpyinfo->font_table[i].name)
12582 {
12583 struct font_info *fontp = dpyinfo->font_table + i;
12584 int w, h;
12585
12586 font = (XFontStruct *) fontp->font;
12587 xassert (font != (XFontStruct *) ~0);
12588 x_font_min_bounds (font, &w, &h);
12589
12590 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
12591 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
12592 }
12593
12594 xassert (dpyinfo->smallest_char_width > 0
12595 && dpyinfo->smallest_font_height > 0);
12596
12597 return (dpyinfo->n_fonts == 1
12598 || dpyinfo->smallest_char_width < old_width
12599 || dpyinfo->smallest_font_height < old_height);
12600}
12601
12602
dc43ef94
KH
12603/* Load font named FONTNAME of the size SIZE for frame F, and return a
12604 pointer to the structure font_info while allocating it dynamically.
12605 If SIZE is 0, load any size of font.
12606 If loading is failed, return NULL. */
12607
12608struct font_info *
12609x_load_font (f, fontname, size)
12610 struct frame *f;
12611 register char *fontname;
12612 int size;
12613{
12614 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12615 Lisp_Object font_names;
d645aaa4 12616 int count;
dc43ef94
KH
12617
12618 /* Get a list of all the fonts that match this name. Once we
12619 have a list of matching fonts, we compare them against the fonts
12620 we already have by comparing names. */
09c6077f 12621 font_names = x_list_fonts (f, build_string (fontname), size, 1);
dc43ef94
KH
12622
12623 if (!NILP (font_names))
12624 {
12625 Lisp_Object tail;
12626 int i;
12627
12628 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 12629 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
06a2c219
GM
12630 if (dpyinfo->font_table[i].name
12631 && (!strcmp (dpyinfo->font_table[i].name,
8e713be6 12632 XSTRING (XCAR (tail))->data)
06a2c219 12633 || !strcmp (dpyinfo->font_table[i].full_name,
8e713be6 12634 XSTRING (XCAR (tail))->data)))
dc43ef94
KH
12635 return (dpyinfo->font_table + i);
12636 }
12637
12638 /* Load the font and add it to the table. */
12639 {
12640 char *full_name;
12641 XFontStruct *font;
12642 struct font_info *fontp;
12643 unsigned long value;
06a2c219 12644 int i;
dc43ef94 12645
2da424f1
KH
12646 /* If we have found fonts by x_list_font, load one of them. If
12647 not, we still try to load a font by the name given as FONTNAME
12648 because XListFonts (called in x_list_font) of some X server has
12649 a bug of not finding a font even if the font surely exists and
12650 is loadable by XLoadQueryFont. */
e1d6d5b9 12651 if (size > 0 && !NILP (font_names))
8e713be6 12652 fontname = (char *) XSTRING (XCAR (font_names))->data;
dc43ef94
KH
12653
12654 BLOCK_INPUT;
d645aaa4 12655 count = x_catch_errors (FRAME_X_DISPLAY (f));
dc43ef94 12656 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
d645aaa4
KH
12657 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12658 {
12659 /* This error is perhaps due to insufficient memory on X
12660 server. Let's just ignore it. */
12661 font = NULL;
12662 x_clear_errors (FRAME_X_DISPLAY (f));
12663 }
12664 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
dc43ef94 12665 UNBLOCK_INPUT;
b5210ea7 12666 if (!font)
dc43ef94
KH
12667 return NULL;
12668
06a2c219
GM
12669 /* Find a free slot in the font table. */
12670 for (i = 0; i < dpyinfo->n_fonts; ++i)
12671 if (dpyinfo->font_table[i].name == NULL)
12672 break;
12673
12674 /* If no free slot found, maybe enlarge the font table. */
12675 if (i == dpyinfo->n_fonts
12676 && dpyinfo->n_fonts == dpyinfo->font_table_size)
dc43ef94 12677 {
06a2c219
GM
12678 int sz;
12679 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
12680 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
dc43ef94 12681 dpyinfo->font_table
06a2c219 12682 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
dc43ef94
KH
12683 }
12684
06a2c219
GM
12685 fontp = dpyinfo->font_table + i;
12686 if (i == dpyinfo->n_fonts)
12687 ++dpyinfo->n_fonts;
dc43ef94
KH
12688
12689 /* Now fill in the slots of *FONTP. */
12690 BLOCK_INPUT;
12691 fontp->font = font;
06a2c219 12692 fontp->font_idx = i;
dc43ef94
KH
12693 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
12694 bcopy (fontname, fontp->name, strlen (fontname) + 1);
12695
12696 /* Try to get the full name of FONT. Put it in FULL_NAME. */
12697 full_name = 0;
12698 if (XGetFontProperty (font, XA_FONT, &value))
12699 {
12700 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
12701 char *p = name;
12702 int dashes = 0;
12703
12704 /* Count the number of dashes in the "full name".
12705 If it is too few, this isn't really the font's full name,
12706 so don't use it.
12707 In X11R4, the fonts did not come with their canonical names
12708 stored in them. */
12709 while (*p)
12710 {
12711 if (*p == '-')
12712 dashes++;
12713 p++;
12714 }
12715
12716 if (dashes >= 13)
12717 {
12718 full_name = (char *) xmalloc (p - name + 1);
12719 bcopy (name, full_name, p - name + 1);
12720 }
12721
12722 XFree (name);
12723 }
12724
12725 if (full_name != 0)
12726 fontp->full_name = full_name;
12727 else
12728 fontp->full_name = fontp->name;
12729
12730 fontp->size = font->max_bounds.width;
d5749adb
KH
12731 fontp->height = FONT_HEIGHT (font);
12732 {
12733 /* For some font, ascent and descent in max_bounds field is
12734 larger than the above value. */
12735 int max_height = font->max_bounds.ascent + font->max_bounds.descent;
12736 if (max_height > fontp->height)
74848a96 12737 fontp->height = max_height;
d5749adb 12738 }
dc43ef94 12739
2da424f1
KH
12740 if (NILP (font_names))
12741 {
12742 /* We come here because of a bug of XListFonts mentioned at
12743 the head of this block. Let's store this information in
12744 the cache for x_list_fonts. */
12745 Lisp_Object lispy_name = build_string (fontname);
12746 Lisp_Object lispy_full_name = build_string (fontp->full_name);
12747
8e713be6 12748 XCDR (dpyinfo->name_list_element)
2da424f1
KH
12749 = Fcons (Fcons (Fcons (lispy_name, make_number (256)),
12750 Fcons (Fcons (lispy_full_name,
12751 make_number (fontp->size)),
12752 Qnil)),
8e713be6 12753 XCDR (dpyinfo->name_list_element));
2da424f1 12754 if (full_name)
8e713be6 12755 XCDR (dpyinfo->name_list_element)
2da424f1
KH
12756 = Fcons (Fcons (Fcons (lispy_full_name, make_number (256)),
12757 Fcons (Fcons (lispy_full_name,
12758 make_number (fontp->size)),
12759 Qnil)),
8e713be6 12760 XCDR (dpyinfo->name_list_element));
2da424f1
KH
12761 }
12762
dc43ef94
KH
12763 /* The slot `encoding' specifies how to map a character
12764 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
ee569018
KH
12765 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
12766 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
8ff102bd 12767 2:0xA020..0xFF7F). For the moment, we don't know which charset
06a2c219 12768 uses this font. So, we set information in fontp->encoding[1]
8ff102bd
RS
12769 which is never used by any charset. If mapping can't be
12770 decided, set FONT_ENCODING_NOT_DECIDED. */
dc43ef94
KH
12771 fontp->encoding[1]
12772 = (font->max_byte1 == 0
12773 /* 1-byte font */
12774 ? (font->min_char_or_byte2 < 0x80
12775 ? (font->max_char_or_byte2 < 0x80
12776 ? 0 /* 0x20..0x7F */
8ff102bd 12777 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
dc43ef94
KH
12778 : 1) /* 0xA0..0xFF */
12779 /* 2-byte font */
12780 : (font->min_byte1 < 0x80
12781 ? (font->max_byte1 < 0x80
12782 ? (font->min_char_or_byte2 < 0x80
12783 ? (font->max_char_or_byte2 < 0x80
12784 ? 0 /* 0x2020..0x7F7F */
8ff102bd 12785 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
dc43ef94 12786 : 3) /* 0x20A0..0x7FFF */
8ff102bd 12787 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
dc43ef94
KH
12788 : (font->min_char_or_byte2 < 0x80
12789 ? (font->max_char_or_byte2 < 0x80
12790 ? 2 /* 0xA020..0xFF7F */
8ff102bd 12791 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
dc43ef94
KH
12792 : 1))); /* 0xA0A0..0xFFFF */
12793
12794 fontp->baseline_offset
12795 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
12796 ? (long) value : 0);
12797 fontp->relative_compose
12798 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
12799 ? (long) value : 0);
f78798df
KH
12800 fontp->default_ascent
12801 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
12802 ? (long) value : 0);
dc43ef94 12803
06a2c219
GM
12804 /* Set global flag fonts_changed_p to non-zero if the font loaded
12805 has a character with a smaller width than any other character
12806 before, or if the font loaded has a smalle>r height than any
12807 other font loaded before. If this happens, it will make a
12808 glyph matrix reallocation necessary. */
12809 fonts_changed_p = x_compute_min_glyph_bounds (f);
dc43ef94 12810 UNBLOCK_INPUT;
dc43ef94
KH
12811 return fontp;
12812 }
12813}
12814
06a2c219
GM
12815
12816/* Return a pointer to struct font_info of a font named FONTNAME for
12817 frame F. If no such font is loaded, return NULL. */
12818
dc43ef94
KH
12819struct font_info *
12820x_query_font (f, fontname)
12821 struct frame *f;
12822 register char *fontname;
12823{
12824 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12825 int i;
12826
12827 for (i = 0; i < dpyinfo->n_fonts; i++)
06a2c219
GM
12828 if (dpyinfo->font_table[i].name
12829 && (!strcmp (dpyinfo->font_table[i].name, fontname)
12830 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
dc43ef94
KH
12831 return (dpyinfo->font_table + i);
12832 return NULL;
12833}
12834
06a2c219
GM
12835
12836/* Find a CCL program for a font specified by FONTP, and set the member
a6582676
KH
12837 `encoder' of the structure. */
12838
12839void
12840x_find_ccl_program (fontp)
12841 struct font_info *fontp;
12842{
a42f54e6 12843 Lisp_Object list, elt;
a6582676 12844
8e713be6 12845 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
a6582676 12846 {
8e713be6 12847 elt = XCAR (list);
a6582676 12848 if (CONSP (elt)
8e713be6
KR
12849 && STRINGP (XCAR (elt))
12850 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
a6582676 12851 >= 0))
a42f54e6
KH
12852 break;
12853 }
12854 if (! NILP (list))
12855 {
d27f8ca7
KH
12856 struct ccl_program *ccl
12857 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
a42f54e6 12858
8e713be6 12859 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
a42f54e6
KH
12860 xfree (ccl);
12861 else
12862 fontp->font_encoder = ccl;
a6582676
KH
12863 }
12864}
12865
06a2c219 12866
dc43ef94 12867\f
06a2c219
GM
12868/***********************************************************************
12869 Initialization
12870 ***********************************************************************/
f451eb13 12871
3afe33e7
RS
12872#ifdef USE_X_TOOLKIT
12873static XrmOptionDescRec emacs_options[] = {
12874 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
12875 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
12876
12877 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
12878 XrmoptionSepArg, NULL},
12879 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
12880
12881 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12882 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12883 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12884 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12885 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12886 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
12887 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
12888};
12889#endif /* USE_X_TOOLKIT */
12890
7a13e894
RS
12891static int x_initialized;
12892
29b38361
KH
12893#ifdef MULTI_KBOARD
12894/* Test whether two display-name strings agree up to the dot that separates
12895 the screen number from the server number. */
12896static int
12897same_x_server (name1, name2)
12898 char *name1, *name2;
12899{
12900 int seen_colon = 0;
cf591cc1
RS
12901 unsigned char *system_name = XSTRING (Vsystem_name)->data;
12902 int system_name_length = strlen (system_name);
12903 int length_until_period = 0;
12904
12905 while (system_name[length_until_period] != 0
12906 && system_name[length_until_period] != '.')
12907 length_until_period++;
12908
12909 /* Treat `unix' like an empty host name. */
12910 if (! strncmp (name1, "unix:", 5))
12911 name1 += 4;
12912 if (! strncmp (name2, "unix:", 5))
12913 name2 += 4;
12914 /* Treat this host's name like an empty host name. */
12915 if (! strncmp (name1, system_name, system_name_length)
12916 && name1[system_name_length] == ':')
12917 name1 += system_name_length;
12918 if (! strncmp (name2, system_name, system_name_length)
12919 && name2[system_name_length] == ':')
12920 name2 += system_name_length;
12921 /* Treat this host's domainless name like an empty host name. */
12922 if (! strncmp (name1, system_name, length_until_period)
12923 && name1[length_until_period] == ':')
12924 name1 += length_until_period;
12925 if (! strncmp (name2, system_name, length_until_period)
12926 && name2[length_until_period] == ':')
12927 name2 += length_until_period;
12928
29b38361
KH
12929 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
12930 {
12931 if (*name1 == ':')
12932 seen_colon++;
12933 if (seen_colon && *name1 == '.')
12934 return 1;
12935 }
12936 return (seen_colon
12937 && (*name1 == '.' || *name1 == '\0')
12938 && (*name2 == '.' || *name2 == '\0'));
12939}
12940#endif
12941
334208b7 12942struct x_display_info *
1f8255f2 12943x_term_init (display_name, xrm_option, resource_name)
334208b7 12944 Lisp_Object display_name;
1f8255f2
RS
12945 char *xrm_option;
12946 char *resource_name;
dc6f92b8 12947{
334208b7 12948 int connection;
7a13e894 12949 Display *dpy;
334208b7
RS
12950 struct x_display_info *dpyinfo;
12951 XrmDatabase xrdb;
12952
60439948
KH
12953 BLOCK_INPUT;
12954
7a13e894
RS
12955 if (!x_initialized)
12956 {
12957 x_initialize ();
12958 x_initialized = 1;
12959 }
dc6f92b8 12960
3afe33e7 12961#ifdef USE_X_TOOLKIT
2d7fc7e8
RS
12962 /* weiner@footloose.sps.mot.com reports that this causes
12963 errors with X11R5:
12964 X protocol error: BadAtom (invalid Atom parameter)
12965 on protocol request 18skiloaf.
12966 So let's not use it until R6. */
12967#ifdef HAVE_X11XTR6
bdcd49ba
RS
12968 XtSetLanguageProc (NULL, NULL, NULL);
12969#endif
12970
7f9c7f94
RS
12971 {
12972 int argc = 0;
12973 char *argv[3];
12974
12975 argv[0] = "";
12976 argc = 1;
12977 if (xrm_option)
12978 {
12979 argv[argc++] = "-xrm";
12980 argv[argc++] = xrm_option;
12981 }
12982 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
12983 resource_name, EMACS_CLASS,
12984 emacs_options, XtNumber (emacs_options),
12985 &argc, argv);
39d8bb4d
KH
12986
12987#ifdef HAVE_X11XTR6
10537cb1 12988 /* I think this is to compensate for XtSetLanguageProc. */
71f8198a 12989 fixup_locale ();
39d8bb4d 12990#endif
7f9c7f94 12991 }
3afe33e7
RS
12992
12993#else /* not USE_X_TOOLKIT */
bdcd49ba
RS
12994#ifdef HAVE_X11R5
12995 XSetLocaleModifiers ("");
12996#endif
7a13e894 12997 dpy = XOpenDisplay (XSTRING (display_name)->data);
3afe33e7 12998#endif /* not USE_X_TOOLKIT */
334208b7 12999
7a13e894
RS
13000 /* Detect failure. */
13001 if (dpy == 0)
60439948
KH
13002 {
13003 UNBLOCK_INPUT;
13004 return 0;
13005 }
7a13e894
RS
13006
13007 /* We have definitely succeeded. Record the new connection. */
13008
13009 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
13010
29b38361
KH
13011#ifdef MULTI_KBOARD
13012 {
13013 struct x_display_info *share;
13014 Lisp_Object tail;
13015
13016 for (share = x_display_list, tail = x_display_name_list; share;
8e713be6
KR
13017 share = share->next, tail = XCDR (tail))
13018 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
29b38361
KH
13019 XSTRING (display_name)->data))
13020 break;
13021 if (share)
13022 dpyinfo->kboard = share->kboard;
13023 else
13024 {
13025 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
13026 init_kboard (dpyinfo->kboard);
59e755be
KH
13027 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
13028 {
13029 char *vendor = ServerVendor (dpy);
9b6ed9f3 13030 UNBLOCK_INPUT;
59e755be
KH
13031 dpyinfo->kboard->Vsystem_key_alist
13032 = call1 (Qvendor_specific_keysyms,
13033 build_string (vendor ? vendor : ""));
9b6ed9f3 13034 BLOCK_INPUT;
59e755be
KH
13035 }
13036
29b38361
KH
13037 dpyinfo->kboard->next_kboard = all_kboards;
13038 all_kboards = dpyinfo->kboard;
0ad5446c
KH
13039 /* Don't let the initial kboard remain current longer than necessary.
13040 That would cause problems if a file loaded on startup tries to
06a2c219 13041 prompt in the mini-buffer. */
0ad5446c
KH
13042 if (current_kboard == initial_kboard)
13043 current_kboard = dpyinfo->kboard;
29b38361
KH
13044 }
13045 dpyinfo->kboard->reference_count++;
13046 }
b9737ad3
KH
13047#endif
13048
7a13e894
RS
13049 /* Put this display on the chain. */
13050 dpyinfo->next = x_display_list;
13051 x_display_list = dpyinfo;
13052
13053 /* Put it on x_display_name_list as well, to keep them parallel. */
13054 x_display_name_list = Fcons (Fcons (display_name, Qnil),
13055 x_display_name_list);
8e713be6 13056 dpyinfo->name_list_element = XCAR (x_display_name_list);
7a13e894
RS
13057
13058 dpyinfo->display = dpy;
dc6f92b8 13059
dc6f92b8 13060#if 0
7a13e894 13061 XSetAfterFunction (x_current_display, x_trace_wire);
c118dd06 13062#endif /* ! 0 */
7a13e894
RS
13063
13064 dpyinfo->x_id_name
fc932ac6
RS
13065 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
13066 + STRING_BYTES (XSTRING (Vsystem_name))
7a13e894
RS
13067 + 2);
13068 sprintf (dpyinfo->x_id_name, "%s@%s",
13069 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
28430d3c
JB
13070
13071 /* Figure out which modifier bits mean what. */
334208b7 13072 x_find_modifier_meanings (dpyinfo);
f451eb13 13073
ab648270 13074 /* Get the scroll bar cursor. */
7a13e894 13075 dpyinfo->vertical_scroll_bar_cursor
334208b7 13076 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
f451eb13 13077
334208b7
RS
13078 xrdb = x_load_resources (dpyinfo->display, xrm_option,
13079 resource_name, EMACS_CLASS);
13080#ifdef HAVE_XRMSETDATABASE
13081 XrmSetDatabase (dpyinfo->display, xrdb);
13082#else
13083 dpyinfo->display->db = xrdb;
13084#endif
547d9db8 13085 /* Put the rdb where we can find it in a way that works on
7a13e894
RS
13086 all versions. */
13087 dpyinfo->xrdb = xrdb;
334208b7
RS
13088
13089 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
13090 DefaultScreen (dpyinfo->display));
5ff67d81 13091 select_visual (dpyinfo);
43bd1b2b 13092 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
334208b7
RS
13093 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
13094 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
13095 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
13096 dpyinfo->grabbed = 0;
13097 dpyinfo->reference_count = 0;
13098 dpyinfo->icon_bitmap_id = -1;
06a2c219 13099 dpyinfo->font_table = NULL;
7a13e894
RS
13100 dpyinfo->n_fonts = 0;
13101 dpyinfo->font_table_size = 0;
13102 dpyinfo->bitmaps = 0;
13103 dpyinfo->bitmaps_size = 0;
13104 dpyinfo->bitmaps_last = 0;
13105 dpyinfo->scratch_cursor_gc = 0;
13106 dpyinfo->mouse_face_mouse_frame = 0;
13107 dpyinfo->mouse_face_deferred_gc = 0;
13108 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
13109 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
06a2c219 13110 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
7a13e894
RS
13111 dpyinfo->mouse_face_window = Qnil;
13112 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
13113 dpyinfo->mouse_face_defer = 0;
0f941935
KH
13114 dpyinfo->x_focus_frame = 0;
13115 dpyinfo->x_focus_event_frame = 0;
13116 dpyinfo->x_highlight_frame = 0;
06a2c219 13117 dpyinfo->image_cache = make_image_cache ();
334208b7 13118
43bd1b2b 13119 /* See if a private colormap is requested. */
5ff67d81
GM
13120 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
13121 {
13122 if (dpyinfo->visual->class == PseudoColor)
13123 {
13124 Lisp_Object value;
13125 value = display_x_get_resource (dpyinfo,
13126 build_string ("privateColormap"),
13127 build_string ("PrivateColormap"),
13128 Qnil, Qnil);
13129 if (STRINGP (value)
13130 && (!strcmp (XSTRING (value)->data, "true")
13131 || !strcmp (XSTRING (value)->data, "on")))
13132 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
13133 }
43bd1b2b 13134 }
5ff67d81
GM
13135 else
13136 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
13137 dpyinfo->visual, AllocNone);
43bd1b2b 13138
06a2c219
GM
13139 {
13140 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
13141 double pixels = DisplayHeight (dpyinfo->display, screen_number);
13142 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
13143 dpyinfo->resy = pixels * 25.4 / mm;
13144 pixels = DisplayWidth (dpyinfo->display, screen_number);
13145 mm = DisplayWidthMM (dpyinfo->display, screen_number);
13146 dpyinfo->resx = pixels * 25.4 / mm;
13147 }
13148
334208b7
RS
13149 dpyinfo->Xatom_wm_protocols
13150 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
13151 dpyinfo->Xatom_wm_take_focus
13152 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
13153 dpyinfo->Xatom_wm_save_yourself
13154 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
13155 dpyinfo->Xatom_wm_delete_window
13156 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
13157 dpyinfo->Xatom_wm_change_state
13158 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
13159 dpyinfo->Xatom_wm_configure_denied
13160 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
13161 dpyinfo->Xatom_wm_window_moved
13162 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
13163 dpyinfo->Xatom_editres
13164 = XInternAtom (dpyinfo->display, "Editres", False);
13165 dpyinfo->Xatom_CLIPBOARD
13166 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
13167 dpyinfo->Xatom_TIMESTAMP
13168 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
13169 dpyinfo->Xatom_TEXT
13170 = XInternAtom (dpyinfo->display, "TEXT", False);
dc43ef94
KH
13171 dpyinfo->Xatom_COMPOUND_TEXT
13172 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
334208b7
RS
13173 dpyinfo->Xatom_DELETE
13174 = XInternAtom (dpyinfo->display, "DELETE", False);
13175 dpyinfo->Xatom_MULTIPLE
13176 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
13177 dpyinfo->Xatom_INCR
13178 = XInternAtom (dpyinfo->display, "INCR", False);
13179 dpyinfo->Xatom_EMACS_TMP
13180 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
13181 dpyinfo->Xatom_TARGETS
13182 = XInternAtom (dpyinfo->display, "TARGETS", False);
13183 dpyinfo->Xatom_NULL
13184 = XInternAtom (dpyinfo->display, "NULL", False);
13185 dpyinfo->Xatom_ATOM_PAIR
13186 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
dc43ef94
KH
13187 /* For properties of font. */
13188 dpyinfo->Xatom_PIXEL_SIZE
13189 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
13190 dpyinfo->Xatom_MULE_BASELINE_OFFSET
13191 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
13192 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
13193 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
f78798df
KH
13194 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
13195 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
334208b7 13196
06a2c219
GM
13197 /* Ghostscript support. */
13198 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
13199 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
13200
13201 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
13202 False);
13203
547d9db8
KH
13204 dpyinfo->cut_buffers_initialized = 0;
13205
334208b7
RS
13206 connection = ConnectionNumber (dpyinfo->display);
13207 dpyinfo->connection = connection;
13208
dc43ef94 13209 {
5d7cc324
RS
13210 char null_bits[1];
13211
13212 null_bits[0] = 0x00;
dc43ef94
KH
13213
13214 dpyinfo->null_pixel
13215 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13216 null_bits, 1, 1, (long) 0, (long) 0,
13217 1);
13218 }
13219
06a2c219
GM
13220 {
13221 extern int gray_bitmap_width, gray_bitmap_height;
13222 extern unsigned char *gray_bitmap_bits;
13223 dpyinfo->gray
13224 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13225 gray_bitmap_bits,
13226 gray_bitmap_width, gray_bitmap_height,
13227 (unsigned long) 1, (unsigned long) 0, 1);
13228 }
13229
f5d11644
GM
13230#ifdef HAVE_X_I18N
13231 xim_initialize (dpyinfo, resource_name);
13232#endif
13233
87485d6f
MW
13234#ifdef subprocesses
13235 /* This is only needed for distinguishing keyboard and process input. */
334208b7 13236 if (connection != 0)
7a13e894 13237 add_keyboard_wait_descriptor (connection);
87485d6f 13238#endif
6d4238f3 13239
041b69ac 13240#ifndef F_SETOWN_BUG
dc6f92b8 13241#ifdef F_SETOWN
dc6f92b8 13242#ifdef F_SETOWN_SOCK_NEG
61c3ce62 13243 /* stdin is a socket here */
334208b7 13244 fcntl (connection, F_SETOWN, -getpid ());
c118dd06 13245#else /* ! defined (F_SETOWN_SOCK_NEG) */
334208b7 13246 fcntl (connection, F_SETOWN, getpid ());
c118dd06
JB
13247#endif /* ! defined (F_SETOWN_SOCK_NEG) */
13248#endif /* ! defined (F_SETOWN) */
041b69ac 13249#endif /* F_SETOWN_BUG */
dc6f92b8
JB
13250
13251#ifdef SIGIO
eee20f6a
KH
13252 if (interrupt_input)
13253 init_sigio (connection);
c118dd06 13254#endif /* ! defined (SIGIO) */
dc6f92b8 13255
51b592fb 13256#ifdef USE_LUCID
f8c39f51 13257#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
51b592fb
RS
13258 /* Make sure that we have a valid font for dialog boxes
13259 so that Xt does not crash. */
13260 {
13261 Display *dpy = dpyinfo->display;
13262 XrmValue d, fr, to;
13263 Font font;
e99db5a1 13264 int count;
51b592fb
RS
13265
13266 d.addr = (XPointer)&dpy;
13267 d.size = sizeof (Display *);
13268 fr.addr = XtDefaultFont;
13269 fr.size = sizeof (XtDefaultFont);
13270 to.size = sizeof (Font *);
13271 to.addr = (XPointer)&font;
e99db5a1 13272 count = x_catch_errors (dpy);
51b592fb
RS
13273 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
13274 abort ();
13275 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
13276 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
e99db5a1 13277 x_uncatch_errors (dpy, count);
51b592fb
RS
13278 }
13279#endif
f8c39f51 13280#endif
51b592fb 13281
34e23e5a
GM
13282 /* See if we should run in synchronous mode. This is useful
13283 for debugging X code. */
13284 {
13285 Lisp_Object value;
13286 value = display_x_get_resource (dpyinfo,
13287 build_string ("synchronous"),
13288 build_string ("Synchronous"),
13289 Qnil, Qnil);
13290 if (STRINGP (value)
13291 && (!strcmp (XSTRING (value)->data, "true")
13292 || !strcmp (XSTRING (value)->data, "on")))
13293 XSynchronize (dpyinfo->display, True);
13294 }
13295
60439948
KH
13296 UNBLOCK_INPUT;
13297
7a13e894
RS
13298 return dpyinfo;
13299}
13300\f
13301/* Get rid of display DPYINFO, assuming all frames are already gone,
13302 and without sending any more commands to the X server. */
dc6f92b8 13303
7a13e894
RS
13304void
13305x_delete_display (dpyinfo)
13306 struct x_display_info *dpyinfo;
13307{
13308 delete_keyboard_wait_descriptor (dpyinfo->connection);
13309
13310 /* Discard this display from x_display_name_list and x_display_list.
13311 We can't use Fdelq because that can quit. */
13312 if (! NILP (x_display_name_list)
8e713be6
KR
13313 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
13314 x_display_name_list = XCDR (x_display_name_list);
7a13e894
RS
13315 else
13316 {
13317 Lisp_Object tail;
13318
13319 tail = x_display_name_list;
8e713be6 13320 while (CONSP (tail) && CONSP (XCDR (tail)))
7a13e894 13321 {
bffcfca9 13322 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
7a13e894 13323 {
8e713be6 13324 XCDR (tail) = XCDR (XCDR (tail));
7a13e894
RS
13325 break;
13326 }
8e713be6 13327 tail = XCDR (tail);
7a13e894
RS
13328 }
13329 }
13330
9bda743f
GM
13331 if (next_noop_dpyinfo == dpyinfo)
13332 next_noop_dpyinfo = dpyinfo->next;
13333
7a13e894
RS
13334 if (x_display_list == dpyinfo)
13335 x_display_list = dpyinfo->next;
7f9c7f94
RS
13336 else
13337 {
13338 struct x_display_info *tail;
7a13e894 13339
7f9c7f94
RS
13340 for (tail = x_display_list; tail; tail = tail->next)
13341 if (tail->next == dpyinfo)
13342 tail->next = tail->next->next;
13343 }
7a13e894 13344
0d777288
RS
13345#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
13346#ifndef AIX /* On AIX, XCloseDisplay calls this. */
7f9c7f94
RS
13347 XrmDestroyDatabase (dpyinfo->xrdb);
13348#endif
0d777288 13349#endif
29b38361
KH
13350#ifdef MULTI_KBOARD
13351 if (--dpyinfo->kboard->reference_count == 0)
39f79001 13352 delete_kboard (dpyinfo->kboard);
b9737ad3 13353#endif
f5d11644
GM
13354#ifdef HAVE_X_I18N
13355 if (dpyinfo->xim)
13356 xim_close_dpy (dpyinfo);
13357#endif
13358
b9737ad3
KH
13359 xfree (dpyinfo->font_table);
13360 xfree (dpyinfo->x_id_name);
13361 xfree (dpyinfo);
7a13e894
RS
13362}
13363\f
13364/* Set up use of X before we make the first connection. */
13365
06a2c219
GM
13366static struct redisplay_interface x_redisplay_interface =
13367{
13368 x_produce_glyphs,
13369 x_write_glyphs,
13370 x_insert_glyphs,
13371 x_clear_end_of_line,
13372 x_scroll_run,
13373 x_after_update_window_line,
13374 x_update_window_begin,
13375 x_update_window_end,
13376 XTcursor_to,
13377 x_flush,
66ac4b0e
GM
13378 x_get_glyph_overhangs,
13379 x_fix_overlapping_area
06a2c219
GM
13380};
13381
dfcf069d 13382void
7a13e894
RS
13383x_initialize ()
13384{
06a2c219
GM
13385 rif = &x_redisplay_interface;
13386
13387 clear_frame_hook = x_clear_frame;
13388 ins_del_lines_hook = x_ins_del_lines;
13389 change_line_highlight_hook = x_change_line_highlight;
13390 delete_glyphs_hook = x_delete_glyphs;
dc6f92b8
JB
13391 ring_bell_hook = XTring_bell;
13392 reset_terminal_modes_hook = XTreset_terminal_modes;
13393 set_terminal_modes_hook = XTset_terminal_modes;
06a2c219
GM
13394 update_begin_hook = x_update_begin;
13395 update_end_hook = x_update_end;
dc6f92b8
JB
13396 set_terminal_window_hook = XTset_terminal_window;
13397 read_socket_hook = XTread_socket;
b8009dd1 13398 frame_up_to_date_hook = XTframe_up_to_date;
dc6f92b8 13399 reassert_line_highlight_hook = XTreassert_line_highlight;
90e65f07 13400 mouse_position_hook = XTmouse_position;
f451eb13 13401 frame_rehighlight_hook = XTframe_rehighlight;
dbc4e1c1 13402 frame_raise_lower_hook = XTframe_raise_lower;
ab648270
JB
13403 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13404 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13405 redeem_scroll_bar_hook = XTredeem_scroll_bar;
13406 judge_scroll_bars_hook = XTjudge_scroll_bars;
06a2c219 13407 estimate_mode_line_height_hook = x_estimate_mode_line_height;
58769bee 13408
f676886a 13409 scroll_region_ok = 1; /* we'll scroll partial frames */
dc6f92b8
JB
13410 char_ins_del_ok = 0; /* just as fast to write the line */
13411 line_ins_del_ok = 1; /* we'll just blt 'em */
13412 fast_clear_end_of_line = 1; /* X does this well */
58769bee 13413 memory_below_frame = 0; /* we don't remember what scrolls
dc6f92b8
JB
13414 off the bottom */
13415 baud_rate = 19200;
13416
7a13e894 13417 x_noop_count = 0;
9ea173e8 13418 last_tool_bar_item = -1;
06a2c219
GM
13419 any_help_event_p = 0;
13420
b30b24cb
RS
13421 /* Try to use interrupt input; if we can't, then start polling. */
13422 Fset_input_mode (Qt, Qnil, Qt, Qnil);
13423
7f9c7f94
RS
13424#ifdef USE_X_TOOLKIT
13425 XtToolkitInitialize ();
13426 Xt_app_con = XtCreateApplicationContext ();
665881ad 13427 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
bffcfca9
GM
13428
13429 /* Install an asynchronous timer that processes Xt timeout events
13430 every 0.1s. This is necessary because some widget sets use
13431 timeouts internally, for example the LessTif menu bar, or the
13432 Xaw3d scroll bar. When Xt timouts aren't processed, these
13433 widgets don't behave normally. */
13434 {
13435 EMACS_TIME interval;
13436 EMACS_SET_SECS_USECS (interval, 0, 100000);
13437 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
13438 }
db74249b 13439#endif
bffcfca9 13440
db74249b 13441#if USE_TOOLKIT_SCROLL_BARS
ec18280f
SM
13442 xaw3d_arrow_scroll = False;
13443 xaw3d_pick_top = True;
7f9c7f94
RS
13444#endif
13445
58769bee 13446 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 13447 original error handler. */
e99db5a1 13448 XSetErrorHandler (x_error_handler);
334208b7 13449 XSetIOErrorHandler (x_io_error_quitter);
dc6f92b8 13450
06a2c219 13451 /* Disable Window Change signals; they are handled by X events. */
dc6f92b8
JB
13452#ifdef SIGWINCH
13453 signal (SIGWINCH, SIG_DFL);
c118dd06 13454#endif /* ! defined (SIGWINCH) */
dc6f92b8 13455
92e2441b 13456 signal (SIGPIPE, x_connection_signal);
dc6f92b8 13457}
55123275 13458
06a2c219 13459
55123275
JB
13460void
13461syms_of_xterm ()
13462{
e99db5a1
RS
13463 staticpro (&x_error_message_string);
13464 x_error_message_string = Qnil;
13465
7a13e894
RS
13466 staticpro (&x_display_name_list);
13467 x_display_name_list = Qnil;
334208b7 13468
ab648270 13469 staticpro (&last_mouse_scroll_bar);
e53cb100 13470 last_mouse_scroll_bar = Qnil;
59e755be
KH
13471
13472 staticpro (&Qvendor_specific_keysyms);
13473 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
2237cac9
RS
13474
13475 staticpro (&last_mouse_press_frame);
13476 last_mouse_press_frame = Qnil;
06a2c219
GM
13477
13478 staticpro (&help_echo);
13479 help_echo = Qnil;
13480 staticpro (&previous_help_echo);
13481 previous_help_echo = Qnil;
13482
13483 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
13484 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
13485For example, if a block cursor is over a tab, it will be drawn as\n\
13486wide as that tab on the display.");
13487 x_stretch_cursor_p = 0;
13488
13489 DEFVAR_BOOL ("x-toolkit-scroll-bars-p", &x_toolkit_scroll_bars_p,
13490 "If not nil, Emacs uses toolkit scroll bars.");
13491#if USE_TOOLKIT_SCROLL_BARS
13492 x_toolkit_scroll_bars_p = 1;
13493#else
13494 x_toolkit_scroll_bars_p = 0;
13495#endif
13496
06a2c219
GM
13497 staticpro (&last_mouse_motion_frame);
13498 last_mouse_motion_frame = Qnil;
55123275 13499}
6cf0ae86
RS
13500
13501#endif /* not HAVE_X_WINDOWS */
06a2c219 13502