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