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