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