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