(scan_lists): Follow coding convention.
[bpt/emacs.git] / src / xterm.c
CommitLineData
dc6f92b8 1/* X Communication module for terminals which understand the X protocol.
043f7f73 2 Copyright (C) 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
8cabe764
GM
3 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
dc6f92b8
JB
5
6This file is part of GNU Emacs.
7
8GNU Emacs is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
684d6f5b 10the Free Software Foundation; either version 3, or (at your option)
dc6f92b8
JB
11any later version.
12
13GNU Emacs is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU Emacs; see the file COPYING. If not, write to
4fc5845f
LK
20the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21Boston, MA 02110-1301, USA. */
dc6f92b8 22
06a2c219 23/* New display code by Gerd Moellmann <gerd@gnu.org>. */
3afe33e7
RS
24/* Xt features made by Fred Pierresteguy. */
25
68c45bf0
PE
26#include <config.h>
27
039440c4 28/* On 4.3 these lose if they come after xterm.h. */
039440c4 29/* Putting these at the beginning seems to be standard for other .c files. */
039440c4
RS
30#include <signal.h>
31
4846819e
RS
32#include <stdio.h>
33
dc6f92b8
JB
34#ifdef HAVE_X_WINDOWS
35
36#include "lisp.h"
9ac0d9e0 37#include "blockinput.h"
dc6f92b8 38
ae79c227
AS
39/* Need syssignal.h for various externs and definitions that may be required
40 by some configurations for calls to signal later in this source file. */
41#include "syssignal.h"
42
dc6f92b8
JB
43/* This may include sys/types.h, and that somehow loses
44 if this is not done before the other system files. */
45#include "xterm.h"
f451eb13 46#include <X11/cursorfont.h>
dc6f92b8
JB
47
48/* Load sys/types.h if not already loaded.
49 In some systems loading it twice is suicidal. */
50#ifndef makedev
51#include <sys/types.h>
c118dd06 52#endif /* makedev */
dc6f92b8 53
6df54671 54#ifdef BSD_SYSTEM
dc6f92b8 55#include <sys/ioctl.h>
6df54671 56#endif /* ! defined (BSD_SYSTEM) */
dc6f92b8 57
3a2712f9 58#include "systime.h"
dc6f92b8 59
b8009dd1 60#ifndef INCLUDED_FCNTL
dc6f92b8 61#include <fcntl.h>
b8009dd1 62#endif
dc6f92b8
JB
63#include <ctype.h>
64#include <errno.h>
65#include <setjmp.h>
66#include <sys/stat.h>
a0a7635f
RS
67/* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
68/* #include <sys/param.h> */
dc6f92b8 69
dc43ef94 70#include "charset.h"
8a78c965 71#include "character.h"
379b5ac0 72#include "coding.h"
dc43ef94 73#include "ccl.h"
7a13e894 74#include "frame.h"
dc6f92b8 75#include "dispextern.h"
ee569018 76#include "fontset.h"
dc6f92b8
JB
77#include "termhooks.h"
78#include "termopts.h"
79#include "termchar.h"
dc6f92b8 80#include "gnu.h"
dc6f92b8 81#include "disptab.h"
dc6f92b8 82#include "buffer.h"
f451eb13 83#include "window.h"
3b2fa4e6 84#include "keyboard.h"
bde7c500 85#include "intervals.h"
dfcf069d 86#include "process.h"
bffcfca9 87#include "atimer.h"
8feddab4 88#include "keymap.h"
dc6f92b8 89
d2bd6bc4
RS
90#ifdef USE_X_TOOLKIT
91#include <X11/Shell.h>
92#endif
93
06a2c219
GM
94#ifdef HAVE_SYS_TIME_H
95#include <sys/time.h>
96#endif
97#ifdef HAVE_UNISTD_H
98#include <unistd.h>
99#endif
100
488dd4c4
JD
101#ifdef USE_GTK
102#include "gtkutil.h"
103#endif
104
c9ff09ee
KH
105#ifdef USE_FONT_BACKEND
106#include "font.h"
107#endif /* USE_FONT_BACKEND */
108
d624284c 109#ifdef USE_LUCID
55a8b3ed 110extern int xlwmenu_window_p P_ ((Widget w, Window window));
d624284c
PJ
111extern void xlwmenu_redisplay P_ ((Widget));
112#endif
113
488dd4c4 114#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
06a2c219 115
952291d9
GM
116extern void free_frame_menubar P_ ((struct frame *));
117extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
118 int));
488dd4c4 119#endif
06a2c219 120
488dd4c4 121#ifdef USE_X_TOOLKIT
0fdff6bb
RS
122#if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
123#define HACK_EDITRES
124extern void _XEditResCheckMessages ();
125#endif /* not NO_EDITRES */
06a2c219
GM
126
127/* Include toolkit specific headers for the scroll bar widget. */
128
129#ifdef USE_TOOLKIT_SCROLL_BARS
130#if defined USE_MOTIF
131#include <Xm/Xm.h> /* for LESSTIF_VERSION */
132#include <Xm/ScrollBar.h>
ec18280f
SM
133#else /* !USE_MOTIF i.e. use Xaw */
134
135#ifdef HAVE_XAW3D
06a2c219 136#include <X11/Xaw3d/Simple.h>
06a2c219 137#include <X11/Xaw3d/Scrollbar.h>
6ca53601 138#include <X11/Xaw3d/ThreeD.h>
ec18280f
SM
139#else /* !HAVE_XAW3D */
140#include <X11/Xaw/Simple.h>
141#include <X11/Xaw/Scrollbar.h>
142#endif /* !HAVE_XAW3D */
143#ifndef XtNpickTop
144#define XtNpickTop "pickTop"
145#endif /* !XtNpickTop */
146#endif /* !USE_MOTIF */
06a2c219
GM
147#endif /* USE_TOOLKIT_SCROLL_BARS */
148
3afe33e7
RS
149#endif /* USE_X_TOOLKIT */
150
488dd4c4 151#if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
b849c413 152#define x_any_window_to_frame x_window_to_frame
5627c40e 153#define x_top_window_to_frame x_window_to_frame
b849c413
RS
154#endif
155
546e6d5b 156#ifdef USE_X_TOOLKIT
d067ea8b 157#include "widget.h"
546e6d5b
RS
158#ifndef XtNinitialState
159#define XtNinitialState "initialState"
160#endif
161#endif
162
51f3cc3b 163/* Default to using XIM if available. */
3ff483be 164#ifdef USE_XIM
51f3cc3b 165int use_xim = 1;
3ff483be
KS
166#else
167int use_xim = 0; /* configure --without-xim */
168#endif
169
06a2c219 170\f
06a2c219 171
5bf04520 172/* Non-nil means Emacs uses toolkit scroll bars. */
06a2c219 173
5bf04520 174Lisp_Object Vx_toolkit_scroll_bars;
06a2c219 175
06a2c219
GM
176/* Non-zero means that a HELP_EVENT has been generated since Emacs
177 start. */
178
179static int any_help_event_p;
180
ee8ceff8 181/* Last window where we saw the mouse. Used by mouse-autoselect-window. */
5bc62483 182static Lisp_Object last_window;
503e457e 183
a72d5ce5
GM
184/* Non-zero means make use of UNDERLINE_POSITION font properties. */
185
186int x_use_underline_position_properties;
187
30f27523
KS
188/* Non-zero means to draw the underline at the same place as the descent line. */
189
190int x_underline_at_descent_line;
191
06a2c219
GM
192/* This is a chain of structures for all the X displays currently in
193 use. */
194
334208b7 195struct x_display_info *x_display_list;
dc6f92b8 196
06a2c219
GM
197/* This is a list of cons cells, each of the form (NAME
198 . FONT-LIST-CACHE), one for each element of x_display_list and in
199 the same order. NAME is the name of the frame. FONT-LIST-CACHE
200 records previous values returned by x-list-fonts. */
201
7a13e894 202Lisp_Object x_display_name_list;
f451eb13 203
987d2ad1 204/* Frame being updated by update_frame. This is declared in term.c.
06a2c219
GM
205 This is set by update_begin and looked at by all the XT functions.
206 It is zero while not inside an update. In that case, the XT
207 functions assume that `selected_frame' is the frame to apply to. */
208
d0386f2a 209extern struct frame *updating_frame;
dc6f92b8 210
06a2c219
GM
211/* This is a frame waiting to be auto-raised, within XTread_socket. */
212
0134a210
RS
213struct frame *pending_autoraise_frame;
214
7f9c7f94
RS
215#ifdef USE_X_TOOLKIT
216/* The application context for Xt use. */
217XtAppContext Xt_app_con;
06a2c219
GM
218static String Xt_default_resources[] = {0};
219#endif /* USE_X_TOOLKIT */
665881ad 220
bffcfca9
GM
221/* Non-zero means user is interacting with a toolkit scroll bar. */
222
223static int toolkit_scroll_bar_interaction;
dc6f92b8 224
6b61353c
KH
225/* Non-zero means to not move point as a result of clicking on a
226 frame to focus it (when focus-follows-mouse is nil). */
227
228int x_mouse_click_focus_ignore_position;
229
230/* Non-zero timeout value means ignore next mouse click if it arrives
231 before that timeout elapses (i.e. as part of the same sequence of
232 events resulting from clicking on a frame to select it). */
233
234static unsigned long ignore_next_mouse_click_timeout;
235
69388238
RS
236/* Mouse movement.
237
06a2c219 238 Formerly, we used PointerMotionHintMask (in standard_event_mask)
f5bb65ec
RS
239 so that we would have to call XQueryPointer after each MotionNotify
240 event to ask for another such event. However, this made mouse tracking
241 slow, and there was a bug that made it eventually stop.
242
243 Simply asking for MotionNotify all the time seems to work better.
244
69388238
RS
245 In order to avoid asking for motion events and then throwing most
246 of them away or busy-polling the server for mouse positions, we ask
247 the server for pointer motion hints. This means that we get only
248 one event per group of mouse movements. "Groups" are delimited by
249 other kinds of events (focus changes and button clicks, for
250 example), or by XQueryPointer calls; when one of these happens, we
251 get another MotionNotify event the next time the mouse moves. This
252 is at least as efficient as getting motion events when mouse
253 tracking is on, and I suspect only negligibly worse when tracking
f5bb65ec 254 is off. */
69388238
RS
255
256/* Where the mouse was last time we reported a mouse event. */
69388238 257
06a2c219 258static XRectangle last_mouse_glyph;
0a3fde9d 259static FRAME_PTR last_mouse_glyph_frame;
2237cac9
RS
260static Lisp_Object last_mouse_press_frame;
261
69388238
RS
262/* The scroll bar in which the last X motion event occurred.
263
06a2c219
GM
264 If the last X motion event occurred in a scroll bar, we set this so
265 XTmouse_position can know whether to report a scroll bar motion or
69388238
RS
266 an ordinary motion.
267
06a2c219
GM
268 If the last X motion event didn't occur in a scroll bar, we set
269 this to Qnil, to tell XTmouse_position to return an ordinary motion
270 event. */
271
69388238
RS
272static Lisp_Object last_mouse_scroll_bar;
273
69388238
RS
274/* This is a hack. We would really prefer that XTmouse_position would
275 return the time associated with the position it returns, but there
06a2c219 276 doesn't seem to be any way to wrest the time-stamp from the server
69388238
RS
277 along with the position query. So, we just keep track of the time
278 of the last movement we received, and return that in hopes that
279 it's somewhat accurate. */
06a2c219 280
69388238
RS
281static Time last_mouse_movement_time;
282
1c8591d0
JD
283/* Time for last user interaction as returned in X events. */
284
285static Time last_user_time;
286
06a2c219
GM
287/* Incremented by XTread_socket whenever it really tries to read
288 events. */
289
c0a04927
RS
290#ifdef __STDC__
291static int volatile input_signal_count;
292#else
293static int input_signal_count;
294#endif
295
7a13e894 296/* Used locally within XTread_socket. */
06a2c219 297
7a13e894 298static int x_noop_count;
dc6f92b8 299
7a13e894 300/* Initial values of argv and argc. */
06a2c219 301
7a13e894
RS
302extern char **initial_argv;
303extern int initial_argc;
dc6f92b8 304
7a13e894 305extern Lisp_Object Vcommand_line_args, Vsystem_name;
dc6f92b8 306
06a2c219 307/* Tells if a window manager is present or not. */
7a13e894
RS
308
309extern Lisp_Object Vx_no_window_manager;
dc6f92b8 310
6b61353c 311extern Lisp_Object Qeql;
b8009dd1 312
dc6f92b8
JB
313extern int errno;
314
dfeccd2d 315/* A mask of extra modifier bits to put into every keyboard char. */
06a2c219 316
31ade731 317extern EMACS_INT extra_keyboard_modifiers;
64bb1782 318
98659da6
KG
319/* The keysyms to use for the various modifiers. */
320
321Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
dbd4b028 322Lisp_Object Vx_keysym_table;
98659da6
KG
323static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
324
59e755be 325static Lisp_Object Qvendor_specific_keysyms;
3db9e31e 326static Lisp_Object Qlatin_1;
59e755be 327
83dcc67c
KL
328/* Used in x_flush. */
329
330extern Lisp_Object Vinhibit_redisplay;
331
952291d9 332extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
3c5034e9 333extern int x_bitmap_mask P_ ((FRAME_PTR, int));
7a13e894 334
651f03b6 335static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
499b1844 336static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
651f03b6 337static const XColor *x_color_cells P_ ((Display *, int *));
71b8321e 338static void x_update_window_end P_ ((struct window *, int, int));
e92928a7 339
06a2c219 340static int x_io_error_quitter P_ ((Display *));
6ed8eeff
KL
341static struct terminal *x_create_terminal P_ ((struct x_display_info *));
342void x_delete_terminal P_ ((struct terminal *));
06a2c219
GM
343static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
344static int x_compute_min_glyph_bounds P_ ((struct frame *));
06a2c219
GM
345static void x_update_end P_ ((struct frame *));
346static void XTframe_up_to_date P_ ((struct frame *));
6ed8eeff
KL
347static void XTset_terminal_modes P_ ((struct terminal *));
348static void XTreset_terminal_modes P_ ((struct terminal *));
385ed61f 349static void x_clear_frame P_ ((struct frame *));
06a2c219
GM
350static void frame_highlight P_ ((struct frame *));
351static void frame_unhighlight P_ ((struct frame *));
352static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
6b61353c
KH
353static void x_focus_changed P_ ((int, int, struct x_display_info *,
354 struct frame *, struct input_event *));
355static void x_detect_focus_change P_ ((struct x_display_info *,
356 XEvent *, struct input_event *));
06a2c219
GM
357static void XTframe_rehighlight P_ ((struct frame *));
358static void x_frame_rehighlight P_ ((struct x_display_info *));
359static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
65c26775
EZ
360static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
361 enum text_cursor_kinds));
06a2c219 362
08f66682 363static void x_clip_to_row P_ ((struct window *, struct glyph_row *, int, GC));
06a2c219 364static void x_flush P_ ((struct frame *f));
952291d9
GM
365static void x_update_begin P_ ((struct frame *));
366static void x_update_window_begin P_ ((struct window *));
952291d9 367static void x_after_update_window_line P_ ((struct glyph_row *));
6b61353c 368static struct scroll_bar *x_window_to_scroll_bar P_ ((Display *, Window));
b52b65bd
GM
369static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
370 enum scroll_bar_part *,
371 Lisp_Object *, Lisp_Object *,
372 unsigned long *));
d0fa3e56 373static void x_check_fullscreen P_ ((struct frame *));
d692a3d5
JD
374static void x_check_expected_move P_ ((struct frame *, int, int));
375static void x_sync_with_move P_ ((struct frame *, int, int, int));
6b61353c
KH
376static int handle_one_xevent P_ ((struct x_display_info *, XEvent *,
377 int *, struct input_event *));
f7fa4950
RS
378/* Don't declare this NO_RETURN because we want no
379 interference with debugging failing X calls. */
380static SIGTYPE x_connection_closed P_ ((Display *, char *));
488dd4c4 381
06a2c219
GM
382
383/* Flush display of frame F, or of all frames if F is null. */
384
385static void
386x_flush (f)
387 struct frame *f;
388{
1a27213c
KL
389 /* Don't call XFlush when it is not safe to redisplay; the X
390 connection may be broken. */
391 if (!NILP (Vinhibit_redisplay))
392 return;
393
06a2c219
GM
394 BLOCK_INPUT;
395 if (f == NULL)
396 {
397 Lisp_Object rest, frame;
398 FOR_EACH_FRAME (rest, frame)
daf01701
KL
399 if (FRAME_X_P (XFRAME (frame)))
400 x_flush (XFRAME (frame));
06a2c219
GM
401 }
402 else if (FRAME_X_P (f))
403 XFlush (FRAME_X_DISPLAY (f));
404 UNBLOCK_INPUT;
405}
406
dc6f92b8 407
06a2c219
GM
408/* Remove calls to XFlush by defining XFlush to an empty replacement.
409 Calls to XFlush should be unnecessary because the X output buffer
410 is flushed automatically as needed by calls to XPending,
411 XNextEvent, or XWindowEvent according to the XFlush man page.
412 XTread_socket calls XPending. Removing XFlush improves
413 performance. */
414
7d0393cf 415#define XFlush(DISPLAY) (void) 0
b8009dd1 416
334208b7 417\f
06a2c219
GM
418/***********************************************************************
419 Debugging
420 ***********************************************************************/
421
9382638d 422#if 0
06a2c219
GM
423
424/* This is a function useful for recording debugging information about
425 the sequence of occurrences in this file. */
9382638d 426
7d0393cf 427struct record
9382638d
KH
428{
429 char *locus;
430 int type;
431};
432
433struct record event_record[100];
434
435int event_record_index;
436
437record_event (locus, type)
438 char *locus;
439 int type;
440{
441 if (event_record_index == sizeof (event_record) / sizeof (struct record))
442 event_record_index = 0;
443
444 event_record[event_record_index].locus = locus;
445 event_record[event_record_index].type = type;
446 event_record_index++;
447}
448
449#endif /* 0 */
06a2c219
GM
450
451
9382638d 452\f
334208b7
RS
453/* Return the struct x_display_info corresponding to DPY. */
454
455struct x_display_info *
456x_display_info_for_display (dpy)
457 Display *dpy;
458{
459 struct x_display_info *dpyinfo;
460
461 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
462 if (dpyinfo->display == dpy)
463 return dpyinfo;
16bd92ea 464
334208b7
RS
465 return 0;
466}
f451eb13 467
06a2c219
GM
468
469\f
470/***********************************************************************
471 Starting and ending an update
472 ***********************************************************************/
7d0393cf 473
06a2c219
GM
474/* Start an update of frame F. This function is installed as a hook
475 for update_begin, i.e. it is called when update_begin is called.
476 This function is called prior to calls to x_update_window_begin for
477 each window being updated. Currently, there is nothing to do here
478 because all interesting stuff is done on a window basis. */
dc6f92b8 479
dfcf069d 480static void
06a2c219 481x_update_begin (f)
f676886a 482 struct frame *f;
58769bee 483{
06a2c219
GM
484 /* Nothing to do. */
485}
dc6f92b8 486
dc6f92b8 487
06a2c219
GM
488/* Start update of window W. Set the global variable updated_window
489 to the window being updated and set output_cursor to the cursor
490 position of W. */
dc6f92b8 491
06a2c219
GM
492static void
493x_update_window_begin (w)
494 struct window *w;
495{
496 struct frame *f = XFRAME (WINDOW_FRAME (w));
497 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
7d0393cf 498
06a2c219
GM
499 updated_window = w;
500 set_output_cursor (&w->cursor);
b8009dd1 501
06a2c219 502 BLOCK_INPUT;
d1bc4182 503
06a2c219 504 if (f == display_info->mouse_face_mouse_frame)
b8009dd1 505 {
514e4681 506 /* Don't do highlighting for mouse motion during the update. */
06a2c219 507 display_info->mouse_face_defer = 1;
37c2c98b 508
06a2c219
GM
509 /* If F needs to be redrawn, simply forget about any prior mouse
510 highlighting. */
9f67f20b 511 if (FRAME_GARBAGED_P (f))
06a2c219
GM
512 display_info->mouse_face_window = Qnil;
513
64f26cf5
GM
514#if 0 /* Rows in a current matrix containing glyphs in mouse-face have
515 their mouse_face_p flag set, which means that they are always
516 unequal to rows in a desired matrix which never have that
517 flag set. So, rows containing mouse-face glyphs are never
518 scrolled, and we don't have to switch the mouse highlight off
519 here to prevent it from being scrolled. */
7d0393cf 520
06a2c219
GM
521 /* Can we tell that this update does not affect the window
522 where the mouse highlight is? If so, no need to turn off.
523 Likewise, don't do anything if the frame is garbaged;
524 in that case, the frame's current matrix that we would use
525 is all wrong, and we will redisplay that line anyway. */
526 if (!NILP (display_info->mouse_face_window)
527 && w == XWINDOW (display_info->mouse_face_window))
514e4681 528 {
06a2c219 529 int i;
514e4681 530
06a2c219
GM
531 for (i = 0; i < w->desired_matrix->nrows; ++i)
532 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
514e4681
RS
533 break;
534
06a2c219
GM
535 if (i < w->desired_matrix->nrows)
536 clear_mouse_face (display_info);
514e4681 537 }
64f26cf5 538#endif /* 0 */
b8009dd1 539 }
6ccf47d1 540
dc6f92b8
JB
541 UNBLOCK_INPUT;
542}
543
06a2c219 544
442a09ea 545/* Draw a vertical window border from (x,y0) to (x,y1) */
06a2c219 546
dfcf069d 547static void
442a09ea 548x_draw_vertical_window_border (w, x, y0, y1)
06a2c219 549 struct window *w;
442a09ea 550 int x, y0, y1;
58769bee 551{
06a2c219 552 struct frame *f = XFRAME (WINDOW_FRAME (w));
7ecc34e9
JL
553 struct face *face;
554
555 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
556 if (face)
557 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
558 face->foreground);
62fe13a4 559
442a09ea
KS
560 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
561 f->output_data.x->normal_gc, x, y0, x, y1);
06a2c219 562}
7d0393cf 563
71b8321e
GM
564/* End update of window W (which is equal to updated_window).
565
566 Draw vertical borders between horizontally adjacent windows, and
567 display W's cursor if CURSOR_ON_P is non-zero.
568
569 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
570 glyphs in mouse-face were overwritten. In that case we have to
571 make sure that the mouse-highlight is properly redrawn.
572
573 W may be a menu bar pseudo-window in case we don't have X toolkit
574 support. Such windows don't have a cursor, so don't display it
575 here. */
06a2c219
GM
576
577static void
71b8321e 578x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
06a2c219 579 struct window *w;
71b8321e 580 int cursor_on_p, mouse_face_overwritten_p;
06a2c219 581{
140330de 582 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7d0393cf 583
06a2c219
GM
584 if (!w->pseudo_window_p)
585 {
586 BLOCK_INPUT;
71b8321e 587
06a2c219 588 if (cursor_on_p)
442a09ea
KS
589 display_and_set_cursor (w, 1, output_cursor.hpos,
590 output_cursor.vpos,
591 output_cursor.x, output_cursor.y);
7d0393cf 592
f94a2622
KS
593 if (draw_window_fringes (w, 1))
594 x_draw_vertical_border (w);
6b61353c 595
06a2c219
GM
596 UNBLOCK_INPUT;
597 }
7d0393cf 598
140330de
GM
599 /* If a row with mouse-face was overwritten, arrange for
600 XTframe_up_to_date to redisplay the mouse highlight. */
601 if (mouse_face_overwritten_p)
602 {
603 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
604 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
605 dpyinfo->mouse_face_window = Qnil;
606 }
7d0393cf 607
06a2c219
GM
608 updated_window = NULL;
609}
dc6f92b8 610
dc6f92b8 611
06a2c219
GM
612/* End update of frame F. This function is installed as a hook in
613 update_end. */
614
615static void
616x_update_end (f)
617 struct frame *f;
618{
619 /* Mouse highlight may be displayed again. */
aa8bff2e 620 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
b8009dd1 621
442a09ea 622#ifndef XFlush
06a2c219 623 BLOCK_INPUT;
334208b7 624 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 625 UNBLOCK_INPUT;
442a09ea 626#endif
dc6f92b8 627}
b8009dd1 628
06a2c219
GM
629
630/* This function is called from various places in xdisp.c whenever a
631 complete update has been performed. The global variable
632 updated_window is not available here. */
b8009dd1 633
dfcf069d 634static void
b8009dd1 635XTframe_up_to_date (f)
06a2c219 636 struct frame *f;
b8009dd1 637{
06a2c219 638 if (FRAME_X_P (f))
514e4681 639 {
06a2c219 640 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
71b8321e 641
06a2c219
GM
642 if (dpyinfo->mouse_face_deferred_gc
643 || f == dpyinfo->mouse_face_mouse_frame)
644 {
645 BLOCK_INPUT;
646 if (dpyinfo->mouse_face_mouse_frame)
647 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
648 dpyinfo->mouse_face_mouse_x,
649 dpyinfo->mouse_face_mouse_y);
650 dpyinfo->mouse_face_deferred_gc = 0;
651 UNBLOCK_INPUT;
652 }
514e4681 653 }
b8009dd1 654}
06a2c219
GM
655
656
657/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
3f332ef3 658 arrow bitmaps, or clear the fringes if no bitmaps are required
06a2c219
GM
659 before DESIRED_ROW is made current. The window being updated is
660 found in updated_window. This function It is called from
661 update_window_line only if it is known that there are differences
662 between bitmaps to be drawn between current row and DESIRED_ROW. */
663
664static void
665x_after_update_window_line (desired_row)
666 struct glyph_row *desired_row;
667{
668 struct window *w = updated_window;
ef253080 669 struct frame *f;
259cf6bc 670 int width, height;
7d0393cf 671
06a2c219 672 xassert (w);
7d0393cf 673
06a2c219 674 if (!desired_row->mode_line_p && !w->pseudo_window_p)
6b61353c 675 desired_row->redraw_fringe_bitmaps_p = 1;
7d0393cf 676
ef253080
GM
677 /* When a window has disappeared, make sure that no rest of
678 full-width rows stays visible in the internal border. Could
679 check here if updated_window is the leftmost/rightmost window,
680 but I guess it's not worth doing since vertically split windows
681 are almost never used, internal border is rarely set, and the
682 overhead is very small. */
683 if (windows_or_buffers_changed
684 && desired_row->full_width_p
685 && (f = XFRAME (w->frame),
686 width = FRAME_INTERNAL_BORDER_WIDTH (f),
259cf6bc
GM
687 width != 0)
688 && (height = desired_row->visible_height,
689 height > 0))
ef253080 690 {
ef253080 691 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
06a2c219 692
ef253080
GM
693 /* Internal border is drawn below the tool bar. */
694 if (WINDOWP (f->tool_bar_window)
695 && w == XWINDOW (f->tool_bar_window))
696 y -= width;
7d0393cf 697
ef253080
GM
698 BLOCK_INPUT;
699 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
700 0, y, width, height, False);
701 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
0899d58c 702 FRAME_PIXEL_WIDTH (f) - width,
ef253080 703 y, width, height, False);
06a2c219
GM
704 UNBLOCK_INPUT;
705 }
706}
707
06a2c219 708static void
5958f265 709x_draw_fringe_bitmap (w, row, p)
06a2c219
GM
710 struct window *w;
711 struct glyph_row *row;
5958f265 712 struct draw_fringe_bitmap_params *p;
06a2c219
GM
713{
714 struct frame *f = XFRAME (WINDOW_FRAME (w));
715 Display *display = FRAME_X_DISPLAY (f);
716 Window window = FRAME_X_WINDOW (f);
06a2c219 717 GC gc = f->output_data.x->normal_gc;
5958f265 718 struct face *face = p->face;
6b61353c 719 int rowY;
06a2c219
GM
720
721 /* Must clip because of partially visible lines. */
6b61353c
KH
722 rowY = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
723 if (p->y < rowY)
724 {
725 /* Adjust position of "bottom aligned" bitmap on partially
726 visible last row. */
727 int oldY = row->y;
728 int oldVH = row->visible_height;
729 row->visible_height = p->h;
730 row->y -= rowY - p->y;
08f66682 731 x_clip_to_row (w, row, -1, gc);
6b61353c
KH
732 row->y = oldY;
733 row->visible_height = oldVH;
734 }
735 else
08f66682 736 x_clip_to_row (w, row, -1, gc);
06a2c219 737
9143075d 738 if (!p->overlay_p)
976b73d7 739 {
9143075d
YM
740 int bx = p->bx, by = p->by, nx = p->nx, ny = p->ny;
741
976b73d7
KS
742 /* In case the same realized face is used for fringes and
743 for something displayed in the text (e.g. face `region' on
744 mono-displays, the fill style may have been changed to
745 FillSolid in x_draw_glyph_string_background. */
746 if (face->stipple)
747 XSetFillStyle (display, face->gc, FillOpaqueStippled);
748 else
749 XSetForeground (display, face->gc, face->background);
7d0393cf 750
9143075d
YM
751#ifdef USE_TOOLKIT_SCROLL_BARS
752 /* If the fringe is adjacent to the left (right) scroll bar of a
753 leftmost (rightmost, respectively) window, then extend its
754 background to the gap between the fringe and the bar. */
755 if ((WINDOW_LEFTMOST_P (w)
756 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
757 || (WINDOW_RIGHTMOST_P (w)
758 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w)))
759 {
760 int sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
761
762 if (sb_width > 0)
763 {
764 int left = WINDOW_SCROLL_BAR_AREA_X (w);
765 int width = (WINDOW_CONFIG_SCROLL_BAR_COLS (w)
766 * FRAME_COLUMN_WIDTH (f));
767
768 if (bx < 0)
769 {
770 /* Bitmap fills the fringe. */
771 if (left + width == p->x)
772 bx = left + sb_width;
773 else if (p->x + p->wd == left)
774 bx = left;
775 if (bx >= 0)
776 {
777 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
778
779 nx = width - sb_width;
780 by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
781 row->y));
782 ny = row->visible_height;
783 }
784 }
785 else
786 {
787 if (left + width == bx)
788 {
789 bx = left + sb_width;
790 nx += width - sb_width;
791 }
792 else if (bx + nx == left)
793 nx += width - sb_width;
794 }
795 }
796 }
797#endif
798 if (bx >= 0 && nx > 0)
799 XFillRectangle (display, window, face->gc, bx, by, nx, ny);
5958f265 800
976b73d7
KS
801 if (!face->stipple)
802 XSetForeground (display, face->gc, face->foreground);
803 }
804
6b61353c 805 if (p->which)
dbdc9702 806 {
6b61353c
KH
807 unsigned char *bits;
808 Pixmap pixmap, clipmask = (Pixmap) 0;
5958f265 809 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
6b61353c
KH
810 XGCValues gcv;
811
812 if (p->wd > 8)
813 bits = (unsigned char *)(p->bits + p->dh);
814 else
815 bits = (unsigned char *)p->bits + p->dh;
5958f265 816
dbdc9702
GM
817 /* Draw the bitmap. I believe these small pixmaps can be cached
818 by the server. */
5958f265 819 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
6b61353c
KH
820 (p->cursor_p
821 ? (p->overlay_p ? face->background
822 : f->output_data.x->cursor_pixel)
823 : face->foreground),
dbdc9702 824 face->background, depth);
6b61353c
KH
825
826 if (p->overlay_p)
827 {
828 clipmask = XCreatePixmapFromBitmapData (display,
829 FRAME_X_DISPLAY_INFO (f)->root_window,
830 bits, p->wd, p->h,
831 1, 0, 1);
832 gcv.clip_mask = clipmask;
833 gcv.clip_x_origin = p->x;
834 gcv.clip_y_origin = p->y;
835 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
836 }
837
5958f265
KS
838 XCopyArea (display, pixmap, window, gc, 0, 0,
839 p->wd, p->h, p->x, p->y);
dbdc9702 840 XFreePixmap (display, pixmap);
6b61353c
KH
841
842 if (p->overlay_p)
843 {
844 gcv.clip_mask = (Pixmap) 0;
845 XChangeGC (display, gc, GCClipMask, &gcv);
846 XFreePixmap (display, clipmask);
847 }
dbdc9702 848 }
7d0393cf 849
06a2c219
GM
850 XSetClipMask (display, gc, None);
851}
852
dc6f92b8 853\f
06a2c219
GM
854
855/* This is called when starting Emacs and when restarting after
856 suspend. When starting Emacs, no X window is mapped. And nothing
857 must be done to Emacs's own window if it is suspended (though that
858 rarely happens). */
dc6f92b8 859
dfcf069d 860static void
6ed8eeff 861XTset_terminal_modes (struct terminal *terminal)
dc6f92b8
JB
862{
863}
864
06a2c219
GM
865/* This is called when exiting or suspending Emacs. Exiting will make
866 the X-windows go away, and suspending requires no action. */
dc6f92b8 867
dfcf069d 868static void
6ed8eeff 869XTreset_terminal_modes (struct terminal *terminal)
dc6f92b8 870{
dc6f92b8 871}
06a2c219
GM
872
873
dc6f92b8 874\f
06a2c219
GM
875/***********************************************************************
876 Display Iterator
877 ***********************************************************************/
878
879/* Function prototypes of this page. */
880
8f924df7
KH
881static int x_encode_char P_ ((int, XChar2b *, struct font_info *,
882 struct charset *, int *));
ee569018
KH
883
884
e2ef8ee6
GM
885/* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
886 is not contained in the font. */
dc43ef94 887
5da0698e
KS
888static XCharStruct *
889x_per_char_metric (font, char2b, font_type)
06a2c219
GM
890 XFontStruct *font;
891 XChar2b *char2b;
5da0698e 892 int font_type; /* unused on X */
06a2c219
GM
893{
894 /* The result metric information. */
895 XCharStruct *pcm = NULL;
dc6f92b8 896
06a2c219 897 xassert (font && char2b);
dc6f92b8 898
06a2c219 899 if (font->per_char != NULL)
dc6f92b8 900 {
06a2c219 901 if (font->min_byte1 == 0 && font->max_byte1 == 0)
dc43ef94 902 {
06a2c219
GM
903 /* min_char_or_byte2 specifies the linear character index
904 corresponding to the first element of the per_char array,
905 max_char_or_byte2 is the index of the last character. A
906 character with non-zero CHAR2B->byte1 is not in the font.
907 A character with byte2 less than min_char_or_byte2 or
908 greater max_char_or_byte2 is not in the font. */
909 if (char2b->byte1 == 0
910 && char2b->byte2 >= font->min_char_or_byte2
911 && char2b->byte2 <= font->max_char_or_byte2)
912 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
dc43ef94 913 }
06a2c219 914 else
dc6f92b8 915 {
06a2c219
GM
916 /* If either min_byte1 or max_byte1 are nonzero, both
917 min_char_or_byte2 and max_char_or_byte2 are less than
918 256, and the 2-byte character index values corresponding
919 to the per_char array element N (counting from 0) are:
920
921 byte1 = N/D + min_byte1
922 byte2 = N\D + min_char_or_byte2
923
924 where:
925
926 D = max_char_or_byte2 - min_char_or_byte2 + 1
927 / = integer division
928 \ = integer modulus */
929 if (char2b->byte1 >= font->min_byte1
930 && char2b->byte1 <= font->max_byte1
931 && char2b->byte2 >= font->min_char_or_byte2
932 && char2b->byte2 <= font->max_char_or_byte2)
933 {
934 pcm = (font->per_char
935 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
936 * (char2b->byte1 - font->min_byte1))
937 + (char2b->byte2 - font->min_char_or_byte2));
938 }
dc6f92b8 939 }
06a2c219
GM
940 }
941 else
942 {
943 /* If the per_char pointer is null, all glyphs between the first
944 and last character indexes inclusive have the same
945 information, as given by both min_bounds and max_bounds. */
946 if (char2b->byte2 >= font->min_char_or_byte2
947 && char2b->byte2 <= font->max_char_or_byte2)
948 pcm = &font->max_bounds;
949 }
dc6f92b8 950
ee569018 951 return ((pcm == NULL
3e71d8f2 952 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
ee569018 953 ? NULL : pcm);
06a2c219 954}
b73b6aaf 955
57b03282 956
06a2c219
GM
957/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
958 the two-byte form of C. Encoding is returned in *CHAR2B. */
dc43ef94 959
5da0698e 960static int
8f924df7 961x_encode_char (c, char2b, font_info, charset, two_byte_p)
06a2c219
GM
962 int c;
963 XChar2b *char2b;
964 struct font_info *font_info;
8a78c965 965 struct charset *charset;
5da0698e 966 int *two_byte_p;
06a2c219 967{
06a2c219
GM
968 XFontStruct *font = font_info->font;
969
970 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
971 This may be either a program in a special encoder language or a
972 fixed encoding. */
973 if (font_info->font_encoder)
974 {
975 /* It's a program. */
976 struct ccl_program *ccl = font_info->font_encoder;
977
d8752c9e 978 check_ccl_update (ccl);
06a2c219
GM
979 if (CHARSET_DIMENSION (charset) == 1)
980 {
8a78c965 981 ccl->reg[0] = CHARSET_ID (charset);
06a2c219 982 ccl->reg[1] = char2b->byte2;
6e666629 983 ccl->reg[2] = -1;
06a2c219
GM
984 }
985 else
986 {
8a78c965 987 ccl->reg[0] = CHARSET_ID (charset);
06a2c219
GM
988 ccl->reg[1] = char2b->byte1;
989 ccl->reg[2] = char2b->byte2;
990 }
7d0393cf 991
d8d032b8 992 ccl_driver (ccl, NULL, NULL, 0, 0, Qnil);
7d0393cf 993
06a2c219
GM
994 /* We assume that MSBs are appropriately set/reset by CCL
995 program. */
996 if (font->max_byte1 == 0) /* 1-byte font */
8f924df7 997 STORE_XCHAR2B (char2b, 0, ccl->reg[1]);
06a2c219 998 else
8f924df7 999 STORE_XCHAR2B (char2b, ccl->reg[1], ccl->reg[2]);
06a2c219 1000 }
8a78c965 1001 else if (font_info->encoding_type)
06a2c219
GM
1002 {
1003 /* Fixed encoding scheme. See fontset.h for the meaning of the
1004 encoding numbers. */
8a78c965 1005 unsigned char enc = font_info->encoding_type;
7d0393cf 1006
06a2c219
GM
1007 if ((enc == 1 || enc == 2)
1008 && CHARSET_DIMENSION (charset) == 2)
1009 char2b->byte1 |= 0x80;
7d0393cf 1010
06a2c219
GM
1011 if (enc == 1 || enc == 3)
1012 char2b->byte2 |= 0x80;
1013 }
06a2c219 1014
ee569018 1015 if (two_byte_p)
5da0698e 1016 *two_byte_p = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
b4192550 1017
5da0698e 1018 return FONT_TYPE_UNKNOWN;
06a2c219
GM
1019}
1020
1021
06a2c219
GM
1022\f
1023/***********************************************************************
1024 Glyph display
1025 ***********************************************************************/
1026
06a2c219 1027
06a2c219 1028
06a2c219
GM
1029static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
1030static void x_set_glyph_string_gc P_ ((struct glyph_string *));
1031static void x_draw_glyph_string_background P_ ((struct glyph_string *,
1032 int));
1033static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
b4192550 1034static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
06a2c219
GM
1035static void x_draw_glyph_string_box P_ ((struct glyph_string *));
1036static void x_draw_glyph_string P_ ((struct glyph_string *));
1037static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
1038static void x_set_cursor_gc P_ ((struct glyph_string *));
1039static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
1040static void x_set_mouse_face_gc P_ ((struct glyph_string *));
06a2c219 1041static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
68c45bf0 1042 unsigned long *, double, int));
06a2c219 1043static void x_setup_relief_color P_ ((struct frame *, struct relief *,
68c45bf0 1044 double, int, unsigned long));
06a2c219
GM
1045static void x_setup_relief_colors P_ ((struct glyph_string *));
1046static void x_draw_image_glyph_string P_ ((struct glyph_string *));
1047static void x_draw_image_relief P_ ((struct glyph_string *));
1048static void x_draw_image_foreground P_ ((struct glyph_string *));
1049static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
06a2c219
GM
1050static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
1051 int, int, int));
1052static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
57326d99
KS
1053 int, int, int, int, int, int,
1054 XRectangle *));
06a2c219
GM
1055static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
1056 int, int, int, XRectangle *));
5da0698e
KS
1057
1058#if GLYPH_DEBUG
1059static void x_check_font P_ ((struct frame *, XFontStruct *));
1060#endif
06a2c219
GM
1061
1062
1063/* Set S->gc to a suitable GC for drawing glyph string S in cursor
1064 face. */
1065
1066static void
1067x_set_cursor_gc (s)
1068 struct glyph_string *s;
1069{
1070 if (s->font == FRAME_FONT (s->f)
1071 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
1072 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
b4192550 1073 && !s->cmp)
06a2c219
GM
1074 s->gc = s->f->output_data.x->cursor_gc;
1075 else
1076 {
1077 /* Cursor on non-default face: must merge. */
1078 XGCValues xgcv;
1079 unsigned long mask;
1080
1081 xgcv.background = s->f->output_data.x->cursor_pixel;
1082 xgcv.foreground = s->face->background;
1083
1084 /* If the glyph would be invisible, try a different foreground. */
1085 if (xgcv.foreground == xgcv.background)
1086 xgcv.foreground = s->face->foreground;
1087 if (xgcv.foreground == xgcv.background)
1088 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
1089 if (xgcv.foreground == xgcv.background)
1090 xgcv.foreground = s->face->foreground;
1091
1092 /* Make sure the cursor is distinct from text in this face. */
1093 if (xgcv.background == s->face->background
1094 && xgcv.foreground == s->face->foreground)
1095 {
1096 xgcv.background = s->face->foreground;
1097 xgcv.foreground = s->face->background;
1098 }
1099
1100 IF_DEBUG (x_check_font (s->f, s->font));
1101 xgcv.font = s->font->fid;
1102 xgcv.graphics_exposures = False;
1103 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
1104
1105 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1106 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1107 mask, &xgcv);
1108 else
1109 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1110 = XCreateGC (s->display, s->window, mask, &xgcv);
1111
1112 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1113 }
1114}
1115
1116
1117/* Set up S->gc of glyph string S for drawing text in mouse face. */
7d0393cf 1118
06a2c219
GM
1119static void
1120x_set_mouse_face_gc (s)
1121 struct glyph_string *s;
7d0393cf 1122{
06a2c219 1123 int face_id;
ee569018 1124 struct face *face;
06a2c219 1125
e4ded23c 1126 /* What face has to be used last for the mouse face? */
06a2c219 1127 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
ee569018 1128 face = FACE_FROM_ID (s->f, face_id);
e4ded23c
GM
1129 if (face == NULL)
1130 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
7d0393cf 1131
033e3e18 1132 if (s->first_glyph->type == CHAR_GLYPH)
e3c7c6a5 1133 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
033e3e18 1134 else
e3c7c6a5 1135 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
06a2c219
GM
1136 s->face = FACE_FROM_ID (s->f, face_id);
1137 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1138
1139 /* If font in this face is same as S->font, use it. */
1140 if (s->font == s->face->font)
1141 s->gc = s->face->gc;
c9ff09ee
KH
1142#ifdef USE_FONT_BACKEND
1143 else if (enable_font_backend)
1144 /* No need of setting a font for s->gc. */
1145 s->gc = s->face->gc;
1146#endif /* USE_FONT_BACKEND */
06a2c219
GM
1147 else
1148 {
1149 /* Otherwise construct scratch_cursor_gc with values from FACE
1150 but font FONT. */
1151 XGCValues xgcv;
1152 unsigned long mask;
7d0393cf 1153
06a2c219
GM
1154 xgcv.background = s->face->background;
1155 xgcv.foreground = s->face->foreground;
1156 IF_DEBUG (x_check_font (s->f, s->font));
1157 xgcv.font = s->font->fid;
1158 xgcv.graphics_exposures = False;
1159 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
7d0393cf 1160
06a2c219
GM
1161 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1162 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1163 mask, &xgcv);
1164 else
1165 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1166 = XCreateGC (s->display, s->window, mask, &xgcv);
7d0393cf 1167
06a2c219
GM
1168 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1169 }
1170
1171 xassert (s->gc != 0);
1172}
1173
1174
1175/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1176 Faces to use in the mode line have already been computed when the
1177 matrix was built, so there isn't much to do, here. */
1178
1179static INLINE void
1180x_set_mode_line_face_gc (s)
1181 struct glyph_string *s;
7d0393cf 1182{
06a2c219 1183 s->gc = s->face->gc;
06a2c219
GM
1184}
1185
1186
1187/* Set S->gc of glyph string S for drawing that glyph string. Set
1188 S->stippled_p to a non-zero value if the face of S has a stipple
1189 pattern. */
1190
1191static INLINE void
1192x_set_glyph_string_gc (s)
1193 struct glyph_string *s;
1194{
209f68d9 1195 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
7d0393cf 1196
06a2c219
GM
1197 if (s->hl == DRAW_NORMAL_TEXT)
1198 {
1199 s->gc = s->face->gc;
1200 s->stippled_p = s->face->stipple != 0;
1201 }
1202 else if (s->hl == DRAW_INVERSE_VIDEO)
1203 {
1204 x_set_mode_line_face_gc (s);
1205 s->stippled_p = s->face->stipple != 0;
1206 }
1207 else if (s->hl == DRAW_CURSOR)
1208 {
1209 x_set_cursor_gc (s);
1210 s->stippled_p = 0;
1211 }
1212 else if (s->hl == DRAW_MOUSE_FACE)
1213 {
1214 x_set_mouse_face_gc (s);
1215 s->stippled_p = s->face->stipple != 0;
1216 }
1217 else if (s->hl == DRAW_IMAGE_RAISED
1218 || s->hl == DRAW_IMAGE_SUNKEN)
1219 {
1220 s->gc = s->face->gc;
1221 s->stippled_p = s->face->stipple != 0;
1222 }
1223 else
1224 {
1225 s->gc = s->face->gc;
1226 s->stippled_p = s->face->stipple != 0;
1227 }
1228
1229 /* GC must have been set. */
1230 xassert (s->gc != 0);
1231}
1232
1233
06a2c219
GM
1234/* Set clipping for output of glyph string S. S may be part of a mode
1235 line or menu if we don't have X toolkit support. */
1236
1237static INLINE void
1238x_set_glyph_string_clipping (s)
1239 struct glyph_string *s;
1240{
c9ff09ee 1241#ifdef USE_FONT_BACKEND
fdd6e0c0
KH
1242 XRectangle *r = s->clip;
1243#else
1244 XRectangle r[2];
1245#endif
1246 int n = get_glyph_string_clip_rects (s, r, 2);
1247
1248 if (n > 0)
1249 XSetClipRectangles (s->display, s->gc, 0, 0, r, n, Unsorted);
1250#ifdef USE_FONT_BACKEND
1251 s->num_clips = n;
1252#endif
06a2c219
GM
1253}
1254
1255
1dd71645
KH
1256/* Set SRC's clipping for output of glyph string DST. This is called
1257 when we are drawing DST's left_overhang or right_overhang only in
1258 the area of SRC. */
1259
1260static void
1261x_set_glyph_string_clipping_exactly (src, dst)
1262 struct glyph_string *src, *dst;
1263{
1264 XRectangle r;
c9ff09ee
KH
1265
1266#ifdef USE_FONT_BACKEND
1267 if (enable_font_backend)
1268 {
fdd6e0c0
KH
1269 r.x = src->x;
1270 r.width = src->width;
1271 r.y = src->y;
1272 r.height = src->height;
1273 dst->clip[0] = r;
1274 dst->num_clips = 1;
c9ff09ee
KH
1275 }
1276 else
1277 {
1278#endif /* USE_FONT_BACKEND */
b32d320e
KH
1279 struct glyph_string *clip_head = src->clip_head;
1280 struct glyph_string *clip_tail = src->clip_tail;
1dd71645 1281
b32d320e
KH
1282 /* This foces clipping just this glyph string. */
1283 src->clip_head = src->clip_tail = src;
1dd71645 1284 get_glyph_string_clip_rect (src, &r);
b32d320e 1285 src->clip_head = clip_head, src->clip_tail = clip_tail;
c9ff09ee
KH
1286#ifdef USE_FONT_BACKEND
1287 }
1288#endif /* USE_FONT_BACKEND */
1dd71645
KH
1289 XSetClipRectangles (dst->display, dst->gc, 0, 0, &r, 1, Unsorted);
1290}
1291
1292
5da0698e 1293/* RIF:
980a8c09 1294 Compute left and right overhang of glyph string S. */
06a2c219 1295
5da0698e 1296static void
06a2c219
GM
1297x_compute_glyph_string_overhangs (s)
1298 struct glyph_string *s;
1299{
b4192550 1300 if (s->cmp == NULL
06a2c219
GM
1301 && s->first_glyph->type == CHAR_GLYPH)
1302 {
1303 XCharStruct cs;
1304 int direction, font_ascent, font_descent;
c9ff09ee
KH
1305
1306#ifdef USE_FONT_BACKEND
1307 if (enable_font_backend)
1308 {
1309 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1310 struct font *font = (struct font *) s->font_info;
1311 struct font_metrics metrics;
1312 int i;
1313
1314 for (i = 0; i < s->nchars; i++)
1315 code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
1316 font->driver->text_extents (font, code, s->nchars, &metrics);
1317 cs.rbearing = metrics.rbearing;
1318 cs.lbearing = metrics.lbearing;
1319 cs.width = metrics.width;
1320 }
1321 else
1322#endif /* USE_FONT_BACKEND */
06a2c219
GM
1323 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
1324 &font_ascent, &font_descent, &cs);
1325 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
1326 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
1327 }
980a8c09
KH
1328 else if (s->cmp)
1329 {
1330 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1331 s->left_overhang = - s->cmp->lbearing;
1332 }
06a2c219
GM
1333}
1334
1335
06a2c219
GM
1336/* Fill rectangle X, Y, W, H with background color of glyph string S. */
1337
1338static INLINE void
1339x_clear_glyph_string_rect (s, x, y, w, h)
1340 struct glyph_string *s;
1341 int x, y, w, h;
1342{
1343 XGCValues xgcv;
1344 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1345 XSetForeground (s->display, s->gc, xgcv.background);
1346 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1347 XSetForeground (s->display, s->gc, xgcv.foreground);
1348}
1349
1350
1351/* Draw the background of glyph_string S. If S->background_filled_p
1352 is non-zero don't draw it. FORCE_P non-zero means draw the
1353 background even if it wouldn't be drawn normally. This is used
b4192550
KH
1354 when a string preceding S draws into the background of S, or S
1355 contains the first component of a composition. */
06a2c219
GM
1356
1357static void
1358x_draw_glyph_string_background (s, force_p)
1359 struct glyph_string *s;
1360 int force_p;
1361{
1362 /* Nothing to do if background has already been drawn or if it
1363 shouldn't be drawn in the first place. */
1364 if (!s->background_filled_p)
1365 {
ea2ba0d4
KH
1366 int box_line_width = max (s->face->box_line_width, 0);
1367
b4192550 1368 if (s->stippled_p)
06a2c219
GM
1369 {
1370 /* Fill background with a stipple pattern. */
1371 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1372 XFillRectangle (s->display, s->window, s->gc, s->x,
ea2ba0d4 1373 s->y + box_line_width,
06a2c219 1374 s->background_width,
ea2ba0d4 1375 s->height - 2 * box_line_width);
06a2c219
GM
1376 XSetFillStyle (s->display, s->gc, FillSolid);
1377 s->background_filled_p = 1;
1378 }
ea2ba0d4 1379 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
06a2c219
GM
1380 || s->font_not_found_p
1381 || s->extends_to_end_of_line_p
06a2c219
GM
1382 || force_p)
1383 {
ea2ba0d4 1384 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
06a2c219 1385 s->background_width,
ea2ba0d4 1386 s->height - 2 * box_line_width);
06a2c219
GM
1387 s->background_filled_p = 1;
1388 }
1389 }
1390}
1391
1392
1393/* Draw the foreground of glyph string S. */
1394
1395static void
1396x_draw_glyph_string_foreground (s)
1397 struct glyph_string *s;
1398{
1399 int i, x;
1400
1401 /* If first glyph of S has a left box line, start drawing the text
1402 of S to the right of that box line. */
1403 if (s->face->box != FACE_NO_BOX
1404 && s->first_glyph->left_box_line_p)
1ea40aa2 1405 x = s->x + eabs (s->face->box_line_width);
06a2c219
GM
1406 else
1407 x = s->x;
1408
b4192550
KH
1409 /* Draw characters of S as rectangles if S's font could not be
1410 loaded. */
1411 if (s->font_not_found_p)
06a2c219 1412 {
b4192550 1413 for (i = 0; i < s->nchars; ++i)
06a2c219 1414 {
b4192550
KH
1415 struct glyph *g = s->first_glyph + i;
1416 XDrawRectangle (s->display, s->window,
1417 s->gc, x, s->y, g->pixel_width - 1,
1418 s->height - 1);
1419 x += g->pixel_width;
06a2c219
GM
1420 }
1421 }
c9ff09ee
KH
1422#ifdef USE_FONT_BACKEND
1423 else if (enable_font_backend)
1424 {
c9ff09ee
KH
1425 int boff = s->font_info->baseline_offset;
1426 struct font *font = (struct font *) s->font_info;
1427 int y;
1428
c9ff09ee
KH
1429 if (s->font_info->vertical_centering)
1430 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
1431
1432 y = s->ybase - boff;
1433 if (s->for_overlaps
1434 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1435 font->driver->draw (s, 0, s->nchars, x, y, 0);
1436 else
1437 font->driver->draw (s, 0, s->nchars, x, y, 1);
1438 if (s->face->overstrike)
1439 font->driver->draw (s, 0, s->nchars, x + 1, y, 0);
1440 }
1441#endif /* USE_FONT_BACKEND */
06a2c219
GM
1442 else
1443 {
b4192550
KH
1444 char *char1b = (char *) s->char2b;
1445 int boff = s->font_info->baseline_offset;
06a2c219 1446
b4192550
KH
1447 if (s->font_info->vertical_centering)
1448 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
1449
1450 /* If we can use 8-bit functions, condense S->char2b. */
1451 if (!s->two_byte_p)
1452 for (i = 0; i < s->nchars; ++i)
1453 char1b[i] = s->char2b[i].byte2;
1454
1455 /* Draw text with XDrawString if background has already been
1456 filled. Otherwise, use XDrawImageString. (Note that
1457 XDrawImageString is usually faster than XDrawString.) Always
1458 use XDrawImageString when drawing the cursor so that there is
1459 no chance that characters under a box cursor are invisible. */
c2ded1b7 1460 if (s->for_overlaps
b4192550
KH
1461 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1462 {
1463 /* Draw characters with 16-bit or 8-bit functions. */
1464 if (s->two_byte_p)
1465 XDrawString16 (s->display, s->window, s->gc, x,
1466 s->ybase - boff, s->char2b, s->nchars);
1467 else
1468 XDrawString (s->display, s->window, s->gc, x,
1469 s->ybase - boff, char1b, s->nchars);
1470 }
06a2c219
GM
1471 else
1472 {
b4192550
KH
1473 if (s->two_byte_p)
1474 XDrawImageString16 (s->display, s->window, s->gc, x,
1475 s->ybase - boff, s->char2b, s->nchars);
06a2c219 1476 else
b4192550
KH
1477 XDrawImageString (s->display, s->window, s->gc, x,
1478 s->ybase - boff, char1b, s->nchars);
1479 }
fce13449
MB
1480
1481 if (s->face->overstrike)
1482 {
1483 /* For overstriking (to simulate bold-face), draw the
1484 characters again shifted to the right by one pixel. */
1485 if (s->two_byte_p)
1486 XDrawString16 (s->display, s->window, s->gc, x + 1,
1487 s->ybase - boff, s->char2b, s->nchars);
1488 else
1489 XDrawString (s->display, s->window, s->gc, x + 1,
1490 s->ybase - boff, char1b, s->nchars);
1491 }
b4192550
KH
1492 }
1493}
06a2c219 1494
b4192550 1495/* Draw the foreground of composite glyph string S. */
06a2c219 1496
b4192550
KH
1497static void
1498x_draw_composite_glyph_string_foreground (s)
1499 struct glyph_string *s;
1500{
e379a039 1501 int i, j, x;
06a2c219 1502
b4192550
KH
1503 /* If first glyph of S has a left box line, start drawing the text
1504 of S to the right of that box line. */
3eb44178 1505 if (s->face && s->face->box != FACE_NO_BOX
b4192550 1506 && s->first_glyph->left_box_line_p)
1ea40aa2 1507 x = s->x + eabs (s->face->box_line_width);
b4192550
KH
1508 else
1509 x = s->x;
06a2c219 1510
b4192550
KH
1511 /* S is a glyph string for a composition. S->gidx is the index of
1512 the first character drawn for glyphs of this composition.
1513 S->gidx == 0 means we are drawing the very first character of
1514 this composition. */
06a2c219 1515
b4192550
KH
1516 /* Draw a rectangle for the composition if the font for the very
1517 first character of the composition could not be loaded. */
1518 if (s->font_not_found_p)
1519 {
1520 if (s->gidx == 0)
1521 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1522 s->width - 1, s->height - 1);
1523 }
c9ff09ee
KH
1524#ifdef USE_FONT_BACKEND
1525 else if (enable_font_backend)
1526 {
1527 struct font *font = (struct font *) s->font_info;
1528 int y = s->ybase;
1529 int width = 0;
1530
1531 if (s->cmp->method == COMPOSITION_WITH_GLYPH_STRING)
1532 {
1533 Lisp_Object gstring = AREF (XHASH_TABLE (composition_hash_table)
1534 ->key_and_value,
1535 s->cmp->hash_index * 2);
1536 int from;
1537
1538 for (i = from = 0; i < s->nchars; i++)
1539 {
1540 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
9de04ace
KH
1541 Lisp_Object adjustment = LGLYPH_ADJUSTMENT (g);
1542 int xoff, yoff, wadjust;
c9ff09ee 1543
878a939b 1544 if (! VECTORP (adjustment))
c9ff09ee 1545 {
11bfed58 1546 width += LGLYPH_WIDTH (g);
c9ff09ee
KH
1547 continue;
1548 }
1549 if (from < i)
1550 {
1551 font->driver->draw (s, from, i, x, y, 0);
1552 x += width;
1553 }
9de04ace
KH
1554 xoff = XINT (AREF (adjustment, 0));
1555 yoff = XINT (AREF (adjustment, 1));
1556 wadjust = XINT (AREF (adjustment, 2));
1557
1558 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, 0);
11bfed58 1559 x += wadjust;
c9ff09ee
KH
1560 from = i + 1;
1561 width = 0;
1562 }
1563 if (from < i)
1564 font->driver->draw (s, from, i, x, y, 0);
1565 }
1566 else
1567 {
e379a039
KH
1568 for (i = 0, j = s->gidx; i < s->nchars; i++, j++)
1569 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
4bad3dc5 1570 {
e379a039
KH
1571 int xx = x + s->cmp->offsets[j * 2];
1572 int yy = y - s->cmp->offsets[j * 2 + 1];
4bad3dc5 1573
e379a039 1574 font->driver->draw (s, j, j + 1, xx, yy, 0);
4bad3dc5 1575 if (s->face->overstrike)
e379a039 1576 font->driver->draw (s, j, j + 1, xx + 1, yy, 0);
4bad3dc5 1577 }
c9ff09ee
KH
1578 }
1579 }
1580#endif /* USE_FONT_BACKEND */
b4192550
KH
1581 else
1582 {
e379a039 1583 for (i = 0, j = s->gidx; i < s->nchars; i++, j++)
3eb44178
KH
1584 if (s->face)
1585 {
fce13449 1586 XDrawString16 (s->display, s->window, s->gc,
e379a039
KH
1587 x + s->cmp->offsets[j * 2],
1588 s->ybase - s->cmp->offsets[j * 2 + 1],
1589 s->char2b + j, 1);
3eb44178
KH
1590 if (s->face->overstrike)
1591 XDrawString16 (s->display, s->window, s->gc,
e379a039
KH
1592 x + s->cmp->offsets[j * 2] + 1,
1593 s->ybase - s->cmp->offsets[j * 2 + 1],
1594 s->char2b + j, 1);
3eb44178 1595 }
06a2c219
GM
1596 }
1597}
1598
1599
80c32bcc
GM
1600#ifdef USE_X_TOOLKIT
1601
3e71d8f2 1602static struct frame *x_frame_of_widget P_ ((Widget));
651f03b6
GM
1603static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
1604 XrmValue *, XrmValue *, XtPointer *));
1605static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
1606 XrmValue *, Cardinal *));
80c32bcc 1607
3e71d8f2
GM
1608
1609/* Return the frame on which widget WIDGET is used.. Abort if frame
1610 cannot be determined. */
1611
e851c833 1612static struct frame *
3e71d8f2 1613x_frame_of_widget (widget)
80c32bcc 1614 Widget widget;
80c32bcc 1615{
80c32bcc 1616 struct x_display_info *dpyinfo;
5c187dee 1617 Lisp_Object tail;
3e71d8f2 1618 struct frame *f;
7d0393cf 1619
80c32bcc 1620 dpyinfo = x_display_info_for_display (XtDisplay (widget));
7d0393cf 1621
80c32bcc
GM
1622 /* Find the top-level shell of the widget. Note that this function
1623 can be called when the widget is not yet realized, so XtWindow
1624 (widget) == 0. That's the reason we can't simply use
1625 x_any_window_to_frame. */
1626 while (!XtIsTopLevelShell (widget))
1627 widget = XtParent (widget);
1628
1629 /* Look for a frame with that top-level widget. Allocate the color
1630 on that frame to get the right gamma correction value. */
8e50cc2d
SM
1631 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1632 if (FRAMEP (XCAR (tail))
80c32bcc 1633 && (f = XFRAME (XCAR (tail)),
daf01701
KL
1634 (FRAME_X_P (f)
1635 && f->output_data.nothing != 1
80c32bcc
GM
1636 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
1637 && f->output_data.x->widget == widget)
3e71d8f2 1638 return f;
80c32bcc
GM
1639
1640 abort ();
1641}
1642
3e71d8f2
GM
1643
1644/* Allocate the color COLOR->pixel on the screen and display of
1645 widget WIDGET in colormap CMAP. If an exact match cannot be
1646 allocated, try the nearest color available. Value is non-zero
1647 if successful. This is called from lwlib. */
1648
1649int
1650x_alloc_nearest_color_for_widget (widget, cmap, color)
1651 Widget widget;
1652 Colormap cmap;
1653 XColor *color;
1654{
1655 struct frame *f = x_frame_of_widget (widget);
1656 return x_alloc_nearest_color (f, cmap, color);
1657}
1658
1659
46d516e5
MB
1660/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1661 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1662 If this produces the same color as PIXEL, try a color where all RGB
1663 values have DELTA added. Return the allocated color in *PIXEL.
1664 DISPLAY is the X display, CMAP is the colormap to operate on.
1665 Value is non-zero if successful. */
1666
1667int
1668x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
1669 Widget widget;
1670 Display *display;
1671 Colormap cmap;
1672 unsigned long *pixel;
1673 double factor;
1674 int delta;
1675{
1676 struct frame *f = x_frame_of_widget (widget);
1677 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1678}
1679
1680
651f03b6
GM
1681/* Structure specifying which arguments should be passed by Xt to
1682 cvt_string_to_pixel. We want the widget's screen and colormap. */
1683
1684static XtConvertArgRec cvt_string_to_pixel_args[] =
1685 {
1686 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1687 sizeof (Screen *)},
1688 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1689 sizeof (Colormap)}
1690 };
1691
1692
1693/* The address of this variable is returned by
1694 cvt_string_to_pixel. */
1695
1696static Pixel cvt_string_to_pixel_value;
1697
1698
1699/* Convert a color name to a pixel color.
1700
1701 DPY is the display we are working on.
1702
1703 ARGS is an array of *NARGS XrmValue structures holding additional
1704 information about the widget for which the conversion takes place.
1705 The contents of this array are determined by the specification
1706 in cvt_string_to_pixel_args.
1707
1708 FROM is a pointer to an XrmValue which points to the color name to
1709 convert. TO is an XrmValue in which to return the pixel color.
1710
1711 CLOSURE_RET is a pointer to user-data, in which we record if
1712 we allocated the color or not.
1713
1714 Value is True if successful, False otherwise. */
1715
1716static Boolean
1717cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
1718 Display *dpy;
1719 XrmValue *args;
1720 Cardinal *nargs;
1721 XrmValue *from, *to;
1722 XtPointer *closure_ret;
1723{
1724 Screen *screen;
1725 Colormap cmap;
1726 Pixel pixel;
1727 String color_name;
1728 XColor color;
1729
1730 if (*nargs != 2)
1731 {
1732 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1733 "wrongParameters", "cvt_string_to_pixel",
1734 "XtToolkitError",
1735 "Screen and colormap args required", NULL, NULL);
1736 return False;
1737 }
1738
1739 screen = *(Screen **) args[0].addr;
1740 cmap = *(Colormap *) args[1].addr;
1741 color_name = (String) from->addr;
1742
1743 if (strcmp (color_name, XtDefaultBackground) == 0)
1744 {
1745 *closure_ret = (XtPointer) False;
1746 pixel = WhitePixelOfScreen (screen);
1747 }
1748 else if (strcmp (color_name, XtDefaultForeground) == 0)
1749 {
1750 *closure_ret = (XtPointer) False;
1751 pixel = BlackPixelOfScreen (screen);
1752 }
1753 else if (XParseColor (dpy, cmap, color_name, &color)
1754 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1755 {
1756 pixel = color.pixel;
1757 *closure_ret = (XtPointer) True;
1758 }
1759 else
1760 {
1761 String params[1];
1762 Cardinal nparams = 1;
7d0393cf 1763
651f03b6
GM
1764 params[0] = color_name;
1765 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1766 "badValue", "cvt_string_to_pixel",
1767 "XtToolkitError", "Invalid color `%s'",
1768 params, &nparams);
1769 return False;
1770 }
1771
1772 if (to->addr != NULL)
1773 {
1774 if (to->size < sizeof (Pixel))
1775 {
1776 to->size = sizeof (Pixel);
1777 return False;
1778 }
7d0393cf 1779
651f03b6
GM
1780 *(Pixel *) to->addr = pixel;
1781 }
1782 else
1783 {
1784 cvt_string_to_pixel_value = pixel;
1785 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1786 }
7d0393cf 1787
651f03b6
GM
1788 to->size = sizeof (Pixel);
1789 return True;
1790}
1791
1792
1793/* Free a pixel color which was previously allocated via
1794 cvt_string_to_pixel. This is registered as the destructor
1795 for this type of resource via XtSetTypeConverter.
1796
1797 APP is the application context in which we work.
1798
1799 TO is a pointer to an XrmValue holding the color to free.
1800 CLOSURE is the value we stored in CLOSURE_RET for this color
1801 in cvt_string_to_pixel.
1802
1803 ARGS and NARGS are like for cvt_string_to_pixel. */
1804
1805static void
1806cvt_pixel_dtor (app, to, closure, args, nargs)
1807 XtAppContext app;
1808 XrmValuePtr to;
1809 XtPointer closure;
1810 XrmValuePtr args;
1811 Cardinal *nargs;
1812{
1813 if (*nargs != 2)
1814 {
1815 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1816 "XtToolkitError",
1817 "Screen and colormap arguments required",
1818 NULL, NULL);
1819 }
1820 else if (closure != NULL)
1821 {
1822 /* We did allocate the pixel, so free it. */
1823 Screen *screen = *(Screen **) args[0].addr;
1824 Colormap cmap = *(Colormap *) args[1].addr;
1825 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
97762eb7 1826 (Pixel *) to->addr, 1);
651f03b6
GM
1827 }
1828}
1829
1830
80c32bcc
GM
1831#endif /* USE_X_TOOLKIT */
1832
1833
f04e1297 1834/* Value is an array of XColor structures for the contents of the
651f03b6 1835 color map of display DPY. Set *NCELLS to the size of the array.
f04e1297
GM
1836 Note that this probably shouldn't be called for large color maps,
1837 say a 24-bit TrueColor map. */
1838
1839static const XColor *
651f03b6
GM
1840x_color_cells (dpy, ncells)
1841 Display *dpy;
f04e1297
GM
1842 int *ncells;
1843{
651f03b6 1844 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
f04e1297
GM
1845
1846 if (dpyinfo->color_cells == NULL)
1847 {
651f03b6 1848 Screen *screen = dpyinfo->screen;
f04e1297 1849 int i;
7d0393cf 1850
f04e1297 1851 dpyinfo->ncolor_cells
651f03b6 1852 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
f04e1297
GM
1853 dpyinfo->color_cells
1854 = (XColor *) xmalloc (dpyinfo->ncolor_cells
1855 * sizeof *dpyinfo->color_cells);
7d0393cf 1856
f04e1297
GM
1857 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
1858 dpyinfo->color_cells[i].pixel = i;
7d0393cf 1859
651f03b6 1860 XQueryColors (dpy, dpyinfo->cmap,
f04e1297
GM
1861 dpyinfo->color_cells, dpyinfo->ncolor_cells);
1862 }
1863
1864 *ncells = dpyinfo->ncolor_cells;
1865 return dpyinfo->color_cells;
1866}
1867
1868
1869/* On frame F, translate pixel colors to RGB values for the NCOLORS
1870 colors in COLORS. Use cached information, if available. */
1871
1872void
1873x_query_colors (f, colors, ncolors)
1874 struct frame *f;
1875 XColor *colors;
1876 int ncolors;
1877{
1878 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1879
1880 if (dpyinfo->color_cells)
1881 {
1882 int i;
1883 for (i = 0; i < ncolors; ++i)
1884 {
1885 unsigned long pixel = colors[i].pixel;
1886 xassert (pixel < dpyinfo->ncolor_cells);
1887 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
1888 colors[i] = dpyinfo->color_cells[pixel];
1889 }
1890 }
1891 else
1892 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1893}
1894
1895
1896/* On frame F, translate pixel color to RGB values for the color in
1897 COLOR. Use cached information, if available. */
1898
1899void
1900x_query_color (f, color)
1901 struct frame *f;
1902 XColor *color;
1903{
1904 x_query_colors (f, color, 1);
1905}
7d0393cf 1906
f04e1297 1907
651f03b6
GM
1908/* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1909 exact match can't be allocated, try the nearest color available.
1910 Value is non-zero if successful. Set *COLOR to the color
1911 allocated. */
06a2c219 1912
651f03b6
GM
1913static int
1914x_alloc_nearest_color_1 (dpy, cmap, color)
1915 Display *dpy;
06a2c219
GM
1916 Colormap cmap;
1917 XColor *color;
1918{
80c32bcc
GM
1919 int rc;
1920
651f03b6 1921 rc = XAllocColor (dpy, cmap, color);
06a2c219
GM
1922 if (rc == 0)
1923 {
1924 /* If we got to this point, the colormap is full, so we're going
1925 to try to get the next closest color. The algorithm used is
1926 a least-squares matching, which is what X uses for closest
1927 color matching with StaticColor visuals. */
1928 int nearest, i;
1929 unsigned long nearest_delta = ~0;
f04e1297 1930 int ncells;
651f03b6 1931 const XColor *cells = x_color_cells (dpy, &ncells);
06a2c219
GM
1932
1933 for (nearest = i = 0; i < ncells; ++i)
1934 {
1935 long dred = (color->red >> 8) - (cells[i].red >> 8);
1936 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
1937 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1938 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
1939
1940 if (delta < nearest_delta)
1941 {
1942 nearest = i;
1943 nearest_delta = delta;
1944 }
1945 }
7d0393cf 1946
06a2c219
GM
1947 color->red = cells[nearest].red;
1948 color->green = cells[nearest].green;
1949 color->blue = cells[nearest].blue;
651f03b6 1950 rc = XAllocColor (dpy, cmap, color);
06a2c219 1951 }
35efe0a1
GM
1952 else
1953 {
1954 /* If allocation succeeded, and the allocated pixel color is not
1955 equal to a cached pixel color recorded earlier, there was a
1956 change in the colormap, so clear the color cache. */
651f03b6 1957 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
35efe0a1 1958 XColor *cached_color;
7d0393cf 1959
35efe0a1
GM
1960 if (dpyinfo->color_cells
1961 && (cached_color = &dpyinfo->color_cells[color->pixel],
cae71efe
GM
1962 (cached_color->red != color->red
1963 || cached_color->blue != color->blue
1964 || cached_color->green != color->green)))
35efe0a1
GM
1965 {
1966 xfree (dpyinfo->color_cells);
1967 dpyinfo->color_cells = NULL;
1968 dpyinfo->ncolor_cells = 0;
1969 }
1970 }
06a2c219 1971
d9c545da
GM
1972#ifdef DEBUG_X_COLORS
1973 if (rc)
1974 register_color (color->pixel);
1975#endif /* DEBUG_X_COLORS */
7d0393cf 1976
06a2c219
GM
1977 return rc;
1978}
1979
1980
651f03b6
GM
1981/* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1982 exact match can't be allocated, try the nearest color available.
1983 Value is non-zero if successful. Set *COLOR to the color
1984 allocated. */
1985
1986int
1987x_alloc_nearest_color (f, cmap, color)
1988 struct frame *f;
1989 Colormap cmap;
1990 XColor *color;
1991{
1992 gamma_correct (f, color);
1993 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1994}
1995
1996
d9c545da
GM
1997/* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1998 It's necessary to do this instead of just using PIXEL directly to
1999 get color reference counts right. */
2000
2001unsigned long
2002x_copy_color (f, pixel)
2003 struct frame *f;
2004 unsigned long pixel;
2005{
2006 XColor color;
2007
2008 color.pixel = pixel;
2009 BLOCK_INPUT;
f04e1297 2010 x_query_color (f, &color);
d9c545da
GM
2011 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
2012 UNBLOCK_INPUT;
2013#ifdef DEBUG_X_COLORS
2014 register_color (pixel);
2015#endif
2016 return color.pixel;
2017}
2018
2019
3e71d8f2
GM
2020/* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
2021 It's necessary to do this instead of just using PIXEL directly to
2022 get color reference counts right. */
2023
2024unsigned long
2025x_copy_dpy_color (dpy, cmap, pixel)
2026 Display *dpy;
2027 Colormap cmap;
2028 unsigned long pixel;
2029{
2030 XColor color;
2031
2032 color.pixel = pixel;
2033 BLOCK_INPUT;
2034 XQueryColor (dpy, cmap, &color);
2035 XAllocColor (dpy, cmap, &color);
2036 UNBLOCK_INPUT;
2037#ifdef DEBUG_X_COLORS
2038 register_color (pixel);
2039#endif
2040 return color.pixel;
2041}
2042
2043
6d8b0acd 2044/* Brightness beyond which a color won't have its highlight brightness
d7361edf 2045 boosted.
6d8b0acd 2046
d7361edf
MB
2047 Nominally, highlight colors for `3d' faces are calculated by
2048 brightening an object's color by a constant scale factor, but this
2049 doesn't yield good results for dark colors, so for colors who's
2050 brightness is less than this value (on a scale of 0-65535) have an
2051 use an additional additive factor.
6d8b0acd
MB
2052
2053 The value here is set so that the default menu-bar/mode-line color
2054 (grey75) will not have its highlights changed at all. */
2055#define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
2056
2057
06a2c219
GM
2058/* Allocate a color which is lighter or darker than *PIXEL by FACTOR
2059 or DELTA. Try a color with RGB values multiplied by FACTOR first.
2060 If this produces the same color as PIXEL, try a color where all RGB
2061 values have DELTA added. Return the allocated color in *PIXEL.
2062 DISPLAY is the X display, CMAP is the colormap to operate on.
2063 Value is non-zero if successful. */
2064
2065static int
2066x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
2067 struct frame *f;
2068 Display *display;
2069 Colormap cmap;
2070 unsigned long *pixel;
68c45bf0 2071 double factor;
06a2c219
GM
2072 int delta;
2073{
2074 XColor color, new;
6d8b0acd 2075 long bright;
06a2c219
GM
2076 int success_p;
2077
2078 /* Get RGB color values. */
2079 color.pixel = *pixel;
f04e1297 2080 x_query_color (f, &color);
06a2c219
GM
2081
2082 /* Change RGB values by specified FACTOR. Avoid overflow! */
2083 xassert (factor >= 0);
2084 new.red = min (0xffff, factor * color.red);
2085 new.green = min (0xffff, factor * color.green);
2086 new.blue = min (0xffff, factor * color.blue);
2087
d7361edf
MB
2088 /* Calculate brightness of COLOR. */
2089 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
6d8b0acd
MB
2090
2091 /* We only boost colors that are darker than
2092 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
2093 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
2094 /* Make an additive adjustment to NEW, because it's dark enough so
2095 that scaling by FACTOR alone isn't enough. */
2096 {
2097 /* How far below the limit this color is (0 - 1, 1 being darker). */
2098 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
2099 /* The additive adjustment. */
d7361edf 2100 int min_delta = delta * dimness * factor / 2;
6d8b0acd
MB
2101
2102 if (factor < 1)
2103 {
6d8b0acd
MB
2104 new.red = max (0, new.red - min_delta);
2105 new.green = max (0, new.green - min_delta);
2106 new.blue = max (0, new.blue - min_delta);
2107 }
2108 else
2109 {
2110 new.red = min (0xffff, min_delta + new.red);
2111 new.green = min (0xffff, min_delta + new.green);
2112 new.blue = min (0xffff, min_delta + new.blue);
2113 }
2114 }
2115
06a2c219 2116 /* Try to allocate the color. */
80c32bcc 2117 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
2118 if (success_p)
2119 {
2120 if (new.pixel == *pixel)
2121 {
2122 /* If we end up with the same color as before, try adding
2123 delta to the RGB values. */
0d605c67 2124 x_free_colors (f, &new.pixel, 1);
7d0393cf 2125
06a2c219
GM
2126 new.red = min (0xffff, delta + color.red);
2127 new.green = min (0xffff, delta + color.green);
2128 new.blue = min (0xffff, delta + color.blue);
80c32bcc 2129 success_p = x_alloc_nearest_color (f, cmap, &new);
06a2c219
GM
2130 }
2131 else
2132 success_p = 1;
2133 *pixel = new.pixel;
2134 }
7d0393cf 2135
06a2c219
GM
2136 return success_p;
2137}
2138
2139
2140/* Set up the foreground color for drawing relief lines of glyph
2141 string S. RELIEF is a pointer to a struct relief containing the GC
2142 with which lines will be drawn. Use a color that is FACTOR or
2143 DELTA lighter or darker than the relief's background which is found
2144 in S->f->output_data.x->relief_background. If such a color cannot
2145 be allocated, use DEFAULT_PIXEL, instead. */
7d0393cf 2146
06a2c219
GM
2147static void
2148x_setup_relief_color (f, relief, factor, delta, default_pixel)
2149 struct frame *f;
2150 struct relief *relief;
68c45bf0 2151 double factor;
06a2c219
GM
2152 int delta;
2153 unsigned long default_pixel;
2154{
2155 XGCValues xgcv;
2156 struct x_output *di = f->output_data.x;
2157 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
2158 unsigned long pixel;
2159 unsigned long background = di->relief_background;
43bd1b2b 2160 Colormap cmap = FRAME_X_COLORMAP (f);
dcd08bfb
GM
2161 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2162 Display *dpy = FRAME_X_DISPLAY (f);
06a2c219
GM
2163
2164 xgcv.graphics_exposures = False;
2165 xgcv.line_width = 1;
2166
2167 /* Free previously allocated color. The color cell will be reused
2168 when it has been freed as many times as it was allocated, so this
2169 doesn't affect faces using the same colors. */
2170 if (relief->gc
2171 && relief->allocated_p)
2172 {
0d605c67 2173 x_free_colors (f, &relief->pixel, 1);
06a2c219
GM
2174 relief->allocated_p = 0;
2175 }
2176
2177 /* Allocate new color. */
2178 xgcv.foreground = default_pixel;
2179 pixel = background;
dcd08bfb
GM
2180 if (dpyinfo->n_planes != 1
2181 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
06a2c219
GM
2182 {
2183 relief->allocated_p = 1;
2184 xgcv.foreground = relief->pixel = pixel;
2185 }
7d0393cf 2186
06a2c219
GM
2187 if (relief->gc == 0)
2188 {
dcd08bfb 2189 xgcv.stipple = dpyinfo->gray;
06a2c219 2190 mask |= GCStipple;
dcd08bfb 2191 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
06a2c219
GM
2192 }
2193 else
dcd08bfb 2194 XChangeGC (dpy, relief->gc, mask, &xgcv);
06a2c219
GM
2195}
2196
2197
2198/* Set up colors for the relief lines around glyph string S. */
2199
2200static void
2201x_setup_relief_colors (s)
2202 struct glyph_string *s;
2203{
2204 struct x_output *di = s->f->output_data.x;
2205 unsigned long color;
2206
2207 if (s->face->use_box_color_for_shadows_p)
2208 color = s->face->box_color;
e2a57b34 2209 else if (s->first_glyph->type == IMAGE_GLYPH
0cb8bb48 2210 && s->img->pixmap
e2a57b34
MB
2211 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2212 color = IMAGE_BACKGROUND (s->img, s->f, 0);
06a2c219
GM
2213 else
2214 {
2215 XGCValues xgcv;
7d0393cf 2216
06a2c219
GM
2217 /* Get the background color of the face. */
2218 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2219 color = xgcv.background;
2220 }
2221
2222 if (di->white_relief.gc == 0
2223 || color != di->relief_background)
2224 {
2225 di->relief_background = color;
2226 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2227 WHITE_PIX_DEFAULT (s->f));
2228 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2229 BLACK_PIX_DEFAULT (s->f));
2230 }
2231}
2232
2233
2234/* Draw a relief on frame F inside the rectangle given by LEFT_X,
2235 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2236 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
2237 relief. LEFT_P non-zero means draw a relief on the left side of
2238 the rectangle. RIGHT_P non-zero means draw a relief on the right
2239 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2240 when drawing. */
2241
2242static void
2243x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
57326d99 2244 raised_p, top_p, bot_p, left_p, right_p, clip_rect)
06a2c219 2245 struct frame *f;
57326d99
KS
2246 int left_x, top_y, right_x, bottom_y, width;
2247 int top_p, bot_p, left_p, right_p, raised_p;
06a2c219
GM
2248 XRectangle *clip_rect;
2249{
de507556
GM
2250 Display *dpy = FRAME_X_DISPLAY (f);
2251 Window window = FRAME_X_WINDOW (f);
06a2c219
GM
2252 int i;
2253 GC gc;
7d0393cf 2254
06a2c219
GM
2255 if (raised_p)
2256 gc = f->output_data.x->white_relief.gc;
2257 else
2258 gc = f->output_data.x->black_relief.gc;
de507556 2259 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
06a2c219
GM
2260
2261 /* Top. */
57326d99
KS
2262 if (top_p)
2263 for (i = 0; i < width; ++i)
2264 XDrawLine (dpy, window, gc,
2265 left_x + i * left_p, top_y + i,
2266 right_x + 1 - i * right_p, top_y + i);
06a2c219
GM
2267
2268 /* Left. */
2269 if (left_p)
2270 for (i = 0; i < width; ++i)
de507556 2271 XDrawLine (dpy, window, gc,
44655e77 2272 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
06a2c219 2273
de507556 2274 XSetClipMask (dpy, gc, None);
06a2c219
GM
2275 if (raised_p)
2276 gc = f->output_data.x->black_relief.gc;
2277 else
2278 gc = f->output_data.x->white_relief.gc;
de507556 2279 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
7d0393cf 2280
06a2c219 2281 /* Bottom. */
57326d99
KS
2282 if (bot_p)
2283 for (i = 0; i < width; ++i)
2284 XDrawLine (dpy, window, gc,
2285 left_x + i * left_p, bottom_y - i,
2286 right_x + 1 - i * right_p, bottom_y - i);
7d0393cf 2287
06a2c219
GM
2288 /* Right. */
2289 if (right_p)
2290 for (i = 0; i < width; ++i)
de507556 2291 XDrawLine (dpy, window, gc,
06a2c219
GM
2292 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
2293
de507556 2294 XSetClipMask (dpy, gc, None);
06a2c219
GM
2295}
2296
2297
2298/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2299 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2300 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
2301 left side of the rectangle. RIGHT_P non-zero means draw a line
2302 on the right side of the rectangle. CLIP_RECT is the clipping
2303 rectangle to use when drawing. */
2304
2305static void
2306x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2307 left_p, right_p, clip_rect)
2308 struct glyph_string *s;
0971e730 2309 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
06a2c219
GM
2310 XRectangle *clip_rect;
2311{
2312 XGCValues xgcv;
7d0393cf 2313
06a2c219
GM
2314 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2315 XSetForeground (s->display, s->gc, s->face->box_color);
2316 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
7d0393cf 2317
06a2c219
GM
2318 /* Top. */
2319 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 2320 left_x, top_y, right_x - left_x + 1, width);
06a2c219
GM
2321
2322 /* Left. */
2323 if (left_p)
2324 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 2325 left_x, top_y, width, bottom_y - top_y + 1);
06a2c219
GM
2326
2327 /* Bottom. */
2328 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 2329 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
7d0393cf 2330
06a2c219
GM
2331 /* Right. */
2332 if (right_p)
2333 XFillRectangle (s->display, s->window, s->gc,
58e4fce8 2334 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
06a2c219
GM
2335
2336 XSetForeground (s->display, s->gc, xgcv.foreground);
2337 XSetClipMask (s->display, s->gc, None);
2338}
2339
2340
2341/* Draw a box around glyph string S. */
2342
2343static void
2344x_draw_glyph_string_box (s)
2345 struct glyph_string *s;
2346{
2347 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2348 int left_p, right_p;
2349 struct glyph *last_glyph;
2350 XRectangle clip_rect;
2351
82ead4b1
KS
2352 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2353 ? WINDOW_RIGHT_EDGE_X (s->w)
2354 : window_box_right (s->w, s->area));
7d0393cf 2355
06a2c219 2356 /* The glyph that may have a right box line. */
b4192550 2357 last_glyph = (s->cmp || s->img
06a2c219
GM
2358 ? s->first_glyph
2359 : s->first_glyph + s->nchars - 1);
2360
1ea40aa2 2361 width = eabs (s->face->box_line_width);
06a2c219
GM
2362 raised_p = s->face->box == FACE_RAISED_BOX;
2363 left_x = s->x;
57ac7c81
GM
2364 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2365 ? last_x - 1
2366 : min (last_x, s->x + s->background_width) - 1);
06a2c219
GM
2367 top_y = s->y;
2368 bottom_y = top_y + s->height - 1;
2369
2370 left_p = (s->first_glyph->left_box_line_p
2371 || (s->hl == DRAW_MOUSE_FACE
2372 && (s->prev == NULL
2373 || s->prev->hl != s->hl)));
2374 right_p = (last_glyph->right_box_line_p
2375 || (s->hl == DRAW_MOUSE_FACE
2376 && (s->next == NULL
2377 || s->next->hl != s->hl)));
327f42ee 2378
442a09ea 2379 get_glyph_string_clip_rect (s, &clip_rect);
06a2c219
GM
2380
2381 if (s->face->box == FACE_SIMPLE_BOX)
2382 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2383 left_p, right_p, &clip_rect);
2384 else
2385 {
2386 x_setup_relief_colors (s);
2387 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
57326d99 2388 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
06a2c219
GM
2389 }
2390}
2391
2392
2393/* Draw foreground of image glyph string S. */
2394
2395static void
2396x_draw_image_foreground (s)
2397 struct glyph_string *s;
2398{
57326d99
KS
2399 int x = s->x;
2400 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
06a2c219
GM
2401
2402 /* If first glyph of S has a left box line, start drawing it to the
2403 right of that line. */
2404 if (s->face->box != FACE_NO_BOX
57326d99
KS
2405 && s->first_glyph->left_box_line_p
2406 && s->slice.x == 0)
1ea40aa2 2407 x += eabs (s->face->box_line_width);
06a2c219
GM
2408
2409 /* If there is a margin around the image, adjust x- and y-position
2410 by that margin. */
57326d99
KS
2411 if (s->slice.x == 0)
2412 x += s->img->hmargin;
2413 if (s->slice.y == 0)
2414 y += s->img->vmargin;
06a2c219
GM
2415
2416 if (s->img->pixmap)
2417 {
2418 if (s->img->mask)
2419 {
2420 /* We can't set both a clip mask and use XSetClipRectangles
2421 because the latter also sets a clip mask. We also can't
2422 trust on the shape extension to be available
2423 (XShapeCombineRegion). So, compute the rectangle to draw
2424 manually. */
2425 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2426 | GCFunction);
2427 XGCValues xgcv;
2428 XRectangle clip_rect, image_rect, r;
2429
2430 xgcv.clip_mask = s->img->mask;
2431 xgcv.clip_x_origin = x;
2432 xgcv.clip_y_origin = y;
2433 xgcv.function = GXcopy;
2434 XChangeGC (s->display, s->gc, mask, &xgcv);
7d0393cf 2435
442a09ea 2436 get_glyph_string_clip_rect (s, &clip_rect);
06a2c219
GM
2437 image_rect.x = x;
2438 image_rect.y = y;
57326d99
KS
2439 image_rect.width = s->slice.width;
2440 image_rect.height = s->slice.height;
06a2c219
GM
2441 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2442 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
57326d99
KS
2443 s->slice.x + r.x - x, s->slice.y + r.y - y,
2444 r.width, r.height, r.x, r.y);
06a2c219
GM
2445 }
2446 else
2447 {
49ad1d99
GM
2448 XRectangle clip_rect, image_rect, r;
2449
442a09ea 2450 get_glyph_string_clip_rect (s, &clip_rect);
49ad1d99
GM
2451 image_rect.x = x;
2452 image_rect.y = y;
57326d99
KS
2453 image_rect.width = s->slice.width;
2454 image_rect.height = s->slice.height;
49ad1d99
GM
2455 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2456 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
57326d99
KS
2457 s->slice.x + r.x - x, s->slice.y + r.y - y,
2458 r.width, r.height, r.x, r.y);
7d0393cf 2459
06a2c219
GM
2460 /* When the image has a mask, we can expect that at
2461 least part of a mouse highlight or a block cursor will
2462 be visible. If the image doesn't have a mask, make
2463 a block cursor visible by drawing a rectangle around
2464 the image. I believe it's looking better if we do
2465 nothing here for mouse-face. */
2466 if (s->hl == DRAW_CURSOR)
534c20b2
KS
2467 {
2468 int r = s->img->relief;
2469 if (r < 0) r = -r;
57326d99
KS
2470 XDrawRectangle (s->display, s->window, s->gc,
2471 x - r, y - r,
2472 s->slice.width + r*2 - 1,
2473 s->slice.height + r*2 - 1);
534c20b2 2474 }
06a2c219
GM
2475 }
2476 }
2477 else
2478 /* Draw a rectangle if image could not be loaded. */
2479 XDrawRectangle (s->display, s->window, s->gc, x, y,
57326d99 2480 s->slice.width - 1, s->slice.height - 1);
06a2c219
GM
2481}
2482
2483
2484/* Draw a relief around the image glyph string S. */
2485
2486static void
2487x_draw_image_relief (s)
2488 struct glyph_string *s;
2489{
2490 int x0, y0, x1, y1, thick, raised_p;
2491 XRectangle r;
57326d99
KS
2492 int x = s->x;
2493 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
7d0393cf 2494
06a2c219
GM
2495 /* If first glyph of S has a left box line, start drawing it to the
2496 right of that line. */
2497 if (s->face->box != FACE_NO_BOX
57326d99
KS
2498 && s->first_glyph->left_box_line_p
2499 && s->slice.x == 0)
1ea40aa2 2500 x += eabs (s->face->box_line_width);
7d0393cf 2501
06a2c219
GM
2502 /* If there is a margin around the image, adjust x- and y-position
2503 by that margin. */
57326d99
KS
2504 if (s->slice.x == 0)
2505 x += s->img->hmargin;
2506 if (s->slice.y == 0)
2507 y += s->img->vmargin;
7d0393cf 2508
06a2c219
GM
2509 if (s->hl == DRAW_IMAGE_SUNKEN
2510 || s->hl == DRAW_IMAGE_RAISED)
2511 {
62854fe2 2512 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
06a2c219
GM
2513 raised_p = s->hl == DRAW_IMAGE_RAISED;
2514 }
2515 else
2516 {
1ea40aa2 2517 thick = eabs (s->img->relief);
06a2c219
GM
2518 raised_p = s->img->relief > 0;
2519 }
7d0393cf 2520
06a2c219
GM
2521 x0 = x - thick;
2522 y0 = y - thick;
57326d99
KS
2523 x1 = x + s->slice.width + thick - 1;
2524 y1 = y + s->slice.height + thick - 1;
7d0393cf 2525
06a2c219 2526 x_setup_relief_colors (s);
442a09ea 2527 get_glyph_string_clip_rect (s, &r);
57326d99
KS
2528 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
2529 s->slice.y == 0,
2530 s->slice.y + s->slice.height == s->img->height,
2531 s->slice.x == 0,
2532 s->slice.x + s->slice.width == s->img->width,
2533 &r);
06a2c219
GM
2534}
2535
2536
2537/* Draw the foreground of image glyph string S to PIXMAP. */
2538
2539static void
2540x_draw_image_foreground_1 (s, pixmap)
2541 struct glyph_string *s;
2542 Pixmap pixmap;
2543{
57326d99
KS
2544 int x = 0;
2545 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
06a2c219
GM
2546
2547 /* If first glyph of S has a left box line, start drawing it to the
2548 right of that line. */
2549 if (s->face->box != FACE_NO_BOX
57326d99
KS
2550 && s->first_glyph->left_box_line_p
2551 && s->slice.x == 0)
1ea40aa2 2552 x += eabs (s->face->box_line_width);
06a2c219
GM
2553
2554 /* If there is a margin around the image, adjust x- and y-position
2555 by that margin. */
57326d99
KS
2556 if (s->slice.x == 0)
2557 x += s->img->hmargin;
2558 if (s->slice.y == 0)
2559 y += s->img->vmargin;
dc43ef94 2560
06a2c219
GM
2561 if (s->img->pixmap)
2562 {
2563 if (s->img->mask)
2564 {
2565 /* We can't set both a clip mask and use XSetClipRectangles
2566 because the latter also sets a clip mask. We also can't
2567 trust on the shape extension to be available
2568 (XShapeCombineRegion). So, compute the rectangle to draw
2569 manually. */
2570 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2571 | GCFunction);
2572 XGCValues xgcv;
2573
2574 xgcv.clip_mask = s->img->mask;
95911c74
JD
2575 xgcv.clip_x_origin = x - s->slice.x;
2576 xgcv.clip_y_origin = y - s->slice.y;
06a2c219
GM
2577 xgcv.function = GXcopy;
2578 XChangeGC (s->display, s->gc, mask, &xgcv);
2579
2580 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
57326d99
KS
2581 s->slice.x, s->slice.y,
2582 s->slice.width, s->slice.height, x, y);
06a2c219
GM
2583 XSetClipMask (s->display, s->gc, None);
2584 }
2585 else
2586 {
2587 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
57326d99
KS
2588 s->slice.x, s->slice.y,
2589 s->slice.width, s->slice.height, x, y);
7d0393cf 2590
06a2c219
GM
2591 /* When the image has a mask, we can expect that at
2592 least part of a mouse highlight or a block cursor will
2593 be visible. If the image doesn't have a mask, make
2594 a block cursor visible by drawing a rectangle around
2595 the image. I believe it's looking better if we do
2596 nothing here for mouse-face. */
2597 if (s->hl == DRAW_CURSOR)
534c20b2
KS
2598 {
2599 int r = s->img->relief;
2600 if (r < 0) r = -r;
2601 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
57326d99
KS
2602 s->slice.width + r*2 - 1,
2603 s->slice.height + r*2 - 1);
534c20b2 2604 }
06a2c219
GM
2605 }
2606 }
2607 else
2608 /* Draw a rectangle if image could not be loaded. */
2609 XDrawRectangle (s->display, pixmap, s->gc, x, y,
57326d99 2610 s->slice.width - 1, s->slice.height - 1);
06a2c219 2611}
dc43ef94 2612
990ba854 2613
06a2c219
GM
2614/* Draw part of the background of glyph string S. X, Y, W, and H
2615 give the rectangle to draw. */
a9a5b0a5 2616
06a2c219
GM
2617static void
2618x_draw_glyph_string_bg_rect (s, x, y, w, h)
2619 struct glyph_string *s;
2620 int x, y, w, h;
2621{
2622 if (s->stippled_p)
2623 {
2624 /* Fill background with a stipple pattern. */
2625 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2626 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2627 XSetFillStyle (s->display, s->gc, FillSolid);
2628 }
2629 else
2630 x_clear_glyph_string_rect (s, x, y, w, h);
2631}
07e34cb0 2632
b5210ea7 2633
7d0393cf 2634/* Draw image glyph string S.
dc43ef94 2635
06a2c219
GM
2636 s->y
2637 s->x +-------------------------
2638 | s->face->box
2639 |
2640 | +-------------------------
2641 | | s->img->margin
2642 | |
2643 | | +-------------------
2644 | | | the image
dc43ef94 2645
06a2c219 2646 */
dc43ef94 2647
06a2c219
GM
2648static void
2649x_draw_image_glyph_string (s)
2650 struct glyph_string *s;
2651{
1ea40aa2 2652 int box_line_hwidth = eabs (s->face->box_line_width);
ea2ba0d4 2653 int box_line_vwidth = max (s->face->box_line_width, 0);
06a2c219
GM
2654 int height;
2655 Pixmap pixmap = None;
2656
57326d99
KS
2657 height = s->height;
2658 if (s->slice.y == 0)
2659 height -= box_line_vwidth;
2660 if (s->slice.y + s->slice.height >= s->img->height)
2661 height -= box_line_vwidth;
488dd4c4 2662
06a2c219
GM
2663 /* Fill background with face under the image. Do it only if row is
2664 taller than image or if image has a clip mask to reduce
2665 flickering. */
2666 s->stippled_p = s->face->stipple != 0;
57326d99 2667 if (height > s->slice.height
22d650b8
GM
2668 || s->img->hmargin
2669 || s->img->vmargin
06a2c219
GM
2670 || s->img->mask
2671 || s->img->pixmap == 0
2672 || s->width != s->background_width)
2673 {
06a2c219
GM
2674 if (s->img->mask)
2675 {
f9b5db02
GM
2676 /* Create a pixmap as large as the glyph string. Fill it
2677 with the background color. Copy the image to it, using
2678 its mask. Copy the temporary pixmap to the display. */
06a2c219
GM
2679 Screen *screen = FRAME_X_SCREEN (s->f);
2680 int depth = DefaultDepthOfScreen (screen);
2681
2682 /* Create a pixmap as large as the glyph string. */
2683 pixmap = XCreatePixmap (s->display, s->window,
2684 s->background_width,
2685 s->height, depth);
7d0393cf 2686
06a2c219
GM
2687 /* Don't clip in the following because we're working on the
2688 pixmap. */
2689 XSetClipMask (s->display, s->gc, None);
2690
2691 /* Fill the pixmap with the background color/stipple. */
2692 if (s->stippled_p)
2693 {
2694 /* Fill background with a stipple pattern. */
2695 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
6c28435c 2696 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
06a2c219
GM
2697 XFillRectangle (s->display, pixmap, s->gc,
2698 0, 0, s->background_width, s->height);
2699 XSetFillStyle (s->display, s->gc, FillSolid);
6c28435c 2700 XSetTSOrigin (s->display, s->gc, 0, 0);
06a2c219
GM
2701 }
2702 else
2703 {
2704 XGCValues xgcv;
2705 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2706 &xgcv);
2707 XSetForeground (s->display, s->gc, xgcv.background);
2708 XFillRectangle (s->display, pixmap, s->gc,
2709 0, 0, s->background_width, s->height);
2710 XSetForeground (s->display, s->gc, xgcv.foreground);
2711 }
2712 }
2713 else
57326d99
KS
2714 {
2715 int x = s->x;
2716 int y = s->y;
2717
2718 if (s->first_glyph->left_box_line_p
2719 && s->slice.x == 0)
2720 x += box_line_hwidth;
2721
2722 if (s->slice.y == 0)
2723 y += box_line_vwidth;
2724
2725 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2726 }
7d0393cf 2727
06a2c219
GM
2728 s->background_filled_p = 1;
2729 }
dc43ef94 2730
06a2c219
GM
2731 /* Draw the foreground. */
2732 if (pixmap != None)
2733 {
2734 x_draw_image_foreground_1 (s, pixmap);
2735 x_set_glyph_string_clipping (s);
2736 XCopyArea (s->display, pixmap, s->window, s->gc,
2737 0, 0, s->background_width, s->height, s->x, s->y);
2738 XFreePixmap (s->display, pixmap);
2739 }
2740 else
2741 x_draw_image_foreground (s);
b5210ea7 2742
06a2c219
GM
2743 /* If we must draw a relief around the image, do it. */
2744 if (s->img->relief
2745 || s->hl == DRAW_IMAGE_RAISED
2746 || s->hl == DRAW_IMAGE_SUNKEN)
2747 x_draw_image_relief (s);
2748}
8c1a6a84 2749
990ba854 2750
06a2c219 2751/* Draw stretch glyph string S. */
dc43ef94 2752
06a2c219
GM
2753static void
2754x_draw_stretch_glyph_string (s)
2755 struct glyph_string *s;
2756{
2757 xassert (s->first_glyph->type == STRETCH_GLYPH);
990ba854 2758
06a2c219
GM
2759 if (s->hl == DRAW_CURSOR
2760 && !x_stretch_cursor_p)
2761 {
2762 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2763 as wide as the stretch glyph. */
e8f6b0db
KS
2764 int width, background_width = s->background_width;
2765 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2766
2767 if (x < left_x)
2768 {
2769 background_width -= left_x - x;
2770 x = left_x;
2771 }
2772 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
990ba854 2773
06a2c219 2774 /* Draw cursor. */
e8f6b0db 2775 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
0cdd0c9f 2776
06a2c219 2777 /* Clear rest using the GC of the original non-cursor face. */
e8f6b0db 2778 if (width < background_width)
06a2c219 2779 {
e8f6b0db
KS
2780 int y = s->y;
2781 int w = background_width - width, h = s->height;
06a2c219 2782 XRectangle r;
b7f83f9e 2783 GC gc;
dc43ef94 2784
e8f6b0db 2785 x += width;
b7f83f9e
GM
2786 if (s->row->mouse_face_p
2787 && cursor_in_mouse_face_p (s->w))
2788 {
2789 x_set_mouse_face_gc (s);
2790 gc = s->gc;
2791 }
2792 else
2793 gc = s->face->gc;
7d0393cf 2794
442a09ea 2795 get_glyph_string_clip_rect (s, &r);
06a2c219 2796 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
7d0393cf 2797
06a2c219
GM
2798 if (s->face->stipple)
2799 {
2800 /* Fill background with a stipple pattern. */
2801 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2802 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2803 XSetFillStyle (s->display, gc, FillSolid);
2804 }
2805 else
2806 {
2807 XGCValues xgcv;
2808 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2809 XSetForeground (s->display, gc, xgcv.background);
2810 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2811 XSetForeground (s->display, gc, xgcv.foreground);
2812 }
2813 }
2814 }
61e9f9f3 2815 else if (!s->background_filled_p)
e8f6b0db
KS
2816 {
2817 int background_width = s->background_width;
2818 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2819
6c693929
KS
2820 /* Don't draw into left margin, fringe or scrollbar area
2821 except for header line and mode line. */
2822 if (x < left_x && !s->row->mode_line_p)
e8f6b0db
KS
2823 {
2824 background_width -= left_x - x;
2825 x = left_x;
2826 }
2827 if (background_width > 0)
2828 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2829 }
7d0393cf 2830
06a2c219
GM
2831 s->background_filled_p = 1;
2832}
2833
2834
2835/* Draw glyph string S. */
2836
2837static void
2838x_draw_glyph_string (s)
2839 struct glyph_string *s;
2840{
4458cf11
KH
2841 int relief_drawn_p = 0;
2842
1dc6c31b
KH
2843 /* If S draws into the background of its successors, draw the
2844 background of the successors first so that S can draw into it.
06a2c219 2845 This makes S->next use XDrawString instead of XDrawImageString. */
c2ded1b7 2846 if (s->next && s->right_overhang && !s->for_overlaps)
06a2c219 2847 {
1dc6c31b
KH
2848 int width;
2849 struct glyph_string *next;
2850
2851 for (width = 0, next = s->next; next;
2852 width += next->width, next = next->next)
2853 if (next->first_glyph->type != IMAGE_GLYPH)
2854 {
2855 x_set_glyph_string_gc (next);
2856 x_set_glyph_string_clipping (next);
2857 x_draw_glyph_string_background (next, 1);
c9ff09ee 2858#ifdef USE_FONT_BACKEND
fdd6e0c0 2859 next->num_clips = 0;
c9ff09ee 2860#endif /* USE_FONT_BACKEND */
1dc6c31b 2861 }
06a2c219 2862 }
97210f4e 2863
06a2c219
GM
2864 /* Set up S->gc, set clipping and draw S. */
2865 x_set_glyph_string_gc (s);
06a2c219 2866
4458cf11
KH
2867 /* Draw relief (if any) in advance for char/composition so that the
2868 glyph string can be drawn over it. */
c2ded1b7 2869 if (!s->for_overlaps
4458cf11
KH
2870 && s->face->box != FACE_NO_BOX
2871 && (s->first_glyph->type == CHAR_GLYPH
2872 || s->first_glyph->type == COMPOSITE_GLYPH))
2873
2874 {
e6269cbb 2875 x_set_glyph_string_clipping (s);
4458cf11
KH
2876 x_draw_glyph_string_background (s, 1);
2877 x_draw_glyph_string_box (s);
e6269cbb 2878 x_set_glyph_string_clipping (s);
4458cf11
KH
2879 relief_drawn_p = 1;
2880 }
1dd71645
KH
2881 else if ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2882 || (s->next && s->next->hl != s->hl && s->right_overhang))
2883 /* We must clip just this glyph. left_overhang part has already
2884 drawn when s->prev was drawn, and right_overhang part will be
2885 drawn later when s->next is drawn. */
2886 x_set_glyph_string_clipping_exactly (s, s);
e6269cbb
GM
2887 else
2888 x_set_glyph_string_clipping (s);
4458cf11 2889
06a2c219
GM
2890 switch (s->first_glyph->type)
2891 {
2892 case IMAGE_GLYPH:
2893 x_draw_image_glyph_string (s);
2894 break;
2895
2896 case STRETCH_GLYPH:
2897 x_draw_stretch_glyph_string (s);
2898 break;
2899
2900 case CHAR_GLYPH:
c2ded1b7 2901 if (s->for_overlaps)
66ac4b0e
GM
2902 s->background_filled_p = 1;
2903 else
2904 x_draw_glyph_string_background (s, 0);
06a2c219
GM
2905 x_draw_glyph_string_foreground (s);
2906 break;
2907
b4192550 2908 case COMPOSITE_GLYPH:
c2ded1b7 2909 if (s->for_overlaps || s->gidx > 0)
b4192550
KH
2910 s->background_filled_p = 1;
2911 else
2912 x_draw_glyph_string_background (s, 1);
2913 x_draw_composite_glyph_string_foreground (s);
2914 break;
2915
06a2c219
GM
2916 default:
2917 abort ();
2918 }
2919
c2ded1b7 2920 if (!s->for_overlaps)
06a2c219 2921 {
66ac4b0e
GM
2922 /* Draw underline. */
2923 if (s->face->underline_p)
2924 {
e24e84cc
GM
2925 unsigned long tem, h;
2926 int y;
06a2c219 2927
e24e84cc 2928 /* Get the underline thickness. Default is 1 pixel. */
c9ff09ee
KH
2929#ifdef USE_FONT_BACKEND
2930 if (enable_font_backend)
2931 /* In the future, we must use information of font. */
2932 h = 1;
2933 else
2934#endif /* USE_FONT_BACKEND */
66ac4b0e
GM
2935 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
2936 h = 1;
e24e84cc 2937
c9ff09ee
KH
2938#ifdef USE_FONT_BACKEND
2939 if (enable_font_backend)
c8c8ff66
KH
2940 {
2941 if (s->face->font)
2942 /* In the future, we must use information of font. */
2943 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
2944 else
2945 y = s->y + s->height - h;
2946 }
c9ff09ee
KH
2947 else
2948#endif
863153c5
MB
2949 {
2950 y = s->y + s->height - h;
2951 if (!x_underline_at_descent_line)
2952 {
2953 /* Get the underline position. This is the recommended
2954 vertical offset in pixels from the baseline to the top of
2955 the underline. This is a signed value according to the
2956 specs, and its default is
2957
2958 ROUND ((maximum descent) / 2), with
2959 ROUND(x) = floor (x + 0.5) */
2960
2961 if (x_use_underline_position_properties
2962 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
2963 y = s->ybase + (long) tem;
2964 else if (s->face->font)
2965 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
2966 }
2967 }
7d0393cf 2968
66ac4b0e 2969 if (s->face->underline_defaulted_p)
e24e84cc 2970 XFillRectangle (s->display, s->window, s->gc,
30f27523 2971 s->x, y, s->background_width, h);
66ac4b0e
GM
2972 else
2973 {
2974 XGCValues xgcv;
2975 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2976 XSetForeground (s->display, s->gc, s->face->underline_color);
e24e84cc 2977 XFillRectangle (s->display, s->window, s->gc,
30f27523 2978 s->x, y, s->background_width, h);
66ac4b0e
GM
2979 XSetForeground (s->display, s->gc, xgcv.foreground);
2980 }
dc6f92b8 2981 }
07e34cb0 2982
66ac4b0e
GM
2983 /* Draw overline. */
2984 if (s->face->overline_p)
06a2c219 2985 {
66ac4b0e
GM
2986 unsigned long dy = 0, h = 1;
2987
2988 if (s->face->overline_color_defaulted_p)
2989 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
30f27523 2990 s->background_width, h);
66ac4b0e
GM
2991 else
2992 {
2993 XGCValues xgcv;
2994 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2995 XSetForeground (s->display, s->gc, s->face->overline_color);
2996 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
30f27523 2997 s->background_width, h);
66ac4b0e
GM
2998 XSetForeground (s->display, s->gc, xgcv.foreground);
2999 }
06a2c219 3000 }
7d0393cf 3001
66ac4b0e
GM
3002 /* Draw strike-through. */
3003 if (s->face->strike_through_p)
06a2c219 3004 {
66ac4b0e
GM
3005 unsigned long h = 1;
3006 unsigned long dy = (s->height - h) / 2;
3007
3008 if (s->face->strike_through_color_defaulted_p)
3009 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3010 s->width, h);
3011 else
3012 {
3013 XGCValues xgcv;
3014 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3015 XSetForeground (s->display, s->gc, s->face->strike_through_color);
3016 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3017 s->width, h);
3018 XSetForeground (s->display, s->gc, xgcv.foreground);
3019 }
06a2c219 3020 }
7d0393cf 3021
4458cf11
KH
3022 /* Draw relief if not yet drawn. */
3023 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
66ac4b0e 3024 x_draw_glyph_string_box (s);
1dd71645 3025
1dc6c31b 3026 if (s->prev)
1dd71645 3027 {
1dc6c31b
KH
3028 struct glyph_string *prev;
3029
3030 for (prev = s->prev; prev; prev = prev->prev)
3031 if (prev->hl != s->hl
3032 && prev->x + prev->width + prev->right_overhang > s->x)
3033 {
3034 /* As prev was drawn while clipped to its own area, we
3035 must draw the right_overhang part using s->hl now. */
3036 enum draw_glyphs_face save = prev->hl;
3037
3038 prev->hl = s->hl;
3039 x_set_glyph_string_gc (prev);
3040 x_set_glyph_string_clipping_exactly (s, prev);
c9ff09ee
KH
3041 if (prev->first_glyph->type == CHAR_GLYPH)
3042 x_draw_glyph_string_foreground (prev);
3043 else
3044 x_draw_composite_glyph_string_foreground (prev);
1dc6c31b
KH
3045 XSetClipMask (prev->display, prev->gc, None);
3046 prev->hl = save;
c9ff09ee 3047#ifdef USE_FONT_BACKEND
fdd6e0c0 3048 prev->num_clips = 0;
c9ff09ee 3049#endif /* USE_FONT_BACKEND */
1dc6c31b 3050 }
1dd71645
KH
3051 }
3052
1dc6c31b 3053 if (s->next)
1dd71645 3054 {
1dc6c31b
KH
3055 struct glyph_string *next;
3056
3057 for (next = s->next; next; next = next->next)
3058 if (next->hl != s->hl
c9ff09ee 3059 && next->x - next->left_overhang < s->x + s->width)
1dc6c31b
KH
3060 {
3061 /* As next will be drawn while clipped to its own area,
3062 we must draw the left_overhang part using s->hl now. */
3063 enum draw_glyphs_face save = next->hl;
3064
3065 next->hl = s->hl;
3066 x_set_glyph_string_gc (next);
3067 x_set_glyph_string_clipping_exactly (s, next);
c9ff09ee
KH
3068 if (next->first_glyph->type == CHAR_GLYPH)
3069 x_draw_glyph_string_foreground (next);
3070 else
3071 x_draw_composite_glyph_string_foreground (next);
1dc6c31b
KH
3072 XSetClipMask (next->display, next->gc, None);
3073 next->hl = save;
c9ff09ee 3074#ifdef USE_FONT_BACKEND
fdd6e0c0 3075 next->num_clips = 0;
c9ff09ee 3076#endif /* USE_FONT_BACKEND */
1dc6c31b 3077 }
1dd71645 3078 }
66ac4b0e 3079 }
7d0393cf 3080
06a2c219
GM
3081 /* Reset clipping. */
3082 XSetClipMask (s->display, s->gc, None);
c9ff09ee 3083#ifdef USE_FONT_BACKEND
fdd6e0c0 3084 s->num_clips = 0;
c9ff09ee 3085#endif /* USE_FONT_BACKEND */
dc6f92b8 3086}
07e34cb0 3087
442a09ea 3088/* Shift display to make room for inserted glyphs. */
06a2c219 3089
442a09ea
KS
3090void
3091x_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
3092 struct frame *f;
3093 int x, y, width, height, shift_by;
06a2c219 3094{
06a2c219
GM
3095 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3096 f->output_data.x->normal_gc,
442a09ea
KS
3097 x, y, width, height,
3098 x + shift_by, y);
0cdd0c9f 3099}
0cdd0c9f 3100
06a2c219
GM
3101/* Delete N glyphs at the nominal cursor position. Not implemented
3102 for X frames. */
c83febd7
RS
3103
3104static void
385ed61f
KL
3105x_delete_glyphs (f, n)
3106 struct frame *f;
06a2c219 3107 register int n;
c83febd7 3108{
06a2c219 3109 abort ();
c83febd7
RS
3110}
3111
0cdd0c9f 3112
c5e6e06b
GM
3113/* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
3114 If they are <= 0, this is probably an error. */
3115
3116void
3117x_clear_area (dpy, window, x, y, width, height, exposures)
3118 Display *dpy;
3119 Window window;
3120 int x, y;
3121 int width, height;
3122 int exposures;
3123{
3124 xassert (width > 0 && height > 0);
3125 XClearArea (dpy, window, x, y, width, height, exposures);
3126}
3127
3128
856dd475 3129/* Clear an entire frame. */
06a2c219
GM
3130
3131static void
385ed61f 3132x_clear_frame (struct frame *f)
0cdd0c9f 3133{
06a2c219
GM
3134 /* Clearing the frame will erase any cursor, so mark them all as no
3135 longer visible. */
3136 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
3137 output_cursor.hpos = output_cursor.vpos = 0;
3138 output_cursor.x = -1;
3139
3140 /* We don't set the output cursor here because there will always
3141 follow an explicit cursor_to. */
3142 BLOCK_INPUT;
3143 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3144
3145 /* We have to clear the scroll bars, too. If we have changed
3146 colors or something like that, then they should be notified. */
3147 x_scroll_bar_clear (f);
0cdd0c9f 3148
06a2c219 3149 XFlush (FRAME_X_DISPLAY (f));
0cb35f4e 3150
06a2c219 3151 UNBLOCK_INPUT;
dc6f92b8 3152}
06a2c219
GM
3153
3154
dc6f92b8 3155\f
dbc4e1c1
JB
3156/* Invert the middle quarter of the frame for .15 sec. */
3157
06a2c219
GM
3158/* We use the select system call to do the waiting, so we have to make
3159 sure it's available. If it isn't, we just won't do visual bells. */
3160
dbc4e1c1
JB
3161#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
3162
06a2c219
GM
3163
3164/* Subtract the `struct timeval' values X and Y, storing the result in
3165 *RESULT. Return 1 if the difference is negative, otherwise 0. */
dbc4e1c1
JB
3166
3167static int
3168timeval_subtract (result, x, y)
3169 struct timeval *result, x, y;
3170{
06a2c219
GM
3171 /* Perform the carry for the later subtraction by updating y. This
3172 is safer because on some systems the tv_sec member is unsigned. */
dbc4e1c1
JB
3173 if (x.tv_usec < y.tv_usec)
3174 {
3175 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
3176 y.tv_usec -= 1000000 * nsec;
3177 y.tv_sec += nsec;
3178 }
7d0393cf 3179
dbc4e1c1
JB
3180 if (x.tv_usec - y.tv_usec > 1000000)
3181 {
3182 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
3183 y.tv_usec += 1000000 * nsec;
3184 y.tv_sec -= nsec;
3185 }
3186
06a2c219
GM
3187 /* Compute the time remaining to wait. tv_usec is certainly
3188 positive. */
dbc4e1c1
JB
3189 result->tv_sec = x.tv_sec - y.tv_sec;
3190 result->tv_usec = x.tv_usec - y.tv_usec;
3191
06a2c219
GM
3192 /* Return indication of whether the result should be considered
3193 negative. */
dbc4e1c1
JB
3194 return x.tv_sec < y.tv_sec;
3195}
dc6f92b8 3196
dfcf069d 3197void
f676886a
JB
3198XTflash (f)
3199 struct frame *f;
dc6f92b8 3200{
dbc4e1c1 3201 BLOCK_INPUT;
dc6f92b8 3202
dbc4e1c1
JB
3203 {
3204 GC gc;
dc6f92b8 3205
06a2c219
GM
3206 /* Create a GC that will use the GXxor function to flip foreground
3207 pixels into background pixels. */
dbc4e1c1
JB
3208 {
3209 XGCValues values;
dc6f92b8 3210
dbc4e1c1 3211 values.function = GXxor;
ce593f6e
KL
3212 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
3213 ^ FRAME_BACKGROUND_PIXEL (f));
58769bee 3214
334208b7 3215 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dbc4e1c1
JB
3216 GCFunction | GCForeground, &values);
3217 }
dc6f92b8 3218
dbc4e1c1 3219 {
e84e14c3 3220 /* Get the height not including a menu bar widget. */
0899d58c 3221 int height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, FRAME_LINES (f));
e84e14c3
RS
3222 /* Height of each line to flash. */
3223 int flash_height = FRAME_LINE_HEIGHT (f);
3224 /* These will be the left and right margins of the rectangles. */
3225 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
0899d58c 3226 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
e84e14c3
RS
3227
3228 int width;
3229
3230 /* Don't flash the area between a scroll bar and the frame
3231 edge it is next to. */
3232 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
3233 {
3234 case vertical_scroll_bar_left:
3235 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3236 break;
3237
3238 case vertical_scroll_bar_right:
3239 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3240 break;
06a2c219
GM
3241
3242 default:
3243 break;
e84e14c3
RS
3244 }
3245
3246 width = flash_right - flash_left;
3247
3248 /* If window is tall, flash top and bottom line. */
3249 if (height > 3 * FRAME_LINE_HEIGHT (f))
3250 {
3251 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
3252 flash_left,
3253 (FRAME_INTERNAL_BORDER_WIDTH (f)
0899d58c 3254 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
e84e14c3
RS
3255 width, flash_height);
3256 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3257 flash_left,
3258 (height - flash_height
3259 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3260 width, flash_height);
3261 }
3262 else
7d0393cf 3263 /* If it is short, flash it all. */
e84e14c3
RS
3264 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3265 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3266 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
dc6f92b8 3267
06a2c219 3268 x_flush (f);
dc6f92b8 3269
dbc4e1c1 3270 {
06a2c219 3271 struct timeval wakeup;
dc6f92b8 3272
66c30ea1 3273 EMACS_GET_TIME (wakeup);
dc6f92b8 3274
dbc4e1c1
JB
3275 /* Compute time to wait until, propagating carry from usecs. */
3276 wakeup.tv_usec += 150000;
3277 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
3278 wakeup.tv_usec %= 1000000;
3279
101922c3
GM
3280 /* Keep waiting until past the time wakeup or any input gets
3281 available. */
3282 while (! detect_input_pending ())
dbc4e1c1 3283 {
101922c3 3284 struct timeval current;
dbc4e1c1
JB
3285 struct timeval timeout;
3286
101922c3 3287 EMACS_GET_TIME (current);
dbc4e1c1 3288
101922c3
GM
3289 /* Break if result would be negative. */
3290 if (timeval_subtract (&current, wakeup, current))
dbc4e1c1
JB
3291 break;
3292
101922c3
GM
3293 /* How long `select' should wait. */
3294 timeout.tv_sec = 0;
3295 timeout.tv_usec = 10000;
3296
dbc4e1c1 3297 /* Try to wait that long--but we might wake up sooner. */
c32cdd9a 3298 select (0, NULL, NULL, NULL, &timeout);
dbc4e1c1
JB
3299 }
3300 }
58769bee 3301
e84e14c3
RS
3302 /* If window is tall, flash top and bottom line. */
3303 if (height > 3 * FRAME_LINE_HEIGHT (f))
3304 {
3305 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
06a2c219
GM
3306 flash_left,
3307 (FRAME_INTERNAL_BORDER_WIDTH (f)
0899d58c 3308 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
e84e14c3
RS
3309 width, flash_height);
3310 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3311 flash_left,
3312 (height - flash_height
3313 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3314 width, flash_height);
3315 }
3316 else
7d0393cf 3317 /* If it is short, flash it all. */
e84e14c3
RS
3318 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3319 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3320 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3321
334208b7 3322 XFreeGC (FRAME_X_DISPLAY (f), gc);
06a2c219 3323 x_flush (f);
dc6f92b8 3324 }
dbc4e1c1
JB
3325 }
3326
3327 UNBLOCK_INPUT;
dc6f92b8
JB
3328}
3329
06a2c219 3330#endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
dbc4e1c1
JB
3331
3332
dc6f92b8
JB
3333/* Make audible bell. */
3334
dfcf069d 3335void
dc6f92b8
JB
3336XTring_bell ()
3337{
b86bd3dd 3338 struct frame *f = SELECTED_FRAME ();
7d0393cf 3339
b86bd3dd
GM
3340 if (FRAME_X_DISPLAY (f))
3341 {
dbc4e1c1 3342#if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
b86bd3dd
GM
3343 if (visible_bell)
3344 XTflash (f);
3345 else
dbc4e1c1 3346#endif
b86bd3dd
GM
3347 {
3348 BLOCK_INPUT;
3349 XBell (FRAME_X_DISPLAY (f), 0);
3350 XFlush (FRAME_X_DISPLAY (f));
3351 UNBLOCK_INPUT;
3352 }
dc6f92b8
JB
3353 }
3354}
06a2c219 3355
dc6f92b8 3356\f
06a2c219
GM
3357/* Specify how many text lines, from the top of the window,
3358 should be affected by insert-lines and delete-lines operations.
3359 This, and those operations, are used only within an update
3360 that is bounded by calls to x_update_begin and x_update_end. */
dc6f92b8 3361
dfcf069d 3362static void
06a2c219
GM
3363XTset_terminal_window (n)
3364 register int n;
dc6f92b8 3365{
06a2c219 3366 /* This function intentionally left blank. */
dc6f92b8
JB
3367}
3368
06a2c219
GM
3369
3370\f
3371/***********************************************************************
3372 Line Dance
3373 ***********************************************************************/
3374
3375/* Perform an insert-lines or delete-lines operation, inserting N
3376 lines or deleting -N lines at vertical position VPOS. */
3377
dfcf069d 3378static void
385ed61f
KL
3379x_ins_del_lines (f, vpos, n)
3380 struct frame *f;
06a2c219 3381 int vpos, n;
dc6f92b8
JB
3382{
3383 abort ();
3384}
06a2c219
GM
3385
3386
3387/* Scroll part of the display as described by RUN. */
dc6f92b8 3388
dfcf069d 3389static void
06a2c219
GM
3390x_scroll_run (w, run)
3391 struct window *w;
3392 struct run *run;
dc6f92b8 3393{
06a2c219
GM
3394 struct frame *f = XFRAME (w->frame);
3395 int x, y, width, height, from_y, to_y, bottom_y;
3396
3397 /* Get frame-relative bounding box of the text display area of W,
3f332ef3
KS
3398 without mode lines. Include in this box the left and right
3399 fringe of W. */
06a2c219 3400 window_box (w, -1, &x, &y, &width, &height);
06a2c219
GM
3401
3402 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3403 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3404 bottom_y = y + height;
dc6f92b8 3405
06a2c219
GM
3406 if (to_y < from_y)
3407 {
3408 /* Scrolling up. Make sure we don't copy part of the mode
3409 line at the bottom. */
3410 if (from_y + run->height > bottom_y)
3411 height = bottom_y - from_y;
3412 else
3413 height = run->height;
3414 }
dc6f92b8 3415 else
06a2c219
GM
3416 {
3417 /* Scolling down. Make sure we don't copy over the mode line.
3418 at the bottom. */
3419 if (to_y + run->height > bottom_y)
3420 height = bottom_y - to_y;
3421 else
3422 height = run->height;
3423 }
7a13e894 3424
06a2c219 3425 BLOCK_INPUT;
7d0393cf 3426
06a2c219
GM
3427 /* Cursor off. Will be switched on again in x_update_window_end. */
3428 updated_window = w;
3429 x_clear_cursor (w);
3430
3431 XCopyArea (FRAME_X_DISPLAY (f),
3432 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3433 f->output_data.x->normal_gc,
3434 x, from_y,
3435 width, height,
3436 x, to_y);
7d0393cf 3437
06a2c219
GM
3438 UNBLOCK_INPUT;
3439}
dc6f92b8 3440
dc6f92b8 3441
06a2c219
GM
3442\f
3443/***********************************************************************
3444 Exposure Events
3445 ***********************************************************************/
7d0393cf 3446
442a09ea 3447\f
06a2c219 3448static void
442a09ea 3449frame_highlight (f)
06a2c219 3450 struct frame *f;
dc6f92b8 3451{
442a09ea
KS
3452 /* We used to only do this if Vx_no_window_manager was non-nil, but
3453 the ICCCM (section 4.1.6) says that the window's border pixmap
3454 and border pixel are window attributes which are "private to the
3455 client", so we can always change it to whatever we want. */
3456 BLOCK_INPUT;
3457 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3458 f->output_data.x->border_pixel);
3459 UNBLOCK_INPUT;
3460 x_update_cursor (f, 1);
3461}
dc6f92b8 3462
442a09ea
KS
3463static void
3464frame_unhighlight (f)
3465 struct frame *f;
3466{
3467 /* We used to only do this if Vx_no_window_manager was non-nil, but
3468 the ICCCM (section 4.1.6) says that the window's border pixmap
3469 and border pixel are window attributes which are "private to the
3470 client", so we can always change it to whatever we want. */
3471 BLOCK_INPUT;
3472 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3473 f->output_data.x->border_tile);
3474 UNBLOCK_INPUT;
3475 x_update_cursor (f, 1);
3476}
dc6f92b8 3477
442a09ea
KS
3478/* The focus has changed. Update the frames as necessary to reflect
3479 the new situation. Note that we can't change the selected frame
3480 here, because the Lisp code we are interrupting might become confused.
3481 Each event gets marked with the frame in which it occurred, so the
3482 Lisp code can tell when the switch took place by examining the events. */
06a2c219 3483
442a09ea
KS
3484static void
3485x_new_focus_frame (dpyinfo, frame)
3486 struct x_display_info *dpyinfo;
3487 struct frame *frame;
3488{
3489 struct frame *old_focus = dpyinfo->x_focus_frame;
06a2c219 3490
442a09ea 3491 if (frame != dpyinfo->x_focus_frame)
dc6f92b8 3492 {
442a09ea
KS
3493 /* Set this before calling other routines, so that they see
3494 the correct value of x_focus_frame. */
3495 dpyinfo->x_focus_frame = frame;
06a2c219 3496
442a09ea
KS
3497 if (old_focus && old_focus->auto_lower)
3498 x_lower_frame (old_focus);
06a2c219 3499
442a09ea
KS
3500#if 0
3501 selected_frame = frame;
3502 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
3503 selected_frame);
f1321dc3 3504 Fselect_window (selected_frame->selected_window, Qnil);
442a09ea
KS
3505 choose_minibuf_frame ();
3506#endif /* ! 0 */
82f053ab 3507
442a09ea
KS
3508 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3509 pending_autoraise_frame = dpyinfo->x_focus_frame;
3510 else
3511 pending_autoraise_frame = 0;
82f053ab 3512 }
dc6f92b8 3513
442a09ea
KS
3514 x_frame_rehighlight (dpyinfo);
3515}
06a2c219 3516
442a09ea
KS
3517/* Handle FocusIn and FocusOut state changes for FRAME.
3518 If FRAME has focus and there exists more than one frame, puts
6b61353c 3519 a FOCUS_IN_EVENT into *BUFP. */
cab34f50 3520
6b61353c
KH
3521static void
3522x_focus_changed (type, state, dpyinfo, frame, bufp)
cab34f50
JD
3523 int type;
3524 int state;
3525 struct x_display_info *dpyinfo;
3526 struct frame *frame;
3527 struct input_event *bufp;
cab34f50 3528{
cab34f50
JD
3529 if (type == FocusIn)
3530 {
3531 if (dpyinfo->x_focus_event_frame != frame)
3532 {
3533 x_new_focus_frame (dpyinfo, frame);
3534 dpyinfo->x_focus_event_frame = frame;
7d0393cf 3535
cab34f50
JD
3536 /* Don't stop displaying the initial startup message
3537 for a switch-frame event we don't need. */
8e50cc2d
SM
3538 if (NILP (Vterminal_frame)
3539 && CONSP (Vframe_list)
3540 && !NILP (XCDR (Vframe_list)))
cab34f50
JD
3541 {
3542 bufp->kind = FOCUS_IN_EVENT;
3543 XSETFRAME (bufp->frame_or_window, frame);
cab34f50
JD
3544 }
3545 }
3546
3547 frame->output_data.x->focus_state |= state;
3548
3549#ifdef HAVE_X_I18N
3550 if (FRAME_XIC (frame))
3551 XSetICFocus (FRAME_XIC (frame));
3552#endif
3553 }
3554 else if (type == FocusOut)
3555 {
3556 frame->output_data.x->focus_state &= ~state;
7d0393cf 3557
cab34f50
JD
3558 if (dpyinfo->x_focus_event_frame == frame)
3559 {
3560 dpyinfo->x_focus_event_frame = 0;
3561 x_new_focus_frame (dpyinfo, 0);
3562 }
3563
3564#ifdef HAVE_X_I18N
3565 if (FRAME_XIC (frame))
3566 XUnsetICFocus (FRAME_XIC (frame));
3567#endif
3568 }
cab34f50
JD
3569}
3570
3571/* The focus may have changed. Figure out if it is a real focus change,
3572 by checking both FocusIn/Out and Enter/LeaveNotify events.
3573
6b61353c 3574 Returns FOCUS_IN_EVENT event in *BUFP. */
cab34f50 3575
6b61353c
KH
3576static void
3577x_detect_focus_change (dpyinfo, event, bufp)
cab34f50
JD
3578 struct x_display_info *dpyinfo;
3579 XEvent *event;
3580 struct input_event *bufp;
cab34f50
JD
3581{
3582 struct frame *frame;
7d0393cf 3583
9d00001f 3584 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
6b61353c
KH
3585 if (! frame)
3586 return;
7d0393cf 3587
cab34f50
JD
3588 switch (event->type)
3589 {
3590 case EnterNotify:
3591 case LeaveNotify:
6b61353c
KH
3592 {
3593 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3594 int focus_state
3595 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3596
3597 if (event->xcrossing.detail != NotifyInferior
3598 && event->xcrossing.focus
3599 && ! (focus_state & FOCUS_EXPLICIT))
3600 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3601 FOCUS_IMPLICIT,
3602 dpyinfo, frame, bufp);
3603 }
cab34f50
JD
3604 break;
3605
3606 case FocusIn:
3607 case FocusOut:
6b61353c
KH
3608 x_focus_changed (event->type,
3609 (event->xfocus.detail == NotifyPointer ?
3610 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3611 dpyinfo, frame, bufp);
cab34f50 3612 break;
81c02ebe
JD
3613
3614 case ClientMessage:
3615 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
3616 {
3617 enum xembed_message msg = event->xclient.data.l[1];
3618 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
3619 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
3620 }
3621 break;
cab34f50 3622 }
cab34f50
JD
3623}
3624
3625
37c2c98b
RS
3626/* Handle an event saying the mouse has moved out of an Emacs frame. */
3627
3628void
0f941935
KH
3629x_mouse_leave (dpyinfo)
3630 struct x_display_info *dpyinfo;
37c2c98b 3631{
0f941935 3632 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
37c2c98b 3633}
6d4238f3 3634
f451eb13
JB
3635/* The focus has changed, or we have redirected a frame's focus to
3636 another frame (this happens when a frame uses a surrogate
06a2c219 3637 mini-buffer frame). Shift the highlight as appropriate.
0f941935
KH
3638
3639 The FRAME argument doesn't necessarily have anything to do with which
06a2c219 3640 frame is being highlighted or un-highlighted; we only use it to find
0f941935 3641 the appropriate X display info. */
06a2c219 3642
6d4238f3 3643static void
0f941935
KH
3644XTframe_rehighlight (frame)
3645 struct frame *frame;
6d4238f3 3646{
0f941935
KH
3647 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
3648}
6d4238f3 3649
0f941935
KH
3650static void
3651x_frame_rehighlight (dpyinfo)
3652 struct x_display_info *dpyinfo;
3653{
3654 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3655
3656 if (dpyinfo->x_focus_frame)
6d4238f3 3657 {
0f941935 3658 dpyinfo->x_highlight_frame
8e50cc2d 3659 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
0f941935
KH
3660 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3661 : dpyinfo->x_focus_frame);
3662 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
f451eb13 3663 {
0f941935
KH
3664 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
3665 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
f451eb13 3666 }
dc6f92b8 3667 }
6d4238f3 3668 else
0f941935 3669 dpyinfo->x_highlight_frame = 0;
dc6f92b8 3670
0f941935 3671 if (dpyinfo->x_highlight_frame != old_highlight)
6d4238f3
JB
3672 {
3673 if (old_highlight)
f676886a 3674 frame_unhighlight (old_highlight);
0f941935
KH
3675 if (dpyinfo->x_highlight_frame)
3676 frame_highlight (dpyinfo->x_highlight_frame);
6d4238f3 3677 }
dc6f92b8 3678}
06a2c219
GM
3679
3680
dc6f92b8 3681\f
06a2c219 3682/* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
dc6f92b8 3683
28430d3c
JB
3684/* Initialize mode_switch_bit and modifier_meaning. */
3685static void
334208b7
RS
3686x_find_modifier_meanings (dpyinfo)
3687 struct x_display_info *dpyinfo;
28430d3c 3688{
f689eb05 3689 int min_code, max_code;
28430d3c
JB
3690 KeySym *syms;
3691 int syms_per_code;
3692 XModifierKeymap *mods;
3693
334208b7
RS
3694 dpyinfo->meta_mod_mask = 0;
3695 dpyinfo->shift_lock_mask = 0;
3696 dpyinfo->alt_mod_mask = 0;
3697 dpyinfo->super_mod_mask = 0;
3698 dpyinfo->hyper_mod_mask = 0;
58769bee 3699
334208b7 3700 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
9658a521 3701
334208b7 3702 syms = XGetKeyboardMapping (dpyinfo->display,
28430d3c
JB
3703 min_code, max_code - min_code + 1,
3704 &syms_per_code);
334208b7 3705 mods = XGetModifierMapping (dpyinfo->display);
28430d3c 3706
58769bee 3707 /* Scan the modifier table to see which modifier bits the Meta and
11edeb03 3708 Alt keysyms are on. */
28430d3c 3709 {
06a2c219 3710 int row, col; /* The row and column in the modifier table. */
b90ee8b5 3711 int found_alt_or_meta;
28430d3c
JB
3712
3713 for (row = 3; row < 8; row++)
b90ee8b5
JD
3714 {
3715 found_alt_or_meta = 0;
28430d3c
JB
3716 for (col = 0; col < mods->max_keypermod; col++)
3717 {
b90ee8b5 3718 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
28430d3c 3719
af92970c
KH
3720 /* Zeroes are used for filler. Skip them. */
3721 if (code == 0)
3722 continue;
3723
28430d3c
JB
3724 /* Are any of this keycode's keysyms a meta key? */
3725 {
3726 int code_col;
3727
3728 for (code_col = 0; code_col < syms_per_code; code_col++)
3729 {
f689eb05 3730 int sym = syms[((code - min_code) * syms_per_code) + code_col];
28430d3c 3731
f689eb05 3732 switch (sym)
28430d3c 3733 {
f689eb05
JB
3734 case XK_Meta_L:
3735 case XK_Meta_R:
b90ee8b5 3736 found_alt_or_meta = 1;
334208b7 3737 dpyinfo->meta_mod_mask |= (1 << row);
28430d3c 3738 break;
f689eb05
JB
3739
3740 case XK_Alt_L:
3741 case XK_Alt_R:
b90ee8b5 3742 found_alt_or_meta = 1;
334208b7 3743 dpyinfo->alt_mod_mask |= (1 << row);
a3c44b14
RS
3744 break;
3745
3746 case XK_Hyper_L:
3747 case XK_Hyper_R:
b90ee8b5
JD
3748 if (!found_alt_or_meta)
3749 dpyinfo->hyper_mod_mask |= (1 << row);
3750 code_col = syms_per_code;
3751 col = mods->max_keypermod;
a3c44b14
RS
3752 break;
3753
3754 case XK_Super_L:
3755 case XK_Super_R:
b90ee8b5
JD
3756 if (!found_alt_or_meta)
3757 dpyinfo->super_mod_mask |= (1 << row);
3758 code_col = syms_per_code;
3759 col = mods->max_keypermod;
f689eb05 3760 break;
11edeb03
JB
3761
3762 case XK_Shift_Lock:
3763 /* Ignore this if it's not on the lock modifier. */
b90ee8b5 3764 if (!found_alt_or_meta && ((1 << row) == LockMask))
334208b7 3765 dpyinfo->shift_lock_mask = LockMask;
b90ee8b5
JD
3766 code_col = syms_per_code;
3767 col = mods->max_keypermod;
11edeb03 3768 break;
28430d3c
JB
3769 }
3770 }
3771 }
3772 }
b90ee8b5 3773 }
28430d3c
JB
3774 }
3775
f689eb05 3776 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
334208b7 3777 if (! dpyinfo->meta_mod_mask)
a3c44b14 3778 {
334208b7
RS
3779 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3780 dpyinfo->alt_mod_mask = 0;
a3c44b14 3781 }
f689eb05 3782
148c4b70
RS
3783 /* If some keys are both alt and meta,
3784 make them just meta, not alt. */
334208b7 3785 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
148c4b70 3786 {
334208b7 3787 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
148c4b70 3788 }
58769bee 3789
28430d3c 3790 XFree ((char *) syms);
f689eb05 3791 XFreeModifiermap (mods);
28430d3c
JB
3792}
3793
dfeccd2d
JB
3794/* Convert between the modifier bits X uses and the modifier bits
3795 Emacs uses. */
06a2c219 3796
e92928a7 3797unsigned int
334208b7
RS
3798x_x_to_emacs_modifiers (dpyinfo, state)
3799 struct x_display_info *dpyinfo;
dc6f92b8
JB
3800 unsigned int state;
3801{
98659da6
KG
3802 EMACS_UINT mod_meta = meta_modifier;
3803 EMACS_UINT mod_alt = alt_modifier;
3804 EMACS_UINT mod_hyper = hyper_modifier;
3805 EMACS_UINT mod_super = super_modifier;
3806 Lisp_Object tem;
7d0393cf 3807
98659da6
KG
3808 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3809 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3810 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3811 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3812 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3813 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3814 tem = Fget (Vx_super_keysym, Qmodifier_value);
3815 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
7d0393cf 3816
98659da6 3817
334208b7 3818 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
98659da6
KG
3819 | ((state & ControlMask) ? ctrl_modifier : 0)
3820 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3821 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3822 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3823 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
dc6f92b8
JB
3824}
3825
dfeccd2d 3826static unsigned int
334208b7
RS
3827x_emacs_to_x_modifiers (dpyinfo, state)
3828 struct x_display_info *dpyinfo;
dfeccd2d
JB
3829 unsigned int state;
3830{
98659da6
KG
3831 EMACS_UINT mod_meta = meta_modifier;
3832 EMACS_UINT mod_alt = alt_modifier;
3833 EMACS_UINT mod_hyper = hyper_modifier;
3834 EMACS_UINT mod_super = super_modifier;
7d0393cf 3835
98659da6 3836 Lisp_Object tem;
7d0393cf 3837
98659da6
KG
3838 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3839 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3840 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3841 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3842 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3843 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3844 tem = Fget (Vx_super_keysym, Qmodifier_value);
3845 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
7d0393cf
JB
3846
3847
98659da6
KG
3848 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3849 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3850 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3851 | ((state & shift_modifier) ? ShiftMask : 0)
3852 | ((state & ctrl_modifier) ? ControlMask : 0)
3853 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
dfeccd2d 3854}
d047c4eb
KH
3855
3856/* Convert a keysym to its name. */
3857
3858char *
3859x_get_keysym_name (keysym)
3860 KeySym keysym;
3861{
3862 char *value;
3863
3864 BLOCK_INPUT;
3865 value = XKeysymToString (keysym);
3866 UNBLOCK_INPUT;
3867
3868 return value;
3869}
06a2c219
GM
3870
3871
e4571a43
JB
3872\f
3873/* Mouse clicks and mouse movement. Rah. */
e4571a43 3874
442a09ea 3875/* Prepare a mouse-event in *RESULT for placement in the input queue.
71b8321e 3876
442a09ea
KS
3877 If the event is a button press, then note that we have grabbed
3878 the mouse. */
3879
3880static Lisp_Object
3881construct_mouse_click (result, event, f)
3882 struct input_event *result;
3883 XButtonEvent *event;
3884 struct frame *f;
71b8321e 3885{
442a09ea
KS
3886 /* Make the event type NO_EVENT; we'll change that when we decide
3887 otherwise. */
3888 result->kind = MOUSE_CLICK_EVENT;
3889 result->code = event->button - Button1;
3890 result->timestamp = event->time;
3891 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
3892 event->state)
3893 | (event->type == ButtonRelease
3894 ? up_modifier
3895 : down_modifier));
71b8321e 3896
442a09ea
KS
3897 XSETINT (result->x, event->x);
3898 XSETINT (result->y, event->y);
3899 XSETFRAME (result->frame_or_window, f);
3900 result->arg = Qnil;
3901 return Qnil;
71b8321e
GM
3902}
3903
442a09ea
KS
3904\f
3905/* Function to report a mouse movement to the mainstream Emacs code.
3906 The input handler calls this.
71b8321e 3907
442a09ea
KS
3908 We have received a mouse movement event, which is given in *event.
3909 If the mouse is over a different glyph than it was last time, tell
3910 the mainstream emacs code by setting mouse_moved. If not, ask for
3911 another motion event, so we can check again the next time it moves. */
e687d06e 3912
442a09ea
KS
3913static XMotionEvent last_mouse_motion_event;
3914static Lisp_Object last_mouse_motion_frame;
3915
bb339c57 3916static int
442a09ea
KS
3917note_mouse_movement (frame, event)
3918 FRAME_PTR frame;
3919 XMotionEvent *event;
e687d06e 3920{
442a09ea
KS
3921 last_mouse_movement_time = event->time;
3922 last_mouse_motion_event = *event;
3923 XSETFRAME (last_mouse_motion_frame, frame);
e687d06e 3924
555ffb46
RS
3925 if (!FRAME_X_OUTPUT (frame))
3926 return 0;
0c0351b5 3927
442a09ea 3928 if (event->window != FRAME_X_WINDOW (frame))
e687d06e 3929 {
442a09ea
KS
3930 frame->mouse_moved = 1;
3931 last_mouse_scroll_bar = Qnil;
3932 note_mouse_highlight (frame, -1, -1);
0a3fde9d 3933 last_mouse_glyph_frame = 0;
bb339c57 3934 return 1;
442a09ea
KS
3935 }
3936
a4b0e228 3937
442a09ea 3938 /* Has the mouse moved off the glyph it was on at the last sighting? */
0a3fde9d
KS
3939 if (frame != last_mouse_glyph_frame
3940 || event->x < last_mouse_glyph.x
a4b0e228
JD
3941 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
3942 || event->y < last_mouse_glyph.y
3943 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
442a09ea
KS
3944 {
3945 frame->mouse_moved = 1;
3946 last_mouse_scroll_bar = Qnil;
3947 note_mouse_highlight (frame, event->x, event->y);
9b909870 3948 /* Remember which glyph we're now on. */
cc9e7d91 3949 remember_mouse_glyph (frame, event->x, event->y, &last_mouse_glyph);
0a3fde9d 3950 last_mouse_glyph_frame = frame;
bb339c57 3951 return 1;
e687d06e 3952 }
bb339c57
KS
3953
3954 return 0;
e687d06e 3955}
b52b65bd 3956
b8009dd1 3957\f
442a09ea
KS
3958/************************************************************************
3959 Mouse Face
3960 ************************************************************************/
3961
3962static void
3963redo_mouse_highlight ()
3964{
3965 if (!NILP (last_mouse_motion_frame)
3966 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3967 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3968 last_mouse_motion_event.x,
3969 last_mouse_motion_event.y);
3970}
3971
3972
12ba150f 3973
90e65f07 3974/* Return the current position of the mouse.
b52b65bd 3975 *FP should be a frame which indicates which display to ask about.
90e65f07 3976
b52b65bd
GM
3977 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3978 and *PART to the frame, window, and scroll bar part that the mouse
3979 is over. Set *X and *Y to the portion and whole of the mouse's
ab648270 3980 position on the scroll bar.
12ba150f 3981
b52b65bd
GM
3982 If the mouse movement started elsewhere, set *FP to the frame the
3983 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
12ba150f
JB
3984 the mouse is over.
3985
b52b65bd 3986 Set *TIME to the server time-stamp for the time at which the mouse
12ba150f
JB
3987 was at this position.
3988
a135645a
RS
3989 Don't store anything if we don't have a valid set of values to report.
3990
90e65f07 3991 This clears the mouse_moved flag, so we can wait for the next mouse
f5bb65ec 3992 movement. */
90e65f07
JB
3993
3994static void
1cf412ec 3995XTmouse_position (fp, insist, bar_window, part, x, y, time)
334208b7 3996 FRAME_PTR *fp;
1cf412ec 3997 int insist;
12ba150f 3998 Lisp_Object *bar_window;
ab648270 3999 enum scroll_bar_part *part;
90e65f07 4000 Lisp_Object *x, *y;
e5d77022 4001 unsigned long *time;
90e65f07 4002{
a135645a
RS
4003 FRAME_PTR f1;
4004
90e65f07
JB
4005 BLOCK_INPUT;
4006
8bcee03e 4007 if (! NILP (last_mouse_scroll_bar) && insist == 0)
334208b7 4008 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
90e65f07
JB
4009 else
4010 {
12ba150f
JB
4011 Window root;
4012 int root_x, root_y;
90e65f07 4013
12ba150f
JB
4014 Window dummy_window;
4015 int dummy;
4016
39d8bb4d
KH
4017 Lisp_Object frame, tail;
4018
4019 /* Clear the mouse-moved flag for every frame on this display. */
4020 FOR_EACH_FRAME (tail, frame)
daf01701
KL
4021 if (FRAME_X_P (XFRAME (frame))
4022 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
39d8bb4d
KH
4023 XFRAME (frame)->mouse_moved = 0;
4024
ab648270 4025 last_mouse_scroll_bar = Qnil;
12ba150f
JB
4026
4027 /* Figure out which root window we're on. */
334208b7
RS
4028 XQueryPointer (FRAME_X_DISPLAY (*fp),
4029 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
12ba150f
JB
4030
4031 /* The root window which contains the pointer. */
4032 &root,
4033
4034 /* Trash which we can't trust if the pointer is on
4035 a different screen. */
4036 &dummy_window,
4037
4038 /* The position on that root window. */
58769bee 4039 &root_x, &root_y,
12ba150f
JB
4040
4041 /* More trash we can't trust. */
4042 &dummy, &dummy,
4043
4044 /* Modifier keys and pointer buttons, about which
4045 we don't care. */
4046 (unsigned int *) &dummy);
4047
4048 /* Now we have a position on the root; find the innermost window
4049 containing the pointer. */
4050 {
4051 Window win, child;
4052 int win_x, win_y;
06a2c219 4053 int parent_x = 0, parent_y = 0;
12ba150f
JB
4054
4055 win = root;
69388238 4056
2d7fc7e8
RS
4057 /* XTranslateCoordinates can get errors if the window
4058 structure is changing at the same time this function
4059 is running. So at least we must not crash from them. */
4060
9ba8e10d 4061 x_catch_errors (FRAME_X_DISPLAY (*fp));
2d7fc7e8 4062
334208b7 4063 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
23faf38f 4064 && FRAME_LIVE_P (last_mouse_frame))
12ba150f 4065 {
69388238
RS
4066 /* If mouse was grabbed on a frame, give coords for that frame
4067 even if the mouse is now outside it. */
334208b7 4068 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
69388238 4069
12ba150f 4070 /* From-window, to-window. */
69388238 4071 root, FRAME_X_WINDOW (last_mouse_frame),
12ba150f
JB
4072
4073 /* From-position, to-position. */
4074 root_x, root_y, &win_x, &win_y,
4075
4076 /* Child of win. */
4077 &child);
69388238
RS
4078 f1 = last_mouse_frame;
4079 }
4080 else
4081 {
4082 while (1)
4083 {
334208b7 4084 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
12ba150f 4085
69388238
RS
4086 /* From-window, to-window. */
4087 root, win,
12ba150f 4088
69388238
RS
4089 /* From-position, to-position. */
4090 root_x, root_y, &win_x, &win_y,
4091
4092 /* Child of win. */
4093 &child);
4094
9af3143a 4095 if (child == None || child == win)
69388238
RS
4096 break;
4097
4098 win = child;
4099 parent_x = win_x;
4100 parent_y = win_y;
4101 }
12ba150f 4102
69388238
RS
4103 /* Now we know that:
4104 win is the innermost window containing the pointer
4105 (XTC says it has no child containing the pointer),
4106 win_x and win_y are the pointer's position in it
4107 (XTC did this the last time through), and
4108 parent_x and parent_y are the pointer's position in win's parent.
4109 (They are what win_x and win_y were when win was child.
4110 If win is the root window, it has no parent, and
4111 parent_{x,y} are invalid, but that's okay, because we'll
4112 never use them in that case.) */
4113
4114 /* Is win one of our frames? */
19126e11 4115 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
aad0f6ab
GM
4116
4117#ifdef USE_X_TOOLKIT
4118 /* If we end up with the menu bar window, say it's not
4119 on the frame. */
4120 if (f1 != NULL
4121 && f1->output_data.x->menubar_widget
4122 && win == XtWindow (f1->output_data.x->menubar_widget))
4123 f1 = NULL;
4124#endif /* USE_X_TOOLKIT */
69388238 4125 }
58769bee 4126
2d7fc7e8
RS
4127 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
4128 f1 = 0;
4129
4545fa20 4130 x_uncatch_errors ();
2d7fc7e8 4131
ab648270 4132 /* If not, is it one of our scroll bars? */
a135645a 4133 if (! f1)
12ba150f 4134 {
6b61353c
KH
4135 struct scroll_bar *bar;
4136
4137 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win);
12ba150f
JB
4138
4139 if (bar)
4140 {
a135645a 4141 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
4142 win_x = parent_x;
4143 win_y = parent_y;
4144 }
4145 }
90e65f07 4146
8bcee03e 4147 if (f1 == 0 && insist > 0)
b86bd3dd 4148 f1 = SELECTED_FRAME ();
1cf412ec 4149
a135645a 4150 if (f1)
12ba150f 4151 {
06a2c219
GM
4152 /* Ok, we found a frame. Store all the values.
4153 last_mouse_glyph is a rectangle used to reduce the
4154 generation of mouse events. To not miss any motion
4155 events, we must divide the frame into rectangles of the
4156 size of the smallest character that could be displayed
4157 on it, i.e. into the same rectangles that matrices on
4158 the frame are divided into. */
4159
cc9e7d91 4160 remember_mouse_glyph (f1, win_x, win_y, &last_mouse_glyph);
0a3fde9d 4161 last_mouse_glyph_frame = f1;
12ba150f
JB
4162
4163 *bar_window = Qnil;
4164 *part = 0;
334208b7 4165 *fp = f1;
e0c1aef2
KH
4166 XSETINT (*x, win_x);
4167 XSETINT (*y, win_y);
12ba150f
JB
4168 *time = last_mouse_movement_time;
4169 }
4170 }
4171 }
90e65f07
JB
4172
4173 UNBLOCK_INPUT;
4174}
f451eb13 4175
06a2c219 4176
06a2c219 4177\f
442a09ea
KS
4178/***********************************************************************
4179 Scroll bars
4180 ***********************************************************************/
4181
06a2c219
GM
4182/* Scroll bar support. */
4183
6b61353c
KH
4184/* Given an X window ID and a DISPLAY, find the struct scroll_bar which
4185 manages it.
06a2c219
GM
4186 This can be called in GC, so we have to make sure to strip off mark
4187 bits. */
bffcfca9 4188
06a2c219 4189static struct scroll_bar *
6b61353c
KH
4190x_window_to_scroll_bar (display, window_id)
4191 Display *display;
06a2c219
GM
4192 Window window_id;
4193{
4194 Lisp_Object tail;
4195
a11e1dce 4196#if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
6b61353c 4197 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
a11e1dce 4198#endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
257f40f2 4199
8e50cc2d 4200 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
06a2c219
GM
4201 {
4202 Lisp_Object frame, bar, condemned;
4203
8e713be6 4204 frame = XCAR (tail);
06a2c219 4205 /* All elements of Vframe_list should be frames. */
8e50cc2d 4206 if (! FRAMEP (frame))
06a2c219
GM
4207 abort ();
4208
daf01701
KL
4209 if (! FRAME_X_P (XFRAME (frame)))
4210 continue;
4211
06a2c219
GM
4212 /* Scan this frame's scroll bar list for a scroll bar with the
4213 right window ID. */
4214 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
4215 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
4216 /* This trick allows us to search both the ordinary and
4217 condemned scroll bar lists with one loop. */
8e50cc2d 4218 ! NILP (bar) || (bar = condemned,
06a2c219 4219 condemned = Qnil,
8e50cc2d 4220 ! NILP (bar));
06a2c219 4221 bar = XSCROLL_BAR (bar)->next)
056ce195 4222 if (XSCROLL_BAR (bar)->x_window == window_id &&
6b61353c 4223 FRAME_X_DISPLAY (XFRAME (frame)) == display)
06a2c219
GM
4224 return XSCROLL_BAR (bar);
4225 }
4226
4227 return 0;
4228}
4229
4230
01f67d2c 4231#if defined USE_LUCID
c95fc5f1
GM
4232
4233/* Return the Lucid menu bar WINDOW is part of. Return null
4234 if WINDOW is not part of a menu bar. */
4235
4236static Widget
4237x_window_to_menu_bar (window)
4238 Window window;
4239{
4240 Lisp_Object tail;
7d0393cf 4241
8e50cc2d 4242 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
c95fc5f1 4243 {
daf01701
KL
4244 if (FRAME_X_P (XFRAME (XCAR (tail))))
4245 {
4246 Lisp_Object frame = XCAR (tail);
4247 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
7d0393cf 4248
daf01701
KL
4249 if (menu_bar && xlwmenu_window_p (menu_bar, window))
4250 return menu_bar;
4251 }
c95fc5f1
GM
4252 }
4253
4254 return NULL;
4255}
4256
01f67d2c 4257#endif /* USE_LUCID */
c95fc5f1 4258
06a2c219
GM
4259\f
4260/************************************************************************
4261 Toolkit scroll bars
4262 ************************************************************************/
4263
eccc05db 4264#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
4265
4266static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
4267static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
4268static void x_create_toolkit_scroll_bar P_ ((struct frame *,
4269 struct scroll_bar *));
4270static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
4271 int, int, int));
4272
4273
06a2c219
GM
4274/* Lisp window being scrolled. Set when starting to interact with
4275 a toolkit scroll bar, reset to nil when ending the interaction. */
4276
4277static Lisp_Object window_being_scrolled;
4278
4279/* Last scroll bar part sent in xm_scroll_callback. */
4280
4281static int last_scroll_bar_part;
4282
ec18280f
SM
4283/* Whether this is an Xaw with arrow-scrollbars. This should imply
4284 that movements of 1/20 of the screen size are mapped to up/down. */
4285
488dd4c4
JD
4286#ifndef USE_GTK
4287/* Id of action hook installed for scroll bars. */
4288
4289static XtActionHookId action_hook_id;
4290
ec18280f
SM
4291static Boolean xaw3d_arrow_scroll;
4292
4293/* Whether the drag scrolling maintains the mouse at the top of the
4294 thumb. If not, resizing the thumb needs to be done more carefully
4295 to avoid jerkyness. */
4296
4297static Boolean xaw3d_pick_top;
4298
06a2c219 4299/* Action hook installed via XtAppAddActionHook when toolkit scroll
ec18280f 4300 bars are used.. The hook is responsible for detecting when
06a2c219 4301 the user ends an interaction with the scroll bar, and generates
3b8f9651 4302 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
06a2c219
GM
4303
4304static void
4305xt_action_hook (widget, client_data, action_name, event, params,
4306 num_params)
4307 Widget widget;
4308 XtPointer client_data;
4309 String action_name;
4310 XEvent *event;
4311 String *params;
4312 Cardinal *num_params;
4313{
4314 int scroll_bar_p;
4315 char *end_action;
7d0393cf 4316
06a2c219
GM
4317#ifdef USE_MOTIF
4318 scroll_bar_p = XmIsScrollBar (widget);
4319 end_action = "Release";
ec18280f 4320#else /* !USE_MOTIF i.e. use Xaw */
06a2c219
GM
4321 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4322 end_action = "EndScroll";
ec18280f 4323#endif /* USE_MOTIF */
06a2c219 4324
06a2c219
GM
4325 if (scroll_bar_p
4326 && strcmp (action_name, end_action) == 0
4327 && WINDOWP (window_being_scrolled))
4328 {
4329 struct window *w;
7d0393cf 4330
06a2c219
GM
4331 x_send_scroll_bar_event (window_being_scrolled,
4332 scroll_bar_end_scroll, 0, 0);
4333 w = XWINDOW (window_being_scrolled);
62fe13a4 4334
3c671a56 4335 if (!NILP (XSCROLL_BAR (w->vertical_scroll_bar)->dragging))
95a39dc5
SM
4336 {
4337 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
4338 /* The thumb size is incorrect while dragging: fix it. */
4339 set_vertical_scroll_bar (w);
4340 }
06a2c219
GM
4341 window_being_scrolled = Qnil;
4342 last_scroll_bar_part = -1;
bffcfca9
GM
4343
4344 /* Xt timeouts no longer needed. */
4345 toolkit_scroll_bar_interaction = 0;
06a2c219
GM
4346 }
4347}
488dd4c4 4348#endif /* not USE_GTK */
06a2c219 4349
07b3d16e
GM
4350/* A vector of windows used for communication between
4351 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
4352
4353static struct window **scroll_bar_windows;
4354static int scroll_bar_windows_size;
4355
06a2c219
GM
4356
4357/* Send a client message with message type Xatom_Scrollbar for a
4358 scroll action to the frame of WINDOW. PART is a value identifying
4359 the part of the scroll bar that was clicked on. PORTION is the
4360 amount to scroll of a whole of WHOLE. */
4361
4362static void
4363x_send_scroll_bar_event (window, part, portion, whole)
4364 Lisp_Object window;
4365 int part, portion, whole;
4366{
4367 XEvent event;
4368 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
07b3d16e
GM
4369 struct window *w = XWINDOW (window);
4370 struct frame *f = XFRAME (w->frame);
4371 int i;
06a2c219 4372
07b3d16e 4373 BLOCK_INPUT;
7d0393cf 4374
06a2c219
GM
4375 /* Construct a ClientMessage event to send to the frame. */
4376 ev->type = ClientMessage;
4377 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
4378 ev->display = FRAME_X_DISPLAY (f);
4379 ev->window = FRAME_X_WINDOW (f);
4380 ev->format = 32;
07b3d16e
GM
4381
4382 /* We can only transfer 32 bits in the XClientMessageEvent, which is
4383 not enough to store a pointer or Lisp_Object on a 64 bit system.
4384 So, store the window in scroll_bar_windows and pass the index
4385 into that array in the event. */
4386 for (i = 0; i < scroll_bar_windows_size; ++i)
4387 if (scroll_bar_windows[i] == NULL)
4388 break;
4389
4390 if (i == scroll_bar_windows_size)
4391 {
4392 int new_size = max (10, 2 * scroll_bar_windows_size);
4393 size_t nbytes = new_size * sizeof *scroll_bar_windows;
4394 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
7d0393cf 4395
07b3d16e
GM
4396 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
4397 nbytes);
4398 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
4399 scroll_bar_windows_size = new_size;
4400 }
4401
4402 scroll_bar_windows[i] = w;
4403 ev->data.l[0] = (long) i;
06a2c219
GM
4404 ev->data.l[1] = (long) part;
4405 ev->data.l[2] = (long) 0;
4406 ev->data.l[3] = (long) portion;
4407 ev->data.l[4] = (long) whole;
4408
bffcfca9
GM
4409 /* Make Xt timeouts work while the scroll bar is active. */
4410 toolkit_scroll_bar_interaction = 1;
98a20c65
CY
4411#ifdef USE_X_TOOLKIT
4412 x_activate_timeout_atimer ();
4413#endif
bffcfca9 4414
06a2c219
GM
4415 /* Setting the event mask to zero means that the message will
4416 be sent to the client that created the window, and if that
4417 window no longer exists, no event will be sent. */
06a2c219
GM
4418 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4419 UNBLOCK_INPUT;
4420}
4421
4422
4423/* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4424 in *IEVENT. */
4425
4426static void
4427x_scroll_bar_to_input_event (event, ievent)
4428 XEvent *event;
4429 struct input_event *ievent;
4430{
4431 XClientMessageEvent *ev = (XClientMessageEvent *) event;
52e386c2
KR
4432 Lisp_Object window;
4433 struct frame *f;
07b3d16e 4434 struct window *w;
7d0393cf 4435
07b3d16e
GM
4436 w = scroll_bar_windows[ev->data.l[0]];
4437 scroll_bar_windows[ev->data.l[0]] = NULL;
52e386c2 4438
07b3d16e
GM
4439 XSETWINDOW (window, w);
4440 f = XFRAME (w->frame);
7d0393cf 4441
3b8f9651 4442 ievent->kind = SCROLL_BAR_CLICK_EVENT;
06a2c219 4443 ievent->frame_or_window = window;
0f8aabe9 4444 ievent->arg = Qnil;
488dd4c4
JD
4445#ifdef USE_GTK
4446 ievent->timestamp = CurrentTime;
4447#else
06a2c219 4448 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
488dd4c4 4449#endif
06a2c219
GM
4450 ievent->part = ev->data.l[1];
4451 ievent->code = ev->data.l[2];
4452 ievent->x = make_number ((int) ev->data.l[3]);
4453 ievent->y = make_number ((int) ev->data.l[4]);
4454 ievent->modifiers = 0;
4455}
4456
4457
4458#ifdef USE_MOTIF
4459
4460/* Minimum and maximum values used for Motif scroll bars. */
4461
06a2c219 4462#define XM_SB_MAX 10000000
06a2c219
GM
4463
4464
4465/* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4466 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4696802b 4467 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
06a2c219
GM
4468
4469static void
4470xm_scroll_callback (widget, client_data, call_data)
4471 Widget widget;
4472 XtPointer client_data, call_data;
4473{
4474 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4475 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
06a2c219
GM
4476 int part = -1, whole = 0, portion = 0;
4477
4478 switch (cs->reason)
4479 {
4480 case XmCR_DECREMENT:
4481 bar->dragging = Qnil;
4482 part = scroll_bar_up_arrow;
4483 break;
4484
4485 case XmCR_INCREMENT:
4486 bar->dragging = Qnil;
4487 part = scroll_bar_down_arrow;
4488 break;
4489
4490 case XmCR_PAGE_DECREMENT:
4491 bar->dragging = Qnil;
4492 part = scroll_bar_above_handle;
4493 break;
4494
4495 case XmCR_PAGE_INCREMENT:
4496 bar->dragging = Qnil;
4497 part = scroll_bar_below_handle;
4498 break;
4499
4500 case XmCR_TO_TOP:
4501 bar->dragging = Qnil;
4502 part = scroll_bar_to_top;
4503 break;
7d0393cf 4504
06a2c219
GM
4505 case XmCR_TO_BOTTOM:
4506 bar->dragging = Qnil;
4507 part = scroll_bar_to_bottom;
4508 break;
4509
4510 case XmCR_DRAG:
4511 {
4512 int slider_size;
06a2c219
GM
4513
4514 /* Get the slider size. */
4515 BLOCK_INPUT;
4516 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4517 UNBLOCK_INPUT;
4518
baa21c48 4519 whole = XM_SB_MAX - slider_size;
3c671a56 4520 portion = min (cs->value, whole);
6f2a7a68
SM
4521 part = scroll_bar_handle;
4522 bar->dragging = make_number (cs->value);
06a2c219
GM
4523 }
4524 break;
7d0393cf 4525
06a2c219
GM
4526 case XmCR_VALUE_CHANGED:
4527 break;
4528 };
4529
4530 if (part >= 0)
4531 {
4532 window_being_scrolled = bar->window;
4533 last_scroll_bar_part = part;
4534 x_send_scroll_bar_event (bar->window, part, portion, whole);
4535 }
4536}
4537
056ce195 4538#elif defined USE_GTK
06a2c219 4539
17097258 4540/* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
f979dc05 4541 bar widget. DATA is a pointer to the scroll_bar structure. */
488dd4c4
JD
4542
4543static void
4544xg_scroll_callback (widget, data)
f979dc05 4545 GtkRange *widget;
488dd4c4
JD
4546 gpointer data;
4547{
4548 struct scroll_bar *bar = (struct scroll_bar *) data;
4549 gdouble previous;
4550 gdouble position;
4551 gdouble *p;
4552 int diff;
177c0ea7 4553
488dd4c4 4554 int part = -1, whole = 0, portion = 0;
f979dc05 4555 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (widget));
177c0ea7 4556
488dd4c4
JD
4557 position = gtk_adjustment_get_value (adj);
4558
4559 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
4560 if (! p)
4561 {
4562 p = (gdouble*) xmalloc (sizeof (gdouble));
4563 *p = XG_SB_MIN;
4564 g_object_set_data (G_OBJECT (widget), XG_LAST_SB_DATA, p);
4565 }
4566
4567 previous = *p;
4568 *p = position;
4569
fecad3f6
JD
4570 if (xg_ignore_gtk_scrollbar) return;
4571
488dd4c4 4572 diff = (int) (position - previous);
177c0ea7 4573
488dd4c4
JD
4574 if (diff == (int) adj->step_increment)
4575 {
4576 part = scroll_bar_down_arrow;
4577 bar->dragging = Qnil;
4578 }
4579 else if (-diff == (int) adj->step_increment)
4580 {
4581 part = scroll_bar_up_arrow;
4582 bar->dragging = Qnil;
4583 }
4584 else if (diff == (int) adj->page_increment)
4585 {
4586 part = scroll_bar_below_handle;
4587 bar->dragging = Qnil;
4588 }
4589 else if (-diff == (int) adj->page_increment)
4590 {
4591 part = scroll_bar_above_handle;
4592 bar->dragging = Qnil;
4593 }
4594 else
4595 {
4596 part = scroll_bar_handle;
4597 whole = adj->upper - adj->page_size;
17097258
JD
4598 portion = min ((int)position, whole);
4599 bar->dragging = make_number ((int)portion);
488dd4c4 4600 }
177c0ea7 4601
488dd4c4 4602 if (part >= 0)
82ead4b1 4603 {
488dd4c4
JD
4604 window_being_scrolled = bar->window;
4605 last_scroll_bar_part = part;
4606 x_send_scroll_bar_event (bar->window, part, portion, whole);
4607 }
4608}
06a2c219 4609
056ce195 4610#else /* not USE_GTK and not USE_MOTIF */
06a2c219 4611
ec18280f 4612/* Xaw scroll bar callback. Invoked when the thumb is dragged.
06a2c219
GM
4613 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4614 scroll bar struct. CALL_DATA is a pointer to a float saying where
4615 the thumb is. */
4616
4617static void
ec18280f 4618xaw_jump_callback (widget, client_data, call_data)
06a2c219
GM
4619 Widget widget;
4620 XtPointer client_data, call_data;
4621{
4622 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4623 float top = *(float *) call_data;
4624 float shown;
ec18280f
SM
4625 int whole, portion, height;
4626 int part;
06a2c219
GM
4627
4628 /* Get the size of the thumb, a value between 0 and 1. */
4629 BLOCK_INPUT;
ec18280f 4630 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
06a2c219
GM
4631 UNBLOCK_INPUT;
4632
4633 whole = 10000000;
4634 portion = shown < 1 ? top * whole : 0;
06a2c219 4635
1ea40aa2 4636 if (shown < 1 && (eabs (top + shown - 1) < 1.0/height))
ec18280f
SM
4637 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4638 the bottom, so we force the scrolling whenever we see that we're
4639 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4640 we try to ensure that we always stay two pixels away from the
4641 bottom). */
06a2c219
GM
4642 part = scroll_bar_down_arrow;
4643 else
4644 part = scroll_bar_handle;
4645
4646 window_being_scrolled = bar->window;
4647 bar->dragging = make_number (portion);
4648 last_scroll_bar_part = part;
4649 x_send_scroll_bar_event (bar->window, part, portion, whole);
4650}
4651
4652
ec18280f
SM
4653/* Xaw scroll bar callback. Invoked for incremental scrolling.,
4654 i.e. line or page up or down. WIDGET is the Xaw scroll bar
06a2c219
GM
4655 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4656 the scroll bar. CALL_DATA is an integer specifying the action that
dbd4b028 4657 has taken place. Its magnitude is in the range 0..height of the
06a2c219
GM
4658 scroll bar. Negative values mean scroll towards buffer start.
4659 Values < height of scroll bar mean line-wise movement. */
4660
4661static void
ec18280f 4662xaw_scroll_callback (widget, client_data, call_data)
06a2c219
GM
4663 Widget widget;
4664 XtPointer client_data, call_data;
4665{
4666 struct scroll_bar *bar = (struct scroll_bar *) client_data;
560d7ceb
DL
4667 /* The position really is stored cast to a pointer. */
4668 int position = (long) call_data;
06a2c219
GM
4669 Dimension height;
4670 int part;
4671
4672 /* Get the height of the scroll bar. */
4673 BLOCK_INPUT;
4674 XtVaGetValues (widget, XtNheight, &height, NULL);
4675 UNBLOCK_INPUT;
4676
1ea40aa2 4677 if (eabs (position) >= height)
ec18280f
SM
4678 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4679
4680 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4681 it maps line-movement to call_data = max(5, height/20). */
1ea40aa2 4682 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
ec18280f 4683 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
06a2c219 4684 else
ec18280f 4685 part = scroll_bar_move_ratio;
06a2c219
GM
4686
4687 window_being_scrolled = bar->window;
4688 bar->dragging = Qnil;
4689 last_scroll_bar_part = part;
ec18280f 4690 x_send_scroll_bar_event (bar->window, part, position, height);
06a2c219
GM
4691}
4692
056ce195 4693#endif /* not USE_GTK and not USE_MOTIF */
06a2c219 4694
488dd4c4 4695#define SCROLL_BAR_NAME "verticalScrollBar"
06a2c219
GM
4696
4697/* Create the widget for scroll bar BAR on frame F. Record the widget
4698 and X window of the scroll bar in BAR. */
4699
488dd4c4
JD
4700#ifdef USE_GTK
4701static void
4702x_create_toolkit_scroll_bar (f, bar)
4703 struct frame *f;
4704 struct scroll_bar *bar;
4705{
4706 char *scroll_bar_name = SCROLL_BAR_NAME;
4707
4708 BLOCK_INPUT;
4709 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4710 scroll_bar_name);
4711 UNBLOCK_INPUT;
4712}
4713
4714#else /* not USE_GTK */
4715
06a2c219
GM
4716static void
4717x_create_toolkit_scroll_bar (f, bar)
4718 struct frame *f;
4719 struct scroll_bar *bar;
4720{
4721 Window xwindow;
4722 Widget widget;
4723 Arg av[20];
4724 int ac = 0;
488dd4c4 4725 char *scroll_bar_name = SCROLL_BAR_NAME;
06a2c219
GM
4726 unsigned long pixel;
4727
4728 BLOCK_INPUT;
4729
4730#ifdef USE_MOTIF
06a2c219
GM
4731 /* Set resources. Create the widget. */
4732 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
3c671a56 4733 XtSetArg (av[ac], XmNminimum, 0); ++ac;
06a2c219
GM
4734 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4735 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4736 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4737 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4738 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4739
4740 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4741 if (pixel != -1)
4742 {
4743 XtSetArg (av[ac], XmNforeground, pixel);
4744 ++ac;
4745 }
7d0393cf 4746
06a2c219
GM
4747 pixel = f->output_data.x->scroll_bar_background_pixel;
4748 if (pixel != -1)
4749 {
4750 XtSetArg (av[ac], XmNbackground, pixel);
4751 ++ac;
4752 }
7d0393cf 4753
06a2c219
GM
4754 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4755 scroll_bar_name, av, ac);
4756
4757 /* Add one callback for everything that can happen. */
4758 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4759 (XtPointer) bar);
4760 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4761 (XtPointer) bar);
4762 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4763 (XtPointer) bar);
4764 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4765 (XtPointer) bar);
4766 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4767 (XtPointer) bar);
4768 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4769 (XtPointer) bar);
4770 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4771 (XtPointer) bar);
7d0393cf 4772
06a2c219
GM
4773 /* Realize the widget. Only after that is the X window created. */
4774 XtRealizeWidget (widget);
4775
4776 /* Set the cursor to an arrow. I didn't find a resource to do that.
4777 And I'm wondering why it hasn't an arrow cursor by default. */
4778 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4779 f->output_data.x->nontext_cursor);
7d0393cf 4780
ec18280f 4781#else /* !USE_MOTIF i.e. use Xaw */
7d0393cf 4782
06a2c219
GM
4783 /* Set resources. Create the widget. The background of the
4784 Xaw3d scroll bar widget is a little bit light for my taste.
4785 We don't alter it here to let users change it according
4786 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4787 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4788 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
ec18280f
SM
4789 /* For smoother scrolling with Xaw3d -sm */
4790 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
7d0393cf 4791
06a2c219
GM
4792 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4793 if (pixel != -1)
4794 {
4795 XtSetArg (av[ac], XtNforeground, pixel);
4796 ++ac;
4797 }
7d0393cf 4798
06a2c219
GM
4799 pixel = f->output_data.x->scroll_bar_background_pixel;
4800 if (pixel != -1)
4801 {
4802 XtSetArg (av[ac], XtNbackground, pixel);
4803 ++ac;
4804 }
7c1bef7a
MB
4805
4806 /* Top/bottom shadow colors. */
4807
4808 /* Allocate them, if necessary. */
4809 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4810 {
4811 pixel = f->output_data.x->scroll_bar_background_pixel;
4812 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4813 &pixel, 1.2, 0x8000))
4814 pixel = -1;
4815 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4816 }
4817 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4818 {
4819 pixel = f->output_data.x->scroll_bar_background_pixel;
4820 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4821 &pixel, 0.6, 0x4000))
4822 pixel = -1;
4823 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4824 }
4825
6ca53601 4826#ifdef XtNbeNiceToColormap
7c1bef7a
MB
4827 /* Tell the toolkit about them. */
4828 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4829 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4830 /* We tried to allocate a color for the top/bottom shadow, and
4831 failed, so tell Xaw3d to use dithering instead. */
4832 {
4833 XtSetArg (av[ac], XtNbeNiceToColormap, True);
4834 ++ac;
4835 }
4836 else
4837 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4838 be more consistent with other emacs 3d colors, and since Xaw3d is
4839 not good at dealing with allocation failure. */
4840 {
4841 /* This tells Xaw3d to use real colors instead of dithering for
4842 the shadows. */
4843 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4844 ++ac;
4845
4846 /* Specify the colors. */
4847 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4848 if (pixel != -1)
4849 {
6ca53601 4850 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
7c1bef7a
MB
4851 ++ac;
4852 }
4853 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4854 if (pixel != -1)
4855 {
6ca53601 4856 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
7c1bef7a
MB
4857 ++ac;
4858 }
4859 }
6ca53601 4860#endif
7c1bef7a 4861
06a2c219
GM
4862 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4863 f->output_data.x->edit_widget, av, ac);
ec18280f
SM
4864
4865 {
4866 char *initial = "";
4867 char *val = initial;
4868 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
9cfc7da4
SM
4869#ifdef XtNarrowScrollbars
4870 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
4871#endif
ec18280f 4872 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
9cfc7da4 4873 if (xaw3d_arrow_scroll || val == initial)
ec18280f
SM
4874 { /* ARROW_SCROLL */
4875 xaw3d_arrow_scroll = True;
9cfc7da4 4876 /* Isn't that just a personal preference ? --Stef */
ec18280f
SM
4877 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4878 }
4879 }
7d0393cf 4880
06a2c219 4881 /* Define callbacks. */
ec18280f
SM
4882 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4883 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
06a2c219 4884 (XtPointer) bar);
7d0393cf 4885
06a2c219
GM
4886 /* Realize the widget. Only after that is the X window created. */
4887 XtRealizeWidget (widget);
7d0393cf 4888
ec18280f 4889#endif /* !USE_MOTIF */
06a2c219 4890
2a51c026 4891 /* Install an action hook that lets us detect when the user
06a2c219
GM
4892 finishes interacting with a scroll bar. */
4893 if (action_hook_id == 0)
4894 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
7d0393cf 4895
06a2c219
GM
4896 /* Remember X window and widget in the scroll bar vector. */
4897 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4898 xwindow = XtWindow (widget);
056ce195 4899 bar->x_window = xwindow;
06a2c219
GM
4900
4901 UNBLOCK_INPUT;
4902}
488dd4c4 4903#endif /* not USE_GTK */
06a2c219
GM
4904
4905
4906/* Set the thumb size and position of scroll bar BAR. We are currently
4907 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4908
488dd4c4
JD
4909#ifdef USE_GTK
4910static void
4911x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4912 struct scroll_bar *bar;
4913 int portion, position, whole;
4914{
4915 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4916}
4917
4918#else /* not USE_GTK */
06a2c219
GM
4919static void
4920x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4921 struct scroll_bar *bar;
4922 int portion, position, whole;
f451eb13 4923{
e83dc917
GM
4924 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4925 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
06a2c219 4926 float top, shown;
f451eb13 4927
6f2a7a68
SM
4928 BLOCK_INPUT;
4929
4930#ifdef USE_MOTIF
4931
4932 /* We use an estimate of 30 chars per line rather than the real
4933 `portion' value. This has the disadvantage that the thumb size
4934 is not very representative, but it makes our life a lot easier.
4935 Otherwise, we have to constantly adjust the thumb size, which
4936 we can't always do quickly enough: while dragging, the size of
4937 the thumb might prevent the user from dragging the thumb all the
4938 way to the end. but Motif and some versions of Xaw3d don't allow
4939 updating the thumb size while dragging. Also, even if we can update
4940 its size, the update will often happen too late.
4941 If you don't believe it, check out revision 1.650 of xterm.c to see
4942 what hoops we were going through and the still poor behavior we got. */
0899d58c 4943 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
6f2a7a68
SM
4944 /* When the thumb is at the bottom, position == whole.
4945 So we need to increase `whole' to make space for the thumb. */
4946 whole += portion;
4947
4948 if (whole <= 0)
06a2c219
GM
4949 top = 0, shown = 1;
4950 else
f451eb13 4951 {
06a2c219
GM
4952 top = (float) position / whole;
4953 shown = (float) portion / whole;
4954 }
f451eb13 4955
6f2a7a68
SM
4956 if (NILP (bar->dragging))
4957 {
4958 int size, value;
06a2c219 4959
6f2a7a68
SM
4960 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4961 is the scroll bar's maximum and MIN is the scroll bar's minimum
4962 value. */
3c671a56
SM
4963 size = shown * XM_SB_MAX;
4964 size = min (size, XM_SB_MAX);
6f2a7a68 4965 size = max (size, 1);
06a2c219 4966
6f2a7a68 4967 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
3c671a56 4968 value = top * XM_SB_MAX;
6f2a7a68 4969 value = min (value, XM_SB_MAX - size);
7d0393cf 4970
06a2c219 4971 XmScrollBarSetValues (widget, value, size, 0, 0, False);
6f2a7a68 4972 }
ec18280f 4973#else /* !USE_MOTIF i.e. use Xaw */
6f2a7a68
SM
4974
4975 if (whole == 0)
4976 top = 0, shown = 1;
4977 else
4978 {
4979 top = (float) position / whole;
4980 shown = (float) portion / whole;
4981 }
4982
06a2c219 4983 {
ec18280f
SM
4984 float old_top, old_shown;
4985 Dimension height;
4986 XtVaGetValues (widget,
4987 XtNtopOfThumb, &old_top,
4988 XtNshown, &old_shown,
4989 XtNheight, &height,
4990 NULL);
4991
4992 /* Massage the top+shown values. */
4993 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
4994 top = max (0, min (1, top));
4995 else
4996 top = old_top;
4997 /* Keep two pixels available for moving the thumb down. */
4998 shown = max (0, min (1 - top - (2.0 / height), shown));
06a2c219
GM
4999
5000 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
5001 check that your system's configuration file contains a define
5002 for `NARROWPROTO'. See s/freebsd.h for an example. */
ec18280f 5003 if (top != old_top || shown != old_shown)
eb393530 5004 {
ec18280f 5005 if (NILP (bar->dragging))
eb393530 5006 XawScrollbarSetThumb (widget, top, shown);
06a2c219
GM
5007 else
5008 {
ec18280f
SM
5009 /* Try to make the scrolling a tad smoother. */
5010 if (!xaw3d_pick_top)
5011 shown = min (shown, old_shown);
7d0393cf 5012
ec18280f 5013 XawScrollbarSetThumb (widget, top, shown);
06a2c219 5014 }
06a2c219
GM
5015 }
5016 }
ec18280f 5017#endif /* !USE_MOTIF */
06a2c219
GM
5018
5019 UNBLOCK_INPUT;
f451eb13 5020}
488dd4c4 5021#endif /* not USE_GTK */
f451eb13 5022
06a2c219
GM
5023#endif /* USE_TOOLKIT_SCROLL_BARS */
5024
5025
5026\f
5027/************************************************************************
5028 Scroll bars, general
5029 ************************************************************************/
7d0393cf 5030
06a2c219
GM
5031/* Create a scroll bar and return the scroll bar vector for it. W is
5032 the Emacs window on which to create the scroll bar. TOP, LEFT,
f7ccf929 5033 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
06a2c219
GM
5034 scroll bar. */
5035
ab648270 5036static struct scroll_bar *
06a2c219
GM
5037x_scroll_bar_create (w, top, left, width, height)
5038 struct window *w;
f451eb13
JB
5039 int top, left, width, height;
5040{
06a2c219 5041 struct frame *f = XFRAME (w->frame);
334208b7 5042 struct scroll_bar *bar
056ce195 5043 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
f451eb13
JB
5044
5045 BLOCK_INPUT;
5046
eccc05db 5047#ifdef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
5048 x_create_toolkit_scroll_bar (f, bar);
5049#else /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
5050 {
5051 XSetWindowAttributes a;
5052 unsigned long mask;
5c187dee 5053 Window window;
06a2c219
GM
5054
5055 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
5056 if (a.background_pixel == -1)
5bcee7ef 5057 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
7d0393cf 5058
12ba150f 5059 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9a572e2a 5060 | ButtonMotionMask | PointerMotionHintMask
12ba150f 5061 | ExposureMask);
7a13e894 5062 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
f451eb13 5063
dbc4e1c1 5064 mask = (CWBackPixel | CWEventMask | CWCursor);
f451eb13 5065
06a2c219
GM
5066 /* Clear the area of W that will serve as a scroll bar. This is
5067 for the case that a window has been split horizontally. In
5068 this case, no clear_frame is generated to reduce flickering. */
7b49b9d2
GM
5069 if (width > 0 && height > 0)
5070 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5071 left, top, width,
5072 window_box_height (w), False);
06a2c219
GM
5073
5074 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5075 /* Position and size of scroll bar. */
5076 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5077 top,
5078 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5079 height,
5080 /* Border width, depth, class, and visual. */
5081 0,
5082 CopyFromParent,
5083 CopyFromParent,
5084 CopyFromParent,
5085 /* Attributes. */
5086 mask, &a);
056ce195 5087 bar->x_window = window;
f451eb13 5088 }
06a2c219 5089#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 5090
06a2c219 5091 XSETWINDOW (bar->window, w);
056ce195
SM
5092 bar->top = top;
5093 bar->left = left;
5094 bar->width = width;
5095 bar->height = height;
5096 bar->start = 0;
5097 bar->end = 0;
12ba150f 5098 bar->dragging = Qnil;
056ce195 5099 bar->fringe_extended_p = 0;
f451eb13
JB
5100
5101 /* Add bar to its frame's list of scroll bars. */
334208b7 5102 bar->next = FRAME_SCROLL_BARS (f);
12ba150f 5103 bar->prev = Qnil;
334208b7 5104 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
06a2c219 5105 if (!NILP (bar->next))
e0c1aef2 5106 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13 5107
06a2c219 5108 /* Map the window/widget. */
eccc05db 5109#ifdef USE_TOOLKIT_SCROLL_BARS
f964b4d7 5110 {
488dd4c4
JD
5111#ifdef USE_GTK
5112 xg_update_scrollbar_pos (f,
056ce195 5113 bar->x_window,
488dd4c4
JD
5114 top,
5115 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5116 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
6bd8015d 5117 max (height, 1));
056ce195 5118 xg_show_scroll_bar (bar->x_window);
488dd4c4 5119#else /* not USE_GTK */
f964b4d7
GM
5120 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
5121 XtConfigureWidget (scroll_bar,
5122 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5123 top,
5124 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5125 max (height, 1), 0);
5126 XtMapWidget (scroll_bar);
488dd4c4 5127#endif /* not USE_GTK */
f964b4d7 5128 }
06a2c219 5129#else /* not USE_TOOLKIT_SCROLL_BARS */
056ce195 5130 XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
06a2c219 5131#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13
JB
5132
5133 UNBLOCK_INPUT;
12ba150f 5134 return bar;
f451eb13
JB
5135}
5136
06a2c219 5137
056ce195
SM
5138#ifndef USE_TOOLKIT_SCROLL_BARS
5139
12ba150f 5140/* Draw BAR's handle in the proper position.
7d0393cf 5141
12ba150f
JB
5142 If the handle is already drawn from START to END, don't bother
5143 redrawing it, unless REBUILD is non-zero; in that case, always
5144 redraw it. (REBUILD is handy for drawing the handle after expose
58769bee 5145 events.)
12ba150f
JB
5146
5147 Normally, we want to constrain the start and end of the handle to
06a2c219
GM
5148 fit inside its rectangle, but if the user is dragging the scroll
5149 bar handle, we want to let them drag it down all the way, so that
5150 the bar's top is as far down as it goes; otherwise, there's no way
5151 to move to the very end of the buffer. */
5152
f451eb13 5153static void
ab648270
JB
5154x_scroll_bar_set_handle (bar, start, end, rebuild)
5155 struct scroll_bar *bar;
f451eb13 5156 int start, end;
12ba150f 5157 int rebuild;
f451eb13 5158{
12ba150f 5159 int dragging = ! NILP (bar->dragging);
056ce195 5160 Window w = bar->x_window;
334208b7 5161 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 5162 GC gc = f->output_data.x->normal_gc;
12ba150f
JB
5163
5164 /* If the display is already accurate, do nothing. */
5165 if (! rebuild
95fbaefc
JD
5166 && start == bar->start
5167 && end == bar->end)
12ba150f
JB
5168 return;
5169
f451eb13
JB
5170 BLOCK_INPUT;
5171
5172 {
95fbaefc
JD
5173 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
5174 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
5175 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
f451eb13
JB
5176
5177 /* Make sure the values are reasonable, and try to preserve
5178 the distance between start and end. */
12ba150f
JB
5179 {
5180 int length = end - start;
5181
5182 if (start < 0)
5183 start = 0;
5184 else if (start > top_range)
5185 start = top_range;
5186 end = start + length;
5187
5188 if (end < start)
5189 end = start;
5190 else if (end > top_range && ! dragging)
5191 end = top_range;
5192 }
f451eb13 5193
ab648270 5194 /* Store the adjusted setting in the scroll bar. */
95fbaefc
JD
5195 bar->start = start;
5196 bar->end = end;
f451eb13 5197
12ba150f
JB
5198 /* Clip the end position, just for display. */
5199 if (end > top_range)
5200 end = top_range;
f451eb13 5201
ab648270 5202 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
12ba150f
JB
5203 below top positions, to make sure the handle is always at least
5204 that many pixels tall. */
ab648270 5205 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
f451eb13 5206
12ba150f
JB
5207 /* Draw the empty space above the handle. Note that we can't clear
5208 zero-height areas; that means "clear to end of window." */
5209 if (0 < start)
c5e6e06b
GM
5210 x_clear_area (FRAME_X_DISPLAY (f), w,
5211 /* x, y, width, height, and exposures. */
5212 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5213 VERTICAL_SCROLL_BAR_TOP_BORDER,
5214 inside_width, start,
5215 False);
f451eb13 5216
06a2c219
GM
5217 /* Change to proper foreground color if one is specified. */
5218 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5219 XSetForeground (FRAME_X_DISPLAY (f), gc,
5220 f->output_data.x->scroll_bar_foreground_pixel);
5221
12ba150f 5222 /* Draw the handle itself. */
334208b7 5223 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
12ba150f 5224 /* x, y, width, height */
ab648270
JB
5225 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5226 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
12ba150f 5227 inside_width, end - start);
f451eb13 5228
06a2c219
GM
5229 /* Restore the foreground color of the GC if we changed it above. */
5230 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5231 XSetForeground (FRAME_X_DISPLAY (f), gc,
5bcee7ef 5232 FRAME_FOREGROUND_PIXEL (f));
f451eb13 5233
12ba150f
JB
5234 /* Draw the empty space below the handle. Note that we can't
5235 clear zero-height areas; that means "clear to end of window." */
5236 if (end < inside_height)
c5e6e06b
GM
5237 x_clear_area (FRAME_X_DISPLAY (f), w,
5238 /* x, y, width, height, and exposures. */
5239 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5240 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
5241 inside_width, inside_height - end,
5242 False);
f451eb13 5243
f451eb13
JB
5244 }
5245
f451eb13
JB
5246 UNBLOCK_INPUT;
5247}
5248
5c187dee 5249#endif /* !USE_TOOLKIT_SCROLL_BARS */
f451eb13 5250
06a2c219
GM
5251/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
5252 nil. */
58769bee 5253
12ba150f 5254static void
ab648270
JB
5255x_scroll_bar_remove (bar)
5256 struct scroll_bar *bar;
12ba150f 5257{
e83dc917 5258 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f
JB
5259 BLOCK_INPUT;
5260
eccc05db 5261#ifdef USE_TOOLKIT_SCROLL_BARS
488dd4c4 5262#ifdef USE_GTK
056ce195 5263 xg_remove_scroll_bar (f, bar->x_window);
488dd4c4 5264#else /* not USE_GTK */
e83dc917 5265 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
488dd4c4 5266#endif /* not USE_GTK */
e83dc917 5267#else
056ce195 5268 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
e83dc917 5269#endif
7d0393cf 5270
ab648270
JB
5271 /* Disassociate this scroll bar from its window. */
5272 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
12ba150f
JB
5273
5274 UNBLOCK_INPUT;
5275}
5276
06a2c219 5277
12ba150f
JB
5278/* Set the handle of the vertical scroll bar for WINDOW to indicate
5279 that we are displaying PORTION characters out of a total of WHOLE
ab648270 5280 characters, starting at POSITION. If WINDOW has no scroll bar,
12ba150f 5281 create one. */
06a2c219 5282
12ba150f 5283static void
06a2c219
GM
5284XTset_vertical_scroll_bar (w, portion, whole, position)
5285 struct window *w;
f451eb13
JB
5286 int portion, whole, position;
5287{
06a2c219 5288 struct frame *f = XFRAME (w->frame);
ab648270 5289 struct scroll_bar *bar;
3c6ede7b 5290 int top, height, left, sb_left, width, sb_width;
0899d58c 5291 int window_y, window_height;
9143075d
YM
5292#ifdef USE_TOOLKIT_SCROLL_BARS
5293 int fringe_extended_p;
5294#endif
06a2c219 5295
3c6ede7b 5296 /* Get window dimensions. */
0899d58c 5297 window_box (w, -1, 0, &window_y, 0, &window_height);
3c6ede7b 5298 top = window_y;
0899d58c 5299 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
3c6ede7b 5300 height = window_height;
06a2c219 5301
3c6ede7b 5302 /* Compute the left edge of the scroll bar area. */
0899d58c 5303 left = WINDOW_SCROLL_BAR_AREA_X (w);
3c6ede7b
GM
5304
5305 /* Compute the width of the scroll bar which might be less than
5306 the width of the area reserved for the scroll bar. */
0899d58c
KS
5307 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
5308 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
06a2c219 5309 else
3c6ede7b 5310 sb_width = width;
12ba150f 5311
3c6ede7b
GM
5312 /* Compute the left edge of the scroll bar. */
5313#ifdef USE_TOOLKIT_SCROLL_BARS
0899d58c 5314 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
9143075d 5315 sb_left = left + (WINDOW_RIGHTMOST_P (w) ? width - sb_width : 0);
3c6ede7b 5316 else
9143075d 5317 sb_left = left + (WINDOW_LEFTMOST_P (w) ? 0 : width - sb_width);
3c6ede7b 5318#else
0899d58c 5319 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
7d0393cf 5320 sb_left = left + width - sb_width;
3c6ede7b
GM
5321 else
5322 sb_left = left;
5323#endif
7d0393cf 5324
9143075d
YM
5325#ifdef USE_TOOLKIT_SCROLL_BARS
5326 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5327 fringe_extended_p = (WINDOW_LEFTMOST_P (w)
5328 && WINDOW_LEFT_FRINGE_WIDTH (w)
5329 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5330 || WINDOW_LEFT_MARGIN_COLS (w) == 0));
5331 else
5332 fringe_extended_p = (WINDOW_RIGHTMOST_P (w)
5333 && WINDOW_RIGHT_FRINGE_WIDTH (w)
5334 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5335 || WINDOW_RIGHT_MARGIN_COLS (w) == 0));
5336#endif
5337
ab648270 5338 /* Does the scroll bar exist yet? */
06a2c219 5339 if (NILP (w->vertical_scroll_bar))
3c6ede7b 5340 {
7b49b9d2 5341 if (width > 0 && height > 0)
b547b6e8
GM
5342 {
5343 BLOCK_INPUT;
9143075d
YM
5344#ifdef USE_TOOLKIT_SCROLL_BARS
5345 if (fringe_extended_p)
5346 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5347 sb_left, top, sb_width, height, False);
5348 else
5349#endif
5350 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5351 left, top, width, height, False);
b547b6e8
GM
5352 UNBLOCK_INPUT;
5353 }
7d0393cf 5354
3c6ede7b
GM
5355 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5356 }
f451eb13 5357 else
12ba150f
JB
5358 {
5359 /* It may just need to be moved and resized. */
06a2c219 5360 unsigned int mask = 0;
7d0393cf 5361
06a2c219
GM
5362 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5363
5364 BLOCK_INPUT;
5365
056ce195 5366 if (sb_left != bar->left)
06a2c219 5367 mask |= CWX;
056ce195 5368 if (top != bar->top)
06a2c219 5369 mask |= CWY;
056ce195 5370 if (sb_width != bar->width)
06a2c219 5371 mask |= CWWidth;
056ce195 5372 if (height != bar->height)
06a2c219 5373 mask |= CWHeight;
7d0393cf 5374
06a2c219 5375#ifdef USE_TOOLKIT_SCROLL_BARS
fe6f39d9 5376
06a2c219 5377 /* Move/size the scroll bar widget. */
056ce195 5378 if (mask || bar->fringe_extended_p != fringe_extended_p)
13fcb889
KS
5379 {
5380 /* Since toolkit scroll bars are smaller than the space reserved
5381 for them on the frame, we have to clear "under" them. */
5382 if (width > 0 && height > 0)
9143075d
YM
5383 {
5384 if (fringe_extended_p)
5385 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5386 sb_left, top, sb_width, height, False);
5387 else
5388 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5389 left, top, width, height, False);
5390 }
6bd8015d
JD
5391#ifdef USE_GTK
5392 xg_update_scrollbar_pos (f,
056ce195 5393 bar->x_window,
6bd8015d
JD
5394 top,
5395 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5396 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM *2,
5397 max (height, 1));
5398#else /* not USE_GTK */
488dd4c4
JD
5399 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5400 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5401 top,
5402 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5403 max (height, 1), 0);
488dd4c4 5404#endif /* not USE_GTK */
6bd8015d 5405 }
06a2c219 5406#else /* not USE_TOOLKIT_SCROLL_BARS */
7d0393cf 5407
357e7376
GM
5408 /* Clear areas not covered by the scroll bar because of
5409 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
e1f6572f
RS
5410 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
5411 {
c5e6e06b
GM
5412 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5413 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5414 height, False);
5415 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5416 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5417 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5418 height, False);
e1f6572f 5419 }
357e7376
GM
5420
5421 /* Clear areas not covered by the scroll bar because it's not as
f7ccf929 5422 wide as the area reserved for it. This makes sure a
357e7376
GM
5423 previous mode line display is cleared after C-x 2 C-x 1, for
5424 example. */
5425 {
0899d58c 5426 int area_width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
357e7376 5427 int rest = area_width - sb_width;
38d2af0c
GM
5428 if (rest > 0 && height > 0)
5429 {
0899d58c 5430 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
38d2af0c
GM
5431 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5432 left + area_width - rest, top,
5433 rest, height, False);
5434 else
5435 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5436 left, top, rest, height, False);
5437 }
357e7376 5438 }
7d0393cf 5439
06a2c219
GM
5440 /* Move/size the scroll bar window. */
5441 if (mask)
5442 {
5443 XWindowChanges wc;
7d0393cf 5444
3c6ede7b
GM
5445 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5446 wc.y = top;
5447 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
5448 wc.height = height;
056ce195 5449 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
06a2c219
GM
5450 mask, &wc);
5451 }
7d0393cf 5452
06a2c219
GM
5453#endif /* not USE_TOOLKIT_SCROLL_BARS */
5454
5455 /* Remember new settings. */
056ce195
SM
5456 bar->left = sb_left;
5457 bar->top = top;
5458 bar->width = sb_width;
5459 bar->height = height;
7d0393cf 5460
06a2c219 5461 UNBLOCK_INPUT;
12ba150f 5462 }
f451eb13 5463
eccc05db 5464#ifdef USE_TOOLKIT_SCROLL_BARS
056ce195 5465 bar->fringe_extended_p = fringe_extended_p;
9143075d 5466
06a2c219
GM
5467 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5468#else /* not USE_TOOLKIT_SCROLL_BARS */
ab648270 5469 /* Set the scroll bar's current state, unless we're currently being
f451eb13 5470 dragged. */
12ba150f 5471 if (NILP (bar->dragging))
f451eb13 5472 {
92857db0 5473 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
f451eb13 5474
12ba150f 5475 if (whole == 0)
ab648270 5476 x_scroll_bar_set_handle (bar, 0, top_range, 0);
12ba150f
JB
5477 else
5478 {
43f868f5
JB
5479 int start = ((double) position * top_range) / whole;
5480 int end = ((double) (position + portion) * top_range) / whole;
ab648270 5481 x_scroll_bar_set_handle (bar, start, end, 0);
12ba150f 5482 }
f451eb13 5483 }
06a2c219 5484#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 5485
06a2c219 5486 XSETVECTOR (w->vertical_scroll_bar, bar);
f451eb13
JB
5487}
5488
12ba150f 5489
f451eb13 5490/* The following three hooks are used when we're doing a thorough
ab648270 5491 redisplay of the frame. We don't explicitly know which scroll bars
f451eb13 5492 are going to be deleted, because keeping track of when windows go
12ba150f
JB
5493 away is a real pain - "Can you say set-window-configuration, boys
5494 and girls?" Instead, we just assert at the beginning of redisplay
ab648270 5495 that *all* scroll bars are to be removed, and then save a scroll bar
12ba150f 5496 from the fiery pit when we actually redisplay its window. */
f451eb13 5497
ab648270
JB
5498/* Arrange for all scroll bars on FRAME to be removed at the next call
5499 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
06a2c219
GM
5500 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5501
58769bee 5502static void
ab648270 5503XTcondemn_scroll_bars (frame)
f451eb13
JB
5504 FRAME_PTR frame;
5505{
f9e24cb9
RS
5506 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5507 while (! NILP (FRAME_SCROLL_BARS (frame)))
5508 {
5509 Lisp_Object bar;
5510 bar = FRAME_SCROLL_BARS (frame);
5511 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
5512 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5513 XSCROLL_BAR (bar)->prev = Qnil;
5514 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5515 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5516 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
5517 }
f451eb13
JB
5518}
5519
fa2dfc30 5520
06a2c219 5521/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
12ba150f 5522 Note that WINDOW isn't necessarily condemned at all. */
fa2dfc30 5523
f451eb13 5524static void
ab648270 5525XTredeem_scroll_bar (window)
12ba150f 5526 struct window *window;
f451eb13 5527{
ab648270 5528 struct scroll_bar *bar;
fa2dfc30 5529 struct frame *f;
12ba150f 5530
ab648270
JB
5531 /* We can't redeem this window's scroll bar if it doesn't have one. */
5532 if (NILP (window->vertical_scroll_bar))
12ba150f
JB
5533 abort ();
5534
ab648270 5535 bar = XSCROLL_BAR (window->vertical_scroll_bar);
12ba150f
JB
5536
5537 /* Unlink it from the condemned list. */
fa2dfc30
GM
5538 f = XFRAME (WINDOW_FRAME (window));
5539 if (NILP (bar->prev))
5540 {
5541 /* If the prev pointer is nil, it must be the first in one of
5542 the lists. */
5543 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5544 /* It's not condemned. Everything's fine. */
5545 return;
5546 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5547 window->vertical_scroll_bar))
5548 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
5549 else
5550 /* If its prev pointer is nil, it must be at the front of
5551 one or the other! */
5552 abort ();
5553 }
5554 else
5555 XSCROLL_BAR (bar->prev)->next = bar->next;
12ba150f 5556
fa2dfc30
GM
5557 if (! NILP (bar->next))
5558 XSCROLL_BAR (bar->next)->prev = bar->prev;
12ba150f 5559
fa2dfc30
GM
5560 bar->next = FRAME_SCROLL_BARS (f);
5561 bar->prev = Qnil;
5562 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5563 if (! NILP (bar->next))
5564 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
f451eb13
JB
5565}
5566
ab648270
JB
5567/* Remove all scroll bars on FRAME that haven't been saved since the
5568 last call to `*condemn_scroll_bars_hook'. */
06a2c219 5569
f451eb13 5570static void
ab648270 5571XTjudge_scroll_bars (f)
12ba150f 5572 FRAME_PTR f;
f451eb13 5573{
12ba150f 5574 Lisp_Object bar, next;
f451eb13 5575
ab648270 5576 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
cf7cb199
JB
5577
5578 /* Clear out the condemned list now so we won't try to process any
ab648270
JB
5579 more events on the hapless scroll bars. */
5580 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
cf7cb199
JB
5581
5582 for (; ! NILP (bar); bar = next)
f451eb13 5583 {
ab648270 5584 struct scroll_bar *b = XSCROLL_BAR (bar);
12ba150f 5585
ab648270 5586 x_scroll_bar_remove (b);
12ba150f
JB
5587
5588 next = b->next;
5589 b->next = b->prev = Qnil;
f451eb13 5590 }
12ba150f 5591
ab648270 5592 /* Now there should be no references to the condemned scroll bars,
12ba150f 5593 and they should get garbage-collected. */
f451eb13
JB
5594}
5595
5596
3c671a56 5597#ifndef USE_TOOLKIT_SCROLL_BARS
06a2c219
GM
5598/* Handle an Expose or GraphicsExpose event on a scroll bar. This
5599 is a no-op when using toolkit scroll bars.
ab648270
JB
5600
5601 This may be called from a signal handler, so we have to ignore GC
5602 mark bits. */
06a2c219 5603
f451eb13 5604static void
ab648270
JB
5605x_scroll_bar_expose (bar, event)
5606 struct scroll_bar *bar;
f451eb13
JB
5607 XEvent *event;
5608{
056ce195 5609 Window w = bar->x_window;
334208b7 5610 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7556890b 5611 GC gc = f->output_data.x->normal_gc;
3cbd2e0b 5612 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
12ba150f 5613
f451eb13
JB
5614 BLOCK_INPUT;
5615
95fbaefc 5616 x_scroll_bar_set_handle (bar, bar->start, bar->end, 1);
f451eb13 5617
f8e2f513 5618 /* Switch to scroll bar foreground color. */
1024f9c0
JD
5619 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5620 XSetForeground (FRAME_X_DISPLAY (f), gc,
5621 f->output_data.x->scroll_bar_foreground_pixel);
5622
06a2c219 5623 /* Draw a one-pixel border just inside the edges of the scroll bar. */
334208b7 5624 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
f451eb13
JB
5625
5626 /* x, y, width, height */
d9cdbb3d 5627 0, 0,
95fbaefc
JD
5628 bar->width - 1 - width_trim - width_trim,
5629 bar->height - 1);
7d0393cf 5630
1024f9c0
JD
5631 /* Restore the foreground color of the GC if we changed it above. */
5632 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5633 XSetForeground (FRAME_X_DISPLAY (f), gc,
0218dbe2 5634 FRAME_FOREGROUND_PIXEL (f));
1024f9c0
JD
5635
5636 UNBLOCK_INPUT;
06a2c219 5637
f451eb13 5638}
3c671a56 5639#endif /* not USE_TOOLKIT_SCROLL_BARS */
f451eb13 5640
ab648270 5641/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
3b8f9651 5642 is set to something other than NO_EVENT, it is enqueued.
ab648270
JB
5643
5644 This may be called from a signal handler, so we have to ignore GC
5645 mark bits. */
06a2c219 5646
5c187dee 5647
f451eb13 5648static void
ab648270
JB
5649x_scroll_bar_handle_click (bar, event, emacs_event)
5650 struct scroll_bar *bar;
f451eb13
JB
5651 XEvent *event;
5652 struct input_event *emacs_event;
5653{
8e50cc2d 5654 if (! WINDOWP (bar->window))
12ba150f
JB
5655 abort ();
5656
3b8f9651 5657 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
69388238 5658 emacs_event->code = event->xbutton.button - Button1;
0299d313 5659 emacs_event->modifiers
7d0393cf 5660 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
0299d313
RS
5661 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5662 event->xbutton.state)
5663 | (event->type == ButtonRelease
5664 ? up_modifier
5665 : down_modifier));
12ba150f 5666 emacs_event->frame_or_window = bar->window;
0f8aabe9 5667 emacs_event->arg = Qnil;
f451eb13 5668 emacs_event->timestamp = event->xbutton.time;
12ba150f 5669 {
06a2c219 5670#if 0
d9cdbb3d 5671 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
0299d313 5672 int internal_height
056ce195 5673 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
06a2c219 5674#endif
0299d313 5675 int top_range
056ce195 5676 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
ab648270 5677 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
12ba150f
JB
5678
5679 if (y < 0) y = 0;
5680 if (y > top_range) y = top_range;
5681
056ce195 5682 if (y < bar->start)
ab648270 5683 emacs_event->part = scroll_bar_above_handle;
056ce195 5684 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
ab648270 5685 emacs_event->part = scroll_bar_handle;
12ba150f 5686 else
ab648270 5687 emacs_event->part = scroll_bar_below_handle;
929787e1
JB
5688
5689 /* Just because the user has clicked on the handle doesn't mean
5116f055
JB
5690 they want to drag it. Lisp code needs to be able to decide
5691 whether or not we're dragging. */
929787e1 5692#if 0
12ba150f
JB
5693 /* If the user has just clicked on the handle, record where they're
5694 holding it. */
5695 if (event->type == ButtonPress
ab648270 5696 && emacs_event->part == scroll_bar_handle)
95fbaefc 5697 XSETINT (bar->dragging, y - bar->start);
929787e1 5698#endif
12ba150f 5699
257f40f2 5700#ifndef USE_TOOLKIT_SCROLL_BARS
12ba150f
JB
5701 /* If the user has released the handle, set it to its final position. */
5702 if (event->type == ButtonRelease
5703 && ! NILP (bar->dragging))
5704 {
5705 int new_start = y - XINT (bar->dragging);
95fbaefc 5706 int new_end = new_start + bar->end - bar->start;
f451eb13 5707
ab648270 5708 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
12ba150f
JB
5709 bar->dragging = Qnil;
5710 }
257f40f2 5711#endif
f451eb13 5712
5116f055
JB
5713 /* Same deal here as the other #if 0. */
5714#if 0
58769bee 5715 /* Clicks on the handle are always reported as occurring at the top of
12ba150f 5716 the handle. */
ab648270 5717 if (emacs_event->part == scroll_bar_handle)
12ba150f
JB
5718 emacs_event->x = bar->start;
5719 else
e0c1aef2 5720 XSETINT (emacs_event->x, y);
5116f055 5721#else
e0c1aef2 5722 XSETINT (emacs_event->x, y);
5116f055 5723#endif
f451eb13 5724
e0c1aef2 5725 XSETINT (emacs_event->y, top_range);
12ba150f
JB
5726 }
5727}
f451eb13 5728
257f40f2
JD
5729#ifndef USE_TOOLKIT_SCROLL_BARS
5730
ab648270
JB
5731/* Handle some mouse motion while someone is dragging the scroll bar.
5732
5733 This may be called from a signal handler, so we have to ignore GC
5734 mark bits. */
06a2c219 5735
f451eb13 5736static void
ab648270
JB
5737x_scroll_bar_note_movement (bar, event)
5738 struct scroll_bar *bar;
f451eb13
JB
5739 XEvent *event;
5740{
39d8bb4d
KH
5741 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
5742
f451eb13
JB
5743 last_mouse_movement_time = event->xmotion.time;
5744
39d8bb4d 5745 f->mouse_moved = 1;
e0c1aef2 5746 XSETVECTOR (last_mouse_scroll_bar, bar);
f451eb13
JB
5747
5748 /* If we're dragging the bar, display it. */
8e50cc2d 5749 if (! NILP (bar->dragging))
f451eb13
JB
5750 {
5751 /* Where should the handle be now? */
12ba150f 5752 int new_start = event->xmotion.y - XINT (bar->dragging);
f451eb13 5753
95fbaefc 5754 if (new_start != bar->start)
f451eb13 5755 {
95fbaefc 5756 int new_end = new_start + bar->end - bar->start;
58769bee 5757
ab648270 5758 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
f451eb13
JB
5759 }
5760 }
f451eb13
JB
5761}
5762
5c187dee
GM
5763#endif /* !USE_TOOLKIT_SCROLL_BARS */
5764
12ba150f 5765/* Return information to the user about the current position of the mouse
ab648270 5766 on the scroll bar. */
06a2c219 5767
12ba150f 5768static void
334208b7
RS
5769x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
5770 FRAME_PTR *fp;
12ba150f 5771 Lisp_Object *bar_window;
ab648270 5772 enum scroll_bar_part *part;
12ba150f
JB
5773 Lisp_Object *x, *y;
5774 unsigned long *time;
5775{
ab648270 5776 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
056ce195 5777 Window w = bar->x_window;
334208b7 5778 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
12ba150f 5779 int win_x, win_y;
559cb2fb
JB
5780 Window dummy_window;
5781 int dummy_coord;
5782 unsigned int dummy_mask;
12ba150f 5783
cf7cb199
JB
5784 BLOCK_INPUT;
5785
ab648270 5786 /* Get the mouse's position relative to the scroll bar window, and
12ba150f 5787 report that. */
334208b7 5788 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
12ba150f 5789
559cb2fb
JB
5790 /* Root, child, root x and root y. */
5791 &dummy_window, &dummy_window,
5792 &dummy_coord, &dummy_coord,
12ba150f 5793
559cb2fb
JB
5794 /* Position relative to scroll bar. */
5795 &win_x, &win_y,
12ba150f 5796
559cb2fb
JB
5797 /* Mouse buttons and modifier keys. */
5798 &dummy_mask))
7a13e894 5799 ;
559cb2fb
JB
5800 else
5801 {
06a2c219 5802#if 0
559cb2fb 5803 int inside_height
056ce195 5804 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
06a2c219 5805#endif
559cb2fb 5806 int top_range
056ce195 5807 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
559cb2fb
JB
5808
5809 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5810
5811 if (! NILP (bar->dragging))
5812 win_y -= XINT (bar->dragging);
5813
5814 if (win_y < 0)
5815 win_y = 0;
5816 if (win_y > top_range)
5817 win_y = top_range;
5818
334208b7 5819 *fp = f;
7a13e894 5820 *bar_window = bar->window;
559cb2fb
JB
5821
5822 if (! NILP (bar->dragging))
5823 *part = scroll_bar_handle;
056ce195 5824 else if (win_y < bar->start)
559cb2fb 5825 *part = scroll_bar_above_handle;
056ce195 5826 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
559cb2fb
JB
5827 *part = scroll_bar_handle;
5828 else
5829 *part = scroll_bar_below_handle;
12ba150f 5830
e0c1aef2
KH
5831 XSETINT (*x, win_y);
5832 XSETINT (*y, top_range);
12ba150f 5833
39d8bb4d 5834 f->mouse_moved = 0;
559cb2fb
JB
5835 last_mouse_scroll_bar = Qnil;
5836 }
12ba150f 5837
559cb2fb 5838 *time = last_mouse_movement_time;
cf7cb199 5839
cf7cb199 5840 UNBLOCK_INPUT;
12ba150f
JB
5841}
5842
f451eb13 5843
dbc4e1c1 5844/* The screen has been cleared so we may have changed foreground or
ab648270
JB
5845 background colors, and the scroll bars may need to be redrawn.
5846 Clear out the scroll bars, and ask for expose events, so we can
dbc4e1c1
JB
5847 redraw them. */
5848
dfcf069d 5849void
ab648270 5850x_scroll_bar_clear (f)
dbc4e1c1
JB
5851 FRAME_PTR f;
5852{
06a2c219 5853#ifndef USE_TOOLKIT_SCROLL_BARS
dbc4e1c1
JB
5854 Lisp_Object bar;
5855
b80c363e
RS
5856 /* We can have scroll bars even if this is 0,
5857 if we just turned off scroll bar mode.
5858 But in that case we should not clear them. */
5859 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5860 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5861 bar = XSCROLL_BAR (bar)->next)
c5e6e06b 5862 XClearArea (FRAME_X_DISPLAY (f),
056ce195 5863 XSCROLL_BAR (bar)->x_window,
b80c363e 5864 0, 0, 0, 0, True);
06a2c219 5865#endif /* not USE_TOOLKIT_SCROLL_BARS */
dbc4e1c1
JB
5866}
5867
09756a85 5868\f
f451eb13 5869/* The main X event-reading loop - XTread_socket. */
dc6f92b8 5870
042c33d3 5871#if 0
06a2c219 5872/* Time stamp of enter window event. This is only used by XTread_socket,
dc6f92b8
JB
5873 but we have to put it out here, since static variables within functions
5874 sometimes don't work. */
06a2c219 5875
dc6f92b8 5876static Time enter_timestamp;
042c33d3 5877#endif
dc6f92b8 5878
11edeb03 5879/* This holds the state XLookupString needs to implement dead keys
58769bee 5880 and other tricks known as "compose processing". _X Window System_
11edeb03
JB
5881 says that a portable program can't use this, but Stephen Gildea assures
5882 me that letting the compiler initialize it to zeros will work okay.
5883
5884 This must be defined outside of XTread_socket, for the same reasons
f7d40b3b 5885 given for enter_timestamp, above. */
06a2c219 5886
11edeb03
JB
5887static XComposeStatus compose_status;
5888
10e6549c
RS
5889/* Record the last 100 characters stored
5890 to help debug the loss-of-chars-during-GC problem. */
06a2c219 5891
2224b905
RS
5892static int temp_index;
5893static short temp_buffer[100];
10e6549c 5894
6b61353c
KH
5895#define STORE_KEYSYM_FOR_DEBUG(keysym) \
5896 if (temp_index == sizeof temp_buffer / sizeof (short)) \
5897 temp_index = 0; \
5898 temp_buffer[temp_index++] = (keysym)
5899
7a13e894
RS
5900/* Set this to nonzero to fake an "X I/O error"
5901 on a particular display. */
06a2c219 5902
7a13e894
RS
5903struct x_display_info *XTread_socket_fake_io_error;
5904
2224b905
RS
5905/* When we find no input here, we occasionally do a no-op command
5906 to verify that the X server is still running and we can still talk with it.
5907 We try all the open displays, one by one.
5908 This variable is used for cycling thru the displays. */
06a2c219 5909
2224b905
RS
5910static struct x_display_info *next_noop_dpyinfo;
5911
06a2c219
GM
5912#define SET_SAVED_MENU_EVENT(size) \
5913 do \
5914 { \
5915 if (f->output_data.x->saved_menu_event == 0) \
5916 f->output_data.x->saved_menu_event \
5917 = (XEvent *) xmalloc (sizeof (XEvent)); \
5918 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9b516537
AS
5919 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT; \
5920 XSETFRAME (inev.ie.frame_or_window, f); \
06a2c219
GM
5921 } \
5922 while (0)
5923
8805890a 5924#define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
06a2c219 5925#define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8805890a 5926
dc6f92b8 5927
bf338245
JD
5928enum
5929{
5930 X_EVENT_NORMAL,
5931 X_EVENT_GOTO_OUT,
5932 X_EVENT_DROP
5933};
dc6f92b8 5934
1fcfb866
JD
5935/* Filter events for the current X input method.
5936 DPYINFO is the display this event is for.
5937 EVENT is the X event to filter.
5938
5939 Returns non-zero if the event was filtered, caller shall not process
5940 this event further.
5941 Returns zero if event is wasn't filtered. */
177c0ea7 5942
1fcfb866
JD
5943#ifdef HAVE_X_I18N
5944static int
5945x_filter_event (dpyinfo, event)
5946 struct x_display_info *dpyinfo;
5947 XEvent *event;
5948{
5949 /* XFilterEvent returns non-zero if the input method has
5950 consumed the event. We pass the frame's X window to
5951 XFilterEvent because that's the one for which the IC
5952 was created. */
5953
5954 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5955 event->xclient.window);
5956
5957 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5958}
5959#endif
5960
488dd4c4 5961#ifdef USE_GTK
488dd4c4
JD
5962static int current_count;
5963static int current_finish;
6b61353c 5964static struct input_event *current_hold_quit;
488dd4c4
JD
5965
5966/* This is the filter function invoked by the GTK event loop.
5967 It is invoked before the XEvent is translated to a GdkEvent,
6b61353c 5968 so we have a chance to act on the event before GTK. */
488dd4c4
JD
5969static GdkFilterReturn
5970event_handler_gdk (gxev, ev, data)
5971 GdkXEvent *gxev;
5972 GdkEvent *ev;
5973 gpointer data;
5974{
6b61353c 5975 XEvent *xev = (XEvent *) gxev;
488dd4c4 5976
6b61353c 5977 if (current_count >= 0)
1fcfb866 5978 {
6b61353c
KH
5979 struct x_display_info *dpyinfo;
5980
5981 dpyinfo = x_display_info_for_display (xev->xany.display);
5982
1fcfb866
JD
5983#ifdef HAVE_X_I18N
5984 /* Filter events for the current X input method.
5985 GTK calls XFilterEvent but not for key press and release,
5986 so we do it here. */
5987 if (xev->type == KeyPress || xev->type == KeyRelease)
6b61353c 5988 if (dpyinfo && x_filter_event (dpyinfo, xev))
1fcfb866
JD
5989 return GDK_FILTER_REMOVE;
5990#endif
6b61353c
KH
5991
5992 if (! dpyinfo)
5993 current_finish = X_EVENT_NORMAL;
5994 else
5995 {
5996 current_count +=
5997 handle_one_xevent (dpyinfo, xev, &current_finish,
5998 current_hold_quit);
5999 }
1fcfb866 6000 }
488dd4c4 6001 else
6b61353c 6002 current_finish = x_dispatch_event (xev, xev->xany.display);
488dd4c4
JD
6003
6004 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
6005 return GDK_FILTER_REMOVE;
6006
6007 return GDK_FILTER_CONTINUE;
6008}
6009#endif /* USE_GTK */
6010
6011
bf338245 6012/* Handles the XEvent EVENT on display DPYINFO.
177c0ea7 6013
bf338245
JD
6014 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
6015 *FINISH is zero if caller should continue reading events.
6016 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
6017
bf338245 6018 We return the number of characters stored into the buffer. */
177c0ea7 6019
6f2a7a68 6020static int
6b61353c 6021handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
bf338245
JD
6022 struct x_display_info *dpyinfo;
6023 XEvent *eventp;
bf338245 6024 int *finish;
6b61353c 6025 struct input_event *hold_quit;
dc6f92b8 6026{
9b516537
AS
6027 union {
6028 struct input_event ie;
6029 struct selection_input_event sie;
6030 } inev;
dc6f92b8 6031 int count = 0;
6b61353c 6032 int do_help = 0;
dc6f92b8 6033 int nbytes = 0;
6ed8eeff 6034 struct frame *f = NULL;
379b5ac0 6035 struct coding_system coding;
bf338245 6036 XEvent event = *eventp;
dc6f92b8 6037
bf338245 6038 *finish = X_EVENT_NORMAL;
177c0ea7 6039
9b516537
AS
6040 EVENT_INIT (inev.ie);
6041 inev.ie.kind = NO_EVENT;
6042 inev.ie.arg = Qnil;
6b61353c 6043
bf338245 6044 switch (event.type)
7a13e894 6045 {
bf338245
JD
6046 case ClientMessage:
6047 {
6048 if (event.xclient.message_type
6049 == dpyinfo->Xatom_wm_protocols
6050 && event.xclient.format == 32)
6051 {
6052 if (event.xclient.data.l[0]
6053 == dpyinfo->Xatom_wm_take_focus)
6054 {
6055 /* Use x_any_window_to_frame because this
6056 could be the shell widget window
6057 if the frame has no title bar. */
6058 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6c183ba5 6059#ifdef HAVE_X_I18N
bf338245
JD
6060 /* Not quite sure this is needed -pd */
6061 if (f && FRAME_XIC (f))
6062 XSetICFocus (FRAME_XIC (f));
6c183ba5 6063#endif
f1da8f06
GM
6064#if 0 /* Emacs sets WM hints whose `input' field is `true'. This
6065 instructs the WM to set the input focus automatically for
6066 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
6067 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
6068 it has set the focus. So, XSetInputFocus below is not
6069 needed.
6070
6071 The call to XSetInputFocus below has also caused trouble. In
6072 cases where the XSetInputFocus done by the WM and the one
6073 below are temporally close (on a fast machine), the call
6074 below can generate additional FocusIn events which confuse
6075 Emacs. */
7d0393cf 6076
bf338245
JD
6077 /* Since we set WM_TAKE_FOCUS, we must call
6078 XSetInputFocus explicitly. But not if f is null,
6079 since that might be an event for a deleted frame. */
6080 if (f)
6081 {
6082 Display *d = event.xclient.display;
6083 /* Catch and ignore errors, in case window has been
6084 iconified by a window manager such as GWM. */
9ba8e10d 6085 x_catch_errors (d);
bf338245
JD
6086 XSetInputFocus (d, event.xclient.window,
6087 /* The ICCCM says this is
6088 the only valid choice. */
6089 RevertToParent,
6090 event.xclient.data.l[1]);
6091 /* This is needed to detect the error
6092 if there is an error. */
6093 XSync (d, False);
4545fa20 6094 x_uncatch_errors ();
bf338245
JD
6095 }
6096 /* Not certain about handling scroll bars here */
f1da8f06 6097#endif /* 0 */
6b61353c 6098 goto done;
bf338245 6099 }
6b61353c
KH
6100
6101 if (event.xclient.data.l[0]
bf338245
JD
6102 == dpyinfo->Xatom_wm_save_yourself)
6103 {
6104 /* Save state modify the WM_COMMAND property to
6105 something which can reinstate us. This notifies
6106 the session manager, who's looking for such a
6107 PropertyNotify. Can restart processing when
6108 a keyboard or mouse event arrives. */
6109 /* If we have a session manager, don't set this.
6110 KDE will then start two Emacsen, one for the
6111 session manager and one for this. */
5f30b957 6112#ifdef HAVE_X_SM
6b61353c 6113 if (! x_session_have_connection ())
5f30b957 6114#endif
bf338245
JD
6115 {
6116 f = x_top_window_to_frame (dpyinfo,
6117 event.xclient.window);
6118 /* This is just so we only give real data once
6119 for a single Emacs process. */
6120 if (f == SELECTED_FRAME ())
6121 XSetCommand (FRAME_X_DISPLAY (f),
6122 event.xclient.window,
6123 initial_argv, initial_argc);
6124 else if (f)
6125 XSetCommand (FRAME_X_DISPLAY (f),
6126 event.xclient.window,
6127 0, 0);
6128 }
6b61353c 6129 goto done;
bf338245 6130 }
6b61353c
KH
6131
6132 if (event.xclient.data.l[0]
6133 == dpyinfo->Xatom_wm_delete_window)
bf338245 6134 {
6b61353c 6135 f = x_any_window_to_frame (dpyinfo,
bf338245 6136 event.xclient.window);
6b61353c
KH
6137 if (!f)
6138 goto OTHER; /* May be a dialog that is to be removed */
7a13e894 6139
9b516537
AS
6140 inev.ie.kind = DELETE_WINDOW_EVENT;
6141 XSETFRAME (inev.ie.frame_or_window, f);
6b61353c 6142 goto done;
bf338245 6143 }
6b61353c
KH
6144
6145 goto done;
bf338245 6146 }
6b61353c
KH
6147
6148 if (event.xclient.message_type
bf338245
JD
6149 == dpyinfo->Xatom_wm_configure_denied)
6150 {
6b61353c 6151 goto done;
bf338245 6152 }
6b61353c
KH
6153
6154 if (event.xclient.message_type
6155 == dpyinfo->Xatom_wm_window_moved)
bf338245
JD
6156 {
6157 int new_x, new_y;
6b61353c 6158 f = x_window_to_frame (dpyinfo, event.xclient.window);
bf338245
JD
6159
6160 new_x = event.xclient.data.s[0];
6161 new_y = event.xclient.data.s[1];
6162
6163 if (f)
6164 {
0899d58c
KS
6165 f->left_pos = new_x;
6166 f->top_pos = new_y;
bf338245 6167 }
6b61353c 6168 goto done;
bf338245 6169 }
6b61353c 6170
0fdff6bb 6171#ifdef HACK_EDITRES
6b61353c
KH
6172 if (event.xclient.message_type
6173 == dpyinfo->Xatom_editres)
bf338245 6174 {
6b61353c 6175 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
79fb0ab7
JD
6176 if (f)
6177 _XEditResCheckMessages (f->output_data.x->widget, NULL,
6178 &event, NULL);
6b61353c 6179 goto done;
bf338245 6180 }
0fdff6bb 6181#endif /* HACK_EDITRES */
6b61353c
KH
6182
6183 if ((event.xclient.message_type
6184 == dpyinfo->Xatom_DONE)
6185 || (event.xclient.message_type
6186 == dpyinfo->Xatom_PAGE))
bf338245
JD
6187 {
6188 /* Ghostview job completed. Kill it. We could
6189 reply with "Next" if we received "Page", but we
6190 currently never do because we are interested in
6191 images, only, which should have 1 page. */
6192 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
6b61353c 6193 f = x_window_to_frame (dpyinfo, event.xclient.window);
fb6341e7
KS
6194 if (!f)
6195 goto OTHER;
bf338245
JD
6196 x_kill_gs_process (pixmap, f);
6197 expose_frame (f, 0, 0, 0, 0);
6b61353c 6198 goto done;
bf338245 6199 }
6b61353c 6200
06a2c219 6201#ifdef USE_TOOLKIT_SCROLL_BARS
bf338245
JD
6202 /* Scroll bar callbacks send a ClientMessage from which
6203 we construct an input_event. */
6b61353c
KH
6204 if (event.xclient.message_type
6205 == dpyinfo->Xatom_Scrollbar)
bf338245 6206 {
9b516537 6207 x_scroll_bar_to_input_event (&event, &inev.ie);
6b61353c
KH
6208 *finish = X_EVENT_GOTO_OUT;
6209 goto done;
bf338245 6210 }
06a2c219 6211#endif /* USE_TOOLKIT_SCROLL_BARS */
6b61353c 6212
81c02ebe
JD
6213 /* XEmbed messages from the embedder (if any). */
6214 if (event.xclient.message_type
6215 == dpyinfo->Xatom_XEMBED)
6216 {
6217 enum xembed_message msg = event.xclient.data.l[1];
6218 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
6219 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6220
6221 *finish = X_EVENT_GOTO_OUT;
6222 goto done;
6223 }
6224
6b61353c 6225 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6b61353c
KH
6226 if (!f)
6227 goto OTHER;
9b516537 6228 if (x_handle_dnd_message (f, &event.xclient, dpyinfo, &inev.ie))
6b61353c 6229 *finish = X_EVENT_DROP;
bf338245
JD
6230 }
6231 break;
dc6f92b8 6232
bf338245 6233 case SelectionNotify:
1c8591d0 6234 last_user_time = event.xselection.time;
3afe33e7 6235#ifdef USE_X_TOOLKIT
bf338245
JD
6236 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
6237 goto OTHER;
3afe33e7 6238#endif /* not USE_X_TOOLKIT */
bf338245
JD
6239 x_handle_selection_notify (&event.xselection);
6240 break;
d56a553a 6241
bf338245 6242 case SelectionClear: /* Someone has grabbed ownership. */
1c8591d0 6243 last_user_time = event.xselectionclear.time;
3afe33e7 6244#ifdef USE_X_TOOLKIT
bf338245
JD
6245 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
6246 goto OTHER;
3afe33e7 6247#endif /* USE_X_TOOLKIT */
bf338245
JD
6248 {
6249 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
d56a553a 6250
9b516537
AS
6251 inev.ie.kind = SELECTION_CLEAR_EVENT;
6252 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6253 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6254 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6255 inev.ie.frame_or_window = Qnil;
bf338245
JD
6256 }
6257 break;
dc6f92b8 6258
bf338245 6259 case SelectionRequest: /* Someone wants our selection. */
1c8591d0 6260 last_user_time = event.xselectionrequest.time;
3afe33e7 6261#ifdef USE_X_TOOLKIT
bf338245
JD
6262 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
6263 goto OTHER;
3afe33e7 6264#endif /* USE_X_TOOLKIT */
958f04e8 6265 {
bf338245
JD
6266 XSelectionRequestEvent *eventp
6267 = (XSelectionRequestEvent *) &event;
6268
9b516537
AS
6269 inev.ie.kind = SELECTION_REQUEST_EVENT;
6270 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6271 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
6272 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6273 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
6274 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
6275 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6276 inev.ie.frame_or_window = Qnil;
958f04e8 6277 }
bf338245 6278 break;
7a13e894 6279
bf338245 6280 case PropertyNotify:
1c8591d0 6281 last_user_time = event.xproperty.time;
1d2b2268
GM
6282#if 0 /* This is plain wrong. In the case that we are waiting for a
6283 PropertyNotify used as an ACK in incremental selection
6284 transfer, the property will be on the receiver's window. */
6285#if defined USE_X_TOOLKIT
bf338245
JD
6286 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
6287 goto OTHER;
1d2b2268
GM
6288#endif
6289#endif
bf338245
JD
6290 x_handle_property_notify (&event.xproperty);
6291 goto OTHER;
dc6f92b8 6292
bf338245
JD
6293 case ReparentNotify:
6294 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
6295 if (f)
6296 {
6297 int x, y;
6298 f->output_data.x->parent_desc = event.xreparent.parent;
6299 x_real_positions (f, &x, &y);
0899d58c
KS
6300 f->left_pos = x;
6301 f->top_pos = y;
6b61353c
KH
6302
6303 /* Perhaps reparented due to a WM restart. Reset this. */
6304 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
1c8591d0 6305 FRAME_X_DISPLAY_INFO (f)->net_supported_window = 0;
bf338245 6306 }
15213141 6307 goto OTHER;
3bd330d4 6308
bf338245
JD
6309 case Expose:
6310 f = x_window_to_frame (dpyinfo, event.xexpose.window);
6311 if (f)
6312 {
6313 x_check_fullscreen (f);
d0fa3e56 6314
42d02da0
JD
6315#ifdef USE_GTK
6316 /* This seems to be needed for GTK 2.6. */
6317 x_clear_area (event.xexpose.display,
6318 event.xexpose.window,
6319 event.xexpose.x, event.xexpose.y,
6320 event.xexpose.width, event.xexpose.height,
6321 FALSE);
6322#endif
bf338245
JD
6323 if (f->async_visible == 0)
6324 {
6325 f->async_visible = 1;
6326 f->async_iconified = 0;
6327 f->output_data.x->has_been_visible = 1;
6328 SET_FRAME_GARBAGED (f);
6329 }
6330 else
0899d58c
KS
6331 expose_frame (f,
6332 event.xexpose.x, event.xexpose.y,
bf338245
JD
6333 event.xexpose.width, event.xexpose.height);
6334 }
6335 else
6336 {
12949a7f 6337#ifndef USE_TOOLKIT_SCROLL_BARS
bf338245 6338 struct scroll_bar *bar;
12949a7f 6339#endif
01f67d2c 6340#if defined USE_LUCID
bf338245
JD
6341 /* Submenus of the Lucid menu bar aren't widgets
6342 themselves, so there's no way to dispatch events
6343 to them. Recognize this case separately. */
6344 {
6345 Widget widget
6346 = x_window_to_menu_bar (event.xexpose.window);
6347 if (widget)
6348 xlwmenu_redisplay (widget);
6349 }
01f67d2c
PJ
6350#endif /* USE_LUCID */
6351
06a2c219 6352#ifdef USE_TOOLKIT_SCROLL_BARS
bf338245
JD
6353 /* Dispatch event to the widget. */
6354 goto OTHER;
06a2c219 6355#else /* not USE_TOOLKIT_SCROLL_BARS */
6b61353c
KH
6356 bar = x_window_to_scroll_bar (event.xexpose.display,
6357 event.xexpose.window);
58769bee 6358
bf338245
JD
6359 if (bar)
6360 x_scroll_bar_expose (bar, &event);
3afe33e7 6361#ifdef USE_X_TOOLKIT
bf338245
JD
6362 else
6363 goto OTHER;
3afe33e7 6364#endif /* USE_X_TOOLKIT */
06a2c219 6365#endif /* not USE_TOOLKIT_SCROLL_BARS */
bf338245
JD
6366 }
6367 break;
dc6f92b8 6368
bf338245
JD
6369 case GraphicsExpose: /* This occurs when an XCopyArea's
6370 source area was obscured or not
6371 available. */
6372 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
6373 if (f)
6374 {
6375 expose_frame (f,
6376 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
6377 event.xgraphicsexpose.width,
6378 event.xgraphicsexpose.height);
6379 }
3afe33e7 6380#ifdef USE_X_TOOLKIT
bf338245
JD
6381 else
6382 goto OTHER;
3afe33e7 6383#endif /* USE_X_TOOLKIT */
bf338245 6384 break;
7d0393cf 6385
bf338245
JD
6386 case NoExpose: /* This occurs when an XCopyArea's
6387 source area was completely
6388 available. */
6389 break;
dc6f92b8 6390
bf338245
JD
6391 case UnmapNotify:
6392 /* Redo the mouse-highlight after the tooltip has gone. */
6393 if (event.xmap.window == tip_window)
6394 {
6395 tip_window = 0;
6396 redo_mouse_highlight ();
6397 }
bddd097c 6398
bf338245
JD
6399 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
6400 if (f) /* F may no longer exist if
6401 the frame was deleted. */
6402 {
6403 /* While a frame is unmapped, display generation is
6404 disabled; you don't want to spend time updating a
6405 display that won't ever be seen. */
6406 f->async_visible = 0;
6407 /* We can't distinguish, from the event, whether the window
6408 has become iconified or invisible. So assume, if it
6409 was previously visible, than now it is iconified.
6410 But x_make_frame_invisible clears both
6411 the visible flag and the iconified flag;
6412 and that way, we know the window is not iconified now. */
6413 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
6414 {
6415 f->async_iconified = 1;
dc6f92b8 6416
9b516537
AS
6417 inev.ie.kind = ICONIFY_EVENT;
6418 XSETFRAME (inev.ie.frame_or_window, f);
bf338245
JD
6419 }
6420 }
6421 goto OTHER;
6422
6423 case MapNotify:
6424 if (event.xmap.window == tip_window)
6425 /* The tooltip has been drawn already. Avoid
6426 the SET_FRAME_GARBAGED below. */
6427 goto OTHER;
6428
6429 /* We use x_top_window_to_frame because map events can
6430 come for sub-windows and they don't mean that the
6431 frame is visible. */
6432 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
6433 if (f)
6434 {
d64b707c 6435 /* wait_reading_process_output will notice this and update
bf338245
JD
6436 the frame's display structures.
6437 If we where iconified, we should not set garbaged,
6438 because that stops redrawing on Expose events. This looks
6439 bad if we are called from a recursive event loop
6440 (x_dispatch_event), for example when a dialog is up. */
6441 if (! f->async_iconified)
6442 SET_FRAME_GARBAGED (f);
6443
6444 f->async_visible = 1;
6445 f->async_iconified = 0;
6446 f->output_data.x->has_been_visible = 1;
6447
6448 if (f->iconified)
6449 {
9b516537
AS
6450 inev.ie.kind = DEICONIFY_EVENT;
6451 XSETFRAME (inev.ie.frame_or_window, f);
bf338245
JD
6452 }
6453 else if (! NILP (Vframe_list)
6454 && ! NILP (XCDR (Vframe_list)))
6455 /* Force a redisplay sooner or later
6456 to update the frame titles
6457 in case this is the second frame. */
6458 record_asynch_buffer_change ();
6459 }
6460 goto OTHER;
2a793c0c 6461
bf338245 6462 case KeyPress:
2a793c0c 6463
1c8591d0 6464 last_user_time = event.xkey.time;
6b61353c
KH
6465 ignore_next_mouse_click_timeout = 0;
6466
22174d10 6467#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
bf338245 6468 /* Dispatch KeyPress events when in menu. */
488dd4c4 6469 if (popup_activated ())
bf338245 6470 goto OTHER;
22174d10 6471#endif
488dd4c4 6472
bf338245 6473 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
f451eb13 6474
92daa848
RS
6475 /* If mouse-highlight is an integer, input clears out
6476 mouse highlighting. */
99c60e86 6477 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
295fa1d4
KS
6478 && (f == 0
6479 || !EQ (f->tool_bar_window, dpyinfo->mouse_face_window)))
bf338245 6480 {
bf338245 6481 clear_mouse_face (dpyinfo);
6b61353c 6482 dpyinfo->mouse_face_hidden = 1;
bf338245 6483 }
66301061 6484
eccc05db 6485#if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
bf338245
JD
6486 if (f == 0)
6487 {
6488 /* Scroll bars consume key events, but we want
6489 the keys to go to the scroll bar's frame. */
6490 Widget widget = XtWindowToWidget (dpyinfo->display,
6491 event.xkey.window);
6492 if (widget && XmIsScrollBar (widget))
6493 {
6494 widget = XtParent (widget);
6495 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6496 }
6497 }
eccc05db 6498#endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
06a2c219 6499
bf338245
JD
6500 if (f != 0)
6501 {
6502 KeySym keysym, orig_keysym;
6503 /* al%imercury@uunet.uu.net says that making this 81
6504 instead of 80 fixed a bug whereby meta chars made
6505 his Emacs hang.
6506
6507 It seems that some version of XmbLookupString has
6508 a bug of not returning XBufferOverflow in
6509 status_return even if the input is too long to
6510 fit in 81 bytes. So, we must prepare sufficient
6511 bytes for copy_buffer. 513 bytes (256 chars for
6512 two-byte character set) seems to be a fairly good
6513 approximation. -- 2000.8.10 handa@etl.go.jp */
6514 unsigned char copy_buffer[513];
6515 unsigned char *copy_bufptr = copy_buffer;
6516 int copy_bufsiz = sizeof (copy_buffer);
6517 int modifiers;
6518 Lisp_Object coding_system = Qlatin_1;
6b61353c
KH
6519 Lisp_Object c;
6520
6521#ifdef USE_GTK
6522 /* Don't pass keys to GTK. A Tab will shift focus to the
6523 tool bar in GTK 2.4. Keys will still go to menus and
6524 dialogs because in that case popup_activated is TRUE
6525 (see above). */
6526 *finish = X_EVENT_DROP;
6527#endif
bf338245
JD
6528
6529 event.xkey.state
6530 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
6531 extra_keyboard_modifiers);
6532 modifiers = event.xkey.state;
6533
6534 /* This will have to go some day... */
6535
6536 /* make_lispy_event turns chars into control chars.
6537 Don't do it here because XLookupString is too eager. */
6538 event.xkey.state &= ~ControlMask;
6539 event.xkey.state &= ~(dpyinfo->meta_mod_mask
6540 | dpyinfo->super_mod_mask
6541 | dpyinfo->hyper_mod_mask
6542 | dpyinfo->alt_mod_mask);
6543
6544 /* In case Meta is ComposeCharacter,
6545 clear its status. According to Markus Ehrnsperger
6546 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6547 this enables ComposeCharacter to work whether or
6548 not it is combined with Meta. */
6549 if (modifiers & dpyinfo->meta_mod_mask)
6550 bzero (&compose_status, sizeof (compose_status));
1cf4a0d1 6551
6c183ba5 6552#ifdef HAVE_X_I18N
bf338245
JD
6553 if (FRAME_XIC (f))
6554 {
6555 Status status_return;
6556
6557 coding_system = Vlocale_coding_system;
6558 nbytes = XmbLookupString (FRAME_XIC (f),
6559 &event.xkey, copy_bufptr,
6560 copy_bufsiz, &keysym,
6561 &status_return);
6562 if (status_return == XBufferOverflow)
6563 {
6564 copy_bufsiz = nbytes + 1;
fa8459a3 6565 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
bf338245
JD
6566 nbytes = XmbLookupString (FRAME_XIC (f),
6567 &event.xkey, copy_bufptr,
6568 copy_bufsiz, &keysym,
6569 &status_return);
6570 }
6571 /* Xutf8LookupString is a new but already deprecated interface. -stef */
96035dca 6572#if 0 && defined X_HAVE_UTF8_STRING
bf338245
JD
6573 else if (status_return == XLookupKeySym)
6574 { /* Try again but with utf-8. */
6575 coding_system = Qutf_8;
6576 nbytes = Xutf8LookupString (FRAME_XIC (f),
6577 &event.xkey, copy_bufptr,
6578 copy_bufsiz, &keysym,
6579 &status_return);
6580 if (status_return == XBufferOverflow)
6581 {
6582 copy_bufsiz = nbytes + 1;
fa8459a3 6583 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
bf338245
JD
6584 nbytes = Xutf8LookupString (FRAME_XIC (f),
6585 &event.xkey,
6586 copy_bufptr,
6587 copy_bufsiz, &keysym,
6588 &status_return);
6589 }
6590 }
c997eae5 6591#endif
f5d11644 6592
bf338245
JD
6593 if (status_return == XLookupNone)
6594 break;
6595 else if (status_return == XLookupChars)
6596 {
6597 keysym = NoSymbol;
6598 modifiers = 0;
6599 }
6600 else if (status_return != XLookupKeySym
6601 && status_return != XLookupBoth)
6602 abort ();
6603 }
6604 else
6605 nbytes = XLookupString (&event.xkey, copy_bufptr,
6606 copy_bufsiz, &keysym,
6607 &compose_status);
6c183ba5 6608#else
bf338245
JD
6609 nbytes = XLookupString (&event.xkey, copy_bufptr,
6610 copy_bufsiz, &keysym,
6611 &compose_status);
6c183ba5 6612#endif
dc6f92b8 6613
6b61353c
KH
6614 /* If not using XIM/XIC, and a compose sequence is in progress,
6615 we break here. Otherwise, chars_matched is always 0. */
6616 if (compose_status.chars_matched > 0 && nbytes == 0)
6617 break;
6618
a6d0ae55 6619 bzero (&compose_status, sizeof (compose_status));
bf338245 6620 orig_keysym = keysym;
55123275 6621
057a9ab4 6622 /* Common for all keysym input events. */
60c73d2e
KL
6623 XSETFRAME (inev.ie.frame_or_window, f);
6624 inev.ie.modifiers
057a9ab4 6625 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), modifiers);
60c73d2e 6626 inev.ie.timestamp = event.xkey.time;
057a9ab4
KL
6627
6628 /* First deal with keysyms which have defined
6629 translations to characters. */
6630 if (keysym >= 32 && keysym < 128)
6631 /* Avoid explicitly decoding each ASCII character. */
6632 {
60c73d2e
KL
6633 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6634 inev.ie.code = keysym;
6b61353c
KH
6635 goto done_keysym;
6636 }
6637
e5e40bff 6638 /* Keysyms directly mapped to Unicode characters. */
05e9a052 6639 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
e5e40bff 6640 {
05e9a052
KH
6641 if (keysym < 0x01000080)
6642 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6643 else
6644 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
e5e40bff
KH
6645 inev.ie.code = keysym & 0xFFFFFF;
6646 goto done_keysym;
6647 }
6648
6b61353c
KH
6649 /* Now non-ASCII. */
6650 if (HASH_TABLE_P (Vx_keysym_table)
6651 && (NATNUMP (c = Fgethash (make_number (keysym),
057a9ab4
KL
6652 Vx_keysym_table,
6653 Qnil))))
6654 {
60c73d2e
KL
6655 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6656 ? ASCII_KEYSTROKE_EVENT
6657 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6658 inev.ie.code = XFASTINT (c);
057a9ab4
KL
6659 goto done_keysym;
6660 }
6661
6662 /* Random non-modifier sorts of keysyms. */
6663 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
bf338245 6664 || keysym == XK_Delete
1097aea0 6665#ifdef XK_ISO_Left_Tab
bf338245
JD
6666 || (keysym >= XK_ISO_Left_Tab
6667 && keysym <= XK_ISO_Enter)
1097aea0 6668#endif
bf338245
JD
6669 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6670 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
c34790e0 6671#ifdef HPUX
bf338245
JD
6672 /* This recognizes the "extended function
6673 keys". It seems there's no cleaner way.
6674 Test IsModifierKey to avoid handling
6675 mode_switch incorrectly. */
6676 || ((unsigned) (keysym) >= XK_Select
6677 && (unsigned)(keysym) < XK_KP_Space)
69388238
RS
6678#endif
6679#ifdef XK_dead_circumflex
bf338245 6680 || orig_keysym == XK_dead_circumflex
69388238
RS
6681#endif
6682#ifdef XK_dead_grave
bf338245 6683 || orig_keysym == XK_dead_grave
69388238
RS
6684#endif
6685#ifdef XK_dead_tilde
bf338245 6686 || orig_keysym == XK_dead_tilde
69388238
RS
6687#endif
6688#ifdef XK_dead_diaeresis
bf338245 6689 || orig_keysym == XK_dead_diaeresis
69388238
RS
6690#endif
6691#ifdef XK_dead_macron
bf338245 6692 || orig_keysym == XK_dead_macron
69388238
RS
6693#endif
6694#ifdef XK_dead_degree
bf338245 6695 || orig_keysym == XK_dead_degree
69388238
RS
6696#endif
6697#ifdef XK_dead_acute
bf338245 6698 || orig_keysym == XK_dead_acute
69388238
RS
6699#endif
6700#ifdef XK_dead_cedilla
bf338245 6701 || orig_keysym == XK_dead_cedilla
69388238
RS
6702#endif
6703#ifdef XK_dead_breve
bf338245 6704 || orig_keysym == XK_dead_breve
69388238
RS
6705#endif
6706#ifdef XK_dead_ogonek
bf338245 6707 || orig_keysym == XK_dead_ogonek
69388238
RS
6708#endif
6709#ifdef XK_dead_caron
bf338245 6710 || orig_keysym == XK_dead_caron
69388238
RS
6711#endif
6712#ifdef XK_dead_doubleacute
bf338245 6713 || orig_keysym == XK_dead_doubleacute
69388238
RS
6714#endif
6715#ifdef XK_dead_abovedot
bf338245 6716 || orig_keysym == XK_dead_abovedot
c34790e0 6717#endif
bf338245
JD
6718 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6719 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6720 /* Any "vendor-specific" key is ok. */
6721 || (orig_keysym & (1 << 28))
6722 || (keysym != NoSymbol && nbytes == 0))
6723 && ! (IsModifierKey (orig_keysym)
bf338245
JD
6724 /* The symbols from XK_ISO_Lock
6725 to XK_ISO_Last_Group_Lock
6726 don't have real modifiers but
6727 should be treated similarly to
6728 Mode_switch by Emacs. */
7ef18d79 6729#if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
bf338245
JD
6730 || ((unsigned)(orig_keysym)
6731 >= XK_ISO_Lock
6732 && (unsigned)(orig_keysym)
6733 <= XK_ISO_Last_Group_Lock)
7ef18d79 6734#endif
bf338245 6735 ))
6b61353c
KH
6736 {
6737 STORE_KEYSYM_FOR_DEBUG (keysym);
6738 /* make_lispy_event will convert this to a symbolic
6739 key. */
9b516537
AS
6740 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
6741 inev.ie.code = keysym;
6b61353c
KH
6742 goto done_keysym;
6743 }
379b5ac0 6744
6b61353c
KH
6745 { /* Raw bytes, not keysym. */
6746 register int i;
6747 register int c;
6748 int nchars, len;
379b5ac0 6749
6b61353c
KH
6750 for (i = 0, nchars = 0; i < nbytes; i++)
6751 {
6752 if (ASCII_BYTE_P (copy_bufptr[i]))
6753 nchars++;
6754 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
6755 }
bf338245 6756
6b61353c
KH
6757 if (nchars < nbytes)
6758 {
6759 /* Decode the input data. */
6760 int require;
6761 unsigned char *p;
6762
6763 /* The input should be decoded with `coding_system'
6764 which depends on which X*LookupString function
6765 we used just above and the locale. */
6766 setup_coding_system (coding_system, &coding);
6767 coding.src_multibyte = 0;
6768 coding.dst_multibyte = 1;
6769 /* The input is converted to events, thus we can't
6770 handle composition. Anyway, there's no XIM that
6771 gives us composition information. */
6772 coding.common_flags &= ~CODING_ANNOTATION_MASK;
6773
6774 require = MAX_MULTIBYTE_LENGTH * nbytes;
6775 coding.destination = alloca (require);
6776 coding.dst_bytes = require;
6777 coding.mode |= CODING_MODE_LAST_BLOCK;
6778 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
6779 nbytes = coding.produced;
6780 nchars = coding.produced_char;
6781 copy_bufptr = coding.destination;
6782 }
bf338245 6783
985773c9
MB
6784 /* Convert the input data to a sequence of
6785 character events. */
6786 for (i = 0; i < nbytes; i += len)
6787 {
6788 if (nchars == nbytes)
6789 c = copy_bufptr[i], len = 1;
6790 else
6791 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
6792 nbytes - i, len);
6793 inev.ie.kind = (SINGLE_BYTE_CHAR_P (c)
6794 ? ASCII_KEYSTROKE_EVENT
6795 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6796 inev.ie.code = c;
6797 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6b61353c
KH
6798 }
6799
d6169680 6800 count += nchars;
6b61353c 6801
9b516537 6802 inev.ie.kind = NO_EVENT; /* Already stored above. */
6b61353c
KH
6803
6804 if (keysym == NoSymbol)
6805 break;
6806 }
bf338245 6807 }
6b61353c 6808 done_keysym:
59ddecde 6809#ifdef HAVE_X_I18N
bf338245
JD
6810 /* Don't dispatch this event since XtDispatchEvent calls
6811 XFilterEvent, and two calls in a row may freeze the
6812 client. */
6813 break;
59ddecde 6814#else
bf338245 6815 goto OTHER;
59ddecde 6816#endif
f451eb13 6817
bf338245 6818 case KeyRelease:
1c8591d0 6819 last_user_time = event.xkey.time;
59ddecde 6820#ifdef HAVE_X_I18N
bf338245
JD
6821 /* Don't dispatch this event since XtDispatchEvent calls
6822 XFilterEvent, and two calls in a row may freeze the
6823 client. */
6824 break;
59ddecde 6825#else
bf338245 6826 goto OTHER;
59ddecde 6827#endif
f5d11644 6828
bf338245 6829 case EnterNotify:
1c8591d0 6830 last_user_time = event.xcrossing.time;
9b516537 6831 x_detect_focus_change (dpyinfo, &event, &inev.ie);
cab34f50 6832
6b61353c 6833 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
7d0393cf 6834
6b61353c
KH
6835 if (f && x_mouse_click_focus_ignore_position)
6836 ignore_next_mouse_click_timeout = event.xmotion.time + 200;
06a2c219 6837
2c850e26 6838#if 0
6b61353c
KH
6839 if (event.xcrossing.focus)
6840 {
6841 /* Avoid nasty pop/raise loops. */
6842 if (f && (!(f->auto_raise)
6843 || !(f->auto_lower)
6844 || (event.xcrossing.time - enter_timestamp) > 500))
6845 {
6846 x_new_focus_frame (dpyinfo, f);
6847 enter_timestamp = event.xcrossing.time;
6848 }
6849 }
6850 else if (f == dpyinfo->x_focus_frame)
6851 x_new_focus_frame (dpyinfo, 0);
bf338245 6852#endif
f9e24cb9 6853
6b61353c
KH
6854 /* EnterNotify counts as mouse movement,
6855 so update things that depend on mouse position. */
6856 if (f && !f->output_data.x->hourglass_p)
6857 note_mouse_movement (f, &event.xmotion);
a8f116e7
JD
6858#ifdef USE_GTK
6859 /* We may get an EnterNotify on the buttons in the toolbar. In that
6860 case we moved out of any highlighted area and need to note this. */
6861 if (!f && last_mouse_glyph_frame)
44342f4b 6862 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
a8f116e7 6863#endif
6b61353c 6864 goto OTHER;
cab34f50 6865
bf338245 6866 case FocusIn:
9b516537 6867 x_detect_focus_change (dpyinfo, &event, &inev.ie);
bf338245 6868 goto OTHER;
7a13e894 6869
bf338245 6870 case LeaveNotify:
1c8591d0 6871 last_user_time = event.xcrossing.time;
9b516537 6872 x_detect_focus_change (dpyinfo, &event, &inev.ie);
cab34f50 6873
bf338245
JD
6874 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
6875 if (f)
6876 {
6877 if (f == dpyinfo->mouse_face_mouse_frame)
6878 {
6879 /* If we move outside the frame, then we're
6880 certainly no longer on any text in the frame. */
6881 clear_mouse_face (dpyinfo);
6882 dpyinfo->mouse_face_mouse_frame = 0;
6883 }
6c183ba5 6884
bf338245
JD
6885 /* Generate a nil HELP_EVENT to cancel a help-echo.
6886 Do it only if there's something to cancel.
6887 Otherwise, the startup message is cleared when
6888 the mouse leaves the frame. */
6889 if (any_help_event_p)
6b61353c 6890 do_help = -1;
bf338245 6891 }
a8f116e7
JD
6892#ifdef USE_GTK
6893 /* See comment in EnterNotify above */
6894 else if (last_mouse_glyph_frame)
44342f4b 6895 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
a8f116e7 6896#endif
bf338245 6897 goto OTHER;
7d0393cf 6898
bf338245 6899 case FocusOut:
9b516537 6900 x_detect_focus_change (dpyinfo, &event, &inev.ie);
bf338245 6901 goto OTHER;
5bc62483 6902
bf338245
JD
6903 case MotionNotify:
6904 {
1c8591d0 6905 last_user_time = event.xmotion.time;
442a09ea 6906 previous_help_echo_string = help_echo_string;
bb339c57 6907 help_echo_string = Qnil;
bf338245
JD
6908
6909 if (dpyinfo->grabbed && last_mouse_frame
6910 && FRAME_LIVE_P (last_mouse_frame))
6911 f = last_mouse_frame;
6912 else
6913 f = x_window_to_frame (dpyinfo, event.xmotion.window);
6914
6915 if (dpyinfo->mouse_face_hidden)
6916 {
6917 dpyinfo->mouse_face_hidden = 0;
6918 clear_mouse_face (dpyinfo);
6919 }
6920
6921 if (f)
6922 {
6923
6924 /* Generate SELECT_WINDOW_EVENTs when needed. */
92b23323 6925 if (!NILP (Vmouse_autoselect_window))
bf338245
JD
6926 {
6927 Lisp_Object window;
bf338245
JD
6928
6929 window = window_from_coordinates (f,
6930 event.xmotion.x, event.xmotion.y,
0899d58c 6931 0, 0, 0, 0);
bf338245
JD
6932
6933 /* Window will be selected only when it is not selected now and
6934 last mouse movement event was not in it. Minibuffer window
e0f24100 6935 will be selected only when it is active. */
6b61353c 6936 if (WINDOWP (window)
bf338245 6937 && !EQ (window, last_window)
50fbcdcb
MR
6938 && !EQ (window, selected_window)
6939 /* For click-to-focus window managers
6940 create event iff we don't leave the
6941 selected frame. */
6942 && (focus_follows_mouse
6943 || (EQ (XWINDOW (window)->frame,
6944 XWINDOW (selected_window)->frame))))
bf338245 6945 {
9b516537
AS
6946 inev.ie.kind = SELECT_WINDOW_EVENT;
6947 inev.ie.frame_or_window = window;
bf338245 6948 }
5bc62483 6949
bf338245
JD
6950 last_window=window;
6951 }
bb339c57
KS
6952 if (!note_mouse_movement (f, &event.xmotion))
6953 help_echo_string = previous_help_echo_string;
bf338245
JD
6954 }
6955 else
6956 {
e88b3c50 6957#ifndef USE_TOOLKIT_SCROLL_BARS
bf338245 6958 struct scroll_bar *bar
6b61353c
KH
6959 = x_window_to_scroll_bar (event.xmotion.display,
6960 event.xmotion.window);
f451eb13 6961
bf338245
JD
6962 if (bar)
6963 x_scroll_bar_note_movement (bar, &event);
e88b3c50 6964#endif /* USE_TOOLKIT_SCROLL_BARS */
b8009dd1 6965
bf338245
JD
6966 /* If we move outside the frame, then we're
6967 certainly no longer on any text in the frame. */
6968 clear_mouse_face (dpyinfo);
6969 }
6970
442a09ea 6971 /* If the contents of the global variable help_echo_string
bf338245 6972 has changed, generate a HELP_EVENT. */
442a09ea
KS
6973 if (!NILP (help_echo_string)
6974 || !NILP (previous_help_echo_string))
6b61353c 6975 do_help = 1;
bf338245
JD
6976 goto OTHER;
6977 }
dc6f92b8 6978
bf338245
JD
6979 case ConfigureNotify:
6980 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
6981 if (f)
6982 {
7a4bce14 6983#ifndef USE_X_TOOLKIT
488dd4c4
JD
6984#ifdef USE_GTK
6985 xg_resize_widgets (f, event.xconfigure.width,
6986 event.xconfigure.height);
6987#else /* not USE_GTK */
bf338245
JD
6988 /* If there is a pending resize for fullscreen, don't
6989 do this one, the right one will come later.
6990 The toolkit version doesn't seem to need this, but we
6991 need to reset it below. */
62fe13a4 6992 int dont_resize
0899d58c
KS
6993 = ((f->want_fullscreen & FULLSCREEN_WAIT)
6994 && f->new_text_cols != 0);
6995 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, event.xconfigure.height);
6996 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, event.xconfigure.width);
6997
bf338245
JD
6998 if (dont_resize)
6999 goto OTHER;
7000
7001 /* In the toolkit version, change_frame_size
7002 is called by the code that handles resizing
7003 of the EmacsFrame widget. */
7004
7005 /* Even if the number of character rows and columns has
7006 not changed, the font size may have changed, so we need
7007 to check the pixel dimensions as well. */
0899d58c
KS
7008 if (columns != FRAME_COLS (f)
7009 || rows != FRAME_LINES (f)
7010 || event.xconfigure.width != FRAME_PIXEL_WIDTH (f)
7011 || event.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
bf338245
JD
7012 {
7013 change_frame_size (f, rows, columns, 0, 1, 0);
7014 SET_FRAME_GARBAGED (f);
7015 cancel_mouse_face (f);
7016 }
488dd4c4 7017#endif /* not USE_GTK */
2d7fc7e8 7018#endif
af395ec1 7019
0899d58c
KS
7020 FRAME_PIXEL_WIDTH (f) = event.xconfigure.width;
7021 FRAME_PIXEL_HEIGHT (f) = event.xconfigure.height;
7a13e894 7022
488dd4c4
JD
7023#ifdef USE_GTK
7024 /* GTK creates windows but doesn't map them.
7025 Only get real positions and check fullscreen when mapped. */
7026 if (FRAME_GTK_OUTER_WIDGET (f)
7027 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
488dd4c4 7028#endif
0899d58c 7029 {
0899d58c
KS
7030 x_real_positions (f, &f->left_pos, &f->top_pos);
7031
0899d58c
KS
7032 if (f->want_fullscreen & FULLSCREEN_WAIT)
7033 f->want_fullscreen &= ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
488dd4c4 7034 }
0899d58c 7035
f5d11644 7036#ifdef HAVE_X_I18N
bf338245
JD
7037 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
7038 xic_set_statusarea (f);
f5d11644
GM
7039#endif
7040
bf338245
JD
7041 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
7042 {
7043 /* Since the WM decorations come below top_pos now,
7044 we must put them below top_pos in the future. */
0899d58c 7045 f->win_gravity = NorthWestGravity;
bf338245
JD
7046 x_wm_set_size_hint (f, (long) 0, 0);
7047 }
7048 }
7049 goto OTHER;
dc6f92b8 7050
bf338245 7051 case ButtonRelease:
488dd4c4 7052 case ButtonPress:
bf338245
JD
7053 {
7054 /* If we decide we want to generate an event to be seen
7055 by the rest of Emacs, we put it here. */
bf338245
JD
7056 int tool_bar_p = 0;
7057
bf338245 7058 bzero (&compose_status, sizeof (compose_status));
0a3fde9d 7059 last_mouse_glyph_frame = 0;
1c8591d0 7060 last_user_time = event.xbutton.time;
bf338245
JD
7061
7062 if (dpyinfo->grabbed
7063 && last_mouse_frame
7064 && FRAME_LIVE_P (last_mouse_frame))
7065 f = last_mouse_frame;
7066 else
7067 f = x_window_to_frame (dpyinfo, event.xbutton.window);
7068
7069 if (f)
7070 {
7071 /* Is this in the tool-bar? */
7072 if (WINDOWP (f->tool_bar_window)
0899d58c 7073 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
bf338245
JD
7074 {
7075 Lisp_Object window;
442a09ea
KS
7076 int x = event.xbutton.x;
7077 int y = event.xbutton.y;
06a2c219 7078
0899d58c 7079 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
a3a44a5a
JD
7080 tool_bar_p = EQ (window, f->tool_bar_window);
7081
7082 if (tool_bar_p && event.xbutton.button < 4)
bf338245 7083 {
056ce195
SM
7084 handle_tool_bar_click (f, x, y,
7085 event.xbutton.type == ButtonPress,
7086 x_x_to_emacs_modifiers (dpyinfo,
7087 event.xbutton.state));
bf338245
JD
7088 }
7089 }
06a2c219 7090
bf338245 7091 if (!tool_bar_p)
22174d10 7092#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
e8953ebf 7093 if (! popup_activated ())
22174d10 7094#endif
e8953ebf
JD
7095 {
7096 if (ignore_next_mouse_click_timeout)
7097 {
7098 if (event.type == ButtonPress
7099 && (int)(event.xbutton.time - ignore_next_mouse_click_timeout) > 0)
7100 {
7101 ignore_next_mouse_click_timeout = 0;
7102 construct_mouse_click (&inev.ie, &event.xbutton, f);
7103 }
7104 if (event.type == ButtonRelease)
7105 ignore_next_mouse_click_timeout = 0;
7106 }
7107 else
7108 construct_mouse_click (&inev.ie, &event.xbutton, f);
488dd4c4 7109 }
81c02ebe
JD
7110 if (FRAME_X_EMBEDDED_P (f))
7111 xembed_send_message (f, event.xbutton.time,
7112 XEMBED_REQUEST_FOCUS, 0, 0, 0);
bf338245
JD
7113 }
7114 else
7115 {
bf338245 7116 struct scroll_bar *bar
6b61353c
KH
7117 = x_window_to_scroll_bar (event.xbutton.display,
7118 event.xbutton.window);
f451eb13 7119
257f40f2
JD
7120#ifdef USE_TOOLKIT_SCROLL_BARS
7121 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
7122 scroll bars. */
7123 if (bar && event.xbutton.state & ControlMask)
7124 {
9b516537 7125 x_scroll_bar_handle_click (bar, &event, &inev.ie);
257f40f2
JD
7126 *finish = X_EVENT_DROP;
7127 }
7128#else /* not USE_TOOLKIT_SCROLL_BARS */
bf338245 7129 if (bar)
9b516537 7130 x_scroll_bar_handle_click (bar, &event, &inev.ie);
06a2c219 7131#endif /* not USE_TOOLKIT_SCROLL_BARS */
bf338245
JD
7132 }
7133
7134 if (event.type == ButtonPress)
7135 {
7136 dpyinfo->grabbed |= (1 << event.xbutton.button);
7137 last_mouse_frame = f;
bf338245
JD
7138
7139 if (!tool_bar_p)
7140 last_tool_bar_item = -1;
7141 }
7142 else
488dd4c4 7143 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
bf338245 7144
24aad441
KS
7145 /* Ignore any mouse motion that happened before this event;
7146 any subsequent mouse-movement Emacs events should reflect
7147 only motion after the ButtonPress/Release. */
7148 if (f != 0)
7149 f->mouse_moved = 0;
7150
488dd4c4 7151#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
bf338245
JD
7152 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
7153 /* For a down-event in the menu bar,
7154 don't pass it to Xt right now.
7155 Instead, save it away
7156 and we will pass it to Xt from kbd_buffer_get_event.
7157 That way, we can run some Lisp code first. */
488dd4c4
JD
7158 if (
7159#ifdef USE_GTK
7160 ! popup_activated ()
7161 &&
7162#endif
7163 f && event.type == ButtonPress
bf338245
JD
7164 /* Verify the event is really within the menu bar
7165 and not just sent to it due to grabbing. */
7166 && event.xbutton.x >= 0
0899d58c 7167 && event.xbutton.x < FRAME_PIXEL_WIDTH (f)
bf338245
JD
7168 && event.xbutton.y >= 0
7169 && event.xbutton.y < f->output_data.x->menubar_height
7170 && event.xbutton.same_screen)
7171 {
7172 SET_SAVED_BUTTON_EVENT;
7173 XSETFRAME (last_mouse_press_frame, f);
488dd4c4
JD
7174#ifdef USE_GTK
7175 *finish = X_EVENT_DROP;
7176#endif
bf338245
JD
7177 }
7178 else if (event.type == ButtonPress)
7179 {
7180 last_mouse_press_frame = Qnil;
7181 goto OTHER;
7182 }
06a2c219 7183
2237cac9
RS
7184#ifdef USE_MOTIF /* This should do not harm for Lucid,
7185 but I am trying to be cautious. */
bf338245
JD
7186 else if (event.type == ButtonRelease)
7187 {
7188 if (!NILP (last_mouse_press_frame))
7189 {
7190 f = XFRAME (last_mouse_press_frame);
7191 if (f->output_data.x)
7192 SET_SAVED_BUTTON_EVENT;
7193 }
7194 else
7195 goto OTHER;
7196 }
2237cac9 7197#endif /* USE_MOTIF */
bf338245
JD
7198 else
7199 goto OTHER;
488dd4c4 7200#endif /* USE_X_TOOLKIT || USE_GTK */
bf338245
JD
7201 }
7202 break;
dc6f92b8 7203
bf338245
JD
7204 case CirculateNotify:
7205 goto OTHER;
7d0393cf 7206
bf338245
JD
7207 case CirculateRequest:
7208 goto OTHER;
06a2c219 7209
bf338245
JD
7210 case VisibilityNotify:
7211 goto OTHER;
dc6f92b8 7212
bf338245
JD
7213 case MappingNotify:
7214 /* Someone has changed the keyboard mapping - update the
7215 local cache. */
7216 switch (event.xmapping.request)
7217 {
7218 case MappingModifier:
7219 x_find_modifier_meanings (dpyinfo);
7220 /* This is meant to fall through. */
7221 case MappingKeyboard:
7222 XRefreshKeyboardMapping (&event.xmapping);
7223 }
7224 goto OTHER;
dc6f92b8 7225
bf338245
JD
7226 default:
7227 OTHER:
717ca130 7228#ifdef USE_X_TOOLKIT
bf338245 7229 BLOCK_INPUT;
257f40f2
JD
7230 if (*finish != X_EVENT_DROP)
7231 XtDispatchEvent (&event);
bf338245 7232 UNBLOCK_INPUT;
3afe33e7 7233#endif /* USE_X_TOOLKIT */
bf338245
JD
7234 break;
7235 }
7236
6b61353c 7237 done:
9b516537 7238 if (inev.ie.kind != NO_EVENT)
6b61353c 7239 {
9b516537 7240 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6b61353c
KH
7241 count++;
7242 }
177c0ea7 7243
6b61353c
KH
7244 if (do_help
7245 && !(hold_quit && hold_quit->kind != NO_EVENT))
7246 {
7247 Lisp_Object frame;
bf338245 7248
6b61353c
KH
7249 if (f)
7250 XSETFRAME (frame, f);
7251 else
7252 frame = Qnil;
177c0ea7 7253
6b61353c
KH
7254 if (do_help > 0)
7255 {
7256 any_help_event_p = 1;
7257 gen_help_event (help_echo_string, frame, help_echo_window,
7258 help_echo_object, help_echo_pos);
7259 }
7260 else
7261 {
7262 help_echo_string = Qnil;
7263 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
7264 }
7265 count++;
7266 }
7267
7268 *eventp = event;
bf338245
JD
7269 return count;
7270}
7271
7272
7273/* Handles the XEvent EVENT on display DISPLAY.
7274 This is used for event loops outside the normal event handling,
1fcfb866
JD
7275 i.e. looping while a popup menu or a dialog is posted.
7276
7277 Returns the value handle_one_xevent sets in the finish argument. */
7278int
bf338245
JD
7279x_dispatch_event (event, display)
7280 XEvent *event;
7281 Display *display;
7282{
7283 struct x_display_info *dpyinfo;
1fcfb866 7284 int finish = X_EVENT_NORMAL;
177c0ea7 7285
6b61353c 7286 dpyinfo = x_display_info_for_display (display);
177c0ea7 7287
bf338245 7288 if (dpyinfo)
6b61353c 7289 handle_one_xevent (dpyinfo, event, &finish, 0);
1fcfb866
JD
7290
7291 return finish;
bf338245
JD
7292}
7293
7294
7295/* Read events coming from the X server.
7296 This routine is called by the SIGIO handler.
7297 We return as soon as there are no more events to be read.
7298
bf338245 7299 We return the number of characters stored into the buffer,
d6169680
JR
7300 thus pretending to be `read' (except the characters we store
7301 in the keyboard buffer can be multibyte, so are not necessarily
7302 C chars).
bf338245
JD
7303
7304 EXPECTED is nonzero if the caller knows input is available. */
7305
7306static int
6ed8eeff
KL
7307XTread_socket (terminal, expected, hold_quit)
7308 struct terminal *terminal;
bf338245 7309 int expected;
6b61353c 7310 struct input_event *hold_quit;
bf338245
JD
7311{
7312 int count = 0;
bf338245
JD
7313 XEvent event;
7314 int event_found = 0;
7315 struct x_display_info *dpyinfo;
7316
7317 if (interrupt_input_blocked)
7318 {
7319 interrupt_input_pending = 1;
7320 return -1;
7321 }
7322
7323 interrupt_input_pending = 0;
7324 BLOCK_INPUT;
7325
7326 /* So people can tell when we have read the available input. */
7327 input_signal_count++;
7328
bf338245
JD
7329 ++handling_signal;
7330
ab797f65
KL
7331#ifdef HAVE_X_SM
7332 /* Only check session manager input for the primary display. */
7333 if (terminal->id == 1 && x_session_have_connection ())
7334 {
7335 struct input_event inev;
7336 BLOCK_INPUT;
7337 /* We don't need to EVENT_INIT (inev) here, as
7338 x_session_check_input copies an entire input_event. */
7339 if (x_session_check_input (&inev))
7340 {
7341 kbd_buffer_store_event_hold (&inev, hold_quit);
7342 count++;
7343 }
7344 UNBLOCK_INPUT;
7345 }
7346#endif
7347
7348 /* For debugging, this gives a way to fake an I/O error. */
7349 if (terminal->display_info.x == XTread_socket_fake_io_error)
7350 {
7351 XTread_socket_fake_io_error = 0;
7352 x_io_error_quitter (dpyinfo->display);
7353 }
7354
7355#if 0 /* This loop is a noop now. */
bf338245
JD
7356 /* Find the display we are supposed to read input for.
7357 It's the one communicating on descriptor SD. */
7358 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
7359 {
7360#if 0 /* This ought to be unnecessary; let's verify it. */
7361#ifdef FIOSNBIO
7362 /* If available, Xlib uses FIOSNBIO to make the socket
7363 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
7364 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
7365 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7366 fcntl (dpyinfo->connection, F_SETFL, 0);
7367#endif /* ! defined (FIOSNBIO) */
7368#endif
7369
7370#if 0 /* This code can't be made to work, with multiple displays,
7371 and appears not to be used on any system any more.
7372 Also keyboard.c doesn't turn O_NDELAY on and off
7373 for X connections. */
7374#ifndef SIGIO
7375#ifndef HAVE_SELECT
7376 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
7377 {
7378 extern int read_alarm_should_throw;
7379 read_alarm_should_throw = 1;
7380 XPeekEvent (dpyinfo->display, &event);
7381 read_alarm_should_throw = 0;
7382 }
7383#endif /* HAVE_SELECT */
7384#endif /* SIGIO */
7385#endif
ab797f65 7386 }
bf338245
JD
7387#endif
7388
6b61353c 7389#ifndef USE_GTK
ab797f65
KL
7390 while (XPending (terminal->display_info.x->display))
7391 {
7392 int finish;
177c0ea7 7393
ab797f65 7394 XNextEvent (terminal->display_info.x->display, &event);
bf338245
JD
7395
7396#ifdef HAVE_X_I18N
ab797f65
KL
7397 /* Filter events for the current X input method. */
7398 if (x_filter_event (terminal->display_info.x, &event))
7399 break;
bf338245 7400#endif
ab797f65 7401 event_found = 1;
bf338245 7402
ab797f65
KL
7403 count += handle_one_xevent (terminal->display_info.x,
7404 &event, &finish, hold_quit);
bf338245 7405
ab797f65
KL
7406 if (finish == X_EVENT_GOTO_OUT)
7407 goto out;
6b61353c
KH
7408 }
7409
ab797f65 7410#else /* USE_GTK */
6b61353c
KH
7411
7412 /* For GTK we must use the GTK event loop. But XEvents gets passed
7413 to our filter function above, and then to the big event switch.
7414 We use a bunch of globals to communicate with our filter function,
7415 that is kind of ugly, but it works.
7416
7417 There is no way to do one display at the time, GTK just does events
7418 from all displays. */
7419
7420 while (gtk_events_pending ())
7421 {
7422 current_count = count;
7423 current_hold_quit = hold_quit;
7424
7425 gtk_main_iteration ();
7426
7427 count = current_count;
7428 current_count = -1;
7429 current_hold_quit = 0;
7430
7431 if (current_finish == X_EVENT_GOTO_OUT)
7432 break;
dc6f92b8 7433 }
6b61353c 7434#endif /* USE_GTK */
dc6f92b8 7435
06a2c219
GM
7436 out:;
7437
9a5196d0
RS
7438 /* On some systems, an X bug causes Emacs to get no more events
7439 when the window is destroyed. Detect that. (1994.) */
58769bee 7440 if (! event_found)
ef2a22d0 7441 {
ef2a22d0
RS
7442 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7443 One XNOOP in 100 loops will make Emacs terminate.
7444 B. Bretthauer, 1994 */
7445 x_noop_count++;
58769bee 7446 if (x_noop_count >= 100)
ef2a22d0
RS
7447 {
7448 x_noop_count=0;
2224b905
RS
7449
7450 if (next_noop_dpyinfo == 0)
7451 next_noop_dpyinfo = x_display_list;
7452
7453 XNoOp (next_noop_dpyinfo->display);
7454
7455 /* Each time we get here, cycle through the displays now open. */
7456 next_noop_dpyinfo = next_noop_dpyinfo->next;
ef2a22d0
RS
7457 }
7458 }
502add23 7459
06a2c219 7460 /* If the focus was just given to an auto-raising frame,
0134a210 7461 raise it now. */
7a13e894 7462 /* ??? This ought to be able to handle more than one such frame. */
0134a210
RS
7463 if (pending_autoraise_frame)
7464 {
7465 x_raise_frame (pending_autoraise_frame);
7466 pending_autoraise_frame = 0;
7467 }
0134a210 7468
bde5503b 7469 --handling_signal;
6b61353c
KH
7470 UNBLOCK_INPUT;
7471
dc6f92b8
JB
7472 return count;
7473}
06a2c219
GM
7474
7475
7476
dc6f92b8 7477\f
06a2c219
GM
7478/***********************************************************************
7479 Text Cursor
7480 ***********************************************************************/
7481
06a2c219
GM
7482/* Set clipping for output in glyph row ROW. W is the window in which
7483 we operate. GC is the graphics context to set clipping in.
06a2c219
GM
7484
7485 ROW may be a text row or, e.g., a mode line. Text rows must be
7486 clipped to the interior of the window dedicated to text display,
7487 mode lines must be clipped to the whole window. */
dc6f92b8
JB
7488
7489static void
08f66682 7490x_clip_to_row (w, row, area, gc)
06a2c219
GM
7491 struct window *w;
7492 struct glyph_row *row;
08f66682 7493 int area;
06a2c219 7494 GC gc;
dc6f92b8 7495{
06a2c219
GM
7496 struct frame *f = XFRAME (WINDOW_FRAME (w));
7497 XRectangle clip_rect;
08f66682 7498 int window_x, window_y, window_width;
dc6f92b8 7499
08f66682 7500 window_box (w, area, &window_x, &window_y, &window_width, 0);
5c1aae96 7501
08f66682 7502 clip_rect.x = window_x;
dbd8ee19 7503 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
06a2c219
GM
7504 clip_rect.y = max (clip_rect.y, window_y);
7505 clip_rect.width = window_width;
7506 clip_rect.height = row->visible_height;
5c1aae96 7507
06a2c219 7508 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
dc6f92b8
JB
7509}
7510
06a2c219
GM
7511
7512/* Draw a hollow box cursor on window W in glyph row ROW. */
dc6f92b8
JB
7513
7514static void
06a2c219
GM
7515x_draw_hollow_cursor (w, row)
7516 struct window *w;
7517 struct glyph_row *row;
dc6f92b8 7518{
06a2c219
GM
7519 struct frame *f = XFRAME (WINDOW_FRAME (w));
7520 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7521 Display *dpy = FRAME_X_DISPLAY (f);
7522 int x, y, wd, h;
7523 XGCValues xgcv;
7524 struct glyph *cursor_glyph;
7525 GC gc;
7526
06a2c219
GM
7527 /* Get the glyph the cursor is on. If we can't tell because
7528 the current matrix is invalid or such, give up. */
7529 cursor_glyph = get_phys_cursor_glyph (w);
7530 if (cursor_glyph == NULL)
dc6f92b8
JB
7531 return;
7532
dbd8ee19 7533 /* Compute frame-relative coordinates for phys cursor. */
e8f6b0db 7534 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
dbd8ee19 7535 wd = w->phys_cursor_width;
6b61353c 7536
06a2c219
GM
7537 /* The foreground of cursor_gc is typically the same as the normal
7538 background color, which can cause the cursor box to be invisible. */
7539 xgcv.foreground = f->output_data.x->cursor_pixel;
7540 if (dpyinfo->scratch_cursor_gc)
7541 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7542 else
7543 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7544 GCForeground, &xgcv);
7545 gc = dpyinfo->scratch_cursor_gc;
7546
7547 /* Set clipping, draw the rectangle, and reset clipping again. */
08f66682 7548 x_clip_to_row (w, row, TEXT_AREA, gc);
005a80de 7549 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h - 1);
06a2c219 7550 XSetClipMask (dpy, gc, None);
dc6f92b8
JB
7551}
7552
06a2c219
GM
7553
7554/* Draw a bar cursor on window W in glyph row ROW.
7555
7556 Implementation note: One would like to draw a bar cursor with an
7557 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7558 Unfortunately, I didn't find a font yet that has this property set.
7559 --gerd. */
dc6f92b8
JB
7560
7561static void
65c26775 7562x_draw_bar_cursor (w, row, width, kind)
06a2c219
GM
7563 struct window *w;
7564 struct glyph_row *row;
f02d8aa0 7565 int width;
65c26775 7566 enum text_cursor_kinds kind;
dc6f92b8 7567{
92f424df
GM
7568 struct frame *f = XFRAME (w->frame);
7569 struct glyph *cursor_glyph;
7d0393cf 7570
92f424df
GM
7571 /* If cursor is out of bounds, don't draw garbage. This can happen
7572 in mini-buffer windows when switching between echo area glyphs
7573 and mini-buffer. */
7574 cursor_glyph = get_phys_cursor_glyph (w);
7575 if (cursor_glyph == NULL)
7576 return;
06a2c219 7577
92f424df
GM
7578 /* If on an image, draw like a normal cursor. That's usually better
7579 visible than drawing a bar, esp. if the image is large so that
7580 the bar might not be in the window. */
7581 if (cursor_glyph->type == IMAGE_GLYPH)
7582 {
7583 struct glyph_row *row;
7584 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
442a09ea 7585 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
92f424df
GM
7586 }
7587 else
7588 {
34e5d0af
GM
7589 Display *dpy = FRAME_X_DISPLAY (f);
7590 Window window = FRAME_X_WINDOW (f);
7591 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7592 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7593 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7594 XGCValues xgcv;
7595
7596 /* If the glyph's background equals the color we normally draw
7597 the bar cursor in, the bar cursor in its normal color is
7598 invisible. Use the glyph's foreground color instead in this
7599 case, on the assumption that the glyph's colors are chosen so
7600 that the glyph is legible. */
7601 if (face->background == f->output_data.x->cursor_pixel)
7602 xgcv.background = xgcv.foreground = face->foreground;
7603 else
7604 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
06a2c219 7605 xgcv.graphics_exposures = 0;
7d0393cf 7606
06a2c219
GM
7607 if (gc)
7608 XChangeGC (dpy, gc, mask, &xgcv);
7609 else
7610 {
7611 gc = XCreateGC (dpy, window, mask, &xgcv);
7612 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7613 }
7d0393cf 7614
f02d8aa0 7615 if (width < 0)
fdbe859c 7616 width = FRAME_CURSOR_WIDTH (f);
34e5d0af 7617 width = min (cursor_glyph->pixel_width, width);
7d0393cf 7618
b3738089 7619 w->phys_cursor_width = width;
08f66682 7620 x_clip_to_row (w, row, TEXT_AREA, gc);
7d0393cf 7621
65c26775
EZ
7622 if (kind == BAR_CURSOR)
7623 XFillRectangle (dpy, window, gc,
7624 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7625 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7626 width, row->height);
7627 else
7628 XFillRectangle (dpy, window, gc,
7629 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7630 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7631 row->height - width),
7632 cursor_glyph->pixel_width,
7633 width);
7634
06a2c219
GM
7635 XSetClipMask (dpy, gc, None);
7636 }
dc6f92b8
JB
7637}
7638
06a2c219 7639
442a09ea 7640/* RIF: Define cursor CURSOR on frame F. */
06a2c219 7641
dc6f92b8 7642static void
442a09ea
KS
7643x_define_frame_cursor (f, cursor)
7644 struct frame *f;
7645 Cursor cursor;
dc6f92b8 7646{
442a09ea 7647 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
06a2c219 7648}
90e65f07 7649
dbc4e1c1 7650
442a09ea 7651/* RIF: Clear area on frame F. */
dbc4e1c1 7652
06a2c219 7653static void
442a09ea
KS
7654x_clear_frame_area (f, x, y, width, height)
7655 struct frame *f;
7656 int x, y, width, height;
06a2c219 7657{
442a09ea
KS
7658 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7659 x, y, width, height, False);
06a2c219 7660}
dbc4e1c1 7661
eea6af04 7662
442a09ea 7663/* RIF: Draw cursor on window W. */
eea6af04 7664
06a2c219 7665static void
e5a3b7d9 7666x_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
06a2c219 7667 struct window *w;
442a09ea 7668 struct glyph_row *glyph_row;
e5a3b7d9
KS
7669 int x, y;
7670 int cursor_type, cursor_width;
7671 int on_p, active_p;
dbc4e1c1 7672{
442a09ea 7673 struct frame *f = XFRAME (WINDOW_FRAME (w));
06a2c219 7674
e5a3b7d9 7675 if (on_p)
06a2c219 7676 {
e5a3b7d9 7677 w->phys_cursor_type = cursor_type;
06a2c219
GM
7678 w->phys_cursor_on_p = 1;
7679
6b61353c
KH
7680 if (glyph_row->exact_window_width_line_p
7681 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
7682 {
7683 glyph_row->cursor_in_fringe_p = 1;
7684 draw_fringe_bitmap (w, glyph_row, 0);
7685 }
7686 else
e5a3b7d9 7687 switch (cursor_type)
dc6f92b8 7688 {
06a2c219
GM
7689 case HOLLOW_BOX_CURSOR:
7690 x_draw_hollow_cursor (w, glyph_row);
7691 break;
7692
7693 case FILLED_BOX_CURSOR:
442a09ea 7694 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
06a2c219
GM
7695 break;
7696
7697 case BAR_CURSOR:
e5a3b7d9 7698 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
65c26775
EZ
7699 break;
7700
7701 case HBAR_CURSOR:
e5a3b7d9 7702 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
06a2c219
GM
7703 break;
7704
7705 case NO_CURSOR:
4398e673 7706 w->phys_cursor_width = 0;
06a2c219 7707 break;
dc6f92b8 7708
06a2c219
GM
7709 default:
7710 abort ();
7711 }
7d0393cf 7712
59ddecde
GM
7713#ifdef HAVE_X_I18N
7714 if (w == XWINDOW (f->selected_window))
7715 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7716 xic_set_preeditarea (w, x, y);
7717#endif
dc6f92b8
JB
7718 }
7719
06a2c219 7720#ifndef XFlush
856dd475 7721 XFlush (FRAME_X_DISPLAY (f));
06a2c219 7722#endif
dc6f92b8
JB
7723}
7724
dc6f92b8
JB
7725\f
7726/* Icons. */
7727
dbc4e1c1 7728/* Make the x-window of frame F use the gnu icon bitmap. */
dc6f92b8
JB
7729
7730int
990ba854 7731x_bitmap_icon (f, file)
f676886a 7732 struct frame *f;
990ba854 7733 Lisp_Object file;
dc6f92b8 7734{
06a2c219 7735 int bitmap_id;
dc6f92b8 7736
c118dd06 7737 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
7738 return 1;
7739
62fe13a4 7740 /* Free up our existing icon bitmap and mask if any. */
7556890b
RS
7741 if (f->output_data.x->icon_bitmap > 0)
7742 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7743 f->output_data.x->icon_bitmap = 0;
990ba854
RS
7744
7745 if (STRINGP (file))
62fe13a4
JB
7746 {
7747#ifdef USE_GTK
6b61353c 7748 /* Use gtk_window_set_icon_from_file () if available,
62fe13a4 7749 It's not restricted to bitmaps */
6b61353c 7750 if (xg_set_icon (f, file))
62fe13a4
JB
7751 return 0;
7752#endif /* USE_GTK */
7753 bitmap_id = x_create_bitmap_from_file (f, file);
6b61353c 7754 x_create_bitmap_mask (f, bitmap_id);
62fe13a4 7755 }
7f2ae036
RS
7756 else
7757 {
62fe13a4 7758 /* Create the GNU bitmap and mask if necessary. */
5bf01b68 7759 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
62fe13a4 7760 {
9f7b984b
CY
7761 int rc = -1;
7762
786a43d6 7763#if defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
9f7b984b
CY
7764#ifdef USE_GTK
7765 if (xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
7766 return 0;
786a43d6 7767#else
9f7b984b
CY
7768 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
7769 if (rc != -1)
7770 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7771#endif /* USE_GTK */
7772#endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
7773
7774 /* If all else fails, use the (black and white) xbm image. */
7775 if (rc == -1)
7776 {
7777 rc = x_create_bitmap_from_data (f, gnu_xbm_bits,
7778 gnu_xbm_width, gnu_xbm_height);
7779 if (rc == -1)
7780 return 1;
7781
7782 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7783 x_create_bitmap_mask (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7784 }
62fe13a4 7785 }
990ba854 7786
62fe13a4 7787 /* The first time we create the GNU bitmap and mask,
06a2c219 7788 this increments the ref-count one extra time.
62fe13a4 7789 As a result, the GNU bitmap and mask are never freed.
990ba854 7790 That way, we don't have to worry about allocating it again. */
334208b7 7791 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
990ba854 7792
334208b7 7793 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7f2ae036
RS
7794 }
7795
7796 x_wm_set_icon_pixmap (f, bitmap_id);
7556890b 7797 f->output_data.x->icon_bitmap = bitmap_id;
dc6f92b8
JB
7798
7799 return 0;
7800}
7801
7802
1be2d067
KH
7803/* Make the x-window of frame F use a rectangle with text.
7804 Use ICON_NAME as the text. */
dc6f92b8
JB
7805
7806int
f676886a
JB
7807x_text_icon (f, icon_name)
7808 struct frame *f;
dc6f92b8
JB
7809 char *icon_name;
7810{
c118dd06 7811 if (FRAME_X_WINDOW (f) == 0)
dc6f92b8
JB
7812 return 1;
7813
1be2d067
KH
7814 {
7815 XTextProperty text;
7816 text.value = (unsigned char *) icon_name;
7817 text.encoding = XA_STRING;
7818 text.format = 8;
7819 text.nitems = strlen (icon_name);
2436a4e4 7820 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
1be2d067 7821 }
58769bee 7822
7556890b
RS
7823 if (f->output_data.x->icon_bitmap > 0)
7824 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7825 f->output_data.x->icon_bitmap = 0;
b1c884c3 7826 x_wm_set_icon_pixmap (f, 0);
dc6f92b8
JB
7827
7828 return 0;
7829}
7830\f
e99db5a1
RS
7831#define X_ERROR_MESSAGE_SIZE 200
7832
7833/* If non-nil, this should be a string.
9ba8e10d
CY
7834 It means catch X errors and store the error message in this string.
7835
7836 The reason we use a stack is that x_catch_error/x_uncatch_error can
7837 be called from a signal handler.
7838*/
e99db5a1 7839
5badc98d
SM
7840struct x_error_message_stack {
7841 char string[X_ERROR_MESSAGE_SIZE];
7842 Display *dpy;
7843 struct x_error_message_stack *prev;
7844};
7845static struct x_error_message_stack *x_error_message;
e99db5a1
RS
7846
7847/* An X error handler which stores the error message in
9e69bfa4 7848 *x_error_message. This is called from x_error_handler if
e99db5a1
RS
7849 x_catch_errors is in effect. */
7850
06a2c219 7851static void
e99db5a1
RS
7852x_error_catcher (display, error)
7853 Display *display;
7854 XErrorEvent *error;
7855{
7856 XGetErrorText (display, error->error_code,
5badc98d 7857 x_error_message->string,
e99db5a1
RS
7858 X_ERROR_MESSAGE_SIZE);
7859}
7860
7861/* Begin trapping X errors for display DPY. Actually we trap X errors
7862 for all displays, but DPY should be the display you are actually
7863 operating on.
7864
7865 After calling this function, X protocol errors no longer cause
7866 Emacs to exit; instead, they are recorded in the string
9e69bfa4 7867 stored in *x_error_message.
e99db5a1
RS
7868
7869 Calling x_check_errors signals an Emacs error if an X error has
7870 occurred since the last call to x_catch_errors or x_check_errors.
7871
7872 Calling x_uncatch_errors resumes the normal error handling. */
7873
7874void x_check_errors ();
e99db5a1 7875
9ba8e10d 7876void
e99db5a1
RS
7877x_catch_errors (dpy)
7878 Display *dpy;
7879{
5eeafdc9 7880 struct x_error_message_stack *data = xmalloc (sizeof (*data));
e99db5a1
RS
7881
7882 /* Make sure any errors from previous requests have been dealt with. */
7883 XSync (dpy, False);
7884
5badc98d
SM
7885 data->dpy = dpy;
7886 data->string[0] = 0;
7887 data->prev = x_error_message;
7888 x_error_message = data;
e99db5a1
RS
7889}
7890
9ba8e10d
CY
7891/* Undo the last x_catch_errors call.
7892 DPY should be the display that was passed to x_catch_errors. */
e99db5a1 7893
9ba8e10d 7894void
4545fa20 7895x_uncatch_errors ()
e99db5a1 7896{
5badc98d 7897 struct x_error_message_stack *tmp;
781d152a 7898
8a34117e
CY
7899 BLOCK_INPUT;
7900
2728b5d0
JD
7901 /* The display may have been closed before this function is called.
7902 Check if it is still open before calling XSync. */
4545fa20 7903 if (x_display_info_for_display (x_error_message->dpy) != 0)
8a34117e 7904 XSync (x_error_message->dpy, False);
781d152a 7905
5badc98d
SM
7906 tmp = x_error_message;
7907 x_error_message = x_error_message->prev;
9ba8e10d 7908 xfree (tmp);
8a34117e 7909 UNBLOCK_INPUT;
e99db5a1
RS
7910}
7911
7912/* If any X protocol errors have arrived since the last call to
7913 x_catch_errors or x_check_errors, signal an Emacs error using
7914 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7915
7916void
7917x_check_errors (dpy, format)
7918 Display *dpy;
7919 char *format;
7920{
7921 /* Make sure to catch any errors incurred so far. */
7922 XSync (dpy, False);
7923
5badc98d 7924 if (x_error_message->string[0])
9ba8e10d
CY
7925 {
7926 char string[X_ERROR_MESSAGE_SIZE];
7927 bcopy (x_error_message->string, string, X_ERROR_MESSAGE_SIZE);
4545fa20 7928 x_uncatch_errors ();
9ba8e10d
CY
7929 error (format, string);
7930 }
e99db5a1
RS
7931}
7932
9829ddba
RS
7933/* Nonzero if we had any X protocol errors
7934 since we did x_catch_errors on DPY. */
e99db5a1
RS
7935
7936int
7937x_had_errors_p (dpy)
7938 Display *dpy;
7939{
7940 /* Make sure to catch any errors incurred so far. */
7941 XSync (dpy, False);
7942
5badc98d 7943 return x_error_message->string[0] != 0;
e99db5a1
RS
7944}
7945
9829ddba
RS
7946/* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7947
06a2c219 7948void
9829ddba
RS
7949x_clear_errors (dpy)
7950 Display *dpy;
7951{
5badc98d 7952 x_error_message->string[0] = 0;
e6feb692
RS
7953}
7954
e6aee454
KL
7955#if 0 /* See comment in unwind_to_catch why calling this is a bad
7956 * idea. --lorentey */
e6feb692
RS
7957/* Close off all unclosed x_catch_errors calls. */
7958
7959void
7960x_fully_uncatch_errors ()
7961{
7962 while (x_error_message)
7963 x_uncatch_errors ();
7964}
e6aee454 7965#endif
e6feb692
RS
7966
7967/* Nonzero if x_catch_errors has been done and not yet canceled. */
7968
7969int
7970x_catching_errors ()
7971{
7972 return x_error_message != 0;
9829ddba
RS
7973}
7974
e99db5a1
RS
7975#if 0
7976static unsigned int x_wire_count;
7977x_trace_wire ()
7978{
7979 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7980}
7981#endif /* ! 0 */
7982
7983\f
7984/* Handle SIGPIPE, which can happen when the connection to a server
7985 simply goes away. SIGPIPE is handled by x_connection_signal.
7d0393cf 7986 Don't need to do anything, because the write which caused the
e99db5a1 7987 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
06a2c219 7988 which will do the appropriate cleanup for us. */
7d0393cf 7989
e99db5a1
RS
7990static SIGTYPE
7991x_connection_signal (signalnum) /* If we don't have an argument, */
06a2c219 7992 int signalnum; /* some compilers complain in signal calls. */
e99db5a1
RS
7993{
7994#ifdef USG
7995 /* USG systems forget handlers when they are used;
7996 must reestablish each time */
7997 signal (signalnum, x_connection_signal);
7998#endif /* USG */
7999}
0da1ab50 8000
e99db5a1 8001\f
0da1ab50
GM
8002/************************************************************************
8003 Handling X errors
8004 ************************************************************************/
4746118a 8005
f0e299de
GM
8006/* Error message passed to x_connection_closed. */
8007
8008static char *error_msg;
8009
a7248e4f 8010/* Function installed as fatal_error_signal_hook in
f0e299de
GM
8011 x_connection_closed. Print the X error message, and exit normally,
8012 instead of dumping core when XtCloseDisplay fails. */
8013
8014static void
8015x_fatal_error_signal ()
8016{
8017 fprintf (stderr, "%s\n", error_msg);
8018 exit (70);
8019}
8020
0da1ab50
GM
8021/* Handle the loss of connection to display DPY. ERROR_MESSAGE is
8022 the text of an error message that lead to the connection loss. */
16bd92ea 8023
4746118a 8024static SIGTYPE
5978125e
GM
8025x_connection_closed (dpy, error_message)
8026 Display *dpy;
7a13e894 8027 char *error_message;
4746118a 8028{
5978125e 8029 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7a13e894 8030 Lisp_Object frame, tail;
5cd43233 8031 int index = SPECPDL_INDEX ();
7d0393cf 8032
f0e299de
GM
8033 error_msg = (char *) alloca (strlen (error_message) + 1);
8034 strcpy (error_msg, error_message);
1a532e54 8035 handling_signal = 0;
7d0393cf 8036
0da1ab50
GM
8037 /* Prevent being called recursively because of an error condition
8038 below. Otherwise, we might end up with printing ``can't find per
8039 display information'' in the recursive call instead of printing
8040 the original message here. */
9ba8e10d 8041 x_catch_errors (dpy);
7d0393cf 8042
e4a92752
KL
8043 /* Inhibit redisplay while frames are being deleted. */
8044 specbind (Qinhibit_redisplay, Qt);
8045
8046 if (dpyinfo)
8047 {
8048 /* Protect display from being closed when we delete the last
8049 frame on it. */
8050 dpyinfo->reference_count++;
6ed8eeff 8051 dpyinfo->terminal->reference_count++;
e4a92752
KL
8052 }
8053
8054 /* First delete frames whose mini-buffers are on frames
8055 that are on the dead display. */
8056 FOR_EACH_FRAME (tail, frame)
8057 {
8058 Lisp_Object minibuf_frame;
8059 minibuf_frame
8060 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
8061 if (FRAME_X_P (XFRAME (frame))
8062 && FRAME_X_P (XFRAME (minibuf_frame))
8063 && ! EQ (frame, minibuf_frame)
8064 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
c53956fe 8065 Fdelete_frame (frame, Qnoelisp);
e4a92752
KL
8066 }
8067
8068 /* Now delete all remaining frames on the dead display.
8069 We are now sure none of these is used as the mini-buffer
8070 for another frame that we need to delete. */
8071 FOR_EACH_FRAME (tail, frame)
8072 if (FRAME_X_P (XFRAME (frame))
8073 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
8074 {
8075 /* Set this to t so that Fdelete_frame won't get confused
8076 trying to find a replacement. */
8077 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
c53956fe 8078 Fdelete_frame (frame, Qnoelisp);
e4a92752
KL
8079 }
8080
8a4f36cc
GM
8081 /* We have to close the display to inform Xt that it doesn't
8082 exist anymore. If we don't, Xt will continue to wait for
8083 events from the display. As a consequence, a sequence of
8084
8085 M-x make-frame-on-display RET :1 RET
8086 ...kill the new frame, so that we get an IO error...
8087 M-x make-frame-on-display RET :1 RET
8088
8089 will indefinitely wait in Xt for events for display `:1', opened
c53956fe 8090 in the first call to make-frame-on-display.
6186a4a0 8091
8a4f36cc
GM
8092 Closing the display is reported to lead to a bus error on
8093 OpenWindows in certain situations. I suspect that is a bug
056ce195 8094 in OpenWindows. I don't know how to circumvent it here. */
7d0393cf 8095
f613a4c8 8096#ifdef USE_X_TOOLKIT
ae24cb3b
GM
8097 /* If DPYINFO is null, this means we didn't open the display
8098 in the first place, so don't try to close it. */
8099 if (dpyinfo)
f0e299de
GM
8100 {
8101 extern void (*fatal_error_signal_hook) P_ ((void));
8102 fatal_error_signal_hook = x_fatal_error_signal;
8103 XtCloseDisplay (dpy);
8104 fatal_error_signal_hook = NULL;
8105 }
f613a4c8 8106#endif
adabc3a9 8107
6b61353c
KH
8108#ifdef USE_GTK
8109 if (dpyinfo)
8110 xg_display_close (dpyinfo->display);
8111#endif
8112
9e80b57d 8113 if (dpyinfo)
7a13e894 8114 {
e4a92752
KL
8115 /* Indicate that this display is dead. */
8116 dpyinfo->display = 0;
7a13e894 8117
e4a92752 8118 dpyinfo->reference_count--;
6ed8eeff 8119 dpyinfo->terminal->reference_count--;
e4a92752
KL
8120 if (dpyinfo->reference_count != 0)
8121 /* We have just closed all frames on this display. */
8122 abort ();
7a13e894 8123
e4a92752
KL
8124 x_delete_display (dpyinfo);
8125 }
7a13e894 8126
4545fa20 8127 x_uncatch_errors ();
7d0393cf 8128
6ed8eeff 8129 if (terminal_list == 0)
7a13e894 8130 {
f0e299de 8131 fprintf (stderr, "%s\n", error_msg);
7a13e894
RS
8132 shut_down_emacs (0, 0, Qnil);
8133 exit (70);
8134 }
12ba150f 8135
7a13e894
RS
8136 /* Ordinary stack unwind doesn't deal with these. */
8137#ifdef SIGIO
8138 sigunblock (sigmask (SIGIO));
8139#endif
8140 sigunblock (sigmask (SIGALRM));
8141 TOTALLY_UNBLOCK_INPUT;
8142
5cd43233 8143 unbind_to (index, Qnil);
aa4d9a9e 8144 clear_waiting_for_input ();
056ce195
SM
8145 /* FIXME: This is an asynchronous interrupt w.r.t elisp, so signalling an
8146 error might not be the best thing to do. I'd vote for creating an
8147 elisp event and stuffing it in the queue so people can bind to it via
8148 the global map. --Stef */
f0e299de 8149 error ("%s", error_msg);
4746118a
JB
8150}
8151
043f7f73
SM
8152/* We specifically use it before defining it, so that gcc doesn't inline it,
8153 otherwise gdb doesn't know how to properly put a breakpoint on it. */
9e69bfa4 8154static void x_error_quitter P_ ((Display *, XErrorEvent *));
043f7f73 8155
b70001a1
RS
8156/* This is the first-level handler for X protocol errors.
8157 It calls x_error_quitter or x_error_catcher. */
8158
8159static int
8160x_error_handler (display, error)
8161 Display *display;
8162 XErrorEvent *error;
8163{
5badc98d 8164 if (x_error_message)
b70001a1
RS
8165 x_error_catcher (display, error);
8166 else
8167 x_error_quitter (display, error);
8168 return 0;
8169}
0da1ab50 8170
7a13e894
RS
8171/* This is the usual handler for X protocol errors.
8172 It kills all frames on the display that we got the error for.
8173 If that was the only one, it prints an error message and kills Emacs. */
8174
799775ff 8175/* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
b70001a1 8176
799775ff
RS
8177#if __GNUC__ >= 3 /* On GCC 3.0 we might get a warning. */
8178#define NO_INLINE __attribute__((noinline))
8179#else
8180#define NO_INLINE
8181#endif
8182
285a529b
RS
8183/* Some versions of GNU/Linux define noinline in their headers. */
8184
5599ac10
RS
8185#ifdef noinline
8186#undef noinline
285a529b
RS
8187#endif
8188
799775ff
RS
8189/* On older GCC versions, just putting x_error_quitter
8190 after x_error_handler prevents inlining into the former. */
8191
8192static void NO_INLINE
c118dd06
JB
8193x_error_quitter (display, error)
8194 Display *display;
8195 XErrorEvent *error;
8196{
7a13e894 8197 char buf[256], buf1[356];
dc6f92b8 8198
9e69bfa4
RS
8199 /* Ignore BadName errors. They can happen because of fonts
8200 or colors that are not defined. */
8201
8202 if (error->error_code == BadName)
8203 return;
8204
58769bee 8205 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 8206 original error handler. */
dc6f92b8 8207
c118dd06 8208 XGetErrorText (display, error->error_code, buf, sizeof (buf));
0a0fdc70 8209 sprintf (buf1, "X protocol error: %s on protocol request %d",
c118dd06 8210 buf, error->request_code);
7a13e894 8211 x_connection_closed (display, buf1);
dc6f92b8
JB
8212}
8213
0da1ab50 8214
e99db5a1
RS
8215/* This is the handler for X IO errors, always.
8216 It kills all frames on the display that we lost touch with.
8217 If that was the only one, it prints an error message and kills Emacs. */
7a13e894 8218
c118dd06 8219static int
e99db5a1 8220x_io_error_quitter (display)
c118dd06 8221 Display *display;
c118dd06 8222{
e99db5a1 8223 char buf[256];
dc6f92b8 8224
e99db5a1
RS
8225 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
8226 x_connection_closed (display, buf);
06a2c219 8227 return 0;
dc6f92b8 8228}
dc6f92b8 8229\f
f451eb13
JB
8230/* Changing the font of the frame. */
8231
76bcdf39
RS
8232/* Give frame F the font named FONTNAME as its default font, and
8233 return the full name of that font. FONTNAME may be a wildcard
8234 pattern; in that case, we choose some font that fits the pattern.
8235 The return value shows which font we chose. */
8236
b5cf7a0e 8237Lisp_Object
f676886a
JB
8238x_new_font (f, fontname)
8239 struct frame *f;
dc6f92b8
JB
8240 register char *fontname;
8241{
dc43ef94 8242 struct font_info *fontp
8a78c965 8243 = FS_LOAD_FONT (f, fontname);
dc6f92b8 8244
dc43ef94
KH
8245 if (!fontp)
8246 return Qnil;
2224a5fc 8247
8f924df7 8248 if (FRAME_FONT (f) == (XFontStruct *) (fontp->font))
2570e344
KH
8249 /* This font is already set in frame F. There's nothing more to
8250 do. */
8251 return build_string (fontp->full_name);
8252
0899d58c
KS
8253 FRAME_FONT (f) = (XFontStruct *) (fontp->font);
8254 FRAME_BASELINE_OFFSET (f) = fontp->baseline_offset;
8255 FRAME_FONTSET (f) = -1;
8256
6875d1ae
KH
8257 FRAME_COLUMN_WIDTH (f) = fontp->average_width;
8258 FRAME_SPACE_WIDTH (f) = fontp->space_width;
0899d58c 8259 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (FRAME_FONT (f));
976b73d7 8260
5958f265 8261 compute_fringe_widths (f, 1);
976b73d7 8262
b2cad826 8263 /* Compute the scroll bar width in character columns. */
0899d58c 8264 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
b2cad826 8265 {
0899d58c
KS
8266 int wid = FRAME_COLUMN_WIDTH (f);
8267 FRAME_CONFIG_SCROLL_BAR_COLS (f)
8268 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
b2cad826
KH
8269 }
8270 else
4e61bddf 8271 {
0899d58c
KS
8272 int wid = FRAME_COLUMN_WIDTH (f);
8273 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
4e61bddf 8274 }
b2cad826 8275
f676886a 8276 /* Now make the frame display the given font. */
c118dd06 8277 if (FRAME_X_WINDOW (f) != 0)
dc6f92b8 8278 {
7556890b 8279 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
0899d58c 8280 FRAME_FONT (f)->fid);
7556890b 8281 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
0899d58c 8282 FRAME_FONT (f)->fid);
7556890b 8283 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
0899d58c 8284 FRAME_FONT (f)->fid);
3497f73e
GM
8285
8286 /* Don't change the size of a tip frame; there's no point in
8287 doing it because it's done in Fx_show_tip, and it leads to
8288 problems because the tip frame has no widget. */
8289 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
0899d58c 8290 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
dc6f92b8
JB
8291 }
8292
dc43ef94
KH
8293 return build_string (fontp->full_name);
8294}
8295
2570e344
KH
8296/* Give frame F the fontset named FONTSETNAME as its default fontset,
8297 and return the full name of that fontset. FONTSETNAME may be a
8298 wildcard pattern; in that case, we choose some fontset that fits
8299 the pattern. FONTSETNAME may be a font name for ASCII characters;
8300 in that case, we create a fontset from that font name.
8301
8f924df7 8302 The return value shows which fontset we chose.
a92bf2cb 8303 If FONTSETNAME specifies the default fontset, return Qt.
2570e344
KH
8304 If an ASCII font in the specified fontset can't be loaded, return
8305 Qnil. */
b5cf7a0e 8306
dc43ef94
KH
8307Lisp_Object
8308x_new_fontset (f, fontsetname)
8309 struct frame *f;
77dabd3b 8310 Lisp_Object fontsetname;
dc43ef94 8311{
77dabd3b 8312 int fontset = fs_query_fontset (fontsetname, 0);
dc43ef94 8313 Lisp_Object result;
b5cf7a0e 8314
a92bf2cb 8315 if (fontset > 0 && f->output_data.x->fontset == fontset)
2da424f1
KH
8316 /* This fontset is already set in frame F. There's nothing more
8317 to do. */
ee569018 8318 return fontset_name (fontset);
a92bf2cb
KH
8319 else if (fontset == 0)
8320 /* The default fontset can't be the default font. */
8321 return Qt;
dc43ef94 8322
8f924df7
KH
8323 if (fontset > 0)
8324 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
8093092d 8325 else
8f924df7 8326 result = x_new_font (f, SDATA (fontsetname));
dc43ef94
KH
8327
8328 if (!STRINGP (result))
8329 /* Can't load ASCII font. */
8330 return Qnil;
8331
8093092d 8332 if (fontset < 0)
77dabd3b 8333 fontset = new_fontset_from_font_name (result);
8093092d 8334
dc43ef94 8335 /* Since x_new_font doesn't update any fontset information, do it now. */
0899d58c 8336 FRAME_FONTSET (f) = fontset;
dc43ef94 8337
f5d11644
GM
8338#ifdef HAVE_X_I18N
8339 if (FRAME_XIC (f)
8340 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
d5db4077 8341 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
f5d11644 8342#endif
7d0393cf 8343
8093092d 8344 return fontset_name (fontset);
dc6f92b8 8345}
f5d11644 8346
c9ff09ee
KH
8347#ifdef USE_FONT_BACKEND
8348Lisp_Object
8140317c 8349x_new_fontset2 (f, fontset, font_object)
c9ff09ee 8350 struct frame *f;
8140317c
KH
8351 int fontset;
8352 Lisp_Object font_object;
c9ff09ee 8353{
8140317c 8354 struct font *font = XSAVE_VALUE (font_object)->pointer;
c9ff09ee 8355
8140317c 8356 if (FRAME_FONT_OBJECT (f) == font)
c9ff09ee
KH
8357 /* This font is already set in frame F. There's nothing more to
8358 do. */
8359 return fontset_name (fontset);
8360
8361 BLOCK_INPUT;
8362
8140317c
KH
8363 FRAME_FONT_OBJECT (f) = font;
8364 FRAME_FONT (f) = font->font.font;
c9ff09ee
KH
8365 FRAME_BASELINE_OFFSET (f) = font->font.baseline_offset;
8366 FRAME_FONTSET (f) = fontset;
8367
8368 FRAME_COLUMN_WIDTH (f) = font->font.average_width;
8369 FRAME_SPACE_WIDTH (f) = font->font.space_width;
8370 FRAME_LINE_HEIGHT (f) = font->font.height;
8371
8372 compute_fringe_widths (f, 1);
8373
8374 /* Compute the scroll bar width in character columns. */
8375 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
8376 {
8377 int wid = FRAME_COLUMN_WIDTH (f);
8378 FRAME_CONFIG_SCROLL_BAR_COLS (f)
8379 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid - 1) / wid;
8380 }
8381 else
8382 {
8383 int wid = FRAME_COLUMN_WIDTH (f);
8384 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
8385 }
8386
8387 /* Now make the frame display the given font. */
8388 if (FRAME_X_WINDOW (f) != 0)
8389 {
8390 /* Don't change the size of a tip frame; there's no point in
8391 doing it because it's done in Fx_show_tip, and it leads to
8392 problems because the tip frame has no widget. */
8393 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8394 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
8395 }
8396
8397#ifdef HAVE_X_I18N
8398 if (FRAME_XIC (f)
8399 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
8400 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
8401#endif
8402
8403 UNBLOCK_INPUT;
8404
8405 return fontset_name (fontset);
8406}
8407#endif /* USE_FONT_BACKEND */
8408
f5d11644
GM
8409\f
8410/***********************************************************************
8411 X Input Methods
8412 ***********************************************************************/
8413
8414#ifdef HAVE_X_I18N
8415
8416#ifdef HAVE_X11R6
8417
8418/* XIM destroy callback function, which is called whenever the
8419 connection to input method XIM dies. CLIENT_DATA contains a
8420 pointer to the x_display_info structure corresponding to XIM. */
8421
8422static void
8423xim_destroy_callback (xim, client_data, call_data)
8424 XIM xim;
8425 XPointer client_data;
8426 XPointer call_data;
8427{
8428 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8429 Lisp_Object frame, tail;
7d0393cf 8430
f5d11644 8431 BLOCK_INPUT;
7d0393cf 8432
f5d11644
GM
8433 /* No need to call XDestroyIC.. */
8434 FOR_EACH_FRAME (tail, frame)
8435 {
8436 struct frame *f = XFRAME (frame);
daf01701 8437 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
f5d11644
GM
8438 {
8439 FRAME_XIC (f) = NULL;
c27ed90a 8440 xic_free_xfontset (f);
f5d11644
GM
8441 }
8442 }
7d0393cf 8443
f5d11644
GM
8444 /* No need to call XCloseIM. */
8445 dpyinfo->xim = NULL;
8446 XFree (dpyinfo->xim_styles);
8447 UNBLOCK_INPUT;
8448}
8449
8450#endif /* HAVE_X11R6 */
8451
dbd4b028
DL
8452#ifdef HAVE_X11R6
8453/* This isn't prototyped in OSF 5.0 or 5.1a. */
8454extern char *XSetIMValues P_ ((XIM, ...));
8455#endif
8456
f5d11644
GM
8457/* Open the connection to the XIM server on display DPYINFO.
8458 RESOURCE_NAME is the resource name Emacs uses. */
8459
8460static void
8461xim_open_dpy (dpyinfo, resource_name)
8462 struct x_display_info *dpyinfo;
8463 char *resource_name;
8464{
8465 XIM xim;
8466
7e7ade6e 8467#ifdef HAVE_XIM
51f3cc3b 8468 if (use_xim)
f5d11644 8469 {
51f3cc3b
DL
8470 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8471 EMACS_CLASS);
8472 dpyinfo->xim = xim;
8473
8474 if (xim)
8475 {
f5d11644 8476#ifdef HAVE_X11R6
51f3cc3b 8477 XIMCallback destroy;
f5d11644 8478#endif
7d0393cf 8479
51f3cc3b
DL
8480 /* Get supported styles and XIM values. */
8481 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
7d0393cf 8482
f5d11644 8483#ifdef HAVE_X11R6
51f3cc3b
DL
8484 destroy.callback = xim_destroy_callback;
8485 destroy.client_data = (XPointer)dpyinfo;
8486 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
f5d11644 8487#endif
51f3cc3b 8488 }
f5d11644 8489 }
7d0393cf 8490
51f3cc3b 8491 else
7e7ade6e 8492#endif /* HAVE_XIM */
51f3cc3b 8493 dpyinfo->xim = NULL;
f5d11644
GM
8494}
8495
8496
b9de836c 8497#ifdef HAVE_X11R6_XIM
f5d11644
GM
8498
8499struct xim_inst_t
8500{
8501 struct x_display_info *dpyinfo;
8502 char *resource_name;
8503};
8504
8505/* XIM instantiate callback function, which is called whenever an XIM
1fcfb866 8506 server is available. DISPLAY is the display of the XIM.
f5d11644
GM
8507 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8508 when the callback was registered. */
8509
8510static void
8511xim_instantiate_callback (display, client_data, call_data)
8512 Display *display;
8513 XPointer client_data;
8514 XPointer call_data;
8515{
8516 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8517 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8518
8519 /* We don't support multiple XIM connections. */
8520 if (dpyinfo->xim)
8521 return;
7d0393cf 8522
f5d11644
GM
8523 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8524
8525 /* Create XIC for the existing frames on the same display, as long
8526 as they have no XIC. */
8527 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8528 {
8529 Lisp_Object tail, frame;
8530
8531 BLOCK_INPUT;
8532 FOR_EACH_FRAME (tail, frame)
8533 {
8534 struct frame *f = XFRAME (frame);
7d0393cf 8535
daf01701
KL
8536 if (FRAME_X_P (f)
8537 && FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
f5d11644
GM
8538 if (FRAME_XIC (f) == NULL)
8539 {
8540 create_frame_xic (f);
8541 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8542 xic_set_statusarea (f);
8543 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8544 {
8545 struct window *w = XWINDOW (f->selected_window);
8546 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8547 }
8548 }
8549 }
7d0393cf 8550
f5d11644
GM
8551 UNBLOCK_INPUT;
8552 }
8553}
8554
b9de836c 8555#endif /* HAVE_X11R6_XIM */
f5d11644
GM
8556
8557
8558/* Open a connection to the XIM server on display DPYINFO.
8559 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8560 connection only at the first time. On X11R6, open the connection
8561 in the XIM instantiate callback function. */
8562
8563static void
8564xim_initialize (dpyinfo, resource_name)
8565 struct x_display_info *dpyinfo;
8566 char *resource_name;
8567{
7e7ade6e 8568#ifdef HAVE_XIM
51f3cc3b
DL
8569 if (use_xim)
8570 {
b9de836c 8571#ifdef HAVE_X11R6_XIM
51f3cc3b
DL
8572 struct xim_inst_t *xim_inst;
8573 int len;
8574
8575 dpyinfo->xim = NULL;
8576 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8577 xim_inst->dpyinfo = dpyinfo;
8578 len = strlen (resource_name);
8579 xim_inst->resource_name = (char *) xmalloc (len + 1);
8580 bcopy (resource_name, xim_inst->resource_name, len + 1);
8581 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8582 resource_name, EMACS_CLASS,
8583 xim_instantiate_callback,
bcea31c2
DL
8584 /* This is XPointer in XFree86
8585 but (XPointer *) on Tru64, at
8586 least, hence the configure test. */
fdc39b59 8587 (XRegisterIMInstantiateCallback_arg6) xim_inst);
b9de836c 8588#else /* not HAVE_X11R6_XIM */
51f3cc3b
DL
8589 dpyinfo->xim = NULL;
8590 xim_open_dpy (dpyinfo, resource_name);
b9de836c 8591#endif /* not HAVE_X11R6_XIM */
7d0393cf 8592
51f3cc3b
DL
8593 }
8594 else
7e7ade6e 8595#endif /* HAVE_XIM */
51f3cc3b 8596 dpyinfo->xim = NULL;
f5d11644
GM
8597}
8598
8599
8600/* Close the connection to the XIM server on display DPYINFO. */
8601
8602static void
8603xim_close_dpy (dpyinfo)
8604 struct x_display_info *dpyinfo;
8605{
7e7ade6e 8606#ifdef HAVE_XIM
51f3cc3b
DL
8607 if (use_xim)
8608 {
b9de836c 8609#ifdef HAVE_X11R6_XIM
51f3cc3b
DL
8610 if (dpyinfo->display)
8611 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8612 NULL, EMACS_CLASS,
8613 xim_instantiate_callback, NULL);
b9de836c 8614#endif /* not HAVE_X11R6_XIM */
51f3cc3b
DL
8615 if (dpyinfo->display)
8616 XCloseIM (dpyinfo->xim);
8617 dpyinfo->xim = NULL;
8618 XFree (dpyinfo->xim_styles);
8619 }
7e7ade6e 8620#endif /* HAVE_XIM */
f5d11644
GM
8621}
8622
b9de836c 8623#endif /* not HAVE_X11R6_XIM */
f5d11644
GM
8624
8625
dc6f92b8 8626\f
2e365682
RS
8627/* Calculate the absolute position in frame F
8628 from its current recorded position values and gravity. */
8629
dfcf069d 8630void
43bca5d5 8631x_calc_absolute_position (f)
f676886a 8632 struct frame *f;
dc6f92b8 8633{
0899d58c 8634 int flags = f->size_hint_flags;
c81412a0 8635
9829ddba
RS
8636 /* We have nothing to do if the current position
8637 is already for the top-left corner. */
8638 if (! ((flags & XNegative) || (flags & YNegative)))
8639 return;
8640
6dba1858
RS
8641 /* Treat negative positions as relative to the leftmost bottommost
8642 position that fits on the screen. */
20f55f9a 8643 if (flags & XNegative)
0899d58c 8644 f->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
c83c9f9d 8645 - FRAME_PIXEL_WIDTH (f) + f->left_pos);
dc6f92b8 8646
7708ced0 8647 {
0899d58c 8648 int height = FRAME_PIXEL_HEIGHT (f);
06a2c219 8649
7708ced0
GM
8650#if defined USE_X_TOOLKIT && defined USE_MOTIF
8651 /* Something is fishy here. When using Motif, starting Emacs with
8652 `-g -0-0', the frame appears too low by a few pixels.
8653
8654 This seems to be so because initially, while Emacs is starting,
8655 the column widget's height and the frame's pixel height are
8656 different. The column widget's height is the right one. In
8657 later invocations, when Emacs is up, the frame's pixel height
8658 is right, though.
8659
8660 It's not obvious where the initial small difference comes from.
8661 2000-12-01, gerd. */
7d0393cf 8662
7708ced0 8663 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
06a2c219 8664#endif
2e365682 8665
7708ced0 8666 if (flags & YNegative)
c83c9f9d 8667 f->top_pos = (FRAME_X_DISPLAY_INFO (f)->height - height + f->top_pos);
7708ced0 8668 }
7d0393cf 8669
3a35ab44
RS
8670 /* The left_pos and top_pos
8671 are now relative to the top and left screen edges,
8672 so the flags should correspond. */
0899d58c 8673 f->size_hint_flags &= ~ (XNegative | YNegative);
dc6f92b8
JB
8674}
8675
3a35ab44
RS
8676/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8677 to really change the position, and 0 when calling from
8678 x_make_frame_visible (in that case, XOFF and YOFF are the current
aa3ff7c9
KH
8679 position values). It is -1 when calling from x_set_frame_parameters,
8680 which means, do adjust for borders but don't change the gravity. */
3a35ab44 8681
dfcf069d 8682void
dc05a16b 8683x_set_offset (f, xoff, yoff, change_gravity)
f676886a 8684 struct frame *f;
dc6f92b8 8685 register int xoff, yoff;
dc05a16b 8686 int change_gravity;
dc6f92b8 8687{
4a4cbdd5
KH
8688 int modified_top, modified_left;
8689
d692a3d5 8690 if (change_gravity != 0)
3a35ab44 8691 {
d692a3d5
JD
8692 FRAME_X_OUTPUT (f)->left_before_move = f->left_pos;
8693 FRAME_X_OUTPUT (f)->top_before_move = f->top_pos;
8694
0899d58c
KS
8695 f->top_pos = yoff;
8696 f->left_pos = xoff;
8697 f->size_hint_flags &= ~ (XNegative | YNegative);
3a35ab44 8698 if (xoff < 0)
0899d58c 8699 f->size_hint_flags |= XNegative;
3a35ab44 8700 if (yoff < 0)
0899d58c
KS
8701 f->size_hint_flags |= YNegative;
8702 f->win_gravity = NorthWestGravity;
3a35ab44 8703 }
43bca5d5 8704 x_calc_absolute_position (f);
dc6f92b8
JB
8705
8706 BLOCK_INPUT;
c32cdd9a 8707 x_wm_set_size_hint (f, (long) 0, 0);
3a35ab44 8708
0899d58c
KS
8709 modified_left = f->left_pos;
8710 modified_top = f->top_pos;
6b61353c 8711
d692a3d5 8712 if (change_gravity != 0 && FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
4a4cbdd5 8713 {
6b61353c
KH
8714 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8715 than the WM decorations. So we use the calculated offset instead
8716 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8717 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8718 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
4a4cbdd5
KH
8719 }
8720
488dd4c4
JD
8721 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8722 modified_left, modified_top);
6b61353c 8723
d692a3d5
JD
8724 x_sync_with_move (f, f->left_pos, f->top_pos,
8725 FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8726 ? 1 : 0);
8727
8728 /* change_gravity is non-zero when this function is called from Lisp to
8729 programmatically move a frame. In that case, we call
8730 x_check_expected_move to discover if we have a "Type A" or "Type B"
8731 window manager, and, for a "Type A" window manager, adjust the position
8732 of the frame.
8733
8734 We call x_check_expected_move if a programmatic move occurred, and
8735 either the window manager type (A/B) is unknown or it is Type A but we
8736 need to compute the top/left offset adjustment for this frame. */
8737
8738 if (change_gravity != 0 &&
8739 (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8740 || (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
8741 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
8742 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
8743 x_check_expected_move (f, modified_left, modified_top);
6b61353c 8744
dc6f92b8
JB
8745 UNBLOCK_INPUT;
8746}
8747
1c8591d0
JD
8748/* Return non-zero if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
8749 on the root window for frame F contains ATOMNAME.
8750 This is how a WM check shall be done according to the Window Manager
8751 Specification/Extended Window Manager Hints at
7ba30657 8752 http://freedesktop.org/wiki/Specifications/wm-spec. */
1c8591d0 8753
b8e7655f 8754static int
1c8591d0 8755wm_supports (f, atomname)
b8e7655f 8756 struct frame *f;
1c8591d0 8757 const char *atomname;
b8e7655f 8758{
1c8591d0
JD
8759 Atom actual_type;
8760 unsigned long actual_size, bytes_remaining;
8761 int i, rc, actual_format;
8762 Atom prop_atom;
8763 Window wmcheck_window;
8764 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8765 Window target_window = dpyinfo->root_window;
8766 long max_len = 65536;
8767 Display *dpy = FRAME_X_DISPLAY (f);
8768 unsigned char *tmp_data = NULL;
8769 Atom target_type = XA_WINDOW;
8770 Atom want_atom;
b8e7655f 8771
1c8591d0
JD
8772 BLOCK_INPUT;
8773
8774 prop_atom = XInternAtom (dpy, "_NET_SUPPORTING_WM_CHECK", False);
8775
8776 x_catch_errors (dpy);
8777 rc = XGetWindowProperty (dpy, target_window,
8778 prop_atom, 0, max_len, False, target_type,
8779 &actual_type, &actual_format, &actual_size,
8780 &bytes_remaining, &tmp_data);
e78bf986 8781
1c8591d0
JD
8782 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
8783 {
8784 if (tmp_data) XFree (tmp_data);
8785 x_uncatch_errors ();
8786 UNBLOCK_INPUT;
8787 return 0;
8788 }
8789
8790 wmcheck_window = *(Window *) tmp_data;
8791 XFree (tmp_data);
8792
8793 /* Check if window exists. */
8794 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
8795 x_sync (f);
8796 if (x_had_errors_p (dpy))
8797 {
8798 x_uncatch_errors ();
8799 UNBLOCK_INPUT;
8800 return 0;
8801 }
8802
8803 if (dpyinfo->net_supported_window != wmcheck_window)
b8e7655f 8804 {
1c8591d0
JD
8805 /* Window changed, reload atoms */
8806 if (dpyinfo->net_supported_atoms != NULL)
8807 XFree (dpyinfo->net_supported_atoms);
8808 dpyinfo->net_supported_atoms = NULL;
8809 dpyinfo->nr_net_supported_atoms = 0;
8810 dpyinfo->net_supported_window = 0;
8811
8812 target_type = XA_ATOM;
8813 prop_atom = XInternAtom (dpy, "_NET_SUPPORTED", False);
8814 tmp_data = NULL;
8815 rc = XGetWindowProperty (dpy, target_window,
8816 prop_atom, 0, max_len, False, target_type,
8817 &actual_type, &actual_format, &actual_size,
8818 &bytes_remaining, &tmp_data);
8819
8820 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
b8e7655f 8821 {
1c8591d0
JD
8822 if (tmp_data) XFree (tmp_data);
8823 x_uncatch_errors ();
8824 UNBLOCK_INPUT;
8825 return 0;
b8e7655f 8826 }
b8e7655f 8827
1c8591d0
JD
8828 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
8829 dpyinfo->nr_net_supported_atoms = actual_size;
8830 dpyinfo->net_supported_window = wmcheck_window;
b8e7655f
JD
8831 }
8832
1c8591d0
JD
8833 rc = 0;
8834 want_atom = XInternAtom (dpy, atomname, False);
8835
e78bf986 8836 for (i = 0; rc == 0 && i < dpyinfo->nr_net_supported_atoms; ++i)
1c8591d0
JD
8837 rc = dpyinfo->net_supported_atoms[i] == want_atom;
8838
8839 x_uncatch_errors ();
8840 UNBLOCK_INPUT;
8841
8842 return rc;
8843}
8844
8845/* Do fullscreen as specified in extended window manager hints */
8846
8847static int
8848do_ewmh_fullscreen (f)
8849 struct frame *f;
8850{
8851 int have_net_atom = wm_supports (f, "_NET_WM_STATE");
8852
6dac214c
JD
8853 /* Some window managers don't say they support _NET_WM_STATE, but they do say
8854 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
8855 if (!have_net_atom)
8856 have_net_atom = wm_supports (f, "_NET_WM_STATE_FULLSCREEN");
8857
5820771b 8858 if (have_net_atom)
b8e7655f
JD
8859 {
8860 Lisp_Object frame;
b8e7655f
JD
8861 const char *atom = "_NET_WM_STATE";
8862 const char *fs = "_NET_WM_STATE_FULLSCREEN";
8863 const char *fw = "_NET_WM_STATE_MAXIMIZED_HORZ";
8864 const char *fh = "_NET_WM_STATE_MAXIMIZED_VERT";
8865 const char *what = NULL;
8866
118ddbdd
JD
8867 XSETFRAME (frame, f);
8868
b8e7655f
JD
8869 /* If there are _NET_ atoms we assume we have extended window manager
8870 hints. */
5820771b 8871 switch (f->want_fullscreen)
b8e7655f
JD
8872 {
8873 case FULLSCREEN_BOTH:
8874 what = fs;
8875 break;
8876 case FULLSCREEN_WIDTH:
8877 what = fw;
8878 break;
8879 case FULLSCREEN_HEIGHT:
8880 what = fh;
8881 break;
8882 }
8883
849bb789 8884 if (what != NULL && !wm_supports (f, what)) return 0;
1c8591d0
JD
8885
8886
b8e7655f
JD
8887 Fx_send_client_event (frame, make_number (0), frame,
8888 make_unibyte_string (atom, strlen (atom)),
8889 make_number (32),
8890 Fcons (make_number (0), /* Remove */
8891 Fcons
8892 (make_unibyte_string (fs,
8893 strlen (fs)),
8894 Qnil)));
8895 Fx_send_client_event (frame, make_number (0), frame,
8896 make_unibyte_string (atom, strlen (atom)),
8897 make_number (32),
8898 Fcons (make_number (0), /* Remove */
8899 Fcons
8900 (make_unibyte_string (fh,
8901 strlen (fh)),
8902 Qnil)));
8903 Fx_send_client_event (frame, make_number (0), frame,
8904 make_unibyte_string (atom, strlen (atom)),
8905 make_number (32),
8906 Fcons (make_number (0), /* Remove */
8907 Fcons
8908 (make_unibyte_string (fw,
8909 strlen (fw)),
8910 Qnil)));
8911 f->want_fullscreen = FULLSCREEN_NONE;
8912 if (what != NULL)
8913 Fx_send_client_event (frame, make_number (0), frame,
8914 make_unibyte_string (atom, strlen (atom)),
8915 make_number (32),
8916 Fcons (make_number (1), /* Add */
8917 Fcons
8918 (make_unibyte_string (what,
8919 strlen (what)),
8920 Qnil)));
8921 }
8922
8923 return have_net_atom;
8924}
8925
8926static void
8927XTfullscreen_hook (f)
8928 FRAME_PTR f;
8929{
5820771b 8930 if (f->async_visible)
b8e7655f
JD
8931 {
8932 BLOCK_INPUT;
8933 do_ewmh_fullscreen (f);
8934 x_sync (f);
8935 UNBLOCK_INPUT;
8936 }
8937}
8938
8939
d0fa3e56
EZ
8940/* Check if we need to resize the frame due to a fullscreen request.
8941 If so needed, resize the frame. */
8942static void
8943x_check_fullscreen (f)
8944 struct frame *f;
8945{
0899d58c 8946 if (f->want_fullscreen & FULLSCREEN_BOTH)
d0fa3e56
EZ
8947 {
8948 int width, height, ign;
7d0393cf 8949
5820771b 8950 if (do_ewmh_fullscreen (f))
b8e7655f
JD
8951 return;
8952
0899d58c 8953 x_real_positions (f, &f->left_pos, &f->top_pos);
d0fa3e56
EZ
8954
8955 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
7d0393cf 8956
d0fa3e56
EZ
8957 /* We do not need to move the window, it shall be taken care of
8958 when setting WM manager hints.
8959 If the frame is visible already, the position is checked by
6b61353c 8960 x_check_expected_move. */
0899d58c 8961 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
d0fa3e56
EZ
8962 {
8963 change_frame_size (f, height, width, 0, 1, 0);
8964 SET_FRAME_GARBAGED (f);
8965 cancel_mouse_face (f);
8966
8967 /* Wait for the change of frame size to occur */
0899d58c 8968 f->want_fullscreen |= FULLSCREEN_WAIT;
d0fa3e56
EZ
8969 }
8970 }
8971}
8972
d692a3d5
JD
8973/* This function is called by x_set_offset to determine whether the window
8974 manager interfered with the positioning of the frame. Type A window
8975 managers position the surrounding window manager decorations a small
8976 amount above and left of the user-supplied position. Type B window
8977 managers position the surrounding window manager decorations at the
8978 user-specified position. If we detect a Type A window manager, we
8979 compensate by moving the window right and down by the proper amount. */
8980
d0fa3e56 8981static void
d692a3d5 8982x_check_expected_move (f, expected_left, expected_top)
d0fa3e56 8983 struct frame *f;
d692a3d5
JD
8984 int expected_left;
8985 int expected_top;
d0fa3e56 8986{
997482f5 8987 int current_left = 0, current_top = 0;
d692a3d5
JD
8988
8989 /* x_real_positions returns the left and top offsets of the outermost
8990 window manager window around the frame. */
7d0393cf 8991
d692a3d5
JD
8992 x_real_positions (f, &current_left, &current_top);
8993
8994 if (current_left != expected_left || current_top != expected_top)
6b61353c 8995 {
d692a3d5
JD
8996 /* It's a "Type A" window manager. */
8997
8998 int adjusted_left;
8999 int adjusted_top;
9000
6b61353c 9001 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
d692a3d5
JD
9002 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
9003 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
9004
9005 /* Now fix the mispositioned frame's location. */
6b61353c 9006
d692a3d5
JD
9007 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
9008 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
9009
9010 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9011 adjusted_left, adjusted_top);
9012
9013 x_sync_with_move (f, expected_left, expected_top, 0);
6b61353c 9014 }
d692a3d5
JD
9015 else
9016 /* It's a "Type B" window manager. We don't have to adjust the
9017 frame's position. */
9018
6b61353c 9019 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
d692a3d5
JD
9020}
9021
9022
9023/* Wait for XGetGeometry to return up-to-date position information for a
9024 recently-moved frame. Call this immediately after calling XMoveWindow.
9025 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
9026 frame has been moved to, so we use a fuzzy position comparison instead
9027 of an exact comparison. */
9028
9029static void
9030x_sync_with_move (f, left, top, fuzzy)
9031 struct frame *f;
9032 int left, top, fuzzy;
9033{
9034 int count = 0;
9035
9036 while (count++ < 50)
9037 {
9038 int current_left = 0, current_top = 0;
9039
9040 /* In theory, this call to XSync only needs to happen once, but in
9041 practice, it doesn't seem to work, hence the need for the surrounding
9042 loop. */
9043
9044 XSync (FRAME_X_DISPLAY (f), False);
9045 x_real_positions (f, &current_left, &current_top);
9046
9047 if (fuzzy)
9048 {
9049 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
9050 pixels. */
d0fa3e56 9051
1ea40aa2
EZ
9052 if (eabs (current_left - left) <= 10
9053 && eabs (current_top - top) <= 40)
d692a3d5 9054 return;
d0fa3e56 9055 }
d692a3d5
JD
9056 else if (current_left == left && current_top == top)
9057 return;
9058 }
9059
9060 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
9061 will then return up-to-date position info. */
9062
9063 wait_reading_process_output (0, 500000, 0, 0, Qnil, NULL, 0);
d0fa3e56
EZ
9064}
9065
9066
499b1844
GM
9067/* Change the size of frame F's X window to COLS/ROWS in the case F
9068 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
9069 top-left-corner window gravity for this size change and subsequent
9070 size changes. Otherwise we leave the window gravity unchanged. */
9071
9072static void
9073x_set_window_size_1 (f, change_gravity, cols, rows)
f676886a 9074 struct frame *f;
bc20ebbf 9075 int change_gravity;
b1c884c3 9076 int cols, rows;
dc6f92b8
JB
9077{
9078 int pixelwidth, pixelheight;
80fd1fe2 9079
b1c884c3 9080 check_frame_size (f, &rows, &cols);
0899d58c 9081 f->scroll_bar_actual_width
b2cad826
KH
9082 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
9083 ? 0
0899d58c
KS
9084 : FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0
9085 ? FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
9086 : (FRAME_CONFIG_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f)));
976b73d7 9087
5958f265 9088 compute_fringe_widths (f, 0);
976b73d7 9089
0899d58c
KS
9090 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
9091 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
dc6f92b8 9092
0899d58c 9093 f->win_gravity = NorthWestGravity;
c32cdd9a 9094 x_wm_set_size_hint (f, (long) 0, 0);
6ccf47d1 9095
334208b7
RS
9096 XSync (FRAME_X_DISPLAY (f), False);
9097 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9098 pixelwidth, pixelheight);
b1c884c3
JB
9099
9100 /* Now, strictly speaking, we can't be sure that this is accurate,
9101 but the window manager will get around to dealing with the size
9102 change request eventually, and we'll hear how it went when the
dbc4e1c1
JB
9103 ConfigureNotify event gets here.
9104
9105 We could just not bother storing any of this information here,
9106 and let the ConfigureNotify event set everything up, but that
fddd5ceb 9107 might be kind of confusing to the Lisp code, since size changes
dbc4e1c1 9108 wouldn't be reported in the frame parameters until some random
fddd5ceb
RS
9109 point in the future when the ConfigureNotify event arrives.
9110
9111 We pass 1 for DELAY since we can't run Lisp code inside of
9112 a BLOCK_INPUT. */
7d1e984f 9113 change_frame_size (f, rows, cols, 0, 1, 0);
0899d58c
KS
9114 FRAME_PIXEL_WIDTH (f) = pixelwidth;
9115 FRAME_PIXEL_HEIGHT (f) = pixelheight;
b1c884c3 9116
aee9a898
RS
9117 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
9118 receive in the ConfigureNotify event; if we get what we asked
9119 for, then the event won't cause the screen to become garbaged, so
9120 we have to make sure to do it here. */
9121 SET_FRAME_GARBAGED (f);
9122
9123 XFlush (FRAME_X_DISPLAY (f));
499b1844
GM
9124}
9125
9126
9127/* Call this to change the size of frame F's x-window.
9128 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
9129 for this size change and subsequent size changes.
9130 Otherwise we leave the window gravity unchanged. */
aee9a898 9131
499b1844
GM
9132void
9133x_set_window_size (f, change_gravity, cols, rows)
9134 struct frame *f;
9135 int change_gravity;
9136 int cols, rows;
9137{
9138 BLOCK_INPUT;
9139
488dd4c4
JD
9140#ifdef USE_GTK
9141 if (FRAME_GTK_WIDGET (f))
9142 xg_frame_set_char_size (f, cols, rows);
9143 else
9144 x_set_window_size_1 (f, change_gravity, cols, rows);
9145#elif USE_X_TOOLKIT
7d0393cf 9146
f1f4d345 9147 if (f->output_data.x->widget != NULL)
499b1844
GM
9148 {
9149 /* The x and y position of the widget is clobbered by the
9150 call to XtSetValues within EmacsFrameSetCharSize.
9151 This is a real kludge, but I don't understand Xt so I can't
9152 figure out a correct fix. Can anyone else tell me? -- rms. */
9153 int xpos = f->output_data.x->widget->core.x;
9154 int ypos = f->output_data.x->widget->core.y;
9155 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
9156 f->output_data.x->widget->core.x = xpos;
9157 f->output_data.x->widget->core.y = ypos;
9158 }
9159 else
9160 x_set_window_size_1 (f, change_gravity, cols, rows);
7d0393cf 9161
499b1844 9162#else /* not USE_X_TOOLKIT */
7d0393cf 9163
499b1844 9164 x_set_window_size_1 (f, change_gravity, cols, rows);
7d0393cf 9165
aee9a898
RS
9166#endif /* not USE_X_TOOLKIT */
9167
4d73d038 9168 /* If cursor was outside the new size, mark it as off. */
06a2c219 9169 mark_window_cursors_off (XWINDOW (f->root_window));
4d73d038 9170
aee9a898 9171 /* Clear out any recollection of where the mouse highlighting was,
7d0393cf 9172 since it might be in a place that's outside the new frame size.
aee9a898
RS
9173 Actually checking whether it is outside is a pain in the neck,
9174 so don't try--just let the highlighting be done afresh with new size. */
e687d06e 9175 cancel_mouse_face (f);
dbc4e1c1 9176
dc6f92b8
JB
9177 UNBLOCK_INPUT;
9178}
dc6f92b8 9179\f
d047c4eb 9180/* Mouse warping. */
dc6f92b8 9181
9b378208 9182void
f676886a
JB
9183x_set_mouse_position (f, x, y)
9184 struct frame *f;
dc6f92b8
JB
9185 int x, y;
9186{
9187 int pix_x, pix_y;
9188
0899d58c
KS
9189 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
9190 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
f451eb13
JB
9191
9192 if (pix_x < 0) pix_x = 0;
0899d58c 9193 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
f451eb13
JB
9194
9195 if (pix_y < 0) pix_y = 0;
0899d58c 9196 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
dc6f92b8
JB
9197
9198 BLOCK_INPUT;
dc6f92b8 9199
334208b7
RS
9200 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
9201 0, 0, 0, 0, pix_x, pix_y);
dc6f92b8
JB
9202 UNBLOCK_INPUT;
9203}
9204
9b378208
RS
9205/* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
9206
9207void
9208x_set_mouse_pixel_position (f, pix_x, pix_y)
9209 struct frame *f;
9210 int pix_x, pix_y;
9211{
9212 BLOCK_INPUT;
9213
334208b7
RS
9214 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
9215 0, 0, 0, 0, pix_x, pix_y);
9b378208
RS
9216 UNBLOCK_INPUT;
9217}
d047c4eb
KH
9218\f
9219/* focus shifting, raising and lowering. */
9b378208 9220
dfcf069d 9221void
f676886a
JB
9222x_focus_on_frame (f)
9223 struct frame *f;
dc6f92b8 9224{
1fb20991 9225#if 0 /* This proves to be unpleasant. */
f676886a 9226 x_raise_frame (f);
1fb20991 9227#endif
6d4238f3
JB
9228#if 0
9229 /* I don't think that the ICCCM allows programs to do things like this
9230 without the interaction of the window manager. Whatever you end up
f676886a 9231 doing with this code, do it to x_unfocus_frame too. */
334208b7 9232 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
dc6f92b8 9233 RevertToPointerRoot, CurrentTime);
c118dd06 9234#endif /* ! 0 */
dc6f92b8
JB
9235}
9236
dfcf069d 9237void
f676886a
JB
9238x_unfocus_frame (f)
9239 struct frame *f;
dc6f92b8 9240{
6d4238f3 9241#if 0
f676886a 9242 /* Look at the remarks in x_focus_on_frame. */
0f941935 9243 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
334208b7 9244 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
dc6f92b8 9245 RevertToPointerRoot, CurrentTime);
c118dd06 9246#endif /* ! 0 */
dc6f92b8
JB
9247}
9248
f676886a 9249/* Raise frame F. */
dc6f92b8 9250
dfcf069d 9251void
f676886a
JB
9252x_raise_frame (f)
9253 struct frame *f;
dc6f92b8 9254{
45a26c42 9255 BLOCK_INPUT;
3a88c238 9256 if (f->async_visible)
45a26c42
JD
9257 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9258
45a26c42
JD
9259 XFlush (FRAME_X_DISPLAY (f));
9260 UNBLOCK_INPUT;
dc6f92b8
JB
9261}
9262
f676886a 9263/* Lower frame F. */
dc6f92b8 9264
dfcf069d 9265void
f676886a
JB
9266x_lower_frame (f)
9267 struct frame *f;
dc6f92b8 9268{
3a88c238 9269 if (f->async_visible)
dc6f92b8
JB
9270 {
9271 BLOCK_INPUT;
2436a4e4 9272 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
334208b7 9273 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8
JB
9274 UNBLOCK_INPUT;
9275 }
9276}
9277
b7e33a43
JD
9278/* Activate frame with Extended Window Manager Hints */
9279
9280void
9281x_ewmh_activate_frame (f)
9282 FRAME_PTR f;
9283{
9284 /* See Window Manager Specification/Extended Window Manager Hints at
7ba30657 9285 http://freedesktop.org/wiki/Specifications/wm-spec */
b7e33a43
JD
9286
9287 const char *atom = "_NET_ACTIVE_WINDOW";
9288 if (f->async_visible && wm_supports (f, atom))
9289 {
9290 Lisp_Object frame;
9291 XSETFRAME (frame, f);
9292 Fx_send_client_event (frame, make_number (0), frame,
9293 make_unibyte_string (atom, strlen (atom)),
9294 make_number (32),
9295 Fcons (make_number (1),
9296 Fcons (make_number (last_user_time),
9297 Qnil)));
9298 }
9299}
9300
dbc4e1c1 9301static void
6b0442dc 9302XTframe_raise_lower (f, raise_flag)
dbc4e1c1 9303 FRAME_PTR f;
6b0442dc 9304 int raise_flag;
dbc4e1c1 9305{
6b0442dc 9306 if (raise_flag)
b7e33a43 9307 x_raise_frame (f);
dbc4e1c1
JB
9308 else
9309 x_lower_frame (f);
9310}
d047c4eb 9311\f
81c02ebe
JD
9312/* XEmbed implementation. */
9313
9314void
9315xembed_set_info (f, flags)
9316 struct frame *f;
9317 enum xembed_info flags;
9318{
9319 Atom atom;
9320 unsigned long data[2];
9321
9322 atom = XInternAtom (FRAME_X_DISPLAY (f), "_XEMBED_INFO", False);
9323
9324 data[0] = XEMBED_VERSION;
9325 data[1] = flags;
9326
9327 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), atom, atom,
9328 32, PropModeReplace, (unsigned char *) data, 2);
9329}
9330
9331void
9332xembed_send_message (f, time, message, detail, data1, data2)
9333 struct frame *f;
9334 Time time;
9335 enum xembed_message message;
9336 long detail;
9337 long data1;
9338 long data2;
9339{
9340 XEvent event;
9341
9342 event.xclient.type = ClientMessage;
9343 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
9344 event.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_XEMBED;
9345 event.xclient.format = 32;
9346 event.xclient.data.l[0] = time;
9347 event.xclient.data.l[1] = message;
9348 event.xclient.data.l[2] = detail;
9349 event.xclient.data.l[3] = data1;
9350 event.xclient.data.l[4] = data2;
9351
9352 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
9353 False, NoEventMask, &event);
9354 XSync (FRAME_X_DISPLAY (f), False);
9355}
9356\f
d047c4eb 9357/* Change of visibility. */
dc6f92b8 9358
9382638d
KH
9359/* This tries to wait until the frame is really visible.
9360 However, if the window manager asks the user where to position
9361 the frame, this will return before the user finishes doing that.
9362 The frame will not actually be visible at that time,
9363 but it will become visible later when the window manager
9364 finishes with it. */
9365
dfcf069d 9366void
f676886a
JB
9367x_make_frame_visible (f)
9368 struct frame *f;
dc6f92b8 9369{
990ba854 9370 Lisp_Object type;
1aa6072f 9371 int original_top, original_left;
31be9251
GM
9372 int retry_count = 2;
9373
9374 retry:
dc6f92b8 9375
dc6f92b8 9376 BLOCK_INPUT;
dc6f92b8 9377
990ba854
RS
9378 type = x_icon_type (f);
9379 if (!NILP (type))
9380 x_bitmap_icon (f, type);
bdcd49ba 9381
f676886a 9382 if (! FRAME_VISIBLE_P (f))
90e65f07 9383 {
1aa6072f
RS
9384 /* We test FRAME_GARBAGED_P here to make sure we don't
9385 call x_set_offset a second time
9386 if we get to x_make_frame_visible a second time
9387 before the window gets really visible. */
9388 if (! FRAME_ICONIFIED_P (f)
81c02ebe 9389 && ! FRAME_X_EMBEDDED_P (f)
1aa6072f 9390 && ! f->output_data.x->asked_for_visible)
0899d58c 9391 x_set_offset (f, f->left_pos, f->top_pos, 0);
1aa6072f
RS
9392
9393 f->output_data.x->asked_for_visible = 1;
9394
90e65f07 9395 if (! EQ (Vx_no_window_manager, Qt))
f676886a 9396 x_wm_set_window_state (f, NormalState);
3afe33e7 9397#ifdef USE_X_TOOLKIT
81c02ebe
JD
9398 if (FRAME_X_EMBEDDED_P (f))
9399 xembed_set_info (f, XEMBED_MAPPED);
9400 else
9401 {
9402 /* This was XtPopup, but that did nothing for an iconified frame. */
9403 XtMapWidget (f->output_data.x->widget);
9404 }
3afe33e7 9405#else /* not USE_X_TOOLKIT */
488dd4c4
JD
9406#ifdef USE_GTK
9407 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
7b76ca1c 9408 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
488dd4c4 9409#else
81c02ebe
JD
9410 if (FRAME_X_EMBEDDED_P (f))
9411 xembed_set_info (f, XEMBED_MAPPED);
9412 else
9413 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
488dd4c4 9414#endif /* not USE_GTK */
3afe33e7 9415#endif /* not USE_X_TOOLKIT */
0134a210
RS
9416#if 0 /* This seems to bring back scroll bars in the wrong places
9417 if the window configuration has changed. They seem
9418 to come back ok without this. */
ab648270 9419 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
334208b7 9420 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
0134a210 9421#endif
90e65f07 9422 }
dc6f92b8 9423
334208b7 9424 XFlush (FRAME_X_DISPLAY (f));
90e65f07 9425
0dacf791
RS
9426 /* Synchronize to ensure Emacs knows the frame is visible
9427 before we do anything else. We do this loop with input not blocked
9428 so that incoming events are handled. */
9429 {
9430 Lisp_Object frame;
12ce2351 9431 int count;
28c01ffe
RS
9432 /* This must be before UNBLOCK_INPUT
9433 since events that arrive in response to the actions above
9434 will set it when they are handled. */
9435 int previously_visible = f->output_data.x->has_been_visible;
1aa6072f 9436
0899d58c
KS
9437 original_left = f->left_pos;
9438 original_top = f->top_pos;
c0a04927
RS
9439
9440 /* This must come after we set COUNT. */
9441 UNBLOCK_INPUT;
9442
2745e6c4 9443 /* We unblock here so that arriving X events are processed. */
1aa6072f 9444
dcb07ae9
RS
9445 /* Now move the window back to where it was "supposed to be".
9446 But don't do it if the gravity is negative.
9447 When the gravity is negative, this uses a position
28c01ffe
RS
9448 that is 3 pixels too low. Perhaps that's really the border width.
9449
9450 Don't do this if the window has never been visible before,
9451 because the window manager may choose the position
9452 and we don't want to override it. */
1aa6072f 9453
81c02ebe
JD
9454 if (! FRAME_VISIBLE_P (f)
9455 && ! FRAME_ICONIFIED_P (f)
9456 && ! FRAME_X_EMBEDDED_P (f)
0899d58c 9457 && f->win_gravity == NorthWestGravity
28c01ffe 9458 && previously_visible)
1aa6072f 9459 {
2745e6c4
RS
9460 Drawable rootw;
9461 int x, y;
9462 unsigned int width, height, border, depth;
7d0393cf 9463
1aa6072f 9464 BLOCK_INPUT;
9829ddba 9465
06a2c219
GM
9466 /* On some window managers (such as FVWM) moving an existing
9467 window, even to the same place, causes the window manager
9468 to introduce an offset. This can cause the window to move
9469 to an unexpected location. Check the geometry (a little
9470 slow here) and then verify that the window is in the right
9471 place. If the window is not in the right place, move it
9472 there, and take the potential window manager hit. */
2745e6c4
RS
9473 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9474 &rootw, &x, &y, &width, &height, &border, &depth);
9475
9476 if (original_left != x || original_top != y)
9477 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9478 original_left, original_top);
9479
1aa6072f
RS
9480 UNBLOCK_INPUT;
9481 }
9829ddba 9482
e0c1aef2 9483 XSETFRAME (frame, f);
c0a04927 9484
12ce2351
GM
9485 /* Wait until the frame is visible. Process X events until a
9486 MapNotify event has been seen, or until we think we won't get a
9487 MapNotify at all.. */
9488 for (count = input_signal_count + 10;
9489 input_signal_count < count && !FRAME_VISIBLE_P (f);)
2a6cf806 9490 {
12ce2351 9491 /* Force processing of queued events. */
334208b7 9492 x_sync (f);
12ce2351
GM
9493
9494 /* Machines that do polling rather than SIGIO have been
9495 observed to go into a busy-wait here. So we'll fake an
9496 alarm signal to let the handler know that there's something
9497 to be read. We used to raise a real alarm, but it seems
9498 that the handler isn't always enabled here. This is
9499 probably a bug. */
8b2f8d4e 9500 if (input_polling_used ())
3b2fa4e6 9501 {
12ce2351
GM
9502 /* It could be confusing if a real alarm arrives while
9503 processing the fake one. Turn it off and let the
9504 handler reset it. */
3e71d8f2 9505 extern void poll_for_input_1 P_ ((void));
bffcfca9
GM
9506 int old_poll_suppress_count = poll_suppress_count;
9507 poll_suppress_count = 1;
9508 poll_for_input_1 ();
9509 poll_suppress_count = old_poll_suppress_count;
3b2fa4e6 9510 }
12ce2351
GM
9511
9512 /* See if a MapNotify event has been processed. */
9513 FRAME_SAMPLE_VISIBILITY (f);
2a6cf806 9514 }
31be9251
GM
9515
9516 /* 2000-09-28: In
9517
9518 (let ((f (selected-frame)))
9519 (iconify-frame f)
9520 (raise-frame f))
9521
9522 the frame is not raised with various window managers on
554061d8 9523 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
31be9251
GM
9524 unknown reason, the call to XtMapWidget is completely ignored.
9525 Mapping the widget a second time works. */
7d0393cf 9526
31be9251
GM
9527 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
9528 goto retry;
0dacf791 9529 }
dc6f92b8
JB
9530}
9531
06a2c219 9532/* Change from mapped state to withdrawn state. */
dc6f92b8 9533
d047c4eb
KH
9534/* Make the frame visible (mapped and not iconified). */
9535
dfcf069d 9536void
f676886a
JB
9537x_make_frame_invisible (f)
9538 struct frame *f;
dc6f92b8 9539{
546e6d5b
RS
9540 Window window;
9541
546e6d5b 9542 /* Use the frame's outermost window, not the one we normally draw on. */
2436a4e4 9543 window = FRAME_OUTER_WINDOW (f);
dc6f92b8 9544
9319ae23 9545 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
9546 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9547 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 9548
5627c40e 9549#if 0/* This might add unreliability; I don't trust it -- rms. */
9319ae23 9550 if (! f->async_visible && ! f->async_iconified)
dc6f92b8 9551 return;
5627c40e 9552#endif
dc6f92b8
JB
9553
9554 BLOCK_INPUT;
c118dd06 9555
af31d76f
RS
9556 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
9557 that the current position of the window is user-specified, rather than
9558 program-specified, so that when the window is mapped again, it will be
9559 placed at the same location, without forcing the user to position it
9560 by hand again (they have already done that once for this window.) */
c32cdd9a 9561 x_wm_set_size_hint (f, (long) 0, 1);
af31d76f 9562
488dd4c4
JD
9563#ifdef USE_GTK
9564 if (FRAME_GTK_OUTER_WIDGET (f))
3c671a56
SM
9565 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
9566 else
81c02ebe
JD
9567#else
9568 if (FRAME_X_EMBEDDED_P (f))
9569 xembed_set_info (f, 0);
9570 else
488dd4c4 9571#endif
3c671a56 9572 {
c118dd06 9573
334208b7
RS
9574 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
9575 DefaultScreen (FRAME_X_DISPLAY (f))))
c118dd06
JB
9576 {
9577 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 9578 error ("Can't notify window manager of window withdrawal");
c118dd06 9579 }
3c671a56 9580 }
dc6f92b8 9581
5627c40e
RS
9582 /* We can't distinguish this from iconification
9583 just by the event that we get from the server.
9584 So we can't win using the usual strategy of letting
9585 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9586 and synchronize with the server to make sure we agree. */
9587 f->visible = 0;
9588 FRAME_ICONIFIED_P (f) = 0;
9589 f->async_visible = 0;
9590 f->async_iconified = 0;
9591
334208b7 9592 x_sync (f);
5627c40e 9593
dc6f92b8
JB
9594 UNBLOCK_INPUT;
9595}
9596
06a2c219 9597/* Change window state from mapped to iconified. */
dc6f92b8 9598
dfcf069d 9599void
f676886a
JB
9600x_iconify_frame (f)
9601 struct frame *f;
dc6f92b8 9602{
3afe33e7 9603 int result;
990ba854 9604 Lisp_Object type;
dc6f92b8 9605
9319ae23 9606 /* Don't keep the highlight on an invisible frame. */
0f941935
KH
9607 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9608 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9319ae23 9609
3a88c238 9610 if (f->async_iconified)
dc6f92b8
JB
9611 return;
9612
3afe33e7 9613 BLOCK_INPUT;
546e6d5b 9614
9af3143a
RS
9615 FRAME_SAMPLE_VISIBILITY (f);
9616
990ba854
RS
9617 type = x_icon_type (f);
9618 if (!NILP (type))
9619 x_bitmap_icon (f, type);
bdcd49ba 9620
488dd4c4
JD
9621#ifdef USE_GTK
9622 if (FRAME_GTK_OUTER_WIDGET (f))
9623 {
9624 if (! FRAME_VISIBLE_P (f))
9625 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9626
9627 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9628 f->iconified = 1;
9629 f->visible = 1;
9630 f->async_iconified = 1;
9631 f->async_visible = 0;
9632 UNBLOCK_INPUT;
9633 return;
9634 }
9635#endif
9636
bdcd49ba
RS
9637#ifdef USE_X_TOOLKIT
9638
546e6d5b
RS
9639 if (! FRAME_VISIBLE_P (f))
9640 {
9641 if (! EQ (Vx_no_window_manager, Qt))
9642 x_wm_set_window_state (f, IconicState);
9643 /* This was XtPopup, but that did nothing for an iconified frame. */
7556890b 9644 XtMapWidget (f->output_data.x->widget);
9cf30a30
RS
9645 /* The server won't give us any event to indicate
9646 that an invisible frame was changed to an icon,
9647 so we have to record it here. */
9648 f->iconified = 1;
1e6bc770 9649 f->visible = 1;
9cf30a30 9650 f->async_iconified = 1;
1e6bc770 9651 f->async_visible = 0;
546e6d5b
RS
9652 UNBLOCK_INPUT;
9653 return;
9654 }
9655
334208b7 9656 result = XIconifyWindow (FRAME_X_DISPLAY (f),
7556890b 9657 XtWindow (f->output_data.x->widget),
334208b7 9658 DefaultScreen (FRAME_X_DISPLAY (f)));
3afe33e7
RS
9659 UNBLOCK_INPUT;
9660
9661 if (!result)
546e6d5b 9662 error ("Can't notify window manager of iconification");
3afe33e7
RS
9663
9664 f->async_iconified = 1;
1e6bc770
RS
9665 f->async_visible = 0;
9666
8c002a25
KH
9667
9668 BLOCK_INPUT;
334208b7 9669 XFlush (FRAME_X_DISPLAY (f));
8c002a25 9670 UNBLOCK_INPUT;
3afe33e7
RS
9671#else /* not USE_X_TOOLKIT */
9672
fd13dbb2
RS
9673 /* Make sure the X server knows where the window should be positioned,
9674 in case the user deiconifies with the window manager. */
81c02ebe
JD
9675 if (! FRAME_VISIBLE_P (f)
9676 && ! FRAME_ICONIFIED_P (f)
9677 && ! FRAME_X_EMBEDDED_P (f))
0899d58c 9678 x_set_offset (f, f->left_pos, f->top_pos, 0);
fd13dbb2 9679
16bd92ea
JB
9680 /* Since we don't know which revision of X we're running, we'll use both
9681 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
9682
9683 /* X11R4: send a ClientMessage to the window manager using the
9684 WM_CHANGE_STATE type. */
9685 {
9686 XEvent message;
58769bee 9687
c118dd06 9688 message.xclient.window = FRAME_X_WINDOW (f);
16bd92ea 9689 message.xclient.type = ClientMessage;
334208b7 9690 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
16bd92ea
JB
9691 message.xclient.format = 32;
9692 message.xclient.data.l[0] = IconicState;
9693
334208b7
RS
9694 if (! XSendEvent (FRAME_X_DISPLAY (f),
9695 DefaultRootWindow (FRAME_X_DISPLAY (f)),
16bd92ea
JB
9696 False,
9697 SubstructureRedirectMask | SubstructureNotifyMask,
9698 &message))
dc6f92b8
JB
9699 {
9700 UNBLOCK_INPUT_RESIGNAL;
546e6d5b 9701 error ("Can't notify window manager of iconification");
dc6f92b8 9702 }
16bd92ea 9703 }
dc6f92b8 9704
58769bee 9705 /* X11R3: set the initial_state field of the window manager hints to
16bd92ea
JB
9706 IconicState. */
9707 x_wm_set_window_state (f, IconicState);
dc6f92b8 9708
a9c00105
RS
9709 if (!FRAME_VISIBLE_P (f))
9710 {
9711 /* If the frame was withdrawn, before, we must map it. */
7f9c7f94 9712 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
a9c00105
RS
9713 }
9714
3a88c238 9715 f->async_iconified = 1;
1e6bc770 9716 f->async_visible = 0;
dc6f92b8 9717
334208b7 9718 XFlush (FRAME_X_DISPLAY (f));
dc6f92b8 9719 UNBLOCK_INPUT;
8c002a25 9720#endif /* not USE_X_TOOLKIT */
dc6f92b8 9721}
19f71add 9722
d047c4eb 9723\f
19f71add 9724/* Free X resources of frame F. */
dc6f92b8 9725
dfcf069d 9726void
19f71add 9727x_free_frame_resources (f)
f676886a 9728 struct frame *f;
dc6f92b8 9729{
7f9c7f94 9730 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
c6ea2775
EZ
9731 Lisp_Object bar;
9732 struct scroll_bar *b;
7f9c7f94 9733
dc6f92b8 9734 BLOCK_INPUT;
c0ff3fab 9735
6186a4a0
RS
9736 /* If a display connection is dead, don't try sending more
9737 commands to the X server. */
19f71add 9738 if (dpyinfo->display)
6186a4a0 9739 {
c9ff09ee
KH
9740#ifdef USE_FONT_BACKEND
9741 /* We must free faces before destroying windows because some
9742 font-driver (e.g. xft) access a window while finishing a
9743 face. */
9744 if (enable_font_backend
9745 && FRAME_FACE_CACHE (f))
9746 free_frame_faces (f);
9747#endif /* USE_FONT_BACKEND */
9748
19f71add 9749 if (f->output_data.x->icon_desc)
6186a4a0 9750 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
c6ea2775
EZ
9751
9752#ifdef USE_X_TOOLKIT
9753 /* Explicitly destroy the scroll bars of the frame. Without
9754 this, we get "BadDrawable" errors from the toolkit later on,
9755 presumably from expose events generated for the disappearing
9756 toolkit scroll bars. */
9757 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9758 {
9759 b = XSCROLL_BAR (bar);
9760 x_scroll_bar_remove (b);
9761 }
9762#endif
9763
31f41daf 9764#ifdef HAVE_X_I18N
f5d11644
GM
9765 if (FRAME_XIC (f))
9766 free_frame_xic (f);
31f41daf 9767#endif
c6ea2775 9768
3afe33e7 9769#ifdef USE_X_TOOLKIT
06a2c219 9770 if (f->output_data.x->widget)
30ca89f5
GM
9771 {
9772 XtDestroyWidget (f->output_data.x->widget);
9773 f->output_data.x->widget = NULL;
9774 }
c6ea2775
EZ
9775 /* Tooltips don't have widgets, only a simple X window, even if
9776 we are using a toolkit. */
9777 else if (FRAME_X_WINDOW (f))
9778 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9779
6186a4a0 9780 free_frame_menubar (f);
c6ea2775 9781#else /* !USE_X_TOOLKIT */
488dd4c4
JD
9782
9783#ifdef USE_GTK
9784 /* In the GTK version, tooltips are normal X
9785 frames. We must check and free both types. */
9786 if (FRAME_GTK_OUTER_WIDGET (f))
9787 {
9788 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
9789 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
9790 FRAME_GTK_OUTER_WIDGET (f) = 0;
9791 }
9792#endif /* USE_GTK */
177c0ea7 9793
c6ea2775
EZ
9794 if (FRAME_X_WINDOW (f))
9795 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9796#endif /* !USE_X_TOOLKIT */
3afe33e7 9797
ce593f6e
KL
9798 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
9799 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
3e71d8f2
GM
9800 unload_color (f, f->output_data.x->cursor_pixel);
9801 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9802 unload_color (f, f->output_data.x->border_pixel);
9803 unload_color (f, f->output_data.x->mouse_pixel);
c6ea2775 9804
3e71d8f2
GM
9805 if (f->output_data.x->scroll_bar_background_pixel != -1)
9806 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9807 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9808 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
7c1bef7a
MB
9809#ifdef USE_TOOLKIT_SCROLL_BARS
9810 /* Scrollbar shadow colors. */
9811 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9812 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9813 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9814 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9815#endif /* USE_TOOLKIT_SCROLL_BARS */
3e71d8f2
GM
9816 if (f->output_data.x->white_relief.allocated_p)
9817 unload_color (f, f->output_data.x->white_relief.pixel);
9818 if (f->output_data.x->black_relief.allocated_p)
9819 unload_color (f, f->output_data.x->black_relief.pixel);
4ca78676 9820
19f71add
GM
9821 if (FRAME_FACE_CACHE (f))
9822 free_frame_faces (f);
7d0393cf 9823
4ca78676 9824 x_free_gcs (f);
6186a4a0
RS
9825 XFlush (FRAME_X_DISPLAY (f));
9826 }
dc6f92b8 9827
df89d8a4 9828 if (f->output_data.x->saved_menu_event)
06a2c219 9829 xfree (f->output_data.x->saved_menu_event);
0c49ce2f 9830
7556890b 9831 xfree (f->output_data.x);
19f71add 9832 f->output_data.x = NULL;
7d0393cf 9833
0f941935
KH
9834 if (f == dpyinfo->x_focus_frame)
9835 dpyinfo->x_focus_frame = 0;
9836 if (f == dpyinfo->x_focus_event_frame)
9837 dpyinfo->x_focus_event_frame = 0;
9838 if (f == dpyinfo->x_highlight_frame)
9839 dpyinfo->x_highlight_frame = 0;
c0ff3fab 9840
7f9c7f94 9841 if (f == dpyinfo->mouse_face_mouse_frame)
dc05a16b 9842 {
7f9c7f94
RS
9843 dpyinfo->mouse_face_beg_row
9844 = dpyinfo->mouse_face_beg_col = -1;
9845 dpyinfo->mouse_face_end_row
9846 = dpyinfo->mouse_face_end_col = -1;
9847 dpyinfo->mouse_face_window = Qnil;
21323706
RS
9848 dpyinfo->mouse_face_deferred_gc = 0;
9849 dpyinfo->mouse_face_mouse_frame = 0;
dc05a16b 9850 }
0134a210 9851
c0ff3fab 9852 UNBLOCK_INPUT;
dc6f92b8 9853}
19f71add
GM
9854
9855
9856/* Destroy the X window of frame F. */
9857
9858void
9859x_destroy_window (f)
9860 struct frame *f;
9861{
9862 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9863
9864 /* If a display connection is dead, don't try sending more
9865 commands to the X server. */
9866 if (dpyinfo->display != 0)
9867 x_free_frame_resources (f);
9868
9869 dpyinfo->reference_count--;
9870}
9871
dc6f92b8 9872\f
f451eb13
JB
9873/* Setting window manager hints. */
9874
af31d76f
RS
9875/* Set the normal size hints for the window manager, for frame F.
9876 FLAGS is the flags word to use--or 0 meaning preserve the flags
9877 that the window now has.
9878 If USER_POSITION is nonzero, we set the USPosition
488dd4c4
JD
9879 flag (this is useful when FLAGS is 0).
9880 The GTK version is in gtkutils.c */
6dba1858 9881
488dd4c4 9882#ifndef USE_GTK
dfcf069d 9883void
af31d76f 9884x_wm_set_size_hint (f, flags, user_position)
f676886a 9885 struct frame *f;
af31d76f
RS
9886 long flags;
9887 int user_position;
dc6f92b8
JB
9888{
9889 XSizeHints size_hints;
3afe33e7
RS
9890
9891#ifdef USE_X_TOOLKIT
7e4f2521
FP
9892 Arg al[2];
9893 int ac = 0;
9894 Dimension widget_width, widget_height;
488dd4c4 9895#endif
177c0ea7 9896
488dd4c4 9897 Window window = FRAME_OUTER_WINDOW (f);
dc6f92b8 9898
b72a58fd
RS
9899 /* Setting PMaxSize caused various problems. */
9900 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
dc6f92b8 9901
0899d58c
KS
9902 size_hints.x = f->left_pos;
9903 size_hints.y = f->top_pos;
7553a6b7 9904
7e4f2521
FP
9905#ifdef USE_X_TOOLKIT
9906 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
9907 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
7556890b 9908 XtGetValues (f->output_data.x->widget, al, ac);
7e4f2521
FP
9909 size_hints.height = widget_height;
9910 size_hints.width = widget_width;
9911#else /* not USE_X_TOOLKIT */
0899d58c
KS
9912 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9913 size_hints.width = FRAME_PIXEL_WIDTH (f);
7e4f2521 9914#endif /* not USE_X_TOOLKIT */
7553a6b7 9915
0899d58c
KS
9916 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
9917 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
334208b7 9918 size_hints.max_width
0899d58c 9919 = FRAME_X_DISPLAY_INFO (f)->width - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
334208b7 9920 size_hints.max_height
0899d58c 9921 = FRAME_X_DISPLAY_INFO (f)->height - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
0134a210 9922
d067ea8b
KH
9923 /* Calculate the base and minimum sizes.
9924
9925 (When we use the X toolkit, we don't do it here.
9926 Instead we copy the values that the widgets are using, below.) */
9927#ifndef USE_X_TOOLKIT
b1c884c3 9928 {
b0342f17 9929 int base_width, base_height;
0134a210 9930 int min_rows = 0, min_cols = 0;
b0342f17 9931
0899d58c
KS
9932 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9933 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
b0342f17 9934
0134a210 9935 check_frame_size (f, &min_rows, &min_cols);
b0342f17 9936
0134a210
RS
9937 /* The window manager uses the base width hints to calculate the
9938 current number of rows and columns in the frame while
9939 resizing; min_width and min_height aren't useful for this
9940 purpose, since they might not give the dimensions for a
9941 zero-row, zero-column frame.
58769bee 9942
0134a210
RS
9943 We use the base_width and base_height members if we have
9944 them; otherwise, we set the min_width and min_height members
9945 to the size for a zero x zero frame. */
b0342f17 9946
0134a210
RS
9947 size_hints.flags |= PBaseSize;
9948 size_hints.base_width = base_width;
9949 size_hints.base_height = base_height;
9950 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9951 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
b1c884c3 9952 }
dc6f92b8 9953
d067ea8b 9954 /* If we don't need the old flags, we don't need the old hint at all. */
af31d76f 9955 if (flags)
dc6f92b8 9956 {
d067ea8b
KH
9957 size_hints.flags |= flags;
9958 goto no_read;
9959 }
9960#endif /* not USE_X_TOOLKIT */
9961
9962 {
9963 XSizeHints hints; /* Sometimes I hate X Windows... */
9964 long supplied_return;
9965 int value;
af31d76f 9966
d067ea8b
KH
9967 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9968 &supplied_return);
58769bee 9969
d067ea8b
KH
9970#ifdef USE_X_TOOLKIT
9971 size_hints.base_height = hints.base_height;
9972 size_hints.base_width = hints.base_width;
9973 size_hints.min_height = hints.min_height;
9974 size_hints.min_width = hints.min_width;
9975#endif
9976
9977 if (flags)
9978 size_hints.flags |= flags;
9979 else
9980 {
9981 if (value == 0)
9982 hints.flags = 0;
9983 if (hints.flags & PSize)
9984 size_hints.flags |= PSize;
9985 if (hints.flags & PPosition)
9986 size_hints.flags |= PPosition;
9987 if (hints.flags & USPosition)
9988 size_hints.flags |= USPosition;
9989 if (hints.flags & USSize)
9990 size_hints.flags |= USSize;
9991 }
9992 }
9993
06a2c219 9994#ifndef USE_X_TOOLKIT
d067ea8b 9995 no_read:
06a2c219 9996#endif
0134a210 9997
af31d76f 9998#ifdef PWinGravity
0899d58c 9999 size_hints.win_gravity = f->win_gravity;
af31d76f 10000 size_hints.flags |= PWinGravity;
dc05a16b 10001
af31d76f 10002 if (user_position)
6dba1858 10003 {
af31d76f
RS
10004 size_hints.flags &= ~ PPosition;
10005 size_hints.flags |= USPosition;
6dba1858 10006 }
2554751d 10007#endif /* PWinGravity */
6dba1858 10008
334208b7 10009 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
dc6f92b8 10010}
488dd4c4 10011#endif /* not USE_GTK */
dc6f92b8
JB
10012
10013/* Used for IconicState or NormalState */
06a2c219 10014
dfcf069d 10015void
f676886a
JB
10016x_wm_set_window_state (f, state)
10017 struct frame *f;
dc6f92b8
JB
10018 int state;
10019{
3afe33e7 10020#ifdef USE_X_TOOLKIT
546e6d5b
RS
10021 Arg al[1];
10022
10023 XtSetArg (al[0], XtNinitialState, state);
7556890b 10024 XtSetValues (f->output_data.x->widget, al, 1);
3afe33e7 10025#else /* not USE_X_TOOLKIT */
c118dd06 10026 Window window = FRAME_X_WINDOW (f);
dc6f92b8 10027
7556890b
RS
10028 f->output_data.x->wm_hints.flags |= StateHint;
10029 f->output_data.x->wm_hints.initial_state = state;
b1c884c3 10030
7556890b 10031 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
546e6d5b 10032#endif /* not USE_X_TOOLKIT */
dc6f92b8
JB
10033}
10034
dfcf069d 10035void
7f2ae036 10036x_wm_set_icon_pixmap (f, pixmap_id)
f676886a 10037 struct frame *f;
7f2ae036 10038 int pixmap_id;
dc6f92b8 10039{
62fe13a4 10040 Pixmap icon_pixmap, icon_mask;
d2bd6bc4 10041
06a2c219 10042#ifndef USE_X_TOOLKIT
488dd4c4 10043 Window window = FRAME_OUTER_WINDOW (f);
75231bad 10044#endif
dc6f92b8 10045
7f2ae036 10046 if (pixmap_id > 0)
dbc4e1c1 10047 {
d2bd6bc4 10048 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
7556890b 10049 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
62fe13a4
JB
10050 icon_mask = x_bitmap_mask (f, pixmap_id);
10051 f->output_data.x->wm_hints.icon_mask = icon_mask;
dbc4e1c1
JB
10052 }
10053 else
68568555
RS
10054 {
10055 /* It seems there is no way to turn off use of an icon pixmap.
10056 The following line does it, only if no icon has yet been created,
10057 for some window managers. But with mwm it crashes.
10058 Some people say it should clear the IconPixmapHint bit in this case,
10059 but that doesn't work, and the X consortium said it isn't the
10060 right thing at all. Since there is no way to win,
10061 best to explicitly give up. */
10062#if 0
10063 f->output_data.x->wm_hints.icon_pixmap = None;
62fe13a4 10064 f->output_data.x->wm_hints.icon_mask = None;
68568555
RS
10065#else
10066 return;
10067#endif
10068 }
b1c884c3 10069
cb515a21
JD
10070
10071#ifdef USE_GTK
10072 {
e76738f9 10073 xg_set_frame_icon (f, icon_pixmap, icon_mask);
cb515a21
JD
10074 return;
10075 }
10076
10077#elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
d2bd6bc4
RS
10078
10079 {
10080 Arg al[1];
10081 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
10082 XtSetValues (f->output_data.x->widget, al, 1);
62fe13a4
JB
10083 XtSetArg (al[0], XtNiconMask, icon_mask);
10084 XtSetValues (f->output_data.x->widget, al, 1);
d2bd6bc4
RS
10085 }
10086
cb515a21 10087#else /* not USE_X_TOOLKIT && not USE_GTK */
7d0393cf 10088
62fe13a4 10089 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
7556890b 10090 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
d2bd6bc4 10091
cb515a21 10092#endif /* not USE_X_TOOLKIT && not USE_GTK */
dc6f92b8
JB
10093}
10094
dfcf069d 10095void
f676886a
JB
10096x_wm_set_icon_position (f, icon_x, icon_y)
10097 struct frame *f;
dc6f92b8
JB
10098 int icon_x, icon_y;
10099{
2436a4e4 10100 Window window = FRAME_OUTER_WINDOW (f);
dc6f92b8 10101
7556890b
RS
10102 f->output_data.x->wm_hints.flags |= IconPositionHint;
10103 f->output_data.x->wm_hints.icon_x = icon_x;
10104 f->output_data.x->wm_hints.icon_y = icon_y;
b1c884c3 10105
7556890b 10106 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
dc6f92b8
JB
10107}
10108
10109\f
06a2c219
GM
10110/***********************************************************************
10111 Fonts
10112 ***********************************************************************/
dc43ef94
KH
10113
10114/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
06a2c219 10115
dc43ef94
KH
10116struct font_info *
10117x_get_font_info (f, font_idx)
10118 FRAME_PTR f;
10119 int font_idx;
10120{
10121 return (FRAME_X_FONT_TABLE (f) + font_idx);
10122}
10123
10124
9c11f79e
GM
10125/* Return a list of names of available fonts matching PATTERN on frame F.
10126
10127 If SIZE is > 0, it is the size (maximum bounds width) of fonts
10128 to be listed.
10129
285f9304 10130 SIZE < 0 means include auto scaled fonts.
9c11f79e
GM
10131
10132 Frame F null means we have not yet created any frame on X, and
10133 consult the first display in x_display_list. MAXNAMES sets a limit
10134 on how many fonts to match. */
dc43ef94
KH
10135
10136Lisp_Object
10137x_list_fonts (f, pattern, size, maxnames)
9c11f79e 10138 struct frame *f;
dc43ef94
KH
10139 Lisp_Object pattern;
10140 int size;
10141 int maxnames;
10142{
06a2c219
GM
10143 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
10144 Lisp_Object tem, second_best;
9c11f79e
GM
10145 struct x_display_info *dpyinfo
10146 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
10147 Display *dpy = dpyinfo->display;
09c6077f 10148 int try_XLoadQueryFont = 0;
285f9304 10149 int allow_auto_scaled_font = 0;
9c11f79e
GM
10150
10151 if (size < 0)
10152 {
285f9304 10153 allow_auto_scaled_font = 1;
9c11f79e
GM
10154 size = 0;
10155 }
dc43ef94 10156
6b0efe73 10157 patterns = Fassoc (pattern, Valternate_fontname_alist);
2da424f1
KH
10158 if (NILP (patterns))
10159 patterns = Fcons (pattern, Qnil);
81ba44e5 10160
09c6077f
KH
10161 if (maxnames == 1 && !size)
10162 /* We can return any single font matching PATTERN. */
10163 try_XLoadQueryFont = 1;
9a32686f 10164
8e713be6 10165 for (; CONSP (patterns); patterns = XCDR (patterns))
dc43ef94 10166 {
dc43ef94 10167 int num_fonts;
3e71d8f2 10168 char **names = NULL;
dc43ef94 10169
8e713be6 10170 pattern = XCAR (patterns);
536f4067
RS
10171 /* See if we cached the result for this particular query.
10172 The cache is an alist of the form:
9c11f79e
GM
10173 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
10174 tem = XCDR (dpyinfo->name_list_element);
10175 key = Fcons (Fcons (pattern, make_number (maxnames)),
285f9304 10176 allow_auto_scaled_font ? Qt : Qnil);
9c11f79e
GM
10177 list = Fassoc (key, tem);
10178 if (!NILP (list))
b5210ea7
KH
10179 {
10180 list = Fcdr_safe (list);
10181 /* We have a cashed list. Don't have to get the list again. */
10182 goto label_cached;
10183 }
10184
10185 /* At first, put PATTERN in the cache. */
09c6077f 10186
dc43ef94 10187 BLOCK_INPUT;
9ba8e10d 10188 x_catch_errors (dpy);
17d85edc 10189
09c6077f
KH
10190 if (try_XLoadQueryFont)
10191 {
10192 XFontStruct *font;
10193 unsigned long value;
10194
d5db4077 10195 font = XLoadQueryFont (dpy, SDATA (pattern));
17d85edc
KH
10196 if (x_had_errors_p (dpy))
10197 {
10198 /* This error is perhaps due to insufficient memory on X
10199 server. Let's just ignore it. */
10200 font = NULL;
10201 x_clear_errors (dpy);
10202 }
10203
09c6077f
KH
10204 if (font
10205 && XGetFontProperty (font, XA_FONT, &value))
10206 {
10207 char *name = (char *) XGetAtomName (dpy, (Atom) value);
10208 int len = strlen (name);
01c752b5 10209 char *tmp;
09c6077f 10210
6f6512e8
KH
10211 /* If DXPC (a Differential X Protocol Compressor)
10212 Ver.3.7 is running, XGetAtomName will return null
10213 string. We must avoid such a name. */
10214 if (len == 0)
10215 try_XLoadQueryFont = 0;
10216 else
10217 {
10218 num_fonts = 1;
10219 names = (char **) alloca (sizeof (char *));
10220 /* Some systems only allow alloca assigned to a
10221 simple var. */
10222 tmp = (char *) alloca (len + 1); names[0] = tmp;
10223 bcopy (name, names[0], len + 1);
10224 XFree (name);
10225 }
09c6077f
KH
10226 }
10227 else
10228 try_XLoadQueryFont = 0;
a083fd23
RS
10229
10230 if (font)
10231 XFreeFont (dpy, font);
09c6077f
KH
10232 }
10233
10234 if (!try_XLoadQueryFont)
17d85edc
KH
10235 {
10236 /* We try at least 10 fonts because XListFonts will return
10237 auto-scaled fonts at the head. */
ee5be7c3
JD
10238 if (maxnames < 0)
10239 {
10240 int limit;
10241
10242 for (limit = 500;;)
10243 {
10244 names = XListFonts (dpy, SDATA (pattern), limit, &num_fonts);
10245 if (num_fonts == limit)
10246 {
10247 BLOCK_INPUT;
10248 XFreeFontNames (names);
10249 UNBLOCK_INPUT;
10250 limit *= 2;
10251 }
10252 else
10253 break;
10254 }
10255 }
10256 else
10257 names = XListFonts (dpy, SDATA (pattern), max (maxnames, 10),
10258 &num_fonts);
10259
17d85edc
KH
10260 if (x_had_errors_p (dpy))
10261 {
10262 /* This error is perhaps due to insufficient memory on X
10263 server. Let's just ignore it. */
10264 names = NULL;
10265 x_clear_errors (dpy);
10266 }
10267 }
10268
4545fa20 10269 x_uncatch_errors ();
dc43ef94
KH
10270 UNBLOCK_INPUT;
10271
10272 if (names)
10273 {
10274 int i;
dc43ef94
KH
10275
10276 /* Make a list of all the fonts we got back.
10277 Store that in the font cache for the display. */
10278 for (i = 0; i < num_fonts; i++)
10279 {
06a2c219 10280 int width = 0;
dc43ef94 10281 char *p = names[i];
285f9304 10282 int average_width = -1, resx = 0, dashes = 0;
7d0393cf 10283
dc43ef94 10284 /* Count the number of dashes in NAMES[I]. If there are
6b61353c
KH
10285 14 dashes, the field value following 9th dash
10286 (RESOLUTION_X) is nonzero, and the field value
285f9304
KH
10287 following 12th dash (AVERAGE_WIDTH) is 0, this is a
10288 auto-scaled font which is usually too ugly to be used
10289 for editing. Let's ignore it. */
dc43ef94
KH
10290 while (*p)
10291 if (*p++ == '-')
10292 {
10293 dashes++;
10294 if (dashes == 7) /* PIXEL_SIZE field */
10295 width = atoi (p);
285f9304
KH
10296 else if (dashes == 9)
10297 resx = atoi (p);
dc43ef94
KH
10298 else if (dashes == 12) /* AVERAGE_WIDTH field */
10299 average_width = atoi (p);
10300 }
7d0393cf 10301
285f9304 10302 if (allow_auto_scaled_font
6b61353c 10303 || dashes < 14 || average_width != 0 || resx == 0)
dc43ef94
KH
10304 {
10305 tem = build_string (names[i]);
10306 if (NILP (Fassoc (tem, list)))
10307 {
10308 if (STRINGP (Vx_pixel_size_width_font_regexp)
f39db7ea
RS
10309 && ((fast_c_string_match_ignore_case
10310 (Vx_pixel_size_width_font_regexp, names[i]))
dc43ef94
KH
10311 >= 0))
10312 /* We can set the value of PIXEL_SIZE to the
b5210ea7 10313 width of this font. */
dc43ef94
KH
10314 list = Fcons (Fcons (tem, make_number (width)), list);
10315 else
10316 /* For the moment, width is not known. */
10317 list = Fcons (Fcons (tem, Qnil), list);
10318 }
10319 }
10320 }
7d0393cf 10321
09c6077f 10322 if (!try_XLoadQueryFont)
e38f4136
GM
10323 {
10324 BLOCK_INPUT;
10325 XFreeFontNames (names);
10326 UNBLOCK_INPUT;
10327 }
dc43ef94
KH
10328 }
10329
b5210ea7 10330 /* Now store the result in the cache. */
f3fbd155
KR
10331 XSETCDR (dpyinfo->name_list_element,
10332 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
dc43ef94 10333
b5210ea7
KH
10334 label_cached:
10335 if (NILP (list)) continue; /* Try the remaining alternatives. */
dc43ef94 10336
b5210ea7
KH
10337 newlist = second_best = Qnil;
10338 /* Make a list of the fonts that have the right width. */
8e713be6 10339 for (; CONSP (list); list = XCDR (list))
b5210ea7 10340 {
536f4067
RS
10341 int found_size;
10342
8e713be6 10343 tem = XCAR (list);
dc43ef94 10344
8e713be6 10345 if (!CONSP (tem) || NILP (XCAR (tem)))
b5210ea7
KH
10346 continue;
10347 if (!size)
10348 {
8e713be6 10349 newlist = Fcons (XCAR (tem), newlist);
b5210ea7
KH
10350 continue;
10351 }
dc43ef94 10352
8e713be6 10353 if (!INTEGERP (XCDR (tem)))
dc43ef94 10354 {
b5210ea7 10355 /* Since we have not yet known the size of this font, we
9c11f79e 10356 must try slow function call XLoadQueryFont. */
dc43ef94
KH
10357 XFontStruct *thisinfo;
10358
10359 BLOCK_INPUT;
9ba8e10d 10360 x_catch_errors (dpy);
dc43ef94 10361 thisinfo = XLoadQueryFont (dpy,
d5db4077 10362 SDATA (XCAR (tem)));
17d85edc
KH
10363 if (x_had_errors_p (dpy))
10364 {
10365 /* This error is perhaps due to insufficient memory on X
10366 server. Let's just ignore it. */
10367 thisinfo = NULL;
10368 x_clear_errors (dpy);
10369 }
4545fa20 10370 x_uncatch_errors ();
dc43ef94
KH
10371 UNBLOCK_INPUT;
10372
10373 if (thisinfo)
10374 {
f3fbd155
KR
10375 XSETCDR (tem,
10376 (thisinfo->min_bounds.width == 0
10377 ? make_number (0)
10378 : make_number (thisinfo->max_bounds.width)));
e38f4136 10379 BLOCK_INPUT;
dc43ef94 10380 XFreeFont (dpy, thisinfo);
e38f4136 10381 UNBLOCK_INPUT;
dc43ef94
KH
10382 }
10383 else
b5210ea7 10384 /* For unknown reason, the previous call of XListFont had
06a2c219 10385 returned a font which can't be opened. Record the size
b5210ea7 10386 as 0 not to try to open it again. */
f3fbd155 10387 XSETCDR (tem, make_number (0));
dc43ef94 10388 }
536f4067 10389
8e713be6 10390 found_size = XINT (XCDR (tem));
536f4067 10391 if (found_size == size)
8e713be6 10392 newlist = Fcons (XCAR (tem), newlist);
536f4067 10393 else if (found_size > 0)
b5210ea7 10394 {
536f4067 10395 if (NILP (second_best))
b5210ea7 10396 second_best = tem;
536f4067
RS
10397 else if (found_size < size)
10398 {
8e713be6
KR
10399 if (XINT (XCDR (second_best)) > size
10400 || XINT (XCDR (second_best)) < found_size)
536f4067
RS
10401 second_best = tem;
10402 }
10403 else
10404 {
8e713be6
KR
10405 if (XINT (XCDR (second_best)) > size
10406 && XINT (XCDR (second_best)) > found_size)
536f4067
RS
10407 second_best = tem;
10408 }
b5210ea7
KH
10409 }
10410 }
10411 if (!NILP (newlist))
10412 break;
10413 else if (!NILP (second_best))
10414 {
8e713be6 10415 newlist = Fcons (XCAR (second_best), Qnil);
b5210ea7 10416 break;
dc43ef94 10417 }
dc43ef94
KH
10418 }
10419
10420 return newlist;
7d0393cf 10421}
dc43ef94 10422
06a2c219
GM
10423
10424#if GLYPH_DEBUG
10425
10426/* Check that FONT is valid on frame F. It is if it can be found in F's
10427 font table. */
10428
10429static void
10430x_check_font (f, font)
10431 struct frame *f;
10432 XFontStruct *font;
10433{
10434 int i;
10435 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10436
10437 xassert (font != NULL);
10438
52e42a75
KH
10439#ifdef USE_FONT_BACKEND
10440 if (enable_font_backend)
10441 /* Fixme: Perhaps we should check all cached fonts. */
10442 return;
10443#endif
06a2c219 10444 for (i = 0; i < dpyinfo->n_fonts; i++)
7d0393cf 10445 if (dpyinfo->font_table[i].name
06a2c219
GM
10446 && font == dpyinfo->font_table[i].font)
10447 break;
10448
10449 xassert (i < dpyinfo->n_fonts);
10450}
10451
10452#endif /* GLYPH_DEBUG != 0 */
10453
10454/* Set *W to the minimum width, *H to the minimum font height of FONT.
10455 Note: There are (broken) X fonts out there with invalid XFontStruct
10456 min_bounds contents. For example, handa@etl.go.jp reports that
10457 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
10458 have font->min_bounds.width == 0. */
10459
10460static INLINE void
10461x_font_min_bounds (font, w, h)
10462 XFontStruct *font;
10463 int *w, *h;
10464{
10465 *h = FONT_HEIGHT (font);
10466 *w = font->min_bounds.width;
10467
10468 /* Try to handle the case where FONT->min_bounds has invalid
10469 contents. Since the only font known to have invalid min_bounds
10470 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
10471 if (*w <= 0)
10472 *w = font->max_bounds.width;
10473}
10474
10475
10476/* Compute the smallest character width and smallest font height over
10477 all fonts available on frame F. Set the members smallest_char_width
10478 and smallest_font_height in F's x_display_info structure to
10479 the values computed. Value is non-zero if smallest_font_height or
10480 smallest_char_width become smaller than they were before. */
10481
10482static int
10483x_compute_min_glyph_bounds (f)
10484 struct frame *f;
10485{
10486 int i;
10487 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10488 XFontStruct *font;
10489 int old_width = dpyinfo->smallest_char_width;
10490 int old_height = dpyinfo->smallest_font_height;
7d0393cf 10491
06a2c219
GM
10492 dpyinfo->smallest_font_height = 100000;
10493 dpyinfo->smallest_char_width = 100000;
7d0393cf 10494
06a2c219
GM
10495 for (i = 0; i < dpyinfo->n_fonts; ++i)
10496 if (dpyinfo->font_table[i].name)
10497 {
10498 struct font_info *fontp = dpyinfo->font_table + i;
10499 int w, h;
7d0393cf 10500
06a2c219
GM
10501 font = (XFontStruct *) fontp->font;
10502 xassert (font != (XFontStruct *) ~0);
10503 x_font_min_bounds (font, &w, &h);
7d0393cf 10504
06a2c219
GM
10505 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
10506 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
10507 }
10508
10509 xassert (dpyinfo->smallest_char_width > 0
10510 && dpyinfo->smallest_font_height > 0);
10511
10512 return (dpyinfo->n_fonts == 1
10513 || dpyinfo->smallest_char_width < old_width
10514 || dpyinfo->smallest_font_height < old_height);
10515}
10516
10517
dc43ef94
KH
10518/* Load font named FONTNAME of the size SIZE for frame F, and return a
10519 pointer to the structure font_info while allocating it dynamically.
10520 If SIZE is 0, load any size of font.
10521 If loading is failed, return NULL. */
10522
10523struct font_info *
10524x_load_font (f, fontname, size)
10525 struct frame *f;
10526 register char *fontname;
10527 int size;
10528{
10529 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10530 Lisp_Object font_names;
10531
10532 /* Get a list of all the fonts that match this name. Once we
10533 have a list of matching fonts, we compare them against the fonts
10534 we already have by comparing names. */
09c6077f 10535 font_names = x_list_fonts (f, build_string (fontname), size, 1);
dc43ef94
KH
10536
10537 if (!NILP (font_names))
10538 {
10539 Lisp_Object tail;
10540 int i;
10541
10542 for (i = 0; i < dpyinfo->n_fonts; i++)
8e713be6 10543 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
06a2c219
GM
10544 if (dpyinfo->font_table[i].name
10545 && (!strcmp (dpyinfo->font_table[i].name,
d5db4077 10546 SDATA (XCAR (tail)))
06a2c219 10547 || !strcmp (dpyinfo->font_table[i].full_name,
d5db4077 10548 SDATA (XCAR (tail)))))
dc43ef94
KH
10549 return (dpyinfo->font_table + i);
10550 }
10551
10552 /* Load the font and add it to the table. */
10553 {
10554 char *full_name;
10555 XFontStruct *font;
10556 struct font_info *fontp;
10557 unsigned long value;
06a2c219 10558 int i;
dc43ef94 10559
2da424f1
KH
10560 /* If we have found fonts by x_list_font, load one of them. If
10561 not, we still try to load a font by the name given as FONTNAME
10562 because XListFonts (called in x_list_font) of some X server has
10563 a bug of not finding a font even if the font surely exists and
10564 is loadable by XLoadQueryFont. */
e1d6d5b9 10565 if (size > 0 && !NILP (font_names))
d5db4077 10566 fontname = (char *) SDATA (XCAR (font_names));
dc43ef94
KH
10567
10568 BLOCK_INPUT;
9ba8e10d 10569 x_catch_errors (FRAME_X_DISPLAY (f));
dc43ef94 10570 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
d645aaa4
KH
10571 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
10572 {
10573 /* This error is perhaps due to insufficient memory on X
10574 server. Let's just ignore it. */
10575 font = NULL;
10576 x_clear_errors (FRAME_X_DISPLAY (f));
10577 }
4545fa20 10578 x_uncatch_errors ();
dc43ef94 10579 UNBLOCK_INPUT;
b5210ea7 10580 if (!font)
dc43ef94
KH
10581 return NULL;
10582
06a2c219
GM
10583 /* Find a free slot in the font table. */
10584 for (i = 0; i < dpyinfo->n_fonts; ++i)
10585 if (dpyinfo->font_table[i].name == NULL)
10586 break;
10587
10588 /* If no free slot found, maybe enlarge the font table. */
10589 if (i == dpyinfo->n_fonts
10590 && dpyinfo->n_fonts == dpyinfo->font_table_size)
dc43ef94 10591 {
06a2c219
GM
10592 int sz;
10593 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
10594 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
dc43ef94 10595 dpyinfo->font_table
06a2c219 10596 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
dc43ef94
KH
10597 }
10598
06a2c219
GM
10599 fontp = dpyinfo->font_table + i;
10600 if (i == dpyinfo->n_fonts)
10601 ++dpyinfo->n_fonts;
dc43ef94
KH
10602
10603 /* Now fill in the slots of *FONTP. */
10604 BLOCK_INPUT;
6b61353c 10605 bzero (fontp, sizeof (*fontp));
dc43ef94 10606 fontp->font = font;
06a2c219 10607 fontp->font_idx = i;
d8891a0f 10608 fontp->charset = -1; /* fs_load_font sets it. */
dc43ef94
KH
10609 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
10610 bcopy (fontname, fontp->name, strlen (fontname) + 1);
10611
6875d1ae
KH
10612 if (font->min_bounds.width == font->max_bounds.width)
10613 {
10614 /* Fixed width font. */
10615 fontp->average_width = fontp->space_width = font->min_bounds.width;
10616 }
10617 else
10618 {
10619 XChar2b char2b;
10620 XCharStruct *pcm;
10621
10622 char2b.byte1 = 0x00, char2b.byte2 = 0x20;
10623 pcm = x_per_char_metric (font, &char2b, 0);
10624 if (pcm)
10625 fontp->space_width = pcm->width;
10626 else
10627 fontp->space_width = FONT_WIDTH (font);
10628
10629 fontp->average_width
10630 = (XGetFontProperty (font, dpyinfo->Xatom_AVERAGE_WIDTH, &value)
10631 ? (long) value / 10 : 0);
10632 if (fontp->average_width < 0)
10633 fontp->average_width = - fontp->average_width;
10634 if (fontp->average_width == 0)
10635 {
10636 if (pcm)
10637 {
10638 int width = pcm->width;
10639 for (char2b.byte2 = 33; char2b.byte2 <= 126; char2b.byte2++)
10640 if ((pcm = x_per_char_metric (font, &char2b, 0)) != NULL)
10641 width += pcm->width;
10642 fontp->average_width = width / 95;
10643 }
10644 else
10645 fontp->average_width = FONT_WIDTH (font);
10646 }
10647 }
10648
dc43ef94
KH
10649 /* Try to get the full name of FONT. Put it in FULL_NAME. */
10650 full_name = 0;
10651 if (XGetFontProperty (font, XA_FONT, &value))
10652 {
10653 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
10654 char *p = name;
10655 int dashes = 0;
10656
10657 /* Count the number of dashes in the "full name".
10658 If it is too few, this isn't really the font's full name,
10659 so don't use it.
10660 In X11R4, the fonts did not come with their canonical names
10661 stored in them. */
10662 while (*p)
10663 {
10664 if (*p == '-')
10665 dashes++;
10666 p++;
10667 }
10668
10669 if (dashes >= 13)
10670 {
10671 full_name = (char *) xmalloc (p - name + 1);
10672 bcopy (name, full_name, p - name + 1);
10673 }
10674
10675 XFree (name);
10676 }
7d0393cf 10677
dc43ef94
KH
10678 if (full_name != 0)
10679 fontp->full_name = full_name;
10680 else
10681 fontp->full_name = fontp->name;
10682
10683 fontp->size = font->max_bounds.width;
d5749adb 10684 fontp->height = FONT_HEIGHT (font);
dc43ef94 10685
2da424f1
KH
10686 if (NILP (font_names))
10687 {
10688 /* We come here because of a bug of XListFonts mentioned at
10689 the head of this block. Let's store this information in
10690 the cache for x_list_fonts. */
10691 Lisp_Object lispy_name = build_string (fontname);
10692 Lisp_Object lispy_full_name = build_string (fontp->full_name);
9c11f79e
GM
10693 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
10694 Qnil);
2da424f1 10695
f3fbd155
KR
10696 XSETCDR (dpyinfo->name_list_element,
10697 Fcons (Fcons (key,
10698 Fcons (Fcons (lispy_full_name,
10699 make_number (fontp->size)),
10700 Qnil)),
10701 XCDR (dpyinfo->name_list_element)));
2da424f1 10702 if (full_name)
9c11f79e
GM
10703 {
10704 key = Fcons (Fcons (lispy_full_name, make_number (256)),
10705 Qnil);
f3fbd155
KR
10706 XSETCDR (dpyinfo->name_list_element,
10707 Fcons (Fcons (key,
10708 Fcons (Fcons (lispy_full_name,
10709 make_number (fontp->size)),
10710 Qnil)),
10711 XCDR (dpyinfo->name_list_element)));
9c11f79e 10712 }
2da424f1
KH
10713 }
10714
dc43ef94
KH
10715 /* The slot `encoding' specifies how to map a character
10716 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
ee569018
KH
10717 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
10718 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
8ff102bd 10719 2:0xA020..0xFF7F). For the moment, we don't know which charset
8f924df7 10720 uses this font. So, we set information in fontp->encoding_type
8ff102bd
RS
10721 which is never used by any charset. If mapping can't be
10722 decided, set FONT_ENCODING_NOT_DECIDED. */
8a78c965 10723 fontp->encoding_type
dc43ef94
KH
10724 = (font->max_byte1 == 0
10725 /* 1-byte font */
10726 ? (font->min_char_or_byte2 < 0x80
10727 ? (font->max_char_or_byte2 < 0x80
10728 ? 0 /* 0x20..0x7F */
8ff102bd 10729 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
dc43ef94
KH
10730 : 1) /* 0xA0..0xFF */
10731 /* 2-byte font */
10732 : (font->min_byte1 < 0x80
10733 ? (font->max_byte1 < 0x80
10734 ? (font->min_char_or_byte2 < 0x80
10735 ? (font->max_char_or_byte2 < 0x80
10736 ? 0 /* 0x2020..0x7F7F */
8ff102bd 10737 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
dc43ef94 10738 : 3) /* 0x20A0..0x7FFF */
8ff102bd 10739 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
dc43ef94
KH
10740 : (font->min_char_or_byte2 < 0x80
10741 ? (font->max_char_or_byte2 < 0x80
10742 ? 2 /* 0xA020..0xFF7F */
8ff102bd 10743 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
dc43ef94
KH
10744 : 1))); /* 0xA0A0..0xFFFF */
10745
10746 fontp->baseline_offset
10747 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
10748 ? (long) value : 0);
10749 fontp->relative_compose
10750 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
10751 ? (long) value : 0);
f78798df
KH
10752 fontp->default_ascent
10753 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
10754 ? (long) value : 0);
dc43ef94 10755
06a2c219
GM
10756 /* Set global flag fonts_changed_p to non-zero if the font loaded
10757 has a character with a smaller width than any other character
437dfb9f 10758 before, or if the font loaded has a smaller height than any
06a2c219
GM
10759 other font loaded before. If this happens, it will make a
10760 glyph matrix reallocation necessary. */
437dfb9f 10761 fonts_changed_p |= x_compute_min_glyph_bounds (f);
dc43ef94 10762 UNBLOCK_INPUT;
dc43ef94
KH
10763 return fontp;
10764 }
10765}
10766
06a2c219
GM
10767
10768/* Return a pointer to struct font_info of a font named FONTNAME for
10769 frame F. If no such font is loaded, return NULL. */
10770
dc43ef94
KH
10771struct font_info *
10772x_query_font (f, fontname)
10773 struct frame *f;
10774 register char *fontname;
10775{
10776 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10777 int i;
10778
10779 for (i = 0; i < dpyinfo->n_fonts; i++)
06a2c219 10780 if (dpyinfo->font_table[i].name
2adccf28
KH
10781 && (!xstricmp (dpyinfo->font_table[i].name, fontname)
10782 || !xstricmp (dpyinfo->font_table[i].full_name, fontname)))
dc43ef94
KH
10783 return (dpyinfo->font_table + i);
10784 return NULL;
10785}
10786
06a2c219
GM
10787
10788/* Find a CCL program for a font specified by FONTP, and set the member
a6582676
KH
10789 `encoder' of the structure. */
10790
10791void
10792x_find_ccl_program (fontp)
10793 struct font_info *fontp;
10794{
a42f54e6 10795 Lisp_Object list, elt;
a6582676 10796
f9b5db02 10797 elt = Qnil;
8e713be6 10798 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
a6582676 10799 {
8e713be6 10800 elt = XCAR (list);
a6582676 10801 if (CONSP (elt)
8e713be6 10802 && STRINGP (XCAR (elt))
9f2feff6
KH
10803 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
10804 >= 0)
10805 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
10806 >= 0)))
a42f54e6
KH
10807 break;
10808 }
7d0393cf 10809
a42f54e6
KH
10810 if (! NILP (list))
10811 {
d27f8ca7
KH
10812 struct ccl_program *ccl
10813 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
a42f54e6 10814
8e713be6 10815 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
a42f54e6
KH
10816 xfree (ccl);
10817 else
10818 fontp->font_encoder = ccl;
a6582676
KH
10819 }
10820}
10821
06a2c219 10822
8093092d 10823/* Return a char-table whose elements are t if the font FONT_INFO
0e3dd87a
KH
10824 contains a glyph for the corresponding character, and nil if
10825 not. */
8093092d
KH
10826
10827Lisp_Object
10828x_get_font_repertory (f, font_info)
10829 FRAME_PTR f;
10830 struct font_info *font_info;
10831{
10832 XFontStruct *font = (XFontStruct *) font_info->font;
8093092d
KH
10833 Lisp_Object table;
10834 int min_byte1, max_byte1, min_byte2, max_byte2;
0e3dd87a 10835 int c;
cf0c289d
KH
10836 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
10837 int offset = CHARSET_OFFSET (charset);
8093092d
KH
10838
10839 table = Fmake_char_table (Qnil, Qnil);
10840
10841 min_byte1 = font->min_byte1;
10842 max_byte1 = font->max_byte1;
10843 min_byte2 = font->min_char_or_byte2;
10844 max_byte2 = font->max_char_or_byte2;
10845 if (min_byte1 == 0 && max_byte1 == 0)
10846 {
10847 if (! font->per_char || font->all_chars_exist == True)
cf0c289d
KH
10848 {
10849 if (offset >= 0)
10850 char_table_set_range (table, offset + min_byte2,
10851 offset + max_byte2, Qt);
10852 else
10853 for (; min_byte2 <= max_byte2; min_byte2++)
10854 {
10855 c = DECODE_CHAR (charset, min_byte2);
10856 CHAR_TABLE_SET (table, c, Qt);
10857 }
10858 }
8093092d
KH
10859 else
10860 {
10861 XCharStruct *pcm = font->per_char;
10862 int from = -1;
10863 int i;
10864
10865 for (i = min_byte2; i <= max_byte2; i++, pcm++)
10866 {
10867 if (pcm->width == 0 && pcm->rbearing == pcm->lbearing)
10868 {
10869 if (from >= 0)
10870 {
cf0c289d
KH
10871 if (offset >= 0)
10872 char_table_set_range (table, offset + from,
10873 offset + i - 1, Qt);
0e3dd87a
KH
10874 else
10875 for (; from < i; from++)
10876 {
cf0c289d 10877 c = DECODE_CHAR (charset, from);
0e3dd87a
KH
10878 CHAR_TABLE_SET (table, c, Qt);
10879 }
8093092d
KH
10880 from = -1;
10881 }
10882 }
10883 else if (from < 0)
10884 from = i;
10885 }
10886 if (from >= 0)
0e3dd87a 10887 {
cf0c289d
KH
10888 if (offset >= 0)
10889 char_table_set_range (table, offset + from, offset + i - 1,
10890 Qt);
0e3dd87a
KH
10891 else
10892 for (; from < i; from++)
10893 {
cf0c289d 10894 c = DECODE_CHAR (charset, from);
0e3dd87a
KH
10895 CHAR_TABLE_SET (table, c, Qt);
10896 }
10897 }
8093092d
KH
10898 }
10899 }
10900 else
10901 {
10902 if (! font->per_char || font->all_chars_exist == True)
10903 {
0e3dd87a 10904 int i, j;
8093092d 10905
cf0c289d 10906 if (offset >= 0)
0e3dd87a 10907 for (i = min_byte1; i <= max_byte1; i++)
cf0c289d
KH
10908 char_table_set_range
10909 (table, offset + ((i << 8) | min_byte2),
10910 offset + ((i << 8) | max_byte2), Qt);
0e3dd87a
KH
10911 else
10912 for (i = min_byte1; i <= max_byte1; i++)
10913 for (j = min_byte2; j <= max_byte2; j++)
10914 {
10915 unsigned code = (i << 8) | j;
cf0c289d
KH
10916 c = DECODE_CHAR (charset, code);
10917 CHAR_TABLE_SET (table, c, Qt);
0e3dd87a 10918 }
8093092d
KH
10919 }
10920 else
10921 {
10922 XCharStruct *pcm = font->per_char;
10923 int i;
10924
10925 for (i = min_byte1; i <= max_byte1; i++)
10926 {
10927 int from = -1;
10928 int j;
10929
10930 for (j = min_byte2; j <= max_byte2; j++, pcm++)
10931 {
10932 if (pcm->width == 0 && pcm->rbearing == pcm->lbearing)
10933 {
10934 if (from >= 0)
10935 {
cf0c289d
KH
10936 if (offset >= 0)
10937 char_table_set_range
10938 (table, offset + ((i << 8) | from),
10939 offset + ((i << 8) | (j - 1)), Qt);
0e3dd87a
KH
10940 else
10941 {
10942 for (; from < j; from++)
10943 {
10944 unsigned code = (i << 8) | from;
10945 c = ENCODE_CHAR (charset, code);
10946 CHAR_TABLE_SET (table, c, Qt);
10947 }
10948 }
8093092d
KH
10949 from = -1;
10950 }
10951 }
10952 else if (from < 0)
10953 from = j;
10954 }
10955 if (from >= 0)
0e3dd87a 10956 {
cf0c289d
KH
10957 if (offset >= 0)
10958 char_table_set_range
10959 (table, offset + ((i << 8) | from),
10960 offset + ((i << 8) | (j - 1)), Qt);
0e3dd87a
KH
10961 else
10962 {
10963 for (; from < j; from++)
10964 {
10965 unsigned code = (i << 8) | from;
cf0c289d 10966 c = DECODE_CHAR (charset, code);
0e3dd87a
KH
10967 CHAR_TABLE_SET (table, c, Qt);
10968 }
10969 }
10970 }
8093092d
KH
10971 }
10972 }
10973 }
10974
10975 return table;
10976}
dc43ef94 10977\f
06a2c219
GM
10978/***********************************************************************
10979 Initialization
10980 ***********************************************************************/
f451eb13 10981
3afe33e7
RS
10982#ifdef USE_X_TOOLKIT
10983static XrmOptionDescRec emacs_options[] = {
10984 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
10985 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
10986
10987 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
10988 XrmoptionSepArg, NULL},
10989 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
10990
10991 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10992 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10993 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10994 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10995 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10996 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
10997 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
10998};
98a20c65
CY
10999
11000/* Whether atimer for Xt timeouts is activated or not. */
11001
11002static int x_timeout_atimer_activated_flag;
11003
3afe33e7
RS
11004#endif /* USE_X_TOOLKIT */
11005
7a13e894
RS
11006static int x_initialized;
11007
11e887e2
JD
11008#ifdef HAVE_X_SM
11009static int x_session_initialized;
11010#endif
11011
29b38361
KH
11012#ifdef MULTI_KBOARD
11013/* Test whether two display-name strings agree up to the dot that separates
11014 the screen number from the server number. */
11015static int
11016same_x_server (name1, name2)
59ab1dc7 11017 const char *name1, *name2;
29b38361
KH
11018{
11019 int seen_colon = 0;
59ab1dc7 11020 const unsigned char *system_name = SDATA (Vsystem_name);
cf591cc1
RS
11021 int system_name_length = strlen (system_name);
11022 int length_until_period = 0;
11023
11024 while (system_name[length_until_period] != 0
11025 && system_name[length_until_period] != '.')
11026 length_until_period++;
11027
11028 /* Treat `unix' like an empty host name. */
11029 if (! strncmp (name1, "unix:", 5))
11030 name1 += 4;
11031 if (! strncmp (name2, "unix:", 5))
11032 name2 += 4;
11033 /* Treat this host's name like an empty host name. */
11034 if (! strncmp (name1, system_name, system_name_length)
11035 && name1[system_name_length] == ':')
11036 name1 += system_name_length;
11037 if (! strncmp (name2, system_name, system_name_length)
11038 && name2[system_name_length] == ':')
11039 name2 += system_name_length;
11040 /* Treat this host's domainless name like an empty host name. */
11041 if (! strncmp (name1, system_name, length_until_period)
11042 && name1[length_until_period] == ':')
11043 name1 += length_until_period;
11044 if (! strncmp (name2, system_name, length_until_period)
11045 && name2[length_until_period] == ':')
11046 name2 += length_until_period;
11047
29b38361
KH
11048 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
11049 {
11050 if (*name1 == ':')
11051 seen_colon++;
11052 if (seen_colon && *name1 == '.')
11053 return 1;
11054 }
11055 return (seen_colon
11056 && (*name1 == '.' || *name1 == '\0')
11057 && (*name2 == '.' || *name2 == '\0'));
11058}
11059#endif
11060
6b61353c
KH
11061/* Count number of set bits in mask and number of bits to shift to
11062 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
11063 to 5. */
11064static void
11065get_bits_and_offset (mask, bits, offset)
11066 unsigned long mask;
11067 int *bits;
11068 int *offset;
11069{
11070 int nr = 0;
11071 int off = 0;
11072
11073 while (!(mask & 1))
11074 {
11075 off++;
11076 mask >>= 1;
11077 }
11078
11079 while (mask & 1)
11080 {
11081 nr++;
11082 mask >>= 1;
11083 }
11084
11085 *offset = off;
11086 *bits = nr;
11087}
11088
5632bd0f
RS
11089/* Return 1 if display DISPLAY is available for use, 0 otherwise.
11090 But don't permanently open it, just test its availability. */
11091
3c7b6f9f
EZ
11092int
11093x_display_ok (display)
5632bd0f 11094 const char *display;
3c7b6f9f
EZ
11095{
11096 int dpy_ok = 1;
11097 Display *dpy;
3c7b6f9f 11098
c1fc674b
AS
11099 dpy = XOpenDisplay (display);
11100 if (dpy)
3c7b6f9f
EZ
11101 XCloseDisplay (dpy);
11102 else
11103 dpy_ok = 0;
11104 return dpy_ok;
11105}
11106
5632bd0f
RS
11107/* Open a connection to X display DISPLAY_NAME, and return
11108 the structure that describes the open display.
11109 If we cannot contact the display, return null. */
11110
334208b7 11111struct x_display_info *
1f8255f2 11112x_term_init (display_name, xrm_option, resource_name)
334208b7 11113 Lisp_Object display_name;
1f8255f2
RS
11114 char *xrm_option;
11115 char *resource_name;
dc6f92b8 11116{
334208b7 11117 int connection;
7a13e894 11118 Display *dpy;
6ed8eeff 11119 struct terminal *terminal;
334208b7
RS
11120 struct x_display_info *dpyinfo;
11121 XrmDatabase xrdb;
11122
60439948
KH
11123 BLOCK_INPUT;
11124
7a13e894
RS
11125 if (!x_initialized)
11126 {
11127 x_initialize ();
6b61353c 11128 ++x_initialized;
7a13e894 11129 }
dc6f92b8 11130
5632bd0f
RS
11131 if (! x_display_ok (SDATA (display_name)))
11132 error ("Display %s can't be opened", SDATA (display_name));
11133
488dd4c4
JD
11134#ifdef USE_GTK
11135 {
11136#define NUM_ARGV 10
11137 int argc;
11138 char *argv[NUM_ARGV];
11139 char **argv2 = argv;
11140 GdkAtom atom;
11141
5f6a587f
KL
11142#ifndef HAVE_GTK_MULTIDISPLAY
11143 if (!EQ (Vinitial_window_system, intern ("x")))
11144 error ("Sorry, you cannot connect to X servers with the GTK toolkit");
11145#endif
11146
6b61353c
KH
11147 if (x_initialized++ > 1)
11148 {
5f6a587f 11149#ifdef HAVE_GTK_MULTIDISPLAY
6b61353c
KH
11150 /* Opening another display. If xg_display_open returns less
11151 than zero, we are probably on GTK 2.0, which can only handle
11152 one display. GTK 2.2 or later can handle more than one. */
11153 if (xg_display_open (SDATA (display_name), &dpy) < 0)
5f6a587f 11154#endif
6b61353c 11155 error ("Sorry, this version of GTK can only handle one display");
5f6a587f 11156 }
6b61353c
KH
11157 else
11158 {
11159 for (argc = 0; argc < NUM_ARGV; ++argc)
11160 argv[argc] = 0;
488dd4c4 11161
6b61353c
KH
11162 argc = 0;
11163 argv[argc++] = initial_argv[0];
488dd4c4 11164
6b61353c
KH
11165 if (! NILP (display_name))
11166 {
11167 argv[argc++] = "--display";
11168 argv[argc++] = SDATA (display_name);
11169 }
488dd4c4 11170
6b61353c
KH
11171 argv[argc++] = "--name";
11172 argv[argc++] = resource_name;
177c0ea7 11173
6b61353c 11174 XSetLocaleModifiers ("");
1fcfb866 11175
6b61353c 11176 gtk_init (&argc, &argv2);
488dd4c4 11177
6b61353c
KH
11178 /* gtk_init does set_locale. We must fix locale after calling it. */
11179 fixup_locale ();
11180 xg_initialize ();
488dd4c4 11181
6b61353c 11182 dpy = GDK_DISPLAY ();
177c0ea7 11183
6b61353c
KH
11184 /* NULL window -> events for all windows go to our function */
11185 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
488dd4c4 11186
6b61353c
KH
11187 /* Load our own gtkrc if it exists. */
11188 {
6b61353c
KH
11189 char *file = "~/.emacs.d/gtkrc";
11190 Lisp_Object s, abs_file;
488dd4c4 11191
6b61353c
KH
11192 s = make_string (file, strlen (file));
11193 abs_file = Fexpand_file_name (s, Qnil);
488dd4c4 11194
6b61353c
KH
11195 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
11196 gtk_rc_parse (SDATA (abs_file));
6b61353c 11197 }
177c0ea7 11198
6b61353c
KH
11199 XSetErrorHandler (x_error_handler);
11200 XSetIOErrorHandler (x_io_error_quitter);
11201 }
488dd4c4
JD
11202 }
11203#else /* not USE_GTK */
3afe33e7 11204#ifdef USE_X_TOOLKIT
2d7fc7e8
RS
11205 /* weiner@footloose.sps.mot.com reports that this causes
11206 errors with X11R5:
11207 X protocol error: BadAtom (invalid Atom parameter)
11208 on protocol request 18skiloaf.
11209 So let's not use it until R6. */
11210#ifdef HAVE_X11XTR6
bdcd49ba
RS
11211 XtSetLanguageProc (NULL, NULL, NULL);
11212#endif
11213
7f9c7f94
RS
11214 {
11215 int argc = 0;
11216 char *argv[3];
11217
11218 argv[0] = "";
11219 argc = 1;
11220 if (xrm_option)
11221 {
11222 argv[argc++] = "-xrm";
11223 argv[argc++] = xrm_option;
11224 }
bc4e60fc 11225 turn_on_atimers (0);
d5db4077 11226 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
7f9c7f94
RS
11227 resource_name, EMACS_CLASS,
11228 emacs_options, XtNumber (emacs_options),
11229 &argc, argv);
bc4e60fc 11230 turn_on_atimers (1);
39d8bb4d
KH
11231
11232#ifdef HAVE_X11XTR6
10537cb1 11233 /* I think this is to compensate for XtSetLanguageProc. */
71f8198a 11234 fixup_locale ();
39d8bb4d 11235#endif
7f9c7f94 11236 }
3afe33e7
RS
11237
11238#else /* not USE_X_TOOLKIT */
bdcd49ba 11239 XSetLocaleModifiers ("");
d5db4077 11240 dpy = XOpenDisplay (SDATA (display_name));
3afe33e7 11241#endif /* not USE_X_TOOLKIT */
488dd4c4 11242#endif /* not USE_GTK*/
334208b7 11243
7a13e894
RS
11244 /* Detect failure. */
11245 if (dpy == 0)
60439948
KH
11246 {
11247 UNBLOCK_INPUT;
11248 return 0;
11249 }
7a13e894
RS
11250
11251 /* We have definitely succeeded. Record the new connection. */
11252
11253 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
f04e1297 11254 bzero (dpyinfo, sizeof *dpyinfo);
7a13e894 11255
6ed8eeff 11256 terminal = x_create_terminal (dpyinfo);
428a555e 11257
29b38361
KH
11258#ifdef MULTI_KBOARD
11259 {
11260 struct x_display_info *share;
11261 Lisp_Object tail;
11262
11263 for (share = x_display_list, tail = x_display_name_list; share;
8e713be6 11264 share = share->next, tail = XCDR (tail))
d5db4077
KR
11265 if (same_x_server (SDATA (XCAR (XCAR (tail))),
11266 SDATA (display_name)))
29b38361
KH
11267 break;
11268 if (share)
6ed8eeff 11269 terminal->kboard = share->terminal->kboard;
29b38361
KH
11270 else
11271 {
6ed8eeff
KL
11272 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
11273 init_kboard (terminal->kboard);
70b8d0a4 11274 terminal->kboard->Vwindow_system = intern ("x");
59e755be
KH
11275 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
11276 {
11277 char *vendor = ServerVendor (dpy);
9b6ed9f3 11278 UNBLOCK_INPUT;
6ed8eeff 11279 terminal->kboard->Vsystem_key_alist
59e755be 11280 = call1 (Qvendor_specific_keysyms,
e78bf986 11281 vendor ? build_string (vendor) : empty_unibyte_string);
9b6ed9f3 11282 BLOCK_INPUT;
59e755be
KH
11283 }
11284
6ed8eeff
KL
11285 terminal->kboard->next_kboard = all_kboards;
11286 all_kboards = terminal->kboard;
0ad5446c
KH
11287 /* Don't let the initial kboard remain current longer than necessary.
11288 That would cause problems if a file loaded on startup tries to
06a2c219 11289 prompt in the mini-buffer. */
0ad5446c 11290 if (current_kboard == initial_kboard)
6ed8eeff 11291 current_kboard = terminal->kboard;
29b38361 11292 }
6ed8eeff 11293 terminal->kboard->reference_count++;
29b38361 11294 }
b9737ad3
KH
11295#endif
11296
7a13e894
RS
11297 /* Put this display on the chain. */
11298 dpyinfo->next = x_display_list;
11299 x_display_list = dpyinfo;
11300
7d0393cf 11301 /* Put it on x_display_name_list as well, to keep them parallel. */
7a13e894
RS
11302 x_display_name_list = Fcons (Fcons (display_name, Qnil),
11303 x_display_name_list);
8e713be6 11304 dpyinfo->name_list_element = XCAR (x_display_name_list);
7a13e894
RS
11305
11306 dpyinfo->display = dpy;
dc6f92b8 11307
6ed8eeff
KL
11308 /* Set the name of the terminal. */
11309 terminal->name = (char *) xmalloc (SBYTES (display_name) + 1);
11310 strncpy (terminal->name, SDATA (display_name), SBYTES (display_name));
11311 terminal->name[SBYTES (display_name)] = 0;
b6660415 11312
dc6f92b8 11313#if 0
7a13e894 11314 XSetAfterFunction (x_current_display, x_trace_wire);
c118dd06 11315#endif /* ! 0 */
7a13e894
RS
11316
11317 dpyinfo->x_id_name
d5db4077
KR
11318 = (char *) xmalloc (SBYTES (Vinvocation_name)
11319 + SBYTES (Vsystem_name)
7a13e894
RS
11320 + 2);
11321 sprintf (dpyinfo->x_id_name, "%s@%s",
d5db4077 11322 SDATA (Vinvocation_name), SDATA (Vsystem_name));
28430d3c
JB
11323
11324 /* Figure out which modifier bits mean what. */
334208b7 11325 x_find_modifier_meanings (dpyinfo);
f451eb13 11326
ab648270 11327 /* Get the scroll bar cursor. */
6b61353c
KH
11328#ifdef USE_GTK
11329 /* We must create a GTK cursor, it is required for GTK widgets. */
11330 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
11331#endif /* USE_GTK */
11332
7a13e894 11333 dpyinfo->vertical_scroll_bar_cursor
334208b7 11334 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
f451eb13 11335
334208b7
RS
11336 xrdb = x_load_resources (dpyinfo->display, xrm_option,
11337 resource_name, EMACS_CLASS);
11338#ifdef HAVE_XRMSETDATABASE
11339 XrmSetDatabase (dpyinfo->display, xrdb);
11340#else
11341 dpyinfo->display->db = xrdb;
11342#endif
547d9db8 11343 /* Put the rdb where we can find it in a way that works on
7a13e894
RS
11344 all versions. */
11345 dpyinfo->xrdb = xrdb;
334208b7
RS
11346
11347 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
11348 DefaultScreen (dpyinfo->display));
5ff67d81 11349 select_visual (dpyinfo);
43bd1b2b 11350 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
334208b7
RS
11351 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
11352 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
11353 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
6b61353c 11354 dpyinfo->client_leader_window = 0;
334208b7
RS
11355 dpyinfo->grabbed = 0;
11356 dpyinfo->reference_count = 0;
11357 dpyinfo->icon_bitmap_id = -1;
06a2c219 11358 dpyinfo->font_table = NULL;
7a13e894
RS
11359 dpyinfo->n_fonts = 0;
11360 dpyinfo->font_table_size = 0;
11361 dpyinfo->bitmaps = 0;
11362 dpyinfo->bitmaps_size = 0;
11363 dpyinfo->bitmaps_last = 0;
11364 dpyinfo->scratch_cursor_gc = 0;
11365 dpyinfo->mouse_face_mouse_frame = 0;
11366 dpyinfo->mouse_face_deferred_gc = 0;
11367 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
11368 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
06a2c219 11369 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
7a13e894 11370 dpyinfo->mouse_face_window = Qnil;
0a61c667 11371 dpyinfo->mouse_face_overlay = Qnil;
7a13e894
RS
11372 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
11373 dpyinfo->mouse_face_defer = 0;
66301061 11374 dpyinfo->mouse_face_hidden = 0;
0f941935
KH
11375 dpyinfo->x_focus_frame = 0;
11376 dpyinfo->x_focus_event_frame = 0;
11377 dpyinfo->x_highlight_frame = 0;
06a2c219 11378 dpyinfo->image_cache = make_image_cache ();
6b61353c
KH
11379 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
11380
11381 /* See if we can construct pixel values from RGB values. */
11382 dpyinfo->red_bits = dpyinfo->blue_bits = dpyinfo->green_bits = 0;
11383 dpyinfo->red_offset = dpyinfo->blue_offset = dpyinfo->green_offset = 0;
11384
11385 if (dpyinfo->visual->class == TrueColor)
11386 {
11387 get_bits_and_offset (dpyinfo->visual->red_mask,
11388 &dpyinfo->red_bits, &dpyinfo->red_offset);
11389 get_bits_and_offset (dpyinfo->visual->blue_mask,
11390 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
11391 get_bits_and_offset (dpyinfo->visual->green_mask,
11392 &dpyinfo->green_bits, &dpyinfo->green_offset);
11393 }
334208b7 11394
43bd1b2b 11395 /* See if a private colormap is requested. */
5ff67d81
GM
11396 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
11397 {
11398 if (dpyinfo->visual->class == PseudoColor)
11399 {
11400 Lisp_Object value;
11401 value = display_x_get_resource (dpyinfo,
11402 build_string ("privateColormap"),
11403 build_string ("PrivateColormap"),
11404 Qnil, Qnil);
11405 if (STRINGP (value)
d5db4077
KR
11406 && (!strcmp (SDATA (value), "true")
11407 || !strcmp (SDATA (value), "on")))
5ff67d81
GM
11408 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
11409 }
43bd1b2b 11410 }
5ff67d81
GM
11411 else
11412 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
11413 dpyinfo->visual, AllocNone);
7d0393cf 11414
06a2c219
GM
11415 {
11416 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
11417 double pixels = DisplayHeight (dpyinfo->display, screen_number);
11418 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
2c51754c
SM
11419 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
11420 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
06a2c219
GM
11421 pixels = DisplayWidth (dpyinfo->display, screen_number);
11422 mm = DisplayWidthMM (dpyinfo->display, screen_number);
056ce195 11423 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
2c51754c 11424 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
06a2c219 11425 }
7d0393cf 11426
334208b7
RS
11427 dpyinfo->Xatom_wm_protocols
11428 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
11429 dpyinfo->Xatom_wm_take_focus
11430 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
11431 dpyinfo->Xatom_wm_save_yourself
11432 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
11433 dpyinfo->Xatom_wm_delete_window
11434 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
11435 dpyinfo->Xatom_wm_change_state
11436 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
11437 dpyinfo->Xatom_wm_configure_denied
11438 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
11439 dpyinfo->Xatom_wm_window_moved
11440 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
6b61353c
KH
11441 dpyinfo->Xatom_wm_client_leader
11442 = XInternAtom (dpyinfo->display, "WM_CLIENT_LEADER", False);
334208b7
RS
11443 dpyinfo->Xatom_editres
11444 = XInternAtom (dpyinfo->display, "Editres", False);
11445 dpyinfo->Xatom_CLIPBOARD
11446 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
11447 dpyinfo->Xatom_TIMESTAMP
11448 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
11449 dpyinfo->Xatom_TEXT
11450 = XInternAtom (dpyinfo->display, "TEXT", False);
dc43ef94
KH
11451 dpyinfo->Xatom_COMPOUND_TEXT
11452 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
c62525b7
KH
11453 dpyinfo->Xatom_UTF8_STRING
11454 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
334208b7
RS
11455 dpyinfo->Xatom_DELETE
11456 = XInternAtom (dpyinfo->display, "DELETE", False);
11457 dpyinfo->Xatom_MULTIPLE
11458 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
11459 dpyinfo->Xatom_INCR
11460 = XInternAtom (dpyinfo->display, "INCR", False);
11461 dpyinfo->Xatom_EMACS_TMP
11462 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
11463 dpyinfo->Xatom_TARGETS
11464 = XInternAtom (dpyinfo->display, "TARGETS", False);
11465 dpyinfo->Xatom_NULL
11466 = XInternAtom (dpyinfo->display, "NULL", False);
11467 dpyinfo->Xatom_ATOM_PAIR
11468 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
dc43ef94
KH
11469 /* For properties of font. */
11470 dpyinfo->Xatom_PIXEL_SIZE
11471 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
6875d1ae
KH
11472 dpyinfo->Xatom_AVERAGE_WIDTH
11473 = XInternAtom (dpyinfo->display, "AVERAGE_WIDTH", False);
dc43ef94
KH
11474 dpyinfo->Xatom_MULE_BASELINE_OFFSET
11475 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
11476 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
11477 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
f78798df
KH
11478 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
11479 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
334208b7 11480
06a2c219
GM
11481 /* Ghostscript support. */
11482 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
11483 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
7d0393cf 11484
06a2c219
GM
11485 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
11486 False);
11487
81c02ebe
JD
11488 dpyinfo->Xatom_XEMBED = XInternAtom (dpyinfo->display, "_XEMBED",
11489 False);
11490
547d9db8
KH
11491 dpyinfo->cut_buffers_initialized = 0;
11492
93acf8f3
JD
11493 dpyinfo->x_dnd_atoms_size = 8;
11494 dpyinfo->x_dnd_atoms_length = 0;
11495 dpyinfo->x_dnd_atoms = xmalloc (sizeof (*dpyinfo->x_dnd_atoms)
11496 * dpyinfo->x_dnd_atoms_size);
11497
1c8591d0
JD
11498 dpyinfo->net_supported_atoms = NULL;
11499 dpyinfo->nr_net_supported_atoms = 0;
11500 dpyinfo->net_supported_window = 0;
11501
334208b7
RS
11502 connection = ConnectionNumber (dpyinfo->display);
11503 dpyinfo->connection = connection;
11504
dc43ef94 11505 {
5d7cc324
RS
11506 char null_bits[1];
11507
11508 null_bits[0] = 0x00;
dc43ef94
KH
11509
11510 dpyinfo->null_pixel
7d0393cf 11511 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
dc43ef94
KH
11512 null_bits, 1, 1, (long) 0, (long) 0,
11513 1);
11514 }
11515
06a2c219
GM
11516 {
11517 extern int gray_bitmap_width, gray_bitmap_height;
10659c77 11518 extern char *gray_bitmap_bits;
06a2c219
GM
11519 dpyinfo->gray
11520 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
11521 gray_bitmap_bits,
11522 gray_bitmap_width, gray_bitmap_height,
11523 (unsigned long) 1, (unsigned long) 0, 1);
11524 }
11525
f5d11644
GM
11526#ifdef HAVE_X_I18N
11527 xim_initialize (dpyinfo, resource_name);
11528#endif
7d0393cf 11529
87485d6f
MW
11530#ifdef subprocesses
11531 /* This is only needed for distinguishing keyboard and process input. */
334208b7 11532 if (connection != 0)
7a13e894 11533 add_keyboard_wait_descriptor (connection);
87485d6f 11534#endif
6d4238f3 11535
dc6f92b8 11536#ifdef F_SETOWN
334208b7 11537 fcntl (connection, F_SETOWN, getpid ());
c118dd06 11538#endif /* ! defined (F_SETOWN) */
dc6f92b8
JB
11539
11540#ifdef SIGIO
eee20f6a
KH
11541 if (interrupt_input)
11542 init_sigio (connection);
c118dd06 11543#endif /* ! defined (SIGIO) */
dc6f92b8 11544
51b592fb 11545#ifdef USE_LUCID
51b592fb
RS
11546 {
11547 Display *dpy = dpyinfo->display;
11548 XrmValue d, fr, to;
11549 Font font;
7d0393cf 11550
51b592fb
RS
11551 d.addr = (XPointer)&dpy;
11552 d.size = sizeof (Display *);
11553 fr.addr = XtDefaultFont;
11554 fr.size = sizeof (XtDefaultFont);
11555 to.size = sizeof (Font *);
11556 to.addr = (XPointer)&font;
9ba8e10d 11557 x_catch_errors (dpy);
51b592fb
RS
11558 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
11559 abort ();
11560 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
11561 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
4545fa20 11562 x_uncatch_errors ();
51b592fb 11563 }
f8c39f51 11564#endif
51b592fb 11565
34e23e5a
GM
11566 /* See if we should run in synchronous mode. This is useful
11567 for debugging X code. */
11568 {
11569 Lisp_Object value;
11570 value = display_x_get_resource (dpyinfo,
11571 build_string ("synchronous"),
11572 build_string ("Synchronous"),
11573 Qnil, Qnil);
11574 if (STRINGP (value)
d5db4077
KR
11575 && (!strcmp (SDATA (value), "true")
11576 || !strcmp (SDATA (value), "on")))
34e23e5a
GM
11577 XSynchronize (dpyinfo->display, True);
11578 }
62fe13a4 11579
51f3cc3b
DL
11580 {
11581 Lisp_Object value;
11582 value = display_x_get_resource (dpyinfo,
11583 build_string ("useXIM"),
11584 build_string ("UseXIM"),
11585 Qnil, Qnil);
539465b3 11586#ifdef USE_XIM
51f3cc3b 11587 if (STRINGP (value)
056ce195
SM
11588 && (!strcmp (SDATA (value), "false")
11589 || !strcmp (SDATA (value), "off")))
51f3cc3b 11590 use_xim = 0;
539465b3
KS
11591#else
11592 if (STRINGP (value)
056ce195
SM
11593 && (!strcmp (SDATA (value), "true")
11594 || !strcmp (SDATA (value), "on")))
539465b3
KS
11595 use_xim = 1;
11596#endif
51f3cc3b 11597 }
7d0393cf 11598
6b61353c 11599#ifdef HAVE_X_SM
d51abf22
KL
11600 /* Only do this for the very first display in the Emacs session.
11601 Ignore X session management when Emacs was first started on a
11602 tty. */
6ed8eeff 11603 if (terminal->id == 1)
6b61353c
KH
11604 x_session_initialize (dpyinfo);
11605#endif
11606
60439948
KH
11607 UNBLOCK_INPUT;
11608
7a13e894
RS
11609 return dpyinfo;
11610}
11611\f
ab797f65 11612/* Get rid of display DPYINFO, deleting all frames on it,
7a13e894 11613 and without sending any more commands to the X server. */
dc6f92b8 11614
7a13e894
RS
11615void
11616x_delete_display (dpyinfo)
11617 struct x_display_info *dpyinfo;
11618{
6b61353c 11619 int i;
6ed8eeff 11620 struct terminal *t;
dc2be2fa 11621
ab797f65
KL
11622 /* Close all frames and delete the generic struct terminal for this
11623 X display. */
6ed8eeff
KL
11624 for (t = terminal_list; t; t = t->next_terminal)
11625 if (t->type == output_x_window && t->display_info.x == dpyinfo)
dc2be2fa 11626 {
ea1bf8c7 11627#ifdef HAVE_X_SM
ab797f65 11628 /* Close X session management when we close its display. */
6ed8eeff 11629 if (t->id == 1 && x_session_have_connection ())
d51abf22 11630 x_session_close();
ea1bf8c7 11631#endif
6ed8eeff 11632 delete_terminal (t);
dc2be2fa
KL
11633 break;
11634 }
6b61353c 11635
7a13e894
RS
11636 delete_keyboard_wait_descriptor (dpyinfo->connection);
11637
11638 /* Discard this display from x_display_name_list and x_display_list.
11639 We can't use Fdelq because that can quit. */
11640 if (! NILP (x_display_name_list)
8e713be6
KR
11641 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
11642 x_display_name_list = XCDR (x_display_name_list);
7a13e894
RS
11643 else
11644 {
11645 Lisp_Object tail;
11646
11647 tail = x_display_name_list;
8e713be6 11648 while (CONSP (tail) && CONSP (XCDR (tail)))
7a13e894 11649 {
bffcfca9 11650 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
7a13e894 11651 {
f3fbd155 11652 XSETCDR (tail, XCDR (XCDR (tail)));
7a13e894
RS
11653 break;
11654 }
8e713be6 11655 tail = XCDR (tail);
7a13e894
RS
11656 }
11657 }
11658
9bda743f
GM
11659 if (next_noop_dpyinfo == dpyinfo)
11660 next_noop_dpyinfo = dpyinfo->next;
11661
7a13e894
RS
11662 if (x_display_list == dpyinfo)
11663 x_display_list = dpyinfo->next;
7f9c7f94
RS
11664 else
11665 {
11666 struct x_display_info *tail;
7a13e894 11667
7f9c7f94
RS
11668 for (tail = x_display_list; tail; tail = tail->next)
11669 if (tail->next == dpyinfo)
11670 tail->next = tail->next->next;
11671 }
7a13e894 11672
0d777288
RS
11673#ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
11674#ifndef AIX /* On AIX, XCloseDisplay calls this. */
7f9c7f94
RS
11675 XrmDestroyDatabase (dpyinfo->xrdb);
11676#endif
0d777288 11677#endif
f5d11644
GM
11678#ifdef HAVE_X_I18N
11679 if (dpyinfo->xim)
11680 xim_close_dpy (dpyinfo);
11681#endif
7d0393cf 11682
52e42a75
KH
11683#ifdef USE_FONT_BACKEND
11684 if (! enable_font_backend)
11685 {
11686#endif
6b61353c
KH
11687 /* Free the font names in the font table. */
11688 for (i = 0; i < dpyinfo->n_fonts; i++)
11689 if (dpyinfo->font_table[i].name)
11690 {
11691 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
11692 xfree (dpyinfo->font_table[i].full_name);
11693 xfree (dpyinfo->font_table[i].name);
11694 }
11695
4fbd0f6d
KS
11696 if (dpyinfo->font_table)
11697 {
11698 if (dpyinfo->font_table->font_encoder)
11699 xfree (dpyinfo->font_table->font_encoder);
11700 xfree (dpyinfo->font_table);
11701 }
52e42a75
KH
11702#ifdef USE_FONT_BACKEND
11703 }
11704#endif
11705
4fbd0f6d
KS
11706 if (dpyinfo->x_id_name)
11707 xfree (dpyinfo->x_id_name);
11708 if (dpyinfo->color_cells)
11709 xfree (dpyinfo->color_cells);
b9737ad3 11710 xfree (dpyinfo);
7a13e894 11711}
f04e1297 11712
442a09ea
KS
11713#ifdef USE_X_TOOLKIT
11714
11715/* Atimer callback function for TIMER. Called every 0.1s to process
11716 Xt timeouts, if needed. We must avoid calling XtAppPending as
11717 much as possible because that function does an implicit XFlush
11718 that slows us down. */
11719
11720static void
11721x_process_timeouts (timer)
11722 struct atimer *timer;
11723{
98a20c65
CY
11724 BLOCK_INPUT;
11725 x_timeout_atimer_activated_flag = 0;
442a09ea
KS
11726 if (toolkit_scroll_bar_interaction || popup_activated ())
11727 {
442a09ea
KS
11728 while (XtAppPending (Xt_app_con) & XtIMTimer)
11729 XtAppProcessEvent (Xt_app_con, XtIMTimer);
98a20c65
CY
11730 /* Reactivate the atimer for next time. */
11731 x_activate_timeout_atimer ();
11732 }
11733 UNBLOCK_INPUT;
11734}
11735
11736/* Install an asynchronous timer that processes Xt timeout events
11737 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
11738 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
11739 function whenever these variables are set. This is necessary
11740 because some widget sets use timeouts internally, for example the
11741 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
11742 processed, these widgets don't behave normally. */
11743
11744void
11745x_activate_timeout_atimer ()
11746{
11747 BLOCK_INPUT;
11748 if (!x_timeout_atimer_activated_flag)
11749 {
11750 EMACS_TIME interval;
11751
11752 EMACS_SET_SECS_USECS (interval, 0, 100000);
11753 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
11754 x_timeout_atimer_activated_flag = 1;
442a09ea 11755 }
98a20c65 11756 UNBLOCK_INPUT;
442a09ea
KS
11757}
11758
11759#endif /* USE_X_TOOLKIT */
11760
7a13e894
RS
11761\f
11762/* Set up use of X before we make the first connection. */
11763
f8240bf8
KS
11764extern frame_parm_handler x_frame_parm_handlers[];
11765
06a2c219 11766static struct redisplay_interface x_redisplay_interface =
daf01701
KL
11767 {
11768 x_frame_parm_handlers,
11769 x_produce_glyphs,
11770 x_write_glyphs,
11771 x_insert_glyphs,
11772 x_clear_end_of_line,
11773 x_scroll_run,
11774 x_after_update_window_line,
11775 x_update_window_begin,
11776 x_update_window_end,
11777 x_cursor_to,
11778 x_flush,
61087d3f 11779#ifdef XFlush
daf01701 11780 x_flush,
442a09ea 11781#else
daf01701 11782 0, /* flush_display_optional */
442a09ea 11783#endif
daf01701
KL
11784 x_clear_window_mouse_face,
11785 x_get_glyph_overhangs,
11786 x_fix_overlapping_area,
11787 x_draw_fringe_bitmap,
e581a466
KL
11788 0, /* define_fringe_bitmap */
11789 0, /* destroy_fringe_bitmap */
daf01701
KL
11790 x_per_char_metric,
11791 x_encode_char,
11792 x_compute_glyph_string_overhangs,
11793 x_draw_glyph_string,
11794 x_define_frame_cursor,
11795 x_clear_frame_area,
11796 x_draw_window_cursor,
11797 x_draw_vertical_window_border,
11798 x_shift_glyphs_for_insert
11799 };
06a2c219 11800
428a555e
KL
11801
11802/* This function is called when the last frame on a display is deleted. */
11803void
6ed8eeff 11804x_delete_terminal (struct terminal *terminal)
428a555e 11805{
6ed8eeff 11806 struct x_display_info *dpyinfo = terminal->display_info.x;
90e2f38a 11807 int i;
a98f1617 11808
ab797f65
KL
11809 /* Protect against recursive calls. Fdelete_frame in
11810 delete_terminal calls us back when it deletes our last frame. */
89acb56d 11811 if (!terminal->name)
a98f1617 11812 return;
3dde8959 11813
335fcf6c 11814 BLOCK_INPUT;
52e42a75
KH
11815#ifdef USE_FONT_BACKEND
11816 if (! enable_font_backend)
11817#endif
335fcf6c
KL
11818 /* Free the fonts in the font table. */
11819 for (i = 0; i < dpyinfo->n_fonts; i++)
11820 if (dpyinfo->font_table[i].name)
11821 {
11822 XFreeFont (dpyinfo->display, dpyinfo->font_table[i].font);
11823 }
11824
11825 x_destroy_all_bitmaps (dpyinfo);
11826 XSetCloseDownMode (dpyinfo->display, DestroyAll);
11827
e4a92752
KL
11828#ifdef USE_GTK
11829 xg_display_close (dpyinfo->display);
12b6af5c
KL
11830#else
11831#ifdef USE_X_TOOLKIT
11832 XtCloseDisplay (dpyinfo->display);
335fcf6c
KL
11833#else
11834 XCloseDisplay (dpyinfo->display);
e4a92752 11835#endif
12b6af5c 11836#endif /* ! USE_GTK */
3dde8959
KL
11837
11838 x_delete_display (dpyinfo);
335fcf6c 11839 UNBLOCK_INPUT;
428a555e
KL
11840}
11841
47cc8819
DN
11842/* Create a struct terminal, initialize it with the X11 specific
11843 functions and make DISPLAY->TERMINAL point to it. */
428a555e 11844
6ed8eeff
KL
11845static struct terminal *
11846x_create_terminal (struct x_display_info *dpyinfo)
428a555e 11847{
6ed8eeff 11848 struct terminal *terminal;
428a555e 11849
6ed8eeff 11850 terminal = create_terminal ();
428a555e 11851
6ed8eeff
KL
11852 terminal->type = output_x_window;
11853 terminal->display_info.x = dpyinfo;
11854 dpyinfo->terminal = terminal;
bedb9c0e
KL
11855
11856 /* kboard is initialized in x_term_init. */
428a555e 11857
6ed8eeff
KL
11858 terminal->clear_frame_hook = x_clear_frame;
11859 terminal->ins_del_lines_hook = x_ins_del_lines;
11860 terminal->delete_glyphs_hook = x_delete_glyphs;
11861 terminal->ring_bell_hook = XTring_bell;
11862 terminal->reset_terminal_modes_hook = XTreset_terminal_modes;
11863 terminal->set_terminal_modes_hook = XTset_terminal_modes;
11864 terminal->update_begin_hook = x_update_begin;
11865 terminal->update_end_hook = x_update_end;
11866 terminal->set_terminal_window_hook = XTset_terminal_window;
11867 terminal->read_socket_hook = XTread_socket;
11868 terminal->frame_up_to_date_hook = XTframe_up_to_date;
11869 terminal->mouse_position_hook = XTmouse_position;
11870 terminal->frame_rehighlight_hook = XTframe_rehighlight;
11871 terminal->frame_raise_lower_hook = XTframe_raise_lower;
974b73e8 11872 terminal->fullscreen_hook = XTfullscreen_hook;
6ed8eeff
KL
11873 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
11874 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
11875 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
11876 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
11877
11878 terminal->delete_frame_hook = x_destroy_window;
11879 terminal->delete_terminal_hook = x_delete_terminal;
428a555e 11880
6ed8eeff
KL
11881 terminal->rif = &x_redisplay_interface;
11882 terminal->scroll_region_ok = 1; /* We'll scroll partial frames. */
11883 terminal->char_ins_del_ok = 1;
11884 terminal->line_ins_del_ok = 1; /* We'll just blt 'em. */
11885 terminal->fast_clear_end_of_line = 1; /* X does this well. */
11886 terminal->memory_below_frame = 0; /* We don't remember what scrolls
428a555e
KL
11887 off the bottom. */
11888
6ed8eeff 11889 return terminal;
428a555e 11890}
06a2c219 11891
dfcf069d 11892void
7a13e894
RS
11893x_initialize ()
11894{
dc6f92b8
JB
11895 baud_rate = 19200;
11896
7a13e894 11897 x_noop_count = 0;
9ea173e8 11898 last_tool_bar_item = -1;
06a2c219 11899 any_help_event_p = 0;
6b61353c 11900 ignore_next_mouse_click_timeout = 0;
11e887e2
JD
11901#ifdef HAVE_X_SM
11902 x_session_initialized = 0;
11903#endif
6b61353c
KH
11904
11905#ifdef USE_GTK
11906 current_count = -1;
11907#endif
7d0393cf 11908
b30b24cb 11909 /* Try to use interrupt input; if we can't, then start polling. */
a712a8c3 11910 Fset_input_interrupt_mode (Qt);
b30b24cb 11911
7f9c7f94
RS
11912#ifdef USE_X_TOOLKIT
11913 XtToolkitInitialize ();
651f03b6 11914
7f9c7f94 11915 Xt_app_con = XtCreateApplicationContext ();
7d0393cf 11916
651f03b6
GM
11917 /* Register a converter from strings to pixels, which uses
11918 Emacs' color allocation infrastructure. */
11919 XtAppSetTypeConverter (Xt_app_con,
11920 XtRString, XtRPixel, cvt_string_to_pixel,
11921 cvt_string_to_pixel_args,
11922 XtNumber (cvt_string_to_pixel_args),
11923 XtCacheByDisplay, cvt_pixel_dtor);
11924
665881ad 11925 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
db74249b 11926#endif
7d0393cf 11927
eccc05db 11928#ifdef USE_TOOLKIT_SCROLL_BARS
488dd4c4 11929#ifndef USE_GTK
ec18280f
SM
11930 xaw3d_arrow_scroll = False;
11931 xaw3d_pick_top = True;
488dd4c4 11932#endif
7f9c7f94
RS
11933#endif
11934
58769bee 11935 /* Note that there is no real way portable across R3/R4 to get the
c118dd06 11936 original error handler. */
e99db5a1 11937 XSetErrorHandler (x_error_handler);
334208b7 11938 XSetIOErrorHandler (x_io_error_quitter);
dc6f92b8 11939
06a2c219 11940 /* Disable Window Change signals; they are handled by X events. */
3de8a253 11941#if 0 /* Don't. We may want to open tty frames later. */
dc6f92b8
JB
11942#ifdef SIGWINCH
11943 signal (SIGWINCH, SIG_DFL);
a59facca 11944#endif /* SIGWINCH */
3de8a253 11945#endif
dc6f92b8 11946
92e2441b 11947 signal (SIGPIPE, x_connection_signal);
dc6f92b8 11948}
55123275 11949
06a2c219 11950
55123275
JB
11951void
11952syms_of_xterm ()
11953{
5badc98d 11954 x_error_message = NULL;
e99db5a1 11955
7a13e894
RS
11956 staticpro (&x_display_name_list);
11957 x_display_name_list = Qnil;
334208b7 11958
ab648270 11959 staticpro (&last_mouse_scroll_bar);
e53cb100 11960 last_mouse_scroll_bar = Qnil;
59e755be
KH
11961
11962 staticpro (&Qvendor_specific_keysyms);
11963 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
2237cac9 11964
c997eae5
SM
11965 staticpro (&Qlatin_1);
11966 Qlatin_1 = intern ("latin-1");
11967
2237cac9
RS
11968 staticpro (&last_mouse_press_frame);
11969 last_mouse_press_frame = Qnil;
06a2c219 11970
a72d5ce5 11971 DEFVAR_BOOL ("x-use-underline-position-properties",
7ee72033
MB
11972 &x_use_underline_position_properties,
11973 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
5820771b 11974A value of nil means ignore them. If you encounter fonts with bogus
228299fa
GM
11975UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
11976to 4.1, set this to nil. */);
a72d5ce5
GM
11977 x_use_underline_position_properties = 1;
11978
30f27523
KS
11979 DEFVAR_BOOL ("x-underline-at-descent-line",
11980 &x_underline_at_descent_line,
11981 doc: /* *Non-nil means to draw the underline at the same place as the descent line.
5820771b
JB
11982A value of nil means to draw the underline according to the value of the
11983variable `x-use-underline-position-properties', which is usually at the
11984baseline level. The default value is nil. */);
30f27523
KS
11985 x_underline_at_descent_line = 0;
11986
6b61353c
KH
11987 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
11988 &x_mouse_click_focus_ignore_position,
11989 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
11990This variable is only used when the window manager requires that you
11991click on a frame to select it (give it focus). In that case, a value
11992of nil, means that the selected window and cursor position changes to
11993reflect the mouse click position, while a non-nil value means that the
11994selected window or cursor position is preserved. */);
11995 x_mouse_click_focus_ignore_position = 0;
11996
7ee72033
MB
11997 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
11998 doc: /* What X toolkit scroll bars Emacs uses.
228299fa
GM
11999A value of nil means Emacs doesn't use X toolkit scroll bars.
12000Otherwise, value is a symbol describing the X toolkit. */);
eccc05db 12001#ifdef USE_TOOLKIT_SCROLL_BARS
5bf04520
GM
12002#ifdef USE_MOTIF
12003 Vx_toolkit_scroll_bars = intern ("motif");
12004#elif defined HAVE_XAW3D
12005 Vx_toolkit_scroll_bars = intern ("xaw3d");
488dd4c4
JD
12006#elif USE_GTK
12007 Vx_toolkit_scroll_bars = intern ("gtk");
5bf04520
GM
12008#else
12009 Vx_toolkit_scroll_bars = intern ("xaw");
12010#endif
06a2c219 12011#else
5bf04520 12012 Vx_toolkit_scroll_bars = Qnil;
06a2c219
GM
12013#endif
12014
06a2c219
GM
12015 staticpro (&last_mouse_motion_frame);
12016 last_mouse_motion_frame = Qnil;
7d0393cf 12017
98659da6
KG
12018 Qmodifier_value = intern ("modifier-value");
12019 Qalt = intern ("alt");
12020 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
12021 Qhyper = intern ("hyper");
12022 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
12023 Qmeta = intern ("meta");
12024 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
12025 Qsuper = intern ("super");
12026 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
7d0393cf 12027
98659da6
KG
12028 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
12029 doc: /* Which keys Emacs uses for the alt modifier.
12030This should be one of the symbols `alt', `hyper', `meta', `super'.
12031For example, `alt' means use the Alt_L and Alt_R keysyms. The default
12032is nil, which is the same as `alt'. */);
12033 Vx_alt_keysym = Qnil;
7d0393cf 12034
98659da6
KG
12035 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
12036 doc: /* Which keys Emacs uses for the hyper modifier.
12037This should be one of the symbols `alt', `hyper', `meta', `super'.
12038For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
12039default is nil, which is the same as `hyper'. */);
12040 Vx_hyper_keysym = Qnil;
7d0393cf 12041
98659da6
KG
12042 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
12043 doc: /* Which keys Emacs uses for the meta modifier.
12044This should be one of the symbols `alt', `hyper', `meta', `super'.
12045For example, `meta' means use the Meta_L and Meta_R keysyms. The
12046default is nil, which is the same as `meta'. */);
12047 Vx_meta_keysym = Qnil;
7d0393cf 12048
98659da6
KG
12049 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
12050 doc: /* Which keys Emacs uses for the super modifier.
12051This should be one of the symbols `alt', `hyper', `meta', `super'.
12052For example, `super' means use the Super_L and Super_R keysyms. The
12053default is nil, which is the same as `super'. */);
12054 Vx_super_keysym = Qnil;
12055
dbd4b028
DL
12056 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
12057 doc: /* Hash table of character codes indexed by X keysym codes. */);
12058 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
12059 make_float (DEFAULT_REHASH_SIZE),
12060 make_float (DEFAULT_REHASH_THRESHOLD),
12061 Qnil, Qnil, Qnil);
55123275 12062}
6cf0ae86 12063
1d6c120a 12064#endif /* HAVE_X_WINDOWS */
6b61353c
KH
12065
12066/* arch-tag: 6d4e4cb7-abc1-4302-9585-d84dcfb09d0f
12067 (do not change this comment) */