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