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