Update years in copyright notice; nfc.
[bpt/emacs.git] / src / xterm.c
CommitLineData
dc6f92b8 1/* X Communication module for terminals which understand the X protocol.
043f7f73 2 Copyright (C) 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
0b5538bd 3 2002, 2003, 2004, 2005 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
4fc5845f
LK
19the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20Boston, MA 02110-1301, 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 132#include <X11/Xaw3d/Scrollbar.h>
6ca53601 133#include <X11/Xaw3d/ThreeD.h>
ec18280f
SM
134#else /* !HAVE_XAW3D */
135#include <X11/Xaw/Simple.h>
136#include <X11/Xaw/Scrollbar.h>
137#endif /* !HAVE_XAW3D */
138#ifndef XtNpickTop
139#define XtNpickTop "pickTop"
140#endif /* !XtNpickTop */
141#endif /* !USE_MOTIF */
06a2c219
GM
142#endif /* USE_TOOLKIT_SCROLL_BARS */
143
3afe33e7
RS
144#endif /* USE_X_TOOLKIT */
145
488dd4c4 146#if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
b849c413 147#define x_any_window_to_frame x_window_to_frame
5627c40e 148#define x_top_window_to_frame x_window_to_frame
b849c413
RS
149#endif
150
546e6d5b 151#ifdef USE_X_TOOLKIT
d067ea8b 152#include "widget.h"
546e6d5b
RS
153#ifndef XtNinitialState
154#define XtNinitialState "initialState"
155#endif
156#endif
157
06a2c219
GM
158#define abs(x) ((x) < 0 ? -(x) : (x))
159
51f3cc3b 160/* Default to using XIM if available. */
3ff483be 161#ifdef USE_XIM
51f3cc3b 162int use_xim = 1;
3ff483be
KS
163#else
164int use_xim = 0; /* configure --without-xim */
165#endif
166
06a2c219 167\f
06a2c219 168
5bf04520 169/* Non-nil means Emacs uses toolkit scroll bars. */
06a2c219 170
5bf04520 171Lisp_Object Vx_toolkit_scroll_bars;
06a2c219 172
06a2c219
GM
173/* Non-zero means that a HELP_EVENT has been generated since Emacs
174 start. */
175
176static int any_help_event_p;
177
ee8ceff8 178/* Last window where we saw the mouse. Used by mouse-autoselect-window. */
5bc62483 179static Lisp_Object last_window;
503e457e 180
a72d5ce5
GM
181/* Non-zero means make use of UNDERLINE_POSITION font properties. */
182
183int x_use_underline_position_properties;
184
06a2c219
GM
185/* This is a chain of structures for all the X displays currently in
186 use. */
187
334208b7 188struct x_display_info *x_display_list;
dc6f92b8 189
06a2c219
GM
190/* This is a list of cons cells, each of the form (NAME
191 . FONT-LIST-CACHE), one for each element of x_display_list and in
192 the same order. NAME is the name of the frame. FONT-LIST-CACHE
193 records previous values returned by x-list-fonts. */
194
7a13e894 195Lisp_Object x_display_name_list;
f451eb13 196
987d2ad1 197/* Frame being updated by update_frame. This is declared in term.c.
06a2c219
GM
198 This is set by update_begin and looked at by all the XT functions.
199 It is zero while not inside an update. In that case, the XT
200 functions assume that `selected_frame' is the frame to apply to. */
201
d0386f2a 202extern struct frame *updating_frame;
dc6f92b8 203
06a2c219
GM
204/* This is a frame waiting to be auto-raised, within XTread_socket. */
205
0134a210
RS
206struct frame *pending_autoraise_frame;
207
7f9c7f94
RS
208#ifdef USE_X_TOOLKIT
209/* The application context for Xt use. */
210XtAppContext Xt_app_con;
06a2c219
GM
211static String Xt_default_resources[] = {0};
212#endif /* USE_X_TOOLKIT */
665881ad 213
bffcfca9
GM
214/* Non-zero means user is interacting with a toolkit scroll bar. */
215
216static int toolkit_scroll_bar_interaction;
dc6f92b8 217
0666d82c
KS
218/* Non-zero means to not move point as a result of clicking on a
219 frame to focus it (when focus-follows-mouse is nil). */
220
221int x_mouse_click_focus_ignore_position;
222
223/* Non-zero timeout value means ignore next mouse click if it arrives
224 before that timeout elapses (i.e. as part of the same sequence of
225 events resulting from clicking on a frame to select it). */
226
227static unsigned long ignore_next_mouse_click_timeout;
228
69388238
RS
229/* Mouse movement.
230
06a2c219 231 Formerly, we used PointerMotionHintMask (in standard_event_mask)
f5bb65ec
RS
232 so that we would have to call XQueryPointer after each MotionNotify
233 event to ask for another such event. However, this made mouse tracking
234 slow, and there was a bug that made it eventually stop.
235
236 Simply asking for MotionNotify all the time seems to work better.
237
69388238
RS
238 In order to avoid asking for motion events and then throwing most
239 of them away or busy-polling the server for mouse positions, we ask
240 the server for pointer motion hints. This means that we get only
241 one event per group of mouse movements. "Groups" are delimited by
242 other kinds of events (focus changes and button clicks, for
243 example), or by XQueryPointer calls; when one of these happens, we
244 get another MotionNotify event the next time the mouse moves. This
245 is at least as efficient as getting motion events when mouse
246 tracking is on, and I suspect only negligibly worse when tracking
f5bb65ec 247 is off. */
69388238
RS
248
249/* Where the mouse was last time we reported a mouse event. */
69388238 250
06a2c219 251static XRectangle last_mouse_glyph;
2237cac9
RS
252static Lisp_Object last_mouse_press_frame;
253
69388238
RS
254/* The scroll bar in which the last X motion event occurred.
255
06a2c219
GM
256 If the last X motion event occurred in a scroll bar, we set this so
257 XTmouse_position can know whether to report a scroll bar motion or
69388238
RS
258 an ordinary motion.
259
06a2c219
GM
260 If the last X motion event didn't occur in a scroll bar, we set
261 this to Qnil, to tell XTmouse_position to return an ordinary motion
262 event. */
263
69388238
RS
264static Lisp_Object last_mouse_scroll_bar;
265
69388238
RS
266/* This is a hack. We would really prefer that XTmouse_position would
267 return the time associated with the position it returns, but there
06a2c219 268 doesn't seem to be any way to wrest the time-stamp from the server
69388238
RS
269 along with the position query. So, we just keep track of the time
270 of the last movement we received, and return that in hopes that
271 it's somewhat accurate. */
06a2c219 272
69388238
RS
273static Time last_mouse_movement_time;
274
06a2c219
GM
275/* Incremented by XTread_socket whenever it really tries to read
276 events. */
277
c0a04927
RS
278#ifdef __STDC__
279static int volatile input_signal_count;
280#else
281static int input_signal_count;
282#endif
283
7a13e894 284/* Used locally within XTread_socket. */
06a2c219 285
7a13e894 286static int x_noop_count;
dc6f92b8 287
7a13e894 288/* Initial values of argv and argc. */
06a2c219 289
7a13e894
RS
290extern char **initial_argv;
291extern int initial_argc;
dc6f92b8 292
7a13e894 293extern Lisp_Object Vcommand_line_args, Vsystem_name;
dc6f92b8 294
06a2c219 295/* Tells if a window manager is present or not. */
7a13e894
RS
296
297extern Lisp_Object Vx_no_window_manager;
dc6f92b8 298
83c6eb57 299extern Lisp_Object Qeql;
b8009dd1 300
dc6f92b8
JB
301extern int errno;
302
dfeccd2d 303/* A mask of extra modifier bits to put into every keyboard char. */
06a2c219 304
31ade731 305extern EMACS_INT extra_keyboard_modifiers;
64bb1782 306
98659da6
KG
307/* The keysyms to use for the various modifiers. */
308
309Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
dbd4b028 310Lisp_Object Vx_keysym_table;
98659da6
KG
311static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
312
59e755be 313static Lisp_Object Qvendor_specific_keysyms;
3db9e31e 314static Lisp_Object Qlatin_1;
59e755be 315
952291d9 316extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
66c8f1a8 317extern int x_bitmap_mask P_ ((FRAME_PTR, int));
7a13e894 318
651f03b6 319static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
499b1844 320static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
651f03b6 321static const XColor *x_color_cells P_ ((Display *, int *));
71b8321e 322static void x_update_window_end P_ ((struct window *, int, int));
06a2c219
GM
323void x_delete_display P_ ((struct x_display_info *));
324static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
325 unsigned));
06a2c219
GM
326static int x_io_error_quitter P_ ((Display *));
327int x_catch_errors P_ ((Display *));
328void x_uncatch_errors P_ ((Display *, int));
329void x_lower_frame P_ ((struct frame *));
330void x_scroll_bar_clear P_ ((struct frame *));
331int x_had_errors_p P_ ((Display *));
332void x_wm_set_size_hint P_ ((struct frame *, long, int));
333void x_raise_frame P_ ((struct frame *));
334void x_set_window_size P_ ((struct frame *, int, int, int));
335void x_wm_set_window_state P_ ((struct frame *, int));
336void x_wm_set_icon_pixmap P_ ((struct frame *, int));
337void x_initialize P_ ((void));
338static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
339static int x_compute_min_glyph_bounds P_ ((struct frame *));
06a2c219
GM
340static void x_update_end P_ ((struct frame *));
341static void XTframe_up_to_date P_ ((struct frame *));
06a2c219
GM
342static void XTset_terminal_modes P_ ((void));
343static void XTreset_terminal_modes P_ ((void));
06a2c219 344static void x_clear_frame P_ ((void));
06a2c219
GM
345static void frame_highlight P_ ((struct frame *));
346static void frame_unhighlight P_ ((struct frame *));
347static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
89079179
KS
348static void x_focus_changed P_ ((int, int, struct x_display_info *,
349 struct frame *, struct input_event *));
350static void x_detect_focus_change P_ ((struct x_display_info *,
351 XEvent *, struct input_event *));
06a2c219
GM
352static void XTframe_rehighlight P_ ((struct frame *));
353static void x_frame_rehighlight P_ ((struct x_display_info *));
354static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
65c26775
EZ
355static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
356 enum text_cursor_kinds));
06a2c219 357
08f66682 358static void x_clip_to_row P_ ((struct window *, struct glyph_row *, int, GC));
06a2c219 359static void x_flush P_ ((struct frame *f));
952291d9
GM
360static void x_update_begin P_ ((struct frame *));
361static void x_update_window_begin P_ ((struct window *));
952291d9 362static void x_after_update_window_line P_ ((struct glyph_row *));
810f2256 363static struct scroll_bar *x_window_to_scroll_bar P_ ((Display *, Window));
b52b65bd
GM
364static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
365 enum scroll_bar_part *,
366 Lisp_Object *, Lisp_Object *,
367 unsigned long *));
d0fa3e56 368static void x_check_fullscreen P_ ((struct frame *));
c1f0671a 369static void x_check_expected_move P_ ((struct frame *));
89079179
KS
370static int handle_one_xevent P_ ((struct x_display_info *, XEvent *,
371 int *, struct input_event *));
488dd4c4 372
06a2c219
GM
373
374/* Flush display of frame F, or of all frames if F is null. */
375
376static void
377x_flush (f)
378 struct frame *f;
379{
380 BLOCK_INPUT;
381 if (f == NULL)
382 {
383 Lisp_Object rest, frame;
384 FOR_EACH_FRAME (rest, frame)
385 x_flush (XFRAME (frame));
386 }
387 else if (FRAME_X_P (f))
388 XFlush (FRAME_X_DISPLAY (f));
389 UNBLOCK_INPUT;
390}
391
dc6f92b8 392
06a2c219
GM
393/* Remove calls to XFlush by defining XFlush to an empty replacement.
394 Calls to XFlush should be unnecessary because the X output buffer
395 is flushed automatically as needed by calls to XPending,
396 XNextEvent, or XWindowEvent according to the XFlush man page.
397 XTread_socket calls XPending. Removing XFlush improves
398 performance. */
399
7d0393cf 400#define XFlush(DISPLAY) (void) 0
b8009dd1 401
334208b7 402\f
06a2c219
GM
403/***********************************************************************
404 Debugging
405 ***********************************************************************/
406
9382638d 407#if 0
06a2c219
GM
408
409/* This is a function useful for recording debugging information about
410 the sequence of occurrences in this file. */
9382638d 411
7d0393cf 412struct record
9382638d
KH
413{
414 char *locus;
415 int type;
416};
417
418struct record event_record[100];
419
420int event_record_index;
421
422record_event (locus, type)
423 char *locus;
424 int type;
425{
426 if (event_record_index == sizeof (event_record) / sizeof (struct record))
427 event_record_index = 0;
428
429 event_record[event_record_index].locus = locus;
430 event_record[event_record_index].type = type;
431 event_record_index++;
432}
433
434#endif /* 0 */
06a2c219
GM
435
436
9382638d 437\f
334208b7
RS
438/* Return the struct x_display_info corresponding to DPY. */
439
440struct x_display_info *
441x_display_info_for_display (dpy)
442 Display *dpy;
443{
444 struct x_display_info *dpyinfo;
445
446 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
447 if (dpyinfo->display == dpy)
448 return dpyinfo;
16bd92ea 449
334208b7
RS
450 return 0;
451}
f451eb13 452
06a2c219
GM
453
454\f
455/***********************************************************************
456 Starting and ending an update
457 ***********************************************************************/
7d0393cf 458
06a2c219
GM
459/* Start an update of frame F. This function is installed as a hook
460 for update_begin, i.e. it is called when update_begin is called.
461 This function is called prior to calls to x_update_window_begin for
462 each window being updated. Currently, there is nothing to do here
463 because all interesting stuff is done on a window basis. */
dc6f92b8 464
dfcf069d 465static void
06a2c219 466x_update_begin (f)
f676886a 467 struct frame *f;
58769bee 468{
06a2c219
GM
469 /* Nothing to do. */
470}
dc6f92b8 471
dc6f92b8 472
06a2c219
GM
473/* Start update of window W. Set the global variable updated_window
474 to the window being updated and set output_cursor to the cursor
475 position of W. */
dc6f92b8 476
06a2c219
GM
477static void
478x_update_window_begin (w)
479 struct window *w;
480{
481 struct frame *f = XFRAME (WINDOW_FRAME (w));
482 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
7d0393cf 483
06a2c219
GM
484 updated_window = w;
485 set_output_cursor (&w->cursor);
b8009dd1 486
06a2c219 487 BLOCK_INPUT;
d1bc4182 488
06a2c219 489 if (f == display_info->mouse_face_mouse_frame)
b8009dd1 490 {
514e4681 491 /* Don't do highlighting for mouse motion during the update. */
06a2c219 492 display_info->mouse_face_defer = 1;
37c2c98b 493
06a2c219
GM
494 /* If F needs to be redrawn, simply forget about any prior mouse
495 highlighting. */
9f67f20b 496 if (FRAME_GARBAGED_P (f))
06a2c219
GM
497 display_info->mouse_face_window = Qnil;
498
64f26cf5
GM
499#if 0 /* Rows in a current matrix containing glyphs in mouse-face have
500 their mouse_face_p flag set, which means that they are always
501 unequal to rows in a desired matrix which never have that
502 flag set. So, rows containing mouse-face glyphs are never
503 scrolled, and we don't have to switch the mouse highlight off
504 here to prevent it from being scrolled. */
7d0393cf 505
06a2c219
GM
506 /* Can we tell that this update does not affect the window
507 where the mouse highlight is? If so, no need to turn off.
508 Likewise, don't do anything if the frame is garbaged;
509 in that case, the frame's current matrix that we would use
510 is all wrong, and we will redisplay that line anyway. */
511 if (!NILP (display_info->mouse_face_window)
512 && w == XWINDOW (display_info->mouse_face_window))
514e4681 513 {
06a2c219 514 int i;
514e4681 515
06a2c219
GM
516 for (i = 0; i < w->desired_matrix->nrows; ++i)
517 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
514e4681
RS
518 break;
519
06a2c219
GM
520 if (i < w->desired_matrix->nrows)
521 clear_mouse_face (display_info);
514e4681 522 }
64f26cf5 523#endif /* 0 */
b8009dd1 524 }
6ccf47d1 525
dc6f92b8
JB
526 UNBLOCK_INPUT;
527}
528
06a2c219 529
442a09ea 530/* Draw a vertical window border from (x,y0) to (x,y1) */
06a2c219 531
dfcf069d 532static void
442a09ea 533x_draw_vertical_window_border (w, x, y0, y1)
06a2c219 534 struct window *w;
442a09ea 535 int x, y0, y1;
58769bee 536{
06a2c219 537 struct frame *f = XFRAME (WINDOW_FRAME (w));
7ecc34e9
JL
538 struct face *face;
539
540 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
541 if (face)
542 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
543 face->foreground);
62fe13a4 544
442a09ea
KS
545 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
546 f->output_data.x->normal_gc, x, y0, x, y1);
06a2c219 547}
7d0393cf 548
71b8321e
GM
549/* End update of window W (which is equal to updated_window).
550
551 Draw vertical borders between horizontally adjacent windows, and
552 display W's cursor if CURSOR_ON_P is non-zero.
553
554 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
555 glyphs in mouse-face were overwritten. In that case we have to
556 make sure that the mouse-highlight is properly redrawn.
557
558 W may be a menu bar pseudo-window in case we don't have X toolkit
559 support. Such windows don't have a cursor, so don't display it
560 here. */
06a2c219
GM
561
562static void
71b8321e 563x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
06a2c219 564 struct window *w;
71b8321e 565 int cursor_on_p, mouse_face_overwritten_p;
06a2c219 566{
140330de 567 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7d0393cf 568
06a2c219
GM
569 if (!w->pseudo_window_p)
570 {
571 BLOCK_INPUT;
71b8321e 572
06a2c219 573 if (cursor_on_p)
442a09ea
KS
574 display_and_set_cursor (w, 1, output_cursor.hpos,
575 output_cursor.vpos,
576 output_cursor.x, output_cursor.y);
7d0393cf 577
f94a2622
KS
578 if (draw_window_fringes (w, 1))
579 x_draw_vertical_border (w);
3c0882ae 580
06a2c219
GM
581 UNBLOCK_INPUT;
582 }
7d0393cf 583
140330de
GM
584 /* If a row with mouse-face was overwritten, arrange for
585 XTframe_up_to_date to redisplay the mouse highlight. */
586 if (mouse_face_overwritten_p)
587 {
588 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
589 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
590 dpyinfo->mouse_face_window = Qnil;
591 }
7d0393cf 592
06a2c219
GM
593 updated_window = NULL;
594}
dc6f92b8 595
dc6f92b8 596
06a2c219
GM
597/* End update of frame F. This function is installed as a hook in
598 update_end. */
599
600static void
601x_update_end (f)
602 struct frame *f;
603{
604 /* Mouse highlight may be displayed again. */
aa8bff2e 605 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
b8009dd1 606
442a09ea 607#ifndef XFlush
06a2c219 608 BLOCK_INPUT;
334208b7 609 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 610 UNBLOCK_INPUT;
442a09ea 611#endif
dc6f92b8 612}
b8009dd1 613
06a2c219
GM
614
615/* This function is called from various places in xdisp.c whenever a
616 complete update has been performed. The global variable
617 updated_window is not available here. */
b8009dd1 618
dfcf069d 619static void
b8009dd1 620XTframe_up_to_date (f)
06a2c219 621 struct frame *f;
b8009dd1 622{
06a2c219 623 if (FRAME_X_P (f))
514e4681 624 {
06a2c219 625 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
71b8321e 626
06a2c219
GM
627 if (dpyinfo->mouse_face_deferred_gc
628 || f == dpyinfo->mouse_face_mouse_frame)
629 {
630 BLOCK_INPUT;
631 if (dpyinfo->mouse_face_mouse_frame)
632 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
633 dpyinfo->mouse_face_mouse_x,
634 dpyinfo->mouse_face_mouse_y);
635 dpyinfo->mouse_face_deferred_gc = 0;
636 UNBLOCK_INPUT;
637 }
514e4681 638 }
b8009dd1 639}
06a2c219
GM
640
641
642/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
3f332ef3 643 arrow bitmaps, or clear the fringes if no bitmaps are required
06a2c219
GM
644 before DESIRED_ROW is made current. The window being updated is
645 found in updated_window. This function It is called from
646 update_window_line only if it is known that there are differences
647 between bitmaps to be drawn between current row and DESIRED_ROW. */
648
649static void
650x_after_update_window_line (desired_row)
651 struct glyph_row *desired_row;
652{
653 struct window *w = updated_window;
ef253080 654 struct frame *f;
259cf6bc 655 int width, height;
7d0393cf 656
06a2c219 657 xassert (w);
7d0393cf 658
06a2c219 659 if (!desired_row->mode_line_p && !w->pseudo_window_p)
3c0882ae 660 desired_row->redraw_fringe_bitmaps_p = 1;
7d0393cf 661
ef253080
GM
662 /* When a window has disappeared, make sure that no rest of
663 full-width rows stays visible in the internal border. Could
664 check here if updated_window is the leftmost/rightmost window,
665 but I guess it's not worth doing since vertically split windows
666 are almost never used, internal border is rarely set, and the
667 overhead is very small. */
668 if (windows_or_buffers_changed
669 && desired_row->full_width_p
670 && (f = XFRAME (w->frame),
671 width = FRAME_INTERNAL_BORDER_WIDTH (f),
259cf6bc
GM
672 width != 0)
673 && (height = desired_row->visible_height,
674 height > 0))
ef253080 675 {
ef253080 676 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
06a2c219 677
ef253080
GM
678 /* Internal border is drawn below the tool bar. */
679 if (WINDOWP (f->tool_bar_window)
680 && w == XWINDOW (f->tool_bar_window))
681 y -= width;
7d0393cf 682
ef253080
GM
683 BLOCK_INPUT;
684 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
685 0, y, width, height, False);
686 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
0899d58c 687 FRAME_PIXEL_WIDTH (f) - width,
ef253080 688 y, width, height, False);
06a2c219
GM
689 UNBLOCK_INPUT;
690 }
691}
692
06a2c219 693static void
5958f265 694x_draw_fringe_bitmap (w, row, p)
06a2c219
GM
695 struct window *w;
696 struct glyph_row *row;
5958f265 697 struct draw_fringe_bitmap_params *p;
06a2c219
GM
698{
699 struct frame *f = XFRAME (WINDOW_FRAME (w));
700 Display *display = FRAME_X_DISPLAY (f);
701 Window window = FRAME_X_WINDOW (f);
06a2c219 702 GC gc = f->output_data.x->normal_gc;
5958f265 703 struct face *face = p->face;
3c0882ae 704 int rowY;
06a2c219
GM
705
706 /* Must clip because of partially visible lines. */
3c0882ae
KS
707 rowY = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
708 if (p->y < rowY)
709 {
710 /* Adjust position of "bottom aligned" bitmap on partially
711 visible last row. */
712 int oldY = row->y;
713 int oldVH = row->visible_height;
714 row->visible_height = p->h;
715 row->y -= rowY - p->y;
08f66682 716 x_clip_to_row (w, row, -1, gc);
3c0882ae
KS
717 row->y = oldY;
718 row->visible_height = oldVH;
719 }
720 else
08f66682 721 x_clip_to_row (w, row, -1, gc);
06a2c219 722
fe1a14c1 723 if (p->bx >= 0 && !p->overlay_p)
976b73d7 724 {
976b73d7
KS
725 /* In case the same realized face is used for fringes and
726 for something displayed in the text (e.g. face `region' on
727 mono-displays, the fill style may have been changed to
728 FillSolid in x_draw_glyph_string_background. */
729 if (face->stipple)
730 XSetFillStyle (display, face->gc, FillOpaqueStippled);
731 else
732 XSetForeground (display, face->gc, face->background);
7d0393cf 733
976b73d7 734 XFillRectangle (display, window, face->gc,
5958f265
KS
735 p->bx, p->by, p->nx, p->ny);
736
976b73d7
KS
737 if (!face->stipple)
738 XSetForeground (display, face->gc, face->foreground);
739 }
740
fe1a14c1 741 if (p->which)
dbdc9702 742 {
82b05d81 743 unsigned char *bits;
fe1a14c1 744 Pixmap pixmap, clipmask = (Pixmap) 0;
5958f265 745 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
fe1a14c1 746 XGCValues gcv;
5958f265 747
82b05d81
KS
748 if (p->wd > 8)
749 bits = (unsigned char *)(p->bits + p->dh);
750 else
751 bits = (unsigned char *)p->bits + p->dh;
752
dbdc9702
GM
753 /* Draw the bitmap. I believe these small pixmaps can be cached
754 by the server. */
5958f265 755 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
fe1a14c1
KS
756 (p->cursor_p
757 ? (p->overlay_p ? face->background
758 : f->output_data.x->cursor_pixel)
759 : face->foreground),
dbdc9702 760 face->background, depth);
fe1a14c1
KS
761
762 if (p->overlay_p)
763 {
0666d82c 764 clipmask = XCreatePixmapFromBitmapData (display,
fe1a14c1 765 FRAME_X_DISPLAY_INFO (f)->root_window,
0666d82c 766 bits, p->wd, p->h,
fe1a14c1
KS
767 1, 0, 1);
768 gcv.clip_mask = clipmask;
769 gcv.clip_x_origin = p->x;
0666d82c 770 gcv.clip_y_origin = p->y;
fe1a14c1
KS
771 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
772 }
773
5958f265
KS
774 XCopyArea (display, pixmap, window, gc, 0, 0,
775 p->wd, p->h, p->x, p->y);
dbdc9702 776 XFreePixmap (display, pixmap);
fe1a14c1
KS
777
778 if (p->overlay_p)
779 {
780 gcv.clip_mask = (Pixmap) 0;
781 XChangeGC (display, gc, GCClipMask, &gcv);
782 XFreePixmap (display, clipmask);
783 }
dbdc9702 784 }
7d0393cf 785
06a2c219
GM
786 XSetClipMask (display, gc, None);
787}
788
dc6f92b8 789\f
06a2c219
GM
790
791/* This is called when starting Emacs and when restarting after
792 suspend. When starting Emacs, no X window is mapped. And nothing
793 must be done to Emacs's own window if it is suspended (though that
794 rarely happens). */
dc6f92b8 795
dfcf069d 796static void
dc6f92b8
JB
797XTset_terminal_modes ()
798{
799}
800
06a2c219
GM
801/* This is called when exiting or suspending Emacs. Exiting will make
802 the X-windows go away, and suspending requires no action. */
dc6f92b8 803
dfcf069d 804static void
dc6f92b8
JB
805XTreset_terminal_modes ()
806{
dc6f92b8 807}
06a2c219
GM
808
809
dc6f92b8 810\f
06a2c219
GM
811/***********************************************************************
812 Display Iterator
813 ***********************************************************************/
814
815/* Function prototypes of this page. */
816
5da0698e 817static int x_encode_char P_ ((int, XChar2b *, struct font_info *, int *));
ee569018
KH
818
819
e2ef8ee6
GM
820/* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
821 is not contained in the font. */
dc43ef94 822
5da0698e
KS
823static XCharStruct *
824x_per_char_metric (font, char2b, font_type)
06a2c219
GM
825 XFontStruct *font;
826 XChar2b *char2b;
5da0698e 827 int font_type; /* unused on X */
06a2c219
GM
828{
829 /* The result metric information. */
830 XCharStruct *pcm = NULL;
dc6f92b8 831
06a2c219 832 xassert (font && char2b);
dc6f92b8 833
06a2c219 834 if (font->per_char != NULL)
dc6f92b8 835 {
06a2c219 836 if (font->min_byte1 == 0 && font->max_byte1 == 0)
dc43ef94 837 {
06a2c219
GM
838 /* min_char_or_byte2 specifies the linear character index
839 corresponding to the first element of the per_char array,
840 max_char_or_byte2 is the index of the last character. A
841 character with non-zero CHAR2B->byte1 is not in the font.
842 A character with byte2 less than min_char_or_byte2 or
843 greater max_char_or_byte2 is not in the font. */
844 if (char2b->byte1 == 0
845 && char2b->byte2 >= font->min_char_or_byte2
846 && char2b->byte2 <= font->max_char_or_byte2)
847 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
dc43ef94 848 }
06a2c219 849 else
dc6f92b8 850 {
06a2c219
GM
851 /* If either min_byte1 or max_byte1 are nonzero, both
852 min_char_or_byte2 and max_char_or_byte2 are less than
853 256, and the 2-byte character index values corresponding
854 to the per_char array element N (counting from 0) are:
855
856 byte1 = N/D + min_byte1
857 byte2 = N\D + min_char_or_byte2
858
859 where:
860
861 D = max_char_or_byte2 - min_char_or_byte2 + 1
862 / = integer division
863 \ = integer modulus */
864 if (char2b->byte1 >= font->min_byte1
865 && char2b->byte1 <= font->max_byte1
866 && char2b->byte2 >= font->min_char_or_byte2
867 && char2b->byte2 <= font->max_char_or_byte2)
868 {
869 pcm = (font->per_char
870 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
871 * (char2b->byte1 - font->min_byte1))
872 + (char2b->byte2 - font->min_char_or_byte2));
873 }
dc6f92b8 874 }
06a2c219
GM
875 }
876 else
877 {
878 /* If the per_char pointer is null, all glyphs between the first
879 and last character indexes inclusive have the same
880 information, as given by both min_bounds and max_bounds. */
881 if (char2b->byte2 >= font->min_char_or_byte2
882 && char2b->byte2 <= font->max_char_or_byte2)
883 pcm = &font->max_bounds;
884 }
dc6f92b8 885
ee569018 886 return ((pcm == NULL
3e71d8f2 887 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
ee569018 888 ? NULL : pcm);
06a2c219 889}
b73b6aaf 890
57b03282 891
06a2c219
GM
892/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
893 the two-byte form of C. Encoding is returned in *CHAR2B. */
dc43ef94 894
5da0698e
KS
895static int
896x_encode_char (c, char2b, font_info, two_byte_p)
06a2c219
GM
897 int c;
898 XChar2b *char2b;
899 struct font_info *font_info;
5da0698e 900 int *two_byte_p;
06a2c219
GM
901{
902 int charset = CHAR_CHARSET (c);
903 XFontStruct *font = font_info->font;
904
905 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
906 This may be either a program in a special encoder language or a
907 fixed encoding. */
908 if (font_info->font_encoder)
909 {
910 /* It's a program. */
911 struct ccl_program *ccl = font_info->font_encoder;
912
d8752c9e 913 check_ccl_update (ccl);
06a2c219
GM
914 if (CHARSET_DIMENSION (charset) == 1)
915 {
916 ccl->reg[0] = charset;
917 ccl->reg[1] = char2b->byte2;
6e666629 918 ccl->reg[2] = -1;
06a2c219
GM
919 }
920 else
921 {
922 ccl->reg[0] = charset;
923 ccl->reg[1] = char2b->byte1;
924 ccl->reg[2] = char2b->byte2;
925 }
7d0393cf 926
06a2c219 927 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
7d0393cf 928
06a2c219
GM
929 /* We assume that MSBs are appropriately set/reset by CCL
930 program. */
931 if (font->max_byte1 == 0) /* 1-byte font */
ee569018 932 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
06a2c219
GM
933 else
934 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
935 }
936 else if (font_info->encoding[charset])
937 {
938 /* Fixed encoding scheme. See fontset.h for the meaning of the
939 encoding numbers. */
940 int enc = font_info->encoding[charset];
7d0393cf 941
06a2c219
GM
942 if ((enc == 1 || enc == 2)
943 && CHARSET_DIMENSION (charset) == 2)
944 char2b->byte1 |= 0x80;
7d0393cf 945
06a2c219
GM
946 if (enc == 1 || enc == 3)
947 char2b->byte2 |= 0x80;
948 }
06a2c219 949
ee569018 950 if (two_byte_p)
5da0698e 951 *two_byte_p = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
b4192550 952
5da0698e 953 return FONT_TYPE_UNKNOWN;
06a2c219
GM
954}
955
956
06a2c219
GM
957\f
958/***********************************************************************
959 Glyph display
960 ***********************************************************************/
961
06a2c219 962
06a2c219 963
06a2c219
GM
964static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
965static void x_set_glyph_string_gc P_ ((struct glyph_string *));
966static void x_draw_glyph_string_background P_ ((struct glyph_string *,
967 int));
968static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
b4192550 969static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
06a2c219
GM
970static void x_draw_glyph_string_box P_ ((struct glyph_string *));
971static void x_draw_glyph_string P_ ((struct glyph_string *));
972static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
973static void x_set_cursor_gc P_ ((struct glyph_string *));
974static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
975static void x_set_mouse_face_gc P_ ((struct glyph_string *));
06a2c219 976static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
68c45bf0 977 unsigned long *, double, int));
06a2c219 978static void x_setup_relief_color P_ ((struct frame *, struct relief *,
68c45bf0 979 double, int, unsigned long));
06a2c219
GM
980static void x_setup_relief_colors P_ ((struct glyph_string *));
981static void x_draw_image_glyph_string P_ ((struct glyph_string *));
982static void x_draw_image_relief P_ ((struct glyph_string *));
983static void x_draw_image_foreground P_ ((struct glyph_string *));
984static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
06a2c219
GM
985static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
986 int, int, int));
987static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
57326d99
KS
988 int, int, int, int, int, int,
989 XRectangle *));
06a2c219
GM
990static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
991 int, int, int, XRectangle *));
5da0698e
KS
992
993#if GLYPH_DEBUG
994static void x_check_font P_ ((struct frame *, XFontStruct *));
995#endif
06a2c219
GM
996
997
998/* Set S->gc to a suitable GC for drawing glyph string S in cursor
999 face. */
1000
1001static void
1002x_set_cursor_gc (s)
1003 struct glyph_string *s;
1004{
1005 if (s->font == FRAME_FONT (s->f)
1006 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
1007 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
b4192550 1008 && !s->cmp)
06a2c219
GM
1009 s->gc = s->f->output_data.x->cursor_gc;
1010 else
1011 {
1012 /* Cursor on non-default face: must merge. */
1013 XGCValues xgcv;
1014 unsigned long mask;
1015
1016 xgcv.background = s->f->output_data.x->cursor_pixel;
1017 xgcv.foreground = s->face->background;
1018
1019 /* If the glyph would be invisible, try a different foreground. */
1020 if (xgcv.foreground == xgcv.background)
1021 xgcv.foreground = s->face->foreground;
1022 if (xgcv.foreground == xgcv.background)
1023 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
1024 if (xgcv.foreground == xgcv.background)
1025 xgcv.foreground = s->face->foreground;
1026
1027 /* Make sure the cursor is distinct from text in this face. */
1028 if (xgcv.background == s->face->background
1029 && xgcv.foreground == s->face->foreground)
1030 {
1031 xgcv.background = s->face->foreground;
1032 xgcv.foreground = s->face->background;
1033 }
1034
1035 IF_DEBUG (x_check_font (s->f, s->font));
1036 xgcv.font = s->font->fid;
1037 xgcv.graphics_exposures = False;
1038 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
1039
1040 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1041 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1042 mask, &xgcv);
1043 else
1044 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1045 = XCreateGC (s->display, s->window, mask, &xgcv);
1046
1047 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1048 }
1049}
1050
1051
1052/* Set up S->gc of glyph string S for drawing text in mouse face. */
7d0393cf 1053
06a2c219
GM
1054static void
1055x_set_mouse_face_gc (s)
1056 struct glyph_string *s;
7d0393cf 1057{
06a2c219 1058 int face_id;
ee569018 1059 struct face *face;
06a2c219 1060
e4ded23c 1061 /* What face has to be used last for the mouse face? */
06a2c219 1062 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
ee569018 1063 face = FACE_FROM_ID (s->f, face_id);
e4ded23c
GM
1064 if (face == NULL)
1065 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
7d0393cf 1066
033e3e18
GM
1067 if (s->first_glyph->type == CHAR_GLYPH)
1068 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
1069 else
1070 face_id = FACE_FOR_CHAR (s->f, face, 0);
06a2c219
GM
1071 s->face = FACE_FROM_ID (s->f, face_id);
1072 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1073
1074 /* If font in this face is same as S->font, use it. */
1075 if (s->font == s->face->font)
1076 s->gc = s->face->gc;
1077 else
1078 {
1079 /* Otherwise construct scratch_cursor_gc with values from FACE
1080 but font FONT. */
1081 XGCValues xgcv;
1082 unsigned long mask;
7d0393cf 1083
06a2c219
GM
1084 xgcv.background = s->face->background;
1085 xgcv.foreground = s->face->foreground;
1086 IF_DEBUG (x_check_font (s->f, s->font));
1087 xgcv.font = s->font->fid;
1088 xgcv.graphics_exposures = False;
1089 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
7d0393cf 1090
06a2c219
GM
1091 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1092 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1093 mask, &xgcv);
1094 else
1095 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1096 = XCreateGC (s->display, s->window, mask, &xgcv);
7d0393cf 1097
06a2c219
GM
1098 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1099 }
1100
1101 xassert (s->gc != 0);
1102}
1103
1104
1105/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1106 Faces to use in the mode line have already been computed when the
1107 matrix was built, so there isn't much to do, here. */
1108
1109static INLINE void
1110x_set_mode_line_face_gc (s)
1111 struct glyph_string *s;
7d0393cf 1112{
06a2c219 1113 s->gc = s->face->gc;
06a2c219
GM
1114}
1115
1116
1117/* Set S->gc of glyph string S for drawing that glyph string. Set
1118 S->stippled_p to a non-zero value if the face of S has a stipple
1119 pattern. */
1120
1121static INLINE void
1122x_set_glyph_string_gc (s)
1123 struct glyph_string *s;
1124{
209f68d9 1125 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
7d0393cf 1126
06a2c219
GM
1127 if (s->hl == DRAW_NORMAL_TEXT)
1128 {
1129 s->gc = s->face->gc;
1130 s->stippled_p = s->face->stipple != 0;
1131 }
1132 else if (s->hl == DRAW_INVERSE_VIDEO)
1133 {
1134 x_set_mode_line_face_gc (s);
1135 s->stippled_p = s->face->stipple != 0;
1136 }
1137 else if (s->hl == DRAW_CURSOR)
1138 {
1139 x_set_cursor_gc (s);
1140 s->stippled_p = 0;
1141 }
1142 else if (s->hl == DRAW_MOUSE_FACE)
1143 {
1144 x_set_mouse_face_gc (s);
1145 s->stippled_p = s->face->stipple != 0;
1146 }
1147 else if (s->hl == DRAW_IMAGE_RAISED
1148 || s->hl == DRAW_IMAGE_SUNKEN)
1149 {
1150 s->gc = s->face->gc;
1151 s->stippled_p = s->face->stipple != 0;
1152 }
1153 else
1154 {
1155 s->gc = s->face->gc;
1156 s->stippled_p = s->face->stipple != 0;
1157 }
1158
1159 /* GC must have been set. */
1160 xassert (s->gc != 0);
1161}
1162
1163
06a2c219
GM
1164/* Set clipping for output of glyph string S. S may be part of a mode
1165 line or menu if we don't have X toolkit support. */
1166
1167static INLINE void
1168x_set_glyph_string_clipping (s)
1169 struct glyph_string *s;
1170{
1171 XRectangle r;
442a09ea 1172 get_glyph_string_clip_rect (s, &r);
06a2c219
GM
1173 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
1174}
1175
1176
5da0698e
KS
1177/* RIF:
1178 Compute left and right overhang of glyph string S. If S is a glyph
b4192550 1179 string for a composition, assume overhangs don't exist. */
06a2c219 1180
5da0698e 1181static void
06a2c219
GM
1182x_compute_glyph_string_overhangs (s)
1183 struct glyph_string *s;
1184{
b4192550 1185 if (s->cmp == NULL
06a2c219
GM
1186 && s->first_glyph->type == CHAR_GLYPH)
1187 {
1188 XCharStruct cs;
1189 int direction, font_ascent, font_descent;
1190 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
1191 &font_ascent, &font_descent, &cs);
1192 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
1193 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
1194 }
1195}
1196
1197
06a2c219
GM
1198/* Fill rectangle X, Y, W, H with background color of glyph string S. */
1199
1200static INLINE void
1201x_clear_glyph_string_rect (s, x, y, w, h)
1202 struct glyph_string *s;
1203 int x, y, w, h;
1204{
1205 XGCValues xgcv;
1206 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1207 XSetForeground (s->display, s->gc, xgcv.background);
1208 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1209 XSetForeground (s->display, s->gc, xgcv.foreground);
1210}
1211
1212
1213/* Draw the background of glyph_string S. If S->background_filled_p
1214 is non-zero don't draw it. FORCE_P non-zero means draw the
1215 background even if it wouldn't be drawn normally. This is used
b4192550
KH
1216 when a string preceding S draws into the background of S, or S
1217 contains the first component of a composition. */
06a2c219
GM
1218
1219static void
1220x_draw_glyph_string_background (s, force_p)
1221 struct glyph_string *s;
1222 int force_p;
1223{
1224 /* Nothing to do if background has already been drawn or if it
1225 shouldn't be drawn in the first place. */
1226 if (!s->background_filled_p)
1227 {
ea2ba0d4
KH
1228 int box_line_width = max (s->face->box_line_width, 0);
1229
b4192550 1230 if (s->stippled_p)
06a2c219
GM
1231 {
1232 /* Fill background with a stipple pattern. */
1233 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1234 XFillRectangle (s->display, s->window, s->gc, s->x,
ea2ba0d4 1235 s->y + box_line_width,
06a2c219 1236 s->background_width,
ea2ba0d4 1237 s->height - 2 * box_line_width);
06a2c219
GM
1238 XSetFillStyle (s->display, s->gc, FillSolid);
1239 s->background_filled_p = 1;
1240 }
ea2ba0d4 1241 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
06a2c219
GM
1242 || s->font_not_found_p
1243 || s->extends_to_end_of_line_p
06a2c219
GM
1244 || force_p)
1245 {
ea2ba0d4 1246 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
06a2c219 1247 s->background_width,
ea2ba0d4 1248 s->height - 2 * box_line_width);
06a2c219
GM
1249 s->background_filled_p = 1;
1250 }
1251 }
1252}
1253
1254
1255/* Draw the foreground of glyph string S. */
1256
1257static void
1258x_draw_glyph_string_foreground (s)
1259 struct glyph_string *s;
1260{
1261 int i, x;
1262
1263 /* If first glyph of S has a left box line, start drawing the text
1264 of S to the right of that box line. */
1265 if (s->face->box != FACE_NO_BOX
1266 && s->first_glyph->left_box_line_p)
ea2ba0d4 1267 x = s->x + abs (s->face->box_line_width);
06a2c219
GM
1268 else
1269 x = s->x;
1270
b4192550
KH
1271 /* Draw characters of S as rectangles if S's font could not be
1272 loaded. */
1273 if (s->font_not_found_p)
06a2c219 1274 {
b4192550 1275 for (i = 0; i < s->nchars; ++i)
06a2c219 1276 {
b4192550
KH
1277 struct glyph *g = s->first_glyph + i;
1278 XDrawRectangle (s->display, s->window,
1279 s->gc, x, s->y, g->pixel_width - 1,
1280 s->height - 1);
1281 x += g->pixel_width;
06a2c219
GM
1282 }
1283 }
1284 else
1285 {
b4192550
KH
1286 char *char1b = (char *) s->char2b;
1287 int boff = s->font_info->baseline_offset;
06a2c219 1288
b4192550
KH
1289 if (s->font_info->vertical_centering)
1290 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
1291
1292 /* If we can use 8-bit functions, condense S->char2b. */
1293 if (!s->two_byte_p)
1294 for (i = 0; i < s->nchars; ++i)
1295 char1b[i] = s->char2b[i].byte2;
1296
1297 /* Draw text with XDrawString if background has already been
1298 filled. Otherwise, use XDrawImageString. (Note that
1299 XDrawImageString is usually faster than XDrawString.) Always
1300 use XDrawImageString when drawing the cursor so that there is
1301 no chance that characters under a box cursor are invisible. */
1302 if (s->for_overlaps_p
1303 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1304 {
1305 /* Draw characters with 16-bit or 8-bit functions. */
1306 if (s->two_byte_p)
1307 XDrawString16 (s->display, s->window, s->gc, x,
1308 s->ybase - boff, s->char2b, s->nchars);
1309 else
1310 XDrawString (s->display, s->window, s->gc, x,
1311 s->ybase - boff, char1b, s->nchars);
1312 }
06a2c219
GM
1313 else
1314 {
b4192550
KH
1315 if (s->two_byte_p)
1316 XDrawImageString16 (s->display, s->window, s->gc, x,
1317 s->ybase - boff, s->char2b, s->nchars);
06a2c219 1318 else
b4192550
KH
1319 XDrawImageString (s->display, s->window, s->gc, x,
1320 s->ybase - boff, char1b, s->nchars);
1321 }
fce13449
MB
1322
1323 if (s->face->overstrike)
1324 {
1325 /* For overstriking (to simulate bold-face), draw the
1326 characters again shifted to the right by one pixel. */
1327 if (s->two_byte_p)
1328 XDrawString16 (s->display, s->window, s->gc, x + 1,
1329 s->ybase - boff, s->char2b, s->nchars);
1330 else
1331 XDrawString (s->display, s->window, s->gc, x + 1,
1332 s->ybase - boff, char1b, s->nchars);
1333 }
b4192550
KH
1334 }
1335}
06a2c219 1336
b4192550 1337/* Draw the foreground of composite glyph string S. */
06a2c219 1338
b4192550
KH
1339static void
1340x_draw_composite_glyph_string_foreground (s)
1341 struct glyph_string *s;
1342{
1343 int i, x;
06a2c219 1344
b4192550
KH
1345 /* If first glyph of S has a left box line, start drawing the text
1346 of S to the right of that box line. */
1347 if (s->face->box != FACE_NO_BOX
1348 && s->first_glyph->left_box_line_p)
ea2ba0d4 1349 x = s->x + abs (s->face->box_line_width);
b4192550
KH
1350 else
1351 x = s->x;
06a2c219 1352
b4192550
KH
1353 /* S is a glyph string for a composition. S->gidx is the index of
1354 the first character drawn for glyphs of this composition.
1355 S->gidx == 0 means we are drawing the very first character of
1356 this composition. */
06a2c219 1357
b4192550
KH
1358 /* Draw a rectangle for the composition if the font for the very
1359 first character of the composition could not be loaded. */
1360 if (s->font_not_found_p)
1361 {
1362 if (s->gidx == 0)
1363 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1364 s->width - 1, s->height - 1);
1365 }
1366 else
1367 {
1368 for (i = 0; i < s->nchars; i++, ++s->gidx)
fce13449
MB
1369 {
1370 XDrawString16 (s->display, s->window, s->gc,
1371 x + s->cmp->offsets[s->gidx * 2],
1372 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
1373 s->char2b + i, 1);
1374 if (s->face->overstrike)
1375 XDrawString16 (s->display, s->window, s->gc,
1376 x + s->cmp->offsets[s->gidx * 2] + 1,
1377 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
1378 s->char2b + i, 1);
1379 }
06a2c219
GM
1380 }
1381}
1382
1383
80c32bcc
GM
1384#ifdef USE_X_TOOLKIT
1385
3e71d8f2 1386static struct frame *x_frame_of_widget P_ ((Widget));
651f03b6
GM
1387static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
1388 XrmValue *, XrmValue *, XtPointer *));
1389static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
1390 XrmValue *, Cardinal *));
80c32bcc 1391
3e71d8f2
GM
1392
1393/* Return the frame on which widget WIDGET is used.. Abort if frame
1394 cannot be determined. */
1395
e851c833 1396static struct frame *
3e71d8f2 1397x_frame_of_widget (widget)
80c32bcc 1398 Widget widget;
80c32bcc 1399{
80c32bcc 1400 struct x_display_info *dpyinfo;
5c187dee 1401 Lisp_Object tail;
3e71d8f2 1402 struct frame *f;
7d0393cf 1403
80c32bcc 1404 dpyinfo = x_display_info_for_display (XtDisplay (widget));
7d0393cf 1405
80c32bcc
GM
1406 /* Find the top-level shell of the widget. Note that this function
1407 can be called when the widget is not yet realized, so XtWindow
1408 (widget) == 0. That's the reason we can't simply use
1409 x_any_window_to_frame. */
1410 while (!XtIsTopLevelShell (widget))
1411 widget = XtParent (widget);
1412
1413 /* Look for a frame with that top-level widget. Allocate the color
1414 on that frame to get the right gamma correction value. */
1415 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
1416 if (GC_FRAMEP (XCAR (tail))
1417 && (f = XFRAME (XCAR (tail)),
1418 (f->output_data.nothing != 1
1419 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
1420 && f->output_data.x->widget == widget)
3e71d8f2 1421 return f;
80c32bcc
GM
1422
1423 abort ();
1424}
1425
3e71d8f2
GM
1426
1427/* Allocate the color COLOR->pixel on the screen and display of
1428 widget WIDGET in colormap CMAP. If an exact match cannot be
1429 allocated, try the nearest color available. Value is non-zero
1430 if successful. This is called from lwlib. */
1431
1432int
1433x_alloc_nearest_color_for_widget (widget, cmap, color)
1434 Widget widget;
1435 Colormap cmap;
1436 XColor *color;
1437{
1438 struct frame *f = x_frame_of_widget (widget);
1439 return x_alloc_nearest_color (f, cmap, color);
1440}
1441
1442
46d516e5
MB
1443/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1444 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1445 If this produces the same color as PIXEL, try a color where all RGB
1446 values have DELTA added. Return the allocated color in *PIXEL.
1447 DISPLAY is the X display, CMAP is the colormap to operate on.
1448 Value is non-zero if successful. */
1449
1450int
1451x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
1452 Widget widget;
1453 Display *display;
1454 Colormap cmap;
1455 unsigned long *pixel;
1456 double factor;
1457 int delta;
1458{
1459 struct frame *f = x_frame_of_widget (widget);
1460 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1461}
1462
1463
651f03b6
GM
1464/* Structure specifying which arguments should be passed by Xt to
1465 cvt_string_to_pixel. We want the widget's screen and colormap. */
1466
1467static XtConvertArgRec cvt_string_to_pixel_args[] =
1468 {
1469 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1470 sizeof (Screen *)},
1471 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1472 sizeof (Colormap)}
1473 };
1474
1475
1476/* The address of this variable is returned by
1477 cvt_string_to_pixel. */
1478
1479static Pixel cvt_string_to_pixel_value;
1480
1481
1482/* Convert a color name to a pixel color.
1483
1484 DPY is the display we are working on.
1485
1486 ARGS is an array of *NARGS XrmValue structures holding additional
1487 information about the widget for which the conversion takes place.
1488 The contents of this array are determined by the specification
1489 in cvt_string_to_pixel_args.
1490
1491 FROM is a pointer to an XrmValue which points to the color name to
1492 convert. TO is an XrmValue in which to return the pixel color.
1493
1494 CLOSURE_RET is a pointer to user-data, in which we record if
1495 we allocated the color or not.
1496
1497 Value is True if successful, False otherwise. */
1498
1499static Boolean
1500cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
1501 Display *dpy;
1502 XrmValue *args;
1503 Cardinal *nargs;
1504 XrmValue *from, *to;
1505 XtPointer *closure_ret;
1506{
1507 Screen *screen;
1508 Colormap cmap;
1509 Pixel pixel;
1510 String color_name;
1511 XColor color;
1512
1513 if (*nargs != 2)
1514 {
1515 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1516 "wrongParameters", "cvt_string_to_pixel",
1517 "XtToolkitError",
1518 "Screen and colormap args required", NULL, NULL);
1519 return False;
1520 }
1521
1522 screen = *(Screen **) args[0].addr;
1523 cmap = *(Colormap *) args[1].addr;
1524 color_name = (String) from->addr;
1525
1526 if (strcmp (color_name, XtDefaultBackground) == 0)
1527 {
1528 *closure_ret = (XtPointer) False;
1529 pixel = WhitePixelOfScreen (screen);
1530 }
1531 else if (strcmp (color_name, XtDefaultForeground) == 0)
1532 {
1533 *closure_ret = (XtPointer) False;
1534 pixel = BlackPixelOfScreen (screen);
1535 }
1536 else if (XParseColor (dpy, cmap, color_name, &color)
1537 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1538 {
1539 pixel = color.pixel;
1540 *closure_ret = (XtPointer) True;
1541 }
1542 else
1543 {
1544 String params[1];
1545 Cardinal nparams = 1;
7d0393cf 1546
651f03b6
GM
1547 params[0] = color_name;
1548 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1549 "badValue", "cvt_string_to_pixel",
1550 "XtToolkitError", "Invalid color `%s'",
1551 params, &nparams);
1552 return False;
1553 }
1554
1555 if (to->addr != NULL)
1556 {
1557 if (to->size < sizeof (Pixel))
1558 {
1559 to->size = sizeof (Pixel);
1560 return False;
1561 }
7d0393cf 1562
651f03b6
GM
1563 *(Pixel *) to->addr = pixel;
1564 }
1565 else
1566 {
1567 cvt_string_to_pixel_value = pixel;
1568 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1569 }
7d0393cf 1570
651f03b6
GM
1571 to->size = sizeof (Pixel);
1572 return True;
1573}
1574
1575
1576/* Free a pixel color which was previously allocated via
1577 cvt_string_to_pixel. This is registered as the destructor
1578 for this type of resource via XtSetTypeConverter.
1579
1580 APP is the application context in which we work.
1581
1582 TO is a pointer to an XrmValue holding the color to free.
1583 CLOSURE is the value we stored in CLOSURE_RET for this color
1584 in cvt_string_to_pixel.
1585
1586 ARGS and NARGS are like for cvt_string_to_pixel. */
1587
1588static void
1589cvt_pixel_dtor (app, to, closure, args, nargs)
1590 XtAppContext app;
1591 XrmValuePtr to;
1592 XtPointer closure;
1593 XrmValuePtr args;
1594 Cardinal *nargs;
1595{
1596 if (*nargs != 2)
1597 {
1598 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1599 "XtToolkitError",
1600 "Screen and colormap arguments required",
1601 NULL, NULL);
1602 }
1603 else if (closure != NULL)
1604 {
1605 /* We did allocate the pixel, so free it. */
1606 Screen *screen = *(Screen **) args[0].addr;
1607 Colormap cmap = *(Colormap *) args[1].addr;
1608 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
97762eb7 1609 (Pixel *) to->addr, 1);
651f03b6
GM
1610 }
1611}
1612
1613
80c32bcc
GM
1614#endif /* USE_X_TOOLKIT */
1615
1616
f04e1297 1617/* Value is an array of XColor structures for the contents of the
651f03b6 1618 color map of display DPY. Set *NCELLS to the size of the array.
f04e1297
GM
1619 Note that this probably shouldn't be called for large color maps,
1620 say a 24-bit TrueColor map. */
1621
1622static const XColor *
651f03b6
GM
1623x_color_cells (dpy, ncells)
1624 Display *dpy;
f04e1297
GM
1625 int *ncells;
1626{
651f03b6 1627 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
f04e1297
GM
1628
1629 if (dpyinfo->color_cells == NULL)
1630 {
651f03b6 1631 Screen *screen = dpyinfo->screen;
f04e1297 1632 int i;
7d0393cf 1633
f04e1297 1634 dpyinfo->ncolor_cells
651f03b6 1635 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
f04e1297
GM
1636 dpyinfo->color_cells
1637 = (XColor *) xmalloc (dpyinfo->ncolor_cells
1638 * sizeof *dpyinfo->color_cells);
7d0393cf 1639
f04e1297
GM
1640 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
1641 dpyinfo->color_cells[i].pixel = i;
7d0393cf 1642
651f03b6 1643 XQueryColors (dpy, dpyinfo->cmap,
f04e1297
GM
1644 dpyinfo->color_cells, dpyinfo->ncolor_cells);
1645 }
1646
1647 *ncells = dpyinfo->ncolor_cells;
1648 return dpyinfo->color_cells;
1649}
1650
1651
1652/* On frame F, translate pixel colors to RGB values for the NCOLORS
1653 colors in COLORS. Use cached information, if available. */
1654
1655void
1656x_query_colors (f, colors, ncolors)
1657 struct frame *f;
1658 XColor *colors;
1659 int ncolors;
1660{
1661 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1662
1663 if (dpyinfo->color_cells)
1664 {
1665 int i;
1666 for (i = 0; i < ncolors; ++i)
1667 {
1668 unsigned long pixel = colors[i].pixel;
1669 xassert (pixel < dpyinfo->ncolor_cells);
1670 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
1671 colors[i] = dpyinfo->color_cells[pixel];
1672 }
1673 }
1674 else
1675 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1676}
1677
1678
1679/* On frame F, translate pixel color to RGB values for the color in
1680 COLOR. Use cached information, if available. */
1681
1682void
1683x_query_color (f, color)
1684 struct frame *f;
1685 XColor *color;
1686{
1687 x_query_colors (f, color, 1);
1688}
7d0393cf 1689
f04e1297 1690
651f03b6
GM
1691/* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1692 exact match can't be allocated, try the nearest color available.
1693 Value is non-zero if successful. Set *COLOR to the color
1694 allocated. */
06a2c219 1695
651f03b6
GM
1696static int
1697x_alloc_nearest_color_1 (dpy, cmap, color)
1698 Display *dpy;
06a2c219
GM
1699 Colormap cmap;
1700 XColor *color;
1701{
80c32bcc
GM
1702 int rc;
1703
651f03b6 1704 rc = XAllocColor (dpy, cmap, color);
06a2c219
GM
1705 if (rc == 0)
1706 {
1707 /* If we got to this point, the colormap is full, so we're going
1708 to try to get the next closest color. The algorithm used is
1709 a least-squares matching, which is what X uses for closest
1710 color matching with StaticColor visuals. */
1711 int nearest, i;
1712 unsigned long nearest_delta = ~0;
f04e1297 1713 int ncells;
651f03b6 1714 const XColor *cells = x_color_cells (dpy, &ncells);
06a2c219
GM
1715
1716 for (nearest = i = 0; i < ncells; ++i)
1717 {
1718 long dred = (color->red >> 8) - (cells[i].red >> 8);
1719 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
1720 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1721 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
1722
1723 if (delta < nearest_delta)
1724 {
1725 nearest = i;
1726 nearest_delta = delta;
1727 }
1728 }
7d0393cf 1729
06a2c219
GM
1730 color->red = cells[nearest].red;
1731 color->green = cells[nearest].green;
1732 color->blue = cells[nearest].blue;
651f03b6 1733 rc = XAllocColor (dpy, cmap, color);
06a2c219 1734 }
35efe0a1
GM
1735 else
1736 {
1737 /* If allocation succeeded, and the allocated pixel color is not
1738 equal to a cached pixel color recorded earlier, there was a
1739 change in the colormap, so clear the color cache. */
651f03b6 1740 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
35efe0a1 1741 XColor *cached_color;
7d0393cf 1742
35efe0a1
GM
1743 if (dpyinfo->color_cells
1744 && (cached_color = &dpyinfo->color_cells[color->pixel],
cae71efe
GM
1745 (cached_color->red != color->red
1746 || cached_color->blue != color->blue
1747 || cached_color->green != color->green)))
35efe0a1
GM
1748 {
1749 xfree (dpyinfo->color_cells);
1750 dpyinfo->color_cells = NULL;
1751 dpyinfo->ncolor_cells = 0;
1752 }
1753 }
06a2c219 1754
d9c545da
GM
1755#ifdef DEBUG_X_COLORS
1756 if (rc)
1757 register_color (color->pixel);
1758#endif /* DEBUG_X_COLORS */
7d0393cf 1759
06a2c219
GM
1760 return rc;
1761}
1762
1763
651f03b6
GM
1764/* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1765 exact match can't be allocated, try the nearest color available.
1766 Value is non-zero if successful. Set *COLOR to the color
1767 allocated. */
1768
1769int
1770x_alloc_nearest_color (f, cmap, color)
1771 struct frame *f;
1772 Colormap cmap;
1773 XColor *color;
1774{
1775 gamma_correct (f, color);
1776 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1777}
1778
1779
d9c545da
GM
1780/* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1781 It's necessary to do this instead of just using PIXEL directly to
1782 get color reference counts right. */
1783
1784unsigned long
1785x_copy_color (f, pixel)
1786 struct frame *f;
1787 unsigned long pixel;
1788{
1789 XColor color;
1790
1791 color.pixel = pixel;
1792 BLOCK_INPUT;
f04e1297 1793 x_query_color (f, &color);
d9c545da
GM
1794 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1795 UNBLOCK_INPUT;
1796#ifdef DEBUG_X_COLORS
1797 register_color (pixel);
1798#endif
1799 return color.pixel;
1800}
1801
1802
3e71d8f2
GM
1803/* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
1804 It's necessary to do this instead of just using PIXEL directly to
1805 get color reference counts right. */
1806
1807unsigned long
1808x_copy_dpy_color (dpy, cmap, pixel)
1809 Display *dpy;
1810 Colormap cmap;
1811 unsigned long pixel;
1812{
1813 XColor color;
1814
1815 color.pixel = pixel;
1816 BLOCK_INPUT;
1817 XQueryColor (dpy, cmap, &color);
1818 XAllocColor (dpy, cmap, &color);
1819 UNBLOCK_INPUT;
1820#ifdef DEBUG_X_COLORS
1821 register_color (pixel);
1822#endif
1823 return color.pixel;
1824}
1825
1826
6d8b0acd 1827/* Brightness beyond which a color won't have its highlight brightness
d7361edf 1828 boosted.
6d8b0acd 1829
d7361edf
MB
1830 Nominally, highlight colors for `3d' faces are calculated by
1831 brightening an object's color by a constant scale factor, but this
1832 doesn't yield good results for dark colors, so for colors who's
1833 brightness is less than this value (on a scale of 0-65535) have an
1834 use an additional additive factor.
6d8b0acd
MB
1835
1836 The value here is set so that the default menu-bar/mode-line color
1837 (grey75) will not have its highlights changed at all. */
1838#define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1839
1840
06a2c219
GM
1841/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1842 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1843 If this produces the same color as PIXEL, try a color where all RGB
1844 values have DELTA added. Return the allocated color in *PIXEL.
1845 DISPLAY is the X display, CMAP is the colormap to operate on.
1846 Value is non-zero if successful. */
1847
1848static int
1849x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
1850 struct frame *f;
1851 Display *display;
1852 Colormap cmap;
1853 unsigned long *pixel;
68c45bf0 1854 double factor;
06a2c219
GM
1855 int delta;
1856{
1857 XColor color, new;
6d8b0acd 1858 long bright;
06a2c219
GM
1859 int success_p;
1860
1861 /* Get RGB color values. */
1862 color.pixel = *pixel;
f04e1297 1863 x_query_color (f, &color);
06a2c219
GM
1864
1865 /* Change RGB values by specified FACTOR. Avoid overflow! */
1866 xassert (factor >= 0);
1867 new.red = min (0xffff, factor * color.red);
1868 new.green = min (0xffff, factor * color.green);
1869 new.blue = min (0xffff, factor * color.blue);
1870
d7361edf
MB
1871 /* Calculate brightness of COLOR. */
1872 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
6d8b0acd
MB
1873
1874 /* We only boost colors that are darker than
1875 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1876 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1877 /* Make an additive adjustment to NEW, because it's dark enough so
1878 that scaling by FACTOR alone isn't enough. */
1879 {
1880 /* How far below the limit this color is (0 - 1, 1 being darker). */
1881 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1882 /* The additive adjustment. */
d7361edf 1883 int min_delta = delta * dimness * factor / 2;
6d8b0acd
MB
1884
1885 if (factor < 1)
1886 {
6d8b0acd
MB
1887 new.red = max (0, new.red - min_delta);
1888 new.green = max (0, new.green - min_delta);
1889 new.blue = max (0, new.blue - min_delta);
1890 }
1891 else
1892 {
1893 new.red = min (0xffff, min_delta + new.red);
1894 new.green = min (0xffff, min_delta + new.green);
1895 new.blue = min (0xffff, min_delta + new.blue);
1896 }
1897 }
1898
06a2c219 1899 /* Try to allocate the color. */
80c32bcc 1900 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
1901 if (success_p)
1902 {
1903 if (new.pixel == *pixel)
1904 {
1905 /* If we end up with the same color as before, try adding
1906 delta to the RGB values. */
0d605c67 1907 x_free_colors (f, &new.pixel, 1);
7d0393cf 1908
06a2c219
GM
1909 new.red = min (0xffff, delta + color.red);
1910 new.green = min (0xffff, delta + color.green);
1911 new.blue = min (0xffff, delta + color.blue);
80c32bcc 1912 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
1913 }
1914 else
1915 success_p = 1;
1916 *pixel = new.pixel;
1917 }
7d0393cf 1918
06a2c219
GM
1919 return success_p;
1920}
1921
1922
1923/* Set up the foreground color for drawing relief lines of glyph
1924 string S. RELIEF is a pointer to a struct relief containing the GC
1925 with which lines will be drawn. Use a color that is FACTOR or
1926 DELTA lighter or darker than the relief's background which is found
1927 in S->f->output_data.x->relief_background. If such a color cannot
1928 be allocated, use DEFAULT_PIXEL, instead. */
7d0393cf 1929
06a2c219
GM
1930static void
1931x_setup_relief_color (f, relief, factor, delta, default_pixel)
1932 struct frame *f;
1933 struct relief *relief;
68c45bf0 1934 double factor;
06a2c219
GM
1935 int delta;
1936 unsigned long default_pixel;
1937{
1938 XGCValues xgcv;
1939 struct x_output *di = f->output_data.x;
1940 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
1941 unsigned long pixel;
1942 unsigned long background = di->relief_background;
43bd1b2b 1943 Colormap cmap = FRAME_X_COLORMAP (f);
dcd08bfb
GM
1944 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1945 Display *dpy = FRAME_X_DISPLAY (f);
06a2c219
GM
1946
1947 xgcv.graphics_exposures = False;
1948 xgcv.line_width = 1;
1949
1950 /* Free previously allocated color. The color cell will be reused
1951 when it has been freed as many times as it was allocated, so this
1952 doesn't affect faces using the same colors. */
1953 if (relief->gc
1954 && relief->allocated_p)
1955 {
0d605c67 1956 x_free_colors (f, &relief->pixel, 1);
06a2c219
GM
1957 relief->allocated_p = 0;
1958 }
1959
1960 /* Allocate new color. */
1961 xgcv.foreground = default_pixel;
1962 pixel = background;
dcd08bfb
GM
1963 if (dpyinfo->n_planes != 1
1964 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
06a2c219
GM
1965 {
1966 relief->allocated_p = 1;
1967 xgcv.foreground = relief->pixel = pixel;
1968 }
7d0393cf 1969
06a2c219
GM
1970 if (relief->gc == 0)
1971 {
dcd08bfb 1972 xgcv.stipple = dpyinfo->gray;
06a2c219 1973 mask |= GCStipple;
dcd08bfb 1974 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
06a2c219
GM
1975 }
1976 else
dcd08bfb 1977 XChangeGC (dpy, relief->gc, mask, &xgcv);
06a2c219
GM
1978}
1979
1980
1981/* Set up colors for the relief lines around glyph string S. */
1982
1983static void
1984x_setup_relief_colors (s)
1985 struct glyph_string *s;
1986{
1987 struct x_output *di = s->f->output_data.x;
1988 unsigned long color;
1989
1990 if (s->face->use_box_color_for_shadows_p)
1991 color = s->face->box_color;
e2a57b34 1992 else if (s->first_glyph->type == IMAGE_GLYPH
0cb8bb48 1993 && s->img->pixmap
e2a57b34
MB
1994 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
1995 color = IMAGE_BACKGROUND (s->img, s->f, 0);
06a2c219
GM
1996 else
1997 {
1998 XGCValues xgcv;
7d0393cf 1999
06a2c219
GM
2000 /* Get the background color of the face. */
2001 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2002 color = xgcv.background;
2003 }
2004
2005 if (di->white_relief.gc == 0
2006 || color != di->relief_background)
2007 {
2008 di->relief_background = color;
2009 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2010 WHITE_PIX_DEFAULT (s->f));
2011 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2012 BLACK_PIX_DEFAULT (s->f));
2013 }
2014}
2015
2016
2017/* Draw a relief on frame F inside the rectangle given by LEFT_X,
2018 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2019 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
2020 relief. LEFT_P non-zero means draw a relief on the left side of
2021 the rectangle. RIGHT_P non-zero means draw a relief on the right
2022 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2023 when drawing. */
2024
2025static void
2026x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
57326d99 2027 raised_p, top_p, bot_p, left_p, right_p, clip_rect)
06a2c219 2028 struct frame *f;
57326d99
KS
2029 int left_x, top_y, right_x, bottom_y, width;
2030 int top_p, bot_p, left_p, right_p, raised_p;
06a2c219
GM
2031 XRectangle *clip_rect;
2032{
de507556
GM
2033 Display *dpy = FRAME_X_DISPLAY (f);
2034 Window window = FRAME_X_WINDOW (f);
06a2c219
GM
2035 int i;
2036 GC gc;
7d0393cf 2037
06a2c219
GM
2038 if (raised_p)
2039 gc = f->output_data.x->white_relief.gc;
2040 else
2041 gc = f->output_data.x->black_relief.gc;
de507556 2042 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
06a2c219
GM
2043
2044 /* Top. */
57326d99
KS
2045 if (top_p)
2046 for (i = 0; i < width; ++i)
2047 XDrawLine (dpy, window, gc,
2048 left_x + i * left_p, top_y + i,
2049 right_x + 1 - i * right_p, top_y + i);
06a2c219
GM
2050
2051 /* Left. */
2052 if (left_p)
2053 for (i = 0; i < width; ++i)
de507556 2054 XDrawLine (dpy, window, gc,
44655e77 2055 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
06a2c219 2056
de507556 2057 XSetClipMask (dpy, gc, None);
06a2c219
GM
2058 if (raised_p)
2059 gc = f->output_data.x->black_relief.gc;
2060 else
2061 gc = f->output_data.x->white_relief.gc;
de507556 2062 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
7d0393cf 2063
06a2c219 2064 /* Bottom. */
57326d99
KS
2065 if (bot_p)
2066 for (i = 0; i < width; ++i)
2067 XDrawLine (dpy, window, gc,
2068 left_x + i * left_p, bottom_y - i,
2069 right_x + 1 - i * right_p, bottom_y - i);
7d0393cf 2070
06a2c219
GM
2071 /* Right. */
2072 if (right_p)
2073 for (i = 0; i < width; ++i)
de507556 2074 XDrawLine (dpy, window, gc,
06a2c219
GM
2075 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
2076
de507556 2077 XSetClipMask (dpy, gc, None);
06a2c219
GM
2078}
2079
2080
2081/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2082 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2083 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
2084 left side of the rectangle. RIGHT_P non-zero means draw a line
2085 on the right side of the rectangle. CLIP_RECT is the clipping
2086 rectangle to use when drawing. */
2087
2088static void
2089x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2090 left_p, right_p, clip_rect)
2091 struct glyph_string *s;
0971e730 2092 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
06a2c219
GM
2093 XRectangle *clip_rect;
2094{
2095 XGCValues xgcv;
7d0393cf 2096
06a2c219
GM
2097 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2098 XSetForeground (s->display, s->gc, s->face->box_color);
2099 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
7d0393cf 2100
06a2c219
GM
2101 /* Top. */
2102 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 2103 left_x, top_y, right_x - left_x + 1, width);
06a2c219
GM
2104
2105 /* Left. */
2106 if (left_p)
2107 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 2108 left_x, top_y, width, bottom_y - top_y + 1);
06a2c219
GM
2109
2110 /* Bottom. */
2111 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 2112 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
7d0393cf 2113
06a2c219
GM
2114 /* Right. */
2115 if (right_p)
2116 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 2117 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
06a2c219
GM
2118
2119 XSetForeground (s->display, s->gc, xgcv.foreground);
2120 XSetClipMask (s->display, s->gc, None);
2121}
2122
2123
2124/* Draw a box around glyph string S. */
2125
2126static void
2127x_draw_glyph_string_box (s)
2128 struct glyph_string *s;
2129{
2130 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2131 int left_p, right_p;
2132 struct glyph *last_glyph;
2133 XRectangle clip_rect;
2134
82ead4b1
KS
2135 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2136 ? WINDOW_RIGHT_EDGE_X (s->w)
2137 : window_box_right (s->w, s->area));
7d0393cf 2138
06a2c219 2139 /* The glyph that may have a right box line. */
b4192550 2140 last_glyph = (s->cmp || s->img
06a2c219
GM
2141 ? s->first_glyph
2142 : s->first_glyph + s->nchars - 1);
2143
ea2ba0d4 2144 width = abs (s->face->box_line_width);
06a2c219
GM
2145 raised_p = s->face->box == FACE_RAISED_BOX;
2146 left_x = s->x;
57ac7c81
GM
2147 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2148 ? last_x - 1
2149 : min (last_x, s->x + s->background_width) - 1);
06a2c219
GM
2150 top_y = s->y;
2151 bottom_y = top_y + s->height - 1;
2152
2153 left_p = (s->first_glyph->left_box_line_p
2154 || (s->hl == DRAW_MOUSE_FACE
2155 && (s->prev == NULL
2156 || s->prev->hl != s->hl)));
2157 right_p = (last_glyph->right_box_line_p
2158 || (s->hl == DRAW_MOUSE_FACE
2159 && (s->next == NULL
2160 || s->next->hl != s->hl)));
327f42ee 2161
442a09ea 2162 get_glyph_string_clip_rect (s, &clip_rect);
06a2c219
GM
2163
2164 if (s->face->box == FACE_SIMPLE_BOX)
2165 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2166 left_p, right_p, &clip_rect);
2167 else
2168 {
2169 x_setup_relief_colors (s);
2170 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
57326d99 2171 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
06a2c219
GM
2172 }
2173}
2174
2175
2176/* Draw foreground of image glyph string S. */
2177
2178static void
2179x_draw_image_foreground (s)
2180 struct glyph_string *s;
2181{
57326d99
KS
2182 int x = s->x;
2183 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
06a2c219
GM
2184
2185 /* If first glyph of S has a left box line, start drawing it to the
2186 right of that line. */
2187 if (s->face->box != FACE_NO_BOX
57326d99
KS
2188 && s->first_glyph->left_box_line_p
2189 && s->slice.x == 0)
2190 x += abs (s->face->box_line_width);
06a2c219
GM
2191
2192 /* If there is a margin around the image, adjust x- and y-position
2193 by that margin. */
57326d99
KS
2194 if (s->slice.x == 0)
2195 x += s->img->hmargin;
2196 if (s->slice.y == 0)
2197 y += s->img->vmargin;
06a2c219
GM
2198
2199 if (s->img->pixmap)
2200 {
2201 if (s->img->mask)
2202 {
2203 /* We can't set both a clip mask and use XSetClipRectangles
2204 because the latter also sets a clip mask. We also can't
2205 trust on the shape extension to be available
2206 (XShapeCombineRegion). So, compute the rectangle to draw
2207 manually. */
2208 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2209 | GCFunction);
2210 XGCValues xgcv;
2211 XRectangle clip_rect, image_rect, r;
2212
2213 xgcv.clip_mask = s->img->mask;
2214 xgcv.clip_x_origin = x;
2215 xgcv.clip_y_origin = y;
2216 xgcv.function = GXcopy;
2217 XChangeGC (s->display, s->gc, mask, &xgcv);
7d0393cf 2218
442a09ea 2219 get_glyph_string_clip_rect (s, &clip_rect);
06a2c219
GM
2220 image_rect.x = x;
2221 image_rect.y = y;
57326d99
KS
2222 image_rect.width = s->slice.width;
2223 image_rect.height = s->slice.height;
06a2c219
GM
2224 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2225 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
57326d99
KS
2226 s->slice.x + r.x - x, s->slice.y + r.y - y,
2227 r.width, r.height, r.x, r.y);
06a2c219
GM
2228 }
2229 else
2230 {
49ad1d99
GM
2231 XRectangle clip_rect, image_rect, r;
2232
442a09ea 2233 get_glyph_string_clip_rect (s, &clip_rect);
49ad1d99
GM
2234 image_rect.x = x;
2235 image_rect.y = y;
57326d99
KS
2236 image_rect.width = s->slice.width;
2237 image_rect.height = s->slice.height;
49ad1d99
GM
2238 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2239 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
57326d99
KS
2240 s->slice.x + r.x - x, s->slice.y + r.y - y,
2241 r.width, r.height, r.x, r.y);
7d0393cf 2242
06a2c219
GM
2243 /* When the image has a mask, we can expect that at
2244 least part of a mouse highlight or a block cursor will
2245 be visible. If the image doesn't have a mask, make
2246 a block cursor visible by drawing a rectangle around
2247 the image. I believe it's looking better if we do
2248 nothing here for mouse-face. */
2249 if (s->hl == DRAW_CURSOR)
534c20b2
KS
2250 {
2251 int r = s->img->relief;
2252 if (r < 0) r = -r;
57326d99
KS
2253 XDrawRectangle (s->display, s->window, s->gc,
2254 x - r, y - r,
2255 s->slice.width + r*2 - 1,
2256 s->slice.height + r*2 - 1);
534c20b2 2257 }
06a2c219
GM
2258 }
2259 }
2260 else
2261 /* Draw a rectangle if image could not be loaded. */
2262 XDrawRectangle (s->display, s->window, s->gc, x, y,
57326d99 2263 s->slice.width - 1, s->slice.height - 1);
06a2c219
GM
2264}
2265
2266
2267/* Draw a relief around the image glyph string S. */
2268
2269static void
2270x_draw_image_relief (s)
2271 struct glyph_string *s;
2272{
2273 int x0, y0, x1, y1, thick, raised_p;
2274 XRectangle r;
57326d99
KS
2275 int x = s->x;
2276 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
7d0393cf 2277
06a2c219
GM
2278 /* If first glyph of S has a left box line, start drawing it to the
2279 right of that line. */
2280 if (s->face->box != FACE_NO_BOX
57326d99
KS
2281 && s->first_glyph->left_box_line_p
2282 && s->slice.x == 0)
2283 x += abs (s->face->box_line_width);
7d0393cf 2284
06a2c219
GM
2285 /* If there is a margin around the image, adjust x- and y-position
2286 by that margin. */
57326d99
KS
2287 if (s->slice.x == 0)
2288 x += s->img->hmargin;
2289 if (s->slice.y == 0)
2290 y += s->img->vmargin;
7d0393cf 2291
06a2c219
GM
2292 if (s->hl == DRAW_IMAGE_SUNKEN
2293 || s->hl == DRAW_IMAGE_RAISED)
2294 {
62854fe2 2295 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
06a2c219
GM
2296 raised_p = s->hl == DRAW_IMAGE_RAISED;
2297 }
2298 else
2299 {
2300 thick = abs (s->img->relief);
2301 raised_p = s->img->relief > 0;
2302 }
7d0393cf 2303
06a2c219
GM
2304 x0 = x - thick;
2305 y0 = y - thick;
57326d99
KS
2306 x1 = x + s->slice.width + thick - 1;
2307 y1 = y + s->slice.height + thick - 1;
7d0393cf 2308
06a2c219 2309 x_setup_relief_colors (s);
442a09ea 2310 get_glyph_string_clip_rect (s, &r);
57326d99
KS
2311 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
2312 s->slice.y == 0,
2313 s->slice.y + s->slice.height == s->img->height,
2314 s->slice.x == 0,
2315 s->slice.x + s->slice.width == s->img->width,
2316 &r);
06a2c219
GM
2317}
2318
2319
2320/* Draw the foreground of image glyph string S to PIXMAP. */
2321
2322static void
2323x_draw_image_foreground_1 (s, pixmap)
2324 struct glyph_string *s;
2325 Pixmap pixmap;
2326{
57326d99
KS
2327 int x = 0;
2328 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
06a2c219
GM
2329
2330 /* If first glyph of S has a left box line, start drawing it to the
2331 right of that line. */
2332 if (s->face->box != FACE_NO_BOX
57326d99
KS
2333 && s->first_glyph->left_box_line_p
2334 && s->slice.x == 0)
2335 x += abs (s->face->box_line_width);
06a2c219
GM
2336
2337 /* If there is a margin around the image, adjust x- and y-position
2338 by that margin. */
57326d99
KS
2339 if (s->slice.x == 0)
2340 x += s->img->hmargin;
2341 if (s->slice.y == 0)
2342 y += s->img->vmargin;
dc43ef94 2343
06a2c219
GM
2344 if (s->img->pixmap)
2345 {
2346 if (s->img->mask)
2347 {
2348 /* We can't set both a clip mask and use XSetClipRectangles
2349 because the latter also sets a clip mask. We also can't
2350 trust on the shape extension to be available
2351 (XShapeCombineRegion). So, compute the rectangle to draw
2352 manually. */
2353 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2354 | GCFunction);
2355 XGCValues xgcv;
2356
2357 xgcv.clip_mask = s->img->mask;
95911c74
JD
2358 xgcv.clip_x_origin = x - s->slice.x;
2359 xgcv.clip_y_origin = y - s->slice.y;
06a2c219
GM
2360 xgcv.function = GXcopy;
2361 XChangeGC (s->display, s->gc, mask, &xgcv);
2362
2363 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
57326d99
KS
2364 s->slice.x, s->slice.y,
2365 s->slice.width, s->slice.height, x, y);
06a2c219
GM
2366 XSetClipMask (s->display, s->gc, None);
2367 }
2368 else
2369 {
2370 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
57326d99
KS
2371 s->slice.x, s->slice.y,
2372 s->slice.width, s->slice.height, x, y);
7d0393cf 2373
06a2c219
GM
2374 /* When the image has a mask, we can expect that at
2375 least part of a mouse highlight or a block cursor will
2376 be visible. If the image doesn't have a mask, make
2377 a block cursor visible by drawing a rectangle around
2378 the image. I believe it's looking better if we do
2379 nothing here for mouse-face. */
2380 if (s->hl == DRAW_CURSOR)
534c20b2
KS
2381 {
2382 int r = s->img->relief;
2383 if (r < 0) r = -r;
2384 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
57326d99
KS
2385 s->slice.width + r*2 - 1,
2386 s->slice.height + r*2 - 1);
534c20b2 2387 }
06a2c219
GM
2388 }
2389 }
2390 else
2391 /* Draw a rectangle if image could not be loaded. */
2392 XDrawRectangle (s->display, pixmap, s->gc, x, y,
57326d99 2393 s->slice.width - 1, s->slice.height - 1);
06a2c219 2394}
dc43ef94 2395
990ba854 2396
06a2c219
GM
2397/* Draw part of the background of glyph string S. X, Y, W, and H
2398 give the rectangle to draw. */
a9a5b0a5 2399
06a2c219
GM
2400static void
2401x_draw_glyph_string_bg_rect (s, x, y, w, h)
2402 struct glyph_string *s;
2403 int x, y, w, h;
2404{
2405 if (s->stippled_p)
2406 {
2407 /* Fill background with a stipple pattern. */
2408 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2409 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2410 XSetFillStyle (s->display, s->gc, FillSolid);
2411 }
2412 else
2413 x_clear_glyph_string_rect (s, x, y, w, h);
2414}
07e34cb0 2415
b5210ea7 2416
7d0393cf 2417/* Draw image glyph string S.
dc43ef94 2418
06a2c219
GM
2419 s->y
2420 s->x +-------------------------
2421 | s->face->box
2422 |
2423 | +-------------------------
2424 | | s->img->margin
2425 | |
2426 | | +-------------------
2427 | | | the image
dc43ef94 2428
06a2c219 2429 */
dc43ef94 2430
06a2c219
GM
2431static void
2432x_draw_image_glyph_string (s)
2433 struct glyph_string *s;
2434{
ea2ba0d4
KH
2435 int box_line_hwidth = abs (s->face->box_line_width);
2436 int box_line_vwidth = max (s->face->box_line_width, 0);
06a2c219
GM
2437 int height;
2438 Pixmap pixmap = None;
2439
57326d99
KS
2440 height = s->height;
2441 if (s->slice.y == 0)
2442 height -= box_line_vwidth;
2443 if (s->slice.y + s->slice.height >= s->img->height)
2444 height -= box_line_vwidth;
488dd4c4 2445
06a2c219
GM
2446 /* Fill background with face under the image. Do it only if row is
2447 taller than image or if image has a clip mask to reduce
2448 flickering. */
2449 s->stippled_p = s->face->stipple != 0;
57326d99 2450 if (height > s->slice.height
22d650b8
GM
2451 || s->img->hmargin
2452 || s->img->vmargin
06a2c219
GM
2453 || s->img->mask
2454 || s->img->pixmap == 0
2455 || s->width != s->background_width)
2456 {
06a2c219
GM
2457 if (s->img->mask)
2458 {
f9b5db02
GM
2459 /* Create a pixmap as large as the glyph string. Fill it
2460 with the background color. Copy the image to it, using
2461 its mask. Copy the temporary pixmap to the display. */
06a2c219
GM
2462 Screen *screen = FRAME_X_SCREEN (s->f);
2463 int depth = DefaultDepthOfScreen (screen);
2464
2465 /* Create a pixmap as large as the glyph string. */
2466 pixmap = XCreatePixmap (s->display, s->window,
2467 s->background_width,
2468 s->height, depth);
7d0393cf 2469
06a2c219
GM
2470 /* Don't clip in the following because we're working on the
2471 pixmap. */
2472 XSetClipMask (s->display, s->gc, None);
2473
2474 /* Fill the pixmap with the background color/stipple. */
2475 if (s->stippled_p)
2476 {
2477 /* Fill background with a stipple pattern. */
2478 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2479 XFillRectangle (s->display, pixmap, s->gc,
2480 0, 0, s->background_width, s->height);
2481 XSetFillStyle (s->display, s->gc, FillSolid);
2482 }
2483 else
2484 {
2485 XGCValues xgcv;
2486 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2487 &xgcv);
2488 XSetForeground (s->display, s->gc, xgcv.background);
2489 XFillRectangle (s->display, pixmap, s->gc,
2490 0, 0, s->background_width, s->height);
2491 XSetForeground (s->display, s->gc, xgcv.foreground);
2492 }
2493 }
2494 else
57326d99
KS
2495 {
2496 int x = s->x;
2497 int y = s->y;
2498
2499 if (s->first_glyph->left_box_line_p
2500 && s->slice.x == 0)
2501 x += box_line_hwidth;
2502
2503 if (s->slice.y == 0)
2504 y += box_line_vwidth;
2505
2506 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2507 }
7d0393cf 2508
06a2c219
GM
2509 s->background_filled_p = 1;
2510 }
dc43ef94 2511
06a2c219
GM
2512 /* Draw the foreground. */
2513 if (pixmap != None)
2514 {
2515 x_draw_image_foreground_1 (s, pixmap);
2516 x_set_glyph_string_clipping (s);
2517 XCopyArea (s->display, pixmap, s->window, s->gc,
2518 0, 0, s->background_width, s->height, s->x, s->y);
2519 XFreePixmap (s->display, pixmap);
2520 }
2521 else
2522 x_draw_image_foreground (s);
b5210ea7 2523
06a2c219
GM
2524 /* If we must draw a relief around the image, do it. */
2525 if (s->img->relief
2526 || s->hl == DRAW_IMAGE_RAISED
2527 || s->hl == DRAW_IMAGE_SUNKEN)
2528 x_draw_image_relief (s);
2529}
8c1a6a84 2530
990ba854 2531
06a2c219 2532/* Draw stretch glyph string S. */
dc43ef94 2533
06a2c219
GM
2534static void
2535x_draw_stretch_glyph_string (s)
2536 struct glyph_string *s;
2537{
2538 xassert (s->first_glyph->type == STRETCH_GLYPH);
2539 s->stippled_p = s->face->stipple != 0;
990ba854 2540
06a2c219
GM
2541 if (s->hl == DRAW_CURSOR
2542 && !x_stretch_cursor_p)
2543 {
2544 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2545 as wide as the stretch glyph. */
0899d58c 2546 int width = min (FRAME_COLUMN_WIDTH (s->f), s->background_width);
990ba854 2547
06a2c219
GM
2548 /* Draw cursor. */
2549 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
0cdd0c9f 2550
06a2c219
GM
2551 /* Clear rest using the GC of the original non-cursor face. */
2552 if (width < s->background_width)
2553 {
06a2c219
GM
2554 int x = s->x + width, y = s->y;
2555 int w = s->background_width - width, h = s->height;
2556 XRectangle r;
b7f83f9e 2557 GC gc;
dc43ef94 2558
b7f83f9e
GM
2559 if (s->row->mouse_face_p
2560 && cursor_in_mouse_face_p (s->w))
2561 {
2562 x_set_mouse_face_gc (s);
2563 gc = s->gc;
2564 }
2565 else
2566 gc = s->face->gc;
7d0393cf 2567
442a09ea 2568 get_glyph_string_clip_rect (s, &r);
06a2c219 2569 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
7d0393cf 2570
06a2c219
GM
2571 if (s->face->stipple)
2572 {
2573 /* Fill background with a stipple pattern. */
2574 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2575 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2576 XSetFillStyle (s->display, gc, FillSolid);
2577 }
2578 else
2579 {
2580 XGCValues xgcv;
2581 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2582 XSetForeground (s->display, gc, xgcv.background);
2583 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2584 XSetForeground (s->display, gc, xgcv.foreground);
2585 }
2586 }
2587 }
61e9f9f3 2588 else if (!s->background_filled_p)
06a2c219
GM
2589 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
2590 s->height);
7d0393cf 2591
06a2c219
GM
2592 s->background_filled_p = 1;
2593}
2594
2595
2596/* Draw glyph string S. */
2597
2598static void
2599x_draw_glyph_string (s)
2600 struct glyph_string *s;
2601{
4458cf11
KH
2602 int relief_drawn_p = 0;
2603
06a2c219
GM
2604 /* If S draws into the background of its successor, draw the
2605 background of the successor first so that S can draw into it.
2606 This makes S->next use XDrawString instead of XDrawImageString. */
66ac4b0e 2607 if (s->next && s->right_overhang && !s->for_overlaps_p)
06a2c219
GM
2608 {
2609 xassert (s->next->img == NULL);
2610 x_set_glyph_string_gc (s->next);
2611 x_set_glyph_string_clipping (s->next);
2612 x_draw_glyph_string_background (s->next, 1);
2613 }
97210f4e 2614
06a2c219
GM
2615 /* Set up S->gc, set clipping and draw S. */
2616 x_set_glyph_string_gc (s);
06a2c219 2617
4458cf11
KH
2618 /* Draw relief (if any) in advance for char/composition so that the
2619 glyph string can be drawn over it. */
2620 if (!s->for_overlaps_p
2621 && s->face->box != FACE_NO_BOX
2622 && (s->first_glyph->type == CHAR_GLYPH
2623 || s->first_glyph->type == COMPOSITE_GLYPH))
2624
2625 {
e6269cbb 2626 x_set_glyph_string_clipping (s);
4458cf11
KH
2627 x_draw_glyph_string_background (s, 1);
2628 x_draw_glyph_string_box (s);
e6269cbb 2629 x_set_glyph_string_clipping (s);
4458cf11
KH
2630 relief_drawn_p = 1;
2631 }
e6269cbb
GM
2632 else
2633 x_set_glyph_string_clipping (s);
4458cf11 2634
06a2c219
GM
2635 switch (s->first_glyph->type)
2636 {
2637 case IMAGE_GLYPH:
2638 x_draw_image_glyph_string (s);
2639 break;
2640
2641 case STRETCH_GLYPH:
2642 x_draw_stretch_glyph_string (s);
2643 break;
2644
2645 case CHAR_GLYPH:
66ac4b0e
GM
2646 if (s->for_overlaps_p)
2647 s->background_filled_p = 1;
2648 else
2649 x_draw_glyph_string_background (s, 0);
06a2c219
GM
2650 x_draw_glyph_string_foreground (s);
2651 break;
2652
b4192550
KH
2653 case COMPOSITE_GLYPH:
2654 if (s->for_overlaps_p || s->gidx > 0)
2655 s->background_filled_p = 1;
2656 else
2657 x_draw_glyph_string_background (s, 1);
2658 x_draw_composite_glyph_string_foreground (s);
2659 break;
2660
06a2c219
GM
2661 default:
2662 abort ();
2663 }
2664
66ac4b0e 2665 if (!s->for_overlaps_p)
06a2c219 2666 {
66ac4b0e
GM
2667 /* Draw underline. */
2668 if (s->face->underline_p)
2669 {
e24e84cc
GM
2670 unsigned long tem, h;
2671 int y;
06a2c219 2672
e24e84cc 2673 /* Get the underline thickness. Default is 1 pixel. */
66ac4b0e
GM
2674 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
2675 h = 1;
e24e84cc
GM
2676
2677 /* Get the underline position. This is the recommended
2678 vertical offset in pixels from the baseline to the top of
2679 the underline. This is a signed value according to the
2680 specs, and its default is
2681
2682 ROUND ((maximum descent) / 2), with
2683 ROUND(x) = floor (x + 0.5) */
7d0393cf 2684
a72d5ce5
GM
2685 if (x_use_underline_position_properties
2686 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
e24e84cc
GM
2687 y = s->ybase + (long) tem;
2688 else if (s->face->font)
2689 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
2690 else
a02f1be0 2691 y = s->y + s->height - h;
7d0393cf 2692
66ac4b0e 2693 if (s->face->underline_defaulted_p)
e24e84cc
GM
2694 XFillRectangle (s->display, s->window, s->gc,
2695 s->x, y, s->width, h);
66ac4b0e
GM
2696 else
2697 {
2698 XGCValues xgcv;
2699 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2700 XSetForeground (s->display, s->gc, s->face->underline_color);
e24e84cc
GM
2701 XFillRectangle (s->display, s->window, s->gc,
2702 s->x, y, s->width, h);
66ac4b0e
GM
2703 XSetForeground (s->display, s->gc, xgcv.foreground);
2704 }
dc6f92b8 2705 }
07e34cb0 2706
66ac4b0e
GM
2707 /* Draw overline. */
2708 if (s->face->overline_p)
06a2c219 2709 {
66ac4b0e
GM
2710 unsigned long dy = 0, h = 1;
2711
2712 if (s->face->overline_color_defaulted_p)
2713 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2714 s->width, h);
2715 else
2716 {
2717 XGCValues xgcv;
2718 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2719 XSetForeground (s->display, s->gc, s->face->overline_color);
2720 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2721 s->width, h);
2722 XSetForeground (s->display, s->gc, xgcv.foreground);
2723 }
06a2c219 2724 }
7d0393cf 2725
66ac4b0e
GM
2726 /* Draw strike-through. */
2727 if (s->face->strike_through_p)
06a2c219 2728 {
66ac4b0e
GM
2729 unsigned long h = 1;
2730 unsigned long dy = (s->height - h) / 2;
2731
2732 if (s->face->strike_through_color_defaulted_p)
2733 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2734 s->width, h);
2735 else
2736 {
2737 XGCValues xgcv;
2738 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2739 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2740 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2741 s->width, h);
2742 XSetForeground (s->display, s->gc, xgcv.foreground);
2743 }
06a2c219 2744 }
7d0393cf 2745
4458cf11
KH
2746 /* Draw relief if not yet drawn. */
2747 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
66ac4b0e
GM
2748 x_draw_glyph_string_box (s);
2749 }
7d0393cf 2750
06a2c219
GM
2751 /* Reset clipping. */
2752 XSetClipMask (s->display, s->gc, None);
dc6f92b8 2753}
07e34cb0 2754
442a09ea 2755/* Shift display to make room for inserted glyphs. */
06a2c219 2756
442a09ea
KS
2757void
2758x_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
2759 struct frame *f;
2760 int x, y, width, height, shift_by;
06a2c219 2761{
06a2c219
GM
2762 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2763 f->output_data.x->normal_gc,
442a09ea
KS
2764 x, y, width, height,
2765 x + shift_by, y);
0cdd0c9f 2766}
0cdd0c9f 2767
06a2c219
GM
2768/* Delete N glyphs at the nominal cursor position. Not implemented
2769 for X frames. */
c83febd7
RS
2770
2771static void
06a2c219
GM
2772x_delete_glyphs (n)
2773 register int n;
c83febd7 2774{
06a2c219 2775 abort ();
c83febd7
RS
2776}
2777
0cdd0c9f 2778
c5e6e06b
GM
2779/* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2780 If they are <= 0, this is probably an error. */
2781
2782void
2783x_clear_area (dpy, window, x, y, width, height, exposures)
2784 Display *dpy;
2785 Window window;
2786 int x, y;
2787 int width, height;
2788 int exposures;
2789{
2790 xassert (width > 0 && height > 0);
2791 XClearArea (dpy, window, x, y, width, height, exposures);
2792}
2793
2794
06a2c219 2795/* Clear entire frame. If updating_frame is non-null, clear that
b86bd3dd 2796 frame. Otherwise clear the selected frame. */
06a2c219
GM
2797
2798static void
2799x_clear_frame ()
0cdd0c9f 2800{
06a2c219 2801 struct frame *f;
0cdd0c9f 2802
06a2c219
GM
2803 if (updating_frame)
2804 f = updating_frame;
0cdd0c9f 2805 else
b86bd3dd 2806 f = SELECTED_FRAME ();
58769bee 2807
06a2c219
GM
2808 /* Clearing the frame will erase any cursor, so mark them all as no
2809 longer visible. */
2810 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2811 output_cursor.hpos = output_cursor.vpos = 0;
2812 output_cursor.x = -1;
2813
2814 /* We don't set the output cursor here because there will always
2815 follow an explicit cursor_to. */
2816 BLOCK_INPUT;
2817 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2818
2819 /* We have to clear the scroll bars, too. If we have changed
2820 colors or something like that, then they should be notified. */
2821 x_scroll_bar_clear (f);
0cdd0c9f 2822
06a2c219 2823 XFlush (FRAME_X_DISPLAY (f));
0cb35f4e 2824
06a2c219 2825 UNBLOCK_INPUT;
dc6f92b8 2826}
06a2c219
GM
2827
2828
dc6f92b8 2829\f
dbc4e1c1
JB
2830/* Invert the middle quarter of the frame for .15 sec. */
2831
06a2c219
GM
2832/* We use the select system call to do the waiting, so we have to make
2833 sure it's available. If it isn't, we just won't do visual bells. */
2834
dbc4e1c1
JB
2835#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
2836
06a2c219
GM
2837
2838/* Subtract the `struct timeval' values X and Y, storing the result in
2839 *RESULT. Return 1 if the difference is negative, otherwise 0. */
dbc4e1c1
JB
2840
2841static int
2842timeval_subtract (result, x, y)
2843 struct timeval *result, x, y;
2844{
06a2c219
GM
2845 /* Perform the carry for the later subtraction by updating y. This
2846 is safer because on some systems the tv_sec member is unsigned. */
dbc4e1c1
JB
2847 if (x.tv_usec < y.tv_usec)
2848 {
2849 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
2850 y.tv_usec -= 1000000 * nsec;
2851 y.tv_sec += nsec;
2852 }
7d0393cf 2853
dbc4e1c1
JB
2854 if (x.tv_usec - y.tv_usec > 1000000)
2855 {
2856 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
2857 y.tv_usec += 1000000 * nsec;
2858 y.tv_sec -= nsec;
2859 }
2860
06a2c219
GM
2861 /* Compute the time remaining to wait. tv_usec is certainly
2862 positive. */
dbc4e1c1
JB
2863 result->tv_sec = x.tv_sec - y.tv_sec;
2864 result->tv_usec = x.tv_usec - y.tv_usec;
2865
06a2c219
GM
2866 /* Return indication of whether the result should be considered
2867 negative. */
dbc4e1c1
JB
2868 return x.tv_sec < y.tv_sec;
2869}
dc6f92b8 2870
dfcf069d 2871void
f676886a
JB
2872XTflash (f)
2873 struct frame *f;
dc6f92b8 2874{
dbc4e1c1 2875 BLOCK_INPUT;
dc6f92b8 2876
dbc4e1c1
JB
2877 {
2878 GC gc;
dc6f92b8 2879
06a2c219
GM
2880 /* Create a GC that will use the GXxor function to flip foreground
2881 pixels into background pixels. */
dbc4e1c1
JB
2882 {
2883 XGCValues values;
dc6f92b8 2884
dbc4e1c1 2885 values.function = GXxor;
7556890b
RS
2886 values.foreground = (f->output_data.x->foreground_pixel
2887 ^ f->output_data.x->background_pixel);
58769bee 2888
334208b7 2889 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dbc4e1c1
JB
2890 GCFunction | GCForeground, &values);
2891 }
dc6f92b8 2892
dbc4e1c1 2893 {
e84e14c3 2894 /* Get the height not including a menu bar widget. */
0899d58c 2895 int height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, FRAME_LINES (f));
e84e14c3
RS
2896 /* Height of each line to flash. */
2897 int flash_height = FRAME_LINE_HEIGHT (f);
2898 /* These will be the left and right margins of the rectangles. */
2899 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
0899d58c 2900 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
e84e14c3
RS
2901
2902 int width;
2903
2904 /* Don't flash the area between a scroll bar and the frame
2905 edge it is next to. */
2906 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
2907 {
2908 case vertical_scroll_bar_left:
2909 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2910 break;
2911
2912 case vertical_scroll_bar_right:
2913 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2914 break;
06a2c219
GM
2915
2916 default:
2917 break;
e84e14c3
RS
2918 }
2919
2920 width = flash_right - flash_left;
2921
2922 /* If window is tall, flash top and bottom line. */
2923 if (height > 3 * FRAME_LINE_HEIGHT (f))
2924 {
2925 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
2926 flash_left,
2927 (FRAME_INTERNAL_BORDER_WIDTH (f)
0899d58c 2928 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
e84e14c3
RS
2929 width, flash_height);
2930 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2931 flash_left,
2932 (height - flash_height
2933 - FRAME_INTERNAL_BORDER_WIDTH (f)),
2934 width, flash_height);
2935 }
2936 else
7d0393cf 2937 /* If it is short, flash it all. */
e84e14c3
RS
2938 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2939 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
2940 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
dc6f92b8 2941
06a2c219 2942 x_flush (f);
dc6f92b8 2943
dbc4e1c1 2944 {
06a2c219 2945 struct timeval wakeup;
dc6f92b8 2946
66c30ea1 2947 EMACS_GET_TIME (wakeup);
dc6f92b8 2948
dbc4e1c1
JB
2949 /* Compute time to wait until, propagating carry from usecs. */
2950 wakeup.tv_usec += 150000;
2951 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
2952 wakeup.tv_usec %= 1000000;
2953
101922c3
GM
2954 /* Keep waiting until past the time wakeup or any input gets
2955 available. */
2956 while (! detect_input_pending ())
dbc4e1c1 2957 {
101922c3 2958 struct timeval current;
dbc4e1c1
JB
2959 struct timeval timeout;
2960
101922c3 2961 EMACS_GET_TIME (current);
dbc4e1c1 2962
101922c3
GM
2963 /* Break if result would be negative. */
2964 if (timeval_subtract (&current, wakeup, current))
dbc4e1c1
JB
2965 break;
2966
101922c3
GM
2967 /* How long `select' should wait. */
2968 timeout.tv_sec = 0;
2969 timeout.tv_usec = 10000;
2970
dbc4e1c1 2971 /* Try to wait that long--but we might wake up sooner. */
c32cdd9a 2972 select (0, NULL, NULL, NULL, &timeout);
dbc4e1c1
JB
2973 }
2974 }
58769bee 2975
e84e14c3
RS
2976 /* If window is tall, flash top and bottom line. */
2977 if (height > 3 * FRAME_LINE_HEIGHT (f))
2978 {
2979 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
2980 flash_left,
2981 (FRAME_INTERNAL_BORDER_WIDTH (f)
0899d58c 2982 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
e84e14c3
RS
2983 width, flash_height);
2984 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2985 flash_left,
2986 (height - flash_height
2987 - FRAME_INTERNAL_BORDER_WIDTH (f)),
2988 width, flash_height);
2989 }
2990 else
7d0393cf 2991 /* If it is short, flash it all. */
e84e14c3
RS
2992 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2993 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
2994 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
2995
334208b7 2996 XFreeGC (FRAME_X_DISPLAY (f), gc);
06a2c219 2997 x_flush (f);
dc6f92b8 2998 }
dbc4e1c1
JB
2999 }
3000
3001 UNBLOCK_INPUT;
dc6f92b8
JB
3002}
3003
06a2c219 3004#endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
dbc4e1c1
JB
3005
3006
dc6f92b8
JB
3007/* Make audible bell. */
3008
dfcf069d 3009void
dc6f92b8
JB
3010XTring_bell ()
3011{
b86bd3dd 3012 struct frame *f = SELECTED_FRAME ();
7d0393cf 3013
b86bd3dd
GM
3014 if (FRAME_X_DISPLAY (f))
3015 {
dbc4e1c1 3016#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
b86bd3dd
GM
3017 if (visible_bell)
3018 XTflash (f);
3019 else
dbc4e1c1 3020#endif
b86bd3dd
GM
3021 {
3022 BLOCK_INPUT;
3023 XBell (FRAME_X_DISPLAY (f), 0);
3024 XFlush (FRAME_X_DISPLAY (f));
3025 UNBLOCK_INPUT;
3026 }
dc6f92b8
JB
3027 }
3028}
06a2c219 3029
dc6f92b8 3030\f
06a2c219
GM
3031/* Specify how many text lines, from the top of the window,
3032 should be affected by insert-lines and delete-lines operations.
3033 This, and those operations, are used only within an update
3034 that is bounded by calls to x_update_begin and x_update_end. */
dc6f92b8 3035
dfcf069d 3036static void
06a2c219
GM
3037XTset_terminal_window (n)
3038 register int n;
dc6f92b8 3039{
06a2c219 3040 /* This function intentionally left blank. */
dc6f92b8
JB
3041}
3042
06a2c219
GM
3043
3044\f
3045/***********************************************************************
3046 Line Dance
3047 ***********************************************************************/
3048
3049/* Perform an insert-lines or delete-lines operation, inserting N
3050 lines or deleting -N lines at vertical position VPOS. */
3051
dfcf069d 3052static void
06a2c219
GM
3053x_ins_del_lines (vpos, n)
3054 int vpos, n;
dc6f92b8
JB
3055{
3056 abort ();
3057}
06a2c219
GM
3058
3059
3060/* Scroll part of the display as described by RUN. */
dc6f92b8 3061
dfcf069d 3062static void
06a2c219
GM
3063x_scroll_run (w, run)
3064 struct window *w;
3065 struct run *run;
dc6f92b8 3066{
06a2c219
GM
3067 struct frame *f = XFRAME (w->frame);
3068 int x, y, width, height, from_y, to_y, bottom_y;
3069
3070 /* Get frame-relative bounding box of the text display area of W,
3f332ef3
KS
3071 without mode lines. Include in this box the left and right
3072 fringe of W. */
06a2c219 3073 window_box (w, -1, &x, &y, &width, &height);
06a2c219
GM
3074
3075 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3076 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3077 bottom_y = y + height;
dc6f92b8 3078
06a2c219
GM
3079 if (to_y < from_y)
3080 {
3081 /* Scrolling up. Make sure we don't copy part of the mode
3082 line at the bottom. */
3083 if (from_y + run->height > bottom_y)
3084 height = bottom_y - from_y;
3085 else
3086 height = run->height;
3087 }
dc6f92b8 3088 else
06a2c219
GM
3089 {
3090 /* Scolling down. Make sure we don't copy over the mode line.
3091 at the bottom. */
3092 if (to_y + run->height > bottom_y)
3093 height = bottom_y - to_y;
3094 else
3095 height = run->height;
3096 }
7a13e894 3097
06a2c219 3098 BLOCK_INPUT;
7d0393cf 3099
06a2c219
GM
3100 /* Cursor off. Will be switched on again in x_update_window_end. */
3101 updated_window = w;
3102 x_clear_cursor (w);
3103
3104 XCopyArea (FRAME_X_DISPLAY (f),
3105 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3106 f->output_data.x->normal_gc,
3107 x, from_y,
3108 width, height,
3109 x, to_y);
7d0393cf 3110
06a2c219
GM
3111 UNBLOCK_INPUT;
3112}
dc6f92b8 3113
dc6f92b8 3114
06a2c219
GM
3115\f
3116/***********************************************************************
3117 Exposure Events
3118 ***********************************************************************/
7d0393cf 3119
442a09ea 3120\f
06a2c219 3121static void
442a09ea 3122frame_highlight (f)
06a2c219 3123 struct frame *f;
dc6f92b8 3124{
442a09ea
KS
3125 /* We used to only do this if Vx_no_window_manager was non-nil, but
3126 the ICCCM (section 4.1.6) says that the window's border pixmap
3127 and border pixel are window attributes which are "private to the
3128 client", so we can always change it to whatever we want. */
3129 BLOCK_INPUT;
3130 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3131 f->output_data.x->border_pixel);
3132 UNBLOCK_INPUT;
3133 x_update_cursor (f, 1);
3134}
dc6f92b8 3135
442a09ea
KS
3136static void
3137frame_unhighlight (f)
3138 struct frame *f;
3139{
3140 /* We used to only do this if Vx_no_window_manager was non-nil, but
3141 the ICCCM (section 4.1.6) says that the window's border pixmap
3142 and border pixel are window attributes which are "private to the
3143 client", so we can always change it to whatever we want. */
3144 BLOCK_INPUT;
3145 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3146 f->output_data.x->border_tile);
3147 UNBLOCK_INPUT;
3148 x_update_cursor (f, 1);
3149}
dc6f92b8 3150
442a09ea
KS
3151/* The focus has changed. Update the frames as necessary to reflect
3152 the new situation. Note that we can't change the selected frame
3153 here, because the Lisp code we are interrupting might become confused.
3154 Each event gets marked with the frame in which it occurred, so the
3155 Lisp code can tell when the switch took place by examining the events. */
06a2c219 3156
442a09ea
KS
3157static void
3158x_new_focus_frame (dpyinfo, frame)
3159 struct x_display_info *dpyinfo;
3160 struct frame *frame;
3161{
3162 struct frame *old_focus = dpyinfo->x_focus_frame;
06a2c219 3163
442a09ea 3164 if (frame != dpyinfo->x_focus_frame)
dc6f92b8 3165 {
442a09ea
KS
3166 /* Set this before calling other routines, so that they see
3167 the correct value of x_focus_frame. */
3168 dpyinfo->x_focus_frame = frame;
06a2c219 3169
442a09ea
KS
3170 if (old_focus && old_focus->auto_lower)
3171 x_lower_frame (old_focus);
06a2c219 3172
442a09ea
KS
3173#if 0
3174 selected_frame = frame;
3175 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
3176 selected_frame);
f1321dc3 3177 Fselect_window (selected_frame->selected_window, Qnil);
442a09ea
KS
3178 choose_minibuf_frame ();
3179#endif /* ! 0 */
82f053ab 3180
442a09ea
KS
3181 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3182 pending_autoraise_frame = dpyinfo->x_focus_frame;
3183 else
3184 pending_autoraise_frame = 0;
82f053ab 3185 }
dc6f92b8 3186
442a09ea
KS
3187 x_frame_rehighlight (dpyinfo);
3188}
06a2c219 3189
442a09ea
KS
3190/* Handle FocusIn and FocusOut state changes for FRAME.
3191 If FRAME has focus and there exists more than one frame, puts
89079179 3192 a FOCUS_IN_EVENT into *BUFP. */
cab34f50 3193
89079179
KS
3194static void
3195x_focus_changed (type, state, dpyinfo, frame, bufp)
cab34f50
JD
3196 int type;
3197 int state;
3198 struct x_display_info *dpyinfo;
3199 struct frame *frame;
3200 struct input_event *bufp;
cab34f50 3201{
cab34f50
JD
3202 if (type == FocusIn)
3203 {
3204 if (dpyinfo->x_focus_event_frame != frame)
3205 {
3206 x_new_focus_frame (dpyinfo, frame);
3207 dpyinfo->x_focus_event_frame = frame;
7d0393cf 3208
cab34f50
JD
3209 /* Don't stop displaying the initial startup message
3210 for a switch-frame event we don't need. */
89079179 3211 if (GC_NILP (Vterminal_frame)
cab34f50
JD
3212 && GC_CONSP (Vframe_list)
3213 && !GC_NILP (XCDR (Vframe_list)))
3214 {
3215 bufp->kind = FOCUS_IN_EVENT;
3216 XSETFRAME (bufp->frame_or_window, frame);
cab34f50
JD
3217 }
3218 }
3219
3220 frame->output_data.x->focus_state |= state;
3221
3222#ifdef HAVE_X_I18N
3223 if (FRAME_XIC (frame))
3224 XSetICFocus (FRAME_XIC (frame));
3225#endif
3226 }
3227 else if (type == FocusOut)
3228 {
3229 frame->output_data.x->focus_state &= ~state;
7d0393cf 3230
cab34f50
JD
3231 if (dpyinfo->x_focus_event_frame == frame)
3232 {
3233 dpyinfo->x_focus_event_frame = 0;
3234 x_new_focus_frame (dpyinfo, 0);
3235 }
3236
3237#ifdef HAVE_X_I18N
3238 if (FRAME_XIC (frame))
3239 XUnsetICFocus (FRAME_XIC (frame));
3240#endif
3241 }
cab34f50
JD
3242}
3243
3244/* The focus may have changed. Figure out if it is a real focus change,
3245 by checking both FocusIn/Out and Enter/LeaveNotify events.
3246
89079179 3247 Returns FOCUS_IN_EVENT event in *BUFP. */
cab34f50 3248
89079179
KS
3249static void
3250x_detect_focus_change (dpyinfo, event, bufp)
cab34f50
JD
3251 struct x_display_info *dpyinfo;
3252 XEvent *event;
3253 struct input_event *bufp;
cab34f50
JD
3254{
3255 struct frame *frame;
7d0393cf 3256
9d00001f 3257 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
89079179
KS
3258 if (! frame)
3259 return;
7d0393cf 3260
cab34f50
JD
3261 switch (event->type)
3262 {
3263 case EnterNotify:
3264 case LeaveNotify:
0b03cc78
JD
3265 {
3266 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3267 int focus_state
3268 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3269
3270 if (event->xcrossing.detail != NotifyInferior
3271 && event->xcrossing.focus
3272 && ! (focus_state & FOCUS_EXPLICIT))
89079179
KS
3273 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3274 FOCUS_IMPLICIT,
3275 dpyinfo, frame, bufp);
0b03cc78 3276 }
cab34f50
JD
3277 break;
3278
3279 case FocusIn:
3280 case FocusOut:
89079179
KS
3281 x_focus_changed (event->type,
3282 (event->xfocus.detail == NotifyPointer ?
3283 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3284 dpyinfo, frame, bufp);
cab34f50
JD
3285 break;
3286 }
cab34f50
JD
3287}
3288
3289
37c2c98b
RS
3290/* Handle an event saying the mouse has moved out of an Emacs frame. */
3291
3292void
0f941935
KH
3293x_mouse_leave (dpyinfo)
3294 struct x_display_info *dpyinfo;
37c2c98b 3295{
0f941935 3296 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
37c2c98b 3297}
6d4238f3 3298
f451eb13
JB
3299/* The focus has changed, or we have redirected a frame's focus to
3300 another frame (this happens when a frame uses a surrogate
06a2c219 3301 mini-buffer frame). Shift the highlight as appropriate.
0f941935
KH
3302
3303 The FRAME argument doesn't necessarily have anything to do with which
06a2c219 3304 frame is being highlighted or un-highlighted; we only use it to find
0f941935 3305 the appropriate X display info. */
06a2c219 3306
6d4238f3 3307static void
0f941935
KH
3308XTframe_rehighlight (frame)
3309 struct frame *frame;
6d4238f3 3310{
0f941935
KH
3311 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
3312}
6d4238f3 3313
0f941935
KH
3314static void
3315x_frame_rehighlight (dpyinfo)
3316 struct x_display_info *dpyinfo;
3317{
3318 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3319
3320 if (dpyinfo->x_focus_frame)
6d4238f3 3321 {
0f941935
KH
3322 dpyinfo->x_highlight_frame
3323 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3324 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3325 : dpyinfo->x_focus_frame);
3326 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
f451eb13 3327 {
0f941935
KH
3328 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
3329 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
f451eb13 3330 }
dc6f92b8 3331 }
6d4238f3 3332 else
0f941935 3333 dpyinfo->x_highlight_frame = 0;
dc6f92b8 3334
0f941935 3335 if (dpyinfo->x_highlight_frame != old_highlight)
6d4238f3
JB
3336 {
3337 if (old_highlight)
f676886a 3338 frame_unhighlight (old_highlight);
0f941935
KH
3339 if (dpyinfo->x_highlight_frame)
3340 frame_highlight (dpyinfo->x_highlight_frame);
6d4238f3 3341 }
dc6f92b8 3342}
06a2c219
GM
3343
3344
dc6f92b8 3345\f
06a2c219 3346/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
dc6f92b8 3347
28430d3c
JB
3348/* Initialize mode_switch_bit and modifier_meaning. */
3349static void
334208b7
RS
3350x_find_modifier_meanings (dpyinfo)
3351 struct x_display_info *dpyinfo;
28430d3c 3352{
f689eb05 3353 int min_code, max_code;
28430d3c
JB
3354 KeySym *syms;
3355 int syms_per_code;
3356 XModifierKeymap *mods;
3357
334208b7
RS
3358 dpyinfo->meta_mod_mask = 0;
3359 dpyinfo->shift_lock_mask = 0;
3360 dpyinfo->alt_mod_mask = 0;
3361 dpyinfo->super_mod_mask = 0;
3362 dpyinfo->hyper_mod_mask = 0;
58769bee 3363
9658a521 3364#ifdef HAVE_X11R4
334208b7 3365 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
9658a521 3366#else
4a60f8c5
RS
3367 min_code = dpyinfo->display->min_keycode;
3368 max_code = dpyinfo->display->max_keycode;
9658a521
JB
3369#endif
3370
334208b7 3371 syms = XGetKeyboardMapping (dpyinfo->display,
28430d3c
JB
3372 min_code, max_code - min_code + 1,
3373 &syms_per_code);
334208b7 3374 mods = XGetModifierMapping (dpyinfo->display);
28430d3c 3375
58769bee 3376 /* Scan the modifier table to see which modifier bits the Meta and
11edeb03 3377 Alt keysyms are on. */
28430d3c 3378 {
06a2c219 3379 int row, col; /* The row and column in the modifier table. */
b90ee8b5 3380 int found_alt_or_meta;
28430d3c
JB
3381
3382 for (row = 3; row < 8; row++)
b90ee8b5
JD
3383 {
3384 found_alt_or_meta = 0;
28430d3c
JB
3385 for (col = 0; col < mods->max_keypermod; col++)
3386 {
b90ee8b5 3387 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
28430d3c 3388
af92970c
KH
3389 /* Zeroes are used for filler. Skip them. */
3390 if (code == 0)
3391 continue;
3392
28430d3c
JB
3393 /* Are any of this keycode's keysyms a meta key? */
3394 {
3395 int code_col;
3396
3397 for (code_col = 0; code_col < syms_per_code; code_col++)
3398 {
f689eb05 3399 int sym = syms[((code - min_code) * syms_per_code) + code_col];
28430d3c 3400
f689eb05 3401 switch (sym)
28430d3c 3402 {
f689eb05
JB
3403 case XK_Meta_L:
3404 case XK_Meta_R:
b90ee8b5 3405 found_alt_or_meta = 1;
334208b7 3406 dpyinfo->meta_mod_mask |= (1 << row);
28430d3c 3407 break;
f689eb05
JB
3408
3409 case XK_Alt_L:
3410 case XK_Alt_R:
b90ee8b5 3411 found_alt_or_meta = 1;
334208b7 3412 dpyinfo->alt_mod_mask |= (1 << row);
a3c44b14
RS
3413 break;
3414
3415 case XK_Hyper_L:
3416 case XK_Hyper_R:
b90ee8b5
JD
3417 if (!found_alt_or_meta)
3418 dpyinfo->hyper_mod_mask |= (1 << row);
3419 code_col = syms_per_code;
3420 col = mods->max_keypermod;
a3c44b14
RS
3421 break;
3422
3423 case XK_Super_L:
3424 case XK_Super_R:
b90ee8b5
JD
3425 if (!found_alt_or_meta)
3426 dpyinfo->super_mod_mask |= (1 << row);
3427 code_col = syms_per_code;
3428 col = mods->max_keypermod;
f689eb05 3429 break;
11edeb03
JB
3430
3431 case XK_Shift_Lock:
3432 /* Ignore this if it's not on the lock modifier. */
b90ee8b5 3433 if (!found_alt_or_meta && ((1 << row) == LockMask))
334208b7 3434 dpyinfo->shift_lock_mask = LockMask;
b90ee8b5
JD
3435 code_col = syms_per_code;
3436 col = mods->max_keypermod;
11edeb03 3437 break;
28430d3c
JB
3438 }
3439 }
3440 }
3441 }
b90ee8b5 3442 }
28430d3c
JB
3443 }
3444
f689eb05 3445 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
334208b7 3446 if (! dpyinfo->meta_mod_mask)
a3c44b14 3447 {
334208b7
RS
3448 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3449 dpyinfo->alt_mod_mask = 0;
a3c44b14 3450 }
f689eb05 3451
148c4b70
RS
3452 /* If some keys are both alt and meta,
3453 make them just meta, not alt. */
334208b7 3454 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
148c4b70 3455 {
334208b7 3456 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
148c4b70 3457 }
58769bee 3458
28430d3c 3459 XFree ((char *) syms);
f689eb05 3460 XFreeModifiermap (mods);
28430d3c
JB
3461}
3462
dfeccd2d
JB
3463/* Convert between the modifier bits X uses and the modifier bits
3464 Emacs uses. */
06a2c219 3465
7c5283e4 3466static unsigned int
334208b7
RS
3467x_x_to_emacs_modifiers (dpyinfo, state)
3468 struct x_display_info *dpyinfo;
dc6f92b8
JB
3469 unsigned int state;
3470{
98659da6
KG
3471 EMACS_UINT mod_meta = meta_modifier;
3472 EMACS_UINT mod_alt = alt_modifier;
3473 EMACS_UINT mod_hyper = hyper_modifier;
3474 EMACS_UINT mod_super = super_modifier;
3475 Lisp_Object tem;
7d0393cf 3476
98659da6
KG
3477 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3478 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3479 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3480 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3481 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3482 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3483 tem = Fget (Vx_super_keysym, Qmodifier_value);
3484 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
7d0393cf 3485
98659da6 3486
334208b7 3487 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
98659da6
KG
3488 | ((state & ControlMask) ? ctrl_modifier : 0)
3489 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3490 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3491 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3492 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
dc6f92b8
JB
3493}
3494
dfeccd2d 3495static unsigned int
334208b7
RS
3496x_emacs_to_x_modifiers (dpyinfo, state)
3497 struct x_display_info *dpyinfo;
dfeccd2d
JB
3498 unsigned int state;
3499{
98659da6
KG
3500 EMACS_UINT mod_meta = meta_modifier;
3501 EMACS_UINT mod_alt = alt_modifier;
3502 EMACS_UINT mod_hyper = hyper_modifier;
3503 EMACS_UINT mod_super = super_modifier;
7d0393cf 3504
98659da6 3505 Lisp_Object tem;
7d0393cf 3506
98659da6
KG
3507 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3508 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3509 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3510 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3511 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3512 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3513 tem = Fget (Vx_super_keysym, Qmodifier_value);
3514 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
7d0393cf
JB
3515
3516
98659da6
KG
3517 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3518 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3519 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3520 | ((state & shift_modifier) ? ShiftMask : 0)
3521 | ((state & ctrl_modifier) ? ControlMask : 0)
3522 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
dfeccd2d 3523}
d047c4eb
KH
3524
3525/* Convert a keysym to its name. */
3526
3527char *
3528x_get_keysym_name (keysym)
3529 KeySym keysym;
3530{
3531 char *value;
3532
3533 BLOCK_INPUT;
3534 value = XKeysymToString (keysym);
3535 UNBLOCK_INPUT;
3536
3537 return value;
3538}
06a2c219
GM
3539
3540
e4571a43
JB
3541\f
3542/* Mouse clicks and mouse movement. Rah. */
e4571a43 3543
442a09ea 3544/* Prepare a mouse-event in *RESULT for placement in the input queue.
71b8321e 3545
442a09ea
KS
3546 If the event is a button press, then note that we have grabbed
3547 the mouse. */
3548
3549static Lisp_Object
3550construct_mouse_click (result, event, f)
3551 struct input_event *result;
3552 XButtonEvent *event;
3553 struct frame *f;
71b8321e 3554{
442a09ea
KS
3555 /* Make the event type NO_EVENT; we'll change that when we decide
3556 otherwise. */
3557 result->kind = MOUSE_CLICK_EVENT;
3558 result->code = event->button - Button1;
3559 result->timestamp = event->time;
3560 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
3561 event->state)
3562 | (event->type == ButtonRelease
3563 ? up_modifier
3564 : down_modifier));
71b8321e 3565
442a09ea
KS
3566 XSETINT (result->x, event->x);
3567 XSETINT (result->y, event->y);
3568 XSETFRAME (result->frame_or_window, f);
3569 result->arg = Qnil;
3570 return Qnil;
71b8321e
GM
3571}
3572
442a09ea
KS
3573\f
3574/* Function to report a mouse movement to the mainstream Emacs code.
3575 The input handler calls this.
71b8321e 3576
442a09ea
KS
3577 We have received a mouse movement event, which is given in *event.
3578 If the mouse is over a different glyph than it was last time, tell
3579 the mainstream emacs code by setting mouse_moved. If not, ask for
3580 another motion event, so we can check again the next time it moves. */
e687d06e 3581
442a09ea
KS
3582static XMotionEvent last_mouse_motion_event;
3583static Lisp_Object last_mouse_motion_frame;
3584
3585static void
3586note_mouse_movement (frame, event)
3587 FRAME_PTR frame;
3588 XMotionEvent *event;
e687d06e 3589{
442a09ea
KS
3590 last_mouse_movement_time = event->time;
3591 last_mouse_motion_event = *event;
3592 XSETFRAME (last_mouse_motion_frame, frame);
e687d06e 3593
442a09ea 3594 if (event->window != FRAME_X_WINDOW (frame))
e687d06e 3595 {
442a09ea
KS
3596 frame->mouse_moved = 1;
3597 last_mouse_scroll_bar = Qnil;
3598 note_mouse_highlight (frame, -1, -1);
3599 }
3600
3601 /* Has the mouse moved off the glyph it was on at the last sighting? */
3602 else if (event->x < last_mouse_glyph.x
3603 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
3604 || event->y < last_mouse_glyph.y
3605 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
3606 {
3607 frame->mouse_moved = 1;
3608 last_mouse_scroll_bar = Qnil;
3609 note_mouse_highlight (frame, event->x, event->y);
e687d06e
RS
3610 }
3611}
b52b65bd 3612
b8009dd1 3613\f
442a09ea
KS
3614/************************************************************************
3615 Mouse Face
3616 ************************************************************************/
3617
3618static void
3619redo_mouse_highlight ()
3620{
3621 if (!NILP (last_mouse_motion_frame)
3622 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3623 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3624 last_mouse_motion_event.x,
3625 last_mouse_motion_event.y);
3626}
3627
3628
b52b65bd
GM
3629static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
3630
3631
3632/* Try to determine frame pixel position and size of the glyph under
3633 frame pixel coordinates X/Y on frame F . Return the position and
3634 size in *RECT. Value is non-zero if we could compute these
3635 values. */
3636
3637static int
3638glyph_rect (f, x, y, rect)
3639 struct frame *f;
3640 int x, y;
3641 XRectangle *rect;
3642{
3643 Lisp_Object window;
89079179
KS
3644 struct window *w;
3645 struct glyph_row *r, *end_row;
b52b65bd 3646
0899d58c 3647 window = window_from_coordinates (f, x, y, 0, &x, &y, 0);
89079179
KS
3648 if (NILP (window))
3649 return 0;
b52b65bd 3650
89079179
KS
3651 w = XWINDOW (window);
3652 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
3653 end_row = r + w->current_matrix->nrows - 1;
7d0393cf 3654
89079179
KS
3655 for (; r < end_row && r->enabled_p; ++r)
3656 {
3657 if (r->y >= y)
3658 {
3659 struct glyph *g = r->glyphs[TEXT_AREA];
3660 struct glyph *end = g + r->used[TEXT_AREA];
3661 int gx = r->x;
3662 while (g < end && gx < x)
3663 gx += g->pixel_width, ++g;
3664 if (g < end)
3665 {
3666 rect->width = g->pixel_width;
3667 rect->height = r->height;
3668 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
3669 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
3670 return 1;
3671 }
3672 break;
3673 }
b52b65bd
GM
3674 }
3675
89079179 3676 return 0;
b52b65bd
GM
3677}
3678
12ba150f 3679
90e65f07 3680/* Return the current position of the mouse.
b52b65bd 3681 *FP should be a frame which indicates which display to ask about.
90e65f07 3682
b52b65bd
GM
3683 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3684 and *PART to the frame, window, and scroll bar part that the mouse
3685 is over. Set *X and *Y to the portion and whole of the mouse's
ab648270 3686 position on the scroll bar.
12ba150f 3687
b52b65bd
GM
3688 If the mouse movement started elsewhere, set *FP to the frame the
3689 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
12ba150f
JB
3690 the mouse is over.
3691
b52b65bd 3692 Set *TIME to the server time-stamp for the time at which the mouse
12ba150f
JB
3693 was at this position.
3694
a135645a
RS
3695 Don't store anything if we don't have a valid set of values to report.
3696
90e65f07 3697 This clears the mouse_moved flag, so we can wait for the next mouse
f5bb65ec 3698 movement. */
90e65f07
JB
3699
3700static void
1cf412ec 3701XTmouse_position (fp, insist, bar_window, part, x, y, time)
334208b7 3702 FRAME_PTR *fp;
1cf412ec 3703 int insist;
12ba150f 3704 Lisp_Object *bar_window;
ab648270 3705 enum scroll_bar_part *part;
90e65f07 3706 Lisp_Object *x, *y;
e5d77022 3707 unsigned long *time;
90e65f07 3708{
a135645a
RS
3709 FRAME_PTR f1;
3710
90e65f07
JB
3711 BLOCK_INPUT;
3712
8bcee03e 3713 if (! NILP (last_mouse_scroll_bar) && insist == 0)
334208b7 3714 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
90e65f07
JB
3715 else
3716 {
12ba150f
JB
3717 Window root;
3718 int root_x, root_y;
90e65f07 3719
12ba150f
JB
3720 Window dummy_window;
3721 int dummy;
3722
39d8bb4d
KH
3723 Lisp_Object frame, tail;
3724
3725 /* Clear the mouse-moved flag for every frame on this display. */
3726 FOR_EACH_FRAME (tail, frame)
3727 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3728 XFRAME (frame)->mouse_moved = 0;
3729
ab648270 3730 last_mouse_scroll_bar = Qnil;
12ba150f
JB
3731
3732 /* Figure out which root window we're on. */
334208b7
RS
3733 XQueryPointer (FRAME_X_DISPLAY (*fp),
3734 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
12ba150f
JB
3735
3736 /* The root window which contains the pointer. */
3737 &root,
3738
3739 /* Trash which we can't trust if the pointer is on
3740 a different screen. */
3741 &dummy_window,
3742
3743 /* The position on that root window. */
58769bee 3744 &root_x, &root_y,
12ba150f
JB
3745
3746 /* More trash we can't trust. */
3747 &dummy, &dummy,
3748
3749 /* Modifier keys and pointer buttons, about which
3750 we don't care. */
3751 (unsigned int *) &dummy);
3752
3753 /* Now we have a position on the root; find the innermost window
3754 containing the pointer. */
3755 {
3756 Window win, child;
3757 int win_x, win_y;
06a2c219 3758 int parent_x = 0, parent_y = 0;
e99db5a1 3759 int count;
12ba150f
JB
3760
3761 win = root;
69388238 3762
2d7fc7e8
RS
3763 /* XTranslateCoordinates can get errors if the window
3764 structure is changing at the same time this function
3765 is running. So at least we must not crash from them. */
3766
e99db5a1 3767 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
2d7fc7e8 3768
334208b7 3769 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
23faf38f 3770 && FRAME_LIVE_P (last_mouse_frame))
12ba150f 3771 {
69388238
RS
3772 /* If mouse was grabbed on a frame, give coords for that frame
3773 even if the mouse is now outside it. */
334208b7 3774 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
69388238 3775
12ba150f 3776 /* From-window, to-window. */
69388238 3777 root, FRAME_X_WINDOW (last_mouse_frame),
12ba150f
JB
3778
3779 /* From-position, to-position. */
3780 root_x, root_y, &win_x, &win_y,
3781
3782 /* Child of win. */
3783 &child);
69388238
RS
3784 f1 = last_mouse_frame;
3785 }
3786 else
3787 {
3788 while (1)
3789 {
334208b7 3790 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
12ba150f 3791
69388238
RS
3792 /* From-window, to-window. */
3793 root, win,
12ba150f 3794
69388238
RS
3795 /* From-position, to-position. */
3796 root_x, root_y, &win_x, &win_y,
3797
3798 /* Child of win. */
3799 &child);
3800
9af3143a 3801 if (child == None || child == win)
69388238
RS
3802 break;
3803
3804 win = child;
3805 parent_x = win_x;
3806 parent_y = win_y;
3807 }
12ba150f 3808
69388238
RS
3809 /* Now we know that:
3810 win is the innermost window containing the pointer
3811 (XTC says it has no child containing the pointer),
3812 win_x and win_y are the pointer's position in it
3813 (XTC did this the last time through), and
3814 parent_x and parent_y are the pointer's position in win's parent.
3815 (They are what win_x and win_y were when win was child.
3816 If win is the root window, it has no parent, and
3817 parent_{x,y} are invalid, but that's okay, because we'll
3818 never use them in that case.) */
3819
3820 /* Is win one of our frames? */
19126e11 3821 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
aad0f6ab
GM
3822
3823#ifdef USE_X_TOOLKIT
3824 /* If we end up with the menu bar window, say it's not
3825 on the frame. */
3826 if (f1 != NULL
3827 && f1->output_data.x->menubar_widget
3828 && win == XtWindow (f1->output_data.x->menubar_widget))
3829 f1 = NULL;
3830#endif /* USE_X_TOOLKIT */
69388238 3831 }
58769bee 3832
2d7fc7e8
RS
3833 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
3834 f1 = 0;
3835
e99db5a1 3836 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
2d7fc7e8 3837
ab648270 3838 /* If not, is it one of our scroll bars? */
a135645a 3839 if (! f1)
12ba150f 3840 {
810f2256
JD
3841 struct scroll_bar *bar;
3842
3843 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win);
12ba150f
JB
3844
3845 if (bar)
3846 {
a135645a 3847 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
3848 win_x = parent_x;
3849 win_y = parent_y;
3850 }
3851 }
90e65f07 3852
8bcee03e 3853 if (f1 == 0 && insist > 0)
b86bd3dd 3854 f1 = SELECTED_FRAME ();
1cf412ec 3855
a135645a 3856 if (f1)
12ba150f 3857 {
06a2c219
GM
3858 /* Ok, we found a frame. Store all the values.
3859 last_mouse_glyph is a rectangle used to reduce the
3860 generation of mouse events. To not miss any motion
3861 events, we must divide the frame into rectangles of the
3862 size of the smallest character that could be displayed
3863 on it, i.e. into the same rectangles that matrices on
3864 the frame are divided into. */
3865
b52b65bd
GM
3866 int width, height, gx, gy;
3867 XRectangle rect;
7d0393cf 3868
b52b65bd
GM
3869 if (glyph_rect (f1, win_x, win_y, &rect))
3870 last_mouse_glyph = rect;
3871 else
3872 {
3873 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
3874 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
3875 gx = win_x;
3876 gy = win_y;
7d0393cf 3877
0899d58c 3878 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
b52b65bd
GM
3879 round down even for negative values. */
3880 if (gx < 0)
3881 gx -= width - 1;
4f00e84d 3882 if (gy < 0)
b52b65bd
GM
3883 gy -= height - 1;
3884 gx = (gx + width - 1) / width * width;
3885 gy = (gy + height - 1) / height * height;
7d0393cf 3886
b52b65bd
GM
3887 last_mouse_glyph.width = width;
3888 last_mouse_glyph.height = height;
3889 last_mouse_glyph.x = gx;
3890 last_mouse_glyph.y = gy;
3891 }
12ba150f
JB
3892
3893 *bar_window = Qnil;
3894 *part = 0;
334208b7 3895 *fp = f1;
e0c1aef2
KH
3896 XSETINT (*x, win_x);
3897 XSETINT (*y, win_y);
12ba150f
JB
3898 *time = last_mouse_movement_time;
3899 }
3900 }
3901 }
90e65f07
JB
3902
3903 UNBLOCK_INPUT;
3904}
f451eb13 3905
06a2c219 3906
06a2c219 3907\f
442a09ea
KS
3908/***********************************************************************
3909 Scroll bars
3910 ***********************************************************************/
3911
06a2c219
GM
3912/* Scroll bar support. */
3913
810f2256
JD
3914/* Given an X window ID and a DISPLAY, find the struct scroll_bar which
3915 manages it.
06a2c219
GM
3916 This can be called in GC, so we have to make sure to strip off mark
3917 bits. */
bffcfca9 3918
06a2c219 3919static struct scroll_bar *
810f2256
JD
3920x_window_to_scroll_bar (display, window_id)
3921 Display *display;
06a2c219
GM
3922 Window window_id;
3923{
3924 Lisp_Object tail;
3925
a11e1dce 3926#if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
810f2256 3927 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
a11e1dce 3928#endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
257f40f2 3929
06a2c219
GM
3930 for (tail = Vframe_list;
3931 XGCTYPE (tail) == Lisp_Cons;
8e713be6 3932 tail = XCDR (tail))
06a2c219
GM
3933 {
3934 Lisp_Object frame, bar, condemned;
3935
8e713be6 3936 frame = XCAR (tail);
06a2c219
GM
3937 /* All elements of Vframe_list should be frames. */
3938 if (! GC_FRAMEP (frame))
3939 abort ();
3940
3941 /* Scan this frame's scroll bar list for a scroll bar with the
3942 right window ID. */
3943 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
3944 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
3945 /* This trick allows us to search both the ordinary and
3946 condemned scroll bar lists with one loop. */
3947 ! GC_NILP (bar) || (bar = condemned,
3948 condemned = Qnil,
3949 ! GC_NILP (bar));
3950 bar = XSCROLL_BAR (bar)->next)
a0c6ef2d
JD
3951 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id &&
3952 FRAME_X_DISPLAY (XFRAME (frame)) == display)
06a2c219
GM
3953 return XSCROLL_BAR (bar);
3954 }
3955
3956 return 0;
3957}
3958
3959
01f67d2c 3960#if defined USE_LUCID
c95fc5f1
GM
3961
3962/* Return the Lucid menu bar WINDOW is part of. Return null
3963 if WINDOW is not part of a menu bar. */
3964
3965static Widget
3966x_window_to_menu_bar (window)
3967 Window window;
3968{
3969 Lisp_Object tail;
7d0393cf 3970
c95fc5f1
GM
3971 for (tail = Vframe_list;
3972 XGCTYPE (tail) == Lisp_Cons;
3973 tail = XCDR (tail))
3974 {
3975 Lisp_Object frame = XCAR (tail);
3976 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
7d0393cf 3977
c95fc5f1
GM
3978 if (menu_bar && xlwmenu_window_p (menu_bar, window))
3979 return menu_bar;
3980 }
3981
3982 return NULL;
3983}
3984
01f67d2c 3985#endif /* USE_LUCID */
c95fc5f1 3986
06a2c219
GM
3987\f
3988/************************************************************************
3989 Toolkit scroll bars
3990 ************************************************************************/
3991
eccc05db 3992#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
3993
3994static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
3995static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
3996static void x_create_toolkit_scroll_bar P_ ((struct frame *,
3997 struct scroll_bar *));
3998static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
3999 int, int, int));
4000
4001
06a2c219
GM
4002/* Lisp window being scrolled. Set when starting to interact with
4003 a toolkit scroll bar, reset to nil when ending the interaction. */
4004
4005static Lisp_Object window_being_scrolled;
4006
4007/* Last scroll bar part sent in xm_scroll_callback. */
4008
4009static int last_scroll_bar_part;
4010
ec18280f
SM
4011/* Whether this is an Xaw with arrow-scrollbars. This should imply
4012 that movements of 1/20 of the screen size are mapped to up/down. */
4013
488dd4c4
JD
4014#ifndef USE_GTK
4015/* Id of action hook installed for scroll bars. */
4016
4017static XtActionHookId action_hook_id;
4018
ec18280f
SM
4019static Boolean xaw3d_arrow_scroll;
4020
4021/* Whether the drag scrolling maintains the mouse at the top of the
4022 thumb. If not, resizing the thumb needs to be done more carefully
4023 to avoid jerkyness. */
4024
4025static Boolean xaw3d_pick_top;
4026
3c671a56 4027extern void set_vertical_scroll_bar P_ ((struct window *));
06a2c219
GM
4028
4029/* Action hook installed via XtAppAddActionHook when toolkit scroll
ec18280f 4030 bars are used.. The hook is responsible for detecting when
06a2c219 4031 the user ends an interaction with the scroll bar, and generates
3b8f9651 4032 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
06a2c219
GM
4033
4034static void
4035xt_action_hook (widget, client_data, action_name, event, params,
4036 num_params)
4037 Widget widget;
4038 XtPointer client_data;
4039 String action_name;
4040 XEvent *event;
4041 String *params;
4042 Cardinal *num_params;
4043{
4044 int scroll_bar_p;
4045 char *end_action;
7d0393cf 4046
06a2c219
GM
4047#ifdef USE_MOTIF
4048 scroll_bar_p = XmIsScrollBar (widget);
4049 end_action = "Release";
ec18280f 4050#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
4051 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4052 end_action = "EndScroll";
ec18280f 4053#endif /* USE_MOTIF */
06a2c219 4054
06a2c219
GM
4055 if (scroll_bar_p
4056 && strcmp (action_name, end_action) == 0
4057 && WINDOWP (window_being_scrolled))
4058 {
4059 struct window *w;
7d0393cf 4060
06a2c219
GM
4061 x_send_scroll_bar_event (window_being_scrolled,
4062 scroll_bar_end_scroll, 0, 0);
4063 w = XWINDOW (window_being_scrolled);
62fe13a4 4064
3c671a56 4065 if (!NILP (XSCROLL_BAR (w->vertical_scroll_bar)->dragging))
95a39dc5
SM
4066 {
4067 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
4068 /* The thumb size is incorrect while dragging: fix it. */
4069 set_vertical_scroll_bar (w);
4070 }
06a2c219
GM
4071 window_being_scrolled = Qnil;
4072 last_scroll_bar_part = -1;
bffcfca9
GM
4073
4074 /* Xt timeouts no longer needed. */
4075 toolkit_scroll_bar_interaction = 0;
06a2c219
GM
4076 }
4077}
488dd4c4 4078#endif /* not USE_GTK */
06a2c219 4079
07b3d16e
GM
4080/* A vector of windows used for communication between
4081 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
4082
4083static struct window **scroll_bar_windows;
4084static int scroll_bar_windows_size;
4085
06a2c219
GM
4086
4087/* Send a client message with message type Xatom_Scrollbar for a
4088 scroll action to the frame of WINDOW. PART is a value identifying
4089 the part of the scroll bar that was clicked on. PORTION is the
4090 amount to scroll of a whole of WHOLE. */
4091
4092static void
4093x_send_scroll_bar_event (window, part, portion, whole)
4094 Lisp_Object window;
4095 int part, portion, whole;
4096{
4097 XEvent event;
4098 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
07b3d16e
GM
4099 struct window *w = XWINDOW (window);
4100 struct frame *f = XFRAME (w->frame);
4101 int i;
06a2c219 4102
07b3d16e 4103 BLOCK_INPUT;
7d0393cf 4104
06a2c219
GM
4105 /* Construct a ClientMessage event to send to the frame. */
4106 ev->type = ClientMessage;
4107 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
4108 ev->display = FRAME_X_DISPLAY (f);
4109 ev->window = FRAME_X_WINDOW (f);
4110 ev->format = 32;
07b3d16e
GM
4111
4112 /* We can only transfer 32 bits in the XClientMessageEvent, which is
4113 not enough to store a pointer or Lisp_Object on a 64 bit system.
4114 So, store the window in scroll_bar_windows and pass the index
4115 into that array in the event. */
4116 for (i = 0; i < scroll_bar_windows_size; ++i)
4117 if (scroll_bar_windows[i] == NULL)
4118 break;
4119
4120 if (i == scroll_bar_windows_size)
4121 {
4122 int new_size = max (10, 2 * scroll_bar_windows_size);
4123 size_t nbytes = new_size * sizeof *scroll_bar_windows;
4124 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
7d0393cf 4125
07b3d16e
GM
4126 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
4127 nbytes);
4128 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
4129 scroll_bar_windows_size = new_size;
4130 }
4131
4132 scroll_bar_windows[i] = w;
4133 ev->data.l[0] = (long) i;
06a2c219
GM
4134 ev->data.l[1] = (long) part;
4135 ev->data.l[2] = (long) 0;
4136 ev->data.l[3] = (long) portion;
4137 ev->data.l[4] = (long) whole;
4138
bffcfca9
GM
4139 /* Make Xt timeouts work while the scroll bar is active. */
4140 toolkit_scroll_bar_interaction = 1;
4141
06a2c219
GM
4142 /* Setting the event mask to zero means that the message will
4143 be sent to the client that created the window, and if that
4144 window no longer exists, no event will be sent. */
06a2c219
GM
4145 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4146 UNBLOCK_INPUT;
4147}
4148
4149
4150/* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4151 in *IEVENT. */
4152
4153static void
4154x_scroll_bar_to_input_event (event, ievent)
4155 XEvent *event;
4156 struct input_event *ievent;
4157{
4158 XClientMessageEvent *ev = (XClientMessageEvent *) event;
52e386c2
KR
4159 Lisp_Object window;
4160 struct frame *f;
07b3d16e 4161 struct window *w;
7d0393cf 4162
07b3d16e
GM
4163 w = scroll_bar_windows[ev->data.l[0]];
4164 scroll_bar_windows[ev->data.l[0]] = NULL;
52e386c2 4165
07b3d16e
GM
4166 XSETWINDOW (window, w);
4167 f = XFRAME (w->frame);
7d0393cf 4168
3b8f9651 4169 ievent->kind = SCROLL_BAR_CLICK_EVENT;
06a2c219 4170 ievent->frame_or_window = window;
0f8aabe9 4171 ievent->arg = Qnil;
488dd4c4
JD
4172#ifdef USE_GTK
4173 ievent->timestamp = CurrentTime;
4174#else
06a2c219 4175 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
488dd4c4 4176#endif
06a2c219
GM
4177 ievent->part = ev->data.l[1];
4178 ievent->code = ev->data.l[2];
4179 ievent->x = make_number ((int) ev->data.l[3]);
4180 ievent->y = make_number ((int) ev->data.l[4]);
4181 ievent->modifiers = 0;
4182}
4183
4184
4185#ifdef USE_MOTIF
4186
4187/* Minimum and maximum values used for Motif scroll bars. */
4188
06a2c219 4189#define XM_SB_MAX 10000000
06a2c219
GM
4190
4191
4192/* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4193 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4696802b 4194 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
06a2c219
GM
4195
4196static void
4197xm_scroll_callback (widget, client_data, call_data)
4198 Widget widget;
4199 XtPointer client_data, call_data;
4200{
4201 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4202 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
06a2c219
GM
4203 int part = -1, whole = 0, portion = 0;
4204
4205 switch (cs->reason)
4206 {
4207 case XmCR_DECREMENT:
4208 bar->dragging = Qnil;
4209 part = scroll_bar_up_arrow;
4210 break;
4211
4212 case XmCR_INCREMENT:
4213 bar->dragging = Qnil;
4214 part = scroll_bar_down_arrow;
4215 break;
4216
4217 case XmCR_PAGE_DECREMENT:
4218 bar->dragging = Qnil;
4219 part = scroll_bar_above_handle;
4220 break;
4221
4222 case XmCR_PAGE_INCREMENT:
4223 bar->dragging = Qnil;
4224 part = scroll_bar_below_handle;
4225 break;
4226
4227 case XmCR_TO_TOP:
4228 bar->dragging = Qnil;
4229 part = scroll_bar_to_top;
4230 break;
7d0393cf 4231
06a2c219
GM
4232 case XmCR_TO_BOTTOM:
4233 bar->dragging = Qnil;
4234 part = scroll_bar_to_bottom;
4235 break;
4236
4237 case XmCR_DRAG:
4238 {
4239 int slider_size;
06a2c219
GM
4240
4241 /* Get the slider size. */
4242 BLOCK_INPUT;
4243 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4244 UNBLOCK_INPUT;
4245
baa21c48 4246 whole = XM_SB_MAX - slider_size;
3c671a56 4247 portion = min (cs->value, whole);
6f2a7a68
SM
4248 part = scroll_bar_handle;
4249 bar->dragging = make_number (cs->value);
06a2c219
GM
4250 }
4251 break;
7d0393cf 4252
06a2c219
GM
4253 case XmCR_VALUE_CHANGED:
4254 break;
4255 };
4256
4257 if (part >= 0)
4258 {
4259 window_being_scrolled = bar->window;
4260 last_scroll_bar_part = part;
4261 x_send_scroll_bar_event (bar->window, part, portion, whole);
4262 }
4263}
4264
4265
488dd4c4
JD
4266#else /* !USE_MOTIF, i.e. Xaw or GTK */
4267#ifdef USE_GTK
17097258 4268/* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
f979dc05 4269 bar widget. DATA is a pointer to the scroll_bar structure. */
488dd4c4
JD
4270
4271static void
4272xg_scroll_callback (widget, data)
f979dc05 4273 GtkRange *widget;
488dd4c4
JD
4274 gpointer data;
4275{
4276 struct scroll_bar *bar = (struct scroll_bar *) data;
4277 gdouble previous;
4278 gdouble position;
4279 gdouble *p;
4280 int diff;
177c0ea7 4281
488dd4c4 4282 int part = -1, whole = 0, portion = 0;
f979dc05 4283 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (widget));
177c0ea7 4284
488dd4c4
JD
4285 position = gtk_adjustment_get_value (adj);
4286
4287 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
4288 if (! p)
4289 {
4290 p = (gdouble*) xmalloc (sizeof (gdouble));
4291 *p = XG_SB_MIN;
4292 g_object_set_data (G_OBJECT (widget), XG_LAST_SB_DATA, p);
4293 }
4294
4295 previous = *p;
4296 *p = position;
4297
fecad3f6
JD
4298 if (xg_ignore_gtk_scrollbar) return;
4299
488dd4c4 4300 diff = (int) (position - previous);
177c0ea7 4301
488dd4c4
JD
4302 if (diff == (int) adj->step_increment)
4303 {
4304 part = scroll_bar_down_arrow;
4305 bar->dragging = Qnil;
4306 }
4307 else if (-diff == (int) adj->step_increment)
4308 {
4309 part = scroll_bar_up_arrow;
4310 bar->dragging = Qnil;
4311 }
4312 else if (diff == (int) adj->page_increment)
4313 {
4314 part = scroll_bar_below_handle;
4315 bar->dragging = Qnil;
4316 }
4317 else if (-diff == (int) adj->page_increment)
4318 {
4319 part = scroll_bar_above_handle;
4320 bar->dragging = Qnil;
4321 }
4322 else
4323 {
4324 part = scroll_bar_handle;
4325 whole = adj->upper - adj->page_size;
17097258
JD
4326 portion = min ((int)position, whole);
4327 bar->dragging = make_number ((int)portion);
488dd4c4 4328 }
177c0ea7 4329
488dd4c4 4330 if (part >= 0)
82ead4b1 4331 {
488dd4c4
JD
4332 window_being_scrolled = bar->window;
4333 last_scroll_bar_part = part;
4334 x_send_scroll_bar_event (bar->window, part, portion, whole);
4335 }
4336}
06a2c219 4337
488dd4c4 4338#else /* not USE_GTK */
06a2c219 4339
ec18280f 4340/* Xaw scroll bar callback. Invoked when the thumb is dragged.
06a2c219
GM
4341 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4342 scroll bar struct. CALL_DATA is a pointer to a float saying where
4343 the thumb is. */
4344
4345static void
ec18280f 4346xaw_jump_callback (widget, client_data, call_data)
06a2c219
GM
4347 Widget widget;
4348 XtPointer client_data, call_data;
4349{
4350 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4351 float top = *(float *) call_data;
4352 float shown;
ec18280f
SM
4353 int whole, portion, height;
4354 int part;
06a2c219
GM
4355
4356 /* Get the size of the thumb, a value between 0 and 1. */
4357 BLOCK_INPUT;
ec18280f 4358 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
06a2c219
GM
4359 UNBLOCK_INPUT;
4360
4361 whole = 10000000;
4362 portion = shown < 1 ? top * whole : 0;
06a2c219 4363
ec18280f
SM
4364 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
4365 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4366 the bottom, so we force the scrolling whenever we see that we're
4367 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4368 we try to ensure that we always stay two pixels away from the
4369 bottom). */
06a2c219
GM
4370 part = scroll_bar_down_arrow;
4371 else
4372 part = scroll_bar_handle;
4373
4374 window_being_scrolled = bar->window;
4375 bar->dragging = make_number (portion);
4376 last_scroll_bar_part = part;
4377 x_send_scroll_bar_event (bar->window, part, portion, whole);
4378}
4379
4380
ec18280f
SM
4381/* Xaw scroll bar callback. Invoked for incremental scrolling.,
4382 i.e. line or page up or down. WIDGET is the Xaw scroll bar
06a2c219
GM
4383 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4384 the scroll bar. CALL_DATA is an integer specifying the action that
dbd4b028 4385 has taken place. Its magnitude is in the range 0..height of the
06a2c219
GM
4386 scroll bar. Negative values mean scroll towards buffer start.
4387 Values < height of scroll bar mean line-wise movement. */
4388
4389static void
ec18280f 4390xaw_scroll_callback (widget, client_data, call_data)
06a2c219
GM
4391 Widget widget;
4392 XtPointer client_data, call_data;
4393{
4394 struct scroll_bar *bar = (struct scroll_bar *) client_data;
560d7ceb
DL
4395 /* The position really is stored cast to a pointer. */
4396 int position = (long) call_data;
06a2c219
GM
4397 Dimension height;
4398 int part;
4399
4400 /* Get the height of the scroll bar. */
4401 BLOCK_INPUT;
4402 XtVaGetValues (widget, XtNheight, &height, NULL);
4403 UNBLOCK_INPUT;
4404
ec18280f
SM
4405 if (abs (position) >= height)
4406 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4407
4408 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4409 it maps line-movement to call_data = max(5, height/20). */
4410 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
4411 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
06a2c219 4412 else
ec18280f 4413 part = scroll_bar_move_ratio;
06a2c219
GM
4414
4415 window_being_scrolled = bar->window;
4416 bar->dragging = Qnil;
4417 last_scroll_bar_part = part;
ec18280f 4418 x_send_scroll_bar_event (bar->window, part, position, height);
06a2c219
GM
4419}
4420
488dd4c4 4421#endif /* not USE_GTK */
06a2c219
GM
4422#endif /* not USE_MOTIF */
4423
488dd4c4 4424#define SCROLL_BAR_NAME "verticalScrollBar"
06a2c219
GM
4425
4426/* Create the widget for scroll bar BAR on frame F. Record the widget
4427 and X window of the scroll bar in BAR. */
4428
488dd4c4
JD
4429#ifdef USE_GTK
4430static void
4431x_create_toolkit_scroll_bar (f, bar)
4432 struct frame *f;
4433 struct scroll_bar *bar;
4434{
4435 char *scroll_bar_name = SCROLL_BAR_NAME;
4436
4437 BLOCK_INPUT;
4438 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4439 scroll_bar_name);
4440 UNBLOCK_INPUT;
4441}
4442
4443#else /* not USE_GTK */
4444
06a2c219
GM
4445static void
4446x_create_toolkit_scroll_bar (f, bar)
4447 struct frame *f;
4448 struct scroll_bar *bar;
4449{
4450 Window xwindow;
4451 Widget widget;
4452 Arg av[20];
4453 int ac = 0;
488dd4c4 4454 char *scroll_bar_name = SCROLL_BAR_NAME;
06a2c219
GM
4455 unsigned long pixel;
4456
4457 BLOCK_INPUT;
4458
4459#ifdef USE_MOTIF
06a2c219
GM
4460 /* Set resources. Create the widget. */
4461 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
3c671a56 4462 XtSetArg (av[ac], XmNminimum, 0); ++ac;
06a2c219
GM
4463 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4464 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4465 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4466 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4467 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4468
4469 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4470 if (pixel != -1)
4471 {
4472 XtSetArg (av[ac], XmNforeground, pixel);
4473 ++ac;
4474 }
7d0393cf 4475
06a2c219
GM
4476 pixel = f->output_data.x->scroll_bar_background_pixel;
4477 if (pixel != -1)
4478 {
4479 XtSetArg (av[ac], XmNbackground, pixel);
4480 ++ac;
4481 }
7d0393cf 4482
06a2c219
GM
4483 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4484 scroll_bar_name, av, ac);
4485
4486 /* Add one callback for everything that can happen. */
4487 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4488 (XtPointer) bar);
4489 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4490 (XtPointer) bar);
4491 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4492 (XtPointer) bar);
4493 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4494 (XtPointer) bar);
4495 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4496 (XtPointer) bar);
4497 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4498 (XtPointer) bar);
4499 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4500 (XtPointer) bar);
7d0393cf 4501
06a2c219
GM
4502 /* Realize the widget. Only after that is the X window created. */
4503 XtRealizeWidget (widget);
4504
4505 /* Set the cursor to an arrow. I didn't find a resource to do that.
4506 And I'm wondering why it hasn't an arrow cursor by default. */
4507 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4508 f->output_data.x->nontext_cursor);
7d0393cf 4509
ec18280f 4510#else /* !USE_MOTIF i.e. use Xaw */
7d0393cf 4511
06a2c219
GM
4512 /* Set resources. Create the widget. The background of the
4513 Xaw3d scroll bar widget is a little bit light for my taste.
4514 We don't alter it here to let users change it according
4515 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4516 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4517 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
ec18280f
SM
4518 /* For smoother scrolling with Xaw3d -sm */
4519 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
7d0393cf 4520
06a2c219
GM
4521 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4522 if (pixel != -1)
4523 {
4524 XtSetArg (av[ac], XtNforeground, pixel);
4525 ++ac;
4526 }
7d0393cf 4527
06a2c219
GM
4528 pixel = f->output_data.x->scroll_bar_background_pixel;
4529 if (pixel != -1)
4530 {
4531 XtSetArg (av[ac], XtNbackground, pixel);
4532 ++ac;
4533 }
7c1bef7a
MB
4534
4535 /* Top/bottom shadow colors. */
4536
4537 /* Allocate them, if necessary. */
4538 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4539 {
4540 pixel = f->output_data.x->scroll_bar_background_pixel;
4541 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4542 &pixel, 1.2, 0x8000))
4543 pixel = -1;
4544 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4545 }
4546 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4547 {
4548 pixel = f->output_data.x->scroll_bar_background_pixel;
4549 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4550 &pixel, 0.6, 0x4000))
4551 pixel = -1;
4552 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4553 }
4554
6ca53601 4555#ifdef XtNbeNiceToColormap
7c1bef7a
MB
4556 /* Tell the toolkit about them. */
4557 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4558 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4559 /* We tried to allocate a color for the top/bottom shadow, and
4560 failed, so tell Xaw3d to use dithering instead. */
4561 {
4562 XtSetArg (av[ac], XtNbeNiceToColormap, True);
4563 ++ac;
4564 }
4565 else
4566 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4567 be more consistent with other emacs 3d colors, and since Xaw3d is
4568 not good at dealing with allocation failure. */
4569 {
4570 /* This tells Xaw3d to use real colors instead of dithering for
4571 the shadows. */
4572 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4573 ++ac;
4574
4575 /* Specify the colors. */
4576 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4577 if (pixel != -1)
4578 {
6ca53601 4579 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
7c1bef7a
MB
4580 ++ac;
4581 }
4582 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4583 if (pixel != -1)
4584 {
6ca53601 4585 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
7c1bef7a
MB
4586 ++ac;
4587 }
4588 }
6ca53601 4589#endif
7c1bef7a 4590
06a2c219
GM
4591 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4592 f->output_data.x->edit_widget, av, ac);
ec18280f
SM
4593
4594 {
4595 char *initial = "";
4596 char *val = initial;
4597 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
9cfc7da4
SM
4598#ifdef XtNarrowScrollbars
4599 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
4600#endif
ec18280f 4601 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
9cfc7da4 4602 if (xaw3d_arrow_scroll || val == initial)
ec18280f
SM
4603 { /* ARROW_SCROLL */
4604 xaw3d_arrow_scroll = True;
9cfc7da4 4605 /* Isn't that just a personal preference ? --Stef */
ec18280f
SM
4606 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4607 }
4608 }
7d0393cf 4609
06a2c219 4610 /* Define callbacks. */
ec18280f
SM
4611 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4612 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
06a2c219 4613 (XtPointer) bar);
7d0393cf 4614
06a2c219
GM
4615 /* Realize the widget. Only after that is the X window created. */
4616 XtRealizeWidget (widget);
7d0393cf 4617
ec18280f 4618#endif /* !USE_MOTIF */
06a2c219 4619
2a51c026 4620 /* Install an action hook that lets us detect when the user
06a2c219
GM
4621 finishes interacting with a scroll bar. */
4622 if (action_hook_id == 0)
4623 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
7d0393cf 4624
06a2c219
GM
4625 /* Remember X window and widget in the scroll bar vector. */
4626 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4627 xwindow = XtWindow (widget);
4628 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
4629
4630 UNBLOCK_INPUT;
4631}
488dd4c4 4632#endif /* not USE_GTK */
06a2c219
GM
4633
4634
4635/* Set the thumb size and position of scroll bar BAR. We are currently
4636 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4637
488dd4c4
JD
4638#ifdef USE_GTK
4639static void
4640x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4641 struct scroll_bar *bar;
4642 int portion, position, whole;
4643{
4644 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4645}
4646
4647#else /* not USE_GTK */
06a2c219
GM
4648static void
4649x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4650 struct scroll_bar *bar;
4651 int portion, position, whole;
f451eb13 4652{
e83dc917
GM
4653 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4654 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
06a2c219 4655 float top, shown;
f451eb13 4656
6f2a7a68
SM
4657 BLOCK_INPUT;
4658
4659#ifdef USE_MOTIF
4660
4661 /* We use an estimate of 30 chars per line rather than the real
4662 `portion' value. This has the disadvantage that the thumb size
4663 is not very representative, but it makes our life a lot easier.
4664 Otherwise, we have to constantly adjust the thumb size, which
4665 we can't always do quickly enough: while dragging, the size of
4666 the thumb might prevent the user from dragging the thumb all the
4667 way to the end. but Motif and some versions of Xaw3d don't allow
4668 updating the thumb size while dragging. Also, even if we can update
4669 its size, the update will often happen too late.
4670 If you don't believe it, check out revision 1.650 of xterm.c to see
4671 what hoops we were going through and the still poor behavior we got. */
0899d58c 4672 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
6f2a7a68
SM
4673 /* When the thumb is at the bottom, position == whole.
4674 So we need to increase `whole' to make space for the thumb. */
4675 whole += portion;
4676
4677 if (whole <= 0)
06a2c219
GM
4678 top = 0, shown = 1;
4679 else
f451eb13 4680 {
06a2c219
GM
4681 top = (float) position / whole;
4682 shown = (float) portion / whole;
4683 }
f451eb13 4684
6f2a7a68
SM
4685 if (NILP (bar->dragging))
4686 {
4687 int size, value;
06a2c219 4688
6f2a7a68
SM
4689 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4690 is the scroll bar's maximum and MIN is the scroll bar's minimum
4691 value. */
3c671a56
SM
4692 size = shown * XM_SB_MAX;
4693 size = min (size, XM_SB_MAX);
6f2a7a68 4694 size = max (size, 1);
06a2c219 4695
6f2a7a68 4696 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
3c671a56 4697 value = top * XM_SB_MAX;
6f2a7a68 4698 value = min (value, XM_SB_MAX - size);
7d0393cf 4699
06a2c219 4700 XmScrollBarSetValues (widget, value, size, 0, 0, False);
6f2a7a68 4701 }
ec18280f 4702#else /* !USE_MOTIF i.e. use Xaw */
6f2a7a68
SM
4703
4704 if (whole == 0)
4705 top = 0, shown = 1;
4706 else
4707 {
4708 top = (float) position / whole;
4709 shown = (float) portion / whole;
4710 }
4711
06a2c219 4712 {
ec18280f
SM
4713 float old_top, old_shown;
4714 Dimension height;
4715 XtVaGetValues (widget,
4716 XtNtopOfThumb, &old_top,
4717 XtNshown, &old_shown,
4718 XtNheight, &height,
4719 NULL);
4720
4721 /* Massage the top+shown values. */
4722 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
4723 top = max (0, min (1, top));
4724 else
4725 top = old_top;
4726 /* Keep two pixels available for moving the thumb down. */
4727 shown = max (0, min (1 - top - (2.0 / height), shown));
06a2c219
GM
4728
4729 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
4730 check that your system's configuration file contains a define
4731 for `NARROWPROTO'. See s/freebsd.h for an example. */
ec18280f 4732 if (top != old_top || shown != old_shown)
eb393530 4733 {
ec18280f 4734 if (NILP (bar->dragging))
eb393530 4735 XawScrollbarSetThumb (widget, top, shown);
06a2c219
GM
4736 else
4737 {
ec18280f
SM
4738 /* Try to make the scrolling a tad smoother. */
4739 if (!xaw3d_pick_top)
4740 shown = min (shown, old_shown);
7d0393cf 4741
ec18280f 4742 XawScrollbarSetThumb (widget, top, shown);
06a2c219 4743 }
06a2c219
GM
4744 }
4745 }
ec18280f 4746#endif /* !USE_MOTIF */
06a2c219
GM
4747
4748 UNBLOCK_INPUT;
f451eb13 4749}
488dd4c4 4750#endif /* not USE_GTK */
f451eb13 4751
06a2c219
GM
4752#endif /* USE_TOOLKIT_SCROLL_BARS */
4753
4754
4755\f
4756/************************************************************************
4757 Scroll bars, general
4758 ************************************************************************/
7d0393cf 4759
06a2c219
GM
4760/* Create a scroll bar and return the scroll bar vector for it. W is
4761 the Emacs window on which to create the scroll bar. TOP, LEFT,
f7ccf929 4762 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
06a2c219
GM
4763 scroll bar. */
4764
ab648270 4765static struct scroll_bar *
06a2c219
GM
4766x_scroll_bar_create (w, top, left, width, height)
4767 struct window *w;
f451eb13
JB
4768 int top, left, width, height;
4769{
06a2c219 4770 struct frame *f = XFRAME (w->frame);
334208b7
RS
4771 struct scroll_bar *bar
4772 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
f451eb13
JB
4773
4774 BLOCK_INPUT;
4775
eccc05db 4776#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
4777 x_create_toolkit_scroll_bar (f, bar);
4778#else /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
4779 {
4780 XSetWindowAttributes a;
4781 unsigned long mask;
5c187dee 4782 Window window;
06a2c219
GM
4783
4784 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4785 if (a.background_pixel == -1)
4786 a.background_pixel = f->output_data.x->background_pixel;
7d0393cf 4787
12ba150f 4788 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9a572e2a 4789 | ButtonMotionMask | PointerMotionHintMask
12ba150f 4790 | ExposureMask);
7a13e894 4791 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
f451eb13 4792
dbc4e1c1 4793 mask = (CWBackPixel | CWEventMask | CWCursor);
f451eb13 4794
06a2c219
GM
4795 /* Clear the area of W that will serve as a scroll bar. This is
4796 for the case that a window has been split horizontally. In
4797 this case, no clear_frame is generated to reduce flickering. */
7b49b9d2
GM
4798 if (width > 0 && height > 0)
4799 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4800 left, top, width,
4801 window_box_height (w), False);
06a2c219
GM
4802
4803 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4804 /* Position and size of scroll bar. */
4805 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4806 top,
4807 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4808 height,
4809 /* Border width, depth, class, and visual. */
4810 0,
4811 CopyFromParent,
4812 CopyFromParent,
4813 CopyFromParent,
4814 /* Attributes. */
4815 mask, &a);
4816 SET_SCROLL_BAR_X_WINDOW (bar, window);
f451eb13 4817 }
06a2c219 4818#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 4819
06a2c219 4820 XSETWINDOW (bar->window, w);
e0c1aef2
KH
4821 XSETINT (bar->top, top);
4822 XSETINT (bar->left, left);
4823 XSETINT (bar->width, width);
4824 XSETINT (bar->height, height);
4825 XSETINT (bar->start, 0);
4826 XSETINT (bar->end, 0);
12ba150f 4827 bar->dragging = Qnil;
f451eb13
JB
4828
4829 /* Add bar to its frame's list of scroll bars. */
334208b7 4830 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 4831 bar->prev = Qnil;
334208b7 4832 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
06a2c219 4833 if (!NILP (bar->next))
e0c1aef2 4834 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13 4835
06a2c219 4836 /* Map the window/widget. */
eccc05db 4837#ifdef USE_TOOLKIT_SCROLL_BARS
f964b4d7 4838 {
488dd4c4
JD
4839#ifdef USE_GTK
4840 xg_update_scrollbar_pos (f,
4841 SCROLL_BAR_X_WINDOW (bar),
4842 top,
4843 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4844 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
6bd8015d 4845 max (height, 1));
488dd4c4
JD
4846 xg_show_scroll_bar (SCROLL_BAR_X_WINDOW (bar));
4847#else /* not USE_GTK */
f964b4d7
GM
4848 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4849 XtConfigureWidget (scroll_bar,
4850 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4851 top,
4852 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4853 max (height, 1), 0);
4854 XtMapWidget (scroll_bar);
488dd4c4 4855#endif /* not USE_GTK */
f964b4d7 4856 }
06a2c219 4857#else /* not USE_TOOLKIT_SCROLL_BARS */
7f9c7f94 4858 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
06a2c219 4859#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
4860
4861 UNBLOCK_INPUT;
12ba150f 4862 return bar;
f451eb13
JB
4863}
4864
06a2c219 4865
12ba150f 4866/* Draw BAR's handle in the proper position.
7d0393cf 4867
12ba150f
JB
4868 If the handle is already drawn from START to END, don't bother
4869 redrawing it, unless REBUILD is non-zero; in that case, always
4870 redraw it. (REBUILD is handy for drawing the handle after expose
58769bee 4871 events.)
12ba150f
JB
4872
4873 Normally, we want to constrain the start and end of the handle to
06a2c219
GM
4874 fit inside its rectangle, but if the user is dragging the scroll
4875 bar handle, we want to let them drag it down all the way, so that
4876 the bar's top is as far down as it goes; otherwise, there's no way
4877 to move to the very end of the buffer. */
4878
5c187dee
GM
4879#ifndef USE_TOOLKIT_SCROLL_BARS
4880
f451eb13 4881static void
ab648270
JB
4882x_scroll_bar_set_handle (bar, start, end, rebuild)
4883 struct scroll_bar *bar;
f451eb13 4884 int start, end;
12ba150f 4885 int rebuild;
f451eb13 4886{
12ba150f 4887 int dragging = ! NILP (bar->dragging);
ab648270 4888 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 4889 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 4890 GC gc = f->output_data.x->normal_gc;
12ba150f
JB
4891
4892 /* If the display is already accurate, do nothing. */
4893 if (! rebuild
4894 && start == XINT (bar->start)
4895 && end == XINT (bar->end))
4896 return;
4897
f451eb13
JB
4898 BLOCK_INPUT;
4899
4900 {
d9cdbb3d
RS
4901 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
4902 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
4903 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
f451eb13
JB
4904
4905 /* Make sure the values are reasonable, and try to preserve
4906 the distance between start and end. */
12ba150f
JB
4907 {
4908 int length = end - start;
4909
4910 if (start < 0)
4911 start = 0;
4912 else if (start > top_range)
4913 start = top_range;
4914 end = start + length;
4915
4916 if (end < start)
4917 end = start;
4918 else if (end > top_range && ! dragging)
4919 end = top_range;
4920 }
f451eb13 4921
ab648270 4922 /* Store the adjusted setting in the scroll bar. */
e0c1aef2
KH
4923 XSETINT (bar->start, start);
4924 XSETINT (bar->end, end);
f451eb13 4925
12ba150f
JB
4926 /* Clip the end position, just for display. */
4927 if (end > top_range)
4928 end = top_range;
f451eb13 4929
ab648270 4930 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
12ba150f
JB
4931 below top positions, to make sure the handle is always at least
4932 that many pixels tall. */
ab648270 4933 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
f451eb13 4934
12ba150f
JB
4935 /* Draw the empty space above the handle. Note that we can't clear
4936 zero-height areas; that means "clear to end of window." */
4937 if (0 < start)
c5e6e06b
GM
4938 x_clear_area (FRAME_X_DISPLAY (f), w,
4939 /* x, y, width, height, and exposures. */
4940 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4941 VERTICAL_SCROLL_BAR_TOP_BORDER,
4942 inside_width, start,
4943 False);
f451eb13 4944
06a2c219
GM
4945 /* Change to proper foreground color if one is specified. */
4946 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
4947 XSetForeground (FRAME_X_DISPLAY (f), gc,
4948 f->output_data.x->scroll_bar_foreground_pixel);
4949
12ba150f 4950 /* Draw the handle itself. */
334208b7 4951 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
12ba150f 4952 /* x, y, width, height */
ab648270
JB
4953 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4954 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
12ba150f 4955 inside_width, end - start);
f451eb13 4956
06a2c219
GM
4957 /* Restore the foreground color of the GC if we changed it above. */
4958 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
4959 XSetForeground (FRAME_X_DISPLAY (f), gc,
4960 f->output_data.x->foreground_pixel);
f451eb13 4961
12ba150f
JB
4962 /* Draw the empty space below the handle. Note that we can't
4963 clear zero-height areas; that means "clear to end of window." */
4964 if (end < inside_height)
c5e6e06b
GM
4965 x_clear_area (FRAME_X_DISPLAY (f), w,
4966 /* x, y, width, height, and exposures. */
4967 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4968 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
4969 inside_width, inside_height - end,
4970 False);
f451eb13 4971
f451eb13
JB
4972 }
4973
f451eb13
JB
4974 UNBLOCK_INPUT;
4975}
4976
5c187dee 4977#endif /* !USE_TOOLKIT_SCROLL_BARS */
f451eb13 4978
06a2c219
GM
4979/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
4980 nil. */
58769bee 4981
12ba150f 4982static void
ab648270
JB
4983x_scroll_bar_remove (bar)
4984 struct scroll_bar *bar;
12ba150f 4985{
e83dc917 4986 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
4987 BLOCK_INPUT;
4988
eccc05db 4989#ifdef USE_TOOLKIT_SCROLL_BARS
488dd4c4
JD
4990#ifdef USE_GTK
4991 xg_remove_scroll_bar (f, SCROLL_BAR_X_WINDOW (bar));
4992#else /* not USE_GTK */
e83dc917 4993 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
488dd4c4 4994#endif /* not USE_GTK */
e83dc917
GM
4995#else
4996 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
4997#endif
7d0393cf 4998
ab648270
JB
4999 /* Disassociate this scroll bar from its window. */
5000 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
12ba150f
JB
5001
5002 UNBLOCK_INPUT;
5003}
5004
06a2c219 5005
12ba150f
JB
5006/* Set the handle of the vertical scroll bar for WINDOW to indicate
5007 that we are displaying PORTION characters out of a total of WHOLE
ab648270 5008 characters, starting at POSITION. If WINDOW has no scroll bar,
12ba150f 5009 create one. */
06a2c219 5010
12ba150f 5011static void
06a2c219
GM
5012XTset_vertical_scroll_bar (w, portion, whole, position)
5013 struct window *w;
f451eb13
JB
5014 int portion, whole, position;
5015{
06a2c219 5016 struct frame *f = XFRAME (w->frame);
ab648270 5017 struct scroll_bar *bar;
3c6ede7b 5018 int top, height, left, sb_left, width, sb_width;
0899d58c 5019 int window_y, window_height;
06a2c219 5020
3c6ede7b 5021 /* Get window dimensions. */
0899d58c 5022 window_box (w, -1, 0, &window_y, 0, &window_height);
3c6ede7b 5023 top = window_y;
0899d58c 5024 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
3c6ede7b 5025 height = window_height;
06a2c219 5026
3c6ede7b 5027 /* Compute the left edge of the scroll bar area. */
0899d58c 5028 left = WINDOW_SCROLL_BAR_AREA_X (w);
3c6ede7b
GM
5029
5030 /* Compute the width of the scroll bar which might be less than
5031 the width of the area reserved for the scroll bar. */
0899d58c
KS
5032 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
5033 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
06a2c219 5034 else
3c6ede7b 5035 sb_width = width;
12ba150f 5036
3c6ede7b
GM
5037 /* Compute the left edge of the scroll bar. */
5038#ifdef USE_TOOLKIT_SCROLL_BARS
0899d58c 5039 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
13fcb889
KS
5040 sb_left = (left +
5041 (WINDOW_RIGHTMOST_P (w)
5042 ? width - sb_width - (width - sb_width) / 2
5043 : 0));
3c6ede7b 5044 else
13fcb889
KS
5045 sb_left = (left +
5046 (WINDOW_LEFTMOST_P (w)
5047 ? (width - sb_width) / 2
5048 : width - sb_width));
3c6ede7b 5049#else
0899d58c 5050 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
7d0393cf 5051 sb_left = left + width - sb_width;
3c6ede7b
GM
5052 else
5053 sb_left = left;
5054#endif
7d0393cf 5055
ab648270 5056 /* Does the scroll bar exist yet? */
06a2c219 5057 if (NILP (w->vertical_scroll_bar))
3c6ede7b 5058 {
7b49b9d2 5059 if (width > 0 && height > 0)
b547b6e8
GM
5060 {
5061 BLOCK_INPUT;
5062 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5063 left, top, width, height, False);
5064 UNBLOCK_INPUT;
5065 }
7d0393cf 5066
3c6ede7b
GM
5067 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5068 }
f451eb13 5069 else
12ba150f
JB
5070 {
5071 /* It may just need to be moved and resized. */
06a2c219 5072 unsigned int mask = 0;
7d0393cf 5073
06a2c219
GM
5074 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5075
5076 BLOCK_INPUT;
5077
3c6ede7b 5078 if (sb_left != XINT (bar->left))
06a2c219 5079 mask |= CWX;
3c6ede7b 5080 if (top != XINT (bar->top))
06a2c219 5081 mask |= CWY;
3c6ede7b 5082 if (sb_width != XINT (bar->width))
06a2c219 5083 mask |= CWWidth;
7d0393cf 5084 if (height != XINT (bar->height))
06a2c219 5085 mask |= CWHeight;
7d0393cf 5086
06a2c219 5087#ifdef USE_TOOLKIT_SCROLL_BARS
fe6f39d9 5088
06a2c219
GM
5089 /* Move/size the scroll bar widget. */
5090 if (mask)
13fcb889
KS
5091 {
5092 /* Since toolkit scroll bars are smaller than the space reserved
5093 for them on the frame, we have to clear "under" them. */
5094 if (width > 0 && height > 0)
5095 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5096 left, top, width, height, False);
6bd8015d
JD
5097#ifdef USE_GTK
5098 xg_update_scrollbar_pos (f,
5099 SCROLL_BAR_X_WINDOW (bar),
5100 top,
5101 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5102 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM *2,
5103 max (height, 1));
5104#else /* not USE_GTK */
488dd4c4
JD
5105 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5106 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5107 top,
5108 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5109 max (height, 1), 0);
488dd4c4 5110#endif /* not USE_GTK */
6bd8015d 5111 }
06a2c219 5112#else /* not USE_TOOLKIT_SCROLL_BARS */
7d0393cf 5113
357e7376
GM
5114 /* Clear areas not covered by the scroll bar because of
5115 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
e1f6572f
RS
5116 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
5117 {
c5e6e06b
GM
5118 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5119 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5120 height, False);
5121 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5122 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5123 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5124 height, False);
e1f6572f 5125 }
357e7376
GM
5126
5127 /* Clear areas not covered by the scroll bar because it's not as
f7ccf929 5128 wide as the area reserved for it. This makes sure a
357e7376
GM
5129 previous mode line display is cleared after C-x 2 C-x 1, for
5130 example. */
5131 {
0899d58c 5132 int area_width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
357e7376 5133 int rest = area_width - sb_width;
38d2af0c
GM
5134 if (rest > 0 && height > 0)
5135 {
0899d58c 5136 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
38d2af0c
GM
5137 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5138 left + area_width - rest, top,
5139 rest, height, False);
5140 else
5141 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5142 left, top, rest, height, False);
5143 }
357e7376 5144 }
7d0393cf 5145
06a2c219
GM
5146 /* Move/size the scroll bar window. */
5147 if (mask)
5148 {
5149 XWindowChanges wc;
7d0393cf 5150
3c6ede7b
GM
5151 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5152 wc.y = top;
5153 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
5154 wc.height = height;
06a2c219
GM
5155 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
5156 mask, &wc);
5157 }
7d0393cf 5158
06a2c219
GM
5159#endif /* not USE_TOOLKIT_SCROLL_BARS */
5160
5161 /* Remember new settings. */
3c6ede7b
GM
5162 XSETINT (bar->left, sb_left);
5163 XSETINT (bar->top, top);
5164 XSETINT (bar->width, sb_width);
5165 XSETINT (bar->height, height);
7d0393cf 5166
06a2c219 5167 UNBLOCK_INPUT;
12ba150f 5168 }
f451eb13 5169
eccc05db 5170#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
5171 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5172#else /* not USE_TOOLKIT_SCROLL_BARS */
ab648270 5173 /* Set the scroll bar's current state, unless we're currently being
f451eb13 5174 dragged. */
12ba150f 5175 if (NILP (bar->dragging))
f451eb13 5176 {
92857db0 5177 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
f451eb13 5178
12ba150f 5179 if (whole == 0)
ab648270 5180 x_scroll_bar_set_handle (bar, 0, top_range, 0);
12ba150f
JB
5181 else
5182 {
43f868f5
JB
5183 int start = ((double) position * top_range) / whole;
5184 int end = ((double) (position + portion) * top_range) / whole;
ab648270 5185 x_scroll_bar_set_handle (bar, start, end, 0);
12ba150f 5186 }
f451eb13 5187 }
06a2c219 5188#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 5189
06a2c219 5190 XSETVECTOR (w->vertical_scroll_bar, bar);
f451eb13
JB
5191}
5192
12ba150f 5193
f451eb13 5194/* The following three hooks are used when we're doing a thorough
ab648270 5195 redisplay of the frame. We don't explicitly know which scroll bars
f451eb13 5196 are going to be deleted, because keeping track of when windows go
12ba150f
JB
5197 away is a real pain - "Can you say set-window-configuration, boys
5198 and girls?" Instead, we just assert at the beginning of redisplay
ab648270 5199 that *all* scroll bars are to be removed, and then save a scroll bar
12ba150f 5200 from the fiery pit when we actually redisplay its window. */
f451eb13 5201
ab648270
JB
5202/* Arrange for all scroll bars on FRAME to be removed at the next call
5203 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
06a2c219
GM
5204 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5205
58769bee 5206static void
ab648270 5207XTcondemn_scroll_bars (frame)
f451eb13
JB
5208 FRAME_PTR frame;
5209{
f9e24cb9
RS
5210 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5211 while (! NILP (FRAME_SCROLL_BARS (frame)))
5212 {
5213 Lisp_Object bar;
5214 bar = FRAME_SCROLL_BARS (frame);
5215 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
5216 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5217 XSCROLL_BAR (bar)->prev = Qnil;
5218 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5219 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5220 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
5221 }
f451eb13
JB
5222}
5223
fa2dfc30 5224
06a2c219 5225/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
12ba150f 5226 Note that WINDOW isn't necessarily condemned at all. */
fa2dfc30 5227
f451eb13 5228static void
ab648270 5229XTredeem_scroll_bar (window)
12ba150f 5230 struct window *window;
f451eb13 5231{
ab648270 5232 struct scroll_bar *bar;
fa2dfc30 5233 struct frame *f;
12ba150f 5234
ab648270
JB
5235 /* We can't redeem this window's scroll bar if it doesn't have one. */
5236 if (NILP (window->vertical_scroll_bar))
12ba150f
JB
5237 abort ();
5238
ab648270 5239 bar = XSCROLL_BAR (window->vertical_scroll_bar);
12ba150f
JB
5240
5241 /* Unlink it from the condemned list. */
fa2dfc30
GM
5242 f = XFRAME (WINDOW_FRAME (window));
5243 if (NILP (bar->prev))
5244 {
5245 /* If the prev pointer is nil, it must be the first in one of
5246 the lists. */
5247 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5248 /* It's not condemned. Everything's fine. */
5249 return;
5250 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5251 window->vertical_scroll_bar))
5252 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
5253 else
5254 /* If its prev pointer is nil, it must be at the front of
5255 one or the other! */
5256 abort ();
5257 }
5258 else
5259 XSCROLL_BAR (bar->prev)->next = bar->next;
12ba150f 5260
fa2dfc30
GM
5261 if (! NILP (bar->next))
5262 XSCROLL_BAR (bar->next)->prev = bar->prev;
12ba150f 5263
fa2dfc30
GM
5264 bar->next = FRAME_SCROLL_BARS (f);
5265 bar->prev = Qnil;
5266 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5267 if (! NILP (bar->next))
5268 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13
JB
5269}
5270
ab648270
JB
5271/* Remove all scroll bars on FRAME that haven't been saved since the
5272 last call to `*condemn_scroll_bars_hook'. */
06a2c219 5273
f451eb13 5274static void
ab648270 5275XTjudge_scroll_bars (f)
12ba150f 5276 FRAME_PTR f;
f451eb13 5277{
12ba150f 5278 Lisp_Object bar, next;
f451eb13 5279
ab648270 5280 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
cf7cb199
JB
5281
5282 /* Clear out the condemned list now so we won't try to process any
ab648270
JB
5283 more events on the hapless scroll bars. */
5284 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
cf7cb199
JB
5285
5286 for (; ! NILP (bar); bar = next)
f451eb13 5287 {
ab648270 5288 struct scroll_bar *b = XSCROLL_BAR (bar);
12ba150f 5289
ab648270 5290 x_scroll_bar_remove (b);
12ba150f
JB
5291
5292 next = b->next;
5293 b->next = b->prev = Qnil;
f451eb13 5294 }
12ba150f 5295
ab648270 5296 /* Now there should be no references to the condemned scroll bars,
12ba150f 5297 and they should get garbage-collected. */
f451eb13
JB
5298}
5299
5300
3c671a56 5301#ifndef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
5302/* Handle an Expose or GraphicsExpose event on a scroll bar. This
5303 is a no-op when using toolkit scroll bars.
ab648270
JB
5304
5305 This may be called from a signal handler, so we have to ignore GC
5306 mark bits. */
06a2c219 5307
f451eb13 5308static void
ab648270
JB
5309x_scroll_bar_expose (bar, event)
5310 struct scroll_bar *bar;
f451eb13
JB
5311 XEvent *event;
5312{
ab648270 5313 Window w = SCROLL_BAR_X_WINDOW (bar);
334208b7 5314 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 5315 GC gc = f->output_data.x->normal_gc;
3cbd2e0b 5316 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
12ba150f 5317
f451eb13
JB
5318 BLOCK_INPUT;
5319
ab648270 5320 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
f451eb13 5321
06a2c219 5322 /* Draw a one-pixel border just inside the edges of the scroll bar. */
334208b7 5323 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13
JB
5324
5325 /* x, y, width, height */
d9cdbb3d 5326 0, 0,
3cbd2e0b 5327 XINT (bar->width) - 1 - width_trim - width_trim,
d9cdbb3d 5328 XINT (bar->height) - 1);
7d0393cf 5329
f451eb13 5330 UNBLOCK_INPUT;
06a2c219 5331
f451eb13 5332}
3c671a56 5333#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 5334
ab648270 5335/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
3b8f9651 5336 is set to something other than NO_EVENT, it is enqueued.
ab648270
JB
5337
5338 This may be called from a signal handler, so we have to ignore GC
5339 mark bits. */
06a2c219 5340
5c187dee 5341
f451eb13 5342static void
ab648270
JB
5343x_scroll_bar_handle_click (bar, event, emacs_event)
5344 struct scroll_bar *bar;
f451eb13
JB
5345 XEvent *event;
5346 struct input_event *emacs_event;
5347{
0299d313 5348 if (! GC_WINDOWP (bar->window))
12ba150f
JB
5349 abort ();
5350
3b8f9651 5351 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
69388238 5352 emacs_event->code = event->xbutton.button - Button1;
0299d313 5353 emacs_event->modifiers
7d0393cf 5354 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
0299d313
RS
5355 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5356 event->xbutton.state)
5357 | (event->type == ButtonRelease
5358 ? up_modifier
5359 : down_modifier));
12ba150f 5360 emacs_event->frame_or_window = bar->window;
0f8aabe9 5361 emacs_event->arg = Qnil;
f451eb13 5362 emacs_event->timestamp = event->xbutton.time;
12ba150f 5363 {
06a2c219 5364#if 0
d9cdbb3d 5365 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
0299d313 5366 int internal_height
d9cdbb3d 5367 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 5368#endif
0299d313 5369 int top_range
d9cdbb3d 5370 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ab648270 5371 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
12ba150f
JB
5372
5373 if (y < 0) y = 0;
5374 if (y > top_range) y = top_range;
5375
5376 if (y < XINT (bar->start))
ab648270
JB
5377 emacs_event->part = scroll_bar_above_handle;
5378 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5379 emacs_event->part = scroll_bar_handle;
12ba150f 5380 else
ab648270 5381 emacs_event->part = scroll_bar_below_handle;
929787e1
JB
5382
5383 /* Just because the user has clicked on the handle doesn't mean
5116f055
JB
5384 they want to drag it. Lisp code needs to be able to decide
5385 whether or not we're dragging. */
929787e1 5386#if 0
12ba150f
JB
5387 /* If the user has just clicked on the handle, record where they're
5388 holding it. */
5389 if (event->type == ButtonPress
ab648270 5390 && emacs_event->part == scroll_bar_handle)
e0c1aef2 5391 XSETINT (bar->dragging, y - XINT (bar->start));
929787e1 5392#endif
12ba150f 5393
257f40f2 5394#ifndef USE_TOOLKIT_SCROLL_BARS
12ba150f
JB
5395 /* If the user has released the handle, set it to its final position. */
5396 if (event->type == ButtonRelease
5397 && ! NILP (bar->dragging))
5398 {
5399 int new_start = y - XINT (bar->dragging);
5400 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
f451eb13 5401
ab648270 5402 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
12ba150f
JB
5403 bar->dragging = Qnil;
5404 }
257f40f2 5405#endif
f451eb13 5406
5116f055
JB
5407 /* Same deal here as the other #if 0. */
5408#if 0
58769bee 5409 /* Clicks on the handle are always reported as occurring at the top of
12ba150f 5410 the handle. */
ab648270 5411 if (emacs_event->part == scroll_bar_handle)
12ba150f
JB
5412 emacs_event->x = bar->start;
5413 else
e0c1aef2 5414 XSETINT (emacs_event->x, y);
5116f055 5415#else
e0c1aef2 5416 XSETINT (emacs_event->x, y);
5116f055 5417#endif
f451eb13 5418
e0c1aef2 5419 XSETINT (emacs_event->y, top_range);
12ba150f
JB
5420 }
5421}
f451eb13 5422
257f40f2
JD
5423#ifndef USE_TOOLKIT_SCROLL_BARS
5424
ab648270
JB
5425/* Handle some mouse motion while someone is dragging the scroll bar.
5426
5427 This may be called from a signal handler, so we have to ignore GC
5428 mark bits. */
06a2c219 5429
f451eb13 5430static void
ab648270
JB
5431x_scroll_bar_note_movement (bar, event)
5432 struct scroll_bar *bar;
f451eb13
JB
5433 XEvent *event;
5434{
39d8bb4d
KH
5435 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
5436
f451eb13
JB
5437 last_mouse_movement_time = event->xmotion.time;
5438
39d8bb4d 5439 f->mouse_moved = 1;
e0c1aef2 5440 XSETVECTOR (last_mouse_scroll_bar, bar);
f451eb13
JB
5441
5442 /* If we're dragging the bar, display it. */
ab648270 5443 if (! GC_NILP (bar->dragging))
f451eb13
JB
5444 {
5445 /* Where should the handle be now? */
12ba150f 5446 int new_start = event->xmotion.y - XINT (bar->dragging);
f451eb13 5447
12ba150f 5448 if (new_start != XINT (bar->start))
f451eb13 5449 {
12ba150f 5450 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
58769bee 5451
ab648270 5452 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
f451eb13
JB
5453 }
5454 }
f451eb13
JB
5455}
5456
5c187dee
GM
5457#endif /* !USE_TOOLKIT_SCROLL_BARS */
5458
12ba150f 5459/* Return information to the user about the current position of the mouse
ab648270 5460 on the scroll bar. */
06a2c219 5461
12ba150f 5462static void
334208b7
RS
5463x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
5464 FRAME_PTR *fp;
12ba150f 5465 Lisp_Object *bar_window;
ab648270 5466 enum scroll_bar_part *part;
12ba150f
JB
5467 Lisp_Object *x, *y;
5468 unsigned long *time;
5469{
ab648270 5470 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
334208b7
RS
5471 Window w = SCROLL_BAR_X_WINDOW (bar);
5472 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f 5473 int win_x, win_y;
559cb2fb
JB
5474 Window dummy_window;
5475 int dummy_coord;
5476 unsigned int dummy_mask;
12ba150f 5477
cf7cb199
JB
5478 BLOCK_INPUT;
5479
ab648270 5480 /* Get the mouse's position relative to the scroll bar window, and
12ba150f 5481 report that. */
334208b7 5482 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
12ba150f 5483
559cb2fb
JB
5484 /* Root, child, root x and root y. */
5485 &dummy_window, &dummy_window,
5486 &dummy_coord, &dummy_coord,
12ba150f 5487
559cb2fb
JB
5488 /* Position relative to scroll bar. */
5489 &win_x, &win_y,
12ba150f 5490
559cb2fb
JB
5491 /* Mouse buttons and modifier keys. */
5492 &dummy_mask))
7a13e894 5493 ;
559cb2fb
JB
5494 else
5495 {
06a2c219 5496#if 0
559cb2fb 5497 int inside_height
d9cdbb3d 5498 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
06a2c219 5499#endif
559cb2fb 5500 int top_range
d9cdbb3d 5501 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
559cb2fb
JB
5502
5503 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5504
5505 if (! NILP (bar->dragging))
5506 win_y -= XINT (bar->dragging);
5507
5508 if (win_y < 0)
5509 win_y = 0;
5510 if (win_y > top_range)
5511 win_y = top_range;
5512
334208b7 5513 *fp = f;
7a13e894 5514 *bar_window = bar->window;
559cb2fb
JB
5515
5516 if (! NILP (bar->dragging))
5517 *part = scroll_bar_handle;
5518 else if (win_y < XINT (bar->start))
5519 *part = scroll_bar_above_handle;
5520 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5521 *part = scroll_bar_handle;
5522 else
5523 *part = scroll_bar_below_handle;
12ba150f 5524
e0c1aef2
KH
5525 XSETINT (*x, win_y);
5526 XSETINT (*y, top_range);
12ba150f 5527
39d8bb4d 5528 f->mouse_moved = 0;
559cb2fb
JB
5529 last_mouse_scroll_bar = Qnil;
5530 }
12ba150f 5531
559cb2fb 5532 *time = last_mouse_movement_time;
cf7cb199 5533
cf7cb199 5534 UNBLOCK_INPUT;
12ba150f
JB
5535}
5536
f451eb13 5537
dbc4e1c1 5538/* The screen has been cleared so we may have changed foreground or
ab648270
JB
5539 background colors, and the scroll bars may need to be redrawn.
5540 Clear out the scroll bars, and ask for expose events, so we can
dbc4e1c1
JB
5541 redraw them. */
5542
dfcf069d 5543void
ab648270 5544x_scroll_bar_clear (f)
dbc4e1c1
JB
5545 FRAME_PTR f;
5546{
06a2c219 5547#ifndef USE_TOOLKIT_SCROLL_BARS
dbc4e1c1
JB
5548 Lisp_Object bar;
5549
b80c363e
RS
5550 /* We can have scroll bars even if this is 0,
5551 if we just turned off scroll bar mode.
5552 But in that case we should not clear them. */
5553 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5554 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5555 bar = XSCROLL_BAR (bar)->next)
c5e6e06b
GM
5556 XClearArea (FRAME_X_DISPLAY (f),
5557 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
b80c363e 5558 0, 0, 0, 0, True);
06a2c219 5559#endif /* not USE_TOOLKIT_SCROLL_BARS */
dbc4e1c1
JB
5560}
5561
09756a85 5562\f
f451eb13 5563/* The main X event-reading loop - XTread_socket. */
dc6f92b8 5564
042c33d3 5565#if 0
06a2c219 5566/* Time stamp of enter window event. This is only used by XTread_socket,
dc6f92b8
JB
5567 but we have to put it out here, since static variables within functions
5568 sometimes don't work. */
06a2c219 5569
dc6f92b8 5570static Time enter_timestamp;
042c33d3 5571#endif
dc6f92b8 5572
11edeb03 5573/* This holds the state XLookupString needs to implement dead keys
58769bee 5574 and other tricks known as "compose processing". _X Window System_
11edeb03
JB
5575 says that a portable program can't use this, but Stephen Gildea assures
5576 me that letting the compiler initialize it to zeros will work okay.
5577
5578 This must be defined outside of XTread_socket, for the same reasons
f7d40b3b 5579 given for enter_timestamp, above. */
06a2c219 5580
11edeb03
JB
5581static XComposeStatus compose_status;
5582
10e6549c
RS
5583/* Record the last 100 characters stored
5584 to help debug the loss-of-chars-during-GC problem. */
06a2c219 5585
2224b905
RS
5586static int temp_index;
5587static short temp_buffer[100];
10e6549c 5588
89079179
KS
5589#define STORE_KEYSYM_FOR_DEBUG(keysym) \
5590 if (temp_index == sizeof temp_buffer / sizeof (short)) \
5591 temp_index = 0; \
5592 temp_buffer[temp_index++] = (keysym)
5593
7a13e894
RS
5594/* Set this to nonzero to fake an "X I/O error"
5595 on a particular display. */
06a2c219 5596
7a13e894
RS
5597struct x_display_info *XTread_socket_fake_io_error;
5598
2224b905
RS
5599/* When we find no input here, we occasionally do a no-op command
5600 to verify that the X server is still running and we can still talk with it.
5601 We try all the open displays, one by one.
5602 This variable is used for cycling thru the displays. */
06a2c219 5603
2224b905
RS
5604static struct x_display_info *next_noop_dpyinfo;
5605
06a2c219
GM
5606#define SET_SAVED_MENU_EVENT(size) \
5607 do \
5608 { \
5609 if (f->output_data.x->saved_menu_event == 0) \
5610 f->output_data.x->saved_menu_event \
5611 = (XEvent *) xmalloc (sizeof (XEvent)); \
5612 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9b516537
AS
5613 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT; \
5614 XSETFRAME (inev.ie.frame_or_window, f); \
06a2c219
GM
5615 } \
5616 while (0)
5617
8805890a 5618#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
06a2c219 5619#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8805890a 5620
dc6f92b8 5621
bf338245
JD
5622enum
5623{
5624 X_EVENT_NORMAL,
5625 X_EVENT_GOTO_OUT,
5626 X_EVENT_DROP
5627};
dc6f92b8 5628
1fcfb866
JD
5629/* Filter events for the current X input method.
5630 DPYINFO is the display this event is for.
5631 EVENT is the X event to filter.
5632
5633 Returns non-zero if the event was filtered, caller shall not process
5634 this event further.
5635 Returns zero if event is wasn't filtered. */
177c0ea7 5636
1fcfb866
JD
5637#ifdef HAVE_X_I18N
5638static int
5639x_filter_event (dpyinfo, event)
5640 struct x_display_info *dpyinfo;
5641 XEvent *event;
5642{
5643 /* XFilterEvent returns non-zero if the input method has
5644 consumed the event. We pass the frame's X window to
5645 XFilterEvent because that's the one for which the IC
5646 was created. */
5647
5648 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5649 event->xclient.window);
5650
5651 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5652}
5653#endif
5654
488dd4c4 5655#ifdef USE_GTK
488dd4c4
JD
5656static int current_count;
5657static int current_finish;
89079179 5658static struct input_event *current_hold_quit;
488dd4c4
JD
5659
5660/* This is the filter function invoked by the GTK event loop.
5661 It is invoked before the XEvent is translated to a GdkEvent,
89079179 5662 so we have a chance to act on the event before GTK. */
488dd4c4
JD
5663static GdkFilterReturn
5664event_handler_gdk (gxev, ev, data)
5665 GdkXEvent *gxev;
5666 GdkEvent *ev;
5667 gpointer data;
5668{
810f2256 5669 XEvent *xev = (XEvent *) gxev;
488dd4c4 5670
89079179 5671 if (current_count >= 0)
1fcfb866 5672 {
810f2256
JD
5673 struct x_display_info *dpyinfo;
5674
5675 dpyinfo = x_display_info_for_display (xev->xany.display);
5676
1fcfb866
JD
5677#ifdef HAVE_X_I18N
5678 /* Filter events for the current X input method.
5679 GTK calls XFilterEvent but not for key press and release,
5680 so we do it here. */
5681 if (xev->type == KeyPress || xev->type == KeyRelease)
810f2256 5682 if (dpyinfo && x_filter_event (dpyinfo, xev))
1fcfb866
JD
5683 return GDK_FILTER_REMOVE;
5684#endif
810f2256
JD
5685
5686 if (! dpyinfo)
5687 current_finish = X_EVENT_NORMAL;
5688 else
89079179
KS
5689 {
5690 current_count +=
0666d82c 5691 handle_one_xevent (dpyinfo, xev, &current_finish,
89079179
KS
5692 current_hold_quit);
5693 }
1fcfb866 5694 }
488dd4c4 5695 else
810f2256 5696 current_finish = x_dispatch_event (xev, xev->xany.display);
488dd4c4
JD
5697
5698 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5699 return GDK_FILTER_REMOVE;
5700
5701 return GDK_FILTER_CONTINUE;
5702}
5703#endif /* USE_GTK */
5704
5705
bf338245 5706/* Handles the XEvent EVENT on display DPYINFO.
177c0ea7 5707
bf338245
JD
5708 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5709 *FINISH is zero if caller should continue reading events.
5710 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5711
bf338245 5712 We return the number of characters stored into the buffer. */
177c0ea7 5713
6f2a7a68 5714static int
89079179 5715handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
bf338245
JD
5716 struct x_display_info *dpyinfo;
5717 XEvent *eventp;
bf338245 5718 int *finish;
89079179 5719 struct input_event *hold_quit;
dc6f92b8 5720{
9b516537
AS
5721 union {
5722 struct input_event ie;
5723 struct selection_input_event sie;
5724 } inev;
dc6f92b8 5725 int count = 0;
89079179 5726 int do_help = 0;
dc6f92b8 5727 int nbytes = 0;
f676886a 5728 struct frame *f;
379b5ac0 5729 struct coding_system coding;
bf338245 5730 XEvent event = *eventp;
dc6f92b8 5731
bf338245 5732 *finish = X_EVENT_NORMAL;
177c0ea7 5733
9b516537
AS
5734 EVENT_INIT (inev.ie);
5735 inev.ie.kind = NO_EVENT;
5736 inev.ie.arg = Qnil;
89079179 5737
bf338245 5738 switch (event.type)
7a13e894 5739 {
bf338245
JD
5740 case ClientMessage:
5741 {
5742 if (event.xclient.message_type
5743 == dpyinfo->Xatom_wm_protocols
5744 && event.xclient.format == 32)
5745 {
5746 if (event.xclient.data.l[0]
5747 == dpyinfo->Xatom_wm_take_focus)
5748 {
5749 /* Use x_any_window_to_frame because this
5750 could be the shell widget window
5751 if the frame has no title bar. */
5752 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6c183ba5 5753#ifdef HAVE_X_I18N
bf338245
JD
5754 /* Not quite sure this is needed -pd */
5755 if (f && FRAME_XIC (f))
5756 XSetICFocus (FRAME_XIC (f));
6c183ba5 5757#endif
f1da8f06
GM
5758#if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5759 instructs the WM to set the input focus automatically for
5760 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5761 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5762 it has set the focus. So, XSetInputFocus below is not
5763 needed.
5764
5765 The call to XSetInputFocus below has also caused trouble. In
5766 cases where the XSetInputFocus done by the WM and the one
5767 below are temporally close (on a fast machine), the call
5768 below can generate additional FocusIn events which confuse
5769 Emacs. */
7d0393cf 5770
bf338245
JD
5771 /* Since we set WM_TAKE_FOCUS, we must call
5772 XSetInputFocus explicitly. But not if f is null,
5773 since that might be an event for a deleted frame. */
5774 if (f)
5775 {
5776 Display *d = event.xclient.display;
5777 /* Catch and ignore errors, in case window has been
5778 iconified by a window manager such as GWM. */
5779 int count = x_catch_errors (d);
5780 XSetInputFocus (d, event.xclient.window,
5781 /* The ICCCM says this is
5782 the only valid choice. */
5783 RevertToParent,
5784 event.xclient.data.l[1]);
5785 /* This is needed to detect the error
5786 if there is an error. */
5787 XSync (d, False);
5788 x_uncatch_errors (d, count);
5789 }
5790 /* Not certain about handling scroll bars here */
f1da8f06 5791#endif /* 0 */
89079179 5792 goto done;
bf338245 5793 }
89079179
KS
5794
5795 if (event.xclient.data.l[0]
bf338245
JD
5796 == dpyinfo->Xatom_wm_save_yourself)
5797 {
5798 /* Save state modify the WM_COMMAND property to
5799 something which can reinstate us. This notifies
5800 the session manager, who's looking for such a
5801 PropertyNotify. Can restart processing when
5802 a keyboard or mouse event arrives. */
5803 /* If we have a session manager, don't set this.
5804 KDE will then start two Emacsen, one for the
5805 session manager and one for this. */
5f30b957 5806#ifdef HAVE_X_SM
89079179 5807 if (! x_session_have_connection ())
5f30b957 5808#endif
bf338245
JD
5809 {
5810 f = x_top_window_to_frame (dpyinfo,
5811 event.xclient.window);
5812 /* This is just so we only give real data once
5813 for a single Emacs process. */
5814 if (f == SELECTED_FRAME ())
5815 XSetCommand (FRAME_X_DISPLAY (f),
5816 event.xclient.window,
5817 initial_argv, initial_argc);
5818 else if (f)
5819 XSetCommand (FRAME_X_DISPLAY (f),
5820 event.xclient.window,
5821 0, 0);
5822 }
89079179 5823 goto done;
bf338245 5824 }
89079179
KS
5825
5826 if (event.xclient.data.l[0]
5827 == dpyinfo->Xatom_wm_delete_window)
bf338245 5828 {
89079179 5829 f = x_any_window_to_frame (dpyinfo,
bf338245 5830 event.xclient.window);
89079179
KS
5831 if (!f)
5832 goto OTHER; /* May be a dialog that is to be removed */
7a13e894 5833
9b516537
AS
5834 inev.ie.kind = DELETE_WINDOW_EVENT;
5835 XSETFRAME (inev.ie.frame_or_window, f);
89079179 5836 goto done;
bf338245 5837 }
89079179
KS
5838
5839 goto done;
bf338245 5840 }
89079179
KS
5841
5842 if (event.xclient.message_type
bf338245
JD
5843 == dpyinfo->Xatom_wm_configure_denied)
5844 {
89079179 5845 goto done;
bf338245 5846 }
89079179
KS
5847
5848 if (event.xclient.message_type
5849 == dpyinfo->Xatom_wm_window_moved)
bf338245
JD
5850 {
5851 int new_x, new_y;
89079179 5852 f = x_window_to_frame (dpyinfo, event.xclient.window);
bf338245
JD
5853
5854 new_x = event.xclient.data.s[0];
5855 new_y = event.xclient.data.s[1];
5856
5857 if (f)
5858 {
0899d58c
KS
5859 f->left_pos = new_x;
5860 f->top_pos = new_y;
bf338245 5861 }
89079179 5862 goto done;
bf338245 5863 }
89079179 5864
0fdff6bb 5865#ifdef HACK_EDITRES
89079179
KS
5866 if (event.xclient.message_type
5867 == dpyinfo->Xatom_editres)
bf338245 5868 {
89079179 5869 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
bf338245
JD
5870 _XEditResCheckMessages (f->output_data.x->widget, NULL,
5871 &event, NULL);
89079179 5872 goto done;
bf338245 5873 }
0fdff6bb 5874#endif /* HACK_EDITRES */
89079179
KS
5875
5876 if ((event.xclient.message_type
5877 == dpyinfo->Xatom_DONE)
5878 || (event.xclient.message_type
5879 == dpyinfo->Xatom_PAGE))
bf338245
JD
5880 {
5881 /* Ghostview job completed. Kill it. We could
5882 reply with "Next" if we received "Page", but we
5883 currently never do because we are interested in
5884 images, only, which should have 1 page. */
5885 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
89079179 5886 f = x_window_to_frame (dpyinfo, event.xclient.window);
bf338245
JD
5887 x_kill_gs_process (pixmap, f);
5888 expose_frame (f, 0, 0, 0, 0);
89079179 5889 goto done;
bf338245 5890 }
89079179 5891
06a2c219 5892#ifdef USE_TOOLKIT_SCROLL_BARS
bf338245
JD
5893 /* Scroll bar callbacks send a ClientMessage from which
5894 we construct an input_event. */
89079179
KS
5895 if (event.xclient.message_type
5896 == dpyinfo->Xatom_Scrollbar)
bf338245 5897 {
9b516537 5898 x_scroll_bar_to_input_event (&event, &inev.ie);
89079179
KS
5899 *finish = X_EVENT_GOTO_OUT;
5900 goto done;
bf338245 5901 }
06a2c219 5902#endif /* USE_TOOLKIT_SCROLL_BARS */
e69745bb 5903
89079179 5904 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
e69745bb 5905
89079179
KS
5906 if (!f)
5907 goto OTHER;
5908
9b516537 5909 if (x_handle_dnd_message (f, &event.xclient, dpyinfo, &inev.ie))
89079179 5910 *finish = X_EVENT_DROP;
bf338245
JD
5911 }
5912 break;
dc6f92b8 5913
bf338245 5914 case SelectionNotify:
3afe33e7 5915#ifdef USE_X_TOOLKIT
bf338245
JD
5916 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
5917 goto OTHER;
3afe33e7 5918#endif /* not USE_X_TOOLKIT */
bf338245
JD
5919 x_handle_selection_notify (&event.xselection);
5920 break;
d56a553a 5921
bf338245 5922 case SelectionClear: /* Someone has grabbed ownership. */
3afe33e7 5923#ifdef USE_X_TOOLKIT
bf338245
JD
5924 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
5925 goto OTHER;
3afe33e7 5926#endif /* USE_X_TOOLKIT */
bf338245
JD
5927 {
5928 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
d56a553a 5929
9b516537
AS
5930 inev.ie.kind = SELECTION_CLEAR_EVENT;
5931 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
5932 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
5933 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
5934 inev.ie.frame_or_window = Qnil;
bf338245
JD
5935 }
5936 break;
dc6f92b8 5937
bf338245 5938 case SelectionRequest: /* Someone wants our selection. */
3afe33e7 5939#ifdef USE_X_TOOLKIT
bf338245
JD
5940 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
5941 goto OTHER;
3afe33e7 5942#endif /* USE_X_TOOLKIT */
958f04e8 5943 {
bf338245
JD
5944 XSelectionRequestEvent *eventp
5945 = (XSelectionRequestEvent *) &event;
5946
9b516537
AS
5947 inev.ie.kind = SELECTION_REQUEST_EVENT;
5948 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
5949 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
5950 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
5951 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
5952 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
5953 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
5954 inev.ie.frame_or_window = Qnil;
958f04e8 5955 }
bf338245 5956 break;
7a13e894 5957
bf338245 5958 case PropertyNotify:
1d2b2268
GM
5959#if 0 /* This is plain wrong. In the case that we are waiting for a
5960 PropertyNotify used as an ACK in incremental selection
5961 transfer, the property will be on the receiver's window. */
5962#if defined USE_X_TOOLKIT
bf338245
JD
5963 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
5964 goto OTHER;
1d2b2268
GM
5965#endif
5966#endif
bf338245
JD
5967 x_handle_property_notify (&event.xproperty);
5968 goto OTHER;
dc6f92b8 5969
bf338245
JD
5970 case ReparentNotify:
5971 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
5972 if (f)
5973 {
5974 int x, y;
5975 f->output_data.x->parent_desc = event.xreparent.parent;
5976 x_real_positions (f, &x, &y);
0899d58c
KS
5977 f->left_pos = x;
5978 f->top_pos = y;
c1f0671a
JD
5979
5980 /* Perhaps reparented due to a WM restart. Reset this. */
5981 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
bf338245 5982 }
15213141 5983 goto OTHER;
3bd330d4 5984
bf338245
JD
5985 case Expose:
5986 f = x_window_to_frame (dpyinfo, event.xexpose.window);
5987 if (f)
5988 {
5989 x_check_fullscreen (f);
d0fa3e56 5990
42d02da0
JD
5991#ifdef USE_GTK
5992 /* This seems to be needed for GTK 2.6. */
5993 x_clear_area (event.xexpose.display,
5994 event.xexpose.window,
5995 event.xexpose.x, event.xexpose.y,
5996 event.xexpose.width, event.xexpose.height,
5997 FALSE);
5998#endif
bf338245
JD
5999 if (f->async_visible == 0)
6000 {
6001 f->async_visible = 1;
6002 f->async_iconified = 0;
6003 f->output_data.x->has_been_visible = 1;
6004 SET_FRAME_GARBAGED (f);
6005 }
6006 else
0899d58c
KS
6007 expose_frame (f,
6008 event.xexpose.x, event.xexpose.y,
bf338245
JD
6009 event.xexpose.width, event.xexpose.height);
6010 }
6011 else
6012 {
12949a7f 6013#ifndef USE_TOOLKIT_SCROLL_BARS
bf338245 6014 struct scroll_bar *bar;
12949a7f 6015#endif
01f67d2c 6016#if defined USE_LUCID
bf338245
JD
6017 /* Submenus of the Lucid menu bar aren't widgets
6018 themselves, so there's no way to dispatch events
6019 to them. Recognize this case separately. */
6020 {
6021 Widget widget
6022 = x_window_to_menu_bar (event.xexpose.window);
6023 if (widget)
6024 xlwmenu_redisplay (widget);
6025 }
01f67d2c
PJ
6026#endif /* USE_LUCID */
6027
06a2c219 6028#ifdef USE_TOOLKIT_SCROLL_BARS
bf338245
JD
6029 /* Dispatch event to the widget. */
6030 goto OTHER;
06a2c219 6031#else /* not USE_TOOLKIT_SCROLL_BARS */
810f2256
JD
6032 bar = x_window_to_scroll_bar (event.xexpose.display,
6033 event.xexpose.window);
58769bee 6034
bf338245
JD
6035 if (bar)
6036 x_scroll_bar_expose (bar, &event);
3afe33e7 6037#ifdef USE_X_TOOLKIT
bf338245
JD
6038 else
6039 goto OTHER;
3afe33e7 6040#endif /* USE_X_TOOLKIT */
06a2c219 6041#endif /* not USE_TOOLKIT_SCROLL_BARS */
bf338245
JD
6042 }
6043 break;
dc6f92b8 6044
bf338245
JD
6045 case GraphicsExpose: /* This occurs when an XCopyArea's
6046 source area was obscured or not
6047 available. */
6048 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
6049 if (f)
6050 {
6051 expose_frame (f,
6052 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
6053 event.xgraphicsexpose.width,
6054 event.xgraphicsexpose.height);
6055 }
3afe33e7 6056#ifdef USE_X_TOOLKIT
bf338245
JD
6057 else
6058 goto OTHER;
3afe33e7 6059#endif /* USE_X_TOOLKIT */
bf338245 6060 break;
7d0393cf 6061
bf338245
JD
6062 case NoExpose: /* This occurs when an XCopyArea's
6063 source area was completely
6064 available. */
6065 break;
dc6f92b8 6066
bf338245
JD
6067 case UnmapNotify:
6068 /* Redo the mouse-highlight after the tooltip has gone. */
6069 if (event.xmap.window == tip_window)
6070 {
6071 tip_window = 0;
6072 redo_mouse_highlight ();
6073 }
bddd097c 6074
bf338245
JD
6075 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
6076 if (f) /* F may no longer exist if
6077 the frame was deleted. */
6078 {
6079 /* While a frame is unmapped, display generation is
6080 disabled; you don't want to spend time updating a
6081 display that won't ever be seen. */
6082 f->async_visible = 0;
6083 /* We can't distinguish, from the event, whether the window
6084 has become iconified or invisible. So assume, if it
6085 was previously visible, than now it is iconified.
6086 But x_make_frame_invisible clears both
6087 the visible flag and the iconified flag;
6088 and that way, we know the window is not iconified now. */
6089 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
6090 {
6091 f->async_iconified = 1;
dc6f92b8 6092
9b516537
AS
6093 inev.ie.kind = ICONIFY_EVENT;
6094 XSETFRAME (inev.ie.frame_or_window, f);
bf338245
JD
6095 }
6096 }
6097 goto OTHER;
6098
6099 case MapNotify:
6100 if (event.xmap.window == tip_window)
6101 /* The tooltip has been drawn already. Avoid
6102 the SET_FRAME_GARBAGED below. */
6103 goto OTHER;
6104
6105 /* We use x_top_window_to_frame because map events can
6106 come for sub-windows and they don't mean that the
6107 frame is visible. */
6108 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
6109 if (f)
6110 {
d64b707c 6111 /* wait_reading_process_output will notice this and update
bf338245
JD
6112 the frame's display structures.
6113 If we where iconified, we should not set garbaged,
6114 because that stops redrawing on Expose events. This looks
6115 bad if we are called from a recursive event loop
6116 (x_dispatch_event), for example when a dialog is up. */
6117 if (! f->async_iconified)
6118 SET_FRAME_GARBAGED (f);
6119
6120 f->async_visible = 1;
6121 f->async_iconified = 0;
6122 f->output_data.x->has_been_visible = 1;
6123
6124 if (f->iconified)
6125 {
9b516537
AS
6126 inev.ie.kind = DEICONIFY_EVENT;
6127 XSETFRAME (inev.ie.frame_or_window, f);
bf338245
JD
6128 }
6129 else if (! NILP (Vframe_list)
6130 && ! NILP (XCDR (Vframe_list)))
6131 /* Force a redisplay sooner or later
6132 to update the frame titles
6133 in case this is the second frame. */
6134 record_asynch_buffer_change ();
6135 }
6136 goto OTHER;
2a793c0c 6137
bf338245 6138 case KeyPress:
2a793c0c 6139
0666d82c
KS
6140 ignore_next_mouse_click_timeout = 0;
6141
22174d10 6142#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
bf338245 6143 /* Dispatch KeyPress events when in menu. */
488dd4c4 6144 if (popup_activated ())
bf338245 6145 goto OTHER;
22174d10 6146#endif
488dd4c4 6147
bf338245 6148 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
f451eb13 6149
bf338245
JD
6150 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
6151 {
bf338245 6152 clear_mouse_face (dpyinfo);
40d0e281 6153 dpyinfo->mouse_face_hidden = 1;
bf338245 6154 }
66301061 6155
eccc05db 6156#if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
bf338245
JD
6157 if (f == 0)
6158 {
6159 /* Scroll bars consume key events, but we want
6160 the keys to go to the scroll bar's frame. */
6161 Widget widget = XtWindowToWidget (dpyinfo->display,
6162 event.xkey.window);
6163 if (widget && XmIsScrollBar (widget))
6164 {
6165 widget = XtParent (widget);
6166 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6167 }
6168 }
eccc05db 6169#endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
06a2c219 6170
bf338245
JD
6171 if (f != 0)
6172 {
6173 KeySym keysym, orig_keysym;
6174 /* al%imercury@uunet.uu.net says that making this 81
6175 instead of 80 fixed a bug whereby meta chars made
6176 his Emacs hang.
6177
6178 It seems that some version of XmbLookupString has
6179 a bug of not returning XBufferOverflow in
6180 status_return even if the input is too long to
6181 fit in 81 bytes. So, we must prepare sufficient
6182 bytes for copy_buffer. 513 bytes (256 chars for
6183 two-byte character set) seems to be a fairly good
6184 approximation. -- 2000.8.10 handa@etl.go.jp */
6185 unsigned char copy_buffer[513];
6186 unsigned char *copy_bufptr = copy_buffer;
6187 int copy_bufsiz = sizeof (copy_buffer);
6188 int modifiers;
6189 Lisp_Object coding_system = Qlatin_1;
89079179 6190 Lisp_Object c;
bf338245 6191
e8a84b6c
JD
6192#ifdef USE_GTK
6193 /* Don't pass keys to GTK. A Tab will shift focus to the
6194 tool bar in GTK 2.4. Keys will still go to menus and
6195 dialogs because in that case popup_activated is TRUE
6196 (see above). */
6197 *finish = X_EVENT_DROP;
6198#endif
6199
bf338245
JD
6200 event.xkey.state
6201 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
6202 extra_keyboard_modifiers);
6203 modifiers = event.xkey.state;
6204
6205 /* This will have to go some day... */
6206
6207 /* make_lispy_event turns chars into control chars.
6208 Don't do it here because XLookupString is too eager. */
6209 event.xkey.state &= ~ControlMask;
6210 event.xkey.state &= ~(dpyinfo->meta_mod_mask
6211 | dpyinfo->super_mod_mask
6212 | dpyinfo->hyper_mod_mask
6213 | dpyinfo->alt_mod_mask);
6214
6215 /* In case Meta is ComposeCharacter,
6216 clear its status. According to Markus Ehrnsperger
6217 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6218 this enables ComposeCharacter to work whether or
6219 not it is combined with Meta. */
6220 if (modifiers & dpyinfo->meta_mod_mask)
6221 bzero (&compose_status, sizeof (compose_status));
1cf4a0d1 6222
6c183ba5 6223#ifdef HAVE_X_I18N
bf338245
JD
6224 if (FRAME_XIC (f))
6225 {
6226 Status status_return;
6227
6228 coding_system = Vlocale_coding_system;
6229 nbytes = XmbLookupString (FRAME_XIC (f),
6230 &event.xkey, copy_bufptr,
6231 copy_bufsiz, &keysym,
6232 &status_return);
6233 if (status_return == XBufferOverflow)
6234 {
6235 copy_bufsiz = nbytes + 1;
6236 copy_bufptr = (char *) alloca (copy_bufsiz);
6237 nbytes = XmbLookupString (FRAME_XIC (f),
6238 &event.xkey, copy_bufptr,
6239 copy_bufsiz, &keysym,
6240 &status_return);
6241 }
6242 /* Xutf8LookupString is a new but already deprecated interface. -stef */
96035dca 6243#if 0 && defined X_HAVE_UTF8_STRING
bf338245
JD
6244 else if (status_return == XLookupKeySym)
6245 { /* Try again but with utf-8. */
6246 coding_system = Qutf_8;
6247 nbytes = Xutf8LookupString (FRAME_XIC (f),
6248 &event.xkey, copy_bufptr,
6249 copy_bufsiz, &keysym,
6250 &status_return);
6251 if (status_return == XBufferOverflow)
6252 {
6253 copy_bufsiz = nbytes + 1;
6254 copy_bufptr = (char *) alloca (copy_bufsiz);
6255 nbytes = Xutf8LookupString (FRAME_XIC (f),
6256 &event.xkey,
6257 copy_bufptr,
6258 copy_bufsiz, &keysym,
6259 &status_return);
6260 }
6261 }
c997eae5 6262#endif
f5d11644 6263
bf338245
JD
6264 if (status_return == XLookupNone)
6265 break;
6266 else if (status_return == XLookupChars)
6267 {
6268 keysym = NoSymbol;
6269 modifiers = 0;
6270 }
6271 else if (status_return != XLookupKeySym
6272 && status_return != XLookupBoth)
6273 abort ();
6274 }
6275 else
6276 nbytes = XLookupString (&event.xkey, copy_bufptr,
6277 copy_bufsiz, &keysym,
6278 &compose_status);
6c183ba5 6279#else
bf338245
JD
6280 nbytes = XLookupString (&event.xkey, copy_bufptr,
6281 copy_bufsiz, &keysym,
6282 &compose_status);
6c183ba5 6283#endif
dc6f92b8 6284
a633a954
JD
6285 /* If not using XIM/XIC, and a compose sequence is in progress,
6286 we break here. Otherwise, chars_matched is always 0. */
6287 if (compose_status.chars_matched > 0 && nbytes == 0)
6288 break;
6289
a6d0ae55 6290 bzero (&compose_status, sizeof (compose_status));
bf338245 6291 orig_keysym = keysym;
55123275 6292
89079179 6293 /* Common for all keysym input events. */
9b516537
AS
6294 XSETFRAME (inev.ie.frame_or_window, f);
6295 inev.ie.modifiers
89079179 6296 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), modifiers);
9b516537 6297 inev.ie.timestamp = event.xkey.time;
bf338245 6298
89079179
KS
6299 /* First deal with keysyms which have defined
6300 translations to characters. */
6301 if (keysym >= 32 && keysym < 128)
6302 /* Avoid explicitly decoding each ASCII character. */
6303 {
9b516537
AS
6304 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6305 inev.ie.code = keysym;
89079179
KS
6306 goto done_keysym;
6307 }
6308
6309 /* Now non-ASCII. */
6310 if (HASH_TABLE_P (Vx_keysym_table)
6311 && (NATNUMP (c = Fgethash (make_number (keysym),
6312 Vx_keysym_table,
6313 Qnil))))
6314 {
9b516537
AS
6315 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6316 ? ASCII_KEYSTROKE_EVENT
6317 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6318 inev.ie.code = XFASTINT (c);
89079179
KS
6319 goto done_keysym;
6320 }
6321
6322 /* Random non-modifier sorts of keysyms. */
6323 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
bf338245 6324 || keysym == XK_Delete
1097aea0 6325#ifdef XK_ISO_Left_Tab
bf338245
JD
6326 || (keysym >= XK_ISO_Left_Tab
6327 && keysym <= XK_ISO_Enter)
1097aea0 6328#endif
bf338245
JD
6329 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6330 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
c34790e0 6331#ifdef HPUX
bf338245
JD
6332 /* This recognizes the "extended function
6333 keys". It seems there's no cleaner way.
6334 Test IsModifierKey to avoid handling
6335 mode_switch incorrectly. */
6336 || ((unsigned) (keysym) >= XK_Select
6337 && (unsigned)(keysym) < XK_KP_Space)
69388238
RS
6338#endif
6339#ifdef XK_dead_circumflex
bf338245 6340 || orig_keysym == XK_dead_circumflex
69388238
RS
6341#endif
6342#ifdef XK_dead_grave
bf338245 6343 || orig_keysym == XK_dead_grave
69388238
RS
6344#endif
6345#ifdef XK_dead_tilde
bf338245 6346 || orig_keysym == XK_dead_tilde
69388238
RS
6347#endif
6348#ifdef XK_dead_diaeresis
bf338245 6349 || orig_keysym == XK_dead_diaeresis
69388238
RS
6350#endif
6351#ifdef XK_dead_macron
bf338245 6352 || orig_keysym == XK_dead_macron
69388238
RS
6353#endif
6354#ifdef XK_dead_degree
bf338245 6355 || orig_keysym == XK_dead_degree
69388238
RS
6356#endif
6357#ifdef XK_dead_acute
bf338245 6358 || orig_keysym == XK_dead_acute
69388238
RS
6359#endif
6360#ifdef XK_dead_cedilla
bf338245 6361 || orig_keysym == XK_dead_cedilla
69388238
RS
6362#endif
6363#ifdef XK_dead_breve
bf338245 6364 || orig_keysym == XK_dead_breve
69388238
RS
6365#endif
6366#ifdef XK_dead_ogonek
bf338245 6367 || orig_keysym == XK_dead_ogonek
69388238
RS
6368#endif
6369#ifdef XK_dead_caron
bf338245 6370 || orig_keysym == XK_dead_caron
69388238
RS
6371#endif
6372#ifdef XK_dead_doubleacute
bf338245 6373 || orig_keysym == XK_dead_doubleacute
69388238
RS
6374#endif
6375#ifdef XK_dead_abovedot
bf338245 6376 || orig_keysym == XK_dead_abovedot
c34790e0 6377#endif
bf338245
JD
6378 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6379 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6380 /* Any "vendor-specific" key is ok. */
6381 || (orig_keysym & (1 << 28))
6382 || (keysym != NoSymbol && nbytes == 0))
6383 && ! (IsModifierKey (orig_keysym)
7719aa06
RS
6384#ifndef HAVE_X11R5
6385#ifdef XK_Mode_switch
bf338245 6386 || ((unsigned)(orig_keysym) == XK_Mode_switch)
7719aa06
RS
6387#endif
6388#ifdef XK_Num_Lock
bf338245 6389 || ((unsigned)(orig_keysym) == XK_Num_Lock)
7719aa06
RS
6390#endif
6391#endif /* not HAVE_X11R5 */
bf338245
JD
6392 /* The symbols from XK_ISO_Lock
6393 to XK_ISO_Last_Group_Lock
6394 don't have real modifiers but
6395 should be treated similarly to
6396 Mode_switch by Emacs. */
7ef18d79 6397#if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
bf338245
JD
6398 || ((unsigned)(orig_keysym)
6399 >= XK_ISO_Lock
6400 && (unsigned)(orig_keysym)
6401 <= XK_ISO_Last_Group_Lock)
7ef18d79 6402#endif
bf338245 6403 ))
89079179
KS
6404 {
6405 STORE_KEYSYM_FOR_DEBUG (keysym);
6406 /* make_lispy_event will convert this to a symbolic
6407 key. */
9b516537
AS
6408 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
6409 inev.ie.code = keysym;
89079179
KS
6410 goto done_keysym;
6411 }
379b5ac0 6412
89079179
KS
6413 { /* Raw bytes, not keysym. */
6414 register int i;
6415 register int c;
6416 int nchars, len;
6417
6418 /* The input should be decoded with `coding_system'
6419 which depends on which X*LookupString function
6420 we used just above and the locale. */
6421 setup_coding_system (coding_system, &coding);
6422 coding.src_multibyte = 0;
6423 coding.dst_multibyte = 1;
6424 /* The input is converted to events, thus we can't
6425 handle composition. Anyway, there's no XIM that
6426 gives us composition information. */
6427 coding.composing = COMPOSITION_DISABLED;
6428
6429 for (i = 0; i < nbytes; i++)
6430 {
6431 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
6432 }
379b5ac0 6433
89079179
KS
6434 {
6435 /* Decode the input data. */
6436 int require;
6437 unsigned char *p;
6438
6439 require = decoding_buffer_size (&coding, nbytes);
6440 p = (unsigned char *) alloca (require);
6441 coding.mode |= CODING_MODE_LAST_BLOCK;
6442 /* We explicitly disable composition handling because
6443 key data should not contain any composition sequence. */
6444 coding.composing = COMPOSITION_DISABLED;
6445 decode_coding (&coding, copy_bufptr, p, nbytes, require);
6446 nbytes = coding.produced;
6447 nchars = coding.produced_char;
6448 copy_bufptr = p;
6449 }
bf338245 6450
89079179
KS
6451 /* Convert the input data to a sequence of
6452 character events. */
6453 for (i = 0; i < nbytes; i += len)
6454 {
6455 if (nchars == nbytes)
6456 c = copy_bufptr[i], len = 1;
6457 else
6458 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
6459 nbytes - i, len);
9b516537 6460 inev.ie.kind = (SINGLE_BYTE_CHAR_P (c)
89079179
KS
6461 ? ASCII_KEYSTROKE_EVENT
6462 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
9b516537
AS
6463 inev.ie.code = c;
6464 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
89079179 6465 }
bf338245 6466
89079179
KS
6467 /* Previous code updated count by nchars rather than nbytes,
6468 but that seems bogus to me. ++kfs */
6469 count += nbytes;
6470
9b516537 6471 inev.ie.kind = NO_EVENT; /* Already stored above. */
89079179
KS
6472
6473 if (keysym == NoSymbol)
6474 break;
6475 }
bf338245 6476 }
89079179 6477 done_keysym:
59ddecde 6478#ifdef HAVE_X_I18N
bf338245
JD
6479 /* Don't dispatch this event since XtDispatchEvent calls
6480 XFilterEvent, and two calls in a row may freeze the
6481 client. */
6482 break;
59ddecde 6483#else
bf338245 6484 goto OTHER;
59ddecde 6485#endif
f451eb13 6486
bf338245 6487 case KeyRelease:
59ddecde 6488#ifdef HAVE_X_I18N
bf338245
JD
6489 /* Don't dispatch this event since XtDispatchEvent calls
6490 XFilterEvent, and two calls in a row may freeze the
6491 client. */
6492 break;
59ddecde 6493#else
bf338245 6494 goto OTHER;
59ddecde 6495#endif
f5d11644 6496
bf338245 6497 case EnterNotify:
9b516537 6498 x_detect_focus_change (dpyinfo, &event, &inev.ie);
7d0393cf 6499
89079179 6500 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
06a2c219 6501
0666d82c
KS
6502 if (f && x_mouse_click_focus_ignore_position)
6503 ignore_next_mouse_click_timeout = event.xmotion.time + 200;
6504
2c850e26 6505#if 0
89079179
KS
6506 if (event.xcrossing.focus)
6507 {
6508 /* Avoid nasty pop/raise loops. */
6509 if (f && (!(f->auto_raise)
6510 || !(f->auto_lower)
6511 || (event.xcrossing.time - enter_timestamp) > 500))
6512 {
6513 x_new_focus_frame (dpyinfo, f);
6514 enter_timestamp = event.xcrossing.time;
6515 }
6516 }
6517 else if (f == dpyinfo->x_focus_frame)
6518 x_new_focus_frame (dpyinfo, 0);
bf338245 6519#endif
f9e24cb9 6520
89079179
KS
6521 /* EnterNotify counts as mouse movement,
6522 so update things that depend on mouse position. */
6523 if (f && !f->output_data.x->hourglass_p)
6524 note_mouse_movement (f, &event.xmotion);
6525 goto OTHER;
cab34f50 6526
bf338245 6527 case FocusIn:
9b516537 6528 x_detect_focus_change (dpyinfo, &event, &inev.ie);
bf338245 6529 goto OTHER;
7a13e894 6530
bf338245 6531 case LeaveNotify:
9b516537 6532 x_detect_focus_change (dpyinfo, &event, &inev.ie);
cab34f50 6533
bf338245
JD
6534 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
6535 if (f)
6536 {
6537 if (f == dpyinfo->mouse_face_mouse_frame)
6538 {
6539 /* If we move outside the frame, then we're
6540 certainly no longer on any text in the frame. */
6541 clear_mouse_face (dpyinfo);
6542 dpyinfo->mouse_face_mouse_frame = 0;
6543 }
6c183ba5 6544
bf338245
JD
6545 /* Generate a nil HELP_EVENT to cancel a help-echo.
6546 Do it only if there's something to cancel.
6547 Otherwise, the startup message is cleared when
6548 the mouse leaves the frame. */
6549 if (any_help_event_p)
89079179 6550 do_help = -1;
bf338245
JD
6551 }
6552 goto OTHER;
7d0393cf 6553
bf338245 6554 case FocusOut:
9b516537 6555 x_detect_focus_change (dpyinfo, &event, &inev.ie);
bf338245 6556 goto OTHER;
5bc62483 6557
bf338245
JD
6558 case MotionNotify:
6559 {
442a09ea
KS
6560 previous_help_echo_string = help_echo_string;
6561 help_echo_string = help_echo_object = help_echo_window = Qnil;
bf338245
JD
6562 help_echo_pos = -1;
6563
6564 if (dpyinfo->grabbed && last_mouse_frame
6565 && FRAME_LIVE_P (last_mouse_frame))
6566 f = last_mouse_frame;
6567 else
6568 f = x_window_to_frame (dpyinfo, event.xmotion.window);
6569
6570 if (dpyinfo->mouse_face_hidden)
6571 {
6572 dpyinfo->mouse_face_hidden = 0;
6573 clear_mouse_face (dpyinfo);
6574 }
6575
6576 if (f)
6577 {
6578
6579 /* Generate SELECT_WINDOW_EVENTs when needed. */
6580 if (mouse_autoselect_window)
6581 {
6582 Lisp_Object window;
bf338245
JD
6583
6584 window = window_from_coordinates (f,
6585 event.xmotion.x, event.xmotion.y,
0899d58c 6586 0, 0, 0, 0);
bf338245
JD
6587
6588 /* Window will be selected only when it is not selected now and
6589 last mouse movement event was not in it. Minibuffer window
6590 will be selected iff it is active. */
810f2256 6591 if (WINDOWP (window)
bf338245 6592 && !EQ (window, last_window)
89079179 6593 && !EQ (window, selected_window))
bf338245 6594 {
9b516537
AS
6595 inev.ie.kind = SELECT_WINDOW_EVENT;
6596 inev.ie.frame_or_window = window;
bf338245 6597 }
5bc62483 6598
bf338245
JD
6599 last_window=window;
6600 }
6601 note_mouse_movement (f, &event.xmotion);
6602 }
6603 else
6604 {
e88b3c50 6605#ifndef USE_TOOLKIT_SCROLL_BARS
bf338245 6606 struct scroll_bar *bar
810f2256
JD
6607 = x_window_to_scroll_bar (event.xmotion.display,
6608 event.xmotion.window);
f451eb13 6609
bf338245
JD
6610 if (bar)
6611 x_scroll_bar_note_movement (bar, &event);
e88b3c50 6612#endif /* USE_TOOLKIT_SCROLL_BARS */
b8009dd1 6613
bf338245
JD
6614 /* If we move outside the frame, then we're
6615 certainly no longer on any text in the frame. */
6616 clear_mouse_face (dpyinfo);
6617 }
6618
442a09ea 6619 /* If the contents of the global variable help_echo_string
bf338245 6620 has changed, generate a HELP_EVENT. */
442a09ea
KS
6621 if (!NILP (help_echo_string)
6622 || !NILP (previous_help_echo_string))
89079179 6623 do_help = 1;
bf338245
JD
6624 goto OTHER;
6625 }
dc6f92b8 6626
bf338245
JD
6627 case ConfigureNotify:
6628 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
6629 if (f)
6630 {
7a4bce14 6631#ifndef USE_X_TOOLKIT
488dd4c4
JD
6632#ifdef USE_GTK
6633 xg_resize_widgets (f, event.xconfigure.width,
6634 event.xconfigure.height);
6635#else /* not USE_GTK */
bf338245
JD
6636 /* If there is a pending resize for fullscreen, don't
6637 do this one, the right one will come later.
6638 The toolkit version doesn't seem to need this, but we
6639 need to reset it below. */
62fe13a4 6640 int dont_resize
0899d58c
KS
6641 = ((f->want_fullscreen & FULLSCREEN_WAIT)
6642 && f->new_text_cols != 0);
6643 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, event.xconfigure.height);
6644 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, event.xconfigure.width);
6645
bf338245
JD
6646 if (dont_resize)
6647 goto OTHER;
6648
6649 /* In the toolkit version, change_frame_size
6650 is called by the code that handles resizing
6651 of the EmacsFrame widget. */
6652
6653 /* Even if the number of character rows and columns has
6654 not changed, the font size may have changed, so we need
6655 to check the pixel dimensions as well. */
0899d58c
KS
6656 if (columns != FRAME_COLS (f)
6657 || rows != FRAME_LINES (f)
6658 || event.xconfigure.width != FRAME_PIXEL_WIDTH (f)
6659 || event.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
bf338245
JD
6660 {
6661 change_frame_size (f, rows, columns, 0, 1, 0);
6662 SET_FRAME_GARBAGED (f);
6663 cancel_mouse_face (f);
6664 }
488dd4c4 6665#endif /* not USE_GTK */
2d7fc7e8 6666#endif
af395ec1 6667
0899d58c
KS
6668 FRAME_PIXEL_WIDTH (f) = event.xconfigure.width;
6669 FRAME_PIXEL_HEIGHT (f) = event.xconfigure.height;
7a13e894 6670
488dd4c4
JD
6671#ifdef USE_GTK
6672 /* GTK creates windows but doesn't map them.
6673 Only get real positions and check fullscreen when mapped. */
6674 if (FRAME_GTK_OUTER_WIDGET (f)
6675 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
488dd4c4 6676#endif
0899d58c
KS
6677 {
6678 /* What we have now is the position of Emacs's own window.
6679 Convert that to the position of the window manager window. */
6680 x_real_positions (f, &f->left_pos, &f->top_pos);
6681
c1f0671a 6682 x_check_expected_move (f);
0899d58c
KS
6683 if (f->want_fullscreen & FULLSCREEN_WAIT)
6684 f->want_fullscreen &= ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
488dd4c4 6685 }
0899d58c 6686
f5d11644 6687#ifdef HAVE_X_I18N
bf338245
JD
6688 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6689 xic_set_statusarea (f);
f5d11644
GM
6690#endif
6691
bf338245
JD
6692 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6693 {
6694 /* Since the WM decorations come below top_pos now,
6695 we must put them below top_pos in the future. */
0899d58c 6696 f->win_gravity = NorthWestGravity;
bf338245
JD
6697 x_wm_set_size_hint (f, (long) 0, 0);
6698 }
6699 }
6700 goto OTHER;
dc6f92b8 6701
bf338245 6702 case ButtonRelease:
488dd4c4 6703 case ButtonPress:
bf338245
JD
6704 {
6705 /* If we decide we want to generate an event to be seen
6706 by the rest of Emacs, we put it here. */
bf338245
JD
6707 int tool_bar_p = 0;
6708
bf338245
JD
6709 bzero (&compose_status, sizeof (compose_status));
6710
6711 if (dpyinfo->grabbed
6712 && last_mouse_frame
6713 && FRAME_LIVE_P (last_mouse_frame))
6714 f = last_mouse_frame;
6715 else
6716 f = x_window_to_frame (dpyinfo, event.xbutton.window);
6717
6718 if (f)
6719 {
6720 /* Is this in the tool-bar? */
6721 if (WINDOWP (f->tool_bar_window)
0899d58c 6722 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
bf338245
JD
6723 {
6724 Lisp_Object window;
442a09ea
KS
6725 int x = event.xbutton.x;
6726 int y = event.xbutton.y;
06a2c219 6727
0899d58c 6728 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
bf338245
JD
6729 if (EQ (window, f->tool_bar_window))
6730 {
442a09ea
KS
6731 if (event.xbutton.type == ButtonPress)
6732 handle_tool_bar_click (f, x, y, 1, 0);
6733 else
6734 handle_tool_bar_click (f, x, y, 0,
6735 x_x_to_emacs_modifiers (dpyinfo,
6736 event.xbutton.state));
6737 tool_bar_p = 1;
bf338245
JD
6738 }
6739 }
06a2c219 6740
bf338245
JD
6741 if (!tool_bar_p)
6742 if (!dpyinfo->x_focus_frame
6743 || f == dpyinfo->x_focus_frame)
488dd4c4 6744 {
22174d10 6745#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
488dd4c4 6746 if (! popup_activated ())
22174d10 6747#endif
0666d82c
KS
6748 {
6749 if (ignore_next_mouse_click_timeout)
6750 {
6751 if (event.type == ButtonPress
6752 && (int)(event.xbutton.time - ignore_next_mouse_click_timeout) > 0)
6753 {
6754 ignore_next_mouse_click_timeout = 0;
9b516537 6755 construct_mouse_click (&inev.ie, &event, f);
0666d82c
KS
6756 }
6757 if (event.type == ButtonRelease)
6758 ignore_next_mouse_click_timeout = 0;
6759 }
6760 else
9b516537 6761 construct_mouse_click (&inev.ie, &event, f);
0666d82c 6762 }
488dd4c4 6763 }
bf338245
JD
6764 }
6765 else
6766 {
bf338245 6767 struct scroll_bar *bar
810f2256
JD
6768 = x_window_to_scroll_bar (event.xbutton.display,
6769 event.xbutton.window);
f451eb13 6770
257f40f2
JD
6771#ifdef USE_TOOLKIT_SCROLL_BARS
6772 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
6773 scroll bars. */
6774 if (bar && event.xbutton.state & ControlMask)
6775 {
9b516537 6776 x_scroll_bar_handle_click (bar, &event, &inev.ie);
257f40f2
JD
6777 *finish = X_EVENT_DROP;
6778 }
6779#else /* not USE_TOOLKIT_SCROLL_BARS */
bf338245 6780 if (bar)
9b516537 6781 x_scroll_bar_handle_click (bar, &event, &inev.ie);
06a2c219 6782#endif /* not USE_TOOLKIT_SCROLL_BARS */
bf338245
JD
6783 }
6784
6785 if (event.type == ButtonPress)
6786 {
6787 dpyinfo->grabbed |= (1 << event.xbutton.button);
6788 last_mouse_frame = f;
bf338245
JD
6789
6790 if (!tool_bar_p)
6791 last_tool_bar_item = -1;
6792 }
6793 else
488dd4c4 6794 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
bf338245 6795
24aad441
KS
6796 /* Ignore any mouse motion that happened before this event;
6797 any subsequent mouse-movement Emacs events should reflect
6798 only motion after the ButtonPress/Release. */
6799 if (f != 0)
6800 f->mouse_moved = 0;
6801
488dd4c4 6802#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
bf338245
JD
6803 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
6804 /* For a down-event in the menu bar,
6805 don't pass it to Xt right now.
6806 Instead, save it away
6807 and we will pass it to Xt from kbd_buffer_get_event.
6808 That way, we can run some Lisp code first. */
488dd4c4
JD
6809 if (
6810#ifdef USE_GTK
6811 ! popup_activated ()
6812 &&
6813#endif
6814 f && event.type == ButtonPress
bf338245
JD
6815 /* Verify the event is really within the menu bar
6816 and not just sent to it due to grabbing. */
6817 && event.xbutton.x >= 0
0899d58c 6818 && event.xbutton.x < FRAME_PIXEL_WIDTH (f)
bf338245
JD
6819 && event.xbutton.y >= 0
6820 && event.xbutton.y < f->output_data.x->menubar_height
6821 && event.xbutton.same_screen)
6822 {
6823 SET_SAVED_BUTTON_EVENT;
6824 XSETFRAME (last_mouse_press_frame, f);
488dd4c4
JD
6825#ifdef USE_GTK
6826 *finish = X_EVENT_DROP;
6827#endif
bf338245
JD
6828 }
6829 else if (event.type == ButtonPress)
6830 {
6831 last_mouse_press_frame = Qnil;
6832 goto OTHER;
6833 }
06a2c219 6834
2237cac9
RS
6835#ifdef USE_MOTIF /* This should do not harm for Lucid,
6836 but I am trying to be cautious. */
bf338245
JD
6837 else if (event.type == ButtonRelease)
6838 {
6839 if (!NILP (last_mouse_press_frame))
6840 {
6841 f = XFRAME (last_mouse_press_frame);
6842 if (f->output_data.x)
6843 SET_SAVED_BUTTON_EVENT;
6844 }
6845 else
6846 goto OTHER;
6847 }
2237cac9 6848#endif /* USE_MOTIF */
bf338245
JD
6849 else
6850 goto OTHER;
488dd4c4 6851#endif /* USE_X_TOOLKIT || USE_GTK */
bf338245
JD
6852 }
6853 break;
dc6f92b8 6854
bf338245
JD
6855 case CirculateNotify:
6856 goto OTHER;
7d0393cf 6857
bf338245
JD
6858 case CirculateRequest:
6859 goto OTHER;
06a2c219 6860
bf338245
JD
6861 case VisibilityNotify:
6862 goto OTHER;
dc6f92b8 6863
bf338245
JD
6864 case MappingNotify:
6865 /* Someone has changed the keyboard mapping - update the
6866 local cache. */
6867 switch (event.xmapping.request)
6868 {
6869 case MappingModifier:
6870 x_find_modifier_meanings (dpyinfo);
6871 /* This is meant to fall through. */
6872 case MappingKeyboard:
6873 XRefreshKeyboardMapping (&event.xmapping);
6874 }
6875 goto OTHER;
dc6f92b8 6876
bf338245
JD
6877 default:
6878 OTHER:
717ca130 6879#ifdef USE_X_TOOLKIT
bf338245 6880 BLOCK_INPUT;
257f40f2
JD
6881 if (*finish != X_EVENT_DROP)
6882 XtDispatchEvent (&event);
bf338245 6883 UNBLOCK_INPUT;
3afe33e7 6884#endif /* USE_X_TOOLKIT */
bf338245
JD
6885 break;
6886 }
6887
89079179 6888 done:
9b516537 6889 if (inev.ie.kind != NO_EVENT)
89079179 6890 {
9b516537 6891 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
89079179
KS
6892 count++;
6893 }
177c0ea7 6894
89079179
KS
6895 if (do_help
6896 && !(hold_quit && hold_quit->kind != NO_EVENT))
6897 {
6898 Lisp_Object frame;
bf338245 6899
89079179
KS
6900 if (f)
6901 XSETFRAME (frame, f);
6902 else
6903 frame = Qnil;
177c0ea7 6904
89079179
KS
6905 if (do_help > 0)
6906 {
6907 any_help_event_p = 1;
6908 gen_help_event (help_echo_string, frame, help_echo_window,
6909 help_echo_object, help_echo_pos);
0666d82c 6910 }
89079179
KS
6911 else
6912 {
6913 help_echo_string = Qnil;
6914 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
6915 }
6916 count++;
6917 }
6918
6919 *eventp = event;
bf338245
JD
6920 return count;
6921}
6922
6923
6924/* Handles the XEvent EVENT on display DISPLAY.
6925 This is used for event loops outside the normal event handling,
1fcfb866
JD
6926 i.e. looping while a popup menu or a dialog is posted.
6927
6928 Returns the value handle_one_xevent sets in the finish argument. */
6929int
bf338245
JD
6930x_dispatch_event (event, display)
6931 XEvent *event;
6932 Display *display;
6933{
6934 struct x_display_info *dpyinfo;
1fcfb866 6935 int finish = X_EVENT_NORMAL;
177c0ea7 6936
810f2256 6937 dpyinfo = x_display_info_for_display (display);
177c0ea7 6938
bf338245 6939 if (dpyinfo)
89079179 6940 handle_one_xevent (dpyinfo, event, &finish, 0);
1fcfb866
JD
6941
6942 return finish;
bf338245
JD
6943}
6944
6945
6946/* Read events coming from the X server.
6947 This routine is called by the SIGIO handler.
6948 We return as soon as there are no more events to be read.
6949
bf338245
JD
6950 We return the number of characters stored into the buffer,
6951 thus pretending to be `read'.
6952
6953 EXPECTED is nonzero if the caller knows input is available. */
6954
6955static int
89079179 6956XTread_socket (sd, expected, hold_quit)
bf338245 6957 register int sd;
bf338245 6958 int expected;
89079179 6959 struct input_event *hold_quit;
bf338245
JD
6960{
6961 int count = 0;
bf338245
JD
6962 XEvent event;
6963 int event_found = 0;
6964 struct x_display_info *dpyinfo;
6965
6966 if (interrupt_input_blocked)
6967 {
6968 interrupt_input_pending = 1;
6969 return -1;
6970 }
6971
6972 interrupt_input_pending = 0;
6973 BLOCK_INPUT;
6974
6975 /* So people can tell when we have read the available input. */
6976 input_signal_count++;
6977
bf338245
JD
6978 ++handling_signal;
6979
6980 /* Find the display we are supposed to read input for.
6981 It's the one communicating on descriptor SD. */
6982 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
6983 {
6984#if 0 /* This ought to be unnecessary; let's verify it. */
6985#ifdef FIOSNBIO
6986 /* If available, Xlib uses FIOSNBIO to make the socket
6987 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
6988 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
6989 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
6990 fcntl (dpyinfo->connection, F_SETFL, 0);
6991#endif /* ! defined (FIOSNBIO) */
6992#endif
6993
6994#if 0 /* This code can't be made to work, with multiple displays,
6995 and appears not to be used on any system any more.
6996 Also keyboard.c doesn't turn O_NDELAY on and off
6997 for X connections. */
6998#ifndef SIGIO
6999#ifndef HAVE_SELECT
7000 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
7001 {
7002 extern int read_alarm_should_throw;
7003 read_alarm_should_throw = 1;
7004 XPeekEvent (dpyinfo->display, &event);
7005 read_alarm_should_throw = 0;
7006 }
7007#endif /* HAVE_SELECT */
7008#endif /* SIGIO */
7009#endif
7010
7011 /* For debugging, this gives a way to fake an I/O error. */
7012 if (dpyinfo == XTread_socket_fake_io_error)
7013 {
7014 XTread_socket_fake_io_error = 0;
7015 x_io_error_quitter (dpyinfo->display);
dc6f92b8 7016 }
bf338245
JD
7017
7018#ifdef HAVE_X_SM
89079179
KS
7019 {
7020 struct input_event inev;
7021 BLOCK_INPUT;
7022 /* We don't need to EVENT_INIT (inev) here, as
7023 x_session_check_input copies an entire input_event. */
7024 if (x_session_check_input (&inev))
7025 {
7026 kbd_buffer_store_event_hold (&inev, hold_quit);
7027 count++;
7028 }
7029 UNBLOCK_INPUT;
7030 }
bf338245
JD
7031#endif
7032
810f2256 7033#ifndef USE_GTK
bf338245
JD
7034 while (XPending (dpyinfo->display))
7035 {
7036 int finish;
177c0ea7 7037
bf338245
JD
7038 XNextEvent (dpyinfo->display, &event);
7039
7040#ifdef HAVE_X_I18N
1fcfb866
JD
7041 /* Filter events for the current X input method. */
7042 if (x_filter_event (dpyinfo, &event))
7043 break;
bf338245
JD
7044#endif
7045 event_found = 1;
7046
89079179 7047 count += handle_one_xevent (dpyinfo, &event, &finish, hold_quit);
bf338245
JD
7048
7049 if (finish == X_EVENT_GOTO_OUT)
7050 goto out;
7051 }
810f2256
JD
7052#endif /* not USE_GTK */
7053 }
7054
7055#ifdef USE_GTK
7056
7057 /* For GTK we must use the GTK event loop. But XEvents gets passed
7058 to our filter function above, and then to the big event switch.
7059 We use a bunch of globals to communicate with our filter function,
7060 that is kind of ugly, but it works.
7061
7062 There is no way to do one display at the time, GTK just does events
7063 from all displays. */
7064
7065 while (gtk_events_pending ())
7066 {
7067 current_count = count;
89079179 7068 current_hold_quit = hold_quit;
810f2256
JD
7069
7070 gtk_main_iteration ();
7071
7072 count = current_count;
89079179
KS
7073 current_count = -1;
7074 current_hold_quit = 0;
810f2256
JD
7075
7076 if (current_finish == X_EVENT_GOTO_OUT)
7077 break;
dc6f92b8 7078 }
810f2256 7079#endif /* USE_GTK */
dc6f92b8 7080
06a2c219
GM
7081 out:;
7082
9a5196d0
RS
7083 /* On some systems, an X bug causes Emacs to get no more events
7084 when the window is destroyed. Detect that. (1994.) */
58769bee 7085 if (! event_found)
ef2a22d0 7086 {
ef2a22d0
RS
7087 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7088 One XNOOP in 100 loops will make Emacs terminate.
7089 B. Bretthauer, 1994 */
7090 x_noop_count++;
58769bee 7091 if (x_noop_count >= 100)
ef2a22d0
RS
7092 {
7093 x_noop_count=0;
2224b905
RS
7094
7095 if (next_noop_dpyinfo == 0)
7096 next_noop_dpyinfo = x_display_list;
7097
7098 XNoOp (next_noop_dpyinfo->display);
7099
7100 /* Each time we get here, cycle through the displays now open. */
7101 next_noop_dpyinfo = next_noop_dpyinfo->next;
ef2a22d0
RS
7102 }
7103 }
502add23 7104
06a2c219 7105 /* If the focus was just given to an auto-raising frame,
0134a210 7106 raise it now. */
7a13e894 7107 /* ??? This ought to be able to handle more than one such frame. */
0134a210
RS
7108 if (pending_autoraise_frame)
7109 {
7110 x_raise_frame (pending_autoraise_frame);
7111 pending_autoraise_frame = 0;
7112 }
0134a210 7113
bde5503b 7114 --handling_signal;
89079179
KS
7115 UNBLOCK_INPUT;
7116
dc6f92b8
JB
7117 return count;
7118}
06a2c219
GM
7119
7120
7121
dc6f92b8 7122\f
06a2c219
GM
7123/***********************************************************************
7124 Text Cursor
7125 ***********************************************************************/
7126
06a2c219
GM
7127/* Set clipping for output in glyph row ROW. W is the window in which
7128 we operate. GC is the graphics context to set clipping in.
06a2c219
GM
7129
7130 ROW may be a text row or, e.g., a mode line. Text rows must be
7131 clipped to the interior of the window dedicated to text display,
7132 mode lines must be clipped to the whole window. */
dc6f92b8
JB
7133
7134static void
08f66682 7135x_clip_to_row (w, row, area, gc)
06a2c219
GM
7136 struct window *w;
7137 struct glyph_row *row;
08f66682 7138 int area;
06a2c219 7139 GC gc;
dc6f92b8 7140{
06a2c219
GM
7141 struct frame *f = XFRAME (WINDOW_FRAME (w));
7142 XRectangle clip_rect;
08f66682 7143 int window_x, window_y, window_width;
dc6f92b8 7144
08f66682 7145 window_box (w, area, &window_x, &window_y, &window_width, 0);
5c1aae96 7146
08f66682 7147 clip_rect.x = window_x;
dbd8ee19 7148 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
06a2c219
GM
7149 clip_rect.y = max (clip_rect.y, window_y);
7150 clip_rect.width = window_width;
7151 clip_rect.height = row->visible_height;
5c1aae96 7152
06a2c219 7153 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
dc6f92b8
JB
7154}
7155
06a2c219
GM
7156
7157/* Draw a hollow box cursor on window W in glyph row ROW. */
dc6f92b8
JB
7158
7159static void
06a2c219
GM
7160x_draw_hollow_cursor (w, row)
7161 struct window *w;
7162 struct glyph_row *row;
dc6f92b8 7163{
06a2c219
GM
7164 struct frame *f = XFRAME (WINDOW_FRAME (w));
7165 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7166 Display *dpy = FRAME_X_DISPLAY (f);
7167 int x, y, wd, h;
7168 XGCValues xgcv;
7169 struct glyph *cursor_glyph;
7170 GC gc;
7171
06a2c219
GM
7172 /* Get the glyph the cursor is on. If we can't tell because
7173 the current matrix is invalid or such, give up. */
7174 cursor_glyph = get_phys_cursor_glyph (w);
7175 if (cursor_glyph == NULL)
dc6f92b8
JB
7176 return;
7177
dbd8ee19 7178 /* Compute frame-relative coordinates for phys cursor. */
83c6eb57 7179 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
dbd8ee19
KS
7180 y = get_phys_cursor_geometry (w, row, cursor_glyph, &h);
7181 wd = w->phys_cursor_width;
83c6eb57 7182
06a2c219
GM
7183 /* The foreground of cursor_gc is typically the same as the normal
7184 background color, which can cause the cursor box to be invisible. */
7185 xgcv.foreground = f->output_data.x->cursor_pixel;
7186 if (dpyinfo->scratch_cursor_gc)
7187 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7188 else
7189 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7190 GCForeground, &xgcv);
7191 gc = dpyinfo->scratch_cursor_gc;
7192
7193 /* Set clipping, draw the rectangle, and reset clipping again. */
08f66682 7194 x_clip_to_row (w, row, TEXT_AREA, gc);
06a2c219
GM
7195 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
7196 XSetClipMask (dpy, gc, None);
dc6f92b8
JB
7197}
7198
06a2c219
GM
7199
7200/* Draw a bar cursor on window W in glyph row ROW.
7201
7202 Implementation note: One would like to draw a bar cursor with an
7203 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7204 Unfortunately, I didn't find a font yet that has this property set.
7205 --gerd. */
dc6f92b8
JB
7206
7207static void
65c26775 7208x_draw_bar_cursor (w, row, width, kind)
06a2c219
GM
7209 struct window *w;
7210 struct glyph_row *row;
f02d8aa0 7211 int width;
65c26775 7212 enum text_cursor_kinds kind;
dc6f92b8 7213{
92f424df
GM
7214 struct frame *f = XFRAME (w->frame);
7215 struct glyph *cursor_glyph;
7d0393cf 7216
92f424df
GM
7217 /* If cursor is out of bounds, don't draw garbage. This can happen
7218 in mini-buffer windows when switching between echo area glyphs
7219 and mini-buffer. */
7220 cursor_glyph = get_phys_cursor_glyph (w);
7221 if (cursor_glyph == NULL)
7222 return;
06a2c219 7223
92f424df
GM
7224 /* If on an image, draw like a normal cursor. That's usually better
7225 visible than drawing a bar, esp. if the image is large so that
7226 the bar might not be in the window. */
7227 if (cursor_glyph->type == IMAGE_GLYPH)
7228 {
7229 struct glyph_row *row;
7230 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
442a09ea 7231 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
92f424df
GM
7232 }
7233 else
7234 {
34e5d0af
GM
7235 Display *dpy = FRAME_X_DISPLAY (f);
7236 Window window = FRAME_X_WINDOW (f);
7237 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7238 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7239 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7240 XGCValues xgcv;
7241
7242 /* If the glyph's background equals the color we normally draw
7243 the bar cursor in, the bar cursor in its normal color is
7244 invisible. Use the glyph's foreground color instead in this
7245 case, on the assumption that the glyph's colors are chosen so
7246 that the glyph is legible. */
7247 if (face->background == f->output_data.x->cursor_pixel)
7248 xgcv.background = xgcv.foreground = face->foreground;
7249 else
7250 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
06a2c219 7251 xgcv.graphics_exposures = 0;
7d0393cf 7252
06a2c219
GM
7253 if (gc)
7254 XChangeGC (dpy, gc, mask, &xgcv);
7255 else
7256 {
7257 gc = XCreateGC (dpy, window, mask, &xgcv);
7258 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7259 }
7d0393cf 7260
f02d8aa0 7261 if (width < 0)
fdbe859c 7262 width = FRAME_CURSOR_WIDTH (f);
34e5d0af 7263 width = min (cursor_glyph->pixel_width, width);
7d0393cf 7264
b3738089 7265 w->phys_cursor_width = width;
08f66682 7266 x_clip_to_row (w, row, TEXT_AREA, gc);
7d0393cf 7267
65c26775
EZ
7268 if (kind == BAR_CURSOR)
7269 XFillRectangle (dpy, window, gc,
7270 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7271 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7272 width, row->height);
7273 else
7274 XFillRectangle (dpy, window, gc,
7275 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7276 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7277 row->height - width),
7278 cursor_glyph->pixel_width,
7279 width);
7280
06a2c219
GM
7281 XSetClipMask (dpy, gc, None);
7282 }
dc6f92b8
JB
7283}
7284
06a2c219 7285
442a09ea 7286/* RIF: Define cursor CURSOR on frame F. */
06a2c219 7287
dc6f92b8 7288static void
442a09ea
KS
7289x_define_frame_cursor (f, cursor)
7290 struct frame *f;
7291 Cursor cursor;
dc6f92b8 7292{
442a09ea 7293 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
06a2c219 7294}
90e65f07 7295
dbc4e1c1 7296
442a09ea 7297/* RIF: Clear area on frame F. */
dbc4e1c1 7298
06a2c219 7299static void
442a09ea
KS
7300x_clear_frame_area (f, x, y, width, height)
7301 struct frame *f;
7302 int x, y, width, height;
06a2c219 7303{
442a09ea
KS
7304 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7305 x, y, width, height, False);
06a2c219 7306}
dbc4e1c1 7307
eea6af04 7308
442a09ea 7309/* RIF: Draw cursor on window W. */
eea6af04 7310
06a2c219 7311static void
e5a3b7d9 7312x_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
06a2c219 7313 struct window *w;
442a09ea 7314 struct glyph_row *glyph_row;
e5a3b7d9
KS
7315 int x, y;
7316 int cursor_type, cursor_width;
7317 int on_p, active_p;
dbc4e1c1 7318{
442a09ea 7319 struct frame *f = XFRAME (WINDOW_FRAME (w));
06a2c219 7320
e5a3b7d9 7321 if (on_p)
06a2c219 7322 {
e5a3b7d9 7323 w->phys_cursor_type = cursor_type;
06a2c219
GM
7324 w->phys_cursor_on_p = 1;
7325
3c0882ae
KS
7326 if (glyph_row->exact_window_width_line_p
7327 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
7328 {
7329 glyph_row->cursor_in_fringe_p = 1;
7330 draw_fringe_bitmap (w, glyph_row, 0);
7331 }
7332 else
e5a3b7d9 7333 switch (cursor_type)
dc6f92b8 7334 {
06a2c219
GM
7335 case HOLLOW_BOX_CURSOR:
7336 x_draw_hollow_cursor (w, glyph_row);
7337 break;
7338
7339 case FILLED_BOX_CURSOR:
442a09ea 7340 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
06a2c219
GM
7341 break;
7342
7343 case BAR_CURSOR:
e5a3b7d9 7344 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
65c26775
EZ
7345 break;
7346
7347 case HBAR_CURSOR:
e5a3b7d9 7348 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
06a2c219
GM
7349 break;
7350
7351 case NO_CURSOR:
4398e673 7352 w->phys_cursor_width = 0;
06a2c219 7353 break;
dc6f92b8 7354
06a2c219
GM
7355 default:
7356 abort ();
7357 }
7d0393cf 7358
59ddecde
GM
7359#ifdef HAVE_X_I18N
7360 if (w == XWINDOW (f->selected_window))
7361 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7362 xic_set_preeditarea (w, x, y);
7363#endif
dc6f92b8
JB
7364 }
7365
06a2c219 7366#ifndef XFlush
f676886a 7367 if (updating_frame != f)
334208b7 7368 XFlush (FRAME_X_DISPLAY (f));
06a2c219 7369#endif
dc6f92b8
JB
7370}
7371
dc6f92b8
JB
7372\f
7373/* Icons. */
7374
dbc4e1c1 7375/* Make the x-window of frame F use the gnu icon bitmap. */
dc6f92b8
JB
7376
7377int
990ba854 7378x_bitmap_icon (f, file)
f676886a 7379 struct frame *f;
990ba854 7380 Lisp_Object file;
dc6f92b8 7381{
06a2c219 7382 int bitmap_id;
dc6f92b8 7383
c118dd06 7384 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
7385 return 1;
7386
62fe13a4 7387 /* Free up our existing icon bitmap and mask if any. */
7556890b
RS
7388 if (f->output_data.x->icon_bitmap > 0)
7389 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7390 f->output_data.x->icon_bitmap = 0;
990ba854
RS
7391
7392 if (STRINGP (file))
62fe13a4
JB
7393 {
7394#ifdef USE_GTK
810f2256 7395 /* Use gtk_window_set_icon_from_file () if available,
62fe13a4 7396 It's not restricted to bitmaps */
810f2256 7397 if (xg_set_icon (f, file))
62fe13a4
JB
7398 return 0;
7399#endif /* USE_GTK */
7400 bitmap_id = x_create_bitmap_from_file (f, file);
810f2256 7401 x_create_bitmap_mask (f, bitmap_id);
62fe13a4 7402 }
7f2ae036
RS
7403 else
7404 {
62fe13a4 7405 /* Create the GNU bitmap and mask if necessary. */
5bf01b68 7406 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
62fe13a4
JB
7407 {
7408 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
7409 = x_create_bitmap_from_data (f, gnu_bits,
7410 gnu_width, gnu_height);
810f2256 7411 x_create_bitmap_mask (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
62fe13a4 7412 }
990ba854 7413
62fe13a4 7414 /* The first time we create the GNU bitmap and mask,
06a2c219 7415 this increments the ref-count one extra time.
62fe13a4 7416 As a result, the GNU bitmap and mask are never freed.
990ba854 7417 That way, we don't have to worry about allocating it again. */
334208b7 7418 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
990ba854 7419
334208b7 7420 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7f2ae036
RS
7421 }
7422
7423 x_wm_set_icon_pixmap (f, bitmap_id);
7556890b 7424 f->output_data.x->icon_bitmap = bitmap_id;
dc6f92b8
JB
7425
7426 return 0;
7427}
7428
7429
1be2d067
KH
7430/* Make the x-window of frame F use a rectangle with text.
7431 Use ICON_NAME as the text. */
dc6f92b8
JB
7432
7433int
f676886a
JB
7434x_text_icon (f, icon_name)
7435 struct frame *f;
dc6f92b8
JB
7436 char *icon_name;
7437{
c118dd06 7438 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
7439 return 1;
7440
1be2d067
KH
7441#ifdef HAVE_X11R4
7442 {
7443 XTextProperty text;
7444 text.value = (unsigned char *) icon_name;
7445 text.encoding = XA_STRING;
7446 text.format = 8;
7447 text.nitems = strlen (icon_name);
2436a4e4 7448 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
1be2d067
KH
7449 }
7450#else /* not HAVE_X11R4 */
2436a4e4 7451 XSetIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), icon_name);
1be2d067 7452#endif /* not HAVE_X11R4 */
58769bee 7453
7556890b
RS
7454 if (f->output_data.x->icon_bitmap > 0)
7455 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7456 f->output_data.x->icon_bitmap = 0;
b1c884c3 7457 x_wm_set_icon_pixmap (f, 0);
dc6f92b8
JB
7458
7459 return 0;
7460}
7461\f
e99db5a1
RS
7462#define X_ERROR_MESSAGE_SIZE 200
7463
7464/* If non-nil, this should be a string.
7465 It means catch X errors and store the error message in this string. */
7466
7467static Lisp_Object x_error_message_string;
7468
7469/* An X error handler which stores the error message in
7470 x_error_message_string. This is called from x_error_handler if
7471 x_catch_errors is in effect. */
7472
06a2c219 7473static void
e99db5a1
RS
7474x_error_catcher (display, error)
7475 Display *display;
7476 XErrorEvent *error;
7477{
7478 XGetErrorText (display, error->error_code,
d5db4077 7479 SDATA (x_error_message_string),
e99db5a1
RS
7480 X_ERROR_MESSAGE_SIZE);
7481}
7482
7483/* Begin trapping X errors for display DPY. Actually we trap X errors
7484 for all displays, but DPY should be the display you are actually
7485 operating on.
7486
7487 After calling this function, X protocol errors no longer cause
7488 Emacs to exit; instead, they are recorded in the string
7489 stored in x_error_message_string.
7490
7491 Calling x_check_errors signals an Emacs error if an X error has
7492 occurred since the last call to x_catch_errors or x_check_errors.
7493
7494 Calling x_uncatch_errors resumes the normal error handling. */
7495
7496void x_check_errors ();
7497static Lisp_Object x_catch_errors_unwind ();
7498
7499int
7500x_catch_errors (dpy)
7501 Display *dpy;
7502{
aed13378 7503 int count = SPECPDL_INDEX ();
e99db5a1
RS
7504
7505 /* Make sure any errors from previous requests have been dealt with. */
7506 XSync (dpy, False);
7507
781d152a
RS
7508 record_unwind_protect (x_catch_errors_unwind,
7509 Fcons (make_save_value (dpy, 0),
7510 x_error_message_string));
e99db5a1
RS
7511
7512 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
7da167cd 7513 SSET (x_error_message_string, 0, 0);
e99db5a1
RS
7514
7515 return count;
7516}
7517
7518/* Unbind the binding that we made to check for X errors. */
7519
7520static Lisp_Object
7521x_catch_errors_unwind (old_val)
7522 Lisp_Object old_val;
7523{
2728b5d0
JD
7524 Lisp_Object first = XCAR (old_val);
7525 Display *dpy = XSAVE_VALUE (first)->pointer;
781d152a 7526
2728b5d0
JD
7527 /* The display may have been closed before this function is called.
7528 Check if it is still open before calling XSync. */
7529 if (x_display_info_for_display (dpy) != 0)
958f04e8
KS
7530 {
7531 BLOCK_INPUT;
7532 XSync (dpy, False);
7533 UNBLOCK_INPUT;
7534 }
781d152a
RS
7535
7536 x_error_message_string = XCDR (old_val);
e99db5a1
RS
7537 return Qnil;
7538}
7539
7540/* If any X protocol errors have arrived since the last call to
7541 x_catch_errors or x_check_errors, signal an Emacs error using
7542 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7543
7544void
7545x_check_errors (dpy, format)
7546 Display *dpy;
7547 char *format;
7548{
7549 /* Make sure to catch any errors incurred so far. */
7550 XSync (dpy, False);
7551
d5db4077
KR
7552 if (SREF (x_error_message_string, 0))
7553 error (format, SDATA (x_error_message_string));
e99db5a1
RS
7554}
7555
9829ddba
RS
7556/* Nonzero if we had any X protocol errors
7557 since we did x_catch_errors on DPY. */
e99db5a1
RS
7558
7559int
7560x_had_errors_p (dpy)
7561 Display *dpy;
7562{
7563 /* Make sure to catch any errors incurred so far. */
7564 XSync (dpy, False);
7565
d5db4077 7566 return SREF (x_error_message_string, 0) != 0;
e99db5a1
RS
7567}
7568
9829ddba
RS
7569/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7570
06a2c219 7571void
9829ddba
RS
7572x_clear_errors (dpy)
7573 Display *dpy;
7574{
7da167cd 7575 SSET (x_error_message_string, 0, 0);
9829ddba
RS
7576}
7577
e99db5a1
RS
7578/* Stop catching X protocol errors and let them make Emacs die.
7579 DPY should be the display that was passed to x_catch_errors.
7580 COUNT should be the value that was returned by
7581 the corresponding call to x_catch_errors. */
7582
7583void
7584x_uncatch_errors (dpy, count)
7585 Display *dpy;
7586 int count;
7587{
7588 unbind_to (count, Qnil);
7589}
7590
7591#if 0
7592static unsigned int x_wire_count;
7593x_trace_wire ()
7594{
7595 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7596}
7597#endif /* ! 0 */
7598
7599\f
7600/* Handle SIGPIPE, which can happen when the connection to a server
7601 simply goes away. SIGPIPE is handled by x_connection_signal.
7d0393cf 7602 Don't need to do anything, because the write which caused the
e99db5a1 7603 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
06a2c219 7604 which will do the appropriate cleanup for us. */
7d0393cf 7605
e99db5a1
RS
7606static SIGTYPE
7607x_connection_signal (signalnum) /* If we don't have an argument, */
06a2c219 7608 int signalnum; /* some compilers complain in signal calls. */
e99db5a1
RS
7609{
7610#ifdef USG
7611 /* USG systems forget handlers when they are used;
7612 must reestablish each time */
7613 signal (signalnum, x_connection_signal);
7614#endif /* USG */
7615}
0da1ab50 7616
e99db5a1 7617\f
0da1ab50
GM
7618/************************************************************************
7619 Handling X errors
7620 ************************************************************************/
4746118a 7621
f0e299de
GM
7622/* Error message passed to x_connection_closed. */
7623
7624static char *error_msg;
7625
a7248e4f 7626/* Function installed as fatal_error_signal_hook in
f0e299de
GM
7627 x_connection_closed. Print the X error message, and exit normally,
7628 instead of dumping core when XtCloseDisplay fails. */
7629
7630static void
7631x_fatal_error_signal ()
7632{
7633 fprintf (stderr, "%s\n", error_msg);
7634 exit (70);
7635}
7636
0da1ab50
GM
7637/* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7638 the text of an error message that lead to the connection loss. */
16bd92ea 7639
4746118a 7640static SIGTYPE
5978125e
GM
7641x_connection_closed (dpy, error_message)
7642 Display *dpy;
7a13e894 7643 char *error_message;
4746118a 7644{
5978125e 7645 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7a13e894 7646 Lisp_Object frame, tail;
0da1ab50 7647 int count;
7d0393cf 7648
f0e299de
GM
7649 error_msg = (char *) alloca (strlen (error_message) + 1);
7650 strcpy (error_msg, error_message);
1a532e54 7651 handling_signal = 0;
7d0393cf 7652
0da1ab50
GM
7653 /* Prevent being called recursively because of an error condition
7654 below. Otherwise, we might end up with printing ``can't find per
7655 display information'' in the recursive call instead of printing
7656 the original message here. */
7657 count = x_catch_errors (dpy);
7d0393cf 7658
8a4f36cc
GM
7659 /* We have to close the display to inform Xt that it doesn't
7660 exist anymore. If we don't, Xt will continue to wait for
7661 events from the display. As a consequence, a sequence of
7662
7663 M-x make-frame-on-display RET :1 RET
7664 ...kill the new frame, so that we get an IO error...
7665 M-x make-frame-on-display RET :1 RET
7666
7667 will indefinitely wait in Xt for events for display `:1', opened
7668 in the first class to make-frame-on-display.
6186a4a0 7669
8a4f36cc
GM
7670 Closing the display is reported to lead to a bus error on
7671 OpenWindows in certain situations. I suspect that is a bug
7672 in OpenWindows. I don't know how to cicumvent it here. */
7d0393cf 7673
f613a4c8 7674#ifdef USE_X_TOOLKIT
ae24cb3b
GM
7675 /* If DPYINFO is null, this means we didn't open the display
7676 in the first place, so don't try to close it. */
7677 if (dpyinfo)
f0e299de
GM
7678 {
7679 extern void (*fatal_error_signal_hook) P_ ((void));
7680 fatal_error_signal_hook = x_fatal_error_signal;
7681 XtCloseDisplay (dpy);
7682 fatal_error_signal_hook = NULL;
7683 }
f613a4c8 7684#endif
adabc3a9 7685
810f2256
JD
7686#ifdef USE_GTK
7687 if (dpyinfo)
7688 xg_display_close (dpyinfo->display);
7689#endif
7690
8a4f36cc 7691 /* Indicate that this display is dead. */
9e80b57d
KR
7692 if (dpyinfo)
7693 dpyinfo->display = 0;
6186a4a0 7694
06a2c219 7695 /* First delete frames whose mini-buffers are on frames
7a13e894
RS
7696 that are on the dead display. */
7697 FOR_EACH_FRAME (tail, frame)
7698 {
7699 Lisp_Object minibuf_frame;
7700 minibuf_frame
7701 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
f48f33ca
RS
7702 if (FRAME_X_P (XFRAME (frame))
7703 && FRAME_X_P (XFRAME (minibuf_frame))
7704 && ! EQ (frame, minibuf_frame)
7705 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7a13e894
RS
7706 Fdelete_frame (frame, Qt);
7707 }
7708
7709 /* Now delete all remaining frames on the dead display.
06a2c219 7710 We are now sure none of these is used as the mini-buffer
7a13e894
RS
7711 for another frame that we need to delete. */
7712 FOR_EACH_FRAME (tail, frame)
f48f33ca
RS
7713 if (FRAME_X_P (XFRAME (frame))
7714 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
07a7096a
KH
7715 {
7716 /* Set this to t so that Fdelete_frame won't get confused
7717 trying to find a replacement. */
7718 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
7719 Fdelete_frame (frame, Qt);
7720 }
7a13e894 7721
482a1bd2
KH
7722 if (dpyinfo)
7723 x_delete_display (dpyinfo);
7a13e894 7724
0da1ab50 7725 x_uncatch_errors (dpy, count);
7d0393cf 7726
7a13e894
RS
7727 if (x_display_list == 0)
7728 {
f0e299de 7729 fprintf (stderr, "%s\n", error_msg);
7a13e894
RS
7730 shut_down_emacs (0, 0, Qnil);
7731 exit (70);
7732 }
12ba150f 7733
7a13e894
RS
7734 /* Ordinary stack unwind doesn't deal with these. */
7735#ifdef SIGIO
7736 sigunblock (sigmask (SIGIO));
7737#endif
7738 sigunblock (sigmask (SIGALRM));
7739 TOTALLY_UNBLOCK_INPUT;
7740
aa4d9a9e 7741 clear_waiting_for_input ();
f0e299de 7742 error ("%s", error_msg);
4746118a
JB
7743}
7744
043f7f73
SM
7745/* We specifically use it before defining it, so that gcc doesn't inline it,
7746 otherwise gdb doesn't know how to properly put a breakpoint on it. */
7747static void x_error_quitter (Display *display, XErrorEvent *error);
7748
b70001a1
RS
7749/* This is the first-level handler for X protocol errors.
7750 It calls x_error_quitter or x_error_catcher. */
7751
7752static int
7753x_error_handler (display, error)
7754 Display *display;
7755 XErrorEvent *error;
7756{
7757 if (! NILP (x_error_message_string))
7758 x_error_catcher (display, error);
7759 else
7760 x_error_quitter (display, error);
7761 return 0;
7762}
0da1ab50 7763
7a13e894
RS
7764/* This is the usual handler for X protocol errors.
7765 It kills all frames on the display that we got the error for.
7766 If that was the only one, it prints an error message and kills Emacs. */
7767
799775ff 7768/* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
b70001a1 7769
799775ff
RS
7770#if __GNUC__ >= 3 /* On GCC 3.0 we might get a warning. */
7771#define NO_INLINE __attribute__((noinline))
7772#else
7773#define NO_INLINE
7774#endif
7775
285a529b
RS
7776/* Some versions of GNU/Linux define noinline in their headers. */
7777
5599ac10
RS
7778#ifdef noinline
7779#undef noinline
285a529b
RS
7780#endif
7781
799775ff
RS
7782/* On older GCC versions, just putting x_error_quitter
7783 after x_error_handler prevents inlining into the former. */
7784
7785static void NO_INLINE
c118dd06
JB
7786x_error_quitter (display, error)
7787 Display *display;
7788 XErrorEvent *error;
7789{
7a13e894 7790 char buf[256], buf1[356];
dc6f92b8 7791
58769bee 7792 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 7793 original error handler. */
dc6f92b8 7794
c118dd06 7795 XGetErrorText (display, error->error_code, buf, sizeof (buf));
0a0fdc70 7796 sprintf (buf1, "X protocol error: %s on protocol request %d",
c118dd06 7797 buf, error->request_code);
7a13e894 7798 x_connection_closed (display, buf1);
dc6f92b8
JB
7799}
7800
0da1ab50 7801
e99db5a1
RS
7802/* This is the handler for X IO errors, always.
7803 It kills all frames on the display that we lost touch with.
7804 If that was the only one, it prints an error message and kills Emacs. */
7a13e894 7805
c118dd06 7806static int
e99db5a1 7807x_io_error_quitter (display)
c118dd06 7808 Display *display;
c118dd06 7809{
e99db5a1 7810 char buf[256];
dc6f92b8 7811
e99db5a1
RS
7812 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
7813 x_connection_closed (display, buf);
06a2c219 7814 return 0;
dc6f92b8 7815}
dc6f92b8 7816\f
f451eb13
JB
7817/* Changing the font of the frame. */
7818
76bcdf39
RS
7819/* Give frame F the font named FONTNAME as its default font, and
7820 return the full name of that font. FONTNAME may be a wildcard
7821 pattern; in that case, we choose some font that fits the pattern.
7822 The return value shows which font we chose. */
7823
b5cf7a0e 7824Lisp_Object
f676886a
JB
7825x_new_font (f, fontname)
7826 struct frame *f;
dc6f92b8
JB
7827 register char *fontname;
7828{
dc43ef94 7829 struct font_info *fontp
ee569018 7830 = FS_LOAD_FONT (f, 0, fontname, -1);
dc6f92b8 7831
dc43ef94
KH
7832 if (!fontp)
7833 return Qnil;
2224a5fc 7834
0899d58c
KS
7835 FRAME_FONT (f) = (XFontStruct *) (fontp->font);
7836 FRAME_BASELINE_OFFSET (f) = fontp->baseline_offset;
7837 FRAME_FONTSET (f) = -1;
7838
6875d1ae
KH
7839 FRAME_COLUMN_WIDTH (f) = fontp->average_width;
7840 FRAME_SPACE_WIDTH (f) = fontp->space_width;
0899d58c 7841 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (FRAME_FONT (f));
976b73d7 7842
5958f265 7843 compute_fringe_widths (f, 1);
976b73d7 7844
b2cad826 7845 /* Compute the scroll bar width in character columns. */
0899d58c 7846 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
b2cad826 7847 {
0899d58c
KS
7848 int wid = FRAME_COLUMN_WIDTH (f);
7849 FRAME_CONFIG_SCROLL_BAR_COLS (f)
7850 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
b2cad826
KH
7851 }
7852 else
4e61bddf 7853 {
0899d58c
KS
7854 int wid = FRAME_COLUMN_WIDTH (f);
7855 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
4e61bddf 7856 }
b2cad826 7857
f676886a 7858 /* Now make the frame display the given font. */
c118dd06 7859 if (FRAME_X_WINDOW (f) != 0)
dc6f92b8 7860 {
7556890b 7861 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
0899d58c 7862 FRAME_FONT (f)->fid);
7556890b 7863 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
0899d58c 7864 FRAME_FONT (f)->fid);
7556890b 7865 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
0899d58c 7866 FRAME_FONT (f)->fid);
3497f73e
GM
7867
7868 /* Don't change the size of a tip frame; there's no point in
7869 doing it because it's done in Fx_show_tip, and it leads to
7870 problems because the tip frame has no widget. */
7871 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
0899d58c 7872 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
dc6f92b8
JB
7873 }
7874
dc43ef94
KH
7875 return build_string (fontp->full_name);
7876}
7877
7878/* Give frame F the fontset named FONTSETNAME as its default font, and
7879 return the full name of that fontset. FONTSETNAME may be a wildcard
b5210ea7
KH
7880 pattern; in that case, we choose some fontset that fits the pattern.
7881 The return value shows which fontset we chose. */
b5cf7a0e 7882
dc43ef94
KH
7883Lisp_Object
7884x_new_fontset (f, fontsetname)
7885 struct frame *f;
7886 char *fontsetname;
7887{
ee569018 7888 int fontset = fs_query_fontset (build_string (fontsetname), 0);
dc43ef94 7889 Lisp_Object result;
b5cf7a0e 7890
dc43ef94
KH
7891 if (fontset < 0)
7892 return Qnil;
b5cf7a0e 7893
0899d58c 7894 if (FRAME_FONTSET (f) == fontset)
2da424f1
KH
7895 /* This fontset is already set in frame F. There's nothing more
7896 to do. */
ee569018 7897 return fontset_name (fontset);
dc43ef94 7898
d5db4077 7899 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
dc43ef94
KH
7900
7901 if (!STRINGP (result))
7902 /* Can't load ASCII font. */
7903 return Qnil;
7904
7905 /* Since x_new_font doesn't update any fontset information, do it now. */
0899d58c 7906 FRAME_FONTSET (f) = fontset;
dc43ef94 7907
f5d11644
GM
7908#ifdef HAVE_X_I18N
7909 if (FRAME_XIC (f)
7910 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
d5db4077 7911 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
f5d11644 7912#endif
7d0393cf 7913
dc43ef94 7914 return build_string (fontsetname);
dc6f92b8 7915}
f5d11644
GM
7916
7917\f
7918/***********************************************************************
7919 X Input Methods
7920 ***********************************************************************/
7921
7922#ifdef HAVE_X_I18N
7923
7924#ifdef HAVE_X11R6
7925
7926/* XIM destroy callback function, which is called whenever the
7927 connection to input method XIM dies. CLIENT_DATA contains a
7928 pointer to the x_display_info structure corresponding to XIM. */
7929
7930static void
7931xim_destroy_callback (xim, client_data, call_data)
7932 XIM xim;
7933 XPointer client_data;
7934 XPointer call_data;
7935{
7936 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
7937 Lisp_Object frame, tail;
7d0393cf 7938
f5d11644 7939 BLOCK_INPUT;
7d0393cf 7940
f5d11644
GM
7941 /* No need to call XDestroyIC.. */
7942 FOR_EACH_FRAME (tail, frame)
7943 {
7944 struct frame *f = XFRAME (frame);
7945 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
7946 {
7947 FRAME_XIC (f) = NULL;
c27ed90a 7948 xic_free_xfontset (f);
f5d11644
GM
7949 }
7950 }
7d0393cf 7951
f5d11644
GM
7952 /* No need to call XCloseIM. */
7953 dpyinfo->xim = NULL;
7954 XFree (dpyinfo->xim_styles);
7955 UNBLOCK_INPUT;
7956}
7957
7958#endif /* HAVE_X11R6 */
7959
dbd4b028
DL
7960#ifdef HAVE_X11R6
7961/* This isn't prototyped in OSF 5.0 or 5.1a. */
7962extern char *XSetIMValues P_ ((XIM, ...));
7963#endif
7964
f5d11644
GM
7965/* Open the connection to the XIM server on display DPYINFO.
7966 RESOURCE_NAME is the resource name Emacs uses. */
7967
7968static void
7969xim_open_dpy (dpyinfo, resource_name)
7970 struct x_display_info *dpyinfo;
7971 char *resource_name;
7972{
7973 XIM xim;
7974
7e7ade6e 7975#ifdef HAVE_XIM
51f3cc3b 7976 if (use_xim)
f5d11644 7977 {
51f3cc3b
DL
7978 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
7979 EMACS_CLASS);
7980 dpyinfo->xim = xim;
7981
7982 if (xim)
7983 {
f5d11644 7984#ifdef HAVE_X11R6
51f3cc3b 7985 XIMCallback destroy;
f5d11644 7986#endif
7d0393cf 7987
51f3cc3b
DL
7988 /* Get supported styles and XIM values. */
7989 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
7d0393cf 7990
f5d11644 7991#ifdef HAVE_X11R6
51f3cc3b
DL
7992 destroy.callback = xim_destroy_callback;
7993 destroy.client_data = (XPointer)dpyinfo;
7994 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
f5d11644 7995#endif
51f3cc3b 7996 }
f5d11644 7997 }
7d0393cf 7998
51f3cc3b 7999 else
7e7ade6e 8000#endif /* HAVE_XIM */
51f3cc3b 8001 dpyinfo->xim = NULL;
f5d11644
GM
8002}
8003
8004
b9de836c 8005#ifdef HAVE_X11R6_XIM
f5d11644
GM
8006
8007struct xim_inst_t
8008{
8009 struct x_display_info *dpyinfo;
8010 char *resource_name;
8011};
8012
8013/* XIM instantiate callback function, which is called whenever an XIM
1fcfb866 8014 server is available. DISPLAY is the display of the XIM.
f5d11644
GM
8015 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8016 when the callback was registered. */
8017
8018static void
8019xim_instantiate_callback (display, client_data, call_data)
8020 Display *display;
8021 XPointer client_data;
8022 XPointer call_data;
8023{
8024 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8025 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8026
8027 /* We don't support multiple XIM connections. */
8028 if (dpyinfo->xim)
8029 return;
7d0393cf 8030
f5d11644
GM
8031 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8032
8033 /* Create XIC for the existing frames on the same display, as long
8034 as they have no XIC. */
8035 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8036 {
8037 Lisp_Object tail, frame;
8038
8039 BLOCK_INPUT;
8040 FOR_EACH_FRAME (tail, frame)
8041 {
8042 struct frame *f = XFRAME (frame);
7d0393cf 8043
f5d11644
GM
8044 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8045 if (FRAME_XIC (f) == NULL)
8046 {
8047 create_frame_xic (f);
8048 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8049 xic_set_statusarea (f);
8050 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8051 {
8052 struct window *w = XWINDOW (f->selected_window);
8053 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8054 }
8055 }
8056 }
7d0393cf 8057
f5d11644
GM
8058 UNBLOCK_INPUT;
8059 }
8060}
8061
b9de836c 8062#endif /* HAVE_X11R6_XIM */
f5d11644
GM
8063
8064
8065/* Open a connection to the XIM server on display DPYINFO.
8066 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8067 connection only at the first time. On X11R6, open the connection
8068 in the XIM instantiate callback function. */
8069
8070static void
8071xim_initialize (dpyinfo, resource_name)
8072 struct x_display_info *dpyinfo;
8073 char *resource_name;
8074{
7e7ade6e 8075#ifdef HAVE_XIM
51f3cc3b
DL
8076 if (use_xim)
8077 {
b9de836c 8078#ifdef HAVE_X11R6_XIM
51f3cc3b
DL
8079 struct xim_inst_t *xim_inst;
8080 int len;
8081
8082 dpyinfo->xim = NULL;
8083 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8084 xim_inst->dpyinfo = dpyinfo;
8085 len = strlen (resource_name);
8086 xim_inst->resource_name = (char *) xmalloc (len + 1);
8087 bcopy (resource_name, xim_inst->resource_name, len + 1);
8088 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8089 resource_name, EMACS_CLASS,
8090 xim_instantiate_callback,
bcea31c2
DL
8091 /* This is XPointer in XFree86
8092 but (XPointer *) on Tru64, at
8093 least, hence the configure test. */
fdc39b59 8094 (XRegisterIMInstantiateCallback_arg6) xim_inst);
b9de836c 8095#else /* not HAVE_X11R6_XIM */
51f3cc3b
DL
8096 dpyinfo->xim = NULL;
8097 xim_open_dpy (dpyinfo, resource_name);
b9de836c 8098#endif /* not HAVE_X11R6_XIM */
7d0393cf 8099
51f3cc3b
DL
8100 }
8101 else
7e7ade6e 8102#endif /* HAVE_XIM */
51f3cc3b 8103 dpyinfo->xim = NULL;
f5d11644
GM
8104}
8105
8106
8107/* Close the connection to the XIM server on display DPYINFO. */
8108
8109static void
8110xim_close_dpy (dpyinfo)
8111 struct x_display_info *dpyinfo;
8112{
7e7ade6e 8113#ifdef HAVE_XIM
51f3cc3b
DL
8114 if (use_xim)
8115 {
b9de836c 8116#ifdef HAVE_X11R6_XIM
51f3cc3b
DL
8117 if (dpyinfo->display)
8118 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8119 NULL, EMACS_CLASS,
8120 xim_instantiate_callback, NULL);
b9de836c 8121#endif /* not HAVE_X11R6_XIM */
51f3cc3b
DL
8122 if (dpyinfo->display)
8123 XCloseIM (dpyinfo->xim);
8124 dpyinfo->xim = NULL;
8125 XFree (dpyinfo->xim_styles);
8126 }
7e7ade6e 8127#endif /* HAVE_XIM */
f5d11644
GM
8128}
8129
b9de836c 8130#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
8131
8132
dc6f92b8 8133\f
2e365682
RS
8134/* Calculate the absolute position in frame F
8135 from its current recorded position values and gravity. */
8136
dfcf069d 8137void
43bca5d5 8138x_calc_absolute_position (f)
f676886a 8139 struct frame *f;
dc6f92b8 8140{
6dba1858 8141 int win_x = 0, win_y = 0;
0899d58c 8142 int flags = f->size_hint_flags;
c81412a0 8143
9829ddba
RS
8144 /* We have nothing to do if the current position
8145 is already for the top-left corner. */
8146 if (! ((flags & XNegative) || (flags & YNegative)))
8147 return;
8148
6dba1858
RS
8149 /* Treat negative positions as relative to the leftmost bottommost
8150 position that fits on the screen. */
20f55f9a 8151 if (flags & XNegative)
0899d58c 8152 f->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
c83c9f9d 8153 - FRAME_PIXEL_WIDTH (f) + f->left_pos);
dc6f92b8 8154
7708ced0 8155 {
0899d58c 8156 int height = FRAME_PIXEL_HEIGHT (f);
06a2c219 8157
7708ced0
GM
8158#if defined USE_X_TOOLKIT && defined USE_MOTIF
8159 /* Something is fishy here. When using Motif, starting Emacs with
8160 `-g -0-0', the frame appears too low by a few pixels.
8161
8162 This seems to be so because initially, while Emacs is starting,
8163 the column widget's height and the frame's pixel height are
8164 different. The column widget's height is the right one. In
8165 later invocations, when Emacs is up, the frame's pixel height
8166 is right, though.
8167
8168 It's not obvious where the initial small difference comes from.
8169 2000-12-01, gerd. */
7d0393cf 8170
7708ced0 8171 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
06a2c219 8172#endif
2e365682 8173
7708ced0 8174 if (flags & YNegative)
c83c9f9d 8175 f->top_pos = (FRAME_X_DISPLAY_INFO (f)->height - height + f->top_pos);
7708ced0 8176 }
7d0393cf 8177
3a35ab44
RS
8178 /* The left_pos and top_pos
8179 are now relative to the top and left screen edges,
8180 so the flags should correspond. */
0899d58c 8181 f->size_hint_flags &= ~ (XNegative | YNegative);
dc6f92b8
JB
8182}
8183
3a35ab44
RS
8184/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8185 to really change the position, and 0 when calling from
8186 x_make_frame_visible (in that case, XOFF and YOFF are the current
aa3ff7c9
KH
8187 position values). It is -1 when calling from x_set_frame_parameters,
8188 which means, do adjust for borders but don't change the gravity. */
3a35ab44 8189
dfcf069d 8190void
dc05a16b 8191x_set_offset (f, xoff, yoff, change_gravity)
f676886a 8192 struct frame *f;
dc6f92b8 8193 register int xoff, yoff;
dc05a16b 8194 int change_gravity;
dc6f92b8 8195{
4a4cbdd5
KH
8196 int modified_top, modified_left;
8197
aa3ff7c9 8198 if (change_gravity > 0)
3a35ab44 8199 {
0899d58c
KS
8200 f->top_pos = yoff;
8201 f->left_pos = xoff;
8202 f->size_hint_flags &= ~ (XNegative | YNegative);
3a35ab44 8203 if (xoff < 0)
0899d58c 8204 f->size_hint_flags |= XNegative;
3a35ab44 8205 if (yoff < 0)
0899d58c
KS
8206 f->size_hint_flags |= YNegative;
8207 f->win_gravity = NorthWestGravity;
3a35ab44 8208 }
43bca5d5 8209 x_calc_absolute_position (f);
0666d82c 8210
dc6f92b8 8211 BLOCK_INPUT;
c32cdd9a 8212 x_wm_set_size_hint (f, (long) 0, 0);
3a35ab44 8213
0899d58c
KS
8214 modified_left = f->left_pos;
8215 modified_top = f->top_pos;
8f5b9e34 8216
c1f0671a
JD
8217 if (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8218 {
068ae0fd
JD
8219 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8220 than the WM decorations. So we use the calculated offset instead
8221 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8222 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8223 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
c1f0671a
JD
8224 }
8225
488dd4c4
JD
8226 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8227 modified_left, modified_top);
c1f0671a
JD
8228
8229 if (FRAME_VISIBLE_P (f)
8230 && FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN)
8231 {
8232 FRAME_X_OUTPUT (f)->check_expected_move = 1;
8233 FRAME_X_OUTPUT (f)->expected_top = f->top_pos;
8234 FRAME_X_OUTPUT (f)->expected_left = f->left_pos;
8235 }
8236
dc6f92b8
JB
8237 UNBLOCK_INPUT;
8238}
8239
d0fa3e56
EZ
8240/* Check if we need to resize the frame due to a fullscreen request.
8241 If so needed, resize the frame. */
8242static void
8243x_check_fullscreen (f)
8244 struct frame *f;
8245{
0899d58c 8246 if (f->want_fullscreen & FULLSCREEN_BOTH)
d0fa3e56
EZ
8247 {
8248 int width, height, ign;
7d0393cf 8249
0899d58c 8250 x_real_positions (f, &f->left_pos, &f->top_pos);
d0fa3e56
EZ
8251
8252 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
7d0393cf 8253
d0fa3e56
EZ
8254 /* We do not need to move the window, it shall be taken care of
8255 when setting WM manager hints.
8256 If the frame is visible already, the position is checked by
c1f0671a 8257 x_check_expected_move. */
0899d58c 8258 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
d0fa3e56
EZ
8259 {
8260 change_frame_size (f, height, width, 0, 1, 0);
8261 SET_FRAME_GARBAGED (f);
8262 cancel_mouse_face (f);
8263
8264 /* Wait for the change of frame size to occur */
0899d58c 8265 f->want_fullscreen |= FULLSCREEN_WAIT;
d0fa3e56
EZ
8266 }
8267 }
8268}
8269
8270/* If frame parameters are set after the frame is mapped, we need to move
c1f0671a 8271 the window.
d0fa3e56
EZ
8272 Some window managers moves the window to the right position, some
8273 moves the outer window manager window to the specified position.
8274 Here we check that we are in the right spot. If not, make a second
8275 move, assuming we are dealing with the second kind of window manager. */
8276static void
c1f0671a 8277x_check_expected_move (f)
d0fa3e56
EZ
8278 struct frame *f;
8279{
c1f0671a 8280 if (FRAME_X_OUTPUT (f)->check_expected_move)
d0fa3e56 8281 {
c1f0671a
JD
8282 int expect_top = FRAME_X_OUTPUT (f)->expected_top;
8283 int expect_left = FRAME_X_OUTPUT (f)->expected_left;
068ae0fd 8284
0899d58c 8285 if (expect_top != f->top_pos || expect_left != f->left_pos)
c1f0671a 8286 {
068ae0fd
JD
8287 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
8288 FRAME_X_OUTPUT (f)->move_offset_left = expect_left - f->left_pos;
8289 FRAME_X_OUTPUT (f)->move_offset_top = expect_top - f->top_pos;
8290
c83c9f9d
JD
8291 f->left_pos = expect_left;
8292 f->top_pos = expect_top;
8293 x_set_offset (f, expect_left, expect_top, 0);
c1f0671a
JD
8294 }
8295 else if (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN)
8296 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
d0fa3e56
EZ
8297
8298 /* Just do this once */
c1f0671a 8299 FRAME_X_OUTPUT (f)->check_expected_move = 0;
d0fa3e56
EZ
8300 }
8301}
8302
8303
499b1844
GM
8304/* Change the size of frame F's X window to COLS/ROWS in the case F
8305 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8306 top-left-corner window gravity for this size change and subsequent
8307 size changes. Otherwise we leave the window gravity unchanged. */
8308
8309static void
8310x_set_window_size_1 (f, change_gravity, cols, rows)
f676886a 8311 struct frame *f;
bc20ebbf 8312 int change_gravity;
b1c884c3 8313 int cols, rows;
dc6f92b8
JB
8314{
8315 int pixelwidth, pixelheight;
80fd1fe2 8316
b1c884c3 8317 check_frame_size (f, &rows, &cols);
0899d58c 8318 f->scroll_bar_actual_width
b2cad826
KH
8319 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8320 ? 0
0899d58c
KS
8321 : FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0
8322 ? FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
8323 : (FRAME_CONFIG_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f)));
976b73d7 8324
5958f265 8325 compute_fringe_widths (f, 0);
976b73d7 8326
0899d58c
KS
8327 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
8328 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
dc6f92b8 8329
0899d58c 8330 f->win_gravity = NorthWestGravity;
c32cdd9a 8331 x_wm_set_size_hint (f, (long) 0, 0);
6ccf47d1 8332
334208b7
RS
8333 XSync (FRAME_X_DISPLAY (f), False);
8334 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8335 pixelwidth, pixelheight);
b1c884c3
JB
8336
8337 /* Now, strictly speaking, we can't be sure that this is accurate,
8338 but the window manager will get around to dealing with the size
8339 change request eventually, and we'll hear how it went when the
dbc4e1c1
JB
8340 ConfigureNotify event gets here.
8341
8342 We could just not bother storing any of this information here,
8343 and let the ConfigureNotify event set everything up, but that
fddd5ceb 8344 might be kind of confusing to the Lisp code, since size changes
dbc4e1c1 8345 wouldn't be reported in the frame parameters until some random
fddd5ceb
RS
8346 point in the future when the ConfigureNotify event arrives.
8347
8348 We pass 1 for DELAY since we can't run Lisp code inside of
8349 a BLOCK_INPUT. */
7d1e984f 8350 change_frame_size (f, rows, cols, 0, 1, 0);
0899d58c
KS
8351 FRAME_PIXEL_WIDTH (f) = pixelwidth;
8352 FRAME_PIXEL_HEIGHT (f) = pixelheight;
b1c884c3 8353
aee9a898
RS
8354 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8355 receive in the ConfigureNotify event; if we get what we asked
8356 for, then the event won't cause the screen to become garbaged, so
8357 we have to make sure to do it here. */
8358 SET_FRAME_GARBAGED (f);
8359
8360 XFlush (FRAME_X_DISPLAY (f));
499b1844
GM
8361}
8362
8363
8364/* Call this to change the size of frame F's x-window.
8365 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8366 for this size change and subsequent size changes.
8367 Otherwise we leave the window gravity unchanged. */
aee9a898 8368
499b1844
GM
8369void
8370x_set_window_size (f, change_gravity, cols, rows)
8371 struct frame *f;
8372 int change_gravity;
8373 int cols, rows;
8374{
8375 BLOCK_INPUT;
8376
488dd4c4
JD
8377#ifdef USE_GTK
8378 if (FRAME_GTK_WIDGET (f))
8379 xg_frame_set_char_size (f, cols, rows);
8380 else
8381 x_set_window_size_1 (f, change_gravity, cols, rows);
8382#elif USE_X_TOOLKIT
7d0393cf 8383
f1f4d345 8384 if (f->output_data.x->widget != NULL)
499b1844
GM
8385 {
8386 /* The x and y position of the widget is clobbered by the
8387 call to XtSetValues within EmacsFrameSetCharSize.
8388 This is a real kludge, but I don't understand Xt so I can't
8389 figure out a correct fix. Can anyone else tell me? -- rms. */
8390 int xpos = f->output_data.x->widget->core.x;
8391 int ypos = f->output_data.x->widget->core.y;
8392 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
8393 f->output_data.x->widget->core.x = xpos;
8394 f->output_data.x->widget->core.y = ypos;
8395 }
8396 else
8397 x_set_window_size_1 (f, change_gravity, cols, rows);
7d0393cf 8398
499b1844 8399#else /* not USE_X_TOOLKIT */
7d0393cf 8400
499b1844 8401 x_set_window_size_1 (f, change_gravity, cols, rows);
7d0393cf 8402
aee9a898
RS
8403#endif /* not USE_X_TOOLKIT */
8404
4d73d038 8405 /* If cursor was outside the new size, mark it as off. */
06a2c219 8406 mark_window_cursors_off (XWINDOW (f->root_window));
4d73d038 8407
aee9a898 8408 /* Clear out any recollection of where the mouse highlighting was,
7d0393cf 8409 since it might be in a place that's outside the new frame size.
aee9a898
RS
8410 Actually checking whether it is outside is a pain in the neck,
8411 so don't try--just let the highlighting be done afresh with new size. */
e687d06e 8412 cancel_mouse_face (f);
dbc4e1c1 8413
dc6f92b8
JB
8414 UNBLOCK_INPUT;
8415}
dc6f92b8 8416\f
d047c4eb 8417/* Mouse warping. */
dc6f92b8 8418
9b378208 8419void
f676886a
JB
8420x_set_mouse_position (f, x, y)
8421 struct frame *f;
dc6f92b8
JB
8422 int x, y;
8423{
8424 int pix_x, pix_y;
8425
0899d58c
KS
8426 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
8427 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
f451eb13
JB
8428
8429 if (pix_x < 0) pix_x = 0;
0899d58c 8430 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
f451eb13
JB
8431
8432 if (pix_y < 0) pix_y = 0;
0899d58c 8433 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
dc6f92b8
JB
8434
8435 BLOCK_INPUT;
dc6f92b8 8436
334208b7
RS
8437 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8438 0, 0, 0, 0, pix_x, pix_y);
dc6f92b8
JB
8439 UNBLOCK_INPUT;
8440}
8441
9b378208
RS
8442/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
8443
8444void
8445x_set_mouse_pixel_position (f, pix_x, pix_y)
8446 struct frame *f;
8447 int pix_x, pix_y;
8448{
8449 BLOCK_INPUT;
8450
334208b7
RS
8451 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8452 0, 0, 0, 0, pix_x, pix_y);
9b378208
RS
8453 UNBLOCK_INPUT;
8454}
d047c4eb
KH
8455\f
8456/* focus shifting, raising and lowering. */
9b378208 8457
dfcf069d 8458void
f676886a
JB
8459x_focus_on_frame (f)
8460 struct frame *f;
dc6f92b8 8461{
1fb20991 8462#if 0 /* This proves to be unpleasant. */
f676886a 8463 x_raise_frame (f);
1fb20991 8464#endif
6d4238f3
JB
8465#if 0
8466 /* I don't think that the ICCCM allows programs to do things like this
8467 without the interaction of the window manager. Whatever you end up
f676886a 8468 doing with this code, do it to x_unfocus_frame too. */
334208b7 8469 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dc6f92b8 8470 RevertToPointerRoot, CurrentTime);
c118dd06 8471#endif /* ! 0 */
dc6f92b8
JB
8472}
8473
dfcf069d 8474void
f676886a
JB
8475x_unfocus_frame (f)
8476 struct frame *f;
dc6f92b8 8477{
6d4238f3 8478#if 0
f676886a 8479 /* Look at the remarks in x_focus_on_frame. */
0f941935 8480 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
334208b7 8481 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
dc6f92b8 8482 RevertToPointerRoot, CurrentTime);
c118dd06 8483#endif /* ! 0 */
dc6f92b8
JB
8484}
8485
f676886a 8486/* Raise frame F. */
dc6f92b8 8487
dfcf069d 8488void
f676886a
JB
8489x_raise_frame (f)
8490 struct frame *f;
dc6f92b8 8491{
3a88c238 8492 if (f->async_visible)
dc6f92b8
JB
8493 {
8494 BLOCK_INPUT;
2436a4e4 8495 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
334208b7 8496 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
8497 UNBLOCK_INPUT;
8498 }
8499}
8500
f676886a 8501/* Lower frame F. */
dc6f92b8 8502
dfcf069d 8503void
f676886a
JB
8504x_lower_frame (f)
8505 struct frame *f;
dc6f92b8 8506{
3a88c238 8507 if (f->async_visible)
dc6f92b8
JB
8508 {
8509 BLOCK_INPUT;
2436a4e4 8510 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
334208b7 8511 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
8512 UNBLOCK_INPUT;
8513 }
8514}
8515
dbc4e1c1 8516static void
6b0442dc 8517XTframe_raise_lower (f, raise_flag)
dbc4e1c1 8518 FRAME_PTR f;
6b0442dc 8519 int raise_flag;
dbc4e1c1 8520{
6b0442dc 8521 if (raise_flag)
dbc4e1c1
JB
8522 x_raise_frame (f);
8523 else
8524 x_lower_frame (f);
8525}
d047c4eb
KH
8526\f
8527/* Change of visibility. */
dc6f92b8 8528
9382638d
KH
8529/* This tries to wait until the frame is really visible.
8530 However, if the window manager asks the user where to position
8531 the frame, this will return before the user finishes doing that.
8532 The frame will not actually be visible at that time,
8533 but it will become visible later when the window manager
8534 finishes with it. */
8535
dfcf069d 8536void
f676886a
JB
8537x_make_frame_visible (f)
8538 struct frame *f;
dc6f92b8 8539{
990ba854 8540 Lisp_Object type;
1aa6072f 8541 int original_top, original_left;
31be9251
GM
8542 int retry_count = 2;
8543
8544 retry:
dc6f92b8 8545
dc6f92b8 8546 BLOCK_INPUT;
dc6f92b8 8547
990ba854
RS
8548 type = x_icon_type (f);
8549 if (!NILP (type))
8550 x_bitmap_icon (f, type);
bdcd49ba 8551
f676886a 8552 if (! FRAME_VISIBLE_P (f))
90e65f07 8553 {
1aa6072f
RS
8554 /* We test FRAME_GARBAGED_P here to make sure we don't
8555 call x_set_offset a second time
8556 if we get to x_make_frame_visible a second time
8557 before the window gets really visible. */
8558 if (! FRAME_ICONIFIED_P (f)
8559 && ! f->output_data.x->asked_for_visible)
0899d58c 8560 x_set_offset (f, f->left_pos, f->top_pos, 0);
1aa6072f
RS
8561
8562 f->output_data.x->asked_for_visible = 1;
8563
90e65f07 8564 if (! EQ (Vx_no_window_manager, Qt))
f676886a 8565 x_wm_set_window_state (f, NormalState);
3afe33e7 8566#ifdef USE_X_TOOLKIT
d7a38a2e 8567 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 8568 XtMapWidget (f->output_data.x->widget);
3afe33e7 8569#else /* not USE_X_TOOLKIT */
488dd4c4
JD
8570#ifdef USE_GTK
8571 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
7b76ca1c 8572 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
488dd4c4 8573#else
7f9c7f94 8574 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
488dd4c4 8575#endif /* not USE_GTK */
3afe33e7 8576#endif /* not USE_X_TOOLKIT */
0134a210
RS
8577#if 0 /* This seems to bring back scroll bars in the wrong places
8578 if the window configuration has changed. They seem
8579 to come back ok without this. */
ab648270 8580 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
334208b7 8581 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
0134a210 8582#endif
90e65f07 8583 }
dc6f92b8 8584
334208b7 8585 XFlush (FRAME_X_DISPLAY (f));
90e65f07 8586
0dacf791
RS
8587 /* Synchronize to ensure Emacs knows the frame is visible
8588 before we do anything else. We do this loop with input not blocked
8589 so that incoming events are handled. */
8590 {
8591 Lisp_Object frame;
12ce2351 8592 int count;
28c01ffe
RS
8593 /* This must be before UNBLOCK_INPUT
8594 since events that arrive in response to the actions above
8595 will set it when they are handled. */
8596 int previously_visible = f->output_data.x->has_been_visible;
1aa6072f 8597
0899d58c
KS
8598 original_left = f->left_pos;
8599 original_top = f->top_pos;
c0a04927
RS
8600
8601 /* This must come after we set COUNT. */
8602 UNBLOCK_INPUT;
8603
2745e6c4 8604 /* We unblock here so that arriving X events are processed. */
1aa6072f 8605
dcb07ae9
RS
8606 /* Now move the window back to where it was "supposed to be".
8607 But don't do it if the gravity is negative.
8608 When the gravity is negative, this uses a position
28c01ffe
RS
8609 that is 3 pixels too low. Perhaps that's really the border width.
8610
8611 Don't do this if the window has never been visible before,
8612 because the window manager may choose the position
8613 and we don't want to override it. */
1aa6072f 8614
4d3f5d9a 8615 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
0899d58c 8616 && f->win_gravity == NorthWestGravity
28c01ffe 8617 && previously_visible)
1aa6072f 8618 {
2745e6c4
RS
8619 Drawable rootw;
8620 int x, y;
8621 unsigned int width, height, border, depth;
7d0393cf 8622
1aa6072f 8623 BLOCK_INPUT;
9829ddba 8624
06a2c219
GM
8625 /* On some window managers (such as FVWM) moving an existing
8626 window, even to the same place, causes the window manager
8627 to introduce an offset. This can cause the window to move
8628 to an unexpected location. Check the geometry (a little
8629 slow here) and then verify that the window is in the right
8630 place. If the window is not in the right place, move it
8631 there, and take the potential window manager hit. */
2745e6c4
RS
8632 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8633 &rootw, &x, &y, &width, &height, &border, &depth);
8634
8635 if (original_left != x || original_top != y)
8636 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8637 original_left, original_top);
8638
1aa6072f
RS
8639 UNBLOCK_INPUT;
8640 }
9829ddba 8641
e0c1aef2 8642 XSETFRAME (frame, f);
c0a04927 8643
12ce2351
GM
8644 /* Wait until the frame is visible. Process X events until a
8645 MapNotify event has been seen, or until we think we won't get a
8646 MapNotify at all.. */
8647 for (count = input_signal_count + 10;
8648 input_signal_count < count && !FRAME_VISIBLE_P (f);)
2a6cf806 8649 {
12ce2351 8650 /* Force processing of queued events. */
334208b7 8651 x_sync (f);
12ce2351
GM
8652
8653 /* Machines that do polling rather than SIGIO have been
8654 observed to go into a busy-wait here. So we'll fake an
8655 alarm signal to let the handler know that there's something
8656 to be read. We used to raise a real alarm, but it seems
8657 that the handler isn't always enabled here. This is
8658 probably a bug. */
8b2f8d4e 8659 if (input_polling_used ())
3b2fa4e6 8660 {
12ce2351
GM
8661 /* It could be confusing if a real alarm arrives while
8662 processing the fake one. Turn it off and let the
8663 handler reset it. */
3e71d8f2 8664 extern void poll_for_input_1 P_ ((void));
bffcfca9
GM
8665 int old_poll_suppress_count = poll_suppress_count;
8666 poll_suppress_count = 1;
8667 poll_for_input_1 ();
8668 poll_suppress_count = old_poll_suppress_count;
3b2fa4e6 8669 }
12ce2351
GM
8670
8671 /* See if a MapNotify event has been processed. */
8672 FRAME_SAMPLE_VISIBILITY (f);
2a6cf806 8673 }
31be9251
GM
8674
8675 /* 2000-09-28: In
8676
8677 (let ((f (selected-frame)))
8678 (iconify-frame f)
8679 (raise-frame f))
8680
8681 the frame is not raised with various window managers on
554061d8 8682 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
31be9251
GM
8683 unknown reason, the call to XtMapWidget is completely ignored.
8684 Mapping the widget a second time works. */
7d0393cf 8685
31be9251
GM
8686 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
8687 goto retry;
0dacf791 8688 }
dc6f92b8
JB
8689}
8690
06a2c219 8691/* Change from mapped state to withdrawn state. */
dc6f92b8 8692
d047c4eb
KH
8693/* Make the frame visible (mapped and not iconified). */
8694
dfcf069d 8695void
f676886a
JB
8696x_make_frame_invisible (f)
8697 struct frame *f;
dc6f92b8 8698{
546e6d5b
RS
8699 Window window;
8700
546e6d5b 8701 /* Use the frame's outermost window, not the one we normally draw on. */
2436a4e4 8702 window = FRAME_OUTER_WINDOW (f);
dc6f92b8 8703
9319ae23 8704 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
8705 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
8706 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 8707
5627c40e 8708#if 0/* This might add unreliability; I don't trust it -- rms. */
9319ae23 8709 if (! f->async_visible && ! f->async_iconified)
dc6f92b8 8710 return;
5627c40e 8711#endif
dc6f92b8
JB
8712
8713 BLOCK_INPUT;
c118dd06 8714
af31d76f
RS
8715 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
8716 that the current position of the window is user-specified, rather than
8717 program-specified, so that when the window is mapped again, it will be
8718 placed at the same location, without forcing the user to position it
8719 by hand again (they have already done that once for this window.) */
c32cdd9a 8720 x_wm_set_size_hint (f, (long) 0, 1);
af31d76f 8721
488dd4c4
JD
8722#ifdef USE_GTK
8723 if (FRAME_GTK_OUTER_WIDGET (f))
3c671a56
SM
8724 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
8725 else
488dd4c4 8726#endif
3c671a56 8727 {
c118dd06
JB
8728#ifdef HAVE_X11R4
8729
334208b7
RS
8730 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
8731 DefaultScreen (FRAME_X_DISPLAY (f))))
c118dd06
JB
8732 {
8733 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 8734 error ("Can't notify window manager of window withdrawal");
c118dd06 8735 }
c118dd06 8736#else /* ! defined (HAVE_X11R4) */
16bd92ea 8737
c118dd06 8738 /* Tell the window manager what we're going to do. */
dc6f92b8
JB
8739 if (! EQ (Vx_no_window_manager, Qt))
8740 {
16bd92ea 8741 XEvent unmap;
dc6f92b8 8742
16bd92ea 8743 unmap.xunmap.type = UnmapNotify;
546e6d5b 8744 unmap.xunmap.window = window;
334208b7 8745 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
16bd92ea 8746 unmap.xunmap.from_configure = False;
334208b7
RS
8747 if (! XSendEvent (FRAME_X_DISPLAY (f),
8748 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea 8749 False,
06a2c219 8750 SubstructureRedirectMaskSubstructureNotifyMask,
16bd92ea
JB
8751 &unmap))
8752 {
8753 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 8754 error ("Can't notify window manager of withdrawal");
16bd92ea 8755 }
dc6f92b8
JB
8756 }
8757
16bd92ea 8758 /* Unmap the window ourselves. Cheeky! */
334208b7 8759 XUnmapWindow (FRAME_X_DISPLAY (f), window);
c118dd06 8760#endif /* ! defined (HAVE_X11R4) */
3c671a56 8761 }
dc6f92b8 8762
5627c40e
RS
8763 /* We can't distinguish this from iconification
8764 just by the event that we get from the server.
8765 So we can't win using the usual strategy of letting
8766 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
8767 and synchronize with the server to make sure we agree. */
8768 f->visible = 0;
8769 FRAME_ICONIFIED_P (f) = 0;
8770 f->async_visible = 0;
8771 f->async_iconified = 0;
8772
334208b7 8773 x_sync (f);
5627c40e 8774
dc6f92b8
JB
8775 UNBLOCK_INPUT;
8776}
8777
06a2c219 8778/* Change window state from mapped to iconified. */
dc6f92b8 8779
dfcf069d 8780void
f676886a
JB
8781x_iconify_frame (f)
8782 struct frame *f;
dc6f92b8 8783{
3afe33e7 8784 int result;
990ba854 8785 Lisp_Object type;
dc6f92b8 8786
9319ae23 8787 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
8788 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
8789 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 8790
3a88c238 8791 if (f->async_iconified)
dc6f92b8
JB
8792 return;
8793
3afe33e7 8794 BLOCK_INPUT;
546e6d5b 8795
9af3143a
RS
8796 FRAME_SAMPLE_VISIBILITY (f);
8797
990ba854
RS
8798 type = x_icon_type (f);
8799 if (!NILP (type))
8800 x_bitmap_icon (f, type);
bdcd49ba 8801
488dd4c4
JD
8802#ifdef USE_GTK
8803 if (FRAME_GTK_OUTER_WIDGET (f))
8804 {
8805 if (! FRAME_VISIBLE_P (f))
8806 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
8807
8808 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
8809 f->iconified = 1;
8810 f->visible = 1;
8811 f->async_iconified = 1;
8812 f->async_visible = 0;
8813 UNBLOCK_INPUT;
8814 return;
8815 }
8816#endif
8817
bdcd49ba
RS
8818#ifdef USE_X_TOOLKIT
8819
546e6d5b
RS
8820 if (! FRAME_VISIBLE_P (f))
8821 {
8822 if (! EQ (Vx_no_window_manager, Qt))
8823 x_wm_set_window_state (f, IconicState);
8824 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 8825 XtMapWidget (f->output_data.x->widget);
9cf30a30
RS
8826 /* The server won't give us any event to indicate
8827 that an invisible frame was changed to an icon,
8828 so we have to record it here. */
8829 f->iconified = 1;
1e6bc770 8830 f->visible = 1;
9cf30a30 8831 f->async_iconified = 1;
1e6bc770 8832 f->async_visible = 0;
546e6d5b
RS
8833 UNBLOCK_INPUT;
8834 return;
8835 }
8836
334208b7 8837 result = XIconifyWindow (FRAME_X_DISPLAY (f),
7556890b 8838 XtWindow (f->output_data.x->widget),
334208b7 8839 DefaultScreen (FRAME_X_DISPLAY (f)));
3afe33e7
RS
8840 UNBLOCK_INPUT;
8841
8842 if (!result)
546e6d5b 8843 error ("Can't notify window manager of iconification");
3afe33e7
RS
8844
8845 f->async_iconified = 1;
1e6bc770
RS
8846 f->async_visible = 0;
8847
8c002a25
KH
8848
8849 BLOCK_INPUT;
334208b7 8850 XFlush (FRAME_X_DISPLAY (f));
8c002a25 8851 UNBLOCK_INPUT;
3afe33e7
RS
8852#else /* not USE_X_TOOLKIT */
8853
fd13dbb2
RS
8854 /* Make sure the X server knows where the window should be positioned,
8855 in case the user deiconifies with the window manager. */
8856 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
0899d58c 8857 x_set_offset (f, f->left_pos, f->top_pos, 0);
fd13dbb2 8858
16bd92ea
JB
8859 /* Since we don't know which revision of X we're running, we'll use both
8860 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
8861
8862 /* X11R4: send a ClientMessage to the window manager using the
8863 WM_CHANGE_STATE type. */
8864 {
8865 XEvent message;
58769bee 8866
c118dd06 8867 message.xclient.window = FRAME_X_WINDOW (f);
16bd92ea 8868 message.xclient.type = ClientMessage;
334208b7 8869 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
16bd92ea
JB
8870 message.xclient.format = 32;
8871 message.xclient.data.l[0] = IconicState;
8872
334208b7
RS
8873 if (! XSendEvent (FRAME_X_DISPLAY (f),
8874 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea
JB
8875 False,
8876 SubstructureRedirectMask | SubstructureNotifyMask,
8877 &message))
dc6f92b8
JB
8878 {
8879 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 8880 error ("Can't notify window manager of iconification");
dc6f92b8 8881 }
16bd92ea 8882 }
dc6f92b8 8883
58769bee 8884 /* X11R3: set the initial_state field of the window manager hints to
16bd92ea
JB
8885 IconicState. */
8886 x_wm_set_window_state (f, IconicState);
dc6f92b8 8887
a9c00105
RS
8888 if (!FRAME_VISIBLE_P (f))
8889 {
8890 /* If the frame was withdrawn, before, we must map it. */
7f9c7f94 8891 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
a9c00105
RS
8892 }
8893
3a88c238 8894 f->async_iconified = 1;
1e6bc770 8895 f->async_visible = 0;
dc6f92b8 8896
334208b7 8897 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 8898 UNBLOCK_INPUT;
8c002a25 8899#endif /* not USE_X_TOOLKIT */
dc6f92b8 8900}
19f71add 8901
d047c4eb 8902\f
19f71add 8903/* Free X resources of frame F. */
dc6f92b8 8904
dfcf069d 8905void
19f71add 8906x_free_frame_resources (f)
f676886a 8907 struct frame *f;
dc6f92b8 8908{
7f9c7f94 8909 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
c6ea2775
EZ
8910 Lisp_Object bar;
8911 struct scroll_bar *b;
7f9c7f94 8912
dc6f92b8 8913 BLOCK_INPUT;
c0ff3fab 8914
6186a4a0
RS
8915 /* If a display connection is dead, don't try sending more
8916 commands to the X server. */
19f71add 8917 if (dpyinfo->display)
6186a4a0 8918 {
19f71add 8919 if (f->output_data.x->icon_desc)
6186a4a0 8920 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
c6ea2775
EZ
8921
8922#ifdef USE_X_TOOLKIT
8923 /* Explicitly destroy the scroll bars of the frame. Without
8924 this, we get "BadDrawable" errors from the toolkit later on,
8925 presumably from expose events generated for the disappearing
8926 toolkit scroll bars. */
8927 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
8928 {
8929 b = XSCROLL_BAR (bar);
8930 x_scroll_bar_remove (b);
8931 }
8932#endif
8933
31f41daf 8934#ifdef HAVE_X_I18N
f5d11644
GM
8935 if (FRAME_XIC (f))
8936 free_frame_xic (f);
31f41daf 8937#endif
c6ea2775 8938
3afe33e7 8939#ifdef USE_X_TOOLKIT
06a2c219 8940 if (f->output_data.x->widget)
30ca89f5
GM
8941 {
8942 XtDestroyWidget (f->output_data.x->widget);
8943 f->output_data.x->widget = NULL;
8944 }
c6ea2775
EZ
8945 /* Tooltips don't have widgets, only a simple X window, even if
8946 we are using a toolkit. */
8947 else if (FRAME_X_WINDOW (f))
8948 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
8949
6186a4a0 8950 free_frame_menubar (f);
c6ea2775 8951#else /* !USE_X_TOOLKIT */
488dd4c4
JD
8952
8953#ifdef USE_GTK
8954 /* In the GTK version, tooltips are normal X
8955 frames. We must check and free both types. */
8956 if (FRAME_GTK_OUTER_WIDGET (f))
8957 {
8958 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
8959 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
8960 FRAME_GTK_OUTER_WIDGET (f) = 0;
8961 }
8962#endif /* USE_GTK */
177c0ea7 8963
c6ea2775
EZ
8964 if (FRAME_X_WINDOW (f))
8965 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
8966#endif /* !USE_X_TOOLKIT */
3afe33e7 8967
3e71d8f2
GM
8968 unload_color (f, f->output_data.x->foreground_pixel);
8969 unload_color (f, f->output_data.x->background_pixel);
8970 unload_color (f, f->output_data.x->cursor_pixel);
8971 unload_color (f, f->output_data.x->cursor_foreground_pixel);
8972 unload_color (f, f->output_data.x->border_pixel);
8973 unload_color (f, f->output_data.x->mouse_pixel);
c6ea2775 8974
3e71d8f2
GM
8975 if (f->output_data.x->scroll_bar_background_pixel != -1)
8976 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
8977 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8978 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
7c1bef7a
MB
8979#ifdef USE_TOOLKIT_SCROLL_BARS
8980 /* Scrollbar shadow colors. */
8981 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
8982 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
8983 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
8984 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
8985#endif /* USE_TOOLKIT_SCROLL_BARS */
3e71d8f2
GM
8986 if (f->output_data.x->white_relief.allocated_p)
8987 unload_color (f, f->output_data.x->white_relief.pixel);
8988 if (f->output_data.x->black_relief.allocated_p)
8989 unload_color (f, f->output_data.x->black_relief.pixel);
4ca78676 8990
19f71add
GM
8991 if (FRAME_FACE_CACHE (f))
8992 free_frame_faces (f);
7d0393cf 8993
4ca78676 8994 x_free_gcs (f);
6186a4a0
RS
8995 XFlush (FRAME_X_DISPLAY (f));
8996 }
dc6f92b8 8997
df89d8a4 8998 if (f->output_data.x->saved_menu_event)
06a2c219 8999 xfree (f->output_data.x->saved_menu_event);
0c49ce2f 9000
7556890b 9001 xfree (f->output_data.x);
19f71add 9002 f->output_data.x = NULL;
7d0393cf 9003
0f941935
KH
9004 if (f == dpyinfo->x_focus_frame)
9005 dpyinfo->x_focus_frame = 0;
9006 if (f == dpyinfo->x_focus_event_frame)
9007 dpyinfo->x_focus_event_frame = 0;
9008 if (f == dpyinfo->x_highlight_frame)
9009 dpyinfo->x_highlight_frame = 0;
c0ff3fab 9010
7f9c7f94 9011 if (f == dpyinfo->mouse_face_mouse_frame)
dc05a16b 9012 {
7f9c7f94
RS
9013 dpyinfo->mouse_face_beg_row
9014 = dpyinfo->mouse_face_beg_col = -1;
9015 dpyinfo->mouse_face_end_row
9016 = dpyinfo->mouse_face_end_col = -1;
9017 dpyinfo->mouse_face_window = Qnil;
21323706
RS
9018 dpyinfo->mouse_face_deferred_gc = 0;
9019 dpyinfo->mouse_face_mouse_frame = 0;
dc05a16b 9020 }
0134a210 9021
c0ff3fab 9022 UNBLOCK_INPUT;
dc6f92b8 9023}
19f71add
GM
9024
9025
9026/* Destroy the X window of frame F. */
9027
9028void
9029x_destroy_window (f)
9030 struct frame *f;
9031{
9032 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9033
9034 /* If a display connection is dead, don't try sending more
9035 commands to the X server. */
9036 if (dpyinfo->display != 0)
9037 x_free_frame_resources (f);
9038
9039 dpyinfo->reference_count--;
9040}
9041
dc6f92b8 9042\f
f451eb13
JB
9043/* Setting window manager hints. */
9044
af31d76f
RS
9045/* Set the normal size hints for the window manager, for frame F.
9046 FLAGS is the flags word to use--or 0 meaning preserve the flags
9047 that the window now has.
9048 If USER_POSITION is nonzero, we set the USPosition
488dd4c4
JD
9049 flag (this is useful when FLAGS is 0).
9050 The GTK version is in gtkutils.c */
6dba1858 9051
488dd4c4 9052#ifndef USE_GTK
dfcf069d 9053void
af31d76f 9054x_wm_set_size_hint (f, flags, user_position)
f676886a 9055 struct frame *f;
af31d76f
RS
9056 long flags;
9057 int user_position;
dc6f92b8
JB
9058{
9059 XSizeHints size_hints;
3afe33e7
RS
9060
9061#ifdef USE_X_TOOLKIT
7e4f2521
FP
9062 Arg al[2];
9063 int ac = 0;
9064 Dimension widget_width, widget_height;
488dd4c4 9065#endif
177c0ea7 9066
488dd4c4 9067 Window window = FRAME_OUTER_WINDOW (f);
dc6f92b8 9068
b72a58fd
RS
9069 /* Setting PMaxSize caused various problems. */
9070 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
dc6f92b8 9071
0899d58c
KS
9072 size_hints.x = f->left_pos;
9073 size_hints.y = f->top_pos;
7553a6b7 9074
7e4f2521
FP
9075#ifdef USE_X_TOOLKIT
9076 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
9077 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
7556890b 9078 XtGetValues (f->output_data.x->widget, al, ac);
7e4f2521
FP
9079 size_hints.height = widget_height;
9080 size_hints.width = widget_width;
9081#else /* not USE_X_TOOLKIT */
0899d58c
KS
9082 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9083 size_hints.width = FRAME_PIXEL_WIDTH (f);
7e4f2521 9084#endif /* not USE_X_TOOLKIT */
7553a6b7 9085
0899d58c
KS
9086 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
9087 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
334208b7 9088 size_hints.max_width
0899d58c 9089 = FRAME_X_DISPLAY_INFO (f)->width - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
334208b7 9090 size_hints.max_height
0899d58c 9091 = FRAME_X_DISPLAY_INFO (f)->height - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
0134a210 9092
d067ea8b
KH
9093 /* Calculate the base and minimum sizes.
9094
9095 (When we use the X toolkit, we don't do it here.
9096 Instead we copy the values that the widgets are using, below.) */
9097#ifndef USE_X_TOOLKIT
b1c884c3 9098 {
b0342f17 9099 int base_width, base_height;
0134a210 9100 int min_rows = 0, min_cols = 0;
b0342f17 9101
0899d58c
KS
9102 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9103 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
b0342f17 9104
0134a210 9105 check_frame_size (f, &min_rows, &min_cols);
b0342f17 9106
0134a210
RS
9107 /* The window manager uses the base width hints to calculate the
9108 current number of rows and columns in the frame while
9109 resizing; min_width and min_height aren't useful for this
9110 purpose, since they might not give the dimensions for a
9111 zero-row, zero-column frame.
58769bee 9112
0134a210
RS
9113 We use the base_width and base_height members if we have
9114 them; otherwise, we set the min_width and min_height members
9115 to the size for a zero x zero frame. */
b0342f17
JB
9116
9117#ifdef HAVE_X11R4
0134a210
RS
9118 size_hints.flags |= PBaseSize;
9119 size_hints.base_width = base_width;
9120 size_hints.base_height = base_height;
9121 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9122 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
b0342f17 9123#else
0134a210
RS
9124 size_hints.min_width = base_width;
9125 size_hints.min_height = base_height;
b0342f17 9126#endif
b1c884c3 9127 }
dc6f92b8 9128
d067ea8b 9129 /* If we don't need the old flags, we don't need the old hint at all. */
af31d76f 9130 if (flags)
dc6f92b8 9131 {
d067ea8b
KH
9132 size_hints.flags |= flags;
9133 goto no_read;
9134 }
9135#endif /* not USE_X_TOOLKIT */
9136
9137 {
9138 XSizeHints hints; /* Sometimes I hate X Windows... */
9139 long supplied_return;
9140 int value;
af31d76f
RS
9141
9142#ifdef HAVE_X11R4
d067ea8b
KH
9143 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9144 &supplied_return);
af31d76f 9145#else
d067ea8b 9146 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
af31d76f 9147#endif
58769bee 9148
d067ea8b
KH
9149#ifdef USE_X_TOOLKIT
9150 size_hints.base_height = hints.base_height;
9151 size_hints.base_width = hints.base_width;
9152 size_hints.min_height = hints.min_height;
9153 size_hints.min_width = hints.min_width;
9154#endif
9155
9156 if (flags)
9157 size_hints.flags |= flags;
9158 else
9159 {
9160 if (value == 0)
9161 hints.flags = 0;
9162 if (hints.flags & PSize)
9163 size_hints.flags |= PSize;
9164 if (hints.flags & PPosition)
9165 size_hints.flags |= PPosition;
9166 if (hints.flags & USPosition)
9167 size_hints.flags |= USPosition;
9168 if (hints.flags & USSize)
9169 size_hints.flags |= USSize;
9170 }
9171 }
9172
06a2c219 9173#ifndef USE_X_TOOLKIT
d067ea8b 9174 no_read:
06a2c219 9175#endif
0134a210 9176
af31d76f 9177#ifdef PWinGravity
0899d58c 9178 size_hints.win_gravity = f->win_gravity;
af31d76f 9179 size_hints.flags |= PWinGravity;
dc05a16b 9180
af31d76f 9181 if (user_position)
6dba1858 9182 {
af31d76f
RS
9183 size_hints.flags &= ~ PPosition;
9184 size_hints.flags |= USPosition;
6dba1858 9185 }
2554751d 9186#endif /* PWinGravity */
6dba1858 9187
b0342f17 9188#ifdef HAVE_X11R4
334208b7 9189 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 9190#else
334208b7 9191 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
b0342f17 9192#endif
dc6f92b8 9193}
488dd4c4 9194#endif /* not USE_GTK */
dc6f92b8
JB
9195
9196/* Used for IconicState or NormalState */
06a2c219 9197
dfcf069d 9198void
f676886a
JB
9199x_wm_set_window_state (f, state)
9200 struct frame *f;
dc6f92b8
JB
9201 int state;
9202{
3afe33e7 9203#ifdef USE_X_TOOLKIT
546e6d5b
RS
9204 Arg al[1];
9205
9206 XtSetArg (al[0], XtNinitialState, state);
7556890b 9207 XtSetValues (f->output_data.x->widget, al, 1);
3afe33e7 9208#else /* not USE_X_TOOLKIT */
c118dd06 9209 Window window = FRAME_X_WINDOW (f);
dc6f92b8 9210
7556890b
RS
9211 f->output_data.x->wm_hints.flags |= StateHint;
9212 f->output_data.x->wm_hints.initial_state = state;
b1c884c3 9213
7556890b 9214 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
546e6d5b 9215#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
9216}
9217
dfcf069d 9218void
7f2ae036 9219x_wm_set_icon_pixmap (f, pixmap_id)
f676886a 9220 struct frame *f;
7f2ae036 9221 int pixmap_id;
dc6f92b8 9222{
62fe13a4 9223 Pixmap icon_pixmap, icon_mask;
d2bd6bc4 9224
06a2c219 9225#ifndef USE_X_TOOLKIT
488dd4c4 9226 Window window = FRAME_OUTER_WINDOW (f);
75231bad 9227#endif
dc6f92b8 9228
7f2ae036 9229 if (pixmap_id > 0)
dbc4e1c1 9230 {
d2bd6bc4 9231 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
7556890b 9232 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
62fe13a4
JB
9233 icon_mask = x_bitmap_mask (f, pixmap_id);
9234 f->output_data.x->wm_hints.icon_mask = icon_mask;
dbc4e1c1
JB
9235 }
9236 else
68568555
RS
9237 {
9238 /* It seems there is no way to turn off use of an icon pixmap.
9239 The following line does it, only if no icon has yet been created,
9240 for some window managers. But with mwm it crashes.
9241 Some people say it should clear the IconPixmapHint bit in this case,
9242 but that doesn't work, and the X consortium said it isn't the
9243 right thing at all. Since there is no way to win,
9244 best to explicitly give up. */
9245#if 0
9246 f->output_data.x->wm_hints.icon_pixmap = None;
62fe13a4 9247 f->output_data.x->wm_hints.icon_mask = None;
68568555
RS
9248#else
9249 return;
9250#endif
9251 }
b1c884c3 9252
d2bd6bc4
RS
9253#ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
9254
9255 {
9256 Arg al[1];
9257 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9258 XtSetValues (f->output_data.x->widget, al, 1);
62fe13a4
JB
9259 XtSetArg (al[0], XtNiconMask, icon_mask);
9260 XtSetValues (f->output_data.x->widget, al, 1);
d2bd6bc4
RS
9261 }
9262
9263#else /* not USE_X_TOOLKIT */
7d0393cf 9264
62fe13a4 9265 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
7556890b 9266 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
d2bd6bc4
RS
9267
9268#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
9269}
9270
dfcf069d 9271void
f676886a
JB
9272x_wm_set_icon_position (f, icon_x, icon_y)
9273 struct frame *f;
dc6f92b8
JB
9274 int icon_x, icon_y;
9275{
2436a4e4 9276 Window window = FRAME_OUTER_WINDOW (f);
dc6f92b8 9277
7556890b
RS
9278 f->output_data.x->wm_hints.flags |= IconPositionHint;
9279 f->output_data.x->wm_hints.icon_x = icon_x;
9280 f->output_data.x->wm_hints.icon_y = icon_y;
b1c884c3 9281
7556890b 9282 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
dc6f92b8
JB
9283}
9284
9285\f
06a2c219
GM
9286/***********************************************************************
9287 Fonts
9288 ***********************************************************************/
dc43ef94
KH
9289
9290/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
06a2c219 9291
dc43ef94
KH
9292struct font_info *
9293x_get_font_info (f, font_idx)
9294 FRAME_PTR f;
9295 int font_idx;
9296{
9297 return (FRAME_X_FONT_TABLE (f) + font_idx);
9298}
9299
9300
9c11f79e
GM
9301/* Return a list of names of available fonts matching PATTERN on frame F.
9302
9303 If SIZE is > 0, it is the size (maximum bounds width) of fonts
9304 to be listed.
9305
9306 SIZE < 0 means include scalable fonts.
9307
9308 Frame F null means we have not yet created any frame on X, and
9309 consult the first display in x_display_list. MAXNAMES sets a limit
9310 on how many fonts to match. */
dc43ef94
KH
9311
9312Lisp_Object
9313x_list_fonts (f, pattern, size, maxnames)
9c11f79e 9314 struct frame *f;
dc43ef94
KH
9315 Lisp_Object pattern;
9316 int size;
9317 int maxnames;
9318{
06a2c219
GM
9319 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
9320 Lisp_Object tem, second_best;
9c11f79e
GM
9321 struct x_display_info *dpyinfo
9322 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
9323 Display *dpy = dpyinfo->display;
09c6077f 9324 int try_XLoadQueryFont = 0;
53ca4657 9325 int count;
176c852b 9326 int allow_auto_scaled_font = 0;
9c11f79e
GM
9327
9328 if (size < 0)
9329 {
176c852b 9330 allow_auto_scaled_font = 1;
9c11f79e
GM
9331 size = 0;
9332 }
dc43ef94 9333
6b0efe73 9334 patterns = Fassoc (pattern, Valternate_fontname_alist);
2da424f1
KH
9335 if (NILP (patterns))
9336 patterns = Fcons (pattern, Qnil);
81ba44e5 9337
09c6077f
KH
9338 if (maxnames == 1 && !size)
9339 /* We can return any single font matching PATTERN. */
9340 try_XLoadQueryFont = 1;
9a32686f 9341
8e713be6 9342 for (; CONSP (patterns); patterns = XCDR (patterns))
dc43ef94 9343 {
dc43ef94 9344 int num_fonts;
3e71d8f2 9345 char **names = NULL;
dc43ef94 9346
8e713be6 9347 pattern = XCAR (patterns);
536f4067
RS
9348 /* See if we cached the result for this particular query.
9349 The cache is an alist of the form:
9c11f79e
GM
9350 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
9351 tem = XCDR (dpyinfo->name_list_element);
9352 key = Fcons (Fcons (pattern, make_number (maxnames)),
176c852b 9353 allow_auto_scaled_font ? Qt : Qnil);
9c11f79e
GM
9354 list = Fassoc (key, tem);
9355 if (!NILP (list))
b5210ea7
KH
9356 {
9357 list = Fcdr_safe (list);
9358 /* We have a cashed list. Don't have to get the list again. */
9359 goto label_cached;
9360 }
9361
9362 /* At first, put PATTERN in the cache. */
09c6077f 9363
dc43ef94 9364 BLOCK_INPUT;
17d85edc
KH
9365 count = x_catch_errors (dpy);
9366
09c6077f
KH
9367 if (try_XLoadQueryFont)
9368 {
9369 XFontStruct *font;
9370 unsigned long value;
9371
d5db4077 9372 font = XLoadQueryFont (dpy, SDATA (pattern));
17d85edc
KH
9373 if (x_had_errors_p (dpy))
9374 {
9375 /* This error is perhaps due to insufficient memory on X
9376 server. Let's just ignore it. */
9377 font = NULL;
9378 x_clear_errors (dpy);
9379 }
9380
09c6077f
KH
9381 if (font
9382 && XGetFontProperty (font, XA_FONT, &value))
9383 {
9384 char *name = (char *) XGetAtomName (dpy, (Atom) value);
9385 int len = strlen (name);
01c752b5 9386 char *tmp;
09c6077f 9387
6f6512e8
KH
9388 /* If DXPC (a Differential X Protocol Compressor)
9389 Ver.3.7 is running, XGetAtomName will return null
9390 string. We must avoid such a name. */
9391 if (len == 0)
9392 try_XLoadQueryFont = 0;
9393 else
9394 {
9395 num_fonts = 1;
9396 names = (char **) alloca (sizeof (char *));
9397 /* Some systems only allow alloca assigned to a
9398 simple var. */
9399 tmp = (char *) alloca (len + 1); names[0] = tmp;
9400 bcopy (name, names[0], len + 1);
9401 XFree (name);
9402 }
09c6077f
KH
9403 }
9404 else
9405 try_XLoadQueryFont = 0;
a083fd23
RS
9406
9407 if (font)
9408 XFreeFont (dpy, font);
09c6077f
KH
9409 }
9410
9411 if (!try_XLoadQueryFont)
17d85edc
KH
9412 {
9413 /* We try at least 10 fonts because XListFonts will return
9414 auto-scaled fonts at the head. */
ee5be7c3
JD
9415 if (maxnames < 0)
9416 {
9417 int limit;
9418
9419 for (limit = 500;;)
9420 {
9421 names = XListFonts (dpy, SDATA (pattern), limit, &num_fonts);
9422 if (num_fonts == limit)
9423 {
9424 BLOCK_INPUT;
9425 XFreeFontNames (names);
9426 UNBLOCK_INPUT;
9427 limit *= 2;
9428 }
9429 else
9430 break;
9431 }
9432 }
9433 else
9434 names = XListFonts (dpy, SDATA (pattern), max (maxnames, 10),
9435 &num_fonts);
9436
17d85edc
KH
9437 if (x_had_errors_p (dpy))
9438 {
9439 /* This error is perhaps due to insufficient memory on X
9440 server. Let's just ignore it. */
9441 names = NULL;
9442 x_clear_errors (dpy);
9443 }
9444 }
9445
9446 x_uncatch_errors (dpy, count);
dc43ef94
KH
9447 UNBLOCK_INPUT;
9448
9449 if (names)
9450 {
9451 int i;
dc43ef94
KH
9452
9453 /* Make a list of all the fonts we got back.
9454 Store that in the font cache for the display. */
9455 for (i = 0; i < num_fonts; i++)
9456 {
06a2c219 9457 int width = 0;
dc43ef94 9458 char *p = names[i];
176c852b 9459 int average_width = -1, resx = 0, dashes = 0;
7d0393cf 9460
dc43ef94 9461 /* Count the number of dashes in NAMES[I]. If there are
176c852b
KH
9462 14 dashes, the field value following 9th dash
9463 (RESOLUTION_X) is nonzero, and the field value
9464 following 12th dash (AVERAGE_WIDTH) is 0, this is a
9465 auto-scaled font which is usually too ugly to be used
9466 for editing. Let's ignore it. */
dc43ef94
KH
9467 while (*p)
9468 if (*p++ == '-')
9469 {
9470 dashes++;
9471 if (dashes == 7) /* PIXEL_SIZE field */
9472 width = atoi (p);
176c852b
KH
9473 else if (dashes == 9)
9474 resx = atoi (p);
dc43ef94
KH
9475 else if (dashes == 12) /* AVERAGE_WIDTH field */
9476 average_width = atoi (p);
9477 }
7d0393cf 9478
176c852b
KH
9479 if (allow_auto_scaled_font
9480 || dashes < 14 || average_width != 0 || resx == 0)
dc43ef94
KH
9481 {
9482 tem = build_string (names[i]);
9483 if (NILP (Fassoc (tem, list)))
9484 {
9485 if (STRINGP (Vx_pixel_size_width_font_regexp)
f39db7ea
RS
9486 && ((fast_c_string_match_ignore_case
9487 (Vx_pixel_size_width_font_regexp, names[i]))
dc43ef94
KH
9488 >= 0))
9489 /* We can set the value of PIXEL_SIZE to the
b5210ea7 9490 width of this font. */
dc43ef94
KH
9491 list = Fcons (Fcons (tem, make_number (width)), list);
9492 else
9493 /* For the moment, width is not known. */
9494 list = Fcons (Fcons (tem, Qnil), list);
9495 }
9496 }
9497 }
7d0393cf 9498
09c6077f 9499 if (!try_XLoadQueryFont)
e38f4136
GM
9500 {
9501 BLOCK_INPUT;
9502 XFreeFontNames (names);
9503 UNBLOCK_INPUT;
9504 }
dc43ef94
KH
9505 }
9506
b5210ea7 9507 /* Now store the result in the cache. */
f3fbd155
KR
9508 XSETCDR (dpyinfo->name_list_element,
9509 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
dc43ef94 9510
b5210ea7
KH
9511 label_cached:
9512 if (NILP (list)) continue; /* Try the remaining alternatives. */
dc43ef94 9513
b5210ea7
KH
9514 newlist = second_best = Qnil;
9515 /* Make a list of the fonts that have the right width. */
8e713be6 9516 for (; CONSP (list); list = XCDR (list))
b5210ea7 9517 {
536f4067
RS
9518 int found_size;
9519
8e713be6 9520 tem = XCAR (list);
dc43ef94 9521
8e713be6 9522 if (!CONSP (tem) || NILP (XCAR (tem)))
b5210ea7
KH
9523 continue;
9524 if (!size)
9525 {
8e713be6 9526 newlist = Fcons (XCAR (tem), newlist);
b5210ea7
KH
9527 continue;
9528 }
dc43ef94 9529
8e713be6 9530 if (!INTEGERP (XCDR (tem)))
dc43ef94 9531 {
b5210ea7 9532 /* Since we have not yet known the size of this font, we
9c11f79e 9533 must try slow function call XLoadQueryFont. */
dc43ef94
KH
9534 XFontStruct *thisinfo;
9535
9536 BLOCK_INPUT;
17d85edc 9537 count = x_catch_errors (dpy);
dc43ef94 9538 thisinfo = XLoadQueryFont (dpy,
d5db4077 9539 SDATA (XCAR (tem)));
17d85edc
KH
9540 if (x_had_errors_p (dpy))
9541 {
9542 /* This error is perhaps due to insufficient memory on X
9543 server. Let's just ignore it. */
9544 thisinfo = NULL;
9545 x_clear_errors (dpy);
9546 }
9547 x_uncatch_errors (dpy, count);
dc43ef94
KH
9548 UNBLOCK_INPUT;
9549
9550 if (thisinfo)
9551 {
f3fbd155
KR
9552 XSETCDR (tem,
9553 (thisinfo->min_bounds.width == 0
9554 ? make_number (0)
9555 : make_number (thisinfo->max_bounds.width)));
e38f4136 9556 BLOCK_INPUT;
dc43ef94 9557 XFreeFont (dpy, thisinfo);
e38f4136 9558 UNBLOCK_INPUT;
dc43ef94
KH
9559 }
9560 else
b5210ea7 9561 /* For unknown reason, the previous call of XListFont had
06a2c219 9562 returned a font which can't be opened. Record the size
b5210ea7 9563 as 0 not to try to open it again. */
f3fbd155 9564 XSETCDR (tem, make_number (0));
dc43ef94 9565 }
536f4067 9566
8e713be6 9567 found_size = XINT (XCDR (tem));
536f4067 9568 if (found_size == size)
8e713be6 9569 newlist = Fcons (XCAR (tem), newlist);
536f4067 9570 else if (found_size > 0)
b5210ea7 9571 {
536f4067 9572 if (NILP (second_best))
b5210ea7 9573 second_best = tem;
536f4067
RS
9574 else if (found_size < size)
9575 {
8e713be6
KR
9576 if (XINT (XCDR (second_best)) > size
9577 || XINT (XCDR (second_best)) < found_size)
536f4067
RS
9578 second_best = tem;
9579 }
9580 else
9581 {
8e713be6
KR
9582 if (XINT (XCDR (second_best)) > size
9583 && XINT (XCDR (second_best)) > found_size)
536f4067
RS
9584 second_best = tem;
9585 }
b5210ea7
KH
9586 }
9587 }
9588 if (!NILP (newlist))
9589 break;
9590 else if (!NILP (second_best))
9591 {
8e713be6 9592 newlist = Fcons (XCAR (second_best), Qnil);
b5210ea7 9593 break;
dc43ef94 9594 }
dc43ef94
KH
9595 }
9596
9597 return newlist;
7d0393cf 9598}
dc43ef94 9599
06a2c219
GM
9600
9601#if GLYPH_DEBUG
9602
9603/* Check that FONT is valid on frame F. It is if it can be found in F's
9604 font table. */
9605
9606static void
9607x_check_font (f, font)
9608 struct frame *f;
9609 XFontStruct *font;
9610{
9611 int i;
9612 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9613
9614 xassert (font != NULL);
9615
9616 for (i = 0; i < dpyinfo->n_fonts; i++)
7d0393cf 9617 if (dpyinfo->font_table[i].name
06a2c219
GM
9618 && font == dpyinfo->font_table[i].font)
9619 break;
9620
9621 xassert (i < dpyinfo->n_fonts);
9622}
9623
9624#endif /* GLYPH_DEBUG != 0 */
9625
9626/* Set *W to the minimum width, *H to the minimum font height of FONT.
9627 Note: There are (broken) X fonts out there with invalid XFontStruct
9628 min_bounds contents. For example, handa@etl.go.jp reports that
9629 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
9630 have font->min_bounds.width == 0. */
9631
9632static INLINE void
9633x_font_min_bounds (font, w, h)
9634 XFontStruct *font;
9635 int *w, *h;
9636{
9637 *h = FONT_HEIGHT (font);
9638 *w = font->min_bounds.width;
9639
9640 /* Try to handle the case where FONT->min_bounds has invalid
9641 contents. Since the only font known to have invalid min_bounds
9642 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
9643 if (*w <= 0)
9644 *w = font->max_bounds.width;
9645}
9646
9647
9648/* Compute the smallest character width and smallest font height over
9649 all fonts available on frame F. Set the members smallest_char_width
9650 and smallest_font_height in F's x_display_info structure to
9651 the values computed. Value is non-zero if smallest_font_height or
9652 smallest_char_width become smaller than they were before. */
9653
9654static int
9655x_compute_min_glyph_bounds (f)
9656 struct frame *f;
9657{
9658 int i;
9659 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9660 XFontStruct *font;
9661 int old_width = dpyinfo->smallest_char_width;
9662 int old_height = dpyinfo->smallest_font_height;
7d0393cf 9663
06a2c219
GM
9664 dpyinfo->smallest_font_height = 100000;
9665 dpyinfo->smallest_char_width = 100000;
7d0393cf 9666
06a2c219
GM
9667 for (i = 0; i < dpyinfo->n_fonts; ++i)
9668 if (dpyinfo->font_table[i].name)
9669 {
9670 struct font_info *fontp = dpyinfo->font_table + i;
9671 int w, h;
7d0393cf 9672
06a2c219
GM
9673 font = (XFontStruct *) fontp->font;
9674 xassert (font != (XFontStruct *) ~0);
9675 x_font_min_bounds (font, &w, &h);
7d0393cf 9676
06a2c219
GM
9677 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
9678 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
9679 }
9680
9681 xassert (dpyinfo->smallest_char_width > 0
9682 && dpyinfo->smallest_font_height > 0);
9683
9684 return (dpyinfo->n_fonts == 1
9685 || dpyinfo->smallest_char_width < old_width
9686 || dpyinfo->smallest_font_height < old_height);
9687}
9688
9689
dc43ef94
KH
9690/* Load font named FONTNAME of the size SIZE for frame F, and return a
9691 pointer to the structure font_info while allocating it dynamically.
9692 If SIZE is 0, load any size of font.
9693 If loading is failed, return NULL. */
9694
9695struct font_info *
9696x_load_font (f, fontname, size)
9697 struct frame *f;
9698 register char *fontname;
9699 int size;
9700{
9701 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9702 Lisp_Object font_names;
d645aaa4 9703 int count;
dc43ef94
KH
9704
9705 /* Get a list of all the fonts that match this name. Once we
9706 have a list of matching fonts, we compare them against the fonts
9707 we already have by comparing names. */
09c6077f 9708 font_names = x_list_fonts (f, build_string (fontname), size, 1);
dc43ef94
KH
9709
9710 if (!NILP (font_names))
9711 {
9712 Lisp_Object tail;
9713 int i;
9714
9715 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 9716 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
06a2c219
GM
9717 if (dpyinfo->font_table[i].name
9718 && (!strcmp (dpyinfo->font_table[i].name,
d5db4077 9719 SDATA (XCAR (tail)))
06a2c219 9720 || !strcmp (dpyinfo->font_table[i].full_name,
d5db4077 9721 SDATA (XCAR (tail)))))
dc43ef94
KH
9722 return (dpyinfo->font_table + i);
9723 }
9724
9725 /* Load the font and add it to the table. */
9726 {
9727 char *full_name;
9728 XFontStruct *font;
9729 struct font_info *fontp;
9730 unsigned long value;
06a2c219 9731 int i;
dc43ef94 9732
2da424f1
KH
9733 /* If we have found fonts by x_list_font, load one of them. If
9734 not, we still try to load a font by the name given as FONTNAME
9735 because XListFonts (called in x_list_font) of some X server has
9736 a bug of not finding a font even if the font surely exists and
9737 is loadable by XLoadQueryFont. */
e1d6d5b9 9738 if (size > 0 && !NILP (font_names))
d5db4077 9739 fontname = (char *) SDATA (XCAR (font_names));
dc43ef94
KH
9740
9741 BLOCK_INPUT;
d645aaa4 9742 count = x_catch_errors (FRAME_X_DISPLAY (f));
dc43ef94 9743 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
d645aaa4
KH
9744 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
9745 {
9746 /* This error is perhaps due to insufficient memory on X
9747 server. Let's just ignore it. */
9748 font = NULL;
9749 x_clear_errors (FRAME_X_DISPLAY (f));
9750 }
9751 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
dc43ef94 9752 UNBLOCK_INPUT;
b5210ea7 9753 if (!font)
dc43ef94
KH
9754 return NULL;
9755
06a2c219
GM
9756 /* Find a free slot in the font table. */
9757 for (i = 0; i < dpyinfo->n_fonts; ++i)
9758 if (dpyinfo->font_table[i].name == NULL)
9759 break;
9760
9761 /* If no free slot found, maybe enlarge the font table. */
9762 if (i == dpyinfo->n_fonts
9763 && dpyinfo->n_fonts == dpyinfo->font_table_size)
dc43ef94 9764 {
06a2c219
GM
9765 int sz;
9766 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
9767 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
dc43ef94 9768 dpyinfo->font_table
06a2c219 9769 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
dc43ef94
KH
9770 }
9771
06a2c219
GM
9772 fontp = dpyinfo->font_table + i;
9773 if (i == dpyinfo->n_fonts)
9774 ++dpyinfo->n_fonts;
dc43ef94
KH
9775
9776 /* Now fill in the slots of *FONTP. */
9777 BLOCK_INPUT;
589e039f 9778 bzero (fontp, sizeof (*fontp));
dc43ef94 9779 fontp->font = font;
06a2c219 9780 fontp->font_idx = i;
dc43ef94
KH
9781 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
9782 bcopy (fontname, fontp->name, strlen (fontname) + 1);
9783
6875d1ae
KH
9784 if (font->min_bounds.width == font->max_bounds.width)
9785 {
9786 /* Fixed width font. */
9787 fontp->average_width = fontp->space_width = font->min_bounds.width;
9788 }
9789 else
9790 {
9791 XChar2b char2b;
9792 XCharStruct *pcm;
9793
9794 char2b.byte1 = 0x00, char2b.byte2 = 0x20;
9795 pcm = x_per_char_metric (font, &char2b, 0);
9796 if (pcm)
9797 fontp->space_width = pcm->width;
9798 else
9799 fontp->space_width = FONT_WIDTH (font);
9800
9801 fontp->average_width
9802 = (XGetFontProperty (font, dpyinfo->Xatom_AVERAGE_WIDTH, &value)
9803 ? (long) value / 10 : 0);
9804 if (fontp->average_width < 0)
9805 fontp->average_width = - fontp->average_width;
9806 if (fontp->average_width == 0)
9807 {
9808 if (pcm)
9809 {
9810 int width = pcm->width;
9811 for (char2b.byte2 = 33; char2b.byte2 <= 126; char2b.byte2++)
9812 if ((pcm = x_per_char_metric (font, &char2b, 0)) != NULL)
9813 width += pcm->width;
9814 fontp->average_width = width / 95;
9815 }
9816 else
9817 fontp->average_width = FONT_WIDTH (font);
9818 }
9819 }
9820
dc43ef94
KH
9821 /* Try to get the full name of FONT. Put it in FULL_NAME. */
9822 full_name = 0;
9823 if (XGetFontProperty (font, XA_FONT, &value))
9824 {
9825 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
9826 char *p = name;
9827 int dashes = 0;
9828
9829 /* Count the number of dashes in the "full name".
9830 If it is too few, this isn't really the font's full name,
9831 so don't use it.
9832 In X11R4, the fonts did not come with their canonical names
9833 stored in them. */
9834 while (*p)
9835 {
9836 if (*p == '-')
9837 dashes++;
9838 p++;
9839 }
9840
9841 if (dashes >= 13)
9842 {
9843 full_name = (char *) xmalloc (p - name + 1);
9844 bcopy (name, full_name, p - name + 1);
9845 }
9846
9847 XFree (name);
9848 }
7d0393cf 9849
dc43ef94
KH
9850 if (full_name != 0)
9851 fontp->full_name = full_name;
9852 else
9853 fontp->full_name = fontp->name;
9854
9855 fontp->size = font->max_bounds.width;
d5749adb 9856 fontp->height = FONT_HEIGHT (font);
dc43ef94 9857
2da424f1
KH
9858 if (NILP (font_names))
9859 {
9860 /* We come here because of a bug of XListFonts mentioned at
9861 the head of this block. Let's store this information in
9862 the cache for x_list_fonts. */
9863 Lisp_Object lispy_name = build_string (fontname);
9864 Lisp_Object lispy_full_name = build_string (fontp->full_name);
9c11f79e
GM
9865 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
9866 Qnil);
2da424f1 9867
f3fbd155
KR
9868 XSETCDR (dpyinfo->name_list_element,
9869 Fcons (Fcons (key,
9870 Fcons (Fcons (lispy_full_name,
9871 make_number (fontp->size)),
9872 Qnil)),
9873 XCDR (dpyinfo->name_list_element)));
2da424f1 9874 if (full_name)
9c11f79e
GM
9875 {
9876 key = Fcons (Fcons (lispy_full_name, make_number (256)),
9877 Qnil);
f3fbd155
KR
9878 XSETCDR (dpyinfo->name_list_element,
9879 Fcons (Fcons (key,
9880 Fcons (Fcons (lispy_full_name,
9881 make_number (fontp->size)),
9882 Qnil)),
9883 XCDR (dpyinfo->name_list_element)));
9c11f79e 9884 }
2da424f1
KH
9885 }
9886
dc43ef94
KH
9887 /* The slot `encoding' specifies how to map a character
9888 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
ee569018
KH
9889 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
9890 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
8ff102bd 9891 2:0xA020..0xFF7F). For the moment, we don't know which charset
06a2c219 9892 uses this font. So, we set information in fontp->encoding[1]
8ff102bd
RS
9893 which is never used by any charset. If mapping can't be
9894 decided, set FONT_ENCODING_NOT_DECIDED. */
dc43ef94
KH
9895 fontp->encoding[1]
9896 = (font->max_byte1 == 0
9897 /* 1-byte font */
9898 ? (font->min_char_or_byte2 < 0x80
9899 ? (font->max_char_or_byte2 < 0x80
9900 ? 0 /* 0x20..0x7F */
8ff102bd 9901 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
dc43ef94
KH
9902 : 1) /* 0xA0..0xFF */
9903 /* 2-byte font */
9904 : (font->min_byte1 < 0x80
9905 ? (font->max_byte1 < 0x80
9906 ? (font->min_char_or_byte2 < 0x80
9907 ? (font->max_char_or_byte2 < 0x80
9908 ? 0 /* 0x2020..0x7F7F */
8ff102bd 9909 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
dc43ef94 9910 : 3) /* 0x20A0..0x7FFF */
8ff102bd 9911 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
dc43ef94
KH
9912 : (font->min_char_or_byte2 < 0x80
9913 ? (font->max_char_or_byte2 < 0x80
9914 ? 2 /* 0xA020..0xFF7F */
8ff102bd 9915 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
dc43ef94
KH
9916 : 1))); /* 0xA0A0..0xFFFF */
9917
9918 fontp->baseline_offset
9919 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
9920 ? (long) value : 0);
9921 fontp->relative_compose
9922 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
9923 ? (long) value : 0);
f78798df
KH
9924 fontp->default_ascent
9925 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
9926 ? (long) value : 0);
dc43ef94 9927
06a2c219
GM
9928 /* Set global flag fonts_changed_p to non-zero if the font loaded
9929 has a character with a smaller width than any other character
437dfb9f 9930 before, or if the font loaded has a smaller height than any
06a2c219
GM
9931 other font loaded before. If this happens, it will make a
9932 glyph matrix reallocation necessary. */
437dfb9f 9933 fonts_changed_p |= x_compute_min_glyph_bounds (f);
dc43ef94 9934 UNBLOCK_INPUT;
dc43ef94
KH
9935 return fontp;
9936 }
9937}
9938
06a2c219
GM
9939
9940/* Return a pointer to struct font_info of a font named FONTNAME for
9941 frame F. If no such font is loaded, return NULL. */
9942
dc43ef94
KH
9943struct font_info *
9944x_query_font (f, fontname)
9945 struct frame *f;
9946 register char *fontname;
9947{
9948 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9949 int i;
9950
9951 for (i = 0; i < dpyinfo->n_fonts; i++)
06a2c219
GM
9952 if (dpyinfo->font_table[i].name
9953 && (!strcmp (dpyinfo->font_table[i].name, fontname)
9954 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
dc43ef94
KH
9955 return (dpyinfo->font_table + i);
9956 return NULL;
9957}
9958
06a2c219
GM
9959
9960/* Find a CCL program for a font specified by FONTP, and set the member
a6582676
KH
9961 `encoder' of the structure. */
9962
9963void
9964x_find_ccl_program (fontp)
9965 struct font_info *fontp;
9966{
a42f54e6 9967 Lisp_Object list, elt;
a6582676 9968
f9b5db02 9969 elt = Qnil;
8e713be6 9970 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
a6582676 9971 {
8e713be6 9972 elt = XCAR (list);
a6582676 9973 if (CONSP (elt)
8e713be6 9974 && STRINGP (XCAR (elt))
9f2feff6
KH
9975 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
9976 >= 0)
9977 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
9978 >= 0)))
a42f54e6
KH
9979 break;
9980 }
7d0393cf 9981
a42f54e6
KH
9982 if (! NILP (list))
9983 {
d27f8ca7
KH
9984 struct ccl_program *ccl
9985 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
a42f54e6 9986
8e713be6 9987 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
a42f54e6
KH
9988 xfree (ccl);
9989 else
9990 fontp->font_encoder = ccl;
a6582676
KH
9991 }
9992}
9993
06a2c219 9994
dc43ef94 9995\f
06a2c219
GM
9996/***********************************************************************
9997 Initialization
9998 ***********************************************************************/
f451eb13 9999
3afe33e7
RS
10000#ifdef USE_X_TOOLKIT
10001static XrmOptionDescRec emacs_options[] = {
10002 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
10003 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
10004
10005 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
10006 XrmoptionSepArg, NULL},
10007 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
10008
10009 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10010 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10011 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10012 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10013 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10014 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
10015 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
10016};
10017#endif /* USE_X_TOOLKIT */
10018
7a13e894
RS
10019static int x_initialized;
10020
29b38361
KH
10021#ifdef MULTI_KBOARD
10022/* Test whether two display-name strings agree up to the dot that separates
10023 the screen number from the server number. */
10024static int
10025same_x_server (name1, name2)
59ab1dc7 10026 const char *name1, *name2;
29b38361
KH
10027{
10028 int seen_colon = 0;
59ab1dc7 10029 const unsigned char *system_name = SDATA (Vsystem_name);
cf591cc1
RS
10030 int system_name_length = strlen (system_name);
10031 int length_until_period = 0;
10032
10033 while (system_name[length_until_period] != 0
10034 && system_name[length_until_period] != '.')
10035 length_until_period++;
10036
10037 /* Treat `unix' like an empty host name. */
10038 if (! strncmp (name1, "unix:", 5))
10039 name1 += 4;
10040 if (! strncmp (name2, "unix:", 5))
10041 name2 += 4;
10042 /* Treat this host's name like an empty host name. */
10043 if (! strncmp (name1, system_name, system_name_length)
10044 && name1[system_name_length] == ':')
10045 name1 += system_name_length;
10046 if (! strncmp (name2, system_name, system_name_length)
10047 && name2[system_name_length] == ':')
10048 name2 += system_name_length;
10049 /* Treat this host's domainless name like an empty host name. */
10050 if (! strncmp (name1, system_name, length_until_period)
10051 && name1[length_until_period] == ':')
10052 name1 += length_until_period;
10053 if (! strncmp (name2, system_name, length_until_period)
10054 && name2[length_until_period] == ':')
10055 name2 += length_until_period;
10056
29b38361
KH
10057 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
10058 {
10059 if (*name1 == ':')
10060 seen_colon++;
10061 if (seen_colon && *name1 == '.')
10062 return 1;
10063 }
10064 return (seen_colon
10065 && (*name1 == '.' || *name1 == '\0')
10066 && (*name2 == '.' || *name2 == '\0'));
10067}
10068#endif
10069
9d35adc7
JD
10070/* Count number of set bits in mask and number of bits to shift to
10071 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
10072 to 5. */
10073static void
10074get_bits_and_offset (mask, bits, offset)
10075 unsigned long mask;
10076 int *bits;
10077 int *offset;
10078{
10079 int nr = 0;
10080 int off = 0;
10081
10082 while (!(mask & 1))
10083 {
10084 off++;
10085 mask >>= 1;
10086 }
10087
10088 while (mask & 1)
10089 {
10090 nr++;
10091 mask >>= 1;
10092 }
10093
10094 *offset = off;
10095 *bits = nr;
10096}
10097
334208b7 10098struct x_display_info *
1f8255f2 10099x_term_init (display_name, xrm_option, resource_name)
334208b7 10100 Lisp_Object display_name;
1f8255f2
RS
10101 char *xrm_option;
10102 char *resource_name;
dc6f92b8 10103{
334208b7 10104 int connection;
7a13e894 10105 Display *dpy;
334208b7
RS
10106 struct x_display_info *dpyinfo;
10107 XrmDatabase xrdb;
10108
60439948
KH
10109 BLOCK_INPUT;
10110
7a13e894
RS
10111 if (!x_initialized)
10112 {
10113 x_initialize ();
231d6cfb 10114 ++x_initialized;
7a13e894 10115 }
dc6f92b8 10116
488dd4c4
JD
10117#ifdef USE_GTK
10118 {
10119#define NUM_ARGV 10
10120 int argc;
10121 char *argv[NUM_ARGV];
10122 char **argv2 = argv;
10123 GdkAtom atom;
10124
810f2256
JD
10125 if (x_initialized++ > 1)
10126 {
10127 /* Opening another display. If xg_display_open returns less
10128 than zero, we are probably on GTK 2.0, which can only handle
10129 one display. GTK 2.2 or later can handle more than one. */
10130 if (xg_display_open (SDATA (display_name), &dpy) < 0)
10131 error ("Sorry, this version of GTK can only handle one display");
10132 }
10133 else
10134 {
10135 for (argc = 0; argc < NUM_ARGV; ++argc)
10136 argv[argc] = 0;
488dd4c4 10137
810f2256
JD
10138 argc = 0;
10139 argv[argc++] = initial_argv[0];
488dd4c4 10140
810f2256
JD
10141 if (! NILP (display_name))
10142 {
10143 argv[argc++] = "--display";
10144 argv[argc++] = SDATA (display_name);
10145 }
488dd4c4 10146
810f2256
JD
10147 argv[argc++] = "--name";
10148 argv[argc++] = resource_name;
177c0ea7 10149
1fcfb866 10150#ifdef HAVE_X11R5
810f2256 10151 XSetLocaleModifiers ("");
1fcfb866
JD
10152#endif
10153
810f2256 10154 gtk_init (&argc, &argv2);
488dd4c4 10155
810f2256
JD
10156 /* gtk_init does set_locale. We must fix locale after calling it. */
10157 fixup_locale ();
10158 xg_initialize ();
488dd4c4 10159
810f2256 10160 dpy = GDK_DISPLAY ();
177c0ea7 10161
810f2256
JD
10162 /* NULL window -> events for all windows go to our function */
10163 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
488dd4c4 10164
810f2256
JD
10165 /* Load our own gtkrc if it exists. */
10166 {
10167 struct gcpro gcpro1, gcpro2;
10168 char *file = "~/.emacs.d/gtkrc";
10169 Lisp_Object s, abs_file;
488dd4c4 10170
810f2256
JD
10171 GCPRO2 (s, abs_file);
10172 s = make_string (file, strlen (file));
10173 abs_file = Fexpand_file_name (s, Qnil);
488dd4c4 10174
810f2256
JD
10175 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
10176 gtk_rc_parse (SDATA (abs_file));
177c0ea7 10177
810f2256
JD
10178 UNGCPRO;
10179 }
177c0ea7 10180
810f2256
JD
10181 XSetErrorHandler (x_error_handler);
10182 XSetIOErrorHandler (x_io_error_quitter);
10183 }
488dd4c4
JD
10184 }
10185#else /* not USE_GTK */
3afe33e7 10186#ifdef USE_X_TOOLKIT
2d7fc7e8
RS
10187 /* weiner@footloose.sps.mot.com reports that this causes
10188 errors with X11R5:
10189 X protocol error: BadAtom (invalid Atom parameter)
10190 on protocol request 18skiloaf.
10191 So let's not use it until R6. */
10192#ifdef HAVE_X11XTR6
bdcd49ba
RS
10193 XtSetLanguageProc (NULL, NULL, NULL);
10194#endif
10195
7f9c7f94
RS
10196 {
10197 int argc = 0;
10198 char *argv[3];
10199
10200 argv[0] = "";
10201 argc = 1;
10202 if (xrm_option)
10203 {
10204 argv[argc++] = "-xrm";
10205 argv[argc++] = xrm_option;
10206 }
bc4e60fc 10207 turn_on_atimers (0);
d5db4077 10208 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
7f9c7f94
RS
10209 resource_name, EMACS_CLASS,
10210 emacs_options, XtNumber (emacs_options),
10211 &argc, argv);
bc4e60fc 10212 turn_on_atimers (1);
39d8bb4d
KH
10213
10214#ifdef HAVE_X11XTR6
10537cb1 10215 /* I think this is to compensate for XtSetLanguageProc. */
71f8198a 10216 fixup_locale ();
39d8bb4d 10217#endif
7f9c7f94 10218 }
3afe33e7
RS
10219
10220#else /* not USE_X_TOOLKIT */
bdcd49ba
RS
10221#ifdef HAVE_X11R5
10222 XSetLocaleModifiers ("");
10223#endif
d5db4077 10224 dpy = XOpenDisplay (SDATA (display_name));
3afe33e7 10225#endif /* not USE_X_TOOLKIT */
488dd4c4 10226#endif /* not USE_GTK*/
334208b7 10227
7a13e894
RS
10228 /* Detect failure. */
10229 if (dpy == 0)
60439948
KH
10230 {
10231 UNBLOCK_INPUT;
10232 return 0;
10233 }
7a13e894
RS
10234
10235 /* We have definitely succeeded. Record the new connection. */
10236
10237 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
f04e1297 10238 bzero (dpyinfo, sizeof *dpyinfo);
7a13e894 10239
29b38361
KH
10240#ifdef MULTI_KBOARD
10241 {
10242 struct x_display_info *share;
10243 Lisp_Object tail;
10244
10245 for (share = x_display_list, tail = x_display_name_list; share;
8e713be6 10246 share = share->next, tail = XCDR (tail))
d5db4077
KR
10247 if (same_x_server (SDATA (XCAR (XCAR (tail))),
10248 SDATA (display_name)))
29b38361
KH
10249 break;
10250 if (share)
10251 dpyinfo->kboard = share->kboard;
10252 else
10253 {
10254 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
10255 init_kboard (dpyinfo->kboard);
59e755be
KH
10256 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10257 {
10258 char *vendor = ServerVendor (dpy);
9b6ed9f3 10259 UNBLOCK_INPUT;
59e755be
KH
10260 dpyinfo->kboard->Vsystem_key_alist
10261 = call1 (Qvendor_specific_keysyms,
10262 build_string (vendor ? vendor : ""));
9b6ed9f3 10263 BLOCK_INPUT;
59e755be
KH
10264 }
10265
29b38361
KH
10266 dpyinfo->kboard->next_kboard = all_kboards;
10267 all_kboards = dpyinfo->kboard;
0ad5446c
KH
10268 /* Don't let the initial kboard remain current longer than necessary.
10269 That would cause problems if a file loaded on startup tries to
06a2c219 10270 prompt in the mini-buffer. */
0ad5446c
KH
10271 if (current_kboard == initial_kboard)
10272 current_kboard = dpyinfo->kboard;
29b38361
KH
10273 }
10274 dpyinfo->kboard->reference_count++;
10275 }
b9737ad3
KH
10276#endif
10277
7a13e894
RS
10278 /* Put this display on the chain. */
10279 dpyinfo->next = x_display_list;
10280 x_display_list = dpyinfo;
10281
7d0393cf 10282 /* Put it on x_display_name_list as well, to keep them parallel. */
7a13e894
RS
10283 x_display_name_list = Fcons (Fcons (display_name, Qnil),
10284 x_display_name_list);
8e713be6 10285 dpyinfo->name_list_element = XCAR (x_display_name_list);
7a13e894
RS
10286
10287 dpyinfo->display = dpy;
dc6f92b8 10288
dc6f92b8 10289#if 0
7a13e894 10290 XSetAfterFunction (x_current_display, x_trace_wire);
c118dd06 10291#endif /* ! 0 */
7a13e894
RS
10292
10293 dpyinfo->x_id_name
d5db4077
KR
10294 = (char *) xmalloc (SBYTES (Vinvocation_name)
10295 + SBYTES (Vsystem_name)
7a13e894
RS
10296 + 2);
10297 sprintf (dpyinfo->x_id_name, "%s@%s",
d5db4077 10298 SDATA (Vinvocation_name), SDATA (Vsystem_name));
28430d3c
JB
10299
10300 /* Figure out which modifier bits mean what. */
334208b7 10301 x_find_modifier_meanings (dpyinfo);
f451eb13 10302
ab648270 10303 /* Get the scroll bar cursor. */
810f2256
JD
10304#ifdef USE_GTK
10305 /* We must create a GTK cursor, it is required for GTK widgets. */
10306 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
10307#endif /* USE_GTK */
10308
7a13e894 10309 dpyinfo->vertical_scroll_bar_cursor
334208b7 10310 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
f451eb13 10311
334208b7
RS
10312 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10313 resource_name, EMACS_CLASS);
10314#ifdef HAVE_XRMSETDATABASE
10315 XrmSetDatabase (dpyinfo->display, xrdb);
10316#else
10317 dpyinfo->display->db = xrdb;
10318#endif
547d9db8 10319 /* Put the rdb where we can find it in a way that works on
7a13e894
RS
10320 all versions. */
10321 dpyinfo->xrdb = xrdb;
334208b7
RS
10322
10323 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10324 DefaultScreen (dpyinfo->display));
5ff67d81 10325 select_visual (dpyinfo);
43bd1b2b 10326 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
334208b7
RS
10327 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
10328 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
10329 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
231d6cfb 10330 dpyinfo->client_leader_window = 0;
334208b7
RS
10331 dpyinfo->grabbed = 0;
10332 dpyinfo->reference_count = 0;
10333 dpyinfo->icon_bitmap_id = -1;
06a2c219 10334 dpyinfo->font_table = NULL;
7a13e894
RS
10335 dpyinfo->n_fonts = 0;
10336 dpyinfo->font_table_size = 0;
10337 dpyinfo->bitmaps = 0;
10338 dpyinfo->bitmaps_size = 0;
10339 dpyinfo->bitmaps_last = 0;
10340 dpyinfo->scratch_cursor_gc = 0;
10341 dpyinfo->mouse_face_mouse_frame = 0;
10342 dpyinfo->mouse_face_deferred_gc = 0;
10343 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10344 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
06a2c219 10345 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
7a13e894 10346 dpyinfo->mouse_face_window = Qnil;
0a61c667 10347 dpyinfo->mouse_face_overlay = Qnil;
7a13e894
RS
10348 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
10349 dpyinfo->mouse_face_defer = 0;
66301061 10350 dpyinfo->mouse_face_hidden = 0;
0f941935
KH
10351 dpyinfo->x_focus_frame = 0;
10352 dpyinfo->x_focus_event_frame = 0;
10353 dpyinfo->x_highlight_frame = 0;
06a2c219 10354 dpyinfo->image_cache = make_image_cache ();
c1f0671a 10355 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
334208b7 10356
9d35adc7
JD
10357 /* See if we can construct pixel values from RGB values. */
10358 dpyinfo->red_bits = dpyinfo->blue_bits = dpyinfo->green_bits = 0;
10359 dpyinfo->red_offset = dpyinfo->blue_offset = dpyinfo->green_offset = 0;
10360
10361 if (dpyinfo->visual->class == TrueColor)
10362 {
10363 get_bits_and_offset (dpyinfo->visual->red_mask,
10364 &dpyinfo->red_bits, &dpyinfo->red_offset);
10365 get_bits_and_offset (dpyinfo->visual->blue_mask,
10366 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
10367 get_bits_and_offset (dpyinfo->visual->green_mask,
10368 &dpyinfo->green_bits, &dpyinfo->green_offset);
10369 }
0666d82c 10370
43bd1b2b 10371 /* See if a private colormap is requested. */
5ff67d81
GM
10372 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10373 {
10374 if (dpyinfo->visual->class == PseudoColor)
10375 {
10376 Lisp_Object value;
10377 value = display_x_get_resource (dpyinfo,
10378 build_string ("privateColormap"),
10379 build_string ("PrivateColormap"),
10380 Qnil, Qnil);
10381 if (STRINGP (value)
d5db4077
KR
10382 && (!strcmp (SDATA (value), "true")
10383 || !strcmp (SDATA (value), "on")))
5ff67d81
GM
10384 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10385 }
43bd1b2b 10386 }
5ff67d81
GM
10387 else
10388 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10389 dpyinfo->visual, AllocNone);
7d0393cf 10390
06a2c219
GM
10391 {
10392 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10393 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10394 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
2c51754c
SM
10395 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10396 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
06a2c219 10397 pixels = DisplayWidth (dpyinfo->display, screen_number);
2c51754c 10398 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
06a2c219 10399 mm = DisplayWidthMM (dpyinfo->display, screen_number);
2c51754c 10400 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
06a2c219 10401 }
7d0393cf 10402
334208b7
RS
10403 dpyinfo->Xatom_wm_protocols
10404 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
10405 dpyinfo->Xatom_wm_take_focus
10406 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
10407 dpyinfo->Xatom_wm_save_yourself
10408 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
10409 dpyinfo->Xatom_wm_delete_window
10410 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
10411 dpyinfo->Xatom_wm_change_state
10412 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
10413 dpyinfo->Xatom_wm_configure_denied
10414 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
10415 dpyinfo->Xatom_wm_window_moved
10416 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
231d6cfb
JD
10417 dpyinfo->Xatom_wm_client_leader
10418 = XInternAtom (dpyinfo->display, "WM_CLIENT_LEADER", False);
334208b7
RS
10419 dpyinfo->Xatom_editres
10420 = XInternAtom (dpyinfo->display, "Editres", False);
10421 dpyinfo->Xatom_CLIPBOARD
10422 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
10423 dpyinfo->Xatom_TIMESTAMP
10424 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
10425 dpyinfo->Xatom_TEXT
10426 = XInternAtom (dpyinfo->display, "TEXT", False);
dc43ef94
KH
10427 dpyinfo->Xatom_COMPOUND_TEXT
10428 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
c62525b7
KH
10429 dpyinfo->Xatom_UTF8_STRING
10430 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
334208b7
RS
10431 dpyinfo->Xatom_DELETE
10432 = XInternAtom (dpyinfo->display, "DELETE", False);
10433 dpyinfo->Xatom_MULTIPLE
10434 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
10435 dpyinfo->Xatom_INCR
10436 = XInternAtom (dpyinfo->display, "INCR", False);
10437 dpyinfo->Xatom_EMACS_TMP
10438 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
10439 dpyinfo->Xatom_TARGETS
10440 = XInternAtom (dpyinfo->display, "TARGETS", False);
10441 dpyinfo->Xatom_NULL
10442 = XInternAtom (dpyinfo->display, "NULL", False);
10443 dpyinfo->Xatom_ATOM_PAIR
10444 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
dc43ef94
KH
10445 /* For properties of font. */
10446 dpyinfo->Xatom_PIXEL_SIZE
10447 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
6875d1ae
KH
10448 dpyinfo->Xatom_AVERAGE_WIDTH
10449 = XInternAtom (dpyinfo->display, "AVERAGE_WIDTH", False);
dc43ef94
KH
10450 dpyinfo->Xatom_MULE_BASELINE_OFFSET
10451 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
10452 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
10453 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
f78798df
KH
10454 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
10455 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
334208b7 10456
06a2c219
GM
10457 /* Ghostscript support. */
10458 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
10459 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
7d0393cf 10460
06a2c219
GM
10461 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
10462 False);
10463
547d9db8
KH
10464 dpyinfo->cut_buffers_initialized = 0;
10465
334208b7
RS
10466 connection = ConnectionNumber (dpyinfo->display);
10467 dpyinfo->connection = connection;
10468
dc43ef94 10469 {
5d7cc324
RS
10470 char null_bits[1];
10471
10472 null_bits[0] = 0x00;
dc43ef94
KH
10473
10474 dpyinfo->null_pixel
7d0393cf 10475 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
dc43ef94
KH
10476 null_bits, 1, 1, (long) 0, (long) 0,
10477 1);
10478 }
10479
06a2c219
GM
10480 {
10481 extern int gray_bitmap_width, gray_bitmap_height;
10659c77 10482 extern char *gray_bitmap_bits;
06a2c219
GM
10483 dpyinfo->gray
10484 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10485 gray_bitmap_bits,
10486 gray_bitmap_width, gray_bitmap_height,
10487 (unsigned long) 1, (unsigned long) 0, 1);
10488 }
10489
f5d11644
GM
10490#ifdef HAVE_X_I18N
10491 xim_initialize (dpyinfo, resource_name);
10492#endif
7d0393cf 10493
87485d6f
MW
10494#ifdef subprocesses
10495 /* This is only needed for distinguishing keyboard and process input. */
334208b7 10496 if (connection != 0)
7a13e894 10497 add_keyboard_wait_descriptor (connection);
87485d6f 10498#endif
6d4238f3 10499
041b69ac 10500#ifndef F_SETOWN_BUG
dc6f92b8 10501#ifdef F_SETOWN
dc6f92b8 10502#ifdef F_SETOWN_SOCK_NEG
61c3ce62 10503 /* stdin is a socket here */
334208b7 10504 fcntl (connection, F_SETOWN, -getpid ());
c118dd06 10505#else /* ! defined (F_SETOWN_SOCK_NEG) */
334208b7 10506 fcntl (connection, F_SETOWN, getpid ());
c118dd06
JB
10507#endif /* ! defined (F_SETOWN_SOCK_NEG) */
10508#endif /* ! defined (F_SETOWN) */
041b69ac 10509#endif /* F_SETOWN_BUG */
dc6f92b8
JB
10510
10511#ifdef SIGIO
eee20f6a
KH
10512 if (interrupt_input)
10513 init_sigio (connection);
c118dd06 10514#endif /* ! defined (SIGIO) */
dc6f92b8 10515
51b592fb 10516#ifdef USE_LUCID
f8c39f51 10517#ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
51b592fb
RS
10518 /* Make sure that we have a valid font for dialog boxes
10519 so that Xt does not crash. */
10520 {
10521 Display *dpy = dpyinfo->display;
10522 XrmValue d, fr, to;
10523 Font font;
e99db5a1 10524 int count;
7d0393cf 10525
51b592fb
RS
10526 d.addr = (XPointer)&dpy;
10527 d.size = sizeof (Display *);
10528 fr.addr = XtDefaultFont;
10529 fr.size = sizeof (XtDefaultFont);
10530 to.size = sizeof (Font *);
10531 to.addr = (XPointer)&font;
e99db5a1 10532 count = x_catch_errors (dpy);
51b592fb
RS
10533 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
10534 abort ();
10535 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
10536 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
e99db5a1 10537 x_uncatch_errors (dpy, count);
51b592fb
RS
10538 }
10539#endif
f8c39f51 10540#endif
51b592fb 10541
34e23e5a
GM
10542 /* See if we should run in synchronous mode. This is useful
10543 for debugging X code. */
10544 {
10545 Lisp_Object value;
10546 value = display_x_get_resource (dpyinfo,
10547 build_string ("synchronous"),
10548 build_string ("Synchronous"),
10549 Qnil, Qnil);
10550 if (STRINGP (value)
d5db4077
KR
10551 && (!strcmp (SDATA (value), "true")
10552 || !strcmp (SDATA (value), "on")))
34e23e5a
GM
10553 XSynchronize (dpyinfo->display, True);
10554 }
62fe13a4 10555
51f3cc3b
DL
10556 {
10557 Lisp_Object value;
10558 value = display_x_get_resource (dpyinfo,
10559 build_string ("useXIM"),
10560 build_string ("UseXIM"),
10561 Qnil, Qnil);
539465b3 10562#ifdef USE_XIM
51f3cc3b
DL
10563 if (STRINGP (value)
10564 && (!strcmp (XSTRING (value)->data, "false")
10565 || !strcmp (XSTRING (value)->data, "off")))
10566 use_xim = 0;
539465b3
KS
10567#else
10568 if (STRINGP (value)
10569 && (!strcmp (XSTRING (value)->data, "true")
10570 || !strcmp (XSTRING (value)->data, "on")))
10571 use_xim = 1;
10572#endif
51f3cc3b 10573 }
7d0393cf 10574
231d6cfb
JD
10575#ifdef HAVE_X_SM
10576 /* Only do this for the first display. */
10577 if (x_initialized == 1)
10578 x_session_initialize (dpyinfo);
10579#endif
10580
60439948
KH
10581 UNBLOCK_INPUT;
10582
7a13e894
RS
10583 return dpyinfo;
10584}
10585\f
10586/* Get rid of display DPYINFO, assuming all frames are already gone,
10587 and without sending any more commands to the X server. */
dc6f92b8 10588
7a13e894
RS
10589void
10590x_delete_display (dpyinfo)
10591 struct x_display_info *dpyinfo;
10592{
96f09305
JD
10593 int i;
10594
7a13e894
RS
10595 delete_keyboard_wait_descriptor (dpyinfo->connection);
10596
10597 /* Discard this display from x_display_name_list and x_display_list.
10598 We can't use Fdelq because that can quit. */
10599 if (! NILP (x_display_name_list)
8e713be6
KR
10600 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
10601 x_display_name_list = XCDR (x_display_name_list);
7a13e894
RS
10602 else
10603 {
10604 Lisp_Object tail;
10605
10606 tail = x_display_name_list;
8e713be6 10607 while (CONSP (tail) && CONSP (XCDR (tail)))
7a13e894 10608 {
bffcfca9 10609 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
7a13e894 10610 {
f3fbd155 10611 XSETCDR (tail, XCDR (XCDR (tail)));
7a13e894
RS
10612 break;
10613 }
8e713be6 10614 tail = XCDR (tail);
7a13e894
RS
10615 }
10616 }
10617
9bda743f
GM
10618 if (next_noop_dpyinfo == dpyinfo)
10619 next_noop_dpyinfo = dpyinfo->next;
10620
7a13e894
RS
10621 if (x_display_list == dpyinfo)
10622 x_display_list = dpyinfo->next;
7f9c7f94
RS
10623 else
10624 {
10625 struct x_display_info *tail;
7a13e894 10626
7f9c7f94
RS
10627 for (tail = x_display_list; tail; tail = tail->next)
10628 if (tail->next == dpyinfo)
10629 tail->next = tail->next->next;
10630 }
7a13e894 10631
0d777288
RS
10632#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
10633#ifndef AIX /* On AIX, XCloseDisplay calls this. */
7f9c7f94
RS
10634 XrmDestroyDatabase (dpyinfo->xrdb);
10635#endif
0d777288 10636#endif
29b38361
KH
10637#ifdef MULTI_KBOARD
10638 if (--dpyinfo->kboard->reference_count == 0)
39f79001 10639 delete_kboard (dpyinfo->kboard);
b9737ad3 10640#endif
f5d11644
GM
10641#ifdef HAVE_X_I18N
10642 if (dpyinfo->xim)
10643 xim_close_dpy (dpyinfo);
10644#endif
7d0393cf 10645
96f09305
JD
10646 /* Free the font names in the font table. */
10647 for (i = 0; i < dpyinfo->n_fonts; i++)
10648 if (dpyinfo->font_table[i].name)
10649 {
10650 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
10651 xfree (dpyinfo->font_table[i].full_name);
10652 xfree (dpyinfo->font_table[i].name);
10653 }
10654
10655 if (dpyinfo->font_table->font_encoder)
10656 xfree (dpyinfo->font_table->font_encoder);
10657
b9737ad3
KH
10658 xfree (dpyinfo->font_table);
10659 xfree (dpyinfo->x_id_name);
f04e1297 10660 xfree (dpyinfo->color_cells);
b9737ad3 10661 xfree (dpyinfo);
7a13e894 10662}
f04e1297 10663
442a09ea
KS
10664#ifdef USE_X_TOOLKIT
10665
10666/* Atimer callback function for TIMER. Called every 0.1s to process
10667 Xt timeouts, if needed. We must avoid calling XtAppPending as
10668 much as possible because that function does an implicit XFlush
10669 that slows us down. */
10670
10671static void
10672x_process_timeouts (timer)
10673 struct atimer *timer;
10674{
10675 if (toolkit_scroll_bar_interaction || popup_activated ())
10676 {
10677 BLOCK_INPUT;
10678 while (XtAppPending (Xt_app_con) & XtIMTimer)
10679 XtAppProcessEvent (Xt_app_con, XtIMTimer);
10680 UNBLOCK_INPUT;
10681 }
10682}
10683
10684#endif /* USE_X_TOOLKIT */
10685
7a13e894
RS
10686\f
10687/* Set up use of X before we make the first connection. */
10688
f8240bf8
KS
10689extern frame_parm_handler x_frame_parm_handlers[];
10690
06a2c219
GM
10691static struct redisplay_interface x_redisplay_interface =
10692{
f8240bf8 10693 x_frame_parm_handlers,
06a2c219
GM
10694 x_produce_glyphs,
10695 x_write_glyphs,
10696 x_insert_glyphs,
10697 x_clear_end_of_line,
10698 x_scroll_run,
10699 x_after_update_window_line,
10700 x_update_window_begin,
10701 x_update_window_end,
442a09ea
KS
10702 x_cursor_to,
10703 x_flush,
61087d3f 10704#ifdef XFlush
06a2c219 10705 x_flush,
442a09ea
KS
10706#else
10707 0, /* flush_display_optional */
10708#endif
10709 x_clear_window_mouse_face,
66ac4b0e 10710 x_get_glyph_overhangs,
5958f265 10711 x_fix_overlapping_area,
5da0698e 10712 x_draw_fringe_bitmap,
fe1a14c1
KS
10713 0, /* define_fringe_bitmap */
10714 0, /* destroy_fringe_bitmap */
5da0698e
KS
10715 x_per_char_metric,
10716 x_encode_char,
10717 x_compute_glyph_string_overhangs,
442a09ea
KS
10718 x_draw_glyph_string,
10719 x_define_frame_cursor,
10720 x_clear_frame_area,
10721 x_draw_window_cursor,
10722 x_draw_vertical_window_border,
10723 x_shift_glyphs_for_insert
06a2c219
GM
10724};
10725
dfcf069d 10726void
7a13e894
RS
10727x_initialize ()
10728{
06a2c219
GM
10729 rif = &x_redisplay_interface;
10730
10731 clear_frame_hook = x_clear_frame;
10732 ins_del_lines_hook = x_ins_del_lines;
06a2c219 10733 delete_glyphs_hook = x_delete_glyphs;
dc6f92b8
JB
10734 ring_bell_hook = XTring_bell;
10735 reset_terminal_modes_hook = XTreset_terminal_modes;
10736 set_terminal_modes_hook = XTset_terminal_modes;
06a2c219
GM
10737 update_begin_hook = x_update_begin;
10738 update_end_hook = x_update_end;
dc6f92b8
JB
10739 set_terminal_window_hook = XTset_terminal_window;
10740 read_socket_hook = XTread_socket;
b8009dd1 10741 frame_up_to_date_hook = XTframe_up_to_date;
90e65f07 10742 mouse_position_hook = XTmouse_position;
f451eb13 10743 frame_rehighlight_hook = XTframe_rehighlight;
dbc4e1c1 10744 frame_raise_lower_hook = XTframe_raise_lower;
ab648270
JB
10745 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
10746 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
10747 redeem_scroll_bar_hook = XTredeem_scroll_bar;
10748 judge_scroll_bars_hook = XTjudge_scroll_bars;
58769bee 10749
f676886a 10750 scroll_region_ok = 1; /* we'll scroll partial frames */
9017309f 10751 char_ins_del_ok = 1;
dc6f92b8
JB
10752 line_ins_del_ok = 1; /* we'll just blt 'em */
10753 fast_clear_end_of_line = 1; /* X does this well */
58769bee 10754 memory_below_frame = 0; /* we don't remember what scrolls
dc6f92b8
JB
10755 off the bottom */
10756 baud_rate = 19200;
10757
7a13e894 10758 x_noop_count = 0;
9ea173e8 10759 last_tool_bar_item = -1;
06a2c219 10760 any_help_event_p = 0;
ccf1626a 10761 ignore_next_mouse_click_timeout = 0;
7d0393cf 10762
89079179
KS
10763#ifdef USE_GTK
10764 current_count = -1;
10765#endif
10766
b30b24cb
RS
10767 /* Try to use interrupt input; if we can't, then start polling. */
10768 Fset_input_mode (Qt, Qnil, Qt, Qnil);
10769
7f9c7f94
RS
10770#ifdef USE_X_TOOLKIT
10771 XtToolkitInitialize ();
651f03b6 10772
7f9c7f94 10773 Xt_app_con = XtCreateApplicationContext ();
7d0393cf 10774
651f03b6
GM
10775 /* Register a converter from strings to pixels, which uses
10776 Emacs' color allocation infrastructure. */
10777 XtAppSetTypeConverter (Xt_app_con,
10778 XtRString, XtRPixel, cvt_string_to_pixel,
10779 cvt_string_to_pixel_args,
10780 XtNumber (cvt_string_to_pixel_args),
10781 XtCacheByDisplay, cvt_pixel_dtor);
10782
665881ad 10783 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
bffcfca9
GM
10784
10785 /* Install an asynchronous timer that processes Xt timeout events
10786 every 0.1s. This is necessary because some widget sets use
10787 timeouts internally, for example the LessTif menu bar, or the
10788 Xaw3d scroll bar. When Xt timouts aren't processed, these
10789 widgets don't behave normally. */
10790 {
10791 EMACS_TIME interval;
10792 EMACS_SET_SECS_USECS (interval, 0, 100000);
10793 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
10794 }
db74249b 10795#endif
7d0393cf 10796
eccc05db 10797#ifdef USE_TOOLKIT_SCROLL_BARS
488dd4c4 10798#ifndef USE_GTK
ec18280f
SM
10799 xaw3d_arrow_scroll = False;
10800 xaw3d_pick_top = True;
488dd4c4 10801#endif
7f9c7f94
RS
10802#endif
10803
58769bee 10804 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 10805 original error handler. */
e99db5a1 10806 XSetErrorHandler (x_error_handler);
334208b7 10807 XSetIOErrorHandler (x_io_error_quitter);
dc6f92b8 10808
06a2c219 10809 /* Disable Window Change signals; they are handled by X events. */
dc6f92b8
JB
10810#ifdef SIGWINCH
10811 signal (SIGWINCH, SIG_DFL);
a59facca 10812#endif /* SIGWINCH */
dc6f92b8 10813
92e2441b 10814 signal (SIGPIPE, x_connection_signal);
dc6f92b8 10815}
55123275 10816
06a2c219 10817
55123275
JB
10818void
10819syms_of_xterm ()
10820{
e99db5a1
RS
10821 staticpro (&x_error_message_string);
10822 x_error_message_string = Qnil;
10823
7a13e894
RS
10824 staticpro (&x_display_name_list);
10825 x_display_name_list = Qnil;
334208b7 10826
ab648270 10827 staticpro (&last_mouse_scroll_bar);
e53cb100 10828 last_mouse_scroll_bar = Qnil;
59e755be
KH
10829
10830 staticpro (&Qvendor_specific_keysyms);
10831 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
2237cac9 10832
c997eae5
SM
10833 staticpro (&Qutf_8);
10834 Qutf_8 = intern ("utf-8");
10835 staticpro (&Qlatin_1);
10836 Qlatin_1 = intern ("latin-1");
10837
2237cac9
RS
10838 staticpro (&last_mouse_press_frame);
10839 last_mouse_press_frame = Qnil;
06a2c219 10840
a72d5ce5 10841 DEFVAR_BOOL ("x-use-underline-position-properties",
7ee72033
MB
10842 &x_use_underline_position_properties,
10843 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
f0529b5b 10844nil means ignore them. If you encounter fonts with bogus
228299fa
GM
10845UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
10846to 4.1, set this to nil. */);
a72d5ce5
GM
10847 x_use_underline_position_properties = 1;
10848
0666d82c
KS
10849 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
10850 &x_mouse_click_focus_ignore_position,
10851 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
10852This variable is only used when the window manager requires that you
10853click on a frame to select it (give it focus). In that case, a value
10854of nil, means that the selected window and cursor position changes to
10855reflect the mouse click position, while a non-nil value means that the
10856selected window or cursor position is preserved. */);
10857 x_mouse_click_focus_ignore_position = 0;
10858
7ee72033
MB
10859 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
10860 doc: /* What X toolkit scroll bars Emacs uses.
228299fa
GM
10861A value of nil means Emacs doesn't use X toolkit scroll bars.
10862Otherwise, value is a symbol describing the X toolkit. */);
eccc05db 10863#ifdef USE_TOOLKIT_SCROLL_BARS
5bf04520
GM
10864#ifdef USE_MOTIF
10865 Vx_toolkit_scroll_bars = intern ("motif");
10866#elif defined HAVE_XAW3D
10867 Vx_toolkit_scroll_bars = intern ("xaw3d");
488dd4c4
JD
10868#elif USE_GTK
10869 Vx_toolkit_scroll_bars = intern ("gtk");
5bf04520
GM
10870#else
10871 Vx_toolkit_scroll_bars = intern ("xaw");
10872#endif
06a2c219 10873#else
5bf04520 10874 Vx_toolkit_scroll_bars = Qnil;
06a2c219
GM
10875#endif
10876
06a2c219
GM
10877 staticpro (&last_mouse_motion_frame);
10878 last_mouse_motion_frame = Qnil;
7d0393cf 10879
98659da6
KG
10880 Qmodifier_value = intern ("modifier-value");
10881 Qalt = intern ("alt");
10882 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
10883 Qhyper = intern ("hyper");
10884 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
10885 Qmeta = intern ("meta");
10886 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
10887 Qsuper = intern ("super");
10888 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
7d0393cf 10889
98659da6
KG
10890 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
10891 doc: /* Which keys Emacs uses for the alt modifier.
10892This should be one of the symbols `alt', `hyper', `meta', `super'.
10893For example, `alt' means use the Alt_L and Alt_R keysyms. The default
10894is nil, which is the same as `alt'. */);
10895 Vx_alt_keysym = Qnil;
7d0393cf 10896
98659da6
KG
10897 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
10898 doc: /* Which keys Emacs uses for the hyper modifier.
10899This should be one of the symbols `alt', `hyper', `meta', `super'.
10900For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
10901default is nil, which is the same as `hyper'. */);
10902 Vx_hyper_keysym = Qnil;
7d0393cf 10903
98659da6
KG
10904 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
10905 doc: /* Which keys Emacs uses for the meta modifier.
10906This should be one of the symbols `alt', `hyper', `meta', `super'.
10907For example, `meta' means use the Meta_L and Meta_R keysyms. The
10908default is nil, which is the same as `meta'. */);
10909 Vx_meta_keysym = Qnil;
7d0393cf 10910
98659da6
KG
10911 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
10912 doc: /* Which keys Emacs uses for the super modifier.
10913This should be one of the symbols `alt', `hyper', `meta', `super'.
10914For example, `super' means use the Super_L and Super_R keysyms. The
10915default is nil, which is the same as `super'. */);
10916 Vx_super_keysym = Qnil;
10917
dbd4b028
DL
10918 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
10919 doc: /* Hash table of character codes indexed by X keysym codes. */);
10920 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
10921 make_float (DEFAULT_REHASH_SIZE),
10922 make_float (DEFAULT_REHASH_THRESHOLD),
10923 Qnil, Qnil, Qnil);
55123275 10924}
6cf0ae86 10925
1d6c120a 10926#endif /* HAVE_X_WINDOWS */
ab5796a9
MB
10927
10928/* arch-tag: 6d4e4cb7-abc1-4302-9585-d84dcfb09d0f
10929 (do not change this comment) */