Keybindings for Tumme in Dired.
[bpt/emacs.git] / src / macterm.c
Content-type: text/html HCoop Git - bpt/emacs.git/blame - src/macterm.c


500 - Internal Server Error

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