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