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

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
18 19
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.  */
20

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

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

26
#include <stdio.h>
27

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

#include "macterm.h"

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

37
#if TARGET_API_MAC_CARBON
Andrew Choi's avatar
In src:  
Andrew Choi committed
38 39 40 41
/* 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
42
#else /* not TARGET_API_MAC_CARBON */
43 44 45 46 47 48 49 50 51 52
#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>
53
#include <Windows.h>
54
#if defined (__MRC__) || (__MSL__ >= 0x6000)
55 56 57 58 59 60
#include <ControlDefinitions.h>
#endif

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

#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
70
#include <sys/param.h>
71

72 73
#include "charset.h"
#include "coding.h"
74 75 76 77 78 79 80 81 82 83
#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"
84
#include "keyboard.h"
85
#include "intervals.h"
86 87
#include "atimer.h"
#include "keymap.h"
88

Andrew Choi's avatar
In src:  
Andrew Choi committed
89 90 91 92 93 94 95 96
/* 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)
97 98


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

101
Lisp_Object Vx_toolkit_scroll_bars;
102

103 104 105 106
/* 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;


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

static int any_help_event_p;

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

115 116 117 118 119 120
/* 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
121 122 123 124 125 126
   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. */
127 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 168 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

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;

/* 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

200
extern Lisp_Object Vsystem_name;
201 202 203

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

204
extern EMACS_INT extra_keyboard_modifiers;
205

206 207 208 209
/* The keysyms to use for the various modifiers.  */

static Lisp_Object Qalt, Qhyper, Qsuper, Qmodifier_value;

210
extern int inhibit_window_system;
211

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

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

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));
231
void mac_initialize P_ ((void));
232 233 234 235 236 237 238 239 240 241
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 *));
242 243 244 245
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 *));
246 247 248
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 *));
249 250 251
static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
				   enum text_cursor_kinds));

252
static void x_clip_to_row P_ ((struct window *, struct glyph_row *, int, GC));
253 254 255 256
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 *));
257 258 259 260
static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
					    enum scroll_bar_part *,
					    Lisp_Object *, Lisp_Object *,
					    unsigned long *));
261

262 263
static int is_emacs_window (WindowPtr);

264 265 266 267 268 269 270 271
int x_bitmap_icon (struct frame *, Lisp_Object);
void x_make_frame_visible (struct frame *);

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

/* X display function emulation */

272
void
273
XFreePixmap (display, pixmap)
274
     Display *display;		/* not used */
275 276
     Pixmap pixmap;
{
277
  DisposeGWorld (pixmap);
278 279 280 281 282 283 284 285 286 287
}


/* Set foreground color for subsequent QuickDraw commands.  Assume
   graphic port has already been set.  */

static void
mac_set_forecolor (unsigned long color)
{
  RGBColor fg_color;
288

289 290 291
  fg_color.red = RED16_FROM_ULONG (color);
  fg_color.green = GREEN16_FROM_ULONG (color);
  fg_color.blue = BLUE16_FROM_ULONG (color);
292 293

  RGBForeColor (&fg_color);
294 295 296 297 298 299 300 301 302 303
}


/* Set background color for subsequent QuickDraw commands.  Assume
   graphic port has already been set.  */

static void
mac_set_backcolor (unsigned long color)
{
  RGBColor bg_color;
304

305 306 307
  bg_color.red = RED16_FROM_ULONG (color);
  bg_color.green = GREEN16_FROM_ULONG (color);
  bg_color.blue = BLUE16_FROM_ULONG (color);
308 309

  RGBBackColor (&bg_color);
310 311 312 313 314 315
}

/* Set foreground and background color for subsequent QuickDraw
   commands.  Assume that the graphic port has already been set.  */

static void
316 317 318
mac_set_colors (gc, bg_save)
     GC gc;
     RGBColor *bg_save;
319
{
320 321
  if (bg_save)
    GetBackColor (bg_save);
322 323 324 325 326 327 328 329 330 331 332 333 334
  mac_set_forecolor (gc->foreground);
  mac_set_backcolor (gc->background);
}

/* Mac version of XDrawLine.  */

static void
XDrawLine (display, w, gc, x1, y1, x2, y2)
     Display *display;
     WindowPtr w;
     GC gc;
     int x1, y1, x2, y2;
{
335 336
  RGBColor old_bg;

337
  SetPortWindowPort (w);
338

339
  mac_set_colors (gc, &old_bg);
340 341 342

  MoveTo (x1, y1);
  LineTo (x2, y2);
343 344

  RGBBackColor (&old_bg);
345 346
}

347 348 349 350 351 352 353
void
mac_draw_line_to_pixmap (display, p, gc, x1, y1, x2, y2)
     Display *display;
     Pixmap p;
     GC gc;
     int x1, y1, x2, y2;
{
354 355 356 357
  CGrafPtr old_port;
  GDHandle old_gdh;

  GetGWorld (&old_port, &old_gdh);
358 359
  SetGWorld (p, NULL);

360
  mac_set_colors (gc, NULL);
361 362 363 364 365

  LockPixels (GetGWorldPixMap (p));
  MoveTo (x1, y1);
  LineTo (x2, y2);
  UnlockPixels (GetGWorldPixMap (p));
366 367

  SetGWorld (old_port, old_gdh);
368 369
}

370 371
/* Mac version of XClearArea.  */

372
void
373 374 375 376 377 378 379 380 381 382
XClearArea (display, w, x, y, width, height, exposures)
     Display *display;
     WindowPtr w;
     int x, y;
     unsigned int width, height;
     int exposures;
{
  struct mac_output *mwp = (mac_output *) GetWRefCon (w);
  Rect r;
  XGCValues xgc;
383
  RGBColor old_bg;
384

385 386
  xgc.foreground = mwp->x_compatible.foreground_pixel;
  xgc.background = mwp->x_compatible.background_pixel;
387

388
  SetPortWindowPort (w);
389

390
  mac_set_colors (&xgc, &old_bg);
391 392 393
  SetRect (&r, x, y, x + width, y + height);

  EraseRect (&r);
394 395

  RGBBackColor (&old_bg);
396 397 398 399 400 401 402 403 404 405 406 407
}

/* Mac version of XClearWindow.  */

static void
XClearWindow (display, w)
     Display *display;
     WindowPtr w;
{
  struct mac_output *mwp = (mac_output *) GetWRefCon (w);
  XGCValues xgc;

408 409
  xgc.foreground = mwp->x_compatible.foreground_pixel;
  xgc.background = mwp->x_compatible.background_pixel;
410

411
  SetPortWindowPort (w);
412

413
  mac_set_colors (&xgc, NULL);
414

415 416 417
#if TARGET_API_MAC_CARBON
  {
    Rect r;
418

419 420 421 422 423 424
    GetWindowPortBounds (w, &r);
    EraseRect (&r);
  }
#else /* not TARGET_API_MAC_CARBON */
  EraseRect (&(w->portRect));
#endif /* not TARGET_API_MAC_CARBON */
425 426 427 428 429 430
}


/* Mac replacement for XCopyArea.  */

static void
431
mac_draw_bitmap (display, w, gc, x, y, width, height, bits, overlay_p)
432 433 434
     Display *display;
     WindowPtr w;
     GC gc;
435 436
     int x, y, width, height;
     unsigned short *bits;
437
     int overlay_p;
438
{
439
  BitMap bitmap;
440
  Rect r;
441
  RGBColor old_bg;
442

443
  bitmap.rowBytes = sizeof(unsigned short);
444
  bitmap.baseAddr = (char *)bits;
445 446
  SetRect (&(bitmap.bounds), 0, 0, width, height);

447
  SetPortWindowPort (w);
448

449
  mac_set_colors (gc, &old_bg);
450
  SetRect (&r, x, y, x + width, y + height);
451

452
#if TARGET_API_MAC_CARBON
453 454 455 456
  LockPortBits (GetWindowPort (w));
  CopyBits (&bitmap, GetPortBitMapForCopyBits (GetWindowPort (w)),
	    &(bitmap.bounds), &r, overlay_p ? srcOr : srcCopy, 0);
  UnlockPortBits (GetWindowPort (w));
457
#else /* not TARGET_API_MAC_CARBON */
458
  CopyBits (&bitmap, &(w->portBits), &(bitmap.bounds), &r,
459
	    overlay_p ? srcOr : srcCopy, 0);
460
#endif /* not TARGET_API_MAC_CARBON */
461 462

  RGBBackColor (&old_bg);
463 464 465 466 467 468 469 470 471 472 473
}


/* Mac replacement for XSetClipRectangles.  */

static void
mac_set_clip_rectangle (display, w, r)
     Display *display;
     WindowPtr w;
     Rect *r;
{
474
  SetPortWindowPort (w);
475 476 477 478 479 480 481 482 483 484 485 486 487

  ClipRect (r);
}


/* Mac replacement for XSetClipMask.  */

static void
mac_reset_clipping (display, w)
     Display *display;
     WindowPtr w;
{
  Rect r;
488

489
  SetPortWindowPort (w);
490 491 492 493 494 495 496 497 498 499 500 501 502 503

  SetRect (&r, -32767, -32767, 32767, 32767);
  ClipRect (&r);
}


/* Mac replacement for XCreateBitmapFromBitmapData.  */

static void
mac_create_bitmap_from_bitmap_data (bitmap, bits, w, h)
     BitMap *bitmap;
     char *bits;
     int w, h;
{
504 505 506 507 508
  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 */
509 510
  int i, j, w1;
  char *p;
511

512 513
  w1 = (w + 7) / 8;         /* nb of 8bits elt in X bitmap */
  bitmap->rowBytes = ((w + 15) / 16) * 2; /* nb of 16bits elt in Mac bitmap */
514 515 516
  bitmap->baseAddr = xmalloc (bitmap->rowBytes * h);
  bzero (bitmap->baseAddr, bitmap->rowBytes * h);
  for (i = 0; i < h; i++)
517 518 519
    {
      p = bitmap->baseAddr + i * bitmap->rowBytes;
      for (j = 0; j < w1; j++)
520 521 522 523 524 525
	{
	  /* 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]));;
	}
526
    }
527 528 529 530 531 532 533 534 535 536 537 538

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


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

539 540 541 542 543 544

Pixmap
XCreatePixmap (display, w, width, height, depth)
     Display *display;		/* not used */
     WindowPtr w;
     unsigned int width, height;
Steven Tamm's avatar
Steven Tamm committed
545
     unsigned int depth;
546 547 548 549 550
{
  Pixmap pixmap;
  Rect r;
  QDErr err;

551
  SetPortWindowPort (w);
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571

  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;
     unsigned int depth;	/* not used */
{
  Pixmap pixmap;
  BitMap bitmap;
572 573
  CGrafPtr old_port;
  GDHandle old_gdh;
574 575 576 577 578

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

579
  GetGWorld (&old_port, &old_gdh);
580 581 582 583 584 585 586 587 588 589 590 591 592
  SetGWorld (pixmap, NULL);
  mac_create_bitmap_from_bitmap_data (&bitmap, data, width, height);
  mac_set_forecolor (fg);
  mac_set_backcolor (bg);
  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));
593
  SetGWorld (old_port, old_gdh);
594 595 596 597 598 599
  mac_free_bitmap (&bitmap);

  return pixmap;
}


600 601 602 603 604 605 606 607 608 609 610
/* Mac replacement for XFillRectangle.  */

static void
XFillRectangle (display, w, gc, x, y, width, height)
     Display *display;
     WindowPtr w;
     GC gc;
     int x, y;
     unsigned int width, height;
{
  Rect r;
611
  RGBColor old_bg;
612

613
  SetPortWindowPort (w);
614

615
  mac_set_colors (gc, &old_bg);
616 617 618
  SetRect (&r, x, y, x + width, y + height);

  PaintRect (&r); /* using foreground color of gc */
619 620

  RGBBackColor (&old_bg);
621 622 623
}


Steven Tamm's avatar
Steven Tamm committed
624
#if 0 /* TODO: figure out if we need to do this on Mac.  */
625 626 627 628 629 630 631 632
static void
mac_fill_rectangle_to_pixmap (display, p, gc, x, y, width, height)
     Display *display;
     Pixmap p;
     GC gc;
     int x, y;
     unsigned int width, height;
{
633 634
  CGrafPtr old_port;
  GDHandle old_gdh;
635 636
  Rect r;

637
  GetGWorld (&old_port, &old_gdh);
638
  SetGWorld (p, NULL);
639
  mac_set_colors (gc, NULL);
640 641 642 643 644
  SetRect (&r, x, y, x + width, y + height);

  LockPixels (GetGWorldPixMap (p));
  PaintRect (&r); /* using foreground color of gc */
  UnlockPixels (GetGWorldPixMap (p));
645 646

  SetGWorld (old_port, old_gdh);
647
}
Steven Tamm's avatar
Steven Tamm committed
648
#endif
649 650


651 652 653 654 655 656 657 658 659 660 661
/* Mac replacement for XDrawRectangle: dest is a window.  */

static void
mac_draw_rectangle (display, w, gc, x, y, width, height)
     Display *display;
     WindowPtr w;
     GC gc;
     int x, y;
     unsigned int width, height;
{
  Rect r;
662
  RGBColor old_bg;
663

664
  SetPortWindowPort (w);
665

666
  mac_set_colors (gc, &old_bg);
667 668 669
  SetRect (&r, x, y, x + width + 1, y + height + 1);

  FrameRect (&r); /* using foreground color of gc */
670 671

  RGBBackColor (&old_bg);
672 673 674
}


Steven Tamm's avatar
Steven Tamm committed
675
#if 0 /* TODO: figure out if we need to do this on Mac.  */
676 677 678 679 680 681 682 683 684 685
/* Mac replacement for XDrawRectangle: dest is a Pixmap.  */

static void
mac_draw_rectangle_to_pixmap (display, p, gc, x, y, width, height)
     Display *display;
     Pixmap p;
     GC gc;
     int x, y;
     unsigned int width, height;
{
686 687
  CGrafPtr old_port;
  GDHandle old_gdh;
688 689
  Rect r;

690
  GetGWorld (&old_port, &old_gdh);
691
  SetGWorld (p, NULL);
692
  mac_set_colors (gc, NULL);
693
  SetRect (&r, x, y, x + width + 1, y + height + 1);
694

695
  LockPixels (GetGWorldPixMap (p));
696
  FrameRect (&r); /* using foreground color of gc */
697
  UnlockPixels (GetGWorldPixMap (p));
698 699

  SetGWorld (old_port, old_gdh);
700
}
Steven Tamm's avatar
Steven Tamm committed
701
#endif
702 703 704 705 706 707 708 709 710 711 712 713


static void
mac_draw_string_common (display, w, gc, x, y, buf, nchars, mode,
			bytes_per_char)
     Display *display;
     WindowPtr w;
     GC gc;
     int x, y;
     char *buf;
     int nchars, mode, bytes_per_char;
{
714 715
  RGBColor old_bg;

716
  SetPortWindowPort (w);
717
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
718 719 720 721 722 723
  UInt32 textFlags, savedFlags;
  if (!NILP(Vmac_use_core_graphics)) {
    textFlags = kQDUseCGTextRendering;
    savedFlags = SwapQDTextFlags(textFlags);
  }
#endif
724

725
  mac_set_colors (gc, &old_bg);
726 727 728 729 730 731 732 733

  TextFont (gc->font->mac_fontnum);
  TextSize (gc->font->mac_fontsize);
  TextFace (gc->font->mac_fontface);
  TextMode (mode);

  MoveTo (x, y);
  DrawText (buf, 0, nchars * bytes_per_char);
734 735

  RGBBackColor (&old_bg);
736
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
737 738 739
  if (!NILP(Vmac_use_core_graphics))
    SwapQDTextFlags(savedFlags);
#endif
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
}


/* Mac replacement for XDrawString.  */

static void
XDrawString (display, w, gc, x, y, buf, nchars)
     Display *display;
     WindowPtr w;
     GC gc;
     int x, y;
     char *buf;
     int nchars;
{
  mac_draw_string_common (display, w, gc, x, y, buf, nchars, srcOr, 1);
}


/* Mac replacement for XDrawString16. */

static void
XDrawString16 (display, w, gc, x, y, buf, nchars)
     Display *display;
     WindowPtr w;
     GC gc;
     int x, y;
     XChar2b *buf;
     int nchars;
{
  mac_draw_string_common (display, w, gc, x, y, (char *) buf, nchars, srcOr,
			  2);
}


/* Mac replacement for XDrawImageString.  */

static void
XDrawImageString (display, w, gc, x, y, buf, nchars)
     Display *display;
     WindowPtr w;
     GC gc;
     int x, y;
     char *buf;
     int nchars;
{
  mac_draw_string_common (display, w, gc, x, y, buf, nchars, srcCopy, 1);
}


/* Mac replacement for XDrawString16.  */

static void
XDrawImageString16 (display, w, gc, x, y, buf, nchars)
     Display *display;
     WindowPtr w;
     GC gc;
     int x, y;
     XChar2b *buf;
     int nchars;
{
  mac_draw_string_common (display, w, gc, x, y, (char *) buf, nchars, srcCopy,
			  2);
}


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

static void
mac_copy_area (display, src, dest, gc, src_x, src_y, width, height, dest_x,
	       dest_y)
     Display *display;
     Pixmap src;
     WindowPtr dest;
     GC gc;
     int src_x, src_y;
     unsigned int width, height;
     int dest_x, dest_y;
{
  Rect src_r, dest_r;

820
  SetPortWindowPort (dest);
821

822 823 824
  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);

825 826 827 828
  ForeColor (blackColor);
  BackColor (whiteColor);

  LockPixels (GetGWorldPixMap (src));
829
#if TARGET_API_MAC_CARBON
830 831 832 833 834 835 836 837 838 839 840
  LockPortBits (GetWindowPort (dest));
  CopyBits (GetPortBitMapForCopyBits (src),
	    GetPortBitMapForCopyBits (GetWindowPort (dest)),
	    &src_r, &dest_r, srcCopy, 0);
  UnlockPortBits (GetWindowPort (dest));
#else /* not TARGET_API_MAC_CARBON */
  CopyBits (&(((GrafPtr)src)->portBits), &(dest->portBits),
	    &src_r, &dest_r, srcCopy, 0);
#endif /* not TARGET_API_MAC_CARBON */
  UnlockPixels (GetGWorldPixMap (src));
}
841

842 843 844 845 846 847 848 849 850 851 852 853 854 855

static void
mac_copy_area_with_mask (display, src, mask, dest, gc, src_x, src_y,
			 width, height, dest_x, dest_y)
     Display *display;
     Pixmap src, mask;
     WindowPtr dest;
     GC gc;
     int src_x, src_y;
     unsigned int width, height;
     int dest_x, dest_y;
{
  Rect src_r, dest_r;

856
  SetPortWindowPort (dest);
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871

  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);

  LockPixels (GetGWorldPixMap (src));
  LockPixels (GetGWorldPixMap (mask));
#if TARGET_API_MAC_CARBON
  LockPortBits (GetWindowPort (dest));
  CopyMask (GetPortBitMapForCopyBits (src), GetPortBitMapForCopyBits (mask),
	    GetPortBitMapForCopyBits (GetWindowPort (dest)),
	    &src_r, &src_r, &dest_r);
  UnlockPortBits (GetWindowPort (dest));
872
#else /* not TARGET_API_MAC_CARBON */
873 874
  CopyMask (&(((GrafPtr)src)->portBits), &(((GrafPtr)mask)->portBits),
	    &(dest->portBits), &src_r, &src_r, &dest_r);
875
#endif /* not TARGET_API_MAC_CARBON */
876 877
  UnlockPixels (GetGWorldPixMap (mask));
  UnlockPixels (GetGWorldPixMap (src));
878 879 880 881 882 883 884 885 886 887 888 889 890 891
}


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

static void
mac_scroll_area (display, w, gc, src_x, src_y, width, height, dest_x, dest_y)
     Display *display;
     WindowPtr w;
     GC gc;
     int src_x, src_y;
     unsigned int width, height;
     int dest_x, dest_y;
{
892
#if TARGET_API_MAC_CARBON
Steven Tamm's avatar
Steven Tamm committed
893 894
  Rect src_r;
  RgnHandle dummy = NewRgn ();	/* For avoiding update events.  */
895 896

  SetRect (&src_r, src_x, src_y, src_x + width, src_y + height);
Steven Tamm's avatar
Steven Tamm committed
897 898 899
  ScrollWindowRect (w, &src_r, dest_x - src_x, dest_y - src_y,
		    kScrollWindowNoOptions, dummy);
  DisposeRgn (dummy);
900
#else /* not TARGET_API_MAC_CARBON */
901 902 903 904 905 906 907
  Rect src_r, dest_r;

  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);

908 909 910 911 912
  /* In Color QuickDraw, set ForeColor and BackColor as follows to avoid
     color mapping in CopyBits.  Otherwise, it will be slow.  */
  ForeColor (blackColor);
  BackColor (whiteColor);
  CopyBits (&(w->portBits), &(w->portBits), &src_r, &dest_r, srcCopy, 0);
913

914
  mac_set_colors (gc, NULL);
915
#endif /* not TARGET_API_MAC_CARBON */
916 917 918
}


Steven Tamm's avatar
Steven Tamm committed
919
#if 0 /* TODO: figure out if we need to do this on Mac.  */
920 921 922
/* Mac replacement for XCopyArea: dest must be Pixmap.  */

static void
923
mac_copy_area_to_pixmap (display, src, dest, gc, src_x, src_y, width, height,
924 925
                     dest_x, dest_y)
     Display *display;
926
     Pixmap src, dest;
927 928 929 930 931
     GC gc;
     int src_x, src_y;
     unsigned int width, height;
     int dest_x, dest_y;
{
932 933
  CGrafPtr old_port;
  GDHandle old_gdh;
934 935
  Rect src_r, dest_r;

936
  GetGWorld (&old_port, &old_gdh);
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
  SetGWorld (dest, NULL);
  ForeColor (blackColor);
  BackColor (whiteColor);

  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);

  LockPixels (GetGWorldPixMap (src));
  LockPixels (GetGWorldPixMap (dest));
#if TARGET_API_MAC_CARBON
  CopyBits (GetPortBitMapForCopyBits (src), GetPortBitMapForCopyBits (dest),
	    &src_r, &dest_r, srcCopy, 0);
#else /* not TARGET_API_MAC_CARBON */
  CopyBits (&(((GrafPtr)src)->portBits), &(((GrafPtr)dest)->portBits),
	    &src_r, &dest_r, srcCopy, 0);
#endif /* not TARGET_API_MAC_CARBON */
  UnlockPixels (GetGWorldPixMap (dest));
  UnlockPixels (GetGWorldPixMap (src));
955 956

  SetGWorld (old_port, old_gdh);
957 958 959 960 961 962 963 964 965 966 967 968 969
}


static void
mac_copy_area_with_mask_to_pixmap (display, src, mask, dest, gc, src_x, src_y,
				   width, height, dest_x, dest_y)
     Display *display;
     Pixmap src, mask, dest;
     GC gc;
     int src_x, src_y;
     unsigned int width, height;
     int dest_x, dest_y;
{
970 971
  CGrafPtr old_port;
  GDHandle old_gdh;
972 973
  Rect src_r, dest_r;

974
  GetGWorld (&old_port, &old_gdh);
975 976 977
  SetGWorld (dest, NULL);
  ForeColor (blackColor);
  BackColor (whiteColor);
978

979 980
  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);
981

982 983 984 985 986 987 988 989 990 991 992 993 994
  LockPixels (GetGWorldPixMap (src));
  LockPixels (GetGWorldPixMap (mask));
  LockPixels (GetGWorldPixMap (dest));
#if TARGET_API_MAC_CARBON
  CopyMask (GetPortBitMapForCopyBits (src), GetPortBitMapForCopyBits (mask),
	    GetPortBitMapForCopyBits (dest), &src_r, &src_r, &dest_r);
#else /* not TARGET_API_MAC_CARBON */
  CopyMask (&(((GrafPtr)src)->portBits), &(((GrafPtr)mask)->portBits),
	    &(((GrafPtr)dest)->portBits), &src_r, &src_r, &dest_r);
#endif /* not TARGET_API_MAC_CARBON */
  UnlockPixels (GetGWorldPixMap (dest));
  UnlockPixels (GetGWorldPixMap (mask));
  UnlockPixels (GetGWorldPixMap (src));
995 996

  SetGWorld (old_port, old_gdh);
997
}
Steven Tamm's avatar
Steven Tamm committed
998
#endif
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053


/* Mac replacement for XChangeGC.  */

static void
XChangeGC (void * ignore, XGCValues* gc, unsigned long mask,
                XGCValues *xgcv)
{
  if (mask & GCForeground)
    gc->foreground = xgcv->foreground;
  if (mask & GCBackground)
    gc->background = xgcv->background;
  if (mask & GCFont)
    gc->font = xgcv->font;
}


/* Mac replacement for XCreateGC.  */

XGCValues *
XCreateGC (void * ignore, Window window, unsigned long mask,
                      XGCValues *xgcv)
{
  XGCValues *gc = (XGCValues *) xmalloc (sizeof (XGCValues));
  bzero (gc, sizeof (XGCValues));

  XChangeGC (ignore, gc, mask, xgcv);

  return gc;
}


/* Used in xfaces.c.  */

void
XFreeGC (display, gc)
     Display *display;
     GC gc;
{
  xfree (gc);
}


/* Mac replacement for XGetGCValues.  */

static void
XGetGCValues (void* ignore, XGCValues *gc,
                   unsigned long mask, XGCValues *xgcv)
{
  XChangeGC (ignore, xgcv, mask, gc);
}


/* Mac replacement for XSetForeground.  */

1054
void
1055 1056 1057 1058 1059 1060 1061 1062 1063
XSetForeground (display, gc, color)
     Display *display;
     GC gc;
     unsigned long color;
{
  gc->foreground = color;
}


1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
/* Mac replacement for XSetBackground.  */

void
XSetBackground (display, gc, color)
     Display *display;
     GC gc;
     unsigned long color;
{
  gc->background = color;
}


/* 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
}


1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
/* Mac replacement for XSetFont.  */

static void
XSetFont (display, gc, font)
     Display *display;
     GC gc;
     XFontStruct *font;
{
  gc->font = font;
}


/* 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
1142
static void
1143 1144 1145
x_flush (f)
     struct frame *f;
{
1146
#if TARGET_API_MAC_CARBON
1147
  BLOCK_INPUT;
Steven Tamm's avatar
Steven Tamm committed
1148 1149 1150 1151
  if (f)
    QDFlushPortBuffer (GetWindowPort (FRAME_MAC_WINDOW (f)), NULL);
  else
    QDFlushPortBuffer (GetQDGlobalsThePort (), NULL);
1152
  UNBLOCK_INPUT;
Steven Tamm's avatar
Steven Tamm committed
1153
#endif
1154 1155 1156
}


Steven Tamm's avatar
Steven Tamm committed
1157 1158 1159 1160 1161 1162 1163 1164 1165
/* Remove calls to XFlush by defining XFlush to an empty replacement.
   Calls to XFlush should be unnecessary because the X output buffer
   is flushed automatically as needed by calls to XPending,
   XNextEvent, or XWindowEvent according to the XFlush man page.
   XTread_socket calls XPending.  Removing XFlush improves
   performance.  */

#define XFlush(DISPLAY)	(void) 0

1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181

/* Return the struct mac_display_info corresponding to DPY.  There's
   only one.  */

struct mac_display_info *
mac_display_info_for_display (dpy)
     Display *dpy;
{
  return &one_mac_display_info;
}



/***********************************************************************
		    Starting and ending an update
 ***********************************************************************/
1182

1183 1184 1185
/* Start an update of frame F.  This function is installed as a hook
   for update_begin, i.e. it is called when update_begin is called.
   This function is called prior to calls to x_update_window_begin for
1186
   each window being updated.  */
1187

1188
static void
1189 1190 1191
x_update_begin (f)
     struct frame *f;
{
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
#if TARGET_API_MAC_CARBON
  /* During update of a frame, availability of input events is
     periodically checked with ReceiveNextEvent if
     redisplay-dont-pause is nil.  That normally flushes window buffer
     changes for every check, and thus screen update looks waving even
     if no input is available.  So we disable screen updates during
     update of a frame.  */
  BLOCK_INPUT;
  DisableScreenUpdates ();
  UNBLOCK_INPUT;
#endif
1203 1204 1205 1206 1207 1208 1209
}


/* Start update of window W.  Set the global variable updated_window
   to the window being updated and set output_cursor to the cursor
   position of W.  */

1210
static void
1211 1212 1213 1214 1215
x_update_window_begin (w)
     struct window *w;
{
  struct frame *f = XFRAME (WINDOW_FRAME (w));
  struct mac_display_info *display_info = FRAME_MAC_DISPLAY_INFO (f);
1216

1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
  updated_window = w;
  set_output_cursor (&w->cursor);

  BLOCK_INPUT;

  if (f == display_info->mouse_face_mouse_frame)
    {
      /* Don't do highlighting for mouse motion during the update.  */
      display_info->mouse_face_defer = 1;

      /* If F needs to be redrawn, simply forget about any prior mouse
	 highlighting.  */
      if (FRAME_GARBAGED_P (f))
	display_info->mouse_face_window = Qnil;

#if 0 /* Rows in a current matrix containing glyphs in mouse-face have
	 their mouse_face_p flag set, which means that they are always
	 unequal to rows in a desired matrix which never have that
	 flag set.  So, rows containing mouse-face glyphs are never
	 scrolled, and we don't have to switch the mouse highlight off
	 here to prevent it from being scrolled.  */
1238

1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
      /* Can we tell that this update does not affect the window
	 where the mouse highlight is?  If so, no need to turn off.
	 Likewise, don't do anything if the frame is garbaged;
	 in that case, the frame's current matrix that we would use
	 is all wrong, and we will redisplay that line anyway.  */
      if (!NILP (display_info->mouse_face_window)
	  && w == XWINDOW (display_info->mouse_face_window))
	{
	  int i;

1249
	  for (i = 0; i < w->desired_matrix->nrows; ++i)
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
	    if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
	      break;

	  if (i < w->desired_matrix->nrows)
	    clear_mouse_face (display_info);
	}
#endif /* 0 */
    }

  UNBLOCK_INPUT;
}


1263
/* Draw a vertical window border from (x,y0) to (x,y1)  */
1264 1265

static void
1266
mac_draw_vertical_window_border (w, x, y0, y1)
1267
     struct window *w;
1268
     int x, y0, y1;
1269 1270
{
  struct frame *f = XFRAME (WINDOW_FRAME (w));
1271 1272 1273 1274 1275 1276
  struct face *face;

  face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
  if (face)
    XSetForeground (FRAME_MAC_DISPLAY (f), f->output_data.mac->normal_gc,
		    face->foreground);
1277

1278 1279
  XDrawLine (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f),
	     f->output_data.mac->normal_gc, x, y0, x, y1);
1280
}
1281

1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
/* End update of window W (which is equal to updated_window).

   Draw vertical borders between horizontally adjacent windows, and
   display W's cursor if CURSOR_ON_P is non-zero.

   MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
   glyphs in mouse-face were overwritten.  In that case we have to
   make sure that the mouse-highlight is properly redrawn.

   W may be a menu bar pseudo-window in case we don't have X toolkit
1292
   support.  Such windows don't have a cursor, so don't display it
1293
   here.  */
1294

1295
static void
1296 1297 1298 1299
x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
     struct window *w;
     int cursor_on_p, mouse_face_overwritten_p;
{
1300
  struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (XFRAME (w->frame));
1301

1302 1303 1304 1305 1306
  if (!w->pseudo_window_p)
    {
      BLOCK_INPUT;

      if (cursor_on_p)
1307 1308 1309
	display_and_set_cursor (w, 1, output_cursor.hpos,
				output_cursor.vpos,
				output_cursor.x, output_cursor.y);
1310

1311 1312
      if (draw_window_fringes (w, 1))
	x_draw_vertical_border (w);
1313

1314 1315
      UNBLOCK_INPUT;
    }
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325

  /* If a row with mouse-face was overwritten, arrange for
     XTframe_up_to_date to redisplay the mouse highlight.  */
  if (mouse_face_overwritten_p)
    {
      dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
      dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
      dpyinfo->mouse_face_window = Qnil;
    }

1326 1327 1328 1329 1330 1331 1332
  updated_window = NULL;
}


/* End update of frame F.  This function is installed as a hook in
   update_end.  */