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