(struct redisplay_interface): Add active_p argument to
[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 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
3478 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
3479 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
3480 not force the value into range. */
3481
3482 void
3483 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
3484 FRAME_PTR f;
3485 register int pix_x, pix_y;
3486 register int *x, *y;
3487 XRectangle *bounds;
3488 int noclip;
3489 {
3490 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
3491 even for negative values. */
3492 if (pix_x < 0)
3493 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
3494 if (pix_y < 0)
3495 pix_y -= (f)->output_data.x->line_height - 1;
3496
3497 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
3498 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
3499
3500 if (bounds)
3501 {
3502 bounds->width = FONT_WIDTH (f->output_data.x->font);
3503 bounds->height = f->output_data.x->line_height;
3504 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
3505 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
3506 }
3507
3508 if (!noclip)
3509 {
3510 if (pix_x < 0)
3511 pix_x = 0;
3512 else if (pix_x > FRAME_WINDOW_WIDTH (f))
3513 pix_x = FRAME_WINDOW_WIDTH (f);
3514
3515 if (pix_y < 0)
3516 pix_y = 0;
3517 else if (pix_y > f->height)
3518 pix_y = f->height;
3519 }
3520
3521 *x = pix_x;
3522 *y = pix_y;
3523 }
3524
3525
3526 /* Given HPOS/VPOS in the current matrix of W, return corresponding
3527 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
3528 can't tell the positions because W's display is not up to date,
3529 return 0. */
3530
3531 int
3532 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
3533 struct window *w;
3534 int hpos, vpos;
3535 int *frame_x, *frame_y;
3536 {
3537 int success_p;
3538
3539 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
3540 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
3541
3542 if (display_completed)
3543 {
3544 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
3545 struct glyph *glyph = row->glyphs[TEXT_AREA];
3546 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
3547
3548 *frame_y = row->y;
3549 *frame_x = row->x;
3550 while (glyph < end)
3551 {
3552 *frame_x += glyph->pixel_width;
3553 ++glyph;
3554 }
3555
3556 success_p = 1;
3557 }
3558 else
3559 {
3560 *frame_y = *frame_x = 0;
3561 success_p = 0;
3562 }
3563
3564 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
3565 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
3566 return success_p;
3567 }
3568
3569
3570 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3571
3572 If the event is a button press, then note that we have grabbed
3573 the mouse. */
3574
3575 static Lisp_Object
3576 construct_mouse_click (result, event, f)
3577 struct input_event *result;
3578 XButtonEvent *event;
3579 struct frame *f;
3580 {
3581 /* Make the event type NO_EVENT; we'll change that when we decide
3582 otherwise. */
3583 result->kind = MOUSE_CLICK_EVENT;
3584 result->code = event->button - Button1;
3585 result->timestamp = event->time;
3586 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
3587 event->state)
3588 | (event->type == ButtonRelease
3589 ? up_modifier
3590 : down_modifier));
3591
3592 XSETINT (result->x, event->x);
3593 XSETINT (result->y, event->y);
3594 XSETFRAME (result->frame_or_window, f);
3595 result->arg = Qnil;
3596 return Qnil;
3597 }
3598
3599 \f
3600 /* Function to report a mouse movement to the mainstream Emacs code.
3601 The input handler calls this.
3602
3603 We have received a mouse movement event, which is given in *event.
3604 If the mouse is over a different glyph than it was last time, tell
3605 the mainstream emacs code by setting mouse_moved. If not, ask for
3606 another motion event, so we can check again the next time it moves. */
3607
3608 static XMotionEvent last_mouse_motion_event;
3609 static Lisp_Object last_mouse_motion_frame;
3610
3611 static void
3612 note_mouse_movement (frame, event)
3613 FRAME_PTR frame;
3614 XMotionEvent *event;
3615 {
3616 last_mouse_movement_time = event->time;
3617 last_mouse_motion_event = *event;
3618 XSETFRAME (last_mouse_motion_frame, frame);
3619
3620 if (event->window != FRAME_X_WINDOW (frame))
3621 {
3622 frame->mouse_moved = 1;
3623 last_mouse_scroll_bar = Qnil;
3624 note_mouse_highlight (frame, -1, -1);
3625 }
3626
3627 /* Has the mouse moved off the glyph it was on at the last sighting? */
3628 else if (event->x < last_mouse_glyph.x
3629 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
3630 || event->y < last_mouse_glyph.y
3631 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
3632 {
3633 frame->mouse_moved = 1;
3634 last_mouse_scroll_bar = Qnil;
3635 note_mouse_highlight (frame, event->x, event->y);
3636 }
3637 }
3638
3639 \f
3640 /************************************************************************
3641 Mouse Face
3642 ************************************************************************/
3643
3644 static void
3645 redo_mouse_highlight ()
3646 {
3647 if (!NILP (last_mouse_motion_frame)
3648 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3649 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3650 last_mouse_motion_event.x,
3651 last_mouse_motion_event.y);
3652 }
3653
3654
3655 static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
3656
3657
3658 /* Try to determine frame pixel position and size of the glyph under
3659 frame pixel coordinates X/Y on frame F . Return the position and
3660 size in *RECT. Value is non-zero if we could compute these
3661 values. */
3662
3663 static int
3664 glyph_rect (f, x, y, rect)
3665 struct frame *f;
3666 int x, y;
3667 XRectangle *rect;
3668 {
3669 Lisp_Object window;
3670 int found = 0;
3671
3672 window = window_from_coordinates (f, x, y, 0, 0);
3673 if (!NILP (window))
3674 {
3675 struct window *w = XWINDOW (window);
3676 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
3677 struct glyph_row *end = r + w->current_matrix->nrows - 1;
3678
3679 frame_to_window_pixel_xy (w, &x, &y);
3680
3681 for (; !found && r < end && r->enabled_p; ++r)
3682 if (r->y >= y)
3683 {
3684 struct glyph *g = r->glyphs[TEXT_AREA];
3685 struct glyph *end = g + r->used[TEXT_AREA];
3686 int gx;
3687
3688 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
3689 if (gx >= x)
3690 {
3691 rect->width = g->pixel_width;
3692 rect->height = r->height;
3693 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
3694 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
3695 found = 1;
3696 }
3697 }
3698 }
3699
3700 return found;
3701 }
3702
3703
3704 /* Return the current position of the mouse.
3705 *FP should be a frame which indicates which display to ask about.
3706
3707 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3708 and *PART to the frame, window, and scroll bar part that the mouse
3709 is over. Set *X and *Y to the portion and whole of the mouse's
3710 position on the scroll bar.
3711
3712 If the mouse movement started elsewhere, set *FP to the frame the
3713 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3714 the mouse is over.
3715
3716 Set *TIME to the server time-stamp for the time at which the mouse
3717 was at this position.
3718
3719 Don't store anything if we don't have a valid set of values to report.
3720
3721 This clears the mouse_moved flag, so we can wait for the next mouse
3722 movement. */
3723
3724 static void
3725 XTmouse_position (fp, insist, bar_window, part, x, y, time)
3726 FRAME_PTR *fp;
3727 int insist;
3728 Lisp_Object *bar_window;
3729 enum scroll_bar_part *part;
3730 Lisp_Object *x, *y;
3731 unsigned long *time;
3732 {
3733 FRAME_PTR f1;
3734
3735 BLOCK_INPUT;
3736
3737 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3738 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3739 else
3740 {
3741 Window root;
3742 int root_x, root_y;
3743
3744 Window dummy_window;
3745 int dummy;
3746
3747 Lisp_Object frame, tail;
3748
3749 /* Clear the mouse-moved flag for every frame on this display. */
3750 FOR_EACH_FRAME (tail, frame)
3751 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3752 XFRAME (frame)->mouse_moved = 0;
3753
3754 last_mouse_scroll_bar = Qnil;
3755
3756 /* Figure out which root window we're on. */
3757 XQueryPointer (FRAME_X_DISPLAY (*fp),
3758 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
3759
3760 /* The root window which contains the pointer. */
3761 &root,
3762
3763 /* Trash which we can't trust if the pointer is on
3764 a different screen. */
3765 &dummy_window,
3766
3767 /* The position on that root window. */
3768 &root_x, &root_y,
3769
3770 /* More trash we can't trust. */
3771 &dummy, &dummy,
3772
3773 /* Modifier keys and pointer buttons, about which
3774 we don't care. */
3775 (unsigned int *) &dummy);
3776
3777 /* Now we have a position on the root; find the innermost window
3778 containing the pointer. */
3779 {
3780 Window win, child;
3781 int win_x, win_y;
3782 int parent_x = 0, parent_y = 0;
3783 int count;
3784
3785 win = root;
3786
3787 /* XTranslateCoordinates can get errors if the window
3788 structure is changing at the same time this function
3789 is running. So at least we must not crash from them. */
3790
3791 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
3792
3793 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3794 && FRAME_LIVE_P (last_mouse_frame))
3795 {
3796 /* If mouse was grabbed on a frame, give coords for that frame
3797 even if the mouse is now outside it. */
3798 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3799
3800 /* From-window, to-window. */
3801 root, FRAME_X_WINDOW (last_mouse_frame),
3802
3803 /* From-position, to-position. */
3804 root_x, root_y, &win_x, &win_y,
3805
3806 /* Child of win. */
3807 &child);
3808 f1 = last_mouse_frame;
3809 }
3810 else
3811 {
3812 while (1)
3813 {
3814 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3815
3816 /* From-window, to-window. */
3817 root, win,
3818
3819 /* From-position, to-position. */
3820 root_x, root_y, &win_x, &win_y,
3821
3822 /* Child of win. */
3823 &child);
3824
3825 if (child == None || child == win)
3826 break;
3827
3828 win = child;
3829 parent_x = win_x;
3830 parent_y = win_y;
3831 }
3832
3833 /* Now we know that:
3834 win is the innermost window containing the pointer
3835 (XTC says it has no child containing the pointer),
3836 win_x and win_y are the pointer's position in it
3837 (XTC did this the last time through), and
3838 parent_x and parent_y are the pointer's position in win's parent.
3839 (They are what win_x and win_y were when win was child.
3840 If win is the root window, it has no parent, and
3841 parent_{x,y} are invalid, but that's okay, because we'll
3842 never use them in that case.) */
3843
3844 /* Is win one of our frames? */
3845 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3846
3847 #ifdef USE_X_TOOLKIT
3848 /* If we end up with the menu bar window, say it's not
3849 on the frame. */
3850 if (f1 != NULL
3851 && f1->output_data.x->menubar_widget
3852 && win == XtWindow (f1->output_data.x->menubar_widget))
3853 f1 = NULL;
3854 #endif /* USE_X_TOOLKIT */
3855 }
3856
3857 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
3858 f1 = 0;
3859
3860 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
3861
3862 /* If not, is it one of our scroll bars? */
3863 if (! f1)
3864 {
3865 struct scroll_bar *bar = x_window_to_scroll_bar (win);
3866
3867 if (bar)
3868 {
3869 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3870 win_x = parent_x;
3871 win_y = parent_y;
3872 }
3873 }
3874
3875 if (f1 == 0 && insist > 0)
3876 f1 = SELECTED_FRAME ();
3877
3878 if (f1)
3879 {
3880 /* Ok, we found a frame. Store all the values.
3881 last_mouse_glyph is a rectangle used to reduce the
3882 generation of mouse events. To not miss any motion
3883 events, we must divide the frame into rectangles of the
3884 size of the smallest character that could be displayed
3885 on it, i.e. into the same rectangles that matrices on
3886 the frame are divided into. */
3887
3888 int width, height, gx, gy;
3889 XRectangle rect;
3890
3891 if (glyph_rect (f1, win_x, win_y, &rect))
3892 last_mouse_glyph = rect;
3893 else
3894 {
3895 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
3896 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
3897 gx = win_x;
3898 gy = win_y;
3899
3900 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
3901 round down even for negative values. */
3902 if (gx < 0)
3903 gx -= width - 1;
3904 if (gy < 0)
3905 gy -= height - 1;
3906 gx = (gx + width - 1) / width * width;
3907 gy = (gy + height - 1) / height * height;
3908
3909 last_mouse_glyph.width = width;
3910 last_mouse_glyph.height = height;
3911 last_mouse_glyph.x = gx;
3912 last_mouse_glyph.y = gy;
3913 }
3914
3915 *bar_window = Qnil;
3916 *part = 0;
3917 *fp = f1;
3918 XSETINT (*x, win_x);
3919 XSETINT (*y, win_y);
3920 *time = last_mouse_movement_time;
3921 }
3922 }
3923 }
3924
3925 UNBLOCK_INPUT;
3926 }
3927
3928
3929 \f
3930 /***********************************************************************
3931 Scroll bars
3932 ***********************************************************************/
3933
3934 /* Scroll bar support. */
3935
3936 /* Given an X window ID, find the struct scroll_bar which manages it.
3937 This can be called in GC, so we have to make sure to strip off mark
3938 bits. */
3939
3940 static struct scroll_bar *
3941 x_window_to_scroll_bar (window_id)
3942 Window window_id;
3943 {
3944 Lisp_Object tail;
3945
3946 #ifdef USE_GTK
3947 window_id = (Window) xg_get_scroll_id_for_window (window_id);
3948 #endif /* USE_GTK */
3949
3950 for (tail = Vframe_list;
3951 XGCTYPE (tail) == Lisp_Cons;
3952 tail = XCDR (tail))
3953 {
3954 Lisp_Object frame, bar, condemned;
3955
3956 frame = XCAR (tail);
3957 /* All elements of Vframe_list should be frames. */
3958 if (! GC_FRAMEP (frame))
3959 abort ();
3960
3961 /* Scan this frame's scroll bar list for a scroll bar with the
3962 right window ID. */
3963 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
3964 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
3965 /* This trick allows us to search both the ordinary and
3966 condemned scroll bar lists with one loop. */
3967 ! GC_NILP (bar) || (bar = condemned,
3968 condemned = Qnil,
3969 ! GC_NILP (bar));
3970 bar = XSCROLL_BAR (bar)->next)
3971 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
3972 return XSCROLL_BAR (bar);
3973 }
3974
3975 return 0;
3976 }
3977
3978
3979 #if defined USE_LUCID
3980
3981 /* Return the Lucid menu bar WINDOW is part of. Return null
3982 if WINDOW is not part of a menu bar. */
3983
3984 static Widget
3985 x_window_to_menu_bar (window)
3986 Window window;
3987 {
3988 Lisp_Object tail;
3989
3990 for (tail = Vframe_list;
3991 XGCTYPE (tail) == Lisp_Cons;
3992 tail = XCDR (tail))
3993 {
3994 Lisp_Object frame = XCAR (tail);
3995 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
3996
3997 if (menu_bar && xlwmenu_window_p (menu_bar, window))
3998 return menu_bar;
3999 }
4000
4001 return NULL;
4002 }
4003
4004 #endif /* USE_LUCID */
4005
4006 \f
4007 /************************************************************************
4008 Toolkit scroll bars
4009 ************************************************************************/
4010
4011 #ifdef USE_TOOLKIT_SCROLL_BARS
4012
4013 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
4014 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
4015 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
4016 struct scroll_bar *));
4017 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
4018 int, int, int));
4019
4020
4021 /* Lisp window being scrolled. Set when starting to interact with
4022 a toolkit scroll bar, reset to nil when ending the interaction. */
4023
4024 static Lisp_Object window_being_scrolled;
4025
4026 /* Last scroll bar part sent in xm_scroll_callback. */
4027
4028 static int last_scroll_bar_part;
4029
4030 /* Whether this is an Xaw with arrow-scrollbars. This should imply
4031 that movements of 1/20 of the screen size are mapped to up/down. */
4032
4033 #ifndef USE_GTK
4034 /* Id of action hook installed for scroll bars. */
4035
4036 static XtActionHookId action_hook_id;
4037
4038 static Boolean xaw3d_arrow_scroll;
4039
4040 /* Whether the drag scrolling maintains the mouse at the top of the
4041 thumb. If not, resizing the thumb needs to be done more carefully
4042 to avoid jerkyness. */
4043
4044 static Boolean xaw3d_pick_top;
4045
4046
4047 /* Action hook installed via XtAppAddActionHook when toolkit scroll
4048 bars are used.. The hook is responsible for detecting when
4049 the user ends an interaction with the scroll bar, and generates
4050 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
4051
4052 static void
4053 xt_action_hook (widget, client_data, action_name, event, params,
4054 num_params)
4055 Widget widget;
4056 XtPointer client_data;
4057 String action_name;
4058 XEvent *event;
4059 String *params;
4060 Cardinal *num_params;
4061 {
4062 int scroll_bar_p;
4063 char *end_action;
4064
4065 #ifdef USE_MOTIF
4066 scroll_bar_p = XmIsScrollBar (widget);
4067 end_action = "Release";
4068 #else /* !USE_MOTIF i.e. use Xaw */
4069 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4070 end_action = "EndScroll";
4071 #endif /* USE_MOTIF */
4072
4073 if (scroll_bar_p
4074 && strcmp (action_name, end_action) == 0
4075 && WINDOWP (window_being_scrolled))
4076 {
4077 struct window *w;
4078
4079 x_send_scroll_bar_event (window_being_scrolled,
4080 scroll_bar_end_scroll, 0, 0);
4081 w = XWINDOW (window_being_scrolled);
4082 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
4083 window_being_scrolled = Qnil;
4084 last_scroll_bar_part = -1;
4085
4086 /* Xt timeouts no longer needed. */
4087 toolkit_scroll_bar_interaction = 0;
4088 }
4089 }
4090 #endif /* not USE_GTK */
4091
4092 /* A vector of windows used for communication between
4093 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
4094
4095 static struct window **scroll_bar_windows;
4096 static int scroll_bar_windows_size;
4097
4098
4099 /* Send a client message with message type Xatom_Scrollbar for a
4100 scroll action to the frame of WINDOW. PART is a value identifying
4101 the part of the scroll bar that was clicked on. PORTION is the
4102 amount to scroll of a whole of WHOLE. */
4103
4104 static void
4105 x_send_scroll_bar_event (window, part, portion, whole)
4106 Lisp_Object window;
4107 int part, portion, whole;
4108 {
4109 XEvent event;
4110 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
4111 struct window *w = XWINDOW (window);
4112 struct frame *f = XFRAME (w->frame);
4113 int i;
4114
4115 BLOCK_INPUT;
4116
4117 /* Construct a ClientMessage event to send to the frame. */
4118 ev->type = ClientMessage;
4119 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
4120 ev->display = FRAME_X_DISPLAY (f);
4121 ev->window = FRAME_X_WINDOW (f);
4122 ev->format = 32;
4123
4124 /* We can only transfer 32 bits in the XClientMessageEvent, which is
4125 not enough to store a pointer or Lisp_Object on a 64 bit system.
4126 So, store the window in scroll_bar_windows and pass the index
4127 into that array in the event. */
4128 for (i = 0; i < scroll_bar_windows_size; ++i)
4129 if (scroll_bar_windows[i] == NULL)
4130 break;
4131
4132 if (i == scroll_bar_windows_size)
4133 {
4134 int new_size = max (10, 2 * scroll_bar_windows_size);
4135 size_t nbytes = new_size * sizeof *scroll_bar_windows;
4136 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
4137
4138 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
4139 nbytes);
4140 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
4141 scroll_bar_windows_size = new_size;
4142 }
4143
4144 scroll_bar_windows[i] = w;
4145 ev->data.l[0] = (long) i;
4146 ev->data.l[1] = (long) part;
4147 ev->data.l[2] = (long) 0;
4148 ev->data.l[3] = (long) portion;
4149 ev->data.l[4] = (long) whole;
4150
4151 /* Make Xt timeouts work while the scroll bar is active. */
4152 toolkit_scroll_bar_interaction = 1;
4153
4154 /* Setting the event mask to zero means that the message will
4155 be sent to the client that created the window, and if that
4156 window no longer exists, no event will be sent. */
4157 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4158 UNBLOCK_INPUT;
4159 }
4160
4161
4162 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4163 in *IEVENT. */
4164
4165 static void
4166 x_scroll_bar_to_input_event (event, ievent)
4167 XEvent *event;
4168 struct input_event *ievent;
4169 {
4170 XClientMessageEvent *ev = (XClientMessageEvent *) event;
4171 Lisp_Object window;
4172 struct frame *f;
4173 struct window *w;
4174
4175 w = scroll_bar_windows[ev->data.l[0]];
4176 scroll_bar_windows[ev->data.l[0]] = NULL;
4177
4178 XSETWINDOW (window, w);
4179 f = XFRAME (w->frame);
4180
4181 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4182 ievent->frame_or_window = window;
4183 ievent->arg = Qnil;
4184 #ifdef USE_GTK
4185 ievent->timestamp = CurrentTime;
4186 #else
4187 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
4188 #endif
4189 ievent->part = ev->data.l[1];
4190 ievent->code = ev->data.l[2];
4191 ievent->x = make_number ((int) ev->data.l[3]);
4192 ievent->y = make_number ((int) ev->data.l[4]);
4193 ievent->modifiers = 0;
4194 }
4195
4196
4197 #ifdef USE_MOTIF
4198
4199 /* Minimum and maximum values used for Motif scroll bars. */
4200
4201 #define XM_SB_MIN 1
4202 #define XM_SB_MAX 10000000
4203 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
4204
4205
4206 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4207 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4208 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4209
4210 static void
4211 xm_scroll_callback (widget, client_data, call_data)
4212 Widget widget;
4213 XtPointer client_data, call_data;
4214 {
4215 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4216 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
4217 int part = -1, whole = 0, portion = 0;
4218
4219 switch (cs->reason)
4220 {
4221 case XmCR_DECREMENT:
4222 bar->dragging = Qnil;
4223 part = scroll_bar_up_arrow;
4224 break;
4225
4226 case XmCR_INCREMENT:
4227 bar->dragging = Qnil;
4228 part = scroll_bar_down_arrow;
4229 break;
4230
4231 case XmCR_PAGE_DECREMENT:
4232 bar->dragging = Qnil;
4233 part = scroll_bar_above_handle;
4234 break;
4235
4236 case XmCR_PAGE_INCREMENT:
4237 bar->dragging = Qnil;
4238 part = scroll_bar_below_handle;
4239 break;
4240
4241 case XmCR_TO_TOP:
4242 bar->dragging = Qnil;
4243 part = scroll_bar_to_top;
4244 break;
4245
4246 case XmCR_TO_BOTTOM:
4247 bar->dragging = Qnil;
4248 part = scroll_bar_to_bottom;
4249 break;
4250
4251 case XmCR_DRAG:
4252 {
4253 int slider_size;
4254 int dragging_down_p = (INTEGERP (bar->dragging)
4255 && XINT (bar->dragging) <= cs->value);
4256
4257 /* Get the slider size. */
4258 BLOCK_INPUT;
4259 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4260 UNBLOCK_INPUT;
4261
4262 whole = XM_SB_RANGE - slider_size;
4263 portion = min (cs->value - XM_SB_MIN, whole);
4264 part = scroll_bar_handle;
4265 bar->dragging = make_number (cs->value);
4266 }
4267 break;
4268
4269 case XmCR_VALUE_CHANGED:
4270 break;
4271 };
4272
4273 if (part >= 0)
4274 {
4275 window_being_scrolled = bar->window;
4276 last_scroll_bar_part = part;
4277 x_send_scroll_bar_event (bar->window, part, portion, whole);
4278 }
4279 }
4280
4281
4282 #else /* !USE_MOTIF, i.e. Xaw or GTK */
4283 #ifdef USE_GTK
4284 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4285 bar adjustment widget. DATA is a pointer to the scroll_bar structure. */
4286
4287 static void
4288 xg_scroll_callback (widget, data)
4289 GtkWidget *widget;
4290 gpointer data;
4291 {
4292 struct scroll_bar *bar = (struct scroll_bar *) data;
4293 gdouble previous;
4294 gdouble position;
4295 gdouble *p;
4296 int diff;
4297
4298 int part = -1, whole = 0, portion = 0;
4299 GtkAdjustment *adj = GTK_ADJUSTMENT (widget);
4300
4301 if (xg_ignore_gtk_scrollbar) return;
4302
4303 position = gtk_adjustment_get_value (adj);
4304
4305 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
4306 if (! p)
4307 {
4308 p = (gdouble*) xmalloc (sizeof (gdouble));
4309 *p = XG_SB_MIN;
4310 g_object_set_data (G_OBJECT (widget), XG_LAST_SB_DATA, p);
4311 }
4312
4313 previous = *p;
4314 *p = position;
4315
4316 diff = (int) (position - previous);
4317
4318 if (diff == (int) adj->step_increment)
4319 {
4320 part = scroll_bar_down_arrow;
4321 bar->dragging = Qnil;
4322 }
4323 else if (-diff == (int) adj->step_increment)
4324 {
4325 part = scroll_bar_up_arrow;
4326 bar->dragging = Qnil;
4327 }
4328 else if (diff == (int) adj->page_increment)
4329 {
4330 part = scroll_bar_below_handle;
4331 bar->dragging = Qnil;
4332 }
4333 else if (-diff == (int) adj->page_increment)
4334 {
4335 part = scroll_bar_above_handle;
4336 bar->dragging = Qnil;
4337 }
4338 else
4339 {
4340 part = scroll_bar_handle;
4341 whole = adj->upper - adj->page_size;
4342 portion = min ((int)position, whole);
4343 bar->dragging = make_number ((int)portion);
4344 }
4345
4346 if (part >= 0)
4347 {
4348 window_being_scrolled = bar->window;
4349 last_scroll_bar_part = part;
4350 x_send_scroll_bar_event (bar->window, part, portion, whole);
4351 }
4352 }
4353
4354 #else /* not USE_GTK */
4355
4356 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4357 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4358 scroll bar struct. CALL_DATA is a pointer to a float saying where
4359 the thumb is. */
4360
4361 static void
4362 xaw_jump_callback (widget, client_data, call_data)
4363 Widget widget;
4364 XtPointer client_data, call_data;
4365 {
4366 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4367 float top = *(float *) call_data;
4368 float shown;
4369 int whole, portion, height;
4370 int part;
4371
4372 /* Get the size of the thumb, a value between 0 and 1. */
4373 BLOCK_INPUT;
4374 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4375 UNBLOCK_INPUT;
4376
4377 whole = 10000000;
4378 portion = shown < 1 ? top * whole : 0;
4379
4380 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
4381 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4382 the bottom, so we force the scrolling whenever we see that we're
4383 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4384 we try to ensure that we always stay two pixels away from the
4385 bottom). */
4386 part = scroll_bar_down_arrow;
4387 else
4388 part = scroll_bar_handle;
4389
4390 window_being_scrolled = bar->window;
4391 bar->dragging = make_number (portion);
4392 last_scroll_bar_part = part;
4393 x_send_scroll_bar_event (bar->window, part, portion, whole);
4394 }
4395
4396
4397 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4398 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4399 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4400 the scroll bar. CALL_DATA is an integer specifying the action that
4401 has taken place. Its magnitude is in the range 0..height of the
4402 scroll bar. Negative values mean scroll towards buffer start.
4403 Values < height of scroll bar mean line-wise movement. */
4404
4405 static void
4406 xaw_scroll_callback (widget, client_data, call_data)
4407 Widget widget;
4408 XtPointer client_data, call_data;
4409 {
4410 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4411 /* The position really is stored cast to a pointer. */
4412 int position = (long) call_data;
4413 Dimension height;
4414 int part;
4415
4416 /* Get the height of the scroll bar. */
4417 BLOCK_INPUT;
4418 XtVaGetValues (widget, XtNheight, &height, NULL);
4419 UNBLOCK_INPUT;
4420
4421 if (abs (position) >= height)
4422 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4423
4424 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4425 it maps line-movement to call_data = max(5, height/20). */
4426 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
4427 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4428 else
4429 part = scroll_bar_move_ratio;
4430
4431 window_being_scrolled = bar->window;
4432 bar->dragging = Qnil;
4433 last_scroll_bar_part = part;
4434 x_send_scroll_bar_event (bar->window, part, position, height);
4435 }
4436
4437 #endif /* not USE_GTK */
4438 #endif /* not USE_MOTIF */
4439
4440 #define SCROLL_BAR_NAME "verticalScrollBar"
4441
4442 /* Create the widget for scroll bar BAR on frame F. Record the widget
4443 and X window of the scroll bar in BAR. */
4444
4445 #ifdef USE_GTK
4446 static void
4447 x_create_toolkit_scroll_bar (f, bar)
4448 struct frame *f;
4449 struct scroll_bar *bar;
4450 {
4451 char *scroll_bar_name = SCROLL_BAR_NAME;
4452
4453 BLOCK_INPUT;
4454 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4455 scroll_bar_name);
4456 UNBLOCK_INPUT;
4457 }
4458
4459 #else /* not USE_GTK */
4460
4461 static void
4462 x_create_toolkit_scroll_bar (f, bar)
4463 struct frame *f;
4464 struct scroll_bar *bar;
4465 {
4466 Window xwindow;
4467 Widget widget;
4468 Arg av[20];
4469 int ac = 0;
4470 char *scroll_bar_name = SCROLL_BAR_NAME;
4471 unsigned long pixel;
4472
4473 BLOCK_INPUT;
4474
4475 #ifdef USE_MOTIF
4476 /* Set resources. Create the widget. */
4477 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4478 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
4479 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4480 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4481 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4482 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4483 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4484
4485 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4486 if (pixel != -1)
4487 {
4488 XtSetArg (av[ac], XmNforeground, pixel);
4489 ++ac;
4490 }
4491
4492 pixel = f->output_data.x->scroll_bar_background_pixel;
4493 if (pixel != -1)
4494 {
4495 XtSetArg (av[ac], XmNbackground, pixel);
4496 ++ac;
4497 }
4498
4499 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4500 scroll_bar_name, av, ac);
4501
4502 /* Add one callback for everything that can happen. */
4503 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4504 (XtPointer) bar);
4505 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4506 (XtPointer) bar);
4507 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4508 (XtPointer) bar);
4509 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4510 (XtPointer) bar);
4511 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4512 (XtPointer) bar);
4513 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4514 (XtPointer) bar);
4515 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4516 (XtPointer) bar);
4517
4518 /* Realize the widget. Only after that is the X window created. */
4519 XtRealizeWidget (widget);
4520
4521 /* Set the cursor to an arrow. I didn't find a resource to do that.
4522 And I'm wondering why it hasn't an arrow cursor by default. */
4523 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4524 f->output_data.x->nontext_cursor);
4525
4526 #else /* !USE_MOTIF i.e. use Xaw */
4527
4528 /* Set resources. Create the widget. The background of the
4529 Xaw3d scroll bar widget is a little bit light for my taste.
4530 We don't alter it here to let users change it according
4531 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4532 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4533 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4534 /* For smoother scrolling with Xaw3d -sm */
4535 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4536
4537 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4538 if (pixel != -1)
4539 {
4540 XtSetArg (av[ac], XtNforeground, pixel);
4541 ++ac;
4542 }
4543
4544 pixel = f->output_data.x->scroll_bar_background_pixel;
4545 if (pixel != -1)
4546 {
4547 XtSetArg (av[ac], XtNbackground, pixel);
4548 ++ac;
4549 }
4550
4551 /* Top/bottom shadow colors. */
4552
4553 /* Allocate them, if necessary. */
4554 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4555 {
4556 pixel = f->output_data.x->scroll_bar_background_pixel;
4557 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4558 &pixel, 1.2, 0x8000))
4559 pixel = -1;
4560 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4561 }
4562 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4563 {
4564 pixel = f->output_data.x->scroll_bar_background_pixel;
4565 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4566 &pixel, 0.6, 0x4000))
4567 pixel = -1;
4568 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4569 }
4570
4571 /* Tell the toolkit about them. */
4572 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4573 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4574 /* We tried to allocate a color for the top/bottom shadow, and
4575 failed, so tell Xaw3d to use dithering instead. */
4576 {
4577 XtSetArg (av[ac], XtNbeNiceToColormap, True);
4578 ++ac;
4579 }
4580 else
4581 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4582 be more consistent with other emacs 3d colors, and since Xaw3d is
4583 not good at dealing with allocation failure. */
4584 {
4585 /* This tells Xaw3d to use real colors instead of dithering for
4586 the shadows. */
4587 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4588 ++ac;
4589
4590 /* Specify the colors. */
4591 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4592 if (pixel != -1)
4593 {
4594 XtSetArg (av[ac], "topShadowPixel", pixel);
4595 ++ac;
4596 }
4597 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4598 if (pixel != -1)
4599 {
4600 XtSetArg (av[ac], "bottomShadowPixel", pixel);
4601 ++ac;
4602 }
4603 }
4604
4605 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4606 f->output_data.x->edit_widget, av, ac);
4607
4608 {
4609 char *initial = "";
4610 char *val = initial;
4611 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4612 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4613 if (val == initial)
4614 { /* ARROW_SCROLL */
4615 xaw3d_arrow_scroll = True;
4616 /* Isn't that just a personal preference ? -sm */
4617 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4618 }
4619 }
4620
4621 /* Define callbacks. */
4622 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4623 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4624 (XtPointer) bar);
4625
4626 /* Realize the widget. Only after that is the X window created. */
4627 XtRealizeWidget (widget);
4628
4629 #endif /* !USE_MOTIF */
4630
4631 /* Install an action hook that lets us detect when the user
4632 finishes interacting with a scroll bar. */
4633 if (action_hook_id == 0)
4634 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4635
4636 /* Remember X window and widget in the scroll bar vector. */
4637 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4638 xwindow = XtWindow (widget);
4639 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
4640
4641 UNBLOCK_INPUT;
4642 }
4643 #endif /* not USE_GTK */
4644
4645
4646 /* Set the thumb size and position of scroll bar BAR. We are currently
4647 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4648
4649 #ifdef USE_GTK
4650 static void
4651 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4652 struct scroll_bar *bar;
4653 int portion, position, whole;
4654 {
4655 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4656 }
4657
4658 #else /* not USE_GTK */
4659 static void
4660 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4661 struct scroll_bar *bar;
4662 int portion, position, whole;
4663 {
4664 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4665 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4666 float top, shown;
4667
4668 BLOCK_INPUT;
4669
4670 #ifdef USE_MOTIF
4671
4672 /* We use an estimate of 30 chars per line rather than the real
4673 `portion' value. This has the disadvantage that the thumb size
4674 is not very representative, but it makes our life a lot easier.
4675 Otherwise, we have to constantly adjust the thumb size, which
4676 we can't always do quickly enough: while dragging, the size of
4677 the thumb might prevent the user from dragging the thumb all the
4678 way to the end. but Motif and some versions of Xaw3d don't allow
4679 updating the thumb size while dragging. Also, even if we can update
4680 its size, the update will often happen too late.
4681 If you don't believe it, check out revision 1.650 of xterm.c to see
4682 what hoops we were going through and the still poor behavior we got. */
4683 portion = XFASTINT (XWINDOW (bar->window)->height) * 30;
4684 /* When the thumb is at the bottom, position == whole.
4685 So we need to increase `whole' to make space for the thumb. */
4686 whole += portion;
4687
4688 if (whole <= 0)
4689 top = 0, shown = 1;
4690 else
4691 {
4692 top = (float) position / whole;
4693 shown = (float) portion / whole;
4694 }
4695
4696 if (NILP (bar->dragging))
4697 {
4698 int size, value;
4699
4700 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4701 is the scroll bar's maximum and MIN is the scroll bar's minimum
4702 value. */
4703 size = shown * XM_SB_RANGE;
4704 size = min (size, XM_SB_RANGE);
4705 size = max (size, 1);
4706
4707 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4708 value = top * XM_SB_RANGE;
4709 value = min (value, XM_SB_MAX - size);
4710 value = max (value, XM_SB_MIN);
4711
4712 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4713 }
4714 #else /* !USE_MOTIF i.e. use Xaw */
4715
4716 if (whole == 0)
4717 top = 0, shown = 1;
4718 else
4719 {
4720 top = (float) position / whole;
4721 shown = (float) portion / whole;
4722 }
4723
4724 {
4725 float old_top, old_shown;
4726 Dimension height;
4727 XtVaGetValues (widget,
4728 XtNtopOfThumb, &old_top,
4729 XtNshown, &old_shown,
4730 XtNheight, &height,
4731 NULL);
4732
4733 /* Massage the top+shown values. */
4734 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
4735 top = max (0, min (1, top));
4736 else
4737 top = old_top;
4738 /* Keep two pixels available for moving the thumb down. */
4739 shown = max (0, min (1 - top - (2.0 / height), shown));
4740
4741 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
4742 check that your system's configuration file contains a define
4743 for `NARROWPROTO'. See s/freebsd.h for an example. */
4744 if (top != old_top || shown != old_shown)
4745 {
4746 if (NILP (bar->dragging))
4747 XawScrollbarSetThumb (widget, top, shown);
4748 else
4749 {
4750 #ifdef HAVE_XAW3D
4751 ScrollbarWidget sb = (ScrollbarWidget) widget;
4752 int scroll_mode = 0;
4753
4754 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
4755 if (xaw3d_arrow_scroll)
4756 {
4757 /* Xaw3d stupidly ignores resize requests while dragging
4758 so we have to make it believe it's not in dragging mode. */
4759 scroll_mode = sb->scrollbar.scroll_mode;
4760 if (scroll_mode == 2)
4761 sb->scrollbar.scroll_mode = 0;
4762 }
4763 #endif
4764 /* Try to make the scrolling a tad smoother. */
4765 if (!xaw3d_pick_top)
4766 shown = min (shown, old_shown);
4767
4768 XawScrollbarSetThumb (widget, top, shown);
4769
4770 #ifdef HAVE_XAW3D
4771 if (xaw3d_arrow_scroll && scroll_mode == 2)
4772 sb->scrollbar.scroll_mode = scroll_mode;
4773 #endif
4774 }
4775 }
4776 }
4777 #endif /* !USE_MOTIF */
4778
4779 UNBLOCK_INPUT;
4780 }
4781 #endif /* not USE_GTK */
4782
4783 #endif /* USE_TOOLKIT_SCROLL_BARS */
4784
4785
4786 \f
4787 /************************************************************************
4788 Scroll bars, general
4789 ************************************************************************/
4790
4791 /* Create a scroll bar and return the scroll bar vector for it. W is
4792 the Emacs window on which to create the scroll bar. TOP, LEFT,
4793 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4794 scroll bar. */
4795
4796 static struct scroll_bar *
4797 x_scroll_bar_create (w, top, left, width, height)
4798 struct window *w;
4799 int top, left, width, height;
4800 {
4801 struct frame *f = XFRAME (w->frame);
4802 struct scroll_bar *bar
4803 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
4804
4805 BLOCK_INPUT;
4806
4807 #ifdef USE_TOOLKIT_SCROLL_BARS
4808 x_create_toolkit_scroll_bar (f, bar);
4809 #else /* not USE_TOOLKIT_SCROLL_BARS */
4810 {
4811 XSetWindowAttributes a;
4812 unsigned long mask;
4813 Window window;
4814
4815 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4816 if (a.background_pixel == -1)
4817 a.background_pixel = f->output_data.x->background_pixel;
4818
4819 a.event_mask = (ButtonPressMask | ButtonReleaseMask
4820 | ButtonMotionMask | PointerMotionHintMask
4821 | ExposureMask);
4822 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
4823
4824 mask = (CWBackPixel | CWEventMask | CWCursor);
4825
4826 /* Clear the area of W that will serve as a scroll bar. This is
4827 for the case that a window has been split horizontally. In
4828 this case, no clear_frame is generated to reduce flickering. */
4829 if (width > 0 && height > 0)
4830 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4831 left, top, width,
4832 window_box_height (w), False);
4833
4834 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4835 /* Position and size of scroll bar. */
4836 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4837 top,
4838 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4839 height,
4840 /* Border width, depth, class, and visual. */
4841 0,
4842 CopyFromParent,
4843 CopyFromParent,
4844 CopyFromParent,
4845 /* Attributes. */
4846 mask, &a);
4847 SET_SCROLL_BAR_X_WINDOW (bar, window);
4848 }
4849 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4850
4851 XSETWINDOW (bar->window, w);
4852 XSETINT (bar->top, top);
4853 XSETINT (bar->left, left);
4854 XSETINT (bar->width, width);
4855 XSETINT (bar->height, height);
4856 XSETINT (bar->start, 0);
4857 XSETINT (bar->end, 0);
4858 bar->dragging = Qnil;
4859
4860 /* Add bar to its frame's list of scroll bars. */
4861 bar->next = FRAME_SCROLL_BARS (f);
4862 bar->prev = Qnil;
4863 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
4864 if (!NILP (bar->next))
4865 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4866
4867 /* Map the window/widget. */
4868 #ifdef USE_TOOLKIT_SCROLL_BARS
4869 {
4870 #ifdef USE_GTK
4871 xg_update_scrollbar_pos (f,
4872 SCROLL_BAR_X_WINDOW (bar),
4873 top,
4874 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4875 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4876 max (height, 1));
4877 xg_show_scroll_bar (SCROLL_BAR_X_WINDOW (bar));
4878 #else /* not USE_GTK */
4879 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4880 XtConfigureWidget (scroll_bar,
4881 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4882 top,
4883 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4884 max (height, 1), 0);
4885 XtMapWidget (scroll_bar);
4886 #endif /* not USE_GTK */
4887 }
4888 #else /* not USE_TOOLKIT_SCROLL_BARS */
4889 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
4890 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4891
4892 UNBLOCK_INPUT;
4893 return bar;
4894 }
4895
4896
4897 /* Draw BAR's handle in the proper position.
4898
4899 If the handle is already drawn from START to END, don't bother
4900 redrawing it, unless REBUILD is non-zero; in that case, always
4901 redraw it. (REBUILD is handy for drawing the handle after expose
4902 events.)
4903
4904 Normally, we want to constrain the start and end of the handle to
4905 fit inside its rectangle, but if the user is dragging the scroll
4906 bar handle, we want to let them drag it down all the way, so that
4907 the bar's top is as far down as it goes; otherwise, there's no way
4908 to move to the very end of the buffer. */
4909
4910 #ifndef USE_TOOLKIT_SCROLL_BARS
4911
4912 static void
4913 x_scroll_bar_set_handle (bar, start, end, rebuild)
4914 struct scroll_bar *bar;
4915 int start, end;
4916 int rebuild;
4917 {
4918 int dragging = ! NILP (bar->dragging);
4919 Window w = SCROLL_BAR_X_WINDOW (bar);
4920 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4921 GC gc = f->output_data.x->normal_gc;
4922
4923 /* If the display is already accurate, do nothing. */
4924 if (! rebuild
4925 && start == XINT (bar->start)
4926 && end == XINT (bar->end))
4927 return;
4928
4929 BLOCK_INPUT;
4930
4931 {
4932 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
4933 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
4934 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
4935
4936 /* Make sure the values are reasonable, and try to preserve
4937 the distance between start and end. */
4938 {
4939 int length = end - start;
4940
4941 if (start < 0)
4942 start = 0;
4943 else if (start > top_range)
4944 start = top_range;
4945 end = start + length;
4946
4947 if (end < start)
4948 end = start;
4949 else if (end > top_range && ! dragging)
4950 end = top_range;
4951 }
4952
4953 /* Store the adjusted setting in the scroll bar. */
4954 XSETINT (bar->start, start);
4955 XSETINT (bar->end, end);
4956
4957 /* Clip the end position, just for display. */
4958 if (end > top_range)
4959 end = top_range;
4960
4961 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
4962 below top positions, to make sure the handle is always at least
4963 that many pixels tall. */
4964 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
4965
4966 /* Draw the empty space above the handle. Note that we can't clear
4967 zero-height areas; that means "clear to end of window." */
4968 if (0 < start)
4969 x_clear_area (FRAME_X_DISPLAY (f), w,
4970 /* x, y, width, height, and exposures. */
4971 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4972 VERTICAL_SCROLL_BAR_TOP_BORDER,
4973 inside_width, start,
4974 False);
4975
4976 /* Change to proper foreground color if one is specified. */
4977 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
4978 XSetForeground (FRAME_X_DISPLAY (f), gc,
4979 f->output_data.x->scroll_bar_foreground_pixel);
4980
4981 /* Draw the handle itself. */
4982 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
4983 /* x, y, width, height */
4984 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4985 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
4986 inside_width, end - start);
4987
4988 /* Restore the foreground color of the GC if we changed it above. */
4989 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
4990 XSetForeground (FRAME_X_DISPLAY (f), gc,
4991 f->output_data.x->foreground_pixel);
4992
4993 /* Draw the empty space below the handle. Note that we can't
4994 clear zero-height areas; that means "clear to end of window." */
4995 if (end < inside_height)
4996 x_clear_area (FRAME_X_DISPLAY (f), w,
4997 /* x, y, width, height, and exposures. */
4998 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4999 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
5000 inside_width, inside_height - end,
5001 False);
5002
5003 }
5004
5005 UNBLOCK_INPUT;
5006 }
5007
5008 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5009
5010 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
5011 nil. */
5012
5013 static void
5014 x_scroll_bar_remove (bar)
5015 struct scroll_bar *bar;
5016 {
5017 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5018 BLOCK_INPUT;
5019
5020 #ifdef USE_TOOLKIT_SCROLL_BARS
5021 #ifdef USE_GTK
5022 xg_remove_scroll_bar (f, SCROLL_BAR_X_WINDOW (bar));
5023 #else /* not USE_GTK */
5024 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
5025 #endif /* not USE_GTK */
5026 #else
5027 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
5028 #endif
5029
5030 /* Disassociate this scroll bar from its window. */
5031 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
5032
5033 UNBLOCK_INPUT;
5034 }
5035
5036
5037 /* Set the handle of the vertical scroll bar for WINDOW to indicate
5038 that we are displaying PORTION characters out of a total of WHOLE
5039 characters, starting at POSITION. If WINDOW has no scroll bar,
5040 create one. */
5041
5042 static void
5043 XTset_vertical_scroll_bar (w, portion, whole, position)
5044 struct window *w;
5045 int portion, whole, position;
5046 {
5047 struct frame *f = XFRAME (w->frame);
5048 struct scroll_bar *bar;
5049 int top, height, left, sb_left, width, sb_width;
5050 int window_x, window_y, window_width, window_height;
5051
5052 /* Get window dimensions. */
5053 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5054 top = window_y;
5055 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
5056 height = window_height;
5057
5058 /* Compute the left edge of the scroll bar area. */
5059 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
5060 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
5061 else
5062 left = XFASTINT (w->left);
5063 left *= CANON_X_UNIT (f);
5064 left += FRAME_INTERNAL_BORDER_WIDTH (f);
5065
5066 /* Compute the width of the scroll bar which might be less than
5067 the width of the area reserved for the scroll bar. */
5068 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
5069 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
5070 else
5071 sb_width = width;
5072
5073 /* Compute the left edge of the scroll bar. */
5074 #ifdef USE_TOOLKIT_SCROLL_BARS
5075 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
5076 sb_left = left + width - sb_width - (width - sb_width) / 2;
5077 else
5078 sb_left = left + (width - sb_width) / 2;
5079 #else
5080 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
5081 sb_left = left + width - sb_width;
5082 else
5083 sb_left = left;
5084 #endif
5085
5086 /* Does the scroll bar exist yet? */
5087 if (NILP (w->vertical_scroll_bar))
5088 {
5089 if (width > 0 && height > 0)
5090 {
5091 BLOCK_INPUT;
5092 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5093 left, top, width, height, False);
5094 UNBLOCK_INPUT;
5095 }
5096
5097 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5098 }
5099 else
5100 {
5101 /* It may just need to be moved and resized. */
5102 unsigned int mask = 0;
5103
5104 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5105
5106 BLOCK_INPUT;
5107
5108 if (sb_left != XINT (bar->left))
5109 mask |= CWX;
5110 if (top != XINT (bar->top))
5111 mask |= CWY;
5112 if (sb_width != XINT (bar->width))
5113 mask |= CWWidth;
5114 if (height != XINT (bar->height))
5115 mask |= CWHeight;
5116
5117 #ifdef USE_TOOLKIT_SCROLL_BARS
5118
5119 #ifdef USE_GTK
5120 if (mask)
5121 xg_update_scrollbar_pos (f,
5122 SCROLL_BAR_X_WINDOW (bar),
5123 top,
5124 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5125 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5126 max (height, 1));
5127 #else /* not USE_GTK */
5128
5129 /* Since toolkit scroll bars are smaller than the space reserved
5130 for them on the frame, we have to clear "under" them. */
5131 if (width > 0 && height > 0)
5132 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5133 left, top, width, height, False);
5134 /* Move/size the scroll bar widget. */
5135 if (mask)
5136 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5137 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5138 top,
5139 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5140 max (height, 1), 0);
5141
5142 #endif /* not USE_GTK */
5143 #else /* not USE_TOOLKIT_SCROLL_BARS */
5144
5145 /* Clear areas not covered by the scroll bar because of
5146 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
5147 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
5148 {
5149 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5150 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5151 height, False);
5152 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5153 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5154 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5155 height, False);
5156 }
5157
5158 /* Clear areas not covered by the scroll bar because it's not as
5159 wide as the area reserved for it. This makes sure a
5160 previous mode line display is cleared after C-x 2 C-x 1, for
5161 example. */
5162 {
5163 int area_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
5164 int rest = area_width - sb_width;
5165 if (rest > 0 && height > 0)
5166 {
5167 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
5168 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5169 left + area_width - rest, top,
5170 rest, height, False);
5171 else
5172 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5173 left, top, rest, height, False);
5174 }
5175 }
5176
5177 /* Move/size the scroll bar window. */
5178 if (mask)
5179 {
5180 XWindowChanges wc;
5181
5182 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5183 wc.y = top;
5184 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
5185 wc.height = height;
5186 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
5187 mask, &wc);
5188 }
5189
5190 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5191
5192 /* Remember new settings. */
5193 XSETINT (bar->left, sb_left);
5194 XSETINT (bar->top, top);
5195 XSETINT (bar->width, sb_width);
5196 XSETINT (bar->height, height);
5197
5198 UNBLOCK_INPUT;
5199 }
5200
5201 #ifdef USE_TOOLKIT_SCROLL_BARS
5202 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5203 #else /* not USE_TOOLKIT_SCROLL_BARS */
5204 /* Set the scroll bar's current state, unless we're currently being
5205 dragged. */
5206 if (NILP (bar->dragging))
5207 {
5208 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5209
5210 if (whole == 0)
5211 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5212 else
5213 {
5214 int start = ((double) position * top_range) / whole;
5215 int end = ((double) (position + portion) * top_range) / whole;
5216 x_scroll_bar_set_handle (bar, start, end, 0);
5217 }
5218 }
5219 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5220
5221 XSETVECTOR (w->vertical_scroll_bar, bar);
5222 }
5223
5224
5225 /* The following three hooks are used when we're doing a thorough
5226 redisplay of the frame. We don't explicitly know which scroll bars
5227 are going to be deleted, because keeping track of when windows go
5228 away is a real pain - "Can you say set-window-configuration, boys
5229 and girls?" Instead, we just assert at the beginning of redisplay
5230 that *all* scroll bars are to be removed, and then save a scroll bar
5231 from the fiery pit when we actually redisplay its window. */
5232
5233 /* Arrange for all scroll bars on FRAME to be removed at the next call
5234 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5235 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5236
5237 static void
5238 XTcondemn_scroll_bars (frame)
5239 FRAME_PTR frame;
5240 {
5241 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5242 while (! NILP (FRAME_SCROLL_BARS (frame)))
5243 {
5244 Lisp_Object bar;
5245 bar = FRAME_SCROLL_BARS (frame);
5246 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
5247 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5248 XSCROLL_BAR (bar)->prev = Qnil;
5249 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5250 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5251 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
5252 }
5253 }
5254
5255
5256 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5257 Note that WINDOW isn't necessarily condemned at all. */
5258
5259 static void
5260 XTredeem_scroll_bar (window)
5261 struct window *window;
5262 {
5263 struct scroll_bar *bar;
5264 struct frame *f;
5265
5266 /* We can't redeem this window's scroll bar if it doesn't have one. */
5267 if (NILP (window->vertical_scroll_bar))
5268 abort ();
5269
5270 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5271
5272 /* Unlink it from the condemned list. */
5273 f = XFRAME (WINDOW_FRAME (window));
5274 if (NILP (bar->prev))
5275 {
5276 /* If the prev pointer is nil, it must be the first in one of
5277 the lists. */
5278 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5279 /* It's not condemned. Everything's fine. */
5280 return;
5281 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5282 window->vertical_scroll_bar))
5283 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
5284 else
5285 /* If its prev pointer is nil, it must be at the front of
5286 one or the other! */
5287 abort ();
5288 }
5289 else
5290 XSCROLL_BAR (bar->prev)->next = bar->next;
5291
5292 if (! NILP (bar->next))
5293 XSCROLL_BAR (bar->next)->prev = bar->prev;
5294
5295 bar->next = FRAME_SCROLL_BARS (f);
5296 bar->prev = Qnil;
5297 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5298 if (! NILP (bar->next))
5299 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5300 }
5301
5302 /* Remove all scroll bars on FRAME that haven't been saved since the
5303 last call to `*condemn_scroll_bars_hook'. */
5304
5305 static void
5306 XTjudge_scroll_bars (f)
5307 FRAME_PTR f;
5308 {
5309 Lisp_Object bar, next;
5310
5311 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5312
5313 /* Clear out the condemned list now so we won't try to process any
5314 more events on the hapless scroll bars. */
5315 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
5316
5317 for (; ! NILP (bar); bar = next)
5318 {
5319 struct scroll_bar *b = XSCROLL_BAR (bar);
5320
5321 x_scroll_bar_remove (b);
5322
5323 next = b->next;
5324 b->next = b->prev = Qnil;
5325 }
5326
5327 /* Now there should be no references to the condemned scroll bars,
5328 and they should get garbage-collected. */
5329 }
5330
5331
5332 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5333 is a no-op when using toolkit scroll bars.
5334
5335 This may be called from a signal handler, so we have to ignore GC
5336 mark bits. */
5337
5338 static void
5339 x_scroll_bar_expose (bar, event)
5340 struct scroll_bar *bar;
5341 XEvent *event;
5342 {
5343 #ifndef USE_TOOLKIT_SCROLL_BARS
5344
5345 Window w = SCROLL_BAR_X_WINDOW (bar);
5346 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5347 GC gc = f->output_data.x->normal_gc;
5348 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5349
5350 BLOCK_INPUT;
5351
5352 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
5353
5354 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5355 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5356
5357 /* x, y, width, height */
5358 0, 0,
5359 XINT (bar->width) - 1 - width_trim - width_trim,
5360 XINT (bar->height) - 1);
5361
5362 UNBLOCK_INPUT;
5363
5364 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5365 }
5366
5367 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5368 is set to something other than NO_EVENT, it is enqueued.
5369
5370 This may be called from a signal handler, so we have to ignore GC
5371 mark bits. */
5372
5373
5374 static void
5375 x_scroll_bar_handle_click (bar, event, emacs_event)
5376 struct scroll_bar *bar;
5377 XEvent *event;
5378 struct input_event *emacs_event;
5379 {
5380 if (! GC_WINDOWP (bar->window))
5381 abort ();
5382
5383 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5384 emacs_event->code = event->xbutton.button - Button1;
5385 emacs_event->modifiers
5386 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
5387 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5388 event->xbutton.state)
5389 | (event->type == ButtonRelease
5390 ? up_modifier
5391 : down_modifier));
5392 emacs_event->frame_or_window = bar->window;
5393 emacs_event->arg = Qnil;
5394 emacs_event->timestamp = event->xbutton.time;
5395 {
5396 #if 0
5397 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5398 int internal_height
5399 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
5400 #endif
5401 int top_range
5402 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
5403 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5404
5405 if (y < 0) y = 0;
5406 if (y > top_range) y = top_range;
5407
5408 if (y < XINT (bar->start))
5409 emacs_event->part = scroll_bar_above_handle;
5410 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5411 emacs_event->part = scroll_bar_handle;
5412 else
5413 emacs_event->part = scroll_bar_below_handle;
5414
5415 /* Just because the user has clicked on the handle doesn't mean
5416 they want to drag it. Lisp code needs to be able to decide
5417 whether or not we're dragging. */
5418 #if 0
5419 /* If the user has just clicked on the handle, record where they're
5420 holding it. */
5421 if (event->type == ButtonPress
5422 && emacs_event->part == scroll_bar_handle)
5423 XSETINT (bar->dragging, y - XINT (bar->start));
5424 #endif
5425
5426 #ifndef USE_TOOLKIT_SCROLL_BARS
5427 /* If the user has released the handle, set it to its final position. */
5428 if (event->type == ButtonRelease
5429 && ! NILP (bar->dragging))
5430 {
5431 int new_start = y - XINT (bar->dragging);
5432 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
5433
5434 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5435 bar->dragging = Qnil;
5436 }
5437 #endif
5438
5439 /* Same deal here as the other #if 0. */
5440 #if 0
5441 /* Clicks on the handle are always reported as occurring at the top of
5442 the handle. */
5443 if (emacs_event->part == scroll_bar_handle)
5444 emacs_event->x = bar->start;
5445 else
5446 XSETINT (emacs_event->x, y);
5447 #else
5448 XSETINT (emacs_event->x, y);
5449 #endif
5450
5451 XSETINT (emacs_event->y, top_range);
5452 }
5453 }
5454
5455 #ifndef USE_TOOLKIT_SCROLL_BARS
5456
5457 /* Handle some mouse motion while someone is dragging the scroll bar.
5458
5459 This may be called from a signal handler, so we have to ignore GC
5460 mark bits. */
5461
5462 static void
5463 x_scroll_bar_note_movement (bar, event)
5464 struct scroll_bar *bar;
5465 XEvent *event;
5466 {
5467 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
5468
5469 last_mouse_movement_time = event->xmotion.time;
5470
5471 f->mouse_moved = 1;
5472 XSETVECTOR (last_mouse_scroll_bar, bar);
5473
5474 /* If we're dragging the bar, display it. */
5475 if (! GC_NILP (bar->dragging))
5476 {
5477 /* Where should the handle be now? */
5478 int new_start = event->xmotion.y - XINT (bar->dragging);
5479
5480 if (new_start != XINT (bar->start))
5481 {
5482 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
5483
5484 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5485 }
5486 }
5487 }
5488
5489 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5490
5491 /* Return information to the user about the current position of the mouse
5492 on the scroll bar. */
5493
5494 static void
5495 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
5496 FRAME_PTR *fp;
5497 Lisp_Object *bar_window;
5498 enum scroll_bar_part *part;
5499 Lisp_Object *x, *y;
5500 unsigned long *time;
5501 {
5502 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
5503 Window w = SCROLL_BAR_X_WINDOW (bar);
5504 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5505 int win_x, win_y;
5506 Window dummy_window;
5507 int dummy_coord;
5508 unsigned int dummy_mask;
5509
5510 BLOCK_INPUT;
5511
5512 /* Get the mouse's position relative to the scroll bar window, and
5513 report that. */
5514 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
5515
5516 /* Root, child, root x and root y. */
5517 &dummy_window, &dummy_window,
5518 &dummy_coord, &dummy_coord,
5519
5520 /* Position relative to scroll bar. */
5521 &win_x, &win_y,
5522
5523 /* Mouse buttons and modifier keys. */
5524 &dummy_mask))
5525 ;
5526 else
5527 {
5528 #if 0
5529 int inside_height
5530 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
5531 #endif
5532 int top_range
5533 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
5534
5535 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5536
5537 if (! NILP (bar->dragging))
5538 win_y -= XINT (bar->dragging);
5539
5540 if (win_y < 0)
5541 win_y = 0;
5542 if (win_y > top_range)
5543 win_y = top_range;
5544
5545 *fp = f;
5546 *bar_window = bar->window;
5547
5548 if (! NILP (bar->dragging))
5549 *part = scroll_bar_handle;
5550 else if (win_y < XINT (bar->start))
5551 *part = scroll_bar_above_handle;
5552 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5553 *part = scroll_bar_handle;
5554 else
5555 *part = scroll_bar_below_handle;
5556
5557 XSETINT (*x, win_y);
5558 XSETINT (*y, top_range);
5559
5560 f->mouse_moved = 0;
5561 last_mouse_scroll_bar = Qnil;
5562 }
5563
5564 *time = last_mouse_movement_time;
5565
5566 UNBLOCK_INPUT;
5567 }
5568
5569
5570 /* The screen has been cleared so we may have changed foreground or
5571 background colors, and the scroll bars may need to be redrawn.
5572 Clear out the scroll bars, and ask for expose events, so we can
5573 redraw them. */
5574
5575 void
5576 x_scroll_bar_clear (f)
5577 FRAME_PTR f;
5578 {
5579 #ifndef USE_TOOLKIT_SCROLL_BARS
5580 Lisp_Object bar;
5581
5582 /* We can have scroll bars even if this is 0,
5583 if we just turned off scroll bar mode.
5584 But in that case we should not clear them. */
5585 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5586 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5587 bar = XSCROLL_BAR (bar)->next)
5588 XClearArea (FRAME_X_DISPLAY (f),
5589 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
5590 0, 0, 0, 0, True);
5591 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5592 }
5593
5594 \f
5595 /* Define a queue to save up SelectionRequest events for later handling. */
5596
5597 struct selection_event_queue
5598 {
5599 XEvent event;
5600 struct selection_event_queue *next;
5601 };
5602
5603 static struct selection_event_queue *queue;
5604
5605 /* Nonzero means queue up certain events--don't process them yet. */
5606
5607 static int x_queue_selection_requests;
5608
5609 /* Queue up an X event *EVENT, to be processed later. */
5610
5611 static void
5612 x_queue_event (f, event)
5613 FRAME_PTR f;
5614 XEvent *event;
5615 {
5616 struct selection_event_queue *queue_tmp
5617 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
5618
5619 if (queue_tmp != NULL)
5620 {
5621 queue_tmp->event = *event;
5622 queue_tmp->next = queue;
5623 queue = queue_tmp;
5624 }
5625 }
5626
5627 /* Take all the queued events and put them back
5628 so that they get processed afresh. */
5629
5630 static void
5631 x_unqueue_events (display)
5632 Display *display;
5633 {
5634 while (queue != NULL)
5635 {
5636 struct selection_event_queue *queue_tmp = queue;
5637 XPutBackEvent (display, &queue_tmp->event);
5638 queue = queue_tmp->next;
5639 xfree ((char *)queue_tmp);
5640 }
5641 }
5642
5643 /* Start queuing SelectionRequest events. */
5644
5645 void
5646 x_start_queuing_selection_requests (display)
5647 Display *display;
5648 {
5649 x_queue_selection_requests++;
5650 }
5651
5652 /* Stop queuing SelectionRequest events. */
5653
5654 void
5655 x_stop_queuing_selection_requests (display)
5656 Display *display;
5657 {
5658 x_queue_selection_requests--;
5659 x_unqueue_events (display);
5660 }
5661 \f
5662 /* The main X event-reading loop - XTread_socket. */
5663
5664 #if 0
5665 /* Time stamp of enter window event. This is only used by XTread_socket,
5666 but we have to put it out here, since static variables within functions
5667 sometimes don't work. */
5668
5669 static Time enter_timestamp;
5670 #endif
5671
5672 /* This holds the state XLookupString needs to implement dead keys
5673 and other tricks known as "compose processing". _X Window System_
5674 says that a portable program can't use this, but Stephen Gildea assures
5675 me that letting the compiler initialize it to zeros will work okay.
5676
5677 This must be defined outside of XTread_socket, for the same reasons
5678 given for enter_timestamp, above. */
5679
5680 static XComposeStatus compose_status;
5681
5682 /* Record the last 100 characters stored
5683 to help debug the loss-of-chars-during-GC problem. */
5684
5685 static int temp_index;
5686 static short temp_buffer[100];
5687
5688 /* Set this to nonzero to fake an "X I/O error"
5689 on a particular display. */
5690
5691 struct x_display_info *XTread_socket_fake_io_error;
5692
5693 /* When we find no input here, we occasionally do a no-op command
5694 to verify that the X server is still running and we can still talk with it.
5695 We try all the open displays, one by one.
5696 This variable is used for cycling thru the displays. */
5697
5698 static struct x_display_info *next_noop_dpyinfo;
5699
5700 #define SET_SAVED_MENU_EVENT(size) \
5701 do \
5702 { \
5703 if (f->output_data.x->saved_menu_event == 0) \
5704 f->output_data.x->saved_menu_event \
5705 = (XEvent *) xmalloc (sizeof (XEvent)); \
5706 bcopy (&event, f->output_data.x->saved_menu_event, size); \
5707 if (numchars >= 1) \
5708 { \
5709 bufp->kind = MENU_BAR_ACTIVATE_EVENT; \
5710 XSETFRAME (bufp->frame_or_window, f); \
5711 bufp->arg = Qnil; \
5712 bufp++; \
5713 count++; \
5714 numchars--; \
5715 } \
5716 } \
5717 while (0)
5718
5719 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
5720 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
5721
5722
5723 enum
5724 {
5725 X_EVENT_NORMAL,
5726 X_EVENT_GOTO_OUT,
5727 X_EVENT_DROP
5728 };
5729
5730 /* Filter events for the current X input method.
5731 DPYINFO is the display this event is for.
5732 EVENT is the X event to filter.
5733
5734 Returns non-zero if the event was filtered, caller shall not process
5735 this event further.
5736 Returns zero if event is wasn't filtered. */
5737
5738 #ifdef HAVE_X_I18N
5739 static int
5740 x_filter_event (dpyinfo, event)
5741 struct x_display_info *dpyinfo;
5742 XEvent *event;
5743 {
5744 /* XFilterEvent returns non-zero if the input method has
5745 consumed the event. We pass the frame's X window to
5746 XFilterEvent because that's the one for which the IC
5747 was created. */
5748
5749 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5750 event->xclient.window);
5751
5752 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5753 }
5754 #endif
5755
5756 #ifdef USE_GTK
5757 static struct x_display_info *current_dpyinfo;
5758 static struct input_event **current_bufp;
5759 static int *current_numcharsp;
5760 static int current_count;
5761 static int current_finish;
5762
5763 /* This is the filter function invoked by the GTK event loop.
5764 It is invoked before the XEvent is translated to a GdkEvent,
5765 so we have a chanse to act on the event before GTK. */
5766 static GdkFilterReturn
5767 event_handler_gdk (gxev, ev, data)
5768 GdkXEvent *gxev;
5769 GdkEvent *ev;
5770 gpointer data;
5771 {
5772 XEvent *xev = (XEvent*)gxev;
5773
5774 if (current_numcharsp)
5775 {
5776 #ifdef HAVE_X_I18N
5777 /* Filter events for the current X input method.
5778 GTK calls XFilterEvent but not for key press and release,
5779 so we do it here. */
5780 if (xev->type == KeyPress || xev->type == KeyRelease)
5781 if (x_filter_event (current_dpyinfo, xev))
5782 return GDK_FILTER_REMOVE;
5783 #endif
5784 current_count += handle_one_xevent (current_dpyinfo,
5785 xev,
5786 current_bufp,
5787 current_numcharsp,
5788 &current_finish);
5789 }
5790 else
5791 current_finish = x_dispatch_event (xev, GDK_DISPLAY ());
5792
5793 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5794 return GDK_FILTER_REMOVE;
5795
5796 return GDK_FILTER_CONTINUE;
5797 }
5798 #endif /* USE_GTK */
5799
5800
5801 /* Handles the XEvent EVENT on display DPYINFO.
5802
5803 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5804 *FINISH is zero if caller should continue reading events.
5805 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5806
5807 Events representing keys are stored in buffer *BUFP_R,
5808 which can hold up to *NUMCHARSP characters.
5809 We return the number of characters stored into the buffer. */
5810
5811 static int
5812 handle_one_xevent (dpyinfo, eventp, bufp_r, numcharsp, finish)
5813 struct x_display_info *dpyinfo;
5814 XEvent *eventp;
5815 /* register */ struct input_event **bufp_r;
5816 /* register */ int *numcharsp;
5817 int *finish;
5818 {
5819 int count = 0;
5820 int nbytes = 0;
5821 struct frame *f;
5822 struct coding_system coding;
5823 struct input_event *bufp = *bufp_r;
5824 int numchars = *numcharsp;
5825 XEvent event = *eventp;
5826
5827 *finish = X_EVENT_NORMAL;
5828
5829 switch (event.type)
5830 {
5831 case ClientMessage:
5832 {
5833 if (event.xclient.message_type
5834 == dpyinfo->Xatom_wm_protocols
5835 && event.xclient.format == 32)
5836 {
5837 if (event.xclient.data.l[0]
5838 == dpyinfo->Xatom_wm_take_focus)
5839 {
5840 /* Use x_any_window_to_frame because this
5841 could be the shell widget window
5842 if the frame has no title bar. */
5843 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5844 #ifdef HAVE_X_I18N
5845 /* Not quite sure this is needed -pd */
5846 if (f && FRAME_XIC (f))
5847 XSetICFocus (FRAME_XIC (f));
5848 #endif
5849 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5850 instructs the WM to set the input focus automatically for
5851 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5852 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5853 it has set the focus. So, XSetInputFocus below is not
5854 needed.
5855
5856 The call to XSetInputFocus below has also caused trouble. In
5857 cases where the XSetInputFocus done by the WM and the one
5858 below are temporally close (on a fast machine), the call
5859 below can generate additional FocusIn events which confuse
5860 Emacs. */
5861
5862 /* Since we set WM_TAKE_FOCUS, we must call
5863 XSetInputFocus explicitly. But not if f is null,
5864 since that might be an event for a deleted frame. */
5865 if (f)
5866 {
5867 Display *d = event.xclient.display;
5868 /* Catch and ignore errors, in case window has been
5869 iconified by a window manager such as GWM. */
5870 int count = x_catch_errors (d);
5871 XSetInputFocus (d, event.xclient.window,
5872 /* The ICCCM says this is
5873 the only valid choice. */
5874 RevertToParent,
5875 event.xclient.data.l[1]);
5876 /* This is needed to detect the error
5877 if there is an error. */
5878 XSync (d, False);
5879 x_uncatch_errors (d, count);
5880 }
5881 /* Not certain about handling scroll bars here */
5882 #endif /* 0 */
5883 }
5884 else if (event.xclient.data.l[0]
5885 == dpyinfo->Xatom_wm_save_yourself)
5886 {
5887 /* Save state modify the WM_COMMAND property to
5888 something which can reinstate us. This notifies
5889 the session manager, who's looking for such a
5890 PropertyNotify. Can restart processing when
5891 a keyboard or mouse event arrives. */
5892 /* If we have a session manager, don't set this.
5893 KDE will then start two Emacsen, one for the
5894 session manager and one for this. */
5895 if (numchars > 0
5896 #ifdef HAVE_X_SM
5897 && ! x_session_have_connection ()
5898 #endif
5899 )
5900 {
5901 f = x_top_window_to_frame (dpyinfo,
5902 event.xclient.window);
5903 /* This is just so we only give real data once
5904 for a single Emacs process. */
5905 if (f == SELECTED_FRAME ())
5906 XSetCommand (FRAME_X_DISPLAY (f),
5907 event.xclient.window,
5908 initial_argv, initial_argc);
5909 else if (f)
5910 XSetCommand (FRAME_X_DISPLAY (f),
5911 event.xclient.window,
5912 0, 0);
5913 }
5914 }
5915 else if (event.xclient.data.l[0]
5916 == dpyinfo->Xatom_wm_delete_window)
5917 {
5918 struct frame *f
5919 = x_any_window_to_frame (dpyinfo,
5920 event.xclient.window);
5921
5922 if (f)
5923 {
5924 if (numchars == 0)
5925 abort ();
5926
5927 bufp->kind = DELETE_WINDOW_EVENT;
5928 XSETFRAME (bufp->frame_or_window, f);
5929 bufp->arg = Qnil;
5930 bufp++;
5931
5932 count += 1;
5933 numchars -= 1;
5934 }
5935 else
5936 goto OTHER; /* May be a dialog that is to be removed */
5937 }
5938 }
5939 else if (event.xclient.message_type
5940 == dpyinfo->Xatom_wm_configure_denied)
5941 {
5942 }
5943 else if (event.xclient.message_type
5944 == dpyinfo->Xatom_wm_window_moved)
5945 {
5946 int new_x, new_y;
5947 struct frame *f
5948 = x_window_to_frame (dpyinfo, event.xclient.window);
5949
5950 new_x = event.xclient.data.s[0];
5951 new_y = event.xclient.data.s[1];
5952
5953 if (f)
5954 {
5955 f->output_data.x->left_pos = new_x;
5956 f->output_data.x->top_pos = new_y;
5957 }
5958 }
5959 #ifdef HACK_EDITRES
5960 else if (event.xclient.message_type
5961 == dpyinfo->Xatom_editres)
5962 {
5963 struct frame *f
5964 = x_any_window_to_frame (dpyinfo, event.xclient.window);
5965 _XEditResCheckMessages (f->output_data.x->widget, NULL,
5966 &event, NULL);
5967 }
5968 #endif /* HACK_EDITRES */
5969 else if ((event.xclient.message_type
5970 == dpyinfo->Xatom_DONE)
5971 || (event.xclient.message_type
5972 == dpyinfo->Xatom_PAGE))
5973 {
5974 /* Ghostview job completed. Kill it. We could
5975 reply with "Next" if we received "Page", but we
5976 currently never do because we are interested in
5977 images, only, which should have 1 page. */
5978 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
5979 struct frame *f
5980 = x_window_to_frame (dpyinfo, event.xclient.window);
5981 x_kill_gs_process (pixmap, f);
5982 expose_frame (f, 0, 0, 0, 0);
5983 }
5984 #ifdef USE_TOOLKIT_SCROLL_BARS
5985 /* Scroll bar callbacks send a ClientMessage from which
5986 we construct an input_event. */
5987 else if (event.xclient.message_type
5988 == dpyinfo->Xatom_Scrollbar)
5989 {
5990 x_scroll_bar_to_input_event (&event, bufp);
5991 ++bufp, ++count, --numchars;
5992 goto out;
5993 }
5994 #endif /* USE_TOOLKIT_SCROLL_BARS */
5995 else
5996 goto OTHER;
5997 }
5998 break;
5999
6000 case SelectionNotify:
6001 #ifdef USE_X_TOOLKIT
6002 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
6003 goto OTHER;
6004 #endif /* not USE_X_TOOLKIT */
6005 x_handle_selection_notify (&event.xselection);
6006 break;
6007
6008 case SelectionClear: /* Someone has grabbed ownership. */
6009 #ifdef USE_X_TOOLKIT
6010 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
6011 goto OTHER;
6012 #endif /* USE_X_TOOLKIT */
6013 {
6014 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
6015
6016 if (numchars == 0)
6017 abort ();
6018
6019 bufp->kind = SELECTION_CLEAR_EVENT;
6020 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
6021 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
6022 SELECTION_EVENT_TIME (bufp) = eventp->time;
6023 bufp->frame_or_window = Qnil;
6024 bufp->arg = Qnil;
6025 bufp++;
6026
6027 count += 1;
6028 numchars -= 1;
6029 }
6030 break;
6031
6032 case SelectionRequest: /* Someone wants our selection. */
6033 #ifdef USE_X_TOOLKIT
6034 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
6035 goto OTHER;
6036 #endif /* USE_X_TOOLKIT */
6037 if (x_queue_selection_requests)
6038 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
6039 &event);
6040 else
6041 {
6042 XSelectionRequestEvent *eventp
6043 = (XSelectionRequestEvent *) &event;
6044
6045 if (numchars == 0)
6046 abort ();
6047
6048 bufp->kind = SELECTION_REQUEST_EVENT;
6049 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
6050 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
6051 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
6052 SELECTION_EVENT_TARGET (bufp) = eventp->target;
6053 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
6054 SELECTION_EVENT_TIME (bufp) = eventp->time;
6055 bufp->frame_or_window = Qnil;
6056 bufp->arg = Qnil;
6057 bufp++;
6058
6059 count += 1;
6060 numchars -= 1;
6061 }
6062 break;
6063
6064 case PropertyNotify:
6065 #if 0 /* This is plain wrong. In the case that we are waiting for a
6066 PropertyNotify used as an ACK in incremental selection
6067 transfer, the property will be on the receiver's window. */
6068 #if defined USE_X_TOOLKIT
6069 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
6070 goto OTHER;
6071 #endif
6072 #endif
6073 x_handle_property_notify (&event.xproperty);
6074 goto OTHER;
6075
6076 case ReparentNotify:
6077 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
6078 if (f)
6079 {
6080 int x, y;
6081 f->output_data.x->parent_desc = event.xreparent.parent;
6082 x_real_positions (f, &x, &y);
6083 f->output_data.x->left_pos = x;
6084 f->output_data.x->top_pos = y;
6085 }
6086 goto OTHER;
6087 break;
6088
6089 case Expose:
6090 f = x_window_to_frame (dpyinfo, event.xexpose.window);
6091 if (f)
6092 {
6093 x_check_fullscreen (f);
6094
6095 if (f->async_visible == 0)
6096 {
6097 f->async_visible = 1;
6098 f->async_iconified = 0;
6099 f->output_data.x->has_been_visible = 1;
6100 SET_FRAME_GARBAGED (f);
6101 }
6102 else
6103 expose_frame (x_window_to_frame (dpyinfo,
6104 event.xexpose.window),
6105 event.xexpose.x, event.xexpose.y,
6106 event.xexpose.width, event.xexpose.height);
6107 }
6108 else
6109 {
6110 #ifndef USE_TOOLKIT_SCROLL_BARS
6111 struct scroll_bar *bar;
6112 #endif
6113 #if defined USE_LUCID
6114 /* Submenus of the Lucid menu bar aren't widgets
6115 themselves, so there's no way to dispatch events
6116 to them. Recognize this case separately. */
6117 {
6118 Widget widget
6119 = x_window_to_menu_bar (event.xexpose.window);
6120 if (widget)
6121 xlwmenu_redisplay (widget);
6122 }
6123 #endif /* USE_LUCID */
6124
6125 #ifdef USE_TOOLKIT_SCROLL_BARS
6126 /* Dispatch event to the widget. */
6127 goto OTHER;
6128 #else /* not USE_TOOLKIT_SCROLL_BARS */
6129 bar = x_window_to_scroll_bar (event.xexpose.window);
6130
6131 if (bar)
6132 x_scroll_bar_expose (bar, &event);
6133 #ifdef USE_X_TOOLKIT
6134 else
6135 goto OTHER;
6136 #endif /* USE_X_TOOLKIT */
6137 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6138 }
6139 break;
6140
6141 case GraphicsExpose: /* This occurs when an XCopyArea's
6142 source area was obscured or not
6143 available. */
6144 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
6145 if (f)
6146 {
6147 expose_frame (f,
6148 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
6149 event.xgraphicsexpose.width,
6150 event.xgraphicsexpose.height);
6151 }
6152 #ifdef USE_X_TOOLKIT
6153 else
6154 goto OTHER;
6155 #endif /* USE_X_TOOLKIT */
6156 break;
6157
6158 case NoExpose: /* This occurs when an XCopyArea's
6159 source area was completely
6160 available. */
6161 break;
6162
6163 case UnmapNotify:
6164 /* Redo the mouse-highlight after the tooltip has gone. */
6165 if (event.xmap.window == tip_window)
6166 {
6167 tip_window = 0;
6168 redo_mouse_highlight ();
6169 }
6170
6171 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
6172 if (f) /* F may no longer exist if
6173 the frame was deleted. */
6174 {
6175 /* While a frame is unmapped, display generation is
6176 disabled; you don't want to spend time updating a
6177 display that won't ever be seen. */
6178 f->async_visible = 0;
6179 /* We can't distinguish, from the event, whether the window
6180 has become iconified or invisible. So assume, if it
6181 was previously visible, than now it is iconified.
6182 But x_make_frame_invisible clears both
6183 the visible flag and the iconified flag;
6184 and that way, we know the window is not iconified now. */
6185 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
6186 {
6187 f->async_iconified = 1;
6188
6189 bufp->kind = ICONIFY_EVENT;
6190 XSETFRAME (bufp->frame_or_window, f);
6191 bufp->arg = Qnil;
6192 bufp++;
6193 count++;
6194 numchars--;
6195 }
6196 }
6197 goto OTHER;
6198
6199 case MapNotify:
6200 if (event.xmap.window == tip_window)
6201 /* The tooltip has been drawn already. Avoid
6202 the SET_FRAME_GARBAGED below. */
6203 goto OTHER;
6204
6205 /* We use x_top_window_to_frame because map events can
6206 come for sub-windows and they don't mean that the
6207 frame is visible. */
6208 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
6209 if (f)
6210 {
6211 /* wait_reading_process_input will notice this and update
6212 the frame's display structures.
6213 If we where iconified, we should not set garbaged,
6214 because that stops redrawing on Expose events. This looks
6215 bad if we are called from a recursive event loop
6216 (x_dispatch_event), for example when a dialog is up. */
6217 if (! f->async_iconified)
6218 SET_FRAME_GARBAGED (f);
6219
6220 f->async_visible = 1;
6221 f->async_iconified = 0;
6222 f->output_data.x->has_been_visible = 1;
6223
6224 if (f->iconified)
6225 {
6226 bufp->kind = DEICONIFY_EVENT;
6227 XSETFRAME (bufp->frame_or_window, f);
6228 bufp->arg = Qnil;
6229 bufp++;
6230 count++;
6231 numchars--;
6232 }
6233 else if (! NILP (Vframe_list)
6234 && ! NILP (XCDR (Vframe_list)))
6235 /* Force a redisplay sooner or later
6236 to update the frame titles
6237 in case this is the second frame. */
6238 record_asynch_buffer_change ();
6239 }
6240 goto OTHER;
6241
6242 case KeyPress:
6243
6244 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6245 /* Dispatch KeyPress events when in menu. */
6246 if (popup_activated ())
6247 goto OTHER;
6248 #endif
6249
6250 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
6251
6252 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
6253 {
6254 dpyinfo->mouse_face_hidden = 1;
6255 clear_mouse_face (dpyinfo);
6256 }
6257
6258 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6259 if (f == 0)
6260 {
6261 /* Scroll bars consume key events, but we want
6262 the keys to go to the scroll bar's frame. */
6263 Widget widget = XtWindowToWidget (dpyinfo->display,
6264 event.xkey.window);
6265 if (widget && XmIsScrollBar (widget))
6266 {
6267 widget = XtParent (widget);
6268 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6269 }
6270 }
6271 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6272
6273 if (f != 0)
6274 {
6275 KeySym keysym, orig_keysym;
6276 /* al%imercury@uunet.uu.net says that making this 81
6277 instead of 80 fixed a bug whereby meta chars made
6278 his Emacs hang.
6279
6280 It seems that some version of XmbLookupString has
6281 a bug of not returning XBufferOverflow in
6282 status_return even if the input is too long to
6283 fit in 81 bytes. So, we must prepare sufficient
6284 bytes for copy_buffer. 513 bytes (256 chars for
6285 two-byte character set) seems to be a fairly good
6286 approximation. -- 2000.8.10 handa@etl.go.jp */
6287 unsigned char copy_buffer[513];
6288 unsigned char *copy_bufptr = copy_buffer;
6289 int copy_bufsiz = sizeof (copy_buffer);
6290 int modifiers;
6291 Lisp_Object coding_system = Qlatin_1;
6292
6293 event.xkey.state
6294 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
6295 extra_keyboard_modifiers);
6296 modifiers = event.xkey.state;
6297
6298 /* This will have to go some day... */
6299
6300 /* make_lispy_event turns chars into control chars.
6301 Don't do it here because XLookupString is too eager. */
6302 event.xkey.state &= ~ControlMask;
6303 event.xkey.state &= ~(dpyinfo->meta_mod_mask
6304 | dpyinfo->super_mod_mask
6305 | dpyinfo->hyper_mod_mask
6306 | dpyinfo->alt_mod_mask);
6307
6308 /* In case Meta is ComposeCharacter,
6309 clear its status. According to Markus Ehrnsperger
6310 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6311 this enables ComposeCharacter to work whether or
6312 not it is combined with Meta. */
6313 if (modifiers & dpyinfo->meta_mod_mask)
6314 bzero (&compose_status, sizeof (compose_status));
6315
6316 #ifdef HAVE_X_I18N
6317 if (FRAME_XIC (f))
6318 {
6319 Status status_return;
6320
6321 coding_system = Vlocale_coding_system;
6322 nbytes = XmbLookupString (FRAME_XIC (f),
6323 &event.xkey, copy_bufptr,
6324 copy_bufsiz, &keysym,
6325 &status_return);
6326 if (status_return == XBufferOverflow)
6327 {
6328 copy_bufsiz = nbytes + 1;
6329 copy_bufptr = (char *) alloca (copy_bufsiz);
6330 nbytes = XmbLookupString (FRAME_XIC (f),
6331 &event.xkey, copy_bufptr,
6332 copy_bufsiz, &keysym,
6333 &status_return);
6334 }
6335 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6336 #if 0 && defined X_HAVE_UTF8_STRING
6337 else if (status_return == XLookupKeySym)
6338 { /* Try again but with utf-8. */
6339 coding_system = Qutf_8;
6340 nbytes = Xutf8LookupString (FRAME_XIC (f),
6341 &event.xkey, copy_bufptr,
6342 copy_bufsiz, &keysym,
6343 &status_return);
6344 if (status_return == XBufferOverflow)
6345 {
6346 copy_bufsiz = nbytes + 1;
6347 copy_bufptr = (char *) alloca (copy_bufsiz);
6348 nbytes = Xutf8LookupString (FRAME_XIC (f),
6349 &event.xkey,
6350 copy_bufptr,
6351 copy_bufsiz, &keysym,
6352 &status_return);
6353 }
6354 }
6355 #endif
6356
6357 if (status_return == XLookupNone)
6358 break;
6359 else if (status_return == XLookupChars)
6360 {
6361 keysym = NoSymbol;
6362 modifiers = 0;
6363 }
6364 else if (status_return != XLookupKeySym
6365 && status_return != XLookupBoth)
6366 abort ();
6367 }
6368 else
6369 nbytes = XLookupString (&event.xkey, copy_bufptr,
6370 copy_bufsiz, &keysym,
6371 &compose_status);
6372 #else
6373 nbytes = XLookupString (&event.xkey, copy_bufptr,
6374 copy_bufsiz, &keysym,
6375 &compose_status);
6376 #endif
6377
6378 orig_keysym = keysym;
6379
6380 if (numchars > 1)
6381 {
6382 Lisp_Object c;
6383
6384 /* First deal with keysyms which have defined
6385 translations to characters. */
6386 if (keysym >= 32 && keysym < 128)
6387 /* Avoid explicitly decoding each ASCII character. */
6388 {
6389 bufp->kind = ASCII_KEYSTROKE_EVENT;
6390 bufp->code = keysym;
6391 XSETFRAME (bufp->frame_or_window, f);
6392 bufp->arg = Qnil;
6393 bufp->modifiers
6394 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6395 modifiers);
6396 bufp->timestamp = event.xkey.time;
6397 bufp++;
6398 count++;
6399 numchars--;
6400 }
6401 /* Now non-ASCII. */
6402 else if (HASH_TABLE_P (Vx_keysym_table)
6403 && (NATNUMP (c = Fgethash (make_number (keysym),
6404 Vx_keysym_table,
6405 Qnil))))
6406 {
6407 bufp->kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6408 ? ASCII_KEYSTROKE_EVENT
6409 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6410 bufp->code = XFASTINT (c);
6411 XSETFRAME (bufp->frame_or_window, f);
6412 bufp->arg = Qnil;
6413 bufp->modifiers
6414 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6415 modifiers);
6416 bufp->timestamp = event.xkey.time;
6417 bufp++;
6418 count++;
6419 numchars--;
6420 }
6421 /* Random non-modifier sorts of keysyms. */
6422 else if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6423 || keysym == XK_Delete
6424 #ifdef XK_ISO_Left_Tab
6425 || (keysym >= XK_ISO_Left_Tab
6426 && keysym <= XK_ISO_Enter)
6427 #endif
6428 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6429 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6430 #ifdef HPUX
6431 /* This recognizes the "extended function
6432 keys". It seems there's no cleaner way.
6433 Test IsModifierKey to avoid handling
6434 mode_switch incorrectly. */
6435 || ((unsigned) (keysym) >= XK_Select
6436 && (unsigned)(keysym) < XK_KP_Space)
6437 #endif
6438 #ifdef XK_dead_circumflex
6439 || orig_keysym == XK_dead_circumflex
6440 #endif
6441 #ifdef XK_dead_grave
6442 || orig_keysym == XK_dead_grave
6443 #endif
6444 #ifdef XK_dead_tilde
6445 || orig_keysym == XK_dead_tilde
6446 #endif
6447 #ifdef XK_dead_diaeresis
6448 || orig_keysym == XK_dead_diaeresis
6449 #endif
6450 #ifdef XK_dead_macron
6451 || orig_keysym == XK_dead_macron
6452 #endif
6453 #ifdef XK_dead_degree
6454 || orig_keysym == XK_dead_degree
6455 #endif
6456 #ifdef XK_dead_acute
6457 || orig_keysym == XK_dead_acute
6458 #endif
6459 #ifdef XK_dead_cedilla
6460 || orig_keysym == XK_dead_cedilla
6461 #endif
6462 #ifdef XK_dead_breve
6463 || orig_keysym == XK_dead_breve
6464 #endif
6465 #ifdef XK_dead_ogonek
6466 || orig_keysym == XK_dead_ogonek
6467 #endif
6468 #ifdef XK_dead_caron
6469 || orig_keysym == XK_dead_caron
6470 #endif
6471 #ifdef XK_dead_doubleacute
6472 || orig_keysym == XK_dead_doubleacute
6473 #endif
6474 #ifdef XK_dead_abovedot
6475 || orig_keysym == XK_dead_abovedot
6476 #endif
6477 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6478 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6479 /* Any "vendor-specific" key is ok. */
6480 || (orig_keysym & (1 << 28))
6481 || (keysym != NoSymbol && nbytes == 0))
6482 && ! (IsModifierKey (orig_keysym)
6483 #ifndef HAVE_X11R5
6484 #ifdef XK_Mode_switch
6485 || ((unsigned)(orig_keysym) == XK_Mode_switch)
6486 #endif
6487 #ifdef XK_Num_Lock
6488 || ((unsigned)(orig_keysym) == XK_Num_Lock)
6489 #endif
6490 #endif /* not HAVE_X11R5 */
6491 /* The symbols from XK_ISO_Lock
6492 to XK_ISO_Last_Group_Lock
6493 don't have real modifiers but
6494 should be treated similarly to
6495 Mode_switch by Emacs. */
6496 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6497 || ((unsigned)(orig_keysym)
6498 >= XK_ISO_Lock
6499 && (unsigned)(orig_keysym)
6500 <= XK_ISO_Last_Group_Lock)
6501 #endif
6502 ))
6503 {
6504 if (temp_index == sizeof temp_buffer / sizeof (short))
6505 temp_index = 0;
6506 temp_buffer[temp_index++] = keysym;
6507 /* make_lispy_event will convert this to a symbolic
6508 key. */
6509 bufp->kind = NON_ASCII_KEYSTROKE_EVENT;
6510 bufp->code = keysym;
6511 XSETFRAME (bufp->frame_or_window, f);
6512 bufp->arg = Qnil;
6513 bufp->modifiers
6514 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6515 modifiers);
6516 bufp->timestamp = event.xkey.time;
6517 bufp++;
6518 count++;
6519 numchars--;
6520 }
6521 else if (numchars > nbytes)
6522 { /* Raw bytes, not keysym. */
6523 register int i;
6524 register int c;
6525 int nchars, len;
6526
6527 /* The input should be decoded with `coding_system'
6528 which depends on which X*LookupString function
6529 we used just above and the locale. */
6530 setup_coding_system (coding_system, &coding);
6531 coding.src_multibyte = 0;
6532 coding.dst_multibyte = 1;
6533 /* The input is converted to events, thus we can't
6534 handle composition. Anyway, there's no XIM that
6535 gives us composition information. */
6536 coding.composing = COMPOSITION_DISABLED;
6537
6538 for (i = 0; i < nbytes; i++)
6539 {
6540 if (temp_index == (sizeof temp_buffer
6541 / sizeof (short)))
6542 temp_index = 0;
6543 temp_buffer[temp_index++] = copy_bufptr[i];
6544 }
6545
6546 {
6547 /* Decode the input data. */
6548 int require;
6549 unsigned char *p;
6550
6551 require = decoding_buffer_size (&coding, nbytes);
6552 p = (unsigned char *) alloca (require);
6553 coding.mode |= CODING_MODE_LAST_BLOCK;
6554 /* We explicitely disable composition
6555 handling because key data should
6556 not contain any composition
6557 sequence. */
6558 coding.composing = COMPOSITION_DISABLED;
6559 decode_coding (&coding, copy_bufptr, p,
6560 nbytes, require);
6561 nbytes = coding.produced;
6562 nchars = coding.produced_char;
6563 copy_bufptr = p;
6564 }
6565
6566 /* Convert the input data to a sequence of
6567 character events. */
6568 for (i = 0; i < nbytes; i += len)
6569 {
6570 if (nchars == nbytes)
6571 c = copy_bufptr[i], len = 1;
6572 else
6573 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
6574 nbytes - i, len);
6575
6576 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
6577 ? ASCII_KEYSTROKE_EVENT
6578 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6579 bufp->code = c;
6580 XSETFRAME (bufp->frame_or_window, f);
6581 bufp->arg = Qnil;
6582 bufp->modifiers
6583 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6584 modifiers);
6585 bufp->timestamp = event.xkey.time;
6586 bufp++;
6587 }
6588
6589 count += nchars;
6590 numchars -= nchars;
6591
6592 if (keysym == NoSymbol)
6593 break;
6594 }
6595 else
6596 abort ();
6597 }
6598 else
6599 abort ();
6600 }
6601 #ifdef HAVE_X_I18N
6602 /* Don't dispatch this event since XtDispatchEvent calls
6603 XFilterEvent, and two calls in a row may freeze the
6604 client. */
6605 break;
6606 #else
6607 goto OTHER;
6608 #endif
6609
6610 case KeyRelease:
6611 #ifdef HAVE_X_I18N
6612 /* Don't dispatch this event since XtDispatchEvent calls
6613 XFilterEvent, and two calls in a row may freeze the
6614 client. */
6615 break;
6616 #else
6617 goto OTHER;
6618 #endif
6619
6620 case EnterNotify:
6621 {
6622 int n;
6623
6624 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
6625 if (n > 0)
6626 {
6627 bufp += n, count += n, numchars -= n;
6628 }
6629
6630 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
6631
6632 #if 0
6633 if (event.xcrossing.focus)
6634 {
6635 /* Avoid nasty pop/raise loops. */
6636 if (f && (!(f->auto_raise)
6637 || !(f->auto_lower)
6638 || (event.xcrossing.time - enter_timestamp) > 500))
6639 {
6640 x_new_focus_frame (dpyinfo, f);
6641 enter_timestamp = event.xcrossing.time;
6642 }
6643 }
6644 else if (f == dpyinfo->x_focus_frame)
6645 x_new_focus_frame (dpyinfo, 0);
6646 #endif
6647
6648 /* EnterNotify counts as mouse movement,
6649 so update things that depend on mouse position. */
6650 if (f && !f->output_data.x->hourglass_p)
6651 note_mouse_movement (f, &event.xmotion);
6652 goto OTHER;
6653 }
6654
6655 case FocusIn:
6656 {
6657 int n;
6658
6659 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
6660 if (n > 0)
6661 {
6662 bufp += n, count += n, numchars -= n;
6663 }
6664 }
6665
6666 goto OTHER;
6667
6668 case LeaveNotify:
6669 {
6670 int n;
6671
6672 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
6673 if (n > 0)
6674 {
6675 bufp += n, count += n, numchars -= n;
6676 }
6677 }
6678
6679 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
6680 if (f)
6681 {
6682 if (f == dpyinfo->mouse_face_mouse_frame)
6683 {
6684 /* If we move outside the frame, then we're
6685 certainly no longer on any text in the frame. */
6686 clear_mouse_face (dpyinfo);
6687 dpyinfo->mouse_face_mouse_frame = 0;
6688 }
6689
6690 /* Generate a nil HELP_EVENT to cancel a help-echo.
6691 Do it only if there's something to cancel.
6692 Otherwise, the startup message is cleared when
6693 the mouse leaves the frame. */
6694 if (any_help_event_p)
6695 {
6696 Lisp_Object frame;
6697 int n;
6698
6699 XSETFRAME (frame, f);
6700 help_echo_string = Qnil;
6701 n = gen_help_event (bufp, numchars,
6702 Qnil, frame, Qnil, Qnil, 0);
6703 bufp += n, count += n, numchars -= n;
6704 }
6705
6706 }
6707 goto OTHER;
6708
6709 case FocusOut:
6710 {
6711 int n;
6712
6713 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
6714 if (n > 0)
6715 {
6716 bufp += n, count += n, numchars -= n;
6717 }
6718 }
6719
6720 goto OTHER;
6721
6722 case MotionNotify:
6723 {
6724 previous_help_echo_string = help_echo_string;
6725 help_echo_string = help_echo_object = help_echo_window = Qnil;
6726 help_echo_pos = -1;
6727
6728 if (dpyinfo->grabbed && last_mouse_frame
6729 && FRAME_LIVE_P (last_mouse_frame))
6730 f = last_mouse_frame;
6731 else
6732 f = x_window_to_frame (dpyinfo, event.xmotion.window);
6733
6734 if (dpyinfo->mouse_face_hidden)
6735 {
6736 dpyinfo->mouse_face_hidden = 0;
6737 clear_mouse_face (dpyinfo);
6738 }
6739
6740 if (f)
6741 {
6742
6743 /* Generate SELECT_WINDOW_EVENTs when needed. */
6744 if (mouse_autoselect_window)
6745 {
6746 Lisp_Object window;
6747
6748 window = window_from_coordinates (f,
6749 event.xmotion.x, event.xmotion.y,
6750 0, 0);
6751
6752 /* Window will be selected only when it is not selected now and
6753 last mouse movement event was not in it. Minibuffer window
6754 will be selected iff it is active. */
6755 if (WINDOWP(window)
6756 && !EQ (window, last_window)
6757 && !EQ (window, selected_window)
6758 && numchars > 0)
6759 {
6760 bufp->kind = SELECT_WINDOW_EVENT;
6761 bufp->frame_or_window = window;
6762 bufp->arg = Qnil;
6763 ++bufp, ++count, --numchars;
6764 }
6765
6766 last_window=window;
6767 }
6768 note_mouse_movement (f, &event.xmotion);
6769 }
6770 else
6771 {
6772 #ifndef USE_TOOLKIT_SCROLL_BARS
6773 struct scroll_bar *bar
6774 = x_window_to_scroll_bar (event.xmotion.window);
6775
6776 if (bar)
6777 x_scroll_bar_note_movement (bar, &event);
6778 #endif /* USE_TOOLKIT_SCROLL_BARS */
6779
6780 /* If we move outside the frame, then we're
6781 certainly no longer on any text in the frame. */
6782 clear_mouse_face (dpyinfo);
6783 }
6784
6785 /* If the contents of the global variable help_echo_string
6786 has changed, generate a HELP_EVENT. */
6787 if (!NILP (help_echo_string)
6788 || !NILP (previous_help_echo_string))
6789 {
6790 Lisp_Object frame;
6791 int n;
6792
6793 if (f)
6794 XSETFRAME (frame, f);
6795 else
6796 frame = Qnil;
6797
6798 any_help_event_p = 1;
6799 n = gen_help_event (bufp, numchars, help_echo_string, frame,
6800 help_echo_window, help_echo_object,
6801 help_echo_pos);
6802 bufp += n, count += n, numchars -= n;
6803 }
6804
6805 goto OTHER;
6806 }
6807
6808 case ConfigureNotify:
6809 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
6810 if (f)
6811 {
6812 #ifndef USE_X_TOOLKIT
6813 #ifdef USE_GTK
6814 xg_resize_widgets (f, event.xconfigure.width,
6815 event.xconfigure.height);
6816 #else /* not USE_GTK */
6817 /* If there is a pending resize for fullscreen, don't
6818 do this one, the right one will come later.
6819 The toolkit version doesn't seem to need this, but we
6820 need to reset it below. */
6821 int dont_resize =
6822 ((f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
6823 && FRAME_NEW_WIDTH (f) != 0);
6824 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
6825 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
6826 if (dont_resize)
6827 goto OTHER;
6828
6829 /* In the toolkit version, change_frame_size
6830 is called by the code that handles resizing
6831 of the EmacsFrame widget. */
6832
6833 /* Even if the number of character rows and columns has
6834 not changed, the font size may have changed, so we need
6835 to check the pixel dimensions as well. */
6836 if (columns != f->width
6837 || rows != f->height
6838 || event.xconfigure.width != f->output_data.x->pixel_width
6839 || event.xconfigure.height != f->output_data.x->pixel_height)
6840 {
6841 change_frame_size (f, rows, columns, 0, 1, 0);
6842 SET_FRAME_GARBAGED (f);
6843 cancel_mouse_face (f);
6844 }
6845 #endif /* not USE_GTK */
6846 #endif
6847
6848 f->output_data.x->pixel_width = event.xconfigure.width;
6849 f->output_data.x->pixel_height = event.xconfigure.height;
6850
6851 #ifdef USE_GTK
6852 /* GTK creates windows but doesn't map them.
6853 Only get real positions and check fullscreen when mapped. */
6854 if (FRAME_GTK_OUTER_WIDGET (f)
6855 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
6856 {
6857 #endif
6858 /* What we have now is the position of Emacs's own window.
6859 Convert that to the position of the window manager window. */
6860 x_real_positions (f, &f->output_data.x->left_pos,
6861 &f->output_data.x->top_pos);
6862
6863 x_check_fullscreen_move (f);
6864 if (f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
6865 f->output_data.x->want_fullscreen &=
6866 ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
6867 #ifdef USE_GTK
6868 }
6869 #endif
6870 #ifdef HAVE_X_I18N
6871 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6872 xic_set_statusarea (f);
6873 #endif
6874
6875 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6876 {
6877 /* Since the WM decorations come below top_pos now,
6878 we must put them below top_pos in the future. */
6879 f->output_data.x->win_gravity = NorthWestGravity;
6880 x_wm_set_size_hint (f, (long) 0, 0);
6881 }
6882 }
6883 goto OTHER;
6884
6885 case ButtonRelease:
6886 case ButtonPress:
6887 {
6888 /* If we decide we want to generate an event to be seen
6889 by the rest of Emacs, we put it here. */
6890 struct input_event emacs_event;
6891 int tool_bar_p = 0;
6892
6893 emacs_event.kind = NO_EVENT;
6894 bzero (&compose_status, sizeof (compose_status));
6895
6896 if (dpyinfo->grabbed
6897 && last_mouse_frame
6898 && FRAME_LIVE_P (last_mouse_frame))
6899 f = last_mouse_frame;
6900 else
6901 f = x_window_to_frame (dpyinfo, event.xbutton.window);
6902
6903 if (f)
6904 {
6905 /* Is this in the tool-bar? */
6906 if (WINDOWP (f->tool_bar_window)
6907 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
6908 {
6909 Lisp_Object window;
6910 int x = event.xbutton.x;
6911 int y = event.xbutton.y;
6912
6913 window = window_from_coordinates (f, x, y, 0, 1);
6914 if (EQ (window, f->tool_bar_window))
6915 {
6916 if (event.xbutton.type == ButtonPress)
6917 handle_tool_bar_click (f, x, y, 1, 0);
6918 else
6919 handle_tool_bar_click (f, x, y, 0,
6920 x_x_to_emacs_modifiers (dpyinfo,
6921 event.xbutton.state));
6922 tool_bar_p = 1;
6923 }
6924 }
6925
6926 if (!tool_bar_p)
6927 if (!dpyinfo->x_focus_frame
6928 || f == dpyinfo->x_focus_frame)
6929 {
6930 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6931 if (! popup_activated ())
6932 #endif
6933 construct_mouse_click (&emacs_event, &event, f);
6934 }
6935 }
6936 else
6937 {
6938 struct scroll_bar *bar
6939 = x_window_to_scroll_bar (event.xbutton.window);
6940
6941 #ifdef USE_TOOLKIT_SCROLL_BARS
6942 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
6943 scroll bars. */
6944 if (bar && event.xbutton.state & ControlMask)
6945 {
6946 x_scroll_bar_handle_click (bar, &event, &emacs_event);
6947 *finish = X_EVENT_DROP;
6948 }
6949 #else /* not USE_TOOLKIT_SCROLL_BARS */
6950 if (bar)
6951 x_scroll_bar_handle_click (bar, &event, &emacs_event);
6952 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6953 }
6954
6955 if (event.type == ButtonPress)
6956 {
6957 dpyinfo->grabbed |= (1 << event.xbutton.button);
6958 last_mouse_frame = f;
6959 /* Ignore any mouse motion that happened
6960 before this event; any subsequent mouse-movement
6961 Emacs events should reflect only motion after
6962 the ButtonPress. */
6963 if (f != 0)
6964 f->mouse_moved = 0;
6965
6966 if (!tool_bar_p)
6967 last_tool_bar_item = -1;
6968 }
6969 else
6970 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
6971
6972 if (numchars >= 1 && emacs_event.kind != NO_EVENT)
6973 {
6974 bcopy (&emacs_event, bufp, sizeof (struct input_event));
6975 bufp++;
6976 count++;
6977 numchars--;
6978 }
6979
6980 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6981 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
6982 /* For a down-event in the menu bar,
6983 don't pass it to Xt right now.
6984 Instead, save it away
6985 and we will pass it to Xt from kbd_buffer_get_event.
6986 That way, we can run some Lisp code first. */
6987 if (
6988 #ifdef USE_GTK
6989 ! popup_activated ()
6990 &&
6991 #endif
6992 f && event.type == ButtonPress
6993 /* Verify the event is really within the menu bar
6994 and not just sent to it due to grabbing. */
6995 && event.xbutton.x >= 0
6996 && event.xbutton.x < f->output_data.x->pixel_width
6997 && event.xbutton.y >= 0
6998 && event.xbutton.y < f->output_data.x->menubar_height
6999 && event.xbutton.same_screen)
7000 {
7001 SET_SAVED_BUTTON_EVENT;
7002 XSETFRAME (last_mouse_press_frame, f);
7003 #ifdef USE_GTK
7004 *finish = X_EVENT_DROP;
7005 #endif
7006 }
7007 else if (event.type == ButtonPress)
7008 {
7009 last_mouse_press_frame = Qnil;
7010 goto OTHER;
7011 }
7012
7013 #ifdef USE_MOTIF /* This should do not harm for Lucid,
7014 but I am trying to be cautious. */
7015 else if (event.type == ButtonRelease)
7016 {
7017 if (!NILP (last_mouse_press_frame))
7018 {
7019 f = XFRAME (last_mouse_press_frame);
7020 if (f->output_data.x)
7021 SET_SAVED_BUTTON_EVENT;
7022 }
7023 else
7024 goto OTHER;
7025 }
7026 #endif /* USE_MOTIF */
7027 else
7028 goto OTHER;
7029 #endif /* USE_X_TOOLKIT || USE_GTK */
7030 }
7031 break;
7032
7033 case CirculateNotify:
7034 goto OTHER;
7035
7036 case CirculateRequest:
7037 goto OTHER;
7038
7039 case VisibilityNotify:
7040 goto OTHER;
7041
7042 case MappingNotify:
7043 /* Someone has changed the keyboard mapping - update the
7044 local cache. */
7045 switch (event.xmapping.request)
7046 {
7047 case MappingModifier:
7048 x_find_modifier_meanings (dpyinfo);
7049 /* This is meant to fall through. */
7050 case MappingKeyboard:
7051 XRefreshKeyboardMapping (&event.xmapping);
7052 }
7053 goto OTHER;
7054
7055 default:
7056 OTHER:
7057 #ifdef USE_X_TOOLKIT
7058 BLOCK_INPUT;
7059 if (*finish != X_EVENT_DROP)
7060 XtDispatchEvent (&event);
7061 UNBLOCK_INPUT;
7062 #endif /* USE_X_TOOLKIT */
7063 break;
7064 }
7065
7066 goto ret;
7067
7068 out:
7069 *finish = X_EVENT_GOTO_OUT;
7070
7071 ret:
7072 *bufp_r = bufp;
7073 *numcharsp = numchars;
7074 *eventp = event;
7075
7076 return count;
7077 }
7078
7079
7080 /* Handles the XEvent EVENT on display DISPLAY.
7081 This is used for event loops outside the normal event handling,
7082 i.e. looping while a popup menu or a dialog is posted.
7083
7084 Returns the value handle_one_xevent sets in the finish argument. */
7085 int
7086 x_dispatch_event (event, display)
7087 XEvent *event;
7088 Display *display;
7089 {
7090 struct x_display_info *dpyinfo;
7091 struct input_event bufp[10];
7092 struct input_event *bufpp = bufp;
7093 int numchars = 10;
7094 int finish = X_EVENT_NORMAL;
7095
7096 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
7097 if (dpyinfo->display == display)
7098 break;
7099
7100 if (dpyinfo)
7101 {
7102 int i, events;
7103 events = handle_one_xevent (dpyinfo,
7104 event,
7105 &bufpp,
7106 &numchars,
7107 &finish);
7108 for (i = 0; i < events; ++i)
7109 kbd_buffer_store_event (&bufp[i]);
7110 }
7111
7112 return finish;
7113 }
7114
7115
7116 /* Read events coming from the X server.
7117 This routine is called by the SIGIO handler.
7118 We return as soon as there are no more events to be read.
7119
7120 Events representing keys are stored in buffer BUFP,
7121 which can hold up to NUMCHARS characters.
7122 We return the number of characters stored into the buffer,
7123 thus pretending to be `read'.
7124
7125 EXPECTED is nonzero if the caller knows input is available. */
7126
7127 static int
7128 XTread_socket (sd, bufp, numchars, expected)
7129 register int sd;
7130 /* register */ struct input_event *bufp;
7131 /* register */ int numchars;
7132 int expected;
7133 {
7134 int count = 0;
7135 int nbytes = 0;
7136 XEvent event;
7137 int event_found = 0;
7138 struct x_display_info *dpyinfo;
7139
7140 if (interrupt_input_blocked)
7141 {
7142 interrupt_input_pending = 1;
7143 return -1;
7144 }
7145
7146 interrupt_input_pending = 0;
7147 BLOCK_INPUT;
7148
7149 /* So people can tell when we have read the available input. */
7150 input_signal_count++;
7151
7152 if (numchars <= 0)
7153 abort (); /* Don't think this happens. */
7154
7155 ++handling_signal;
7156
7157 /* Find the display we are supposed to read input for.
7158 It's the one communicating on descriptor SD. */
7159 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
7160 {
7161 #if 0 /* This ought to be unnecessary; let's verify it. */
7162 #ifdef FIOSNBIO
7163 /* If available, Xlib uses FIOSNBIO to make the socket
7164 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
7165 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
7166 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7167 fcntl (dpyinfo->connection, F_SETFL, 0);
7168 #endif /* ! defined (FIOSNBIO) */
7169 #endif
7170
7171 #if 0 /* This code can't be made to work, with multiple displays,
7172 and appears not to be used on any system any more.
7173 Also keyboard.c doesn't turn O_NDELAY on and off
7174 for X connections. */
7175 #ifndef SIGIO
7176 #ifndef HAVE_SELECT
7177 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
7178 {
7179 extern int read_alarm_should_throw;
7180 read_alarm_should_throw = 1;
7181 XPeekEvent (dpyinfo->display, &event);
7182 read_alarm_should_throw = 0;
7183 }
7184 #endif /* HAVE_SELECT */
7185 #endif /* SIGIO */
7186 #endif
7187
7188 /* For debugging, this gives a way to fake an I/O error. */
7189 if (dpyinfo == XTread_socket_fake_io_error)
7190 {
7191 XTread_socket_fake_io_error = 0;
7192 x_io_error_quitter (dpyinfo->display);
7193 }
7194
7195 #ifdef HAVE_X_SM
7196 BLOCK_INPUT;
7197 count += x_session_check_input (bufp, &numchars);
7198 UNBLOCK_INPUT;
7199 #endif
7200
7201 #ifdef USE_GTK
7202 /* For GTK we must use the GTK event loop. But XEvents gets passed
7203 to our filter function above, and then to the big event switch.
7204 We use a bunch of globals to communicate with our filter function,
7205 that is kind of ugly, but it works. */
7206 current_dpyinfo = dpyinfo;
7207
7208 while (gtk_events_pending ())
7209 {
7210 static int nr = 0;
7211 current_count = count;
7212 current_numcharsp = &numchars;
7213 current_bufp = &bufp;
7214
7215 gtk_main_iteration ();
7216
7217 count = current_count;
7218 current_bufp = 0;
7219 current_numcharsp = 0;
7220
7221 if (current_finish == X_EVENT_GOTO_OUT)
7222 goto out;
7223 }
7224
7225 #else /* not USE_GTK */
7226 while (XPending (dpyinfo->display))
7227 {
7228 int finish;
7229
7230 XNextEvent (dpyinfo->display, &event);
7231
7232 #ifdef HAVE_X_I18N
7233 /* Filter events for the current X input method. */
7234 if (x_filter_event (dpyinfo, &event))
7235 break;
7236 #endif
7237 event_found = 1;
7238
7239 count += handle_one_xevent (dpyinfo,
7240 &event,
7241 &bufp,
7242 &numchars,
7243 &finish);
7244
7245 if (finish == X_EVENT_GOTO_OUT)
7246 goto out;
7247 }
7248 #endif /* USE_GTK */
7249 }
7250
7251 out:;
7252
7253 /* On some systems, an X bug causes Emacs to get no more events
7254 when the window is destroyed. Detect that. (1994.) */
7255 if (! event_found)
7256 {
7257 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7258 One XNOOP in 100 loops will make Emacs terminate.
7259 B. Bretthauer, 1994 */
7260 x_noop_count++;
7261 if (x_noop_count >= 100)
7262 {
7263 x_noop_count=0;
7264
7265 if (next_noop_dpyinfo == 0)
7266 next_noop_dpyinfo = x_display_list;
7267
7268 XNoOp (next_noop_dpyinfo->display);
7269
7270 /* Each time we get here, cycle through the displays now open. */
7271 next_noop_dpyinfo = next_noop_dpyinfo->next;
7272 }
7273 }
7274
7275 /* If the focus was just given to an auto-raising frame,
7276 raise it now. */
7277 /* ??? This ought to be able to handle more than one such frame. */
7278 if (pending_autoraise_frame)
7279 {
7280 x_raise_frame (pending_autoraise_frame);
7281 pending_autoraise_frame = 0;
7282 }
7283
7284 UNBLOCK_INPUT;
7285 --handling_signal;
7286 return count;
7287 }
7288
7289
7290
7291 \f
7292 /***********************************************************************
7293 Text Cursor
7294 ***********************************************************************/
7295
7296 /* Set clipping for output in glyph row ROW. W is the window in which
7297 we operate. GC is the graphics context to set clipping in.
7298 WHOLE_LINE_P non-zero means include the areas used for truncation
7299 mark display and alike in the clipping rectangle.
7300
7301 ROW may be a text row or, e.g., a mode line. Text rows must be
7302 clipped to the interior of the window dedicated to text display,
7303 mode lines must be clipped to the whole window. */
7304
7305 static void
7306 x_clip_to_row (w, row, gc, whole_line_p)
7307 struct window *w;
7308 struct glyph_row *row;
7309 GC gc;
7310 int whole_line_p;
7311 {
7312 struct frame *f = XFRAME (WINDOW_FRAME (w));
7313 XRectangle clip_rect;
7314 int window_x, window_y, window_width, window_height;
7315
7316 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
7317
7318 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
7319 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
7320 clip_rect.y = max (clip_rect.y, window_y);
7321 clip_rect.width = window_width;
7322 clip_rect.height = row->visible_height;
7323
7324 /* If clipping to the whole line, including trunc marks, extend
7325 the rectangle to the left and increase its width. */
7326 if (whole_line_p)
7327 {
7328 clip_rect.x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
7329 clip_rect.width += FRAME_X_FRINGE_WIDTH (f);
7330 }
7331
7332 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7333 }
7334
7335
7336 /* Draw a hollow box cursor on window W in glyph row ROW. */
7337
7338 static void
7339 x_draw_hollow_cursor (w, row)
7340 struct window *w;
7341 struct glyph_row *row;
7342 {
7343 struct frame *f = XFRAME (WINDOW_FRAME (w));
7344 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7345 Display *dpy = FRAME_X_DISPLAY (f);
7346 int x, y, wd, h;
7347 XGCValues xgcv;
7348 struct glyph *cursor_glyph;
7349 GC gc;
7350
7351 /* Compute frame-relative coordinates from window-relative
7352 coordinates. */
7353 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
7354 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
7355 + row->ascent - w->phys_cursor_ascent);
7356 h = row->height - 1;
7357
7358 /* Get the glyph the cursor is on. If we can't tell because
7359 the current matrix is invalid or such, give up. */
7360 cursor_glyph = get_phys_cursor_glyph (w);
7361 if (cursor_glyph == NULL)
7362 return;
7363
7364 /* Compute the width of the rectangle to draw. If on a stretch
7365 glyph, and `x-stretch-block-cursor' is nil, don't draw a
7366 rectangle as wide as the glyph, but use a canonical character
7367 width instead. */
7368 wd = cursor_glyph->pixel_width - 1;
7369 if (cursor_glyph->type == STRETCH_GLYPH
7370 && !x_stretch_cursor_p)
7371 wd = min (CANON_X_UNIT (f), wd);
7372 w->phys_cursor_width = wd;
7373
7374 /* The foreground of cursor_gc is typically the same as the normal
7375 background color, which can cause the cursor box to be invisible. */
7376 xgcv.foreground = f->output_data.x->cursor_pixel;
7377 if (dpyinfo->scratch_cursor_gc)
7378 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7379 else
7380 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7381 GCForeground, &xgcv);
7382 gc = dpyinfo->scratch_cursor_gc;
7383
7384 /* Set clipping, draw the rectangle, and reset clipping again. */
7385 x_clip_to_row (w, row, gc, 0);
7386 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
7387 XSetClipMask (dpy, gc, None);
7388 }
7389
7390
7391 /* Draw a bar cursor on window W in glyph row ROW.
7392
7393 Implementation note: One would like to draw a bar cursor with an
7394 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7395 Unfortunately, I didn't find a font yet that has this property set.
7396 --gerd. */
7397
7398 static void
7399 x_draw_bar_cursor (w, row, width, kind)
7400 struct window *w;
7401 struct glyph_row *row;
7402 int width;
7403 enum text_cursor_kinds kind;
7404 {
7405 struct frame *f = XFRAME (w->frame);
7406 struct glyph *cursor_glyph;
7407
7408 /* If cursor is out of bounds, don't draw garbage. This can happen
7409 in mini-buffer windows when switching between echo area glyphs
7410 and mini-buffer. */
7411 cursor_glyph = get_phys_cursor_glyph (w);
7412 if (cursor_glyph == NULL)
7413 return;
7414
7415 /* If on an image, draw like a normal cursor. That's usually better
7416 visible than drawing a bar, esp. if the image is large so that
7417 the bar might not be in the window. */
7418 if (cursor_glyph->type == IMAGE_GLYPH)
7419 {
7420 struct glyph_row *row;
7421 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7422 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
7423 }
7424 else
7425 {
7426 Display *dpy = FRAME_X_DISPLAY (f);
7427 Window window = FRAME_X_WINDOW (f);
7428 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7429 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7430 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7431 XGCValues xgcv;
7432
7433 /* If the glyph's background equals the color we normally draw
7434 the bar cursor in, the bar cursor in its normal color is
7435 invisible. Use the glyph's foreground color instead in this
7436 case, on the assumption that the glyph's colors are chosen so
7437 that the glyph is legible. */
7438 if (face->background == f->output_data.x->cursor_pixel)
7439 xgcv.background = xgcv.foreground = face->foreground;
7440 else
7441 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7442 xgcv.graphics_exposures = 0;
7443
7444 if (gc)
7445 XChangeGC (dpy, gc, mask, &xgcv);
7446 else
7447 {
7448 gc = XCreateGC (dpy, window, mask, &xgcv);
7449 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7450 }
7451
7452 if (width < 0)
7453 width = FRAME_CURSOR_WIDTH (f);
7454 width = min (cursor_glyph->pixel_width, width);
7455
7456 w->phys_cursor_width = width;
7457 x_clip_to_row (w, row, gc, 0);
7458
7459 if (kind == BAR_CURSOR)
7460 XFillRectangle (dpy, window, gc,
7461 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7462 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7463 width, row->height);
7464 else
7465 XFillRectangle (dpy, window, gc,
7466 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7467 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7468 row->height - width),
7469 cursor_glyph->pixel_width,
7470 width);
7471
7472 XSetClipMask (dpy, gc, None);
7473 }
7474 }
7475
7476
7477 /* RIF: Define cursor CURSOR on frame F. */
7478
7479 static void
7480 x_define_frame_cursor (f, cursor)
7481 struct frame *f;
7482 Cursor cursor;
7483 {
7484 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7485 }
7486
7487
7488 /* RIF: Clear area on frame F. */
7489
7490 static void
7491 x_clear_frame_area (f, x, y, width, height)
7492 struct frame *f;
7493 int x, y, width, height;
7494 {
7495 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7496 x, y, width, height, False);
7497 }
7498
7499
7500 /* RIF: Draw cursor on window W. */
7501
7502 static void
7503 x_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
7504 struct window *w;
7505 struct glyph_row *glyph_row;
7506 int x, y;
7507 int cursor_type, cursor_width;
7508 int on_p, active_p;
7509 {
7510 struct frame *f = XFRAME (WINDOW_FRAME (w));
7511
7512 if (on_p)
7513 {
7514 w->phys_cursor_type = cursor_type;
7515 w->phys_cursor_on_p = 1;
7516
7517 switch (cursor_type)
7518 {
7519 case HOLLOW_BOX_CURSOR:
7520 x_draw_hollow_cursor (w, glyph_row);
7521 break;
7522
7523 case FILLED_BOX_CURSOR:
7524 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7525 break;
7526
7527 case BAR_CURSOR:
7528 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7529 break;
7530
7531 case HBAR_CURSOR:
7532 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7533 break;
7534
7535 case NO_CURSOR:
7536 w->phys_cursor_width = 0;
7537 break;
7538
7539 default:
7540 abort ();
7541 }
7542
7543 #ifdef HAVE_X_I18N
7544 if (w == XWINDOW (f->selected_window))
7545 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7546 xic_set_preeditarea (w, x, y);
7547 #endif
7548 }
7549
7550 #ifndef XFlush
7551 if (updating_frame != f)
7552 XFlush (FRAME_X_DISPLAY (f));
7553 #endif
7554 }
7555
7556 \f
7557 /* Icons. */
7558
7559 /* Make the x-window of frame F use the gnu icon bitmap. */
7560
7561 int
7562 x_bitmap_icon (f, file)
7563 struct frame *f;
7564 Lisp_Object file;
7565 {
7566 int bitmap_id;
7567
7568 if (FRAME_X_WINDOW (f) == 0)
7569 return 1;
7570
7571 /* Free up our existing icon bitmap if any. */
7572 if (f->output_data.x->icon_bitmap > 0)
7573 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7574 f->output_data.x->icon_bitmap = 0;
7575
7576 if (STRINGP (file))
7577 bitmap_id = x_create_bitmap_from_file (f, file);
7578 else
7579 {
7580 /* Create the GNU bitmap if necessary. */
7581 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7582 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
7583 = x_create_bitmap_from_data (f, gnu_bits,
7584 gnu_width, gnu_height);
7585
7586 /* The first time we create the GNU bitmap,
7587 this increments the ref-count one extra time.
7588 As a result, the GNU bitmap is never freed.
7589 That way, we don't have to worry about allocating it again. */
7590 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7591
7592 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7593 }
7594
7595 x_wm_set_icon_pixmap (f, bitmap_id);
7596 f->output_data.x->icon_bitmap = bitmap_id;
7597
7598 return 0;
7599 }
7600
7601
7602 /* Make the x-window of frame F use a rectangle with text.
7603 Use ICON_NAME as the text. */
7604
7605 int
7606 x_text_icon (f, icon_name)
7607 struct frame *f;
7608 char *icon_name;
7609 {
7610 if (FRAME_X_WINDOW (f) == 0)
7611 return 1;
7612
7613 #ifdef HAVE_X11R4
7614 {
7615 XTextProperty text;
7616 text.value = (unsigned char *) icon_name;
7617 text.encoding = XA_STRING;
7618 text.format = 8;
7619 text.nitems = strlen (icon_name);
7620 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7621 }
7622 #else /* not HAVE_X11R4 */
7623 XSetIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), icon_name);
7624 #endif /* not HAVE_X11R4 */
7625
7626 if (f->output_data.x->icon_bitmap > 0)
7627 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7628 f->output_data.x->icon_bitmap = 0;
7629 x_wm_set_icon_pixmap (f, 0);
7630
7631 return 0;
7632 }
7633 \f
7634 #define X_ERROR_MESSAGE_SIZE 200
7635
7636 /* If non-nil, this should be a string.
7637 It means catch X errors and store the error message in this string. */
7638
7639 static Lisp_Object x_error_message_string;
7640
7641 /* An X error handler which stores the error message in
7642 x_error_message_string. This is called from x_error_handler if
7643 x_catch_errors is in effect. */
7644
7645 static void
7646 x_error_catcher (display, error)
7647 Display *display;
7648 XErrorEvent *error;
7649 {
7650 XGetErrorText (display, error->error_code,
7651 SDATA (x_error_message_string),
7652 X_ERROR_MESSAGE_SIZE);
7653 }
7654
7655 /* Begin trapping X errors for display DPY. Actually we trap X errors
7656 for all displays, but DPY should be the display you are actually
7657 operating on.
7658
7659 After calling this function, X protocol errors no longer cause
7660 Emacs to exit; instead, they are recorded in the string
7661 stored in x_error_message_string.
7662
7663 Calling x_check_errors signals an Emacs error if an X error has
7664 occurred since the last call to x_catch_errors or x_check_errors.
7665
7666 Calling x_uncatch_errors resumes the normal error handling. */
7667
7668 void x_check_errors ();
7669 static Lisp_Object x_catch_errors_unwind ();
7670
7671 int
7672 x_catch_errors (dpy)
7673 Display *dpy;
7674 {
7675 int count = SPECPDL_INDEX ();
7676
7677 /* Make sure any errors from previous requests have been dealt with. */
7678 XSync (dpy, False);
7679
7680 record_unwind_protect (x_catch_errors_unwind,
7681 Fcons (make_save_value (dpy, 0),
7682 x_error_message_string));
7683
7684 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
7685 SSET (x_error_message_string, 0, 0);
7686
7687 return count;
7688 }
7689
7690 /* Unbind the binding that we made to check for X errors. */
7691
7692 static Lisp_Object
7693 x_catch_errors_unwind (old_val)
7694 Lisp_Object old_val;
7695 {
7696 Lisp_Object first;
7697
7698 first = XCAR (old_val);
7699
7700 XSync (XSAVE_VALUE (first)->pointer, False);
7701
7702 x_error_message_string = XCDR (old_val);
7703 return Qnil;
7704 }
7705
7706 /* If any X protocol errors have arrived since the last call to
7707 x_catch_errors or x_check_errors, signal an Emacs error using
7708 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7709
7710 void
7711 x_check_errors (dpy, format)
7712 Display *dpy;
7713 char *format;
7714 {
7715 /* Make sure to catch any errors incurred so far. */
7716 XSync (dpy, False);
7717
7718 if (SREF (x_error_message_string, 0))
7719 error (format, SDATA (x_error_message_string));
7720 }
7721
7722 /* Nonzero if we had any X protocol errors
7723 since we did x_catch_errors on DPY. */
7724
7725 int
7726 x_had_errors_p (dpy)
7727 Display *dpy;
7728 {
7729 /* Make sure to catch any errors incurred so far. */
7730 XSync (dpy, False);
7731
7732 return SREF (x_error_message_string, 0) != 0;
7733 }
7734
7735 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7736
7737 void
7738 x_clear_errors (dpy)
7739 Display *dpy;
7740 {
7741 SSET (x_error_message_string, 0, 0);
7742 }
7743
7744 /* Stop catching X protocol errors and let them make Emacs die.
7745 DPY should be the display that was passed to x_catch_errors.
7746 COUNT should be the value that was returned by
7747 the corresponding call to x_catch_errors. */
7748
7749 void
7750 x_uncatch_errors (dpy, count)
7751 Display *dpy;
7752 int count;
7753 {
7754 unbind_to (count, Qnil);
7755 }
7756
7757 #if 0
7758 static unsigned int x_wire_count;
7759 x_trace_wire ()
7760 {
7761 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7762 }
7763 #endif /* ! 0 */
7764
7765 \f
7766 /* Handle SIGPIPE, which can happen when the connection to a server
7767 simply goes away. SIGPIPE is handled by x_connection_signal.
7768 Don't need to do anything, because the write which caused the
7769 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
7770 which will do the appropriate cleanup for us. */
7771
7772 static SIGTYPE
7773 x_connection_signal (signalnum) /* If we don't have an argument, */
7774 int signalnum; /* some compilers complain in signal calls. */
7775 {
7776 #ifdef USG
7777 /* USG systems forget handlers when they are used;
7778 must reestablish each time */
7779 signal (signalnum, x_connection_signal);
7780 #endif /* USG */
7781 }
7782
7783 \f
7784 /************************************************************************
7785 Handling X errors
7786 ************************************************************************/
7787
7788 /* Error message passed to x_connection_closed. */
7789
7790 static char *error_msg;
7791
7792 /* Function installed as fatal_error_signal_hook in
7793 x_connection_closed. Print the X error message, and exit normally,
7794 instead of dumping core when XtCloseDisplay fails. */
7795
7796 static void
7797 x_fatal_error_signal ()
7798 {
7799 fprintf (stderr, "%s\n", error_msg);
7800 exit (70);
7801 }
7802
7803 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7804 the text of an error message that lead to the connection loss. */
7805
7806 static SIGTYPE
7807 x_connection_closed (dpy, error_message)
7808 Display *dpy;
7809 char *error_message;
7810 {
7811 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7812 Lisp_Object frame, tail;
7813 int count;
7814
7815 error_msg = (char *) alloca (strlen (error_message) + 1);
7816 strcpy (error_msg, error_message);
7817 handling_signal = 0;
7818
7819 /* Prevent being called recursively because of an error condition
7820 below. Otherwise, we might end up with printing ``can't find per
7821 display information'' in the recursive call instead of printing
7822 the original message here. */
7823 count = x_catch_errors (dpy);
7824
7825 /* We have to close the display to inform Xt that it doesn't
7826 exist anymore. If we don't, Xt will continue to wait for
7827 events from the display. As a consequence, a sequence of
7828
7829 M-x make-frame-on-display RET :1 RET
7830 ...kill the new frame, so that we get an IO error...
7831 M-x make-frame-on-display RET :1 RET
7832
7833 will indefinitely wait in Xt for events for display `:1', opened
7834 in the first class to make-frame-on-display.
7835
7836 Closing the display is reported to lead to a bus error on
7837 OpenWindows in certain situations. I suspect that is a bug
7838 in OpenWindows. I don't know how to cicumvent it here. */
7839
7840 #ifdef USE_X_TOOLKIT
7841 /* If DPYINFO is null, this means we didn't open the display
7842 in the first place, so don't try to close it. */
7843 if (dpyinfo)
7844 {
7845 extern void (*fatal_error_signal_hook) P_ ((void));
7846 fatal_error_signal_hook = x_fatal_error_signal;
7847 XtCloseDisplay (dpy);
7848 fatal_error_signal_hook = NULL;
7849 }
7850 #endif
7851
7852 /* Indicate that this display is dead. */
7853 if (dpyinfo)
7854 dpyinfo->display = 0;
7855
7856 /* First delete frames whose mini-buffers are on frames
7857 that are on the dead display. */
7858 FOR_EACH_FRAME (tail, frame)
7859 {
7860 Lisp_Object minibuf_frame;
7861 minibuf_frame
7862 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7863 if (FRAME_X_P (XFRAME (frame))
7864 && FRAME_X_P (XFRAME (minibuf_frame))
7865 && ! EQ (frame, minibuf_frame)
7866 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7867 Fdelete_frame (frame, Qt);
7868 }
7869
7870 /* Now delete all remaining frames on the dead display.
7871 We are now sure none of these is used as the mini-buffer
7872 for another frame that we need to delete. */
7873 FOR_EACH_FRAME (tail, frame)
7874 if (FRAME_X_P (XFRAME (frame))
7875 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7876 {
7877 /* Set this to t so that Fdelete_frame won't get confused
7878 trying to find a replacement. */
7879 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
7880 Fdelete_frame (frame, Qt);
7881 }
7882
7883 if (dpyinfo)
7884 x_delete_display (dpyinfo);
7885
7886 x_uncatch_errors (dpy, count);
7887
7888 if (x_display_list == 0)
7889 {
7890 fprintf (stderr, "%s\n", error_msg);
7891 shut_down_emacs (0, 0, Qnil);
7892 exit (70);
7893 }
7894
7895 /* Ordinary stack unwind doesn't deal with these. */
7896 #ifdef SIGIO
7897 sigunblock (sigmask (SIGIO));
7898 #endif
7899 sigunblock (sigmask (SIGALRM));
7900 TOTALLY_UNBLOCK_INPUT;
7901
7902 clear_waiting_for_input ();
7903 error ("%s", error_msg);
7904 }
7905
7906
7907 /* This is the usual handler for X protocol errors.
7908 It kills all frames on the display that we got the error for.
7909 If that was the only one, it prints an error message and kills Emacs. */
7910
7911 static void
7912 x_error_quitter (display, error)
7913 Display *display;
7914 XErrorEvent *error;
7915 {
7916 char buf[256], buf1[356];
7917
7918 /* Note that there is no real way portable across R3/R4 to get the
7919 original error handler. */
7920
7921 XGetErrorText (display, error->error_code, buf, sizeof (buf));
7922 sprintf (buf1, "X protocol error: %s on protocol request %d",
7923 buf, error->request_code);
7924 x_connection_closed (display, buf1);
7925 }
7926
7927
7928 /* This is the first-level handler for X protocol errors.
7929 It calls x_error_quitter or x_error_catcher. */
7930
7931 static int
7932 x_error_handler (display, error)
7933 Display *display;
7934 XErrorEvent *error;
7935 {
7936 if (! NILP (x_error_message_string))
7937 x_error_catcher (display, error);
7938 else
7939 x_error_quitter (display, error);
7940 return 0;
7941 }
7942
7943 /* This is the handler for X IO errors, always.
7944 It kills all frames on the display that we lost touch with.
7945 If that was the only one, it prints an error message and kills Emacs. */
7946
7947 static int
7948 x_io_error_quitter (display)
7949 Display *display;
7950 {
7951 char buf[256];
7952
7953 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
7954 x_connection_closed (display, buf);
7955 return 0;
7956 }
7957 \f
7958 /* Changing the font of the frame. */
7959
7960 /* Give frame F the font named FONTNAME as its default font, and
7961 return the full name of that font. FONTNAME may be a wildcard
7962 pattern; in that case, we choose some font that fits the pattern.
7963 The return value shows which font we chose. */
7964
7965 Lisp_Object
7966 x_new_font (f, fontname)
7967 struct frame *f;
7968 register char *fontname;
7969 {
7970 struct font_info *fontp
7971 = FS_LOAD_FONT (f, 0, fontname, -1);
7972
7973 if (!fontp)
7974 return Qnil;
7975
7976 f->output_data.x->font = (XFontStruct *) (fontp->font);
7977 f->output_data.x->baseline_offset = fontp->baseline_offset;
7978 f->output_data.x->fontset = -1;
7979
7980 compute_fringe_widths (f, 1);
7981
7982 /* Compute the scroll bar width in character columns. */
7983 if (f->scroll_bar_pixel_width > 0)
7984 {
7985 int wid = FONT_WIDTH (f->output_data.x->font);
7986 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
7987 }
7988 else
7989 {
7990 int wid = FONT_WIDTH (f->output_data.x->font);
7991 f->scroll_bar_cols = (14 + wid - 1) / wid;
7992 }
7993
7994 /* Now make the frame display the given font. */
7995 if (FRAME_X_WINDOW (f) != 0)
7996 {
7997 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
7998 f->output_data.x->font->fid);
7999 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
8000 f->output_data.x->font->fid);
8001 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
8002 f->output_data.x->font->fid);
8003
8004 frame_update_line_height (f);
8005
8006 /* Don't change the size of a tip frame; there's no point in
8007 doing it because it's done in Fx_show_tip, and it leads to
8008 problems because the tip frame has no widget. */
8009 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8010 x_set_window_size (f, 0, f->width, f->height);
8011 }
8012 else
8013 /* If we are setting a new frame's font for the first time,
8014 there are no faces yet, so this font's height is the line height. */
8015 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
8016
8017 return build_string (fontp->full_name);
8018 }
8019
8020 /* Give frame F the fontset named FONTSETNAME as its default font, and
8021 return the full name of that fontset. FONTSETNAME may be a wildcard
8022 pattern; in that case, we choose some fontset that fits the pattern.
8023 The return value shows which fontset we chose. */
8024
8025 Lisp_Object
8026 x_new_fontset (f, fontsetname)
8027 struct frame *f;
8028 char *fontsetname;
8029 {
8030 int fontset = fs_query_fontset (build_string (fontsetname), 0);
8031 Lisp_Object result;
8032
8033 if (fontset < 0)
8034 return Qnil;
8035
8036 if (f->output_data.x->fontset == fontset)
8037 /* This fontset is already set in frame F. There's nothing more
8038 to do. */
8039 return fontset_name (fontset);
8040
8041 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
8042
8043 if (!STRINGP (result))
8044 /* Can't load ASCII font. */
8045 return Qnil;
8046
8047 /* Since x_new_font doesn't update any fontset information, do it now. */
8048 f->output_data.x->fontset = fontset;
8049
8050 #ifdef HAVE_X_I18N
8051 if (FRAME_XIC (f)
8052 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
8053 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
8054 #endif
8055
8056 return build_string (fontsetname);
8057 }
8058
8059 \f
8060 /***********************************************************************
8061 X Input Methods
8062 ***********************************************************************/
8063
8064 #ifdef HAVE_X_I18N
8065
8066 #ifdef HAVE_X11R6
8067
8068 /* XIM destroy callback function, which is called whenever the
8069 connection to input method XIM dies. CLIENT_DATA contains a
8070 pointer to the x_display_info structure corresponding to XIM. */
8071
8072 static void
8073 xim_destroy_callback (xim, client_data, call_data)
8074 XIM xim;
8075 XPointer client_data;
8076 XPointer call_data;
8077 {
8078 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8079 Lisp_Object frame, tail;
8080
8081 BLOCK_INPUT;
8082
8083 /* No need to call XDestroyIC.. */
8084 FOR_EACH_FRAME (tail, frame)
8085 {
8086 struct frame *f = XFRAME (frame);
8087 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
8088 {
8089 FRAME_XIC (f) = NULL;
8090 if (FRAME_XIC_FONTSET (f))
8091 {
8092 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
8093 FRAME_XIC_FONTSET (f) = NULL;
8094 }
8095 }
8096 }
8097
8098 /* No need to call XCloseIM. */
8099 dpyinfo->xim = NULL;
8100 XFree (dpyinfo->xim_styles);
8101 UNBLOCK_INPUT;
8102 }
8103
8104 #endif /* HAVE_X11R6 */
8105
8106 #ifdef HAVE_X11R6
8107 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8108 extern char *XSetIMValues P_ ((XIM, ...));
8109 #endif
8110
8111 /* Open the connection to the XIM server on display DPYINFO.
8112 RESOURCE_NAME is the resource name Emacs uses. */
8113
8114 static void
8115 xim_open_dpy (dpyinfo, resource_name)
8116 struct x_display_info *dpyinfo;
8117 char *resource_name;
8118 {
8119 #ifdef USE_XIM
8120 XIM xim;
8121
8122 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
8123 dpyinfo->xim = xim;
8124
8125 if (xim)
8126 {
8127 #ifdef HAVE_X11R6
8128 XIMCallback destroy;
8129 #endif
8130
8131 /* Get supported styles and XIM values. */
8132 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8133
8134 #ifdef HAVE_X11R6
8135 destroy.callback = xim_destroy_callback;
8136 destroy.client_data = (XPointer)dpyinfo;
8137 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8138 #endif
8139 }
8140
8141 #else /* not USE_XIM */
8142 dpyinfo->xim = NULL;
8143 #endif /* not USE_XIM */
8144 }
8145
8146
8147 #ifdef HAVE_X11R6_XIM
8148
8149 struct xim_inst_t
8150 {
8151 struct x_display_info *dpyinfo;
8152 char *resource_name;
8153 };
8154
8155 /* XIM instantiate callback function, which is called whenever an XIM
8156 server is available. DISPLAY is the display of the XIM.
8157 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8158 when the callback was registered. */
8159
8160 static void
8161 xim_instantiate_callback (display, client_data, call_data)
8162 Display *display;
8163 XPointer client_data;
8164 XPointer call_data;
8165 {
8166 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8167 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8168
8169 /* We don't support multiple XIM connections. */
8170 if (dpyinfo->xim)
8171 return;
8172
8173 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8174
8175 /* Create XIC for the existing frames on the same display, as long
8176 as they have no XIC. */
8177 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8178 {
8179 Lisp_Object tail, frame;
8180
8181 BLOCK_INPUT;
8182 FOR_EACH_FRAME (tail, frame)
8183 {
8184 struct frame *f = XFRAME (frame);
8185
8186 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8187 if (FRAME_XIC (f) == NULL)
8188 {
8189 create_frame_xic (f);
8190 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8191 xic_set_statusarea (f);
8192 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8193 {
8194 struct window *w = XWINDOW (f->selected_window);
8195 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8196 }
8197 }
8198 }
8199
8200 UNBLOCK_INPUT;
8201 }
8202 }
8203
8204 #endif /* HAVE_X11R6_XIM */
8205
8206
8207 /* Open a connection to the XIM server on display DPYINFO.
8208 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8209 connection only at the first time. On X11R6, open the connection
8210 in the XIM instantiate callback function. */
8211
8212 static void
8213 xim_initialize (dpyinfo, resource_name)
8214 struct x_display_info *dpyinfo;
8215 char *resource_name;
8216 {
8217 #ifdef USE_XIM
8218 #ifdef HAVE_X11R6_XIM
8219 struct xim_inst_t *xim_inst;
8220 int len;
8221
8222 dpyinfo->xim = NULL;
8223 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8224 xim_inst->dpyinfo = dpyinfo;
8225 len = strlen (resource_name);
8226 xim_inst->resource_name = (char *) xmalloc (len + 1);
8227 bcopy (resource_name, xim_inst->resource_name, len + 1);
8228 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8229 resource_name, EMACS_CLASS,
8230 xim_instantiate_callback,
8231 /* Fixme: This is XPointer in
8232 XFree86 but (XPointer *) on
8233 Tru64, at least. */
8234 (XPointer) xim_inst);
8235 #else /* not HAVE_X11R6_XIM */
8236 dpyinfo->xim = NULL;
8237 xim_open_dpy (dpyinfo, resource_name);
8238 #endif /* not HAVE_X11R6_XIM */
8239
8240 #else /* not USE_XIM */
8241 dpyinfo->xim = NULL;
8242 #endif /* not USE_XIM */
8243 }
8244
8245
8246 /* Close the connection to the XIM server on display DPYINFO. */
8247
8248 static void
8249 xim_close_dpy (dpyinfo)
8250 struct x_display_info *dpyinfo;
8251 {
8252 #ifdef USE_XIM
8253 #ifdef HAVE_X11R6_XIM
8254 if (dpyinfo->display)
8255 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8256 NULL, EMACS_CLASS,
8257 xim_instantiate_callback, NULL);
8258 #endif /* not HAVE_X11R6_XIM */
8259 if (dpyinfo->display)
8260 XCloseIM (dpyinfo->xim);
8261 dpyinfo->xim = NULL;
8262 XFree (dpyinfo->xim_styles);
8263 #endif /* USE_XIM */
8264 }
8265
8266 #endif /* not HAVE_X11R6_XIM */
8267
8268
8269 \f
8270 /* Calculate the absolute position in frame F
8271 from its current recorded position values and gravity. */
8272
8273 void
8274 x_calc_absolute_position (f)
8275 struct frame *f;
8276 {
8277 Window child;
8278 int win_x = 0, win_y = 0;
8279 int flags = f->output_data.x->size_hint_flags;
8280 int this_window;
8281
8282 /* We have nothing to do if the current position
8283 is already for the top-left corner. */
8284 if (! ((flags & XNegative) || (flags & YNegative)))
8285 return;
8286
8287 this_window = FRAME_OUTER_WINDOW (f);
8288
8289 /* Find the position of the outside upper-left corner of
8290 the inner window, with respect to the outer window.
8291 But do this only if we will need the results. */
8292 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
8293 {
8294 int count;
8295
8296 BLOCK_INPUT;
8297 count = x_catch_errors (FRAME_X_DISPLAY (f));
8298 while (1)
8299 {
8300 x_clear_errors (FRAME_X_DISPLAY (f));
8301 XTranslateCoordinates (FRAME_X_DISPLAY (f),
8302
8303 /* From-window, to-window. */
8304 this_window,
8305 f->output_data.x->parent_desc,
8306
8307 /* From-position, to-position. */
8308 0, 0, &win_x, &win_y,
8309
8310 /* Child of win. */
8311 &child);
8312 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
8313 {
8314 Window newroot, newparent = 0xdeadbeef;
8315 Window *newchildren;
8316 unsigned int nchildren;
8317
8318 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
8319 &newparent, &newchildren, &nchildren))
8320 break;
8321
8322 XFree ((char *) newchildren);
8323
8324 f->output_data.x->parent_desc = newparent;
8325 }
8326 else
8327 break;
8328 }
8329
8330 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
8331 UNBLOCK_INPUT;
8332 }
8333
8334 /* Treat negative positions as relative to the leftmost bottommost
8335 position that fits on the screen. */
8336 if (flags & XNegative)
8337 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
8338 - 2 * f->output_data.x->border_width - win_x
8339 - PIXEL_WIDTH (f)
8340 + f->output_data.x->left_pos);
8341
8342 {
8343 int height = PIXEL_HEIGHT (f);
8344
8345 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8346 /* Something is fishy here. When using Motif, starting Emacs with
8347 `-g -0-0', the frame appears too low by a few pixels.
8348
8349 This seems to be so because initially, while Emacs is starting,
8350 the column widget's height and the frame's pixel height are
8351 different. The column widget's height is the right one. In
8352 later invocations, when Emacs is up, the frame's pixel height
8353 is right, though.
8354
8355 It's not obvious where the initial small difference comes from.
8356 2000-12-01, gerd. */
8357
8358 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8359 #endif
8360
8361 if (flags & YNegative)
8362 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
8363 - 2 * f->output_data.x->border_width
8364 - win_y
8365 - height
8366 + f->output_data.x->top_pos);
8367 }
8368
8369 /* The left_pos and top_pos
8370 are now relative to the top and left screen edges,
8371 so the flags should correspond. */
8372 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
8373 }
8374
8375 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8376 to really change the position, and 0 when calling from
8377 x_make_frame_visible (in that case, XOFF and YOFF are the current
8378 position values). It is -1 when calling from x_set_frame_parameters,
8379 which means, do adjust for borders but don't change the gravity. */
8380
8381 void
8382 x_set_offset (f, xoff, yoff, change_gravity)
8383 struct frame *f;
8384 register int xoff, yoff;
8385 int change_gravity;
8386 {
8387 int modified_top, modified_left;
8388
8389 if (change_gravity > 0)
8390 {
8391 f->output_data.x->top_pos = yoff;
8392 f->output_data.x->left_pos = xoff;
8393 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
8394 if (xoff < 0)
8395 f->output_data.x->size_hint_flags |= XNegative;
8396 if (yoff < 0)
8397 f->output_data.x->size_hint_flags |= YNegative;
8398 f->output_data.x->win_gravity = NorthWestGravity;
8399 }
8400 x_calc_absolute_position (f);
8401
8402 BLOCK_INPUT;
8403 x_wm_set_size_hint (f, (long) 0, 0);
8404
8405 modified_left = f->output_data.x->left_pos;
8406 modified_top = f->output_data.x->top_pos;
8407 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
8408 this seems to be unnecessary and incorrect. rms, 4/17/97. */
8409 /* It is a mystery why we need to add the border_width here
8410 when the frame is already visible, but experiment says we do. */
8411 if (change_gravity != 0)
8412 {
8413 modified_left += f->output_data.x->border_width;
8414 modified_top += f->output_data.x->border_width;
8415 }
8416 #endif
8417
8418 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8419 modified_left, modified_top);
8420 UNBLOCK_INPUT;
8421 }
8422
8423 /* Check if we need to resize the frame due to a fullscreen request.
8424 If so needed, resize the frame. */
8425 static void
8426 x_check_fullscreen (f)
8427 struct frame *f;
8428 {
8429 if (f->output_data.x->want_fullscreen & FULLSCREEN_BOTH)
8430 {
8431 int width, height, ign;
8432
8433 x_real_positions (f, &f->output_data.x->left_pos,
8434 &f->output_data.x->top_pos);
8435
8436 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
8437
8438 /* We do not need to move the window, it shall be taken care of
8439 when setting WM manager hints.
8440 If the frame is visible already, the position is checked by
8441 x_check_fullscreen_move. */
8442 if (f->width != width || f->height != height)
8443 {
8444 change_frame_size (f, height, width, 0, 1, 0);
8445 SET_FRAME_GARBAGED (f);
8446 cancel_mouse_face (f);
8447
8448 /* Wait for the change of frame size to occur */
8449 f->output_data.x->want_fullscreen |= FULLSCREEN_WAIT;
8450 }
8451 }
8452 }
8453
8454 /* If frame parameters are set after the frame is mapped, we need to move
8455 the window. This is done in xfns.c.
8456 Some window managers moves the window to the right position, some
8457 moves the outer window manager window to the specified position.
8458 Here we check that we are in the right spot. If not, make a second
8459 move, assuming we are dealing with the second kind of window manager. */
8460 static void
8461 x_check_fullscreen_move (f)
8462 struct frame *f;
8463 {
8464 if (f->output_data.x->want_fullscreen & FULLSCREEN_MOVE_WAIT)
8465 {
8466 int expect_top = f->output_data.x->top_pos;
8467 int expect_left = f->output_data.x->left_pos;
8468
8469 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
8470 expect_top = 0;
8471 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
8472 expect_left = 0;
8473
8474 if (expect_top != f->output_data.x->top_pos
8475 || expect_left != f->output_data.x->left_pos)
8476 x_set_offset (f, expect_left, expect_top, 1);
8477
8478 /* Just do this once */
8479 f->output_data.x->want_fullscreen &= ~FULLSCREEN_MOVE_WAIT;
8480 }
8481 }
8482
8483
8484 /* Calculate fullscreen size. Return in *TOP_POS and *LEFT_POS the
8485 wanted positions of the WM window (not emacs window).
8486 Return in *WIDTH and *HEIGHT the wanted width and height of Emacs
8487 window (FRAME_X_WINDOW).
8488 */
8489 void
8490 x_fullscreen_adjust (f, width, height, top_pos, left_pos)
8491 struct frame *f;
8492 int *width;
8493 int *height;
8494 int *top_pos;
8495 int *left_pos;
8496 {
8497 int newwidth = f->width, newheight = f->height;
8498
8499 *top_pos = f->output_data.x->top_pos;
8500 *left_pos = f->output_data.x->left_pos;
8501
8502 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
8503 {
8504 int ph;
8505
8506 ph = FRAME_X_DISPLAY_INFO (f)->height;
8507 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
8508 ph = CHAR_TO_PIXEL_HEIGHT (f, newheight)
8509 - f->output_data.x->y_pixels_diff;
8510 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
8511 *top_pos = 0;
8512 }
8513
8514 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
8515 {
8516 int pw;
8517
8518 pw = FRAME_X_DISPLAY_INFO (f)->width;
8519 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
8520 pw = CHAR_TO_PIXEL_WIDTH (f, newwidth)
8521 - f->output_data.x->x_pixels_diff;
8522 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
8523 *left_pos = 0;
8524 }
8525
8526 *width = newwidth;
8527 *height = newheight;
8528 }
8529
8530
8531 /* Change the size of frame F's X window to COLS/ROWS in the case F
8532 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8533 top-left-corner window gravity for this size change and subsequent
8534 size changes. Otherwise we leave the window gravity unchanged. */
8535
8536 static void
8537 x_set_window_size_1 (f, change_gravity, cols, rows)
8538 struct frame *f;
8539 int change_gravity;
8540 int cols, rows;
8541 {
8542 int pixelwidth, pixelheight;
8543
8544 check_frame_size (f, &rows, &cols);
8545 f->output_data.x->vertical_scroll_bar_extra
8546 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8547 ? 0
8548 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
8549 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
8550 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
8551
8552 compute_fringe_widths (f, 0);
8553
8554 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
8555 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
8556
8557 f->output_data.x->win_gravity = NorthWestGravity;
8558 x_wm_set_size_hint (f, (long) 0, 0);
8559
8560 XSync (FRAME_X_DISPLAY (f), False);
8561 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8562 pixelwidth, pixelheight);
8563
8564 /* Now, strictly speaking, we can't be sure that this is accurate,
8565 but the window manager will get around to dealing with the size
8566 change request eventually, and we'll hear how it went when the
8567 ConfigureNotify event gets here.
8568
8569 We could just not bother storing any of this information here,
8570 and let the ConfigureNotify event set everything up, but that
8571 might be kind of confusing to the Lisp code, since size changes
8572 wouldn't be reported in the frame parameters until some random
8573 point in the future when the ConfigureNotify event arrives.
8574
8575 We pass 1 for DELAY since we can't run Lisp code inside of
8576 a BLOCK_INPUT. */
8577 change_frame_size (f, rows, cols, 0, 1, 0);
8578 PIXEL_WIDTH (f) = pixelwidth;
8579 PIXEL_HEIGHT (f) = pixelheight;
8580
8581 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8582 receive in the ConfigureNotify event; if we get what we asked
8583 for, then the event won't cause the screen to become garbaged, so
8584 we have to make sure to do it here. */
8585 SET_FRAME_GARBAGED (f);
8586
8587 XFlush (FRAME_X_DISPLAY (f));
8588 }
8589
8590
8591 /* Call this to change the size of frame F's x-window.
8592 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8593 for this size change and subsequent size changes.
8594 Otherwise we leave the window gravity unchanged. */
8595
8596 void
8597 x_set_window_size (f, change_gravity, cols, rows)
8598 struct frame *f;
8599 int change_gravity;
8600 int cols, rows;
8601 {
8602 BLOCK_INPUT;
8603
8604 #ifdef USE_GTK
8605 if (FRAME_GTK_WIDGET (f))
8606 xg_frame_set_char_size (f, cols, rows);
8607 else
8608 x_set_window_size_1 (f, change_gravity, cols, rows);
8609 #elif USE_X_TOOLKIT
8610
8611 if (f->output_data.x->widget != NULL)
8612 {
8613 /* The x and y position of the widget is clobbered by the
8614 call to XtSetValues within EmacsFrameSetCharSize.
8615 This is a real kludge, but I don't understand Xt so I can't
8616 figure out a correct fix. Can anyone else tell me? -- rms. */
8617 int xpos = f->output_data.x->widget->core.x;
8618 int ypos = f->output_data.x->widget->core.y;
8619 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
8620 f->output_data.x->widget->core.x = xpos;
8621 f->output_data.x->widget->core.y = ypos;
8622 }
8623 else
8624 x_set_window_size_1 (f, change_gravity, cols, rows);
8625
8626 #else /* not USE_X_TOOLKIT */
8627
8628 x_set_window_size_1 (f, change_gravity, cols, rows);
8629
8630 #endif /* not USE_X_TOOLKIT */
8631
8632 /* If cursor was outside the new size, mark it as off. */
8633 mark_window_cursors_off (XWINDOW (f->root_window));
8634
8635 /* Clear out any recollection of where the mouse highlighting was,
8636 since it might be in a place that's outside the new frame size.
8637 Actually checking whether it is outside is a pain in the neck,
8638 so don't try--just let the highlighting be done afresh with new size. */
8639 cancel_mouse_face (f);
8640
8641 UNBLOCK_INPUT;
8642 }
8643 \f
8644 /* Mouse warping. */
8645
8646 void
8647 x_set_mouse_position (f, x, y)
8648 struct frame *f;
8649 int x, y;
8650 {
8651 int pix_x, pix_y;
8652
8653 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
8654 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
8655
8656 if (pix_x < 0) pix_x = 0;
8657 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
8658
8659 if (pix_y < 0) pix_y = 0;
8660 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
8661
8662 BLOCK_INPUT;
8663
8664 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8665 0, 0, 0, 0, pix_x, pix_y);
8666 UNBLOCK_INPUT;
8667 }
8668
8669 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
8670
8671 void
8672 x_set_mouse_pixel_position (f, pix_x, pix_y)
8673 struct frame *f;
8674 int pix_x, pix_y;
8675 {
8676 BLOCK_INPUT;
8677
8678 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8679 0, 0, 0, 0, pix_x, pix_y);
8680 UNBLOCK_INPUT;
8681 }
8682 \f
8683 /* focus shifting, raising and lowering. */
8684
8685 void
8686 x_focus_on_frame (f)
8687 struct frame *f;
8688 {
8689 #if 0 /* This proves to be unpleasant. */
8690 x_raise_frame (f);
8691 #endif
8692 #if 0
8693 /* I don't think that the ICCCM allows programs to do things like this
8694 without the interaction of the window manager. Whatever you end up
8695 doing with this code, do it to x_unfocus_frame too. */
8696 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8697 RevertToPointerRoot, CurrentTime);
8698 #endif /* ! 0 */
8699 }
8700
8701 void
8702 x_unfocus_frame (f)
8703 struct frame *f;
8704 {
8705 #if 0
8706 /* Look at the remarks in x_focus_on_frame. */
8707 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
8708 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
8709 RevertToPointerRoot, CurrentTime);
8710 #endif /* ! 0 */
8711 }
8712
8713 /* Raise frame F. */
8714
8715 void
8716 x_raise_frame (f)
8717 struct frame *f;
8718 {
8719 if (f->async_visible)
8720 {
8721 BLOCK_INPUT;
8722 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8723 XFlush (FRAME_X_DISPLAY (f));
8724 UNBLOCK_INPUT;
8725 }
8726 }
8727
8728 /* Lower frame F. */
8729
8730 void
8731 x_lower_frame (f)
8732 struct frame *f;
8733 {
8734 if (f->async_visible)
8735 {
8736 BLOCK_INPUT;
8737 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8738 XFlush (FRAME_X_DISPLAY (f));
8739 UNBLOCK_INPUT;
8740 }
8741 }
8742
8743 static void
8744 XTframe_raise_lower (f, raise_flag)
8745 FRAME_PTR f;
8746 int raise_flag;
8747 {
8748 if (raise_flag)
8749 x_raise_frame (f);
8750 else
8751 x_lower_frame (f);
8752 }
8753 \f
8754 /* Change of visibility. */
8755
8756 /* This tries to wait until the frame is really visible.
8757 However, if the window manager asks the user where to position
8758 the frame, this will return before the user finishes doing that.
8759 The frame will not actually be visible at that time,
8760 but it will become visible later when the window manager
8761 finishes with it. */
8762
8763 void
8764 x_make_frame_visible (f)
8765 struct frame *f;
8766 {
8767 Lisp_Object type;
8768 int original_top, original_left;
8769 int retry_count = 2;
8770
8771 retry:
8772
8773 BLOCK_INPUT;
8774
8775 type = x_icon_type (f);
8776 if (!NILP (type))
8777 x_bitmap_icon (f, type);
8778
8779 if (! FRAME_VISIBLE_P (f))
8780 {
8781 /* We test FRAME_GARBAGED_P here to make sure we don't
8782 call x_set_offset a second time
8783 if we get to x_make_frame_visible a second time
8784 before the window gets really visible. */
8785 if (! FRAME_ICONIFIED_P (f)
8786 && ! f->output_data.x->asked_for_visible)
8787 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
8788
8789 f->output_data.x->asked_for_visible = 1;
8790
8791 if (! EQ (Vx_no_window_manager, Qt))
8792 x_wm_set_window_state (f, NormalState);
8793 #ifdef USE_X_TOOLKIT
8794 /* This was XtPopup, but that did nothing for an iconified frame. */
8795 XtMapWidget (f->output_data.x->widget);
8796 #else /* not USE_X_TOOLKIT */
8797 #ifdef USE_GTK
8798 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
8799 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
8800 #else
8801 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
8802 #endif /* not USE_GTK */
8803 #endif /* not USE_X_TOOLKIT */
8804 #if 0 /* This seems to bring back scroll bars in the wrong places
8805 if the window configuration has changed. They seem
8806 to come back ok without this. */
8807 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8808 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
8809 #endif
8810 }
8811
8812 XFlush (FRAME_X_DISPLAY (f));
8813
8814 /* Synchronize to ensure Emacs knows the frame is visible
8815 before we do anything else. We do this loop with input not blocked
8816 so that incoming events are handled. */
8817 {
8818 Lisp_Object frame;
8819 int count;
8820 /* This must be before UNBLOCK_INPUT
8821 since events that arrive in response to the actions above
8822 will set it when they are handled. */
8823 int previously_visible = f->output_data.x->has_been_visible;
8824
8825 original_left = f->output_data.x->left_pos;
8826 original_top = f->output_data.x->top_pos;
8827
8828 /* This must come after we set COUNT. */
8829 UNBLOCK_INPUT;
8830
8831 /* We unblock here so that arriving X events are processed. */
8832
8833 /* Now move the window back to where it was "supposed to be".
8834 But don't do it if the gravity is negative.
8835 When the gravity is negative, this uses a position
8836 that is 3 pixels too low. Perhaps that's really the border width.
8837
8838 Don't do this if the window has never been visible before,
8839 because the window manager may choose the position
8840 and we don't want to override it. */
8841
8842 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
8843 && f->output_data.x->win_gravity == NorthWestGravity
8844 && previously_visible)
8845 {
8846 Drawable rootw;
8847 int x, y;
8848 unsigned int width, height, border, depth;
8849
8850 BLOCK_INPUT;
8851
8852 /* On some window managers (such as FVWM) moving an existing
8853 window, even to the same place, causes the window manager
8854 to introduce an offset. This can cause the window to move
8855 to an unexpected location. Check the geometry (a little
8856 slow here) and then verify that the window is in the right
8857 place. If the window is not in the right place, move it
8858 there, and take the potential window manager hit. */
8859 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8860 &rootw, &x, &y, &width, &height, &border, &depth);
8861
8862 if (original_left != x || original_top != y)
8863 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8864 original_left, original_top);
8865
8866 UNBLOCK_INPUT;
8867 }
8868
8869 XSETFRAME (frame, f);
8870
8871 /* Wait until the frame is visible. Process X events until a
8872 MapNotify event has been seen, or until we think we won't get a
8873 MapNotify at all.. */
8874 for (count = input_signal_count + 10;
8875 input_signal_count < count && !FRAME_VISIBLE_P (f);)
8876 {
8877 /* Force processing of queued events. */
8878 x_sync (f);
8879
8880 /* Machines that do polling rather than SIGIO have been
8881 observed to go into a busy-wait here. So we'll fake an
8882 alarm signal to let the handler know that there's something
8883 to be read. We used to raise a real alarm, but it seems
8884 that the handler isn't always enabled here. This is
8885 probably a bug. */
8886 if (input_polling_used ())
8887 {
8888 /* It could be confusing if a real alarm arrives while
8889 processing the fake one. Turn it off and let the
8890 handler reset it. */
8891 extern void poll_for_input_1 P_ ((void));
8892 int old_poll_suppress_count = poll_suppress_count;
8893 poll_suppress_count = 1;
8894 poll_for_input_1 ();
8895 poll_suppress_count = old_poll_suppress_count;
8896 }
8897
8898 /* See if a MapNotify event has been processed. */
8899 FRAME_SAMPLE_VISIBILITY (f);
8900 }
8901
8902 /* 2000-09-28: In
8903
8904 (let ((f (selected-frame)))
8905 (iconify-frame f)
8906 (raise-frame f))
8907
8908 the frame is not raised with various window managers on
8909 FreeBSD, Linux and Solaris. It turns out that, for some
8910 unknown reason, the call to XtMapWidget is completely ignored.
8911 Mapping the widget a second time works. */
8912
8913 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
8914 goto retry;
8915 }
8916 }
8917
8918 /* Change from mapped state to withdrawn state. */
8919
8920 /* Make the frame visible (mapped and not iconified). */
8921
8922 void
8923 x_make_frame_invisible (f)
8924 struct frame *f;
8925 {
8926 Window window;
8927
8928 /* Use the frame's outermost window, not the one we normally draw on. */
8929 window = FRAME_OUTER_WINDOW (f);
8930
8931 /* Don't keep the highlight on an invisible frame. */
8932 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
8933 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
8934
8935 #if 0/* This might add unreliability; I don't trust it -- rms. */
8936 if (! f->async_visible && ! f->async_iconified)
8937 return;
8938 #endif
8939
8940 BLOCK_INPUT;
8941
8942 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
8943 that the current position of the window is user-specified, rather than
8944 program-specified, so that when the window is mapped again, it will be
8945 placed at the same location, without forcing the user to position it
8946 by hand again (they have already done that once for this window.) */
8947 x_wm_set_size_hint (f, (long) 0, 1);
8948
8949 #ifdef USE_GTK
8950 if (FRAME_GTK_OUTER_WIDGET (f))
8951 {
8952 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
8953 goto out;
8954 }
8955 #endif
8956
8957 #ifdef HAVE_X11R4
8958
8959 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
8960 DefaultScreen (FRAME_X_DISPLAY (f))))
8961 {
8962 UNBLOCK_INPUT_RESIGNAL;
8963 error ("Can't notify window manager of window withdrawal");
8964 }
8965 #else /* ! defined (HAVE_X11R4) */
8966
8967 /* Tell the window manager what we're going to do. */
8968 if (! EQ (Vx_no_window_manager, Qt))
8969 {
8970 XEvent unmap;
8971
8972 unmap.xunmap.type = UnmapNotify;
8973 unmap.xunmap.window = window;
8974 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
8975 unmap.xunmap.from_configure = False;
8976 if (! XSendEvent (FRAME_X_DISPLAY (f),
8977 DefaultRootWindow (FRAME_X_DISPLAY (f)),
8978 False,
8979 SubstructureRedirectMaskSubstructureNotifyMask,
8980 &unmap))
8981 {
8982 UNBLOCK_INPUT_RESIGNAL;
8983 error ("Can't notify window manager of withdrawal");
8984 }
8985 }
8986
8987 /* Unmap the window ourselves. Cheeky! */
8988 XUnmapWindow (FRAME_X_DISPLAY (f), window);
8989 #endif /* ! defined (HAVE_X11R4) */
8990
8991 out:
8992 /* We can't distinguish this from iconification
8993 just by the event that we get from the server.
8994 So we can't win using the usual strategy of letting
8995 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
8996 and synchronize with the server to make sure we agree. */
8997 f->visible = 0;
8998 FRAME_ICONIFIED_P (f) = 0;
8999 f->async_visible = 0;
9000 f->async_iconified = 0;
9001
9002 x_sync (f);
9003
9004 UNBLOCK_INPUT;
9005 }
9006
9007 /* Change window state from mapped to iconified. */
9008
9009 void
9010 x_iconify_frame (f)
9011 struct frame *f;
9012 {
9013 int result;
9014 Lisp_Object type;
9015
9016 /* Don't keep the highlight on an invisible frame. */
9017 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9018 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9019
9020 if (f->async_iconified)
9021 return;
9022
9023 BLOCK_INPUT;
9024
9025 FRAME_SAMPLE_VISIBILITY (f);
9026
9027 type = x_icon_type (f);
9028 if (!NILP (type))
9029 x_bitmap_icon (f, type);
9030
9031 #ifdef USE_GTK
9032 if (FRAME_GTK_OUTER_WIDGET (f))
9033 {
9034 if (! FRAME_VISIBLE_P (f))
9035 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9036
9037 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9038 f->iconified = 1;
9039 f->visible = 1;
9040 f->async_iconified = 1;
9041 f->async_visible = 0;
9042 UNBLOCK_INPUT;
9043 return;
9044 }
9045 #endif
9046
9047 #ifdef USE_X_TOOLKIT
9048
9049 if (! FRAME_VISIBLE_P (f))
9050 {
9051 if (! EQ (Vx_no_window_manager, Qt))
9052 x_wm_set_window_state (f, IconicState);
9053 /* This was XtPopup, but that did nothing for an iconified frame. */
9054 XtMapWidget (f->output_data.x->widget);
9055 /* The server won't give us any event to indicate
9056 that an invisible frame was changed to an icon,
9057 so we have to record it here. */
9058 f->iconified = 1;
9059 f->visible = 1;
9060 f->async_iconified = 1;
9061 f->async_visible = 0;
9062 UNBLOCK_INPUT;
9063 return;
9064 }
9065
9066 result = XIconifyWindow (FRAME_X_DISPLAY (f),
9067 XtWindow (f->output_data.x->widget),
9068 DefaultScreen (FRAME_X_DISPLAY (f)));
9069 UNBLOCK_INPUT;
9070
9071 if (!result)
9072 error ("Can't notify window manager of iconification");
9073
9074 f->async_iconified = 1;
9075 f->async_visible = 0;
9076
9077
9078 BLOCK_INPUT;
9079 XFlush (FRAME_X_DISPLAY (f));
9080 UNBLOCK_INPUT;
9081 #else /* not USE_X_TOOLKIT */
9082
9083 /* Make sure the X server knows where the window should be positioned,
9084 in case the user deiconifies with the window manager. */
9085 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
9086 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
9087
9088 /* Since we don't know which revision of X we're running, we'll use both
9089 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
9090
9091 /* X11R4: send a ClientMessage to the window manager using the
9092 WM_CHANGE_STATE type. */
9093 {
9094 XEvent message;
9095
9096 message.xclient.window = FRAME_X_WINDOW (f);
9097 message.xclient.type = ClientMessage;
9098 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
9099 message.xclient.format = 32;
9100 message.xclient.data.l[0] = IconicState;
9101
9102 if (! XSendEvent (FRAME_X_DISPLAY (f),
9103 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9104 False,
9105 SubstructureRedirectMask | SubstructureNotifyMask,
9106 &message))
9107 {
9108 UNBLOCK_INPUT_RESIGNAL;
9109 error ("Can't notify window manager of iconification");
9110 }
9111 }
9112
9113 /* X11R3: set the initial_state field of the window manager hints to
9114 IconicState. */
9115 x_wm_set_window_state (f, IconicState);
9116
9117 if (!FRAME_VISIBLE_P (f))
9118 {
9119 /* If the frame was withdrawn, before, we must map it. */
9120 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9121 }
9122
9123 f->async_iconified = 1;
9124 f->async_visible = 0;
9125
9126 XFlush (FRAME_X_DISPLAY (f));
9127 UNBLOCK_INPUT;
9128 #endif /* not USE_X_TOOLKIT */
9129 }
9130
9131 \f
9132 /* Free X resources of frame F. */
9133
9134 void
9135 x_free_frame_resources (f)
9136 struct frame *f;
9137 {
9138 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9139 Lisp_Object bar;
9140 struct scroll_bar *b;
9141
9142 BLOCK_INPUT;
9143
9144 /* If a display connection is dead, don't try sending more
9145 commands to the X server. */
9146 if (dpyinfo->display)
9147 {
9148 if (f->output_data.x->icon_desc)
9149 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9150
9151 #ifdef USE_X_TOOLKIT
9152 /* Explicitly destroy the scroll bars of the frame. Without
9153 this, we get "BadDrawable" errors from the toolkit later on,
9154 presumably from expose events generated for the disappearing
9155 toolkit scroll bars. */
9156 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9157 {
9158 b = XSCROLL_BAR (bar);
9159 x_scroll_bar_remove (b);
9160 }
9161 #endif
9162
9163 #ifdef HAVE_X_I18N
9164 if (FRAME_XIC (f))
9165 free_frame_xic (f);
9166 #endif
9167
9168 #ifdef USE_X_TOOLKIT
9169 if (f->output_data.x->widget)
9170 {
9171 XtDestroyWidget (f->output_data.x->widget);
9172 f->output_data.x->widget = NULL;
9173 }
9174 /* Tooltips don't have widgets, only a simple X window, even if
9175 we are using a toolkit. */
9176 else if (FRAME_X_WINDOW (f))
9177 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9178
9179 free_frame_menubar (f);
9180 #else /* !USE_X_TOOLKIT */
9181
9182 #ifdef USE_GTK
9183 /* In the GTK version, tooltips are normal X
9184 frames. We must check and free both types. */
9185 if (FRAME_GTK_OUTER_WIDGET (f))
9186 {
9187 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
9188 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
9189 FRAME_GTK_OUTER_WIDGET (f) = 0;
9190 }
9191 #endif /* USE_GTK */
9192
9193 if (FRAME_X_WINDOW (f))
9194 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9195 #endif /* !USE_X_TOOLKIT */
9196
9197 unload_color (f, f->output_data.x->foreground_pixel);
9198 unload_color (f, f->output_data.x->background_pixel);
9199 unload_color (f, f->output_data.x->cursor_pixel);
9200 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9201 unload_color (f, f->output_data.x->border_pixel);
9202 unload_color (f, f->output_data.x->mouse_pixel);
9203
9204 if (f->output_data.x->scroll_bar_background_pixel != -1)
9205 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9206 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9207 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9208 #ifdef USE_TOOLKIT_SCROLL_BARS
9209 /* Scrollbar shadow colors. */
9210 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9211 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9212 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9213 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9214 #endif /* USE_TOOLKIT_SCROLL_BARS */
9215 if (f->output_data.x->white_relief.allocated_p)
9216 unload_color (f, f->output_data.x->white_relief.pixel);
9217 if (f->output_data.x->black_relief.allocated_p)
9218 unload_color (f, f->output_data.x->black_relief.pixel);
9219
9220 if (FRAME_FACE_CACHE (f))
9221 free_frame_faces (f);
9222
9223 x_free_gcs (f);
9224 XFlush (FRAME_X_DISPLAY (f));
9225 }
9226
9227 if (f->output_data.x->saved_menu_event)
9228 xfree (f->output_data.x->saved_menu_event);
9229
9230 xfree (f->output_data.x);
9231 f->output_data.x = NULL;
9232
9233 if (f == dpyinfo->x_focus_frame)
9234 dpyinfo->x_focus_frame = 0;
9235 if (f == dpyinfo->x_focus_event_frame)
9236 dpyinfo->x_focus_event_frame = 0;
9237 if (f == dpyinfo->x_highlight_frame)
9238 dpyinfo->x_highlight_frame = 0;
9239
9240 if (f == dpyinfo->mouse_face_mouse_frame)
9241 {
9242 dpyinfo->mouse_face_beg_row
9243 = dpyinfo->mouse_face_beg_col = -1;
9244 dpyinfo->mouse_face_end_row
9245 = dpyinfo->mouse_face_end_col = -1;
9246 dpyinfo->mouse_face_window = Qnil;
9247 dpyinfo->mouse_face_deferred_gc = 0;
9248 dpyinfo->mouse_face_mouse_frame = 0;
9249 }
9250
9251 UNBLOCK_INPUT;
9252 }
9253
9254
9255 /* Destroy the X window of frame F. */
9256
9257 void
9258 x_destroy_window (f)
9259 struct frame *f;
9260 {
9261 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9262
9263 /* If a display connection is dead, don't try sending more
9264 commands to the X server. */
9265 if (dpyinfo->display != 0)
9266 x_free_frame_resources (f);
9267
9268 dpyinfo->reference_count--;
9269 }
9270
9271 \f
9272 /* Setting window manager hints. */
9273
9274 /* Set the normal size hints for the window manager, for frame F.
9275 FLAGS is the flags word to use--or 0 meaning preserve the flags
9276 that the window now has.
9277 If USER_POSITION is nonzero, we set the USPosition
9278 flag (this is useful when FLAGS is 0).
9279 The GTK version is in gtkutils.c */
9280
9281 #ifndef USE_GTK
9282 void
9283 x_wm_set_size_hint (f, flags, user_position)
9284 struct frame *f;
9285 long flags;
9286 int user_position;
9287 {
9288 XSizeHints size_hints;
9289
9290 #ifdef USE_X_TOOLKIT
9291 Arg al[2];
9292 int ac = 0;
9293 Dimension widget_width, widget_height;
9294 #endif
9295
9296 Window window = FRAME_OUTER_WINDOW (f);
9297
9298 /* Setting PMaxSize caused various problems. */
9299 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9300
9301 size_hints.x = f->output_data.x->left_pos;
9302 size_hints.y = f->output_data.x->top_pos;
9303
9304 #ifdef USE_X_TOOLKIT
9305 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
9306 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
9307 XtGetValues (f->output_data.x->widget, al, ac);
9308 size_hints.height = widget_height;
9309 size_hints.width = widget_width;
9310 #else /* not USE_X_TOOLKIT */
9311 size_hints.height = PIXEL_HEIGHT (f);
9312 size_hints.width = PIXEL_WIDTH (f);
9313 #endif /* not USE_X_TOOLKIT */
9314
9315 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
9316 size_hints.height_inc = f->output_data.x->line_height;
9317 size_hints.max_width
9318 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
9319 size_hints.max_height
9320 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
9321
9322 /* Calculate the base and minimum sizes.
9323
9324 (When we use the X toolkit, we don't do it here.
9325 Instead we copy the values that the widgets are using, below.) */
9326 #ifndef USE_X_TOOLKIT
9327 {
9328 int base_width, base_height;
9329 int min_rows = 0, min_cols = 0;
9330
9331 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
9332 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
9333
9334 check_frame_size (f, &min_rows, &min_cols);
9335
9336 /* The window manager uses the base width hints to calculate the
9337 current number of rows and columns in the frame while
9338 resizing; min_width and min_height aren't useful for this
9339 purpose, since they might not give the dimensions for a
9340 zero-row, zero-column frame.
9341
9342 We use the base_width and base_height members if we have
9343 them; otherwise, we set the min_width and min_height members
9344 to the size for a zero x zero frame. */
9345
9346 #ifdef HAVE_X11R4
9347 size_hints.flags |= PBaseSize;
9348 size_hints.base_width = base_width;
9349 size_hints.base_height = base_height;
9350 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9351 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9352 #else
9353 size_hints.min_width = base_width;
9354 size_hints.min_height = base_height;
9355 #endif
9356 }
9357
9358 /* If we don't need the old flags, we don't need the old hint at all. */
9359 if (flags)
9360 {
9361 size_hints.flags |= flags;
9362 goto no_read;
9363 }
9364 #endif /* not USE_X_TOOLKIT */
9365
9366 {
9367 XSizeHints hints; /* Sometimes I hate X Windows... */
9368 long supplied_return;
9369 int value;
9370
9371 #ifdef HAVE_X11R4
9372 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9373 &supplied_return);
9374 #else
9375 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
9376 #endif
9377
9378 #ifdef USE_X_TOOLKIT
9379 size_hints.base_height = hints.base_height;
9380 size_hints.base_width = hints.base_width;
9381 size_hints.min_height = hints.min_height;
9382 size_hints.min_width = hints.min_width;
9383 #endif
9384
9385 if (flags)
9386 size_hints.flags |= flags;
9387 else
9388 {
9389 if (value == 0)
9390 hints.flags = 0;
9391 if (hints.flags & PSize)
9392 size_hints.flags |= PSize;
9393 if (hints.flags & PPosition)
9394 size_hints.flags |= PPosition;
9395 if (hints.flags & USPosition)
9396 size_hints.flags |= USPosition;
9397 if (hints.flags & USSize)
9398 size_hints.flags |= USSize;
9399 }
9400 }
9401
9402 #ifndef USE_X_TOOLKIT
9403 no_read:
9404 #endif
9405
9406 #ifdef PWinGravity
9407 size_hints.win_gravity = f->output_data.x->win_gravity;
9408 size_hints.flags |= PWinGravity;
9409
9410 if (user_position)
9411 {
9412 size_hints.flags &= ~ PPosition;
9413 size_hints.flags |= USPosition;
9414 }
9415 #endif /* PWinGravity */
9416
9417 #ifdef HAVE_X11R4
9418 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9419 #else
9420 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9421 #endif
9422 }
9423 #endif /* not USE_GTK */
9424
9425 /* Used for IconicState or NormalState */
9426
9427 void
9428 x_wm_set_window_state (f, state)
9429 struct frame *f;
9430 int state;
9431 {
9432 #ifdef USE_X_TOOLKIT
9433 Arg al[1];
9434
9435 XtSetArg (al[0], XtNinitialState, state);
9436 XtSetValues (f->output_data.x->widget, al, 1);
9437 #else /* not USE_X_TOOLKIT */
9438 Window window = FRAME_X_WINDOW (f);
9439
9440 f->output_data.x->wm_hints.flags |= StateHint;
9441 f->output_data.x->wm_hints.initial_state = state;
9442
9443 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9444 #endif /* not USE_X_TOOLKIT */
9445 }
9446
9447 void
9448 x_wm_set_icon_pixmap (f, pixmap_id)
9449 struct frame *f;
9450 int pixmap_id;
9451 {
9452 Pixmap icon_pixmap;
9453
9454 #ifndef USE_X_TOOLKIT
9455 Window window = FRAME_OUTER_WINDOW (f);
9456 #endif
9457
9458 if (pixmap_id > 0)
9459 {
9460 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9461 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9462 }
9463 else
9464 {
9465 /* It seems there is no way to turn off use of an icon pixmap.
9466 The following line does it, only if no icon has yet been created,
9467 for some window managers. But with mwm it crashes.
9468 Some people say it should clear the IconPixmapHint bit in this case,
9469 but that doesn't work, and the X consortium said it isn't the
9470 right thing at all. Since there is no way to win,
9471 best to explicitly give up. */
9472 #if 0
9473 f->output_data.x->wm_hints.icon_pixmap = None;
9474 #else
9475 return;
9476 #endif
9477 }
9478
9479 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
9480
9481 {
9482 Arg al[1];
9483 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9484 XtSetValues (f->output_data.x->widget, al, 1);
9485 }
9486
9487 #else /* not USE_X_TOOLKIT */
9488
9489 f->output_data.x->wm_hints.flags |= IconPixmapHint;
9490 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9491
9492 #endif /* not USE_X_TOOLKIT */
9493 }
9494
9495 void
9496 x_wm_set_icon_position (f, icon_x, icon_y)
9497 struct frame *f;
9498 int icon_x, icon_y;
9499 {
9500 Window window = FRAME_OUTER_WINDOW (f);
9501
9502 f->output_data.x->wm_hints.flags |= IconPositionHint;
9503 f->output_data.x->wm_hints.icon_x = icon_x;
9504 f->output_data.x->wm_hints.icon_y = icon_y;
9505
9506 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9507 }
9508
9509 \f
9510 /***********************************************************************
9511 Fonts
9512 ***********************************************************************/
9513
9514 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
9515
9516 struct font_info *
9517 x_get_font_info (f, font_idx)
9518 FRAME_PTR f;
9519 int font_idx;
9520 {
9521 return (FRAME_X_FONT_TABLE (f) + font_idx);
9522 }
9523
9524
9525 /* Return a list of names of available fonts matching PATTERN on frame F.
9526
9527 If SIZE is > 0, it is the size (maximum bounds width) of fonts
9528 to be listed.
9529
9530 SIZE < 0 means include scalable fonts.
9531
9532 Frame F null means we have not yet created any frame on X, and
9533 consult the first display in x_display_list. MAXNAMES sets a limit
9534 on how many fonts to match. */
9535
9536 Lisp_Object
9537 x_list_fonts (f, pattern, size, maxnames)
9538 struct frame *f;
9539 Lisp_Object pattern;
9540 int size;
9541 int maxnames;
9542 {
9543 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
9544 Lisp_Object tem, second_best;
9545 struct x_display_info *dpyinfo
9546 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
9547 Display *dpy = dpyinfo->display;
9548 int try_XLoadQueryFont = 0;
9549 int count;
9550 int allow_scalable_fonts_p = 0;
9551
9552 if (size < 0)
9553 {
9554 allow_scalable_fonts_p = 1;
9555 size = 0;
9556 }
9557
9558 patterns = Fassoc (pattern, Valternate_fontname_alist);
9559 if (NILP (patterns))
9560 patterns = Fcons (pattern, Qnil);
9561
9562 if (maxnames == 1 && !size)
9563 /* We can return any single font matching PATTERN. */
9564 try_XLoadQueryFont = 1;
9565
9566 for (; CONSP (patterns); patterns = XCDR (patterns))
9567 {
9568 int num_fonts;
9569 char **names = NULL;
9570
9571 pattern = XCAR (patterns);
9572 /* See if we cached the result for this particular query.
9573 The cache is an alist of the form:
9574 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
9575 tem = XCDR (dpyinfo->name_list_element);
9576 key = Fcons (Fcons (pattern, make_number (maxnames)),
9577 allow_scalable_fonts_p ? Qt : Qnil);
9578 list = Fassoc (key, tem);
9579 if (!NILP (list))
9580 {
9581 list = Fcdr_safe (list);
9582 /* We have a cashed list. Don't have to get the list again. */
9583 goto label_cached;
9584 }
9585
9586 /* At first, put PATTERN in the cache. */
9587
9588 BLOCK_INPUT;
9589 count = x_catch_errors (dpy);
9590
9591 if (try_XLoadQueryFont)
9592 {
9593 XFontStruct *font;
9594 unsigned long value;
9595
9596 font = XLoadQueryFont (dpy, SDATA (pattern));
9597 if (x_had_errors_p (dpy))
9598 {
9599 /* This error is perhaps due to insufficient memory on X
9600 server. Let's just ignore it. */
9601 font = NULL;
9602 x_clear_errors (dpy);
9603 }
9604
9605 if (font
9606 && XGetFontProperty (font, XA_FONT, &value))
9607 {
9608 char *name = (char *) XGetAtomName (dpy, (Atom) value);
9609 int len = strlen (name);
9610 char *tmp;
9611
9612 /* If DXPC (a Differential X Protocol Compressor)
9613 Ver.3.7 is running, XGetAtomName will return null
9614 string. We must avoid such a name. */
9615 if (len == 0)
9616 try_XLoadQueryFont = 0;
9617 else
9618 {
9619 num_fonts = 1;
9620 names = (char **) alloca (sizeof (char *));
9621 /* Some systems only allow alloca assigned to a
9622 simple var. */
9623 tmp = (char *) alloca (len + 1); names[0] = tmp;
9624 bcopy (name, names[0], len + 1);
9625 XFree (name);
9626 }
9627 }
9628 else
9629 try_XLoadQueryFont = 0;
9630
9631 if (font)
9632 XFreeFont (dpy, font);
9633 }
9634
9635 if (!try_XLoadQueryFont)
9636 {
9637 /* We try at least 10 fonts because XListFonts will return
9638 auto-scaled fonts at the head. */
9639 if (maxnames < 0)
9640 {
9641 int limit;
9642
9643 for (limit = 500;;)
9644 {
9645 names = XListFonts (dpy, SDATA (pattern), limit, &num_fonts);
9646 if (num_fonts == limit)
9647 {
9648 BLOCK_INPUT;
9649 XFreeFontNames (names);
9650 UNBLOCK_INPUT;
9651 limit *= 2;
9652 }
9653 else
9654 break;
9655 }
9656 }
9657 else
9658 names = XListFonts (dpy, SDATA (pattern), max (maxnames, 10),
9659 &num_fonts);
9660
9661 if (x_had_errors_p (dpy))
9662 {
9663 /* This error is perhaps due to insufficient memory on X
9664 server. Let's just ignore it. */
9665 names = NULL;
9666 x_clear_errors (dpy);
9667 }
9668 }
9669
9670 x_uncatch_errors (dpy, count);
9671 UNBLOCK_INPUT;
9672
9673 if (names)
9674 {
9675 int i;
9676
9677 /* Make a list of all the fonts we got back.
9678 Store that in the font cache for the display. */
9679 for (i = 0; i < num_fonts; i++)
9680 {
9681 int width = 0;
9682 char *p = names[i];
9683 int average_width = -1, dashes = 0;
9684
9685 /* Count the number of dashes in NAMES[I]. If there are
9686 14 dashes, and the field value following 12th dash
9687 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
9688 is usually too ugly to be used for editing. Let's
9689 ignore it. */
9690 while (*p)
9691 if (*p++ == '-')
9692 {
9693 dashes++;
9694 if (dashes == 7) /* PIXEL_SIZE field */
9695 width = atoi (p);
9696 else if (dashes == 12) /* AVERAGE_WIDTH field */
9697 average_width = atoi (p);
9698 }
9699
9700 if (allow_scalable_fonts_p
9701 || dashes < 14 || average_width != 0)
9702 {
9703 tem = build_string (names[i]);
9704 if (NILP (Fassoc (tem, list)))
9705 {
9706 if (STRINGP (Vx_pixel_size_width_font_regexp)
9707 && ((fast_c_string_match_ignore_case
9708 (Vx_pixel_size_width_font_regexp, names[i]))
9709 >= 0))
9710 /* We can set the value of PIXEL_SIZE to the
9711 width of this font. */
9712 list = Fcons (Fcons (tem, make_number (width)), list);
9713 else
9714 /* For the moment, width is not known. */
9715 list = Fcons (Fcons (tem, Qnil), list);
9716 }
9717 }
9718 }
9719
9720 if (!try_XLoadQueryFont)
9721 {
9722 BLOCK_INPUT;
9723 XFreeFontNames (names);
9724 UNBLOCK_INPUT;
9725 }
9726 }
9727
9728 /* Now store the result in the cache. */
9729 XSETCDR (dpyinfo->name_list_element,
9730 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
9731
9732 label_cached:
9733 if (NILP (list)) continue; /* Try the remaining alternatives. */
9734
9735 newlist = second_best = Qnil;
9736 /* Make a list of the fonts that have the right width. */
9737 for (; CONSP (list); list = XCDR (list))
9738 {
9739 int found_size;
9740
9741 tem = XCAR (list);
9742
9743 if (!CONSP (tem) || NILP (XCAR (tem)))
9744 continue;
9745 if (!size)
9746 {
9747 newlist = Fcons (XCAR (tem), newlist);
9748 continue;
9749 }
9750
9751 if (!INTEGERP (XCDR (tem)))
9752 {
9753 /* Since we have not yet known the size of this font, we
9754 must try slow function call XLoadQueryFont. */
9755 XFontStruct *thisinfo;
9756
9757 BLOCK_INPUT;
9758 count = x_catch_errors (dpy);
9759 thisinfo = XLoadQueryFont (dpy,
9760 SDATA (XCAR (tem)));
9761 if (x_had_errors_p (dpy))
9762 {
9763 /* This error is perhaps due to insufficient memory on X
9764 server. Let's just ignore it. */
9765 thisinfo = NULL;
9766 x_clear_errors (dpy);
9767 }
9768 x_uncatch_errors (dpy, count);
9769 UNBLOCK_INPUT;
9770
9771 if (thisinfo)
9772 {
9773 XSETCDR (tem,
9774 (thisinfo->min_bounds.width == 0
9775 ? make_number (0)
9776 : make_number (thisinfo->max_bounds.width)));
9777 BLOCK_INPUT;
9778 XFreeFont (dpy, thisinfo);
9779 UNBLOCK_INPUT;
9780 }
9781 else
9782 /* For unknown reason, the previous call of XListFont had
9783 returned a font which can't be opened. Record the size
9784 as 0 not to try to open it again. */
9785 XSETCDR (tem, make_number (0));
9786 }
9787
9788 found_size = XINT (XCDR (tem));
9789 if (found_size == size)
9790 newlist = Fcons (XCAR (tem), newlist);
9791 else if (found_size > 0)
9792 {
9793 if (NILP (second_best))
9794 second_best = tem;
9795 else if (found_size < size)
9796 {
9797 if (XINT (XCDR (second_best)) > size
9798 || XINT (XCDR (second_best)) < found_size)
9799 second_best = tem;
9800 }
9801 else
9802 {
9803 if (XINT (XCDR (second_best)) > size
9804 && XINT (XCDR (second_best)) > found_size)
9805 second_best = tem;
9806 }
9807 }
9808 }
9809 if (!NILP (newlist))
9810 break;
9811 else if (!NILP (second_best))
9812 {
9813 newlist = Fcons (XCAR (second_best), Qnil);
9814 break;
9815 }
9816 }
9817
9818 return newlist;
9819 }
9820
9821
9822 #if GLYPH_DEBUG
9823
9824 /* Check that FONT is valid on frame F. It is if it can be found in F's
9825 font table. */
9826
9827 static void
9828 x_check_font (f, font)
9829 struct frame *f;
9830 XFontStruct *font;
9831 {
9832 int i;
9833 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9834
9835 xassert (font != NULL);
9836
9837 for (i = 0; i < dpyinfo->n_fonts; i++)
9838 if (dpyinfo->font_table[i].name
9839 && font == dpyinfo->font_table[i].font)
9840 break;
9841
9842 xassert (i < dpyinfo->n_fonts);
9843 }
9844
9845 #endif /* GLYPH_DEBUG != 0 */
9846
9847 /* Set *W to the minimum width, *H to the minimum font height of FONT.
9848 Note: There are (broken) X fonts out there with invalid XFontStruct
9849 min_bounds contents. For example, handa@etl.go.jp reports that
9850 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
9851 have font->min_bounds.width == 0. */
9852
9853 static INLINE void
9854 x_font_min_bounds (font, w, h)
9855 XFontStruct *font;
9856 int *w, *h;
9857 {
9858 *h = FONT_HEIGHT (font);
9859 *w = font->min_bounds.width;
9860
9861 /* Try to handle the case where FONT->min_bounds has invalid
9862 contents. Since the only font known to have invalid min_bounds
9863 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
9864 if (*w <= 0)
9865 *w = font->max_bounds.width;
9866 }
9867
9868
9869 /* Compute the smallest character width and smallest font height over
9870 all fonts available on frame F. Set the members smallest_char_width
9871 and smallest_font_height in F's x_display_info structure to
9872 the values computed. Value is non-zero if smallest_font_height or
9873 smallest_char_width become smaller than they were before. */
9874
9875 static int
9876 x_compute_min_glyph_bounds (f)
9877 struct frame *f;
9878 {
9879 int i;
9880 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9881 XFontStruct *font;
9882 int old_width = dpyinfo->smallest_char_width;
9883 int old_height = dpyinfo->smallest_font_height;
9884
9885 dpyinfo->smallest_font_height = 100000;
9886 dpyinfo->smallest_char_width = 100000;
9887
9888 for (i = 0; i < dpyinfo->n_fonts; ++i)
9889 if (dpyinfo->font_table[i].name)
9890 {
9891 struct font_info *fontp = dpyinfo->font_table + i;
9892 int w, h;
9893
9894 font = (XFontStruct *) fontp->font;
9895 xassert (font != (XFontStruct *) ~0);
9896 x_font_min_bounds (font, &w, &h);
9897
9898 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
9899 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
9900 }
9901
9902 xassert (dpyinfo->smallest_char_width > 0
9903 && dpyinfo->smallest_font_height > 0);
9904
9905 return (dpyinfo->n_fonts == 1
9906 || dpyinfo->smallest_char_width < old_width
9907 || dpyinfo->smallest_font_height < old_height);
9908 }
9909
9910
9911 /* Load font named FONTNAME of the size SIZE for frame F, and return a
9912 pointer to the structure font_info while allocating it dynamically.
9913 If SIZE is 0, load any size of font.
9914 If loading is failed, return NULL. */
9915
9916 struct font_info *
9917 x_load_font (f, fontname, size)
9918 struct frame *f;
9919 register char *fontname;
9920 int size;
9921 {
9922 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9923 Lisp_Object font_names;
9924 int count;
9925
9926 /* Get a list of all the fonts that match this name. Once we
9927 have a list of matching fonts, we compare them against the fonts
9928 we already have by comparing names. */
9929 font_names = x_list_fonts (f, build_string (fontname), size, 1);
9930
9931 if (!NILP (font_names))
9932 {
9933 Lisp_Object tail;
9934 int i;
9935
9936 for (i = 0; i < dpyinfo->n_fonts; i++)
9937 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
9938 if (dpyinfo->font_table[i].name
9939 && (!strcmp (dpyinfo->font_table[i].name,
9940 SDATA (XCAR (tail)))
9941 || !strcmp (dpyinfo->font_table[i].full_name,
9942 SDATA (XCAR (tail)))))
9943 return (dpyinfo->font_table + i);
9944 }
9945
9946 /* Load the font and add it to the table. */
9947 {
9948 char *full_name;
9949 XFontStruct *font;
9950 struct font_info *fontp;
9951 unsigned long value;
9952 int i;
9953
9954 /* If we have found fonts by x_list_font, load one of them. If
9955 not, we still try to load a font by the name given as FONTNAME
9956 because XListFonts (called in x_list_font) of some X server has
9957 a bug of not finding a font even if the font surely exists and
9958 is loadable by XLoadQueryFont. */
9959 if (size > 0 && !NILP (font_names))
9960 fontname = (char *) SDATA (XCAR (font_names));
9961
9962 BLOCK_INPUT;
9963 count = x_catch_errors (FRAME_X_DISPLAY (f));
9964 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
9965 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
9966 {
9967 /* This error is perhaps due to insufficient memory on X
9968 server. Let's just ignore it. */
9969 font = NULL;
9970 x_clear_errors (FRAME_X_DISPLAY (f));
9971 }
9972 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
9973 UNBLOCK_INPUT;
9974 if (!font)
9975 return NULL;
9976
9977 /* Find a free slot in the font table. */
9978 for (i = 0; i < dpyinfo->n_fonts; ++i)
9979 if (dpyinfo->font_table[i].name == NULL)
9980 break;
9981
9982 /* If no free slot found, maybe enlarge the font table. */
9983 if (i == dpyinfo->n_fonts
9984 && dpyinfo->n_fonts == dpyinfo->font_table_size)
9985 {
9986 int sz;
9987 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
9988 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
9989 dpyinfo->font_table
9990 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
9991 }
9992
9993 fontp = dpyinfo->font_table + i;
9994 if (i == dpyinfo->n_fonts)
9995 ++dpyinfo->n_fonts;
9996
9997 /* Now fill in the slots of *FONTP. */
9998 BLOCK_INPUT;
9999 fontp->font = font;
10000 fontp->font_idx = i;
10001 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
10002 bcopy (fontname, fontp->name, strlen (fontname) + 1);
10003
10004 /* Try to get the full name of FONT. Put it in FULL_NAME. */
10005 full_name = 0;
10006 if (XGetFontProperty (font, XA_FONT, &value))
10007 {
10008 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
10009 char *p = name;
10010 int dashes = 0;
10011
10012 /* Count the number of dashes in the "full name".
10013 If it is too few, this isn't really the font's full name,
10014 so don't use it.
10015 In X11R4, the fonts did not come with their canonical names
10016 stored in them. */
10017 while (*p)
10018 {
10019 if (*p == '-')
10020 dashes++;
10021 p++;
10022 }
10023
10024 if (dashes >= 13)
10025 {
10026 full_name = (char *) xmalloc (p - name + 1);
10027 bcopy (name, full_name, p - name + 1);
10028 }
10029
10030 XFree (name);
10031 }
10032
10033 if (full_name != 0)
10034 fontp->full_name = full_name;
10035 else
10036 fontp->full_name = fontp->name;
10037
10038 fontp->size = font->max_bounds.width;
10039 fontp->height = FONT_HEIGHT (font);
10040
10041 if (NILP (font_names))
10042 {
10043 /* We come here because of a bug of XListFonts mentioned at
10044 the head of this block. Let's store this information in
10045 the cache for x_list_fonts. */
10046 Lisp_Object lispy_name = build_string (fontname);
10047 Lisp_Object lispy_full_name = build_string (fontp->full_name);
10048 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
10049 Qnil);
10050
10051 XSETCDR (dpyinfo->name_list_element,
10052 Fcons (Fcons (key,
10053 Fcons (Fcons (lispy_full_name,
10054 make_number (fontp->size)),
10055 Qnil)),
10056 XCDR (dpyinfo->name_list_element)));
10057 if (full_name)
10058 {
10059 key = Fcons (Fcons (lispy_full_name, make_number (256)),
10060 Qnil);
10061 XSETCDR (dpyinfo->name_list_element,
10062 Fcons (Fcons (key,
10063 Fcons (Fcons (lispy_full_name,
10064 make_number (fontp->size)),
10065 Qnil)),
10066 XCDR (dpyinfo->name_list_element)));
10067 }
10068 }
10069
10070 /* The slot `encoding' specifies how to map a character
10071 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
10072 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
10073 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
10074 2:0xA020..0xFF7F). For the moment, we don't know which charset
10075 uses this font. So, we set information in fontp->encoding[1]
10076 which is never used by any charset. If mapping can't be
10077 decided, set FONT_ENCODING_NOT_DECIDED. */
10078 fontp->encoding[1]
10079 = (font->max_byte1 == 0
10080 /* 1-byte font */
10081 ? (font->min_char_or_byte2 < 0x80
10082 ? (font->max_char_or_byte2 < 0x80
10083 ? 0 /* 0x20..0x7F */
10084 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
10085 : 1) /* 0xA0..0xFF */
10086 /* 2-byte font */
10087 : (font->min_byte1 < 0x80
10088 ? (font->max_byte1 < 0x80
10089 ? (font->min_char_or_byte2 < 0x80
10090 ? (font->max_char_or_byte2 < 0x80
10091 ? 0 /* 0x2020..0x7F7F */
10092 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
10093 : 3) /* 0x20A0..0x7FFF */
10094 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
10095 : (font->min_char_or_byte2 < 0x80
10096 ? (font->max_char_or_byte2 < 0x80
10097 ? 2 /* 0xA020..0xFF7F */
10098 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
10099 : 1))); /* 0xA0A0..0xFFFF */
10100
10101 fontp->baseline_offset
10102 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
10103 ? (long) value : 0);
10104 fontp->relative_compose
10105 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
10106 ? (long) value : 0);
10107 fontp->default_ascent
10108 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
10109 ? (long) value : 0);
10110
10111 /* Set global flag fonts_changed_p to non-zero if the font loaded
10112 has a character with a smaller width than any other character
10113 before, or if the font loaded has a smaller height than any
10114 other font loaded before. If this happens, it will make a
10115 glyph matrix reallocation necessary. */
10116 fonts_changed_p |= x_compute_min_glyph_bounds (f);
10117 UNBLOCK_INPUT;
10118 return fontp;
10119 }
10120 }
10121
10122
10123 /* Return a pointer to struct font_info of a font named FONTNAME for
10124 frame F. If no such font is loaded, return NULL. */
10125
10126 struct font_info *
10127 x_query_font (f, fontname)
10128 struct frame *f;
10129 register char *fontname;
10130 {
10131 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10132 int i;
10133
10134 for (i = 0; i < dpyinfo->n_fonts; i++)
10135 if (dpyinfo->font_table[i].name
10136 && (!strcmp (dpyinfo->font_table[i].name, fontname)
10137 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
10138 return (dpyinfo->font_table + i);
10139 return NULL;
10140 }
10141
10142
10143 /* Find a CCL program for a font specified by FONTP, and set the member
10144 `encoder' of the structure. */
10145
10146 void
10147 x_find_ccl_program (fontp)
10148 struct font_info *fontp;
10149 {
10150 Lisp_Object list, elt;
10151
10152 elt = Qnil;
10153 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
10154 {
10155 elt = XCAR (list);
10156 if (CONSP (elt)
10157 && STRINGP (XCAR (elt))
10158 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
10159 >= 0)
10160 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
10161 >= 0)))
10162 break;
10163 }
10164
10165 if (! NILP (list))
10166 {
10167 struct ccl_program *ccl
10168 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
10169
10170 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
10171 xfree (ccl);
10172 else
10173 fontp->font_encoder = ccl;
10174 }
10175 }
10176
10177
10178 \f
10179 /***********************************************************************
10180 Initialization
10181 ***********************************************************************/
10182
10183 #ifdef USE_X_TOOLKIT
10184 static XrmOptionDescRec emacs_options[] = {
10185 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
10186 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
10187
10188 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
10189 XrmoptionSepArg, NULL},
10190 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
10191
10192 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10193 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10194 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10195 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10196 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10197 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
10198 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
10199 };
10200 #endif /* USE_X_TOOLKIT */
10201
10202 static int x_initialized;
10203
10204 #ifdef MULTI_KBOARD
10205 /* Test whether two display-name strings agree up to the dot that separates
10206 the screen number from the server number. */
10207 static int
10208 same_x_server (name1, name2)
10209 const char *name1, *name2;
10210 {
10211 int seen_colon = 0;
10212 const unsigned char *system_name = SDATA (Vsystem_name);
10213 int system_name_length = strlen (system_name);
10214 int length_until_period = 0;
10215
10216 while (system_name[length_until_period] != 0
10217 && system_name[length_until_period] != '.')
10218 length_until_period++;
10219
10220 /* Treat `unix' like an empty host name. */
10221 if (! strncmp (name1, "unix:", 5))
10222 name1 += 4;
10223 if (! strncmp (name2, "unix:", 5))
10224 name2 += 4;
10225 /* Treat this host's name like an empty host name. */
10226 if (! strncmp (name1, system_name, system_name_length)
10227 && name1[system_name_length] == ':')
10228 name1 += system_name_length;
10229 if (! strncmp (name2, system_name, system_name_length)
10230 && name2[system_name_length] == ':')
10231 name2 += system_name_length;
10232 /* Treat this host's domainless name like an empty host name. */
10233 if (! strncmp (name1, system_name, length_until_period)
10234 && name1[length_until_period] == ':')
10235 name1 += length_until_period;
10236 if (! strncmp (name2, system_name, length_until_period)
10237 && name2[length_until_period] == ':')
10238 name2 += length_until_period;
10239
10240 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
10241 {
10242 if (*name1 == ':')
10243 seen_colon++;
10244 if (seen_colon && *name1 == '.')
10245 return 1;
10246 }
10247 return (seen_colon
10248 && (*name1 == '.' || *name1 == '\0')
10249 && (*name2 == '.' || *name2 == '\0'));
10250 }
10251 #endif
10252
10253 struct x_display_info *
10254 x_term_init (display_name, xrm_option, resource_name)
10255 Lisp_Object display_name;
10256 char *xrm_option;
10257 char *resource_name;
10258 {
10259 int connection;
10260 Display *dpy;
10261 struct x_display_info *dpyinfo;
10262 XrmDatabase xrdb;
10263
10264 BLOCK_INPUT;
10265
10266 if (!x_initialized)
10267 {
10268 x_initialize ();
10269 x_initialized = 1;
10270 }
10271
10272 #ifdef USE_GTK
10273 {
10274 #define NUM_ARGV 10
10275 int argc;
10276 char *argv[NUM_ARGV];
10277 char **argv2 = argv;
10278 GdkAtom atom;
10279
10280 /* GTK 2.0 can only handle one display, GTK 2.2 can handle more
10281 than one, but this remains to be implemented. */
10282 if (x_initialized > 1)
10283 return 0;
10284
10285 x_initialized++;
10286
10287 for (argc = 0; argc < NUM_ARGV; ++argc)
10288 argv[argc] = 0;
10289
10290 argc = 0;
10291 argv[argc++] = initial_argv[0];
10292
10293 if (! NILP (display_name))
10294 {
10295 argv[argc++] = "--display";
10296 argv[argc++] = SDATA (display_name);
10297 }
10298
10299 argv[argc++] = "--name";
10300 argv[argc++] = resource_name;
10301
10302 #ifdef HAVE_X11R5
10303 XSetLocaleModifiers ("");
10304 #endif
10305
10306 gtk_init (&argc, &argv2);
10307
10308 /* gtk_init does set_locale. We must fix locale after calling it. */
10309 fixup_locale ();
10310 xg_initialize ();
10311
10312 dpy = GDK_DISPLAY ();
10313
10314 /* NULL window -> events for all windows go to our function */
10315 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
10316
10317 /* Load our own gtkrc if it exists. */
10318 {
10319 struct gcpro gcpro1, gcpro2;
10320 char *file = "~/.emacs.d/gtkrc";
10321 Lisp_Object s, abs_file;
10322
10323 GCPRO2 (s, abs_file);
10324 s = make_string (file, strlen (file));
10325 abs_file = Fexpand_file_name(s, Qnil);
10326
10327 if (! NILP (abs_file) && Ffile_readable_p (abs_file))
10328 gtk_rc_parse (SDATA (abs_file));
10329
10330 UNGCPRO;
10331 }
10332
10333 XSetErrorHandler (x_error_handler);
10334 XSetIOErrorHandler (x_io_error_quitter);
10335 }
10336 #else /* not USE_GTK */
10337 #ifdef USE_X_TOOLKIT
10338 /* weiner@footloose.sps.mot.com reports that this causes
10339 errors with X11R5:
10340 X protocol error: BadAtom (invalid Atom parameter)
10341 on protocol request 18skiloaf.
10342 So let's not use it until R6. */
10343 #ifdef HAVE_X11XTR6
10344 XtSetLanguageProc (NULL, NULL, NULL);
10345 #endif
10346
10347 {
10348 int argc = 0;
10349 char *argv[3];
10350
10351 argv[0] = "";
10352 argc = 1;
10353 if (xrm_option)
10354 {
10355 argv[argc++] = "-xrm";
10356 argv[argc++] = xrm_option;
10357 }
10358 turn_on_atimers (0);
10359 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
10360 resource_name, EMACS_CLASS,
10361 emacs_options, XtNumber (emacs_options),
10362 &argc, argv);
10363 turn_on_atimers (1);
10364
10365 #ifdef HAVE_X11XTR6
10366 /* I think this is to compensate for XtSetLanguageProc. */
10367 fixup_locale ();
10368 #endif
10369 }
10370
10371 #else /* not USE_X_TOOLKIT */
10372 #ifdef HAVE_X11R5
10373 XSetLocaleModifiers ("");
10374 #endif
10375 dpy = XOpenDisplay (SDATA (display_name));
10376 #endif /* not USE_X_TOOLKIT */
10377 #endif /* not USE_GTK*/
10378
10379 /* Detect failure. */
10380 if (dpy == 0)
10381 {
10382 UNBLOCK_INPUT;
10383 return 0;
10384 }
10385
10386 /* We have definitely succeeded. Record the new connection. */
10387
10388 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
10389 bzero (dpyinfo, sizeof *dpyinfo);
10390
10391 #ifdef MULTI_KBOARD
10392 {
10393 struct x_display_info *share;
10394 Lisp_Object tail;
10395
10396 for (share = x_display_list, tail = x_display_name_list; share;
10397 share = share->next, tail = XCDR (tail))
10398 if (same_x_server (SDATA (XCAR (XCAR (tail))),
10399 SDATA (display_name)))
10400 break;
10401 if (share)
10402 dpyinfo->kboard = share->kboard;
10403 else
10404 {
10405 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
10406 init_kboard (dpyinfo->kboard);
10407 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10408 {
10409 char *vendor = ServerVendor (dpy);
10410 UNBLOCK_INPUT;
10411 dpyinfo->kboard->Vsystem_key_alist
10412 = call1 (Qvendor_specific_keysyms,
10413 build_string (vendor ? vendor : ""));
10414 BLOCK_INPUT;
10415 }
10416
10417 dpyinfo->kboard->next_kboard = all_kboards;
10418 all_kboards = dpyinfo->kboard;
10419 /* Don't let the initial kboard remain current longer than necessary.
10420 That would cause problems if a file loaded on startup tries to
10421 prompt in the mini-buffer. */
10422 if (current_kboard == initial_kboard)
10423 current_kboard = dpyinfo->kboard;
10424 }
10425 dpyinfo->kboard->reference_count++;
10426 }
10427 #endif
10428
10429 /* Put this display on the chain. */
10430 dpyinfo->next = x_display_list;
10431 x_display_list = dpyinfo;
10432
10433 /* Put it on x_display_name_list as well, to keep them parallel. */
10434 x_display_name_list = Fcons (Fcons (display_name, Qnil),
10435 x_display_name_list);
10436 dpyinfo->name_list_element = XCAR (x_display_name_list);
10437
10438 dpyinfo->display = dpy;
10439
10440 #if 0
10441 XSetAfterFunction (x_current_display, x_trace_wire);
10442 #endif /* ! 0 */
10443
10444 dpyinfo->x_id_name
10445 = (char *) xmalloc (SBYTES (Vinvocation_name)
10446 + SBYTES (Vsystem_name)
10447 + 2);
10448 sprintf (dpyinfo->x_id_name, "%s@%s",
10449 SDATA (Vinvocation_name), SDATA (Vsystem_name));
10450
10451 /* Figure out which modifier bits mean what. */
10452 x_find_modifier_meanings (dpyinfo);
10453
10454 /* Get the scroll bar cursor. */
10455 dpyinfo->vertical_scroll_bar_cursor
10456 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10457
10458 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10459 resource_name, EMACS_CLASS);
10460 #ifdef HAVE_XRMSETDATABASE
10461 XrmSetDatabase (dpyinfo->display, xrdb);
10462 #else
10463 dpyinfo->display->db = xrdb;
10464 #endif
10465 /* Put the rdb where we can find it in a way that works on
10466 all versions. */
10467 dpyinfo->xrdb = xrdb;
10468
10469 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10470 DefaultScreen (dpyinfo->display));
10471 select_visual (dpyinfo);
10472 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10473 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
10474 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
10475 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10476 dpyinfo->grabbed = 0;
10477 dpyinfo->reference_count = 0;
10478 dpyinfo->icon_bitmap_id = -1;
10479 dpyinfo->font_table = NULL;
10480 dpyinfo->n_fonts = 0;
10481 dpyinfo->font_table_size = 0;
10482 dpyinfo->bitmaps = 0;
10483 dpyinfo->bitmaps_size = 0;
10484 dpyinfo->bitmaps_last = 0;
10485 dpyinfo->scratch_cursor_gc = 0;
10486 dpyinfo->mouse_face_mouse_frame = 0;
10487 dpyinfo->mouse_face_deferred_gc = 0;
10488 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10489 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
10490 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10491 dpyinfo->mouse_face_window = Qnil;
10492 dpyinfo->mouse_face_overlay = Qnil;
10493 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
10494 dpyinfo->mouse_face_defer = 0;
10495 dpyinfo->mouse_face_hidden = 0;
10496 dpyinfo->x_focus_frame = 0;
10497 dpyinfo->x_focus_event_frame = 0;
10498 dpyinfo->x_highlight_frame = 0;
10499 dpyinfo->image_cache = make_image_cache ();
10500
10501 /* See if a private colormap is requested. */
10502 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10503 {
10504 if (dpyinfo->visual->class == PseudoColor)
10505 {
10506 Lisp_Object value;
10507 value = display_x_get_resource (dpyinfo,
10508 build_string ("privateColormap"),
10509 build_string ("PrivateColormap"),
10510 Qnil, Qnil);
10511 if (STRINGP (value)
10512 && (!strcmp (SDATA (value), "true")
10513 || !strcmp (SDATA (value), "on")))
10514 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10515 }
10516 }
10517 else
10518 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10519 dpyinfo->visual, AllocNone);
10520
10521 {
10522 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10523 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10524 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
10525 dpyinfo->resy = pixels * 25.4 / mm;
10526 pixels = DisplayWidth (dpyinfo->display, screen_number);
10527 mm = DisplayWidthMM (dpyinfo->display, screen_number);
10528 dpyinfo->resx = pixels * 25.4 / mm;
10529 }
10530
10531 dpyinfo->Xatom_wm_protocols
10532 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
10533 dpyinfo->Xatom_wm_take_focus
10534 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
10535 dpyinfo->Xatom_wm_save_yourself
10536 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
10537 dpyinfo->Xatom_wm_delete_window
10538 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
10539 dpyinfo->Xatom_wm_change_state
10540 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
10541 dpyinfo->Xatom_wm_configure_denied
10542 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
10543 dpyinfo->Xatom_wm_window_moved
10544 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
10545 dpyinfo->Xatom_editres
10546 = XInternAtom (dpyinfo->display, "Editres", False);
10547 dpyinfo->Xatom_CLIPBOARD
10548 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
10549 dpyinfo->Xatom_TIMESTAMP
10550 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
10551 dpyinfo->Xatom_TEXT
10552 = XInternAtom (dpyinfo->display, "TEXT", False);
10553 dpyinfo->Xatom_COMPOUND_TEXT
10554 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
10555 dpyinfo->Xatom_UTF8_STRING
10556 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
10557 dpyinfo->Xatom_DELETE
10558 = XInternAtom (dpyinfo->display, "DELETE", False);
10559 dpyinfo->Xatom_MULTIPLE
10560 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
10561 dpyinfo->Xatom_INCR
10562 = XInternAtom (dpyinfo->display, "INCR", False);
10563 dpyinfo->Xatom_EMACS_TMP
10564 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
10565 dpyinfo->Xatom_TARGETS
10566 = XInternAtom (dpyinfo->display, "TARGETS", False);
10567 dpyinfo->Xatom_NULL
10568 = XInternAtom (dpyinfo->display, "NULL", False);
10569 dpyinfo->Xatom_ATOM_PAIR
10570 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
10571 /* For properties of font. */
10572 dpyinfo->Xatom_PIXEL_SIZE
10573 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
10574 dpyinfo->Xatom_MULE_BASELINE_OFFSET
10575 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
10576 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
10577 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
10578 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
10579 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
10580
10581 /* Ghostscript support. */
10582 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
10583 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
10584
10585 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
10586 False);
10587
10588 dpyinfo->cut_buffers_initialized = 0;
10589
10590 connection = ConnectionNumber (dpyinfo->display);
10591 dpyinfo->connection = connection;
10592
10593 {
10594 char null_bits[1];
10595
10596 null_bits[0] = 0x00;
10597
10598 dpyinfo->null_pixel
10599 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10600 null_bits, 1, 1, (long) 0, (long) 0,
10601 1);
10602 }
10603
10604 {
10605 extern int gray_bitmap_width, gray_bitmap_height;
10606 extern char *gray_bitmap_bits;
10607 dpyinfo->gray
10608 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10609 gray_bitmap_bits,
10610 gray_bitmap_width, gray_bitmap_height,
10611 (unsigned long) 1, (unsigned long) 0, 1);
10612 }
10613
10614 #ifdef HAVE_X_I18N
10615 xim_initialize (dpyinfo, resource_name);
10616 #endif
10617
10618 #ifdef subprocesses
10619 /* This is only needed for distinguishing keyboard and process input. */
10620 if (connection != 0)
10621 add_keyboard_wait_descriptor (connection);
10622 #endif
10623
10624 #ifndef F_SETOWN_BUG
10625 #ifdef F_SETOWN
10626 #ifdef F_SETOWN_SOCK_NEG
10627 /* stdin is a socket here */
10628 fcntl (connection, F_SETOWN, -getpid ());
10629 #else /* ! defined (F_SETOWN_SOCK_NEG) */
10630 fcntl (connection, F_SETOWN, getpid ());
10631 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
10632 #endif /* ! defined (F_SETOWN) */
10633 #endif /* F_SETOWN_BUG */
10634
10635 #ifdef SIGIO
10636 if (interrupt_input)
10637 init_sigio (connection);
10638 #endif /* ! defined (SIGIO) */
10639
10640 #ifdef USE_LUCID
10641 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
10642 /* Make sure that we have a valid font for dialog boxes
10643 so that Xt does not crash. */
10644 {
10645 Display *dpy = dpyinfo->display;
10646 XrmValue d, fr, to;
10647 Font font;
10648 int count;
10649
10650 d.addr = (XPointer)&dpy;
10651 d.size = sizeof (Display *);
10652 fr.addr = XtDefaultFont;
10653 fr.size = sizeof (XtDefaultFont);
10654 to.size = sizeof (Font *);
10655 to.addr = (XPointer)&font;
10656 count = x_catch_errors (dpy);
10657 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
10658 abort ();
10659 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
10660 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
10661 x_uncatch_errors (dpy, count);
10662 }
10663 #endif
10664 #endif
10665
10666 /* See if we should run in synchronous mode. This is useful
10667 for debugging X code. */
10668 {
10669 Lisp_Object value;
10670 value = display_x_get_resource (dpyinfo,
10671 build_string ("synchronous"),
10672 build_string ("Synchronous"),
10673 Qnil, Qnil);
10674 if (STRINGP (value)
10675 && (!strcmp (SDATA (value), "true")
10676 || !strcmp (SDATA (value), "on")))
10677 XSynchronize (dpyinfo->display, True);
10678 }
10679
10680 UNBLOCK_INPUT;
10681
10682 return dpyinfo;
10683 }
10684 \f
10685 /* Get rid of display DPYINFO, assuming all frames are already gone,
10686 and without sending any more commands to the X server. */
10687
10688 void
10689 x_delete_display (dpyinfo)
10690 struct x_display_info *dpyinfo;
10691 {
10692 delete_keyboard_wait_descriptor (dpyinfo->connection);
10693
10694 /* Discard this display from x_display_name_list and x_display_list.
10695 We can't use Fdelq because that can quit. */
10696 if (! NILP (x_display_name_list)
10697 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
10698 x_display_name_list = XCDR (x_display_name_list);
10699 else
10700 {
10701 Lisp_Object tail;
10702
10703 tail = x_display_name_list;
10704 while (CONSP (tail) && CONSP (XCDR (tail)))
10705 {
10706 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
10707 {
10708 XSETCDR (tail, XCDR (XCDR (tail)));
10709 break;
10710 }
10711 tail = XCDR (tail);
10712 }
10713 }
10714
10715 if (next_noop_dpyinfo == dpyinfo)
10716 next_noop_dpyinfo = dpyinfo->next;
10717
10718 if (x_display_list == dpyinfo)
10719 x_display_list = dpyinfo->next;
10720 else
10721 {
10722 struct x_display_info *tail;
10723
10724 for (tail = x_display_list; tail; tail = tail->next)
10725 if (tail->next == dpyinfo)
10726 tail->next = tail->next->next;
10727 }
10728
10729 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
10730 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
10731 XrmDestroyDatabase (dpyinfo->xrdb);
10732 #endif
10733 #endif
10734 #ifdef MULTI_KBOARD
10735 if (--dpyinfo->kboard->reference_count == 0)
10736 delete_kboard (dpyinfo->kboard);
10737 #endif
10738 #ifdef HAVE_X_I18N
10739 if (dpyinfo->xim)
10740 xim_close_dpy (dpyinfo);
10741 #endif
10742
10743 xfree (dpyinfo->font_table);
10744 xfree (dpyinfo->x_id_name);
10745 xfree (dpyinfo->color_cells);
10746 xfree (dpyinfo);
10747 }
10748
10749 #ifdef USE_X_TOOLKIT
10750
10751 /* Atimer callback function for TIMER. Called every 0.1s to process
10752 Xt timeouts, if needed. We must avoid calling XtAppPending as
10753 much as possible because that function does an implicit XFlush
10754 that slows us down. */
10755
10756 static void
10757 x_process_timeouts (timer)
10758 struct atimer *timer;
10759 {
10760 if (toolkit_scroll_bar_interaction || popup_activated ())
10761 {
10762 BLOCK_INPUT;
10763 while (XtAppPending (Xt_app_con) & XtIMTimer)
10764 XtAppProcessEvent (Xt_app_con, XtIMTimer);
10765 UNBLOCK_INPUT;
10766 }
10767 }
10768
10769 #endif /* USE_X_TOOLKIT */
10770
10771 \f
10772 /* Set up use of X before we make the first connection. */
10773
10774 static struct redisplay_interface x_redisplay_interface =
10775 {
10776 x_produce_glyphs,
10777 x_write_glyphs,
10778 x_insert_glyphs,
10779 x_clear_end_of_line,
10780 x_scroll_run,
10781 x_after_update_window_line,
10782 x_update_window_begin,
10783 x_update_window_end,
10784 x_cursor_to,
10785 x_flush,
10786 #ifndef XFlush
10787 x_flush,
10788 #else
10789 0, /* flush_display_optional */
10790 #endif
10791 x_clear_window_mouse_face,
10792 x_get_glyph_overhangs,
10793 x_fix_overlapping_area,
10794 x_draw_fringe_bitmap,
10795 x_per_char_metric,
10796 x_encode_char,
10797 x_compute_glyph_string_overhangs,
10798 x_draw_glyph_string,
10799 x_define_frame_cursor,
10800 x_clear_frame_area,
10801 x_draw_window_cursor,
10802 x_draw_vertical_window_border,
10803 x_shift_glyphs_for_insert
10804 };
10805
10806 void
10807 x_initialize ()
10808 {
10809 rif = &x_redisplay_interface;
10810
10811 clear_frame_hook = x_clear_frame;
10812 ins_del_lines_hook = x_ins_del_lines;
10813 delete_glyphs_hook = x_delete_glyphs;
10814 ring_bell_hook = XTring_bell;
10815 reset_terminal_modes_hook = XTreset_terminal_modes;
10816 set_terminal_modes_hook = XTset_terminal_modes;
10817 update_begin_hook = x_update_begin;
10818 update_end_hook = x_update_end;
10819 set_terminal_window_hook = XTset_terminal_window;
10820 read_socket_hook = XTread_socket;
10821 frame_up_to_date_hook = XTframe_up_to_date;
10822 mouse_position_hook = XTmouse_position;
10823 frame_rehighlight_hook = XTframe_rehighlight;
10824 frame_raise_lower_hook = XTframe_raise_lower;
10825 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
10826 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
10827 redeem_scroll_bar_hook = XTredeem_scroll_bar;
10828 judge_scroll_bars_hook = XTjudge_scroll_bars;
10829
10830 scroll_region_ok = 1; /* we'll scroll partial frames */
10831 char_ins_del_ok = 1;
10832 line_ins_del_ok = 1; /* we'll just blt 'em */
10833 fast_clear_end_of_line = 1; /* X does this well */
10834 memory_below_frame = 0; /* we don't remember what scrolls
10835 off the bottom */
10836 baud_rate = 19200;
10837
10838 x_noop_count = 0;
10839 last_tool_bar_item = -1;
10840 any_help_event_p = 0;
10841
10842 /* Try to use interrupt input; if we can't, then start polling. */
10843 Fset_input_mode (Qt, Qnil, Qt, Qnil);
10844
10845 #ifdef USE_X_TOOLKIT
10846 XtToolkitInitialize ();
10847
10848 Xt_app_con = XtCreateApplicationContext ();
10849
10850 /* Register a converter from strings to pixels, which uses
10851 Emacs' color allocation infrastructure. */
10852 XtAppSetTypeConverter (Xt_app_con,
10853 XtRString, XtRPixel, cvt_string_to_pixel,
10854 cvt_string_to_pixel_args,
10855 XtNumber (cvt_string_to_pixel_args),
10856 XtCacheByDisplay, cvt_pixel_dtor);
10857
10858 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
10859
10860 /* Install an asynchronous timer that processes Xt timeout events
10861 every 0.1s. This is necessary because some widget sets use
10862 timeouts internally, for example the LessTif menu bar, or the
10863 Xaw3d scroll bar. When Xt timouts aren't processed, these
10864 widgets don't behave normally. */
10865 {
10866 EMACS_TIME interval;
10867 EMACS_SET_SECS_USECS (interval, 0, 100000);
10868 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
10869 }
10870 #endif
10871
10872 #ifdef USE_TOOLKIT_SCROLL_BARS
10873 #ifndef USE_GTK
10874 xaw3d_arrow_scroll = False;
10875 xaw3d_pick_top = True;
10876 #endif
10877 #endif
10878
10879 /* Note that there is no real way portable across R3/R4 to get the
10880 original error handler. */
10881 XSetErrorHandler (x_error_handler);
10882 XSetIOErrorHandler (x_io_error_quitter);
10883
10884 /* Disable Window Change signals; they are handled by X events. */
10885 #ifdef SIGWINCH
10886 signal (SIGWINCH, SIG_DFL);
10887 #endif /* SIGWINCH */
10888
10889 signal (SIGPIPE, x_connection_signal);
10890
10891 #ifdef HAVE_X_SM
10892 x_session_initialize ();
10893 #endif
10894 }
10895
10896
10897 void
10898 syms_of_xterm ()
10899 {
10900 staticpro (&x_error_message_string);
10901 x_error_message_string = Qnil;
10902
10903 staticpro (&x_display_name_list);
10904 x_display_name_list = Qnil;
10905
10906 staticpro (&last_mouse_scroll_bar);
10907 last_mouse_scroll_bar = Qnil;
10908
10909 staticpro (&Qvendor_specific_keysyms);
10910 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
10911
10912 staticpro (&Qutf_8);
10913 Qutf_8 = intern ("utf-8");
10914 staticpro (&Qlatin_1);
10915 Qlatin_1 = intern ("latin-1");
10916
10917 staticpro (&last_mouse_press_frame);
10918 last_mouse_press_frame = Qnil;
10919
10920 DEFVAR_BOOL ("x-use-underline-position-properties",
10921 &x_use_underline_position_properties,
10922 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
10923 nil means ignore them. If you encounter fonts with bogus
10924 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
10925 to 4.1, set this to nil. */);
10926 x_use_underline_position_properties = 1;
10927
10928 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
10929 doc: /* What X toolkit scroll bars Emacs uses.
10930 A value of nil means Emacs doesn't use X toolkit scroll bars.
10931 Otherwise, value is a symbol describing the X toolkit. */);
10932 #ifdef USE_TOOLKIT_SCROLL_BARS
10933 #ifdef USE_MOTIF
10934 Vx_toolkit_scroll_bars = intern ("motif");
10935 #elif defined HAVE_XAW3D
10936 Vx_toolkit_scroll_bars = intern ("xaw3d");
10937 #elif USE_GTK
10938 Vx_toolkit_scroll_bars = intern ("gtk");
10939 #else
10940 Vx_toolkit_scroll_bars = intern ("xaw");
10941 #endif
10942 #else
10943 Vx_toolkit_scroll_bars = Qnil;
10944 #endif
10945
10946 staticpro (&last_mouse_motion_frame);
10947 last_mouse_motion_frame = Qnil;
10948
10949 Qmodifier_value = intern ("modifier-value");
10950 Qalt = intern ("alt");
10951 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
10952 Qhyper = intern ("hyper");
10953 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
10954 Qmeta = intern ("meta");
10955 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
10956 Qsuper = intern ("super");
10957 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
10958
10959 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
10960 doc: /* Which keys Emacs uses for the alt modifier.
10961 This should be one of the symbols `alt', `hyper', `meta', `super'.
10962 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
10963 is nil, which is the same as `alt'. */);
10964 Vx_alt_keysym = Qnil;
10965
10966 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
10967 doc: /* Which keys Emacs uses for the hyper modifier.
10968 This should be one of the symbols `alt', `hyper', `meta', `super'.
10969 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
10970 default is nil, which is the same as `hyper'. */);
10971 Vx_hyper_keysym = Qnil;
10972
10973 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
10974 doc: /* Which keys Emacs uses for the meta modifier.
10975 This should be one of the symbols `alt', `hyper', `meta', `super'.
10976 For example, `meta' means use the Meta_L and Meta_R keysyms. The
10977 default is nil, which is the same as `meta'. */);
10978 Vx_meta_keysym = Qnil;
10979
10980 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
10981 doc: /* Which keys Emacs uses for the super modifier.
10982 This should be one of the symbols `alt', `hyper', `meta', `super'.
10983 For example, `super' means use the Super_L and Super_R keysyms. The
10984 default is nil, which is the same as `super'. */);
10985 Vx_super_keysym = Qnil;
10986
10987 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
10988 doc: /* Hash table of character codes indexed by X keysym codes. */);
10989 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
10990 make_float (DEFAULT_REHASH_SIZE),
10991 make_float (DEFAULT_REHASH_THRESHOLD),
10992 Qnil, Qnil, Qnil);
10993 }
10994
10995 #endif /* HAVE_X_WINDOWS */