* xterm.c: Remove consolidated defines and code.
[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, on, x, y, new_cursor_type, new_cursor_width)
7504 struct window *w;
7505 struct glyph_row *glyph_row;
7506 int on, x, y;
7507 int new_cursor_type, new_cursor_width;
7508 {
7509 struct frame *f = XFRAME (WINDOW_FRAME (w));
7510
7511 if (on)
7512 {
7513 w->phys_cursor_type = new_cursor_type;
7514 w->phys_cursor_on_p = 1;
7515
7516 switch (new_cursor_type)
7517 {
7518 case HOLLOW_BOX_CURSOR:
7519 x_draw_hollow_cursor (w, glyph_row);
7520 break;
7521
7522 case FILLED_BOX_CURSOR:
7523 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7524 break;
7525
7526 case BAR_CURSOR:
7527 x_draw_bar_cursor (w, glyph_row, new_cursor_width, BAR_CURSOR);
7528 break;
7529
7530 case HBAR_CURSOR:
7531 x_draw_bar_cursor (w, glyph_row, new_cursor_width, HBAR_CURSOR);
7532 break;
7533
7534 case NO_CURSOR:
7535 w->phys_cursor_width = 0;
7536 break;
7537
7538 default:
7539 abort ();
7540 }
7541
7542 #ifdef HAVE_X_I18N
7543 if (w == XWINDOW (f->selected_window))
7544 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7545 xic_set_preeditarea (w, x, y);
7546 #endif
7547 }
7548
7549 #ifndef XFlush
7550 if (updating_frame != f)
7551 XFlush (FRAME_X_DISPLAY (f));
7552 #endif
7553 }
7554
7555 \f
7556 /* Icons. */
7557
7558 /* Make the x-window of frame F use the gnu icon bitmap. */
7559
7560 int
7561 x_bitmap_icon (f, file)
7562 struct frame *f;
7563 Lisp_Object file;
7564 {
7565 int bitmap_id;
7566
7567 if (FRAME_X_WINDOW (f) == 0)
7568 return 1;
7569
7570 /* Free up our existing icon bitmap if any. */
7571 if (f->output_data.x->icon_bitmap > 0)
7572 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7573 f->output_data.x->icon_bitmap = 0;
7574
7575 if (STRINGP (file))
7576 bitmap_id = x_create_bitmap_from_file (f, file);
7577 else
7578 {
7579 /* Create the GNU bitmap if necessary. */
7580 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7581 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
7582 = x_create_bitmap_from_data (f, gnu_bits,
7583 gnu_width, gnu_height);
7584
7585 /* The first time we create the GNU bitmap,
7586 this increments the ref-count one extra time.
7587 As a result, the GNU bitmap is never freed.
7588 That way, we don't have to worry about allocating it again. */
7589 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7590
7591 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7592 }
7593
7594 x_wm_set_icon_pixmap (f, bitmap_id);
7595 f->output_data.x->icon_bitmap = bitmap_id;
7596
7597 return 0;
7598 }
7599
7600
7601 /* Make the x-window of frame F use a rectangle with text.
7602 Use ICON_NAME as the text. */
7603
7604 int
7605 x_text_icon (f, icon_name)
7606 struct frame *f;
7607 char *icon_name;
7608 {
7609 if (FRAME_X_WINDOW (f) == 0)
7610 return 1;
7611
7612 #ifdef HAVE_X11R4
7613 {
7614 XTextProperty text;
7615 text.value = (unsigned char *) icon_name;
7616 text.encoding = XA_STRING;
7617 text.format = 8;
7618 text.nitems = strlen (icon_name);
7619 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7620 }
7621 #else /* not HAVE_X11R4 */
7622 XSetIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), icon_name);
7623 #endif /* not HAVE_X11R4 */
7624
7625 if (f->output_data.x->icon_bitmap > 0)
7626 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7627 f->output_data.x->icon_bitmap = 0;
7628 x_wm_set_icon_pixmap (f, 0);
7629
7630 return 0;
7631 }
7632 \f
7633 #define X_ERROR_MESSAGE_SIZE 200
7634
7635 /* If non-nil, this should be a string.
7636 It means catch X errors and store the error message in this string. */
7637
7638 static Lisp_Object x_error_message_string;
7639
7640 /* An X error handler which stores the error message in
7641 x_error_message_string. This is called from x_error_handler if
7642 x_catch_errors is in effect. */
7643
7644 static void
7645 x_error_catcher (display, error)
7646 Display *display;
7647 XErrorEvent *error;
7648 {
7649 XGetErrorText (display, error->error_code,
7650 SDATA (x_error_message_string),
7651 X_ERROR_MESSAGE_SIZE);
7652 }
7653
7654 /* Begin trapping X errors for display DPY. Actually we trap X errors
7655 for all displays, but DPY should be the display you are actually
7656 operating on.
7657
7658 After calling this function, X protocol errors no longer cause
7659 Emacs to exit; instead, they are recorded in the string
7660 stored in x_error_message_string.
7661
7662 Calling x_check_errors signals an Emacs error if an X error has
7663 occurred since the last call to x_catch_errors or x_check_errors.
7664
7665 Calling x_uncatch_errors resumes the normal error handling. */
7666
7667 void x_check_errors ();
7668 static Lisp_Object x_catch_errors_unwind ();
7669
7670 int
7671 x_catch_errors (dpy)
7672 Display *dpy;
7673 {
7674 int count = SPECPDL_INDEX ();
7675
7676 /* Make sure any errors from previous requests have been dealt with. */
7677 XSync (dpy, False);
7678
7679 record_unwind_protect (x_catch_errors_unwind,
7680 Fcons (make_save_value (dpy, 0),
7681 x_error_message_string));
7682
7683 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
7684 SSET (x_error_message_string, 0, 0);
7685
7686 return count;
7687 }
7688
7689 /* Unbind the binding that we made to check for X errors. */
7690
7691 static Lisp_Object
7692 x_catch_errors_unwind (old_val)
7693 Lisp_Object old_val;
7694 {
7695 Lisp_Object first;
7696
7697 first = XCAR (old_val);
7698
7699 XSync (XSAVE_VALUE (first)->pointer, False);
7700
7701 x_error_message_string = XCDR (old_val);
7702 return Qnil;
7703 }
7704
7705 /* If any X protocol errors have arrived since the last call to
7706 x_catch_errors or x_check_errors, signal an Emacs error using
7707 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7708
7709 void
7710 x_check_errors (dpy, format)
7711 Display *dpy;
7712 char *format;
7713 {
7714 /* Make sure to catch any errors incurred so far. */
7715 XSync (dpy, False);
7716
7717 if (SREF (x_error_message_string, 0))
7718 error (format, SDATA (x_error_message_string));
7719 }
7720
7721 /* Nonzero if we had any X protocol errors
7722 since we did x_catch_errors on DPY. */
7723
7724 int
7725 x_had_errors_p (dpy)
7726 Display *dpy;
7727 {
7728 /* Make sure to catch any errors incurred so far. */
7729 XSync (dpy, False);
7730
7731 return SREF (x_error_message_string, 0) != 0;
7732 }
7733
7734 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7735
7736 void
7737 x_clear_errors (dpy)
7738 Display *dpy;
7739 {
7740 SSET (x_error_message_string, 0, 0);
7741 }
7742
7743 /* Stop catching X protocol errors and let them make Emacs die.
7744 DPY should be the display that was passed to x_catch_errors.
7745 COUNT should be the value that was returned by
7746 the corresponding call to x_catch_errors. */
7747
7748 void
7749 x_uncatch_errors (dpy, count)
7750 Display *dpy;
7751 int count;
7752 {
7753 unbind_to (count, Qnil);
7754 }
7755
7756 #if 0
7757 static unsigned int x_wire_count;
7758 x_trace_wire ()
7759 {
7760 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7761 }
7762 #endif /* ! 0 */
7763
7764 \f
7765 /* Handle SIGPIPE, which can happen when the connection to a server
7766 simply goes away. SIGPIPE is handled by x_connection_signal.
7767 Don't need to do anything, because the write which caused the
7768 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
7769 which will do the appropriate cleanup for us. */
7770
7771 static SIGTYPE
7772 x_connection_signal (signalnum) /* If we don't have an argument, */
7773 int signalnum; /* some compilers complain in signal calls. */
7774 {
7775 #ifdef USG
7776 /* USG systems forget handlers when they are used;
7777 must reestablish each time */
7778 signal (signalnum, x_connection_signal);
7779 #endif /* USG */
7780 }
7781
7782 \f
7783 /************************************************************************
7784 Handling X errors
7785 ************************************************************************/
7786
7787 /* Error message passed to x_connection_closed. */
7788
7789 static char *error_msg;
7790
7791 /* Function installed as fatal_error_signal_hook in
7792 x_connection_closed. Print the X error message, and exit normally,
7793 instead of dumping core when XtCloseDisplay fails. */
7794
7795 static void
7796 x_fatal_error_signal ()
7797 {
7798 fprintf (stderr, "%s\n", error_msg);
7799 exit (70);
7800 }
7801
7802 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7803 the text of an error message that lead to the connection loss. */
7804
7805 static SIGTYPE
7806 x_connection_closed (dpy, error_message)
7807 Display *dpy;
7808 char *error_message;
7809 {
7810 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7811 Lisp_Object frame, tail;
7812 int count;
7813
7814 error_msg = (char *) alloca (strlen (error_message) + 1);
7815 strcpy (error_msg, error_message);
7816 handling_signal = 0;
7817
7818 /* Prevent being called recursively because of an error condition
7819 below. Otherwise, we might end up with printing ``can't find per
7820 display information'' in the recursive call instead of printing
7821 the original message here. */
7822 count = x_catch_errors (dpy);
7823
7824 /* We have to close the display to inform Xt that it doesn't
7825 exist anymore. If we don't, Xt will continue to wait for
7826 events from the display. As a consequence, a sequence of
7827
7828 M-x make-frame-on-display RET :1 RET
7829 ...kill the new frame, so that we get an IO error...
7830 M-x make-frame-on-display RET :1 RET
7831
7832 will indefinitely wait in Xt for events for display `:1', opened
7833 in the first class to make-frame-on-display.
7834
7835 Closing the display is reported to lead to a bus error on
7836 OpenWindows in certain situations. I suspect that is a bug
7837 in OpenWindows. I don't know how to cicumvent it here. */
7838
7839 #ifdef USE_X_TOOLKIT
7840 /* If DPYINFO is null, this means we didn't open the display
7841 in the first place, so don't try to close it. */
7842 if (dpyinfo)
7843 {
7844 extern void (*fatal_error_signal_hook) P_ ((void));
7845 fatal_error_signal_hook = x_fatal_error_signal;
7846 XtCloseDisplay (dpy);
7847 fatal_error_signal_hook = NULL;
7848 }
7849 #endif
7850
7851 /* Indicate that this display is dead. */
7852 if (dpyinfo)
7853 dpyinfo->display = 0;
7854
7855 /* First delete frames whose mini-buffers are on frames
7856 that are on the dead display. */
7857 FOR_EACH_FRAME (tail, frame)
7858 {
7859 Lisp_Object minibuf_frame;
7860 minibuf_frame
7861 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7862 if (FRAME_X_P (XFRAME (frame))
7863 && FRAME_X_P (XFRAME (minibuf_frame))
7864 && ! EQ (frame, minibuf_frame)
7865 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7866 Fdelete_frame (frame, Qt);
7867 }
7868
7869 /* Now delete all remaining frames on the dead display.
7870 We are now sure none of these is used as the mini-buffer
7871 for another frame that we need to delete. */
7872 FOR_EACH_FRAME (tail, frame)
7873 if (FRAME_X_P (XFRAME (frame))
7874 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7875 {
7876 /* Set this to t so that Fdelete_frame won't get confused
7877 trying to find a replacement. */
7878 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
7879 Fdelete_frame (frame, Qt);
7880 }
7881
7882 if (dpyinfo)
7883 x_delete_display (dpyinfo);
7884
7885 x_uncatch_errors (dpy, count);
7886
7887 if (x_display_list == 0)
7888 {
7889 fprintf (stderr, "%s\n", error_msg);
7890 shut_down_emacs (0, 0, Qnil);
7891 exit (70);
7892 }
7893
7894 /* Ordinary stack unwind doesn't deal with these. */
7895 #ifdef SIGIO
7896 sigunblock (sigmask (SIGIO));
7897 #endif
7898 sigunblock (sigmask (SIGALRM));
7899 TOTALLY_UNBLOCK_INPUT;
7900
7901 clear_waiting_for_input ();
7902 error ("%s", error_msg);
7903 }
7904
7905
7906 /* This is the usual handler for X protocol errors.
7907 It kills all frames on the display that we got the error for.
7908 If that was the only one, it prints an error message and kills Emacs. */
7909
7910 static void
7911 x_error_quitter (display, error)
7912 Display *display;
7913 XErrorEvent *error;
7914 {
7915 char buf[256], buf1[356];
7916
7917 /* Note that there is no real way portable across R3/R4 to get the
7918 original error handler. */
7919
7920 XGetErrorText (display, error->error_code, buf, sizeof (buf));
7921 sprintf (buf1, "X protocol error: %s on protocol request %d",
7922 buf, error->request_code);
7923 x_connection_closed (display, buf1);
7924 }
7925
7926
7927 /* This is the first-level handler for X protocol errors.
7928 It calls x_error_quitter or x_error_catcher. */
7929
7930 static int
7931 x_error_handler (display, error)
7932 Display *display;
7933 XErrorEvent *error;
7934 {
7935 if (! NILP (x_error_message_string))
7936 x_error_catcher (display, error);
7937 else
7938 x_error_quitter (display, error);
7939 return 0;
7940 }
7941
7942 /* This is the handler for X IO errors, always.
7943 It kills all frames on the display that we lost touch with.
7944 If that was the only one, it prints an error message and kills Emacs. */
7945
7946 static int
7947 x_io_error_quitter (display)
7948 Display *display;
7949 {
7950 char buf[256];
7951
7952 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
7953 x_connection_closed (display, buf);
7954 return 0;
7955 }
7956 \f
7957 /* Changing the font of the frame. */
7958
7959 /* Give frame F the font named FONTNAME as its default font, and
7960 return the full name of that font. FONTNAME may be a wildcard
7961 pattern; in that case, we choose some font that fits the pattern.
7962 The return value shows which font we chose. */
7963
7964 Lisp_Object
7965 x_new_font (f, fontname)
7966 struct frame *f;
7967 register char *fontname;
7968 {
7969 struct font_info *fontp
7970 = FS_LOAD_FONT (f, 0, fontname, -1);
7971
7972 if (!fontp)
7973 return Qnil;
7974
7975 f->output_data.x->font = (XFontStruct *) (fontp->font);
7976 f->output_data.x->baseline_offset = fontp->baseline_offset;
7977 f->output_data.x->fontset = -1;
7978
7979 compute_fringe_widths (f, 1);
7980
7981 /* Compute the scroll bar width in character columns. */
7982 if (f->scroll_bar_pixel_width > 0)
7983 {
7984 int wid = FONT_WIDTH (f->output_data.x->font);
7985 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
7986 }
7987 else
7988 {
7989 int wid = FONT_WIDTH (f->output_data.x->font);
7990 f->scroll_bar_cols = (14 + wid - 1) / wid;
7991 }
7992
7993 /* Now make the frame display the given font. */
7994 if (FRAME_X_WINDOW (f) != 0)
7995 {
7996 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
7997 f->output_data.x->font->fid);
7998 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
7999 f->output_data.x->font->fid);
8000 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
8001 f->output_data.x->font->fid);
8002
8003 frame_update_line_height (f);
8004
8005 /* Don't change the size of a tip frame; there's no point in
8006 doing it because it's done in Fx_show_tip, and it leads to
8007 problems because the tip frame has no widget. */
8008 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8009 x_set_window_size (f, 0, f->width, f->height);
8010 }
8011 else
8012 /* If we are setting a new frame's font for the first time,
8013 there are no faces yet, so this font's height is the line height. */
8014 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
8015
8016 return build_string (fontp->full_name);
8017 }
8018
8019 /* Give frame F the fontset named FONTSETNAME as its default font, and
8020 return the full name of that fontset. FONTSETNAME may be a wildcard
8021 pattern; in that case, we choose some fontset that fits the pattern.
8022 The return value shows which fontset we chose. */
8023
8024 Lisp_Object
8025 x_new_fontset (f, fontsetname)
8026 struct frame *f;
8027 char *fontsetname;
8028 {
8029 int fontset = fs_query_fontset (build_string (fontsetname), 0);
8030 Lisp_Object result;
8031
8032 if (fontset < 0)
8033 return Qnil;
8034
8035 if (f->output_data.x->fontset == fontset)
8036 /* This fontset is already set in frame F. There's nothing more
8037 to do. */
8038 return fontset_name (fontset);
8039
8040 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
8041
8042 if (!STRINGP (result))
8043 /* Can't load ASCII font. */
8044 return Qnil;
8045
8046 /* Since x_new_font doesn't update any fontset information, do it now. */
8047 f->output_data.x->fontset = fontset;
8048
8049 #ifdef HAVE_X_I18N
8050 if (FRAME_XIC (f)
8051 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
8052 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
8053 #endif
8054
8055 return build_string (fontsetname);
8056 }
8057
8058 \f
8059 /***********************************************************************
8060 X Input Methods
8061 ***********************************************************************/
8062
8063 #ifdef HAVE_X_I18N
8064
8065 #ifdef HAVE_X11R6
8066
8067 /* XIM destroy callback function, which is called whenever the
8068 connection to input method XIM dies. CLIENT_DATA contains a
8069 pointer to the x_display_info structure corresponding to XIM. */
8070
8071 static void
8072 xim_destroy_callback (xim, client_data, call_data)
8073 XIM xim;
8074 XPointer client_data;
8075 XPointer call_data;
8076 {
8077 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8078 Lisp_Object frame, tail;
8079
8080 BLOCK_INPUT;
8081
8082 /* No need to call XDestroyIC.. */
8083 FOR_EACH_FRAME (tail, frame)
8084 {
8085 struct frame *f = XFRAME (frame);
8086 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
8087 {
8088 FRAME_XIC (f) = NULL;
8089 if (FRAME_XIC_FONTSET (f))
8090 {
8091 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
8092 FRAME_XIC_FONTSET (f) = NULL;
8093 }
8094 }
8095 }
8096
8097 /* No need to call XCloseIM. */
8098 dpyinfo->xim = NULL;
8099 XFree (dpyinfo->xim_styles);
8100 UNBLOCK_INPUT;
8101 }
8102
8103 #endif /* HAVE_X11R6 */
8104
8105 #ifdef HAVE_X11R6
8106 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8107 extern char *XSetIMValues P_ ((XIM, ...));
8108 #endif
8109
8110 /* Open the connection to the XIM server on display DPYINFO.
8111 RESOURCE_NAME is the resource name Emacs uses. */
8112
8113 static void
8114 xim_open_dpy (dpyinfo, resource_name)
8115 struct x_display_info *dpyinfo;
8116 char *resource_name;
8117 {
8118 #ifdef USE_XIM
8119 XIM xim;
8120
8121 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
8122 dpyinfo->xim = xim;
8123
8124 if (xim)
8125 {
8126 #ifdef HAVE_X11R6
8127 XIMCallback destroy;
8128 #endif
8129
8130 /* Get supported styles and XIM values. */
8131 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8132
8133 #ifdef HAVE_X11R6
8134 destroy.callback = xim_destroy_callback;
8135 destroy.client_data = (XPointer)dpyinfo;
8136 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8137 #endif
8138 }
8139
8140 #else /* not USE_XIM */
8141 dpyinfo->xim = NULL;
8142 #endif /* not USE_XIM */
8143 }
8144
8145
8146 #ifdef HAVE_X11R6_XIM
8147
8148 struct xim_inst_t
8149 {
8150 struct x_display_info *dpyinfo;
8151 char *resource_name;
8152 };
8153
8154 /* XIM instantiate callback function, which is called whenever an XIM
8155 server is available. DISPLAY is the display of the XIM.
8156 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8157 when the callback was registered. */
8158
8159 static void
8160 xim_instantiate_callback (display, client_data, call_data)
8161 Display *display;
8162 XPointer client_data;
8163 XPointer call_data;
8164 {
8165 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8166 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8167
8168 /* We don't support multiple XIM connections. */
8169 if (dpyinfo->xim)
8170 return;
8171
8172 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8173
8174 /* Create XIC for the existing frames on the same display, as long
8175 as they have no XIC. */
8176 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8177 {
8178 Lisp_Object tail, frame;
8179
8180 BLOCK_INPUT;
8181 FOR_EACH_FRAME (tail, frame)
8182 {
8183 struct frame *f = XFRAME (frame);
8184
8185 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8186 if (FRAME_XIC (f) == NULL)
8187 {
8188 create_frame_xic (f);
8189 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8190 xic_set_statusarea (f);
8191 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8192 {
8193 struct window *w = XWINDOW (f->selected_window);
8194 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8195 }
8196 }
8197 }
8198
8199 UNBLOCK_INPUT;
8200 }
8201 }
8202
8203 #endif /* HAVE_X11R6_XIM */
8204
8205
8206 /* Open a connection to the XIM server on display DPYINFO.
8207 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8208 connection only at the first time. On X11R6, open the connection
8209 in the XIM instantiate callback function. */
8210
8211 static void
8212 xim_initialize (dpyinfo, resource_name)
8213 struct x_display_info *dpyinfo;
8214 char *resource_name;
8215 {
8216 #ifdef USE_XIM
8217 #ifdef HAVE_X11R6_XIM
8218 struct xim_inst_t *xim_inst;
8219 int len;
8220
8221 dpyinfo->xim = NULL;
8222 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8223 xim_inst->dpyinfo = dpyinfo;
8224 len = strlen (resource_name);
8225 xim_inst->resource_name = (char *) xmalloc (len + 1);
8226 bcopy (resource_name, xim_inst->resource_name, len + 1);
8227 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8228 resource_name, EMACS_CLASS,
8229 xim_instantiate_callback,
8230 /* Fixme: This is XPointer in
8231 XFree86 but (XPointer *) on
8232 Tru64, at least. */
8233 (XPointer) xim_inst);
8234 #else /* not HAVE_X11R6_XIM */
8235 dpyinfo->xim = NULL;
8236 xim_open_dpy (dpyinfo, resource_name);
8237 #endif /* not HAVE_X11R6_XIM */
8238
8239 #else /* not USE_XIM */
8240 dpyinfo->xim = NULL;
8241 #endif /* not USE_XIM */
8242 }
8243
8244
8245 /* Close the connection to the XIM server on display DPYINFO. */
8246
8247 static void
8248 xim_close_dpy (dpyinfo)
8249 struct x_display_info *dpyinfo;
8250 {
8251 #ifdef USE_XIM
8252 #ifdef HAVE_X11R6_XIM
8253 if (dpyinfo->display)
8254 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8255 NULL, EMACS_CLASS,
8256 xim_instantiate_callback, NULL);
8257 #endif /* not HAVE_X11R6_XIM */
8258 if (dpyinfo->display)
8259 XCloseIM (dpyinfo->xim);
8260 dpyinfo->xim = NULL;
8261 XFree (dpyinfo->xim_styles);
8262 #endif /* USE_XIM */
8263 }
8264
8265 #endif /* not HAVE_X11R6_XIM */
8266
8267
8268 \f
8269 /* Calculate the absolute position in frame F
8270 from its current recorded position values and gravity. */
8271
8272 void
8273 x_calc_absolute_position (f)
8274 struct frame *f;
8275 {
8276 Window child;
8277 int win_x = 0, win_y = 0;
8278 int flags = f->output_data.x->size_hint_flags;
8279 int this_window;
8280
8281 /* We have nothing to do if the current position
8282 is already for the top-left corner. */
8283 if (! ((flags & XNegative) || (flags & YNegative)))
8284 return;
8285
8286 this_window = FRAME_OUTER_WINDOW (f);
8287
8288 /* Find the position of the outside upper-left corner of
8289 the inner window, with respect to the outer window.
8290 But do this only if we will need the results. */
8291 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
8292 {
8293 int count;
8294
8295 BLOCK_INPUT;
8296 count = x_catch_errors (FRAME_X_DISPLAY (f));
8297 while (1)
8298 {
8299 x_clear_errors (FRAME_X_DISPLAY (f));
8300 XTranslateCoordinates (FRAME_X_DISPLAY (f),
8301
8302 /* From-window, to-window. */
8303 this_window,
8304 f->output_data.x->parent_desc,
8305
8306 /* From-position, to-position. */
8307 0, 0, &win_x, &win_y,
8308
8309 /* Child of win. */
8310 &child);
8311 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
8312 {
8313 Window newroot, newparent = 0xdeadbeef;
8314 Window *newchildren;
8315 unsigned int nchildren;
8316
8317 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
8318 &newparent, &newchildren, &nchildren))
8319 break;
8320
8321 XFree ((char *) newchildren);
8322
8323 f->output_data.x->parent_desc = newparent;
8324 }
8325 else
8326 break;
8327 }
8328
8329 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
8330 UNBLOCK_INPUT;
8331 }
8332
8333 /* Treat negative positions as relative to the leftmost bottommost
8334 position that fits on the screen. */
8335 if (flags & XNegative)
8336 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
8337 - 2 * f->output_data.x->border_width - win_x
8338 - PIXEL_WIDTH (f)
8339 + f->output_data.x->left_pos);
8340
8341 {
8342 int height = PIXEL_HEIGHT (f);
8343
8344 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8345 /* Something is fishy here. When using Motif, starting Emacs with
8346 `-g -0-0', the frame appears too low by a few pixels.
8347
8348 This seems to be so because initially, while Emacs is starting,
8349 the column widget's height and the frame's pixel height are
8350 different. The column widget's height is the right one. In
8351 later invocations, when Emacs is up, the frame's pixel height
8352 is right, though.
8353
8354 It's not obvious where the initial small difference comes from.
8355 2000-12-01, gerd. */
8356
8357 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8358 #endif
8359
8360 if (flags & YNegative)
8361 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
8362 - 2 * f->output_data.x->border_width
8363 - win_y
8364 - height
8365 + f->output_data.x->top_pos);
8366 }
8367
8368 /* The left_pos and top_pos
8369 are now relative to the top and left screen edges,
8370 so the flags should correspond. */
8371 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
8372 }
8373
8374 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8375 to really change the position, and 0 when calling from
8376 x_make_frame_visible (in that case, XOFF and YOFF are the current
8377 position values). It is -1 when calling from x_set_frame_parameters,
8378 which means, do adjust for borders but don't change the gravity. */
8379
8380 void
8381 x_set_offset (f, xoff, yoff, change_gravity)
8382 struct frame *f;
8383 register int xoff, yoff;
8384 int change_gravity;
8385 {
8386 int modified_top, modified_left;
8387
8388 if (change_gravity > 0)
8389 {
8390 f->output_data.x->top_pos = yoff;
8391 f->output_data.x->left_pos = xoff;
8392 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
8393 if (xoff < 0)
8394 f->output_data.x->size_hint_flags |= XNegative;
8395 if (yoff < 0)
8396 f->output_data.x->size_hint_flags |= YNegative;
8397 f->output_data.x->win_gravity = NorthWestGravity;
8398 }
8399 x_calc_absolute_position (f);
8400
8401 BLOCK_INPUT;
8402 x_wm_set_size_hint (f, (long) 0, 0);
8403
8404 modified_left = f->output_data.x->left_pos;
8405 modified_top = f->output_data.x->top_pos;
8406 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
8407 this seems to be unnecessary and incorrect. rms, 4/17/97. */
8408 /* It is a mystery why we need to add the border_width here
8409 when the frame is already visible, but experiment says we do. */
8410 if (change_gravity != 0)
8411 {
8412 modified_left += f->output_data.x->border_width;
8413 modified_top += f->output_data.x->border_width;
8414 }
8415 #endif
8416
8417 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8418 modified_left, modified_top);
8419 UNBLOCK_INPUT;
8420 }
8421
8422 /* Check if we need to resize the frame due to a fullscreen request.
8423 If so needed, resize the frame. */
8424 static void
8425 x_check_fullscreen (f)
8426 struct frame *f;
8427 {
8428 if (f->output_data.x->want_fullscreen & FULLSCREEN_BOTH)
8429 {
8430 int width, height, ign;
8431
8432 x_real_positions (f, &f->output_data.x->left_pos,
8433 &f->output_data.x->top_pos);
8434
8435 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
8436
8437 /* We do not need to move the window, it shall be taken care of
8438 when setting WM manager hints.
8439 If the frame is visible already, the position is checked by
8440 x_check_fullscreen_move. */
8441 if (f->width != width || f->height != height)
8442 {
8443 change_frame_size (f, height, width, 0, 1, 0);
8444 SET_FRAME_GARBAGED (f);
8445 cancel_mouse_face (f);
8446
8447 /* Wait for the change of frame size to occur */
8448 f->output_data.x->want_fullscreen |= FULLSCREEN_WAIT;
8449 }
8450 }
8451 }
8452
8453 /* If frame parameters are set after the frame is mapped, we need to move
8454 the window. This is done in xfns.c.
8455 Some window managers moves the window to the right position, some
8456 moves the outer window manager window to the specified position.
8457 Here we check that we are in the right spot. If not, make a second
8458 move, assuming we are dealing with the second kind of window manager. */
8459 static void
8460 x_check_fullscreen_move (f)
8461 struct frame *f;
8462 {
8463 if (f->output_data.x->want_fullscreen & FULLSCREEN_MOVE_WAIT)
8464 {
8465 int expect_top = f->output_data.x->top_pos;
8466 int expect_left = f->output_data.x->left_pos;
8467
8468 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
8469 expect_top = 0;
8470 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
8471 expect_left = 0;
8472
8473 if (expect_top != f->output_data.x->top_pos
8474 || expect_left != f->output_data.x->left_pos)
8475 x_set_offset (f, expect_left, expect_top, 1);
8476
8477 /* Just do this once */
8478 f->output_data.x->want_fullscreen &= ~FULLSCREEN_MOVE_WAIT;
8479 }
8480 }
8481
8482
8483 /* Calculate fullscreen size. Return in *TOP_POS and *LEFT_POS the
8484 wanted positions of the WM window (not emacs window).
8485 Return in *WIDTH and *HEIGHT the wanted width and height of Emacs
8486 window (FRAME_X_WINDOW).
8487 */
8488 void
8489 x_fullscreen_adjust (f, width, height, top_pos, left_pos)
8490 struct frame *f;
8491 int *width;
8492 int *height;
8493 int *top_pos;
8494 int *left_pos;
8495 {
8496 int newwidth = f->width, newheight = f->height;
8497
8498 *top_pos = f->output_data.x->top_pos;
8499 *left_pos = f->output_data.x->left_pos;
8500
8501 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
8502 {
8503 int ph;
8504
8505 ph = FRAME_X_DISPLAY_INFO (f)->height;
8506 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
8507 ph = CHAR_TO_PIXEL_HEIGHT (f, newheight)
8508 - f->output_data.x->y_pixels_diff;
8509 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
8510 *top_pos = 0;
8511 }
8512
8513 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
8514 {
8515 int pw;
8516
8517 pw = FRAME_X_DISPLAY_INFO (f)->width;
8518 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
8519 pw = CHAR_TO_PIXEL_WIDTH (f, newwidth)
8520 - f->output_data.x->x_pixels_diff;
8521 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
8522 *left_pos = 0;
8523 }
8524
8525 *width = newwidth;
8526 *height = newheight;
8527 }
8528
8529
8530 /* Change the size of frame F's X window to COLS/ROWS in the case F
8531 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8532 top-left-corner window gravity for this size change and subsequent
8533 size changes. Otherwise we leave the window gravity unchanged. */
8534
8535 static void
8536 x_set_window_size_1 (f, change_gravity, cols, rows)
8537 struct frame *f;
8538 int change_gravity;
8539 int cols, rows;
8540 {
8541 int pixelwidth, pixelheight;
8542
8543 check_frame_size (f, &rows, &cols);
8544 f->output_data.x->vertical_scroll_bar_extra
8545 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8546 ? 0
8547 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
8548 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
8549 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
8550
8551 compute_fringe_widths (f, 0);
8552
8553 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
8554 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
8555
8556 f->output_data.x->win_gravity = NorthWestGravity;
8557 x_wm_set_size_hint (f, (long) 0, 0);
8558
8559 XSync (FRAME_X_DISPLAY (f), False);
8560 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8561 pixelwidth, pixelheight);
8562
8563 /* Now, strictly speaking, we can't be sure that this is accurate,
8564 but the window manager will get around to dealing with the size
8565 change request eventually, and we'll hear how it went when the
8566 ConfigureNotify event gets here.
8567
8568 We could just not bother storing any of this information here,
8569 and let the ConfigureNotify event set everything up, but that
8570 might be kind of confusing to the Lisp code, since size changes
8571 wouldn't be reported in the frame parameters until some random
8572 point in the future when the ConfigureNotify event arrives.
8573
8574 We pass 1 for DELAY since we can't run Lisp code inside of
8575 a BLOCK_INPUT. */
8576 change_frame_size (f, rows, cols, 0, 1, 0);
8577 PIXEL_WIDTH (f) = pixelwidth;
8578 PIXEL_HEIGHT (f) = pixelheight;
8579
8580 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8581 receive in the ConfigureNotify event; if we get what we asked
8582 for, then the event won't cause the screen to become garbaged, so
8583 we have to make sure to do it here. */
8584 SET_FRAME_GARBAGED (f);
8585
8586 XFlush (FRAME_X_DISPLAY (f));
8587 }
8588
8589
8590 /* Call this to change the size of frame F's x-window.
8591 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8592 for this size change and subsequent size changes.
8593 Otherwise we leave the window gravity unchanged. */
8594
8595 void
8596 x_set_window_size (f, change_gravity, cols, rows)
8597 struct frame *f;
8598 int change_gravity;
8599 int cols, rows;
8600 {
8601 BLOCK_INPUT;
8602
8603 #ifdef USE_GTK
8604 if (FRAME_GTK_WIDGET (f))
8605 xg_frame_set_char_size (f, cols, rows);
8606 else
8607 x_set_window_size_1 (f, change_gravity, cols, rows);
8608 #elif USE_X_TOOLKIT
8609
8610 if (f->output_data.x->widget != NULL)
8611 {
8612 /* The x and y position of the widget is clobbered by the
8613 call to XtSetValues within EmacsFrameSetCharSize.
8614 This is a real kludge, but I don't understand Xt so I can't
8615 figure out a correct fix. Can anyone else tell me? -- rms. */
8616 int xpos = f->output_data.x->widget->core.x;
8617 int ypos = f->output_data.x->widget->core.y;
8618 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
8619 f->output_data.x->widget->core.x = xpos;
8620 f->output_data.x->widget->core.y = ypos;
8621 }
8622 else
8623 x_set_window_size_1 (f, change_gravity, cols, rows);
8624
8625 #else /* not USE_X_TOOLKIT */
8626
8627 x_set_window_size_1 (f, change_gravity, cols, rows);
8628
8629 #endif /* not USE_X_TOOLKIT */
8630
8631 /* If cursor was outside the new size, mark it as off. */
8632 mark_window_cursors_off (XWINDOW (f->root_window));
8633
8634 /* Clear out any recollection of where the mouse highlighting was,
8635 since it might be in a place that's outside the new frame size.
8636 Actually checking whether it is outside is a pain in the neck,
8637 so don't try--just let the highlighting be done afresh with new size. */
8638 cancel_mouse_face (f);
8639
8640 UNBLOCK_INPUT;
8641 }
8642 \f
8643 /* Mouse warping. */
8644
8645 void
8646 x_set_mouse_position (f, x, y)
8647 struct frame *f;
8648 int x, y;
8649 {
8650 int pix_x, pix_y;
8651
8652 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
8653 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
8654
8655 if (pix_x < 0) pix_x = 0;
8656 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
8657
8658 if (pix_y < 0) pix_y = 0;
8659 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
8660
8661 BLOCK_INPUT;
8662
8663 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8664 0, 0, 0, 0, pix_x, pix_y);
8665 UNBLOCK_INPUT;
8666 }
8667
8668 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
8669
8670 void
8671 x_set_mouse_pixel_position (f, pix_x, pix_y)
8672 struct frame *f;
8673 int pix_x, pix_y;
8674 {
8675 BLOCK_INPUT;
8676
8677 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8678 0, 0, 0, 0, pix_x, pix_y);
8679 UNBLOCK_INPUT;
8680 }
8681 \f
8682 /* focus shifting, raising and lowering. */
8683
8684 void
8685 x_focus_on_frame (f)
8686 struct frame *f;
8687 {
8688 #if 0 /* This proves to be unpleasant. */
8689 x_raise_frame (f);
8690 #endif
8691 #if 0
8692 /* I don't think that the ICCCM allows programs to do things like this
8693 without the interaction of the window manager. Whatever you end up
8694 doing with this code, do it to x_unfocus_frame too. */
8695 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8696 RevertToPointerRoot, CurrentTime);
8697 #endif /* ! 0 */
8698 }
8699
8700 void
8701 x_unfocus_frame (f)
8702 struct frame *f;
8703 {
8704 #if 0
8705 /* Look at the remarks in x_focus_on_frame. */
8706 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
8707 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
8708 RevertToPointerRoot, CurrentTime);
8709 #endif /* ! 0 */
8710 }
8711
8712 /* Raise frame F. */
8713
8714 void
8715 x_raise_frame (f)
8716 struct frame *f;
8717 {
8718 if (f->async_visible)
8719 {
8720 BLOCK_INPUT;
8721 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8722 XFlush (FRAME_X_DISPLAY (f));
8723 UNBLOCK_INPUT;
8724 }
8725 }
8726
8727 /* Lower frame F. */
8728
8729 void
8730 x_lower_frame (f)
8731 struct frame *f;
8732 {
8733 if (f->async_visible)
8734 {
8735 BLOCK_INPUT;
8736 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8737 XFlush (FRAME_X_DISPLAY (f));
8738 UNBLOCK_INPUT;
8739 }
8740 }
8741
8742 static void
8743 XTframe_raise_lower (f, raise_flag)
8744 FRAME_PTR f;
8745 int raise_flag;
8746 {
8747 if (raise_flag)
8748 x_raise_frame (f);
8749 else
8750 x_lower_frame (f);
8751 }
8752 \f
8753 /* Change of visibility. */
8754
8755 /* This tries to wait until the frame is really visible.
8756 However, if the window manager asks the user where to position
8757 the frame, this will return before the user finishes doing that.
8758 The frame will not actually be visible at that time,
8759 but it will become visible later when the window manager
8760 finishes with it. */
8761
8762 void
8763 x_make_frame_visible (f)
8764 struct frame *f;
8765 {
8766 Lisp_Object type;
8767 int original_top, original_left;
8768 int retry_count = 2;
8769
8770 retry:
8771
8772 BLOCK_INPUT;
8773
8774 type = x_icon_type (f);
8775 if (!NILP (type))
8776 x_bitmap_icon (f, type);
8777
8778 if (! FRAME_VISIBLE_P (f))
8779 {
8780 /* We test FRAME_GARBAGED_P here to make sure we don't
8781 call x_set_offset a second time
8782 if we get to x_make_frame_visible a second time
8783 before the window gets really visible. */
8784 if (! FRAME_ICONIFIED_P (f)
8785 && ! f->output_data.x->asked_for_visible)
8786 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
8787
8788 f->output_data.x->asked_for_visible = 1;
8789
8790 if (! EQ (Vx_no_window_manager, Qt))
8791 x_wm_set_window_state (f, NormalState);
8792 #ifdef USE_X_TOOLKIT
8793 /* This was XtPopup, but that did nothing for an iconified frame. */
8794 XtMapWidget (f->output_data.x->widget);
8795 #else /* not USE_X_TOOLKIT */
8796 #ifdef USE_GTK
8797 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
8798 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
8799 #else
8800 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
8801 #endif /* not USE_GTK */
8802 #endif /* not USE_X_TOOLKIT */
8803 #if 0 /* This seems to bring back scroll bars in the wrong places
8804 if the window configuration has changed. They seem
8805 to come back ok without this. */
8806 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8807 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
8808 #endif
8809 }
8810
8811 XFlush (FRAME_X_DISPLAY (f));
8812
8813 /* Synchronize to ensure Emacs knows the frame is visible
8814 before we do anything else. We do this loop with input not blocked
8815 so that incoming events are handled. */
8816 {
8817 Lisp_Object frame;
8818 int count;
8819 /* This must be before UNBLOCK_INPUT
8820 since events that arrive in response to the actions above
8821 will set it when they are handled. */
8822 int previously_visible = f->output_data.x->has_been_visible;
8823
8824 original_left = f->output_data.x->left_pos;
8825 original_top = f->output_data.x->top_pos;
8826
8827 /* This must come after we set COUNT. */
8828 UNBLOCK_INPUT;
8829
8830 /* We unblock here so that arriving X events are processed. */
8831
8832 /* Now move the window back to where it was "supposed to be".
8833 But don't do it if the gravity is negative.
8834 When the gravity is negative, this uses a position
8835 that is 3 pixels too low. Perhaps that's really the border width.
8836
8837 Don't do this if the window has never been visible before,
8838 because the window manager may choose the position
8839 and we don't want to override it. */
8840
8841 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
8842 && f->output_data.x->win_gravity == NorthWestGravity
8843 && previously_visible)
8844 {
8845 Drawable rootw;
8846 int x, y;
8847 unsigned int width, height, border, depth;
8848
8849 BLOCK_INPUT;
8850
8851 /* On some window managers (such as FVWM) moving an existing
8852 window, even to the same place, causes the window manager
8853 to introduce an offset. This can cause the window to move
8854 to an unexpected location. Check the geometry (a little
8855 slow here) and then verify that the window is in the right
8856 place. If the window is not in the right place, move it
8857 there, and take the potential window manager hit. */
8858 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8859 &rootw, &x, &y, &width, &height, &border, &depth);
8860
8861 if (original_left != x || original_top != y)
8862 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8863 original_left, original_top);
8864
8865 UNBLOCK_INPUT;
8866 }
8867
8868 XSETFRAME (frame, f);
8869
8870 /* Wait until the frame is visible. Process X events until a
8871 MapNotify event has been seen, or until we think we won't get a
8872 MapNotify at all.. */
8873 for (count = input_signal_count + 10;
8874 input_signal_count < count && !FRAME_VISIBLE_P (f);)
8875 {
8876 /* Force processing of queued events. */
8877 x_sync (f);
8878
8879 /* Machines that do polling rather than SIGIO have been
8880 observed to go into a busy-wait here. So we'll fake an
8881 alarm signal to let the handler know that there's something
8882 to be read. We used to raise a real alarm, but it seems
8883 that the handler isn't always enabled here. This is
8884 probably a bug. */
8885 if (input_polling_used ())
8886 {
8887 /* It could be confusing if a real alarm arrives while
8888 processing the fake one. Turn it off and let the
8889 handler reset it. */
8890 extern void poll_for_input_1 P_ ((void));
8891 int old_poll_suppress_count = poll_suppress_count;
8892 poll_suppress_count = 1;
8893 poll_for_input_1 ();
8894 poll_suppress_count = old_poll_suppress_count;
8895 }
8896
8897 /* See if a MapNotify event has been processed. */
8898 FRAME_SAMPLE_VISIBILITY (f);
8899 }
8900
8901 /* 2000-09-28: In
8902
8903 (let ((f (selected-frame)))
8904 (iconify-frame f)
8905 (raise-frame f))
8906
8907 the frame is not raised with various window managers on
8908 FreeBSD, Linux and Solaris. It turns out that, for some
8909 unknown reason, the call to XtMapWidget is completely ignored.
8910 Mapping the widget a second time works. */
8911
8912 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
8913 goto retry;
8914 }
8915 }
8916
8917 /* Change from mapped state to withdrawn state. */
8918
8919 /* Make the frame visible (mapped and not iconified). */
8920
8921 void
8922 x_make_frame_invisible (f)
8923 struct frame *f;
8924 {
8925 Window window;
8926
8927 /* Use the frame's outermost window, not the one we normally draw on. */
8928 window = FRAME_OUTER_WINDOW (f);
8929
8930 /* Don't keep the highlight on an invisible frame. */
8931 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
8932 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
8933
8934 #if 0/* This might add unreliability; I don't trust it -- rms. */
8935 if (! f->async_visible && ! f->async_iconified)
8936 return;
8937 #endif
8938
8939 BLOCK_INPUT;
8940
8941 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
8942 that the current position of the window is user-specified, rather than
8943 program-specified, so that when the window is mapped again, it will be
8944 placed at the same location, without forcing the user to position it
8945 by hand again (they have already done that once for this window.) */
8946 x_wm_set_size_hint (f, (long) 0, 1);
8947
8948 #ifdef USE_GTK
8949 if (FRAME_GTK_OUTER_WIDGET (f))
8950 {
8951 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
8952 goto out;
8953 }
8954 #endif
8955
8956 #ifdef HAVE_X11R4
8957
8958 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
8959 DefaultScreen (FRAME_X_DISPLAY (f))))
8960 {
8961 UNBLOCK_INPUT_RESIGNAL;
8962 error ("Can't notify window manager of window withdrawal");
8963 }
8964 #else /* ! defined (HAVE_X11R4) */
8965
8966 /* Tell the window manager what we're going to do. */
8967 if (! EQ (Vx_no_window_manager, Qt))
8968 {
8969 XEvent unmap;
8970
8971 unmap.xunmap.type = UnmapNotify;
8972 unmap.xunmap.window = window;
8973 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
8974 unmap.xunmap.from_configure = False;
8975 if (! XSendEvent (FRAME_X_DISPLAY (f),
8976 DefaultRootWindow (FRAME_X_DISPLAY (f)),
8977 False,
8978 SubstructureRedirectMaskSubstructureNotifyMask,
8979 &unmap))
8980 {
8981 UNBLOCK_INPUT_RESIGNAL;
8982 error ("Can't notify window manager of withdrawal");
8983 }
8984 }
8985
8986 /* Unmap the window ourselves. Cheeky! */
8987 XUnmapWindow (FRAME_X_DISPLAY (f), window);
8988 #endif /* ! defined (HAVE_X11R4) */
8989
8990 out:
8991 /* We can't distinguish this from iconification
8992 just by the event that we get from the server.
8993 So we can't win using the usual strategy of letting
8994 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
8995 and synchronize with the server to make sure we agree. */
8996 f->visible = 0;
8997 FRAME_ICONIFIED_P (f) = 0;
8998 f->async_visible = 0;
8999 f->async_iconified = 0;
9000
9001 x_sync (f);
9002
9003 UNBLOCK_INPUT;
9004 }
9005
9006 /* Change window state from mapped to iconified. */
9007
9008 void
9009 x_iconify_frame (f)
9010 struct frame *f;
9011 {
9012 int result;
9013 Lisp_Object type;
9014
9015 /* Don't keep the highlight on an invisible frame. */
9016 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9017 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9018
9019 if (f->async_iconified)
9020 return;
9021
9022 BLOCK_INPUT;
9023
9024 FRAME_SAMPLE_VISIBILITY (f);
9025
9026 type = x_icon_type (f);
9027 if (!NILP (type))
9028 x_bitmap_icon (f, type);
9029
9030 #ifdef USE_GTK
9031 if (FRAME_GTK_OUTER_WIDGET (f))
9032 {
9033 if (! FRAME_VISIBLE_P (f))
9034 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9035
9036 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9037 f->iconified = 1;
9038 f->visible = 1;
9039 f->async_iconified = 1;
9040 f->async_visible = 0;
9041 UNBLOCK_INPUT;
9042 return;
9043 }
9044 #endif
9045
9046 #ifdef USE_X_TOOLKIT
9047
9048 if (! FRAME_VISIBLE_P (f))
9049 {
9050 if (! EQ (Vx_no_window_manager, Qt))
9051 x_wm_set_window_state (f, IconicState);
9052 /* This was XtPopup, but that did nothing for an iconified frame. */
9053 XtMapWidget (f->output_data.x->widget);
9054 /* The server won't give us any event to indicate
9055 that an invisible frame was changed to an icon,
9056 so we have to record it here. */
9057 f->iconified = 1;
9058 f->visible = 1;
9059 f->async_iconified = 1;
9060 f->async_visible = 0;
9061 UNBLOCK_INPUT;
9062 return;
9063 }
9064
9065 result = XIconifyWindow (FRAME_X_DISPLAY (f),
9066 XtWindow (f->output_data.x->widget),
9067 DefaultScreen (FRAME_X_DISPLAY (f)));
9068 UNBLOCK_INPUT;
9069
9070 if (!result)
9071 error ("Can't notify window manager of iconification");
9072
9073 f->async_iconified = 1;
9074 f->async_visible = 0;
9075
9076
9077 BLOCK_INPUT;
9078 XFlush (FRAME_X_DISPLAY (f));
9079 UNBLOCK_INPUT;
9080 #else /* not USE_X_TOOLKIT */
9081
9082 /* Make sure the X server knows where the window should be positioned,
9083 in case the user deiconifies with the window manager. */
9084 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
9085 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
9086
9087 /* Since we don't know which revision of X we're running, we'll use both
9088 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
9089
9090 /* X11R4: send a ClientMessage to the window manager using the
9091 WM_CHANGE_STATE type. */
9092 {
9093 XEvent message;
9094
9095 message.xclient.window = FRAME_X_WINDOW (f);
9096 message.xclient.type = ClientMessage;
9097 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
9098 message.xclient.format = 32;
9099 message.xclient.data.l[0] = IconicState;
9100
9101 if (! XSendEvent (FRAME_X_DISPLAY (f),
9102 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9103 False,
9104 SubstructureRedirectMask | SubstructureNotifyMask,
9105 &message))
9106 {
9107 UNBLOCK_INPUT_RESIGNAL;
9108 error ("Can't notify window manager of iconification");
9109 }
9110 }
9111
9112 /* X11R3: set the initial_state field of the window manager hints to
9113 IconicState. */
9114 x_wm_set_window_state (f, IconicState);
9115
9116 if (!FRAME_VISIBLE_P (f))
9117 {
9118 /* If the frame was withdrawn, before, we must map it. */
9119 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9120 }
9121
9122 f->async_iconified = 1;
9123 f->async_visible = 0;
9124
9125 XFlush (FRAME_X_DISPLAY (f));
9126 UNBLOCK_INPUT;
9127 #endif /* not USE_X_TOOLKIT */
9128 }
9129
9130 \f
9131 /* Free X resources of frame F. */
9132
9133 void
9134 x_free_frame_resources (f)
9135 struct frame *f;
9136 {
9137 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9138 Lisp_Object bar;
9139 struct scroll_bar *b;
9140
9141 BLOCK_INPUT;
9142
9143 /* If a display connection is dead, don't try sending more
9144 commands to the X server. */
9145 if (dpyinfo->display)
9146 {
9147 if (f->output_data.x->icon_desc)
9148 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9149
9150 #ifdef USE_X_TOOLKIT
9151 /* Explicitly destroy the scroll bars of the frame. Without
9152 this, we get "BadDrawable" errors from the toolkit later on,
9153 presumably from expose events generated for the disappearing
9154 toolkit scroll bars. */
9155 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9156 {
9157 b = XSCROLL_BAR (bar);
9158 x_scroll_bar_remove (b);
9159 }
9160 #endif
9161
9162 #ifdef HAVE_X_I18N
9163 if (FRAME_XIC (f))
9164 free_frame_xic (f);
9165 #endif
9166
9167 #ifdef USE_X_TOOLKIT
9168 if (f->output_data.x->widget)
9169 {
9170 XtDestroyWidget (f->output_data.x->widget);
9171 f->output_data.x->widget = NULL;
9172 }
9173 /* Tooltips don't have widgets, only a simple X window, even if
9174 we are using a toolkit. */
9175 else if (FRAME_X_WINDOW (f))
9176 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9177
9178 free_frame_menubar (f);
9179 #else /* !USE_X_TOOLKIT */
9180
9181 #ifdef USE_GTK
9182 /* In the GTK version, tooltips are normal X
9183 frames. We must check and free both types. */
9184 if (FRAME_GTK_OUTER_WIDGET (f))
9185 {
9186 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
9187 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
9188 FRAME_GTK_OUTER_WIDGET (f) = 0;
9189 }
9190 #endif /* USE_GTK */
9191
9192 if (FRAME_X_WINDOW (f))
9193 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9194 #endif /* !USE_X_TOOLKIT */
9195
9196 unload_color (f, f->output_data.x->foreground_pixel);
9197 unload_color (f, f->output_data.x->background_pixel);
9198 unload_color (f, f->output_data.x->cursor_pixel);
9199 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9200 unload_color (f, f->output_data.x->border_pixel);
9201 unload_color (f, f->output_data.x->mouse_pixel);
9202
9203 if (f->output_data.x->scroll_bar_background_pixel != -1)
9204 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9205 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9206 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9207 #ifdef USE_TOOLKIT_SCROLL_BARS
9208 /* Scrollbar shadow colors. */
9209 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9210 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9211 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9212 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9213 #endif /* USE_TOOLKIT_SCROLL_BARS */
9214 if (f->output_data.x->white_relief.allocated_p)
9215 unload_color (f, f->output_data.x->white_relief.pixel);
9216 if (f->output_data.x->black_relief.allocated_p)
9217 unload_color (f, f->output_data.x->black_relief.pixel);
9218
9219 if (FRAME_FACE_CACHE (f))
9220 free_frame_faces (f);
9221
9222 x_free_gcs (f);
9223 XFlush (FRAME_X_DISPLAY (f));
9224 }
9225
9226 if (f->output_data.x->saved_menu_event)
9227 xfree (f->output_data.x->saved_menu_event);
9228
9229 xfree (f->output_data.x);
9230 f->output_data.x = NULL;
9231
9232 if (f == dpyinfo->x_focus_frame)
9233 dpyinfo->x_focus_frame = 0;
9234 if (f == dpyinfo->x_focus_event_frame)
9235 dpyinfo->x_focus_event_frame = 0;
9236 if (f == dpyinfo->x_highlight_frame)
9237 dpyinfo->x_highlight_frame = 0;
9238
9239 if (f == dpyinfo->mouse_face_mouse_frame)
9240 {
9241 dpyinfo->mouse_face_beg_row
9242 = dpyinfo->mouse_face_beg_col = -1;
9243 dpyinfo->mouse_face_end_row
9244 = dpyinfo->mouse_face_end_col = -1;
9245 dpyinfo->mouse_face_window = Qnil;
9246 dpyinfo->mouse_face_deferred_gc = 0;
9247 dpyinfo->mouse_face_mouse_frame = 0;
9248 }
9249
9250 UNBLOCK_INPUT;
9251 }
9252
9253
9254 /* Destroy the X window of frame F. */
9255
9256 void
9257 x_destroy_window (f)
9258 struct frame *f;
9259 {
9260 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9261
9262 /* If a display connection is dead, don't try sending more
9263 commands to the X server. */
9264 if (dpyinfo->display != 0)
9265 x_free_frame_resources (f);
9266
9267 dpyinfo->reference_count--;
9268 }
9269
9270 \f
9271 /* Setting window manager hints. */
9272
9273 /* Set the normal size hints for the window manager, for frame F.
9274 FLAGS is the flags word to use--or 0 meaning preserve the flags
9275 that the window now has.
9276 If USER_POSITION is nonzero, we set the USPosition
9277 flag (this is useful when FLAGS is 0).
9278 The GTK version is in gtkutils.c */
9279
9280 #ifndef USE_GTK
9281 void
9282 x_wm_set_size_hint (f, flags, user_position)
9283 struct frame *f;
9284 long flags;
9285 int user_position;
9286 {
9287 XSizeHints size_hints;
9288
9289 #ifdef USE_X_TOOLKIT
9290 Arg al[2];
9291 int ac = 0;
9292 Dimension widget_width, widget_height;
9293 #endif
9294
9295 Window window = FRAME_OUTER_WINDOW (f);
9296
9297 /* Setting PMaxSize caused various problems. */
9298 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9299
9300 size_hints.x = f->output_data.x->left_pos;
9301 size_hints.y = f->output_data.x->top_pos;
9302
9303 #ifdef USE_X_TOOLKIT
9304 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
9305 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
9306 XtGetValues (f->output_data.x->widget, al, ac);
9307 size_hints.height = widget_height;
9308 size_hints.width = widget_width;
9309 #else /* not USE_X_TOOLKIT */
9310 size_hints.height = PIXEL_HEIGHT (f);
9311 size_hints.width = PIXEL_WIDTH (f);
9312 #endif /* not USE_X_TOOLKIT */
9313
9314 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
9315 size_hints.height_inc = f->output_data.x->line_height;
9316 size_hints.max_width
9317 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
9318 size_hints.max_height
9319 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
9320
9321 /* Calculate the base and minimum sizes.
9322
9323 (When we use the X toolkit, we don't do it here.
9324 Instead we copy the values that the widgets are using, below.) */
9325 #ifndef USE_X_TOOLKIT
9326 {
9327 int base_width, base_height;
9328 int min_rows = 0, min_cols = 0;
9329
9330 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
9331 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
9332
9333 check_frame_size (f, &min_rows, &min_cols);
9334
9335 /* The window manager uses the base width hints to calculate the
9336 current number of rows and columns in the frame while
9337 resizing; min_width and min_height aren't useful for this
9338 purpose, since they might not give the dimensions for a
9339 zero-row, zero-column frame.
9340
9341 We use the base_width and base_height members if we have
9342 them; otherwise, we set the min_width and min_height members
9343 to the size for a zero x zero frame. */
9344
9345 #ifdef HAVE_X11R4
9346 size_hints.flags |= PBaseSize;
9347 size_hints.base_width = base_width;
9348 size_hints.base_height = base_height;
9349 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9350 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9351 #else
9352 size_hints.min_width = base_width;
9353 size_hints.min_height = base_height;
9354 #endif
9355 }
9356
9357 /* If we don't need the old flags, we don't need the old hint at all. */
9358 if (flags)
9359 {
9360 size_hints.flags |= flags;
9361 goto no_read;
9362 }
9363 #endif /* not USE_X_TOOLKIT */
9364
9365 {
9366 XSizeHints hints; /* Sometimes I hate X Windows... */
9367 long supplied_return;
9368 int value;
9369
9370 #ifdef HAVE_X11R4
9371 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9372 &supplied_return);
9373 #else
9374 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
9375 #endif
9376
9377 #ifdef USE_X_TOOLKIT
9378 size_hints.base_height = hints.base_height;
9379 size_hints.base_width = hints.base_width;
9380 size_hints.min_height = hints.min_height;
9381 size_hints.min_width = hints.min_width;
9382 #endif
9383
9384 if (flags)
9385 size_hints.flags |= flags;
9386 else
9387 {
9388 if (value == 0)
9389 hints.flags = 0;
9390 if (hints.flags & PSize)
9391 size_hints.flags |= PSize;
9392 if (hints.flags & PPosition)
9393 size_hints.flags |= PPosition;
9394 if (hints.flags & USPosition)
9395 size_hints.flags |= USPosition;
9396 if (hints.flags & USSize)
9397 size_hints.flags |= USSize;
9398 }
9399 }
9400
9401 #ifndef USE_X_TOOLKIT
9402 no_read:
9403 #endif
9404
9405 #ifdef PWinGravity
9406 size_hints.win_gravity = f->output_data.x->win_gravity;
9407 size_hints.flags |= PWinGravity;
9408
9409 if (user_position)
9410 {
9411 size_hints.flags &= ~ PPosition;
9412 size_hints.flags |= USPosition;
9413 }
9414 #endif /* PWinGravity */
9415
9416 #ifdef HAVE_X11R4
9417 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9418 #else
9419 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9420 #endif
9421 }
9422 #endif /* not USE_GTK */
9423
9424 /* Used for IconicState or NormalState */
9425
9426 void
9427 x_wm_set_window_state (f, state)
9428 struct frame *f;
9429 int state;
9430 {
9431 #ifdef USE_X_TOOLKIT
9432 Arg al[1];
9433
9434 XtSetArg (al[0], XtNinitialState, state);
9435 XtSetValues (f->output_data.x->widget, al, 1);
9436 #else /* not USE_X_TOOLKIT */
9437 Window window = FRAME_X_WINDOW (f);
9438
9439 f->output_data.x->wm_hints.flags |= StateHint;
9440 f->output_data.x->wm_hints.initial_state = state;
9441
9442 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9443 #endif /* not USE_X_TOOLKIT */
9444 }
9445
9446 void
9447 x_wm_set_icon_pixmap (f, pixmap_id)
9448 struct frame *f;
9449 int pixmap_id;
9450 {
9451 Pixmap icon_pixmap;
9452
9453 #ifndef USE_X_TOOLKIT
9454 Window window = FRAME_OUTER_WINDOW (f);
9455 #endif
9456
9457 if (pixmap_id > 0)
9458 {
9459 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9460 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9461 }
9462 else
9463 {
9464 /* It seems there is no way to turn off use of an icon pixmap.
9465 The following line does it, only if no icon has yet been created,
9466 for some window managers. But with mwm it crashes.
9467 Some people say it should clear the IconPixmapHint bit in this case,
9468 but that doesn't work, and the X consortium said it isn't the
9469 right thing at all. Since there is no way to win,
9470 best to explicitly give up. */
9471 #if 0
9472 f->output_data.x->wm_hints.icon_pixmap = None;
9473 #else
9474 return;
9475 #endif
9476 }
9477
9478 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
9479
9480 {
9481 Arg al[1];
9482 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9483 XtSetValues (f->output_data.x->widget, al, 1);
9484 }
9485
9486 #else /* not USE_X_TOOLKIT */
9487
9488 f->output_data.x->wm_hints.flags |= IconPixmapHint;
9489 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9490
9491 #endif /* not USE_X_TOOLKIT */
9492 }
9493
9494 void
9495 x_wm_set_icon_position (f, icon_x, icon_y)
9496 struct frame *f;
9497 int icon_x, icon_y;
9498 {
9499 Window window = FRAME_OUTER_WINDOW (f);
9500
9501 f->output_data.x->wm_hints.flags |= IconPositionHint;
9502 f->output_data.x->wm_hints.icon_x = icon_x;
9503 f->output_data.x->wm_hints.icon_y = icon_y;
9504
9505 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9506 }
9507
9508 \f
9509 /***********************************************************************
9510 Fonts
9511 ***********************************************************************/
9512
9513 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
9514
9515 struct font_info *
9516 x_get_font_info (f, font_idx)
9517 FRAME_PTR f;
9518 int font_idx;
9519 {
9520 return (FRAME_X_FONT_TABLE (f) + font_idx);
9521 }
9522
9523
9524 /* Return a list of names of available fonts matching PATTERN on frame F.
9525
9526 If SIZE is > 0, it is the size (maximum bounds width) of fonts
9527 to be listed.
9528
9529 SIZE < 0 means include scalable fonts.
9530
9531 Frame F null means we have not yet created any frame on X, and
9532 consult the first display in x_display_list. MAXNAMES sets a limit
9533 on how many fonts to match. */
9534
9535 Lisp_Object
9536 x_list_fonts (f, pattern, size, maxnames)
9537 struct frame *f;
9538 Lisp_Object pattern;
9539 int size;
9540 int maxnames;
9541 {
9542 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
9543 Lisp_Object tem, second_best;
9544 struct x_display_info *dpyinfo
9545 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
9546 Display *dpy = dpyinfo->display;
9547 int try_XLoadQueryFont = 0;
9548 int count;
9549 int allow_scalable_fonts_p = 0;
9550
9551 if (size < 0)
9552 {
9553 allow_scalable_fonts_p = 1;
9554 size = 0;
9555 }
9556
9557 patterns = Fassoc (pattern, Valternate_fontname_alist);
9558 if (NILP (patterns))
9559 patterns = Fcons (pattern, Qnil);
9560
9561 if (maxnames == 1 && !size)
9562 /* We can return any single font matching PATTERN. */
9563 try_XLoadQueryFont = 1;
9564
9565 for (; CONSP (patterns); patterns = XCDR (patterns))
9566 {
9567 int num_fonts;
9568 char **names = NULL;
9569
9570 pattern = XCAR (patterns);
9571 /* See if we cached the result for this particular query.
9572 The cache is an alist of the form:
9573 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
9574 tem = XCDR (dpyinfo->name_list_element);
9575 key = Fcons (Fcons (pattern, make_number (maxnames)),
9576 allow_scalable_fonts_p ? Qt : Qnil);
9577 list = Fassoc (key, tem);
9578 if (!NILP (list))
9579 {
9580 list = Fcdr_safe (list);
9581 /* We have a cashed list. Don't have to get the list again. */
9582 goto label_cached;
9583 }
9584
9585 /* At first, put PATTERN in the cache. */
9586
9587 BLOCK_INPUT;
9588 count = x_catch_errors (dpy);
9589
9590 if (try_XLoadQueryFont)
9591 {
9592 XFontStruct *font;
9593 unsigned long value;
9594
9595 font = XLoadQueryFont (dpy, SDATA (pattern));
9596 if (x_had_errors_p (dpy))
9597 {
9598 /* This error is perhaps due to insufficient memory on X
9599 server. Let's just ignore it. */
9600 font = NULL;
9601 x_clear_errors (dpy);
9602 }
9603
9604 if (font
9605 && XGetFontProperty (font, XA_FONT, &value))
9606 {
9607 char *name = (char *) XGetAtomName (dpy, (Atom) value);
9608 int len = strlen (name);
9609 char *tmp;
9610
9611 /* If DXPC (a Differential X Protocol Compressor)
9612 Ver.3.7 is running, XGetAtomName will return null
9613 string. We must avoid such a name. */
9614 if (len == 0)
9615 try_XLoadQueryFont = 0;
9616 else
9617 {
9618 num_fonts = 1;
9619 names = (char **) alloca (sizeof (char *));
9620 /* Some systems only allow alloca assigned to a
9621 simple var. */
9622 tmp = (char *) alloca (len + 1); names[0] = tmp;
9623 bcopy (name, names[0], len + 1);
9624 XFree (name);
9625 }
9626 }
9627 else
9628 try_XLoadQueryFont = 0;
9629
9630 if (font)
9631 XFreeFont (dpy, font);
9632 }
9633
9634 if (!try_XLoadQueryFont)
9635 {
9636 /* We try at least 10 fonts because XListFonts will return
9637 auto-scaled fonts at the head. */
9638 if (maxnames < 0)
9639 {
9640 int limit;
9641
9642 for (limit = 500;;)
9643 {
9644 names = XListFonts (dpy, SDATA (pattern), limit, &num_fonts);
9645 if (num_fonts == limit)
9646 {
9647 BLOCK_INPUT;
9648 XFreeFontNames (names);
9649 UNBLOCK_INPUT;
9650 limit *= 2;
9651 }
9652 else
9653 break;
9654 }
9655 }
9656 else
9657 names = XListFonts (dpy, SDATA (pattern), max (maxnames, 10),
9658 &num_fonts);
9659
9660 if (x_had_errors_p (dpy))
9661 {
9662 /* This error is perhaps due to insufficient memory on X
9663 server. Let's just ignore it. */
9664 names = NULL;
9665 x_clear_errors (dpy);
9666 }
9667 }
9668
9669 x_uncatch_errors (dpy, count);
9670 UNBLOCK_INPUT;
9671
9672 if (names)
9673 {
9674 int i;
9675
9676 /* Make a list of all the fonts we got back.
9677 Store that in the font cache for the display. */
9678 for (i = 0; i < num_fonts; i++)
9679 {
9680 int width = 0;
9681 char *p = names[i];
9682 int average_width = -1, dashes = 0;
9683
9684 /* Count the number of dashes in NAMES[I]. If there are
9685 14 dashes, and the field value following 12th dash
9686 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
9687 is usually too ugly to be used for editing. Let's
9688 ignore it. */
9689 while (*p)
9690 if (*p++ == '-')
9691 {
9692 dashes++;
9693 if (dashes == 7) /* PIXEL_SIZE field */
9694 width = atoi (p);
9695 else if (dashes == 12) /* AVERAGE_WIDTH field */
9696 average_width = atoi (p);
9697 }
9698
9699 if (allow_scalable_fonts_p
9700 || dashes < 14 || average_width != 0)
9701 {
9702 tem = build_string (names[i]);
9703 if (NILP (Fassoc (tem, list)))
9704 {
9705 if (STRINGP (Vx_pixel_size_width_font_regexp)
9706 && ((fast_c_string_match_ignore_case
9707 (Vx_pixel_size_width_font_regexp, names[i]))
9708 >= 0))
9709 /* We can set the value of PIXEL_SIZE to the
9710 width of this font. */
9711 list = Fcons (Fcons (tem, make_number (width)), list);
9712 else
9713 /* For the moment, width is not known. */
9714 list = Fcons (Fcons (tem, Qnil), list);
9715 }
9716 }
9717 }
9718
9719 if (!try_XLoadQueryFont)
9720 {
9721 BLOCK_INPUT;
9722 XFreeFontNames (names);
9723 UNBLOCK_INPUT;
9724 }
9725 }
9726
9727 /* Now store the result in the cache. */
9728 XSETCDR (dpyinfo->name_list_element,
9729 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
9730
9731 label_cached:
9732 if (NILP (list)) continue; /* Try the remaining alternatives. */
9733
9734 newlist = second_best = Qnil;
9735 /* Make a list of the fonts that have the right width. */
9736 for (; CONSP (list); list = XCDR (list))
9737 {
9738 int found_size;
9739
9740 tem = XCAR (list);
9741
9742 if (!CONSP (tem) || NILP (XCAR (tem)))
9743 continue;
9744 if (!size)
9745 {
9746 newlist = Fcons (XCAR (tem), newlist);
9747 continue;
9748 }
9749
9750 if (!INTEGERP (XCDR (tem)))
9751 {
9752 /* Since we have not yet known the size of this font, we
9753 must try slow function call XLoadQueryFont. */
9754 XFontStruct *thisinfo;
9755
9756 BLOCK_INPUT;
9757 count = x_catch_errors (dpy);
9758 thisinfo = XLoadQueryFont (dpy,
9759 SDATA (XCAR (tem)));
9760 if (x_had_errors_p (dpy))
9761 {
9762 /* This error is perhaps due to insufficient memory on X
9763 server. Let's just ignore it. */
9764 thisinfo = NULL;
9765 x_clear_errors (dpy);
9766 }
9767 x_uncatch_errors (dpy, count);
9768 UNBLOCK_INPUT;
9769
9770 if (thisinfo)
9771 {
9772 XSETCDR (tem,
9773 (thisinfo->min_bounds.width == 0
9774 ? make_number (0)
9775 : make_number (thisinfo->max_bounds.width)));
9776 BLOCK_INPUT;
9777 XFreeFont (dpy, thisinfo);
9778 UNBLOCK_INPUT;
9779 }
9780 else
9781 /* For unknown reason, the previous call of XListFont had
9782 returned a font which can't be opened. Record the size
9783 as 0 not to try to open it again. */
9784 XSETCDR (tem, make_number (0));
9785 }
9786
9787 found_size = XINT (XCDR (tem));
9788 if (found_size == size)
9789 newlist = Fcons (XCAR (tem), newlist);
9790 else if (found_size > 0)
9791 {
9792 if (NILP (second_best))
9793 second_best = tem;
9794 else if (found_size < size)
9795 {
9796 if (XINT (XCDR (second_best)) > size
9797 || XINT (XCDR (second_best)) < found_size)
9798 second_best = tem;
9799 }
9800 else
9801 {
9802 if (XINT (XCDR (second_best)) > size
9803 && XINT (XCDR (second_best)) > found_size)
9804 second_best = tem;
9805 }
9806 }
9807 }
9808 if (!NILP (newlist))
9809 break;
9810 else if (!NILP (second_best))
9811 {
9812 newlist = Fcons (XCAR (second_best), Qnil);
9813 break;
9814 }
9815 }
9816
9817 return newlist;
9818 }
9819
9820
9821 #if GLYPH_DEBUG
9822
9823 /* Check that FONT is valid on frame F. It is if it can be found in F's
9824 font table. */
9825
9826 static void
9827 x_check_font (f, font)
9828 struct frame *f;
9829 XFontStruct *font;
9830 {
9831 int i;
9832 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9833
9834 xassert (font != NULL);
9835
9836 for (i = 0; i < dpyinfo->n_fonts; i++)
9837 if (dpyinfo->font_table[i].name
9838 && font == dpyinfo->font_table[i].font)
9839 break;
9840
9841 xassert (i < dpyinfo->n_fonts);
9842 }
9843
9844 #endif /* GLYPH_DEBUG != 0 */
9845
9846 /* Set *W to the minimum width, *H to the minimum font height of FONT.
9847 Note: There are (broken) X fonts out there with invalid XFontStruct
9848 min_bounds contents. For example, handa@etl.go.jp reports that
9849 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
9850 have font->min_bounds.width == 0. */
9851
9852 static INLINE void
9853 x_font_min_bounds (font, w, h)
9854 XFontStruct *font;
9855 int *w, *h;
9856 {
9857 *h = FONT_HEIGHT (font);
9858 *w = font->min_bounds.width;
9859
9860 /* Try to handle the case where FONT->min_bounds has invalid
9861 contents. Since the only font known to have invalid min_bounds
9862 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
9863 if (*w <= 0)
9864 *w = font->max_bounds.width;
9865 }
9866
9867
9868 /* Compute the smallest character width and smallest font height over
9869 all fonts available on frame F. Set the members smallest_char_width
9870 and smallest_font_height in F's x_display_info structure to
9871 the values computed. Value is non-zero if smallest_font_height or
9872 smallest_char_width become smaller than they were before. */
9873
9874 static int
9875 x_compute_min_glyph_bounds (f)
9876 struct frame *f;
9877 {
9878 int i;
9879 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9880 XFontStruct *font;
9881 int old_width = dpyinfo->smallest_char_width;
9882 int old_height = dpyinfo->smallest_font_height;
9883
9884 dpyinfo->smallest_font_height = 100000;
9885 dpyinfo->smallest_char_width = 100000;
9886
9887 for (i = 0; i < dpyinfo->n_fonts; ++i)
9888 if (dpyinfo->font_table[i].name)
9889 {
9890 struct font_info *fontp = dpyinfo->font_table + i;
9891 int w, h;
9892
9893 font = (XFontStruct *) fontp->font;
9894 xassert (font != (XFontStruct *) ~0);
9895 x_font_min_bounds (font, &w, &h);
9896
9897 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
9898 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
9899 }
9900
9901 xassert (dpyinfo->smallest_char_width > 0
9902 && dpyinfo->smallest_font_height > 0);
9903
9904 return (dpyinfo->n_fonts == 1
9905 || dpyinfo->smallest_char_width < old_width
9906 || dpyinfo->smallest_font_height < old_height);
9907 }
9908
9909
9910 /* Load font named FONTNAME of the size SIZE for frame F, and return a
9911 pointer to the structure font_info while allocating it dynamically.
9912 If SIZE is 0, load any size of font.
9913 If loading is failed, return NULL. */
9914
9915 struct font_info *
9916 x_load_font (f, fontname, size)
9917 struct frame *f;
9918 register char *fontname;
9919 int size;
9920 {
9921 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9922 Lisp_Object font_names;
9923 int count;
9924
9925 /* Get a list of all the fonts that match this name. Once we
9926 have a list of matching fonts, we compare them against the fonts
9927 we already have by comparing names. */
9928 font_names = x_list_fonts (f, build_string (fontname), size, 1);
9929
9930 if (!NILP (font_names))
9931 {
9932 Lisp_Object tail;
9933 int i;
9934
9935 for (i = 0; i < dpyinfo->n_fonts; i++)
9936 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
9937 if (dpyinfo->font_table[i].name
9938 && (!strcmp (dpyinfo->font_table[i].name,
9939 SDATA (XCAR (tail)))
9940 || !strcmp (dpyinfo->font_table[i].full_name,
9941 SDATA (XCAR (tail)))))
9942 return (dpyinfo->font_table + i);
9943 }
9944
9945 /* Load the font and add it to the table. */
9946 {
9947 char *full_name;
9948 XFontStruct *font;
9949 struct font_info *fontp;
9950 unsigned long value;
9951 int i;
9952
9953 /* If we have found fonts by x_list_font, load one of them. If
9954 not, we still try to load a font by the name given as FONTNAME
9955 because XListFonts (called in x_list_font) of some X server has
9956 a bug of not finding a font even if the font surely exists and
9957 is loadable by XLoadQueryFont. */
9958 if (size > 0 && !NILP (font_names))
9959 fontname = (char *) SDATA (XCAR (font_names));
9960
9961 BLOCK_INPUT;
9962 count = x_catch_errors (FRAME_X_DISPLAY (f));
9963 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
9964 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
9965 {
9966 /* This error is perhaps due to insufficient memory on X
9967 server. Let's just ignore it. */
9968 font = NULL;
9969 x_clear_errors (FRAME_X_DISPLAY (f));
9970 }
9971 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
9972 UNBLOCK_INPUT;
9973 if (!font)
9974 return NULL;
9975
9976 /* Find a free slot in the font table. */
9977 for (i = 0; i < dpyinfo->n_fonts; ++i)
9978 if (dpyinfo->font_table[i].name == NULL)
9979 break;
9980
9981 /* If no free slot found, maybe enlarge the font table. */
9982 if (i == dpyinfo->n_fonts
9983 && dpyinfo->n_fonts == dpyinfo->font_table_size)
9984 {
9985 int sz;
9986 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
9987 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
9988 dpyinfo->font_table
9989 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
9990 }
9991
9992 fontp = dpyinfo->font_table + i;
9993 if (i == dpyinfo->n_fonts)
9994 ++dpyinfo->n_fonts;
9995
9996 /* Now fill in the slots of *FONTP. */
9997 BLOCK_INPUT;
9998 fontp->font = font;
9999 fontp->font_idx = i;
10000 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
10001 bcopy (fontname, fontp->name, strlen (fontname) + 1);
10002
10003 /* Try to get the full name of FONT. Put it in FULL_NAME. */
10004 full_name = 0;
10005 if (XGetFontProperty (font, XA_FONT, &value))
10006 {
10007 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
10008 char *p = name;
10009 int dashes = 0;
10010
10011 /* Count the number of dashes in the "full name".
10012 If it is too few, this isn't really the font's full name,
10013 so don't use it.
10014 In X11R4, the fonts did not come with their canonical names
10015 stored in them. */
10016 while (*p)
10017 {
10018 if (*p == '-')
10019 dashes++;
10020 p++;
10021 }
10022
10023 if (dashes >= 13)
10024 {
10025 full_name = (char *) xmalloc (p - name + 1);
10026 bcopy (name, full_name, p - name + 1);
10027 }
10028
10029 XFree (name);
10030 }
10031
10032 if (full_name != 0)
10033 fontp->full_name = full_name;
10034 else
10035 fontp->full_name = fontp->name;
10036
10037 fontp->size = font->max_bounds.width;
10038 fontp->height = FONT_HEIGHT (font);
10039
10040 if (NILP (font_names))
10041 {
10042 /* We come here because of a bug of XListFonts mentioned at
10043 the head of this block. Let's store this information in
10044 the cache for x_list_fonts. */
10045 Lisp_Object lispy_name = build_string (fontname);
10046 Lisp_Object lispy_full_name = build_string (fontp->full_name);
10047 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
10048 Qnil);
10049
10050 XSETCDR (dpyinfo->name_list_element,
10051 Fcons (Fcons (key,
10052 Fcons (Fcons (lispy_full_name,
10053 make_number (fontp->size)),
10054 Qnil)),
10055 XCDR (dpyinfo->name_list_element)));
10056 if (full_name)
10057 {
10058 key = Fcons (Fcons (lispy_full_name, make_number (256)),
10059 Qnil);
10060 XSETCDR (dpyinfo->name_list_element,
10061 Fcons (Fcons (key,
10062 Fcons (Fcons (lispy_full_name,
10063 make_number (fontp->size)),
10064 Qnil)),
10065 XCDR (dpyinfo->name_list_element)));
10066 }
10067 }
10068
10069 /* The slot `encoding' specifies how to map a character
10070 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
10071 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
10072 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
10073 2:0xA020..0xFF7F). For the moment, we don't know which charset
10074 uses this font. So, we set information in fontp->encoding[1]
10075 which is never used by any charset. If mapping can't be
10076 decided, set FONT_ENCODING_NOT_DECIDED. */
10077 fontp->encoding[1]
10078 = (font->max_byte1 == 0
10079 /* 1-byte font */
10080 ? (font->min_char_or_byte2 < 0x80
10081 ? (font->max_char_or_byte2 < 0x80
10082 ? 0 /* 0x20..0x7F */
10083 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
10084 : 1) /* 0xA0..0xFF */
10085 /* 2-byte font */
10086 : (font->min_byte1 < 0x80
10087 ? (font->max_byte1 < 0x80
10088 ? (font->min_char_or_byte2 < 0x80
10089 ? (font->max_char_or_byte2 < 0x80
10090 ? 0 /* 0x2020..0x7F7F */
10091 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
10092 : 3) /* 0x20A0..0x7FFF */
10093 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
10094 : (font->min_char_or_byte2 < 0x80
10095 ? (font->max_char_or_byte2 < 0x80
10096 ? 2 /* 0xA020..0xFF7F */
10097 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
10098 : 1))); /* 0xA0A0..0xFFFF */
10099
10100 fontp->baseline_offset
10101 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
10102 ? (long) value : 0);
10103 fontp->relative_compose
10104 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
10105 ? (long) value : 0);
10106 fontp->default_ascent
10107 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
10108 ? (long) value : 0);
10109
10110 /* Set global flag fonts_changed_p to non-zero if the font loaded
10111 has a character with a smaller width than any other character
10112 before, or if the font loaded has a smaller height than any
10113 other font loaded before. If this happens, it will make a
10114 glyph matrix reallocation necessary. */
10115 fonts_changed_p |= x_compute_min_glyph_bounds (f);
10116 UNBLOCK_INPUT;
10117 return fontp;
10118 }
10119 }
10120
10121
10122 /* Return a pointer to struct font_info of a font named FONTNAME for
10123 frame F. If no such font is loaded, return NULL. */
10124
10125 struct font_info *
10126 x_query_font (f, fontname)
10127 struct frame *f;
10128 register char *fontname;
10129 {
10130 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10131 int i;
10132
10133 for (i = 0; i < dpyinfo->n_fonts; i++)
10134 if (dpyinfo->font_table[i].name
10135 && (!strcmp (dpyinfo->font_table[i].name, fontname)
10136 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
10137 return (dpyinfo->font_table + i);
10138 return NULL;
10139 }
10140
10141
10142 /* Find a CCL program for a font specified by FONTP, and set the member
10143 `encoder' of the structure. */
10144
10145 void
10146 x_find_ccl_program (fontp)
10147 struct font_info *fontp;
10148 {
10149 Lisp_Object list, elt;
10150
10151 elt = Qnil;
10152 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
10153 {
10154 elt = XCAR (list);
10155 if (CONSP (elt)
10156 && STRINGP (XCAR (elt))
10157 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
10158 >= 0)
10159 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
10160 >= 0)))
10161 break;
10162 }
10163
10164 if (! NILP (list))
10165 {
10166 struct ccl_program *ccl
10167 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
10168
10169 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
10170 xfree (ccl);
10171 else
10172 fontp->font_encoder = ccl;
10173 }
10174 }
10175
10176
10177 \f
10178 /***********************************************************************
10179 Initialization
10180 ***********************************************************************/
10181
10182 #ifdef USE_X_TOOLKIT
10183 static XrmOptionDescRec emacs_options[] = {
10184 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
10185 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
10186
10187 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
10188 XrmoptionSepArg, NULL},
10189 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
10190
10191 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10192 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10193 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10194 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10195 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10196 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
10197 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
10198 };
10199 #endif /* USE_X_TOOLKIT */
10200
10201 static int x_initialized;
10202
10203 #ifdef MULTI_KBOARD
10204 /* Test whether two display-name strings agree up to the dot that separates
10205 the screen number from the server number. */
10206 static int
10207 same_x_server (name1, name2)
10208 const char *name1, *name2;
10209 {
10210 int seen_colon = 0;
10211 const unsigned char *system_name = SDATA (Vsystem_name);
10212 int system_name_length = strlen (system_name);
10213 int length_until_period = 0;
10214
10215 while (system_name[length_until_period] != 0
10216 && system_name[length_until_period] != '.')
10217 length_until_period++;
10218
10219 /* Treat `unix' like an empty host name. */
10220 if (! strncmp (name1, "unix:", 5))
10221 name1 += 4;
10222 if (! strncmp (name2, "unix:", 5))
10223 name2 += 4;
10224 /* Treat this host's name like an empty host name. */
10225 if (! strncmp (name1, system_name, system_name_length)
10226 && name1[system_name_length] == ':')
10227 name1 += system_name_length;
10228 if (! strncmp (name2, system_name, system_name_length)
10229 && name2[system_name_length] == ':')
10230 name2 += system_name_length;
10231 /* Treat this host's domainless name like an empty host name. */
10232 if (! strncmp (name1, system_name, length_until_period)
10233 && name1[length_until_period] == ':')
10234 name1 += length_until_period;
10235 if (! strncmp (name2, system_name, length_until_period)
10236 && name2[length_until_period] == ':')
10237 name2 += length_until_period;
10238
10239 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
10240 {
10241 if (*name1 == ':')
10242 seen_colon++;
10243 if (seen_colon && *name1 == '.')
10244 return 1;
10245 }
10246 return (seen_colon
10247 && (*name1 == '.' || *name1 == '\0')
10248 && (*name2 == '.' || *name2 == '\0'));
10249 }
10250 #endif
10251
10252 struct x_display_info *
10253 x_term_init (display_name, xrm_option, resource_name)
10254 Lisp_Object display_name;
10255 char *xrm_option;
10256 char *resource_name;
10257 {
10258 int connection;
10259 Display *dpy;
10260 struct x_display_info *dpyinfo;
10261 XrmDatabase xrdb;
10262
10263 BLOCK_INPUT;
10264
10265 if (!x_initialized)
10266 {
10267 x_initialize ();
10268 x_initialized = 1;
10269 }
10270
10271 #ifdef USE_GTK
10272 {
10273 #define NUM_ARGV 10
10274 int argc;
10275 char *argv[NUM_ARGV];
10276 char **argv2 = argv;
10277 GdkAtom atom;
10278
10279 /* GTK 2.0 can only handle one display, GTK 2.2 can handle more
10280 than one, but this remains to be implemented. */
10281 if (x_initialized > 1)
10282 return 0;
10283
10284 x_initialized++;
10285
10286 for (argc = 0; argc < NUM_ARGV; ++argc)
10287 argv[argc] = 0;
10288
10289 argc = 0;
10290 argv[argc++] = initial_argv[0];
10291
10292 if (! NILP (display_name))
10293 {
10294 argv[argc++] = "--display";
10295 argv[argc++] = SDATA (display_name);
10296 }
10297
10298 argv[argc++] = "--name";
10299 argv[argc++] = resource_name;
10300
10301 #ifdef HAVE_X11R5
10302 XSetLocaleModifiers ("");
10303 #endif
10304
10305 gtk_init (&argc, &argv2);
10306
10307 /* gtk_init does set_locale. We must fix locale after calling it. */
10308 fixup_locale ();
10309 xg_initialize ();
10310
10311 dpy = GDK_DISPLAY ();
10312
10313 /* NULL window -> events for all windows go to our function */
10314 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
10315
10316 /* Load our own gtkrc if it exists. */
10317 {
10318 struct gcpro gcpro1, gcpro2;
10319 char *file = "~/.emacs.d/gtkrc";
10320 Lisp_Object s, abs_file;
10321
10322 GCPRO2 (s, abs_file);
10323 s = make_string (file, strlen (file));
10324 abs_file = Fexpand_file_name(s, Qnil);
10325
10326 if (! NILP (abs_file) && Ffile_readable_p (abs_file))
10327 gtk_rc_parse (SDATA (abs_file));
10328
10329 UNGCPRO;
10330 }
10331
10332 XSetErrorHandler (x_error_handler);
10333 XSetIOErrorHandler (x_io_error_quitter);
10334 }
10335 #else /* not USE_GTK */
10336 #ifdef USE_X_TOOLKIT
10337 /* weiner@footloose.sps.mot.com reports that this causes
10338 errors with X11R5:
10339 X protocol error: BadAtom (invalid Atom parameter)
10340 on protocol request 18skiloaf.
10341 So let's not use it until R6. */
10342 #ifdef HAVE_X11XTR6
10343 XtSetLanguageProc (NULL, NULL, NULL);
10344 #endif
10345
10346 {
10347 int argc = 0;
10348 char *argv[3];
10349
10350 argv[0] = "";
10351 argc = 1;
10352 if (xrm_option)
10353 {
10354 argv[argc++] = "-xrm";
10355 argv[argc++] = xrm_option;
10356 }
10357 turn_on_atimers (0);
10358 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
10359 resource_name, EMACS_CLASS,
10360 emacs_options, XtNumber (emacs_options),
10361 &argc, argv);
10362 turn_on_atimers (1);
10363
10364 #ifdef HAVE_X11XTR6
10365 /* I think this is to compensate for XtSetLanguageProc. */
10366 fixup_locale ();
10367 #endif
10368 }
10369
10370 #else /* not USE_X_TOOLKIT */
10371 #ifdef HAVE_X11R5
10372 XSetLocaleModifiers ("");
10373 #endif
10374 dpy = XOpenDisplay (SDATA (display_name));
10375 #endif /* not USE_X_TOOLKIT */
10376 #endif /* not USE_GTK*/
10377
10378 /* Detect failure. */
10379 if (dpy == 0)
10380 {
10381 UNBLOCK_INPUT;
10382 return 0;
10383 }
10384
10385 /* We have definitely succeeded. Record the new connection. */
10386
10387 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
10388 bzero (dpyinfo, sizeof *dpyinfo);
10389
10390 #ifdef MULTI_KBOARD
10391 {
10392 struct x_display_info *share;
10393 Lisp_Object tail;
10394
10395 for (share = x_display_list, tail = x_display_name_list; share;
10396 share = share->next, tail = XCDR (tail))
10397 if (same_x_server (SDATA (XCAR (XCAR (tail))),
10398 SDATA (display_name)))
10399 break;
10400 if (share)
10401 dpyinfo->kboard = share->kboard;
10402 else
10403 {
10404 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
10405 init_kboard (dpyinfo->kboard);
10406 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10407 {
10408 char *vendor = ServerVendor (dpy);
10409 UNBLOCK_INPUT;
10410 dpyinfo->kboard->Vsystem_key_alist
10411 = call1 (Qvendor_specific_keysyms,
10412 build_string (vendor ? vendor : ""));
10413 BLOCK_INPUT;
10414 }
10415
10416 dpyinfo->kboard->next_kboard = all_kboards;
10417 all_kboards = dpyinfo->kboard;
10418 /* Don't let the initial kboard remain current longer than necessary.
10419 That would cause problems if a file loaded on startup tries to
10420 prompt in the mini-buffer. */
10421 if (current_kboard == initial_kboard)
10422 current_kboard = dpyinfo->kboard;
10423 }
10424 dpyinfo->kboard->reference_count++;
10425 }
10426 #endif
10427
10428 /* Put this display on the chain. */
10429 dpyinfo->next = x_display_list;
10430 x_display_list = dpyinfo;
10431
10432 /* Put it on x_display_name_list as well, to keep them parallel. */
10433 x_display_name_list = Fcons (Fcons (display_name, Qnil),
10434 x_display_name_list);
10435 dpyinfo->name_list_element = XCAR (x_display_name_list);
10436
10437 dpyinfo->display = dpy;
10438
10439 #if 0
10440 XSetAfterFunction (x_current_display, x_trace_wire);
10441 #endif /* ! 0 */
10442
10443 dpyinfo->x_id_name
10444 = (char *) xmalloc (SBYTES (Vinvocation_name)
10445 + SBYTES (Vsystem_name)
10446 + 2);
10447 sprintf (dpyinfo->x_id_name, "%s@%s",
10448 SDATA (Vinvocation_name), SDATA (Vsystem_name));
10449
10450 /* Figure out which modifier bits mean what. */
10451 x_find_modifier_meanings (dpyinfo);
10452
10453 /* Get the scroll bar cursor. */
10454 dpyinfo->vertical_scroll_bar_cursor
10455 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10456
10457 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10458 resource_name, EMACS_CLASS);
10459 #ifdef HAVE_XRMSETDATABASE
10460 XrmSetDatabase (dpyinfo->display, xrdb);
10461 #else
10462 dpyinfo->display->db = xrdb;
10463 #endif
10464 /* Put the rdb where we can find it in a way that works on
10465 all versions. */
10466 dpyinfo->xrdb = xrdb;
10467
10468 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10469 DefaultScreen (dpyinfo->display));
10470 select_visual (dpyinfo);
10471 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10472 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
10473 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
10474 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10475 dpyinfo->grabbed = 0;
10476 dpyinfo->reference_count = 0;
10477 dpyinfo->icon_bitmap_id = -1;
10478 dpyinfo->font_table = NULL;
10479 dpyinfo->n_fonts = 0;
10480 dpyinfo->font_table_size = 0;
10481 dpyinfo->bitmaps = 0;
10482 dpyinfo->bitmaps_size = 0;
10483 dpyinfo->bitmaps_last = 0;
10484 dpyinfo->scratch_cursor_gc = 0;
10485 dpyinfo->mouse_face_mouse_frame = 0;
10486 dpyinfo->mouse_face_deferred_gc = 0;
10487 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10488 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
10489 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10490 dpyinfo->mouse_face_window = Qnil;
10491 dpyinfo->mouse_face_overlay = Qnil;
10492 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
10493 dpyinfo->mouse_face_defer = 0;
10494 dpyinfo->mouse_face_hidden = 0;
10495 dpyinfo->x_focus_frame = 0;
10496 dpyinfo->x_focus_event_frame = 0;
10497 dpyinfo->x_highlight_frame = 0;
10498 dpyinfo->image_cache = make_image_cache ();
10499
10500 /* See if a private colormap is requested. */
10501 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10502 {
10503 if (dpyinfo->visual->class == PseudoColor)
10504 {
10505 Lisp_Object value;
10506 value = display_x_get_resource (dpyinfo,
10507 build_string ("privateColormap"),
10508 build_string ("PrivateColormap"),
10509 Qnil, Qnil);
10510 if (STRINGP (value)
10511 && (!strcmp (SDATA (value), "true")
10512 || !strcmp (SDATA (value), "on")))
10513 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10514 }
10515 }
10516 else
10517 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10518 dpyinfo->visual, AllocNone);
10519
10520 {
10521 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10522 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10523 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
10524 dpyinfo->resy = pixels * 25.4 / mm;
10525 pixels = DisplayWidth (dpyinfo->display, screen_number);
10526 mm = DisplayWidthMM (dpyinfo->display, screen_number);
10527 dpyinfo->resx = pixels * 25.4 / mm;
10528 }
10529
10530 dpyinfo->Xatom_wm_protocols
10531 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
10532 dpyinfo->Xatom_wm_take_focus
10533 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
10534 dpyinfo->Xatom_wm_save_yourself
10535 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
10536 dpyinfo->Xatom_wm_delete_window
10537 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
10538 dpyinfo->Xatom_wm_change_state
10539 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
10540 dpyinfo->Xatom_wm_configure_denied
10541 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
10542 dpyinfo->Xatom_wm_window_moved
10543 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
10544 dpyinfo->Xatom_editres
10545 = XInternAtom (dpyinfo->display, "Editres", False);
10546 dpyinfo->Xatom_CLIPBOARD
10547 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
10548 dpyinfo->Xatom_TIMESTAMP
10549 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
10550 dpyinfo->Xatom_TEXT
10551 = XInternAtom (dpyinfo->display, "TEXT", False);
10552 dpyinfo->Xatom_COMPOUND_TEXT
10553 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
10554 dpyinfo->Xatom_UTF8_STRING
10555 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
10556 dpyinfo->Xatom_DELETE
10557 = XInternAtom (dpyinfo->display, "DELETE", False);
10558 dpyinfo->Xatom_MULTIPLE
10559 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
10560 dpyinfo->Xatom_INCR
10561 = XInternAtom (dpyinfo->display, "INCR", False);
10562 dpyinfo->Xatom_EMACS_TMP
10563 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
10564 dpyinfo->Xatom_TARGETS
10565 = XInternAtom (dpyinfo->display, "TARGETS", False);
10566 dpyinfo->Xatom_NULL
10567 = XInternAtom (dpyinfo->display, "NULL", False);
10568 dpyinfo->Xatom_ATOM_PAIR
10569 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
10570 /* For properties of font. */
10571 dpyinfo->Xatom_PIXEL_SIZE
10572 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
10573 dpyinfo->Xatom_MULE_BASELINE_OFFSET
10574 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
10575 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
10576 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
10577 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
10578 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
10579
10580 /* Ghostscript support. */
10581 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
10582 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
10583
10584 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
10585 False);
10586
10587 dpyinfo->cut_buffers_initialized = 0;
10588
10589 connection = ConnectionNumber (dpyinfo->display);
10590 dpyinfo->connection = connection;
10591
10592 {
10593 char null_bits[1];
10594
10595 null_bits[0] = 0x00;
10596
10597 dpyinfo->null_pixel
10598 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10599 null_bits, 1, 1, (long) 0, (long) 0,
10600 1);
10601 }
10602
10603 {
10604 extern int gray_bitmap_width, gray_bitmap_height;
10605 extern char *gray_bitmap_bits;
10606 dpyinfo->gray
10607 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10608 gray_bitmap_bits,
10609 gray_bitmap_width, gray_bitmap_height,
10610 (unsigned long) 1, (unsigned long) 0, 1);
10611 }
10612
10613 #ifdef HAVE_X_I18N
10614 xim_initialize (dpyinfo, resource_name);
10615 #endif
10616
10617 #ifdef subprocesses
10618 /* This is only needed for distinguishing keyboard and process input. */
10619 if (connection != 0)
10620 add_keyboard_wait_descriptor (connection);
10621 #endif
10622
10623 #ifndef F_SETOWN_BUG
10624 #ifdef F_SETOWN
10625 #ifdef F_SETOWN_SOCK_NEG
10626 /* stdin is a socket here */
10627 fcntl (connection, F_SETOWN, -getpid ());
10628 #else /* ! defined (F_SETOWN_SOCK_NEG) */
10629 fcntl (connection, F_SETOWN, getpid ());
10630 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
10631 #endif /* ! defined (F_SETOWN) */
10632 #endif /* F_SETOWN_BUG */
10633
10634 #ifdef SIGIO
10635 if (interrupt_input)
10636 init_sigio (connection);
10637 #endif /* ! defined (SIGIO) */
10638
10639 #ifdef USE_LUCID
10640 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
10641 /* Make sure that we have a valid font for dialog boxes
10642 so that Xt does not crash. */
10643 {
10644 Display *dpy = dpyinfo->display;
10645 XrmValue d, fr, to;
10646 Font font;
10647 int count;
10648
10649 d.addr = (XPointer)&dpy;
10650 d.size = sizeof (Display *);
10651 fr.addr = XtDefaultFont;
10652 fr.size = sizeof (XtDefaultFont);
10653 to.size = sizeof (Font *);
10654 to.addr = (XPointer)&font;
10655 count = x_catch_errors (dpy);
10656 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
10657 abort ();
10658 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
10659 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
10660 x_uncatch_errors (dpy, count);
10661 }
10662 #endif
10663 #endif
10664
10665 /* See if we should run in synchronous mode. This is useful
10666 for debugging X code. */
10667 {
10668 Lisp_Object value;
10669 value = display_x_get_resource (dpyinfo,
10670 build_string ("synchronous"),
10671 build_string ("Synchronous"),
10672 Qnil, Qnil);
10673 if (STRINGP (value)
10674 && (!strcmp (SDATA (value), "true")
10675 || !strcmp (SDATA (value), "on")))
10676 XSynchronize (dpyinfo->display, True);
10677 }
10678
10679 UNBLOCK_INPUT;
10680
10681 return dpyinfo;
10682 }
10683 \f
10684 /* Get rid of display DPYINFO, assuming all frames are already gone,
10685 and without sending any more commands to the X server. */
10686
10687 void
10688 x_delete_display (dpyinfo)
10689 struct x_display_info *dpyinfo;
10690 {
10691 delete_keyboard_wait_descriptor (dpyinfo->connection);
10692
10693 /* Discard this display from x_display_name_list and x_display_list.
10694 We can't use Fdelq because that can quit. */
10695 if (! NILP (x_display_name_list)
10696 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
10697 x_display_name_list = XCDR (x_display_name_list);
10698 else
10699 {
10700 Lisp_Object tail;
10701
10702 tail = x_display_name_list;
10703 while (CONSP (tail) && CONSP (XCDR (tail)))
10704 {
10705 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
10706 {
10707 XSETCDR (tail, XCDR (XCDR (tail)));
10708 break;
10709 }
10710 tail = XCDR (tail);
10711 }
10712 }
10713
10714 if (next_noop_dpyinfo == dpyinfo)
10715 next_noop_dpyinfo = dpyinfo->next;
10716
10717 if (x_display_list == dpyinfo)
10718 x_display_list = dpyinfo->next;
10719 else
10720 {
10721 struct x_display_info *tail;
10722
10723 for (tail = x_display_list; tail; tail = tail->next)
10724 if (tail->next == dpyinfo)
10725 tail->next = tail->next->next;
10726 }
10727
10728 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
10729 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
10730 XrmDestroyDatabase (dpyinfo->xrdb);
10731 #endif
10732 #endif
10733 #ifdef MULTI_KBOARD
10734 if (--dpyinfo->kboard->reference_count == 0)
10735 delete_kboard (dpyinfo->kboard);
10736 #endif
10737 #ifdef HAVE_X_I18N
10738 if (dpyinfo->xim)
10739 xim_close_dpy (dpyinfo);
10740 #endif
10741
10742 xfree (dpyinfo->font_table);
10743 xfree (dpyinfo->x_id_name);
10744 xfree (dpyinfo->color_cells);
10745 xfree (dpyinfo);
10746 }
10747
10748 #ifdef USE_X_TOOLKIT
10749
10750 /* Atimer callback function for TIMER. Called every 0.1s to process
10751 Xt timeouts, if needed. We must avoid calling XtAppPending as
10752 much as possible because that function does an implicit XFlush
10753 that slows us down. */
10754
10755 static void
10756 x_process_timeouts (timer)
10757 struct atimer *timer;
10758 {
10759 if (toolkit_scroll_bar_interaction || popup_activated ())
10760 {
10761 BLOCK_INPUT;
10762 while (XtAppPending (Xt_app_con) & XtIMTimer)
10763 XtAppProcessEvent (Xt_app_con, XtIMTimer);
10764 UNBLOCK_INPUT;
10765 }
10766 }
10767
10768 #endif /* USE_X_TOOLKIT */
10769
10770 \f
10771 /* Set up use of X before we make the first connection. */
10772
10773 static struct redisplay_interface x_redisplay_interface =
10774 {
10775 x_produce_glyphs,
10776 x_write_glyphs,
10777 x_insert_glyphs,
10778 x_clear_end_of_line,
10779 x_scroll_run,
10780 x_after_update_window_line,
10781 x_update_window_begin,
10782 x_update_window_end,
10783 x_cursor_to,
10784 x_flush,
10785 #ifndef XFlush
10786 x_flush,
10787 #else
10788 0, /* flush_display_optional */
10789 #endif
10790 x_clear_window_mouse_face,
10791 x_get_glyph_overhangs,
10792 x_fix_overlapping_area,
10793 x_draw_fringe_bitmap,
10794 x_per_char_metric,
10795 x_encode_char,
10796 x_compute_glyph_string_overhangs,
10797 x_draw_glyph_string,
10798 x_define_frame_cursor,
10799 x_clear_frame_area,
10800 x_draw_window_cursor,
10801 x_draw_vertical_window_border,
10802 x_shift_glyphs_for_insert
10803 };
10804
10805 void
10806 x_initialize ()
10807 {
10808 rif = &x_redisplay_interface;
10809
10810 clear_frame_hook = x_clear_frame;
10811 ins_del_lines_hook = x_ins_del_lines;
10812 delete_glyphs_hook = x_delete_glyphs;
10813 ring_bell_hook = XTring_bell;
10814 reset_terminal_modes_hook = XTreset_terminal_modes;
10815 set_terminal_modes_hook = XTset_terminal_modes;
10816 update_begin_hook = x_update_begin;
10817 update_end_hook = x_update_end;
10818 set_terminal_window_hook = XTset_terminal_window;
10819 read_socket_hook = XTread_socket;
10820 frame_up_to_date_hook = XTframe_up_to_date;
10821 mouse_position_hook = XTmouse_position;
10822 frame_rehighlight_hook = XTframe_rehighlight;
10823 frame_raise_lower_hook = XTframe_raise_lower;
10824 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
10825 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
10826 redeem_scroll_bar_hook = XTredeem_scroll_bar;
10827 judge_scroll_bars_hook = XTjudge_scroll_bars;
10828
10829 scroll_region_ok = 1; /* we'll scroll partial frames */
10830 char_ins_del_ok = 1;
10831 line_ins_del_ok = 1; /* we'll just blt 'em */
10832 fast_clear_end_of_line = 1; /* X does this well */
10833 memory_below_frame = 0; /* we don't remember what scrolls
10834 off the bottom */
10835 baud_rate = 19200;
10836
10837 x_noop_count = 0;
10838 last_tool_bar_item = -1;
10839 any_help_event_p = 0;
10840
10841 /* Try to use interrupt input; if we can't, then start polling. */
10842 Fset_input_mode (Qt, Qnil, Qt, Qnil);
10843
10844 #ifdef USE_X_TOOLKIT
10845 XtToolkitInitialize ();
10846
10847 Xt_app_con = XtCreateApplicationContext ();
10848
10849 /* Register a converter from strings to pixels, which uses
10850 Emacs' color allocation infrastructure. */
10851 XtAppSetTypeConverter (Xt_app_con,
10852 XtRString, XtRPixel, cvt_string_to_pixel,
10853 cvt_string_to_pixel_args,
10854 XtNumber (cvt_string_to_pixel_args),
10855 XtCacheByDisplay, cvt_pixel_dtor);
10856
10857 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
10858
10859 /* Install an asynchronous timer that processes Xt timeout events
10860 every 0.1s. This is necessary because some widget sets use
10861 timeouts internally, for example the LessTif menu bar, or the
10862 Xaw3d scroll bar. When Xt timouts aren't processed, these
10863 widgets don't behave normally. */
10864 {
10865 EMACS_TIME interval;
10866 EMACS_SET_SECS_USECS (interval, 0, 100000);
10867 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
10868 }
10869 #endif
10870
10871 #ifdef USE_TOOLKIT_SCROLL_BARS
10872 #ifndef USE_GTK
10873 xaw3d_arrow_scroll = False;
10874 xaw3d_pick_top = True;
10875 #endif
10876 #endif
10877
10878 /* Note that there is no real way portable across R3/R4 to get the
10879 original error handler. */
10880 XSetErrorHandler (x_error_handler);
10881 XSetIOErrorHandler (x_io_error_quitter);
10882
10883 /* Disable Window Change signals; they are handled by X events. */
10884 #ifdef SIGWINCH
10885 signal (SIGWINCH, SIG_DFL);
10886 #endif /* SIGWINCH */
10887
10888 signal (SIGPIPE, x_connection_signal);
10889
10890 #ifdef HAVE_X_SM
10891 x_session_initialize ();
10892 #endif
10893 }
10894
10895
10896 void
10897 syms_of_xterm ()
10898 {
10899 staticpro (&x_error_message_string);
10900 x_error_message_string = Qnil;
10901
10902 staticpro (&x_display_name_list);
10903 x_display_name_list = Qnil;
10904
10905 staticpro (&last_mouse_scroll_bar);
10906 last_mouse_scroll_bar = Qnil;
10907
10908 staticpro (&Qvendor_specific_keysyms);
10909 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
10910
10911 staticpro (&Qutf_8);
10912 Qutf_8 = intern ("utf-8");
10913 staticpro (&Qlatin_1);
10914 Qlatin_1 = intern ("latin-1");
10915
10916 staticpro (&last_mouse_press_frame);
10917 last_mouse_press_frame = Qnil;
10918
10919 DEFVAR_BOOL ("x-use-underline-position-properties",
10920 &x_use_underline_position_properties,
10921 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
10922 nil means ignore them. If you encounter fonts with bogus
10923 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
10924 to 4.1, set this to nil. */);
10925 x_use_underline_position_properties = 1;
10926
10927 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
10928 doc: /* What X toolkit scroll bars Emacs uses.
10929 A value of nil means Emacs doesn't use X toolkit scroll bars.
10930 Otherwise, value is a symbol describing the X toolkit. */);
10931 #ifdef USE_TOOLKIT_SCROLL_BARS
10932 #ifdef USE_MOTIF
10933 Vx_toolkit_scroll_bars = intern ("motif");
10934 #elif defined HAVE_XAW3D
10935 Vx_toolkit_scroll_bars = intern ("xaw3d");
10936 #elif USE_GTK
10937 Vx_toolkit_scroll_bars = intern ("gtk");
10938 #else
10939 Vx_toolkit_scroll_bars = intern ("xaw");
10940 #endif
10941 #else
10942 Vx_toolkit_scroll_bars = Qnil;
10943 #endif
10944
10945 staticpro (&last_mouse_motion_frame);
10946 last_mouse_motion_frame = Qnil;
10947
10948 Qmodifier_value = intern ("modifier-value");
10949 Qalt = intern ("alt");
10950 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
10951 Qhyper = intern ("hyper");
10952 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
10953 Qmeta = intern ("meta");
10954 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
10955 Qsuper = intern ("super");
10956 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
10957
10958 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
10959 doc: /* Which keys Emacs uses for the alt modifier.
10960 This should be one of the symbols `alt', `hyper', `meta', `super'.
10961 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
10962 is nil, which is the same as `alt'. */);
10963 Vx_alt_keysym = Qnil;
10964
10965 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
10966 doc: /* Which keys Emacs uses for the hyper modifier.
10967 This should be one of the symbols `alt', `hyper', `meta', `super'.
10968 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
10969 default is nil, which is the same as `hyper'. */);
10970 Vx_hyper_keysym = Qnil;
10971
10972 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
10973 doc: /* Which keys Emacs uses for the meta modifier.
10974 This should be one of the symbols `alt', `hyper', `meta', `super'.
10975 For example, `meta' means use the Meta_L and Meta_R keysyms. The
10976 default is nil, which is the same as `meta'. */);
10977 Vx_meta_keysym = Qnil;
10978
10979 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
10980 doc: /* Which keys Emacs uses for the super modifier.
10981 This should be one of the symbols `alt', `hyper', `meta', `super'.
10982 For example, `super' means use the Super_L and Super_R keysyms. The
10983 default is nil, which is the same as `super'. */);
10984 Vx_super_keysym = Qnil;
10985
10986 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
10987 doc: /* Hash table of character codes indexed by X keysym codes. */);
10988 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
10989 make_float (DEFAULT_REHASH_SIZE),
10990 make_float (DEFAULT_REHASH_THRESHOLD),
10991 Qnil, Qnil, Qnil);
10992 }
10993
10994 #endif /* HAVE_X_WINDOWS */