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