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