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