]> code.delx.au - gnu-emacs/blob - src/macterm.c
(archive-l-e): New optional argument `float' means generate a float value.
[gnu-emacs] / src / macterm.c
1 /* Implementation of GUI terminal on the Mac OS.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2006 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
21
22 /* Contributed by Andrew Choi (akochoi@mac.com). */
23
24 #include <config.h>
25 #include <signal.h>
26
27 #include <stdio.h>
28
29 #include "lisp.h"
30 #include "blockinput.h"
31
32 #include "macterm.h"
33
34 #ifndef MAC_OSX
35 #include <alloca.h>
36 #endif
37
38 #if TARGET_API_MAC_CARBON
39 /* USE_CARBON_EVENTS determines if the Carbon Event Manager is used to
40 obtain events from the event queue. If set to 0, WaitNextEvent is
41 used instead. */
42 #define USE_CARBON_EVENTS 1
43 #else /* not TARGET_API_MAC_CARBON */
44 #include <Quickdraw.h>
45 #include <ToolUtils.h>
46 #include <Sound.h>
47 #include <Events.h>
48 #include <Script.h>
49 #include <Resources.h>
50 #include <Fonts.h>
51 #include <TextUtils.h>
52 #include <LowMem.h>
53 #include <Controls.h>
54 #include <Windows.h>
55 #if defined (__MRC__) || (__MSL__ >= 0x6000)
56 #include <ControlDefinitions.h>
57 #endif
58
59 #if __profile__
60 #include <profiler.h>
61 #endif
62 #endif /* not TARGET_API_MAC_CARBON */
63
64 #include "systty.h"
65 #include "systime.h"
66
67 #include <ctype.h>
68 #include <errno.h>
69 #include <setjmp.h>
70 #include <sys/stat.h>
71
72 #include "charset.h"
73 #include "coding.h"
74 #include "frame.h"
75 #include "dispextern.h"
76 #include "fontset.h"
77 #include "termhooks.h"
78 #include "termopts.h"
79 #include "termchar.h"
80 #include "disptab.h"
81 #include "buffer.h"
82 #include "window.h"
83 #include "keyboard.h"
84 #include "intervals.h"
85 #include "atimer.h"
86 #include "keymap.h"
87
88 \f
89
90 /* Non-nil means Emacs uses toolkit scroll bars. */
91
92 Lisp_Object Vx_toolkit_scroll_bars;
93
94 /* If non-zero, the text will be rendered using Core Graphics text
95 rendering which may anti-alias the text. */
96 int mac_use_core_graphics;
97
98
99 /* Non-zero means that a HELP_EVENT has been generated since Emacs
100 start. */
101
102 static int any_help_event_p;
103
104 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
105 static Lisp_Object last_window;
106
107 /* Non-zero means make use of UNDERLINE_POSITION font properties.
108 (Not yet supported.) */
109 int x_use_underline_position_properties;
110
111 /* This is a chain of structures for all the X displays currently in
112 use. */
113
114 struct x_display_info *x_display_list;
115
116 /* This is a list of cons cells, each of the form (NAME
117 FONT-LIST-CACHE . RESOURCE-DATABASE), one for each element of
118 x_display_list and in the same order. NAME is the name of the
119 frame. FONT-LIST-CACHE records previous values returned by
120 x-list-fonts. RESOURCE-DATABASE preserves the X Resource Database
121 equivalent, which is implemented with a Lisp object, for the
122 display. */
123
124 Lisp_Object x_display_name_list;
125
126 /* This is display since Mac does not support multiple ones. */
127 struct mac_display_info one_mac_display_info;
128
129 /* Frame being updated by update_frame. This is declared in term.c.
130 This is set by update_begin and looked at by all the XT functions.
131 It is zero while not inside an update. In that case, the XT
132 functions assume that `selected_frame' is the frame to apply to. */
133
134 extern struct frame *updating_frame;
135
136 /* This is a frame waiting to be auto-raised, within XTread_socket. */
137
138 struct frame *pending_autoraise_frame;
139
140 /* Mouse movement.
141
142 Formerly, we used PointerMotionHintMask (in standard_event_mask)
143 so that we would have to call XQueryPointer after each MotionNotify
144 event to ask for another such event. However, this made mouse tracking
145 slow, and there was a bug that made it eventually stop.
146
147 Simply asking for MotionNotify all the time seems to work better.
148
149 In order to avoid asking for motion events and then throwing most
150 of them away or busy-polling the server for mouse positions, we ask
151 the server for pointer motion hints. This means that we get only
152 one event per group of mouse movements. "Groups" are delimited by
153 other kinds of events (focus changes and button clicks, for
154 example), or by XQueryPointer calls; when one of these happens, we
155 get another MotionNotify event the next time the mouse moves. This
156 is at least as efficient as getting motion events when mouse
157 tracking is on, and I suspect only negligibly worse when tracking
158 is off. */
159
160 /* Where the mouse was last time we reported a mouse event. */
161
162 static Rect last_mouse_glyph;
163 static FRAME_PTR last_mouse_glyph_frame;
164
165 /* The scroll bar in which the last X motion event occurred.
166
167 If the last X motion event occurred in a scroll bar, we set this so
168 XTmouse_position can know whether to report a scroll bar motion or
169 an ordinary motion.
170
171 If the last X motion event didn't occur in a scroll bar, we set
172 this to Qnil, to tell XTmouse_position to return an ordinary motion
173 event. */
174
175 static Lisp_Object last_mouse_scroll_bar;
176
177 /* This is a hack. We would really prefer that XTmouse_position would
178 return the time associated with the position it returns, but there
179 doesn't seem to be any way to wrest the time-stamp from the server
180 along with the position query. So, we just keep track of the time
181 of the last movement we received, and return that in hopes that
182 it's somewhat accurate. */
183
184 static Time last_mouse_movement_time;
185
186 struct scroll_bar *tracked_scroll_bar = NULL;
187
188 /* Incremented by XTread_socket whenever it really tries to read
189 events. */
190
191 #ifdef __STDC__
192 static int volatile input_signal_count;
193 #else
194 static int input_signal_count;
195 #endif
196
197 extern Lisp_Object Vsystem_name;
198
199 /* A mask of extra modifier bits to put into every keyboard char. */
200
201 extern EMACS_INT extra_keyboard_modifiers;
202
203 /* The keysyms to use for the various modifiers. */
204
205 static Lisp_Object Qalt, Qhyper, Qsuper, Qcontrol, Qmeta, Qmodifier_value;
206
207 extern int inhibit_window_system;
208
209 #if __MRC__ && !TARGET_API_MAC_CARBON
210 QDGlobals qd; /* QuickDraw global information structure. */
211 #endif
212
213 #define mac_window_to_frame(wp) (((mac_output *) GetWRefCon (wp))->mFP)
214
215 struct mac_display_info *mac_display_info_for_display (Display *);
216 static void x_update_window_end P_ ((struct window *, int, int));
217 int x_catch_errors P_ ((Display *));
218 void x_uncatch_errors P_ ((Display *, int));
219 void x_lower_frame P_ ((struct frame *));
220 void x_scroll_bar_clear P_ ((struct frame *));
221 int x_had_errors_p P_ ((Display *));
222 void x_wm_set_size_hint P_ ((struct frame *, long, int));
223 void x_raise_frame P_ ((struct frame *));
224 void x_set_window_size P_ ((struct frame *, int, int, int));
225 void x_wm_set_window_state P_ ((struct frame *, int));
226 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
227 void mac_initialize P_ ((void));
228 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
229 static int x_compute_min_glyph_bounds P_ ((struct frame *));
230 static void x_update_end P_ ((struct frame *));
231 static void XTframe_up_to_date P_ ((struct frame *));
232 static void XTset_terminal_modes P_ ((void));
233 static void XTreset_terminal_modes P_ ((void));
234 static void x_clear_frame P_ ((void));
235 static void frame_highlight P_ ((struct frame *));
236 static void frame_unhighlight P_ ((struct frame *));
237 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
238 static void mac_focus_changed P_ ((int, struct mac_display_info *,
239 struct frame *, struct input_event *));
240 static void x_detect_focus_change P_ ((struct mac_display_info *,
241 EventRecord *, struct input_event *));
242 static void XTframe_rehighlight P_ ((struct frame *));
243 static void x_frame_rehighlight P_ ((struct x_display_info *));
244 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
245 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
246 enum text_cursor_kinds));
247
248 static void x_clip_to_row P_ ((struct window *, struct glyph_row *, int, GC));
249 static void x_flush P_ ((struct frame *f));
250 static void x_update_begin P_ ((struct frame *));
251 static void x_update_window_begin P_ ((struct window *));
252 static void x_after_update_window_line P_ ((struct glyph_row *));
253 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
254 enum scroll_bar_part *,
255 Lisp_Object *, Lisp_Object *,
256 unsigned long *));
257
258 static int is_emacs_window P_ ((WindowPtr));
259 static XCharStruct *mac_per_char_metric P_ ((XFontStruct *, XChar2b *, int));
260 static void XSetFont P_ ((Display *, GC, XFontStruct *));
261
262 /* Defined in macmenu.h. */
263 extern void menubar_selection_callback (FRAME_PTR, int);
264
265 #define GC_FORE_COLOR(gc) (&(gc)->fore_color)
266 #define GC_BACK_COLOR(gc) (&(gc)->back_color)
267 #define GC_FONT(gc) ((gc)->xgcv.font)
268 #define FRAME_NORMAL_GC(f) ((f)->output_data.mac->normal_gc)
269 #define CG_SET_FILL_COLOR(context, color) \
270 CGContextSetRGBFillColor (context, \
271 RED_FROM_ULONG (color) / 255.0f, \
272 GREEN_FROM_ULONG (color) / 255.0f, \
273 BLUE_FROM_ULONG (color) / 255.0f, 1.0f)
274 #define CG_SET_STROKE_COLOR(context, color) \
275 CGContextSetRGBStrokeColor (context, \
276 RED_FROM_ULONG (color) / 255.0f, \
277 GREEN_FROM_ULONG (color) / 255.0f, \
278 BLUE_FROM_ULONG (color) / 255.0f, 1.0f)
279 #if USE_CG_DRAWING
280 #define FRAME_CG_CONTEXT(f) ((f)->output_data.mac->cg_context)
281
282 /* Fringe bitmaps. */
283
284 static int max_fringe_bmp = 0;
285 static CGImageRef *fringe_bmp = 0;
286
287 static CGContextRef
288 mac_begin_cg_clip (f, gc)
289 struct frame *f;
290 GC gc;
291 {
292 CGContextRef context = FRAME_CG_CONTEXT (f);
293
294 if (!context)
295 {
296 QDBeginCGContext (GetWindowPort (FRAME_MAC_WINDOW (f)), &context);
297 FRAME_CG_CONTEXT (f) = context;
298 }
299
300 CGContextSaveGState (context);
301 CGContextTranslateCTM (context, 0, FRAME_PIXEL_HEIGHT (f));
302 CGContextScaleCTM (context, 1, -1);
303 if (gc && gc->n_clip_rects)
304 CGContextClipToRects (context, gc->clip_rects, gc->n_clip_rects);
305
306 return context;
307 }
308
309 static void
310 mac_end_cg_clip (f)
311 struct frame *f;
312 {
313 CGContextRestoreGState (FRAME_CG_CONTEXT (f));
314 }
315
316 void
317 mac_prepare_for_quickdraw (f)
318 struct frame *f;
319 {
320 if (f == NULL)
321 {
322 Lisp_Object rest, frame;
323 FOR_EACH_FRAME (rest, frame)
324 if (FRAME_MAC_P (XFRAME (frame)))
325 mac_prepare_for_quickdraw (XFRAME (frame));
326 }
327 else
328 {
329 CGContextRef context = FRAME_CG_CONTEXT (f);
330
331 if (context)
332 {
333 CGContextSynchronize (context);
334 QDEndCGContext (GetWindowPort (FRAME_MAC_WINDOW (f)),
335 &FRAME_CG_CONTEXT (f));
336 }
337 }
338 }
339 #endif
340
341 static RgnHandle saved_port_clip_region = NULL;
342
343 static void
344 mac_begin_clip (gc)
345 GC gc;
346 {
347 static RgnHandle new_region = NULL;
348
349 if (saved_port_clip_region == NULL)
350 saved_port_clip_region = NewRgn ();
351 if (new_region == NULL)
352 new_region = NewRgn ();
353
354 if (gc->n_clip_rects)
355 {
356 GetClip (saved_port_clip_region);
357 SectRgn (saved_port_clip_region, gc->clip_region, new_region);
358 SetClip (new_region);
359 }
360 }
361
362 static void
363 mac_end_clip (gc)
364 GC gc;
365 {
366 if (gc->n_clip_rects)
367 SetClip (saved_port_clip_region);
368 }
369
370
371 /* X display function emulation */
372
373 void
374 XFreePixmap (display, pixmap)
375 Display *display; /* not used */
376 Pixmap pixmap;
377 {
378 DisposeGWorld (pixmap);
379 }
380
381
382 /* Mac version of XDrawLine. */
383
384 static void
385 mac_draw_line (f, gc, x1, y1, x2, y2)
386 struct frame *f;
387 GC gc;
388 int x1, y1, x2, y2;
389 {
390 #if USE_CG_DRAWING
391 CGContextRef context;
392 float gx1 = x1, gy1 = y1, gx2 = x2, gy2 = y2;
393
394 if (y1 != y2)
395 gx1 += 0.5f, gx2 += 0.5f;
396 if (x1 != x2)
397 gy1 += 0.5f, gy2 += 0.5f;
398
399 context = mac_begin_cg_clip (f, gc);
400 CG_SET_STROKE_COLOR (context, gc->xgcv.foreground);
401 CGContextBeginPath (context);
402 CGContextMoveToPoint (context, gx1, gy1);
403 CGContextAddLineToPoint (context, gx2, gy2);
404 CGContextClosePath (context);
405 CGContextStrokePath (context);
406 mac_end_cg_clip (f);
407 #else
408 if (x1 == x2)
409 {
410 if (y1 > y2)
411 y1--;
412 else if (y2 > y1)
413 y2--;
414 }
415 else if (y1 == y2)
416 {
417 if (x1 > x2)
418 x1--;
419 else
420 x2--;
421 }
422
423 SetPortWindowPort (FRAME_MAC_WINDOW (f));
424
425 RGBForeColor (GC_FORE_COLOR (gc));
426
427 mac_begin_clip (gc);
428 MoveTo (x1, y1);
429 LineTo (x2, y2);
430 mac_end_clip (gc);
431 #endif
432 }
433
434 void
435 mac_draw_line_to_pixmap (display, p, gc, x1, y1, x2, y2)
436 Display *display;
437 Pixmap p;
438 GC gc;
439 int x1, y1, x2, y2;
440 {
441 CGrafPtr old_port;
442 GDHandle old_gdh;
443
444 if (x1 == x2)
445 {
446 if (y1 > y2)
447 y1--;
448 else if (y2 > y1)
449 y2--;
450 }
451 else if (y1 == y2)
452 {
453 if (x1 > x2)
454 x1--;
455 else
456 x2--;
457 }
458
459 GetGWorld (&old_port, &old_gdh);
460 SetGWorld (p, NULL);
461
462 RGBForeColor (GC_FORE_COLOR (gc));
463
464 LockPixels (GetGWorldPixMap (p));
465 MoveTo (x1, y1);
466 LineTo (x2, y2);
467 UnlockPixels (GetGWorldPixMap (p));
468
469 SetGWorld (old_port, old_gdh);
470 }
471
472
473 static void
474 mac_erase_rectangle (f, gc, x, y, width, height)
475 struct frame *f;
476 GC gc;
477 int x, y;
478 unsigned int width, height;
479 {
480 #if USE_CG_DRAWING
481 CGContextRef context;
482
483 context = mac_begin_cg_clip (f, gc);
484 CG_SET_FILL_COLOR (context, gc->xgcv.background);
485 CGContextFillRect (context, CGRectMake (x, y, width, height));
486 mac_end_cg_clip (f);
487 #else
488 Rect r;
489
490 SetPortWindowPort (FRAME_MAC_WINDOW (f));
491
492 RGBBackColor (GC_BACK_COLOR (gc));
493 SetRect (&r, x, y, x + width, y + height);
494
495 mac_begin_clip (gc);
496 EraseRect (&r);
497 mac_end_clip (gc);
498
499 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
500 #endif
501 }
502
503
504 /* Mac version of XClearArea. */
505
506 void
507 mac_clear_area (f, x, y, width, height)
508 struct frame *f;
509 int x, y;
510 unsigned int width, height;
511 {
512 mac_erase_rectangle (f, FRAME_NORMAL_GC (f), x, y, width, height);
513 }
514
515 /* Mac version of XClearWindow. */
516
517 static void
518 mac_clear_window (f)
519 struct frame *f;
520 {
521 #if USE_CG_DRAWING
522 CGContextRef context;
523 GC gc = FRAME_NORMAL_GC (f);
524
525 context = mac_begin_cg_clip (f, NULL);
526 CG_SET_FILL_COLOR (context, gc->xgcv.background);
527 CGContextFillRect (context, CGRectMake (0, 0, FRAME_PIXEL_WIDTH (f),
528 FRAME_PIXEL_HEIGHT (f)));
529 mac_end_cg_clip (f);
530 #else
531 SetPortWindowPort (FRAME_MAC_WINDOW (f));
532
533 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
534
535 #if TARGET_API_MAC_CARBON
536 {
537 Rect r;
538
539 GetWindowPortBounds (FRAME_MAC_WINDOW (f), &r);
540 EraseRect (&r);
541 }
542 #else /* not TARGET_API_MAC_CARBON */
543 EraseRect (&(FRAME_MAC_WINDOW (f)->portRect));
544 #endif /* not TARGET_API_MAC_CARBON */
545 #endif
546 }
547
548
549 /* Mac replacement for XCopyArea. */
550
551 #if USE_CG_DRAWING
552 static void
553 mac_draw_cg_image (image, f, gc, src_x, src_y, width, height,
554 dest_x, dest_y, overlay_p)
555 CGImageRef image;
556 struct frame *f;
557 GC gc;
558 int src_x, src_y;
559 unsigned int width, height;
560 int dest_x, dest_y, overlay_p;
561 {
562 CGContextRef context;
563 float port_height = FRAME_PIXEL_HEIGHT (f);
564 CGRect dest_rect = CGRectMake (dest_x, dest_y, width, height);
565
566 context = mac_begin_cg_clip (f, gc);
567 if (!overlay_p)
568 {
569 CG_SET_FILL_COLOR (context, gc->xgcv.background);
570 CGContextFillRect (context, dest_rect);
571 }
572 CGContextClipToRect (context, dest_rect);
573 CGContextScaleCTM (context, 1, -1);
574 CGContextTranslateCTM (context, 0, -port_height);
575 if (CGImageIsMask (image))
576 CG_SET_FILL_COLOR (context, gc->xgcv.foreground);
577 CGContextDrawImage (context,
578 CGRectMake (dest_x - src_x,
579 port_height - (dest_y - src_y
580 + CGImageGetHeight (image)),
581 CGImageGetWidth (image),
582 CGImageGetHeight (image)),
583 image);
584 mac_end_cg_clip (f);
585 }
586
587 #else /* !USE_CG_DRAWING */
588
589 static void
590 mac_draw_bitmap (f, gc, x, y, width, height, bits, overlay_p)
591 struct frame *f;
592 GC gc;
593 int x, y, width, height;
594 unsigned short *bits;
595 int overlay_p;
596 {
597 BitMap bitmap;
598 Rect r;
599
600 bitmap.rowBytes = sizeof(unsigned short);
601 bitmap.baseAddr = (char *)bits;
602 SetRect (&(bitmap.bounds), 0, 0, width, height);
603
604 SetPortWindowPort (FRAME_MAC_WINDOW (f));
605
606 RGBForeColor (GC_FORE_COLOR (gc));
607 RGBBackColor (GC_BACK_COLOR (gc));
608 SetRect (&r, x, y, x + width, y + height);
609
610 mac_begin_clip (gc);
611 #if TARGET_API_MAC_CARBON
612 {
613 CGrafPtr port;
614
615 GetPort (&port);
616 LockPortBits (port);
617 CopyBits (&bitmap, GetPortBitMapForCopyBits (port),
618 &(bitmap.bounds), &r, overlay_p ? srcOr : srcCopy, 0);
619 UnlockPortBits (port);
620 }
621 #else /* not TARGET_API_MAC_CARBON */
622 CopyBits (&bitmap, &(FRAME_MAC_WINDOW (f)->portBits), &(bitmap.bounds), &r,
623 overlay_p ? srcOr : srcCopy, 0);
624 #endif /* not TARGET_API_MAC_CARBON */
625 mac_end_clip (gc);
626
627 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
628 }
629 #endif /* !USE_CG_DRAWING */
630
631
632 /* Mac replacement for XCreateBitmapFromBitmapData. */
633
634 static void
635 mac_create_bitmap_from_bitmap_data (bitmap, bits, w, h)
636 BitMap *bitmap;
637 char *bits;
638 int w, h;
639 {
640 static unsigned char swap_nibble[16]
641 = { 0x0, 0x8, 0x4, 0xc, /* 0000 1000 0100 1100 */
642 0x2, 0xa, 0x6, 0xe, /* 0010 1010 0110 1110 */
643 0x1, 0x9, 0x5, 0xd, /* 0001 1001 0101 1101 */
644 0x3, 0xb, 0x7, 0xf }; /* 0011 1011 0111 1111 */
645 int i, j, w1;
646 char *p;
647
648 w1 = (w + 7) / 8; /* nb of 8bits elt in X bitmap */
649 bitmap->rowBytes = ((w + 15) / 16) * 2; /* nb of 16bits elt in Mac bitmap */
650 bitmap->baseAddr = xmalloc (bitmap->rowBytes * h);
651 bzero (bitmap->baseAddr, bitmap->rowBytes * h);
652 for (i = 0; i < h; i++)
653 {
654 p = bitmap->baseAddr + i * bitmap->rowBytes;
655 for (j = 0; j < w1; j++)
656 {
657 /* Bitswap XBM bytes to match how Mac does things. */
658 unsigned char c = *bits++;
659 *p++ = (unsigned char)((swap_nibble[c & 0xf] << 4)
660 | (swap_nibble[(c>>4) & 0xf]));;
661 }
662 }
663
664 SetRect (&(bitmap->bounds), 0, 0, w, h);
665 }
666
667
668 static void
669 mac_free_bitmap (bitmap)
670 BitMap *bitmap;
671 {
672 xfree (bitmap->baseAddr);
673 }
674
675
676 Pixmap
677 XCreatePixmap (display, w, width, height, depth)
678 Display *display; /* not used */
679 WindowPtr w;
680 unsigned int width, height;
681 unsigned int depth;
682 {
683 Pixmap pixmap;
684 Rect r;
685 QDErr err;
686
687 SetPortWindowPort (w);
688
689 SetRect (&r, 0, 0, width, height);
690 #if !defined (WORDS_BIG_ENDIAN) && USE_CG_DRAWING
691 if (depth == 1)
692 #endif
693 err = NewGWorld (&pixmap, depth, &r, NULL, NULL, 0);
694 #if !defined (WORDS_BIG_ENDIAN) && USE_CG_DRAWING
695 else
696 /* CreateCGImageFromPixMaps requires ARGB format. */
697 err = QTNewGWorld (&pixmap, k32ARGBPixelFormat, &r, NULL, NULL, 0);
698 #endif
699 if (err != noErr)
700 return NULL;
701 return pixmap;
702 }
703
704
705 Pixmap
706 XCreatePixmapFromBitmapData (display, w, data, width, height, fg, bg, depth)
707 Display *display; /* not used */
708 WindowPtr w;
709 char *data;
710 unsigned int width, height;
711 unsigned long fg, bg;
712 unsigned int depth;
713 {
714 Pixmap pixmap;
715 BitMap bitmap;
716 CGrafPtr old_port;
717 GDHandle old_gdh;
718 static GC gc = NULL; /* not reentrant */
719
720 if (gc == NULL)
721 gc = XCreateGC (display, w, 0, NULL);
722
723 pixmap = XCreatePixmap (display, w, width, height, depth);
724 if (pixmap == NULL)
725 return NULL;
726
727 GetGWorld (&old_port, &old_gdh);
728 SetGWorld (pixmap, NULL);
729 mac_create_bitmap_from_bitmap_data (&bitmap, data, width, height);
730 XSetForeground (display, gc, fg);
731 XSetBackground (display, gc, bg);
732 RGBForeColor (GC_FORE_COLOR (gc));
733 RGBBackColor (GC_BACK_COLOR (gc));
734 LockPixels (GetGWorldPixMap (pixmap));
735 #if TARGET_API_MAC_CARBON
736 CopyBits (&bitmap, GetPortBitMapForCopyBits (pixmap),
737 &bitmap.bounds, &bitmap.bounds, srcCopy, 0);
738 #else /* not TARGET_API_MAC_CARBON */
739 CopyBits (&bitmap, &(((GrafPtr)pixmap)->portBits),
740 &bitmap.bounds, &bitmap.bounds, srcCopy, 0);
741 #endif /* not TARGET_API_MAC_CARBON */
742 UnlockPixels (GetGWorldPixMap (pixmap));
743 SetGWorld (old_port, old_gdh);
744 mac_free_bitmap (&bitmap);
745
746 return pixmap;
747 }
748
749
750 /* Mac replacement for XFillRectangle. */
751
752 static void
753 mac_fill_rectangle (f, gc, x, y, width, height)
754 struct frame *f;
755 GC gc;
756 int x, y;
757 unsigned int width, height;
758 {
759 #if USE_CG_DRAWING
760 CGContextRef context;
761
762 context = mac_begin_cg_clip (f, gc);
763 CG_SET_FILL_COLOR (context, gc->xgcv.foreground);
764 CGContextFillRect (context, CGRectMake (x, y, width, height));
765 mac_end_cg_clip (f);
766 #else
767 Rect r;
768
769 SetPortWindowPort (FRAME_MAC_WINDOW (f));
770
771 RGBForeColor (GC_FORE_COLOR (gc));
772 SetRect (&r, x, y, x + width, y + height);
773
774 mac_begin_clip (gc);
775 PaintRect (&r); /* using foreground color of gc */
776 mac_end_clip (gc);
777 #endif
778 }
779
780
781 /* Mac replacement for XDrawRectangle: dest is a window. */
782
783 static void
784 mac_draw_rectangle (f, gc, x, y, width, height)
785 struct frame *f;
786 GC gc;
787 int x, y;
788 unsigned int width, height;
789 {
790 #if USE_CG_DRAWING
791 CGContextRef context;
792
793 context = mac_begin_cg_clip (f, gc);
794 CG_SET_STROKE_COLOR (context, gc->xgcv.foreground);
795 CGContextStrokeRect (context,
796 CGRectMake (x + 0.5f, y + 0.5f, width, height));
797 mac_end_cg_clip (f);
798 #else
799 Rect r;
800
801 SetPortWindowPort (FRAME_MAC_WINDOW (f));
802
803 RGBForeColor (GC_FORE_COLOR (gc));
804 SetRect (&r, x, y, x + width + 1, y + height + 1);
805
806 mac_begin_clip (gc);
807 FrameRect (&r); /* using foreground color of gc */
808 mac_end_clip (gc);
809 #endif
810 }
811
812
813 #if USE_ATSUI
814 static OSStatus
815 atsu_get_text_layout_with_text_ptr (text, text_length, style, text_layout)
816 ConstUniCharArrayPtr text;
817 UniCharCount text_length;
818 ATSUStyle style;
819 ATSUTextLayout *text_layout;
820 {
821 OSStatus err;
822 static ATSUTextLayout saved_text_layout = NULL; /* not reentrant */
823
824 if (saved_text_layout == NULL)
825 {
826 UniCharCount lengths[] = {kATSUToTextEnd};
827 ATSUAttributeTag tags[] = {kATSULineLayoutOptionsTag};
828 ByteCount sizes[] = {sizeof (ATSLineLayoutOptions)};
829 static ATSLineLayoutOptions line_layout =
830 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
831 kATSLineDisableAllLayoutOperations | kATSLineUseDeviceMetrics
832 | kATSLineUseQDRendering
833 #else
834 kATSLineIsDisplayOnly | kATSLineFractDisable
835 #endif
836 ;
837 ATSUAttributeValuePtr values[] = {&line_layout};
838
839 err = ATSUCreateTextLayoutWithTextPtr (text,
840 kATSUFromTextBeginning,
841 kATSUToTextEnd,
842 text_length,
843 1, lengths, &style,
844 &saved_text_layout);
845 if (err == noErr)
846 err = ATSUSetLayoutControls (saved_text_layout,
847 sizeof (tags) / sizeof (tags[0]),
848 tags, sizes, values);
849 /* XXX: Should we do this? */
850 if (err == noErr)
851 err = ATSUSetTransientFontMatching (saved_text_layout, true);
852 }
853 else
854 {
855 err = ATSUSetRunStyle (saved_text_layout, style,
856 kATSUFromTextBeginning, kATSUToTextEnd);
857 if (err == noErr)
858 err = ATSUSetTextPointerLocation (saved_text_layout, text,
859 kATSUFromTextBeginning,
860 kATSUToTextEnd,
861 text_length);
862 }
863
864 if (err == noErr)
865 *text_layout = saved_text_layout;
866 return err;
867 }
868 #endif
869
870
871 static void
872 mac_invert_rectangle (f, x, y, width, height)
873 struct frame *f;
874 int x, y;
875 unsigned int width, height;
876 {
877 Rect r;
878
879 #if USE_CG_DRAWING
880 mac_prepare_for_quickdraw (f);
881 #endif
882 SetPortWindowPort (FRAME_MAC_WINDOW (f));
883
884 SetRect (&r, x, y, x + width, y + height);
885
886 InvertRect (&r);
887 }
888
889
890 static void
891 mac_draw_string_common (f, gc, x, y, buf, nchars, bg_width, bytes_per_char)
892 struct frame *f;
893 GC gc;
894 int x, y;
895 char *buf;
896 int nchars, bg_width, bytes_per_char;
897 {
898 SetPortWindowPort (FRAME_MAC_WINDOW (f));
899
900 #if USE_ATSUI
901 if (GC_FONT (gc)->mac_style)
902 {
903 OSErr err;
904 ATSUTextLayout text_layout;
905
906 xassert (bytes_per_char == 2);
907
908 #ifndef WORDS_BIG_ENDIAN
909 {
910 int i;
911 UniChar *text = (UniChar *)buf;
912
913 for (i = 0; i < nchars; i++)
914 text[i] = EndianU16_BtoN (text[i]);
915 }
916 #endif
917 err = atsu_get_text_layout_with_text_ptr ((ConstUniCharArrayPtr)buf,
918 nchars,
919 GC_FONT (gc)->mac_style,
920 &text_layout);
921 if (err != noErr)
922 return;
923 #ifdef MAC_OSX
924 if (!mac_use_core_graphics)
925 {
926 #endif
927 #if USE_CG_DRAWING
928 mac_prepare_for_quickdraw (f);
929 #endif
930 mac_begin_clip (gc);
931 RGBForeColor (GC_FORE_COLOR (gc));
932 if (bg_width)
933 {
934 Rect r;
935
936 SetRect (&r, x, y - FONT_BASE (GC_FONT (gc)),
937 x + bg_width, y + FONT_DESCENT (GC_FONT (gc)));
938 RGBBackColor (GC_BACK_COLOR (gc));
939 EraseRect (&r);
940 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
941 }
942 MoveTo (x, y);
943 ATSUDrawText (text_layout,
944 kATSUFromTextBeginning, kATSUToTextEnd,
945 kATSUUseGrafPortPenLoc, kATSUUseGrafPortPenLoc);
946 mac_end_clip (gc);
947 #ifdef MAC_OSX
948 }
949 else
950 {
951 CGrafPtr port;
952 CGContextRef context;
953 float port_height = FRAME_PIXEL_HEIGHT (f);
954 ATSUAttributeTag tags[] = {kATSUCGContextTag};
955 ByteCount sizes[] = {sizeof (CGContextRef)};
956 ATSUAttributeValuePtr values[] = {&context};
957
958 #if USE_CG_DRAWING
959 context = mac_begin_cg_clip (f, gc);
960 #else
961 GetPort (&port);
962 QDBeginCGContext (port, &context);
963 if (gc->n_clip_rects || bg_width)
964 {
965 CGContextTranslateCTM (context, 0, port_height);
966 CGContextScaleCTM (context, 1, -1);
967 if (gc->n_clip_rects)
968 CGContextClipToRects (context, gc->clip_rects,
969 gc->n_clip_rects);
970 #endif
971 if (bg_width)
972 {
973 CG_SET_FILL_COLOR (context, gc->xgcv.background);
974 CGContextFillRect
975 (context,
976 CGRectMake (x, y - FONT_BASE (GC_FONT (gc)),
977 bg_width, FONT_HEIGHT (GC_FONT (gc))));
978 }
979 CGContextScaleCTM (context, 1, -1);
980 CGContextTranslateCTM (context, 0, -port_height);
981 #if !USE_CG_DRAWING
982 }
983 #endif
984 CG_SET_FILL_COLOR (context, gc->xgcv.foreground);
985 err = ATSUSetLayoutControls (text_layout,
986 sizeof (tags) / sizeof (tags[0]),
987 tags, sizes, values);
988 if (err == noErr)
989 ATSUDrawText (text_layout,
990 kATSUFromTextBeginning, kATSUToTextEnd,
991 Long2Fix (x), Long2Fix (port_height - y));
992 #if USE_CG_DRAWING
993 mac_end_cg_clip (f);
994 context = NULL;
995 #else
996 CGContextSynchronize (context);
997 QDEndCGContext (port, &context);
998 #endif
999 #if 0
1000 /* This doesn't work on Mac OS X 10.1. */
1001 ATSUClearLayoutControls (text_layout,
1002 sizeof (tags) / sizeof (tags[0]), tags);
1003 #else
1004 ATSUSetLayoutControls (text_layout,
1005 sizeof (tags) / sizeof (tags[0]),
1006 tags, sizes, values);
1007 #endif
1008 }
1009 #endif /* MAC_OSX */
1010 }
1011 else
1012 #endif /* USE_ATSUI */
1013 {
1014 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
1015 UInt32 savedFlags;
1016
1017 if (mac_use_core_graphics)
1018 savedFlags = SwapQDTextFlags (kQDUseCGTextRendering);
1019 #endif
1020 #if USE_CG_DRAWING
1021 mac_prepare_for_quickdraw (f);
1022 #endif
1023 mac_begin_clip (gc);
1024 RGBForeColor (GC_FORE_COLOR (gc));
1025 #ifdef MAC_OS8
1026 if (bg_width)
1027 {
1028 RGBBackColor (GC_BACK_COLOR (gc));
1029 TextMode (srcCopy);
1030 }
1031 else
1032 TextMode (srcOr);
1033 #else
1034 /* We prefer not to use srcCopy text transfer mode on Mac OS X
1035 because:
1036 - Screen is double-buffered. (In srcCopy mode, a text is
1037 drawn into an offscreen graphics world first. So
1038 performance gain cannot be expected.)
1039 - It lowers rendering quality.
1040 - Some fonts leave garbage on cursor movement. */
1041 if (bg_width)
1042 {
1043 Rect r;
1044
1045 RGBBackColor (GC_BACK_COLOR (gc));
1046 SetRect (&r, x, y - FONT_BASE (GC_FONT (gc)),
1047 x + bg_width, y + FONT_DESCENT (GC_FONT (gc)));
1048 EraseRect (&r);
1049 }
1050 TextMode (srcOr);
1051 #endif
1052 TextFont (GC_FONT (gc)->mac_fontnum);
1053 TextSize (GC_FONT (gc)->mac_fontsize);
1054 TextFace (GC_FONT (gc)->mac_fontface);
1055 MoveTo (x, y);
1056 DrawText (buf, 0, nchars * bytes_per_char);
1057 if (bg_width)
1058 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
1059 mac_end_clip (gc);
1060
1061 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
1062 if (mac_use_core_graphics)
1063 SwapQDTextFlags(savedFlags);
1064 #endif
1065 }
1066 }
1067
1068
1069 /* Mac replacement for XDrawString. */
1070
1071 static void
1072 mac_draw_string (f, gc, x, y, buf, nchars)
1073 struct frame *f;
1074 GC gc;
1075 int x, y;
1076 char *buf;
1077 int nchars;
1078 {
1079 mac_draw_string_common (f, gc, x, y, buf, nchars, 0, 1);
1080 }
1081
1082
1083 /* Mac replacement for XDrawString16. */
1084
1085 static void
1086 mac_draw_string_16 (f, gc, x, y, buf, nchars)
1087 struct frame *f;
1088 GC gc;
1089 int x, y;
1090 XChar2b *buf;
1091 int nchars;
1092 {
1093 mac_draw_string_common (f, gc, x, y, (char *) buf, nchars, 0, 2);
1094 }
1095
1096
1097 /* Mac replacement for XDrawImageString. */
1098
1099 static void
1100 mac_draw_image_string (f, gc, x, y, buf, nchars, bg_width)
1101 struct frame *f;
1102 GC gc;
1103 int x, y;
1104 char *buf;
1105 int nchars, bg_width;
1106 {
1107 mac_draw_string_common (f, gc, x, y, buf, nchars, bg_width, 1);
1108 }
1109
1110
1111 /* Mac replacement for XDrawString16. */
1112
1113 static void
1114 mac_draw_image_string_16 (f, gc, x, y, buf, nchars, bg_width)
1115 struct frame *f;
1116 GC gc;
1117 int x, y;
1118 XChar2b *buf;
1119 int nchars, bg_width;
1120 {
1121 mac_draw_string_common (f, gc, x, y, (char *) buf, nchars, bg_width, 2);
1122 }
1123
1124
1125 /* Mac replacement for XQueryTextExtents, but takes a character. If
1126 STYLE is NULL, measurement is done by QuickDraw Text routines for
1127 the font of the current graphics port. If CG_GLYPH is not NULL,
1128 *CG_GLYPH is set to the glyph ID or 0 if it cannot be obtained. */
1129
1130 static OSErr
1131 mac_query_char_extents (style, c,
1132 font_ascent_return, font_descent_return,
1133 overall_return, cg_glyph)
1134 #if USE_ATSUI
1135 ATSUStyle style;
1136 #else
1137 void *style;
1138 #endif
1139 int c;
1140 int *font_ascent_return, *font_descent_return;
1141 XCharStruct *overall_return;
1142 #if USE_CG_TEXT_DRAWING
1143 CGGlyph *cg_glyph;
1144 #else
1145 void *cg_glyph;
1146 #endif
1147 {
1148 OSErr err = noErr;
1149 int width;
1150 Rect char_bounds;
1151
1152 #if USE_ATSUI
1153 if (style)
1154 {
1155 ATSUTextLayout text_layout;
1156 UniChar ch = c;
1157
1158 err = atsu_get_text_layout_with_text_ptr (&ch, 1, style, &text_layout);
1159 if (err == noErr)
1160 {
1161 ATSTrapezoid glyph_bounds;
1162
1163 err = ATSUGetGlyphBounds (text_layout, 0, 0,
1164 kATSUFromTextBeginning, kATSUToTextEnd,
1165 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
1166 kATSUseFractionalOrigins,
1167 #else
1168 kATSUseDeviceOrigins,
1169 #endif
1170 1, &glyph_bounds, NULL);
1171 if (err == noErr)
1172 {
1173 xassert (glyph_bounds.lowerRight.x - glyph_bounds.lowerLeft.x
1174 == glyph_bounds.upperRight.x - glyph_bounds.upperLeft.x);
1175
1176 width = Fix2Long (glyph_bounds.upperRight.x
1177 - glyph_bounds.upperLeft.x);
1178 if (font_ascent_return)
1179 *font_ascent_return = -Fix2Long (glyph_bounds.upperLeft.y);
1180 if (font_descent_return)
1181 *font_descent_return = Fix2Long (glyph_bounds.lowerLeft.y);
1182 }
1183 }
1184 if (err == noErr && overall_return)
1185 {
1186 err = ATSUMeasureTextImage (text_layout,
1187 kATSUFromTextBeginning, kATSUToTextEnd,
1188 0, 0, &char_bounds);
1189 if (err == noErr)
1190 STORE_XCHARSTRUCT (*overall_return, width, char_bounds);
1191 #if USE_CG_TEXT_DRAWING
1192 if (err == noErr && cg_glyph)
1193 {
1194 OSErr err1;
1195 ATSUGlyphInfoArray glyph_info_array;
1196 ByteCount count = sizeof (ATSUGlyphInfoArray);
1197
1198 err1 = ATSUMatchFontsToText (text_layout, kATSUFromTextBeginning,
1199 kATSUToTextEnd, NULL, NULL, NULL);
1200 if (err1 == noErr)
1201 err1 = ATSUGetGlyphInfo (text_layout, kATSUFromTextBeginning,
1202 kATSUToTextEnd, &count,
1203 &glyph_info_array);
1204 if (err1 == noErr)
1205 {
1206 xassert (glyph_info_array.glyphs[0].glyphID);
1207 *cg_glyph = glyph_info_array.glyphs[0].glyphID;
1208 }
1209 else
1210 *cg_glyph = 0;
1211 }
1212 #endif
1213 }
1214 }
1215 else
1216 #endif
1217 {
1218 if (font_ascent_return || font_descent_return)
1219 {
1220 FontInfo font_info;
1221
1222 GetFontInfo (&font_info);
1223 if (font_ascent_return)
1224 *font_ascent_return = font_info.ascent;
1225 if (font_descent_return)
1226 *font_descent_return = font_info.descent;
1227 }
1228 if (overall_return)
1229 {
1230 char ch = c;
1231
1232 width = CharWidth (ch);
1233 QDTextBounds (1, &ch, &char_bounds);
1234 STORE_XCHARSTRUCT (*overall_return, width, char_bounds);
1235 }
1236 }
1237
1238 return err;
1239 }
1240
1241
1242 /* Mac replacement for XTextExtents16. Only sets horizontal metrics. */
1243
1244 static int
1245 mac_text_extents_16 (font_struct, string, nchars, overall_return)
1246 XFontStruct *font_struct;
1247 XChar2b *string;
1248 int nchars;
1249 XCharStruct *overall_return;
1250 {
1251 int i;
1252 short width = 0, lbearing = 0, rbearing = 0;
1253 XCharStruct *pcm;
1254
1255 for (i = 0; i < nchars; i++)
1256 {
1257 pcm = mac_per_char_metric (font_struct, string, 0);
1258 if (pcm == NULL)
1259 width += FONT_WIDTH (font_struct);
1260 else
1261 {
1262 lbearing = min (lbearing, width + pcm->lbearing);
1263 rbearing = max (rbearing, width + pcm->rbearing);
1264 width += pcm->width;
1265 }
1266 string++;
1267 }
1268
1269 overall_return->lbearing = lbearing;
1270 overall_return->rbearing = rbearing;
1271 overall_return->width = width;
1272
1273 /* What's the meaning of the return value of XTextExtents16? */
1274 }
1275
1276
1277 #if USE_CG_TEXT_DRAWING
1278 static int cg_text_anti_aliasing_threshold = 8;
1279
1280 static void
1281 init_cg_text_anti_aliasing_threshold ()
1282 {
1283 int threshold;
1284 Boolean valid_p;
1285
1286 threshold =
1287 CFPreferencesGetAppIntegerValue (CFSTR ("AppleAntiAliasingThreshold"),
1288 kCFPreferencesCurrentApplication,
1289 &valid_p);
1290 if (valid_p)
1291 cg_text_anti_aliasing_threshold = threshold;
1292 }
1293
1294 static int
1295 mac_draw_image_string_cg (f, gc, x, y, buf, nchars, bg_width)
1296 struct frame *f;
1297 GC gc;
1298 int x, y;
1299 XChar2b *buf;
1300 int nchars, bg_width;
1301 {
1302 CGrafPtr port;
1303 float port_height, gx, gy;
1304 int i;
1305 CGContextRef context;
1306 CGGlyph *glyphs;
1307 CGSize *advances;
1308
1309 if (!mac_use_core_graphics || GC_FONT (gc)->cg_font == NULL)
1310 return 0;
1311
1312 port = GetWindowPort (FRAME_MAC_WINDOW (f));
1313 port_height = FRAME_PIXEL_HEIGHT (f);
1314 gx = x;
1315 gy = port_height - y;
1316 glyphs = (CGGlyph *)buf;
1317 advances = alloca (sizeof (CGSize) * nchars);
1318 if (advances == NULL)
1319 return 0;
1320 for (i = 0; i < nchars; i++)
1321 {
1322 XCharStruct *pcm = mac_per_char_metric (GC_FONT (gc), buf, 0);
1323
1324 advances[i].width = pcm->width;
1325 advances[i].height = 0;
1326 glyphs[i] = GC_FONT (gc)->cg_glyphs[buf->byte2];
1327 buf++;
1328 }
1329
1330 #if USE_CG_DRAWING
1331 context = mac_begin_cg_clip (f, gc);
1332 #else
1333 QDBeginCGContext (port, &context);
1334 if (gc->n_clip_rects || bg_width)
1335 {
1336 CGContextTranslateCTM (context, 0, port_height);
1337 CGContextScaleCTM (context, 1, -1);
1338 if (gc->n_clip_rects)
1339 CGContextClipToRects (context, gc->clip_rects, gc->n_clip_rects);
1340 #endif
1341 if (bg_width)
1342 {
1343 CG_SET_FILL_COLOR (context, gc->xgcv.background);
1344 CGContextFillRect
1345 (context,
1346 CGRectMake (gx, y - FONT_BASE (GC_FONT (gc)),
1347 bg_width, FONT_HEIGHT (GC_FONT (gc))));
1348 }
1349 CGContextScaleCTM (context, 1, -1);
1350 CGContextTranslateCTM (context, 0, -port_height);
1351 #if !USE_CG_DRAWING
1352 }
1353 #endif
1354 CG_SET_FILL_COLOR (context, gc->xgcv.foreground);
1355 CGContextSetFont (context, GC_FONT (gc)->cg_font);
1356 CGContextSetFontSize (context, GC_FONT (gc)->mac_fontsize);
1357 if (GC_FONT (gc)->mac_fontsize <= cg_text_anti_aliasing_threshold)
1358 CGContextSetShouldAntialias (context, false);
1359 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1030
1360 CGContextSetTextPosition (context, gx, gy);
1361 CGContextShowGlyphsWithAdvances (context, glyphs, advances, nchars);
1362 #else
1363 for (i = 0; i < nchars; i++)
1364 {
1365 CGContextShowGlyphsAtPoint (context, gx, gy, glyphs + i, 1);
1366 gx += advances[i].width;
1367 }
1368 #endif
1369 #if USE_CG_DRAWING
1370 mac_end_cg_clip (f);
1371 #else
1372 CGContextSynchronize (context);
1373 QDEndCGContext (port, &context);
1374 #endif
1375
1376 return 1;
1377 }
1378 #endif
1379
1380
1381 #if !USE_CG_DRAWING
1382 /* Mac replacement for XCopyArea: dest must be window. */
1383
1384 static void
1385 mac_copy_area (src, f, gc, src_x, src_y, width, height, dest_x, dest_y)
1386 Pixmap src;
1387 struct frame *f;
1388 GC gc;
1389 int src_x, src_y;
1390 unsigned int width, height;
1391 int dest_x, dest_y;
1392 {
1393 Rect src_r, dest_r;
1394
1395 SetPortWindowPort (FRAME_MAC_WINDOW (f));
1396
1397 SetRect (&src_r, src_x, src_y, src_x + width, src_y + height);
1398 SetRect (&dest_r, dest_x, dest_y, dest_x + width, dest_y + height);
1399
1400 ForeColor (blackColor);
1401 BackColor (whiteColor);
1402
1403 mac_begin_clip (gc);
1404 LockPixels (GetGWorldPixMap (src));
1405 #if TARGET_API_MAC_CARBON
1406 {
1407 CGrafPtr port;
1408
1409 GetPort (&port);
1410 LockPortBits (port);
1411 CopyBits (GetPortBitMapForCopyBits (src),
1412 GetPortBitMapForCopyBits (port),
1413 &src_r, &dest_r, srcCopy, 0);
1414 UnlockPortBits (port);
1415 }
1416 #else /* not TARGET_API_MAC_CARBON */
1417 CopyBits (&(((GrafPtr)src)->portBits), &(FRAME_MAC_WINDOW (f)->portBits),
1418 &src_r, &dest_r, srcCopy, 0);
1419 #endif /* not TARGET_API_MAC_CARBON */
1420 UnlockPixels (GetGWorldPixMap (src));
1421 mac_end_clip (gc);
1422
1423 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
1424 }
1425
1426
1427 static void
1428 mac_copy_area_with_mask (src, mask, f, gc, src_x, src_y,
1429 width, height, dest_x, dest_y)
1430 Pixmap src, mask;
1431 struct frame *f;
1432 GC gc;
1433 int src_x, src_y;
1434 unsigned int width, height;
1435 int dest_x, dest_y;
1436 {
1437 Rect src_r, dest_r;
1438
1439 SetPortWindowPort (FRAME_MAC_WINDOW (f));
1440
1441 SetRect (&src_r, src_x, src_y, src_x + width, src_y + height);
1442 SetRect (&dest_r, dest_x, dest_y, dest_x + width, dest_y + height);
1443
1444 ForeColor (blackColor);
1445 BackColor (whiteColor);
1446
1447 mac_begin_clip (gc);
1448 LockPixels (GetGWorldPixMap (src));
1449 LockPixels (GetGWorldPixMap (mask));
1450 #if TARGET_API_MAC_CARBON
1451 {
1452 CGrafPtr port;
1453
1454 GetPort (&port);
1455 LockPortBits (port);
1456 CopyMask (GetPortBitMapForCopyBits (src), GetPortBitMapForCopyBits (mask),
1457 GetPortBitMapForCopyBits (port),
1458 &src_r, &src_r, &dest_r);
1459 UnlockPortBits (port);
1460 }
1461 #else /* not TARGET_API_MAC_CARBON */
1462 CopyMask (&(((GrafPtr)src)->portBits), &(((GrafPtr)mask)->portBits),
1463 &(FRAME_MAC_WINDOW (f)->portBits), &src_r, &src_r, &dest_r);
1464 #endif /* not TARGET_API_MAC_CARBON */
1465 UnlockPixels (GetGWorldPixMap (mask));
1466 UnlockPixels (GetGWorldPixMap (src));
1467 mac_end_clip (gc);
1468
1469 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
1470 }
1471 #endif /* !USE_CG_DRAWING */
1472
1473
1474 /* Mac replacement for XCopyArea: used only for scrolling. */
1475
1476 static void
1477 mac_scroll_area (f, gc, src_x, src_y, width, height, dest_x, dest_y)
1478 struct frame *f;
1479 GC gc;
1480 int src_x, src_y;
1481 unsigned int width, height;
1482 int dest_x, dest_y;
1483 {
1484 #if TARGET_API_MAC_CARBON
1485 Rect src_r;
1486 RgnHandle dummy = NewRgn (); /* For avoiding update events. */
1487
1488 SetRect (&src_r, src_x, src_y, src_x + width, src_y + height);
1489 #if USE_CG_DRAWING
1490 mac_prepare_for_quickdraw (f);
1491 #endif
1492 ScrollWindowRect (FRAME_MAC_WINDOW (f),
1493 &src_r, dest_x - src_x, dest_y - src_y,
1494 kScrollWindowNoOptions, dummy);
1495 DisposeRgn (dummy);
1496 #else /* not TARGET_API_MAC_CARBON */
1497 Rect src_r, dest_r;
1498 WindowPtr w = FRAME_MAC_WINDOW (f);
1499
1500 SetPort (w);
1501
1502 SetRect (&src_r, src_x, src_y, src_x + width, src_y + height);
1503 SetRect (&dest_r, dest_x, dest_y, dest_x + width, dest_y + height);
1504
1505 /* In Color QuickDraw, set ForeColor and BackColor as follows to avoid
1506 color mapping in CopyBits. Otherwise, it will be slow. */
1507 ForeColor (blackColor);
1508 BackColor (whiteColor);
1509 mac_begin_clip (gc);
1510 CopyBits (&(w->portBits), &(w->portBits), &src_r, &dest_r, srcCopy, 0);
1511 mac_end_clip (gc);
1512
1513 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
1514 #endif /* not TARGET_API_MAC_CARBON */
1515 }
1516
1517
1518 /* Mac replacement for XChangeGC. */
1519
1520 static void
1521 XChangeGC (display, gc, mask, xgcv)
1522 Display *display;
1523 GC gc;
1524 unsigned long mask;
1525 XGCValues *xgcv;
1526 {
1527 if (mask & GCForeground)
1528 XSetForeground (display, gc, xgcv->foreground);
1529 if (mask & GCBackground)
1530 XSetBackground (display, gc, xgcv->background);
1531 if (mask & GCFont)
1532 XSetFont (display, gc, xgcv->font);
1533 }
1534
1535
1536 /* Mac replacement for XCreateGC. */
1537
1538 GC
1539 XCreateGC (display, window, mask, xgcv)
1540 Display *display;
1541 Window window;
1542 unsigned long mask;
1543 XGCValues *xgcv;
1544 {
1545 GC gc = xmalloc (sizeof (*gc));
1546
1547 bzero (gc, sizeof (*gc));
1548 XChangeGC (display, gc, mask, xgcv);
1549
1550 return gc;
1551 }
1552
1553
1554 /* Used in xfaces.c. */
1555
1556 void
1557 XFreeGC (display, gc)
1558 Display *display;
1559 GC gc;
1560 {
1561 if (gc->clip_region)
1562 DisposeRgn (gc->clip_region);
1563 xfree (gc);
1564 }
1565
1566
1567 /* Mac replacement for XGetGCValues. */
1568
1569 static void
1570 XGetGCValues (display, gc, mask, xgcv)
1571 Display *display;
1572 GC gc;
1573 unsigned long mask;
1574 XGCValues *xgcv;
1575 {
1576 if (mask & GCForeground)
1577 xgcv->foreground = gc->xgcv.foreground;
1578 if (mask & GCBackground)
1579 xgcv->background = gc->xgcv.background;
1580 if (mask & GCFont)
1581 xgcv->font = gc->xgcv.font;
1582 }
1583
1584
1585 /* Mac replacement for XSetForeground. */
1586
1587 void
1588 XSetForeground (display, gc, color)
1589 Display *display;
1590 GC gc;
1591 unsigned long color;
1592 {
1593 if (gc->xgcv.foreground != color)
1594 {
1595 gc->xgcv.foreground = color;
1596 gc->fore_color.red = RED16_FROM_ULONG (color);
1597 gc->fore_color.green = GREEN16_FROM_ULONG (color);
1598 gc->fore_color.blue = BLUE16_FROM_ULONG (color);
1599 }
1600 }
1601
1602
1603 /* Mac replacement for XSetBackground. */
1604
1605 void
1606 XSetBackground (display, gc, color)
1607 Display *display;
1608 GC gc;
1609 unsigned long color;
1610 {
1611 if (gc->xgcv.background != color)
1612 {
1613 gc->xgcv.background = color;
1614 gc->back_color.red = RED16_FROM_ULONG (color);
1615 gc->back_color.green = GREEN16_FROM_ULONG (color);
1616 gc->back_color.blue = BLUE16_FROM_ULONG (color);
1617 }
1618 }
1619
1620
1621 /* Mac replacement for XSetFont. */
1622
1623 static void
1624 XSetFont (display, gc, font)
1625 Display *display;
1626 GC gc;
1627 XFontStruct *font;
1628 {
1629 gc->xgcv.font = font;
1630 }
1631
1632
1633 /* Mac replacement for XSetClipRectangles. */
1634
1635 static void
1636 mac_set_clip_rectangles (display, gc, rectangles, n)
1637 Display *display;
1638 GC gc;
1639 Rect *rectangles;
1640 int n;
1641 {
1642 int i;
1643
1644 xassert (n >= 0 && n <= MAX_CLIP_RECTS);
1645
1646 gc->n_clip_rects = n;
1647 if (n > 0)
1648 {
1649 if (gc->clip_region == NULL)
1650 gc->clip_region = NewRgn ();
1651 RectRgn (gc->clip_region, rectangles);
1652 if (n > 1)
1653 {
1654 RgnHandle region = NewRgn ();
1655
1656 for (i = 1; i < n; i++)
1657 {
1658 RectRgn (region, rectangles + i);
1659 UnionRgn (gc->clip_region, region, gc->clip_region);
1660 }
1661 DisposeRgn (region);
1662 }
1663 }
1664 #if defined (MAC_OSX) && (USE_ATSUI || USE_CG_DRAWING)
1665 for (i = 0; i < n; i++)
1666 {
1667 Rect *rect = rectangles + i;
1668
1669 gc->clip_rects[i] = CGRectMake (rect->left, rect->top,
1670 rect->right - rect->left,
1671 rect->bottom - rect->top);
1672 }
1673 #endif
1674 }
1675
1676
1677 /* Mac replacement for XSetClipMask. */
1678
1679 static INLINE void
1680 mac_reset_clip_rectangles (display, gc)
1681 Display *display;
1682 GC gc;
1683 {
1684 gc->n_clip_rects = 0;
1685 }
1686
1687
1688 /* Mac replacement for XSetWindowBackground. */
1689
1690 void
1691 XSetWindowBackground (display, w, color)
1692 Display *display;
1693 WindowPtr w;
1694 unsigned long color;
1695 {
1696 #if !TARGET_API_MAC_CARBON
1697 AuxWinHandle aw_handle;
1698 CTabHandle ctab_handle;
1699 ColorSpecPtr ct_table;
1700 short ct_size;
1701 #endif
1702 RGBColor bg_color;
1703
1704 bg_color.red = RED16_FROM_ULONG (color);
1705 bg_color.green = GREEN16_FROM_ULONG (color);
1706 bg_color.blue = BLUE16_FROM_ULONG (color);
1707
1708 #if TARGET_API_MAC_CARBON
1709 SetWindowContentColor (w, &bg_color);
1710 #else
1711 if (GetAuxWin (w, &aw_handle))
1712 {
1713 ctab_handle = (*aw_handle)->awCTable;
1714 HandToHand ((Handle *) &ctab_handle);
1715 ct_table = (*ctab_handle)->ctTable;
1716 ct_size = (*ctab_handle)->ctSize;
1717 while (ct_size > -1)
1718 {
1719 if (ct_table->value == 0)
1720 {
1721 ct_table->rgb = bg_color;
1722 CTabChanged (ctab_handle);
1723 SetWinColor (w, (WCTabHandle) ctab_handle);
1724 }
1725 ct_size--;
1726 }
1727 }
1728 #endif
1729 }
1730
1731 /* Flush display of frame F, or of all frames if F is null. */
1732
1733 static void
1734 x_flush (f)
1735 struct frame *f;
1736 {
1737 #if TARGET_API_MAC_CARBON
1738 BLOCK_INPUT;
1739 #if USE_CG_DRAWING
1740 mac_prepare_for_quickdraw (f);
1741 #endif
1742 if (f)
1743 QDFlushPortBuffer (GetWindowPort (FRAME_MAC_WINDOW (f)), NULL);
1744 else
1745 QDFlushPortBuffer (GetQDGlobalsThePort (), NULL);
1746 UNBLOCK_INPUT;
1747 #endif
1748 }
1749
1750
1751 /* Remove calls to XFlush by defining XFlush to an empty replacement.
1752 Calls to XFlush should be unnecessary because the X output buffer
1753 is flushed automatically as needed by calls to XPending,
1754 XNextEvent, or XWindowEvent according to the XFlush man page.
1755 XTread_socket calls XPending. Removing XFlush improves
1756 performance. */
1757
1758 #define XFlush(DISPLAY) (void) 0
1759
1760 \f
1761 /* Return the struct mac_display_info corresponding to DPY. There's
1762 only one. */
1763
1764 struct mac_display_info *
1765 mac_display_info_for_display (dpy)
1766 Display *dpy;
1767 {
1768 return &one_mac_display_info;
1769 }
1770
1771
1772 \f
1773 /***********************************************************************
1774 Starting and ending an update
1775 ***********************************************************************/
1776
1777 /* Start an update of frame F. This function is installed as a hook
1778 for update_begin, i.e. it is called when update_begin is called.
1779 This function is called prior to calls to x_update_window_begin for
1780 each window being updated. */
1781
1782 static void
1783 x_update_begin (f)
1784 struct frame *f;
1785 {
1786 #if TARGET_API_MAC_CARBON
1787 /* During update of a frame, availability of input events is
1788 periodically checked with ReceiveNextEvent if
1789 redisplay-dont-pause is nil. That normally flushes window buffer
1790 changes for every check, and thus screen update looks waving even
1791 if no input is available. So we disable screen updates during
1792 update of a frame. */
1793 BLOCK_INPUT;
1794 DisableScreenUpdates ();
1795 UNBLOCK_INPUT;
1796 #endif
1797 }
1798
1799
1800 /* Start update of window W. Set the global variable updated_window
1801 to the window being updated and set output_cursor to the cursor
1802 position of W. */
1803
1804 static void
1805 x_update_window_begin (w)
1806 struct window *w;
1807 {
1808 struct frame *f = XFRAME (WINDOW_FRAME (w));
1809 struct mac_display_info *display_info = FRAME_MAC_DISPLAY_INFO (f);
1810
1811 updated_window = w;
1812 set_output_cursor (&w->cursor);
1813
1814 BLOCK_INPUT;
1815
1816 if (f == display_info->mouse_face_mouse_frame)
1817 {
1818 /* Don't do highlighting for mouse motion during the update. */
1819 display_info->mouse_face_defer = 1;
1820
1821 /* If F needs to be redrawn, simply forget about any prior mouse
1822 highlighting. */
1823 if (FRAME_GARBAGED_P (f))
1824 display_info->mouse_face_window = Qnil;
1825
1826 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
1827 their mouse_face_p flag set, which means that they are always
1828 unequal to rows in a desired matrix which never have that
1829 flag set. So, rows containing mouse-face glyphs are never
1830 scrolled, and we don't have to switch the mouse highlight off
1831 here to prevent it from being scrolled. */
1832
1833 /* Can we tell that this update does not affect the window
1834 where the mouse highlight is? If so, no need to turn off.
1835 Likewise, don't do anything if the frame is garbaged;
1836 in that case, the frame's current matrix that we would use
1837 is all wrong, and we will redisplay that line anyway. */
1838 if (!NILP (display_info->mouse_face_window)
1839 && w == XWINDOW (display_info->mouse_face_window))
1840 {
1841 int i;
1842
1843 for (i = 0; i < w->desired_matrix->nrows; ++i)
1844 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
1845 break;
1846
1847 if (i < w->desired_matrix->nrows)
1848 clear_mouse_face (display_info);
1849 }
1850 #endif /* 0 */
1851 }
1852
1853 UNBLOCK_INPUT;
1854 }
1855
1856
1857 /* Draw a vertical window border from (x,y0) to (x,y1) */
1858
1859 static void
1860 mac_draw_vertical_window_border (w, x, y0, y1)
1861 struct window *w;
1862 int x, y0, y1;
1863 {
1864 struct frame *f = XFRAME (WINDOW_FRAME (w));
1865 struct face *face;
1866
1867 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
1868 if (face)
1869 XSetForeground (FRAME_MAC_DISPLAY (f), f->output_data.mac->normal_gc,
1870 face->foreground);
1871
1872 mac_draw_line (f, f->output_data.mac->normal_gc, x, y0, x, y1);
1873 }
1874
1875 /* End update of window W (which is equal to updated_window).
1876
1877 Draw vertical borders between horizontally adjacent windows, and
1878 display W's cursor if CURSOR_ON_P is non-zero.
1879
1880 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
1881 glyphs in mouse-face were overwritten. In that case we have to
1882 make sure that the mouse-highlight is properly redrawn.
1883
1884 W may be a menu bar pseudo-window in case we don't have X toolkit
1885 support. Such windows don't have a cursor, so don't display it
1886 here. */
1887
1888 static void
1889 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
1890 struct window *w;
1891 int cursor_on_p, mouse_face_overwritten_p;
1892 {
1893 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (XFRAME (w->frame));
1894
1895 if (!w->pseudo_window_p)
1896 {
1897 BLOCK_INPUT;
1898
1899 if (cursor_on_p)
1900 display_and_set_cursor (w, 1, output_cursor.hpos,
1901 output_cursor.vpos,
1902 output_cursor.x, output_cursor.y);
1903
1904 if (draw_window_fringes (w, 1))
1905 x_draw_vertical_border (w);
1906
1907 UNBLOCK_INPUT;
1908 }
1909
1910 /* If a row with mouse-face was overwritten, arrange for
1911 XTframe_up_to_date to redisplay the mouse highlight. */
1912 if (mouse_face_overwritten_p)
1913 {
1914 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
1915 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
1916 dpyinfo->mouse_face_window = Qnil;
1917 }
1918
1919 updated_window = NULL;
1920 }
1921
1922
1923 /* End update of frame F. This function is installed as a hook in
1924 update_end. */
1925
1926 static void
1927 x_update_end (f)
1928 struct frame *f;
1929 {
1930 /* Mouse highlight may be displayed again. */
1931 FRAME_MAC_DISPLAY_INFO (f)->mouse_face_defer = 0;
1932
1933 BLOCK_INPUT;
1934 #if TARGET_API_MAC_CARBON
1935 EnableScreenUpdates ();
1936 #endif
1937 XFlush (FRAME_MAC_DISPLAY (f));
1938 UNBLOCK_INPUT;
1939 }
1940
1941
1942 /* This function is called from various places in xdisp.c whenever a
1943 complete update has been performed. The global variable
1944 updated_window is not available here. */
1945
1946 static void
1947 XTframe_up_to_date (f)
1948 struct frame *f;
1949 {
1950 if (FRAME_MAC_P (f))
1951 {
1952 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
1953
1954 if (dpyinfo->mouse_face_deferred_gc
1955 || f == dpyinfo->mouse_face_mouse_frame)
1956 {
1957 BLOCK_INPUT;
1958 if (dpyinfo->mouse_face_mouse_frame)
1959 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
1960 dpyinfo->mouse_face_mouse_x,
1961 dpyinfo->mouse_face_mouse_y);
1962 dpyinfo->mouse_face_deferred_gc = 0;
1963 UNBLOCK_INPUT;
1964 }
1965 }
1966 }
1967
1968
1969 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
1970 arrow bitmaps, or clear the fringes if no bitmaps are required
1971 before DESIRED_ROW is made current. The window being updated is
1972 found in updated_window. This function is called from
1973 update_window_line only if it is known that there are differences
1974 between bitmaps to be drawn between current row and DESIRED_ROW. */
1975
1976 static void
1977 x_after_update_window_line (desired_row)
1978 struct glyph_row *desired_row;
1979 {
1980 struct window *w = updated_window;
1981 struct frame *f;
1982 int width, height;
1983
1984 xassert (w);
1985
1986 if (!desired_row->mode_line_p && !w->pseudo_window_p)
1987 desired_row->redraw_fringe_bitmaps_p = 1;
1988
1989 /* When a window has disappeared, make sure that no rest of
1990 full-width rows stays visible in the internal border. Could
1991 check here if updated_window is the leftmost/rightmost window,
1992 but I guess it's not worth doing since vertically split windows
1993 are almost never used, internal border is rarely set, and the
1994 overhead is very small. */
1995 if (windows_or_buffers_changed
1996 && desired_row->full_width_p
1997 && (f = XFRAME (w->frame),
1998 width = FRAME_INTERNAL_BORDER_WIDTH (f),
1999 width != 0)
2000 && (height = desired_row->visible_height,
2001 height > 0))
2002 {
2003 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
2004
2005 /* Internal border is drawn below the tool bar. */
2006 if (WINDOWP (f->tool_bar_window)
2007 && w == XWINDOW (f->tool_bar_window))
2008 y -= width;
2009
2010 BLOCK_INPUT;
2011 mac_clear_area (f, 0, y, width, height);
2012 mac_clear_area (f, FRAME_PIXEL_WIDTH (f) - width, y, width, height);
2013 UNBLOCK_INPUT;
2014 }
2015 }
2016
2017
2018 /* Draw the bitmap WHICH in one of the left or right fringes of
2019 window W. ROW is the glyph row for which to display the bitmap; it
2020 determines the vertical position at which the bitmap has to be
2021 drawn. */
2022
2023 static void
2024 x_draw_fringe_bitmap (w, row, p)
2025 struct window *w;
2026 struct glyph_row *row;
2027 struct draw_fringe_bitmap_params *p;
2028 {
2029 struct frame *f = XFRAME (WINDOW_FRAME (w));
2030 Display *display = FRAME_MAC_DISPLAY (f);
2031 struct face *face = p->face;
2032 int rowY;
2033
2034 /* Must clip because of partially visible lines. */
2035 rowY = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
2036 if (p->y < rowY)
2037 {
2038 /* Adjust position of "bottom aligned" bitmap on partially
2039 visible last row. */
2040 int oldY = row->y;
2041 int oldVH = row->visible_height;
2042 row->visible_height = p->h;
2043 row->y -= rowY - p->y;
2044 x_clip_to_row (w, row, -1, face->gc);
2045 row->y = oldY;
2046 row->visible_height = oldVH;
2047 }
2048 else
2049 x_clip_to_row (w, row, -1, face->gc);
2050
2051 if (p->bx >= 0 && !p->overlay_p)
2052 {
2053 #if 0 /* MAC_TODO: stipple */
2054 /* In case the same realized face is used for fringes and
2055 for something displayed in the text (e.g. face `region' on
2056 mono-displays, the fill style may have been changed to
2057 FillSolid in x_draw_glyph_string_background. */
2058 if (face->stipple)
2059 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
2060 else
2061 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
2062 #endif
2063
2064 mac_erase_rectangle (f, face->gc, p->bx, p->by, p->nx, p->ny);
2065
2066 #if 0 /* MAC_TODO: stipple */
2067 if (!face->stipple)
2068 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
2069 #endif
2070 }
2071
2072 if (p->which
2073 #if USE_CG_DRAWING
2074 && p->which < max_fringe_bmp
2075 #endif
2076 )
2077 {
2078 XGCValues gcv;
2079
2080 XGetGCValues (display, face->gc, GCForeground, &gcv);
2081 XSetForeground (display, face->gc,
2082 (p->cursor_p
2083 ? (p->overlay_p ? face->background
2084 : f->output_data.mac->cursor_pixel)
2085 : face->foreground));
2086 #if USE_CG_DRAWING
2087 mac_draw_cg_image (fringe_bmp[p->which], f, face->gc, 0, p->dh,
2088 p->wd, p->h, p->x, p->y, p->overlay_p);
2089 #else
2090 mac_draw_bitmap (f, face->gc, p->x, p->y,
2091 p->wd, p->h, p->bits + p->dh, p->overlay_p);
2092 #endif
2093 XSetForeground (display, face->gc, gcv.foreground);
2094 }
2095
2096 mac_reset_clip_rectangles (display, face->gc);
2097 }
2098
2099 #if USE_CG_DRAWING
2100 static void
2101 mac_define_fringe_bitmap (which, bits, h, wd)
2102 int which;
2103 unsigned short *bits;
2104 int h, wd;
2105 {
2106 int i;
2107 CGDataProviderRef provider;
2108
2109 if (which >= max_fringe_bmp)
2110 {
2111 i = max_fringe_bmp;
2112 max_fringe_bmp = which + 20;
2113 fringe_bmp = (CGImageRef *) xrealloc (fringe_bmp, max_fringe_bmp * sizeof (CGImageRef));
2114 while (i < max_fringe_bmp)
2115 fringe_bmp[i++] = 0;
2116 }
2117
2118 for (i = 0; i < h; i++)
2119 bits[i] = ~bits[i];
2120 provider = CGDataProviderCreateWithData (NULL, bits,
2121 sizeof (unsigned short) * h, NULL);
2122 if (provider)
2123 {
2124 fringe_bmp[which] = CGImageMaskCreate (wd, h, 1, 1,
2125 sizeof (unsigned short),
2126 provider, NULL, 0);
2127 CGDataProviderRelease (provider);
2128 }
2129 }
2130
2131 static void
2132 mac_destroy_fringe_bitmap (which)
2133 int which;
2134 {
2135 if (which >= max_fringe_bmp)
2136 return;
2137
2138 if (fringe_bmp[which])
2139 CGImageRelease (fringe_bmp[which]);
2140 fringe_bmp[which] = 0;
2141 }
2142 #endif
2143 \f
2144
2145 /* This is called when starting Emacs and when restarting after
2146 suspend. When starting Emacs, no window is mapped. And nothing
2147 must be done to Emacs's own window if it is suspended (though that
2148 rarely happens). */
2149
2150 static void
2151 XTset_terminal_modes ()
2152 {
2153 }
2154
2155 /* This is called when exiting or suspending Emacs. Exiting will make
2156 the windows go away, and suspending requires no action. */
2157
2158 static void
2159 XTreset_terminal_modes ()
2160 {
2161 }
2162
2163
2164 \f
2165 /***********************************************************************
2166 Display Iterator
2167 ***********************************************************************/
2168
2169 /* Function prototypes of this page. */
2170
2171 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
2172 static int mac_encode_char P_ ((int, XChar2b *, struct font_info *, int *));
2173
2174
2175 static void
2176 pcm_init (pcm, count)
2177 XCharStruct *pcm;
2178 int count;
2179 {
2180 bzero (pcm, sizeof (XCharStruct) * count);
2181 while (--count >= 0)
2182 {
2183 pcm->descent = PCM_INVALID;
2184 pcm++;
2185 }
2186 }
2187
2188 static enum pcm_status
2189 pcm_get_status (pcm)
2190 XCharStruct *pcm;
2191 {
2192 int height = pcm->ascent + pcm->descent;
2193
2194 /* Negative height means some special status. */
2195 return height >= 0 ? PCM_VALID : height;
2196 }
2197
2198 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
2199 is not contained in the font. */
2200
2201 static INLINE XCharStruct *
2202 x_per_char_metric (font, char2b)
2203 XFontStruct *font;
2204 XChar2b *char2b;
2205 {
2206 /* The result metric information. */
2207 XCharStruct *pcm = NULL;
2208
2209 xassert (font && char2b);
2210
2211 #if USE_ATSUI
2212 if (font->mac_style)
2213 {
2214 XCharStruct **row = font->bounds.rows + char2b->byte1;
2215
2216 if (*row == NULL)
2217 {
2218 *row = xmalloc (sizeof (XCharStruct) * 0x100);
2219 pcm_init (*row, 0x100);
2220 }
2221 pcm = *row + char2b->byte2;
2222 if (pcm_get_status (pcm) != PCM_VALID)
2223 {
2224 BLOCK_INPUT;
2225 mac_query_char_extents (font->mac_style,
2226 (char2b->byte1 << 8) + char2b->byte2,
2227 NULL, NULL, pcm, NULL);
2228 UNBLOCK_INPUT;
2229 }
2230 }
2231 else
2232 {
2233 #endif
2234 if (font->bounds.per_char != NULL)
2235 {
2236 if (font->min_byte1 == 0 && font->max_byte1 == 0)
2237 {
2238 /* min_char_or_byte2 specifies the linear character index
2239 corresponding to the first element of the per_char array,
2240 max_char_or_byte2 is the index of the last character. A
2241 character with non-zero CHAR2B->byte1 is not in the font.
2242 A character with byte2 less than min_char_or_byte2 or
2243 greater max_char_or_byte2 is not in the font. */
2244 if (char2b->byte1 == 0
2245 && char2b->byte2 >= font->min_char_or_byte2
2246 && char2b->byte2 <= font->max_char_or_byte2)
2247 pcm = font->bounds.per_char
2248 + (char2b->byte2 - font->min_char_or_byte2);
2249 }
2250 else
2251 {
2252 /* If either min_byte1 or max_byte1 are nonzero, both
2253 min_char_or_byte2 and max_char_or_byte2 are less than
2254 256, and the 2-byte character index values corresponding
2255 to the per_char array element N (counting from 0) are:
2256
2257 byte1 = N/D + min_byte1
2258 byte2 = N\D + min_char_or_byte2
2259
2260 where:
2261
2262 D = max_char_or_byte2 - min_char_or_byte2 + 1
2263 / = integer division
2264 \ = integer modulus */
2265 if (char2b->byte1 >= font->min_byte1
2266 && char2b->byte1 <= font->max_byte1
2267 && char2b->byte2 >= font->min_char_or_byte2
2268 && char2b->byte2 <= font->max_char_or_byte2)
2269 {
2270 pcm = (font->bounds.per_char
2271 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
2272 * (char2b->byte1 - font->min_byte1))
2273 + (char2b->byte2 - font->min_char_or_byte2));
2274 }
2275 }
2276 }
2277 else
2278 {
2279 /* If the per_char pointer is null, all glyphs between the first
2280 and last character indexes inclusive have the same
2281 information, as given by both min_bounds and max_bounds. */
2282 if (char2b->byte2 >= font->min_char_or_byte2
2283 && char2b->byte2 <= font->max_char_or_byte2)
2284 pcm = &font->max_bounds;
2285 }
2286 #if USE_ATSUI
2287 }
2288 #endif
2289
2290 return ((pcm == NULL
2291 || (pcm->width == 0
2292 #if 0 /* Show hollow boxes for zero-width glyphs such as combining diacritics. */
2293 && (pcm->rbearing - pcm->lbearing) == 0
2294 #endif
2295 ))
2296 ? NULL : pcm);
2297 }
2298
2299 /* RIF:
2300 */
2301
2302 static XCharStruct *
2303 mac_per_char_metric (font, char2b, font_type)
2304 XFontStruct *font;
2305 XChar2b *char2b;
2306 int font_type;
2307 {
2308 return x_per_char_metric (font, char2b);
2309 }
2310
2311 /* RIF:
2312 Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
2313 the two-byte form of C. Encoding is returned in *CHAR2B. */
2314
2315 static int
2316 mac_encode_char (c, char2b, font_info, two_byte_p)
2317 int c;
2318 XChar2b *char2b;
2319 struct font_info *font_info;
2320 int *two_byte_p;
2321 {
2322 int charset = CHAR_CHARSET (c);
2323 XFontStruct *font = font_info->font;
2324
2325 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
2326 This may be either a program in a special encoder language or a
2327 fixed encoding. */
2328 if (font_info->font_encoder)
2329 {
2330 /* It's a program. */
2331 struct ccl_program *ccl = font_info->font_encoder;
2332
2333 check_ccl_update (ccl);
2334 if (CHARSET_DIMENSION (charset) == 1)
2335 {
2336 ccl->reg[0] = charset;
2337 ccl->reg[1] = char2b->byte2;
2338 ccl->reg[2] = -1;
2339 }
2340 else
2341 {
2342 ccl->reg[0] = charset;
2343 ccl->reg[1] = char2b->byte1;
2344 ccl->reg[2] = char2b->byte2;
2345 }
2346
2347 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
2348
2349 /* We assume that MSBs are appropriately set/reset by CCL
2350 program. */
2351 if (font->max_byte1 == 0) /* 1-byte font */
2352 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
2353 else
2354 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
2355 }
2356 else if (font_info->encoding[charset])
2357 {
2358 /* Fixed encoding scheme. See fontset.h for the meaning of the
2359 encoding numbers. */
2360 int enc = font_info->encoding[charset];
2361
2362 if ((enc == 1 || enc == 2)
2363 && CHARSET_DIMENSION (charset) == 2)
2364 char2b->byte1 |= 0x80;
2365
2366 if (enc == 1 || enc == 3)
2367 char2b->byte2 |= 0x80;
2368
2369 if (enc == 4)
2370 {
2371 int sjis1, sjis2;
2372
2373 ENCODE_SJIS (char2b->byte1, char2b->byte2, sjis1, sjis2);
2374 char2b->byte1 = sjis1;
2375 char2b->byte2 = sjis2;
2376 }
2377 }
2378
2379 if (two_byte_p)
2380 *two_byte_p = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
2381
2382 return FONT_TYPE_UNKNOWN;
2383 }
2384
2385
2386 \f
2387 /***********************************************************************
2388 Glyph display
2389 ***********************************************************************/
2390
2391
2392
2393 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2394 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2395 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2396 int));
2397 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2398 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2399 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2400 static void x_draw_glyph_string P_ ((struct glyph_string *));
2401 static void mac_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2402 static void x_set_cursor_gc P_ ((struct glyph_string *));
2403 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2404 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2405 /*static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2406 unsigned long *, double, int));*/
2407 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2408 double, int, unsigned long));
2409 static void x_setup_relief_colors P_ ((struct glyph_string *));
2410 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2411 static void x_draw_image_relief P_ ((struct glyph_string *));
2412 static void x_draw_image_foreground P_ ((struct glyph_string *));
2413 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2414 int, int, int));
2415 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2416 int, int, int, int, int, int,
2417 Rect *));
2418 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2419 int, int, int, Rect *));
2420
2421 #if GLYPH_DEBUG
2422 static void x_check_font P_ ((struct frame *, XFontStruct *));
2423 #endif
2424
2425
2426 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2427 face. */
2428
2429 static void
2430 x_set_cursor_gc (s)
2431 struct glyph_string *s;
2432 {
2433 if (s->font == FRAME_FONT (s->f)
2434 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2435 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2436 && !s->cmp)
2437 s->gc = s->f->output_data.mac->cursor_gc;
2438 else
2439 {
2440 /* Cursor on non-default face: must merge. */
2441 XGCValues xgcv;
2442 unsigned long mask;
2443
2444 xgcv.background = s->f->output_data.mac->cursor_pixel;
2445 xgcv.foreground = s->face->background;
2446
2447 /* If the glyph would be invisible, try a different foreground. */
2448 if (xgcv.foreground == xgcv.background)
2449 xgcv.foreground = s->face->foreground;
2450 if (xgcv.foreground == xgcv.background)
2451 xgcv.foreground = s->f->output_data.mac->cursor_foreground_pixel;
2452 if (xgcv.foreground == xgcv.background)
2453 xgcv.foreground = s->face->foreground;
2454
2455 /* Make sure the cursor is distinct from text in this face. */
2456 if (xgcv.background == s->face->background
2457 && xgcv.foreground == s->face->foreground)
2458 {
2459 xgcv.background = s->face->foreground;
2460 xgcv.foreground = s->face->background;
2461 }
2462
2463 IF_DEBUG (x_check_font (s->f, s->font));
2464 xgcv.font = s->font;
2465 mask = GCForeground | GCBackground | GCFont;
2466
2467 if (FRAME_MAC_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2468 XChangeGC (s->display, FRAME_MAC_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2469 mask, &xgcv);
2470 else
2471 FRAME_MAC_DISPLAY_INFO (s->f)->scratch_cursor_gc
2472 = XCreateGC (s->display, s->window, mask, &xgcv);
2473
2474 s->gc = FRAME_MAC_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2475 }
2476 }
2477
2478
2479 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2480
2481 static void
2482 x_set_mouse_face_gc (s)
2483 struct glyph_string *s;
2484 {
2485 int face_id;
2486 struct face *face;
2487
2488 /* What face has to be used last for the mouse face? */
2489 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2490 face = FACE_FROM_ID (s->f, face_id);
2491 if (face == NULL)
2492 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2493
2494 if (s->first_glyph->type == CHAR_GLYPH)
2495 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2496 else
2497 face_id = FACE_FOR_CHAR (s->f, face, 0);
2498 s->face = FACE_FROM_ID (s->f, face_id);
2499 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2500
2501 /* If font in this face is same as S->font, use it. */
2502 if (s->font == s->face->font)
2503 s->gc = s->face->gc;
2504 else
2505 {
2506 /* Otherwise construct scratch_cursor_gc with values from FACE
2507 but font FONT. */
2508 XGCValues xgcv;
2509 unsigned long mask;
2510
2511 xgcv.background = s->face->background;
2512 xgcv.foreground = s->face->foreground;
2513 IF_DEBUG (x_check_font (s->f, s->font));
2514 xgcv.font = s->font;
2515 mask = GCForeground | GCBackground | GCFont;
2516
2517 if (FRAME_MAC_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2518 XChangeGC (s->display, FRAME_MAC_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2519 mask, &xgcv);
2520 else
2521 FRAME_MAC_DISPLAY_INFO (s->f)->scratch_cursor_gc
2522 = XCreateGC (s->display, s->window, mask, &xgcv);
2523
2524 s->gc = FRAME_MAC_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2525 }
2526
2527 xassert (s->gc != 0);
2528 }
2529
2530
2531 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2532 Faces to use in the mode line have already been computed when the
2533 matrix was built, so there isn't much to do, here. */
2534
2535 static INLINE void
2536 x_set_mode_line_face_gc (s)
2537 struct glyph_string *s;
2538 {
2539 s->gc = s->face->gc;
2540 }
2541
2542
2543 /* Set S->gc of glyph string S for drawing that glyph string. Set
2544 S->stippled_p to a non-zero value if the face of S has a stipple
2545 pattern. */
2546
2547 static INLINE void
2548 x_set_glyph_string_gc (s)
2549 struct glyph_string *s;
2550 {
2551 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2552
2553 if (s->hl == DRAW_NORMAL_TEXT)
2554 {
2555 s->gc = s->face->gc;
2556 s->stippled_p = s->face->stipple != 0;
2557 }
2558 else if (s->hl == DRAW_INVERSE_VIDEO)
2559 {
2560 x_set_mode_line_face_gc (s);
2561 s->stippled_p = s->face->stipple != 0;
2562 }
2563 else if (s->hl == DRAW_CURSOR)
2564 {
2565 x_set_cursor_gc (s);
2566 s->stippled_p = 0;
2567 }
2568 else if (s->hl == DRAW_MOUSE_FACE)
2569 {
2570 x_set_mouse_face_gc (s);
2571 s->stippled_p = s->face->stipple != 0;
2572 }
2573 else if (s->hl == DRAW_IMAGE_RAISED
2574 || s->hl == DRAW_IMAGE_SUNKEN)
2575 {
2576 s->gc = s->face->gc;
2577 s->stippled_p = s->face->stipple != 0;
2578 }
2579 else
2580 {
2581 s->gc = s->face->gc;
2582 s->stippled_p = s->face->stipple != 0;
2583 }
2584
2585 /* GC must have been set. */
2586 xassert (s->gc != 0);
2587 }
2588
2589
2590 /* Set clipping for output of glyph string S. S may be part of a mode
2591 line or menu if we don't have X toolkit support. */
2592
2593 static INLINE void
2594 x_set_glyph_string_clipping (s)
2595 struct glyph_string *s;
2596 {
2597 Rect rects[MAX_CLIP_RECTS];
2598 int n;
2599
2600 n = get_glyph_string_clip_rects (s, rects, MAX_CLIP_RECTS);
2601 mac_set_clip_rectangles (s->display, s->gc, rects, n);
2602 }
2603
2604
2605 /* RIF:
2606 Compute left and right overhang of glyph string S. If S is a glyph
2607 string for a composition, assume overhangs don't exist. */
2608
2609 static void
2610 mac_compute_glyph_string_overhangs (s)
2611 struct glyph_string *s;
2612 {
2613 if (!(s->cmp == NULL
2614 && s->first_glyph->type == CHAR_GLYPH))
2615 return;
2616
2617 if (!s->two_byte_p
2618 #if USE_ATSUI
2619 || s->font->mac_style
2620 #endif
2621 )
2622 {
2623 XCharStruct cs;
2624
2625 mac_text_extents_16 (s->font, s->char2b, s->nchars, &cs);
2626 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2627 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2628 }
2629 else
2630 {
2631 Rect r;
2632 MacFontStruct *font = s->font;
2633
2634 TextFont (font->mac_fontnum);
2635 TextSize (font->mac_fontsize);
2636 TextFace (font->mac_fontface);
2637
2638 QDTextBounds (s->nchars * 2, (char *)s->char2b, &r);
2639
2640 s->right_overhang = r.right > s->width ? r.right - s->width : 0;
2641 s->left_overhang = r.left < 0 ? -r.left : 0;
2642 }
2643 }
2644
2645
2646 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
2647
2648 static INLINE void
2649 x_clear_glyph_string_rect (s, x, y, w, h)
2650 struct glyph_string *s;
2651 int x, y, w, h;
2652 {
2653 mac_erase_rectangle (s->f, s->gc, x, y, w, h);
2654 }
2655
2656
2657 /* Draw the background of glyph_string S. If S->background_filled_p
2658 is non-zero don't draw it. FORCE_P non-zero means draw the
2659 background even if it wouldn't be drawn normally. This is used
2660 when a string preceding S draws into the background of S, or S
2661 contains the first component of a composition. */
2662
2663 static void
2664 x_draw_glyph_string_background (s, force_p)
2665 struct glyph_string *s;
2666 int force_p;
2667 {
2668 /* Nothing to do if background has already been drawn or if it
2669 shouldn't be drawn in the first place. */
2670 if (!s->background_filled_p)
2671 {
2672 int box_line_width = max (s->face->box_line_width, 0);
2673
2674 #if 0 /* MAC_TODO: stipple */
2675 if (s->stippled_p)
2676 {
2677 /* Fill background with a stipple pattern. */
2678 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2679 XFillRectangle (s->display, s->window, s->gc, s->x,
2680 s->y + box_line_width,
2681 s->background_width,
2682 s->height - 2 * box_line_width);
2683 XSetFillStyle (s->display, s->gc, FillSolid);
2684 s->background_filled_p = 1;
2685 }
2686 else
2687 #endif
2688 if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
2689 || s->font_not_found_p
2690 || s->extends_to_end_of_line_p
2691 || force_p)
2692 {
2693 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
2694 s->background_width,
2695 s->height - 2 * box_line_width);
2696 s->background_filled_p = 1;
2697 }
2698 }
2699 }
2700
2701
2702 /* Draw the foreground of glyph string S. */
2703
2704 static void
2705 x_draw_glyph_string_foreground (s)
2706 struct glyph_string *s;
2707 {
2708 int i, x, bg_width;
2709
2710 /* If first glyph of S has a left box line, start drawing the text
2711 of S to the right of that box line. */
2712 if (s->face->box != FACE_NO_BOX
2713 && s->first_glyph->left_box_line_p)
2714 x = s->x + abs (s->face->box_line_width);
2715 else
2716 x = s->x;
2717
2718 /* Draw characters of S as rectangles if S's font could not be
2719 loaded. */
2720 if (s->font_not_found_p)
2721 {
2722 for (i = 0; i < s->nchars; ++i)
2723 {
2724 struct glyph *g = s->first_glyph + i;
2725 mac_draw_rectangle (s->f, s->gc, x, s->y,
2726 g->pixel_width - 1, s->height - 1);
2727 x += g->pixel_width;
2728 }
2729 }
2730 else
2731 {
2732 char *char1b = (char *) s->char2b;
2733 int boff = s->font_info->baseline_offset;
2734
2735 if (s->font_info->vertical_centering)
2736 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
2737
2738 /* If we can use 8-bit functions, condense S->char2b. */
2739 if (!s->two_byte_p
2740 #if USE_ATSUI
2741 && GC_FONT (s->gc)->mac_style == NULL
2742 #endif
2743 )
2744 for (i = 0; i < s->nchars; ++i)
2745 char1b[i] = s->char2b[i].byte2;
2746
2747 /* Draw text with XDrawString if background has already been
2748 filled. Otherwise, use XDrawImageString. (Note that
2749 XDrawImageString is usually faster than XDrawString.) Always
2750 use XDrawImageString when drawing the cursor so that there is
2751 no chance that characters under a box cursor are invisible. */
2752 if (s->for_overlaps
2753 || (s->background_filled_p && s->hl != DRAW_CURSOR))
2754 bg_width = 0; /* Corresponds to XDrawString. */
2755 else
2756 bg_width = s->background_width; /* Corresponds to XDrawImageString. */
2757
2758 if (s->two_byte_p
2759 #if USE_ATSUI
2760 || GC_FONT (s->gc)->mac_style
2761 #endif
2762 )
2763 #if USE_CG_TEXT_DRAWING
2764 if (!s->two_byte_p
2765 && mac_draw_image_string_cg (s->f, s->gc, x, s->ybase - boff,
2766 s->char2b, s->nchars, bg_width))
2767 ;
2768 else
2769 #endif
2770 mac_draw_image_string_16 (s->f, s->gc, x, s->ybase - boff,
2771 s->char2b, s->nchars, bg_width);
2772 else
2773 mac_draw_image_string (s->f, s->gc, x, s->ybase - boff,
2774 char1b, s->nchars, bg_width);
2775 }
2776 }
2777
2778 /* Draw the foreground of composite glyph string S. */
2779
2780 static void
2781 x_draw_composite_glyph_string_foreground (s)
2782 struct glyph_string *s;
2783 {
2784 int i, x;
2785
2786 /* If first glyph of S has a left box line, start drawing the text
2787 of S to the right of that box line. */
2788 if (s->face->box != FACE_NO_BOX
2789 && s->first_glyph->left_box_line_p)
2790 x = s->x + abs (s->face->box_line_width);
2791 else
2792 x = s->x;
2793
2794 /* S is a glyph string for a composition. S->gidx is the index of
2795 the first character drawn for glyphs of this composition.
2796 S->gidx == 0 means we are drawing the very first character of
2797 this composition. */
2798
2799 /* Draw a rectangle for the composition if the font for the very
2800 first character of the composition could not be loaded. */
2801 if (s->font_not_found_p)
2802 {
2803 if (s->gidx == 0)
2804 mac_draw_rectangle (s->f, s->gc, x, s->y,
2805 s->width - 1, s->height - 1);
2806 }
2807 else
2808 {
2809 for (i = 0; i < s->nchars; i++, ++s->gidx)
2810 mac_draw_string_16 (s->f, s->gc,
2811 x + s->cmp->offsets[s->gidx * 2],
2812 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
2813 s->char2b + i, 1);
2814 }
2815 }
2816
2817
2818 #ifdef USE_X_TOOLKIT
2819
2820 static struct frame *x_frame_of_widget P_ ((Widget));
2821
2822
2823 /* Return the frame on which widget WIDGET is used.. Abort if frame
2824 cannot be determined. */
2825
2826 static struct frame *
2827 x_frame_of_widget (widget)
2828 Widget widget;
2829 {
2830 struct x_display_info *dpyinfo;
2831 Lisp_Object tail;
2832 struct frame *f;
2833
2834 dpyinfo = x_display_info_for_display (XtDisplay (widget));
2835
2836 /* Find the top-level shell of the widget. Note that this function
2837 can be called when the widget is not yet realized, so XtWindow
2838 (widget) == 0. That's the reason we can't simply use
2839 x_any_window_to_frame. */
2840 while (!XtIsTopLevelShell (widget))
2841 widget = XtParent (widget);
2842
2843 /* Look for a frame with that top-level widget. Allocate the color
2844 on that frame to get the right gamma correction value. */
2845 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
2846 if (GC_FRAMEP (XCAR (tail))
2847 && (f = XFRAME (XCAR (tail)),
2848 (f->output_data.nothing != 1
2849 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
2850 && f->output_data.x->widget == widget)
2851 return f;
2852
2853 abort ();
2854 }
2855
2856
2857 /* Allocate the color COLOR->pixel on the screen and display of
2858 widget WIDGET in colormap CMAP. If an exact match cannot be
2859 allocated, try the nearest color available. Value is non-zero
2860 if successful. This is called from lwlib. */
2861
2862 int
2863 x_alloc_nearest_color_for_widget (widget, cmap, color)
2864 Widget widget;
2865 Colormap cmap;
2866 XColor *color;
2867 {
2868 struct frame *f = x_frame_of_widget (widget);
2869 return x_alloc_nearest_color (f, cmap, color);
2870 }
2871
2872
2873 #endif /* USE_X_TOOLKIT */
2874
2875 #if 0 /* MAC_TODO */
2876
2877 /* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
2878 CMAP. If an exact match can't be allocated, try the nearest color
2879 available. Value is non-zero if successful. Set *COLOR to the
2880 color allocated. */
2881
2882 int
2883 x_alloc_nearest_color (f, cmap, color)
2884 struct frame *f;
2885 Colormap cmap;
2886 XColor *color;
2887 {
2888 Display *display = FRAME_X_DISPLAY (f);
2889 Screen *screen = FRAME_X_SCREEN (f);
2890 int rc;
2891
2892 gamma_correct (f, color);
2893 rc = XAllocColor (display, cmap, color);
2894 if (rc == 0)
2895 {
2896 /* If we got to this point, the colormap is full, so we're going
2897 to try to get the next closest color. The algorithm used is
2898 a least-squares matching, which is what X uses for closest
2899 color matching with StaticColor visuals. */
2900 int nearest, i;
2901 unsigned long nearest_delta = ~0;
2902 int ncells = XDisplayCells (display, XScreenNumberOfScreen (screen));
2903 XColor *cells = (XColor *) alloca (ncells * sizeof *cells);
2904
2905 for (i = 0; i < ncells; ++i)
2906 cells[i].pixel = i;
2907 XQueryColors (display, cmap, cells, ncells);
2908
2909 for (nearest = i = 0; i < ncells; ++i)
2910 {
2911 long dred = (color->red >> 8) - (cells[i].red >> 8);
2912 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
2913 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
2914 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
2915
2916 if (delta < nearest_delta)
2917 {
2918 nearest = i;
2919 nearest_delta = delta;
2920 }
2921 }
2922
2923 color->red = cells[nearest].red;
2924 color->green = cells[nearest].green;
2925 color->blue = cells[nearest].blue;
2926 rc = XAllocColor (display, cmap, color);
2927 }
2928
2929 #ifdef DEBUG_X_COLORS
2930 if (rc)
2931 register_color (color->pixel);
2932 #endif /* DEBUG_X_COLORS */
2933
2934 return rc;
2935 }
2936
2937
2938 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
2939 It's necessary to do this instead of just using PIXEL directly to
2940 get color reference counts right. */
2941
2942 unsigned long
2943 x_copy_color (f, pixel)
2944 struct frame *f;
2945 unsigned long pixel;
2946 {
2947 XColor color;
2948
2949 color.pixel = pixel;
2950 BLOCK_INPUT;
2951 XQueryColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
2952 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
2953 UNBLOCK_INPUT;
2954 #ifdef DEBUG_X_COLORS
2955 register_color (pixel);
2956 #endif
2957 return color.pixel;
2958 }
2959
2960
2961 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
2962 It's necessary to do this instead of just using PIXEL directly to
2963 get color reference counts right. */
2964
2965 unsigned long
2966 x_copy_dpy_color (dpy, cmap, pixel)
2967 Display *dpy;
2968 Colormap cmap;
2969 unsigned long pixel;
2970 {
2971 XColor color;
2972
2973 color.pixel = pixel;
2974 BLOCK_INPUT;
2975 XQueryColor (dpy, cmap, &color);
2976 XAllocColor (dpy, cmap, &color);
2977 UNBLOCK_INPUT;
2978 #ifdef DEBUG_X_COLORS
2979 register_color (pixel);
2980 #endif
2981 return color.pixel;
2982 }
2983
2984 #endif /* MAC_TODO */
2985
2986
2987 /* Brightness beyond which a color won't have its highlight brightness
2988 boosted.
2989
2990 Nominally, highlight colors for `3d' faces are calculated by
2991 brightening an object's color by a constant scale factor, but this
2992 doesn't yield good results for dark colors, so for colors who's
2993 brightness is less than this value (on a scale of 0-255) have to
2994 use an additional additive factor.
2995
2996 The value here is set so that the default menu-bar/mode-line color
2997 (grey75) will not have its highlights changed at all. */
2998 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 187
2999
3000
3001 /* Allocate a color which is lighter or darker than *COLOR by FACTOR
3002 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3003 If this produces the same color as COLOR, try a color where all RGB
3004 values have DELTA added. Return the allocated color in *COLOR.
3005 DISPLAY is the X display, CMAP is the colormap to operate on.
3006 Value is non-zero if successful. */
3007
3008 static int
3009 mac_alloc_lighter_color (f, color, factor, delta)
3010 struct frame *f;
3011 unsigned long *color;
3012 double factor;
3013 int delta;
3014 {
3015 unsigned long new;
3016 long bright;
3017
3018 /* On Mac, RGB values are 0-255, not 0-65535, so scale delta. */
3019 delta /= 256;
3020
3021 /* Change RGB values by specified FACTOR. Avoid overflow! */
3022 xassert (factor >= 0);
3023 new = RGB_TO_ULONG (min (0xff, (int) (factor * RED_FROM_ULONG (*color))),
3024 min (0xff, (int) (factor * GREEN_FROM_ULONG (*color))),
3025 min (0xff, (int) (factor * BLUE_FROM_ULONG (*color))));
3026
3027 /* Calculate brightness of COLOR. */
3028 bright = (2 * RED_FROM_ULONG (*color) + 3 * GREEN_FROM_ULONG (*color)
3029 + BLUE_FROM_ULONG (*color)) / 6;
3030
3031 /* We only boost colors that are darker than
3032 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3033 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3034 /* Make an additive adjustment to NEW, because it's dark enough so
3035 that scaling by FACTOR alone isn't enough. */
3036 {
3037 /* How far below the limit this color is (0 - 1, 1 being darker). */
3038 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3039 /* The additive adjustment. */
3040 int min_delta = delta * dimness * factor / 2;
3041
3042 if (factor < 1)
3043 new = RGB_TO_ULONG (max (0, min (0xff, (int) (RED_FROM_ULONG (*color)) - min_delta)),
3044 max (0, min (0xff, (int) (GREEN_FROM_ULONG (*color)) - min_delta)),
3045 max (0, min (0xff, (int) (BLUE_FROM_ULONG (*color)) - min_delta)));
3046 else
3047 new = RGB_TO_ULONG (max (0, min (0xff, (int) (min_delta + RED_FROM_ULONG (*color)))),
3048 max (0, min (0xff, (int) (min_delta + GREEN_FROM_ULONG (*color)))),
3049 max (0, min (0xff, (int) (min_delta + BLUE_FROM_ULONG (*color)))));
3050 }
3051
3052 if (new == *color)
3053 new = RGB_TO_ULONG (max (0, min (0xff, (int) (delta + RED_FROM_ULONG (*color)))),
3054 max (0, min (0xff, (int) (delta + GREEN_FROM_ULONG (*color)))),
3055 max (0, min (0xff, (int) (delta + BLUE_FROM_ULONG (*color)))));
3056
3057 /* MAC_TODO: Map to palette and retry with delta if same? */
3058 /* MAC_TODO: Free colors (if using palette)? */
3059
3060 if (new == *color)
3061 return 0;
3062
3063 *color = new;
3064
3065 return 1;
3066 }
3067
3068
3069 /* Set up the foreground color for drawing relief lines of glyph
3070 string S. RELIEF is a pointer to a struct relief containing the GC
3071 with which lines will be drawn. Use a color that is FACTOR or
3072 DELTA lighter or darker than the relief's background which is found
3073 in S->f->output_data.x->relief_background. If such a color cannot
3074 be allocated, use DEFAULT_PIXEL, instead. */
3075
3076 static void
3077 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3078 struct frame *f;
3079 struct relief *relief;
3080 double factor;
3081 int delta;
3082 unsigned long default_pixel;
3083 {
3084 XGCValues xgcv;
3085 struct mac_output *di = f->output_data.mac;
3086 unsigned long mask = GCForeground;
3087 unsigned long pixel;
3088 unsigned long background = di->relief_background;
3089 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
3090
3091 /* MAC_TODO: Free colors (if using palette)? */
3092
3093 /* Allocate new color. */
3094 xgcv.foreground = default_pixel;
3095 pixel = background;
3096 if (dpyinfo->n_planes != 1
3097 && mac_alloc_lighter_color (f, &pixel, factor, delta))
3098 {
3099 relief->allocated_p = 1;
3100 xgcv.foreground = relief->pixel = pixel;
3101 }
3102
3103 if (relief->gc == 0)
3104 {
3105 #if 0 /* MAC_TODO: stipple */
3106 xgcv.stipple = dpyinfo->gray;
3107 mask |= GCStipple;
3108 #endif
3109 relief->gc = XCreateGC (NULL, FRAME_MAC_WINDOW (f), mask, &xgcv);
3110 }
3111 else
3112 XChangeGC (NULL, relief->gc, mask, &xgcv);
3113 }
3114
3115
3116 /* Set up colors for the relief lines around glyph string S. */
3117
3118 static void
3119 x_setup_relief_colors (s)
3120 struct glyph_string *s;
3121 {
3122 struct mac_output *di = s->f->output_data.mac;
3123 unsigned long color;
3124
3125 if (s->face->use_box_color_for_shadows_p)
3126 color = s->face->box_color;
3127 else if (s->first_glyph->type == IMAGE_GLYPH
3128 && s->img->pixmap
3129 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
3130 color = IMAGE_BACKGROUND (s->img, s->f, 0);
3131 else
3132 {
3133 XGCValues xgcv;
3134
3135 /* Get the background color of the face. */
3136 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3137 color = xgcv.background;
3138 }
3139
3140 if (di->white_relief.gc == 0
3141 || color != di->relief_background)
3142 {
3143 di->relief_background = color;
3144 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3145 WHITE_PIX_DEFAULT (s->f));
3146 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3147 BLACK_PIX_DEFAULT (s->f));
3148 }
3149 }
3150
3151
3152 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3153 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3154 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3155 relief. LEFT_P non-zero means draw a relief on the left side of
3156 the rectangle. RIGHT_P non-zero means draw a relief on the right
3157 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3158 when drawing. */
3159
3160 static void
3161 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3162 raised_p, top_p, bot_p, left_p, right_p, clip_rect)
3163 struct frame *f;
3164 int left_x, top_y, right_x, bottom_y, width;
3165 int top_p, bot_p, left_p, right_p, raised_p;
3166 Rect *clip_rect;
3167 {
3168 Display *dpy = FRAME_MAC_DISPLAY (f);
3169 int i;
3170 GC gc;
3171
3172 if (raised_p)
3173 gc = f->output_data.mac->white_relief.gc;
3174 else
3175 gc = f->output_data.mac->black_relief.gc;
3176 mac_set_clip_rectangles (dpy, gc, clip_rect, 1);
3177
3178 /* Top. */
3179 if (top_p)
3180 for (i = 0; i < width; ++i)
3181 mac_draw_line (f, gc,
3182 left_x + i * left_p, top_y + i,
3183 right_x + 1 - i * right_p, top_y + i);
3184
3185 /* Left. */
3186 if (left_p)
3187 for (i = 0; i < width; ++i)
3188 mac_draw_line (f, gc,
3189 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
3190
3191 mac_reset_clip_rectangles (dpy, gc);
3192 if (raised_p)
3193 gc = f->output_data.mac->black_relief.gc;
3194 else
3195 gc = f->output_data.mac->white_relief.gc;
3196 mac_set_clip_rectangles (dpy, gc, clip_rect, 1);
3197
3198 /* Bottom. */
3199 if (bot_p)
3200 for (i = 0; i < width; ++i)
3201 mac_draw_line (f, gc,
3202 left_x + i * left_p, bottom_y - i,
3203 right_x + 1 - i * right_p, bottom_y - i);
3204
3205 /* Right. */
3206 if (right_p)
3207 for (i = 0; i < width; ++i)
3208 mac_draw_line (f, gc,
3209 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3210
3211 mac_reset_clip_rectangles (dpy, gc);
3212 }
3213
3214
3215 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3216 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3217 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3218 left side of the rectangle. RIGHT_P non-zero means draw a line
3219 on the right side of the rectangle. CLIP_RECT is the clipping
3220 rectangle to use when drawing. */
3221
3222 static void
3223 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3224 left_p, right_p, clip_rect)
3225 struct glyph_string *s;
3226 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
3227 Rect *clip_rect;
3228 {
3229 XGCValues xgcv;
3230
3231 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3232 XSetForeground (s->display, s->gc, s->face->box_color);
3233 mac_set_clip_rectangles (s->display, s->gc, clip_rect, 1);
3234
3235 /* Top. */
3236 mac_fill_rectangle (s->f, s->gc, left_x, top_y,
3237 right_x - left_x + 1, width);
3238
3239 /* Left. */
3240 if (left_p)
3241 mac_fill_rectangle (s->f, s->gc, left_x, top_y,
3242 width, bottom_y - top_y + 1);
3243
3244 /* Bottom. */
3245 mac_fill_rectangle (s->f, s->gc, left_x, bottom_y - width + 1,
3246 right_x - left_x + 1, width);
3247
3248 /* Right. */
3249 if (right_p)
3250 mac_fill_rectangle (s->f, s->gc, right_x - width + 1,
3251 top_y, width, bottom_y - top_y + 1);
3252
3253 XSetForeground (s->display, s->gc, xgcv.foreground);
3254 mac_reset_clip_rectangles (s->display, s->gc);
3255 }
3256
3257
3258 /* Draw a box around glyph string S. */
3259
3260 static void
3261 x_draw_glyph_string_box (s)
3262 struct glyph_string *s;
3263 {
3264 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3265 int left_p, right_p;
3266 struct glyph *last_glyph;
3267 Rect clip_rect;
3268
3269 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
3270 ? WINDOW_RIGHT_EDGE_X (s->w)
3271 : window_box_right (s->w, s->area));
3272
3273 /* The glyph that may have a right box line. */
3274 last_glyph = (s->cmp || s->img
3275 ? s->first_glyph
3276 : s->first_glyph + s->nchars - 1);
3277
3278 width = abs (s->face->box_line_width);
3279 raised_p = s->face->box == FACE_RAISED_BOX;
3280 left_x = s->x;
3281 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
3282 ? last_x - 1
3283 : min (last_x, s->x + s->background_width) - 1);
3284 top_y = s->y;
3285 bottom_y = top_y + s->height - 1;
3286
3287 left_p = (s->first_glyph->left_box_line_p
3288 || (s->hl == DRAW_MOUSE_FACE
3289 && (s->prev == NULL
3290 || s->prev->hl != s->hl)));
3291 right_p = (last_glyph->right_box_line_p
3292 || (s->hl == DRAW_MOUSE_FACE
3293 && (s->next == NULL
3294 || s->next->hl != s->hl)));
3295
3296 get_glyph_string_clip_rect (s, &clip_rect);
3297
3298 if (s->face->box == FACE_SIMPLE_BOX)
3299 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3300 left_p, right_p, &clip_rect);
3301 else
3302 {
3303 x_setup_relief_colors (s);
3304 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3305 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
3306 }
3307 }
3308
3309
3310 /* Draw foreground of image glyph string S. */
3311
3312 static void
3313 x_draw_image_foreground (s)
3314 struct glyph_string *s;
3315 {
3316 int x = s->x;
3317 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
3318
3319 /* If first glyph of S has a left box line, start drawing it to the
3320 right of that line. */
3321 if (s->face->box != FACE_NO_BOX
3322 && s->first_glyph->left_box_line_p
3323 && s->slice.x == 0)
3324 x += abs (s->face->box_line_width);
3325
3326 /* If there is a margin around the image, adjust x- and y-position
3327 by that margin. */
3328 if (s->slice.x == 0)
3329 x += s->img->hmargin;
3330 if (s->slice.y == 0)
3331 y += s->img->vmargin;
3332
3333 if (s->img->pixmap)
3334 {
3335 x_set_glyph_string_clipping (s);
3336
3337 #if USE_CG_DRAWING
3338 mac_draw_cg_image (s->img->data.ptr_val,
3339 s->f, s->gc, s->slice.x, s->slice.y,
3340 s->slice.width, s->slice.height, x, y, 1);
3341 #endif
3342 if (s->img->mask)
3343 #if !USE_CG_DRAWING
3344 mac_copy_area_with_mask (s->img->pixmap, s->img->mask,
3345 s->f, s->gc, s->slice.x, s->slice.y,
3346 s->slice.width, s->slice.height, x, y);
3347 #else
3348 ;
3349 #endif
3350 else
3351 {
3352 #if !USE_CG_DRAWING
3353 mac_copy_area (s->img->pixmap,
3354 s->f, s->gc, s->slice.x, s->slice.y,
3355 s->slice.width, s->slice.height, x, y);
3356 #endif
3357
3358 /* When the image has a mask, we can expect that at
3359 least part of a mouse highlight or a block cursor will
3360 be visible. If the image doesn't have a mask, make
3361 a block cursor visible by drawing a rectangle around
3362 the image. I believe it's looking better if we do
3363 nothing here for mouse-face. */
3364 if (s->hl == DRAW_CURSOR)
3365 {
3366 int r = s->img->relief;
3367 if (r < 0) r = -r;
3368 mac_draw_rectangle (s->f, s->gc, x - r, y - r,
3369 s->slice.width + r*2 - 1,
3370 s->slice.height + r*2 - 1);
3371 }
3372 }
3373 }
3374 else
3375 /* Draw a rectangle if image could not be loaded. */
3376 mac_draw_rectangle (s->f, s->gc, x, y,
3377 s->slice.width - 1, s->slice.height - 1);
3378 }
3379
3380
3381 /* Draw a relief around the image glyph string S. */
3382
3383 static void
3384 x_draw_image_relief (s)
3385 struct glyph_string *s;
3386 {
3387 int x0, y0, x1, y1, thick, raised_p;
3388 Rect r;
3389 int x = s->x;
3390 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
3391
3392 /* If first glyph of S has a left box line, start drawing it to the
3393 right of that line. */
3394 if (s->face->box != FACE_NO_BOX
3395 && s->first_glyph->left_box_line_p
3396 && s->slice.x == 0)
3397 x += abs (s->face->box_line_width);
3398
3399 /* If there is a margin around the image, adjust x- and y-position
3400 by that margin. */
3401 if (s->slice.x == 0)
3402 x += s->img->hmargin;
3403 if (s->slice.y == 0)
3404 y += s->img->vmargin;
3405
3406 if (s->hl == DRAW_IMAGE_SUNKEN
3407 || s->hl == DRAW_IMAGE_RAISED)
3408 {
3409 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
3410 raised_p = s->hl == DRAW_IMAGE_RAISED;
3411 }
3412 else
3413 {
3414 thick = abs (s->img->relief);
3415 raised_p = s->img->relief > 0;
3416 }
3417
3418 x0 = x - thick;
3419 y0 = y - thick;
3420 x1 = x + s->slice.width + thick - 1;
3421 y1 = y + s->slice.height + thick - 1;
3422
3423 x_setup_relief_colors (s);
3424 get_glyph_string_clip_rect (s, &r);
3425 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
3426 s->slice.y == 0,
3427 s->slice.y + s->slice.height == s->img->height,
3428 s->slice.x == 0,
3429 s->slice.x + s->slice.width == s->img->width,
3430 &r);
3431 }
3432
3433
3434 /* Draw part of the background of glyph string S. X, Y, W, and H
3435 give the rectangle to draw. */
3436
3437 static void
3438 x_draw_glyph_string_bg_rect (s, x, y, w, h)
3439 struct glyph_string *s;
3440 int x, y, w, h;
3441 {
3442 #if 0 /* MAC_TODO: stipple */
3443 if (s->stippled_p)
3444 {
3445 /* Fill background with a stipple pattern. */
3446 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3447 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3448 XSetFillStyle (s->display, s->gc, FillSolid);
3449 }
3450 else
3451 #endif /* MAC_TODO */
3452 x_clear_glyph_string_rect (s, x, y, w, h);
3453 }
3454
3455
3456 /* Draw image glyph string S.
3457
3458 s->y
3459 s->x +-------------------------
3460 | s->face->box
3461 |
3462 | +-------------------------
3463 | | s->img->margin
3464 | |
3465 | | +-------------------
3466 | | | the image
3467
3468 */
3469
3470 static void
3471 x_draw_image_glyph_string (s)
3472 struct glyph_string *s;
3473 {
3474 int x, y;
3475 int box_line_hwidth = abs (s->face->box_line_width);
3476 int box_line_vwidth = max (s->face->box_line_width, 0);
3477 int height;
3478
3479 height = s->height - 2 * box_line_vwidth;
3480
3481
3482 /* Fill background with face under the image. Do it only if row is
3483 taller than image or if image has a clip mask to reduce
3484 flickering. */
3485 s->stippled_p = s->face->stipple != 0;
3486 if (height > s->slice.height
3487 || s->img->hmargin
3488 || s->img->vmargin
3489 || s->img->mask
3490 || s->img->pixmap == 0
3491 || s->width != s->background_width)
3492 {
3493 x = s->x;
3494 if (s->first_glyph->left_box_line_p
3495 && s->slice.x == 0)
3496 x += box_line_hwidth;
3497
3498 y = s->y;
3499 if (s->slice.y == 0)
3500 y += box_line_vwidth;
3501
3502 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
3503
3504 s->background_filled_p = 1;
3505 }
3506
3507 /* Draw the foreground. */
3508 x_draw_image_foreground (s);
3509
3510 /* If we must draw a relief around the image, do it. */
3511 if (s->img->relief
3512 || s->hl == DRAW_IMAGE_RAISED
3513 || s->hl == DRAW_IMAGE_SUNKEN)
3514 x_draw_image_relief (s);
3515 }
3516
3517
3518 /* Draw stretch glyph string S. */
3519
3520 static void
3521 x_draw_stretch_glyph_string (s)
3522 struct glyph_string *s;
3523 {
3524 xassert (s->first_glyph->type == STRETCH_GLYPH);
3525 s->stippled_p = s->face->stipple != 0;
3526
3527 if (s->hl == DRAW_CURSOR
3528 && !x_stretch_cursor_p)
3529 {
3530 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
3531 as wide as the stretch glyph. */
3532 int width = min (FRAME_COLUMN_WIDTH (s->f), s->background_width);
3533
3534 /* Draw cursor. */
3535 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
3536
3537 /* Clear rest using the GC of the original non-cursor face. */
3538 if (width < s->background_width)
3539 {
3540 int x = s->x + width, y = s->y;
3541 int w = s->background_width - width, h = s->height;
3542 Rect r;
3543 GC gc;
3544
3545 if (s->row->mouse_face_p
3546 && cursor_in_mouse_face_p (s->w))
3547 {
3548 x_set_mouse_face_gc (s);
3549 gc = s->gc;
3550 }
3551 else
3552 gc = s->face->gc;
3553
3554 get_glyph_string_clip_rect (s, &r);
3555 mac_set_clip_rectangles (s->display, gc, &r, 1);
3556
3557 #if 0 /* MAC_TODO: stipple */
3558 if (s->face->stipple)
3559 {
3560 /* Fill background with a stipple pattern. */
3561 XSetFillStyle (s->display, gc, FillOpaqueStippled);
3562 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3563 XSetFillStyle (s->display, gc, FillSolid);
3564 }
3565 else
3566 #endif /* MAC_TODO */
3567 mac_erase_rectangle (s->f, gc, x, y, w, h);
3568 }
3569 }
3570 else if (!s->background_filled_p)
3571 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
3572 s->height);
3573
3574 s->background_filled_p = 1;
3575 }
3576
3577
3578 /* Draw glyph string S. */
3579
3580 static void
3581 x_draw_glyph_string (s)
3582 struct glyph_string *s;
3583 {
3584 int relief_drawn_p = 0;
3585
3586 /* If S draws into the background of its successor that does not
3587 draw a cursor, draw the background of the successor first so that
3588 S can draw into it. This makes S->next use XDrawString instead
3589 of XDrawImageString. */
3590 if (s->next && s->right_overhang && !s->for_overlaps
3591 && s->next->hl != DRAW_CURSOR)
3592 {
3593 xassert (s->next->img == NULL);
3594 x_set_glyph_string_gc (s->next);
3595 x_set_glyph_string_clipping (s->next);
3596 x_draw_glyph_string_background (s->next, 1);
3597 }
3598
3599 /* Set up S->gc, set clipping and draw S. */
3600 x_set_glyph_string_gc (s);
3601
3602 /* Draw relief (if any) in advance for char/composition so that the
3603 glyph string can be drawn over it. */
3604 if (!s->for_overlaps
3605 && s->face->box != FACE_NO_BOX
3606 && (s->first_glyph->type == CHAR_GLYPH
3607 || s->first_glyph->type == COMPOSITE_GLYPH))
3608
3609 {
3610 x_set_glyph_string_clipping (s);
3611 x_draw_glyph_string_background (s, 1);
3612 x_draw_glyph_string_box (s);
3613 x_set_glyph_string_clipping (s);
3614 relief_drawn_p = 1;
3615 }
3616 else
3617 x_set_glyph_string_clipping (s);
3618
3619 switch (s->first_glyph->type)
3620 {
3621 case IMAGE_GLYPH:
3622 x_draw_image_glyph_string (s);
3623 break;
3624
3625 case STRETCH_GLYPH:
3626 x_draw_stretch_glyph_string (s);
3627 break;
3628
3629 case CHAR_GLYPH:
3630 if (s->for_overlaps)
3631 s->background_filled_p = 1;
3632 else
3633 x_draw_glyph_string_background (s, 0);
3634 x_draw_glyph_string_foreground (s);
3635 break;
3636
3637 case COMPOSITE_GLYPH:
3638 if (s->for_overlaps || s->gidx > 0)
3639 s->background_filled_p = 1;
3640 else
3641 x_draw_glyph_string_background (s, 1);
3642 x_draw_composite_glyph_string_foreground (s);
3643 break;
3644
3645 default:
3646 abort ();
3647 }
3648
3649 if (!s->for_overlaps)
3650 {
3651 /* Draw underline. */
3652 if (s->face->underline_p)
3653 {
3654 unsigned long h = 1;
3655 unsigned long dy = s->height - h;
3656
3657 if (s->face->underline_defaulted_p)
3658 mac_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3659 s->width, h);
3660 else
3661 {
3662 XGCValues xgcv;
3663 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3664 XSetForeground (s->display, s->gc, s->face->underline_color);
3665 mac_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3666 s->width, h);
3667 XSetForeground (s->display, s->gc, xgcv.foreground);
3668 }
3669 }
3670
3671 /* Draw overline. */
3672 if (s->face->overline_p)
3673 {
3674 unsigned long dy = 0, h = 1;
3675
3676 if (s->face->overline_color_defaulted_p)
3677 mac_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3678 s->width, h);
3679 else
3680 {
3681 XGCValues xgcv;
3682 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3683 XSetForeground (s->display, s->gc, s->face->overline_color);
3684 mac_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3685 s->width, h);
3686 XSetForeground (s->display, s->gc, xgcv.foreground);
3687 }
3688 }
3689
3690 /* Draw strike-through. */
3691 if (s->face->strike_through_p)
3692 {
3693 unsigned long h = 1;
3694 unsigned long dy = (s->height - h) / 2;
3695
3696 if (s->face->strike_through_color_defaulted_p)
3697 mac_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3698 s->width, h);
3699 else
3700 {
3701 XGCValues xgcv;
3702 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3703 XSetForeground (s->display, s->gc, s->face->strike_through_color);
3704 mac_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3705 s->width, h);
3706 XSetForeground (s->display, s->gc, xgcv.foreground);
3707 }
3708 }
3709
3710 /* Draw relief if not yet drawn. */
3711 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
3712 x_draw_glyph_string_box (s);
3713 }
3714
3715 /* Reset clipping. */
3716 mac_reset_clip_rectangles (s->display, s->gc);
3717 }
3718
3719 /* Shift display to make room for inserted glyphs. */
3720
3721 void
3722 mac_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
3723 struct frame *f;
3724 int x, y, width, height, shift_by;
3725 {
3726 mac_scroll_area (f, f->output_data.mac->normal_gc,
3727 x, y, width, height,
3728 x + shift_by, y);
3729 }
3730
3731 /* Delete N glyphs at the nominal cursor position. Not implemented
3732 for X frames. */
3733
3734 static void
3735 x_delete_glyphs (n)
3736 register int n;
3737 {
3738 abort ();
3739 }
3740
3741
3742 /* Clear entire frame. If updating_frame is non-null, clear that
3743 frame. Otherwise clear the selected frame. */
3744
3745 static void
3746 x_clear_frame ()
3747 {
3748 struct frame *f;
3749
3750 if (updating_frame)
3751 f = updating_frame;
3752 else
3753 f = SELECTED_FRAME ();
3754
3755 /* Clearing the frame will erase any cursor, so mark them all as no
3756 longer visible. */
3757 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
3758 output_cursor.hpos = output_cursor.vpos = 0;
3759 output_cursor.x = -1;
3760
3761 /* We don't set the output cursor here because there will always
3762 follow an explicit cursor_to. */
3763 BLOCK_INPUT;
3764 mac_clear_window (f);
3765
3766 /* We have to clear the scroll bars, too. If we have changed
3767 colors or something like that, then they should be notified. */
3768 x_scroll_bar_clear (f);
3769
3770 XFlush (FRAME_MAC_DISPLAY (f));
3771 UNBLOCK_INPUT;
3772 }
3773
3774
3775 \f
3776 /* Invert the middle quarter of the frame for .15 sec. */
3777
3778 /* We use the select system call to do the waiting, so we have to make
3779 sure it's available. If it isn't, we just won't do visual bells. */
3780
3781 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
3782
3783
3784 /* Subtract the `struct timeval' values X and Y, storing the result in
3785 *RESULT. Return 1 if the difference is negative, otherwise 0. */
3786
3787 static int
3788 timeval_subtract (result, x, y)
3789 struct timeval *result, x, y;
3790 {
3791 /* Perform the carry for the later subtraction by updating y. This
3792 is safer because on some systems the tv_sec member is unsigned. */
3793 if (x.tv_usec < y.tv_usec)
3794 {
3795 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
3796 y.tv_usec -= 1000000 * nsec;
3797 y.tv_sec += nsec;
3798 }
3799
3800 if (x.tv_usec - y.tv_usec > 1000000)
3801 {
3802 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
3803 y.tv_usec += 1000000 * nsec;
3804 y.tv_sec -= nsec;
3805 }
3806
3807 /* Compute the time remaining to wait. tv_usec is certainly
3808 positive. */
3809 result->tv_sec = x.tv_sec - y.tv_sec;
3810 result->tv_usec = x.tv_usec - y.tv_usec;
3811
3812 /* Return indication of whether the result should be considered
3813 negative. */
3814 return x.tv_sec < y.tv_sec;
3815 }
3816
3817 void
3818 XTflash (f)
3819 struct frame *f;
3820 {
3821 /* Get the height not including a menu bar widget. */
3822 int height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, FRAME_LINES (f));
3823 /* Height of each line to flash. */
3824 int flash_height = FRAME_LINE_HEIGHT (f);
3825 /* These will be the left and right margins of the rectangles. */
3826 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
3827 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
3828
3829 int width;
3830
3831 /* Don't flash the area between a scroll bar and the frame
3832 edge it is next to. */
3833 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
3834 {
3835 case vertical_scroll_bar_left:
3836 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3837 break;
3838
3839 case vertical_scroll_bar_right:
3840 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3841 break;
3842
3843 default:
3844 break;
3845 }
3846
3847 width = flash_right - flash_left;
3848
3849 BLOCK_INPUT;
3850
3851 /* If window is tall, flash top and bottom line. */
3852 if (height > 3 * FRAME_LINE_HEIGHT (f))
3853 {
3854 mac_invert_rectangle (f, flash_left,
3855 (FRAME_INTERNAL_BORDER_WIDTH (f)
3856 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
3857 width, flash_height);
3858 mac_invert_rectangle (f, flash_left,
3859 (height - flash_height
3860 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3861 width, flash_height);
3862 }
3863 else
3864 /* If it is short, flash it all. */
3865 mac_invert_rectangle (f, flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3866 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3867
3868 x_flush (f);
3869
3870 {
3871 struct timeval wakeup;
3872
3873 EMACS_GET_TIME (wakeup);
3874
3875 /* Compute time to wait until, propagating carry from usecs. */
3876 wakeup.tv_usec += 150000;
3877 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
3878 wakeup.tv_usec %= 1000000;
3879
3880 /* Keep waiting until past the time wakeup or any input gets
3881 available. */
3882 while (! detect_input_pending ())
3883 {
3884 struct timeval current;
3885 struct timeval timeout;
3886
3887 EMACS_GET_TIME (current);
3888
3889 /* Break if result would be negative. */
3890 if (timeval_subtract (&current, wakeup, current))
3891 break;
3892
3893 /* How long `select' should wait. */
3894 timeout.tv_sec = 0;
3895 timeout.tv_usec = 10000;
3896
3897 /* Try to wait that long--but we might wake up sooner. */
3898 select (0, NULL, NULL, NULL, &timeout);
3899 }
3900 }
3901
3902 /* If window is tall, flash top and bottom line. */
3903 if (height > 3 * FRAME_LINE_HEIGHT (f))
3904 {
3905 mac_invert_rectangle (f, flash_left,
3906 (FRAME_INTERNAL_BORDER_WIDTH (f)
3907 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
3908 width, flash_height);
3909 mac_invert_rectangle (f, flash_left,
3910 (height - flash_height
3911 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3912 width, flash_height);
3913 }
3914 else
3915 /* If it is short, flash it all. */
3916 mac_invert_rectangle (f, flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3917 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3918
3919 x_flush (f);
3920
3921 UNBLOCK_INPUT;
3922 }
3923
3924 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
3925
3926
3927 /* Make audible bell. */
3928
3929 void
3930 XTring_bell ()
3931 {
3932 struct frame *f = SELECTED_FRAME ();
3933
3934 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
3935 if (visible_bell)
3936 XTflash (f);
3937 else
3938 #endif
3939 {
3940 BLOCK_INPUT;
3941 SysBeep (1);
3942 XFlush (FRAME_MAC_DISPLAY (f));
3943 UNBLOCK_INPUT;
3944 }
3945 }
3946
3947 \f
3948 /* Specify how many text lines, from the top of the window,
3949 should be affected by insert-lines and delete-lines operations.
3950 This, and those operations, are used only within an update
3951 that is bounded by calls to x_update_begin and x_update_end. */
3952
3953 static void
3954 XTset_terminal_window (n)
3955 register int n;
3956 {
3957 /* This function intentionally left blank. */
3958 }
3959
3960
3961 \f
3962 /***********************************************************************
3963 Line Dance
3964 ***********************************************************************/
3965
3966 /* Perform an insert-lines or delete-lines operation, inserting N
3967 lines or deleting -N lines at vertical position VPOS. */
3968
3969 static void
3970 x_ins_del_lines (vpos, n)
3971 int vpos, n;
3972 {
3973 abort ();
3974 }
3975
3976
3977 /* Scroll part of the display as described by RUN. */
3978
3979 static void
3980 x_scroll_run (w, run)
3981 struct window *w;
3982 struct run *run;
3983 {
3984 struct frame *f = XFRAME (w->frame);
3985 int x, y, width, height, from_y, to_y, bottom_y;
3986
3987 /* Get frame-relative bounding box of the text display area of W,
3988 without mode lines. Include in this box the left and right
3989 fringe of W. */
3990 window_box (w, -1, &x, &y, &width, &height);
3991
3992 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3993 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3994 bottom_y = y + height;
3995
3996 if (to_y < from_y)
3997 {
3998 /* Scrolling up. Make sure we don't copy part of the mode
3999 line at the bottom. */
4000 if (from_y + run->height > bottom_y)
4001 height = bottom_y - from_y;
4002 else
4003 height = run->height;
4004 }
4005 else
4006 {
4007 /* Scolling down. Make sure we don't copy over the mode line.
4008 at the bottom. */
4009 if (to_y + run->height > bottom_y)
4010 height = bottom_y - to_y;
4011 else
4012 height = run->height;
4013 }
4014
4015 BLOCK_INPUT;
4016
4017 /* Cursor off. Will be switched on again in x_update_window_end. */
4018 updated_window = w;
4019 x_clear_cursor (w);
4020
4021 mac_scroll_area (f, f->output_data.mac->normal_gc,
4022 x, from_y,
4023 width, height,
4024 x, to_y);
4025
4026 UNBLOCK_INPUT;
4027 }
4028
4029
4030 \f
4031 /***********************************************************************
4032 Exposure Events
4033 ***********************************************************************/
4034
4035 \f
4036 static void
4037 frame_highlight (f)
4038 struct frame *f;
4039 {
4040 OSErr err;
4041 ControlRef root_control;
4042
4043 BLOCK_INPUT;
4044 err = GetRootControl (FRAME_MAC_WINDOW (f), &root_control);
4045 if (err == noErr)
4046 ActivateControl (root_control);
4047 UNBLOCK_INPUT;
4048 x_update_cursor (f, 1);
4049 }
4050
4051 static void
4052 frame_unhighlight (f)
4053 struct frame *f;
4054 {
4055 OSErr err;
4056 ControlRef root_control;
4057
4058 BLOCK_INPUT;
4059 err = GetRootControl (FRAME_MAC_WINDOW (f), &root_control);
4060 if (err == noErr)
4061 DeactivateControl (root_control);
4062 UNBLOCK_INPUT;
4063 x_update_cursor (f, 1);
4064 }
4065
4066 /* The focus has changed. Update the frames as necessary to reflect
4067 the new situation. Note that we can't change the selected frame
4068 here, because the Lisp code we are interrupting might become confused.
4069 Each event gets marked with the frame in which it occurred, so the
4070 Lisp code can tell when the switch took place by examining the events. */
4071
4072 static void
4073 x_new_focus_frame (dpyinfo, frame)
4074 struct x_display_info *dpyinfo;
4075 struct frame *frame;
4076 {
4077 struct frame *old_focus = dpyinfo->x_focus_frame;
4078
4079 if (frame != dpyinfo->x_focus_frame)
4080 {
4081 /* Set this before calling other routines, so that they see
4082 the correct value of x_focus_frame. */
4083 dpyinfo->x_focus_frame = frame;
4084
4085 if (old_focus && old_focus->auto_lower)
4086 x_lower_frame (old_focus);
4087
4088 #if 0
4089 selected_frame = frame;
4090 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
4091 selected_frame);
4092 Fselect_window (selected_frame->selected_window, Qnil);
4093 choose_minibuf_frame ();
4094 #endif /* ! 0 */
4095
4096 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
4097 pending_autoraise_frame = dpyinfo->x_focus_frame;
4098 else
4099 pending_autoraise_frame = 0;
4100 }
4101
4102 x_frame_rehighlight (dpyinfo);
4103 }
4104
4105 /* Handle FocusIn and FocusOut state changes for FRAME.
4106 If FRAME has focus and there exists more than one frame, puts
4107 a FOCUS_IN_EVENT into *BUFP. */
4108
4109 static void
4110 mac_focus_changed (type, dpyinfo, frame, bufp)
4111 int type;
4112 struct mac_display_info *dpyinfo;
4113 struct frame *frame;
4114 struct input_event *bufp;
4115 {
4116 if (type == activeFlag)
4117 {
4118 if (dpyinfo->x_focus_event_frame != frame)
4119 {
4120 x_new_focus_frame (dpyinfo, frame);
4121 dpyinfo->x_focus_event_frame = frame;
4122
4123 /* Don't stop displaying the initial startup message
4124 for a switch-frame event we don't need. */
4125 if (GC_NILP (Vterminal_frame)
4126 && GC_CONSP (Vframe_list)
4127 && !GC_NILP (XCDR (Vframe_list)))
4128 {
4129 bufp->kind = FOCUS_IN_EVENT;
4130 XSETFRAME (bufp->frame_or_window, frame);
4131 }
4132 }
4133 }
4134 else
4135 {
4136 if (dpyinfo->x_focus_event_frame == frame)
4137 {
4138 dpyinfo->x_focus_event_frame = 0;
4139 x_new_focus_frame (dpyinfo, 0);
4140 }
4141 }
4142 }
4143
4144 /* The focus may have changed. Figure out if it is a real focus change,
4145 by checking both FocusIn/Out and Enter/LeaveNotify events.
4146
4147 Returns FOCUS_IN_EVENT event in *BUFP. */
4148
4149 static void
4150 x_detect_focus_change (dpyinfo, event, bufp)
4151 struct mac_display_info *dpyinfo;
4152 EventRecord *event;
4153 struct input_event *bufp;
4154 {
4155 struct frame *frame;
4156
4157 frame = mac_window_to_frame ((WindowPtr) event->message);
4158 if (! frame)
4159 return;
4160
4161 /* On Mac, this is only called from focus events, so no switch needed. */
4162 mac_focus_changed ((event->modifiers & activeFlag),
4163 dpyinfo, frame, bufp);
4164 }
4165
4166
4167 /* Handle an event saying the mouse has moved out of an Emacs frame. */
4168
4169 void
4170 x_mouse_leave (dpyinfo)
4171 struct x_display_info *dpyinfo;
4172 {
4173 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
4174 }
4175
4176 /* The focus has changed, or we have redirected a frame's focus to
4177 another frame (this happens when a frame uses a surrogate
4178 mini-buffer frame). Shift the highlight as appropriate.
4179
4180 The FRAME argument doesn't necessarily have anything to do with which
4181 frame is being highlighted or un-highlighted; we only use it to find
4182 the appropriate X display info. */
4183
4184 static void
4185 XTframe_rehighlight (frame)
4186 struct frame *frame;
4187 {
4188 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
4189 }
4190
4191 static void
4192 x_frame_rehighlight (dpyinfo)
4193 struct x_display_info *dpyinfo;
4194 {
4195 struct frame *old_highlight = dpyinfo->x_highlight_frame;
4196
4197 if (dpyinfo->x_focus_frame)
4198 {
4199 dpyinfo->x_highlight_frame
4200 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
4201 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
4202 : dpyinfo->x_focus_frame);
4203 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
4204 {
4205 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
4206 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
4207 }
4208 }
4209 else
4210 dpyinfo->x_highlight_frame = 0;
4211
4212 if (dpyinfo->x_highlight_frame != old_highlight)
4213 {
4214 if (old_highlight)
4215 frame_unhighlight (old_highlight);
4216 if (dpyinfo->x_highlight_frame)
4217 frame_highlight (dpyinfo->x_highlight_frame);
4218 }
4219 }
4220
4221
4222 \f
4223 /* Convert a keysym to its name. */
4224
4225 char *
4226 x_get_keysym_name (keysym)
4227 int keysym;
4228 {
4229 char *value;
4230
4231 BLOCK_INPUT;
4232 #if 0
4233 value = XKeysymToString (keysym);
4234 #else
4235 value = 0;
4236 #endif
4237 UNBLOCK_INPUT;
4238
4239 return value;
4240 }
4241
4242
4243 \f
4244 /* Function to report a mouse movement to the mainstream Emacs code.
4245 The input handler calls this.
4246
4247 We have received a mouse movement event, which is given in *event.
4248 If the mouse is over a different glyph than it was last time, tell
4249 the mainstream emacs code by setting mouse_moved. If not, ask for
4250 another motion event, so we can check again the next time it moves. */
4251
4252 static Point last_mouse_motion_position;
4253 static Lisp_Object last_mouse_motion_frame;
4254
4255 static int
4256 note_mouse_movement (frame, pos)
4257 FRAME_PTR frame;
4258 Point *pos;
4259 {
4260 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (frame);
4261 #if TARGET_API_MAC_CARBON
4262 Rect r;
4263 #endif
4264
4265 last_mouse_movement_time = TickCount () * (1000 / 60); /* to milliseconds */
4266 last_mouse_motion_position = *pos;
4267 XSETFRAME (last_mouse_motion_frame, frame);
4268
4269 #if TARGET_API_MAC_CARBON
4270 if (!PtInRect (*pos, GetWindowPortBounds (FRAME_MAC_WINDOW (frame), &r)))
4271 #else
4272 if (!PtInRect (*pos, &FRAME_MAC_WINDOW (frame)->portRect))
4273 #endif
4274 {
4275 if (frame == dpyinfo->mouse_face_mouse_frame)
4276 /* This case corresponds to LeaveNotify in X11. */
4277 {
4278 /* If we move outside the frame, then we're certainly no
4279 longer on any text in the frame. */
4280 clear_mouse_face (dpyinfo);
4281 dpyinfo->mouse_face_mouse_frame = 0;
4282 if (!dpyinfo->grabbed)
4283 rif->define_frame_cursor (frame,
4284 frame->output_data.mac->nontext_cursor);
4285 }
4286 return 1;
4287 }
4288 /* Has the mouse moved off the glyph it was on at the last sighting? */
4289 if (frame != last_mouse_glyph_frame
4290 || !PtInRect (*pos, &last_mouse_glyph))
4291 {
4292 frame->mouse_moved = 1;
4293 last_mouse_scroll_bar = Qnil;
4294 note_mouse_highlight (frame, pos->h, pos->v);
4295 /* Remember which glyph we're now on. */
4296 remember_mouse_glyph (frame, pos->h, pos->v, &last_mouse_glyph);
4297 last_mouse_glyph_frame = frame;
4298 return 1;
4299 }
4300
4301 return 0;
4302 }
4303
4304 \f
4305 /************************************************************************
4306 Mouse Face
4307 ************************************************************************/
4308
4309 /* MAC TODO: This should be called from somewhere (or removed) ++KFS */
4310
4311 static void
4312 redo_mouse_highlight ()
4313 {
4314 if (!NILP (last_mouse_motion_frame)
4315 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
4316 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
4317 last_mouse_motion_position.h,
4318 last_mouse_motion_position.v);
4319 }
4320
4321
4322 static struct frame *
4323 mac_focus_frame (dpyinfo)
4324 struct mac_display_info *dpyinfo;
4325 {
4326 if (dpyinfo->x_focus_frame)
4327 return dpyinfo->x_focus_frame;
4328 else
4329 /* Mac version may get events, such as a menu bar click, even when
4330 all the frames are invisible. In this case, we regard the
4331 event came to the selected frame. */
4332 return SELECTED_FRAME ();
4333 }
4334
4335
4336 /* Return the current position of the mouse.
4337 *FP should be a frame which indicates which display to ask about.
4338
4339 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
4340 and *PART to the frame, window, and scroll bar part that the mouse
4341 is over. Set *X and *Y to the portion and whole of the mouse's
4342 position on the scroll bar.
4343
4344 If the mouse movement started elsewhere, set *FP to the frame the
4345 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
4346 the mouse is over.
4347
4348 Set *TIME to the server time-stamp for the time at which the mouse
4349 was at this position.
4350
4351 Don't store anything if we don't have a valid set of values to report.
4352
4353 This clears the mouse_moved flag, so we can wait for the next mouse
4354 movement. */
4355
4356 static void
4357 XTmouse_position (fp, insist, bar_window, part, x, y, time)
4358 FRAME_PTR *fp;
4359 int insist;
4360 Lisp_Object *bar_window;
4361 enum scroll_bar_part *part;
4362 Lisp_Object *x, *y;
4363 unsigned long *time;
4364 {
4365 FRAME_PTR f1;
4366
4367 BLOCK_INPUT;
4368
4369 if (! NILP (last_mouse_scroll_bar) && insist == 0)
4370 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
4371 else
4372 {
4373 Lisp_Object frame, tail;
4374
4375 /* Clear the mouse-moved flag for every frame on this display. */
4376 FOR_EACH_FRAME (tail, frame)
4377 XFRAME (frame)->mouse_moved = 0;
4378
4379 last_mouse_scroll_bar = Qnil;
4380
4381 if (FRAME_MAC_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
4382 && FRAME_LIVE_P (last_mouse_frame))
4383 f1 = last_mouse_frame;
4384 else
4385 f1 = mac_focus_frame (FRAME_MAC_DISPLAY_INFO (*fp));
4386
4387 if (f1)
4388 {
4389 /* Ok, we found a frame. Store all the values.
4390 last_mouse_glyph is a rectangle used to reduce the
4391 generation of mouse events. To not miss any motion
4392 events, we must divide the frame into rectangles of the
4393 size of the smallest character that could be displayed
4394 on it, i.e. into the same rectangles that matrices on
4395 the frame are divided into. */
4396 Point mouse_pos;
4397
4398 SetPortWindowPort (FRAME_MAC_WINDOW (f1));
4399 GetMouse (&mouse_pos);
4400 remember_mouse_glyph (f1, mouse_pos.h, mouse_pos.v,
4401 &last_mouse_glyph);
4402 last_mouse_glyph_frame = f1;
4403
4404 *bar_window = Qnil;
4405 *part = 0;
4406 *fp = f1;
4407 XSETINT (*x, mouse_pos.h);
4408 XSETINT (*y, mouse_pos.v);
4409 *time = last_mouse_movement_time;
4410 }
4411 }
4412
4413 UNBLOCK_INPUT;
4414 }
4415
4416 \f
4417 /************************************************************************
4418 Toolkit scroll bars
4419 ************************************************************************/
4420
4421 #ifdef USE_TOOLKIT_SCROLL_BARS
4422
4423 static pascal void scroll_bar_timer_callback P_ ((EventLoopTimerRef, void *));
4424 static OSStatus install_scroll_bar_timer P_ ((void));
4425 static OSStatus set_scroll_bar_timer P_ ((EventTimerInterval));
4426 static int control_part_code_to_scroll_bar_part P_ ((ControlPartCode));
4427 static void construct_scroll_bar_click P_ ((struct scroll_bar *, int,
4428 struct input_event *));
4429 static OSStatus get_control_part_bounds P_ ((ControlHandle, ControlPartCode,
4430 Rect *));
4431 static void x_scroll_bar_handle_press P_ ((struct scroll_bar *,
4432 ControlPartCode,
4433 struct input_event *));
4434 static void x_scroll_bar_handle_release P_ ((struct scroll_bar *,
4435 struct input_event *));
4436 static void x_scroll_bar_handle_drag P_ ((WindowPtr, struct scroll_bar *,
4437 Point, struct input_event *));
4438 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
4439 int, int, int));
4440
4441 /* Last scroll bar part sent in x_scroll_bar_handle_*. */
4442
4443 static int last_scroll_bar_part;
4444
4445 static EventLoopTimerRef scroll_bar_timer;
4446
4447 static int scroll_bar_timer_event_posted_p;
4448
4449 #define SCROLL_BAR_FIRST_DELAY 0.5
4450 #define SCROLL_BAR_CONTINUOUS_DELAY (1.0 / 15)
4451
4452 static pascal void
4453 scroll_bar_timer_callback (timer, data)
4454 EventLoopTimerRef timer;
4455 void *data;
4456 {
4457 OSStatus err;
4458
4459 err = mac_post_mouse_moved_event ();
4460 if (err == noErr)
4461 scroll_bar_timer_event_posted_p = 1;
4462 }
4463
4464 static OSStatus
4465 install_scroll_bar_timer ()
4466 {
4467 static EventLoopTimerUPP scroll_bar_timer_callbackUPP = NULL;
4468
4469 if (scroll_bar_timer_callbackUPP == NULL)
4470 scroll_bar_timer_callbackUPP =
4471 NewEventLoopTimerUPP (scroll_bar_timer_callback);
4472
4473 if (scroll_bar_timer == NULL)
4474 /* Mac OS X and CarbonLib 1.5 and later allow us to specify
4475 kEventDurationForever as delays. */
4476 return
4477 InstallEventLoopTimer (GetCurrentEventLoop (),
4478 kEventDurationForever, kEventDurationForever,
4479 scroll_bar_timer_callbackUPP, NULL,
4480 &scroll_bar_timer);
4481 }
4482
4483 static OSStatus
4484 set_scroll_bar_timer (delay)
4485 EventTimerInterval delay;
4486 {
4487 if (scroll_bar_timer == NULL)
4488 install_scroll_bar_timer ();
4489
4490 scroll_bar_timer_event_posted_p = 0;
4491
4492 return SetEventLoopTimerNextFireTime (scroll_bar_timer, delay);
4493 }
4494
4495 static int
4496 control_part_code_to_scroll_bar_part (part_code)
4497 ControlPartCode part_code;
4498 {
4499 switch (part_code)
4500 {
4501 case kControlUpButtonPart: return scroll_bar_up_arrow;
4502 case kControlDownButtonPart: return scroll_bar_down_arrow;
4503 case kControlPageUpPart: return scroll_bar_above_handle;
4504 case kControlPageDownPart: return scroll_bar_below_handle;
4505 case kControlIndicatorPart: return scroll_bar_handle;
4506 }
4507
4508 return -1;
4509 }
4510
4511 static void
4512 construct_scroll_bar_click (bar, part, bufp)
4513 struct scroll_bar *bar;
4514 int part;
4515 struct input_event *bufp;
4516 {
4517 bufp->kind = SCROLL_BAR_CLICK_EVENT;
4518 bufp->frame_or_window = bar->window;
4519 bufp->arg = Qnil;
4520 bufp->part = part;
4521 bufp->code = 0;
4522 XSETINT (bufp->x, 0);
4523 XSETINT (bufp->y, 0);
4524 bufp->modifiers = 0;
4525 }
4526
4527 static OSStatus
4528 get_control_part_bounds (ch, part_code, rect)
4529 ControlHandle ch;
4530 ControlPartCode part_code;
4531 Rect *rect;
4532 {
4533 RgnHandle region = NewRgn ();
4534 OSStatus err;
4535
4536 err = GetControlRegion (ch, part_code, region);
4537 if (err == noErr)
4538 GetRegionBounds (region, rect);
4539 DisposeRgn (region);
4540
4541 return err;
4542 }
4543
4544 static void
4545 x_scroll_bar_handle_press (bar, part_code, bufp)
4546 struct scroll_bar *bar;
4547 ControlPartCode part_code;
4548 struct input_event *bufp;
4549 {
4550 int part = control_part_code_to_scroll_bar_part (part_code);
4551
4552 if (part < 0)
4553 return;
4554
4555 if (part != scroll_bar_handle)
4556 {
4557 construct_scroll_bar_click (bar, part, bufp);
4558 HiliteControl (SCROLL_BAR_CONTROL_HANDLE (bar), part_code);
4559 set_scroll_bar_timer (SCROLL_BAR_FIRST_DELAY);
4560 }
4561
4562 last_scroll_bar_part = part;
4563 bar->dragging = Qnil;
4564 tracked_scroll_bar = bar;
4565 }
4566
4567 static void
4568 x_scroll_bar_handle_release (bar, bufp)
4569 struct scroll_bar *bar;
4570 struct input_event *bufp;
4571 {
4572 if (last_scroll_bar_part != scroll_bar_handle
4573 || !GC_NILP (bar->dragging))
4574 construct_scroll_bar_click (bar, scroll_bar_end_scroll, bufp);
4575
4576 HiliteControl (SCROLL_BAR_CONTROL_HANDLE (bar), 0);
4577 set_scroll_bar_timer (kEventDurationForever);
4578
4579 last_scroll_bar_part = -1;
4580 bar->dragging = Qnil;
4581 tracked_scroll_bar = NULL;
4582 }
4583
4584 static void
4585 x_scroll_bar_handle_drag (win, bar, mouse_pos, bufp)
4586 WindowPtr win;
4587 struct scroll_bar *bar;
4588 Point mouse_pos;
4589 struct input_event *bufp;
4590 {
4591 ControlHandle ch = SCROLL_BAR_CONTROL_HANDLE (bar);
4592
4593 if (last_scroll_bar_part == scroll_bar_handle)
4594 {
4595 int top, top_range;
4596 Rect r;
4597
4598 get_control_part_bounds (SCROLL_BAR_CONTROL_HANDLE (bar),
4599 kControlIndicatorPart, &r);
4600
4601 if (GC_NILP (bar->dragging))
4602 XSETINT (bar->dragging, mouse_pos.v - r.top);
4603
4604 top = mouse_pos.v - XINT (bar->dragging) - XINT (bar->track_top);
4605 top_range = (XINT (bar->track_height) - (r.bottom - r.top)) *
4606 (1.0 + (float) GetControlViewSize (ch) / GetControl32BitMaximum (ch))
4607 + .5;
4608
4609 if (top < 0)
4610 top = 0;
4611 if (top > top_range)
4612 top = top_range;
4613
4614 construct_scroll_bar_click (bar, scroll_bar_handle, bufp);
4615 XSETINT (bufp->x, top);
4616 XSETINT (bufp->y, top_range);
4617 }
4618 else
4619 {
4620 ControlPartCode part_code;
4621 int unhilite_p = 0, part;
4622
4623 if (ch != FindControlUnderMouse (mouse_pos, win, &part_code))
4624 unhilite_p = 1;
4625 else
4626 {
4627 part = control_part_code_to_scroll_bar_part (part_code);
4628
4629 switch (last_scroll_bar_part)
4630 {
4631 case scroll_bar_above_handle:
4632 case scroll_bar_below_handle:
4633 if (part != scroll_bar_above_handle
4634 && part != scroll_bar_below_handle)
4635 unhilite_p = 1;
4636 break;
4637
4638 case scroll_bar_up_arrow:
4639 case scroll_bar_down_arrow:
4640 if (part != scroll_bar_up_arrow
4641 && part != scroll_bar_down_arrow)
4642 unhilite_p = 1;
4643 break;
4644 }
4645 }
4646
4647 if (unhilite_p)
4648 HiliteControl (SCROLL_BAR_CONTROL_HANDLE (bar), 0);
4649 else if (part != last_scroll_bar_part
4650 || scroll_bar_timer_event_posted_p)
4651 {
4652 construct_scroll_bar_click (bar, part, bufp);
4653 last_scroll_bar_part = part;
4654 HiliteControl (SCROLL_BAR_CONTROL_HANDLE (bar), part_code);
4655 set_scroll_bar_timer (SCROLL_BAR_CONTINUOUS_DELAY);
4656 }
4657 }
4658 }
4659
4660 /* Set the thumb size and position of scroll bar BAR. We are currently
4661 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4662
4663 static void
4664 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4665 struct scroll_bar *bar;
4666 int portion, position, whole;
4667 {
4668 ControlHandle ch = SCROLL_BAR_CONTROL_HANDLE (bar);
4669 int value, viewsize, maximum;
4670
4671 if (XINT (bar->track_height) == 0)
4672 return;
4673
4674 if (whole == 0)
4675 value = 0, viewsize = 1, maximum = 0;
4676 else
4677 {
4678 value = position;
4679 viewsize = portion;
4680 maximum = max (0, whole - portion);
4681 }
4682
4683 BLOCK_INPUT;
4684
4685 if (GetControlViewSize (ch) != viewsize
4686 || GetControl32BitValue (ch) != value
4687 || GetControl32BitMaximum (ch) != maximum)
4688 {
4689 /* Temporarily hide the scroll bar to avoid multiple redraws. */
4690 SetControlVisibility (ch, false, false);
4691
4692 SetControl32BitMaximum (ch, maximum);
4693 SetControl32BitValue (ch, value);
4694 SetControlViewSize (ch, viewsize);
4695
4696 SetControlVisibility (ch, true, true);
4697 }
4698
4699 UNBLOCK_INPUT;
4700 }
4701
4702 #endif /* USE_TOOLKIT_SCROLL_BARS */
4703
4704
4705 \f
4706 /************************************************************************
4707 Scroll bars, general
4708 ************************************************************************/
4709
4710 /* Create a scroll bar and return the scroll bar vector for it. W is
4711 the Emacs window on which to create the scroll bar. TOP, LEFT,
4712 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4713 scroll bar. */
4714
4715 static struct scroll_bar *
4716 x_scroll_bar_create (w, top, left, width, height, disp_top, disp_height)
4717 struct window *w;
4718 int top, left, width, height, disp_top, disp_height;
4719 {
4720 struct frame *f = XFRAME (w->frame);
4721 struct scroll_bar *bar
4722 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
4723 Rect r;
4724 ControlHandle ch;
4725
4726 BLOCK_INPUT;
4727
4728 r.left = left;
4729 r.top = disp_top;
4730 r.right = left + width;
4731 r.bottom = disp_top + disp_height;
4732
4733 #if USE_CG_DRAWING
4734 mac_prepare_for_quickdraw (f);
4735 #endif
4736 #if TARGET_API_MAC_CARBON
4737 ch = NewControl (FRAME_MAC_WINDOW (f), &r, "\p",
4738 #if USE_TOOLKIT_SCROLL_BARS
4739 false,
4740 #else
4741 width < disp_height,
4742 #endif
4743 0, 0, 0, kControlScrollBarProc, (long) bar);
4744 #else
4745 ch = NewControl (FRAME_MAC_WINDOW (f), &r, "\p", width < disp_height,
4746 0, 0, 0, scrollBarProc, (long) bar);
4747 #endif
4748 SET_SCROLL_BAR_CONTROL_HANDLE (bar, ch);
4749
4750 XSETWINDOW (bar->window, w);
4751 XSETINT (bar->top, top);
4752 XSETINT (bar->left, left);
4753 XSETINT (bar->width, width);
4754 XSETINT (bar->height, height);
4755 XSETINT (bar->start, 0);
4756 XSETINT (bar->end, 0);
4757 bar->dragging = Qnil;
4758 #ifdef USE_TOOLKIT_SCROLL_BARS
4759 bar->track_top = Qnil;
4760 bar->track_height = Qnil;
4761 #endif
4762
4763 /* Add bar to its frame's list of scroll bars. */
4764 bar->next = FRAME_SCROLL_BARS (f);
4765 bar->prev = Qnil;
4766 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
4767 if (!NILP (bar->next))
4768 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4769
4770 UNBLOCK_INPUT;
4771 return bar;
4772 }
4773
4774
4775 /* Draw BAR's handle in the proper position.
4776
4777 If the handle is already drawn from START to END, don't bother
4778 redrawing it, unless REBUILD is non-zero; in that case, always
4779 redraw it. (REBUILD is handy for drawing the handle after expose
4780 events.)
4781
4782 Normally, we want to constrain the start and end of the handle to
4783 fit inside its rectangle, but if the user is dragging the scroll
4784 bar handle, we want to let them drag it down all the way, so that
4785 the bar's top is as far down as it goes; otherwise, there's no way
4786 to move to the very end of the buffer. */
4787
4788 #ifndef USE_TOOLKIT_SCROLL_BARS
4789
4790 static void
4791 x_scroll_bar_set_handle (bar, start, end, rebuild)
4792 struct scroll_bar *bar;
4793 int start, end;
4794 int rebuild;
4795 {
4796 int dragging = ! NILP (bar->dragging);
4797 ControlHandle ch = SCROLL_BAR_CONTROL_HANDLE (bar);
4798 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4799 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
4800 int length = end - start;
4801
4802 /* If the display is already accurate, do nothing. */
4803 if (! rebuild
4804 && start == XINT (bar->start)
4805 && end == XINT (bar->end))
4806 return;
4807
4808 BLOCK_INPUT;
4809
4810 /* Make sure the values are reasonable, and try to preserve the
4811 distance between start and end. */
4812 if (start < 0)
4813 start = 0;
4814 else if (start > top_range)
4815 start = top_range;
4816 end = start + length;
4817
4818 if (end < start)
4819 end = start;
4820 else if (end > top_range && ! dragging)
4821 end = top_range;
4822
4823 /* Store the adjusted setting in the scroll bar. */
4824 XSETINT (bar->start, start);
4825 XSETINT (bar->end, end);
4826
4827 /* Clip the end position, just for display. */
4828 if (end > top_range)
4829 end = top_range;
4830
4831 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels below
4832 top positions, to make sure the handle is always at least that
4833 many pixels tall. */
4834 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
4835
4836 SetControlMinimum (ch, 0);
4837 /* Don't inadvertently activate deactivated scroll bars */
4838 if (GetControlMaximum (ch) != -1)
4839 SetControlMaximum (ch, top_range + VERTICAL_SCROLL_BAR_MIN_HANDLE
4840 - (end - start));
4841 SetControlValue (ch, start);
4842 #if TARGET_API_MAC_CARBON
4843 SetControlViewSize (ch, end - start);
4844 #endif
4845
4846 UNBLOCK_INPUT;
4847 }
4848
4849 #endif /* !USE_TOOLKIT_SCROLL_BARS */
4850
4851 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
4852 nil. */
4853
4854 static void
4855 x_scroll_bar_remove (bar)
4856 struct scroll_bar *bar;
4857 {
4858 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4859
4860 BLOCK_INPUT;
4861
4862 #if USE_CG_DRAWING
4863 mac_prepare_for_quickdraw (f);
4864 #endif
4865 /* Destroy the Mac scroll bar control */
4866 DisposeControl (SCROLL_BAR_CONTROL_HANDLE (bar));
4867
4868 /* Disassociate this scroll bar from its window. */
4869 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
4870
4871 UNBLOCK_INPUT;
4872 }
4873
4874
4875 /* Set the handle of the vertical scroll bar for WINDOW to indicate
4876 that we are displaying PORTION characters out of a total of WHOLE
4877 characters, starting at POSITION. If WINDOW has no scroll bar,
4878 create one. */
4879
4880 static void
4881 XTset_vertical_scroll_bar (w, portion, whole, position)
4882 struct window *w;
4883 int portion, whole, position;
4884 {
4885 struct frame *f = XFRAME (w->frame);
4886 struct scroll_bar *bar;
4887 int top, height, left, sb_left, width, sb_width, disp_top, disp_height;
4888 int window_y, window_height;
4889
4890 /* Get window dimensions. */
4891 window_box (w, -1, 0, &window_y, 0, &window_height);
4892 top = window_y;
4893 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
4894 height = window_height;
4895
4896 /* Compute the left edge of the scroll bar area. */
4897 left = WINDOW_SCROLL_BAR_AREA_X (w);
4898
4899 /* Compute the width of the scroll bar which might be less than
4900 the width of the area reserved for the scroll bar. */
4901 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
4902 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
4903 else
4904 sb_width = width;
4905
4906 /* Compute the left edge of the scroll bar. */
4907 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
4908 sb_left = left;
4909 else
4910 sb_left = left + width - sb_width;
4911
4912 /* Adjustments according to Inside Macintosh to make it look nice */
4913 disp_top = top;
4914 disp_height = height;
4915 #ifdef MAC_OS8
4916 if (disp_top == 0)
4917 {
4918 disp_top = -1;
4919 disp_height++;
4920 }
4921 else if (disp_top == FRAME_PIXEL_HEIGHT (f) - 16)
4922 {
4923 disp_top++;
4924 disp_height--;
4925 }
4926
4927 if (sb_left + sb_width == FRAME_PIXEL_WIDTH (f))
4928 sb_left++;
4929 #endif
4930
4931 /* Does the scroll bar exist yet? */
4932 if (NILP (w->vertical_scroll_bar))
4933 {
4934 BLOCK_INPUT;
4935 mac_clear_area (f, left, top, width, height);
4936 UNBLOCK_INPUT;
4937 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height, disp_top,
4938 disp_height);
4939 XSETVECTOR (w->vertical_scroll_bar, bar);
4940 }
4941 else
4942 {
4943 /* It may just need to be moved and resized. */
4944 ControlHandle ch;
4945
4946 bar = XSCROLL_BAR (w->vertical_scroll_bar);
4947 ch = SCROLL_BAR_CONTROL_HANDLE (bar);
4948
4949 BLOCK_INPUT;
4950
4951 /* If already correctly positioned, do nothing. */
4952 if (!(XINT (bar->left) == sb_left
4953 && XINT (bar->top) == top
4954 && XINT (bar->width) == sb_width
4955 && XINT (bar->height) == height))
4956 {
4957 /* Since toolkit scroll bars are smaller than the space reserved
4958 for them on the frame, we have to clear "under" them. */
4959 mac_clear_area (f, left, top, width, height);
4960
4961 #if USE_CG_DRAWING
4962 mac_prepare_for_quickdraw (f);
4963 #endif
4964 HideControl (ch);
4965 MoveControl (ch, sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM, disp_top);
4966 SizeControl (ch, sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4967 disp_height);
4968 #ifndef USE_TOOLKIT_SCROLL_BARS
4969 if (sb_width < disp_height)
4970 ShowControl (ch);
4971 #endif
4972
4973 /* Remember new settings. */
4974 XSETINT (bar->left, sb_left);
4975 XSETINT (bar->top, top);
4976 XSETINT (bar->width, sb_width);
4977 XSETINT (bar->height, height);
4978 #ifdef USE_TOOLKIT_SCROLL_BARS
4979 bar->track_top = Qnil;
4980 bar->track_height = Qnil;
4981 #endif
4982 }
4983
4984 UNBLOCK_INPUT;
4985 }
4986
4987 #ifdef USE_TOOLKIT_SCROLL_BARS
4988 if (NILP (bar->track_top))
4989 {
4990 if (sb_width >= disp_height)
4991 {
4992 XSETINT (bar->track_top, 0);
4993 XSETINT (bar->track_height, 0);
4994 }
4995 else
4996 {
4997 ControlHandle ch = SCROLL_BAR_CONTROL_HANDLE (bar);
4998 Rect r0, r1;
4999
5000 BLOCK_INPUT;
5001
5002 SetControl32BitMinimum (ch, 0);
5003 SetControl32BitMaximum (ch, 1);
5004 SetControlViewSize (ch, 1);
5005
5006 /* Move the scroll bar thumb to the top. */
5007 SetControl32BitValue (ch, 0);
5008 get_control_part_bounds (ch, kControlIndicatorPart, &r0);
5009
5010 /* Move the scroll bar thumb to the bottom. */
5011 SetControl32BitValue (ch, 1);
5012 get_control_part_bounds (ch, kControlIndicatorPart, &r1);
5013
5014 UnionRect (&r0, &r1, &r0);
5015 XSETINT (bar->track_top, r0.top);
5016 XSETINT (bar->track_height, r0.bottom - r0.top);
5017
5018 /* Don't show the scroll bar if its height is not enough to
5019 display the scroll bar thumb. */
5020 if (r0.bottom - r0.top > 0)
5021 ShowControl (ch);
5022
5023 UNBLOCK_INPUT;
5024 }
5025 }
5026
5027 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5028 #else /* not USE_TOOLKIT_SCROLL_BARS */
5029 /* Set the scroll bar's current state, unless we're currently being
5030 dragged. */
5031 if (NILP (bar->dragging))
5032 {
5033 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5034
5035 if (whole == 0)
5036 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5037 else
5038 {
5039 int start = ((double) position * top_range) / whole;
5040 int end = ((double) (position + portion) * top_range) / whole;
5041 x_scroll_bar_set_handle (bar, start, end, 0);
5042 }
5043 }
5044 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5045 }
5046
5047
5048 /* The following three hooks are used when we're doing a thorough
5049 redisplay of the frame. We don't explicitly know which scroll bars
5050 are going to be deleted, because keeping track of when windows go
5051 away is a real pain - "Can you say set-window-configuration, boys
5052 and girls?" Instead, we just assert at the beginning of redisplay
5053 that *all* scroll bars are to be removed, and then save a scroll bar
5054 from the fiery pit when we actually redisplay its window. */
5055
5056 /* Arrange for all scroll bars on FRAME to be removed at the next call
5057 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5058 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5059
5060 static void
5061 XTcondemn_scroll_bars (frame)
5062 FRAME_PTR frame;
5063 {
5064 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5065 while (! NILP (FRAME_SCROLL_BARS (frame)))
5066 {
5067 Lisp_Object bar;
5068 bar = FRAME_SCROLL_BARS (frame);
5069 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
5070 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5071 XSCROLL_BAR (bar)->prev = Qnil;
5072 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5073 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5074 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
5075 }
5076 }
5077
5078
5079 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5080 Note that WINDOW isn't necessarily condemned at all. */
5081
5082 static void
5083 XTredeem_scroll_bar (window)
5084 struct window *window;
5085 {
5086 struct scroll_bar *bar;
5087 struct frame *f;
5088
5089 /* We can't redeem this window's scroll bar if it doesn't have one. */
5090 if (NILP (window->vertical_scroll_bar))
5091 abort ();
5092
5093 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5094
5095 /* Unlink it from the condemned list. */
5096 f = XFRAME (WINDOW_FRAME (window));
5097 if (NILP (bar->prev))
5098 {
5099 /* If the prev pointer is nil, it must be the first in one of
5100 the lists. */
5101 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5102 /* It's not condemned. Everything's fine. */
5103 return;
5104 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5105 window->vertical_scroll_bar))
5106 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
5107 else
5108 /* If its prev pointer is nil, it must be at the front of
5109 one or the other! */
5110 abort ();
5111 }
5112 else
5113 XSCROLL_BAR (bar->prev)->next = bar->next;
5114
5115 if (! NILP (bar->next))
5116 XSCROLL_BAR (bar->next)->prev = bar->prev;
5117
5118 bar->next = FRAME_SCROLL_BARS (f);
5119 bar->prev = Qnil;
5120 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5121 if (! NILP (bar->next))
5122 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5123 }
5124
5125 /* Remove all scroll bars on FRAME that haven't been saved since the
5126 last call to `*condemn_scroll_bars_hook'. */
5127
5128 static void
5129 XTjudge_scroll_bars (f)
5130 FRAME_PTR f;
5131 {
5132 Lisp_Object bar, next;
5133
5134 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5135
5136 /* Clear out the condemned list now so we won't try to process any
5137 more events on the hapless scroll bars. */
5138 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
5139
5140 for (; ! NILP (bar); bar = next)
5141 {
5142 struct scroll_bar *b = XSCROLL_BAR (bar);
5143
5144 x_scroll_bar_remove (b);
5145
5146 next = b->next;
5147 b->next = b->prev = Qnil;
5148 }
5149
5150 /* Now there should be no references to the condemned scroll bars,
5151 and they should get garbage-collected. */
5152 }
5153
5154
5155 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5156 is set to something other than NO_EVENT, it is enqueued.
5157
5158 This may be called from a signal handler, so we have to ignore GC
5159 mark bits. */
5160
5161 static void
5162 x_scroll_bar_handle_click (bar, part_code, er, bufp)
5163 struct scroll_bar *bar;
5164 ControlPartCode part_code;
5165 EventRecord *er;
5166 struct input_event *bufp;
5167 {
5168 int win_y, top_range;
5169
5170 if (! GC_WINDOWP (bar->window))
5171 abort ();
5172
5173 bufp->kind = SCROLL_BAR_CLICK_EVENT;
5174 bufp->frame_or_window = bar->window;
5175 bufp->arg = Qnil;
5176
5177 bar->dragging = Qnil;
5178
5179 switch (part_code)
5180 {
5181 case kControlUpButtonPart:
5182 bufp->part = scroll_bar_up_arrow;
5183 break;
5184 case kControlDownButtonPart:
5185 bufp->part = scroll_bar_down_arrow;
5186 break;
5187 case kControlPageUpPart:
5188 bufp->part = scroll_bar_above_handle;
5189 break;
5190 case kControlPageDownPart:
5191 bufp->part = scroll_bar_below_handle;
5192 break;
5193 #if TARGET_API_MAC_CARBON
5194 default:
5195 #else
5196 case kControlIndicatorPart:
5197 #endif
5198 if (er->what == mouseDown)
5199 bar->dragging = make_number (0);
5200 XSETVECTOR (last_mouse_scroll_bar, bar);
5201 bufp->part = scroll_bar_handle;
5202 break;
5203 }
5204
5205 win_y = XINT (bufp->y) - XINT (bar->top);
5206 top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (0/*dummy*/, XINT (bar->height));
5207
5208 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5209
5210 win_y -= 24;
5211
5212 if (! NILP (bar->dragging))
5213 win_y -= XINT (bar->dragging);
5214
5215 if (win_y < 0)
5216 win_y = 0;
5217 if (win_y > top_range)
5218 win_y = top_range;
5219
5220 XSETINT (bufp->x, win_y);
5221 XSETINT (bufp->y, top_range);
5222 }
5223
5224 #ifndef USE_TOOLKIT_SCROLL_BARS
5225
5226 /* Handle some mouse motion while someone is dragging the scroll bar.
5227
5228 This may be called from a signal handler, so we have to ignore GC
5229 mark bits. */
5230
5231 static void
5232 x_scroll_bar_note_movement (bar, y_pos, t)
5233 struct scroll_bar *bar;
5234 int y_pos;
5235 Time t;
5236 {
5237 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
5238
5239 last_mouse_movement_time = t;
5240
5241 f->mouse_moved = 1;
5242 XSETVECTOR (last_mouse_scroll_bar, bar);
5243
5244 /* If we're dragging the bar, display it. */
5245 if (! GC_NILP (bar->dragging))
5246 {
5247 /* Where should the handle be now? */
5248 int new_start = y_pos - 24;
5249
5250 if (new_start != XINT (bar->start))
5251 {
5252 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
5253
5254 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5255 }
5256 }
5257 }
5258
5259 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5260
5261 /* Return information to the user about the current position of the mouse
5262 on the scroll bar. */
5263
5264 static void
5265 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
5266 FRAME_PTR *fp;
5267 Lisp_Object *bar_window;
5268 enum scroll_bar_part *part;
5269 Lisp_Object *x, *y;
5270 unsigned long *time;
5271 {
5272 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
5273 ControlHandle ch = SCROLL_BAR_CONTROL_HANDLE (bar);
5274 #if TARGET_API_MAC_CARBON
5275 WindowPtr wp = GetControlOwner (ch);
5276 #else
5277 WindowPtr wp = (*ch)->contrlOwner;
5278 #endif
5279 Point mouse_pos;
5280 struct frame *f = mac_window_to_frame (wp);
5281 int win_y, top_range;
5282
5283 SetPortWindowPort (wp);
5284
5285 GetMouse (&mouse_pos);
5286
5287 win_y = mouse_pos.v - XINT (bar->top);
5288 top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
5289
5290 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5291
5292 win_y -= 24;
5293
5294 if (! NILP (bar->dragging))
5295 win_y -= XINT (bar->dragging);
5296
5297 if (win_y < 0)
5298 win_y = 0;
5299 if (win_y > top_range)
5300 win_y = top_range;
5301
5302 *fp = f;
5303 *bar_window = bar->window;
5304
5305 if (! NILP (bar->dragging))
5306 *part = scroll_bar_handle;
5307 else if (win_y < XINT (bar->start))
5308 *part = scroll_bar_above_handle;
5309 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5310 *part = scroll_bar_handle;
5311 else
5312 *part = scroll_bar_below_handle;
5313
5314 XSETINT (*x, win_y);
5315 XSETINT (*y, top_range);
5316
5317 f->mouse_moved = 0;
5318 last_mouse_scroll_bar = Qnil;
5319
5320 *time = last_mouse_movement_time;
5321 }
5322
5323
5324 /* The screen has been cleared so we may have changed foreground or
5325 background colors, and the scroll bars may need to be redrawn.
5326 Clear out the scroll bars, and ask for expose events, so we can
5327 redraw them. */
5328
5329 void
5330 x_scroll_bar_clear (f)
5331 FRAME_PTR f;
5332 {
5333 XTcondemn_scroll_bars (f);
5334 XTjudge_scroll_bars (f);
5335 }
5336
5337 \f
5338 /***********************************************************************
5339 Text Cursor
5340 ***********************************************************************/
5341
5342 /* Set clipping for output in glyph row ROW. W is the window in which
5343 we operate. GC is the graphics context to set clipping in.
5344
5345 ROW may be a text row or, e.g., a mode line. Text rows must be
5346 clipped to the interior of the window dedicated to text display,
5347 mode lines must be clipped to the whole window. */
5348
5349 static void
5350 x_clip_to_row (w, row, area, gc)
5351 struct window *w;
5352 struct glyph_row *row;
5353 int area;
5354 GC gc;
5355 {
5356 struct frame *f = XFRAME (WINDOW_FRAME (w));
5357 Rect clip_rect;
5358 int window_x, window_y, window_width;
5359
5360 window_box (w, area, &window_x, &window_y, &window_width, 0);
5361
5362 clip_rect.left = window_x;
5363 clip_rect.top = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
5364 clip_rect.top = max (clip_rect.top, window_y);
5365 clip_rect.right = clip_rect.left + window_width;
5366 clip_rect.bottom = clip_rect.top + row->visible_height;
5367
5368 mac_set_clip_rectangles (FRAME_MAC_DISPLAY (f), gc, &clip_rect, 1);
5369 }
5370
5371
5372 /* Draw a hollow box cursor on window W in glyph row ROW. */
5373
5374 static void
5375 x_draw_hollow_cursor (w, row)
5376 struct window *w;
5377 struct glyph_row *row;
5378 {
5379 struct frame *f = XFRAME (WINDOW_FRAME (w));
5380 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
5381 Display *dpy = FRAME_MAC_DISPLAY (f);
5382 int x, y, wd, h;
5383 XGCValues xgcv;
5384 struct glyph *cursor_glyph;
5385 GC gc;
5386
5387 /* Get the glyph the cursor is on. If we can't tell because
5388 the current matrix is invalid or such, give up. */
5389 cursor_glyph = get_phys_cursor_glyph (w);
5390 if (cursor_glyph == NULL)
5391 return;
5392
5393 /* Compute frame-relative coordinates for phys cursor. */
5394 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
5395 y = get_phys_cursor_geometry (w, row, cursor_glyph, &h);
5396 wd = w->phys_cursor_width;
5397
5398 /* The foreground of cursor_gc is typically the same as the normal
5399 background color, which can cause the cursor box to be invisible. */
5400 xgcv.foreground = f->output_data.mac->cursor_pixel;
5401 if (dpyinfo->scratch_cursor_gc)
5402 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
5403 else
5404 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_MAC_WINDOW (f),
5405 GCForeground, &xgcv);
5406 gc = dpyinfo->scratch_cursor_gc;
5407
5408 /* Set clipping, draw the rectangle, and reset clipping again. */
5409 x_clip_to_row (w, row, TEXT_AREA, gc);
5410 mac_draw_rectangle (f, gc, x, y, wd, h - 1);
5411 mac_reset_clip_rectangles (dpy, gc);
5412 }
5413
5414
5415 /* Draw a bar cursor on window W in glyph row ROW.
5416
5417 Implementation note: One would like to draw a bar cursor with an
5418 angle equal to the one given by the font property XA_ITALIC_ANGLE.
5419 Unfortunately, I didn't find a font yet that has this property set.
5420 --gerd. */
5421
5422 static void
5423 x_draw_bar_cursor (w, row, width, kind)
5424 struct window *w;
5425 struct glyph_row *row;
5426 int width;
5427 enum text_cursor_kinds kind;
5428 {
5429 struct frame *f = XFRAME (w->frame);
5430 struct glyph *cursor_glyph;
5431
5432 /* If cursor is out of bounds, don't draw garbage. This can happen
5433 in mini-buffer windows when switching between echo area glyphs
5434 and mini-buffer. */
5435 cursor_glyph = get_phys_cursor_glyph (w);
5436 if (cursor_glyph == NULL)
5437 return;
5438
5439 /* If on an image, draw like a normal cursor. That's usually better
5440 visible than drawing a bar, esp. if the image is large so that
5441 the bar might not be in the window. */
5442 if (cursor_glyph->type == IMAGE_GLYPH)
5443 {
5444 struct glyph_row *row;
5445 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
5446 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
5447 }
5448 else
5449 {
5450 Display *dpy = FRAME_MAC_DISPLAY (f);
5451 Window window = FRAME_MAC_WINDOW (f);
5452 GC gc = FRAME_MAC_DISPLAY_INFO (f)->scratch_cursor_gc;
5453 unsigned long mask = GCForeground | GCBackground;
5454 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
5455 XGCValues xgcv;
5456
5457 /* If the glyph's background equals the color we normally draw
5458 the bar cursor in, the bar cursor in its normal color is
5459 invisible. Use the glyph's foreground color instead in this
5460 case, on the assumption that the glyph's colors are chosen so
5461 that the glyph is legible. */
5462 if (face->background == f->output_data.mac->cursor_pixel)
5463 xgcv.background = xgcv.foreground = face->foreground;
5464 else
5465 xgcv.background = xgcv.foreground = f->output_data.mac->cursor_pixel;
5466
5467 if (gc)
5468 XChangeGC (dpy, gc, mask, &xgcv);
5469 else
5470 {
5471 gc = XCreateGC (dpy, window, mask, &xgcv);
5472 FRAME_MAC_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
5473 }
5474
5475 if (width < 0)
5476 width = FRAME_CURSOR_WIDTH (f);
5477 width = min (cursor_glyph->pixel_width, width);
5478
5479 w->phys_cursor_width = width;
5480 x_clip_to_row (w, row, TEXT_AREA, gc);
5481
5482 if (kind == BAR_CURSOR)
5483 mac_fill_rectangle (f, gc,
5484 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
5485 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
5486 width, row->height);
5487 else
5488 mac_fill_rectangle (f, gc,
5489 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
5490 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
5491 row->height - width),
5492 cursor_glyph->pixel_width,
5493 width);
5494
5495 mac_reset_clip_rectangles (dpy, gc);
5496 }
5497 }
5498
5499
5500 /* RIF: Define cursor CURSOR on frame F. */
5501
5502 static void
5503 mac_define_frame_cursor (f, cursor)
5504 struct frame *f;
5505 Cursor cursor;
5506 {
5507 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
5508
5509 if (dpyinfo->x_focus_frame == f)
5510 SetThemeCursor (cursor);
5511 }
5512
5513
5514 /* RIF: Clear area on frame F. */
5515
5516 static void
5517 mac_clear_frame_area (f, x, y, width, height)
5518 struct frame *f;
5519 int x, y, width, height;
5520 {
5521 mac_clear_area (f, x, y, width, height);
5522 }
5523
5524
5525 /* RIF: Draw cursor on window W. */
5526
5527 static void
5528 mac_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
5529 struct window *w;
5530 struct glyph_row *glyph_row;
5531 int x, y;
5532 int cursor_type, cursor_width;
5533 int on_p, active_p;
5534 {
5535 if (on_p)
5536 {
5537 w->phys_cursor_type = cursor_type;
5538 w->phys_cursor_on_p = 1;
5539
5540 if (glyph_row->exact_window_width_line_p
5541 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
5542 {
5543 glyph_row->cursor_in_fringe_p = 1;
5544 draw_fringe_bitmap (w, glyph_row, 0);
5545 }
5546 else
5547 switch (cursor_type)
5548 {
5549 case HOLLOW_BOX_CURSOR:
5550 x_draw_hollow_cursor (w, glyph_row);
5551 break;
5552
5553 case FILLED_BOX_CURSOR:
5554 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
5555 break;
5556
5557 case BAR_CURSOR:
5558 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
5559 break;
5560
5561 case HBAR_CURSOR:
5562 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
5563 break;
5564
5565 case NO_CURSOR:
5566 w->phys_cursor_width = 0;
5567 break;
5568
5569 default:
5570 abort ();
5571 }
5572 }
5573 }
5574
5575 \f
5576 /* Icons. */
5577
5578 #if 0 /* MAC_TODO: no icon support yet. */
5579 int
5580 x_bitmap_icon (f, icon)
5581 struct frame *f;
5582 Lisp_Object icon;
5583 {
5584 HANDLE hicon;
5585
5586 if (FRAME_W32_WINDOW (f) == 0)
5587 return 1;
5588
5589 if (NILP (icon))
5590 hicon = LoadIcon (hinst, EMACS_CLASS);
5591 else if (STRINGP (icon))
5592 hicon = LoadImage (NULL, (LPCTSTR) SDATA (icon), IMAGE_ICON, 0, 0,
5593 LR_DEFAULTSIZE | LR_LOADFROMFILE);
5594 else if (SYMBOLP (icon))
5595 {
5596 LPCTSTR name;
5597
5598 if (EQ (icon, intern ("application")))
5599 name = (LPCTSTR) IDI_APPLICATION;
5600 else if (EQ (icon, intern ("hand")))
5601 name = (LPCTSTR) IDI_HAND;
5602 else if (EQ (icon, intern ("question")))
5603 name = (LPCTSTR) IDI_QUESTION;
5604 else if (EQ (icon, intern ("exclamation")))
5605 name = (LPCTSTR) IDI_EXCLAMATION;
5606 else if (EQ (icon, intern ("asterisk")))
5607 name = (LPCTSTR) IDI_ASTERISK;
5608 else if (EQ (icon, intern ("winlogo")))
5609 name = (LPCTSTR) IDI_WINLOGO;
5610 else
5611 return 1;
5612
5613 hicon = LoadIcon (NULL, name);
5614 }
5615 else
5616 return 1;
5617
5618 if (hicon == NULL)
5619 return 1;
5620
5621 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_BIG,
5622 (LPARAM) hicon);
5623
5624 return 0;
5625 }
5626 #endif /* MAC_TODO */
5627 \f
5628 /************************************************************************
5629 Handling X errors
5630 ************************************************************************/
5631
5632 /* Display Error Handling functions not used on W32. Listing them here
5633 helps diff stay in step when comparing w32term.c with xterm.c.
5634
5635 x_error_catcher (display, error)
5636 x_catch_errors (dpy)
5637 x_catch_errors_unwind (old_val)
5638 x_check_errors (dpy, format)
5639 x_had_errors_p (dpy)
5640 x_clear_errors (dpy)
5641 x_uncatch_errors (dpy, count)
5642 x_trace_wire ()
5643 x_connection_signal (signalnum)
5644 x_connection_closed (dpy, error_message)
5645 x_error_quitter (display, error)
5646 x_error_handler (display, error)
5647 x_io_error_quitter (display)
5648
5649 */
5650
5651 \f
5652 /* Changing the font of the frame. */
5653
5654 /* Give frame F the font named FONTNAME as its default font, and
5655 return the full name of that font. FONTNAME may be a wildcard
5656 pattern; in that case, we choose some font that fits the pattern.
5657 The return value shows which font we chose. */
5658
5659 Lisp_Object
5660 x_new_font (f, fontname)
5661 struct frame *f;
5662 register char *fontname;
5663 {
5664 struct font_info *fontp
5665 = FS_LOAD_FONT (f, 0, fontname, -1);
5666
5667 if (!fontp)
5668 return Qnil;
5669
5670 FRAME_FONT (f) = (XFontStruct *) (fontp->font);
5671 FRAME_BASELINE_OFFSET (f) = fontp->baseline_offset;
5672 FRAME_FONTSET (f) = -1;
5673
5674 FRAME_COLUMN_WIDTH (f) = fontp->average_width;
5675 FRAME_SPACE_WIDTH (f) = fontp->space_width;
5676 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (FRAME_FONT (f));
5677
5678 compute_fringe_widths (f, 1);
5679
5680 /* Compute the scroll bar width in character columns. */
5681 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
5682 {
5683 int wid = FRAME_COLUMN_WIDTH (f);
5684 FRAME_CONFIG_SCROLL_BAR_COLS (f)
5685 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
5686 }
5687 else
5688 {
5689 int wid = FRAME_COLUMN_WIDTH (f);
5690 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
5691 }
5692
5693 /* Now make the frame display the given font. */
5694 if (FRAME_MAC_WINDOW (f) != 0)
5695 {
5696 XSetFont (FRAME_MAC_DISPLAY (f), f->output_data.mac->normal_gc,
5697 FRAME_FONT (f));
5698 XSetFont (FRAME_MAC_DISPLAY (f), f->output_data.mac->reverse_gc,
5699 FRAME_FONT (f));
5700 XSetFont (FRAME_MAC_DISPLAY (f), f->output_data.mac->cursor_gc,
5701 FRAME_FONT (f));
5702
5703 /* Don't change the size of a tip frame; there's no point in
5704 doing it because it's done in Fx_show_tip, and it leads to
5705 problems because the tip frame has no widget. */
5706 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
5707 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
5708 }
5709
5710 return build_string (fontp->full_name);
5711 }
5712
5713 /* Give frame F the fontset named FONTSETNAME as its default font, and
5714 return the full name of that fontset. FONTSETNAME may be a wildcard
5715 pattern; in that case, we choose some fontset that fits the pattern.
5716 The return value shows which fontset we chose. */
5717
5718 Lisp_Object
5719 x_new_fontset (f, fontsetname)
5720 struct frame *f;
5721 char *fontsetname;
5722 {
5723 int fontset = fs_query_fontset (build_string (fontsetname), 0);
5724 Lisp_Object result;
5725
5726 if (fontset < 0)
5727 return Qnil;
5728
5729 if (FRAME_FONTSET (f) == fontset)
5730 /* This fontset is already set in frame F. There's nothing more
5731 to do. */
5732 return fontset_name (fontset);
5733
5734 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
5735
5736 if (!STRINGP (result))
5737 /* Can't load ASCII font. */
5738 return Qnil;
5739
5740 /* Since x_new_font doesn't update any fontset information, do it now. */
5741 FRAME_FONTSET (f) = fontset;
5742
5743 return build_string (fontsetname);
5744 }
5745
5746 \f
5747 /***********************************************************************
5748 TODO: W32 Input Methods
5749 ***********************************************************************/
5750 /* Listing missing functions from xterm.c helps diff stay in step.
5751
5752 xim_destroy_callback (xim, client_data, call_data)
5753 xim_open_dpy (dpyinfo, resource_name)
5754 struct xim_inst_t
5755 xim_instantiate_callback (display, client_data, call_data)
5756 xim_initialize (dpyinfo, resource_name)
5757 xim_close_dpy (dpyinfo)
5758
5759 */
5760
5761 \f
5762 void
5763 mac_get_window_bounds (f, inner, outer)
5764 struct frame *f;
5765 Rect *inner, *outer;
5766 {
5767 #if TARGET_API_MAC_CARBON
5768 GetWindowBounds (FRAME_MAC_WINDOW (f), kWindowContentRgn, inner);
5769 GetWindowBounds (FRAME_MAC_WINDOW (f), kWindowStructureRgn, outer);
5770 #else /* not TARGET_API_MAC_CARBON */
5771 RgnHandle region = NewRgn ();
5772
5773 GetWindowRegion (FRAME_MAC_WINDOW (f), kWindowContentRgn, region);
5774 *inner = (*region)->rgnBBox;
5775 GetWindowRegion (FRAME_MAC_WINDOW (f), kWindowStructureRgn, region);
5776 *outer = (*region)->rgnBBox;
5777 DisposeRgn (region);
5778 #endif /* not TARGET_API_MAC_CARBON */
5779 }
5780
5781
5782 \f
5783 /* Calculate the absolute position in frame F
5784 from its current recorded position values and gravity. */
5785
5786 void
5787 x_calc_absolute_position (f)
5788 struct frame *f;
5789 {
5790 int width_diff = 0, height_diff = 0;
5791 int flags = f->size_hint_flags;
5792 Rect inner, outer;
5793
5794 /* We have nothing to do if the current position
5795 is already for the top-left corner. */
5796 if (! ((flags & XNegative) || (flags & YNegative)))
5797 return;
5798
5799 /* Find the offsets of the outside upper-left corner of
5800 the inner window, with respect to the outer window. */
5801 mac_get_window_bounds (f, &inner, &outer);
5802
5803 width_diff = (outer.right - outer.left) - (inner.right - inner.left);
5804 height_diff = (outer.bottom - outer.top) - (inner.bottom - inner.top);
5805
5806 /* Treat negative positions as relative to the leftmost bottommost
5807 position that fits on the screen. */
5808 if (flags & XNegative)
5809 f->left_pos = (FRAME_MAC_DISPLAY_INFO (f)->width
5810 - width_diff
5811 - FRAME_PIXEL_WIDTH (f)
5812 + f->left_pos);
5813
5814 if (flags & YNegative)
5815 f->top_pos = (FRAME_MAC_DISPLAY_INFO (f)->height
5816 - height_diff
5817 - FRAME_PIXEL_HEIGHT (f)
5818 + f->top_pos);
5819
5820 /* The left_pos and top_pos
5821 are now relative to the top and left screen edges,
5822 so the flags should correspond. */
5823 f->size_hint_flags &= ~ (XNegative | YNegative);
5824 }
5825
5826 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
5827 to really change the position, and 0 when calling from
5828 x_make_frame_visible (in that case, XOFF and YOFF are the current
5829 position values). It is -1 when calling from x_set_frame_parameters,
5830 which means, do adjust for borders but don't change the gravity. */
5831
5832 void
5833 x_set_offset (f, xoff, yoff, change_gravity)
5834 struct frame *f;
5835 register int xoff, yoff;
5836 int change_gravity;
5837 {
5838 if (change_gravity > 0)
5839 {
5840 f->top_pos = yoff;
5841 f->left_pos = xoff;
5842 f->size_hint_flags &= ~ (XNegative | YNegative);
5843 if (xoff < 0)
5844 f->size_hint_flags |= XNegative;
5845 if (yoff < 0)
5846 f->size_hint_flags |= YNegative;
5847 f->win_gravity = NorthWestGravity;
5848 }
5849 x_calc_absolute_position (f);
5850
5851 BLOCK_INPUT;
5852 x_wm_set_size_hint (f, (long) 0, 0);
5853
5854 #if TARGET_API_MAC_CARBON
5855 MoveWindowStructure (FRAME_MAC_WINDOW (f), f->left_pos, f->top_pos);
5856 /* If the title bar is completely outside the screen, adjust the
5857 position. */
5858 ConstrainWindowToScreen (FRAME_MAC_WINDOW (f), kWindowTitleBarRgn,
5859 kWindowConstrainMoveRegardlessOfFit
5860 | kWindowConstrainAllowPartial, NULL, NULL);
5861 x_real_positions (f, &f->left_pos, &f->top_pos);
5862 #else
5863 {
5864 Rect inner, outer, screen_rect, dummy;
5865 RgnHandle region = NewRgn ();
5866
5867 mac_get_window_bounds (f, &inner, &outer);
5868 f->x_pixels_diff = inner.left - outer.left;
5869 f->y_pixels_diff = inner.top - outer.top;
5870 MoveWindow (FRAME_MAC_WINDOW (f), f->left_pos + f->x_pixels_diff,
5871 f->top_pos + f->y_pixels_diff, false);
5872
5873 /* If the title bar is completely outside the screen, adjust the
5874 position. The variable `outer' holds the title bar rectangle.
5875 The variable `inner' holds slightly smaller one than `outer',
5876 so that the calculation of overlapping may not become too
5877 strict. */
5878 GetWindowRegion (FRAME_MAC_WINDOW (f), kWindowTitleBarRgn, region);
5879 outer = (*region)->rgnBBox;
5880 DisposeRgn (region);
5881 inner = outer;
5882 InsetRect (&inner, 8, 8);
5883 screen_rect = qd.screenBits.bounds;
5884 screen_rect.top += GetMBarHeight ();
5885
5886 if (!SectRect (&inner, &screen_rect, &dummy))
5887 {
5888 if (inner.right <= screen_rect.left)
5889 f->left_pos = screen_rect.left;
5890 else if (inner.left >= screen_rect.right)
5891 f->left_pos = screen_rect.right - (outer.right - outer.left);
5892
5893 if (inner.bottom <= screen_rect.top)
5894 f->top_pos = screen_rect.top;
5895 else if (inner.top >= screen_rect.bottom)
5896 f->top_pos = screen_rect.bottom - (outer.bottom - outer.top);
5897
5898 MoveWindow (FRAME_MAC_WINDOW (f), f->left_pos + f->x_pixels_diff,
5899 f->top_pos + f->y_pixels_diff, false);
5900 }
5901 }
5902 #endif
5903
5904 UNBLOCK_INPUT;
5905 }
5906
5907 /* Call this to change the size of frame F's x-window.
5908 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
5909 for this size change and subsequent size changes.
5910 Otherwise we leave the window gravity unchanged. */
5911
5912 void
5913 x_set_window_size (f, change_gravity, cols, rows)
5914 struct frame *f;
5915 int change_gravity;
5916 int cols, rows;
5917 {
5918 int pixelwidth, pixelheight;
5919
5920 BLOCK_INPUT;
5921
5922 check_frame_size (f, &rows, &cols);
5923 f->scroll_bar_actual_width
5924 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
5925
5926 compute_fringe_widths (f, 0);
5927
5928 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
5929 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
5930
5931 f->win_gravity = NorthWestGravity;
5932 x_wm_set_size_hint (f, (long) 0, 0);
5933
5934 SizeWindow (FRAME_MAC_WINDOW (f), pixelwidth, pixelheight, 0);
5935 #if TARGET_API_MAC_CARBON
5936 if (f->output_data.mac->hourglass_control)
5937 {
5938 #if USE_CG_DRAWING
5939 mac_prepare_for_quickdraw (f);
5940 #endif
5941 MoveControl (f->output_data.mac->hourglass_control,
5942 pixelwidth - HOURGLASS_WIDTH, 0);
5943 }
5944 #endif
5945
5946 /* Now, strictly speaking, we can't be sure that this is accurate,
5947 but the window manager will get around to dealing with the size
5948 change request eventually, and we'll hear how it went when the
5949 ConfigureNotify event gets here.
5950
5951 We could just not bother storing any of this information here,
5952 and let the ConfigureNotify event set everything up, but that
5953 might be kind of confusing to the Lisp code, since size changes
5954 wouldn't be reported in the frame parameters until some random
5955 point in the future when the ConfigureNotify event arrives.
5956
5957 We pass 1 for DELAY since we can't run Lisp code inside of
5958 a BLOCK_INPUT. */
5959 change_frame_size (f, rows, cols, 0, 1, 0);
5960 FRAME_PIXEL_WIDTH (f) = pixelwidth;
5961 FRAME_PIXEL_HEIGHT (f) = pixelheight;
5962
5963 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
5964 receive in the ConfigureNotify event; if we get what we asked
5965 for, then the event won't cause the screen to become garbaged, so
5966 we have to make sure to do it here. */
5967 SET_FRAME_GARBAGED (f);
5968
5969 XFlush (FRAME_X_DISPLAY (f));
5970
5971 /* If cursor was outside the new size, mark it as off. */
5972 mark_window_cursors_off (XWINDOW (f->root_window));
5973
5974 /* Clear out any recollection of where the mouse highlighting was,
5975 since it might be in a place that's outside the new frame size.
5976 Actually checking whether it is outside is a pain in the neck,
5977 so don't try--just let the highlighting be done afresh with new size. */
5978 cancel_mouse_face (f);
5979
5980 UNBLOCK_INPUT;
5981 }
5982 \f
5983 /* Mouse warping. */
5984
5985 void x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y);
5986
5987 void
5988 x_set_mouse_position (f, x, y)
5989 struct frame *f;
5990 int x, y;
5991 {
5992 int pix_x, pix_y;
5993
5994 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
5995 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
5996
5997 if (pix_x < 0) pix_x = 0;
5998 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
5999
6000 if (pix_y < 0) pix_y = 0;
6001 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
6002
6003 x_set_mouse_pixel_position (f, pix_x, pix_y);
6004 }
6005
6006 void
6007 x_set_mouse_pixel_position (f, pix_x, pix_y)
6008 struct frame *f;
6009 int pix_x, pix_y;
6010 {
6011 #if 0 /* MAC_TODO: CursorDeviceMoveTo is non-Carbon */
6012 BLOCK_INPUT;
6013
6014 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
6015 0, 0, 0, 0, pix_x, pix_y);
6016 UNBLOCK_INPUT;
6017 #endif
6018 }
6019 \f
6020 /* focus shifting, raising and lowering. */
6021
6022 void
6023 x_focus_on_frame (f)
6024 struct frame *f;
6025 {
6026 #if 0 /* This proves to be unpleasant. */
6027 x_raise_frame (f);
6028 #endif
6029 #if 0
6030 /* I don't think that the ICCCM allows programs to do things like this
6031 without the interaction of the window manager. Whatever you end up
6032 doing with this code, do it to x_unfocus_frame too. */
6033 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6034 RevertToPointerRoot, CurrentTime);
6035 #endif /* ! 0 */
6036 }
6037
6038 void
6039 x_unfocus_frame (f)
6040 struct frame *f;
6041 {
6042 }
6043
6044 /* Raise frame F. */
6045
6046 void
6047 x_raise_frame (f)
6048 struct frame *f;
6049 {
6050 if (f->async_visible)
6051 {
6052 BLOCK_INPUT;
6053 BringToFront (FRAME_MAC_WINDOW (f));
6054 UNBLOCK_INPUT;
6055 }
6056 }
6057
6058 /* Lower frame F. */
6059
6060 void
6061 x_lower_frame (f)
6062 struct frame *f;
6063 {
6064 if (f->async_visible)
6065 {
6066 BLOCK_INPUT;
6067 SendBehind (FRAME_MAC_WINDOW (f), NULL);
6068 UNBLOCK_INPUT;
6069 }
6070 }
6071
6072 static void
6073 XTframe_raise_lower (f, raise_flag)
6074 FRAME_PTR f;
6075 int raise_flag;
6076 {
6077 if (raise_flag)
6078 x_raise_frame (f);
6079 else
6080 x_lower_frame (f);
6081 }
6082 \f
6083 /* Change of visibility. */
6084
6085 static void
6086 mac_handle_visibility_change (f)
6087 struct frame *f;
6088 {
6089 WindowPtr wp = FRAME_MAC_WINDOW (f);
6090 int visible = 0, iconified = 0;
6091 struct input_event buf;
6092
6093 if (IsWindowVisible (wp))
6094 {
6095 if (IsWindowCollapsed (wp))
6096 iconified = 1;
6097 else
6098 visible = 1;
6099 }
6100
6101 if (!f->async_visible && visible)
6102 {
6103 if (f->iconified)
6104 {
6105 /* wait_reading_process_output will notice this and update
6106 the frame's display structures. If we were made
6107 invisible, we should not set garbaged, because that stops
6108 redrawing on Update events. */
6109 SET_FRAME_GARBAGED (f);
6110
6111 EVENT_INIT (buf);
6112 buf.kind = DEICONIFY_EVENT;
6113 XSETFRAME (buf.frame_or_window, f);
6114 kbd_buffer_store_event (&buf);
6115 }
6116 else if (! NILP (Vframe_list) && ! NILP (XCDR (Vframe_list)))
6117 /* Force a redisplay sooner or later to update the
6118 frame titles in case this is the second frame. */
6119 record_asynch_buffer_change ();
6120 }
6121 else if (f->async_visible && !visible)
6122 if (iconified)
6123 {
6124 EVENT_INIT (buf);
6125 buf.kind = ICONIFY_EVENT;
6126 XSETFRAME (buf.frame_or_window, f);
6127 kbd_buffer_store_event (&buf);
6128 }
6129
6130 f->async_visible = visible;
6131 f->async_iconified = iconified;
6132 }
6133
6134 /* This tries to wait until the frame is really visible.
6135 However, if the window manager asks the user where to position
6136 the frame, this will return before the user finishes doing that.
6137 The frame will not actually be visible at that time,
6138 but it will become visible later when the window manager
6139 finishes with it. */
6140
6141 void
6142 x_make_frame_visible (f)
6143 struct frame *f;
6144 {
6145 BLOCK_INPUT;
6146
6147 if (! FRAME_VISIBLE_P (f))
6148 {
6149 /* We test FRAME_GARBAGED_P here to make sure we don't
6150 call x_set_offset a second time
6151 if we get to x_make_frame_visible a second time
6152 before the window gets really visible. */
6153 if (! FRAME_ICONIFIED_P (f)
6154 && ! f->output_data.mac->asked_for_visible)
6155 {
6156 #if TARGET_API_MAC_CARBON
6157 if (!(FRAME_SIZE_HINTS (f)->flags & (USPosition | PPosition)))
6158 {
6159 struct frame *sf = SELECTED_FRAME ();
6160 if (!FRAME_MAC_P (sf))
6161 RepositionWindow (FRAME_MAC_WINDOW (f), NULL,
6162 kWindowCenterOnMainScreen);
6163 else
6164 RepositionWindow (FRAME_MAC_WINDOW (f),
6165 FRAME_MAC_WINDOW (sf),
6166 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
6167 kWindowCascadeStartAtParentWindowScreen
6168 #else
6169 kWindowCascadeOnParentWindowScreen
6170 #endif
6171 );
6172 x_real_positions (f, &f->left_pos, &f->top_pos);
6173 }
6174 else
6175 #endif
6176 x_set_offset (f, f->left_pos, f->top_pos, 0);
6177 }
6178
6179 f->output_data.mac->asked_for_visible = 1;
6180
6181 CollapseWindow (FRAME_MAC_WINDOW (f), false);
6182 ShowWindow (FRAME_MAC_WINDOW (f));
6183 }
6184
6185 XFlush (FRAME_MAC_DISPLAY (f));
6186
6187 /* Synchronize to ensure Emacs knows the frame is visible
6188 before we do anything else. We do this loop with input not blocked
6189 so that incoming events are handled. */
6190 {
6191 Lisp_Object frame;
6192 int count;
6193
6194 /* This must come after we set COUNT. */
6195 UNBLOCK_INPUT;
6196
6197 XSETFRAME (frame, f);
6198
6199 /* Wait until the frame is visible. Process X events until a
6200 MapNotify event has been seen, or until we think we won't get a
6201 MapNotify at all.. */
6202 for (count = input_signal_count + 10;
6203 input_signal_count < count && !FRAME_VISIBLE_P (f);)
6204 {
6205 /* Force processing of queued events. */
6206 x_sync (f);
6207
6208 /* Machines that do polling rather than SIGIO have been
6209 observed to go into a busy-wait here. So we'll fake an
6210 alarm signal to let the handler know that there's something
6211 to be read. We used to raise a real alarm, but it seems
6212 that the handler isn't always enabled here. This is
6213 probably a bug. */
6214 if (input_polling_used ())
6215 {
6216 /* It could be confusing if a real alarm arrives while
6217 processing the fake one. Turn it off and let the
6218 handler reset it. */
6219 extern void poll_for_input_1 P_ ((void));
6220 int old_poll_suppress_count = poll_suppress_count;
6221 poll_suppress_count = 1;
6222 poll_for_input_1 ();
6223 poll_suppress_count = old_poll_suppress_count;
6224 }
6225
6226 /* See if a MapNotify event has been processed. */
6227 FRAME_SAMPLE_VISIBILITY (f);
6228 }
6229 }
6230 }
6231
6232 /* Change from mapped state to withdrawn state. */
6233
6234 /* Make the frame visible (mapped and not iconified). */
6235
6236 void
6237 x_make_frame_invisible (f)
6238 struct frame *f;
6239 {
6240 /* A deactivate event does not occur when the last visible frame is
6241 made invisible. So if we clear the highlight here, it will not
6242 be rehighlighted when it is made visible. */
6243 #if 0
6244 /* Don't keep the highlight on an invisible frame. */
6245 if (FRAME_MAC_DISPLAY_INFO (f)->x_highlight_frame == f)
6246 FRAME_MAC_DISPLAY_INFO (f)->x_highlight_frame = 0;
6247 #endif
6248
6249 BLOCK_INPUT;
6250
6251 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
6252 that the current position of the window is user-specified, rather than
6253 program-specified, so that when the window is mapped again, it will be
6254 placed at the same location, without forcing the user to position it
6255 by hand again (they have already done that once for this window.) */
6256 x_wm_set_size_hint (f, (long) 0, 1);
6257
6258 HideWindow (FRAME_MAC_WINDOW (f));
6259
6260 UNBLOCK_INPUT;
6261
6262 #if !USE_CARBON_EVENTS
6263 mac_handle_visibility_change (f);
6264 #endif
6265 }
6266
6267 /* Change window state from mapped to iconified. */
6268
6269 void
6270 x_iconify_frame (f)
6271 struct frame *f;
6272 {
6273 OSErr err;
6274
6275 /* A deactivate event does not occur when the last visible frame is
6276 iconified. So if we clear the highlight here, it will not be
6277 rehighlighted when it is deiconified. */
6278 #if 0
6279 /* Don't keep the highlight on an invisible frame. */
6280 if (FRAME_MAC_DISPLAY_INFO (f)->x_highlight_frame == f)
6281 FRAME_MAC_DISPLAY_INFO (f)->x_highlight_frame = 0;
6282 #endif
6283
6284 if (f->async_iconified)
6285 return;
6286
6287 BLOCK_INPUT;
6288
6289 FRAME_SAMPLE_VISIBILITY (f);
6290
6291 if (! FRAME_VISIBLE_P (f))
6292 ShowWindow (FRAME_MAC_WINDOW (f));
6293
6294 err = CollapseWindow (FRAME_MAC_WINDOW (f), true);
6295
6296 UNBLOCK_INPUT;
6297
6298 if (err != noErr)
6299 error ("Can't notify window manager of iconification");
6300
6301 #if !USE_CARBON_EVENTS
6302 mac_handle_visibility_change (f);
6303 #endif
6304 }
6305
6306 \f
6307 /* Free X resources of frame F. */
6308
6309 void
6310 x_free_frame_resources (f)
6311 struct frame *f;
6312 {
6313 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
6314 WindowPtr wp = FRAME_MAC_WINDOW (f);
6315
6316 BLOCK_INPUT;
6317
6318 if (wp != tip_window)
6319 remove_window_handler (wp);
6320
6321 DisposeWindow (wp);
6322 if (wp == tip_window)
6323 /* Neither WaitNextEvent nor ReceiveNextEvent receives `window
6324 closed' event. So we reset tip_window here. */
6325 tip_window = NULL;
6326
6327 free_frame_menubar (f);
6328
6329 if (FRAME_FACE_CACHE (f))
6330 free_frame_faces (f);
6331
6332 x_free_gcs (f);
6333
6334 if (FRAME_SIZE_HINTS (f))
6335 xfree (FRAME_SIZE_HINTS (f));
6336
6337 #if TARGET_API_MAC_CARBON
6338 if (FRAME_FILE_NAME (f))
6339 xfree (FRAME_FILE_NAME (f));
6340 #endif
6341
6342 xfree (f->output_data.mac);
6343 f->output_data.mac = NULL;
6344
6345 if (f == dpyinfo->x_focus_frame)
6346 dpyinfo->x_focus_frame = 0;
6347 if (f == dpyinfo->x_focus_event_frame)
6348 dpyinfo->x_focus_event_frame = 0;
6349 if (f == dpyinfo->x_highlight_frame)
6350 dpyinfo->x_highlight_frame = 0;
6351
6352 if (f == dpyinfo->mouse_face_mouse_frame)
6353 {
6354 dpyinfo->mouse_face_beg_row
6355 = dpyinfo->mouse_face_beg_col = -1;
6356 dpyinfo->mouse_face_end_row
6357 = dpyinfo->mouse_face_end_col = -1;
6358 dpyinfo->mouse_face_window = Qnil;
6359 dpyinfo->mouse_face_deferred_gc = 0;
6360 dpyinfo->mouse_face_mouse_frame = 0;
6361 }
6362
6363 UNBLOCK_INPUT;
6364 }
6365
6366
6367 /* Destroy the X window of frame F. */
6368
6369 void
6370 x_destroy_window (f)
6371 struct frame *f;
6372 {
6373 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
6374
6375 x_free_frame_resources (f);
6376
6377 dpyinfo->reference_count--;
6378 }
6379
6380 \f
6381 /* Setting window manager hints. */
6382
6383 /* Set the normal size hints for the window manager, for frame F.
6384 FLAGS is the flags word to use--or 0 meaning preserve the flags
6385 that the window now has.
6386 If USER_POSITION is nonzero, we set the USPosition
6387 flag (this is useful when FLAGS is 0). */
6388 void
6389 x_wm_set_size_hint (f, flags, user_position)
6390 struct frame *f;
6391 long flags;
6392 int user_position;
6393 {
6394 int base_width, base_height, width_inc, height_inc;
6395 int min_rows = 0, min_cols = 0;
6396 XSizeHints *size_hints;
6397
6398 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
6399 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
6400 width_inc = FRAME_COLUMN_WIDTH (f);
6401 height_inc = FRAME_LINE_HEIGHT (f);
6402
6403 check_frame_size (f, &min_rows, &min_cols);
6404
6405 size_hints = FRAME_SIZE_HINTS (f);
6406 if (size_hints == NULL)
6407 {
6408 size_hints = FRAME_SIZE_HINTS (f) = xmalloc (sizeof (XSizeHints));
6409 bzero (size_hints, sizeof (XSizeHints));
6410 }
6411
6412 size_hints->flags |= PResizeInc | PMinSize | PBaseSize ;
6413 size_hints->width_inc = width_inc;
6414 size_hints->height_inc = height_inc;
6415 size_hints->min_width = base_width + min_cols * width_inc;
6416 size_hints->min_height = base_height + min_rows * height_inc;
6417 size_hints->base_width = base_width;
6418 size_hints->base_height = base_height;
6419
6420 if (flags)
6421 size_hints->flags = flags;
6422 else if (user_position)
6423 {
6424 size_hints->flags &= ~ PPosition;
6425 size_hints->flags |= USPosition;
6426 }
6427 }
6428
6429 #if 0 /* MAC_TODO: hide application instead of iconify? */
6430 /* Used for IconicState or NormalState */
6431
6432 void
6433 x_wm_set_window_state (f, state)
6434 struct frame *f;
6435 int state;
6436 {
6437 #ifdef USE_X_TOOLKIT
6438 Arg al[1];
6439
6440 XtSetArg (al[0], XtNinitialState, state);
6441 XtSetValues (f->output_data.x->widget, al, 1);
6442 #else /* not USE_X_TOOLKIT */
6443 Window window = FRAME_X_WINDOW (f);
6444
6445 f->output_data.x->wm_hints.flags |= StateHint;
6446 f->output_data.x->wm_hints.initial_state = state;
6447
6448 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
6449 #endif /* not USE_X_TOOLKIT */
6450 }
6451
6452 void
6453 x_wm_set_icon_pixmap (f, pixmap_id)
6454 struct frame *f;
6455 int pixmap_id;
6456 {
6457 Pixmap icon_pixmap;
6458
6459 #ifndef USE_X_TOOLKIT
6460 Window window = FRAME_X_WINDOW (f);
6461 #endif
6462
6463 if (pixmap_id > 0)
6464 {
6465 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
6466 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
6467 }
6468 else
6469 {
6470 /* It seems there is no way to turn off use of an icon pixmap.
6471 The following line does it, only if no icon has yet been created,
6472 for some window managers. But with mwm it crashes.
6473 Some people say it should clear the IconPixmapHint bit in this case,
6474 but that doesn't work, and the X consortium said it isn't the
6475 right thing at all. Since there is no way to win,
6476 best to explicitly give up. */
6477 #if 0
6478 f->output_data.x->wm_hints.icon_pixmap = None;
6479 #else
6480 return;
6481 #endif
6482 }
6483
6484 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
6485
6486 {
6487 Arg al[1];
6488 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
6489 XtSetValues (f->output_data.x->widget, al, 1);
6490 }
6491
6492 #else /* not USE_X_TOOLKIT */
6493
6494 f->output_data.x->wm_hints.flags |= IconPixmapHint;
6495 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
6496
6497 #endif /* not USE_X_TOOLKIT */
6498 }
6499
6500 #endif /* MAC_TODO */
6501
6502 void
6503 x_wm_set_icon_position (f, icon_x, icon_y)
6504 struct frame *f;
6505 int icon_x, icon_y;
6506 {
6507 #if 0 /* MAC_TODO: no icons on Mac */
6508 #ifdef USE_X_TOOLKIT
6509 Window window = XtWindow (f->output_data.x->widget);
6510 #else
6511 Window window = FRAME_X_WINDOW (f);
6512 #endif
6513
6514 f->output_data.x->wm_hints.flags |= IconPositionHint;
6515 f->output_data.x->wm_hints.icon_x = icon_x;
6516 f->output_data.x->wm_hints.icon_y = icon_y;
6517
6518 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
6519 #endif /* MAC_TODO */
6520 }
6521
6522 \f
6523 /***********************************************************************
6524 XLFD Pattern Match
6525 ***********************************************************************/
6526
6527 /* An XLFD pattern is divided into blocks delimited by '*'. This
6528 structure holds information for each block. */
6529 struct xlfdpat_block
6530 {
6531 /* Length of the pattern string in this block. Non-zero except for
6532 the first and the last blocks. */
6533 int len;
6534
6535 /* Pattern string except the last character in this block. The last
6536 character is replaced with NUL in order to use it as a
6537 sentinel. */
6538 unsigned char *pattern;
6539
6540 /* Last character of the pattern string. Must not be '?'. */
6541 unsigned char last_char;
6542
6543 /* One of the tables for the Boyer-Moore string search. It
6544 specifies the number of positions to proceed for each character
6545 with which the match fails. */
6546 int skip[256];
6547
6548 /* The skip value for the last character in the above `skip' is
6549 assigned to `infinity' in order to simplify a loop condition.
6550 The original value is saved here. */
6551 int last_char_skip;
6552 };
6553
6554 struct xlfdpat
6555 {
6556 /* Normalized pattern string. "Normalized" means that capital
6557 letters are lowered, blocks are not empty except the first and
6558 the last ones, and trailing '?'s in a block that is not the last
6559 one are moved to the next one. The last character in each block
6560 is replaced with NUL. */
6561 unsigned char *buf;
6562
6563 /* Number of characters except '*'s and trailing '?'s in the
6564 normalized pattern string. */
6565 int nchars;
6566
6567 /* Number of trailing '?'s in the normalized pattern string. */
6568 int trailing_anychars;
6569
6570 /* Number of blocks and information for each block. The latter is
6571 NULL if the pattern is exact (no '*' or '?' in it). */
6572 int nblocks;
6573 struct xlfdpat_block *blocks;
6574 };
6575
6576 static void
6577 xlfdpat_destroy (pat)
6578 struct xlfdpat *pat;
6579 {
6580 if (pat)
6581 {
6582 if (pat->buf)
6583 {
6584 if (pat->blocks)
6585 xfree (pat->blocks);
6586 xfree (pat->buf);
6587 }
6588 xfree (pat);
6589 }
6590 }
6591
6592 static struct xlfdpat *
6593 xlfdpat_create (pattern)
6594 char *pattern;
6595 {
6596 struct xlfdpat *pat;
6597 int nblocks, i, skip;
6598 unsigned char last_char, *p, *q, *anychar_head;
6599 struct xlfdpat_block *blk;
6600
6601 pat = xmalloc (sizeof (struct xlfdpat));
6602 pat->buf = xmalloc (strlen (pattern) + 1);
6603
6604 /* Normalize the pattern string and store it to `pat->buf'. */
6605 nblocks = 0;
6606 anychar_head = NULL;
6607 q = pat->buf;
6608 last_char = '\0';
6609 for (p = pattern; *p; p++)
6610 {
6611 unsigned char c = *p;
6612
6613 if (c == '*')
6614 if (last_char == '*')
6615 /* ...a** -> ...a* */
6616 continue;
6617 else
6618 {
6619 if (last_char == '?')
6620 {
6621 if (anychar_head > pat->buf && *(anychar_head - 1) == '*')
6622 /* ...*??* -> ...*?? */
6623 continue;
6624 else
6625 /* ...a??* -> ...a*?? */
6626 {
6627 *anychar_head++ = '*';
6628 c = '?';
6629 }
6630 }
6631 nblocks++;
6632 }
6633 else if (c == '?')
6634 {
6635 if (last_char != '?')
6636 anychar_head = q;
6637 }
6638 else
6639 /* On Mac OS X 10.3, tolower also converts non-ASCII
6640 characters for some locales. */
6641 if (isascii (c))
6642 c = tolower (c);
6643
6644 *q++ = last_char = c;
6645 }
6646 *q = '\0';
6647 nblocks++;
6648 pat->nblocks = nblocks;
6649 if (last_char != '?')
6650 pat->trailing_anychars = 0;
6651 else
6652 {
6653 pat->trailing_anychars = q - anychar_head;
6654 q = anychar_head;
6655 }
6656 pat->nchars = q - pat->buf - (nblocks - 1);
6657
6658 if (anychar_head == NULL && nblocks == 1)
6659 {
6660 /* The pattern is exact. */
6661 pat->blocks = NULL;
6662 return pat;
6663 }
6664
6665 pat->blocks = xmalloc (sizeof (struct xlfdpat_block) * nblocks);
6666
6667 /* Divide the normalized pattern into blocks. */
6668 p = pat->buf;
6669 for (blk = pat->blocks; blk < pat->blocks + nblocks - 1; blk++)
6670 {
6671 blk->pattern = p;
6672 while (*p != '*')
6673 p++;
6674 blk->len = p - blk->pattern;
6675 p++;
6676 }
6677 blk->pattern = p;
6678 blk->len = q - blk->pattern;
6679
6680 /* Setup a table for the Boyer-Moore string search. */
6681 for (blk = pat->blocks; blk < pat->blocks + nblocks; blk++)
6682 if (blk->len != 0)
6683 {
6684 blk->last_char = blk->pattern[blk->len - 1];
6685 blk->pattern[blk->len - 1] = '\0';
6686
6687 for (skip = 1; skip < blk->len; skip++)
6688 if (blk->pattern[blk->len - skip - 1] == '?')
6689 break;
6690
6691 for (i = 0; i < 256; i++)
6692 blk->skip[i] = skip;
6693
6694 p = blk->pattern + (blk->len - skip);
6695 while (--skip > 0)
6696 blk->skip[*p++] = skip;
6697
6698 blk->last_char_skip = blk->skip[blk->last_char];
6699 }
6700
6701 return pat;
6702 }
6703
6704 static INLINE int
6705 xlfdpat_exact_p (pat)
6706 struct xlfdpat *pat;
6707 {
6708 return pat->blocks == NULL;
6709 }
6710
6711 /* Return the first string in STRING + 0, ..., STRING + START_MAX such
6712 that the pattern in *BLK matches with its prefix. Return NULL
6713 there is no such strings. STRING must be lowered in advance. */
6714
6715 static char *
6716 xlfdpat_block_match_1 (blk, string, start_max)
6717 struct xlfdpat_block *blk;
6718 unsigned char *string;
6719 int start_max;
6720 {
6721 int start, infinity;
6722 unsigned char *p, *s;
6723
6724 xassert (blk->len > 0);
6725 xassert (start_max + blk->len <= strlen (string));
6726 xassert (blk->last_char != '?');
6727
6728 /* See the comments in the function `boyer_moore' (search.c) for the
6729 use of `infinity'. */
6730 infinity = start_max + blk->len + 1;
6731 blk->skip[blk->last_char] = infinity;
6732
6733 start = 0;
6734 do
6735 {
6736 /* Check the last character of the pattern. */
6737 s = string + blk->len - 1;
6738 do
6739 {
6740 start += blk->skip[*(s + start)];
6741 }
6742 while (start <= start_max);
6743
6744 if (start < infinity)
6745 /* Couldn't find the last character. */
6746 return NULL;
6747
6748 /* No less than `infinity' means we could find the last
6749 character at `s[start - infinity]'. */
6750 start -= infinity;
6751
6752 /* Check the remaining characters. We prefer making no-'?'
6753 cases faster because the use of '?' is really rare. */
6754 p = blk->pattern;
6755 s = string + start;
6756 do
6757 {
6758 while (*p++ == *s++)
6759 ;
6760 }
6761 while (*(p - 1) == '?');
6762
6763 if (*(p - 1) == '\0')
6764 /* Matched. */
6765 return string + start;
6766
6767 /* Didn't match. */
6768 start += blk->last_char_skip;
6769 }
6770 while (start <= start_max);
6771
6772 return NULL;
6773 }
6774
6775 #define xlfdpat_block_match(b, s, m) \
6776 ((b)->len == 1 ? memchr ((s), (b)->last_char, (m) + 1) \
6777 : xlfdpat_block_match_1 (b, s, m))
6778
6779 /* Check if XLFD pattern PAT, which is generated by `xfldpat_create',
6780 matches with STRING. STRING must be lowered in advance. */
6781
6782 static int
6783 xlfdpat_match (pat, string)
6784 struct xlfdpat *pat;
6785 unsigned char *string;
6786 {
6787 int str_len, nblocks, i, start_max;
6788 struct xlfdpat_block *blk;
6789 unsigned char *s;
6790
6791 xassert (pat->nblocks > 0);
6792
6793 if (xlfdpat_exact_p (pat))
6794 return strcmp (pat->buf, string) == 0;
6795
6796 /* The number of the characters in the string must not be smaller
6797 than that in the pattern. */
6798 str_len = strlen (string);
6799 if (str_len < pat->nchars + pat->trailing_anychars)
6800 return 0;
6801
6802 /* Chop off the trailing '?'s. */
6803 str_len -= pat->trailing_anychars;
6804
6805 /* The last block. When it is non-empty, it must match at the end
6806 of the string. */
6807 nblocks = pat->nblocks;
6808 blk = pat->blocks + (nblocks - 1);
6809 if (nblocks == 1)
6810 /* The last block is also the first one. */
6811 return (str_len == blk->len
6812 && (blk->len == 0 || xlfdpat_block_match (blk, string, 0)));
6813 else if (blk->len != 0)
6814 if (!xlfdpat_block_match (blk, string + (str_len - blk->len), 0))
6815 return 0;
6816
6817 /* The first block. When it is non-empty, it must match at the
6818 beginning of the string. */
6819 blk = pat->blocks;
6820 if (blk->len != 0)
6821 {
6822 s = xlfdpat_block_match (blk, string, 0);
6823 if (s == NULL)
6824 return 0;
6825 string = s + blk->len;
6826 }
6827
6828 /* The rest of the blocks. */
6829 start_max = str_len - pat->nchars;
6830 for (i = 1, blk++; i < nblocks - 1; i++, blk++)
6831 {
6832 s = xlfdpat_block_match (blk, string, start_max);
6833 if (s == NULL)
6834 return 0;
6835 start_max -= s - string;
6836 string = s + blk->len;
6837 }
6838
6839 return 1;
6840 }
6841
6842 \f
6843 /***********************************************************************
6844 Fonts
6845 ***********************************************************************/
6846
6847 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
6848
6849 struct font_info *
6850 x_get_font_info (f, font_idx)
6851 FRAME_PTR f;
6852 int font_idx;
6853 {
6854 return (FRAME_MAC_FONT_TABLE (f) + font_idx);
6855 }
6856
6857 /* the global font name table */
6858 static char **font_name_table = NULL;
6859 static int font_name_table_size = 0;
6860 static int font_name_count = 0;
6861
6862 /* Alist linking font family names to Font Manager font family
6863 references (which can also be used as QuickDraw font IDs). We use
6864 an alist because hash tables are not ready when the terminal frame
6865 for Mac OS Classic is created. */
6866 static Lisp_Object fm_font_family_alist;
6867 #if USE_ATSUI
6868 /* Hash table linking font family names to ATSU font IDs. */
6869 static Lisp_Object atsu_font_id_hash;
6870 #endif
6871
6872 /* Alist linking character set strings to Mac text encoding and Emacs
6873 coding system. */
6874 static Lisp_Object Vmac_charset_info_alist;
6875
6876 static Lisp_Object
6877 create_text_encoding_info_alist ()
6878 {
6879 Lisp_Object result = Qnil, rest;
6880
6881 for (rest = Vmac_charset_info_alist; CONSP (rest); rest = XCDR (rest))
6882 {
6883 Lisp_Object charset_info = XCAR (rest);
6884 Lisp_Object charset, coding_system, text_encoding;
6885 Lisp_Object existing_info;
6886
6887 if (!(CONSP (charset_info)
6888 && STRINGP (charset = XCAR (charset_info))
6889 && CONSP (XCDR (charset_info))
6890 && INTEGERP (text_encoding = XCAR (XCDR (charset_info)))
6891 && CONSP (XCDR (XCDR (charset_info)))
6892 && SYMBOLP (coding_system = XCAR (XCDR (XCDR (charset_info))))))
6893 continue;
6894
6895 existing_info = assq_no_quit (text_encoding, result);
6896 if (NILP (existing_info))
6897 result = Fcons (list3 (text_encoding, coding_system, charset),
6898 result);
6899 else
6900 if (NILP (Fmember (charset, XCDR (XCDR (existing_info)))))
6901 XSETCDR (XCDR (existing_info),
6902 Fcons (charset, XCDR (XCDR (existing_info))));
6903 }
6904
6905 return result;
6906 }
6907
6908
6909 static void
6910 decode_mac_font_name (name, size, coding_system)
6911 char *name;
6912 int size;
6913 Lisp_Object coding_system;
6914 {
6915 struct coding_system coding;
6916 char *buf, *p;
6917
6918 if (!NILP (coding_system) && !NILP (Fcoding_system_p (coding_system)))
6919 {
6920 for (p = name; *p; p++)
6921 if (!isascii (*p) || iscntrl (*p))
6922 break;
6923
6924 if (*p)
6925 {
6926 setup_coding_system (coding_system, &coding);
6927 coding.src_multibyte = 0;
6928 coding.dst_multibyte = 1;
6929 coding.mode |= CODING_MODE_LAST_BLOCK;
6930 coding.composing = COMPOSITION_DISABLED;
6931 buf = (char *) alloca (size);
6932
6933 decode_coding (&coding, name, buf, strlen (name), size - 1);
6934 bcopy (buf, name, coding.produced);
6935 name[coding.produced] = '\0';
6936 }
6937 }
6938
6939 /* If there's just one occurrence of '-' in the family name, it is
6940 replaced with '_'. (More than one occurrence of '-' means a
6941 "FOUNDRY-FAMILY-CHARSET"-style name.) */
6942 p = strchr (name, '-');
6943 if (p && strchr (p + 1, '-') == NULL)
6944 *p = '_';
6945
6946 for (p = name; *p; p++)
6947 /* On Mac OS X 10.3, tolower also converts non-ASCII characters
6948 for some locales. */
6949 if (isascii (*p))
6950 *p = tolower (*p);
6951 }
6952
6953
6954 static char *
6955 mac_to_x_fontname (name, size, style, charset)
6956 char *name;
6957 int size;
6958 Style style;
6959 char *charset;
6960 {
6961 Str31 foundry, cs;
6962 Str255 family;
6963 char xf[256], *result;
6964 unsigned char *p;
6965
6966 if (sscanf (name, "%31[^-]-%255[^-]-%31s", foundry, family, cs) == 3)
6967 charset = cs;
6968 else
6969 {
6970 strcpy(foundry, "Apple");
6971 strcpy(family, name);
6972 }
6973
6974 sprintf (xf, "%s-%c-normal--%d-%d-%d-%d-m-%d-%s",
6975 style & bold ? "bold" : "medium", style & italic ? 'i' : 'r',
6976 size, size * 10, size ? 72 : 0, size ? 72 : 0, size * 10, charset);
6977
6978 result = xmalloc (strlen (foundry) + strlen (family) + strlen (xf) + 3 + 1);
6979 sprintf (result, "-%s-%s-%s", foundry, family, xf);
6980 for (p = result; *p; p++)
6981 /* On Mac OS X 10.3, tolower also converts non-ASCII characters
6982 for some locales. */
6983 if (isascii (*p))
6984 *p = tolower (*p);
6985 return result;
6986 }
6987
6988
6989 /* Parse fully-specified and instantiated X11 font spec XF, and store
6990 the results to FAMILY, *SIZE, *STYLE, and CHARSET. Return 1 if the
6991 parsing succeeded, and 0 otherwise. For FAMILY and CHARSET, the
6992 caller must allocate at least 256 and 32 bytes respectively. For
6993 ordinary Mac fonts, the value stored to FAMILY should just be their
6994 names, like "monaco", "Taipei", etc. Fonts converted from the GNU
6995 intlfonts collection contain their charset designation in their
6996 names, like "ETL-Fixed-iso8859-1", "ETL-Fixed-koi8-r", etc. Both
6997 types of font names are handled accordingly. */
6998
6999 const int kDefaultFontSize = 12;
7000
7001 static int
7002 parse_x_font_name (xf, family, size, style, charset)
7003 char *xf, *family;
7004 int *size;
7005 Style *style;
7006 char *charset;
7007 {
7008 Str31 foundry, weight;
7009 int point_size, avgwidth;
7010 char slant[2], *p;
7011
7012 if (sscanf (xf, "-%31[^-]-%255[^-]-%31[^-]-%1[^-]-%*[^-]-%*[^-]-%d-%d-%*[^-]-%*[^-]-%*c-%d-%31s",
7013 foundry, family, weight, slant, size,
7014 &point_size, &avgwidth, charset) != 8
7015 && sscanf (xf, "-%31[^-]-%255[^-]-%31[^-]-%1[^-]-%*[^-]--%d-%d-%*[^-]-%*[^-]-%*c-%d-%31s",
7016 foundry, family, weight, slant, size,
7017 &point_size, &avgwidth, charset) != 8)
7018 return 0;
7019
7020 if (*size == 0)
7021 {
7022 if (point_size > 0)
7023 *size = point_size / 10;
7024 else if (avgwidth > 0)
7025 *size = avgwidth / 10;
7026 }
7027 if (*size == 0)
7028 *size = kDefaultFontSize;
7029
7030 *style = normal;
7031 if (strcmp (weight, "bold") == 0)
7032 *style |= bold;
7033 if (*slant == 'i')
7034 *style |= italic;
7035
7036 if (NILP (Fassoc (build_string (charset), Vmac_charset_info_alist)))
7037 {
7038 int foundry_len = strlen (foundry), family_len = strlen (family);
7039
7040 if (foundry_len + family_len + strlen (charset) + 2 < sizeof (Str255))
7041 {
7042 /* Like sprintf (family, "%s-%s-%s", foundry, family, charset),
7043 but take overlap into account. */
7044 memmove (family + foundry_len + 1, family, family_len);
7045 memcpy (family, foundry, foundry_len);
7046 family[foundry_len] = '-';
7047 family[foundry_len + 1 + family_len] = '-';
7048 strcpy (family + foundry_len + 1 + family_len + 1, charset);
7049 }
7050 else
7051 return 0;
7052 }
7053
7054 for (p = family; *p; p++)
7055 /* On Mac OS X 10.3, tolower also converts non-ASCII characters
7056 for some locales. */
7057 if (isascii (*p))
7058 *p = tolower (*p);
7059
7060 return 1;
7061 }
7062
7063
7064 static void
7065 add_font_name_table_entry (char *font_name)
7066 {
7067 if (font_name_table_size == 0)
7068 {
7069 font_name_table_size = 256;
7070 font_name_table = (char **)
7071 xmalloc (font_name_table_size * sizeof (char *));
7072 }
7073 else if (font_name_count + 1 >= font_name_table_size)
7074 {
7075 font_name_table_size *= 2;
7076 font_name_table = (char **)
7077 xrealloc (font_name_table,
7078 font_name_table_size * sizeof (char *));
7079 }
7080
7081 font_name_table[font_name_count++] = font_name;
7082 }
7083
7084 static void
7085 add_mac_font_name (name, size, style, charset)
7086 char *name;
7087 int size;
7088 Style style;
7089 char *charset;
7090 {
7091 if (size > 0)
7092 add_font_name_table_entry (mac_to_x_fontname (name, size, style, charset));
7093 else
7094 {
7095 add_font_name_table_entry (mac_to_x_fontname (name, 0, style, charset));
7096 add_font_name_table_entry (mac_to_x_fontname (name, 0, italic, charset));
7097 add_font_name_table_entry (mac_to_x_fontname (name, 0, bold, charset));
7098 add_font_name_table_entry (mac_to_x_fontname (name, 0, italic | bold,
7099 charset));
7100 }
7101 }
7102
7103 /* Sets up the table font_name_table to contain the list of all fonts
7104 in the system the first time the table is used so that the Resource
7105 Manager need not be accessed every time this information is
7106 needed. */
7107
7108 static void
7109 init_font_name_table ()
7110 {
7111 #if TARGET_API_MAC_CARBON
7112 FMFontFamilyIterator ffi;
7113 FMFontFamilyInstanceIterator ffii;
7114 FMFontFamily ff;
7115 Lisp_Object text_encoding_info_alist;
7116 struct gcpro gcpro1;
7117
7118 text_encoding_info_alist = create_text_encoding_info_alist ();
7119
7120 #if USE_ATSUI
7121 #if USE_CG_TEXT_DRAWING
7122 init_cg_text_anti_aliasing_threshold ();
7123 #endif
7124 if (!NILP (assq_no_quit (make_number (kTextEncodingMacUnicode),
7125 text_encoding_info_alist)))
7126 {
7127 OSErr err;
7128 struct Lisp_Hash_Table *h;
7129 unsigned hash_code;
7130 ItemCount nfonts, i;
7131 ATSUFontID *font_ids = NULL;
7132 Ptr name;
7133 ByteCount name_len;
7134 Lisp_Object family;
7135
7136 atsu_font_id_hash =
7137 make_hash_table (Qequal, make_number (DEFAULT_HASH_SIZE),
7138 make_float (DEFAULT_REHASH_SIZE),
7139 make_float (DEFAULT_REHASH_THRESHOLD),
7140 Qnil, Qnil, Qnil);;
7141 h = XHASH_TABLE (atsu_font_id_hash);
7142
7143 err = ATSUFontCount (&nfonts);
7144 if (err == noErr)
7145 {
7146 font_ids = xmalloc (sizeof (ATSUFontID) * nfonts);
7147 err = ATSUGetFontIDs (font_ids, nfonts, NULL);
7148 }
7149 if (err == noErr)
7150 for (i = 0; i < nfonts; i++)
7151 {
7152 err = ATSUFindFontName (font_ids[i], kFontFamilyName,
7153 kFontMacintoshPlatform, kFontNoScript,
7154 kFontNoLanguage, 0, NULL, &name_len, NULL);
7155 if (err != noErr)
7156 continue;
7157 name = xmalloc (name_len + 1);
7158 name[name_len] = '\0';
7159 err = ATSUFindFontName (font_ids[i], kFontFamilyName,
7160 kFontMacintoshPlatform, kFontNoScript,
7161 kFontNoLanguage, name_len, name,
7162 NULL, NULL);
7163 if (err == noErr)
7164 {
7165 decode_mac_font_name (name, name_len + 1, Qnil);
7166 family = make_unibyte_string (name, name_len);
7167 if (*name != '.'
7168 && hash_lookup (h, family, &hash_code) < 0)
7169 {
7170 add_mac_font_name (name, 0, normal, "iso10646-1");
7171 hash_put (h, family, long_to_cons (font_ids[i]),
7172 hash_code);
7173 }
7174 }
7175 xfree (name);
7176 }
7177 if (font_ids)
7178 xfree (font_ids);
7179 }
7180 #endif
7181
7182 /* Create a dummy instance iterator here to avoid creating and
7183 destroying it in the loop. */
7184 if (FMCreateFontFamilyInstanceIterator (0, &ffii) != noErr)
7185 return;
7186 /* Create an iterator to enumerate the font families. */
7187 if (FMCreateFontFamilyIterator (NULL, NULL, kFMDefaultOptions, &ffi)
7188 != noErr)
7189 {
7190 FMDisposeFontFamilyInstanceIterator (&ffii);
7191 return;
7192 }
7193
7194 GCPRO1 (text_encoding_info_alist);
7195
7196 while (FMGetNextFontFamily (&ffi, &ff) == noErr)
7197 {
7198 Str255 name;
7199 FMFont font;
7200 FMFontStyle style;
7201 FMFontSize size;
7202 TextEncoding encoding;
7203 TextEncodingBase sc;
7204 Lisp_Object text_encoding_info, family;
7205
7206 if (FMGetFontFamilyName (ff, name) != noErr)
7207 continue;
7208 p2cstr (name);
7209 if (*name == '.')
7210 continue;
7211
7212 if (FMGetFontFamilyTextEncoding (ff, &encoding) != noErr)
7213 continue;
7214 sc = GetTextEncodingBase (encoding);
7215 text_encoding_info = assq_no_quit (make_number (sc),
7216 text_encoding_info_alist);
7217 if (NILP (text_encoding_info))
7218 text_encoding_info = assq_no_quit (make_number (kTextEncodingMacRoman),
7219 text_encoding_info_alist);
7220 decode_mac_font_name (name, sizeof (name),
7221 XCAR (XCDR (text_encoding_info)));
7222 family = build_string (name);
7223 if (!NILP (Fassoc (family, fm_font_family_alist)))
7224 continue;
7225 fm_font_family_alist = Fcons (Fcons (family, make_number (ff)),
7226 fm_font_family_alist);
7227
7228 /* Point the instance iterator at the current font family. */
7229 if (FMResetFontFamilyInstanceIterator (ff, &ffii) != noErr)
7230 continue;
7231
7232 while (FMGetNextFontFamilyInstance (&ffii, &font, &style, &size)
7233 == noErr)
7234 {
7235 Lisp_Object rest = XCDR (XCDR (text_encoding_info));
7236
7237 if (size > 0 || style == normal)
7238 for (; !NILP (rest); rest = XCDR (rest))
7239 add_mac_font_name (name, size, style, SDATA (XCAR (rest)));
7240 }
7241 }
7242
7243 UNGCPRO;
7244
7245 /* Dispose of the iterators. */
7246 FMDisposeFontFamilyIterator (&ffi);
7247 FMDisposeFontFamilyInstanceIterator (&ffii);
7248 #else /* !TARGET_API_MAC_CARBON */
7249 GrafPtr port;
7250 SInt16 fontnum, old_fontnum;
7251 int num_mac_fonts = CountResources('FOND');
7252 int i, j;
7253 Handle font_handle, font_handle_2;
7254 short id, scriptcode;
7255 ResType type;
7256 Str255 name;
7257 struct FontAssoc *fat;
7258 struct AsscEntry *assc_entry;
7259 Lisp_Object text_encoding_info_alist, text_encoding_info, family;
7260 struct gcpro gcpro1;
7261
7262 GetPort (&port); /* save the current font number used */
7263 old_fontnum = port->txFont;
7264
7265 text_encoding_info_alist = create_text_encoding_info_alist ();
7266
7267 GCPRO1 (text_encoding_info_alist);
7268
7269 for (i = 1; i <= num_mac_fonts; i++) /* get all available fonts */
7270 {
7271 font_handle = GetIndResource ('FOND', i);
7272 if (!font_handle)
7273 continue;
7274
7275 GetResInfo (font_handle, &id, &type, name);
7276 GetFNum (name, &fontnum);
7277 p2cstr (name);
7278 if (fontnum == 0 || *name == '.')
7279 continue;
7280
7281 TextFont (fontnum);
7282 scriptcode = FontToScript (fontnum);
7283 text_encoding_info = assq_no_quit (make_number (scriptcode),
7284 text_encoding_info_alist);
7285 if (NILP (text_encoding_info))
7286 text_encoding_info = assq_no_quit (make_number (smRoman),
7287 text_encoding_info_alist);
7288 decode_mac_font_name (name, sizeof (name),
7289 XCAR (XCDR (text_encoding_info)));
7290 family = build_string (name);
7291 if (!NILP (Fassoc (family, fm_font_family_alist)))
7292 continue;
7293 fm_font_family_alist = Fcons (Fcons (family, make_number (fontnum)),
7294 fm_font_family_alist);
7295 do
7296 {
7297 HLock (font_handle);
7298
7299 if (GetResourceSizeOnDisk (font_handle)
7300 >= sizeof (struct FamRec))
7301 {
7302 fat = (struct FontAssoc *) (*font_handle
7303 + sizeof (struct FamRec));
7304 assc_entry
7305 = (struct AsscEntry *) (*font_handle
7306 + sizeof (struct FamRec)
7307 + sizeof (struct FontAssoc));
7308
7309 for (j = 0; j <= fat->numAssoc; j++, assc_entry++)
7310 {
7311 Lisp_Object rest = XCDR (XCDR (text_encoding_info));
7312
7313 for (; !NILP (rest); rest = XCDR (rest))
7314 add_mac_font_name (name, assc_entry->fontSize,
7315 assc_entry->fontStyle,
7316 SDATA (XCAR (rest)));
7317 }
7318 }
7319
7320 HUnlock (font_handle);
7321 font_handle_2 = GetNextFOND (font_handle);
7322 ReleaseResource (font_handle);
7323 font_handle = font_handle_2;
7324 }
7325 while (ResError () == noErr && font_handle);
7326 }
7327
7328 UNGCPRO;
7329
7330 TextFont (old_fontnum);
7331 #endif /* !TARGET_API_MAC_CARBON */
7332 }
7333
7334
7335 void
7336 mac_clear_font_name_table ()
7337 {
7338 int i;
7339
7340 for (i = 0; i < font_name_count; i++)
7341 xfree (font_name_table[i]);
7342 xfree (font_name_table);
7343 font_name_table = NULL;
7344 font_name_table_size = font_name_count = 0;
7345 fm_font_family_alist = Qnil;
7346 }
7347
7348
7349 enum xlfd_scalable_field_index
7350 {
7351 XLFD_SCL_PIXEL_SIZE,
7352 XLFD_SCL_POINT_SIZE,
7353 XLFD_SCL_AVGWIDTH,
7354 XLFD_SCL_LAST
7355 };
7356
7357 static int xlfd_scalable_fields[] =
7358 {
7359 6, /* PIXEL_SIZE */
7360 7, /* POINT_SIZE */
7361 11, /* AVGWIDTH */
7362 -1
7363 };
7364
7365 static Lisp_Object
7366 mac_do_list_fonts (pattern, maxnames)
7367 char *pattern;
7368 int maxnames;
7369 {
7370 int i, n_fonts = 0;
7371 Lisp_Object font_list = Qnil;
7372 struct xlfdpat *pat;
7373 char *scaled, *ptr;
7374 int scl_val[XLFD_SCL_LAST], *field, *val;
7375 int exact;
7376
7377 if (font_name_table == NULL) /* Initialize when first used. */
7378 init_font_name_table ();
7379
7380 for (i = 0; i < XLFD_SCL_LAST; i++)
7381 scl_val[i] = -1;
7382
7383 /* If the pattern contains 14 dashes and one of PIXEL_SIZE,
7384 POINT_SIZE, and AVGWIDTH fields is explicitly specified, scalable
7385 fonts are scaled according to the specified size. */
7386 ptr = pattern;
7387 i = 0;
7388 field = xlfd_scalable_fields;
7389 val = scl_val;
7390 if (*ptr == '-')
7391 do
7392 {
7393 ptr++;
7394 if (i == *field)
7395 {
7396 if ('0' <= *ptr && *ptr <= '9')
7397 {
7398 *val = *ptr++ - '0';
7399 while ('0' <= *ptr && *ptr <= '9' && *val < 10000)
7400 *val = *val * 10 + *ptr++ - '0';
7401 if (*ptr != '-')
7402 *val = -1;
7403 }
7404 field++;
7405 val++;
7406 }
7407 ptr = strchr (ptr, '-');
7408 i++;
7409 }
7410 while (ptr && i < 14);
7411
7412 if (i == 14 && ptr == NULL)
7413 {
7414 if (scl_val[XLFD_SCL_PIXEL_SIZE] < 0)
7415 scl_val[XLFD_SCL_PIXEL_SIZE] =
7416 (scl_val[XLFD_SCL_POINT_SIZE] > 0 ? scl_val[XLFD_SCL_POINT_SIZE] / 10
7417 : (scl_val[XLFD_SCL_AVGWIDTH] > 0 ? scl_val[XLFD_SCL_AVGWIDTH] / 10
7418 : -1));
7419 if (scl_val[XLFD_SCL_POINT_SIZE] < 0)
7420 scl_val[XLFD_SCL_POINT_SIZE] =
7421 (scl_val[XLFD_SCL_PIXEL_SIZE] > 0 ? scl_val[XLFD_SCL_PIXEL_SIZE] * 10
7422 : (scl_val[XLFD_SCL_AVGWIDTH] > 0 ? scl_val[XLFD_SCL_AVGWIDTH]
7423 : -1));
7424 if (scl_val[XLFD_SCL_AVGWIDTH] < 0)
7425 scl_val[XLFD_SCL_AVGWIDTH] =
7426 (scl_val[XLFD_SCL_PIXEL_SIZE] > 0 ? scl_val[XLFD_SCL_PIXEL_SIZE] * 10
7427 : (scl_val[XLFD_SCL_POINT_SIZE] > 0 ? scl_val[XLFD_SCL_POINT_SIZE]
7428 : -1));
7429 }
7430 else
7431 scl_val[XLFD_SCL_PIXEL_SIZE] = -1;
7432
7433 pat = xlfdpat_create (pattern);
7434 if (pat == NULL)
7435 return Qnil;
7436
7437 exact = xlfdpat_exact_p (pat);
7438
7439 for (i = 0; i < font_name_count; i++)
7440 {
7441 if (xlfdpat_match (pat, font_name_table[i]))
7442 {
7443 font_list = Fcons (build_string (font_name_table[i]), font_list);
7444 if (exact || (maxnames > 0 && ++n_fonts >= maxnames))
7445 break;
7446 }
7447 else if (scl_val[XLFD_SCL_PIXEL_SIZE] > 0
7448 && (ptr = strstr (font_name_table[i], "-0-0-0-0-m-0-")))
7449 {
7450 int former_len = ptr - font_name_table[i];
7451
7452 scaled = xmalloc (strlen (font_name_table[i]) + 20 + 1);
7453 memcpy (scaled, font_name_table[i], former_len);
7454 sprintf (scaled + former_len,
7455 "-%d-%d-72-72-m-%d-%s",
7456 scl_val[XLFD_SCL_PIXEL_SIZE],
7457 scl_val[XLFD_SCL_POINT_SIZE],
7458 scl_val[XLFD_SCL_AVGWIDTH],
7459 ptr + sizeof ("-0-0-0-0-m-0-") - 1);
7460
7461 if (xlfdpat_match (pat, scaled))
7462 {
7463 font_list = Fcons (build_string (scaled), font_list);
7464 xfree (scaled);
7465 if (exact || (maxnames > 0 && ++n_fonts >= maxnames))
7466 break;
7467 }
7468 else
7469 xfree (scaled);
7470 }
7471 }
7472
7473 xlfdpat_destroy (pat);
7474
7475 return font_list;
7476 }
7477
7478 /* Return a list of names of available fonts matching PATTERN on frame F.
7479
7480 Frame F null means we have not yet created any frame on Mac, and
7481 consult the first display in x_display_list. MAXNAMES sets a limit
7482 on how many fonts to match. */
7483
7484 Lisp_Object
7485 x_list_fonts (f, pattern, size, maxnames)
7486 struct frame *f;
7487 Lisp_Object pattern;
7488 int size, maxnames;
7489 {
7490 Lisp_Object list = Qnil, patterns, tem, key;
7491 struct mac_display_info *dpyinfo
7492 = f ? FRAME_MAC_DISPLAY_INFO (f) : x_display_list;
7493
7494 xassert (size <= 0);
7495
7496 patterns = Fassoc (pattern, Valternate_fontname_alist);
7497 if (NILP (patterns))
7498 patterns = Fcons (pattern, Qnil);
7499
7500 for (; CONSP (patterns); patterns = XCDR (patterns))
7501 {
7502 pattern = XCAR (patterns);
7503
7504 if (!STRINGP (pattern))
7505 continue;
7506
7507 tem = XCAR (XCDR (dpyinfo->name_list_element));
7508 key = Fcons (pattern, make_number (maxnames));
7509
7510 list = Fassoc (key, tem);
7511 if (!NILP (list))
7512 {
7513 list = Fcdr_safe (list);
7514 /* We have a cashed list. Don't have to get the list again. */
7515 goto label_cached;
7516 }
7517
7518 BLOCK_INPUT;
7519 list = mac_do_list_fonts (SDATA (pattern), maxnames);
7520 UNBLOCK_INPUT;
7521
7522 /* MAC_TODO: add code for matching outline fonts here */
7523
7524 /* Now store the result in the cache. */
7525 XSETCAR (XCDR (dpyinfo->name_list_element),
7526 Fcons (Fcons (key, list),
7527 XCAR (XCDR (dpyinfo->name_list_element))));
7528
7529 label_cached:
7530 if (NILP (list)) continue; /* Try the remaining alternatives. */
7531 }
7532
7533 return list;
7534 }
7535
7536
7537 #if GLYPH_DEBUG
7538
7539 /* Check that FONT is valid on frame F. It is if it can be found in F's
7540 font table. */
7541
7542 static void
7543 x_check_font (f, font)
7544 struct frame *f;
7545 XFontStruct *font;
7546 {
7547 int i;
7548 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7549
7550 xassert (font != NULL);
7551
7552 for (i = 0; i < dpyinfo->n_fonts; i++)
7553 if (dpyinfo->font_table[i].name
7554 && font == dpyinfo->font_table[i].font)
7555 break;
7556
7557 xassert (i < dpyinfo->n_fonts);
7558 }
7559
7560 #endif /* GLYPH_DEBUG != 0 */
7561
7562 /* Set *W to the minimum width, *H to the minimum font height of FONT.
7563 Note: There are (broken) X fonts out there with invalid XFontStruct
7564 min_bounds contents. For example, handa@etl.go.jp reports that
7565 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
7566 have font->min_bounds.width == 0. */
7567
7568 static INLINE void
7569 x_font_min_bounds (font, w, h)
7570 MacFontStruct *font;
7571 int *w, *h;
7572 {
7573 *h = FONT_HEIGHT (font);
7574 *w = font->min_bounds.width;
7575 }
7576
7577
7578 /* Compute the smallest character width and smallest font height over
7579 all fonts available on frame F. Set the members smallest_char_width
7580 and smallest_font_height in F's x_display_info structure to
7581 the values computed. Value is non-zero if smallest_font_height or
7582 smallest_char_width become smaller than they were before. */
7583
7584 static int
7585 x_compute_min_glyph_bounds (f)
7586 struct frame *f;
7587 {
7588 int i;
7589 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
7590 MacFontStruct *font;
7591 int old_width = dpyinfo->smallest_char_width;
7592 int old_height = dpyinfo->smallest_font_height;
7593
7594 dpyinfo->smallest_font_height = 100000;
7595 dpyinfo->smallest_char_width = 100000;
7596
7597 for (i = 0; i < dpyinfo->n_fonts; ++i)
7598 if (dpyinfo->font_table[i].name)
7599 {
7600 struct font_info *fontp = dpyinfo->font_table + i;
7601 int w, h;
7602
7603 font = (MacFontStruct *) fontp->font;
7604 xassert (font != (MacFontStruct *) ~0);
7605 x_font_min_bounds (font, &w, &h);
7606
7607 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
7608 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
7609 }
7610
7611 xassert (dpyinfo->smallest_char_width > 0
7612 && dpyinfo->smallest_font_height > 0);
7613
7614 return (dpyinfo->n_fonts == 1
7615 || dpyinfo->smallest_char_width < old_width
7616 || dpyinfo->smallest_font_height < old_height);
7617 }
7618
7619
7620 /* Determine whether given string is a fully-specified XLFD: all 14
7621 fields are present, none is '*'. */
7622
7623 static int
7624 is_fully_specified_xlfd (char *p)
7625 {
7626 int i;
7627 char *q;
7628
7629 if (*p != '-')
7630 return 0;
7631
7632 for (i = 0; i < 13; i++)
7633 {
7634 q = strchr (p + 1, '-');
7635 if (q == NULL)
7636 return 0;
7637 if (q - p == 2 && *(p + 1) == '*')
7638 return 0;
7639 p = q;
7640 }
7641
7642 if (strchr (p + 1, '-') != NULL)
7643 return 0;
7644
7645 if (*(p + 1) == '*' && *(p + 2) == '\0')
7646 return 0;
7647
7648 return 1;
7649 }
7650
7651
7652 /* XLoadQueryFont creates and returns an internal representation for a
7653 font in a MacFontStruct struct. There is really no concept
7654 corresponding to "loading" a font on the Mac. But we check its
7655 existence and find the font number and all other information for it
7656 and store them in the returned MacFontStruct. */
7657
7658 static MacFontStruct *
7659 XLoadQueryFont (Display *dpy, char *fontname)
7660 {
7661 int size;
7662 char *name;
7663 Str255 family;
7664 Str31 charset;
7665 SInt16 fontnum;
7666 #if USE_ATSUI
7667 static ATSUFontID font_id;
7668 ATSUStyle mac_style = NULL;
7669 #endif
7670 Style fontface;
7671 #if TARGET_API_MAC_CARBON
7672 TextEncoding encoding;
7673 int scriptcode;
7674 #else
7675 short scriptcode;
7676 #endif
7677 MacFontStruct *font;
7678 XCharStruct *space_bounds = NULL, *pcm;
7679
7680 if (is_fully_specified_xlfd (fontname))
7681 name = fontname;
7682 else
7683 {
7684 Lisp_Object matched_fonts;
7685
7686 matched_fonts = mac_do_list_fonts (fontname, 1);
7687 if (NILP (matched_fonts))
7688 return NULL;
7689 name = SDATA (XCAR (matched_fonts));
7690 }
7691
7692 if (parse_x_font_name (name, family, &size, &fontface, charset) == 0)
7693 return NULL;
7694
7695 #if USE_ATSUI
7696 if (strcmp (charset, "iso10646-1") == 0) /* XXX */
7697 {
7698 OSErr err;
7699 ATSUAttributeTag tags[] = {kATSUFontTag, kATSUSizeTag,
7700 kATSUQDBoldfaceTag, kATSUQDItalicTag};
7701 ByteCount sizes[] = {sizeof (ATSUFontID), sizeof (Fixed),
7702 sizeof (Boolean), sizeof (Boolean)};
7703 static Fixed size_fixed;
7704 static Boolean bold_p, italic_p;
7705 ATSUAttributeValuePtr values[] = {&font_id, &size_fixed,
7706 &bold_p, &italic_p};
7707 ATSUFontFeatureType types[] = {kAllTypographicFeaturesType,
7708 kDiacriticsType};
7709 ATSUFontFeatureSelector selectors[] = {kAllTypeFeaturesOffSelector,
7710 kDecomposeDiacriticsSelector};
7711 Lisp_Object font_id_cons;
7712
7713 font_id_cons = Fgethash (make_unibyte_string (family, strlen (family)),
7714 atsu_font_id_hash, Qnil);
7715 if (NILP (font_id_cons))
7716 return NULL;
7717 font_id = cons_to_long (font_id_cons);
7718 size_fixed = Long2Fix (size);
7719 bold_p = (fontface & bold) != 0;
7720 italic_p = (fontface & italic) != 0;
7721 err = ATSUCreateStyle (&mac_style);
7722 if (err != noErr)
7723 return NULL;
7724 err = ATSUSetFontFeatures (mac_style, sizeof (types) / sizeof (types[0]),
7725 types, selectors);
7726 if (err != noErr)
7727 return NULL;
7728 err = ATSUSetAttributes (mac_style, sizeof (tags) / sizeof (tags[0]),
7729 tags, sizes, values);
7730 fontnum = -1;
7731 scriptcode = kTextEncodingMacUnicode;
7732 }
7733 else
7734 #endif
7735 {
7736 Lisp_Object tmp = Fassoc (build_string (family), fm_font_family_alist);
7737
7738 if (NILP (tmp))
7739 return NULL;
7740 fontnum = XINT (XCDR (tmp));
7741 #if TARGET_API_MAC_CARBON
7742 if (FMGetFontFamilyTextEncoding (fontnum, &encoding) != noErr)
7743 return NULL;
7744 scriptcode = GetTextEncodingBase (encoding);
7745 #else
7746 scriptcode = FontToScript (fontnum);
7747 #endif
7748 }
7749
7750 font = (MacFontStruct *) xmalloc (sizeof (struct MacFontStruct));
7751
7752 font->mac_fontnum = fontnum;
7753 font->mac_fontsize = size;
7754 font->mac_fontface = fontface;
7755 font->mac_scriptcode = scriptcode;
7756 #if USE_ATSUI
7757 font->mac_style = mac_style;
7758 #if USE_CG_TEXT_DRAWING
7759 font->cg_font = NULL;
7760 font->cg_glyphs = NULL;
7761 #endif
7762 #endif
7763
7764 /* Apple Japanese (SJIS) font is listed as both
7765 "*-jisx0208.1983-sjis" (Japanese script) and "*-jisx0201.1976-0"
7766 (Roman script) in init_font_name_table (). The latter should be
7767 treated as a one-byte font. */
7768 if (scriptcode == smJapanese && strcmp (charset, "jisx0201.1976-0") == 0)
7769 font->mac_scriptcode = smRoman;
7770
7771 font->full_name = mac_to_x_fontname (family, size, fontface, charset);
7772
7773 #if USE_ATSUI
7774 if (font->mac_style)
7775 {
7776 OSErr err;
7777 UniChar c;
7778
7779 font->min_byte1 = 0;
7780 font->max_byte1 = 0xff;
7781 font->min_char_or_byte2 = 0;
7782 font->max_char_or_byte2 = 0xff;
7783
7784 font->bounds.rows = xmalloc (sizeof (XCharStruct *) * 0x100);
7785 bzero (font->bounds.rows, sizeof (XCharStruct *) * 0x100);
7786 font->bounds.rows[0] = xmalloc (sizeof (XCharStruct) * 0x100);
7787 pcm_init (font->bounds.rows[0], 0x100);
7788
7789 #if USE_CG_TEXT_DRAWING
7790 {
7791 FMFontFamily font_family;
7792 FMFontStyle style;
7793 ATSFontRef ats_font;
7794
7795 err = FMGetFontFamilyInstanceFromFont (font_id, &font_family, &style);
7796 if (err == noErr)
7797 err = FMGetFontFromFontFamilyInstance (font_family, fontface,
7798 &font_id, &style);
7799 /* Use CG text drawing if italic/bold is not synthesized. */
7800 if (err == noErr && style == fontface)
7801 {
7802 ats_font = FMGetATSFontRefFromFont (font_id);
7803 font->cg_font = CGFontCreateWithPlatformFont (&ats_font);
7804 }
7805 }
7806
7807 if (font->cg_font)
7808 {
7809 font->cg_glyphs = xmalloc (sizeof (CGGlyph) * 0x100);
7810 bzero (font->cg_glyphs, sizeof (CGGlyph) * 0x100);
7811 }
7812 #endif
7813 space_bounds = font->bounds.rows[0] + 0x20;
7814 err = mac_query_char_extents (font->mac_style, 0x20,
7815 &font->ascent, &font->descent,
7816 space_bounds,
7817 #if USE_CG_TEXT_DRAWING
7818 (font->cg_glyphs ? font->cg_glyphs + 0x20
7819 : NULL)
7820 #else
7821 NULL
7822 #endif
7823 );
7824 if (err != noErr)
7825 {
7826 mac_unload_font (&one_mac_display_info, font);
7827 return NULL;
7828 }
7829
7830 pcm = font->bounds.rows[0];
7831 for (c = 0x21; c <= 0xff; c++)
7832 {
7833 if (c == 0xad)
7834 /* Soft hyphen is not supported in ATSUI. */
7835 continue;
7836 else if (c == 0x7f)
7837 {
7838 c = 0x9f;
7839 continue;
7840 }
7841
7842 mac_query_char_extents (font->mac_style, c, NULL, NULL, pcm + c,
7843 #if USE_CG_TEXT_DRAWING
7844 (font->cg_glyphs ? font->cg_glyphs + c
7845 : NULL)
7846 #else
7847 NULL
7848 #endif
7849 );
7850
7851 #if USE_CG_TEXT_DRAWING
7852 if (font->cg_glyphs && font->cg_glyphs[c] == 0)
7853 {
7854 /* Don't use CG text drawing if font substitution occurs in
7855 ASCII or Latin-1 characters. */
7856 CGFontRelease (font->cg_font);
7857 font->cg_font = NULL;
7858 xfree (font->cg_glyphs);
7859 font->cg_glyphs = NULL;
7860 }
7861 #endif
7862 }
7863 }
7864 else
7865 #endif
7866 {
7867 GrafPtr port;
7868 SInt16 old_fontnum, old_fontsize;
7869 Style old_fontface;
7870 FontInfo the_fontinfo;
7871 int is_two_byte_font;
7872
7873 /* Save the current font number used. */
7874 GetPort (&port);
7875 #if TARGET_API_MAC_CARBON
7876 old_fontnum = GetPortTextFont (port);
7877 old_fontsize = GetPortTextSize (port);
7878 old_fontface = GetPortTextFace (port);
7879 #else
7880 old_fontnum = port->txFont;
7881 old_fontsize = port->txSize;
7882 old_fontface = port->txFace;
7883 #endif
7884
7885 TextFont (fontnum);
7886 TextSize (size);
7887 TextFace (fontface);
7888
7889 GetFontInfo (&the_fontinfo);
7890
7891 font->ascent = the_fontinfo.ascent;
7892 font->descent = the_fontinfo.descent;
7893
7894 is_two_byte_font = (font->mac_scriptcode == smJapanese
7895 || font->mac_scriptcode == smTradChinese
7896 || font->mac_scriptcode == smSimpChinese
7897 || font->mac_scriptcode == smKorean);
7898
7899 if (is_two_byte_font)
7900 {
7901 int char_width;
7902
7903 font->min_byte1 = 0xa1;
7904 font->max_byte1 = 0xfe;
7905 font->min_char_or_byte2 = 0xa1;
7906 font->max_char_or_byte2 = 0xfe;
7907
7908 /* Use the width of an "ideographic space" of that font
7909 because the_fontinfo.widMax returns the wrong width for
7910 some fonts. */
7911 switch (font->mac_scriptcode)
7912 {
7913 case smJapanese:
7914 font->min_byte1 = 0x81;
7915 font->max_byte1 = 0xfc;
7916 font->min_char_or_byte2 = 0x40;
7917 font->max_char_or_byte2 = 0xfc;
7918 char_width = StringWidth("\p\x81\x40");
7919 break;
7920 case smTradChinese:
7921 font->min_char_or_byte2 = 0x40;
7922 char_width = StringWidth("\p\xa1\x40");
7923 break;
7924 case smSimpChinese:
7925 char_width = StringWidth("\p\xa1\xa1");
7926 break;
7927 case smKorean:
7928 char_width = StringWidth("\p\xa1\xa1");
7929 break;
7930 }
7931
7932 font->bounds.per_char = NULL;
7933
7934 if (fontface & italic)
7935 font->max_bounds.rbearing = char_width + 1;
7936 else
7937 font->max_bounds.rbearing = char_width;
7938 font->max_bounds.lbearing = 0;
7939 font->max_bounds.width = char_width;
7940 font->max_bounds.ascent = the_fontinfo.ascent;
7941 font->max_bounds.descent = the_fontinfo.descent;
7942
7943 font->min_bounds = font->max_bounds;
7944 }
7945 else
7946 {
7947 int c;
7948
7949 font->min_byte1 = font->max_byte1 = 0;
7950 font->min_char_or_byte2 = 0x20;
7951 font->max_char_or_byte2 = 0xff;
7952
7953 font->bounds.per_char =
7954 xmalloc (sizeof (XCharStruct) * (0xff - 0x20 + 1));
7955 bzero (font->bounds.per_char,
7956 sizeof (XCharStruct) * (0xff - 0x20 + 1));
7957
7958 space_bounds = font->bounds.per_char;
7959 mac_query_char_extents (NULL, 0x20, &font->ascent, &font->descent,
7960 space_bounds, NULL);
7961
7962 for (c = 0x21, pcm = space_bounds + 1; c <= 0xff; c++, pcm++)
7963 mac_query_char_extents (NULL, c, NULL, NULL, pcm, NULL);
7964 }
7965
7966 /* Restore previous font number, size and face. */
7967 TextFont (old_fontnum);
7968 TextSize (old_fontsize);
7969 TextFace (old_fontface);
7970 }
7971
7972 if (space_bounds)
7973 {
7974 int c;
7975
7976 font->min_bounds = font->max_bounds = *space_bounds;
7977 for (c = 0x21, pcm = space_bounds + 1; c <= 0x7f; c++, pcm++)
7978 if (pcm->width > 0)
7979 {
7980 font->min_bounds.lbearing = min (font->min_bounds.lbearing,
7981 pcm->lbearing);
7982 font->min_bounds.rbearing = min (font->min_bounds.rbearing,
7983 pcm->rbearing);
7984 font->min_bounds.width = min (font->min_bounds.width,
7985 pcm->width);
7986 font->min_bounds.ascent = min (font->min_bounds.ascent,
7987 pcm->ascent);
7988
7989 font->max_bounds.lbearing = max (font->max_bounds.lbearing,
7990 pcm->lbearing);
7991 font->max_bounds.rbearing = max (font->max_bounds.rbearing,
7992 pcm->rbearing);
7993 font->max_bounds.width = max (font->max_bounds.width,
7994 pcm->width);
7995 font->max_bounds.ascent = max (font->max_bounds.ascent,
7996 pcm->ascent);
7997 }
7998 if (
7999 #if USE_ATSUI
8000 font->mac_style == NULL &&
8001 #endif
8002 font->max_bounds.width == font->min_bounds.width
8003 && font->min_bounds.lbearing >= 0
8004 && font->max_bounds.rbearing <= font->max_bounds.width)
8005 {
8006 /* Fixed width and no overhangs. */
8007 xfree (font->bounds.per_char);
8008 font->bounds.per_char = NULL;
8009 }
8010 }
8011
8012 #if !defined (MAC_OS8) || USE_ATSUI
8013 /* AppKit and WebKit do some adjustment to the heights of Courier,
8014 Helvetica, and Times. This only works on the environments where
8015 srcCopy text transfer mode is never used. */
8016 if (
8017 #ifdef MAC_OS8 /* implies USE_ATSUI */
8018 font->mac_style &&
8019 #endif
8020 (strcmp (family, "courier") == 0 || strcmp (family, "helvetica") == 0
8021 || strcmp (family, "times") == 0))
8022 font->ascent += (font->ascent + font->descent) * .15 + 0.5;
8023 #endif
8024
8025 return font;
8026 }
8027
8028
8029 void
8030 mac_unload_font (dpyinfo, font)
8031 struct mac_display_info *dpyinfo;
8032 XFontStruct *font;
8033 {
8034 xfree (font->full_name);
8035 #if USE_ATSUI
8036 if (font->mac_style)
8037 {
8038 int i;
8039
8040 for (i = font->min_byte1; i <= font->max_byte1; i++)
8041 if (font->bounds.rows[i])
8042 xfree (font->bounds.rows[i]);
8043 xfree (font->bounds.rows);
8044 ATSUDisposeStyle (font->mac_style);
8045 }
8046 else
8047 #endif
8048 if (font->bounds.per_char)
8049 xfree (font->bounds.per_char);
8050 #if USE_CG_TEXT_DRAWING
8051 if (font->cg_font)
8052 CGFontRelease (font->cg_font);
8053 if (font->cg_glyphs)
8054 xfree (font->cg_glyphs);
8055 #endif
8056 xfree (font);
8057 }
8058
8059
8060 /* Load font named FONTNAME of the size SIZE for frame F, and return a
8061 pointer to the structure font_info while allocating it dynamically.
8062 If SIZE is 0, load any size of font.
8063 If loading is failed, return NULL. */
8064
8065 struct font_info *
8066 x_load_font (f, fontname, size)
8067 struct frame *f;
8068 register char *fontname;
8069 int size;
8070 {
8071 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
8072 Lisp_Object font_names;
8073
8074 /* Get a list of all the fonts that match this name. Once we
8075 have a list of matching fonts, we compare them against the fonts
8076 we already have by comparing names. */
8077 font_names = x_list_fonts (f, build_string (fontname), size, 1);
8078
8079 if (!NILP (font_names))
8080 {
8081 Lisp_Object tail;
8082 int i;
8083
8084 for (i = 0; i < dpyinfo->n_fonts; i++)
8085 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
8086 if (dpyinfo->font_table[i].name
8087 && (!strcmp (dpyinfo->font_table[i].name,
8088 SDATA (XCAR (tail)))
8089 || !strcmp (dpyinfo->font_table[i].full_name,
8090 SDATA (XCAR (tail)))))
8091 return (dpyinfo->font_table + i);
8092 }
8093 else
8094 return NULL;
8095
8096 /* Load the font and add it to the table. */
8097 {
8098 struct MacFontStruct *font;
8099 struct font_info *fontp;
8100 int i;
8101
8102 fontname = (char *) SDATA (XCAR (font_names));
8103
8104 BLOCK_INPUT;
8105 font = (MacFontStruct *) XLoadQueryFont (FRAME_MAC_DISPLAY (f), fontname);
8106 UNBLOCK_INPUT;
8107 if (!font)
8108 return NULL;
8109
8110 /* Find a free slot in the font table. */
8111 for (i = 0; i < dpyinfo->n_fonts; ++i)
8112 if (dpyinfo->font_table[i].name == NULL)
8113 break;
8114
8115 /* If no free slot found, maybe enlarge the font table. */
8116 if (i == dpyinfo->n_fonts
8117 && dpyinfo->n_fonts == dpyinfo->font_table_size)
8118 {
8119 int sz;
8120 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
8121 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
8122 dpyinfo->font_table
8123 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
8124 }
8125
8126 fontp = dpyinfo->font_table + i;
8127 if (i == dpyinfo->n_fonts)
8128 ++dpyinfo->n_fonts;
8129
8130 /* Now fill in the slots of *FONTP. */
8131 BLOCK_INPUT;
8132 bzero (fontp, sizeof (*fontp));
8133 fontp->font = font;
8134 fontp->font_idx = i;
8135 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
8136 bcopy (fontname, fontp->name, strlen (fontname) + 1);
8137
8138 if (font->min_bounds.width == font->max_bounds.width)
8139 {
8140 /* Fixed width font. */
8141 fontp->average_width = fontp->space_width = font->min_bounds.width;
8142 }
8143 else
8144 {
8145 XChar2b char2b;
8146 XCharStruct *pcm;
8147
8148 char2b.byte1 = 0x00, char2b.byte2 = 0x20;
8149 pcm = mac_per_char_metric (font, &char2b, 0);
8150 if (pcm)
8151 fontp->space_width = pcm->width;
8152 else
8153 fontp->space_width = FONT_WIDTH (font);
8154
8155 if (pcm)
8156 {
8157 int width = pcm->width;
8158 for (char2b.byte2 = 33; char2b.byte2 <= 126; char2b.byte2++)
8159 if ((pcm = mac_per_char_metric (font, &char2b, 0)) != NULL)
8160 width += pcm->width;
8161 fontp->average_width = width / 95;
8162 }
8163 else
8164 fontp->average_width = FONT_WIDTH (font);
8165 }
8166
8167 fontp->full_name = (char *) xmalloc (strlen (font->full_name) + 1);
8168 bcopy (font->full_name, fontp->full_name, strlen (font->full_name) + 1);
8169
8170 fontp->size = font->max_bounds.width;
8171 fontp->height = FONT_HEIGHT (font);
8172 {
8173 /* For some font, ascent and descent in max_bounds field is
8174 larger than the above value. */
8175 int max_height = font->max_bounds.ascent + font->max_bounds.descent;
8176 if (max_height > fontp->height)
8177 fontp->height = max_height;
8178 }
8179
8180 /* The slot `encoding' specifies how to map a character
8181 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
8182 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
8183 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
8184 2:0xA020..0xFF7F). For the moment, we don't know which charset
8185 uses this font. So, we set information in fontp->encoding[1]
8186 which is never used by any charset. If mapping can't be
8187 decided, set FONT_ENCODING_NOT_DECIDED. */
8188 if (font->mac_scriptcode == smJapanese)
8189 fontp->encoding[1] = 4;
8190 else
8191 {
8192 fontp->encoding[1]
8193 = (font->max_byte1 == 0
8194 /* 1-byte font */
8195 ? (font->min_char_or_byte2 < 0x80
8196 ? (font->max_char_or_byte2 < 0x80
8197 ? 0 /* 0x20..0x7F */
8198 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
8199 : 1) /* 0xA0..0xFF */
8200 /* 2-byte font */
8201 : (font->min_byte1 < 0x80
8202 ? (font->max_byte1 < 0x80
8203 ? (font->min_char_or_byte2 < 0x80
8204 ? (font->max_char_or_byte2 < 0x80
8205 ? 0 /* 0x2020..0x7F7F */
8206 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
8207 : 3) /* 0x20A0..0x7FFF */
8208 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
8209 : (font->min_char_or_byte2 < 0x80
8210 ? (font->max_char_or_byte2 < 0x80
8211 ? 2 /* 0xA020..0xFF7F */
8212 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
8213 : 1))); /* 0xA0A0..0xFFFF */
8214 }
8215
8216 #if 0 /* MAC_TODO: fill these out with more reasonably values */
8217 fontp->baseline_offset
8218 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
8219 ? (long) value : 0);
8220 fontp->relative_compose
8221 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
8222 ? (long) value : 0);
8223 fontp->default_ascent
8224 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
8225 ? (long) value : 0);
8226 #else
8227 fontp->baseline_offset = 0;
8228 fontp->relative_compose = 0;
8229 fontp->default_ascent = 0;
8230 #endif
8231
8232 /* Set global flag fonts_changed_p to non-zero if the font loaded
8233 has a character with a smaller width than any other character
8234 before, or if the font loaded has a smaller height than any
8235 other font loaded before. If this happens, it will make a
8236 glyph matrix reallocation necessary. */
8237 fonts_changed_p |= x_compute_min_glyph_bounds (f);
8238 UNBLOCK_INPUT;
8239 return fontp;
8240 }
8241 }
8242
8243
8244 /* Return a pointer to struct font_info of a font named FONTNAME for
8245 frame F. If no such font is loaded, return NULL. */
8246
8247 struct font_info *
8248 x_query_font (f, fontname)
8249 struct frame *f;
8250 register char *fontname;
8251 {
8252 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
8253 int i;
8254
8255 for (i = 0; i < dpyinfo->n_fonts; i++)
8256 if (dpyinfo->font_table[i].name
8257 && (!strcmp (dpyinfo->font_table[i].name, fontname)
8258 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
8259 return (dpyinfo->font_table + i);
8260 return NULL;
8261 }
8262
8263
8264 /* Find a CCL program for a font specified by FONTP, and set the member
8265 `encoder' of the structure. */
8266
8267 void
8268 x_find_ccl_program (fontp)
8269 struct font_info *fontp;
8270 {
8271 Lisp_Object list, elt;
8272
8273 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
8274 {
8275 elt = XCAR (list);
8276 if (CONSP (elt)
8277 && STRINGP (XCAR (elt))
8278 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
8279 >= 0))
8280 break;
8281 }
8282 if (! NILP (list))
8283 {
8284 struct ccl_program *ccl
8285 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
8286
8287 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
8288 xfree (ccl);
8289 else
8290 fontp->font_encoder = ccl;
8291 }
8292 }
8293
8294
8295 \f
8296 /* The Mac Event loop code */
8297
8298 #if !TARGET_API_MAC_CARBON
8299 #include <Events.h>
8300 #include <Quickdraw.h>
8301 #include <Balloons.h>
8302 #include <Devices.h>
8303 #include <Fonts.h>
8304 #include <Gestalt.h>
8305 #include <Menus.h>
8306 #include <Processes.h>
8307 #include <Sound.h>
8308 #include <ToolUtils.h>
8309 #include <TextUtils.h>
8310 #include <Dialogs.h>
8311 #include <Script.h>
8312 #include <Types.h>
8313 #include <Resources.h>
8314
8315 #if __MWERKS__
8316 #include <unix.h>
8317 #endif
8318 #endif /* ! TARGET_API_MAC_CARBON */
8319
8320 #define M_APPLE 128
8321 #define I_ABOUT 1
8322
8323 #define WINDOW_RESOURCE 128
8324 #define TERM_WINDOW_RESOURCE 129
8325
8326 #define DEFAULT_NUM_COLS 80
8327
8328 #define MIN_DOC_SIZE 64
8329 #define MAX_DOC_SIZE 32767
8330
8331 #define EXTRA_STACK_ALLOC (256 * 1024)
8332
8333 #define ARGV_STRING_LIST_ID 129
8334 #define ABOUT_ALERT_ID 128
8335 #define RAM_TOO_LARGE_ALERT_ID 129
8336
8337 /* Contains the string "reverse", which is a constant for mouse button emu.*/
8338 Lisp_Object Qreverse;
8339
8340
8341 /* Modifier associated with the control key, or nil to ignore. */
8342 Lisp_Object Vmac_control_modifier;
8343
8344 /* Modifier associated with the option key, or nil to ignore. */
8345 Lisp_Object Vmac_option_modifier;
8346
8347 /* Modifier associated with the command key, or nil to ignore. */
8348 Lisp_Object Vmac_command_modifier;
8349
8350 /* Modifier associated with the function key, or nil to ignore. */
8351 Lisp_Object Vmac_function_modifier;
8352
8353 /* True if the option and command modifiers should be used to emulate
8354 a three button mouse */
8355 Lisp_Object Vmac_emulate_three_button_mouse;
8356
8357 #if USE_CARBON_EVENTS
8358 /* Non-zero if the mouse wheel button (i.e. button 4) should map to
8359 mouse-2, instead of mouse-3. */
8360 int mac_wheel_button_is_mouse_2;
8361
8362 /* If non-zero, the Mac "Command" key is passed on to the Mac Toolbox
8363 for processing before Emacs sees it. */
8364 int mac_pass_command_to_system;
8365
8366 /* If non-zero, the Mac "Control" key is passed on to the Mac Toolbox
8367 for processing before Emacs sees it. */
8368 int mac_pass_control_to_system;
8369 #endif
8370
8371 /* Points to the variable `inev' in the function XTread_socket. It is
8372 used for passing an input event to the function back from
8373 Carbon/Apple event handlers. */
8374 static struct input_event *read_socket_inev = NULL;
8375
8376 Point saved_menu_event_location;
8377
8378 /* Apple Events */
8379 #if USE_CARBON_EVENTS
8380 static Lisp_Object Qhicommand;
8381 #endif
8382 extern int mac_ready_for_apple_events;
8383 extern Lisp_Object Qundefined;
8384 extern void init_apple_event_handler P_ ((void));
8385 extern void mac_find_apple_event_spec P_ ((AEEventClass, AEEventID,
8386 Lisp_Object *, Lisp_Object *,
8387 Lisp_Object *));
8388 extern OSErr init_coercion_handler P_ ((void));
8389
8390 /* Drag and Drop */
8391 OSErr install_drag_handler P_ ((WindowRef));
8392 void remove_drag_handler P_ ((WindowRef));
8393
8394 #if USE_CARBON_EVENTS
8395 #ifdef MAC_OSX
8396 extern void init_service_handler ();
8397 static Lisp_Object Qservices, Qpaste, Qperform;
8398 #endif
8399 /* Window Event Handler */
8400 static pascal OSStatus mac_handle_window_event (EventHandlerCallRef,
8401 EventRef, void *);
8402 #endif
8403 OSErr install_window_handler (WindowPtr);
8404
8405 extern void init_emacs_passwd_dir ();
8406 extern int emacs_main (int, char **, char **);
8407
8408 extern void initialize_applescript();
8409 extern void terminate_applescript();
8410
8411 static unsigned int
8412 #if USE_CARBON_EVENTS
8413 mac_to_emacs_modifiers (UInt32 mods)
8414 #else
8415 mac_to_emacs_modifiers (EventModifiers mods)
8416 #endif
8417 {
8418 unsigned int result = 0;
8419 if (mods & shiftKey)
8420 result |= shift_modifier;
8421
8422 /* Deactivated to simplify configuration:
8423 if Vmac_option_modifier is non-NIL, we fully process the Option
8424 key. Otherwise, we only process it if an additional Ctrl or Command
8425 is pressed. That way the system may convert the character to a
8426 composed one.
8427 if ((mods & optionKey) &&
8428 (( !NILP(Vmac_option_modifier) ||
8429 ((mods & cmdKey) || (mods & controlKey))))) */
8430
8431 if (!NILP (Vmac_option_modifier) && (mods & optionKey)) {
8432 Lisp_Object val = Fget(Vmac_option_modifier, Qmodifier_value);
8433 if (INTEGERP(val))
8434 result |= XUINT(val);
8435 }
8436 if (!NILP (Vmac_command_modifier) && (mods & cmdKey)) {
8437 Lisp_Object val = Fget(Vmac_command_modifier, Qmodifier_value);
8438 if (INTEGERP(val))
8439 result |= XUINT(val);
8440 }
8441 if (!NILP (Vmac_control_modifier) && (mods & controlKey)) {
8442 Lisp_Object val = Fget(Vmac_control_modifier, Qmodifier_value);
8443 if (INTEGERP(val))
8444 result |= XUINT(val);
8445 }
8446
8447 #ifdef MAC_OSX
8448 if (!NILP (Vmac_function_modifier) && (mods & kEventKeyModifierFnMask)) {
8449 Lisp_Object val = Fget(Vmac_function_modifier, Qmodifier_value);
8450 if (INTEGERP(val))
8451 result |= XUINT(val);
8452 }
8453 #endif
8454
8455 return result;
8456 }
8457
8458 static int
8459 mac_get_emulated_btn ( UInt32 modifiers )
8460 {
8461 int result = 0;
8462 if (!NILP (Vmac_emulate_three_button_mouse)) {
8463 int cmdIs3 = !EQ (Vmac_emulate_three_button_mouse, Qreverse);
8464 if (modifiers & cmdKey)
8465 result = cmdIs3 ? 2 : 1;
8466 else if (modifiers & optionKey)
8467 result = cmdIs3 ? 1 : 2;
8468 }
8469 return result;
8470 }
8471
8472 #if USE_CARBON_EVENTS
8473 /* Obtains the event modifiers from the event ref and then calls
8474 mac_to_emacs_modifiers. */
8475 static UInt32
8476 mac_event_to_emacs_modifiers (EventRef eventRef)
8477 {
8478 UInt32 mods = 0;
8479 GetEventParameter (eventRef, kEventParamKeyModifiers, typeUInt32, NULL,
8480 sizeof (UInt32), NULL, &mods);
8481 if (!NILP (Vmac_emulate_three_button_mouse) &&
8482 GetEventClass(eventRef) == kEventClassMouse)
8483 {
8484 mods &= ~(optionKey | cmdKey);
8485 }
8486 return mac_to_emacs_modifiers (mods);
8487 }
8488
8489 /* Given an event ref, return the code to use for the mouse button
8490 code in the emacs input_event. */
8491 static int
8492 mac_get_mouse_btn (EventRef ref)
8493 {
8494 EventMouseButton result = kEventMouseButtonPrimary;
8495 GetEventParameter (ref, kEventParamMouseButton, typeMouseButton, NULL,
8496 sizeof (EventMouseButton), NULL, &result);
8497 switch (result)
8498 {
8499 case kEventMouseButtonPrimary:
8500 if (NILP (Vmac_emulate_three_button_mouse))
8501 return 0;
8502 else {
8503 UInt32 mods = 0;
8504 GetEventParameter (ref, kEventParamKeyModifiers, typeUInt32, NULL,
8505 sizeof (UInt32), NULL, &mods);
8506 return mac_get_emulated_btn(mods);
8507 }
8508 case kEventMouseButtonSecondary:
8509 return mac_wheel_button_is_mouse_2 ? 2 : 1;
8510 case kEventMouseButtonTertiary:
8511 case 4: /* 4 is the number for the mouse wheel button */
8512 return mac_wheel_button_is_mouse_2 ? 1 : 2;
8513 default:
8514 return 0;
8515 }
8516 }
8517
8518 /* Normally, ConvertEventRefToEventRecord will correctly handle all
8519 events. However the click of the mouse wheel is not converted to a
8520 mouseDown or mouseUp event. Likewise for dead key down events.
8521 This calls ConvertEventRef, but then checks to see if it is a mouse
8522 up/down, or a dead key down carbon event that has not been
8523 converted, and if so, converts it by hand (to be picked up in the
8524 XTread_socket loop). */
8525 static Boolean mac_convert_event_ref (EventRef eventRef, EventRecord *eventRec)
8526 {
8527 OSStatus err;
8528 Boolean result = ConvertEventRefToEventRecord (eventRef, eventRec);
8529
8530 if (result)
8531 return result;
8532
8533 switch (GetEventClass (eventRef))
8534 {
8535 case kEventClassMouse:
8536 switch (GetEventKind (eventRef))
8537 {
8538 case kEventMouseDown:
8539 eventRec->what = mouseDown;
8540 result = 1;
8541 break;
8542
8543 case kEventMouseUp:
8544 eventRec->what = mouseUp;
8545 result = 1;
8546 break;
8547
8548 default:
8549 break;
8550 }
8551 break;
8552
8553 case kEventClassKeyboard:
8554 switch (GetEventKind (eventRef))
8555 {
8556 case kEventRawKeyDown:
8557 {
8558 unsigned char char_codes;
8559 UInt32 key_code;
8560
8561 err = GetEventParameter (eventRef, kEventParamKeyMacCharCodes,
8562 typeChar, NULL, sizeof (char),
8563 NULL, &char_codes);
8564 if (err == noErr)
8565 err = GetEventParameter (eventRef, kEventParamKeyCode,
8566 typeUInt32, NULL, sizeof (UInt32),
8567 NULL, &key_code);
8568 if (err == noErr)
8569 {
8570 eventRec->what = keyDown;
8571 eventRec->message = char_codes | ((key_code & 0xff) << 8);
8572 result = 1;
8573 }
8574 }
8575 break;
8576
8577 default:
8578 break;
8579 }
8580 break;
8581
8582 default:
8583 break;
8584 }
8585
8586 if (result)
8587 {
8588 /* Need where and when. */
8589 UInt32 mods = 0;
8590
8591 GetEventParameter (eventRef, kEventParamMouseLocation, typeQDPoint,
8592 NULL, sizeof (Point), NULL, &eventRec->where);
8593 /* Use two step process because new event modifiers are 32-bit
8594 and old are 16-bit. Currently, only loss is NumLock & Fn. */
8595 GetEventParameter (eventRef, kEventParamKeyModifiers, typeUInt32,
8596 NULL, sizeof (UInt32), NULL, &mods);
8597 eventRec->modifiers = mods;
8598
8599 eventRec->when = EventTimeToTicks (GetEventTime (eventRef));
8600 }
8601
8602 return result;
8603 }
8604
8605 #endif
8606
8607 #ifdef MAC_OS8
8608 static void
8609 do_get_menus (void)
8610 {
8611 Handle menubar_handle;
8612 MenuHandle menu_handle;
8613
8614 menubar_handle = GetNewMBar (128);
8615 if(menubar_handle == NULL)
8616 abort ();
8617 SetMenuBar (menubar_handle);
8618 DrawMenuBar ();
8619
8620 #if !TARGET_API_MAC_CARBON
8621 menu_handle = GetMenuHandle (M_APPLE);
8622 if(menu_handle != NULL)
8623 AppendResMenu (menu_handle,'DRVR');
8624 else
8625 abort ();
8626 #endif
8627 }
8628
8629
8630 static void
8631 do_init_managers (void)
8632 {
8633 #if !TARGET_API_MAC_CARBON
8634 InitGraf (&qd.thePort);
8635 InitFonts ();
8636 FlushEvents (everyEvent, 0);
8637 InitWindows ();
8638 InitMenus ();
8639 TEInit ();
8640 InitDialogs (NULL);
8641 #endif /* !TARGET_API_MAC_CARBON */
8642 InitCursor ();
8643
8644 #if !TARGET_API_MAC_CARBON
8645 /* set up some extra stack space for use by emacs */
8646 SetApplLimit ((Ptr) ((long) GetApplLimit () - EXTRA_STACK_ALLOC));
8647
8648 /* MaxApplZone must be called for AppleScript to execute more
8649 complicated scripts */
8650 MaxApplZone ();
8651 MoreMasters ();
8652 #endif /* !TARGET_API_MAC_CARBON */
8653 }
8654
8655 static void
8656 do_check_ram_size (void)
8657 {
8658 SInt32 physical_ram_size, logical_ram_size;
8659
8660 if (Gestalt (gestaltPhysicalRAMSize, &physical_ram_size) != noErr
8661 || Gestalt (gestaltLogicalRAMSize, &logical_ram_size) != noErr
8662 || physical_ram_size > (1 << VALBITS)
8663 || logical_ram_size > (1 << VALBITS))
8664 {
8665 StopAlert (RAM_TOO_LARGE_ALERT_ID, NULL);
8666 exit (1);
8667 }
8668 }
8669 #endif /* MAC_OS8 */
8670
8671 static void
8672 do_window_update (WindowPtr win)
8673 {
8674 struct frame *f = mac_window_to_frame (win);
8675
8676 BeginUpdate (win);
8677
8678 /* The tooltip has been drawn already. Avoid the SET_FRAME_GARBAGED
8679 below. */
8680 if (win != tip_window)
8681 {
8682 if (f->async_visible == 0)
8683 {
8684 /* Update events may occur when a frame gets iconified. */
8685 #if 0
8686 f->async_visible = 1;
8687 f->async_iconified = 0;
8688 SET_FRAME_GARBAGED (f);
8689 #endif
8690 }
8691 else
8692 {
8693 Rect r;
8694 #if TARGET_API_MAC_CARBON
8695 RgnHandle region = NewRgn ();
8696
8697 GetPortVisibleRegion (GetWindowPort (win), region);
8698 GetRegionBounds (region, &r);
8699 expose_frame (f, r.left, r.top, r.right - r.left, r.bottom - r.top);
8700 UpdateControls (win, region);
8701 DisposeRgn (region);
8702 #else
8703 r = (*win->visRgn)->rgnBBox;
8704 expose_frame (f, r.left, r.top, r.right - r.left, r.bottom - r.top);
8705 UpdateControls (win, win->visRgn);
8706 #endif
8707 }
8708 }
8709
8710 EndUpdate (win);
8711 }
8712
8713 static int
8714 is_emacs_window (WindowPtr win)
8715 {
8716 Lisp_Object tail, frame;
8717
8718 if (!win)
8719 return 0;
8720
8721 FOR_EACH_FRAME (tail, frame)
8722 if (FRAME_MAC_P (XFRAME (frame)))
8723 if (FRAME_MAC_WINDOW (XFRAME (frame)) == win)
8724 return 1;
8725
8726 return 0;
8727 }
8728
8729 static void
8730 do_app_resume ()
8731 {
8732 /* Window-activate events will do the job. */
8733 }
8734
8735 static void
8736 do_app_suspend ()
8737 {
8738 /* Window-deactivate events will do the job. */
8739 }
8740
8741
8742 static void
8743 do_apple_menu (SInt16 menu_item)
8744 {
8745 #if !TARGET_API_MAC_CARBON
8746 Str255 item_name;
8747 SInt16 da_driver_refnum;
8748
8749 if (menu_item == I_ABOUT)
8750 NoteAlert (ABOUT_ALERT_ID, NULL);
8751 else
8752 {
8753 GetMenuItemText (GetMenuHandle (M_APPLE), menu_item, item_name);
8754 da_driver_refnum = OpenDeskAcc (item_name);
8755 }
8756 #endif /* !TARGET_API_MAC_CARBON */
8757 }
8758
8759 void
8760 do_menu_choice (SInt32 menu_choice)
8761 {
8762 SInt16 menu_id, menu_item;
8763
8764 menu_id = HiWord (menu_choice);
8765 menu_item = LoWord (menu_choice);
8766
8767 switch (menu_id)
8768 {
8769 case 0:
8770 break;
8771
8772 case M_APPLE:
8773 do_apple_menu (menu_item);
8774 break;
8775
8776 default:
8777 {
8778 struct frame *f = mac_focus_frame (&one_mac_display_info);
8779 MenuHandle menu = GetMenuHandle (menu_id);
8780 if (menu)
8781 {
8782 UInt32 refcon;
8783
8784 GetMenuItemRefCon (menu, menu_item, &refcon);
8785 menubar_selection_callback (f, refcon);
8786 }
8787 }
8788 }
8789
8790 HiliteMenu (0);
8791 }
8792
8793
8794 /* Handle drags in size box. Based on code contributed by Ben
8795 Mesander and IM - Window Manager A. */
8796
8797 static void
8798 do_grow_window (WindowPtr w, EventRecord *e)
8799 {
8800 Rect limit_rect;
8801 int rows, columns, width, height;
8802 struct frame *f = mac_window_to_frame (w);
8803 XSizeHints *size_hints = FRAME_SIZE_HINTS (f);
8804 int min_width = MIN_DOC_SIZE, min_height = MIN_DOC_SIZE;
8805 #if TARGET_API_MAC_CARBON
8806 Rect new_rect;
8807 #else
8808 long grow_size;
8809 #endif
8810
8811 if (size_hints->flags & PMinSize)
8812 {
8813 min_width = size_hints->min_width;
8814 min_height = size_hints->min_height;
8815 }
8816 SetRect (&limit_rect, min_width, min_height, MAX_DOC_SIZE, MAX_DOC_SIZE);
8817
8818 #if TARGET_API_MAC_CARBON
8819 if (!ResizeWindow (w, e->where, &limit_rect, &new_rect))
8820 return;
8821 height = new_rect.bottom - new_rect.top;
8822 width = new_rect.right - new_rect.left;
8823 #else
8824 grow_size = GrowWindow (w, e->where, &limit_rect);
8825 /* see if it really changed size */
8826 if (grow_size == 0)
8827 return;
8828 height = HiWord (grow_size);
8829 width = LoWord (grow_size);
8830 #endif
8831
8832 if (width != FRAME_PIXEL_WIDTH (f)
8833 || height != FRAME_PIXEL_HEIGHT (f))
8834 {
8835 rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, height);
8836 columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, width);
8837
8838 x_set_window_size (f, 0, columns, rows);
8839 }
8840 }
8841
8842
8843 /* Handle clicks in zoom box. Calculation of "standard state" based
8844 on code in IM - Window Manager A and code contributed by Ben
8845 Mesander. The standard state of an Emacs window is 80-characters
8846 wide (DEFAULT_NUM_COLS) and as tall as will fit on the screen. */
8847
8848 static void
8849 do_zoom_window (WindowPtr w, int zoom_in_or_out)
8850 {
8851 Rect zoom_rect, port_rect;
8852 int columns, rows, width, height;
8853 struct frame *f = mac_window_to_frame (w);
8854 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
8855 #if TARGET_API_MAC_CARBON
8856 Point standard_size;
8857
8858 standard_size.h = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, DEFAULT_NUM_COLS);
8859 standard_size.v = dpyinfo->height;
8860
8861 if (IsWindowInStandardState (w, &standard_size, &zoom_rect))
8862 zoom_in_or_out = inZoomIn;
8863 else
8864 {
8865 /* Adjust the standard size according to character boundaries. */
8866
8867 columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, zoom_rect.right - zoom_rect.left);
8868 rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, zoom_rect.bottom - zoom_rect.top);
8869 standard_size.h = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, columns);
8870 standard_size.v = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
8871 GetWindowBounds (w, kWindowContentRgn, &port_rect);
8872 if (IsWindowInStandardState (w, &standard_size, &zoom_rect)
8873 && port_rect.left == zoom_rect.left
8874 && port_rect.top == zoom_rect.top)
8875 zoom_in_or_out = inZoomIn;
8876 else
8877 zoom_in_or_out = inZoomOut;
8878 }
8879
8880 ZoomWindowIdeal (w, zoom_in_or_out, &standard_size);
8881 #else /* not TARGET_API_MAC_CARBON */
8882 GrafPtr save_port;
8883 Point top_left;
8884 int w_title_height;
8885
8886 GetPort (&save_port);
8887
8888 SetPortWindowPort (w);
8889
8890 /* Clear window to avoid flicker. */
8891 EraseRect (&(w->portRect));
8892 if (zoom_in_or_out == inZoomOut)
8893 {
8894 SetPt (&top_left, w->portRect.left, w->portRect.top);
8895 LocalToGlobal (&top_left);
8896
8897 /* calculate height of window's title bar */
8898 w_title_height = top_left.v - 1
8899 - (**((WindowPeek) w)->strucRgn).rgnBBox.top + GetMBarHeight ();
8900
8901 /* get maximum height of window into zoom_rect.bottom - zoom_rect.top */
8902 zoom_rect = qd.screenBits.bounds;
8903 zoom_rect.top += w_title_height;
8904 InsetRect (&zoom_rect, 8, 4); /* not too tight */
8905
8906 zoom_rect.right = zoom_rect.left
8907 + FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, DEFAULT_NUM_COLS);
8908
8909 /* Adjust the standard size according to character boundaries. */
8910 rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, zoom_rect.bottom - zoom_rect.top);
8911 zoom_rect.bottom =
8912 zoom_rect.top + FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
8913
8914 (**((WStateDataHandle) ((WindowPeek) w)->dataHandle)).stdState
8915 = zoom_rect;
8916 }
8917
8918 ZoomWindow (w, zoom_in_or_out, f == mac_focus_frame (dpyinfo));
8919
8920 SetPort (save_port);
8921 #endif /* not TARGET_API_MAC_CARBON */
8922
8923 /* retrieve window size and update application values */
8924 #if TARGET_API_MAC_CARBON
8925 GetWindowPortBounds (w, &port_rect);
8926 #else
8927 port_rect = w->portRect;
8928 #endif
8929 height = port_rect.bottom - port_rect.top;
8930 width = port_rect.right - port_rect.left;
8931
8932 if (width != FRAME_PIXEL_WIDTH (f)
8933 || height != FRAME_PIXEL_HEIGHT (f))
8934 {
8935 rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, height);
8936 columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, width);
8937
8938 change_frame_size (f, rows, columns, 0, 1, 0);
8939 SET_FRAME_GARBAGED (f);
8940 cancel_mouse_face (f);
8941
8942 FRAME_PIXEL_WIDTH (f) = width;
8943 FRAME_PIXEL_HEIGHT (f) = height;
8944 }
8945 x_real_positions (f, &f->left_pos, &f->top_pos);
8946 }
8947
8948 void
8949 mac_store_apple_event (class, id, desc)
8950 Lisp_Object class, id;
8951 const AEDesc *desc;
8952 {
8953 struct input_event buf;
8954
8955 EVENT_INIT (buf);
8956
8957 buf.kind = MAC_APPLE_EVENT;
8958 buf.x = class;
8959 buf.y = id;
8960 XSETFRAME (buf.frame_or_window,
8961 mac_focus_frame (&one_mac_display_info));
8962 /* Now that Lisp object allocations are protected by BLOCK_INPUT, it
8963 is safe to use them during read_socket_hook. */
8964 buf.arg = mac_aedesc_to_lisp (desc);
8965 kbd_buffer_store_event (&buf);
8966 }
8967
8968 #if TARGET_API_MAC_CARBON
8969 void
8970 mac_store_drag_event (window, mouse_pos, modifiers, desc)
8971 WindowRef window;
8972 Point mouse_pos;
8973 SInt16 modifiers;
8974 const AEDesc *desc;
8975 {
8976 struct input_event buf;
8977
8978 EVENT_INIT (buf);
8979
8980 buf.kind = DRAG_N_DROP_EVENT;
8981 buf.modifiers = mac_to_emacs_modifiers (modifiers);
8982 buf.timestamp = TickCount () * (1000 / 60);
8983 XSETINT (buf.x, mouse_pos.h);
8984 XSETINT (buf.y, mouse_pos.v);
8985 XSETFRAME (buf.frame_or_window, mac_window_to_frame (window));
8986 buf.arg = mac_aedesc_to_lisp (desc);
8987 kbd_buffer_store_event (&buf);
8988 }
8989 #endif
8990
8991 #if USE_CARBON_EVENTS
8992 static pascal OSStatus
8993 mac_handle_command_event (next_handler, event, data)
8994 EventHandlerCallRef next_handler;
8995 EventRef event;
8996 void *data;
8997 {
8998 OSStatus result, err;
8999 HICommand command;
9000 Lisp_Object class_key, id_key, binding;
9001
9002 result = CallNextEventHandler (next_handler, event);
9003 if (result != eventNotHandledErr)
9004 return result;
9005
9006 err = GetEventParameter (event, kEventParamDirectObject, typeHICommand,
9007 NULL, sizeof (HICommand), NULL, &command);
9008
9009 if (err != noErr || command.commandID == 0)
9010 return eventNotHandledErr;
9011
9012 /* A HICommand event is mapped to an Apple event whose event class
9013 symbol is `hicommand' and event ID is its command ID. */
9014 class_key = Qhicommand;
9015 mac_find_apple_event_spec (0, command.commandID,
9016 &class_key, &id_key, &binding);
9017 if (!NILP (binding) && !EQ (binding, Qundefined))
9018 {
9019 if (INTEGERP (binding))
9020 return XINT (binding);
9021 else
9022 {
9023 AppleEvent apple_event;
9024 static EventParamName names[] = {kEventParamDirectObject,
9025 kEventParamKeyModifiers};
9026 static EventParamType types[] = {typeHICommand,
9027 typeUInt32};
9028 err = create_apple_event_from_event_ref (event, 2, names, types,
9029 &apple_event);
9030 if (err == noErr)
9031 {
9032 mac_store_apple_event (class_key, id_key, &apple_event);
9033 AEDisposeDesc (&apple_event);
9034 return noErr;
9035 }
9036 }
9037 }
9038
9039 return eventNotHandledErr;
9040 }
9041
9042 static OSErr
9043 init_command_handler ()
9044 {
9045 EventTypeSpec specs[] = {{kEventClassCommand, kEventCommandProcess}};
9046 static EventHandlerUPP handle_command_eventUPP = NULL;
9047
9048 if (handle_command_eventUPP == NULL)
9049 handle_command_eventUPP = NewEventHandlerUPP (mac_handle_command_event);
9050 return InstallApplicationEventHandler (handle_command_eventUPP,
9051 GetEventTypeCount (specs), specs,
9052 NULL, NULL);
9053 }
9054
9055 static pascal OSStatus
9056 mac_handle_window_event (next_handler, event, data)
9057 EventHandlerCallRef next_handler;
9058 EventRef event;
9059 void *data;
9060 {
9061 WindowPtr wp;
9062 OSStatus result, err;
9063 UInt32 attributes;
9064 XSizeHints *size_hints;
9065
9066 err = GetEventParameter (event, kEventParamDirectObject, typeWindowRef,
9067 NULL, sizeof (WindowPtr), NULL, &wp);
9068 if (err != noErr)
9069 return eventNotHandledErr;
9070
9071 switch (GetEventKind (event))
9072 {
9073 case kEventWindowUpdate:
9074 result = CallNextEventHandler (next_handler, event);
9075 if (result != eventNotHandledErr)
9076 return result;
9077
9078 do_window_update (wp);
9079 return noErr;
9080
9081 case kEventWindowBoundsChanging:
9082 result = CallNextEventHandler (next_handler, event);
9083 if (result != eventNotHandledErr)
9084 return result;
9085
9086 err = GetEventParameter (event, kEventParamAttributes, typeUInt32,
9087 NULL, sizeof (UInt32), NULL, &attributes);
9088 if (err != noErr)
9089 break;
9090
9091 size_hints = FRAME_SIZE_HINTS (mac_window_to_frame (wp));
9092 if ((attributes & kWindowBoundsChangeUserResize)
9093 && ((size_hints->flags & (PResizeInc | PBaseSize | PMinSize))
9094 == (PResizeInc | PBaseSize | PMinSize)))
9095 {
9096 Rect bounds;
9097 int width, height;
9098
9099 err = GetEventParameter (event, kEventParamCurrentBounds,
9100 typeQDRectangle, NULL, sizeof (Rect),
9101 NULL, &bounds);
9102 if (err != noErr)
9103 break;
9104
9105 width = bounds.right - bounds.left;
9106 height = bounds.bottom - bounds.top;
9107
9108 if (width < size_hints->min_width)
9109 width = size_hints->min_width;
9110 else
9111 width = size_hints->base_width
9112 + (int) ((width - size_hints->base_width)
9113 / (float) size_hints->width_inc + .5)
9114 * size_hints->width_inc;
9115
9116 if (height < size_hints->min_height)
9117 height = size_hints->min_height;
9118 else
9119 height = size_hints->base_height
9120 + (int) ((height - size_hints->base_height)
9121 / (float) size_hints->height_inc + .5)
9122 * size_hints->height_inc;
9123
9124 bounds.right = bounds.left + width;
9125 bounds.bottom = bounds.top + height;
9126 SetEventParameter (event, kEventParamCurrentBounds,
9127 typeQDRectangle, sizeof (Rect), &bounds);
9128 return noErr;
9129 }
9130 break;
9131
9132 case kEventWindowShown:
9133 case kEventWindowHidden:
9134 case kEventWindowExpanded:
9135 case kEventWindowCollapsed:
9136 result = CallNextEventHandler (next_handler, event);
9137
9138 mac_handle_visibility_change (mac_window_to_frame (wp));
9139 return noErr;
9140
9141 break;
9142 }
9143
9144 return eventNotHandledErr;
9145 }
9146
9147 static pascal OSStatus
9148 mac_handle_mouse_event (next_handler, event, data)
9149 EventHandlerCallRef next_handler;
9150 EventRef event;
9151 void *data;
9152 {
9153 OSStatus result, err;
9154
9155 switch (GetEventKind (event))
9156 {
9157 case kEventMouseWheelMoved:
9158 {
9159 WindowPtr wp;
9160 struct frame *f;
9161 EventMouseWheelAxis axis;
9162 SInt32 delta;
9163 Point point;
9164
9165 result = CallNextEventHandler (next_handler, event);
9166 if (result != eventNotHandledErr || read_socket_inev == NULL)
9167 return result;
9168
9169 err = GetEventParameter (event, kEventParamWindowRef, typeWindowRef,
9170 NULL, sizeof (WindowRef), NULL, &wp);
9171 if (err != noErr)
9172 break;
9173
9174 f = mac_window_to_frame (wp);
9175 if (f != mac_focus_frame (&one_mac_display_info))
9176 break;
9177
9178 err = GetEventParameter (event, kEventParamMouseWheelAxis,
9179 typeMouseWheelAxis, NULL,
9180 sizeof (EventMouseWheelAxis), NULL, &axis);
9181 if (err != noErr || axis != kEventMouseWheelAxisY)
9182 break;
9183
9184 err = GetEventParameter (event, kEventParamMouseWheelDelta,
9185 typeSInt32, NULL, sizeof (SInt32),
9186 NULL, &delta);
9187 if (err != noErr)
9188 break;
9189 err = GetEventParameter (event, kEventParamMouseLocation,
9190 typeQDPoint, NULL, sizeof (Point),
9191 NULL, &point);
9192 if (err != noErr)
9193 break;
9194 read_socket_inev->kind = WHEEL_EVENT;
9195 read_socket_inev->code = 0;
9196 read_socket_inev->modifiers =
9197 (mac_event_to_emacs_modifiers (event)
9198 | ((delta < 0) ? down_modifier : up_modifier));
9199 SetPortWindowPort (wp);
9200 GlobalToLocal (&point);
9201 XSETINT (read_socket_inev->x, point.h);
9202 XSETINT (read_socket_inev->y, point.v);
9203 XSETFRAME (read_socket_inev->frame_or_window, f);
9204
9205 return noErr;
9206 }
9207 break;
9208
9209 default:
9210 break;
9211 }
9212
9213 return eventNotHandledErr;
9214 }
9215
9216 #ifdef MAC_OSX
9217 OSErr
9218 mac_store_services_event (event)
9219 EventRef event;
9220 {
9221 OSErr err;
9222 AppleEvent apple_event;
9223 Lisp_Object id_key;
9224
9225 switch (GetEventKind (event))
9226 {
9227 case kEventServicePaste:
9228 id_key = Qpaste;
9229 err = create_apple_event_from_event_ref (event, 0, NULL, NULL,
9230 &apple_event);
9231 break;
9232
9233 case kEventServicePerform:
9234 {
9235 static EventParamName names[] = {kEventParamServiceMessageName,
9236 kEventParamServiceUserData};
9237 static EventParamType types[] = {typeCFStringRef,
9238 typeCFStringRef};
9239
9240 id_key = Qperform;
9241 err = create_apple_event_from_event_ref (event, 2, names, types,
9242 &apple_event);
9243 }
9244 break;
9245
9246 default:
9247 abort ();
9248 }
9249
9250 if (err == noErr)
9251 {
9252 mac_store_apple_event (Qservices, id_key, &apple_event);
9253 AEDisposeDesc (&apple_event);
9254 }
9255
9256 return err;
9257 }
9258 #endif /* MAC_OSX */
9259 #endif /* USE_CARBON_EVENTS */
9260
9261
9262 OSErr
9263 install_window_handler (window)
9264 WindowPtr window;
9265 {
9266 OSErr err = noErr;
9267 #if USE_CARBON_EVENTS
9268 EventTypeSpec specs_window[] =
9269 {{kEventClassWindow, kEventWindowUpdate},
9270 {kEventClassWindow, kEventWindowBoundsChanging},
9271 {kEventClassWindow, kEventWindowShown},
9272 {kEventClassWindow, kEventWindowHidden},
9273 {kEventClassWindow, kEventWindowExpanded},
9274 {kEventClassWindow, kEventWindowCollapsed}};
9275 EventTypeSpec specs_mouse[] = {{kEventClassMouse, kEventMouseWheelMoved}};
9276 static EventHandlerUPP handle_window_eventUPP = NULL;
9277 static EventHandlerUPP handle_mouse_eventUPP = NULL;
9278
9279 if (handle_window_eventUPP == NULL)
9280 handle_window_eventUPP = NewEventHandlerUPP (mac_handle_window_event);
9281 if (handle_mouse_eventUPP == NULL)
9282 handle_mouse_eventUPP = NewEventHandlerUPP (mac_handle_mouse_event);
9283 err = InstallWindowEventHandler (window, handle_window_eventUPP,
9284 GetEventTypeCount (specs_window),
9285 specs_window, NULL, NULL);
9286 if (err == noErr)
9287 err = InstallWindowEventHandler (window, handle_mouse_eventUPP,
9288 GetEventTypeCount (specs_mouse),
9289 specs_mouse, NULL, NULL);
9290 #endif
9291 if (err == noErr)
9292 err = install_drag_handler (window);
9293
9294 return err;
9295 }
9296
9297 void
9298 remove_window_handler (window)
9299 WindowPtr window;
9300 {
9301 remove_drag_handler (window);
9302 }
9303
9304
9305 #if __profile__
9306 void
9307 profiler_exit_proc ()
9308 {
9309 ProfilerDump ("\pEmacs.prof");
9310 ProfilerTerm ();
9311 }
9312 #endif
9313
9314 /* These few functions implement Emacs as a normal Mac application
9315 (almost): set up the heap and the Toolbox, handle necessary system
9316 events plus a few simple menu events. They also set up Emacs's
9317 access to functions defined in the rest of this file. Emacs uses
9318 function hooks to perform all its terminal I/O. A complete list of
9319 these functions appear in termhooks.h. For what they do, read the
9320 comments there and see also w32term.c and xterm.c. What's
9321 noticeably missing here is the event loop, which is normally
9322 present in most Mac application. After performing the necessary
9323 Mac initializations, main passes off control to emacs_main
9324 (corresponding to main in emacs.c). Emacs_main calls XTread_socket
9325 (defined further below) to read input. This is where
9326 WaitNextEvent/ReceiveNextEvent is called to process Mac events. */
9327
9328 #ifdef MAC_OS8
9329 #undef main
9330 int
9331 main (void)
9332 {
9333 #if __profile__ /* is the profiler on? */
9334 if (ProfilerInit(collectDetailed, bestTimeBase, 5000, 200))
9335 exit(1);
9336 #endif
9337
9338 #if __MWERKS__
9339 /* set creator and type for files created by MSL */
9340 _fcreator = 'EMAx';
9341 _ftype = 'TEXT';
9342 #endif
9343
9344 do_init_managers ();
9345
9346 do_get_menus ();
9347
9348 #ifndef USE_LSB_TAG
9349 do_check_ram_size ();
9350 #endif
9351
9352 init_emacs_passwd_dir ();
9353
9354 init_environ ();
9355
9356 init_coercion_handler ();
9357
9358 initialize_applescript ();
9359
9360 init_apple_event_handler ();
9361
9362 {
9363 char **argv;
9364 int argc = 0;
9365
9366 /* set up argv array from STR# resource */
9367 get_string_list (&argv, ARGV_STRING_LIST_ID);
9368 while (argv[argc])
9369 argc++;
9370
9371 /* free up AppleScript resources on exit */
9372 atexit (terminate_applescript);
9373
9374 #if __profile__ /* is the profiler on? */
9375 atexit (profiler_exit_proc);
9376 #endif
9377
9378 /* 3rd param "envp" never used in emacs_main */
9379 (void) emacs_main (argc, argv, 0);
9380 }
9381
9382 /* Never reached - real exit in Fkill_emacs */
9383 return 0;
9384 }
9385 #endif
9386
9387 /* Table for translating Mac keycode to X keysym values. Contributed
9388 by Sudhir Shenoy.
9389 Mapping for special keys is now identical to that in Apple X11
9390 except `clear' (-> <clear>) on the KeyPad, `enter' (-> <kp-enter>)
9391 on the right of the Cmd key on laptops, and fn + `enter' (->
9392 <linefeed>). */
9393 static unsigned char keycode_to_xkeysym_table[] = {
9394 /*0x00*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9395 /*0x10*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9396 /*0x20*/ 0, 0, 0, 0, 0x0d /*return*/, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9397
9398 /*0x30*/ 0x09 /*tab*/, 0 /*0x0020 space*/, 0, 0x08 /*backspace*/,
9399 /*0x34*/ 0x8d /*enter on laptops*/, 0x1b /*escape*/, 0, 0,
9400 /*0x38*/ 0, 0, 0, 0,
9401 /*0x3C*/ 0, 0, 0, 0,
9402
9403 /*0x40*/ 0, 0xae /*kp-.*/, 0, 0xaa /*kp-**/,
9404 /*0x44*/ 0, 0xab /*kp-+*/, 0, 0x0b /*clear*/,
9405 /*0x48*/ 0, 0, 0, 0xaf /*kp-/*/,
9406 /*0x4C*/ 0x8d /*kp-enter*/, 0, 0xad /*kp--*/, 0,
9407
9408 /*0x50*/ 0, 0xbd /*kp-=*/, 0xb0 /*kp-0*/, 0xb1 /*kp-1*/,
9409 /*0x54*/ 0xb2 /*kp-2*/, 0xb3 /*kp-3*/, 0xb4 /*kp-4*/, 0xb5 /*kp-5*/,
9410 /*0x58*/ 0xb6 /*kp-6*/, 0xb7 /*kp-7*/, 0, 0xb8 /*kp-8*/,
9411 /*0x5C*/ 0xb9 /*kp-9*/, 0, 0, 0,
9412
9413 /*0x60*/ 0xc2 /*f5*/, 0xc3 /*f6*/, 0xc4 /*f7*/, 0xc0 /*f3*/,
9414 /*0x64*/ 0xc5 /*f8*/, 0xc6 /*f9*/, 0, 0xc8 /*f11*/,
9415 /*0x68*/ 0, 0xca /*f13*/, 0, 0xcb /*f14*/,
9416 /*0x6C*/ 0, 0xc7 /*f10*/, 0x0a /*fn+enter on laptops*/, 0xc9 /*f12*/,
9417
9418 /*0x70*/ 0, 0xcc /*f15*/, 0x6a /*help*/, 0x50 /*home*/,
9419 /*0x74*/ 0x55 /*pgup*/, 0xff /*delete*/, 0xc1 /*f4*/, 0x57 /*end*/,
9420 /*0x78*/ 0xbf /*f2*/, 0x56 /*pgdown*/, 0xbe /*f1*/, 0x51 /*left*/,
9421 /*0x7C*/ 0x53 /*right*/, 0x54 /*down*/, 0x52 /*up*/, 0
9422 };
9423
9424
9425 static int
9426 keycode_to_xkeysym (int keyCode, int *xKeySym)
9427 {
9428 *xKeySym = keycode_to_xkeysym_table [keyCode & 0x7f];
9429 return *xKeySym != 0;
9430 }
9431
9432 static unsigned char fn_keycode_to_xkeysym_table[] = {
9433 /*0x00*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9434 /*0x10*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9435 /*0x20*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9436
9437 /*0x30*/ 0, 0, 0, 0,
9438 /*0x34*/ 0, 0, 0, 0,
9439 /*0x38*/ 0, 0, 0, 0,
9440 /*0x3C*/ 0, 0, 0, 0,
9441
9442 /*0x40*/ 0, 0x2e /*kp-. = .*/, 0, 0x50 /*kp-* = 'p'*/,
9443 /*0x44*/ 0, '/' /*kp-+*/, 0, 0,
9444 /*0x48*/ 0, 0, 0, 0x30 /*kp-/ = '0'*/,
9445 /*0x4C*/ 0, 0, 0x3b /*kp-- = ';'*/, 0,
9446
9447 /*0x50*/ 0, 0x2d /*kp-= = '-'*/, 0x6d /*kp-0 = 'm'*/, 0x6a /*kp-1 = 'j'*/,
9448 /*0x54*/ 0x6b /*kp-2 = 'k'*/, 0x6c /*kp-3 = 'l'*/, 'u' /*kp-4*/, 'i' /*kp-5*/,
9449 /*0x58*/ 'o' /*kp-6*/, '7' /*kp-7*/, 0, '8' /*kp-8*/,
9450 /*0x5C*/ '9' /*kp-9*/, 0, 0, 0,
9451
9452 /*0x60*/ 0, 0, 0, 0,
9453 /*0x64*/ 0, 0, 0, 0,
9454 /*0x68*/ 0, 0, 0, 0,
9455 /*0x6C*/ 0, 0, 0, 0,
9456
9457 /*0x70*/ 0, 0, 0, 0,
9458 /*0x74*/ 0, 0, 0, 0,
9459 /*0x78*/ 0, 0, 0, 0,
9460 /*0x7C*/ 0, 0, 0, 0
9461 };
9462 static int
9463 convert_fn_keycode (EventRef eventRef, int keyCode, int *newCode)
9464 {
9465 #ifdef MAC_OSX
9466 /* Use the special map to translate keys when function modifier is
9467 to be caught. KeyTranslate can't be used in that case.
9468 We can't detect the function key using the input_event.modifiers,
9469 because this uses the high word of an UInt32. Therefore,
9470 we'll just read it out of the original eventRef.
9471 */
9472
9473
9474 /* TODO / known issues
9475
9476 - Fn-Shift-j is regonized as Fn-j and not Fn-J.
9477 The above table always translates to lower characters. We need to use
9478 the KCHR keyboard resource (KeyTranslate() ) to map k->K and 8->*.
9479
9480 - The table is meant for English language keyboards, and it will work
9481 for many others with the exception of key combinations like Fn-ö on
9482 a German keyboard, which is currently mapped to Fn-;.
9483 How to solve this without keeping separate tables for all keyboards
9484 around? KeyTranslate isn't of much help here, as it only takes a 16-bit
9485 value for keycode with the modifiers in he high byte, i.e. no room for the
9486 Fn modifier. That's why we need the table.
9487
9488 */
9489 OSStatus err;
9490 UInt32 mods = 0;
9491 if (!NILP(Vmac_function_modifier))
9492 {
9493 err = GetEventParameter (eventRef, kEventParamKeyModifiers, typeUInt32,
9494 NULL, sizeof (UInt32), NULL, &mods);
9495 if (err != noErr && mods & kEventKeyModifierFnMask)
9496 { *newCode = fn_keycode_to_xkeysym_table [keyCode & 0x7f];
9497
9498 return (*newCode != 0);
9499 }
9500 }
9501 #endif
9502 return false;
9503 }
9504
9505 static int
9506 backtranslate_modified_keycode(int mods, int keycode, int def)
9507 {
9508 EventModifiers mapped_modifiers =
9509 (NILP (Vmac_control_modifier) ? 0 : controlKey)
9510 | (NILP (Vmac_option_modifier) ? 0 : optionKey)
9511 | (NILP (Vmac_command_modifier) ? 0 : cmdKey);
9512
9513 if (mods & mapped_modifiers)
9514 {
9515 /* This code comes from Keyboard Resource,
9516 Appendix C of IM - Text. This is necessary
9517 since shift is ignored in KCHR table
9518 translation when option or command is pressed.
9519 It also does not translate correctly
9520 control-shift chars like C-% so mask off shift
9521 here also.
9522
9523 Not done for combinations with the option key (alt)
9524 unless it is to be caught by Emacs: this is
9525 to preserve key combinations translated by the OS
9526 such as Alt-3.
9527 */
9528 /* Mask off modifier keys that are mapped to some Emacs
9529 modifiers. */
9530 int new_modifiers = mods & ~mapped_modifiers;
9531 /* set high byte of keycode to modifier high byte*/
9532 int new_keycode = keycode | new_modifiers;
9533 Ptr kchr_ptr = (Ptr) GetScriptManagerVariable (smKCHRCache);
9534 unsigned long some_state = 0;
9535 return (int) KeyTranslate (kchr_ptr, new_keycode,
9536 &some_state) & 0xff;
9537 /* TO DO: Recognize two separate resulting characters, "for
9538 example, when the user presses Option-E followed by N, you
9539 can map this through the KeyTranslate function using the
9540 U.S. 'KCHR' resource to produce ´n, which KeyTranslate
9541 returns as two characters in the bytes labeled Character code
9542 1 and Character code 2." (from Carbon API doc) */
9543
9544 }
9545 else
9546 return def;
9547 }
9548
9549
9550 #if !USE_CARBON_EVENTS
9551 static RgnHandle mouse_region = NULL;
9552
9553 Boolean
9554 mac_wait_next_event (er, sleep_time, dequeue)
9555 EventRecord *er;
9556 UInt32 sleep_time;
9557 Boolean dequeue;
9558 {
9559 static EventRecord er_buf = {nullEvent};
9560 UInt32 target_tick, current_tick;
9561 EventMask event_mask;
9562
9563 if (mouse_region == NULL)
9564 mouse_region = NewRgn ();
9565
9566 event_mask = everyEvent;
9567 if (!mac_ready_for_apple_events)
9568 event_mask -= highLevelEventMask;
9569
9570 current_tick = TickCount ();
9571 target_tick = current_tick + sleep_time;
9572
9573 if (er_buf.what == nullEvent)
9574 while (!WaitNextEvent (event_mask, &er_buf,
9575 target_tick - current_tick, mouse_region))
9576 {
9577 current_tick = TickCount ();
9578 if (target_tick <= current_tick)
9579 return false;
9580 }
9581
9582 *er = er_buf;
9583 if (dequeue)
9584 er_buf.what = nullEvent;
9585 return true;
9586 }
9587 #endif /* not USE_CARBON_EVENTS */
9588
9589 #if TARGET_API_MAC_CARBON
9590 OSStatus
9591 mac_post_mouse_moved_event ()
9592 {
9593 EventRef event = NULL;
9594 OSStatus err;
9595
9596 err = CreateEvent (NULL, kEventClassMouse, kEventMouseMoved, 0,
9597 kEventAttributeNone, &event);
9598 if (err == noErr)
9599 {
9600 Point mouse_pos;
9601
9602 GetMouse (&mouse_pos);
9603 LocalToGlobal (&mouse_pos);
9604 err = SetEventParameter (event, kEventParamMouseLocation, typeQDPoint,
9605 sizeof (Point), &mouse_pos);
9606 }
9607 if (err == noErr)
9608 {
9609 UInt32 modifiers = GetCurrentKeyModifiers ();
9610
9611 err = SetEventParameter (event, kEventParamKeyModifiers, typeUInt32,
9612 sizeof (UInt32), &modifiers);
9613 }
9614 if (err == noErr)
9615 err = PostEventToQueue (GetCurrentEventQueue (), event,
9616 kEventPriorityStandard);
9617 if (event)
9618 ReleaseEvent (event);
9619
9620 return err;
9621 }
9622 #endif
9623
9624 /* Emacs calls this whenever it wants to read an input event from the
9625 user. */
9626 int
9627 XTread_socket (sd, expected, hold_quit)
9628 int sd, expected;
9629 struct input_event *hold_quit;
9630 {
9631 struct input_event inev;
9632 int count = 0;
9633 #if USE_CARBON_EVENTS
9634 EventRef eventRef;
9635 EventTargetRef toolbox_dispatcher;
9636 #endif
9637 EventRecord er;
9638 struct mac_display_info *dpyinfo = &one_mac_display_info;
9639
9640 if (interrupt_input_blocked)
9641 {
9642 interrupt_input_pending = 1;
9643 return -1;
9644 }
9645
9646 interrupt_input_pending = 0;
9647 BLOCK_INPUT;
9648
9649 /* So people can tell when we have read the available input. */
9650 input_signal_count++;
9651
9652 ++handling_signal;
9653
9654 #if USE_CARBON_EVENTS
9655 toolbox_dispatcher = GetEventDispatcherTarget ();
9656
9657 while (
9658 #if USE_CG_DRAWING
9659 mac_prepare_for_quickdraw (NULL),
9660 #endif
9661 !ReceiveNextEvent (0, NULL, kEventDurationNoWait,
9662 kEventRemoveFromQueue, &eventRef))
9663 #else /* !USE_CARBON_EVENTS */
9664 while (mac_wait_next_event (&er, 0, true))
9665 #endif /* !USE_CARBON_EVENTS */
9666 {
9667 int do_help = 0;
9668 struct frame *f;
9669 unsigned long timestamp;
9670
9671 /* It is necessary to set this (additional) argument slot of an
9672 event to nil because keyboard.c protects incompletely
9673 processed event from being garbage collected by placing them
9674 in the kbd_buffer_gcpro vector. */
9675 EVENT_INIT (inev);
9676 inev.kind = NO_EVENT;
9677 inev.arg = Qnil;
9678
9679 #if USE_CARBON_EVENTS
9680 timestamp = GetEventTime (eventRef) / kEventDurationMillisecond;
9681 #else
9682 timestamp = er.when * (1000 / 60); /* ticks to milliseconds */
9683 #endif
9684
9685 #if USE_CARBON_EVENTS
9686 /* Handle new events */
9687 if (!mac_convert_event_ref (eventRef, &er))
9688 {
9689 /* There used to be a handler for the kEventMouseWheelMoved
9690 event here. But as of Mac OS X 10.4, this kind of event
9691 is not directly posted to the main event queue by
9692 two-finger scrolling on the trackpad. Instead, some
9693 private event is posted and it is converted to a wheel
9694 event by the default handler for the application target.
9695 The converted one can be received by a Carbon event
9696 handler installed on a window target. */
9697 read_socket_inev = &inev;
9698 SendEventToEventTarget (eventRef, toolbox_dispatcher);
9699 read_socket_inev = NULL;
9700 }
9701 else
9702 #endif /* USE_CARBON_EVENTS */
9703 switch (er.what)
9704 {
9705 case mouseDown:
9706 case mouseUp:
9707 {
9708 WindowPtr window_ptr;
9709 ControlPartCode part_code;
9710 int tool_bar_p = 0;
9711
9712 #if USE_CARBON_EVENTS
9713 /* This is needed to send mouse events like aqua window
9714 buttons to the correct handler. */
9715 if (SendEventToEventTarget (eventRef, toolbox_dispatcher)
9716 != eventNotHandledErr)
9717 break;
9718 #endif
9719 last_mouse_glyph_frame = 0;
9720
9721 if (dpyinfo->grabbed && last_mouse_frame
9722 && FRAME_LIVE_P (last_mouse_frame))
9723 {
9724 window_ptr = FRAME_MAC_WINDOW (last_mouse_frame);
9725 part_code = inContent;
9726 }
9727 else
9728 {
9729 part_code = FindWindow (er.where, &window_ptr);
9730 if (tip_window && window_ptr == tip_window)
9731 {
9732 HideWindow (tip_window);
9733 part_code = FindWindow (er.where, &window_ptr);
9734 }
9735 }
9736
9737 if (er.what != mouseDown &&
9738 (part_code != inContent || dpyinfo->grabbed == 0))
9739 break;
9740
9741 switch (part_code)
9742 {
9743 case inMenuBar:
9744 f = mac_focus_frame (dpyinfo);
9745 saved_menu_event_location = er.where;
9746 inev.kind = MENU_BAR_ACTIVATE_EVENT;
9747 XSETFRAME (inev.frame_or_window, f);
9748 break;
9749
9750 case inContent:
9751 if (window_ptr != FRAME_MAC_WINDOW (mac_focus_frame (dpyinfo)))
9752 SelectWindow (window_ptr);
9753 else
9754 {
9755 ControlPartCode control_part_code;
9756 ControlHandle ch;
9757 Point mouse_loc = er.where;
9758 #ifdef MAC_OSX
9759 ControlKind control_kind;
9760 #endif
9761
9762 f = mac_window_to_frame (window_ptr);
9763 /* convert to local coordinates of new window */
9764 SetPortWindowPort (window_ptr);
9765
9766 GlobalToLocal (&mouse_loc);
9767 #if TARGET_API_MAC_CARBON
9768 ch = FindControlUnderMouse (mouse_loc, window_ptr,
9769 &control_part_code);
9770 #ifdef MAC_OSX
9771 if (ch)
9772 GetControlKind (ch, &control_kind);
9773 #endif
9774 #else
9775 control_part_code = FindControl (mouse_loc, window_ptr,
9776 &ch);
9777 #endif
9778
9779 #if USE_CARBON_EVENTS
9780 inev.code = mac_get_mouse_btn (eventRef);
9781 inev.modifiers = mac_event_to_emacs_modifiers (eventRef);
9782 #else
9783 inev.code = mac_get_emulated_btn (er.modifiers);
9784 inev.modifiers = mac_to_emacs_modifiers (er.modifiers);
9785 #endif
9786 XSETINT (inev.x, mouse_loc.h);
9787 XSETINT (inev.y, mouse_loc.v);
9788
9789 if ((dpyinfo->grabbed && tracked_scroll_bar)
9790 || (ch != 0
9791 #ifndef USE_TOOLKIT_SCROLL_BARS
9792 /* control_part_code becomes kControlNoPart if
9793 a progress indicator is clicked. */
9794 && control_part_code != kControlNoPart
9795 #else /* USE_TOOLKIT_SCROLL_BARS */
9796 #ifdef MAC_OSX
9797 && control_kind.kind == kControlKindScrollBar
9798 #endif /* MAC_OSX */
9799 #endif /* USE_TOOLKIT_SCROLL_BARS */
9800 ))
9801 {
9802 struct scroll_bar *bar;
9803
9804 if (dpyinfo->grabbed && tracked_scroll_bar)
9805 {
9806 bar = tracked_scroll_bar;
9807 #ifndef USE_TOOLKIT_SCROLL_BARS
9808 control_part_code = kControlIndicatorPart;
9809 #endif
9810 }
9811 else
9812 bar = (struct scroll_bar *) GetControlReference (ch);
9813 #ifdef USE_TOOLKIT_SCROLL_BARS
9814 /* Make the "Ctrl-Mouse-2 splits window" work
9815 for toolkit scroll bars. */
9816 if (er.modifiers & controlKey)
9817 x_scroll_bar_handle_click (bar, control_part_code,
9818 &er, &inev);
9819 else if (er.what == mouseDown)
9820 x_scroll_bar_handle_press (bar, control_part_code,
9821 &inev);
9822 else
9823 x_scroll_bar_handle_release (bar, &inev);
9824 #else /* not USE_TOOLKIT_SCROLL_BARS */
9825 x_scroll_bar_handle_click (bar, control_part_code,
9826 &er, &inev);
9827 if (er.what == mouseDown
9828 && control_part_code == kControlIndicatorPart)
9829 tracked_scroll_bar = bar;
9830 else
9831 tracked_scroll_bar = NULL;
9832 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9833 }
9834 else
9835 {
9836 Lisp_Object window;
9837 int x = mouse_loc.h;
9838 int y = mouse_loc.v;
9839
9840 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
9841 if (EQ (window, f->tool_bar_window))
9842 {
9843 if (er.what == mouseDown)
9844 handle_tool_bar_click (f, x, y, 1, 0);
9845 else
9846 handle_tool_bar_click (f, x, y, 0,
9847 inev.modifiers);
9848 tool_bar_p = 1;
9849 }
9850 else
9851 {
9852 XSETFRAME (inev.frame_or_window, f);
9853 inev.kind = MOUSE_CLICK_EVENT;
9854 }
9855 }
9856
9857 if (er.what == mouseDown)
9858 {
9859 dpyinfo->grabbed |= (1 << inev.code);
9860 last_mouse_frame = f;
9861
9862 if (!tool_bar_p)
9863 last_tool_bar_item = -1;
9864 }
9865 else
9866 {
9867 if ((dpyinfo->grabbed & (1 << inev.code)) == 0)
9868 /* If a button is released though it was not
9869 previously pressed, that would be because
9870 of multi-button emulation. */
9871 dpyinfo->grabbed = 0;
9872 else
9873 dpyinfo->grabbed &= ~(1 << inev.code);
9874 }
9875
9876 /* Ignore any mouse motion that happened before
9877 this event; any subsequent mouse-movement Emacs
9878 events should reflect only motion after the
9879 ButtonPress. */
9880 if (f != 0)
9881 f->mouse_moved = 0;
9882
9883 #ifdef USE_TOOLKIT_SCROLL_BARS
9884 if (inev.kind == MOUSE_CLICK_EVENT)
9885 #endif
9886 switch (er.what)
9887 {
9888 case mouseDown:
9889 inev.modifiers |= down_modifier;
9890 break;
9891 case mouseUp:
9892 inev.modifiers |= up_modifier;
9893 break;
9894 }
9895 }
9896 break;
9897
9898 case inDrag:
9899 #if TARGET_API_MAC_CARBON
9900 case inProxyIcon:
9901 if (IsWindowPathSelectClick (window_ptr, &er))
9902 {
9903 WindowPathSelect (window_ptr, NULL, NULL);
9904 break;
9905 }
9906 if (part_code == inProxyIcon
9907 && (TrackWindowProxyDrag (window_ptr, er.where)
9908 != errUserWantsToDragWindow))
9909 break;
9910 DragWindow (window_ptr, er.where, NULL);
9911 #else /* not TARGET_API_MAC_CARBON */
9912 DragWindow (window_ptr, er.where, &qd.screenBits.bounds);
9913 #endif /* not TARGET_API_MAC_CARBON */
9914 /* Update the frame parameters. */
9915 {
9916 struct frame *f = mac_window_to_frame (window_ptr);
9917
9918 if (f && !f->async_iconified)
9919 x_real_positions (f, &f->left_pos, &f->top_pos);
9920 }
9921 break;
9922
9923 case inGoAway:
9924 if (TrackGoAway (window_ptr, er.where))
9925 {
9926 inev.kind = DELETE_WINDOW_EVENT;
9927 XSETFRAME (inev.frame_or_window,
9928 mac_window_to_frame (window_ptr));
9929 }
9930 break;
9931
9932 /* window resize handling added --ben */
9933 case inGrow:
9934 do_grow_window (window_ptr, &er);
9935 break;
9936
9937 /* window zoom handling added --ben */
9938 case inZoomIn:
9939 case inZoomOut:
9940 if (TrackBox (window_ptr, er.where, part_code))
9941 do_zoom_window (window_ptr, part_code);
9942 break;
9943
9944 default:
9945 break;
9946 }
9947 }
9948 break;
9949
9950 case updateEvt:
9951 #if USE_CARBON_EVENTS
9952 if (SendEventToEventTarget (eventRef, toolbox_dispatcher)
9953 != eventNotHandledErr)
9954 break;
9955 #else
9956 do_window_update ((WindowPtr) er.message);
9957 #endif
9958 break;
9959
9960 case osEvt:
9961 #if USE_CARBON_EVENTS
9962 if (SendEventToEventTarget (eventRef, toolbox_dispatcher)
9963 != eventNotHandledErr)
9964 break;
9965 #endif
9966 switch ((er.message >> 24) & 0x000000FF)
9967 {
9968 case suspendResumeMessage:
9969 if ((er.message & resumeFlag) == 1)
9970 do_app_resume ();
9971 else
9972 do_app_suspend ();
9973 break;
9974
9975 case mouseMovedMessage:
9976 #if !USE_CARBON_EVENTS
9977 SetRectRgn (mouse_region, er.where.h, er.where.v,
9978 er.where.h + 1, er.where.v + 1);
9979 #endif
9980 previous_help_echo_string = help_echo_string;
9981 help_echo_string = Qnil;
9982
9983 if (dpyinfo->grabbed && last_mouse_frame
9984 && FRAME_LIVE_P (last_mouse_frame))
9985 f = last_mouse_frame;
9986 else
9987 f = dpyinfo->x_focus_frame;
9988
9989 if (dpyinfo->mouse_face_hidden)
9990 {
9991 dpyinfo->mouse_face_hidden = 0;
9992 clear_mouse_face (dpyinfo);
9993 }
9994
9995 if (f)
9996 {
9997 WindowPtr wp = FRAME_MAC_WINDOW (f);
9998 Point mouse_pos = er.where;
9999
10000 SetPortWindowPort (wp);
10001
10002 GlobalToLocal (&mouse_pos);
10003
10004 if (dpyinfo->grabbed && tracked_scroll_bar)
10005 #ifdef USE_TOOLKIT_SCROLL_BARS
10006 x_scroll_bar_handle_drag (wp, tracked_scroll_bar,
10007 mouse_pos, &inev);
10008 #else /* not USE_TOOLKIT_SCROLL_BARS */
10009 x_scroll_bar_note_movement (tracked_scroll_bar,
10010 mouse_pos.v
10011 - XINT (tracked_scroll_bar->top),
10012 er.when * (1000 / 60));
10013 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10014 else
10015 {
10016 /* Generate SELECT_WINDOW_EVENTs when needed. */
10017 if (mouse_autoselect_window)
10018 {
10019 Lisp_Object window;
10020
10021 window = window_from_coordinates (f,
10022 mouse_pos.h,
10023 mouse_pos.v,
10024 0, 0, 0, 0);
10025
10026 /* Window will be selected only when it is
10027 not selected now and last mouse movement
10028 event was not in it. Minibuffer window
10029 will be selected iff it is active. */
10030 if (WINDOWP (window)
10031 && !EQ (window, last_window)
10032 && !EQ (window, selected_window))
10033 {
10034 inev.kind = SELECT_WINDOW_EVENT;
10035 inev.frame_or_window = window;
10036 }
10037
10038 last_window=window;
10039 }
10040 if (!note_mouse_movement (f, &mouse_pos))
10041 help_echo_string = previous_help_echo_string;
10042 }
10043 }
10044
10045 /* If the contents of the global variable
10046 help_echo_string has changed, generate a
10047 HELP_EVENT. */
10048 if (!NILP (help_echo_string) || !NILP (previous_help_echo_string))
10049 do_help = 1;
10050 break;
10051 }
10052 break;
10053
10054 case activateEvt:
10055 {
10056 WindowPtr window_ptr = (WindowPtr) er.message;
10057
10058 #if USE_CARBON_EVENTS
10059 if (SendEventToEventTarget (eventRef, toolbox_dispatcher)
10060 != eventNotHandledErr)
10061 break;
10062 #endif
10063 if (window_ptr == tip_window)
10064 {
10065 HideWindow (tip_window);
10066 break;
10067 }
10068
10069 if (!is_emacs_window (window_ptr))
10070 break;
10071
10072 if ((er.modifiers & activeFlag) != 0)
10073 {
10074 /* A window has been activated */
10075 Point mouse_loc = er.where;
10076
10077 x_detect_focus_change (dpyinfo, &er, &inev);
10078
10079 SetPortWindowPort (window_ptr);
10080 GlobalToLocal (&mouse_loc);
10081 /* Window-activated event counts as mouse movement,
10082 so update things that depend on mouse position. */
10083 note_mouse_movement (mac_window_to_frame (window_ptr),
10084 &mouse_loc);
10085 }
10086 else
10087 {
10088 /* A window has been deactivated */
10089 #if USE_TOOLKIT_SCROLL_BARS
10090 if (dpyinfo->grabbed && tracked_scroll_bar)
10091 {
10092 struct input_event event;
10093
10094 EVENT_INIT (event);
10095 event.kind = NO_EVENT;
10096 x_scroll_bar_handle_release (tracked_scroll_bar, &event);
10097 if (event.kind != NO_EVENT)
10098 {
10099 event.timestamp = timestamp;
10100 kbd_buffer_store_event_hold (&event, hold_quit);
10101 count++;
10102 }
10103 }
10104 #endif
10105 dpyinfo->grabbed = 0;
10106
10107 x_detect_focus_change (dpyinfo, &er, &inev);
10108
10109 f = mac_window_to_frame (window_ptr);
10110 if (f == dpyinfo->mouse_face_mouse_frame)
10111 {
10112 /* If we move outside the frame, then we're
10113 certainly no longer on any text in the
10114 frame. */
10115 clear_mouse_face (dpyinfo);
10116 dpyinfo->mouse_face_mouse_frame = 0;
10117 }
10118
10119 /* Generate a nil HELP_EVENT to cancel a help-echo.
10120 Do it only if there's something to cancel.
10121 Otherwise, the startup message is cleared when the
10122 mouse leaves the frame. */
10123 if (any_help_event_p)
10124 do_help = -1;
10125 }
10126 }
10127 break;
10128
10129 case keyDown:
10130 case autoKey:
10131 {
10132 int keycode = (er.message & keyCodeMask) >> 8;
10133 int xkeysym;
10134
10135 #if USE_CARBON_EVENTS && defined (MAC_OSX)
10136 /* When using Carbon Events, we need to pass raw keyboard
10137 events to the TSM ourselves. If TSM handles it, it
10138 will pass back noErr, otherwise it will pass back
10139 "eventNotHandledErr" and we can process it
10140 normally. */
10141 if ((mac_pass_command_to_system
10142 || !(er.modifiers & cmdKey))
10143 && (mac_pass_control_to_system
10144 || !(er.modifiers & controlKey))
10145 && (NILP (Vmac_option_modifier)
10146 || !(er.modifiers & optionKey)))
10147 if (SendEventToEventTarget (eventRef, toolbox_dispatcher)
10148 != eventNotHandledErr)
10149 break;
10150 #endif
10151
10152 #if 0
10153 if (dpyinfo->x_focus_frame == NULL)
10154 {
10155 /* Beep if keyboard input occurs when all the frames
10156 are invisible. */
10157 SysBeep (1);
10158 break;
10159 }
10160 #endif
10161
10162 {
10163 static SInt16 last_key_script = -1;
10164 SInt16 current_key_script = GetScriptManagerVariable (smKeyScript);
10165
10166 if (last_key_script != current_key_script)
10167 {
10168 struct input_event event;
10169
10170 EVENT_INIT (event);
10171 event.kind = LANGUAGE_CHANGE_EVENT;
10172 event.arg = Qnil;
10173 event.code = current_key_script;
10174 event.timestamp = timestamp;
10175 kbd_buffer_store_event (&event);
10176 count++;
10177 }
10178 last_key_script = current_key_script;
10179 }
10180
10181 ObscureCursor ();
10182
10183 f = mac_focus_frame (dpyinfo);
10184
10185 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
10186 && !EQ (f->tool_bar_window, dpyinfo->mouse_face_window))
10187 {
10188 clear_mouse_face (dpyinfo);
10189 dpyinfo->mouse_face_hidden = 1;
10190 }
10191
10192 /* translate the keycode back to determine the original key */
10193 /* Convert key code if function key is pressed.
10194 Otherwise, if non-ASCII-event, take care of that
10195 without re-translating the key code. */
10196 #if USE_CARBON_EVENTS
10197 if (convert_fn_keycode (eventRef, keycode, &xkeysym))
10198 {
10199 inev.code = xkeysym;
10200 /* this doesn't work - tried to add shift modifiers */
10201 inev.code =
10202 backtranslate_modified_keycode(er.modifiers & (~0x2200),
10203 xkeysym | 0x80, xkeysym);
10204 inev.kind = ASCII_KEYSTROKE_EVENT;
10205 }
10206 else
10207 #endif
10208 if (keycode_to_xkeysym (keycode, &xkeysym))
10209 {
10210 inev.code = 0xff00 | xkeysym;
10211 inev.kind = NON_ASCII_KEYSTROKE_EVENT;
10212 }
10213 else
10214 {
10215 inev.code =
10216 backtranslate_modified_keycode(er.modifiers, keycode,
10217 er.message & charCodeMask);
10218 inev.kind = ASCII_KEYSTROKE_EVENT;
10219 }
10220 }
10221
10222 #if USE_CARBON_EVENTS
10223 inev.modifiers = mac_event_to_emacs_modifiers (eventRef);
10224 #else
10225 inev.modifiers = mac_to_emacs_modifiers (er.modifiers);
10226 #endif
10227 inev.modifiers |= (extra_keyboard_modifiers
10228 & (meta_modifier | alt_modifier
10229 | hyper_modifier | super_modifier));
10230 XSETFRAME (inev.frame_or_window, f);
10231 break;
10232
10233 case kHighLevelEvent:
10234 read_socket_inev = &inev;
10235 AEProcessAppleEvent (&er);
10236 read_socket_inev = NULL;
10237 break;
10238
10239 default:
10240 break;
10241 }
10242 #if USE_CARBON_EVENTS
10243 ReleaseEvent (eventRef);
10244 #endif
10245
10246 if (inev.kind != NO_EVENT)
10247 {
10248 inev.timestamp = timestamp;
10249 kbd_buffer_store_event_hold (&inev, hold_quit);
10250 count++;
10251 }
10252
10253 if (do_help
10254 && !(hold_quit && hold_quit->kind != NO_EVENT))
10255 {
10256 Lisp_Object frame;
10257
10258 if (f)
10259 XSETFRAME (frame, f);
10260 else
10261 frame = Qnil;
10262
10263 if (do_help > 0)
10264 {
10265 any_help_event_p = 1;
10266 gen_help_event (help_echo_string, frame, help_echo_window,
10267 help_echo_object, help_echo_pos);
10268 }
10269 else
10270 {
10271 help_echo_string = Qnil;
10272 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
10273 }
10274 count++;
10275 }
10276
10277 }
10278
10279 /* If the focus was just given to an autoraising frame,
10280 raise it now. */
10281 /* ??? This ought to be able to handle more than one such frame. */
10282 if (pending_autoraise_frame)
10283 {
10284 x_raise_frame (pending_autoraise_frame);
10285 pending_autoraise_frame = 0;
10286 }
10287
10288 #if !USE_CARBON_EVENTS
10289 /* Check which frames are still visible. We do this here because
10290 there doesn't seem to be any direct notification from the Window
10291 Manager that the visibility of a window has changed (at least,
10292 not in all cases). */
10293 {
10294 Lisp_Object tail, frame;
10295
10296 FOR_EACH_FRAME (tail, frame)
10297 {
10298 struct frame *f = XFRAME (frame);
10299
10300 /* The tooltip has been drawn already. Avoid the
10301 SET_FRAME_GARBAGED in mac_handle_visibility_change. */
10302 if (EQ (frame, tip_frame))
10303 continue;
10304
10305 if (FRAME_MAC_P (f))
10306 mac_handle_visibility_change (f);
10307 }
10308 }
10309 #endif
10310
10311 --handling_signal;
10312 UNBLOCK_INPUT;
10313 return count;
10314 }
10315
10316
10317 /* Need to override CodeWarrior's input function so no conversion is
10318 done on newlines Otherwise compiled functions in .elc files will be
10319 read incorrectly. Defined in ...:MSL C:MSL
10320 Common:Source:buffer_io.c. */
10321 #ifdef __MWERKS__
10322 void
10323 __convert_to_newlines (unsigned char * p, size_t * n)
10324 {
10325 #pragma unused(p,n)
10326 }
10327
10328 void
10329 __convert_from_newlines (unsigned char * p, size_t * n)
10330 {
10331 #pragma unused(p,n)
10332 }
10333 #endif
10334
10335 #ifdef MAC_OS8
10336 void
10337 make_mac_terminal_frame (struct frame *f)
10338 {
10339 Lisp_Object frame;
10340 Rect r;
10341
10342 XSETFRAME (frame, f);
10343
10344 f->output_method = output_mac;
10345 f->output_data.mac = (struct mac_output *)
10346 xmalloc (sizeof (struct mac_output));
10347 bzero (f->output_data.mac, sizeof (struct mac_output));
10348
10349 XSETFRAME (FRAME_KBOARD (f)->Vdefault_minibuffer_frame, f);
10350
10351 FRAME_COLS (f) = 96;
10352 FRAME_LINES (f) = 4;
10353
10354 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
10355 FRAME_VERTICAL_SCROLL_BAR_TYPE (f) = vertical_scroll_bar_right;
10356
10357 FRAME_DESIRED_CURSOR (f) = FILLED_BOX_CURSOR;
10358
10359 f->output_data.mac->cursor_pixel = 0;
10360 f->output_data.mac->border_pixel = 0x00ff00;
10361 f->output_data.mac->mouse_pixel = 0xff00ff;
10362 f->output_data.mac->cursor_foreground_pixel = 0x0000ff;
10363
10364 f->output_data.mac->text_cursor = kThemeIBeamCursor;
10365 f->output_data.mac->nontext_cursor = kThemeArrowCursor;
10366 f->output_data.mac->modeline_cursor = kThemeArrowCursor;
10367 f->output_data.mac->hand_cursor = kThemePointingHandCursor;
10368 f->output_data.mac->hourglass_cursor = kThemeWatchCursor;
10369 f->output_data.mac->horizontal_drag_cursor = kThemeResizeLeftRightCursor;
10370
10371 FRAME_FONTSET (f) = -1;
10372 f->output_data.mac->explicit_parent = 0;
10373 f->left_pos = 8;
10374 f->top_pos = 32;
10375 f->border_width = 0;
10376
10377 f->internal_border_width = 0;
10378
10379 f->auto_raise = 1;
10380 f->auto_lower = 1;
10381
10382 f->new_text_cols = 0;
10383 f->new_text_lines = 0;
10384
10385 SetRect (&r, f->left_pos, f->top_pos,
10386 f->left_pos + FRAME_PIXEL_WIDTH (f),
10387 f->top_pos + FRAME_PIXEL_HEIGHT (f));
10388
10389 BLOCK_INPUT;
10390
10391 if (!(FRAME_MAC_WINDOW (f) =
10392 NewCWindow (NULL, &r, "\p", true, dBoxProc,
10393 (WindowPtr) -1, 1, (long) f->output_data.mac)))
10394 abort ();
10395 /* so that update events can find this mac_output struct */
10396 f->output_data.mac->mFP = f; /* point back to emacs frame */
10397
10398 UNBLOCK_INPUT;
10399
10400 x_make_gc (f);
10401
10402 /* Need to be initialized for unshow_buffer in window.c. */
10403 selected_window = f->selected_window;
10404
10405 Fmodify_frame_parameters (frame,
10406 Fcons (Fcons (Qfont,
10407 build_string ("-*-monaco-medium-r-*--*-90-*-*-*-*-mac-roman")), Qnil));
10408 Fmodify_frame_parameters (frame,
10409 Fcons (Fcons (Qforeground_color,
10410 build_string ("black")), Qnil));
10411 Fmodify_frame_parameters (frame,
10412 Fcons (Fcons (Qbackground_color,
10413 build_string ("white")), Qnil));
10414 }
10415 #endif
10416
10417 \f
10418 /***********************************************************************
10419 Initialization
10420 ***********************************************************************/
10421
10422 int mac_initialized = 0;
10423
10424 void
10425 mac_initialize_display_info ()
10426 {
10427 struct mac_display_info *dpyinfo = &one_mac_display_info;
10428 GDHandle main_device_handle;
10429
10430 bzero (dpyinfo, sizeof (*dpyinfo));
10431
10432 #ifdef MAC_OSX
10433 dpyinfo->mac_id_name
10434 = (char *) xmalloc (SCHARS (Vinvocation_name)
10435 + SCHARS (Vsystem_name)
10436 + 2);
10437 sprintf (dpyinfo->mac_id_name, "%s@%s",
10438 SDATA (Vinvocation_name), SDATA (Vsystem_name));
10439 #else
10440 dpyinfo->mac_id_name = (char *) xmalloc (strlen ("Mac Display") + 1);
10441 strcpy (dpyinfo->mac_id_name, "Mac Display");
10442 #endif
10443
10444 main_device_handle = LMGetMainDevice();
10445
10446 dpyinfo->reference_count = 0;
10447 dpyinfo->resx = 72.0;
10448 dpyinfo->resy = 72.0;
10449 dpyinfo->color_p = TestDeviceAttribute (main_device_handle, gdDevType);
10450 #ifdef MAC_OSX
10451 /* HasDepth returns true if it is possible to have a 32 bit display,
10452 but this may not be what is actually used. Mac OSX can do better.
10453 CGMainDisplayID is only available on OSX 10.2 and higher, but the
10454 header for CGGetActiveDisplayList says that the first display returned
10455 is the active one, so we use that. */
10456 {
10457 CGDirectDisplayID disp_id[1];
10458 CGDisplayCount disp_count;
10459 CGDisplayErr error_code;
10460
10461 error_code = CGGetActiveDisplayList (1, disp_id, &disp_count);
10462 if (error_code != 0)
10463 error ("No display found, CGGetActiveDisplayList error %d", error_code);
10464
10465 dpyinfo->n_planes = CGDisplayBitsPerPixel (disp_id[0]);
10466 }
10467 #else
10468 for (dpyinfo->n_planes = 32; dpyinfo->n_planes > 0; dpyinfo->n_planes >>= 1)
10469 if (HasDepth (main_device_handle, dpyinfo->n_planes,
10470 gdDevType, dpyinfo->color_p))
10471 break;
10472 #endif
10473 dpyinfo->height = (**main_device_handle).gdRect.bottom;
10474 dpyinfo->width = (**main_device_handle).gdRect.right;
10475 dpyinfo->grabbed = 0;
10476 dpyinfo->root_window = NULL;
10477 dpyinfo->image_cache = make_image_cache ();
10478
10479 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10480 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
10481 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10482 dpyinfo->mouse_face_window = Qnil;
10483 dpyinfo->mouse_face_overlay = Qnil;
10484 dpyinfo->mouse_face_hidden = 0;
10485 }
10486
10487
10488 static XrmDatabase
10489 mac_make_rdb (xrm_option)
10490 char *xrm_option;
10491 {
10492 XrmDatabase database;
10493
10494 database = xrm_get_preference_database (NULL);
10495 if (xrm_option)
10496 xrm_merge_string_database (database, xrm_option);
10497
10498 return database;
10499 }
10500
10501 struct mac_display_info *
10502 mac_term_init (display_name, xrm_option, resource_name)
10503 Lisp_Object display_name;
10504 char *xrm_option;
10505 char *resource_name;
10506 {
10507 struct mac_display_info *dpyinfo;
10508
10509 BLOCK_INPUT;
10510
10511 if (!mac_initialized)
10512 {
10513 mac_initialize ();
10514 mac_initialized = 1;
10515 }
10516
10517 if (x_display_list)
10518 error ("Sorry, this version can only handle one display");
10519
10520 mac_initialize_display_info ();
10521
10522 dpyinfo = &one_mac_display_info;
10523
10524 dpyinfo->xrdb = mac_make_rdb (xrm_option);
10525
10526 /* Put this display on the chain. */
10527 dpyinfo->next = x_display_list;
10528 x_display_list = dpyinfo;
10529
10530 /* Put it on x_display_name_list. */
10531 x_display_name_list = Fcons (Fcons (display_name,
10532 Fcons (Qnil, dpyinfo->xrdb)),
10533 x_display_name_list);
10534 dpyinfo->name_list_element = XCAR (x_display_name_list);
10535
10536 UNBLOCK_INPUT;
10537
10538 return dpyinfo;
10539 }
10540 /* Get rid of display DPYINFO, assuming all frames are already gone. */
10541
10542 void
10543 x_delete_display (dpyinfo)
10544 struct mac_display_info *dpyinfo;
10545 {
10546 int i;
10547
10548 /* Discard this display from x_display_name_list and x_display_list.
10549 We can't use Fdelq because that can quit. */
10550 if (! NILP (x_display_name_list)
10551 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
10552 x_display_name_list = XCDR (x_display_name_list);
10553 else
10554 {
10555 Lisp_Object tail;
10556
10557 tail = x_display_name_list;
10558 while (CONSP (tail) && CONSP (XCDR (tail)))
10559 {
10560 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
10561 {
10562 XSETCDR (tail, XCDR (XCDR (tail)));
10563 break;
10564 }
10565 tail = XCDR (tail);
10566 }
10567 }
10568
10569 if (x_display_list == dpyinfo)
10570 x_display_list = dpyinfo->next;
10571 else
10572 {
10573 struct x_display_info *tail;
10574
10575 for (tail = x_display_list; tail; tail = tail->next)
10576 if (tail->next == dpyinfo)
10577 tail->next = tail->next->next;
10578 }
10579
10580 /* Free the font names in the font table. */
10581 for (i = 0; i < dpyinfo->n_fonts; i++)
10582 if (dpyinfo->font_table[i].name)
10583 {
10584 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
10585 xfree (dpyinfo->font_table[i].full_name);
10586 xfree (dpyinfo->font_table[i].name);
10587 }
10588
10589 if (dpyinfo->font_table->font_encoder)
10590 xfree (dpyinfo->font_table->font_encoder);
10591
10592 xfree (dpyinfo->font_table);
10593 xfree (dpyinfo->mac_id_name);
10594
10595 if (x_display_list == 0)
10596 {
10597 mac_clear_font_name_table ();
10598 bzero (dpyinfo, sizeof (*dpyinfo));
10599 }
10600 }
10601
10602 \f
10603 #ifdef MAC_OSX
10604 void
10605 mac_check_bundle()
10606 {
10607 extern int inhibit_window_system;
10608 extern int noninteractive;
10609 CFBundleRef appsBundle;
10610
10611 /* No need to test if already -nw*/
10612 if (inhibit_window_system || noninteractive)
10613 return;
10614
10615 appsBundle = CFBundleGetMainBundle();
10616 if (appsBundle != NULL)
10617 {
10618 CFStringRef cfBI = CFSTR("CFBundleIdentifier");
10619 CFTypeRef res = CFBundleGetValueForInfoDictionaryKey(appsBundle, cfBI);
10620 /* We found the bundle identifier, now we know we are valid. */
10621 if (res != NULL)
10622 {
10623 CFRelease(res);
10624 return;
10625 }
10626 }
10627 /* MAC_TODO: Have this start the bundled executable */
10628
10629 /* For now, prevent the fatal error by bringing it up in the terminal */
10630 inhibit_window_system = 1;
10631 }
10632
10633 void
10634 MakeMeTheFrontProcess ()
10635 {
10636 ProcessSerialNumber psn;
10637 OSErr err;
10638
10639 err = GetCurrentProcess (&psn);
10640 if (err == noErr)
10641 (void) SetFrontProcess (&psn);
10642 }
10643
10644 /***** Code to handle C-g testing *****/
10645
10646 /* Contains the Mac modifier formed from quit_char */
10647 int mac_quit_char_modifiers = 0;
10648 int mac_quit_char_keycode;
10649 extern int quit_char;
10650
10651 static void
10652 mac_determine_quit_char_modifiers()
10653 {
10654 /* Todo: Determine modifiers from quit_char. */
10655 UInt32 qc_modifiers = ctrl_modifier;
10656
10657 /* Map modifiers */
10658 mac_quit_char_modifiers = 0;
10659 if (qc_modifiers & ctrl_modifier) mac_quit_char_modifiers |= controlKey;
10660 if (qc_modifiers & shift_modifier) mac_quit_char_modifiers |= shiftKey;
10661 if (qc_modifiers & alt_modifier) mac_quit_char_modifiers |= optionKey;
10662 }
10663
10664 static void
10665 init_quit_char_handler ()
10666 {
10667 /* TODO: Let this support keys other the 'g' */
10668 mac_quit_char_keycode = 5;
10669 /* Look at <architecture/adb_kb_map.h> for details */
10670 /* http://gemma.apple.com/techpubs/mac/Toolbox/Toolbox-40.html#MARKER-9-184*/
10671
10672 mac_determine_quit_char_modifiers();
10673 }
10674 #endif /* MAC_OSX */
10675
10676 static void
10677 init_menu_bar ()
10678 {
10679 #ifdef MAC_OSX
10680 OSErr err;
10681 MenuRef menu;
10682 MenuItemIndex menu_index;
10683
10684 err = GetIndMenuItemWithCommandID (NULL, kHICommandQuit, 1,
10685 &menu, &menu_index);
10686 if (err == noErr)
10687 SetMenuItemCommandKey (menu, menu_index, false, 0);
10688 #if USE_CARBON_EVENTS
10689 EnableMenuCommand (NULL, kHICommandPreferences);
10690 err = GetIndMenuItemWithCommandID (NULL, kHICommandPreferences, 1,
10691 &menu, &menu_index);
10692 if (err == noErr)
10693 {
10694 SetMenuItemCommandKey (menu, menu_index, false, 0);
10695 InsertMenuItemTextWithCFString (menu, NULL,
10696 0, kMenuItemAttrSeparator, 0);
10697 InsertMenuItemTextWithCFString (menu, CFSTR ("About Emacs"),
10698 0, 0, kHICommandAbout);
10699 }
10700 #endif /* USE_CARBON_EVENTS */
10701 #else /* !MAC_OSX */
10702 #if USE_CARBON_EVENTS
10703 SetMenuItemCommandID (GetMenuHandle (M_APPLE), I_ABOUT, kHICommandAbout);
10704 #endif
10705 #endif
10706 }
10707
10708
10709 /* Set up use of X before we make the first connection. */
10710
10711 extern frame_parm_handler mac_frame_parm_handlers[];
10712
10713 static struct redisplay_interface x_redisplay_interface =
10714 {
10715 mac_frame_parm_handlers,
10716 x_produce_glyphs,
10717 x_write_glyphs,
10718 x_insert_glyphs,
10719 x_clear_end_of_line,
10720 x_scroll_run,
10721 x_after_update_window_line,
10722 x_update_window_begin,
10723 x_update_window_end,
10724 x_cursor_to,
10725 x_flush,
10726 0, /* flush_display_optional */
10727 x_clear_window_mouse_face,
10728 x_get_glyph_overhangs,
10729 x_fix_overlapping_area,
10730 x_draw_fringe_bitmap,
10731 #if USE_CG_DRAWING
10732 mac_define_fringe_bitmap,
10733 mac_destroy_fringe_bitmap,
10734 #else
10735 0, /* define_fringe_bitmap */
10736 0, /* destroy_fringe_bitmap */
10737 #endif
10738 mac_per_char_metric,
10739 mac_encode_char,
10740 mac_compute_glyph_string_overhangs,
10741 x_draw_glyph_string,
10742 mac_define_frame_cursor,
10743 mac_clear_frame_area,
10744 mac_draw_window_cursor,
10745 mac_draw_vertical_window_border,
10746 mac_shift_glyphs_for_insert
10747 };
10748
10749 void
10750 mac_initialize ()
10751 {
10752 rif = &x_redisplay_interface;
10753
10754 clear_frame_hook = x_clear_frame;
10755 ins_del_lines_hook = x_ins_del_lines;
10756 delete_glyphs_hook = x_delete_glyphs;
10757 ring_bell_hook = XTring_bell;
10758 reset_terminal_modes_hook = XTreset_terminal_modes;
10759 set_terminal_modes_hook = XTset_terminal_modes;
10760 update_begin_hook = x_update_begin;
10761 update_end_hook = x_update_end;
10762 set_terminal_window_hook = XTset_terminal_window;
10763 read_socket_hook = XTread_socket;
10764 frame_up_to_date_hook = XTframe_up_to_date;
10765 mouse_position_hook = XTmouse_position;
10766 frame_rehighlight_hook = XTframe_rehighlight;
10767 frame_raise_lower_hook = XTframe_raise_lower;
10768
10769 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
10770 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
10771 redeem_scroll_bar_hook = XTredeem_scroll_bar;
10772 judge_scroll_bars_hook = XTjudge_scroll_bars;
10773
10774 scroll_region_ok = 1; /* we'll scroll partial frames */
10775 char_ins_del_ok = 1;
10776 line_ins_del_ok = 1; /* we'll just blt 'em */
10777 fast_clear_end_of_line = 1; /* X does this well */
10778 memory_below_frame = 0; /* we don't remember what scrolls
10779 off the bottom */
10780 baud_rate = 19200;
10781
10782 last_tool_bar_item = -1;
10783 any_help_event_p = 0;
10784
10785 /* Try to use interrupt input; if we can't, then start polling. */
10786 Fset_input_mode (Qt, Qnil, Qt, Qnil);
10787
10788 BLOCK_INPUT;
10789
10790 #if TARGET_API_MAC_CARBON
10791
10792 #if USE_CARBON_EVENTS
10793 #ifdef MAC_OSX
10794 init_service_handler ();
10795
10796 init_quit_char_handler ();
10797 #endif /* MAC_OSX */
10798
10799 init_command_handler ();
10800
10801 init_menu_bar ();
10802 #endif /* USE_CARBON_EVENTS */
10803
10804 #ifdef MAC_OSX
10805 init_coercion_handler ();
10806
10807 init_apple_event_handler ();
10808
10809 if (!inhibit_window_system)
10810 MakeMeTheFrontProcess ();
10811 #endif
10812 #endif
10813
10814 #if USE_CG_DRAWING
10815 mac_init_fringe ();
10816 #endif
10817
10818 UNBLOCK_INPUT;
10819 }
10820
10821
10822 void
10823 syms_of_macterm ()
10824 {
10825 #if 0
10826 staticpro (&x_error_message_string);
10827 x_error_message_string = Qnil;
10828 #endif
10829
10830 Qcontrol = intern ("control"); staticpro (&Qcontrol);
10831 Qmeta = intern ("meta"); staticpro (&Qmeta);
10832 Qalt = intern ("alt"); staticpro (&Qalt);
10833 Qhyper = intern ("hyper"); staticpro (&Qhyper);
10834 Qsuper = intern ("super"); staticpro (&Qsuper);
10835 Qmodifier_value = intern ("modifier-value");
10836 staticpro (&Qmodifier_value);
10837
10838 Fput (Qcontrol, Qmodifier_value, make_number (ctrl_modifier));
10839 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
10840 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
10841 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
10842 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
10843
10844 #if USE_CARBON_EVENTS
10845 Qhicommand = intern ("hicommand"); staticpro (&Qhicommand);
10846 #ifdef MAC_OSX
10847 Qservices = intern ("services"); staticpro (&Qservices);
10848 Qpaste = intern ("paste"); staticpro (&Qpaste);
10849 Qperform = intern ("perform"); staticpro (&Qperform);
10850 #endif
10851 #endif
10852
10853 #ifdef MAC_OSX
10854 Fprovide (intern ("mac-carbon"), Qnil);
10855 #endif
10856
10857 staticpro (&Qreverse);
10858 Qreverse = intern ("reverse");
10859
10860 staticpro (&x_display_name_list);
10861 x_display_name_list = Qnil;
10862
10863 staticpro (&last_mouse_scroll_bar);
10864 last_mouse_scroll_bar = Qnil;
10865
10866 staticpro (&fm_font_family_alist);
10867 fm_font_family_alist = Qnil;
10868
10869 #if USE_ATSUI
10870 staticpro (&atsu_font_id_hash);
10871 atsu_font_id_hash = Qnil;
10872 #endif
10873
10874 /* We don't yet support this, but defining this here avoids whining
10875 from cus-start.el and other places, like "M-x set-variable". */
10876 DEFVAR_BOOL ("x-use-underline-position-properties",
10877 &x_use_underline_position_properties,
10878 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
10879 nil means ignore them. If you encounter fonts with bogus
10880 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
10881 to 4.1, set this to nil.
10882
10883 NOTE: Not supported on Mac yet. */);
10884 x_use_underline_position_properties = 0;
10885
10886 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
10887 doc: /* If not nil, Emacs uses toolkit scroll bars. */);
10888 #ifdef USE_TOOLKIT_SCROLL_BARS
10889 Vx_toolkit_scroll_bars = Qt;
10890 #else
10891 Vx_toolkit_scroll_bars = Qnil;
10892 #endif
10893
10894 staticpro (&last_mouse_motion_frame);
10895 last_mouse_motion_frame = Qnil;
10896
10897 /* Variables to configure modifier key assignment. */
10898
10899 DEFVAR_LISP ("mac-control-modifier", &Vmac_control_modifier,
10900 doc: /* *Modifier key assumed when the Mac control key is pressed.
10901 The value can be `control', `meta', `alt', `hyper', or `super' for the
10902 respective modifier. The default is `control'. */);
10903 Vmac_control_modifier = Qcontrol;
10904
10905 DEFVAR_LISP ("mac-option-modifier", &Vmac_option_modifier,
10906 doc: /* *Modifier key assumed when the Mac alt/option key is pressed.
10907 The value can be `control', `meta', `alt', `hyper', or `super' for the
10908 respective modifier. If the value is nil then the key will act as the
10909 normal Mac control modifier, and the option key can be used to compose
10910 characters depending on the chosen Mac keyboard setting. */);
10911 Vmac_option_modifier = Qnil;
10912
10913 DEFVAR_LISP ("mac-command-modifier", &Vmac_command_modifier,
10914 doc: /* *Modifier key assumed when the Mac command key is pressed.
10915 The value can be `control', `meta', `alt', `hyper', or `super' for the
10916 respective modifier. The default is `meta'. */);
10917 Vmac_command_modifier = Qmeta;
10918
10919 DEFVAR_LISP ("mac-function-modifier", &Vmac_function_modifier,
10920 doc: /* *Modifier key assumed when the Mac function key is pressed.
10921 The value can be `control', `meta', `alt', `hyper', or `super' for the
10922 respective modifier. Note that remapping the function key may lead to
10923 unexpected results for some keys on non-US/GB keyboards. */);
10924 Vmac_function_modifier = Qnil;
10925
10926 DEFVAR_LISP ("mac-emulate-three-button-mouse",
10927 &Vmac_emulate_three_button_mouse,
10928 doc: /* *Specify a way of three button mouse emulation.
10929 The value can be nil, t, or the symbol `reverse'.
10930 nil means that no emulation should be done and the modifiers should be
10931 placed on the mouse-1 event.
10932 t means that when the option-key is held down while pressing the mouse
10933 button, the click will register as mouse-2 and while the command-key
10934 is held down, the click will register as mouse-3.
10935 The symbol `reverse' means that the option-key will register for
10936 mouse-3 and the command-key will register for mouse-2. */);
10937 Vmac_emulate_three_button_mouse = Qnil;
10938
10939 #if USE_CARBON_EVENTS
10940 DEFVAR_BOOL ("mac-wheel-button-is-mouse-2", &mac_wheel_button_is_mouse_2,
10941 doc: /* *Non-nil if the wheel button is mouse-2 and the right click mouse-3.
10942 Otherwise, the right click will be treated as mouse-2 and the wheel
10943 button will be mouse-3. */);
10944 mac_wheel_button_is_mouse_2 = 1;
10945
10946 DEFVAR_BOOL ("mac-pass-command-to-system", &mac_pass_command_to_system,
10947 doc: /* *Non-nil if command key presses are passed on to the Mac Toolbox. */);
10948 mac_pass_command_to_system = 1;
10949
10950 DEFVAR_BOOL ("mac-pass-control-to-system", &mac_pass_control_to_system,
10951 doc: /* *Non-nil if control key presses are passed on to the Mac Toolbox. */);
10952 mac_pass_control_to_system = 1;
10953
10954 #endif
10955
10956 DEFVAR_BOOL ("mac-allow-anti-aliasing", &mac_use_core_graphics,
10957 doc: /* *If non-nil, allow anti-aliasing.
10958 The text will be rendered using Core Graphics text rendering which
10959 may anti-alias the text. */);
10960 #if USE_CG_DRAWING
10961 mac_use_core_graphics = 1;
10962 #else
10963 mac_use_core_graphics = 0;
10964 #endif
10965
10966 /* Register an entry for `mac-roman' so that it can be used when
10967 creating the terminal frame on Mac OS 9 before loading
10968 term/mac-win.elc. */
10969 DEFVAR_LISP ("mac-charset-info-alist", &Vmac_charset_info_alist,
10970 doc: /* Alist of Emacs character sets vs text encodings and coding systems.
10971 Each entry should be of the form:
10972
10973 (CHARSET-NAME TEXT-ENCODING CODING-SYSTEM)
10974
10975 where CHARSET-NAME is a string used in font names to identify the
10976 charset, TEXT-ENCODING is a TextEncodingBase value in Mac, and
10977 CODING_SYSTEM is a coding system corresponding to TEXT-ENCODING. */);
10978 Vmac_charset_info_alist =
10979 Fcons (list3 (build_string ("mac-roman"),
10980 make_number (smRoman), Qnil), Qnil);
10981 }
10982
10983 /* arch-tag: f2259165-4454-4c04-a029-a133c8af7b5b
10984 (do not change this comment) */