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