macterm.c 286 KB
Newer Older
1
/* Implementation of GUI terminal on the Mac OS.
2 3
   Copyright (C) 2000, 2001, 2002, 2003, 2004,
                 2005 Free Software Foundation, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

This file is part of GNU Emacs.

GNU Emacs is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs; see the file COPYING.  If not, write to
Lute Kamstra's avatar
Lute Kamstra committed
19 20
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.  */
21

22
/* Contributed by Andrew Choi (akochoi@mac.com).  */
23 24 25

#include <config.h>
#include <signal.h>
26

27
#include <stdio.h>
28

29 30 31 32 33
#include "lisp.h"
#include "blockinput.h"

#include "macterm.h"

34
#ifndef MAC_OSX
35
#include <alloca.h>
36
#endif
37

38
#if TARGET_API_MAC_CARBON
Andrew Choi's avatar
In src:  
Andrew Choi committed
39 40 41 42
/* USE_CARBON_EVENTS determines if the Carbon Event Manager is used to
   obtain events from the event queue.  If set to 0, WaitNextEvent is
   used instead.  */
#define USE_CARBON_EVENTS 1
43
#else /* not TARGET_API_MAC_CARBON */
44 45 46 47 48 49 50 51 52 53
#include <Quickdraw.h>
#include <ToolUtils.h>
#include <Sound.h>
#include <Events.h>
#include <Script.h>
#include <Resources.h>
#include <Fonts.h>
#include <TextUtils.h>
#include <LowMem.h>
#include <Controls.h>
54
#include <Windows.h>
55
#if defined (__MRC__) || (__MSL__ >= 0x6000)
56 57 58 59 60 61
#include <ControlDefinitions.h>
#endif

#if __profile__
#include <profiler.h>
#endif
62
#endif /* not TARGET_API_MAC_CARBON */
63 64 65 66 67 68 69 70

#include "systty.h"
#include "systime.h"

#include <ctype.h>
#include <errno.h>
#include <setjmp.h>
#include <sys/stat.h>
Steven Tamm's avatar
Steven Tamm committed
71
#include <sys/param.h>
72

73 74
#include "charset.h"
#include "coding.h"
75 76 77 78 79 80 81 82 83 84
#include "frame.h"
#include "dispextern.h"
#include "fontset.h"
#include "termhooks.h"
#include "termopts.h"
#include "termchar.h"
#include "gnu.h"
#include "disptab.h"
#include "buffer.h"
#include "window.h"
85
#include "keyboard.h"
86
#include "intervals.h"
87 88
#include "atimer.h"
#include "keymap.h"
89

Andrew Choi's avatar
In src:  
Andrew Choi committed
90 91 92 93 94 95 96 97
/* Set of macros that handle mapping of Mac modifier keys to emacs.  */
#define macCtrlKey     (NILP (Vmac_reverse_ctrl_meta) ? controlKey :	\
			(NILP (Vmac_command_key_is_meta) ? optionKey : cmdKey))
#define macShiftKey    (shiftKey)
#define macMetaKey     (NILP (Vmac_reverse_ctrl_meta) ?			\
			(NILP (Vmac_command_key_is_meta) ? optionKey : cmdKey) \
			: controlKey)
#define macAltKey      (NILP (Vmac_command_key_is_meta) ? cmdKey : optionKey)
98 99


100
/* Non-nil means Emacs uses toolkit scroll bars.  */
101

102
Lisp_Object Vx_toolkit_scroll_bars;
103

104 105 106 107
/* If Non-nil, the text will be rendered using Core Graphics text rendering which may anti-alias the text.  */
Lisp_Object Vmac_use_core_graphics;


108 109 110 111 112
/* Non-zero means that a HELP_EVENT has been generated since Emacs
   start.  */

static int any_help_event_p;

113 114
/* Last window where we saw the mouse.  Used by mouse-autoselect-window.  */
static Lisp_Object last_window;
115

116 117 118 119 120 121
/* This is a chain of structures for all the X displays currently in
   use.  */

struct x_display_info *x_display_list;

/* This is a list of cons cells, each of the form (NAME
122 123 124 125 126 127
   FONT-LIST-CACHE . RESOURCE-DATABASE), one for each element of
   x_display_list and in the same order.  NAME is the name of the
   frame.  FONT-LIST-CACHE records previous values returned by
   x-list-fonts.  RESOURCE-DATABASE preserves the X Resource Database
   equivalent, which is implemented with a Lisp object, for the
   display. */
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

Lisp_Object x_display_name_list;

/* This is display since Mac does not support multiple ones.  */
struct mac_display_info one_mac_display_info;

/* Frame being updated by update_frame.  This is declared in term.c.
   This is set by update_begin and looked at by all the XT functions.
   It is zero while not inside an update.  In that case, the XT
   functions assume that `selected_frame' is the frame to apply to.  */

extern struct frame *updating_frame;

/* This is a frame waiting to be auto-raised, within XTread_socket.  */

struct frame *pending_autoraise_frame;

/* Mouse movement.

   Formerly, we used PointerMotionHintMask (in standard_event_mask)
   so that we would have to call XQueryPointer after each MotionNotify
   event to ask for another such event.  However, this made mouse tracking
   slow, and there was a bug that made it eventually stop.

   Simply asking for MotionNotify all the time seems to work better.

   In order to avoid asking for motion events and then throwing most
   of them away or busy-polling the server for mouse positions, we ask
   the server for pointer motion hints.  This means that we get only
   one event per group of mouse movements.  "Groups" are delimited by
   other kinds of events (focus changes and button clicks, for
   example), or by XQueryPointer calls; when one of these happens, we
   get another MotionNotify event the next time the mouse moves.  This
   is at least as efficient as getting motion events when mouse
   tracking is on, and I suspect only negligibly worse when tracking
   is off.  */

/* Where the mouse was last time we reported a mouse event.  */

static Rect last_mouse_glyph;
168
static FRAME_PTR last_mouse_glyph_frame;
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

/* The scroll bar in which the last X motion event occurred.

   If the last X motion event occurred in a scroll bar, we set this so
   XTmouse_position can know whether to report a scroll bar motion or
   an ordinary motion.

   If the last X motion event didn't occur in a scroll bar, we set
   this to Qnil, to tell XTmouse_position to return an ordinary motion
   event.  */

static Lisp_Object last_mouse_scroll_bar;

/* This is a hack.  We would really prefer that XTmouse_position would
   return the time associated with the position it returns, but there
   doesn't seem to be any way to wrest the time-stamp from the server
   along with the position query.  So, we just keep track of the time
   of the last movement we received, and return that in hopes that
   it's somewhat accurate.  */

static Time last_mouse_movement_time;

struct scroll_bar *tracked_scroll_bar = NULL;

/* Incremented by XTread_socket whenever it really tries to read
   events.  */

#ifdef __STDC__
static int volatile input_signal_count;
#else
static int input_signal_count;
#endif

202
extern Lisp_Object Vsystem_name;
203 204 205

/* A mask of extra modifier bits to put into every keyboard char.  */

206
extern EMACS_INT extra_keyboard_modifiers;
207

208 209 210 211
/* The keysyms to use for the various modifiers.  */

static Lisp_Object Qalt, Qhyper, Qsuper, Qmodifier_value;

212
extern int inhibit_window_system;
213

214
#if __MRC__ && !TARGET_API_MAC_CARBON
215 216 217
QDGlobals qd;  /* QuickDraw global information structure.  */
#endif

218
#define mac_window_to_frame(wp) (((mac_output *) GetWRefCon (wp))->mFP)
219 220 221 222 223 224 225 226 227 228 229 230 231 232

struct mac_display_info *mac_display_info_for_display (Display *);
static void x_update_window_end P_ ((struct window *, int, int));
static int x_io_error_quitter P_ ((Display *));
int x_catch_errors P_ ((Display *));
void x_uncatch_errors P_ ((Display *, int));
void x_lower_frame P_ ((struct frame *));
void x_scroll_bar_clear P_ ((struct frame *));
int x_had_errors_p P_ ((Display *));
void x_wm_set_size_hint P_ ((struct frame *, long, int));
void x_raise_frame P_ ((struct frame *));
void x_set_window_size P_ ((struct frame *, int, int, int));
void x_wm_set_window_state P_ ((struct frame *, int));
void x_wm_set_icon_pixmap P_ ((struct frame *, int));
233
void mac_initialize P_ ((void));
234 235 236 237 238 239 240 241 242 243
static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
static int x_compute_min_glyph_bounds P_ ((struct frame *));
static void x_update_end P_ ((struct frame *));
static void XTframe_up_to_date P_ ((struct frame *));
static void XTset_terminal_modes P_ ((void));
static void XTreset_terminal_modes P_ ((void));
static void x_clear_frame P_ ((void));
static void frame_highlight P_ ((struct frame *));
static void frame_unhighlight P_ ((struct frame *));
static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
244 245 246 247
static void mac_focus_changed P_ ((int, struct mac_display_info *,
				   struct frame *, struct input_event *));
static void x_detect_focus_change P_ ((struct mac_display_info *,
				       EventRecord *, struct input_event *));
248 249 250
static void XTframe_rehighlight P_ ((struct frame *));
static void x_frame_rehighlight P_ ((struct x_display_info *));
static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
251 252 253
static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
				   enum text_cursor_kinds));

254
static void x_clip_to_row P_ ((struct window *, struct glyph_row *, int, GC));
255 256 257 258
static void x_flush P_ ((struct frame *f));
static void x_update_begin P_ ((struct frame *));
static void x_update_window_begin P_ ((struct window *));
static void x_after_update_window_line P_ ((struct glyph_row *));
259 260 261 262
static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
					    enum scroll_bar_part *,
					    Lisp_Object *, Lisp_Object *,
					    unsigned long *));
263

264
static int is_emacs_window P_ ((WindowPtr));
265

266
static void XSetFont P_ ((Display *, GC, XFontStruct *));
267 268 269 270

/* Defined in macmenu.h.  */
extern void menubar_selection_callback (FRAME_PTR, int);

271 272 273
#define GC_FORE_COLOR(gc)	(&(gc)->fore_color)
#define GC_BACK_COLOR(gc)	(&(gc)->back_color)
#define GC_FONT(gc)		((gc)->xgcv.font)
274
#define GC_CLIP_REGION(gc)	((gc)->clip_region)
275
#define FRAME_NORMAL_GC(f)	((f)->output_data.mac->normal_gc)
276

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
static RgnHandle saved_port_clip_region = NULL;

static void
mac_begin_clip (region)
     RgnHandle region;
{
  static RgnHandle new_region = NULL;

  if (saved_port_clip_region == NULL)
    saved_port_clip_region = NewRgn ();
  if (new_region == NULL)
    new_region = NewRgn ();

  if (region)
    {
      GetClip (saved_port_clip_region);
      SectRgn (saved_port_clip_region, region, new_region);
      SetClip (new_region);
    }
}

static void
mac_end_clip (region)
     RgnHandle region;
{
  if (region)
    SetClip (saved_port_clip_region);
}

306

307 308
/* X display function emulation */

309
void
310
XFreePixmap (display, pixmap)
311
     Display *display;		/* not used */
312 313
     Pixmap pixmap;
{
314
  DisposeGWorld (pixmap);
315 316 317 318 319 320
}


/* Mac version of XDrawLine.  */

static void
321 322
mac_draw_line (f, gc, x1, y1, x2, y2)
     struct frame *f;
323 324 325
     GC gc;
     int x1, y1, x2, y2;
{
326
  SetPortWindowPort (FRAME_MAC_WINDOW (f));
327

328
  RGBForeColor (GC_FORE_COLOR (gc));
329

330
  mac_begin_clip (GC_CLIP_REGION (gc));
331 332
  MoveTo (x1, y1);
  LineTo (x2, y2);
333
  mac_end_clip (GC_CLIP_REGION (gc));
334 335
}

336 337 338 339 340 341 342
void
mac_draw_line_to_pixmap (display, p, gc, x1, y1, x2, y2)
     Display *display;
     Pixmap p;
     GC gc;
     int x1, y1, x2, y2;
{
343 344 345 346
  CGrafPtr old_port;
  GDHandle old_gdh;

  GetGWorld (&old_port, &old_gdh);
347 348
  SetGWorld (p, NULL);

349
  RGBForeColor (GC_FORE_COLOR (gc));
350 351 352 353 354

  LockPixels (GetGWorldPixMap (p));
  MoveTo (x1, y1);
  LineTo (x2, y2);
  UnlockPixels (GetGWorldPixMap (p));
355 356

  SetGWorld (old_port, old_gdh);
357 358
}

359

360
static void
361 362
mac_erase_rectangle (f, gc, x, y, width, height)
     struct frame *f;
363
     GC gc;
364 365 366 367 368
     int x, y;
     unsigned int width, height;
{
  Rect r;

369
  SetPortWindowPort (FRAME_MAC_WINDOW (f));
370

371
  RGBBackColor (GC_BACK_COLOR (gc));
372 373
  SetRect (&r, x, y, x + width, y + height);

374
  mac_begin_clip (GC_CLIP_REGION (gc));
375
  EraseRect (&r);
376
  mac_end_clip (GC_CLIP_REGION (gc));
377

378
  RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
379 380 381 382 383 384
}


/* Mac version of XClearArea.  */

void
385 386
mac_clear_area (f, x, y, width, height)
     struct frame *f;
387 388 389
     int x, y;
     unsigned int width, height;
{
390
  mac_erase_rectangle (f, FRAME_NORMAL_GC (f), x, y, width, height);
391 392 393 394 395
}

/* Mac version of XClearWindow.  */

static void
396 397
mac_clear_window (f)
     struct frame *f;
398
{
399
  SetPortWindowPort (FRAME_MAC_WINDOW (f));
400

401
  RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
402

403 404 405
#if TARGET_API_MAC_CARBON
  {
    Rect r;
406

407
    GetWindowPortBounds (FRAME_MAC_WINDOW (f), &r);
408 409 410
    EraseRect (&r);
  }
#else /* not TARGET_API_MAC_CARBON */
411
  EraseRect (&(FRAME_MAC_WINDOW (f)->portRect));
412
#endif /* not TARGET_API_MAC_CARBON */
413 414 415 416 417 418
}


/* Mac replacement for XCopyArea.  */

static void
419 420
mac_draw_bitmap (f, gc, x, y, width, height, bits, overlay_p)
     struct frame *f;
421
     GC gc;
422 423
     int x, y, width, height;
     unsigned short *bits;
424
     int overlay_p;
425
{
426
  BitMap bitmap;
427 428
  Rect r;

429
  bitmap.rowBytes = sizeof(unsigned short);
430
  bitmap.baseAddr = (char *)bits;
431 432
  SetRect (&(bitmap.bounds), 0, 0, width, height);

433
  SetPortWindowPort (FRAME_MAC_WINDOW (f));
434

435 436
  RGBForeColor (GC_FORE_COLOR (gc));
  RGBBackColor (GC_BACK_COLOR (gc));
437
  SetRect (&r, x, y, x + width, y + height);
438

439
  mac_begin_clip (GC_CLIP_REGION (gc));
440
#if TARGET_API_MAC_CARBON
441 442 443 444 445 446 447 448 449
  {
    CGrafPtr port;

    GetPort (&port);
    LockPortBits (port);
    CopyBits (&bitmap, GetPortBitMapForCopyBits (port),
	      &(bitmap.bounds), &r, overlay_p ? srcOr : srcCopy, 0);
    UnlockPortBits (port);
  }
450
#else /* not TARGET_API_MAC_CARBON */
451
  CopyBits (&bitmap, &(FRAME_MAC_WINDOW (f)->portBits), &(bitmap.bounds), &r,
452
	    overlay_p ? srcOr : srcCopy, 0);
453
#endif /* not TARGET_API_MAC_CARBON */
454
  mac_end_clip (GC_CLIP_REGION (gc));
455

456
  RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
457 458 459 460 461 462 463 464 465 466 467
}


/* Mac replacement for XCreateBitmapFromBitmapData.  */

static void
mac_create_bitmap_from_bitmap_data (bitmap, bits, w, h)
     BitMap *bitmap;
     char *bits;
     int w, h;
{
468 469 470 471 472
  static unsigned char swap_nibble[16]
    = { 0x0, 0x8, 0x4, 0xc,    /* 0000 1000 0100 1100 */
	0x2, 0xa, 0x6, 0xe,    /* 0010 1010 0110 1110 */
	0x1, 0x9, 0x5, 0xd,    /* 0001 1001 0101 1101 */
	0x3, 0xb, 0x7, 0xf };  /* 0011 1011 0111 1111 */
473 474
  int i, j, w1;
  char *p;
475

476 477
  w1 = (w + 7) / 8;         /* nb of 8bits elt in X bitmap */
  bitmap->rowBytes = ((w + 15) / 16) * 2; /* nb of 16bits elt in Mac bitmap */
478 479 480
  bitmap->baseAddr = xmalloc (bitmap->rowBytes * h);
  bzero (bitmap->baseAddr, bitmap->rowBytes * h);
  for (i = 0; i < h; i++)
481 482 483
    {
      p = bitmap->baseAddr + i * bitmap->rowBytes;
      for (j = 0; j < w1; j++)
484 485 486 487 488 489
	{
	  /* Bitswap XBM bytes to match how Mac does things.  */
	  unsigned char c = *bits++;
	  *p++ = (unsigned char)((swap_nibble[c & 0xf] << 4)
				 | (swap_nibble[(c>>4) & 0xf]));;
	}
490
    }
491 492 493 494 495 496 497 498 499 500 501 502

  SetRect (&(bitmap->bounds), 0, 0, w, h);
}


static void
mac_free_bitmap (bitmap)
     BitMap *bitmap;
{
  xfree (bitmap->baseAddr);
}

503 504 505 506 507 508

Pixmap
XCreatePixmap (display, w, width, height, depth)
     Display *display;		/* not used */
     WindowPtr w;
     unsigned int width, height;
Steven Tamm's avatar
Steven Tamm committed
509
     unsigned int depth;
510 511 512 513 514
{
  Pixmap pixmap;
  Rect r;
  QDErr err;

515
  SetPortWindowPort (w);
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531

  SetRect (&r, 0, 0, width, height);
  err = NewGWorld (&pixmap, depth, &r, NULL, NULL, 0);
  if (err != noErr)
    return NULL;
  return pixmap;
}


Pixmap
XCreatePixmapFromBitmapData (display, w, data, width, height, fg, bg, depth)
     Display *display;		/* not used */
     WindowPtr w;
     char *data;
     unsigned int width, height;
     unsigned long fg, bg;
532
     unsigned int depth;
533 534 535
{
  Pixmap pixmap;
  BitMap bitmap;
536 537
  CGrafPtr old_port;
  GDHandle old_gdh;
538 539 540 541
  static GC gc = NULL;		/* not reentrant */

  if (gc == NULL)
    gc = XCreateGC (display, w, 0, NULL);
542 543 544 545 546

  pixmap = XCreatePixmap (display, w, width, height, depth);
  if (pixmap == NULL)
    return NULL;

547
  GetGWorld (&old_port, &old_gdh);
548 549
  SetGWorld (pixmap, NULL);
  mac_create_bitmap_from_bitmap_data (&bitmap, data, width, height);
550 551 552 553
  XSetForeground (display, gc, fg);
  XSetBackground (display, gc, bg);
  RGBForeColor (GC_FORE_COLOR (gc));
  RGBBackColor (GC_BACK_COLOR (gc));
554 555 556 557 558 559 560 561 562
  LockPixels (GetGWorldPixMap (pixmap));
#if TARGET_API_MAC_CARBON
  CopyBits (&bitmap, GetPortBitMapForCopyBits (pixmap),
	    &bitmap.bounds, &bitmap.bounds, srcCopy, 0);
#else /* not TARGET_API_MAC_CARBON */
  CopyBits (&bitmap, &(((GrafPtr)pixmap)->portBits),
	    &bitmap.bounds, &bitmap.bounds, srcCopy, 0);
#endif /* not TARGET_API_MAC_CARBON */
  UnlockPixels (GetGWorldPixMap (pixmap));
563
  SetGWorld (old_port, old_gdh);
564 565 566 567 568 569
  mac_free_bitmap (&bitmap);

  return pixmap;
}


570 571 572
/* Mac replacement for XFillRectangle.  */

static void
573 574
mac_fill_rectangle (f, gc, x, y, width, height)
     struct frame *f;
575 576 577 578 579 580
     GC gc;
     int x, y;
     unsigned int width, height;
{
  Rect r;

581
  SetPortWindowPort (FRAME_MAC_WINDOW (f));
582

583
  RGBForeColor (GC_FORE_COLOR (gc));
584 585
  SetRect (&r, x, y, x + width, y + height);

586
  mac_begin_clip (GC_CLIP_REGION (gc));
587
  PaintRect (&r); /* using foreground color of gc */
588
  mac_end_clip (GC_CLIP_REGION (gc));
589 590 591 592 593 594
}


/* Mac replacement for XDrawRectangle: dest is a window.  */

static void
595 596
mac_draw_rectangle (f, gc, x, y, width, height)
     struct frame *f;
597 598 599 600 601 602
     GC gc;
     int x, y;
     unsigned int width, height;
{
  Rect r;

603
  SetPortWindowPort (FRAME_MAC_WINDOW (f));
604

605
  RGBForeColor (GC_FORE_COLOR (gc));
606 607
  SetRect (&r, x, y, x + width + 1, y + height + 1);

608
  mac_begin_clip (GC_CLIP_REGION (gc));
609
  FrameRect (&r); /* using foreground color of gc */
610
  mac_end_clip (GC_CLIP_REGION (gc));
611 612 613
}


614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
#if USE_ATSUI
static OSStatus
atsu_get_text_layout_with_text_ptr (text, text_length, style, text_layout)
     ConstUniCharArrayPtr text;
     UniCharCount text_length;
     ATSUStyle style;
     ATSUTextLayout *text_layout;
{
  OSStatus err;
  static ATSUTextLayout saved_text_layout = NULL; /* not reentrant */

  if (saved_text_layout == NULL)
    {
      UniCharCount lengths[] = {kATSUToTextEnd};
      ATSUAttributeTag tags[] = {kATSULineLayoutOptionsTag};
      ByteCount sizes[] = {sizeof (ATSLineLayoutOptions)};
      static ATSLineLayoutOptions line_layout =
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
	kATSLineDisableAllLayoutOperations  | kATSLineUseDeviceMetrics
#else
634
	kATSLineIsDisplayOnly | kATSLineFractDisable
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
#endif
	;
      ATSUAttributeValuePtr values[] = {&line_layout};

      err = ATSUCreateTextLayoutWithTextPtr (text,
					     kATSUFromTextBeginning,
					     kATSUToTextEnd,
					     text_length,
					     1, lengths, &style,
					     &saved_text_layout);
      if (err == noErr)
	err = ATSUSetLayoutControls (saved_text_layout,
				     sizeof (tags) / sizeof (tags[0]),
				     tags, sizes, values);
      /* XXX: Should we do this? */
      if (err == noErr)
	err = ATSUSetTransientFontMatching (saved_text_layout, true);
    }
  else
    {
      err = ATSUSetRunStyle (saved_text_layout, style,
			     kATSUFromTextBeginning, kATSUToTextEnd);
      if (err == noErr)
	err = ATSUSetTextPointerLocation (saved_text_layout, text,
					  kATSUFromTextBeginning,
					  kATSUToTextEnd,
					  text_length);
    }

  if (err == noErr)
    *text_layout = saved_text_layout;
  return err;
}
#endif

670

671
static void
672 673
mac_invert_rectangle (f, x, y, width, height)
     struct frame *f;
674 675 676 677 678
     int x, y;
     unsigned int width, height;
{
  Rect r;

679
  SetPortWindowPort (FRAME_MAC_WINDOW (f));
680 681 682 683 684 685 686

  SetRect (&r, x, y, x + width, y + height);

  InvertRect (&r);
}


687
static void
688 689
mac_draw_string_common (f, gc, x, y, buf, nchars, mode, bytes_per_char)
     struct frame *f;
690 691 692 693 694
     GC gc;
     int x, y;
     char *buf;
     int nchars, mode, bytes_per_char;
{
695
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
696 697 698 699 700 701
  UInt32 textFlags, savedFlags;
  if (!NILP(Vmac_use_core_graphics)) {
    textFlags = kQDUseCGTextRendering;
    savedFlags = SwapQDTextFlags(textFlags);
  }
#endif
702

703
  SetPortWindowPort (FRAME_MAC_WINDOW (f));
704 705 706 707

  RGBForeColor (GC_FORE_COLOR (gc));
  if (mode != srcOr)
    RGBBackColor (GC_BACK_COLOR (gc));
708

709 710 711 712 713 714 715 716 717 718 719
#if USE_ATSUI
  if (GC_FONT (gc)->mac_style)
    {
      OSErr err;
      ATSUTextLayout text_layout;

      xassert (bytes_per_char == 2);

#ifndef WORDS_BIG_ENDIAN
      {
	int i;
720
	UniChar *text = (UniChar *)buf;
721 722

	for (i = 0; i < nchars; i++)
723
	  text[i] = EndianU16_BtoN (text[i]);
724 725 726 727 728 729 730 731 732 733 734 735
      }
#endif
      err = atsu_get_text_layout_with_text_ptr ((ConstUniCharArrayPtr)buf,
						nchars,
						GC_FONT (gc)->mac_style,
						&text_layout);
      if (err == noErr)
	{
#ifdef MAC_OSX
	  if (NILP (Vmac_use_core_graphics))
	    {
#endif
736
	      mac_begin_clip (GC_CLIP_REGION (gc));
737 738 739 740
	      MoveTo (x, y);
	      ATSUDrawText (text_layout,
			    kATSUFromTextBeginning, kATSUToTextEnd,
			    kATSUUseGrafPortPenLoc, kATSUUseGrafPortPenLoc);
741
	      mac_end_clip (GC_CLIP_REGION (gc));
742 743 744 745 746 747
#ifdef MAC_OSX
	    }
	  else
	    {
	      CGrafPtr port;
	      CGContextRef context;
748
	      float port_height = FRAME_PIXEL_HEIGHT (f);
749 750 751 752 753 754
	      ATSUAttributeTag tags[] = {kATSUCGContextTag};
	      ByteCount sizes[] = {sizeof (CGContextRef)};
	      ATSUAttributeValuePtr values[] = {&context};

	      GetPort (&port);
	      QDBeginCGContext (port, &context);
755 756 757 758 759 760 761 762 763
	      if (gc->n_clip_rects)
		{
		  CGContextTranslateCTM (context, 0, port_height);
		  CGContextScaleCTM (context, 1, -1);
		  CGContextClipToRects (context, gc->clip_rects,
					gc->n_clip_rects);
		  CGContextScaleCTM (context, 1, -1);
		  CGContextTranslateCTM (context, 0, -port_height);
		}
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
	      CGContextSetRGBFillColor
		(context,
		 RED_FROM_ULONG (gc->xgcv.foreground) / 255.0,
		 GREEN_FROM_ULONG (gc->xgcv.foreground) / 255.0,
		 BLUE_FROM_ULONG (gc->xgcv.foreground) / 255.0,
		 1.0);
	      err = ATSUSetLayoutControls (text_layout,
					   sizeof (tags) / sizeof (tags[0]),
					   tags, sizes, values);
	      if (err == noErr)
		ATSUDrawText (text_layout,
			      kATSUFromTextBeginning, kATSUToTextEnd,
			      Long2Fix (x), Long2Fix (port_height - y));
	      CGContextSynchronize (context);
	      QDEndCGContext (port, &context);
779 780 781 782 783 784 785 786 787 788
#if 0
	      /* This doesn't work on Mac OS X 10.1.  */
	      ATSUClearLayoutControls (text_layout, 
				       sizeof (tags) / sizeof (tags[0]),
				       tags);
#else
	      ATSUSetLayoutControls (text_layout,
				     sizeof (tags) / sizeof (tags[0]),
				     tags, sizes, values);
#endif
789 790 791 792 793 794 795
	    }
#endif
	}
    }
  else
    {
#endif
796 797 798
  TextFont (GC_FONT (gc)->mac_fontnum);
  TextSize (GC_FONT (gc)->mac_fontsize);
  TextFace (GC_FONT (gc)->mac_fontface);
799 800
  TextMode (mode);

801
  mac_begin_clip (GC_CLIP_REGION (gc));
802 803
  MoveTo (x, y);
  DrawText (buf, 0, nchars * bytes_per_char);
804
  mac_end_clip (GC_CLIP_REGION (gc));
805 806 807
#if USE_ATSUI
    }
#endif
808

809
  if (mode != srcOr)
810
    RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
811
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
812 813 814
  if (!NILP(Vmac_use_core_graphics))
    SwapQDTextFlags(savedFlags);
#endif
815 816 817 818 819 820
}


/* Mac replacement for XDrawString.  */

static void
821 822
mac_draw_string (f, gc, x, y, buf, nchars)
     struct frame *f;
823 824 825 826 827
     GC gc;
     int x, y;
     char *buf;
     int nchars;
{
828
  mac_draw_string_common (f, gc, x, y, buf, nchars, srcOr, 1);
829 830 831 832 833 834
}


/* Mac replacement for XDrawString16. */

static void
835 836
mac_draw_string_16 (f, gc, x, y, buf, nchars)
     struct frame *f;
837 838 839 840 841
     GC gc;
     int x, y;
     XChar2b *buf;
     int nchars;
{
842
  mac_draw_string_common (f, gc, x, y, (char *) buf, nchars, srcOr, 2);
843 844 845 846 847 848
}


/* Mac replacement for XDrawImageString.  */

static void
849 850
mac_draw_image_string (f, gc, x, y, buf, nchars)
     struct frame *f;
851 852 853 854 855
     GC gc;
     int x, y;
     char *buf;
     int nchars;
{
856
  mac_draw_string_common (f, gc, x, y, buf, nchars, srcCopy, 1);
857 858 859 860 861 862
}


/* Mac replacement for XDrawString16.  */

static void
863 864
mac_draw_image_string_16 (f, gc, x, y, buf, nchars)
     struct frame *f;
865 866 867 868 869
     GC gc;
     int x, y;
     XChar2b *buf;
     int nchars;
{
870
  mac_draw_string_common (f, gc, x, y, (char *) buf, nchars, srcCopy, 2);
871 872 873 874 875 876
}


/* Mac replacement for XCopyArea: dest must be window.  */

static void
877
mac_copy_area (src, f, gc, src_x, src_y, width, height, dest_x, dest_y)
878
     Pixmap src;
879
     struct frame *f;
880 881 882 883 884 885 886
     GC gc;
     int src_x, src_y;
     unsigned int width, height;
     int dest_x, dest_y;
{
  Rect src_r, dest_r;

887
  SetPortWindowPort (FRAME_MAC_WINDOW (f));
888

889 890 891
  SetRect (&src_r, src_x, src_y, src_x + width, src_y + height);
  SetRect (&dest_r, dest_x, dest_y, dest_x + width, dest_y + height);

892 893 894
  ForeColor (blackColor);
  BackColor (whiteColor);

895
  mac_begin_clip (GC_CLIP_REGION (gc));
896
  LockPixels (GetGWorldPixMap (src));
897
#if TARGET_API_MAC_CARBON
898 899 900 901 902 903 904 905 906 907
  {
    CGrafPtr port;

    GetPort (&port);
    LockPortBits (port);
    CopyBits (GetPortBitMapForCopyBits (src),
	      GetPortBitMapForCopyBits (port),
	      &src_r, &dest_r, srcCopy, 0);
    UnlockPortBits (port);
  }
908
#else /* not TARGET_API_MAC_CARBON */
909
  CopyBits (&(((GrafPtr)src)->portBits), &(FRAME_MAC_WINDOW (f)->portBits),
910 911 912
	    &src_r, &dest_r, srcCopy, 0);
#endif /* not TARGET_API_MAC_CARBON */
  UnlockPixels (GetGWorldPixMap (src));
913
  mac_end_clip (GC_CLIP_REGION (gc));
914

915
  RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
916
}
917

918 919

static void
920
mac_copy_area_with_mask (src, mask, f, gc, src_x, src_y,
921 922
			 width, height, dest_x, dest_y)
     Pixmap src, mask;
923
     struct frame *f;
924 925 926 927 928 929 930
     GC gc;
     int src_x, src_y;
     unsigned int width, height;
     int dest_x, dest_y;
{
  Rect src_r, dest_r;

931
  SetPortWindowPort (FRAME_MAC_WINDOW (f));
932 933 934 935 936 937 938

  SetRect (&src_r, src_x, src_y, src_x + width, src_y + height);
  SetRect (&dest_r, dest_x, dest_y, dest_x + width, dest_y + height);

  ForeColor (blackColor);
  BackColor (whiteColor);

939
  mac_begin_clip (GC_CLIP_REGION (gc));
940 941 942
  LockPixels (GetGWorldPixMap (src));
  LockPixels (GetGWorldPixMap (mask));
#if TARGET_API_MAC_CARBON
943 944 945 946 947 948 949 950 951 952
  {
    CGrafPtr port;

    GetPort (&port);
    LockPortBits (port);
    CopyMask (GetPortBitMapForCopyBits (src), GetPortBitMapForCopyBits (mask),
	      GetPortBitMapForCopyBits (port),
	      &src_r, &src_r, &dest_r);
    UnlockPortBits (port);
  }
953
#else /* not TARGET_API_MAC_CARBON */
954
  CopyMask (&(((GrafPtr)src)->portBits), &(((GrafPtr)mask)->portBits),
955
	    &(FRAME_MAC_WINDOW (f)->portBits), &src_r, &src_r, &dest_r);
956
#endif /* not TARGET_API_MAC_CARBON */
957 958
  UnlockPixels (GetGWorldPixMap (mask));
  UnlockPixels (GetGWorldPixMap (src));
959
  mac_end_clip (GC_CLIP_REGION (gc));
960

961
  RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
962 963 964 965 966 967
}


/* Mac replacement for XCopyArea: used only for scrolling.  */

static void
968 969
mac_scroll_area (f, gc, src_x, src_y, width, height, dest_x, dest_y)
     struct frame *f;
970 971 972 973 974
     GC gc;
     int src_x, src_y;
     unsigned int width, height;
     int dest_x, dest_y;
{
975
#if TARGET_API_MAC_CARBON
Steven Tamm's avatar
Steven Tamm committed
976 977
  Rect src_r;
  RgnHandle dummy = NewRgn ();	/* For avoiding update events.  */
978 979

  SetRect (&src_r, src_x, src_y, src_x + width, src_y + height);
980 981
  ScrollWindowRect (FRAME_MAC_WINDOW (f),
		    &src_r, dest_x - src_x, dest_y - src_y,
Steven Tamm's avatar
Steven Tamm committed
982 983
		    kScrollWindowNoOptions, dummy);
  DisposeRgn (dummy);
984
#else /* not TARGET_API_MAC_CARBON */
985
  Rect src_r, dest_r;
986
  WindowPtr w = FRAME_MAC_WINDOW (f);
987 988 989 990 991 992

  SetPort (w);

  SetRect (&src_r, src_x, src_y, src_x + width, src_y + height);
  SetRect (&dest_r, dest_x, dest_y, dest_x + width, dest_y + height);

993 994 995 996
  /* In Color QuickDraw, set ForeColor and BackColor as follows to avoid
     color mapping in CopyBits.  Otherwise, it will be slow.  */
  ForeColor (blackColor);
  BackColor (whiteColor);
997
  mac_begin_clip (GC_CLIP_REGION (gc));
998
  CopyBits (&(w->portBits), &(w->portBits), &src_r, &dest_r, srcCopy, 0);
999
  mac_end_clip (GC_CLIP_REGION (gc));
1000

1001
  RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
1002
#endif /* not TARGET_API_MAC_CARBON */
1003 1004 1005 1006 1007 1008
}


/* Mac replacement for XChangeGC.  */

static void
1009 1010 1011 1012 1013
XChangeGC (display, gc, mask, xgcv)
     Display *display;
     GC gc;
     unsigned long mask;
     XGCValues *xgcv;
1014 1015
{
  if (mask & GCForeground)
1016
    XSetForeground (display, gc, xgcv->foreground);
1017
  if (mask & GCBackground)
1018
    XSetBackground (display, gc, xgcv->background);
1019
  if (mask & GCFont)
1020
    XSetFont (display, gc, xgcv->font);
1021 1022 1023 1024 1025
}


/* Mac replacement for XCreateGC.  */

1026 1027 1028 1029 1030 1031
GC
XCreateGC (display, window, mask, xgcv)
     Display *display;
     Window window;
     unsigned long mask;
     XGCValues *xgcv;
1032
{
1033
  GC gc = xmalloc (sizeof (*gc));
1034

1035 1036 1037 1038 1039
  if (gc)
    {
      bzero (gc, sizeof (*gc));
      XChangeGC (display, gc, mask, xgcv);
    }
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051

  return gc;
}


/* Used in xfaces.c.  */

void
XFreeGC (display, gc)
     Display *display;
     GC gc;
{
1052 1053
  if (gc->clip_region)
    DisposeRgn (gc->clip_region);
1054 1055 1056 1057 1058 1059 1060
  xfree (gc);
}


/* Mac replacement for XGetGCValues.  */

static void
1061 1062 1063 1064 1065
XGetGCValues (display, gc, mask, xgcv)
     Display *display;
     GC gc;
     unsigned long mask;
     XGCValues *xgcv;
1066
{
1067 1068 1069 1070 1071 1072
  if (mask & GCForeground)
    xgcv->foreground = gc->xgcv.foreground;
  if (mask & GCBackground)
    xgcv->background = gc->xgcv.background;
  if (mask & GCFont)
    xgcv->font = gc->xgcv.font;
1073 1074 1075 1076 1077
}


/* Mac replacement for XSetForeground.  */

1078
void
1079 1080 1081 1082 1083
XSetForeground (display, gc, color)
     Display *display;
     GC gc;
     unsigned long color;
{
1084 1085 1086 1087 1088 1089 1090
  if (gc->xgcv.foreground != color)
    {
      gc->xgcv.foreground = color;
      gc->fore_color.red = RED16_FROM_ULONG (color);
      gc->fore_color.green = GREEN16_FROM_ULONG (color);
      gc->fore_color.blue = BLUE16_FROM_ULONG (color);
    }
1091 1092 1093
}


1094 1095 1096 1097 1098 1099 1100 1101
/* Mac replacement for XSetBackground.  */

void
XSetBackground (display, gc, color)
     Display *display;
     GC gc;
     unsigned long color;
{
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
  if (gc->xgcv.background != color)
    {
      gc->xgcv.background = color;
      gc->back_color.red = RED16_FROM_ULONG (color);
      gc->back_color.green = GREEN16_FROM_ULONG (color);
      gc->back_color.blue = BLUE16_FROM_ULONG (color);
    }
}


/* Mac replacement for XSetFont.  */

static void
XSetFont (display, gc, font)
     Display *display;
     GC gc;
     XFontStruct *font;
{
  gc->xgcv.font = font;
1121 1122 1123
}


1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
/* Mac replacement for XSetClipRectangles.  */

static void
mac_set_clip_rectangles (display, gc, rectangles, n)
     Display *display;
     GC gc;
     Rect *rectangles;
     int n;
{
  int i;

  if (n < 0 || n > MAX_CLIP_RECTS)
    abort ();
  if (n == 0)
    {
      if (gc->clip_region)
	{
	  DisposeRgn (gc->clip_region);
	  gc->clip_region = NULL;
	}
    }
  else
    {
      if (gc->clip_region == NULL)
	gc->clip_region = NewRgn ();
      RectRgn (gc->clip_region, rectangles);
      if (n > 1)
	{
	  RgnHandle region = NewRgn ();

	  for (i = 1; i < n; i++)
	    {
	      RectRgn (region, rectangles + i);
	      UnionRgn (gc->clip_region, region, gc->clip_region);
	    }
	  DisposeRgn (region);
	}
    }
#if defined (MAC_OSX) && USE_ATSUI
  gc->n_clip_rects = n;

  for (i = 0; i < n; i++)
    {
      Rect *rect = rectangles + i;

      gc->clip_rects[i] = CGRectMake (rect->left, rect->top,
				      rect->right - rect->left,
				      rect->bottom - rect->top);
    }
#endif
}


/* Mac replacement for XSetClipMask.  */

static INLINE void
mac_reset_clip_rectangles (display, gc)
     Display *display;
     GC gc;
{
  mac_set_clip_rectangles (display, gc, NULL, 0);
}


1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
/* Mac replacement for XSetWindowBackground.  */

void
XSetWindowBackground (display, w, color)
     Display *display;
     WindowPtr w;
     unsigned long color;
{
#if !TARGET_API_MAC_CARBON
  AuxWinHandle aw_handle;
  CTabHandle ctab_handle;
  ColorSpecPtr ct_table;
  short ct_size;
#endif
  RGBColor bg_color;

  bg_color.red = RED16_FROM_ULONG (color);
  bg_color.green = GREEN16_FROM_ULONG (color);
  bg_color.blue = BLUE16_FROM_ULONG (color);

#if TARGET_API_MAC_CARBON
  SetWindowContentColor (w, &bg_color);
#else
  if (GetAuxWin (w, &aw_handle))
    {
      ctab_handle = (*aw_handle)->awCTable;
      HandToHand ((Handle *) &ctab_handle);
      ct_table = (*ctab_handle)->ctTable;
      ct_size = (*ctab_handle)->ctSize;
      while (ct_size > -1)
	{
	  if (ct_table->value == 0)
	    {
	      ct_table->rgb = bg_color;
	      CTabChanged (ctab_handle);
	      SetWinColor (w, (WCTabHandle) ctab_handle);
	    }
	  ct_size--;
	}
    }
#endif
}

1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
/* x_sync is a no-op on Mac.  */
void
x_sync (f)
     void *f;
{
}


/* Flush display of frame F, or of all frames if F is null.  */

Steven Tamm's avatar
Steven Tamm committed
1241
static void
1242 1243 1244
x_flush (f)
     struct frame *f;
{
1245
#if TARGET_API_MAC_CARBON