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