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