* lisp/simple.el (clone-indirect-buffer): Mention the return value.
[emacs.git] / src / xterm.c
1 /* X Communication module for terminals which understand the X protocol.
2
3 Copyright (C) 1989, 1993-2014 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
21 /* Xt features made by Fred Pierresteguy. */
22
23 #include <config.h>
24 #include <stdio.h>
25
26 #include "lisp.h"
27 #include "blockinput.h"
28 #include "syssignal.h"
29
30 /* This may include sys/types.h, and that somehow loses
31 if this is not done before the other system files. */
32 #include "xterm.h"
33 #include <X11/cursorfont.h>
34
35 /* If we have Xfixes extension, use it for pointer blanking. */
36 #ifdef HAVE_XFIXES
37 #include <X11/extensions/Xfixes.h>
38 #endif
39
40 /* Using Xft implies that XRender is available. */
41 #ifdef HAVE_XFT
42 #include <X11/extensions/Xrender.h>
43 #endif
44
45 /* Load sys/types.h if not already loaded.
46 In some systems loading it twice is suicidal. */
47 #ifndef makedev
48 #include <sys/types.h>
49 #endif /* makedev */
50
51 #include <sys/ioctl.h>
52
53 #include "systime.h"
54
55 #include <fcntl.h>
56 #include <errno.h>
57 #include <sys/stat.h>
58 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
59 /* #include <sys/param.h> */
60
61 #include "charset.h"
62 #include "character.h"
63 #include "coding.h"
64 #include "frame.h"
65 #include "dispextern.h"
66 #include "fontset.h"
67 #include "termhooks.h"
68 #include "termopts.h"
69 #include "termchar.h"
70 #include "emacs-icon.h"
71 #include "disptab.h"
72 #include "buffer.h"
73 #include "window.h"
74 #include "keyboard.h"
75 #include "intervals.h"
76 #include "process.h"
77 #include "atimer.h"
78 #include "keymap.h"
79 #include "font.h"
80 #include "xsettings.h"
81 #include "xgselect.h"
82 #include "sysselect.h"
83 #include "menu.h"
84
85 #ifdef USE_X_TOOLKIT
86 #include <X11/Shell.h>
87 #endif
88
89 #include <unistd.h>
90
91 #ifdef USE_GTK
92 #include "gtkutil.h"
93 #ifdef HAVE_GTK3
94 #include <X11/Xproto.h>
95 #endif
96 #endif
97
98 #if defined (USE_LUCID) || defined (USE_MOTIF)
99 #include "../lwlib/xlwmenu.h"
100 #endif
101
102 #ifdef USE_X_TOOLKIT
103 #if !defined (NO_EDITRES)
104 #define HACK_EDITRES
105 extern void _XEditResCheckMessages (Widget, XtPointer, XEvent *, Boolean *);
106 #endif /* not NO_EDITRES */
107
108 /* Include toolkit specific headers for the scroll bar widget. */
109
110 #ifdef USE_TOOLKIT_SCROLL_BARS
111 #if defined USE_MOTIF
112 #include <Xm/Xm.h> /* For LESSTIF_VERSION */
113 #include <Xm/ScrollBar.h>
114 #else /* !USE_MOTIF i.e. use Xaw */
115
116 #ifdef HAVE_XAW3D
117 #include <X11/Xaw3d/Simple.h>
118 #include <X11/Xaw3d/Scrollbar.h>
119 #include <X11/Xaw3d/ThreeD.h>
120 #else /* !HAVE_XAW3D */
121 #include <X11/Xaw/Simple.h>
122 #include <X11/Xaw/Scrollbar.h>
123 #endif /* !HAVE_XAW3D */
124 #ifndef XtNpickTop
125 #define XtNpickTop "pickTop"
126 #endif /* !XtNpickTop */
127 #endif /* !USE_MOTIF */
128 #endif /* USE_TOOLKIT_SCROLL_BARS */
129
130 #endif /* USE_X_TOOLKIT */
131
132 #ifdef USE_X_TOOLKIT
133 #include "widget.h"
134 #ifndef XtNinitialState
135 #define XtNinitialState "initialState"
136 #endif
137 #endif
138
139 #include "bitmaps/gray.xbm"
140
141 #ifdef HAVE_XKB
142 #include <X11/XKBlib.h>
143 #endif
144
145 /* Default to using XIM if available. */
146 #ifdef USE_XIM
147 bool use_xim = true;
148 #else
149 bool use_xim = false; /* configure --without-xim */
150 #endif
151
152 /* Non-zero means that a HELP_EVENT has been generated since Emacs
153 start. */
154
155 static bool any_help_event_p;
156
157 /* This is a chain of structures for all the X displays currently in
158 use. */
159
160 struct x_display_info *x_display_list;
161
162 #ifdef USE_X_TOOLKIT
163
164 /* The application context for Xt use. */
165 XtAppContext Xt_app_con;
166 static String Xt_default_resources[] = {0};
167
168 /* Non-zero means user is interacting with a toolkit scroll bar. */
169 static bool toolkit_scroll_bar_interaction;
170
171 #endif /* USE_X_TOOLKIT */
172
173 /* Non-zero timeout value means ignore next mouse click if it arrives
174 before that timeout elapses (i.e. as part of the same sequence of
175 events resulting from clicking on a frame to select it). */
176
177 static Time ignore_next_mouse_click_timeout;
178
179 /* Used locally within XTread_socket. */
180
181 static int x_noop_count;
182
183 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
184
185 static Lisp_Object Qvendor_specific_keysyms;
186 static Lisp_Object Qlatin_1;
187
188 #ifdef USE_GTK
189 /* The name of the Emacs icon file. */
190 static Lisp_Object xg_default_icon_file;
191
192 /* Used in gtkutil.c. */
193 Lisp_Object Qx_gtk_map_stock;
194 #endif
195
196 /* Some functions take this as char *, not const char *. */
197 static char emacs_class[] = EMACS_CLASS;
198
199 enum xembed_info
200 {
201 XEMBED_MAPPED = 1 << 0
202 };
203
204 enum xembed_message
205 {
206 XEMBED_EMBEDDED_NOTIFY = 0,
207 XEMBED_WINDOW_ACTIVATE = 1,
208 XEMBED_WINDOW_DEACTIVATE = 2,
209 XEMBED_REQUEST_FOCUS = 3,
210 XEMBED_FOCUS_IN = 4,
211 XEMBED_FOCUS_OUT = 5,
212 XEMBED_FOCUS_NEXT = 6,
213 XEMBED_FOCUS_PREV = 7,
214
215 XEMBED_MODALITY_ON = 10,
216 XEMBED_MODALITY_OFF = 11,
217 XEMBED_REGISTER_ACCELERATOR = 12,
218 XEMBED_UNREGISTER_ACCELERATOR = 13,
219 XEMBED_ACTIVATE_ACCELERATOR = 14
220 };
221
222 static bool x_alloc_nearest_color_1 (Display *, Colormap, XColor *);
223 static void x_set_window_size_1 (struct frame *, int, int, int, bool);
224 static void x_raise_frame (struct frame *);
225 static void x_lower_frame (struct frame *);
226 static const XColor *x_color_cells (Display *, int *);
227 static int x_io_error_quitter (Display *);
228 static struct terminal *x_create_terminal (struct x_display_info *);
229 static void x_update_end (struct frame *);
230 static void XTframe_up_to_date (struct frame *);
231 static void x_clear_frame (struct frame *);
232 static _Noreturn void x_ins_del_lines (struct frame *, int, int);
233 static void frame_highlight (struct frame *);
234 static void frame_unhighlight (struct frame *);
235 static void x_new_focus_frame (struct x_display_info *, struct frame *);
236 static void x_focus_changed (int, int, struct x_display_info *,
237 struct frame *, struct input_event *);
238 static void XTframe_rehighlight (struct frame *);
239 static void x_frame_rehighlight (struct x_display_info *);
240 static void x_draw_hollow_cursor (struct window *, struct glyph_row *);
241 static void x_draw_bar_cursor (struct window *, struct glyph_row *, int,
242 enum text_cursor_kinds);
243
244 static void x_clip_to_row (struct window *, struct glyph_row *,
245 enum glyph_row_area, GC);
246 static void x_flush (struct frame *f);
247 static void x_update_begin (struct frame *);
248 static void x_update_window_begin (struct window *);
249 static struct scroll_bar *x_window_to_scroll_bar (Display *, Window, int);
250 static void x_scroll_bar_report_motion (struct frame **, Lisp_Object *,
251 enum scroll_bar_part *,
252 Lisp_Object *, Lisp_Object *,
253 Time *);
254 static void x_horizontal_scroll_bar_report_motion (struct frame **, Lisp_Object *,
255 enum scroll_bar_part *,
256 Lisp_Object *, Lisp_Object *,
257 Time *);
258 static int x_handle_net_wm_state (struct frame *, const XPropertyEvent *);
259 static void x_check_fullscreen (struct frame *);
260 static void x_check_expected_move (struct frame *, int, int);
261 static void x_sync_with_move (struct frame *, int, int, int);
262 static int handle_one_xevent (struct x_display_info *,
263 const XEvent *, int *,
264 struct input_event *);
265 #if ! (defined USE_X_TOOLKIT || defined USE_MOTIF)
266 static int x_dispatch_event (XEvent *, Display *);
267 #endif
268 /* Don't declare this _Noreturn because we want no
269 interference with debugging failing X calls. */
270 static void x_connection_closed (Display *, const char *);
271 static void x_wm_set_window_state (struct frame *, int);
272 static void x_wm_set_icon_pixmap (struct frame *, ptrdiff_t);
273 static void x_initialize (void);
274
275 static int get_current_wm_state (struct frame *, Window, int *, int *);
276
277 /* Flush display of frame F. */
278
279 static void
280 x_flush (struct frame *f)
281 {
282 eassert (f && FRAME_X_P (f));
283 /* Don't call XFlush when it is not safe to redisplay; the X
284 connection may be broken. */
285 if (!NILP (Vinhibit_redisplay))
286 return;
287
288 block_input ();
289 XFlush (FRAME_X_DISPLAY (f));
290 unblock_input ();
291 }
292
293
294 /* Remove calls to XFlush by defining XFlush to an empty replacement.
295 Calls to XFlush should be unnecessary because the X output buffer
296 is flushed automatically as needed by calls to XPending,
297 XNextEvent, or XWindowEvent according to the XFlush man page.
298 XTread_socket calls XPending. Removing XFlush improves
299 performance. */
300
301 #define XFlush(DISPLAY) (void) 0
302
303 \f
304 /***********************************************************************
305 Debugging
306 ***********************************************************************/
307
308 #if 0
309
310 /* This is a function useful for recording debugging information about
311 the sequence of occurrences in this file. */
312
313 struct record
314 {
315 char *locus;
316 int type;
317 };
318
319 struct record event_record[100];
320
321 int event_record_index;
322
323 void
324 record_event (char *locus, int type)
325 {
326 if (event_record_index == ARRAYELTS (event_record))
327 event_record_index = 0;
328
329 event_record[event_record_index].locus = locus;
330 event_record[event_record_index].type = type;
331 event_record_index++;
332 }
333
334 #endif /* 0 */
335
336
337 \f
338 /* Return the struct x_display_info corresponding to DPY. */
339
340 struct x_display_info *
341 x_display_info_for_display (Display *dpy)
342 {
343 struct x_display_info *dpyinfo;
344
345 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
346 if (dpyinfo->display == dpy)
347 return dpyinfo;
348
349 return 0;
350 }
351
352 static Window
353 x_find_topmost_parent (struct frame *f)
354 {
355 struct x_output *x = f->output_data.x;
356 Window win = None, wi = x->parent_desc;
357 Display *dpy = FRAME_X_DISPLAY (f);
358
359 while (wi != FRAME_DISPLAY_INFO (f)->root_window)
360 {
361 Window root;
362 Window *children;
363 unsigned int nchildren;
364
365 win = wi;
366 if (XQueryTree (dpy, win, &root, &wi, &children, &nchildren))
367 XFree (children);
368 else
369 break;
370 }
371
372 return win;
373 }
374
375 #define OPAQUE 0xffffffff
376
377 void
378 x_set_frame_alpha (struct frame *f)
379 {
380 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
381 Display *dpy = FRAME_X_DISPLAY (f);
382 Window win = FRAME_OUTER_WINDOW (f);
383 double alpha = 1.0;
384 double alpha_min = 1.0;
385 unsigned long opac;
386 Window parent;
387
388 if (dpyinfo->x_highlight_frame == f)
389 alpha = f->alpha[0];
390 else
391 alpha = f->alpha[1];
392
393 if (FLOATP (Vframe_alpha_lower_limit))
394 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
395 else if (INTEGERP (Vframe_alpha_lower_limit))
396 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
397
398 if (alpha < 0.0)
399 return;
400 else if (alpha > 1.0)
401 alpha = 1.0;
402 else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
403 alpha = alpha_min;
404
405 opac = alpha * OPAQUE;
406
407 x_catch_errors (dpy);
408
409 /* If there is a parent from the window manager, put the property there
410 also, to work around broken window managers that fail to do that.
411 Do this unconditionally as this function is called on reparent when
412 alpha has not changed on the frame. */
413
414 parent = x_find_topmost_parent (f);
415 if (parent != None)
416 XChangeProperty (dpy, parent, dpyinfo->Xatom_net_wm_window_opacity,
417 XA_CARDINAL, 32, PropModeReplace,
418 (unsigned char *) &opac, 1);
419
420 /* return unless necessary */
421 {
422 unsigned char *data;
423 Atom actual;
424 int rc, format;
425 unsigned long n, left;
426
427 rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
428 0, 1, False, XA_CARDINAL,
429 &actual, &format, &n, &left,
430 &data);
431
432 if (rc == Success && actual != None)
433 {
434 unsigned long value = *(unsigned long *)data;
435 XFree (data);
436 if (value == opac)
437 {
438 x_uncatch_errors ();
439 return;
440 }
441 }
442 }
443
444 XChangeProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
445 XA_CARDINAL, 32, PropModeReplace,
446 (unsigned char *) &opac, 1);
447 x_uncatch_errors ();
448 }
449
450 /***********************************************************************
451 Starting and ending an update
452 ***********************************************************************/
453
454 /* Start an update of frame F. This function is installed as a hook
455 for update_begin, i.e. it is called when update_begin is called.
456 This function is called prior to calls to x_update_window_begin for
457 each window being updated. Currently, there is nothing to do here
458 because all interesting stuff is done on a window basis. */
459
460 static void
461 x_update_begin (struct frame *f)
462 {
463 /* Nothing to do. */
464 }
465
466
467 /* Start update of window W. */
468
469 static void
470 x_update_window_begin (struct window *w)
471 {
472 struct frame *f = XFRAME (WINDOW_FRAME (w));
473 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
474
475 w->output_cursor = w->cursor;
476
477 block_input ();
478
479 if (f == hlinfo->mouse_face_mouse_frame)
480 {
481 /* Don't do highlighting for mouse motion during the update. */
482 hlinfo->mouse_face_defer = 1;
483
484 /* If F needs to be redrawn, simply forget about any prior mouse
485 highlighting. */
486 if (FRAME_GARBAGED_P (f))
487 hlinfo->mouse_face_window = Qnil;
488 }
489
490 unblock_input ();
491 }
492
493
494 /* Draw a vertical window border from (x,y0) to (x,y1) */
495
496 static void
497 x_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
498 {
499 struct frame *f = XFRAME (WINDOW_FRAME (w));
500 struct face *face;
501
502 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
503 if (face)
504 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
505 face->foreground);
506
507 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
508 f->output_data.x->normal_gc, x, y0, x, y1);
509 }
510
511 /* Draw a window divider from (x0,y0) to (x1,y1) */
512
513 static void
514 x_draw_window_divider (struct window *w, int x0, int x1, int y0, int y1)
515 {
516 struct frame *f = XFRAME (WINDOW_FRAME (w));
517 struct face *face = FACE_FROM_ID (f, WINDOW_DIVIDER_FACE_ID);
518 struct face *face_first = FACE_FROM_ID (f, WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
519 struct face *face_last = FACE_FROM_ID (f, WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
520 unsigned long color = face ? face->foreground : FRAME_FOREGROUND_PIXEL (f);
521 unsigned long color_first = (face_first
522 ? face_first->foreground
523 : FRAME_FOREGROUND_PIXEL (f));
524 unsigned long color_last = (face_last
525 ? face_last->foreground
526 : FRAME_FOREGROUND_PIXEL (f));
527 Display *display = FRAME_X_DISPLAY (f);
528 Window window = FRAME_X_WINDOW (f);
529
530 if (y1 - y0 > x1 - x0 && x1 - x0 > 2)
531 /* Vertical. */
532 {
533 XSetForeground (display, f->output_data.x->normal_gc, color_first);
534 XFillRectangle (display, window, f->output_data.x->normal_gc,
535 x0, y0, 1, y1 - y0);
536 XSetForeground (display, f->output_data.x->normal_gc, color);
537 XFillRectangle (display, window, f->output_data.x->normal_gc,
538 x0 + 1, y0, x1 - x0 - 2, y1 - y0);
539 XSetForeground (display, f->output_data.x->normal_gc, color_last);
540 XFillRectangle (display, window, f->output_data.x->normal_gc,
541 x1 - 1, y0, 1, y1 - y0);
542 }
543 else if (x1 - x0 > y1 - y0 && y1 - y0 > 3)
544 /* Horizontal. */
545 {
546 XSetForeground (display, f->output_data.x->normal_gc, color_first);
547 XFillRectangle (display, window, f->output_data.x->normal_gc,
548 x0, y0, x1 - x0, 1);
549 XSetForeground (display, f->output_data.x->normal_gc, color);
550 XFillRectangle (display, window, f->output_data.x->normal_gc,
551 x0, y0 + 1, x1 - x0, y1 - y0 - 2);
552 XSetForeground (display, f->output_data.x->normal_gc, color_last);
553 XFillRectangle (display, window, f->output_data.x->normal_gc,
554 x0, y1 - 1, x1 - x0, 1);
555 }
556 else
557 {
558 XSetForeground (display, f->output_data.x->normal_gc, color);
559 XFillRectangle (display, window, f->output_data.x->normal_gc,
560 x0, y0, x1 - x0, y1 - y0);
561 }
562 }
563
564 /* End update of window W.
565
566 Draw vertical borders between horizontally adjacent windows, and
567 display W's cursor if CURSOR_ON_P is non-zero.
568
569 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
570 glyphs in mouse-face were overwritten. In that case we have to
571 make sure that the mouse-highlight is properly redrawn.
572
573 W may be a menu bar pseudo-window in case we don't have X toolkit
574 support. Such windows don't have a cursor, so don't display it
575 here. */
576
577 static void
578 x_update_window_end (struct window *w, bool cursor_on_p,
579 bool mouse_face_overwritten_p)
580 {
581 if (!w->pseudo_window_p)
582 {
583 block_input ();
584
585 if (cursor_on_p)
586 display_and_set_cursor (w, 1,
587 w->output_cursor.hpos, w->output_cursor.vpos,
588 w->output_cursor.x, w->output_cursor.y);
589
590 if (draw_window_fringes (w, 1))
591 {
592 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
593 x_draw_right_divider (w);
594 else
595 x_draw_vertical_border (w);
596 }
597
598 unblock_input ();
599 }
600
601 /* If a row with mouse-face was overwritten, arrange for
602 XTframe_up_to_date to redisplay the mouse highlight. */
603 if (mouse_face_overwritten_p)
604 {
605 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
606
607 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
608 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
609 hlinfo->mouse_face_window = Qnil;
610 }
611 }
612
613
614 /* End update of frame F. This function is installed as a hook in
615 update_end. */
616
617 static void
618 x_update_end (struct frame *f)
619 {
620 /* Mouse highlight may be displayed again. */
621 MOUSE_HL_INFO (f)->mouse_face_defer = 0;
622
623 #ifndef XFlush
624 block_input ();
625 XFlush (FRAME_X_DISPLAY (f));
626 unblock_input ();
627 #endif
628 }
629
630
631 /* This function is called from various places in xdisp.c
632 whenever a complete update has been performed. */
633
634 static void
635 XTframe_up_to_date (struct frame *f)
636 {
637 if (FRAME_X_P (f))
638 FRAME_MOUSE_UPDATE (f);
639 }
640
641
642 /* Clear under internal border if any (GTK has its own version). */
643 #ifndef USE_GTK
644 void
645 x_clear_under_internal_border (struct frame *f)
646 {
647 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0)
648 {
649 Display *display = FRAME_X_DISPLAY (f);
650 Window window = FRAME_X_WINDOW (f);
651 int border = FRAME_INTERNAL_BORDER_WIDTH (f);
652 int width = FRAME_PIXEL_WIDTH (f);
653 int height = FRAME_PIXEL_HEIGHT (f);
654 int margin = FRAME_TOP_MARGIN_HEIGHT (f);
655
656 block_input ();
657 x_clear_area (display, window, 0, 0, border, height);
658 x_clear_area (display, window, 0, margin, width, border);
659 x_clear_area (display, window, width - border, 0, border, height);
660 x_clear_area (display, window, 0, height - border, width, border);
661 unblock_input ();
662 }
663 }
664 #endif
665
666 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
667 arrow bitmaps, or clear the fringes if no bitmaps are required
668 before DESIRED_ROW is made current. This function is called from
669 update_window_line only if it is known that there are differences
670 between bitmaps to be drawn between current row and DESIRED_ROW. */
671
672 static void
673 x_after_update_window_line (struct window *w, struct glyph_row *desired_row)
674 {
675 eassert (w);
676
677 if (!desired_row->mode_line_p && !w->pseudo_window_p)
678 desired_row->redraw_fringe_bitmaps_p = 1;
679
680 #ifdef USE_X_TOOLKIT
681 /* When a window has disappeared, make sure that no rest of
682 full-width rows stays visible in the internal border. Could
683 check here if updated window is the leftmost/rightmost window,
684 but I guess it's not worth doing since vertically split windows
685 are almost never used, internal border is rarely set, and the
686 overhead is very small. */
687 {
688 struct frame *f;
689 int width, height;
690
691 if (windows_or_buffers_changed
692 && desired_row->full_width_p
693 && (f = XFRAME (w->frame),
694 width = FRAME_INTERNAL_BORDER_WIDTH (f),
695 width != 0)
696 && (height = desired_row->visible_height,
697 height > 0))
698 {
699 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
700
701 block_input ();
702 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
703 0, y, width, height);
704 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
705 FRAME_PIXEL_WIDTH (f) - width,
706 y, width, height);
707 unblock_input ();
708 }
709 }
710 #endif
711 }
712
713 static void
714 x_draw_fringe_bitmap (struct window *w, struct glyph_row *row, struct draw_fringe_bitmap_params *p)
715 {
716 struct frame *f = XFRAME (WINDOW_FRAME (w));
717 Display *display = FRAME_X_DISPLAY (f);
718 Window window = FRAME_X_WINDOW (f);
719 GC gc = f->output_data.x->normal_gc;
720 struct face *face = p->face;
721
722 /* Must clip because of partially visible lines. */
723 x_clip_to_row (w, row, ANY_AREA, gc);
724
725 if (p->bx >= 0 && !p->overlay_p)
726 {
727 /* In case the same realized face is used for fringes and
728 for something displayed in the text (e.g. face `region' on
729 mono-displays, the fill style may have been changed to
730 FillSolid in x_draw_glyph_string_background. */
731 if (face->stipple)
732 XSetFillStyle (display, face->gc, FillOpaqueStippled);
733 else
734 XSetForeground (display, face->gc, face->background);
735
736 XFillRectangle (display, window, face->gc,
737 p->bx, p->by, p->nx, p->ny);
738
739 if (!face->stipple)
740 XSetForeground (display, face->gc, face->foreground);
741 }
742
743 if (p->which)
744 {
745 char *bits;
746 Pixmap pixmap, clipmask = (Pixmap) 0;
747 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
748 XGCValues gcv;
749
750 if (p->wd > 8)
751 bits = (char *) (p->bits + p->dh);
752 else
753 bits = (char *) p->bits + p->dh;
754
755 /* Draw the bitmap. I believe these small pixmaps can be cached
756 by the server. */
757 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
758 (p->cursor_p
759 ? (p->overlay_p ? face->background
760 : f->output_data.x->cursor_pixel)
761 : face->foreground),
762 face->background, depth);
763
764 if (p->overlay_p)
765 {
766 clipmask = XCreatePixmapFromBitmapData (display,
767 FRAME_DISPLAY_INFO (f)->root_window,
768 bits, p->wd, p->h,
769 1, 0, 1);
770 gcv.clip_mask = clipmask;
771 gcv.clip_x_origin = p->x;
772 gcv.clip_y_origin = p->y;
773 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
774 }
775
776 XCopyArea (display, pixmap, window, gc, 0, 0,
777 p->wd, p->h, p->x, p->y);
778 XFreePixmap (display, pixmap);
779
780 if (p->overlay_p)
781 {
782 gcv.clip_mask = (Pixmap) 0;
783 XChangeGC (display, gc, GCClipMask, &gcv);
784 XFreePixmap (display, clipmask);
785 }
786 }
787
788 XSetClipMask (display, gc, None);
789 }
790
791 /***********************************************************************
792 Glyph display
793 ***********************************************************************/
794
795
796
797 static void x_set_glyph_string_clipping (struct glyph_string *);
798 static void x_set_glyph_string_gc (struct glyph_string *);
799 static void x_draw_glyph_string_foreground (struct glyph_string *);
800 static void x_draw_composite_glyph_string_foreground (struct glyph_string *);
801 static void x_draw_glyph_string_box (struct glyph_string *);
802 static void x_draw_glyph_string (struct glyph_string *);
803 static _Noreturn void x_delete_glyphs (struct frame *, int);
804 static void x_compute_glyph_string_overhangs (struct glyph_string *);
805 static void x_set_cursor_gc (struct glyph_string *);
806 static void x_set_mode_line_face_gc (struct glyph_string *);
807 static void x_set_mouse_face_gc (struct glyph_string *);
808 static bool x_alloc_lighter_color (struct frame *, Display *, Colormap,
809 unsigned long *, double, int);
810 static void x_setup_relief_color (struct frame *, struct relief *,
811 double, int, unsigned long);
812 static void x_setup_relief_colors (struct glyph_string *);
813 static void x_draw_image_glyph_string (struct glyph_string *);
814 static void x_draw_image_relief (struct glyph_string *);
815 static void x_draw_image_foreground (struct glyph_string *);
816 static void x_draw_image_foreground_1 (struct glyph_string *, Pixmap);
817 static void x_clear_glyph_string_rect (struct glyph_string *, int,
818 int, int, int);
819 static void x_draw_relief_rect (struct frame *, int, int, int, int,
820 int, int, int, int, int, int,
821 XRectangle *);
822 static void x_draw_box_rect (struct glyph_string *, int, int, int, int,
823 int, int, int, XRectangle *);
824 static void x_scroll_bar_clear (struct frame *);
825
826 #ifdef GLYPH_DEBUG
827 static void x_check_font (struct frame *, struct font *);
828 #endif
829
830
831 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
832 face. */
833
834 static void
835 x_set_cursor_gc (struct glyph_string *s)
836 {
837 if (s->font == FRAME_FONT (s->f)
838 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
839 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
840 && !s->cmp)
841 s->gc = s->f->output_data.x->cursor_gc;
842 else
843 {
844 /* Cursor on non-default face: must merge. */
845 XGCValues xgcv;
846 unsigned long mask;
847
848 xgcv.background = s->f->output_data.x->cursor_pixel;
849 xgcv.foreground = s->face->background;
850
851 /* If the glyph would be invisible, try a different foreground. */
852 if (xgcv.foreground == xgcv.background)
853 xgcv.foreground = s->face->foreground;
854 if (xgcv.foreground == xgcv.background)
855 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
856 if (xgcv.foreground == xgcv.background)
857 xgcv.foreground = s->face->foreground;
858
859 /* Make sure the cursor is distinct from text in this face. */
860 if (xgcv.background == s->face->background
861 && xgcv.foreground == s->face->foreground)
862 {
863 xgcv.background = s->face->foreground;
864 xgcv.foreground = s->face->background;
865 }
866
867 IF_DEBUG (x_check_font (s->f, s->font));
868 xgcv.graphics_exposures = False;
869 mask = GCForeground | GCBackground | GCGraphicsExposures;
870
871 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
872 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
873 mask, &xgcv);
874 else
875 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
876 = XCreateGC (s->display, s->window, mask, &xgcv);
877
878 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
879 }
880 }
881
882
883 /* Set up S->gc of glyph string S for drawing text in mouse face. */
884
885 static void
886 x_set_mouse_face_gc (struct glyph_string *s)
887 {
888 int face_id;
889 struct face *face;
890
891 /* What face has to be used last for the mouse face? */
892 face_id = MOUSE_HL_INFO (s->f)->mouse_face_face_id;
893 face = FACE_FROM_ID (s->f, face_id);
894 if (face == NULL)
895 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
896
897 if (s->first_glyph->type == CHAR_GLYPH)
898 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
899 else
900 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
901 s->face = FACE_FROM_ID (s->f, face_id);
902 prepare_face_for_display (s->f, s->face);
903
904 if (s->font == s->face->font)
905 s->gc = s->face->gc;
906 else
907 {
908 /* Otherwise construct scratch_cursor_gc with values from FACE
909 except for FONT. */
910 XGCValues xgcv;
911 unsigned long mask;
912
913 xgcv.background = s->face->background;
914 xgcv.foreground = s->face->foreground;
915 xgcv.graphics_exposures = False;
916 mask = GCForeground | GCBackground | GCGraphicsExposures;
917
918 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
919 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
920 mask, &xgcv);
921 else
922 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
923 = XCreateGC (s->display, s->window, mask, &xgcv);
924
925 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
926
927 }
928 eassert (s->gc != 0);
929 }
930
931
932 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
933 Faces to use in the mode line have already been computed when the
934 matrix was built, so there isn't much to do, here. */
935
936 static void
937 x_set_mode_line_face_gc (struct glyph_string *s)
938 {
939 s->gc = s->face->gc;
940 }
941
942
943 /* Set S->gc of glyph string S for drawing that glyph string. Set
944 S->stippled_p to a non-zero value if the face of S has a stipple
945 pattern. */
946
947 static void
948 x_set_glyph_string_gc (struct glyph_string *s)
949 {
950 prepare_face_for_display (s->f, s->face);
951
952 if (s->hl == DRAW_NORMAL_TEXT)
953 {
954 s->gc = s->face->gc;
955 s->stippled_p = s->face->stipple != 0;
956 }
957 else if (s->hl == DRAW_INVERSE_VIDEO)
958 {
959 x_set_mode_line_face_gc (s);
960 s->stippled_p = s->face->stipple != 0;
961 }
962 else if (s->hl == DRAW_CURSOR)
963 {
964 x_set_cursor_gc (s);
965 s->stippled_p = 0;
966 }
967 else if (s->hl == DRAW_MOUSE_FACE)
968 {
969 x_set_mouse_face_gc (s);
970 s->stippled_p = s->face->stipple != 0;
971 }
972 else if (s->hl == DRAW_IMAGE_RAISED
973 || s->hl == DRAW_IMAGE_SUNKEN)
974 {
975 s->gc = s->face->gc;
976 s->stippled_p = s->face->stipple != 0;
977 }
978 else
979 emacs_abort ();
980
981 /* GC must have been set. */
982 eassert (s->gc != 0);
983 }
984
985
986 /* Set clipping for output of glyph string S. S may be part of a mode
987 line or menu if we don't have X toolkit support. */
988
989 static void
990 x_set_glyph_string_clipping (struct glyph_string *s)
991 {
992 XRectangle *r = s->clip;
993 int n = get_glyph_string_clip_rects (s, r, 2);
994
995 if (n > 0)
996 XSetClipRectangles (s->display, s->gc, 0, 0, r, n, Unsorted);
997 s->num_clips = n;
998 }
999
1000
1001 /* Set SRC's clipping for output of glyph string DST. This is called
1002 when we are drawing DST's left_overhang or right_overhang only in
1003 the area of SRC. */
1004
1005 static void
1006 x_set_glyph_string_clipping_exactly (struct glyph_string *src, struct glyph_string *dst)
1007 {
1008 XRectangle r;
1009
1010 r.x = src->x;
1011 r.width = src->width;
1012 r.y = src->y;
1013 r.height = src->height;
1014 dst->clip[0] = r;
1015 dst->num_clips = 1;
1016 XSetClipRectangles (dst->display, dst->gc, 0, 0, &r, 1, Unsorted);
1017 }
1018
1019
1020 /* RIF:
1021 Compute left and right overhang of glyph string S. */
1022
1023 static void
1024 x_compute_glyph_string_overhangs (struct glyph_string *s)
1025 {
1026 if (s->cmp == NULL
1027 && (s->first_glyph->type == CHAR_GLYPH
1028 || s->first_glyph->type == COMPOSITE_GLYPH))
1029 {
1030 struct font_metrics metrics;
1031
1032 if (s->first_glyph->type == CHAR_GLYPH)
1033 {
1034 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1035 struct font *font = s->font;
1036 int i;
1037
1038 for (i = 0; i < s->nchars; i++)
1039 code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
1040 font->driver->text_extents (font, code, s->nchars, &metrics);
1041 }
1042 else
1043 {
1044 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1045
1046 composition_gstring_width (gstring, s->cmp_from, s->cmp_to, &metrics);
1047 }
1048 s->right_overhang = (metrics.rbearing > metrics.width
1049 ? metrics.rbearing - metrics.width : 0);
1050 s->left_overhang = metrics.lbearing < 0 ? - metrics.lbearing : 0;
1051 }
1052 else if (s->cmp)
1053 {
1054 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1055 s->left_overhang = - s->cmp->lbearing;
1056 }
1057 }
1058
1059
1060 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1061
1062 static void
1063 x_clear_glyph_string_rect (struct glyph_string *s, int x, int y, int w, int h)
1064 {
1065 XGCValues xgcv;
1066 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1067 XSetForeground (s->display, s->gc, xgcv.background);
1068 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1069 XSetForeground (s->display, s->gc, xgcv.foreground);
1070 }
1071
1072
1073 /* Draw the background of glyph_string S. If S->background_filled_p
1074 is non-zero don't draw it. FORCE_P non-zero means draw the
1075 background even if it wouldn't be drawn normally. This is used
1076 when a string preceding S draws into the background of S, or S
1077 contains the first component of a composition. */
1078
1079 static void
1080 x_draw_glyph_string_background (struct glyph_string *s, bool force_p)
1081 {
1082 /* Nothing to do if background has already been drawn or if it
1083 shouldn't be drawn in the first place. */
1084 if (!s->background_filled_p)
1085 {
1086 int box_line_width = max (s->face->box_line_width, 0);
1087
1088 if (s->stippled_p)
1089 {
1090 /* Fill background with a stipple pattern. */
1091 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1092 XFillRectangle (s->display, s->window, s->gc, s->x,
1093 s->y + box_line_width,
1094 s->background_width,
1095 s->height - 2 * box_line_width);
1096 XSetFillStyle (s->display, s->gc, FillSolid);
1097 s->background_filled_p = 1;
1098 }
1099 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1100 || s->font_not_found_p
1101 || s->extends_to_end_of_line_p
1102 || force_p)
1103 {
1104 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1105 s->background_width,
1106 s->height - 2 * box_line_width);
1107 s->background_filled_p = 1;
1108 }
1109 }
1110 }
1111
1112
1113 /* Draw the foreground of glyph string S. */
1114
1115 static void
1116 x_draw_glyph_string_foreground (struct glyph_string *s)
1117 {
1118 int i, x;
1119
1120 /* If first glyph of S has a left box line, start drawing the text
1121 of S to the right of that box line. */
1122 if (s->face->box != FACE_NO_BOX
1123 && s->first_glyph->left_box_line_p)
1124 x = s->x + eabs (s->face->box_line_width);
1125 else
1126 x = s->x;
1127
1128 /* Draw characters of S as rectangles if S's font could not be
1129 loaded. */
1130 if (s->font_not_found_p)
1131 {
1132 for (i = 0; i < s->nchars; ++i)
1133 {
1134 struct glyph *g = s->first_glyph + i;
1135 XDrawRectangle (s->display, s->window,
1136 s->gc, x, s->y, g->pixel_width - 1,
1137 s->height - 1);
1138 x += g->pixel_width;
1139 }
1140 }
1141 else
1142 {
1143 struct font *font = s->font;
1144 int boff = font->baseline_offset;
1145 int y;
1146
1147 if (font->vertical_centering)
1148 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1149
1150 y = s->ybase - boff;
1151 if (s->for_overlaps
1152 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1153 font->driver->draw (s, 0, s->nchars, x, y, 0);
1154 else
1155 font->driver->draw (s, 0, s->nchars, x, y, 1);
1156 if (s->face->overstrike)
1157 font->driver->draw (s, 0, s->nchars, x + 1, y, 0);
1158 }
1159 }
1160
1161 /* Draw the foreground of composite glyph string S. */
1162
1163 static void
1164 x_draw_composite_glyph_string_foreground (struct glyph_string *s)
1165 {
1166 int i, j, x;
1167 struct font *font = s->font;
1168
1169 /* If first glyph of S has a left box line, start drawing the text
1170 of S to the right of that box line. */
1171 if (s->face && s->face->box != FACE_NO_BOX
1172 && s->first_glyph->left_box_line_p)
1173 x = s->x + eabs (s->face->box_line_width);
1174 else
1175 x = s->x;
1176
1177 /* S is a glyph string for a composition. S->cmp_from is the index
1178 of the first character drawn for glyphs of this composition.
1179 S->cmp_from == 0 means we are drawing the very first character of
1180 this composition. */
1181
1182 /* Draw a rectangle for the composition if the font for the very
1183 first character of the composition could not be loaded. */
1184 if (s->font_not_found_p)
1185 {
1186 if (s->cmp_from == 0)
1187 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1188 s->width - 1, s->height - 1);
1189 }
1190 else if (! s->first_glyph->u.cmp.automatic)
1191 {
1192 int y = s->ybase;
1193
1194 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1195 /* TAB in a composition means display glyphs with padding
1196 space on the left or right. */
1197 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1198 {
1199 int xx = x + s->cmp->offsets[j * 2];
1200 int yy = y - s->cmp->offsets[j * 2 + 1];
1201
1202 font->driver->draw (s, j, j + 1, xx, yy, 0);
1203 if (s->face->overstrike)
1204 font->driver->draw (s, j, j + 1, xx + 1, yy, 0);
1205 }
1206 }
1207 else
1208 {
1209 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1210 Lisp_Object glyph;
1211 int y = s->ybase;
1212 int width = 0;
1213
1214 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1215 {
1216 glyph = LGSTRING_GLYPH (gstring, i);
1217 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1218 width += LGLYPH_WIDTH (glyph);
1219 else
1220 {
1221 int xoff, yoff, wadjust;
1222
1223 if (j < i)
1224 {
1225 font->driver->draw (s, j, i, x, y, 0);
1226 if (s->face->overstrike)
1227 font->driver->draw (s, j, i, x + 1, y, 0);
1228 x += width;
1229 }
1230 xoff = LGLYPH_XOFF (glyph);
1231 yoff = LGLYPH_YOFF (glyph);
1232 wadjust = LGLYPH_WADJUST (glyph);
1233 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, 0);
1234 if (s->face->overstrike)
1235 font->driver->draw (s, i, i + 1, x + xoff + 1, y + yoff, 0);
1236 x += wadjust;
1237 j = i + 1;
1238 width = 0;
1239 }
1240 }
1241 if (j < i)
1242 {
1243 font->driver->draw (s, j, i, x, y, 0);
1244 if (s->face->overstrike)
1245 font->driver->draw (s, j, i, x + 1, y, 0);
1246 }
1247 }
1248 }
1249
1250
1251 /* Draw the foreground of glyph string S for glyphless characters. */
1252
1253 static void
1254 x_draw_glyphless_glyph_string_foreground (struct glyph_string *s)
1255 {
1256 struct glyph *glyph = s->first_glyph;
1257 XChar2b char2b[8];
1258 int x, i, j;
1259
1260 /* If first glyph of S has a left box line, start drawing the text
1261 of S to the right of that box line. */
1262 if (s->face && s->face->box != FACE_NO_BOX
1263 && s->first_glyph->left_box_line_p)
1264 x = s->x + eabs (s->face->box_line_width);
1265 else
1266 x = s->x;
1267
1268 s->char2b = char2b;
1269
1270 for (i = 0; i < s->nchars; i++, glyph++)
1271 {
1272 char buf[7], *str = NULL;
1273 int len = glyph->u.glyphless.len;
1274
1275 if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM)
1276 {
1277 if (len > 0
1278 && CHAR_TABLE_P (Vglyphless_char_display)
1279 && (CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display))
1280 >= 1))
1281 {
1282 Lisp_Object acronym
1283 = (! glyph->u.glyphless.for_no_font
1284 ? CHAR_TABLE_REF (Vglyphless_char_display,
1285 glyph->u.glyphless.ch)
1286 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
1287 if (STRINGP (acronym))
1288 str = SSDATA (acronym);
1289 }
1290 }
1291 else if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE)
1292 {
1293 sprintf (buf, "%0*X",
1294 glyph->u.glyphless.ch < 0x10000 ? 4 : 6,
1295 glyph->u.glyphless.ch);
1296 str = buf;
1297 }
1298
1299 if (str)
1300 {
1301 int upper_len = (len + 1) / 2;
1302 unsigned code;
1303
1304 /* It is assured that all LEN characters in STR is ASCII. */
1305 for (j = 0; j < len; j++)
1306 {
1307 code = s->font->driver->encode_char (s->font, str[j]);
1308 STORE_XCHAR2B (char2b + j, code >> 8, code & 0xFF);
1309 }
1310 s->font->driver->draw (s, 0, upper_len,
1311 x + glyph->slice.glyphless.upper_xoff,
1312 s->ybase + glyph->slice.glyphless.upper_yoff,
1313 0);
1314 s->font->driver->draw (s, upper_len, len,
1315 x + glyph->slice.glyphless.lower_xoff,
1316 s->ybase + glyph->slice.glyphless.lower_yoff,
1317 0);
1318 }
1319 if (glyph->u.glyphless.method != GLYPHLESS_DISPLAY_THIN_SPACE)
1320 XDrawRectangle (s->display, s->window, s->gc,
1321 x, s->ybase - glyph->ascent,
1322 glyph->pixel_width - 1,
1323 glyph->ascent + glyph->descent - 1);
1324 x += glyph->pixel_width;
1325 }
1326 }
1327
1328 #ifdef USE_X_TOOLKIT
1329
1330 #ifdef USE_LUCID
1331
1332 /* Return the frame on which widget WIDGET is used.. Abort if frame
1333 cannot be determined. */
1334
1335 static struct frame *
1336 x_frame_of_widget (Widget widget)
1337 {
1338 struct x_display_info *dpyinfo;
1339 Lisp_Object tail, frame;
1340 struct frame *f;
1341
1342 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1343
1344 /* Find the top-level shell of the widget. Note that this function
1345 can be called when the widget is not yet realized, so XtWindow
1346 (widget) == 0. That's the reason we can't simply use
1347 x_any_window_to_frame. */
1348 while (!XtIsTopLevelShell (widget))
1349 widget = XtParent (widget);
1350
1351 /* Look for a frame with that top-level widget. Allocate the color
1352 on that frame to get the right gamma correction value. */
1353 FOR_EACH_FRAME (tail, frame)
1354 {
1355 f = XFRAME (frame);
1356 if (FRAME_X_P (f)
1357 && f->output_data.nothing != 1
1358 && FRAME_DISPLAY_INFO (f) == dpyinfo
1359 && f->output_data.x->widget == widget)
1360 return f;
1361 }
1362 emacs_abort ();
1363 }
1364
1365 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1366 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1367 If this produces the same color as PIXEL, try a color where all RGB
1368 values have DELTA added. Return the allocated color in *PIXEL.
1369 DISPLAY is the X display, CMAP is the colormap to operate on.
1370 Value is true if successful. */
1371
1372 bool
1373 x_alloc_lighter_color_for_widget (Widget widget, Display *display, Colormap cmap,
1374 unsigned long *pixel, double factor, int delta)
1375 {
1376 struct frame *f = x_frame_of_widget (widget);
1377 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1378 }
1379
1380 #endif /* USE_LUCID */
1381
1382
1383 /* Structure specifying which arguments should be passed by Xt to
1384 cvt_string_to_pixel. We want the widget's screen and colormap. */
1385
1386 static XtConvertArgRec cvt_string_to_pixel_args[] =
1387 {
1388 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1389 sizeof (Screen *)},
1390 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1391 sizeof (Colormap)}
1392 };
1393
1394
1395 /* The address of this variable is returned by
1396 cvt_string_to_pixel. */
1397
1398 static Pixel cvt_string_to_pixel_value;
1399
1400
1401 /* Convert a color name to a pixel color.
1402
1403 DPY is the display we are working on.
1404
1405 ARGS is an array of *NARGS XrmValue structures holding additional
1406 information about the widget for which the conversion takes place.
1407 The contents of this array are determined by the specification
1408 in cvt_string_to_pixel_args.
1409
1410 FROM is a pointer to an XrmValue which points to the color name to
1411 convert. TO is an XrmValue in which to return the pixel color.
1412
1413 CLOSURE_RET is a pointer to user-data, in which we record if
1414 we allocated the color or not.
1415
1416 Value is True if successful, False otherwise. */
1417
1418 static Boolean
1419 cvt_string_to_pixel (Display *dpy, XrmValue *args, Cardinal *nargs,
1420 XrmValue *from, XrmValue *to,
1421 XtPointer *closure_ret)
1422 {
1423 Screen *screen;
1424 Colormap cmap;
1425 Pixel pixel;
1426 String color_name;
1427 XColor color;
1428
1429 if (*nargs != 2)
1430 {
1431 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1432 "wrongParameters", "cvt_string_to_pixel",
1433 "XtToolkitError",
1434 "Screen and colormap args required", NULL, NULL);
1435 return False;
1436 }
1437
1438 screen = *(Screen **) args[0].addr;
1439 cmap = *(Colormap *) args[1].addr;
1440 color_name = (String) from->addr;
1441
1442 if (strcmp (color_name, XtDefaultBackground) == 0)
1443 {
1444 *closure_ret = (XtPointer) False;
1445 pixel = WhitePixelOfScreen (screen);
1446 }
1447 else if (strcmp (color_name, XtDefaultForeground) == 0)
1448 {
1449 *closure_ret = (XtPointer) False;
1450 pixel = BlackPixelOfScreen (screen);
1451 }
1452 else if (XParseColor (dpy, cmap, color_name, &color)
1453 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1454 {
1455 pixel = color.pixel;
1456 *closure_ret = (XtPointer) True;
1457 }
1458 else
1459 {
1460 String params[1];
1461 Cardinal nparams = 1;
1462
1463 params[0] = color_name;
1464 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1465 "badValue", "cvt_string_to_pixel",
1466 "XtToolkitError", "Invalid color `%s'",
1467 params, &nparams);
1468 return False;
1469 }
1470
1471 if (to->addr != NULL)
1472 {
1473 if (to->size < sizeof (Pixel))
1474 {
1475 to->size = sizeof (Pixel);
1476 return False;
1477 }
1478
1479 *(Pixel *) to->addr = pixel;
1480 }
1481 else
1482 {
1483 cvt_string_to_pixel_value = pixel;
1484 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1485 }
1486
1487 to->size = sizeof (Pixel);
1488 return True;
1489 }
1490
1491
1492 /* Free a pixel color which was previously allocated via
1493 cvt_string_to_pixel. This is registered as the destructor
1494 for this type of resource via XtSetTypeConverter.
1495
1496 APP is the application context in which we work.
1497
1498 TO is a pointer to an XrmValue holding the color to free.
1499 CLOSURE is the value we stored in CLOSURE_RET for this color
1500 in cvt_string_to_pixel.
1501
1502 ARGS and NARGS are like for cvt_string_to_pixel. */
1503
1504 static void
1505 cvt_pixel_dtor (XtAppContext app, XrmValuePtr to, XtPointer closure, XrmValuePtr args,
1506 Cardinal *nargs)
1507 {
1508 if (*nargs != 2)
1509 {
1510 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1511 "XtToolkitError",
1512 "Screen and colormap arguments required",
1513 NULL, NULL);
1514 }
1515 else if (closure != NULL)
1516 {
1517 /* We did allocate the pixel, so free it. */
1518 Screen *screen = *(Screen **) args[0].addr;
1519 Colormap cmap = *(Colormap *) args[1].addr;
1520 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1521 (Pixel *) to->addr, 1);
1522 }
1523 }
1524
1525
1526 #endif /* USE_X_TOOLKIT */
1527
1528
1529 /* Value is an array of XColor structures for the contents of the
1530 color map of display DPY. Set *NCELLS to the size of the array.
1531 Note that this probably shouldn't be called for large color maps,
1532 say a 24-bit TrueColor map. */
1533
1534 static const XColor *
1535 x_color_cells (Display *dpy, int *ncells)
1536 {
1537 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1538
1539 if (dpyinfo->color_cells == NULL)
1540 {
1541 Screen *screen = dpyinfo->screen;
1542 int ncolor_cells = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1543 int i;
1544
1545 dpyinfo->color_cells = xnmalloc (ncolor_cells,
1546 sizeof *dpyinfo->color_cells);
1547 dpyinfo->ncolor_cells = ncolor_cells;
1548
1549 for (i = 0; i < ncolor_cells; ++i)
1550 dpyinfo->color_cells[i].pixel = i;
1551
1552 XQueryColors (dpy, dpyinfo->cmap,
1553 dpyinfo->color_cells, ncolor_cells);
1554 }
1555
1556 *ncells = dpyinfo->ncolor_cells;
1557 return dpyinfo->color_cells;
1558 }
1559
1560
1561 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1562 colors in COLORS. Use cached information, if available. */
1563
1564 void
1565 x_query_colors (struct frame *f, XColor *colors, int ncolors)
1566 {
1567 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1568
1569 if (dpyinfo->color_cells)
1570 {
1571 int i;
1572 for (i = 0; i < ncolors; ++i)
1573 {
1574 unsigned long pixel = colors[i].pixel;
1575 eassert (pixel < dpyinfo->ncolor_cells);
1576 eassert (dpyinfo->color_cells[pixel].pixel == pixel);
1577 colors[i] = dpyinfo->color_cells[pixel];
1578 }
1579 }
1580 else
1581 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1582 }
1583
1584
1585 /* On frame F, translate pixel color to RGB values for the color in
1586 COLOR. Use cached information, if available. */
1587
1588 void
1589 x_query_color (struct frame *f, XColor *color)
1590 {
1591 x_query_colors (f, color, 1);
1592 }
1593
1594
1595 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1596 exact match can't be allocated, try the nearest color available.
1597 Value is true if successful. Set *COLOR to the color
1598 allocated. */
1599
1600 static bool
1601 x_alloc_nearest_color_1 (Display *dpy, Colormap cmap, XColor *color)
1602 {
1603 bool rc;
1604
1605 rc = XAllocColor (dpy, cmap, color) != 0;
1606 if (rc == 0)
1607 {
1608 /* If we got to this point, the colormap is full, so we're going
1609 to try to get the next closest color. The algorithm used is
1610 a least-squares matching, which is what X uses for closest
1611 color matching with StaticColor visuals. */
1612 int nearest, i;
1613 int max_color_delta = 255;
1614 int max_delta = 3 * max_color_delta;
1615 int nearest_delta = max_delta + 1;
1616 int ncells;
1617 const XColor *cells = x_color_cells (dpy, &ncells);
1618
1619 for (nearest = i = 0; i < ncells; ++i)
1620 {
1621 int dred = (color->red >> 8) - (cells[i].red >> 8);
1622 int dgreen = (color->green >> 8) - (cells[i].green >> 8);
1623 int dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1624 int delta = dred * dred + dgreen * dgreen + dblue * dblue;
1625
1626 if (delta < nearest_delta)
1627 {
1628 nearest = i;
1629 nearest_delta = delta;
1630 }
1631 }
1632
1633 color->red = cells[nearest].red;
1634 color->green = cells[nearest].green;
1635 color->blue = cells[nearest].blue;
1636 rc = XAllocColor (dpy, cmap, color) != 0;
1637 }
1638 else
1639 {
1640 /* If allocation succeeded, and the allocated pixel color is not
1641 equal to a cached pixel color recorded earlier, there was a
1642 change in the colormap, so clear the color cache. */
1643 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1644 XColor *cached_color;
1645
1646 if (dpyinfo->color_cells
1647 && (cached_color = &dpyinfo->color_cells[color->pixel],
1648 (cached_color->red != color->red
1649 || cached_color->blue != color->blue
1650 || cached_color->green != color->green)))
1651 {
1652 xfree (dpyinfo->color_cells);
1653 dpyinfo->color_cells = NULL;
1654 dpyinfo->ncolor_cells = 0;
1655 }
1656 }
1657
1658 #ifdef DEBUG_X_COLORS
1659 if (rc)
1660 register_color (color->pixel);
1661 #endif /* DEBUG_X_COLORS */
1662
1663 return rc;
1664 }
1665
1666
1667 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1668 exact match can't be allocated, try the nearest color available.
1669 Value is true if successful. Set *COLOR to the color
1670 allocated. */
1671
1672 bool
1673 x_alloc_nearest_color (struct frame *f, Colormap cmap, XColor *color)
1674 {
1675 gamma_correct (f, color);
1676 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1677 }
1678
1679
1680 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1681 It's necessary to do this instead of just using PIXEL directly to
1682 get color reference counts right. */
1683
1684 unsigned long
1685 x_copy_color (struct frame *f, unsigned long pixel)
1686 {
1687 XColor color;
1688
1689 color.pixel = pixel;
1690 block_input ();
1691 x_query_color (f, &color);
1692 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1693 unblock_input ();
1694 #ifdef DEBUG_X_COLORS
1695 register_color (pixel);
1696 #endif
1697 return color.pixel;
1698 }
1699
1700
1701 /* Brightness beyond which a color won't have its highlight brightness
1702 boosted.
1703
1704 Nominally, highlight colors for `3d' faces are calculated by
1705 brightening an object's color by a constant scale factor, but this
1706 doesn't yield good results for dark colors, so for colors who's
1707 brightness is less than this value (on a scale of 0-65535) have an
1708 use an additional additive factor.
1709
1710 The value here is set so that the default menu-bar/mode-line color
1711 (grey75) will not have its highlights changed at all. */
1712 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1713
1714
1715 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1716 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1717 If this produces the same color as PIXEL, try a color where all RGB
1718 values have DELTA added. Return the allocated color in *PIXEL.
1719 DISPLAY is the X display, CMAP is the colormap to operate on.
1720 Value is non-zero if successful. */
1721
1722 static bool
1723 x_alloc_lighter_color (struct frame *f, Display *display, Colormap cmap,
1724 unsigned long *pixel, double factor, int delta)
1725 {
1726 XColor color, new;
1727 long bright;
1728 bool success_p;
1729
1730 /* Get RGB color values. */
1731 color.pixel = *pixel;
1732 x_query_color (f, &color);
1733
1734 /* Change RGB values by specified FACTOR. Avoid overflow! */
1735 eassert (factor >= 0);
1736 new.red = min (0xffff, factor * color.red);
1737 new.green = min (0xffff, factor * color.green);
1738 new.blue = min (0xffff, factor * color.blue);
1739
1740 /* Calculate brightness of COLOR. */
1741 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
1742
1743 /* We only boost colors that are darker than
1744 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1745 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1746 /* Make an additive adjustment to NEW, because it's dark enough so
1747 that scaling by FACTOR alone isn't enough. */
1748 {
1749 /* How far below the limit this color is (0 - 1, 1 being darker). */
1750 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1751 /* The additive adjustment. */
1752 int min_delta = delta * dimness * factor / 2;
1753
1754 if (factor < 1)
1755 {
1756 new.red = max (0, new.red - min_delta);
1757 new.green = max (0, new.green - min_delta);
1758 new.blue = max (0, new.blue - min_delta);
1759 }
1760 else
1761 {
1762 new.red = min (0xffff, min_delta + new.red);
1763 new.green = min (0xffff, min_delta + new.green);
1764 new.blue = min (0xffff, min_delta + new.blue);
1765 }
1766 }
1767
1768 /* Try to allocate the color. */
1769 success_p = x_alloc_nearest_color (f, cmap, &new);
1770 if (success_p)
1771 {
1772 if (new.pixel == *pixel)
1773 {
1774 /* If we end up with the same color as before, try adding
1775 delta to the RGB values. */
1776 x_free_colors (f, &new.pixel, 1);
1777
1778 new.red = min (0xffff, delta + color.red);
1779 new.green = min (0xffff, delta + color.green);
1780 new.blue = min (0xffff, delta + color.blue);
1781 success_p = x_alloc_nearest_color (f, cmap, &new);
1782 }
1783 else
1784 success_p = 1;
1785 *pixel = new.pixel;
1786 }
1787
1788 return success_p;
1789 }
1790
1791
1792 /* Set up the foreground color for drawing relief lines of glyph
1793 string S. RELIEF is a pointer to a struct relief containing the GC
1794 with which lines will be drawn. Use a color that is FACTOR or
1795 DELTA lighter or darker than the relief's background which is found
1796 in S->f->output_data.x->relief_background. If such a color cannot
1797 be allocated, use DEFAULT_PIXEL, instead. */
1798
1799 static void
1800 x_setup_relief_color (struct frame *f, struct relief *relief, double factor,
1801 int delta, unsigned long default_pixel)
1802 {
1803 XGCValues xgcv;
1804 struct x_output *di = f->output_data.x;
1805 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
1806 unsigned long pixel;
1807 unsigned long background = di->relief_background;
1808 Colormap cmap = FRAME_X_COLORMAP (f);
1809 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1810 Display *dpy = FRAME_X_DISPLAY (f);
1811
1812 xgcv.graphics_exposures = False;
1813 xgcv.line_width = 1;
1814
1815 /* Free previously allocated color. The color cell will be reused
1816 when it has been freed as many times as it was allocated, so this
1817 doesn't affect faces using the same colors. */
1818 if (relief->gc && relief->pixel != -1)
1819 {
1820 x_free_colors (f, &relief->pixel, 1);
1821 relief->pixel = -1;
1822 }
1823
1824 /* Allocate new color. */
1825 xgcv.foreground = default_pixel;
1826 pixel = background;
1827 if (dpyinfo->n_planes != 1
1828 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
1829 xgcv.foreground = relief->pixel = pixel;
1830
1831 if (relief->gc == 0)
1832 {
1833 xgcv.stipple = dpyinfo->gray;
1834 mask |= GCStipple;
1835 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
1836 }
1837 else
1838 XChangeGC (dpy, relief->gc, mask, &xgcv);
1839 }
1840
1841
1842 /* Set up colors for the relief lines around glyph string S. */
1843
1844 static void
1845 x_setup_relief_colors (struct glyph_string *s)
1846 {
1847 struct x_output *di = s->f->output_data.x;
1848 unsigned long color;
1849
1850 if (s->face->use_box_color_for_shadows_p)
1851 color = s->face->box_color;
1852 else if (s->first_glyph->type == IMAGE_GLYPH
1853 && s->img->pixmap
1854 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
1855 color = IMAGE_BACKGROUND (s->img, s->f, 0);
1856 else
1857 {
1858 XGCValues xgcv;
1859
1860 /* Get the background color of the face. */
1861 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
1862 color = xgcv.background;
1863 }
1864
1865 if (di->white_relief.gc == 0
1866 || color != di->relief_background)
1867 {
1868 di->relief_background = color;
1869 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
1870 WHITE_PIX_DEFAULT (s->f));
1871 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
1872 BLACK_PIX_DEFAULT (s->f));
1873 }
1874 }
1875
1876
1877 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
1878 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
1879 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
1880 relief. LEFT_P non-zero means draw a relief on the left side of
1881 the rectangle. RIGHT_P non-zero means draw a relief on the right
1882 side of the rectangle. CLIP_RECT is the clipping rectangle to use
1883 when drawing. */
1884
1885 static void
1886 x_draw_relief_rect (struct frame *f,
1887 int left_x, int top_y, int right_x, int bottom_y, int width,
1888 int raised_p, int top_p, int bot_p, int left_p, int right_p,
1889 XRectangle *clip_rect)
1890 {
1891 Display *dpy = FRAME_X_DISPLAY (f);
1892 Window window = FRAME_X_WINDOW (f);
1893 int i;
1894 GC gc;
1895
1896 if (raised_p)
1897 gc = f->output_data.x->white_relief.gc;
1898 else
1899 gc = f->output_data.x->black_relief.gc;
1900 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
1901
1902 /* This code is more complicated than it has to be, because of two
1903 minor hacks to make the boxes look nicer: (i) if width > 1, draw
1904 the outermost line using the black relief. (ii) Omit the four
1905 corner pixels. */
1906
1907 /* Top. */
1908 if (top_p)
1909 {
1910 if (width == 1)
1911 XDrawLine (dpy, window, gc,
1912 left_x + (left_p ? 1 : 0), top_y,
1913 right_x + (right_p ? 0 : 1), top_y);
1914
1915 for (i = 1; i < width; ++i)
1916 XDrawLine (dpy, window, gc,
1917 left_x + i * left_p, top_y + i,
1918 right_x + 1 - i * right_p, top_y + i);
1919 }
1920
1921 /* Left. */
1922 if (left_p)
1923 {
1924 if (width == 1)
1925 XDrawLine (dpy, window, gc, left_x, top_y + 1, left_x, bottom_y);
1926
1927 XClearArea (dpy, window, left_x, top_y, 1, 1, False);
1928 XClearArea (dpy, window, left_x, bottom_y, 1, 1, False);
1929
1930 for (i = (width > 1 ? 1 : 0); i < width; ++i)
1931 XDrawLine (dpy, window, gc,
1932 left_x + i, top_y + (i + 1) * top_p,
1933 left_x + i, bottom_y + 1 - (i + 1) * bot_p);
1934 }
1935
1936 XSetClipMask (dpy, gc, None);
1937 if (raised_p)
1938 gc = f->output_data.x->black_relief.gc;
1939 else
1940 gc = f->output_data.x->white_relief.gc;
1941 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
1942
1943 if (width > 1)
1944 {
1945 /* Outermost top line. */
1946 if (top_p)
1947 XDrawLine (dpy, window, gc,
1948 left_x + (left_p ? 1 : 0), top_y,
1949 right_x + (right_p ? 0 : 1), top_y);
1950
1951 /* Outermost left line. */
1952 if (left_p)
1953 XDrawLine (dpy, window, gc, left_x, top_y + 1, left_x, bottom_y);
1954 }
1955
1956 /* Bottom. */
1957 if (bot_p)
1958 {
1959 XDrawLine (dpy, window, gc,
1960 left_x + (left_p ? 1 : 0), bottom_y,
1961 right_x + (right_p ? 0 : 1), bottom_y);
1962 for (i = 1; i < width; ++i)
1963 XDrawLine (dpy, window, gc,
1964 left_x + i * left_p, bottom_y - i,
1965 right_x + 1 - i * right_p, bottom_y - i);
1966 }
1967
1968 /* Right. */
1969 if (right_p)
1970 {
1971 XClearArea (dpy, window, right_x, top_y, 1, 1, False);
1972 XClearArea (dpy, window, right_x, bottom_y, 1, 1, False);
1973 for (i = 0; i < width; ++i)
1974 XDrawLine (dpy, window, gc,
1975 right_x - i, top_y + (i + 1) * top_p,
1976 right_x - i, bottom_y + 1 - (i + 1) * bot_p);
1977 }
1978
1979 XSetClipMask (dpy, gc, None);
1980 }
1981
1982
1983 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
1984 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
1985 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
1986 left side of the rectangle. RIGHT_P non-zero means draw a line
1987 on the right side of the rectangle. CLIP_RECT is the clipping
1988 rectangle to use when drawing. */
1989
1990 static void
1991 x_draw_box_rect (struct glyph_string *s,
1992 int left_x, int top_y, int right_x, int bottom_y, int width,
1993 int left_p, int right_p, XRectangle *clip_rect)
1994 {
1995 XGCValues xgcv;
1996
1997 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
1998 XSetForeground (s->display, s->gc, s->face->box_color);
1999 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
2000
2001 /* Top. */
2002 XFillRectangle (s->display, s->window, s->gc,
2003 left_x, top_y, right_x - left_x + 1, width);
2004
2005 /* Left. */
2006 if (left_p)
2007 XFillRectangle (s->display, s->window, s->gc,
2008 left_x, top_y, width, bottom_y - top_y + 1);
2009
2010 /* Bottom. */
2011 XFillRectangle (s->display, s->window, s->gc,
2012 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2013
2014 /* Right. */
2015 if (right_p)
2016 XFillRectangle (s->display, s->window, s->gc,
2017 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2018
2019 XSetForeground (s->display, s->gc, xgcv.foreground);
2020 XSetClipMask (s->display, s->gc, None);
2021 }
2022
2023
2024 /* Draw a box around glyph string S. */
2025
2026 static void
2027 x_draw_glyph_string_box (struct glyph_string *s)
2028 {
2029 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2030 int left_p, right_p;
2031 struct glyph *last_glyph;
2032 XRectangle clip_rect;
2033
2034 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2035 ? WINDOW_RIGHT_EDGE_X (s->w)
2036 : window_box_right (s->w, s->area));
2037
2038 /* The glyph that may have a right box line. */
2039 last_glyph = (s->cmp || s->img
2040 ? s->first_glyph
2041 : s->first_glyph + s->nchars - 1);
2042
2043 width = eabs (s->face->box_line_width);
2044 raised_p = s->face->box == FACE_RAISED_BOX;
2045 left_x = s->x;
2046 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2047 ? last_x - 1
2048 : min (last_x, s->x + s->background_width) - 1);
2049 top_y = s->y;
2050 bottom_y = top_y + s->height - 1;
2051
2052 left_p = (s->first_glyph->left_box_line_p
2053 || (s->hl == DRAW_MOUSE_FACE
2054 && (s->prev == NULL
2055 || s->prev->hl != s->hl)));
2056 right_p = (last_glyph->right_box_line_p
2057 || (s->hl == DRAW_MOUSE_FACE
2058 && (s->next == NULL
2059 || s->next->hl != s->hl)));
2060
2061 get_glyph_string_clip_rect (s, &clip_rect);
2062
2063 if (s->face->box == FACE_SIMPLE_BOX)
2064 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2065 left_p, right_p, &clip_rect);
2066 else
2067 {
2068 x_setup_relief_colors (s);
2069 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2070 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
2071 }
2072 }
2073
2074
2075 /* Draw foreground of image glyph string S. */
2076
2077 static void
2078 x_draw_image_foreground (struct glyph_string *s)
2079 {
2080 int x = s->x;
2081 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2082
2083 /* If first glyph of S has a left box line, start drawing it to the
2084 right of that line. */
2085 if (s->face->box != FACE_NO_BOX
2086 && s->first_glyph->left_box_line_p
2087 && s->slice.x == 0)
2088 x += eabs (s->face->box_line_width);
2089
2090 /* If there is a margin around the image, adjust x- and y-position
2091 by that margin. */
2092 if (s->slice.x == 0)
2093 x += s->img->hmargin;
2094 if (s->slice.y == 0)
2095 y += s->img->vmargin;
2096
2097 if (s->img->pixmap)
2098 {
2099 if (s->img->mask)
2100 {
2101 /* We can't set both a clip mask and use XSetClipRectangles
2102 because the latter also sets a clip mask. We also can't
2103 trust on the shape extension to be available
2104 (XShapeCombineRegion). So, compute the rectangle to draw
2105 manually. */
2106 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2107 | GCFunction);
2108 XGCValues xgcv;
2109 XRectangle clip_rect, image_rect, r;
2110
2111 xgcv.clip_mask = s->img->mask;
2112 xgcv.clip_x_origin = x;
2113 xgcv.clip_y_origin = y;
2114 xgcv.function = GXcopy;
2115 XChangeGC (s->display, s->gc, mask, &xgcv);
2116
2117 get_glyph_string_clip_rect (s, &clip_rect);
2118 image_rect.x = x;
2119 image_rect.y = y;
2120 image_rect.width = s->slice.width;
2121 image_rect.height = s->slice.height;
2122 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2123 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2124 s->slice.x + r.x - x, s->slice.y + r.y - y,
2125 r.width, r.height, r.x, r.y);
2126 }
2127 else
2128 {
2129 XRectangle clip_rect, image_rect, r;
2130
2131 get_glyph_string_clip_rect (s, &clip_rect);
2132 image_rect.x = x;
2133 image_rect.y = y;
2134 image_rect.width = s->slice.width;
2135 image_rect.height = s->slice.height;
2136 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2137 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2138 s->slice.x + r.x - x, s->slice.y + r.y - y,
2139 r.width, r.height, r.x, r.y);
2140
2141 /* When the image has a mask, we can expect that at
2142 least part of a mouse highlight or a block cursor will
2143 be visible. If the image doesn't have a mask, make
2144 a block cursor visible by drawing a rectangle around
2145 the image. I believe it's looking better if we do
2146 nothing here for mouse-face. */
2147 if (s->hl == DRAW_CURSOR)
2148 {
2149 int relief = eabs (s->img->relief);
2150 XDrawRectangle (s->display, s->window, s->gc,
2151 x - relief, y - relief,
2152 s->slice.width + relief*2 - 1,
2153 s->slice.height + relief*2 - 1);
2154 }
2155 }
2156 }
2157 else
2158 /* Draw a rectangle if image could not be loaded. */
2159 XDrawRectangle (s->display, s->window, s->gc, x, y,
2160 s->slice.width - 1, s->slice.height - 1);
2161 }
2162
2163
2164 /* Draw a relief around the image glyph string S. */
2165
2166 static void
2167 x_draw_image_relief (struct glyph_string *s)
2168 {
2169 int x1, y1, thick, raised_p, top_p, bot_p, left_p, right_p;
2170 int extra_x, extra_y;
2171 XRectangle r;
2172 int x = s->x;
2173 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2174
2175 /* If first glyph of S has a left box line, start drawing it to the
2176 right of that line. */
2177 if (s->face->box != FACE_NO_BOX
2178 && s->first_glyph->left_box_line_p
2179 && s->slice.x == 0)
2180 x += eabs (s->face->box_line_width);
2181
2182 /* If there is a margin around the image, adjust x- and y-position
2183 by that margin. */
2184 if (s->slice.x == 0)
2185 x += s->img->hmargin;
2186 if (s->slice.y == 0)
2187 y += s->img->vmargin;
2188
2189 if (s->hl == DRAW_IMAGE_SUNKEN
2190 || s->hl == DRAW_IMAGE_RAISED)
2191 {
2192 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2193 raised_p = s->hl == DRAW_IMAGE_RAISED;
2194 }
2195 else
2196 {
2197 thick = eabs (s->img->relief);
2198 raised_p = s->img->relief > 0;
2199 }
2200
2201 x1 = x + s->slice.width - 1;
2202 y1 = y + s->slice.height - 1;
2203
2204 extra_x = extra_y = 0;
2205 if (s->face->id == TOOL_BAR_FACE_ID)
2206 {
2207 if (CONSP (Vtool_bar_button_margin)
2208 && INTEGERP (XCAR (Vtool_bar_button_margin))
2209 && INTEGERP (XCDR (Vtool_bar_button_margin)))
2210 {
2211 extra_x = XINT (XCAR (Vtool_bar_button_margin));
2212 extra_y = XINT (XCDR (Vtool_bar_button_margin));
2213 }
2214 else if (INTEGERP (Vtool_bar_button_margin))
2215 extra_x = extra_y = XINT (Vtool_bar_button_margin);
2216 }
2217
2218 top_p = bot_p = left_p = right_p = 0;
2219
2220 if (s->slice.x == 0)
2221 x -= thick + extra_x, left_p = 1;
2222 if (s->slice.y == 0)
2223 y -= thick + extra_y, top_p = 1;
2224 if (s->slice.x + s->slice.width == s->img->width)
2225 x1 += thick + extra_x, right_p = 1;
2226 if (s->slice.y + s->slice.height == s->img->height)
2227 y1 += thick + extra_y, bot_p = 1;
2228
2229 x_setup_relief_colors (s);
2230 get_glyph_string_clip_rect (s, &r);
2231 x_draw_relief_rect (s->f, x, y, x1, y1, thick, raised_p,
2232 top_p, bot_p, left_p, right_p, &r);
2233 }
2234
2235
2236 /* Draw the foreground of image glyph string S to PIXMAP. */
2237
2238 static void
2239 x_draw_image_foreground_1 (struct glyph_string *s, Pixmap pixmap)
2240 {
2241 int x = 0;
2242 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2243
2244 /* If first glyph of S has a left box line, start drawing it to the
2245 right of that line. */
2246 if (s->face->box != FACE_NO_BOX
2247 && s->first_glyph->left_box_line_p
2248 && s->slice.x == 0)
2249 x += eabs (s->face->box_line_width);
2250
2251 /* If there is a margin around the image, adjust x- and y-position
2252 by that margin. */
2253 if (s->slice.x == 0)
2254 x += s->img->hmargin;
2255 if (s->slice.y == 0)
2256 y += s->img->vmargin;
2257
2258 if (s->img->pixmap)
2259 {
2260 if (s->img->mask)
2261 {
2262 /* We can't set both a clip mask and use XSetClipRectangles
2263 because the latter also sets a clip mask. We also can't
2264 trust on the shape extension to be available
2265 (XShapeCombineRegion). So, compute the rectangle to draw
2266 manually. */
2267 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2268 | GCFunction);
2269 XGCValues xgcv;
2270
2271 xgcv.clip_mask = s->img->mask;
2272 xgcv.clip_x_origin = x - s->slice.x;
2273 xgcv.clip_y_origin = y - s->slice.y;
2274 xgcv.function = GXcopy;
2275 XChangeGC (s->display, s->gc, mask, &xgcv);
2276
2277 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2278 s->slice.x, s->slice.y,
2279 s->slice.width, s->slice.height, x, y);
2280 XSetClipMask (s->display, s->gc, None);
2281 }
2282 else
2283 {
2284 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2285 s->slice.x, s->slice.y,
2286 s->slice.width, s->slice.height, x, y);
2287
2288 /* When the image has a mask, we can expect that at
2289 least part of a mouse highlight or a block cursor will
2290 be visible. If the image doesn't have a mask, make
2291 a block cursor visible by drawing a rectangle around
2292 the image. I believe it's looking better if we do
2293 nothing here for mouse-face. */
2294 if (s->hl == DRAW_CURSOR)
2295 {
2296 int r = eabs (s->img->relief);
2297 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2298 s->slice.width + r*2 - 1,
2299 s->slice.height + r*2 - 1);
2300 }
2301 }
2302 }
2303 else
2304 /* Draw a rectangle if image could not be loaded. */
2305 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2306 s->slice.width - 1, s->slice.height - 1);
2307 }
2308
2309
2310 /* Draw part of the background of glyph string S. X, Y, W, and H
2311 give the rectangle to draw. */
2312
2313 static void
2314 x_draw_glyph_string_bg_rect (struct glyph_string *s, int x, int y, int w, int h)
2315 {
2316 if (s->stippled_p)
2317 {
2318 /* Fill background with a stipple pattern. */
2319 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2320 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2321 XSetFillStyle (s->display, s->gc, FillSolid);
2322 }
2323 else
2324 x_clear_glyph_string_rect (s, x, y, w, h);
2325 }
2326
2327
2328 /* Draw image glyph string S.
2329
2330 s->y
2331 s->x +-------------------------
2332 | s->face->box
2333 |
2334 | +-------------------------
2335 | | s->img->margin
2336 | |
2337 | | +-------------------
2338 | | | the image
2339
2340 */
2341
2342 static void
2343 x_draw_image_glyph_string (struct glyph_string *s)
2344 {
2345 int box_line_hwidth = eabs (s->face->box_line_width);
2346 int box_line_vwidth = max (s->face->box_line_width, 0);
2347 int height;
2348 Pixmap pixmap = None;
2349
2350 height = s->height;
2351 if (s->slice.y == 0)
2352 height -= box_line_vwidth;
2353 if (s->slice.y + s->slice.height >= s->img->height)
2354 height -= box_line_vwidth;
2355
2356 /* Fill background with face under the image. Do it only if row is
2357 taller than image or if image has a clip mask to reduce
2358 flickering. */
2359 s->stippled_p = s->face->stipple != 0;
2360 if (height > s->slice.height
2361 || s->img->hmargin
2362 || s->img->vmargin
2363 || s->img->mask
2364 || s->img->pixmap == 0
2365 || s->width != s->background_width)
2366 {
2367 if (s->img->mask)
2368 {
2369 /* Create a pixmap as large as the glyph string. Fill it
2370 with the background color. Copy the image to it, using
2371 its mask. Copy the temporary pixmap to the display. */
2372 Screen *screen = FRAME_X_SCREEN (s->f);
2373 int depth = DefaultDepthOfScreen (screen);
2374
2375 /* Create a pixmap as large as the glyph string. */
2376 pixmap = XCreatePixmap (s->display, s->window,
2377 s->background_width,
2378 s->height, depth);
2379
2380 /* Don't clip in the following because we're working on the
2381 pixmap. */
2382 XSetClipMask (s->display, s->gc, None);
2383
2384 /* Fill the pixmap with the background color/stipple. */
2385 if (s->stippled_p)
2386 {
2387 /* Fill background with a stipple pattern. */
2388 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2389 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
2390 XFillRectangle (s->display, pixmap, s->gc,
2391 0, 0, s->background_width, s->height);
2392 XSetFillStyle (s->display, s->gc, FillSolid);
2393 XSetTSOrigin (s->display, s->gc, 0, 0);
2394 }
2395 else
2396 {
2397 XGCValues xgcv;
2398 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2399 &xgcv);
2400 XSetForeground (s->display, s->gc, xgcv.background);
2401 XFillRectangle (s->display, pixmap, s->gc,
2402 0, 0, s->background_width, s->height);
2403 XSetForeground (s->display, s->gc, xgcv.foreground);
2404 }
2405 }
2406 else
2407 {
2408 int x = s->x;
2409 int y = s->y;
2410 int width = s->background_width;
2411
2412 if (s->first_glyph->left_box_line_p
2413 && s->slice.x == 0)
2414 {
2415 x += box_line_hwidth;
2416 width -= box_line_hwidth;
2417 }
2418
2419 if (s->slice.y == 0)
2420 y += box_line_vwidth;
2421
2422 x_draw_glyph_string_bg_rect (s, x, y, width, height);
2423 }
2424
2425 s->background_filled_p = 1;
2426 }
2427
2428 /* Draw the foreground. */
2429 if (pixmap != None)
2430 {
2431 x_draw_image_foreground_1 (s, pixmap);
2432 x_set_glyph_string_clipping (s);
2433 XCopyArea (s->display, pixmap, s->window, s->gc,
2434 0, 0, s->background_width, s->height, s->x, s->y);
2435 XFreePixmap (s->display, pixmap);
2436 }
2437 else
2438 x_draw_image_foreground (s);
2439
2440 /* If we must draw a relief around the image, do it. */
2441 if (s->img->relief
2442 || s->hl == DRAW_IMAGE_RAISED
2443 || s->hl == DRAW_IMAGE_SUNKEN)
2444 x_draw_image_relief (s);
2445 }
2446
2447
2448 /* Draw stretch glyph string S. */
2449
2450 static void
2451 x_draw_stretch_glyph_string (struct glyph_string *s)
2452 {
2453 eassert (s->first_glyph->type == STRETCH_GLYPH);
2454
2455 if (s->hl == DRAW_CURSOR
2456 && !x_stretch_cursor_p)
2457 {
2458 /* If `x-stretch-cursor' is nil, don't draw a block cursor as
2459 wide as the stretch glyph. */
2460 int width, background_width = s->background_width;
2461 int x = s->x;
2462
2463 if (!s->row->reversed_p)
2464 {
2465 int left_x = window_box_left_offset (s->w, TEXT_AREA);
2466
2467 if (x < left_x)
2468 {
2469 background_width -= left_x - x;
2470 x = left_x;
2471 }
2472 }
2473 else
2474 {
2475 /* In R2L rows, draw the cursor on the right edge of the
2476 stretch glyph. */
2477 int right_x = window_box_right_offset (s->w, TEXT_AREA);
2478
2479 if (x + background_width > right_x)
2480 background_width -= x - right_x;
2481 x += background_width;
2482 }
2483 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2484 if (s->row->reversed_p)
2485 x -= width;
2486
2487 /* Draw cursor. */
2488 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2489
2490 /* Clear rest using the GC of the original non-cursor face. */
2491 if (width < background_width)
2492 {
2493 int y = s->y;
2494 int w = background_width - width, h = s->height;
2495 XRectangle r;
2496 GC gc;
2497
2498 if (!s->row->reversed_p)
2499 x += width;
2500 else
2501 x = s->x;
2502 if (s->row->mouse_face_p
2503 && cursor_in_mouse_face_p (s->w))
2504 {
2505 x_set_mouse_face_gc (s);
2506 gc = s->gc;
2507 }
2508 else
2509 gc = s->face->gc;
2510
2511 get_glyph_string_clip_rect (s, &r);
2512 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2513
2514 if (s->face->stipple)
2515 {
2516 /* Fill background with a stipple pattern. */
2517 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2518 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2519 XSetFillStyle (s->display, gc, FillSolid);
2520 }
2521 else
2522 {
2523 XGCValues xgcv;
2524 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2525 XSetForeground (s->display, gc, xgcv.background);
2526 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2527 XSetForeground (s->display, gc, xgcv.foreground);
2528 }
2529
2530 XSetClipMask (s->display, gc, None);
2531 }
2532 }
2533 else if (!s->background_filled_p)
2534 {
2535 int background_width = s->background_width;
2536 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2537
2538 /* Don't draw into left margin, fringe or scrollbar area
2539 except for header line and mode line. */
2540 if (x < left_x && !s->row->mode_line_p)
2541 {
2542 background_width -= left_x - x;
2543 x = left_x;
2544 }
2545 if (background_width > 0)
2546 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2547 }
2548
2549 s->background_filled_p = 1;
2550 }
2551
2552 /*
2553 Draw a wavy line under S. The wave fills wave_height pixels from y0.
2554
2555 x0 wave_length = 2
2556 --
2557 y0 * * * * *
2558 |* * * * * * * * *
2559 wave_height = 3 | * * * *
2560
2561 */
2562
2563 static void
2564 x_draw_underwave (struct glyph_string *s)
2565 {
2566 int wave_height = 3, wave_length = 2;
2567 int dx, dy, x0, y0, width, x1, y1, x2, y2, odd, xmax;
2568 XRectangle wave_clip, string_clip, final_clip;
2569
2570 dx = wave_length;
2571 dy = wave_height - 1;
2572 x0 = s->x;
2573 y0 = s->ybase - wave_height + 3;
2574 width = s->width;
2575 xmax = x0 + width;
2576
2577 /* Find and set clipping rectangle */
2578
2579 wave_clip.x = x0;
2580 wave_clip.y = y0;
2581 wave_clip.width = width;
2582 wave_clip.height = wave_height;
2583 get_glyph_string_clip_rect (s, &string_clip);
2584
2585 if (!x_intersect_rectangles (&wave_clip, &string_clip, &final_clip))
2586 return;
2587
2588 XSetClipRectangles (s->display, s->gc, 0, 0, &final_clip, 1, Unsorted);
2589
2590 /* Draw the waves */
2591
2592 x1 = x0 - (x0 % dx);
2593 x2 = x1 + dx;
2594 odd = (x1/dx) % 2;
2595 y1 = y2 = y0;
2596
2597 if (odd)
2598 y1 += dy;
2599 else
2600 y2 += dy;
2601
2602 if (INT_MAX - dx < xmax)
2603 emacs_abort ();
2604
2605 while (x1 <= xmax)
2606 {
2607 XDrawLine (s->display, s->window, s->gc, x1, y1, x2, y2);
2608 x1 = x2, y1 = y2;
2609 x2 += dx, y2 = y0 + odd*dy;
2610 odd = !odd;
2611 }
2612
2613 /* Restore previous clipping rectangle(s) */
2614 XSetClipRectangles (s->display, s->gc, 0, 0, s->clip, s->num_clips, Unsorted);
2615 }
2616
2617
2618 /* Draw glyph string S. */
2619
2620 static void
2621 x_draw_glyph_string (struct glyph_string *s)
2622 {
2623 bool relief_drawn_p = 0;
2624
2625 /* If S draws into the background of its successors, draw the
2626 background of the successors first so that S can draw into it.
2627 This makes S->next use XDrawString instead of XDrawImageString. */
2628 if (s->next && s->right_overhang && !s->for_overlaps)
2629 {
2630 int width;
2631 struct glyph_string *next;
2632
2633 for (width = 0, next = s->next;
2634 next && width < s->right_overhang;
2635 width += next->width, next = next->next)
2636 if (next->first_glyph->type != IMAGE_GLYPH)
2637 {
2638 x_set_glyph_string_gc (next);
2639 x_set_glyph_string_clipping (next);
2640 if (next->first_glyph->type == STRETCH_GLYPH)
2641 x_draw_stretch_glyph_string (next);
2642 else
2643 x_draw_glyph_string_background (next, 1);
2644 next->num_clips = 0;
2645 }
2646 }
2647
2648 /* Set up S->gc, set clipping and draw S. */
2649 x_set_glyph_string_gc (s);
2650
2651 /* Draw relief (if any) in advance for char/composition so that the
2652 glyph string can be drawn over it. */
2653 if (!s->for_overlaps
2654 && s->face->box != FACE_NO_BOX
2655 && (s->first_glyph->type == CHAR_GLYPH
2656 || s->first_glyph->type == COMPOSITE_GLYPH))
2657
2658 {
2659 x_set_glyph_string_clipping (s);
2660 x_draw_glyph_string_background (s, 1);
2661 x_draw_glyph_string_box (s);
2662 x_set_glyph_string_clipping (s);
2663 relief_drawn_p = 1;
2664 }
2665 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
2666 && !s->clip_tail
2667 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2668 || (s->next && s->next->hl != s->hl && s->right_overhang)))
2669 /* We must clip just this glyph. left_overhang part has already
2670 drawn when s->prev was drawn, and right_overhang part will be
2671 drawn later when s->next is drawn. */
2672 x_set_glyph_string_clipping_exactly (s, s);
2673 else
2674 x_set_glyph_string_clipping (s);
2675
2676 switch (s->first_glyph->type)
2677 {
2678 case IMAGE_GLYPH:
2679 x_draw_image_glyph_string (s);
2680 break;
2681
2682 case STRETCH_GLYPH:
2683 x_draw_stretch_glyph_string (s);
2684 break;
2685
2686 case CHAR_GLYPH:
2687 if (s->for_overlaps)
2688 s->background_filled_p = 1;
2689 else
2690 x_draw_glyph_string_background (s, 0);
2691 x_draw_glyph_string_foreground (s);
2692 break;
2693
2694 case COMPOSITE_GLYPH:
2695 if (s->for_overlaps || (s->cmp_from > 0
2696 && ! s->first_glyph->u.cmp.automatic))
2697 s->background_filled_p = 1;
2698 else
2699 x_draw_glyph_string_background (s, 1);
2700 x_draw_composite_glyph_string_foreground (s);
2701 break;
2702
2703 case GLYPHLESS_GLYPH:
2704 if (s->for_overlaps)
2705 s->background_filled_p = 1;
2706 else
2707 x_draw_glyph_string_background (s, 1);
2708 x_draw_glyphless_glyph_string_foreground (s);
2709 break;
2710
2711 default:
2712 emacs_abort ();
2713 }
2714
2715 if (!s->for_overlaps)
2716 {
2717 /* Draw underline. */
2718 if (s->face->underline_p)
2719 {
2720 if (s->face->underline_type == FACE_UNDER_WAVE)
2721 {
2722 if (s->face->underline_defaulted_p)
2723 x_draw_underwave (s);
2724 else
2725 {
2726 XGCValues xgcv;
2727 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2728 XSetForeground (s->display, s->gc, s->face->underline_color);
2729 x_draw_underwave (s);
2730 XSetForeground (s->display, s->gc, xgcv.foreground);
2731 }
2732 }
2733 else if (s->face->underline_type == FACE_UNDER_LINE)
2734 {
2735 unsigned long thickness, position;
2736 int y;
2737
2738 if (s->prev && s->prev->face->underline_p
2739 && s->prev->face->underline_type == FACE_UNDER_LINE)
2740 {
2741 /* We use the same underline style as the previous one. */
2742 thickness = s->prev->underline_thickness;
2743 position = s->prev->underline_position;
2744 }
2745 else
2746 {
2747 /* Get the underline thickness. Default is 1 pixel. */
2748 if (s->font && s->font->underline_thickness > 0)
2749 thickness = s->font->underline_thickness;
2750 else
2751 thickness = 1;
2752 if (x_underline_at_descent_line)
2753 position = (s->height - thickness) - (s->ybase - s->y);
2754 else
2755 {
2756 /* Get the underline position. This is the recommended
2757 vertical offset in pixels from the baseline to the top of
2758 the underline. This is a signed value according to the
2759 specs, and its default is
2760
2761 ROUND ((maximum descent) / 2), with
2762 ROUND(x) = floor (x + 0.5) */
2763
2764 if (x_use_underline_position_properties
2765 && s->font && s->font->underline_position >= 0)
2766 position = s->font->underline_position;
2767 else if (s->font)
2768 position = (s->font->descent + 1) / 2;
2769 else
2770 position = underline_minimum_offset;
2771 }
2772 position = max (position, underline_minimum_offset);
2773 }
2774 /* Check the sanity of thickness and position. We should
2775 avoid drawing underline out of the current line area. */
2776 if (s->y + s->height <= s->ybase + position)
2777 position = (s->height - 1) - (s->ybase - s->y);
2778 if (s->y + s->height < s->ybase + position + thickness)
2779 thickness = (s->y + s->height) - (s->ybase + position);
2780 s->underline_thickness = thickness;
2781 s->underline_position = position;
2782 y = s->ybase + position;
2783 if (s->face->underline_defaulted_p)
2784 XFillRectangle (s->display, s->window, s->gc,
2785 s->x, y, s->width, thickness);
2786 else
2787 {
2788 XGCValues xgcv;
2789 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2790 XSetForeground (s->display, s->gc, s->face->underline_color);
2791 XFillRectangle (s->display, s->window, s->gc,
2792 s->x, y, s->width, thickness);
2793 XSetForeground (s->display, s->gc, xgcv.foreground);
2794 }
2795 }
2796 }
2797 /* Draw overline. */
2798 if (s->face->overline_p)
2799 {
2800 unsigned long dy = 0, h = 1;
2801
2802 if (s->face->overline_color_defaulted_p)
2803 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2804 s->width, h);
2805 else
2806 {
2807 XGCValues xgcv;
2808 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2809 XSetForeground (s->display, s->gc, s->face->overline_color);
2810 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2811 s->width, h);
2812 XSetForeground (s->display, s->gc, xgcv.foreground);
2813 }
2814 }
2815
2816 /* Draw strike-through. */
2817 if (s->face->strike_through_p)
2818 {
2819 unsigned long h = 1;
2820 unsigned long dy = (s->height - h) / 2;
2821
2822 if (s->face->strike_through_color_defaulted_p)
2823 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2824 s->width, h);
2825 else
2826 {
2827 XGCValues xgcv;
2828 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2829 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2830 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2831 s->width, h);
2832 XSetForeground (s->display, s->gc, xgcv.foreground);
2833 }
2834 }
2835
2836 /* Draw relief if not yet drawn. */
2837 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2838 x_draw_glyph_string_box (s);
2839
2840 if (s->prev)
2841 {
2842 struct glyph_string *prev;
2843
2844 for (prev = s->prev; prev; prev = prev->prev)
2845 if (prev->hl != s->hl
2846 && prev->x + prev->width + prev->right_overhang > s->x)
2847 {
2848 /* As prev was drawn while clipped to its own area, we
2849 must draw the right_overhang part using s->hl now. */
2850 enum draw_glyphs_face save = prev->hl;
2851
2852 prev->hl = s->hl;
2853 x_set_glyph_string_gc (prev);
2854 x_set_glyph_string_clipping_exactly (s, prev);
2855 if (prev->first_glyph->type == CHAR_GLYPH)
2856 x_draw_glyph_string_foreground (prev);
2857 else
2858 x_draw_composite_glyph_string_foreground (prev);
2859 XSetClipMask (prev->display, prev->gc, None);
2860 prev->hl = save;
2861 prev->num_clips = 0;
2862 }
2863 }
2864
2865 if (s->next)
2866 {
2867 struct glyph_string *next;
2868
2869 for (next = s->next; next; next = next->next)
2870 if (next->hl != s->hl
2871 && next->x - next->left_overhang < s->x + s->width)
2872 {
2873 /* As next will be drawn while clipped to its own area,
2874 we must draw the left_overhang part using s->hl now. */
2875 enum draw_glyphs_face save = next->hl;
2876
2877 next->hl = s->hl;
2878 x_set_glyph_string_gc (next);
2879 x_set_glyph_string_clipping_exactly (s, next);
2880 if (next->first_glyph->type == CHAR_GLYPH)
2881 x_draw_glyph_string_foreground (next);
2882 else
2883 x_draw_composite_glyph_string_foreground (next);
2884 XSetClipMask (next->display, next->gc, None);
2885 next->hl = save;
2886 next->num_clips = 0;
2887 next->clip_head = s->next;
2888 }
2889 }
2890 }
2891
2892 /* Reset clipping. */
2893 XSetClipMask (s->display, s->gc, None);
2894 s->num_clips = 0;
2895 }
2896
2897 /* Shift display to make room for inserted glyphs. */
2898
2899 static void
2900 x_shift_glyphs_for_insert (struct frame *f, int x, int y, int width, int height, int shift_by)
2901 {
2902 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2903 f->output_data.x->normal_gc,
2904 x, y, width, height,
2905 x + shift_by, y);
2906 }
2907
2908 /* Delete N glyphs at the nominal cursor position. Not implemented
2909 for X frames. */
2910
2911 static void
2912 x_delete_glyphs (struct frame *f, register int n)
2913 {
2914 emacs_abort ();
2915 }
2916
2917
2918 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2919 If they are <= 0, this is probably an error. */
2920
2921 void
2922 x_clear_area (Display *dpy, Window window, int x, int y, int width, int height)
2923 {
2924 eassert (width > 0 && height > 0);
2925 XClearArea (dpy, window, x, y, width, height, False);
2926 }
2927
2928
2929 /* Clear an entire frame. */
2930
2931 static void
2932 x_clear_frame (struct frame *f)
2933 {
2934 /* Clearing the frame will erase any cursor, so mark them all as no
2935 longer visible. */
2936 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2937
2938 block_input ();
2939
2940 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2941
2942 /* We have to clear the scroll bars. If we have changed colors or
2943 something like that, then they should be notified. */
2944 x_scroll_bar_clear (f);
2945
2946 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
2947 /* Make sure scroll bars are redrawn. As they aren't redrawn by
2948 redisplay, do it here. */
2949 if (FRAME_GTK_WIDGET (f))
2950 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
2951 #endif
2952
2953 XFlush (FRAME_X_DISPLAY (f));
2954
2955 unblock_input ();
2956 }
2957
2958 /* RIF: Show hourglass cursor on frame F. */
2959
2960 static void
2961 x_show_hourglass (struct frame *f)
2962 {
2963 Display *dpy = FRAME_X_DISPLAY (f);
2964
2965 if (dpy)
2966 {
2967 struct x_output *x = FRAME_X_OUTPUT (f);
2968 #ifdef USE_X_TOOLKIT
2969 if (x->widget)
2970 #else
2971 if (FRAME_OUTER_WINDOW (f))
2972 #endif
2973 {
2974 x->hourglass_p = 1;
2975
2976 if (!x->hourglass_window)
2977 {
2978 unsigned long mask = CWCursor;
2979 XSetWindowAttributes attrs;
2980 #ifdef USE_GTK
2981 Window parent = FRAME_X_WINDOW (f);
2982 #else
2983 Window parent = FRAME_OUTER_WINDOW (f);
2984 #endif
2985 attrs.cursor = x->hourglass_cursor;
2986
2987 x->hourglass_window = XCreateWindow
2988 (dpy, parent, 0, 0, 32000, 32000, 0, 0,
2989 InputOnly, CopyFromParent, mask, &attrs);
2990 }
2991
2992 XMapRaised (dpy, x->hourglass_window);
2993 XFlush (dpy);
2994 }
2995 }
2996 }
2997
2998 /* RIF: Cancel hourglass cursor on frame F. */
2999
3000 static void
3001 x_hide_hourglass (struct frame *f)
3002 {
3003 struct x_output *x = FRAME_X_OUTPUT (f);
3004
3005 /* Watch out for newly created frames. */
3006 if (x->hourglass_window)
3007 {
3008 XUnmapWindow (FRAME_X_DISPLAY (f), x->hourglass_window);
3009 /* Sync here because XTread_socket looks at the
3010 hourglass_p flag that is reset to zero below. */
3011 XSync (FRAME_X_DISPLAY (f), False);
3012 x->hourglass_p = 0;
3013 }
3014 }
3015
3016 /* Invert the middle quarter of the frame for .15 sec. */
3017
3018 static void
3019 XTflash (struct frame *f)
3020 {
3021 block_input ();
3022
3023 {
3024 #ifdef USE_GTK
3025 /* Use Gdk routines to draw. This way, we won't draw over scroll bars
3026 when the scroll bars and the edit widget share the same X window. */
3027 GdkWindow *window = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
3028 #ifdef HAVE_GTK3
3029 cairo_t *cr = gdk_cairo_create (window);
3030 cairo_set_source_rgb (cr, 1, 1, 1);
3031 cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
3032 #define XFillRectangle(d, win, gc, x, y, w, h) \
3033 do { \
3034 cairo_rectangle (cr, x, y, w, h); \
3035 cairo_fill (cr); \
3036 } \
3037 while (0)
3038 #else /* ! HAVE_GTK3 */
3039 GdkGCValues vals;
3040 GdkGC *gc;
3041 vals.foreground.pixel = (FRAME_FOREGROUND_PIXEL (f)
3042 ^ FRAME_BACKGROUND_PIXEL (f));
3043 vals.function = GDK_XOR;
3044 gc = gdk_gc_new_with_values (window,
3045 &vals, GDK_GC_FUNCTION | GDK_GC_FOREGROUND);
3046 #define XFillRectangle(d, win, gc, x, y, w, h) \
3047 gdk_draw_rectangle (window, gc, TRUE, x, y, w, h)
3048 #endif /* ! HAVE_GTK3 */
3049 #else /* ! USE_GTK */
3050 GC gc;
3051
3052 /* Create a GC that will use the GXxor function to flip foreground
3053 pixels into background pixels. */
3054 {
3055 XGCValues values;
3056
3057 values.function = GXxor;
3058 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
3059 ^ FRAME_BACKGROUND_PIXEL (f));
3060
3061 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3062 GCFunction | GCForeground, &values);
3063 }
3064 #endif
3065 {
3066 /* Get the height not including a menu bar widget. */
3067 int height = FRAME_PIXEL_HEIGHT (f);
3068 /* Height of each line to flash. */
3069 int flash_height = FRAME_LINE_HEIGHT (f);
3070 /* These will be the left and right margins of the rectangles. */
3071 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
3072 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
3073 int width = flash_right - flash_left;
3074
3075 /* If window is tall, flash top and bottom line. */
3076 if (height > 3 * FRAME_LINE_HEIGHT (f))
3077 {
3078 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3079 flash_left,
3080 (FRAME_INTERNAL_BORDER_WIDTH (f)
3081 + FRAME_TOP_MARGIN_HEIGHT (f)),
3082 width, flash_height);
3083 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3084 flash_left,
3085 (height - flash_height
3086 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3087 width, flash_height);
3088
3089 }
3090 else
3091 /* If it is short, flash it all. */
3092 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3093 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3094 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3095
3096 x_flush (f);
3097
3098 {
3099 struct timespec delay = make_timespec (0, 150 * 1000 * 1000);
3100 struct timespec wakeup = timespec_add (current_timespec (), delay);
3101
3102 /* Keep waiting until past the time wakeup or any input gets
3103 available. */
3104 while (! detect_input_pending ())
3105 {
3106 struct timespec current = current_timespec ();
3107 struct timespec timeout;
3108
3109 /* Break if result would not be positive. */
3110 if (timespec_cmp (wakeup, current) <= 0)
3111 break;
3112
3113 /* How long `select' should wait. */
3114 timeout = make_timespec (0, 10 * 1000 * 1000);
3115
3116 /* Try to wait that long--but we might wake up sooner. */
3117 pselect (0, NULL, NULL, NULL, &timeout, NULL);
3118 }
3119 }
3120
3121 /* If window is tall, flash top and bottom line. */
3122 if (height > 3 * FRAME_LINE_HEIGHT (f))
3123 {
3124 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3125 flash_left,
3126 (FRAME_INTERNAL_BORDER_WIDTH (f)
3127 + FRAME_TOP_MARGIN_HEIGHT (f)),
3128 width, flash_height);
3129 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3130 flash_left,
3131 (height - flash_height
3132 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3133 width, flash_height);
3134 }
3135 else
3136 /* If it is short, flash it all. */
3137 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3138 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3139 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3140
3141 #ifdef USE_GTK
3142 #ifdef HAVE_GTK3
3143 cairo_destroy (cr);
3144 #else
3145 g_object_unref (G_OBJECT (gc));
3146 #endif
3147 #undef XFillRectangle
3148 #else
3149 XFreeGC (FRAME_X_DISPLAY (f), gc);
3150 #endif
3151 x_flush (f);
3152 }
3153 }
3154
3155 unblock_input ();
3156 }
3157
3158
3159 static void
3160 XTtoggle_invisible_pointer (struct frame *f, int invisible)
3161 {
3162 block_input ();
3163 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, invisible);
3164 unblock_input ();
3165 }
3166
3167
3168 /* Make audible bell. */
3169
3170 static void
3171 XTring_bell (struct frame *f)
3172 {
3173 if (FRAME_X_DISPLAY (f))
3174 {
3175 if (visible_bell)
3176 XTflash (f);
3177 else
3178 {
3179 block_input ();
3180 #ifdef HAVE_XKB
3181 XkbBell (FRAME_X_DISPLAY (f), None, 0, None);
3182 #else
3183 XBell (FRAME_X_DISPLAY (f), 0);
3184 #endif
3185 XFlush (FRAME_X_DISPLAY (f));
3186 unblock_input ();
3187 }
3188 }
3189 }
3190
3191 /***********************************************************************
3192 Line Dance
3193 ***********************************************************************/
3194
3195 /* Perform an insert-lines or delete-lines operation, inserting N
3196 lines or deleting -N lines at vertical position VPOS. */
3197
3198 static void
3199 x_ins_del_lines (struct frame *f, int vpos, int n)
3200 {
3201 emacs_abort ();
3202 }
3203
3204
3205 /* Scroll part of the display as described by RUN. */
3206
3207 static void
3208 x_scroll_run (struct window *w, struct run *run)
3209 {
3210 struct frame *f = XFRAME (w->frame);
3211 int x, y, width, height, from_y, to_y, bottom_y;
3212
3213 /* Get frame-relative bounding box of the text display area of W,
3214 without mode lines. Include in this box the left and right
3215 fringe of W. */
3216 window_box (w, ANY_AREA, &x, &y, &width, &height);
3217
3218 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3219 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3220 bottom_y = y + height;
3221
3222 if (to_y < from_y)
3223 {
3224 /* Scrolling up. Make sure we don't copy part of the mode
3225 line at the bottom. */
3226 if (from_y + run->height > bottom_y)
3227 height = bottom_y - from_y;
3228 else
3229 height = run->height;
3230 }
3231 else
3232 {
3233 /* Scrolling down. Make sure we don't copy over the mode line.
3234 at the bottom. */
3235 if (to_y + run->height > bottom_y)
3236 height = bottom_y - to_y;
3237 else
3238 height = run->height;
3239 }
3240
3241 block_input ();
3242
3243 /* Cursor off. Will be switched on again in x_update_window_end. */
3244 x_clear_cursor (w);
3245
3246 XCopyArea (FRAME_X_DISPLAY (f),
3247 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3248 f->output_data.x->normal_gc,
3249 x, from_y,
3250 width, height,
3251 x, to_y);
3252
3253 unblock_input ();
3254 }
3255
3256
3257 \f
3258 /***********************************************************************
3259 Exposure Events
3260 ***********************************************************************/
3261
3262 \f
3263 static void
3264 frame_highlight (struct frame *f)
3265 {
3266 /* We used to only do this if Vx_no_window_manager was non-nil, but
3267 the ICCCM (section 4.1.6) says that the window's border pixmap
3268 and border pixel are window attributes which are "private to the
3269 client", so we can always change it to whatever we want. */
3270 block_input ();
3271 /* I recently started to get errors in this XSetWindowBorder, depending on
3272 the window-manager in use, tho something more is at play since I've been
3273 using that same window-manager binary for ever. Let's not crash just
3274 because of this (bug#9310). */
3275 x_catch_errors (FRAME_X_DISPLAY (f));
3276 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3277 f->output_data.x->border_pixel);
3278 x_uncatch_errors ();
3279 unblock_input ();
3280 x_update_cursor (f, 1);
3281 x_set_frame_alpha (f);
3282 }
3283
3284 static void
3285 frame_unhighlight (struct frame *f)
3286 {
3287 /* We used to only do this if Vx_no_window_manager was non-nil, but
3288 the ICCCM (section 4.1.6) says that the window's border pixmap
3289 and border pixel are window attributes which are "private to the
3290 client", so we can always change it to whatever we want. */
3291 block_input ();
3292 /* Same as above for XSetWindowBorder (bug#9310). */
3293 x_catch_errors (FRAME_X_DISPLAY (f));
3294 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3295 f->output_data.x->border_tile);
3296 x_uncatch_errors ();
3297 unblock_input ();
3298 x_update_cursor (f, 1);
3299 x_set_frame_alpha (f);
3300 }
3301
3302 /* The focus has changed. Update the frames as necessary to reflect
3303 the new situation. Note that we can't change the selected frame
3304 here, because the Lisp code we are interrupting might become confused.
3305 Each event gets marked with the frame in which it occurred, so the
3306 Lisp code can tell when the switch took place by examining the events. */
3307
3308 static void
3309 x_new_focus_frame (struct x_display_info *dpyinfo, struct frame *frame)
3310 {
3311 struct frame *old_focus = dpyinfo->x_focus_frame;
3312
3313 if (frame != dpyinfo->x_focus_frame)
3314 {
3315 /* Set this before calling other routines, so that they see
3316 the correct value of x_focus_frame. */
3317 dpyinfo->x_focus_frame = frame;
3318
3319 if (old_focus && old_focus->auto_lower)
3320 x_lower_frame (old_focus);
3321
3322 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3323 dpyinfo->x_pending_autoraise_frame = dpyinfo->x_focus_frame;
3324 else
3325 dpyinfo->x_pending_autoraise_frame = NULL;
3326 }
3327
3328 x_frame_rehighlight (dpyinfo);
3329 }
3330
3331 /* Handle FocusIn and FocusOut state changes for FRAME.
3332 If FRAME has focus and there exists more than one frame, puts
3333 a FOCUS_IN_EVENT into *BUFP. */
3334
3335 static void
3336 x_focus_changed (int type, int state, struct x_display_info *dpyinfo, struct frame *frame, struct input_event *bufp)
3337 {
3338 if (type == FocusIn)
3339 {
3340 if (dpyinfo->x_focus_event_frame != frame)
3341 {
3342 x_new_focus_frame (dpyinfo, frame);
3343 dpyinfo->x_focus_event_frame = frame;
3344
3345 /* Don't stop displaying the initial startup message
3346 for a switch-frame event we don't need. */
3347 /* When run as a daemon, Vterminal_frame is always NIL. */
3348 bufp->arg = (((NILP (Vterminal_frame)
3349 || ! FRAME_X_P (XFRAME (Vterminal_frame))
3350 || EQ (Fdaemonp (), Qt))
3351 && CONSP (Vframe_list)
3352 && !NILP (XCDR (Vframe_list)))
3353 ? Qt : Qnil);
3354 bufp->kind = FOCUS_IN_EVENT;
3355 XSETFRAME (bufp->frame_or_window, frame);
3356 }
3357
3358 frame->output_data.x->focus_state |= state;
3359
3360 #ifdef HAVE_X_I18N
3361 if (FRAME_XIC (frame))
3362 XSetICFocus (FRAME_XIC (frame));
3363 #endif
3364 }
3365 else if (type == FocusOut)
3366 {
3367 frame->output_data.x->focus_state &= ~state;
3368
3369 if (dpyinfo->x_focus_event_frame == frame)
3370 {
3371 dpyinfo->x_focus_event_frame = 0;
3372 x_new_focus_frame (dpyinfo, 0);
3373
3374 bufp->kind = FOCUS_OUT_EVENT;
3375 XSETFRAME (bufp->frame_or_window, frame);
3376 }
3377
3378 #ifdef HAVE_X_I18N
3379 if (FRAME_XIC (frame))
3380 XUnsetICFocus (FRAME_XIC (frame));
3381 #endif
3382 if (frame->pointer_invisible)
3383 XTtoggle_invisible_pointer (frame, 0);
3384 }
3385 }
3386
3387 /* Return the Emacs frame-object corresponding to an X window.
3388 It could be the frame's main window or an icon window. */
3389
3390 static struct frame *
3391 x_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
3392 {
3393 Lisp_Object tail, frame;
3394 struct frame *f;
3395
3396 if (wdesc == None)
3397 return NULL;
3398
3399 FOR_EACH_FRAME (tail, frame)
3400 {
3401 f = XFRAME (frame);
3402 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
3403 continue;
3404 if (f->output_data.x->hourglass_window == wdesc)
3405 return f;
3406 #ifdef USE_X_TOOLKIT
3407 if ((f->output_data.x->edit_widget
3408 && XtWindow (f->output_data.x->edit_widget) == wdesc)
3409 /* A tooltip frame? */
3410 || (!f->output_data.x->edit_widget
3411 && FRAME_X_WINDOW (f) == wdesc)
3412 || f->output_data.x->icon_desc == wdesc)
3413 return f;
3414 #else /* not USE_X_TOOLKIT */
3415 #ifdef USE_GTK
3416 if (f->output_data.x->edit_widget)
3417 {
3418 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
3419 struct x_output *x = f->output_data.x;
3420 if (gwdesc != 0 && gwdesc == x->edit_widget)
3421 return f;
3422 }
3423 #endif /* USE_GTK */
3424 if (FRAME_X_WINDOW (f) == wdesc
3425 || f->output_data.x->icon_desc == wdesc)
3426 return f;
3427 #endif /* not USE_X_TOOLKIT */
3428 }
3429 return 0;
3430 }
3431
3432 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
3433
3434 /* Like x_window_to_frame but also compares the window with the widget's
3435 windows. */
3436
3437 static struct frame *
3438 x_any_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
3439 {
3440 Lisp_Object tail, frame;
3441 struct frame *f, *found = NULL;
3442 struct x_output *x;
3443
3444 if (wdesc == None)
3445 return NULL;
3446
3447 FOR_EACH_FRAME (tail, frame)
3448 {
3449 if (found)
3450 break;
3451 f = XFRAME (frame);
3452 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
3453 {
3454 /* This frame matches if the window is any of its widgets. */
3455 x = f->output_data.x;
3456 if (x->hourglass_window == wdesc)
3457 found = f;
3458 else if (x->widget)
3459 {
3460 #ifdef USE_GTK
3461 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
3462 if (gwdesc != 0
3463 && gtk_widget_get_toplevel (gwdesc) == x->widget)
3464 found = f;
3465 #else
3466 if (wdesc == XtWindow (x->widget)
3467 || wdesc == XtWindow (x->column_widget)
3468 || wdesc == XtWindow (x->edit_widget))
3469 found = f;
3470 /* Match if the window is this frame's menubar. */
3471 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
3472 found = f;
3473 #endif
3474 }
3475 else if (FRAME_X_WINDOW (f) == wdesc)
3476 /* A tooltip frame. */
3477 found = f;
3478 }
3479 }
3480
3481 return found;
3482 }
3483
3484 /* Likewise, but consider only the menu bar widget. */
3485
3486 static struct frame *
3487 x_menubar_window_to_frame (struct x_display_info *dpyinfo,
3488 const XEvent *event)
3489 {
3490 Window wdesc = event->xany.window;
3491 Lisp_Object tail, frame;
3492 struct frame *f;
3493 struct x_output *x;
3494
3495 if (wdesc == None)
3496 return NULL;
3497
3498 FOR_EACH_FRAME (tail, frame)
3499 {
3500 f = XFRAME (frame);
3501 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
3502 continue;
3503 x = f->output_data.x;
3504 #ifdef USE_GTK
3505 if (x->menubar_widget && xg_event_is_for_menubar (f, event))
3506 return f;
3507 #else
3508 /* Match if the window is this frame's menubar. */
3509 if (x->menubar_widget
3510 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
3511 return f;
3512 #endif
3513 }
3514 return 0;
3515 }
3516
3517 /* Return the frame whose principal (outermost) window is WDESC.
3518 If WDESC is some other (smaller) window, we return 0. */
3519
3520 struct frame *
3521 x_top_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
3522 {
3523 Lisp_Object tail, frame;
3524 struct frame *f;
3525 struct x_output *x;
3526
3527 if (wdesc == None)
3528 return NULL;
3529
3530 FOR_EACH_FRAME (tail, frame)
3531 {
3532 f = XFRAME (frame);
3533 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
3534 continue;
3535 x = f->output_data.x;
3536
3537 if (x->widget)
3538 {
3539 /* This frame matches if the window is its topmost widget. */
3540 #ifdef USE_GTK
3541 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
3542 if (gwdesc == x->widget)
3543 return f;
3544 #else
3545 if (wdesc == XtWindow (x->widget))
3546 return f;
3547 #endif
3548 }
3549 else if (FRAME_X_WINDOW (f) == wdesc)
3550 /* Tooltip frame. */
3551 return f;
3552 }
3553 return 0;
3554 }
3555
3556 #else /* !USE_X_TOOLKIT && !USE_GTK */
3557
3558 #define x_any_window_to_frame(d, i) x_window_to_frame (d, i)
3559 #define x_top_window_to_frame(d, i) x_window_to_frame (d, i)
3560
3561 #endif /* USE_X_TOOLKIT || USE_GTK */
3562
3563 /* The focus may have changed. Figure out if it is a real focus change,
3564 by checking both FocusIn/Out and Enter/LeaveNotify events.
3565
3566 Returns FOCUS_IN_EVENT event in *BUFP. */
3567
3568 static void
3569 x_detect_focus_change (struct x_display_info *dpyinfo, struct frame *frame,
3570 const XEvent *event, struct input_event *bufp)
3571 {
3572 if (!frame)
3573 return;
3574
3575 switch (event->type)
3576 {
3577 case EnterNotify:
3578 case LeaveNotify:
3579 {
3580 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3581 int focus_state
3582 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3583
3584 if (event->xcrossing.detail != NotifyInferior
3585 && event->xcrossing.focus
3586 && ! (focus_state & FOCUS_EXPLICIT))
3587 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3588 FOCUS_IMPLICIT,
3589 dpyinfo, frame, bufp);
3590 }
3591 break;
3592
3593 case FocusIn:
3594 case FocusOut:
3595 x_focus_changed (event->type,
3596 (event->xfocus.detail == NotifyPointer ?
3597 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3598 dpyinfo, frame, bufp);
3599 break;
3600
3601 case ClientMessage:
3602 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
3603 {
3604 enum xembed_message msg = event->xclient.data.l[1];
3605 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
3606 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
3607 }
3608 break;
3609 }
3610 }
3611
3612
3613 #if !defined USE_X_TOOLKIT && !defined USE_GTK
3614 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3615
3616 void
3617 x_mouse_leave (struct x_display_info *dpyinfo)
3618 {
3619 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3620 }
3621 #endif
3622
3623 /* The focus has changed, or we have redirected a frame's focus to
3624 another frame (this happens when a frame uses a surrogate
3625 mini-buffer frame). Shift the highlight as appropriate.
3626
3627 The FRAME argument doesn't necessarily have anything to do with which
3628 frame is being highlighted or un-highlighted; we only use it to find
3629 the appropriate X display info. */
3630
3631 static void
3632 XTframe_rehighlight (struct frame *frame)
3633 {
3634 x_frame_rehighlight (FRAME_DISPLAY_INFO (frame));
3635 }
3636
3637 static void
3638 x_frame_rehighlight (struct x_display_info *dpyinfo)
3639 {
3640 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3641
3642 if (dpyinfo->x_focus_frame)
3643 {
3644 dpyinfo->x_highlight_frame
3645 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3646 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3647 : dpyinfo->x_focus_frame);
3648 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3649 {
3650 fset_focus_frame (dpyinfo->x_focus_frame, Qnil);
3651 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3652 }
3653 }
3654 else
3655 dpyinfo->x_highlight_frame = 0;
3656
3657 if (dpyinfo->x_highlight_frame != old_highlight)
3658 {
3659 if (old_highlight)
3660 frame_unhighlight (old_highlight);
3661 if (dpyinfo->x_highlight_frame)
3662 frame_highlight (dpyinfo->x_highlight_frame);
3663 }
3664 }
3665
3666
3667 \f
3668 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3669
3670 /* Initialize mode_switch_bit and modifier_meaning. */
3671 static void
3672 x_find_modifier_meanings (struct x_display_info *dpyinfo)
3673 {
3674 int min_code, max_code;
3675 KeySym *syms;
3676 int syms_per_code;
3677 XModifierKeymap *mods;
3678
3679 dpyinfo->meta_mod_mask = 0;
3680 dpyinfo->shift_lock_mask = 0;
3681 dpyinfo->alt_mod_mask = 0;
3682 dpyinfo->super_mod_mask = 0;
3683 dpyinfo->hyper_mod_mask = 0;
3684
3685 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3686
3687 syms = XGetKeyboardMapping (dpyinfo->display,
3688 min_code, max_code - min_code + 1,
3689 &syms_per_code);
3690 mods = XGetModifierMapping (dpyinfo->display);
3691
3692 /* Scan the modifier table to see which modifier bits the Meta and
3693 Alt keysyms are on. */
3694 {
3695 int row, col; /* The row and column in the modifier table. */
3696 int found_alt_or_meta;
3697
3698 for (row = 3; row < 8; row++)
3699 {
3700 found_alt_or_meta = 0;
3701 for (col = 0; col < mods->max_keypermod; col++)
3702 {
3703 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
3704
3705 /* Zeroes are used for filler. Skip them. */
3706 if (code == 0)
3707 continue;
3708
3709 /* Are any of this keycode's keysyms a meta key? */
3710 {
3711 int code_col;
3712
3713 for (code_col = 0; code_col < syms_per_code; code_col++)
3714 {
3715 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3716
3717 switch (sym)
3718 {
3719 case XK_Meta_L:
3720 case XK_Meta_R:
3721 found_alt_or_meta = 1;
3722 dpyinfo->meta_mod_mask |= (1 << row);
3723 break;
3724
3725 case XK_Alt_L:
3726 case XK_Alt_R:
3727 found_alt_or_meta = 1;
3728 dpyinfo->alt_mod_mask |= (1 << row);
3729 break;
3730
3731 case XK_Hyper_L:
3732 case XK_Hyper_R:
3733 if (!found_alt_or_meta)
3734 dpyinfo->hyper_mod_mask |= (1 << row);
3735 code_col = syms_per_code;
3736 col = mods->max_keypermod;
3737 break;
3738
3739 case XK_Super_L:
3740 case XK_Super_R:
3741 if (!found_alt_or_meta)
3742 dpyinfo->super_mod_mask |= (1 << row);
3743 code_col = syms_per_code;
3744 col = mods->max_keypermod;
3745 break;
3746
3747 case XK_Shift_Lock:
3748 /* Ignore this if it's not on the lock modifier. */
3749 if (!found_alt_or_meta && ((1 << row) == LockMask))
3750 dpyinfo->shift_lock_mask = LockMask;
3751 code_col = syms_per_code;
3752 col = mods->max_keypermod;
3753 break;
3754 }
3755 }
3756 }
3757 }
3758 }
3759 }
3760
3761 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3762 if (! dpyinfo->meta_mod_mask)
3763 {
3764 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3765 dpyinfo->alt_mod_mask = 0;
3766 }
3767
3768 /* If some keys are both alt and meta,
3769 make them just meta, not alt. */
3770 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3771 {
3772 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3773 }
3774
3775 XFree (syms);
3776 XFreeModifiermap (mods);
3777 }
3778
3779 /* Convert between the modifier bits X uses and the modifier bits
3780 Emacs uses. */
3781
3782 int
3783 x_x_to_emacs_modifiers (struct x_display_info *dpyinfo, int state)
3784 {
3785 int mod_meta = meta_modifier;
3786 int mod_alt = alt_modifier;
3787 int mod_hyper = hyper_modifier;
3788 int mod_super = super_modifier;
3789 Lisp_Object tem;
3790
3791 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3792 if (INTEGERP (tem)) mod_alt = XINT (tem) & INT_MAX;
3793 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3794 if (INTEGERP (tem)) mod_meta = XINT (tem) & INT_MAX;
3795 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3796 if (INTEGERP (tem)) mod_hyper = XINT (tem) & INT_MAX;
3797 tem = Fget (Vx_super_keysym, Qmodifier_value);
3798 if (INTEGERP (tem)) mod_super = XINT (tem) & INT_MAX;
3799
3800 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3801 | ((state & ControlMask) ? ctrl_modifier : 0)
3802 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3803 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3804 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3805 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3806 }
3807
3808 static int
3809 x_emacs_to_x_modifiers (struct x_display_info *dpyinfo, EMACS_INT state)
3810 {
3811 EMACS_INT mod_meta = meta_modifier;
3812 EMACS_INT mod_alt = alt_modifier;
3813 EMACS_INT mod_hyper = hyper_modifier;
3814 EMACS_INT mod_super = super_modifier;
3815
3816 Lisp_Object tem;
3817
3818 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3819 if (INTEGERP (tem)) mod_alt = XINT (tem);
3820 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3821 if (INTEGERP (tem)) mod_meta = XINT (tem);
3822 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3823 if (INTEGERP (tem)) mod_hyper = XINT (tem);
3824 tem = Fget (Vx_super_keysym, Qmodifier_value);
3825 if (INTEGERP (tem)) mod_super = XINT (tem);
3826
3827
3828 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3829 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3830 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3831 | ((state & shift_modifier) ? ShiftMask : 0)
3832 | ((state & ctrl_modifier) ? ControlMask : 0)
3833 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3834 }
3835
3836 /* Convert a keysym to its name. */
3837
3838 char *
3839 x_get_keysym_name (int keysym)
3840 {
3841 char *value;
3842
3843 block_input ();
3844 value = XKeysymToString (keysym);
3845 unblock_input ();
3846
3847 return value;
3848 }
3849
3850 /* Mouse clicks and mouse movement. Rah.
3851
3852 Formerly, we used PointerMotionHintMask (in standard_event_mask)
3853 so that we would have to call XQueryPointer after each MotionNotify
3854 event to ask for another such event. However, this made mouse tracking
3855 slow, and there was a bug that made it eventually stop.
3856
3857 Simply asking for MotionNotify all the time seems to work better.
3858
3859 In order to avoid asking for motion events and then throwing most
3860 of them away or busy-polling the server for mouse positions, we ask
3861 the server for pointer motion hints. This means that we get only
3862 one event per group of mouse movements. "Groups" are delimited by
3863 other kinds of events (focus changes and button clicks, for
3864 example), or by XQueryPointer calls; when one of these happens, we
3865 get another MotionNotify event the next time the mouse moves. This
3866 is at least as efficient as getting motion events when mouse
3867 tracking is on, and I suspect only negligibly worse when tracking
3868 is off. */
3869
3870 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3871
3872 If the event is a button press, then note that we have grabbed
3873 the mouse. */
3874
3875 static Lisp_Object
3876 construct_mouse_click (struct input_event *result,
3877 const XButtonEvent *event,
3878 struct frame *f)
3879 {
3880 /* Make the event type NO_EVENT; we'll change that when we decide
3881 otherwise. */
3882 result->kind = MOUSE_CLICK_EVENT;
3883 result->code = event->button - Button1;
3884 result->timestamp = event->time;
3885 result->modifiers = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f),
3886 event->state)
3887 | (event->type == ButtonRelease
3888 ? up_modifier
3889 : down_modifier));
3890
3891 XSETINT (result->x, event->x);
3892 XSETINT (result->y, event->y);
3893 XSETFRAME (result->frame_or_window, f);
3894 result->arg = Qnil;
3895 return Qnil;
3896 }
3897
3898 /* Function to report a mouse movement to the mainstream Emacs code.
3899 The input handler calls this.
3900
3901 We have received a mouse movement event, which is given in *event.
3902 If the mouse is over a different glyph than it was last time, tell
3903 the mainstream emacs code by setting mouse_moved. If not, ask for
3904 another motion event, so we can check again the next time it moves. */
3905
3906 static int
3907 note_mouse_movement (struct frame *frame, const XMotionEvent *event)
3908 {
3909 XRectangle *r;
3910 struct x_display_info *dpyinfo;
3911
3912 if (!FRAME_X_OUTPUT (frame))
3913 return 0;
3914
3915 dpyinfo = FRAME_DISPLAY_INFO (frame);
3916 dpyinfo->last_mouse_movement_time = event->time;
3917 dpyinfo->last_mouse_motion_frame = frame;
3918 dpyinfo->last_mouse_motion_x = event->x;
3919 dpyinfo->last_mouse_motion_y = event->y;
3920
3921 if (event->window != FRAME_X_WINDOW (frame))
3922 {
3923 frame->mouse_moved = 1;
3924 dpyinfo->last_mouse_scroll_bar = NULL;
3925 note_mouse_highlight (frame, -1, -1);
3926 dpyinfo->last_mouse_glyph_frame = NULL;
3927 return 1;
3928 }
3929
3930
3931 /* Has the mouse moved off the glyph it was on at the last sighting? */
3932 r = &dpyinfo->last_mouse_glyph;
3933 if (frame != dpyinfo->last_mouse_glyph_frame
3934 || event->x < r->x || event->x >= r->x + r->width
3935 || event->y < r->y || event->y >= r->y + r->height)
3936 {
3937 frame->mouse_moved = 1;
3938 dpyinfo->last_mouse_scroll_bar = NULL;
3939 note_mouse_highlight (frame, event->x, event->y);
3940 /* Remember which glyph we're now on. */
3941 remember_mouse_glyph (frame, event->x, event->y, r);
3942 dpyinfo->last_mouse_glyph_frame = frame;
3943 return 1;
3944 }
3945
3946 return 0;
3947 }
3948
3949 /* Return the current position of the mouse.
3950 *FP should be a frame which indicates which display to ask about.
3951
3952 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3953 and *PART to the frame, window, and scroll bar part that the mouse
3954 is over. Set *X and *Y to the portion and whole of the mouse's
3955 position on the scroll bar.
3956
3957 If the mouse movement started elsewhere, set *FP to the frame the
3958 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3959 the mouse is over.
3960
3961 Set *TIMESTAMP to the server time-stamp for the time at which the mouse
3962 was at this position.
3963
3964 Don't store anything if we don't have a valid set of values to report.
3965
3966 This clears the mouse_moved flag, so we can wait for the next mouse
3967 movement. */
3968
3969 static void
3970 XTmouse_position (struct frame **fp, int insist, Lisp_Object *bar_window,
3971 enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
3972 Time *timestamp)
3973 {
3974 struct frame *f1;
3975 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
3976
3977 block_input ();
3978
3979 if (dpyinfo->last_mouse_scroll_bar && insist == 0)
3980 {
3981 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
3982
3983 if (bar->horizontal)
3984 x_horizontal_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
3985 else
3986 x_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
3987 }
3988 else
3989 {
3990 Window root;
3991 int root_x, root_y;
3992
3993 Window dummy_window;
3994 int dummy;
3995
3996 Lisp_Object frame, tail;
3997
3998 /* Clear the mouse-moved flag for every frame on this display. */
3999 FOR_EACH_FRAME (tail, frame)
4000 if (FRAME_X_P (XFRAME (frame))
4001 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
4002 XFRAME (frame)->mouse_moved = 0;
4003
4004 dpyinfo->last_mouse_scroll_bar = NULL;
4005
4006 /* Figure out which root window we're on. */
4007 XQueryPointer (FRAME_X_DISPLAY (*fp),
4008 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
4009
4010 /* The root window which contains the pointer. */
4011 &root,
4012
4013 /* Trash which we can't trust if the pointer is on
4014 a different screen. */
4015 &dummy_window,
4016
4017 /* The position on that root window. */
4018 &root_x, &root_y,
4019
4020 /* More trash we can't trust. */
4021 &dummy, &dummy,
4022
4023 /* Modifier keys and pointer buttons, about which
4024 we don't care. */
4025 (unsigned int *) &dummy);
4026
4027 /* Now we have a position on the root; find the innermost window
4028 containing the pointer. */
4029 {
4030 Window win, child;
4031 int win_x, win_y;
4032 int parent_x = 0, parent_y = 0;
4033
4034 win = root;
4035
4036 /* XTranslateCoordinates can get errors if the window
4037 structure is changing at the same time this function
4038 is running. So at least we must not crash from them. */
4039
4040 x_catch_errors (FRAME_X_DISPLAY (*fp));
4041
4042 if (x_mouse_grabbed (dpyinfo))
4043 {
4044 /* If mouse was grabbed on a frame, give coords for that frame
4045 even if the mouse is now outside it. */
4046 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
4047
4048 /* From-window. */
4049 root,
4050
4051 /* To-window. */
4052 FRAME_X_WINDOW (dpyinfo->last_mouse_frame),
4053
4054 /* From-position, to-position. */
4055 root_x, root_y, &win_x, &win_y,
4056
4057 /* Child of win. */
4058 &child);
4059 f1 = dpyinfo->last_mouse_frame;
4060 }
4061 else
4062 {
4063 while (1)
4064 {
4065 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
4066
4067 /* From-window, to-window. */
4068 root, win,
4069
4070 /* From-position, to-position. */
4071 root_x, root_y, &win_x, &win_y,
4072
4073 /* Child of win. */
4074 &child);
4075
4076 if (child == None || child == win)
4077 break;
4078 #ifdef USE_GTK
4079 /* We don't wan't to know the innermost window. We
4080 want the edit window. For non-Gtk+ the innermost
4081 window is the edit window. For Gtk+ it might not
4082 be. It might be the tool bar for example. */
4083 if (x_window_to_frame (dpyinfo, win))
4084 break;
4085 #endif
4086 win = child;
4087 parent_x = win_x;
4088 parent_y = win_y;
4089 }
4090
4091 /* Now we know that:
4092 win is the innermost window containing the pointer
4093 (XTC says it has no child containing the pointer),
4094 win_x and win_y are the pointer's position in it
4095 (XTC did this the last time through), and
4096 parent_x and parent_y are the pointer's position in win's parent.
4097 (They are what win_x and win_y were when win was child.
4098 If win is the root window, it has no parent, and
4099 parent_{x,y} are invalid, but that's okay, because we'll
4100 never use them in that case.) */
4101
4102 #ifdef USE_GTK
4103 /* We don't wan't to know the innermost window. We
4104 want the edit window. */
4105 f1 = x_window_to_frame (dpyinfo, win);
4106 #else
4107 /* Is win one of our frames? */
4108 f1 = x_any_window_to_frame (dpyinfo, win);
4109 #endif
4110
4111 #ifdef USE_X_TOOLKIT
4112 /* If we end up with the menu bar window, say it's not
4113 on the frame. */
4114 if (f1 != NULL
4115 && f1->output_data.x->menubar_widget
4116 && win == XtWindow (f1->output_data.x->menubar_widget))
4117 f1 = NULL;
4118 #endif /* USE_X_TOOLKIT */
4119 }
4120
4121 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
4122 f1 = 0;
4123
4124 x_uncatch_errors ();
4125
4126 /* If not, is it one of our scroll bars? */
4127 if (! f1)
4128 {
4129 struct scroll_bar *bar;
4130
4131 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win, 2);
4132
4133 if (bar)
4134 {
4135 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4136 win_x = parent_x;
4137 win_y = parent_y;
4138 }
4139 }
4140
4141 if (f1 == 0 && insist > 0)
4142 f1 = SELECTED_FRAME ();
4143
4144 if (f1)
4145 {
4146 /* Ok, we found a frame. Store all the values.
4147 last_mouse_glyph is a rectangle used to reduce the
4148 generation of mouse events. To not miss any motion
4149 events, we must divide the frame into rectangles of the
4150 size of the smallest character that could be displayed
4151 on it, i.e. into the same rectangles that matrices on
4152 the frame are divided into. */
4153
4154 /* FIXME: what if F1 is not an X frame? */
4155 dpyinfo = FRAME_DISPLAY_INFO (f1);
4156 remember_mouse_glyph (f1, win_x, win_y, &dpyinfo->last_mouse_glyph);
4157 dpyinfo->last_mouse_glyph_frame = f1;
4158
4159 *bar_window = Qnil;
4160 *part = 0;
4161 *fp = f1;
4162 XSETINT (*x, win_x);
4163 XSETINT (*y, win_y);
4164 *timestamp = dpyinfo->last_mouse_movement_time;
4165 }
4166 }
4167 }
4168
4169 unblock_input ();
4170 }
4171
4172
4173 \f
4174 /***********************************************************************
4175 Scroll bars
4176 ***********************************************************************/
4177
4178 /* Scroll bar support. */
4179
4180 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
4181 manages it.
4182 This can be called in GC, so we have to make sure to strip off mark
4183 bits. */
4184
4185 static struct scroll_bar *
4186 x_window_to_scroll_bar (Display *display, Window window_id, int type)
4187 {
4188 Lisp_Object tail, frame;
4189
4190 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
4191 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
4192 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
4193
4194 FOR_EACH_FRAME (tail, frame)
4195 {
4196 Lisp_Object bar, condemned;
4197
4198 if (! FRAME_X_P (XFRAME (frame)))
4199 continue;
4200
4201 /* Scan this frame's scroll bar list for a scroll bar with the
4202 right window ID. */
4203 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
4204 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
4205 /* This trick allows us to search both the ordinary and
4206 condemned scroll bar lists with one loop. */
4207 ! NILP (bar) || (bar = condemned,
4208 condemned = Qnil,
4209 ! NILP (bar));
4210 bar = XSCROLL_BAR (bar)->next)
4211 if (XSCROLL_BAR (bar)->x_window == window_id
4212 && FRAME_X_DISPLAY (XFRAME (frame)) == display
4213 && (type = 2
4214 || (type == 1 && XSCROLL_BAR (bar)->horizontal)
4215 || (type == 0 && !XSCROLL_BAR (bar)->horizontal)))
4216 return XSCROLL_BAR (bar);
4217 }
4218
4219 return NULL;
4220 }
4221
4222
4223 #if defined USE_LUCID
4224
4225 /* Return the Lucid menu bar WINDOW is part of. Return null
4226 if WINDOW is not part of a menu bar. */
4227
4228 static Widget
4229 x_window_to_menu_bar (Window window)
4230 {
4231 Lisp_Object tail, frame;
4232
4233 FOR_EACH_FRAME (tail, frame)
4234 if (FRAME_X_P (XFRAME (frame)))
4235 {
4236 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
4237
4238 if (menu_bar && xlwmenu_window_p (menu_bar, window))
4239 return menu_bar;
4240 }
4241 return NULL;
4242 }
4243
4244 #endif /* USE_LUCID */
4245
4246 \f
4247 /************************************************************************
4248 Toolkit scroll bars
4249 ************************************************************************/
4250
4251 #ifdef USE_TOOLKIT_SCROLL_BARS
4252
4253 static void x_send_scroll_bar_event (Lisp_Object, int, int, int, bool);
4254
4255 /* Lisp window being scrolled. Set when starting to interact with
4256 a toolkit scroll bar, reset to nil when ending the interaction. */
4257
4258 static Lisp_Object window_being_scrolled;
4259
4260 /* Whether this is an Xaw with arrow-scrollbars. This should imply
4261 that movements of 1/20 of the screen size are mapped to up/down. */
4262
4263 #ifndef USE_GTK
4264 /* Id of action hook installed for scroll bars. */
4265
4266 static XtActionHookId action_hook_id;
4267 static XtActionHookId horizontal_action_hook_id;
4268
4269 static Boolean xaw3d_arrow_scroll;
4270
4271 /* Whether the drag scrolling maintains the mouse at the top of the
4272 thumb. If not, resizing the thumb needs to be done more carefully
4273 to avoid jerkiness. */
4274
4275 static Boolean xaw3d_pick_top;
4276
4277 /* Action hook installed via XtAppAddActionHook when toolkit scroll
4278 bars are used.. The hook is responsible for detecting when
4279 the user ends an interaction with the scroll bar, and generates
4280 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
4281
4282 static void
4283 xt_action_hook (Widget widget, XtPointer client_data, String action_name,
4284 XEvent *event, String *params, Cardinal *num_params)
4285 {
4286 int scroll_bar_p;
4287 const char *end_action;
4288
4289 #ifdef USE_MOTIF
4290 scroll_bar_p = XmIsScrollBar (widget);
4291 end_action = "Release";
4292 #else /* !USE_MOTIF i.e. use Xaw */
4293 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4294 end_action = "EndScroll";
4295 #endif /* USE_MOTIF */
4296
4297 if (scroll_bar_p
4298 && strcmp (action_name, end_action) == 0
4299 && WINDOWP (window_being_scrolled))
4300 {
4301 struct window *w;
4302 struct scroll_bar *bar;
4303
4304 x_send_scroll_bar_event (window_being_scrolled,
4305 scroll_bar_end_scroll, 0, 0, 0);
4306 w = XWINDOW (window_being_scrolled);
4307 bar = XSCROLL_BAR (w->vertical_scroll_bar);
4308
4309 if (bar->dragging != -1)
4310 {
4311 bar->dragging = -1;
4312 /* The thumb size is incorrect while dragging: fix it. */
4313 set_vertical_scroll_bar (w);
4314 }
4315 window_being_scrolled = Qnil;
4316 #if defined (USE_LUCID)
4317 bar->last_seen_part = scroll_bar_nowhere;
4318 #endif
4319 /* Xt timeouts no longer needed. */
4320 toolkit_scroll_bar_interaction = 0;
4321 }
4322 }
4323
4324
4325 static void
4326 xt_horizontal_action_hook (Widget widget, XtPointer client_data, String action_name,
4327 XEvent *event, String *params, Cardinal *num_params)
4328 {
4329 int scroll_bar_p;
4330 const char *end_action;
4331
4332 #ifdef USE_MOTIF
4333 scroll_bar_p = XmIsScrollBar (widget);
4334 end_action = "Release";
4335 #else /* !USE_MOTIF i.e. use Xaw */
4336 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4337 end_action = "EndScroll";
4338 #endif /* USE_MOTIF */
4339
4340 if (scroll_bar_p
4341 && strcmp (action_name, end_action) == 0
4342 && WINDOWP (window_being_scrolled))
4343 {
4344 struct window *w;
4345 struct scroll_bar *bar;
4346
4347 x_send_scroll_bar_event (window_being_scrolled,
4348 scroll_bar_end_scroll, 0, 0, 1);
4349 w = XWINDOW (window_being_scrolled);
4350 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
4351
4352 if (bar->dragging != -1)
4353 {
4354 bar->dragging = -1;
4355 /* The thumb size is incorrect while dragging: fix it. */
4356 set_horizontal_scroll_bar (w);
4357 }
4358 window_being_scrolled = Qnil;
4359 #if defined (USE_LUCID)
4360 bar->last_seen_part = scroll_bar_nowhere;
4361 #endif
4362 /* Xt timeouts no longer needed. */
4363 toolkit_scroll_bar_interaction = 0;
4364 }
4365 }
4366 #endif /* not USE_GTK */
4367
4368 /* Send a client message with message type Xatom_Scrollbar for a
4369 scroll action to the frame of WINDOW. PART is a value identifying
4370 the part of the scroll bar that was clicked on. PORTION is the
4371 amount to scroll of a whole of WHOLE. */
4372
4373 static void
4374 x_send_scroll_bar_event (Lisp_Object window, int part, int portion, int whole, bool horizontal)
4375 {
4376 XEvent event;
4377 XClientMessageEvent *ev = &event.xclient;
4378 struct window *w = XWINDOW (window);
4379 struct frame *f = XFRAME (w->frame);
4380 intptr_t iw = (intptr_t) w;
4381 enum { BITS_PER_INTPTR = CHAR_BIT * sizeof iw };
4382 verify (BITS_PER_INTPTR <= 64);
4383 int sign_shift = BITS_PER_INTPTR - 32;
4384
4385 block_input ();
4386
4387 /* Construct a ClientMessage event to send to the frame. */
4388 ev->type = ClientMessage;
4389 ev->message_type = (horizontal
4390 ? FRAME_DISPLAY_INFO (f)->Xatom_Horizontal_Scrollbar
4391 : FRAME_DISPLAY_INFO (f)->Xatom_Scrollbar);
4392 ev->display = FRAME_X_DISPLAY (f);
4393 ev->window = FRAME_X_WINDOW (f);
4394 ev->format = 32;
4395
4396 /* A 32-bit X client on a 64-bit X server can pass a window pointer
4397 as-is. A 64-bit client on a 32-bit X server is in trouble
4398 because a pointer does not fit and would be truncated while
4399 passing through the server. So use two slots and hope that X12
4400 will resolve such issues someday. */
4401 ev->data.l[0] = iw >> 31 >> 1;
4402 ev->data.l[1] = sign_shift <= 0 ? iw : iw << sign_shift >> sign_shift;
4403 ev->data.l[2] = part;
4404 ev->data.l[3] = portion;
4405 ev->data.l[4] = whole;
4406
4407 /* Make Xt timeouts work while the scroll bar is active. */
4408 #ifdef USE_X_TOOLKIT
4409 toolkit_scroll_bar_interaction = 1;
4410 x_activate_timeout_atimer ();
4411 #endif
4412
4413 /* Setting the event mask to zero means that the message will
4414 be sent to the client that created the window, and if that
4415 window no longer exists, no event will be sent. */
4416 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4417 unblock_input ();
4418 }
4419
4420
4421 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4422 in *IEVENT. */
4423
4424 static void
4425 x_scroll_bar_to_input_event (const XEvent *event,
4426 struct input_event *ievent)
4427 {
4428 const XClientMessageEvent *ev = &event->xclient;
4429 Lisp_Object window;
4430 struct window *w;
4431
4432 /* See the comment in the function above. */
4433 intptr_t iw0 = ev->data.l[0];
4434 intptr_t iw1 = ev->data.l[1];
4435 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
4436 w = (struct window *) iw;
4437
4438 XSETWINDOW (window, w);
4439
4440 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4441 ievent->frame_or_window = window;
4442 ievent->arg = Qnil;
4443 #ifdef USE_GTK
4444 ievent->timestamp = CurrentTime;
4445 #else
4446 ievent->timestamp =
4447 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
4448 #endif
4449 ievent->code = 0;
4450 ievent->part = ev->data.l[2];
4451 ievent->x = make_number (ev->data.l[3]);
4452 ievent->y = make_number (ev->data.l[4]);
4453 ievent->modifiers = 0;
4454 }
4455
4456 /* Transform a horizontal scroll bar ClientMessage EVENT to an Emacs
4457 input event in *IEVENT. */
4458
4459 static void
4460 x_horizontal_scroll_bar_to_input_event (const XEvent *event,
4461 struct input_event *ievent)
4462 {
4463 const XClientMessageEvent *ev = &event->xclient;
4464 Lisp_Object window;
4465 struct window *w;
4466
4467 /* See the comment in the function above. */
4468 intptr_t iw0 = ev->data.l[0];
4469 intptr_t iw1 = ev->data.l[1];
4470 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
4471 w = (struct window *) iw;
4472
4473 XSETWINDOW (window, w);
4474
4475 ievent->kind = HORIZONTAL_SCROLL_BAR_CLICK_EVENT;
4476 ievent->frame_or_window = window;
4477 ievent->arg = Qnil;
4478 #ifdef USE_GTK
4479 ievent->timestamp = CurrentTime;
4480 #else
4481 ievent->timestamp =
4482 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
4483 #endif
4484 ievent->code = 0;
4485 ievent->part = ev->data.l[2];
4486 ievent->x = make_number (ev->data.l[3]);
4487 ievent->y = make_number (ev->data.l[4]);
4488 ievent->modifiers = 0;
4489 }
4490
4491
4492 #ifdef USE_MOTIF
4493
4494 /* Minimum and maximum values used for Motif scroll bars. */
4495
4496 #define XM_SB_MAX 10000000
4497
4498 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4499 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4500 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4501
4502 static void
4503 xm_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4504 {
4505 struct scroll_bar *bar = client_data;
4506 XmScrollBarCallbackStruct *cs = call_data;
4507 int part = -1, whole = 0, portion = 0;
4508 int horizontal = bar->horizontal;
4509
4510 switch (cs->reason)
4511 {
4512 case XmCR_DECREMENT:
4513 bar->dragging = -1;
4514 part = horizontal ? scroll_bar_left_arrow : scroll_bar_up_arrow;
4515 break;
4516
4517 case XmCR_INCREMENT:
4518 bar->dragging = -1;
4519 part = horizontal ? scroll_bar_right_arrow : scroll_bar_down_arrow;
4520 break;
4521
4522 case XmCR_PAGE_DECREMENT:
4523 bar->dragging = -1;
4524 part = horizontal ? scroll_bar_before_handle : scroll_bar_above_handle;
4525 break;
4526
4527 case XmCR_PAGE_INCREMENT:
4528 bar->dragging = -1;
4529 part = horizontal ? scroll_bar_after_handle : scroll_bar_below_handle;
4530 break;
4531
4532 case XmCR_TO_TOP:
4533 bar->dragging = -1;
4534 part = horizontal ? scroll_bar_to_leftmost : scroll_bar_to_top;
4535 break;
4536
4537 case XmCR_TO_BOTTOM:
4538 bar->dragging = -1;
4539 part = horizontal ? scroll_bar_to_rightmost : scroll_bar_to_bottom;
4540 break;
4541
4542 case XmCR_DRAG:
4543 {
4544 int slider_size;
4545
4546 block_input ();
4547 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4548 unblock_input ();
4549
4550 if (horizontal)
4551 {
4552 portion = bar->whole * ((float)cs->value / XM_SB_MAX);
4553 whole = bar->whole * ((float)(XM_SB_MAX - slider_size) / XM_SB_MAX);
4554 portion = min (portion, whole);
4555 part = scroll_bar_horizontal_handle;
4556 }
4557 else
4558 {
4559 whole = XM_SB_MAX - slider_size;
4560 portion = min (cs->value, whole);
4561 part = scroll_bar_handle;
4562 }
4563
4564 bar->dragging = cs->value;
4565 }
4566 break;
4567
4568 case XmCR_VALUE_CHANGED:
4569 break;
4570 };
4571
4572 if (part >= 0)
4573 {
4574 window_being_scrolled = bar->window;
4575 x_send_scroll_bar_event (bar->window, part, portion, whole, bar->horizontal);
4576 }
4577 }
4578
4579 #elif defined USE_GTK
4580
4581 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4582 bar widget. DATA is a pointer to the scroll_bar structure. */
4583
4584 static gboolean
4585 xg_scroll_callback (GtkRange *range,
4586 GtkScrollType scroll,
4587 gdouble value,
4588 gpointer user_data)
4589 {
4590 struct scroll_bar *bar = user_data;
4591 int part = -1, whole = 0, portion = 0;
4592 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (range));
4593 struct frame *f = g_object_get_data (G_OBJECT (range), XG_FRAME_DATA);
4594
4595 if (xg_ignore_gtk_scrollbar) return FALSE;
4596
4597 switch (scroll)
4598 {
4599 case GTK_SCROLL_JUMP:
4600 /* Buttons 1 2 or 3 must be grabbed. */
4601 if (FRAME_DISPLAY_INFO (f)->grabbed != 0
4602 && FRAME_DISPLAY_INFO (f)->grabbed < (1 << 4))
4603 {
4604 if (bar->horizontal)
4605 {
4606 part = scroll_bar_horizontal_handle;
4607 whole = (int)(gtk_adjustment_get_upper (adj) -
4608 gtk_adjustment_get_page_size (adj));
4609 portion = min ((int)value, whole);
4610 bar->dragging = portion;
4611 }
4612 else
4613 {
4614 part = scroll_bar_handle;
4615 whole = gtk_adjustment_get_upper (adj) -
4616 gtk_adjustment_get_page_size (adj);
4617 portion = min ((int)value, whole);
4618 bar->dragging = portion;
4619 }
4620 }
4621 break;
4622 case GTK_SCROLL_STEP_BACKWARD:
4623 part = (bar->horizontal
4624 ? scroll_bar_left_arrow : scroll_bar_up_arrow);
4625 bar->dragging = -1;
4626 break;
4627 case GTK_SCROLL_STEP_FORWARD:
4628 part = (bar->horizontal
4629 ? scroll_bar_right_arrow : scroll_bar_down_arrow);
4630 bar->dragging = -1;
4631 break;
4632 case GTK_SCROLL_PAGE_BACKWARD:
4633 part = (bar->horizontal
4634 ? scroll_bar_before_handle : scroll_bar_above_handle);
4635 bar->dragging = -1;
4636 break;
4637 case GTK_SCROLL_PAGE_FORWARD:
4638 part = (bar->horizontal
4639 ? scroll_bar_after_handle : scroll_bar_below_handle);
4640 bar->dragging = -1;
4641 break;
4642 }
4643
4644 if (part >= 0)
4645 {
4646 window_being_scrolled = bar->window;
4647 x_send_scroll_bar_event (bar->window, part, portion, whole, bar->horizontal);
4648 }
4649
4650 return FALSE;
4651 }
4652
4653 /* Callback for button release. Sets dragging to -1 when dragging is done. */
4654
4655 static gboolean
4656 xg_end_scroll_callback (GtkWidget *widget,
4657 GdkEventButton *event,
4658 gpointer user_data)
4659 {
4660 struct scroll_bar *bar = user_data;
4661 bar->dragging = -1;
4662 if (WINDOWP (window_being_scrolled))
4663 {
4664 x_send_scroll_bar_event (window_being_scrolled,
4665 scroll_bar_end_scroll, 0, 0, bar->horizontal);
4666 window_being_scrolled = Qnil;
4667 }
4668
4669 return FALSE;
4670 }
4671
4672
4673 #else /* not USE_GTK and not USE_MOTIF */
4674
4675 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4676 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4677 scroll bar struct. CALL_DATA is a pointer to a float saying where
4678 the thumb is. */
4679
4680 static void
4681 xaw_jump_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4682 {
4683 struct scroll_bar *bar = client_data;
4684 float *top_addr = call_data;
4685 float top = *top_addr;
4686 float shown;
4687 int whole, portion, height, width;
4688 enum scroll_bar_part part;
4689 int horizontal = bar->horizontal;
4690
4691
4692 if (horizontal)
4693 {
4694 /* Get the size of the thumb, a value between 0 and 1. */
4695 block_input ();
4696 XtVaGetValues (widget, XtNshown, &shown, XtNwidth, &width, NULL);
4697 unblock_input ();
4698
4699 if (shown < 1)
4700 {
4701 whole = bar->whole - (shown * bar->whole);
4702 portion = min (top * bar->whole, whole);
4703 }
4704 else
4705 {
4706 whole = bar->whole;
4707 portion = 0;
4708 }
4709
4710 part = scroll_bar_horizontal_handle;
4711 }
4712 else
4713 {
4714 /* Get the size of the thumb, a value between 0 and 1. */
4715 block_input ();
4716 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4717 unblock_input ();
4718
4719 whole = 10000000;
4720 portion = shown < 1 ? top * whole : 0;
4721
4722 if (shown < 1 && (eabs (top + shown - 1) < 1.0f / height))
4723 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4724 the bottom, so we force the scrolling whenever we see that we're
4725 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4726 we try to ensure that we always stay two pixels away from the
4727 bottom). */
4728 part = scroll_bar_down_arrow;
4729 else
4730 part = scroll_bar_handle;
4731 }
4732
4733 window_being_scrolled = bar->window;
4734 bar->dragging = portion;
4735 bar->last_seen_part = part;
4736 x_send_scroll_bar_event (bar->window, part, portion, whole, bar->horizontal);
4737 }
4738
4739
4740 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4741 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4742 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4743 the scroll bar. CALL_DATA is an integer specifying the action that
4744 has taken place. Its magnitude is in the range 0..height of the
4745 scroll bar. Negative values mean scroll towards buffer start.
4746 Values < height of scroll bar mean line-wise movement. */
4747
4748 static void
4749 xaw_scroll_callback (Widget widget, XtPointer client_data, XtPointe