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