]> code.delx.au - gnu-emacs/blob - src/macterm.c
2002-08-25 Andrew Choi <akochoi@shaw.ca>
[gnu-emacs] / src / macterm.c
1 /* Implementation of GUI terminal on the Mac OS.
2 Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 /* Contributed by Andrew Choi (akochoi@mac.com). */
22
23 #include <config.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include "lisp.h"
28 #include "charset.h"
29 #include "blockinput.h"
30
31 #include "macterm.h"
32
33 #ifndef MAC_OSX
34 #include <alloca.h>
35 #endif
36
37 #ifdef MAC_OSX
38 #undef mktime
39 #undef DEBUG
40 #undef free
41 #undef malloc
42 #undef realloc
43 /* Macros max and min defined in lisp.h conflict with those in
44 precompiled header Carbon.h. */
45 #undef max
46 #undef min
47 #undef init_process
48 #include <Carbon/Carbon.h>
49 #undef free
50 #define free unexec_free
51 #undef malloc
52 #define malloc unexec_malloc
53 #undef realloc
54 #define realloc unexec_realloc
55 #undef min
56 #define min(a, b) ((a) < (b) ? (a) : (b))
57 #undef max
58 #define max(a, b) ((a) > (b) ? (a) : (b))
59 #undef init_process
60 #define init_process emacs_init_process
61 /* USE_CARBON_EVENTS determines if the Carbon Event Manager is used to
62 obtain events from the event queue. If set to 0, WaitNextEvent is
63 used instead. */
64 #define USE_CARBON_EVENTS 1
65 #else /* not MAC_OSX */
66 #include <Quickdraw.h>
67 #include <ToolUtils.h>
68 #include <Sound.h>
69 #include <Events.h>
70 #include <Script.h>
71 #include <Resources.h>
72 #include <Fonts.h>
73 #include <TextUtils.h>
74 #include <LowMem.h>
75 #include <Controls.h>
76 #if defined (__MRC__) || (__MSL__ >= 0x6000)
77 #include <ControlDefinitions.h>
78 #endif
79 #include <Gestalt.h>
80
81 #if __profile__
82 #include <profiler.h>
83 #endif
84 #endif /* not MAC_OSX */
85
86 #include "systty.h"
87 #include "systime.h"
88 #include "atimer.h"
89 #include "keymap.h"
90
91 #include <ctype.h>
92 #include <errno.h>
93 #include <setjmp.h>
94 #include <sys/stat.h>
95
96 #include "keyboard.h"
97 #include "frame.h"
98 #include "dispextern.h"
99 #include "fontset.h"
100 #include "termhooks.h"
101 #include "termopts.h"
102 #include "termchar.h"
103 #include "gnu.h"
104 #include "disptab.h"
105 #include "buffer.h"
106 #include "window.h"
107 #include "intervals.h"
108 #include "composite.h"
109 #include "coding.h"
110
111 #define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
112
113 /* Set of macros that handle mapping of Mac modifier keys to emacs. */
114 #define macCtrlKey (NILP (Vmac_reverse_ctrl_meta) ? controlKey : \
115 (NILP (Vmac_command_key_is_meta) ? optionKey : cmdKey))
116 #define macShiftKey (shiftKey)
117 #define macMetaKey (NILP (Vmac_reverse_ctrl_meta) ? \
118 (NILP (Vmac_command_key_is_meta) ? optionKey : cmdKey) \
119 : controlKey)
120 #define macAltKey (NILP (Vmac_command_key_is_meta) ? cmdKey : optionKey)
121
122 \f
123 /* Fringe bitmaps. */
124
125 enum fringe_bitmap_type
126 {
127 NO_FRINGE_BITMAP,
128 LEFT_TRUNCATION_BITMAP,
129 RIGHT_TRUNCATION_BITMAP,
130 OVERLAY_ARROW_BITMAP,
131 CONTINUED_LINE_BITMAP,
132 CONTINUATION_LINE_BITMAP,
133 ZV_LINE_BITMAP
134 };
135
136 /* Bitmap drawn to indicate lines not displaying text if
137 `indicate-empty-lines' is non-nil. */
138
139 #define zv_width 8
140 #define zv_height 72
141 #define zv_period 3
142 static unsigned char zv_bits[] = {
143 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
144 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
145 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
146 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
147 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
148 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
149 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
150 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00};
151
152 /* An arrow like this: `<-'. */
153
154 #define left_width 8
155 #define left_height 8
156 static unsigned char left_bits[] = {
157 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
158
159 /* Right truncation arrow bitmap `->'. */
160
161 #define right_width 8
162 #define right_height 8
163 static unsigned char right_bits[] = {
164 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
165
166 /* Marker for continued lines. */
167
168 #define continued_width 8
169 #define continued_height 8
170 static unsigned char continued_bits[] = {
171 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
172
173 /* Marker for continuation lines. */
174
175 #define continuation_width 8
176 #define continuation_height 8
177 static unsigned char continuation_bits[] = {
178 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
179
180 /* Overlay arrow bitmap. */
181
182 #if 0
183 /* A bomb. */
184 #define ov_width 8
185 #define ov_height 8
186 static unsigned char ov_bits[] = {
187 0x0c, 0x10, 0x3c, 0x7e, 0x5e, 0x5e, 0x46, 0x3c};
188 #else
189 /* A triangular arrow. */
190 #define ov_width 8
191 #define ov_height 8
192 static unsigned char ov_bits[] = {
193 0xc0, 0xf0, 0xf8, 0xfc, 0xfc, 0xf8, 0xf0, 0xc0};
194 #endif
195
196 extern Lisp_Object Qhelp_echo;
197
198 \f
199 /* Non-nil means Emacs uses toolkit scroll bars. */
200
201 Lisp_Object Vx_toolkit_scroll_bars;
202
203 /* If a string, XTread_socket generates an event to display that string.
204 (The display is done in read_char.) */
205
206 static Lisp_Object help_echo;
207 static Lisp_Object help_echo_window;
208 static Lisp_Object help_echo_object;
209 static int help_echo_pos;
210
211 /* Temporary variable for XTread_socket. */
212
213 static Lisp_Object previous_help_echo;
214
215 /* Non-zero means that a HELP_EVENT has been generated since Emacs
216 start. */
217
218 static int any_help_event_p;
219
220 /* Non-zero means autoselect window with the mouse cursor. */
221
222 int x_autoselect_window_p;
223
224 /* Non-zero means draw block and hollow cursor as wide as the glyph
225 under it. For example, if a block cursor is over a tab, it will be
226 drawn as wide as that tab on the display. */
227
228 int x_stretch_cursor_p;
229
230 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
231
232 int x_use_underline_position_properties;
233
234 /* This is a chain of structures for all the X displays currently in
235 use. */
236
237 struct x_display_info *x_display_list;
238
239 /* This is a list of cons cells, each of the form (NAME
240 . FONT-LIST-CACHE), one for each element of x_display_list and in
241 the same order. NAME is the name of the frame. FONT-LIST-CACHE
242 records previous values returned by x-list-fonts. */
243
244 Lisp_Object x_display_name_list;
245
246 /* This is display since Mac does not support multiple ones. */
247 struct mac_display_info one_mac_display_info;
248
249 /* Frame being updated by update_frame. This is declared in term.c.
250 This is set by update_begin and looked at by all the XT functions.
251 It is zero while not inside an update. In that case, the XT
252 functions assume that `selected_frame' is the frame to apply to. */
253
254 extern struct frame *updating_frame;
255
256 extern int waiting_for_input;
257
258 /* This is a frame waiting to be auto-raised, within XTread_socket. */
259
260 struct frame *pending_autoraise_frame;
261
262 /* Nominal cursor position -- where to draw output.
263 HPOS and VPOS are window relative glyph matrix coordinates.
264 X and Y are window relative pixel coordinates. */
265
266 struct cursor_pos output_cursor;
267
268 /* Non-zero means user is interacting with a toolkit scroll bar. */
269
270 static int toolkit_scroll_bar_interaction;
271
272 /* Mouse movement.
273
274 Formerly, we used PointerMotionHintMask (in standard_event_mask)
275 so that we would have to call XQueryPointer after each MotionNotify
276 event to ask for another such event. However, this made mouse tracking
277 slow, and there was a bug that made it eventually stop.
278
279 Simply asking for MotionNotify all the time seems to work better.
280
281 In order to avoid asking for motion events and then throwing most
282 of them away or busy-polling the server for mouse positions, we ask
283 the server for pointer motion hints. This means that we get only
284 one event per group of mouse movements. "Groups" are delimited by
285 other kinds of events (focus changes and button clicks, for
286 example), or by XQueryPointer calls; when one of these happens, we
287 get another MotionNotify event the next time the mouse moves. This
288 is at least as efficient as getting motion events when mouse
289 tracking is on, and I suspect only negligibly worse when tracking
290 is off. */
291
292 /* Where the mouse was last time we reported a mouse event. */
293
294 FRAME_PTR last_mouse_frame;
295 static Rect last_mouse_glyph;
296 static Lisp_Object last_mouse_press_frame;
297
298 /* The scroll bar in which the last X motion event occurred.
299
300 If the last X motion event occurred in a scroll bar, we set this so
301 XTmouse_position can know whether to report a scroll bar motion or
302 an ordinary motion.
303
304 If the last X motion event didn't occur in a scroll bar, we set
305 this to Qnil, to tell XTmouse_position to return an ordinary motion
306 event. */
307
308 static Lisp_Object last_mouse_scroll_bar;
309
310 /* This is a hack. We would really prefer that XTmouse_position would
311 return the time associated with the position it returns, but there
312 doesn't seem to be any way to wrest the time-stamp from the server
313 along with the position query. So, we just keep track of the time
314 of the last movement we received, and return that in hopes that
315 it's somewhat accurate. */
316
317 static Time last_mouse_movement_time;
318
319 enum mouse_tracking_type {
320 mouse_tracking_none,
321 mouse_tracking_mouse_movement,
322 mouse_tracking_scroll_bar
323 };
324
325 enum mouse_tracking_type mouse_tracking_in_progress = mouse_tracking_none;
326
327 struct scroll_bar *tracked_scroll_bar = NULL;
328
329 /* Incremented by XTread_socket whenever it really tries to read
330 events. */
331
332 #ifdef __STDC__
333 static int volatile input_signal_count;
334 #else
335 static int input_signal_count;
336 #endif
337
338 /* Used locally within XTread_socket. */
339
340 static int x_noop_count;
341
342 /* Initial values of argv and argc. */
343
344 extern char **initial_argv;
345 extern int initial_argc;
346
347 extern Lisp_Object Vcommand_line_args, Vsystem_name;
348
349 /* Tells if a window manager is present or not. */
350
351 extern Lisp_Object Vx_no_window_manager;
352
353 extern Lisp_Object Qface, Qmouse_face;
354
355 extern int errno;
356
357 /* A mask of extra modifier bits to put into every keyboard char. */
358
359 extern int extra_keyboard_modifiers;
360
361 static Lisp_Object Qvendor_specific_keysyms;
362
363 #if 0
364 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
365 #endif
366
367 extern Lisp_Object x_icon_type P_ ((struct frame *));
368
369 extern int inhibit_window_system;
370
371 #if __MRC__
372 QDGlobals qd; /* QuickDraw global information structure. */
373 #endif
374
375
376 /* Enumeration for overriding/changing the face to use for drawing
377 glyphs in x_draw_glyphs. */
378
379 enum draw_glyphs_face
380 {
381 DRAW_NORMAL_TEXT,
382 DRAW_INVERSE_VIDEO,
383 DRAW_CURSOR,
384 DRAW_MOUSE_FACE,
385 DRAW_IMAGE_RAISED,
386 DRAW_IMAGE_SUNKEN
387 };
388
389 struct frame * x_window_to_frame (struct mac_display_info *, WindowPtr);
390 struct mac_display_info *mac_display_info_for_display (Display *);
391 static void x_update_window_end P_ ((struct window *, int, int));
392 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
393 static int fast_find_position P_ ((struct window *, int, int *, int *,
394 int *, int *, Lisp_Object));
395 static int fast_find_string_pos P_ ((struct window *, int, Lisp_Object,
396 int *, int *, int *, int *, int));
397 static void set_output_cursor P_ ((struct cursor_pos *));
398 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
399 int *, int *, int *, int));
400 static void note_mode_line_highlight P_ ((struct window *, int, int));
401 static void note_mouse_highlight P_ ((struct frame *, int, int));
402 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
403 static void x_handle_tool_bar_click P_ ((struct frame *, EventRecord *));
404 static void show_mouse_face P_ ((struct x_display_info *,
405 enum draw_glyphs_face));
406 static int cursor_in_mouse_face_p P_ ((struct window *));
407 static int clear_mouse_face P_ ((struct mac_display_info *));
408 static int x_io_error_quitter P_ ((Display *));
409 int x_catch_errors P_ ((Display *));
410 void x_uncatch_errors P_ ((Display *, int));
411 void x_lower_frame P_ ((struct frame *));
412 void x_scroll_bar_clear P_ ((struct frame *));
413 int x_had_errors_p P_ ((Display *));
414 void x_wm_set_size_hint P_ ((struct frame *, long, int));
415 void x_raise_frame P_ ((struct frame *));
416 void x_set_window_size P_ ((struct frame *, int, int, int));
417 void x_wm_set_window_state P_ ((struct frame *, int));
418 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
419 void mac_initialize P_ ((void));
420 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
421 static int x_compute_min_glyph_bounds P_ ((struct frame *));
422 enum text_cursor_kinds x_specified_cursor_type P_ ((Lisp_Object, int *));
423 static void x_draw_phys_cursor_glyph P_ ((struct window *,
424 struct glyph_row *,
425 enum draw_glyphs_face));
426 static void x_update_end P_ ((struct frame *));
427 static void XTframe_up_to_date P_ ((struct frame *));
428 static void XTreassert_line_highlight P_ ((int, int));
429 static void x_change_line_highlight P_ ((int, int, int, int));
430 static void XTset_terminal_modes P_ ((void));
431 static void XTreset_terminal_modes P_ ((void));
432 static void XTcursor_to P_ ((int, int, int, int));
433 static void x_write_glyphs P_ ((struct glyph *, int));
434 static void x_clear_end_of_line P_ ((int));
435 static void x_clear_frame P_ ((void));
436 static void x_clear_cursor P_ ((struct window *));
437 static void frame_highlight P_ ((struct frame *));
438 static void frame_unhighlight P_ ((struct frame *));
439 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
440 static void XTframe_rehighlight P_ ((struct frame *));
441 static void x_frame_rehighlight P_ ((struct x_display_info *));
442 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
443 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
444 static int x_intersect_rectangles P_ ((Rect *, Rect *, Rect *));
445 static void expose_frame P_ ((struct frame *, int, int, int, int));
446 static int expose_window_tree P_ ((struct window *, Rect *));
447 static int expose_window P_ ((struct window *, Rect *));
448 static void expose_area P_ ((struct window *, struct glyph_row *,
449 Rect *, enum glyph_row_area));
450 static int expose_line P_ ((struct window *, struct glyph_row *,
451 Rect *));
452 void x_display_cursor (struct window *, int, int, int, int, int);
453 void x_update_cursor P_ ((struct frame *, int));
454 static void x_update_cursor_in_window_tree P_ ((struct window *, int));
455 static void x_update_window_cursor P_ ((struct window *, int));
456 static void x_erase_phys_cursor P_ ((struct window *));
457 void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
458 static void x_draw_fringe_bitmap P_ ((struct window *, struct glyph_row *,
459 enum fringe_bitmap_type, int left_p));
460 static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
461 GC, int));
462 static int x_phys_cursor_in_rect_p P_ ((struct window *, Rect *));
463 static void x_draw_row_fringe_bitmaps P_ ((struct window *, struct glyph_row *));
464 static void notice_overwritten_cursor P_ ((struct window *,
465 enum glyph_row_area,
466 int, int, int, int));
467 static void x_flush P_ ((struct frame *f));
468 static void x_update_begin P_ ((struct frame *));
469 static void x_update_window_begin P_ ((struct window *));
470 static void x_draw_vertical_border P_ ((struct window *));
471 static void x_after_update_window_line P_ ((struct glyph_row *));
472 static INLINE void take_vertical_position_into_account P_ ((struct it *));
473 static void x_produce_stretch_glyph P_ ((struct it *));
474
475 static void activate_scroll_bars (FRAME_PTR);
476 static void deactivate_scroll_bars (FRAME_PTR);
477
478 static int is_emacs_window (WindowPtr);
479
480 extern int image_ascent (struct image *, struct face *);
481 void x_set_offset (struct frame *, int, int, int);
482 int x_bitmap_icon (struct frame *, Lisp_Object);
483 void x_make_frame_visible (struct frame *);
484
485 extern void window_scroll (Lisp_Object, int, int, int);
486
487 /* Defined in macmenu.h. */
488 extern void menubar_selection_callback (FRAME_PTR, int);
489 extern void set_frame_menubar (FRAME_PTR, int, int);
490
491 /* X display function emulation */
492
493 /* Structure borrowed from Xlib.h to represent two-byte characters in
494 dumpglyphs. */
495
496 typedef struct {
497 unsigned char byte1;
498 unsigned char byte2;
499 } XChar2b;
500
501 static void
502 XFreePixmap (display, pixmap)
503 Display *display;
504 Pixmap pixmap;
505 {
506 PixMap *p = (PixMap *) pixmap;
507
508 xfree (p->baseAddr);
509 xfree (p);
510 }
511
512
513 /* Set foreground color for subsequent QuickDraw commands. Assume
514 graphic port has already been set. */
515
516 static void
517 mac_set_forecolor (unsigned long color)
518 {
519 RGBColor fg_color;
520
521 fg_color.red = RED_FROM_ULONG (color) * 256;
522 fg_color.green = GREEN_FROM_ULONG (color) * 256;
523 fg_color.blue = BLUE_FROM_ULONG (color) * 256;
524
525 RGBForeColor (&fg_color);
526 }
527
528
529 /* Set background color for subsequent QuickDraw commands. Assume
530 graphic port has already been set. */
531
532 static void
533 mac_set_backcolor (unsigned long color)
534 {
535 RGBColor bg_color;
536
537 bg_color.red = RED_FROM_ULONG (color) * 256;
538 bg_color.green = GREEN_FROM_ULONG (color) * 256;
539 bg_color.blue = BLUE_FROM_ULONG (color) * 256;
540
541 RGBBackColor (&bg_color);
542 }
543
544 /* Set foreground and background color for subsequent QuickDraw
545 commands. Assume that the graphic port has already been set. */
546
547 static void
548 mac_set_colors (GC gc)
549 {
550 mac_set_forecolor (gc->foreground);
551 mac_set_backcolor (gc->background);
552 }
553
554 /* Mac version of XDrawLine. */
555
556 static void
557 XDrawLine (display, w, gc, x1, y1, x2, y2)
558 Display *display;
559 WindowPtr w;
560 GC gc;
561 int x1, y1, x2, y2;
562 {
563 #if TARGET_API_MAC_CARBON
564 SetPort (GetWindowPort (w));
565 #else
566 SetPort (w);
567 #endif
568
569 mac_set_colors (gc);
570
571 MoveTo (x1, y1);
572 LineTo (x2, y2);
573 }
574
575 /* Mac version of XClearArea. */
576
577 void
578 XClearArea (display, w, x, y, width, height, exposures)
579 Display *display;
580 WindowPtr w;
581 int x, y;
582 unsigned int width, height;
583 int exposures;
584 {
585 struct mac_output *mwp = (mac_output *) GetWRefCon (w);
586 Rect r;
587 XGCValues xgc;
588
589 xgc.foreground = mwp->x_compatible.foreground_pixel;
590 xgc.background = mwp->x_compatible.background_pixel;
591
592 #if TARGET_API_MAC_CARBON
593 SetPort (GetWindowPort (w));
594 #else
595 SetPort (w);
596 #endif
597
598 mac_set_colors (&xgc);
599 SetRect (&r, x, y, x + width, y + height);
600
601 EraseRect (&r);
602 }
603
604 /* Mac version of XClearWindow. */
605
606 static void
607 XClearWindow (display, w)
608 Display *display;
609 WindowPtr w;
610 {
611 struct mac_output *mwp = (mac_output *) GetWRefCon (w);
612 XGCValues xgc;
613
614 xgc.foreground = mwp->x_compatible.foreground_pixel;
615 xgc.background = mwp->x_compatible.background_pixel;
616
617 #if TARGET_API_MAC_CARBON
618 SetPort (GetWindowPort (w));
619 #else
620 SetPort (w);
621 #endif
622
623 mac_set_colors (&xgc);
624
625 #if TARGET_API_MAC_CARBON
626 {
627 Rect r;
628
629 GetWindowPortBounds (w, &r);
630 EraseRect (&r);
631 }
632 #else /* not TARGET_API_MAC_CARBON */
633 EraseRect (&(w->portRect));
634 #endif /* not TARGET_API_MAC_CARBON */
635 }
636
637
638 /* Mac replacement for XCopyArea. */
639
640 static void
641 mac_draw_bitmap (display, w, gc, x, y, bitmap)
642 Display *display;
643 WindowPtr w;
644 GC gc;
645 int x, y;
646 BitMap *bitmap;
647 {
648 Rect r;
649
650 #if TARGET_API_MAC_CARBON
651 SetPort (GetWindowPort (w));
652 #else
653 SetPort (w);
654 #endif
655
656 mac_set_colors (gc);
657 SetRect (&r, x, y, x + bitmap->bounds.right, y + bitmap->bounds.bottom);
658
659 #if TARGET_API_MAC_CARBON
660 {
661 PixMapHandle pmh;
662
663 LockPortBits (GetWindowPort (w));
664 pmh = GetPortPixMap (GetWindowPort (w));
665 CopyBits (bitmap, (BitMap *) *pmh, &(bitmap->bounds), &r, srcCopy, 0);
666 UnlockPortBits (GetWindowPort (w));
667 }
668 #else /* not TARGET_API_MAC_CARBON */
669 CopyBits (bitmap, &(w->portBits), &(bitmap->bounds), &r, srcCopy, 0);
670 #endif /* not TARGET_API_MAC_CARBON */
671 }
672
673
674 /* Mac replacement for XSetClipRectangles. */
675
676 static void
677 mac_set_clip_rectangle (display, w, r)
678 Display *display;
679 WindowPtr w;
680 Rect *r;
681 {
682 #if TARGET_API_MAC_CARBON
683 SetPort (GetWindowPort (w));
684 #else
685 SetPort (w);
686 #endif
687
688 ClipRect (r);
689 }
690
691
692 /* Mac replacement for XSetClipMask. */
693
694 static void
695 mac_reset_clipping (display, w)
696 Display *display;
697 WindowPtr w;
698 {
699 Rect r;
700
701 #if TARGET_API_MAC_CARBON
702 SetPort (GetWindowPort (w));
703 #else
704 SetPort (w);
705 #endif
706
707 SetRect (&r, -32767, -32767, 32767, 32767);
708 ClipRect (&r);
709 }
710
711
712 /* Mac replacement for XCreateBitmapFromBitmapData. */
713
714 static void
715 mac_create_bitmap_from_bitmap_data (bitmap, bits, w, h)
716 BitMap *bitmap;
717 char *bits;
718 int w, h;
719 {
720 int bytes_per_row, i, j;
721
722 bitmap->rowBytes = (w + 15) / 16 * 2; /* must be on word boundary */
723 bitmap->baseAddr = xmalloc (bitmap->rowBytes * h);
724 if (!bitmap->baseAddr)
725 abort ();
726
727 bzero (bitmap->baseAddr, bitmap->rowBytes * h);
728 for (i = 0; i < h; i++)
729 for (j = 0; j < w; j++)
730 if (BitTst (bits, i * w + j))
731 BitSet (bitmap->baseAddr, i * bitmap->rowBytes * 8 + j);
732
733 SetRect (&(bitmap->bounds), 0, 0, w, h);
734 }
735
736
737 static void
738 mac_free_bitmap (bitmap)
739 BitMap *bitmap;
740 {
741 xfree (bitmap->baseAddr);
742 }
743
744 /* Mac replacement for XFillRectangle. */
745
746 static void
747 XFillRectangle (display, w, gc, x, y, width, height)
748 Display *display;
749 WindowPtr w;
750 GC gc;
751 int x, y;
752 unsigned int width, height;
753 {
754 Rect r;
755
756 #if TARGET_API_MAC_CARBON
757 SetPort (GetWindowPort (w));
758 #else
759 SetPort (w);
760 #endif
761
762 mac_set_colors (gc);
763 SetRect (&r, x, y, x + width, y + height);
764
765 PaintRect (&r); /* using foreground color of gc */
766 }
767
768
769 /* Mac replacement for XDrawRectangle: dest is a window. */
770
771 static void
772 mac_draw_rectangle (display, w, gc, x, y, width, height)
773 Display *display;
774 WindowPtr w;
775 GC gc;
776 int x, y;
777 unsigned int width, height;
778 {
779 Rect r;
780
781 #if TARGET_API_MAC_CARBON
782 SetPort (GetWindowPort (w));
783 #else
784 SetPort (w);
785 #endif
786
787 mac_set_colors (gc);
788 SetRect (&r, x, y, x + width + 1, y + height + 1);
789
790 FrameRect (&r); /* using foreground color of gc */
791 }
792
793
794 /* Mac replacement for XDrawRectangle: dest is a Pixmap. */
795
796 static void
797 mac_draw_rectangle_to_pixmap (display, p, gc, x, y, width, height)
798 Display *display;
799 Pixmap p;
800 GC gc;
801 int x, y;
802 unsigned int width, height;
803 {
804 #if 0 /* MAC_TODO: draw a rectangle in a PixMap */
805 Rect r;
806
807 #if TARGET_API_MAC_CARBON
808 SetPort (GetWindowPort (w));
809 #else
810 SetPort (w);
811 #endif
812
813 mac_set_colors (gc);
814 SetRect (&r, x, y, x + width, y + height);
815
816 FrameRect (&r); /* using foreground color of gc */
817 #endif /* 0 */
818 }
819
820
821 static void
822 mac_draw_string_common (display, w, gc, x, y, buf, nchars, mode,
823 bytes_per_char)
824 Display *display;
825 WindowPtr w;
826 GC gc;
827 int x, y;
828 char *buf;
829 int nchars, mode, bytes_per_char;
830 {
831 #if TARGET_API_MAC_CARBON
832 SetPort (GetWindowPort (w));
833 #else
834 SetPort (w);
835 #endif
836
837 mac_set_colors (gc);
838
839 TextFont (gc->font->mac_fontnum);
840 TextSize (gc->font->mac_fontsize);
841 TextFace (gc->font->mac_fontface);
842 TextMode (mode);
843
844 MoveTo (x, y);
845 DrawText (buf, 0, nchars * bytes_per_char);
846 }
847
848
849 /* Mac replacement for XDrawString. */
850
851 static void
852 XDrawString (display, w, gc, x, y, buf, nchars)
853 Display *display;
854 WindowPtr w;
855 GC gc;
856 int x, y;
857 char *buf;
858 int nchars;
859 {
860 mac_draw_string_common (display, w, gc, x, y, buf, nchars, srcOr, 1);
861 }
862
863
864 /* Mac replacement for XDrawString16. */
865
866 static void
867 XDrawString16 (display, w, gc, x, y, buf, nchars)
868 Display *display;
869 WindowPtr w;
870 GC gc;
871 int x, y;
872 XChar2b *buf;
873 int nchars;
874 {
875 mac_draw_string_common (display, w, gc, x, y, (char *) buf, nchars, srcOr,
876 2);
877 }
878
879
880 /* Mac replacement for XDrawImageString. */
881
882 static void
883 XDrawImageString (display, w, gc, x, y, buf, nchars)
884 Display *display;
885 WindowPtr w;
886 GC gc;
887 int x, y;
888 char *buf;
889 int nchars;
890 {
891 mac_draw_string_common (display, w, gc, x, y, buf, nchars, srcCopy, 1);
892 }
893
894
895 /* Mac replacement for XDrawString16. */
896
897 static void
898 XDrawImageString16 (display, w, gc, x, y, buf, nchars)
899 Display *display;
900 WindowPtr w;
901 GC gc;
902 int x, y;
903 XChar2b *buf;
904 int nchars;
905 {
906 mac_draw_string_common (display, w, gc, x, y, (char *) buf, nchars, srcCopy,
907 2);
908 }
909
910
911 /* Mac replacement for XCopyArea: dest must be window. */
912
913 static void
914 mac_copy_area (display, src, dest, gc, src_x, src_y, width, height, dest_x,
915 dest_y)
916 Display *display;
917 Pixmap src;
918 WindowPtr dest;
919 GC gc;
920 int src_x, src_y;
921 unsigned int width, height;
922 int dest_x, dest_y;
923 {
924 Rect src_r, dest_r;
925
926 #if TARGET_API_MAC_CARBON
927 SetPort (GetWindowPort (dest));
928 #else
929 SetPort (dest);
930 #endif
931
932 mac_set_colors (gc);
933
934 SetRect (&src_r, src_x, src_y, src_x + width, src_y + height);
935 SetRect (&dest_r, dest_x, dest_y, dest_x + width, dest_y + height);
936
937 #if TARGET_API_MAC_CARBON
938 {
939 PixMapHandle pmh;
940
941 LockPortBits (GetWindowPort (dest));
942 pmh = GetPortPixMap (GetWindowPort (dest));
943 CopyBits ((BitMap *) &src, (BitMap *) *pmh, &src_r, &dest_r, srcCopy, 0);
944 UnlockPortBits (GetWindowPort (dest));
945 }
946 #else /* not TARGET_API_MAC_CARBON */
947 CopyBits ((BitMap *) &src, &(dest->portBits), &src_r, &dest_r, srcCopy, 0);
948 #endif /* not TARGET_API_MAC_CARBON */
949 }
950
951
952 #if 0
953 /* Convert a pair of local coordinates to global (screen) coordinates.
954 Assume graphic port has been properly set. */
955 static void
956 local_to_global_coord (short *h, short *v)
957 {
958 Point p;
959
960 p.h = *h;
961 p.v = *v;
962
963 LocalToGlobal (&p);
964
965 *h = p.h;
966 *v = p.v;
967 }
968 #endif
969
970 /* Mac replacement for XCopyArea: used only for scrolling. */
971
972 static void
973 mac_scroll_area (display, w, gc, src_x, src_y, width, height, dest_x, dest_y)
974 Display *display;
975 WindowPtr w;
976 GC gc;
977 int src_x, src_y;
978 unsigned int width, height;
979 int dest_x, dest_y;
980 {
981 #if TARGET_API_MAC_CARBON
982 Rect gw_r, src_r, dest_r;
983 PixMapHandle pmh;
984
985 SetRect (&src_r, src_x, src_y, src_x + width, src_y + height);
986 SetRect (&dest_r, dest_x, dest_y, dest_x + width, dest_y + height);
987
988 SetPort (GetWindowPort (w));
989
990 ForeColor (blackColor);
991 BackColor (whiteColor);
992
993 LockPortBits (GetWindowPort (w));
994 pmh = GetPortPixMap (GetWindowPort (w));
995 CopyBits ((BitMap *) *pmh, (BitMap *) *pmh, &src_r, &dest_r, srcCopy, 0);
996 UnlockPortBits (GetWindowPort (w));
997
998 mac_set_colors (gc);
999 #else /* not TARGET_API_MAC_CARBON */
1000 Rect src_r, dest_r;
1001
1002 SetPort (w);
1003 #if 0
1004 mac_set_colors (gc);
1005 #endif
1006
1007 SetRect (&src_r, src_x, src_y, src_x + width, src_y + height);
1008 SetRect (&dest_r, dest_x, dest_y, dest_x + width, dest_y + height);
1009
1010 #if 0
1011 /* Need to use global coordinates and screenBits since src and dest
1012 areas overlap in general. */
1013 local_to_global_coord (&src_r.left, &src_r.top);
1014 local_to_global_coord (&src_r.right, &src_r.bottom);
1015 local_to_global_coord (&dest_r.left, &dest_r.top);
1016 local_to_global_coord (&dest_r.right, &dest_r.bottom);
1017
1018 CopyBits (&qd.screenBits, &qd.screenBits, &src_r, &dest_r, srcCopy, 0);
1019 #else
1020 /* In Color QuickDraw, set ForeColor and BackColor as follows to avoid
1021 color mapping in CopyBits. Otherwise, it will be slow. */
1022 ForeColor (blackColor);
1023 BackColor (whiteColor);
1024 CopyBits (&(w->portBits), &(w->portBits), &src_r, &dest_r, srcCopy, 0);
1025
1026 mac_set_colors (gc);
1027 #endif
1028 #endif /* not TARGET_API_MAC_CARBON */
1029 }
1030
1031
1032 /* Mac replacement for XCopyArea: dest must be Pixmap. */
1033
1034 static void
1035 mac_copy_area_to_pixmap (display, src, dest, gc, src_x, src_y, width, height,
1036 dest_x, dest_y)
1037 Display *display;
1038 Pixmap src;
1039 Pixmap dest;
1040 GC gc;
1041 int src_x, src_y;
1042 unsigned int width, height;
1043 int dest_x, dest_y;
1044 {
1045 Rect src_r, dest_r;
1046 int src_right = ((PixMap *) src)->bounds.right;
1047 int src_bottom = ((PixMap *) src)->bounds.bottom;
1048 int w = src_right - src_x;
1049 int h = src_bottom - src_y;
1050
1051 mac_set_colors (gc);
1052
1053 SetRect (&src_r, src_x, src_y, src_right, src_bottom);
1054 SetRect (&dest_r, dest_x, dest_y, dest_x + w, dest_y + h);
1055
1056 CopyBits ((BitMap *) &src, (BitMap *) &dest, &src_r, &dest_r, srcCopy, 0);
1057 }
1058
1059
1060 /* Mac replacement for XChangeGC. */
1061
1062 static void
1063 XChangeGC (void * ignore, XGCValues* gc, unsigned long mask,
1064 XGCValues *xgcv)
1065 {
1066 if (mask & GCForeground)
1067 gc->foreground = xgcv->foreground;
1068 if (mask & GCBackground)
1069 gc->background = xgcv->background;
1070 if (mask & GCFont)
1071 gc->font = xgcv->font;
1072 }
1073
1074
1075 /* Mac replacement for XCreateGC. */
1076
1077 XGCValues *
1078 XCreateGC (void * ignore, Window window, unsigned long mask,
1079 XGCValues *xgcv)
1080 {
1081 XGCValues *gc = (XGCValues *) xmalloc (sizeof (XGCValues));
1082 bzero (gc, sizeof (XGCValues));
1083
1084 XChangeGC (ignore, gc, mask, xgcv);
1085
1086 return gc;
1087 }
1088
1089
1090 /* Used in xfaces.c. */
1091
1092 void
1093 XFreeGC (display, gc)
1094 Display *display;
1095 GC gc;
1096 {
1097 xfree (gc);
1098 }
1099
1100
1101 /* Mac replacement for XGetGCValues. */
1102
1103 static void
1104 XGetGCValues (void* ignore, XGCValues *gc,
1105 unsigned long mask, XGCValues *xgcv)
1106 {
1107 XChangeGC (ignore, xgcv, mask, gc);
1108 }
1109
1110
1111 /* Mac replacement for XSetForeground. */
1112
1113 static void
1114 XSetForeground (display, gc, color)
1115 Display *display;
1116 GC gc;
1117 unsigned long color;
1118 {
1119 gc->foreground = color;
1120 }
1121
1122
1123 /* Mac replacement for XSetFont. */
1124
1125 static void
1126 XSetFont (display, gc, font)
1127 Display *display;
1128 GC gc;
1129 XFontStruct *font;
1130 {
1131 gc->font = font;
1132 }
1133
1134
1135 static void
1136 XTextExtents16 (XFontStruct *font, XChar2b *text, int nchars,
1137 int *direction,int *font_ascent,
1138 int *font_descent, XCharStruct *cs)
1139 {
1140 /* MAC_TODO: Use GetTextMetrics to do this and inline it below. */
1141 }
1142
1143
1144 /* x_sync is a no-op on Mac. */
1145 void
1146 x_sync (f)
1147 void *f;
1148 {
1149 }
1150
1151
1152 /* Remove calls to XFlush by defining XFlush to an empty replacement.
1153 Calls to XFlush should be unnecessary because the X output buffer
1154 is flushed automatically as needed by calls to XPending,
1155 XNextEvent, or XWindowEvent according to the XFlush man page.
1156 XTread_socket calls XPending. Removing XFlush improves
1157 performance. */
1158
1159 #if TARGET_API_MAC_CARBON
1160 #define XFlush(DISPLAY) QDFlushPortBuffer (GetQDGlobalsThePort (), NULL)
1161 #else
1162 #define XFlush(DISPLAY) (void) 0
1163 #endif
1164
1165 /* Flush display of frame F, or of all frames if F is null. */
1166
1167 void
1168 x_flush (f)
1169 struct frame *f;
1170 {
1171 #if TARGET_API_MAC_CARBON
1172 BLOCK_INPUT;
1173 if (f == NULL)
1174 {
1175 Lisp_Object rest, frame;
1176 FOR_EACH_FRAME (rest, frame)
1177 x_flush (XFRAME (frame));
1178 }
1179 else if (FRAME_X_P (f))
1180 XFlush (FRAME_MAC_DISPLAY (f));
1181 UNBLOCK_INPUT;
1182 #endif /* TARGET_API_MAC_CARBON */
1183 }
1184
1185
1186 \f
1187 /* Return the struct mac_display_info corresponding to DPY. There's
1188 only one. */
1189
1190 struct mac_display_info *
1191 mac_display_info_for_display (dpy)
1192 Display *dpy;
1193 {
1194 return &one_mac_display_info;
1195 }
1196
1197
1198 \f
1199 /***********************************************************************
1200 Starting and ending an update
1201 ***********************************************************************/
1202
1203 /* Start an update of frame F. This function is installed as a hook
1204 for update_begin, i.e. it is called when update_begin is called.
1205 This function is called prior to calls to x_update_window_begin for
1206 each window being updated. */
1207
1208 static void
1209 x_update_begin (f)
1210 struct frame *f;
1211 {
1212 /* Nothing to do. */
1213 }
1214
1215
1216 /* Start update of window W. Set the global variable updated_window
1217 to the window being updated and set output_cursor to the cursor
1218 position of W. */
1219
1220 static void
1221 x_update_window_begin (w)
1222 struct window *w;
1223 {
1224 struct frame *f = XFRAME (WINDOW_FRAME (w));
1225 struct mac_display_info *display_info = FRAME_MAC_DISPLAY_INFO (f);
1226
1227 updated_window = w;
1228 set_output_cursor (&w->cursor);
1229
1230 BLOCK_INPUT;
1231
1232 if (f == display_info->mouse_face_mouse_frame)
1233 {
1234 /* Don't do highlighting for mouse motion during the update. */
1235 display_info->mouse_face_defer = 1;
1236
1237 /* If F needs to be redrawn, simply forget about any prior mouse
1238 highlighting. */
1239 if (FRAME_GARBAGED_P (f))
1240 display_info->mouse_face_window = Qnil;
1241
1242 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
1243 their mouse_face_p flag set, which means that they are always
1244 unequal to rows in a desired matrix which never have that
1245 flag set. So, rows containing mouse-face glyphs are never
1246 scrolled, and we don't have to switch the mouse highlight off
1247 here to prevent it from being scrolled. */
1248
1249 /* Can we tell that this update does not affect the window
1250 where the mouse highlight is? If so, no need to turn off.
1251 Likewise, don't do anything if the frame is garbaged;
1252 in that case, the frame's current matrix that we would use
1253 is all wrong, and we will redisplay that line anyway. */
1254 if (!NILP (display_info->mouse_face_window)
1255 && w == XWINDOW (display_info->mouse_face_window))
1256 {
1257 int i;
1258
1259 for (i = 0; i < w->desired_matrix->nrows; ++i)
1260 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
1261 break;
1262
1263 if (i < w->desired_matrix->nrows)
1264 clear_mouse_face (display_info);
1265 }
1266 #endif /* 0 */
1267 }
1268
1269 UNBLOCK_INPUT;
1270 }
1271
1272
1273 /* Draw a vertical window border to the right of window W if W doesn't
1274 have vertical scroll bars. */
1275
1276 static void
1277 x_draw_vertical_border (w)
1278 struct window *w;
1279 {
1280 struct frame *f = XFRAME (WINDOW_FRAME (w));
1281
1282 /* Redraw borders between horizontally adjacent windows. Don't
1283 do it for frames with vertical scroll bars because either the
1284 right scroll bar of a window, or the left scroll bar of its
1285 neighbor will suffice as a border. */
1286 if (!WINDOW_RIGHTMOST_P (w)
1287 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
1288 {
1289 int x0, x1, y0, y1;
1290
1291 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
1292 x1 += FRAME_X_RIGHT_FRINGE_WIDTH (f);
1293 y1 -= 1;
1294
1295 XDrawLine (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f),
1296 f->output_data.mac->normal_gc, x1, y0, x1, y1);
1297 }
1298 }
1299
1300
1301 /* End update of window W (which is equal to updated_window).
1302
1303 Draw vertical borders between horizontally adjacent windows, and
1304 display W's cursor if CURSOR_ON_P is non-zero.
1305
1306 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
1307 glyphs in mouse-face were overwritten. In that case we have to
1308 make sure that the mouse-highlight is properly redrawn.
1309
1310 W may be a menu bar pseudo-window in case we don't have X toolkit
1311 support. Such windows don't have a cursor, so don't display it
1312 here. */
1313
1314 static void
1315 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
1316 struct window *w;
1317 int cursor_on_p, mouse_face_overwritten_p;
1318 {
1319 struct mac_display_info *dpyinfo
1320 = FRAME_MAC_DISPLAY_INFO (XFRAME (w->frame));
1321
1322 if (!w->pseudo_window_p)
1323 {
1324 BLOCK_INPUT;
1325
1326 if (cursor_on_p)
1327 x_display_and_set_cursor (w, 1, output_cursor.hpos,
1328 output_cursor.vpos,
1329 output_cursor.x, output_cursor.y);
1330
1331 x_draw_vertical_border (w);
1332 UNBLOCK_INPUT;
1333 }
1334
1335 /* If a row with mouse-face was overwritten, arrange for
1336 XTframe_up_to_date to redisplay the mouse highlight. */
1337 if (mouse_face_overwritten_p)
1338 {
1339 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
1340 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
1341 dpyinfo->mouse_face_window = Qnil;
1342 }
1343
1344 #if 0
1345 /* Unhide the caret. This won't actually show the cursor, unless it
1346 was visible before the corresponding call to HideCaret in
1347 x_update_window_begin. */
1348 if (w32_use_visible_system_caret)
1349 SendMessage (w32_system_caret_hwnd, WM_EMACS_SHOW_CARET, 0, 0);
1350 #endif
1351
1352 updated_window = NULL;
1353 }
1354
1355
1356 /* End update of frame F. This function is installed as a hook in
1357 update_end. */
1358
1359 static void
1360 x_update_end (f)
1361 struct frame *f;
1362 {
1363 /* Reset the background color of Mac OS Window to that of the frame after
1364 update so that it is used by Mac Toolbox to clear the update region before
1365 an update event is generated. */
1366 #if TARGET_API_MAC_CARBON
1367 SetPort (GetWindowPort (FRAME_MAC_WINDOW (f)));
1368 #else
1369 SetPort (FRAME_MAC_WINDOW (f));
1370 #endif
1371
1372 mac_set_backcolor (FRAME_BACKGROUND_PIXEL (f));
1373
1374 /* Mouse highlight may be displayed again. */
1375 FRAME_MAC_DISPLAY_INFO (f)->mouse_face_defer = 0;
1376
1377 BLOCK_INPUT;
1378 XFlush (FRAME_MAC_DISPLAY (f));
1379 UNBLOCK_INPUT;
1380 }
1381
1382
1383 /* This function is called from various places in xdisp.c whenever a
1384 complete update has been performed. The global variable
1385 updated_window is not available here. */
1386
1387 static void
1388 XTframe_up_to_date (f)
1389 struct frame *f;
1390 {
1391 if (FRAME_X_P (f))
1392 {
1393 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
1394
1395 if (dpyinfo->mouse_face_deferred_gc
1396 || f == dpyinfo->mouse_face_mouse_frame)
1397 {
1398 BLOCK_INPUT;
1399 if (dpyinfo->mouse_face_mouse_frame)
1400 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
1401 dpyinfo->mouse_face_mouse_x,
1402 dpyinfo->mouse_face_mouse_y);
1403 dpyinfo->mouse_face_deferred_gc = 0;
1404 UNBLOCK_INPUT;
1405 }
1406 }
1407 }
1408
1409
1410 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
1411 arrow bitmaps, or clear the fringes if no bitmaps are required
1412 before DESIRED_ROW is made current. The window being updated is
1413 found in updated_window. This function is called from
1414 update_window_line only if it is known that there are differences
1415 between bitmaps to be drawn between current row and DESIRED_ROW. */
1416
1417 static void
1418 x_after_update_window_line (desired_row)
1419 struct glyph_row *desired_row;
1420 {
1421 struct window *w = updated_window;
1422 struct frame *f;
1423 int width, height;
1424
1425 xassert (w);
1426
1427 if (!desired_row->mode_line_p && !w->pseudo_window_p)
1428 {
1429 BLOCK_INPUT;
1430 x_draw_row_fringe_bitmaps (w, desired_row);
1431 UNBLOCK_INPUT;
1432 }
1433
1434 /* When a window has disappeared, make sure that no rest of
1435 full-width rows stays visible in the internal border. Could
1436 check here if updated_window is the leftmost/rightmost window,
1437 but I guess it's not worth doing since vertically split windows
1438 are almost never used, internal border is rarely set, and the
1439 overhead is very small. */
1440 if (windows_or_buffers_changed
1441 && desired_row->full_width_p
1442 && (f = XFRAME (w->frame),
1443 width = FRAME_INTERNAL_BORDER_WIDTH (f),
1444 width != 0)
1445 && (height = desired_row->visible_height,
1446 height > 0))
1447 {
1448 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
1449 /* Internal border is drawn below the tool bar. */
1450 if (WINDOWP (f->tool_bar_window)
1451 && w == XWINDOW (f->tool_bar_window))
1452 y -= width;
1453
1454 BLOCK_INPUT;
1455
1456 XClearArea (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f),
1457 0, y, width, height, 0);
1458 XClearArea (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f),
1459 f->output_data.mac->pixel_width - width, y,
1460 width, height, 0);
1461
1462 UNBLOCK_INPUT;
1463 }
1464 }
1465
1466
1467 /* Draw the bitmap WHICH in one of the left or right fringes of
1468 window W. ROW is the glyph row for which to display the bitmap; it
1469 determines the vertical position at which the bitmap has to be
1470 drawn. */
1471
1472 static void
1473 x_draw_fringe_bitmap (w, row, which, left_p)
1474 struct window *w;
1475 struct glyph_row *row;
1476 enum fringe_bitmap_type which;
1477 int left_p;
1478 {
1479 struct frame *f = XFRAME (WINDOW_FRAME (w));
1480 Display *display = FRAME_MAC_DISPLAY (f);
1481 WindowPtr window = FRAME_MAC_WINDOW (f);
1482 int x, y, wd, h, dy;
1483 int b1, b2;
1484 unsigned char *bits;
1485 BitMap bitmap;
1486 XGCValues gcv;
1487 GC gc = f->output_data.mac->normal_gc;
1488 struct face *face;
1489
1490 /* Must clip because of partially visible lines. */
1491 x_clip_to_row (w, row, gc, 1);
1492
1493 /* Convert row to frame coordinates. */
1494 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
1495
1496 switch (which)
1497 {
1498 case NO_FRINGE_BITMAP:
1499 wd = 0;
1500 h = 0;
1501 break;
1502
1503 case LEFT_TRUNCATION_BITMAP:
1504 wd = left_width;
1505 h = left_height;
1506 bits = left_bits;
1507 break;
1508
1509 case OVERLAY_ARROW_BITMAP:
1510 wd = ov_width;
1511 h = ov_height;
1512 bits = ov_bits;
1513 break;
1514
1515 case RIGHT_TRUNCATION_BITMAP:
1516 wd = right_width;
1517 h = right_height;
1518 bits = right_bits;
1519 break;
1520
1521 case CONTINUED_LINE_BITMAP:
1522 wd = continued_width;
1523 h = continued_height;
1524 bits = continued_bits;
1525 break;
1526
1527 case CONTINUATION_LINE_BITMAP:
1528 wd = continuation_width;
1529 h = continuation_height;
1530 bits = continuation_bits;
1531 break;
1532
1533 case ZV_LINE_BITMAP:
1534 wd = zv_width;
1535 h = zv_height - (y % zv_period);
1536 bits = zv_bits + (y % zv_period);
1537 break;
1538
1539 default:
1540 abort ();
1541 }
1542
1543 /* Clip bitmap if too high. */
1544 if (h > row->height)
1545 h = row->height;
1546
1547 /* Set dy to the offset in the row to start drawing the bitmap. */
1548 dy = (row->height - h) / 2;
1549
1550 /* Draw the bitmap. */
1551 face = FACE_FROM_ID (f, FRINGE_FACE_ID);
1552 PREPARE_FACE_FOR_DISPLAY (f, face);
1553
1554 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
1555 the fringe. */
1556 b1 = -1;
1557 if (left_p)
1558 {
1559 if (wd > FRAME_X_LEFT_FRINGE_WIDTH (f))
1560 wd = FRAME_X_LEFT_FRINGE_WIDTH (f);
1561 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
1562 - wd
1563 - (FRAME_X_LEFT_FRINGE_WIDTH (f) - wd) / 2);
1564 if (wd < FRAME_X_LEFT_FRINGE_WIDTH (f) || row->height > h)
1565 {
1566 /* If W has a vertical border to its left, don't draw over it. */
1567 int border = ((XFASTINT (w->left) > 0
1568 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
1569 ? 1 : 0);
1570 b1 = (window_box_left (w, -1)
1571 - FRAME_X_LEFT_FRINGE_WIDTH (f)
1572 + border);
1573 b2 = (FRAME_X_LEFT_FRINGE_WIDTH (f) - border);
1574 }
1575 }
1576 else
1577 {
1578 if (wd > FRAME_X_RIGHT_FRINGE_WIDTH (f))
1579 wd = FRAME_X_RIGHT_FRINGE_WIDTH (f);
1580 x = (window_box_right (w, -1)
1581 + (FRAME_X_RIGHT_FRINGE_WIDTH (f) - wd) / 2);
1582 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
1583 the fringe. */
1584 if (wd < FRAME_X_RIGHT_FRINGE_WIDTH (f) || row->height > h)
1585 {
1586 b1 = window_box_right (w, -1);
1587 b2 = FRAME_X_RIGHT_FRINGE_WIDTH (f);
1588 }
1589 }
1590
1591 if (b1 >= 0)
1592 {
1593 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1594 XGCValues gcv;
1595 gcv.foreground = face->background;
1596
1597 #if 0 /* MAC_TODO: stipple */
1598 /* In case the same realized face is used for fringes and
1599 for something displayed in the text (e.g. face `region' on
1600 mono-displays, the fill style may have been changed to
1601 FillSolid in x_draw_glyph_string_background. */
1602 if (face->stipple)
1603 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
1604 else
1605 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
1606 #endif
1607
1608 XFillRectangle (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f),
1609 &gcv,
1610 b1,
1611 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
1612 row->y)),
1613 b2,
1614 row->visible_height);
1615
1616 #if 0 /* MAC_TODO: stipple */
1617 if (!face->stipple)
1618 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
1619 #endif
1620 }
1621
1622 if (which == NO_FRINGE_BITMAP)
1623 {
1624 mac_reset_clipping (display, window);
1625 return;
1626 }
1627
1628 mac_create_bitmap_from_bitmap_data (&bitmap, bits, wd, h);
1629 gcv.foreground = face->foreground;
1630 gcv.background = face->background;
1631
1632 mac_draw_bitmap (display, window, &gcv, x, y + dy, &bitmap);
1633
1634 mac_free_bitmap (&bitmap);
1635 mac_reset_clipping (display, window);
1636 }
1637
1638
1639 /* Draw fringe bitmaps for glyph row ROW on window W. Call this
1640 function with input blocked. */
1641
1642 static void
1643 x_draw_row_fringe_bitmaps (w, row)
1644 struct window *w;
1645 struct glyph_row *row;
1646 {
1647 struct frame *f = XFRAME (w->frame);
1648 enum fringe_bitmap_type bitmap;
1649
1650 xassert (interrupt_input_blocked);
1651
1652 /* If row is completely invisible, because of vscrolling, we
1653 don't have to draw anything. */
1654 if (row->visible_height <= 0)
1655 return;
1656
1657 if (FRAME_X_LEFT_FRINGE_WIDTH (f) != 0)
1658 {
1659 /* Decide which bitmap to draw in the left fringe. */
1660 if (row->overlay_arrow_p)
1661 bitmap = OVERLAY_ARROW_BITMAP;
1662 else if (row->truncated_on_left_p)
1663 bitmap = LEFT_TRUNCATION_BITMAP;
1664 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
1665 bitmap = CONTINUATION_LINE_BITMAP;
1666 else if (row->indicate_empty_line_p)
1667 bitmap = ZV_LINE_BITMAP;
1668 else
1669 bitmap = NO_FRINGE_BITMAP;
1670
1671 x_draw_fringe_bitmap (w, row, bitmap, 1);
1672 }
1673
1674 if (FRAME_X_RIGHT_FRINGE_WIDTH (f) != 0)
1675 {
1676 /* Decide which bitmap to draw in the right fringe. */
1677 if (row->truncated_on_right_p)
1678 bitmap = RIGHT_TRUNCATION_BITMAP;
1679 else if (row->continued_p)
1680 bitmap = CONTINUED_LINE_BITMAP;
1681 else if (row->indicate_empty_line_p && FRAME_X_LEFT_FRINGE_WIDTH (f) == 0)
1682 bitmap = ZV_LINE_BITMAP;
1683 else
1684 bitmap = NO_FRINGE_BITMAP;
1685
1686 x_draw_fringe_bitmap (w, row, bitmap, 0);
1687 }
1688 }
1689
1690 \f
1691 /* This is called when starting Emacs and when restarting after
1692 suspend. When starting Emacs, no window is mapped. And nothing
1693 must be done to Emacs's own window if it is suspended (though that
1694 rarely happens). */
1695
1696 static void
1697 XTset_terminal_modes ()
1698 {
1699 }
1700
1701 /* This is called when exiting or suspending Emacs. Exiting will make
1702 the windows go away, and suspending requires no action. */
1703
1704 static void
1705 XTreset_terminal_modes ()
1706 {
1707 }
1708
1709
1710 \f
1711 /***********************************************************************
1712 Output Cursor
1713 ***********************************************************************/
1714
1715 /* Set the global variable output_cursor to CURSOR. All cursor
1716 positions are relative to updated_window. */
1717
1718 static void
1719 set_output_cursor (cursor)
1720 struct cursor_pos *cursor;
1721 {
1722 output_cursor.hpos = cursor->hpos;
1723 output_cursor.vpos = cursor->vpos;
1724 output_cursor.x = cursor->x;
1725 output_cursor.y = cursor->y;
1726 }
1727
1728
1729 /* Set a nominal cursor position.
1730
1731 HPOS and VPOS are column/row positions in a window glyph matrix. X
1732 and Y are window text area relative pixel positions.
1733
1734 If this is done during an update, updated_window will contain the
1735 window that is being updated and the position is the future output
1736 cursor position for that window. If updated_window is null, use
1737 selected_window and display the cursor at the given position. */
1738
1739 static void
1740 XTcursor_to (vpos, hpos, y, x)
1741 int vpos, hpos, y, x;
1742 {
1743 struct window *w;
1744
1745 /* If updated_window is not set, work on selected_window. */
1746 if (updated_window)
1747 w = updated_window;
1748 else
1749 w = XWINDOW (selected_window);
1750
1751 /* Set the output cursor. */
1752 output_cursor.hpos = hpos;
1753 output_cursor.vpos = vpos;
1754 output_cursor.x = x;
1755 output_cursor.y = y;
1756
1757 /* If not called as part of an update, really display the cursor.
1758 This will also set the cursor position of W. */
1759 if (updated_window == NULL)
1760 {
1761 BLOCK_INPUT;
1762 x_display_cursor (w, 1, hpos, vpos, x, y);
1763 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
1764 UNBLOCK_INPUT;
1765 }
1766 }
1767
1768
1769 \f
1770 /***********************************************************************
1771 Display Iterator
1772 ***********************************************************************/
1773
1774 /* Function prototypes of this page. */
1775
1776 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1777 struct glyph *,
1778 XChar2b *,
1779 int *));
1780 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1781 int, XChar2b *, int));
1782 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1783 static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1784 static void x_append_glyph P_ ((struct it *));
1785 static void x_append_composite_glyph P_ ((struct it *));
1786 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1787 int, int, double));
1788 static void x_produce_glyphs P_ ((struct it *));
1789 static void x_produce_image_glyph P_ ((struct it *it));
1790
1791
1792 /* Return a pointer to per-char metric information in FONT of a
1793 character pointed by B which is a pointer to an XChar2b. */
1794
1795 #define PER_CHAR_METRIC(font, b) \
1796 ((font)->per_char \
1797 ? ((font)->per_char + (b)->byte2 - (font)->min_char_or_byte2 \
1798 + (((font)->min_byte1 || (font)->max_byte1) \
1799 ? (((b)->byte1 - (font)->min_byte1) \
1800 * ((font)->max_char_or_byte2 - (font)->min_char_or_byte2 + 1)) \
1801 : 0)) \
1802 : &((font)->max_bounds))
1803
1804
1805 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1806 is not contained in the font. */
1807
1808 static INLINE XCharStruct *
1809 x_per_char_metric (font, char2b)
1810 XFontStruct *font;
1811 XChar2b *char2b;
1812 {
1813 /* The result metric information. */
1814 XCharStruct *pcm = NULL;
1815
1816 xassert (font && char2b);
1817
1818 if (font->per_char != NULL)
1819 {
1820 if (font->min_byte1 == 0 && font->max_byte1 == 0)
1821 {
1822 /* min_char_or_byte2 specifies the linear character index
1823 corresponding to the first element of the per_char array,
1824 max_char_or_byte2 is the index of the last character. A
1825 character with non-zero CHAR2B->byte1 is not in the font.
1826 A character with byte2 less than min_char_or_byte2 or
1827 greater max_char_or_byte2 is not in the font. */
1828 if (char2b->byte1 == 0
1829 && char2b->byte2 >= font->min_char_or_byte2
1830 && char2b->byte2 <= font->max_char_or_byte2)
1831 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
1832 }
1833 else
1834 {
1835 /* If either min_byte1 or max_byte1 are nonzero, both
1836 min_char_or_byte2 and max_char_or_byte2 are less than
1837 256, and the 2-byte character index values corresponding
1838 to the per_char array element N (counting from 0) are:
1839
1840 byte1 = N/D + min_byte1
1841 byte2 = N\D + min_char_or_byte2
1842
1843 where:
1844
1845 D = max_char_or_byte2 - min_char_or_byte2 + 1
1846 / = integer division
1847 \ = integer modulus */
1848 if (char2b->byte1 >= font->min_byte1
1849 && char2b->byte1 <= font->max_byte1
1850 && char2b->byte2 >= font->min_char_or_byte2
1851 && char2b->byte2 <= font->max_char_or_byte2)
1852 {
1853 pcm = (font->per_char
1854 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1855 * (char2b->byte1 - font->min_byte1))
1856 + (char2b->byte2 - font->min_char_or_byte2));
1857 }
1858 }
1859 }
1860 else
1861 {
1862 /* If the per_char pointer is null, all glyphs between the first
1863 and last character indexes inclusive have the same
1864 information, as given by both min_bounds and max_bounds. */
1865 if (char2b->byte2 >= font->min_char_or_byte2
1866 && char2b->byte2 <= font->max_char_or_byte2)
1867 pcm = &font->max_bounds;
1868 }
1869
1870 return ((pcm == NULL
1871 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
1872 ? NULL : pcm);
1873 }
1874
1875
1876 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1877 the two-byte form of C. Encoding is returned in *CHAR2B. */
1878
1879 static INLINE void
1880 x_encode_char (c, char2b, font_info)
1881 int c;
1882 XChar2b *char2b;
1883 struct font_info *font_info;
1884 {
1885 int charset = CHAR_CHARSET (c);
1886 XFontStruct *font = font_info->font;
1887
1888 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1889 This may be either a program in a special encoder language or a
1890 fixed encoding. */
1891 if (font_info->font_encoder)
1892 {
1893 /* It's a program. */
1894 struct ccl_program *ccl = font_info->font_encoder;
1895
1896 if (CHARSET_DIMENSION (charset) == 1)
1897 {
1898 ccl->reg[0] = charset;
1899 ccl->reg[1] = char2b->byte2;
1900 }
1901 else
1902 {
1903 ccl->reg[0] = charset;
1904 ccl->reg[1] = char2b->byte1;
1905 ccl->reg[2] = char2b->byte2;
1906 }
1907
1908 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1909
1910 /* We assume that MSBs are appropriately set/reset by CCL
1911 program. */
1912 if (font->max_byte1 == 0) /* 1-byte font */
1913 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
1914 else
1915 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1916 }
1917 else if (font_info->encoding[charset])
1918 {
1919 /* Fixed encoding scheme. See fontset.h for the meaning of the
1920 encoding numbers. */
1921 int enc = font_info->encoding[charset];
1922
1923 if ((enc == 1 || enc == 2)
1924 && CHARSET_DIMENSION (charset) == 2)
1925 char2b->byte1 |= 0x80;
1926
1927 if (enc == 1 || enc == 3)
1928 char2b->byte2 |= 0x80;
1929
1930 if (enc == 4)
1931 {
1932 int sjis1, sjis2;
1933
1934 ENCODE_SJIS (char2b->byte1, char2b->byte2, sjis1, sjis2);
1935 char2b->byte1 = sjis1;
1936 char2b->byte2 = sjis2;
1937 }
1938 }
1939 }
1940
1941
1942 /* Get face and two-byte form of character C in face FACE_ID on frame
1943 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1944 means we want to display multibyte text. Value is a pointer to a
1945 realized face that is ready for display. */
1946
1947 static INLINE struct face *
1948 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1949 struct frame *f;
1950 int c, face_id;
1951 XChar2b *char2b;
1952 int multibyte_p;
1953 {
1954 struct face *face = FACE_FROM_ID (f, face_id);
1955
1956 if (!multibyte_p)
1957 {
1958 /* Unibyte case. We don't have to encode, but we have to make
1959 sure to use a face suitable for unibyte. */
1960 char2b->byte1 = 0;
1961 char2b->byte2 = c;
1962 face_id = FACE_FOR_CHAR (f, face, c);
1963 face = FACE_FROM_ID (f, face_id);
1964 }
1965 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1966 {
1967 /* Case of ASCII in a face known to fit ASCII. */
1968 char2b->byte1 = 0;
1969 char2b->byte2 = c;
1970 }
1971 else
1972 {
1973 int c1, c2, charset;
1974
1975 /* Split characters into bytes. If c2 is -1 afterwards, C is
1976 really a one-byte character so that byte1 is zero. */
1977 SPLIT_CHAR (c, charset, c1, c2);
1978 if (c2 > 0)
1979 char2b->byte1 = c1, char2b->byte2 = c2;
1980 else
1981 char2b->byte1 = 0, char2b->byte2 = c1;
1982
1983 /* Maybe encode the character in *CHAR2B. */
1984 if (face->font != NULL)
1985 {
1986 struct font_info *font_info
1987 = FONT_INFO_FROM_ID (f, face->font_info_id);
1988 if (font_info)
1989 x_encode_char (c, char2b, font_info);
1990 }
1991 }
1992
1993 /* Make sure X resources of the face are allocated. */
1994 xassert (face != NULL);
1995 PREPARE_FACE_FOR_DISPLAY (f, face);
1996
1997 return face;
1998 }
1999
2000
2001 /* Get face and two-byte form of character glyph GLYPH on frame F.
2002 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
2003 a pointer to a realized face that is ready for display. */
2004
2005 static INLINE struct face *
2006 x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
2007 struct frame *f;
2008 struct glyph *glyph;
2009 XChar2b *char2b;
2010 int *two_byte_p;
2011 {
2012 struct face *face;
2013
2014 xassert (glyph->type == CHAR_GLYPH);
2015 face = FACE_FROM_ID (f, glyph->face_id);
2016
2017 if (two_byte_p)
2018 *two_byte_p = 0;
2019
2020 if (!glyph->multibyte_p)
2021 {
2022 /* Unibyte case. We don't have to encode, but we have to make
2023 sure to use a face suitable for unibyte. */
2024 char2b->byte1 = 0;
2025 char2b->byte2 = glyph->u.ch;
2026 }
2027 else if (glyph->u.ch < 128
2028 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
2029 {
2030 /* Case of ASCII in a face known to fit ASCII. */
2031 char2b->byte1 = 0;
2032 char2b->byte2 = glyph->u.ch;
2033 }
2034 else
2035 {
2036 int c1, c2, charset;
2037
2038 /* Split characters into bytes. If c2 is -1 afterwards, C is
2039 really a one-byte character so that byte1 is zero. */
2040 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
2041 if (c2 > 0)
2042 char2b->byte1 = c1, char2b->byte2 = c2;
2043 else
2044 char2b->byte1 = 0, char2b->byte2 = c1;
2045
2046 /* Maybe encode the character in *CHAR2B. */
2047 if (charset != CHARSET_ASCII)
2048 {
2049 struct font_info *font_info
2050 = FONT_INFO_FROM_ID (f, face->font_info_id);
2051 if (font_info)
2052 {
2053 x_encode_char (glyph->u.ch, char2b, font_info);
2054 if (two_byte_p)
2055 *two_byte_p
2056 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
2057 }
2058 }
2059 }
2060
2061 /* Make sure X resources of the face are allocated. */
2062 xassert (face != NULL);
2063 PREPARE_FACE_FOR_DISPLAY (f, face);
2064 return face;
2065 }
2066
2067
2068 /* Store one glyph for IT->char_to_display in IT->glyph_row.
2069 Called from x_produce_glyphs when IT->glyph_row is non-null. */
2070
2071 static INLINE void
2072 x_append_glyph (it)
2073 struct it *it;
2074 {
2075 struct glyph *glyph;
2076 enum glyph_row_area area = it->area;
2077
2078 xassert (it->glyph_row);
2079 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
2080
2081 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
2082 if (glyph < it->glyph_row->glyphs[area + 1])
2083 {
2084 glyph->charpos = CHARPOS (it->position);
2085 glyph->object = it->object;
2086 glyph->pixel_width = it->pixel_width;
2087 glyph->voffset = it->voffset;
2088 glyph->type = CHAR_GLYPH;
2089 glyph->multibyte_p = it->multibyte_p;
2090 glyph->left_box_line_p = it->start_of_box_run_p;
2091 glyph->right_box_line_p = it->end_of_box_run_p;
2092 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
2093 || it->phys_descent > it->descent);
2094 glyph->padding_p = 0;
2095 glyph->glyph_not_available_p = it->glyph_not_available_p;
2096 glyph->face_id = it->face_id;
2097 glyph->u.ch = it->char_to_display;
2098 ++it->glyph_row->used[area];
2099 }
2100 }
2101
2102 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
2103 Called from x_produce_glyphs when IT->glyph_row is non-null. */
2104
2105 static INLINE void
2106 x_append_composite_glyph (it)
2107 struct it *it;
2108 {
2109 struct glyph *glyph;
2110 enum glyph_row_area area = it->area;
2111
2112 xassert (it->glyph_row);
2113
2114 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
2115 if (glyph < it->glyph_row->glyphs[area + 1])
2116 {
2117 glyph->charpos = CHARPOS (it->position);
2118 glyph->object = it->object;
2119 glyph->pixel_width = it->pixel_width;
2120 glyph->voffset = it->voffset;
2121 glyph->type = COMPOSITE_GLYPH;
2122 glyph->multibyte_p = it->multibyte_p;
2123 glyph->left_box_line_p = it->start_of_box_run_p;
2124 glyph->right_box_line_p = it->end_of_box_run_p;
2125 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
2126 || it->phys_descent > it->descent);
2127 glyph->padding_p = 0;
2128 glyph->glyph_not_available_p = 0;
2129 glyph->face_id = it->face_id;
2130 glyph->u.cmp_id = it->cmp_id;
2131 ++it->glyph_row->used[area];
2132 }
2133 }
2134
2135
2136 /* Change IT->ascent and IT->height according to the setting of
2137 IT->voffset. */
2138
2139 static INLINE void
2140 take_vertical_position_into_account (it)
2141 struct it *it;
2142 {
2143 if (it->voffset)
2144 {
2145 if (it->voffset < 0)
2146 /* Increase the ascent so that we can display the text higher
2147 in the line. */
2148 it->ascent += abs (it->voffset);
2149 else
2150 /* Increase the descent so that we can display the text lower
2151 in the line. */
2152 it->descent += it->voffset;
2153 }
2154 }
2155
2156
2157 /* Produce glyphs/get display metrics for the image IT is loaded with.
2158 See the description of struct display_iterator in dispextern.h for
2159 an overview of struct display_iterator. */
2160
2161 static void
2162 x_produce_image_glyph (it)
2163 struct it *it;
2164 {
2165 struct image *img;
2166 struct face *face;
2167
2168 xassert (it->what == IT_IMAGE);
2169
2170 face = FACE_FROM_ID (it->f, it->face_id);
2171 img = IMAGE_FROM_ID (it->f, it->image_id);
2172 xassert (img);
2173
2174 /* Make sure X resources of the face and image are loaded. */
2175 PREPARE_FACE_FOR_DISPLAY (it->f, face);
2176 prepare_image_for_display (it->f, img);
2177
2178 it->ascent = it->phys_ascent = image_ascent (img, face);
2179 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
2180 it->pixel_width = img->width + 2 * img->hmargin;
2181
2182 it->nglyphs = 1;
2183
2184 if (face->box != FACE_NO_BOX)
2185 {
2186 if (face->box_line_width > 0)
2187 {
2188 it->ascent += face->box_line_width;
2189 it->descent += face->box_line_width;
2190 }
2191
2192 if (it->start_of_box_run_p)
2193 it->pixel_width += abs (face->box_line_width);
2194 if (it->end_of_box_run_p)
2195 it->pixel_width += abs (face->box_line_width);
2196 }
2197
2198 take_vertical_position_into_account (it);
2199
2200 if (it->glyph_row)
2201 {
2202 struct glyph *glyph;
2203 enum glyph_row_area area = it->area;
2204
2205 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
2206 if (glyph < it->glyph_row->glyphs[area + 1])
2207 {
2208 glyph->charpos = CHARPOS (it->position);
2209 glyph->object = it->object;
2210 glyph->pixel_width = it->pixel_width;
2211 glyph->voffset = it->voffset;
2212 glyph->type = IMAGE_GLYPH;
2213 glyph->multibyte_p = it->multibyte_p;
2214 glyph->left_box_line_p = it->start_of_box_run_p;
2215 glyph->right_box_line_p = it->end_of_box_run_p;
2216 glyph->overlaps_vertically_p = 0;
2217 glyph->padding_p = 0;
2218 glyph->glyph_not_available_p = 0;
2219 glyph->face_id = it->face_id;
2220 glyph->u.img_id = img->id;
2221 ++it->glyph_row->used[area];
2222 }
2223 }
2224 }
2225
2226
2227 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
2228 of the glyph, WIDTH and HEIGHT are the width and height of the
2229 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
2230 ascent of the glyph (0 <= ASCENT <= 1). */
2231
2232 static void
2233 x_append_stretch_glyph (it, object, width, height, ascent)
2234 struct it *it;
2235 Lisp_Object object;
2236 int width, height;
2237 double ascent;
2238 {
2239 struct glyph *glyph;
2240 enum glyph_row_area area = it->area;
2241
2242 xassert (ascent >= 0 && ascent <= 1);
2243
2244 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
2245 if (glyph < it->glyph_row->glyphs[area + 1])
2246 {
2247 glyph->charpos = CHARPOS (it->position);
2248 glyph->object = object;
2249 glyph->pixel_width = width;
2250 glyph->voffset = it->voffset;
2251 glyph->type = STRETCH_GLYPH;
2252 glyph->multibyte_p = it->multibyte_p;
2253 glyph->left_box_line_p = it->start_of_box_run_p;
2254 glyph->right_box_line_p = it->end_of_box_run_p;
2255 glyph->overlaps_vertically_p = 0;
2256 glyph->padding_p = 0;
2257 glyph->glyph_not_available_p = 0;
2258 glyph->face_id = it->face_id;
2259 glyph->u.stretch.ascent = height * ascent;
2260 glyph->u.stretch.height = height;
2261 ++it->glyph_row->used[area];
2262 }
2263 }
2264
2265
2266 /* Produce a stretch glyph for iterator IT. IT->object is the value
2267 of the glyph property displayed. The value must be a list
2268 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
2269 being recognized:
2270
2271 1. `:width WIDTH' specifies that the space should be WIDTH *
2272 canonical char width wide. WIDTH may be an integer or floating
2273 point number.
2274
2275 2. `:relative-width FACTOR' specifies that the width of the stretch
2276 should be computed from the width of the first character having the
2277 `glyph' property, and should be FACTOR times that width.
2278
2279 3. `:align-to HPOS' specifies that the space should be wide enough
2280 to reach HPOS, a value in canonical character units.
2281
2282 Exactly one of the above pairs must be present.
2283
2284 4. `:height HEIGHT' specifies that the height of the stretch produced
2285 should be HEIGHT, measured in canonical character units.
2286
2287 5. `:relative-height FACTOR' specifies that the height of the
2288 stretch should be FACTOR times the height of the characters having
2289 the glyph property.
2290
2291 Either none or exactly one of 4 or 5 must be present.
2292
2293 6. `:ascent ASCENT' specifies that ASCENT percent of the height
2294 of the stretch should be used for the ascent of the stretch.
2295 ASCENT must be in the range 0 <= ASCENT <= 100. */
2296
2297 #define NUMVAL(X) \
2298 ((INTEGERP (X) || FLOATP (X)) \
2299 ? XFLOATINT (X) \
2300 : - 1)
2301
2302
2303 static void
2304 x_produce_stretch_glyph (it)
2305 struct it *it;
2306 {
2307 /* (space :width WIDTH :height HEIGHT. */
2308 #if GLYPH_DEBUG
2309 extern Lisp_Object Qspace;
2310 #endif
2311 extern Lisp_Object QCwidth, QCheight, QCascent;
2312 extern Lisp_Object QCrelative_width, QCrelative_height;
2313 extern Lisp_Object QCalign_to;
2314 Lisp_Object prop, plist;
2315 double width = 0, height = 0, ascent = 0;
2316 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2317 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
2318
2319 PREPARE_FACE_FOR_DISPLAY (it->f, face);
2320
2321 /* List should start with `space'. */
2322 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
2323 plist = XCDR (it->object);
2324
2325 /* Compute the width of the stretch. */
2326 if (prop = Fplist_get (plist, QCwidth),
2327 NUMVAL (prop) > 0)
2328 /* Absolute width `:width WIDTH' specified and valid. */
2329 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
2330 else if (prop = Fplist_get (plist, QCrelative_width),
2331 NUMVAL (prop) > 0)
2332 {
2333 /* Relative width `:relative-width FACTOR' specified and valid.
2334 Compute the width of the characters having the `glyph'
2335 property. */
2336 struct it it2;
2337 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
2338
2339 it2 = *it;
2340 if (it->multibyte_p)
2341 {
2342 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
2343 - IT_BYTEPOS (*it));
2344 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
2345 }
2346 else
2347 it2.c = *p, it2.len = 1;
2348
2349 it2.glyph_row = NULL;
2350 it2.what = IT_CHARACTER;
2351 x_produce_glyphs (&it2);
2352 width = NUMVAL (prop) * it2.pixel_width;
2353 }
2354 else if (prop = Fplist_get (plist, QCalign_to),
2355 NUMVAL (prop) > 0)
2356 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
2357 else
2358 /* Nothing specified -> width defaults to canonical char width. */
2359 width = CANON_X_UNIT (it->f);
2360
2361 /* Compute height. */
2362 if (prop = Fplist_get (plist, QCheight),
2363 NUMVAL (prop) > 0)
2364 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
2365 else if (prop = Fplist_get (plist, QCrelative_height),
2366 NUMVAL (prop) > 0)
2367 height = FONT_HEIGHT (font) * NUMVAL (prop);
2368 else
2369 height = FONT_HEIGHT (font);
2370
2371 /* Compute percentage of height used for ascent. If
2372 `:ascent ASCENT' is present and valid, use that. Otherwise,
2373 derive the ascent from the font in use. */
2374 if (prop = Fplist_get (plist, QCascent),
2375 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
2376 ascent = NUMVAL (prop) / 100.0;
2377 else
2378 ascent = (double) FONT_BASE (font) / FONT_HEIGHT (font);
2379
2380 if (width <= 0)
2381 width = 1;
2382 if (height <= 0)
2383 height = 1;
2384
2385 if (it->glyph_row)
2386 {
2387 Lisp_Object object = it->stack[it->sp - 1].string;
2388 if (!STRINGP (object))
2389 object = it->w->buffer;
2390 x_append_stretch_glyph (it, object, width, height, ascent);
2391 }
2392
2393 it->pixel_width = width;
2394 it->ascent = it->phys_ascent = height * ascent;
2395 it->descent = it->phys_descent = height - it->ascent;
2396 it->nglyphs = 1;
2397
2398 if (face->box != FACE_NO_BOX)
2399 {
2400 if (face->box_line_width > 0)
2401 {
2402 it->ascent += face->box_line_width;
2403 it->descent += face->box_line_width;
2404 }
2405
2406 if (it->start_of_box_run_p)
2407 it->pixel_width += abs (face->box_line_width);
2408 if (it->end_of_box_run_p)
2409 it->pixel_width += abs (face->box_line_width);
2410 }
2411
2412 take_vertical_position_into_account (it);
2413 }
2414
2415 /* Return proper value to be used as baseline offset of font that has
2416 ASCENT and DESCENT to draw characters by the font at the vertical
2417 center of the line of frame F.
2418
2419 Here, out task is to find the value of BOFF in the following figure;
2420
2421 -------------------------+-----------+-
2422 -+-+---------+-+ | |
2423 | | | | | |
2424 | | | | F_ASCENT F_HEIGHT
2425 | | | ASCENT | |
2426 HEIGHT | | | | |
2427 | | |-|-+------+-----------|------- baseline
2428 | | | | BOFF | |
2429 | |---------|-+-+ | |
2430 | | | DESCENT | |
2431 -+-+---------+-+ F_DESCENT |
2432 -------------------------+-----------+-
2433
2434 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
2435 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
2436 DESCENT = FONT->descent
2437 HEIGHT = FONT_HEIGHT (FONT)
2438 F_DESCENT = (F->output_data.x->font->descent
2439 - F->output_data.x->baseline_offset)
2440 F_HEIGHT = FRAME_LINE_HEIGHT (F)
2441 */
2442
2443 #define VCENTER_BASELINE_OFFSET(FONT, F) \
2444 (FONT_DESCENT (FONT) \
2445 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
2446 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
2447 - (FONT_DESCENT (FRAME_FONT (F)) - FRAME_BASELINE_OFFSET (F)))
2448
2449 /* Produce glyphs/get display metrics for the display element IT is
2450 loaded with. See the description of struct display_iterator in
2451 dispextern.h for an overview of struct display_iterator. */
2452
2453 static void
2454 x_produce_glyphs (it)
2455 struct it *it;
2456 {
2457 it->glyph_not_available_p = 0;
2458
2459 if (it->what == IT_CHARACTER)
2460 {
2461 XChar2b char2b;
2462 XFontStruct *font;
2463 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2464 XCharStruct *pcm;
2465 int font_not_found_p;
2466 struct font_info *font_info;
2467 int boff; /* baseline offset */
2468 /* We may change it->multibyte_p upon unibyte<->multibyte
2469 conversion. So, save the current value now and restore it
2470 later.
2471
2472 Note: It seems that we don't have to record multibyte_p in
2473 struct glyph because the character code itself tells if or
2474 not the character is multibyte. Thus, in the future, we must
2475 consider eliminating the field `multibyte_p' in the struct
2476 glyph.
2477 */
2478 int saved_multibyte_p = it->multibyte_p;
2479
2480 /* Maybe translate single-byte characters to multibyte, or the
2481 other way. */
2482 it->char_to_display = it->c;
2483 if (!ASCII_BYTE_P (it->c))
2484 {
2485 if (unibyte_display_via_language_environment
2486 && SINGLE_BYTE_CHAR_P (it->c)
2487 && (it->c >= 0240
2488 || !NILP (Vnonascii_translation_table)))
2489 {
2490 it->char_to_display = unibyte_char_to_multibyte (it->c);
2491 it->multibyte_p = 1;
2492 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2493 face = FACE_FROM_ID (it->f, it->face_id);
2494 }
2495 else if (!SINGLE_BYTE_CHAR_P (it->c)
2496 && !it->multibyte_p)
2497 {
2498 it->multibyte_p = 1;
2499 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2500 face = FACE_FROM_ID (it->f, it->face_id);
2501 }
2502 }
2503
2504 /* Get font to use. Encode IT->char_to_display. */
2505 x_get_char_face_and_encoding (it->f, it->char_to_display,
2506 it->face_id, &char2b,
2507 it->multibyte_p);
2508 font = face->font;
2509
2510 /* When no suitable font found, use the default font. */
2511 font_not_found_p = font == NULL;
2512 if (font_not_found_p)
2513 {
2514 font = FRAME_FONT (it->f);
2515 boff = it->f->output_data.mac->baseline_offset;
2516 font_info = NULL;
2517 }
2518 else
2519 {
2520 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2521 boff = font_info->baseline_offset;
2522 if (font_info->vertical_centering)
2523 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2524 }
2525
2526 if (it->char_to_display >= ' '
2527 && (!it->multibyte_p || it->char_to_display < 128))
2528 {
2529 /* Either unibyte or ASCII. */
2530 int stretched_p;
2531
2532 it->nglyphs = 1;
2533
2534 pcm = x_per_char_metric (font, &char2b);
2535 it->ascent = FONT_BASE (font) + boff;
2536 it->descent = FONT_DESCENT (font) - boff;
2537
2538 if (pcm)
2539 {
2540 it->phys_ascent = pcm->ascent + boff;
2541 it->phys_descent = pcm->descent - boff;
2542 it->pixel_width = pcm->width;
2543 }
2544 else
2545 {
2546 it->glyph_not_available_p = 1;
2547 it->phys_ascent = FONT_BASE (font) + boff;
2548 it->phys_descent = FONT_DESCENT (font) - boff;
2549 it->pixel_width = FONT_WIDTH (font);
2550 }
2551
2552 /* If this is a space inside a region of text with
2553 `space-width' property, change its width. */
2554 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
2555 if (stretched_p)
2556 it->pixel_width *= XFLOATINT (it->space_width);
2557
2558 /* If face has a box, add the box thickness to the character
2559 height. If character has a box line to the left and/or
2560 right, add the box line width to the character's width. */
2561 if (face->box != FACE_NO_BOX)
2562 {
2563 int thick = face->box_line_width;
2564
2565 if (thick > 0)
2566 {
2567 it->ascent += thick;
2568 it->descent += thick;
2569 }
2570 else
2571 thick = -thick;
2572
2573 if (it->start_of_box_run_p)
2574 it->pixel_width += thick;
2575 if (it->end_of_box_run_p)
2576 it->pixel_width += thick;
2577 }
2578
2579 /* If face has an overline, add the height of the overline
2580 (1 pixel) and a 1 pixel margin to the character height. */
2581 if (face->overline_p)
2582 it->ascent += 2;
2583
2584 take_vertical_position_into_account (it);
2585
2586 /* If we have to actually produce glyphs, do it. */
2587 if (it->glyph_row)
2588 {
2589 if (stretched_p)
2590 {
2591 /* Translate a space with a `space-width' property
2592 into a stretch glyph. */
2593 double ascent = (double) FONT_BASE (font)
2594 / FONT_HEIGHT (font);
2595 x_append_stretch_glyph (it, it->object, it->pixel_width,
2596 it->ascent + it->descent, ascent);
2597 }
2598 else
2599 x_append_glyph (it);
2600
2601 /* If characters with lbearing or rbearing are displayed
2602 in this line, record that fact in a flag of the
2603 glyph row. This is used to optimize X output code. */
2604 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
2605 it->glyph_row->contains_overlapping_glyphs_p = 1;
2606 }
2607 }
2608 else if (it->char_to_display == '\n')
2609 {
2610 /* A newline has no width but we need the height of the line. */
2611 it->pixel_width = 0;
2612 it->nglyphs = 0;
2613 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
2614 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
2615
2616 if (face->box != FACE_NO_BOX
2617 && face->box_line_width > 0)
2618 {
2619 it->ascent += face->box_line_width;
2620 it->descent += face->box_line_width;
2621 }
2622 }
2623 else if (it->char_to_display == '\t')
2624 {
2625 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
2626 int x = it->current_x + it->continuation_lines_width;
2627 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
2628
2629 /* If the distance from the current position to the next tab
2630 stop is less than a canonical character width, use the
2631 tab stop after that. */
2632 if (next_tab_x - x < CANON_X_UNIT (it->f))
2633 next_tab_x += tab_width;
2634
2635 it->pixel_width = next_tab_x - x;
2636 it->nglyphs = 1;
2637 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
2638 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
2639
2640 if (it->glyph_row)
2641 {
2642 double ascent = (double) it->ascent / (it->ascent + it->descent);
2643 x_append_stretch_glyph (it, it->object, it->pixel_width,
2644 it->ascent + it->descent, ascent);
2645 }
2646 }
2647 else
2648 {
2649 /* A multi-byte character. Assume that the display width of the
2650 character is the width of the character multiplied by the
2651 width of the font. */
2652
2653 /* If we found a font, this font should give us the right
2654 metrics. If we didn't find a font, use the frame's
2655 default font and calculate the width of the character
2656 from the charset width; this is what old redisplay code
2657 did. */
2658 pcm = x_per_char_metric (font, &char2b);
2659 if (font_not_found_p || !pcm)
2660 {
2661 int charset = CHAR_CHARSET (it->char_to_display);
2662
2663 it->glyph_not_available_p = 1;
2664 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
2665 * CHARSET_WIDTH (charset));
2666 it->phys_ascent = FONT_BASE (font) + boff;
2667 it->phys_descent = FONT_DESCENT (font) - boff;
2668 }
2669 else
2670 {
2671 it->pixel_width = pcm->width;
2672 it->phys_ascent = pcm->ascent + boff;
2673 it->phys_descent = pcm->descent - boff;
2674 if (it->glyph_row
2675 && (pcm->lbearing < 0
2676 || pcm->rbearing > pcm->width))
2677 it->glyph_row->contains_overlapping_glyphs_p = 1;
2678 }
2679 it->nglyphs = 1;
2680 it->ascent = FONT_BASE (font) + boff;
2681 it->descent = FONT_DESCENT (font) - boff;
2682 if (face->box != FACE_NO_BOX)
2683 {
2684 int thick = face->box_line_width;
2685
2686 if (thick > 0)
2687 {
2688 it->ascent += thick;
2689 it->descent += thick;
2690 }
2691 else
2692 thick = - thick;
2693
2694 if (it->start_of_box_run_p)
2695 it->pixel_width += thick;
2696 if (it->end_of_box_run_p)
2697 it->pixel_width += thick;
2698 }
2699
2700 /* If face has an overline, add the height of the overline
2701 (1 pixel) and a 1 pixel margin to the character height. */
2702 if (face->overline_p)
2703 it->ascent += 2;
2704
2705 take_vertical_position_into_account (it);
2706
2707 if (it->glyph_row)
2708 x_append_glyph (it);
2709 }
2710 it->multibyte_p = saved_multibyte_p;
2711 }
2712 else if (it->what == IT_COMPOSITION)
2713 {
2714 /* Note: A composition is represented as one glyph in the
2715 glyph matrix. There are no padding glyphs. */
2716 XChar2b char2b;
2717 XFontStruct *font;
2718 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2719 XCharStruct *pcm;
2720 int font_not_found_p;
2721 struct font_info *font_info;
2722 int boff; /* baseline offset */
2723 struct composition *cmp = composition_table[it->cmp_id];
2724
2725 /* Maybe translate single-byte characters to multibyte. */
2726 it->char_to_display = it->c;
2727 if (unibyte_display_via_language_environment
2728 && SINGLE_BYTE_CHAR_P (it->c)
2729 && (it->c >= 0240
2730 || (it->c >= 0200
2731 && !NILP (Vnonascii_translation_table))))
2732 {
2733 it->char_to_display = unibyte_char_to_multibyte (it->c);
2734 }
2735
2736 /* Get face and font to use. Encode IT->char_to_display. */
2737 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2738 face = FACE_FROM_ID (it->f, it->face_id);
2739 x_get_char_face_and_encoding (it->f, it->char_to_display,
2740 it->face_id, &char2b, it->multibyte_p);
2741 font = face->font;
2742
2743 /* When no suitable font found, use the default font. */
2744 font_not_found_p = font == NULL;
2745 if (font_not_found_p)
2746 {
2747 font = FRAME_FONT (it->f);
2748 boff = it->f->output_data.mac->baseline_offset;
2749 font_info = NULL;
2750 }
2751 else
2752 {
2753 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2754 boff = font_info->baseline_offset;
2755 if (font_info->vertical_centering)
2756 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2757 }
2758
2759 /* There are no padding glyphs, so there is only one glyph to
2760 produce for the composition. Important is that pixel_width,
2761 ascent and descent are the values of what is drawn by
2762 draw_glyphs (i.e. the values of the overall glyphs composed). */
2763 it->nglyphs = 1;
2764
2765 /* If we have not yet calculated pixel size data of glyphs of
2766 the composition for the current face font, calculate them
2767 now. Theoretically, we have to check all fonts for the
2768 glyphs, but that requires much time and memory space. So,
2769 here we check only the font of the first glyph. This leads
2770 to incorrect display very rarely, and C-l (recenter) can
2771 correct the display anyway. */
2772 if (cmp->font != (void *) font)
2773 {
2774 /* Ascent and descent of the font of the first character of
2775 this composition (adjusted by baseline offset). Ascent
2776 and descent of overall glyphs should not be less than
2777 them respectively. */
2778 int font_ascent = FONT_BASE (font) + boff;
2779 int font_descent = FONT_DESCENT (font) - boff;
2780 /* Bounding box of the overall glyphs. */
2781 int leftmost, rightmost, lowest, highest;
2782 int i, width, ascent, descent;
2783
2784 cmp->font = (void *) font;
2785
2786 /* Initialize the bounding box. */
2787 pcm = x_per_char_metric (font, &char2b);
2788 if (pcm)
2789 {
2790 width = pcm->width;
2791 ascent = pcm->ascent;
2792 descent = pcm->descent;
2793 }
2794 else
2795 {
2796 width = FONT_WIDTH (font);
2797 ascent = FONT_BASE (font);
2798 descent = FONT_DESCENT (font);
2799 }
2800
2801 rightmost = width;
2802 lowest = - descent + boff;
2803 highest = ascent + boff;
2804 leftmost = 0;
2805
2806 if (font_info
2807 && font_info->default_ascent
2808 && CHAR_TABLE_P (Vuse_default_ascent)
2809 && !NILP (Faref (Vuse_default_ascent,
2810 make_number (it->char_to_display))))
2811 highest = font_info->default_ascent + boff;
2812
2813 /* Draw the first glyph at the normal position. It may be
2814 shifted to right later if some other glyphs are drawn at
2815 the left. */
2816 cmp->offsets[0] = 0;
2817 cmp->offsets[1] = boff;
2818
2819 /* Set cmp->offsets for the remaining glyphs. */
2820 for (i = 1; i < cmp->glyph_len; i++)
2821 {
2822 int left, right, btm, top;
2823 int ch = COMPOSITION_GLYPH (cmp, i);
2824 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2825
2826 face = FACE_FROM_ID (it->f, face_id);
2827 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2828 it->multibyte_p);
2829 font = face->font;
2830 if (font == NULL)
2831 {
2832 font = FRAME_FONT (it->f);
2833 boff = it->f->output_data.mac->baseline_offset;
2834 font_info = NULL;
2835 }
2836 else
2837 {
2838 font_info
2839 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2840 boff = font_info->baseline_offset;
2841 if (font_info->vertical_centering)
2842 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2843 }
2844
2845 pcm = x_per_char_metric (font, &char2b);
2846 if (pcm)
2847 {
2848 width = pcm->width;
2849 ascent = pcm->ascent;
2850 descent = pcm->descent;
2851 }
2852 else
2853 {
2854 width = FONT_WIDTH (font);
2855 ascent = 1;
2856 descent = 0;
2857 }
2858
2859 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2860 {
2861 /* Relative composition with or without
2862 alternate chars. */
2863 left = (leftmost + rightmost - width) / 2;
2864 btm = - descent + boff;
2865 if (font_info && font_info->relative_compose
2866 && (! CHAR_TABLE_P (Vignore_relative_composition)
2867 || NILP (Faref (Vignore_relative_composition,
2868 make_number (ch)))))
2869 {
2870
2871 if (- descent >= font_info->relative_compose)
2872 /* One extra pixel between two glyphs. */
2873 btm = highest + 1;
2874 else if (ascent <= 0)
2875 /* One extra pixel between two glyphs. */
2876 btm = lowest - 1 - ascent - descent;
2877 }
2878 }
2879 else
2880 {
2881 /* A composition rule is specified by an integer
2882 value that encodes global and new reference
2883 points (GREF and NREF). GREF and NREF are
2884 specified by numbers as below:
2885
2886 0---1---2 -- ascent
2887 | |
2888 | |
2889 | |
2890 9--10--11 -- center
2891 | |
2892 ---3---4---5--- baseline
2893 | |
2894 6---7---8 -- descent
2895 */
2896 int rule = COMPOSITION_RULE (cmp, i);
2897 int gref, nref, grefx, grefy, nrefx, nrefy;
2898
2899 COMPOSITION_DECODE_RULE (rule, gref, nref);
2900 grefx = gref % 3, nrefx = nref % 3;
2901 grefy = gref / 3, nrefy = nref / 3;
2902
2903 left = (leftmost
2904 + grefx * (rightmost - leftmost) / 2
2905 - nrefx * width / 2);
2906 btm = ((grefy == 0 ? highest
2907 : grefy == 1 ? 0
2908 : grefy == 2 ? lowest
2909 : (highest + lowest) / 2)
2910 - (nrefy == 0 ? ascent + descent
2911 : nrefy == 1 ? descent - boff
2912 : nrefy == 2 ? 0
2913 : (ascent + descent) / 2));
2914 }
2915
2916 cmp->offsets[i * 2] = left;
2917 cmp->offsets[i * 2 + 1] = btm + descent;
2918
2919 /* Update the bounding box of the overall glyphs. */
2920 right = left + width;
2921 top = btm + descent + ascent;
2922 if (left < leftmost)
2923 leftmost = left;
2924 if (right > rightmost)
2925 rightmost = right;
2926 if (top > highest)
2927 highest = top;
2928 if (btm < lowest)
2929 lowest = btm;
2930 }
2931
2932 /* If there are glyphs whose x-offsets are negative,
2933 shift all glyphs to the right and make all x-offsets
2934 non-negative. */
2935 if (leftmost < 0)
2936 {
2937 for (i = 0; i < cmp->glyph_len; i++)
2938 cmp->offsets[i * 2] -= leftmost;
2939 rightmost -= leftmost;
2940 }
2941
2942 cmp->pixel_width = rightmost;
2943 cmp->ascent = highest;
2944 cmp->descent = - lowest;
2945 if (cmp->ascent < font_ascent)
2946 cmp->ascent = font_ascent;
2947 if (cmp->descent < font_descent)
2948 cmp->descent = font_descent;
2949 }
2950
2951 it->pixel_width = cmp->pixel_width;
2952 it->ascent = it->phys_ascent = cmp->ascent;
2953 it->descent = it->phys_descent = cmp->descent;
2954
2955 if (face->box != FACE_NO_BOX)
2956 {
2957 int thick = face->box_line_width;
2958
2959 if (thick > 0)
2960 {
2961 it->ascent += thick;
2962 it->descent += thick;
2963 }
2964 else
2965 thick = - thick;
2966
2967 if (it->start_of_box_run_p)
2968 it->pixel_width += thick;
2969 if (it->end_of_box_run_p)
2970 it->pixel_width += thick;
2971 }
2972
2973 /* If face has an overline, add the height of the overline
2974 (1 pixel) and a 1 pixel margin to the character height. */
2975 if (face->overline_p)
2976 it->ascent += 2;
2977
2978 take_vertical_position_into_account (it);
2979
2980 if (it->glyph_row)
2981 x_append_composite_glyph (it);
2982 }
2983 else if (it->what == IT_IMAGE)
2984 x_produce_image_glyph (it);
2985 else if (it->what == IT_STRETCH)
2986 x_produce_stretch_glyph (it);
2987
2988 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2989 because this isn't true for images with `:ascent 100'. */
2990 xassert (it->ascent >= 0 && it->descent >= 0);
2991 if (it->area == TEXT_AREA)
2992 it->current_x += it->pixel_width;
2993
2994 it->descent += it->extra_line_spacing;
2995
2996 it->max_ascent = max (it->max_ascent, it->ascent);
2997 it->max_descent = max (it->max_descent, it->descent);
2998 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2999 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
3000 }
3001
3002
3003 /* Estimate the pixel height of the mode or top line on frame F.
3004 FACE_ID specifies what line's height to estimate. */
3005
3006 int
3007 x_estimate_mode_line_height (f, face_id)
3008 struct frame *f;
3009 enum face_id face_id;
3010 {
3011 int height = FONT_HEIGHT (FRAME_FONT (f));
3012
3013 /* This function is called so early when Emacs starts that the face
3014 cache and mode line face are not yet initialized. */
3015 if (FRAME_FACE_CACHE (f))
3016 {
3017 struct face *face = FACE_FROM_ID (f, face_id);
3018 if (face)
3019 {
3020 if (face->font)
3021 height = FONT_HEIGHT (face->font);
3022 if (face->box_line_width > 0)
3023 height += 2 * face->box_line_width;
3024 }
3025 }
3026
3027 return height;
3028 }
3029
3030 \f
3031 /***********************************************************************
3032 Glyph display
3033 ***********************************************************************/
3034
3035 /* A sequence of glyphs to be drawn in the same face.
3036
3037 This data structure is not really completely X specific, so it
3038 could possibly, at least partially, be useful for other systems. It
3039 is currently not part of the external redisplay interface because
3040 it's not clear what other systems will need. */
3041
3042 struct glyph_string
3043 {
3044 /* X-origin of the string. */
3045 int x;
3046
3047 /* Y-origin and y-position of the base line of this string. */
3048 int y, ybase;
3049
3050 /* The width of the string, not including a face extension. */
3051 int width;
3052
3053 /* The width of the string, including a face extension. */
3054 int background_width;
3055
3056 /* The height of this string. This is the height of the line this
3057 string is drawn in, and can be different from the height of the
3058 font the string is drawn in. */
3059 int height;
3060
3061 /* Number of pixels this string overwrites in front of its x-origin.
3062 This number is zero if the string has an lbearing >= 0; it is
3063 -lbearing, if the string has an lbearing < 0. */
3064 int left_overhang;
3065
3066 /* Number of pixels this string overwrites past its right-most
3067 nominal x-position, i.e. x + width. Zero if the string's
3068 rbearing is <= its nominal width, rbearing - width otherwise. */
3069 int right_overhang;
3070
3071 /* The frame on which the glyph string is drawn. */
3072 struct frame *f;
3073
3074 /* The window on which the glyph string is drawn. */
3075 struct window *w;
3076
3077 /* X display and window for convenience. */
3078 Display *display;
3079 Window window;
3080
3081 /* The glyph row for which this string was built. It determines the
3082 y-origin and height of the string. */
3083 struct glyph_row *row;
3084
3085 /* The area within row. */
3086 enum glyph_row_area area;
3087
3088 /* Characters to be drawn, and number of characters. */
3089 XChar2b *char2b;
3090 int nchars;
3091
3092 /* A face-override for drawing cursors, mouse face and similar. */
3093 enum draw_glyphs_face hl;
3094
3095 /* Face in which this string is to be drawn. */
3096 struct face *face;
3097
3098 /* Font in which this string is to be drawn. */
3099 XFontStruct *font;
3100
3101 /* Font info for this string. */
3102 struct font_info *font_info;
3103
3104 /* Non-null means this string describes (part of) a composition.
3105 All characters from char2b are drawn composed. */
3106 struct composition *cmp;
3107
3108 /* Index of this glyph string's first character in the glyph
3109 definition of CMP. If this is zero, this glyph string describes
3110 the first character of a composition. */
3111 int gidx;
3112
3113 /* 1 means this glyph strings face has to be drawn to the right end
3114 of the window's drawing area. */
3115 unsigned extends_to_end_of_line_p : 1;
3116
3117 /* 1 means the background of this string has been drawn. */
3118 unsigned background_filled_p : 1;
3119
3120 /* 1 means glyph string must be drawn with 16-bit functions. */
3121 unsigned two_byte_p : 1;
3122
3123 /* 1 means that the original font determined for drawing this glyph
3124 string could not be loaded. The member `font' has been set to
3125 the frame's default font in this case. */
3126 unsigned font_not_found_p : 1;
3127
3128 /* 1 means that the face in which this glyph string is drawn has a
3129 stipple pattern. */
3130 unsigned stippled_p : 1;
3131
3132 /* 1 means only the foreground of this glyph string must be drawn,
3133 and we should use the physical height of the line this glyph
3134 string appears in as clip rect. */
3135 unsigned for_overlaps_p : 1;
3136
3137 /* The GC to use for drawing this glyph string. */
3138 GC gc;
3139
3140 /* A pointer to the first glyph in the string. This glyph
3141 corresponds to char2b[0]. Needed to draw rectangles if
3142 font_not_found_p is 1. */
3143 struct glyph *first_glyph;
3144
3145 /* Image, if any. */
3146 struct image *img;
3147
3148 struct glyph_string *next, *prev;
3149 };
3150
3151
3152 #if 0
3153
3154 static void
3155 x_dump_glyph_string (s)
3156 struct glyph_string *s;
3157 {
3158 fprintf (stderr, "glyph string\n");
3159 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
3160 s->x, s->y, s->width, s->height);
3161 fprintf (stderr, " ybase = %d\n", s->ybase);
3162 fprintf (stderr, " hl = %d\n", s->hl);
3163 fprintf (stderr, " left overhang = %d, right = %d\n",
3164 s->left_overhang, s->right_overhang);
3165 fprintf (stderr, " nchars = %d\n", s->nchars);
3166 fprintf (stderr, " extends to end of line = %d\n",
3167 s->extends_to_end_of_line_p);
3168 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
3169 fprintf (stderr, " bg width = %d\n", s->background_width);
3170 }
3171
3172 #endif /* GLYPH_DEBUG */
3173
3174
3175
3176 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
3177 struct glyph_string **,
3178 struct glyph_string *,
3179 struct glyph_string *));
3180 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
3181 struct glyph_string **,
3182 struct glyph_string *,
3183 struct glyph_string *));
3184 static void x_append_glyph_string P_ ((struct glyph_string **,
3185 struct glyph_string **,
3186 struct glyph_string *));
3187 static int x_left_overwritten P_ ((struct glyph_string *));
3188 static int x_left_overwriting P_ ((struct glyph_string *));
3189 static int x_right_overwritten P_ ((struct glyph_string *));
3190 static int x_right_overwriting P_ ((struct glyph_string *));
3191 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
3192 int));
3193 static void x_init_glyph_string P_ ((struct glyph_string *,
3194 XChar2b *, struct window *,
3195 struct glyph_row *,
3196 enum glyph_row_area, int,
3197 enum draw_glyphs_face));
3198 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
3199 enum glyph_row_area, int, int,
3200 enum draw_glyphs_face, int));
3201 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
3202 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
3203 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
3204 int));
3205 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
3206 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
3207 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
3208 static void x_draw_glyph_string P_ ((struct glyph_string *));
3209 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
3210 static void x_set_cursor_gc P_ ((struct glyph_string *));
3211 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
3212 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
3213 static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
3214 int *, int *));
3215 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
3216 /*static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
3217 unsigned long *, double, int));*/
3218 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
3219 double, int, unsigned long));
3220 static void x_setup_relief_colors P_ ((struct glyph_string *));
3221 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
3222 static void x_draw_image_relief P_ ((struct glyph_string *));
3223 static void x_draw_image_foreground P_ ((struct glyph_string *));
3224 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
3225 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
3226 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
3227 int, int, int));
3228 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
3229 int, int, int, int, Rect *));
3230 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
3231 int, int, int, Rect *));
3232 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
3233 enum glyph_row_area));
3234 static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
3235 struct glyph_row *,
3236 enum glyph_row_area, int, int));
3237
3238 #if GLYPH_DEBUG
3239 static void x_check_font P_ ((struct frame *, XFontStruct *));
3240 #endif
3241
3242
3243 /* Append the list of glyph strings with head H and tail T to the list
3244 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
3245
3246 static INLINE void
3247 x_append_glyph_string_lists (head, tail, h, t)
3248 struct glyph_string **head, **tail;
3249 struct glyph_string *h, *t;
3250 {
3251 if (h)
3252 {
3253 if (*head)
3254 (*tail)->next = h;
3255 else
3256 *head = h;
3257 h->prev = *tail;
3258 *tail = t;
3259 }
3260 }
3261
3262
3263 /* Prepend the list of glyph strings with head H and tail T to the
3264 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
3265 result. */
3266
3267 static INLINE void
3268 x_prepend_glyph_string_lists (head, tail, h, t)
3269 struct glyph_string **head, **tail;
3270 struct glyph_string *h, *t;
3271 {
3272 if (h)
3273 {
3274 if (*head)
3275 (*head)->prev = t;
3276 else
3277 *tail = t;
3278 t->next = *head;
3279 *head = h;
3280 }
3281 }
3282
3283
3284 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
3285 Set *HEAD and *TAIL to the resulting list. */
3286
3287 static INLINE void
3288 x_append_glyph_string (head, tail, s)
3289 struct glyph_string **head, **tail;
3290 struct glyph_string *s;
3291 {
3292 s->next = s->prev = NULL;
3293 x_append_glyph_string_lists (head, tail, s, s);
3294 }
3295
3296
3297 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
3298 face. */
3299
3300 static void
3301 x_set_cursor_gc (s)
3302 struct glyph_string *s;
3303 {
3304 if (s->font == FRAME_FONT (s->f)
3305 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
3306 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
3307 && !s->cmp)
3308 s->gc = s->f->output_data.mac->cursor_gc;
3309 else
3310 {
3311 /* Cursor on non-default face: must merge. */
3312 XGCValues xgcv;
3313 unsigned long mask;
3314
3315 xgcv.background = s->f->output_data.mac->cursor_pixel;
3316 xgcv.foreground = s->face->background;
3317
3318 /* If the glyph would be invisible, try a different foreground. */
3319 if (xgcv.foreground == xgcv.background)
3320 xgcv.foreground = s->face->foreground;
3321 if (xgcv.foreground == xgcv.background)
3322 xgcv.foreground = s->f->output_data.mac->cursor_foreground_pixel;
3323 if (xgcv.foreground == xgcv.background)
3324 xgcv.foreground = s->face->foreground;
3325
3326 /* Make sure the cursor is distinct from text in this face. */
3327 if (xgcv.background == s->face->background
3328 && xgcv.foreground == s->face->foreground)
3329 {
3330 xgcv.background = s->face->foreground;
3331 xgcv.foreground = s->face->background;
3332 }
3333
3334 IF_DEBUG (x_check_font (s->f, s->font));
3335 xgcv.font = s->font;
3336 mask = GCForeground | GCBackground | GCFont;
3337
3338 if (FRAME_MAC_DISPLAY_INFO (s->f)->scratch_cursor_gc)
3339 XChangeGC (s->display, FRAME_MAC_DISPLAY_INFO (s->f)->scratch_cursor_gc,
3340 mask, &xgcv);
3341 else
3342 FRAME_MAC_DISPLAY_INFO (s->f)->scratch_cursor_gc
3343 = XCreateGC (s->display, s->window, mask, &xgcv);
3344
3345 s->gc = FRAME_MAC_DISPLAY_INFO (s->f)->scratch_cursor_gc;
3346 }
3347 }
3348
3349
3350 /* Set up S->gc of glyph string S for drawing text in mouse face. */
3351
3352 static void
3353 x_set_mouse_face_gc (s)
3354 struct glyph_string *s;
3355 {
3356 int face_id;
3357 struct face *face;
3358
3359 /* What face has to be used last for the mouse face? */
3360 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
3361 face = FACE_FROM_ID (s->f, face_id);
3362 if (face == NULL)
3363 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
3364
3365 if (s->first_glyph->type == CHAR_GLYPH)
3366 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
3367 else
3368 face_id = FACE_FOR_CHAR (s->f, face, 0);
3369 s->face = FACE_FROM_ID (s->f, face_id);
3370 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
3371
3372 /* If font in this face is same as S->font, use it. */
3373 if (s->font == s->face->font)
3374 s->gc = s->face->gc;
3375 else
3376 {
3377 /* Otherwise construct scratch_cursor_gc with values from FACE
3378 but font FONT. */
3379 XGCValues xgcv;
3380 unsigned long mask;
3381
3382 xgcv.background = s->face->background;
3383 xgcv.foreground = s->face->foreground;
3384 IF_DEBUG (x_check_font (s->f, s->font));
3385 xgcv.font = s->font;
3386 mask = GCForeground | GCBackground | GCFont;
3387
3388 if (FRAME_MAC_DISPLAY_INFO (s->f)->scratch_cursor_gc)
3389 XChangeGC (s->display, FRAME_MAC_DISPLAY_INFO (s->f)->scratch_cursor_gc,
3390 mask, &xgcv);
3391 else
3392 FRAME_MAC_DISPLAY_INFO (s->f)->scratch_cursor_gc
3393 = XCreateGC (s->display, s->window, mask, &xgcv);
3394
3395 s->gc = FRAME_MAC_DISPLAY_INFO (s->f)->scratch_cursor_gc;
3396 }
3397
3398 xassert (s->gc != 0);
3399 }
3400
3401
3402 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
3403 Faces to use in the mode line have already been computed when the
3404 matrix was built, so there isn't much to do, here. */
3405
3406 static INLINE void
3407 x_set_mode_line_face_gc (s)
3408 struct glyph_string *s;
3409 {
3410 s->gc = s->face->gc;
3411 }
3412
3413
3414 /* Set S->gc of glyph string S for drawing that glyph string. Set
3415 S->stippled_p to a non-zero value if the face of S has a stipple
3416 pattern. */
3417
3418 static INLINE void
3419 x_set_glyph_string_gc (s)
3420 struct glyph_string *s;
3421 {
3422 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
3423
3424 if (s->hl == DRAW_NORMAL_TEXT)
3425 {
3426 s->gc = s->face->gc;
3427 s->stippled_p = s->face->stipple != 0;
3428 }
3429 else if (s->hl == DRAW_INVERSE_VIDEO)
3430 {
3431 x_set_mode_line_face_gc (s);
3432 s->stippled_p = s->face->stipple != 0;
3433 }
3434 else if (s->hl == DRAW_CURSOR)
3435 {
3436 x_set_cursor_gc (s);
3437 s->stippled_p = 0;
3438 }
3439 else if (s->hl == DRAW_MOUSE_FACE)
3440 {
3441 x_set_mouse_face_gc (s);
3442 s->stippled_p = s->face->stipple != 0;
3443 }
3444 else if (s->hl == DRAW_IMAGE_RAISED
3445 || s->hl == DRAW_IMAGE_SUNKEN)
3446 {
3447 s->gc = s->face->gc;
3448 s->stippled_p = s->face->stipple != 0;
3449 }
3450 else
3451 {
3452 s->gc = s->face->gc;
3453 s->stippled_p = s->face->stipple != 0;
3454 }
3455
3456 /* GC must have been set. */
3457 xassert (s->gc != 0);
3458 }
3459
3460
3461 /* Return in *R the clipping rectangle for glyph string S. */
3462
3463 static void
3464 x_get_glyph_string_clip_rect (s, r)
3465 struct glyph_string *s;
3466 Rect *r;
3467 {
3468 int r_height, r_width;
3469
3470 if (s->row->full_width_p)
3471 {
3472 /* Draw full-width. X coordinates are relative to S->w->left. */
3473 int canon_x = CANON_X_UNIT (s->f);
3474
3475 r->left = WINDOW_LEFT_MARGIN (s->w) * canon_x;
3476 r_width = XFASTINT (s->w->width) * canon_x;
3477
3478 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
3479 {
3480 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
3481 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
3482 r->left -= width;
3483 }
3484
3485 r->left += FRAME_INTERNAL_BORDER_WIDTH (s->f);
3486
3487 /* Unless displaying a mode or menu bar line, which are always
3488 fully visible, clip to the visible part of the row. */
3489 if (s->w->pseudo_window_p)
3490 r_height = s->row->visible_height;
3491 else
3492 r_height = s->height;
3493 }
3494 else
3495 {
3496 /* This is a text line that may be partially visible. */
3497 r->left = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
3498 r_width = window_box_width (s->w, s->area);
3499 r_height = s->row->visible_height;
3500 }
3501
3502 /* If S draws overlapping rows, it's sufficient to use the top and
3503 bottom of the window for clipping because this glyph string
3504 intentionally draws over other lines. */
3505 if (s->for_overlaps_p)
3506 {
3507 r->top = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
3508 r_height = window_text_bottom_y (s->w) - r->top;
3509 }
3510 else
3511 {
3512 /* Don't use S->y for clipping because it doesn't take partially
3513 visible lines into account. For example, it can be negative for
3514 partially visible lines at the top of a window. */
3515 if (!s->row->full_width_p
3516 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
3517 r->top = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
3518 else
3519 r->top = max (0, s->row->y);
3520
3521 /* If drawing a tool-bar window, draw it over the internal border
3522 at the top of the window. */
3523 if (s->w == XWINDOW (s->f->tool_bar_window))
3524 r->top -= s->f->output_data.mac->internal_border_width;
3525 }
3526
3527 r->top = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->top);
3528
3529 r->bottom = r->top + r_height;
3530 r->right = r->left + r_width;
3531 }
3532
3533
3534 /* Set clipping for output of glyph string S. S may be part of a mode
3535 line or menu if we don't have X toolkit support. */
3536
3537 static INLINE void
3538 x_set_glyph_string_clipping (s)
3539 struct glyph_string *s;
3540 {
3541 Rect r;
3542 x_get_glyph_string_clip_rect (s, &r);
3543 mac_set_clip_rectangle (s->display, s->window, &r);
3544 }
3545
3546
3547 /* Compute left and right overhang of glyph string S. If S is a glyph
3548 string for a composition, assume overhangs don't exist. */
3549
3550 static INLINE void
3551 x_compute_glyph_string_overhangs (s)
3552 struct glyph_string *s;
3553 {
3554 if (s->cmp == NULL
3555 && s->first_glyph->type == CHAR_GLYPH)
3556 {
3557 XCharStruct cs;
3558 int direction, font_ascent, font_descent;
3559 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
3560 &font_ascent, &font_descent, &cs);
3561 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
3562 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
3563 }
3564 }
3565
3566
3567 /* Compute overhangs and x-positions for glyph string S and its
3568 predecessors, or successors. X is the starting x-position for S.
3569 BACKWARD_P non-zero means process predecessors. */
3570
3571 static void
3572 x_compute_overhangs_and_x (s, x, backward_p)
3573 struct glyph_string *s;
3574 int x;
3575 int backward_p;
3576 {
3577 if (backward_p)
3578 {
3579 while (s)
3580 {
3581 x_compute_glyph_string_overhangs (s);
3582 x -= s->width;
3583 s->x = x;
3584 s = s->prev;
3585 }
3586 }
3587 else
3588 {
3589 while (s)
3590 {
3591 x_compute_glyph_string_overhangs (s);
3592 s->x = x;
3593 x += s->width;
3594 s = s->next;
3595 }
3596 }
3597 }
3598
3599
3600 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
3601 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
3602 assumed to be zero. */
3603
3604 void
3605 x_get_glyph_overhangs (glyph, f, left, right)
3606 struct glyph *glyph;
3607 struct frame *f;
3608 int *left, *right;
3609 {
3610 *left = *right = 0;
3611
3612 if (glyph->type == CHAR_GLYPH)
3613 {
3614 XFontStruct *font;
3615 struct face *face;
3616 struct font_info *font_info;
3617 XChar2b char2b;
3618 XCharStruct *pcm;
3619
3620 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
3621 font = face->font;
3622 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
3623 if (font
3624 && (pcm = x_per_char_metric (font, &char2b)))
3625 {
3626 if (pcm->rbearing > pcm->width)
3627 *right = pcm->rbearing - pcm->width;
3628 if (pcm->lbearing < 0)
3629 *left = -pcm->lbearing;
3630 }
3631 }
3632 }
3633
3634
3635 /* Return the index of the first glyph preceding glyph string S that
3636 is overwritten by S because of S's left overhang. Value is -1
3637 if no glyphs are overwritten. */
3638
3639 static int
3640 x_left_overwritten (s)
3641 struct glyph_string *s;
3642 {
3643 int k;
3644
3645 if (s->left_overhang)
3646 {
3647 int x = 0, i;
3648 struct glyph *glyphs = s->row->glyphs[s->area];
3649 int first = s->first_glyph - glyphs;
3650
3651 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
3652 x -= glyphs[i].pixel_width;
3653
3654 k = i + 1;
3655 }
3656 else
3657 k = -1;
3658
3659 return k;
3660 }
3661
3662
3663 /* Return the index of the first glyph preceding glyph string S that
3664 is overwriting S because of its right overhang. Value is -1 if no
3665 glyph in front of S overwrites S. */
3666
3667 static int
3668 x_left_overwriting (s)
3669 struct glyph_string *s;
3670 {
3671 int i, k, x;
3672 struct glyph *glyphs = s->row->glyphs[s->area];
3673 int first = s->first_glyph - glyphs;
3674
3675 k = -1;
3676 x = 0;
3677 for (i = first - 1; i >= 0; --i)
3678 {
3679 int left, right;
3680 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3681 if (x + right > 0)
3682 k = i;
3683 x -= glyphs[i].pixel_width;
3684 }
3685
3686 return k;
3687 }
3688
3689
3690 /* Return the index of the last glyph following glyph string S that is
3691 not overwritten by S because of S's right overhang. Value is -1 if
3692 no such glyph is found. */
3693
3694 static int
3695 x_right_overwritten (s)
3696 struct glyph_string *s;
3697 {
3698 int k = -1;
3699
3700 if (s->right_overhang)
3701 {
3702 int x = 0, i;
3703 struct glyph *glyphs = s->row->glyphs[s->area];
3704 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3705 int end = s->row->used[s->area];
3706
3707 for (i = first; i < end && s->right_overhang > x; ++i)
3708 x += glyphs[i].pixel_width;
3709
3710 k = i;
3711 }
3712
3713 return k;
3714 }
3715
3716
3717 /* Return the index of the last glyph following glyph string S that
3718 overwrites S because of its left overhang. Value is negative
3719 if no such glyph is found. */
3720
3721 static int
3722 x_right_overwriting (s)
3723 struct glyph_string *s;
3724 {
3725 int i, k, x;
3726 int end = s->row->used[s->area];
3727 struct glyph *glyphs = s->row->glyphs[s->area];
3728 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3729
3730 k = -1;
3731 x = 0;
3732 for (i = first; i < end; ++i)
3733 {
3734 int left, right;
3735 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3736 if (x - left < 0)
3737 k = i;
3738 x += glyphs[i].pixel_width;
3739 }
3740
3741 return k;
3742 }
3743
3744
3745 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
3746
3747 static INLINE void
3748 x_clear_glyph_string_rect (s, x, y, w, h)
3749 struct glyph_string *s;
3750 int x, y, w, h;
3751 {
3752 XGCValues xgcv;
3753
3754 xgcv.foreground = s->gc->background;
3755 XFillRectangle (s->display, s->window, &xgcv, x, y, w, h);
3756 }
3757
3758
3759 /* Draw the background of glyph_string S. If S->background_filled_p
3760 is non-zero don't draw it. FORCE_P non-zero means draw the
3761 background even if it wouldn't be drawn normally. This is used
3762 when a string preceding S draws into the background of S, or S
3763 contains the first component of a composition. */
3764
3765 static void
3766 x_draw_glyph_string_background (s, force_p)
3767 struct glyph_string *s;
3768 int force_p;
3769 {
3770 /* Nothing to do if background has already been drawn or if it
3771 shouldn't be drawn in the first place. */
3772 if (!s->background_filled_p)
3773 {
3774 int box_line_width = max (s->face->box_line_width, 0);
3775
3776 #if 0 /* MAC_TODO: stipple */
3777 if (s->stippled_p)
3778 {
3779 /* Fill background with a stipple pattern. */
3780 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3781 XFillRectangle (s->display, s->window, s->gc, s->x,
3782 s->y + box_line_width,
3783 s->background_width,
3784 s->height - 2 * box_line_width);
3785 XSetFillStyle (s->display, s->gc, FillSolid);
3786 s->background_filled_p = 1;
3787 }
3788 else
3789 #endif
3790 if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
3791 || s->font_not_found_p
3792 || s->extends_to_end_of_line_p
3793 || force_p)
3794 {
3795 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
3796 s->background_width,
3797 s->height - 2 * box_line_width);
3798 s->background_filled_p = 1;
3799 }
3800 }
3801 }
3802
3803
3804 /* Draw the foreground of glyph string S. */
3805
3806 static void
3807 x_draw_glyph_string_foreground (s)
3808 struct glyph_string *s;
3809 {
3810 int i, x;
3811
3812 /* If first glyph of S has a left box line, start drawing the text
3813 of S to the right of that box line. */
3814 if (s->face->box != FACE_NO_BOX
3815 && s->first_glyph->left_box_line_p)
3816 x = s->x + abs (s->face->box_line_width);
3817 else
3818 x = s->x;
3819
3820 /* Draw characters of S as rectangles if S's font could not be
3821 loaded. */
3822 if (s->font_not_found_p)
3823 {
3824 for (i = 0; i < s->nchars; ++i)
3825 {
3826 struct glyph *g = s->first_glyph + i;
3827 mac_draw_rectangle (s->display, s->window,
3828 s->gc, x, s->y, g->pixel_width - 1,
3829 s->height - 1);
3830 x += g->pixel_width;
3831 }
3832 }
3833 else
3834 {
3835 char *char1b = (char *) s->char2b;
3836 int boff = s->font_info->baseline_offset;
3837
3838 if (s->font_info->vertical_centering)
3839 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3840
3841 /* If we can use 8-bit functions, condense S->char2b. */
3842 if (!s->two_byte_p)
3843 for (i = 0; i < s->nchars; ++i)
3844 char1b[i] = s->char2b[i].byte2;
3845
3846 /* Draw text with XDrawString if background has already been
3847 filled. Otherwise, use XDrawImageString. (Note that
3848 XDrawImageString is usually faster than XDrawString.) Always
3849 use XDrawImageString when drawing the cursor so that there is
3850 no chance that characters under a box cursor are invisible. */
3851 if (s->for_overlaps_p
3852 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3853 {
3854 /* Draw characters with 16-bit or 8-bit functions. */
3855 if (s->two_byte_p)
3856 XDrawString16 (s->display, s->window, s->gc, x,
3857 s->ybase - boff, s->char2b, s->nchars);
3858 else
3859 XDrawString (s->display, s->window, s->gc, x,
3860 s->ybase - boff, char1b, s->nchars);
3861 }
3862 else
3863 {
3864 if (s->two_byte_p)
3865 XDrawImageString16 (s->display, s->window, s->gc, x,
3866 s->ybase - boff, s->char2b, s->nchars);
3867 else
3868 XDrawImageString (s->display, s->window, s->gc, x,
3869 s->ybase - boff, char1b, s->nchars);
3870 }
3871 }
3872 }
3873
3874 /* Draw the foreground of composite glyph string S. */
3875
3876 static void
3877 x_draw_composite_glyph_string_foreground (s)
3878 struct glyph_string *s;
3879 {
3880 int i, x;
3881
3882 /* If first glyph of S has a left box line, start drawing the text
3883 of S to the right of that box line. */
3884 if (s->face->box != FACE_NO_BOX
3885 && s->first_glyph->left_box_line_p)
3886 x = s->x + abs (s->face->box_line_width);
3887 else
3888 x = s->x;
3889
3890 /* S is a glyph string for a composition. S->gidx is the index of
3891 the first character drawn for glyphs of this composition.
3892 S->gidx == 0 means we are drawing the very first character of
3893 this composition. */
3894
3895 /* Draw a rectangle for the composition if the font for the very
3896 first character of the composition could not be loaded. */
3897 if (s->font_not_found_p)
3898 {
3899 if (s->gidx == 0)
3900 mac_draw_rectangle (s->display, s->window, s->gc, x, s->y,
3901 s->width - 1, s->height - 1);
3902 }
3903 else
3904 {
3905 for (i = 0; i < s->nchars; i++, ++s->gidx)
3906 XDrawString16 (s->display, s->window, s->gc,
3907 x + s->cmp->offsets[s->gidx * 2],
3908 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3909 s->char2b + i, 1);
3910 }
3911 }
3912
3913
3914 #ifdef USE_X_TOOLKIT
3915
3916 static struct frame *x_frame_of_widget P_ ((Widget));
3917
3918
3919 /* Return the frame on which widget WIDGET is used.. Abort if frame
3920 cannot be determined. */
3921
3922 static struct frame *
3923 x_frame_of_widget (widget)
3924 Widget widget;
3925 {
3926 struct x_display_info *dpyinfo;
3927 Lisp_Object tail;
3928 struct frame *f;
3929
3930 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3931
3932 /* Find the top-level shell of the widget. Note that this function
3933 can be called when the widget is not yet realized, so XtWindow
3934 (widget) == 0. That's the reason we can't simply use
3935 x_any_window_to_frame. */
3936 while (!XtIsTopLevelShell (widget))
3937 widget = XtParent (widget);
3938
3939 /* Look for a frame with that top-level widget. Allocate the color
3940 on that frame to get the right gamma correction value. */
3941 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3942 if (GC_FRAMEP (XCAR (tail))
3943 && (f = XFRAME (XCAR (tail)),
3944 (f->output_data.nothing != 1
3945 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3946 && f->output_data.x->widget == widget)
3947 return f;
3948
3949 abort ();
3950 }
3951
3952
3953 /* Allocate the color COLOR->pixel on the screen and display of
3954 widget WIDGET in colormap CMAP. If an exact match cannot be
3955 allocated, try the nearest color available. Value is non-zero
3956 if successful. This is called from lwlib. */
3957
3958 int
3959 x_alloc_nearest_color_for_widget (widget, cmap, color)
3960 Widget widget;
3961 Colormap cmap;
3962 XColor *color;
3963 {
3964 struct frame *f = x_frame_of_widget (widget);
3965 return x_alloc_nearest_color (f, cmap, color);
3966 }
3967
3968
3969 #endif /* USE_X_TOOLKIT */
3970
3971 #if 0 /* MAC_TODO */
3972
3973 /* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3974 CMAP. If an exact match can't be allocated, try the nearest color
3975 available. Value is non-zero if successful. Set *COLOR to the
3976 color allocated. */
3977
3978 int
3979 x_alloc_nearest_color (f, cmap, color)
3980 struct frame *f;
3981 Colormap cmap;
3982 XColor *color;
3983 {
3984 Display *display = FRAME_X_DISPLAY (f);
3985 Screen *screen = FRAME_X_SCREEN (f);
3986 int rc;
3987
3988 gamma_correct (f, color);
3989 rc = XAllocColor (display, cmap, color);
3990 if (rc == 0)
3991 {
3992 /* If we got to this point, the colormap is full, so we're going
3993 to try to get the next closest color. The algorithm used is
3994 a least-squares matching, which is what X uses for closest
3995 color matching with StaticColor visuals. */
3996 int nearest, i;
3997 unsigned long nearest_delta = ~0;
3998 int ncells = XDisplayCells (display, XScreenNumberOfScreen (screen));
3999 XColor *cells = (XColor *) alloca (ncells * sizeof *cells);
4000
4001 for (i = 0; i < ncells; ++i)
4002 cells[i].pixel = i;
4003 XQueryColors (display, cmap, cells, ncells);
4004
4005 for (nearest = i = 0; i < ncells; ++i)
4006 {
4007 long dred = (color->red >> 8) - (cells[i].red >> 8);
4008 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
4009 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
4010 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
4011
4012 if (delta < nearest_delta)
4013 {
4014 nearest = i;
4015 nearest_delta = delta;
4016 }
4017 }
4018
4019 color->red = cells[nearest].red;
4020 color->green = cells[nearest].green;
4021 color->blue = cells[nearest].blue;
4022 rc = XAllocColor (display, cmap, color);
4023 }
4024
4025 #ifdef DEBUG_X_COLORS
4026 if (rc)
4027 register_color (color->pixel);
4028 #endif /* DEBUG_X_COLORS */
4029
4030 return rc;
4031 }
4032
4033
4034 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
4035 It's necessary to do this instead of just using PIXEL directly to
4036 get color reference counts right. */
4037
4038 unsigned long
4039 x_copy_color (f, pixel)
4040 struct frame *f;
4041 unsigned long pixel;
4042 {
4043 XColor color;
4044
4045 color.pixel = pixel;
4046 BLOCK_INPUT;
4047 XQueryColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
4048 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
4049 UNBLOCK_INPUT;
4050 #ifdef DEBUG_X_COLORS
4051 register_color (pixel);
4052 #endif
4053 return color.pixel;
4054 }
4055
4056
4057 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
4058 It's necessary to do this instead of just using PIXEL directly to
4059 get color reference counts right. */
4060
4061 unsigned long
4062 x_copy_dpy_color (dpy, cmap, pixel)
4063 Display *dpy;
4064 Colormap cmap;
4065 unsigned long pixel;
4066 {
4067 XColor color;
4068
4069 color.pixel = pixel;
4070 BLOCK_INPUT;
4071 XQueryColor (dpy, cmap, &color);
4072 XAllocColor (dpy, cmap, &color);
4073 UNBLOCK_INPUT;
4074 #ifdef DEBUG_X_COLORS
4075 register_color (pixel);
4076 #endif
4077 return color.pixel;
4078 }
4079
4080 #endif /* MAC_TODO */
4081
4082 /* Allocate a color which is lighter or darker than *COLOR by FACTOR
4083 or DELTA. Try a color with RGB values multiplied by FACTOR first.
4084 If this produces the same color as COLOR, try a color where all RGB
4085 values have DELTA added. Return the allocated color in *COLOR.
4086 DISPLAY is the X display, CMAP is the colormap to operate on.
4087 Value is non-zero if successful. */
4088
4089 static int
4090 mac_alloc_lighter_color (f, color, factor, delta)
4091 struct frame *f;
4092 unsigned long *color;
4093 double factor;
4094 int delta;
4095 {
4096 unsigned long new;
4097
4098 /* Change RGB values by specified FACTOR. Avoid overflow! */
4099 xassert (factor >= 0);
4100 new = RGB_TO_ULONG (min (0xff, (int) (factor * RED_FROM_ULONG (*color))),
4101 min (0xff, (int) (factor * GREEN_FROM_ULONG (*color))),
4102 min (0xff, (int) (factor * BLUE_FROM_ULONG (*color))));
4103 if (new == *color)
4104 new = RGB_TO_ULONG (max (0, min (0xff, (int) (delta + RED_FROM_ULONG (*color)))),
4105 max (0, min (0xff, (int) (delta + GREEN_FROM_ULONG (*color)))),
4106 max (0, min (0xff, (int) (delta + BLUE_FROM_ULONG (*color)))));
4107
4108 /* MAC_TODO: Map to palette and retry with delta if same? */
4109 /* MAC_TODO: Free colors (if using palette)? */
4110
4111 if (new == *color)
4112 return 0;
4113
4114 *color = new;
4115
4116 return 1;
4117 }
4118
4119
4120 /* Set up the foreground color for drawing relief lines of glyph
4121 string S. RELIEF is a pointer to a struct relief containing the GC
4122 with which lines will be drawn. Use a color that is FACTOR or
4123 DELTA lighter or darker than the relief's background which is found
4124 in S->f->output_data.x->relief_background. If such a color cannot
4125 be allocated, use DEFAULT_PIXEL, instead. */
4126
4127 static void
4128 x_setup_relief_color (f, relief, factor, delta, default_pixel)
4129 struct frame *f;
4130 struct relief *relief;
4131 double factor;
4132 int delta;
4133 unsigned long default_pixel;
4134 {
4135 XGCValues xgcv;
4136 struct mac_output *di = f->output_data.mac;
4137 unsigned long mask = GCForeground;
4138 unsigned long pixel;
4139 unsigned long background = di->relief_background;
4140 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
4141
4142 /* MAC_TODO: Free colors (if using palette)? */
4143
4144 /* Allocate new color. */
4145 xgcv.foreground = default_pixel;
4146 pixel = background;
4147 if (mac_alloc_lighter_color (f, &pixel, factor, delta))
4148 {
4149 relief->allocated_p = 1;
4150 xgcv.foreground = relief->pixel = pixel;
4151 }
4152
4153 if (relief->gc == 0)
4154 {
4155 #if 0 /* MAC_TODO: stipple */
4156 xgcv.stipple = dpyinfo->gray;
4157 mask |= GCStipple;
4158 #endif
4159 relief->gc = XCreateGC (NULL, FRAME_MAC_WINDOW (f), mask, &xgcv);
4160 }
4161 else
4162 XChangeGC (NULL, relief->gc, mask, &xgcv);
4163 }
4164
4165
4166 /* Set up colors for the relief lines around glyph string S. */
4167
4168 static void
4169 x_setup_relief_colors (s)
4170 struct glyph_string *s;
4171 {
4172 struct mac_output *di = s->f->output_data.mac;
4173 unsigned long color;
4174
4175 if (s->face->use_box_color_for_shadows_p)
4176 color = s->face->box_color;
4177 else
4178 {
4179 XGCValues xgcv;
4180
4181 /* Get the background color of the face. */
4182 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
4183 color = xgcv.background;
4184 }
4185
4186 if (di->white_relief.gc == 0
4187 || color != di->relief_background)
4188 {
4189 di->relief_background = color;
4190 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
4191 WHITE_PIX_DEFAULT (s->f));
4192 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
4193 BLACK_PIX_DEFAULT (s->f));
4194 }
4195 }
4196
4197
4198 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
4199 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
4200 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
4201 relief. LEFT_P non-zero means draw a relief on the left side of
4202 the rectangle. RIGHT_P non-zero means draw a relief on the right
4203 side of the rectangle. CLIP_RECT is the clipping rectangle to use
4204 when drawing. */
4205
4206 static void
4207 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
4208 raised_p, left_p, right_p, clip_rect)
4209 struct frame *f;
4210 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
4211 Rect *clip_rect;
4212 {
4213 int i;
4214 GC gc;
4215
4216 if (raised_p)
4217 gc = f->output_data.mac->white_relief.gc;
4218 else
4219 gc = f->output_data.mac->black_relief.gc;
4220 mac_set_clip_rectangle (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f), clip_rect);
4221
4222 /* Top. */
4223 for (i = 0; i < width; ++i)
4224 XDrawLine (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f), gc,
4225 left_x + i * left_p, top_y + i,
4226 right_x + 1 - i * right_p, top_y + i);
4227
4228 /* Left. */
4229 if (left_p)
4230 for (i = 0; i < width; ++i)
4231 XDrawLine (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f), gc,
4232 left_x + i, top_y + i, left_x + i, bottom_y - i);
4233
4234 mac_reset_clipping (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f));
4235 if (raised_p)
4236 gc = f->output_data.mac->black_relief.gc;
4237 else
4238 gc = f->output_data.mac->white_relief.gc;
4239 mac_set_clip_rectangle (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f),
4240 clip_rect);
4241
4242 /* Bottom. */
4243 for (i = 0; i < width; ++i)
4244 XDrawLine (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f), gc,
4245 left_x + i * left_p, bottom_y - i,
4246 right_x + 1 - i * right_p, bottom_y - i);
4247
4248 /* Right. */
4249 if (right_p)
4250 for (i = 0; i < width; ++i)
4251 XDrawLine (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f), gc,
4252 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
4253
4254 mac_reset_clipping (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f));
4255 }
4256
4257
4258 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
4259 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
4260 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
4261 left side of the rectangle. RIGHT_P non-zero means draw a line
4262 on the right side of the rectangle. CLIP_RECT is the clipping
4263 rectangle to use when drawing. */
4264
4265 static void
4266 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
4267 left_p, right_p, clip_rect)
4268 struct glyph_string *s;
4269 int left_x, top_y, right_x, bottom_y, left_p, right_p;
4270 Rect *clip_rect;
4271 {
4272 XGCValues xgcv;
4273
4274 xgcv.foreground = s->face->box_color;
4275 mac_set_clip_rectangle (s->display, s->window, clip_rect);
4276
4277 /* Top. */
4278 XFillRectangle (s->display, s->window, &xgcv,
4279 left_x, top_y, right_x - left_x, width);
4280
4281 /* Left. */
4282 if (left_p)
4283 XFillRectangle (s->display, s->window, &xgcv,
4284 left_x, top_y, width, bottom_y - top_y);
4285
4286 /* Bottom. */
4287 XFillRectangle (s->display, s->window, &xgcv,
4288 left_x, bottom_y - width, right_x - left_x, width);
4289
4290 /* Right. */
4291 if (right_p)
4292 XFillRectangle (s->display, s->window, &xgcv,
4293 right_x - width, top_y, width, bottom_y - top_y);
4294
4295 mac_reset_clipping (s->display, s->window);
4296 }
4297
4298
4299 /* Draw a box around glyph string S. */
4300
4301 static void
4302 x_draw_glyph_string_box (s)
4303 struct glyph_string *s;
4304 {
4305 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
4306 int left_p, right_p;
4307 struct glyph *last_glyph;
4308 Rect clip_rect;
4309
4310 last_x = window_box_right (s->w, s->area);
4311 if (s->row->full_width_p
4312 && !s->w->pseudo_window_p)
4313 {
4314 last_x += FRAME_X_RIGHT_FRINGE_WIDTH (s->f);
4315 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
4316 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
4317 }
4318
4319 /* The glyph that may have a right box line. */
4320 last_glyph = (s->cmp || s->img
4321 ? s->first_glyph
4322 : s->first_glyph + s->nchars - 1);
4323
4324 width = abs (s->face->box_line_width);
4325 raised_p = s->face->box == FACE_RAISED_BOX;
4326 left_x = s->x;
4327 right_x = ((s->row->full_width_p && s->extends_to_end_of_line_p
4328 ? last_x - 1
4329 : min (last_x, s->x + s->background_width) - 1));
4330 top_y = s->y;
4331 bottom_y = top_y + s->height - 1;
4332
4333 left_p = (s->first_glyph->left_box_line_p
4334 || (s->hl == DRAW_MOUSE_FACE
4335 && (s->prev == NULL
4336 || s->prev->hl != s->hl)));
4337 right_p = (last_glyph->right_box_line_p
4338 || (s->hl == DRAW_MOUSE_FACE
4339 && (s->next == NULL
4340 || s->next->hl != s->hl)));
4341
4342 x_get_glyph_string_clip_rect (s, &clip_rect);
4343
4344 if (s->face->box == FACE_SIMPLE_BOX)
4345 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
4346 left_p, right_p, &clip_rect);
4347 else
4348 {
4349 x_setup_relief_colors (s);
4350 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
4351 width, raised_p, left_p, right_p, &clip_rect);
4352 }
4353 }
4354
4355
4356 /* Draw foreground of image glyph string S. */
4357
4358 static void
4359 x_draw_image_foreground (s)
4360 struct glyph_string *s;
4361 {
4362 int x;
4363 int y = s->ybase - image_ascent (s->img, s->face);
4364
4365 /* If first glyph of S has a left box line, start drawing it to the
4366 right of that line. */
4367 if (s->face->box != FACE_NO_BOX
4368 && s->first_glyph->left_box_line_p)
4369 x = s->x + abs (s->face->box_line_width);
4370 else
4371 x = s->x;
4372
4373 /* If there is a margin around the image, adjust x- and y-position
4374 by that margin. */
4375 x += s->img->hmargin;
4376 y += s->img->vmargin;
4377
4378 if (s->img->pixmap)
4379 {
4380 #if 0 /* MAC_TODO: image mask */
4381 if (s->img->mask)
4382 {
4383 /* We can't set both a clip mask and use XSetClipRectangles
4384 because the latter also sets a clip mask. We also can't
4385 trust on the shape extension to be available
4386 (XShapeCombineRegion). So, compute the rectangle to draw
4387 manually. */
4388 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4389 | GCFunction);
4390 XGCValues xgcv;
4391 XRectangle clip_rect, image_rect, r;
4392
4393 xgcv.clip_mask = s->img->mask;
4394 xgcv.clip_x_origin = x;
4395 xgcv.clip_y_origin = y;
4396 xgcv.function = GXcopy;
4397 XChangeGC (s->display, s->gc, mask, &xgcv);
4398
4399 x_get_glyph_string_clip_rect (s, &clip_rect);
4400 image_rect.x = x;
4401 image_rect.y = y;
4402 image_rect.width = s->img->width;
4403 image_rect.height = s->img->height;
4404 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4405 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4406 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4407 }
4408 else
4409 #endif /* MAC_TODO */
4410 {
4411 mac_copy_area (s->display, s->img->pixmap, s->window, s->gc,
4412 0, 0, s->img->width, s->img->height, x, y);
4413
4414 /* When the image has a mask, we can expect that at
4415 least part of a mouse highlight or a block cursor will
4416 be visible. If the image doesn't have a mask, make
4417 a block cursor visible by drawing a rectangle around
4418 the image. I believe it's looking better if we do
4419 nothing here for mouse-face. */
4420 if (s->hl == DRAW_CURSOR)
4421 {
4422 int r = s->img->relief;
4423 if (r < 0) r = -r;
4424 mac_draw_rectangle (s->display, s->window, s->gc, x - r, y - r,
4425 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
4426 }
4427 }
4428 }
4429 else
4430 /* Draw a rectangle if image could not be loaded. */
4431 mac_draw_rectangle (s->display, s->window, s->gc, x, y,
4432 s->img->width - 1, s->img->height - 1);
4433 }
4434
4435
4436
4437 /* Draw a relief around the image glyph string S. */
4438
4439 static void
4440 x_draw_image_relief (s)
4441 struct glyph_string *s;
4442 {
4443 int x0, y0, x1, y1, thick, raised_p;
4444 Rect r;
4445 int x;
4446 int y = s->ybase - image_ascent (s->img, s->face);
4447
4448 /* If first glyph of S has a left box line, start drawing it to the
4449 right of that line. */
4450 if (s->face->box != FACE_NO_BOX
4451 && s->first_glyph->left_box_line_p)
4452 x = s->x + abs (s->face->box_line_width);
4453 else
4454 x = s->x;
4455
4456 /* If there is a margin around the image, adjust x- and y-position
4457 by that margin. */
4458 x += s->img->hmargin;
4459 y += s->img->vmargin;
4460
4461 if (s->hl == DRAW_IMAGE_SUNKEN
4462 || s->hl == DRAW_IMAGE_RAISED)
4463 {
4464 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
4465 raised_p = s->hl == DRAW_IMAGE_RAISED;
4466 }
4467 else
4468 {
4469 thick = abs (s->img->relief);
4470 raised_p = s->img->relief > 0;
4471 }
4472
4473 x0 = x - thick;
4474 y0 = y - thick;
4475 x1 = x + s->img->width + thick - 1;
4476 y1 = y + s->img->height + thick - 1;
4477
4478 x_setup_relief_colors (s);
4479 x_get_glyph_string_clip_rect (s, &r);
4480 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
4481 }
4482
4483
4484 /* Draw the foreground of image glyph string S to PIXMAP. */
4485
4486 static void
4487 x_draw_image_foreground_1 (s, pixmap)
4488 struct glyph_string *s;
4489 Pixmap pixmap;
4490 {
4491 int x;
4492 int y = s->ybase - s->y - image_ascent (s->img, s->face);
4493
4494 /* If first glyph of S has a left box line, start drawing it to the
4495 right of that line. */
4496 if (s->face->box != FACE_NO_BOX
4497 && s->first_glyph->left_box_line_p)
4498 x = abs (s->face->box_line_width);
4499 else
4500 x = 0;
4501
4502 /* If there is a margin around the image, adjust x- and y-position
4503 by that margin. */
4504 x += s->img->hmargin;
4505 y += s->img->vmargin;
4506
4507 if (s->img->pixmap)
4508 {
4509 #if 0 /* MAC_TODO: image mask */
4510 if (s->img->mask)
4511 {
4512 /* We can't set both a clip mask and use XSetClipRectangles
4513 because the latter also sets a clip mask. We also can't
4514 trust on the shape extension to be available
4515 (XShapeCombineRegion). So, compute the rectangle to draw
4516 manually. */
4517 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4518 | GCFunction);
4519 XGCValues xgcv;
4520
4521 xgcv.clip_mask = s->img->mask;
4522 xgcv.clip_x_origin = x;
4523 xgcv.clip_y_origin = y;
4524 xgcv.function = GXcopy;
4525 XChangeGC (s->display, s->gc, mask, &xgcv);
4526
4527 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4528 0, 0, s->img->width, s->img->height, x, y);
4529 XSetClipMask (s->display, s->gc, None);
4530 }
4531 else
4532 #endif /* MAC_TODO */
4533 {
4534 mac_copy_area_to_pixmap (s->display, s->img->pixmap, pixmap, s->gc,
4535 0, 0, s->img->width, s->img->height, x, y);
4536
4537 /* When the image has a mask, we can expect that at
4538 least part of a mouse highlight or a block cursor will
4539 be visible. If the image doesn't have a mask, make
4540 a block cursor visible by drawing a rectangle around
4541 the image. I believe it's looking better if we do
4542 nothing here for mouse-face. */
4543 if (s->hl == DRAW_CURSOR)
4544 {
4545 int r = s->img->relief;
4546 if (r < 0) r = -r;
4547 mac_draw_rectangle_to_pixmap (s->display, pixmap, s->gc, x - r, y - r,
4548 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
4549 }
4550 }
4551 }
4552 else
4553 /* Draw a rectangle if image could not be loaded. */
4554 mac_draw_rectangle_to_pixmap (s->display, pixmap, s->gc, x, y,
4555 s->img->width - 1, s->img->height - 1);
4556 }
4557
4558
4559 /* Draw part of the background of glyph string S. X, Y, W, and H
4560 give the rectangle to draw. */
4561
4562 static void
4563 x_draw_glyph_string_bg_rect (s, x, y, w, h)
4564 struct glyph_string *s;
4565 int x, y, w, h;
4566 {
4567 #if 0 /* MAC_TODO: stipple */
4568 if (s->stippled_p)
4569 {
4570 /* Fill background with a stipple pattern. */
4571 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4572 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4573 XSetFillStyle (s->display, s->gc, FillSolid);
4574 }
4575 else
4576 #endif /* MAC_TODO */
4577 x_clear_glyph_string_rect (s, x, y, w, h);
4578 }
4579
4580
4581 /* Draw image glyph string S.
4582
4583 s->y
4584 s->x +-------------------------
4585 | s->face->box
4586 |
4587 | +-------------------------
4588 | | s->img->vmargin
4589 | |
4590 | | +-------------------
4591 | | | the image
4592
4593 */
4594
4595 static void
4596 x_draw_image_glyph_string (s)
4597 struct glyph_string *s;
4598 {
4599 int x, y;
4600 int box_line_hwidth = abs (s->face->box_line_width);
4601 int box_line_vwidth = max (s->face->box_line_width, 0);
4602 int height;
4603 Pixmap pixmap = 0;
4604
4605 height = s->height - 2 * box_line_vwidth;
4606
4607 /* Fill background with face under the image. Do it only if row is
4608 taller than image or if image has a clip mask to reduce
4609 flickering. */
4610 s->stippled_p = s->face->stipple != 0;
4611 if (height > s->img->height
4612 || s->img->hmargin
4613 || s->img->vmargin
4614 #if 0 /* TODO: image mask */
4615 || s->img->mask
4616 #endif
4617 || s->img->pixmap == 0
4618 || s->width != s->background_width)
4619 {
4620 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4621 x = s->x + box_line_hwidth;
4622 else
4623 x = s->x;
4624
4625 y = s->y + box_line_vwidth;
4626 #if 0 /* TODO: image mask */
4627 if (s->img->mask)
4628 {
4629 /* Create a pixmap as large as the glyph string. Fill it
4630 with the background color. Copy the image to it, using
4631 its mask. Copy the temporary pixmap to the display. */
4632 Screen *screen = FRAME_X_SCREEN (s->f);
4633 int depth = DefaultDepthOfScreen (screen);
4634
4635 /* Create a pixmap as large as the glyph string. */
4636 pixmap = XCreatePixmap (s->display, s->window,
4637 s->background_width,
4638 s->height, depth);
4639
4640 /* Don't clip in the following because we're working on the
4641 pixmap. */
4642 XSetClipMask (s->display, s->gc, None);
4643
4644 /* Fill the pixmap with the background color/stipple. */
4645 if (s->stippled_p)
4646 {
4647 /* Fill background with a stipple pattern. */
4648 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4649 XFillRectangle (s->display, pixmap, s->gc,
4650 0, 0, s->background_width, s->height);
4651 XSetFillStyle (s->display, s->gc, FillSolid);
4652 }
4653 else
4654 {
4655 XGCValues xgcv;
4656 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4657 &xgcv);
4658 XSetForeground (s->display, s->gc, xgcv.background);
4659 XFillRectangle (s->display, pixmap, s->gc,
4660 0, 0, s->background_width, s->height);
4661 XSetForeground (s->display, s->gc, xgcv.foreground);
4662 }
4663 }
4664 else
4665 #endif
4666 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4667
4668 s->background_filled_p = 1;
4669 }
4670
4671 /* Draw the foreground. */
4672 if (pixmap != 0)
4673 {
4674 x_draw_image_foreground_1 (s, pixmap);
4675 x_set_glyph_string_clipping (s);
4676 mac_copy_area (s->display, pixmap, s->window, s->gc,
4677 0, 0, s->background_width, s->height, s->x, s->y);
4678 mac_reset_clipping (s->display, s->window);
4679 XFreePixmap (s->display, pixmap);
4680 }
4681 else
4682 x_draw_image_foreground (s);
4683
4684 /* If we must draw a relief around the image, do it. */
4685 if (s->img->relief
4686 || s->hl == DRAW_IMAGE_RAISED
4687 || s->hl == DRAW_IMAGE_SUNKEN)
4688 x_draw_image_relief (s);
4689 }
4690
4691
4692 /* Draw stretch glyph string S. */
4693
4694 static void
4695 x_draw_stretch_glyph_string (s)
4696 struct glyph_string *s;
4697 {
4698 xassert (s->first_glyph->type == STRETCH_GLYPH);
4699 s->stippled_p = s->face->stipple != 0;
4700
4701 if (s->hl == DRAW_CURSOR
4702 && !x_stretch_cursor_p)
4703 {
4704 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4705 as wide as the stretch glyph. */
4706 int width = min (CANON_X_UNIT (s->f), s->background_width);
4707
4708 /* Draw cursor. */
4709 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4710
4711 /* Clear rest using the GC of the original non-cursor face. */
4712 if (width < s->background_width)
4713 {
4714 GC gc = s->face->gc;
4715 int x = s->x + width, y = s->y;
4716 int w = s->background_width - width, h = s->height;
4717 Rect r;
4718
4719 if (s->row->mouse_face_p
4720 && cursor_in_mouse_face_p (s->w))
4721 {
4722 x_set_mouse_face_gc (s);
4723 gc = s->gc;
4724 }
4725 else
4726 gc = s->face->gc;
4727
4728 x_get_glyph_string_clip_rect (s, &r);
4729 mac_set_clip_rectangle (s->display, s->window, &r);
4730
4731 #if 0 /* MAC_TODO: stipple */
4732 if (s->face->stipple)
4733 {
4734 /* Fill background with a stipple pattern. */
4735 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4736 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4737 XSetFillStyle (s->display, gc, FillSolid);
4738 }
4739 else
4740 #endif /* MAC_TODO */
4741 {
4742 XGCValues xgcv;
4743 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4744 XSetForeground (s->display, gc, xgcv.background);
4745 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4746 XSetForeground (s->display, gc, xgcv.foreground);
4747 }
4748
4749 mac_reset_clipping (s->display, s->window);
4750 }
4751 }
4752 else if (!s->background_filled_p)
4753 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4754 s->height);
4755
4756 s->background_filled_p = 1;
4757 }
4758
4759
4760 /* Draw glyph string S. */
4761
4762 static void
4763 x_draw_glyph_string (s)
4764 struct glyph_string *s;
4765 {
4766 int relief_drawn_p = 0;
4767
4768 /* If S draws into the background of its successor, draw the
4769 background of the successor first so that S can draw into it.
4770 This makes S->next use XDrawString instead of XDrawImageString. */
4771 if (s->next && s->right_overhang && !s->for_overlaps_p)
4772 {
4773 xassert (s->next->img == NULL);
4774 x_set_glyph_string_gc (s->next);
4775 x_set_glyph_string_clipping (s->next);
4776 x_draw_glyph_string_background (s->next, 1);
4777
4778 }
4779
4780 /* Set up S->gc, set clipping and draw S. */
4781 x_set_glyph_string_gc (s);
4782
4783 /* Draw relief (if any) in advance for char/composition so that the
4784 glyph string can be drawn over it. */
4785 if (!s->for_overlaps_p
4786 && s->face->box != FACE_NO_BOX
4787 && (s->first_glyph->type == CHAR_GLYPH
4788 || s->first_glyph->type == COMPOSITE_GLYPH))
4789
4790 {
4791 x_set_glyph_string_clipping (s);
4792 x_draw_glyph_string_background (s, 1);
4793 x_draw_glyph_string_box (s);
4794 x_set_glyph_string_clipping (s);
4795 relief_drawn_p = 1;
4796 }
4797 else
4798 x_set_glyph_string_clipping (s);
4799
4800 switch (s->first_glyph->type)
4801 {
4802 case IMAGE_GLYPH:
4803 x_draw_image_glyph_string (s);
4804 break;
4805
4806 case STRETCH_GLYPH:
4807 x_draw_stretch_glyph_string (s);
4808 break;
4809
4810 case CHAR_GLYPH:
4811 if (s->for_overlaps_p)
4812 s->background_filled_p = 1;
4813 else
4814 x_draw_glyph_string_background (s, 0);
4815 x_draw_glyph_string_foreground (s);
4816 break;
4817
4818 case COMPOSITE_GLYPH:
4819 if (s->for_overlaps_p || s->gidx > 0)
4820 s->background_filled_p = 1;
4821 else
4822 x_draw_glyph_string_background (s, 1);
4823 x_draw_composite_glyph_string_foreground (s);
4824 break;
4825
4826 default:
4827 abort ();
4828 }
4829
4830 if (!s->for_overlaps_p)
4831 {
4832 /* Draw underline. */
4833 if (s->face->underline_p)
4834 {
4835 unsigned long h = 1;
4836 unsigned long dy = s->height - h;
4837
4838 if (s->face->underline_defaulted_p)
4839 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4840 s->width, h);
4841 else
4842 {
4843 XGCValues xgcv;
4844 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4845 XSetForeground (s->display, s->gc, s->face->underline_color);
4846 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4847 s->width, h);
4848 XSetForeground (s->display, s->gc, xgcv.foreground);
4849 }
4850 }
4851
4852 /* Draw overline. */
4853 if (s->face->overline_p)
4854 {
4855 unsigned long dy = 0, h = 1;
4856
4857 if (s->face->overline_color_defaulted_p)
4858 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4859 s->width, h);
4860 else
4861 {
4862 XGCValues xgcv;
4863 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4864 XSetForeground (s->display, s->gc, s->face->overline_color);
4865 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4866 s->width, h);
4867 XSetForeground (s->display, s->gc, xgcv.foreground);
4868 }
4869 }
4870
4871 /* Draw strike-through. */
4872 if (s->face->strike_through_p)
4873 {
4874 unsigned long h = 1;
4875 unsigned long dy = (s->height - h) / 2;
4876
4877 if (s->face->strike_through_color_defaulted_p)
4878 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4879 s->width, h);
4880 else
4881 {
4882 XGCValues xgcv;
4883 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4884 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4885 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4886 s->width, h);
4887 XSetForeground (s->display, s->gc, xgcv.foreground);
4888 }
4889 }
4890
4891 /* Draw relief. */
4892 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
4893 x_draw_glyph_string_box (s);
4894 }
4895
4896 /* Reset clipping. */
4897 mac_reset_clipping (s->display, s->window);
4898 }
4899
4900
4901 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4902 struct face **, int));
4903
4904
4905 /* Fill glyph string S with composition components specified by S->cmp.
4906
4907 FACES is an array of faces for all components of this composition.
4908 S->gidx is the index of the first component for S.
4909 OVERLAPS_P non-zero means S should draw the foreground only, and
4910 use its physical height for clipping.
4911
4912 Value is the index of a component not in S. */
4913
4914 static int
4915 x_fill_composite_glyph_string (s, faces, overlaps_p)
4916 struct glyph_string *s;
4917 struct face **faces;
4918 int overlaps_p;
4919 {
4920 int i;
4921
4922 xassert (s);
4923
4924 s->for_overlaps_p = overlaps_p;
4925
4926 s->face = faces[s->gidx];
4927 s->font = s->face->font;
4928 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4929
4930 /* For all glyphs of this composition, starting at the offset
4931 S->gidx, until we reach the end of the definition or encounter a
4932 glyph that requires the different face, add it to S. */
4933 ++s->nchars;
4934 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4935 ++s->nchars;
4936
4937 /* All glyph strings for the same composition has the same width,
4938 i.e. the width set for the first component of the composition. */
4939
4940 s->width = s->first_glyph->pixel_width;
4941
4942 /* If the specified font could not be loaded, use the frame's
4943 default font, but record the fact that we couldn't load it in
4944 the glyph string so that we can draw rectangles for the
4945 characters of the glyph string. */
4946 if (s->font == NULL)
4947 {
4948 s->font_not_found_p = 1;
4949 s->font = FRAME_FONT (s->f);
4950 }
4951
4952 /* Adjust base line for subscript/superscript text. */
4953 s->ybase += s->first_glyph->voffset;
4954
4955 xassert (s->face && s->face->gc);
4956
4957 /* This glyph string must always be drawn with 16-bit functions. */
4958 s->two_byte_p = 1;
4959
4960 return s->gidx + s->nchars;
4961 }
4962
4963
4964 /* Fill glyph string S from a sequence of character glyphs.
4965
4966 FACE_ID is the face id of the string. START is the index of the
4967 first glyph to consider, END is the index of the last + 1.
4968 OVERLAPS_P non-zero means S should draw the foreground only, and
4969 use its physical height for clipping.
4970
4971 Value is the index of the first glyph not in S. */
4972
4973 static int
4974 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4975 struct glyph_string *s;
4976 int face_id;
4977 int start, end, overlaps_p;
4978 {
4979 struct glyph *glyph, *last;
4980 int voffset;
4981 int glyph_not_available_p;
4982
4983 xassert (s->f == XFRAME (s->w->frame));
4984 xassert (s->nchars == 0);
4985 xassert (start >= 0 && end > start);
4986
4987 s->for_overlaps_p = overlaps_p;
4988 glyph = s->row->glyphs[s->area] + start;
4989 last = s->row->glyphs[s->area] + end;
4990 voffset = glyph->voffset;
4991
4992 glyph_not_available_p = glyph->glyph_not_available_p;
4993
4994 while (glyph < last
4995 && glyph->type == CHAR_GLYPH
4996 && glyph->voffset == voffset
4997 /* Same face id implies same font, nowadays. */
4998 && glyph->face_id == face_id
4999 && glyph->glyph_not_available_p == glyph_not_available_p)
5000 {
5001 int two_byte_p;
5002
5003 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
5004 s->char2b + s->nchars,
5005 &two_byte_p);
5006 s->two_byte_p = two_byte_p;
5007 ++s->nchars;
5008 xassert (s->nchars <= end - start);
5009 s->width += glyph->pixel_width;
5010 ++glyph;
5011 }
5012
5013 s->font = s->face->font;
5014 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
5015
5016 /* If the specified font could not be loaded, use the frame's font,
5017 but record the fact that we couldn't load it in
5018 S->font_not_found_p so that we can draw rectangles for the
5019 characters of the glyph string. */
5020 if (s->font == NULL || glyph_not_available_p)
5021 {
5022 s->font_not_found_p = 1;
5023 s->font = FRAME_FONT (s->f);
5024 }
5025
5026 /* Adjust base line for subscript/superscript text. */
5027 s->ybase += voffset;
5028
5029 xassert (s->face && s->face->gc);
5030 return glyph - s->row->glyphs[s->area];
5031 }
5032
5033
5034 /* Fill glyph string S from image glyph S->first_glyph. */
5035
5036 static void
5037 x_fill_image_glyph_string (s)
5038 struct glyph_string *s;
5039 {
5040 xassert (s->first_glyph->type == IMAGE_GLYPH);
5041 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
5042 xassert (s->img);
5043 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
5044 s->font = s->face->font;
5045 s->width = s->first_glyph->pixel_width;
5046
5047 /* Adjust base line for subscript/superscript text. */
5048 s->ybase += s->first_glyph->voffset;
5049 }
5050
5051
5052 /* Fill glyph string S from a sequence of stretch glyphs.
5053
5054 ROW is the glyph row in which the glyphs are found, AREA is the
5055 area within the row. START is the index of the first glyph to
5056 consider, END is the index of the last + 1.
5057
5058 Value is the index of the first glyph not in S. */
5059
5060 static int
5061 x_fill_stretch_glyph_string (s, row, area, start, end)
5062 struct glyph_string *s;
5063 struct glyph_row *row;
5064 enum glyph_row_area area;
5065 int start, end;
5066 {
5067 struct glyph *glyph, *last;
5068 int voffset, face_id;
5069
5070 xassert (s->first_glyph->type == STRETCH_GLYPH);
5071
5072 glyph = s->row->glyphs[s->area] + start;
5073 last = s->row->glyphs[s->area] + end;
5074 face_id = glyph->face_id;
5075 s->face = FACE_FROM_ID (s->f, face_id);
5076 s->font = s->face->font;
5077 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
5078 s->width = glyph->pixel_width;
5079 voffset = glyph->voffset;
5080
5081 for (++glyph;
5082 (glyph < last
5083 && glyph->type == STRETCH_GLYPH
5084 && glyph->voffset == voffset
5085 && glyph->face_id == face_id);
5086 ++glyph)
5087 s->width += glyph->pixel_width;
5088
5089 /* Adjust base line for subscript/superscript text. */
5090 s->ybase += voffset;
5091
5092 xassert (s->face);
5093 return glyph - s->row->glyphs[s->area];
5094 }
5095
5096
5097 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
5098 of XChar2b structures for S; it can't be allocated in
5099 x_init_glyph_string because it must be allocated via `alloca'. W
5100 is the window on which S is drawn. ROW and AREA are the glyph row
5101 and area within the row from which S is constructed. START is the
5102 index of the first glyph structure covered by S. HL is a
5103 face-override for drawing S. */
5104
5105 static void
5106 x_init_glyph_string (s, char2b, w, row, area, start, hl)
5107 struct glyph_string *s;
5108 XChar2b *char2b;
5109 struct window *w;
5110 struct glyph_row *row;
5111 enum glyph_row_area area;
5112 int start;
5113 enum draw_glyphs_face hl;
5114 {
5115 bzero (s, sizeof *s);
5116 s->w = w;
5117 s->f = XFRAME (w->frame);
5118 s->display = FRAME_MAC_DISPLAY (s->f);
5119 s->window = FRAME_MAC_WINDOW (s->f);
5120 s->char2b = char2b;
5121 s->hl = hl;
5122 s->row = row;
5123 s->area = area;
5124 s->first_glyph = row->glyphs[area] + start;
5125 s->height = row->height;
5126 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
5127
5128 /* Display the internal border below the tool-bar window. */
5129 if (s->w == XWINDOW (s->f->tool_bar_window))
5130 s->y -= s->f->output_data.mac->internal_border_width;
5131
5132 s->ybase = s->y + row->ascent;
5133 }
5134
5135
5136 /* Set background width of glyph string S. START is the index of the
5137 first glyph following S. LAST_X is the right-most x-position + 1
5138 in the drawing area. */
5139
5140 static INLINE void
5141 x_set_glyph_string_background_width (s, start, last_x)
5142 struct glyph_string *s;
5143 int start;
5144 int last_x;
5145 {
5146 /* If the face of this glyph string has to be drawn to the end of
5147 the drawing area, set S->extends_to_end_of_line_p. */
5148 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
5149
5150 if (start == s->row->used[s->area]
5151 && s->area == TEXT_AREA
5152 && ((s->hl == DRAW_NORMAL_TEXT
5153 && (s->row->fill_line_p
5154 || s->face->background != default_face->background
5155 || s->face->stipple != default_face->stipple
5156 || s->row->mouse_face_p))
5157 || s->hl == DRAW_MOUSE_FACE
5158 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
5159 && s->row->fill_line_p)))
5160 s->extends_to_end_of_line_p = 1;
5161
5162 /* If S extends its face to the end of the line, set its
5163 background_width to the distance to the right edge of the drawing
5164 area. */
5165 if (s->extends_to_end_of_line_p)
5166 s->background_width = last_x - s->x + 1;
5167 else
5168 s->background_width = s->width;
5169 }
5170
5171
5172 /* Add a glyph string for a stretch glyph to the list of strings
5173 between HEAD and TAIL. START is the index of the stretch glyph in
5174 row area AREA of glyph row ROW. END is the index of the last glyph
5175 in that glyph row area. X is the current output position assigned
5176 to the new glyph string constructed. HL overrides that face of the
5177 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
5178 is the right-most x-position of the drawing area. */
5179
5180 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
5181 and below -- keep them on one line. */
5182 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
5183 do \
5184 { \
5185 s = (struct glyph_string *) alloca (sizeof *s); \
5186 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
5187 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
5188 x_append_glyph_string (&HEAD, &TAIL, s); \
5189 s->x = (X); \
5190 } \
5191 while (0)
5192
5193
5194 /* Add a glyph string for an image glyph to the list of strings
5195 between HEAD and TAIL. START is the index of the image glyph in
5196 row area AREA of glyph row ROW. END is the index of the last glyph
5197 in that glyph row area. X is the current output position assigned
5198 to the new glyph string constructed. HL overrides that face of the
5199 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
5200 is the right-most x-position of the drawing area. */
5201
5202 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
5203 do \
5204 { \
5205 s = (struct glyph_string *) alloca (sizeof *s); \
5206 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
5207 x_fill_image_glyph_string (s); \
5208 x_append_glyph_string (&HEAD, &TAIL, s); \
5209 ++START; \
5210 s->x = (X); \
5211 } \
5212 while (0)
5213
5214
5215 /* Add a glyph string for a sequence of character glyphs to the list
5216 of strings between HEAD and TAIL. START is the index of the first
5217 glyph in row area AREA of glyph row ROW that is part of the new
5218 glyph string. END is the index of the last glyph in that glyph row
5219 area. X is the current output position assigned to the new glyph
5220 string constructed. HL overrides that face of the glyph; e.g. it
5221 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
5222 right-most x-position of the drawing area. */
5223
5224 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
5225 do \
5226 { \
5227 int c, face_id; \
5228 XChar2b *char2b; \
5229 \
5230 c = (ROW)->glyphs[AREA][START].u.ch; \
5231 face_id = (ROW)->glyphs[AREA][START].face_id; \
5232 \
5233 s = (struct glyph_string *) alloca (sizeof *s); \
5234 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
5235 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
5236 x_append_glyph_string (&HEAD, &TAIL, s); \
5237 s->x = (X); \
5238 START = x_fill_glyph_string (s, face_id, START, END, \
5239 OVERLAPS_P); \
5240 } \
5241 while (0)
5242
5243
5244 /* Add a glyph string for a composite sequence to the list of strings
5245 between HEAD and TAIL. START is the index of the first glyph in
5246 row area AREA of glyph row ROW that is part of the new glyph
5247 string. END is the index of the last glyph in that glyph row area.
5248 X is the current output position assigned to the new glyph string
5249 constructed. HL overrides that face of the glyph; e.g. it is
5250 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
5251 x-position of the drawing area. */
5252
5253 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
5254 do { \
5255 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
5256 int face_id = (ROW)->glyphs[AREA][START].face_id; \
5257 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
5258 struct composition *cmp = composition_table[cmp_id]; \
5259 int glyph_len = cmp->glyph_len; \
5260 XChar2b *char2b; \
5261 struct face **faces; \
5262 struct glyph_string *first_s = NULL; \
5263 int n; \
5264 \
5265 base_face = base_face->ascii_face; \
5266 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
5267 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
5268 /* At first, fill in `char2b' and `faces'. */ \
5269 for (n = 0; n < glyph_len; n++) \
5270 { \
5271 int c = COMPOSITION_GLYPH (cmp, n); \
5272 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
5273 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
5274 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
5275 this_face_id, char2b + n, 1); \
5276 } \
5277 \
5278 /* Make glyph_strings for each glyph sequence that is drawable by \
5279 the same face, and append them to HEAD/TAIL. */ \
5280 for (n = 0; n < cmp->glyph_len;) \
5281 { \
5282 s = (struct glyph_string *) alloca (sizeof *s); \
5283 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
5284 x_append_glyph_string (&(HEAD), &(TAIL), s); \
5285 s->cmp = cmp; \
5286 s->gidx = n; \
5287 s->x = (X); \
5288 \
5289 if (n == 0) \
5290 first_s = s; \
5291 \
5292 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
5293 } \
5294 \
5295 ++START; \
5296 s = first_s; \
5297 } while (0)
5298
5299
5300 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
5301 of AREA of glyph row ROW on window W between indices START and END.
5302 HL overrides the face for drawing glyph strings, e.g. it is
5303 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
5304 x-positions of the drawing area.
5305
5306 This is an ugly monster macro construct because we must use alloca
5307 to allocate glyph strings (because x_draw_glyphs can be called
5308 asynchronously). */
5309
5310 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
5311 do \
5312 { \
5313 HEAD = TAIL = NULL; \
5314 while (START < END) \
5315 { \
5316 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
5317 switch (first_glyph->type) \
5318 { \
5319 case CHAR_GLYPH: \
5320 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
5321 TAIL, HL, X, LAST_X, \
5322 OVERLAPS_P); \
5323 break; \
5324 \
5325 case COMPOSITE_GLYPH: \
5326 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
5327 HEAD, TAIL, HL, X, LAST_X,\
5328 OVERLAPS_P); \
5329 break; \
5330 \
5331 case STRETCH_GLYPH: \
5332 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
5333 HEAD, TAIL, HL, X, LAST_X); \
5334 break; \
5335 \
5336 case IMAGE_GLYPH: \
5337 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
5338 TAIL, HL, X, LAST_X); \
5339 break; \
5340 \
5341 default: \
5342 abort (); \
5343 } \
5344 \
5345 x_set_glyph_string_background_width (s, START, LAST_X); \
5346 (X) += s->width; \
5347 } \
5348 } \
5349 while (0)
5350
5351
5352 /* Draw glyphs between START and END in AREA of ROW on window W,
5353 starting at x-position X. X is relative to AREA in W. HL is a
5354 face-override with the following meaning:
5355
5356 DRAW_NORMAL_TEXT draw normally
5357 DRAW_CURSOR draw in cursor face
5358 DRAW_MOUSE_FACE draw in mouse face.
5359 DRAW_INVERSE_VIDEO draw in mode line face
5360 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
5361 DRAW_IMAGE_RAISED draw an image with a raised relief around it
5362
5363 If OVERLAPS_P is non-zero, draw only the foreground of characters
5364 and clip to the physical height of ROW.
5365
5366 Value is the x-position reached, relative to AREA of W. */
5367
5368 static int
5369 x_draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
5370 struct window *w;
5371 int x;
5372 struct glyph_row *row;
5373 enum glyph_row_area area;
5374 int start, end;
5375 enum draw_glyphs_face hl;
5376 int overlaps_p;
5377 {
5378 struct glyph_string *head, *tail;
5379 struct glyph_string *s;
5380 int last_x, area_width;
5381 int x_reached;
5382 int i, j;
5383
5384 /* Let's rather be paranoid than getting a SEGV. */
5385 end = min (end, row->used[area]);
5386 start = max (0, start);
5387 start = min (end, start);
5388
5389 /* Translate X to frame coordinates. Set last_x to the right
5390 end of the drawing area. */
5391 if (row->full_width_p)
5392 {
5393 /* X is relative to the left edge of W, without scroll bars
5394 or fringes. */
5395 struct frame *f = XFRAME (WINDOW_FRAME (w));
5396 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
5397
5398 x += window_left_x;
5399 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
5400 last_x = window_left_x + area_width;
5401
5402 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5403 {
5404 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5405 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
5406 last_x += width;
5407 else
5408 x -= width;
5409 }
5410
5411 x += FRAME_INTERNAL_BORDER_WIDTH (f);
5412 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
5413 }
5414 else
5415 {
5416 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
5417 area_width = window_box_width (w, area);
5418 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
5419 }
5420
5421 /* Build a doubly-linked list of glyph_string structures between
5422 head and tail from what we have to draw. Note that the macro
5423 BUILD_GLYPH_STRINGS will modify its start parameter. That's
5424 the reason we use a separate variable `i'. */
5425 i = start;
5426 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
5427 overlaps_p);
5428 if (tail)
5429 x_reached = tail->x + tail->background_width;
5430 else
5431 x_reached = x;
5432
5433 /* If there are any glyphs with lbearing < 0 or rbearing > width in
5434 the row, redraw some glyphs in front or following the glyph
5435 strings built above. */
5436 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
5437 {
5438 int dummy_x = 0;
5439 struct glyph_string *h, *t;
5440
5441 /* Compute overhangs for all glyph strings. */
5442 for (s = head; s; s = s->next)
5443 x_compute_glyph_string_overhangs (s);
5444
5445 /* Prepend glyph strings for glyphs in front of the first glyph
5446 string that are overwritten because of the first glyph
5447 string's left overhang. The background of all strings
5448 prepended must be drawn because the first glyph string
5449 draws over it. */
5450 i = x_left_overwritten (head);
5451 if (i >= 0)
5452 {
5453 j = i;
5454 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
5455 DRAW_NORMAL_TEXT, dummy_x, last_x,
5456 overlaps_p);
5457 start = i;
5458 x_compute_overhangs_and_x (t, head->x, 1);
5459 x_prepend_glyph_string_lists (&head, &tail, h, t);
5460 }
5461
5462 /* Prepend glyph strings for glyphs in front of the first glyph
5463 string that overwrite that glyph string because of their
5464 right overhang. For these strings, only the foreground must
5465 be drawn, because it draws over the glyph string at `head'.
5466 The background must not be drawn because this would overwrite
5467 right overhangs of preceding glyphs for which no glyph
5468 strings exist. */
5469 i = x_left_overwriting (head);
5470 if (i >= 0)
5471 {
5472 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
5473 DRAW_NORMAL_TEXT, dummy_x, last_x,
5474 overlaps_p);
5475 for (s = h; s; s = s->next)
5476 s->background_filled_p = 1;
5477 x_compute_overhangs_and_x (t, head->x, 1);
5478 x_prepend_glyph_string_lists (&head, &tail, h, t);
5479 }
5480
5481 /* Append glyphs strings for glyphs following the last glyph
5482 string tail that are overwritten by tail. The background of
5483 these strings has to be drawn because tail's foreground draws
5484 over it. */
5485 i = x_right_overwritten (tail);
5486 if (i >= 0)
5487 {
5488 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5489 DRAW_NORMAL_TEXT, x, last_x,
5490 overlaps_p);
5491 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5492 x_append_glyph_string_lists (&head, &tail, h, t);
5493 }
5494
5495 /* Append glyph strings for glyphs following the last glyph
5496 string tail that overwrite tail. The foreground of such
5497 glyphs has to be drawn because it writes into the background
5498 of tail. The background must not be drawn because it could
5499 paint over the foreground of following glyphs. */
5500 i = x_right_overwriting (tail);
5501 if (i >= 0)
5502 {
5503 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5504 DRAW_NORMAL_TEXT, x, last_x,
5505 overlaps_p);
5506 for (s = h; s; s = s->next)
5507 s->background_filled_p = 1;
5508 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5509 x_append_glyph_string_lists (&head, &tail, h, t);
5510 }
5511 }
5512
5513 /* Draw all strings. */
5514 for (s = head; s; s = s->next)
5515 x_draw_glyph_string (s);
5516
5517 if (area == TEXT_AREA
5518 && !row->full_width_p
5519 /* When drawing overlapping rows, only the glyph strings'
5520 foreground is drawn, which doesn't erase a cursor
5521 completely. */
5522 && !overlaps_p)
5523 {
5524 int x0 = head ? head->x : x;
5525 int x1 = tail ? tail->x + tail->background_width : x;
5526
5527 x0 = FRAME_TO_WINDOW_PIXEL_X (w, x0);
5528 x1 = FRAME_TO_WINDOW_PIXEL_X (w, x1);
5529
5530 if (!row->full_width_p && XFASTINT (w->left_margin_width) != 0)
5531 {
5532 int left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
5533 x0 -= left_area_width;
5534 x1 -= left_area_width;
5535 }
5536
5537 notice_overwritten_cursor (w, area, x0, x1,
5538 row->y, MATRIX_ROW_BOTTOM_Y (row));
5539 }
5540
5541 /* Value is the x-position up to which drawn, relative to AREA of W.
5542 This doesn't include parts drawn because of overhangs. */
5543 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5544 if (!row->full_width_p)
5545 {
5546 if (area > LEFT_MARGIN_AREA)
5547 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5548 if (area > TEXT_AREA)
5549 x_reached -= window_box_width (w, TEXT_AREA);
5550 }
5551
5552 return x_reached;
5553 }
5554
5555
5556 /* Fix the display of area AREA of overlapping row ROW in window W. */
5557
5558 static void
5559 x_fix_overlapping_area (w, row, area)
5560 struct window *w;
5561 struct glyph_row *row;
5562 enum glyph_row_area area;
5563 {
5564 int i, x;
5565
5566 BLOCK_INPUT;
5567
5568 if (area == LEFT_MARGIN_AREA)
5569 x = 0;
5570 else if (area == TEXT_AREA)
5571 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5572 else
5573 x = (window_box_width (w, LEFT_MARGIN_AREA)
5574 + window_box_width (w, TEXT_AREA));
5575
5576 for (i = 0; i < row->used[area];)
5577 {
5578 if (row->glyphs[area][i].overlaps_vertically_p)
5579 {
5580 int start = i, start_x = x;
5581
5582 do
5583 {
5584 x += row->glyphs[area][i].pixel_width;
5585 ++i;
5586 }
5587 while (i < row->used[area]
5588 && row->glyphs[area][i].overlaps_vertically_p);
5589
5590 x_draw_glyphs (w, start_x, row, area, start, i,
5591 DRAW_NORMAL_TEXT, 1);
5592 }
5593 else
5594 {
5595 x += row->glyphs[area][i].pixel_width;
5596 ++i;
5597 }
5598 }
5599
5600 UNBLOCK_INPUT;
5601 }
5602
5603
5604 /* Output LEN glyphs starting at START at the nominal cursor position.
5605 Advance the nominal cursor over the text. The global variable
5606 updated_window contains the window being updated, updated_row is
5607 the glyph row being updated, and updated_area is the area of that
5608 row being updated. */
5609
5610 static void
5611 x_write_glyphs (start, len)
5612 struct glyph *start;
5613 int len;
5614 {
5615 int x, hpos;
5616
5617 xassert (updated_window && updated_row);
5618 BLOCK_INPUT;
5619
5620 /* Write glyphs. */
5621
5622 hpos = start - updated_row->glyphs[updated_area];
5623 x = x_draw_glyphs (updated_window, output_cursor.x,
5624 updated_row, updated_area,
5625 hpos, hpos + len,
5626 DRAW_NORMAL_TEXT, 0);
5627
5628 UNBLOCK_INPUT;
5629
5630 /* Advance the output cursor. */
5631 output_cursor.hpos += len;
5632 output_cursor.x = x;
5633 }
5634
5635
5636 /* Insert LEN glyphs from START at the nominal cursor position. */
5637
5638 static void
5639 x_insert_glyphs (start, len)
5640 struct glyph *start;
5641 register int len;
5642 {
5643 struct frame *f;
5644 struct window *w;
5645 int line_height, shift_by_width, shifted_region_width;
5646 struct glyph_row *row;
5647 struct glyph *glyph;
5648 int frame_x, frame_y, hpos;
5649
5650 xassert (updated_window && updated_row);
5651 BLOCK_INPUT;
5652 w = updated_window;
5653 f = XFRAME (WINDOW_FRAME (w));
5654
5655 /* Get the height of the line we are in. */
5656 row = updated_row;
5657 line_height = row->height;
5658
5659 /* Get the width of the glyphs to insert. */
5660 shift_by_width = 0;
5661 for (glyph = start; glyph < start + len; ++glyph)
5662 shift_by_width += glyph->pixel_width;
5663
5664 /* Get the width of the region to shift right. */
5665 shifted_region_width = (window_box_width (w, updated_area)
5666 - output_cursor.x
5667 - shift_by_width);
5668
5669 /* Shift right. */
5670 frame_x = window_box_left (w, updated_area) + output_cursor.x;
5671 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5672
5673 mac_scroll_area (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f),
5674 f->output_data.mac->normal_gc,
5675 frame_x, frame_y,
5676 shifted_region_width, line_height,
5677 frame_x + shift_by_width, frame_y);
5678
5679 /* Write the glyphs. */
5680 hpos = start - row->glyphs[updated_area];
5681 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
5682 DRAW_NORMAL_TEXT, 0);
5683
5684 /* Advance the output cursor. */
5685 output_cursor.hpos += len;
5686 output_cursor.x += shift_by_width;
5687 UNBLOCK_INPUT;
5688 }
5689
5690
5691 /* Delete N glyphs at the nominal cursor position. Not implemented
5692 for X frames. */
5693
5694 static void
5695 x_delete_glyphs (n)
5696 register int n;
5697 {
5698 abort ();
5699 }
5700
5701
5702 /* Erase the current text line from the nominal cursor position
5703 (inclusive) to pixel column TO_X (exclusive). The idea is that
5704 everything from TO_X onward is already erased.
5705
5706 TO_X is a pixel position relative to updated_area of
5707 updated_window. TO_X == -1 means clear to the end of this area. */
5708
5709 static void
5710 x_clear_end_of_line (to_x)
5711 int to_x;
5712 {
5713 struct frame *f;
5714 struct window *w = updated_window;
5715 int max_x, min_y, max_y;
5716 int from_x, from_y, to_y;
5717
5718 xassert (updated_window && updated_row);
5719 f = XFRAME (w->frame);
5720
5721 if (updated_row->full_width_p)
5722 {
5723 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5724 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5725 && !w->pseudo_window_p)
5726 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5727 }
5728 else
5729 max_x = window_box_width (w, updated_area);
5730 max_y = window_text_bottom_y (w);
5731
5732 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5733 of window. For TO_X > 0, truncate to end of drawing area. */
5734 if (to_x == 0)
5735 return;
5736 else if (to_x < 0)
5737 to_x = max_x;
5738 else
5739 to_x = min (to_x, max_x);
5740
5741 to_y = min (max_y, output_cursor.y + updated_row->height);
5742
5743 /* Notice if the cursor will be cleared by this operation. */
5744 if (!updated_row->full_width_p)
5745 notice_overwritten_cursor (w, updated_area,
5746 output_cursor.x, -1,
5747 updated_row->y,
5748 MATRIX_ROW_BOTTOM_Y (updated_row));
5749
5750 from_x = output_cursor.x;
5751
5752 /* Translate to frame coordinates. */
5753 if (updated_row->full_width_p)
5754 {
5755 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5756 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5757 }
5758 else
5759 {
5760 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5761 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5762 }
5763
5764 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5765 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5766 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5767
5768 /* Prevent inadvertently clearing to end of the X window. */
5769 if (to_x > from_x && to_y > from_y)
5770 {
5771 BLOCK_INPUT;
5772 XClearArea (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f),
5773 from_x, from_y, to_x - from_x, to_y - from_y,
5774 0);
5775 UNBLOCK_INPUT;
5776 }
5777 }
5778
5779
5780 /* Clear entire frame. If updating_frame is non-null, clear that
5781 frame. Otherwise clear the selected frame. */
5782
5783 static void
5784 x_clear_frame ()
5785 {
5786 struct frame *f;
5787
5788 if (updating_frame)
5789 f = updating_frame;
5790 else
5791 f = SELECTED_FRAME ();
5792
5793 /* Clearing the frame will erase any cursor, so mark them all as no
5794 longer visible. */
5795 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5796 output_cursor.hpos = output_cursor.vpos = 0;
5797 output_cursor.x = -1;
5798
5799 /* We don't set the output cursor here because there will always
5800 follow an explicit cursor_to. */
5801 BLOCK_INPUT;
5802 XClearWindow (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f));
5803
5804 #if 0 /* Clearing frame on Mac OS clears scroll bars. */
5805 /* We have to clear the scroll bars, too. If we have changed
5806 colors or something like that, then they should be notified. */
5807 x_scroll_bar_clear (f);
5808 #endif
5809
5810 XFlush (FRAME_MAC_DISPLAY (f));
5811 UNBLOCK_INPUT;
5812 }
5813
5814
5815 \f
5816 /* Invert the middle quarter of the frame for .15 sec. */
5817
5818 /* We use the select system call to do the waiting, so we have to make
5819 sure it's available. If it isn't, we just won't do visual bells. */
5820
5821 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5822
5823 /* Subtract the `struct timeval' values X and Y, storing the result in
5824 *RESULT. Return 1 if the difference is negative, otherwise 0. */
5825
5826 static int
5827 timeval_subtract (result, x, y)
5828 struct timeval *result, x, y;
5829 {
5830 /* Perform the carry for the later subtraction by updating y. This
5831 is safer because on some systems the tv_sec member is unsigned. */
5832 if (x.tv_usec < y.tv_usec)
5833 {
5834 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5835 y.tv_usec -= 1000000 * nsec;
5836 y.tv_sec += nsec;
5837 }
5838
5839 if (x.tv_usec - y.tv_usec > 1000000)
5840 {
5841 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5842 y.tv_usec += 1000000 * nsec;
5843 y.tv_sec -= nsec;
5844 }
5845
5846 /* Compute the time remaining to wait. tv_usec is certainly
5847 positive. */
5848 result->tv_sec = x.tv_sec - y.tv_sec;
5849 result->tv_usec = x.tv_usec - y.tv_usec;
5850
5851 /* Return indication of whether the result should be considered
5852 negative. */
5853 return x.tv_sec < y.tv_sec;
5854 }
5855
5856 void
5857 XTflash (f)
5858 struct frame *f;
5859 {
5860 BLOCK_INPUT;
5861
5862 FlashMenuBar (0);
5863
5864 {
5865 struct timeval wakeup;
5866
5867 EMACS_GET_TIME (wakeup);
5868
5869 /* Compute time to wait until, propagating carry from usecs. */
5870 wakeup.tv_usec += 150000;
5871 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5872 wakeup.tv_usec %= 1000000;
5873
5874 /* Keep waiting until past the time wakeup. */
5875 while (1)
5876 {
5877 struct timeval timeout;
5878
5879 EMACS_GET_TIME (timeout);
5880
5881 /* In effect, timeout = wakeup - timeout.
5882 Break if result would be negative. */
5883 if (timeval_subtract (&timeout, wakeup, timeout))
5884 break;
5885
5886 /* Try to wait that long--but we might wake up sooner. */
5887 select (0, NULL, NULL, NULL, &timeout);
5888 }
5889 }
5890
5891 FlashMenuBar (0);
5892
5893 UNBLOCK_INPUT;
5894 }
5895
5896 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5897
5898
5899 /* Make audible bell. */
5900
5901 void
5902 XTring_bell ()
5903 {
5904 struct frame *f = SELECTED_FRAME ();
5905
5906 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5907 if (visible_bell)
5908 XTflash (f);
5909 else
5910 #endif
5911 {
5912 BLOCK_INPUT;
5913 SysBeep (1);
5914 XFlush (FRAME_MAC_DISPLAY (f));
5915 UNBLOCK_INPUT;
5916 }
5917 }
5918
5919
5920 \f
5921 /* Specify how many text lines, from the top of the window,
5922 should be affected by insert-lines and delete-lines operations.
5923 This, and those operations, are used only within an update
5924 that is bounded by calls to x_update_begin and x_update_end. */
5925
5926 void
5927 XTset_terminal_window (n)
5928 register int n;
5929 {
5930 /* This function intentionally left blank. */
5931 }
5932
5933
5934 \f
5935 /***********************************************************************
5936 Line Dance
5937 ***********************************************************************/
5938
5939 /* Perform an insert-lines or delete-lines operation, inserting N
5940 lines or deleting -N lines at vertical position VPOS. */
5941
5942 static void
5943 x_ins_del_lines (vpos, n)
5944 int vpos, n;
5945 {
5946 abort ();
5947 }
5948
5949
5950 /* Scroll part of the display as described by RUN. */
5951
5952 static void
5953 x_scroll_run (w, run)
5954 struct window *w;
5955 struct run *run;
5956 {
5957 struct frame *f = XFRAME (w->frame);
5958 int x, y, width, height, from_y, to_y, bottom_y;
5959
5960 /* Get frame-relative bounding box of the text display area of W,
5961 without mode lines. Include in this box the left and right
5962 fringes of W. */
5963 window_box (w, -1, &x, &y, &width, &height);
5964 width += FRAME_X_FRINGE_WIDTH (f);
5965 x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
5966
5967 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5968 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5969 bottom_y = y + height;
5970
5971 if (to_y < from_y)
5972 {
5973 /* Scrolling up. Make sure we don't copy part of the mode
5974 line at the bottom. */
5975 if (from_y + run->height > bottom_y)
5976 height = bottom_y - from_y;
5977 else
5978 height = run->height;
5979 }
5980 else
5981 {
5982 /* Scolling down. Make sure we don't copy over the mode line.
5983 at the bottom. */
5984 if (to_y + run->height > bottom_y)
5985 height = bottom_y - to_y;
5986 else
5987 height = run->height;
5988 }
5989
5990 BLOCK_INPUT;
5991
5992 /* Cursor off. Will be switched on again in x_update_window_end. */
5993 updated_window = w;
5994 x_clear_cursor (w);
5995
5996 mac_scroll_area (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f),
5997 f->output_data.mac->normal_gc,
5998 x, from_y,
5999 width, height,
6000 x, to_y);
6001
6002 UNBLOCK_INPUT;
6003 }
6004
6005
6006 \f
6007 /***********************************************************************
6008 Exposure Events
6009 ***********************************************************************/
6010
6011 /* Redisplay an exposed area of frame F. X and Y are the upper-left
6012 corner of the exposed rectangle. W and H are width and height of
6013 the exposed area. All are pixel values. W or H zero means redraw
6014 the entire frame. */
6015
6016 static void
6017 expose_frame (f, x, y, w, h)
6018 struct frame *f;
6019 int x, y, w, h;
6020 {
6021 Rect r;
6022 int mouse_face_overwritten_p = 0;
6023
6024 TRACE ((stderr, "expose_frame "));
6025
6026 /* No need to redraw if frame will be redrawn soon. */
6027 if (FRAME_GARBAGED_P (f))
6028 {
6029 TRACE ((stderr, " garbaged\n"));
6030 return;
6031 }
6032
6033 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
6034 or deactivated here, for unknown reasons, activated scroll bars
6035 are shown in deactivated frames in some instances. */
6036 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
6037 activate_scroll_bars (f);
6038 else
6039 deactivate_scroll_bars (f);
6040
6041 /* If basic faces haven't been realized yet, there is no point in
6042 trying to redraw anything. This can happen when we get an expose
6043 event while Emacs is starting, e.g. by moving another window. */
6044 if (FRAME_FACE_CACHE (f) == NULL
6045 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
6046 {
6047 TRACE ((stderr, " no faces\n"));
6048 return;
6049 }
6050
6051 if (w == 0 || h == 0)
6052 {
6053 r.left = r.top = 0;
6054 r.right = CANON_X_UNIT (f) * f->width;
6055 r.bottom = CANON_Y_UNIT (f) * f->height;
6056 }
6057 else
6058 {
6059 r.left = x;
6060 r.top = y;
6061 r.right = x + w;
6062 r.bottom = y + h;
6063 }
6064
6065 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.left, r.top, r.right, r.bottom));
6066 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
6067
6068 if (WINDOWP (f->tool_bar_window))
6069 mouse_face_overwritten_p
6070 |= expose_window (XWINDOW (f->tool_bar_window), &r);
6071
6072 /* Some window managers support a focus-follows-mouse style with
6073 delayed raising of frames. Imagine a partially obscured frame,
6074 and moving the mouse into partially obscured mouse-face on that
6075 frame. The visible part of the mouse-face will be highlighted,
6076 then the WM raises the obscured frame. With at least one WM, KDE
6077 2.1, Emacs is not getting any event for the raising of the frame
6078 (even tried with SubstructureRedirectMask), only Expose events.
6079 These expose events will draw text normally, i.e. not
6080 highlighted. Which means we must redo the highlight here.
6081 Subsume it under ``we love X''. --gerd 2001-08-15 */
6082 /* Included in Windows version because Windows most likely does not
6083 do the right thing if any third party tool offers
6084 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
6085 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
6086 {
6087 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
6088 if (f == dpyinfo->mouse_face_mouse_frame)
6089 {
6090 int x = dpyinfo->mouse_face_mouse_x;
6091 int y = dpyinfo->mouse_face_mouse_y;
6092 clear_mouse_face (dpyinfo);
6093 note_mouse_highlight (f, x, y);
6094 }
6095 }
6096 }
6097
6098
6099 /* Redraw (parts) of all windows in the window tree rooted at W that
6100 intersect R. R contains frame pixel coordinates. */
6101
6102 static int
6103 expose_window_tree (w, r)
6104 struct window *w;
6105 Rect *r;
6106 {
6107 struct frame *f = XFRAME (w->frame);
6108 int mouse_face_overwritten_p = 0;
6109
6110 while (w && !FRAME_GARBAGED_P (f))
6111 {
6112 if (!NILP (w->hchild))
6113 mouse_face_overwritten_p
6114 |= expose_window_tree (XWINDOW (w->hchild), r);
6115 else if (!NILP (w->vchild))
6116 mouse_face_overwritten_p
6117 |= expose_window_tree (XWINDOW (w->vchild), r);
6118 else
6119 mouse_face_overwritten_p |= expose_window (w, r);
6120
6121 w = NILP (w->next) ? NULL : XWINDOW (w->next);
6122 }
6123
6124 return mouse_face_overwritten_p;
6125 }
6126
6127
6128 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
6129 which intersects rectangle R. R is in window-relative coordinates. */
6130
6131 static void
6132 expose_area (w, row, r, area)
6133 struct window *w;
6134 struct glyph_row *row;
6135 Rect *r;
6136 enum glyph_row_area area;
6137 {
6138 struct glyph *first = row->glyphs[area];
6139 struct glyph *end = row->glyphs[area] + row->used[area];
6140 struct glyph *last;
6141 int first_x, start_x, x;
6142
6143 if (area == TEXT_AREA && row->fill_line_p)
6144 /* If row extends face to end of line write the whole line. */
6145 x_draw_glyphs (w, 0, row, area,
6146 0, row->used[area],
6147 DRAW_NORMAL_TEXT, 0);
6148 else
6149 {
6150 /* Set START_X to the window-relative start position for drawing glyphs of
6151 AREA. The first glyph of the text area can be partially visible.
6152 The first glyphs of other areas cannot. */
6153 if (area == LEFT_MARGIN_AREA)
6154 start_x = 0;
6155 else if (area == TEXT_AREA)
6156 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
6157 else
6158 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
6159 + window_box_width (w, TEXT_AREA));
6160 x = start_x;
6161
6162 /* Find the first glyph that must be redrawn. */
6163 while (first < end
6164 && x + first->pixel_width < r->left)
6165 {
6166 x += first->pixel_width;
6167 ++first;
6168 }
6169
6170 /* Find the last one. */
6171 last = first;
6172 first_x = x;
6173 while (last < end
6174 && x < r->right)
6175 {
6176 x += last->pixel_width;
6177 ++last;
6178 }
6179
6180 /* Repaint. */
6181 if (last > first)
6182 x_draw_glyphs (w, first_x - start_x, row, area,
6183 first - row->glyphs[area],
6184 last - row->glyphs[area],
6185 DRAW_NORMAL_TEXT, 0);
6186 }
6187 }
6188
6189
6190 /* Redraw the parts of the glyph row ROW on window W intersecting
6191 rectangle R. R is in window-relative coordinates. Value is
6192 non-zero if mouse face was overwritten. */
6193
6194 static int
6195 expose_line (w, row, r)
6196 struct window *w;
6197 struct glyph_row *row;
6198 Rect *r;
6199 {
6200 xassert (row->enabled_p);
6201
6202 if (row->mode_line_p || w->pseudo_window_p)
6203 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
6204 DRAW_NORMAL_TEXT, 0);
6205 else
6206 {
6207 if (row->used[LEFT_MARGIN_AREA])
6208 expose_area (w, row, r, LEFT_MARGIN_AREA);
6209 if (row->used[TEXT_AREA])
6210 expose_area (w, row, r, TEXT_AREA);
6211 if (row->used[RIGHT_MARGIN_AREA])
6212 expose_area (w, row, r, RIGHT_MARGIN_AREA);
6213 x_draw_row_fringe_bitmaps (w, row);
6214 }
6215
6216 return row->mouse_face_p;
6217 }
6218
6219
6220 /* Return non-zero if W's cursor intersects rectangle R. */
6221
6222 static int
6223 x_phys_cursor_in_rect_p (w, r)
6224 struct window *w;
6225 Rect *r;
6226 {
6227 Rect cr, result;
6228 struct glyph *cursor_glyph;
6229
6230 cursor_glyph = get_phys_cursor_glyph (w);
6231 if (cursor_glyph)
6232 {
6233 cr.left = w->phys_cursor.x;
6234 cr.top = w->phys_cursor.y;
6235 cr.right = cr.left + cursor_glyph->pixel_width;
6236 cr.bottom = cr.top + w->phys_cursor_height;
6237 return x_intersect_rectangles (&cr, r, &result);
6238 }
6239 else
6240 return 0;
6241 }
6242
6243
6244 /* Redraw the part of window W intersection rectagle FR. Pixel
6245 coordinates in FR are frame relative. Call this function with
6246 input blocked. Value is non-zero if the exposure overwrites
6247 mouse-face. */
6248
6249 static int
6250 expose_window (w, fr)
6251 struct window *w;
6252 Rect *fr;
6253 {
6254 struct frame *f = XFRAME (w->frame);
6255 Rect wr, r;
6256 int mouse_face_overwritten_p = 0;
6257
6258 /* If window is not yet fully initialized, do nothing. This can
6259 happen when toolkit scroll bars are used and a window is split.
6260 Reconfiguring the scroll bar will generate an expose for a newly
6261 created window. */
6262 if (w->current_matrix == NULL)
6263 return 0;
6264
6265 /* When we're currently updating the window, display and current
6266 matrix usually don't agree. Arrange for a thorough display
6267 later. */
6268 if (w == updated_window)
6269 {
6270 SET_FRAME_GARBAGED (f);
6271 return 0;
6272 }
6273
6274 /* Frame-relative pixel rectangle of W. */
6275 wr.left = XFASTINT (w->left) * CANON_X_UNIT (f);
6276 wr.top = XFASTINT (w->top) * CANON_Y_UNIT (f);
6277 wr.right = wr.left + XFASTINT (w->width) * CANON_X_UNIT (f);
6278 wr.bottom = wr.top + XFASTINT (w->height) * CANON_Y_UNIT (f);
6279
6280 if (x_intersect_rectangles (fr, &wr, &r))
6281 {
6282 int yb = window_text_bottom_y (w);
6283 struct glyph_row *row;
6284 int cursor_cleared_p;
6285
6286 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
6287 r.left, r.top, r.right, r.bottom));
6288
6289 /* Convert to window coordinates. */
6290 r.left = FRAME_TO_WINDOW_PIXEL_X (w, r.left);
6291 r.right = FRAME_TO_WINDOW_PIXEL_X (w, r.right);
6292 r.top = FRAME_TO_WINDOW_PIXEL_Y (w, r.top);
6293 r.bottom = FRAME_TO_WINDOW_PIXEL_Y (w, r.bottom);
6294
6295 /* Turn off the cursor. */
6296 if (!w->pseudo_window_p
6297 && x_phys_cursor_in_rect_p (w, &r))
6298 {
6299 x_clear_cursor (w);
6300 cursor_cleared_p = 1;
6301 }
6302 else
6303 cursor_cleared_p = 0;
6304
6305 /* Find the first row intersecting the rectangle R. */
6306 for (row = w->current_matrix->rows;
6307 row->enabled_p;
6308 ++row)
6309 {
6310 int y0 = row->y;
6311 int y1 = MATRIX_ROW_BOTTOM_Y (row);
6312
6313 if ((y0 >= r.top && y0 < r.bottom)
6314 || (y1 > r.top && y1 < r.bottom)
6315 || (r.top >= y0 && r.top < y1)
6316 || (r.bottom > y0 && r.bottom < y1))
6317 {
6318 if (expose_line (w, row, &r))
6319 mouse_face_overwritten_p = 1;
6320 }
6321
6322 if (y1 >= yb)
6323 break;
6324 }
6325
6326 /* Display the mode line if there is one. */
6327 if (WINDOW_WANTS_MODELINE_P (w)
6328 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
6329 row->enabled_p)
6330 && row->y < r.bottom)
6331 {
6332 if (expose_line (w, row, &r))
6333 mouse_face_overwritten_p = 1;
6334 }
6335
6336 if (!w->pseudo_window_p)
6337 {
6338 /* Draw border between windows. */
6339 x_draw_vertical_border (w);
6340
6341 /* Turn the cursor on again. */
6342 if (cursor_cleared_p)
6343 x_update_window_cursor (w, 1);
6344 }
6345 }
6346
6347 /* Display scroll bar for this window. */
6348 if (!NILP (w->vertical_scroll_bar))
6349 {
6350 ControlHandle ch
6351 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
6352
6353 Draw1Control (ch);
6354 }
6355
6356 return mouse_face_overwritten_p;
6357 }
6358
6359 static int
6360 x_intersect_rectangles (r1, r2, result)
6361 Rect *r1, *r2, *result;
6362 {
6363 Rect *left, *right;
6364 Rect *upper, *lower;
6365 int intersection_p = 0;
6366
6367 /* Rerrange so that R1 is the left-most rectangle. */
6368 if (r1->left < r2->left)
6369 left = r1, right = r2;
6370 else
6371 left = r2, right = r1;
6372
6373 /* X0 of the intersection is right.x0, if this is inside R1,
6374 otherwise there is no intersection. */
6375 if (right->left <= left->right)
6376 {
6377 result->left = right->left;
6378
6379 /* The right end of the intersection is the minimum of the
6380 the right ends of left and right. */
6381 result->right = min (left->right, right->right);
6382
6383 /* Same game for Y. */
6384 if (r1->top < r2->top)
6385 upper = r1, lower = r2;
6386 else
6387 upper = r2, lower = r1;
6388
6389 /* The upper end of the intersection is lower.y0, if this is inside
6390 of upper. Otherwise, there is no intersection. */
6391 if (lower->top <= upper->bottom)
6392 {
6393 result->top = lower->top;
6394
6395 /* The lower end of the intersection is the minimum of the lower
6396 ends of upper and lower. */
6397 result->bottom = min (lower->bottom, upper->bottom);
6398 intersection_p = 1;
6399 }
6400 }
6401
6402 return intersection_p;
6403 }
6404
6405
6406
6407
6408 \f
6409 static void
6410 frame_highlight (f)
6411 struct frame *f;
6412 {
6413 x_update_cursor (f, 1);
6414 }
6415
6416 static void
6417 frame_unhighlight (f)
6418 struct frame *f;
6419 {
6420 x_update_cursor (f, 1);
6421 }
6422
6423 /* The focus has changed. Update the frames as necessary to reflect
6424 the new situation. Note that we can't change the selected frame
6425 here, because the Lisp code we are interrupting might become confused.
6426 Each event gets marked with the frame in which it occurred, so the
6427 Lisp code can tell when the switch took place by examining the events. */
6428
6429 static void
6430 x_new_focus_frame (dpyinfo, frame)
6431 struct x_display_info *dpyinfo;
6432 struct frame *frame;
6433 {
6434 struct frame *old_focus = dpyinfo->x_focus_frame;
6435
6436 if (frame != dpyinfo->x_focus_frame)
6437 {
6438 /* Set this before calling other routines, so that they see
6439 the correct value of x_focus_frame. */
6440 dpyinfo->x_focus_frame = frame;
6441
6442 if (old_focus && old_focus->auto_lower)
6443 x_lower_frame (old_focus);
6444
6445 #if 0
6446 selected_frame = frame;
6447 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
6448 selected_frame);
6449 Fselect_window (selected_frame->selected_window);
6450 choose_minibuf_frame ();
6451 #endif /* ! 0 */
6452
6453 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6454 pending_autoraise_frame = dpyinfo->x_focus_frame;
6455 else
6456 pending_autoraise_frame = 0;
6457 }
6458
6459 x_frame_rehighlight (dpyinfo);
6460 }
6461
6462 /* Handle an event saying the mouse has moved out of an Emacs frame. */
6463
6464 void
6465 x_mouse_leave (dpyinfo)
6466 struct x_display_info *dpyinfo;
6467 {
6468 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
6469 }
6470
6471 /* The focus has changed, or we have redirected a frame's focus to
6472 another frame (this happens when a frame uses a surrogate
6473 mini-buffer frame). Shift the highlight as appropriate.
6474
6475 The FRAME argument doesn't necessarily have anything to do with which
6476 frame is being highlighted or un-highlighted; we only use it to find
6477 the appropriate X display info. */
6478
6479 static void
6480 XTframe_rehighlight (frame)
6481 struct frame *frame;
6482 {
6483 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6484 }
6485
6486 static void
6487 x_frame_rehighlight (dpyinfo)
6488 struct x_display_info *dpyinfo;
6489 {
6490 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6491
6492 if (dpyinfo->x_focus_frame)
6493 {
6494 dpyinfo->x_highlight_frame
6495 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6496 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6497 : dpyinfo->x_focus_frame);
6498 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
6499 {
6500 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6501 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
6502 }
6503 }
6504 else
6505 dpyinfo->x_highlight_frame = 0;
6506
6507 if (dpyinfo->x_highlight_frame != old_highlight)
6508 {
6509 if (old_highlight)
6510 frame_unhighlight (old_highlight);
6511 if (dpyinfo->x_highlight_frame)
6512 frame_highlight (dpyinfo->x_highlight_frame);
6513 }
6514 }
6515
6516
6517 \f
6518 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
6519
6520 #if 0 /* MAC_TODO */
6521 /* Initialize mode_switch_bit and modifier_meaning. */
6522 static void
6523 x_find_modifier_meanings (dpyinfo)
6524 struct x_display_info *dpyinfo;
6525 {
6526 int min_code, max_code;
6527 KeySym *syms;
6528 int syms_per_code;
6529 XModifierKeymap *mods;
6530
6531 dpyinfo->meta_mod_mask = 0;
6532 dpyinfo->shift_lock_mask = 0;
6533 dpyinfo->alt_mod_mask = 0;
6534 dpyinfo->super_mod_mask = 0;
6535 dpyinfo->hyper_mod_mask = 0;
6536
6537 #ifdef HAVE_X11R4
6538 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
6539 #else
6540 min_code = dpyinfo->display->min_keycode;
6541 max_code = dpyinfo->display->max_keycode;
6542 #endif
6543
6544 syms = XGetKeyboardMapping (dpyinfo->display,
6545 min_code, max_code - min_code + 1,
6546 &syms_per_code);
6547 mods = XGetModifierMapping (dpyinfo->display);
6548
6549 /* Scan the modifier table to see which modifier bits the Meta and
6550 Alt keysyms are on. */
6551 {
6552 int row, col; /* The row and column in the modifier table. */
6553
6554 for (row = 3; row < 8; row++)
6555 for (col = 0; col < mods->max_keypermod; col++)
6556 {
6557 KeyCode code
6558 = mods->modifiermap[(row * mods->max_keypermod) + col];
6559
6560 /* Zeroes are used for filler. Skip them. */
6561 if (code == 0)
6562 continue;
6563
6564 /* Are any of this keycode's keysyms a meta key? */
6565 {
6566 int code_col;
6567
6568 for (code_col = 0; code_col < syms_per_code; code_col++)
6569 {
6570 int sym = syms[((code - min_code) * syms_per_code) + code_col];
6571
6572 switch (sym)
6573 {
6574 case XK_Meta_L:
6575 case XK_Meta_R:
6576 dpyinfo->meta_mod_mask |= (1 << row);
6577 break;
6578
6579 case XK_Alt_L:
6580 case XK_Alt_R:
6581 dpyinfo->alt_mod_mask |= (1 << row);
6582 break;
6583
6584 case XK_Hyper_L:
6585 case XK_Hyper_R:
6586 dpyinfo->hyper_mod_mask |= (1 << row);
6587 break;
6588
6589 case XK_Super_L:
6590 case XK_Super_R:
6591 dpyinfo->super_mod_mask |= (1 << row);
6592 break;
6593
6594 case XK_Shift_Lock:
6595 /* Ignore this if it's not on the lock modifier. */
6596 if ((1 << row) == LockMask)
6597 dpyinfo->shift_lock_mask = LockMask;
6598 break;
6599 }
6600 }
6601 }
6602 }
6603 }
6604
6605 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
6606 if (! dpyinfo->meta_mod_mask)
6607 {
6608 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6609 dpyinfo->alt_mod_mask = 0;
6610 }
6611
6612 /* If some keys are both alt and meta,
6613 make them just meta, not alt. */
6614 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
6615 {
6616 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
6617 }
6618
6619 XFree ((char *) syms);
6620 XFreeModifiermap (mods);
6621 }
6622
6623 #endif /* MAC_TODO */
6624
6625 /* Convert between the modifier bits X uses and the modifier bits
6626 Emacs uses. */
6627
6628 static unsigned int
6629 x_mac_to_emacs_modifiers (dpyinfo, state)
6630 struct x_display_info *dpyinfo;
6631 unsigned short state;
6632 {
6633 return (((state & shiftKey) ? shift_modifier : 0)
6634 | ((state & controlKey) ? ctrl_modifier : 0)
6635 | ((state & cmdKey) ? meta_modifier : 0)
6636 | ((state & optionKey) ? alt_modifier : 0));
6637 }
6638
6639 #if 0 /* MAC_TODO */
6640 static unsigned short
6641 x_emacs_to_x_modifiers (dpyinfo, state)
6642 struct x_display_info *dpyinfo;
6643 unsigned int state;
6644 {
6645 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
6646 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
6647 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
6648 | ((state & shift_modifier) ? ShiftMask : 0)
6649 | ((state & ctrl_modifier) ? ControlMask : 0)
6650 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
6651 }
6652 #endif /* MAC_TODO */
6653
6654 /* Convert a keysym to its name. */
6655
6656 char *
6657 x_get_keysym_name (keysym)
6658 int keysym;
6659 {
6660 char *value;
6661
6662 BLOCK_INPUT;
6663 #if 0
6664 value = XKeysymToString (keysym);
6665 #else
6666 value = 0;
6667 #endif
6668 UNBLOCK_INPUT;
6669
6670 return value;
6671 }
6672
6673
6674 \f
6675 /* Mouse clicks and mouse movement. Rah. */
6676
6677 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6678 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6679 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6680 not force the value into range. */
6681
6682 void
6683 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
6684 FRAME_PTR f;
6685 register int pix_x, pix_y;
6686 register int *x, *y;
6687 Rect *bounds;
6688 int noclip;
6689 {
6690 /* Support tty mode: if Vwindow_system is nil, behave correctly. */
6691 if (NILP (Vwindow_system))
6692 {
6693 *x = pix_x;
6694 *y = pix_y;
6695 return;
6696 }
6697
6698 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
6699 even for negative values. */
6700 if (pix_x < 0)
6701 pix_x -= FONT_WIDTH (FRAME_FONT (f)) - 1;
6702 if (pix_y < 0)
6703 pix_y -= (f)->output_data.mac->line_height - 1;
6704
6705 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6706 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6707
6708 if (bounds)
6709 {
6710 bounds->left = CHAR_TO_PIXEL_COL (f, pix_x);
6711 bounds->top = CHAR_TO_PIXEL_ROW (f, pix_y);
6712 bounds->right = bounds->left + FONT_WIDTH (FRAME_FONT (f)) - 1;
6713 bounds->bottom = bounds->top + f->output_data.mac->line_height - 1;
6714 }
6715
6716 if (!noclip)
6717 {
6718 if (pix_x < 0)
6719 pix_x = 0;
6720 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6721 pix_x = FRAME_WINDOW_WIDTH (f);
6722
6723 if (pix_y < 0)
6724 pix_y = 0;
6725 else if (pix_y > f->height)
6726 pix_y = f->height;
6727 }
6728
6729 *x = pix_x;
6730 *y = pix_y;
6731 }
6732
6733
6734 /* Given HPOS/VPOS in the current matrix of W, return corresponding
6735 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6736 can't tell the positions because W's display is not up to date,
6737 return 0. */
6738
6739 int
6740 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6741 struct window *w;
6742 int hpos, vpos;
6743 int *frame_x, *frame_y;
6744 {
6745 int success_p;
6746
6747 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6748 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6749
6750 if (display_completed)
6751 {
6752 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6753 struct glyph *glyph = row->glyphs[TEXT_AREA];
6754 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6755
6756 *frame_y = row->y;
6757 *frame_x = row->x;
6758 while (glyph < end)
6759 {
6760 *frame_x += glyph->pixel_width;
6761 ++glyph;
6762 }
6763
6764 success_p = 1;
6765 }
6766 else
6767 {
6768 *frame_y = *frame_x = 0;
6769 success_p = 0;
6770 }
6771
6772 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6773 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6774 return success_p;
6775 }
6776
6777
6778 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6779
6780 If the event is a button press, then note that we have grabbed
6781 the mouse. */
6782
6783 static Lisp_Object
6784 construct_mouse_click (result, event, f)
6785 struct input_event *result;
6786 EventRecord *event;
6787 struct frame *f;
6788 {
6789 Point mouseLoc;
6790
6791 result->kind = MOUSE_CLICK_EVENT;
6792 result->code = 0; /* only one mouse button */
6793 result->timestamp = event->when;
6794 result->modifiers = event->what == mouseDown ? down_modifier : up_modifier;
6795
6796 mouseLoc = event->where;
6797
6798 #if TARGET_API_MAC_CARBON
6799 SetPort (GetWindowPort (FRAME_MAC_WINDOW (f)));
6800 #else
6801 SetPort (FRAME_MAC_WINDOW (f));
6802 #endif
6803
6804 GlobalToLocal (&mouseLoc);
6805 XSETINT (result->x, mouseLoc.h);
6806 XSETINT (result->y, mouseLoc.v);
6807
6808 XSETFRAME (result->frame_or_window, f);
6809
6810 result->arg = Qnil;
6811 return Qnil;
6812 }
6813
6814 \f
6815 /* Function to report a mouse movement to the mainstream Emacs code.
6816 The input handler calls this.
6817
6818 We have received a mouse movement event, which is given in *event.
6819 If the mouse is over a different glyph than it was last time, tell
6820 the mainstream emacs code by setting mouse_moved. If not, ask for
6821 another motion event, so we can check again the next time it moves. */
6822
6823 static Point last_mouse_motion_position;
6824 static Lisp_Object last_mouse_motion_frame;
6825
6826 static void
6827 note_mouse_movement (frame, pos)
6828 FRAME_PTR frame;
6829 Point *pos;
6830 {
6831 #if TARGET_API_MAC_CARBON
6832 Rect r;
6833 #endif
6834
6835 last_mouse_movement_time = TickCount () * (1000 / 60); /* to milliseconds */
6836 last_mouse_motion_position = *pos;
6837 XSETFRAME (last_mouse_motion_frame, frame);
6838
6839 #if TARGET_API_MAC_CARBON
6840 if (!PtInRect (*pos, GetWindowPortBounds (FRAME_MAC_WINDOW (frame), &r)))
6841 #else
6842 if (!PtInRect (*pos, &FRAME_MAC_WINDOW (frame)->portRect))
6843 #endif
6844 {
6845 frame->mouse_moved = 1;
6846 last_mouse_scroll_bar = Qnil;
6847 note_mouse_highlight (frame, -1, -1);
6848 }
6849 /* Has the mouse moved off the glyph it was on at the last sighting? */
6850 else if (pos->h < last_mouse_glyph.left
6851 || pos->h >= last_mouse_glyph.right
6852 || pos->v < last_mouse_glyph.top
6853 || pos->v >= last_mouse_glyph.bottom)
6854 {
6855 frame->mouse_moved = 1;
6856 last_mouse_scroll_bar = Qnil;
6857 note_mouse_highlight (frame, pos->h, pos->v);
6858 }
6859 }
6860
6861 /* This is used for debugging, to turn off note_mouse_highlight. */
6862
6863 int disable_mouse_highlight;
6864
6865
6866 \f
6867 /************************************************************************
6868 Mouse Face
6869 ************************************************************************/
6870
6871 /* Find the glyph under window-relative coordinates X/Y in window W.
6872 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6873 strings. Return in *HPOS and *VPOS the row and column number of
6874 the glyph found. Return in *AREA the glyph area containing X.
6875 Value is a pointer to the glyph found or null if X/Y is not on
6876 text, or we can't tell because W's current matrix is not up to
6877 date. */
6878
6879 static struct glyph *
6880 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
6881 struct window *w;
6882 int x, y;
6883 int *hpos, *vpos, *area;
6884 int buffer_only_p;
6885 {
6886 struct glyph *glyph, *end;
6887 struct glyph_row *row = NULL;
6888 int x0, i, left_area_width;
6889
6890 /* Find row containing Y. Give up if some row is not enabled. */
6891 for (i = 0; i < w->current_matrix->nrows; ++i)
6892 {
6893 row = MATRIX_ROW (w->current_matrix, i);
6894 if (!row->enabled_p)
6895 return NULL;
6896 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6897 break;
6898 }
6899
6900 *vpos = i;
6901 *hpos = 0;
6902
6903 /* Give up if Y is not in the window. */
6904 if (i == w->current_matrix->nrows)
6905 return NULL;
6906
6907 /* Get the glyph area containing X. */
6908 if (w->pseudo_window_p)
6909 {
6910 *area = TEXT_AREA;
6911 x0 = 0;
6912 }
6913 else
6914 {
6915 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6916 if (x < left_area_width)
6917 {
6918 *area = LEFT_MARGIN_AREA;
6919 x0 = 0;
6920 }
6921 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6922 {
6923 *area = TEXT_AREA;
6924 x0 = row->x + left_area_width;
6925 }
6926 else
6927 {
6928 *area = RIGHT_MARGIN_AREA;
6929 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6930 }
6931 }
6932
6933 /* Find glyph containing X. */
6934 glyph = row->glyphs[*area];
6935 end = glyph + row->used[*area];
6936 while (glyph < end)
6937 {
6938 if (x < x0 + glyph->pixel_width)
6939 {
6940 if (w->pseudo_window_p)
6941 break;
6942 else if (!buffer_only_p || BUFFERP (glyph->object))
6943 break;
6944 }
6945
6946 x0 += glyph->pixel_width;
6947 ++glyph;
6948 }
6949
6950 if (glyph == end)
6951 return NULL;
6952
6953 *hpos = glyph - row->glyphs[*area];
6954 return glyph;
6955 }
6956
6957
6958 /* Convert frame-relative x/y to coordinates relative to window W.
6959 Takes pseudo-windows into account. */
6960
6961 static void
6962 frame_to_window_pixel_xy (w, x, y)
6963 struct window *w;
6964 int *x, *y;
6965 {
6966 if (w->pseudo_window_p)
6967 {
6968 /* A pseudo-window is always full-width, and starts at the
6969 left edge of the frame, plus a frame border. */
6970 struct frame *f = XFRAME (w->frame);
6971 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6972 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6973 }
6974 else
6975 {
6976 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6977 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6978 }
6979 }
6980
6981
6982 /* Take proper action when mouse has moved to the mode or header line of
6983 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6984 mode line. X is relative to the start of the text display area of
6985 W, so the width of fringes and scroll bars must be subtracted
6986 to get a position relative to the start of the mode line. */
6987
6988 static void
6989 note_mode_line_highlight (w, x, mode_line_p)
6990 struct window *w;
6991 int x, mode_line_p;
6992 {
6993 struct frame *f = XFRAME (w->frame);
6994 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
6995 struct Cursor *cursor = dpyinfo->vertical_scroll_bar_cursor;
6996 struct glyph_row *row;
6997
6998 if (mode_line_p)
6999 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
7000 else
7001 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
7002
7003 if (row->enabled_p)
7004 {
7005 struct glyph *glyph, *end;
7006 Lisp_Object help, map;
7007 int x0;
7008
7009 /* Find the glyph under X. */
7010 glyph = row->glyphs[TEXT_AREA];
7011 end = glyph + row->used[TEXT_AREA];
7012 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
7013 + FRAME_X_LEFT_FRINGE_WIDTH (f));
7014
7015 while (glyph < end
7016 && x >= x0 + glyph->pixel_width)
7017 {
7018 x0 += glyph->pixel_width;
7019 ++glyph;
7020 }
7021
7022 if (glyph < end
7023 && STRINGP (glyph->object)
7024 && STRING_INTERVALS (glyph->object)
7025 && glyph->charpos >= 0
7026 && glyph->charpos < SCHARS (glyph->object))
7027 {
7028 /* If we're on a string with `help-echo' text property,
7029 arrange for the help to be displayed. This is done by
7030 setting the global variable help_echo to the help string. */
7031 help = Fget_text_property (make_number (glyph->charpos),
7032 Qhelp_echo, glyph->object);
7033 if (!NILP (help))
7034 {
7035 help_echo = help;
7036 XSETWINDOW (help_echo_window, w);
7037 help_echo_object = glyph->object;
7038 help_echo_pos = glyph->charpos;
7039 }
7040
7041 /* Change the mouse pointer according to what is under X/Y. */
7042 map = Fget_text_property (make_number (glyph->charpos),
7043 Qlocal_map, glyph->object);
7044 if (KEYMAPP (map))
7045 cursor = f->output_data.mac->nontext_cursor;
7046 else
7047 {
7048 map = Fget_text_property (make_number (glyph->charpos),
7049 Qkeymap, glyph->object);
7050 if (KEYMAPP (map))
7051 cursor = f->output_data.mac->nontext_cursor;
7052 }
7053 }
7054 }
7055
7056 #if 0 /* MAC_TODO: mouse cursor */
7057 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7058 #endif
7059 }
7060
7061
7062 /* Take proper action when the mouse has moved to position X, Y on
7063 frame F as regards highlighting characters that have mouse-face
7064 properties. Also de-highlighting chars where the mouse was before.
7065 X and Y can be negative or out of range. */
7066
7067 static void
7068 note_mouse_highlight (f, x, y)
7069 struct frame *f;
7070 int x, y;
7071 {
7072 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
7073 int portion;
7074 Lisp_Object window;
7075 struct window *w;
7076 struct buffer *b;
7077
7078 #if 0
7079 /* When a menu is active, don't highlight because this looks odd. */
7080 if (popup_activated ())
7081 return;
7082 #endif
7083
7084 if (NILP (Vmouse_highlight)
7085 || !f->glyphs_initialized_p)
7086 return;
7087
7088 dpyinfo->mouse_face_mouse_x = x;
7089 dpyinfo->mouse_face_mouse_y = y;
7090 dpyinfo->mouse_face_mouse_frame = f;
7091
7092 if (dpyinfo->mouse_face_defer)
7093 return;
7094
7095 if (gc_in_progress)
7096 {
7097 dpyinfo->mouse_face_deferred_gc = 1;
7098 return;
7099 }
7100
7101 /* Which window is that in? */
7102 window = window_from_coordinates (f, x, y, &portion, 1);
7103
7104 /* If we were displaying active text in another window, clear that. */
7105 if (! EQ (window, dpyinfo->mouse_face_window))
7106 clear_mouse_face (dpyinfo);
7107
7108 /* Not on a window -> return. */
7109 if (!WINDOWP (window))
7110 return;
7111
7112 /* Reset help_echo. It will get recomputed below. */
7113 help_echo = Qnil;
7114
7115 /* Convert to window-relative pixel coordinates. */
7116 w = XWINDOW (window);
7117 frame_to_window_pixel_xy (w, &x, &y);
7118
7119 /* Handle tool-bar window differently since it doesn't display a
7120 buffer. */
7121 if (EQ (window, f->tool_bar_window))
7122 {
7123 note_tool_bar_highlight (f, x, y);
7124 return;
7125 }
7126
7127 /* Mouse is on the mode or header line? */
7128 if (portion == 1 || portion == 3)
7129 {
7130 note_mode_line_highlight (w, x, portion == 1);
7131 return;
7132 }
7133 #if 0 /* TODO: mouse cursor */
7134 if (portion == 2)
7135 cursor = f->output_data.x->horizontal_drag_cursor;
7136 else
7137 cursor = f->output_data.x->text_cursor;
7138 #endif
7139 /* Are we in a window whose display is up to date?
7140 And verify the buffer's text has not changed. */
7141 b = XBUFFER (w->buffer);
7142 if (/* Within text portion of the window. */
7143 portion == 0
7144 && EQ (w->window_end_valid, w->buffer)
7145 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
7146 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
7147 {
7148 int hpos, vpos, pos, i, area;
7149 struct glyph *glyph;
7150 Lisp_Object object;
7151 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
7152 Lisp_Object *overlay_vec = NULL;
7153 int len, noverlays;
7154 struct buffer *obuf;
7155 int obegv, ozv, same_region;
7156
7157 /* Find the glyph under X/Y. */
7158 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
7159
7160 /* Clear mouse face if X/Y not over text. */
7161 if (glyph == NULL
7162 || area != TEXT_AREA
7163 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
7164 {
7165 clear_mouse_face (dpyinfo);
7166 /* TODO: mouse cursor */
7167 goto set_cursor;
7168 }
7169
7170 pos = glyph->charpos;
7171 object = glyph->object;
7172 if (!STRINGP (object) && !BUFFERP (object))
7173 goto set_cursor;
7174
7175 /* If we get an out-of-range value, return now; avoid an error. */
7176 if (BUFFERP (object) && pos > BUF_Z (b))
7177 goto set_cursor;
7178
7179 /* Make the window's buffer temporarily current for
7180 overlays_at and compute_char_face. */
7181 obuf = current_buffer;
7182 current_buffer = b;
7183 obegv = BEGV;
7184 ozv = ZV;
7185 BEGV = BEG;
7186 ZV = Z;
7187
7188 /* Is this char mouse-active or does it have help-echo? */
7189 position = make_number (pos);
7190
7191 if (BUFFERP (object))
7192 {
7193 /* Put all the overlays we want in a vector in overlay_vec.
7194 Store the length in len. If there are more than 10, make
7195 enough space for all, and try again. */
7196 len = 10;
7197 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
7198 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
7199 if (noverlays > len)
7200 {
7201 len = noverlays;
7202 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
7203 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
7204 }
7205
7206 /* Sort overlays into increasing priority order. */
7207 noverlays = sort_overlays (overlay_vec, noverlays, w);
7208 }
7209 else
7210 noverlays = 0;
7211
7212 same_region = (EQ (window, dpyinfo->mouse_face_window)
7213 && vpos >= dpyinfo->mouse_face_beg_row
7214 && vpos <= dpyinfo->mouse_face_end_row
7215 && (vpos > dpyinfo->mouse_face_beg_row
7216 || hpos >= dpyinfo->mouse_face_beg_col)
7217 && (vpos < dpyinfo->mouse_face_end_row
7218 || hpos < dpyinfo->mouse_face_end_col
7219 || dpyinfo->mouse_face_past_end));
7220
7221 /* TODO: if (same_region)
7222 mouse cursor */
7223
7224 /* Check mouse-face highlighting. */
7225 if (! same_region
7226 /* If there exists an overlay with mouse-face overlapping
7227 the one we are currently highlighting, we have to
7228 check if we enter the overlapping overlay, and then
7229 highlight that. */
7230 || (OVERLAYP (dpyinfo->mouse_face_overlay)
7231 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
7232 {
7233 /* Find the highest priority overlay that has a mouse-face
7234 property. */
7235 overlay = Qnil;
7236 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
7237 {
7238 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
7239 if (!NILP (mouse_face))
7240 overlay = overlay_vec[i];
7241 }
7242
7243 /* If we're actually highlighting the same overlay as
7244 before, there's no need to do that again. */
7245 if (!NILP (overlay)
7246 && EQ (overlay, dpyinfo->mouse_face_overlay))
7247 goto check_help_echo;
7248
7249 dpyinfo->mouse_face_overlay = overlay;
7250
7251 /* Clear the display of the old active region, if any. */
7252 clear_mouse_face (dpyinfo);
7253 /* TODO: mouse cursor changes. */
7254
7255 /* If no overlay applies, get a text property. */
7256 if (NILP (overlay))
7257 mouse_face = Fget_text_property (position, Qmouse_face, object);
7258
7259 /* Handle the overlay case. */
7260 if (!NILP (overlay))
7261 {
7262 /* Find the range of text around this char that
7263 should be active. */
7264 Lisp_Object before, after;
7265 int ignore;
7266
7267 before = Foverlay_start (overlay);
7268 after = Foverlay_end (overlay);
7269 /* Record this as the current active region. */
7270 fast_find_position (w, XFASTINT (before),
7271 &dpyinfo->mouse_face_beg_col,
7272 &dpyinfo->mouse_face_beg_row,
7273 &dpyinfo->mouse_face_beg_x,
7274 &dpyinfo->mouse_face_beg_y, Qnil);
7275
7276 dpyinfo->mouse_face_past_end
7277 = !fast_find_position (w, XFASTINT (after),
7278 &dpyinfo->mouse_face_end_col,
7279 &dpyinfo->mouse_face_end_row,
7280 &dpyinfo->mouse_face_end_x,
7281 &dpyinfo->mouse_face_end_y, Qnil);
7282 dpyinfo->mouse_face_window = window;
7283
7284 dpyinfo->mouse_face_face_id
7285 = face_at_buffer_position (w, pos, 0, 0,
7286 &ignore, pos + 1, 1);
7287
7288 /* Display it as active. */
7289 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7290 /* TODO: mouse cursor changes. */
7291 }
7292 /* Handle the text property case. */
7293 else if (! NILP (mouse_face) && BUFFERP (object))
7294 {
7295 /* Find the range of text around this char that
7296 should be active. */
7297 Lisp_Object before, after, beginning, end;
7298 int ignore;
7299
7300 beginning = Fmarker_position (w->start);
7301 end = make_number (BUF_Z (XBUFFER (object))
7302 - XFASTINT (w->window_end_pos));
7303 before
7304 = Fprevious_single_property_change (make_number (pos + 1),
7305 Qmouse_face,
7306 object, beginning);
7307 after
7308 = Fnext_single_property_change (position, Qmouse_face,
7309 object, end);
7310
7311 /* Record this as the current active region. */
7312 fast_find_position (w, XFASTINT (before),
7313 &dpyinfo->mouse_face_beg_col,
7314 &dpyinfo->mouse_face_beg_row,
7315 &dpyinfo->mouse_face_beg_x,
7316 &dpyinfo->mouse_face_beg_y, Qnil);
7317 dpyinfo->mouse_face_past_end
7318 = !fast_find_position (w, XFASTINT (after),
7319 &dpyinfo->mouse_face_end_col,
7320 &dpyinfo->mouse_face_end_row,
7321 &dpyinfo->mouse_face_end_x,
7322 &dpyinfo->mouse_face_end_y, Qnil);
7323 dpyinfo->mouse_face_window = window;
7324
7325 if (BUFFERP (object))
7326 dpyinfo->mouse_face_face_id
7327 = face_at_buffer_position (w, pos, 0, 0,
7328 &ignore, pos + 1, 1);
7329
7330 /* Display it as active. */
7331 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7332 /* TODO: mouse cursor changes. */
7333 }
7334 else if (!NILP (mouse_face) && STRINGP (object))
7335 {
7336 Lisp_Object b, e;
7337 int ignore;
7338
7339 b = Fprevious_single_property_change (make_number (pos + 1),
7340 Qmouse_face,
7341 object, Qnil);
7342 e = Fnext_single_property_change (position, Qmouse_face,
7343 object, Qnil);
7344 if (NILP (b))
7345 b = make_number (0);
7346 if (NILP (e))
7347 e = make_number (SCHARS (object) - 1);
7348 fast_find_string_pos (w, XINT (b), object,
7349 &dpyinfo->mouse_face_beg_col,
7350 &dpyinfo->mouse_face_beg_row,
7351 &dpyinfo->mouse_face_beg_x,
7352 &dpyinfo->mouse_face_beg_y, 0);
7353 fast_find_string_pos (w, XINT (e), object,
7354 &dpyinfo->mouse_face_end_col,
7355 &dpyinfo->mouse_face_end_row,
7356 &dpyinfo->mouse_face_end_x,
7357 &dpyinfo->mouse_face_end_y, 1);
7358 dpyinfo->mouse_face_past_end = 0;
7359 dpyinfo->mouse_face_window = window;
7360 dpyinfo->mouse_face_face_id
7361 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
7362 glyph->face_id, 1);
7363 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7364 /* TODO: mouse cursor changes. */
7365 }
7366 else if (STRINGP (object) && NILP (mouse_face))
7367 {
7368 /* A string which doesn't have mouse-face, but
7369 the text ``under'' it might have. */
7370 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
7371 int start = MATRIX_ROW_START_CHARPOS (r);
7372
7373 pos = string_buffer_position (w, object, start);
7374 if (pos > 0)
7375 mouse_face = get_char_property_and_overlay (make_number (pos),
7376 Qmouse_face,
7377 w->buffer,
7378 &overlay);
7379 if (!NILP (mouse_face) && !NILP (overlay))
7380 {
7381 Lisp_Object before = Foverlay_start (overlay);
7382 Lisp_Object after = Foverlay_end (overlay);
7383 int ignore;
7384
7385 /* Note that we might not be able to find position
7386 BEFORE in the glyph matrix if the overlay is
7387 entirely covered by a `display' property. In
7388 this case, we overshoot. So let's stop in
7389 the glyph matrix before glyphs for OBJECT. */
7390 fast_find_position (w, XFASTINT (before),
7391 &dpyinfo->mouse_face_beg_col,
7392 &dpyinfo->mouse_face_beg_row,
7393 &dpyinfo->mouse_face_beg_x,
7394 &dpyinfo->mouse_face_beg_y,
7395 object);
7396
7397 dpyinfo->mouse_face_past_end
7398 = !fast_find_position (w, XFASTINT (after),
7399 &dpyinfo->mouse_face_end_col,
7400 &dpyinfo->mouse_face_end_row,
7401 &dpyinfo->mouse_face_end_x,
7402 &dpyinfo->mouse_face_end_y,
7403 Qnil);
7404 dpyinfo->mouse_face_window = window;
7405 dpyinfo->mouse_face_face_id
7406 = face_at_buffer_position (w, pos, 0, 0,
7407 &ignore, pos + 1, 1);
7408
7409 /* Display it as active. */
7410 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7411 /* TODO: mouse cursor changes. */
7412 }
7413 }
7414 }
7415
7416 check_help_echo:
7417
7418 /* Look for a `help-echo' property. */
7419 {
7420 Lisp_Object help, overlay;
7421
7422 /* Check overlays first. */
7423 help = overlay = Qnil;
7424 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
7425 {
7426 overlay = overlay_vec[i];
7427 help = Foverlay_get (overlay, Qhelp_echo);
7428 }
7429
7430 if (!NILP (help))
7431 {
7432 help_echo = help;
7433 help_echo_window = window;
7434 help_echo_object = overlay;
7435 help_echo_pos = pos;
7436 }
7437 else
7438 {
7439 Lisp_Object object = glyph->object;
7440 int charpos = glyph->charpos;
7441
7442 /* Try text properties. */
7443 if (STRINGP (object)
7444 && charpos >= 0
7445 && charpos < SCHARS (object))
7446 {
7447 help = Fget_text_property (make_number (charpos),
7448 Qhelp_echo, object);
7449 if (NILP (help))
7450 {
7451 /* If the string itself doesn't specify a help-echo,
7452 see if the buffer text ``under'' it does. */
7453 struct glyph_row *r
7454 = MATRIX_ROW (w->current_matrix, vpos);
7455 int start = MATRIX_ROW_START_CHARPOS (r);
7456 int pos = string_buffer_position (w, object, start);
7457 if (pos > 0)
7458 {
7459 help = Fget_char_property (make_number (pos),
7460 Qhelp_echo, w->buffer);
7461 if (!NILP (help))
7462 {
7463 charpos = pos;
7464 object = w->buffer;
7465 }
7466 }
7467 }
7468 }
7469 else if (BUFFERP (object)
7470 && charpos >= BEGV
7471 && charpos < ZV)
7472 help = Fget_text_property (make_number (charpos), Qhelp_echo,
7473 object);
7474
7475 if (!NILP (help))
7476 {
7477 help_echo = help;
7478 help_echo_window = window;
7479 help_echo_object = object;
7480 help_echo_pos = charpos;
7481 }
7482 }
7483 }
7484
7485 BEGV = obegv;
7486 ZV = ozv;
7487 current_buffer = obuf;
7488 }
7489
7490 set_cursor:
7491 /* TODO: mouse cursor changes. */
7492 ;
7493 }
7494
7495 static void
7496 redo_mouse_highlight ()
7497 {
7498 if (!NILP (last_mouse_motion_frame)
7499 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
7500 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
7501 last_mouse_motion_position.h,
7502 last_mouse_motion_position.v);
7503 }
7504
7505
7506 \f
7507 /***********************************************************************
7508 Tool-bars
7509 ***********************************************************************/
7510
7511 static int x_tool_bar_item P_ ((struct frame *, int, int,
7512 struct glyph **, int *, int *, int *));
7513
7514 /* Tool-bar item index of the item on which a mouse button was pressed
7515 or -1. */
7516
7517 static int last_tool_bar_item;
7518
7519
7520 /* Get information about the tool-bar item at position X/Y on frame F.
7521 Return in *GLYPH a pointer to the glyph of the tool-bar item in
7522 the current matrix of the tool-bar window of F, or NULL if not
7523 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
7524 item in F->current_tool_bar_items. Value is
7525
7526 -1 if X/Y is not on a tool-bar item
7527 0 if X/Y is on the same item that was highlighted before.
7528 1 otherwise. */
7529
7530 static int
7531 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
7532 struct frame *f;
7533 int x, y;
7534 struct glyph **glyph;
7535 int *hpos, *vpos, *prop_idx;
7536 {
7537 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
7538 struct window *w = XWINDOW (f->tool_bar_window);
7539 int area;
7540
7541 /* Find the glyph under X/Y. */
7542 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
7543 if (*glyph == NULL)
7544 return -1;
7545
7546 /* Get the start of this tool-bar item's properties in
7547 f->current_tool_bar_items. */
7548 if (!tool_bar_item_info (f, *glyph, prop_idx))
7549 return -1;
7550
7551 /* Is mouse on the highlighted item? */
7552 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
7553 && *vpos >= dpyinfo->mouse_face_beg_row
7554 && *vpos <= dpyinfo->mouse_face_end_row
7555 && (*vpos > dpyinfo->mouse_face_beg_row
7556 || *hpos >= dpyinfo->mouse_face_beg_col)
7557 && (*vpos < dpyinfo->mouse_face_end_row
7558 || *hpos < dpyinfo->mouse_face_end_col
7559 || dpyinfo->mouse_face_past_end))
7560 return 0;
7561
7562 return 1;
7563 }
7564
7565
7566 /* Handle mouse button event on the tool-bar of frame F, at
7567 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
7568 or ButtonRelase. */
7569
7570 static void
7571 x_handle_tool_bar_click (f, button_event)
7572 struct frame *f;
7573 EventRecord *button_event;
7574 {
7575 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
7576 struct window *w = XWINDOW (f->tool_bar_window);
7577 int hpos, vpos, prop_idx;
7578 struct glyph *glyph;
7579 Lisp_Object enabled_p;
7580 int x = button_event->where.h;
7581 int y = button_event->where.v;
7582
7583 /* If not on the highlighted tool-bar item, return. */
7584 frame_to_window_pixel_xy (w, &x, &y);
7585 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
7586 return;
7587
7588 /* If item is disabled, do nothing. */
7589 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7590 if (NILP (enabled_p))
7591 return;
7592
7593 if (button_event->what == mouseDown)
7594 {
7595 /* Show item in pressed state. */
7596 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7597 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
7598 last_tool_bar_item = prop_idx;
7599 }
7600 else
7601 {
7602 Lisp_Object key, frame;
7603 struct input_event event;
7604
7605 /* Show item in released state. */
7606 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7607 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7608
7609 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
7610
7611 XSETFRAME (frame, f);
7612 event.kind = TOOL_BAR_EVENT;
7613 event.frame_or_window = frame;
7614 event.arg = frame;
7615 kbd_buffer_store_event (&event);
7616
7617 event.kind = TOOL_BAR_EVENT;
7618 event.frame_or_window = frame;
7619 event.arg = key;
7620 event.modifiers = x_mac_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
7621 button_event->modifiers);
7622 kbd_buffer_store_event (&event);
7623 last_tool_bar_item = -1;
7624 }
7625 }
7626
7627
7628 /* Possibly highlight a tool-bar item on frame F when mouse moves to
7629 tool-bar window-relative coordinates X/Y. Called from
7630 note_mouse_highlight. */
7631
7632 static void
7633 note_tool_bar_highlight (f, x, y)
7634 struct frame *f;
7635 int x, y;
7636 {
7637 Lisp_Object window = f->tool_bar_window;
7638 struct window *w = XWINDOW (window);
7639 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
7640 int hpos, vpos;
7641 struct glyph *glyph;
7642 struct glyph_row *row;
7643 int i;
7644 Lisp_Object enabled_p;
7645 int prop_idx;
7646 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
7647 int mouse_down_p, rc;
7648
7649 /* Function note_mouse_highlight is called with negative x(y
7650 values when mouse moves outside of the frame. */
7651 if (x <= 0 || y <= 0)
7652 {
7653 clear_mouse_face (dpyinfo);
7654 return;
7655 }
7656
7657 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
7658 if (rc < 0)
7659 {
7660 /* Not on tool-bar item. */
7661 clear_mouse_face (dpyinfo);
7662 return;
7663 }
7664 else if (rc == 0)
7665 /* On same tool-bar item as before. */
7666 goto set_help_echo;
7667
7668 clear_mouse_face (dpyinfo);
7669
7670 /* Mouse is down, but on different tool-bar item? */
7671 mouse_down_p = (dpyinfo->grabbed
7672 && f == last_mouse_frame
7673 && FRAME_LIVE_P (f));
7674 if (mouse_down_p
7675 && last_tool_bar_item != prop_idx)
7676 return;
7677
7678 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7679 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7680
7681 /* If tool-bar item is not enabled, don't highlight it. */
7682 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7683 if (!NILP (enabled_p))
7684 {
7685 /* Compute the x-position of the glyph. In front and past the
7686 image is a space. We include this is the highlighted area. */
7687 row = MATRIX_ROW (w->current_matrix, vpos);
7688 for (i = x = 0; i < hpos; ++i)
7689 x += row->glyphs[TEXT_AREA][i].pixel_width;
7690
7691 /* Record this as the current active region. */
7692 dpyinfo->mouse_face_beg_col = hpos;
7693 dpyinfo->mouse_face_beg_row = vpos;
7694 dpyinfo->mouse_face_beg_x = x;
7695 dpyinfo->mouse_face_beg_y = row->y;
7696 dpyinfo->mouse_face_past_end = 0;
7697
7698 dpyinfo->mouse_face_end_col = hpos + 1;
7699 dpyinfo->mouse_face_end_row = vpos;
7700 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7701 dpyinfo->mouse_face_end_y = row->y;
7702 dpyinfo->mouse_face_window = window;
7703 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
7704
7705 /* Display it as active. */
7706 show_mouse_face (dpyinfo, draw);
7707 dpyinfo->mouse_face_image_state = draw;
7708 }
7709
7710 set_help_echo:
7711
7712 /* Set help_echo to a help string.to display for this tool-bar item.
7713 XTread_socket does the rest. */
7714 help_echo_object = help_echo_window = Qnil;
7715 help_echo_pos = -1;
7716 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
7717 if (NILP (help_echo))
7718 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
7719 }
7720
7721
7722 \f
7723 /* Find the glyph matrix position of buffer position CHARPOS in window
7724 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7725 current glyphs must be up to date. If CHARPOS is above window
7726 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
7727 of last line in W. In the row containing CHARPOS, stop before glyphs
7728 having STOP as object. */
7729
7730 #if 0 /* This is a version of fast_find_position that's more correct
7731 in the presence of hscrolling, for example. I didn't install
7732 it right away because the problem fixed is minor, it failed
7733 in 20.x as well, and I think it's too risky to install
7734 so near the release of 21.1. 2001-09-25 gerd. */
7735
7736 static int
7737 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
7738 struct window *w;
7739 int charpos;
7740 int *hpos, *vpos, *x, *y;
7741 Lisp_Object stop;
7742 {
7743 struct glyph_row *row, *first;
7744 struct glyph *glyph, *end;
7745 int i, past_end = 0;
7746
7747 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7748 row = row_containing_pos (w, charpos, first, NULL, 0);
7749 if (row == NULL)
7750 {
7751 if (charpos < MATRIX_ROW_START_CHARPOS (first))
7752 {
7753 *x = *y = *hpos = *vpos = 0;
7754 return 0;
7755 }
7756 else
7757 {
7758 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
7759 past_end = 1;
7760 }
7761 }
7762
7763 *x = row->x;
7764 *y = row->y;
7765 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7766
7767 glyph = row->glyphs[TEXT_AREA];
7768 end = glyph + row->used[TEXT_AREA];
7769
7770 /* Skip over glyphs not having an object at the start of the row.
7771 These are special glyphs like truncation marks on terminal
7772 frames. */
7773 if (row->displays_text_p)
7774 while (glyph < end
7775 && INTEGERP (glyph->object)
7776 && !EQ (stop, glyph->object)
7777 && glyph->charpos < 0)
7778 {
7779 *x += glyph->pixel_width;
7780 ++glyph;
7781 }
7782
7783 while (glyph < end
7784 && !INTEGERP (glyph->object)
7785 && !EQ (stop, glyph->object)
7786 && (!BUFFERP (glyph->object)
7787 || glyph->charpos < charpos))
7788 {
7789 *x += glyph->pixel_width;
7790 ++glyph;
7791 }
7792
7793 *hpos = glyph - row->glyphs[TEXT_AREA];
7794 return past_end;
7795 }
7796
7797 #else /* not 0 */
7798
7799 static int
7800 fast_find_position (w, pos, hpos, vpos, x, y, stop)
7801 struct window *w;
7802 int pos;
7803 int *hpos, *vpos, *x, *y;
7804 Lisp_Object stop;
7805 {
7806 int i;
7807 int lastcol;
7808 int maybe_next_line_p = 0;
7809 int line_start_position;
7810 int yb = window_text_bottom_y (w);
7811 struct glyph_row *row, *best_row;
7812 int row_vpos, best_row_vpos;
7813 int current_x;
7814
7815 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7816 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7817
7818 while (row->y < yb)
7819 {
7820 if (row->used[TEXT_AREA])
7821 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7822 else
7823 line_start_position = 0;
7824
7825 if (line_start_position > pos)
7826 break;
7827 /* If the position sought is the end of the buffer,
7828 don't include the blank lines at the bottom of the window. */
7829 else if (line_start_position == pos
7830 && pos == BUF_ZV (XBUFFER (w->buffer)))
7831 {
7832 maybe_next_line_p = 1;
7833 break;
7834 }
7835 else if (line_start_position > 0)
7836 {
7837 best_row = row;
7838 best_row_vpos = row_vpos;
7839 }
7840
7841 if (row->y + row->height >= yb)
7842 break;
7843
7844 ++row;
7845 ++row_vpos;
7846 }
7847
7848 /* Find the right column within BEST_ROW. */
7849 lastcol = 0;
7850 current_x = best_row->x;
7851 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
7852 {
7853 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7854 int charpos = glyph->charpos;
7855
7856 if (BUFFERP (glyph->object))
7857 {
7858 if (charpos == pos)
7859 {
7860 *hpos = i;
7861 *vpos = best_row_vpos;
7862 *x = current_x;
7863 *y = best_row->y;
7864 return 1;
7865 }
7866 else if (charpos > pos)
7867 break;
7868 }
7869 else if (EQ (glyph->object, stop))
7870 break;
7871
7872 if (charpos > 0)
7873 lastcol = i;
7874 current_x += glyph->pixel_width;
7875 }
7876
7877 /* If we're looking for the end of the buffer,
7878 and we didn't find it in the line we scanned,
7879 use the start of the following line. */
7880 if (maybe_next_line_p)
7881 {
7882 ++best_row;
7883 ++best_row_vpos;
7884 lastcol = 0;
7885 current_x = best_row->x;
7886 }
7887
7888 *vpos = best_row_vpos;
7889 *hpos = lastcol + 1;
7890 *x = current_x;
7891 *y = best_row->y;
7892 return 0;
7893 }
7894
7895 #endif /* not 0 */
7896
7897
7898 /* Find the position of the glyph for position POS in OBJECT in
7899 window W's current matrix, and return in *X/*Y the pixel
7900 coordinates, and return in *HPOS/*VPOS the column/row of the glyph.
7901
7902 RIGHT_P non-zero means return the position of the right edge of the
7903 glyph, RIGHT_P zero means return the left edge position.
7904
7905 If no glyph for POS exists in the matrix, return the position of
7906 the glyph with the next smaller position that is in the matrix, if
7907 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
7908 exists in the matrix, return the position of the glyph with the
7909 next larger position in OBJECT.
7910
7911 Value is non-zero if a glyph was found. */
7912
7913 static int
7914 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
7915 struct window *w;
7916 int pos;
7917 Lisp_Object object;
7918 int *hpos, *vpos, *x, *y;
7919 int right_p;
7920 {
7921 int yb = window_text_bottom_y (w);
7922 struct glyph_row *r;
7923 struct glyph *best_glyph = NULL;
7924 struct glyph_row *best_row = NULL;
7925 int best_x = 0;
7926
7927 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7928 r->enabled_p && r->y < yb;
7929 ++r)
7930 {
7931 struct glyph *g = r->glyphs[TEXT_AREA];
7932 struct glyph *e = g + r->used[TEXT_AREA];
7933 int gx;
7934
7935 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
7936 if (EQ (g->object, object))
7937 {
7938 if (g->charpos == pos)
7939 {
7940 best_glyph = g;
7941 best_x = gx;
7942 best_row = r;
7943 goto found;
7944 }
7945 else if (best_glyph == NULL
7946 || ((abs (g->charpos - pos)
7947 < abs (best_glyph->charpos - pos))
7948 && (right_p
7949 ? g->charpos < pos
7950 : g->charpos > pos)))
7951 {
7952 best_glyph = g;
7953 best_x = gx;
7954 best_row = r;
7955 }
7956 }
7957 }
7958
7959 found:
7960
7961 if (best_glyph)
7962 {
7963 *x = best_x;
7964 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
7965
7966 if (right_p)
7967 {
7968 *x += best_glyph->pixel_width;
7969 ++*hpos;
7970 }
7971
7972 *y = best_row->y;
7973 *vpos = best_row - w->current_matrix->rows;
7974 }
7975
7976 return best_glyph != NULL;
7977 }
7978
7979
7980 /* Display the active region described by mouse_face_*
7981 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7982
7983 static void
7984 show_mouse_face (dpyinfo, draw)
7985 struct mac_display_info *dpyinfo;
7986 enum draw_glyphs_face draw;
7987 {
7988 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
7989 struct frame *f = XFRAME (WINDOW_FRAME (w));
7990
7991 if (/* If window is in the process of being destroyed, don't bother
7992 to do anything. */
7993 w->current_matrix != NULL
7994 /* Don't update mouse highlight if hidden */
7995 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
7996 /* Recognize when we are called to operate on rows that don't exist
7997 anymore. This can happen when a window is split. */
7998 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
7999 {
8000 int phys_cursor_on_p = w->phys_cursor_on_p;
8001 struct glyph_row *row, *first, *last;
8002
8003 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
8004 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
8005
8006 for (row = first; row <= last && row->enabled_p; ++row)
8007 {
8008 int start_hpos, end_hpos, start_x;
8009
8010 /* For all but the first row, the highlight starts at column 0. */
8011 if (row == first)
8012 {
8013 start_hpos = dpyinfo->mouse_face_beg_col;
8014 start_x = dpyinfo->mouse_face_beg_x;
8015 }
8016 else
8017 {
8018 start_hpos = 0;
8019 start_x = 0;
8020 }
8021
8022 if (row == last)
8023 end_hpos = dpyinfo->mouse_face_end_col;
8024 else
8025 end_hpos = row->used[TEXT_AREA];
8026
8027 if (end_hpos > start_hpos)
8028 {
8029 x_draw_glyphs (w, start_x, row, TEXT_AREA,
8030 start_hpos, end_hpos, draw, 0);
8031
8032 row->mouse_face_p
8033 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
8034 }
8035 }
8036
8037 /* When we've written over the cursor, arrange for it to
8038 be displayed again. */
8039 if (phys_cursor_on_p && !w->phys_cursor_on_p)
8040 x_display_cursor (w, 1,
8041 w->phys_cursor.hpos, w->phys_cursor.vpos,
8042 w->phys_cursor.x, w->phys_cursor.y);
8043 }
8044
8045 #if 0 /* MAC_TODO: mouse cursor */
8046 /* Change the mouse cursor. */
8047 if (draw == DRAW_NORMAL_TEXT)
8048 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8049 f->output_data.x->text_cursor);
8050 else if (draw == DRAW_MOUSE_FACE)
8051 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8052 f->output_data.x->cross_cursor);
8053 else
8054 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8055 f->output_data.x->nontext_cursor);
8056 #endif
8057 }
8058
8059 /* Clear out the mouse-highlighted active region.
8060 Redraw it un-highlighted first. */
8061
8062 static int
8063 clear_mouse_face (dpyinfo)
8064 struct mac_display_info *dpyinfo;
8065 {
8066 int cleared = 0;
8067
8068 if (! NILP (dpyinfo->mouse_face_window))
8069 {
8070 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
8071 cleared = 1;
8072 }
8073
8074 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
8075 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
8076 dpyinfo->mouse_face_window = Qnil;
8077 dpyinfo->mouse_face_overlay = Qnil;
8078 return cleared;
8079 }
8080
8081
8082 /* Clear any mouse-face on window W. This function is part of the
8083 redisplay interface, and is called from try_window_id and similar
8084 functions to ensure the mouse-highlight is off. */
8085
8086 static void
8087 x_clear_mouse_face (w)
8088 struct window *w;
8089 {
8090 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (XFRAME (w->frame));
8091 Lisp_Object window;
8092
8093 BLOCK_INPUT;
8094 XSETWINDOW (window, w);
8095 if (EQ (window, dpyinfo->mouse_face_window))
8096 clear_mouse_face (dpyinfo);
8097 UNBLOCK_INPUT;
8098 }
8099
8100
8101 /* Just discard the mouse face information for frame F, if any.
8102 This is used when the size of F is changed. */
8103
8104 void
8105 cancel_mouse_face (f)
8106 FRAME_PTR f;
8107 {
8108 Lisp_Object window;
8109 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
8110
8111 window = dpyinfo->mouse_face_window;
8112 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
8113 {
8114 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
8115 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
8116 dpyinfo->mouse_face_window = Qnil;
8117 }
8118 }
8119 \f
8120 static struct scroll_bar *x_window_to_scroll_bar ();
8121 static void x_scroll_bar_report_motion ();
8122 static void x_check_fullscreen P_ ((struct frame *));
8123 static void x_check_fullscreen_move P_ ((struct frame *));
8124 static int glyph_rect P_ ((struct frame *f, int, int, Rect *));
8125
8126
8127 /* Try to determine frame pixel position and size of the glyph under
8128 frame pixel coordinates X/Y on frame F . Return the position and
8129 size in *RECT. Value is non-zero if we could compute these
8130 values. */
8131
8132 static int
8133 glyph_rect (f, x, y, rect)
8134 struct frame *f;
8135 int x, y;
8136 Rect *rect;
8137 {
8138 Lisp_Object window;
8139 int part;
8140
8141 window = window_from_coordinates (f, x, y, &part, 0);
8142 if (!NILP (window))
8143 {
8144 struct window *w = XWINDOW (window);
8145 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
8146 struct glyph_row *end = r + w->current_matrix->nrows - 1;
8147
8148 frame_to_window_pixel_xy (w, &x, &y);
8149
8150 for (; r < end && r->enabled_p; ++r)
8151 if (r->y <= y && r->y + r->height > y)
8152 {
8153 /* Found the row at y. */
8154 struct glyph *g = r->glyphs[TEXT_AREA];
8155 struct glyph *end = g + r->used[TEXT_AREA];
8156 int gx;
8157
8158 rect->top = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
8159 rect->bottom = rect->top + r->height;
8160
8161 if (x < r->x)
8162 {
8163 /* x is to the left of the first glyph in the row. */
8164 rect->left = XINT (w->left);
8165 rect->right = WINDOW_TO_FRAME_PIXEL_X (w, r->x);
8166 return 1;
8167 }
8168
8169 for (gx = r->x; g < end; gx += g->pixel_width, ++g)
8170 if (gx <= x && gx + g->pixel_width > x)
8171 {
8172 /* x is on a glyph. */
8173 rect->left = WINDOW_TO_FRAME_PIXEL_X (w, gx);
8174 rect->right = rect->left + g->pixel_width;
8175 return 1;
8176 }
8177
8178 /* x is to the right of the last glyph in the row. */
8179 rect->left = WINDOW_TO_FRAME_PIXEL_X (w, gx);
8180 rect->right = XINT (w->left) + XINT (w->width);
8181 return 1;
8182 }
8183 }
8184
8185 /* The y is not on any row. */
8186 return 0;
8187 }
8188
8189 /* Record the position of the mouse in last_mouse_glyph. */
8190 static void
8191 remember_mouse_glyph (f1, gx, gy)
8192 struct frame * f1;
8193 int gx, gy;
8194 {
8195 if (!glyph_rect (f1, gx, gy, &last_mouse_glyph))
8196 {
8197 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
8198 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
8199
8200 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
8201 round down even for negative values. */
8202 if (gx < 0)
8203 gx -= width - 1;
8204 if (gy < 0)
8205 gy -= height - 1;
8206 #if 0
8207 /* This was the original code from XTmouse_position, but it seems
8208 to give the position of the glyph diagonally next to the one
8209 the mouse is over. */
8210 gx = (gx + width - 1) / width * width;
8211 gy = (gy + height - 1) / height * height;
8212 #else
8213 gx = gx / width * width;
8214 gy = gy / height * height;
8215 #endif
8216
8217 last_mouse_glyph.left = gx;
8218 last_mouse_glyph.top = gy;
8219 last_mouse_glyph.right = gx + width;
8220 last_mouse_glyph.bottom = gy + height;
8221 }
8222 }
8223
8224 /* Return the current position of the mouse.
8225 *fp should be a frame which indicates which display to ask about.
8226
8227 If the mouse movement started in a scroll bar, set *fp, *bar_window,
8228 and *part to the frame, window, and scroll bar part that the mouse
8229 is over. Set *x and *y to the portion and whole of the mouse's
8230 position on the scroll bar.
8231
8232 If the mouse movement started elsewhere, set *fp to the frame the
8233 mouse is on, *bar_window to nil, and *x and *y to the character cell
8234 the mouse is over.
8235
8236 Set *time to the server time-stamp for the time at which the mouse
8237 was at this position.
8238
8239 Don't store anything if we don't have a valid set of values to report.
8240
8241 This clears the mouse_moved flag, so we can wait for the next mouse
8242 movement. */
8243
8244 static void
8245 XTmouse_position (fp, insist, bar_window, part, x, y, time)
8246 FRAME_PTR *fp;
8247 int insist;
8248 Lisp_Object *bar_window;
8249 enum scroll_bar_part *part;
8250 Lisp_Object *x, *y;
8251 unsigned long *time;
8252 {
8253 Point mouse_pos;
8254 int ignore1, ignore2;
8255 WindowPtr wp = FrontWindow ();
8256 struct frame *f;
8257 Lisp_Object frame, tail;
8258
8259 if (is_emacs_window(wp))
8260 f = ((mac_output *) GetWRefCon (wp))->mFP;
8261
8262 BLOCK_INPUT;
8263
8264 if (! NILP (last_mouse_scroll_bar) && insist == 0)
8265 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
8266 else
8267 {
8268 /* Clear the mouse-moved flag for every frame on this display. */
8269 FOR_EACH_FRAME (tail, frame)
8270 XFRAME (frame)->mouse_moved = 0;
8271
8272 last_mouse_scroll_bar = Qnil;
8273
8274 #if TARGET_API_MAC_CARBON
8275 SetPort (GetWindowPort (wp));
8276 #else
8277 SetPort (wp);
8278 #endif
8279
8280 GetMouse (&mouse_pos);
8281
8282 pixel_to_glyph_coords (f, mouse_pos.h, mouse_pos.v, &ignore1, &ignore2,
8283 &last_mouse_glyph, insist);
8284
8285 *bar_window = Qnil;
8286 *part = scroll_bar_handle;
8287 *fp = f;
8288 XSETINT (*x, mouse_pos.h);
8289 XSETINT (*y, mouse_pos.v);
8290 *time = last_mouse_movement_time;
8291 }
8292
8293 UNBLOCK_INPUT;
8294 }
8295
8296 \f
8297 /************************************************************************
8298 Scroll bars, general
8299 ************************************************************************/
8300
8301 /* Create a scroll bar and return the scroll bar vector for it. W is
8302 the Emacs window on which to create the scroll bar. TOP, LEFT,
8303 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
8304 scroll bar. */
8305
8306 static struct scroll_bar *
8307 x_scroll_bar_create (w, top, left, width, height, disp_top, disp_height)
8308 struct window *w;
8309 int top, left, width, height, disp_top, disp_height;
8310 {
8311 struct frame *f = XFRAME (w->frame);
8312 struct scroll_bar *bar
8313 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
8314 Rect r;
8315 ControlHandle ch;
8316
8317 BLOCK_INPUT;
8318
8319 r.left = left;
8320 r.top = disp_top;
8321 r.right = left + width;
8322 r.bottom = disp_top + disp_height;
8323
8324 #ifdef TARGET_API_MAC_CARBON
8325 ch = NewControl (FRAME_MAC_WINDOW (f), &r, "\p", 1, 0, 0, 0,
8326 kControlScrollBarProc, 0L);
8327 #else
8328 ch = NewControl (FRAME_MAC_WINDOW (f), &r, "\p", 1, 0, 0, 0, scrollBarProc,
8329 0L);
8330 #endif
8331 SET_SCROLL_BAR_CONTROL_HANDLE (bar, ch);
8332 SetControlReference (ch, (long) bar);
8333
8334 XSETWINDOW (bar->window, w);
8335 XSETINT (bar->top, top);
8336 XSETINT (bar->left, left);
8337 XSETINT (bar->width, width);
8338 XSETINT (bar->height, height);
8339 XSETINT (bar->start, 0);
8340 XSETINT (bar->end, 0);
8341 bar->dragging = Qnil;
8342
8343 /* Add bar to its frame's list of scroll bars. */
8344 bar->next = FRAME_SCROLL_BARS (f);
8345 bar->prev = Qnil;
8346 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8347 if (!NILP (bar->next))
8348 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8349
8350 UNBLOCK_INPUT;
8351 return bar;
8352 }
8353
8354
8355 /* Draw BAR's handle in the proper position.
8356
8357 If the handle is already drawn from START to END, don't bother
8358 redrawing it, unless REBUILD is non-zero; in that case, always
8359 redraw it. (REBUILD is handy for drawing the handle after expose
8360 events.)
8361
8362 Normally, we want to constrain the start and end of the handle to
8363 fit inside its rectangle, but if the user is dragging the scroll
8364 bar handle, we want to let them drag it down all the way, so that
8365 the bar's top is as far down as it goes; otherwise, there's no way
8366 to move to the very end of the buffer. */
8367
8368 static void
8369 x_scroll_bar_set_handle (bar, start, end, rebuild)
8370 struct scroll_bar *bar;
8371 int start, end;
8372 int rebuild;
8373 {
8374 int dragging = ! NILP (bar->dragging);
8375 ControlHandle ch = SCROLL_BAR_CONTROL_HANDLE (bar);
8376 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8377 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8378 int length = end - start;
8379
8380 /* If the display is already accurate, do nothing. */
8381 if (! rebuild
8382 && start == XINT (bar->start)
8383 && end == XINT (bar->end))
8384 return;
8385
8386 BLOCK_INPUT;
8387
8388 /* Make sure the values are reasonable, and try to preserve the
8389 distance between start and end. */
8390 if (start < 0)
8391 start = 0;
8392 else if (start > top_range)
8393 start = top_range;
8394 end = start + length;
8395
8396 if (end < start)
8397 end = start;
8398 else if (end > top_range && ! dragging)
8399 end = top_range;
8400
8401 /* Store the adjusted setting in the scroll bar. */
8402 XSETINT (bar->start, start);
8403 XSETINT (bar->end, end);
8404
8405 /* Clip the end position, just for display. */
8406 if (end > top_range)
8407 end = top_range;
8408
8409 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels below
8410 top positions, to make sure the handle is always at least that
8411 many pixels tall. */
8412 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
8413
8414 SetControlMinimum (ch, 0);
8415 /* Don't inadvertently activate deactivated scroll bars */
8416 if (GetControlMaximum (ch) != -1)
8417 SetControlMaximum (ch, top_range + VERTICAL_SCROLL_BAR_MIN_HANDLE
8418 - (end - start));
8419 SetControlValue (ch, start);
8420 #if TARGET_API_MAC_CARBON
8421 SetControlViewSize (ch, end - start);
8422 #endif
8423
8424 UNBLOCK_INPUT;
8425 }
8426
8427
8428 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8429 nil. */
8430
8431 static void
8432 x_scroll_bar_remove (bar)
8433 struct scroll_bar *bar;
8434 {
8435 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8436
8437 BLOCK_INPUT;
8438
8439 /* Destroy the Mac scroll bar control */
8440 DisposeControl (SCROLL_BAR_CONTROL_HANDLE (bar));
8441
8442 /* Disassociate this scroll bar from its window. */
8443 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
8444
8445 UNBLOCK_INPUT;
8446 }
8447
8448 /* Set the handle of the vertical scroll bar for WINDOW to indicate
8449 that we are displaying PORTION characters out of a total of WHOLE
8450 characters, starting at POSITION. If WINDOW has no scroll bar,
8451 create one. */
8452 static void
8453 XTset_vertical_scroll_bar (w, portion, whole, position)
8454 struct window *w;
8455 int portion, whole, position;
8456 {
8457 struct frame *f = XFRAME (w->frame);
8458 struct scroll_bar *bar;
8459 int top, height, left, sb_left, width, sb_width, disp_top, disp_height;
8460 int window_x, window_y, window_width, window_height;
8461
8462 /* Get window dimensions. */
8463 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
8464 top = window_y;
8465 #ifdef MAC_OSX
8466 width = 16;
8467 #else
8468 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8469 #endif
8470 height = window_height;
8471
8472 /* Compute the left edge of the scroll bar area. */
8473 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8474 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8475 else
8476 left = XFASTINT (w->left);
8477 left *= CANON_X_UNIT (f);
8478 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8479
8480 /* Compute the width of the scroll bar which might be less than
8481 the width of the area reserved for the scroll bar. */
8482 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8483 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
8484 else
8485 sb_width = width;
8486
8487 /* Compute the left edge of the scroll bar. */
8488 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8489 sb_left = left + width - sb_width - (width - sb_width) / 2;
8490 else
8491 sb_left = left + (width - sb_width) / 2;
8492
8493 /* Adjustments according to Inside Macintosh to make it look nice */
8494 disp_top = top;
8495 disp_height = height;
8496 if (disp_top == 0)
8497 {
8498 disp_top = -1;
8499 disp_height++;
8500 }
8501 else if (disp_top == PIXEL_HEIGHT (f) - 16)
8502 {
8503 disp_top++;
8504 disp_height--;
8505 }
8506
8507 if (sb_left + sb_width == PIXEL_WIDTH (f))
8508 sb_left++;
8509
8510 /* Does the scroll bar exist yet? */
8511 if (NILP (w->vertical_scroll_bar))
8512 {
8513 BLOCK_INPUT;
8514 XClearArea (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f),
8515 left, top, width, height, 0);
8516 UNBLOCK_INPUT;
8517 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height, disp_top,
8518 disp_height);
8519 XSETVECTOR (w->vertical_scroll_bar, bar);
8520 }
8521 else
8522 {
8523 /* It may just need to be moved and resized. */
8524 ControlHandle ch;
8525
8526 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8527 ch = SCROLL_BAR_CONTROL_HANDLE (bar);
8528
8529 BLOCK_INPUT;
8530
8531 /* If already correctly positioned, do nothing. */
8532 if (XINT (bar->left) == sb_left
8533 && XINT (bar->top) == top
8534 && XINT (bar->width) == sb_width
8535 && XINT (bar->height) == height)
8536 Draw1Control (ch);
8537 else
8538 {
8539 /* Clear areas not covered by the scroll bar because it's not as
8540 wide as the area reserved for it . This makes sure a
8541 previous mode line display is cleared after C-x 2 C-x 1, for
8542 example. */
8543 int area_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8544 XClearArea (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f),
8545 left, top, area_width, height, 0);
8546
8547 #if 0
8548 if (sb_left + sb_width >= PIXEL_WIDTH (f))
8549 XClearArea (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f),
8550 sb_left - 1, top, 1, height, 0);
8551 #endif
8552
8553 HideControl (ch);
8554 MoveControl (ch, sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM, disp_top);
8555 SizeControl (ch, sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8556 disp_height);
8557 ShowControl (ch);
8558
8559 /* Remember new settings. */
8560 XSETINT (bar->left, sb_left);
8561 XSETINT (bar->top, top);
8562 XSETINT (bar->width, sb_width);
8563 XSETINT (bar->height, height);
8564 }
8565
8566 UNBLOCK_INPUT;
8567 }
8568
8569 /* Set the scroll bar's current state, unless we're currently being
8570 dragged. */
8571 if (NILP (bar->dragging))
8572 {
8573 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
8574
8575 if (whole == 0)
8576 x_scroll_bar_set_handle (bar, 0, top_range, 0);
8577 else
8578 {
8579 int start = ((double) position * top_range) / whole;
8580 int end = ((double) (position + portion) * top_range) / whole;
8581 x_scroll_bar_set_handle (bar, start, end, 0);
8582 }
8583 }
8584 }
8585
8586
8587 /* The following three hooks are used when we're doing a thorough
8588 redisplay of the frame. We don't explicitly know which scroll bars
8589 are going to be deleted, because keeping track of when windows go
8590 away is a real pain - "Can you say set-window-configuration, boys
8591 and girls?" Instead, we just assert at the beginning of redisplay
8592 that *all* scroll bars are to be removed, and then save a scroll bar
8593 from the fiery pit when we actually redisplay its window. */
8594
8595 /* Arrange for all scroll bars on FRAME to be removed at the next call
8596 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
8597 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8598
8599 static void
8600 XTcondemn_scroll_bars (frame)
8601 FRAME_PTR frame;
8602 {
8603 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8604 while (! NILP (FRAME_SCROLL_BARS (frame)))
8605 {
8606 Lisp_Object bar;
8607 bar = FRAME_SCROLL_BARS (frame);
8608 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8609 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8610 XSCROLL_BAR (bar)->prev = Qnil;
8611 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8612 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8613 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8614 }
8615 }
8616
8617
8618 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
8619 Note that WINDOW isn't necessarily condemned at all. */
8620
8621 static void
8622 XTredeem_scroll_bar (window)
8623 struct window *window;
8624 {
8625 struct scroll_bar *bar;
8626
8627 /* We can't redeem this window's scroll bar if it doesn't have one. */
8628 if (NILP (window->vertical_scroll_bar))
8629 abort ();
8630
8631 bar = XSCROLL_BAR (window->vertical_scroll_bar);
8632
8633 /* Unlink it from the condemned list. */
8634 {
8635 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
8636
8637 if (NILP (bar->prev))
8638 {
8639 /* If the prev pointer is nil, it must be the first in one of
8640 the lists. */
8641 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
8642 /* It's not condemned. Everything's fine. */
8643 return;
8644 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8645 window->vertical_scroll_bar))
8646 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
8647 else
8648 /* If its prev pointer is nil, it must be at the front of
8649 one or the other! */
8650 abort ();
8651 }
8652 else
8653 XSCROLL_BAR (bar->prev)->next = bar->next;
8654
8655 if (! NILP (bar->next))
8656 XSCROLL_BAR (bar->next)->prev = bar->prev;
8657
8658 bar->next = FRAME_SCROLL_BARS (f);
8659 bar->prev = Qnil;
8660 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8661 if (! NILP (bar->next))
8662 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8663 }
8664 }
8665
8666 /* Remove all scroll bars on FRAME that haven't been saved since the
8667 last call to `*condemn_scroll_bars_hook'. */
8668
8669 static void
8670 XTjudge_scroll_bars (f)
8671 FRAME_PTR f;
8672 {
8673 Lisp_Object bar, next;
8674
8675 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
8676
8677 /* Clear out the condemned list now so we won't try to process any
8678 more events on the hapless scroll bars. */
8679 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
8680
8681 for (; ! NILP (bar); bar = next)
8682 {
8683 struct scroll_bar *b = XSCROLL_BAR (bar);
8684
8685 x_scroll_bar_remove (b);
8686
8687 next = b->next;
8688 b->next = b->prev = Qnil;
8689 }
8690
8691 /* Now there should be no references to the condemned scroll bars,
8692 and they should get garbage-collected. */
8693 }
8694
8695
8696 static void
8697 activate_scroll_bars (frame)
8698 FRAME_PTR frame;
8699 {
8700 Lisp_Object bar;
8701 ControlHandle ch;
8702
8703 bar = FRAME_SCROLL_BARS (frame);
8704 while (! NILP (bar))
8705 {
8706 ch = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (bar));
8707 #ifdef TARGET_API_MAC_CARBON
8708 ActivateControl (ch);
8709 #else
8710 SetControlMaximum (ch,
8711 VERTICAL_SCROLL_BAR_TOP_RANGE (frame,
8712 XINT (XSCROLL_BAR (bar)
8713 ->height)) - 1);
8714 #endif
8715 bar = XSCROLL_BAR (bar)->next;
8716 }
8717 }
8718
8719
8720 static void
8721 deactivate_scroll_bars (frame)
8722 FRAME_PTR frame;
8723 {
8724 Lisp_Object bar;
8725 ControlHandle ch;
8726
8727 bar = FRAME_SCROLL_BARS (frame);
8728 while (! NILP (bar))
8729 {
8730 ch = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (bar));
8731 #ifdef TARGET_API_MAC_CARBON
8732 DeactivateControl (ch);
8733 #else
8734 SetControlMaximum (ch, XINT (-1));
8735 #endif
8736 bar = XSCROLL_BAR (bar)->next;
8737 }
8738 }
8739
8740 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8741 is set to something other than NO_EVENT, it is enqueued.
8742
8743 This may be called from a signal handler, so we have to ignore GC
8744 mark bits. */
8745
8746 static void
8747 x_scroll_bar_handle_click (bar, part_code, er, bufp)
8748 struct scroll_bar *bar;
8749 int part_code;
8750 EventRecord *er;
8751 struct input_event *bufp;
8752 {
8753 if (! GC_WINDOWP (bar->window))
8754 abort ();
8755
8756 bufp->kind = SCROLL_BAR_CLICK_EVENT;
8757 bufp->frame_or_window = bar->window;
8758 bufp->arg = Qnil;
8759
8760 bar->dragging = Qnil;
8761
8762 switch (part_code)
8763 {
8764 case kControlUpButtonPart:
8765 bufp->part = scroll_bar_up_arrow;
8766 break;
8767 case kControlDownButtonPart:
8768 bufp->part = scroll_bar_down_arrow;
8769 break;
8770 case kControlPageUpPart:
8771 bufp->part = scroll_bar_above_handle;
8772 break;
8773 case kControlPageDownPart:
8774 bufp->part = scroll_bar_below_handle;
8775 break;
8776 #ifdef TARGET_API_MAC_CARBON
8777 default:
8778 #else
8779 case kControlIndicatorPart:
8780 #endif
8781 if (er->what == mouseDown)
8782 bar->dragging = make_number (0);
8783 XSETVECTOR (last_mouse_scroll_bar, bar);
8784 bufp->part = scroll_bar_handle;
8785 break;
8786 }
8787 }
8788
8789
8790 /* Handle some mouse motion while someone is dragging the scroll bar.
8791
8792 This may be called from a signal handler, so we have to ignore GC
8793 mark bits. */
8794
8795 static void
8796 x_scroll_bar_note_movement (bar, y_pos, t)
8797 struct scroll_bar *bar;
8798 int y_pos;
8799 Time t;
8800 {
8801 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8802
8803 last_mouse_movement_time = t;
8804
8805 f->mouse_moved = 1;
8806 XSETVECTOR (last_mouse_scroll_bar, bar);
8807
8808 /* If we're dragging the bar, display it. */
8809 if (! GC_NILP (bar->dragging))
8810 {
8811 /* Where should the handle be now? */
8812 int new_start = y_pos - 24;
8813
8814 if (new_start != XINT (bar->start))
8815 {
8816 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
8817
8818 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
8819 }
8820 }
8821 }
8822
8823
8824 /* Return information to the user about the current position of the
8825 mouse on the scroll bar. */
8826
8827 static void
8828 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8829 FRAME_PTR *fp;
8830 Lisp_Object *bar_window;
8831 enum scroll_bar_part *part;
8832 Lisp_Object *x, *y;
8833 unsigned long *time;
8834 {
8835 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
8836 WindowPtr wp = FrontWindow ();
8837 Point mouse_pos;
8838 struct frame *f = ((mac_output *) GetWRefCon (wp))->mFP;
8839 int win_y, top_range;
8840
8841 #if TARGET_API_MAC_CARBON
8842 SetPort (GetWindowPort (wp));
8843 #else
8844 SetPort (wp);
8845 #endif
8846
8847 GetMouse (&mouse_pos);
8848
8849 win_y = mouse_pos.v - XINT (bar->top);
8850 top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8851
8852 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
8853
8854 win_y -= 24;
8855
8856 if (! NILP (bar->dragging))
8857 win_y -= XINT (bar->dragging);
8858
8859 if (win_y < 0)
8860 win_y = 0;
8861 if (win_y > top_range)
8862 win_y = top_range;
8863
8864 *fp = f;
8865 *bar_window = bar->window;
8866
8867 if (! NILP (bar->dragging))
8868 *part = scroll_bar_handle;
8869 else if (win_y < XINT (bar->start))
8870 *part = scroll_bar_above_handle;
8871 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8872 *part = scroll_bar_handle;
8873 else
8874 *part = scroll_bar_below_handle;
8875
8876 XSETINT (*x, win_y);
8877 XSETINT (*y, top_range);
8878
8879 f->mouse_moved = 0;
8880 last_mouse_scroll_bar = Qnil;
8881
8882 *time = last_mouse_movement_time;
8883 }
8884 \f
8885 /***********************************************************************
8886 Text Cursor
8887 ***********************************************************************/
8888
8889 /* Notice if the text cursor of window W has been overwritten by a
8890 drawing operation that outputs glyphs starting at START_X and
8891 ending at END_X in the line given by output_cursor.vpos.
8892 Coordinates are area-relative. END_X < 0 means all the rest
8893 of the line after START_X has been written. */
8894
8895 static void
8896 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
8897 struct window *w;
8898 enum glyph_row_area area;
8899 int x0, x1, y0, y1;
8900 {
8901 if (area == TEXT_AREA
8902 && w->phys_cursor_on_p
8903 && y0 <= w->phys_cursor.y
8904 && y1 >= w->phys_cursor.y + w->phys_cursor_height
8905 && x0 <= w->phys_cursor.x
8906 && (x1 < 0 || x1 > w->phys_cursor.x))
8907 w->phys_cursor_on_p = 0;
8908 }
8909
8910
8911 /* Set clipping for output in glyph row ROW. W is the window in which
8912 we operate. GC is the graphics context to set clipping in.
8913 WHOLE_LINE_P non-zero means include the areas used for truncation
8914 mark display and alike in the clipping rectangle.
8915
8916 ROW may be a text row or, e.g., a mode line. Text rows must be
8917 clipped to the interior of the window dedicated to text display,
8918 mode lines must be clipped to the whole window. */
8919
8920 static void
8921 x_clip_to_row (w, row, gc, whole_line_p)
8922 struct window *w;
8923 struct glyph_row *row;
8924 GC gc;
8925 int whole_line_p;
8926 {
8927 struct frame *f = XFRAME (WINDOW_FRAME (w));
8928 Rect clip_rect;
8929 int window_x, window_y, window_width, window_height;
8930
8931 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
8932
8933 clip_rect.left = WINDOW_TO_FRAME_PIXEL_X (w, 0);
8934 clip_rect.top = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
8935 clip_rect.top = max (clip_rect.top, window_y);
8936 clip_rect.right = clip_rect.left + window_width;
8937 clip_rect.bottom = clip_rect.top + row->visible_height;
8938
8939 /* If clipping to the whole line, including trunc marks, extend
8940 the rectangle to the left and increase its width. */
8941 if (whole_line_p)
8942 {
8943 clip_rect.left -= FRAME_X_LEFT_FRINGE_WIDTH (f);
8944 clip_rect.right += FRAME_X_FRINGE_WIDTH (f);
8945 }
8946
8947 mac_set_clip_rectangle (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f), &clip_rect);
8948 }
8949
8950
8951 /* Draw a hollow box cursor on window W in glyph row ROW. */
8952
8953 static void
8954 x_draw_hollow_cursor (w, row)
8955 struct window *w;
8956 struct glyph_row *row;
8957 {
8958 struct frame *f = XFRAME (WINDOW_FRAME (w));
8959 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
8960 Display *dpy = FRAME_MAC_DISPLAY (f);
8961 int x, y, wd, h;
8962 XGCValues xgcv;
8963 struct glyph *cursor_glyph;
8964 GC gc;
8965
8966 /* Compute frame-relative coordinates from window-relative
8967 coordinates. */
8968 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
8969 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
8970 + row->ascent - w->phys_cursor_ascent);
8971 h = row->height - 1;
8972
8973 /* Get the glyph the cursor is on. If we can't tell because
8974 the current matrix is invalid or such, give up. */
8975 cursor_glyph = get_phys_cursor_glyph (w);
8976 if (cursor_glyph == NULL)
8977 return;
8978
8979 /* Compute the width of the rectangle to draw. If on a stretch
8980 glyph, and `x-stretch-block-cursor' is nil, don't draw a
8981 rectangle as wide as the glyph, but use a canonical character
8982 width instead. */
8983 wd = cursor_glyph->pixel_width - 1;
8984 if (cursor_glyph->type == STRETCH_GLYPH
8985 && !x_stretch_cursor_p)
8986 wd = min (CANON_X_UNIT (f), wd);
8987
8988 /* The foreground of cursor_gc is typically the same as the normal
8989 background color, which can cause the cursor box to be invisible. */
8990 xgcv.foreground = f->output_data.mac->cursor_pixel;
8991 if (dpyinfo->scratch_cursor_gc)
8992 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
8993 else
8994 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_MAC_WINDOW (f),
8995 GCForeground, &xgcv);
8996 gc = dpyinfo->scratch_cursor_gc;
8997
8998 /* Set clipping, draw the rectangle, and reset clipping again. */
8999 x_clip_to_row (w, row, gc, 0);
9000 mac_draw_rectangle (dpy, FRAME_MAC_WINDOW (f), gc, x, y, wd, h);
9001 mac_reset_clipping (dpy, FRAME_MAC_WINDOW (f));
9002 }
9003
9004
9005 /* Draw a bar cursor on window W in glyph row ROW.
9006
9007 Implementation note: One would like to draw a bar cursor with an
9008 angle equal to the one given by the font property XA_ITALIC_ANGLE.
9009 Unfortunately, I didn't find a font yet that has this property set.
9010 --gerd. */
9011
9012 static void
9013 x_draw_bar_cursor (w, row, width)
9014 struct window *w;
9015 struct glyph_row *row;
9016 int width;
9017 {
9018 /* If cursor hpos is out of bounds, don't draw garbage. This can
9019 happen in mini-buffer windows when switching between echo area
9020 glyphs and mini-buffer. */
9021 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
9022 {
9023 struct frame *f = XFRAME (w->frame);
9024 struct glyph *cursor_glyph;
9025 GC gc;
9026 int x;
9027 unsigned long mask;
9028 XGCValues xgcv;
9029 Display *dpy;
9030 Window window;
9031
9032 cursor_glyph = get_phys_cursor_glyph (w);
9033 if (cursor_glyph == NULL)
9034 return;
9035
9036 xgcv.background = f->output_data.mac->cursor_pixel;
9037 xgcv.foreground = f->output_data.mac->cursor_pixel;
9038 mask = GCForeground | GCBackground;
9039 dpy = FRAME_MAC_DISPLAY (f);
9040 window = FRAME_MAC_WINDOW (f);
9041 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
9042
9043 if (gc)
9044 XChangeGC (dpy, gc, mask, &xgcv);
9045 else
9046 {
9047 gc = XCreateGC (dpy, window, mask, &xgcv);
9048 FRAME_MAC_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
9049 }
9050
9051 if (width < 0)
9052 width = f->output_data.mac->cursor_width;
9053
9054 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9055 x_clip_to_row (w, row, gc, 0);
9056 XFillRectangle (dpy, window, gc,
9057 x,
9058 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
9059 min (cursor_glyph->pixel_width, width),
9060 row->height);
9061 mac_reset_clipping (dpy, FRAME_MAC_WINDOW (f));
9062 }
9063 }
9064
9065
9066 /* Clear the cursor of window W to background color, and mark the
9067 cursor as not shown. This is used when the text where the cursor
9068 is is about to be rewritten. */
9069
9070 static void
9071 x_clear_cursor (w)
9072 struct window *w;
9073 {
9074 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
9075 x_update_window_cursor (w, 0);
9076 }
9077
9078
9079 /* Draw the cursor glyph of window W in glyph row ROW. See the
9080 comment of x_draw_glyphs for the meaning of HL. */
9081
9082 static void
9083 x_draw_phys_cursor_glyph (w, row, hl)
9084 struct window *w;
9085 struct glyph_row *row;
9086 enum draw_glyphs_face hl;
9087 {
9088 /* If cursor hpos is out of bounds, don't draw garbage. This can
9089 happen in mini-buffer windows when switching between echo area
9090 glyphs and mini-buffer. */
9091 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
9092 {
9093 int on_p = w->phys_cursor_on_p;
9094 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
9095 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
9096 hl, 0);
9097 w->phys_cursor_on_p = on_p;
9098
9099 /* When we erase the cursor, and ROW is overlapped by other
9100 rows, make sure that these overlapping parts of other rows
9101 are redrawn. */
9102 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
9103 {
9104 if (row > w->current_matrix->rows
9105 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
9106 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
9107
9108 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
9109 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
9110 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
9111 }
9112 }
9113 }
9114
9115
9116 /* Erase the image of a cursor of window W from the screen. */
9117
9118 static void
9119 x_erase_phys_cursor (w)
9120 struct window *w;
9121 {
9122 struct frame *f = XFRAME (w->frame);
9123 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9124 int hpos = w->phys_cursor.hpos;
9125 int vpos = w->phys_cursor.vpos;
9126 int mouse_face_here_p = 0;
9127 struct glyph_matrix *active_glyphs = w->current_matrix;
9128 struct glyph_row *cursor_row;
9129 struct glyph *cursor_glyph;
9130 enum draw_glyphs_face hl;
9131
9132 /* No cursor displayed or row invalidated => nothing to do on the
9133 screen. */
9134 if (w->phys_cursor_type == NO_CURSOR)
9135 goto mark_cursor_off;
9136
9137 /* VPOS >= active_glyphs->nrows means that window has been resized.
9138 Don't bother to erase the cursor. */
9139 if (vpos >= active_glyphs->nrows)
9140 goto mark_cursor_off;
9141
9142 /* If row containing cursor is marked invalid, there is nothing we
9143 can do. */
9144 cursor_row = MATRIX_ROW (active_glyphs, vpos);
9145 if (!cursor_row->enabled_p)
9146 goto mark_cursor_off;
9147
9148 /* If row is completely invisible, don't attempt to delete a cursor which
9149 isn't there. This may happen if cursor is at top of window, and
9150 we switch to a buffer with a header line in that window. */
9151 if (cursor_row->visible_height <= 0)
9152 goto mark_cursor_off;
9153
9154 /* This can happen when the new row is shorter than the old one.
9155 In this case, either x_draw_glyphs or clear_end_of_line
9156 should have cleared the cursor. Note that we wouldn't be
9157 able to erase the cursor in this case because we don't have a
9158 cursor glyph at hand. */
9159 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
9160 goto mark_cursor_off;
9161
9162 /* If the cursor is in the mouse face area, redisplay that when
9163 we clear the cursor. */
9164 if (! NILP (dpyinfo->mouse_face_window)
9165 && w == XWINDOW (dpyinfo->mouse_face_window)
9166 && (vpos > dpyinfo->mouse_face_beg_row
9167 || (vpos == dpyinfo->mouse_face_beg_row
9168 && hpos >= dpyinfo->mouse_face_beg_col))
9169 && (vpos < dpyinfo->mouse_face_end_row
9170 || (vpos == dpyinfo->mouse_face_end_row
9171 && hpos < dpyinfo->mouse_face_end_col))
9172 /* Don't redraw the cursor's spot in mouse face if it is at the
9173 end of a line (on a newline). The cursor appears there, but
9174 mouse highlighting does not. */
9175 && cursor_row->used[TEXT_AREA] > hpos)
9176 mouse_face_here_p = 1;
9177
9178 /* Maybe clear the display under the cursor. */
9179 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
9180 {
9181 int x;
9182 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9183
9184 cursor_glyph = get_phys_cursor_glyph (w);
9185 if (cursor_glyph == NULL)
9186 goto mark_cursor_off;
9187
9188 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
9189
9190 XClearArea (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f),
9191 x,
9192 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
9193 cursor_row->y)),
9194 cursor_glyph->pixel_width,
9195 cursor_row->visible_height,
9196 0);
9197 }
9198
9199 /* Erase the cursor by redrawing the character underneath it. */
9200 if (mouse_face_here_p)
9201 hl = DRAW_MOUSE_FACE;
9202 else
9203 hl = DRAW_NORMAL_TEXT;
9204 x_draw_phys_cursor_glyph (w, cursor_row, hl);
9205
9206 mark_cursor_off:
9207 w->phys_cursor_on_p = 0;
9208 w->phys_cursor_type = NO_CURSOR;
9209 }
9210
9211
9212 /* Non-zero if physical cursor of window W is within mouse face. */
9213
9214 static int
9215 cursor_in_mouse_face_p (w)
9216 struct window *w;
9217 {
9218 struct mac_display_info *dpyinfo
9219 = FRAME_MAC_DISPLAY_INFO (XFRAME (w->frame));
9220 int in_mouse_face = 0;
9221
9222 if (WINDOWP (dpyinfo->mouse_face_window)
9223 && XWINDOW (dpyinfo->mouse_face_window) == w)
9224 {
9225 int hpos = w->phys_cursor.hpos;
9226 int vpos = w->phys_cursor.vpos;
9227
9228 if (vpos >= dpyinfo->mouse_face_beg_row
9229 && vpos <= dpyinfo->mouse_face_end_row
9230 && (vpos > dpyinfo->mouse_face_beg_row
9231 || hpos >= dpyinfo->mouse_face_beg_col)
9232 && (vpos < dpyinfo->mouse_face_end_row
9233 || hpos < dpyinfo->mouse_face_end_col
9234 || dpyinfo->mouse_face_past_end))
9235 in_mouse_face = 1;
9236 }
9237
9238 return in_mouse_face;
9239 }
9240
9241
9242 /* Display or clear cursor of window W. If ON is zero, clear the
9243 cursor. If it is non-zero, display the cursor. If ON is nonzero,
9244 where to put the cursor is specified by HPOS, VPOS, X and Y. */
9245
9246 void
9247 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
9248 struct window *w;
9249 int on, hpos, vpos, x, y;
9250 {
9251 struct frame *f = XFRAME (w->frame);
9252 int new_cursor_type;
9253 int new_cursor_width;
9254 struct glyph_matrix *current_glyphs;
9255 struct glyph_row *glyph_row;
9256 struct glyph *glyph;
9257 int cursor_non_selected;
9258 int active_cursor = 1;
9259
9260 /* This is pointless on invisible frames, and dangerous on garbaged
9261 windows and frames; in the latter case, the frame or window may
9262 be in the midst of changing its size, and x and y may be off the
9263 window. */
9264 if (! FRAME_VISIBLE_P (f)
9265 || FRAME_GARBAGED_P (f)
9266 || vpos >= w->current_matrix->nrows
9267 || hpos >= w->current_matrix->matrix_w)
9268 return;
9269
9270 /* If cursor is off and we want it off, return quickly. */
9271 if (!on && !w->phys_cursor_on_p)
9272 return;
9273
9274 current_glyphs = w->current_matrix;
9275 glyph_row = MATRIX_ROW (current_glyphs, vpos);
9276 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
9277
9278 /* If cursor row is not enabled, we don't really know where to
9279 display the cursor. */
9280 if (!glyph_row->enabled_p)
9281 {
9282 w->phys_cursor_on_p = 0;
9283 return;
9284 }
9285
9286 xassert (interrupt_input_blocked);
9287
9288 /* Set new_cursor_type to the cursor we want to be displayed. In a
9289 mini-buffer window, we want the cursor only to appear if we are
9290 reading input from this window. For the selected window, we want
9291 the cursor type given by the frame parameter. If explicitly
9292 marked off, draw no cursor. In all other cases, we want a hollow
9293 box cursor. */
9294 cursor_non_selected
9295 = !NILP (Fbuffer_local_value (Qcursor_in_non_selected_windows,
9296 w->buffer));
9297 new_cursor_width = -1;
9298 if (cursor_in_echo_area
9299 && FRAME_HAS_MINIBUF_P (f)
9300 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
9301 {
9302 if (w == XWINDOW (echo_area_window))
9303 new_cursor_type = FRAME_DESIRED_CURSOR (f);
9304 else
9305 {
9306 if (cursor_non_selected)
9307 new_cursor_type = HOLLOW_BOX_CURSOR;
9308 else
9309 new_cursor_type = NO_CURSOR;
9310 active_cursor = 0;
9311 }
9312 }
9313 else
9314 {
9315 if (f != FRAME_MAC_DISPLAY_INFO (f)->x_highlight_frame
9316 || w != XWINDOW (f->selected_window))
9317 {
9318 active_cursor = 0;
9319
9320 if (MINI_WINDOW_P (w)
9321 || !cursor_non_selected
9322 || NILP (XBUFFER (w->buffer)->cursor_type))
9323 new_cursor_type = NO_CURSOR;
9324 else
9325 new_cursor_type = HOLLOW_BOX_CURSOR;
9326 }
9327 else
9328 {
9329 struct buffer *b = XBUFFER (w->buffer);
9330
9331 if (EQ (b->cursor_type, Qt))
9332 new_cursor_type = FRAME_DESIRED_CURSOR (f);
9333 else
9334 new_cursor_type = x_specified_cursor_type (b->cursor_type,
9335 &new_cursor_width);
9336 if (w->cursor_off_p)
9337 {
9338 if (new_cursor_type == FILLED_BOX_CURSOR)
9339 new_cursor_type = HOLLOW_BOX_CURSOR;
9340 else if (new_cursor_type == BAR_CURSOR && new_cursor_width > 1)
9341 new_cursor_width = 1;
9342 else
9343 new_cursor_type = NO_CURSOR;
9344 }
9345 }
9346 }
9347
9348 /* If cursor is currently being shown and we don't want it to be or
9349 it is in the wrong place, or the cursor type is not what we want,
9350 erase it. */
9351 if (w->phys_cursor_on_p
9352 && (!on
9353 || w->phys_cursor.x != x
9354 || w->phys_cursor.y != y
9355 || new_cursor_type != w->phys_cursor_type
9356 || (new_cursor_type == BAR_CURSOR
9357 && new_cursor_width != w->phys_cursor_width)))
9358 x_erase_phys_cursor (w);
9359
9360 /* If the cursor is now invisible and we want it to be visible,
9361 display it. */
9362 if (on && !w->phys_cursor_on_p)
9363 {
9364 w->phys_cursor_ascent = glyph_row->ascent;
9365 w->phys_cursor_height = glyph_row->height;
9366
9367 /* Set phys_cursor_.* before x_draw_.* is called because some
9368 of them may need the information. */
9369 w->phys_cursor.x = x;
9370 w->phys_cursor.y = glyph_row->y;
9371 w->phys_cursor.hpos = hpos;
9372 w->phys_cursor.vpos = vpos;
9373 w->phys_cursor_type = new_cursor_type;
9374 w->phys_cursor_width = new_cursor_width;
9375 w->phys_cursor_on_p = 1;
9376
9377 switch (new_cursor_type)
9378 {
9379 case HOLLOW_BOX_CURSOR:
9380 x_draw_hollow_cursor (w, glyph_row);
9381 break;
9382
9383 case FILLED_BOX_CURSOR:
9384 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
9385 break;
9386
9387 case BAR_CURSOR:
9388 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
9389 break;
9390
9391 case NO_CURSOR:
9392 break;
9393
9394 default:
9395 abort ();
9396 }
9397 }
9398 }
9399
9400
9401 /* Display the cursor on window W, or clear it. X and Y are window
9402 relative pixel coordinates. HPOS and VPOS are glyph matrix
9403 positions. If W is not the selected window, display a hollow
9404 cursor. ON non-zero means display the cursor at X, Y which
9405 correspond to HPOS, VPOS, otherwise it is cleared. */
9406
9407 void
9408 x_display_cursor (w, on, hpos, vpos, x, y)
9409 struct window *w;
9410 int on, hpos, vpos, x, y;
9411 {
9412 BLOCK_INPUT;
9413 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
9414 UNBLOCK_INPUT;
9415 }
9416
9417
9418 /* Display the cursor on window W, or clear it, according to ON_P.
9419 Don't change the cursor's position. */
9420
9421 void
9422 x_update_cursor (f, on_p)
9423 struct frame *f;
9424 int on_p;
9425 {
9426 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
9427 }
9428
9429
9430 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
9431 in the window tree rooted at W. */
9432
9433 static void
9434 x_update_cursor_in_window_tree (w, on_p)
9435 struct window *w;
9436 int on_p;
9437 {
9438 while (w)
9439 {
9440 if (!NILP (w->hchild))
9441 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
9442 else if (!NILP (w->vchild))
9443 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
9444 else
9445 x_update_window_cursor (w, on_p);
9446
9447 w = NILP (w->next) ? 0 : XWINDOW (w->next);
9448 }
9449 }
9450
9451
9452 /* Switch the display of W's cursor on or off, according to the value
9453 of ON. */
9454
9455 static void
9456 x_update_window_cursor (w, on)
9457 struct window *w;
9458 int on;
9459 {
9460 /* Don't update cursor in windows whose frame is in the process
9461 of being deleted. */
9462 if (w->current_matrix)
9463 {
9464 BLOCK_INPUT;
9465 x_display_and_set_cursor (w, on, w->phys_cursor.hpos,
9466 w->phys_cursor.vpos, w->phys_cursor.x,
9467 w->phys_cursor.y);
9468 UNBLOCK_INPUT;
9469 }
9470 }
9471
9472
9473
9474 \f
9475 /* Icons. */
9476
9477 #if 0 /* MAC_TODO: no icon support yet. */
9478 int
9479 x_bitmap_icon (f, icon)
9480 struct frame *f;
9481 Lisp_Object icon;
9482 {
9483 HANDLE hicon;
9484
9485 if (FRAME_W32_WINDOW (f) == 0)
9486 return 1;
9487
9488 if (NILP (icon))
9489 hicon = LoadIcon (hinst, EMACS_CLASS);
9490 else if (STRINGP (icon))
9491 hicon = LoadImage (NULL, (LPCTSTR) SDATA (icon), IMAGE_ICON, 0, 0,
9492 LR_DEFAULTSIZE | LR_LOADFROMFILE);
9493 else if (SYMBOLP (icon))
9494 {
9495 LPCTSTR name;
9496
9497 if (EQ (icon, intern ("application")))
9498 name = (LPCTSTR) IDI_APPLICATION;
9499 else if (EQ (icon, intern ("hand")))
9500 name = (LPCTSTR) IDI_HAND;
9501 else if (EQ (icon, intern ("question")))
9502 name = (LPCTSTR) IDI_QUESTION;
9503 else if (EQ (icon, intern ("exclamation")))
9504 name = (LPCTSTR) IDI_EXCLAMATION;
9505 else if (EQ (icon, intern ("asterisk")))
9506 name = (LPCTSTR) IDI_ASTERISK;
9507 else if (EQ (icon, intern ("winlogo")))
9508 name = (LPCTSTR) IDI_WINLOGO;
9509 else
9510 return 1;
9511
9512 hicon = LoadIcon (NULL, name);
9513 }
9514 else
9515 return 1;
9516
9517 if (hicon == NULL)
9518 return 1;
9519
9520 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_BIG,
9521 (LPARAM) hicon);
9522
9523 return 0;
9524 }
9525 #endif /* MAC_TODO */
9526 \f
9527 /************************************************************************
9528 Handling X errors
9529 ************************************************************************/
9530
9531 /* Display Error Handling functions not used on W32. Listing them here
9532 helps diff stay in step when comparing w32term.c with xterm.c.
9533
9534 x_error_catcher (display, error)
9535 x_catch_errors (dpy)
9536 x_catch_errors_unwind (old_val)
9537 x_check_errors (dpy, format)
9538 x_had_errors_p (dpy)
9539 x_clear_errors (dpy)
9540 x_uncatch_errors (dpy, count)
9541 x_trace_wire ()
9542 x_connection_signal (signalnum)
9543 x_connection_closed (dpy, error_message)
9544 x_error_quitter (display, error)
9545 x_error_handler (display, error)
9546 x_io_error_quitter (display)
9547
9548 */
9549
9550 \f
9551 /* Changing the font of the frame. */
9552
9553 /* Give frame F the font named FONTNAME as its default font, and
9554 return the full name of that font. FONTNAME may be a wildcard
9555 pattern; in that case, we choose some font that fits the pattern.
9556 The return value shows which font we chose. */
9557
9558 Lisp_Object
9559 x_new_font (f, fontname)
9560 struct frame *f;
9561 register char *fontname;
9562 {
9563 struct font_info *fontp
9564 = FS_LOAD_FONT (f, 0, fontname, -1);
9565
9566 if (!fontp)
9567 return Qnil;
9568
9569 FRAME_FONT (f) = (XFontStruct *) (fontp->font);
9570 FRAME_BASELINE_OFFSET (f) = fontp->baseline_offset;
9571 FRAME_FONTSET (f) = -1;
9572
9573 /* Compute the scroll bar width in character columns. */
9574 if (f->scroll_bar_pixel_width > 0)
9575 {
9576 int wid = FONT_WIDTH (FRAME_FONT (f));
9577 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
9578 }
9579 else
9580 {
9581 int wid = FONT_WIDTH (FRAME_FONT (f));
9582 f->scroll_bar_cols = (14 + wid - 1) / wid;
9583 }
9584
9585 /* Now make the frame display the given font. */
9586 if (FRAME_MAC_WINDOW (f) != 0)
9587 {
9588 XSetFont (FRAME_MAC_DISPLAY (f), f->output_data.mac->normal_gc,
9589 f->output_data.mac->font);
9590 XSetFont (FRAME_MAC_DISPLAY (f), f->output_data.mac->reverse_gc,
9591 f->output_data.mac->font);
9592 XSetFont (FRAME_MAC_DISPLAY (f), f->output_data.mac->cursor_gc,
9593 f->output_data.mac->font);
9594
9595 frame_update_line_height (f);
9596 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
9597 x_set_window_size (f, 0, f->width, f->height);
9598 }
9599 else
9600 /* If we are setting a new frame's font for the first time,
9601 there are no faces yet, so this font's height is the line height. */
9602 f->output_data.mac->line_height = FONT_HEIGHT (FRAME_FONT (f));
9603
9604 return build_string (fontp->full_name);
9605 }
9606 \f
9607 /* Give frame F the fontset named FONTSETNAME as its default font, and
9608 return the full name of that fontset. FONTSETNAME may be a wildcard
9609 pattern; in that case, we choose some fontset that fits the pattern.
9610 The return value shows which fontset we chose. */
9611
9612 Lisp_Object
9613 x_new_fontset (f, fontsetname)
9614 struct frame *f;
9615 char *fontsetname;
9616 {
9617 int fontset = fs_query_fontset (build_string (fontsetname), 0);
9618 Lisp_Object result;
9619
9620 if (fontset < 0)
9621 return Qnil;
9622
9623 if (FRAME_FONTSET (f) == fontset)
9624 /* This fontset is already set in frame F. There's nothing more
9625 to do. */
9626 return fontset_name (fontset);
9627
9628 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
9629
9630 if (!STRINGP (result))
9631 /* Can't load ASCII font. */
9632 return Qnil;
9633
9634 /* Since x_new_font doesn't update any fontset information, do it now. */
9635 FRAME_FONTSET(f) = fontset;
9636
9637 return build_string (fontsetname);
9638 }
9639
9640 /* Compute actual fringe widths */
9641
9642 void
9643 x_compute_fringe_widths (f, redraw)
9644 struct frame *f;
9645 int redraw;
9646 {
9647 int o_left = f->output_data.mac->left_fringe_width;
9648 int o_right = f->output_data.mac->right_fringe_width;
9649 int o_cols = f->output_data.mac->fringe_cols;
9650
9651 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
9652 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
9653 int left_fringe_width, right_fringe_width;
9654
9655 if (!NILP (left_fringe))
9656 left_fringe = Fcdr (left_fringe);
9657 if (!NILP (right_fringe))
9658 right_fringe = Fcdr (right_fringe);
9659
9660 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
9661 XINT (left_fringe));
9662 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
9663 XINT (right_fringe));
9664
9665 if (left_fringe_width || right_fringe_width)
9666 {
9667 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
9668 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
9669 int conf_wid = left_wid + right_wid;
9670 int font_wid = FONT_WIDTH (f->output_data.mac->font);
9671 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
9672 int real_wid = cols * font_wid;
9673 if (left_wid && right_wid)
9674 {
9675 if (left_fringe_width < 0)
9676 {
9677 /* Left fringe width is fixed, adjust right fringe if necessary */
9678 f->output_data.mac->left_fringe_width = left_wid;
9679 f->output_data.mac->right_fringe_width = real_wid - left_wid;
9680 }
9681 else if (right_fringe_width < 0)
9682 {
9683 /* Right fringe width is fixed, adjust left fringe if necessary */
9684 f->output_data.mac->left_fringe_width = real_wid - right_wid;
9685 f->output_data.mac->right_fringe_width = right_wid;
9686 }
9687 else
9688 {
9689 /* Adjust both fringes with an equal amount.
9690 Note that we are doing integer arithmetic here, so don't
9691 lose a pixel if the total width is an odd number. */
9692 int fill = real_wid - conf_wid;
9693 f->output_data.mac->left_fringe_width = left_wid + fill/2;
9694 f->output_data.mac->right_fringe_width = right_wid + fill - fill/2;
9695 }
9696 }
9697 else if (left_fringe_width)
9698 {
9699 f->output_data.mac->left_fringe_width = real_wid;
9700 f->output_data.mac->right_fringe_width = 0;
9701 }
9702 else
9703 {
9704 f->output_data.mac->left_fringe_width = 0;
9705 f->output_data.mac->right_fringe_width = real_wid;
9706 }
9707 f->output_data.mac->fringe_cols = cols;
9708 f->output_data.mac->fringes_extra = real_wid;
9709 }
9710 else
9711 {
9712 f->output_data.mac->left_fringe_width = 0;
9713 f->output_data.mac->right_fringe_width = 0;
9714 f->output_data.mac->fringe_cols = 0;
9715 f->output_data.mac->fringes_extra = 0;
9716 }
9717
9718 if (redraw && FRAME_VISIBLE_P (f))
9719 if (o_left != f->output_data.mac->left_fringe_width ||
9720 o_right != f->output_data.mac->right_fringe_width ||
9721 o_cols != f->output_data.mac->fringe_cols)
9722 redraw_frame (f);
9723 }
9724 \f
9725 /***********************************************************************
9726 TODO: W32 Input Methods
9727 ***********************************************************************/
9728 /* Listing missing functions from xterm.c helps diff stay in step.
9729
9730 xim_destroy_callback (xim, client_data, call_data)
9731 xim_open_dpy (dpyinfo, resource_name)
9732 struct xim_inst_t
9733 xim_instantiate_callback (display, client_data, call_data)
9734 xim_initialize (dpyinfo, resource_name)
9735 xim_close_dpy (dpyinfo)
9736
9737 */
9738
9739 \f
9740 /* Calculate the absolute position in frame F
9741 from its current recorded position values and gravity. */
9742
9743 void
9744 x_calc_absolute_position (f)
9745 struct frame *f;
9746 {
9747 Point pt;
9748 int flags = f->output_data.mac->size_hint_flags;
9749
9750 pt.h = pt.v = 0;
9751
9752 /* Find the position of the outside upper-left corner of
9753 the inner window, with respect to the outer window. */
9754 if (f->output_data.mac->parent_desc != FRAME_MAC_DISPLAY_INFO (f)->root_window)
9755 {
9756 GrafPtr savePort;
9757 GetPort (&savePort);
9758
9759 #if TARGET_API_MAC_CARBON
9760 SetPort (GetWindowPort (FRAME_MAC_WINDOW (f)));
9761 #else
9762 SetPort (FRAME_MAC_WINDOW (f));
9763 #endif
9764
9765 #if TARGET_API_MAC_CARBON
9766 {
9767 Rect r;
9768
9769 GetWindowPortBounds (FRAME_MAC_WINDOW (f), &r);
9770 SetPt(&pt, r.left, r.top);
9771 }
9772 #else /* not TARGET_API_MAC_CARBON */
9773 SetPt(&pt, FRAME_MAC_WINDOW (f)->portRect.left, FRAME_MAC_WINDOW (f)->portRect.top);
9774 #endif /* not TARGET_API_MAC_CARBON */
9775 LocalToGlobal (&pt);
9776 SetPort (savePort);
9777 }
9778
9779 /* Treat negative positions as relative to the leftmost bottommost
9780 position that fits on the screen. */
9781 if (flags & XNegative)
9782 f->output_data.mac->left_pos = (FRAME_MAC_DISPLAY_INFO (f)->width
9783 - 2 * f->output_data.mac->border_width - pt.h
9784 - PIXEL_WIDTH (f)
9785 + f->output_data.mac->left_pos);
9786 /* NTEMACS_TODO: Subtract menubar height? */
9787 if (flags & YNegative)
9788 f->output_data.mac->top_pos = (FRAME_MAC_DISPLAY_INFO (f)->height
9789 - 2 * f->output_data.mac->border_width - pt.v
9790 - PIXEL_HEIGHT (f)
9791 + f->output_data.mac->top_pos);
9792 /* The left_pos and top_pos
9793 are now relative to the top and left screen edges,
9794 so the flags should correspond. */
9795 f->output_data.mac->size_hint_flags &= ~ (XNegative | YNegative);
9796 }
9797
9798 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
9799 to really change the position, and 0 when calling from
9800 x_make_frame_visible (in that case, XOFF and YOFF are the current
9801 position values). It is -1 when calling from x_set_frame_parameters,
9802 which means, do adjust for borders but don't change the gravity. */
9803
9804 void
9805 x_set_offset (f, xoff, yoff, change_gravity)
9806 struct frame *f;
9807 register int xoff, yoff;
9808 int change_gravity;
9809 {
9810 int modified_top, modified_left;
9811
9812 if (change_gravity > 0)
9813 {
9814 f->output_data.mac->top_pos = yoff;
9815 f->output_data.mac->left_pos = xoff;
9816 f->output_data.mac->size_hint_flags &= ~ (XNegative | YNegative);
9817 if (xoff < 0)
9818 f->output_data.mac->size_hint_flags |= XNegative;
9819 if (yoff < 0)
9820 f->output_data.mac->size_hint_flags |= YNegative;
9821 f->output_data.mac->win_gravity = NorthWestGravity;
9822 }
9823 x_calc_absolute_position (f);
9824
9825 BLOCK_INPUT;
9826 x_wm_set_size_hint (f, (long) 0, 0);
9827
9828 modified_left = f->output_data.mac->left_pos;
9829 modified_top = f->output_data.mac->top_pos;
9830
9831 MoveWindow (f->output_data.mac->mWP, modified_left + 6,
9832 modified_top + 42, false);
9833
9834 UNBLOCK_INPUT;
9835 }
9836
9837 /* Call this to change the size of frame F's x-window.
9838 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
9839 for this size change and subsequent size changes.
9840 Otherwise we leave the window gravity unchanged. */
9841
9842 void
9843 x_set_window_size (f, change_gravity, cols, rows)
9844 struct frame *f;
9845 int change_gravity;
9846 int cols, rows;
9847 {
9848 int pixelwidth, pixelheight;
9849
9850 BLOCK_INPUT;
9851
9852 check_frame_size (f, &rows, &cols);
9853 f->output_data.mac->vertical_scroll_bar_extra
9854 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
9855 ? 0
9856 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.mac->font)));
9857
9858 x_compute_fringe_widths (f, 0);
9859
9860 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
9861 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
9862
9863 f->output_data.mac->win_gravity = NorthWestGravity;
9864 x_wm_set_size_hint (f, (long) 0, 0);
9865
9866 SizeWindow (FRAME_MAC_WINDOW (f), pixelwidth, pixelheight, 0);
9867
9868 /* Now, strictly speaking, we can't be sure that this is accurate,
9869 but the window manager will get around to dealing with the size
9870 change request eventually, and we'll hear how it went when the
9871 ConfigureNotify event gets here.
9872
9873 We could just not bother storing any of this information here,
9874 and let the ConfigureNotify event set everything up, but that
9875 might be kind of confusing to the Lisp code, since size changes
9876 wouldn't be reported in the frame parameters until some random
9877 point in the future when the ConfigureNotify event arrives.
9878
9879 We pass 1 for DELAY since we can't run Lisp code inside of
9880 a BLOCK_INPUT. */
9881 change_frame_size (f, rows, cols, 0, 1, 0);
9882 PIXEL_WIDTH (f) = pixelwidth;
9883 PIXEL_HEIGHT (f) = pixelheight;
9884
9885 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
9886 receive in the ConfigureNotify event; if we get what we asked
9887 for, then the event won't cause the screen to become garbaged, so
9888 we have to make sure to do it here. */
9889 SET_FRAME_GARBAGED (f);
9890
9891 XFlush (FRAME_X_DISPLAY (f));
9892
9893 /* If cursor was outside the new size, mark it as off. */
9894 mark_window_cursors_off (XWINDOW (f->root_window));
9895
9896 /* Clear out any recollection of where the mouse highlighting was,
9897 since it might be in a place that's outside the new frame size.
9898 Actually checking whether it is outside is a pain in the neck,
9899 so don't try--just let the highlighting be done afresh with new size. */
9900 cancel_mouse_face (f);
9901
9902 UNBLOCK_INPUT;
9903 }
9904 \f
9905 /* Mouse warping. */
9906
9907 void x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y);
9908
9909 void
9910 x_set_mouse_position (f, x, y)
9911 struct frame *f;
9912 int x, y;
9913 {
9914 int pix_x, pix_y;
9915
9916 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.mac->font) / 2;
9917 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.mac->line_height / 2;
9918
9919 if (pix_x < 0) pix_x = 0;
9920 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
9921
9922 if (pix_y < 0) pix_y = 0;
9923 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
9924
9925 x_set_mouse_pixel_position (f, pix_x, pix_y);
9926 }
9927
9928 void
9929 x_set_mouse_pixel_position (f, pix_x, pix_y)
9930 struct frame *f;
9931 int pix_x, pix_y;
9932 {
9933 #if 0 /* MAC_TODO: CursorDeviceMoveTo is non-Carbon */
9934 BLOCK_INPUT;
9935
9936 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
9937 0, 0, 0, 0, pix_x, pix_y);
9938 UNBLOCK_INPUT;
9939 #endif
9940 }
9941
9942 \f
9943 /* focus shifting, raising and lowering. */
9944
9945 void
9946 x_focus_on_frame (f)
9947 struct frame *f;
9948 {
9949 #if 0 /* This proves to be unpleasant. */
9950 x_raise_frame (f);
9951 #endif
9952 #if 0
9953 /* I don't think that the ICCCM allows programs to do things like this
9954 without the interaction of the window manager. Whatever you end up
9955 doing with this code, do it to x_unfocus_frame too. */
9956 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9957 RevertToPointerRoot, CurrentTime);
9958 #endif /* ! 0 */
9959 }
9960
9961 void
9962 x_unfocus_frame (f)
9963 struct frame *f;
9964 {
9965 }
9966
9967 /* Raise frame F. */
9968 void
9969 x_raise_frame (f)
9970 struct frame *f;
9971 {
9972 if (f->async_visible)
9973 SelectWindow (FRAME_MAC_WINDOW (f));
9974 }
9975
9976 /* Lower frame F. */
9977 void
9978 x_lower_frame (f)
9979 struct frame *f;
9980 {
9981 if (f->async_visible)
9982 SendBehind (FRAME_MAC_WINDOW (f), nil);
9983 }
9984
9985 static void
9986 XTframe_raise_lower (f, raise_flag)
9987 FRAME_PTR f;
9988 int raise_flag;
9989 {
9990 if (raise_flag)
9991 x_raise_frame (f);
9992 else
9993 x_lower_frame (f);
9994 }
9995 \f
9996 /* Change of visibility. */
9997
9998 /* This tries to wait until the frame is really visible.
9999 However, if the window manager asks the user where to position
10000 the frame, this will return before the user finishes doing that.
10001 The frame will not actually be visible at that time,
10002 but it will become visible later when the window manager
10003 finishes with it. */
10004
10005 void
10006 x_make_frame_visible (f)
10007 struct frame *f;
10008 {
10009 Lisp_Object type;
10010 int original_top, original_left;
10011
10012 BLOCK_INPUT;
10013
10014 if (! FRAME_VISIBLE_P (f))
10015 {
10016 /* We test FRAME_GARBAGED_P here to make sure we don't
10017 call x_set_offset a second time
10018 if we get to x_make_frame_visible a second time
10019 before the window gets really visible. */
10020 if (! FRAME_ICONIFIED_P (f)
10021 && ! f->output_data.mac->asked_for_visible)
10022 x_set_offset (f, f->output_data.mac->left_pos,
10023 f->output_data.mac->top_pos, 0);
10024
10025 f->output_data.mac->asked_for_visible = 1;
10026
10027 ShowWindow (FRAME_MAC_WINDOW (f));
10028 }
10029
10030 XFlush (FRAME_MAC_DISPLAY (f));
10031
10032 #if 0 /* MAC_TODO */
10033 /* Synchronize to ensure Emacs knows the frame is visible
10034 before we do anything else. We do this loop with input not blocked
10035 so that incoming events are handled. */
10036 {
10037 Lisp_Object frame;
10038 int count;
10039
10040 /* This must come after we set COUNT. */
10041 UNBLOCK_INPUT;
10042
10043 XSETFRAME (frame, f);
10044
10045 /* Wait until the frame is visible. Process X events until a
10046 MapNotify event has been seen, or until we think we won't get a
10047 MapNotify at all.. */
10048 for (count = input_signal_count + 10;
10049 input_signal_count < count && !FRAME_VISIBLE_P (f);)
10050 {
10051 /* Force processing of queued events. */
10052 x_sync (f);
10053
10054 /* Machines that do polling rather than SIGIO have been
10055 observed to go into a busy-wait here. So we'll fake an
10056 alarm signal to let the handler know that there's something
10057 to be read. We used to raise a real alarm, but it seems
10058 that the handler isn't always enabled here. This is
10059 probably a bug. */
10060 if (input_polling_used ())
10061 {
10062 /* It could be confusing if a real alarm arrives while
10063 processing the fake one. Turn it off and let the
10064 handler reset it. */
10065 extern void poll_for_input_1 P_ ((void));
10066 int old_poll_suppress_count = poll_suppress_count;
10067 poll_suppress_count = 1;
10068 poll_for_input_1 ();
10069 poll_suppress_count = old_poll_suppress_count;
10070 }
10071
10072 /* See if a MapNotify event has been processed. */
10073 FRAME_SAMPLE_VISIBILITY (f);
10074 }
10075 }
10076 #endif /* MAC_TODO */
10077 }
10078
10079 /* Change from mapped state to withdrawn state. */
10080
10081 /* Make the frame visible (mapped and not iconified). */
10082
10083 void
10084 x_make_frame_invisible (f)
10085 struct frame *f;
10086 {
10087 /* Don't keep the highlight on an invisible frame. */
10088 if (FRAME_MAC_DISPLAY_INFO (f)->x_highlight_frame == f)
10089 FRAME_MAC_DISPLAY_INFO (f)->x_highlight_frame = 0;
10090
10091 BLOCK_INPUT;
10092
10093 HideWindow (FRAME_MAC_WINDOW (f));
10094
10095 /* We can't distinguish this from iconification
10096 just by the event that we get from the server.
10097 So we can't win using the usual strategy of letting
10098 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
10099 and synchronize with the server to make sure we agree. */
10100 f->visible = 0;
10101 FRAME_ICONIFIED_P (f) = 0;
10102 f->async_visible = 0;
10103 f->async_iconified = 0;
10104
10105 UNBLOCK_INPUT;
10106 }
10107
10108 /* Change window state from mapped to iconified. */
10109
10110 void
10111 x_iconify_frame (f)
10112 struct frame *f;
10113 {
10114 /* Don't keep the highlight on an invisible frame. */
10115 if (FRAME_MAC_DISPLAY_INFO (f)->x_highlight_frame == f)
10116 FRAME_MAC_DISPLAY_INFO (f)->x_highlight_frame = 0;
10117
10118 #if 0
10119 /* Review: Since window is still visible in dock, still allow updates? */
10120 if (f->async_iconified)
10121 return;
10122 #endif
10123
10124 BLOCK_INPUT;
10125
10126 CollapseWindow (FRAME_MAC_WINDOW (f), true);
10127
10128 UNBLOCK_INPUT;
10129 }
10130
10131 \f
10132 /* Destroy the X window of frame F. */
10133
10134 void
10135 x_destroy_window (f)
10136 struct frame *f;
10137 {
10138 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
10139
10140 BLOCK_INPUT;
10141
10142 DisposeWindow (FRAME_MAC_WINDOW (f));
10143
10144 free_frame_menubar (f);
10145 free_frame_faces (f);
10146
10147 xfree (f->output_data.mac);
10148 f->output_data.mac = 0;
10149 if (f == dpyinfo->x_focus_frame)
10150 dpyinfo->x_focus_frame = 0;
10151 if (f == dpyinfo->x_focus_event_frame)
10152 dpyinfo->x_focus_event_frame = 0;
10153 if (f == dpyinfo->x_highlight_frame)
10154 dpyinfo->x_highlight_frame = 0;
10155
10156 dpyinfo->reference_count--;
10157
10158 if (f == dpyinfo->mouse_face_mouse_frame)
10159 {
10160 dpyinfo->mouse_face_beg_row
10161 = dpyinfo->mouse_face_beg_col = -1;
10162 dpyinfo->mouse_face_end_row
10163 = dpyinfo->mouse_face_end_col = -1;
10164 dpyinfo->mouse_face_window = Qnil;
10165 dpyinfo->mouse_face_deferred_gc = 0;
10166 dpyinfo->mouse_face_mouse_frame = 0;
10167 }
10168
10169 UNBLOCK_INPUT;
10170 }
10171 \f
10172 /* Setting window manager hints. */
10173
10174 /* Set the normal size hints for the window manager, for frame F.
10175 FLAGS is the flags word to use--or 0 meaning preserve the flags
10176 that the window now has.
10177 If USER_POSITION is nonzero, we set the USPosition
10178 flag (this is useful when FLAGS is 0). */
10179 void
10180 x_wm_set_size_hint (f, flags, user_position)
10181 struct frame *f;
10182 long flags;
10183 int user_position;
10184 {
10185 #if 0 /* MAC_TODO: connect this to the Appearance Manager */
10186 XSizeHints size_hints;
10187
10188 #ifdef USE_X_TOOLKIT
10189 Arg al[2];
10190 int ac = 0;
10191 Dimension widget_width, widget_height;
10192 Window window = XtWindow (f->output_data.x->widget);
10193 #else /* not USE_X_TOOLKIT */
10194 Window window = FRAME_X_WINDOW (f);
10195 #endif /* not USE_X_TOOLKIT */
10196
10197 /* Setting PMaxSize caused various problems. */
10198 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
10199
10200 size_hints.x = f->output_data.x->left_pos;
10201 size_hints.y = f->output_data.x->top_pos;
10202
10203 #ifdef USE_X_TOOLKIT
10204 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
10205 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
10206 XtGetValues (f->output_data.x->widget, al, ac);
10207 size_hints.height = widget_height;
10208 size_hints.width = widget_width;
10209 #else /* not USE_X_TOOLKIT */
10210 size_hints.height = PIXEL_HEIGHT (f);
10211 size_hints.width = PIXEL_WIDTH (f);
10212 #endif /* not USE_X_TOOLKIT */
10213
10214 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
10215 size_hints.height_inc = f->output_data.x->line_height;
10216 size_hints.max_width
10217 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
10218 size_hints.max_height
10219 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
10220
10221 /* Calculate the base and minimum sizes.
10222
10223 (When we use the X toolkit, we don't do it here.
10224 Instead we copy the values that the widgets are using, below.) */
10225 #ifndef USE_X_TOOLKIT
10226 {
10227 int base_width, base_height;
10228 int min_rows = 0, min_cols = 0;
10229
10230 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
10231 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
10232
10233 check_frame_size (f, &min_rows, &min_cols);
10234
10235 /* The window manager uses the base width hints to calculate the
10236 current number of rows and columns in the frame while
10237 resizing; min_width and min_height aren't useful for this
10238 purpose, since they might not give the dimensions for a
10239 zero-row, zero-column frame.
10240
10241 We use the base_width and base_height members if we have
10242 them; otherwise, we set the min_width and min_height members
10243 to the size for a zero x zero frame. */
10244
10245 #ifdef HAVE_X11R4
10246 size_hints.flags |= PBaseSize;
10247 size_hints.base_width = base_width;
10248 size_hints.base_height = base_height;
10249 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
10250 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
10251 #else
10252 size_hints.min_width = base_width;
10253 size_hints.min_height = base_height;
10254 #endif
10255 }
10256
10257 /* If we don't need the old flags, we don't need the old hint at all. */
10258 if (flags)
10259 {
10260 size_hints.flags |= flags;
10261 goto no_read;
10262 }
10263 #endif /* not USE_X_TOOLKIT */
10264
10265 {
10266 XSizeHints hints; /* Sometimes I hate X Windows... */
10267 long supplied_return;
10268 int value;
10269
10270 #ifdef HAVE_X11R4
10271 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
10272 &supplied_return);
10273 #else
10274 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
10275 #endif
10276
10277 #ifdef USE_X_TOOLKIT
10278 size_hints.base_height = hints.base_height;
10279 size_hints.base_width = hints.base_width;
10280 size_hints.min_height = hints.min_height;
10281 size_hints.min_width = hints.min_width;
10282 #endif
10283
10284 if (flags)
10285 size_hints.flags |= flags;
10286 else
10287 {
10288 if (value == 0)
10289 hints.flags = 0;
10290 if (hints.flags & PSize)
10291 size_hints.flags |= PSize;
10292 if (hints.flags & PPosition)
10293 size_hints.flags |= PPosition;
10294 if (hints.flags & USPosition)
10295 size_hints.flags |= USPosition;
10296 if (hints.flags & USSize)
10297 size_hints.flags |= USSize;
10298 }
10299 }
10300
10301 #ifndef USE_X_TOOLKIT
10302 no_read:
10303 #endif
10304
10305 #ifdef PWinGravity
10306 size_hints.win_gravity = f->output_data.x->win_gravity;
10307 size_hints.flags |= PWinGravity;
10308
10309 if (user_position)
10310 {
10311 size_hints.flags &= ~ PPosition;
10312 size_hints.flags |= USPosition;
10313 }
10314 #endif /* PWinGravity */
10315
10316 #ifdef HAVE_X11R4
10317 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
10318 #else
10319 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
10320 #endif
10321 #endif /* MAC_TODO */
10322 }
10323
10324 #if 0 /* MAC_TODO: hide application instead of iconify? */
10325 /* Used for IconicState or NormalState */
10326
10327 void
10328 x_wm_set_window_state (f, state)
10329 struct frame *f;
10330 int state;
10331 {
10332 #ifdef USE_X_TOOLKIT
10333 Arg al[1];
10334
10335 XtSetArg (al[0], XtNinitialState, state);
10336 XtSetValues (f->output_data.x->widget, al, 1);
10337 #else /* not USE_X_TOOLKIT */
10338 Window window = FRAME_X_WINDOW (f);
10339
10340 f->output_data.x->wm_hints.flags |= StateHint;
10341 f->output_data.x->wm_hints.initial_state = state;
10342
10343 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
10344 #endif /* not USE_X_TOOLKIT */
10345 }
10346
10347 void
10348 x_wm_set_icon_pixmap (f, pixmap_id)
10349 struct frame *f;
10350 int pixmap_id;
10351 {
10352 Pixmap icon_pixmap;
10353
10354 #ifndef USE_X_TOOLKIT
10355 Window window = FRAME_X_WINDOW (f);
10356 #endif
10357
10358 if (pixmap_id > 0)
10359 {
10360 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
10361 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
10362 }
10363 else
10364 {
10365 /* It seems there is no way to turn off use of an icon pixmap.
10366 The following line does it, only if no icon has yet been created,
10367 for some window managers. But with mwm it crashes.
10368 Some people say it should clear the IconPixmapHint bit in this case,
10369 but that doesn't work, and the X consortium said it isn't the
10370 right thing at all. Since there is no way to win,
10371 best to explicitly give up. */
10372 #if 0
10373 f->output_data.x->wm_hints.icon_pixmap = None;
10374 #else
10375 return;
10376 #endif
10377 }
10378
10379 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
10380
10381 {
10382 Arg al[1];
10383 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
10384 XtSetValues (f->output_data.x->widget, al, 1);
10385 }
10386
10387 #else /* not USE_X_TOOLKIT */
10388
10389 f->output_data.x->wm_hints.flags |= IconPixmapHint;
10390 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
10391
10392 #endif /* not USE_X_TOOLKIT */
10393 }
10394
10395 #endif /* MAC_TODO */
10396
10397 void
10398 x_wm_set_icon_position (f, icon_x, icon_y)
10399 struct frame *f;
10400 int icon_x, icon_y;
10401 {
10402 #if 0 /* MAC_TODO: no icons on Mac */
10403 #ifdef USE_X_TOOLKIT
10404 Window window = XtWindow (f->output_data.x->widget);
10405 #else
10406 Window window = FRAME_X_WINDOW (f);
10407 #endif
10408
10409 f->output_data.x->wm_hints.flags |= IconPositionHint;
10410 f->output_data.x->wm_hints.icon_x = icon_x;
10411 f->output_data.x->wm_hints.icon_y = icon_y;
10412
10413 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
10414 #endif /* MAC_TODO */
10415 }
10416
10417 \f
10418 /***********************************************************************
10419 Fonts
10420 ***********************************************************************/
10421
10422 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
10423
10424 struct font_info *
10425 x_get_font_info (f, font_idx)
10426 FRAME_PTR f;
10427 int font_idx;
10428 {
10429 return (FRAME_MAC_FONT_TABLE (f) + font_idx);
10430 }
10431
10432 /* the global font name table */
10433 char **font_name_table = NULL;
10434 int font_name_table_size = 0;
10435 int font_name_count = 0;
10436
10437 /* compare two strings ignoring case */
10438 static int
10439 stricmp (const char *s, const char *t)
10440 {
10441 for ( ; tolower (*s) == tolower (*t); s++, t++)
10442 if (*s == '\0')
10443 return 0;
10444 return tolower (*s) - tolower (*t);
10445 }
10446
10447 /* compare two strings ignoring case and handling wildcard */
10448 static int
10449 wildstrieq (char *s1, char *s2)
10450 {
10451 if (strcmp (s1, "*") == 0 || strcmp (s2, "*") == 0)
10452 return true;
10453
10454 return stricmp (s1, s2) == 0;
10455 }
10456
10457 /* Assume parameter 1 is fully qualified, no wildcards. */
10458 static int
10459 mac_font_pattern_match (fontname, pattern)
10460 char * fontname;
10461 char * pattern;
10462 {
10463 char *regex = (char *) alloca (strlen (pattern) * 2 + 3);
10464 char *font_name_copy = (char *) alloca (strlen (fontname) + 1);
10465 char *ptr;
10466
10467 /* Copy fontname so we can modify it during comparison. */
10468 strcpy (font_name_copy, fontname);
10469
10470 ptr = regex;
10471 *ptr++ = '^';
10472
10473 /* Turn pattern into a regexp and do a regexp match. */
10474 for (; *pattern; pattern++)
10475 {
10476 if (*pattern == '?')
10477 *ptr++ = '.';
10478 else if (*pattern == '*')
10479 {
10480 *ptr++ = '.';
10481 *ptr++ = '*';
10482 }
10483 else
10484 *ptr++ = *pattern;
10485 }
10486 *ptr = '$';
10487 *(ptr + 1) = '\0';
10488
10489 return (fast_c_string_match_ignore_case (build_string (regex),
10490 font_name_copy) >= 0);
10491 }
10492
10493 /* Two font specs are considered to match if their foundry, family,
10494 weight, slant, and charset match. */
10495 static int
10496 mac_font_match (char *mf, char *xf)
10497 {
10498 char m_foundry[50], m_family[50], m_weight[20], m_slant[2], m_charset[20];
10499 char x_foundry[50], x_family[50], x_weight[20], x_slant[2], x_charset[20];
10500
10501 if (sscanf (mf, "-%49[^-]-%49[^-]-%19[^-]-%1[^-]-%*[^-]--%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*c-%*[^-]-%19s",
10502 m_foundry, m_family, m_weight, m_slant, m_charset) != 5)
10503 return mac_font_pattern_match (mf, xf);
10504
10505 if (sscanf (xf, "-%49[^-]-%49[^-]-%19[^-]-%1[^-]-%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*c-%*[^-]-%19s",
10506 x_foundry, x_family, x_weight, x_slant, x_charset) != 5)
10507 return mac_font_pattern_match (mf, xf);
10508
10509 return (wildstrieq (m_foundry, x_foundry)
10510 && wildstrieq (m_family, x_family)
10511 && wildstrieq (m_weight, x_weight)
10512 && wildstrieq (m_slant, x_slant)
10513 && wildstrieq (m_charset, x_charset))
10514 || mac_font_pattern_match (mf, xf);
10515 }
10516
10517
10518 static char *
10519 mac_to_x_fontname (char *name, int size, Style style, short scriptcode)
10520 {
10521 char foundry[32], family[32], cs[32];
10522 char xf[255], *result, *p;
10523
10524 if (sscanf (name, "%31[^-]-%31[^-]-%31s", foundry, family, cs) != 3)
10525 {
10526 strcpy(foundry, "Apple");
10527 strcpy(family, name);
10528
10529 switch (scriptcode)
10530 {
10531 case smTradChinese:
10532 strcpy(cs, "big5-0");
10533 break;
10534 case smSimpChinese:
10535 strcpy(cs, "gb2312.1980-0");
10536 break;
10537 case smJapanese:
10538 strcpy(cs, "jisx0208.1983-sjis");
10539 break;
10540 case -smJapanese:
10541 /* Each Apple Japanese font is entered into the font table
10542 twice: once as a jisx0208.1983-sjis font and once as a
10543 jisx0201.1976-0 font. The latter can be used to display
10544 the ascii charset and katakana-jisx0201 charset. A
10545 negative script code signals that the name of this latter
10546 font is being built. */
10547 strcpy(cs, "jisx0201.1976-0");
10548 break;
10549 case smKorean:
10550 strcpy(cs, "ksc5601.1989-0");
10551 break;
10552 default:
10553 strcpy(cs, "mac-roman");
10554 break;
10555 }
10556 }
10557
10558 sprintf(xf, "-%s-%s-%s-%c-normal--%d-%d-75-75-m-%d-%s",
10559 foundry, family, style & bold ? "bold" : "medium",
10560 style & italic ? 'i' : 'r', size, size * 10, size * 10, cs);
10561
10562 result = (char *) xmalloc (strlen (xf) + 1);
10563 strcpy (result, xf);
10564 for (p = result; *p; p++)
10565 *p = tolower(*p);
10566 return result;
10567 }
10568
10569
10570 /* Convert an X font spec to the corresponding mac font name, which
10571 can then be passed to GetFNum after conversion to a Pascal string.
10572 For ordinary Mac fonts, this should just be their names, like
10573 "monaco", "Taipei", etc. Fonts converted from the GNU intlfonts
10574 collection contain their charset designation in their names, like
10575 "ETL-Fixed-iso8859-1", "ETL-Fixed-koi8-r", etc. Both types of font
10576 names are handled accordingly. */
10577 static void
10578 x_font_name_to_mac_font_name (char *xf, char *mf)
10579 {
10580 char foundry[32], family[32], weight[20], slant[2], cs[32];
10581
10582 strcpy (mf, "");
10583
10584 if (sscanf (xf, "-%31[^-]-%31[^-]-%19[^-]-%1[^-]-%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*c-%*[^-]-%31s",
10585 foundry, family, weight, slant, cs) != 5 &&
10586 sscanf (xf, "-%31[^-]-%31[^-]-%19[^-]-%1[^-]-%*[^-]--%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*c-%*[^-]-%31s",
10587 foundry, family, weight, slant, cs) != 5)
10588 return;
10589
10590 if (strcmp (cs, "big5-0") == 0 || strcmp (cs, "gb2312.1980-0") == 0
10591 || strcmp (cs, "jisx0208.1983-sjis") == 0
10592 || strcmp (cs, "jisx0201.1976-0") == 0
10593 || strcmp (cs, "ksc5601.1989-0") == 0 || strcmp (cs, "mac-roman") == 0)
10594 strcpy(mf, family);
10595 else
10596 sprintf(mf, "%s-%s-%s", foundry, family, cs);
10597 }
10598
10599
10600 static void
10601 add_font_name_table_entry (char *font_name)
10602 {
10603 if (font_name_table_size == 0)
10604 {
10605 font_name_table_size = 16;
10606 font_name_table = (char **)
10607 xmalloc (font_name_table_size * sizeof (char *));
10608 }
10609 else if (font_name_count + 1 >= font_name_table_size)
10610 {
10611 font_name_table_size += 16;
10612 font_name_table = (char **)
10613 xrealloc (font_name_table,
10614 font_name_table_size * sizeof (char *));
10615 }
10616
10617 font_name_table[font_name_count++] = font_name;
10618 }
10619
10620 /* Sets up the table font_name_table to contain the list of all fonts
10621 in the system the first time the table is used so that the Resource
10622 Manager need not be accessed every time this information is
10623 needed. */
10624
10625 static void
10626 init_font_name_table ()
10627 {
10628 #if TARGET_API_MAC_CARBON
10629 SInt32 sv;
10630
10631 if (Gestalt (gestaltSystemVersion, &sv) == noErr && sv >= 0x1000)
10632 {
10633 FMFontFamilyIterator ffi;
10634 FMFontFamilyInstanceIterator ffii;
10635 FMFontFamily ff;
10636
10637 /* Create a dummy instance iterator here to avoid creating and
10638 destroying it in the loop. */
10639 if (FMCreateFontFamilyInstanceIterator (0, &ffii) != noErr)
10640 return;
10641 /* Create an iterator to enumerate the font families. */
10642 if (FMCreateFontFamilyIterator (NULL, NULL, kFMDefaultOptions, &ffi)
10643 != noErr)
10644 {
10645 FMDisposeFontFamilyInstanceIterator (&ffii);
10646 return;
10647 }
10648
10649 while (FMGetNextFontFamily (&ffi, &ff) == noErr)
10650 {
10651 Str255 name;
10652 FMFont font;
10653 FMFontStyle style;
10654 FMFontSize size;
10655 SInt16 sc;
10656
10657 if (FMGetFontFamilyName (ff, name) != noErr)
10658 break;
10659 p2cstr (name);
10660
10661 sc = FontToScript (ff);
10662
10663 /* Point the instance iterator at the current font family. */
10664 if (FMResetFontFamilyInstanceIterator(ff, &ffii) != noErr)
10665 break;
10666
10667 while (FMGetNextFontFamilyInstance (&ffii, &font, &style, &size)
10668 == noErr)
10669 if (size == 0)
10670 {
10671 add_font_name_table_entry (mac_to_x_fontname (name, size,
10672 style, sc));
10673 add_font_name_table_entry (mac_to_x_fontname (name, size,
10674 italic, sc));
10675 add_font_name_table_entry (mac_to_x_fontname (name, size,
10676 bold, sc));
10677 add_font_name_table_entry (mac_to_x_fontname (name, size,
10678 italic | bold,
10679 sc));
10680 }
10681 else
10682 {
10683 add_font_name_table_entry (mac_to_x_fontname (name, size,
10684 style, sc));
10685 if (smJapanese == sc)
10686 add_font_name_table_entry (mac_to_x_fontname (name, size,
10687 style,
10688 -smJapanese));
10689 }
10690 }
10691
10692 /* Dispose of the iterators. */
10693 FMDisposeFontFamilyIterator (&ffi);
10694 FMDisposeFontFamilyInstanceIterator (&ffii);
10695 }
10696 else
10697 {
10698 #endif /* TARGET_API_MAC_CARBON */
10699 GrafPtr port;
10700 SInt16 fontnum, old_fontnum;
10701 int num_mac_fonts = CountResources('FOND');
10702 int i, j;
10703 Handle font_handle, font_handle_2;
10704 short id, scriptcode;
10705 ResType type;
10706 Str32 name;
10707 struct FontAssoc *fat;
10708 struct AsscEntry *assc_entry;
10709
10710 GetPort (&port); /* save the current font number used */
10711 #if TARGET_API_MAC_CARBON
10712 old_fontnum = GetPortTextFont (port);
10713 #else
10714 old_fontnum = port->txFont;
10715 #endif
10716
10717 for (i = 1; i <= num_mac_fonts; i++) /* get all available fonts */
10718 {
10719 font_handle = GetIndResource ('FOND', i);
10720 if (!font_handle)
10721 continue;
10722
10723 GetResInfo (font_handle, &id, &type, name);
10724 GetFNum (name, &fontnum);
10725 p2cstr (name);
10726 if (fontnum == 0)
10727 continue;
10728
10729 TextFont (fontnum);
10730 scriptcode = FontToScript (fontnum);
10731 do
10732 {
10733 HLock (font_handle);
10734
10735 if (GetResourceSizeOnDisk (font_handle)
10736 >= sizeof (struct FamRec))
10737 {
10738 fat = (struct FontAssoc *) (*font_handle
10739 + sizeof (struct FamRec));
10740 assc_entry
10741 = (struct AsscEntry *) (*font_handle
10742 + sizeof (struct FamRec)
10743 + sizeof (struct FontAssoc));
10744
10745 for (j = 0; j <= fat->numAssoc; j++, assc_entry++)
10746 {
10747 if (font_name_table_size == 0)
10748 {
10749 font_name_table_size = 16;
10750 font_name_table = (char **)
10751 xmalloc (font_name_table_size * sizeof (char *));
10752 }
10753 else if (font_name_count >= font_name_table_size)
10754 {
10755 font_name_table_size += 16;
10756 font_name_table = (char **)
10757 xrealloc (font_name_table,
10758 font_name_table_size * sizeof (char *));
10759 }
10760 font_name_table[font_name_count++]
10761 = mac_to_x_fontname (name,
10762 assc_entry->fontSize,
10763 assc_entry->fontStyle,
10764 scriptcode);
10765 /* Both jisx0208.1983-sjis and
10766 jisx0201.1976-sjis parts are contained in
10767 Apple Japanese (SJIS) font. */
10768 if (smJapanese == scriptcode)
10769 {
10770 font_name_table[font_name_count++]
10771 = mac_to_x_fontname (name,
10772 assc_entry->fontSize,
10773 assc_entry->fontStyle,
10774 -smJapanese);
10775 }
10776 }
10777 }
10778
10779 HUnlock (font_handle);
10780 font_handle_2 = GetNextFOND (font_handle);
10781 ReleaseResource (font_handle);
10782 font_handle = font_handle_2;
10783 }
10784 while (ResError () == noErr && font_handle);
10785 }
10786
10787 TextFont (old_fontnum);
10788 #if TARGET_API_MAC_CARBON
10789 }
10790 #endif /* TARGET_API_MAC_CARBON */
10791 }
10792
10793
10794 /* Return a list of at most MAXNAMES font specs matching the one in
10795 PATTERN. Cache matching fonts for patterns in
10796 dpyinfo->name_list_element to avoid looking them up again by
10797 calling mac_font_pattern_match (slow). */
10798
10799 Lisp_Object
10800 x_list_fonts (struct frame *f,
10801 Lisp_Object pattern,
10802 int size,
10803 int maxnames)
10804 {
10805 char *ptnstr;
10806 Lisp_Object newlist = Qnil, tem, key;
10807 int n_fonts = 0;
10808 int i;
10809 struct gcpro gcpro1, gcpro2;
10810 struct mac_display_info *dpyinfo = f ? FRAME_MAC_DISPLAY_INFO (f) : NULL;
10811
10812 if (font_name_table == NULL) /* Initialize when first used. */
10813 init_font_name_table ();
10814
10815 if (dpyinfo)
10816 {
10817 tem = XCDR (dpyinfo->name_list_element);
10818 key = Fcons (pattern, make_number (maxnames));
10819
10820 newlist = Fassoc (key, tem);
10821 if (!NILP (newlist))
10822 {
10823 newlist = Fcdr_safe (newlist);
10824 goto label_cached;
10825 }
10826 }
10827
10828 ptnstr = SDATA (pattern);
10829
10830 GCPRO2 (pattern, newlist);
10831
10832 /* Scan and matching bitmap fonts. */
10833 for (i = 0; i < font_name_count; i++)
10834 {
10835 if (mac_font_pattern_match (font_name_table[i], ptnstr))
10836 {
10837 newlist = Fcons (build_string (font_name_table[i]), newlist);
10838
10839 n_fonts++;
10840 if (n_fonts >= maxnames)
10841 break;
10842 }
10843 }
10844
10845 /* MAC_TODO: add code for matching outline fonts here */
10846
10847 UNGCPRO;
10848
10849 if (dpyinfo)
10850 {
10851 XSETCDR (dpyinfo->name_list_element,
10852 Fcons (Fcons (key, newlist),
10853 XCDR (dpyinfo->name_list_element)));
10854 }
10855 label_cached:
10856
10857 return newlist;
10858 }
10859
10860
10861 #if GLYPH_DEBUG
10862
10863 /* Check that FONT is valid on frame F. It is if it can be found in F's
10864 font table. */
10865
10866 static void
10867 x_check_font (f, font)
10868 struct frame *f;
10869 XFontStruct *font;
10870 {
10871 int i;
10872 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10873
10874 xassert (font != NULL);
10875
10876 for (i = 0; i < dpyinfo->n_fonts; i++)
10877 if (dpyinfo->font_table[i].name
10878 && font == dpyinfo->font_table[i].font)
10879 break;
10880
10881 xassert (i < dpyinfo->n_fonts);
10882 }
10883
10884 #endif /* GLYPH_DEBUG != 0 */
10885
10886 /* Set *W to the minimum width, *H to the minimum font height of FONT.
10887 Note: There are (broken) X fonts out there with invalid XFontStruct
10888 min_bounds contents. For example, handa@etl.go.jp reports that
10889 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
10890 have font->min_bounds.width == 0. */
10891
10892 static INLINE void
10893 x_font_min_bounds (font, w, h)
10894 MacFontStruct *font;
10895 int *w, *h;
10896 {
10897 /*
10898 * TODO: Windows does not appear to offer min bound, only
10899 * average and maximum width, and maximum height.
10900 */
10901 *h = FONT_HEIGHT (font);
10902 *w = FONT_WIDTH (font);
10903 }
10904
10905
10906 /* Compute the smallest character width and smallest font height over
10907 all fonts available on frame F. Set the members smallest_char_width
10908 and smallest_font_height in F's x_display_info structure to
10909 the values computed. Value is non-zero if smallest_font_height or
10910 smallest_char_width become smaller than they were before. */
10911
10912 int
10913 x_compute_min_glyph_bounds (f)
10914 struct frame *f;
10915 {
10916 int i;
10917 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
10918 MacFontStruct *font;
10919 int old_width = dpyinfo->smallest_char_width;
10920 int old_height = dpyinfo->smallest_font_height;
10921
10922 dpyinfo->smallest_font_height = 100000;
10923 dpyinfo->smallest_char_width = 100000;
10924
10925 for (i = 0; i < dpyinfo->n_fonts; ++i)
10926 if (dpyinfo->font_table[i].name)
10927 {
10928 struct font_info *fontp = dpyinfo->font_table + i;
10929 int w, h;
10930
10931 font = (MacFontStruct *) fontp->font;
10932 xassert (font != (MacFontStruct *) ~0);
10933 x_font_min_bounds (font, &w, &h);
10934
10935 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
10936 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
10937 }
10938
10939 xassert (dpyinfo->smallest_char_width > 0
10940 && dpyinfo->smallest_font_height > 0);
10941
10942 return (dpyinfo->n_fonts == 1
10943 || dpyinfo->smallest_char_width < old_width
10944 || dpyinfo->smallest_font_height < old_height);
10945 }
10946
10947
10948 /* Determine whether given string is a fully-specified XLFD: all 14
10949 fields are present, none is '*'. */
10950
10951 static int
10952 is_fully_specified_xlfd (char *p)
10953 {
10954 int i;
10955 char *q;
10956
10957 if (*p != '-')
10958 return 0;
10959
10960 for (i = 0; i < 13; i++)
10961 {
10962 q = strchr (p + 1, '-');
10963 if (q == NULL)
10964 return 0;
10965 if (q - p == 2 && *(p + 1) == '*')
10966 return 0;
10967 p = q;
10968 }
10969
10970 if (strchr (p + 1, '-') != NULL)
10971 return 0;
10972
10973 if (*(p + 1) == '*' && *(p + 2) == '\0')
10974 return 0;
10975
10976 return 1;
10977 }
10978
10979
10980 const int kDefaultFontSize = 9;
10981
10982
10983 /* XLoadQueryFont creates and returns an internal representation for a
10984 font in a MacFontStruct struct. There is really no concept
10985 corresponding to "loading" a font on the Mac. But we check its
10986 existence and find the font number and all other information for it
10987 and store them in the returned MacFontStruct. */
10988
10989 static MacFontStruct *
10990 XLoadQueryFont (Display *dpy, char *fontname)
10991 {
10992 int i, size, is_two_byte_font, char_width;
10993 char *name;
10994 GrafPtr port;
10995 SInt16 old_fontnum, old_fontsize;
10996 Style old_fontface;
10997 Str32 mfontname;
10998 SInt16 fontnum;
10999 Style fontface = normal;
11000 MacFontStruct *font;
11001 FontInfo the_fontinfo;
11002 char s_weight[7], c_slant;
11003
11004 if (is_fully_specified_xlfd (fontname))
11005 name = fontname;
11006 else
11007 {
11008 for (i = 0; i < font_name_count; i++)
11009 if (mac_font_pattern_match (font_name_table[i], fontname))
11010 break;
11011
11012 if (i >= font_name_count)
11013 return NULL;
11014
11015 name = font_name_table[i];
11016 }
11017
11018 GetPort (&port); /* save the current font number used */
11019 #if TARGET_API_MAC_CARBON
11020 old_fontnum = GetPortTextFont (port);
11021 old_fontsize = GetPortTextSize (port);
11022 old_fontface = GetPortTextFace (port);
11023 #else
11024 old_fontnum = port->txFont;
11025 old_fontsize = port->txSize;
11026 old_fontface = port->txFace;
11027 #endif
11028
11029 if (sscanf (name, "-%*[^-]-%*[^-]-%*[^-]-%*c-%*[^-]--%d-%*[^-]-%*[^-]-%*[^-]-%*c-%*[^-]-%*s", &size) != 1)
11030 size = kDefaultFontSize;
11031
11032 if (sscanf (name, "-%*[^-]-%*[^-]-%6[^-]-%*c-%*[^-]--%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*c-%*[^-]-%*s", s_weight) == 1)
11033 if (strcmp (s_weight, "bold") == 0)
11034 fontface |= bold;
11035
11036 if (sscanf (name, "-%*[^-]-%*[^-]-%*[^-]-%c-%*[^-]--%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*c-%*[^-]-%*s", &c_slant) == 1)
11037 if (c_slant == 'i')
11038 fontface |= italic;
11039
11040 x_font_name_to_mac_font_name (name, mfontname);
11041 c2pstr (mfontname);
11042 GetFNum (mfontname, &fontnum);
11043 if (fontnum == 0)
11044 return NULL;
11045
11046 font = (MacFontStruct *) xmalloc (sizeof (struct MacFontStruct));
11047
11048 font->fontname = (char *) xmalloc (strlen (name) + 1);
11049 bcopy (name, font->fontname, strlen (name) + 1);
11050
11051 font->mac_fontnum = fontnum;
11052 font->mac_fontsize = size;
11053 font->mac_fontface = fontface;
11054 font->mac_scriptcode = FontToScript (fontnum);
11055
11056 /* Apple Japanese (SJIS) font is listed as both
11057 "*-jisx0208.1983-sjis" (Japanese script) and "*-jisx0201.1976-0"
11058 (Roman script) in init_font_name_table (). The latter should be
11059 treated as a one-byte font. */
11060 {
11061 char cs[32];
11062
11063 if (sscanf (name,
11064 "-%*[^-]-%*[^-]-%*[^-]-%*c-%*[^-]--%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*c-%*[^-]-%31s",
11065 cs) == 1
11066 && 0 == strcmp (cs, "jisx0201.1976-0"))
11067 font->mac_scriptcode = smRoman;
11068 }
11069
11070 is_two_byte_font = font->mac_scriptcode == smJapanese ||
11071 font->mac_scriptcode == smTradChinese ||
11072 font->mac_scriptcode == smSimpChinese ||
11073 font->mac_scriptcode == smKorean;
11074
11075 TextFont (fontnum);
11076 TextSize (size);
11077 TextFace (fontface);
11078
11079 GetFontInfo (&the_fontinfo);
11080
11081 font->ascent = the_fontinfo.ascent;
11082 font->descent = the_fontinfo.descent;
11083
11084 font->min_byte1 = 0;
11085 if (is_two_byte_font)
11086 font->max_byte1 = 1;
11087 else
11088 font->max_byte1 = 0;
11089 font->min_char_or_byte2 = 0x20;
11090 font->max_char_or_byte2 = 0xff;
11091
11092 if (is_two_byte_font)
11093 {
11094 /* Use the width of an "ideographic space" of that font because
11095 the_fontinfo.widMax returns the wrong width for some fonts. */
11096 switch (font->mac_scriptcode)
11097 {
11098 case smJapanese:
11099 char_width = StringWidth("\p\x81\x40");
11100 break;
11101 case smTradChinese:
11102 char_width = StringWidth("\p\xa1\x40");
11103 break;
11104 case smSimpChinese:
11105 char_width = StringWidth("\p\xa1\xa1");
11106 break;
11107 case smKorean:
11108 char_width = StringWidth("\p\xa1\xa1");
11109 break;
11110 }
11111 }
11112 else
11113 /* Do this instead of use the_fontinfo.widMax, which incorrectly
11114 returns 15 for 12-point Monaco! */
11115 char_width = CharWidth ('m');
11116
11117 font->max_bounds.rbearing = char_width;
11118 font->max_bounds.lbearing = 0;
11119 font->max_bounds.width = char_width;
11120 font->max_bounds.ascent = the_fontinfo.ascent;
11121 font->max_bounds.descent = the_fontinfo.descent;
11122
11123 font->min_bounds = font->max_bounds;
11124
11125 if (is_two_byte_font || CharWidth ('m') == CharWidth ('i'))
11126 font->per_char = NULL;
11127 else
11128 {
11129 font->per_char = (XCharStruct *)
11130 xmalloc (sizeof (XCharStruct) * (0xff - 0x20 + 1));
11131 {
11132 int c;
11133
11134 for (c = 0x20; c <= 0xff; c++)
11135 {
11136 font->per_char[c - 0x20] = font->max_bounds;
11137 font->per_char[c - 0x20].width = CharWidth (c);
11138 }
11139 }
11140 }
11141
11142 TextFont (old_fontnum); /* restore previous font number, size and face */
11143 TextSize (old_fontsize);
11144 TextFace (old_fontface);
11145
11146 return font;
11147 }
11148
11149
11150 /* Load font named FONTNAME of the size SIZE for frame F, and return a
11151 pointer to the structure font_info while allocating it dynamically.
11152 If SIZE is 0, load any size of font.
11153 If loading is failed, return NULL. */
11154
11155 struct font_info *
11156 x_load_font (f, fontname, size)
11157 struct frame *f;
11158 register char *fontname;
11159 int size;
11160 {
11161 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
11162 Lisp_Object font_names;
11163
11164 /* Get a list of all the fonts that match this name. Once we
11165 have a list of matching fonts, we compare them against the fonts
11166 we already have by comparing names. */
11167 font_names = x_list_fonts (f, build_string (fontname), size, 1);
11168
11169 if (!NILP (font_names))
11170 {
11171 Lisp_Object tail;
11172 int i;
11173
11174 for (i = 0; i < dpyinfo->n_fonts; i++)
11175 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
11176 if (dpyinfo->font_table[i].name
11177 && (!strcmp (dpyinfo->font_table[i].name,
11178 SDATA (XCAR (tail)))
11179 || !strcmp (dpyinfo->font_table[i].full_name,
11180 SDATA (XCAR (tail)))))
11181 return (dpyinfo->font_table + i);
11182 }
11183
11184 /* Load the font and add it to the table. */
11185 {
11186 char *full_name;
11187 struct MacFontStruct *font;
11188 struct font_info *fontp;
11189 unsigned long value;
11190 int i;
11191
11192 /* If we have found fonts by x_list_font, load one of them. If
11193 not, we still try to load a font by the name given as FONTNAME
11194 because XListFonts (called in x_list_font) of some X server has
11195 a bug of not finding a font even if the font surely exists and
11196 is loadable by XLoadQueryFont. */
11197 if (size > 0 && !NILP (font_names))
11198 fontname = (char *) SDATA (XCAR (font_names));
11199
11200 font = (MacFontStruct *) XLoadQueryFont (FRAME_MAC_DISPLAY (f), fontname);
11201 if (!font)
11202 return NULL;
11203
11204 /* Find a free slot in the font table. */
11205 for (i = 0; i < dpyinfo->n_fonts; ++i)
11206 if (dpyinfo->font_table[i].name == NULL)
11207 break;
11208
11209 /* If no free slot found, maybe enlarge the font table. */
11210 if (i == dpyinfo->n_fonts
11211 && dpyinfo->n_fonts == dpyinfo->font_table_size)
11212 {
11213 int sz;
11214 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
11215 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
11216 dpyinfo->font_table
11217 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
11218 }
11219
11220 fontp = dpyinfo->font_table + i;
11221 if (i == dpyinfo->n_fonts)
11222 ++dpyinfo->n_fonts;
11223
11224 /* Now fill in the slots of *FONTP. */
11225 BLOCK_INPUT;
11226 fontp->font = font;
11227 fontp->font_idx = i;
11228 fontp->name = (char *) xmalloc (strlen (font->fontname) + 1);
11229 bcopy (font->fontname, fontp->name, strlen (font->fontname) + 1);
11230
11231 fontp->full_name = fontp->name;
11232
11233 fontp->size = font->max_bounds.width;
11234 fontp->height = FONT_HEIGHT (font);
11235 {
11236 /* For some font, ascent and descent in max_bounds field is
11237 larger than the above value. */
11238 int max_height = font->max_bounds.ascent + font->max_bounds.descent;
11239 if (max_height > fontp->height)
11240 fontp->height = max_height;
11241 }
11242
11243 /* The slot `encoding' specifies how to map a character
11244 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
11245 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
11246 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
11247 2:0xA020..0xFF7F). For the moment, we don't know which charset
11248 uses this font. So, we set information in fontp->encoding[1]
11249 which is never used by any charset. If mapping can't be
11250 decided, set FONT_ENCODING_NOT_DECIDED. */
11251 if (font->mac_scriptcode == smJapanese)
11252 fontp->encoding[1] = 4;
11253 else
11254 {
11255 fontp->encoding[1]
11256 = (font->max_byte1 == 0
11257 /* 1-byte font */
11258 ? (font->min_char_or_byte2 < 0x80
11259 ? (font->max_char_or_byte2 < 0x80
11260 ? 0 /* 0x20..0x7F */
11261 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
11262 : 1) /* 0xA0..0xFF */
11263 /* 2-byte font */
11264 : (font->min_byte1 < 0x80
11265 ? (font->max_byte1 < 0x80
11266 ? (font->min_char_or_byte2 < 0x80
11267 ? (font->max_char_or_byte2 < 0x80
11268 ? 0 /* 0x2020..0x7F7F */
11269 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
11270 : 3) /* 0x20A0..0x7FFF */
11271 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
11272 : (font->min_char_or_byte2 < 0x80
11273 ? (font->max_char_or_byte2 < 0x80
11274 ? 2 /* 0xA020..0xFF7F */
11275 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
11276 : 1))); /* 0xA0A0..0xFFFF */
11277 }
11278
11279 #if 0 /* MAC_TODO: fill these out with more reasonably values */
11280 fontp->baseline_offset
11281 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
11282 ? (long) value : 0);
11283 fontp->relative_compose
11284 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
11285 ? (long) value : 0);
11286 fontp->default_ascent
11287 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
11288 ? (long) value : 0);
11289 #else
11290 fontp->baseline_offset = 0;
11291 fontp->relative_compose = 0;
11292 fontp->default_ascent = 0;
11293 #endif
11294
11295 /* Set global flag fonts_changed_p to non-zero if the font loaded
11296 has a character with a smaller width than any other character
11297 before, or if the font loaded has a smalle>r height than any
11298 other font loaded before. If this happens, it will make a
11299 glyph matrix reallocation necessary. */
11300 fonts_changed_p = x_compute_min_glyph_bounds (f);
11301 UNBLOCK_INPUT;
11302 return fontp;
11303 }
11304 }
11305
11306
11307 /* Return a pointer to struct font_info of a font named FONTNAME for
11308 frame F. If no such font is loaded, return NULL. */
11309
11310 struct font_info *
11311 x_query_font (f, fontname)
11312 struct frame *f;
11313 register char *fontname;
11314 {
11315 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
11316 int i;
11317
11318 for (i = 0; i < dpyinfo->n_fonts; i++)
11319 if (dpyinfo->font_table[i].name
11320 && (!strcmp (dpyinfo->font_table[i].name, fontname)
11321 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
11322 return (dpyinfo->font_table + i);
11323 return NULL;
11324 }
11325
11326
11327 /* Find a CCL program for a font specified by FONTP, and set the member
11328 `encoder' of the structure. */
11329
11330 void
11331 x_find_ccl_program (fontp)
11332 struct font_info *fontp;
11333 {
11334 Lisp_Object list, elt;
11335
11336 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
11337 {
11338 elt = XCAR (list);
11339 if (CONSP (elt)
11340 && STRINGP (XCAR (elt))
11341 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
11342 >= 0))
11343 break;
11344 }
11345 if (! NILP (list))
11346 {
11347 struct ccl_program *ccl
11348 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
11349
11350 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
11351 xfree (ccl);
11352 else
11353 fontp->font_encoder = ccl;
11354 }
11355 }
11356
11357
11358 \f
11359 /***********************************************************************
11360 Initialization
11361 ***********************************************************************/
11362
11363 #ifdef USE_X_TOOLKIT
11364 static XrmOptionDescRec emacs_options[] = {
11365 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
11366 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
11367
11368 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
11369 XrmoptionSepArg, NULL},
11370 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
11371
11372 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
11373 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
11374 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
11375 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
11376 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
11377 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
11378 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
11379 };
11380 #endif /* USE_X_TOOLKIT */
11381
11382 static int x_initialized;
11383
11384 #ifdef MULTI_KBOARD
11385 /* Test whether two display-name strings agree up to the dot that separates
11386 the screen number from the server number. */
11387 static int
11388 same_x_server (name1, name2)
11389 char *name1, *name2;
11390 {
11391 int seen_colon = 0;
11392 unsigned char *system_name = SDATA (Vsystem_name);
11393 int system_name_length = strlen (system_name);
11394 int length_until_period = 0;
11395
11396 while (system_name[length_until_period] != 0
11397 && system_name[length_until_period] != '.')
11398 length_until_period++;
11399
11400 /* Treat `unix' like an empty host name. */
11401 if (! strncmp (name1, "unix:", 5))
11402 name1 += 4;
11403 if (! strncmp (name2, "unix:", 5))
11404 name2 += 4;
11405 /* Treat this host's name like an empty host name. */
11406 if (! strncmp (name1, system_name, system_name_length)
11407 && name1[system_name_length] == ':')
11408 name1 += system_name_length;
11409 if (! strncmp (name2, system_name, system_name_length)
11410 && name2[system_name_length] == ':')
11411 name2 += system_name_length;
11412 /* Treat this host's domainless name like an empty host name. */
11413 if (! strncmp (name1, system_name, length_until_period)
11414 && name1[length_until_period] == ':')
11415 name1 += length_until_period;
11416 if (! strncmp (name2, system_name, length_until_period)
11417 && name2[length_until_period] == ':')
11418 name2 += length_until_period;
11419
11420 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
11421 {
11422 if (*name1 == ':')
11423 seen_colon++;
11424 if (seen_colon && *name1 == '.')
11425 return 1;
11426 }
11427 return (seen_colon
11428 && (*name1 == '.' || *name1 == '\0')
11429 && (*name2 == '.' || *name2 == '\0'));
11430 }
11431 #endif
11432
11433
11434 /* The Mac Event loop code */
11435
11436 #ifndef MAC_OSX
11437 #include <Events.h>
11438 #include <Quickdraw.h>
11439 #include <Balloons.h>
11440 #include <Devices.h>
11441 #include <Fonts.h>
11442 #include <Gestalt.h>
11443 #include <Menus.h>
11444 #include <Processes.h>
11445 #include <Sound.h>
11446 #include <ToolUtils.h>
11447 #include <TextUtils.h>
11448 #include <Dialogs.h>
11449 #include <Script.h>
11450 #include <Types.h>
11451 #include <TextEncodingConverter.h>
11452 #include <Resources.h>
11453
11454 #if __MWERKS__
11455 #include <unix.h>
11456 #endif
11457 #endif /* ! MAC_OSX */
11458
11459 #define M_APPLE 128
11460 #define I_ABOUT 1
11461
11462 #define WINDOW_RESOURCE 128
11463 #define TERM_WINDOW_RESOURCE 129
11464
11465 #define DEFAULT_NUM_COLS 80
11466
11467 #define MIN_DOC_SIZE 64
11468 #define MAX_DOC_SIZE 32767
11469
11470 /* sleep time for WaitNextEvent */
11471 #define WNE_SLEEP_AT_SUSPEND 10
11472 #define WNE_SLEEP_AT_RESUME 1
11473
11474 /* true when cannot handle any Mac OS events */
11475 static int handling_window_update = 0;
11476
11477 /* the flag appl_is_suspended is used both for determining the sleep
11478 time to be passed to WaitNextEvent and whether the cursor should be
11479 drawn when updating the display. The cursor is turned off when
11480 Emacs is suspended. Redrawing it is unnecessary and what needs to
11481 be done depends on whether the cursor lies inside or outside the
11482 redraw region. So we might as well skip drawing it when Emacs is
11483 suspended. */
11484 static Boolean app_is_suspended = false;
11485 static long app_sleep_time = WNE_SLEEP_AT_RESUME;
11486
11487 #define EXTRA_STACK_ALLOC (256 * 1024)
11488
11489 #define ARGV_STRING_LIST_ID 129
11490 #define ABOUT_ALERT_ID 128
11491 #define RAM_TOO_LARGE_ALERT_ID 129
11492
11493 Boolean terminate_flag = false;
11494
11495 /* True if using command key as meta key. */
11496 Lisp_Object Vmac_command_key_is_meta;
11497
11498 /* True if the ctrl and meta keys should be reversed. */
11499 Lisp_Object Vmac_reverse_ctrl_meta;
11500
11501 #if USE_CARBON_EVENTS
11502 /* True if the mouse wheel button (i.e. button 4) should map to
11503 mouse-2, instead of mouse-3. */
11504 Lisp_Object Vmac_wheel_button_is_mouse_2;
11505
11506 /* If Non-nil, the Mac "Command" key is passed on to the Mac Toolbox
11507 for processing before Emacs sees it. */
11508 Lisp_Object Vmac_pass_command_to_system;
11509
11510 /* If Non-nil, the Mac "Control" key is passed on to the Mac Toolbox
11511 for processing before Emacs sees it. */
11512 Lisp_Object Vmac_pass_control_to_system;
11513 #endif
11514
11515 /* convert input from Mac keyboard (assumed to be in Mac Roman coding)
11516 to this text encoding */
11517 int mac_keyboard_text_encoding;
11518 int current_mac_keyboard_text_encoding = kTextEncodingMacRoman;
11519
11520 /* Set in term/mac-win.el to indicate that event loop can now generate
11521 drag and drop events. */
11522 Lisp_Object Qmac_ready_for_drag_n_drop;
11523
11524 Lisp_Object drag_and_drop_file_list;
11525
11526 Point saved_menu_event_location;
11527
11528 /* Apple Events */
11529 static void init_required_apple_events (void);
11530 static pascal OSErr
11531 do_ae_open_application (const AppleEvent *, AppleEvent *, long);
11532 static pascal OSErr
11533 do_ae_print_documents (const AppleEvent *, AppleEvent *, long);
11534 static pascal OSErr do_ae_open_documents (AppleEvent *, AppleEvent *, long);
11535 static pascal OSErr do_ae_quit_application (AppleEvent *, AppleEvent *, long);
11536
11537 /* Drag and Drop */
11538 static OSErr init_mac_drag_n_drop ();
11539 static pascal OSErr mac_do_receive_drag (WindowPtr, void*, DragReference);
11540
11541 #if USE_CARBON_EVENTS
11542 /* Preliminary Support for the OSX Services Menu */
11543 static OSStatus mac_handle_service_event (EventHandlerCallRef,EventRef,void*);
11544 static void init_service_handler ();
11545 #endif
11546
11547 extern void init_emacs_passwd_dir ();
11548 extern int emacs_main (int, char **, char **);
11549 extern void check_alarm ();
11550
11551 extern void initialize_applescript();
11552 extern void terminate_applescript();
11553
11554 static unsigned int
11555 #if USE_CARBON_EVENTS
11556 mac_to_emacs_modifiers (UInt32 mods)
11557 #else
11558 mac_to_emacs_modifiers (EventModifiers mods)
11559 #endif
11560 {
11561 unsigned int result = 0;
11562 if (mods & macShiftKey)
11563 result |= shift_modifier;
11564 if (mods & macCtrlKey)
11565 result |= ctrl_modifier;
11566 if (mods & macMetaKey)
11567 result |= meta_modifier;
11568 if (NILP (Vmac_command_key_is_meta) && (mods & macAltKey))
11569 result |= alt_modifier;
11570 return result;
11571 }
11572
11573 #if USE_CARBON_EVENTS
11574 /* Obtains the event modifiers from the event ref and then calls
11575 mac_to_emacs_modifiers. */
11576 static int
11577 mac_event_to_emacs_modifiers (EventRef eventRef)
11578 {
11579 UInt32 mods = 0;
11580 GetEventParameter (eventRef, kEventParamKeyModifiers, typeUInt32, NULL,
11581 sizeof (UInt32), NULL, &mods);
11582 return mac_to_emacs_modifiers (mods);
11583 }
11584
11585 /* Given an event ref, return the code to use for the mouse button
11586 code in the emacs input_event. */
11587 static int
11588 mac_get_mouse_btn (EventRef ref)
11589 {
11590 EventMouseButton result = kEventMouseButtonPrimary;
11591 GetEventParameter (ref, kEventParamMouseButton, typeMouseButton, NULL,
11592 sizeof (EventMouseButton), NULL, &result);
11593 switch (result)
11594 {
11595 case kEventMouseButtonPrimary:
11596 return 0;
11597 case kEventMouseButtonSecondary:
11598 return NILP (Vmac_wheel_button_is_mouse_2) ? 2 : 1;
11599 case kEventMouseButtonTertiary:
11600 case 4: /* 4 is the number for the mouse wheel button */
11601 return NILP (Vmac_wheel_button_is_mouse_2) ? 1 : 2;
11602 default:
11603 return 0;
11604 }
11605 }
11606
11607 /* Normally, ConvertEventRefToEventRecord will correctly handle all
11608 events. However the click of the mouse wheel is not converted to a
11609 mouseDown or mouseUp event. This calls ConvertEventRef, but then
11610 checks to see if it is a mouse up or down carbon event that has not
11611 been converted, and if so, converts it by hand (to be picked up in
11612 the XTread_socket loop). */
11613 static Boolean mac_convert_event_ref (EventRef eventRef, EventRecord *eventRec)
11614 {
11615 Boolean result = ConvertEventRefToEventRecord (eventRef, eventRec);
11616 /* Do special case for mouse wheel button. */
11617 if (!result && GetEventClass (eventRef) == kEventClassMouse)
11618 {
11619 UInt32 kind = GetEventKind (eventRef);
11620 if (kind == kEventMouseDown && !(eventRec->what == mouseDown))
11621 {
11622 eventRec->what = mouseDown;
11623 result=1;
11624 }
11625 if (kind == kEventMouseUp && !(eventRec->what == mouseUp))
11626 {
11627 eventRec->what = mouseUp;
11628 result=1;
11629 }
11630 if (result)
11631 {
11632 /* Need where and when. */
11633 UInt32 mods;
11634 GetEventParameter (eventRef, kEventParamMouseLocation,
11635 typeQDPoint, NULL, sizeof (Point),
11636 NULL, &eventRec->where);
11637 /* Use two step process because new event modifiers are
11638 32-bit and old are 16-bit. Currently, only loss is
11639 NumLock & Fn. */
11640 GetEventParameter (eventRef, kEventParamKeyModifiers,
11641 typeUInt32, NULL, sizeof (UInt32),
11642 NULL, &mods);
11643 eventRec->modifiers = mods;
11644
11645 eventRec->when = EventTimeToTicks (GetEventTime (eventRef));
11646 }
11647 }
11648 return result;
11649 }
11650
11651 #endif
11652
11653 static void
11654 do_get_menus (void)
11655 {
11656 Handle menubar_handle;
11657 MenuHandle menu_handle;
11658
11659 menubar_handle = GetNewMBar (128);
11660 if(menubar_handle == NULL)
11661 abort ();
11662 SetMenuBar (menubar_handle);
11663 DrawMenuBar ();
11664
11665 menu_handle = GetMenuHandle (M_APPLE);
11666 if(menu_handle != NULL)
11667 AppendResMenu (menu_handle,'DRVR');
11668 else
11669 abort ();
11670 }
11671
11672
11673 static void
11674 do_init_managers (void)
11675 {
11676 #if !TARGET_API_MAC_CARBON
11677 InitGraf (&qd.thePort);
11678 InitFonts ();
11679 FlushEvents (everyEvent, 0);
11680 InitWindows ();
11681 InitMenus ();
11682 TEInit ();
11683 InitDialogs (NULL);
11684 #endif /* !TARGET_API_MAC_CARBON */
11685 InitCursor ();
11686
11687 #if !TARGET_API_MAC_CARBON
11688 /* set up some extra stack space for use by emacs */
11689 SetApplLimit ((Ptr) ((long) GetApplLimit () - EXTRA_STACK_ALLOC));
11690
11691 /* MaxApplZone must be called for AppleScript to execute more
11692 complicated scripts */
11693 MaxApplZone ();
11694 MoreMasters ();
11695 #endif /* !TARGET_API_MAC_CARBON */
11696 }
11697
11698 static void
11699 do_check_ram_size (void)
11700 {
11701 SInt32 physical_ram_size, logical_ram_size;
11702
11703 if (Gestalt (gestaltPhysicalRAMSize, &physical_ram_size) != noErr
11704 || Gestalt (gestaltLogicalRAMSize, &logical_ram_size) != noErr
11705 || physical_ram_size > 256 * 1024 * 1024
11706 || logical_ram_size > 256 * 1024 * 1024)
11707 {
11708 StopAlert (RAM_TOO_LARGE_ALERT_ID, NULL);
11709 exit (1);
11710 }
11711 }
11712
11713 static void
11714 do_window_update (WindowPtr win)
11715 {
11716 struct mac_output *mwp = (mac_output *) GetWRefCon (win);
11717 struct frame *f = mwp->mFP;
11718
11719 if (f)
11720 {
11721 if (f->async_visible == 0)
11722 {
11723 f->async_visible = 1;
11724 f->async_iconified = 0;
11725 SET_FRAME_GARBAGED (f);
11726
11727 /* An update event is equivalent to MapNotify on X, so report
11728 visibility changes properly. */
11729 if (! NILP(Vframe_list) && ! NILP (XCDR (Vframe_list)))
11730 /* Force a redisplay sooner or later to update the
11731 frame titles in case this is the second frame. */
11732 record_asynch_buffer_change ();
11733 }
11734 else
11735 {
11736 BeginUpdate (win);
11737 handling_window_update = 1;
11738
11739 XClearWindow (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f));
11740
11741 expose_frame (f, 0, 0, 0, 0);
11742
11743 handling_window_update = 0;
11744 EndUpdate (win);
11745 }
11746 }
11747 }
11748
11749 static int
11750 is_emacs_window (WindowPtr win)
11751 {
11752 Lisp_Object tail, frame;
11753
11754 if (!win)
11755 return 0;
11756
11757 FOR_EACH_FRAME (tail, frame)
11758 if (FRAME_MAC_P (XFRAME (frame)))
11759 if (FRAME_MAC_WINDOW (XFRAME (frame)) == win)
11760 return 1;
11761
11762 return 0;
11763 }
11764
11765 static void
11766 do_window_activate (WindowPtr win)
11767 {
11768 mac_output *mwp;
11769 struct frame *f;
11770
11771 if (is_emacs_window (win))
11772 {
11773 mwp = (mac_output *) GetWRefCon (win);
11774 f = mwp->mFP;
11775
11776 if (f)
11777 {
11778 x_new_focus_frame (FRAME_MAC_DISPLAY_INFO (f), f);
11779 activate_scroll_bars (f);
11780 }
11781 }
11782 }
11783
11784 static void
11785 do_window_deactivate (WindowPtr win)
11786 {
11787 mac_output *mwp;
11788 struct frame *f;
11789
11790 if (is_emacs_window (win))
11791 {
11792 mwp = (mac_output *) GetWRefCon (win);
11793 f = mwp->mFP;
11794
11795 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
11796 {
11797 x_new_focus_frame (FRAME_MAC_DISPLAY_INFO (f), 0);
11798 deactivate_scroll_bars (f);
11799 }
11800 }
11801 }
11802
11803 static void
11804 do_app_resume ()
11805 {
11806 WindowPtr wp;
11807 mac_output *mwp;
11808 struct frame *f;
11809
11810 wp = FrontWindow();
11811 if (is_emacs_window (wp))
11812 {
11813 mwp = (mac_output *) GetWRefCon (wp);
11814 f = mwp->mFP;
11815
11816 if (f)
11817 {
11818 x_new_focus_frame (FRAME_MAC_DISPLAY_INFO (f), f);
11819 activate_scroll_bars (f);
11820 }
11821 }
11822
11823 app_is_suspended = false;
11824 app_sleep_time = WNE_SLEEP_AT_RESUME;
11825 }
11826
11827 static void
11828 do_app_suspend ()
11829 {
11830 WindowPtr wp;
11831 mac_output *mwp;
11832 struct frame *f;
11833
11834 wp = FrontWindow();
11835 if (is_emacs_window (wp))
11836 {
11837 mwp = (mac_output *) GetWRefCon (wp);
11838 f = mwp->mFP;
11839
11840 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
11841 {
11842 x_new_focus_frame (FRAME_MAC_DISPLAY_INFO (f), 0);
11843 deactivate_scroll_bars (f);
11844 }
11845 }
11846
11847 app_is_suspended = true;
11848 app_sleep_time = WNE_SLEEP_AT_SUSPEND;
11849 }
11850
11851
11852 static void
11853 do_mouse_moved (Point mouse_pos)
11854 {
11855 WindowPtr wp = FrontWindow ();
11856 struct frame *f;
11857
11858 if (is_emacs_window (wp))
11859 {
11860 f = ((mac_output *) GetWRefCon (wp))->mFP;
11861
11862 #if TARGET_API_MAC_CARBON
11863 SetPort (GetWindowPort (wp));
11864 #else
11865 SetPort (wp);
11866 #endif
11867
11868 GlobalToLocal (&mouse_pos);
11869
11870 note_mouse_movement (f, &mouse_pos);
11871 }
11872 }
11873
11874
11875 static void
11876 do_os_event (EventRecord *erp)
11877 {
11878 switch((erp->message >> 24) & 0x000000FF)
11879 {
11880 case suspendResumeMessage:
11881 if((erp->message & resumeFlag) == 1)
11882 do_app_resume ();
11883 else
11884 do_app_suspend ();
11885 break;
11886
11887 case mouseMovedMessage:
11888 do_mouse_moved (erp->where);
11889 break;
11890 }
11891 }
11892
11893 static void
11894 do_events (EventRecord *erp)
11895 {
11896 switch (erp->what)
11897 {
11898 case updateEvt:
11899 do_window_update ((WindowPtr) erp->message);
11900 break;
11901
11902 case osEvt:
11903 do_os_event (erp);
11904 break;
11905
11906 case activateEvt:
11907 if ((erp->modifiers & activeFlag) != 0)
11908 do_window_activate ((WindowPtr) erp->message);
11909 else
11910 do_window_deactivate ((WindowPtr) erp->message);
11911 break;
11912 }
11913 }
11914
11915 static void
11916 do_apple_menu (SInt16 menu_item)
11917 {
11918 #if !TARGET_API_MAC_CARBON
11919 Str255 item_name;
11920 SInt16 da_driver_refnum;
11921
11922 if (menu_item == I_ABOUT)
11923 NoteAlert (ABOUT_ALERT_ID, NULL);
11924 else
11925 {
11926 GetMenuItemText (GetMenuHandle (M_APPLE), menu_item, item_name);
11927 da_driver_refnum = OpenDeskAcc (item_name);
11928 }
11929 #endif /* !TARGET_API_MAC_CARBON */
11930 }
11931
11932 void
11933 do_menu_choice (SInt32 menu_choice)
11934 {
11935 SInt16 menu_id, menu_item;
11936
11937 menu_id = HiWord (menu_choice);
11938 menu_item = LoWord (menu_choice);
11939
11940 if (menu_id == 0)
11941 return;
11942
11943 switch (menu_id)
11944 {
11945 case M_APPLE:
11946 do_apple_menu (menu_item);
11947 break;
11948
11949 default:
11950 {
11951 WindowPtr wp = FrontWindow ();
11952 struct frame *f = ((mac_output *) GetWRefCon (wp))->mFP;
11953 MenuHandle menu = GetMenuHandle (menu_id);
11954 if (menu)
11955 {
11956 UInt32 refcon;
11957
11958 GetMenuItemRefCon (menu, menu_item, &refcon);
11959 menubar_selection_callback (f, refcon);
11960 }
11961 }
11962 }
11963
11964 HiliteMenu (0);
11965 }
11966
11967
11968 /* Handle drags in size box. Based on code contributed by Ben
11969 Mesander and IM - Window Manager A. */
11970
11971 static void
11972 do_grow_window (WindowPtr w, EventRecord *e)
11973 {
11974 long grow_size;
11975 Rect limit_rect;
11976 int rows, columns;
11977 mac_output *mwp = (mac_output *) GetWRefCon (w);
11978 struct frame *f = mwp->mFP;
11979
11980 SetRect(&limit_rect, MIN_DOC_SIZE, MIN_DOC_SIZE, MAX_DOC_SIZE, MAX_DOC_SIZE);
11981
11982 grow_size = GrowWindow (w, e->where, &limit_rect);
11983
11984 /* see if it really changed size */
11985 if (grow_size != 0)
11986 {
11987 rows = PIXEL_TO_CHAR_HEIGHT (f, HiWord (grow_size));
11988 columns = PIXEL_TO_CHAR_WIDTH (f, LoWord (grow_size));
11989
11990 x_set_window_size (f, 0, columns, rows);
11991 }
11992 }
11993
11994
11995 /* Handle clicks in zoom box. Calculation of "standard state" based
11996 on code in IM - Window Manager A and code contributed by Ben
11997 Mesander. The standard state of an Emacs window is 80-characters
11998 wide (DEFAULT_NUM_COLS) and as tall as will fit on the screen. */
11999
12000 static void
12001 do_zoom_window (WindowPtr w, int zoom_in_or_out)
12002 {
12003 GrafPtr save_port;
12004 Rect zoom_rect, port_rect;
12005 Point top_left;
12006 int w_title_height, columns, rows, width, height, dummy, x, y;
12007 mac_output *mwp = (mac_output *) GetWRefCon (w);
12008 struct frame *f = mwp->mFP;
12009
12010 GetPort (&save_port);
12011
12012 #if TARGET_API_MAC_CARBON
12013 SetPort (GetWindowPort (w));
12014 #else
12015 SetPort (w);
12016 #endif
12017
12018 /* Clear window to avoid flicker. */
12019 #if TARGET_API_MAC_CARBON
12020 {
12021 Rect r;
12022 BitMap bm;
12023
12024 GetWindowPortBounds (w, &r);
12025 EraseRect (&r);
12026
12027 if (zoom_in_or_out == inZoomOut)
12028 {
12029 /* calculate height of window's title bar (hard card it for now). */
12030 w_title_height = 20 + GetMBarHeight ();
12031
12032 /* get maximum height of window into zoom_rect.bottom -
12033 zoom_rect.top */
12034 GetQDGlobalsScreenBits (&bm);
12035 zoom_rect = bm.bounds;
12036 zoom_rect.top += w_title_height;
12037 InsetRect (&zoom_rect, 8, 4); /* not too tight */
12038
12039 zoom_rect.right = zoom_rect.left
12040 + CHAR_TO_PIXEL_WIDTH (f, DEFAULT_NUM_COLS);
12041
12042 SetWindowStandardState (w, &zoom_rect);
12043 }
12044 }
12045 #else /* not TARGET_API_MAC_CARBON */
12046 EraseRect (&(w->portRect));
12047 if (zoom_in_or_out == inZoomOut)
12048 {
12049 SetPt (&top_left, w->portRect.left, w->portRect.top);
12050 LocalToGlobal (&top_left);
12051
12052 /* calculate height of window's title bar */
12053 w_title_height = top_left.v - 1
12054 - (**((WindowPeek) w)->strucRgn).rgnBBox.top + GetMBarHeight ();
12055
12056 /* get maximum height of window into zoom_rect.bottom - zoom_rect.top */
12057 zoom_rect = qd.screenBits.bounds;
12058 zoom_rect.top += w_title_height;
12059 InsetRect (&zoom_rect, 8, 4); /* not too tight */
12060
12061 zoom_rect.right = zoom_rect.left
12062 + CHAR_TO_PIXEL_WIDTH (f, DEFAULT_NUM_COLS);
12063
12064 (**((WStateDataHandle) ((WindowPeek) w)->dataHandle)).stdState
12065 = zoom_rect;
12066 }
12067 #endif /* not TARGET_API_MAC_CARBON */
12068
12069 ZoomWindow (w, zoom_in_or_out, w == FrontWindow ());
12070
12071 /* retrieve window size and update application values */
12072 #if TARGET_API_MAC_CARBON
12073 GetWindowPortBounds (w, &port_rect);
12074 #else
12075 port_rect = w->portRect;
12076 #endif
12077 rows = PIXEL_TO_CHAR_HEIGHT (f, port_rect.bottom - port_rect.top);
12078 columns = PIXEL_TO_CHAR_WIDTH (f, port_rect.right - port_rect.left);
12079 x_set_window_size (mwp->mFP, 0, columns, rows);
12080
12081 SetPort (save_port);
12082 }
12083
12084 /* Initialize Drag And Drop to allow files to be dropped onto emacs frames */
12085 static OSErr
12086 init_mac_drag_n_drop ()
12087 {
12088 OSErr result = InstallReceiveHandler (mac_do_receive_drag, 0L, NULL);
12089 return result;
12090 }
12091
12092 /* Intialize AppleEvent dispatcher table for the required events. */
12093 void
12094 init_required_apple_events ()
12095 {
12096 OSErr err;
12097 long result;
12098
12099 /* Make sure we have apple events before starting. */
12100 err = Gestalt (gestaltAppleEventsAttr, &result);
12101 if (err != noErr)
12102 abort ();
12103
12104 if (!(result & (1 << gestaltAppleEventsPresent)))
12105 abort ();
12106
12107 #if TARGET_API_MAC_CARBON
12108 err = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
12109 NewAEEventHandlerUPP
12110 ((AEEventHandlerProcPtr) do_ae_open_application),
12111 0L, false);
12112 #else
12113 err = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
12114 NewAEEventHandlerProc
12115 ((AEEventHandlerProcPtr) do_ae_open_application),
12116 0L, false);
12117 #endif
12118 if (err != noErr)
12119 abort ();
12120
12121 #if TARGET_API_MAC_CARBON
12122 err = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
12123 NewAEEventHandlerUPP
12124 ((AEEventHandlerProcPtr) do_ae_open_documents),
12125 0L, false);
12126 #else
12127 err = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
12128 NewAEEventHandlerProc
12129 ((AEEventHandlerProcPtr) do_ae_open_documents),
12130 0L, false);
12131 #endif
12132 if (err != noErr)
12133 abort ();
12134
12135 #if TARGET_API_MAC_CARBON
12136 err = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
12137 NewAEEventHandlerUPP
12138 ((AEEventHandlerProcPtr) do_ae_print_documents),
12139 0L, false);
12140 #else
12141 err = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
12142 NewAEEventHandlerProc
12143 ((AEEventHandlerProcPtr) do_ae_print_documents),
12144 0L, false);
12145 #endif
12146 if (err != noErr)
12147 abort ();
12148
12149 #if TARGET_API_MAC_CARBON
12150 err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
12151 NewAEEventHandlerUPP
12152 ((AEEventHandlerProcPtr) do_ae_quit_application),
12153 0L, false);
12154 #else
12155 err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
12156 NewAEEventHandlerProc
12157 ((AEEventHandlerProcPtr) do_ae_quit_application),
12158 0L, false);
12159 #endif
12160 if (err != noErr)
12161 abort ();
12162 }
12163
12164 #if USE_CARBON_EVENTS
12165
12166 void init_service_handler()
12167 {
12168 EventTypeSpec specs[] = {{kEventClassService, kEventServiceGetTypes},
12169 {kEventClassService, kEventServiceCopy},
12170 {kEventClassService, kEventServicePaste}};
12171 InstallApplicationEventHandler (NewEventHandlerUPP (mac_handle_service_event),
12172 3, specs, NULL, NULL);
12173 }
12174
12175 /*
12176 MAC_TODO: Check to see if this is called by AEProcessDesc...
12177 */
12178 OSStatus mac_handle_service_event (EventHandlerCallRef callRef,
12179 EventRef event, void *data)
12180 {
12181 OSStatus err = noErr;
12182 switch (GetEventKind (event))
12183 {
12184 case kEventServiceGetTypes:
12185 {
12186 CFMutableArrayRef copyTypes, pasteTypes;
12187 CFStringRef type;
12188 Boolean selection = true;
12189 /*
12190 GetEventParameter(event, kEventParamServicePasteTypes,
12191 typeCFMutableArrayRef, NULL,
12192 sizeof (CFMutableArrayRef), NULL, &pasteTypes);
12193 */
12194 GetEventParameter(event, kEventParamServiceCopyTypes,
12195 typeCFMutableArrayRef, NULL,
12196 sizeof (CFMutableArrayRef), NULL, &copyTypes);
12197 type = CreateTypeStringWithOSType (kScrapFlavorTypeText);
12198 if (type) {
12199 CFArrayAppendValue (copyTypes, type);
12200 //CFArrayAppendValue (pasteTypes, type);
12201 CFRelease (type);
12202 }
12203 }
12204 case kEventServiceCopy:
12205 {
12206 ScrapRef currentScrap, specificScrap;
12207 char * buf = "";
12208 Size byteCount = 0;
12209
12210 GetCurrentScrap (&currentScrap);
12211
12212 err = GetScrapFlavorSize (currentScrap, kScrapFlavorTypeText, &byteCount);
12213 if (err == noErr)
12214 {
12215 void *buffer = xmalloc (byteCount);
12216 if (buffer != NULL)
12217 {
12218 GetEventParameter (event, kEventParamScrapRef, typeScrapRef, NULL,
12219 sizeof (ScrapRef), NULL, &specificScrap);
12220
12221 err = GetScrapFlavorData (currentScrap, kScrapFlavorTypeText,
12222 &byteCount, buffer);
12223 if (err == noErr)
12224 PutScrapFlavor (specificScrap, kScrapFlavorTypeText,
12225 kScrapFlavorMaskNone, byteCount, buffer);
12226 xfree (buffer);
12227 }
12228 }
12229 err = noErr;
12230 }
12231 case kEventServicePaste:
12232 {
12233 /*
12234 // Get the current location
12235 Size byteCount;
12236 ScrapRef specificScrap;
12237 GetEventParameter(event, kEventParamScrapRef, typeScrapRef, NULL,
12238 sizeof(ScrapRef), NULL, &specificScrap);
12239 err = GetScrapFlavorSize(specificScrap, kScrapFlavorTypeText, &byteCount);
12240 if (err == noErr) {
12241 void * buffer = xmalloc(byteCount);
12242 if (buffer != NULL ) {
12243 err = GetScrapFlavorData(specificScrap, kScrapFlavorTypeText,
12244 &byteCount, buffer);
12245 if (err == noErr) {
12246 // Actually place in the buffer
12247 BLOCK_INPUT;
12248 // Get the current "selection" string here
12249 UNBLOCK_INPUT;
12250 }
12251 }
12252 xfree(buffer);
12253 }
12254 */
12255 }
12256 }
12257 return err;
12258 }
12259 #endif
12260
12261 /* Open Application Apple Event */
12262 static pascal OSErr
12263 do_ae_open_application(const AppleEvent *pae, AppleEvent *preply, long prefcon)
12264 {
12265 return noErr;
12266 }
12267
12268
12269 /* Defined in mac.c. */
12270 extern int
12271 path_from_vol_dir_name (char *, int, short, long, char *);
12272
12273
12274 /* Called when we receive an AppleEvent with an ID of
12275 "kAEOpenDocuments". This routine gets the direct parameter,
12276 extracts the FSSpecs in it, and puts their names on a list. */
12277 static pascal OSErr
12278 do_ae_open_documents(AppleEvent *message, AppleEvent *reply, long refcon)
12279 {
12280 OSErr err, err2;
12281 AEDesc the_desc;
12282 AEKeyword keyword;
12283 DescType actual_type;
12284 Size actual_size;
12285
12286 err = AEGetParamDesc (message, keyDirectObject, typeAEList, &the_desc);
12287 if (err != noErr)
12288 goto descriptor_error_exit;
12289
12290 /* Check to see that we got all of the required parameters from the
12291 event descriptor. For an 'odoc' event this should just be the
12292 file list. */
12293 err = AEGetAttributePtr(message, keyMissedKeywordAttr, typeWildCard,
12294 &actual_type, (Ptr) &keyword,
12295 sizeof (keyword), &actual_size);
12296 /* No error means that we found some unused parameters.
12297 errAEDescNotFound means that there are no more parameters. If we
12298 get an error code other than that, flag it. */
12299 if ((err == noErr) || (err != errAEDescNotFound))
12300 {
12301 err = errAEEventNotHandled;
12302 goto error_exit;
12303 }
12304 err = noErr;
12305
12306 /* Got all the parameters we need. Now, go through the direct
12307 object list and parse it up. */
12308 {
12309 long num_files_to_open;
12310
12311 err = AECountItems (&the_desc, &num_files_to_open);
12312 if (err == noErr)
12313 {
12314 int i;
12315
12316 /* AE file list is one based so just use that for indexing here. */
12317 for (i = 1; (err == noErr) && (i <= num_files_to_open); i++)
12318 {
12319 FSSpec fs;
12320 Str255 path_name, unix_path_name;
12321 #ifdef MAC_OSX
12322 FSRef fref;
12323 #endif
12324
12325 err = AEGetNthPtr(&the_desc, i, typeFSS, &keyword, &actual_type,
12326 (Ptr) &fs, sizeof (fs), &actual_size);
12327 if (err != noErr) break;
12328
12329 #ifdef MAC_OSX
12330 err = FSpMakeFSRef (&fs, &fref);
12331 if (err != noErr) break;
12332
12333 if (FSRefMakePath (&fref, unix_path_name, 255) == noErr)
12334 #else
12335 if (path_from_vol_dir_name (path_name, 255, fs.vRefNum, fs.parID,
12336 fs.name) &&
12337 mac_to_posix_pathname (path_name, unix_path_name, 255))
12338 #endif
12339 drag_and_drop_file_list = Fcons (build_string (unix_path_name),
12340 drag_and_drop_file_list);
12341 }
12342 }
12343 }
12344
12345 error_exit:
12346 /* Nuke the coerced file list in any case */
12347 err2 = AEDisposeDesc(&the_desc);
12348
12349 descriptor_error_exit:
12350 /* InvalRect(&(gFrontMacWindowP->mWP->portRect)); */
12351 return err;
12352 }
12353
12354
12355 static pascal OSErr mac_do_receive_drag (WindowPtr window, void *handlerRefCon,
12356 DragReference theDrag)
12357 {
12358 short items;
12359 short index;
12360 FlavorFlags theFlags;
12361 Point mouse;
12362 OSErr result;
12363 ItemReference theItem;
12364 HFSFlavor data;
12365 FSRef fref;
12366 Size size = sizeof (HFSFlavor);
12367
12368 drag_and_drop_file_list = Qnil;
12369 GetDragMouse (theDrag, &mouse, 0L);
12370 CountDragItems (theDrag, &items);
12371 for (index = 1; index <= items; index++)
12372 {
12373 /* Only handle file references. */
12374 GetDragItemReferenceNumber (theDrag, index, &theItem);
12375 result = GetFlavorFlags (theDrag, theItem, flavorTypeHFS, &theFlags);
12376 if (result == noErr)
12377 {
12378 #ifdef MAC_OSX
12379 FSRef frref;
12380 #else
12381 Str255 path_name;
12382 #endif
12383 Str255 unix_path_name;
12384 GetFlavorData (theDrag, theItem, flavorTypeHFS, &data, &size, 0L);
12385 #ifdef MAC_OSX
12386 /* Use Carbon routines, otherwise it converts the file name
12387 to /Macintosh HD/..., which is not correct. */
12388 FSpMakeFSRef (&data.fileSpec, &fref);
12389 if (! FSRefMakePath (&fref, unix_path_name, sizeof (unix_path_name)));
12390 #else
12391 if (path_from_vol_dir_name (path_name, 255, data.fileSpec.vRefNum,
12392 data.fileSpec.parID, data.fileSpec.name) &&
12393 mac_to_posix_pathname (path_name, unix_path_name, 255))
12394 #endif
12395 drag_and_drop_file_list = Fcons (build_string (unix_path_name),
12396 drag_and_drop_file_list);
12397 }
12398 else
12399 return;
12400 }
12401 /* If there are items in the list, construct an event and post it to
12402 the queue like an interrupt using kbd_buffer_store_event. */
12403 if (!NILP (drag_and_drop_file_list))
12404 {
12405 struct input_event event;
12406 Lisp_Object frame;
12407 struct frame *f = ((mac_output *) GetWRefCon(window))->mFP;
12408 SetPort (GetWindowPort (window));
12409 GlobalToLocal (&mouse);
12410
12411 event.kind = DRAG_N_DROP_EVENT;
12412 event.code = 0;
12413 event.modifiers = 0;
12414 event.timestamp = TickCount () * (1000 / 60);
12415 XSETINT (event.x, mouse.h);
12416 XSETINT (event.y, mouse.v);
12417 XSETFRAME (frame, f);
12418 event.frame_or_window = Fcons (frame, drag_and_drop_file_list);
12419 event.arg = Qnil;
12420 /* Post to the interrupt queue */
12421 kbd_buffer_store_event (&event);
12422 /* MAC_TODO: Mimic behavior of windows by switching contexts to Emacs */
12423 {
12424 ProcessSerialNumber psn;
12425 GetCurrentProcess (&psn);
12426 SetFrontProcess (&psn);
12427 }
12428 }
12429 }
12430
12431
12432 /* Print Document Apple Event */
12433 static pascal OSErr
12434 do_ae_print_documents (const AppleEvent *pAE, AppleEvent *reply, long refcon)
12435 {
12436 return errAEEventNotHandled;
12437 }
12438
12439
12440 static pascal OSErr
12441 do_ae_quit_application (AppleEvent* message, AppleEvent *reply, long refcon)
12442 {
12443 /* FixMe: Do we need an unwind-protect or something here? And what
12444 do we do about unsaved files. Currently just forces quit rather
12445 than doing recursive callback to get user input. */
12446
12447 terminate_flag = true;
12448
12449 /* Fkill_emacs doesn't return. We have to return. (TI) */
12450 return noErr;
12451 }
12452
12453
12454 #if __profile__
12455 void
12456 profiler_exit_proc ()
12457 {
12458 ProfilerDump ("\pEmacs.prof");
12459 ProfilerTerm ();
12460 }
12461 #endif
12462
12463 /* These few functions implement Emacs as a normal Mac application
12464 (almost): set up the heap and the Toolbox, handle necessary
12465 system events plus a few simple menu events. They also set up
12466 Emacs's access to functions defined in the rest of this file.
12467 Emacs uses function hooks to perform all its terminal I/O. A
12468 complete list of these functions appear in termhooks.h. For what
12469 they do, read the comments there and see also w32term.c and
12470 xterm.c. What's noticeably missing here is the event loop, which
12471 is normally present in most Mac application. After performing the
12472 necessary Mac initializations, main passes off control to
12473 emacs_main (corresponding to main in emacs.c). Emacs_main calls
12474 mac_read_socket (defined further below) to read input. This is
12475 where WaitNextEvent is called to process Mac events. This is also
12476 where check_alarm in sysdep.c is called to simulate alarm signals.
12477 This makes the cursor jump back to its correct position after
12478 briefly jumping to that of the matching parenthesis, print useful
12479 hints and prompts in the minibuffer after the user stops typing for
12480 a wait, etc. */
12481
12482 #if !TARGET_API_MAC_CARBON
12483 #undef main
12484 int
12485 main (void)
12486 {
12487 #if __profile__ /* is the profiler on? */
12488 if (ProfilerInit(collectDetailed, bestTimeBase, 5000, 200))
12489 exit(1);
12490 #endif
12491
12492 #if __MWERKS__
12493 /* set creator and type for files created by MSL */
12494 _fcreator = 'EMAx';
12495 _ftype = 'TEXT';
12496 #endif
12497
12498 do_init_managers ();
12499
12500 do_get_menus ();
12501
12502 do_check_ram_size ();
12503
12504 init_emacs_passwd_dir ();
12505
12506 init_environ ();
12507
12508 initialize_applescript ();
12509
12510 init_required_apple_events ();
12511
12512 {
12513 char **argv;
12514 int argc = 0;
12515
12516 /* set up argv array from STR# resource */
12517 get_string_list (&argv, ARGV_STRING_LIST_ID);
12518 while (argv[argc])
12519 argc++;
12520
12521 /* free up AppleScript resources on exit */
12522 atexit (terminate_applescript);
12523
12524 #if __profile__ /* is the profiler on? */
12525 atexit (profiler_exit_proc);
12526 #endif
12527
12528 /* 3rd param "envp" never used in emacs_main */
12529 (void) emacs_main (argc, argv, 0);
12530 }
12531
12532 /* Never reached - real exit in Fkill_emacs */
12533 return 0;
12534 }
12535 #endif
12536
12537 /* Table for translating Mac keycode to X keysym values. Contributed
12538 by Sudhir Shenoy. */
12539 static unsigned char keycode_to_xkeysym_table[] = {
12540 /* 0x00 - 0x3f */
12541 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
12542 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
12543 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
12544 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
12545 /* 0x40 */
12546 0, '\xae' /* kp. */, 0, '\xaa' /* kp* */,
12547 0, '\xab' /* kp+ */, 0, '\x7f' /* kp_clr */,
12548 0, 0, 0, '\xaf' /* kp/ */,
12549 '\x8d' /* kp_ent */, 0, '\xad' /* kp- */, 0,
12550 /* 0x50 */
12551 0, '\xbd' /* kp= */, '\xb0' /* kp0 */, '\xb1' /* kp1 */,
12552 '\xb2' /* kp2 */, '\xb3' /* kp3 */, '\xb4' /* kp4 */, '\xb5' /* kp5 */,
12553 '\xb6' /* kp6 */, '\xb7' /* kp7 */, 0, '\xb8' /* kp8 */,
12554 '\xb9' /* kp9 */, 0, 0, 0,
12555 /* 0x60 */
12556 '\xc2' /* F5 */, '\xc3' /* F6 */, '\xc4' /* F7 */, '\xc0' /* F3 */,
12557 '\xc5' /* F8 */, '\xc6' /* F9 */, 0, '\xc8' /* F11 */,
12558 0, '\xca' /* F13 */, 0, '\xcb' /* F14 */,
12559 0, '\xc7' /* F10 */, 0, '\xc9' /* F12 */,
12560 /* 0x70 */
12561 0, '\xcc' /* F15 */, '\x9e' /* ins */, '\x95' /* home */,
12562 '\x9a' /* pgup */, '\x9f' /* del */, '\xc1' /* F4 */, '\x9c' /* end */,
12563 '\xbf' /* F2 */, '\x9b' /* pgdown */, '\xbe' /* F1 */, '\x51' /* left */,
12564 '\x53' /* right */, '\x54' /* down */, '\x52' /* up */, 0
12565 };
12566
12567 static int
12568 keycode_to_xkeysym (int keyCode, int *xKeySym)
12569 {
12570 *xKeySym = keycode_to_xkeysym_table [keyCode & 0x7f];
12571 return *xKeySym != 0;
12572 }
12573
12574 /* Emacs calls this whenever it wants to read an input event from the
12575 user. */
12576 int
12577 XTread_socket (int sd, struct input_event *bufp, int numchars, int expected)
12578 {
12579 int count = 0;
12580 #if USE_CARBON_EVENTS
12581 OSStatus rneResult;
12582 EventRef eventRef;
12583 EventMouseButton mouseBtn;
12584 #endif
12585 EventRecord er;
12586 int the_modifiers;
12587 EventMask event_mask;
12588
12589 #if 0
12590 if (interrupt_input_blocked)
12591 {
12592 interrupt_input_pending = 1;
12593 return -1;
12594 }
12595 #endif
12596
12597 interrupt_input_pending = 0;
12598 BLOCK_INPUT;
12599
12600 /* So people can tell when we have read the available input. */
12601 input_signal_count++;
12602
12603 if (numchars <= 0)
12604 abort ();
12605
12606 /* Don't poll for events to process (specifically updateEvt) if
12607 window update currently already in progress. A call to redisplay
12608 (in do_window_update) can be preempted by another call to
12609 redisplay, causing blank regions to be left on the screen and the
12610 cursor to be left at strange places. */
12611 if (handling_window_update)
12612 {
12613 UNBLOCK_INPUT;
12614 return 0;
12615 }
12616
12617 if (terminate_flag)
12618 Fkill_emacs (make_number (1));
12619
12620 /* It is necessary to set this (additional) argument slot of an
12621 event to nil because keyboard.c protects incompletely processed
12622 event from being garbage collected by placing them in the
12623 kbd_buffer_gcpro vector. */
12624 bufp->arg = Qnil;
12625
12626 event_mask = everyEvent;
12627 if (NILP (Fboundp (Qmac_ready_for_drag_n_drop)))
12628 event_mask -= highLevelEventMask;
12629
12630 #if USE_CARBON_EVENTS
12631 rneResult = ReceiveNextEvent (0, NULL,
12632 expected
12633 ? TicksToEventTime (app_sleep_time)
12634 : 0,
12635 true, &eventRef);
12636 if (!rneResult)
12637 {
12638 /* Handle new events */
12639 if (!mac_convert_event_ref (eventRef, &er))
12640 switch (GetEventClass (eventRef))
12641 {
12642 case kEventClassMouse:
12643 if (GetEventKind (eventRef) == kEventMouseWheelMoved)
12644 {
12645 SInt32 delta;
12646 Point point;
12647 WindowPtr window_ptr = FrontNonFloatingWindow ();
12648 struct mac_output *mwp = (mac_output *) GetWRefCon (window_ptr);
12649 GetEventParameter(eventRef, kEventParamMouseWheelDelta,
12650 typeSInt32, NULL, sizeof (SInt32),
12651 NULL, &delta);
12652 GetEventParameter(eventRef, kEventParamMouseLocation,
12653 typeQDPoint, NULL, sizeof (Point),
12654 NULL, &point);
12655 bufp->kind = MOUSE_WHEEL_EVENT;
12656 bufp->code = delta;
12657 bufp->modifiers = mac_event_to_emacs_modifiers(eventRef);
12658 SetPort (GetWindowPort (window_ptr));
12659 GlobalToLocal (&point);
12660 XSETINT (bufp->x, point.h);
12661 XSETINT (bufp->y, point.v);
12662 XSETFRAME (bufp->frame_or_window, mwp->mFP);
12663 bufp->timestamp = EventTimeToTicks (GetEventTime (eventRef))*(1000/60);
12664 count++;
12665 }
12666 else
12667 SendEventToEventTarget (eventRef, GetEventDispatcherTarget ());
12668
12669 break;
12670 default:
12671 /* Send the event to the appropriate receiver. */
12672 SendEventToEventTarget (eventRef, GetEventDispatcherTarget ());
12673 }
12674 else
12675 #else
12676 if (WaitNextEvent (event_mask, &er, (expected ? app_sleep_time : 0L), NULL))
12677 #endif /* USE_CARBON_EVENTS */
12678 switch (er.what)
12679 {
12680 case mouseDown:
12681 case mouseUp:
12682 {
12683 WindowPtr window_ptr = FrontWindow ();
12684 SInt16 part_code;
12685
12686 #if USE_CARBON_EVENTS
12687 /* This is needed to send mouse events like aqua window buttons
12688 to the correct handler. */
12689 if (eventNotHandledErr != SendEventToEventTarget (eventRef, GetEventDispatcherTarget ())) {
12690 break;
12691 }
12692
12693 if (!is_emacs_window(window_ptr))
12694 break;
12695 #endif
12696
12697 if (mouse_tracking_in_progress == mouse_tracking_scroll_bar
12698 && er.what == mouseUp)
12699 {
12700 struct mac_output *mwp = (mac_output *) GetWRefCon (window_ptr);
12701 Point mouse_loc = er.where;
12702
12703 /* Convert to local coordinates of new window. */
12704 #if TARGET_API_MAC_CARBON
12705 SetPort (GetWindowPort (window_ptr));
12706 #else
12707 SetPort (window_ptr);
12708 #endif
12709
12710 GlobalToLocal (&mouse_loc);
12711
12712 #if USE_CARBON_EVENTS
12713 bufp->code = mac_get_mouse_btn (eventRef);
12714 #else
12715 bufp->code = 0; /* only one mouse button */
12716 #endif
12717 bufp->kind = SCROLL_BAR_CLICK_EVENT;
12718 bufp->frame_or_window = tracked_scroll_bar->window;
12719 bufp->part = scroll_bar_handle;
12720 #if USE_CARBON_EVENTS
12721 bufp->modifiers = mac_event_to_emacs_modifiers (eventRef);
12722 #else
12723 bufp->modifiers = mac_to_emacs_modifiers (er.modifiers);
12724 #endif
12725 bufp->modifiers |= up_modifier;
12726 bufp->timestamp = er.when * (1000 / 60);
12727 /* ticks to milliseconds */
12728
12729 XSETINT (bufp->x, tracked_scroll_bar->left + 2);
12730 XSETINT (bufp->y, mouse_loc.v - 24);
12731 tracked_scroll_bar->dragging = Qnil;
12732 mouse_tracking_in_progress = mouse_tracking_none;
12733 tracked_scroll_bar = NULL;
12734 count++;
12735 break;
12736 }
12737
12738 part_code = FindWindow (er.where, &window_ptr);
12739
12740 switch (part_code)
12741 {
12742 case inMenuBar:
12743 {
12744 struct frame *f = ((mac_output *)
12745 GetWRefCon (FrontWindow ()))->mFP;
12746 saved_menu_event_location = er.where;
12747 bufp->kind = MENU_BAR_ACTIVATE_EVENT;
12748 XSETFRAME (bufp->frame_or_window, f);
12749 count++;
12750 }
12751 break;
12752
12753 case inContent:
12754 if (window_ptr != FrontWindow ())
12755 SelectWindow (window_ptr);
12756 else
12757 {
12758 SInt16 control_part_code;
12759 ControlHandle ch;
12760 struct mac_output *mwp = (mac_output *)
12761 GetWRefCon (window_ptr);
12762 Point mouse_loc = er.where;
12763
12764 /* convert to local coordinates of new window */
12765 #if TARGET_API_MAC_CARBON
12766 SetPort (GetWindowPort (window_ptr));
12767 #else
12768 SetPort (window_ptr);
12769 #endif
12770
12771 GlobalToLocal (&mouse_loc);
12772 #if TARGET_API_MAC_CARBON
12773 ch = FindControlUnderMouse (mouse_loc, window_ptr,
12774 &control_part_code);
12775 #else
12776 control_part_code = FindControl (mouse_loc, window_ptr, &ch);
12777 #endif
12778
12779 #if USE_CARBON_EVENTS
12780 bufp->code = mac_get_mouse_btn (eventRef);
12781 #else
12782 bufp->code = 0; /* only one mouse button */
12783 #endif
12784 XSETINT (bufp->x, mouse_loc.h);
12785 XSETINT (bufp->y, mouse_loc.v);
12786 bufp->timestamp = er.when * (1000 / 60);
12787 /* ticks to milliseconds */
12788
12789 #if TARGET_API_MAC_CARBON
12790 if (ch != 0)
12791 #else
12792 if (control_part_code != 0)
12793 #endif
12794 {
12795 struct scroll_bar *bar = (struct scroll_bar *)
12796 GetControlReference (ch);
12797 x_scroll_bar_handle_click (bar, control_part_code, &er,
12798 bufp);
12799 if (er.what == mouseDown
12800 && control_part_code == kControlIndicatorPart)
12801 {
12802 mouse_tracking_in_progress
12803 = mouse_tracking_scroll_bar;
12804 tracked_scroll_bar = bar;
12805 }
12806 else
12807 {
12808 mouse_tracking_in_progress = mouse_tracking_none;
12809 tracked_scroll_bar = NULL;
12810 }
12811 }
12812 else
12813 {
12814 bufp->kind = MOUSE_CLICK_EVENT;
12815 XSETFRAME (bufp->frame_or_window, mwp->mFP);
12816 if (er.what == mouseDown)
12817 mouse_tracking_in_progress
12818 = mouse_tracking_mouse_movement;
12819 else
12820 mouse_tracking_in_progress = mouse_tracking_none;
12821 }
12822
12823 #if USE_CARBON_EVENTS
12824 bufp->modifiers = mac_event_to_emacs_modifiers (eventRef);
12825 #else
12826 bufp->modifiers = mac_to_emacs_modifiers (er.modifiers);
12827 #endif
12828
12829 switch (er.what)
12830 {
12831 case mouseDown:
12832 bufp->modifiers |= down_modifier;
12833 break;
12834 case mouseUp:
12835 bufp->modifiers |= up_modifier;
12836 break;
12837 }
12838
12839 count++;
12840 }
12841 break;
12842
12843 case inDrag:
12844 #if TARGET_API_MAC_CARBON
12845 {
12846 BitMap bm;
12847
12848 GetQDGlobalsScreenBits (&bm);
12849 DragWindow (window_ptr, er.where, &bm.bounds);
12850 }
12851 #else /* not TARGET_API_MAC_CARBON */
12852 DragWindow (window_ptr, er.where, &qd.screenBits.bounds);
12853 #endif /* not TARGET_API_MAC_CARBON */
12854 break;
12855
12856 case inGoAway:
12857 if (TrackGoAway (window_ptr, er.where))
12858 {
12859 bufp->kind = DELETE_WINDOW_EVENT;
12860 XSETFRAME (bufp->frame_or_window,
12861 ((mac_output *) GetWRefCon (window_ptr))->mFP);
12862 count++;
12863 }
12864 break;
12865
12866 /* window resize handling added --ben */
12867 case inGrow:
12868 do_grow_window(window_ptr, &er);
12869 break;
12870
12871 /* window zoom handling added --ben */
12872 case inZoomIn:
12873 case inZoomOut:
12874 if (TrackBox (window_ptr, er.where, part_code))
12875 do_zoom_window (window_ptr, part_code);
12876 break;
12877
12878 default:
12879 break;
12880 }
12881 }
12882 break;
12883
12884 case updateEvt:
12885 case osEvt:
12886 case activateEvt:
12887 #if USE_CARBON_EVENTS
12888 if (eventNotHandledErr == SendEventToEventTarget (eventRef, GetEventDispatcherTarget ()))
12889 #endif
12890 do_events (&er);
12891 break;
12892
12893 case keyDown:
12894 case autoKey:
12895 {
12896 int keycode = (er.message & keyCodeMask) >> 8;
12897 int xkeysym;
12898
12899 #if USE_CARBON_EVENTS
12900 /* When using Carbon Events, we need to pass raw keyboard events
12901 to the TSM ourselves. If TSM handles it, it will pass back
12902 noErr, otherwise it will pass back "eventNotHandledErr" and
12903 we can process it normally. */
12904 if ((!NILP (Vmac_pass_command_to_system)
12905 || !(er.modifiers & cmdKey))
12906 && (!NILP (Vmac_pass_control_to_system)
12907 || !(er.modifiers & controlKey)))
12908 {
12909 OSStatus err;
12910 err = SendEventToEventTarget (eventRef,
12911 GetEventDispatcherTarget ());
12912 if (err != eventNotHandledErr)
12913 break;
12914 }
12915 #endif
12916
12917 if (!IsValidWindowPtr (FrontNonFloatingWindow ()))
12918 {
12919 SysBeep (1);
12920 UNBLOCK_INPUT;
12921 return 0;
12922 }
12923
12924 ObscureCursor ();
12925
12926 if (keycode == 0x33) /* delete key (charCode translated to 0x8) */
12927 {
12928 bufp->code = 0x7f;
12929 bufp->kind = ASCII_KEYSTROKE_EVENT;
12930 }
12931 else if (keycode_to_xkeysym (keycode, &xkeysym))
12932 {
12933 bufp->code = 0xff00 | xkeysym;
12934 bufp->kind = NON_ASCII_KEYSTROKE_EVENT;
12935 }
12936 else if (!NILP (Vmac_reverse_ctrl_meta) && (er.modifiers & controlKey))
12937 {
12938 /* This is a special case to deal with converting from
12939 a control character to non-control character */
12940 int new_modifiers = er.modifiers & ~controlKey;
12941 int new_keycode = keycode | new_modifiers;
12942 Ptr kchr_ptr = (Ptr) GetScriptManagerVariable (smKCHRCache);
12943 unsigned long some_state = 0;
12944 bufp->code = KeyTranslate (kchr_ptr, new_keycode, &some_state) & 0xff;
12945 bufp->kind = ASCII_KEYSTROKE_EVENT;
12946 }
12947 else
12948 {
12949 if (er.modifiers & macMetaKey)
12950 {
12951 /* This code comes from Keyboard Resource, Appendix
12952 C of IM - Text. This is necessary since shift is
12953 ignored in KCHR table translation when option or
12954 command is pressed. */
12955 int new_modifiers = er.modifiers & 0xf600;
12956 /* mask off option and command */
12957 int new_keycode = keycode | new_modifiers;
12958 Ptr kchr_ptr = (Ptr) GetScriptManagerVariable (smKCHRCache);
12959 unsigned long some_state = 0;
12960 bufp->code = KeyTranslate (kchr_ptr, new_keycode,
12961 &some_state) & 0xff;
12962 }
12963 else
12964 bufp->code = er.message & charCodeMask;
12965 bufp->kind = ASCII_KEYSTROKE_EVENT;
12966 }
12967 }
12968
12969 /* If variable mac-convert-keyboard-input-to-latin-1 is non-nil,
12970 convert non-ASCII characters typed at the Mac keyboard
12971 (presumed to be in the Mac Roman encoding) to iso-latin-1
12972 encoding before they are passed to Emacs. This enables the
12973 Mac keyboard to be used to enter non-ASCII iso-latin-1
12974 characters directly. */
12975 if (mac_keyboard_text_encoding != kTextEncodingMacRoman
12976 && bufp->kind == ASCII_KEYSTROKE_EVENT && bufp->code >= 128)
12977 {
12978 static TECObjectRef converter = NULL;
12979 OSStatus the_err = noErr;
12980 OSStatus convert_status = noErr;
12981
12982 if (converter == NULL)
12983 {
12984 the_err = TECCreateConverter (&converter,
12985 kTextEncodingMacRoman,
12986 mac_keyboard_text_encoding);
12987 current_mac_keyboard_text_encoding
12988 = mac_keyboard_text_encoding;
12989 }
12990 else if (mac_keyboard_text_encoding
12991 != current_mac_keyboard_text_encoding)
12992 {
12993 /* Free the converter for the current encoding before
12994 creating a new one. */
12995 TECDisposeConverter (converter);
12996 the_err = TECCreateConverter (&converter,
12997 kTextEncodingMacRoman,
12998 mac_keyboard_text_encoding);
12999 current_mac_keyboard_text_encoding
13000 = mac_keyboard_text_encoding;
13001 }
13002
13003 if (the_err == noErr)
13004 {
13005 unsigned char ch = bufp->code;
13006 ByteCount actual_input_length, actual_output_length;
13007 unsigned char outch;
13008
13009 convert_status = TECConvertText (converter, &ch, 1,
13010 &actual_input_length,
13011 &outch, 1,
13012 &actual_output_length);
13013 if (convert_status == noErr
13014 && actual_input_length == 1
13015 && actual_output_length == 1)
13016 bufp->code = outch;
13017 }
13018 }
13019
13020 #if USE_CARBON_EVENTS
13021 bufp->modifiers = mac_event_to_emacs_modifiers (eventRef);
13022 #else
13023 bufp->modifiers = mac_to_emacs_modifiers (er.modifiers);
13024 #endif
13025
13026 {
13027 mac_output *mwp
13028 = (mac_output *) GetWRefCon (FrontNonFloatingWindow ());
13029 XSETFRAME (bufp->frame_or_window, mwp->mFP);
13030 }
13031
13032 bufp->timestamp = er.when * (1000 / 60); /* ticks to milliseconds */
13033
13034 count++;
13035 break;
13036
13037 case kHighLevelEvent:
13038 drag_and_drop_file_list = Qnil;
13039
13040 AEProcessAppleEvent(&er);
13041
13042 /* Build a DRAG_N_DROP_EVENT type event as is done in
13043 constuct_drag_n_drop in w32term.c. */
13044 if (!NILP (drag_and_drop_file_list))
13045 {
13046 struct frame *f = NULL;
13047 WindowPtr wp;
13048 Lisp_Object frame;
13049
13050 wp = FrontNonFloatingWindow ();
13051 if (wp && is_emacs_window(wp))
13052 f = ((mac_output *) GetWRefCon (wp))->mFP;
13053
13054 bufp->kind = DRAG_N_DROP_EVENT;
13055 bufp->code = 0;
13056 bufp->timestamp = er.when * (1000 / 60);
13057 /* ticks to milliseconds */
13058 #if USE_CARBON_EVENTS
13059 bufp->modifiers = mac_event_to_emacs_modifiers (eventRef);
13060 #else
13061 bufp->modifiers = mac_to_emacs_modifiers (er.modifiers);
13062 #endif
13063
13064 XSETINT (bufp->x, 0);
13065 XSETINT (bufp->y, 0);
13066
13067 XSETFRAME (frame, f);
13068 bufp->frame_or_window = Fcons (frame, drag_and_drop_file_list);
13069
13070 /* Regardless of whether Emacs was suspended or in the
13071 foreground, ask it to redraw its entire screen.
13072 Otherwise parts of the screen can be left in an
13073 inconsistent state. */
13074 if (wp)
13075 #if TARGET_API_MAC_CARBON
13076 {
13077 Rect r;
13078
13079 GetWindowPortBounds (wp, &r);
13080 InvalWindowRect (wp, &r);
13081 }
13082 #else /* not TARGET_API_MAC_CARBON */
13083 InvalRect (&(wp->portRect));
13084 #endif /* not TARGET_API_MAC_CARBON */
13085
13086 count++;
13087 }
13088 default:
13089 break;
13090 }
13091 #if USE_CARBON_EVENTS
13092 ReleaseEvent (eventRef);
13093 }
13094 #endif
13095
13096 /* If the focus was just given to an autoraising frame,
13097 raise it now. */
13098 /* ??? This ought to be able to handle more than one such frame. */
13099 if (pending_autoraise_frame)
13100 {
13101 x_raise_frame (pending_autoraise_frame);
13102 pending_autoraise_frame = 0;
13103 }
13104
13105 #if !TARGET_API_MAC_CARBON
13106 check_alarm (); /* simulate the handling of a SIGALRM */
13107 #endif
13108
13109 {
13110 static Point old_mouse_pos = { -1, -1 };
13111
13112 if (app_is_suspended)
13113 {
13114 old_mouse_pos.h = -1;
13115 old_mouse_pos.v = -1;
13116 }
13117 else
13118 {
13119 Point mouse_pos;
13120 WindowPtr wp;
13121 struct frame *f;
13122 Lisp_Object bar;
13123 struct scroll_bar *sb;
13124
13125 wp = FrontWindow ();
13126 if (is_emacs_window (wp))
13127 {
13128 f = ((mac_output *) GetWRefCon (wp))->mFP;
13129
13130 #if TARGET_API_MAC_CARBON
13131 SetPort (GetWindowPort (wp));
13132 #else
13133 SetPort (wp);
13134 #endif
13135
13136 GetMouse (&mouse_pos);
13137
13138 if (!EqualPt (mouse_pos, old_mouse_pos))
13139 {
13140 if (mouse_tracking_in_progress == mouse_tracking_scroll_bar
13141 && tracked_scroll_bar)
13142 x_scroll_bar_note_movement (tracked_scroll_bar,
13143 mouse_pos.v
13144 - XINT (tracked_scroll_bar->top),
13145 TickCount() * (1000 / 60));
13146 else
13147 note_mouse_movement (f, &mouse_pos);
13148
13149 old_mouse_pos = mouse_pos;
13150 }
13151 }
13152 }
13153 }
13154
13155 UNBLOCK_INPUT;
13156
13157 return count;
13158 }
13159
13160
13161 /* Need to override CodeWarrior's input function so no conversion is
13162 done on newlines Otherwise compiled functions in .elc files will be
13163 read incorrectly. Defined in ...:MSL C:MSL
13164 Common:Source:buffer_io.c. */
13165 #ifdef __MWERKS__
13166 void
13167 __convert_to_newlines (unsigned char * p, size_t * n)
13168 {
13169 #pragma unused(p,n)
13170 }
13171
13172 void
13173 __convert_from_newlines (unsigned char * p, size_t * n)
13174 {
13175 #pragma unused(p,n)
13176 }
13177 #endif
13178
13179
13180 /* Initialize the struct pointed to by MW to represent a new COLS x
13181 ROWS Macintosh window, using font with name FONTNAME and size
13182 FONTSIZE. */
13183 void
13184 NewMacWindow (FRAME_PTR fp)
13185 {
13186 mac_output *mwp;
13187 #if TARGET_API_MAC_CARBON
13188 static int making_terminal_window = 0;
13189 #else
13190 static int making_terminal_window = 1;
13191 #endif
13192
13193 mwp = fp->output_data.mac;
13194
13195 if (making_terminal_window)
13196 {
13197 if (!(mwp->mWP = GetNewCWindow (TERM_WINDOW_RESOURCE, NULL,
13198 (WindowPtr) -1)))
13199 abort ();
13200 making_terminal_window = 0;
13201 }
13202 else
13203 if (!(mwp->mWP = GetNewCWindow (WINDOW_RESOURCE, NULL, (WindowPtr) -1)))
13204 abort ();
13205
13206 SetWRefCon (mwp->mWP, (long) mwp);
13207 /* so that update events can find this mac_output struct */
13208 mwp->mFP = fp; /* point back to emacs frame */
13209
13210 #if TARGET_API_MAC_CARBON
13211 SetPort (GetWindowPort (mwp->mWP));
13212 #else
13213 SetPort (mwp->mWP);
13214 #endif
13215
13216 mwp->fontset = -1;
13217
13218 SizeWindow (mwp->mWP, mwp->pixel_width, mwp->pixel_height, false);
13219 ShowWindow (mwp->mWP);
13220
13221 }
13222
13223
13224 void make_mac_frame (struct frame *f)
13225 {
13226 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
13227 FRAME_VERTICAL_SCROLL_BAR_TYPE (f) = vertical_scroll_bar_right;
13228
13229 NewMacWindow(f);
13230
13231 f->output_data.mac->cursor_pixel = 0;
13232 f->output_data.mac->border_pixel = 0x00ff00;
13233 f->output_data.mac->mouse_pixel = 0xff00ff;
13234 f->output_data.mac->cursor_foreground_pixel = 0x0000ff;
13235
13236 f->output_data.mac->desired_cursor = FILLED_BOX_CURSOR;
13237
13238 f->output_data.mac->fontset = -1;
13239 f->output_data.mac->scroll_bar_foreground_pixel = -1;
13240 f->output_data.mac->scroll_bar_background_pixel = -1;
13241 f->output_data.mac->left_pos = 4;
13242 f->output_data.mac->top_pos = 4;
13243 f->output_data.mac->border_width = 0;
13244 f->output_data.mac->explicit_parent = 0;
13245
13246 f->output_data.mac->internal_border_width = 0;
13247
13248 f->output_method = output_mac;
13249
13250 f->auto_raise = 1;
13251 f->auto_lower = 1;
13252
13253 f->new_width = 0;
13254 f->new_height = 0;
13255 }
13256
13257 void make_mac_terminal_frame (struct frame *f)
13258 {
13259 Lisp_Object frame;
13260
13261 XSETFRAME (frame, f);
13262
13263 f->output_method = output_mac;
13264 f->output_data.mac = (struct mac_output *)
13265 xmalloc (sizeof (struct mac_output));
13266 bzero (f->output_data.mac, sizeof (struct mac_output));
13267 f->output_data.mac->fontset = -1;
13268 f->output_data.mac->scroll_bar_foreground_pixel = -1;
13269 f->output_data.mac->scroll_bar_background_pixel = -1;
13270
13271 XSETFRAME (FRAME_KBOARD (f)->Vdefault_minibuffer_frame, f);
13272
13273 f->width = 96;
13274 f->height = 4;
13275
13276 make_mac_frame (f);
13277
13278 x_make_gc (f);
13279
13280 /* Need to be initialized for unshow_buffer in window.c. */
13281 selected_window = f->selected_window;
13282
13283 Fmodify_frame_parameters (frame,
13284 Fcons (Fcons (Qfont,
13285 build_string ("-*-monaco-medium-r-*--*-90-*-*-*-*-mac-roman")), Qnil));
13286 Fmodify_frame_parameters (frame,
13287 Fcons (Fcons (Qforeground_color,
13288 build_string ("black")), Qnil));
13289 Fmodify_frame_parameters (frame,
13290 Fcons (Fcons (Qbackground_color,
13291 build_string ("white")), Qnil));
13292 }
13293
13294 \f
13295 /***********************************************************************
13296 Initialization
13297 ***********************************************************************/
13298
13299 #ifdef USE_X_TOOLKIT
13300 static XrmOptionDescRec emacs_options[] = {
13301 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
13302 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
13303
13304 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
13305 XrmoptionSepArg, NULL},
13306 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
13307
13308 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13309 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13310 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13311 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13312 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13313 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
13314 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
13315 };
13316 #endif /* USE_X_TOOLKIT */
13317
13318 #ifdef MULTI_KBOARD
13319 /* Test whether two display-name strings agree up to the dot that separates
13320 the screen number from the server number. */
13321 static int
13322 same_x_server (name1, name2)
13323 char *name1, *name2;
13324 {
13325 int seen_colon = 0;
13326 unsigned char *system_name = SDATA (Vsystem_name);
13327 int system_name_length = strlen (system_name);
13328 int length_until_period = 0;
13329
13330 while (system_name[length_until_period] != 0
13331 && system_name[length_until_period] != '.')
13332 length_until_period++;
13333
13334 /* Treat `unix' like an empty host name. */
13335 if (! strncmp (name1, "unix:", 5))
13336 name1 += 4;
13337 if (! strncmp (name2, "unix:", 5))
13338 name2 += 4;
13339 /* Treat this host's name like an empty host name. */
13340 if (! strncmp (name1, system_name, system_name_length)
13341 && name1[system_name_length] == ':')
13342 name1 += system_name_length;
13343 if (! strncmp (name2, system_name, system_name_length)
13344 && name2[system_name_length] == ':')
13345 name2 += system_name_length;
13346 /* Treat this host's domainless name like an empty host name. */
13347 if (! strncmp (name1, system_name, length_until_period)
13348 && name1[length_until_period] == ':')
13349 name1 += length_until_period;
13350 if (! strncmp (name2, system_name, length_until_period)
13351 && name2[length_until_period] == ':')
13352 name2 += length_until_period;
13353
13354 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
13355 {
13356 if (*name1 == ':')
13357 seen_colon++;
13358 if (seen_colon && *name1 == '.')
13359 return 1;
13360 }
13361 return (seen_colon
13362 && (*name1 == '.' || *name1 == '\0')
13363 && (*name2 == '.' || *name2 == '\0'));
13364 }
13365 #endif
13366
13367 int mac_initialized = 0;
13368
13369 void
13370 mac_initialize_display_info ()
13371 {
13372 struct mac_display_info *dpyinfo = &one_mac_display_info;
13373 GDHandle main_device_handle;
13374
13375 bzero (dpyinfo, sizeof (*dpyinfo));
13376
13377 /* Put it on x_display_name_list. */
13378 x_display_name_list = Fcons (Fcons (build_string ("Mac"), Qnil),
13379 x_display_name_list);
13380 dpyinfo->name_list_element = XCAR (x_display_name_list);
13381
13382 #if 0
13383 dpyinfo->mac_id_name
13384 = (char *) xmalloc (SCHARS (Vinvocation_name)
13385 + SCHARS (Vsystem_name)
13386 + 2);
13387 sprintf (dpyinfo->mac_id_name, "%s@%s",
13388 SDATA (Vinvocation_name), SDATA (Vsystem_name));
13389 #else
13390 dpyinfo->mac_id_name = (char *) xmalloc (strlen ("Mac Display") + 1);
13391 strcpy (dpyinfo->mac_id_name, "Mac Display");
13392 #endif
13393
13394 main_device_handle = LMGetMainDevice();
13395
13396 dpyinfo->reference_count = 0;
13397 dpyinfo->resx = 75.0;
13398 dpyinfo->resy = 75.0;
13399 dpyinfo->n_planes = 1;
13400 dpyinfo->n_cbits = 16;
13401 dpyinfo->height = (**main_device_handle).gdRect.bottom;
13402 dpyinfo->width = (**main_device_handle).gdRect.right;
13403 dpyinfo->grabbed = 0;
13404 dpyinfo->root_window = NULL;
13405
13406 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
13407 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
13408 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
13409 dpyinfo->mouse_face_window = Qnil;
13410 }
13411
13412 struct mac_display_info *
13413 mac_term_init (display_name, xrm_option, resource_name)
13414 Lisp_Object display_name;
13415 char *xrm_option;
13416 char *resource_name;
13417 {
13418 struct mac_display_info *dpyinfo;
13419 GDHandle main_device_handle;
13420
13421 if (!mac_initialized)
13422 {
13423 mac_initialize ();
13424 mac_initialized = 1;
13425 }
13426
13427 mac_initialize_display_info (display_name);
13428
13429 dpyinfo = &one_mac_display_info;
13430
13431 main_device_handle = LMGetMainDevice();
13432
13433 dpyinfo->height = (**main_device_handle).gdRect.bottom;
13434 dpyinfo->width = (**main_device_handle).gdRect.right;
13435
13436 return dpyinfo;
13437 }
13438 \f
13439 #ifdef MAC_OSX
13440 void MakeMeTheFrontProcess ()
13441 {
13442 ProcessSerialNumber psn;
13443 OSErr err;
13444
13445 err = GetCurrentProcess (&psn);
13446 if (err == noErr)
13447 (void) SetFrontProcess (&psn);
13448 }
13449 #endif /* MAC_OSX */
13450
13451 /* Set up use of X before we make the first connection. */
13452
13453 static struct redisplay_interface x_redisplay_interface =
13454 {
13455 x_produce_glyphs,
13456 x_write_glyphs,
13457 x_insert_glyphs,
13458 x_clear_end_of_line,
13459 x_scroll_run,
13460 x_after_update_window_line,
13461 x_update_window_begin,
13462 x_update_window_end,
13463 XTcursor_to,
13464 x_flush,
13465 x_clear_mouse_face,
13466 x_get_glyph_overhangs,
13467 x_fix_overlapping_area
13468 };
13469
13470 void
13471 mac_initialize ()
13472 {
13473 rif = &x_redisplay_interface;
13474
13475 clear_frame_hook = x_clear_frame;
13476 ins_del_lines_hook = x_ins_del_lines;
13477 delete_glyphs_hook = x_delete_glyphs;
13478 ring_bell_hook = XTring_bell;
13479 reset_terminal_modes_hook = XTreset_terminal_modes;
13480 set_terminal_modes_hook = XTset_terminal_modes;
13481 update_begin_hook = x_update_begin;
13482 update_end_hook = x_update_end;
13483 set_terminal_window_hook = XTset_terminal_window;
13484 read_socket_hook = XTread_socket;
13485 frame_up_to_date_hook = XTframe_up_to_date;
13486 mouse_position_hook = XTmouse_position;
13487 frame_rehighlight_hook = XTframe_rehighlight;
13488 frame_raise_lower_hook = XTframe_raise_lower;
13489
13490 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13491 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13492 redeem_scroll_bar_hook = XTredeem_scroll_bar;
13493 judge_scroll_bars_hook = XTjudge_scroll_bars;
13494
13495 estimate_mode_line_height_hook = x_estimate_mode_line_height;
13496
13497 scroll_region_ok = 1; /* we'll scroll partial frames */
13498 char_ins_del_ok = 1;
13499 line_ins_del_ok = 1; /* we'll just blt 'em */
13500 fast_clear_end_of_line = 1; /* X does this well */
13501 memory_below_frame = 0; /* we don't remember what scrolls
13502 off the bottom */
13503 baud_rate = 19200;
13504
13505 x_noop_count = 0;
13506 last_tool_bar_item = -1;
13507 any_help_event_p = 0;
13508
13509 /* Try to use interrupt input; if we can't, then start polling. */
13510 Fset_input_mode (Qt, Qnil, Qt, Qnil);
13511
13512 #ifdef USE_X_TOOLKIT
13513 XtToolkitInitialize ();
13514 Xt_app_con = XtCreateApplicationContext ();
13515 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
13516
13517 /* Install an asynchronous timer that processes Xt timeout events
13518 every 0.1s. This is necessary because some widget sets use
13519 timeouts internally, for example the LessTif menu bar, or the
13520 Xaw3d scroll bar. When Xt timouts aren't processed, these
13521 widgets don't behave normally. */
13522 {
13523 EMACS_TIME interval;
13524 EMACS_SET_SECS_USECS (interval, 0, 100000);
13525 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
13526 }
13527 #endif
13528
13529 #if USE_TOOLKIT_SCROLL_BARS
13530 xaw3d_arrow_scroll = False;
13531 xaw3d_pick_top = True;
13532 #endif
13533
13534 #if 0
13535 /* Note that there is no real way portable across R3/R4 to get the
13536 original error handler. */
13537 XSetErrorHandler (x_error_handler);
13538 XSetIOErrorHandler (x_io_error_quitter);
13539
13540 /* Disable Window Change signals; they are handled by X events. */
13541 #ifdef SIGWINCH
13542 signal (SIGWINCH, SIG_DFL);
13543 #endif /* ! defined (SIGWINCH) */
13544
13545 signal (SIGPIPE, x_connection_signal);
13546 #endif
13547
13548 mac_initialize_display_info ();
13549
13550 #if TARGET_API_MAC_CARBON
13551 init_required_apple_events ();
13552
13553 init_mac_drag_n_drop ();
13554
13555 #if USE_CARBON_EVENTS
13556 init_service_handler ();
13557 #endif
13558
13559 DisableMenuCommand (NULL, kHICommandQuit);
13560
13561 if (!inhibit_window_system)
13562 MakeMeTheFrontProcess ();
13563 #endif
13564 }
13565
13566
13567 void
13568 syms_of_macterm ()
13569 {
13570 #if 0
13571 staticpro (&x_error_message_string);
13572 x_error_message_string = Qnil;
13573 #endif
13574
13575 staticpro (&x_display_name_list);
13576 x_display_name_list = Qnil;
13577
13578 staticpro (&last_mouse_scroll_bar);
13579 last_mouse_scroll_bar = Qnil;
13580
13581 staticpro (&Qvendor_specific_keysyms);
13582 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
13583
13584 staticpro (&last_mouse_press_frame);
13585 last_mouse_press_frame = Qnil;
13586
13587 Qmac_ready_for_drag_n_drop = intern ("mac-ready-for-drag-n-drop");
13588 staticpro (&Qmac_ready_for_drag_n_drop);
13589
13590 help_echo = Qnil;
13591 staticpro (&help_echo);
13592 help_echo_object = Qnil;
13593 staticpro (&help_echo_object);
13594 help_echo_window = Qnil;
13595 staticpro (&help_echo_window);
13596 previous_help_echo = Qnil;
13597 staticpro (&previous_help_echo);
13598 help_echo_pos = -1;
13599
13600 DEFVAR_BOOL ("x-autoselect-window", &x_autoselect_window_p,
13601 doc: /* *Non-nil means autoselect window with mouse pointer. */);
13602 x_autoselect_window_p = 0;
13603
13604 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
13605 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
13606 For example, if a block cursor is over a tab, it will be drawn as
13607 wide as that tab on the display. */);
13608 x_stretch_cursor_p = 0;
13609
13610 #if 0 /* TODO: Setting underline position from font properties. */
13611 DEFVAR_BOOL ("x-use-underline-position-properties",
13612 &x_use_underline_position_properties,
13613 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
13614 nil means ignore them. If you encounter fonts with bogus
13615 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
13616 to 4.1, set this to nil. */);
13617 x_use_underline_position_properties = 1;
13618 #endif
13619
13620 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
13621 doc: /* If not nil, Emacs uses toolkit scroll bars. */);
13622 Vx_toolkit_scroll_bars = Qt;
13623
13624 staticpro (&last_mouse_motion_frame);
13625 last_mouse_motion_frame = Qnil;
13626
13627 DEFVAR_LISP ("mac-command-key-is-meta", &Vmac_command_key_is_meta,
13628 doc: /* Non-nil means that the command key is used as the Emacs meta key.
13629 Otherwise the option key is used. */);
13630 Vmac_command_key_is_meta = Qt;
13631
13632 DEFVAR_LISP ("mac-reverse-ctrl-meta", &Vmac_reverse_ctrl_meta,
13633 doc: /* Non-nil means that the control and meta keys are reversed. This is
13634 useful for non-standard keyboard layouts. */);
13635 Vmac_reverse_ctrl_meta = Qnil;
13636
13637 #if USE_CARBON_EVENTS
13638 DEFVAR_LISP ("mac-wheel-button-is-mouse-2", &Vmac_wheel_button_is_mouse_2,
13639 doc: /* Non-nil means that the wheel button will be treated as mouse-2 and
13640 the right click will be mouse-3.
13641 Otherwise, the right click will be mouse-2 and the wheel button mouse-3.*/);
13642 Vmac_wheel_button_is_mouse_2 = Qt;
13643
13644 DEFVAR_LISP ("mac-pass-command-to-system", &Vmac_pass_command_to_system,
13645 doc: /* If non-nil, the Mac \"Command\" key is passed on to the Mac
13646 Toolbox for processing before Emacs sees it. */);
13647 Vmac_pass_command_to_system = Qt;
13648
13649 DEFVAR_LISP ("mac-pass-control-to-system", &Vmac_pass_control_to_system,
13650 doc: /* If non-nil, the Mac \"Control\" key is passed on to the Mac
13651 Toolbox for processing before Emacs sees it. */);
13652 Vmac_pass_control_to_system = Qt;
13653 #endif
13654
13655 DEFVAR_INT ("mac-keyboard-text-encoding", &mac_keyboard_text_encoding,
13656 doc: /* One of the Text Encoding Base constant values defined in the
13657 Basic Text Constants section of Inside Macintosh - Text Encoding
13658 Conversion Manager. Its value determines the encoding characters
13659 typed at the Mac keyboard (presumed to be in the MacRoman encoding)
13660 will convert into. E.g., if it is set to kTextEncodingMacRoman (0),
13661 its default value, no conversion takes place. If it is set to
13662 kTextEncodingISOLatin1 (0x201) or kTextEncodingISOLatin2 (0x202),
13663 characters typed on Mac keyboard are first converted into the
13664 ISO Latin-1 or ISO Latin-2 encoding, respectively before being
13665 passed to Emacs. Together with Emacs's set-keyboard-coding-system
13666 command, this enables the Mac keyboard to be used to enter non-ASCII
13667 characters directly. */);
13668 mac_keyboard_text_encoding = kTextEncodingMacRoman;
13669 }