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