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