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