(XChar2b): Move typedef here for consolidation.
[bpt/emacs.git] / src / xterm.c
CommitLineData
dc6f92b8 1/* X Communication module for terminals which understand the X protocol.
abfb6b46 2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000, 2001, 2002
06a2c219 3 Free Software Foundation, Inc.
dc6f92b8
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
4746118a 9the Free Software Foundation; either version 2, or (at your option)
dc6f92b8
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
dc6f92b8 21
06a2c219 22/* New display code by Gerd Moellmann <gerd@gnu.org>. */
3afe33e7
RS
23/* Xt features made by Fred Pierresteguy. */
24
68c45bf0
PE
25#include <config.h>
26
039440c4 27/* On 4.3 these lose if they come after xterm.h. */
039440c4 28/* Putting these at the beginning seems to be standard for other .c files. */
039440c4
RS
29#include <signal.h>
30
4846819e
RS
31#include <stdio.h>
32
dc6f92b8
JB
33#ifdef HAVE_X_WINDOWS
34
35#include "lisp.h"
9ac0d9e0 36#include "blockinput.h"
dc6f92b8 37
ae79c227
AS
38/* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40#include "syssignal.h"
41
dc6f92b8
JB
42/* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44#include "xterm.h"
f451eb13 45#include <X11/cursorfont.h>
dc6f92b8
JB
46
47/* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49#ifndef makedev
50#include <sys/types.h>
c118dd06 51#endif /* makedev */
dc6f92b8 52
6df54671 53#ifdef BSD_SYSTEM
dc6f92b8 54#include <sys/ioctl.h>
6df54671 55#endif /* ! defined (BSD_SYSTEM) */
dc6f92b8 56
2d368234 57#include "systty.h"
3a2712f9 58#include "systime.h"
dc6f92b8 59
b8009dd1 60#ifndef INCLUDED_FCNTL
dc6f92b8 61#include <fcntl.h>
b8009dd1 62#endif
dc6f92b8
JB
63#include <ctype.h>
64#include <errno.h>
65#include <setjmp.h>
66#include <sys/stat.h>
a0a7635f
RS
67/* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
68/* #include <sys/param.h> */
dc6f92b8 69
dc43ef94 70#include "charset.h"
379b5ac0 71#include "coding.h"
dc43ef94 72#include "ccl.h"
7a13e894 73#include "frame.h"
dc6f92b8 74#include "dispextern.h"
ee569018 75#include "fontset.h"
dc6f92b8
JB
76#include "termhooks.h"
77#include "termopts.h"
78#include "termchar.h"
dc6f92b8 79#include "gnu.h"
dc6f92b8 80#include "disptab.h"
dc6f92b8 81#include "buffer.h"
f451eb13 82#include "window.h"
3b2fa4e6 83#include "keyboard.h"
bde7c500 84#include "intervals.h"
dfcf069d 85#include "process.h"
bffcfca9 86#include "atimer.h"
8feddab4 87#include "keymap.h"
dc6f92b8 88
d2bd6bc4
RS
89#ifdef USE_X_TOOLKIT
90#include <X11/Shell.h>
91#endif
92
06a2c219
GM
93#ifdef HAVE_SYS_TIME_H
94#include <sys/time.h>
95#endif
96#ifdef HAVE_UNISTD_H
97#include <unistd.h>
98#endif
99
488dd4c4
JD
100#ifdef USE_GTK
101#include "gtkutil.h"
102#endif
103
d624284c 104#ifdef USE_LUCID
55a8b3ed 105extern int xlwmenu_window_p P_ ((Widget w, Window window));
d624284c
PJ
106extern void xlwmenu_redisplay P_ ((Widget));
107#endif
108
488dd4c4 109#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
06a2c219 110
952291d9
GM
111extern void free_frame_menubar P_ ((struct frame *));
112extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
113 int));
488dd4c4 114#endif
06a2c219 115
488dd4c4 116#ifdef USE_X_TOOLKIT
0fdff6bb
RS
117#if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
118#define HACK_EDITRES
119extern void _XEditResCheckMessages ();
120#endif /* not NO_EDITRES */
06a2c219
GM
121
122/* Include toolkit specific headers for the scroll bar widget. */
123
124#ifdef USE_TOOLKIT_SCROLL_BARS
125#if defined USE_MOTIF
126#include <Xm/Xm.h> /* for LESSTIF_VERSION */
127#include <Xm/ScrollBar.h>
ec18280f
SM
128#else /* !USE_MOTIF i.e. use Xaw */
129
130#ifdef HAVE_XAW3D
06a2c219 131#include <X11/Xaw3d/Simple.h>
06a2c219
GM
132#include <X11/Xaw3d/Scrollbar.h>
133#define ARROW_SCROLLBAR
134#include <X11/Xaw3d/ScrollbarP.h>
ec18280f
SM
135#else /* !HAVE_XAW3D */
136#include <X11/Xaw/Simple.h>
137#include <X11/Xaw/Scrollbar.h>
138#endif /* !HAVE_XAW3D */
139#ifndef XtNpickTop
140#define XtNpickTop "pickTop"
141#endif /* !XtNpickTop */
142#endif /* !USE_MOTIF */
06a2c219
GM
143#endif /* USE_TOOLKIT_SCROLL_BARS */
144
3afe33e7
RS
145#endif /* USE_X_TOOLKIT */
146
488dd4c4 147#if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
b849c413 148#define x_any_window_to_frame x_window_to_frame
5627c40e 149#define x_top_window_to_frame x_window_to_frame
b849c413
RS
150#endif
151
546e6d5b 152#ifdef USE_X_TOOLKIT
d067ea8b 153#include "widget.h"
546e6d5b
RS
154#ifndef XtNinitialState
155#define XtNinitialState "initialState"
156#endif
157#endif
158
06a2c219
GM
159#define abs(x) ((x) < 0 ? -(x) : (x))
160
161#define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
162
163\f
06a2c219
GM
164
165extern Lisp_Object Qhelp_echo;
166
5bf04520 167/* Non-nil means Emacs uses toolkit scroll bars. */
06a2c219 168
5bf04520 169Lisp_Object Vx_toolkit_scroll_bars;
06a2c219
GM
170
171/* If a string, XTread_socket generates an event to display that string.
172 (The display is done in read_char.) */
7d0393cf 173
06a2c219 174static Lisp_Object help_echo;
7cea38bc 175static Lisp_Object help_echo_window;
be010514
GM
176static Lisp_Object help_echo_object;
177static int help_echo_pos;
06a2c219
GM
178
179/* Temporary variable for XTread_socket. */
180
181static Lisp_Object previous_help_echo;
182
183/* Non-zero means that a HELP_EVENT has been generated since Emacs
184 start. */
185
186static int any_help_event_p;
187
503e457e
PJ
188/* Non-zero means autoselect window with the mouse cursor. */
189
ee8ceff8 190int mouse_autoselect_window;
5bc62483 191
ee8ceff8 192/* Last window where we saw the mouse. Used by mouse-autoselect-window. */
5bc62483 193static Lisp_Object last_window;
503e457e 194
06a2c219
GM
195/* Non-zero means draw block and hollow cursor as wide as the glyph
196 under it. For example, if a block cursor is over a tab, it will be
197 drawn as wide as that tab on the display. */
198
199int x_stretch_cursor_p;
200
a72d5ce5
GM
201/* Non-zero means make use of UNDERLINE_POSITION font properties. */
202
203int x_use_underline_position_properties;
204
06a2c219
GM
205/* This is a chain of structures for all the X displays currently in
206 use. */
207
334208b7 208struct x_display_info *x_display_list;
dc6f92b8 209
06a2c219
GM
210/* This is a list of cons cells, each of the form (NAME
211 . FONT-LIST-CACHE), one for each element of x_display_list and in
212 the same order. NAME is the name of the frame. FONT-LIST-CACHE
213 records previous values returned by x-list-fonts. */
214
7a13e894 215Lisp_Object x_display_name_list;
f451eb13 216
987d2ad1 217/* Frame being updated by update_frame. This is declared in term.c.
06a2c219
GM
218 This is set by update_begin and looked at by all the XT functions.
219 It is zero while not inside an update. In that case, the XT
220 functions assume that `selected_frame' is the frame to apply to. */
221
d0386f2a 222extern struct frame *updating_frame;
dc6f92b8 223
06a2c219
GM
224/* This is a frame waiting to be auto-raised, within XTread_socket. */
225
0134a210
RS
226struct frame *pending_autoraise_frame;
227
7f9c7f94
RS
228#ifdef USE_X_TOOLKIT
229/* The application context for Xt use. */
230XtAppContext Xt_app_con;
06a2c219
GM
231static String Xt_default_resources[] = {0};
232#endif /* USE_X_TOOLKIT */
665881ad 233
7d0393cf 234/* Nominal cursor position -- where to draw output.
06a2c219
GM
235 HPOS and VPOS are window relative glyph matrix coordinates.
236 X and Y are window relative pixel coordinates. */
dc6f92b8 237
06a2c219 238struct cursor_pos output_cursor;
dc6f92b8 239
bffcfca9
GM
240/* Non-zero means user is interacting with a toolkit scroll bar. */
241
242static int toolkit_scroll_bar_interaction;
dc6f92b8 243
69388238
RS
244/* Mouse movement.
245
06a2c219 246 Formerly, we used PointerMotionHintMask (in standard_event_mask)
f5bb65ec
RS
247 so that we would have to call XQueryPointer after each MotionNotify
248 event to ask for another such event. However, this made mouse tracking
249 slow, and there was a bug that made it eventually stop.
250
251 Simply asking for MotionNotify all the time seems to work better.
252
69388238
RS
253 In order to avoid asking for motion events and then throwing most
254 of them away or busy-polling the server for mouse positions, we ask
255 the server for pointer motion hints. This means that we get only
256 one event per group of mouse movements. "Groups" are delimited by
257 other kinds of events (focus changes and button clicks, for
258 example), or by XQueryPointer calls; when one of these happens, we
259 get another MotionNotify event the next time the mouse moves. This
260 is at least as efficient as getting motion events when mouse
261 tracking is on, and I suspect only negligibly worse when tracking
f5bb65ec 262 is off. */
69388238
RS
263
264/* Where the mouse was last time we reported a mouse event. */
69388238 265
06a2c219
GM
266FRAME_PTR last_mouse_frame;
267static XRectangle last_mouse_glyph;
2237cac9
RS
268static Lisp_Object last_mouse_press_frame;
269
69388238
RS
270/* The scroll bar in which the last X motion event occurred.
271
06a2c219
GM
272 If the last X motion event occurred in a scroll bar, we set this so
273 XTmouse_position can know whether to report a scroll bar motion or
69388238
RS
274 an ordinary motion.
275
06a2c219
GM
276 If the last X motion event didn't occur in a scroll bar, we set
277 this to Qnil, to tell XTmouse_position to return an ordinary motion
278 event. */
279
69388238
RS
280static Lisp_Object last_mouse_scroll_bar;
281
69388238
RS
282/* This is a hack. We would really prefer that XTmouse_position would
283 return the time associated with the position it returns, but there
06a2c219 284 doesn't seem to be any way to wrest the time-stamp from the server
69388238
RS
285 along with the position query. So, we just keep track of the time
286 of the last movement we received, and return that in hopes that
287 it's somewhat accurate. */
06a2c219 288
69388238
RS
289static Time last_mouse_movement_time;
290
06a2c219
GM
291/* Incremented by XTread_socket whenever it really tries to read
292 events. */
293
c0a04927
RS
294#ifdef __STDC__
295static int volatile input_signal_count;
296#else
297static int input_signal_count;
298#endif
299
7a13e894 300/* Used locally within XTread_socket. */
06a2c219 301
7a13e894 302static int x_noop_count;
dc6f92b8 303
7a13e894 304/* Initial values of argv and argc. */
06a2c219 305
7a13e894
RS
306extern char **initial_argv;
307extern int initial_argc;
dc6f92b8 308
7a13e894 309extern Lisp_Object Vcommand_line_args, Vsystem_name;
dc6f92b8 310
06a2c219 311/* Tells if a window manager is present or not. */
7a13e894
RS
312
313extern Lisp_Object Vx_no_window_manager;
dc6f92b8 314
dbd4b028 315extern Lisp_Object Qface, Qmouse_face, Qeql;
b8009dd1 316
dc6f92b8
JB
317extern int errno;
318
dfeccd2d 319/* A mask of extra modifier bits to put into every keyboard char. */
06a2c219 320
31ade731 321extern EMACS_INT extra_keyboard_modifiers;
64bb1782 322
98659da6
KG
323/* The keysyms to use for the various modifiers. */
324
325Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
dbd4b028 326Lisp_Object Vx_keysym_table;
98659da6
KG
327static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
328
59e755be 329static Lisp_Object Qvendor_specific_keysyms;
c997eae5 330static Lisp_Object Qlatin_1, Qutf_8;
59e755be 331
952291d9
GM
332extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
333extern Lisp_Object x_icon_type P_ ((struct frame *));
c32cdd9a 334
7a13e894 335
b7f83f9e 336static int cursor_in_mouse_face_p P_ ((struct window *));
fa262c07 337static int clear_mouse_face P_ ((struct x_display_info *));
651f03b6 338static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
499b1844 339static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
651f03b6 340static const XColor *x_color_cells P_ ((Display *, int *));
71b8321e 341static void x_update_window_end P_ ((struct window *, int, int));
06a2c219
GM
342static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
343void x_delete_display P_ ((struct x_display_info *));
344static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
345 unsigned));
346static int fast_find_position P_ ((struct window *, int, int *, int *,
7e376260 347 int *, int *, Lisp_Object));
f9db2310
GM
348static int fast_find_string_pos P_ ((struct window *, int, Lisp_Object,
349 int *, int *, int *, int *, int));
06a2c219
GM
350static void set_output_cursor P_ ((struct cursor_pos *));
351static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
f9db2310 352 int *, int *, int *, int));
b436feb6
GM
353static void note_mode_line_or_margin_highlight P_ ((struct window *, int,
354 int, int));
06a2c219 355static void note_mouse_highlight P_ ((struct frame *, int, int));
9ea173e8
GM
356static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
357static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
06a2c219
GM
358static void show_mouse_face P_ ((struct x_display_info *,
359 enum draw_glyphs_face));
360static int x_io_error_quitter P_ ((Display *));
361int x_catch_errors P_ ((Display *));
362void x_uncatch_errors P_ ((Display *, int));
363void x_lower_frame P_ ((struct frame *));
364void x_scroll_bar_clear P_ ((struct frame *));
365int x_had_errors_p P_ ((Display *));
366void x_wm_set_size_hint P_ ((struct frame *, long, int));
367void x_raise_frame P_ ((struct frame *));
368void x_set_window_size P_ ((struct frame *, int, int, int));
369void x_wm_set_window_state P_ ((struct frame *, int));
370void x_wm_set_icon_pixmap P_ ((struct frame *, int));
371void x_initialize P_ ((void));
372static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
373static int x_compute_min_glyph_bounds P_ ((struct frame *));
374static void x_draw_phys_cursor_glyph P_ ((struct window *,
375 struct glyph_row *,
376 enum draw_glyphs_face));
377static void x_update_end P_ ((struct frame *));
378static void XTframe_up_to_date P_ ((struct frame *));
06a2c219
GM
379static void XTset_terminal_modes P_ ((void));
380static void XTreset_terminal_modes P_ ((void));
381static void XTcursor_to P_ ((int, int, int, int));
382static void x_write_glyphs P_ ((struct glyph *, int));
383static void x_clear_end_of_line P_ ((int));
384static void x_clear_frame P_ ((void));
385static void x_clear_cursor P_ ((struct window *));
386static void frame_highlight P_ ((struct frame *));
387static void frame_unhighlight P_ ((struct frame *));
388static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
cab34f50
JD
389static int x_focus_changed P_ ((int,
390 int,
391 struct x_display_info *,
392 struct frame *,
393 struct input_event *,
394 int));
395static int x_detect_focus_change P_ ((struct x_display_info *,
396 XEvent *,
397 struct input_event *,
398 int));
06a2c219
GM
399static void XTframe_rehighlight P_ ((struct frame *));
400static void x_frame_rehighlight P_ ((struct x_display_info *));
401static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
65c26775
EZ
402static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
403 enum text_cursor_kinds));
06a2c219
GM
404static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
405 XRectangle *));
406static void expose_frame P_ ((struct frame *, int, int, int, int));
82f053ab 407static int expose_window_tree P_ ((struct window *, XRectangle *));
4f53f3b8
RS
408static void expose_overlaps P_ ((struct window *, struct glyph_row *,
409 struct glyph_row *));
a39202f6 410static int expose_window P_ ((struct window *, XRectangle *));
06a2c219
GM
411static void expose_area P_ ((struct window *, struct glyph_row *,
412 XRectangle *, enum glyph_row_area));
82f053ab 413static int expose_line P_ ((struct window *, struct glyph_row *,
06a2c219
GM
414 XRectangle *));
415static void x_update_cursor_in_window_tree P_ ((struct window *, int));
416static void x_update_window_cursor P_ ((struct window *, int));
417static void x_erase_phys_cursor P_ ((struct window *));
418void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
06a2c219
GM
419
420static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
421 GC, int));
422static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
06a2c219 423static void x_flush P_ ((struct frame *f));
952291d9
GM
424static void x_update_begin P_ ((struct frame *));
425static void x_update_window_begin P_ ((struct window *));
426static void x_draw_vertical_border P_ ((struct window *));
427static void x_after_update_window_line P_ ((struct glyph_row *));
428static INLINE void take_vertical_position_into_account P_ ((struct it *));
b52b65bd
GM
429static struct scroll_bar *x_window_to_scroll_bar P_ ((Window));
430static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
431 enum scroll_bar_part *,
432 Lisp_Object *, Lisp_Object *,
433 unsigned long *));
d0fa3e56
EZ
434static void x_check_fullscreen P_ ((struct frame *));
435static void x_check_fullscreen_move P_ ((struct frame *));
488dd4c4
JD
436static int handle_one_xevent P_ ((struct x_display_info *,
437 XEvent *,
438 struct input_event **,
439 int *,
440 int *));
441
06a2c219
GM
442
443/* Flush display of frame F, or of all frames if F is null. */
444
445static void
446x_flush (f)
447 struct frame *f;
448{
449 BLOCK_INPUT;
450 if (f == NULL)
451 {
452 Lisp_Object rest, frame;
453 FOR_EACH_FRAME (rest, frame)
454 x_flush (XFRAME (frame));
455 }
456 else if (FRAME_X_P (f))
457 XFlush (FRAME_X_DISPLAY (f));
458 UNBLOCK_INPUT;
459}
460
dc6f92b8 461
06a2c219
GM
462/* Remove calls to XFlush by defining XFlush to an empty replacement.
463 Calls to XFlush should be unnecessary because the X output buffer
464 is flushed automatically as needed by calls to XPending,
465 XNextEvent, or XWindowEvent according to the XFlush man page.
466 XTread_socket calls XPending. Removing XFlush improves
467 performance. */
468
7d0393cf 469#define XFlush(DISPLAY) (void) 0
b8009dd1 470
334208b7 471\f
06a2c219
GM
472/***********************************************************************
473 Debugging
474 ***********************************************************************/
475
9382638d 476#if 0
06a2c219
GM
477
478/* This is a function useful for recording debugging information about
479 the sequence of occurrences in this file. */
9382638d 480
7d0393cf 481struct record
9382638d
KH
482{
483 char *locus;
484 int type;
485};
486
487struct record event_record[100];
488
489int event_record_index;
490
491record_event (locus, type)
492 char *locus;
493 int type;
494{
495 if (event_record_index == sizeof (event_record) / sizeof (struct record))
496 event_record_index = 0;
497
498 event_record[event_record_index].locus = locus;
499 event_record[event_record_index].type = type;
500 event_record_index++;
501}
502
503#endif /* 0 */
06a2c219
GM
504
505
9382638d 506\f
334208b7
RS
507/* Return the struct x_display_info corresponding to DPY. */
508
509struct x_display_info *
510x_display_info_for_display (dpy)
511 Display *dpy;
512{
513 struct x_display_info *dpyinfo;
514
515 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
516 if (dpyinfo->display == dpy)
517 return dpyinfo;
16bd92ea 518
334208b7
RS
519 return 0;
520}
f451eb13 521
06a2c219
GM
522
523\f
524/***********************************************************************
525 Starting and ending an update
526 ***********************************************************************/
7d0393cf 527
06a2c219
GM
528/* Start an update of frame F. This function is installed as a hook
529 for update_begin, i.e. it is called when update_begin is called.
530 This function is called prior to calls to x_update_window_begin for
531 each window being updated. Currently, there is nothing to do here
532 because all interesting stuff is done on a window basis. */
dc6f92b8 533
dfcf069d 534static void
06a2c219 535x_update_begin (f)
f676886a 536 struct frame *f;
58769bee 537{
06a2c219
GM
538 /* Nothing to do. */
539}
dc6f92b8 540
dc6f92b8 541
06a2c219
GM
542/* Start update of window W. Set the global variable updated_window
543 to the window being updated and set output_cursor to the cursor
544 position of W. */
dc6f92b8 545
06a2c219
GM
546static void
547x_update_window_begin (w)
548 struct window *w;
549{
550 struct frame *f = XFRAME (WINDOW_FRAME (w));
551 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
7d0393cf 552
06a2c219
GM
553 updated_window = w;
554 set_output_cursor (&w->cursor);
b8009dd1 555
06a2c219 556 BLOCK_INPUT;
d1bc4182 557
06a2c219 558 if (f == display_info->mouse_face_mouse_frame)
b8009dd1 559 {
514e4681 560 /* Don't do highlighting for mouse motion during the update. */
06a2c219 561 display_info->mouse_face_defer = 1;
37c2c98b 562
06a2c219
GM
563 /* If F needs to be redrawn, simply forget about any prior mouse
564 highlighting. */
9f67f20b 565 if (FRAME_GARBAGED_P (f))
06a2c219
GM
566 display_info->mouse_face_window = Qnil;
567
64f26cf5
GM
568#if 0 /* Rows in a current matrix containing glyphs in mouse-face have
569 their mouse_face_p flag set, which means that they are always
570 unequal to rows in a desired matrix which never have that
571 flag set. So, rows containing mouse-face glyphs are never
572 scrolled, and we don't have to switch the mouse highlight off
573 here to prevent it from being scrolled. */
7d0393cf 574
06a2c219
GM
575 /* Can we tell that this update does not affect the window
576 where the mouse highlight is? If so, no need to turn off.
577 Likewise, don't do anything if the frame is garbaged;
578 in that case, the frame's current matrix that we would use
579 is all wrong, and we will redisplay that line anyway. */
580 if (!NILP (display_info->mouse_face_window)
581 && w == XWINDOW (display_info->mouse_face_window))
514e4681 582 {
06a2c219 583 int i;
514e4681 584
06a2c219
GM
585 for (i = 0; i < w->desired_matrix->nrows; ++i)
586 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
514e4681
RS
587 break;
588
06a2c219
GM
589 if (i < w->desired_matrix->nrows)
590 clear_mouse_face (display_info);
514e4681 591 }
64f26cf5 592#endif /* 0 */
b8009dd1 593 }
6ccf47d1 594
dc6f92b8
JB
595 UNBLOCK_INPUT;
596}
597
06a2c219
GM
598
599/* Draw a vertical window border to the right of window W if W doesn't
600 have vertical scroll bars. */
601
dfcf069d 602static void
06a2c219
GM
603x_draw_vertical_border (w)
604 struct window *w;
58769bee 605{
06a2c219 606 struct frame *f = XFRAME (WINDOW_FRAME (w));
7d0393cf 607
06a2c219
GM
608 /* Redraw borders between horizontally adjacent windows. Don't
609 do it for frames with vertical scroll bars because either the
610 right scroll bar of a window, or the left scroll bar of its
611 neighbor will suffice as a border. */
612 if (!WINDOW_RIGHTMOST_P (w)
613 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
614 {
615 int x0, x1, y0, y1;
dc6f92b8 616
06a2c219 617 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
3f332ef3 618 x1 += FRAME_X_RIGHT_FRINGE_WIDTH (f);
06a2c219 619 y1 -= 1;
7d0393cf
JB
620
621 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
06a2c219
GM
622 f->output_data.x->normal_gc, x1, y0, x1, y1);
623 }
624}
7d0393cf
JB
625
626
71b8321e
GM
627/* End update of window W (which is equal to updated_window).
628
629 Draw vertical borders between horizontally adjacent windows, and
630 display W's cursor if CURSOR_ON_P is non-zero.
631
632 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
633 glyphs in mouse-face were overwritten. In that case we have to
634 make sure that the mouse-highlight is properly redrawn.
635
636 W may be a menu bar pseudo-window in case we don't have X toolkit
637 support. Such windows don't have a cursor, so don't display it
638 here. */
06a2c219
GM
639
640static void
71b8321e 641x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
06a2c219 642 struct window *w;
71b8321e 643 int cursor_on_p, mouse_face_overwritten_p;
06a2c219 644{
140330de 645 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7d0393cf 646
06a2c219
GM
647 if (!w->pseudo_window_p)
648 {
649 BLOCK_INPUT;
71b8321e 650
06a2c219
GM
651 if (cursor_on_p)
652 x_display_and_set_cursor (w, 1, output_cursor.hpos,
653 output_cursor.vpos,
654 output_cursor.x, output_cursor.y);
7d0393cf 655
06a2c219
GM
656 x_draw_vertical_border (w);
657 UNBLOCK_INPUT;
658 }
7d0393cf 659
140330de
GM
660 /* If a row with mouse-face was overwritten, arrange for
661 XTframe_up_to_date to redisplay the mouse highlight. */
662 if (mouse_face_overwritten_p)
663 {
664 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
665 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
666 dpyinfo->mouse_face_window = Qnil;
667 }
7d0393cf 668
06a2c219
GM
669 updated_window = NULL;
670}
dc6f92b8 671
dc6f92b8 672
06a2c219
GM
673/* End update of frame F. This function is installed as a hook in
674 update_end. */
675
676static void
677x_update_end (f)
678 struct frame *f;
679{
680 /* Mouse highlight may be displayed again. */
aa8bff2e 681 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
b8009dd1 682
06a2c219 683 BLOCK_INPUT;
334208b7 684 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
685 UNBLOCK_INPUT;
686}
b8009dd1 687
06a2c219
GM
688
689/* This function is called from various places in xdisp.c whenever a
690 complete update has been performed. The global variable
691 updated_window is not available here. */
b8009dd1 692
dfcf069d 693static void
b8009dd1 694XTframe_up_to_date (f)
06a2c219 695 struct frame *f;
b8009dd1 696{
06a2c219 697 if (FRAME_X_P (f))
514e4681 698 {
06a2c219 699 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
71b8321e 700
06a2c219
GM
701 if (dpyinfo->mouse_face_deferred_gc
702 || f == dpyinfo->mouse_face_mouse_frame)
703 {
704 BLOCK_INPUT;
705 if (dpyinfo->mouse_face_mouse_frame)
706 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
707 dpyinfo->mouse_face_mouse_x,
708 dpyinfo->mouse_face_mouse_y);
709 dpyinfo->mouse_face_deferred_gc = 0;
710 UNBLOCK_INPUT;
711 }
514e4681 712 }
b8009dd1 713}
06a2c219
GM
714
715
716/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
3f332ef3 717 arrow bitmaps, or clear the fringes if no bitmaps are required
06a2c219
GM
718 before DESIRED_ROW is made current. The window being updated is
719 found in updated_window. This function It is called from
720 update_window_line only if it is known that there are differences
721 between bitmaps to be drawn between current row and DESIRED_ROW. */
722
723static void
724x_after_update_window_line (desired_row)
725 struct glyph_row *desired_row;
726{
727 struct window *w = updated_window;
ef253080 728 struct frame *f;
259cf6bc 729 int width, height;
7d0393cf 730
06a2c219 731 xassert (w);
7d0393cf 732
06a2c219
GM
733 if (!desired_row->mode_line_p && !w->pseudo_window_p)
734 {
735 BLOCK_INPUT;
5958f265 736 draw_row_fringe_bitmaps (w, desired_row);
ef253080
GM
737 UNBLOCK_INPUT;
738 }
7d0393cf 739
ef253080
GM
740 /* When a window has disappeared, make sure that no rest of
741 full-width rows stays visible in the internal border. Could
742 check here if updated_window is the leftmost/rightmost window,
743 but I guess it's not worth doing since vertically split windows
744 are almost never used, internal border is rarely set, and the
745 overhead is very small. */
746 if (windows_or_buffers_changed
747 && desired_row->full_width_p
748 && (f = XFRAME (w->frame),
749 width = FRAME_INTERNAL_BORDER_WIDTH (f),
259cf6bc
GM
750 width != 0)
751 && (height = desired_row->visible_height,
752 height > 0))
ef253080 753 {
ef253080 754 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
06a2c219 755
ef253080
GM
756 /* Internal border is drawn below the tool bar. */
757 if (WINDOWP (f->tool_bar_window)
758 && w == XWINDOW (f->tool_bar_window))
759 y -= width;
7d0393cf 760
ef253080
GM
761 BLOCK_INPUT;
762 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
763 0, y, width, height, False);
764 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
765 f->output_data.x->pixel_width - width,
766 y, width, height, False);
06a2c219
GM
767 UNBLOCK_INPUT;
768 }
769}
770
06a2c219 771static void
5958f265 772x_draw_fringe_bitmap (w, row, p)
06a2c219
GM
773 struct window *w;
774 struct glyph_row *row;
5958f265 775 struct draw_fringe_bitmap_params *p;
06a2c219
GM
776{
777 struct frame *f = XFRAME (WINDOW_FRAME (w));
778 Display *display = FRAME_X_DISPLAY (f);
779 Window window = FRAME_X_WINDOW (f);
06a2c219 780 GC gc = f->output_data.x->normal_gc;
5958f265 781 struct face *face = p->face;
06a2c219
GM
782
783 /* Must clip because of partially visible lines. */
784 x_clip_to_row (w, row, gc, 1);
785
5958f265 786 if (p->bx >= 0)
976b73d7 787 {
976b73d7
KS
788 /* In case the same realized face is used for fringes and
789 for something displayed in the text (e.g. face `region' on
790 mono-displays, the fill style may have been changed to
791 FillSolid in x_draw_glyph_string_background. */
792 if (face->stipple)
793 XSetFillStyle (display, face->gc, FillOpaqueStippled);
794 else
795 XSetForeground (display, face->gc, face->background);
7d0393cf 796
976b73d7 797 XFillRectangle (display, window, face->gc,
5958f265
KS
798 p->bx, p->by, p->nx, p->ny);
799
976b73d7
KS
800 if (!face->stipple)
801 XSetForeground (display, face->gc, face->foreground);
802 }
803
5958f265 804 if (p->which != NO_FRINGE_BITMAP)
dbdc9702 805 {
5958f265
KS
806 unsigned char *bits = fringe_bitmaps[p->which].bits + p->dh;
807 Pixmap pixmap;
808 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
809
dbdc9702
GM
810 /* Draw the bitmap. I believe these small pixmaps can be cached
811 by the server. */
5958f265 812 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
dbdc9702
GM
813 face->foreground,
814 face->background, depth);
5958f265
KS
815 XCopyArea (display, pixmap, window, gc, 0, 0,
816 p->wd, p->h, p->x, p->y);
dbdc9702
GM
817 XFreePixmap (display, pixmap);
818 }
7d0393cf 819
06a2c219
GM
820 XSetClipMask (display, gc, None);
821}
822
dc6f92b8 823\f
06a2c219
GM
824
825/* This is called when starting Emacs and when restarting after
826 suspend. When starting Emacs, no X window is mapped. And nothing
827 must be done to Emacs's own window if it is suspended (though that
828 rarely happens). */
dc6f92b8 829
dfcf069d 830static void
dc6f92b8
JB
831XTset_terminal_modes ()
832{
833}
834
06a2c219
GM
835/* This is called when exiting or suspending Emacs. Exiting will make
836 the X-windows go away, and suspending requires no action. */
dc6f92b8 837
dfcf069d 838static void
dc6f92b8
JB
839XTreset_terminal_modes ()
840{
dc6f92b8 841}
06a2c219
GM
842
843
dc6f92b8 844\f
06a2c219
GM
845/***********************************************************************
846 Output Cursor
847 ***********************************************************************/
848
849/* Set the global variable output_cursor to CURSOR. All cursor
850 positions are relative to updated_window. */
dc6f92b8 851
dfcf069d 852static void
06a2c219
GM
853set_output_cursor (cursor)
854 struct cursor_pos *cursor;
dc6f92b8 855{
06a2c219
GM
856 output_cursor.hpos = cursor->hpos;
857 output_cursor.vpos = cursor->vpos;
858 output_cursor.x = cursor->x;
859 output_cursor.y = cursor->y;
860}
861
862
863/* Set a nominal cursor position.
dc6f92b8 864
06a2c219
GM
865 HPOS and VPOS are column/row positions in a window glyph matrix. X
866 and Y are window text area relative pixel positions.
7d0393cf 867
06a2c219
GM
868 If this is done during an update, updated_window will contain the
869 window that is being updated and the position is the future output
870 cursor position for that window. If updated_window is null, use
871 selected_window and display the cursor at the given position. */
872
873static void
874XTcursor_to (vpos, hpos, y, x)
875 int vpos, hpos, y, x;
876{
877 struct window *w;
878
879 /* If updated_window is not set, work on selected_window. */
880 if (updated_window)
881 w = updated_window;
882 else
883 w = XWINDOW (selected_window);
dbcb258a 884
06a2c219
GM
885 /* Set the output cursor. */
886 output_cursor.hpos = hpos;
887 output_cursor.vpos = vpos;
888 output_cursor.x = x;
889 output_cursor.y = y;
dc6f92b8 890
06a2c219
GM
891 /* If not called as part of an update, really display the cursor.
892 This will also set the cursor position of W. */
893 if (updated_window == NULL)
dc6f92b8
JB
894 {
895 BLOCK_INPUT;
06a2c219 896 x_display_cursor (w, 1, hpos, vpos, x, y);
b86bd3dd 897 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
dc6f92b8
JB
898 UNBLOCK_INPUT;
899 }
900}
dc43ef94 901
06a2c219
GM
902
903\f
904/***********************************************************************
905 Display Iterator
906 ***********************************************************************/
907
908/* Function prototypes of this page. */
909
5da0698e 910static int x_encode_char P_ ((int, XChar2b *, struct font_info *, int *));
ee569018
KH
911
912
e2ef8ee6
GM
913/* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
914 is not contained in the font. */
dc43ef94 915
5da0698e
KS
916static XCharStruct *
917x_per_char_metric (font, char2b, font_type)
06a2c219
GM
918 XFontStruct *font;
919 XChar2b *char2b;
5da0698e 920 int font_type; /* unused on X */
06a2c219
GM
921{
922 /* The result metric information. */
923 XCharStruct *pcm = NULL;
dc6f92b8 924
06a2c219 925 xassert (font && char2b);
dc6f92b8 926
06a2c219 927 if (font->per_char != NULL)
dc6f92b8 928 {
06a2c219 929 if (font->min_byte1 == 0 && font->max_byte1 == 0)
dc43ef94 930 {
06a2c219
GM
931 /* min_char_or_byte2 specifies the linear character index
932 corresponding to the first element of the per_char array,
933 max_char_or_byte2 is the index of the last character. A
934 character with non-zero CHAR2B->byte1 is not in the font.
935 A character with byte2 less than min_char_or_byte2 or
936 greater max_char_or_byte2 is not in the font. */
937 if (char2b->byte1 == 0
938 && char2b->byte2 >= font->min_char_or_byte2
939 && char2b->byte2 <= font->max_char_or_byte2)
940 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
dc43ef94 941 }
06a2c219 942 else
dc6f92b8 943 {
06a2c219
GM
944 /* If either min_byte1 or max_byte1 are nonzero, both
945 min_char_or_byte2 and max_char_or_byte2 are less than
946 256, and the 2-byte character index values corresponding
947 to the per_char array element N (counting from 0) are:
948
949 byte1 = N/D + min_byte1
950 byte2 = N\D + min_char_or_byte2
951
952 where:
953
954 D = max_char_or_byte2 - min_char_or_byte2 + 1
955 / = integer division
956 \ = integer modulus */
957 if (char2b->byte1 >= font->min_byte1
958 && char2b->byte1 <= font->max_byte1
959 && char2b->byte2 >= font->min_char_or_byte2
960 && char2b->byte2 <= font->max_char_or_byte2)
961 {
962 pcm = (font->per_char
963 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
964 * (char2b->byte1 - font->min_byte1))
965 + (char2b->byte2 - font->min_char_or_byte2));
966 }
dc6f92b8 967 }
06a2c219
GM
968 }
969 else
970 {
971 /* If the per_char pointer is null, all glyphs between the first
972 and last character indexes inclusive have the same
973 information, as given by both min_bounds and max_bounds. */
974 if (char2b->byte2 >= font->min_char_or_byte2
975 && char2b->byte2 <= font->max_char_or_byte2)
976 pcm = &font->max_bounds;
977 }
dc6f92b8 978
ee569018 979 return ((pcm == NULL
3e71d8f2 980 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
ee569018 981 ? NULL : pcm);
06a2c219 982}
b73b6aaf 983
57b03282 984
06a2c219
GM
985/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
986 the two-byte form of C. Encoding is returned in *CHAR2B. */
dc43ef94 987
5da0698e
KS
988static int
989x_encode_char (c, char2b, font_info, two_byte_p)
06a2c219
GM
990 int c;
991 XChar2b *char2b;
992 struct font_info *font_info;
5da0698e 993 int *two_byte_p;
06a2c219
GM
994{
995 int charset = CHAR_CHARSET (c);
996 XFontStruct *font = font_info->font;
997
998 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
999 This may be either a program in a special encoder language or a
1000 fixed encoding. */
1001 if (font_info->font_encoder)
1002 {
1003 /* It's a program. */
1004 struct ccl_program *ccl = font_info->font_encoder;
1005
1006 if (CHARSET_DIMENSION (charset) == 1)
1007 {
1008 ccl->reg[0] = charset;
1009 ccl->reg[1] = char2b->byte2;
6e666629 1010 ccl->reg[2] = -1;
06a2c219
GM
1011 }
1012 else
1013 {
1014 ccl->reg[0] = charset;
1015 ccl->reg[1] = char2b->byte1;
1016 ccl->reg[2] = char2b->byte2;
1017 }
7d0393cf 1018
06a2c219 1019 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
7d0393cf 1020
06a2c219
GM
1021 /* We assume that MSBs are appropriately set/reset by CCL
1022 program. */
1023 if (font->max_byte1 == 0) /* 1-byte font */
ee569018 1024 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
06a2c219
GM
1025 else
1026 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1027 }
1028 else if (font_info->encoding[charset])
1029 {
1030 /* Fixed encoding scheme. See fontset.h for the meaning of the
1031 encoding numbers. */
1032 int enc = font_info->encoding[charset];
7d0393cf 1033
06a2c219
GM
1034 if ((enc == 1 || enc == 2)
1035 && CHARSET_DIMENSION (charset) == 2)
1036 char2b->byte1 |= 0x80;
7d0393cf 1037
06a2c219
GM
1038 if (enc == 1 || enc == 3)
1039 char2b->byte2 |= 0x80;
1040 }
06a2c219 1041
ee569018 1042 if (two_byte_p)
5da0698e 1043 *two_byte_p = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
b4192550 1044
5da0698e 1045 return FONT_TYPE_UNKNOWN;
06a2c219
GM
1046}
1047
1048
1049/* Estimate the pixel height of the mode or top line on frame F.
1050 FACE_ID specifies what line's height to estimate. */
1051
1052int
1053x_estimate_mode_line_height (f, face_id)
1054 struct frame *f;
1055 enum face_id face_id;
1056{
43281ee3 1057 int height = FONT_HEIGHT (FRAME_FONT (f));
06a2c219
GM
1058
1059 /* This function is called so early when Emacs starts that the face
1060 cache and mode line face are not yet initialized. */
1061 if (FRAME_FACE_CACHE (f))
1062 {
1063 struct face *face = FACE_FROM_ID (f, face_id);
1064 if (face)
43281ee3
GM
1065 {
1066 if (face->font)
1067 height = FONT_HEIGHT (face->font);
ea2ba0d4
KH
1068 if (face->box_line_width > 0)
1069 height += 2 * face->box_line_width;
43281ee3 1070 }
06a2c219 1071 }
7d0393cf 1072
06a2c219
GM
1073 return height;
1074}
1075
1076\f
1077/***********************************************************************
1078 Glyph display
1079 ***********************************************************************/
1080
06a2c219 1081
06a2c219 1082
06a2c219
GM
1083static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
1084static void x_set_glyph_string_gc P_ ((struct glyph_string *));
1085static void x_draw_glyph_string_background P_ ((struct glyph_string *,
1086 int));
1087static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
b4192550 1088static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
06a2c219
GM
1089static void x_draw_glyph_string_box P_ ((struct glyph_string *));
1090static void x_draw_glyph_string P_ ((struct glyph_string *));
1091static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
1092static void x_set_cursor_gc P_ ((struct glyph_string *));
1093static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
1094static void x_set_mouse_face_gc P_ ((struct glyph_string *));
06a2c219 1095static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
68c45bf0 1096 unsigned long *, double, int));
06a2c219 1097static void x_setup_relief_color P_ ((struct frame *, struct relief *,
68c45bf0 1098 double, int, unsigned long));
06a2c219
GM
1099static void x_setup_relief_colors P_ ((struct glyph_string *));
1100static void x_draw_image_glyph_string P_ ((struct glyph_string *));
1101static void x_draw_image_relief P_ ((struct glyph_string *));
1102static void x_draw_image_foreground P_ ((struct glyph_string *));
1103static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
06a2c219
GM
1104static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
1105 int, int, int));
1106static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
1107 int, int, int, int, XRectangle *));
1108static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
1109 int, int, int, XRectangle *));
66ac4b0e 1110static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
5da0698e
KS
1111 enum glyph_row_area));
1112
1113#if GLYPH_DEBUG
1114static void x_check_font P_ ((struct frame *, XFontStruct *));
1115#endif
06a2c219
GM
1116
1117
1118/* Set S->gc to a suitable GC for drawing glyph string S in cursor
1119 face. */
1120
1121static void
1122x_set_cursor_gc (s)
1123 struct glyph_string *s;
1124{
1125 if (s->font == FRAME_FONT (s->f)
1126 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
1127 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
b4192550 1128 && !s->cmp)
06a2c219
GM
1129 s->gc = s->f->output_data.x->cursor_gc;
1130 else
1131 {
1132 /* Cursor on non-default face: must merge. */
1133 XGCValues xgcv;
1134 unsigned long mask;
1135
1136 xgcv.background = s->f->output_data.x->cursor_pixel;
1137 xgcv.foreground = s->face->background;
1138
1139 /* If the glyph would be invisible, try a different foreground. */
1140 if (xgcv.foreground == xgcv.background)
1141 xgcv.foreground = s->face->foreground;
1142 if (xgcv.foreground == xgcv.background)
1143 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
1144 if (xgcv.foreground == xgcv.background)
1145 xgcv.foreground = s->face->foreground;
1146
1147 /* Make sure the cursor is distinct from text in this face. */
1148 if (xgcv.background == s->face->background
1149 && xgcv.foreground == s->face->foreground)
1150 {
1151 xgcv.background = s->face->foreground;
1152 xgcv.foreground = s->face->background;
1153 }
1154
1155 IF_DEBUG (x_check_font (s->f, s->font));
1156 xgcv.font = s->font->fid;
1157 xgcv.graphics_exposures = False;
1158 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
1159
1160 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1161 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1162 mask, &xgcv);
1163 else
1164 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1165 = XCreateGC (s->display, s->window, mask, &xgcv);
1166
1167 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1168 }
1169}
1170
1171
1172/* Set up S->gc of glyph string S for drawing text in mouse face. */
7d0393cf 1173
06a2c219
GM
1174static void
1175x_set_mouse_face_gc (s)
1176 struct glyph_string *s;
7d0393cf 1177{
06a2c219 1178 int face_id;
ee569018 1179 struct face *face;
06a2c219 1180
e4ded23c 1181 /* What face has to be used last for the mouse face? */
06a2c219 1182 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
ee569018 1183 face = FACE_FROM_ID (s->f, face_id);
e4ded23c
GM
1184 if (face == NULL)
1185 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
7d0393cf 1186
033e3e18
GM
1187 if (s->first_glyph->type == CHAR_GLYPH)
1188 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
1189 else
1190 face_id = FACE_FOR_CHAR (s->f, face, 0);
06a2c219
GM
1191 s->face = FACE_FROM_ID (s->f, face_id);
1192 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1193
1194 /* If font in this face is same as S->font, use it. */
1195 if (s->font == s->face->font)
1196 s->gc = s->face->gc;
1197 else
1198 {
1199 /* Otherwise construct scratch_cursor_gc with values from FACE
1200 but font FONT. */
1201 XGCValues xgcv;
1202 unsigned long mask;
7d0393cf 1203
06a2c219
GM
1204 xgcv.background = s->face->background;
1205 xgcv.foreground = s->face->foreground;
1206 IF_DEBUG (x_check_font (s->f, s->font));
1207 xgcv.font = s->font->fid;
1208 xgcv.graphics_exposures = False;
1209 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
7d0393cf 1210
06a2c219
GM
1211 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1212 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1213 mask, &xgcv);
1214 else
1215 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1216 = XCreateGC (s->display, s->window, mask, &xgcv);
7d0393cf 1217
06a2c219
GM
1218 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1219 }
1220
1221 xassert (s->gc != 0);
1222}
1223
1224
1225/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1226 Faces to use in the mode line have already been computed when the
1227 matrix was built, so there isn't much to do, here. */
1228
1229static INLINE void
1230x_set_mode_line_face_gc (s)
1231 struct glyph_string *s;
7d0393cf 1232{
06a2c219 1233 s->gc = s->face->gc;
06a2c219
GM
1234}
1235
1236
1237/* Set S->gc of glyph string S for drawing that glyph string. Set
1238 S->stippled_p to a non-zero value if the face of S has a stipple
1239 pattern. */
1240
1241static INLINE void
1242x_set_glyph_string_gc (s)
1243 struct glyph_string *s;
1244{
209f68d9 1245 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
7d0393cf 1246
06a2c219
GM
1247 if (s->hl == DRAW_NORMAL_TEXT)
1248 {
1249 s->gc = s->face->gc;
1250 s->stippled_p = s->face->stipple != 0;
1251 }
1252 else if (s->hl == DRAW_INVERSE_VIDEO)
1253 {
1254 x_set_mode_line_face_gc (s);
1255 s->stippled_p = s->face->stipple != 0;
1256 }
1257 else if (s->hl == DRAW_CURSOR)
1258 {
1259 x_set_cursor_gc (s);
1260 s->stippled_p = 0;
1261 }
1262 else if (s->hl == DRAW_MOUSE_FACE)
1263 {
1264 x_set_mouse_face_gc (s);
1265 s->stippled_p = s->face->stipple != 0;
1266 }
1267 else if (s->hl == DRAW_IMAGE_RAISED
1268 || s->hl == DRAW_IMAGE_SUNKEN)
1269 {
1270 s->gc = s->face->gc;
1271 s->stippled_p = s->face->stipple != 0;
1272 }
1273 else
1274 {
1275 s->gc = s->face->gc;
1276 s->stippled_p = s->face->stipple != 0;
1277 }
1278
1279 /* GC must have been set. */
1280 xassert (s->gc != 0);
1281}
1282
1283
1284/* Return in *R the clipping rectangle for glyph string S. */
1285
1286static void
1287x_get_glyph_string_clip_rect (s, r)
1288 struct glyph_string *s;
1289 XRectangle *r;
1290{
1291 if (s->row->full_width_p)
1292 {
1293 /* Draw full-width. X coordinates are relative to S->w->left. */
1da3fd71 1294 int canon_x = CANON_X_UNIT (s->f);
7d0393cf 1295
1da3fd71
GM
1296 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
1297 r->width = XFASTINT (s->w->width) * canon_x;
06a2c219
GM
1298
1299 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
1300 {
1da3fd71 1301 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
06a2c219
GM
1302 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
1303 r->x -= width;
1304 }
7d0393cf 1305
b9432a85 1306 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
7d0393cf 1307
06a2c219
GM
1308 /* Unless displaying a mode or menu bar line, which are always
1309 fully visible, clip to the visible part of the row. */
1310 if (s->w->pseudo_window_p)
1311 r->height = s->row->visible_height;
1312 else
1313 r->height = s->height;
1314 }
1315 else
1316 {
1317 /* This is a text line that may be partially visible. */
1318 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
1319 r->width = window_box_width (s->w, s->area);
1320 r->height = s->row->visible_height;
1321 }
1322
66ac4b0e
GM
1323 /* If S draws overlapping rows, it's sufficient to use the top and
1324 bottom of the window for clipping because this glyph string
1325 intentionally draws over other lines. */
1326 if (s->for_overlaps_p)
1327 {
045dee35 1328 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
66ac4b0e
GM
1329 r->height = window_text_bottom_y (s->w) - r->y;
1330 }
98b8a90f
GM
1331 else
1332 {
1333 /* Don't use S->y for clipping because it doesn't take partially
1334 visible lines into account. For example, it can be negative for
1335 partially visible lines at the top of a window. */
1336 if (!s->row->full_width_p
1337 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1338 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
1339 else
1340 r->y = max (0, s->row->y);
1341
1342 /* If drawing a tool-bar window, draw it over the internal border
1343 at the top of the window. */
1344 if (s->w == XWINDOW (s->f->tool_bar_window))
1345 r->y -= s->f->output_data.x->internal_border_width;
1346 }
1347
66ac4b0e 1348 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
06a2c219
GM
1349}
1350
1351
1352/* Set clipping for output of glyph string S. S may be part of a mode
1353 line or menu if we don't have X toolkit support. */
1354
1355static INLINE void
1356x_set_glyph_string_clipping (s)
1357 struct glyph_string *s;
1358{
1359 XRectangle r;
1360 x_get_glyph_string_clip_rect (s, &r);
1361 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
1362}
1363
1364
5da0698e
KS
1365/* RIF:
1366 Compute left and right overhang of glyph string S. If S is a glyph
b4192550 1367 string for a composition, assume overhangs don't exist. */
06a2c219 1368
5da0698e 1369static void
06a2c219
GM
1370x_compute_glyph_string_overhangs (s)
1371 struct glyph_string *s;
1372{
b4192550 1373 if (s->cmp == NULL
06a2c219
GM
1374 && s->first_glyph->type == CHAR_GLYPH)
1375 {
1376 XCharStruct cs;
1377 int direction, font_ascent, font_descent;
1378 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
1379 &font_ascent, &font_descent, &cs);
1380 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
1381 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
1382 }
1383}
1384
1385
06a2c219
GM
1386/* Fill rectangle X, Y, W, H with background color of glyph string S. */
1387
1388static INLINE void
1389x_clear_glyph_string_rect (s, x, y, w, h)
1390 struct glyph_string *s;
1391 int x, y, w, h;
1392{
1393 XGCValues xgcv;
1394 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1395 XSetForeground (s->display, s->gc, xgcv.background);
1396 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1397 XSetForeground (s->display, s->gc, xgcv.foreground);
1398}
1399
1400
1401/* Draw the background of glyph_string S. If S->background_filled_p
1402 is non-zero don't draw it. FORCE_P non-zero means draw the
1403 background even if it wouldn't be drawn normally. This is used
b4192550
KH
1404 when a string preceding S draws into the background of S, or S
1405 contains the first component of a composition. */
06a2c219
GM
1406
1407static void
1408x_draw_glyph_string_background (s, force_p)
1409 struct glyph_string *s;
1410 int force_p;
1411{
1412 /* Nothing to do if background has already been drawn or if it
1413 shouldn't be drawn in the first place. */
1414 if (!s->background_filled_p)
1415 {
ea2ba0d4
KH
1416 int box_line_width = max (s->face->box_line_width, 0);
1417
b4192550 1418 if (s->stippled_p)
06a2c219
GM
1419 {
1420 /* Fill background with a stipple pattern. */
1421 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1422 XFillRectangle (s->display, s->window, s->gc, s->x,
ea2ba0d4 1423 s->y + box_line_width,
06a2c219 1424 s->background_width,
ea2ba0d4 1425 s->height - 2 * box_line_width);
06a2c219
GM
1426 XSetFillStyle (s->display, s->gc, FillSolid);
1427 s->background_filled_p = 1;
1428 }
ea2ba0d4 1429 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
06a2c219
GM
1430 || s->font_not_found_p
1431 || s->extends_to_end_of_line_p
06a2c219
GM
1432 || force_p)
1433 {
ea2ba0d4 1434 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
06a2c219 1435 s->background_width,
ea2ba0d4 1436 s->height - 2 * box_line_width);
06a2c219
GM
1437 s->background_filled_p = 1;
1438 }
1439 }
1440}
1441
1442
1443/* Draw the foreground of glyph string S. */
1444
1445static void
1446x_draw_glyph_string_foreground (s)
1447 struct glyph_string *s;
1448{
1449 int i, x;
1450
1451 /* If first glyph of S has a left box line, start drawing the text
1452 of S to the right of that box line. */
1453 if (s->face->box != FACE_NO_BOX
1454 && s->first_glyph->left_box_line_p)
ea2ba0d4 1455 x = s->x + abs (s->face->box_line_width);
06a2c219
GM
1456 else
1457 x = s->x;
1458
b4192550
KH
1459 /* Draw characters of S as rectangles if S's font could not be
1460 loaded. */
1461 if (s->font_not_found_p)
06a2c219 1462 {
b4192550 1463 for (i = 0; i < s->nchars; ++i)
06a2c219 1464 {
b4192550
KH
1465 struct glyph *g = s->first_glyph + i;
1466 XDrawRectangle (s->display, s->window,
1467 s->gc, x, s->y, g->pixel_width - 1,
1468 s->height - 1);
1469 x += g->pixel_width;
06a2c219
GM
1470 }
1471 }
1472 else
1473 {
b4192550
KH
1474 char *char1b = (char *) s->char2b;
1475 int boff = s->font_info->baseline_offset;
06a2c219 1476
b4192550
KH
1477 if (s->font_info->vertical_centering)
1478 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
1479
1480 /* If we can use 8-bit functions, condense S->char2b. */
1481 if (!s->two_byte_p)
1482 for (i = 0; i < s->nchars; ++i)
1483 char1b[i] = s->char2b[i].byte2;
1484
1485 /* Draw text with XDrawString if background has already been
1486 filled. Otherwise, use XDrawImageString. (Note that
1487 XDrawImageString is usually faster than XDrawString.) Always
1488 use XDrawImageString when drawing the cursor so that there is
1489 no chance that characters under a box cursor are invisible. */
1490 if (s->for_overlaps_p
1491 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1492 {
1493 /* Draw characters with 16-bit or 8-bit functions. */
1494 if (s->two_byte_p)
1495 XDrawString16 (s->display, s->window, s->gc, x,
1496 s->ybase - boff, s->char2b, s->nchars);
1497 else
1498 XDrawString (s->display, s->window, s->gc, x,
1499 s->ybase - boff, char1b, s->nchars);
1500 }
06a2c219
GM
1501 else
1502 {
b4192550
KH
1503 if (s->two_byte_p)
1504 XDrawImageString16 (s->display, s->window, s->gc, x,
1505 s->ybase - boff, s->char2b, s->nchars);
06a2c219 1506 else
b4192550
KH
1507 XDrawImageString (s->display, s->window, s->gc, x,
1508 s->ybase - boff, char1b, s->nchars);
1509 }
fce13449
MB
1510
1511 if (s->face->overstrike)
1512 {
1513 /* For overstriking (to simulate bold-face), draw the
1514 characters again shifted to the right by one pixel. */
1515 if (s->two_byte_p)
1516 XDrawString16 (s->display, s->window, s->gc, x + 1,
1517 s->ybase - boff, s->char2b, s->nchars);
1518 else
1519 XDrawString (s->display, s->window, s->gc, x + 1,
1520 s->ybase - boff, char1b, s->nchars);
1521 }
b4192550
KH
1522 }
1523}
06a2c219 1524
b4192550 1525/* Draw the foreground of composite glyph string S. */
06a2c219 1526
b4192550
KH
1527static void
1528x_draw_composite_glyph_string_foreground (s)
1529 struct glyph_string *s;
1530{
1531 int i, x;
06a2c219 1532
b4192550
KH
1533 /* If first glyph of S has a left box line, start drawing the text
1534 of S to the right of that box line. */
1535 if (s->face->box != FACE_NO_BOX
1536 && s->first_glyph->left_box_line_p)
ea2ba0d4 1537 x = s->x + abs (s->face->box_line_width);
b4192550
KH
1538 else
1539 x = s->x;
06a2c219 1540
b4192550
KH
1541 /* S is a glyph string for a composition. S->gidx is the index of
1542 the first character drawn for glyphs of this composition.
1543 S->gidx == 0 means we are drawing the very first character of
1544 this composition. */
06a2c219 1545
b4192550
KH
1546 /* Draw a rectangle for the composition if the font for the very
1547 first character of the composition could not be loaded. */
1548 if (s->font_not_found_p)
1549 {
1550 if (s->gidx == 0)
1551 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1552 s->width - 1, s->height - 1);
1553 }
1554 else
1555 {
1556 for (i = 0; i < s->nchars; i++, ++s->gidx)
fce13449
MB
1557 {
1558 XDrawString16 (s->display, s->window, s->gc,
1559 x + s->cmp->offsets[s->gidx * 2],
1560 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
1561 s->char2b + i, 1);
1562 if (s->face->overstrike)
1563 XDrawString16 (s->display, s->window, s->gc,
1564 x + s->cmp->offsets[s->gidx * 2] + 1,
1565 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
1566 s->char2b + i, 1);
1567 }
06a2c219
GM
1568 }
1569}
1570
1571
80c32bcc
GM
1572#ifdef USE_X_TOOLKIT
1573
3e71d8f2 1574static struct frame *x_frame_of_widget P_ ((Widget));
651f03b6
GM
1575static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
1576 XrmValue *, XrmValue *, XtPointer *));
1577static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
1578 XrmValue *, Cardinal *));
80c32bcc 1579
3e71d8f2
GM
1580
1581/* Return the frame on which widget WIDGET is used.. Abort if frame
1582 cannot be determined. */
1583
e851c833 1584static struct frame *
3e71d8f2 1585x_frame_of_widget (widget)
80c32bcc 1586 Widget widget;
80c32bcc 1587{
80c32bcc 1588 struct x_display_info *dpyinfo;
5c187dee 1589 Lisp_Object tail;
3e71d8f2 1590 struct frame *f;
7d0393cf 1591
80c32bcc 1592 dpyinfo = x_display_info_for_display (XtDisplay (widget));
7d0393cf 1593
80c32bcc
GM
1594 /* Find the top-level shell of the widget. Note that this function
1595 can be called when the widget is not yet realized, so XtWindow
1596 (widget) == 0. That's the reason we can't simply use
1597 x_any_window_to_frame. */
1598 while (!XtIsTopLevelShell (widget))
1599 widget = XtParent (widget);
1600
1601 /* Look for a frame with that top-level widget. Allocate the color
1602 on that frame to get the right gamma correction value. */
1603 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
1604 if (GC_FRAMEP (XCAR (tail))
1605 && (f = XFRAME (XCAR (tail)),
1606 (f->output_data.nothing != 1
1607 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
1608 && f->output_data.x->widget == widget)
3e71d8f2 1609 return f;
80c32bcc
GM
1610
1611 abort ();
1612}
1613
3e71d8f2
GM
1614
1615/* Allocate the color COLOR->pixel on the screen and display of
1616 widget WIDGET in colormap CMAP. If an exact match cannot be
1617 allocated, try the nearest color available. Value is non-zero
1618 if successful. This is called from lwlib. */
1619
1620int
1621x_alloc_nearest_color_for_widget (widget, cmap, color)
1622 Widget widget;
1623 Colormap cmap;
1624 XColor *color;
1625{
1626 struct frame *f = x_frame_of_widget (widget);
1627 return x_alloc_nearest_color (f, cmap, color);
1628}
1629
1630
46d516e5
MB
1631/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1632 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1633 If this produces the same color as PIXEL, try a color where all RGB
1634 values have DELTA added. Return the allocated color in *PIXEL.
1635 DISPLAY is the X display, CMAP is the colormap to operate on.
1636 Value is non-zero if successful. */
1637
1638int
1639x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
1640 Widget widget;
1641 Display *display;
1642 Colormap cmap;
1643 unsigned long *pixel;
1644 double factor;
1645 int delta;
1646{
1647 struct frame *f = x_frame_of_widget (widget);
1648 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1649}
1650
1651
651f03b6
GM
1652/* Structure specifying which arguments should be passed by Xt to
1653 cvt_string_to_pixel. We want the widget's screen and colormap. */
1654
1655static XtConvertArgRec cvt_string_to_pixel_args[] =
1656 {
1657 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1658 sizeof (Screen *)},
1659 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1660 sizeof (Colormap)}
1661 };
1662
1663
1664/* The address of this variable is returned by
1665 cvt_string_to_pixel. */
1666
1667static Pixel cvt_string_to_pixel_value;
1668
1669
1670/* Convert a color name to a pixel color.
1671
1672 DPY is the display we are working on.
1673
1674 ARGS is an array of *NARGS XrmValue structures holding additional
1675 information about the widget for which the conversion takes place.
1676 The contents of this array are determined by the specification
1677 in cvt_string_to_pixel_args.
1678
1679 FROM is a pointer to an XrmValue which points to the color name to
1680 convert. TO is an XrmValue in which to return the pixel color.
1681
1682 CLOSURE_RET is a pointer to user-data, in which we record if
1683 we allocated the color or not.
1684
1685 Value is True if successful, False otherwise. */
1686
1687static Boolean
1688cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
1689 Display *dpy;
1690 XrmValue *args;
1691 Cardinal *nargs;
1692 XrmValue *from, *to;
1693 XtPointer *closure_ret;
1694{
1695 Screen *screen;
1696 Colormap cmap;
1697 Pixel pixel;
1698 String color_name;
1699 XColor color;
1700
1701 if (*nargs != 2)
1702 {
1703 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1704 "wrongParameters", "cvt_string_to_pixel",
1705 "XtToolkitError",
1706 "Screen and colormap args required", NULL, NULL);
1707 return False;
1708 }
1709
1710 screen = *(Screen **) args[0].addr;
1711 cmap = *(Colormap *) args[1].addr;
1712 color_name = (String) from->addr;
1713
1714 if (strcmp (color_name, XtDefaultBackground) == 0)
1715 {
1716 *closure_ret = (XtPointer) False;
1717 pixel = WhitePixelOfScreen (screen);
1718 }
1719 else if (strcmp (color_name, XtDefaultForeground) == 0)
1720 {
1721 *closure_ret = (XtPointer) False;
1722 pixel = BlackPixelOfScreen (screen);
1723 }
1724 else if (XParseColor (dpy, cmap, color_name, &color)
1725 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1726 {
1727 pixel = color.pixel;
1728 *closure_ret = (XtPointer) True;
1729 }
1730 else
1731 {
1732 String params[1];
1733 Cardinal nparams = 1;
7d0393cf 1734
651f03b6
GM
1735 params[0] = color_name;
1736 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1737 "badValue", "cvt_string_to_pixel",
1738 "XtToolkitError", "Invalid color `%s'",
1739 params, &nparams);
1740 return False;
1741 }
1742
1743 if (to->addr != NULL)
1744 {
1745 if (to->size < sizeof (Pixel))
1746 {
1747 to->size = sizeof (Pixel);
1748 return False;
1749 }
7d0393cf 1750
651f03b6
GM
1751 *(Pixel *) to->addr = pixel;
1752 }
1753 else
1754 {
1755 cvt_string_to_pixel_value = pixel;
1756 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1757 }
7d0393cf 1758
651f03b6
GM
1759 to->size = sizeof (Pixel);
1760 return True;
1761}
1762
1763
1764/* Free a pixel color which was previously allocated via
1765 cvt_string_to_pixel. This is registered as the destructor
1766 for this type of resource via XtSetTypeConverter.
1767
1768 APP is the application context in which we work.
1769
1770 TO is a pointer to an XrmValue holding the color to free.
1771 CLOSURE is the value we stored in CLOSURE_RET for this color
1772 in cvt_string_to_pixel.
1773
1774 ARGS and NARGS are like for cvt_string_to_pixel. */
1775
1776static void
1777cvt_pixel_dtor (app, to, closure, args, nargs)
1778 XtAppContext app;
1779 XrmValuePtr to;
1780 XtPointer closure;
1781 XrmValuePtr args;
1782 Cardinal *nargs;
1783{
1784 if (*nargs != 2)
1785 {
1786 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1787 "XtToolkitError",
1788 "Screen and colormap arguments required",
1789 NULL, NULL);
1790 }
1791 else if (closure != NULL)
1792 {
1793 /* We did allocate the pixel, so free it. */
1794 Screen *screen = *(Screen **) args[0].addr;
1795 Colormap cmap = *(Colormap *) args[1].addr;
1796 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
97762eb7 1797 (Pixel *) to->addr, 1);
651f03b6
GM
1798 }
1799}
1800
1801
80c32bcc
GM
1802#endif /* USE_X_TOOLKIT */
1803
1804
f04e1297 1805/* Value is an array of XColor structures for the contents of the
651f03b6 1806 color map of display DPY. Set *NCELLS to the size of the array.
f04e1297
GM
1807 Note that this probably shouldn't be called for large color maps,
1808 say a 24-bit TrueColor map. */
1809
1810static const XColor *
651f03b6
GM
1811x_color_cells (dpy, ncells)
1812 Display *dpy;
f04e1297
GM
1813 int *ncells;
1814{
651f03b6 1815 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
f04e1297
GM
1816
1817 if (dpyinfo->color_cells == NULL)
1818 {
651f03b6 1819 Screen *screen = dpyinfo->screen;
f04e1297 1820 int i;
7d0393cf 1821
f04e1297 1822 dpyinfo->ncolor_cells
651f03b6 1823 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
f04e1297
GM
1824 dpyinfo->color_cells
1825 = (XColor *) xmalloc (dpyinfo->ncolor_cells
1826 * sizeof *dpyinfo->color_cells);
7d0393cf 1827
f04e1297
GM
1828 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
1829 dpyinfo->color_cells[i].pixel = i;
7d0393cf 1830
651f03b6 1831 XQueryColors (dpy, dpyinfo->cmap,
f04e1297
GM
1832 dpyinfo->color_cells, dpyinfo->ncolor_cells);
1833 }
1834
1835 *ncells = dpyinfo->ncolor_cells;
1836 return dpyinfo->color_cells;
1837}
1838
1839
1840/* On frame F, translate pixel colors to RGB values for the NCOLORS
1841 colors in COLORS. Use cached information, if available. */
1842
1843void
1844x_query_colors (f, colors, ncolors)
1845 struct frame *f;
1846 XColor *colors;
1847 int ncolors;
1848{
1849 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1850
1851 if (dpyinfo->color_cells)
1852 {
1853 int i;
1854 for (i = 0; i < ncolors; ++i)
1855 {
1856 unsigned long pixel = colors[i].pixel;
1857 xassert (pixel < dpyinfo->ncolor_cells);
1858 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
1859 colors[i] = dpyinfo->color_cells[pixel];
1860 }
1861 }
1862 else
1863 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1864}
1865
1866
1867/* On frame F, translate pixel color to RGB values for the color in
1868 COLOR. Use cached information, if available. */
1869
1870void
1871x_query_color (f, color)
1872 struct frame *f;
1873 XColor *color;
1874{
1875 x_query_colors (f, color, 1);
1876}
7d0393cf 1877
f04e1297 1878
651f03b6
GM
1879/* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1880 exact match can't be allocated, try the nearest color available.
1881 Value is non-zero if successful. Set *COLOR to the color
1882 allocated. */
06a2c219 1883
651f03b6
GM
1884static int
1885x_alloc_nearest_color_1 (dpy, cmap, color)
1886 Display *dpy;
06a2c219
GM
1887 Colormap cmap;
1888 XColor *color;
1889{
80c32bcc
GM
1890 int rc;
1891
651f03b6 1892 rc = XAllocColor (dpy, cmap, color);
06a2c219
GM
1893 if (rc == 0)
1894 {
1895 /* If we got to this point, the colormap is full, so we're going
1896 to try to get the next closest color. The algorithm used is
1897 a least-squares matching, which is what X uses for closest
1898 color matching with StaticColor visuals. */
1899 int nearest, i;
1900 unsigned long nearest_delta = ~0;
f04e1297 1901 int ncells;
651f03b6 1902 const XColor *cells = x_color_cells (dpy, &ncells);
06a2c219
GM
1903
1904 for (nearest = i = 0; i < ncells; ++i)
1905 {
1906 long dred = (color->red >> 8) - (cells[i].red >> 8);
1907 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
1908 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1909 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
1910
1911 if (delta < nearest_delta)
1912 {
1913 nearest = i;
1914 nearest_delta = delta;
1915 }
1916 }
7d0393cf 1917
06a2c219
GM
1918 color->red = cells[nearest].red;
1919 color->green = cells[nearest].green;
1920 color->blue = cells[nearest].blue;
651f03b6 1921 rc = XAllocColor (dpy, cmap, color);
06a2c219 1922 }
35efe0a1
GM
1923 else
1924 {
1925 /* If allocation succeeded, and the allocated pixel color is not
1926 equal to a cached pixel color recorded earlier, there was a
1927 change in the colormap, so clear the color cache. */
651f03b6 1928 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
35efe0a1 1929 XColor *cached_color;
7d0393cf 1930
35efe0a1
GM
1931 if (dpyinfo->color_cells
1932 && (cached_color = &dpyinfo->color_cells[color->pixel],
cae71efe
GM
1933 (cached_color->red != color->red
1934 || cached_color->blue != color->blue
1935 || cached_color->green != color->green)))
35efe0a1
GM
1936 {
1937 xfree (dpyinfo->color_cells);
1938 dpyinfo->color_cells = NULL;
1939 dpyinfo->ncolor_cells = 0;
1940 }
1941 }
06a2c219 1942
d9c545da
GM
1943#ifdef DEBUG_X_COLORS
1944 if (rc)
1945 register_color (color->pixel);
1946#endif /* DEBUG_X_COLORS */
7d0393cf 1947
06a2c219
GM
1948 return rc;
1949}
1950
1951
651f03b6
GM
1952/* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1953 exact match can't be allocated, try the nearest color available.
1954 Value is non-zero if successful. Set *COLOR to the color
1955 allocated. */
1956
1957int
1958x_alloc_nearest_color (f, cmap, color)
1959 struct frame *f;
1960 Colormap cmap;
1961 XColor *color;
1962{
1963 gamma_correct (f, color);
1964 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1965}
1966
1967
d9c545da
GM
1968/* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1969 It's necessary to do this instead of just using PIXEL directly to
1970 get color reference counts right. */
1971
1972unsigned long
1973x_copy_color (f, pixel)
1974 struct frame *f;
1975 unsigned long pixel;
1976{
1977 XColor color;
1978
1979 color.pixel = pixel;
1980 BLOCK_INPUT;
f04e1297 1981 x_query_color (f, &color);
d9c545da
GM
1982 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1983 UNBLOCK_INPUT;
1984#ifdef DEBUG_X_COLORS
1985 register_color (pixel);
1986#endif
1987 return color.pixel;
1988}
1989
1990
3e71d8f2
GM
1991/* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
1992 It's necessary to do this instead of just using PIXEL directly to
1993 get color reference counts right. */
1994
1995unsigned long
1996x_copy_dpy_color (dpy, cmap, pixel)
1997 Display *dpy;
1998 Colormap cmap;
1999 unsigned long pixel;
2000{
2001 XColor color;
2002
2003 color.pixel = pixel;
2004 BLOCK_INPUT;
2005 XQueryColor (dpy, cmap, &color);
2006 XAllocColor (dpy, cmap, &color);
2007 UNBLOCK_INPUT;
2008#ifdef DEBUG_X_COLORS
2009 register_color (pixel);
2010#endif
2011 return color.pixel;
2012}
2013
2014
6d8b0acd 2015/* Brightness beyond which a color won't have its highlight brightness
d7361edf 2016 boosted.
6d8b0acd 2017
d7361edf
MB
2018 Nominally, highlight colors for `3d' faces are calculated by
2019 brightening an object's color by a constant scale factor, but this
2020 doesn't yield good results for dark colors, so for colors who's
2021 brightness is less than this value (on a scale of 0-65535) have an
2022 use an additional additive factor.
6d8b0acd
MB
2023
2024 The value here is set so that the default menu-bar/mode-line color
2025 (grey75) will not have its highlights changed at all. */
2026#define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
2027
2028
06a2c219
GM
2029/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
2030 or DELTA. Try a color with RGB values multiplied by FACTOR first.
2031 If this produces the same color as PIXEL, try a color where all RGB
2032 values have DELTA added. Return the allocated color in *PIXEL.
2033 DISPLAY is the X display, CMAP is the colormap to operate on.
2034 Value is non-zero if successful. */
2035
2036static int
2037x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
2038 struct frame *f;
2039 Display *display;
2040 Colormap cmap;
2041 unsigned long *pixel;
68c45bf0 2042 double factor;
06a2c219
GM
2043 int delta;
2044{
2045 XColor color, new;
6d8b0acd 2046 long bright;
06a2c219
GM
2047 int success_p;
2048
2049 /* Get RGB color values. */
2050 color.pixel = *pixel;
f04e1297 2051 x_query_color (f, &color);
06a2c219
GM
2052
2053 /* Change RGB values by specified FACTOR. Avoid overflow! */
2054 xassert (factor >= 0);
2055 new.red = min (0xffff, factor * color.red);
2056 new.green = min (0xffff, factor * color.green);
2057 new.blue = min (0xffff, factor * color.blue);
2058
d7361edf
MB
2059 /* Calculate brightness of COLOR. */
2060 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
6d8b0acd
MB
2061
2062 /* We only boost colors that are darker than
2063 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
2064 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
2065 /* Make an additive adjustment to NEW, because it's dark enough so
2066 that scaling by FACTOR alone isn't enough. */
2067 {
2068 /* How far below the limit this color is (0 - 1, 1 being darker). */
2069 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
2070 /* The additive adjustment. */
d7361edf 2071 int min_delta = delta * dimness * factor / 2;
6d8b0acd
MB
2072
2073 if (factor < 1)
2074 {
6d8b0acd
MB
2075 new.red = max (0, new.red - min_delta);
2076 new.green = max (0, new.green - min_delta);
2077 new.blue = max (0, new.blue - min_delta);
2078 }
2079 else
2080 {
2081 new.red = min (0xffff, min_delta + new.red);
2082 new.green = min (0xffff, min_delta + new.green);
2083 new.blue = min (0xffff, min_delta + new.blue);
2084 }
2085 }
2086
06a2c219 2087 /* Try to allocate the color. */
80c32bcc 2088 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
2089 if (success_p)
2090 {
2091 if (new.pixel == *pixel)
2092 {
2093 /* If we end up with the same color as before, try adding
2094 delta to the RGB values. */
0d605c67 2095 x_free_colors (f, &new.pixel, 1);
7d0393cf 2096
06a2c219
GM
2097 new.red = min (0xffff, delta + color.red);
2098 new.green = min (0xffff, delta + color.green);
2099 new.blue = min (0xffff, delta + color.blue);
80c32bcc 2100 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
2101 }
2102 else
2103 success_p = 1;
2104 *pixel = new.pixel;
2105 }
7d0393cf 2106
06a2c219
GM
2107 return success_p;
2108}
2109
2110
2111/* Set up the foreground color for drawing relief lines of glyph
2112 string S. RELIEF is a pointer to a struct relief containing the GC
2113 with which lines will be drawn. Use a color that is FACTOR or
2114 DELTA lighter or darker than the relief's background which is found
2115 in S->f->output_data.x->relief_background. If such a color cannot
2116 be allocated, use DEFAULT_PIXEL, instead. */
7d0393cf 2117
06a2c219
GM
2118static void
2119x_setup_relief_color (f, relief, factor, delta, default_pixel)
2120 struct frame *f;
2121 struct relief *relief;
68c45bf0 2122 double factor;
06a2c219
GM
2123 int delta;
2124 unsigned long default_pixel;
2125{
2126 XGCValues xgcv;
2127 struct x_output *di = f->output_data.x;
2128 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
2129 unsigned long pixel;
2130 unsigned long background = di->relief_background;
43bd1b2b 2131 Colormap cmap = FRAME_X_COLORMAP (f);
dcd08bfb
GM
2132 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2133 Display *dpy = FRAME_X_DISPLAY (f);
06a2c219
GM
2134
2135 xgcv.graphics_exposures = False;
2136 xgcv.line_width = 1;
2137
2138 /* Free previously allocated color. The color cell will be reused
2139 when it has been freed as many times as it was allocated, so this
2140 doesn't affect faces using the same colors. */
2141 if (relief->gc
2142 && relief->allocated_p)
2143 {
0d605c67 2144 x_free_colors (f, &relief->pixel, 1);
06a2c219
GM
2145 relief->allocated_p = 0;
2146 }
2147
2148 /* Allocate new color. */
2149 xgcv.foreground = default_pixel;
2150 pixel = background;
dcd08bfb
GM
2151 if (dpyinfo->n_planes != 1
2152 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
06a2c219
GM
2153 {
2154 relief->allocated_p = 1;
2155 xgcv.foreground = relief->pixel = pixel;
2156 }
7d0393cf 2157
06a2c219
GM
2158 if (relief->gc == 0)
2159 {
dcd08bfb 2160 xgcv.stipple = dpyinfo->gray;
06a2c219 2161 mask |= GCStipple;
dcd08bfb 2162 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
06a2c219
GM
2163 }
2164 else
dcd08bfb 2165 XChangeGC (dpy, relief->gc, mask, &xgcv);
06a2c219
GM
2166}
2167
2168
2169/* Set up colors for the relief lines around glyph string S. */
2170
2171static void
2172x_setup_relief_colors (s)
2173 struct glyph_string *s;
2174{
2175 struct x_output *di = s->f->output_data.x;
2176 unsigned long color;
2177
2178 if (s->face->use_box_color_for_shadows_p)
2179 color = s->face->box_color;
e2a57b34 2180 else if (s->first_glyph->type == IMAGE_GLYPH
0cb8bb48 2181 && s->img->pixmap
e2a57b34
MB
2182 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2183 color = IMAGE_BACKGROUND (s->img, s->f, 0);
06a2c219
GM
2184 else
2185 {
2186 XGCValues xgcv;
7d0393cf 2187
06a2c219
GM
2188 /* Get the background color of the face. */
2189 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2190 color = xgcv.background;
2191 }
2192
2193 if (di->white_relief.gc == 0
2194 || color != di->relief_background)
2195 {
2196 di->relief_background = color;
2197 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2198 WHITE_PIX_DEFAULT (s->f));
2199 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2200 BLACK_PIX_DEFAULT (s->f));
2201 }
2202}
2203
2204
2205/* Draw a relief on frame F inside the rectangle given by LEFT_X,
2206 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2207 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
2208 relief. LEFT_P non-zero means draw a relief on the left side of
2209 the rectangle. RIGHT_P non-zero means draw a relief on the right
2210 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2211 when drawing. */
2212
2213static void
2214x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
2215 raised_p, left_p, right_p, clip_rect)
2216 struct frame *f;
0971e730 2217 int left_x, top_y, right_x, bottom_y, width, left_p, right_p, raised_p;
06a2c219
GM
2218 XRectangle *clip_rect;
2219{
de507556
GM
2220 Display *dpy = FRAME_X_DISPLAY (f);
2221 Window window = FRAME_X_WINDOW (f);
06a2c219
GM
2222 int i;
2223 GC gc;
7d0393cf 2224
06a2c219
GM
2225 if (raised_p)
2226 gc = f->output_data.x->white_relief.gc;
2227 else
2228 gc = f->output_data.x->black_relief.gc;
de507556 2229 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
06a2c219
GM
2230
2231 /* Top. */
2232 for (i = 0; i < width; ++i)
de507556 2233 XDrawLine (dpy, window, gc,
06a2c219
GM
2234 left_x + i * left_p, top_y + i,
2235 right_x + 1 - i * right_p, top_y + i);
2236
2237 /* Left. */
2238 if (left_p)
2239 for (i = 0; i < width; ++i)
de507556 2240 XDrawLine (dpy, window, gc,
44655e77 2241 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
06a2c219 2242
de507556 2243 XSetClipMask (dpy, gc, None);
06a2c219
GM
2244 if (raised_p)
2245 gc = f->output_data.x->black_relief.gc;
2246 else
2247 gc = f->output_data.x->white_relief.gc;
de507556 2248 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
7d0393cf 2249
06a2c219
GM
2250 /* Bottom. */
2251 for (i = 0; i < width; ++i)
de507556
GM
2252 XDrawLine (dpy, window, gc,
2253 left_x + i * left_p, bottom_y - i,
327f42ee 2254 right_x + 1 - i * right_p, bottom_y - i);
7d0393cf 2255
06a2c219
GM
2256 /* Right. */
2257 if (right_p)
2258 for (i = 0; i < width; ++i)
de507556 2259 XDrawLine (dpy, window, gc,
06a2c219
GM
2260 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
2261
de507556 2262 XSetClipMask (dpy, gc, None);
06a2c219
GM
2263}
2264
2265
2266/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2267 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2268 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
2269 left side of the rectangle. RIGHT_P non-zero means draw a line
2270 on the right side of the rectangle. CLIP_RECT is the clipping
2271 rectangle to use when drawing. */
2272
2273static void
2274x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2275 left_p, right_p, clip_rect)
2276 struct glyph_string *s;
0971e730 2277 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
06a2c219
GM
2278 XRectangle *clip_rect;
2279{
2280 XGCValues xgcv;
7d0393cf 2281
06a2c219
GM
2282 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2283 XSetForeground (s->display, s->gc, s->face->box_color);
2284 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
7d0393cf 2285
06a2c219
GM
2286 /* Top. */
2287 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 2288 left_x, top_y, right_x - left_x + 1, width);
06a2c219
GM
2289
2290 /* Left. */
2291 if (left_p)
2292 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 2293 left_x, top_y, width, bottom_y - top_y + 1);
06a2c219
GM
2294
2295 /* Bottom. */
2296 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 2297 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
7d0393cf 2298
06a2c219
GM
2299 /* Right. */
2300 if (right_p)
2301 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 2302 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
06a2c219
GM
2303
2304 XSetForeground (s->display, s->gc, xgcv.foreground);
2305 XSetClipMask (s->display, s->gc, None);
2306}
2307
2308
2309/* Draw a box around glyph string S. */
2310
2311static void
2312x_draw_glyph_string_box (s)
2313 struct glyph_string *s;
2314{
2315 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2316 int left_p, right_p;
2317 struct glyph *last_glyph;
2318 XRectangle clip_rect;
2319
2320 last_x = window_box_right (s->w, s->area);
2321 if (s->row->full_width_p
2322 && !s->w->pseudo_window_p)
2323 {
3f332ef3 2324 last_x += FRAME_X_RIGHT_FRINGE_WIDTH (s->f);
06a2c219
GM
2325 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
2326 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
2327 }
7d0393cf 2328
06a2c219 2329 /* The glyph that may have a right box line. */
b4192550 2330 last_glyph = (s->cmp || s->img
06a2c219
GM
2331 ? s->first_glyph
2332 : s->first_glyph + s->nchars - 1);
2333
ea2ba0d4 2334 width = abs (s->face->box_line_width);
06a2c219
GM
2335 raised_p = s->face->box == FACE_RAISED_BOX;
2336 left_x = s->x;
57ac7c81
GM
2337 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2338 ? last_x - 1
2339 : min (last_x, s->x + s->background_width) - 1);
06a2c219
GM
2340 top_y = s->y;
2341 bottom_y = top_y + s->height - 1;
2342
2343 left_p = (s->first_glyph->left_box_line_p
2344 || (s->hl == DRAW_MOUSE_FACE
2345 && (s->prev == NULL
2346 || s->prev->hl != s->hl)));
2347 right_p = (last_glyph->right_box_line_p
2348 || (s->hl == DRAW_MOUSE_FACE
2349 && (s->next == NULL
2350 || s->next->hl != s->hl)));
327f42ee 2351
06a2c219
GM
2352 x_get_glyph_string_clip_rect (s, &clip_rect);
2353
2354 if (s->face->box == FACE_SIMPLE_BOX)
2355 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2356 left_p, right_p, &clip_rect);
2357 else
2358 {
2359 x_setup_relief_colors (s);
2360 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2361 width, raised_p, left_p, right_p, &clip_rect);
2362 }
2363}
2364
2365
2366/* Draw foreground of image glyph string S. */
2367
2368static void
2369x_draw_image_foreground (s)
2370 struct glyph_string *s;
2371{
2372 int x;
95af8492 2373 int y = s->ybase - image_ascent (s->img, s->face);
06a2c219
GM
2374
2375 /* If first glyph of S has a left box line, start drawing it to the
2376 right of that line. */
2377 if (s->face->box != FACE_NO_BOX
2378 && s->first_glyph->left_box_line_p)
ea2ba0d4 2379 x = s->x + abs (s->face->box_line_width);
06a2c219
GM
2380 else
2381 x = s->x;
2382
2383 /* If there is a margin around the image, adjust x- and y-position
2384 by that margin. */
22d650b8
GM
2385 x += s->img->hmargin;
2386 y += s->img->vmargin;
06a2c219
GM
2387
2388 if (s->img->pixmap)
2389 {
2390 if (s->img->mask)
2391 {
2392 /* We can't set both a clip mask and use XSetClipRectangles
2393 because the latter also sets a clip mask. We also can't
2394 trust on the shape extension to be available
2395 (XShapeCombineRegion). So, compute the rectangle to draw
2396 manually. */
2397 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2398 | GCFunction);
2399 XGCValues xgcv;
2400 XRectangle clip_rect, image_rect, r;
2401
2402 xgcv.clip_mask = s->img->mask;
2403 xgcv.clip_x_origin = x;
2404 xgcv.clip_y_origin = y;
2405 xgcv.function = GXcopy;
2406 XChangeGC (s->display, s->gc, mask, &xgcv);
7d0393cf 2407
06a2c219
GM
2408 x_get_glyph_string_clip_rect (s, &clip_rect);
2409 image_rect.x = x;
2410 image_rect.y = y;
2411 image_rect.width = s->img->width;
2412 image_rect.height = s->img->height;
2413 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2414 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2415 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
2416 }
2417 else
2418 {
49ad1d99
GM
2419 XRectangle clip_rect, image_rect, r;
2420
2421 x_get_glyph_string_clip_rect (s, &clip_rect);
2422 image_rect.x = x;
2423 image_rect.y = y;
2424 image_rect.width = s->img->width;
2425 image_rect.height = s->img->height;
2426 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2427 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2428 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
7d0393cf 2429
06a2c219
GM
2430 /* When the image has a mask, we can expect that at
2431 least part of a mouse highlight or a block cursor will
2432 be visible. If the image doesn't have a mask, make
2433 a block cursor visible by drawing a rectangle around
2434 the image. I believe it's looking better if we do
2435 nothing here for mouse-face. */
2436 if (s->hl == DRAW_CURSOR)
534c20b2
KS
2437 {
2438 int r = s->img->relief;
2439 if (r < 0) r = -r;
2440 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2441 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
2442 }
06a2c219
GM
2443 }
2444 }
2445 else
2446 /* Draw a rectangle if image could not be loaded. */
2447 XDrawRectangle (s->display, s->window, s->gc, x, y,
2448 s->img->width - 1, s->img->height - 1);
2449}
2450
2451
2452/* Draw a relief around the image glyph string S. */
2453
2454static void
2455x_draw_image_relief (s)
2456 struct glyph_string *s;
2457{
2458 int x0, y0, x1, y1, thick, raised_p;
2459 XRectangle r;
2460 int x;
95af8492 2461 int y = s->ybase - image_ascent (s->img, s->face);
7d0393cf 2462
06a2c219
GM
2463 /* If first glyph of S has a left box line, start drawing it to the
2464 right of that line. */
2465 if (s->face->box != FACE_NO_BOX
2466 && s->first_glyph->left_box_line_p)
ea2ba0d4 2467 x = s->x + abs (s->face->box_line_width);
06a2c219
GM
2468 else
2469 x = s->x;
7d0393cf 2470
06a2c219
GM
2471 /* If there is a margin around the image, adjust x- and y-position
2472 by that margin. */
22d650b8
GM
2473 x += s->img->hmargin;
2474 y += s->img->vmargin;
7d0393cf 2475
06a2c219
GM
2476 if (s->hl == DRAW_IMAGE_SUNKEN
2477 || s->hl == DRAW_IMAGE_RAISED)
2478 {
62854fe2 2479 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
06a2c219
GM
2480 raised_p = s->hl == DRAW_IMAGE_RAISED;
2481 }
2482 else
2483 {
2484 thick = abs (s->img->relief);
2485 raised_p = s->img->relief > 0;
2486 }
7d0393cf 2487
06a2c219
GM
2488 x0 = x - thick;
2489 y0 = y - thick;
2490 x1 = x + s->img->width + thick - 1;
2491 y1 = y + s->img->height + thick - 1;
7d0393cf 2492
06a2c219
GM
2493 x_setup_relief_colors (s);
2494 x_get_glyph_string_clip_rect (s, &r);
2495 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
2496}
2497
2498
2499/* Draw the foreground of image glyph string S to PIXMAP. */
2500
2501static void
2502x_draw_image_foreground_1 (s, pixmap)
2503 struct glyph_string *s;
2504 Pixmap pixmap;
2505{
2506 int x;
95af8492 2507 int y = s->ybase - s->y - image_ascent (s->img, s->face);
06a2c219
GM
2508
2509 /* If first glyph of S has a left box line, start drawing it to the
2510 right of that line. */
2511 if (s->face->box != FACE_NO_BOX
2512 && s->first_glyph->left_box_line_p)
ea2ba0d4 2513 x = abs (s->face->box_line_width);
06a2c219
GM
2514 else
2515 x = 0;
2516
2517 /* If there is a margin around the image, adjust x- and y-position
2518 by that margin. */
22d650b8
GM
2519 x += s->img->hmargin;
2520 y += s->img->vmargin;
dc43ef94 2521
06a2c219
GM
2522 if (s->img->pixmap)
2523 {
2524 if (s->img->mask)
2525 {
2526 /* We can't set both a clip mask and use XSetClipRectangles
2527 because the latter also sets a clip mask. We also can't
2528 trust on the shape extension to be available
2529 (XShapeCombineRegion). So, compute the rectangle to draw
2530 manually. */
2531 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2532 | GCFunction);
2533 XGCValues xgcv;
2534
2535 xgcv.clip_mask = s->img->mask;
2536 xgcv.clip_x_origin = x;
2537 xgcv.clip_y_origin = y;
2538 xgcv.function = GXcopy;
2539 XChangeGC (s->display, s->gc, mask, &xgcv);
2540
2541 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2542 0, 0, s->img->width, s->img->height, x, y);
2543 XSetClipMask (s->display, s->gc, None);
2544 }
2545 else
2546 {
2547 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2548 0, 0, s->img->width, s->img->height, x, y);
7d0393cf 2549
06a2c219
GM
2550 /* When the image has a mask, we can expect that at
2551 least part of a mouse highlight or a block cursor will
2552 be visible. If the image doesn't have a mask, make
2553 a block cursor visible by drawing a rectangle around
2554 the image. I believe it's looking better if we do
2555 nothing here for mouse-face. */
2556 if (s->hl == DRAW_CURSOR)
534c20b2
KS
2557 {
2558 int r = s->img->relief;
2559 if (r < 0) r = -r;
2560 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2561 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
2562 }
06a2c219
GM
2563 }
2564 }
2565 else
2566 /* Draw a rectangle if image could not be loaded. */
2567 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2568 s->img->width - 1, s->img->height - 1);
2569}
dc43ef94 2570
990ba854 2571
06a2c219
GM
2572/* Draw part of the background of glyph string S. X, Y, W, and H
2573 give the rectangle to draw. */
a9a5b0a5 2574
06a2c219
GM
2575static void
2576x_draw_glyph_string_bg_rect (s, x, y, w, h)
2577 struct glyph_string *s;
2578 int x, y, w, h;
2579{
2580 if (s->stippled_p)
2581 {
2582 /* Fill background with a stipple pattern. */
2583 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2584 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2585 XSetFillStyle (s->display, s->gc, FillSolid);
2586 }
2587 else
2588 x_clear_glyph_string_rect (s, x, y, w, h);
2589}
07e34cb0 2590
b5210ea7 2591
7d0393cf 2592/* Draw image glyph string S.
dc43ef94 2593
06a2c219
GM
2594 s->y
2595 s->x +-------------------------
2596 | s->face->box
2597 |
2598 | +-------------------------
2599 | | s->img->margin
2600 | |
2601 | | +-------------------
2602 | | | the image
dc43ef94 2603
06a2c219 2604 */
dc43ef94 2605
06a2c219
GM
2606static void
2607x_draw_image_glyph_string (s)
2608 struct glyph_string *s;
2609{
2610 int x, y;
ea2ba0d4
KH
2611 int box_line_hwidth = abs (s->face->box_line_width);
2612 int box_line_vwidth = max (s->face->box_line_width, 0);
06a2c219
GM
2613 int height;
2614 Pixmap pixmap = None;
2615
ea2ba0d4 2616 height = s->height - 2 * box_line_vwidth;
06a2c219 2617
488dd4c4 2618
06a2c219
GM
2619 /* Fill background with face under the image. Do it only if row is
2620 taller than image or if image has a clip mask to reduce
2621 flickering. */
2622 s->stippled_p = s->face->stipple != 0;
2623 if (height > s->img->height
22d650b8
GM
2624 || s->img->hmargin
2625 || s->img->vmargin
06a2c219
GM
2626 || s->img->mask
2627 || s->img->pixmap == 0
2628 || s->width != s->background_width)
2629 {
ea2ba0d4
KH
2630 if (box_line_hwidth && s->first_glyph->left_box_line_p)
2631 x = s->x + box_line_hwidth;
06a2c219
GM
2632 else
2633 x = s->x;
7d0393cf 2634
ea2ba0d4 2635 y = s->y + box_line_vwidth;
7d0393cf 2636
06a2c219
GM
2637 if (s->img->mask)
2638 {
f9b5db02
GM
2639 /* Create a pixmap as large as the glyph string. Fill it
2640 with the background color. Copy the image to it, using
2641 its mask. Copy the temporary pixmap to the display. */
06a2c219
GM
2642 Screen *screen = FRAME_X_SCREEN (s->f);
2643 int depth = DefaultDepthOfScreen (screen);
2644
2645 /* Create a pixmap as large as the glyph string. */
2646 pixmap = XCreatePixmap (s->display, s->window,
2647 s->background_width,
2648 s->height, depth);
7d0393cf 2649
06a2c219
GM
2650 /* Don't clip in the following because we're working on the
2651 pixmap. */
2652 XSetClipMask (s->display, s->gc, None);
2653
2654 /* Fill the pixmap with the background color/stipple. */
2655 if (s->stippled_p)
2656 {
2657 /* Fill background with a stipple pattern. */
2658 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2659 XFillRectangle (s->display, pixmap, s->gc,
2660 0, 0, s->background_width, s->height);
2661 XSetFillStyle (s->display, s->gc, FillSolid);
2662 }
2663 else
2664 {
2665 XGCValues xgcv;
2666 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2667 &xgcv);
2668 XSetForeground (s->display, s->gc, xgcv.background);
2669 XFillRectangle (s->display, pixmap, s->gc,
2670 0, 0, s->background_width, s->height);
2671 XSetForeground (s->display, s->gc, xgcv.foreground);
2672 }
2673 }
2674 else
06a2c219 2675 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
7d0393cf 2676
06a2c219
GM
2677 s->background_filled_p = 1;
2678 }
dc43ef94 2679
06a2c219
GM
2680 /* Draw the foreground. */
2681 if (pixmap != None)
2682 {
2683 x_draw_image_foreground_1 (s, pixmap);
2684 x_set_glyph_string_clipping (s);
2685 XCopyArea (s->display, pixmap, s->window, s->gc,
2686 0, 0, s->background_width, s->height, s->x, s->y);
2687 XFreePixmap (s->display, pixmap);
2688 }
2689 else
2690 x_draw_image_foreground (s);
b5210ea7 2691
06a2c219
GM
2692 /* If we must draw a relief around the image, do it. */
2693 if (s->img->relief
2694 || s->hl == DRAW_IMAGE_RAISED
2695 || s->hl == DRAW_IMAGE_SUNKEN)
2696 x_draw_image_relief (s);
2697}
8c1a6a84 2698
990ba854 2699
06a2c219 2700/* Draw stretch glyph string S. */
dc43ef94 2701
06a2c219
GM
2702static void
2703x_draw_stretch_glyph_string (s)
2704 struct glyph_string *s;
2705{
2706 xassert (s->first_glyph->type == STRETCH_GLYPH);
2707 s->stippled_p = s->face->stipple != 0;
990ba854 2708
06a2c219
GM
2709 if (s->hl == DRAW_CURSOR
2710 && !x_stretch_cursor_p)
2711 {
2712 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2713 as wide as the stretch glyph. */
2714 int width = min (CANON_X_UNIT (s->f), s->background_width);
990ba854 2715
06a2c219
GM
2716 /* Draw cursor. */
2717 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
0cdd0c9f 2718
06a2c219
GM
2719 /* Clear rest using the GC of the original non-cursor face. */
2720 if (width < s->background_width)
2721 {
06a2c219
GM
2722 int x = s->x + width, y = s->y;
2723 int w = s->background_width - width, h = s->height;
2724 XRectangle r;
b7f83f9e 2725 GC gc;
dc43ef94 2726
b7f83f9e
GM
2727 if (s->row->mouse_face_p
2728 && cursor_in_mouse_face_p (s->w))
2729 {
2730 x_set_mouse_face_gc (s);
2731 gc = s->gc;
2732 }
2733 else
2734 gc = s->face->gc;
7d0393cf 2735
06a2c219
GM
2736 x_get_glyph_string_clip_rect (s, &r);
2737 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
7d0393cf 2738
06a2c219
GM
2739 if (s->face->stipple)
2740 {
2741 /* Fill background with a stipple pattern. */
2742 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2743 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2744 XSetFillStyle (s->display, gc, FillSolid);
2745 }
2746 else
2747 {
2748 XGCValues xgcv;
2749 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2750 XSetForeground (s->display, gc, xgcv.background);
2751 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2752 XSetForeground (s->display, gc, xgcv.foreground);
2753 }
2754 }
2755 }
61e9f9f3 2756 else if (!s->background_filled_p)
06a2c219
GM
2757 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
2758 s->height);
7d0393cf 2759
06a2c219
GM
2760 s->background_filled_p = 1;
2761}
2762
2763
2764/* Draw glyph string S. */
2765
2766static void
2767x_draw_glyph_string (s)
2768 struct glyph_string *s;
2769{
4458cf11
KH
2770 int relief_drawn_p = 0;
2771
06a2c219
GM
2772 /* If S draws into the background of its successor, draw the
2773 background of the successor first so that S can draw into it.
2774 This makes S->next use XDrawString instead of XDrawImageString. */
66ac4b0e 2775 if (s->next && s->right_overhang && !s->for_overlaps_p)
06a2c219
GM
2776 {
2777 xassert (s->next->img == NULL);
2778 x_set_glyph_string_gc (s->next);
2779 x_set_glyph_string_clipping (s->next);
2780 x_draw_glyph_string_background (s->next, 1);
2781 }
97210f4e 2782
06a2c219
GM
2783 /* Set up S->gc, set clipping and draw S. */
2784 x_set_glyph_string_gc (s);
06a2c219 2785
4458cf11
KH
2786 /* Draw relief (if any) in advance for char/composition so that the
2787 glyph string can be drawn over it. */
2788 if (!s->for_overlaps_p
2789 && s->face->box != FACE_NO_BOX
2790 && (s->first_glyph->type == CHAR_GLYPH
2791 || s->first_glyph->type == COMPOSITE_GLYPH))
2792
2793 {
e6269cbb 2794 x_set_glyph_string_clipping (s);
4458cf11
KH
2795 x_draw_glyph_string_background (s, 1);
2796 x_draw_glyph_string_box (s);
e6269cbb 2797 x_set_glyph_string_clipping (s);
4458cf11
KH
2798 relief_drawn_p = 1;
2799 }
e6269cbb
GM
2800 else
2801 x_set_glyph_string_clipping (s);
4458cf11 2802
06a2c219
GM
2803 switch (s->first_glyph->type)
2804 {
2805 case IMAGE_GLYPH:
2806 x_draw_image_glyph_string (s);
2807 break;
2808
2809 case STRETCH_GLYPH:
2810 x_draw_stretch_glyph_string (s);
2811 break;
2812
2813 case CHAR_GLYPH:
66ac4b0e
GM
2814 if (s->for_overlaps_p)
2815 s->background_filled_p = 1;
2816 else
2817 x_draw_glyph_string_background (s, 0);
06a2c219
GM
2818 x_draw_glyph_string_foreground (s);
2819 break;
2820
b4192550
KH
2821 case COMPOSITE_GLYPH:
2822 if (s->for_overlaps_p || s->gidx > 0)
2823 s->background_filled_p = 1;
2824 else
2825 x_draw_glyph_string_background (s, 1);
2826 x_draw_composite_glyph_string_foreground (s);
2827 break;
2828
06a2c219
GM
2829 default:
2830 abort ();
2831 }
2832
66ac4b0e 2833 if (!s->for_overlaps_p)
06a2c219 2834 {
66ac4b0e
GM
2835 /* Draw underline. */
2836 if (s->face->underline_p)
2837 {
e24e84cc
GM
2838 unsigned long tem, h;
2839 int y;
06a2c219 2840
e24e84cc 2841 /* Get the underline thickness. Default is 1 pixel. */
66ac4b0e
GM
2842 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
2843 h = 1;
e24e84cc
GM
2844
2845 /* Get the underline position. This is the recommended
2846 vertical offset in pixels from the baseline to the top of
2847 the underline. This is a signed value according to the
2848 specs, and its default is
2849
2850 ROUND ((maximum descent) / 2), with
2851 ROUND(x) = floor (x + 0.5) */
7d0393cf 2852
a72d5ce5
GM
2853 if (x_use_underline_position_properties
2854 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
e24e84cc
GM
2855 y = s->ybase + (long) tem;
2856 else if (s->face->font)
2857 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
2858 else
a02f1be0 2859 y = s->y + s->height - h;
7d0393cf 2860
66ac4b0e 2861 if (s->face->underline_defaulted_p)
e24e84cc
GM
2862 XFillRectangle (s->display, s->window, s->gc,
2863 s->x, y, s->width, h);
66ac4b0e
GM
2864 else
2865 {
2866 XGCValues xgcv;
2867 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2868 XSetForeground (s->display, s->gc, s->face->underline_color);
e24e84cc
GM
2869 XFillRectangle (s->display, s->window, s->gc,
2870 s->x, y, s->width, h);
66ac4b0e
GM
2871 XSetForeground (s->display, s->gc, xgcv.foreground);
2872 }
dc6f92b8 2873 }
07e34cb0 2874
66ac4b0e
GM
2875 /* Draw overline. */
2876 if (s->face->overline_p)
06a2c219 2877 {
66ac4b0e
GM
2878 unsigned long dy = 0, h = 1;
2879
2880 if (s->face->overline_color_defaulted_p)
2881 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2882 s->width, h);
2883 else
2884 {
2885 XGCValues xgcv;
2886 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2887 XSetForeground (s->display, s->gc, s->face->overline_color);
2888 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2889 s->width, h);
2890 XSetForeground (s->display, s->gc, xgcv.foreground);
2891 }
06a2c219 2892 }
7d0393cf 2893
66ac4b0e
GM
2894 /* Draw strike-through. */
2895 if (s->face->strike_through_p)
06a2c219 2896 {
66ac4b0e
GM
2897 unsigned long h = 1;
2898 unsigned long dy = (s->height - h) / 2;
2899
2900 if (s->face->strike_through_color_defaulted_p)
2901 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2902 s->width, h);
2903 else
2904 {
2905 XGCValues xgcv;
2906 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2907 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2908 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2909 s->width, h);
2910 XSetForeground (s->display, s->gc, xgcv.foreground);
2911 }
06a2c219 2912 }
7d0393cf 2913
4458cf11
KH
2914 /* Draw relief if not yet drawn. */
2915 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
66ac4b0e
GM
2916 x_draw_glyph_string_box (s);
2917 }
7d0393cf 2918
06a2c219
GM
2919 /* Reset clipping. */
2920 XSetClipMask (s->display, s->gc, None);
dc6f92b8 2921}
07e34cb0 2922
06a2c219 2923
66ac4b0e
GM
2924/* Fix the display of area AREA of overlapping row ROW in window W. */
2925
2926static void
2927x_fix_overlapping_area (w, row, area)
2928 struct window *w;
2929 struct glyph_row *row;
2930 enum glyph_row_area area;
2931{
2932 int i, x;
7d0393cf 2933
66ac4b0e 2934 BLOCK_INPUT;
7d0393cf 2935
66ac4b0e
GM
2936 if (area == LEFT_MARGIN_AREA)
2937 x = 0;
2938 else if (area == TEXT_AREA)
2939 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
2940 else
2941 x = (window_box_width (w, LEFT_MARGIN_AREA)
2942 + window_box_width (w, TEXT_AREA));
2943
2944 for (i = 0; i < row->used[area];)
2945 {
2946 if (row->glyphs[area][i].overlaps_vertically_p)
2947 {
2948 int start = i, start_x = x;
2949
2950 do
2951 {
2952 x += row->glyphs[area][i].pixel_width;
2953 ++i;
2954 }
2955 while (i < row->used[area]
2956 && row->glyphs[area][i].overlaps_vertically_p);
2957
2958 x_draw_glyphs (w, start_x, row, area, start, i,
f0a48a01 2959 DRAW_NORMAL_TEXT, 1);
66ac4b0e
GM
2960 }
2961 else
2962 {
2963 x += row->glyphs[area][i].pixel_width;
2964 ++i;
2965 }
2966 }
7d0393cf 2967
66ac4b0e
GM
2968 UNBLOCK_INPUT;
2969}
2970
2971
06a2c219
GM
2972/* Output LEN glyphs starting at START at the nominal cursor position.
2973 Advance the nominal cursor over the text. The global variable
2974 updated_window contains the window being updated, updated_row is
2975 the glyph row being updated, and updated_area is the area of that
2976 row being updated. */
dc6f92b8 2977
06a2c219
GM
2978static void
2979x_write_glyphs (start, len)
2980 struct glyph *start;
2981 int len;
2982{
f0a48a01 2983 int x, hpos;
d9cdbb3d 2984
06a2c219 2985 xassert (updated_window && updated_row);
dc6f92b8 2986 BLOCK_INPUT;
7d0393cf 2987
06a2c219 2988 /* Write glyphs. */
dc6f92b8 2989
06a2c219
GM
2990 hpos = start - updated_row->glyphs[updated_area];
2991 x = x_draw_glyphs (updated_window, output_cursor.x,
2992 updated_row, updated_area,
2993 hpos, hpos + len,
f0a48a01 2994 DRAW_NORMAL_TEXT, 0);
dc6f92b8 2995
6065f9e2
KS
2996 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
2997 if (updated_area == TEXT_AREA
2998 && updated_window->phys_cursor_on_p
2999 && updated_window->phys_cursor.vpos == output_cursor.vpos
3000 && updated_window->phys_cursor.hpos >= hpos
3001 && updated_window->phys_cursor.hpos < hpos + len)
3002 updated_window->phys_cursor_on_p = 0;
3003
dc6f92b8 3004 UNBLOCK_INPUT;
7d0393cf 3005
06a2c219
GM
3006 /* Advance the output cursor. */
3007 output_cursor.hpos += len;
3008 output_cursor.x = x;
dc6f92b8
JB
3009}
3010
0cdd0c9f 3011
06a2c219 3012/* Insert LEN glyphs from START at the nominal cursor position. */
0cdd0c9f 3013
06a2c219
GM
3014static void
3015x_insert_glyphs (start, len)
3016 struct glyph *start;
3017 register int len;
3018{
3019 struct frame *f;
3020 struct window *w;
3021 int line_height, shift_by_width, shifted_region_width;
3022 struct glyph_row *row;
3023 struct glyph *glyph;
2beb36f9 3024 int frame_x, frame_y, hpos;
58769bee 3025
06a2c219 3026 xassert (updated_window && updated_row);
0cdd0c9f 3027 BLOCK_INPUT;
06a2c219
GM
3028 w = updated_window;
3029 f = XFRAME (WINDOW_FRAME (w));
3030
3031 /* Get the height of the line we are in. */
3032 row = updated_row;
3033 line_height = row->height;
3034
3035 /* Get the width of the glyphs to insert. */
3036 shift_by_width = 0;
3037 for (glyph = start; glyph < start + len; ++glyph)
3038 shift_by_width += glyph->pixel_width;
3039
3040 /* Get the width of the region to shift right. */
3041 shifted_region_width = (window_box_width (w, updated_area)
3042 - output_cursor.x
3043 - shift_by_width);
3044
3045 /* Shift right. */
bf0ab8a2 3046 frame_x = window_box_left (w, updated_area) + output_cursor.x;
06a2c219
GM
3047 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
3048 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3049 f->output_data.x->normal_gc,
3050 frame_x, frame_y,
3051 shifted_region_width, line_height,
3052 frame_x + shift_by_width, frame_y);
3053
3054 /* Write the glyphs. */
3055 hpos = start - row->glyphs[updated_area];
3056 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
f0a48a01 3057 DRAW_NORMAL_TEXT, 0);
7d0393cf 3058
06a2c219
GM
3059 /* Advance the output cursor. */
3060 output_cursor.hpos += len;
3061 output_cursor.x += shift_by_width;
0cdd0c9f
RS
3062 UNBLOCK_INPUT;
3063}
0cdd0c9f 3064
0cdd0c9f 3065
06a2c219
GM
3066/* Delete N glyphs at the nominal cursor position. Not implemented
3067 for X frames. */
c83febd7
RS
3068
3069static void
06a2c219
GM
3070x_delete_glyphs (n)
3071 register int n;
c83febd7 3072{
06a2c219 3073 abort ();
c83febd7
RS
3074}
3075
0cdd0c9f 3076
c5e6e06b
GM
3077/* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
3078 If they are <= 0, this is probably an error. */
3079
3080void
3081x_clear_area (dpy, window, x, y, width, height, exposures)
3082 Display *dpy;
3083 Window window;
3084 int x, y;
3085 int width, height;
3086 int exposures;
3087{
3088 xassert (width > 0 && height > 0);
3089 XClearArea (dpy, window, x, y, width, height, exposures);
3090}
3091
3092
06a2c219
GM
3093/* Erase the current text line from the nominal cursor position
3094 (inclusive) to pixel column TO_X (exclusive). The idea is that
3095 everything from TO_X onward is already erased.
3096
3097 TO_X is a pixel position relative to updated_area of
3098 updated_window. TO_X == -1 means clear to the end of this area. */
dc6f92b8 3099
06a2c219
GM
3100static void
3101x_clear_end_of_line (to_x)
3102 int to_x;
3103{
3104 struct frame *f;
3105 struct window *w = updated_window;
3106 int max_x, min_y, max_y;
3107 int from_x, from_y, to_y;
7d0393cf 3108
06a2c219
GM
3109 xassert (updated_window && updated_row);
3110 f = XFRAME (w->frame);
7d0393cf 3111
06a2c219
GM
3112 if (updated_row->full_width_p)
3113 {
3114 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
3115 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
3116 && !w->pseudo_window_p)
3117 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
0cdd0c9f 3118 }
06a2c219
GM
3119 else
3120 max_x = window_box_width (w, updated_area);
3121 max_y = window_text_bottom_y (w);
dc6f92b8 3122
06a2c219
GM
3123 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
3124 of window. For TO_X > 0, truncate to end of drawing area. */
3125 if (to_x == 0)
3126 return;
3127 else if (to_x < 0)
3128 to_x = max_x;
3129 else
3130 to_x = min (to_x, max_x);
dbc4e1c1 3131
06a2c219 3132 to_y = min (max_y, output_cursor.y + updated_row->height);
7d0393cf 3133
06a2c219
GM
3134 /* Notice if the cursor will be cleared by this operation. */
3135 if (!updated_row->full_width_p)
60626bab
GM
3136 notice_overwritten_cursor (w, updated_area,
3137 output_cursor.x, -1,
3138 updated_row->y,
3139 MATRIX_ROW_BOTTOM_Y (updated_row));
dbc4e1c1 3140
06a2c219 3141 from_x = output_cursor.x;
7d0393cf 3142
06a2c219
GM
3143 /* Translate to frame coordinates. */
3144 if (updated_row->full_width_p)
3145 {
3146 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
3147 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
3148 }
0cdd0c9f
RS
3149 else
3150 {
06a2c219
GM
3151 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
3152 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
3153 }
7d0393cf 3154
045dee35 3155 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
06a2c219
GM
3156 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
3157 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
7d0393cf 3158
06a2c219
GM
3159 /* Prevent inadvertently clearing to end of the X window. */
3160 if (to_x > from_x && to_y > from_y)
3161 {
3162 BLOCK_INPUT;
c5e6e06b
GM
3163 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3164 from_x, from_y, to_x - from_x, to_y - from_y,
3165 False);
06a2c219 3166 UNBLOCK_INPUT;
0cdd0c9f 3167 }
0cdd0c9f 3168}
dbc4e1c1 3169
0cdd0c9f 3170
06a2c219 3171/* Clear entire frame. If updating_frame is non-null, clear that
b86bd3dd 3172 frame. Otherwise clear the selected frame. */
06a2c219
GM
3173
3174static void
3175x_clear_frame ()
0cdd0c9f 3176{
06a2c219 3177 struct frame *f;
0cdd0c9f 3178
06a2c219
GM
3179 if (updating_frame)
3180 f = updating_frame;
0cdd0c9f 3181 else
b86bd3dd 3182 f = SELECTED_FRAME ();
58769bee 3183
06a2c219
GM
3184 /* Clearing the frame will erase any cursor, so mark them all as no
3185 longer visible. */
3186 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
3187 output_cursor.hpos = output_cursor.vpos = 0;
3188 output_cursor.x = -1;
3189
3190 /* We don't set the output cursor here because there will always
3191 follow an explicit cursor_to. */
3192 BLOCK_INPUT;
3193 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3194
3195 /* We have to clear the scroll bars, too. If we have changed
3196 colors or something like that, then they should be notified. */
3197 x_scroll_bar_clear (f);
0cdd0c9f 3198
06a2c219 3199 XFlush (FRAME_X_DISPLAY (f));
0cb35f4e
JD
3200
3201#ifdef USE_GTK
3202 xg_frame_cleared (f);
3203#endif
3204
06a2c219 3205 UNBLOCK_INPUT;
dc6f92b8 3206}
06a2c219
GM
3207
3208
dc6f92b8 3209\f
dbc4e1c1
JB
3210/* Invert the middle quarter of the frame for .15 sec. */
3211
06a2c219
GM
3212/* We use the select system call to do the waiting, so we have to make
3213 sure it's available. If it isn't, we just won't do visual bells. */
3214
dbc4e1c1
JB
3215#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
3216
06a2c219
GM
3217
3218/* Subtract the `struct timeval' values X and Y, storing the result in
3219 *RESULT. Return 1 if the difference is negative, otherwise 0. */
dbc4e1c1
JB
3220
3221static int
3222timeval_subtract (result, x, y)
3223 struct timeval *result, x, y;
3224{
06a2c219
GM
3225 /* Perform the carry for the later subtraction by updating y. This
3226 is safer because on some systems the tv_sec member is unsigned. */
dbc4e1c1
JB
3227 if (x.tv_usec < y.tv_usec)
3228 {
3229 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
3230 y.tv_usec -= 1000000 * nsec;
3231 y.tv_sec += nsec;
3232 }
7d0393cf 3233
dbc4e1c1
JB
3234 if (x.tv_usec - y.tv_usec > 1000000)
3235 {
3236 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
3237 y.tv_usec += 1000000 * nsec;
3238 y.tv_sec -= nsec;
3239 }
3240
06a2c219
GM
3241 /* Compute the time remaining to wait. tv_usec is certainly
3242 positive. */
dbc4e1c1
JB
3243 result->tv_sec = x.tv_sec - y.tv_sec;
3244 result->tv_usec = x.tv_usec - y.tv_usec;
3245
06a2c219
GM
3246 /* Return indication of whether the result should be considered
3247 negative. */
dbc4e1c1
JB
3248 return x.tv_sec < y.tv_sec;
3249}
dc6f92b8 3250
dfcf069d 3251void
f676886a
JB
3252XTflash (f)
3253 struct frame *f;
dc6f92b8 3254{
dbc4e1c1 3255 BLOCK_INPUT;
dc6f92b8 3256
dbc4e1c1
JB
3257 {
3258 GC gc;
dc6f92b8 3259
06a2c219
GM
3260 /* Create a GC that will use the GXxor function to flip foreground
3261 pixels into background pixels. */
dbc4e1c1
JB
3262 {
3263 XGCValues values;
dc6f92b8 3264
dbc4e1c1 3265 values.function = GXxor;
7556890b
RS
3266 values.foreground = (f->output_data.x->foreground_pixel
3267 ^ f->output_data.x->background_pixel);
58769bee 3268
334208b7 3269 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dbc4e1c1
JB
3270 GCFunction | GCForeground, &values);
3271 }
dc6f92b8 3272
dbc4e1c1 3273 {
e84e14c3
RS
3274 /* Get the height not including a menu bar widget. */
3275 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
3276 /* Height of each line to flash. */
3277 int flash_height = FRAME_LINE_HEIGHT (f);
3278 /* These will be the left and right margins of the rectangles. */
3279 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
3280 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
3281
3282 int width;
3283
3284 /* Don't flash the area between a scroll bar and the frame
3285 edge it is next to. */
3286 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
3287 {
3288 case vertical_scroll_bar_left:
3289 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3290 break;
3291
3292 case vertical_scroll_bar_right:
3293 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3294 break;
06a2c219
GM
3295
3296 default:
3297 break;
e84e14c3
RS
3298 }
3299
3300 width = flash_right - flash_left;
3301
3302 /* If window is tall, flash top and bottom line. */
3303 if (height > 3 * FRAME_LINE_HEIGHT (f))
3304 {
3305 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
3306 flash_left,
3307 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 3308 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
3309 width, flash_height);
3310 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3311 flash_left,
3312 (height - flash_height
3313 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3314 width, flash_height);
3315 }
3316 else
7d0393cf 3317 /* If it is short, flash it all. */
e84e14c3
RS
3318 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3319 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3320 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
dc6f92b8 3321
06a2c219 3322 x_flush (f);
dc6f92b8 3323
dbc4e1c1 3324 {
06a2c219 3325 struct timeval wakeup;
dc6f92b8 3326
66c30ea1 3327 EMACS_GET_TIME (wakeup);
dc6f92b8 3328
dbc4e1c1
JB
3329 /* Compute time to wait until, propagating carry from usecs. */
3330 wakeup.tv_usec += 150000;
3331 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
3332 wakeup.tv_usec %= 1000000;
3333
101922c3
GM
3334 /* Keep waiting until past the time wakeup or any input gets
3335 available. */
3336 while (! detect_input_pending ())
dbc4e1c1 3337 {
101922c3 3338 struct timeval current;
dbc4e1c1
JB
3339 struct timeval timeout;
3340
101922c3 3341 EMACS_GET_TIME (current);
dbc4e1c1 3342
101922c3
GM
3343 /* Break if result would be negative. */
3344 if (timeval_subtract (&current, wakeup, current))
dbc4e1c1
JB
3345 break;
3346
101922c3
GM
3347 /* How long `select' should wait. */
3348 timeout.tv_sec = 0;
3349 timeout.tv_usec = 10000;
3350
dbc4e1c1 3351 /* Try to wait that long--but we might wake up sooner. */
c32cdd9a 3352 select (0, NULL, NULL, NULL, &timeout);
dbc4e1c1
JB
3353 }
3354 }
58769bee 3355
e84e14c3
RS
3356 /* If window is tall, flash top and bottom line. */
3357 if (height > 3 * FRAME_LINE_HEIGHT (f))
3358 {
3359 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
3360 flash_left,
3361 (FRAME_INTERNAL_BORDER_WIDTH (f)
9ea173e8 3362 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
e84e14c3
RS
3363 width, flash_height);
3364 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3365 flash_left,
3366 (height - flash_height
3367 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3368 width, flash_height);
3369 }
3370 else
7d0393cf 3371 /* If it is short, flash it all. */
e84e14c3
RS
3372 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3373 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3374 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3375
334208b7 3376 XFreeGC (FRAME_X_DISPLAY (f), gc);
06a2c219 3377 x_flush (f);
dc6f92b8 3378 }
dbc4e1c1
JB
3379 }
3380
3381 UNBLOCK_INPUT;
dc6f92b8
JB
3382}
3383
06a2c219 3384#endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
dbc4e1c1
JB
3385
3386
dc6f92b8
JB
3387/* Make audible bell. */
3388
dfcf069d 3389void
dc6f92b8
JB
3390XTring_bell ()
3391{
b86bd3dd 3392 struct frame *f = SELECTED_FRAME ();
7d0393cf 3393
b86bd3dd
GM
3394 if (FRAME_X_DISPLAY (f))
3395 {
dbc4e1c1 3396#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
b86bd3dd
GM
3397 if (visible_bell)
3398 XTflash (f);
3399 else
dbc4e1c1 3400#endif
b86bd3dd
GM
3401 {
3402 BLOCK_INPUT;
3403 XBell (FRAME_X_DISPLAY (f), 0);
3404 XFlush (FRAME_X_DISPLAY (f));
3405 UNBLOCK_INPUT;
3406 }
dc6f92b8
JB
3407 }
3408}
06a2c219 3409
dc6f92b8 3410\f
06a2c219
GM
3411/* Specify how many text lines, from the top of the window,
3412 should be affected by insert-lines and delete-lines operations.
3413 This, and those operations, are used only within an update
3414 that is bounded by calls to x_update_begin and x_update_end. */
dc6f92b8 3415
dfcf069d 3416static void
06a2c219
GM
3417XTset_terminal_window (n)
3418 register int n;
dc6f92b8 3419{
06a2c219 3420 /* This function intentionally left blank. */
dc6f92b8
JB
3421}
3422
06a2c219
GM
3423
3424\f
3425/***********************************************************************
3426 Line Dance
3427 ***********************************************************************/
3428
3429/* Perform an insert-lines or delete-lines operation, inserting N
3430 lines or deleting -N lines at vertical position VPOS. */
3431
dfcf069d 3432static void
06a2c219
GM
3433x_ins_del_lines (vpos, n)
3434 int vpos, n;
dc6f92b8
JB
3435{
3436 abort ();
3437}
06a2c219
GM
3438
3439
3440/* Scroll part of the display as described by RUN. */
dc6f92b8 3441
dfcf069d 3442static void
06a2c219
GM
3443x_scroll_run (w, run)
3444 struct window *w;
3445 struct run *run;
dc6f92b8 3446{
06a2c219
GM
3447 struct frame *f = XFRAME (w->frame);
3448 int x, y, width, height, from_y, to_y, bottom_y;
3449
3450 /* Get frame-relative bounding box of the text display area of W,
3f332ef3
KS
3451 without mode lines. Include in this box the left and right
3452 fringe of W. */
06a2c219 3453 window_box (w, -1, &x, &y, &width, &height);
3f332ef3
KS
3454 width += FRAME_X_FRINGE_WIDTH (f);
3455 x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
06a2c219
GM
3456
3457 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3458 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3459 bottom_y = y + height;
dc6f92b8 3460
06a2c219
GM
3461 if (to_y < from_y)
3462 {
3463 /* Scrolling up. Make sure we don't copy part of the mode
3464 line at the bottom. */
3465 if (from_y + run->height > bottom_y)
3466 height = bottom_y - from_y;
3467 else
3468 height = run->height;
3469 }
dc6f92b8 3470 else
06a2c219
GM
3471 {
3472 /* Scolling down. Make sure we don't copy over the mode line.
3473 at the bottom. */
3474 if (to_y + run->height > bottom_y)
3475 height = bottom_y - to_y;
3476 else
3477 height = run->height;
3478 }
7a13e894 3479
06a2c219 3480 BLOCK_INPUT;
7d0393cf 3481
06a2c219
GM
3482 /* Cursor off. Will be switched on again in x_update_window_end. */
3483 updated_window = w;
3484 x_clear_cursor (w);
3485
3486 XCopyArea (FRAME_X_DISPLAY (f),
3487 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3488 f->output_data.x->normal_gc,
3489 x, from_y,
3490 width, height,
3491 x, to_y);
7d0393cf 3492
06a2c219
GM
3493 UNBLOCK_INPUT;
3494}
dc6f92b8 3495
dc6f92b8 3496
06a2c219
GM
3497\f
3498/***********************************************************************
3499 Exposure Events
3500 ***********************************************************************/
7d0393cf 3501
06a2c219
GM
3502/* Redisplay an exposed area of frame F. X and Y are the upper-left
3503 corner of the exposed rectangle. W and H are width and height of
3504 the exposed area. All are pixel values. W or H zero means redraw
3505 the entire frame. */
dc6f92b8 3506
06a2c219
GM
3507static void
3508expose_frame (f, x, y, w, h)
3509 struct frame *f;
3510 int x, y, w, h;
dc6f92b8 3511{
06a2c219 3512 XRectangle r;
82f053ab 3513 int mouse_face_overwritten_p = 0;
dc6f92b8 3514
06a2c219 3515 TRACE ((stderr, "expose_frame "));
dc6f92b8 3516
06a2c219
GM
3517 /* No need to redraw if frame will be redrawn soon. */
3518 if (FRAME_GARBAGED_P (f))
dc6f92b8 3519 {
06a2c219
GM
3520 TRACE ((stderr, " garbaged\n"));
3521 return;
3522 }
3523
3524 /* If basic faces haven't been realized yet, there is no point in
3525 trying to redraw anything. This can happen when we get an expose
3526 event while Emacs is starting, e.g. by moving another window. */
3527 if (FRAME_FACE_CACHE (f) == NULL
3528 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
3529 {
3530 TRACE ((stderr, " no faces\n"));
3531 return;
58769bee 3532 }
06a2c219
GM
3533
3534 if (w == 0 || h == 0)
58769bee 3535 {
06a2c219
GM
3536 r.x = r.y = 0;
3537 r.width = CANON_X_UNIT (f) * f->width;
3538 r.height = CANON_Y_UNIT (f) * f->height;
dc6f92b8
JB
3539 }
3540 else
3541 {
06a2c219
GM
3542 r.x = x;
3543 r.y = y;
3544 r.width = w;
3545 r.height = h;
3546 }
3547
3548 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
82f053ab 3549 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
06a2c219 3550
9ea173e8 3551 if (WINDOWP (f->tool_bar_window))
82f053ab
GM
3552 mouse_face_overwritten_p
3553 |= expose_window (XWINDOW (f->tool_bar_window), &r);
06a2c219
GM
3554
3555#ifndef USE_X_TOOLKIT
3556 if (WINDOWP (f->menu_bar_window))
82f053ab
GM
3557 mouse_face_overwritten_p
3558 |= expose_window (XWINDOW (f->menu_bar_window), &r);
06a2c219 3559#endif /* not USE_X_TOOLKIT */
82f053ab
GM
3560
3561 /* Some window managers support a focus-follows-mouse style with
3562 delayed raising of frames. Imagine a partially obscured frame,
3563 and moving the mouse into partially obscured mouse-face on that
3564 frame. The visible part of the mouse-face will be highlighted,
3565 then the WM raises the obscured frame. With at least one WM, KDE
3566 2.1, Emacs is not getting any event for the raising of the frame
3567 (even tried with SubstructureRedirectMask), only Expose events.
3568 These expose events will draw text normally, i.e. not
3569 highlighted. Which means we must redo the highlight here.
3570 Subsume it under ``we love X''. --gerd 2001-08-15 */
3571 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
3572 {
3573 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3574 if (f == dpyinfo->mouse_face_mouse_frame)
3575 {
3576 int x = dpyinfo->mouse_face_mouse_x;
3577 int y = dpyinfo->mouse_face_mouse_y;
3578 clear_mouse_face (dpyinfo);
3579 note_mouse_highlight (f, x, y);
3580 }
3581 }
dc6f92b8
JB
3582}
3583
06a2c219
GM
3584
3585/* Redraw (parts) of all windows in the window tree rooted at W that
82f053ab
GM
3586 intersect R. R contains frame pixel coordinates. Value is
3587 non-zero if the exposure overwrites mouse-face. */
06a2c219 3588
82f053ab 3589static int
06a2c219
GM
3590expose_window_tree (w, r)
3591 struct window *w;
3592 XRectangle *r;
dc6f92b8 3593{
82f053ab
GM
3594 struct frame *f = XFRAME (w->frame);
3595 int mouse_face_overwritten_p = 0;
7d0393cf 3596
82f053ab 3597 while (w && !FRAME_GARBAGED_P (f))
06a2c219
GM
3598 {
3599 if (!NILP (w->hchild))
82f053ab
GM
3600 mouse_face_overwritten_p
3601 |= expose_window_tree (XWINDOW (w->hchild), r);
06a2c219 3602 else if (!NILP (w->vchild))
82f053ab
GM
3603 mouse_face_overwritten_p
3604 |= expose_window_tree (XWINDOW (w->vchild), r);
3605 else
3606 mouse_face_overwritten_p |= expose_window (w, r);
7d0393cf 3607
a02f1be0 3608 w = NILP (w->next) ? NULL : XWINDOW (w->next);
06a2c219 3609 }
82f053ab
GM
3610
3611 return mouse_face_overwritten_p;
06a2c219 3612}
58769bee 3613
dc6f92b8 3614
06a2c219
GM
3615/* Redraw the part of glyph row area AREA of glyph row ROW on window W
3616 which intersects rectangle R. R is in window-relative coordinates. */
3617
3618static void
3619expose_area (w, row, r, area)
3620 struct window *w;
3621 struct glyph_row *row;
3622 XRectangle *r;
3623 enum glyph_row_area area;
3624{
06a2c219
GM
3625 struct glyph *first = row->glyphs[area];
3626 struct glyph *end = row->glyphs[area] + row->used[area];
3627 struct glyph *last;
4bc6dcc7 3628 int first_x, start_x, x;
06a2c219 3629
6fb13182
GM
3630 if (area == TEXT_AREA && row->fill_line_p)
3631 /* If row extends face to end of line write the whole line. */
153f5ed7 3632 x_draw_glyphs (w, 0, row, area, 0, row->used[area],
f0a48a01 3633 DRAW_NORMAL_TEXT, 0);
6fb13182
GM
3634 else
3635 {
4bc6dcc7
GM
3636 /* Set START_X to the window-relative start position for drawing glyphs of
3637 AREA. The first glyph of the text area can be partially visible.
3638 The first glyphs of other areas cannot. */
3639 if (area == LEFT_MARGIN_AREA)
3640 start_x = 0;
3641 else if (area == TEXT_AREA)
3642 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
3643 else
3644 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
3645 + window_box_width (w, TEXT_AREA));
3646 x = start_x;
3647
6fb13182
GM
3648 /* Find the first glyph that must be redrawn. */
3649 while (first < end
3650 && x + first->pixel_width < r->x)
3651 {
3652 x += first->pixel_width;
3653 ++first;
3654 }
7d0393cf 3655
6fb13182
GM
3656 /* Find the last one. */
3657 last = first;
3658 first_x = x;
3659 while (last < end
3660 && x < r->x + r->width)
3661 {
3662 x += last->pixel_width;
3663 ++last;
3664 }
7d0393cf 3665
6fb13182
GM
3666 /* Repaint. */
3667 if (last > first)
4bc6dcc7 3668 x_draw_glyphs (w, first_x - start_x, row, area,
6fb13182
GM
3669 first - row->glyphs[area],
3670 last - row->glyphs[area],
f0a48a01 3671 DRAW_NORMAL_TEXT, 0);
6fb13182 3672 }
06a2c219 3673}
7d0393cf 3674
58769bee 3675
06a2c219 3676/* Redraw the parts of the glyph row ROW on window W intersecting
82f053ab
GM
3677 rectangle R. R is in window-relative coordinates. Value is
3678 non-zero if mouse-face was overwritten. */
dc6f92b8 3679
82f053ab 3680static int
06a2c219
GM
3681expose_line (w, row, r)
3682 struct window *w;
3683 struct glyph_row *row;
3684 XRectangle *r;
3685{
3686 xassert (row->enabled_p);
7d0393cf 3687
06a2c219
GM
3688 if (row->mode_line_p || w->pseudo_window_p)
3689 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
f0a48a01 3690 DRAW_NORMAL_TEXT, 0);
06a2c219
GM
3691 else
3692 {
3693 if (row->used[LEFT_MARGIN_AREA])
3694 expose_area (w, row, r, LEFT_MARGIN_AREA);
3695 if (row->used[TEXT_AREA])
3696 expose_area (w, row, r, TEXT_AREA);
3697 if (row->used[RIGHT_MARGIN_AREA])
3698 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5958f265 3699 draw_row_fringe_bitmaps (w, row);
06a2c219 3700 }
82f053ab
GM
3701
3702 return row->mouse_face_p;
06a2c219 3703}
dc6f92b8 3704
58769bee 3705
06a2c219
GM
3706/* Return non-zero if W's cursor intersects rectangle R. */
3707
3708static int
3709x_phys_cursor_in_rect_p (w, r)
3710 struct window *w;
3711 XRectangle *r;
3712{
3713 XRectangle cr, result;
3714 struct glyph *cursor_glyph;
3715
3716 cursor_glyph = get_phys_cursor_glyph (w);
3717 if (cursor_glyph)
3718 {
3719 cr.x = w->phys_cursor.x;
3720 cr.y = w->phys_cursor.y;
3721 cr.width = cursor_glyph->pixel_width;
3722 cr.height = w->phys_cursor_height;
3723 return x_intersect_rectangles (&cr, r, &result);
3724 }
3725 else
3726 return 0;
dc6f92b8 3727}
dc6f92b8 3728
06a2c219 3729
4f53f3b8
RS
3730/* Redraw those parts of glyphs rows during expose event handling that
3731 overlap other rows. Redrawing of an exposed line writes over parts
3732 of lines overlapping that exposed line; this function fixes that.
3733
3734 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
3735 row in W's current matrix that is exposed and overlaps other rows.
3736 LAST_OVERLAPPING_ROW is the last such row. */
3737
3738static void
3739expose_overlaps (w, first_overlapping_row, last_overlapping_row)
3740 struct window *w;
3741 struct glyph_row *first_overlapping_row;
3742 struct glyph_row *last_overlapping_row;
3743{
3744 struct glyph_row *row;
7d0393cf 3745
4f53f3b8
RS
3746 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
3747 if (row->overlapping_p)
3748 {
3749 xassert (row->enabled_p && !row->mode_line_p);
7d0393cf 3750
4f53f3b8
RS
3751 if (row->used[LEFT_MARGIN_AREA])
3752 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
7d0393cf 3753
4f53f3b8
RS
3754 if (row->used[TEXT_AREA])
3755 x_fix_overlapping_area (w, row, TEXT_AREA);
7d0393cf 3756
4f53f3b8
RS
3757 if (row->used[RIGHT_MARGIN_AREA])
3758 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
3759 }
3760}
3761
3762
a02f1be0
GM
3763/* Redraw the part of window W intersection rectangle FR. Pixel
3764 coordinates in FR are frame-relative. Call this function with
82f053ab
GM
3765 input blocked. Value is non-zero if the exposure overwrites
3766 mouse-face. */
dc6f92b8 3767
a39202f6 3768static int
a02f1be0 3769expose_window (w, fr)
06a2c219 3770 struct window *w;
a02f1be0 3771 XRectangle *fr;
dc6f92b8 3772{
a02f1be0 3773 struct frame *f = XFRAME (w->frame);
a02f1be0 3774 XRectangle wr, r;
82f053ab 3775 int mouse_face_overwritten_p = 0;
dc6f92b8 3776
80c32bcc
GM
3777 /* If window is not yet fully initialized, do nothing. This can
3778 happen when toolkit scroll bars are used and a window is split.
3779 Reconfiguring the scroll bar will generate an expose for a newly
3780 created window. */
a39202f6 3781 if (w->current_matrix == NULL)
82f053ab 3782 return 0;
a39202f6
GM
3783
3784 /* When we're currently updating the window, display and current
3785 matrix usually don't agree. Arrange for a thorough display
3786 later. */
3787 if (w == updated_window)
3788 {
3789 SET_FRAME_GARBAGED (f);
3790 return 0;
3791 }
80c32bcc 3792
a39202f6 3793 /* Frame-relative pixel rectangle of W. */
a02f1be0
GM
3794 wr.x = XFASTINT (w->left) * CANON_X_UNIT (f);
3795 wr.y = XFASTINT (w->top) * CANON_Y_UNIT (f);
3796 wr.width = XFASTINT (w->width) * CANON_X_UNIT (f);
3797 wr.height = XFASTINT (w->height) * CANON_Y_UNIT (f);
3798
a39202f6
GM
3799 if (x_intersect_rectangles (fr, &wr, &r))
3800 {
3801 int yb = window_text_bottom_y (w);
3802 struct glyph_row *row;
3803 int cursor_cleared_p;
4f53f3b8 3804 struct glyph_row *first_overlapping_row, *last_overlapping_row;
7d0393cf 3805
a39202f6
GM
3806 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
3807 r.x, r.y, r.width, r.height));
dc6f92b8 3808
a39202f6
GM
3809 /* Convert to window coordinates. */
3810 r.x = FRAME_TO_WINDOW_PIXEL_X (w, r.x);
3811 r.y = FRAME_TO_WINDOW_PIXEL_Y (w, r.y);
dc6f92b8 3812
a39202f6
GM
3813 /* Turn off the cursor. */
3814 if (!w->pseudo_window_p
3815 && x_phys_cursor_in_rect_p (w, &r))
3816 {
3817 x_clear_cursor (w);
3818 cursor_cleared_p = 1;
3819 }
3820 else
3821 cursor_cleared_p = 0;
06a2c219 3822
4f53f3b8
RS
3823 /* Update lines intersecting rectangle R. */
3824 first_overlapping_row = last_overlapping_row = NULL;
a39202f6
GM
3825 for (row = w->current_matrix->rows;
3826 row->enabled_p;
3827 ++row)
3828 {
3829 int y0 = row->y;
3830 int y1 = MATRIX_ROW_BOTTOM_Y (row);
7d0393cf 3831
a39202f6
GM
3832 if ((y0 >= r.y && y0 < r.y + r.height)
3833 || (y1 > r.y && y1 < r.y + r.height)
3834 || (r.y >= y0 && r.y < y1)
3835 || (r.y + r.height > y0 && r.y + r.height < y1))
82f053ab 3836 {
4f53f3b8
RS
3837 if (row->overlapping_p)
3838 {
3839 if (first_overlapping_row == NULL)
3840 first_overlapping_row = row;
3841 last_overlapping_row = row;
3842 }
7d0393cf 3843
82f053ab
GM
3844 if (expose_line (w, row, &r))
3845 mouse_face_overwritten_p = 1;
3846 }
7d0393cf 3847
a39202f6
GM
3848 if (y1 >= yb)
3849 break;
3850 }
dc6f92b8 3851
a39202f6
GM
3852 /* Display the mode line if there is one. */
3853 if (WINDOW_WANTS_MODELINE_P (w)
3854 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
3855 row->enabled_p)
3856 && row->y < r.y + r.height)
82f053ab
GM
3857 {
3858 if (expose_line (w, row, &r))
3859 mouse_face_overwritten_p = 1;
3860 }
a39202f6
GM
3861
3862 if (!w->pseudo_window_p)
3863 {
4f53f3b8
RS
3864 /* Fix the display of overlapping rows. */
3865 if (first_overlapping_row)
3866 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
7d0393cf 3867
a39202f6
GM
3868 /* Draw border between windows. */
3869 x_draw_vertical_border (w);
7d0393cf 3870
a39202f6
GM
3871 /* Turn the cursor on again. */
3872 if (cursor_cleared_p)
3873 x_update_window_cursor (w, 1);
3874 }
06a2c219 3875 }
82f053ab
GM
3876
3877 return mouse_face_overwritten_p;
06a2c219 3878}
dc6f92b8 3879
dc6f92b8 3880
06a2c219
GM
3881/* Determine the intersection of two rectangles R1 and R2. Return
3882 the intersection in *RESULT. Value is non-zero if RESULT is not
3883 empty. */
3884
3885static int
3886x_intersect_rectangles (r1, r2, result)
3887 XRectangle *r1, *r2, *result;
3888{
3889 XRectangle *left, *right;
3890 XRectangle *upper, *lower;
3891 int intersection_p = 0;
7d0393cf 3892
06a2c219
GM
3893 /* Rearrange so that R1 is the left-most rectangle. */
3894 if (r1->x < r2->x)
3895 left = r1, right = r2;
3896 else
3897 left = r2, right = r1;
3898
3899 /* X0 of the intersection is right.x0, if this is inside R1,
3900 otherwise there is no intersection. */
3901 if (right->x <= left->x + left->width)
3902 {
3903 result->x = right->x;
7d0393cf 3904
06a2c219
GM
3905 /* The right end of the intersection is the minimum of the
3906 the right ends of left and right. */
3907 result->width = (min (left->x + left->width, right->x + right->width)
3908 - result->x);
3909
3910 /* Same game for Y. */
3911 if (r1->y < r2->y)
3912 upper = r1, lower = r2;
3913 else
3914 upper = r2, lower = r1;
3915
3916 /* The upper end of the intersection is lower.y0, if this is inside
3917 of upper. Otherwise, there is no intersection. */
3918 if (lower->y <= upper->y + upper->height)
dc43ef94 3919 {
06a2c219 3920 result->y = lower->y;
7d0393cf 3921
06a2c219
GM
3922 /* The lower end of the intersection is the minimum of the lower
3923 ends of upper and lower. */
7d0393cf 3924 result->height = (min (lower->y + lower->height,
06a2c219
GM
3925 upper->y + upper->height)
3926 - result->y);
3927 intersection_p = 1;
dc43ef94 3928 }
dc6f92b8
JB
3929 }
3930
06a2c219 3931 return intersection_p;
dc6f92b8 3932}
06a2c219
GM
3933
3934
3935
3936
dc6f92b8 3937\f
dc6f92b8 3938static void
334208b7
RS
3939frame_highlight (f)
3940 struct frame *f;
dc6f92b8 3941{
b3e1e05c
JB
3942 /* We used to only do this if Vx_no_window_manager was non-nil, but
3943 the ICCCM (section 4.1.6) says that the window's border pixmap
3944 and border pixel are window attributes which are "private to the
3945 client", so we can always change it to whatever we want. */
3946 BLOCK_INPUT;
334208b7 3947 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 3948 f->output_data.x->border_pixel);
b3e1e05c 3949 UNBLOCK_INPUT;
5d46f928 3950 x_update_cursor (f, 1);
dc6f92b8
JB
3951}
3952
3953static void
334208b7
RS
3954frame_unhighlight (f)
3955 struct frame *f;
dc6f92b8 3956{
b3e1e05c
JB
3957 /* We used to only do this if Vx_no_window_manager was non-nil, but
3958 the ICCCM (section 4.1.6) says that the window's border pixmap
3959 and border pixel are window attributes which are "private to the
3960 client", so we can always change it to whatever we want. */
3961 BLOCK_INPUT;
334208b7 3962 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 3963 f->output_data.x->border_tile);
b3e1e05c 3964 UNBLOCK_INPUT;
5d46f928 3965 x_update_cursor (f, 1);
dc6f92b8 3966}
dc6f92b8 3967
f676886a
JB
3968/* The focus has changed. Update the frames as necessary to reflect
3969 the new situation. Note that we can't change the selected frame
c5acd733 3970 here, because the Lisp code we are interrupting might become confused.
eb8c3be9 3971 Each event gets marked with the frame in which it occurred, so the
c5acd733 3972 Lisp code can tell when the switch took place by examining the events. */
dc6f92b8 3973
6d4238f3 3974static void
0f941935
KH
3975x_new_focus_frame (dpyinfo, frame)
3976 struct x_display_info *dpyinfo;
f676886a 3977 struct frame *frame;
dc6f92b8 3978{
0f941935 3979 struct frame *old_focus = dpyinfo->x_focus_frame;
dc6f92b8 3980
0f941935 3981 if (frame != dpyinfo->x_focus_frame)
dc6f92b8 3982 {
58769bee 3983 /* Set this before calling other routines, so that they see
f676886a 3984 the correct value of x_focus_frame. */
0f941935 3985 dpyinfo->x_focus_frame = frame;
6d4238f3
JB
3986
3987 if (old_focus && old_focus->auto_lower)
f676886a 3988 x_lower_frame (old_focus);
dc6f92b8
JB
3989
3990#if 0
f676886a 3991 selected_frame = frame;
e0c1aef2
KH
3992 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
3993 selected_frame);
f676886a
JB
3994 Fselect_window (selected_frame->selected_window);
3995 choose_minibuf_frame ();
c118dd06 3996#endif /* ! 0 */
dc6f92b8 3997
0f941935
KH
3998 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3999 pending_autoraise_frame = dpyinfo->x_focus_frame;
0134a210
RS
4000 else
4001 pending_autoraise_frame = 0;
6d4238f3 4002 }
dc6f92b8 4003
0f941935 4004 x_frame_rehighlight (dpyinfo);
6d4238f3
JB
4005}
4006
cab34f50
JD
4007/* Handle FocusIn and FocusOut state changes for FRAME.
4008 If FRAME has focus and there exists more than one frame, puts
7d0393cf 4009 a FOCUS_IN_EVENT into BUFP.
cab34f50
JD
4010 Returns number of events inserted into BUFP. */
4011
4012static int
4013x_focus_changed (type, state, dpyinfo, frame, bufp, numchars)
4014 int type;
4015 int state;
4016 struct x_display_info *dpyinfo;
4017 struct frame *frame;
4018 struct input_event *bufp;
4019 int numchars;
4020{
4021 int nr_events = 0;
4022
4023 if (type == FocusIn)
4024 {
4025 if (dpyinfo->x_focus_event_frame != frame)
4026 {
4027 x_new_focus_frame (dpyinfo, frame);
4028 dpyinfo->x_focus_event_frame = frame;
7d0393cf 4029
cab34f50
JD
4030 /* Don't stop displaying the initial startup message
4031 for a switch-frame event we don't need. */
4032 if (numchars > 0
4033 && GC_NILP (Vterminal_frame)
4034 && GC_CONSP (Vframe_list)
4035 && !GC_NILP (XCDR (Vframe_list)))
4036 {
4037 bufp->kind = FOCUS_IN_EVENT;
4038 XSETFRAME (bufp->frame_or_window, frame);
4039 bufp->arg = Qnil;
4040 ++bufp;
4041 numchars--;
4042 ++nr_events;
4043 }
4044 }
4045
4046 frame->output_data.x->focus_state |= state;
4047
4048#ifdef HAVE_X_I18N
4049 if (FRAME_XIC (frame))
4050 XSetICFocus (FRAME_XIC (frame));
4051#endif
4052 }
4053 else if (type == FocusOut)
4054 {
4055 frame->output_data.x->focus_state &= ~state;
7d0393cf 4056
cab34f50
JD
4057 if (dpyinfo->x_focus_event_frame == frame)
4058 {
4059 dpyinfo->x_focus_event_frame = 0;
4060 x_new_focus_frame (dpyinfo, 0);
4061 }
4062
4063#ifdef HAVE_X_I18N
4064 if (FRAME_XIC (frame))
4065 XUnsetICFocus (FRAME_XIC (frame));
4066#endif
4067 }
4068
4069 return nr_events;
4070}
4071
4072/* The focus may have changed. Figure out if it is a real focus change,
4073 by checking both FocusIn/Out and Enter/LeaveNotify events.
4074
4075 Returns number of events inserted into BUFP. */
4076
4077static int
4078x_detect_focus_change (dpyinfo, event, bufp, numchars)
4079 struct x_display_info *dpyinfo;
4080 XEvent *event;
4081 struct input_event *bufp;
4082 int numchars;
4083{
4084 struct frame *frame;
4085 int nr_events = 0;
7d0393cf 4086
9d00001f 4087 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
cab34f50 4088 if (! frame) return nr_events;
7d0393cf 4089
cab34f50
JD
4090 switch (event->type)
4091 {
4092 case EnterNotify:
4093 case LeaveNotify:
4094 if (event->xcrossing.detail != NotifyInferior
4095 && event->xcrossing.focus
4096 && ! (frame->output_data.x->focus_state & FOCUS_EXPLICIT))
4097 nr_events = x_focus_changed ((event->type == EnterNotify
4098 ? FocusIn : FocusOut),
4099 FOCUS_IMPLICIT,
4100 dpyinfo,
4101 frame,
4102 bufp,
4103 numchars);
4104 break;
4105
4106 case FocusIn:
4107 case FocusOut:
4108 nr_events = x_focus_changed (event->type,
4109 (event->xfocus.detail == NotifyPointer
4110 ? FOCUS_IMPLICIT : FOCUS_EXPLICIT),
4111 dpyinfo,
4112 frame,
4113 bufp,
4114 numchars);
4115 break;
4116 }
4117
4118 return nr_events;
4119}
4120
4121
37c2c98b
RS
4122/* Handle an event saying the mouse has moved out of an Emacs frame. */
4123
4124void
0f941935
KH
4125x_mouse_leave (dpyinfo)
4126 struct x_display_info *dpyinfo;
37c2c98b 4127{
0f941935 4128 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
37c2c98b 4129}
6d4238f3 4130
f451eb13
JB
4131/* The focus has changed, or we have redirected a frame's focus to
4132 another frame (this happens when a frame uses a surrogate
06a2c219 4133 mini-buffer frame). Shift the highlight as appropriate.
0f941935
KH
4134
4135 The FRAME argument doesn't necessarily have anything to do with which
06a2c219 4136 frame is being highlighted or un-highlighted; we only use it to find
0f941935 4137 the appropriate X display info. */
06a2c219 4138
6d4238f3 4139static void
0f941935
KH
4140XTframe_rehighlight (frame)
4141 struct frame *frame;
6d4238f3 4142{
0f941935
KH
4143 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
4144}
6d4238f3 4145
0f941935
KH
4146static void
4147x_frame_rehighlight (dpyinfo)
4148 struct x_display_info *dpyinfo;
4149{
4150 struct frame *old_highlight = dpyinfo->x_highlight_frame;
4151
4152 if (dpyinfo->x_focus_frame)
6d4238f3 4153 {
0f941935
KH
4154 dpyinfo->x_highlight_frame
4155 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
4156 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
4157 : dpyinfo->x_focus_frame);
4158 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
f451eb13 4159 {
0f941935
KH
4160 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
4161 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
f451eb13 4162 }
dc6f92b8 4163 }
6d4238f3 4164 else
0f941935 4165 dpyinfo->x_highlight_frame = 0;
dc6f92b8 4166
0f941935 4167 if (dpyinfo->x_highlight_frame != old_highlight)
6d4238f3
JB
4168 {
4169 if (old_highlight)
f676886a 4170 frame_unhighlight (old_highlight);
0f941935
KH
4171 if (dpyinfo->x_highlight_frame)
4172 frame_highlight (dpyinfo->x_highlight_frame);
6d4238f3 4173 }
dc6f92b8 4174}
06a2c219
GM
4175
4176
dc6f92b8 4177\f
06a2c219 4178/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
dc6f92b8 4179
28430d3c
JB
4180/* Initialize mode_switch_bit and modifier_meaning. */
4181static void
334208b7
RS
4182x_find_modifier_meanings (dpyinfo)
4183 struct x_display_info *dpyinfo;
28430d3c 4184{
f689eb05 4185 int min_code, max_code;
28430d3c
JB
4186 KeySym *syms;
4187 int syms_per_code;
4188 XModifierKeymap *mods;
4189
334208b7
RS
4190 dpyinfo->meta_mod_mask = 0;
4191 dpyinfo->shift_lock_mask = 0;
4192 dpyinfo->alt_mod_mask = 0;
4193 dpyinfo->super_mod_mask = 0;
4194 dpyinfo->hyper_mod_mask = 0;
58769bee 4195
9658a521 4196#ifdef HAVE_X11R4
334208b7 4197 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
9658a521 4198#else
4a60f8c5
RS
4199 min_code = dpyinfo->display->min_keycode;
4200 max_code = dpyinfo->display->max_keycode;
9658a521
JB
4201#endif
4202
334208b7 4203 syms = XGetKeyboardMapping (dpyinfo->display,
28430d3c
JB
4204 min_code, max_code - min_code + 1,
4205 &syms_per_code);
334208b7 4206 mods = XGetModifierMapping (dpyinfo->display);
28430d3c 4207
58769bee 4208 /* Scan the modifier table to see which modifier bits the Meta and
11edeb03 4209 Alt keysyms are on. */
28430d3c 4210 {
06a2c219 4211 int row, col; /* The row and column in the modifier table. */
28430d3c
JB
4212
4213 for (row = 3; row < 8; row++)
4214 for (col = 0; col < mods->max_keypermod; col++)
4215 {
0299d313
RS
4216 KeyCode code
4217 = mods->modifiermap[(row * mods->max_keypermod) + col];
28430d3c 4218
af92970c
KH
4219 /* Zeroes are used for filler. Skip them. */
4220 if (code == 0)
4221 continue;
4222
28430d3c
JB
4223 /* Are any of this keycode's keysyms a meta key? */
4224 {
4225 int code_col;
4226
4227 for (code_col = 0; code_col < syms_per_code; code_col++)
4228 {
f689eb05 4229 int sym = syms[((code - min_code) * syms_per_code) + code_col];
28430d3c 4230
f689eb05 4231 switch (sym)
28430d3c 4232 {
f689eb05
JB
4233 case XK_Meta_L:
4234 case XK_Meta_R:
334208b7 4235 dpyinfo->meta_mod_mask |= (1 << row);
28430d3c 4236 break;
f689eb05
JB
4237
4238 case XK_Alt_L:
4239 case XK_Alt_R:
334208b7 4240 dpyinfo->alt_mod_mask |= (1 << row);
a3c44b14
RS
4241 break;
4242
4243 case XK_Hyper_L:
4244 case XK_Hyper_R:
334208b7 4245 dpyinfo->hyper_mod_mask |= (1 << row);
a3c44b14
RS
4246 break;
4247
4248 case XK_Super_L:
4249 case XK_Super_R:
334208b7 4250 dpyinfo->super_mod_mask |= (1 << row);
f689eb05 4251 break;
11edeb03
JB
4252
4253 case XK_Shift_Lock:
4254 /* Ignore this if it's not on the lock modifier. */
4255 if ((1 << row) == LockMask)
334208b7 4256 dpyinfo->shift_lock_mask = LockMask;
11edeb03 4257 break;
28430d3c
JB
4258 }
4259 }
4260 }
4261 }
4262 }
4263
f689eb05 4264 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
334208b7 4265 if (! dpyinfo->meta_mod_mask)
a3c44b14 4266 {
334208b7
RS
4267 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
4268 dpyinfo->alt_mod_mask = 0;
a3c44b14 4269 }
f689eb05 4270
148c4b70
RS
4271 /* If some keys are both alt and meta,
4272 make them just meta, not alt. */
334208b7 4273 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
148c4b70 4274 {
334208b7 4275 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
148c4b70 4276 }
58769bee 4277
28430d3c 4278 XFree ((char *) syms);
f689eb05 4279 XFreeModifiermap (mods);
28430d3c
JB
4280}
4281
dfeccd2d
JB
4282/* Convert between the modifier bits X uses and the modifier bits
4283 Emacs uses. */
06a2c219 4284
7c5283e4 4285static unsigned int
334208b7
RS
4286x_x_to_emacs_modifiers (dpyinfo, state)
4287 struct x_display_info *dpyinfo;
dc6f92b8
JB
4288 unsigned int state;
4289{
98659da6
KG
4290 EMACS_UINT mod_meta = meta_modifier;
4291 EMACS_UINT mod_alt = alt_modifier;
4292 EMACS_UINT mod_hyper = hyper_modifier;
4293 EMACS_UINT mod_super = super_modifier;
4294 Lisp_Object tem;
7d0393cf 4295
98659da6
KG
4296 tem = Fget (Vx_alt_keysym, Qmodifier_value);
4297 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
4298 tem = Fget (Vx_meta_keysym, Qmodifier_value);
4299 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
4300 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
4301 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
4302 tem = Fget (Vx_super_keysym, Qmodifier_value);
4303 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
7d0393cf 4304
98659da6 4305
334208b7 4306 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
98659da6
KG
4307 | ((state & ControlMask) ? ctrl_modifier : 0)
4308 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
4309 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
4310 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
4311 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
dc6f92b8
JB
4312}
4313
dfeccd2d 4314static unsigned int
334208b7
RS
4315x_emacs_to_x_modifiers (dpyinfo, state)
4316 struct x_display_info *dpyinfo;
dfeccd2d
JB
4317 unsigned int state;
4318{
98659da6
KG
4319 EMACS_UINT mod_meta = meta_modifier;
4320 EMACS_UINT mod_alt = alt_modifier;
4321 EMACS_UINT mod_hyper = hyper_modifier;
4322 EMACS_UINT mod_super = super_modifier;
7d0393cf 4323
98659da6 4324 Lisp_Object tem;
7d0393cf 4325
98659da6
KG
4326 tem = Fget (Vx_alt_keysym, Qmodifier_value);
4327 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
4328 tem = Fget (Vx_meta_keysym, Qmodifier_value);
4329 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
4330 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
4331 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
4332 tem = Fget (Vx_super_keysym, Qmodifier_value);
4333 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
7d0393cf
JB
4334
4335
98659da6
KG
4336 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
4337 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
4338 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
4339 | ((state & shift_modifier) ? ShiftMask : 0)
4340 | ((state & ctrl_modifier) ? ControlMask : 0)
4341 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
dfeccd2d 4342}
d047c4eb
KH
4343
4344/* Convert a keysym to its name. */
4345
4346char *
4347x_get_keysym_name (keysym)
4348 KeySym keysym;
4349{
4350 char *value;
4351
4352 BLOCK_INPUT;
4353 value = XKeysymToString (keysym);
4354 UNBLOCK_INPUT;
4355
4356 return value;
4357}
06a2c219
GM
4358
4359
e4571a43
JB
4360\f
4361/* Mouse clicks and mouse movement. Rah. */
e4571a43 4362
06a2c219
GM
4363/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
4364 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
4365 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
4366 not force the value into range. */
69388238 4367
c8dba240 4368void
69388238 4369pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
e4571a43 4370 FRAME_PTR f;
69388238 4371 register int pix_x, pix_y;
e4571a43
JB
4372 register int *x, *y;
4373 XRectangle *bounds;
69388238 4374 int noclip;
e4571a43 4375{
06a2c219 4376 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
69388238
RS
4377 even for negative values. */
4378 if (pix_x < 0)
7556890b 4379 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
69388238 4380 if (pix_y < 0)
7556890b 4381 pix_y -= (f)->output_data.x->line_height - 1;
69388238 4382
e4571a43
JB
4383 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
4384 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
4385
4386 if (bounds)
4387 {
7556890b
RS
4388 bounds->width = FONT_WIDTH (f->output_data.x->font);
4389 bounds->height = f->output_data.x->line_height;
e4571a43
JB
4390 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
4391 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
4392 }
4393
69388238
RS
4394 if (!noclip)
4395 {
4396 if (pix_x < 0)
4397 pix_x = 0;
3cbd2e0b
RS
4398 else if (pix_x > FRAME_WINDOW_WIDTH (f))
4399 pix_x = FRAME_WINDOW_WIDTH (f);
69388238
RS
4400
4401 if (pix_y < 0)
4402 pix_y = 0;
4403 else if (pix_y > f->height)
4404 pix_y = f->height;
4405 }
e4571a43
JB
4406
4407 *x = pix_x;
4408 *y = pix_y;
4409}
4410
06a2c219
GM
4411
4412/* Given HPOS/VPOS in the current matrix of W, return corresponding
4413 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
4414 can't tell the positions because W's display is not up to date,
4415 return 0. */
4416
4417int
4418glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
4419 struct window *w;
4420 int hpos, vpos;
4421 int *frame_x, *frame_y;
2b5c9e71 4422{
06a2c219
GM
4423 int success_p;
4424
4425 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
4426 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
4427
4428 if (display_completed)
4429 {
4430 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
4431 struct glyph *glyph = row->glyphs[TEXT_AREA];
4432 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
4433
4434 *frame_y = row->y;
4435 *frame_x = row->x;
4436 while (glyph < end)
4437 {
4438 *frame_x += glyph->pixel_width;
4439 ++glyph;
4440 }
4441
4442 success_p = 1;
4443 }
4444 else
4445 {
4446 *frame_y = *frame_x = 0;
4447 success_p = 0;
4448 }
4449
4450 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
4451 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
4452 return success_p;
2b5c9e71
RS
4453}
4454
06a2c219 4455
dc6f92b8
JB
4456/* Prepare a mouse-event in *RESULT for placement in the input queue.
4457
4458 If the event is a button press, then note that we have grabbed
f451eb13 4459 the mouse. */
dc6f92b8
JB
4460
4461static Lisp_Object
f451eb13 4462construct_mouse_click (result, event, f)
dc6f92b8
JB
4463 struct input_event *result;
4464 XButtonEvent *event;
f676886a 4465 struct frame *f;
dc6f92b8 4466{
3b8f9651 4467 /* Make the event type NO_EVENT; we'll change that when we decide
dc6f92b8 4468 otherwise. */
3b8f9651 4469 result->kind = MOUSE_CLICK_EVENT;
69388238 4470 result->code = event->button - Button1;
1113d9db 4471 result->timestamp = event->time;
334208b7
RS
4472 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
4473 event->state)
f689eb05 4474 | (event->type == ButtonRelease
58769bee 4475 ? up_modifier
f689eb05 4476 : down_modifier));
dc6f92b8 4477
06a2c219
GM
4478 XSETINT (result->x, event->x);
4479 XSETINT (result->y, event->y);
4480 XSETFRAME (result->frame_or_window, f);
0f8aabe9 4481 result->arg = Qnil;
06a2c219 4482 return Qnil;
dc6f92b8 4483}
b849c413 4484
69388238 4485\f
90e65f07
JB
4486/* Function to report a mouse movement to the mainstream Emacs code.
4487 The input handler calls this.
4488
4489 We have received a mouse movement event, which is given in *event.
4490 If the mouse is over a different glyph than it was last time, tell
4491 the mainstream emacs code by setting mouse_moved. If not, ask for
4492 another motion event, so we can check again the next time it moves. */
b8009dd1 4493
06a2c219
GM
4494static XMotionEvent last_mouse_motion_event;
4495static Lisp_Object last_mouse_motion_frame;
4496
90e65f07 4497static void
12ba150f 4498note_mouse_movement (frame, event)
f676886a 4499 FRAME_PTR frame;
90e65f07 4500 XMotionEvent *event;
90e65f07 4501{
e5d77022 4502 last_mouse_movement_time = event->time;
06a2c219
GM
4503 last_mouse_motion_event = *event;
4504 XSETFRAME (last_mouse_motion_frame, frame);
e5d77022 4505
27f338af
RS
4506 if (event->window != FRAME_X_WINDOW (frame))
4507 {
39d8bb4d 4508 frame->mouse_moved = 1;
27f338af 4509 last_mouse_scroll_bar = Qnil;
27f338af 4510 note_mouse_highlight (frame, -1, -1);
27f338af
RS
4511 }
4512
90e65f07 4513 /* Has the mouse moved off the glyph it was on at the last sighting? */
27f338af
RS
4514 else if (event->x < last_mouse_glyph.x
4515 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
4516 || event->y < last_mouse_glyph.y
4517 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
12ba150f 4518 {
39d8bb4d 4519 frame->mouse_moved = 1;
ab648270 4520 last_mouse_scroll_bar = Qnil;
b8009dd1 4521 note_mouse_highlight (frame, event->x, event->y);
90e65f07
JB
4522 }
4523}
4524
06a2c219
GM
4525\f
4526/************************************************************************
4527 Mouse Face
4528 ************************************************************************/
4529
4530/* Find the glyph under window-relative coordinates X/Y in window W.
4531 Consider only glyphs from buffer text, i.e. no glyphs from overlay
4532 strings. Return in *HPOS and *VPOS the row and column number of
4533 the glyph found. Return in *AREA the glyph area containing X.
4534 Value is a pointer to the glyph found or null if X/Y is not on
4535 text, or we can't tell because W's current matrix is not up to
4536 date. */
4537
4538static struct glyph *
f9db2310 4539x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
06a2c219
GM
4540 struct window *w;
4541 int x, y;
4542 int *hpos, *vpos, *area;
f9db2310 4543 int buffer_only_p;
06a2c219
GM
4544{
4545 struct glyph *glyph, *end;
3e71d8f2 4546 struct glyph_row *row = NULL;
06a2c219
GM
4547 int x0, i, left_area_width;
4548
4549 /* Find row containing Y. Give up if some row is not enabled. */
4550 for (i = 0; i < w->current_matrix->nrows; ++i)
4551 {
4552 row = MATRIX_ROW (w->current_matrix, i);
4553 if (!row->enabled_p)
4554 return NULL;
4555 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
4556 break;
4557 }
4558
4559 *vpos = i;
4560 *hpos = 0;
4561
4562 /* Give up if Y is not in the window. */
4563 if (i == w->current_matrix->nrows)
4564 return NULL;
4565
4566 /* Get the glyph area containing X. */
4567 if (w->pseudo_window_p)
4568 {
4569 *area = TEXT_AREA;
4570 x0 = 0;
4571 }
4572 else
4573 {
4574 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
4575 if (x < left_area_width)
4576 {
4577 *area = LEFT_MARGIN_AREA;
4578 x0 = 0;
4579 }
4580 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
4581 {
4582 *area = TEXT_AREA;
4583 x0 = row->x + left_area_width;
4584 }
4585 else
4586 {
4587 *area = RIGHT_MARGIN_AREA;
4588 x0 = left_area_width + window_box_width (w, TEXT_AREA);
4589 }
4590 }
4591
4592 /* Find glyph containing X. */
4593 glyph = row->glyphs[*area];
4594 end = glyph + row->used[*area];
4595 while (glyph < end)
4596 {
4597 if (x < x0 + glyph->pixel_width)
4598 {
4599 if (w->pseudo_window_p)
4600 break;
f9db2310 4601 else if (!buffer_only_p || BUFFERP (glyph->object))
06a2c219
GM
4602 break;
4603 }
7d0393cf 4604
06a2c219
GM
4605 x0 += glyph->pixel_width;
4606 ++glyph;
4607 }
4608
4609 if (glyph == end)
4610 return NULL;
4611
4612 *hpos = glyph - row->glyphs[*area];
4613 return glyph;
4614}
4615
4616
4617/* Convert frame-relative x/y to coordinates relative to window W.
4618 Takes pseudo-windows into account. */
4619
4620static void
4621frame_to_window_pixel_xy (w, x, y)
4622 struct window *w;
4623 int *x, *y;
4624{
4625 if (w->pseudo_window_p)
4626 {
4627 /* A pseudo-window is always full-width, and starts at the
4628 left edge of the frame, plus a frame border. */
4629 struct frame *f = XFRAME (w->frame);
4630 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
4631 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
4632 }
4633 else
4634 {
4635 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
4636 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
4637 }
4638}
4639
4640
b436feb6
GM
4641/* Take proper action when mouse has moved to the mode or header line
4642 or marginal area of window W, x-position X and y-position Y. Area
4643 is 1, 3, 6 or 7 for the mode line, header line, left and right
4644 marginal area respectively. X is relative to the start of the text
4645 display area of W, so the width of bitmap areas and scroll bars
4646 must be subtracted to get a position relative to the start of the
4647 mode line. */
7d0393cf 4648
06a2c219 4649static void
b436feb6 4650note_mode_line_or_margin_highlight (w, x, y, portion)
06a2c219 4651 struct window *w;
b436feb6 4652 int x, y, portion;
06a2c219
GM
4653{
4654 struct frame *f = XFRAME (w->frame);
4655 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
4656 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
2a793c0c 4657 int charpos;
b436feb6 4658 Lisp_Object string, help, map, pos;
06a2c219 4659
b436feb6
GM
4660 if (portion == 1 || portion == 3)
4661 string = mode_line_string (w, x, y, portion == 1, &charpos);
4662 else
4663 string = marginal_area_string (w, x, y, portion, &charpos);
e371a781 4664
b436feb6 4665 if (STRINGP (string))
06a2c219 4666 {
b436feb6 4667 pos = make_number (charpos);
7d0393cf 4668
b436feb6
GM
4669 /* If we're on a string with `help-echo' text property, arrange
4670 for the help to be displayed. This is done by setting the
4671 global variable help_echo to the help string. */
4672 help = Fget_text_property (pos, Qhelp_echo, string);
4673 if (!NILP (help))
06a2c219 4674 {
b436feb6
GM
4675 help_echo = help;
4676 XSETWINDOW (help_echo_window, w);
4677 help_echo_object = string;
4678 help_echo_pos = charpos;
06a2c219
GM
4679 }
4680
b436feb6
GM
4681 /* Change the mouse pointer according to what is under X/Y. */
4682 map = Fget_text_property (pos, Qlocal_map, string);
4683 if (!KEYMAPP (map))
4684 map = Fget_text_property (pos, Qkeymap, string);
4685 if (KEYMAPP (map))
4686 cursor = f->output_data.x->nontext_cursor;
06a2c219 4687 }
7d0393cf 4688
06a2c219
GM
4689 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
4690}
4691
4692
4693/* Take proper action when the mouse has moved to position X, Y on
4694 frame F as regards highlighting characters that have mouse-face
4695 properties. Also de-highlighting chars where the mouse was before.
27f338af 4696 X and Y can be negative or out of range. */
b8009dd1
RS
4697
4698static void
4699note_mouse_highlight (f, x, y)
06a2c219 4700 struct frame *f;
c32cdd9a 4701 int x, y;
b8009dd1 4702{
06a2c219
GM
4703 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
4704 int portion;
b8009dd1
RS
4705 Lisp_Object window;
4706 struct window *w;
0d487c52
GM
4707 Cursor cursor = None;
4708 struct buffer *b;
b8009dd1 4709
06a2c219 4710 /* When a menu is active, don't highlight because this looks odd. */
488dd4c4 4711#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
06a2c219
GM
4712 if (popup_activated ())
4713 return;
4714#endif
4715
66301061 4716 if (NILP (Vmouse_highlight)
04fff9c0 4717 || !f->glyphs_initialized_p)
bf1c0ba1
RS
4718 return;
4719
06a2c219
GM
4720 dpyinfo->mouse_face_mouse_x = x;
4721 dpyinfo->mouse_face_mouse_y = y;
4722 dpyinfo->mouse_face_mouse_frame = f;
b8009dd1 4723
06a2c219 4724 if (dpyinfo->mouse_face_defer)
b8009dd1
RS
4725 return;
4726
514e4681
RS
4727 if (gc_in_progress)
4728 {
06a2c219 4729 dpyinfo->mouse_face_deferred_gc = 1;
514e4681
RS
4730 return;
4731 }
4732
b8009dd1 4733 /* Which window is that in? */
06a2c219 4734 window = window_from_coordinates (f, x, y, &portion, 1);
b8009dd1
RS
4735
4736 /* If we were displaying active text in another window, clear that. */
06a2c219
GM
4737 if (! EQ (window, dpyinfo->mouse_face_window))
4738 clear_mouse_face (dpyinfo);
4739
4740 /* Not on a window -> return. */
4741 if (!WINDOWP (window))
4742 return;
4743
4744 /* Convert to window-relative pixel coordinates. */
4745 w = XWINDOW (window);
4746 frame_to_window_pixel_xy (w, &x, &y);
4747
9ea173e8 4748 /* Handle tool-bar window differently since it doesn't display a
06a2c219 4749 buffer. */
9ea173e8 4750 if (EQ (window, f->tool_bar_window))
06a2c219 4751 {
9ea173e8 4752 note_tool_bar_highlight (f, x, y);
06a2c219
GM
4753 return;
4754 }
4755
b436feb6
GM
4756 /* Mouse is on the mode, header line or margin? */
4757 if (portion == 1 || portion == 3 || portion == 6 || portion == 7)
06a2c219 4758 {
b436feb6 4759 note_mode_line_or_margin_highlight (w, x, y, portion);
06a2c219
GM
4760 return;
4761 }
7d0393cf 4762
0d487c52
GM
4763 if (portion == 2)
4764 cursor = f->output_data.x->horizontal_drag_cursor;
06a2c219 4765 else
0d487c52 4766 cursor = f->output_data.x->text_cursor;
b8009dd1 4767
0cdd0c9f
RS
4768 /* Are we in a window whose display is up to date?
4769 And verify the buffer's text has not changed. */
0d487c52 4770 b = XBUFFER (w->buffer);
06a2c219
GM
4771 if (/* Within text portion of the window. */
4772 portion == 0
0cdd0c9f 4773 && EQ (w->window_end_valid, w->buffer)
0d487c52
GM
4774 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
4775 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
b8009dd1 4776 {
06a2c219
GM
4777 int hpos, vpos, pos, i, area;
4778 struct glyph *glyph;
f9db2310 4779 Lisp_Object object;
0d487c52
GM
4780 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
4781 Lisp_Object *overlay_vec = NULL;
4782 int len, noverlays;
4783 struct buffer *obuf;
4784 int obegv, ozv, same_region;
b8009dd1 4785
06a2c219 4786 /* Find the glyph under X/Y. */
f9db2310 4787 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
06a2c219
GM
4788
4789 /* Clear mouse face if X/Y not over text. */
4790 if (glyph == NULL
4791 || area != TEXT_AREA
4792 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
b8009dd1 4793 {
fa262c07
GM
4794 if (clear_mouse_face (dpyinfo))
4795 cursor = None;
4796 goto set_cursor;
06a2c219
GM
4797 }
4798
4799 pos = glyph->charpos;
f9db2310
GM
4800 object = glyph->object;
4801 if (!STRINGP (object) && !BUFFERP (object))
fa262c07 4802 goto set_cursor;
06a2c219 4803
0d487c52
GM
4804 /* If we get an out-of-range value, return now; avoid an error. */
4805 if (BUFFERP (object) && pos > BUF_Z (b))
fa262c07 4806 goto set_cursor;
06a2c219 4807
0d487c52
GM
4808 /* Make the window's buffer temporarily current for
4809 overlays_at and compute_char_face. */
4810 obuf = current_buffer;
4811 current_buffer = b;
4812 obegv = BEGV;
4813 ozv = ZV;
4814 BEGV = BEG;
4815 ZV = Z;
06a2c219 4816
0d487c52
GM
4817 /* Is this char mouse-active or does it have help-echo? */
4818 position = make_number (pos);
f9db2310 4819
0d487c52
GM
4820 if (BUFFERP (object))
4821 {
4822 /* Put all the overlays we want in a vector in overlay_vec.
4823 Store the length in len. If there are more than 10, make
4824 enough space for all, and try again. */
4825 len = 10;
4826 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
4827 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
4828 if (noverlays > len)
4829 {
4830 len = noverlays;
4831 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
4832 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
4833 }
f8349001 4834
0d487c52
GM
4835 /* Sort overlays into increasing priority order. */
4836 noverlays = sort_overlays (overlay_vec, noverlays, w);
4837 }
4838 else
4839 noverlays = 0;
4840
4841 same_region = (EQ (window, dpyinfo->mouse_face_window)
4842 && vpos >= dpyinfo->mouse_face_beg_row
4843 && vpos <= dpyinfo->mouse_face_end_row
4844 && (vpos > dpyinfo->mouse_face_beg_row
4845 || hpos >= dpyinfo->mouse_face_beg_col)
4846 && (vpos < dpyinfo->mouse_face_end_row
4847 || hpos < dpyinfo->mouse_face_end_col
4848 || dpyinfo->mouse_face_past_end));
4849
4850 if (same_region)
4851 cursor = None;
4852
4853 /* Check mouse-face highlighting. */
4854 if (! same_region
4855 /* If there exists an overlay with mouse-face overlapping
4856 the one we are currently highlighting, we have to
4857 check if we enter the overlapping overlay, and then
4858 highlight only that. */
4859 || (OVERLAYP (dpyinfo->mouse_face_overlay)
4860 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
4861 {
0d487c52
GM
4862 /* Find the highest priority overlay that has a mouse-face
4863 property. */
4864 overlay = Qnil;
4865 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
4866 {
4867 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
4868 if (!NILP (mouse_face))
4869 overlay = overlay_vec[i];
4870 }
7d0393cf 4871
8bd189fb
GM
4872 /* If we're actually highlighting the same overlay as
4873 before, there's no need to do that again. */
4874 if (!NILP (overlay)
4875 && EQ (overlay, dpyinfo->mouse_face_overlay))
4876 goto check_help_echo;
7d0393cf 4877
8bd189fb
GM
4878 dpyinfo->mouse_face_overlay = overlay;
4879
4880 /* Clear the display of the old active region, if any. */
4881 if (clear_mouse_face (dpyinfo))
4882 cursor = None;
4883
0d487c52
GM
4884 /* If no overlay applies, get a text property. */
4885 if (NILP (overlay))
4886 mouse_face = Fget_text_property (position, Qmouse_face, object);
06a2c219 4887
0d487c52
GM
4888 /* Handle the overlay case. */
4889 if (!NILP (overlay))
4890 {
4891 /* Find the range of text around this char that
4892 should be active. */
4893 Lisp_Object before, after;
4894 int ignore;
4895
4896 before = Foverlay_start (overlay);
4897 after = Foverlay_end (overlay);
4898 /* Record this as the current active region. */
4899 fast_find_position (w, XFASTINT (before),
4900 &dpyinfo->mouse_face_beg_col,
4901 &dpyinfo->mouse_face_beg_row,
4902 &dpyinfo->mouse_face_beg_x,
7e376260 4903 &dpyinfo->mouse_face_beg_y, Qnil);
7d0393cf 4904
0d487c52
GM
4905 dpyinfo->mouse_face_past_end
4906 = !fast_find_position (w, XFASTINT (after),
4907 &dpyinfo->mouse_face_end_col,
4908 &dpyinfo->mouse_face_end_row,
4909 &dpyinfo->mouse_face_end_x,
7e376260 4910 &dpyinfo->mouse_face_end_y, Qnil);
0d487c52
GM
4911 dpyinfo->mouse_face_window = window;
4912 dpyinfo->mouse_face_face_id
4913 = face_at_buffer_position (w, pos, 0, 0,
1b7334a4
KS
4914 &ignore, pos + 1,
4915 !dpyinfo->mouse_face_hidden);
0d487c52
GM
4916
4917 /* Display it as active. */
4918 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
58e5af83 4919 cursor = None;
0d487c52
GM
4920 }
4921 /* Handle the text property case. */
4922 else if (!NILP (mouse_face) && BUFFERP (object))
4923 {
4924 /* Find the range of text around this char that
4925 should be active. */
4926 Lisp_Object before, after, beginning, end;
4927 int ignore;
4928
4929 beginning = Fmarker_position (w->start);
4930 end = make_number (BUF_Z (XBUFFER (object))
4931 - XFASTINT (w->window_end_pos));
4932 before
4933 = Fprevious_single_property_change (make_number (pos + 1),
4934 Qmouse_face,
4935 object, beginning);
4936 after
4937 = Fnext_single_property_change (position, Qmouse_face,
4938 object, end);
7d0393cf 4939
0d487c52
GM
4940 /* Record this as the current active region. */
4941 fast_find_position (w, XFASTINT (before),
4942 &dpyinfo->mouse_face_beg_col,
4943 &dpyinfo->mouse_face_beg_row,
4944 &dpyinfo->mouse_face_beg_x,
7e376260 4945 &dpyinfo->mouse_face_beg_y, Qnil);
0d487c52
GM
4946 dpyinfo->mouse_face_past_end
4947 = !fast_find_position (w, XFASTINT (after),
4948 &dpyinfo->mouse_face_end_col,
4949 &dpyinfo->mouse_face_end_row,
4950 &dpyinfo->mouse_face_end_x,
7e376260 4951 &dpyinfo->mouse_face_end_y, Qnil);
0d487c52
GM
4952 dpyinfo->mouse_face_window = window;
4953
4954 if (BUFFERP (object))
06a2c219
GM
4955 dpyinfo->mouse_face_face_id
4956 = face_at_buffer_position (w, pos, 0, 0,
1b7334a4
KS
4957 &ignore, pos + 1,
4958 !dpyinfo->mouse_face_hidden);
06a2c219 4959
0d487c52
GM
4960 /* Display it as active. */
4961 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
58e5af83 4962 cursor = None;
0d487c52
GM
4963 }
4964 else if (!NILP (mouse_face) && STRINGP (object))
4965 {
4966 Lisp_Object b, e;
4967 int ignore;
7d0393cf 4968
0d487c52
GM
4969 b = Fprevious_single_property_change (make_number (pos + 1),
4970 Qmouse_face,
4971 object, Qnil);
4972 e = Fnext_single_property_change (position, Qmouse_face,
4973 object, Qnil);
4974 if (NILP (b))
4975 b = make_number (0);
4976 if (NILP (e))
d5db4077 4977 e = make_number (SCHARS (object) - 1);
0d487c52 4978 fast_find_string_pos (w, XINT (b), object,
06a2c219
GM
4979 &dpyinfo->mouse_face_beg_col,
4980 &dpyinfo->mouse_face_beg_row,
4981 &dpyinfo->mouse_face_beg_x,
0d487c52
GM
4982 &dpyinfo->mouse_face_beg_y, 0);
4983 fast_find_string_pos (w, XINT (e), object,
4984 &dpyinfo->mouse_face_end_col,
4985 &dpyinfo->mouse_face_end_row,
4986 &dpyinfo->mouse_face_end_x,
4987 &dpyinfo->mouse_face_end_y, 1);
4988 dpyinfo->mouse_face_past_end = 0;
4989 dpyinfo->mouse_face_window = window;
4990 dpyinfo->mouse_face_face_id
4991 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
4992 glyph->face_id, 1);
4993 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
58e5af83 4994 cursor = None;
0d487c52 4995 }
7e376260
GM
4996 else if (STRINGP (object) && NILP (mouse_face))
4997 {
4998 /* A string which doesn't have mouse-face, but
4999 the text ``under'' it might have. */
5000 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
5001 int start = MATRIX_ROW_START_CHARPOS (r);
7d0393cf 5002
7e376260
GM
5003 pos = string_buffer_position (w, object, start);
5004 if (pos > 0)
5005 mouse_face = get_char_property_and_overlay (make_number (pos),
5006 Qmouse_face,
5007 w->buffer,
5008 &overlay);
5009 if (!NILP (mouse_face) && !NILP (overlay))
5010 {
5011 Lisp_Object before = Foverlay_start (overlay);
5012 Lisp_Object after = Foverlay_end (overlay);
91c153e2 5013 int ignore;
7e376260
GM
5014
5015 /* Note that we might not be able to find position
5016 BEFORE in the glyph matrix if the overlay is
5017 entirely covered by a `display' property. In
5018 this case, we overshoot. So let's stop in
5019 the glyph matrix before glyphs for OBJECT. */
5020 fast_find_position (w, XFASTINT (before),
5021 &dpyinfo->mouse_face_beg_col,
5022 &dpyinfo->mouse_face_beg_row,
5023 &dpyinfo->mouse_face_beg_x,
5024 &dpyinfo->mouse_face_beg_y,
5025 object);
7d0393cf 5026
7e376260
GM
5027 dpyinfo->mouse_face_past_end
5028 = !fast_find_position (w, XFASTINT (after),
5029 &dpyinfo->mouse_face_end_col,
5030 &dpyinfo->mouse_face_end_row,
5031 &dpyinfo->mouse_face_end_x,
5032 &dpyinfo->mouse_face_end_y,
5033 Qnil);
5034 dpyinfo->mouse_face_window = window;
5035 dpyinfo->mouse_face_face_id
5036 = face_at_buffer_position (w, pos, 0, 0,
1b7334a4
KS
5037 &ignore, pos + 1,
5038 !dpyinfo->mouse_face_hidden);
7e376260
GM
5039
5040 /* Display it as active. */
5041 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
5042 cursor = None;
5043 }
5044 }
0d487c52 5045 }
06a2c219 5046
8bd189fb
GM
5047 check_help_echo:
5048
0d487c52
GM
5049 /* Look for a `help-echo' property. */
5050 {
5051 Lisp_Object help, overlay;
06a2c219 5052
0d487c52
GM
5053 /* Check overlays first. */
5054 help = overlay = Qnil;
5055 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
5056 {
5057 overlay = overlay_vec[i];
5058 help = Foverlay_get (overlay, Qhelp_echo);
5059 }
be010514 5060
0d487c52
GM
5061 if (!NILP (help))
5062 {
5063 help_echo = help;
5064 help_echo_window = window;
5065 help_echo_object = overlay;
5066 help_echo_pos = pos;
5067 }
5068 else
5069 {
5070 Lisp_Object object = glyph->object;
5071 int charpos = glyph->charpos;
7d0393cf 5072
0d487c52
GM
5073 /* Try text properties. */
5074 if (STRINGP (object)
5075 && charpos >= 0
d5db4077 5076 && charpos < SCHARS (object))
0d487c52
GM
5077 {
5078 help = Fget_text_property (make_number (charpos),
5079 Qhelp_echo, object);
5080 if (NILP (help))
5081 {
5082 /* If the string itself doesn't specify a help-echo,
5083 see if the buffer text ``under'' it does. */
5084 struct glyph_row *r
5085 = MATRIX_ROW (w->current_matrix, vpos);
5086 int start = MATRIX_ROW_START_CHARPOS (r);
5087 int pos = string_buffer_position (w, object, start);
5088 if (pos > 0)
5089 {
7e376260 5090 help = Fget_char_property (make_number (pos),
0d487c52
GM
5091 Qhelp_echo, w->buffer);
5092 if (!NILP (help))
5093 {
5094 charpos = pos;
5095 object = w->buffer;
5096 }
5097 }
5098 }
5099 }
5100 else if (BUFFERP (object)
5101 && charpos >= BEGV
5102 && charpos < ZV)
5103 help = Fget_text_property (make_number (charpos), Qhelp_echo,
5104 object);
7d0393cf 5105
0d487c52
GM
5106 if (!NILP (help))
5107 {
5108 help_echo = help;
5109 help_echo_window = window;
5110 help_echo_object = object;
5111 help_echo_pos = charpos;
5112 }
5113 }
06a2c219 5114 }
7d0393cf 5115
0d487c52
GM
5116 BEGV = obegv;
5117 ZV = ozv;
5118 current_buffer = obuf;
06a2c219 5119 }
0d487c52 5120
fa262c07 5121 set_cursor:
7d0393cf 5122
0d487c52
GM
5123 if (cursor != None)
5124 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
06a2c219
GM
5125}
5126
5127static void
5128redo_mouse_highlight ()
5129{
5130 if (!NILP (last_mouse_motion_frame)
5131 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
5132 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
5133 last_mouse_motion_event.x,
5134 last_mouse_motion_event.y);
5135}
5136
5137
5138\f
5139/***********************************************************************
9ea173e8 5140 Tool-bars
06a2c219
GM
5141 ***********************************************************************/
5142
9ea173e8
GM
5143static int x_tool_bar_item P_ ((struct frame *, int, int,
5144 struct glyph **, int *, int *, int *));
06a2c219 5145
9ea173e8 5146/* Tool-bar item index of the item on which a mouse button was pressed
06a2c219
GM
5147 or -1. */
5148
9ea173e8 5149static int last_tool_bar_item;
06a2c219
GM
5150
5151
9ea173e8
GM
5152/* Get information about the tool-bar item at position X/Y on frame F.
5153 Return in *GLYPH a pointer to the glyph of the tool-bar item in
5154 the current matrix of the tool-bar window of F, or NULL if not
5155 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8daf1204 5156 item in F->tool_bar_items. Value is
06a2c219 5157
9ea173e8 5158 -1 if X/Y is not on a tool-bar item
06a2c219
GM
5159 0 if X/Y is on the same item that was highlighted before.
5160 1 otherwise. */
5161
5162static int
9ea173e8 5163x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
06a2c219
GM
5164 struct frame *f;
5165 int x, y;
5166 struct glyph **glyph;
5167 int *hpos, *vpos, *prop_idx;
5168{
5169 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 5170 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
5171 int area;
5172
5173 /* Find the glyph under X/Y. */
f9db2310 5174 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
06a2c219
GM
5175 if (*glyph == NULL)
5176 return -1;
5177
9ea173e8 5178 /* Get the start of this tool-bar item's properties in
8daf1204 5179 f->tool_bar_items. */
9ea173e8 5180 if (!tool_bar_item_info (f, *glyph, prop_idx))
06a2c219
GM
5181 return -1;
5182
5183 /* Is mouse on the highlighted item? */
9ea173e8 5184 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
06a2c219
GM
5185 && *vpos >= dpyinfo->mouse_face_beg_row
5186 && *vpos <= dpyinfo->mouse_face_end_row
5187 && (*vpos > dpyinfo->mouse_face_beg_row
5188 || *hpos >= dpyinfo->mouse_face_beg_col)
5189 && (*vpos < dpyinfo->mouse_face_end_row
5190 || *hpos < dpyinfo->mouse_face_end_col
5191 || dpyinfo->mouse_face_past_end))
5192 return 0;
7d0393cf 5193
06a2c219
GM
5194 return 1;
5195}
5196
5197
9ea173e8 5198/* Handle mouse button event on the tool-bar of frame F, at
6c4a22e6 5199 frame-relative coordinates X/Y. EVENT_TYPE is either ButtonPress
06a2c219
GM
5200 or ButtonRelase. */
5201
5202static void
9ea173e8 5203x_handle_tool_bar_click (f, button_event)
06a2c219
GM
5204 struct frame *f;
5205 XButtonEvent *button_event;
5206{
5207 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9ea173e8 5208 struct window *w = XWINDOW (f->tool_bar_window);
06a2c219
GM
5209 int hpos, vpos, prop_idx;
5210 struct glyph *glyph;
5211 Lisp_Object enabled_p;
5212 int x = button_event->x;
5213 int y = button_event->y;
7d0393cf 5214
9ea173e8 5215 /* If not on the highlighted tool-bar item, return. */
06a2c219 5216 frame_to_window_pixel_xy (w, &x, &y);
9ea173e8 5217 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
06a2c219
GM
5218 return;
5219
5220 /* If item is disabled, do nothing. */
8daf1204 5221 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
06a2c219
GM
5222 if (NILP (enabled_p))
5223 return;
7d0393cf 5224
06a2c219
GM
5225 if (button_event->type == ButtonPress)
5226 {
5227 /* Show item in pressed state. */
5228 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
5229 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9ea173e8 5230 last_tool_bar_item = prop_idx;
06a2c219
GM
5231 }
5232 else
5233 {
5234 Lisp_Object key, frame;
5235 struct input_event event;
5236
5237 /* Show item in released state. */
5238 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
5239 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
5240
8daf1204 5241 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
06a2c219
GM
5242
5243 XSETFRAME (frame, f);
9ea173e8 5244 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
5245 event.frame_or_window = frame;
5246 event.arg = frame;
06a2c219
GM
5247 kbd_buffer_store_event (&event);
5248
9ea173e8 5249 event.kind = TOOL_BAR_EVENT;
0f1a9b23
GM
5250 event.frame_or_window = frame;
5251 event.arg = key;
06a2c219
GM
5252 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
5253 button_event->state);
5254 kbd_buffer_store_event (&event);
9ea173e8 5255 last_tool_bar_item = -1;
06a2c219
GM
5256 }
5257}
5258
5259
9ea173e8
GM
5260/* Possibly highlight a tool-bar item on frame F when mouse moves to
5261 tool-bar window-relative coordinates X/Y. Called from
06a2c219
GM
5262 note_mouse_highlight. */
5263
5264static void
9ea173e8 5265note_tool_bar_highlight (f, x, y)
06a2c219
GM
5266 struct frame *f;
5267 int x, y;
5268{
9ea173e8 5269 Lisp_Object window = f->tool_bar_window;
06a2c219
GM
5270 struct window *w = XWINDOW (window);
5271 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
5272 int hpos, vpos;
5273 struct glyph *glyph;
5274 struct glyph_row *row;
5c187dee 5275 int i;
06a2c219
GM
5276 Lisp_Object enabled_p;
5277 int prop_idx;
140330de 5278 enum draw_glyphs_face draw;
5c187dee 5279 int mouse_down_p, rc;
06a2c219
GM
5280
5281 /* Function note_mouse_highlight is called with negative x(y
5282 values when mouse moves outside of the frame. */
5283 if (x <= 0 || y <= 0)
5284 {
5285 clear_mouse_face (dpyinfo);
5286 return;
5287 }
5288
9ea173e8 5289 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
06a2c219
GM
5290 if (rc < 0)
5291 {
9ea173e8 5292 /* Not on tool-bar item. */
06a2c219
GM
5293 clear_mouse_face (dpyinfo);
5294 return;
5295 }
5296 else if (rc == 0)
06a2c219 5297 goto set_help_echo;
b8009dd1 5298
06a2c219 5299 clear_mouse_face (dpyinfo);
7d0393cf 5300
9ea173e8 5301 /* Mouse is down, but on different tool-bar item? */
06a2c219
GM
5302 mouse_down_p = (dpyinfo->grabbed
5303 && f == last_mouse_frame
5304 && FRAME_LIVE_P (f));
5305 if (mouse_down_p
9ea173e8 5306 && last_tool_bar_item != prop_idx)
06a2c219
GM
5307 return;
5308
5309 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
5310 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7d0393cf 5311
9ea173e8 5312 /* If tool-bar item is not enabled, don't highlight it. */
8daf1204 5313 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
06a2c219
GM
5314 if (!NILP (enabled_p))
5315 {
5316 /* Compute the x-position of the glyph. In front and past the
5317 image is a space. We include this is the highlighted area. */
5318 row = MATRIX_ROW (w->current_matrix, vpos);
5319 for (i = x = 0; i < hpos; ++i)
5320 x += row->glyphs[TEXT_AREA][i].pixel_width;
7d0393cf 5321
06a2c219
GM
5322 /* Record this as the current active region. */
5323 dpyinfo->mouse_face_beg_col = hpos;
5324 dpyinfo->mouse_face_beg_row = vpos;
5325 dpyinfo->mouse_face_beg_x = x;
5326 dpyinfo->mouse_face_beg_y = row->y;
5327 dpyinfo->mouse_face_past_end = 0;
7d0393cf 5328
06a2c219
GM
5329 dpyinfo->mouse_face_end_col = hpos + 1;
5330 dpyinfo->mouse_face_end_row = vpos;
5331 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
5332 dpyinfo->mouse_face_end_y = row->y;
5333 dpyinfo->mouse_face_window = window;
9ea173e8 5334 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
7d0393cf 5335
06a2c219
GM
5336 /* Display it as active. */
5337 show_mouse_face (dpyinfo, draw);
5338 dpyinfo->mouse_face_image_state = draw;
b8009dd1 5339 }
7d0393cf 5340
06a2c219 5341 set_help_echo:
7d0393cf 5342
50590060 5343 /* Set help_echo to a help string to display for this tool-bar item.
06a2c219 5344 XTread_socket does the rest. */
7cea38bc 5345 help_echo_object = help_echo_window = Qnil;
be010514 5346 help_echo_pos = -1;
8daf1204 5347 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
b7e80413 5348 if (NILP (help_echo))
8daf1204 5349 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
b8009dd1 5350}
4d73d038 5351
06a2c219
GM
5352
5353\f
9f8531e5
GM
5354/* Find the glyph matrix position of buffer position CHARPOS in window
5355 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
5356 current glyphs must be up to date. If CHARPOS is above window
5357 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
5358 of last line in W. In the row containing CHARPOS, stop before glyphs
7e376260 5359 having STOP as object. */
b8009dd1 5360
9f8531e5
GM
5361#if 0 /* This is a version of fast_find_position that's more correct
5362 in the presence of hscrolling, for example. I didn't install
5363 it right away because the problem fixed is minor, it failed
7d0393cf 5364 in 20.x as well, and I think it's too risky to install
9f8531e5
GM
5365 so near the release of 21.1. 2001-09-25 gerd. */
5366
5367static int
5368fast_find_position (w, charpos, hpos, vpos, x, y, stop)
5369 struct window *w;
5370 int charpos;
5371 int *hpos, *vpos, *x, *y;
5372 Lisp_Object stop;
5373{
5374 struct glyph_row *row, *first;
5375 struct glyph *glyph, *end;
5376 int i, past_end = 0;
5377
5378 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
62e33982 5379 row = row_containing_pos (w, charpos, first, NULL, 0);
9f8531e5
GM
5380 if (row == NULL)
5381 {
5382 if (charpos < MATRIX_ROW_START_CHARPOS (first))
5383 {
5384 *x = *y = *hpos = *vpos = 0;
5385 return 0;
5386 }
5387 else
5388 {
5389 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
5390 past_end = 1;
5391 }
5392 }
5393
5394 *x = row->x;
5395 *y = row->y;
5396 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7d0393cf 5397
9f8531e5
GM
5398 glyph = row->glyphs[TEXT_AREA];
5399 end = glyph + row->used[TEXT_AREA];
7d0393cf 5400
9f8531e5
GM
5401 /* Skip over glyphs not having an object at the start of the row.
5402 These are special glyphs like truncation marks on terminal
5403 frames. */
5404 if (row->displays_text_p)
5405 while (glyph < end
5406 && INTEGERP (glyph->object)
5407 && !EQ (stop, glyph->object)
5408 && glyph->charpos < 0)
5409 {
5410 *x += glyph->pixel_width;
5411 ++glyph;
5412 }
5413
5414 while (glyph < end
5415 && !INTEGERP (glyph->object)
5416 && !EQ (stop, glyph->object)
5417 && (!BUFFERP (glyph->object)
5418 || glyph->charpos < charpos))
5419 {
5420 *x += glyph->pixel_width;
5421 ++glyph;
5422 }
5423
5424 *hpos = glyph - row->glyphs[TEXT_AREA];
5425 return past_end;
5426}
5427
5428#else /* not 0 */
5429
b8009dd1 5430static int
7e376260 5431fast_find_position (w, pos, hpos, vpos, x, y, stop)
06a2c219 5432 struct window *w;
b8009dd1 5433 int pos;
06a2c219 5434 int *hpos, *vpos, *x, *y;
7e376260 5435 Lisp_Object stop;
b8009dd1 5436{
b8009dd1 5437 int i;
bf1c0ba1 5438 int lastcol;
06a2c219
GM
5439 int maybe_next_line_p = 0;
5440 int line_start_position;
5441 int yb = window_text_bottom_y (w);
03d1a189
GM
5442 struct glyph_row *row, *best_row;
5443 int row_vpos, best_row_vpos;
06a2c219
GM
5444 int current_x;
5445
03d1a189
GM
5446 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
5447 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
5448
06a2c219 5449 while (row->y < yb)
b8009dd1 5450 {
06a2c219
GM
5451 if (row->used[TEXT_AREA])
5452 line_start_position = row->glyphs[TEXT_AREA]->charpos;
5453 else
5454 line_start_position = 0;
5455
5456 if (line_start_position > pos)
b8009dd1 5457 break;
77b68646
RS
5458 /* If the position sought is the end of the buffer,
5459 don't include the blank lines at the bottom of the window. */
06a2c219
GM
5460 else if (line_start_position == pos
5461 && pos == BUF_ZV (XBUFFER (w->buffer)))
77b68646 5462 {
06a2c219 5463 maybe_next_line_p = 1;
77b68646
RS
5464 break;
5465 }
06a2c219
GM
5466 else if (line_start_position > 0)
5467 {
5468 best_row = row;
5469 best_row_vpos = row_vpos;
5470 }
4b0bb6f3
GM
5471
5472 if (row->y + row->height >= yb)
5473 break;
7d0393cf 5474
06a2c219
GM
5475 ++row;
5476 ++row_vpos;
b8009dd1 5477 }
7d0393cf 5478
06a2c219
GM
5479 /* Find the right column within BEST_ROW. */
5480 lastcol = 0;
5481 current_x = best_row->x;
5482 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
bf1c0ba1 5483 {
06a2c219 5484 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7e376260 5485 int charpos = glyph->charpos;
06a2c219 5486
7e376260 5487 if (BUFFERP (glyph->object))
bf1c0ba1 5488 {
7e376260
GM
5489 if (charpos == pos)
5490 {
5491 *hpos = i;
5492 *vpos = best_row_vpos;
5493 *x = current_x;
5494 *y = best_row->y;
5495 return 1;
5496 }
5497 else if (charpos > pos)
5498 break;
bf1c0ba1 5499 }
7e376260 5500 else if (EQ (glyph->object, stop))
4d73d038 5501 break;
06a2c219 5502
7e376260
GM
5503 if (charpos > 0)
5504 lastcol = i;
06a2c219 5505 current_x += glyph->pixel_width;
bf1c0ba1 5506 }
b8009dd1 5507
77b68646
RS
5508 /* If we're looking for the end of the buffer,
5509 and we didn't find it in the line we scanned,
5510 use the start of the following line. */
06a2c219 5511 if (maybe_next_line_p)
77b68646 5512 {
06a2c219
GM
5513 ++best_row;
5514 ++best_row_vpos;
5515 lastcol = 0;
5516 current_x = best_row->x;
77b68646
RS
5517 }
5518
06a2c219
GM
5519 *vpos = best_row_vpos;
5520 *hpos = lastcol + 1;
5521 *x = current_x;
5522 *y = best_row->y;
b8009dd1
RS
5523 return 0;
5524}
5525
9f8531e5
GM
5526#endif /* not 0 */
5527
06a2c219 5528
269b7745 5529/* Find the position of the glyph for position POS in OBJECT in
7a4bce14
PJ
5530 window W's current matrix, and return in *X, *Y the pixel
5531 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
f9db2310
GM
5532
5533 RIGHT_P non-zero means return the position of the right edge of the
5534 glyph, RIGHT_P zero means return the left edge position.
5535
5536 If no glyph for POS exists in the matrix, return the position of
5537 the glyph with the next smaller position that is in the matrix, if
5538 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
5539 exists in the matrix, return the position of the glyph with the
5540 next larger position in OBJECT.
5541
5542 Value is non-zero if a glyph was found. */
5543
5544static int
5545fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
5546 struct window *w;
5547 int pos;
5548 Lisp_Object object;
5549 int *hpos, *vpos, *x, *y;
5550 int right_p;
5551{
5552 int yb = window_text_bottom_y (w);
5553 struct glyph_row *r;
5554 struct glyph *best_glyph = NULL;
5555 struct glyph_row *best_row = NULL;
5556 int best_x = 0;
5557
5558 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
5559 r->enabled_p && r->y < yb;
5560 ++r)
5561 {
5562 struct glyph *g = r->glyphs[TEXT_AREA];
5563 struct glyph *e = g + r->used[TEXT_AREA];
5564 int gx;
5565
5566 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
5567 if (EQ (g->object, object))
5568 {
5569 if (g->charpos == pos)
5570 {
5571 best_glyph = g;
5572 best_x = gx;
5573 best_row = r;
5574 goto found;
5575 }
5576 else if (best_glyph == NULL
5577 || ((abs (g->charpos - pos)
5578 < abs (best_glyph->charpos - pos))
5579 && (right_p
5580 ? g->charpos < pos
5581 : g->charpos > pos)))
5582 {
5583 best_glyph = g;
5584 best_x = gx;
5585 best_row = r;
5586 }
5587 }
5588 }
5589
5590 found:
5591
5592 if (best_glyph)
5593 {
5594 *x = best_x;
5595 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
5596
5597 if (right_p)
5598 {
5599 *x += best_glyph->pixel_width;
5600 ++*hpos;
5601 }
7d0393cf 5602
f9db2310
GM
5603 *y = best_row->y;
5604 *vpos = best_row - w->current_matrix->rows;
5605 }
5606
5607 return best_glyph != NULL;
5608}
5609
5610
b8009dd1
RS
5611/* Display the active region described by mouse_face_*
5612 in its mouse-face if HL > 0, in its normal face if HL = 0. */
5613
5614static void
06a2c219 5615show_mouse_face (dpyinfo, draw)
7a13e894 5616 struct x_display_info *dpyinfo;
06a2c219 5617 enum draw_glyphs_face draw;
b8009dd1 5618{
7a13e894 5619 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
06a2c219 5620 struct frame *f = XFRAME (WINDOW_FRAME (w));
06a2c219 5621
b2bbd509
GM
5622 if (/* If window is in the process of being destroyed, don't bother
5623 to do anything. */
5624 w->current_matrix != NULL
66301061
KS
5625 /* Don't update mouse highlight if hidden */
5626 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
b2bbd509
GM
5627 /* Recognize when we are called to operate on rows that don't exist
5628 anymore. This can happen when a window is split. */
5629 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
06a2c219 5630 {
b2bbd509
GM
5631 int phys_cursor_on_p = w->phys_cursor_on_p;
5632 struct glyph_row *row, *first, *last;
06a2c219 5633
b2bbd509
GM
5634 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
5635 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
7d0393cf 5636
b2bbd509 5637 for (row = first; row <= last && row->enabled_p; ++row)
06a2c219 5638 {
b2bbd509 5639 int start_hpos, end_hpos, start_x;
06a2c219 5640
b2bbd509
GM
5641 /* For all but the first row, the highlight starts at column 0. */
5642 if (row == first)
5643 {
5644 start_hpos = dpyinfo->mouse_face_beg_col;
5645 start_x = dpyinfo->mouse_face_beg_x;
5646 }
5647 else
5648 {
5649 start_hpos = 0;
5650 start_x = 0;
5651 }
06a2c219 5652
b2bbd509
GM
5653 if (row == last)
5654 end_hpos = dpyinfo->mouse_face_end_col;
5655 else
5656 end_hpos = row->used[TEXT_AREA];
b8009dd1 5657
b2bbd509
GM
5658 if (end_hpos > start_hpos)
5659 {
7d0393cf 5660 x_draw_glyphs (w, start_x, row, TEXT_AREA,
b2bbd509 5661 start_hpos, end_hpos, draw, 0);
b8009dd1 5662
60626bab
GM
5663 row->mouse_face_p
5664 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
b2bbd509
GM
5665 }
5666 }
2729a2b5 5667
b2bbd509
GM
5668 /* When we've written over the cursor, arrange for it to
5669 be displayed again. */
5670 if (phys_cursor_on_p && !w->phys_cursor_on_p)
5671 x_display_cursor (w, 1,
5672 w->phys_cursor.hpos, w->phys_cursor.vpos,
5673 w->phys_cursor.x, w->phys_cursor.y);
5674 }
fb3b7de5 5675
06a2c219
GM
5676 /* Change the mouse cursor. */
5677 if (draw == DRAW_NORMAL_TEXT)
5678 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5679 f->output_data.x->text_cursor);
5680 else if (draw == DRAW_MOUSE_FACE)
334208b7 5681 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 5682 f->output_data.x->cross_cursor);
27ead1d5 5683 else
334208b7 5684 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
06a2c219 5685 f->output_data.x->nontext_cursor);
b8009dd1
RS
5686}
5687
5688/* Clear out the mouse-highlighted active region.
fa262c07
GM
5689 Redraw it un-highlighted first. Value is non-zero if mouse
5690 face was actually drawn unhighlighted. */
b8009dd1 5691
fa262c07 5692static int
7a13e894
RS
5693clear_mouse_face (dpyinfo)
5694 struct x_display_info *dpyinfo;
b8009dd1 5695{
fa262c07 5696 int cleared = 0;
7d0393cf 5697
fa262c07
GM
5698 if (!NILP (dpyinfo->mouse_face_window))
5699 {
5700 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
5701 cleared = 1;
5702 }
b8009dd1 5703
7a13e894
RS
5704 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
5705 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
5706 dpyinfo->mouse_face_window = Qnil;
1b85dc1c 5707 dpyinfo->mouse_face_overlay = Qnil;
fa262c07 5708 return cleared;
b8009dd1 5709}
e687d06e 5710
71b8321e
GM
5711
5712/* Clear any mouse-face on window W. This function is part of the
5713 redisplay interface, and is called from try_window_id and similar
5714 functions to ensure the mouse-highlight is off. */
5715
5716static void
5717x_clear_mouse_face (w)
5718 struct window *w;
5719{
5720 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
5721 Lisp_Object window;
5722
2e636f9d 5723 BLOCK_INPUT;
71b8321e
GM
5724 XSETWINDOW (window, w);
5725 if (EQ (window, dpyinfo->mouse_face_window))
5726 clear_mouse_face (dpyinfo);
2e636f9d 5727 UNBLOCK_INPUT;
71b8321e
GM
5728}
5729
5730
e687d06e
RS
5731/* Just discard the mouse face information for frame F, if any.
5732 This is used when the size of F is changed. */
5733
dfcf069d 5734void
e687d06e
RS
5735cancel_mouse_face (f)
5736 FRAME_PTR f;
5737{
5738 Lisp_Object window;
5739 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
5740
5741 window = dpyinfo->mouse_face_window;
5742 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
5743 {
5744 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
5745 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
5746 dpyinfo->mouse_face_window = Qnil;
5747 }
5748}
b52b65bd 5749
b8009dd1 5750\f
b52b65bd
GM
5751static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
5752
5753
5754/* Try to determine frame pixel position and size of the glyph under
5755 frame pixel coordinates X/Y on frame F . Return the position and
5756 size in *RECT. Value is non-zero if we could compute these
5757 values. */
5758
5759static int
5760glyph_rect (f, x, y, rect)
5761 struct frame *f;
5762 int x, y;
5763 XRectangle *rect;
5764{
5765 Lisp_Object window;
5766 int part, found = 0;
5767
5768 window = window_from_coordinates (f, x, y, &part, 0);
5769 if (!NILP (window))
5770 {
5771 struct window *w = XWINDOW (window);
5772 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
5773 struct glyph_row *end = r + w->current_matrix->nrows - 1;
b52b65bd
GM
5774
5775 frame_to_window_pixel_xy (w, &x, &y);
7d0393cf 5776
b52b65bd
GM
5777 for (; !found && r < end && r->enabled_p; ++r)
5778 if (r->y >= y)
5779 {
5780 struct glyph *g = r->glyphs[TEXT_AREA];
5781 struct glyph *end = g + r->used[TEXT_AREA];
5782 int gx;
7d0393cf 5783
b52b65bd
GM
5784 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
5785 if (gx >= x)
5786 {
5787 rect->width = g->pixel_width;
5788 rect->height = r->height;
5789 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
5790 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
5791 found = 1;
5792 }
5793 }
5794 }
5795
5796 return found;
5797}
5798
12ba150f 5799
90e65f07 5800/* Return the current position of the mouse.
b52b65bd 5801 *FP should be a frame which indicates which display to ask about.
90e65f07 5802
b52b65bd
GM
5803 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
5804 and *PART to the frame, window, and scroll bar part that the mouse
5805 is over. Set *X and *Y to the portion and whole of the mouse's
ab648270 5806 position on the scroll bar.
12ba150f 5807
b52b65bd
GM
5808 If the mouse movement started elsewhere, set *FP to the frame the
5809 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
12ba150f
JB
5810 the mouse is over.
5811
b52b65bd 5812 Set *TIME to the server time-stamp for the time at which the mouse
12ba150f
JB
5813 was at this position.
5814
a135645a
RS
5815 Don't store anything if we don't have a valid set of values to report.
5816
90e65f07 5817 This clears the mouse_moved flag, so we can wait for the next mouse
f5bb65ec 5818 movement. */
90e65f07
JB
5819
5820static void
1cf412ec 5821XTmouse_position (fp, insist, bar_window, part, x, y, time)
334208b7 5822 FRAME_PTR *fp;
1cf412ec 5823 int insist;
12ba150f 5824 Lisp_Object *bar_window;
ab648270 5825 enum scroll_bar_part *part;
90e65f07 5826 Lisp_Object *x, *y;
e5d77022 5827 unsigned long *time;
90e65f07 5828{
a135645a
RS
5829 FRAME_PTR f1;
5830
90e65f07
JB
5831 BLOCK_INPUT;
5832
8bcee03e 5833 if (! NILP (last_mouse_scroll_bar) && insist == 0)
334208b7 5834 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
90e65f07
JB
5835 else
5836 {
12ba150f
JB
5837 Window root;
5838 int root_x, root_y;
90e65f07 5839
12ba150f
JB
5840 Window dummy_window;
5841 int dummy;
5842
39d8bb4d
KH
5843 Lisp_Object frame, tail;
5844
5845 /* Clear the mouse-moved flag for every frame on this display. */
5846 FOR_EACH_FRAME (tail, frame)
5847 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
5848 XFRAME (frame)->mouse_moved = 0;
5849
ab648270 5850 last_mouse_scroll_bar = Qnil;
12ba150f
JB
5851
5852 /* Figure out which root window we're on. */
334208b7
RS
5853 XQueryPointer (FRAME_X_DISPLAY (*fp),
5854 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
12ba150f
JB
5855
5856 /* The root window which contains the pointer. */
5857 &root,
5858
5859 /* Trash which we can't trust if the pointer is on
5860 a different screen. */
5861 &dummy_window,
5862
5863 /* The position on that root window. */
58769bee 5864 &root_x, &root_y,
12ba150f
JB
5865
5866 /* More trash we can't trust. */
5867 &dummy, &dummy,
5868
5869 /* Modifier keys and pointer buttons, about which
5870 we don't care. */
5871 (unsigned int *) &dummy);
5872
5873 /* Now we have a position on the root; find the innermost window
5874 containing the pointer. */
5875 {
5876 Window win, child;
5877 int win_x, win_y;
06a2c219 5878 int parent_x = 0, parent_y = 0;
e99db5a1 5879 int count;
12ba150f
JB
5880
5881 win = root;
69388238 5882
2d7fc7e8
RS
5883 /* XTranslateCoordinates can get errors if the window
5884 structure is changing at the same time this function
5885 is running. So at least we must not crash from them. */
5886
e99db5a1 5887 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
2d7fc7e8 5888
334208b7 5889 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
23faf38f 5890 && FRAME_LIVE_P (last_mouse_frame))
12ba150f 5891 {
69388238
RS
5892 /* If mouse was grabbed on a frame, give coords for that frame
5893 even if the mouse is now outside it. */
334208b7 5894 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
69388238 5895
12ba150f 5896 /* From-window, to-window. */
69388238 5897 root, FRAME_X_WINDOW (last_mouse_frame),
12ba150f
JB
5898
5899 /* From-position, to-position. */
5900 root_x, root_y, &win_x, &win_y,
5901
5902 /* Child of win. */
5903 &child);
69388238
RS
5904 f1 = last_mouse_frame;
5905 }
5906 else
5907 {
5908 while (1)
5909 {
334208b7 5910 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
12ba150f 5911
69388238
RS
5912 /* From-window, to-window. */
5913 root, win,
12ba150f 5914
69388238
RS
5915 /* From-position, to-position. */
5916 root_x, root_y, &win_x, &win_y,
5917
5918 /* Child of win. */
5919 &child);
5920
9af3143a 5921 if (child == None || child == win)
69388238
RS
5922 break;
5923
5924 win = child;
5925 parent_x = win_x;
5926 parent_y = win_y;
5927 }
12ba150f 5928
69388238
RS
5929 /* Now we know that:
5930 win is the innermost window containing the pointer
5931 (XTC says it has no child containing the pointer),
5932 win_x and win_y are the pointer's position in it
5933 (XTC did this the last time through), and
5934 parent_x and parent_y are the pointer's position in win's parent.
5935 (They are what win_x and win_y were when win was child.
5936 If win is the root window, it has no parent, and
5937 parent_{x,y} are invalid, but that's okay, because we'll
5938 never use them in that case.) */
5939
5940 /* Is win one of our frames? */
19126e11 5941 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
aad0f6ab
GM
5942
5943#ifdef USE_X_TOOLKIT
5944 /* If we end up with the menu bar window, say it's not
5945 on the frame. */
5946 if (f1 != NULL
5947 && f1->output_data.x->menubar_widget
5948 && win == XtWindow (f1->output_data.x->menubar_widget))
5949 f1 = NULL;
5950#endif /* USE_X_TOOLKIT */
69388238 5951 }
58769bee 5952
2d7fc7e8
RS
5953 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
5954 f1 = 0;
5955
e99db5a1 5956 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
2d7fc7e8 5957
ab648270 5958 /* If not, is it one of our scroll bars? */
a135645a 5959 if (! f1)
12ba150f 5960 {
ab648270 5961 struct scroll_bar *bar = x_window_to_scroll_bar (win);
12ba150f
JB
5962
5963 if (bar)
5964 {
a135645a 5965 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
5966 win_x = parent_x;
5967 win_y = parent_y;
5968 }
5969 }
90e65f07 5970
8bcee03e 5971 if (f1 == 0 && insist > 0)
b86bd3dd 5972 f1 = SELECTED_FRAME ();
1cf412ec 5973
a135645a 5974 if (f1)
12ba150f 5975 {
06a2c219
GM
5976 /* Ok, we found a frame. Store all the values.
5977 last_mouse_glyph is a rectangle used to reduce the
5978 generation of mouse events. To not miss any motion
5979 events, we must divide the frame into rectangles of the
5980 size of the smallest character that could be displayed
5981 on it, i.e. into the same rectangles that matrices on
5982 the frame are divided into. */
5983
b52b65bd
GM
5984 int width, height, gx, gy;
5985 XRectangle rect;
7d0393cf 5986
b52b65bd
GM
5987 if (glyph_rect (f1, win_x, win_y, &rect))
5988 last_mouse_glyph = rect;
5989 else
5990 {
5991 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
5992 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
5993 gx = win_x;
5994 gy = win_y;
7d0393cf 5995
b52b65bd
GM
5996 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
5997 round down even for negative values. */
5998 if (gx < 0)
5999 gx -= width - 1;
4f00e84d 6000 if (gy < 0)
b52b65bd
GM
6001 gy -= height - 1;
6002 gx = (gx + width - 1) / width * width;
6003 gy = (gy + height - 1) / height * height;
7d0393cf 6004
b52b65bd
GM
6005 last_mouse_glyph.width = width;
6006 last_mouse_glyph.height = height;
6007 last_mouse_glyph.x = gx;
6008 last_mouse_glyph.y = gy;
6009 }
12ba150f
JB
6010
6011 *bar_window = Qnil;
6012 *part = 0;
334208b7 6013 *fp = f1;
e0c1aef2
KH
6014 XSETINT (*x, win_x);
6015 XSETINT (*y, win_y);
12ba150f
JB
6016 *time = last_mouse_movement_time;
6017 }
6018 }
6019 }
90e65f07
JB
6020
6021 UNBLOCK_INPUT;
6022}
f451eb13 6023
06a2c219 6024
06a2c219 6025#ifdef USE_X_TOOLKIT
bffcfca9
GM
6026
6027/* Atimer callback function for TIMER. Called every 0.1s to process
6028 Xt timeouts, if needed. We must avoid calling XtAppPending as
6029 much as possible because that function does an implicit XFlush
6030 that slows us down. */
6031
6032static void
6033x_process_timeouts (timer)
6034 struct atimer *timer;
6035{
488dd4c4 6036 if (toolkit_scroll_bar_interaction || popup_activated ())
bffcfca9
GM
6037 {
6038 BLOCK_INPUT;
6039 while (XtAppPending (Xt_app_con) & XtIMTimer)
6040 XtAppProcessEvent (Xt_app_con, XtIMTimer);
6041 UNBLOCK_INPUT;
6042 }
06a2c219
GM
6043}
6044
bffcfca9 6045#endif /* USE_X_TOOLKIT */
06a2c219
GM
6046
6047\f
6048/* Scroll bar support. */
6049
6050/* Given an X window ID, find the struct scroll_bar which manages it.
6051 This can be called in GC, so we have to make sure to strip off mark
6052 bits. */
bffcfca9 6053
06a2c219
GM
6054static struct scroll_bar *
6055x_window_to_scroll_bar (window_id)
6056 Window window_id;
6057{
6058 Lisp_Object tail;
6059
257f40f2
JD
6060#ifdef USE_GTK
6061 window_id = (Window) xg_get_scroll_id_for_window (window_id);
6062#endif /* USE_GTK */
6063
06a2c219
GM
6064 for (tail = Vframe_list;
6065 XGCTYPE (tail) == Lisp_Cons;
8e713be6 6066 tail = XCDR (tail))
06a2c219
GM
6067 {
6068 Lisp_Object frame, bar, condemned;
6069
8e713be6 6070 frame = XCAR (tail);
06a2c219
GM
6071 /* All elements of Vframe_list should be frames. */
6072 if (! GC_FRAMEP (frame))
6073 abort ();
6074
6075 /* Scan this frame's scroll bar list for a scroll bar with the
6076 right window ID. */
6077 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
6078 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
6079 /* This trick allows us to search both the ordinary and
6080 condemned scroll bar lists with one loop. */
6081 ! GC_NILP (bar) || (bar = condemned,
6082 condemned = Qnil,
6083 ! GC_NILP (bar));
6084 bar = XSCROLL_BAR (bar)->next)
6085 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
6086 return XSCROLL_BAR (bar);
6087 }
6088
6089 return 0;
6090}
6091
6092
01f67d2c 6093#if defined USE_LUCID
c95fc5f1
GM
6094
6095/* Return the Lucid menu bar WINDOW is part of. Return null
6096 if WINDOW is not part of a menu bar. */
6097
6098static Widget
6099x_window_to_menu_bar (window)
6100 Window window;
6101{
6102 Lisp_Object tail;
7d0393cf 6103
c95fc5f1
GM
6104 for (tail = Vframe_list;
6105 XGCTYPE (tail) == Lisp_Cons;
6106 tail = XCDR (tail))
6107 {
6108 Lisp_Object frame = XCAR (tail);
6109 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
7d0393cf 6110
c95fc5f1
GM
6111 if (menu_bar && xlwmenu_window_p (menu_bar, window))
6112 return menu_bar;
6113 }
6114
6115 return NULL;
6116}
6117
01f67d2c 6118#endif /* USE_LUCID */
c95fc5f1 6119
06a2c219
GM
6120\f
6121/************************************************************************
6122 Toolkit scroll bars
6123 ************************************************************************/
6124
eccc05db 6125#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
6126
6127static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
6128static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
6129static void x_create_toolkit_scroll_bar P_ ((struct frame *,
6130 struct scroll_bar *));
6131static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
6132 int, int, int));
6133
6134
06a2c219
GM
6135/* Lisp window being scrolled. Set when starting to interact with
6136 a toolkit scroll bar, reset to nil when ending the interaction. */
6137
6138static Lisp_Object window_being_scrolled;
6139
6140/* Last scroll bar part sent in xm_scroll_callback. */
6141
6142static int last_scroll_bar_part;
6143
ec18280f
SM
6144/* Whether this is an Xaw with arrow-scrollbars. This should imply
6145 that movements of 1/20 of the screen size are mapped to up/down. */
6146
488dd4c4
JD
6147#ifndef USE_GTK
6148/* Id of action hook installed for scroll bars. */
6149
6150static XtActionHookId action_hook_id;
6151
ec18280f
SM
6152static Boolean xaw3d_arrow_scroll;
6153
6154/* Whether the drag scrolling maintains the mouse at the top of the
6155 thumb. If not, resizing the thumb needs to be done more carefully
6156 to avoid jerkyness. */
6157
6158static Boolean xaw3d_pick_top;
6159
06a2c219
GM
6160
6161/* Action hook installed via XtAppAddActionHook when toolkit scroll
ec18280f 6162 bars are used.. The hook is responsible for detecting when
06a2c219 6163 the user ends an interaction with the scroll bar, and generates
3b8f9651 6164 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
06a2c219
GM
6165
6166static void
6167xt_action_hook (widget, client_data, action_name, event, params,
6168 num_params)
6169 Widget widget;
6170 XtPointer client_data;
6171 String action_name;
6172 XEvent *event;
6173 String *params;
6174 Cardinal *num_params;
6175{
6176 int scroll_bar_p;
6177 char *end_action;
7d0393cf 6178
06a2c219
GM
6179#ifdef USE_MOTIF
6180 scroll_bar_p = XmIsScrollBar (widget);
6181 end_action = "Release";
ec18280f 6182#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
6183 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
6184 end_action = "EndScroll";
ec18280f 6185#endif /* USE_MOTIF */
06a2c219 6186
06a2c219
GM
6187 if (scroll_bar_p
6188 && strcmp (action_name, end_action) == 0
6189 && WINDOWP (window_being_scrolled))
6190 {
6191 struct window *w;
7d0393cf 6192
06a2c219
GM
6193 x_send_scroll_bar_event (window_being_scrolled,
6194 scroll_bar_end_scroll, 0, 0);
6195 w = XWINDOW (window_being_scrolled);
6196 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
6197 window_being_scrolled = Qnil;
6198 last_scroll_bar_part = -1;
bffcfca9
GM
6199
6200 /* Xt timeouts no longer needed. */
6201 toolkit_scroll_bar_interaction = 0;
06a2c219
GM
6202 }
6203}
488dd4c4 6204#endif /* not USE_GTK */
06a2c219 6205
07b3d16e
GM
6206/* A vector of windows used for communication between
6207 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
6208
6209static struct window **scroll_bar_windows;
6210static int scroll_bar_windows_size;
6211
06a2c219
GM
6212
6213/* Send a client message with message type Xatom_Scrollbar for a
6214 scroll action to the frame of WINDOW. PART is a value identifying
6215 the part of the scroll bar that was clicked on. PORTION is the
6216 amount to scroll of a whole of WHOLE. */
6217
6218static void
6219x_send_scroll_bar_event (window, part, portion, whole)
6220 Lisp_Object window;
6221 int part, portion, whole;
6222{
6223 XEvent event;
6224 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
07b3d16e
GM
6225 struct window *w = XWINDOW (window);
6226 struct frame *f = XFRAME (w->frame);
6227 int i;
06a2c219 6228
07b3d16e 6229 BLOCK_INPUT;
7d0393cf 6230
06a2c219
GM
6231 /* Construct a ClientMessage event to send to the frame. */
6232 ev->type = ClientMessage;
6233 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
6234 ev->display = FRAME_X_DISPLAY (f);
6235 ev->window = FRAME_X_WINDOW (f);
6236 ev->format = 32;
07b3d16e
GM
6237
6238 /* We can only transfer 32 bits in the XClientMessageEvent, which is
6239 not enough to store a pointer or Lisp_Object on a 64 bit system.
6240 So, store the window in scroll_bar_windows and pass the index
6241 into that array in the event. */
6242 for (i = 0; i < scroll_bar_windows_size; ++i)
6243 if (scroll_bar_windows[i] == NULL)
6244 break;
6245
6246 if (i == scroll_bar_windows_size)
6247 {
6248 int new_size = max (10, 2 * scroll_bar_windows_size);
6249 size_t nbytes = new_size * sizeof *scroll_bar_windows;
6250 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
7d0393cf 6251
07b3d16e
GM
6252 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
6253 nbytes);
6254 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
6255 scroll_bar_windows_size = new_size;
6256 }
6257
6258 scroll_bar_windows[i] = w;
6259 ev->data.l[0] = (long) i;
06a2c219
GM
6260 ev->data.l[1] = (long) part;
6261 ev->data.l[2] = (long) 0;
6262 ev->data.l[3] = (long) portion;
6263 ev->data.l[4] = (long) whole;
6264
bffcfca9
GM
6265 /* Make Xt timeouts work while the scroll bar is active. */
6266 toolkit_scroll_bar_interaction = 1;
6267
06a2c219
GM
6268 /* Setting the event mask to zero means that the message will
6269 be sent to the client that created the window, and if that
6270 window no longer exists, no event will be sent. */
06a2c219
GM
6271 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
6272 UNBLOCK_INPUT;
6273}
6274
6275
6276/* Transform a scroll bar ClientMessage EVENT to an Emacs input event
6277 in *IEVENT. */
6278
6279static void
6280x_scroll_bar_to_input_event (event, ievent)
6281 XEvent *event;
6282 struct input_event *ievent;
6283{
6284 XClientMessageEvent *ev = (XClientMessageEvent *) event;
52e386c2
KR
6285 Lisp_Object window;
6286 struct frame *f;
07b3d16e 6287 struct window *w;
7d0393cf 6288
07b3d16e
GM
6289 w = scroll_bar_windows[ev->data.l[0]];
6290 scroll_bar_windows[ev->data.l[0]] = NULL;
52e386c2 6291
07b3d16e
GM
6292 XSETWINDOW (window, w);
6293 f = XFRAME (w->frame);
7d0393cf 6294
3b8f9651 6295 ievent->kind = SCROLL_BAR_CLICK_EVENT;
06a2c219 6296 ievent->frame_or_window = window;
0f8aabe9 6297 ievent->arg = Qnil;
488dd4c4
JD
6298#ifdef USE_GTK
6299 ievent->timestamp = CurrentTime;
6300#else
06a2c219 6301 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
488dd4c4 6302#endif
06a2c219
GM
6303 ievent->part = ev->data.l[1];
6304 ievent->code = ev->data.l[2];
6305 ievent->x = make_number ((int) ev->data.l[3]);
6306 ievent->y = make_number ((int) ev->data.l[4]);
6307 ievent->modifiers = 0;
6308}
6309
6310
6311#ifdef USE_MOTIF
6312
6313/* Minimum and maximum values used for Motif scroll bars. */
6314
6315#define XM_SB_MIN 1
6316#define XM_SB_MAX 10000000
6317#define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
6318
6319
6320/* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
6321 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4696802b 6322 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
06a2c219
GM
6323
6324static void
6325xm_scroll_callback (widget, client_data, call_data)
6326 Widget widget;
6327 XtPointer client_data, call_data;
6328{
6329 struct scroll_bar *bar = (struct scroll_bar *) client_data;
6330 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
06a2c219
GM
6331 int part = -1, whole = 0, portion = 0;
6332
6333 switch (cs->reason)
6334 {
6335 case XmCR_DECREMENT:
6336 bar->dragging = Qnil;
6337 part = scroll_bar_up_arrow;
6338 break;
6339
6340 case XmCR_INCREMENT:
6341 bar->dragging = Qnil;
6342 part = scroll_bar_down_arrow;
6343 break;
6344
6345 case XmCR_PAGE_DECREMENT:
6346 bar->dragging = Qnil;
6347 part = scroll_bar_above_handle;
6348 break;
6349
6350 case XmCR_PAGE_INCREMENT:
6351 bar->dragging = Qnil;
6352 part = scroll_bar_below_handle;
6353 break;
6354
6355 case XmCR_TO_TOP:
6356 bar->dragging = Qnil;
6357 part = scroll_bar_to_top;
6358 break;
7d0393cf 6359
06a2c219
GM
6360 case XmCR_TO_BOTTOM:
6361 bar->dragging = Qnil;
6362 part = scroll_bar_to_bottom;
6363 break;
6364
6365 case XmCR_DRAG:
6366 {
6367 int slider_size;
6368 int dragging_down_p = (INTEGERP (bar->dragging)
6369 && XINT (bar->dragging) <= cs->value);
6370
6371 /* Get the slider size. */
6372 BLOCK_INPUT;
6373 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
6374 UNBLOCK_INPUT;
6375
6f2a7a68
SM
6376 whole = XM_SB_RANGE - slider_size;
6377 portion = min (cs->value - XM_SB_MIN, whole);
6378 part = scroll_bar_handle;
6379 bar->dragging = make_number (cs->value);
06a2c219
GM
6380 }
6381 break;
7d0393cf 6382
06a2c219
GM
6383 case XmCR_VALUE_CHANGED:
6384 break;
6385 };
6386
6387 if (part >= 0)
6388 {
6389 window_being_scrolled = bar->window;
6390 last_scroll_bar_part = part;
6391 x_send_scroll_bar_event (bar->window, part, portion, whole);
6392 }
6393}
6394
6395
488dd4c4
JD
6396#else /* !USE_MOTIF, i.e. Xaw or GTK */
6397#ifdef USE_GTK
6398/* Scroll bar callback for Gtk scroll bars. WIDGET is the scroll
6399 bar adjustment widget. DATA is a pointer to the scroll_bar structure. */
6400
6401static void
6402xg_scroll_callback (widget, data)
6403 GtkWidget *widget;
6404 gpointer data;
6405{
6406 struct scroll_bar *bar = (struct scroll_bar *) data;
6407 gdouble previous;
6408 gdouble position;
6409 gdouble *p;
6410 int diff;
177c0ea7 6411
488dd4c4
JD
6412 int part = -1, whole = 0, portion = 0;
6413 GtkAdjustment *adj = GTK_ADJUSTMENT (widget);
177c0ea7 6414
488dd4c4 6415 if (xg_ignore_gtk_scrollbar) return;
177c0ea7 6416
488dd4c4
JD
6417 position = gtk_adjustment_get_value (adj);
6418
6419 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
6420 if (! p)
6421 {
6422 p = (gdouble*) xmalloc (sizeof (gdouble));
6423 *p = XG_SB_MIN;
6424 g_object_set_data (G_OBJECT (widget), XG_LAST_SB_DATA, p);
6425 }
6426
6427 previous = *p;
6428 *p = position;
6429
6430 diff = (int) (position - previous);
177c0ea7 6431
488dd4c4
JD
6432 if (diff == (int) adj->step_increment)
6433 {
6434 part = scroll_bar_down_arrow;
6435 bar->dragging = Qnil;
6436 }
6437 else if (-diff == (int) adj->step_increment)
6438 {
6439 part = scroll_bar_up_arrow;
6440 bar->dragging = Qnil;
6441 }
6442 else if (diff == (int) adj->page_increment)
6443 {
6444 part = scroll_bar_below_handle;
6445 bar->dragging = Qnil;
6446 }
6447 else if (-diff == (int) adj->page_increment)
6448 {
6449 part = scroll_bar_above_handle;
6450 bar->dragging = Qnil;
6451 }
6452 else
6453 {
6454 part = scroll_bar_handle;
6455 whole = adj->upper - adj->page_size;
6456 portion = min (position, whole);
6457 bar->dragging = make_number (portion);
6458 }
177c0ea7 6459
488dd4c4
JD
6460 if (part >= 0)
6461 {
6462 xg_ignore_next_thumb = 1;
6463 window_being_scrolled = bar->window;
6464 last_scroll_bar_part = part;
6465 x_send_scroll_bar_event (bar->window, part, portion, whole);
6466 }
6467}
06a2c219 6468
488dd4c4 6469#else /* not USE_GTK */
06a2c219 6470
ec18280f 6471/* Xaw scroll bar callback. Invoked when the thumb is dragged.
06a2c219
GM
6472 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
6473 scroll bar struct. CALL_DATA is a pointer to a float saying where
6474 the thumb is. */
6475
6476static void
ec18280f 6477xaw_jump_callback (widget, client_data, call_data)
06a2c219
GM
6478 Widget widget;
6479 XtPointer client_data, call_data;
6480{
6481 struct scroll_bar *bar = (struct scroll_bar *) client_data;
6482 float top = *(float *) call_data;
6483 float shown;
ec18280f
SM
6484 int whole, portion, height;
6485 int part;
06a2c219
GM
6486
6487 /* Get the size of the thumb, a value between 0 and 1. */
6488 BLOCK_INPUT;
ec18280f 6489 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
06a2c219
GM
6490 UNBLOCK_INPUT;
6491
6492 whole = 10000000;
6493 portion = shown < 1 ? top * whole : 0;
06a2c219 6494
ec18280f
SM
6495 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
6496 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
6497 the bottom, so we force the scrolling whenever we see that we're
6498 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
6499 we try to ensure that we always stay two pixels away from the
6500 bottom). */
06a2c219
GM
6501 part = scroll_bar_down_arrow;
6502 else
6503 part = scroll_bar_handle;
6504
6505 window_being_scrolled = bar->window;
6506 bar->dragging = make_number (portion);
6507 last_scroll_bar_part = part;
6508 x_send_scroll_bar_event (bar->window, part, portion, whole);
6509}
6510
6511
ec18280f
SM
6512/* Xaw scroll bar callback. Invoked for incremental scrolling.,
6513 i.e. line or page up or down. WIDGET is the Xaw scroll bar
06a2c219
GM
6514 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
6515 the scroll bar. CALL_DATA is an integer specifying the action that
dbd4b028 6516 has taken place. Its magnitude is in the range 0..height of the
06a2c219
GM
6517 scroll bar. Negative values mean scroll towards buffer start.
6518 Values < height of scroll bar mean line-wise movement. */
6519
6520static void
ec18280f 6521xaw_scroll_callback (widget, client_data, call_data)
06a2c219
GM
6522 Widget widget;
6523 XtPointer client_data, call_data;
6524{
6525 struct scroll_bar *bar = (struct scroll_bar *) client_data;
560d7ceb
DL
6526 /* The position really is stored cast to a pointer. */
6527 int position = (long) call_data;
06a2c219
GM
6528 Dimension height;
6529 int part;
6530
6531 /* Get the height of the scroll bar. */
6532 BLOCK_INPUT;
6533 XtVaGetValues (widget, XtNheight, &height, NULL);
6534 UNBLOCK_INPUT;
6535
ec18280f
SM
6536 if (abs (position) >= height)
6537 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
6538
6539 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
6540 it maps line-movement to call_data = max(5, height/20). */
6541 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
6542 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
06a2c219 6543 else
ec18280f 6544 part = scroll_bar_move_ratio;
06a2c219
GM
6545
6546 window_being_scrolled = bar->window;
6547 bar->dragging = Qnil;
6548 last_scroll_bar_part = part;
ec18280f 6549 x_send_scroll_bar_event (bar->window, part, position, height);
06a2c219
GM
6550}
6551
488dd4c4 6552#endif /* not USE_GTK */
06a2c219
GM
6553#endif /* not USE_MOTIF */
6554
488dd4c4 6555#define SCROLL_BAR_NAME "verticalScrollBar"
06a2c219
GM
6556
6557/* Create the widget for scroll bar BAR on frame F. Record the widget
6558 and X window of the scroll bar in BAR. */
6559
488dd4c4
JD
6560#ifdef USE_GTK
6561static void
6562x_create_toolkit_scroll_bar (f, bar)
6563 struct frame *f;
6564 struct scroll_bar *bar;
6565{
6566 char *scroll_bar_name = SCROLL_BAR_NAME;
6567
6568 BLOCK_INPUT;
6569 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
6570 scroll_bar_name);
6571 UNBLOCK_INPUT;
6572}
6573
6574#else /* not USE_GTK */
6575
06a2c219
GM
6576static void
6577x_create_toolkit_scroll_bar (f, bar)
6578 struct frame *f;
6579 struct scroll_bar *bar;
6580{
6581 Window xwindow;
6582 Widget widget;
6583 Arg av[20];
6584 int ac = 0;
488dd4c4 6585 char *scroll_bar_name = SCROLL_BAR_NAME;
06a2c219
GM
6586 unsigned long pixel;
6587
6588 BLOCK_INPUT;
6589
6590#ifdef USE_MOTIF
06a2c219
GM
6591 /* Set resources. Create the widget. */
6592 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
6593 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
6594 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
6595 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
6596 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
6597 XtSetArg (av[ac], XmNincrement, 1); ++ac;
6598 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
6599
6600 pixel = f->output_data.x->scroll_bar_foreground_pixel;
6601 if (pixel != -1)
6602 {
6603 XtSetArg (av[ac], XmNforeground, pixel);
6604 ++ac;
6605 }
7d0393cf 6606
06a2c219
GM
6607 pixel = f->output_data.x->scroll_bar_background_pixel;
6608 if (pixel != -1)
6609 {
6610 XtSetArg (av[ac], XmNbackground, pixel);
6611 ++ac;
6612 }
7d0393cf 6613
06a2c219
GM
6614 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
6615 scroll_bar_name, av, ac);
6616
6617 /* Add one callback for everything that can happen. */
6618 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
6619 (XtPointer) bar);
6620 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
6621 (XtPointer) bar);
6622 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
6623 (XtPointer) bar);
6624 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
6625 (XtPointer) bar);
6626 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
6627 (XtPointer) bar);
6628 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
6629 (XtPointer) bar);
6630 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
6631 (XtPointer) bar);
7d0393cf 6632
06a2c219
GM
6633 /* Realize the widget. Only after that is the X window created. */
6634 XtRealizeWidget (widget);
6635
6636 /* Set the cursor to an arrow. I didn't find a resource to do that.
6637 And I'm wondering why it hasn't an arrow cursor by default. */
6638 XDefineCursor (XtDisplay (widget), XtWindow (widget),
6639 f->output_data.x->nontext_cursor);
7d0393cf 6640
ec18280f 6641#else /* !USE_MOTIF i.e. use Xaw */
7d0393cf 6642
06a2c219
GM
6643 /* Set resources. Create the widget. The background of the
6644 Xaw3d scroll bar widget is a little bit light for my taste.
6645 We don't alter it here to let users change it according
6646 to their taste with `emacs*verticalScrollBar.background: xxx'. */
6647 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
6648 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
ec18280f
SM
6649 /* For smoother scrolling with Xaw3d -sm */
6650 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
7d0393cf 6651
06a2c219
GM
6652 pixel = f->output_data.x->scroll_bar_foreground_pixel;
6653 if (pixel != -1)
6654 {
6655 XtSetArg (av[ac], XtNforeground, pixel);
6656 ++ac;
6657 }
7d0393cf 6658
06a2c219
GM
6659 pixel = f->output_data.x->scroll_bar_background_pixel;
6660 if (pixel != -1)
6661 {
6662 XtSetArg (av[ac], XtNbackground, pixel);
6663 ++ac;
6664 }
7c1bef7a
MB
6665
6666 /* Top/bottom shadow colors. */
6667
6668 /* Allocate them, if necessary. */
6669 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
6670 {
6671 pixel = f->output_data.x->scroll_bar_background_pixel;
6672 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
6673 &pixel, 1.2, 0x8000))
6674 pixel = -1;
6675 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
6676 }
6677 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
6678 {
6679 pixel = f->output_data.x->scroll_bar_background_pixel;
6680 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
6681 &pixel, 0.6, 0x4000))
6682 pixel = -1;
6683 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
6684 }
6685
6686 /* Tell the toolkit about them. */
6687 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
6688 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
6689 /* We tried to allocate a color for the top/bottom shadow, and
6690 failed, so tell Xaw3d to use dithering instead. */
6691 {
6692 XtSetArg (av[ac], XtNbeNiceToColormap, True);
6693 ++ac;
6694 }
6695 else
6696 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
6697 be more consistent with other emacs 3d colors, and since Xaw3d is
6698 not good at dealing with allocation failure. */
6699 {
6700 /* This tells Xaw3d to use real colors instead of dithering for
6701 the shadows. */
6702 XtSetArg (av[ac], XtNbeNiceToColormap, False);
6703 ++ac;
6704
6705 /* Specify the colors. */
6706 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
6707 if (pixel != -1)
6708 {
6709 XtSetArg (av[ac], "topShadowPixel", pixel);
6710 ++ac;
6711 }
6712 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
6713 if (pixel != -1)
6714 {
6715 XtSetArg (av[ac], "bottomShadowPixel", pixel);
6716 ++ac;
6717 }
6718 }
6719
06a2c219
GM
6720 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
6721 f->output_data.x->edit_widget, av, ac);
ec18280f
SM
6722
6723 {
6724 char *initial = "";
6725 char *val = initial;
6726 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
6727 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
6728 if (val == initial)
6729 { /* ARROW_SCROLL */
6730 xaw3d_arrow_scroll = True;
6731 /* Isn't that just a personal preference ? -sm */
6732 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
6733 }
6734 }
7d0393cf 6735
06a2c219 6736 /* Define callbacks. */
ec18280f
SM
6737 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
6738 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
06a2c219 6739 (XtPointer) bar);
7d0393cf 6740
06a2c219
GM
6741 /* Realize the widget. Only after that is the X window created. */
6742 XtRealizeWidget (widget);
7d0393cf 6743
ec18280f 6744#endif /* !USE_MOTIF */
06a2c219 6745
2a51c026 6746 /* Install an action hook that lets us detect when the user
06a2c219
GM
6747 finishes interacting with a scroll bar. */
6748 if (action_hook_id == 0)
6749 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
7d0393cf 6750
06a2c219
GM
6751 /* Remember X window and widget in the scroll bar vector. */
6752 SET_SCROLL_BAR_X_WIDGET (bar, widget);
6753 xwindow = XtWindow (widget);
6754 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
6755
6756 UNBLOCK_INPUT;
6757}
488dd4c4 6758#endif /* not USE_GTK */
06a2c219
GM
6759
6760
6761/* Set the thumb size and position of scroll bar BAR. We are currently
6762 displaying PORTION out of a whole WHOLE, and our position POSITION. */
6763
488dd4c4
JD
6764#ifdef USE_GTK
6765static void
6766x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
6767 struct scroll_bar *bar;
6768 int portion, position, whole;
6769{
6770 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
6771}
6772
6773#else /* not USE_GTK */
06a2c219
GM
6774static void
6775x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
6776 struct scroll_bar *bar;
6777 int portion, position, whole;
f451eb13 6778{
e83dc917
GM
6779 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6780 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
06a2c219 6781 float top, shown;
f451eb13 6782
6f2a7a68
SM
6783 BLOCK_INPUT;
6784
6785#ifdef USE_MOTIF
6786
6787 /* We use an estimate of 30 chars per line rather than the real
6788 `portion' value. This has the disadvantage that the thumb size
6789 is not very representative, but it makes our life a lot easier.
6790 Otherwise, we have to constantly adjust the thumb size, which
6791 we can't always do quickly enough: while dragging, the size of
6792 the thumb might prevent the user from dragging the thumb all the
6793 way to the end. but Motif and some versions of Xaw3d don't allow
6794 updating the thumb size while dragging. Also, even if we can update
6795 its size, the update will often happen too late.
6796 If you don't believe it, check out revision 1.650 of xterm.c to see
6797 what hoops we were going through and the still poor behavior we got. */
6798 portion = XFASTINT (XWINDOW (bar->window)->height) * 30;
6799 /* When the thumb is at the bottom, position == whole.
6800 So we need to increase `whole' to make space for the thumb. */
6801 whole += portion;
6802
6803 if (whole <= 0)
06a2c219
GM
6804 top = 0, shown = 1;
6805 else
f451eb13 6806 {
06a2c219
GM
6807 top = (float) position / whole;
6808 shown = (float) portion / whole;
6809 }
f451eb13 6810
6f2a7a68
SM
6811 if (NILP (bar->dragging))
6812 {
6813 int size, value;
06a2c219 6814
6f2a7a68
SM
6815 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
6816 is the scroll bar's maximum and MIN is the scroll bar's minimum
6817 value. */
6818 size = shown * XM_SB_RANGE;
6819 size = min (size, XM_SB_RANGE);
6820 size = max (size, 1);
06a2c219 6821
6f2a7a68
SM
6822 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
6823 value = top * XM_SB_RANGE;
6824 value = min (value, XM_SB_MAX - size);
6825 value = max (value, XM_SB_MIN);
7d0393cf 6826
06a2c219 6827 XmScrollBarSetValues (widget, value, size, 0, 0, False);
6f2a7a68 6828 }
ec18280f 6829#else /* !USE_MOTIF i.e. use Xaw */
6f2a7a68
SM
6830
6831 if (whole == 0)
6832 top = 0, shown = 1;
6833 else
6834 {
6835 top = (float) position / whole;
6836 shown = (float) portion / whole;
6837 }
6838
06a2c219 6839 {
ec18280f
SM
6840 float old_top, old_shown;
6841 Dimension height;
6842 XtVaGetValues (widget,
6843 XtNtopOfThumb, &old_top,
6844 XtNshown, &old_shown,
6845 XtNheight, &height,
6846 NULL);
6847
6848 /* Massage the top+shown values. */
6849 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
6850 top = max (0, min (1, top));
6851 else
6852 top = old_top;
6853 /* Keep two pixels available for moving the thumb down. */
6854 shown = max (0, min (1 - top - (2.0 / height), shown));
06a2c219
GM
6855
6856 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
6857 check that your system's configuration file contains a define
6858 for `NARROWPROTO'. See s/freebsd.h for an example. */
ec18280f 6859 if (top != old_top || shown != old_shown)
eb393530 6860 {
ec18280f 6861 if (NILP (bar->dragging))
eb393530 6862 XawScrollbarSetThumb (widget, top, shown);
06a2c219
GM
6863 else
6864 {
ec18280f
SM
6865#ifdef HAVE_XAW3D
6866 ScrollbarWidget sb = (ScrollbarWidget) widget;
3e71d8f2 6867 int scroll_mode = 0;
7d0393cf 6868
ec18280f
SM
6869 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
6870 if (xaw3d_arrow_scroll)
6871 {
6872 /* Xaw3d stupidly ignores resize requests while dragging
6873 so we have to make it believe it's not in dragging mode. */
6874 scroll_mode = sb->scrollbar.scroll_mode;
6875 if (scroll_mode == 2)
6876 sb->scrollbar.scroll_mode = 0;
6877 }
6878#endif
6879 /* Try to make the scrolling a tad smoother. */
6880 if (!xaw3d_pick_top)
6881 shown = min (shown, old_shown);
7d0393cf 6882
ec18280f 6883 XawScrollbarSetThumb (widget, top, shown);
7d0393cf 6884
ec18280f
SM
6885#ifdef HAVE_XAW3D
6886 if (xaw3d_arrow_scroll && scroll_mode == 2)
6887 sb->scrollbar.scroll_mode = scroll_mode;
6888#endif
06a2c219 6889 }
06a2c219
GM
6890 }
6891 }
ec18280f 6892#endif /* !USE_MOTIF */
06a2c219
GM
6893
6894 UNBLOCK_INPUT;
f451eb13 6895}
488dd4c4 6896#endif /* not USE_GTK */
f451eb13 6897
06a2c219
GM
6898#endif /* USE_TOOLKIT_SCROLL_BARS */
6899
6900
6901\f
6902/************************************************************************
6903 Scroll bars, general
6904 ************************************************************************/
7d0393cf 6905
06a2c219
GM
6906/* Create a scroll bar and return the scroll bar vector for it. W is
6907 the Emacs window on which to create the scroll bar. TOP, LEFT,
f7ccf929 6908 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
06a2c219
GM
6909 scroll bar. */
6910
ab648270 6911static struct scroll_bar *
06a2c219
GM
6912x_scroll_bar_create (w, top, left, width, height)
6913 struct window *w;
f451eb13
JB
6914 int top, left, width, height;
6915{
06a2c219 6916 struct frame *f = XFRAME (w->frame);
334208b7
RS
6917 struct scroll_bar *bar
6918 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
f451eb13
JB
6919
6920 BLOCK_INPUT;
6921
eccc05db 6922#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
6923 x_create_toolkit_scroll_bar (f, bar);
6924#else /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
6925 {
6926 XSetWindowAttributes a;
6927 unsigned long mask;
5c187dee 6928 Window window;
06a2c219
GM
6929
6930 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
6931 if (a.background_pixel == -1)
6932 a.background_pixel = f->output_data.x->background_pixel;
7d0393cf 6933
12ba150f 6934 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9a572e2a 6935 | ButtonMotionMask | PointerMotionHintMask
12ba150f 6936 | ExposureMask);
7a13e894 6937 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
f451eb13 6938
dbc4e1c1 6939 mask = (CWBackPixel | CWEventMask | CWCursor);
f451eb13 6940
06a2c219
GM
6941 /* Clear the area of W that will serve as a scroll bar. This is
6942 for the case that a window has been split horizontally. In
6943 this case, no clear_frame is generated to reduce flickering. */
7b49b9d2
GM
6944 if (width > 0 && height > 0)
6945 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6946 left, top, width,
6947 window_box_height (w), False);
06a2c219
GM
6948
6949 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6950 /* Position and size of scroll bar. */
6951 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
6952 top,
6953 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
6954 height,
6955 /* Border width, depth, class, and visual. */
6956 0,
6957 CopyFromParent,
6958 CopyFromParent,
6959 CopyFromParent,
6960 /* Attributes. */
6961 mask, &a);
6962 SET_SCROLL_BAR_X_WINDOW (bar, window);
f451eb13 6963 }
06a2c219 6964#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 6965
06a2c219 6966 XSETWINDOW (bar->window, w);
e0c1aef2
KH
6967 XSETINT (bar->top, top);
6968 XSETINT (bar->left, left);
6969 XSETINT (bar->width, width);
6970 XSETINT (bar->height, height);
6971 XSETINT (bar->start, 0);
6972 XSETINT (bar->end, 0);
12ba150f 6973 bar->dragging = Qnil;
f451eb13
JB
6974
6975 /* Add bar to its frame's list of scroll bars. */
334208b7 6976 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 6977 bar->prev = Qnil;
334208b7 6978 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
06a2c219 6979 if (!NILP (bar->next))
e0c1aef2 6980 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13 6981
06a2c219 6982 /* Map the window/widget. */
eccc05db 6983#ifdef USE_TOOLKIT_SCROLL_BARS
f964b4d7 6984 {
488dd4c4
JD
6985#ifdef USE_GTK
6986 xg_update_scrollbar_pos (f,
6987 SCROLL_BAR_X_WINDOW (bar),
6988 top,
6989 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
6990 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
6991 max (height, 1));
6992 xg_show_scroll_bar (SCROLL_BAR_X_WINDOW (bar));
6993#else /* not USE_GTK */
f964b4d7
GM
6994 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
6995 XtConfigureWidget (scroll_bar,
6996 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
6997 top,
6998 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
6999 max (height, 1), 0);
7000 XtMapWidget (scroll_bar);
488dd4c4 7001#endif /* not USE_GTK */
f964b4d7 7002 }
06a2c219 7003#else /* not USE_TOOLKIT_SCROLL_BARS */
7f9c7f94 7004 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
06a2c219 7005#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
7006
7007 UNBLOCK_INPUT;
12ba150f 7008 return bar;
f451eb13
JB
7009}
7010
06a2c219 7011
12ba150f 7012/* Draw BAR's handle in the proper position.
7d0393cf 7013
12ba150f
JB
7014 If the handle is already drawn from START to END, don't bother
7015 redrawing it, unless REBUILD is non-zero; in that case, always
7016 redraw it. (REBUILD is handy for drawing the handle after expose
58769bee 7017 events.)
12ba150f
JB
7018
7019 Normally, we want to constrain the start and end of the handle to
06a2c219
GM
7020 fit inside its rectangle, but if the user is dragging the scroll
7021 bar handle, we want to let them drag it down all the way, so that
7022 the bar's top is as far down as it goes; otherwise, there's no way
7023 to move to the very end of the buffer. */
7024
5c187dee
GM
7025#ifndef USE_TOOLKIT_SCROLL_BARS
7026
f451eb13 7027static void
ab648270
JB
7028x_scroll_bar_set_handle (bar, start, end, rebuild)
7029 struct scroll_bar *bar;
f451eb13 7030 int start, end;
12ba150f 7031 int rebuild;
f451eb13 7032{
12ba150f 7033 int dragging = ! NILP (bar->dragging);
ab648270 7034 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 7035 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 7036 GC gc = f->output_data.x->normal_gc;
12ba150f
JB
7037
7038 /* If the display is already accurate, do nothing. */
7039 if (! rebuild
7040 && start == XINT (bar->start)
7041 && end == XINT (bar->end))
7042 return;
7043
f451eb13
JB
7044 BLOCK_INPUT;
7045
7046 {
d9cdbb3d
RS
7047 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
7048 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
7049 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
f451eb13
JB
7050
7051 /* Make sure the values are reasonable, and try to preserve
7052 the distance between start and end. */
12ba150f
JB
7053 {
7054 int length = end - start;
7055
7056 if (start < 0)
7057 start = 0;
7058 else if (start > top_range)
7059 start = top_range;
7060 end = start + length;
7061
7062 if (end < start)
7063 end = start;
7064 else if (end > top_range && ! dragging)
7065 end = top_range;
7066 }
f451eb13 7067
ab648270 7068 /* Store the adjusted setting in the scroll bar. */
e0c1aef2
KH
7069 XSETINT (bar->start, start);
7070 XSETINT (bar->end, end);
f451eb13 7071
12ba150f
JB
7072 /* Clip the end position, just for display. */
7073 if (end > top_range)
7074 end = top_range;
f451eb13 7075
ab648270 7076 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
12ba150f
JB
7077 below top positions, to make sure the handle is always at least
7078 that many pixels tall. */
ab648270 7079 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
f451eb13 7080
12ba150f
JB
7081 /* Draw the empty space above the handle. Note that we can't clear
7082 zero-height areas; that means "clear to end of window." */
7083 if (0 < start)
c5e6e06b
GM
7084 x_clear_area (FRAME_X_DISPLAY (f), w,
7085 /* x, y, width, height, and exposures. */
7086 VERTICAL_SCROLL_BAR_LEFT_BORDER,
7087 VERTICAL_SCROLL_BAR_TOP_BORDER,
7088 inside_width, start,
7089 False);
f451eb13 7090
06a2c219
GM
7091 /* Change to proper foreground color if one is specified. */
7092 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
7093 XSetForeground (FRAME_X_DISPLAY (f), gc,
7094 f->output_data.x->scroll_bar_foreground_pixel);
7095
12ba150f 7096 /* Draw the handle itself. */
334208b7 7097 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
12ba150f 7098 /* x, y, width, height */
ab648270
JB
7099 VERTICAL_SCROLL_BAR_LEFT_BORDER,
7100 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
12ba150f 7101 inside_width, end - start);
f451eb13 7102
06a2c219
GM
7103 /* Restore the foreground color of the GC if we changed it above. */
7104 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
7105 XSetForeground (FRAME_X_DISPLAY (f), gc,
7106 f->output_data.x->foreground_pixel);
f451eb13 7107
12ba150f
JB
7108 /* Draw the empty space below the handle. Note that we can't
7109 clear zero-height areas; that means "clear to end of window." */
7110 if (end < inside_height)
c5e6e06b
GM
7111 x_clear_area (FRAME_X_DISPLAY (f), w,
7112 /* x, y, width, height, and exposures. */
7113 VERTICAL_SCROLL_BAR_LEFT_BORDER,
7114 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
7115 inside_width, inside_height - end,
7116 False);
f451eb13 7117
f451eb13
JB
7118 }
7119
f451eb13
JB
7120 UNBLOCK_INPUT;
7121}
7122
5c187dee 7123#endif /* !USE_TOOLKIT_SCROLL_BARS */
f451eb13 7124
06a2c219
GM
7125/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
7126 nil. */
58769bee 7127
12ba150f 7128static void
ab648270
JB
7129x_scroll_bar_remove (bar)
7130 struct scroll_bar *bar;
12ba150f 7131{
e83dc917 7132 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
7133 BLOCK_INPUT;
7134
eccc05db 7135#ifdef USE_TOOLKIT_SCROLL_BARS
488dd4c4
JD
7136#ifdef USE_GTK
7137 xg_remove_scroll_bar (f, SCROLL_BAR_X_WINDOW (bar));
7138#else /* not USE_GTK */
e83dc917 7139 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
488dd4c4 7140#endif /* not USE_GTK */
e83dc917
GM
7141#else
7142 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
7143#endif
7d0393cf 7144
ab648270
JB
7145 /* Disassociate this scroll bar from its window. */
7146 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
12ba150f
JB
7147
7148 UNBLOCK_INPUT;
7149}
7150
06a2c219 7151
12ba150f
JB
7152/* Set the handle of the vertical scroll bar for WINDOW to indicate
7153 that we are displaying PORTION characters out of a total of WHOLE
ab648270 7154 characters, starting at POSITION. If WINDOW has no scroll bar,
12ba150f 7155 create one. */
06a2c219 7156
12ba150f 7157static void
06a2c219
GM
7158XTset_vertical_scroll_bar (w, portion, whole, position)
7159 struct window *w;
f451eb13
JB
7160 int portion, whole, position;
7161{
06a2c219 7162 struct frame *f = XFRAME (w->frame);
ab648270 7163 struct scroll_bar *bar;
3c6ede7b 7164 int top, height, left, sb_left, width, sb_width;
06a2c219 7165 int window_x, window_y, window_width, window_height;
06a2c219 7166
3c6ede7b 7167 /* Get window dimensions. */
06a2c219 7168 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
3c6ede7b
GM
7169 top = window_y;
7170 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
7171 height = window_height;
06a2c219 7172
3c6ede7b 7173 /* Compute the left edge of the scroll bar area. */
06a2c219 7174 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
3c6ede7b
GM
7175 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
7176 else
7177 left = XFASTINT (w->left);
7178 left *= CANON_X_UNIT (f);
7179 left += FRAME_INTERNAL_BORDER_WIDTH (f);
7180
7181 /* Compute the width of the scroll bar which might be less than
7182 the width of the area reserved for the scroll bar. */
7183 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
7184 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
06a2c219 7185 else
3c6ede7b 7186 sb_width = width;
12ba150f 7187
3c6ede7b
GM
7188 /* Compute the left edge of the scroll bar. */
7189#ifdef USE_TOOLKIT_SCROLL_BARS
7190 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
7d0393cf 7191 sb_left = left + width - sb_width - (width - sb_width) / 2;
3c6ede7b
GM
7192 else
7193 sb_left = left + (width - sb_width) / 2;
7194#else
7195 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
7d0393cf 7196 sb_left = left + width - sb_width;
3c6ede7b
GM
7197 else
7198 sb_left = left;
7199#endif
7d0393cf 7200
ab648270 7201 /* Does the scroll bar exist yet? */
06a2c219 7202 if (NILP (w->vertical_scroll_bar))
3c6ede7b 7203 {
7b49b9d2 7204 if (width > 0 && height > 0)
b547b6e8
GM
7205 {
7206 BLOCK_INPUT;
7207 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7208 left, top, width, height, False);
7209 UNBLOCK_INPUT;
7210 }
7d0393cf 7211
3c6ede7b
GM
7212 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
7213 }
f451eb13 7214 else
12ba150f
JB
7215 {
7216 /* It may just need to be moved and resized. */
06a2c219 7217 unsigned int mask = 0;
7d0393cf 7218
06a2c219
GM
7219 bar = XSCROLL_BAR (w->vertical_scroll_bar);
7220
7221 BLOCK_INPUT;
7222
3c6ede7b 7223 if (sb_left != XINT (bar->left))
06a2c219 7224 mask |= CWX;
3c6ede7b 7225 if (top != XINT (bar->top))
06a2c219 7226 mask |= CWY;
3c6ede7b 7227 if (sb_width != XINT (bar->width))
06a2c219 7228 mask |= CWWidth;
7d0393cf 7229 if (height != XINT (bar->height))
06a2c219 7230 mask |= CWHeight;
7d0393cf 7231
06a2c219 7232#ifdef USE_TOOLKIT_SCROLL_BARS
fe6f39d9 7233
488dd4c4
JD
7234#ifdef USE_GTK
7235 if (mask)
7236 xg_update_scrollbar_pos (f,
7237 SCROLL_BAR_X_WINDOW (bar),
7238 top,
7239 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7240 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
7241 max (height, 1));
7242#else /* not USE_GTK */
7243
fe6f39d9 7244 /* Since toolkit scroll bars are smaller than the space reserved
488dd4c4 7245 for them on the frame, we have to clear "under" them. */
7b49b9d2 7246 if (width > 0 && height > 0)
488dd4c4
JD
7247 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7248 left, top, width, height, False);
06a2c219
GM
7249 /* Move/size the scroll bar widget. */
7250 if (mask)
488dd4c4
JD
7251 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
7252 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7253 top,
7254 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
7255 max (height, 1), 0);
06a2c219 7256
488dd4c4 7257#endif /* not USE_GTK */
06a2c219 7258#else /* not USE_TOOLKIT_SCROLL_BARS */
7d0393cf 7259
357e7376
GM
7260 /* Clear areas not covered by the scroll bar because of
7261 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
e1f6572f
RS
7262 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
7263 {
c5e6e06b
GM
7264 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7265 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7266 height, False);
7267 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7268 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7269 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7270 height, False);
e1f6572f 7271 }
357e7376
GM
7272
7273 /* Clear areas not covered by the scroll bar because it's not as
f7ccf929 7274 wide as the area reserved for it. This makes sure a
357e7376
GM
7275 previous mode line display is cleared after C-x 2 C-x 1, for
7276 example. */
7277 {
7278 int area_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
7279 int rest = area_width - sb_width;
38d2af0c
GM
7280 if (rest > 0 && height > 0)
7281 {
7282 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
7283 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7284 left + area_width - rest, top,
7285 rest, height, False);
7286 else
7287 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7288 left, top, rest, height, False);
7289 }
357e7376 7290 }
7d0393cf 7291
06a2c219
GM
7292 /* Move/size the scroll bar window. */
7293 if (mask)
7294 {
7295 XWindowChanges wc;
7d0393cf 7296
3c6ede7b
GM
7297 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
7298 wc.y = top;
7299 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
7300 wc.height = height;
06a2c219
GM
7301 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
7302 mask, &wc);
7303 }
7d0393cf 7304
06a2c219
GM
7305#endif /* not USE_TOOLKIT_SCROLL_BARS */
7306
7307 /* Remember new settings. */
3c6ede7b
GM
7308 XSETINT (bar->left, sb_left);
7309 XSETINT (bar->top, top);
7310 XSETINT (bar->width, sb_width);
7311 XSETINT (bar->height, height);
7d0393cf 7312
06a2c219 7313 UNBLOCK_INPUT;
12ba150f 7314 }
f451eb13 7315
eccc05db 7316#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
7317 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
7318#else /* not USE_TOOLKIT_SCROLL_BARS */
ab648270 7319 /* Set the scroll bar's current state, unless we're currently being
f451eb13 7320 dragged. */
12ba150f 7321 if (NILP (bar->dragging))
f451eb13 7322 {
92857db0 7323 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
f451eb13 7324
12ba150f 7325 if (whole == 0)
ab648270 7326 x_scroll_bar_set_handle (bar, 0, top_range, 0);
12ba150f
JB
7327 else
7328 {
43f868f5
JB
7329 int start = ((double) position * top_range) / whole;
7330 int end = ((double) (position + portion) * top_range) / whole;
ab648270 7331 x_scroll_bar_set_handle (bar, start, end, 0);
12ba150f 7332 }
f451eb13 7333 }
06a2c219 7334#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 7335
06a2c219 7336 XSETVECTOR (w->vertical_scroll_bar, bar);
f451eb13
JB
7337}
7338
12ba150f 7339
f451eb13 7340/* The following three hooks are used when we're doing a thorough
ab648270 7341 redisplay of the frame. We don't explicitly know which scroll bars
f451eb13 7342 are going to be deleted, because keeping track of when windows go
12ba150f
JB
7343 away is a real pain - "Can you say set-window-configuration, boys
7344 and girls?" Instead, we just assert at the beginning of redisplay
ab648270 7345 that *all* scroll bars are to be removed, and then save a scroll bar
12ba150f 7346 from the fiery pit when we actually redisplay its window. */
f451eb13 7347
ab648270
JB
7348/* Arrange for all scroll bars on FRAME to be removed at the next call
7349 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
06a2c219
GM
7350 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
7351
58769bee 7352static void
ab648270 7353XTcondemn_scroll_bars (frame)
f451eb13
JB
7354 FRAME_PTR frame;
7355{
f9e24cb9
RS
7356 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
7357 while (! NILP (FRAME_SCROLL_BARS (frame)))
7358 {
7359 Lisp_Object bar;
7360 bar = FRAME_SCROLL_BARS (frame);
7361 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
7362 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
7363 XSCROLL_BAR (bar)->prev = Qnil;
7364 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
7365 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
7366 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
7367 }
f451eb13
JB
7368}
7369
fa2dfc30 7370
06a2c219 7371/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
12ba150f 7372 Note that WINDOW isn't necessarily condemned at all. */
fa2dfc30 7373
f451eb13 7374static void
ab648270 7375XTredeem_scroll_bar (window)
12ba150f 7376 struct window *window;
f451eb13 7377{
ab648270 7378 struct scroll_bar *bar;
fa2dfc30 7379 struct frame *f;
12ba150f 7380
ab648270
JB
7381 /* We can't redeem this window's scroll bar if it doesn't have one. */
7382 if (NILP (window->vertical_scroll_bar))
12ba150f
JB
7383 abort ();
7384
ab648270 7385 bar = XSCROLL_BAR (window->vertical_scroll_bar);
12ba150f
JB
7386
7387 /* Unlink it from the condemned list. */
fa2dfc30
GM
7388 f = XFRAME (WINDOW_FRAME (window));
7389 if (NILP (bar->prev))
7390 {
7391 /* If the prev pointer is nil, it must be the first in one of
7392 the lists. */
7393 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
7394 /* It's not condemned. Everything's fine. */
7395 return;
7396 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
7397 window->vertical_scroll_bar))
7398 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
7399 else
7400 /* If its prev pointer is nil, it must be at the front of
7401 one or the other! */
7402 abort ();
7403 }
7404 else
7405 XSCROLL_BAR (bar->prev)->next = bar->next;
12ba150f 7406
fa2dfc30
GM
7407 if (! NILP (bar->next))
7408 XSCROLL_BAR (bar->next)->prev = bar->prev;
12ba150f 7409
fa2dfc30
GM
7410 bar->next = FRAME_SCROLL_BARS (f);
7411 bar->prev = Qnil;
7412 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
7413 if (! NILP (bar->next))
7414 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13
JB
7415}
7416
ab648270
JB
7417/* Remove all scroll bars on FRAME that haven't been saved since the
7418 last call to `*condemn_scroll_bars_hook'. */
06a2c219 7419
f451eb13 7420static void
ab648270 7421XTjudge_scroll_bars (f)
12ba150f 7422 FRAME_PTR f;
f451eb13 7423{
12ba150f 7424 Lisp_Object bar, next;
f451eb13 7425
ab648270 7426 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
cf7cb199
JB
7427
7428 /* Clear out the condemned list now so we won't try to process any
ab648270
JB
7429 more events on the hapless scroll bars. */
7430 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
cf7cb199
JB
7431
7432 for (; ! NILP (bar); bar = next)
f451eb13 7433 {
ab648270 7434 struct scroll_bar *b = XSCROLL_BAR (bar);
12ba150f 7435
ab648270 7436 x_scroll_bar_remove (b);
12ba150f
JB
7437
7438 next = b->next;
7439 b->next = b->prev = Qnil;
f451eb13 7440 }
12ba150f 7441
ab648270 7442 /* Now there should be no references to the condemned scroll bars,
12ba150f 7443 and they should get garbage-collected. */
f451eb13
JB
7444}
7445
7446
06a2c219
GM
7447/* Handle an Expose or GraphicsExpose event on a scroll bar. This
7448 is a no-op when using toolkit scroll bars.
ab648270
JB
7449
7450 This may be called from a signal handler, so we have to ignore GC
7451 mark bits. */
06a2c219 7452
f451eb13 7453static void
ab648270
JB
7454x_scroll_bar_expose (bar, event)
7455 struct scroll_bar *bar;
f451eb13
JB
7456 XEvent *event;
7457{
06a2c219
GM
7458#ifndef USE_TOOLKIT_SCROLL_BARS
7459
ab648270 7460 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 7461 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 7462 GC gc = f->output_data.x->normal_gc;
3cbd2e0b 7463 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
12ba150f 7464
f451eb13
JB
7465 BLOCK_INPUT;
7466
ab648270 7467 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
f451eb13 7468
06a2c219 7469 /* Draw a one-pixel border just inside the edges of the scroll bar. */
334208b7 7470 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13
JB
7471
7472 /* x, y, width, height */
d9cdbb3d 7473 0, 0,
3cbd2e0b 7474 XINT (bar->width) - 1 - width_trim - width_trim,
d9cdbb3d 7475 XINT (bar->height) - 1);
7d0393cf 7476
f451eb13 7477 UNBLOCK_INPUT;
06a2c219
GM
7478
7479#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
7480}
7481
ab648270 7482/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
3b8f9651 7483 is set to something other than NO_EVENT, it is enqueued.
ab648270
JB
7484
7485 This may be called from a signal handler, so we have to ignore GC
7486 mark bits. */
06a2c219 7487
5c187dee 7488
f451eb13 7489static void
ab648270
JB
7490x_scroll_bar_handle_click (bar, event, emacs_event)
7491 struct scroll_bar *bar;
f451eb13
JB
7492 XEvent *event;
7493 struct input_event *emacs_event;
7494{
0299d313 7495 if (! GC_WINDOWP (bar->window))
12ba150f
JB
7496 abort ();
7497
3b8f9651 7498 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
69388238 7499 emacs_event->code = event->xbutton.button - Button1;
0299d313 7500 emacs_event->modifiers
7d0393cf 7501 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
0299d313
RS
7502 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
7503 event->xbutton.state)
7504 | (event->type == ButtonRelease
7505 ? up_modifier
7506 : down_modifier));
12ba150f 7507 emacs_event->frame_or_window = bar->window;
0f8aabe9 7508 emacs_event->arg = Qnil;
f451eb13 7509 emacs_event->timestamp = event->xbutton.time;
12ba150f 7510 {
06a2c219 7511#if 0
d9cdbb3d 7512 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
0299d313 7513 int internal_height
d9cdbb3d 7514 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 7515#endif
0299d313 7516 int top_range
d9cdbb3d 7517 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ab648270 7518 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
12ba150f
JB
7519
7520 if (y < 0) y = 0;
7521 if (y > top_range) y = top_range;
7522
7523 if (y < XINT (bar->start))
ab648270
JB
7524 emacs_event->part = scroll_bar_above_handle;
7525 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
7526 emacs_event->part = scroll_bar_handle;
12ba150f 7527 else
ab648270 7528 emacs_event->part = scroll_bar_below_handle;
929787e1
JB
7529
7530 /* Just because the user has clicked on the handle doesn't mean
5116f055
JB
7531 they want to drag it. Lisp code needs to be able to decide
7532 whether or not we're dragging. */
929787e1 7533#if 0
12ba150f
JB
7534 /* If the user has just clicked on the handle, record where they're
7535 holding it. */
7536 if (event->type == ButtonPress
ab648270 7537 && emacs_event->part == scroll_bar_handle)
e0c1aef2 7538 XSETINT (bar->dragging, y - XINT (bar->start));
929787e1 7539#endif
12ba150f 7540
257f40f2 7541#ifndef USE_TOOLKIT_SCROLL_BARS
12ba150f
JB
7542 /* If the user has released the handle, set it to its final position. */
7543 if (event->type == ButtonRelease
7544 && ! NILP (bar->dragging))
7545 {
7546 int new_start = y - XINT (bar->dragging);
7547 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
f451eb13 7548
ab648270 7549 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
12ba150f
JB
7550 bar->dragging = Qnil;
7551 }
257f40f2 7552#endif
f451eb13 7553
5116f055
JB
7554 /* Same deal here as the other #if 0. */
7555#if 0
58769bee 7556 /* Clicks on the handle are always reported as occurring at the top of
12ba150f 7557 the handle. */
ab648270 7558 if (emacs_event->part == scroll_bar_handle)
12ba150f
JB
7559 emacs_event->x = bar->start;
7560 else
e0c1aef2 7561 XSETINT (emacs_event->x, y);
5116f055 7562#else
e0c1aef2 7563 XSETINT (emacs_event->x, y);
5116f055 7564#endif
f451eb13 7565
e0c1aef2 7566 XSETINT (emacs_event->y, top_range);
12ba150f
JB
7567 }
7568}
f451eb13 7569
257f40f2
JD
7570#ifndef USE_TOOLKIT_SCROLL_BARS
7571
ab648270
JB
7572/* Handle some mouse motion while someone is dragging the scroll bar.
7573
7574 This may be called from a signal handler, so we have to ignore GC
7575 mark bits. */
06a2c219 7576
f451eb13 7577static void
ab648270
JB
7578x_scroll_bar_note_movement (bar, event)
7579 struct scroll_bar *bar;
f451eb13
JB
7580 XEvent *event;
7581{
39d8bb4d
KH
7582 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
7583
f451eb13
JB
7584 last_mouse_movement_time = event->xmotion.time;
7585
39d8bb4d 7586 f->mouse_moved = 1;
e0c1aef2 7587 XSETVECTOR (last_mouse_scroll_bar, bar);
f451eb13
JB
7588
7589 /* If we're dragging the bar, display it. */
ab648270 7590 if (! GC_NILP (bar->dragging))
f451eb13
JB
7591 {
7592 /* Where should the handle be now? */
12ba150f 7593 int new_start = event->xmotion.y - XINT (bar->dragging);
f451eb13 7594
12ba150f 7595 if (new_start != XINT (bar->start))
f451eb13 7596 {
12ba150f 7597 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
58769bee 7598
ab648270 7599 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
f451eb13
JB
7600 }
7601 }
f451eb13
JB
7602}
7603
5c187dee
GM
7604#endif /* !USE_TOOLKIT_SCROLL_BARS */
7605
12ba150f 7606/* Return information to the user about the current position of the mouse
ab648270 7607 on the scroll bar. */
06a2c219 7608
12ba150f 7609static void
334208b7
RS
7610x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
7611 FRAME_PTR *fp;
12ba150f 7612 Lisp_Object *bar_window;
ab648270 7613 enum scroll_bar_part *part;
12ba150f
JB
7614 Lisp_Object *x, *y;
7615 unsigned long *time;
7616{
ab648270 7617 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
334208b7
RS
7618 Window w = SCROLL_BAR_X_WINDOW (bar);
7619 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f 7620 int win_x, win_y;
559cb2fb
JB
7621 Window dummy_window;
7622 int dummy_coord;
7623 unsigned int dummy_mask;
12ba150f 7624
cf7cb199
JB
7625 BLOCK_INPUT;
7626
ab648270 7627 /* Get the mouse's position relative to the scroll bar window, and
12ba150f 7628 report that. */
334208b7 7629 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
12ba150f 7630
559cb2fb
JB
7631 /* Root, child, root x and root y. */
7632 &dummy_window, &dummy_window,
7633 &dummy_coord, &dummy_coord,
12ba150f 7634
559cb2fb
JB
7635 /* Position relative to scroll bar. */
7636 &win_x, &win_y,
12ba150f 7637
559cb2fb
JB
7638 /* Mouse buttons and modifier keys. */
7639 &dummy_mask))
7a13e894 7640 ;
559cb2fb
JB
7641 else
7642 {
06a2c219 7643#if 0
559cb2fb 7644 int inside_height
d9cdbb3d 7645 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 7646#endif
559cb2fb 7647 int top_range
d9cdbb3d 7648 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
559cb2fb
JB
7649
7650 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
7651
7652 if (! NILP (bar->dragging))
7653 win_y -= XINT (bar->dragging);
7654
7655 if (win_y < 0)
7656 win_y = 0;
7657 if (win_y > top_range)
7658 win_y = top_range;
7659
334208b7 7660 *fp = f;
7a13e894 7661 *bar_window = bar->window;
559cb2fb
JB
7662
7663 if (! NILP (bar->dragging))
7664 *part = scroll_bar_handle;
7665 else if (win_y < XINT (bar->start))
7666 *part = scroll_bar_above_handle;
7667 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
7668 *part = scroll_bar_handle;
7669 else
7670 *part = scroll_bar_below_handle;
12ba150f 7671
e0c1aef2
KH
7672 XSETINT (*x, win_y);
7673 XSETINT (*y, top_range);
12ba150f 7674
39d8bb4d 7675 f->mouse_moved = 0;
559cb2fb
JB
7676 last_mouse_scroll_bar = Qnil;
7677 }
12ba150f 7678
559cb2fb 7679 *time = last_mouse_movement_time;
cf7cb199 7680
cf7cb199 7681 UNBLOCK_INPUT;
12ba150f
JB
7682}
7683
f451eb13 7684
dbc4e1c1 7685/* The screen has been cleared so we may have changed foreground or
ab648270
JB
7686 background colors, and the scroll bars may need to be redrawn.
7687 Clear out the scroll bars, and ask for expose events, so we can
dbc4e1c1
JB
7688 redraw them. */
7689
dfcf069d 7690void
ab648270 7691x_scroll_bar_clear (f)
dbc4e1c1
JB
7692 FRAME_PTR f;
7693{
06a2c219 7694#ifndef USE_TOOLKIT_SCROLL_BARS
dbc4e1c1
JB
7695 Lisp_Object bar;
7696
b80c363e
RS
7697 /* We can have scroll bars even if this is 0,
7698 if we just turned off scroll bar mode.
7699 But in that case we should not clear them. */
7700 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
7701 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
7702 bar = XSCROLL_BAR (bar)->next)
c5e6e06b
GM
7703 XClearArea (FRAME_X_DISPLAY (f),
7704 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
b80c363e 7705 0, 0, 0, 0, True);
06a2c219 7706#endif /* not USE_TOOLKIT_SCROLL_BARS */
dbc4e1c1
JB
7707}
7708
09756a85
RS
7709\f
7710/* Define a queue to save up SelectionRequest events for later handling. */
7711
7712struct selection_event_queue
7713 {
7714 XEvent event;
7715 struct selection_event_queue *next;
7716 };
7717
7718static struct selection_event_queue *queue;
7719
7720/* Nonzero means queue up certain events--don't process them yet. */
06a2c219 7721
09756a85
RS
7722static int x_queue_selection_requests;
7723
7724/* Queue up an X event *EVENT, to be processed later. */
dbc4e1c1 7725
09756a85 7726static void
334208b7
RS
7727x_queue_event (f, event)
7728 FRAME_PTR f;
09756a85
RS
7729 XEvent *event;
7730{
7731 struct selection_event_queue *queue_tmp
06a2c219 7732 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
09756a85 7733
58769bee 7734 if (queue_tmp != NULL)
09756a85
RS
7735 {
7736 queue_tmp->event = *event;
7737 queue_tmp->next = queue;
7738 queue = queue_tmp;
7739 }
7740}
7741
7742/* Take all the queued events and put them back
7743 so that they get processed afresh. */
7744
7745static void
db3906fd
RS
7746x_unqueue_events (display)
7747 Display *display;
09756a85 7748{
58769bee 7749 while (queue != NULL)
09756a85
RS
7750 {
7751 struct selection_event_queue *queue_tmp = queue;
db3906fd 7752 XPutBackEvent (display, &queue_tmp->event);
09756a85 7753 queue = queue_tmp->next;
06a2c219 7754 xfree ((char *)queue_tmp);
09756a85
RS
7755 }
7756}
7757
7758/* Start queuing SelectionRequest events. */
7759
7760void
db3906fd
RS
7761x_start_queuing_selection_requests (display)
7762 Display *display;
09756a85
RS
7763{
7764 x_queue_selection_requests++;
7765}
7766
7767/* Stop queuing SelectionRequest events. */
7768
7769void
db3906fd
RS
7770x_stop_queuing_selection_requests (display)
7771 Display *display;
09756a85
RS
7772{
7773 x_queue_selection_requests--;
db3906fd 7774 x_unqueue_events (display);
09756a85 7775}
f451eb13
JB
7776\f
7777/* The main X event-reading loop - XTread_socket. */
dc6f92b8 7778
042c33d3 7779#if 0
06a2c219 7780/* Time stamp of enter window event. This is only used by XTread_socket,
dc6f92b8
JB
7781 but we have to put it out here, since static variables within functions
7782 sometimes don't work. */
06a2c219 7783
dc6f92b8 7784static Time enter_timestamp;
042c33d3 7785#endif
dc6f92b8 7786
11edeb03 7787/* This holds the state XLookupString needs to implement dead keys
58769bee 7788 and other tricks known as "compose processing". _X Window System_
11edeb03
JB
7789 says that a portable program can't use this, but Stephen Gildea assures
7790 me that letting the compiler initialize it to zeros will work okay.
7791
7792 This must be defined outside of XTread_socket, for the same reasons
f7d40b3b 7793 given for enter_timestamp, above. */
06a2c219 7794
11edeb03
JB
7795static XComposeStatus compose_status;
7796
10e6549c
RS
7797/* Record the last 100 characters stored
7798 to help debug the loss-of-chars-during-GC problem. */
06a2c219 7799
2224b905
RS
7800static int temp_index;
7801static short temp_buffer[100];
10e6549c 7802
7a13e894
RS
7803/* Set this to nonzero to fake an "X I/O error"
7804 on a particular display. */
06a2c219 7805
7a13e894
RS
7806struct x_display_info *XTread_socket_fake_io_error;
7807
2224b905
RS
7808/* When we find no input here, we occasionally do a no-op command
7809 to verify that the X server is still running and we can still talk with it.
7810 We try all the open displays, one by one.
7811 This variable is used for cycling thru the displays. */
06a2c219 7812
2224b905
RS
7813static struct x_display_info *next_noop_dpyinfo;
7814
06a2c219
GM
7815#define SET_SAVED_MENU_EVENT(size) \
7816 do \
7817 { \
7818 if (f->output_data.x->saved_menu_event == 0) \
7819 f->output_data.x->saved_menu_event \
7820 = (XEvent *) xmalloc (sizeof (XEvent)); \
7821 bcopy (&event, f->output_data.x->saved_menu_event, size); \
7822 if (numchars >= 1) \
7823 { \
3b8f9651 7824 bufp->kind = MENU_BAR_ACTIVATE_EVENT; \
06a2c219 7825 XSETFRAME (bufp->frame_or_window, f); \
0f8aabe9 7826 bufp->arg = Qnil; \
06a2c219
GM
7827 bufp++; \
7828 count++; \
7829 numchars--; \
7830 } \
7831 } \
7832 while (0)
7833
8805890a 7834#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
06a2c219 7835#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8805890a 7836
dc6f92b8 7837
bf338245
JD
7838enum
7839{
7840 X_EVENT_NORMAL,
7841 X_EVENT_GOTO_OUT,
7842 X_EVENT_DROP
7843};
dc6f92b8 7844
1fcfb866
JD
7845/* Filter events for the current X input method.
7846 DPYINFO is the display this event is for.
7847 EVENT is the X event to filter.
7848
7849 Returns non-zero if the event was filtered, caller shall not process
7850 this event further.
7851 Returns zero if event is wasn't filtered. */
177c0ea7 7852
1fcfb866
JD
7853#ifdef HAVE_X_I18N
7854static int
7855x_filter_event (dpyinfo, event)
7856 struct x_display_info *dpyinfo;
7857 XEvent *event;
7858{
7859 /* XFilterEvent returns non-zero if the input method has
7860 consumed the event. We pass the frame's X window to
7861 XFilterEvent because that's the one for which the IC
7862 was created. */
7863
7864 struct frame *f1 = x_any_window_to_frame (dpyinfo,
7865 event->xclient.window);
7866
7867 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
7868}
7869#endif
7870
488dd4c4
JD
7871#ifdef USE_GTK
7872static struct x_display_info *current_dpyinfo;
7873static struct input_event **current_bufp;
7874static int *current_numcharsp;
7875static int current_count;
7876static int current_finish;
7877
7878/* This is the filter function invoked by the GTK event loop.
7879 It is invoked before the XEvent is translated to a GdkEvent,
7880 so we have a chanse to act on the event before GTK. */
7881static GdkFilterReturn
7882event_handler_gdk (gxev, ev, data)
7883 GdkXEvent *gxev;
7884 GdkEvent *ev;
7885 gpointer data;
7886{
7887 XEvent *xev = (XEvent*)gxev;
7888
7889 if (current_numcharsp)
1fcfb866
JD
7890 {
7891#ifdef HAVE_X_I18N
7892 /* Filter events for the current X input method.
7893 GTK calls XFilterEvent but not for key press and release,
7894 so we do it here. */
7895 if (xev->type == KeyPress || xev->type == KeyRelease)
7896 if (x_filter_event (current_dpyinfo, xev))
7897 return GDK_FILTER_REMOVE;
7898#endif
7899 current_count += handle_one_xevent (current_dpyinfo,
7900 xev,
7901 current_bufp,
7902 current_numcharsp,
7903 &current_finish);
7904 }
488dd4c4 7905 else
1fcfb866 7906 current_finish = x_dispatch_event (xev, GDK_DISPLAY ());
488dd4c4
JD
7907
7908 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
7909 return GDK_FILTER_REMOVE;
7910
7911 return GDK_FILTER_CONTINUE;
7912}
7913#endif /* USE_GTK */
7914
7915
bf338245 7916/* Handles the XEvent EVENT on display DPYINFO.
177c0ea7 7917
bf338245
JD
7918 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
7919 *FINISH is zero if caller should continue reading events.
7920 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
7921
7922 Events representing keys are stored in buffer *BUFP_R,
7923 which can hold up to *NUMCHARSP characters.
7924 We return the number of characters stored into the buffer. */
177c0ea7 7925
6f2a7a68 7926static int
bf338245
JD
7927handle_one_xevent (dpyinfo, eventp, bufp_r, numcharsp, finish)
7928 struct x_display_info *dpyinfo;
7929 XEvent *eventp;
7930 /* register */ struct input_event **bufp_r;
7931 /* register */ int *numcharsp;
7932 int *finish;
dc6f92b8
JB
7933{
7934 int count = 0;
7935 int nbytes = 0;
f676886a 7936 struct frame *f;
379b5ac0 7937 struct coding_system coding;
bf338245
JD
7938 struct input_event *bufp = *bufp_r;
7939 int numchars = *numcharsp;
7940 XEvent event = *eventp;
dc6f92b8 7941
bf338245 7942 *finish = X_EVENT_NORMAL;
177c0ea7 7943
bf338245 7944 switch (event.type)
7a13e894 7945 {
bf338245
JD
7946 case ClientMessage:
7947 {
7948 if (event.xclient.message_type
7949 == dpyinfo->Xatom_wm_protocols
7950 && event.xclient.format == 32)
7951 {
7952 if (event.xclient.data.l[0]
7953 == dpyinfo->Xatom_wm_take_focus)
7954 {
7955 /* Use x_any_window_to_frame because this
7956 could be the shell widget window
7957 if the frame has no title bar. */
7958 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6c183ba5 7959#ifdef HAVE_X_I18N
bf338245
JD
7960 /* Not quite sure this is needed -pd */
7961 if (f && FRAME_XIC (f))
7962 XSetICFocus (FRAME_XIC (f));
6c183ba5 7963#endif
f1da8f06
GM
7964#if 0 /* Emacs sets WM hints whose `input' field is `true'. This
7965 instructs the WM to set the input focus automatically for
7966 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
7967 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
7968 it has set the focus. So, XSetInputFocus below is not
7969 needed.
7970
7971 The call to XSetInputFocus below has also caused trouble. In
7972 cases where the XSetInputFocus done by the WM and the one
7973 below are temporally close (on a fast machine), the call
7974 below can generate additional FocusIn events which confuse
7975 Emacs. */
7d0393cf 7976
bf338245
JD
7977 /* Since we set WM_TAKE_FOCUS, we must call
7978 XSetInputFocus explicitly. But not if f is null,
7979 since that might be an event for a deleted frame. */
7980 if (f)
7981 {
7982 Display *d = event.xclient.display;
7983 /* Catch and ignore errors, in case window has been
7984 iconified by a window manager such as GWM. */
7985 int count = x_catch_errors (d);
7986 XSetInputFocus (d, event.xclient.window,
7987 /* The ICCCM says this is
7988 the only valid choice. */
7989 RevertToParent,
7990 event.xclient.data.l[1]);
7991 /* This is needed to detect the error
7992 if there is an error. */
7993 XSync (d, False);
7994 x_uncatch_errors (d, count);
7995 }
7996 /* Not certain about handling scroll bars here */
f1da8f06 7997#endif /* 0 */
bf338245
JD
7998 }
7999 else if (event.xclient.data.l[0]
8000 == dpyinfo->Xatom_wm_save_yourself)
8001 {
8002 /* Save state modify the WM_COMMAND property to
8003 something which can reinstate us. This notifies
8004 the session manager, who's looking for such a
8005 PropertyNotify. Can restart processing when
8006 a keyboard or mouse event arrives. */
8007 /* If we have a session manager, don't set this.
8008 KDE will then start two Emacsen, one for the
8009 session manager and one for this. */
8010 if (numchars > 0
5f30b957 8011#ifdef HAVE_X_SM
bf338245 8012 && ! x_session_have_connection ()
5f30b957 8013#endif
bf338245
JD
8014 )
8015 {
8016 f = x_top_window_to_frame (dpyinfo,
8017 event.xclient.window);
8018 /* This is just so we only give real data once
8019 for a single Emacs process. */
8020 if (f == SELECTED_FRAME ())
8021 XSetCommand (FRAME_X_DISPLAY (f),
8022 event.xclient.window,
8023 initial_argv, initial_argc);
8024 else if (f)
8025 XSetCommand (FRAME_X_DISPLAY (f),
8026 event.xclient.window,
8027 0, 0);
8028 }
8029 }
8030 else if (event.xclient.data.l[0]
8031 == dpyinfo->Xatom_wm_delete_window)
8032 {
8033 struct frame *f
8034 = x_any_window_to_frame (dpyinfo,
8035 event.xclient.window);
7a13e894 8036
bf338245
JD
8037 if (f)
8038 {
8039 if (numchars == 0)
8040 abort ();
58769bee 8041
bf338245
JD
8042 bufp->kind = DELETE_WINDOW_EVENT;
8043 XSETFRAME (bufp->frame_or_window, f);
8044 bufp->arg = Qnil;
8045 bufp++;
1fb20991 8046
bf338245
JD
8047 count += 1;
8048 numchars -= 1;
8049 }
8050 else
8051 goto OTHER; /* May be a dialog that is to be removed */
8052 }
8053 }
8054 else if (event.xclient.message_type
8055 == dpyinfo->Xatom_wm_configure_denied)
8056 {
8057 }
8058 else if (event.xclient.message_type
8059 == dpyinfo->Xatom_wm_window_moved)
8060 {
8061 int new_x, new_y;
8062 struct frame *f
8063 = x_window_to_frame (dpyinfo, event.xclient.window);
8064
8065 new_x = event.xclient.data.s[0];
8066 new_y = event.xclient.data.s[1];
8067
8068 if (f)
8069 {
8070 f->output_data.x->left_pos = new_x;
8071 f->output_data.x->top_pos = new_y;
8072 }
8073 }
0fdff6bb 8074#ifdef HACK_EDITRES
bf338245
JD
8075 else if (event.xclient.message_type
8076 == dpyinfo->Xatom_editres)
8077 {
8078 struct frame *f
8079 = x_any_window_to_frame (dpyinfo, event.xclient.window);
8080 _XEditResCheckMessages (f->output_data.x->widget, NULL,
8081 &event, NULL);
8082 }
0fdff6bb 8083#endif /* HACK_EDITRES */
bf338245
JD
8084 else if ((event.xclient.message_type
8085 == dpyinfo->Xatom_DONE)
8086 || (event.xclient.message_type
8087 == dpyinfo->Xatom_PAGE))
8088 {
8089 /* Ghostview job completed. Kill it. We could
8090 reply with "Next" if we received "Page", but we
8091 currently never do because we are interested in
8092 images, only, which should have 1 page. */
8093 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
8094 struct frame *f
8095 = x_window_to_frame (dpyinfo, event.xclient.window);
8096 x_kill_gs_process (pixmap, f);
8097 expose_frame (f, 0, 0, 0, 0);
8098 }
06a2c219 8099#ifdef USE_TOOLKIT_SCROLL_BARS
bf338245
JD
8100 /* Scroll bar callbacks send a ClientMessage from which
8101 we construct an input_event. */
8102 else if (event.xclient.message_type
8103 == dpyinfo->Xatom_Scrollbar)
8104 {
8105 x_scroll_bar_to_input_event (&event, bufp);
8106 ++bufp, ++count, --numchars;
8107 goto out;
8108 }
06a2c219 8109#endif /* USE_TOOLKIT_SCROLL_BARS */
bf338245
JD
8110 else
8111 goto OTHER;
8112 }
8113 break;
dc6f92b8 8114
bf338245 8115 case SelectionNotify:
3afe33e7 8116#ifdef USE_X_TOOLKIT
bf338245
JD
8117 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
8118 goto OTHER;
3afe33e7 8119#endif /* not USE_X_TOOLKIT */
bf338245
JD
8120 x_handle_selection_notify (&event.xselection);
8121 break;
d56a553a 8122
bf338245 8123 case SelectionClear: /* Someone has grabbed ownership. */
3afe33e7 8124#ifdef USE_X_TOOLKIT
bf338245
JD
8125 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
8126 goto OTHER;
3afe33e7 8127#endif /* USE_X_TOOLKIT */
bf338245
JD
8128 {
8129 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
d56a553a 8130
bf338245
JD
8131 if (numchars == 0)
8132 abort ();
d56a553a 8133
bf338245
JD
8134 bufp->kind = SELECTION_CLEAR_EVENT;
8135 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
8136 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
8137 SELECTION_EVENT_TIME (bufp) = eventp->time;
8138 bufp->frame_or_window = Qnil;
8139 bufp->arg = Qnil;
8140 bufp++;
d56a553a 8141
bf338245
JD
8142 count += 1;
8143 numchars -= 1;
8144 }
8145 break;
dc6f92b8 8146
bf338245 8147 case SelectionRequest: /* Someone wants our selection. */
3afe33e7 8148#ifdef USE_X_TOOLKIT
bf338245
JD
8149 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
8150 goto OTHER;
3afe33e7 8151#endif /* USE_X_TOOLKIT */
bf338245
JD
8152 if (x_queue_selection_requests)
8153 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
8154 &event);
8155 else
8156 {
8157 XSelectionRequestEvent *eventp
8158 = (XSelectionRequestEvent *) &event;
8159
8160 if (numchars == 0)
8161 abort ();
8162
8163 bufp->kind = SELECTION_REQUEST_EVENT;
8164 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
8165 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
8166 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
8167 SELECTION_EVENT_TARGET (bufp) = eventp->target;
8168 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
8169 SELECTION_EVENT_TIME (bufp) = eventp->time;
8170 bufp->frame_or_window = Qnil;
8171 bufp->arg = Qnil;
8172 bufp++;
8173
8174 count += 1;
8175 numchars -= 1;
8176 }
8177 break;
7a13e894 8178
bf338245 8179 case PropertyNotify:
1d2b2268
GM
8180#if 0 /* This is plain wrong. In the case that we are waiting for a
8181 PropertyNotify used as an ACK in incremental selection
8182 transfer, the property will be on the receiver's window. */
8183#if defined USE_X_TOOLKIT
bf338245
JD
8184 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
8185 goto OTHER;
1d2b2268
GM
8186#endif
8187#endif
bf338245
JD
8188 x_handle_property_notify (&event.xproperty);
8189 goto OTHER;
dc6f92b8 8190
bf338245
JD
8191 case ReparentNotify:
8192 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
8193 if (f)
8194 {
8195 int x, y;
8196 f->output_data.x->parent_desc = event.xreparent.parent;
8197 x_real_positions (f, &x, &y);
8198 f->output_data.x->left_pos = x;
8199 f->output_data.x->top_pos = y;
bf338245 8200 }
15213141 8201 goto OTHER;
bf338245 8202 break;
3bd330d4 8203
bf338245
JD
8204 case Expose:
8205 f = x_window_to_frame (dpyinfo, event.xexpose.window);
8206 if (f)
8207 {
8208 x_check_fullscreen (f);
d0fa3e56 8209
bf338245
JD
8210 if (f->async_visible == 0)
8211 {
8212 f->async_visible = 1;
8213 f->async_iconified = 0;
8214 f->output_data.x->has_been_visible = 1;
8215 SET_FRAME_GARBAGED (f);
8216 }
8217 else
8218 expose_frame (x_window_to_frame (dpyinfo,
8219 event.xexpose.window),
8220 event.xexpose.x, event.xexpose.y,
8221 event.xexpose.width, event.xexpose.height);
8222 }
8223 else
8224 {
12949a7f 8225#ifndef USE_TOOLKIT_SCROLL_BARS
bf338245 8226 struct scroll_bar *bar;
12949a7f 8227#endif
01f67d2c 8228#if defined USE_LUCID
bf338245
JD
8229 /* Submenus of the Lucid menu bar aren't widgets
8230 themselves, so there's no way to dispatch events
8231 to them. Recognize this case separately. */
8232 {
8233 Widget widget
8234 = x_window_to_menu_bar (event.xexpose.window);
8235 if (widget)
8236 xlwmenu_redisplay (widget);
8237 }
01f67d2c
PJ
8238#endif /* USE_LUCID */
8239
06a2c219 8240#ifdef USE_TOOLKIT_SCROLL_BARS
bf338245
JD
8241 /* Dispatch event to the widget. */
8242 goto OTHER;
06a2c219 8243#else /* not USE_TOOLKIT_SCROLL_BARS */
bf338245 8244 bar = x_window_to_scroll_bar (event.xexpose.window);
58769bee 8245
bf338245
JD
8246 if (bar)
8247 x_scroll_bar_expose (bar, &event);
3afe33e7 8248#ifdef USE_X_TOOLKIT
bf338245
JD
8249 else
8250 goto OTHER;
3afe33e7 8251#endif /* USE_X_TOOLKIT */
06a2c219 8252#endif /* not USE_TOOLKIT_SCROLL_BARS */
bf338245
JD
8253 }
8254 break;
dc6f92b8 8255
bf338245
JD
8256 case GraphicsExpose: /* This occurs when an XCopyArea's
8257 source area was obscured or not
8258 available. */
8259 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
8260 if (f)
8261 {
8262 expose_frame (f,
8263 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
8264 event.xgraphicsexpose.width,
8265 event.xgraphicsexpose.height);
8266 }
3afe33e7 8267#ifdef USE_X_TOOLKIT
bf338245
JD
8268 else
8269 goto OTHER;
3afe33e7 8270#endif /* USE_X_TOOLKIT */
bf338245 8271 break;
7d0393cf 8272
bf338245
JD
8273 case NoExpose: /* This occurs when an XCopyArea's
8274 source area was completely
8275 available. */
8276 break;
dc6f92b8 8277
bf338245
JD
8278 case UnmapNotify:
8279 /* Redo the mouse-highlight after the tooltip has gone. */
8280 if (event.xmap.window == tip_window)
8281 {
8282 tip_window = 0;
8283 redo_mouse_highlight ();
8284 }
bddd097c 8285
bf338245
JD
8286 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
8287 if (f) /* F may no longer exist if
8288 the frame was deleted. */
8289 {
8290 /* While a frame is unmapped, display generation is
8291 disabled; you don't want to spend time updating a
8292 display that won't ever be seen. */
8293 f->async_visible = 0;
8294 /* We can't distinguish, from the event, whether the window
8295 has become iconified or invisible. So assume, if it
8296 was previously visible, than now it is iconified.
8297 But x_make_frame_invisible clears both
8298 the visible flag and the iconified flag;
8299 and that way, we know the window is not iconified now. */
8300 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
8301 {
8302 f->async_iconified = 1;
dc6f92b8 8303
bf338245
JD
8304 bufp->kind = ICONIFY_EVENT;
8305 XSETFRAME (bufp->frame_or_window, f);
8306 bufp->arg = Qnil;
8307 bufp++;
8308 count++;
8309 numchars--;
8310 }
8311 }
8312 goto OTHER;
8313
8314 case MapNotify:
8315 if (event.xmap.window == tip_window)
8316 /* The tooltip has been drawn already. Avoid
8317 the SET_FRAME_GARBAGED below. */
8318 goto OTHER;
8319
8320 /* We use x_top_window_to_frame because map events can
8321 come for sub-windows and they don't mean that the
8322 frame is visible. */
8323 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
8324 if (f)
8325 {
8326 /* wait_reading_process_input will notice this and update
8327 the frame's display structures.
8328 If we where iconified, we should not set garbaged,
8329 because that stops redrawing on Expose events. This looks
8330 bad if we are called from a recursive event loop
8331 (x_dispatch_event), for example when a dialog is up. */
8332 if (! f->async_iconified)
8333 SET_FRAME_GARBAGED (f);
8334
8335 f->async_visible = 1;
8336 f->async_iconified = 0;
8337 f->output_data.x->has_been_visible = 1;
8338
8339 if (f->iconified)
8340 {
8341 bufp->kind = DEICONIFY_EVENT;
8342 XSETFRAME (bufp->frame_or_window, f);
8343 bufp->arg = Qnil;
8344 bufp++;
8345 count++;
8346 numchars--;
8347 }
8348 else if (! NILP (Vframe_list)
8349 && ! NILP (XCDR (Vframe_list)))
8350 /* Force a redisplay sooner or later
8351 to update the frame titles
8352 in case this is the second frame. */
8353 record_asynch_buffer_change ();
8354 }
8355 goto OTHER;
2a793c0c 8356
bf338245 8357 case KeyPress:
2a793c0c 8358
22174d10 8359#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
bf338245 8360 /* Dispatch KeyPress events when in menu. */
488dd4c4 8361 if (popup_activated ())
bf338245 8362 goto OTHER;
22174d10 8363#endif
488dd4c4 8364
bf338245 8365 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
f451eb13 8366
bf338245
JD
8367 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
8368 {
8369 dpyinfo->mouse_face_hidden = 1;
8370 clear_mouse_face (dpyinfo);
8371 }
66301061 8372
eccc05db 8373#if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
bf338245
JD
8374 if (f == 0)
8375 {
8376 /* Scroll bars consume key events, but we want
8377 the keys to go to the scroll bar's frame. */
8378 Widget widget = XtWindowToWidget (dpyinfo->display,
8379 event.xkey.window);
8380 if (widget && XmIsScrollBar (widget))
8381 {
8382 widget = XtParent (widget);
8383 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
8384 }
8385 }
eccc05db 8386#endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
06a2c219 8387
bf338245
JD
8388 if (f != 0)
8389 {
8390 KeySym keysym, orig_keysym;
8391 /* al%imercury@uunet.uu.net says that making this 81
8392 instead of 80 fixed a bug whereby meta chars made
8393 his Emacs hang.
8394
8395 It seems that some version of XmbLookupString has
8396 a bug of not returning XBufferOverflow in
8397 status_return even if the input is too long to
8398 fit in 81 bytes. So, we must prepare sufficient
8399 bytes for copy_buffer. 513 bytes (256 chars for
8400 two-byte character set) seems to be a fairly good
8401 approximation. -- 2000.8.10 handa@etl.go.jp */
8402 unsigned char copy_buffer[513];
8403 unsigned char *copy_bufptr = copy_buffer;
8404 int copy_bufsiz = sizeof (copy_buffer);
8405 int modifiers;
8406 Lisp_Object coding_system = Qlatin_1;
8407
8408 event.xkey.state
8409 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
8410 extra_keyboard_modifiers);
8411 modifiers = event.xkey.state;
8412
8413 /* This will have to go some day... */
8414
8415 /* make_lispy_event turns chars into control chars.
8416 Don't do it here because XLookupString is too eager. */
8417 event.xkey.state &= ~ControlMask;
8418 event.xkey.state &= ~(dpyinfo->meta_mod_mask
8419 | dpyinfo->super_mod_mask
8420 | dpyinfo->hyper_mod_mask
8421 | dpyinfo->alt_mod_mask);
8422
8423 /* In case Meta is ComposeCharacter,
8424 clear its status. According to Markus Ehrnsperger
8425 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
8426 this enables ComposeCharacter to work whether or
8427 not it is combined with Meta. */
8428 if (modifiers & dpyinfo->meta_mod_mask)
8429 bzero (&compose_status, sizeof (compose_status));
1cf4a0d1 8430
6c183ba5 8431#ifdef HAVE_X_I18N
bf338245
JD
8432 if (FRAME_XIC (f))
8433 {
8434 Status status_return;
8435
8436 coding_system = Vlocale_coding_system;
8437 nbytes = XmbLookupString (FRAME_XIC (f),
8438 &event.xkey, copy_bufptr,
8439 copy_bufsiz, &keysym,
8440 &status_return);
8441 if (status_return == XBufferOverflow)
8442 {
8443 copy_bufsiz = nbytes + 1;
8444 copy_bufptr = (char *) alloca (copy_bufsiz);
8445 nbytes = XmbLookupString (FRAME_XIC (f),
8446 &event.xkey, copy_bufptr,
8447 copy_bufsiz, &keysym,
8448 &status_return);
8449 }
8450 /* Xutf8LookupString is a new but already deprecated interface. -stef */
96035dca 8451#if 0 && defined X_HAVE_UTF8_STRING
bf338245
JD
8452 else if (status_return == XLookupKeySym)
8453 { /* Try again but with utf-8. */
8454 coding_system = Qutf_8;
8455 nbytes = Xutf8LookupString (FRAME_XIC (f),
8456 &event.xkey, copy_bufptr,
8457 copy_bufsiz, &keysym,
8458 &status_return);
8459 if (status_return == XBufferOverflow)
8460 {
8461 copy_bufsiz = nbytes + 1;
8462 copy_bufptr = (char *) alloca (copy_bufsiz);
8463 nbytes = Xutf8LookupString (FRAME_XIC (f),
8464 &event.xkey,
8465 copy_bufptr,
8466 copy_bufsiz, &keysym,
8467 &status_return);
8468 }
8469 }
c997eae5 8470#endif
f5d11644 8471
bf338245
JD
8472 if (status_return == XLookupNone)
8473 break;
8474 else if (status_return == XLookupChars)
8475 {
8476 keysym = NoSymbol;
8477 modifiers = 0;
8478 }
8479 else if (status_return != XLookupKeySym
8480 && status_return != XLookupBoth)
8481 abort ();
8482 }
8483 else
8484 nbytes = XLookupString (&event.xkey, copy_bufptr,
8485 copy_bufsiz, &keysym,
8486 &compose_status);
6c183ba5 8487#else
bf338245
JD
8488 nbytes = XLookupString (&event.xkey, copy_bufptr,
8489 copy_bufsiz, &keysym,
8490 &compose_status);
6c183ba5 8491#endif
dc6f92b8 8492
bf338245 8493 orig_keysym = keysym;
55123275 8494
bf338245
JD
8495 if (numchars > 1)
8496 {
8497 Lisp_Object c;
8498
8499 /* First deal with keysyms which have defined
8500 translations to characters. */
8501 if (keysym >= 32 && keysym < 128)
8502 /* Avoid explicitly decoding each ASCII character. */
8503 {
8504 bufp->kind = ASCII_KEYSTROKE_EVENT;
8505 bufp->code = keysym;
8506 XSETFRAME (bufp->frame_or_window, f);
8507 bufp->arg = Qnil;
8508 bufp->modifiers
8509 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
8510 modifiers);
8511 bufp->timestamp = event.xkey.time;
8512 bufp++;
8513 count++;
8514 numchars--;
8515 }
8516 /* Now non-ASCII. */
8517 else if (HASH_TABLE_P (Vx_keysym_table)
8518 && (NATNUMP (c = Fgethash (make_number (keysym),
8519 Vx_keysym_table,
8520 Qnil))))
8521 {
8522 bufp->kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
8523 ? ASCII_KEYSTROKE_EVENT
8524 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
8525 bufp->code = XFASTINT (c);
8526 XSETFRAME (bufp->frame_or_window, f);
8527 bufp->arg = Qnil;
8528 bufp->modifiers
8529 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
8530 modifiers);
8531 bufp->timestamp = event.xkey.time;
8532 bufp++;
8533 count++;
8534 numchars--;
8535 }
8536 /* Random non-modifier sorts of keysyms. */
8537 else if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
8538 || keysym == XK_Delete
1097aea0 8539#ifdef XK_ISO_Left_Tab
bf338245
JD
8540 || (keysym >= XK_ISO_Left_Tab
8541 && keysym <= XK_ISO_Enter)
1097aea0 8542#endif
bf338245
JD
8543 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
8544 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
c34790e0 8545#ifdef HPUX
bf338245
JD
8546 /* This recognizes the "extended function
8547 keys". It seems there's no cleaner way.
8548 Test IsModifierKey to avoid handling
8549 mode_switch incorrectly. */
8550 || ((unsigned) (keysym) >= XK_Select
8551 && (unsigned)(keysym) < XK_KP_Space)
69388238
RS
8552#endif
8553#ifdef XK_dead_circumflex
bf338245 8554 || orig_keysym == XK_dead_circumflex
69388238
RS
8555#endif
8556#ifdef XK_dead_grave
bf338245 8557 || orig_keysym == XK_dead_grave
69388238
RS
8558#endif
8559#ifdef XK_dead_tilde
bf338245 8560 || orig_keysym == XK_dead_tilde
69388238
RS
8561#endif
8562#ifdef XK_dead_diaeresis
bf338245 8563 || orig_keysym == XK_dead_diaeresis
69388238
RS
8564#endif
8565#ifdef XK_dead_macron
bf338245 8566 || orig_keysym == XK_dead_macron
69388238
RS
8567#endif
8568#ifdef XK_dead_degree
bf338245 8569 || orig_keysym == XK_dead_degree
69388238
RS
8570#endif
8571#ifdef XK_dead_acute
bf338245 8572 || orig_keysym == XK_dead_acute
69388238
RS
8573#endif
8574#ifdef XK_dead_cedilla
bf338245 8575 || orig_keysym == XK_dead_cedilla
69388238
RS
8576#endif
8577#ifdef XK_dead_breve
bf338245 8578 || orig_keysym == XK_dead_breve
69388238
RS
8579#endif
8580#ifdef XK_dead_ogonek
bf338245 8581 || orig_keysym == XK_dead_ogonek
69388238
RS
8582#endif
8583#ifdef XK_dead_caron
bf338245 8584 || orig_keysym == XK_dead_caron
69388238
RS
8585#endif
8586#ifdef XK_dead_doubleacute
bf338245 8587 || orig_keysym == XK_dead_doubleacute
69388238
RS
8588#endif
8589#ifdef XK_dead_abovedot
bf338245 8590 || orig_keysym == XK_dead_abovedot
c34790e0 8591#endif
bf338245
JD
8592 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
8593 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
8594 /* Any "vendor-specific" key is ok. */
8595 || (orig_keysym & (1 << 28))
8596 || (keysym != NoSymbol && nbytes == 0))
8597 && ! (IsModifierKey (orig_keysym)
7719aa06
RS
8598#ifndef HAVE_X11R5
8599#ifdef XK_Mode_switch
bf338245 8600 || ((unsigned)(orig_keysym) == XK_Mode_switch)
7719aa06
RS
8601#endif
8602#ifdef XK_Num_Lock
bf338245 8603 || ((unsigned)(orig_keysym) == XK_Num_Lock)
7719aa06
RS
8604#endif
8605#endif /* not HAVE_X11R5 */
bf338245
JD
8606 /* The symbols from XK_ISO_Lock
8607 to XK_ISO_Last_Group_Lock
8608 don't have real modifiers but
8609 should be treated similarly to
8610 Mode_switch by Emacs. */
7ef18d79 8611#if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
bf338245
JD
8612 || ((unsigned)(orig_keysym)
8613 >= XK_ISO_Lock
8614 && (unsigned)(orig_keysym)
8615 <= XK_ISO_Last_Group_Lock)
7ef18d79 8616#endif
bf338245
JD
8617 ))
8618 {
8619 if (temp_index == sizeof temp_buffer / sizeof (short))
8620 temp_index = 0;
8621 temp_buffer[temp_index++] = keysym;
8622 /* make_lispy_event will convert this to a symbolic
8623 key. */
8624 bufp->kind = NON_ASCII_KEYSTROKE_EVENT;
8625 bufp->code = keysym;
8626 XSETFRAME (bufp->frame_or_window, f);
8627 bufp->arg = Qnil;
8628 bufp->modifiers
8629 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
8630 modifiers);
8631 bufp->timestamp = event.xkey.time;
8632 bufp++;
8633 count++;
8634 numchars--;
8635 }
8636 else if (numchars > nbytes)
8637 { /* Raw bytes, not keysym. */
8638 register int i;
8639 register int c;
8640 int nchars, len;
8641
8642 /* The input should be decoded with `coding_system'
8643 which depends on which X*LookupString function
8644 we used just above and the locale. */
8645 setup_coding_system (coding_system, &coding);
8646 coding.src_multibyte = 0;
8647 coding.dst_multibyte = 1;
8648 /* The input is converted to events, thus we can't
8649 handle composition. Anyway, there's no XIM that
8650 gives us composition information. */
8651 coding.composing = COMPOSITION_DISABLED;
8652
8653 for (i = 0; i < nbytes; i++)
8654 {
8655 if (temp_index == (sizeof temp_buffer
8656 / sizeof (short)))
8657 temp_index = 0;
8658 temp_buffer[temp_index++] = copy_bufptr[i];
8659 }
379b5ac0 8660
bf338245
JD
8661 {
8662 /* Decode the input data. */
8663 int require;
8664 unsigned char *p;
8665
8666 require = decoding_buffer_size (&coding, nbytes);
8667 p = (unsigned char *) alloca (require);
8668 coding.mode |= CODING_MODE_LAST_BLOCK;
8669 /* We explicitely disable composition
8670 handling because key data should
8671 not contain any composition
8672 sequence. */
8673 coding.composing = COMPOSITION_DISABLED;
8674 decode_coding (&coding, copy_bufptr, p,
8675 nbytes, require);
8676 nbytes = coding.produced;
8677 nchars = coding.produced_char;
8678 copy_bufptr = p;
8679 }
379b5ac0 8680
bf338245
JD
8681 /* Convert the input data to a sequence of
8682 character events. */
8683 for (i = 0; i < nbytes; i += len)
8684 {
8685 if (nchars == nbytes)
8686 c = copy_bufptr[i], len = 1;
8687 else
8688 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
8689 nbytes - i, len);
8690
8691 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
8692 ? ASCII_KEYSTROKE_EVENT
8693 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
8694 bufp->code = c;
8695 XSETFRAME (bufp->frame_or_window, f);
8696 bufp->arg = Qnil;
8697 bufp->modifiers
8698 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
8699 modifiers);
8700 bufp->timestamp = event.xkey.time;
8701 bufp++;
8702 }
8703
8704 count += nchars;
8705 numchars -= nchars;
8706
8707 if (keysym == NoSymbol)
8708 break;
8709 }
8710 else
8711 abort ();
8712 }
8713 else
8714 abort ();
8715 }
59ddecde 8716#ifdef HAVE_X_I18N
bf338245
JD
8717 /* Don't dispatch this event since XtDispatchEvent calls
8718 XFilterEvent, and two calls in a row may freeze the
8719 client. */
8720 break;
59ddecde 8721#else
bf338245 8722 goto OTHER;
59ddecde 8723#endif
f451eb13 8724
bf338245 8725 case KeyRelease:
59ddecde 8726#ifdef HAVE_X_I18N
bf338245
JD
8727 /* Don't dispatch this event since XtDispatchEvent calls
8728 XFilterEvent, and two calls in a row may freeze the
8729 client. */
8730 break;
59ddecde 8731#else
bf338245 8732 goto OTHER;
59ddecde 8733#endif
f5d11644 8734
bf338245
JD
8735 case EnterNotify:
8736 {
8737 int n;
cab34f50 8738
bf338245
JD
8739 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
8740 if (n > 0)
8741 {
8742 bufp += n, count += n, numchars -= n;
8743 }
7d0393cf 8744
bf338245 8745 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
06a2c219 8746
2c850e26 8747#if 0
bf338245
JD
8748 if (event.xcrossing.focus)
8749 {
8750 /* Avoid nasty pop/raise loops. */
8751 if (f && (!(f->auto_raise)
8752 || !(f->auto_lower)
8753 || (event.xcrossing.time - enter_timestamp) > 500))
fd42232c 8754 {
bf338245
JD
8755 x_new_focus_frame (dpyinfo, f);
8756 enter_timestamp = event.xcrossing.time;
fd42232c 8757 }
bf338245
JD
8758 }
8759 else if (f == dpyinfo->x_focus_frame)
8760 x_new_focus_frame (dpyinfo, 0);
8761#endif
f9e24cb9 8762
bf338245
JD
8763 /* EnterNotify counts as mouse movement,
8764 so update things that depend on mouse position. */
8765 if (f && !f->output_data.x->hourglass_p)
8766 note_mouse_movement (f, &event.xmotion);
8767 goto OTHER;
8768 }
cab34f50 8769
bf338245
JD
8770 case FocusIn:
8771 {
8772 int n;
cab34f50 8773
bf338245
JD
8774 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
8775 if (n > 0)
8776 {
8777 bufp += n, count += n, numchars -= n;
8778 }
8779 }
06a2c219 8780
bf338245 8781 goto OTHER;
7a13e894 8782
bf338245
JD
8783 case LeaveNotify:
8784 {
8785 int n;
dc6f92b8 8786
bf338245
JD
8787 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
8788 if (n > 0)
8789 {
8790 bufp += n, count += n, numchars -= n;
8791 }
8792 }
cab34f50 8793
bf338245
JD
8794 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
8795 if (f)
8796 {
8797 if (f == dpyinfo->mouse_face_mouse_frame)
8798 {
8799 /* If we move outside the frame, then we're
8800 certainly no longer on any text in the frame. */
8801 clear_mouse_face (dpyinfo);
8802 dpyinfo->mouse_face_mouse_frame = 0;
8803 }
6c183ba5 8804
bf338245
JD
8805 /* Generate a nil HELP_EVENT to cancel a help-echo.
8806 Do it only if there's something to cancel.
8807 Otherwise, the startup message is cleared when
8808 the mouse leaves the frame. */
8809 if (any_help_event_p)
8810 {
8811 Lisp_Object frame;
8812 int n;
8813
8814 XSETFRAME (frame, f);
8815 help_echo = Qnil;
8816 n = gen_help_event (bufp, numchars,
8817 Qnil, frame, Qnil, Qnil, 0);
8818 bufp += n, count += n, numchars -= n;
8819 }
dc6f92b8 8820
bf338245
JD
8821 }
8822 goto OTHER;
7d0393cf 8823
bf338245
JD
8824 case FocusOut:
8825 {
8826 int n;
06a2c219 8827
bf338245
JD
8828 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
8829 if (n > 0)
8830 {
8831 bufp += n, count += n, numchars -= n;
8832 }
8833 }
66301061 8834
bf338245 8835 goto OTHER;
5bc62483 8836
bf338245
JD
8837 case MotionNotify:
8838 {
8839 previous_help_echo = help_echo;
8840 help_echo = help_echo_object = help_echo_window = Qnil;
8841 help_echo_pos = -1;
8842
8843 if (dpyinfo->grabbed && last_mouse_frame
8844 && FRAME_LIVE_P (last_mouse_frame))
8845 f = last_mouse_frame;
8846 else
8847 f = x_window_to_frame (dpyinfo, event.xmotion.window);
8848
8849 if (dpyinfo->mouse_face_hidden)
8850 {
8851 dpyinfo->mouse_face_hidden = 0;
8852 clear_mouse_face (dpyinfo);
8853 }
8854
8855 if (f)
8856 {
8857
8858 /* Generate SELECT_WINDOW_EVENTs when needed. */
8859 if (mouse_autoselect_window)
8860 {
8861 Lisp_Object window;
8862 int area;
8863
8864 window = window_from_coordinates (f,
8865 event.xmotion.x, event.xmotion.y,
8866 &area, 0);
8867
8868 /* Window will be selected only when it is not selected now and
8869 last mouse movement event was not in it. Minibuffer window
8870 will be selected iff it is active. */
8871 if (WINDOWP(window)
8872 && !EQ (window, last_window)
8873 && !EQ (window, selected_window)
8874 && numchars > 0)
8875 {
8876 bufp->kind = SELECT_WINDOW_EVENT;
8877 bufp->frame_or_window = window;
8878 bufp->arg = Qnil;
8879 ++bufp, ++count, --numchars;
8880 }
5bc62483 8881
bf338245
JD
8882 last_window=window;
8883 }
8884 note_mouse_movement (f, &event.xmotion);
8885 }
8886 else
8887 {
e88b3c50 8888#ifndef USE_TOOLKIT_SCROLL_BARS
bf338245
JD
8889 struct scroll_bar *bar
8890 = x_window_to_scroll_bar (event.xmotion.window);
f451eb13 8891
bf338245
JD
8892 if (bar)
8893 x_scroll_bar_note_movement (bar, &event);
e88b3c50 8894#endif /* USE_TOOLKIT_SCROLL_BARS */
b8009dd1 8895
bf338245
JD
8896 /* If we move outside the frame, then we're
8897 certainly no longer on any text in the frame. */
8898 clear_mouse_face (dpyinfo);
8899 }
8900
8901 /* If the contents of the global variable help_echo
8902 has changed, generate a HELP_EVENT. */
8903 if (!NILP (help_echo)
8904 || !NILP (previous_help_echo))
8905 {
8906 Lisp_Object frame;
8907 int n;
8908
8909 if (f)
8910 XSETFRAME (frame, f);
8911 else
8912 frame = Qnil;
8913
8914 any_help_event_p = 1;
8915 n = gen_help_event (bufp, numchars, help_echo, frame,
8916 help_echo_window, help_echo_object,
8917 help_echo_pos);
8918 bufp += n, count += n, numchars -= n;
8919 }
8920
8921 goto OTHER;
8922 }
dc6f92b8 8923
bf338245
JD
8924 case ConfigureNotify:
8925 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
8926 if (f)
8927 {
7a4bce14 8928#ifndef USE_X_TOOLKIT
488dd4c4
JD
8929#ifdef USE_GTK
8930 xg_resize_widgets (f, event.xconfigure.width,
8931 event.xconfigure.height);
8932#else /* not USE_GTK */
bf338245
JD
8933 /* If there is a pending resize for fullscreen, don't
8934 do this one, the right one will come later.
8935 The toolkit version doesn't seem to need this, but we
8936 need to reset it below. */
8937 int dont_resize =
8938 ((f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
8939 && FRAME_NEW_WIDTH (f) != 0);
8940 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
8941 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
8942 if (dont_resize)
8943 goto OTHER;
8944
8945 /* In the toolkit version, change_frame_size
8946 is called by the code that handles resizing
8947 of the EmacsFrame widget. */
8948
8949 /* Even if the number of character rows and columns has
8950 not changed, the font size may have changed, so we need
8951 to check the pixel dimensions as well. */
8952 if (columns != f->width
8953 || rows != f->height
8954 || event.xconfigure.width != f->output_data.x->pixel_width
8955 || event.xconfigure.height != f->output_data.x->pixel_height)
8956 {
8957 change_frame_size (f, rows, columns, 0, 1, 0);
8958 SET_FRAME_GARBAGED (f);
8959 cancel_mouse_face (f);
8960 }
488dd4c4 8961#endif /* not USE_GTK */
2d7fc7e8 8962#endif
af395ec1 8963
bf338245
JD
8964 f->output_data.x->pixel_width = event.xconfigure.width;
8965 f->output_data.x->pixel_height = event.xconfigure.height;
7a13e894 8966
488dd4c4
JD
8967#ifdef USE_GTK
8968 /* GTK creates windows but doesn't map them.
8969 Only get real positions and check fullscreen when mapped. */
8970 if (FRAME_GTK_OUTER_WIDGET (f)
8971 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
8972 {
8973#endif
bf338245
JD
8974 /* What we have now is the position of Emacs's own window.
8975 Convert that to the position of the window manager window. */
8976 x_real_positions (f, &f->output_data.x->left_pos,
8977 &f->output_data.x->top_pos);
dcb07ae9 8978
488dd4c4 8979 x_check_fullscreen_move (f);
bf338245
JD
8980 if (f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
8981 f->output_data.x->want_fullscreen &=
8982 ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
488dd4c4
JD
8983#ifdef USE_GTK
8984 }
8985#endif
f5d11644 8986#ifdef HAVE_X_I18N
bf338245
JD
8987 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
8988 xic_set_statusarea (f);
f5d11644
GM
8989#endif
8990
bf338245
JD
8991 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
8992 {
8993 /* Since the WM decorations come below top_pos now,
8994 we must put them below top_pos in the future. */
8995 f->output_data.x->win_gravity = NorthWestGravity;
8996 x_wm_set_size_hint (f, (long) 0, 0);
8997 }
8998 }
8999 goto OTHER;
dc6f92b8 9000
bf338245 9001 case ButtonRelease:
488dd4c4 9002 case ButtonPress:
bf338245
JD
9003 {
9004 /* If we decide we want to generate an event to be seen
9005 by the rest of Emacs, we put it here. */
9006 struct input_event emacs_event;
9007 int tool_bar_p = 0;
9008
9009 emacs_event.kind = NO_EVENT;
9010 bzero (&compose_status, sizeof (compose_status));
9011
9012 if (dpyinfo->grabbed
9013 && last_mouse_frame
9014 && FRAME_LIVE_P (last_mouse_frame))
9015 f = last_mouse_frame;
9016 else
9017 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9018
9019 if (f)
9020 {
9021 /* Is this in the tool-bar? */
9022 if (WINDOWP (f->tool_bar_window)
9023 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
9024 {
9025 Lisp_Object window;
9026 int p, x, y;
06a2c219 9027
bf338245
JD
9028 x = event.xbutton.x;
9029 y = event.xbutton.y;
06a2c219 9030
bf338245
JD
9031 /* Set x and y. */
9032 window = window_from_coordinates (f, x, y, &p, 1);
9033 if (EQ (window, f->tool_bar_window))
9034 {
9035 x_handle_tool_bar_click (f, &event.xbutton);
9036 tool_bar_p = 1;
9037 }
9038 }
06a2c219 9039
bf338245
JD
9040 if (!tool_bar_p)
9041 if (!dpyinfo->x_focus_frame
9042 || f == dpyinfo->x_focus_frame)
488dd4c4 9043 {
22174d10 9044#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
488dd4c4 9045 if (! popup_activated ())
22174d10 9046#endif
488dd4c4
JD
9047 construct_mouse_click (&emacs_event, &event, f);
9048 }
bf338245
JD
9049 }
9050 else
9051 {
bf338245
JD
9052 struct scroll_bar *bar
9053 = x_window_to_scroll_bar (event.xbutton.window);
f451eb13 9054
257f40f2
JD
9055#ifdef USE_TOOLKIT_SCROLL_BARS
9056 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
9057 scroll bars. */
9058 if (bar && event.xbutton.state & ControlMask)
9059 {
9060 x_scroll_bar_handle_click (bar, &event, &emacs_event);
9061 *finish = X_EVENT_DROP;
9062 }
9063#else /* not USE_TOOLKIT_SCROLL_BARS */
bf338245
JD
9064 if (bar)
9065 x_scroll_bar_handle_click (bar, &event, &emacs_event);
06a2c219 9066#endif /* not USE_TOOLKIT_SCROLL_BARS */
bf338245
JD
9067 }
9068
9069 if (event.type == ButtonPress)
9070 {
9071 dpyinfo->grabbed |= (1 << event.xbutton.button);
9072 last_mouse_frame = f;
9073 /* Ignore any mouse motion that happened
9074 before this event; any subsequent mouse-movement
9075 Emacs events should reflect only motion after
9076 the ButtonPress. */
9077 if (f != 0)
9078 f->mouse_moved = 0;
9079
9080 if (!tool_bar_p)
9081 last_tool_bar_item = -1;
9082 }
9083 else
488dd4c4 9084 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
bf338245
JD
9085
9086 if (numchars >= 1 && emacs_event.kind != NO_EVENT)
9087 {
9088 bcopy (&emacs_event, bufp, sizeof (struct input_event));
9089 bufp++;
9090 count++;
9091 numchars--;
9092 }
3afe33e7 9093
488dd4c4 9094#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
bf338245
JD
9095 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
9096 /* For a down-event in the menu bar,
9097 don't pass it to Xt right now.
9098 Instead, save it away
9099 and we will pass it to Xt from kbd_buffer_get_event.
9100 That way, we can run some Lisp code first. */
488dd4c4
JD
9101 if (
9102#ifdef USE_GTK
9103 ! popup_activated ()
9104 &&
9105#endif
9106 f && event.type == ButtonPress
bf338245
JD
9107 /* Verify the event is really within the menu bar
9108 and not just sent to it due to grabbing. */
9109 && event.xbutton.x >= 0
9110 && event.xbutton.x < f->output_data.x->pixel_width
9111 && event.xbutton.y >= 0
9112 && event.xbutton.y < f->output_data.x->menubar_height
9113 && event.xbutton.same_screen)
9114 {
9115 SET_SAVED_BUTTON_EVENT;
9116 XSETFRAME (last_mouse_press_frame, f);
488dd4c4
JD
9117#ifdef USE_GTK
9118 *finish = X_EVENT_DROP;
9119#endif
bf338245
JD
9120 }
9121 else if (event.type == ButtonPress)
9122 {
9123 last_mouse_press_frame = Qnil;
9124 goto OTHER;
9125 }
06a2c219 9126
2237cac9
RS
9127#ifdef USE_MOTIF /* This should do not harm for Lucid,
9128 but I am trying to be cautious. */
bf338245
JD
9129 else if (event.type == ButtonRelease)
9130 {
9131 if (!NILP (last_mouse_press_frame))
9132 {
9133 f = XFRAME (last_mouse_press_frame);
9134 if (f->output_data.x)
9135 SET_SAVED_BUTTON_EVENT;
9136 }
9137 else
9138 goto OTHER;
9139 }
2237cac9 9140#endif /* USE_MOTIF */
bf338245
JD
9141 else
9142 goto OTHER;
488dd4c4 9143#endif /* USE_X_TOOLKIT || USE_GTK */
bf338245
JD
9144 }
9145 break;
dc6f92b8 9146
bf338245
JD
9147 case CirculateNotify:
9148 goto OTHER;
7d0393cf 9149
bf338245
JD
9150 case CirculateRequest:
9151 goto OTHER;
06a2c219 9152
bf338245
JD
9153 case VisibilityNotify:
9154 goto OTHER;
dc6f92b8 9155
bf338245
JD
9156 case MappingNotify:
9157 /* Someone has changed the keyboard mapping - update the
9158 local cache. */
9159 switch (event.xmapping.request)
9160 {
9161 case MappingModifier:
9162 x_find_modifier_meanings (dpyinfo);
9163 /* This is meant to fall through. */
9164 case MappingKeyboard:
9165 XRefreshKeyboardMapping (&event.xmapping);
9166 }
9167 goto OTHER;
dc6f92b8 9168
bf338245
JD
9169 default:
9170 OTHER:
717ca130 9171#ifdef USE_X_TOOLKIT
bf338245 9172 BLOCK_INPUT;
257f40f2
JD
9173 if (*finish != X_EVENT_DROP)
9174 XtDispatchEvent (&event);
bf338245 9175 UNBLOCK_INPUT;
3afe33e7 9176#endif /* USE_X_TOOLKIT */
bf338245
JD
9177 break;
9178 }
9179
9180 goto ret;
177c0ea7 9181
bf338245
JD
9182 out:
9183 *finish = X_EVENT_GOTO_OUT;
9184
9185 ret:
9186 *bufp_r = bufp;
9187 *numcharsp = numchars;
9188 *eventp = event;
177c0ea7 9189
bf338245
JD
9190 return count;
9191}
9192
9193
9194/* Handles the XEvent EVENT on display DISPLAY.
9195 This is used for event loops outside the normal event handling,
1fcfb866
JD
9196 i.e. looping while a popup menu or a dialog is posted.
9197
9198 Returns the value handle_one_xevent sets in the finish argument. */
9199int
bf338245
JD
9200x_dispatch_event (event, display)
9201 XEvent *event;
9202 Display *display;
9203{
9204 struct x_display_info *dpyinfo;
9205 struct input_event bufp[10];
9206 struct input_event *bufpp = bufp;
9207 int numchars = 10;
1fcfb866 9208 int finish = X_EVENT_NORMAL;
177c0ea7 9209
bf338245
JD
9210 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9211 if (dpyinfo->display == display)
9212 break;
177c0ea7 9213
bf338245
JD
9214 if (dpyinfo)
9215 {
9216 int i, events;
9217 events = handle_one_xevent (dpyinfo,
9218 event,
9219 &bufpp,
9220 &numchars,
9221 &finish);
9222 for (i = 0; i < events; ++i)
9223 kbd_buffer_store_event (&bufp[i]);
9224 }
1fcfb866
JD
9225
9226 return finish;
bf338245
JD
9227}
9228
9229
9230/* Read events coming from the X server.
9231 This routine is called by the SIGIO handler.
9232 We return as soon as there are no more events to be read.
9233
9234 Events representing keys are stored in buffer BUFP,
9235 which can hold up to NUMCHARS characters.
9236 We return the number of characters stored into the buffer,
9237 thus pretending to be `read'.
9238
9239 EXPECTED is nonzero if the caller knows input is available. */
9240
9241static int
9242XTread_socket (sd, bufp, numchars, expected)
9243 register int sd;
9244 /* register */ struct input_event *bufp;
9245 /* register */ int numchars;
9246 int expected;
9247{
9248 int count = 0;
9249 int nbytes = 0;
9250 XEvent event;
9251 int event_found = 0;
9252 struct x_display_info *dpyinfo;
9253
9254 if (interrupt_input_blocked)
9255 {
9256 interrupt_input_pending = 1;
9257 return -1;
9258 }
9259
9260 interrupt_input_pending = 0;
9261 BLOCK_INPUT;
9262
9263 /* So people can tell when we have read the available input. */
9264 input_signal_count++;
9265
9266 if (numchars <= 0)
9267 abort (); /* Don't think this happens. */
9268
9269 ++handling_signal;
9270
9271 /* Find the display we are supposed to read input for.
9272 It's the one communicating on descriptor SD. */
9273 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9274 {
9275#if 0 /* This ought to be unnecessary; let's verify it. */
9276#ifdef FIOSNBIO
9277 /* If available, Xlib uses FIOSNBIO to make the socket
9278 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
9279 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
9280 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
9281 fcntl (dpyinfo->connection, F_SETFL, 0);
9282#endif /* ! defined (FIOSNBIO) */
9283#endif
9284
9285#if 0 /* This code can't be made to work, with multiple displays,
9286 and appears not to be used on any system any more.
9287 Also keyboard.c doesn't turn O_NDELAY on and off
9288 for X connections. */
9289#ifndef SIGIO
9290#ifndef HAVE_SELECT
9291 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9292 {
9293 extern int read_alarm_should_throw;
9294 read_alarm_should_throw = 1;
9295 XPeekEvent (dpyinfo->display, &event);
9296 read_alarm_should_throw = 0;
9297 }
9298#endif /* HAVE_SELECT */
9299#endif /* SIGIO */
9300#endif
9301
9302 /* For debugging, this gives a way to fake an I/O error. */
9303 if (dpyinfo == XTread_socket_fake_io_error)
9304 {
9305 XTread_socket_fake_io_error = 0;
9306 x_io_error_quitter (dpyinfo->display);
dc6f92b8 9307 }
bf338245
JD
9308
9309#ifdef HAVE_X_SM
9310 BLOCK_INPUT;
9311 count += x_session_check_input (bufp, &numchars);
9312 UNBLOCK_INPUT;
9313#endif
9314
488dd4c4
JD
9315#ifdef USE_GTK
9316 /* For GTK we must use the GTK event loop. But XEvents gets passed
9317 to our filter function above, and then to the big event switch.
9318 We use a bunch of globals to communicate with our filter function,
9319 that is kind of ugly, but it works. */
9320 current_dpyinfo = dpyinfo;
177c0ea7 9321
488dd4c4
JD
9322 while (gtk_events_pending ())
9323 {
9324 static int nr = 0;
9325 current_count = count;
9326 current_numcharsp = &numchars;
9327 current_bufp = &bufp;
9328
9329 gtk_main_iteration ();
9330
9331 count = current_count;
9332 current_bufp = 0;
9333 current_numcharsp = 0;
9334
9335 if (current_finish == X_EVENT_GOTO_OUT)
9336 goto out;
9337 }
9338
9339#else /* not USE_GTK */
bf338245
JD
9340 while (XPending (dpyinfo->display))
9341 {
9342 int finish;
177c0ea7 9343
bf338245
JD
9344 XNextEvent (dpyinfo->display, &event);
9345
9346#ifdef HAVE_X_I18N
1fcfb866
JD
9347 /* Filter events for the current X input method. */
9348 if (x_filter_event (dpyinfo, &event))
9349 break;
bf338245
JD
9350#endif
9351 event_found = 1;
9352
9353 count += handle_one_xevent (dpyinfo,
9354 &event,
9355 &bufp,
9356 &numchars,
9357 &finish);
9358
9359 if (finish == X_EVENT_GOTO_OUT)
9360 goto out;
9361 }
488dd4c4 9362#endif /* USE_GTK */
dc6f92b8
JB
9363 }
9364
06a2c219
GM
9365 out:;
9366
9a5196d0
RS
9367 /* On some systems, an X bug causes Emacs to get no more events
9368 when the window is destroyed. Detect that. (1994.) */
58769bee 9369 if (! event_found)
ef2a22d0 9370 {
ef2a22d0
RS
9371 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
9372 One XNOOP in 100 loops will make Emacs terminate.
9373 B. Bretthauer, 1994 */
9374 x_noop_count++;
58769bee 9375 if (x_noop_count >= 100)
ef2a22d0
RS
9376 {
9377 x_noop_count=0;
2224b905
RS
9378
9379 if (next_noop_dpyinfo == 0)
9380 next_noop_dpyinfo = x_display_list;
9381
9382 XNoOp (next_noop_dpyinfo->display);
9383
9384 /* Each time we get here, cycle through the displays now open. */
9385 next_noop_dpyinfo = next_noop_dpyinfo->next;
ef2a22d0
RS
9386 }
9387 }
502add23 9388
06a2c219 9389 /* If the focus was just given to an auto-raising frame,
0134a210 9390 raise it now. */
7a13e894 9391 /* ??? This ought to be able to handle more than one such frame. */
0134a210
RS
9392 if (pending_autoraise_frame)
9393 {
9394 x_raise_frame (pending_autoraise_frame);
9395 pending_autoraise_frame = 0;
9396 }
0134a210 9397
dc6f92b8 9398 UNBLOCK_INPUT;
bde5503b 9399 --handling_signal;
dc6f92b8
JB
9400 return count;
9401}
06a2c219
GM
9402
9403
9404
dc6f92b8 9405\f
06a2c219
GM
9406/***********************************************************************
9407 Text Cursor
9408 ***********************************************************************/
9409
06a2c219
GM
9410/* Set clipping for output in glyph row ROW. W is the window in which
9411 we operate. GC is the graphics context to set clipping in.
9412 WHOLE_LINE_P non-zero means include the areas used for truncation
9413 mark display and alike in the clipping rectangle.
9414
9415 ROW may be a text row or, e.g., a mode line. Text rows must be
9416 clipped to the interior of the window dedicated to text display,
9417 mode lines must be clipped to the whole window. */
dc6f92b8
JB
9418
9419static void
06a2c219
GM
9420x_clip_to_row (w, row, gc, whole_line_p)
9421 struct window *w;
9422 struct glyph_row *row;
9423 GC gc;
9424 int whole_line_p;
dc6f92b8 9425{
06a2c219
GM
9426 struct frame *f = XFRAME (WINDOW_FRAME (w));
9427 XRectangle clip_rect;
9428 int window_x, window_y, window_width, window_height;
dc6f92b8 9429
06a2c219 9430 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5c1aae96 9431
06a2c219
GM
9432 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
9433 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
9434 clip_rect.y = max (clip_rect.y, window_y);
9435 clip_rect.width = window_width;
9436 clip_rect.height = row->visible_height;
5c1aae96 9437
06a2c219
GM
9438 /* If clipping to the whole line, including trunc marks, extend
9439 the rectangle to the left and increase its width. */
9440 if (whole_line_p)
9441 {
3f332ef3
KS
9442 clip_rect.x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
9443 clip_rect.width += FRAME_X_FRINGE_WIDTH (f);
06a2c219 9444 }
5c1aae96 9445
06a2c219 9446 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
dc6f92b8
JB
9447}
9448
06a2c219
GM
9449
9450/* Draw a hollow box cursor on window W in glyph row ROW. */
dc6f92b8
JB
9451
9452static void
06a2c219
GM
9453x_draw_hollow_cursor (w, row)
9454 struct window *w;
9455 struct glyph_row *row;
dc6f92b8 9456{
06a2c219
GM
9457 struct frame *f = XFRAME (WINDOW_FRAME (w));
9458 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9459 Display *dpy = FRAME_X_DISPLAY (f);
9460 int x, y, wd, h;
9461 XGCValues xgcv;
9462 struct glyph *cursor_glyph;
9463 GC gc;
9464
9465 /* Compute frame-relative coordinates from window-relative
9466 coordinates. */
9467 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9468 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
9469 + row->ascent - w->phys_cursor_ascent);
9470 h = row->height - 1;
9471
9472 /* Get the glyph the cursor is on. If we can't tell because
9473 the current matrix is invalid or such, give up. */
9474 cursor_glyph = get_phys_cursor_glyph (w);
9475 if (cursor_glyph == NULL)
dc6f92b8
JB
9476 return;
9477
06a2c219
GM
9478 /* Compute the width of the rectangle to draw. If on a stretch
9479 glyph, and `x-stretch-block-cursor' is nil, don't draw a
9480 rectangle as wide as the glyph, but use a canonical character
9481 width instead. */
9482 wd = cursor_glyph->pixel_width - 1;
9483 if (cursor_glyph->type == STRETCH_GLYPH
9484 && !x_stretch_cursor_p)
9485 wd = min (CANON_X_UNIT (f), wd);
b3738089 9486 w->phys_cursor_width = wd;
7d0393cf 9487
06a2c219
GM
9488 /* The foreground of cursor_gc is typically the same as the normal
9489 background color, which can cause the cursor box to be invisible. */
9490 xgcv.foreground = f->output_data.x->cursor_pixel;
9491 if (dpyinfo->scratch_cursor_gc)
9492 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
9493 else
9494 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
9495 GCForeground, &xgcv);
9496 gc = dpyinfo->scratch_cursor_gc;
9497
9498 /* Set clipping, draw the rectangle, and reset clipping again. */
9499 x_clip_to_row (w, row, gc, 0);
9500 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
9501 XSetClipMask (dpy, gc, None);
dc6f92b8
JB
9502}
9503
06a2c219
GM
9504
9505/* Draw a bar cursor on window W in glyph row ROW.
9506
9507 Implementation note: One would like to draw a bar cursor with an
9508 angle equal to the one given by the font property XA_ITALIC_ANGLE.
9509 Unfortunately, I didn't find a font yet that has this property set.
9510 --gerd. */
dc6f92b8
JB
9511
9512static void
65c26775 9513x_draw_bar_cursor (w, row, width, kind)
06a2c219
GM
9514 struct window *w;
9515 struct glyph_row *row;
f02d8aa0 9516 int width;
65c26775 9517 enum text_cursor_kinds kind;
dc6f92b8 9518{
92f424df
GM
9519 struct frame *f = XFRAME (w->frame);
9520 struct glyph *cursor_glyph;
7d0393cf 9521
92f424df
GM
9522 /* If cursor is out of bounds, don't draw garbage. This can happen
9523 in mini-buffer windows when switching between echo area glyphs
9524 and mini-buffer. */
9525 cursor_glyph = get_phys_cursor_glyph (w);
9526 if (cursor_glyph == NULL)
9527 return;
06a2c219 9528
92f424df
GM
9529 /* If on an image, draw like a normal cursor. That's usually better
9530 visible than drawing a bar, esp. if the image is large so that
9531 the bar might not be in the window. */
9532 if (cursor_glyph->type == IMAGE_GLYPH)
9533 {
9534 struct glyph_row *row;
9535 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
9536 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
9537 }
9538 else
9539 {
34e5d0af
GM
9540 Display *dpy = FRAME_X_DISPLAY (f);
9541 Window window = FRAME_X_WINDOW (f);
9542 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
9543 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
9544 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
9545 XGCValues xgcv;
9546
9547 /* If the glyph's background equals the color we normally draw
9548 the bar cursor in, the bar cursor in its normal color is
9549 invisible. Use the glyph's foreground color instead in this
9550 case, on the assumption that the glyph's colors are chosen so
9551 that the glyph is legible. */
9552 if (face->background == f->output_data.x->cursor_pixel)
9553 xgcv.background = xgcv.foreground = face->foreground;
9554 else
9555 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
06a2c219 9556 xgcv.graphics_exposures = 0;
7d0393cf 9557
06a2c219
GM
9558 if (gc)
9559 XChangeGC (dpy, gc, mask, &xgcv);
9560 else
9561 {
9562 gc = XCreateGC (dpy, window, mask, &xgcv);
9563 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
9564 }
7d0393cf 9565
f02d8aa0 9566 if (width < 0)
fdbe859c 9567 width = FRAME_CURSOR_WIDTH (f);
34e5d0af 9568 width = min (cursor_glyph->pixel_width, width);
7d0393cf 9569
b3738089 9570 w->phys_cursor_width = width;
06a2c219 9571 x_clip_to_row (w, row, gc, 0);
7d0393cf 9572
65c26775
EZ
9573 if (kind == BAR_CURSOR)
9574 XFillRectangle (dpy, window, gc,
9575 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
9576 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
9577 width, row->height);
9578 else
9579 XFillRectangle (dpy, window, gc,
9580 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
9581 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
9582 row->height - width),
9583 cursor_glyph->pixel_width,
9584 width);
9585
06a2c219
GM
9586 XSetClipMask (dpy, gc, None);
9587 }
dc6f92b8
JB
9588}
9589
06a2c219
GM
9590
9591/* Clear the cursor of window W to background color, and mark the
9592 cursor as not shown. This is used when the text where the cursor
9593 is is about to be rewritten. */
9594
dc6f92b8 9595static void
06a2c219
GM
9596x_clear_cursor (w)
9597 struct window *w;
dc6f92b8 9598{
06a2c219
GM
9599 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
9600 x_update_window_cursor (w, 0);
9601}
90e65f07 9602
dbc4e1c1 9603
06a2c219
GM
9604/* Draw the cursor glyph of window W in glyph row ROW. See the
9605 comment of x_draw_glyphs for the meaning of HL. */
dbc4e1c1 9606
06a2c219
GM
9607static void
9608x_draw_phys_cursor_glyph (w, row, hl)
9609 struct window *w;
9610 struct glyph_row *row;
9611 enum draw_glyphs_face hl;
9612{
9613 /* If cursor hpos is out of bounds, don't draw garbage. This can
9614 happen in mini-buffer windows when switching between echo area
9615 glyphs and mini-buffer. */
9616 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
66ac4b0e 9617 {
f0a48a01 9618 int on_p = w->phys_cursor_on_p;
56a0382c 9619 int x1;
7d0393cf 9620
56a0382c
GM
9621 x1 = x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
9622 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
9623 hl, 0);
f0a48a01 9624 w->phys_cursor_on_p = on_p;
66ac4b0e 9625
b3738089 9626 if (hl == DRAW_CURSOR)
56a0382c 9627 w->phys_cursor_width = x1 - w->phys_cursor.x;
b3738089 9628
66ac4b0e
GM
9629 /* When we erase the cursor, and ROW is overlapped by other
9630 rows, make sure that these overlapping parts of other rows
9631 are redrawn. */
b3738089 9632 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
66ac4b0e
GM
9633 {
9634 if (row > w->current_matrix->rows
9635 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
9636 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
9637
9638 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
9639 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
9640 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
9641 }
9642 }
06a2c219 9643}
dbc4e1c1 9644
eea6af04 9645
06a2c219 9646/* Erase the image of a cursor of window W from the screen. */
eea6af04 9647
06a2c219
GM
9648static void
9649x_erase_phys_cursor (w)
9650 struct window *w;
9651{
9652 struct frame *f = XFRAME (w->frame);
9653 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9654 int hpos = w->phys_cursor.hpos;
9655 int vpos = w->phys_cursor.vpos;
9656 int mouse_face_here_p = 0;
9657 struct glyph_matrix *active_glyphs = w->current_matrix;
9658 struct glyph_row *cursor_row;
9659 struct glyph *cursor_glyph;
9660 enum draw_glyphs_face hl;
9661
9662 /* No cursor displayed or row invalidated => nothing to do on the
9663 screen. */
9664 if (w->phys_cursor_type == NO_CURSOR)
9665 goto mark_cursor_off;
7d0393cf 9666
06a2c219
GM
9667 /* VPOS >= active_glyphs->nrows means that window has been resized.
9668 Don't bother to erase the cursor. */
9669 if (vpos >= active_glyphs->nrows)
9670 goto mark_cursor_off;
9671
9672 /* If row containing cursor is marked invalid, there is nothing we
9673 can do. */
9674 cursor_row = MATRIX_ROW (active_glyphs, vpos);
9675 if (!cursor_row->enabled_p)
9676 goto mark_cursor_off;
7d0393cf 9677
a2c6de8e
KS
9678 /* If row is completely invisible, don't attempt to delete a cursor which
9679 isn't there. This can happen if cursor is at top of a window, and
9680 we switch to a buffer with a header line in that window. */
9681 if (cursor_row->visible_height <= 0)
9682 goto mark_cursor_off;
7d0393cf 9683
06a2c219
GM
9684 /* This can happen when the new row is shorter than the old one.
9685 In this case, either x_draw_glyphs or clear_end_of_line
9686 should have cleared the cursor. Note that we wouldn't be
9687 able to erase the cursor in this case because we don't have a
9688 cursor glyph at hand. */
9689 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
9690 goto mark_cursor_off;
7d0393cf 9691
06a2c219
GM
9692 /* If the cursor is in the mouse face area, redisplay that when
9693 we clear the cursor. */
8801a864
KR
9694 if (! NILP (dpyinfo->mouse_face_window)
9695 && w == XWINDOW (dpyinfo->mouse_face_window)
06a2c219
GM
9696 && (vpos > dpyinfo->mouse_face_beg_row
9697 || (vpos == dpyinfo->mouse_face_beg_row
9698 && hpos >= dpyinfo->mouse_face_beg_col))
9699 && (vpos < dpyinfo->mouse_face_end_row
9700 || (vpos == dpyinfo->mouse_face_end_row
9701 && hpos < dpyinfo->mouse_face_end_col))
9702 /* Don't redraw the cursor's spot in mouse face if it is at the
9703 end of a line (on a newline). The cursor appears there, but
9704 mouse highlighting does not. */
9705 && cursor_row->used[TEXT_AREA] > hpos)
9706 mouse_face_here_p = 1;
9707
9708 /* Maybe clear the display under the cursor. */
9709 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
9710 {
9711 int x;
045dee35 9712 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
dbc4e1c1 9713
06a2c219
GM
9714 cursor_glyph = get_phys_cursor_glyph (w);
9715 if (cursor_glyph == NULL)
9716 goto mark_cursor_off;
dbc4e1c1 9717
e0300d33 9718 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
7d0393cf 9719
c5e6e06b
GM
9720 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9721 x,
9722 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
9723 cursor_row->y)),
9724 cursor_glyph->pixel_width,
9725 cursor_row->visible_height,
9726 False);
dbc4e1c1 9727 }
7d0393cf 9728
06a2c219
GM
9729 /* Erase the cursor by redrawing the character underneath it. */
9730 if (mouse_face_here_p)
9731 hl = DRAW_MOUSE_FACE;
06a2c219
GM
9732 else
9733 hl = DRAW_NORMAL_TEXT;
9734 x_draw_phys_cursor_glyph (w, cursor_row, hl);
dbc4e1c1 9735
06a2c219
GM
9736 mark_cursor_off:
9737 w->phys_cursor_on_p = 0;
9738 w->phys_cursor_type = NO_CURSOR;
dbc4e1c1
JB
9739}
9740
9741
b7f83f9e
GM
9742/* Non-zero if physical cursor of window W is within mouse face. */
9743
9744static int
9745cursor_in_mouse_face_p (w)
9746 struct window *w;
9747{
9748 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
9749 int in_mouse_face = 0;
7d0393cf 9750
b7f83f9e
GM
9751 if (WINDOWP (dpyinfo->mouse_face_window)
9752 && XWINDOW (dpyinfo->mouse_face_window) == w)
9753 {
9754 int hpos = w->phys_cursor.hpos;
9755 int vpos = w->phys_cursor.vpos;
9756
9757 if (vpos >= dpyinfo->mouse_face_beg_row
9758 && vpos <= dpyinfo->mouse_face_end_row
9759 && (vpos > dpyinfo->mouse_face_beg_row
9760 || hpos >= dpyinfo->mouse_face_beg_col)
9761 && (vpos < dpyinfo->mouse_face_end_row
9762 || hpos < dpyinfo->mouse_face_end_col
9763 || dpyinfo->mouse_face_past_end))
9764 in_mouse_face = 1;
9765 }
9766
9767 return in_mouse_face;
9768}
9769
9770
06a2c219
GM
9771/* Display or clear cursor of window W. If ON is zero, clear the
9772 cursor. If it is non-zero, display the cursor. If ON is nonzero,
9773 where to put the cursor is specified by HPOS, VPOS, X and Y. */
dbc4e1c1 9774
06a2c219
GM
9775void
9776x_display_and_set_cursor (w, on, hpos, vpos, x, y)
9777 struct window *w;
9778 int on, hpos, vpos, x, y;
dbc4e1c1 9779{
06a2c219
GM
9780 struct frame *f = XFRAME (w->frame);
9781 int new_cursor_type;
f02d8aa0 9782 int new_cursor_width;
e9c99027 9783 int active_cursor;
06a2c219
GM
9784 struct glyph_matrix *current_glyphs;
9785 struct glyph_row *glyph_row;
9786 struct glyph *glyph;
dbc4e1c1 9787
49d838ea 9788 /* This is pointless on invisible frames, and dangerous on garbaged
06a2c219
GM
9789 windows and frames; in the latter case, the frame or window may
9790 be in the midst of changing its size, and x and y may be off the
9791 window. */
9792 if (! FRAME_VISIBLE_P (f)
9793 || FRAME_GARBAGED_P (f)
9794 || vpos >= w->current_matrix->nrows
9795 || hpos >= w->current_matrix->matrix_w)
dc6f92b8
JB
9796 return;
9797
9798 /* If cursor is off and we want it off, return quickly. */
06a2c219 9799 if (!on && !w->phys_cursor_on_p)
dc6f92b8
JB
9800 return;
9801
06a2c219
GM
9802 current_glyphs = w->current_matrix;
9803 glyph_row = MATRIX_ROW (current_glyphs, vpos);
9804 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
7d0393cf
JB
9805
9806 /* If cursor row is not enabled, we don't really know where to
06a2c219
GM
9807 display the cursor. */
9808 if (!glyph_row->enabled_p)
9809 {
9810 w->phys_cursor_on_p = 0;
9811 return;
9812 }
9813
9814 xassert (interrupt_input_blocked);
9815
fdbe859c 9816 /* Set new_cursor_type to the cursor we want to be displayed. */
e9c99027 9817 new_cursor_type = get_window_cursor_type (w, &new_cursor_width, &active_cursor);
f7e039de 9818
06a2c219
GM
9819 /* If cursor is currently being shown and we don't want it to be or
9820 it is in the wrong place, or the cursor type is not what we want,
dc6f92b8 9821 erase it. */
06a2c219 9822 if (w->phys_cursor_on_p
dc6f92b8 9823 && (!on
06a2c219
GM
9824 || w->phys_cursor.x != x
9825 || w->phys_cursor.y != y
e1429afe 9826 || new_cursor_type != w->phys_cursor_type
fdbe859c 9827 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
e1429afe 9828 && new_cursor_width != w->phys_cursor_width)))
06a2c219
GM
9829 x_erase_phys_cursor (w);
9830
56a0382c
GM
9831 /* Don't check phys_cursor_on_p here because that flag is only set
9832 to zero in some cases where we know that the cursor has been
9833 completely erased, to avoid the extra work of erasing the cursor
9834 twice. In other words, phys_cursor_on_p can be 1 and the cursor
9835 still not be visible, or it has only been partly erased. */
9836 if (on)
06a2c219
GM
9837 {
9838 w->phys_cursor_ascent = glyph_row->ascent;
9839 w->phys_cursor_height = glyph_row->height;
7d0393cf 9840
06a2c219
GM
9841 /* Set phys_cursor_.* before x_draw_.* is called because some
9842 of them may need the information. */
9843 w->phys_cursor.x = x;
9844 w->phys_cursor.y = glyph_row->y;
9845 w->phys_cursor.hpos = hpos;
9846 w->phys_cursor.vpos = vpos;
9847 w->phys_cursor_type = new_cursor_type;
9848 w->phys_cursor_on_p = 1;
9849
9850 switch (new_cursor_type)
dc6f92b8 9851 {
06a2c219
GM
9852 case HOLLOW_BOX_CURSOR:
9853 x_draw_hollow_cursor (w, glyph_row);
9854 break;
9855
9856 case FILLED_BOX_CURSOR:
9857 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
9858 break;
9859
9860 case BAR_CURSOR:
65c26775
EZ
9861 x_draw_bar_cursor (w, glyph_row, new_cursor_width, BAR_CURSOR);
9862 break;
9863
9864 case HBAR_CURSOR:
b3738089 9865 x_draw_bar_cursor (w, glyph_row, new_cursor_width, HBAR_CURSOR);
06a2c219
GM
9866 break;
9867
9868 case NO_CURSOR:
4398e673 9869 w->phys_cursor_width = 0;
06a2c219 9870 break;
dc6f92b8 9871
06a2c219
GM
9872 default:
9873 abort ();
9874 }
7d0393cf 9875
59ddecde
GM
9876#ifdef HAVE_X_I18N
9877 if (w == XWINDOW (f->selected_window))
9878 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
9879 xic_set_preeditarea (w, x, y);
9880#endif
dc6f92b8
JB
9881 }
9882
06a2c219 9883#ifndef XFlush
f676886a 9884 if (updating_frame != f)
334208b7 9885 XFlush (FRAME_X_DISPLAY (f));
06a2c219 9886#endif
dc6f92b8
JB
9887}
9888
06a2c219
GM
9889
9890/* Display the cursor on window W, or clear it. X and Y are window
9891 relative pixel coordinates. HPOS and VPOS are glyph matrix
9892 positions. If W is not the selected window, display a hollow
9893 cursor. ON non-zero means display the cursor at X, Y which
9894 correspond to HPOS, VPOS, otherwise it is cleared. */
5d46f928 9895
dfcf069d 9896void
06a2c219
GM
9897x_display_cursor (w, on, hpos, vpos, x, y)
9898 struct window *w;
9899 int on, hpos, vpos, x, y;
dc6f92b8 9900{
f94397b5 9901 BLOCK_INPUT;
06a2c219 9902 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
5d46f928
RS
9903 UNBLOCK_INPUT;
9904}
9905
06a2c219
GM
9906
9907/* Display the cursor on window W, or clear it, according to ON_P.
5d46f928
RS
9908 Don't change the cursor's position. */
9909
dfcf069d 9910void
06a2c219 9911x_update_cursor (f, on_p)
5d46f928 9912 struct frame *f;
0971e730 9913 int on_p;
5d46f928 9914{
06a2c219
GM
9915 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
9916}
9917
9918
9919/* Call x_update_window_cursor with parameter ON_P on all leaf windows
9920 in the window tree rooted at W. */
9921
9922static void
9923x_update_cursor_in_window_tree (w, on_p)
9924 struct window *w;
9925 int on_p;
9926{
9927 while (w)
9928 {
9929 if (!NILP (w->hchild))
9930 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
9931 else if (!NILP (w->vchild))
9932 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
9933 else
9934 x_update_window_cursor (w, on_p);
9935
9936 w = NILP (w->next) ? 0 : XWINDOW (w->next);
9937 }
9938}
5d46f928 9939
f94397b5 9940
06a2c219
GM
9941/* Switch the display of W's cursor on or off, according to the value
9942 of ON. */
9943
9944static void
9945x_update_window_cursor (w, on)
9946 struct window *w;
9947 int on;
9948{
16b5d424
GM
9949 /* Don't update cursor in windows whose frame is in the process
9950 of being deleted. */
9951 if (w->current_matrix)
9952 {
9953 BLOCK_INPUT;
9954 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
9955 w->phys_cursor.x, w->phys_cursor.y);
9956 UNBLOCK_INPUT;
9957 }
dc6f92b8 9958}
06a2c219
GM
9959
9960
9961
dc6f92b8
JB
9962\f
9963/* Icons. */
9964
dbc4e1c1 9965/* Make the x-window of frame F use the gnu icon bitmap. */
dc6f92b8
JB
9966
9967int
990ba854 9968x_bitmap_icon (f, file)
f676886a 9969 struct frame *f;
990ba854 9970 Lisp_Object file;
dc6f92b8 9971{
06a2c219 9972 int bitmap_id;
dc6f92b8 9973
c118dd06 9974 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
9975 return 1;
9976
990ba854 9977 /* Free up our existing icon bitmap if any. */
7556890b
RS
9978 if (f->output_data.x->icon_bitmap > 0)
9979 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
9980 f->output_data.x->icon_bitmap = 0;
990ba854
RS
9981
9982 if (STRINGP (file))
7f2ae036
RS
9983 bitmap_id = x_create_bitmap_from_file (f, file);
9984 else
9985 {
990ba854 9986 /* Create the GNU bitmap if necessary. */
5bf01b68 9987 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
334208b7
RS
9988 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
9989 = x_create_bitmap_from_data (f, gnu_bits,
9990 gnu_width, gnu_height);
990ba854
RS
9991
9992 /* The first time we create the GNU bitmap,
06a2c219 9993 this increments the ref-count one extra time.
990ba854
RS
9994 As a result, the GNU bitmap is never freed.
9995 That way, we don't have to worry about allocating it again. */
334208b7 9996 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
990ba854 9997
334208b7 9998 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7f2ae036
RS
9999 }
10000
10001 x_wm_set_icon_pixmap (f, bitmap_id);
7556890b 10002 f->output_data.x->icon_bitmap = bitmap_id;
dc6f92b8
JB
10003
10004 return 0;
10005}
10006
10007
1be2d067
KH
10008/* Make the x-window of frame F use a rectangle with text.
10009 Use ICON_NAME as the text. */
dc6f92b8
JB
10010
10011int
f676886a
JB
10012x_text_icon (f, icon_name)
10013 struct frame *f;
dc6f92b8
JB
10014 char *icon_name;
10015{
c118dd06 10016 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
10017 return 1;
10018
1be2d067
KH
10019#ifdef HAVE_X11R4
10020 {
10021 XTextProperty text;
10022 text.value = (unsigned char *) icon_name;
10023 text.encoding = XA_STRING;
10024 text.format = 8;
10025 text.nitems = strlen (icon_name);
2436a4e4 10026 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
1be2d067
KH
10027 }
10028#else /* not HAVE_X11R4 */
2436a4e4 10029 XSetIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), icon_name);
1be2d067 10030#endif /* not HAVE_X11R4 */
58769bee 10031
7556890b
RS
10032 if (f->output_data.x->icon_bitmap > 0)
10033 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10034 f->output_data.x->icon_bitmap = 0;
b1c884c3 10035 x_wm_set_icon_pixmap (f, 0);
dc6f92b8
JB
10036
10037 return 0;
10038}
10039\f
e99db5a1
RS
10040#define X_ERROR_MESSAGE_SIZE 200
10041
10042/* If non-nil, this should be a string.
10043 It means catch X errors and store the error message in this string. */
10044
10045static Lisp_Object x_error_message_string;
10046
10047/* An X error handler which stores the error message in
10048 x_error_message_string. This is called from x_error_handler if
10049 x_catch_errors is in effect. */
10050
06a2c219 10051static void
e99db5a1
RS
10052x_error_catcher (display, error)
10053 Display *display;
10054 XErrorEvent *error;
10055{
10056 XGetErrorText (display, error->error_code,
d5db4077 10057 SDATA (x_error_message_string),
e99db5a1
RS
10058 X_ERROR_MESSAGE_SIZE);
10059}
10060
10061/* Begin trapping X errors for display DPY. Actually we trap X errors
10062 for all displays, but DPY should be the display you are actually
10063 operating on.
10064
10065 After calling this function, X protocol errors no longer cause
10066 Emacs to exit; instead, they are recorded in the string
10067 stored in x_error_message_string.
10068
10069 Calling x_check_errors signals an Emacs error if an X error has
10070 occurred since the last call to x_catch_errors or x_check_errors.
10071
10072 Calling x_uncatch_errors resumes the normal error handling. */
10073
10074void x_check_errors ();
10075static Lisp_Object x_catch_errors_unwind ();
10076
10077int
10078x_catch_errors (dpy)
10079 Display *dpy;
10080{
aed13378 10081 int count = SPECPDL_INDEX ();
e99db5a1
RS
10082
10083 /* Make sure any errors from previous requests have been dealt with. */
10084 XSync (dpy, False);
10085
781d152a
RS
10086 record_unwind_protect (x_catch_errors_unwind,
10087 Fcons (make_save_value (dpy, 0),
10088 x_error_message_string));
e99db5a1
RS
10089
10090 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
7da167cd 10091 SSET (x_error_message_string, 0, 0);
e99db5a1
RS
10092
10093 return count;
10094}
10095
10096/* Unbind the binding that we made to check for X errors. */
10097
10098static Lisp_Object
10099x_catch_errors_unwind (old_val)
10100 Lisp_Object old_val;
10101{
781d152a
RS
10102 Lisp_Object first;
10103
10104 first = XCAR (old_val);
10105
10106 XSync (XSAVE_VALUE (first)->pointer, False);
10107
10108 x_error_message_string = XCDR (old_val);
e99db5a1
RS
10109 return Qnil;
10110}
10111
10112/* If any X protocol errors have arrived since the last call to
10113 x_catch_errors or x_check_errors, signal an Emacs error using
10114 sprintf (a buffer, FORMAT, the x error message text) as the text. */
10115
10116void
10117x_check_errors (dpy, format)
10118 Display *dpy;
10119 char *format;
10120{
10121 /* Make sure to catch any errors incurred so far. */
10122 XSync (dpy, False);
10123
d5db4077
KR
10124 if (SREF (x_error_message_string, 0))
10125 error (format, SDATA (x_error_message_string));
e99db5a1
RS
10126}
10127
9829ddba
RS
10128/* Nonzero if we had any X protocol errors
10129 since we did x_catch_errors on DPY. */
e99db5a1
RS
10130
10131int
10132x_had_errors_p (dpy)
10133 Display *dpy;
10134{
10135 /* Make sure to catch any errors incurred so far. */
10136 XSync (dpy, False);
10137
d5db4077 10138 return SREF (x_error_message_string, 0) != 0;
e99db5a1
RS
10139}
10140
9829ddba
RS
10141/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
10142
06a2c219 10143void
9829ddba
RS
10144x_clear_errors (dpy)
10145 Display *dpy;
10146{
7da167cd 10147 SSET (x_error_message_string, 0, 0);
9829ddba
RS
10148}
10149
e99db5a1
RS
10150/* Stop catching X protocol errors and let them make Emacs die.
10151 DPY should be the display that was passed to x_catch_errors.
10152 COUNT should be the value that was returned by
10153 the corresponding call to x_catch_errors. */
10154
10155void
10156x_uncatch_errors (dpy, count)
10157 Display *dpy;
10158 int count;
10159{
10160 unbind_to (count, Qnil);
10161}
10162
10163#if 0
10164static unsigned int x_wire_count;
10165x_trace_wire ()
10166{
10167 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
10168}
10169#endif /* ! 0 */
10170
10171\f
10172/* Handle SIGPIPE, which can happen when the connection to a server
10173 simply goes away. SIGPIPE is handled by x_connection_signal.
7d0393cf 10174 Don't need to do anything, because the write which caused the
e99db5a1 10175 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
06a2c219 10176 which will do the appropriate cleanup for us. */
7d0393cf 10177
e99db5a1
RS
10178static SIGTYPE
10179x_connection_signal (signalnum) /* If we don't have an argument, */
06a2c219 10180 int signalnum; /* some compilers complain in signal calls. */
e99db5a1
RS
10181{
10182#ifdef USG
10183 /* USG systems forget handlers when they are used;
10184 must reestablish each time */
10185 signal (signalnum, x_connection_signal);
10186#endif /* USG */
10187}
0da1ab50 10188
e99db5a1 10189\f
0da1ab50
GM
10190/************************************************************************
10191 Handling X errors
10192 ************************************************************************/
4746118a 10193
f0e299de
GM
10194/* Error message passed to x_connection_closed. */
10195
10196static char *error_msg;
10197
a7248e4f 10198/* Function installed as fatal_error_signal_hook in
f0e299de
GM
10199 x_connection_closed. Print the X error message, and exit normally,
10200 instead of dumping core when XtCloseDisplay fails. */
10201
10202static void
10203x_fatal_error_signal ()
10204{
10205 fprintf (stderr, "%s\n", error_msg);
10206 exit (70);
10207}
10208
0da1ab50
GM
10209/* Handle the loss of connection to display DPY. ERROR_MESSAGE is
10210 the text of an error message that lead to the connection loss. */
16bd92ea 10211
4746118a 10212static SIGTYPE
5978125e
GM
10213x_connection_closed (dpy, error_message)
10214 Display *dpy;
7a13e894 10215 char *error_message;
4746118a 10216{
5978125e 10217 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7a13e894 10218 Lisp_Object frame, tail;
0da1ab50 10219 int count;
7d0393cf 10220
f0e299de
GM
10221 error_msg = (char *) alloca (strlen (error_message) + 1);
10222 strcpy (error_msg, error_message);
1a532e54 10223 handling_signal = 0;
7d0393cf 10224
0da1ab50
GM
10225 /* Prevent being called recursively because of an error condition
10226 below. Otherwise, we might end up with printing ``can't find per
10227 display information'' in the recursive call instead of printing
10228 the original message here. */
10229 count = x_catch_errors (dpy);
7d0393cf 10230
8a4f36cc
GM
10231 /* We have to close the display to inform Xt that it doesn't
10232 exist anymore. If we don't, Xt will continue to wait for
10233 events from the display. As a consequence, a sequence of
10234
10235 M-x make-frame-on-display RET :1 RET
10236 ...kill the new frame, so that we get an IO error...
10237 M-x make-frame-on-display RET :1 RET
10238
10239 will indefinitely wait in Xt for events for display `:1', opened
10240 in the first class to make-frame-on-display.
6186a4a0 10241
8a4f36cc
GM
10242 Closing the display is reported to lead to a bus error on
10243 OpenWindows in certain situations. I suspect that is a bug
10244 in OpenWindows. I don't know how to cicumvent it here. */
7d0393cf 10245
f613a4c8 10246#ifdef USE_X_TOOLKIT
ae24cb3b
GM
10247 /* If DPYINFO is null, this means we didn't open the display
10248 in the first place, so don't try to close it. */
10249 if (dpyinfo)
f0e299de
GM
10250 {
10251 extern void (*fatal_error_signal_hook) P_ ((void));
10252 fatal_error_signal_hook = x_fatal_error_signal;
10253 XtCloseDisplay (dpy);
10254 fatal_error_signal_hook = NULL;
10255 }
f613a4c8 10256#endif
adabc3a9 10257
8a4f36cc 10258 /* Indicate that this display is dead. */
9e80b57d
KR
10259 if (dpyinfo)
10260 dpyinfo->display = 0;
6186a4a0 10261
06a2c219 10262 /* First delete frames whose mini-buffers are on frames
7a13e894
RS
10263 that are on the dead display. */
10264 FOR_EACH_FRAME (tail, frame)
10265 {
10266 Lisp_Object minibuf_frame;
10267 minibuf_frame
10268 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
f48f33ca
RS
10269 if (FRAME_X_P (XFRAME (frame))
10270 && FRAME_X_P (XFRAME (minibuf_frame))
10271 && ! EQ (frame, minibuf_frame)
10272 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7a13e894
RS
10273 Fdelete_frame (frame, Qt);
10274 }
10275
10276 /* Now delete all remaining frames on the dead display.
06a2c219 10277 We are now sure none of these is used as the mini-buffer
7a13e894
RS
10278 for another frame that we need to delete. */
10279 FOR_EACH_FRAME (tail, frame)
f48f33ca
RS
10280 if (FRAME_X_P (XFRAME (frame))
10281 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
07a7096a
KH
10282 {
10283 /* Set this to t so that Fdelete_frame won't get confused
10284 trying to find a replacement. */
10285 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
10286 Fdelete_frame (frame, Qt);
10287 }
7a13e894 10288
482a1bd2
KH
10289 if (dpyinfo)
10290 x_delete_display (dpyinfo);
7a13e894 10291
0da1ab50 10292 x_uncatch_errors (dpy, count);
7d0393cf 10293
7a13e894
RS
10294 if (x_display_list == 0)
10295 {
f0e299de 10296 fprintf (stderr, "%s\n", error_msg);
7a13e894
RS
10297 shut_down_emacs (0, 0, Qnil);
10298 exit (70);
10299 }
12ba150f 10300
7a13e894
RS
10301 /* Ordinary stack unwind doesn't deal with these. */
10302#ifdef SIGIO
10303 sigunblock (sigmask (SIGIO));
10304#endif
10305 sigunblock (sigmask (SIGALRM));
10306 TOTALLY_UNBLOCK_INPUT;
10307
aa4d9a9e 10308 clear_waiting_for_input ();
f0e299de 10309 error ("%s", error_msg);
4746118a
JB
10310}
10311
0da1ab50 10312
7a13e894
RS
10313/* This is the usual handler for X protocol errors.
10314 It kills all frames on the display that we got the error for.
10315 If that was the only one, it prints an error message and kills Emacs. */
10316
06a2c219 10317static void
c118dd06
JB
10318x_error_quitter (display, error)
10319 Display *display;
10320 XErrorEvent *error;
10321{
7a13e894 10322 char buf[256], buf1[356];
dc6f92b8 10323
58769bee 10324 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 10325 original error handler. */
dc6f92b8 10326
c118dd06 10327 XGetErrorText (display, error->error_code, buf, sizeof (buf));
0a0fdc70 10328 sprintf (buf1, "X protocol error: %s on protocol request %d",
c118dd06 10329 buf, error->request_code);
7a13e894 10330 x_connection_closed (display, buf1);
dc6f92b8
JB
10331}
10332
0da1ab50 10333
e99db5a1
RS
10334/* This is the first-level handler for X protocol errors.
10335 It calls x_error_quitter or x_error_catcher. */
7a13e894 10336
8922af5f 10337static int
e99db5a1 10338x_error_handler (display, error)
8922af5f 10339 Display *display;
e99db5a1 10340 XErrorEvent *error;
8922af5f 10341{
e99db5a1
RS
10342 if (! NILP (x_error_message_string))
10343 x_error_catcher (display, error);
10344 else
10345 x_error_quitter (display, error);
06a2c219 10346 return 0;
f9e24cb9 10347}
c118dd06 10348
e99db5a1
RS
10349/* This is the handler for X IO errors, always.
10350 It kills all frames on the display that we lost touch with.
10351 If that was the only one, it prints an error message and kills Emacs. */
7a13e894 10352
c118dd06 10353static int
e99db5a1 10354x_io_error_quitter (display)
c118dd06 10355 Display *display;
c118dd06 10356{
e99db5a1 10357 char buf[256];
dc6f92b8 10358
e99db5a1
RS
10359 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
10360 x_connection_closed (display, buf);
06a2c219 10361 return 0;
dc6f92b8 10362}
dc6f92b8 10363\f
f451eb13
JB
10364/* Changing the font of the frame. */
10365
76bcdf39
RS
10366/* Give frame F the font named FONTNAME as its default font, and
10367 return the full name of that font. FONTNAME may be a wildcard
10368 pattern; in that case, we choose some font that fits the pattern.
10369 The return value shows which font we chose. */
10370
b5cf7a0e 10371Lisp_Object
f676886a
JB
10372x_new_font (f, fontname)
10373 struct frame *f;
dc6f92b8
JB
10374 register char *fontname;
10375{
dc43ef94 10376 struct font_info *fontp
ee569018 10377 = FS_LOAD_FONT (f, 0, fontname, -1);
dc6f92b8 10378
dc43ef94
KH
10379 if (!fontp)
10380 return Qnil;
2224a5fc 10381
dc43ef94 10382 f->output_data.x->font = (XFontStruct *) (fontp->font);
b4192550 10383 f->output_data.x->baseline_offset = fontp->baseline_offset;
dc43ef94 10384 f->output_data.x->fontset = -1;
976b73d7 10385
5958f265 10386 compute_fringe_widths (f, 1);
976b73d7 10387
b2cad826
KH
10388 /* Compute the scroll bar width in character columns. */
10389 if (f->scroll_bar_pixel_width > 0)
10390 {
7556890b 10391 int wid = FONT_WIDTH (f->output_data.x->font);
b2cad826
KH
10392 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
10393 }
10394 else
4e61bddf
RS
10395 {
10396 int wid = FONT_WIDTH (f->output_data.x->font);
10397 f->scroll_bar_cols = (14 + wid - 1) / wid;
10398 }
b2cad826 10399
f676886a 10400 /* Now make the frame display the given font. */
c118dd06 10401 if (FRAME_X_WINDOW (f) != 0)
dc6f92b8 10402 {
7556890b
RS
10403 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
10404 f->output_data.x->font->fid);
10405 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
10406 f->output_data.x->font->fid);
10407 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
10408 f->output_data.x->font->fid);
f676886a 10409
a27f9f86 10410 frame_update_line_height (f);
3497f73e
GM
10411
10412 /* Don't change the size of a tip frame; there's no point in
10413 doing it because it's done in Fx_show_tip, and it leads to
10414 problems because the tip frame has no widget. */
10415 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
10416 x_set_window_size (f, 0, f->width, f->height);
dc6f92b8 10417 }
a27f9f86
RS
10418 else
10419 /* If we are setting a new frame's font for the first time,
10420 there are no faces yet, so this font's height is the line height. */
7556890b 10421 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
dc6f92b8 10422
dc43ef94
KH
10423 return build_string (fontp->full_name);
10424}
10425
10426/* Give frame F the fontset named FONTSETNAME as its default font, and
10427 return the full name of that fontset. FONTSETNAME may be a wildcard
b5210ea7
KH
10428 pattern; in that case, we choose some fontset that fits the pattern.
10429 The return value shows which fontset we chose. */
b5cf7a0e 10430
dc43ef94
KH
10431Lisp_Object
10432x_new_fontset (f, fontsetname)
10433 struct frame *f;
10434 char *fontsetname;
10435{
ee569018 10436 int fontset = fs_query_fontset (build_string (fontsetname), 0);
dc43ef94 10437 Lisp_Object result;
b5cf7a0e 10438
dc43ef94
KH
10439 if (fontset < 0)
10440 return Qnil;
b5cf7a0e 10441
2da424f1
KH
10442 if (f->output_data.x->fontset == fontset)
10443 /* This fontset is already set in frame F. There's nothing more
10444 to do. */
ee569018 10445 return fontset_name (fontset);
dc43ef94 10446
d5db4077 10447 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
dc43ef94
KH
10448
10449 if (!STRINGP (result))
10450 /* Can't load ASCII font. */
10451 return Qnil;
10452
10453 /* Since x_new_font doesn't update any fontset information, do it now. */
10454 f->output_data.x->fontset = fontset;
dc43ef94 10455
f5d11644
GM
10456#ifdef HAVE_X_I18N
10457 if (FRAME_XIC (f)
10458 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
d5db4077 10459 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
f5d11644 10460#endif
7d0393cf 10461
dc43ef94 10462 return build_string (fontsetname);
dc6f92b8 10463}
f5d11644
GM
10464
10465\f
10466/***********************************************************************
10467 X Input Methods
10468 ***********************************************************************/
10469
10470#ifdef HAVE_X_I18N
10471
10472#ifdef HAVE_X11R6
10473
10474/* XIM destroy callback function, which is called whenever the
10475 connection to input method XIM dies. CLIENT_DATA contains a
10476 pointer to the x_display_info structure corresponding to XIM. */
10477
10478static void
10479xim_destroy_callback (xim, client_data, call_data)
10480 XIM xim;
10481 XPointer client_data;
10482 XPointer call_data;
10483{
10484 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
10485 Lisp_Object frame, tail;
7d0393cf 10486
f5d11644 10487 BLOCK_INPUT;
7d0393cf 10488
f5d11644
GM
10489 /* No need to call XDestroyIC.. */
10490 FOR_EACH_FRAME (tail, frame)
10491 {
10492 struct frame *f = XFRAME (frame);
10493 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
10494 {
10495 FRAME_XIC (f) = NULL;
10496 if (FRAME_XIC_FONTSET (f))
10497 {
10498 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
10499 FRAME_XIC_FONTSET (f) = NULL;
10500 }
10501 }
10502 }
7d0393cf 10503
f5d11644
GM
10504 /* No need to call XCloseIM. */
10505 dpyinfo->xim = NULL;
10506 XFree (dpyinfo->xim_styles);
10507 UNBLOCK_INPUT;
10508}
10509
10510#endif /* HAVE_X11R6 */
10511
dbd4b028
DL
10512#ifdef HAVE_X11R6
10513/* This isn't prototyped in OSF 5.0 or 5.1a. */
10514extern char *XSetIMValues P_ ((XIM, ...));
10515#endif
10516
f5d11644
GM
10517/* Open the connection to the XIM server on display DPYINFO.
10518 RESOURCE_NAME is the resource name Emacs uses. */
10519
10520static void
10521xim_open_dpy (dpyinfo, resource_name)
10522 struct x_display_info *dpyinfo;
10523 char *resource_name;
10524{
287f7dd6 10525#ifdef USE_XIM
f5d11644
GM
10526 XIM xim;
10527
10528 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
10529 dpyinfo->xim = xim;
10530
10531 if (xim)
10532 {
f5d11644
GM
10533#ifdef HAVE_X11R6
10534 XIMCallback destroy;
10535#endif
7d0393cf 10536
f5d11644
GM
10537 /* Get supported styles and XIM values. */
10538 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
7d0393cf 10539
f5d11644
GM
10540#ifdef HAVE_X11R6
10541 destroy.callback = xim_destroy_callback;
10542 destroy.client_data = (XPointer)dpyinfo;
10543 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
10544#endif
10545 }
7d0393cf 10546
287f7dd6
GM
10547#else /* not USE_XIM */
10548 dpyinfo->xim = NULL;
10549#endif /* not USE_XIM */
f5d11644
GM
10550}
10551
10552
b9de836c 10553#ifdef HAVE_X11R6_XIM
f5d11644
GM
10554
10555struct xim_inst_t
10556{
10557 struct x_display_info *dpyinfo;
10558 char *resource_name;
10559};
10560
10561/* XIM instantiate callback function, which is called whenever an XIM
1fcfb866 10562 server is available. DISPLAY is the display of the XIM.
f5d11644
GM
10563 CLIENT_DATA contains a pointer to an xim_inst_t structure created
10564 when the callback was registered. */
10565
10566static void
10567xim_instantiate_callback (display, client_data, call_data)
10568 Display *display;
10569 XPointer client_data;
10570 XPointer call_data;
10571{
10572 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
10573 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
10574
10575 /* We don't support multiple XIM connections. */
10576 if (dpyinfo->xim)
10577 return;
7d0393cf 10578
f5d11644
GM
10579 xim_open_dpy (dpyinfo, xim_inst->resource_name);
10580
10581 /* Create XIC for the existing frames on the same display, as long
10582 as they have no XIC. */
10583 if (dpyinfo->xim && dpyinfo->reference_count > 0)
10584 {
10585 Lisp_Object tail, frame;
10586
10587 BLOCK_INPUT;
10588 FOR_EACH_FRAME (tail, frame)
10589 {
10590 struct frame *f = XFRAME (frame);
7d0393cf 10591
f5d11644
GM
10592 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
10593 if (FRAME_XIC (f) == NULL)
10594 {
10595 create_frame_xic (f);
10596 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
10597 xic_set_statusarea (f);
10598 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
10599 {
10600 struct window *w = XWINDOW (f->selected_window);
10601 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
10602 }
10603 }
10604 }
7d0393cf 10605
f5d11644
GM
10606 UNBLOCK_INPUT;
10607 }
10608}
10609
b9de836c 10610#endif /* HAVE_X11R6_XIM */
f5d11644
GM
10611
10612
10613/* Open a connection to the XIM server on display DPYINFO.
10614 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
10615 connection only at the first time. On X11R6, open the connection
10616 in the XIM instantiate callback function. */
10617
10618static void
10619xim_initialize (dpyinfo, resource_name)
10620 struct x_display_info *dpyinfo;
10621 char *resource_name;
10622{
287f7dd6 10623#ifdef USE_XIM
b9de836c 10624#ifdef HAVE_X11R6_XIM
f5d11644
GM
10625 struct xim_inst_t *xim_inst;
10626 int len;
7d0393cf 10627
f5d11644
GM
10628 dpyinfo->xim = NULL;
10629 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
10630 xim_inst->dpyinfo = dpyinfo;
10631 len = strlen (resource_name);
10632 xim_inst->resource_name = (char *) xmalloc (len + 1);
10633 bcopy (resource_name, xim_inst->resource_name, len + 1);
10634 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
10635 resource_name, EMACS_CLASS,
10636 xim_instantiate_callback,
2ebb2f8b
DL
10637 /* Fixme: This is XPointer in
10638 XFree86 but (XPointer *) on
10639 Tru64, at least. */
10640 (XPointer) xim_inst);
b9de836c 10641#else /* not HAVE_X11R6_XIM */
f5d11644
GM
10642 dpyinfo->xim = NULL;
10643 xim_open_dpy (dpyinfo, resource_name);
b9de836c 10644#endif /* not HAVE_X11R6_XIM */
7d0393cf 10645
287f7dd6
GM
10646#else /* not USE_XIM */
10647 dpyinfo->xim = NULL;
10648#endif /* not USE_XIM */
f5d11644
GM
10649}
10650
10651
10652/* Close the connection to the XIM server on display DPYINFO. */
10653
10654static void
10655xim_close_dpy (dpyinfo)
10656 struct x_display_info *dpyinfo;
10657{
287f7dd6 10658#ifdef USE_XIM
b9de836c 10659#ifdef HAVE_X11R6_XIM
8a4f36cc
GM
10660 if (dpyinfo->display)
10661 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
10662 NULL, EMACS_CLASS,
10663 xim_instantiate_callback, NULL);
b9de836c 10664#endif /* not HAVE_X11R6_XIM */
8a4f36cc
GM
10665 if (dpyinfo->display)
10666 XCloseIM (dpyinfo->xim);
f5d11644
GM
10667 dpyinfo->xim = NULL;
10668 XFree (dpyinfo->xim_styles);
287f7dd6 10669#endif /* USE_XIM */
f5d11644
GM
10670}
10671
b9de836c 10672#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
10673
10674
dc6f92b8 10675\f
2e365682
RS
10676/* Calculate the absolute position in frame F
10677 from its current recorded position values and gravity. */
10678
dfcf069d 10679void
43bca5d5 10680x_calc_absolute_position (f)
f676886a 10681 struct frame *f;
dc6f92b8 10682{
06a2c219 10683 Window child;
6dba1858 10684 int win_x = 0, win_y = 0;
7556890b 10685 int flags = f->output_data.x->size_hint_flags;
c81412a0
KH
10686 int this_window;
10687
9829ddba
RS
10688 /* We have nothing to do if the current position
10689 is already for the top-left corner. */
10690 if (! ((flags & XNegative) || (flags & YNegative)))
10691 return;
10692
488dd4c4 10693 this_window = FRAME_OUTER_WINDOW (f);
6dba1858
RS
10694
10695 /* Find the position of the outside upper-left corner of
9829ddba
RS
10696 the inner window, with respect to the outer window.
10697 But do this only if we will need the results. */
7556890b 10698 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6dba1858 10699 {
9829ddba
RS
10700 int count;
10701
6dba1858 10702 BLOCK_INPUT;
9829ddba
RS
10703 count = x_catch_errors (FRAME_X_DISPLAY (f));
10704 while (1)
10705 {
10706 x_clear_errors (FRAME_X_DISPLAY (f));
10707 XTranslateCoordinates (FRAME_X_DISPLAY (f),
10708
10709 /* From-window, to-window. */
10710 this_window,
10711 f->output_data.x->parent_desc,
10712
10713 /* From-position, to-position. */
10714 0, 0, &win_x, &win_y,
10715
10716 /* Child of win. */
10717 &child);
10718 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
10719 {
10720 Window newroot, newparent = 0xdeadbeef;
10721 Window *newchildren;
2ebb2f8b 10722 unsigned int nchildren;
9829ddba
RS
10723
10724 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
10725 &newparent, &newchildren, &nchildren))
10726 break;
58769bee 10727
7c3c78a3 10728 XFree ((char *) newchildren);
6dba1858 10729
9829ddba
RS
10730 f->output_data.x->parent_desc = newparent;
10731 }
10732 else
10733 break;
10734 }
6dba1858 10735
9829ddba 10736 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
6dba1858
RS
10737 UNBLOCK_INPUT;
10738 }
10739
10740 /* Treat negative positions as relative to the leftmost bottommost
10741 position that fits on the screen. */
20f55f9a 10742 if (flags & XNegative)
7556890b 10743 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
2e365682
RS
10744 - 2 * f->output_data.x->border_width - win_x
10745 - PIXEL_WIDTH (f)
10746 + f->output_data.x->left_pos);
dc6f92b8 10747
7708ced0
GM
10748 {
10749 int height = PIXEL_HEIGHT (f);
06a2c219 10750
7708ced0
GM
10751#if defined USE_X_TOOLKIT && defined USE_MOTIF
10752 /* Something is fishy here. When using Motif, starting Emacs with
10753 `-g -0-0', the frame appears too low by a few pixels.
10754
10755 This seems to be so because initially, while Emacs is starting,
10756 the column widget's height and the frame's pixel height are
10757 different. The column widget's height is the right one. In
10758 later invocations, when Emacs is up, the frame's pixel height
10759 is right, though.
10760
10761 It's not obvious where the initial small difference comes from.
10762 2000-12-01, gerd. */
7d0393cf 10763
7708ced0 10764 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
06a2c219 10765#endif
2e365682 10766
7708ced0
GM
10767 if (flags & YNegative)
10768 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
10769 - 2 * f->output_data.x->border_width
10770 - win_y
10771 - height
10772 + f->output_data.x->top_pos);
10773 }
7d0393cf 10774
3a35ab44
RS
10775 /* The left_pos and top_pos
10776 are now relative to the top and left screen edges,
10777 so the flags should correspond. */
7556890b 10778 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
dc6f92b8
JB
10779}
10780
3a35ab44
RS
10781/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
10782 to really change the position, and 0 when calling from
10783 x_make_frame_visible (in that case, XOFF and YOFF are the current
aa3ff7c9
KH
10784 position values). It is -1 when calling from x_set_frame_parameters,
10785 which means, do adjust for borders but don't change the gravity. */
3a35ab44 10786
dfcf069d 10787void
dc05a16b 10788x_set_offset (f, xoff, yoff, change_gravity)
f676886a 10789 struct frame *f;
dc6f92b8 10790 register int xoff, yoff;
dc05a16b 10791 int change_gravity;
dc6f92b8 10792{
4a4cbdd5
KH
10793 int modified_top, modified_left;
10794
aa3ff7c9 10795 if (change_gravity > 0)
3a35ab44 10796 {
7556890b
RS
10797 f->output_data.x->top_pos = yoff;
10798 f->output_data.x->left_pos = xoff;
10799 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
3a35ab44 10800 if (xoff < 0)
7556890b 10801 f->output_data.x->size_hint_flags |= XNegative;
3a35ab44 10802 if (yoff < 0)
7556890b
RS
10803 f->output_data.x->size_hint_flags |= YNegative;
10804 f->output_data.x->win_gravity = NorthWestGravity;
3a35ab44 10805 }
43bca5d5 10806 x_calc_absolute_position (f);
dc6f92b8
JB
10807
10808 BLOCK_INPUT;
c32cdd9a 10809 x_wm_set_size_hint (f, (long) 0, 0);
3a35ab44 10810
7556890b
RS
10811 modified_left = f->output_data.x->left_pos;
10812 modified_top = f->output_data.x->top_pos;
e73ec6fa
RS
10813#if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
10814 this seems to be unnecessary and incorrect. rms, 4/17/97. */
10815 /* It is a mystery why we need to add the border_width here
10816 when the frame is already visible, but experiment says we do. */
aa3ff7c9 10817 if (change_gravity != 0)
4a4cbdd5 10818 {
7556890b
RS
10819 modified_left += f->output_data.x->border_width;
10820 modified_top += f->output_data.x->border_width;
4a4cbdd5 10821 }
e73ec6fa 10822#endif
4a4cbdd5 10823
488dd4c4
JD
10824 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10825 modified_left, modified_top);
dc6f92b8
JB
10826 UNBLOCK_INPUT;
10827}
10828
d0fa3e56
EZ
10829/* Check if we need to resize the frame due to a fullscreen request.
10830 If so needed, resize the frame. */
10831static void
10832x_check_fullscreen (f)
10833 struct frame *f;
10834{
10835 if (f->output_data.x->want_fullscreen & FULLSCREEN_BOTH)
10836 {
10837 int width, height, ign;
7d0393cf 10838
d0fa3e56
EZ
10839 x_real_positions (f, &f->output_data.x->left_pos,
10840 &f->output_data.x->top_pos);
10841
10842 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
7d0393cf 10843
d0fa3e56
EZ
10844 /* We do not need to move the window, it shall be taken care of
10845 when setting WM manager hints.
10846 If the frame is visible already, the position is checked by
10847 x_check_fullscreen_move. */
10848 if (f->width != width || f->height != height)
10849 {
10850 change_frame_size (f, height, width, 0, 1, 0);
10851 SET_FRAME_GARBAGED (f);
10852 cancel_mouse_face (f);
10853
10854 /* Wait for the change of frame size to occur */
10855 f->output_data.x->want_fullscreen |= FULLSCREEN_WAIT;
7d0393cf 10856
d0fa3e56
EZ
10857 }
10858 }
10859}
10860
10861/* If frame parameters are set after the frame is mapped, we need to move
10862 the window. This is done in xfns.c.
10863 Some window managers moves the window to the right position, some
10864 moves the outer window manager window to the specified position.
10865 Here we check that we are in the right spot. If not, make a second
10866 move, assuming we are dealing with the second kind of window manager. */
10867static void
10868x_check_fullscreen_move (f)
10869 struct frame *f;
10870{
10871 if (f->output_data.x->want_fullscreen & FULLSCREEN_MOVE_WAIT)
10872 {
10873 int expect_top = f->output_data.x->top_pos;
10874 int expect_left = f->output_data.x->left_pos;
10875
10876 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
10877 expect_top = 0;
10878 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
10879 expect_left = 0;
7d0393cf 10880
d0fa3e56
EZ
10881 if (expect_top != f->output_data.x->top_pos
10882 || expect_left != f->output_data.x->left_pos)
10883 x_set_offset (f, expect_left, expect_top, 1);
10884
10885 /* Just do this once */
10886 f->output_data.x->want_fullscreen &= ~FULLSCREEN_MOVE_WAIT;
10887 }
10888}
10889
10890
10891/* Calculate fullscreen size. Return in *TOP_POS and *LEFT_POS the
10892 wanted positions of the WM window (not emacs window).
10893 Return in *WIDTH and *HEIGHT the wanted width and height of Emacs
10894 window (FRAME_X_WINDOW).
10895 */
10896void
10897x_fullscreen_adjust (f, width, height, top_pos, left_pos)
10898 struct frame *f;
10899 int *width;
10900 int *height;
10901 int *top_pos;
10902 int *left_pos;
10903{
10904 int newwidth = f->width, newheight = f->height;
10905
10906 *top_pos = f->output_data.x->top_pos;
10907 *left_pos = f->output_data.x->left_pos;
7d0393cf 10908
d0fa3e56
EZ
10909 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
10910 {
10911 int ph;
7d0393cf 10912
d0fa3e56
EZ
10913 ph = FRAME_X_DISPLAY_INFO (f)->height;
10914 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
10915 ph = CHAR_TO_PIXEL_HEIGHT (f, newheight)
10916 - f->output_data.x->y_pixels_diff;
10917 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
10918 *top_pos = 0;
10919 }
10920
10921 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
10922 {
10923 int pw;
7d0393cf 10924
d0fa3e56
EZ
10925 pw = FRAME_X_DISPLAY_INFO (f)->width;
10926 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
10927 pw = CHAR_TO_PIXEL_WIDTH (f, newwidth)
10928 - f->output_data.x->x_pixels_diff;
10929 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
10930 *left_pos = 0;
10931 }
10932
10933 *width = newwidth;
10934 *height = newheight;
10935}
10936
dc6f92b8 10937
499b1844
GM
10938/* Change the size of frame F's X window to COLS/ROWS in the case F
10939 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
10940 top-left-corner window gravity for this size change and subsequent
10941 size changes. Otherwise we leave the window gravity unchanged. */
10942
10943static void
10944x_set_window_size_1 (f, change_gravity, cols, rows)
f676886a 10945 struct frame *f;
bc20ebbf 10946 int change_gravity;
b1c884c3 10947 int cols, rows;
dc6f92b8
JB
10948{
10949 int pixelwidth, pixelheight;
80fd1fe2 10950
b1c884c3 10951 check_frame_size (f, &rows, &cols);
7556890b 10952 f->output_data.x->vertical_scroll_bar_extra
b2cad826
KH
10953 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
10954 ? 0
10955 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
480407eb 10956 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
7556890b 10957 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
976b73d7 10958
5958f265 10959 compute_fringe_widths (f, 0);
976b73d7 10960
f451eb13
JB
10961 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
10962 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
dc6f92b8 10963
7556890b 10964 f->output_data.x->win_gravity = NorthWestGravity;
c32cdd9a 10965 x_wm_set_size_hint (f, (long) 0, 0);
6ccf47d1 10966
334208b7
RS
10967 XSync (FRAME_X_DISPLAY (f), False);
10968 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10969 pixelwidth, pixelheight);
b1c884c3
JB
10970
10971 /* Now, strictly speaking, we can't be sure that this is accurate,
10972 but the window manager will get around to dealing with the size
10973 change request eventually, and we'll hear how it went when the
dbc4e1c1
JB
10974 ConfigureNotify event gets here.
10975
10976 We could just not bother storing any of this information here,
10977 and let the ConfigureNotify event set everything up, but that
fddd5ceb 10978 might be kind of confusing to the Lisp code, since size changes
dbc4e1c1 10979 wouldn't be reported in the frame parameters until some random
fddd5ceb
RS
10980 point in the future when the ConfigureNotify event arrives.
10981
10982 We pass 1 for DELAY since we can't run Lisp code inside of
10983 a BLOCK_INPUT. */
7d1e984f 10984 change_frame_size (f, rows, cols, 0, 1, 0);
b1c884c3
JB
10985 PIXEL_WIDTH (f) = pixelwidth;
10986 PIXEL_HEIGHT (f) = pixelheight;
10987
aee9a898
RS
10988 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
10989 receive in the ConfigureNotify event; if we get what we asked
10990 for, then the event won't cause the screen to become garbaged, so
10991 we have to make sure to do it here. */
10992 SET_FRAME_GARBAGED (f);
10993
10994 XFlush (FRAME_X_DISPLAY (f));
499b1844
GM
10995}
10996
10997
10998/* Call this to change the size of frame F's x-window.
10999 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
11000 for this size change and subsequent size changes.
11001 Otherwise we leave the window gravity unchanged. */
aee9a898 11002
499b1844
GM
11003void
11004x_set_window_size (f, change_gravity, cols, rows)
11005 struct frame *f;
11006 int change_gravity;
11007 int cols, rows;
11008{
11009 BLOCK_INPUT;
11010
488dd4c4
JD
11011#ifdef USE_GTK
11012 if (FRAME_GTK_WIDGET (f))
11013 xg_frame_set_char_size (f, cols, rows);
11014 else
11015 x_set_window_size_1 (f, change_gravity, cols, rows);
11016#elif USE_X_TOOLKIT
7d0393cf 11017
f1f4d345 11018 if (f->output_data.x->widget != NULL)
499b1844
GM
11019 {
11020 /* The x and y position of the widget is clobbered by the
11021 call to XtSetValues within EmacsFrameSetCharSize.
11022 This is a real kludge, but I don't understand Xt so I can't
11023 figure out a correct fix. Can anyone else tell me? -- rms. */
11024 int xpos = f->output_data.x->widget->core.x;
11025 int ypos = f->output_data.x->widget->core.y;
11026 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
11027 f->output_data.x->widget->core.x = xpos;
11028 f->output_data.x->widget->core.y = ypos;
11029 }
11030 else
11031 x_set_window_size_1 (f, change_gravity, cols, rows);
7d0393cf 11032
499b1844 11033#else /* not USE_X_TOOLKIT */
7d0393cf 11034
499b1844 11035 x_set_window_size_1 (f, change_gravity, cols, rows);
7d0393cf 11036
aee9a898
RS
11037#endif /* not USE_X_TOOLKIT */
11038
4d73d038 11039 /* If cursor was outside the new size, mark it as off. */
06a2c219 11040 mark_window_cursors_off (XWINDOW (f->root_window));
4d73d038 11041
aee9a898 11042 /* Clear out any recollection of where the mouse highlighting was,
7d0393cf 11043 since it might be in a place that's outside the new frame size.
aee9a898
RS
11044 Actually checking whether it is outside is a pain in the neck,
11045 so don't try--just let the highlighting be done afresh with new size. */
e687d06e 11046 cancel_mouse_face (f);
dbc4e1c1 11047
dc6f92b8
JB
11048 UNBLOCK_INPUT;
11049}
dc6f92b8 11050\f
d047c4eb 11051/* Mouse warping. */
dc6f92b8 11052
9b378208 11053void
f676886a
JB
11054x_set_mouse_position (f, x, y)
11055 struct frame *f;
dc6f92b8
JB
11056 int x, y;
11057{
11058 int pix_x, pix_y;
11059
7556890b
RS
11060 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
11061 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
f451eb13
JB
11062
11063 if (pix_x < 0) pix_x = 0;
11064 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
11065
11066 if (pix_y < 0) pix_y = 0;
11067 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
dc6f92b8
JB
11068
11069 BLOCK_INPUT;
dc6f92b8 11070
334208b7
RS
11071 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11072 0, 0, 0, 0, pix_x, pix_y);
dc6f92b8
JB
11073 UNBLOCK_INPUT;
11074}
11075
9b378208
RS
11076/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11077
11078void
11079x_set_mouse_pixel_position (f, pix_x, pix_y)
11080 struct frame *f;
11081 int pix_x, pix_y;
11082{
11083 BLOCK_INPUT;
11084
334208b7
RS
11085 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11086 0, 0, 0, 0, pix_x, pix_y);
9b378208
RS
11087 UNBLOCK_INPUT;
11088}
d047c4eb
KH
11089\f
11090/* focus shifting, raising and lowering. */
9b378208 11091
dfcf069d 11092void
f676886a
JB
11093x_focus_on_frame (f)
11094 struct frame *f;
dc6f92b8 11095{
1fb20991 11096#if 0 /* This proves to be unpleasant. */
f676886a 11097 x_raise_frame (f);
1fb20991 11098#endif
6d4238f3
JB
11099#if 0
11100 /* I don't think that the ICCCM allows programs to do things like this
11101 without the interaction of the window manager. Whatever you end up
f676886a 11102 doing with this code, do it to x_unfocus_frame too. */
334208b7 11103 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dc6f92b8 11104 RevertToPointerRoot, CurrentTime);
c118dd06 11105#endif /* ! 0 */
dc6f92b8
JB
11106}
11107
dfcf069d 11108void
f676886a
JB
11109x_unfocus_frame (f)
11110 struct frame *f;
dc6f92b8 11111{
6d4238f3 11112#if 0
f676886a 11113 /* Look at the remarks in x_focus_on_frame. */
0f941935 11114 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
334208b7 11115 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
dc6f92b8 11116 RevertToPointerRoot, CurrentTime);
c118dd06 11117#endif /* ! 0 */
dc6f92b8
JB
11118}
11119
f676886a 11120/* Raise frame F. */
dc6f92b8 11121
dfcf069d 11122void
f676886a
JB
11123x_raise_frame (f)
11124 struct frame *f;
dc6f92b8 11125{
3a88c238 11126 if (f->async_visible)
dc6f92b8
JB
11127 {
11128 BLOCK_INPUT;
2436a4e4 11129 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
334208b7 11130 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
11131 UNBLOCK_INPUT;
11132 }
11133}
11134
f676886a 11135/* Lower frame F. */
dc6f92b8 11136
dfcf069d 11137void
f676886a
JB
11138x_lower_frame (f)
11139 struct frame *f;
dc6f92b8 11140{
3a88c238 11141 if (f->async_visible)
dc6f92b8
JB
11142 {
11143 BLOCK_INPUT;
2436a4e4 11144 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
334208b7 11145 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
11146 UNBLOCK_INPUT;
11147 }
11148}
11149
dbc4e1c1 11150static void
6b0442dc 11151XTframe_raise_lower (f, raise_flag)
dbc4e1c1 11152 FRAME_PTR f;
6b0442dc 11153 int raise_flag;
dbc4e1c1 11154{
6b0442dc 11155 if (raise_flag)
dbc4e1c1
JB
11156 x_raise_frame (f);
11157 else
11158 x_lower_frame (f);
11159}
d047c4eb
KH
11160\f
11161/* Change of visibility. */
dc6f92b8 11162
9382638d
KH
11163/* This tries to wait until the frame is really visible.
11164 However, if the window manager asks the user where to position
11165 the frame, this will return before the user finishes doing that.
11166 The frame will not actually be visible at that time,
11167 but it will become visible later when the window manager
11168 finishes with it. */
11169
dfcf069d 11170void
f676886a
JB
11171x_make_frame_visible (f)
11172 struct frame *f;
dc6f92b8 11173{
990ba854 11174 Lisp_Object type;
1aa6072f 11175 int original_top, original_left;
31be9251
GM
11176 int retry_count = 2;
11177
11178 retry:
dc6f92b8 11179
dc6f92b8 11180 BLOCK_INPUT;
dc6f92b8 11181
990ba854
RS
11182 type = x_icon_type (f);
11183 if (!NILP (type))
11184 x_bitmap_icon (f, type);
bdcd49ba 11185
f676886a 11186 if (! FRAME_VISIBLE_P (f))
90e65f07 11187 {
1aa6072f
RS
11188 /* We test FRAME_GARBAGED_P here to make sure we don't
11189 call x_set_offset a second time
11190 if we get to x_make_frame_visible a second time
11191 before the window gets really visible. */
11192 if (! FRAME_ICONIFIED_P (f)
11193 && ! f->output_data.x->asked_for_visible)
11194 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
11195
11196 f->output_data.x->asked_for_visible = 1;
11197
90e65f07 11198 if (! EQ (Vx_no_window_manager, Qt))
f676886a 11199 x_wm_set_window_state (f, NormalState);
3afe33e7 11200#ifdef USE_X_TOOLKIT
d7a38a2e 11201 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 11202 XtMapWidget (f->output_data.x->widget);
3afe33e7 11203#else /* not USE_X_TOOLKIT */
488dd4c4
JD
11204#ifdef USE_GTK
11205 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
7b76ca1c 11206 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
488dd4c4 11207#else
7f9c7f94 11208 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
488dd4c4 11209#endif /* not USE_GTK */
3afe33e7 11210#endif /* not USE_X_TOOLKIT */
0134a210
RS
11211#if 0 /* This seems to bring back scroll bars in the wrong places
11212 if the window configuration has changed. They seem
11213 to come back ok without this. */
ab648270 11214 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
334208b7 11215 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
0134a210 11216#endif
90e65f07 11217 }
dc6f92b8 11218
334208b7 11219 XFlush (FRAME_X_DISPLAY (f));
90e65f07 11220
0dacf791
RS
11221 /* Synchronize to ensure Emacs knows the frame is visible
11222 before we do anything else. We do this loop with input not blocked
11223 so that incoming events are handled. */
11224 {
11225 Lisp_Object frame;
12ce2351 11226 int count;
28c01ffe
RS
11227 /* This must be before UNBLOCK_INPUT
11228 since events that arrive in response to the actions above
11229 will set it when they are handled. */
11230 int previously_visible = f->output_data.x->has_been_visible;
1aa6072f
RS
11231
11232 original_left = f->output_data.x->left_pos;
11233 original_top = f->output_data.x->top_pos;
c0a04927
RS
11234
11235 /* This must come after we set COUNT. */
11236 UNBLOCK_INPUT;
11237
2745e6c4 11238 /* We unblock here so that arriving X events are processed. */
1aa6072f 11239
dcb07ae9
RS
11240 /* Now move the window back to where it was "supposed to be".
11241 But don't do it if the gravity is negative.
11242 When the gravity is negative, this uses a position
28c01ffe
RS
11243 that is 3 pixels too low. Perhaps that's really the border width.
11244
11245 Don't do this if the window has never been visible before,
11246 because the window manager may choose the position
11247 and we don't want to override it. */
1aa6072f 11248
4d3f5d9a 11249 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
06c488fd 11250 && f->output_data.x->win_gravity == NorthWestGravity
28c01ffe 11251 && previously_visible)
1aa6072f 11252 {
2745e6c4
RS
11253 Drawable rootw;
11254 int x, y;
11255 unsigned int width, height, border, depth;
7d0393cf 11256
1aa6072f 11257 BLOCK_INPUT;
9829ddba 11258
06a2c219
GM
11259 /* On some window managers (such as FVWM) moving an existing
11260 window, even to the same place, causes the window manager
11261 to introduce an offset. This can cause the window to move
11262 to an unexpected location. Check the geometry (a little
11263 slow here) and then verify that the window is in the right
11264 place. If the window is not in the right place, move it
11265 there, and take the potential window manager hit. */
2745e6c4
RS
11266 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11267 &rootw, &x, &y, &width, &height, &border, &depth);
11268
11269 if (original_left != x || original_top != y)
11270 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11271 original_left, original_top);
11272
1aa6072f
RS
11273 UNBLOCK_INPUT;
11274 }
9829ddba 11275
e0c1aef2 11276 XSETFRAME (frame, f);
c0a04927 11277
12ce2351
GM
11278 /* Wait until the frame is visible. Process X events until a
11279 MapNotify event has been seen, or until we think we won't get a
11280 MapNotify at all.. */
11281 for (count = input_signal_count + 10;
11282 input_signal_count < count && !FRAME_VISIBLE_P (f);)
2a6cf806 11283 {
12ce2351 11284 /* Force processing of queued events. */
334208b7 11285 x_sync (f);
12ce2351
GM
11286
11287 /* Machines that do polling rather than SIGIO have been
11288 observed to go into a busy-wait here. So we'll fake an
11289 alarm signal to let the handler know that there's something
11290 to be read. We used to raise a real alarm, but it seems
11291 that the handler isn't always enabled here. This is
11292 probably a bug. */
8b2f8d4e 11293 if (input_polling_used ())
3b2fa4e6 11294 {
12ce2351
GM
11295 /* It could be confusing if a real alarm arrives while
11296 processing the fake one. Turn it off and let the
11297 handler reset it. */
3e71d8f2 11298 extern void poll_for_input_1 P_ ((void));
bffcfca9
GM
11299 int old_poll_suppress_count = poll_suppress_count;
11300 poll_suppress_count = 1;
11301 poll_for_input_1 ();
11302 poll_suppress_count = old_poll_suppress_count;
3b2fa4e6 11303 }
12ce2351
GM
11304
11305 /* See if a MapNotify event has been processed. */
11306 FRAME_SAMPLE_VISIBILITY (f);
2a6cf806 11307 }
31be9251
GM
11308
11309 /* 2000-09-28: In
11310
11311 (let ((f (selected-frame)))
11312 (iconify-frame f)
11313 (raise-frame f))
11314
11315 the frame is not raised with various window managers on
11316 FreeBSD, Linux and Solaris. It turns out that, for some
11317 unknown reason, the call to XtMapWidget is completely ignored.
11318 Mapping the widget a second time works. */
7d0393cf 11319
31be9251
GM
11320 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
11321 goto retry;
0dacf791 11322 }
dc6f92b8
JB
11323}
11324
06a2c219 11325/* Change from mapped state to withdrawn state. */
dc6f92b8 11326
d047c4eb
KH
11327/* Make the frame visible (mapped and not iconified). */
11328
dfcf069d 11329void
f676886a
JB
11330x_make_frame_invisible (f)
11331 struct frame *f;
dc6f92b8 11332{
546e6d5b
RS
11333 Window window;
11334
546e6d5b 11335 /* Use the frame's outermost window, not the one we normally draw on. */
2436a4e4 11336 window = FRAME_OUTER_WINDOW (f);
dc6f92b8 11337
9319ae23 11338 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
11339 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11340 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 11341
5627c40e 11342#if 0/* This might add unreliability; I don't trust it -- rms. */
9319ae23 11343 if (! f->async_visible && ! f->async_iconified)
dc6f92b8 11344 return;
5627c40e 11345#endif
dc6f92b8
JB
11346
11347 BLOCK_INPUT;
c118dd06 11348
af31d76f
RS
11349 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
11350 that the current position of the window is user-specified, rather than
11351 program-specified, so that when the window is mapped again, it will be
11352 placed at the same location, without forcing the user to position it
11353 by hand again (they have already done that once for this window.) */
c32cdd9a 11354 x_wm_set_size_hint (f, (long) 0, 1);
af31d76f 11355
488dd4c4
JD
11356#ifdef USE_GTK
11357 if (FRAME_GTK_OUTER_WIDGET (f))
11358 {
11359 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
11360 goto out;
11361 }
11362#endif
11363
c118dd06
JB
11364#ifdef HAVE_X11R4
11365
334208b7
RS
11366 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
11367 DefaultScreen (FRAME_X_DISPLAY (f))))
c118dd06
JB
11368 {
11369 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11370 error ("Can't notify window manager of window withdrawal");
c118dd06 11371 }
c118dd06 11372#else /* ! defined (HAVE_X11R4) */
16bd92ea 11373
c118dd06 11374 /* Tell the window manager what we're going to do. */
dc6f92b8
JB
11375 if (! EQ (Vx_no_window_manager, Qt))
11376 {
16bd92ea 11377 XEvent unmap;
dc6f92b8 11378
16bd92ea 11379 unmap.xunmap.type = UnmapNotify;
546e6d5b 11380 unmap.xunmap.window = window;
334208b7 11381 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
16bd92ea 11382 unmap.xunmap.from_configure = False;
334208b7
RS
11383 if (! XSendEvent (FRAME_X_DISPLAY (f),
11384 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea 11385 False,
06a2c219 11386 SubstructureRedirectMaskSubstructureNotifyMask,
16bd92ea
JB
11387 &unmap))
11388 {
11389 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11390 error ("Can't notify window manager of withdrawal");
16bd92ea 11391 }
dc6f92b8
JB
11392 }
11393
16bd92ea 11394 /* Unmap the window ourselves. Cheeky! */
334208b7 11395 XUnmapWindow (FRAME_X_DISPLAY (f), window);
c118dd06 11396#endif /* ! defined (HAVE_X11R4) */
dc6f92b8 11397
488dd4c4 11398 out:
5627c40e
RS
11399 /* We can't distinguish this from iconification
11400 just by the event that we get from the server.
11401 So we can't win using the usual strategy of letting
11402 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
11403 and synchronize with the server to make sure we agree. */
11404 f->visible = 0;
11405 FRAME_ICONIFIED_P (f) = 0;
11406 f->async_visible = 0;
11407 f->async_iconified = 0;
11408
334208b7 11409 x_sync (f);
5627c40e 11410
dc6f92b8
JB
11411 UNBLOCK_INPUT;
11412}
11413
06a2c219 11414/* Change window state from mapped to iconified. */
dc6f92b8 11415
dfcf069d 11416void
f676886a
JB
11417x_iconify_frame (f)
11418 struct frame *f;
dc6f92b8 11419{
3afe33e7 11420 int result;
990ba854 11421 Lisp_Object type;
dc6f92b8 11422
9319ae23 11423 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
11424 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11425 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 11426
3a88c238 11427 if (f->async_iconified)
dc6f92b8
JB
11428 return;
11429
3afe33e7 11430 BLOCK_INPUT;
546e6d5b 11431
9af3143a
RS
11432 FRAME_SAMPLE_VISIBILITY (f);
11433
990ba854
RS
11434 type = x_icon_type (f);
11435 if (!NILP (type))
11436 x_bitmap_icon (f, type);
bdcd49ba 11437
488dd4c4
JD
11438#ifdef USE_GTK
11439 if (FRAME_GTK_OUTER_WIDGET (f))
11440 {
11441 if (! FRAME_VISIBLE_P (f))
11442 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
11443
11444 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
11445 f->iconified = 1;
11446 f->visible = 1;
11447 f->async_iconified = 1;
11448 f->async_visible = 0;
11449 UNBLOCK_INPUT;
11450 return;
11451 }
11452#endif
11453
bdcd49ba
RS
11454#ifdef USE_X_TOOLKIT
11455
546e6d5b
RS
11456 if (! FRAME_VISIBLE_P (f))
11457 {
11458 if (! EQ (Vx_no_window_manager, Qt))
11459 x_wm_set_window_state (f, IconicState);
11460 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 11461 XtMapWidget (f->output_data.x->widget);
9cf30a30
RS
11462 /* The server won't give us any event to indicate
11463 that an invisible frame was changed to an icon,
11464 so we have to record it here. */
11465 f->iconified = 1;
1e6bc770 11466 f->visible = 1;
9cf30a30 11467 f->async_iconified = 1;
1e6bc770 11468 f->async_visible = 0;
546e6d5b
RS
11469 UNBLOCK_INPUT;
11470 return;
11471 }
11472
334208b7 11473 result = XIconifyWindow (FRAME_X_DISPLAY (f),
7556890b 11474 XtWindow (f->output_data.x->widget),
334208b7 11475 DefaultScreen (FRAME_X_DISPLAY (f)));
3afe33e7
RS
11476 UNBLOCK_INPUT;
11477
11478 if (!result)
546e6d5b 11479 error ("Can't notify window manager of iconification");
3afe33e7
RS
11480
11481 f->async_iconified = 1;
1e6bc770
RS
11482 f->async_visible = 0;
11483
8c002a25
KH
11484
11485 BLOCK_INPUT;
334208b7 11486 XFlush (FRAME_X_DISPLAY (f));
8c002a25 11487 UNBLOCK_INPUT;
3afe33e7
RS
11488#else /* not USE_X_TOOLKIT */
11489
fd13dbb2
RS
11490 /* Make sure the X server knows where the window should be positioned,
11491 in case the user deiconifies with the window manager. */
11492 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
7556890b 11493 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
fd13dbb2 11494
16bd92ea
JB
11495 /* Since we don't know which revision of X we're running, we'll use both
11496 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
11497
11498 /* X11R4: send a ClientMessage to the window manager using the
11499 WM_CHANGE_STATE type. */
11500 {
11501 XEvent message;
58769bee 11502
c118dd06 11503 message.xclient.window = FRAME_X_WINDOW (f);
16bd92ea 11504 message.xclient.type = ClientMessage;
334208b7 11505 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
16bd92ea
JB
11506 message.xclient.format = 32;
11507 message.xclient.data.l[0] = IconicState;
11508
334208b7
RS
11509 if (! XSendEvent (FRAME_X_DISPLAY (f),
11510 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea
JB
11511 False,
11512 SubstructureRedirectMask | SubstructureNotifyMask,
11513 &message))
dc6f92b8
JB
11514 {
11515 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 11516 error ("Can't notify window manager of iconification");
dc6f92b8 11517 }
16bd92ea 11518 }
dc6f92b8 11519
58769bee 11520 /* X11R3: set the initial_state field of the window manager hints to
16bd92ea
JB
11521 IconicState. */
11522 x_wm_set_window_state (f, IconicState);
dc6f92b8 11523
a9c00105
RS
11524 if (!FRAME_VISIBLE_P (f))
11525 {
11526 /* If the frame was withdrawn, before, we must map it. */
7f9c7f94 11527 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
a9c00105
RS
11528 }
11529
3a88c238 11530 f->async_iconified = 1;
1e6bc770 11531 f->async_visible = 0;
dc6f92b8 11532
334208b7 11533 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 11534 UNBLOCK_INPUT;
8c002a25 11535#endif /* not USE_X_TOOLKIT */
dc6f92b8 11536}
19f71add 11537
d047c4eb 11538\f
19f71add 11539/* Free X resources of frame F. */
dc6f92b8 11540
dfcf069d 11541void
19f71add 11542x_free_frame_resources (f)
f676886a 11543 struct frame *f;
dc6f92b8 11544{
7f9c7f94 11545 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
c6ea2775
EZ
11546 Lisp_Object bar;
11547 struct scroll_bar *b;
7f9c7f94 11548
dc6f92b8 11549 BLOCK_INPUT;
c0ff3fab 11550
6186a4a0
RS
11551 /* If a display connection is dead, don't try sending more
11552 commands to the X server. */
19f71add 11553 if (dpyinfo->display)
6186a4a0 11554 {
19f71add 11555 if (f->output_data.x->icon_desc)
6186a4a0 11556 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
c6ea2775
EZ
11557
11558#ifdef USE_X_TOOLKIT
11559 /* Explicitly destroy the scroll bars of the frame. Without
11560 this, we get "BadDrawable" errors from the toolkit later on,
11561 presumably from expose events generated for the disappearing
11562 toolkit scroll bars. */
11563 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
11564 {
11565 b = XSCROLL_BAR (bar);
11566 x_scroll_bar_remove (b);
11567 }
11568#endif
11569
31f41daf 11570#ifdef HAVE_X_I18N
f5d11644
GM
11571 if (FRAME_XIC (f))
11572 free_frame_xic (f);
31f41daf 11573#endif
c6ea2775 11574
3afe33e7 11575#ifdef USE_X_TOOLKIT
06a2c219 11576 if (f->output_data.x->widget)
30ca89f5
GM
11577 {
11578 XtDestroyWidget (f->output_data.x->widget);
11579 f->output_data.x->widget = NULL;
11580 }
c6ea2775
EZ
11581 /* Tooltips don't have widgets, only a simple X window, even if
11582 we are using a toolkit. */
11583 else if (FRAME_X_WINDOW (f))
11584 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11585
6186a4a0 11586 free_frame_menubar (f);
c6ea2775 11587#else /* !USE_X_TOOLKIT */
488dd4c4
JD
11588
11589#ifdef USE_GTK
11590 /* In the GTK version, tooltips are normal X
11591 frames. We must check and free both types. */
11592 if (FRAME_GTK_OUTER_WIDGET (f))
11593 {
11594 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
11595 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
11596 FRAME_GTK_OUTER_WIDGET (f) = 0;
11597 }
11598#endif /* USE_GTK */
177c0ea7 11599
c6ea2775
EZ
11600 if (FRAME_X_WINDOW (f))
11601 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11602#endif /* !USE_X_TOOLKIT */
3afe33e7 11603
3e71d8f2
GM
11604 unload_color (f, f->output_data.x->foreground_pixel);
11605 unload_color (f, f->output_data.x->background_pixel);
11606 unload_color (f, f->output_data.x->cursor_pixel);
11607 unload_color (f, f->output_data.x->cursor_foreground_pixel);
11608 unload_color (f, f->output_data.x->border_pixel);
11609 unload_color (f, f->output_data.x->mouse_pixel);
c6ea2775 11610
3e71d8f2
GM
11611 if (f->output_data.x->scroll_bar_background_pixel != -1)
11612 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
11613 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
11614 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
7c1bef7a
MB
11615#ifdef USE_TOOLKIT_SCROLL_BARS
11616 /* Scrollbar shadow colors. */
11617 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
11618 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
11619 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
11620 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
11621#endif /* USE_TOOLKIT_SCROLL_BARS */
3e71d8f2
GM
11622 if (f->output_data.x->white_relief.allocated_p)
11623 unload_color (f, f->output_data.x->white_relief.pixel);
11624 if (f->output_data.x->black_relief.allocated_p)
11625 unload_color (f, f->output_data.x->black_relief.pixel);
4ca78676 11626
19f71add
GM
11627 if (FRAME_FACE_CACHE (f))
11628 free_frame_faces (f);
7d0393cf 11629
4ca78676 11630 x_free_gcs (f);
6186a4a0
RS
11631 XFlush (FRAME_X_DISPLAY (f));
11632 }
dc6f92b8 11633
df89d8a4 11634 if (f->output_data.x->saved_menu_event)
06a2c219 11635 xfree (f->output_data.x->saved_menu_event);
0c49ce2f 11636
7556890b 11637 xfree (f->output_data.x);
19f71add 11638 f->output_data.x = NULL;
7d0393cf 11639
0f941935
KH
11640 if (f == dpyinfo->x_focus_frame)
11641 dpyinfo->x_focus_frame = 0;
11642 if (f == dpyinfo->x_focus_event_frame)
11643 dpyinfo->x_focus_event_frame = 0;
11644 if (f == dpyinfo->x_highlight_frame)
11645 dpyinfo->x_highlight_frame = 0;
c0ff3fab 11646
7f9c7f94 11647 if (f == dpyinfo->mouse_face_mouse_frame)
dc05a16b 11648 {
7f9c7f94
RS
11649 dpyinfo->mouse_face_beg_row
11650 = dpyinfo->mouse_face_beg_col = -1;
11651 dpyinfo->mouse_face_end_row
11652 = dpyinfo->mouse_face_end_col = -1;
11653 dpyinfo->mouse_face_window = Qnil;
21323706
RS
11654 dpyinfo->mouse_face_deferred_gc = 0;
11655 dpyinfo->mouse_face_mouse_frame = 0;
dc05a16b 11656 }
0134a210 11657
c0ff3fab 11658 UNBLOCK_INPUT;
dc6f92b8 11659}
19f71add
GM
11660
11661
11662/* Destroy the X window of frame F. */
11663
11664void
11665x_destroy_window (f)
11666 struct frame *f;
11667{
11668 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11669
11670 /* If a display connection is dead, don't try sending more
11671 commands to the X server. */
11672 if (dpyinfo->display != 0)
11673 x_free_frame_resources (f);
11674
11675 dpyinfo->reference_count--;
11676}
11677
dc6f92b8 11678\f
f451eb13
JB
11679/* Setting window manager hints. */
11680
af31d76f
RS
11681/* Set the normal size hints for the window manager, for frame F.
11682 FLAGS is the flags word to use--or 0 meaning preserve the flags
11683 that the window now has.
11684 If USER_POSITION is nonzero, we set the USPosition
488dd4c4
JD
11685 flag (this is useful when FLAGS is 0).
11686 The GTK version is in gtkutils.c */
6dba1858 11687
488dd4c4 11688#ifndef USE_GTK
dfcf069d 11689void
af31d76f 11690x_wm_set_size_hint (f, flags, user_position)
f676886a 11691 struct frame *f;
af31d76f
RS
11692 long flags;
11693 int user_position;
dc6f92b8
JB
11694{
11695 XSizeHints size_hints;
3afe33e7
RS
11696
11697#ifdef USE_X_TOOLKIT
7e4f2521
FP
11698 Arg al[2];
11699 int ac = 0;
11700 Dimension widget_width, widget_height;
488dd4c4 11701#endif
177c0ea7 11702
488dd4c4 11703 Window window = FRAME_OUTER_WINDOW (f);
dc6f92b8 11704
b72a58fd
RS
11705 /* Setting PMaxSize caused various problems. */
11706 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
dc6f92b8 11707
7556890b
RS
11708 size_hints.x = f->output_data.x->left_pos;
11709 size_hints.y = f->output_data.x->top_pos;
7553a6b7 11710
7e4f2521
FP
11711#ifdef USE_X_TOOLKIT
11712 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
11713 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
7556890b 11714 XtGetValues (f->output_data.x->widget, al, ac);
7e4f2521
FP
11715 size_hints.height = widget_height;
11716 size_hints.width = widget_width;
11717#else /* not USE_X_TOOLKIT */
f676886a
JB
11718 size_hints.height = PIXEL_HEIGHT (f);
11719 size_hints.width = PIXEL_WIDTH (f);
7e4f2521 11720#endif /* not USE_X_TOOLKIT */
7553a6b7 11721
7556890b
RS
11722 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
11723 size_hints.height_inc = f->output_data.x->line_height;
334208b7
RS
11724 size_hints.max_width
11725 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
11726 size_hints.max_height
11727 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
0134a210 11728
d067ea8b
KH
11729 /* Calculate the base and minimum sizes.
11730
11731 (When we use the X toolkit, we don't do it here.
11732 Instead we copy the values that the widgets are using, below.) */
11733#ifndef USE_X_TOOLKIT
b1c884c3 11734 {
b0342f17 11735 int base_width, base_height;
0134a210 11736 int min_rows = 0, min_cols = 0;
b0342f17 11737
f451eb13
JB
11738 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
11739 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
b0342f17 11740
0134a210 11741 check_frame_size (f, &min_rows, &min_cols);
b0342f17 11742
0134a210
RS
11743 /* The window manager uses the base width hints to calculate the
11744 current number of rows and columns in the frame while
11745 resizing; min_width and min_height aren't useful for this
11746 purpose, since they might not give the dimensions for a
11747 zero-row, zero-column frame.
58769bee 11748
0134a210
RS
11749 We use the base_width and base_height members if we have
11750 them; otherwise, we set the min_width and min_height members
11751 to the size for a zero x zero frame. */
b0342f17
JB
11752
11753#ifdef HAVE_X11R4
0134a210
RS
11754 size_hints.flags |= PBaseSize;
11755 size_hints.base_width = base_width;
11756 size_hints.base_height = base_height;
11757 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
11758 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
b0342f17 11759#else
0134a210
RS
11760 size_hints.min_width = base_width;
11761 size_hints.min_height = base_height;
b0342f17 11762#endif
b1c884c3 11763 }
dc6f92b8 11764
d067ea8b 11765 /* If we don't need the old flags, we don't need the old hint at all. */
af31d76f 11766 if (flags)
dc6f92b8 11767 {
d067ea8b
KH
11768 size_hints.flags |= flags;
11769 goto no_read;
11770 }
11771#endif /* not USE_X_TOOLKIT */
11772
11773 {
11774 XSizeHints hints; /* Sometimes I hate X Windows... */
11775 long supplied_return;
11776 int value;
af31d76f
RS
11777
11778#ifdef HAVE_X11R4
d067ea8b
KH
11779 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
11780 &supplied_return);
af31d76f 11781#else
d067ea8b 11782 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
af31d76f 11783#endif
58769bee 11784
d067ea8b
KH
11785#ifdef USE_X_TOOLKIT
11786 size_hints.base_height = hints.base_height;
11787 size_hints.base_width = hints.base_width;
11788 size_hints.min_height = hints.min_height;
11789 size_hints.min_width = hints.min_width;
11790#endif
11791
11792 if (flags)
11793 size_hints.flags |= flags;
11794 else
11795 {
11796 if (value == 0)
11797 hints.flags = 0;
11798 if (hints.flags & PSize)
11799 size_hints.flags |= PSize;
11800 if (hints.flags & PPosition)
11801 size_hints.flags |= PPosition;
11802 if (hints.flags & USPosition)
11803 size_hints.flags |= USPosition;
11804 if (hints.flags & USSize)
11805 size_hints.flags |= USSize;
11806 }
11807 }
11808
06a2c219 11809#ifndef USE_X_TOOLKIT
d067ea8b 11810 no_read:
06a2c219 11811#endif
0134a210 11812
af31d76f 11813#ifdef PWinGravity
7556890b 11814 size_hints.win_gravity = f->output_data.x->win_gravity;
af31d76f 11815 size_hints.flags |= PWinGravity;
dc05a16b 11816
af31d76f 11817 if (user_position)
6dba1858 11818 {
af31d76f
RS
11819 size_hints.flags &= ~ PPosition;
11820 size_hints.flags |= USPosition;
6dba1858 11821 }
2554751d 11822#endif /* PWinGravity */
6dba1858 11823
b0342f17 11824#ifdef HAVE_X11R4
334208b7 11825 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 11826#else
334208b7 11827 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 11828#endif
dc6f92b8 11829}
488dd4c4 11830#endif /* not USE_GTK */
dc6f92b8
JB
11831
11832/* Used for IconicState or NormalState */
06a2c219 11833
dfcf069d 11834void
f676886a
JB
11835x_wm_set_window_state (f, state)
11836 struct frame *f;
dc6f92b8
JB
11837 int state;
11838{
3afe33e7 11839#ifdef USE_X_TOOLKIT
546e6d5b
RS
11840 Arg al[1];
11841
11842 XtSetArg (al[0], XtNinitialState, state);
7556890b 11843 XtSetValues (f->output_data.x->widget, al, 1);
3afe33e7 11844#else /* not USE_X_TOOLKIT */
c118dd06 11845 Window window = FRAME_X_WINDOW (f);
dc6f92b8 11846
7556890b
RS
11847 f->output_data.x->wm_hints.flags |= StateHint;
11848 f->output_data.x->wm_hints.initial_state = state;
b1c884c3 11849
7556890b 11850 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
546e6d5b 11851#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11852}
11853
dfcf069d 11854void
7f2ae036 11855x_wm_set_icon_pixmap (f, pixmap_id)
f676886a 11856 struct frame *f;
7f2ae036 11857 int pixmap_id;
dc6f92b8 11858{
d2bd6bc4
RS
11859 Pixmap icon_pixmap;
11860
06a2c219 11861#ifndef USE_X_TOOLKIT
488dd4c4 11862 Window window = FRAME_OUTER_WINDOW (f);
75231bad 11863#endif
dc6f92b8 11864
7f2ae036 11865 if (pixmap_id > 0)
dbc4e1c1 11866 {
d2bd6bc4 11867 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
7556890b 11868 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
dbc4e1c1
JB
11869 }
11870 else
68568555
RS
11871 {
11872 /* It seems there is no way to turn off use of an icon pixmap.
11873 The following line does it, only if no icon has yet been created,
11874 for some window managers. But with mwm it crashes.
11875 Some people say it should clear the IconPixmapHint bit in this case,
11876 but that doesn't work, and the X consortium said it isn't the
11877 right thing at all. Since there is no way to win,
11878 best to explicitly give up. */
11879#if 0
11880 f->output_data.x->wm_hints.icon_pixmap = None;
11881#else
11882 return;
11883#endif
11884 }
b1c884c3 11885
d2bd6bc4
RS
11886#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
11887
11888 {
11889 Arg al[1];
11890 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
11891 XtSetValues (f->output_data.x->widget, al, 1);
11892 }
11893
11894#else /* not USE_X_TOOLKIT */
7d0393cf 11895
7556890b
RS
11896 f->output_data.x->wm_hints.flags |= IconPixmapHint;
11897 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
d2bd6bc4
RS
11898
11899#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
11900}
11901
dfcf069d 11902void
f676886a
JB
11903x_wm_set_icon_position (f, icon_x, icon_y)
11904 struct frame *f;
dc6f92b8
JB
11905 int icon_x, icon_y;
11906{
2436a4e4 11907 Window window = FRAME_OUTER_WINDOW (f);
dc6f92b8 11908
7556890b
RS
11909 f->output_data.x->wm_hints.flags |= IconPositionHint;
11910 f->output_data.x->wm_hints.icon_x = icon_x;
11911 f->output_data.x->wm_hints.icon_y = icon_y;
b1c884c3 11912
7556890b 11913 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
dc6f92b8
JB
11914}
11915
11916\f
06a2c219
GM
11917/***********************************************************************
11918 Fonts
11919 ***********************************************************************/
dc43ef94
KH
11920
11921/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
06a2c219 11922
dc43ef94
KH
11923struct font_info *
11924x_get_font_info (f, font_idx)
11925 FRAME_PTR f;
11926 int font_idx;
11927{
11928 return (FRAME_X_FONT_TABLE (f) + font_idx);
11929}
11930
11931
9c11f79e
GM
11932/* Return a list of names of available fonts matching PATTERN on frame F.
11933
11934 If SIZE is > 0, it is the size (maximum bounds width) of fonts
11935 to be listed.
11936
11937 SIZE < 0 means include scalable fonts.
11938
11939 Frame F null means we have not yet created any frame on X, and
11940 consult the first display in x_display_list. MAXNAMES sets a limit
11941 on how many fonts to match. */
dc43ef94
KH
11942
11943Lisp_Object
11944x_list_fonts (f, pattern, size, maxnames)
9c11f79e 11945 struct frame *f;
dc43ef94
KH
11946 Lisp_Object pattern;
11947 int size;
11948 int maxnames;
11949{
06a2c219
GM
11950 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
11951 Lisp_Object tem, second_best;
9c11f79e
GM
11952 struct x_display_info *dpyinfo
11953 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
11954 Display *dpy = dpyinfo->display;
09c6077f 11955 int try_XLoadQueryFont = 0;
53ca4657 11956 int count;
9c11f79e
GM
11957 int allow_scalable_fonts_p = 0;
11958
11959 if (size < 0)
11960 {
11961 allow_scalable_fonts_p = 1;
11962 size = 0;
11963 }
dc43ef94 11964
6b0efe73 11965 patterns = Fassoc (pattern, Valternate_fontname_alist);
2da424f1
KH
11966 if (NILP (patterns))
11967 patterns = Fcons (pattern, Qnil);
81ba44e5 11968
09c6077f
KH
11969 if (maxnames == 1 && !size)
11970 /* We can return any single font matching PATTERN. */
11971 try_XLoadQueryFont = 1;
9a32686f 11972
8e713be6 11973 for (; CONSP (patterns); patterns = XCDR (patterns))
dc43ef94 11974 {
dc43ef94 11975 int num_fonts;
3e71d8f2 11976 char **names = NULL;
dc43ef94 11977
8e713be6 11978 pattern = XCAR (patterns);
536f4067
RS
11979 /* See if we cached the result for this particular query.
11980 The cache is an alist of the form:
9c11f79e
GM
11981 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
11982 tem = XCDR (dpyinfo->name_list_element);
11983 key = Fcons (Fcons (pattern, make_number (maxnames)),
11984 allow_scalable_fonts_p ? Qt : Qnil);
11985 list = Fassoc (key, tem);
11986 if (!NILP (list))
b5210ea7
KH
11987 {
11988 list = Fcdr_safe (list);
11989 /* We have a cashed list. Don't have to get the list again. */
11990 goto label_cached;
11991 }
11992
11993 /* At first, put PATTERN in the cache. */
09c6077f 11994
dc43ef94 11995 BLOCK_INPUT;
17d85edc
KH
11996 count = x_catch_errors (dpy);
11997
09c6077f
KH
11998 if (try_XLoadQueryFont)
11999 {
12000 XFontStruct *font;
12001 unsigned long value;
12002
d5db4077 12003 font = XLoadQueryFont (dpy, SDATA (pattern));
17d85edc
KH
12004 if (x_had_errors_p (dpy))
12005 {
12006 /* This error is perhaps due to insufficient memory on X
12007 server. Let's just ignore it. */
12008 font = NULL;
12009 x_clear_errors (dpy);
12010 }
12011
09c6077f
KH
12012 if (font
12013 && XGetFontProperty (font, XA_FONT, &value))
12014 {
12015 char *name = (char *) XGetAtomName (dpy, (Atom) value);
12016 int len = strlen (name);
01c752b5 12017 char *tmp;
09c6077f 12018
6f6512e8
KH
12019 /* If DXPC (a Differential X Protocol Compressor)
12020 Ver.3.7 is running, XGetAtomName will return null
12021 string. We must avoid such a name. */
12022 if (len == 0)
12023 try_XLoadQueryFont = 0;
12024 else
12025 {
12026 num_fonts = 1;
12027 names = (char **) alloca (sizeof (char *));
12028 /* Some systems only allow alloca assigned to a
12029 simple var. */
12030 tmp = (char *) alloca (len + 1); names[0] = tmp;
12031 bcopy (name, names[0], len + 1);
12032 XFree (name);
12033 }
09c6077f
KH
12034 }
12035 else
12036 try_XLoadQueryFont = 0;
a083fd23
RS
12037
12038 if (font)
12039 XFreeFont (dpy, font);
09c6077f
KH
12040 }
12041
12042 if (!try_XLoadQueryFont)
17d85edc
KH
12043 {
12044 /* We try at least 10 fonts because XListFonts will return
12045 auto-scaled fonts at the head. */
ee5be7c3
JD
12046 if (maxnames < 0)
12047 {
12048 int limit;
12049
12050 for (limit = 500;;)
12051 {
12052 names = XListFonts (dpy, SDATA (pattern), limit, &num_fonts);
12053 if (num_fonts == limit)
12054 {
12055 BLOCK_INPUT;
12056 XFreeFontNames (names);
12057 UNBLOCK_INPUT;
12058 limit *= 2;
12059 }
12060 else
12061 break;
12062 }
12063 }
12064 else
12065 names = XListFonts (dpy, SDATA (pattern), max (maxnames, 10),
12066 &num_fonts);
12067
17d85edc
KH
12068 if (x_had_errors_p (dpy))
12069 {
12070 /* This error is perhaps due to insufficient memory on X
12071 server. Let's just ignore it. */
12072 names = NULL;
12073 x_clear_errors (dpy);
12074 }
12075 }
12076
12077 x_uncatch_errors (dpy, count);
dc43ef94
KH
12078 UNBLOCK_INPUT;
12079
12080 if (names)
12081 {
12082 int i;
dc43ef94
KH
12083
12084 /* Make a list of all the fonts we got back.
12085 Store that in the font cache for the display. */
12086 for (i = 0; i < num_fonts; i++)
12087 {
06a2c219 12088 int width = 0;
dc43ef94 12089 char *p = names[i];
06a2c219 12090 int average_width = -1, dashes = 0;
7d0393cf 12091
dc43ef94 12092 /* Count the number of dashes in NAMES[I]. If there are
9c11f79e
GM
12093 14 dashes, and the field value following 12th dash
12094 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
12095 is usually too ugly to be used for editing. Let's
12096 ignore it. */
dc43ef94
KH
12097 while (*p)
12098 if (*p++ == '-')
12099 {
12100 dashes++;
12101 if (dashes == 7) /* PIXEL_SIZE field */
12102 width = atoi (p);
12103 else if (dashes == 12) /* AVERAGE_WIDTH field */
12104 average_width = atoi (p);
12105 }
7d0393cf 12106
9c11f79e
GM
12107 if (allow_scalable_fonts_p
12108 || dashes < 14 || average_width != 0)
dc43ef94
KH
12109 {
12110 tem = build_string (names[i]);
12111 if (NILP (Fassoc (tem, list)))
12112 {
12113 if (STRINGP (Vx_pixel_size_width_font_regexp)
f39db7ea
RS
12114 && ((fast_c_string_match_ignore_case
12115 (Vx_pixel_size_width_font_regexp, names[i]))
dc43ef94
KH
12116 >= 0))
12117 /* We can set the value of PIXEL_SIZE to the
b5210ea7 12118 width of this font. */
dc43ef94
KH
12119 list = Fcons (Fcons (tem, make_number (width)), list);
12120 else
12121 /* For the moment, width is not known. */
12122 list = Fcons (Fcons (tem, Qnil), list);
12123 }
12124 }
12125 }
7d0393cf 12126
09c6077f 12127 if (!try_XLoadQueryFont)
e38f4136
GM
12128 {
12129 BLOCK_INPUT;
12130 XFreeFontNames (names);
12131 UNBLOCK_INPUT;
12132 }
dc43ef94
KH
12133 }
12134
b5210ea7 12135 /* Now store the result in the cache. */
f3fbd155
KR
12136 XSETCDR (dpyinfo->name_list_element,
12137 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
dc43ef94 12138
b5210ea7
KH
12139 label_cached:
12140 if (NILP (list)) continue; /* Try the remaining alternatives. */
dc43ef94 12141
b5210ea7
KH
12142 newlist = second_best = Qnil;
12143 /* Make a list of the fonts that have the right width. */
8e713be6 12144 for (; CONSP (list); list = XCDR (list))
b5210ea7 12145 {
536f4067
RS
12146 int found_size;
12147
8e713be6 12148 tem = XCAR (list);
dc43ef94 12149
8e713be6 12150 if (!CONSP (tem) || NILP (XCAR (tem)))
b5210ea7
KH
12151 continue;
12152 if (!size)
12153 {
8e713be6 12154 newlist = Fcons (XCAR (tem), newlist);
b5210ea7
KH
12155 continue;
12156 }
dc43ef94 12157
8e713be6 12158 if (!INTEGERP (XCDR (tem)))
dc43ef94 12159 {
b5210ea7 12160 /* Since we have not yet known the size of this font, we
9c11f79e 12161 must try slow function call XLoadQueryFont. */
dc43ef94
KH
12162 XFontStruct *thisinfo;
12163
12164 BLOCK_INPUT;
17d85edc 12165 count = x_catch_errors (dpy);
dc43ef94 12166 thisinfo = XLoadQueryFont (dpy,
d5db4077 12167 SDATA (XCAR (tem)));
17d85edc
KH
12168 if (x_had_errors_p (dpy))
12169 {
12170 /* This error is perhaps due to insufficient memory on X
12171 server. Let's just ignore it. */
12172 thisinfo = NULL;
12173 x_clear_errors (dpy);
12174 }
12175 x_uncatch_errors (dpy, count);
dc43ef94
KH
12176 UNBLOCK_INPUT;
12177
12178 if (thisinfo)
12179 {
f3fbd155
KR
12180 XSETCDR (tem,
12181 (thisinfo->min_bounds.width == 0
12182 ? make_number (0)
12183 : make_number (thisinfo->max_bounds.width)));
e38f4136 12184 BLOCK_INPUT;
dc43ef94 12185 XFreeFont (dpy, thisinfo);
e38f4136 12186 UNBLOCK_INPUT;
dc43ef94
KH
12187 }
12188 else
b5210ea7 12189 /* For unknown reason, the previous call of XListFont had
06a2c219 12190 returned a font which can't be opened. Record the size
b5210ea7 12191 as 0 not to try to open it again. */
f3fbd155 12192 XSETCDR (tem, make_number (0));
dc43ef94 12193 }
536f4067 12194
8e713be6 12195 found_size = XINT (XCDR (tem));
536f4067 12196 if (found_size == size)
8e713be6 12197 newlist = Fcons (XCAR (tem), newlist);
536f4067 12198 else if (found_size > 0)
b5210ea7 12199 {
536f4067 12200 if (NILP (second_best))
b5210ea7 12201 second_best = tem;
536f4067
RS
12202 else if (found_size < size)
12203 {
8e713be6
KR
12204 if (XINT (XCDR (second_best)) > size
12205 || XINT (XCDR (second_best)) < found_size)
536f4067
RS
12206 second_best = tem;
12207 }
12208 else
12209 {
8e713be6
KR
12210 if (XINT (XCDR (second_best)) > size
12211 && XINT (XCDR (second_best)) > found_size)
536f4067
RS
12212 second_best = tem;
12213 }
b5210ea7
KH
12214 }
12215 }
12216 if (!NILP (newlist))
12217 break;
12218 else if (!NILP (second_best))
12219 {
8e713be6 12220 newlist = Fcons (XCAR (second_best), Qnil);
b5210ea7 12221 break;
dc43ef94 12222 }
dc43ef94
KH
12223 }
12224
12225 return newlist;
7d0393cf 12226}
dc43ef94 12227
06a2c219
GM
12228
12229#if GLYPH_DEBUG
12230
12231/* Check that FONT is valid on frame F. It is if it can be found in F's
12232 font table. */
12233
12234static void
12235x_check_font (f, font)
12236 struct frame *f;
12237 XFontStruct *font;
12238{
12239 int i;
12240 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12241
12242 xassert (font != NULL);
12243
12244 for (i = 0; i < dpyinfo->n_fonts; i++)
7d0393cf 12245 if (dpyinfo->font_table[i].name
06a2c219
GM
12246 && font == dpyinfo->font_table[i].font)
12247 break;
12248
12249 xassert (i < dpyinfo->n_fonts);
12250}
12251
12252#endif /* GLYPH_DEBUG != 0 */
12253
12254/* Set *W to the minimum width, *H to the minimum font height of FONT.
12255 Note: There are (broken) X fonts out there with invalid XFontStruct
12256 min_bounds contents. For example, handa@etl.go.jp reports that
12257 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
12258 have font->min_bounds.width == 0. */
12259
12260static INLINE void
12261x_font_min_bounds (font, w, h)
12262 XFontStruct *font;
12263 int *w, *h;
12264{
12265 *h = FONT_HEIGHT (font);
12266 *w = font->min_bounds.width;
12267
12268 /* Try to handle the case where FONT->min_bounds has invalid
12269 contents. Since the only font known to have invalid min_bounds
12270 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
12271 if (*w <= 0)
12272 *w = font->max_bounds.width;
12273}
12274
12275
12276/* Compute the smallest character width and smallest font height over
12277 all fonts available on frame F. Set the members smallest_char_width
12278 and smallest_font_height in F's x_display_info structure to
12279 the values computed. Value is non-zero if smallest_font_height or
12280 smallest_char_width become smaller than they were before. */
12281
12282static int
12283x_compute_min_glyph_bounds (f)
12284 struct frame *f;
12285{
12286 int i;
12287 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12288 XFontStruct *font;
12289 int old_width = dpyinfo->smallest_char_width;
12290 int old_height = dpyinfo->smallest_font_height;
7d0393cf 12291
06a2c219
GM
12292 dpyinfo->smallest_font_height = 100000;
12293 dpyinfo->smallest_char_width = 100000;
7d0393cf 12294
06a2c219
GM
12295 for (i = 0; i < dpyinfo->n_fonts; ++i)
12296 if (dpyinfo->font_table[i].name)
12297 {
12298 struct font_info *fontp = dpyinfo->font_table + i;
12299 int w, h;
7d0393cf 12300
06a2c219
GM
12301 font = (XFontStruct *) fontp->font;
12302 xassert (font != (XFontStruct *) ~0);
12303 x_font_min_bounds (font, &w, &h);
7d0393cf 12304
06a2c219
GM
12305 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
12306 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
12307 }
12308
12309 xassert (dpyinfo->smallest_char_width > 0
12310 && dpyinfo->smallest_font_height > 0);
12311
12312 return (dpyinfo->n_fonts == 1
12313 || dpyinfo->smallest_char_width < old_width
12314 || dpyinfo->smallest_font_height < old_height);
12315}
12316
12317
dc43ef94
KH
12318/* Load font named FONTNAME of the size SIZE for frame F, and return a
12319 pointer to the structure font_info while allocating it dynamically.
12320 If SIZE is 0, load any size of font.
12321 If loading is failed, return NULL. */
12322
12323struct font_info *
12324x_load_font (f, fontname, size)
12325 struct frame *f;
12326 register char *fontname;
12327 int size;
12328{
12329 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12330 Lisp_Object font_names;
d645aaa4 12331 int count;
dc43ef94
KH
12332
12333 /* Get a list of all the fonts that match this name. Once we
12334 have a list of matching fonts, we compare them against the fonts
12335 we already have by comparing names. */
09c6077f 12336 font_names = x_list_fonts (f, build_string (fontname), size, 1);
dc43ef94
KH
12337
12338 if (!NILP (font_names))
12339 {
12340 Lisp_Object tail;
12341 int i;
12342
12343 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 12344 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
06a2c219
GM
12345 if (dpyinfo->font_table[i].name
12346 && (!strcmp (dpyinfo->font_table[i].name,
d5db4077 12347 SDATA (XCAR (tail)))
06a2c219 12348 || !strcmp (dpyinfo->font_table[i].full_name,
d5db4077 12349 SDATA (XCAR (tail)))))
dc43ef94
KH
12350 return (dpyinfo->font_table + i);
12351 }
12352
12353 /* Load the font and add it to the table. */
12354 {
12355 char *full_name;
12356 XFontStruct *font;
12357 struct font_info *fontp;
12358 unsigned long value;
06a2c219 12359 int i;
dc43ef94 12360
2da424f1
KH
12361 /* If we have found fonts by x_list_font, load one of them. If
12362 not, we still try to load a font by the name given as FONTNAME
12363 because XListFonts (called in x_list_font) of some X server has
12364 a bug of not finding a font even if the font surely exists and
12365 is loadable by XLoadQueryFont. */
e1d6d5b9 12366 if (size > 0 && !NILP (font_names))
d5db4077 12367 fontname = (char *) SDATA (XCAR (font_names));
dc43ef94
KH
12368
12369 BLOCK_INPUT;
d645aaa4 12370 count = x_catch_errors (FRAME_X_DISPLAY (f));
dc43ef94 12371 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
d645aaa4
KH
12372 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12373 {
12374 /* This error is perhaps due to insufficient memory on X
12375 server. Let's just ignore it. */
12376 font = NULL;
12377 x_clear_errors (FRAME_X_DISPLAY (f));
12378 }
12379 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
dc43ef94 12380 UNBLOCK_INPUT;
b5210ea7 12381 if (!font)
dc43ef94
KH
12382 return NULL;
12383
06a2c219
GM
12384 /* Find a free slot in the font table. */
12385 for (i = 0; i < dpyinfo->n_fonts; ++i)
12386 if (dpyinfo->font_table[i].name == NULL)
12387 break;
12388
12389 /* If no free slot found, maybe enlarge the font table. */
12390 if (i == dpyinfo->n_fonts
12391 && dpyinfo->n_fonts == dpyinfo->font_table_size)
dc43ef94 12392 {
06a2c219
GM
12393 int sz;
12394 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
12395 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
dc43ef94 12396 dpyinfo->font_table
06a2c219 12397 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
dc43ef94
KH
12398 }
12399
06a2c219
GM
12400 fontp = dpyinfo->font_table + i;
12401 if (i == dpyinfo->n_fonts)
12402 ++dpyinfo->n_fonts;
dc43ef94
KH
12403
12404 /* Now fill in the slots of *FONTP. */
12405 BLOCK_INPUT;
12406 fontp->font = font;
06a2c219 12407 fontp->font_idx = i;
dc43ef94
KH
12408 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
12409 bcopy (fontname, fontp->name, strlen (fontname) + 1);
12410
12411 /* Try to get the full name of FONT. Put it in FULL_NAME. */
12412 full_name = 0;
12413 if (XGetFontProperty (font, XA_FONT, &value))
12414 {
12415 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
12416 char *p = name;
12417 int dashes = 0;
12418
12419 /* Count the number of dashes in the "full name".
12420 If it is too few, this isn't really the font's full name,
12421 so don't use it.
12422 In X11R4, the fonts did not come with their canonical names
12423 stored in them. */
12424 while (*p)
12425 {
12426 if (*p == '-')
12427 dashes++;
12428 p++;
12429 }
12430
12431 if (dashes >= 13)
12432 {
12433 full_name = (char *) xmalloc (p - name + 1);
12434 bcopy (name, full_name, p - name + 1);
12435 }
12436
12437 XFree (name);
12438 }
7d0393cf 12439
dc43ef94
KH
12440 if (full_name != 0)
12441 fontp->full_name = full_name;
12442 else
12443 fontp->full_name = fontp->name;
12444
12445 fontp->size = font->max_bounds.width;
d5749adb 12446 fontp->height = FONT_HEIGHT (font);
dc43ef94 12447
2da424f1
KH
12448 if (NILP (font_names))
12449 {
12450 /* We come here because of a bug of XListFonts mentioned at
12451 the head of this block. Let's store this information in
12452 the cache for x_list_fonts. */
12453 Lisp_Object lispy_name = build_string (fontname);
12454 Lisp_Object lispy_full_name = build_string (fontp->full_name);
9c11f79e
GM
12455 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
12456 Qnil);
2da424f1 12457
f3fbd155
KR
12458 XSETCDR (dpyinfo->name_list_element,
12459 Fcons (Fcons (key,
12460 Fcons (Fcons (lispy_full_name,
12461 make_number (fontp->size)),
12462 Qnil)),
12463 XCDR (dpyinfo->name_list_element)));
2da424f1 12464 if (full_name)
9c11f79e
GM
12465 {
12466 key = Fcons (Fcons (lispy_full_name, make_number (256)),
12467 Qnil);
f3fbd155
KR
12468 XSETCDR (dpyinfo->name_list_element,
12469 Fcons (Fcons (key,
12470 Fcons (Fcons (lispy_full_name,
12471 make_number (fontp->size)),
12472 Qnil)),
12473 XCDR (dpyinfo->name_list_element)));
9c11f79e 12474 }
2da424f1
KH
12475 }
12476
dc43ef94
KH
12477 /* The slot `encoding' specifies how to map a character
12478 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
ee569018
KH
12479 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
12480 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
8ff102bd 12481 2:0xA020..0xFF7F). For the moment, we don't know which charset
06a2c219 12482 uses this font. So, we set information in fontp->encoding[1]
8ff102bd
RS
12483 which is never used by any charset. If mapping can't be
12484 decided, set FONT_ENCODING_NOT_DECIDED. */
dc43ef94
KH
12485 fontp->encoding[1]
12486 = (font->max_byte1 == 0
12487 /* 1-byte font */
12488 ? (font->min_char_or_byte2 < 0x80
12489 ? (font->max_char_or_byte2 < 0x80
12490 ? 0 /* 0x20..0x7F */
8ff102bd 12491 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
dc43ef94
KH
12492 : 1) /* 0xA0..0xFF */
12493 /* 2-byte font */
12494 : (font->min_byte1 < 0x80
12495 ? (font->max_byte1 < 0x80
12496 ? (font->min_char_or_byte2 < 0x80
12497 ? (font->max_char_or_byte2 < 0x80
12498 ? 0 /* 0x2020..0x7F7F */
8ff102bd 12499 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
dc43ef94 12500 : 3) /* 0x20A0..0x7FFF */
8ff102bd 12501 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
dc43ef94
KH
12502 : (font->min_char_or_byte2 < 0x80
12503 ? (font->max_char_or_byte2 < 0x80
12504 ? 2 /* 0xA020..0xFF7F */
8ff102bd 12505 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
dc43ef94
KH
12506 : 1))); /* 0xA0A0..0xFFFF */
12507
12508 fontp->baseline_offset
12509 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
12510 ? (long) value : 0);
12511 fontp->relative_compose
12512 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
12513 ? (long) value : 0);
f78798df
KH
12514 fontp->default_ascent
12515 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
12516 ? (long) value : 0);
dc43ef94 12517
06a2c219
GM
12518 /* Set global flag fonts_changed_p to non-zero if the font loaded
12519 has a character with a smaller width than any other character
437dfb9f 12520 before, or if the font loaded has a smaller height than any
06a2c219
GM
12521 other font loaded before. If this happens, it will make a
12522 glyph matrix reallocation necessary. */
437dfb9f 12523 fonts_changed_p |= x_compute_min_glyph_bounds (f);
dc43ef94 12524 UNBLOCK_INPUT;
dc43ef94
KH
12525 return fontp;
12526 }
12527}
12528
06a2c219
GM
12529
12530/* Return a pointer to struct font_info of a font named FONTNAME for
12531 frame F. If no such font is loaded, return NULL. */
12532
dc43ef94
KH
12533struct font_info *
12534x_query_font (f, fontname)
12535 struct frame *f;
12536 register char *fontname;
12537{
12538 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12539 int i;
12540
12541 for (i = 0; i < dpyinfo->n_fonts; i++)
06a2c219
GM
12542 if (dpyinfo->font_table[i].name
12543 && (!strcmp (dpyinfo->font_table[i].name, fontname)
12544 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
dc43ef94
KH
12545 return (dpyinfo->font_table + i);
12546 return NULL;
12547}
12548
06a2c219
GM
12549
12550/* Find a CCL program for a font specified by FONTP, and set the member
a6582676
KH
12551 `encoder' of the structure. */
12552
12553void
12554x_find_ccl_program (fontp)
12555 struct font_info *fontp;
12556{
a42f54e6 12557 Lisp_Object list, elt;
a6582676 12558
f9b5db02 12559 elt = Qnil;
8e713be6 12560 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
a6582676 12561 {
8e713be6 12562 elt = XCAR (list);
a6582676 12563 if (CONSP (elt)
8e713be6 12564 && STRINGP (XCAR (elt))
9f2feff6
KH
12565 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
12566 >= 0)
12567 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
12568 >= 0)))
a42f54e6
KH
12569 break;
12570 }
7d0393cf 12571
a42f54e6
KH
12572 if (! NILP (list))
12573 {
d27f8ca7
KH
12574 struct ccl_program *ccl
12575 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
a42f54e6 12576
8e713be6 12577 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
a42f54e6
KH
12578 xfree (ccl);
12579 else
12580 fontp->font_encoder = ccl;
a6582676
KH
12581 }
12582}
12583
06a2c219 12584
dc43ef94 12585\f
06a2c219
GM
12586/***********************************************************************
12587 Initialization
12588 ***********************************************************************/
f451eb13 12589
3afe33e7
RS
12590#ifdef USE_X_TOOLKIT
12591static XrmOptionDescRec emacs_options[] = {
12592 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
12593 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
12594
12595 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
12596 XrmoptionSepArg, NULL},
12597 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
12598
12599 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12600 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12601 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12602 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12603 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12604 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
12605 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
12606};
12607#endif /* USE_X_TOOLKIT */
12608
7a13e894
RS
12609static int x_initialized;
12610
29b38361
KH
12611#ifdef MULTI_KBOARD
12612/* Test whether two display-name strings agree up to the dot that separates
12613 the screen number from the server number. */
12614static int
12615same_x_server (name1, name2)
59ab1dc7 12616 const char *name1, *name2;
29b38361
KH
12617{
12618 int seen_colon = 0;
59ab1dc7 12619 const unsigned char *system_name = SDATA (Vsystem_name);
cf591cc1
RS
12620 int system_name_length = strlen (system_name);
12621 int length_until_period = 0;
12622
12623 while (system_name[length_until_period] != 0
12624 && system_name[length_until_period] != '.')
12625 length_until_period++;
12626
12627 /* Treat `unix' like an empty host name. */
12628 if (! strncmp (name1, "unix:", 5))
12629 name1 += 4;
12630 if (! strncmp (name2, "unix:", 5))
12631 name2 += 4;
12632 /* Treat this host's name like an empty host name. */
12633 if (! strncmp (name1, system_name, system_name_length)
12634 && name1[system_name_length] == ':')
12635 name1 += system_name_length;
12636 if (! strncmp (name2, system_name, system_name_length)
12637 && name2[system_name_length] == ':')
12638 name2 += system_name_length;
12639 /* Treat this host's domainless name like an empty host name. */
12640 if (! strncmp (name1, system_name, length_until_period)
12641 && name1[length_until_period] == ':')
12642 name1 += length_until_period;
12643 if (! strncmp (name2, system_name, length_until_period)
12644 && name2[length_until_period] == ':')
12645 name2 += length_until_period;
12646
29b38361
KH
12647 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
12648 {
12649 if (*name1 == ':')
12650 seen_colon++;
12651 if (seen_colon && *name1 == '.')
12652 return 1;
12653 }
12654 return (seen_colon
12655 && (*name1 == '.' || *name1 == '\0')
12656 && (*name2 == '.' || *name2 == '\0'));
12657}
12658#endif
12659
334208b7 12660struct x_display_info *
1f8255f2 12661x_term_init (display_name, xrm_option, resource_name)
334208b7 12662 Lisp_Object display_name;
1f8255f2
RS
12663 char *xrm_option;
12664 char *resource_name;
dc6f92b8 12665{
334208b7 12666 int connection;
7a13e894 12667 Display *dpy;
334208b7
RS
12668 struct x_display_info *dpyinfo;
12669 XrmDatabase xrdb;
12670
60439948
KH
12671 BLOCK_INPUT;
12672
7a13e894
RS
12673 if (!x_initialized)
12674 {
12675 x_initialize ();
12676 x_initialized = 1;
12677 }
dc6f92b8 12678
488dd4c4
JD
12679#ifdef USE_GTK
12680 {
12681#define NUM_ARGV 10
12682 int argc;
12683 char *argv[NUM_ARGV];
12684 char **argv2 = argv;
12685 GdkAtom atom;
12686
12687 /* GTK 2.0 can only handle one display, GTK 2.2 can handle more
12688 than one, but this remains to be implemented. */
12689 if (x_initialized > 1)
12690 return 0;
12691
12692 x_initialized++;
12693
12694 for (argc = 0; argc < NUM_ARGV; ++argc)
12695 argv[argc] = 0;
12696
12697 argc = 0;
12698 argv[argc++] = initial_argv[0];
12699
12700 if (! NILP (display_name))
12701 {
12702 argv[argc++] = "--display";
12703 argv[argc++] = SDATA (display_name);
12704 }
12705
12706 argv[argc++] = "--name";
12707 argv[argc++] = resource_name;
177c0ea7 12708
1fcfb866
JD
12709#ifdef HAVE_X11R5
12710 XSetLocaleModifiers ("");
12711#endif
12712
488dd4c4
JD
12713 gtk_init (&argc, &argv2);
12714
12715 /* gtk_init does set_locale. We must fix locale after calling it. */
12716 fixup_locale ();
12717 xg_initialize ();
12718
12719 dpy = GDK_DISPLAY ();
177c0ea7 12720
488dd4c4
JD
12721 /* NULL window -> events for all windows go to our function */
12722 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
12723
12724 /* Load our own gtkrc if it exists. */
12725 {
12726 struct gcpro gcpro1, gcpro2;
12727 char *file = "~/.emacs.d/gtkrc";
12728 Lisp_Object s, abs_file;
12729
a3e4741f 12730 GCPRO2 (s, abs_file);
488dd4c4
JD
12731 s = make_string (file, strlen (file));
12732 abs_file = Fexpand_file_name(s, Qnil);
12733
12734 if (! NILP (abs_file) && Ffile_readable_p (abs_file))
12735 gtk_rc_parse (SDATA (abs_file));
177c0ea7 12736
488dd4c4
JD
12737 UNGCPRO;
12738 }
177c0ea7 12739
488dd4c4
JD
12740 XSetErrorHandler (x_error_handler);
12741 XSetIOErrorHandler (x_io_error_quitter);
12742 }
12743#else /* not USE_GTK */
3afe33e7 12744#ifdef USE_X_TOOLKIT
2d7fc7e8
RS
12745 /* weiner@footloose.sps.mot.com reports that this causes
12746 errors with X11R5:
12747 X protocol error: BadAtom (invalid Atom parameter)
12748 on protocol request 18skiloaf.
12749 So let's not use it until R6. */
12750#ifdef HAVE_X11XTR6
bdcd49ba
RS
12751 XtSetLanguageProc (NULL, NULL, NULL);
12752#endif
12753
7f9c7f94
RS
12754 {
12755 int argc = 0;
12756 char *argv[3];
12757
12758 argv[0] = "";
12759 argc = 1;
12760 if (xrm_option)
12761 {
12762 argv[argc++] = "-xrm";
12763 argv[argc++] = xrm_option;
12764 }
bc4e60fc 12765 turn_on_atimers (0);
d5db4077 12766 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
7f9c7f94
RS
12767 resource_name, EMACS_CLASS,
12768 emacs_options, XtNumber (emacs_options),
12769 &argc, argv);
bc4e60fc 12770 turn_on_atimers (1);
39d8bb4d
KH
12771
12772#ifdef HAVE_X11XTR6
10537cb1 12773 /* I think this is to compensate for XtSetLanguageProc. */
71f8198a 12774 fixup_locale ();
39d8bb4d 12775#endif
7f9c7f94 12776 }
3afe33e7
RS
12777
12778#else /* not USE_X_TOOLKIT */
bdcd49ba
RS
12779#ifdef HAVE_X11R5
12780 XSetLocaleModifiers ("");
12781#endif
d5db4077 12782 dpy = XOpenDisplay (SDATA (display_name));
3afe33e7 12783#endif /* not USE_X_TOOLKIT */
488dd4c4 12784#endif /* not USE_GTK*/
334208b7 12785
7a13e894
RS
12786 /* Detect failure. */
12787 if (dpy == 0)
60439948
KH
12788 {
12789 UNBLOCK_INPUT;
12790 return 0;
12791 }
7a13e894
RS
12792
12793 /* We have definitely succeeded. Record the new connection. */
12794
12795 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
f04e1297 12796 bzero (dpyinfo, sizeof *dpyinfo);
7a13e894 12797
29b38361
KH
12798#ifdef MULTI_KBOARD
12799 {
12800 struct x_display_info *share;
12801 Lisp_Object tail;
12802
12803 for (share = x_display_list, tail = x_display_name_list; share;
8e713be6 12804 share = share->next, tail = XCDR (tail))
d5db4077
KR
12805 if (same_x_server (SDATA (XCAR (XCAR (tail))),
12806 SDATA (display_name)))
29b38361
KH
12807 break;
12808 if (share)
12809 dpyinfo->kboard = share->kboard;
12810 else
12811 {
12812 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
12813 init_kboard (dpyinfo->kboard);
59e755be
KH
12814 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
12815 {
12816 char *vendor = ServerVendor (dpy);
9b6ed9f3 12817 UNBLOCK_INPUT;
59e755be
KH
12818 dpyinfo->kboard->Vsystem_key_alist
12819 = call1 (Qvendor_specific_keysyms,
12820 build_string (vendor ? vendor : ""));
9b6ed9f3 12821 BLOCK_INPUT;
59e755be
KH
12822 }
12823
29b38361
KH
12824 dpyinfo->kboard->next_kboard = all_kboards;
12825 all_kboards = dpyinfo->kboard;
0ad5446c
KH
12826 /* Don't let the initial kboard remain current longer than necessary.
12827 That would cause problems if a file loaded on startup tries to
06a2c219 12828 prompt in the mini-buffer. */
0ad5446c
KH
12829 if (current_kboard == initial_kboard)
12830 current_kboard = dpyinfo->kboard;
29b38361
KH
12831 }
12832 dpyinfo->kboard->reference_count++;
12833 }
b9737ad3
KH
12834#endif
12835
7a13e894
RS
12836 /* Put this display on the chain. */
12837 dpyinfo->next = x_display_list;
12838 x_display_list = dpyinfo;
12839
7d0393cf 12840 /* Put it on x_display_name_list as well, to keep them parallel. */
7a13e894
RS
12841 x_display_name_list = Fcons (Fcons (display_name, Qnil),
12842 x_display_name_list);
8e713be6 12843 dpyinfo->name_list_element = XCAR (x_display_name_list);
7a13e894
RS
12844
12845 dpyinfo->display = dpy;
dc6f92b8 12846
dc6f92b8 12847#if 0
7a13e894 12848 XSetAfterFunction (x_current_display, x_trace_wire);
c118dd06 12849#endif /* ! 0 */
7a13e894
RS
12850
12851 dpyinfo->x_id_name
d5db4077
KR
12852 = (char *) xmalloc (SBYTES (Vinvocation_name)
12853 + SBYTES (Vsystem_name)
7a13e894
RS
12854 + 2);
12855 sprintf (dpyinfo->x_id_name, "%s@%s",
d5db4077 12856 SDATA (Vinvocation_name), SDATA (Vsystem_name));
28430d3c
JB
12857
12858 /* Figure out which modifier bits mean what. */
334208b7 12859 x_find_modifier_meanings (dpyinfo);
f451eb13 12860
ab648270 12861 /* Get the scroll bar cursor. */
7a13e894 12862 dpyinfo->vertical_scroll_bar_cursor
334208b7 12863 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
f451eb13 12864
334208b7
RS
12865 xrdb = x_load_resources (dpyinfo->display, xrm_option,
12866 resource_name, EMACS_CLASS);
12867#ifdef HAVE_XRMSETDATABASE
12868 XrmSetDatabase (dpyinfo->display, xrdb);
12869#else
12870 dpyinfo->display->db = xrdb;
12871#endif
547d9db8 12872 /* Put the rdb where we can find it in a way that works on
7a13e894
RS
12873 all versions. */
12874 dpyinfo->xrdb = xrdb;
334208b7
RS
12875
12876 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
12877 DefaultScreen (dpyinfo->display));
5ff67d81 12878 select_visual (dpyinfo);
43bd1b2b 12879 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
334208b7
RS
12880 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
12881 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
12882 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
12883 dpyinfo->grabbed = 0;
12884 dpyinfo->reference_count = 0;
12885 dpyinfo->icon_bitmap_id = -1;
06a2c219 12886 dpyinfo->font_table = NULL;
7a13e894
RS
12887 dpyinfo->n_fonts = 0;
12888 dpyinfo->font_table_size = 0;
12889 dpyinfo->bitmaps = 0;
12890 dpyinfo->bitmaps_size = 0;
12891 dpyinfo->bitmaps_last = 0;
12892 dpyinfo->scratch_cursor_gc = 0;
12893 dpyinfo->mouse_face_mouse_frame = 0;
12894 dpyinfo->mouse_face_deferred_gc = 0;
12895 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
12896 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
06a2c219 12897 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
7a13e894 12898 dpyinfo->mouse_face_window = Qnil;
0a61c667 12899 dpyinfo->mouse_face_overlay = Qnil;
7a13e894
RS
12900 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
12901 dpyinfo->mouse_face_defer = 0;
66301061 12902 dpyinfo->mouse_face_hidden = 0;
0f941935
KH
12903 dpyinfo->x_focus_frame = 0;
12904 dpyinfo->x_focus_event_frame = 0;
12905 dpyinfo->x_highlight_frame = 0;
06a2c219 12906 dpyinfo->image_cache = make_image_cache ();
334208b7 12907
43bd1b2b 12908 /* See if a private colormap is requested. */
5ff67d81
GM
12909 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
12910 {
12911 if (dpyinfo->visual->class == PseudoColor)
12912 {
12913 Lisp_Object value;
12914 value = display_x_get_resource (dpyinfo,
12915 build_string ("privateColormap"),
12916 build_string ("PrivateColormap"),
12917 Qnil, Qnil);
12918 if (STRINGP (value)
d5db4077
KR
12919 && (!strcmp (SDATA (value), "true")
12920 || !strcmp (SDATA (value), "on")))
5ff67d81
GM
12921 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
12922 }
43bd1b2b 12923 }
5ff67d81
GM
12924 else
12925 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
12926 dpyinfo->visual, AllocNone);
7d0393cf 12927
06a2c219
GM
12928 {
12929 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
12930 double pixels = DisplayHeight (dpyinfo->display, screen_number);
12931 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
12932 dpyinfo->resy = pixels * 25.4 / mm;
12933 pixels = DisplayWidth (dpyinfo->display, screen_number);
12934 mm = DisplayWidthMM (dpyinfo->display, screen_number);
12935 dpyinfo->resx = pixels * 25.4 / mm;
12936 }
7d0393cf 12937
334208b7
RS
12938 dpyinfo->Xatom_wm_protocols
12939 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
12940 dpyinfo->Xatom_wm_take_focus
12941 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
12942 dpyinfo->Xatom_wm_save_yourself
12943 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
12944 dpyinfo->Xatom_wm_delete_window
12945 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
12946 dpyinfo->Xatom_wm_change_state
12947 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
12948 dpyinfo->Xatom_wm_configure_denied
12949 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
12950 dpyinfo->Xatom_wm_window_moved
12951 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
12952 dpyinfo->Xatom_editres
12953 = XInternAtom (dpyinfo->display, "Editres", False);
12954 dpyinfo->Xatom_CLIPBOARD
12955 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
12956 dpyinfo->Xatom_TIMESTAMP
12957 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
12958 dpyinfo->Xatom_TEXT
12959 = XInternAtom (dpyinfo->display, "TEXT", False);
dc43ef94
KH
12960 dpyinfo->Xatom_COMPOUND_TEXT
12961 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
c62525b7
KH
12962 dpyinfo->Xatom_UTF8_STRING
12963 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
334208b7
RS
12964 dpyinfo->Xatom_DELETE
12965 = XInternAtom (dpyinfo->display, "DELETE", False);
12966 dpyinfo->Xatom_MULTIPLE
12967 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
12968 dpyinfo->Xatom_INCR
12969 = XInternAtom (dpyinfo->display, "INCR", False);
12970 dpyinfo->Xatom_EMACS_TMP
12971 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
12972 dpyinfo->Xatom_TARGETS
12973 = XInternAtom (dpyinfo->display, "TARGETS", False);
12974 dpyinfo->Xatom_NULL
12975 = XInternAtom (dpyinfo->display, "NULL", False);
12976 dpyinfo->Xatom_ATOM_PAIR
12977 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
dc43ef94
KH
12978 /* For properties of font. */
12979 dpyinfo->Xatom_PIXEL_SIZE
12980 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
12981 dpyinfo->Xatom_MULE_BASELINE_OFFSET
12982 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
12983 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
12984 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
f78798df
KH
12985 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
12986 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
334208b7 12987
06a2c219
GM
12988 /* Ghostscript support. */
12989 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
12990 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
7d0393cf 12991
06a2c219
GM
12992 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
12993 False);
12994
547d9db8
KH
12995 dpyinfo->cut_buffers_initialized = 0;
12996
334208b7
RS
12997 connection = ConnectionNumber (dpyinfo->display);
12998 dpyinfo->connection = connection;
12999
dc43ef94 13000 {
5d7cc324
RS
13001 char null_bits[1];
13002
13003 null_bits[0] = 0x00;
dc43ef94
KH
13004
13005 dpyinfo->null_pixel
7d0393cf 13006 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
dc43ef94
KH
13007 null_bits, 1, 1, (long) 0, (long) 0,
13008 1);
13009 }
13010
06a2c219
GM
13011 {
13012 extern int gray_bitmap_width, gray_bitmap_height;
10659c77 13013 extern char *gray_bitmap_bits;
06a2c219
GM
13014 dpyinfo->gray
13015 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13016 gray_bitmap_bits,
13017 gray_bitmap_width, gray_bitmap_height,
13018 (unsigned long) 1, (unsigned long) 0, 1);
13019 }
13020
f5d11644
GM
13021#ifdef HAVE_X_I18N
13022 xim_initialize (dpyinfo, resource_name);
13023#endif
7d0393cf 13024
87485d6f
MW
13025#ifdef subprocesses
13026 /* This is only needed for distinguishing keyboard and process input. */
334208b7 13027 if (connection != 0)
7a13e894 13028 add_keyboard_wait_descriptor (connection);
87485d6f 13029#endif
6d4238f3 13030
041b69ac 13031#ifndef F_SETOWN_BUG
dc6f92b8 13032#ifdef F_SETOWN
dc6f92b8 13033#ifdef F_SETOWN_SOCK_NEG
61c3ce62 13034 /* stdin is a socket here */
334208b7 13035 fcntl (connection, F_SETOWN, -getpid ());
c118dd06 13036#else /* ! defined (F_SETOWN_SOCK_NEG) */
334208b7 13037 fcntl (connection, F_SETOWN, getpid ());
c118dd06
JB
13038#endif /* ! defined (F_SETOWN_SOCK_NEG) */
13039#endif /* ! defined (F_SETOWN) */
041b69ac 13040#endif /* F_SETOWN_BUG */
dc6f92b8
JB
13041
13042#ifdef SIGIO
eee20f6a
KH
13043 if (interrupt_input)
13044 init_sigio (connection);
c118dd06 13045#endif /* ! defined (SIGIO) */
dc6f92b8 13046
51b592fb 13047#ifdef USE_LUCID
f8c39f51 13048#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
51b592fb
RS
13049 /* Make sure that we have a valid font for dialog boxes
13050 so that Xt does not crash. */
13051 {
13052 Display *dpy = dpyinfo->display;
13053 XrmValue d, fr, to;
13054 Font font;
e99db5a1 13055 int count;
7d0393cf 13056
51b592fb
RS
13057 d.addr = (XPointer)&dpy;
13058 d.size = sizeof (Display *);
13059 fr.addr = XtDefaultFont;
13060 fr.size = sizeof (XtDefaultFont);
13061 to.size = sizeof (Font *);
13062 to.addr = (XPointer)&font;
e99db5a1 13063 count = x_catch_errors (dpy);
51b592fb
RS
13064 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
13065 abort ();
13066 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
13067 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
e99db5a1 13068 x_uncatch_errors (dpy, count);
51b592fb
RS
13069 }
13070#endif
f8c39f51 13071#endif
51b592fb 13072
34e23e5a
GM
13073 /* See if we should run in synchronous mode. This is useful
13074 for debugging X code. */
13075 {
13076 Lisp_Object value;
13077 value = display_x_get_resource (dpyinfo,
13078 build_string ("synchronous"),
13079 build_string ("Synchronous"),
13080 Qnil, Qnil);
13081 if (STRINGP (value)
d5db4077
KR
13082 && (!strcmp (SDATA (value), "true")
13083 || !strcmp (SDATA (value), "on")))
34e23e5a
GM
13084 XSynchronize (dpyinfo->display, True);
13085 }
7d0393cf 13086
60439948
KH
13087 UNBLOCK_INPUT;
13088
7a13e894
RS
13089 return dpyinfo;
13090}
13091\f
13092/* Get rid of display DPYINFO, assuming all frames are already gone,
13093 and without sending any more commands to the X server. */
dc6f92b8 13094
7a13e894
RS
13095void
13096x_delete_display (dpyinfo)
13097 struct x_display_info *dpyinfo;
13098{
13099 delete_keyboard_wait_descriptor (dpyinfo->connection);
13100
13101 /* Discard this display from x_display_name_list and x_display_list.
13102 We can't use Fdelq because that can quit. */
13103 if (! NILP (x_display_name_list)
8e713be6
KR
13104 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
13105 x_display_name_list = XCDR (x_display_name_list);
7a13e894
RS
13106 else
13107 {
13108 Lisp_Object tail;
13109
13110 tail = x_display_name_list;
8e713be6 13111 while (CONSP (tail) && CONSP (XCDR (tail)))
7a13e894 13112 {
bffcfca9 13113 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
7a13e894 13114 {
f3fbd155 13115 XSETCDR (tail, XCDR (XCDR (tail)));
7a13e894
RS
13116 break;
13117 }
8e713be6 13118 tail = XCDR (tail);
7a13e894
RS
13119 }
13120 }
13121
9bda743f
GM
13122 if (next_noop_dpyinfo == dpyinfo)
13123 next_noop_dpyinfo = dpyinfo->next;
13124
7a13e894
RS
13125 if (x_display_list == dpyinfo)
13126 x_display_list = dpyinfo->next;
7f9c7f94
RS
13127 else
13128 {
13129 struct x_display_info *tail;
7a13e894 13130
7f9c7f94
RS
13131 for (tail = x_display_list; tail; tail = tail->next)
13132 if (tail->next == dpyinfo)
13133 tail->next = tail->next->next;
13134 }
7a13e894 13135
0d777288
RS
13136#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
13137#ifndef AIX /* On AIX, XCloseDisplay calls this. */
7f9c7f94
RS
13138 XrmDestroyDatabase (dpyinfo->xrdb);
13139#endif
0d777288 13140#endif
29b38361
KH
13141#ifdef MULTI_KBOARD
13142 if (--dpyinfo->kboard->reference_count == 0)
39f79001 13143 delete_kboard (dpyinfo->kboard);
b9737ad3 13144#endif
f5d11644
GM
13145#ifdef HAVE_X_I18N
13146 if (dpyinfo->xim)
13147 xim_close_dpy (dpyinfo);
13148#endif
7d0393cf 13149
b9737ad3
KH
13150 xfree (dpyinfo->font_table);
13151 xfree (dpyinfo->x_id_name);
f04e1297 13152 xfree (dpyinfo->color_cells);
b9737ad3 13153 xfree (dpyinfo);
7a13e894 13154}
f04e1297 13155
7a13e894
RS
13156\f
13157/* Set up use of X before we make the first connection. */
13158
06a2c219
GM
13159static struct redisplay_interface x_redisplay_interface =
13160{
13161 x_produce_glyphs,
13162 x_write_glyphs,
13163 x_insert_glyphs,
13164 x_clear_end_of_line,
13165 x_scroll_run,
13166 x_after_update_window_line,
13167 x_update_window_begin,
13168 x_update_window_end,
13169 XTcursor_to,
13170 x_flush,
71b8321e 13171 x_clear_mouse_face,
66ac4b0e 13172 x_get_glyph_overhangs,
5958f265 13173 x_fix_overlapping_area,
5da0698e
KS
13174 x_draw_fringe_bitmap,
13175 x_per_char_metric,
13176 x_encode_char,
13177 x_compute_glyph_string_overhangs,
13178 x_draw_glyph_string
06a2c219
GM
13179};
13180
dfcf069d 13181void
7a13e894
RS
13182x_initialize ()
13183{
06a2c219
GM
13184 rif = &x_redisplay_interface;
13185
13186 clear_frame_hook = x_clear_frame;
13187 ins_del_lines_hook = x_ins_del_lines;
06a2c219 13188 delete_glyphs_hook = x_delete_glyphs;
dc6f92b8
JB
13189 ring_bell_hook = XTring_bell;
13190 reset_terminal_modes_hook = XTreset_terminal_modes;
13191 set_terminal_modes_hook = XTset_terminal_modes;
06a2c219
GM
13192 update_begin_hook = x_update_begin;
13193 update_end_hook = x_update_end;
dc6f92b8
JB
13194 set_terminal_window_hook = XTset_terminal_window;
13195 read_socket_hook = XTread_socket;
b8009dd1 13196 frame_up_to_date_hook = XTframe_up_to_date;
90e65f07 13197 mouse_position_hook = XTmouse_position;
f451eb13 13198 frame_rehighlight_hook = XTframe_rehighlight;
dbc4e1c1 13199 frame_raise_lower_hook = XTframe_raise_lower;
ab648270
JB
13200 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13201 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13202 redeem_scroll_bar_hook = XTredeem_scroll_bar;
13203 judge_scroll_bars_hook = XTjudge_scroll_bars;
06a2c219 13204 estimate_mode_line_height_hook = x_estimate_mode_line_height;
58769bee 13205
f676886a 13206 scroll_region_ok = 1; /* we'll scroll partial frames */
9017309f 13207 char_ins_del_ok = 1;
dc6f92b8
JB
13208 line_ins_del_ok = 1; /* we'll just blt 'em */
13209 fast_clear_end_of_line = 1; /* X does this well */
58769bee 13210 memory_below_frame = 0; /* we don't remember what scrolls
dc6f92b8
JB
13211 off the bottom */
13212 baud_rate = 19200;
13213
7a13e894 13214 x_noop_count = 0;
9ea173e8 13215 last_tool_bar_item = -1;
06a2c219 13216 any_help_event_p = 0;
7d0393cf 13217
b30b24cb
RS
13218 /* Try to use interrupt input; if we can't, then start polling. */
13219 Fset_input_mode (Qt, Qnil, Qt, Qnil);
13220
7f9c7f94
RS
13221#ifdef USE_X_TOOLKIT
13222 XtToolkitInitialize ();
651f03b6 13223
7f9c7f94 13224 Xt_app_con = XtCreateApplicationContext ();
7d0393cf 13225
651f03b6
GM
13226 /* Register a converter from strings to pixels, which uses
13227 Emacs' color allocation infrastructure. */
13228 XtAppSetTypeConverter (Xt_app_con,
13229 XtRString, XtRPixel, cvt_string_to_pixel,
13230 cvt_string_to_pixel_args,
13231 XtNumber (cvt_string_to_pixel_args),
13232 XtCacheByDisplay, cvt_pixel_dtor);
13233
665881ad 13234 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
bffcfca9
GM
13235
13236 /* Install an asynchronous timer that processes Xt timeout events
13237 every 0.1s. This is necessary because some widget sets use
13238 timeouts internally, for example the LessTif menu bar, or the
13239 Xaw3d scroll bar. When Xt timouts aren't processed, these
13240 widgets don't behave normally. */
13241 {
13242 EMACS_TIME interval;
13243 EMACS_SET_SECS_USECS (interval, 0, 100000);
13244 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
13245 }
db74249b 13246#endif
7d0393cf 13247
eccc05db 13248#ifdef USE_TOOLKIT_SCROLL_BARS
488dd4c4 13249#ifndef USE_GTK
ec18280f
SM
13250 xaw3d_arrow_scroll = False;
13251 xaw3d_pick_top = True;
488dd4c4 13252#endif
7f9c7f94
RS
13253#endif
13254
58769bee 13255 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 13256 original error handler. */
e99db5a1 13257 XSetErrorHandler (x_error_handler);
334208b7 13258 XSetIOErrorHandler (x_io_error_quitter);
dc6f92b8 13259
06a2c219 13260 /* Disable Window Change signals; they are handled by X events. */
dc6f92b8
JB
13261#ifdef SIGWINCH
13262 signal (SIGWINCH, SIG_DFL);
a59facca 13263#endif /* SIGWINCH */
dc6f92b8 13264
92e2441b 13265 signal (SIGPIPE, x_connection_signal);
5f30b957
JD
13266
13267#ifdef HAVE_X_SM
13268 x_session_initialize ();
13269#endif
dc6f92b8 13270}
55123275 13271
06a2c219 13272
55123275
JB
13273void
13274syms_of_xterm ()
13275{
e99db5a1
RS
13276 staticpro (&x_error_message_string);
13277 x_error_message_string = Qnil;
13278
7a13e894
RS
13279 staticpro (&x_display_name_list);
13280 x_display_name_list = Qnil;
334208b7 13281
ab648270 13282 staticpro (&last_mouse_scroll_bar);
e53cb100 13283 last_mouse_scroll_bar = Qnil;
59e755be
KH
13284
13285 staticpro (&Qvendor_specific_keysyms);
13286 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
2237cac9 13287
c997eae5
SM
13288 staticpro (&Qutf_8);
13289 Qutf_8 = intern ("utf-8");
13290 staticpro (&Qlatin_1);
13291 Qlatin_1 = intern ("latin-1");
13292
2237cac9
RS
13293 staticpro (&last_mouse_press_frame);
13294 last_mouse_press_frame = Qnil;
06a2c219 13295
06a2c219 13296 help_echo = Qnil;
be010514
GM
13297 staticpro (&help_echo);
13298 help_echo_object = Qnil;
13299 staticpro (&help_echo_object);
7cea38bc
GM
13300 help_echo_window = Qnil;
13301 staticpro (&help_echo_window);
06a2c219 13302 previous_help_echo = Qnil;
be010514
GM
13303 staticpro (&previous_help_echo);
13304 help_echo_pos = -1;
06a2c219 13305
ee8ceff8 13306 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
503e457e 13307 doc: /* *Non-nil means autoselect window with mouse pointer. */);
ee8ceff8 13308 mouse_autoselect_window = 0;
503e457e 13309
7ee72033
MB
13310 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
13311 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
228299fa
GM
13312For example, if a block cursor is over a tab, it will be drawn as
13313wide as that tab on the display. */);
06a2c219
GM
13314 x_stretch_cursor_p = 0;
13315
a72d5ce5 13316 DEFVAR_BOOL ("x-use-underline-position-properties",
7ee72033
MB
13317 &x_use_underline_position_properties,
13318 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
f0529b5b 13319nil means ignore them. If you encounter fonts with bogus
228299fa
GM
13320UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
13321to 4.1, set this to nil. */);
a72d5ce5
GM
13322 x_use_underline_position_properties = 1;
13323
7ee72033
MB
13324 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
13325 doc: /* What X toolkit scroll bars Emacs uses.
228299fa
GM
13326A value of nil means Emacs doesn't use X toolkit scroll bars.
13327Otherwise, value is a symbol describing the X toolkit. */);
eccc05db 13328#ifdef USE_TOOLKIT_SCROLL_BARS
5bf04520
GM
13329#ifdef USE_MOTIF
13330 Vx_toolkit_scroll_bars = intern ("motif");
13331#elif defined HAVE_XAW3D
13332 Vx_toolkit_scroll_bars = intern ("xaw3d");
488dd4c4
JD
13333#elif USE_GTK
13334 Vx_toolkit_scroll_bars = intern ("gtk");
5bf04520
GM
13335#else
13336 Vx_toolkit_scroll_bars = intern ("xaw");
13337#endif
06a2c219 13338#else
5bf04520 13339 Vx_toolkit_scroll_bars = Qnil;
06a2c219
GM
13340#endif
13341
06a2c219
GM
13342 staticpro (&last_mouse_motion_frame);
13343 last_mouse_motion_frame = Qnil;
7d0393cf 13344
98659da6
KG
13345 Qmodifier_value = intern ("modifier-value");
13346 Qalt = intern ("alt");
13347 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
13348 Qhyper = intern ("hyper");
13349 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
13350 Qmeta = intern ("meta");
13351 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
13352 Qsuper = intern ("super");
13353 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
7d0393cf 13354
98659da6
KG
13355 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
13356 doc: /* Which keys Emacs uses for the alt modifier.
13357This should be one of the symbols `alt', `hyper', `meta', `super'.
13358For example, `alt' means use the Alt_L and Alt_R keysyms. The default
13359is nil, which is the same as `alt'. */);
13360 Vx_alt_keysym = Qnil;
7d0393cf 13361
98659da6
KG
13362 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
13363 doc: /* Which keys Emacs uses for the hyper modifier.
13364This should be one of the symbols `alt', `hyper', `meta', `super'.
13365For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
13366default is nil, which is the same as `hyper'. */);
13367 Vx_hyper_keysym = Qnil;
7d0393cf 13368
98659da6
KG
13369 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
13370 doc: /* Which keys Emacs uses for the meta modifier.
13371This should be one of the symbols `alt', `hyper', `meta', `super'.
13372For example, `meta' means use the Meta_L and Meta_R keysyms. The
13373default is nil, which is the same as `meta'. */);
13374 Vx_meta_keysym = Qnil;
7d0393cf 13375
98659da6
KG
13376 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
13377 doc: /* Which keys Emacs uses for the super modifier.
13378This should be one of the symbols `alt', `hyper', `meta', `super'.
13379For example, `super' means use the Super_L and Super_R keysyms. The
13380default is nil, which is the same as `super'. */);
13381 Vx_super_keysym = Qnil;
13382
dbd4b028
DL
13383 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
13384 doc: /* Hash table of character codes indexed by X keysym codes. */);
13385 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
13386 make_float (DEFAULT_REHASH_SIZE),
13387 make_float (DEFAULT_REHASH_THRESHOLD),
13388 Qnil, Qnil, Qnil);
55123275 13389}
6cf0ae86 13390
1d6c120a 13391#endif /* HAVE_X_WINDOWS */