]> 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 (!dpyinfo->x_focus_frame
7069 || f == dpyinfo->x_focus_frame)
7070 {
7071 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
7072 if (! popup_activated ())
7073 #endif
7074 {
7075 if (ignore_next_mouse_click_timeout)
7076 {
7077 if (event.type == ButtonPress
7078 && (int)(event.xbutton.time - ignore_next_mouse_click_timeout) > 0)
7079 {
7080 ignore_next_mouse_click_timeout = 0;
7081 construct_mouse_click (&inev.ie, &event.xbutton, f);
7082 }
7083 if (event.type == ButtonRelease)
7084 ignore_next_mouse_click_timeout = 0;
7085 }
7086 else
7087 construct_mouse_click (&inev.ie, &event.xbutton, f);
7088 }
7089 }
7090 }
7091 else
7092 {
7093 struct scroll_bar *bar
7094 = x_window_to_scroll_bar (event.xbutton.display,
7095 event.xbutton.window);
7096
7097 #ifdef USE_TOOLKIT_SCROLL_BARS
7098 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
7099 scroll bars. */
7100 if (bar && event.xbutton.state & ControlMask)
7101 {
7102 x_scroll_bar_handle_click (bar, &event, &inev.ie);
7103 *finish = X_EVENT_DROP;
7104 }
7105 #else /* not USE_TOOLKIT_SCROLL_BARS */
7106 if (bar)
7107 x_scroll_bar_handle_click (bar, &event, &inev.ie);
7108 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7109 }
7110
7111 if (event.type == ButtonPress)
7112 {
7113 dpyinfo->grabbed |= (1 << event.xbutton.button);
7114 last_mouse_frame = f;
7115
7116 if (!tool_bar_p)
7117 last_tool_bar_item = -1;
7118 }
7119 else
7120 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
7121
7122 /* Ignore any mouse motion that happened before this event;
7123 any subsequent mouse-movement Emacs events should reflect
7124 only motion after the ButtonPress/Release. */
7125 if (f != 0)
7126 f->mouse_moved = 0;
7127
7128 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
7129 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
7130 /* For a down-event in the menu bar,
7131 don't pass it to Xt right now.
7132 Instead, save it away
7133 and we will pass it to Xt from kbd_buffer_get_event.
7134 That way, we can run some Lisp code first. */
7135 if (
7136 #ifdef USE_GTK
7137 ! popup_activated ()
7138 &&
7139 #endif
7140 f && event.type == ButtonPress
7141 /* Verify the event is really within the menu bar
7142 and not just sent to it due to grabbing. */
7143 && event.xbutton.x >= 0
7144 && event.xbutton.x < FRAME_PIXEL_WIDTH (f)
7145 && event.xbutton.y >= 0
7146 && event.xbutton.y < f->output_data.x->menubar_height
7147 && event.xbutton.same_screen)
7148 {
7149 SET_SAVED_BUTTON_EVENT;
7150 XSETFRAME (last_mouse_press_frame, f);
7151 #ifdef USE_GTK
7152 *finish = X_EVENT_DROP;
7153 #endif
7154 }
7155 else if (event.type == ButtonPress)
7156 {
7157 last_mouse_press_frame = Qnil;
7158 goto OTHER;
7159 }
7160
7161 #ifdef USE_MOTIF /* This should do not harm for Lucid,
7162 but I am trying to be cautious. */
7163 else if (event.type == ButtonRelease)
7164 {
7165 if (!NILP (last_mouse_press_frame))
7166 {
7167 f = XFRAME (last_mouse_press_frame);
7168 if (f->output_data.x)
7169 SET_SAVED_BUTTON_EVENT;
7170 }
7171 else
7172 goto OTHER;
7173 }
7174 #endif /* USE_MOTIF */
7175 else
7176 goto OTHER;
7177 #endif /* USE_X_TOOLKIT || USE_GTK */
7178 }
7179 break;
7180
7181 case CirculateNotify:
7182 goto OTHER;
7183
7184 case CirculateRequest:
7185 goto OTHER;
7186
7187 case VisibilityNotify:
7188 goto OTHER;
7189
7190 case MappingNotify:
7191 /* Someone has changed the keyboard mapping - update the
7192 local cache. */
7193 switch (event.xmapping.request)
7194 {
7195 case MappingModifier:
7196 x_find_modifier_meanings (dpyinfo);
7197 /* This is meant to fall through. */
7198 case MappingKeyboard:
7199 XRefreshKeyboardMapping (&event.xmapping);
7200 }
7201 goto OTHER;
7202
7203 default:
7204 OTHER:
7205 #ifdef USE_X_TOOLKIT
7206 BLOCK_INPUT;
7207 if (*finish != X_EVENT_DROP)
7208 XtDispatchEvent (&event);
7209 UNBLOCK_INPUT;
7210 #endif /* USE_X_TOOLKIT */
7211 break;
7212 }
7213
7214 done:
7215 if (inev.ie.kind != NO_EVENT)
7216 {
7217 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
7218 count++;
7219 }
7220
7221 if (do_help
7222 && !(hold_quit && hold_quit->kind != NO_EVENT))
7223 {
7224 Lisp_Object frame;
7225
7226 if (f)
7227 XSETFRAME (frame, f);
7228 else
7229 frame = Qnil;
7230
7231 if (do_help > 0)
7232 {
7233 any_help_event_p = 1;
7234 gen_help_event (help_echo_string, frame, help_echo_window,
7235 help_echo_object, help_echo_pos);
7236 }
7237 else
7238 {
7239 help_echo_string = Qnil;
7240 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
7241 }
7242 count++;
7243 }
7244
7245 *eventp = event;
7246 return count;
7247 }
7248
7249
7250 /* Handles the XEvent EVENT on display DISPLAY.
7251 This is used for event loops outside the normal event handling,
7252 i.e. looping while a popup menu or a dialog is posted.
7253
7254 Returns the value handle_one_xevent sets in the finish argument. */
7255 int
7256 x_dispatch_event (event, display)
7257 XEvent *event;
7258 Display *display;
7259 {
7260 struct x_display_info *dpyinfo;
7261 int finish = X_EVENT_NORMAL;
7262
7263 dpyinfo = x_display_info_for_display (display);
7264
7265 if (dpyinfo)
7266 handle_one_xevent (dpyinfo, event, &finish, 0);
7267
7268 return finish;
7269 }
7270
7271
7272 /* Read events coming from the X server.
7273 This routine is called by the SIGIO handler.
7274 We return as soon as there are no more events to be read.
7275
7276 We return the number of characters stored into the buffer,
7277 thus pretending to be `read'.
7278
7279 EXPECTED is nonzero if the caller knows input is available. */
7280
7281 static int
7282 XTread_socket (sd, expected, hold_quit)
7283 register int sd;
7284 int expected;
7285 struct input_event *hold_quit;
7286 {
7287 int count = 0;
7288 XEvent event;
7289 int event_found = 0;
7290 struct x_display_info *dpyinfo;
7291
7292 if (interrupt_input_blocked)
7293 {
7294 interrupt_input_pending = 1;
7295 return -1;
7296 }
7297
7298 interrupt_input_pending = 0;
7299 BLOCK_INPUT;
7300
7301 /* So people can tell when we have read the available input. */
7302 input_signal_count++;
7303
7304 ++handling_signal;
7305
7306 /* Find the display we are supposed to read input for.
7307 It's the one communicating on descriptor SD. */
7308 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
7309 {
7310 #if 0 /* This ought to be unnecessary; let's verify it. */
7311 #ifdef FIOSNBIO
7312 /* If available, Xlib uses FIOSNBIO to make the socket
7313 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
7314 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
7315 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7316 fcntl (dpyinfo->connection, F_SETFL, 0);
7317 #endif /* ! defined (FIOSNBIO) */
7318 #endif
7319
7320 #if 0 /* This code can't be made to work, with multiple displays,
7321 and appears not to be used on any system any more.
7322 Also keyboard.c doesn't turn O_NDELAY on and off
7323 for X connections. */
7324 #ifndef SIGIO
7325 #ifndef HAVE_SELECT
7326 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
7327 {
7328 extern int read_alarm_should_throw;
7329 read_alarm_should_throw = 1;
7330 XPeekEvent (dpyinfo->display, &event);
7331 read_alarm_should_throw = 0;
7332 }
7333 #endif /* HAVE_SELECT */
7334 #endif /* SIGIO */
7335 #endif
7336
7337 /* For debugging, this gives a way to fake an I/O error. */
7338 if (dpyinfo == XTread_socket_fake_io_error)
7339 {
7340 XTread_socket_fake_io_error = 0;
7341 x_io_error_quitter (dpyinfo->display);
7342 }
7343
7344 #ifdef HAVE_X_SM
7345 {
7346 struct input_event inev;
7347 BLOCK_INPUT;
7348 /* We don't need to EVENT_INIT (inev) here, as
7349 x_session_check_input copies an entire input_event. */
7350 if (x_session_check_input (&inev))
7351 {
7352 kbd_buffer_store_event_hold (&inev, hold_quit);
7353 count++;
7354 }
7355 UNBLOCK_INPUT;
7356 }
7357 #endif
7358
7359 #ifndef USE_GTK
7360 while (XPending (dpyinfo->display))
7361 {
7362 int finish;
7363
7364 XNextEvent (dpyinfo->display, &event);
7365
7366 #ifdef HAVE_X_I18N
7367 /* Filter events for the current X input method. */
7368 if (x_filter_event (dpyinfo, &event))
7369 break;
7370 #endif
7371 event_found = 1;
7372
7373 count += handle_one_xevent (dpyinfo, &event, &finish, hold_quit);
7374
7375 if (finish == X_EVENT_GOTO_OUT)
7376 goto out;
7377 }
7378 #endif /* not USE_GTK */
7379 }
7380
7381 #ifdef USE_GTK
7382
7383 /* For GTK we must use the GTK event loop. But XEvents gets passed
7384 to our filter function above, and then to the big event switch.
7385 We use a bunch of globals to communicate with our filter function,
7386 that is kind of ugly, but it works.
7387
7388 There is no way to do one display at the time, GTK just does events
7389 from all displays. */
7390
7391 while (gtk_events_pending ())
7392 {
7393 current_count = count;
7394 current_hold_quit = hold_quit;
7395
7396 gtk_main_iteration ();
7397
7398 count = current_count;
7399 current_count = -1;
7400 current_hold_quit = 0;
7401
7402 if (current_finish == X_EVENT_GOTO_OUT)
7403 break;
7404 }
7405 #endif /* USE_GTK */
7406
7407 out:;
7408
7409 /* On some systems, an X bug causes Emacs to get no more events
7410 when the window is destroyed. Detect that. (1994.) */
7411 if (! event_found)
7412 {
7413 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7414 One XNOOP in 100 loops will make Emacs terminate.
7415 B. Bretthauer, 1994 */
7416 x_noop_count++;
7417 if (x_noop_count >= 100)
7418 {
7419 x_noop_count=0;
7420
7421 if (next_noop_dpyinfo == 0)
7422 next_noop_dpyinfo = x_display_list;
7423
7424 XNoOp (next_noop_dpyinfo->display);
7425
7426 /* Each time we get here, cycle through the displays now open. */
7427 next_noop_dpyinfo = next_noop_dpyinfo->next;
7428 }
7429 }
7430
7431 /* If the focus was just given to an auto-raising frame,
7432 raise it now. */
7433 /* ??? This ought to be able to handle more than one such frame. */
7434 if (pending_autoraise_frame)
7435 {
7436 x_raise_frame (pending_autoraise_frame);
7437 pending_autoraise_frame = 0;
7438 }
7439
7440 --handling_signal;
7441 UNBLOCK_INPUT;
7442
7443 return count;
7444 }
7445
7446
7447
7448 \f
7449 /***********************************************************************
7450 Text Cursor
7451 ***********************************************************************/
7452
7453 /* Set clipping for output in glyph row ROW. W is the window in which
7454 we operate. GC is the graphics context to set clipping in.
7455
7456 ROW may be a text row or, e.g., a mode line. Text rows must be
7457 clipped to the interior of the window dedicated to text display,
7458 mode lines must be clipped to the whole window. */
7459
7460 static void
7461 x_clip_to_row (w, row, area, gc)
7462 struct window *w;
7463 struct glyph_row *row;
7464 int area;
7465 GC gc;
7466 {
7467 struct frame *f = XFRAME (WINDOW_FRAME (w));
7468 XRectangle clip_rect;
7469 int window_x, window_y, window_width;
7470
7471 window_box (w, area, &window_x, &window_y, &window_width, 0);
7472
7473 clip_rect.x = window_x;
7474 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
7475 clip_rect.y = max (clip_rect.y, window_y);
7476 clip_rect.width = window_width;
7477 clip_rect.height = row->visible_height;
7478
7479 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7480 }
7481
7482
7483 /* Draw a hollow box cursor on window W in glyph row ROW. */
7484
7485 static void
7486 x_draw_hollow_cursor (w, row)
7487 struct window *w;
7488 struct glyph_row *row;
7489 {
7490 struct frame *f = XFRAME (WINDOW_FRAME (w));
7491 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7492 Display *dpy = FRAME_X_DISPLAY (f);
7493 int x, y, wd, h;
7494 XGCValues xgcv;
7495 struct glyph *cursor_glyph;
7496 GC gc;
7497
7498 /* Get the glyph the cursor is on. If we can't tell because
7499 the current matrix is invalid or such, give up. */
7500 cursor_glyph = get_phys_cursor_glyph (w);
7501 if (cursor_glyph == NULL)
7502 return;
7503
7504 /* Compute frame-relative coordinates for phys cursor. */
7505 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
7506 wd = w->phys_cursor_width;
7507
7508 /* The foreground of cursor_gc is typically the same as the normal
7509 background color, which can cause the cursor box to be invisible. */
7510 xgcv.foreground = f->output_data.x->cursor_pixel;
7511 if (dpyinfo->scratch_cursor_gc)
7512 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7513 else
7514 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7515 GCForeground, &xgcv);
7516 gc = dpyinfo->scratch_cursor_gc;
7517
7518 /* Set clipping, draw the rectangle, and reset clipping again. */
7519 x_clip_to_row (w, row, TEXT_AREA, gc);
7520 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h - 1);
7521 XSetClipMask (dpy, gc, None);
7522 }
7523
7524
7525 /* Draw a bar cursor on window W in glyph row ROW.
7526
7527 Implementation note: One would like to draw a bar cursor with an
7528 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7529 Unfortunately, I didn't find a font yet that has this property set.
7530 --gerd. */
7531
7532 static void
7533 x_draw_bar_cursor (w, row, width, kind)
7534 struct window *w;
7535 struct glyph_row *row;
7536 int width;
7537 enum text_cursor_kinds kind;
7538 {
7539 struct frame *f = XFRAME (w->frame);
7540 struct glyph *cursor_glyph;
7541
7542 /* If cursor is out of bounds, don't draw garbage. This can happen
7543 in mini-buffer windows when switching between echo area glyphs
7544 and mini-buffer. */
7545 cursor_glyph = get_phys_cursor_glyph (w);
7546 if (cursor_glyph == NULL)
7547 return;
7548
7549 /* If on an image, draw like a normal cursor. That's usually better
7550 visible than drawing a bar, esp. if the image is large so that
7551 the bar might not be in the window. */
7552 if (cursor_glyph->type == IMAGE_GLYPH)
7553 {
7554 struct glyph_row *row;
7555 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7556 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
7557 }
7558 else
7559 {
7560 Display *dpy = FRAME_X_DISPLAY (f);
7561 Window window = FRAME_X_WINDOW (f);
7562 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7563 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7564 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7565 XGCValues xgcv;
7566
7567 /* If the glyph's background equals the color we normally draw
7568 the bar cursor in, the bar cursor in its normal color is
7569 invisible. Use the glyph's foreground color instead in this
7570 case, on the assumption that the glyph's colors are chosen so
7571 that the glyph is legible. */
7572 if (face->background == f->output_data.x->cursor_pixel)
7573 xgcv.background = xgcv.foreground = face->foreground;
7574 else
7575 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7576 xgcv.graphics_exposures = 0;
7577
7578 if (gc)
7579 XChangeGC (dpy, gc, mask, &xgcv);
7580 else
7581 {
7582 gc = XCreateGC (dpy, window, mask, &xgcv);
7583 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7584 }
7585
7586 if (width < 0)
7587 width = FRAME_CURSOR_WIDTH (f);
7588 width = min (cursor_glyph->pixel_width, width);
7589
7590 w->phys_cursor_width = width;
7591 x_clip_to_row (w, row, TEXT_AREA, gc);
7592
7593 if (kind == BAR_CURSOR)
7594 XFillRectangle (dpy, window, gc,
7595 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7596 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7597 width, row->height);
7598 else
7599 XFillRectangle (dpy, window, gc,
7600 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7601 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7602 row->height - width),
7603 cursor_glyph->pixel_width,
7604 width);
7605
7606 XSetClipMask (dpy, gc, None);
7607 }
7608 }
7609
7610
7611 /* RIF: Define cursor CURSOR on frame F. */
7612
7613 static void
7614 x_define_frame_cursor (f, cursor)
7615 struct frame *f;
7616 Cursor cursor;
7617 {
7618 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7619 }
7620
7621
7622 /* RIF: Clear area on frame F. */
7623
7624 static void
7625 x_clear_frame_area (f, x, y, width, height)
7626 struct frame *f;
7627 int x, y, width, height;
7628 {
7629 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7630 x, y, width, height, False);
7631 }
7632
7633
7634 /* RIF: Draw cursor on window W. */
7635
7636 static void
7637 x_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
7638 struct window *w;
7639 struct glyph_row *glyph_row;
7640 int x, y;
7641 int cursor_type, cursor_width;
7642 int on_p, active_p;
7643 {
7644 struct frame *f = XFRAME (WINDOW_FRAME (w));
7645
7646 if (on_p)
7647 {
7648 w->phys_cursor_type = cursor_type;
7649 w->phys_cursor_on_p = 1;
7650
7651 if (glyph_row->exact_window_width_line_p
7652 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
7653 {
7654 glyph_row->cursor_in_fringe_p = 1;
7655 draw_fringe_bitmap (w, glyph_row, 0);
7656 }
7657 else
7658 switch (cursor_type)
7659 {
7660 case HOLLOW_BOX_CURSOR:
7661 x_draw_hollow_cursor (w, glyph_row);
7662 break;
7663
7664 case FILLED_BOX_CURSOR:
7665 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7666 break;
7667
7668 case BAR_CURSOR:
7669 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7670 break;
7671
7672 case HBAR_CURSOR:
7673 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7674 break;
7675
7676 case NO_CURSOR:
7677 w->phys_cursor_width = 0;
7678 break;
7679
7680 default:
7681 abort ();
7682 }
7683
7684 #ifdef HAVE_X_I18N
7685 if (w == XWINDOW (f->selected_window))
7686 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7687 xic_set_preeditarea (w, x, y);
7688 #endif
7689 }
7690
7691 #ifndef XFlush
7692 if (updating_frame != f)
7693 XFlush (FRAME_X_DISPLAY (f));
7694 #endif
7695 }
7696
7697 \f
7698 /* Icons. */
7699
7700 /* Make the x-window of frame F use the gnu icon bitmap. */
7701
7702 int
7703 x_bitmap_icon (f, file)
7704 struct frame *f;
7705 Lisp_Object file;
7706 {
7707 int bitmap_id;
7708
7709 if (FRAME_X_WINDOW (f) == 0)
7710 return 1;
7711
7712 /* Free up our existing icon bitmap and mask if any. */
7713 if (f->output_data.x->icon_bitmap > 0)
7714 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7715 f->output_data.x->icon_bitmap = 0;
7716
7717 if (STRINGP (file))
7718 {
7719 #ifdef USE_GTK
7720 /* Use gtk_window_set_icon_from_file () if available,
7721 It's not restricted to bitmaps */
7722 if (xg_set_icon (f, file))
7723 return 0;
7724 #endif /* USE_GTK */
7725 bitmap_id = x_create_bitmap_from_file (f, file);
7726 x_create_bitmap_mask (f, bitmap_id);
7727 }
7728 else
7729 {
7730 /* Create the GNU bitmap and mask if necessary. */
7731 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7732 {
7733 int rc = -1;
7734
7735 #if defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
7736 #ifdef USE_GTK
7737 if (xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
7738 return 0;
7739 #else
7740 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
7741 if (rc != -1)
7742 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7743 #endif /* USE_GTK */
7744 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
7745
7746 /* If all else fails, use the (black and white) xbm image. */
7747 if (rc == -1)
7748 {
7749 rc = x_create_bitmap_from_data (f, gnu_xbm_bits,
7750 gnu_xbm_width, gnu_xbm_height);
7751 if (rc == -1)
7752 return 1;
7753
7754 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7755 x_create_bitmap_mask (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7756 }
7757 }
7758
7759 /* The first time we create the GNU bitmap and mask,
7760 this increments the ref-count one extra time.
7761 As a result, the GNU bitmap and mask are never freed.
7762 That way, we don't have to worry about allocating it again. */
7763 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7764
7765 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7766 }
7767
7768 x_wm_set_icon_pixmap (f, bitmap_id);
7769 f->output_data.x->icon_bitmap = bitmap_id;
7770
7771 return 0;
7772 }
7773
7774
7775 /* Make the x-window of frame F use a rectangle with text.
7776 Use ICON_NAME as the text. */
7777
7778 int
7779 x_text_icon (f, icon_name)
7780 struct frame *f;
7781 char *icon_name;
7782 {
7783 if (FRAME_X_WINDOW (f) == 0)
7784 return 1;
7785
7786 #ifdef HAVE_X11R4
7787 {
7788 XTextProperty text;
7789 text.value = (unsigned char *) icon_name;
7790 text.encoding = XA_STRING;
7791 text.format = 8;
7792 text.nitems = strlen (icon_name);
7793 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7794 }
7795 #else /* not HAVE_X11R4 */
7796 XSetIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), icon_name);
7797 #endif /* not HAVE_X11R4 */
7798
7799 if (f->output_data.x->icon_bitmap > 0)
7800 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7801 f->output_data.x->icon_bitmap = 0;
7802 x_wm_set_icon_pixmap (f, 0);
7803
7804 return 0;
7805 }
7806 \f
7807 #define X_ERROR_MESSAGE_SIZE 200
7808
7809 /* If non-nil, this should be a string.
7810 It means catch X errors and store the error message in this string.
7811
7812 The reason we use a stack is that x_catch_error/x_uncatch_error can
7813 be called from a signal handler.
7814 */
7815
7816 struct x_error_message_stack {
7817 char string[X_ERROR_MESSAGE_SIZE];
7818 Display *dpy;
7819 struct x_error_message_stack *prev;
7820 };
7821 static struct x_error_message_stack *x_error_message;
7822
7823 /* An X error handler which stores the error message in
7824 *x_error_message. This is called from x_error_handler if
7825 x_catch_errors is in effect. */
7826
7827 static void
7828 x_error_catcher (display, error)
7829 Display *display;
7830 XErrorEvent *error;
7831 {
7832 XGetErrorText (display, error->error_code,
7833 x_error_message->string,
7834 X_ERROR_MESSAGE_SIZE);
7835 }
7836
7837 /* Begin trapping X errors for display DPY. Actually we trap X errors
7838 for all displays, but DPY should be the display you are actually
7839 operating on.
7840
7841 After calling this function, X protocol errors no longer cause
7842 Emacs to exit; instead, they are recorded in the string
7843 stored in *x_error_message.
7844
7845 Calling x_check_errors signals an Emacs error if an X error has
7846 occurred since the last call to x_catch_errors or x_check_errors.
7847
7848 Calling x_uncatch_errors resumes the normal error handling. */
7849
7850 void x_check_errors ();
7851
7852 void
7853 x_catch_errors (dpy)
7854 Display *dpy;
7855 {
7856 struct x_error_message_stack *data = xmalloc (sizeof (*data));
7857
7858 /* Make sure any errors from previous requests have been dealt with. */
7859 XSync (dpy, False);
7860
7861 data->dpy = dpy;
7862 data->string[0] = 0;
7863 data->prev = x_error_message;
7864 x_error_message = data;
7865 }
7866
7867 /* Undo the last x_catch_errors call.
7868 DPY should be the display that was passed to x_catch_errors. */
7869
7870 void
7871 x_uncatch_errors ()
7872 {
7873 struct x_error_message_stack *tmp;
7874
7875 BLOCK_INPUT;
7876
7877 /* The display may have been closed before this function is called.
7878 Check if it is still open before calling XSync. */
7879 if (x_display_info_for_display (x_error_message->dpy) != 0)
7880 XSync (x_error_message->dpy, False);
7881
7882 tmp = x_error_message;
7883 x_error_message = x_error_message->prev;
7884 xfree (tmp);
7885 UNBLOCK_INPUT;
7886 }
7887
7888 /* If any X protocol errors have arrived since the last call to
7889 x_catch_errors or x_check_errors, signal an Emacs error using
7890 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7891
7892 void
7893 x_check_errors (dpy, format)
7894 Display *dpy;
7895 char *format;
7896 {
7897 /* Make sure to catch any errors incurred so far. */
7898 XSync (dpy, False);
7899
7900 if (x_error_message->string[0])
7901 {
7902 char string[X_ERROR_MESSAGE_SIZE];
7903 bcopy (x_error_message->string, string, X_ERROR_MESSAGE_SIZE);
7904 x_uncatch_errors ();
7905 error (format, string);
7906 }
7907 }
7908
7909 /* Nonzero if we had any X protocol errors
7910 since we did x_catch_errors on DPY. */
7911
7912 int
7913 x_had_errors_p (dpy)
7914 Display *dpy;
7915 {
7916 /* Make sure to catch any errors incurred so far. */
7917 XSync (dpy, False);
7918
7919 return x_error_message->string[0] != 0;
7920 }
7921
7922 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7923
7924 void
7925 x_clear_errors (dpy)
7926 Display *dpy;
7927 {
7928 x_error_message->string[0] = 0;
7929 }
7930
7931 /* Close off all unclosed x_catch_errors calls. */
7932
7933 void
7934 x_fully_uncatch_errors ()
7935 {
7936 while (x_error_message)
7937 x_uncatch_errors ();
7938 }
7939
7940 /* Nonzero if x_catch_errors has been done and not yet canceled. */
7941
7942 int
7943 x_catching_errors ()
7944 {
7945 return x_error_message != 0;
7946 }
7947
7948 #if 0
7949 static unsigned int x_wire_count;
7950 x_trace_wire ()
7951 {
7952 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7953 }
7954 #endif /* ! 0 */
7955
7956 \f
7957 /* Handle SIGPIPE, which can happen when the connection to a server
7958 simply goes away. SIGPIPE is handled by x_connection_signal.
7959 Don't need to do anything, because the write which caused the
7960 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
7961 which will do the appropriate cleanup for us. */
7962
7963 static SIGTYPE
7964 x_connection_signal (signalnum) /* If we don't have an argument, */
7965 int signalnum; /* some compilers complain in signal calls. */
7966 {
7967 #ifdef USG
7968 /* USG systems forget handlers when they are used;
7969 must reestablish each time */
7970 signal (signalnum, x_connection_signal);
7971 #endif /* USG */
7972 }
7973
7974 \f
7975 /************************************************************************
7976 Handling X errors
7977 ************************************************************************/
7978
7979 /* Error message passed to x_connection_closed. */
7980
7981 static char *error_msg;
7982
7983 /* Function installed as fatal_error_signal_hook in
7984 x_connection_closed. Print the X error message, and exit normally,
7985 instead of dumping core when XtCloseDisplay fails. */
7986
7987 static void
7988 x_fatal_error_signal ()
7989 {
7990 fprintf (stderr, "%s\n", error_msg);
7991 exit (70);
7992 }
7993
7994 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7995 the text of an error message that lead to the connection loss. */
7996
7997 static SIGTYPE
7998 x_connection_closed (dpy, error_message)
7999 Display *dpy;
8000 char *error_message;
8001 {
8002 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
8003 Lisp_Object frame, tail;
8004
8005 error_msg = (char *) alloca (strlen (error_message) + 1);
8006 strcpy (error_msg, error_message);
8007 handling_signal = 0;
8008
8009 /* Prevent being called recursively because of an error condition
8010 below. Otherwise, we might end up with printing ``can't find per
8011 display information'' in the recursive call instead of printing
8012 the original message here. */
8013 x_catch_errors (dpy);
8014
8015 /* We have to close the display to inform Xt that it doesn't
8016 exist anymore. If we don't, Xt will continue to wait for
8017 events from the display. As a consequence, a sequence of
8018
8019 M-x make-frame-on-display RET :1 RET
8020 ...kill the new frame, so that we get an IO error...
8021 M-x make-frame-on-display RET :1 RET
8022
8023 will indefinitely wait in Xt for events for display `:1', opened
8024 in the first class to make-frame-on-display.
8025
8026 Closing the display is reported to lead to a bus error on
8027 OpenWindows in certain situations. I suspect that is a bug
8028 in OpenWindows. I don't know how to cicumvent it here. */
8029
8030 #ifdef USE_X_TOOLKIT
8031 /* If DPYINFO is null, this means we didn't open the display
8032 in the first place, so don't try to close it. */
8033 if (dpyinfo)
8034 {
8035 extern void (*fatal_error_signal_hook) P_ ((void));
8036 fatal_error_signal_hook = x_fatal_error_signal;
8037 XtCloseDisplay (dpy);
8038 fatal_error_signal_hook = NULL;
8039 }
8040 #endif
8041
8042 #ifdef USE_GTK
8043 if (dpyinfo)
8044 xg_display_close (dpyinfo->display);
8045 #endif
8046
8047 /* Indicate that this display is dead. */
8048 if (dpyinfo)
8049 dpyinfo->display = 0;
8050
8051 /* First delete frames whose mini-buffers are on frames
8052 that are on the dead display. */
8053 FOR_EACH_FRAME (tail, frame)
8054 {
8055 Lisp_Object minibuf_frame;
8056 minibuf_frame
8057 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
8058 if (FRAME_X_P (XFRAME (frame))
8059 && FRAME_X_P (XFRAME (minibuf_frame))
8060 && ! EQ (frame, minibuf_frame)
8061 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
8062 Fdelete_frame (frame, Qt);
8063 }
8064
8065 /* Now delete all remaining frames on the dead display.
8066 We are now sure none of these is used as the mini-buffer
8067 for another frame that we need to delete. */
8068 FOR_EACH_FRAME (tail, frame)
8069 if (FRAME_X_P (XFRAME (frame))
8070 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
8071 {
8072 /* Set this to t so that Fdelete_frame won't get confused
8073 trying to find a replacement. */
8074 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
8075 Fdelete_frame (frame, Qt);
8076 }
8077
8078 if (dpyinfo)
8079 x_delete_display (dpyinfo);
8080
8081 x_uncatch_errors ();
8082
8083 if (x_display_list == 0)
8084 {
8085 fprintf (stderr, "%s\n", error_msg);
8086 shut_down_emacs (0, 0, Qnil);
8087 exit (70);
8088 }
8089
8090 /* Ordinary stack unwind doesn't deal with these. */
8091 #ifdef SIGIO
8092 sigunblock (sigmask (SIGIO));
8093 #endif
8094 sigunblock (sigmask (SIGALRM));
8095 TOTALLY_UNBLOCK_INPUT;
8096
8097 clear_waiting_for_input ();
8098 error ("%s", error_msg);
8099 }
8100
8101 /* We specifically use it before defining it, so that gcc doesn't inline it,
8102 otherwise gdb doesn't know how to properly put a breakpoint on it. */
8103 static void x_error_quitter P_ ((Display *, XErrorEvent *));
8104
8105 /* This is the first-level handler for X protocol errors.
8106 It calls x_error_quitter or x_error_catcher. */
8107
8108 static int
8109 x_error_handler (display, error)
8110 Display *display;
8111 XErrorEvent *error;
8112 {
8113 if (x_error_message)
8114 x_error_catcher (display, error);
8115 else
8116 x_error_quitter (display, error);
8117 return 0;
8118 }
8119
8120 /* This is the usual handler for X protocol errors.
8121 It kills all frames on the display that we got the error for.
8122 If that was the only one, it prints an error message and kills Emacs. */
8123
8124 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
8125
8126 #if __GNUC__ >= 3 /* On GCC 3.0 we might get a warning. */
8127 #define NO_INLINE __attribute__((noinline))
8128 #else
8129 #define NO_INLINE
8130 #endif
8131
8132 /* Some versions of GNU/Linux define noinline in their headers. */
8133
8134 #ifdef noinline
8135 #undef noinline
8136 #endif
8137
8138 /* On older GCC versions, just putting x_error_quitter
8139 after x_error_handler prevents inlining into the former. */
8140
8141 static void NO_INLINE
8142 x_error_quitter (display, error)
8143 Display *display;
8144 XErrorEvent *error;
8145 {
8146 char buf[256], buf1[356];
8147
8148 /* Ignore BadName errors. They can happen because of fonts
8149 or colors that are not defined. */
8150
8151 if (error->error_code == BadName)
8152 return;
8153
8154 /* Note that there is no real way portable across R3/R4 to get the
8155 original error handler. */
8156
8157 XGetErrorText (display, error->error_code, buf, sizeof (buf));
8158 sprintf (buf1, "X protocol error: %s on protocol request %d",
8159 buf, error->request_code);
8160 x_connection_closed (display, buf1);
8161 }
8162
8163
8164 /* This is the handler for X IO errors, always.
8165 It kills all frames on the display that we lost touch with.
8166 If that was the only one, it prints an error message and kills Emacs. */
8167
8168 static int
8169 x_io_error_quitter (display)
8170 Display *display;
8171 {
8172 char buf[256];
8173
8174 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
8175 x_connection_closed (display, buf);
8176 return 0;
8177 }
8178 \f
8179 /* Changing the font of the frame. */
8180
8181 /* Give frame F the font named FONTNAME as its default font, and
8182 return the full name of that font. FONTNAME may be a wildcard
8183 pattern; in that case, we choose some font that fits the pattern.
8184 The return value shows which font we chose. */
8185
8186 Lisp_Object
8187 x_new_font (f, fontname)
8188 struct frame *f;
8189 register char *fontname;
8190 {
8191 struct font_info *fontp
8192 = FS_LOAD_FONT (f, fontname);
8193
8194 if (!fontp)
8195 return Qnil;
8196
8197 if (FRAME_FONT (f) == (XFontStruct *) (fontp->font))
8198 /* This font is already set in frame F. There's nothing more to
8199 do. */
8200 return build_string (fontp->full_name);
8201
8202 FRAME_FONT (f) = (XFontStruct *) (fontp->font);
8203 FRAME_BASELINE_OFFSET (f) = fontp->baseline_offset;
8204 FRAME_FONTSET (f) = -1;
8205
8206 FRAME_COLUMN_WIDTH (f) = fontp->average_width;
8207 FRAME_SPACE_WIDTH (f) = fontp->space_width;
8208 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (FRAME_FONT (f));
8209
8210 compute_fringe_widths (f, 1);
8211
8212 /* Compute the scroll bar width in character columns. */
8213 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
8214 {
8215 int wid = FRAME_COLUMN_WIDTH (f);
8216 FRAME_CONFIG_SCROLL_BAR_COLS (f)
8217 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
8218 }
8219 else
8220 {
8221 int wid = FRAME_COLUMN_WIDTH (f);
8222 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
8223 }
8224
8225 /* Now make the frame display the given font. */
8226 if (FRAME_X_WINDOW (f) != 0)
8227 {
8228 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
8229 FRAME_FONT (f)->fid);
8230 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
8231 FRAME_FONT (f)->fid);
8232 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
8233 FRAME_FONT (f)->fid);
8234
8235 /* Don't change the size of a tip frame; there's no point in
8236 doing it because it's done in Fx_show_tip, and it leads to
8237 problems because the tip frame has no widget. */
8238 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8239 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
8240 }
8241
8242 return build_string (fontp->full_name);
8243 }
8244
8245 /* Give frame F the fontset named FONTSETNAME as its default fontset,
8246 and return the full name of that fontset. FONTSETNAME may be a
8247 wildcard pattern; in that case, we choose some fontset that fits
8248 the pattern. FONTSETNAME may be a font name for ASCII characters;
8249 in that case, we create a fontset from that font name.
8250
8251 The return value shows which fontset we chose.
8252 If FONTSETNAME specifies the default fontset, return Qt.
8253 If an ASCII font in the specified fontset can't be loaded, return
8254 Qnil. */
8255
8256 Lisp_Object
8257 x_new_fontset (f, fontsetname)
8258 struct frame *f;
8259 Lisp_Object fontsetname;
8260 {
8261 int fontset = fs_query_fontset (fontsetname, 0);
8262 Lisp_Object result;
8263
8264 if (fontset > 0 && f->output_data.x->fontset == fontset)
8265 /* This fontset is already set in frame F. There's nothing more
8266 to do. */
8267 return fontset_name (fontset);
8268 else if (fontset == 0)
8269 /* The default fontset can't be the default font. */
8270 return Qt;
8271
8272 if (fontset > 0)
8273 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
8274 else
8275 result = x_new_font (f, SDATA (fontsetname));
8276
8277 if (!STRINGP (result))
8278 /* Can't load ASCII font. */
8279 return Qnil;
8280
8281 if (fontset < 0)
8282 fontset = new_fontset_from_font_name (result);
8283
8284 /* Since x_new_font doesn't update any fontset information, do it now. */
8285 FRAME_FONTSET (f) = fontset;
8286
8287 #ifdef HAVE_X_I18N
8288 if (FRAME_XIC (f)
8289 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
8290 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
8291 #endif
8292
8293 return fontset_name (fontset);
8294 }
8295
8296 #ifdef USE_FONT_BACKEND
8297 Lisp_Object
8298 x_new_fontset2 (f, fontset, font_object)
8299 struct frame *f;
8300 int fontset;
8301 Lisp_Object font_object;
8302 {
8303 struct font *font = XSAVE_VALUE (font_object)->pointer;
8304
8305 if (FRAME_FONT_OBJECT (f) == font)
8306 /* This font is already set in frame F. There's nothing more to
8307 do. */
8308 return fontset_name (fontset);
8309
8310 BLOCK_INPUT;
8311
8312 FRAME_FONT_OBJECT (f) = font;
8313 FRAME_FONT (f) = font->font.font;
8314 FRAME_BASELINE_OFFSET (f) = font->font.baseline_offset;
8315 FRAME_FONTSET (f) = fontset;
8316
8317 FRAME_COLUMN_WIDTH (f) = font->font.average_width;
8318 FRAME_SPACE_WIDTH (f) = font->font.space_width;
8319 FRAME_LINE_HEIGHT (f) = font->font.height;
8320
8321 compute_fringe_widths (f, 1);
8322
8323 /* Compute the scroll bar width in character columns. */
8324 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
8325 {
8326 int wid = FRAME_COLUMN_WIDTH (f);
8327 FRAME_CONFIG_SCROLL_BAR_COLS (f)
8328 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid - 1) / wid;
8329 }
8330 else
8331 {
8332 int wid = FRAME_COLUMN_WIDTH (f);
8333 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
8334 }
8335
8336 /* Now make the frame display the given font. */
8337 if (FRAME_X_WINDOW (f) != 0)
8338 {
8339 /* Don't change the size of a tip frame; there's no point in
8340 doing it because it's done in Fx_show_tip, and it leads to
8341 problems because the tip frame has no widget. */
8342 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8343 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
8344 }
8345
8346 #ifdef HAVE_X_I18N
8347 if (FRAME_XIC (f)
8348 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
8349 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
8350 #endif
8351
8352 UNBLOCK_INPUT;
8353
8354 return fontset_name (fontset);
8355 }
8356 #endif /* USE_FONT_BACKEND */
8357
8358 \f
8359 /***********************************************************************
8360 X Input Methods
8361 ***********************************************************************/
8362
8363 #ifdef HAVE_X_I18N
8364
8365 #ifdef HAVE_X11R6
8366
8367 /* XIM destroy callback function, which is called whenever the
8368 connection to input method XIM dies. CLIENT_DATA contains a
8369 pointer to the x_display_info structure corresponding to XIM. */
8370
8371 static void
8372 xim_destroy_callback (xim, client_data, call_data)
8373 XIM xim;
8374 XPointer client_data;
8375 XPointer call_data;
8376 {
8377 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8378 Lisp_Object frame, tail;
8379
8380 BLOCK_INPUT;
8381
8382 /* No need to call XDestroyIC.. */
8383 FOR_EACH_FRAME (tail, frame)
8384 {
8385 struct frame *f = XFRAME (frame);
8386 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
8387 {
8388 FRAME_XIC (f) = NULL;
8389 xic_free_xfontset (f);
8390 }
8391 }
8392
8393 /* No need to call XCloseIM. */
8394 dpyinfo->xim = NULL;
8395 XFree (dpyinfo->xim_styles);
8396 UNBLOCK_INPUT;
8397 }
8398
8399 #endif /* HAVE_X11R6 */
8400
8401 #ifdef HAVE_X11R6
8402 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8403 extern char *XSetIMValues P_ ((XIM, ...));
8404 #endif
8405
8406 /* Open the connection to the XIM server on display DPYINFO.
8407 RESOURCE_NAME is the resource name Emacs uses. */
8408
8409 static void
8410 xim_open_dpy (dpyinfo, resource_name)
8411 struct x_display_info *dpyinfo;
8412 char *resource_name;
8413 {
8414 XIM xim;
8415
8416 #ifdef HAVE_XIM
8417 if (use_xim)
8418 {
8419 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8420 EMACS_CLASS);
8421 dpyinfo->xim = xim;
8422
8423 if (xim)
8424 {
8425 #ifdef HAVE_X11R6
8426 XIMCallback destroy;
8427 #endif
8428
8429 /* Get supported styles and XIM values. */
8430 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8431
8432 #ifdef HAVE_X11R6
8433 destroy.callback = xim_destroy_callback;
8434 destroy.client_data = (XPointer)dpyinfo;
8435 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8436 #endif
8437 }
8438 }
8439
8440 else
8441 #endif /* HAVE_XIM */
8442 dpyinfo->xim = NULL;
8443 }
8444
8445
8446 #ifdef HAVE_X11R6_XIM
8447
8448 struct xim_inst_t
8449 {
8450 struct x_display_info *dpyinfo;
8451 char *resource_name;
8452 };
8453
8454 /* XIM instantiate callback function, which is called whenever an XIM
8455 server is available. DISPLAY is the display of the XIM.
8456 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8457 when the callback was registered. */
8458
8459 static void
8460 xim_instantiate_callback (display, client_data, call_data)
8461 Display *display;
8462 XPointer client_data;
8463 XPointer call_data;
8464 {
8465 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8466 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8467
8468 /* We don't support multiple XIM connections. */
8469 if (dpyinfo->xim)
8470 return;
8471
8472 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8473
8474 /* Create XIC for the existing frames on the same display, as long
8475 as they have no XIC. */
8476 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8477 {
8478 Lisp_Object tail, frame;
8479
8480 BLOCK_INPUT;
8481 FOR_EACH_FRAME (tail, frame)
8482 {
8483 struct frame *f = XFRAME (frame);
8484
8485 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8486 if (FRAME_XIC (f) == NULL)
8487 {
8488 create_frame_xic (f);
8489 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8490 xic_set_statusarea (f);
8491 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8492 {
8493 struct window *w = XWINDOW (f->selected_window);
8494 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8495 }
8496 }
8497 }
8498
8499 UNBLOCK_INPUT;
8500 }
8501 }
8502
8503 #endif /* HAVE_X11R6_XIM */
8504
8505
8506 /* Open a connection to the XIM server on display DPYINFO.
8507 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8508 connection only at the first time. On X11R6, open the connection
8509 in the XIM instantiate callback function. */
8510
8511 static void
8512 xim_initialize (dpyinfo, resource_name)
8513 struct x_display_info *dpyinfo;
8514 char *resource_name;
8515 {
8516 #ifdef HAVE_XIM
8517 if (use_xim)
8518 {
8519 #ifdef HAVE_X11R6_XIM
8520 struct xim_inst_t *xim_inst;
8521 int len;
8522
8523 dpyinfo->xim = NULL;
8524 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8525 xim_inst->dpyinfo = dpyinfo;
8526 len = strlen (resource_name);
8527 xim_inst->resource_name = (char *) xmalloc (len + 1);
8528 bcopy (resource_name, xim_inst->resource_name, len + 1);
8529 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8530 resource_name, EMACS_CLASS,
8531 xim_instantiate_callback,
8532 /* This is XPointer in XFree86
8533 but (XPointer *) on Tru64, at
8534 least, hence the configure test. */
8535 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8536 #else /* not HAVE_X11R6_XIM */
8537 dpyinfo->xim = NULL;
8538 xim_open_dpy (dpyinfo, resource_name);
8539 #endif /* not HAVE_X11R6_XIM */
8540
8541 }
8542 else
8543 #endif /* HAVE_XIM */
8544 dpyinfo->xim = NULL;
8545 }
8546
8547
8548 /* Close the connection to the XIM server on display DPYINFO. */
8549
8550 static void
8551 xim_close_dpy (dpyinfo)
8552 struct x_display_info *dpyinfo;
8553 {
8554 #ifdef HAVE_XIM
8555 if (use_xim)
8556 {
8557 #ifdef HAVE_X11R6_XIM
8558 if (dpyinfo->display)
8559 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8560 NULL, EMACS_CLASS,
8561 xim_instantiate_callback, NULL);
8562 #endif /* not HAVE_X11R6_XIM */
8563 if (dpyinfo->display)
8564 XCloseIM (dpyinfo->xim);
8565 dpyinfo->xim = NULL;
8566 XFree (dpyinfo->xim_styles);
8567 }
8568 #endif /* HAVE_XIM */
8569 }
8570
8571 #endif /* not HAVE_X11R6_XIM */
8572
8573
8574 \f
8575 /* Calculate the absolute position in frame F
8576 from its current recorded position values and gravity. */
8577
8578 void
8579 x_calc_absolute_position (f)
8580 struct frame *f;
8581 {
8582 int flags = f->size_hint_flags;
8583
8584 /* We have nothing to do if the current position
8585 is already for the top-left corner. */
8586 if (! ((flags & XNegative) || (flags & YNegative)))
8587 return;
8588
8589 /* Treat negative positions as relative to the leftmost bottommost
8590 position that fits on the screen. */
8591 if (flags & XNegative)
8592 f->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
8593 - FRAME_PIXEL_WIDTH (f) + f->left_pos);
8594
8595 {
8596 int height = FRAME_PIXEL_HEIGHT (f);
8597
8598 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8599 /* Something is fishy here. When using Motif, starting Emacs with
8600 `-g -0-0', the frame appears too low by a few pixels.
8601
8602 This seems to be so because initially, while Emacs is starting,
8603 the column widget's height and the frame's pixel height are
8604 different. The column widget's height is the right one. In
8605 later invocations, when Emacs is up, the frame's pixel height
8606 is right, though.
8607
8608 It's not obvious where the initial small difference comes from.
8609 2000-12-01, gerd. */
8610
8611 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8612 #endif
8613
8614 if (flags & YNegative)
8615 f->top_pos = (FRAME_X_DISPLAY_INFO (f)->height - height + f->top_pos);
8616 }
8617
8618 /* The left_pos and top_pos
8619 are now relative to the top and left screen edges,
8620 so the flags should correspond. */
8621 f->size_hint_flags &= ~ (XNegative | YNegative);
8622 }
8623
8624 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8625 to really change the position, and 0 when calling from
8626 x_make_frame_visible (in that case, XOFF and YOFF are the current
8627 position values). It is -1 when calling from x_set_frame_parameters,
8628 which means, do adjust for borders but don't change the gravity. */
8629
8630 void
8631 x_set_offset (f, xoff, yoff, change_gravity)
8632 struct frame *f;
8633 register int xoff, yoff;
8634 int change_gravity;
8635 {
8636 int modified_top, modified_left;
8637
8638 if (change_gravity != 0)
8639 {
8640 FRAME_X_OUTPUT (f)->left_before_move = f->left_pos;
8641 FRAME_X_OUTPUT (f)->top_before_move = f->top_pos;
8642
8643 f->top_pos = yoff;
8644 f->left_pos = xoff;
8645 f->size_hint_flags &= ~ (XNegative | YNegative);
8646 if (xoff < 0)
8647 f->size_hint_flags |= XNegative;
8648 if (yoff < 0)
8649 f->size_hint_flags |= YNegative;
8650 f->win_gravity = NorthWestGravity;
8651 }
8652 x_calc_absolute_position (f);
8653
8654 BLOCK_INPUT;
8655 x_wm_set_size_hint (f, (long) 0, 0);
8656
8657 modified_left = f->left_pos;
8658 modified_top = f->top_pos;
8659
8660 if (change_gravity != 0 && FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8661 {
8662 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8663 than the WM decorations. So we use the calculated offset instead
8664 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8665 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8666 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8667 }
8668
8669 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8670 modified_left, modified_top);
8671
8672 x_sync_with_move (f, f->left_pos, f->top_pos,
8673 FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8674 ? 1 : 0);
8675
8676 /* change_gravity is non-zero when this function is called from Lisp to
8677 programmatically move a frame. In that case, we call
8678 x_check_expected_move to discover if we have a "Type A" or "Type B"
8679 window manager, and, for a "Type A" window manager, adjust the position
8680 of the frame.
8681
8682 We call x_check_expected_move if a programmatic move occurred, and
8683 either the window manager type (A/B) is unknown or it is Type A but we
8684 need to compute the top/left offset adjustment for this frame. */
8685
8686 if (change_gravity != 0 &&
8687 (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8688 || (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
8689 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
8690 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
8691 x_check_expected_move (f, modified_left, modified_top);
8692
8693 UNBLOCK_INPUT;
8694 }
8695
8696 /* Return non-zero if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
8697 on the root window for frame F contains ATOMNAME.
8698 This is how a WM check shall be done according to the Window Manager
8699 Specification/Extended Window Manager Hints at
8700 http://freedesktop.org/wiki/Standards_2fwm_2dspec. */
8701
8702 static int
8703 wm_supports (f, atomname)
8704 struct frame *f;
8705 const char *atomname;
8706 {
8707 Atom actual_type;
8708 unsigned long actual_size, bytes_remaining;
8709 int i, rc, actual_format;
8710 Atom prop_atom;
8711 Window wmcheck_window;
8712 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8713 Window target_window = dpyinfo->root_window;
8714 long max_len = 65536;
8715 Display *dpy = FRAME_X_DISPLAY (f);
8716 unsigned char *tmp_data = NULL;
8717 Atom target_type = XA_WINDOW;
8718 Atom want_atom;
8719
8720 BLOCK_INPUT;
8721
8722 prop_atom = XInternAtom (dpy, "_NET_SUPPORTING_WM_CHECK", False);
8723
8724 x_catch_errors (dpy);
8725 rc = XGetWindowProperty (dpy, target_window,
8726 prop_atom, 0, max_len, False, target_type,
8727 &actual_type, &actual_format, &actual_size,
8728 &bytes_remaining, &tmp_data);
8729
8730 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
8731 {
8732 if (tmp_data) XFree (tmp_data);
8733 x_uncatch_errors ();
8734 UNBLOCK_INPUT;
8735 return 0;
8736 }
8737
8738 wmcheck_window = *(Window *) tmp_data;
8739 XFree (tmp_data);
8740
8741 /* Check if window exists. */
8742 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
8743 x_sync (f);
8744 if (x_had_errors_p (dpy))
8745 {
8746 x_uncatch_errors ();
8747 UNBLOCK_INPUT;
8748 return 0;
8749 }
8750
8751 if (dpyinfo->net_supported_window != wmcheck_window)
8752 {
8753 /* Window changed, reload atoms */
8754 if (dpyinfo->net_supported_atoms != NULL)
8755 XFree (dpyinfo->net_supported_atoms);
8756 dpyinfo->net_supported_atoms = NULL;
8757 dpyinfo->nr_net_supported_atoms = 0;
8758 dpyinfo->net_supported_window = 0;
8759
8760 target_type = XA_ATOM;
8761 prop_atom = XInternAtom (dpy, "_NET_SUPPORTED", False);
8762 tmp_data = NULL;
8763 rc = XGetWindowProperty (dpy, target_window,
8764 prop_atom, 0, max_len, False, target_type,
8765 &actual_type, &actual_format, &actual_size,
8766 &bytes_remaining, &tmp_data);
8767
8768 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
8769 {
8770 if (tmp_data) XFree (tmp_data);
8771 x_uncatch_errors ();
8772 UNBLOCK_INPUT;
8773 return 0;
8774 }
8775
8776 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
8777 dpyinfo->nr_net_supported_atoms = actual_size;
8778 dpyinfo->net_supported_window = wmcheck_window;
8779 }
8780
8781 rc = 0;
8782 want_atom = XInternAtom (dpy, atomname, False);
8783
8784 for (i = 0; rc == 0 && i < dpyinfo->nr_net_supported_atoms; ++i)
8785 rc = dpyinfo->net_supported_atoms[i] == want_atom;
8786
8787 x_uncatch_errors ();
8788 UNBLOCK_INPUT;
8789
8790 return rc;
8791 }
8792
8793 /* Do fullscreen as specified in extended window manager hints */
8794
8795 static int
8796 do_ewmh_fullscreen (f)
8797 struct frame *f;
8798 {
8799 int have_net_atom = wm_supports (f, "_NET_WM_STATE");
8800
8801 /* Some window managers don't say they support _NET_WM_STATE, but they do say
8802 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
8803 if (!have_net_atom)
8804 have_net_atom = wm_supports (f, "_NET_WM_STATE_FULLSCREEN");
8805
8806 if (have_net_atom)
8807 {
8808 Lisp_Object frame;
8809 const char *atom = "_NET_WM_STATE";
8810 const char *fs = "_NET_WM_STATE_FULLSCREEN";
8811 const char *fw = "_NET_WM_STATE_MAXIMIZED_HORZ";
8812 const char *fh = "_NET_WM_STATE_MAXIMIZED_VERT";
8813 const char *what = NULL;
8814
8815 XSETFRAME (frame, f);
8816
8817 /* If there are _NET_ atoms we assume we have extended window manager
8818 hints. */
8819 switch (f->want_fullscreen)
8820 {
8821 case FULLSCREEN_BOTH:
8822 what = fs;
8823 break;
8824 case FULLSCREEN_WIDTH:
8825 what = fw;
8826 break;
8827 case FULLSCREEN_HEIGHT:
8828 what = fh;
8829 break;
8830 }
8831
8832 if (what != NULL && !wm_supports (f, what)) return 0;
8833
8834
8835 Fx_send_client_event (frame, make_number (0), frame,
8836 make_unibyte_string (atom, strlen (atom)),
8837 make_number (32),
8838 Fcons (make_number (0), /* Remove */
8839 Fcons
8840 (make_unibyte_string (fs,
8841 strlen (fs)),
8842 Qnil)));
8843 Fx_send_client_event (frame, make_number (0), frame,
8844 make_unibyte_string (atom, strlen (atom)),
8845 make_number (32),
8846 Fcons (make_number (0), /* Remove */
8847 Fcons
8848 (make_unibyte_string (fh,
8849 strlen (fh)),
8850 Qnil)));
8851 Fx_send_client_event (frame, make_number (0), frame,
8852 make_unibyte_string (atom, strlen (atom)),
8853 make_number (32),
8854 Fcons (make_number (0), /* Remove */
8855 Fcons
8856 (make_unibyte_string (fw,
8857 strlen (fw)),
8858 Qnil)));
8859 f->want_fullscreen = FULLSCREEN_NONE;
8860 if (what != NULL)
8861 Fx_send_client_event (frame, make_number (0), frame,
8862 make_unibyte_string (atom, strlen (atom)),
8863 make_number (32),
8864 Fcons (make_number (1), /* Add */
8865 Fcons
8866 (make_unibyte_string (what,
8867 strlen (what)),
8868 Qnil)));
8869 }
8870
8871 return have_net_atom;
8872 }
8873
8874 static void
8875 XTfullscreen_hook (f)
8876 FRAME_PTR f;
8877 {
8878 if (f->async_visible)
8879 {
8880 BLOCK_INPUT;
8881 do_ewmh_fullscreen (f);
8882 x_sync (f);
8883 UNBLOCK_INPUT;
8884 }
8885 }
8886
8887
8888 /* Check if we need to resize the frame due to a fullscreen request.
8889 If so needed, resize the frame. */
8890 static void
8891 x_check_fullscreen (f)
8892 struct frame *f;
8893 {
8894 if (f->want_fullscreen & FULLSCREEN_BOTH)
8895 {
8896 int width, height, ign;
8897
8898 if (do_ewmh_fullscreen (f))
8899 return;
8900
8901 x_real_positions (f, &f->left_pos, &f->top_pos);
8902
8903 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
8904
8905 /* We do not need to move the window, it shall be taken care of
8906 when setting WM manager hints.
8907 If the frame is visible already, the position is checked by
8908 x_check_expected_move. */
8909 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
8910 {
8911 change_frame_size (f, height, width, 0, 1, 0);
8912 SET_FRAME_GARBAGED (f);
8913 cancel_mouse_face (f);
8914
8915 /* Wait for the change of frame size to occur */
8916 f->want_fullscreen |= FULLSCREEN_WAIT;
8917 }
8918 }
8919 }
8920
8921 /* This function is called by x_set_offset to determine whether the window
8922 manager interfered with the positioning of the frame. Type A window
8923 managers position the surrounding window manager decorations a small
8924 amount above and left of the user-supplied position. Type B window
8925 managers position the surrounding window manager decorations at the
8926 user-specified position. If we detect a Type A window manager, we
8927 compensate by moving the window right and down by the proper amount. */
8928
8929 static void
8930 x_check_expected_move (f, expected_left, expected_top)
8931 struct frame *f;
8932 int expected_left;
8933 int expected_top;
8934 {
8935 int current_left = 0, current_top = 0;
8936
8937 /* x_real_positions returns the left and top offsets of the outermost
8938 window manager window around the frame. */
8939
8940 x_real_positions (f, &current_left, &current_top);
8941
8942 if (current_left != expected_left || current_top != expected_top)
8943 {
8944 /* It's a "Type A" window manager. */
8945
8946 int adjusted_left;
8947 int adjusted_top;
8948
8949 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
8950 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
8951 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
8952
8953 /* Now fix the mispositioned frame's location. */
8954
8955 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
8956 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
8957
8958 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8959 adjusted_left, adjusted_top);
8960
8961 x_sync_with_move (f, expected_left, expected_top, 0);
8962 }
8963 else
8964 /* It's a "Type B" window manager. We don't have to adjust the
8965 frame's position. */
8966
8967 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
8968 }
8969
8970
8971 /* Wait for XGetGeometry to return up-to-date position information for a
8972 recently-moved frame. Call this immediately after calling XMoveWindow.
8973 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
8974 frame has been moved to, so we use a fuzzy position comparison instead
8975 of an exact comparison. */
8976
8977 static void
8978 x_sync_with_move (f, left, top, fuzzy)
8979 struct frame *f;
8980 int left, top, fuzzy;
8981 {
8982 int count = 0;
8983
8984 while (count++ < 50)
8985 {
8986 int current_left = 0, current_top = 0;
8987
8988 /* In theory, this call to XSync only needs to happen once, but in
8989 practice, it doesn't seem to work, hence the need for the surrounding
8990 loop. */
8991
8992 XSync (FRAME_X_DISPLAY (f), False);
8993 x_real_positions (f, &current_left, &current_top);
8994
8995 if (fuzzy)
8996 {
8997 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
8998 pixels. */
8999
9000 if (abs (current_left - left) <= 10 && abs (current_top - top) <= 40)
9001 return;
9002 }
9003 else if (current_left == left && current_top == top)
9004 return;
9005 }
9006
9007 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
9008 will then return up-to-date position info. */
9009
9010 wait_reading_process_output (0, 500000, 0, 0, Qnil, NULL, 0);
9011 }
9012
9013
9014 /* Change the size of frame F's X window to COLS/ROWS in the case F
9015 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
9016 top-left-corner window gravity for this size change and subsequent
9017 size changes. Otherwise we leave the window gravity unchanged. */
9018
9019 static void
9020 x_set_window_size_1 (f, change_gravity, cols, rows)
9021 struct frame *f;
9022 int change_gravity;
9023 int cols, rows;
9024 {
9025 int pixelwidth, pixelheight;
9026
9027 check_frame_size (f, &rows, &cols);
9028 f->scroll_bar_actual_width
9029 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
9030 ? 0
9031 : FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0
9032 ? FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
9033 : (FRAME_CONFIG_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f)));
9034
9035 compute_fringe_widths (f, 0);
9036
9037 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
9038 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
9039
9040 f->win_gravity = NorthWestGravity;
9041 x_wm_set_size_hint (f, (long) 0, 0);
9042
9043 XSync (FRAME_X_DISPLAY (f), False);
9044 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9045 pixelwidth, pixelheight);
9046
9047 /* Now, strictly speaking, we can't be sure that this is accurate,
9048 but the window manager will get around to dealing with the size
9049 change request eventually, and we'll hear how it went when the
9050 ConfigureNotify event gets here.
9051
9052 We could just not bother storing any of this information here,
9053 and let the ConfigureNotify event set everything up, but that
9054 might be kind of confusing to the Lisp code, since size changes
9055 wouldn't be reported in the frame parameters until some random
9056 point in the future when the ConfigureNotify event arrives.
9057
9058 We pass 1 for DELAY since we can't run Lisp code inside of
9059 a BLOCK_INPUT. */
9060 change_frame_size (f, rows, cols, 0, 1, 0);
9061 FRAME_PIXEL_WIDTH (f) = pixelwidth;
9062 FRAME_PIXEL_HEIGHT (f) = pixelheight;
9063
9064 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
9065 receive in the ConfigureNotify event; if we get what we asked
9066 for, then the event won't cause the screen to become garbaged, so
9067 we have to make sure to do it here. */
9068 SET_FRAME_GARBAGED (f);
9069
9070 XFlush (FRAME_X_DISPLAY (f));
9071 }
9072
9073
9074 /* Call this to change the size of frame F's x-window.
9075 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
9076 for this size change and subsequent size changes.
9077 Otherwise we leave the window gravity unchanged. */
9078
9079 void
9080 x_set_window_size (f, change_gravity, cols, rows)
9081 struct frame *f;
9082 int change_gravity;
9083 int cols, rows;
9084 {
9085 BLOCK_INPUT;
9086
9087 #ifdef USE_GTK
9088 if (FRAME_GTK_WIDGET (f))
9089 xg_frame_set_char_size (f, cols, rows);
9090 else
9091 x_set_window_size_1 (f, change_gravity, cols, rows);
9092 #elif USE_X_TOOLKIT
9093
9094 if (f->output_data.x->widget != NULL)
9095 {
9096 /* The x and y position of the widget is clobbered by the
9097 call to XtSetValues within EmacsFrameSetCharSize.
9098 This is a real kludge, but I don't understand Xt so I can't
9099 figure out a correct fix. Can anyone else tell me? -- rms. */
9100 int xpos = f->output_data.x->widget->core.x;
9101 int ypos = f->output_data.x->widget->core.y;
9102 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
9103 f->output_data.x->widget->core.x = xpos;
9104 f->output_data.x->widget->core.y = ypos;
9105 }
9106 else
9107 x_set_window_size_1 (f, change_gravity, cols, rows);
9108
9109 #else /* not USE_X_TOOLKIT */
9110
9111 x_set_window_size_1 (f, change_gravity, cols, rows);
9112
9113 #endif /* not USE_X_TOOLKIT */
9114
9115 /* If cursor was outside the new size, mark it as off. */
9116 mark_window_cursors_off (XWINDOW (f->root_window));
9117
9118 /* Clear out any recollection of where the mouse highlighting was,
9119 since it might be in a place that's outside the new frame size.
9120 Actually checking whether it is outside is a pain in the neck,
9121 so don't try--just let the highlighting be done afresh with new size. */
9122 cancel_mouse_face (f);
9123
9124 UNBLOCK_INPUT;
9125 }
9126 \f
9127 /* Mouse warping. */
9128
9129 void
9130 x_set_mouse_position (f, x, y)
9131 struct frame *f;
9132 int x, y;
9133 {
9134 int pix_x, pix_y;
9135
9136 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
9137 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
9138
9139 if (pix_x < 0) pix_x = 0;
9140 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
9141
9142 if (pix_y < 0) pix_y = 0;
9143 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
9144
9145 BLOCK_INPUT;
9146
9147 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
9148 0, 0, 0, 0, pix_x, pix_y);
9149 UNBLOCK_INPUT;
9150 }
9151
9152 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
9153
9154 void
9155 x_set_mouse_pixel_position (f, pix_x, pix_y)
9156 struct frame *f;
9157 int pix_x, pix_y;
9158 {
9159 BLOCK_INPUT;
9160
9161 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
9162 0, 0, 0, 0, pix_x, pix_y);
9163 UNBLOCK_INPUT;
9164 }
9165 \f
9166 /* focus shifting, raising and lowering. */
9167
9168 void
9169 x_focus_on_frame (f)
9170 struct frame *f;
9171 {
9172 #if 0 /* This proves to be unpleasant. */
9173 x_raise_frame (f);
9174 #endif
9175 #if 0
9176 /* I don't think that the ICCCM allows programs to do things like this
9177 without the interaction of the window manager. Whatever you end up
9178 doing with this code, do it to x_unfocus_frame too. */
9179 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9180 RevertToPointerRoot, CurrentTime);
9181 #endif /* ! 0 */
9182 }
9183
9184 void
9185 x_unfocus_frame (f)
9186 struct frame *f;
9187 {
9188 #if 0
9189 /* Look at the remarks in x_focus_on_frame. */
9190 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
9191 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
9192 RevertToPointerRoot, CurrentTime);
9193 #endif /* ! 0 */
9194 }
9195
9196 /* Raise frame F. */
9197
9198 void
9199 x_raise_frame (f)
9200 struct frame *f;
9201 {
9202 BLOCK_INPUT;
9203 if (f->async_visible)
9204 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9205
9206 XFlush (FRAME_X_DISPLAY (f));
9207 UNBLOCK_INPUT;
9208 }
9209
9210 /* Lower frame F. */
9211
9212 void
9213 x_lower_frame (f)
9214 struct frame *f;
9215 {
9216 if (f->async_visible)
9217 {
9218 BLOCK_INPUT;
9219 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9220 XFlush (FRAME_X_DISPLAY (f));
9221 UNBLOCK_INPUT;
9222 }
9223 }
9224
9225 /* Activate frame with Extended Window Manager Hints */
9226
9227 void
9228 x_ewmh_activate_frame (f)
9229 FRAME_PTR f;
9230 {
9231 /* See Window Manager Specification/Extended Window Manager Hints at
9232 http://freedesktop.org/wiki/Standards_2fwm_2dspec */
9233
9234 const char *atom = "_NET_ACTIVE_WINDOW";
9235 if (f->async_visible && wm_supports (f, atom))
9236 {
9237 Lisp_Object frame;
9238 XSETFRAME (frame, f);
9239 Fx_send_client_event (frame, make_number (0), frame,
9240 make_unibyte_string (atom, strlen (atom)),
9241 make_number (32),
9242 Fcons (make_number (1),
9243 Fcons (make_number (last_user_time),
9244 Qnil)));
9245 }
9246 }
9247
9248 static void
9249 XTframe_raise_lower (f, raise_flag)
9250 FRAME_PTR f;
9251 int raise_flag;
9252 {
9253 if (raise_flag)
9254 x_raise_frame (f);
9255 else
9256 x_lower_frame (f);
9257 }
9258 \f
9259 /* Change of visibility. */
9260
9261 /* This tries to wait until the frame is really visible.
9262 However, if the window manager asks the user where to position
9263 the frame, this will return before the user finishes doing that.
9264 The frame will not actually be visible at that time,
9265 but it will become visible later when the window manager
9266 finishes with it. */
9267
9268 void
9269 x_make_frame_visible (f)
9270 struct frame *f;
9271 {
9272 Lisp_Object type;
9273 int original_top, original_left;
9274 int retry_count = 2;
9275
9276 retry:
9277
9278 BLOCK_INPUT;
9279
9280 type = x_icon_type (f);
9281 if (!NILP (type))
9282 x_bitmap_icon (f, type);
9283
9284 if (! FRAME_VISIBLE_P (f))
9285 {
9286 /* We test FRAME_GARBAGED_P here to make sure we don't
9287 call x_set_offset a second time
9288 if we get to x_make_frame_visible a second time
9289 before the window gets really visible. */
9290 if (! FRAME_ICONIFIED_P (f)
9291 && ! f->output_data.x->asked_for_visible)
9292 x_set_offset (f, f->left_pos, f->top_pos, 0);
9293
9294 f->output_data.x->asked_for_visible = 1;
9295
9296 if (! EQ (Vx_no_window_manager, Qt))
9297 x_wm_set_window_state (f, NormalState);
9298 #ifdef USE_X_TOOLKIT
9299 /* This was XtPopup, but that did nothing for an iconified frame. */
9300 XtMapWidget (f->output_data.x->widget);
9301 #else /* not USE_X_TOOLKIT */
9302 #ifdef USE_GTK
9303 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9304 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9305 #else
9306 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9307 #endif /* not USE_GTK */
9308 #endif /* not USE_X_TOOLKIT */
9309 #if 0 /* This seems to bring back scroll bars in the wrong places
9310 if the window configuration has changed. They seem
9311 to come back ok without this. */
9312 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9313 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9314 #endif
9315 }
9316
9317 XFlush (FRAME_X_DISPLAY (f));
9318
9319 /* Synchronize to ensure Emacs knows the frame is visible
9320 before we do anything else. We do this loop with input not blocked
9321 so that incoming events are handled. */
9322 {
9323 Lisp_Object frame;
9324 int count;
9325 /* This must be before UNBLOCK_INPUT
9326 since events that arrive in response to the actions above
9327 will set it when they are handled. */
9328 int previously_visible = f->output_data.x->has_been_visible;
9329
9330 original_left = f->left_pos;
9331 original_top = f->top_pos;
9332
9333 /* This must come after we set COUNT. */
9334 UNBLOCK_INPUT;
9335
9336 /* We unblock here so that arriving X events are processed. */
9337
9338 /* Now move the window back to where it was "supposed to be".
9339 But don't do it if the gravity is negative.
9340 When the gravity is negative, this uses a position
9341 that is 3 pixels too low. Perhaps that's really the border width.
9342
9343 Don't do this if the window has never been visible before,
9344 because the window manager may choose the position
9345 and we don't want to override it. */
9346
9347 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
9348 && f->win_gravity == NorthWestGravity
9349 && previously_visible)
9350 {
9351 Drawable rootw;
9352 int x, y;
9353 unsigned int width, height, border, depth;
9354
9355 BLOCK_INPUT;
9356
9357 /* On some window managers (such as FVWM) moving an existing
9358 window, even to the same place, causes the window manager
9359 to introduce an offset. This can cause the window to move
9360 to an unexpected location. Check the geometry (a little
9361 slow here) and then verify that the window is in the right
9362 place. If the window is not in the right place, move it
9363 there, and take the potential window manager hit. */
9364 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9365 &rootw, &x, &y, &width, &height, &border, &depth);
9366
9367 if (original_left != x || original_top != y)
9368 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9369 original_left, original_top);
9370
9371 UNBLOCK_INPUT;
9372 }
9373
9374 XSETFRAME (frame, f);
9375
9376 /* Wait until the frame is visible. Process X events until a
9377 MapNotify event has been seen, or until we think we won't get a
9378 MapNotify at all.. */
9379 for (count = input_signal_count + 10;
9380 input_signal_count < count && !FRAME_VISIBLE_P (f);)
9381 {
9382 /* Force processing of queued events. */
9383 x_sync (f);
9384
9385 /* Machines that do polling rather than SIGIO have been
9386 observed to go into a busy-wait here. So we'll fake an
9387 alarm signal to let the handler know that there's something
9388 to be read. We used to raise a real alarm, but it seems
9389 that the handler isn't always enabled here. This is
9390 probably a bug. */
9391 if (input_polling_used ())
9392 {
9393 /* It could be confusing if a real alarm arrives while
9394 processing the fake one. Turn it off and let the
9395 handler reset it. */
9396 extern void poll_for_input_1 P_ ((void));
9397 int old_poll_suppress_count = poll_suppress_count;
9398 poll_suppress_count = 1;
9399 poll_for_input_1 ();
9400 poll_suppress_count = old_poll_suppress_count;
9401 }
9402
9403 /* See if a MapNotify event has been processed. */
9404 FRAME_SAMPLE_VISIBILITY (f);
9405 }
9406
9407 /* 2000-09-28: In
9408
9409 (let ((f (selected-frame)))
9410 (iconify-frame f)
9411 (raise-frame f))
9412
9413 the frame is not raised with various window managers on
9414 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
9415 unknown reason, the call to XtMapWidget is completely ignored.
9416 Mapping the widget a second time works. */
9417
9418 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
9419 goto retry;
9420 }
9421 }
9422
9423 /* Change from mapped state to withdrawn state. */
9424
9425 /* Make the frame visible (mapped and not iconified). */
9426
9427 void
9428 x_make_frame_invisible (f)
9429 struct frame *f;
9430 {
9431 Window window;
9432
9433 /* Use the frame's outermost window, not the one we normally draw on. */
9434 window = FRAME_OUTER_WINDOW (f);
9435
9436 /* Don't keep the highlight on an invisible frame. */
9437 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9438 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9439
9440 #if 0/* This might add unreliability; I don't trust it -- rms. */
9441 if (! f->async_visible && ! f->async_iconified)
9442 return;
9443 #endif
9444
9445 BLOCK_INPUT;
9446
9447 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
9448 that the current position of the window is user-specified, rather than
9449 program-specified, so that when the window is mapped again, it will be
9450 placed at the same location, without forcing the user to position it
9451 by hand again (they have already done that once for this window.) */
9452 x_wm_set_size_hint (f, (long) 0, 1);
9453
9454 #ifdef USE_GTK
9455 if (FRAME_GTK_OUTER_WIDGET (f))
9456 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
9457 else
9458 #endif
9459 {
9460 #ifdef HAVE_X11R4
9461
9462 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
9463 DefaultScreen (FRAME_X_DISPLAY (f))))
9464 {
9465 UNBLOCK_INPUT_RESIGNAL;
9466 error ("Can't notify window manager of window withdrawal");
9467 }
9468 #else /* ! defined (HAVE_X11R4) */
9469
9470 /* Tell the window manager what we're going to do. */
9471 if (! EQ (Vx_no_window_manager, Qt))
9472 {
9473 XEvent unmap;
9474
9475 unmap.xunmap.type = UnmapNotify;
9476 unmap.xunmap.window = window;
9477 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
9478 unmap.xunmap.from_configure = False;
9479 if (! XSendEvent (FRAME_X_DISPLAY (f),
9480 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9481 False,
9482 SubstructureRedirectMaskSubstructureNotifyMask,
9483 &unmap))
9484 {
9485 UNBLOCK_INPUT_RESIGNAL;
9486 error ("Can't notify window manager of withdrawal");
9487 }
9488 }
9489
9490 /* Unmap the window ourselves. Cheeky! */
9491 XUnmapWindow (FRAME_X_DISPLAY (f), window);
9492 #endif /* ! defined (HAVE_X11R4) */
9493 }
9494
9495 /* We can't distinguish this from iconification
9496 just by the event that we get from the server.
9497 So we can't win using the usual strategy of letting
9498 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9499 and synchronize with the server to make sure we agree. */
9500 f->visible = 0;
9501 FRAME_ICONIFIED_P (f) = 0;
9502 f->async_visible = 0;
9503 f->async_iconified = 0;
9504
9505 x_sync (f);
9506
9507 UNBLOCK_INPUT;
9508 }
9509
9510 /* Change window state from mapped to iconified. */
9511
9512 void
9513 x_iconify_frame (f)
9514 struct frame *f;
9515 {
9516 int result;
9517 Lisp_Object type;
9518
9519 /* Don't keep the highlight on an invisible frame. */
9520 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9521 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9522
9523 if (f->async_iconified)
9524 return;
9525
9526 BLOCK_INPUT;
9527
9528 FRAME_SAMPLE_VISIBILITY (f);
9529
9530 type = x_icon_type (f);
9531 if (!NILP (type))
9532 x_bitmap_icon (f, type);
9533
9534 #ifdef USE_GTK
9535 if (FRAME_GTK_OUTER_WIDGET (f))
9536 {
9537 if (! FRAME_VISIBLE_P (f))
9538 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9539
9540 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9541 f->iconified = 1;
9542 f->visible = 1;
9543 f->async_iconified = 1;
9544 f->async_visible = 0;
9545 UNBLOCK_INPUT;
9546 return;
9547 }
9548 #endif
9549
9550 #ifdef USE_X_TOOLKIT
9551
9552 if (! FRAME_VISIBLE_P (f))
9553 {
9554 if (! EQ (Vx_no_window_manager, Qt))
9555 x_wm_set_window_state (f, IconicState);
9556 /* This was XtPopup, but that did nothing for an iconified frame. */
9557 XtMapWidget (f->output_data.x->widget);
9558 /* The server won't give us any event to indicate
9559 that an invisible frame was changed to an icon,
9560 so we have to record it here. */
9561 f->iconified = 1;
9562 f->visible = 1;
9563 f->async_iconified = 1;
9564 f->async_visible = 0;
9565 UNBLOCK_INPUT;
9566 return;
9567 }
9568
9569 result = XIconifyWindow (FRAME_X_DISPLAY (f),
9570 XtWindow (f->output_data.x->widget),
9571 DefaultScreen (FRAME_X_DISPLAY (f)));
9572 UNBLOCK_INPUT;
9573
9574 if (!result)
9575 error ("Can't notify window manager of iconification");
9576
9577 f->async_iconified = 1;
9578 f->async_visible = 0;
9579
9580
9581 BLOCK_INPUT;
9582 XFlush (FRAME_X_DISPLAY (f));
9583 UNBLOCK_INPUT;
9584 #else /* not USE_X_TOOLKIT */
9585
9586 /* Make sure the X server knows where the window should be positioned,
9587 in case the user deiconifies with the window manager. */
9588 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
9589 x_set_offset (f, f->left_pos, f->top_pos, 0);
9590
9591 /* Since we don't know which revision of X we're running, we'll use both
9592 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
9593
9594 /* X11R4: send a ClientMessage to the window manager using the
9595 WM_CHANGE_STATE type. */
9596 {
9597 XEvent message;
9598
9599 message.xclient.window = FRAME_X_WINDOW (f);
9600 message.xclient.type = ClientMessage;
9601 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
9602 message.xclient.format = 32;
9603 message.xclient.data.l[0] = IconicState;
9604
9605 if (! XSendEvent (FRAME_X_DISPLAY (f),
9606 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9607 False,
9608 SubstructureRedirectMask | SubstructureNotifyMask,
9609 &message))
9610 {
9611 UNBLOCK_INPUT_RESIGNAL;
9612 error ("Can't notify window manager of iconification");
9613 }
9614 }
9615
9616 /* X11R3: set the initial_state field of the window manager hints to
9617 IconicState. */
9618 x_wm_set_window_state (f, IconicState);
9619
9620 if (!FRAME_VISIBLE_P (f))
9621 {
9622 /* If the frame was withdrawn, before, we must map it. */
9623 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9624 }
9625
9626 f->async_iconified = 1;
9627 f->async_visible = 0;
9628
9629 XFlush (FRAME_X_DISPLAY (f));
9630 UNBLOCK_INPUT;
9631 #endif /* not USE_X_TOOLKIT */
9632 }
9633
9634 \f
9635 /* Free X resources of frame F. */
9636
9637 void
9638 x_free_frame_resources (f)
9639 struct frame *f;
9640 {
9641 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9642 Lisp_Object bar;
9643 struct scroll_bar *b;
9644
9645 BLOCK_INPUT;
9646
9647 /* If a display connection is dead, don't try sending more
9648 commands to the X server. */
9649 if (dpyinfo->display)
9650 {
9651 #ifdef USE_FONT_BACKEND
9652 /* We must free faces before destroying windows because some
9653 font-driver (e.g. xft) access a window while finishing a
9654 face. */
9655 if (enable_font_backend
9656 && FRAME_FACE_CACHE (f))
9657 free_frame_faces (f);
9658 #endif /* USE_FONT_BACKEND */
9659
9660 if (f->output_data.x->icon_desc)
9661 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9662
9663 #ifdef USE_X_TOOLKIT
9664 /* Explicitly destroy the scroll bars of the frame. Without
9665 this, we get "BadDrawable" errors from the toolkit later on,
9666 presumably from expose events generated for the disappearing
9667 toolkit scroll bars. */
9668 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9669 {
9670 b = XSCROLL_BAR (bar);
9671 x_scroll_bar_remove (b);
9672 }
9673 #endif
9674
9675 #ifdef HAVE_X_I18N
9676 if (FRAME_XIC (f))
9677 free_frame_xic (f);
9678 #endif
9679
9680 #ifdef USE_X_TOOLKIT
9681 if (f->output_data.x->widget)
9682 {
9683 XtDestroyWidget (f->output_data.x->widget);
9684 f->output_data.x->widget = NULL;
9685 }
9686 /* Tooltips don't have widgets, only a simple X window, even if
9687 we are using a toolkit. */
9688 else if (FRAME_X_WINDOW (f))
9689 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9690
9691 free_frame_menubar (f);
9692 #else /* !USE_X_TOOLKIT */
9693
9694 #ifdef USE_GTK
9695 /* In the GTK version, tooltips are normal X
9696 frames. We must check and free both types. */
9697 if (FRAME_GTK_OUTER_WIDGET (f))
9698 {
9699 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
9700 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
9701 FRAME_GTK_OUTER_WIDGET (f) = 0;
9702 }
9703 #endif /* USE_GTK */
9704
9705 if (FRAME_X_WINDOW (f))
9706 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9707 #endif /* !USE_X_TOOLKIT */
9708
9709 unload_color (f, f->output_data.x->foreground_pixel);
9710 unload_color (f, f->output_data.x->background_pixel);
9711 unload_color (f, f->output_data.x->cursor_pixel);
9712 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9713 unload_color (f, f->output_data.x->border_pixel);
9714 unload_color (f, f->output_data.x->mouse_pixel);
9715
9716 if (f->output_data.x->scroll_bar_background_pixel != -1)
9717 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9718 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9719 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9720 #ifdef USE_TOOLKIT_SCROLL_BARS
9721 /* Scrollbar shadow colors. */
9722 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9723 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9724 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9725 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9726 #endif /* USE_TOOLKIT_SCROLL_BARS */
9727 if (f->output_data.x->white_relief.allocated_p)
9728 unload_color (f, f->output_data.x->white_relief.pixel);
9729 if (f->output_data.x->black_relief.allocated_p)
9730 unload_color (f, f->output_data.x->black_relief.pixel);
9731
9732 if (FRAME_FACE_CACHE (f))
9733 free_frame_faces (f);
9734
9735 x_free_gcs (f);
9736 XFlush (FRAME_X_DISPLAY (f));
9737 }
9738
9739 if (f->output_data.x->saved_menu_event)
9740 xfree (f->output_data.x->saved_menu_event);
9741
9742 xfree (f->output_data.x);
9743 f->output_data.x = NULL;
9744
9745 if (f == dpyinfo->x_focus_frame)
9746 dpyinfo->x_focus_frame = 0;
9747 if (f == dpyinfo->x_focus_event_frame)
9748 dpyinfo->x_focus_event_frame = 0;
9749 if (f == dpyinfo->x_highlight_frame)
9750 dpyinfo->x_highlight_frame = 0;
9751
9752 if (f == dpyinfo->mouse_face_mouse_frame)
9753 {
9754 dpyinfo->mouse_face_beg_row
9755 = dpyinfo->mouse_face_beg_col = -1;
9756 dpyinfo->mouse_face_end_row
9757 = dpyinfo->mouse_face_end_col = -1;
9758 dpyinfo->mouse_face_window = Qnil;
9759 dpyinfo->mouse_face_deferred_gc = 0;
9760 dpyinfo->mouse_face_mouse_frame = 0;
9761 }
9762
9763 UNBLOCK_INPUT;
9764 }
9765
9766
9767 /* Destroy the X window of frame F. */
9768
9769 void
9770 x_destroy_window (f)
9771 struct frame *f;
9772 {
9773 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9774
9775 /* If a display connection is dead, don't try sending more
9776 commands to the X server. */
9777 if (dpyinfo->display != 0)
9778 x_free_frame_resources (f);
9779
9780 dpyinfo->reference_count--;
9781 }
9782
9783 \f
9784 /* Setting window manager hints. */
9785
9786 /* Set the normal size hints for the window manager, for frame F.
9787 FLAGS is the flags word to use--or 0 meaning preserve the flags
9788 that the window now has.
9789 If USER_POSITION is nonzero, we set the USPosition
9790 flag (this is useful when FLAGS is 0).
9791 The GTK version is in gtkutils.c */
9792
9793 #ifndef USE_GTK
9794 void
9795 x_wm_set_size_hint (f, flags, user_position)
9796 struct frame *f;
9797 long flags;
9798 int user_position;
9799 {
9800 XSizeHints size_hints;
9801
9802 #ifdef USE_X_TOOLKIT
9803 Arg al[2];
9804 int ac = 0;
9805 Dimension widget_width, widget_height;
9806 #endif
9807
9808 Window window = FRAME_OUTER_WINDOW (f);
9809
9810 /* Setting PMaxSize caused various problems. */
9811 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9812
9813 size_hints.x = f->left_pos;
9814 size_hints.y = f->top_pos;
9815
9816 #ifdef USE_X_TOOLKIT
9817 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
9818 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
9819 XtGetValues (f->output_data.x->widget, al, ac);
9820 size_hints.height = widget_height;
9821 size_hints.width = widget_width;
9822 #else /* not USE_X_TOOLKIT */
9823 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9824 size_hints.width = FRAME_PIXEL_WIDTH (f);
9825 #endif /* not USE_X_TOOLKIT */
9826
9827 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
9828 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
9829 size_hints.max_width
9830 = FRAME_X_DISPLAY_INFO (f)->width - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9831 size_hints.max_height
9832 = FRAME_X_DISPLAY_INFO (f)->height - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9833
9834 /* Calculate the base and minimum sizes.
9835
9836 (When we use the X toolkit, we don't do it here.
9837 Instead we copy the values that the widgets are using, below.) */
9838 #ifndef USE_X_TOOLKIT
9839 {
9840 int base_width, base_height;
9841 int min_rows = 0, min_cols = 0;
9842
9843 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9844 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9845
9846 check_frame_size (f, &min_rows, &min_cols);
9847
9848 /* The window manager uses the base width hints to calculate the
9849 current number of rows and columns in the frame while
9850 resizing; min_width and min_height aren't useful for this
9851 purpose, since they might not give the dimensions for a
9852 zero-row, zero-column frame.
9853
9854 We use the base_width and base_height members if we have
9855 them; otherwise, we set the min_width and min_height members
9856 to the size for a zero x zero frame. */
9857
9858 #ifdef HAVE_X11R4
9859 size_hints.flags |= PBaseSize;
9860 size_hints.base_width = base_width;
9861 size_hints.base_height = base_height;
9862 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9863 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9864 #else
9865 size_hints.min_width = base_width;
9866 size_hints.min_height = base_height;
9867 #endif
9868 }
9869
9870 /* If we don't need the old flags, we don't need the old hint at all. */
9871 if (flags)
9872 {
9873 size_hints.flags |= flags;
9874 goto no_read;
9875 }
9876 #endif /* not USE_X_TOOLKIT */
9877
9878 {
9879 XSizeHints hints; /* Sometimes I hate X Windows... */
9880 long supplied_return;
9881 int value;
9882
9883 #ifdef HAVE_X11R4
9884 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9885 &supplied_return);
9886 #else
9887 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
9888 #endif
9889
9890 #ifdef USE_X_TOOLKIT
9891 size_hints.base_height = hints.base_height;
9892 size_hints.base_width = hints.base_width;
9893 size_hints.min_height = hints.min_height;
9894 size_hints.min_width = hints.min_width;
9895 #endif
9896
9897 if (flags)
9898 size_hints.flags |= flags;
9899 else
9900 {
9901 if (value == 0)
9902 hints.flags = 0;
9903 if (hints.flags & PSize)
9904 size_hints.flags |= PSize;
9905 if (hints.flags & PPosition)
9906 size_hints.flags |= PPosition;
9907 if (hints.flags & USPosition)
9908 size_hints.flags |= USPosition;
9909 if (hints.flags & USSize)
9910 size_hints.flags |= USSize;
9911 }
9912 }
9913
9914 #ifndef USE_X_TOOLKIT
9915 no_read:
9916 #endif
9917
9918 #ifdef PWinGravity
9919 size_hints.win_gravity = f->win_gravity;
9920 size_hints.flags |= PWinGravity;
9921
9922 if (user_position)
9923 {
9924 size_hints.flags &= ~ PPosition;
9925 size_hints.flags |= USPosition;
9926 }
9927 #endif /* PWinGravity */
9928
9929 #ifdef HAVE_X11R4
9930 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9931 #else
9932 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9933 #endif
9934 }
9935 #endif /* not USE_GTK */
9936
9937 /* Used for IconicState or NormalState */
9938
9939 void
9940 x_wm_set_window_state (f, state)
9941 struct frame *f;
9942 int state;
9943 {
9944 #ifdef USE_X_TOOLKIT
9945 Arg al[1];
9946
9947 XtSetArg (al[0], XtNinitialState, state);
9948 XtSetValues (f->output_data.x->widget, al, 1);
9949 #else /* not USE_X_TOOLKIT */
9950 Window window = FRAME_X_WINDOW (f);
9951
9952 f->output_data.x->wm_hints.flags |= StateHint;
9953 f->output_data.x->wm_hints.initial_state = state;
9954
9955 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9956 #endif /* not USE_X_TOOLKIT */
9957 }
9958
9959 void
9960 x_wm_set_icon_pixmap (f, pixmap_id)
9961 struct frame *f;
9962 int pixmap_id;
9963 {
9964 Pixmap icon_pixmap, icon_mask;
9965
9966 #ifndef USE_X_TOOLKIT
9967 Window window = FRAME_OUTER_WINDOW (f);
9968 #endif
9969
9970 if (pixmap_id > 0)
9971 {
9972 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9973 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9974 icon_mask = x_bitmap_mask (f, pixmap_id);
9975 f->output_data.x->wm_hints.icon_mask = icon_mask;
9976 }
9977 else
9978 {
9979 /* It seems there is no way to turn off use of an icon pixmap.
9980 The following line does it, only if no icon has yet been created,
9981 for some window managers. But with mwm it crashes.
9982 Some people say it should clear the IconPixmapHint bit in this case,
9983 but that doesn't work, and the X consortium said it isn't the
9984 right thing at all. Since there is no way to win,
9985 best to explicitly give up. */
9986 #if 0
9987 f->output_data.x->wm_hints.icon_pixmap = None;
9988 f->output_data.x->wm_hints.icon_mask = None;
9989 #else
9990 return;
9991 #endif
9992 }
9993
9994
9995 #ifdef USE_GTK
9996 {
9997 xg_set_frame_icon (f, icon_pixmap, icon_mask);
9998 return;
9999 }
10000
10001 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
10002
10003 {
10004 Arg al[1];
10005 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
10006 XtSetValues (f->output_data.x->widget, al, 1);
10007 XtSetArg (al[0], XtNiconMask, icon_mask);
10008 XtSetValues (f->output_data.x->widget, al, 1);
10009 }
10010
10011 #else /* not USE_X_TOOLKIT && not USE_GTK */
10012
10013 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
10014 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
10015
10016 #endif /* not USE_X_TOOLKIT && not USE_GTK */
10017 }
10018
10019 void
10020 x_wm_set_icon_position (f, icon_x, icon_y)
10021 struct frame *f;
10022 int icon_x, icon_y;
10023 {
10024 Window window = FRAME_OUTER_WINDOW (f);
10025
10026 f->output_data.x->wm_hints.flags |= IconPositionHint;
10027 f->output_data.x->wm_hints.icon_x = icon_x;
10028 f->output_data.x->wm_hints.icon_y = icon_y;
10029
10030 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
10031 }
10032
10033 \f
10034 /***********************************************************************
10035 Fonts
10036 ***********************************************************************/
10037
10038 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
10039
10040 struct font_info *
10041 x_get_font_info (f, font_idx)
10042 FRAME_PTR f;
10043 int font_idx;
10044 {
10045 return (FRAME_X_FONT_TABLE (f) + font_idx);
10046 }
10047
10048
10049 /* Return a list of names of available fonts matching PATTERN on frame F.
10050
10051 If SIZE is > 0, it is the size (maximum bounds width) of fonts
10052 to be listed.
10053
10054 SIZE < 0 means include auto scaled fonts.
10055
10056 Frame F null means we have not yet created any frame on X, and
10057 consult the first display in x_display_list. MAXNAMES sets a limit
10058 on how many fonts to match. */
10059
10060 Lisp_Object
10061 x_list_fonts (f, pattern, size, maxnames)
10062 struct frame *f;
10063 Lisp_Object pattern;
10064 int size;
10065 int maxnames;
10066 {
10067 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
10068 Lisp_Object tem, second_best;
10069 struct x_display_info *dpyinfo
10070 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
10071 Display *dpy = dpyinfo->display;
10072 int try_XLoadQueryFont = 0;
10073 int allow_auto_scaled_font = 0;
10074
10075 if (size < 0)
10076 {
10077 allow_auto_scaled_font = 1;
10078 size = 0;
10079 }
10080
10081 patterns = Fassoc (pattern, Valternate_fontname_alist);
10082 if (NILP (patterns))
10083 patterns = Fcons (pattern, Qnil);
10084
10085 if (maxnames == 1 && !size)
10086 /* We can return any single font matching PATTERN. */
10087 try_XLoadQueryFont = 1;
10088
10089 for (; CONSP (patterns); patterns = XCDR (patterns))
10090 {
10091 int num_fonts;
10092 char **names = NULL;
10093
10094 pattern = XCAR (patterns);
10095 /* See if we cached the result for this particular query.
10096 The cache is an alist of the form:
10097 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
10098 tem = XCDR (dpyinfo->name_list_element);
10099 key = Fcons (Fcons (pattern, make_number (maxnames)),
10100 allow_auto_scaled_font ? Qt : Qnil);
10101 list = Fassoc (key, tem);
10102 if (!NILP (list))
10103 {
10104 list = Fcdr_safe (list);
10105 /* We have a cashed list. Don't have to get the list again. */
10106 goto label_cached;
10107 }
10108
10109 /* At first, put PATTERN in the cache. */
10110
10111 BLOCK_INPUT;
10112 x_catch_errors (dpy);
10113
10114 if (try_XLoadQueryFont)
10115 {
10116 XFontStruct *font;
10117 unsigned long value;
10118
10119 font = XLoadQueryFont (dpy, SDATA (pattern));
10120 if (x_had_errors_p (dpy))
10121 {
10122 /* This error is perhaps due to insufficient memory on X
10123 server. Let's just ignore it. */
10124 font = NULL;
10125 x_clear_errors (dpy);
10126 }
10127
10128 if (font
10129 && XGetFontProperty (font, XA_FONT, &value))
10130 {
10131 char *name = (char *) XGetAtomName (dpy, (Atom) value);
10132 int len = strlen (name);
10133 char *tmp;
10134
10135 /* If DXPC (a Differential X Protocol Compressor)
10136 Ver.3.7 is running, XGetAtomName will return null
10137 string. We must avoid such a name. */
10138 if (len == 0)
10139 try_XLoadQueryFont = 0;
10140 else
10141 {
10142 num_fonts = 1;
10143 names = (char **) alloca (sizeof (char *));
10144 /* Some systems only allow alloca assigned to a
10145 simple var. */
10146 tmp = (char *) alloca (len + 1); names[0] = tmp;
10147 bcopy (name, names[0], len + 1);
10148 XFree (name);
10149 }
10150 }
10151 else
10152 try_XLoadQueryFont = 0;
10153
10154 if (font)
10155 XFreeFont (dpy, font);
10156 }
10157
10158 if (!try_XLoadQueryFont)
10159 {
10160 /* We try at least 10 fonts because XListFonts will return
10161 auto-scaled fonts at the head. */
10162 if (maxnames < 0)
10163 {
10164 int limit;
10165
10166 for (limit = 500;;)
10167 {
10168 names = XListFonts (dpy, SDATA (pattern), limit, &num_fonts);
10169 if (num_fonts == limit)
10170 {
10171 BLOCK_INPUT;
10172 XFreeFontNames (names);
10173 UNBLOCK_INPUT;
10174 limit *= 2;
10175 }
10176 else
10177 break;
10178 }
10179 }
10180 else
10181 names = XListFonts (dpy, SDATA (pattern), max (maxnames, 10),
10182 &num_fonts);
10183
10184 if (x_had_errors_p (dpy))
10185 {
10186 /* This error is perhaps due to insufficient memory on X
10187 server. Let's just ignore it. */
10188 names = NULL;
10189 x_clear_errors (dpy);
10190 }
10191 }
10192
10193 x_uncatch_errors ();
10194 UNBLOCK_INPUT;
10195
10196 if (names)
10197 {
10198 int i;
10199
10200 /* Make a list of all the fonts we got back.
10201 Store that in the font cache for the display. */
10202 for (i = 0; i < num_fonts; i++)
10203 {
10204 int width = 0;
10205 char *p = names[i];
10206 int average_width = -1, resx = 0, dashes = 0;
10207
10208 /* Count the number of dashes in NAMES[I]. If there are
10209 14 dashes, the field value following 9th dash
10210 (RESOLUTION_X) is nonzero, and the field value
10211 following 12th dash (AVERAGE_WIDTH) is 0, this is a
10212 auto-scaled font which is usually too ugly to be used
10213 for editing. Let's ignore it. */
10214 while (*p)
10215 if (*p++ == '-')
10216 {
10217 dashes++;
10218 if (dashes == 7) /* PIXEL_SIZE field */
10219 width = atoi (p);
10220 else if (dashes == 9)
10221 resx = atoi (p);
10222 else if (dashes == 12) /* AVERAGE_WIDTH field */
10223 average_width = atoi (p);
10224 }
10225
10226 if (allow_auto_scaled_font
10227 || dashes < 14 || average_width != 0 || resx == 0)
10228 {
10229 tem = build_string (names[i]);
10230 if (NILP (Fassoc (tem, list)))
10231 {
10232 if (STRINGP (Vx_pixel_size_width_font_regexp)
10233 && ((fast_c_string_match_ignore_case
10234 (Vx_pixel_size_width_font_regexp, names[i]))
10235 >= 0))
10236 /* We can set the value of PIXEL_SIZE to the
10237 width of this font. */
10238 list = Fcons (Fcons (tem, make_number (width)), list);
10239 else
10240 /* For the moment, width is not known. */
10241 list = Fcons (Fcons (tem, Qnil), list);
10242 }
10243 }
10244 }
10245
10246 if (!try_XLoadQueryFont)
10247 {
10248 BLOCK_INPUT;
10249 XFreeFontNames (names);
10250 UNBLOCK_INPUT;
10251 }
10252 }
10253
10254 /* Now store the result in the cache. */
10255 XSETCDR (dpyinfo->name_list_element,
10256 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
10257
10258 label_cached:
10259 if (NILP (list)) continue; /* Try the remaining alternatives. */
10260
10261 newlist = second_best = Qnil;
10262 /* Make a list of the fonts that have the right width. */
10263 for (; CONSP (list); list = XCDR (list))
10264 {
10265 int found_size;
10266
10267 tem = XCAR (list);
10268
10269 if (!CONSP (tem) || NILP (XCAR (tem)))
10270 continue;
10271 if (!size)
10272 {
10273 newlist = Fcons (XCAR (tem), newlist);
10274 continue;
10275 }
10276
10277 if (!INTEGERP (XCDR (tem)))
10278 {
10279 /* Since we have not yet known the size of this font, we
10280 must try slow function call XLoadQueryFont. */
10281 XFontStruct *thisinfo;
10282
10283 BLOCK_INPUT;
10284 x_catch_errors (dpy);
10285 thisinfo = XLoadQueryFont (dpy,
10286 SDATA (XCAR (tem)));
10287 if (x_had_errors_p (dpy))
10288 {
10289 /* This error is perhaps due to insufficient memory on X
10290 server. Let's just ignore it. */
10291 thisinfo = NULL;
10292 x_clear_errors (dpy);
10293 }
10294 x_uncatch_errors ();
10295 UNBLOCK_INPUT;
10296
10297 if (thisinfo)
10298 {
10299 XSETCDR (tem,
10300 (thisinfo->min_bounds.width == 0
10301 ? make_number (0)
10302 : make_number (thisinfo->max_bounds.width)));
10303 BLOCK_INPUT;
10304 XFreeFont (dpy, thisinfo);
10305 UNBLOCK_INPUT;
10306 }
10307 else
10308 /* For unknown reason, the previous call of XListFont had
10309 returned a font which can't be opened. Record the size
10310 as 0 not to try to open it again. */
10311 XSETCDR (tem, make_number (0));
10312 }
10313
10314 found_size = XINT (XCDR (tem));
10315 if (found_size == size)
10316 newlist = Fcons (XCAR (tem), newlist);
10317 else if (found_size > 0)
10318 {
10319 if (NILP (second_best))
10320 second_best = tem;
10321 else if (found_size < size)
10322 {
10323 if (XINT (XCDR (second_best)) > size
10324 || XINT (XCDR (second_best)) < found_size)
10325 second_best = tem;
10326 }
10327 else
10328 {
10329 if (XINT (XCDR (second_best)) > size
10330 && XINT (XCDR (second_best)) > found_size)
10331 second_best = tem;
10332 }
10333 }
10334 }
10335 if (!NILP (newlist))
10336 break;
10337 else if (!NILP (second_best))
10338 {
10339 newlist = Fcons (XCAR (second_best), Qnil);
10340 break;
10341 }
10342 }
10343
10344 return newlist;
10345 }
10346
10347
10348 #if GLYPH_DEBUG
10349
10350 /* Check that FONT is valid on frame F. It is if it can be found in F's
10351 font table. */
10352
10353 static void
10354 x_check_font (f, font)
10355 struct frame *f;
10356 XFontStruct *font;
10357 {
10358 int i;
10359 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10360
10361 xassert (font != NULL);
10362
10363 for (i = 0; i < dpyinfo->n_fonts; i++)
10364 if (dpyinfo->font_table[i].name
10365 && font == dpyinfo->font_table[i].font)
10366 break;
10367
10368 xassert (i < dpyinfo->n_fonts);
10369 }
10370
10371 #endif /* GLYPH_DEBUG != 0 */
10372
10373 /* Set *W to the minimum width, *H to the minimum font height of FONT.
10374 Note: There are (broken) X fonts out there with invalid XFontStruct
10375 min_bounds contents. For example, handa@etl.go.jp reports that
10376 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
10377 have font->min_bounds.width == 0. */
10378
10379 static INLINE void
10380 x_font_min_bounds (font, w, h)
10381 XFontStruct *font;
10382 int *w, *h;
10383 {
10384 *h = FONT_HEIGHT (font);
10385 *w = font->min_bounds.width;
10386
10387 /* Try to handle the case where FONT->min_bounds has invalid
10388 contents. Since the only font known to have invalid min_bounds
10389 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
10390 if (*w <= 0)
10391 *w = font->max_bounds.width;
10392 }
10393
10394
10395 /* Compute the smallest character width and smallest font height over
10396 all fonts available on frame F. Set the members smallest_char_width
10397 and smallest_font_height in F's x_display_info structure to
10398 the values computed. Value is non-zero if smallest_font_height or
10399 smallest_char_width become smaller than they were before. */
10400
10401 static int
10402 x_compute_min_glyph_bounds (f)
10403 struct frame *f;
10404 {
10405 int i;
10406 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10407 XFontStruct *font;
10408 int old_width = dpyinfo->smallest_char_width;
10409 int old_height = dpyinfo->smallest_font_height;
10410
10411 dpyinfo->smallest_font_height = 100000;
10412 dpyinfo->smallest_char_width = 100000;
10413
10414 for (i = 0; i < dpyinfo->n_fonts; ++i)
10415 if (dpyinfo->font_table[i].name)
10416 {
10417 struct font_info *fontp = dpyinfo->font_table + i;
10418 int w, h;
10419
10420 font = (XFontStruct *) fontp->font;
10421 xassert (font != (XFontStruct *) ~0);
10422 x_font_min_bounds (font, &w, &h);
10423
10424 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
10425 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
10426 }
10427
10428 xassert (dpyinfo->smallest_char_width > 0
10429 && dpyinfo->smallest_font_height > 0);
10430
10431 return (dpyinfo->n_fonts == 1
10432 || dpyinfo->smallest_char_width < old_width
10433 || dpyinfo->smallest_font_height < old_height);
10434 }
10435
10436
10437 /* Load font named FONTNAME of the size SIZE for frame F, and return a
10438 pointer to the structure font_info while allocating it dynamically.
10439 If SIZE is 0, load any size of font.
10440 If loading is failed, return NULL. */
10441
10442 struct font_info *
10443 x_load_font (f, fontname, size)
10444 struct frame *f;
10445 register char *fontname;
10446 int size;
10447 {
10448 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10449 Lisp_Object font_names;
10450
10451 /* Get a list of all the fonts that match this name. Once we
10452 have a list of matching fonts, we compare them against the fonts
10453 we already have by comparing names. */
10454 font_names = x_list_fonts (f, build_string (fontname), size, 1);
10455
10456 if (!NILP (font_names))
10457 {
10458 Lisp_Object tail;
10459 int i;
10460
10461 for (i = 0; i < dpyinfo->n_fonts; i++)
10462 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
10463 if (dpyinfo->font_table[i].name
10464 && (!strcmp (dpyinfo->font_table[i].name,
10465 SDATA (XCAR (tail)))
10466 || !strcmp (dpyinfo->font_table[i].full_name,
10467 SDATA (XCAR (tail)))))
10468 return (dpyinfo->font_table + i);
10469 }
10470
10471 /* Load the font and add it to the table. */
10472 {
10473 char *full_name;
10474 XFontStruct *font;
10475 struct font_info *fontp;
10476 unsigned long value;
10477 int i;
10478
10479 /* If we have found fonts by x_list_font, load one of them. If
10480 not, we still try to load a font by the name given as FONTNAME
10481 because XListFonts (called in x_list_font) of some X server has
10482 a bug of not finding a font even if the font surely exists and
10483 is loadable by XLoadQueryFont. */
10484 if (size > 0 && !NILP (font_names))
10485 fontname = (char *) SDATA (XCAR (font_names));
10486
10487 BLOCK_INPUT;
10488 x_catch_errors (FRAME_X_DISPLAY (f));
10489 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
10490 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
10491 {
10492 /* This error is perhaps due to insufficient memory on X
10493 server. Let's just ignore it. */
10494 font = NULL;
10495 x_clear_errors (FRAME_X_DISPLAY (f));
10496 }
10497 x_uncatch_errors ();
10498 UNBLOCK_INPUT;
10499 if (!font)
10500 return NULL;
10501
10502 /* Find a free slot in the font table. */
10503 for (i = 0; i < dpyinfo->n_fonts; ++i)
10504 if (dpyinfo->font_table[i].name == NULL)
10505 break;
10506
10507 /* If no free slot found, maybe enlarge the font table. */
10508 if (i == dpyinfo->n_fonts
10509 && dpyinfo->n_fonts == dpyinfo->font_table_size)
10510 {
10511 int sz;
10512 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
10513 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
10514 dpyinfo->font_table
10515 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
10516 }
10517
10518 fontp = dpyinfo->font_table + i;
10519 if (i == dpyinfo->n_fonts)
10520 ++dpyinfo->n_fonts;
10521
10522 /* Now fill in the slots of *FONTP. */
10523 BLOCK_INPUT;
10524 bzero (fontp, sizeof (*fontp));
10525 fontp->font = font;
10526 fontp->font_idx = i;
10527 fontp->charset = -1; /* fs_load_font sets it. */
10528 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
10529 bcopy (fontname, fontp->name, strlen (fontname) + 1);
10530
10531 if (font->min_bounds.width == font->max_bounds.width)
10532 {
10533 /* Fixed width font. */
10534 fontp->average_width = fontp->space_width = font->min_bounds.width;
10535 }
10536 else
10537 {
10538 XChar2b char2b;
10539 XCharStruct *pcm;
10540
10541 char2b.byte1 = 0x00, char2b.byte2 = 0x20;
10542 pcm = x_per_char_metric (font, &char2b, 0);
10543 if (pcm)
10544 fontp->space_width = pcm->width;
10545 else
10546 fontp->space_width = FONT_WIDTH (font);
10547
10548 fontp->average_width
10549 = (XGetFontProperty (font, dpyinfo->Xatom_AVERAGE_WIDTH, &value)
10550 ? (long) value / 10 : 0);
10551 if (fontp->average_width < 0)
10552 fontp->average_width = - fontp->average_width;
10553 if (fontp->average_width == 0)
10554 {
10555 if (pcm)
10556 {
10557 int width = pcm->width;
10558 for (char2b.byte2 = 33; char2b.byte2 <= 126; char2b.byte2++)
10559 if ((pcm = x_per_char_metric (font, &char2b, 0)) != NULL)
10560 width += pcm->width;
10561 fontp->average_width = width / 95;
10562 }
10563 else
10564 fontp->average_width = FONT_WIDTH (font);
10565 }
10566 }
10567
10568 /* Try to get the full name of FONT. Put it in FULL_NAME. */
10569 full_name = 0;
10570 if (XGetFontProperty (font, XA_FONT, &value))
10571 {
10572 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
10573 char *p = name;
10574 int dashes = 0;
10575
10576 /* Count the number of dashes in the "full name".
10577 If it is too few, this isn't really the font's full name,
10578 so don't use it.
10579 In X11R4, the fonts did not come with their canonical names
10580 stored in them. */
10581 while (*p)
10582 {
10583 if (*p == '-')
10584 dashes++;
10585 p++;
10586 }
10587
10588 if (dashes >= 13)
10589 {
10590 full_name = (char *) xmalloc (p - name + 1);
10591 bcopy (name, full_name, p - name + 1);
10592 }
10593
10594 XFree (name);
10595 }
10596
10597 if (full_name != 0)
10598 fontp->full_name = full_name;
10599 else
10600 fontp->full_name = fontp->name;
10601
10602 fontp->size = font->max_bounds.width;
10603 fontp->height = FONT_HEIGHT (font);
10604
10605 if (NILP (font_names))
10606 {
10607 /* We come here because of a bug of XListFonts mentioned at
10608 the head of this block. Let's store this information in
10609 the cache for x_list_fonts. */
10610 Lisp_Object lispy_name = build_string (fontname);
10611 Lisp_Object lispy_full_name = build_string (fontp->full_name);
10612 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
10613 Qnil);
10614
10615 XSETCDR (dpyinfo->name_list_element,
10616 Fcons (Fcons (key,
10617 Fcons (Fcons (lispy_full_name,
10618 make_number (fontp->size)),
10619 Qnil)),
10620 XCDR (dpyinfo->name_list_element)));
10621 if (full_name)
10622 {
10623 key = Fcons (Fcons (lispy_full_name, make_number (256)),
10624 Qnil);
10625 XSETCDR (dpyinfo->name_list_element,
10626 Fcons (Fcons (key,
10627 Fcons (Fcons (lispy_full_name,
10628 make_number (fontp->size)),
10629 Qnil)),
10630 XCDR (dpyinfo->name_list_element)));
10631 }
10632 }
10633
10634 /* The slot `encoding' specifies how to map a character
10635 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
10636 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
10637 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
10638 2:0xA020..0xFF7F). For the moment, we don't know which charset
10639 uses this font. So, we set information in fontp->encoding_type
10640 which is never used by any charset. If mapping can't be
10641 decided, set FONT_ENCODING_NOT_DECIDED. */
10642 fontp->encoding_type
10643 = (font->max_byte1 == 0
10644 /* 1-byte font */
10645 ? (font->min_char_or_byte2 < 0x80
10646 ? (font->max_char_or_byte2 < 0x80
10647 ? 0 /* 0x20..0x7F */
10648 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
10649 : 1) /* 0xA0..0xFF */
10650 /* 2-byte font */
10651 : (font->min_byte1 < 0x80
10652 ? (font->max_byte1 < 0x80
10653 ? (font->min_char_or_byte2 < 0x80
10654 ? (font->max_char_or_byte2 < 0x80
10655 ? 0 /* 0x2020..0x7F7F */
10656 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
10657 : 3) /* 0x20A0..0x7FFF */
10658 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
10659 : (font->min_char_or_byte2 < 0x80
10660 ? (font->max_char_or_byte2 < 0x80
10661 ? 2 /* 0xA020..0xFF7F */
10662 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
10663 : 1))); /* 0xA0A0..0xFFFF */
10664
10665 fontp->baseline_offset
10666 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
10667 ? (long) value : 0);
10668 fontp->relative_compose
10669 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
10670 ? (long) value : 0);
10671 fontp->default_ascent
10672 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
10673 ? (long) value : 0);
10674
10675 /* Set global flag fonts_changed_p to non-zero if the font loaded
10676 has a character with a smaller width than any other character
10677 before, or if the font loaded has a smaller height than any
10678 other font loaded before. If this happens, it will make a
10679 glyph matrix reallocation necessary. */
10680 fonts_changed_p |= x_compute_min_glyph_bounds (f);
10681 UNBLOCK_INPUT;
10682 return fontp;
10683 }
10684 }
10685
10686
10687 /* Return a pointer to struct font_info of a font named FONTNAME for
10688 frame F. If no such font is loaded, return NULL. */
10689
10690 struct font_info *
10691 x_query_font (f, fontname)
10692 struct frame *f;
10693 register char *fontname;
10694 {
10695 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10696 int i;
10697
10698 for (i = 0; i < dpyinfo->n_fonts; i++)
10699 if (dpyinfo->font_table[i].name
10700 && (!xstricmp (dpyinfo->font_table[i].name, fontname)
10701 || !xstricmp (dpyinfo->font_table[i].full_name, fontname)))
10702 return (dpyinfo->font_table + i);
10703 return NULL;
10704 }
10705
10706
10707 /* Find a CCL program for a font specified by FONTP, and set the member
10708 `encoder' of the structure. */
10709
10710 void
10711 x_find_ccl_program (fontp)
10712 struct font_info *fontp;
10713 {
10714 Lisp_Object list, elt;
10715
10716 elt = Qnil;
10717 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
10718 {
10719 elt = XCAR (list);
10720 if (CONSP (elt)
10721 && STRINGP (XCAR (elt))
10722 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
10723 >= 0)
10724 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
10725 >= 0)))
10726 break;
10727 }
10728
10729 if (! NILP (list))
10730 {
10731 struct ccl_program *ccl
10732 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
10733
10734 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
10735 xfree (ccl);
10736 else
10737 fontp->font_encoder = ccl;
10738 }
10739 }
10740
10741
10742 /* Return a char-table whose elements are t if the font FONT_INFO
10743 contains a glyph for the corresponding character, and nil if
10744 not. */
10745
10746 Lisp_Object
10747 x_get_font_repertory (f, font_info)
10748 FRAME_PTR f;
10749 struct font_info *font_info;
10750 {
10751 XFontStruct *font = (XFontStruct *) font_info->font;
10752 Lisp_Object table;
10753 int min_byte1, max_byte1, min_byte2, max_byte2;
10754 int c;
10755 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
10756 int offset = CHARSET_OFFSET (charset);
10757
10758 table = Fmake_char_table (Qnil, Qnil);
10759
10760 min_byte1 = font->min_byte1;
10761 max_byte1 = font->max_byte1;
10762 min_byte2 = font->min_char_or_byte2;
10763 max_byte2 = font->max_char_or_byte2;
10764 if (min_byte1 == 0 && max_byte1 == 0)
10765 {
10766 if (! font->per_char || font->all_chars_exist == True)
10767 {
10768 if (offset >= 0)
10769 char_table_set_range (table, offset + min_byte2,
10770 offset + max_byte2, Qt);
10771 else
10772 for (; min_byte2 <= max_byte2; min_byte2++)
10773 {
10774 c = DECODE_CHAR (charset, min_byte2);
10775 CHAR_TABLE_SET (table, c, Qt);
10776 }
10777 }
10778 else
10779 {
10780 XCharStruct *pcm = font->per_char;
10781 int from = -1;
10782 int i;
10783
10784 for (i = min_byte2; i <= max_byte2; i++, pcm++)
10785 {
10786 if (pcm->width == 0 && pcm->rbearing == pcm->lbearing)
10787 {
10788 if (from >= 0)
10789 {
10790 if (offset >= 0)
10791 char_table_set_range (table, offset + from,
10792 offset + i - 1, Qt);
10793 else
10794 for (; from < i; from++)
10795 {
10796 c = DECODE_CHAR (charset, from);
10797 CHAR_TABLE_SET (table, c, Qt);
10798 }
10799 from = -1;
10800 }
10801 }
10802 else if (from < 0)
10803 from = i;
10804 }
10805 if (from >= 0)
10806 {
10807 if (offset >= 0)
10808 char_table_set_range (table, offset + from, offset + i - 1,
10809 Qt);
10810 else
10811 for (; from < i; from++)
10812 {
10813 c = DECODE_CHAR (charset, from);
10814 CHAR_TABLE_SET (table, c, Qt);
10815 }
10816 }
10817 }
10818 }
10819 else
10820 {
10821 if (! font->per_char || font->all_chars_exist == True)
10822 {
10823 int i, j;
10824
10825 if (offset >= 0)
10826 for (i = min_byte1; i <= max_byte1; i++)
10827 char_table_set_range
10828 (table, offset + ((i << 8) | min_byte2),
10829 offset + ((i << 8) | max_byte2), Qt);
10830 else
10831 for (i = min_byte1; i <= max_byte1; i++)
10832 for (j = min_byte2; j <= max_byte2; j++)
10833 {
10834 unsigned code = (i << 8) | j;
10835 c = DECODE_CHAR (charset, code);
10836 CHAR_TABLE_SET (table, c, Qt);
10837 }
10838 }
10839 else
10840 {
10841 XCharStruct *pcm = font->per_char;
10842 int i;
10843
10844 for (i = min_byte1; i <= max_byte1; i++)
10845 {
10846 int from = -1;
10847 int j;
10848
10849 for (j = min_byte2; j <= max_byte2; j++, pcm++)
10850 {
10851 if (pcm->width == 0 && pcm->rbearing == pcm->lbearing)
10852 {
10853 if (from >= 0)
10854 {
10855 if (offset >= 0)
10856 char_table_set_range
10857 (table, offset + ((i << 8) | from),
10858 offset + ((i << 8) | (j - 1)), Qt);
10859 else
10860 {
10861 for (; from < j; from++)
10862 {
10863 unsigned code = (i << 8) | from;
10864 c = ENCODE_CHAR (charset, code);
10865 CHAR_TABLE_SET (table, c, Qt);
10866 }
10867 }
10868 from = -1;
10869 }
10870 }
10871 else if (from < 0)
10872 from = j;
10873 }
10874 if (from >= 0)
10875 {
10876 if (offset >= 0)
10877 char_table_set_range
10878 (table, offset + ((i << 8) | from),
10879 offset + ((i << 8) | (j - 1)), Qt);
10880 else
10881 {
10882 for (; from < j; from++)
10883 {
10884 unsigned code = (i << 8) | from;
10885 c = DECODE_CHAR (charset, code);
10886 CHAR_TABLE_SET (table, c, Qt);
10887 }
10888 }
10889 }
10890 }
10891 }
10892 }
10893
10894 return table;
10895 }
10896 \f
10897 /***********************************************************************
10898 Initialization
10899 ***********************************************************************/
10900
10901 #ifdef USE_X_TOOLKIT
10902 static XrmOptionDescRec emacs_options[] = {
10903 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
10904 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
10905
10906 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
10907 XrmoptionSepArg, NULL},
10908 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
10909
10910 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10911 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10912 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10913 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10914 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10915 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
10916 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
10917 };
10918
10919 /* Whether atimer for Xt timeouts is activated or not. */
10920
10921 static int x_timeout_atimer_activated_flag;
10922
10923 #endif /* USE_X_TOOLKIT */
10924
10925 static int x_initialized;
10926
10927 #ifdef HAVE_X_SM
10928 static int x_session_initialized;
10929 #endif
10930
10931 #ifdef MULTI_KBOARD
10932 /* Test whether two display-name strings agree up to the dot that separates
10933 the screen number from the server number. */
10934 static int
10935 same_x_server (name1, name2)
10936 const char *name1, *name2;
10937 {
10938 int seen_colon = 0;
10939 const unsigned char *system_name = SDATA (Vsystem_name);
10940 int system_name_length = strlen (system_name);
10941 int length_until_period = 0;
10942
10943 while (system_name[length_until_period] != 0
10944 && system_name[length_until_period] != '.')
10945 length_until_period++;
10946
10947 /* Treat `unix' like an empty host name. */
10948 if (! strncmp (name1, "unix:", 5))
10949 name1 += 4;
10950 if (! strncmp (name2, "unix:", 5))
10951 name2 += 4;
10952 /* Treat this host's name like an empty host name. */
10953 if (! strncmp (name1, system_name, system_name_length)
10954 && name1[system_name_length] == ':')
10955 name1 += system_name_length;
10956 if (! strncmp (name2, system_name, system_name_length)
10957 && name2[system_name_length] == ':')
10958 name2 += system_name_length;
10959 /* Treat this host's domainless name like an empty host name. */
10960 if (! strncmp (name1, system_name, length_until_period)
10961 && name1[length_until_period] == ':')
10962 name1 += length_until_period;
10963 if (! strncmp (name2, system_name, length_until_period)
10964 && name2[length_until_period] == ':')
10965 name2 += length_until_period;
10966
10967 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
10968 {
10969 if (*name1 == ':')
10970 seen_colon++;
10971 if (seen_colon && *name1 == '.')
10972 return 1;
10973 }
10974 return (seen_colon
10975 && (*name1 == '.' || *name1 == '\0')
10976 && (*name2 == '.' || *name2 == '\0'));
10977 }
10978 #endif
10979
10980 /* Count number of set bits in mask and number of bits to shift to
10981 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
10982 to 5. */
10983 static void
10984 get_bits_and_offset (mask, bits, offset)
10985 unsigned long mask;
10986 int *bits;
10987 int *offset;
10988 {
10989 int nr = 0;
10990 int off = 0;
10991
10992 while (!(mask & 1))
10993 {
10994 off++;
10995 mask >>= 1;
10996 }
10997
10998 while (mask & 1)
10999 {
11000 nr++;
11001 mask >>= 1;
11002 }
11003
11004 *offset = off;
11005 *bits = nr;
11006 }
11007
11008 int
11009 x_display_ok (display)
11010 const char * display;
11011 {
11012 int dpy_ok = 1;
11013 Display *dpy;
11014
11015 dpy = XOpenDisplay (display);
11016 if (dpy)
11017 XCloseDisplay (dpy);
11018 else
11019 dpy_ok = 0;
11020 return dpy_ok;
11021 }
11022
11023 struct x_display_info *
11024 x_term_init (display_name, xrm_option, resource_name)
11025 Lisp_Object display_name;
11026 char *xrm_option;
11027 char *resource_name;
11028 {
11029 int connection;
11030 Display *dpy;
11031 struct x_display_info *dpyinfo;
11032 XrmDatabase xrdb;
11033
11034 BLOCK_INPUT;
11035
11036 if (!x_initialized)
11037 {
11038 x_initialize ();
11039 ++x_initialized;
11040 }
11041
11042 #ifdef USE_GTK
11043 {
11044 #define NUM_ARGV 10
11045 int argc;
11046 char *argv[NUM_ARGV];
11047 char **argv2 = argv;
11048 GdkAtom atom;
11049
11050 if (x_initialized++ > 1)
11051 {
11052 /* Opening another display. If xg_display_open returns less
11053 than zero, we are probably on GTK 2.0, which can only handle
11054 one display. GTK 2.2 or later can handle more than one. */
11055 if (xg_display_open (SDATA (display_name), &dpy) < 0)
11056 error ("Sorry, this version of GTK can only handle one display");
11057 }
11058 else
11059 {
11060 for (argc = 0; argc < NUM_ARGV; ++argc)
11061 argv[argc] = 0;
11062
11063 argc = 0;
11064 argv[argc++] = initial_argv[0];
11065
11066 if (! NILP (display_name))
11067 {
11068 argv[argc++] = "--display";
11069 argv[argc++] = SDATA (display_name);
11070 }
11071
11072 argv[argc++] = "--name";
11073 argv[argc++] = resource_name;
11074
11075 #ifdef HAVE_X11R5
11076 XSetLocaleModifiers ("");
11077 #endif
11078
11079 gtk_init (&argc, &argv2);
11080
11081 /* gtk_init does set_locale. We must fix locale after calling it. */
11082 fixup_locale ();
11083 xg_initialize ();
11084
11085 dpy = GDK_DISPLAY ();
11086
11087 /* NULL window -> events for all windows go to our function */
11088 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
11089
11090 /* Load our own gtkrc if it exists. */
11091 {
11092 char *file = "~/.emacs.d/gtkrc";
11093 Lisp_Object s, abs_file;
11094
11095 s = make_string (file, strlen (file));
11096 abs_file = Fexpand_file_name (s, Qnil);
11097
11098 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
11099 gtk_rc_parse (SDATA (abs_file));
11100 }
11101
11102 XSetErrorHandler (x_error_handler);
11103 XSetIOErrorHandler (x_io_error_quitter);
11104 }
11105 }
11106 #else /* not USE_GTK */
11107 #ifdef USE_X_TOOLKIT
11108 /* weiner@footloose.sps.mot.com reports that this causes
11109 errors with X11R5:
11110 X protocol error: BadAtom (invalid Atom parameter)
11111 on protocol request 18skiloaf.
11112 So let's not use it until R6. */
11113 #ifdef HAVE_X11XTR6
11114 XtSetLanguageProc (NULL, NULL, NULL);
11115 #endif
11116
11117 {
11118 int argc = 0;
11119 char *argv[3];
11120
11121 argv[0] = "";
11122 argc = 1;
11123 if (xrm_option)
11124 {
11125 argv[argc++] = "-xrm";
11126 argv[argc++] = xrm_option;
11127 }
11128 turn_on_atimers (0);
11129 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
11130 resource_name, EMACS_CLASS,
11131 emacs_options, XtNumber (emacs_options),
11132 &argc, argv);
11133 turn_on_atimers (1);
11134
11135 #ifdef HAVE_X11XTR6
11136 /* I think this is to compensate for XtSetLanguageProc. */
11137 fixup_locale ();
11138 #endif
11139 }
11140
11141 #else /* not USE_X_TOOLKIT */
11142 #ifdef HAVE_X11R5
11143 XSetLocaleModifiers ("");
11144 #endif
11145 dpy = XOpenDisplay (SDATA (display_name));
11146 #endif /* not USE_X_TOOLKIT */
11147 #endif /* not USE_GTK*/
11148
11149 /* Detect failure. */
11150 if (dpy == 0)
11151 {
11152 UNBLOCK_INPUT;
11153 return 0;
11154 }
11155
11156 /* We have definitely succeeded. Record the new connection. */
11157
11158 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
11159 bzero (dpyinfo, sizeof *dpyinfo);
11160
11161 #ifdef MULTI_KBOARD
11162 {
11163 struct x_display_info *share;
11164 Lisp_Object tail;
11165
11166 for (share = x_display_list, tail = x_display_name_list; share;
11167 share = share->next, tail = XCDR (tail))
11168 if (same_x_server (SDATA (XCAR (XCAR (tail))),
11169 SDATA (display_name)))
11170 break;
11171 if (share)
11172 dpyinfo->kboard = share->kboard;
11173 else
11174 {
11175 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
11176 init_kboard (dpyinfo->kboard);
11177 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
11178 {
11179 char *vendor = ServerVendor (dpy);
11180 UNBLOCK_INPUT;
11181 dpyinfo->kboard->Vsystem_key_alist
11182 = call1 (Qvendor_specific_keysyms,
11183 vendor ? build_string (vendor) : empty_unibyte_string);
11184 BLOCK_INPUT;
11185 }
11186
11187 dpyinfo->kboard->next_kboard = all_kboards;
11188 all_kboards = dpyinfo->kboard;
11189 /* Don't let the initial kboard remain current longer than necessary.
11190 That would cause problems if a file loaded on startup tries to
11191 prompt in the mini-buffer. */
11192 if (current_kboard == initial_kboard)
11193 current_kboard = dpyinfo->kboard;
11194 }
11195 dpyinfo->kboard->reference_count++;
11196 }
11197 #endif
11198
11199 /* Put this display on the chain. */
11200 dpyinfo->next = x_display_list;
11201 x_display_list = dpyinfo;
11202
11203 /* Put it on x_display_name_list as well, to keep them parallel. */
11204 x_display_name_list = Fcons (Fcons (display_name, Qnil),
11205 x_display_name_list);
11206 dpyinfo->name_list_element = XCAR (x_display_name_list);
11207
11208 dpyinfo->display = dpy;
11209
11210 #if 0
11211 XSetAfterFunction (x_current_display, x_trace_wire);
11212 #endif /* ! 0 */
11213
11214 dpyinfo->x_id_name
11215 = (char *) xmalloc (SBYTES (Vinvocation_name)
11216 + SBYTES (Vsystem_name)
11217 + 2);
11218 sprintf (dpyinfo->x_id_name, "%s@%s",
11219 SDATA (Vinvocation_name), SDATA (Vsystem_name));
11220
11221 /* Figure out which modifier bits mean what. */
11222 x_find_modifier_meanings (dpyinfo);
11223
11224 /* Get the scroll bar cursor. */
11225 #ifdef USE_GTK
11226 /* We must create a GTK cursor, it is required for GTK widgets. */
11227 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
11228 #endif /* USE_GTK */
11229
11230 dpyinfo->vertical_scroll_bar_cursor
11231 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
11232
11233 xrdb = x_load_resources (dpyinfo->display, xrm_option,
11234 resource_name, EMACS_CLASS);
11235 #ifdef HAVE_XRMSETDATABASE
11236 XrmSetDatabase (dpyinfo->display, xrdb);
11237 #else
11238 dpyinfo->display->db = xrdb;
11239 #endif
11240 /* Put the rdb where we can find it in a way that works on
11241 all versions. */
11242 dpyinfo->xrdb = xrdb;
11243
11244 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
11245 DefaultScreen (dpyinfo->display));
11246 select_visual (dpyinfo);
11247 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
11248 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
11249 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
11250 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
11251 dpyinfo->client_leader_window = 0;
11252 dpyinfo->grabbed = 0;
11253 dpyinfo->reference_count = 0;
11254 dpyinfo->icon_bitmap_id = -1;
11255 dpyinfo->font_table = NULL;
11256 dpyinfo->n_fonts = 0;
11257 dpyinfo->font_table_size = 0;
11258 dpyinfo->bitmaps = 0;
11259 dpyinfo->bitmaps_size = 0;
11260 dpyinfo->bitmaps_last = 0;
11261 dpyinfo->scratch_cursor_gc = 0;
11262 dpyinfo->mouse_face_mouse_frame = 0;
11263 dpyinfo->mouse_face_deferred_gc = 0;
11264 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
11265 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
11266 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
11267 dpyinfo->mouse_face_window = Qnil;
11268 dpyinfo->mouse_face_overlay = Qnil;
11269 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
11270 dpyinfo->mouse_face_defer = 0;
11271 dpyinfo->mouse_face_hidden = 0;
11272 dpyinfo->x_focus_frame = 0;
11273 dpyinfo->x_focus_event_frame = 0;
11274 dpyinfo->x_highlight_frame = 0;
11275 dpyinfo->image_cache = make_image_cache ();
11276 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
11277
11278 /* See if we can construct pixel values from RGB values. */
11279 dpyinfo->red_bits = dpyinfo->blue_bits = dpyinfo->green_bits = 0;
11280 dpyinfo->red_offset = dpyinfo->blue_offset = dpyinfo->green_offset = 0;
11281
11282 if (dpyinfo->visual->class == TrueColor)
11283 {
11284 get_bits_and_offset (dpyinfo->visual->red_mask,
11285 &dpyinfo->red_bits, &dpyinfo->red_offset);
11286 get_bits_and_offset (dpyinfo->visual->blue_mask,
11287 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
11288 get_bits_and_offset (dpyinfo->visual->green_mask,
11289 &dpyinfo->green_bits, &dpyinfo->green_offset);
11290 }
11291
11292 /* See if a private colormap is requested. */
11293 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
11294 {
11295 if (dpyinfo->visual->class == PseudoColor)
11296 {
11297 Lisp_Object value;
11298 value = display_x_get_resource (dpyinfo,
11299 build_string ("privateColormap"),
11300 build_string ("PrivateColormap"),
11301 Qnil, Qnil);
11302 if (STRINGP (value)
11303 && (!strcmp (SDATA (value), "true")
11304 || !strcmp (SDATA (value), "on")))
11305 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
11306 }
11307 }
11308 else
11309 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
11310 dpyinfo->visual, AllocNone);
11311
11312 {
11313 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
11314 double pixels = DisplayHeight (dpyinfo->display, screen_number);
11315 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
11316 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
11317 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
11318 pixels = DisplayWidth (dpyinfo->display, screen_number);
11319 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
11320 mm = DisplayWidthMM (dpyinfo->display, screen_number);
11321 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
11322 }
11323
11324 dpyinfo->Xatom_wm_protocols
11325 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
11326 dpyinfo->Xatom_wm_take_focus
11327 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
11328 dpyinfo->Xatom_wm_save_yourself
11329 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
11330 dpyinfo->Xatom_wm_delete_window
11331 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
11332 dpyinfo->Xatom_wm_change_state
11333 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
11334 dpyinfo->Xatom_wm_configure_denied
11335 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
11336 dpyinfo->Xatom_wm_window_moved
11337 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
11338 dpyinfo->Xatom_wm_client_leader
11339 = XInternAtom (dpyinfo->display, "WM_CLIENT_LEADER", False);
11340 dpyinfo->Xatom_editres
11341 = XInternAtom (dpyinfo->display, "Editres", False);
11342 dpyinfo->Xatom_CLIPBOARD
11343 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
11344 dpyinfo->Xatom_TIMESTAMP
11345 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
11346 dpyinfo->Xatom_TEXT
11347 = XInternAtom (dpyinfo->display, "TEXT", False);
11348 dpyinfo->Xatom_COMPOUND_TEXT
11349 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
11350 dpyinfo->Xatom_UTF8_STRING
11351 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
11352 dpyinfo->Xatom_DELETE
11353 = XInternAtom (dpyinfo->display, "DELETE", False);
11354 dpyinfo->Xatom_MULTIPLE
11355 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
11356 dpyinfo->Xatom_INCR
11357 = XInternAtom (dpyinfo->display, "INCR", False);
11358 dpyinfo->Xatom_EMACS_TMP
11359 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
11360 dpyinfo->Xatom_TARGETS
11361 = XInternAtom (dpyinfo->display, "TARGETS", False);
11362 dpyinfo->Xatom_NULL
11363 = XInternAtom (dpyinfo->display, "NULL", False);
11364 dpyinfo->Xatom_ATOM_PAIR
11365 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
11366 /* For properties of font. */
11367 dpyinfo->Xatom_PIXEL_SIZE
11368 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
11369 dpyinfo->Xatom_AVERAGE_WIDTH
11370 = XInternAtom (dpyinfo->display, "AVERAGE_WIDTH", False);
11371 dpyinfo->Xatom_MULE_BASELINE_OFFSET
11372 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
11373 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
11374 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
11375 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
11376 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
11377
11378 /* Ghostscript support. */
11379 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
11380 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
11381
11382 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
11383 False);
11384
11385 dpyinfo->cut_buffers_initialized = 0;
11386
11387 dpyinfo->x_dnd_atoms_size = 8;
11388 dpyinfo->x_dnd_atoms_length = 0;
11389 dpyinfo->x_dnd_atoms = xmalloc (sizeof (*dpyinfo->x_dnd_atoms)
11390 * dpyinfo->x_dnd_atoms_size);
11391
11392 dpyinfo->net_supported_atoms = NULL;
11393 dpyinfo->nr_net_supported_atoms = 0;
11394 dpyinfo->net_supported_window = 0;
11395
11396 connection = ConnectionNumber (dpyinfo->display);
11397 dpyinfo->connection = connection;
11398
11399 {
11400 char null_bits[1];
11401
11402 null_bits[0] = 0x00;
11403
11404 dpyinfo->null_pixel
11405 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
11406 null_bits, 1, 1, (long) 0, (long) 0,
11407 1);
11408 }
11409
11410 {
11411 extern int gray_bitmap_width, gray_bitmap_height;
11412 extern char *gray_bitmap_bits;
11413 dpyinfo->gray
11414 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
11415 gray_bitmap_bits,
11416 gray_bitmap_width, gray_bitmap_height,
11417 (unsigned long) 1, (unsigned long) 0, 1);
11418 }
11419
11420 #ifdef HAVE_X_I18N
11421 xim_initialize (dpyinfo, resource_name);
11422 #endif
11423
11424 #ifdef subprocesses
11425 /* This is only needed for distinguishing keyboard and process input. */
11426 if (connection != 0)
11427 add_keyboard_wait_descriptor (connection);
11428 #endif
11429
11430 #ifndef F_SETOWN_BUG
11431 #ifdef F_SETOWN
11432 #ifdef F_SETOWN_SOCK_NEG
11433 /* stdin is a socket here */
11434 fcntl (connection, F_SETOWN, -getpid ());
11435 #else /* ! defined (F_SETOWN_SOCK_NEG) */
11436 fcntl (connection, F_SETOWN, getpid ());
11437 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
11438 #endif /* ! defined (F_SETOWN) */
11439 #endif /* F_SETOWN_BUG */
11440
11441 #ifdef SIGIO
11442 if (interrupt_input)
11443 init_sigio (connection);
11444 #endif /* ! defined (SIGIO) */
11445
11446 #ifdef USE_LUCID
11447 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
11448 /* Make sure that we have a valid font for dialog boxes
11449 so that Xt does not crash. */
11450 {
11451 Display *dpy = dpyinfo->display;
11452 XrmValue d, fr, to;
11453 Font font;
11454
11455 d.addr = (XPointer)&dpy;
11456 d.size = sizeof (Display *);
11457 fr.addr = XtDefaultFont;
11458 fr.size = sizeof (XtDefaultFont);
11459 to.size = sizeof (Font *);
11460 to.addr = (XPointer)&font;
11461 x_catch_errors (dpy);
11462 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
11463 abort ();
11464 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
11465 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
11466 x_uncatch_errors ();
11467 }
11468 #endif
11469 #endif
11470
11471 /* See if we should run in synchronous mode. This is useful
11472 for debugging X code. */
11473 {
11474 Lisp_Object value;
11475 value = display_x_get_resource (dpyinfo,
11476 build_string ("synchronous"),
11477 build_string ("Synchronous"),
11478 Qnil, Qnil);
11479 if (STRINGP (value)
11480 && (!strcmp (SDATA (value), "true")
11481 || !strcmp (SDATA (value), "on")))
11482 XSynchronize (dpyinfo->display, True);
11483 }
11484
11485 {
11486 Lisp_Object value;
11487 value = display_x_get_resource (dpyinfo,
11488 build_string ("useXIM"),
11489 build_string ("UseXIM"),
11490 Qnil, Qnil);
11491 #ifdef USE_XIM
11492 if (STRINGP (value)
11493 && (!strcmp (XSTRING (value)->data, "false")
11494 || !strcmp (XSTRING (value)->data, "off")))
11495 use_xim = 0;
11496 #else
11497 if (STRINGP (value)
11498 && (!strcmp (XSTRING (value)->data, "true")
11499 || !strcmp (XSTRING (value)->data, "on")))
11500 use_xim = 1;
11501 #endif
11502 }
11503
11504 #ifdef HAVE_X_SM
11505 /* Only do this for the first display. */
11506 if (!x_session_initialized++)
11507 x_session_initialize (dpyinfo);
11508 #endif
11509
11510 UNBLOCK_INPUT;
11511
11512 return dpyinfo;
11513 }
11514 \f
11515 /* Get rid of display DPYINFO, assuming all frames are already gone,
11516 and without sending any more commands to the X server. */
11517
11518 void
11519 x_delete_display (dpyinfo)
11520 struct x_display_info *dpyinfo;
11521 {
11522 int i;
11523
11524 delete_keyboard_wait_descriptor (dpyinfo->connection);
11525
11526 /* Discard this display from x_display_name_list and x_display_list.
11527 We can't use Fdelq because that can quit. */
11528 if (! NILP (x_display_name_list)
11529 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
11530 x_display_name_list = XCDR (x_display_name_list);
11531 else
11532 {
11533 Lisp_Object tail;
11534
11535 tail = x_display_name_list;
11536 while (CONSP (tail) && CONSP (XCDR (tail)))
11537 {
11538 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
11539 {
11540 XSETCDR (tail, XCDR (XCDR (tail)));
11541 break;
11542 }
11543 tail = XCDR (tail);
11544 }
11545 }
11546
11547 if (next_noop_dpyinfo == dpyinfo)
11548 next_noop_dpyinfo = dpyinfo->next;
11549
11550 if (x_display_list == dpyinfo)
11551 x_display_list = dpyinfo->next;
11552 else
11553 {
11554 struct x_display_info *tail;
11555
11556 for (tail = x_display_list; tail; tail = tail->next)
11557 if (tail->next == dpyinfo)
11558 tail->next = tail->next->next;
11559 }
11560
11561 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
11562 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
11563 XrmDestroyDatabase (dpyinfo->xrdb);
11564 #endif
11565 #endif
11566 #ifdef MULTI_KBOARD
11567 if (--dpyinfo->kboard->reference_count == 0)
11568 delete_kboard (dpyinfo->kboard);
11569 #endif
11570 #ifdef HAVE_X_I18N
11571 if (dpyinfo->xim)
11572 xim_close_dpy (dpyinfo);
11573 #endif
11574
11575 /* Free the font names in the font table. */
11576 for (i = 0; i < dpyinfo->n_fonts; i++)
11577 if (dpyinfo->font_table[i].name)
11578 {
11579 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
11580 xfree (dpyinfo->font_table[i].full_name);
11581 xfree (dpyinfo->font_table[i].name);
11582 }
11583
11584 if (dpyinfo->font_table)
11585 {
11586 if (dpyinfo->font_table->font_encoder)
11587 xfree (dpyinfo->font_table->font_encoder);
11588 xfree (dpyinfo->font_table);
11589 }
11590 if (dpyinfo->x_id_name)
11591 xfree (dpyinfo->x_id_name);
11592 if (dpyinfo->color_cells)
11593 xfree (dpyinfo->color_cells);
11594 xfree (dpyinfo);
11595 }
11596
11597 #ifdef USE_X_TOOLKIT
11598
11599 /* Atimer callback function for TIMER. Called every 0.1s to process
11600 Xt timeouts, if needed. We must avoid calling XtAppPending as
11601 much as possible because that function does an implicit XFlush
11602 that slows us down. */
11603
11604 static void
11605 x_process_timeouts (timer)
11606 struct atimer *timer;
11607 {
11608 BLOCK_INPUT;
11609 x_timeout_atimer_activated_flag = 0;
11610 if (toolkit_scroll_bar_interaction || popup_activated ())
11611 {
11612 while (XtAppPending (Xt_app_con) & XtIMTimer)
11613 XtAppProcessEvent (Xt_app_con, XtIMTimer);
11614 /* Reactivate the atimer for next time. */
11615 x_activate_timeout_atimer ();
11616 }
11617 UNBLOCK_INPUT;
11618 }
11619
11620 /* Install an asynchronous timer that processes Xt timeout events
11621 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
11622 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
11623 function whenever these variables are set. This is necessary
11624 because some widget sets use timeouts internally, for example the
11625 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
11626 processed, these widgets don't behave normally. */
11627
11628 void
11629 x_activate_timeout_atimer ()
11630 {
11631 BLOCK_INPUT;
11632 if (!x_timeout_atimer_activated_flag)
11633 {
11634 EMACS_TIME interval;
11635
11636 EMACS_SET_SECS_USECS (interval, 0, 100000);
11637 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
11638 x_timeout_atimer_activated_flag = 1;
11639 }
11640 UNBLOCK_INPUT;
11641 }
11642
11643 #endif /* USE_X_TOOLKIT */
11644
11645 \f
11646 /* Set up use of X before we make the first connection. */
11647
11648 extern frame_parm_handler x_frame_parm_handlers[];
11649
11650 static struct redisplay_interface x_redisplay_interface =
11651 {
11652 x_frame_parm_handlers,
11653 x_produce_glyphs,
11654 x_write_glyphs,
11655 x_insert_glyphs,
11656 x_clear_end_of_line,
11657 x_scroll_run,
11658 x_after_update_window_line,
11659 x_update_window_begin,
11660 x_update_window_end,
11661 x_cursor_to,
11662 x_flush,
11663 #ifdef XFlush
11664 x_flush,
11665 #else
11666 0, /* flush_display_optional */
11667 #endif
11668 x_clear_window_mouse_face,
11669 x_get_glyph_overhangs,
11670 x_fix_overlapping_area,
11671 x_draw_fringe_bitmap,
11672 0, /* define_fringe_bitmap */
11673 0, /* destroy_fringe_bitmap */
11674 x_per_char_metric,
11675 x_encode_char,
11676 x_compute_glyph_string_overhangs,
11677 x_draw_glyph_string,
11678 x_define_frame_cursor,
11679 x_clear_frame_area,
11680 x_draw_window_cursor,
11681 x_draw_vertical_window_border,
11682 x_shift_glyphs_for_insert
11683 };
11684
11685 void
11686 x_initialize ()
11687 {
11688 rif = &x_redisplay_interface;
11689
11690 clear_frame_hook = x_clear_frame;
11691 ins_del_lines_hook = x_ins_del_lines;
11692 delete_glyphs_hook = x_delete_glyphs;
11693 ring_bell_hook = XTring_bell;
11694 reset_terminal_modes_hook = XTreset_terminal_modes;
11695 set_terminal_modes_hook = XTset_terminal_modes;
11696 update_begin_hook = x_update_begin;
11697 update_end_hook = x_update_end;
11698 set_terminal_window_hook = XTset_terminal_window;
11699 read_socket_hook = XTread_socket;
11700 frame_up_to_date_hook = XTframe_up_to_date;
11701 mouse_position_hook = XTmouse_position;
11702 frame_rehighlight_hook = XTframe_rehighlight;
11703 frame_raise_lower_hook = XTframe_raise_lower;
11704 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
11705 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
11706 redeem_scroll_bar_hook = XTredeem_scroll_bar;
11707 judge_scroll_bars_hook = XTjudge_scroll_bars;
11708 fullscreen_hook = XTfullscreen_hook;
11709
11710 scroll_region_ok = 1; /* we'll scroll partial frames */
11711 char_ins_del_ok = 1;
11712 line_ins_del_ok = 1; /* we'll just blt 'em */
11713 fast_clear_end_of_line = 1; /* X does this well */
11714 memory_below_frame = 0; /* we don't remember what scrolls
11715 off the bottom */
11716 baud_rate = 19200;
11717
11718 x_noop_count = 0;
11719 last_tool_bar_item = -1;
11720 any_help_event_p = 0;
11721 ignore_next_mouse_click_timeout = 0;
11722 #ifdef HAVE_X_SM
11723 x_session_initialized = 0;
11724 #endif
11725
11726 #ifdef USE_GTK
11727 current_count = -1;
11728 #endif
11729
11730 /* Try to use interrupt input; if we can't, then start polling. */
11731 Fset_input_mode (Qt, Qnil, Qt, Qnil);
11732
11733 #ifdef USE_X_TOOLKIT
11734 XtToolkitInitialize ();
11735
11736 Xt_app_con = XtCreateApplicationContext ();
11737
11738 /* Register a converter from strings to pixels, which uses
11739 Emacs' color allocation infrastructure. */
11740 XtAppSetTypeConverter (Xt_app_con,
11741 XtRString, XtRPixel, cvt_string_to_pixel,
11742 cvt_string_to_pixel_args,
11743 XtNumber (cvt_string_to_pixel_args),
11744 XtCacheByDisplay, cvt_pixel_dtor);
11745
11746 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
11747 #endif
11748
11749 #ifdef USE_TOOLKIT_SCROLL_BARS
11750 #ifndef USE_GTK
11751 xaw3d_arrow_scroll = False;
11752 xaw3d_pick_top = True;
11753 #endif
11754 #endif
11755
11756 /* Note that there is no real way portable across R3/R4 to get the
11757 original error handler. */
11758 XSetErrorHandler (x_error_handler);
11759 XSetIOErrorHandler (x_io_error_quitter);
11760
11761 /* Disable Window Change signals; they are handled by X events. */
11762 #ifdef SIGWINCH
11763 signal (SIGWINCH, SIG_DFL);
11764 #endif /* SIGWINCH */
11765
11766 signal (SIGPIPE, x_connection_signal);
11767 }
11768
11769
11770 void
11771 syms_of_xterm ()
11772 {
11773 x_error_message = NULL;
11774
11775 staticpro (&x_display_name_list);
11776 x_display_name_list = Qnil;
11777
11778 staticpro (&last_mouse_scroll_bar);
11779 last_mouse_scroll_bar = Qnil;
11780
11781 staticpro (&Qvendor_specific_keysyms);
11782 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
11783
11784 staticpro (&Qlatin_1);
11785 Qlatin_1 = intern ("latin-1");
11786
11787 staticpro (&last_mouse_press_frame);
11788 last_mouse_press_frame = Qnil;
11789
11790 DEFVAR_BOOL ("x-use-underline-position-properties",
11791 &x_use_underline_position_properties,
11792 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
11793 A value of nil means ignore them. If you encounter fonts with bogus
11794 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
11795 to 4.1, set this to nil. */);
11796 x_use_underline_position_properties = 1;
11797
11798 DEFVAR_BOOL ("x-underline-at-descent-line",
11799 &x_underline_at_descent_line,
11800 doc: /* *Non-nil means to draw the underline at the same place as the descent line.
11801 A value of nil means to draw the underline according to the value of the
11802 variable `x-use-underline-position-properties', which is usually at the
11803 baseline level. The default value is nil. */);
11804 x_underline_at_descent_line = 0;
11805
11806 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
11807 &x_mouse_click_focus_ignore_position,
11808 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
11809 This variable is only used when the window manager requires that you
11810 click on a frame to select it (give it focus). In that case, a value
11811 of nil, means that the selected window and cursor position changes to
11812 reflect the mouse click position, while a non-nil value means that the
11813 selected window or cursor position is preserved. */);
11814 x_mouse_click_focus_ignore_position = 0;
11815
11816 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
11817 doc: /* What X toolkit scroll bars Emacs uses.
11818 A value of nil means Emacs doesn't use X toolkit scroll bars.
11819 Otherwise, value is a symbol describing the X toolkit. */);
11820 #ifdef USE_TOOLKIT_SCROLL_BARS
11821 #ifdef USE_MOTIF
11822 Vx_toolkit_scroll_bars = intern ("motif");
11823 #elif defined HAVE_XAW3D
11824 Vx_toolkit_scroll_bars = intern ("xaw3d");
11825 #elif USE_GTK
11826 Vx_toolkit_scroll_bars = intern ("gtk");
11827 #else
11828 Vx_toolkit_scroll_bars = intern ("xaw");
11829 #endif
11830 #else
11831 Vx_toolkit_scroll_bars = Qnil;
11832 #endif
11833
11834 staticpro (&last_mouse_motion_frame);
11835 last_mouse_motion_frame = Qnil;
11836
11837 Qmodifier_value = intern ("modifier-value");
11838 Qalt = intern ("alt");
11839 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
11840 Qhyper = intern ("hyper");
11841 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
11842 Qmeta = intern ("meta");
11843 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
11844 Qsuper = intern ("super");
11845 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
11846
11847 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
11848 doc: /* Which keys Emacs uses for the alt modifier.
11849 This should be one of the symbols `alt', `hyper', `meta', `super'.
11850 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
11851 is nil, which is the same as `alt'. */);
11852 Vx_alt_keysym = Qnil;
11853
11854 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
11855 doc: /* Which keys Emacs uses for the hyper modifier.
11856 This should be one of the symbols `alt', `hyper', `meta', `super'.
11857 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
11858 default is nil, which is the same as `hyper'. */);
11859 Vx_hyper_keysym = Qnil;
11860
11861 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
11862 doc: /* Which keys Emacs uses for the meta modifier.
11863 This should be one of the symbols `alt', `hyper', `meta', `super'.
11864 For example, `meta' means use the Meta_L and Meta_R keysyms. The
11865 default is nil, which is the same as `meta'. */);
11866 Vx_meta_keysym = Qnil;
11867
11868 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
11869 doc: /* Which keys Emacs uses for the super modifier.
11870 This should be one of the symbols `alt', `hyper', `meta', `super'.
11871 For example, `super' means use the Super_L and Super_R keysyms. The
11872 default is nil, which is the same as `super'. */);
11873 Vx_super_keysym = Qnil;
11874
11875 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
11876 doc: /* Hash table of character codes indexed by X keysym codes. */);
11877 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
11878 make_float (DEFAULT_REHASH_SIZE),
11879 make_float (DEFAULT_REHASH_THRESHOLD),
11880 Qnil, Qnil, Qnil);
11881 }
11882
11883 #endif /* HAVE_X_WINDOWS */
11884
11885 /* arch-tag: 6d4e4cb7-abc1-4302-9585-d84dcfb09d0f
11886 (do not change this comment) */