(PC-do-completion): Do not forget to use `pred' as the default-directory
[emacs.git] / src / macterm.c
blob6fe2bc1603c4f7642afb8cf131526f16f1f9bae4
1 /* Implementation of GUI terminal on the Mac OS.
2 Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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.
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.
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. */
21 /* Contributed by Andrew Choi (akochoi@mac.com). */
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"
31 #include "macterm.h"
33 #ifndef MAC_OSX
34 #include <alloca.h>
35 #endif
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>
81 #if __profile__
82 #include <profiler.h>
83 #endif
84 #endif /* not MAC_OSX */
86 #include "systty.h"
87 #include "systime.h"
88 #include "atimer.h"
89 #include "keymap.h"
91 #include <ctype.h>
92 #include <errno.h>
93 #include <setjmp.h>
94 #include <sys/stat.h>
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"
111 /* Set of macros that handle mapping of Mac modifier keys to emacs. */
112 #define macCtrlKey (NILP (Vmac_reverse_ctrl_meta) ? controlKey : \
113 (NILP (Vmac_command_key_is_meta) ? optionKey : cmdKey))
114 #define macShiftKey (shiftKey)
115 #define macMetaKey (NILP (Vmac_reverse_ctrl_meta) ? \
116 (NILP (Vmac_command_key_is_meta) ? optionKey : cmdKey) \
117 : controlKey)
118 #define macAltKey (NILP (Vmac_command_key_is_meta) ? cmdKey : optionKey)
122 /* Non-nil means Emacs uses toolkit scroll bars. */
124 Lisp_Object Vx_toolkit_scroll_bars;
126 /* Non-zero means that a HELP_EVENT has been generated since Emacs
127 start. */
129 static int any_help_event_p;
131 /* Non-zero means autoselect window with the mouse cursor. */
133 int x_autoselect_window_p;
135 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
137 int x_use_underline_position_properties;
139 /* Non-zero means draw block and hollow cursor as wide as the glyph
140 under it. For example, if a block cursor is over a tab, it will be
141 drawn as wide as that tab on the display. */
144 /* This is a chain of structures for all the X displays currently in
145 use. */
147 struct x_display_info *x_display_list;
149 /* This is a list of cons cells, each of the form (NAME
150 . FONT-LIST-CACHE), one for each element of x_display_list and in
151 the same order. NAME is the name of the frame. FONT-LIST-CACHE
152 records previous values returned by x-list-fonts. */
154 Lisp_Object x_display_name_list;
156 /* This is display since Mac does not support multiple ones. */
157 struct mac_display_info one_mac_display_info;
159 /* Frame being updated by update_frame. This is declared in term.c.
160 This is set by update_begin and looked at by all the XT functions.
161 It is zero while not inside an update. In that case, the XT
162 functions assume that `selected_frame' is the frame to apply to. */
164 extern struct frame *updating_frame;
166 extern int waiting_for_input;
168 /* This is a frame waiting to be auto-raised, within XTread_socket. */
170 struct frame *pending_autoraise_frame;
172 /* Non-zero means user is interacting with a toolkit scroll bar. */
174 static int toolkit_scroll_bar_interaction;
176 /* Mouse movement.
178 Formerly, we used PointerMotionHintMask (in standard_event_mask)
179 so that we would have to call XQueryPointer after each MotionNotify
180 event to ask for another such event. However, this made mouse tracking
181 slow, and there was a bug that made it eventually stop.
183 Simply asking for MotionNotify all the time seems to work better.
185 In order to avoid asking for motion events and then throwing most
186 of them away or busy-polling the server for mouse positions, we ask
187 the server for pointer motion hints. This means that we get only
188 one event per group of mouse movements. "Groups" are delimited by
189 other kinds of events (focus changes and button clicks, for
190 example), or by XQueryPointer calls; when one of these happens, we
191 get another MotionNotify event the next time the mouse moves. This
192 is at least as efficient as getting motion events when mouse
193 tracking is on, and I suspect only negligibly worse when tracking
194 is off. */
196 /* Where the mouse was last time we reported a mouse event. */
198 static Rect last_mouse_glyph;
199 static Lisp_Object last_mouse_press_frame;
201 /* The scroll bar in which the last X motion event occurred.
203 If the last X motion event occurred in a scroll bar, we set this so
204 XTmouse_position can know whether to report a scroll bar motion or
205 an ordinary motion.
207 If the last X motion event didn't occur in a scroll bar, we set
208 this to Qnil, to tell XTmouse_position to return an ordinary motion
209 event. */
211 static Lisp_Object last_mouse_scroll_bar;
213 /* This is a hack. We would really prefer that XTmouse_position would
214 return the time associated with the position it returns, but there
215 doesn't seem to be any way to wrest the time-stamp from the server
216 along with the position query. So, we just keep track of the time
217 of the last movement we received, and return that in hopes that
218 it's somewhat accurate. */
220 static Time last_mouse_movement_time;
222 enum mouse_tracking_type {
223 mouse_tracking_none,
224 mouse_tracking_mouse_movement,
225 mouse_tracking_scroll_bar
228 enum mouse_tracking_type mouse_tracking_in_progress = mouse_tracking_none;
230 struct scroll_bar *tracked_scroll_bar = NULL;
232 /* Incremented by XTread_socket whenever it really tries to read
233 events. */
235 #ifdef __STDC__
236 static int volatile input_signal_count;
237 #else
238 static int input_signal_count;
239 #endif
241 /* Used locally within XTread_socket. */
243 static int x_noop_count;
245 /* Initial values of argv and argc. */
247 extern char **initial_argv;
248 extern int initial_argc;
250 extern Lisp_Object Vcommand_line_args, Vsystem_name;
252 /* Tells if a window manager is present or not. */
254 extern Lisp_Object Vx_no_window_manager;
256 extern Lisp_Object Qface, Qmouse_face;
258 extern int errno;
260 /* A mask of extra modifier bits to put into every keyboard char. */
262 extern int extra_keyboard_modifiers;
264 static Lisp_Object Qvendor_specific_keysyms;
266 #if 0
267 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
268 #endif
270 extern int inhibit_window_system;
272 #if __MRC__
273 QDGlobals qd; /* QuickDraw global information structure. */
274 #endif
277 struct frame * x_window_to_frame (struct mac_display_info *, WindowPtr);
278 struct mac_display_info *mac_display_info_for_display (Display *);
279 static void x_update_window_end P_ ((struct window *, int, int));
280 static void mac_handle_tool_bar_click P_ ((struct frame *, EventRecord *));
281 static int x_io_error_quitter P_ ((Display *));
282 int x_catch_errors P_ ((Display *));
283 void x_uncatch_errors P_ ((Display *, int));
284 void x_lower_frame P_ ((struct frame *));
285 void x_scroll_bar_clear P_ ((struct frame *));
286 int x_had_errors_p P_ ((Display *));
287 void x_wm_set_size_hint P_ ((struct frame *, long, int));
288 void x_raise_frame P_ ((struct frame *));
289 void x_set_window_size P_ ((struct frame *, int, int, int));
290 void x_wm_set_window_state P_ ((struct frame *, int));
291 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
292 void mac_initialize P_ ((void));
293 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
294 static int x_compute_min_glyph_bounds P_ ((struct frame *));
295 static void x_update_end P_ ((struct frame *));
296 static void XTframe_up_to_date P_ ((struct frame *));
297 static void XTreassert_line_highlight P_ ((int, int));
298 static void x_change_line_highlight P_ ((int, int, int, int));
299 static void XTset_terminal_modes P_ ((void));
300 static void XTreset_terminal_modes P_ ((void));
301 static void x_clear_frame P_ ((void));
302 static void frame_highlight P_ ((struct frame *));
303 static void frame_unhighlight P_ ((struct frame *));
304 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
305 static void XTframe_rehighlight P_ ((struct frame *));
306 static void x_frame_rehighlight P_ ((struct x_display_info *));
307 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
308 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
309 static void x_clip_to_row P_ ((struct window *, struct glyph_row *, GC));
310 static void x_flush P_ ((struct frame *f));
311 static void x_update_begin P_ ((struct frame *));
312 static void x_update_window_begin P_ ((struct window *));
313 static void x_after_update_window_line P_ ((struct glyph_row *));
315 void activate_scroll_bars (FRAME_PTR);
316 void deactivate_scroll_bars (FRAME_PTR);
318 static int is_emacs_window (WindowPtr);
320 extern int image_ascent (struct image *, struct face *);
321 int x_bitmap_icon (struct frame *, Lisp_Object);
322 void x_make_frame_visible (struct frame *);
324 extern void window_scroll (Lisp_Object, int, int, int);
326 /* Defined in macmenu.h. */
327 extern void menubar_selection_callback (FRAME_PTR, int);
328 extern void set_frame_menubar (FRAME_PTR, int, int);
330 /* X display function emulation */
332 static void
333 XFreePixmap (display, pixmap)
334 Display *display;
335 Pixmap pixmap;
337 PixMap *p = (PixMap *) pixmap;
339 xfree (p->baseAddr);
340 xfree (p);
344 /* Set foreground color for subsequent QuickDraw commands. Assume
345 graphic port has already been set. */
347 static void
348 mac_set_forecolor (unsigned long color)
350 RGBColor fg_color;
352 fg_color.red = RED_FROM_ULONG (color) * 256;
353 fg_color.green = GREEN_FROM_ULONG (color) * 256;
354 fg_color.blue = BLUE_FROM_ULONG (color) * 256;
356 RGBForeColor (&fg_color);
360 /* Set background color for subsequent QuickDraw commands. Assume
361 graphic port has already been set. */
363 static void
364 mac_set_backcolor (unsigned long color)
366 RGBColor bg_color;
368 bg_color.red = RED_FROM_ULONG (color) * 256;
369 bg_color.green = GREEN_FROM_ULONG (color) * 256;
370 bg_color.blue = BLUE_FROM_ULONG (color) * 256;
372 RGBBackColor (&bg_color);
375 /* Set foreground and background color for subsequent QuickDraw
376 commands. Assume that the graphic port has already been set. */
378 static void
379 mac_set_colors (GC gc)
381 mac_set_forecolor (gc->foreground);
382 mac_set_backcolor (gc->background);
385 /* Mac version of XDrawLine. */
387 static void
388 XDrawLine (display, w, gc, x1, y1, x2, y2)
389 Display *display;
390 WindowPtr w;
391 GC gc;
392 int x1, y1, x2, y2;
394 #if TARGET_API_MAC_CARBON
395 SetPort (GetWindowPort (w));
396 #else
397 SetPort (w);
398 #endif
400 mac_set_colors (gc);
402 MoveTo (x1, y1);
403 LineTo (x2, y2);
406 /* Mac version of XClearArea. */
408 void
409 XClearArea (display, w, x, y, width, height, exposures)
410 Display *display;
411 WindowPtr w;
412 int x, y;
413 unsigned int width, height;
414 int exposures;
416 struct mac_output *mwp = (mac_output *) GetWRefCon (w);
417 Rect r;
418 XGCValues xgc;
420 xgc.foreground = mwp->x_compatible.foreground_pixel;
421 xgc.background = mwp->x_compatible.background_pixel;
423 #if TARGET_API_MAC_CARBON
424 SetPort (GetWindowPort (w));
425 #else
426 SetPort (w);
427 #endif
429 mac_set_colors (&xgc);
430 SetRect (&r, x, y, x + width, y + height);
432 EraseRect (&r);
435 /* Mac version of XClearWindow. */
437 static void
438 XClearWindow (display, w)
439 Display *display;
440 WindowPtr w;
442 struct mac_output *mwp = (mac_output *) GetWRefCon (w);
443 XGCValues xgc;
445 xgc.foreground = mwp->x_compatible.foreground_pixel;
446 xgc.background = mwp->x_compatible.background_pixel;
448 #if TARGET_API_MAC_CARBON
449 SetPort (GetWindowPort (w));
450 #else
451 SetPort (w);
452 #endif
454 mac_set_colors (&xgc);
456 #if TARGET_API_MAC_CARBON
458 Rect r;
460 GetWindowPortBounds (w, &r);
461 EraseRect (&r);
463 #else /* not TARGET_API_MAC_CARBON */
464 EraseRect (&(w->portRect));
465 #endif /* not TARGET_API_MAC_CARBON */
469 /* Mac replacement for XCopyArea. */
471 static void
472 mac_draw_bitmap (display, w, gc, x, y, bitmap)
473 Display *display;
474 WindowPtr w;
475 GC gc;
476 int x, y;
477 BitMap *bitmap;
479 Rect r;
481 #if TARGET_API_MAC_CARBON
482 SetPort (GetWindowPort (w));
483 #else
484 SetPort (w);
485 #endif
487 mac_set_colors (gc);
488 SetRect (&r, x, y, x + bitmap->bounds.right, y + bitmap->bounds.bottom);
490 #if TARGET_API_MAC_CARBON
492 PixMapHandle pmh;
494 LockPortBits (GetWindowPort (w));
495 pmh = GetPortPixMap (GetWindowPort (w));
496 CopyBits (bitmap, (BitMap *) *pmh, &(bitmap->bounds), &r, srcCopy, 0);
497 UnlockPortBits (GetWindowPort (w));
499 #else /* not TARGET_API_MAC_CARBON */
500 CopyBits (bitmap, &(w->portBits), &(bitmap->bounds), &r, srcCopy, 0);
501 #endif /* not TARGET_API_MAC_CARBON */
505 /* Mac replacement for XSetClipRectangles. */
507 static void
508 mac_set_clip_rectangle (display, w, r)
509 Display *display;
510 WindowPtr w;
511 Rect *r;
513 #if TARGET_API_MAC_CARBON
514 SetPort (GetWindowPort (w));
515 #else
516 SetPort (w);
517 #endif
519 ClipRect (r);
523 /* Mac replacement for XSetClipMask. */
525 static void
526 mac_reset_clipping (display, w)
527 Display *display;
528 WindowPtr w;
530 Rect r;
532 #if TARGET_API_MAC_CARBON
533 SetPort (GetWindowPort (w));
534 #else
535 SetPort (w);
536 #endif
538 SetRect (&r, -32767, -32767, 32767, 32767);
539 ClipRect (&r);
543 /* Mac replacement for XCreateBitmapFromBitmapData. */
545 static void
546 mac_create_bitmap_from_bitmap_data (bitmap, bits, w, h)
547 BitMap *bitmap;
548 char *bits;
549 int w, h;
551 int bytes_per_row, i, j;
553 bitmap->rowBytes = (w + 15) / 16 * 2; /* must be on word boundary */
554 bitmap->baseAddr = xmalloc (bitmap->rowBytes * h);
555 if (!bitmap->baseAddr)
556 abort ();
558 bzero (bitmap->baseAddr, bitmap->rowBytes * h);
559 for (i = 0; i < h; i++)
560 for (j = 0; j < w; j++)
561 if (BitTst (bits, i * w + j))
562 BitSet (bitmap->baseAddr, i * bitmap->rowBytes * 8 + j);
564 SetRect (&(bitmap->bounds), 0, 0, w, h);
568 static void
569 mac_free_bitmap (bitmap)
570 BitMap *bitmap;
572 xfree (bitmap->baseAddr);
575 /* Mac replacement for XFillRectangle. */
577 static void
578 XFillRectangle (display, w, gc, x, y, width, height)
579 Display *display;
580 WindowPtr w;
581 GC gc;
582 int x, y;
583 unsigned int width, height;
585 Rect r;
587 #if TARGET_API_MAC_CARBON
588 SetPort (GetWindowPort (w));
589 #else
590 SetPort (w);
591 #endif
593 mac_set_colors (gc);
594 SetRect (&r, x, y, x + width, y + height);
596 PaintRect (&r); /* using foreground color of gc */
600 /* Mac replacement for XDrawRectangle: dest is a window. */
602 static void
603 mac_draw_rectangle (display, w, gc, x, y, width, height)
604 Display *display;
605 WindowPtr w;
606 GC gc;
607 int x, y;
608 unsigned int width, height;
610 Rect r;
612 #if TARGET_API_MAC_CARBON
613 SetPort (GetWindowPort (w));
614 #else
615 SetPort (w);
616 #endif
618 mac_set_colors (gc);
619 SetRect (&r, x, y, x + width + 1, y + height + 1);
621 FrameRect (&r); /* using foreground color of gc */
625 /* Mac replacement for XDrawRectangle: dest is a Pixmap. */
627 static void
628 mac_draw_rectangle_to_pixmap (display, p, gc, x, y, width, height)
629 Display *display;
630 Pixmap p;
631 GC gc;
632 int x, y;
633 unsigned int width, height;
635 #if 0 /* MAC_TODO: draw a rectangle in a PixMap */
636 Rect r;
638 #if TARGET_API_MAC_CARBON
639 SetPort (GetWindowPort (w));
640 #else
641 SetPort (w);
642 #endif
644 mac_set_colors (gc);
645 SetRect (&r, x, y, x + width, y + height);
647 FrameRect (&r); /* using foreground color of gc */
648 #endif /* 0 */
652 static void
653 mac_draw_string_common (display, w, gc, x, y, buf, nchars, mode,
654 bytes_per_char)
655 Display *display;
656 WindowPtr w;
657 GC gc;
658 int x, y;
659 char *buf;
660 int nchars, mode, bytes_per_char;
662 #if TARGET_API_MAC_CARBON
663 SetPort (GetWindowPort (w));
664 #else
665 SetPort (w);
666 #endif
668 mac_set_colors (gc);
670 TextFont (gc->font->mac_fontnum);
671 TextSize (gc->font->mac_fontsize);
672 TextFace (gc->font->mac_fontface);
673 TextMode (mode);
675 MoveTo (x, y);
676 DrawText (buf, 0, nchars * bytes_per_char);
680 /* Mac replacement for XDrawString. */
682 static void
683 XDrawString (display, w, gc, x, y, buf, nchars)
684 Display *display;
685 WindowPtr w;
686 GC gc;
687 int x, y;
688 char *buf;
689 int nchars;
691 mac_draw_string_common (display, w, gc, x, y, buf, nchars, srcOr, 1);
695 /* Mac replacement for XDrawString16. */
697 static void
698 XDrawString16 (display, w, gc, x, y, buf, nchars)
699 Display *display;
700 WindowPtr w;
701 GC gc;
702 int x, y;
703 XChar2b *buf;
704 int nchars;
706 mac_draw_string_common (display, w, gc, x, y, (char *) buf, nchars, srcOr,
711 /* Mac replacement for XDrawImageString. */
713 static void
714 XDrawImageString (display, w, gc, x, y, buf, nchars)
715 Display *display;
716 WindowPtr w;
717 GC gc;
718 int x, y;
719 char *buf;
720 int nchars;
722 mac_draw_string_common (display, w, gc, x, y, buf, nchars, srcCopy, 1);
726 /* Mac replacement for XDrawString16. */
728 static void
729 XDrawImageString16 (display, w, gc, x, y, buf, nchars)
730 Display *display;
731 WindowPtr w;
732 GC gc;
733 int x, y;
734 XChar2b *buf;
735 int nchars;
737 mac_draw_string_common (display, w, gc, x, y, (char *) buf, nchars, srcCopy,
742 /* Mac replacement for XCopyArea: dest must be window. */
744 static void
745 mac_copy_area (display, src, dest, gc, src_x, src_y, width, height, dest_x,
746 dest_y)
747 Display *display;
748 Pixmap src;
749 WindowPtr dest;
750 GC gc;
751 int src_x, src_y;
752 unsigned int width, height;
753 int dest_x, dest_y;
755 Rect src_r, dest_r;
757 #if TARGET_API_MAC_CARBON
758 SetPort (GetWindowPort (dest));
759 #else
760 SetPort (dest);
761 #endif
763 mac_set_colors (gc);
765 SetRect (&src_r, src_x, src_y, src_x + width, src_y + height);
766 SetRect (&dest_r, dest_x, dest_y, dest_x + width, dest_y + height);
768 #if TARGET_API_MAC_CARBON
770 PixMapHandle pmh;
772 LockPortBits (GetWindowPort (dest));
773 pmh = GetPortPixMap (GetWindowPort (dest));
774 CopyBits ((BitMap *) &src, (BitMap *) *pmh, &src_r, &dest_r, srcCopy, 0);
775 UnlockPortBits (GetWindowPort (dest));
777 #else /* not TARGET_API_MAC_CARBON */
778 CopyBits ((BitMap *) &src, &(dest->portBits), &src_r, &dest_r, srcCopy, 0);
779 #endif /* not TARGET_API_MAC_CARBON */
783 #if 0
784 /* Convert a pair of local coordinates to global (screen) coordinates.
785 Assume graphic port has been properly set. */
786 static void
787 local_to_global_coord (short *h, short *v)
789 Point p;
791 p.h = *h;
792 p.v = *v;
794 LocalToGlobal (&p);
796 *h = p.h;
797 *v = p.v;
799 #endif
801 /* Mac replacement for XCopyArea: used only for scrolling. */
803 static void
804 mac_scroll_area (display, w, gc, src_x, src_y, width, height, dest_x, dest_y)
805 Display *display;
806 WindowPtr w;
807 GC gc;
808 int src_x, src_y;
809 unsigned int width, height;
810 int dest_x, dest_y;
812 #if TARGET_API_MAC_CARBON
813 Rect gw_r, src_r, dest_r;
814 PixMapHandle pmh;
816 SetRect (&src_r, src_x, src_y, src_x + width, src_y + height);
817 SetRect (&dest_r, dest_x, dest_y, dest_x + width, dest_y + height);
819 SetPort (GetWindowPort (w));
821 ForeColor (blackColor);
822 BackColor (whiteColor);
824 LockPortBits (GetWindowPort (w));
825 pmh = GetPortPixMap (GetWindowPort (w));
826 CopyBits ((BitMap *) *pmh, (BitMap *) *pmh, &src_r, &dest_r, srcCopy, 0);
827 UnlockPortBits (GetWindowPort (w));
829 mac_set_colors (gc);
830 #else /* not TARGET_API_MAC_CARBON */
831 Rect src_r, dest_r;
833 SetPort (w);
834 #if 0
835 mac_set_colors (gc);
836 #endif
838 SetRect (&src_r, src_x, src_y, src_x + width, src_y + height);
839 SetRect (&dest_r, dest_x, dest_y, dest_x + width, dest_y + height);
841 #if 0
842 /* Need to use global coordinates and screenBits since src and dest
843 areas overlap in general. */
844 local_to_global_coord (&src_r.left, &src_r.top);
845 local_to_global_coord (&src_r.right, &src_r.bottom);
846 local_to_global_coord (&dest_r.left, &dest_r.top);
847 local_to_global_coord (&dest_r.right, &dest_r.bottom);
849 CopyBits (&qd.screenBits, &qd.screenBits, &src_r, &dest_r, srcCopy, 0);
850 #else
851 /* In Color QuickDraw, set ForeColor and BackColor as follows to avoid
852 color mapping in CopyBits. Otherwise, it will be slow. */
853 ForeColor (blackColor);
854 BackColor (whiteColor);
855 CopyBits (&(w->portBits), &(w->portBits), &src_r, &dest_r, srcCopy, 0);
857 mac_set_colors (gc);
858 #endif
859 #endif /* not TARGET_API_MAC_CARBON */
863 /* Mac replacement for XCopyArea: dest must be Pixmap. */
865 static void
866 mac_copy_area_to_pixmap (display, src, dest, gc, src_x, src_y, width, height,
867 dest_x, dest_y)
868 Display *display;
869 Pixmap src;
870 Pixmap dest;
871 GC gc;
872 int src_x, src_y;
873 unsigned int width, height;
874 int dest_x, dest_y;
876 Rect src_r, dest_r;
877 int src_right = ((PixMap *) src)->bounds.right;
878 int src_bottom = ((PixMap *) src)->bounds.bottom;
879 int w = src_right - src_x;
880 int h = src_bottom - src_y;
882 mac_set_colors (gc);
884 SetRect (&src_r, src_x, src_y, src_right, src_bottom);
885 SetRect (&dest_r, dest_x, dest_y, dest_x + w, dest_y + h);
887 CopyBits ((BitMap *) &src, (BitMap *) &dest, &src_r, &dest_r, srcCopy, 0);
891 /* Mac replacement for XChangeGC. */
893 static void
894 XChangeGC (void * ignore, XGCValues* gc, unsigned long mask,
895 XGCValues *xgcv)
897 if (mask & GCForeground)
898 gc->foreground = xgcv->foreground;
899 if (mask & GCBackground)
900 gc->background = xgcv->background;
901 if (mask & GCFont)
902 gc->font = xgcv->font;
906 /* Mac replacement for XCreateGC. */
908 XGCValues *
909 XCreateGC (void * ignore, Window window, unsigned long mask,
910 XGCValues *xgcv)
912 XGCValues *gc = (XGCValues *) xmalloc (sizeof (XGCValues));
913 bzero (gc, sizeof (XGCValues));
915 XChangeGC (ignore, gc, mask, xgcv);
917 return gc;
921 /* Used in xfaces.c. */
923 void
924 XFreeGC (display, gc)
925 Display *display;
926 GC gc;
928 xfree (gc);
932 /* Mac replacement for XGetGCValues. */
934 static void
935 XGetGCValues (void* ignore, XGCValues *gc,
936 unsigned long mask, XGCValues *xgcv)
938 XChangeGC (ignore, xgcv, mask, gc);
942 /* Mac replacement for XSetForeground. */
944 static void
945 XSetForeground (display, gc, color)
946 Display *display;
947 GC gc;
948 unsigned long color;
950 gc->foreground = color;
954 /* Mac replacement for XSetFont. */
956 static void
957 XSetFont (display, gc, font)
958 Display *display;
959 GC gc;
960 XFontStruct *font;
962 gc->font = font;
966 static void
967 XTextExtents16 (XFontStruct *font, XChar2b *text, int nchars,
968 int *direction,int *font_ascent,
969 int *font_descent, XCharStruct *cs)
971 /* MAC_TODO: Use GetTextMetrics to do this and inline it below. */
975 /* x_sync is a no-op on Mac. */
976 void
977 x_sync (f)
978 void *f;
983 /* Remove calls to XFlush by defining XFlush to an empty replacement.
984 Calls to XFlush should be unnecessary because the X output buffer
985 is flushed automatically as needed by calls to XPending,
986 XNextEvent, or XWindowEvent according to the XFlush man page.
987 XTread_socket calls XPending. Removing XFlush improves
988 performance. */
990 #if TARGET_API_MAC_CARBON
991 #define XFlush(DISPLAY) QDFlushPortBuffer (GetQDGlobalsThePort (), NULL)
992 #else
993 #define XFlush(DISPLAY) (void) 0
994 #endif
996 /* Flush display of frame F, or of all frames if F is null. */
998 void
999 x_flush (f)
1000 struct frame *f;
1002 #if TARGET_API_MAC_CARBON
1003 BLOCK_INPUT;
1004 if (f == NULL)
1006 Lisp_Object rest, frame;
1007 FOR_EACH_FRAME (rest, frame)
1008 x_flush (XFRAME (frame));
1010 else if (FRAME_X_P (f))
1011 XFlush (FRAME_MAC_DISPLAY (f));
1012 UNBLOCK_INPUT;
1013 #endif /* TARGET_API_MAC_CARBON */
1018 /* Return the struct mac_display_info corresponding to DPY. There's
1019 only one. */
1021 struct mac_display_info *
1022 mac_display_info_for_display (dpy)
1023 Display *dpy;
1025 return &one_mac_display_info;
1030 /***********************************************************************
1031 Starting and ending an update
1032 ***********************************************************************/
1034 /* Start an update of frame F. This function is installed as a hook
1035 for update_begin, i.e. it is called when update_begin is called.
1036 This function is called prior to calls to x_update_window_begin for
1037 each window being updated. */
1039 static void
1040 x_update_begin (f)
1041 struct frame *f;
1043 /* Nothing to do. */
1047 /* Start update of window W. Set the global variable updated_window
1048 to the window being updated and set output_cursor to the cursor
1049 position of W. */
1051 static void
1052 x_update_window_begin (w)
1053 struct window *w;
1055 struct frame *f = XFRAME (WINDOW_FRAME (w));
1056 struct mac_display_info *display_info = FRAME_MAC_DISPLAY_INFO (f);
1058 updated_window = w;
1059 set_output_cursor (&w->cursor);
1061 BLOCK_INPUT;
1063 if (f == display_info->mouse_face_mouse_frame)
1065 /* Don't do highlighting for mouse motion during the update. */
1066 display_info->mouse_face_defer = 1;
1068 /* If F needs to be redrawn, simply forget about any prior mouse
1069 highlighting. */
1070 if (FRAME_GARBAGED_P (f))
1071 display_info->mouse_face_window = Qnil;
1073 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
1074 their mouse_face_p flag set, which means that they are always
1075 unequal to rows in a desired matrix which never have that
1076 flag set. So, rows containing mouse-face glyphs are never
1077 scrolled, and we don't have to switch the mouse highlight off
1078 here to prevent it from being scrolled. */
1080 /* Can we tell that this update does not affect the window
1081 where the mouse highlight is? If so, no need to turn off.
1082 Likewise, don't do anything if the frame is garbaged;
1083 in that case, the frame's current matrix that we would use
1084 is all wrong, and we will redisplay that line anyway. */
1085 if (!NILP (display_info->mouse_face_window)
1086 && w == XWINDOW (display_info->mouse_face_window))
1088 int i;
1090 for (i = 0; i < w->desired_matrix->nrows; ++i)
1091 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
1092 break;
1094 if (i < w->desired_matrix->nrows)
1095 clear_mouse_face (display_info);
1097 #endif /* 0 */
1100 UNBLOCK_INPUT;
1104 /* Draw a vertical window border from (x,y0) to (x,y1) */
1106 static void
1107 mac_draw_vertical_window_border (w, x, y0, y1)
1108 struct window *w;
1109 int x, y0, y1;
1111 struct frame *f = XFRAME (WINDOW_FRAME (w));
1113 XDrawLine (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f),
1114 f->output_data.mac->normal_gc, x, y0, x, y1);
1118 /* End update of window W (which is equal to updated_window).
1120 Draw vertical borders between horizontally adjacent windows, and
1121 display W's cursor if CURSOR_ON_P is non-zero.
1123 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
1124 glyphs in mouse-face were overwritten. In that case we have to
1125 make sure that the mouse-highlight is properly redrawn.
1127 W may be a menu bar pseudo-window in case we don't have X toolkit
1128 support. Such windows don't have a cursor, so don't display it
1129 here. */
1131 static void
1132 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
1133 struct window *w;
1134 int cursor_on_p, mouse_face_overwritten_p;
1136 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (XFRAME (w->frame));
1138 if (!w->pseudo_window_p)
1140 BLOCK_INPUT;
1142 if (cursor_on_p)
1143 display_and_set_cursor (w, 1, output_cursor.hpos,
1144 output_cursor.vpos,
1145 output_cursor.x, output_cursor.y);
1147 x_draw_vertical_border (w);
1148 UNBLOCK_INPUT;
1151 /* If a row with mouse-face was overwritten, arrange for
1152 XTframe_up_to_date to redisplay the mouse highlight. */
1153 if (mouse_face_overwritten_p)
1155 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
1156 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
1157 dpyinfo->mouse_face_window = Qnil;
1160 #if 0
1161 /* Unhide the caret. This won't actually show the cursor, unless it
1162 was visible before the corresponding call to HideCaret in
1163 x_update_window_begin. */
1164 if (w32_use_visible_system_caret)
1165 SendMessage (w32_system_caret_hwnd, WM_EMACS_SHOW_CARET, 0, 0);
1166 #endif
1168 updated_window = NULL;
1172 /* End update of frame F. This function is installed as a hook in
1173 update_end. */
1175 static void
1176 x_update_end (f)
1177 struct frame *f;
1179 /* Reset the background color of Mac OS Window to that of the frame after
1180 update so that it is used by Mac Toolbox to clear the update region before
1181 an update event is generated. */
1182 #if TARGET_API_MAC_CARBON
1183 SetPort (GetWindowPort (FRAME_MAC_WINDOW (f)));
1184 #else
1185 SetPort (FRAME_MAC_WINDOW (f));
1186 #endif
1188 mac_set_backcolor (FRAME_BACKGROUND_PIXEL (f));
1190 /* Mouse highlight may be displayed again. */
1191 FRAME_MAC_DISPLAY_INFO (f)->mouse_face_defer = 0;
1193 BLOCK_INPUT;
1194 XFlush (FRAME_MAC_DISPLAY (f));
1195 UNBLOCK_INPUT;
1199 /* This function is called from various places in xdisp.c whenever a
1200 complete update has been performed. The global variable
1201 updated_window is not available here. */
1203 static void
1204 XTframe_up_to_date (f)
1205 struct frame *f;
1207 if (FRAME_X_P (f))
1209 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
1211 if (dpyinfo->mouse_face_deferred_gc
1212 || f == dpyinfo->mouse_face_mouse_frame)
1214 BLOCK_INPUT;
1215 if (dpyinfo->mouse_face_mouse_frame)
1216 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
1217 dpyinfo->mouse_face_mouse_x,
1218 dpyinfo->mouse_face_mouse_y);
1219 dpyinfo->mouse_face_deferred_gc = 0;
1220 UNBLOCK_INPUT;
1226 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
1227 arrow bitmaps, or clear the fringes if no bitmaps are required
1228 before DESIRED_ROW is made current. The window being updated is
1229 found in updated_window. This function is called from
1230 update_window_line only if it is known that there are differences
1231 between bitmaps to be drawn between current row and DESIRED_ROW. */
1233 static void
1234 x_after_update_window_line (desired_row)
1235 struct glyph_row *desired_row;
1237 struct window *w = updated_window;
1238 struct frame *f;
1239 int width, height;
1241 xassert (w);
1243 if (!desired_row->mode_line_p && !w->pseudo_window_p)
1245 BLOCK_INPUT;
1246 draw_row_fringe_bitmaps (w, desired_row);
1247 UNBLOCK_INPUT;
1250 /* When a window has disappeared, make sure that no rest of
1251 full-width rows stays visible in the internal border. Could
1252 check here if updated_window is the leftmost/rightmost window,
1253 but I guess it's not worth doing since vertically split windows
1254 are almost never used, internal border is rarely set, and the
1255 overhead is very small. */
1256 if (windows_or_buffers_changed
1257 && desired_row->full_width_p
1258 && (f = XFRAME (w->frame),
1259 width = FRAME_INTERNAL_BORDER_WIDTH (f),
1260 width != 0)
1261 && (height = desired_row->visible_height,
1262 height > 0))
1264 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
1265 /* Internal border is drawn below the tool bar. */
1266 if (WINDOWP (f->tool_bar_window)
1267 && w == XWINDOW (f->tool_bar_window))
1268 y -= width;
1270 BLOCK_INPUT;
1272 XClearArea (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f),
1273 0, y, width, height, 0);
1274 XClearArea (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f),
1275 FRAME_PIXEL_WIDTH (f) - width, y,
1276 width, height, 0);
1278 UNBLOCK_INPUT;
1283 /* Draw the bitmap WHICH in one of the left or right fringes of
1284 window W. ROW is the glyph row for which to display the bitmap; it
1285 determines the vertical position at which the bitmap has to be
1286 drawn. */
1288 static void
1289 x_draw_fringe_bitmap (w, row, p)
1290 struct window *w;
1291 struct glyph_row *row;
1292 struct draw_fringe_bitmap_params *p;
1294 struct frame *f = XFRAME (WINDOW_FRAME (w));
1295 Display *display = FRAME_MAC_DISPLAY (f);
1296 WindowPtr window = FRAME_MAC_WINDOW (f);
1297 XGCValues gcv;
1298 GC gc = f->output_data.mac->normal_gc;
1299 struct face *face = p->face;
1301 /* Must clip because of partially visible lines. */
1302 x_clip_to_row (w, row, gc);
1304 if (p->bx >= 0)
1306 XGCValues gcv;
1307 gcv.foreground = face->background;
1309 #if 0 /* MAC_TODO: stipple */
1310 /* In case the same realized face is used for fringes and
1311 for something displayed in the text (e.g. face `region' on
1312 mono-displays, the fill style may have been changed to
1313 FillSolid in x_draw_glyph_string_background. */
1314 if (face->stipple)
1315 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
1316 else
1317 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
1318 #endif
1320 XFillRectangle (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f),
1321 &gcv,
1322 p->bx, p->by, p->nx, p->ny);
1324 #if 0 /* MAC_TODO: stipple */
1325 if (!face->stipple)
1326 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
1327 #endif
1330 if (p->which != NO_FRINGE_BITMAP)
1332 unsigned char *bits = fringe_bitmaps[p->which].bits + p->dh;
1333 BitMap bitmap;
1335 mac_create_bitmap_from_bitmap_data (&bitmap, bits, p->wd, p->h);
1336 gcv.foreground = face->foreground;
1337 gcv.background = face->background;
1339 mac_draw_bitmap (display, window, &gcv, p->x, p->y, &bitmap);
1341 mac_free_bitmap (&bitmap);
1344 mac_reset_clipping (display, window);
1348 /* This is called when starting Emacs and when restarting after
1349 suspend. When starting Emacs, no window is mapped. And nothing
1350 must be done to Emacs's own window if it is suspended (though that
1351 rarely happens). */
1353 static void
1354 XTset_terminal_modes ()
1358 /* This is called when exiting or suspending Emacs. Exiting will make
1359 the windows go away, and suspending requires no action. */
1361 static void
1362 XTreset_terminal_modes ()
1367 /***********************************************************************
1368 Display Iterator
1369 ***********************************************************************/
1371 /* Function prototypes of this page. */
1373 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1374 static int mac_encode_char P_ ((int, XChar2b *, struct font_info *, int *));
1377 /* Return a pointer to per-char metric information in FONT of a
1378 character pointed by B which is a pointer to an XChar2b. */
1380 #define PER_CHAR_METRIC(font, b) \
1381 ((font)->per_char \
1382 ? ((font)->per_char + (b)->byte2 - (font)->min_char_or_byte2 \
1383 + (((font)->min_byte1 || (font)->max_byte1) \
1384 ? (((b)->byte1 - (font)->min_byte1) \
1385 * ((font)->max_char_or_byte2 - (font)->min_char_or_byte2 + 1)) \
1386 : 0)) \
1387 : &((font)->max_bounds))
1390 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1391 is not contained in the font. */
1393 static INLINE XCharStruct *
1394 x_per_char_metric (font, char2b)
1395 XFontStruct *font;
1396 XChar2b *char2b;
1398 /* The result metric information. */
1399 XCharStruct *pcm = NULL;
1401 xassert (font && char2b);
1403 if (font->per_char != NULL)
1405 if (font->min_byte1 == 0 && font->max_byte1 == 0)
1407 /* min_char_or_byte2 specifies the linear character index
1408 corresponding to the first element of the per_char array,
1409 max_char_or_byte2 is the index of the last character. A
1410 character with non-zero CHAR2B->byte1 is not in the font.
1411 A character with byte2 less than min_char_or_byte2 or
1412 greater max_char_or_byte2 is not in the font. */
1413 if (char2b->byte1 == 0
1414 && char2b->byte2 >= font->min_char_or_byte2
1415 && char2b->byte2 <= font->max_char_or_byte2)
1416 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
1418 else
1420 /* If either min_byte1 or max_byte1 are nonzero, both
1421 min_char_or_byte2 and max_char_or_byte2 are less than
1422 256, and the 2-byte character index values corresponding
1423 to the per_char array element N (counting from 0) are:
1425 byte1 = N/D + min_byte1
1426 byte2 = N\D + min_char_or_byte2
1428 where:
1430 D = max_char_or_byte2 - min_char_or_byte2 + 1
1431 / = integer division
1432 \ = integer modulus */
1433 if (char2b->byte1 >= font->min_byte1
1434 && char2b->byte1 <= font->max_byte1
1435 && char2b->byte2 >= font->min_char_or_byte2
1436 && char2b->byte2 <= font->max_char_or_byte2)
1438 pcm = (font->per_char
1439 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1440 * (char2b->byte1 - font->min_byte1))
1441 + (char2b->byte2 - font->min_char_or_byte2));
1445 else
1447 /* If the per_char pointer is null, all glyphs between the first
1448 and last character indexes inclusive have the same
1449 information, as given by both min_bounds and max_bounds. */
1450 if (char2b->byte2 >= font->min_char_or_byte2
1451 && char2b->byte2 <= font->max_char_or_byte2)
1452 pcm = &font->max_bounds;
1455 return ((pcm == NULL
1456 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
1457 ? NULL : pcm);
1460 /* RIF:
1463 static XCharStruct *
1464 mac_per_char_metric (font, char2b, font_type)
1465 XFontStruct *font;
1466 XChar2b *char2b;
1467 int font_type;
1469 return x_per_char_metric (font, char2b);
1472 /* RIF:
1473 Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1474 the two-byte form of C. Encoding is returned in *CHAR2B. */
1476 static int
1477 mac_encode_char (c, char2b, font_info, two_byte_p)
1478 int c;
1479 XChar2b *char2b;
1480 struct font_info *font_info;
1481 int *two_byte_p;
1483 int charset = CHAR_CHARSET (c);
1484 XFontStruct *font = font_info->font;
1486 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1487 This may be either a program in a special encoder language or a
1488 fixed encoding. */
1489 if (font_info->font_encoder)
1491 /* It's a program. */
1492 struct ccl_program *ccl = font_info->font_encoder;
1494 if (CHARSET_DIMENSION (charset) == 1)
1496 ccl->reg[0] = charset;
1497 ccl->reg[1] = char2b->byte2;
1499 else
1501 ccl->reg[0] = charset;
1502 ccl->reg[1] = char2b->byte1;
1503 ccl->reg[2] = char2b->byte2;
1506 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1508 /* We assume that MSBs are appropriately set/reset by CCL
1509 program. */
1510 if (font->max_byte1 == 0) /* 1-byte font */
1511 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
1512 else
1513 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1515 else if (font_info->encoding[charset])
1517 /* Fixed encoding scheme. See fontset.h for the meaning of the
1518 encoding numbers. */
1519 int enc = font_info->encoding[charset];
1521 if ((enc == 1 || enc == 2)
1522 && CHARSET_DIMENSION (charset) == 2)
1523 char2b->byte1 |= 0x80;
1525 if (enc == 1 || enc == 3)
1526 char2b->byte2 |= 0x80;
1528 if (enc == 4)
1530 int sjis1, sjis2;
1532 ENCODE_SJIS (char2b->byte1, char2b->byte2, sjis1, sjis2);
1533 char2b->byte1 = sjis1;
1534 char2b->byte2 = sjis2;
1538 if (two_byte_p)
1539 *two_byte_p = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1541 return FONT_TYPE_UNKNOWN;
1546 /***********************************************************************
1547 Glyph display
1548 ***********************************************************************/
1551 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
1552 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
1553 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
1554 int));
1555 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
1556 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
1557 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
1558 static void x_draw_glyph_string P_ ((struct glyph_string *));
1559 static void x_set_cursor_gc P_ ((struct glyph_string *));
1560 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
1561 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
1562 /*static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
1563 unsigned long *, double, int));*/
1564 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
1565 double, int, unsigned long));
1566 static void x_setup_relief_colors P_ ((struct glyph_string *));
1567 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
1568 static void x_draw_image_relief P_ ((struct glyph_string *));
1569 static void x_draw_image_foreground P_ ((struct glyph_string *));
1570 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
1571 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
1572 int, int, int));
1573 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
1574 int, int, int, int, Rect *));
1575 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
1576 int, int, int, Rect *));
1578 #if GLYPH_DEBUG
1579 static void x_check_font P_ ((struct frame *, XFontStruct *));
1580 #endif
1583 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
1584 face. */
1586 static void
1587 x_set_cursor_gc (s)
1588 struct glyph_string *s;
1590 if (s->font == FRAME_FONT (s->f)
1591 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
1592 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
1593 && !s->cmp)
1594 s->gc = s->f->output_data.mac->cursor_gc;
1595 else
1597 /* Cursor on non-default face: must merge. */
1598 XGCValues xgcv;
1599 unsigned long mask;
1601 xgcv.background = s->f->output_data.mac->cursor_pixel;
1602 xgcv.foreground = s->face->background;
1604 /* If the glyph would be invisible, try a different foreground. */
1605 if (xgcv.foreground == xgcv.background)
1606 xgcv.foreground = s->face->foreground;
1607 if (xgcv.foreground == xgcv.background)
1608 xgcv.foreground = s->f->output_data.mac->cursor_foreground_pixel;
1609 if (xgcv.foreground == xgcv.background)
1610 xgcv.foreground = s->face->foreground;
1612 /* Make sure the cursor is distinct from text in this face. */
1613 if (xgcv.background == s->face->background
1614 && xgcv.foreground == s->face->foreground)
1616 xgcv.background = s->face->foreground;
1617 xgcv.foreground = s->face->background;
1620 IF_DEBUG (x_check_font (s->f, s->font));
1621 xgcv.font = s->font;
1622 mask = GCForeground | GCBackground | GCFont;
1624 if (FRAME_MAC_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1625 XChangeGC (s->display, FRAME_MAC_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1626 mask, &xgcv);
1627 else
1628 FRAME_MAC_DISPLAY_INFO (s->f)->scratch_cursor_gc
1629 = XCreateGC (s->display, s->window, mask, &xgcv);
1631 s->gc = FRAME_MAC_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1636 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1638 static void
1639 x_set_mouse_face_gc (s)
1640 struct glyph_string *s;
1642 int face_id;
1643 struct face *face;
1645 /* What face has to be used last for the mouse face? */
1646 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
1647 face = FACE_FROM_ID (s->f, face_id);
1648 if (face == NULL)
1649 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1651 if (s->first_glyph->type == CHAR_GLYPH)
1652 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
1653 else
1654 face_id = FACE_FOR_CHAR (s->f, face, 0);
1655 s->face = FACE_FROM_ID (s->f, face_id);
1656 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1658 /* If font in this face is same as S->font, use it. */
1659 if (s->font == s->face->font)
1660 s->gc = s->face->gc;
1661 else
1663 /* Otherwise construct scratch_cursor_gc with values from FACE
1664 but font FONT. */
1665 XGCValues xgcv;
1666 unsigned long mask;
1668 xgcv.background = s->face->background;
1669 xgcv.foreground = s->face->foreground;
1670 IF_DEBUG (x_check_font (s->f, s->font));
1671 xgcv.font = s->font;
1672 mask = GCForeground | GCBackground | GCFont;
1674 if (FRAME_MAC_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1675 XChangeGC (s->display, FRAME_MAC_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1676 mask, &xgcv);
1677 else
1678 FRAME_MAC_DISPLAY_INFO (s->f)->scratch_cursor_gc
1679 = XCreateGC (s->display, s->window, mask, &xgcv);
1681 s->gc = FRAME_MAC_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1684 xassert (s->gc != 0);
1688 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1689 Faces to use in the mode line have already been computed when the
1690 matrix was built, so there isn't much to do, here. */
1692 static INLINE void
1693 x_set_mode_line_face_gc (s)
1694 struct glyph_string *s;
1696 s->gc = s->face->gc;
1700 /* Set S->gc of glyph string S for drawing that glyph string. Set
1701 S->stippled_p to a non-zero value if the face of S has a stipple
1702 pattern. */
1704 static INLINE void
1705 x_set_glyph_string_gc (s)
1706 struct glyph_string *s;
1708 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1710 if (s->hl == DRAW_NORMAL_TEXT)
1712 s->gc = s->face->gc;
1713 s->stippled_p = s->face->stipple != 0;
1715 else if (s->hl == DRAW_INVERSE_VIDEO)
1717 x_set_mode_line_face_gc (s);
1718 s->stippled_p = s->face->stipple != 0;
1720 else if (s->hl == DRAW_CURSOR)
1722 x_set_cursor_gc (s);
1723 s->stippled_p = 0;
1725 else if (s->hl == DRAW_MOUSE_FACE)
1727 x_set_mouse_face_gc (s);
1728 s->stippled_p = s->face->stipple != 0;
1730 else if (s->hl == DRAW_IMAGE_RAISED
1731 || s->hl == DRAW_IMAGE_SUNKEN)
1733 s->gc = s->face->gc;
1734 s->stippled_p = s->face->stipple != 0;
1736 else
1738 s->gc = s->face->gc;
1739 s->stippled_p = s->face->stipple != 0;
1742 /* GC must have been set. */
1743 xassert (s->gc != 0);
1747 /* Set clipping for output of glyph string S. S may be part of a mode
1748 line or menu if we don't have X toolkit support. */
1750 static INLINE void
1751 x_set_glyph_string_clipping (s)
1752 struct glyph_string *s;
1754 Rect r;
1755 get_glyph_string_clip_rect (s, &r);
1756 mac_set_clip_rectangle (s->display, s->window, &r);
1760 /* RIF:
1761 Compute left and right overhang of glyph string S. If S is a glyph
1762 string for a composition, assume overhangs don't exist. */
1764 static void
1765 mac_compute_glyph_string_overhangs (s)
1766 struct glyph_string *s;
1768 #if 0
1769 /* MAC_TODO: XTextExtents16 does nothing yet... */
1771 if (s->cmp == NULL
1772 && s->first_glyph->type == CHAR_GLYPH)
1774 XCharStruct cs;
1775 int direction, font_ascent, font_descent;
1776 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
1777 &font_ascent, &font_descent, &cs);
1778 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
1779 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
1781 #endif
1785 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1787 static INLINE void
1788 x_clear_glyph_string_rect (s, x, y, w, h)
1789 struct glyph_string *s;
1790 int x, y, w, h;
1792 XGCValues xgcv;
1794 xgcv.foreground = s->gc->background;
1795 XFillRectangle (s->display, s->window, &xgcv, x, y, w, h);
1799 /* Draw the background of glyph_string S. If S->background_filled_p
1800 is non-zero don't draw it. FORCE_P non-zero means draw the
1801 background even if it wouldn't be drawn normally. This is used
1802 when a string preceding S draws into the background of S, or S
1803 contains the first component of a composition. */
1805 static void
1806 x_draw_glyph_string_background (s, force_p)
1807 struct glyph_string *s;
1808 int force_p;
1810 /* Nothing to do if background has already been drawn or if it
1811 shouldn't be drawn in the first place. */
1812 if (!s->background_filled_p)
1814 int box_line_width = max (s->face->box_line_width, 0);
1816 #if 0 /* MAC_TODO: stipple */
1817 if (s->stippled_p)
1819 /* Fill background with a stipple pattern. */
1820 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1821 XFillRectangle (s->display, s->window, s->gc, s->x,
1822 s->y + box_line_width,
1823 s->background_width,
1824 s->height - 2 * box_line_width);
1825 XSetFillStyle (s->display, s->gc, FillSolid);
1826 s->background_filled_p = 1;
1828 else
1829 #endif
1830 if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1831 || s->font_not_found_p
1832 || s->extends_to_end_of_line_p
1833 || force_p)
1835 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1836 s->background_width,
1837 s->height - 2 * box_line_width);
1838 s->background_filled_p = 1;
1844 /* Draw the foreground of glyph string S. */
1846 static void
1847 x_draw_glyph_string_foreground (s)
1848 struct glyph_string *s;
1850 int i, x;
1852 /* If first glyph of S has a left box line, start drawing the text
1853 of S to the right of that box line. */
1854 if (s->face->box != FACE_NO_BOX
1855 && s->first_glyph->left_box_line_p)
1856 x = s->x + abs (s->face->box_line_width);
1857 else
1858 x = s->x;
1860 /* Draw characters of S as rectangles if S's font could not be
1861 loaded. */
1862 if (s->font_not_found_p)
1864 for (i = 0; i < s->nchars; ++i)
1866 struct glyph *g = s->first_glyph + i;
1867 mac_draw_rectangle (s->display, s->window,
1868 s->gc, x, s->y, g->pixel_width - 1,
1869 s->height - 1);
1870 x += g->pixel_width;
1873 else
1875 char *char1b = (char *) s->char2b;
1876 int boff = s->font_info->baseline_offset;
1878 if (s->font_info->vertical_centering)
1879 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
1881 /* If we can use 8-bit functions, condense S->char2b. */
1882 if (!s->two_byte_p)
1883 for (i = 0; i < s->nchars; ++i)
1884 char1b[i] = s->char2b[i].byte2;
1886 /* Draw text with XDrawString if background has already been
1887 filled. Otherwise, use XDrawImageString. (Note that
1888 XDrawImageString is usually faster than XDrawString.) Always
1889 use XDrawImageString when drawing the cursor so that there is
1890 no chance that characters under a box cursor are invisible. */
1891 if (s->for_overlaps_p
1892 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1894 /* Draw characters with 16-bit or 8-bit functions. */
1895 if (s->two_byte_p)
1896 XDrawString16 (s->display, s->window, s->gc, x,
1897 s->ybase - boff, s->char2b, s->nchars);
1898 else
1899 XDrawString (s->display, s->window, s->gc, x,
1900 s->ybase - boff, char1b, s->nchars);
1902 else
1904 if (s->two_byte_p)
1905 XDrawImageString16 (s->display, s->window, s->gc, x,
1906 s->ybase - boff, s->char2b, s->nchars);
1907 else
1908 XDrawImageString (s->display, s->window, s->gc, x,
1909 s->ybase - boff, char1b, s->nchars);
1914 /* Draw the foreground of composite glyph string S. */
1916 static void
1917 x_draw_composite_glyph_string_foreground (s)
1918 struct glyph_string *s;
1920 int i, x;
1922 /* If first glyph of S has a left box line, start drawing the text
1923 of S to the right of that box line. */
1924 if (s->face->box != FACE_NO_BOX
1925 && s->first_glyph->left_box_line_p)
1926 x = s->x + abs (s->face->box_line_width);
1927 else
1928 x = s->x;
1930 /* S is a glyph string for a composition. S->gidx is the index of
1931 the first character drawn for glyphs of this composition.
1932 S->gidx == 0 means we are drawing the very first character of
1933 this composition. */
1935 /* Draw a rectangle for the composition if the font for the very
1936 first character of the composition could not be loaded. */
1937 if (s->font_not_found_p)
1939 if (s->gidx == 0)
1940 mac_draw_rectangle (s->display, s->window, s->gc, x, s->y,
1941 s->width - 1, s->height - 1);
1943 else
1945 for (i = 0; i < s->nchars; i++, ++s->gidx)
1946 XDrawString16 (s->display, s->window, s->gc,
1947 x + s->cmp->offsets[s->gidx * 2],
1948 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
1949 s->char2b + i, 1);
1954 #ifdef USE_X_TOOLKIT
1956 static struct frame *x_frame_of_widget P_ ((Widget));
1959 /* Return the frame on which widget WIDGET is used.. Abort if frame
1960 cannot be determined. */
1962 static struct frame *
1963 x_frame_of_widget (widget)
1964 Widget widget;
1966 struct x_display_info *dpyinfo;
1967 Lisp_Object tail;
1968 struct frame *f;
1970 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1972 /* Find the top-level shell of the widget. Note that this function
1973 can be called when the widget is not yet realized, so XtWindow
1974 (widget) == 0. That's the reason we can't simply use
1975 x_any_window_to_frame. */
1976 while (!XtIsTopLevelShell (widget))
1977 widget = XtParent (widget);
1979 /* Look for a frame with that top-level widget. Allocate the color
1980 on that frame to get the right gamma correction value. */
1981 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
1982 if (GC_FRAMEP (XCAR (tail))
1983 && (f = XFRAME (XCAR (tail)),
1984 (f->output_data.nothing != 1
1985 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
1986 && f->output_data.x->widget == widget)
1987 return f;
1989 abort ();
1993 /* Allocate the color COLOR->pixel on the screen and display of
1994 widget WIDGET in colormap CMAP. If an exact match cannot be
1995 allocated, try the nearest color available. Value is non-zero
1996 if successful. This is called from lwlib. */
1999 x_alloc_nearest_color_for_widget (widget, cmap, color)
2000 Widget widget;
2001 Colormap cmap;
2002 XColor *color;
2004 struct frame *f = x_frame_of_widget (widget);
2005 return x_alloc_nearest_color (f, cmap, color);
2009 #endif /* USE_X_TOOLKIT */
2011 #if 0 /* MAC_TODO */
2013 /* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
2014 CMAP. If an exact match can't be allocated, try the nearest color
2015 available. Value is non-zero if successful. Set *COLOR to the
2016 color allocated. */
2019 x_alloc_nearest_color (f, cmap, color)
2020 struct frame *f;
2021 Colormap cmap;
2022 XColor *color;
2024 Display *display = FRAME_X_DISPLAY (f);
2025 Screen *screen = FRAME_X_SCREEN (f);
2026 int rc;
2028 gamma_correct (f, color);
2029 rc = XAllocColor (display, cmap, color);
2030 if (rc == 0)
2032 /* If we got to this point, the colormap is full, so we're going
2033 to try to get the next closest color. The algorithm used is
2034 a least-squares matching, which is what X uses for closest
2035 color matching with StaticColor visuals. */
2036 int nearest, i;
2037 unsigned long nearest_delta = ~0;
2038 int ncells = XDisplayCells (display, XScreenNumberOfScreen (screen));
2039 XColor *cells = (XColor *) alloca (ncells * sizeof *cells);
2041 for (i = 0; i < ncells; ++i)
2042 cells[i].pixel = i;
2043 XQueryColors (display, cmap, cells, ncells);
2045 for (nearest = i = 0; i < ncells; ++i)
2047 long dred = (color->red >> 8) - (cells[i].red >> 8);
2048 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
2049 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
2050 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
2052 if (delta < nearest_delta)
2054 nearest = i;
2055 nearest_delta = delta;
2059 color->red = cells[nearest].red;
2060 color->green = cells[nearest].green;
2061 color->blue = cells[nearest].blue;
2062 rc = XAllocColor (display, cmap, color);
2065 #ifdef DEBUG_X_COLORS
2066 if (rc)
2067 register_color (color->pixel);
2068 #endif /* DEBUG_X_COLORS */
2070 return rc;
2074 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
2075 It's necessary to do this instead of just using PIXEL directly to
2076 get color reference counts right. */
2078 unsigned long
2079 x_copy_color (f, pixel)
2080 struct frame *f;
2081 unsigned long pixel;
2083 XColor color;
2085 color.pixel = pixel;
2086 BLOCK_INPUT;
2087 XQueryColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
2088 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
2089 UNBLOCK_INPUT;
2090 #ifdef DEBUG_X_COLORS
2091 register_color (pixel);
2092 #endif
2093 return color.pixel;
2097 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
2098 It's necessary to do this instead of just using PIXEL directly to
2099 get color reference counts right. */
2101 unsigned long
2102 x_copy_dpy_color (dpy, cmap, pixel)
2103 Display *dpy;
2104 Colormap cmap;
2105 unsigned long pixel;
2107 XColor color;
2109 color.pixel = pixel;
2110 BLOCK_INPUT;
2111 XQueryColor (dpy, cmap, &color);
2112 XAllocColor (dpy, cmap, &color);
2113 UNBLOCK_INPUT;
2114 #ifdef DEBUG_X_COLORS
2115 register_color (pixel);
2116 #endif
2117 return color.pixel;
2120 #endif /* MAC_TODO */
2122 /* Allocate a color which is lighter or darker than *COLOR by FACTOR
2123 or DELTA. Try a color with RGB values multiplied by FACTOR first.
2124 If this produces the same color as COLOR, try a color where all RGB
2125 values have DELTA added. Return the allocated color in *COLOR.
2126 DISPLAY is the X display, CMAP is the colormap to operate on.
2127 Value is non-zero if successful. */
2129 static int
2130 mac_alloc_lighter_color (f, color, factor, delta)
2131 struct frame *f;
2132 unsigned long *color;
2133 double factor;
2134 int delta;
2136 unsigned long new;
2138 /* Change RGB values by specified FACTOR. Avoid overflow! */
2139 xassert (factor >= 0);
2140 new = RGB_TO_ULONG (min (0xff, (int) (factor * RED_FROM_ULONG (*color))),
2141 min (0xff, (int) (factor * GREEN_FROM_ULONG (*color))),
2142 min (0xff, (int) (factor * BLUE_FROM_ULONG (*color))));
2143 if (new == *color)
2144 new = RGB_TO_ULONG (max (0, min (0xff, (int) (delta + RED_FROM_ULONG (*color)))),
2145 max (0, min (0xff, (int) (delta + GREEN_FROM_ULONG (*color)))),
2146 max (0, min (0xff, (int) (delta + BLUE_FROM_ULONG (*color)))));
2148 /* MAC_TODO: Map to palette and retry with delta if same? */
2149 /* MAC_TODO: Free colors (if using palette)? */
2151 if (new == *color)
2152 return 0;
2154 *color = new;
2156 return 1;
2160 /* Set up the foreground color for drawing relief lines of glyph
2161 string S. RELIEF is a pointer to a struct relief containing the GC
2162 with which lines will be drawn. Use a color that is FACTOR or
2163 DELTA lighter or darker than the relief's background which is found
2164 in S->f->output_data.x->relief_background. If such a color cannot
2165 be allocated, use DEFAULT_PIXEL, instead. */
2167 static void
2168 x_setup_relief_color (f, relief, factor, delta, default_pixel)
2169 struct frame *f;
2170 struct relief *relief;
2171 double factor;
2172 int delta;
2173 unsigned long default_pixel;
2175 XGCValues xgcv;
2176 struct mac_output *di = f->output_data.mac;
2177 unsigned long mask = GCForeground;
2178 unsigned long pixel;
2179 unsigned long background = di->relief_background;
2180 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
2182 /* MAC_TODO: Free colors (if using palette)? */
2184 /* Allocate new color. */
2185 xgcv.foreground = default_pixel;
2186 pixel = background;
2187 if (mac_alloc_lighter_color (f, &pixel, factor, delta))
2189 relief->allocated_p = 1;
2190 xgcv.foreground = relief->pixel = pixel;
2193 if (relief->gc == 0)
2195 #if 0 /* MAC_TODO: stipple */
2196 xgcv.stipple = dpyinfo->gray;
2197 mask |= GCStipple;
2198 #endif
2199 relief->gc = XCreateGC (NULL, FRAME_MAC_WINDOW (f), mask, &xgcv);
2201 else
2202 XChangeGC (NULL, relief->gc, mask, &xgcv);
2206 /* Set up colors for the relief lines around glyph string S. */
2208 static void
2209 x_setup_relief_colors (s)
2210 struct glyph_string *s;
2212 struct mac_output *di = s->f->output_data.mac;
2213 unsigned long color;
2215 if (s->face->use_box_color_for_shadows_p)
2216 color = s->face->box_color;
2217 else
2219 XGCValues xgcv;
2221 /* Get the background color of the face. */
2222 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2223 color = xgcv.background;
2226 if (di->white_relief.gc == 0
2227 || color != di->relief_background)
2229 di->relief_background = color;
2230 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2231 WHITE_PIX_DEFAULT (s->f));
2232 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2233 BLACK_PIX_DEFAULT (s->f));
2238 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2239 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2240 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
2241 relief. LEFT_P non-zero means draw a relief on the left side of
2242 the rectangle. RIGHT_P non-zero means draw a relief on the right
2243 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2244 when drawing. */
2246 static void
2247 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
2248 raised_p, left_p, right_p, clip_rect)
2249 struct frame *f;
2250 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
2251 Rect *clip_rect;
2253 int i;
2254 GC gc;
2256 if (raised_p)
2257 gc = f->output_data.mac->white_relief.gc;
2258 else
2259 gc = f->output_data.mac->black_relief.gc;
2260 mac_set_clip_rectangle (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f), clip_rect);
2262 /* Top. */
2263 for (i = 0; i < width; ++i)
2264 XDrawLine (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f), gc,
2265 left_x + i * left_p, top_y + i,
2266 right_x + 1 - i * right_p, top_y + i);
2268 /* Left. */
2269 if (left_p)
2270 for (i = 0; i < width; ++i)
2271 XDrawLine (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f), gc,
2272 left_x + i, top_y + i, left_x + i, bottom_y - i);
2274 mac_reset_clipping (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f));
2275 if (raised_p)
2276 gc = f->output_data.mac->black_relief.gc;
2277 else
2278 gc = f->output_data.mac->white_relief.gc;
2279 mac_set_clip_rectangle (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f),
2280 clip_rect);
2282 /* Bottom. */
2283 for (i = 0; i < width; ++i)
2284 XDrawLine (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f), gc,
2285 left_x + i * left_p, bottom_y - i,
2286 right_x + 1 - i * right_p, bottom_y - i);
2288 /* Right. */
2289 if (right_p)
2290 for (i = 0; i < width; ++i)
2291 XDrawLine (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f), gc,
2292 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
2294 mac_reset_clipping (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f));
2298 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2299 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2300 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
2301 left side of the rectangle. RIGHT_P non-zero means draw a line
2302 on the right side of the rectangle. CLIP_RECT is the clipping
2303 rectangle to use when drawing. */
2305 static void
2306 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2307 left_p, right_p, clip_rect)
2308 struct glyph_string *s;
2309 int left_x, top_y, right_x, bottom_y, left_p, right_p;
2310 Rect *clip_rect;
2312 XGCValues xgcv;
2314 xgcv.foreground = s->face->box_color;
2315 mac_set_clip_rectangle (s->display, s->window, clip_rect);
2317 /* Top. */
2318 XFillRectangle (s->display, s->window, &xgcv,
2319 left_x, top_y, right_x - left_x, width);
2321 /* Left. */
2322 if (left_p)
2323 XFillRectangle (s->display, s->window, &xgcv,
2324 left_x, top_y, width, bottom_y - top_y);
2326 /* Bottom. */
2327 XFillRectangle (s->display, s->window, &xgcv,
2328 left_x, bottom_y - width, right_x - left_x, width);
2330 /* Right. */
2331 if (right_p)
2332 XFillRectangle (s->display, s->window, &xgcv,
2333 right_x - width, top_y, width, bottom_y - top_y);
2335 mac_reset_clipping (s->display, s->window);
2339 /* Draw a box around glyph string S. */
2341 static void
2342 x_draw_glyph_string_box (s)
2343 struct glyph_string *s;
2345 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2346 int left_p, right_p;
2347 struct glyph *last_glyph;
2348 Rect clip_rect;
2350 last_x = window_box_right (s->w, s->area);
2351 if (s->row->full_width_p
2352 && !s->w->pseudo_window_p)
2354 last_x += WINDOW_RIGHT_SCROLL_BAR_AREA_WIDTH (s->w);
2355 if (s->area != RIGHT_MARGIN_AREA
2356 || WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (s->w))
2357 last_x += WINDOW_RIGHT_FRINGE_WIDTH (s->w);
2360 /* The glyph that may have a right box line. */
2361 last_glyph = (s->cmp || s->img
2362 ? s->first_glyph
2363 : s->first_glyph + s->nchars - 1);
2365 width = abs (s->face->box_line_width);
2366 raised_p = s->face->box == FACE_RAISED_BOX;
2367 left_x = s->x;
2368 right_x = ((s->row->full_width_p && s->extends_to_end_of_line_p
2369 ? last_x - 1
2370 : min (last_x, s->x + s->background_width) - 1));
2371 top_y = s->y;
2372 bottom_y = top_y + s->height - 1;
2374 left_p = (s->first_glyph->left_box_line_p
2375 || (s->hl == DRAW_MOUSE_FACE
2376 && (s->prev == NULL
2377 || s->prev->hl != s->hl)));
2378 right_p = (last_glyph->right_box_line_p
2379 || (s->hl == DRAW_MOUSE_FACE
2380 && (s->next == NULL
2381 || s->next->hl != s->hl)));
2383 get_glyph_string_clip_rect (s, &clip_rect);
2385 if (s->face->box == FACE_SIMPLE_BOX)
2386 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2387 left_p, right_p, &clip_rect);
2388 else
2390 x_setup_relief_colors (s);
2391 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2392 width, raised_p, left_p, right_p, &clip_rect);
2397 /* Draw foreground of image glyph string S. */
2399 static void
2400 x_draw_image_foreground (s)
2401 struct glyph_string *s;
2403 int x;
2404 int y = s->ybase - image_ascent (s->img, s->face);
2406 /* If first glyph of S has a left box line, start drawing it to the
2407 right of that line. */
2408 if (s->face->box != FACE_NO_BOX
2409 && s->first_glyph->left_box_line_p)
2410 x = s->x + abs (s->face->box_line_width);
2411 else
2412 x = s->x;
2414 /* If there is a margin around the image, adjust x- and y-position
2415 by that margin. */
2416 x += s->img->hmargin;
2417 y += s->img->vmargin;
2419 if (s->img->pixmap)
2421 #if 0 /* MAC_TODO: image mask */
2422 if (s->img->mask)
2424 /* We can't set both a clip mask and use XSetClipRectangles
2425 because the latter also sets a clip mask. We also can't
2426 trust on the shape extension to be available
2427 (XShapeCombineRegion). So, compute the rectangle to draw
2428 manually. */
2429 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2430 | GCFunction);
2431 XGCValues xgcv;
2432 XRectangle clip_rect, image_rect, r;
2434 xgcv.clip_mask = s->img->mask;
2435 xgcv.clip_x_origin = x;
2436 xgcv.clip_y_origin = y;
2437 xgcv.function = GXcopy;
2438 XChangeGC (s->display, s->gc, mask, &xgcv);
2440 get_glyph_string_clip_rect (s, &clip_rect);
2441 image_rect.x = x;
2442 image_rect.y = y;
2443 image_rect.width = s->img->width;
2444 image_rect.height = s->img->height;
2445 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2446 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2447 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
2449 else
2450 #endif /* MAC_TODO */
2452 mac_copy_area (s->display, s->img->pixmap, s->window, s->gc,
2453 0, 0, s->img->width, s->img->height, x, y);
2455 /* When the image has a mask, we can expect that at
2456 least part of a mouse highlight or a block cursor will
2457 be visible. If the image doesn't have a mask, make
2458 a block cursor visible by drawing a rectangle around
2459 the image. I believe it's looking better if we do
2460 nothing here for mouse-face. */
2461 if (s->hl == DRAW_CURSOR)
2463 int r = s->img->relief;
2464 if (r < 0) r = -r;
2465 mac_draw_rectangle (s->display, s->window, s->gc, x - r, y - r,
2466 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
2470 else
2471 /* Draw a rectangle if image could not be loaded. */
2472 mac_draw_rectangle (s->display, s->window, s->gc, x, y,
2473 s->img->width - 1, s->img->height - 1);
2478 /* Draw a relief around the image glyph string S. */
2480 static void
2481 x_draw_image_relief (s)
2482 struct glyph_string *s;
2484 int x0, y0, x1, y1, thick, raised_p;
2485 Rect r;
2486 int x;
2487 int y = s->ybase - image_ascent (s->img, s->face);
2489 /* If first glyph of S has a left box line, start drawing it to the
2490 right of that line. */
2491 if (s->face->box != FACE_NO_BOX
2492 && s->first_glyph->left_box_line_p)
2493 x = s->x + abs (s->face->box_line_width);
2494 else
2495 x = s->x;
2497 /* If there is a margin around the image, adjust x- and y-position
2498 by that margin. */
2499 x += s->img->hmargin;
2500 y += s->img->vmargin;
2502 if (s->hl == DRAW_IMAGE_SUNKEN
2503 || s->hl == DRAW_IMAGE_RAISED)
2505 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2506 raised_p = s->hl == DRAW_IMAGE_RAISED;
2508 else
2510 thick = abs (s->img->relief);
2511 raised_p = s->img->relief > 0;
2514 x0 = x - thick;
2515 y0 = y - thick;
2516 x1 = x + s->img->width + thick - 1;
2517 y1 = y + s->img->height + thick - 1;
2519 x_setup_relief_colors (s);
2520 get_glyph_string_clip_rect (s, &r);
2521 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
2525 /* Draw the foreground of image glyph string S to PIXMAP. */
2527 static void
2528 x_draw_image_foreground_1 (s, pixmap)
2529 struct glyph_string *s;
2530 Pixmap pixmap;
2532 int x;
2533 int y = s->ybase - s->y - image_ascent (s->img, s->face);
2535 /* If first glyph of S has a left box line, start drawing it to the
2536 right of that line. */
2537 if (s->face->box != FACE_NO_BOX
2538 && s->first_glyph->left_box_line_p)
2539 x = abs (s->face->box_line_width);
2540 else
2541 x = 0;
2543 /* If there is a margin around the image, adjust x- and y-position
2544 by that margin. */
2545 x += s->img->hmargin;
2546 y += s->img->vmargin;
2548 if (s->img->pixmap)
2550 #if 0 /* MAC_TODO: image mask */
2551 if (s->img->mask)
2553 /* We can't set both a clip mask and use XSetClipRectangles
2554 because the latter also sets a clip mask. We also can't
2555 trust on the shape extension to be available
2556 (XShapeCombineRegion). So, compute the rectangle to draw
2557 manually. */
2558 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2559 | GCFunction);
2560 XGCValues xgcv;
2562 xgcv.clip_mask = s->img->mask;
2563 xgcv.clip_x_origin = x;
2564 xgcv.clip_y_origin = y;
2565 xgcv.function = GXcopy;
2566 XChangeGC (s->display, s->gc, mask, &xgcv);
2568 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2569 0, 0, s->img->width, s->img->height, x, y);
2570 XSetClipMask (s->display, s->gc, None);
2572 else
2573 #endif /* MAC_TODO */
2575 mac_copy_area_to_pixmap (s->display, s->img->pixmap, pixmap, s->gc,
2576 0, 0, s->img->width, s->img->height, x, y);
2578 /* When the image has a mask, we can expect that at
2579 least part of a mouse highlight or a block cursor will
2580 be visible. If the image doesn't have a mask, make
2581 a block cursor visible by drawing a rectangle around
2582 the image. I believe it's looking better if we do
2583 nothing here for mouse-face. */
2584 if (s->hl == DRAW_CURSOR)
2586 int r = s->img->relief;
2587 if (r < 0) r = -r;
2588 mac_draw_rectangle_to_pixmap (s->display, pixmap, s->gc, x - r, y - r,
2589 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
2593 else
2594 /* Draw a rectangle if image could not be loaded. */
2595 mac_draw_rectangle_to_pixmap (s->display, pixmap, s->gc, x, y,
2596 s->img->width - 1, s->img->height - 1);
2600 /* Draw part of the background of glyph string S. X, Y, W, and H
2601 give the rectangle to draw. */
2603 static void
2604 x_draw_glyph_string_bg_rect (s, x, y, w, h)
2605 struct glyph_string *s;
2606 int x, y, w, h;
2608 #if 0 /* MAC_TODO: stipple */
2609 if (s->stippled_p)
2611 /* Fill background with a stipple pattern. */
2612 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2613 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2614 XSetFillStyle (s->display, s->gc, FillSolid);
2616 else
2617 #endif /* MAC_TODO */
2618 x_clear_glyph_string_rect (s, x, y, w, h);
2622 /* Draw image glyph string S.
2624 s->y
2625 s->x +-------------------------
2626 | s->face->box
2628 | +-------------------------
2629 | | s->img->vmargin
2631 | | +-------------------
2632 | | | the image
2636 static void
2637 x_draw_image_glyph_string (s)
2638 struct glyph_string *s;
2640 int x, y;
2641 int box_line_hwidth = abs (s->face->box_line_width);
2642 int box_line_vwidth = max (s->face->box_line_width, 0);
2643 int height;
2644 Pixmap pixmap = 0;
2646 height = s->height - 2 * box_line_vwidth;
2648 /* Fill background with face under the image. Do it only if row is
2649 taller than image or if image has a clip mask to reduce
2650 flickering. */
2651 s->stippled_p = s->face->stipple != 0;
2652 if (height > s->img->height
2653 || s->img->hmargin
2654 || s->img->vmargin
2655 #if 0 /* TODO: image mask */
2656 || s->img->mask
2657 #endif
2658 || s->img->pixmap == 0
2659 || s->width != s->background_width)
2661 if (box_line_hwidth && s->first_glyph->left_box_line_p)
2662 x = s->x + box_line_hwidth;
2663 else
2664 x = s->x;
2666 y = s->y + box_line_vwidth;
2667 #if 0 /* TODO: image mask */
2668 if (s->img->mask)
2670 /* Create a pixmap as large as the glyph string. Fill it
2671 with the background color. Copy the image to it, using
2672 its mask. Copy the temporary pixmap to the display. */
2673 Screen *screen = FRAME_X_SCREEN (s->f);
2674 int depth = DefaultDepthOfScreen (screen);
2676 /* Create a pixmap as large as the glyph string. */
2677 pixmap = XCreatePixmap (s->display, s->window,
2678 s->background_width,
2679 s->height, depth);
2681 /* Don't clip in the following because we're working on the
2682 pixmap. */
2683 XSetClipMask (s->display, s->gc, None);
2685 /* Fill the pixmap with the background color/stipple. */
2686 if (s->stippled_p)
2688 /* Fill background with a stipple pattern. */
2689 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2690 XFillRectangle (s->display, pixmap, s->gc,
2691 0, 0, s->background_width, s->height);
2692 XSetFillStyle (s->display, s->gc, FillSolid);
2694 else
2696 XGCValues xgcv;
2697 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2698 &xgcv);
2699 XSetForeground (s->display, s->gc, xgcv.background);
2700 XFillRectangle (s->display, pixmap, s->gc,
2701 0, 0, s->background_width, s->height);
2702 XSetForeground (s->display, s->gc, xgcv.foreground);
2705 else
2706 #endif
2707 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2709 s->background_filled_p = 1;
2712 /* Draw the foreground. */
2713 if (pixmap != 0)
2715 x_draw_image_foreground_1 (s, pixmap);
2716 x_set_glyph_string_clipping (s);
2717 mac_copy_area (s->display, pixmap, s->window, s->gc,
2718 0, 0, s->background_width, s->height, s->x, s->y);
2719 mac_reset_clipping (s->display, s->window);
2720 XFreePixmap (s->display, pixmap);
2722 else
2723 x_draw_image_foreground (s);
2725 /* If we must draw a relief around the image, do it. */
2726 if (s->img->relief
2727 || s->hl == DRAW_IMAGE_RAISED
2728 || s->hl == DRAW_IMAGE_SUNKEN)
2729 x_draw_image_relief (s);
2733 /* Draw stretch glyph string S. */
2735 static void
2736 x_draw_stretch_glyph_string (s)
2737 struct glyph_string *s;
2739 xassert (s->first_glyph->type == STRETCH_GLYPH);
2740 s->stippled_p = s->face->stipple != 0;
2742 if (s->hl == DRAW_CURSOR
2743 && !x_stretch_cursor_p)
2745 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2746 as wide as the stretch glyph. */
2747 int width = min (FRAME_COLUMN_WIDTH (s->f), s->background_width);
2749 /* Draw cursor. */
2750 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
2752 /* Clear rest using the GC of the original non-cursor face. */
2753 if (width < s->background_width)
2755 GC gc = s->face->gc;
2756 int x = s->x + width, y = s->y;
2757 int w = s->background_width - width, h = s->height;
2758 Rect r;
2760 if (s->row->mouse_face_p
2761 && cursor_in_mouse_face_p (s->w))
2763 x_set_mouse_face_gc (s);
2764 gc = s->gc;
2766 else
2767 gc = s->face->gc;
2769 get_glyph_string_clip_rect (s, &r);
2770 mac_set_clip_rectangle (s->display, s->window, &r);
2772 #if 0 /* MAC_TODO: stipple */
2773 if (s->face->stipple)
2775 /* Fill background with a stipple pattern. */
2776 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2777 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2778 XSetFillStyle (s->display, gc, FillSolid);
2780 else
2781 #endif /* MAC_TODO */
2783 XGCValues xgcv;
2784 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2785 XSetForeground (s->display, gc, xgcv.background);
2786 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2787 XSetForeground (s->display, gc, xgcv.foreground);
2790 mac_reset_clipping (s->display, s->window);
2793 else if (!s->background_filled_p)
2794 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
2795 s->height);
2797 s->background_filled_p = 1;
2801 /* Draw glyph string S. */
2803 static void
2804 x_draw_glyph_string (s)
2805 struct glyph_string *s;
2807 int relief_drawn_p = 0;
2809 /* If S draws into the background of its successor, draw the
2810 background of the successor first so that S can draw into it.
2811 This makes S->next use XDrawString instead of XDrawImageString. */
2812 if (s->next && s->right_overhang && !s->for_overlaps_p)
2814 xassert (s->next->img == NULL);
2815 x_set_glyph_string_gc (s->next);
2816 x_set_glyph_string_clipping (s->next);
2817 x_draw_glyph_string_background (s->next, 1);
2821 /* Set up S->gc, set clipping and draw S. */
2822 x_set_glyph_string_gc (s);
2824 /* Draw relief (if any) in advance for char/composition so that the
2825 glyph string can be drawn over it. */
2826 if (!s->for_overlaps_p
2827 && s->face->box != FACE_NO_BOX
2828 && (s->first_glyph->type == CHAR_GLYPH
2829 || s->first_glyph->type == COMPOSITE_GLYPH))
2832 x_set_glyph_string_clipping (s);
2833 x_draw_glyph_string_background (s, 1);
2834 x_draw_glyph_string_box (s);
2835 x_set_glyph_string_clipping (s);
2836 relief_drawn_p = 1;
2838 else
2839 x_set_glyph_string_clipping (s);
2841 switch (s->first_glyph->type)
2843 case IMAGE_GLYPH:
2844 x_draw_image_glyph_string (s);
2845 break;
2847 case STRETCH_GLYPH:
2848 x_draw_stretch_glyph_string (s);
2849 break;
2851 case CHAR_GLYPH:
2852 if (s->for_overlaps_p)
2853 s->background_filled_p = 1;
2854 else
2855 x_draw_glyph_string_background (s, 0);
2856 x_draw_glyph_string_foreground (s);
2857 break;
2859 case COMPOSITE_GLYPH:
2860 if (s->for_overlaps_p || s->gidx > 0)
2861 s->background_filled_p = 1;
2862 else
2863 x_draw_glyph_string_background (s, 1);
2864 x_draw_composite_glyph_string_foreground (s);
2865 break;
2867 default:
2868 abort ();
2871 if (!s->for_overlaps_p)
2873 /* Draw underline. */
2874 if (s->face->underline_p)
2876 unsigned long h = 1;
2877 unsigned long dy = s->height - h;
2879 if (s->face->underline_defaulted_p)
2880 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2881 s->width, h);
2882 else
2884 XGCValues xgcv;
2885 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2886 XSetForeground (s->display, s->gc, s->face->underline_color);
2887 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2888 s->width, h);
2889 XSetForeground (s->display, s->gc, xgcv.foreground);
2893 /* Draw overline. */
2894 if (s->face->overline_p)
2896 unsigned long dy = 0, h = 1;
2898 if (s->face->overline_color_defaulted_p)
2899 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2900 s->width, h);
2901 else
2903 XGCValues xgcv;
2904 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2905 XSetForeground (s->display, s->gc, s->face->overline_color);
2906 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2907 s->width, h);
2908 XSetForeground (s->display, s->gc, xgcv.foreground);
2912 /* Draw strike-through. */
2913 if (s->face->strike_through_p)
2915 unsigned long h = 1;
2916 unsigned long dy = (s->height - h) / 2;
2918 if (s->face->strike_through_color_defaulted_p)
2919 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2920 s->width, h);
2921 else
2923 XGCValues xgcv;
2924 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2925 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2926 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2927 s->width, h);
2928 XSetForeground (s->display, s->gc, xgcv.foreground);
2932 /* Draw relief. */
2933 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2934 x_draw_glyph_string_box (s);
2937 /* Reset clipping. */
2938 mac_reset_clipping (s->display, s->window);
2941 /* Shift display to make room for inserted glyphs. */
2943 void
2944 mac_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
2945 struct frame *f;
2946 int x, y, width, height, shift_by;
2948 mac_scroll_area (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f),
2949 f->output_data.mac->normal_gc,
2950 x, y, width, height,
2951 x + shift_by, y);
2955 /* Delete N glyphs at the nominal cursor position. Not implemented
2956 for X frames. */
2958 static void
2959 x_delete_glyphs (n)
2960 register int n;
2962 abort ();
2966 /* Clear entire frame. If updating_frame is non-null, clear that
2967 frame. Otherwise clear the selected frame. */
2969 static void
2970 x_clear_frame ()
2972 struct frame *f;
2974 if (updating_frame)
2975 f = updating_frame;
2976 else
2977 f = SELECTED_FRAME ();
2979 /* Clearing the frame will erase any cursor, so mark them all as no
2980 longer visible. */
2981 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2982 output_cursor.hpos = output_cursor.vpos = 0;
2983 output_cursor.x = -1;
2985 /* We don't set the output cursor here because there will always
2986 follow an explicit cursor_to. */
2987 BLOCK_INPUT;
2988 XClearWindow (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f));
2990 #if 0 /* Clearing frame on Mac OS clears scroll bars. */
2991 /* We have to clear the scroll bars, too. If we have changed
2992 colors or something like that, then they should be notified. */
2993 x_scroll_bar_clear (f);
2994 #endif
2996 XFlush (FRAME_MAC_DISPLAY (f));
2997 UNBLOCK_INPUT;
3002 /* Invert the middle quarter of the frame for .15 sec. */
3004 /* We use the select system call to do the waiting, so we have to make
3005 sure it's available. If it isn't, we just won't do visual bells. */
3007 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
3009 /* Subtract the `struct timeval' values X and Y, storing the result in
3010 *RESULT. Return 1 if the difference is negative, otherwise 0. */
3012 static int
3013 timeval_subtract (result, x, y)
3014 struct timeval *result, x, y;
3016 /* Perform the carry for the later subtraction by updating y. This
3017 is safer because on some systems the tv_sec member is unsigned. */
3018 if (x.tv_usec < y.tv_usec)
3020 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
3021 y.tv_usec -= 1000000 * nsec;
3022 y.tv_sec += nsec;
3025 if (x.tv_usec - y.tv_usec > 1000000)
3027 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
3028 y.tv_usec += 1000000 * nsec;
3029 y.tv_sec -= nsec;
3032 /* Compute the time remaining to wait. tv_usec is certainly
3033 positive. */
3034 result->tv_sec = x.tv_sec - y.tv_sec;
3035 result->tv_usec = x.tv_usec - y.tv_usec;
3037 /* Return indication of whether the result should be considered
3038 negative. */
3039 return x.tv_sec < y.tv_sec;
3042 void
3043 XTflash (f)
3044 struct frame *f;
3046 BLOCK_INPUT;
3048 FlashMenuBar (0);
3051 struct timeval wakeup;
3053 EMACS_GET_TIME (wakeup);
3055 /* Compute time to wait until, propagating carry from usecs. */
3056 wakeup.tv_usec += 150000;
3057 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
3058 wakeup.tv_usec %= 1000000;
3060 /* Keep waiting until past the time wakeup. */
3061 while (1)
3063 struct timeval timeout;
3065 EMACS_GET_TIME (timeout);
3067 /* In effect, timeout = wakeup - timeout.
3068 Break if result would be negative. */
3069 if (timeval_subtract (&timeout, wakeup, timeout))
3070 break;
3072 /* Try to wait that long--but we might wake up sooner. */
3073 select (0, NULL, NULL, NULL, &timeout);
3077 FlashMenuBar (0);
3079 UNBLOCK_INPUT;
3082 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
3085 /* Make audible bell. */
3087 void
3088 XTring_bell ()
3090 struct frame *f = SELECTED_FRAME ();
3092 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
3093 if (visible_bell)
3094 XTflash (f);
3095 else
3096 #endif
3098 BLOCK_INPUT;
3099 SysBeep (1);
3100 XFlush (FRAME_MAC_DISPLAY (f));
3101 UNBLOCK_INPUT;
3107 /* Specify how many text lines, from the top of the window,
3108 should be affected by insert-lines and delete-lines operations.
3109 This, and those operations, are used only within an update
3110 that is bounded by calls to x_update_begin and x_update_end. */
3112 void
3113 XTset_terminal_window (n)
3114 register int n;
3116 /* This function intentionally left blank. */
3121 /***********************************************************************
3122 Line Dance
3123 ***********************************************************************/
3125 /* Perform an insert-lines or delete-lines operation, inserting N
3126 lines or deleting -N lines at vertical position VPOS. */
3128 static void
3129 x_ins_del_lines (vpos, n)
3130 int vpos, n;
3132 abort ();
3136 /* Scroll part of the display as described by RUN. */
3138 static void
3139 x_scroll_run (w, run)
3140 struct window *w;
3141 struct run *run;
3143 struct frame *f = XFRAME (w->frame);
3144 int x, y, width, height, from_y, to_y, bottom_y;
3146 /* Get frame-relative bounding box of the text display area of W,
3147 without mode lines. Include in this box the left and right
3148 fringes of W. */
3149 window_box (w, -1, &x, &y, &width, &height);
3151 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3152 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3153 bottom_y = y + height;
3155 if (to_y < from_y)
3157 /* Scrolling up. Make sure we don't copy part of the mode
3158 line at the bottom. */
3159 if (from_y + run->height > bottom_y)
3160 height = bottom_y - from_y;
3161 else
3162 height = run->height;
3164 else
3166 /* Scolling down. Make sure we don't copy over the mode line.
3167 at the bottom. */
3168 if (to_y + run->height > bottom_y)
3169 height = bottom_y - to_y;
3170 else
3171 height = run->height;
3174 BLOCK_INPUT;
3176 /* Cursor off. Will be switched on again in x_update_window_end. */
3177 updated_window = w;
3178 x_clear_cursor (w);
3180 mac_scroll_area (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f),
3181 f->output_data.mac->normal_gc,
3182 x, from_y,
3183 width, height,
3184 x, to_y);
3186 UNBLOCK_INPUT;
3191 /***********************************************************************
3192 Exposure Events
3193 ***********************************************************************/
3196 static void
3197 frame_highlight (f)
3198 struct frame *f;
3200 x_update_cursor (f, 1);
3203 static void
3204 frame_unhighlight (f)
3205 struct frame *f;
3207 x_update_cursor (f, 1);
3210 /* The focus has changed. Update the frames as necessary to reflect
3211 the new situation. Note that we can't change the selected frame
3212 here, because the Lisp code we are interrupting might become confused.
3213 Each event gets marked with the frame in which it occurred, so the
3214 Lisp code can tell when the switch took place by examining the events. */
3216 static void
3217 x_new_focus_frame (dpyinfo, frame)
3218 struct x_display_info *dpyinfo;
3219 struct frame *frame;
3221 struct frame *old_focus = dpyinfo->x_focus_frame;
3223 if (frame != dpyinfo->x_focus_frame)
3225 /* Set this before calling other routines, so that they see
3226 the correct value of x_focus_frame. */
3227 dpyinfo->x_focus_frame = frame;
3229 if (old_focus && old_focus->auto_lower)
3230 x_lower_frame (old_focus);
3232 #if 0
3233 selected_frame = frame;
3234 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
3235 selected_frame);
3236 Fselect_window (selected_frame->selected_window, Qnil);
3237 choose_minibuf_frame ();
3238 #endif /* ! 0 */
3240 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3241 pending_autoraise_frame = dpyinfo->x_focus_frame;
3242 else
3243 pending_autoraise_frame = 0;
3246 x_frame_rehighlight (dpyinfo);
3249 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3251 void
3252 x_mouse_leave (dpyinfo)
3253 struct x_display_info *dpyinfo;
3255 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3258 /* The focus has changed, or we have redirected a frame's focus to
3259 another frame (this happens when a frame uses a surrogate
3260 mini-buffer frame). Shift the highlight as appropriate.
3262 The FRAME argument doesn't necessarily have anything to do with which
3263 frame is being highlighted or un-highlighted; we only use it to find
3264 the appropriate X display info. */
3266 static void
3267 XTframe_rehighlight (frame)
3268 struct frame *frame;
3272 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
3275 static void
3276 x_frame_rehighlight (dpyinfo)
3277 struct x_display_info *dpyinfo;
3279 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3281 if (dpyinfo->x_focus_frame)
3283 dpyinfo->x_highlight_frame
3284 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3285 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3286 : dpyinfo->x_focus_frame);
3287 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3289 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
3290 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3293 else
3294 dpyinfo->x_highlight_frame = 0;
3296 if (dpyinfo->x_highlight_frame != old_highlight)
3298 if (old_highlight)
3299 frame_unhighlight (old_highlight);
3300 if (dpyinfo->x_highlight_frame)
3301 frame_highlight (dpyinfo->x_highlight_frame);
3307 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3309 #if 0 /* MAC_TODO */
3310 /* Initialize mode_switch_bit and modifier_meaning. */
3311 static void
3312 x_find_modifier_meanings (dpyinfo)
3313 struct x_display_info *dpyinfo;
3315 int min_code, max_code;
3316 KeySym *syms;
3317 int syms_per_code;
3318 XModifierKeymap *mods;
3320 dpyinfo->meta_mod_mask = 0;
3321 dpyinfo->shift_lock_mask = 0;
3322 dpyinfo->alt_mod_mask = 0;
3323 dpyinfo->super_mod_mask = 0;
3324 dpyinfo->hyper_mod_mask = 0;
3326 #ifdef HAVE_X11R4
3327 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3328 #else
3329 min_code = dpyinfo->display->min_keycode;
3330 max_code = dpyinfo->display->max_keycode;
3331 #endif
3333 syms = XGetKeyboardMapping (dpyinfo->display,
3334 min_code, max_code - min_code + 1,
3335 &syms_per_code);
3336 mods = XGetModifierMapping (dpyinfo->display);
3338 /* Scan the modifier table to see which modifier bits the Meta and
3339 Alt keysyms are on. */
3341 int row, col; /* The row and column in the modifier table. */
3343 for (row = 3; row < 8; row++)
3344 for (col = 0; col < mods->max_keypermod; col++)
3346 KeyCode code
3347 = mods->modifiermap[(row * mods->max_keypermod) + col];
3349 /* Zeroes are used for filler. Skip them. */
3350 if (code == 0)
3351 continue;
3353 /* Are any of this keycode's keysyms a meta key? */
3355 int code_col;
3357 for (code_col = 0; code_col < syms_per_code; code_col++)
3359 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3361 switch (sym)
3363 case XK_Meta_L:
3364 case XK_Meta_R:
3365 dpyinfo->meta_mod_mask |= (1 << row);
3366 break;
3368 case XK_Alt_L:
3369 case XK_Alt_R:
3370 dpyinfo->alt_mod_mask |= (1 << row);
3371 break;
3373 case XK_Hyper_L:
3374 case XK_Hyper_R:
3375 dpyinfo->hyper_mod_mask |= (1 << row);
3376 break;
3378 case XK_Super_L:
3379 case XK_Super_R:
3380 dpyinfo->super_mod_mask |= (1 << row);
3381 break;
3383 case XK_Shift_Lock:
3384 /* Ignore this if it's not on the lock modifier. */
3385 if ((1 << row) == LockMask)
3386 dpyinfo->shift_lock_mask = LockMask;
3387 break;
3394 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3395 if (! dpyinfo->meta_mod_mask)
3397 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3398 dpyinfo->alt_mod_mask = 0;
3401 /* If some keys are both alt and meta,
3402 make them just meta, not alt. */
3403 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3405 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3408 XFree ((char *) syms);
3409 XFreeModifiermap (mods);
3412 #endif /* MAC_TODO */
3414 /* Convert between the modifier bits X uses and the modifier bits
3415 Emacs uses. */
3417 static unsigned int
3418 x_mac_to_emacs_modifiers (dpyinfo, state)
3419 struct x_display_info *dpyinfo;
3420 unsigned short state;
3422 return (((state & shiftKey) ? shift_modifier : 0)
3423 | ((state & controlKey) ? ctrl_modifier : 0)
3424 | ((state & cmdKey) ? meta_modifier : 0)
3425 | ((state & optionKey) ? alt_modifier : 0));
3428 #if 0 /* MAC_TODO */
3429 static unsigned short
3430 x_emacs_to_x_modifiers (dpyinfo, state)
3431 struct x_display_info *dpyinfo;
3432 unsigned int state;
3434 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
3435 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
3436 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
3437 | ((state & shift_modifier) ? ShiftMask : 0)
3438 | ((state & ctrl_modifier) ? ControlMask : 0)
3439 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
3441 #endif /* MAC_TODO */
3443 /* Convert a keysym to its name. */
3445 char *
3446 x_get_keysym_name (keysym)
3447 int keysym;
3449 char *value;
3451 BLOCK_INPUT;
3452 #if 0
3453 value = XKeysymToString (keysym);
3454 #else
3455 value = 0;
3456 #endif
3457 UNBLOCK_INPUT;
3459 return value;
3464 /* Mouse clicks and mouse movement. Rah. */
3466 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3468 If the event is a button press, then note that we have grabbed
3469 the mouse. */
3471 static Lisp_Object
3472 construct_mouse_click (result, event, f)
3473 struct input_event *result;
3474 EventRecord *event;
3475 struct frame *f;
3477 Point mouseLoc;
3479 result->kind = MOUSE_CLICK_EVENT;
3480 result->code = 0; /* only one mouse button */
3481 result->timestamp = event->when;
3482 result->modifiers = event->what == mouseDown ? down_modifier : up_modifier;
3484 mouseLoc = event->where;
3486 #if TARGET_API_MAC_CARBON
3487 SetPort (GetWindowPort (FRAME_MAC_WINDOW (f)));
3488 #else
3489 SetPort (FRAME_MAC_WINDOW (f));
3490 #endif
3492 GlobalToLocal (&mouseLoc);
3493 XSETINT (result->x, mouseLoc.h);
3494 XSETINT (result->y, mouseLoc.v);
3496 XSETFRAME (result->frame_or_window, f);
3498 result->arg = Qnil;
3499 return Qnil;
3503 /* Function to report a mouse movement to the mainstream Emacs code.
3504 The input handler calls this.
3506 We have received a mouse movement event, which is given in *event.
3507 If the mouse is over a different glyph than it was last time, tell
3508 the mainstream emacs code by setting mouse_moved. If not, ask for
3509 another motion event, so we can check again the next time it moves. */
3511 static Point last_mouse_motion_position;
3512 static Lisp_Object last_mouse_motion_frame;
3514 static void
3515 note_mouse_movement (frame, pos)
3516 FRAME_PTR frame;
3517 Point *pos;
3519 #if TARGET_API_MAC_CARBON
3520 Rect r;
3521 #endif
3523 last_mouse_movement_time = TickCount () * (1000 / 60); /* to milliseconds */
3524 last_mouse_motion_position = *pos;
3525 XSETFRAME (last_mouse_motion_frame, frame);
3527 #if TARGET_API_MAC_CARBON
3528 if (!PtInRect (*pos, GetWindowPortBounds (FRAME_MAC_WINDOW (frame), &r)))
3529 #else
3530 if (!PtInRect (*pos, &FRAME_MAC_WINDOW (frame)->portRect))
3531 #endif
3533 frame->mouse_moved = 1;
3534 last_mouse_scroll_bar = Qnil;
3535 note_mouse_highlight (frame, -1, -1);
3537 /* Has the mouse moved off the glyph it was on at the last sighting? */
3538 else if (pos->h < last_mouse_glyph.left
3539 || pos->h >= last_mouse_glyph.right
3540 || pos->v < last_mouse_glyph.top
3541 || pos->v >= last_mouse_glyph.bottom)
3543 frame->mouse_moved = 1;
3544 last_mouse_scroll_bar = Qnil;
3545 note_mouse_highlight (frame, pos->h, pos->v);
3549 /* This is used for debugging, to turn off note_mouse_highlight. */
3551 int disable_mouse_highlight;
3555 /************************************************************************
3556 Mouse Face
3557 ************************************************************************/
3559 static struct scroll_bar *x_window_to_scroll_bar ();
3560 static void x_scroll_bar_report_motion ();
3561 static void x_check_fullscreen P_ ((struct frame *));
3562 static void x_check_fullscreen_move P_ ((struct frame *));
3563 static int glyph_rect P_ ((struct frame *f, int, int, Rect *));
3566 /* MAC TODO: This should be called from somewhere (or removed) ++KFS */
3568 static void
3569 redo_mouse_highlight ()
3571 if (!NILP (last_mouse_motion_frame)
3572 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3573 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3574 last_mouse_motion_position.h,
3575 last_mouse_motion_position.v);
3579 /* Try to determine frame pixel position and size of the glyph under
3580 frame pixel coordinates X/Y on frame F . Return the position and
3581 size in *RECT. Value is non-zero if we could compute these
3582 values. */
3584 static int
3585 glyph_rect (f, x, y, rect)
3586 struct frame *f;
3587 int x, y;
3588 Rect *rect;
3590 Lisp_Object window;
3592 window = window_from_coordinates (f, x, y, 0, &x, &y, 0);
3594 if (!NILP (window))
3596 struct window *w = XWINDOW (window);
3597 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
3598 struct glyph_row *end = r + w->current_matrix->nrows - 1;
3600 for (; r < end && r->enabled_p; ++r)
3601 if (r->y <= y && r->y + r->height > y)
3603 /* Found the row at y. */
3604 struct glyph *g = r->glyphs[TEXT_AREA];
3605 struct glyph *end = g + r->used[TEXT_AREA];
3606 int gx;
3608 rect->top = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
3609 rect->bottom = rect->top + r->height;
3611 if (x < r->x)
3613 /* x is to the left of the first glyph in the row. */
3614 /* Shouldn't this be a pixel value?
3615 WINDOW_LEFT_EDGE_X (w) seems to be the right value.
3616 ++KFS */
3617 rect->left = WINDOW_LEFT_EDGE_COL (w);
3618 rect->right = WINDOW_TO_FRAME_PIXEL_X (w, r->x);
3619 return 1;
3622 for (gx = r->x; g < end; gx += g->pixel_width, ++g)
3623 if (gx <= x && gx + g->pixel_width > x)
3625 /* x is on a glyph. */
3626 rect->left = WINDOW_TO_FRAME_PIXEL_X (w, gx);
3627 rect->right = rect->left + g->pixel_width;
3628 return 1;
3631 /* x is to the right of the last glyph in the row. */
3632 rect->left = WINDOW_TO_FRAME_PIXEL_X (w, gx);
3633 /* Shouldn't this be a pixel value?
3634 WINDOW_RIGHT_EDGE_X (w) seems to be the right value.
3635 ++KFS */
3636 rect->right = WINDOW_RIGHT_EDGE_COL (w);
3637 return 1;
3641 /* The y is not on any row. */
3642 return 0;
3645 /* MAC TODO: This should be called from somewhere (or removed) ++KFS */
3647 /* Record the position of the mouse in last_mouse_glyph. */
3648 static void
3649 remember_mouse_glyph (f1, gx, gy)
3650 struct frame * f1;
3651 int gx, gy;
3653 if (!glyph_rect (f1, gx, gy, &last_mouse_glyph))
3655 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
3656 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
3658 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
3659 round down even for negative values. */
3660 if (gx < 0)
3661 gx -= width - 1;
3662 if (gy < 0)
3663 gy -= height - 1;
3664 #if 0
3665 /* This was the original code from XTmouse_position, but it seems
3666 to give the position of the glyph diagonally next to the one
3667 the mouse is over. */
3668 gx = (gx + width - 1) / width * width;
3669 gy = (gy + height - 1) / height * height;
3670 #else
3671 gx = gx / width * width;
3672 gy = gy / height * height;
3673 #endif
3675 last_mouse_glyph.left = gx;
3676 last_mouse_glyph.top = gy;
3677 last_mouse_glyph.right = gx + width;
3678 last_mouse_glyph.bottom = gy + height;
3683 /* Return the current position of the mouse.
3684 *fp should be a frame which indicates which display to ask about.
3686 If the mouse movement started in a scroll bar, set *fp, *bar_window,
3687 and *part to the frame, window, and scroll bar part that the mouse
3688 is over. Set *x and *y to the portion and whole of the mouse's
3689 position on the scroll bar.
3691 If the mouse movement started elsewhere, set *fp to the frame the
3692 mouse is on, *bar_window to nil, and *x and *y to the character cell
3693 the mouse is over.
3695 Set *time to the server time-stamp for the time at which the mouse
3696 was at this position.
3698 Don't store anything if we don't have a valid set of values to report.
3700 This clears the mouse_moved flag, so we can wait for the next mouse
3701 movement. */
3703 static void
3704 XTmouse_position (fp, insist, bar_window, part, x, y, time)
3705 FRAME_PTR *fp;
3706 int insist;
3707 Lisp_Object *bar_window;
3708 enum scroll_bar_part *part;
3709 Lisp_Object *x, *y;
3710 unsigned long *time;
3712 Point mouse_pos;
3713 int ignore1, ignore2;
3714 WindowPtr wp = FrontWindow ();
3715 struct frame *f;
3716 Lisp_Object frame, tail;
3718 if (is_emacs_window(wp))
3719 f = ((mac_output *) GetWRefCon (wp))->mFP;
3721 BLOCK_INPUT;
3723 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3724 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3725 else
3727 /* Clear the mouse-moved flag for every frame on this display. */
3728 FOR_EACH_FRAME (tail, frame)
3729 XFRAME (frame)->mouse_moved = 0;
3731 last_mouse_scroll_bar = Qnil;
3733 #if TARGET_API_MAC_CARBON
3734 SetPort (GetWindowPort (wp));
3735 #else
3736 SetPort (wp);
3737 #endif
3739 GetMouse (&mouse_pos);
3741 pixel_to_glyph_coords (f, mouse_pos.h, mouse_pos.v, &ignore1, &ignore2,
3742 &last_mouse_glyph, insist);
3744 *bar_window = Qnil;
3745 *part = scroll_bar_handle;
3746 *fp = f;
3747 XSETINT (*x, mouse_pos.h);
3748 XSETINT (*y, mouse_pos.v);
3749 *time = last_mouse_movement_time;
3752 UNBLOCK_INPUT;
3756 /***********************************************************************
3757 Tool-bars
3758 ***********************************************************************/
3760 /* Handle mouse button event on the tool-bar of frame F, at
3761 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
3762 or ButtonRelase. */
3764 static void
3765 mac_handle_tool_bar_click (f, button_event)
3766 struct frame *f;
3767 EventRecord *button_event;
3769 int x = button_event->where.h;
3770 int y = button_event->where.v;
3772 if (button_event->what == mouseDown)
3773 handle_tool_bar_click (f, x, y, 1, 0);
3774 else
3775 handle_tool_bar_click (f, x, y, 0,
3776 x_mac_to_emacs_modifiers (FRAME_MAC_DISPLAY_INFO (f),
3777 button_event->modifiers));
3781 /************************************************************************
3782 Scroll bars, general
3783 ************************************************************************/
3785 /* Create a scroll bar and return the scroll bar vector for it. W is
3786 the Emacs window on which to create the scroll bar. TOP, LEFT,
3787 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
3788 scroll bar. */
3790 static struct scroll_bar *
3791 x_scroll_bar_create (w, top, left, width, height, disp_top, disp_height)
3792 struct window *w;
3793 int top, left, width, height, disp_top, disp_height;
3795 struct frame *f = XFRAME (w->frame);
3796 struct scroll_bar *bar
3797 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
3798 Rect r;
3799 ControlHandle ch;
3801 BLOCK_INPUT;
3803 r.left = left;
3804 r.top = disp_top;
3805 r.right = left + width;
3806 r.bottom = disp_top + disp_height;
3808 #ifdef TARGET_API_MAC_CARBON
3809 ch = NewControl (FRAME_MAC_WINDOW (f), &r, "\p", 1, 0, 0, 0,
3810 kControlScrollBarProc, 0L);
3811 #else
3812 ch = NewControl (FRAME_MAC_WINDOW (f), &r, "\p", 1, 0, 0, 0, scrollBarProc,
3813 0L);
3814 #endif
3815 SET_SCROLL_BAR_CONTROL_HANDLE (bar, ch);
3816 SetControlReference (ch, (long) bar);
3818 XSETWINDOW (bar->window, w);
3819 XSETINT (bar->top, top);
3820 XSETINT (bar->left, left);
3821 XSETINT (bar->width, width);
3822 XSETINT (bar->height, height);
3823 XSETINT (bar->start, 0);
3824 XSETINT (bar->end, 0);
3825 bar->dragging = Qnil;
3827 /* Add bar to its frame's list of scroll bars. */
3828 bar->next = FRAME_SCROLL_BARS (f);
3829 bar->prev = Qnil;
3830 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
3831 if (!NILP (bar->next))
3832 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
3834 UNBLOCK_INPUT;
3835 return bar;
3839 /* Draw BAR's handle in the proper position.
3841 If the handle is already drawn from START to END, don't bother
3842 redrawing it, unless REBUILD is non-zero; in that case, always
3843 redraw it. (REBUILD is handy for drawing the handle after expose
3844 events.)
3846 Normally, we want to constrain the start and end of the handle to
3847 fit inside its rectangle, but if the user is dragging the scroll
3848 bar handle, we want to let them drag it down all the way, so that
3849 the bar's top is as far down as it goes; otherwise, there's no way
3850 to move to the very end of the buffer. */
3852 static void
3853 x_scroll_bar_set_handle (bar, start, end, rebuild)
3854 struct scroll_bar *bar;
3855 int start, end;
3856 int rebuild;
3858 int dragging = ! NILP (bar->dragging);
3859 ControlHandle ch = SCROLL_BAR_CONTROL_HANDLE (bar);
3860 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3861 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
3862 int length = end - start;
3864 /* If the display is already accurate, do nothing. */
3865 if (! rebuild
3866 && start == XINT (bar->start)
3867 && end == XINT (bar->end))
3868 return;
3870 BLOCK_INPUT;
3872 /* Make sure the values are reasonable, and try to preserve the
3873 distance between start and end. */
3874 if (start < 0)
3875 start = 0;
3876 else if (start > top_range)
3877 start = top_range;
3878 end = start + length;
3880 if (end < start)
3881 end = start;
3882 else if (end > top_range && ! dragging)
3883 end = top_range;
3885 /* Store the adjusted setting in the scroll bar. */
3886 XSETINT (bar->start, start);
3887 XSETINT (bar->end, end);
3889 /* Clip the end position, just for display. */
3890 if (end > top_range)
3891 end = top_range;
3893 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels below
3894 top positions, to make sure the handle is always at least that
3895 many pixels tall. */
3896 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
3898 SetControlMinimum (ch, 0);
3899 /* Don't inadvertently activate deactivated scroll bars */
3900 if (GetControlMaximum (ch) != -1)
3901 SetControlMaximum (ch, top_range + VERTICAL_SCROLL_BAR_MIN_HANDLE
3902 - (end - start));
3903 SetControlValue (ch, start);
3904 #if TARGET_API_MAC_CARBON
3905 SetControlViewSize (ch, end - start);
3906 #endif
3908 UNBLOCK_INPUT;
3912 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
3913 nil. */
3915 static void
3916 x_scroll_bar_remove (bar)
3917 struct scroll_bar *bar;
3919 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3921 BLOCK_INPUT;
3923 /* Destroy the Mac scroll bar control */
3924 DisposeControl (SCROLL_BAR_CONTROL_HANDLE (bar));
3926 /* Disassociate this scroll bar from its window. */
3927 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
3929 UNBLOCK_INPUT;
3932 /* Set the handle of the vertical scroll bar for WINDOW to indicate
3933 that we are displaying PORTION characters out of a total of WHOLE
3934 characters, starting at POSITION. If WINDOW has no scroll bar,
3935 create one. */
3936 static void
3937 XTset_vertical_scroll_bar (w, portion, whole, position)
3938 struct window *w;
3939 int portion, whole, position;
3941 struct frame *f = XFRAME (w->frame);
3942 struct scroll_bar *bar;
3943 int top, height, left, sb_left, width, sb_width, disp_top, disp_height;
3944 int window_y, window_height;
3946 /* Get window dimensions. */
3947 window_box (w, -1, 0, &window_y, 0, &window_height);
3948 top = window_y;
3949 #ifdef MAC_OSX
3950 width = 16;
3951 #else
3952 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
3953 #endif
3954 height = window_height;
3956 /* Compute the left edge of the scroll bar area. */
3957 left = WINDOW_SCROLL_BAR_AREA_X (w);
3959 /* Compute the width of the scroll bar which might be less than
3960 the width of the area reserved for the scroll bar. */
3961 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
3962 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
3963 else
3964 sb_width = width;
3966 /* Compute the left edge of the scroll bar. */
3967 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
3968 sb_left = left + width - sb_width - (width - sb_width) / 2;
3969 else
3970 sb_left = left + (width - sb_width) / 2;
3972 /* Adjustments according to Inside Macintosh to make it look nice */
3973 disp_top = top;
3974 disp_height = height;
3975 if (disp_top == 0)
3977 disp_top = -1;
3978 disp_height++;
3980 else if (disp_top == FRAME_PIXEL_HEIGHT (f) - 16)
3982 disp_top++;
3983 disp_height--;
3986 if (sb_left + sb_width == FRAME_PIXEL_WIDTH (f))
3987 sb_left++;
3989 /* Does the scroll bar exist yet? */
3990 if (NILP (w->vertical_scroll_bar))
3992 BLOCK_INPUT;
3993 XClearArea (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f),
3994 left, top, width, height, 0);
3995 UNBLOCK_INPUT;
3996 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height, disp_top,
3997 disp_height);
3998 XSETVECTOR (w->vertical_scroll_bar, bar);
4000 else
4002 /* It may just need to be moved and resized. */
4003 ControlHandle ch;
4005 bar = XSCROLL_BAR (w->vertical_scroll_bar);
4006 ch = SCROLL_BAR_CONTROL_HANDLE (bar);
4008 BLOCK_INPUT;
4010 /* If already correctly positioned, do nothing. */
4011 if (XINT (bar->left) == sb_left
4012 && XINT (bar->top) == top
4013 && XINT (bar->width) == sb_width
4014 && XINT (bar->height) == height)
4015 Draw1Control (ch);
4016 else
4018 /* Clear areas not covered by the scroll bar because it's not as
4019 wide as the area reserved for it . This makes sure a
4020 previous mode line display is cleared after C-x 2 C-x 1, for
4021 example. */
4022 int area_width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
4023 XClearArea (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f),
4024 left, top, area_width, height, 0);
4026 #if 0
4027 if (sb_left + sb_width >= FRAME_PIXEL_WIDTH (f))
4028 XClearArea (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f),
4029 sb_left - 1, top, 1, height, 0);
4030 #endif
4032 HideControl (ch);
4033 MoveControl (ch, sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM, disp_top);
4034 SizeControl (ch, sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4035 disp_height);
4036 ShowControl (ch);
4038 /* Remember new settings. */
4039 XSETINT (bar->left, sb_left);
4040 XSETINT (bar->top, top);
4041 XSETINT (bar->width, sb_width);
4042 XSETINT (bar->height, height);
4045 UNBLOCK_INPUT;
4048 /* Set the scroll bar's current state, unless we're currently being
4049 dragged. */
4050 if (NILP (bar->dragging))
4052 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
4054 if (whole == 0)
4055 x_scroll_bar_set_handle (bar, 0, top_range, 0);
4056 else
4058 int start = ((double) position * top_range) / whole;
4059 int end = ((double) (position + portion) * top_range) / whole;
4060 x_scroll_bar_set_handle (bar, start, end, 0);
4066 /* The following three hooks are used when we're doing a thorough
4067 redisplay of the frame. We don't explicitly know which scroll bars
4068 are going to be deleted, because keeping track of when windows go
4069 away is a real pain - "Can you say set-window-configuration, boys
4070 and girls?" Instead, we just assert at the beginning of redisplay
4071 that *all* scroll bars are to be removed, and then save a scroll bar
4072 from the fiery pit when we actually redisplay its window. */
4074 /* Arrange for all scroll bars on FRAME to be removed at the next call
4075 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
4076 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
4078 static void
4079 XTcondemn_scroll_bars (frame)
4080 FRAME_PTR frame;
4082 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
4083 while (! NILP (FRAME_SCROLL_BARS (frame)))
4085 Lisp_Object bar;
4086 bar = FRAME_SCROLL_BARS (frame);
4087 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
4088 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
4089 XSCROLL_BAR (bar)->prev = Qnil;
4090 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
4091 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
4092 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
4097 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
4098 Note that WINDOW isn't necessarily condemned at all. */
4100 static void
4101 XTredeem_scroll_bar (window)
4102 struct window *window;
4104 struct scroll_bar *bar;
4106 /* We can't redeem this window's scroll bar if it doesn't have one. */
4107 if (NILP (window->vertical_scroll_bar))
4108 abort ();
4110 bar = XSCROLL_BAR (window->vertical_scroll_bar);
4112 /* Unlink it from the condemned list. */
4114 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
4116 if (NILP (bar->prev))
4118 /* If the prev pointer is nil, it must be the first in one of
4119 the lists. */
4120 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
4121 /* It's not condemned. Everything's fine. */
4122 return;
4123 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
4124 window->vertical_scroll_bar))
4125 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
4126 else
4127 /* If its prev pointer is nil, it must be at the front of
4128 one or the other! */
4129 abort ();
4131 else
4132 XSCROLL_BAR (bar->prev)->next = bar->next;
4134 if (! NILP (bar->next))
4135 XSCROLL_BAR (bar->next)->prev = bar->prev;
4137 bar->next = FRAME_SCROLL_BARS (f);
4138 bar->prev = Qnil;
4139 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
4140 if (! NILP (bar->next))
4141 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4145 /* Remove all scroll bars on FRAME that haven't been saved since the
4146 last call to `*condemn_scroll_bars_hook'. */
4148 static void
4149 XTjudge_scroll_bars (f)
4150 FRAME_PTR f;
4152 Lisp_Object bar, next;
4154 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
4156 /* Clear out the condemned list now so we won't try to process any
4157 more events on the hapless scroll bars. */
4158 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
4160 for (; ! NILP (bar); bar = next)
4162 struct scroll_bar *b = XSCROLL_BAR (bar);
4164 x_scroll_bar_remove (b);
4166 next = b->next;
4167 b->next = b->prev = Qnil;
4170 /* Now there should be no references to the condemned scroll bars,
4171 and they should get garbage-collected. */
4175 void
4176 activate_scroll_bars (frame)
4177 FRAME_PTR frame;
4179 Lisp_Object bar;
4180 ControlHandle ch;
4182 bar = FRAME_SCROLL_BARS (frame);
4183 while (! NILP (bar))
4185 ch = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (bar));
4186 #ifdef TARGET_API_MAC_CARBON
4187 ActivateControl (ch);
4188 #else
4189 SetControlMaximum (ch,
4190 VERTICAL_SCROLL_BAR_TOP_RANGE (frame,
4191 XINT (XSCROLL_BAR (bar)
4192 ->height)) - 1);
4193 #endif
4194 bar = XSCROLL_BAR (bar)->next;
4199 void
4200 deactivate_scroll_bars (frame)
4201 FRAME_PTR frame;
4203 Lisp_Object bar;
4204 ControlHandle ch;
4206 bar = FRAME_SCROLL_BARS (frame);
4207 while (! NILP (bar))
4209 ch = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (bar));
4210 #ifdef TARGET_API_MAC_CARBON
4211 DeactivateControl (ch);
4212 #else
4213 SetControlMaximum (ch, XINT (-1));
4214 #endif
4215 bar = XSCROLL_BAR (bar)->next;
4219 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
4220 is set to something other than NO_EVENT, it is enqueued.
4222 This may be called from a signal handler, so we have to ignore GC
4223 mark bits. */
4225 static void
4226 x_scroll_bar_handle_click (bar, part_code, er, bufp)
4227 struct scroll_bar *bar;
4228 int part_code;
4229 EventRecord *er;
4230 struct input_event *bufp;
4232 if (! GC_WINDOWP (bar->window))
4233 abort ();
4235 bufp->kind = SCROLL_BAR_CLICK_EVENT;
4236 bufp->frame_or_window = bar->window;
4237 bufp->arg = Qnil;
4239 bar->dragging = Qnil;
4241 switch (part_code)
4243 case kControlUpButtonPart:
4244 bufp->part = scroll_bar_up_arrow;
4245 break;
4246 case kControlDownButtonPart:
4247 bufp->part = scroll_bar_down_arrow;
4248 break;
4249 case kControlPageUpPart:
4250 bufp->part = scroll_bar_above_handle;
4251 break;
4252 case kControlPageDownPart:
4253 bufp->part = scroll_bar_below_handle;
4254 break;
4255 #ifdef TARGET_API_MAC_CARBON
4256 default:
4257 #else
4258 case kControlIndicatorPart:
4259 #endif
4260 if (er->what == mouseDown)
4261 bar->dragging = make_number (0);
4262 XSETVECTOR (last_mouse_scroll_bar, bar);
4263 bufp->part = scroll_bar_handle;
4264 break;
4269 /* Handle some mouse motion while someone is dragging the scroll bar.
4271 This may be called from a signal handler, so we have to ignore GC
4272 mark bits. */
4274 static void
4275 x_scroll_bar_note_movement (bar, y_pos, t)
4276 struct scroll_bar *bar;
4277 int y_pos;
4278 Time t;
4280 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
4282 last_mouse_movement_time = t;
4284 f->mouse_moved = 1;
4285 XSETVECTOR (last_mouse_scroll_bar, bar);
4287 /* If we're dragging the bar, display it. */
4288 if (! GC_NILP (bar->dragging))
4290 /* Where should the handle be now? */
4291 int new_start = y_pos - 24;
4293 if (new_start != XINT (bar->start))
4295 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
4297 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
4303 /* Return information to the user about the current position of the
4304 mouse on the scroll bar. */
4306 static void
4307 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
4308 FRAME_PTR *fp;
4309 Lisp_Object *bar_window;
4310 enum scroll_bar_part *part;
4311 Lisp_Object *x, *y;
4312 unsigned long *time;
4314 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
4315 WindowPtr wp = FrontWindow ();
4316 Point mouse_pos;
4317 struct frame *f = ((mac_output *) GetWRefCon (wp))->mFP;
4318 int win_y, top_range;
4320 #if TARGET_API_MAC_CARBON
4321 SetPort (GetWindowPort (wp));
4322 #else
4323 SetPort (wp);
4324 #endif
4326 GetMouse (&mouse_pos);
4328 win_y = mouse_pos.v - XINT (bar->top);
4329 top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
4331 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
4333 win_y -= 24;
4335 if (! NILP (bar->dragging))
4336 win_y -= XINT (bar->dragging);
4338 if (win_y < 0)
4339 win_y = 0;
4340 if (win_y > top_range)
4341 win_y = top_range;
4343 *fp = f;
4344 *bar_window = bar->window;
4346 if (! NILP (bar->dragging))
4347 *part = scroll_bar_handle;
4348 else if (win_y < XINT (bar->start))
4349 *part = scroll_bar_above_handle;
4350 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
4351 *part = scroll_bar_handle;
4352 else
4353 *part = scroll_bar_below_handle;
4355 XSETINT (*x, win_y);
4356 XSETINT (*y, top_range);
4358 f->mouse_moved = 0;
4359 last_mouse_scroll_bar = Qnil;
4361 *time = last_mouse_movement_time;
4364 /***********************************************************************
4365 Text Cursor
4366 ***********************************************************************/
4368 /* Set clipping for output in glyph row ROW. W is the window in which
4369 we operate. GC is the graphics context to set clipping in.
4371 ROW may be a text row or, e.g., a mode line. Text rows must be
4372 clipped to the interior of the window dedicated to text display,
4373 mode lines must be clipped to the whole window. */
4375 static void
4376 x_clip_to_row (w, row, gc)
4377 struct window *w;
4378 struct glyph_row *row;
4379 GC gc;
4381 struct frame *f = XFRAME (WINDOW_FRAME (w));
4382 Rect clip_rect;
4383 int window_y, window_width;
4385 window_box (w, -1, 0, &window_y, &window_width, 0);
4387 clip_rect.left = WINDOW_TO_FRAME_PIXEL_X (w, 0);
4388 clip_rect.top = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4389 clip_rect.top = max (clip_rect.top, window_y);
4390 clip_rect.right = clip_rect.left + window_width;
4391 clip_rect.bottom = clip_rect.top + row->visible_height;
4393 mac_set_clip_rectangle (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f), &clip_rect);
4397 /* Draw a hollow box cursor on window W in glyph row ROW. */
4399 static void
4400 x_draw_hollow_cursor (w, row)
4401 struct window *w;
4402 struct glyph_row *row;
4404 struct frame *f = XFRAME (WINDOW_FRAME (w));
4405 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
4406 Display *dpy = FRAME_MAC_DISPLAY (f);
4407 int x, y, wd, h;
4408 XGCValues xgcv;
4409 struct glyph *cursor_glyph;
4410 GC gc;
4412 /* Compute frame-relative coordinates from window-relative
4413 coordinates. */
4414 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
4415 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
4416 + row->ascent - w->phys_cursor_ascent);
4417 h = row->height - 1;
4419 /* Get the glyph the cursor is on. If we can't tell because
4420 the current matrix is invalid or such, give up. */
4421 cursor_glyph = get_phys_cursor_glyph (w);
4422 if (cursor_glyph == NULL)
4423 return;
4425 /* Compute the width of the rectangle to draw. If on a stretch
4426 glyph, and `x-stretch-block-cursor' is nil, don't draw a
4427 rectangle as wide as the glyph, but use a canonical character
4428 width instead. */
4429 wd = cursor_glyph->pixel_width - 1;
4430 if (cursor_glyph->type == STRETCH_GLYPH
4431 && !x_stretch_cursor_p)
4432 wd = min (FRAME_COLUMN_WIDTH (f), wd);
4434 /* The foreground of cursor_gc is typically the same as the normal
4435 background color, which can cause the cursor box to be invisible. */
4436 xgcv.foreground = f->output_data.mac->cursor_pixel;
4437 if (dpyinfo->scratch_cursor_gc)
4438 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
4439 else
4440 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_MAC_WINDOW (f),
4441 GCForeground, &xgcv);
4442 gc = dpyinfo->scratch_cursor_gc;
4444 /* Set clipping, draw the rectangle, and reset clipping again. */
4445 x_clip_to_row (w, row, gc);
4446 mac_draw_rectangle (dpy, FRAME_MAC_WINDOW (f), gc, x, y, wd, h);
4447 mac_reset_clipping (dpy, FRAME_MAC_WINDOW (f));
4451 /* Draw a bar cursor on window W in glyph row ROW.
4453 Implementation note: One would like to draw a bar cursor with an
4454 angle equal to the one given by the font property XA_ITALIC_ANGLE.
4455 Unfortunately, I didn't find a font yet that has this property set.
4456 --gerd. */
4458 static void
4459 x_draw_bar_cursor (w, row, width)
4460 struct window *w;
4461 struct glyph_row *row;
4462 int width;
4464 /* If cursor hpos is out of bounds, don't draw garbage. This can
4465 happen in mini-buffer windows when switching between echo area
4466 glyphs and mini-buffer. */
4467 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
4469 struct frame *f = XFRAME (w->frame);
4470 struct glyph *cursor_glyph;
4471 GC gc;
4472 int x;
4473 unsigned long mask;
4474 XGCValues xgcv;
4475 Display *dpy;
4476 Window window;
4478 cursor_glyph = get_phys_cursor_glyph (w);
4479 if (cursor_glyph == NULL)
4480 return;
4482 xgcv.background = f->output_data.mac->cursor_pixel;
4483 xgcv.foreground = f->output_data.mac->cursor_pixel;
4484 mask = GCForeground | GCBackground;
4485 dpy = FRAME_MAC_DISPLAY (f);
4486 window = FRAME_MAC_WINDOW (f);
4487 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
4489 if (gc)
4490 XChangeGC (dpy, gc, mask, &xgcv);
4491 else
4493 gc = XCreateGC (dpy, window, mask, &xgcv);
4494 FRAME_MAC_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
4497 if (width < 0)
4498 width = FRAME_CURSOR_WIDTH (f);
4500 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
4501 x_clip_to_row (w, row, gc);
4502 XFillRectangle (dpy, window, gc,
4504 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
4505 min (cursor_glyph->pixel_width, width),
4506 row->height);
4507 mac_reset_clipping (dpy, FRAME_MAC_WINDOW (f));
4512 /* RIF: Define cursor CURSOR on frame F. */
4514 static void
4515 mac_define_frame_cursor (f, cursor)
4516 struct frame *f;
4517 Cursor cursor;
4519 /* MAC TODO */
4523 /* RIF: Clear area on frame F. */
4525 static void
4526 mac_clear_frame_area (f, x, y, width, height)
4527 struct frame *f;
4528 int x, y, width, height;
4530 XClearArea (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f),
4531 x, y, width, height, 0);
4535 /* RIF: Draw cursor on window W. */
4537 static void
4538 mac_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
4539 struct window *w;
4540 struct glyph_row *glyph_row;
4541 int x, y;
4542 int cursor_type, cursor_width;
4543 int on_p, active_p;
4545 if (on_p)
4547 w->phys_cursor_type = cursor_type;
4548 w->phys_cursor_width = cursor_width;
4549 w->phys_cursor_on_p = 1;
4551 switch (cursor_type)
4553 case HOLLOW_BOX_CURSOR:
4554 x_draw_hollow_cursor (w, glyph_row);
4555 break;
4557 case FILLED_BOX_CURSOR:
4558 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
4559 break;
4561 case HBAR_CURSOR:
4562 /* TODO. For now, just draw bar cursor. */
4563 case BAR_CURSOR:
4564 x_draw_bar_cursor (w, glyph_row, cursor_width);
4565 break;
4567 case NO_CURSOR:
4568 break;
4570 default:
4571 abort ();
4577 /* Icons. */
4579 #if 0 /* MAC_TODO: no icon support yet. */
4581 x_bitmap_icon (f, icon)
4582 struct frame *f;
4583 Lisp_Object icon;
4585 HANDLE hicon;
4587 if (FRAME_W32_WINDOW (f) == 0)
4588 return 1;
4590 if (NILP (icon))
4591 hicon = LoadIcon (hinst, EMACS_CLASS);
4592 else if (STRINGP (icon))
4593 hicon = LoadImage (NULL, (LPCTSTR) SDATA (icon), IMAGE_ICON, 0, 0,
4594 LR_DEFAULTSIZE | LR_LOADFROMFILE);
4595 else if (SYMBOLP (icon))
4597 LPCTSTR name;
4599 if (EQ (icon, intern ("application")))
4600 name = (LPCTSTR) IDI_APPLICATION;
4601 else if (EQ (icon, intern ("hand")))
4602 name = (LPCTSTR) IDI_HAND;
4603 else if (EQ (icon, intern ("question")))
4604 name = (LPCTSTR) IDI_QUESTION;
4605 else if (EQ (icon, intern ("exclamation")))
4606 name = (LPCTSTR) IDI_EXCLAMATION;
4607 else if (EQ (icon, intern ("asterisk")))
4608 name = (LPCTSTR) IDI_ASTERISK;
4609 else if (EQ (icon, intern ("winlogo")))
4610 name = (LPCTSTR) IDI_WINLOGO;
4611 else
4612 return 1;
4614 hicon = LoadIcon (NULL, name);
4616 else
4617 return 1;
4619 if (hicon == NULL)
4620 return 1;
4622 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_BIG,
4623 (LPARAM) hicon);
4625 return 0;
4627 #endif /* MAC_TODO */
4629 /************************************************************************
4630 Handling X errors
4631 ************************************************************************/
4633 /* Display Error Handling functions not used on W32. Listing them here
4634 helps diff stay in step when comparing w32term.c with xterm.c.
4636 x_error_catcher (display, error)
4637 x_catch_errors (dpy)
4638 x_catch_errors_unwind (old_val)
4639 x_check_errors (dpy, format)
4640 x_had_errors_p (dpy)
4641 x_clear_errors (dpy)
4642 x_uncatch_errors (dpy, count)
4643 x_trace_wire ()
4644 x_connection_signal (signalnum)
4645 x_connection_closed (dpy, error_message)
4646 x_error_quitter (display, error)
4647 x_error_handler (display, error)
4648 x_io_error_quitter (display)
4653 /* Changing the font of the frame. */
4655 /* Give frame F the font named FONTNAME as its default font, and
4656 return the full name of that font. FONTNAME may be a wildcard
4657 pattern; in that case, we choose some font that fits the pattern.
4658 The return value shows which font we chose. */
4660 Lisp_Object
4661 x_new_font (f, fontname)
4662 struct frame *f;
4663 register char *fontname;
4665 struct font_info *fontp
4666 = FS_LOAD_FONT (f, 0, fontname, -1);
4668 if (!fontp)
4669 return Qnil;
4671 FRAME_FONT (f) = (XFontStruct *) (fontp->font);
4672 FRAME_BASELINE_OFFSET (f) = fontp->baseline_offset;
4673 FRAME_FONTSET (f) = -1;
4675 FRAME_COLUMN_WIDTH (f) = FONT_WIDTH (FRAME_FONT (f));
4676 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (FRAME_FONT (f));
4678 compute_fringe_widths (f, 1);
4680 /* Compute the scroll bar width in character columns. */
4681 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
4683 int wid = FRAME_COLUMN_WIDTH (f);
4684 FRAME_CONFIG_SCROLL_BAR_COLS (f)
4685 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
4687 else
4689 int wid = FRAME_COLUMN_WIDTH (f);
4690 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
4693 /* Now make the frame display the given font. */
4694 if (FRAME_MAC_WINDOW (f) != 0)
4696 XSetFont (FRAME_MAC_DISPLAY (f), f->output_data.mac->normal_gc,
4697 FRAME_FONT (f));
4698 XSetFont (FRAME_MAC_DISPLAY (f), f->output_data.mac->reverse_gc,
4699 FRAME_FONT (f));
4700 XSetFont (FRAME_MAC_DISPLAY (f), f->output_data.mac->cursor_gc,
4701 FRAME_FONT (f));
4703 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
4704 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
4707 return build_string (fontp->full_name);
4710 /* Give frame F the fontset named FONTSETNAME as its default font, and
4711 return the full name of that fontset. FONTSETNAME may be a wildcard
4712 pattern; in that case, we choose some fontset that fits the pattern.
4713 The return value shows which fontset we chose. */
4715 Lisp_Object
4716 x_new_fontset (f, fontsetname)
4717 struct frame *f;
4718 char *fontsetname;
4720 int fontset = fs_query_fontset (build_string (fontsetname), 0);
4721 Lisp_Object result;
4723 if (fontset < 0)
4724 return Qnil;
4726 if (FRAME_FONTSET (f) == fontset)
4727 /* This fontset is already set in frame F. There's nothing more
4728 to do. */
4729 return fontset_name (fontset);
4731 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
4733 if (!STRINGP (result))
4734 /* Can't load ASCII font. */
4735 return Qnil;
4737 /* Since x_new_font doesn't update any fontset information, do it now. */
4738 FRAME_FONTSET(f) = fontset;
4740 return build_string (fontsetname);
4744 /***********************************************************************
4745 TODO: W32 Input Methods
4746 ***********************************************************************/
4747 /* Listing missing functions from xterm.c helps diff stay in step.
4749 xim_destroy_callback (xim, client_data, call_data)
4750 xim_open_dpy (dpyinfo, resource_name)
4751 struct xim_inst_t
4752 xim_instantiate_callback (display, client_data, call_data)
4753 xim_initialize (dpyinfo, resource_name)
4754 xim_close_dpy (dpyinfo)
4759 /* Calculate the absolute position in frame F
4760 from its current recorded position values and gravity. */
4762 void
4763 x_calc_absolute_position (f)
4764 struct frame *f;
4766 Point pt;
4767 int flags = f->size_hint_flags;
4769 pt.h = pt.v = 0;
4771 /* Find the position of the outside upper-left corner of
4772 the inner window, with respect to the outer window. */
4773 if (f->output_data.mac->parent_desc != FRAME_MAC_DISPLAY_INFO (f)->root_window)
4775 GrafPtr savePort;
4776 GetPort (&savePort);
4778 #if TARGET_API_MAC_CARBON
4779 SetPort (GetWindowPort (FRAME_MAC_WINDOW (f)));
4780 #else
4781 SetPort (FRAME_MAC_WINDOW (f));
4782 #endif
4784 #if TARGET_API_MAC_CARBON
4786 Rect r;
4788 GetWindowPortBounds (FRAME_MAC_WINDOW (f), &r);
4789 SetPt(&pt, r.left, r.top);
4791 #else /* not TARGET_API_MAC_CARBON */
4792 SetPt(&pt, FRAME_MAC_WINDOW (f)->portRect.left, FRAME_MAC_WINDOW (f)->portRect.top);
4793 #endif /* not TARGET_API_MAC_CARBON */
4794 LocalToGlobal (&pt);
4795 SetPort (savePort);
4798 /* Treat negative positions as relative to the leftmost bottommost
4799 position that fits on the screen. */
4800 if (flags & XNegative)
4801 f->left_pos = (FRAME_MAC_DISPLAY_INFO (f)->width
4802 - 2 * f->border_width - pt.h
4803 - FRAME_PIXEL_WIDTH (f)
4804 + f->left_pos);
4805 /* NTEMACS_TODO: Subtract menubar height? */
4806 if (flags & YNegative)
4807 f->top_pos = (FRAME_MAC_DISPLAY_INFO (f)->height
4808 - 2 * f->border_width - pt.v
4809 - FRAME_PIXEL_HEIGHT (f)
4810 + f->top_pos);
4811 /* The left_pos and top_pos
4812 are now relative to the top and left screen edges,
4813 so the flags should correspond. */
4814 f->size_hint_flags &= ~ (XNegative | YNegative);
4817 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
4818 to really change the position, and 0 when calling from
4819 x_make_frame_visible (in that case, XOFF and YOFF are the current
4820 position values). It is -1 when calling from x_set_frame_parameters,
4821 which means, do adjust for borders but don't change the gravity. */
4823 void
4824 x_set_offset (f, xoff, yoff, change_gravity)
4825 struct frame *f;
4826 register int xoff, yoff;
4827 int change_gravity;
4829 int modified_top, modified_left;
4831 if (change_gravity > 0)
4833 f->top_pos = yoff;
4834 f->left_pos = xoff;
4835 f->size_hint_flags &= ~ (XNegative | YNegative);
4836 if (xoff < 0)
4837 f->size_hint_flags |= XNegative;
4838 if (yoff < 0)
4839 f->size_hint_flags |= YNegative;
4840 f->win_gravity = NorthWestGravity;
4842 x_calc_absolute_position (f);
4844 BLOCK_INPUT;
4845 x_wm_set_size_hint (f, (long) 0, 0);
4847 modified_left = f->left_pos;
4848 modified_top = f->top_pos;
4850 MoveWindow (f->output_data.mac->mWP, modified_left + 6,
4851 modified_top + 42, false);
4853 UNBLOCK_INPUT;
4856 /* Call this to change the size of frame F's x-window.
4857 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
4858 for this size change and subsequent size changes.
4859 Otherwise we leave the window gravity unchanged. */
4861 void
4862 x_set_window_size (f, change_gravity, cols, rows)
4863 struct frame *f;
4864 int change_gravity;
4865 int cols, rows;
4867 int pixelwidth, pixelheight;
4869 BLOCK_INPUT;
4871 check_frame_size (f, &rows, &cols);
4872 f->scroll_bar_actual_width
4873 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
4875 compute_fringe_widths (f, 0);
4877 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
4878 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
4880 f->win_gravity = NorthWestGravity;
4881 x_wm_set_size_hint (f, (long) 0, 0);
4883 SizeWindow (FRAME_MAC_WINDOW (f), pixelwidth, pixelheight, 0);
4885 /* Now, strictly speaking, we can't be sure that this is accurate,
4886 but the window manager will get around to dealing with the size
4887 change request eventually, and we'll hear how it went when the
4888 ConfigureNotify event gets here.
4890 We could just not bother storing any of this information here,
4891 and let the ConfigureNotify event set everything up, but that
4892 might be kind of confusing to the Lisp code, since size changes
4893 wouldn't be reported in the frame parameters until some random
4894 point in the future when the ConfigureNotify event arrives.
4896 We pass 1 for DELAY since we can't run Lisp code inside of
4897 a BLOCK_INPUT. */
4898 change_frame_size (f, rows, cols, 0, 1, 0);
4899 FRAME_PIXEL_WIDTH (f) = pixelwidth;
4900 FRAME_PIXEL_HEIGHT (f) = pixelheight;
4902 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
4903 receive in the ConfigureNotify event; if we get what we asked
4904 for, then the event won't cause the screen to become garbaged, so
4905 we have to make sure to do it here. */
4906 SET_FRAME_GARBAGED (f);
4908 XFlush (FRAME_X_DISPLAY (f));
4910 /* If cursor was outside the new size, mark it as off. */
4911 mark_window_cursors_off (XWINDOW (f->root_window));
4913 /* Clear out any recollection of where the mouse highlighting was,
4914 since it might be in a place that's outside the new frame size.
4915 Actually checking whether it is outside is a pain in the neck,
4916 so don't try--just let the highlighting be done afresh with new size. */
4917 cancel_mouse_face (f);
4919 UNBLOCK_INPUT;
4922 /* Mouse warping. */
4924 void x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y);
4926 void
4927 x_set_mouse_position (f, x, y)
4928 struct frame *f;
4929 int x, y;
4931 int pix_x, pix_y;
4933 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
4934 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
4936 if (pix_x < 0) pix_x = 0;
4937 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
4939 if (pix_y < 0) pix_y = 0;
4940 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
4942 x_set_mouse_pixel_position (f, pix_x, pix_y);
4945 void
4946 x_set_mouse_pixel_position (f, pix_x, pix_y)
4947 struct frame *f;
4948 int pix_x, pix_y;
4950 #if 0 /* MAC_TODO: CursorDeviceMoveTo is non-Carbon */
4951 BLOCK_INPUT;
4953 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
4954 0, 0, 0, 0, pix_x, pix_y);
4955 UNBLOCK_INPUT;
4956 #endif
4960 /* focus shifting, raising and lowering. */
4962 void
4963 x_focus_on_frame (f)
4964 struct frame *f;
4966 #if 0 /* This proves to be unpleasant. */
4967 x_raise_frame (f);
4968 #endif
4969 #if 0
4970 /* I don't think that the ICCCM allows programs to do things like this
4971 without the interaction of the window manager. Whatever you end up
4972 doing with this code, do it to x_unfocus_frame too. */
4973 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4974 RevertToPointerRoot, CurrentTime);
4975 #endif /* ! 0 */
4978 void
4979 x_unfocus_frame (f)
4980 struct frame *f;
4984 /* Raise frame F. */
4985 void
4986 x_raise_frame (f)
4987 struct frame *f;
4989 if (f->async_visible)
4990 SelectWindow (FRAME_MAC_WINDOW (f));
4993 /* Lower frame F. */
4994 void
4995 x_lower_frame (f)
4996 struct frame *f;
4998 if (f->async_visible)
4999 SendBehind (FRAME_MAC_WINDOW (f), nil);
5002 static void
5003 XTframe_raise_lower (f, raise_flag)
5004 FRAME_PTR f;
5005 int raise_flag;
5007 if (raise_flag)
5008 x_raise_frame (f);
5009 else
5010 x_lower_frame (f);
5013 /* Change of visibility. */
5015 /* This tries to wait until the frame is really visible.
5016 However, if the window manager asks the user where to position
5017 the frame, this will return before the user finishes doing that.
5018 The frame will not actually be visible at that time,
5019 but it will become visible later when the window manager
5020 finishes with it. */
5022 void
5023 x_make_frame_visible (f)
5024 struct frame *f;
5026 Lisp_Object type;
5027 int original_top, original_left;
5029 BLOCK_INPUT;
5031 if (! FRAME_VISIBLE_P (f))
5033 /* We test FRAME_GARBAGED_P here to make sure we don't
5034 call x_set_offset a second time
5035 if we get to x_make_frame_visible a second time
5036 before the window gets really visible. */
5037 if (! FRAME_ICONIFIED_P (f)
5038 && ! f->output_data.mac->asked_for_visible)
5039 x_set_offset (f, f->left_pos, f->top_pos, 0);
5041 f->output_data.mac->asked_for_visible = 1;
5043 ShowWindow (FRAME_MAC_WINDOW (f));
5046 XFlush (FRAME_MAC_DISPLAY (f));
5048 #if 0 /* MAC_TODO */
5049 /* Synchronize to ensure Emacs knows the frame is visible
5050 before we do anything else. We do this loop with input not blocked
5051 so that incoming events are handled. */
5053 Lisp_Object frame;
5054 int count;
5056 /* This must come after we set COUNT. */
5057 UNBLOCK_INPUT;
5059 XSETFRAME (frame, f);
5061 /* Wait until the frame is visible. Process X events until a
5062 MapNotify event has been seen, or until we think we won't get a
5063 MapNotify at all.. */
5064 for (count = input_signal_count + 10;
5065 input_signal_count < count && !FRAME_VISIBLE_P (f);)
5067 /* Force processing of queued events. */
5068 x_sync (f);
5070 /* Machines that do polling rather than SIGIO have been
5071 observed to go into a busy-wait here. So we'll fake an
5072 alarm signal to let the handler know that there's something
5073 to be read. We used to raise a real alarm, but it seems
5074 that the handler isn't always enabled here. This is
5075 probably a bug. */
5076 if (input_polling_used ())
5078 /* It could be confusing if a real alarm arrives while
5079 processing the fake one. Turn it off and let the
5080 handler reset it. */
5081 extern void poll_for_input_1 P_ ((void));
5082 int old_poll_suppress_count = poll_suppress_count;
5083 poll_suppress_count = 1;
5084 poll_for_input_1 ();
5085 poll_suppress_count = old_poll_suppress_count;
5088 /* See if a MapNotify event has been processed. */
5089 FRAME_SAMPLE_VISIBILITY (f);
5092 #endif /* MAC_TODO */
5095 /* Change from mapped state to withdrawn state. */
5097 /* Make the frame visible (mapped and not iconified). */
5099 void
5100 x_make_frame_invisible (f)
5101 struct frame *f;
5103 /* Don't keep the highlight on an invisible frame. */
5104 if (FRAME_MAC_DISPLAY_INFO (f)->x_highlight_frame == f)
5105 FRAME_MAC_DISPLAY_INFO (f)->x_highlight_frame = 0;
5107 BLOCK_INPUT;
5109 HideWindow (FRAME_MAC_WINDOW (f));
5111 /* We can't distinguish this from iconification
5112 just by the event that we get from the server.
5113 So we can't win using the usual strategy of letting
5114 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
5115 and synchronize with the server to make sure we agree. */
5116 f->visible = 0;
5117 FRAME_ICONIFIED_P (f) = 0;
5118 f->async_visible = 0;
5119 f->async_iconified = 0;
5121 UNBLOCK_INPUT;
5124 /* Change window state from mapped to iconified. */
5126 void
5127 x_iconify_frame (f)
5128 struct frame *f;
5130 /* Don't keep the highlight on an invisible frame. */
5131 if (FRAME_MAC_DISPLAY_INFO (f)->x_highlight_frame == f)
5132 FRAME_MAC_DISPLAY_INFO (f)->x_highlight_frame = 0;
5134 #if 0
5135 /* Review: Since window is still visible in dock, still allow updates? */
5136 if (f->async_iconified)
5137 return;
5138 #endif
5140 BLOCK_INPUT;
5142 CollapseWindow (FRAME_MAC_WINDOW (f), true);
5144 UNBLOCK_INPUT;
5148 /* Destroy the X window of frame F. */
5150 void
5151 x_destroy_window (f)
5152 struct frame *f;
5154 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
5156 BLOCK_INPUT;
5158 DisposeWindow (FRAME_MAC_WINDOW (f));
5160 free_frame_menubar (f);
5161 free_frame_faces (f);
5163 xfree (f->output_data.mac);
5164 f->output_data.mac = 0;
5165 if (f == dpyinfo->x_focus_frame)
5166 dpyinfo->x_focus_frame = 0;
5167 if (f == dpyinfo->x_focus_event_frame)
5168 dpyinfo->x_focus_event_frame = 0;
5169 if (f == dpyinfo->x_highlight_frame)
5170 dpyinfo->x_highlight_frame = 0;
5172 dpyinfo->reference_count--;
5174 if (f == dpyinfo->mouse_face_mouse_frame)
5176 dpyinfo->mouse_face_beg_row
5177 = dpyinfo->mouse_face_beg_col = -1;
5178 dpyinfo->mouse_face_end_row
5179 = dpyinfo->mouse_face_end_col = -1;
5180 dpyinfo->mouse_face_window = Qnil;
5181 dpyinfo->mouse_face_deferred_gc = 0;
5182 dpyinfo->mouse_face_mouse_frame = 0;
5185 UNBLOCK_INPUT;
5188 /* Setting window manager hints. */
5190 /* Set the normal size hints for the window manager, for frame F.
5191 FLAGS is the flags word to use--or 0 meaning preserve the flags
5192 that the window now has.
5193 If USER_POSITION is nonzero, we set the USPosition
5194 flag (this is useful when FLAGS is 0). */
5195 void
5196 x_wm_set_size_hint (f, flags, user_position)
5197 struct frame *f;
5198 long flags;
5199 int user_position;
5201 #if 0 /* MAC_TODO: connect this to the Appearance Manager */
5202 XSizeHints size_hints;
5204 #ifdef USE_X_TOOLKIT
5205 Arg al[2];
5206 int ac = 0;
5207 Dimension widget_width, widget_height;
5208 Window window = XtWindow (f->output_data.x->widget);
5209 #else /* not USE_X_TOOLKIT */
5210 Window window = FRAME_X_WINDOW (f);
5211 #endif /* not USE_X_TOOLKIT */
5213 /* Setting PMaxSize caused various problems. */
5214 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
5216 size_hints.x = f->left_pos;
5217 size_hints.y = f->top_pos;
5219 #ifdef USE_X_TOOLKIT
5220 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
5221 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
5222 XtGetValues (f->output_data.x->widget, al, ac);
5223 size_hints.height = widget_height;
5224 size_hints.width = widget_width;
5225 #else /* not USE_X_TOOLKIT */
5226 size_hints.height = FRAME_PIXEL_HEIGHT (f);
5227 size_hints.width = FRAME_PIXEL_WIDTH (f);
5228 #endif /* not USE_X_TOOLKIT */
5230 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
5231 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
5232 size_hints.max_width
5233 = FRAME_X_DISPLAY_INFO (f)->width - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
5234 size_hints.max_height
5235 = FRAME_X_DISPLAY_INFO (f)->height - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
5237 /* Calculate the base and minimum sizes.
5239 (When we use the X toolkit, we don't do it here.
5240 Instead we copy the values that the widgets are using, below.) */
5241 #ifndef USE_X_TOOLKIT
5243 int base_width, base_height;
5244 int min_rows = 0, min_cols = 0;
5246 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
5247 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
5249 check_frame_size (f, &min_rows, &min_cols);
5251 /* The window manager uses the base width hints to calculate the
5252 current number of rows and columns in the frame while
5253 resizing; min_width and min_height aren't useful for this
5254 purpose, since they might not give the dimensions for a
5255 zero-row, zero-column frame.
5257 We use the base_width and base_height members if we have
5258 them; otherwise, we set the min_width and min_height members
5259 to the size for a zero x zero frame. */
5261 #ifdef HAVE_X11R4
5262 size_hints.flags |= PBaseSize;
5263 size_hints.base_width = base_width;
5264 size_hints.base_height = base_height;
5265 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
5266 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
5267 #else
5268 size_hints.min_width = base_width;
5269 size_hints.min_height = base_height;
5270 #endif
5273 /* If we don't need the old flags, we don't need the old hint at all. */
5274 if (flags)
5276 size_hints.flags |= flags;
5277 goto no_read;
5279 #endif /* not USE_X_TOOLKIT */
5282 XSizeHints hints; /* Sometimes I hate X Windows... */
5283 long supplied_return;
5284 int value;
5286 #ifdef HAVE_X11R4
5287 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
5288 &supplied_return);
5289 #else
5290 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
5291 #endif
5293 #ifdef USE_X_TOOLKIT
5294 size_hints.base_height = hints.base_height;
5295 size_hints.base_width = hints.base_width;
5296 size_hints.min_height = hints.min_height;
5297 size_hints.min_width = hints.min_width;
5298 #endif
5300 if (flags)
5301 size_hints.flags |= flags;
5302 else
5304 if (value == 0)
5305 hints.flags = 0;
5306 if (hints.flags & PSize)
5307 size_hints.flags |= PSize;
5308 if (hints.flags & PPosition)
5309 size_hints.flags |= PPosition;
5310 if (hints.flags & USPosition)
5311 size_hints.flags |= USPosition;
5312 if (hints.flags & USSize)
5313 size_hints.flags |= USSize;
5317 #ifndef USE_X_TOOLKIT
5318 no_read:
5319 #endif
5321 #ifdef PWinGravity
5322 size_hints.win_gravity = f->win_gravity;
5323 size_hints.flags |= PWinGravity;
5325 if (user_position)
5327 size_hints.flags &= ~ PPosition;
5328 size_hints.flags |= USPosition;
5330 #endif /* PWinGravity */
5332 #ifdef HAVE_X11R4
5333 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
5334 #else
5335 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
5336 #endif
5337 #endif /* MAC_TODO */
5340 #if 0 /* MAC_TODO: hide application instead of iconify? */
5341 /* Used for IconicState or NormalState */
5343 void
5344 x_wm_set_window_state (f, state)
5345 struct frame *f;
5346 int state;
5348 #ifdef USE_X_TOOLKIT
5349 Arg al[1];
5351 XtSetArg (al[0], XtNinitialState, state);
5352 XtSetValues (f->output_data.x->widget, al, 1);
5353 #else /* not USE_X_TOOLKIT */
5354 Window window = FRAME_X_WINDOW (f);
5356 f->output_data.x->wm_hints.flags |= StateHint;
5357 f->output_data.x->wm_hints.initial_state = state;
5359 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
5360 #endif /* not USE_X_TOOLKIT */
5363 void
5364 x_wm_set_icon_pixmap (f, pixmap_id)
5365 struct frame *f;
5366 int pixmap_id;
5368 Pixmap icon_pixmap;
5370 #ifndef USE_X_TOOLKIT
5371 Window window = FRAME_X_WINDOW (f);
5372 #endif
5374 if (pixmap_id > 0)
5376 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
5377 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
5379 else
5381 /* It seems there is no way to turn off use of an icon pixmap.
5382 The following line does it, only if no icon has yet been created,
5383 for some window managers. But with mwm it crashes.
5384 Some people say it should clear the IconPixmapHint bit in this case,
5385 but that doesn't work, and the X consortium said it isn't the
5386 right thing at all. Since there is no way to win,
5387 best to explicitly give up. */
5388 #if 0
5389 f->output_data.x->wm_hints.icon_pixmap = None;
5390 #else
5391 return;
5392 #endif
5395 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
5398 Arg al[1];
5399 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
5400 XtSetValues (f->output_data.x->widget, al, 1);
5403 #else /* not USE_X_TOOLKIT */
5405 f->output_data.x->wm_hints.flags |= IconPixmapHint;
5406 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
5408 #endif /* not USE_X_TOOLKIT */
5411 #endif /* MAC_TODO */
5413 void
5414 x_wm_set_icon_position (f, icon_x, icon_y)
5415 struct frame *f;
5416 int icon_x, icon_y;
5418 #if 0 /* MAC_TODO: no icons on Mac */
5419 #ifdef USE_X_TOOLKIT
5420 Window window = XtWindow (f->output_data.x->widget);
5421 #else
5422 Window window = FRAME_X_WINDOW (f);
5423 #endif
5425 f->output_data.x->wm_hints.flags |= IconPositionHint;
5426 f->output_data.x->wm_hints.icon_x = icon_x;
5427 f->output_data.x->wm_hints.icon_y = icon_y;
5429 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
5430 #endif /* MAC_TODO */
5434 /***********************************************************************
5435 Fonts
5436 ***********************************************************************/
5438 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
5440 struct font_info *
5441 x_get_font_info (f, font_idx)
5442 FRAME_PTR f;
5443 int font_idx;
5445 return (FRAME_MAC_FONT_TABLE (f) + font_idx);
5448 /* the global font name table */
5449 char **font_name_table = NULL;
5450 int font_name_table_size = 0;
5451 int font_name_count = 0;
5453 /* compare two strings ignoring case */
5454 static int
5455 stricmp (const char *s, const char *t)
5457 for ( ; tolower (*s) == tolower (*t); s++, t++)
5458 if (*s == '\0')
5459 return 0;
5460 return tolower (*s) - tolower (*t);
5463 /* compare two strings ignoring case and handling wildcard */
5464 static int
5465 wildstrieq (char *s1, char *s2)
5467 if (strcmp (s1, "*") == 0 || strcmp (s2, "*") == 0)
5468 return true;
5470 return stricmp (s1, s2) == 0;
5473 /* Assume parameter 1 is fully qualified, no wildcards. */
5474 static int
5475 mac_font_pattern_match (fontname, pattern)
5476 char * fontname;
5477 char * pattern;
5479 char *regex = (char *) alloca (strlen (pattern) * 2 + 3);
5480 char *font_name_copy = (char *) alloca (strlen (fontname) + 1);
5481 char *ptr;
5483 /* Copy fontname so we can modify it during comparison. */
5484 strcpy (font_name_copy, fontname);
5486 ptr = regex;
5487 *ptr++ = '^';
5489 /* Turn pattern into a regexp and do a regexp match. */
5490 for (; *pattern; pattern++)
5492 if (*pattern == '?')
5493 *ptr++ = '.';
5494 else if (*pattern == '*')
5496 *ptr++ = '.';
5497 *ptr++ = '*';
5499 else
5500 *ptr++ = *pattern;
5502 *ptr = '$';
5503 *(ptr + 1) = '\0';
5505 return (fast_c_string_match_ignore_case (build_string (regex),
5506 font_name_copy) >= 0);
5509 /* Two font specs are considered to match if their foundry, family,
5510 weight, slant, and charset match. */
5511 static int
5512 mac_font_match (char *mf, char *xf)
5514 char m_foundry[50], m_family[50], m_weight[20], m_slant[2], m_charset[20];
5515 char x_foundry[50], x_family[50], x_weight[20], x_slant[2], x_charset[20];
5517 if (sscanf (mf, "-%49[^-]-%49[^-]-%19[^-]-%1[^-]-%*[^-]--%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*c-%*[^-]-%19s",
5518 m_foundry, m_family, m_weight, m_slant, m_charset) != 5)
5519 return mac_font_pattern_match (mf, xf);
5521 if (sscanf (xf, "-%49[^-]-%49[^-]-%19[^-]-%1[^-]-%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*c-%*[^-]-%19s",
5522 x_foundry, x_family, x_weight, x_slant, x_charset) != 5)
5523 return mac_font_pattern_match (mf, xf);
5525 return (wildstrieq (m_foundry, x_foundry)
5526 && wildstrieq (m_family, x_family)
5527 && wildstrieq (m_weight, x_weight)
5528 && wildstrieq (m_slant, x_slant)
5529 && wildstrieq (m_charset, x_charset))
5530 || mac_font_pattern_match (mf, xf);
5534 static char *
5535 mac_to_x_fontname (char *name, int size, Style style, short scriptcode)
5537 char foundry[32], family[32], cs[32];
5538 char xf[255], *result, *p;
5540 if (sscanf (name, "%31[^-]-%31[^-]-%31s", foundry, family, cs) != 3)
5542 strcpy(foundry, "Apple");
5543 strcpy(family, name);
5545 switch (scriptcode)
5547 case smTradChinese:
5548 strcpy(cs, "big5-0");
5549 break;
5550 case smSimpChinese:
5551 strcpy(cs, "gb2312.1980-0");
5552 break;
5553 case smJapanese:
5554 strcpy(cs, "jisx0208.1983-sjis");
5555 break;
5556 case -smJapanese:
5557 /* Each Apple Japanese font is entered into the font table
5558 twice: once as a jisx0208.1983-sjis font and once as a
5559 jisx0201.1976-0 font. The latter can be used to display
5560 the ascii charset and katakana-jisx0201 charset. A
5561 negative script code signals that the name of this latter
5562 font is being built. */
5563 strcpy(cs, "jisx0201.1976-0");
5564 break;
5565 case smKorean:
5566 strcpy(cs, "ksc5601.1989-0");
5567 break;
5568 default:
5569 strcpy(cs, "mac-roman");
5570 break;
5574 sprintf(xf, "-%s-%s-%s-%c-normal--%d-%d-75-75-m-%d-%s",
5575 foundry, family, style & bold ? "bold" : "medium",
5576 style & italic ? 'i' : 'r', size, size * 10, size * 10, cs);
5578 result = (char *) xmalloc (strlen (xf) + 1);
5579 strcpy (result, xf);
5580 for (p = result; *p; p++)
5581 *p = tolower(*p);
5582 return result;
5586 /* Convert an X font spec to the corresponding mac font name, which
5587 can then be passed to GetFNum after conversion to a Pascal string.
5588 For ordinary Mac fonts, this should just be their names, like
5589 "monaco", "Taipei", etc. Fonts converted from the GNU intlfonts
5590 collection contain their charset designation in their names, like
5591 "ETL-Fixed-iso8859-1", "ETL-Fixed-koi8-r", etc. Both types of font
5592 names are handled accordingly. */
5593 static void
5594 x_font_name_to_mac_font_name (char *xf, char *mf)
5596 char foundry[32], family[32], weight[20], slant[2], cs[32];
5598 strcpy (mf, "");
5600 if (sscanf (xf, "-%31[^-]-%31[^-]-%19[^-]-%1[^-]-%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*c-%*[^-]-%31s",
5601 foundry, family, weight, slant, cs) != 5 &&
5602 sscanf (xf, "-%31[^-]-%31[^-]-%19[^-]-%1[^-]-%*[^-]--%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*c-%*[^-]-%31s",
5603 foundry, family, weight, slant, cs) != 5)
5604 return;
5606 if (strcmp (cs, "big5-0") == 0 || strcmp (cs, "gb2312.1980-0") == 0
5607 || strcmp (cs, "jisx0208.1983-sjis") == 0
5608 || strcmp (cs, "jisx0201.1976-0") == 0
5609 || strcmp (cs, "ksc5601.1989-0") == 0 || strcmp (cs, "mac-roman") == 0)
5610 strcpy(mf, family);
5611 else
5612 sprintf(mf, "%s-%s-%s", foundry, family, cs);
5616 static void
5617 add_font_name_table_entry (char *font_name)
5619 if (font_name_table_size == 0)
5621 font_name_table_size = 16;
5622 font_name_table = (char **)
5623 xmalloc (font_name_table_size * sizeof (char *));
5625 else if (font_name_count + 1 >= font_name_table_size)
5627 font_name_table_size += 16;
5628 font_name_table = (char **)
5629 xrealloc (font_name_table,
5630 font_name_table_size * sizeof (char *));
5633 font_name_table[font_name_count++] = font_name;
5636 /* Sets up the table font_name_table to contain the list of all fonts
5637 in the system the first time the table is used so that the Resource
5638 Manager need not be accessed every time this information is
5639 needed. */
5641 static void
5642 init_font_name_table ()
5644 #if TARGET_API_MAC_CARBON
5645 SInt32 sv;
5647 if (Gestalt (gestaltSystemVersion, &sv) == noErr && sv >= 0x1000)
5649 FMFontFamilyIterator ffi;
5650 FMFontFamilyInstanceIterator ffii;
5651 FMFontFamily ff;
5653 /* Create a dummy instance iterator here to avoid creating and
5654 destroying it in the loop. */
5655 if (FMCreateFontFamilyInstanceIterator (0, &ffii) != noErr)
5656 return;
5657 /* Create an iterator to enumerate the font families. */
5658 if (FMCreateFontFamilyIterator (NULL, NULL, kFMDefaultOptions, &ffi)
5659 != noErr)
5661 FMDisposeFontFamilyInstanceIterator (&ffii);
5662 return;
5665 while (FMGetNextFontFamily (&ffi, &ff) == noErr)
5667 Str255 name;
5668 FMFont font;
5669 FMFontStyle style;
5670 FMFontSize size;
5671 SInt16 sc;
5673 if (FMGetFontFamilyName (ff, name) != noErr)
5674 break;
5675 p2cstr (name);
5677 sc = FontToScript (ff);
5679 /* Point the instance iterator at the current font family. */
5680 if (FMResetFontFamilyInstanceIterator(ff, &ffii) != noErr)
5681 break;
5683 while (FMGetNextFontFamilyInstance (&ffii, &font, &style, &size)
5684 == noErr)
5685 if (size == 0)
5687 add_font_name_table_entry (mac_to_x_fontname (name, size,
5688 style, sc));
5689 add_font_name_table_entry (mac_to_x_fontname (name, size,
5690 italic, sc));
5691 add_font_name_table_entry (mac_to_x_fontname (name, size,
5692 bold, sc));
5693 add_font_name_table_entry (mac_to_x_fontname (name, size,
5694 italic | bold,
5695 sc));
5697 else
5699 add_font_name_table_entry (mac_to_x_fontname (name, size,
5700 style, sc));
5701 if (smJapanese == sc)
5702 add_font_name_table_entry (mac_to_x_fontname (name, size,
5703 style,
5704 -smJapanese));
5708 /* Dispose of the iterators. */
5709 FMDisposeFontFamilyIterator (&ffi);
5710 FMDisposeFontFamilyInstanceIterator (&ffii);
5712 else
5714 #endif /* TARGET_API_MAC_CARBON */
5715 GrafPtr port;
5716 SInt16 fontnum, old_fontnum;
5717 int num_mac_fonts = CountResources('FOND');
5718 int i, j;
5719 Handle font_handle, font_handle_2;
5720 short id, scriptcode;
5721 ResType type;
5722 Str32 name;
5723 struct FontAssoc *fat;
5724 struct AsscEntry *assc_entry;
5726 GetPort (&port); /* save the current font number used */
5727 #if TARGET_API_MAC_CARBON
5728 old_fontnum = GetPortTextFont (port);
5729 #else
5730 old_fontnum = port->txFont;
5731 #endif
5733 for (i = 1; i <= num_mac_fonts; i++) /* get all available fonts */
5735 font_handle = GetIndResource ('FOND', i);
5736 if (!font_handle)
5737 continue;
5739 GetResInfo (font_handle, &id, &type, name);
5740 GetFNum (name, &fontnum);
5741 p2cstr (name);
5742 if (fontnum == 0)
5743 continue;
5745 TextFont (fontnum);
5746 scriptcode = FontToScript (fontnum);
5749 HLock (font_handle);
5751 if (GetResourceSizeOnDisk (font_handle)
5752 >= sizeof (struct FamRec))
5754 fat = (struct FontAssoc *) (*font_handle
5755 + sizeof (struct FamRec));
5756 assc_entry
5757 = (struct AsscEntry *) (*font_handle
5758 + sizeof (struct FamRec)
5759 + sizeof (struct FontAssoc));
5761 for (j = 0; j <= fat->numAssoc; j++, assc_entry++)
5763 if (font_name_table_size == 0)
5765 font_name_table_size = 16;
5766 font_name_table = (char **)
5767 xmalloc (font_name_table_size * sizeof (char *));
5769 else if (font_name_count >= font_name_table_size)
5771 font_name_table_size += 16;
5772 font_name_table = (char **)
5773 xrealloc (font_name_table,
5774 font_name_table_size * sizeof (char *));
5776 font_name_table[font_name_count++]
5777 = mac_to_x_fontname (name,
5778 assc_entry->fontSize,
5779 assc_entry->fontStyle,
5780 scriptcode);
5781 /* Both jisx0208.1983-sjis and
5782 jisx0201.1976-sjis parts are contained in
5783 Apple Japanese (SJIS) font. */
5784 if (smJapanese == scriptcode)
5786 font_name_table[font_name_count++]
5787 = mac_to_x_fontname (name,
5788 assc_entry->fontSize,
5789 assc_entry->fontStyle,
5790 -smJapanese);
5795 HUnlock (font_handle);
5796 font_handle_2 = GetNextFOND (font_handle);
5797 ReleaseResource (font_handle);
5798 font_handle = font_handle_2;
5800 while (ResError () == noErr && font_handle);
5803 TextFont (old_fontnum);
5804 #if TARGET_API_MAC_CARBON
5806 #endif /* TARGET_API_MAC_CARBON */
5810 /* Return a list of at most MAXNAMES font specs matching the one in
5811 PATTERN. Cache matching fonts for patterns in
5812 dpyinfo->name_list_element to avoid looking them up again by
5813 calling mac_font_pattern_match (slow). Return as many matching
5814 fonts as possible if MAXNAMES = -1. */
5816 Lisp_Object
5817 x_list_fonts (struct frame *f,
5818 Lisp_Object pattern,
5819 int size,
5820 int maxnames)
5822 char *ptnstr;
5823 Lisp_Object newlist = Qnil, tem, key;
5824 int n_fonts = 0;
5825 int i;
5826 struct gcpro gcpro1, gcpro2;
5827 struct mac_display_info *dpyinfo = f ? FRAME_MAC_DISPLAY_INFO (f) : NULL;
5829 if (font_name_table == NULL) /* Initialize when first used. */
5830 init_font_name_table ();
5832 if (dpyinfo)
5834 tem = XCDR (dpyinfo->name_list_element);
5835 key = Fcons (pattern, make_number (maxnames));
5837 newlist = Fassoc (key, tem);
5838 if (!NILP (newlist))
5840 newlist = Fcdr_safe (newlist);
5841 goto label_cached;
5845 ptnstr = SDATA (pattern);
5847 GCPRO2 (pattern, newlist);
5849 /* Scan and matching bitmap fonts. */
5850 for (i = 0; i < font_name_count; i++)
5852 if (mac_font_pattern_match (font_name_table[i], ptnstr))
5854 newlist = Fcons (build_string (font_name_table[i]), newlist);
5856 n_fonts++;
5857 if (maxnames > 0 && n_fonts >= maxnames)
5858 break;
5862 /* MAC_TODO: add code for matching outline fonts here */
5864 UNGCPRO;
5866 if (dpyinfo)
5868 XSETCDR (dpyinfo->name_list_element,
5869 Fcons (Fcons (key, newlist),
5870 XCDR (dpyinfo->name_list_element)));
5872 label_cached:
5874 return newlist;
5878 #if GLYPH_DEBUG
5880 /* Check that FONT is valid on frame F. It is if it can be found in F's
5881 font table. */
5883 static void
5884 x_check_font (f, font)
5885 struct frame *f;
5886 XFontStruct *font;
5888 int i;
5889 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
5891 xassert (font != NULL);
5893 for (i = 0; i < dpyinfo->n_fonts; i++)
5894 if (dpyinfo->font_table[i].name
5895 && font == dpyinfo->font_table[i].font)
5896 break;
5898 xassert (i < dpyinfo->n_fonts);
5901 #endif /* GLYPH_DEBUG != 0 */
5903 /* Set *W to the minimum width, *H to the minimum font height of FONT.
5904 Note: There are (broken) X fonts out there with invalid XFontStruct
5905 min_bounds contents. For example, handa@etl.go.jp reports that
5906 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
5907 have font->min_bounds.width == 0. */
5909 static INLINE void
5910 x_font_min_bounds (font, w, h)
5911 MacFontStruct *font;
5912 int *w, *h;
5915 * TODO: Windows does not appear to offer min bound, only
5916 * average and maximum width, and maximum height.
5918 *h = FONT_HEIGHT (font);
5919 *w = FONT_WIDTH (font);
5923 /* Compute the smallest character width and smallest font height over
5924 all fonts available on frame F. Set the members smallest_char_width
5925 and smallest_font_height in F's x_display_info structure to
5926 the values computed. Value is non-zero if smallest_font_height or
5927 smallest_char_width become smaller than they were before. */
5930 x_compute_min_glyph_bounds (f)
5931 struct frame *f;
5933 int i;
5934 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
5935 MacFontStruct *font;
5936 int old_width = dpyinfo->smallest_char_width;
5937 int old_height = dpyinfo->smallest_font_height;
5939 dpyinfo->smallest_font_height = 100000;
5940 dpyinfo->smallest_char_width = 100000;
5942 for (i = 0; i < dpyinfo->n_fonts; ++i)
5943 if (dpyinfo->font_table[i].name)
5945 struct font_info *fontp = dpyinfo->font_table + i;
5946 int w, h;
5948 font = (MacFontStruct *) fontp->font;
5949 xassert (font != (MacFontStruct *) ~0);
5950 x_font_min_bounds (font, &w, &h);
5952 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
5953 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
5956 xassert (dpyinfo->smallest_char_width > 0
5957 && dpyinfo->smallest_font_height > 0);
5959 return (dpyinfo->n_fonts == 1
5960 || dpyinfo->smallest_char_width < old_width
5961 || dpyinfo->smallest_font_height < old_height);
5965 /* Determine whether given string is a fully-specified XLFD: all 14
5966 fields are present, none is '*'. */
5968 static int
5969 is_fully_specified_xlfd (char *p)
5971 int i;
5972 char *q;
5974 if (*p != '-')
5975 return 0;
5977 for (i = 0; i < 13; i++)
5979 q = strchr (p + 1, '-');
5980 if (q == NULL)
5981 return 0;
5982 if (q - p == 2 && *(p + 1) == '*')
5983 return 0;
5984 p = q;
5987 if (strchr (p + 1, '-') != NULL)
5988 return 0;
5990 if (*(p + 1) == '*' && *(p + 2) == '\0')
5991 return 0;
5993 return 1;
5997 const int kDefaultFontSize = 9;
6000 /* XLoadQueryFont creates and returns an internal representation for a
6001 font in a MacFontStruct struct. There is really no concept
6002 corresponding to "loading" a font on the Mac. But we check its
6003 existence and find the font number and all other information for it
6004 and store them in the returned MacFontStruct. */
6006 static MacFontStruct *
6007 XLoadQueryFont (Display *dpy, char *fontname)
6009 int i, size, is_two_byte_font, char_width;
6010 char *name;
6011 GrafPtr port;
6012 SInt16 old_fontnum, old_fontsize;
6013 Style old_fontface;
6014 Str32 mfontname;
6015 SInt16 fontnum;
6016 Style fontface = normal;
6017 MacFontStruct *font;
6018 FontInfo the_fontinfo;
6019 char s_weight[7], c_slant;
6021 if (is_fully_specified_xlfd (fontname))
6022 name = fontname;
6023 else
6025 for (i = 0; i < font_name_count; i++)
6026 if (mac_font_pattern_match (font_name_table[i], fontname))
6027 break;
6029 if (i >= font_name_count)
6030 return NULL;
6032 name = font_name_table[i];
6035 GetPort (&port); /* save the current font number used */
6036 #if TARGET_API_MAC_CARBON
6037 old_fontnum = GetPortTextFont (port);
6038 old_fontsize = GetPortTextSize (port);
6039 old_fontface = GetPortTextFace (port);
6040 #else
6041 old_fontnum = port->txFont;
6042 old_fontsize = port->txSize;
6043 old_fontface = port->txFace;
6044 #endif
6046 if (sscanf (name, "-%*[^-]-%*[^-]-%*[^-]-%*c-%*[^-]--%d-%*[^-]-%*[^-]-%*[^-]-%*c-%*[^-]-%*s", &size) != 1)
6047 size = kDefaultFontSize;
6049 if (sscanf (name, "-%*[^-]-%*[^-]-%6[^-]-%*c-%*[^-]--%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*c-%*[^-]-%*s", s_weight) == 1)
6050 if (strcmp (s_weight, "bold") == 0)
6051 fontface |= bold;
6053 if (sscanf (name, "-%*[^-]-%*[^-]-%*[^-]-%c-%*[^-]--%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*c-%*[^-]-%*s", &c_slant) == 1)
6054 if (c_slant == 'i')
6055 fontface |= italic;
6057 x_font_name_to_mac_font_name (name, mfontname);
6058 c2pstr (mfontname);
6059 GetFNum (mfontname, &fontnum);
6060 if (fontnum == 0)
6061 return NULL;
6063 font = (MacFontStruct *) xmalloc (sizeof (struct MacFontStruct));
6065 font->fontname = (char *) xmalloc (strlen (name) + 1);
6066 bcopy (name, font->fontname, strlen (name) + 1);
6068 font->mac_fontnum = fontnum;
6069 font->mac_fontsize = size;
6070 font->mac_fontface = fontface;
6071 font->mac_scriptcode = FontToScript (fontnum);
6073 /* Apple Japanese (SJIS) font is listed as both
6074 "*-jisx0208.1983-sjis" (Japanese script) and "*-jisx0201.1976-0"
6075 (Roman script) in init_font_name_table (). The latter should be
6076 treated as a one-byte font. */
6078 char cs[32];
6080 if (sscanf (name,
6081 "-%*[^-]-%*[^-]-%*[^-]-%*c-%*[^-]--%*[^-]-%*[^-]-%*[^-]-%*[^-]-%*c-%*[^-]-%31s",
6082 cs) == 1
6083 && 0 == strcmp (cs, "jisx0201.1976-0"))
6084 font->mac_scriptcode = smRoman;
6087 is_two_byte_font = font->mac_scriptcode == smJapanese ||
6088 font->mac_scriptcode == smTradChinese ||
6089 font->mac_scriptcode == smSimpChinese ||
6090 font->mac_scriptcode == smKorean;
6092 TextFont (fontnum);
6093 TextSize (size);
6094 TextFace (fontface);
6096 GetFontInfo (&the_fontinfo);
6098 font->ascent = the_fontinfo.ascent;
6099 font->descent = the_fontinfo.descent;
6101 font->min_byte1 = 0;
6102 if (is_two_byte_font)
6103 font->max_byte1 = 1;
6104 else
6105 font->max_byte1 = 0;
6106 font->min_char_or_byte2 = 0x20;
6107 font->max_char_or_byte2 = 0xff;
6109 if (is_two_byte_font)
6111 /* Use the width of an "ideographic space" of that font because
6112 the_fontinfo.widMax returns the wrong width for some fonts. */
6113 switch (font->mac_scriptcode)
6115 case smJapanese:
6116 char_width = StringWidth("\p\x81\x40");
6117 break;
6118 case smTradChinese:
6119 char_width = StringWidth("\p\xa1\x40");
6120 break;
6121 case smSimpChinese:
6122 char_width = StringWidth("\p\xa1\xa1");
6123 break;
6124 case smKorean:
6125 char_width = StringWidth("\p\xa1\xa1");
6126 break;
6129 else
6130 /* Do this instead of use the_fontinfo.widMax, which incorrectly
6131 returns 15 for 12-point Monaco! */
6132 char_width = CharWidth ('m');
6134 font->max_bounds.rbearing = char_width;
6135 font->max_bounds.lbearing = 0;
6136 font->max_bounds.width = char_width;
6137 font->max_bounds.ascent = the_fontinfo.ascent;
6138 font->max_bounds.descent = the_fontinfo.descent;
6140 font->min_bounds = font->max_bounds;
6142 if (is_two_byte_font || CharWidth ('m') == CharWidth ('i'))
6143 font->per_char = NULL;
6144 else
6146 font->per_char = (XCharStruct *)
6147 xmalloc (sizeof (XCharStruct) * (0xff - 0x20 + 1));
6149 int c;
6151 for (c = 0x20; c <= 0xff; c++)
6153 font->per_char[c - 0x20] = font->max_bounds;
6154 font->per_char[c - 0x20].width = CharWidth (c);
6159 TextFont (old_fontnum); /* restore previous font number, size and face */
6160 TextSize (old_fontsize);
6161 TextFace (old_fontface);
6163 return font;
6167 /* Load font named FONTNAME of the size SIZE for frame F, and return a
6168 pointer to the structure font_info while allocating it dynamically.
6169 If SIZE is 0, load any size of font.
6170 If loading is failed, return NULL. */
6172 struct font_info *
6173 x_load_font (f, fontname, size)
6174 struct frame *f;
6175 register char *fontname;
6176 int size;
6178 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
6179 Lisp_Object font_names;
6181 /* Get a list of all the fonts that match this name. Once we
6182 have a list of matching fonts, we compare them against the fonts
6183 we already have by comparing names. */
6184 font_names = x_list_fonts (f, build_string (fontname), size, 1);
6186 if (!NILP (font_names))
6188 Lisp_Object tail;
6189 int i;
6191 for (i = 0; i < dpyinfo->n_fonts; i++)
6192 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
6193 if (dpyinfo->font_table[i].name
6194 && (!strcmp (dpyinfo->font_table[i].name,
6195 SDATA (XCAR (tail)))
6196 || !strcmp (dpyinfo->font_table[i].full_name,
6197 SDATA (XCAR (tail)))))
6198 return (dpyinfo->font_table + i);
6201 /* Load the font and add it to the table. */
6203 char *full_name;
6204 struct MacFontStruct *font;
6205 struct font_info *fontp;
6206 unsigned long value;
6207 int i;
6209 /* If we have found fonts by x_list_font, load one of them. If
6210 not, we still try to load a font by the name given as FONTNAME
6211 because XListFonts (called in x_list_font) of some X server has
6212 a bug of not finding a font even if the font surely exists and
6213 is loadable by XLoadQueryFont. */
6214 if (size > 0 && !NILP (font_names))
6215 fontname = (char *) SDATA (XCAR (font_names));
6217 font = (MacFontStruct *) XLoadQueryFont (FRAME_MAC_DISPLAY (f), fontname);
6218 if (!font)
6219 return NULL;
6221 /* Find a free slot in the font table. */
6222 for (i = 0; i < dpyinfo->n_fonts; ++i)
6223 if (dpyinfo->font_table[i].name == NULL)
6224 break;
6226 /* If no free slot found, maybe enlarge the font table. */
6227 if (i == dpyinfo->n_fonts
6228 && dpyinfo->n_fonts == dpyinfo->font_table_size)
6230 int sz;
6231 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
6232 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
6233 dpyinfo->font_table
6234 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
6237 fontp = dpyinfo->font_table + i;
6238 if (i == dpyinfo->n_fonts)
6239 ++dpyinfo->n_fonts;
6241 /* Now fill in the slots of *FONTP. */
6242 BLOCK_INPUT;
6243 bzero (fontp, sizeof (*fontp));
6244 fontp->font = font;
6245 fontp->font_idx = i;
6246 fontp->name = (char *) xmalloc (strlen (font->fontname) + 1);
6247 bcopy (font->fontname, fontp->name, strlen (font->fontname) + 1);
6249 fontp->full_name = fontp->name;
6251 fontp->size = font->max_bounds.width;
6252 fontp->height = FONT_HEIGHT (font);
6254 /* For some font, ascent and descent in max_bounds field is
6255 larger than the above value. */
6256 int max_height = font->max_bounds.ascent + font->max_bounds.descent;
6257 if (max_height > fontp->height)
6258 fontp->height = max_height;
6261 /* The slot `encoding' specifies how to map a character
6262 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
6263 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
6264 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
6265 2:0xA020..0xFF7F). For the moment, we don't know which charset
6266 uses this font. So, we set information in fontp->encoding[1]
6267 which is never used by any charset. If mapping can't be
6268 decided, set FONT_ENCODING_NOT_DECIDED. */
6269 if (font->mac_scriptcode == smJapanese)
6270 fontp->encoding[1] = 4;
6271 else
6273 fontp->encoding[1]
6274 = (font->max_byte1 == 0
6275 /* 1-byte font */
6276 ? (font->min_char_or_byte2 < 0x80
6277 ? (font->max_char_or_byte2 < 0x80
6278 ? 0 /* 0x20..0x7F */
6279 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
6280 : 1) /* 0xA0..0xFF */
6281 /* 2-byte font */
6282 : (font->min_byte1 < 0x80
6283 ? (font->max_byte1 < 0x80
6284 ? (font->min_char_or_byte2 < 0x80
6285 ? (font->max_char_or_byte2 < 0x80
6286 ? 0 /* 0x2020..0x7F7F */
6287 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
6288 : 3) /* 0x20A0..0x7FFF */
6289 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
6290 : (font->min_char_or_byte2 < 0x80
6291 ? (font->max_char_or_byte2 < 0x80
6292 ? 2 /* 0xA020..0xFF7F */
6293 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
6294 : 1))); /* 0xA0A0..0xFFFF */
6297 #if 0 /* MAC_TODO: fill these out with more reasonably values */
6298 fontp->baseline_offset
6299 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
6300 ? (long) value : 0);
6301 fontp->relative_compose
6302 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
6303 ? (long) value : 0);
6304 fontp->default_ascent
6305 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
6306 ? (long) value : 0);
6307 #else
6308 fontp->baseline_offset = 0;
6309 fontp->relative_compose = 0;
6310 fontp->default_ascent = 0;
6311 #endif
6313 /* Set global flag fonts_changed_p to non-zero if the font loaded
6314 has a character with a smaller width than any other character
6315 before, or if the font loaded has a smalle>r height than any
6316 other font loaded before. If this happens, it will make a
6317 glyph matrix reallocation necessary. */
6318 fonts_changed_p = x_compute_min_glyph_bounds (f);
6319 UNBLOCK_INPUT;
6320 return fontp;
6325 /* Return a pointer to struct font_info of a font named FONTNAME for
6326 frame F. If no such font is loaded, return NULL. */
6328 struct font_info *
6329 x_query_font (f, fontname)
6330 struct frame *f;
6331 register char *fontname;
6333 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
6334 int i;
6336 for (i = 0; i < dpyinfo->n_fonts; i++)
6337 if (dpyinfo->font_table[i].name
6338 && (!strcmp (dpyinfo->font_table[i].name, fontname)
6339 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
6340 return (dpyinfo->font_table + i);
6341 return NULL;
6345 /* Find a CCL program for a font specified by FONTP, and set the member
6346 `encoder' of the structure. */
6348 void
6349 x_find_ccl_program (fontp)
6350 struct font_info *fontp;
6352 Lisp_Object list, elt;
6354 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
6356 elt = XCAR (list);
6357 if (CONSP (elt)
6358 && STRINGP (XCAR (elt))
6359 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
6360 >= 0))
6361 break;
6363 if (! NILP (list))
6365 struct ccl_program *ccl
6366 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
6368 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
6369 xfree (ccl);
6370 else
6371 fontp->font_encoder = ccl;
6377 /***********************************************************************
6378 Initialization
6379 ***********************************************************************/
6381 #ifdef USE_X_TOOLKIT
6382 static XrmOptionDescRec emacs_options[] = {
6383 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
6384 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
6386 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
6387 XrmoptionSepArg, NULL},
6388 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
6390 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
6391 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
6392 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
6393 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
6394 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
6395 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
6396 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
6398 #endif /* USE_X_TOOLKIT */
6400 static int x_initialized;
6402 #ifdef MULTI_KBOARD
6403 /* Test whether two display-name strings agree up to the dot that separates
6404 the screen number from the server number. */
6405 static int
6406 same_x_server (name1, name2)
6407 char *name1, *name2;
6409 int seen_colon = 0;
6410 unsigned char *system_name = SDATA (Vsystem_name);
6411 int system_name_length = strlen (system_name);
6412 int length_until_period = 0;
6414 while (system_name[length_until_period] != 0
6415 && system_name[length_until_period] != '.')
6416 length_until_period++;
6418 /* Treat `unix' like an empty host name. */
6419 if (! strncmp (name1, "unix:", 5))
6420 name1 += 4;
6421 if (! strncmp (name2, "unix:", 5))
6422 name2 += 4;
6423 /* Treat this host's name like an empty host name. */
6424 if (! strncmp (name1, system_name, system_name_length)
6425 && name1[system_name_length] == ':')
6426 name1 += system_name_length;
6427 if (! strncmp (name2, system_name, system_name_length)
6428 && name2[system_name_length] == ':')
6429 name2 += system_name_length;
6430 /* Treat this host's domainless name like an empty host name. */
6431 if (! strncmp (name1, system_name, length_until_period)
6432 && name1[length_until_period] == ':')
6433 name1 += length_until_period;
6434 if (! strncmp (name2, system_name, length_until_period)
6435 && name2[length_until_period] == ':')
6436 name2 += length_until_period;
6438 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
6440 if (*name1 == ':')
6441 seen_colon++;
6442 if (seen_colon && *name1 == '.')
6443 return 1;
6445 return (seen_colon
6446 && (*name1 == '.' || *name1 == '\0')
6447 && (*name2 == '.' || *name2 == '\0'));
6449 #endif
6452 /* The Mac Event loop code */
6454 #ifndef MAC_OSX
6455 #include <Events.h>
6456 #include <Quickdraw.h>
6457 #include <Balloons.h>
6458 #include <Devices.h>
6459 #include <Fonts.h>
6460 #include <Gestalt.h>
6461 #include <Menus.h>
6462 #include <Processes.h>
6463 #include <Sound.h>
6464 #include <ToolUtils.h>
6465 #include <TextUtils.h>
6466 #include <Dialogs.h>
6467 #include <Script.h>
6468 #include <Types.h>
6469 #include <TextEncodingConverter.h>
6470 #include <Resources.h>
6472 #if __MWERKS__
6473 #include <unix.h>
6474 #endif
6475 #endif /* ! MAC_OSX */
6477 #define M_APPLE 128
6478 #define I_ABOUT 1
6480 #define WINDOW_RESOURCE 128
6481 #define TERM_WINDOW_RESOURCE 129
6483 #define DEFAULT_NUM_COLS 80
6485 #define MIN_DOC_SIZE 64
6486 #define MAX_DOC_SIZE 32767
6488 /* sleep time for WaitNextEvent */
6489 #define WNE_SLEEP_AT_SUSPEND 10
6490 #define WNE_SLEEP_AT_RESUME 1
6492 /* true when cannot handle any Mac OS events */
6493 static int handling_window_update = 0;
6495 /* the flag appl_is_suspended is used both for determining the sleep
6496 time to be passed to WaitNextEvent and whether the cursor should be
6497 drawn when updating the display. The cursor is turned off when
6498 Emacs is suspended. Redrawing it is unnecessary and what needs to
6499 be done depends on whether the cursor lies inside or outside the
6500 redraw region. So we might as well skip drawing it when Emacs is
6501 suspended. */
6502 static Boolean app_is_suspended = false;
6503 static long app_sleep_time = WNE_SLEEP_AT_RESUME;
6505 #define EXTRA_STACK_ALLOC (256 * 1024)
6507 #define ARGV_STRING_LIST_ID 129
6508 #define ABOUT_ALERT_ID 128
6509 #define RAM_TOO_LARGE_ALERT_ID 129
6511 Boolean terminate_flag = false;
6513 /* True if using command key as meta key. */
6514 Lisp_Object Vmac_command_key_is_meta;
6516 /* True if the ctrl and meta keys should be reversed. */
6517 Lisp_Object Vmac_reverse_ctrl_meta;
6519 #if USE_CARBON_EVENTS
6520 /* True if the mouse wheel button (i.e. button 4) should map to
6521 mouse-2, instead of mouse-3. */
6522 Lisp_Object Vmac_wheel_button_is_mouse_2;
6524 /* If Non-nil, the Mac "Command" key is passed on to the Mac Toolbox
6525 for processing before Emacs sees it. */
6526 Lisp_Object Vmac_pass_command_to_system;
6528 /* If Non-nil, the Mac "Control" key is passed on to the Mac Toolbox
6529 for processing before Emacs sees it. */
6530 Lisp_Object Vmac_pass_control_to_system;
6531 #endif
6533 /* convert input from Mac keyboard (assumed to be in Mac Roman coding)
6534 to this text encoding */
6535 int mac_keyboard_text_encoding;
6536 int current_mac_keyboard_text_encoding = kTextEncodingMacRoman;
6538 /* Set in term/mac-win.el to indicate that event loop can now generate
6539 drag and drop events. */
6540 Lisp_Object Qmac_ready_for_drag_n_drop;
6542 Lisp_Object drag_and_drop_file_list;
6544 Point saved_menu_event_location;
6546 /* Apple Events */
6547 static void init_required_apple_events (void);
6548 static pascal OSErr
6549 do_ae_open_application (const AppleEvent *, AppleEvent *, long);
6550 static pascal OSErr
6551 do_ae_print_documents (const AppleEvent *, AppleEvent *, long);
6552 static pascal OSErr do_ae_open_documents (AppleEvent *, AppleEvent *, long);
6553 static pascal OSErr do_ae_quit_application (AppleEvent *, AppleEvent *, long);
6555 /* Drag and Drop */
6556 static OSErr init_mac_drag_n_drop ();
6557 static pascal OSErr mac_do_receive_drag (WindowPtr, void*, DragReference);
6559 #if USE_CARBON_EVENTS
6560 /* Preliminary Support for the OSX Services Menu */
6561 static OSStatus mac_handle_service_event (EventHandlerCallRef,EventRef,void*);
6562 static void init_service_handler ();
6563 #endif
6565 extern void init_emacs_passwd_dir ();
6566 extern int emacs_main (int, char **, char **);
6567 extern void check_alarm ();
6569 extern void initialize_applescript();
6570 extern void terminate_applescript();
6572 static unsigned int
6573 #if USE_CARBON_EVENTS
6574 mac_to_emacs_modifiers (UInt32 mods)
6575 #else
6576 mac_to_emacs_modifiers (EventModifiers mods)
6577 #endif
6579 unsigned int result = 0;
6580 if (mods & macShiftKey)
6581 result |= shift_modifier;
6582 if (mods & macCtrlKey)
6583 result |= ctrl_modifier;
6584 if (mods & macMetaKey)
6585 result |= meta_modifier;
6586 if (NILP (Vmac_command_key_is_meta) && (mods & macAltKey))
6587 result |= alt_modifier;
6588 return result;
6591 #if USE_CARBON_EVENTS
6592 /* Obtains the event modifiers from the event ref and then calls
6593 mac_to_emacs_modifiers. */
6594 static int
6595 mac_event_to_emacs_modifiers (EventRef eventRef)
6597 UInt32 mods = 0;
6598 GetEventParameter (eventRef, kEventParamKeyModifiers, typeUInt32, NULL,
6599 sizeof (UInt32), NULL, &mods);
6600 return mac_to_emacs_modifiers (mods);
6603 /* Given an event ref, return the code to use for the mouse button
6604 code in the emacs input_event. */
6605 static int
6606 mac_get_mouse_btn (EventRef ref)
6608 EventMouseButton result = kEventMouseButtonPrimary;
6609 GetEventParameter (ref, kEventParamMouseButton, typeMouseButton, NULL,
6610 sizeof (EventMouseButton), NULL, &result);
6611 switch (result)
6613 case kEventMouseButtonPrimary:
6614 return 0;
6615 case kEventMouseButtonSecondary:
6616 return NILP (Vmac_wheel_button_is_mouse_2) ? 1 : 2;
6617 case kEventMouseButtonTertiary:
6618 case 4: /* 4 is the number for the mouse wheel button */
6619 return NILP (Vmac_wheel_button_is_mouse_2) ? 2 : 1;
6620 default:
6621 return 0;
6625 /* Normally, ConvertEventRefToEventRecord will correctly handle all
6626 events. However the click of the mouse wheel is not converted to a
6627 mouseDown or mouseUp event. This calls ConvertEventRef, but then
6628 checks to see if it is a mouse up or down carbon event that has not
6629 been converted, and if so, converts it by hand (to be picked up in
6630 the XTread_socket loop). */
6631 static Boolean mac_convert_event_ref (EventRef eventRef, EventRecord *eventRec)
6633 Boolean result = ConvertEventRefToEventRecord (eventRef, eventRec);
6634 /* Do special case for mouse wheel button. */
6635 if (!result && GetEventClass (eventRef) == kEventClassMouse)
6637 UInt32 kind = GetEventKind (eventRef);
6638 if (kind == kEventMouseDown && !(eventRec->what == mouseDown))
6640 eventRec->what = mouseDown;
6641 result=1;
6643 if (kind == kEventMouseUp && !(eventRec->what == mouseUp))
6645 eventRec->what = mouseUp;
6646 result=1;
6648 if (result)
6650 /* Need where and when. */
6651 UInt32 mods;
6652 GetEventParameter (eventRef, kEventParamMouseLocation,
6653 typeQDPoint, NULL, sizeof (Point),
6654 NULL, &eventRec->where);
6655 /* Use two step process because new event modifiers are
6656 32-bit and old are 16-bit. Currently, only loss is
6657 NumLock & Fn. */
6658 GetEventParameter (eventRef, kEventParamKeyModifiers,
6659 typeUInt32, NULL, sizeof (UInt32),
6660 NULL, &mods);
6661 eventRec->modifiers = mods;
6663 eventRec->when = EventTimeToTicks (GetEventTime (eventRef));
6666 return result;
6669 #endif
6671 static void
6672 do_get_menus (void)
6674 Handle menubar_handle;
6675 MenuHandle menu_handle;
6677 menubar_handle = GetNewMBar (128);
6678 if(menubar_handle == NULL)
6679 abort ();
6680 SetMenuBar (menubar_handle);
6681 DrawMenuBar ();
6683 menu_handle = GetMenuHandle (M_APPLE);
6684 if(menu_handle != NULL)
6685 AppendResMenu (menu_handle,'DRVR');
6686 else
6687 abort ();
6691 static void
6692 do_init_managers (void)
6694 #if !TARGET_API_MAC_CARBON
6695 InitGraf (&qd.thePort);
6696 InitFonts ();
6697 FlushEvents (everyEvent, 0);
6698 InitWindows ();
6699 InitMenus ();
6700 TEInit ();
6701 InitDialogs (NULL);
6702 #endif /* !TARGET_API_MAC_CARBON */
6703 InitCursor ();
6705 #if !TARGET_API_MAC_CARBON
6706 /* set up some extra stack space for use by emacs */
6707 SetApplLimit ((Ptr) ((long) GetApplLimit () - EXTRA_STACK_ALLOC));
6709 /* MaxApplZone must be called for AppleScript to execute more
6710 complicated scripts */
6711 MaxApplZone ();
6712 MoreMasters ();
6713 #endif /* !TARGET_API_MAC_CARBON */
6716 static void
6717 do_check_ram_size (void)
6719 SInt32 physical_ram_size, logical_ram_size;
6721 if (Gestalt (gestaltPhysicalRAMSize, &physical_ram_size) != noErr
6722 || Gestalt (gestaltLogicalRAMSize, &logical_ram_size) != noErr
6723 || physical_ram_size > 256 * 1024 * 1024
6724 || logical_ram_size > 256 * 1024 * 1024)
6726 StopAlert (RAM_TOO_LARGE_ALERT_ID, NULL);
6727 exit (1);
6731 static void
6732 do_window_update (WindowPtr win)
6734 struct mac_output *mwp = (mac_output *) GetWRefCon (win);
6735 struct frame *f = mwp->mFP;
6737 if (f)
6739 if (f->async_visible == 0)
6741 f->async_visible = 1;
6742 f->async_iconified = 0;
6743 SET_FRAME_GARBAGED (f);
6745 /* An update event is equivalent to MapNotify on X, so report
6746 visibility changes properly. */
6747 if (! NILP(Vframe_list) && ! NILP (XCDR (Vframe_list)))
6748 /* Force a redisplay sooner or later to update the
6749 frame titles in case this is the second frame. */
6750 record_asynch_buffer_change ();
6752 else
6754 BeginUpdate (win);
6755 handling_window_update = 1;
6757 XClearWindow (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f));
6759 expose_frame (f, 0, 0, 0, 0);
6761 handling_window_update = 0;
6762 EndUpdate (win);
6767 static int
6768 is_emacs_window (WindowPtr win)
6770 Lisp_Object tail, frame;
6772 if (!win)
6773 return 0;
6775 FOR_EACH_FRAME (tail, frame)
6776 if (FRAME_MAC_P (XFRAME (frame)))
6777 if (FRAME_MAC_WINDOW (XFRAME (frame)) == win)
6778 return 1;
6780 return 0;
6783 static void
6784 do_window_activate (WindowPtr win)
6786 mac_output *mwp;
6787 struct frame *f;
6789 if (is_emacs_window (win))
6791 mwp = (mac_output *) GetWRefCon (win);
6792 f = mwp->mFP;
6794 if (f)
6796 x_new_focus_frame (FRAME_MAC_DISPLAY_INFO (f), f);
6797 activate_scroll_bars (f);
6802 static void
6803 do_window_deactivate (WindowPtr win)
6805 mac_output *mwp;
6806 struct frame *f;
6808 if (is_emacs_window (win))
6810 mwp = (mac_output *) GetWRefCon (win);
6811 f = mwp->mFP;
6813 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
6815 x_new_focus_frame (FRAME_MAC_DISPLAY_INFO (f), 0);
6816 deactivate_scroll_bars (f);
6821 static void
6822 do_app_resume ()
6824 WindowPtr wp;
6825 mac_output *mwp;
6826 struct frame *f;
6828 wp = FrontWindow();
6829 if (is_emacs_window (wp))
6831 mwp = (mac_output *) GetWRefCon (wp);
6832 f = mwp->mFP;
6834 if (f)
6836 x_new_focus_frame (FRAME_MAC_DISPLAY_INFO (f), f);
6837 activate_scroll_bars (f);
6841 app_is_suspended = false;
6842 app_sleep_time = WNE_SLEEP_AT_RESUME;
6845 static void
6846 do_app_suspend ()
6848 WindowPtr wp;
6849 mac_output *mwp;
6850 struct frame *f;
6852 wp = FrontWindow();
6853 if (is_emacs_window (wp))
6855 mwp = (mac_output *) GetWRefCon (wp);
6856 f = mwp->mFP;
6858 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
6860 x_new_focus_frame (FRAME_MAC_DISPLAY_INFO (f), 0);
6861 deactivate_scroll_bars (f);
6865 app_is_suspended = true;
6866 app_sleep_time = WNE_SLEEP_AT_SUSPEND;
6870 static void
6871 do_mouse_moved (Point mouse_pos)
6873 WindowPtr wp = FrontWindow ();
6874 struct frame *f;
6876 if (is_emacs_window (wp))
6878 f = ((mac_output *) GetWRefCon (wp))->mFP;
6880 #if TARGET_API_MAC_CARBON
6881 SetPort (GetWindowPort (wp));
6882 #else
6883 SetPort (wp);
6884 #endif
6886 GlobalToLocal (&mouse_pos);
6888 note_mouse_movement (f, &mouse_pos);
6893 static void
6894 do_os_event (EventRecord *erp)
6896 switch((erp->message >> 24) & 0x000000FF)
6898 case suspendResumeMessage:
6899 if((erp->message & resumeFlag) == 1)
6900 do_app_resume ();
6901 else
6902 do_app_suspend ();
6903 break;
6905 case mouseMovedMessage:
6906 do_mouse_moved (erp->where);
6907 break;
6911 static void
6912 do_events (EventRecord *erp)
6914 switch (erp->what)
6916 case updateEvt:
6917 do_window_update ((WindowPtr) erp->message);
6918 break;
6920 case osEvt:
6921 do_os_event (erp);
6922 break;
6924 case activateEvt:
6925 if ((erp->modifiers & activeFlag) != 0)
6926 do_window_activate ((WindowPtr) erp->message);
6927 else
6928 do_window_deactivate ((WindowPtr) erp->message);
6929 break;
6933 static void
6934 do_apple_menu (SInt16 menu_item)
6936 #if !TARGET_API_MAC_CARBON
6937 Str255 item_name;
6938 SInt16 da_driver_refnum;
6940 if (menu_item == I_ABOUT)
6941 NoteAlert (ABOUT_ALERT_ID, NULL);
6942 else
6944 GetMenuItemText (GetMenuHandle (M_APPLE), menu_item, item_name);
6945 da_driver_refnum = OpenDeskAcc (item_name);
6947 #endif /* !TARGET_API_MAC_CARBON */
6950 void
6951 do_menu_choice (SInt32 menu_choice)
6953 SInt16 menu_id, menu_item;
6955 menu_id = HiWord (menu_choice);
6956 menu_item = LoWord (menu_choice);
6958 if (menu_id == 0)
6959 return;
6961 switch (menu_id)
6963 case M_APPLE:
6964 do_apple_menu (menu_item);
6965 break;
6967 default:
6969 WindowPtr wp = FrontWindow ();
6970 struct frame *f = ((mac_output *) GetWRefCon (wp))->mFP;
6971 MenuHandle menu = GetMenuHandle (menu_id);
6972 if (menu)
6974 UInt32 refcon;
6976 GetMenuItemRefCon (menu, menu_item, &refcon);
6977 menubar_selection_callback (f, refcon);
6982 HiliteMenu (0);
6986 /* Handle drags in size box. Based on code contributed by Ben
6987 Mesander and IM - Window Manager A. */
6989 static void
6990 do_grow_window (WindowPtr w, EventRecord *e)
6992 long grow_size;
6993 Rect limit_rect;
6994 int rows, columns;
6995 mac_output *mwp = (mac_output *) GetWRefCon (w);
6996 struct frame *f = mwp->mFP;
6998 SetRect(&limit_rect, MIN_DOC_SIZE, MIN_DOC_SIZE, MAX_DOC_SIZE, MAX_DOC_SIZE);
7000 grow_size = GrowWindow (w, e->where, &limit_rect);
7002 /* see if it really changed size */
7003 if (grow_size != 0)
7005 rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, HiWord (grow_size));
7006 columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, LoWord (grow_size));
7008 x_set_window_size (f, 0, columns, rows);
7013 /* Handle clicks in zoom box. Calculation of "standard state" based
7014 on code in IM - Window Manager A and code contributed by Ben
7015 Mesander. The standard state of an Emacs window is 80-characters
7016 wide (DEFAULT_NUM_COLS) and as tall as will fit on the screen. */
7018 static void
7019 do_zoom_window (WindowPtr w, int zoom_in_or_out)
7021 GrafPtr save_port;
7022 Rect zoom_rect, port_rect;
7023 Point top_left;
7024 int w_title_height, columns, rows, width, height, dummy, x, y;
7025 mac_output *mwp = (mac_output *) GetWRefCon (w);
7026 struct frame *f = mwp->mFP;
7028 GetPort (&save_port);
7030 #if TARGET_API_MAC_CARBON
7031 SetPort (GetWindowPort (w));
7032 #else
7033 SetPort (w);
7034 #endif
7036 /* Clear window to avoid flicker. */
7037 #if TARGET_API_MAC_CARBON
7039 Rect r;
7040 BitMap bm;
7042 GetWindowPortBounds (w, &r);
7043 EraseRect (&r);
7045 if (zoom_in_or_out == inZoomOut)
7047 /* calculate height of window's title bar (hard card it for now). */
7048 w_title_height = 20 + GetMBarHeight ();
7050 /* get maximum height of window into zoom_rect.bottom -
7051 zoom_rect.top */
7052 GetQDGlobalsScreenBits (&bm);
7053 zoom_rect = bm.bounds;
7054 zoom_rect.top += w_title_height;
7055 InsetRect (&zoom_rect, 8, 4); /* not too tight */
7057 zoom_rect.right = zoom_rect.left
7058 + FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, DEFAULT_NUM_COLS);
7060 SetWindowStandardState (w, &zoom_rect);
7063 #else /* not TARGET_API_MAC_CARBON */
7064 EraseRect (&(w->portRect));
7065 if (zoom_in_or_out == inZoomOut)
7067 SetPt (&top_left, w->portRect.left, w->portRect.top);
7068 LocalToGlobal (&top_left);
7070 /* calculate height of window's title bar */
7071 w_title_height = top_left.v - 1
7072 - (**((WindowPeek) w)->strucRgn).rgnBBox.top + GetMBarHeight ();
7074 /* get maximum height of window into zoom_rect.bottom - zoom_rect.top */
7075 zoom_rect = qd.screenBits.bounds;
7076 zoom_rect.top += w_title_height;
7077 InsetRect (&zoom_rect, 8, 4); /* not too tight */
7079 zoom_rect.right = zoom_rect.left
7080 + FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, DEFAULT_NUM_COLS);
7082 (**((WStateDataHandle) ((WindowPeek) w)->dataHandle)).stdState
7083 = zoom_rect;
7085 #endif /* not TARGET_API_MAC_CARBON */
7087 ZoomWindow (w, zoom_in_or_out, w == FrontWindow ());
7089 /* retrieve window size and update application values */
7090 #if TARGET_API_MAC_CARBON
7091 GetWindowPortBounds (w, &port_rect);
7092 #else
7093 port_rect = w->portRect;
7094 #endif
7095 rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, port_rect.bottom - port_rect.top);
7096 columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, port_rect.right - port_rect.left);
7097 x_set_window_size (mwp->mFP, 0, columns, rows);
7099 SetPort (save_port);
7102 /* Initialize Drag And Drop to allow files to be dropped onto emacs frames */
7103 static OSErr
7104 init_mac_drag_n_drop ()
7106 OSErr result = InstallReceiveHandler (mac_do_receive_drag, 0L, NULL);
7107 return result;
7110 /* Intialize AppleEvent dispatcher table for the required events. */
7111 void
7112 init_required_apple_events ()
7114 OSErr err;
7115 long result;
7117 /* Make sure we have apple events before starting. */
7118 err = Gestalt (gestaltAppleEventsAttr, &result);
7119 if (err != noErr)
7120 abort ();
7122 if (!(result & (1 << gestaltAppleEventsPresent)))
7123 abort ();
7125 #if TARGET_API_MAC_CARBON
7126 err = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
7127 NewAEEventHandlerUPP
7128 ((AEEventHandlerProcPtr) do_ae_open_application),
7129 0L, false);
7130 #else
7131 err = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
7132 NewAEEventHandlerProc
7133 ((AEEventHandlerProcPtr) do_ae_open_application),
7134 0L, false);
7135 #endif
7136 if (err != noErr)
7137 abort ();
7139 #if TARGET_API_MAC_CARBON
7140 err = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
7141 NewAEEventHandlerUPP
7142 ((AEEventHandlerProcPtr) do_ae_open_documents),
7143 0L, false);
7144 #else
7145 err = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
7146 NewAEEventHandlerProc
7147 ((AEEventHandlerProcPtr) do_ae_open_documents),
7148 0L, false);
7149 #endif
7150 if (err != noErr)
7151 abort ();
7153 #if TARGET_API_MAC_CARBON
7154 err = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
7155 NewAEEventHandlerUPP
7156 ((AEEventHandlerProcPtr) do_ae_print_documents),
7157 0L, false);
7158 #else
7159 err = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
7160 NewAEEventHandlerProc
7161 ((AEEventHandlerProcPtr) do_ae_print_documents),
7162 0L, false);
7163 #endif
7164 if (err != noErr)
7165 abort ();
7167 #if TARGET_API_MAC_CARBON
7168 err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
7169 NewAEEventHandlerUPP
7170 ((AEEventHandlerProcPtr) do_ae_quit_application),
7171 0L, false);
7172 #else
7173 err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
7174 NewAEEventHandlerProc
7175 ((AEEventHandlerProcPtr) do_ae_quit_application),
7176 0L, false);
7177 #endif
7178 if (err != noErr)
7179 abort ();
7182 #if USE_CARBON_EVENTS
7184 void
7185 init_service_handler ()
7187 EventTypeSpec specs[] = {{kEventClassService, kEventServiceGetTypes},
7188 {kEventClassService, kEventServiceCopy},
7189 {kEventClassService, kEventServicePaste}};
7190 InstallApplicationEventHandler (NewEventHandlerUPP (mac_handle_service_event),
7191 3, specs, NULL, NULL);
7195 MAC_TODO: Check to see if this is called by AEProcessDesc...
7197 OSStatus
7198 mac_handle_service_event (EventHandlerCallRef callRef,
7199 EventRef event, void *data)
7201 OSStatus err = noErr;
7202 switch (GetEventKind (event))
7204 case kEventServiceGetTypes:
7206 CFMutableArrayRef copyTypes, pasteTypes;
7207 CFStringRef type;
7208 Boolean selection = true;
7210 GetEventParameter(event, kEventParamServicePasteTypes,
7211 typeCFMutableArrayRef, NULL,
7212 sizeof (CFMutableArrayRef), NULL, &pasteTypes);
7214 GetEventParameter(event, kEventParamServiceCopyTypes,
7215 typeCFMutableArrayRef, NULL,
7216 sizeof (CFMutableArrayRef), NULL, &copyTypes);
7217 type = CreateTypeStringWithOSType (kScrapFlavorTypeText);
7218 if (type) {
7219 CFArrayAppendValue (copyTypes, type);
7220 //CFArrayAppendValue (pasteTypes, type);
7221 CFRelease (type);
7224 case kEventServiceCopy:
7226 ScrapRef currentScrap, specificScrap;
7227 char * buf = "";
7228 Size byteCount = 0;
7230 GetCurrentScrap (&currentScrap);
7232 err = GetScrapFlavorSize (currentScrap, kScrapFlavorTypeText, &byteCount);
7233 if (err == noErr)
7235 void *buffer = xmalloc (byteCount);
7236 if (buffer != NULL)
7238 GetEventParameter (event, kEventParamScrapRef, typeScrapRef, NULL,
7239 sizeof (ScrapRef), NULL, &specificScrap);
7241 err = GetScrapFlavorData (currentScrap, kScrapFlavorTypeText,
7242 &byteCount, buffer);
7243 if (err == noErr)
7244 PutScrapFlavor (specificScrap, kScrapFlavorTypeText,
7245 kScrapFlavorMaskNone, byteCount, buffer);
7246 xfree (buffer);
7249 err = noErr;
7251 case kEventServicePaste:
7254 // Get the current location
7255 Size byteCount;
7256 ScrapRef specificScrap;
7257 GetEventParameter(event, kEventParamScrapRef, typeScrapRef, NULL,
7258 sizeof(ScrapRef), NULL, &specificScrap);
7259 err = GetScrapFlavorSize(specificScrap, kScrapFlavorTypeText, &byteCount);
7260 if (err == noErr) {
7261 void * buffer = xmalloc(byteCount);
7262 if (buffer != NULL ) {
7263 err = GetScrapFlavorData(specificScrap, kScrapFlavorTypeText,
7264 &byteCount, buffer);
7265 if (err == noErr) {
7266 // Actually place in the buffer
7267 BLOCK_INPUT;
7268 // Get the current "selection" string here
7269 UNBLOCK_INPUT;
7272 xfree(buffer);
7277 return err;
7279 #endif
7281 /* Open Application Apple Event */
7282 static pascal OSErr
7283 do_ae_open_application(const AppleEvent *pae, AppleEvent *preply, long prefcon)
7285 return noErr;
7289 /* Defined in mac.c. */
7290 extern int
7291 path_from_vol_dir_name (char *, int, short, long, char *);
7294 /* Called when we receive an AppleEvent with an ID of
7295 "kAEOpenDocuments". This routine gets the direct parameter,
7296 extracts the FSSpecs in it, and puts their names on a list. */
7297 static pascal OSErr
7298 do_ae_open_documents(AppleEvent *message, AppleEvent *reply, long refcon)
7300 OSErr err, err2;
7301 AEDesc the_desc;
7302 AEKeyword keyword;
7303 DescType actual_type;
7304 Size actual_size;
7306 err = AEGetParamDesc (message, keyDirectObject, typeAEList, &the_desc);
7307 if (err != noErr)
7308 goto descriptor_error_exit;
7310 /* Check to see that we got all of the required parameters from the
7311 event descriptor. For an 'odoc' event this should just be the
7312 file list. */
7313 err = AEGetAttributePtr(message, keyMissedKeywordAttr, typeWildCard,
7314 &actual_type, (Ptr) &keyword,
7315 sizeof (keyword), &actual_size);
7316 /* No error means that we found some unused parameters.
7317 errAEDescNotFound means that there are no more parameters. If we
7318 get an error code other than that, flag it. */
7319 if ((err == noErr) || (err != errAEDescNotFound))
7321 err = errAEEventNotHandled;
7322 goto error_exit;
7324 err = noErr;
7326 /* Got all the parameters we need. Now, go through the direct
7327 object list and parse it up. */
7329 long num_files_to_open;
7331 err = AECountItems (&the_desc, &num_files_to_open);
7332 if (err == noErr)
7334 int i;
7336 /* AE file list is one based so just use that for indexing here. */
7337 for (i = 1; (err == noErr) && (i <= num_files_to_open); i++)
7339 FSSpec fs;
7340 Str255 path_name, unix_path_name;
7341 #ifdef MAC_OSX
7342 FSRef fref;
7343 #endif
7345 err = AEGetNthPtr(&the_desc, i, typeFSS, &keyword, &actual_type,
7346 (Ptr) &fs, sizeof (fs), &actual_size);
7347 if (err != noErr) break;
7349 #ifdef MAC_OSX
7350 err = FSpMakeFSRef (&fs, &fref);
7351 if (err != noErr) break;
7353 if (FSRefMakePath (&fref, unix_path_name, 255) == noErr)
7354 #else
7355 if (path_from_vol_dir_name (path_name, 255, fs.vRefNum, fs.parID,
7356 fs.name) &&
7357 mac_to_posix_pathname (path_name, unix_path_name, 255))
7358 #endif
7359 drag_and_drop_file_list = Fcons (build_string (unix_path_name),
7360 drag_and_drop_file_list);
7365 error_exit:
7366 /* Nuke the coerced file list in any case */
7367 err2 = AEDisposeDesc(&the_desc);
7369 descriptor_error_exit:
7370 /* InvalRect(&(gFrontMacWindowP->mWP->portRect)); */
7371 return err;
7375 static pascal OSErr
7376 mac_do_receive_drag (WindowPtr window, void *handlerRefCon,
7377 DragReference theDrag)
7379 short items;
7380 short index;
7381 FlavorFlags theFlags;
7382 Point mouse;
7383 OSErr result;
7384 ItemReference theItem;
7385 HFSFlavor data;
7386 FSRef fref;
7387 Size size = sizeof (HFSFlavor);
7389 drag_and_drop_file_list = Qnil;
7390 GetDragMouse (theDrag, &mouse, 0L);
7391 CountDragItems (theDrag, &items);
7392 for (index = 1; index <= items; index++)
7394 /* Only handle file references. */
7395 GetDragItemReferenceNumber (theDrag, index, &theItem);
7396 result = GetFlavorFlags (theDrag, theItem, flavorTypeHFS, &theFlags);
7397 if (result == noErr)
7399 #ifdef MAC_OSX
7400 FSRef frref;
7401 #else
7402 Str255 path_name;
7403 #endif
7404 Str255 unix_path_name;
7405 GetFlavorData (theDrag, theItem, flavorTypeHFS, &data, &size, 0L);
7406 #ifdef MAC_OSX
7407 /* Use Carbon routines, otherwise it converts the file name
7408 to /Macintosh HD/..., which is not correct. */
7409 FSpMakeFSRef (&data.fileSpec, &fref);
7410 if (! FSRefMakePath (&fref, unix_path_name, sizeof (unix_path_name)));
7411 #else
7412 if (path_from_vol_dir_name (path_name, 255, data.fileSpec.vRefNum,
7413 data.fileSpec.parID, data.fileSpec.name) &&
7414 mac_to_posix_pathname (path_name, unix_path_name, 255))
7415 #endif
7416 drag_and_drop_file_list = Fcons (build_string (unix_path_name),
7417 drag_and_drop_file_list);
7419 else
7420 return;
7422 /* If there are items in the list, construct an event and post it to
7423 the queue like an interrupt using kbd_buffer_store_event. */
7424 if (!NILP (drag_and_drop_file_list))
7426 struct input_event event;
7427 Lisp_Object frame;
7428 struct frame *f = ((mac_output *) GetWRefCon(window))->mFP;
7429 SetPort (GetWindowPort (window));
7430 GlobalToLocal (&mouse);
7432 event.kind = DRAG_N_DROP_EVENT;
7433 event.code = 0;
7434 event.modifiers = 0;
7435 event.timestamp = TickCount () * (1000 / 60);
7436 XSETINT (event.x, mouse.h);
7437 XSETINT (event.y, mouse.v);
7438 XSETFRAME (frame, f);
7439 event.frame_or_window = Fcons (frame, drag_and_drop_file_list);
7440 event.arg = Qnil;
7441 /* Post to the interrupt queue */
7442 kbd_buffer_store_event (&event);
7443 /* MAC_TODO: Mimic behavior of windows by switching contexts to Emacs */
7445 ProcessSerialNumber psn;
7446 GetCurrentProcess (&psn);
7447 SetFrontProcess (&psn);
7453 /* Print Document Apple Event */
7454 static pascal OSErr
7455 do_ae_print_documents (const AppleEvent *pAE, AppleEvent *reply, long refcon)
7457 return errAEEventNotHandled;
7461 static pascal OSErr
7462 do_ae_quit_application (AppleEvent* message, AppleEvent *reply, long refcon)
7464 /* FixMe: Do we need an unwind-protect or something here? And what
7465 do we do about unsaved files. Currently just forces quit rather
7466 than doing recursive callback to get user input. */
7468 terminate_flag = true;
7470 /* Fkill_emacs doesn't return. We have to return. (TI) */
7471 return noErr;
7475 #if __profile__
7476 void
7477 profiler_exit_proc ()
7479 ProfilerDump ("\pEmacs.prof");
7480 ProfilerTerm ();
7482 #endif
7484 /* These few functions implement Emacs as a normal Mac application
7485 (almost): set up the heap and the Toolbox, handle necessary
7486 system events plus a few simple menu events. They also set up
7487 Emacs's access to functions defined in the rest of this file.
7488 Emacs uses function hooks to perform all its terminal I/O. A
7489 complete list of these functions appear in termhooks.h. For what
7490 they do, read the comments there and see also w32term.c and
7491 xterm.c. What's noticeably missing here is the event loop, which
7492 is normally present in most Mac application. After performing the
7493 necessary Mac initializations, main passes off control to
7494 emacs_main (corresponding to main in emacs.c). Emacs_main calls
7495 mac_read_socket (defined further below) to read input. This is
7496 where WaitNextEvent is called to process Mac events. This is also
7497 where check_alarm in sysdep.c is called to simulate alarm signals.
7498 This makes the cursor jump back to its correct position after
7499 briefly jumping to that of the matching parenthesis, print useful
7500 hints and prompts in the minibuffer after the user stops typing for
7501 a wait, etc. */
7503 #if !TARGET_API_MAC_CARBON
7504 #undef main
7506 main (void)
7508 #if __profile__ /* is the profiler on? */
7509 if (ProfilerInit(collectDetailed, bestTimeBase, 5000, 200))
7510 exit(1);
7511 #endif
7513 #if __MWERKS__
7514 /* set creator and type for files created by MSL */
7515 _fcreator = 'EMAx';
7516 _ftype = 'TEXT';
7517 #endif
7519 do_init_managers ();
7521 do_get_menus ();
7523 do_check_ram_size ();
7525 init_emacs_passwd_dir ();
7527 init_environ ();
7529 initialize_applescript ();
7531 init_required_apple_events ();
7534 char **argv;
7535 int argc = 0;
7537 /* set up argv array from STR# resource */
7538 get_string_list (&argv, ARGV_STRING_LIST_ID);
7539 while (argv[argc])
7540 argc++;
7542 /* free up AppleScript resources on exit */
7543 atexit (terminate_applescript);
7545 #if __profile__ /* is the profiler on? */
7546 atexit (profiler_exit_proc);
7547 #endif
7549 /* 3rd param "envp" never used in emacs_main */
7550 (void) emacs_main (argc, argv, 0);
7553 /* Never reached - real exit in Fkill_emacs */
7554 return 0;
7556 #endif
7558 /* Table for translating Mac keycode to X keysym values. Contributed
7559 by Sudhir Shenoy. */
7560 static unsigned char keycode_to_xkeysym_table[] = {
7561 /*0x00*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7562 /*0x10*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7563 /*0x20*/ 0, 0, 0, 0, 0x0d /*return*/, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7565 /*0x30*/ 0x09 /*tab*/, 0 /*0x0020 space*/, 0, 0x08 /*backspace*/,
7566 /*0x34*/ 0, 0x1b /*escape*/, 0, 0,
7567 /*0x38*/ 0, 0, 0, 0,
7568 /*0x3C*/ 0, 0, 0, 0,
7570 /*0x40*/ 0, 0xae /*kp-.*/, 0, 0xaa /*kp-**/,
7571 /*0x44*/ 0, 0xab /*kp-+*/, 0, 0x7f /*kp-clear*/,
7572 /*0x48*/ 0, 0, 0, 0xaf /*kp-/*/,
7573 /*0x4C*/ 0x8d /*kp-enter*/, 0, 0xad /*kp--*/, 0,
7575 /*0x50*/ 0, 0xbd /*kp-=*/, 0xb0 /*kp-0*/, 0xb1 /*kp-1*/,
7576 /*0x54*/ 0xb2 /*kp-2*/, 0xb3 /*kp-3*/, 0xb4 /*kp-4*/, 0xb5 /*kp-5*/,
7577 /*0x58*/ 0xb6 /*kp-6*/, 0xb7 /*kp-7*/, 0, 0xb8 /*kp-8*/,
7578 /*0x5C*/ 0xb9 /*kp-9*/, 0, 0, 0,
7580 /*0x60*/ 0xc2 /*f5*/, 0xc3 /*f6*/, 0xc4 /*f7*/, 0xc0 /*f3*/,
7581 /*0x64*/ 0xc5 /*f8*/, 0xc6 /*f9*/, 0, 0xc8 /*f11*/,
7582 /*0x68*/ 0, 0xca /*f13*/, 0, 0xcb /*f14*/,
7583 /*0x6C*/ 0, 0xc7 /*f10*/, 0, 0xc9 /*f12*/,
7585 /*0x70*/ 0, 0xcc /*f15*/, 0x9e /*insert (or 0x6a==help)*/, 0x95 /*home*/,
7586 /*0x74*/ 0x9a /*pgup*/, 0x9f /*delete*/, 0xc1 /*f4*/, 0x9c /*end*/,
7587 /*0x78*/ 0xbf /*f2*/, 0x9b /*pgdown*/, 0xbe /*f1*/, 0x51 /*left*/,
7588 /*0x7C*/ 0x53 /*right*/, 0x54 /*down*/, 0x52 /*up*/, 0
7591 static int
7592 keycode_to_xkeysym (int keyCode, int *xKeySym)
7594 *xKeySym = keycode_to_xkeysym_table [keyCode & 0x7f];
7595 return *xKeySym != 0;
7598 /* Emacs calls this whenever it wants to read an input event from the
7599 user. */
7601 XTread_socket (int sd, struct input_event *bufp, int numchars, int expected)
7603 int count = 0;
7604 #if USE_CARBON_EVENTS
7605 OSStatus rneResult;
7606 EventRef eventRef;
7607 EventMouseButton mouseBtn;
7608 #endif
7609 EventRecord er;
7610 int the_modifiers;
7611 EventMask event_mask;
7613 #if 0
7614 if (interrupt_input_blocked)
7616 interrupt_input_pending = 1;
7617 return -1;
7619 #endif
7621 interrupt_input_pending = 0;
7622 BLOCK_INPUT;
7624 /* So people can tell when we have read the available input. */
7625 input_signal_count++;
7627 if (numchars <= 0)
7628 abort ();
7630 /* Don't poll for events to process (specifically updateEvt) if
7631 window update currently already in progress. A call to redisplay
7632 (in do_window_update) can be preempted by another call to
7633 redisplay, causing blank regions to be left on the screen and the
7634 cursor to be left at strange places. */
7635 if (handling_window_update)
7637 UNBLOCK_INPUT;
7638 return 0;
7641 if (terminate_flag)
7642 Fkill_emacs (make_number (1));
7644 /* It is necessary to set this (additional) argument slot of an
7645 event to nil because keyboard.c protects incompletely processed
7646 event from being garbage collected by placing them in the
7647 kbd_buffer_gcpro vector. */
7648 bufp->arg = Qnil;
7650 event_mask = everyEvent;
7651 if (NILP (Fboundp (Qmac_ready_for_drag_n_drop)))
7652 event_mask -= highLevelEventMask;
7654 #if USE_CARBON_EVENTS
7655 rneResult = ReceiveNextEvent (0, NULL,
7656 expected
7657 ? TicksToEventTime (app_sleep_time)
7658 : 0,
7659 kEventRemoveFromQueue, &eventRef);
7660 if (!rneResult)
7662 /* Handle new events */
7663 if (!mac_convert_event_ref (eventRef, &er))
7664 switch (GetEventClass (eventRef))
7666 case kEventClassMouse:
7667 if (GetEventKind (eventRef) == kEventMouseWheelMoved)
7669 SInt32 delta;
7670 Point point;
7671 WindowPtr window_ptr = FrontNonFloatingWindow ();
7672 struct mac_output *mwp = (mac_output *) GetWRefCon (window_ptr);
7673 if (!IsValidWindowPtr (window_ptr))
7675 SysBeep(1);
7676 UNBLOCK_INPUT;
7677 return 0;
7680 GetEventParameter(eventRef, kEventParamMouseWheelDelta,
7681 typeSInt32, NULL, sizeof (SInt32),
7682 NULL, &delta);
7683 GetEventParameter(eventRef, kEventParamMouseLocation,
7684 typeQDPoint, NULL, sizeof (Point),
7685 NULL, &point);
7686 bufp->kind = WHEEL_EVENT;
7687 bufp->code = 0;
7688 bufp->modifiers = (mac_event_to_emacs_modifiers(eventRef)
7689 | ((delta < 0) ? down_modifier
7690 : up_modifier));
7691 SetPort (GetWindowPort (window_ptr));
7692 GlobalToLocal (&point);
7693 XSETINT (bufp->x, point.h);
7694 XSETINT (bufp->y, point.v);
7695 XSETFRAME (bufp->frame_or_window, mwp->mFP);
7696 bufp->timestamp = EventTimeToTicks (GetEventTime (eventRef))*(1000/60);
7697 count++;
7699 else
7700 SendEventToEventTarget (eventRef, GetEventDispatcherTarget ());
7702 break;
7703 default:
7704 /* Send the event to the appropriate receiver. */
7705 SendEventToEventTarget (eventRef, GetEventDispatcherTarget ());
7707 else
7708 #else
7709 if (WaitNextEvent (event_mask, &er, (expected ? app_sleep_time : 0L), NULL))
7710 #endif /* USE_CARBON_EVENTS */
7711 switch (er.what)
7713 case mouseDown:
7714 case mouseUp:
7716 WindowPtr window_ptr = FrontWindow ();
7717 SInt16 part_code;
7719 #if USE_CARBON_EVENTS
7720 /* This is needed to send mouse events like aqua window buttons
7721 to the correct handler. */
7722 if (eventNotHandledErr != SendEventToEventTarget (eventRef, GetEventDispatcherTarget ())) {
7723 break;
7726 if (!is_emacs_window(window_ptr))
7727 break;
7728 #endif
7730 if (mouse_tracking_in_progress == mouse_tracking_scroll_bar
7731 && er.what == mouseUp)
7733 struct mac_output *mwp = (mac_output *) GetWRefCon (window_ptr);
7734 Point mouse_loc = er.where;
7736 /* Convert to local coordinates of new window. */
7737 #if TARGET_API_MAC_CARBON
7738 SetPort (GetWindowPort (window_ptr));
7739 #else
7740 SetPort (window_ptr);
7741 #endif
7743 GlobalToLocal (&mouse_loc);
7745 #if USE_CARBON_EVENTS
7746 bufp->code = mac_get_mouse_btn (eventRef);
7747 #else
7748 bufp->code = 0; /* only one mouse button */
7749 #endif
7750 bufp->kind = SCROLL_BAR_CLICK_EVENT;
7751 bufp->frame_or_window = tracked_scroll_bar->window;
7752 bufp->part = scroll_bar_handle;
7753 #if USE_CARBON_EVENTS
7754 bufp->modifiers = mac_event_to_emacs_modifiers (eventRef);
7755 #else
7756 bufp->modifiers = mac_to_emacs_modifiers (er.modifiers);
7757 #endif
7758 bufp->modifiers |= up_modifier;
7759 bufp->timestamp = er.when * (1000 / 60);
7760 /* ticks to milliseconds */
7762 XSETINT (bufp->x, tracked_scroll_bar->left + 2);
7763 XSETINT (bufp->y, mouse_loc.v - 24);
7764 tracked_scroll_bar->dragging = Qnil;
7765 mouse_tracking_in_progress = mouse_tracking_none;
7766 tracked_scroll_bar = NULL;
7767 count++;
7768 break;
7771 part_code = FindWindow (er.where, &window_ptr);
7773 switch (part_code)
7775 case inMenuBar:
7777 struct frame *f = ((mac_output *)
7778 GetWRefCon (FrontWindow ()))->mFP;
7779 saved_menu_event_location = er.where;
7780 bufp->kind = MENU_BAR_ACTIVATE_EVENT;
7781 XSETFRAME (bufp->frame_or_window, f);
7782 count++;
7784 break;
7786 case inContent:
7787 if (window_ptr != FrontWindow ())
7788 SelectWindow (window_ptr);
7789 else
7791 SInt16 control_part_code;
7792 ControlHandle ch;
7793 struct mac_output *mwp = (mac_output *)
7794 GetWRefCon (window_ptr);
7795 Point mouse_loc = er.where;
7797 /* convert to local coordinates of new window */
7798 #if TARGET_API_MAC_CARBON
7799 SetPort (GetWindowPort (window_ptr));
7800 #else
7801 SetPort (window_ptr);
7802 #endif
7804 GlobalToLocal (&mouse_loc);
7805 #if TARGET_API_MAC_CARBON
7806 ch = FindControlUnderMouse (mouse_loc, window_ptr,
7807 &control_part_code);
7808 #else
7809 control_part_code = FindControl (mouse_loc, window_ptr, &ch);
7810 #endif
7812 #if USE_CARBON_EVENTS
7813 bufp->code = mac_get_mouse_btn (eventRef);
7814 #else
7815 bufp->code = 0; /* only one mouse button */
7816 #endif
7817 XSETINT (bufp->x, mouse_loc.h);
7818 XSETINT (bufp->y, mouse_loc.v);
7819 bufp->timestamp = er.when * (1000 / 60);
7820 /* ticks to milliseconds */
7822 #if TARGET_API_MAC_CARBON
7823 if (ch != 0)
7824 #else
7825 if (control_part_code != 0)
7826 #endif
7828 struct scroll_bar *bar = (struct scroll_bar *)
7829 GetControlReference (ch);
7830 x_scroll_bar_handle_click (bar, control_part_code, &er,
7831 bufp);
7832 if (er.what == mouseDown
7833 && control_part_code == kControlIndicatorPart)
7835 mouse_tracking_in_progress
7836 = mouse_tracking_scroll_bar;
7837 tracked_scroll_bar = bar;
7839 else
7841 mouse_tracking_in_progress = mouse_tracking_none;
7842 tracked_scroll_bar = NULL;
7845 else
7847 bufp->kind = MOUSE_CLICK_EVENT;
7848 XSETFRAME (bufp->frame_or_window, mwp->mFP);
7849 if (er.what == mouseDown)
7850 mouse_tracking_in_progress
7851 = mouse_tracking_mouse_movement;
7852 else
7853 mouse_tracking_in_progress = mouse_tracking_none;
7856 #if USE_CARBON_EVENTS
7857 bufp->modifiers = mac_event_to_emacs_modifiers (eventRef);
7858 #else
7859 bufp->modifiers = mac_to_emacs_modifiers (er.modifiers);
7860 #endif
7862 switch (er.what)
7864 case mouseDown:
7865 bufp->modifiers |= down_modifier;
7866 break;
7867 case mouseUp:
7868 bufp->modifiers |= up_modifier;
7869 break;
7872 count++;
7874 break;
7876 case inDrag:
7877 #if TARGET_API_MAC_CARBON
7878 if (er.what == mouseDown)
7880 BitMap bm;
7882 GetQDGlobalsScreenBits (&bm);
7883 DragWindow (window_ptr, er.where, &bm.bounds);
7885 #else /* not TARGET_API_MAC_CARBON */
7886 DragWindow (window_ptr, er.where, &qd.screenBits.bounds);
7887 #endif /* not TARGET_API_MAC_CARBON */
7888 break;
7890 case inGoAway:
7891 if (TrackGoAway (window_ptr, er.where))
7893 bufp->kind = DELETE_WINDOW_EVENT;
7894 XSETFRAME (bufp->frame_or_window,
7895 ((mac_output *) GetWRefCon (window_ptr))->mFP);
7896 count++;
7898 break;
7900 /* window resize handling added --ben */
7901 case inGrow:
7902 do_grow_window(window_ptr, &er);
7903 break;
7905 /* window zoom handling added --ben */
7906 case inZoomIn:
7907 case inZoomOut:
7908 if (TrackBox (window_ptr, er.where, part_code))
7909 do_zoom_window (window_ptr, part_code);
7910 break;
7912 default:
7913 break;
7916 break;
7918 case updateEvt:
7919 case osEvt:
7920 case activateEvt:
7921 #if USE_CARBON_EVENTS
7922 if (eventNotHandledErr == SendEventToEventTarget (eventRef, GetEventDispatcherTarget ()))
7923 #endif
7924 do_events (&er);
7925 break;
7927 case keyDown:
7928 case autoKey:
7930 int keycode = (er.message & keyCodeMask) >> 8;
7931 int xkeysym;
7933 #if USE_CARBON_EVENTS
7934 /* When using Carbon Events, we need to pass raw keyboard events
7935 to the TSM ourselves. If TSM handles it, it will pass back
7936 noErr, otherwise it will pass back "eventNotHandledErr" and
7937 we can process it normally. */
7938 if ((!NILP (Vmac_pass_command_to_system)
7939 || !(er.modifiers & cmdKey))
7940 && (!NILP (Vmac_pass_control_to_system)
7941 || !(er.modifiers & controlKey)))
7943 OSStatus err;
7944 err = SendEventToEventTarget (eventRef,
7945 GetEventDispatcherTarget ());
7946 if (err != eventNotHandledErr)
7947 break;
7949 #endif
7951 if (!IsValidWindowPtr (FrontNonFloatingWindow ()))
7953 SysBeep (1);
7954 UNBLOCK_INPUT;
7955 return 0;
7958 ObscureCursor ();
7960 if (keycode_to_xkeysym (keycode, &xkeysym))
7962 bufp->code = 0xff00 | xkeysym;
7963 bufp->kind = NON_ASCII_KEYSTROKE_EVENT;
7965 else
7967 if (er.modifiers & (controlKey |
7968 (NILP (Vmac_command_key_is_meta) ? optionKey
7969 : cmdKey)))
7971 /* This code comes from Keyboard Resource, Appendix
7972 C of IM - Text. This is necessary since shift is
7973 ignored in KCHR table translation when option or
7974 command is pressed. It also does not translate
7975 correctly control-shift chars like C-% so mask off
7976 shift here also */
7977 int new_modifiers = er.modifiers & 0xe600;
7978 /* mask off option and command */
7979 int new_keycode = keycode | new_modifiers;
7980 Ptr kchr_ptr = (Ptr) GetScriptManagerVariable (smKCHRCache);
7981 unsigned long some_state = 0;
7982 bufp->code = KeyTranslate (kchr_ptr, new_keycode,
7983 &some_state) & 0xff;
7985 else
7986 bufp->code = er.message & charCodeMask;
7987 bufp->kind = ASCII_KEYSTROKE_EVENT;
7991 /* If variable mac-convert-keyboard-input-to-latin-1 is non-nil,
7992 convert non-ASCII characters typed at the Mac keyboard
7993 (presumed to be in the Mac Roman encoding) to iso-latin-1
7994 encoding before they are passed to Emacs. This enables the
7995 Mac keyboard to be used to enter non-ASCII iso-latin-1
7996 characters directly. */
7997 if (mac_keyboard_text_encoding != kTextEncodingMacRoman
7998 && bufp->kind == ASCII_KEYSTROKE_EVENT && bufp->code >= 128)
8000 static TECObjectRef converter = NULL;
8001 OSStatus the_err = noErr;
8002 OSStatus convert_status = noErr;
8004 if (converter == NULL)
8006 the_err = TECCreateConverter (&converter,
8007 kTextEncodingMacRoman,
8008 mac_keyboard_text_encoding);
8009 current_mac_keyboard_text_encoding
8010 = mac_keyboard_text_encoding;
8012 else if (mac_keyboard_text_encoding
8013 != current_mac_keyboard_text_encoding)
8015 /* Free the converter for the current encoding before
8016 creating a new one. */
8017 TECDisposeConverter (converter);
8018 the_err = TECCreateConverter (&converter,
8019 kTextEncodingMacRoman,
8020 mac_keyboard_text_encoding);
8021 current_mac_keyboard_text_encoding
8022 = mac_keyboard_text_encoding;
8025 if (the_err == noErr)
8027 unsigned char ch = bufp->code;
8028 ByteCount actual_input_length, actual_output_length;
8029 unsigned char outch;
8031 convert_status = TECConvertText (converter, &ch, 1,
8032 &actual_input_length,
8033 &outch, 1,
8034 &actual_output_length);
8035 if (convert_status == noErr
8036 && actual_input_length == 1
8037 && actual_output_length == 1)
8038 bufp->code = outch;
8042 #if USE_CARBON_EVENTS
8043 bufp->modifiers = mac_event_to_emacs_modifiers (eventRef);
8044 #else
8045 bufp->modifiers = mac_to_emacs_modifiers (er.modifiers);
8046 #endif
8049 mac_output *mwp
8050 = (mac_output *) GetWRefCon (FrontNonFloatingWindow ());
8051 XSETFRAME (bufp->frame_or_window, mwp->mFP);
8054 bufp->timestamp = er.when * (1000 / 60); /* ticks to milliseconds */
8056 count++;
8057 break;
8059 case kHighLevelEvent:
8060 drag_and_drop_file_list = Qnil;
8062 AEProcessAppleEvent(&er);
8064 /* Build a DRAG_N_DROP_EVENT type event as is done in
8065 constuct_drag_n_drop in w32term.c. */
8066 if (!NILP (drag_and_drop_file_list))
8068 struct frame *f = NULL;
8069 WindowPtr wp;
8070 Lisp_Object frame;
8072 wp = FrontNonFloatingWindow ();
8074 if (!wp)
8076 struct frame *f = XFRAME (XCAR (Vframe_list));
8077 CollapseWindow (FRAME_MAC_WINDOW (f), false);
8078 wp = FrontNonFloatingWindow ();
8081 if (wp && is_emacs_window(wp))
8082 f = ((mac_output *) GetWRefCon (wp))->mFP;
8084 bufp->kind = DRAG_N_DROP_EVENT;
8085 bufp->code = 0;
8086 bufp->timestamp = er.when * (1000 / 60);
8087 /* ticks to milliseconds */
8088 #if USE_CARBON_EVENTS
8089 bufp->modifiers = mac_event_to_emacs_modifiers (eventRef);
8090 #else
8091 bufp->modifiers = mac_to_emacs_modifiers (er.modifiers);
8092 #endif
8094 XSETINT (bufp->x, 0);
8095 XSETINT (bufp->y, 0);
8097 XSETFRAME (frame, f);
8098 bufp->frame_or_window = Fcons (frame, drag_and_drop_file_list);
8100 /* Regardless of whether Emacs was suspended or in the
8101 foreground, ask it to redraw its entire screen.
8102 Otherwise parts of the screen can be left in an
8103 inconsistent state. */
8104 if (wp)
8105 #if TARGET_API_MAC_CARBON
8107 Rect r;
8109 GetWindowPortBounds (wp, &r);
8110 InvalWindowRect (wp, &r);
8112 #else /* not TARGET_API_MAC_CARBON */
8113 InvalRect (&(wp->portRect));
8114 #endif /* not TARGET_API_MAC_CARBON */
8116 count++;
8118 default:
8119 break;
8121 #if USE_CARBON_EVENTS
8122 ReleaseEvent (eventRef);
8124 #endif
8126 /* If the focus was just given to an autoraising frame,
8127 raise it now. */
8128 /* ??? This ought to be able to handle more than one such frame. */
8129 if (pending_autoraise_frame)
8131 x_raise_frame (pending_autoraise_frame);
8132 pending_autoraise_frame = 0;
8135 #if !TARGET_API_MAC_CARBON
8136 check_alarm (); /* simulate the handling of a SIGALRM */
8137 #endif
8140 static Point old_mouse_pos = { -1, -1 };
8142 if (app_is_suspended)
8144 old_mouse_pos.h = -1;
8145 old_mouse_pos.v = -1;
8147 else
8149 Point mouse_pos;
8150 WindowPtr wp;
8151 struct frame *f;
8152 Lisp_Object bar;
8153 struct scroll_bar *sb;
8155 wp = FrontWindow ();
8156 if (is_emacs_window (wp))
8158 f = ((mac_output *) GetWRefCon (wp))->mFP;
8160 #if TARGET_API_MAC_CARBON
8161 SetPort (GetWindowPort (wp));
8162 #else
8163 SetPort (wp);
8164 #endif
8166 GetMouse (&mouse_pos);
8168 if (!EqualPt (mouse_pos, old_mouse_pos))
8170 if (mouse_tracking_in_progress == mouse_tracking_scroll_bar
8171 && tracked_scroll_bar)
8172 x_scroll_bar_note_movement (tracked_scroll_bar,
8173 mouse_pos.v
8174 - XINT (tracked_scroll_bar->top),
8175 TickCount() * (1000 / 60));
8176 else
8177 note_mouse_movement (f, &mouse_pos);
8179 old_mouse_pos = mouse_pos;
8185 UNBLOCK_INPUT;
8187 return count;
8191 /* Need to override CodeWarrior's input function so no conversion is
8192 done on newlines Otherwise compiled functions in .elc files will be
8193 read incorrectly. Defined in ...:MSL C:MSL
8194 Common:Source:buffer_io.c. */
8195 #ifdef __MWERKS__
8196 void
8197 __convert_to_newlines (unsigned char * p, size_t * n)
8199 #pragma unused(p,n)
8202 void
8203 __convert_from_newlines (unsigned char * p, size_t * n)
8205 #pragma unused(p,n)
8207 #endif
8210 /* Initialize the struct pointed to by MW to represent a new COLS x
8211 ROWS Macintosh window, using font with name FONTNAME and size
8212 FONTSIZE. */
8213 void
8214 NewMacWindow (FRAME_PTR fp)
8216 mac_output *mwp;
8217 #if TARGET_API_MAC_CARBON
8218 static int making_terminal_window = 0;
8219 #else
8220 static int making_terminal_window = 1;
8221 #endif
8223 mwp = fp->output_data.mac;
8225 if (making_terminal_window)
8227 if (!(mwp->mWP = GetNewCWindow (TERM_WINDOW_RESOURCE, NULL,
8228 (WindowPtr) -1)))
8229 abort ();
8230 making_terminal_window = 0;
8232 else
8233 if (!(mwp->mWP = GetNewCWindow (WINDOW_RESOURCE, NULL, (WindowPtr) -1)))
8234 abort ();
8236 SetWRefCon (mwp->mWP, (long) mwp);
8237 /* so that update events can find this mac_output struct */
8238 mwp->mFP = fp; /* point back to emacs frame */
8240 #if TARGET_API_MAC_CARBON
8241 SetPort (GetWindowPort (mwp->mWP));
8242 #else
8243 SetPort (mwp->mWP);
8244 #endif
8246 mwp->fontset = -1;
8248 SizeWindow (mwp->mWP, FRAME_PIXEL_WIDTH (fp), FRAME_PIXEL_HEIGHT (fp), false);
8249 ShowWindow (mwp->mWP);
8254 void
8255 make_mac_frame (struct frame *f)
8257 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
8258 FRAME_VERTICAL_SCROLL_BAR_TYPE (f) = vertical_scroll_bar_right;
8260 FRAME_DESIRED_CURSOR (f) = FILLED_BOX_CURSOR;
8262 NewMacWindow(f);
8264 f->output_data.mac->cursor_pixel = 0;
8265 f->output_data.mac->border_pixel = 0x00ff00;
8266 f->output_data.mac->mouse_pixel = 0xff00ff;
8267 f->output_data.mac->cursor_foreground_pixel = 0x0000ff;
8269 FRAME_FONTSET (f) = -1;
8270 f->output_data.mac->scroll_bar_foreground_pixel = -1;
8271 f->output_data.mac->scroll_bar_background_pixel = -1;
8272 f->output_data.mac->explicit_parent = 0;
8273 f->left_pos = 4;
8274 f->top_pos = 4;
8275 f->border_width = 0;
8277 f->internal_border_width = 0;
8279 f->output_method = output_mac;
8281 f->auto_raise = 1;
8282 f->auto_lower = 1;
8284 f->new_text_cols = 0;
8285 f->new_text_lines = 0;
8288 void
8289 make_mac_terminal_frame (struct frame *f)
8291 Lisp_Object frame;
8293 XSETFRAME (frame, f);
8295 f->output_method = output_mac;
8296 f->output_data.mac = (struct mac_output *)
8297 xmalloc (sizeof (struct mac_output));
8298 bzero (f->output_data.mac, sizeof (struct mac_output));
8299 FRAME_FONTSET (f) = -1;
8300 f->output_data.mac->scroll_bar_foreground_pixel = -1;
8301 f->output_data.mac->scroll_bar_background_pixel = -1;
8303 XSETFRAME (FRAME_KBOARD (f)->Vdefault_minibuffer_frame, f);
8305 FRAME_COLS (f) = 96;
8306 FRAME_LINES (f) = 4;
8308 make_mac_frame (f);
8310 x_make_gc (f);
8312 /* Need to be initialized for unshow_buffer in window.c. */
8313 selected_window = f->selected_window;
8315 Fmodify_frame_parameters (frame,
8316 Fcons (Fcons (Qfont,
8317 build_string ("-*-monaco-medium-r-*--*-90-*-*-*-*-mac-roman")), Qnil));
8318 Fmodify_frame_parameters (frame,
8319 Fcons (Fcons (Qforeground_color,
8320 build_string ("black")), Qnil));
8321 Fmodify_frame_parameters (frame,
8322 Fcons (Fcons (Qbackground_color,
8323 build_string ("white")), Qnil));
8327 /***********************************************************************
8328 Initialization
8329 ***********************************************************************/
8331 #ifdef USE_X_TOOLKIT
8332 static XrmOptionDescRec emacs_options[] = {
8333 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
8334 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
8336 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
8337 XrmoptionSepArg, NULL},
8338 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
8340 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
8341 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
8342 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
8343 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
8344 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
8345 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
8346 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
8348 #endif /* USE_X_TOOLKIT */
8350 #ifdef MULTI_KBOARD
8351 /* Test whether two display-name strings agree up to the dot that separates
8352 the screen number from the server number. */
8353 static int
8354 same_x_server (name1, name2)
8355 char *name1, *name2;
8357 int seen_colon = 0;
8358 unsigned char *system_name = SDATA (Vsystem_name);
8359 int system_name_length = strlen (system_name);
8360 int length_until_period = 0;
8362 while (system_name[length_until_period] != 0
8363 && system_name[length_until_period] != '.')
8364 length_until_period++;
8366 /* Treat `unix' like an empty host name. */
8367 if (! strncmp (name1, "unix:", 5))
8368 name1 += 4;
8369 if (! strncmp (name2, "unix:", 5))
8370 name2 += 4;
8371 /* Treat this host's name like an empty host name. */
8372 if (! strncmp (name1, system_name, system_name_length)
8373 && name1[system_name_length] == ':')
8374 name1 += system_name_length;
8375 if (! strncmp (name2, system_name, system_name_length)
8376 && name2[system_name_length] == ':')
8377 name2 += system_name_length;
8378 /* Treat this host's domainless name like an empty host name. */
8379 if (! strncmp (name1, system_name, length_until_period)
8380 && name1[length_until_period] == ':')
8381 name1 += length_until_period;
8382 if (! strncmp (name2, system_name, length_until_period)
8383 && name2[length_until_period] == ':')
8384 name2 += length_until_period;
8386 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
8388 if (*name1 == ':')
8389 seen_colon++;
8390 if (seen_colon && *name1 == '.')
8391 return 1;
8393 return (seen_colon
8394 && (*name1 == '.' || *name1 == '\0')
8395 && (*name2 == '.' || *name2 == '\0'));
8397 #endif
8399 int mac_initialized = 0;
8401 void
8402 mac_initialize_display_info ()
8404 struct mac_display_info *dpyinfo = &one_mac_display_info;
8405 GDHandle main_device_handle;
8407 bzero (dpyinfo, sizeof (*dpyinfo));
8409 /* Put it on x_display_name_list. */
8410 x_display_name_list = Fcons (Fcons (build_string ("Mac"), Qnil),
8411 x_display_name_list);
8412 dpyinfo->name_list_element = XCAR (x_display_name_list);
8414 #if 0
8415 dpyinfo->mac_id_name
8416 = (char *) xmalloc (SCHARS (Vinvocation_name)
8417 + SCHARS (Vsystem_name)
8418 + 2);
8419 sprintf (dpyinfo->mac_id_name, "%s@%s",
8420 SDATA (Vinvocation_name), SDATA (Vsystem_name));
8421 #else
8422 dpyinfo->mac_id_name = (char *) xmalloc (strlen ("Mac Display") + 1);
8423 strcpy (dpyinfo->mac_id_name, "Mac Display");
8424 #endif
8426 main_device_handle = LMGetMainDevice();
8428 dpyinfo->reference_count = 0;
8429 dpyinfo->resx = 75.0;
8430 dpyinfo->resy = 75.0;
8431 dpyinfo->n_planes = 1;
8432 dpyinfo->n_cbits = 16;
8433 dpyinfo->height = (**main_device_handle).gdRect.bottom;
8434 dpyinfo->width = (**main_device_handle).gdRect.right;
8435 dpyinfo->grabbed = 0;
8436 dpyinfo->root_window = NULL;
8438 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
8439 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
8440 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
8441 dpyinfo->mouse_face_window = Qnil;
8444 struct mac_display_info *
8445 mac_term_init (display_name, xrm_option, resource_name)
8446 Lisp_Object display_name;
8447 char *xrm_option;
8448 char *resource_name;
8450 struct mac_display_info *dpyinfo;
8451 GDHandle main_device_handle;
8453 if (!mac_initialized)
8455 mac_initialize ();
8456 mac_initialized = 1;
8459 mac_initialize_display_info (display_name);
8461 dpyinfo = &one_mac_display_info;
8463 main_device_handle = LMGetMainDevice();
8465 dpyinfo->height = (**main_device_handle).gdRect.bottom;
8466 dpyinfo->width = (**main_device_handle).gdRect.right;
8468 return dpyinfo;
8471 #ifdef MAC_OSX
8472 void
8473 mac_check_bundle()
8475 extern int inhibit_window_system;
8476 extern int noninteractive;
8477 CFBundleRef appsBundle;
8478 pid_t child;
8480 /* No need to test if already -nw*/
8481 if (inhibit_window_system || noninteractive)
8482 return;
8484 appsBundle = CFBundleGetMainBundle();
8485 if (appsBundle != NULL)
8487 CFStringRef cfBI = CFSTR("CFBundleIdentifier");
8488 CFTypeRef res = CFBundleGetValueForInfoDictionaryKey(appsBundle, cfBI);
8489 /* We found the bundle identifier, now we know we are valid. */
8490 if (res != NULL)
8492 CFRelease(res);
8493 return;
8496 /* MAC_TODO: Have this start the bundled executable */
8498 /* For now, prevent the fatal error by bringing it up in the terminal */
8499 inhibit_window_system = 1;
8502 void
8503 MakeMeTheFrontProcess ()
8505 ProcessSerialNumber psn;
8506 OSErr err;
8508 err = GetCurrentProcess (&psn);
8509 if (err == noErr)
8510 (void) SetFrontProcess (&psn);
8513 /***** Code to handle C-g testing *****/
8515 /* Contains the Mac modifier formed from quit_char */
8516 static mac_quit_char_modifiers = 0;
8517 static mac_quit_char_keycode;
8518 extern int quit_char;
8520 static void
8521 mac_determine_quit_char_modifiers()
8523 /* Todo: Determine modifiers from quit_char. */
8524 UInt32 qc_modifiers = ctrl_modifier;
8526 /* Map modifiers */
8527 mac_quit_char_modifiers = 0;
8528 if (qc_modifiers & ctrl_modifier) mac_quit_char_modifiers |= macCtrlKey;
8529 if (qc_modifiers & shift_modifier) mac_quit_char_modifiers |= macShiftKey;
8530 if (qc_modifiers & meta_modifier) mac_quit_char_modifiers |= macMetaKey;
8531 if (qc_modifiers & alt_modifier) mac_quit_char_modifiers |= macAltKey;
8534 static void
8535 init_quit_char_handler ()
8537 /* TODO: Let this support keys other the 'g' */
8538 mac_quit_char_keycode = 5;
8539 /* Look at <architecture/adb_kb_map.h> for details */
8540 /* http://gemma.apple.com/techpubs/mac/Toolbox/Toolbox-40.html#MARKER-9-184*/
8542 mac_determine_quit_char_modifiers();
8545 static Boolean
8546 quit_char_comp (EventRef inEvent, void *inCompData)
8548 if (GetEventClass(inEvent) != kEventClassKeyboard)
8549 return false;
8550 if (GetEventKind(inEvent) != kEventRawKeyDown)
8551 return false;
8553 UInt32 keyCode;
8554 UInt32 keyModifiers;
8555 GetEventParameter(inEvent, kEventParamKeyCode,
8556 typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode);
8557 if (keyCode != mac_quit_char_keycode)
8558 return false;
8559 GetEventParameter(inEvent, kEventParamKeyModifiers,
8560 typeUInt32, NULL, sizeof(UInt32), NULL, &keyModifiers);
8561 if (keyModifiers != mac_quit_char_modifiers)
8562 return false;
8564 return true;
8567 void
8568 mac_check_for_quit_char ()
8570 EventRef event;
8571 static EMACS_TIME last_check_time = { 0, 0 };
8572 static EMACS_TIME one_second = { 1, 0 };
8573 EMACS_TIME now, t;
8575 /* If windows are not initialized, return immediately (keep it bouncin'). */
8576 if (!mac_quit_char_modifiers)
8577 return;
8579 /* Don't check if last check is less than a second ago. */
8580 EMACS_GET_TIME (now);
8581 EMACS_SUB_TIME (t, now, last_check_time);
8582 if (EMACS_TIME_LT (t, one_second))
8583 return;
8584 last_check_time = now;
8586 /* Redetermine modifiers because they are based on lisp variables */
8587 mac_determine_quit_char_modifiers ();
8589 /* Fill the queue with events */
8590 ReceiveNextEvent (0, NULL, kEventDurationNoWait, false, &event);
8591 event = FindSpecificEventInQueue (GetMainEventQueue (), quit_char_comp,
8592 NULL);
8593 if (event)
8595 struct input_event e;
8596 struct mac_output *mwp =
8597 (mac_output *) GetWRefCon (FrontNonFloatingWindow ());
8598 /* Use an input_event to emulate what the interrupt handler does. */
8599 EVENT_INIT (e);
8600 e.kind = ASCII_KEYSTROKE_EVENT;
8601 e.code = quit_char;
8602 e.arg = NULL;
8603 e.modifiers = NULL;
8604 e.timestamp = EventTimeToTicks (GetEventTime (event)) * (1000/60);
8605 XSETFRAME (e.frame_or_window, mwp->mFP);
8606 /* Remove event from queue to prevent looping. */
8607 RemoveEventFromQueue (GetMainEventQueue (), event);
8608 ReleaseEvent (event);
8609 kbd_buffer_store_event (&e);
8613 #endif /* MAC_OSX */
8615 /* Set up use of X before we make the first connection. */
8617 extern frame_parm_handler mac_frame_parm_handlers[];
8619 static struct redisplay_interface x_redisplay_interface =
8621 mac_frame_parm_handlers,
8622 x_produce_glyphs,
8623 x_write_glyphs,
8624 x_insert_glyphs,
8625 x_clear_end_of_line,
8626 x_scroll_run,
8627 x_after_update_window_line,
8628 x_update_window_begin,
8629 x_update_window_end,
8630 x_cursor_to,
8631 x_flush,
8632 x_flush,
8633 x_clear_window_mouse_face,
8634 x_get_glyph_overhangs,
8635 x_fix_overlapping_area,
8636 x_draw_fringe_bitmap,
8637 mac_per_char_metric,
8638 mac_encode_char,
8639 NULL, /* mac_compute_glyph_string_overhangs */
8640 x_draw_glyph_string,
8641 mac_define_frame_cursor,
8642 mac_clear_frame_area,
8643 mac_draw_window_cursor,
8644 mac_draw_vertical_window_border,
8645 mac_shift_glyphs_for_insert
8648 void
8649 mac_initialize ()
8651 rif = &x_redisplay_interface;
8653 clear_frame_hook = x_clear_frame;
8654 ins_del_lines_hook = x_ins_del_lines;
8655 delete_glyphs_hook = x_delete_glyphs;
8656 ring_bell_hook = XTring_bell;
8657 reset_terminal_modes_hook = XTreset_terminal_modes;
8658 set_terminal_modes_hook = XTset_terminal_modes;
8659 update_begin_hook = x_update_begin;
8660 update_end_hook = x_update_end;
8661 set_terminal_window_hook = XTset_terminal_window;
8662 read_socket_hook = XTread_socket;
8663 frame_up_to_date_hook = XTframe_up_to_date;
8664 mouse_position_hook = XTmouse_position;
8665 frame_rehighlight_hook = XTframe_rehighlight;
8666 frame_raise_lower_hook = XTframe_raise_lower;
8668 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
8669 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
8670 redeem_scroll_bar_hook = XTredeem_scroll_bar;
8671 judge_scroll_bars_hook = XTjudge_scroll_bars;
8673 scroll_region_ok = 1; /* we'll scroll partial frames */
8674 char_ins_del_ok = 1;
8675 line_ins_del_ok = 1; /* we'll just blt 'em */
8676 fast_clear_end_of_line = 1; /* X does this well */
8677 memory_below_frame = 0; /* we don't remember what scrolls
8678 off the bottom */
8679 baud_rate = 19200;
8681 x_noop_count = 0;
8682 last_tool_bar_item = -1;
8683 any_help_event_p = 0;
8685 /* Try to use interrupt input; if we can't, then start polling. */
8686 Fset_input_mode (Qt, Qnil, Qt, Qnil);
8688 #ifdef USE_X_TOOLKIT
8689 XtToolkitInitialize ();
8690 Xt_app_con = XtCreateApplicationContext ();
8691 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
8693 /* Install an asynchronous timer that processes Xt timeout events
8694 every 0.1s. This is necessary because some widget sets use
8695 timeouts internally, for example the LessTif menu bar, or the
8696 Xaw3d scroll bar. When Xt timouts aren't processed, these
8697 widgets don't behave normally. */
8699 EMACS_TIME interval;
8700 EMACS_SET_SECS_USECS (interval, 0, 100000);
8701 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
8703 #endif
8705 #if USE_TOOLKIT_SCROLL_BARS
8706 xaw3d_arrow_scroll = False;
8707 xaw3d_pick_top = True;
8708 #endif
8710 #if 0
8711 /* Note that there is no real way portable across R3/R4 to get the
8712 original error handler. */
8713 XSetErrorHandler (x_error_handler);
8714 XSetIOErrorHandler (x_io_error_quitter);
8716 /* Disable Window Change signals; they are handled by X events. */
8717 #ifdef SIGWINCH
8718 signal (SIGWINCH, SIG_DFL);
8719 #endif /* ! defined (SIGWINCH) */
8721 signal (SIGPIPE, x_connection_signal);
8722 #endif
8724 mac_initialize_display_info ();
8726 #if TARGET_API_MAC_CARBON
8727 init_required_apple_events ();
8729 init_mac_drag_n_drop ();
8731 #if USE_CARBON_EVENTS
8732 init_service_handler ();
8734 init_quit_char_handler ();
8735 #endif
8737 DisableMenuCommand (NULL, kHICommandQuit);
8739 if (!inhibit_window_system)
8740 MakeMeTheFrontProcess ();
8741 #endif
8745 void
8746 syms_of_macterm ()
8748 #if 0
8749 staticpro (&x_error_message_string);
8750 x_error_message_string = Qnil;
8751 #endif
8753 Fprovide (intern ("mac-carbon"), Qnil);
8755 staticpro (&x_display_name_list);
8756 x_display_name_list = Qnil;
8758 staticpro (&last_mouse_scroll_bar);
8759 last_mouse_scroll_bar = Qnil;
8761 staticpro (&Qvendor_specific_keysyms);
8762 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
8764 staticpro (&last_mouse_press_frame);
8765 last_mouse_press_frame = Qnil;
8767 Qmac_ready_for_drag_n_drop = intern ("mac-ready-for-drag-n-drop");
8768 staticpro (&Qmac_ready_for_drag_n_drop);
8770 DEFVAR_BOOL ("x-autoselect-window", &x_autoselect_window_p,
8771 doc: /* *Non-nil means autoselect window with mouse pointer. */);
8772 x_autoselect_window_p = 0;
8774 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
8775 doc: /* If not nil, Emacs uses toolkit scroll bars. */);
8776 Vx_toolkit_scroll_bars = Qt;
8778 DEFVAR_BOOL ("x-use-underline-position-properties",
8779 &x_use_underline_position_properties,
8780 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
8781 nil means ignore them. If you encounter fonts with bogus
8782 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
8783 to 4.1, set this to nil. */);
8784 x_use_underline_position_properties = 0;
8786 staticpro (&last_mouse_motion_frame);
8787 last_mouse_motion_frame = Qnil;
8789 DEFVAR_LISP ("mac-command-key-is-meta", &Vmac_command_key_is_meta,
8790 doc: /* Non-nil means that the command key is used as the Emacs meta key.
8791 Otherwise the option key is used. */);
8792 Vmac_command_key_is_meta = Qt;
8794 DEFVAR_LISP ("mac-reverse-ctrl-meta", &Vmac_reverse_ctrl_meta,
8795 doc: /* Non-nil means that the control and meta keys are reversed. This is
8796 useful for non-standard keyboard layouts. */);
8797 Vmac_reverse_ctrl_meta = Qnil;
8799 #if USE_CARBON_EVENTS
8800 DEFVAR_LISP ("mac-wheel-button-is-mouse-2", &Vmac_wheel_button_is_mouse_2,
8801 doc: /* Non-nil means that the wheel button will be treated as mouse-2 and
8802 the right click will be mouse-3.
8803 Otherwise, the right click will be mouse-2 and the wheel button mouse-3.*/);
8804 Vmac_wheel_button_is_mouse_2 = Qt;
8806 DEFVAR_LISP ("mac-pass-command-to-system", &Vmac_pass_command_to_system,
8807 doc: /* If non-nil, the Mac \"Command\" key is passed on to the Mac
8808 Toolbox for processing before Emacs sees it. */);
8809 Vmac_pass_command_to_system = Qt;
8811 DEFVAR_LISP ("mac-pass-control-to-system", &Vmac_pass_control_to_system,
8812 doc: /* If non-nil, the Mac \"Control\" key is passed on to the Mac
8813 Toolbox for processing before Emacs sees it. */);
8814 Vmac_pass_control_to_system = Qt;
8815 #endif
8817 DEFVAR_INT ("mac-keyboard-text-encoding", &mac_keyboard_text_encoding,
8818 doc: /* One of the Text Encoding Base constant values defined in the
8819 Basic Text Constants section of Inside Macintosh - Text Encoding
8820 Conversion Manager. Its value determines the encoding characters
8821 typed at the Mac keyboard (presumed to be in the MacRoman encoding)
8822 will convert into. E.g., if it is set to kTextEncodingMacRoman (0),
8823 its default value, no conversion takes place. If it is set to
8824 kTextEncodingISOLatin1 (0x201) or kTextEncodingISOLatin2 (0x202),
8825 characters typed on Mac keyboard are first converted into the
8826 ISO Latin-1 or ISO Latin-2 encoding, respectively before being
8827 passed to Emacs. Together with Emacs's set-keyboard-coding-system
8828 command, this enables the Mac keyboard to be used to enter non-ASCII
8829 characters directly. */);
8830 mac_keyboard_text_encoding = kTextEncodingMacRoman;
8833 /* arch-tag: f2259165-4454-4c04-a029-a133c8af7b5b
8834 (do not change this comment) */