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