Merged in changes from CVS trunk.
[bpt/emacs.git] / src / macterm.c
index 2af910c..da3dcb1 100644 (file)
@@ -50,6 +50,7 @@ Boston, MA 02111-1307, USA.  */
 #include <TextUtils.h>
 #include <LowMem.h>
 #include <Controls.h>
+#include <Windows.h>
 #if defined (__MRC__) || (__MSL__ >= 0x6000)
 #include <ControlDefinitions.h>
 #endif
@@ -227,6 +228,10 @@ extern int errno;
 
 extern int extra_keyboard_modifiers;
 
+/* The keysyms to use for the various modifiers.  */
+
+static Lisp_Object Qalt, Qhyper, Qsuper, Qmodifier_value;
+
 static Lisp_Object Qvendor_specific_keysyms;
 
 #if 0
@@ -274,7 +279,7 @@ static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
                                   enum text_cursor_kinds));
 
-static void x_clip_to_row P_ ((struct window *, struct glyph_row *, GC));
+static void x_clip_to_row P_ ((struct window *, struct glyph_row *, int, GC));
 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 *));
@@ -562,7 +567,7 @@ XCreatePixmap (display, w, width, height, depth)
      Display *display;         /* not used */
      WindowPtr w;
      unsigned int width, height;
-     unsigned int depth;       /* not used */
+     unsigned int depth;
 {
   Pixmap pixmap;
   Rect r;
@@ -638,6 +643,7 @@ XFillRectangle (display, w, gc, x, y, width, height)
 }
 
 
+#if 0 /* TODO: figure out if we need to do this on Mac.  */
 static void
 mac_fill_rectangle_to_pixmap (display, p, gc, x, y, width, height)
      Display *display;
@@ -661,6 +667,7 @@ mac_fill_rectangle_to_pixmap (display, p, gc, x, y, width, height)
 
   SetGWorld (old_port, old_gdh);
 }
+#endif
 
 
 /* Mac replacement for XDrawRectangle: dest is a window.  */
@@ -684,6 +691,7 @@ mac_draw_rectangle (display, w, gc, x, y, width, height)
 }
 
 
+#if 0 /* TODO: figure out if we need to do this on Mac.  */
 /* Mac replacement for XDrawRectangle: dest is a Pixmap.  */
 
 static void
@@ -709,6 +717,7 @@ mac_draw_rectangle_to_pixmap (display, p, gc, x, y, width, height)
 
   SetGWorld (old_port, old_gdh);
 }
+#endif
 
 
 static void
@@ -903,24 +912,13 @@ mac_scroll_area (display, w, gc, src_x, src_y, width, height, dest_x, dest_y)
      int dest_x, dest_y;
 {
 #if TARGET_API_MAC_CARBON
-  Rect gw_r, src_r, dest_r;
+  Rect src_r;
+  RgnHandle dummy = NewRgn (); /* For avoiding update events.  */
 
   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);
-
-  SetPortWindowPort (w);
-
-  ForeColor (blackColor);
-  BackColor (whiteColor);
-
-  LockPortBits (GetWindowPort (w));
-  {
-    const BitMap *bitmap = GetPortBitMapForCopyBits (GetWindowPort (w));
-    CopyBits (bitmap, bitmap, &src_r, &dest_r, srcCopy, 0);
-  }
-  UnlockPortBits (GetWindowPort (w));
-
-  mac_set_colors (gc);
+  ScrollWindowRect (w, &src_r, dest_x - src_x, dest_y - src_y,
+                   kScrollWindowNoOptions, dummy);
+  DisposeRgn (dummy);
 #else /* not TARGET_API_MAC_CARBON */
   Rect src_r, dest_r;
 
@@ -954,6 +952,7 @@ mac_scroll_area (display, w, gc, src_x, src_y, width, height, dest_x, dest_y)
 }
 
 
+#if 0 /* TODO: figure out if we need to do this on Mac.  */
 /* Mac replacement for XCopyArea: dest must be Pixmap.  */
 
 static void
@@ -1032,6 +1031,7 @@ mac_copy_area_with_mask_to_pixmap (display, src, mask, dest, gc, src_x, src_y,
 
   SetGWorld (old_port, old_gdh);
 }
+#endif
 
 
 /* Mac replacement for XChangeGC.  */
@@ -1126,40 +1126,32 @@ x_sync (f)
 }
 
 
-/* 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.  */
-
-#if TARGET_API_MAC_CARBON
-#define XFlush(DISPLAY) QDFlushPortBuffer (GetQDGlobalsThePort (), NULL)
-#else
-#define XFlush(DISPLAY)        (void) 0
-#endif
-
 /* Flush display of frame F, or of all frames if F is null.  */
 
-void
+static void
 x_flush (f)
      struct frame *f;
 {
 #if TARGET_API_MAC_CARBON
   BLOCK_INPUT;
-  if (f == NULL)
-    {
-      Lisp_Object rest, frame;
-      FOR_EACH_FRAME (rest, frame)
-       x_flush (XFRAME (frame));
-    }
-  else if (FRAME_MAC_P (f))
-    XFlush (FRAME_MAC_DISPLAY (f));
+  if (f)
+    QDFlushPortBuffer (GetWindowPort (FRAME_MAC_WINDOW (f)), NULL);
+  else
+    QDFlushPortBuffer (GetQDGlobalsThePort (), NULL);
   UNBLOCK_INPUT;
-#endif /* TARGET_API_MAC_CARBON */
+#endif
 }
 
 
+/* 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
+
 \f
 /* Return the struct mac_display_info corresponding to DPY.  There's
    only one.  */
@@ -1290,9 +1282,8 @@ x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
                                output_cursor.vpos,
                                output_cursor.x, output_cursor.y);
 
-      x_draw_vertical_border (w);
-
-      draw_window_fringes (w);
+      if (draw_window_fringes (w, 1))
+       x_draw_vertical_border (w);
 
       UNBLOCK_INPUT;
     }
@@ -1325,6 +1316,10 @@ static void
 x_update_end (f)
      struct frame *f;
 {
+  /* Mouse highlight may be displayed again.  */
+  FRAME_MAC_DISPLAY_INFO (f)->mouse_face_defer = 0;
+
+  BLOCK_INPUT;
   /* Reset the background color of Mac OS Window to that of the frame after
      update so that it is used by Mac Toolbox to clear the update region before
      an update event is generated.  */
@@ -1332,10 +1327,6 @@ x_update_end (f)
 
   mac_set_backcolor (FRAME_BACKGROUND_PIXEL (f));
 
-  /* Mouse highlight may be displayed again.  */
-  FRAME_MAC_DISPLAY_INFO (f)->mouse_face_defer = 0;
-
-  BLOCK_INPUT;
   XFlush (FRAME_MAC_DISPLAY (f));
   UNBLOCK_INPUT;
 }
@@ -1450,12 +1441,12 @@ x_draw_fringe_bitmap (w, row, p)
       int oldVH = row->visible_height;
       row->visible_height = p->h;
       row->y -= rowY - p->y;
-      x_clip_to_row (w, row, gc);
+      x_clip_to_row (w, row, -1, gc);
       row->y = oldY;
       row->visible_height = oldVH;
     }
   else
-    x_clip_to_row (w, row, gc);
+    x_clip_to_row (w, row, -1, gc);
 
   if (p->bx >= 0 && !p->overlay_p)
     {
@@ -1953,6 +1944,14 @@ x_clear_glyph_string_rect (s, x, y, w, h)
 }
 
 
+/* We prefer not to use XDrawImageString (srcCopy text transfer mode)
+   on Mac OS X because:
+   - Screen is double-buffered.  (In srcCopy mode, a text is drawn
+     into an offscreen graphics world first.  So performance gain
+     cannot be expected.)
+   - It lowers rendering quality.
+   - Some fonts leave garbage on cursor movement.  */
+
 /* Draw the background of glyph_string S.  If S->background_filled_p
    is non-zero don't draw it.  FORCE_P non-zero means draw the
    background even if it wouldn't be drawn normally.  This is used
@@ -1984,10 +1983,12 @@ x_draw_glyph_string_background (s, force_p)
        }
       else
 #endif
+#if 0 /* defined(MAC_OS8)*/
         if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
               || s->font_not_found_p
               || s->extends_to_end_of_line_p
               || force_p)
+#endif
        {
          x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
                                     s->background_width,
@@ -2040,6 +2041,7 @@ x_draw_glyph_string_foreground (s)
        for (i = 0; i < s->nchars; ++i)
          char1b[i] = s->char2b[i].byte2;
 
+#if 0 /* defined(MAC_OS8) */
       /* Draw text with XDrawString if background has already been
         filled.  Otherwise, use XDrawImageString.  (Note that
         XDrawImageString is usually faster than XDrawString.)  Always
@@ -2047,6 +2049,7 @@ x_draw_glyph_string_foreground (s)
         no chance that characters under a box cursor are invisible.  */
       if (s->for_overlaps_p
          || (s->background_filled_p && s->hl != DRAW_CURSOR))
+#endif
        {
          /* Draw characters with 16-bit or 8-bit functions.  */
          if (s->two_byte_p)
@@ -2056,6 +2059,7 @@ x_draw_glyph_string_foreground (s)
            XDrawString (s->display, s->window, s->gc, x,
                         s->ybase - boff, char1b, s->nchars);
        }
+#if 0 /* defined(MAC_OS8)*/
       else
        {
          if (s->two_byte_p)
@@ -2065,6 +2069,7 @@ x_draw_glyph_string_foreground (s)
            XDrawImageString (s->display, s->window, s->gc, x,
                              s->ybase - boff, char1b, s->nchars);
        }
+#endif
     }
 }
 
@@ -2631,38 +2636,17 @@ x_draw_image_foreground (s)
 
   if (s->img->pixmap)
     {
+      x_set_glyph_string_clipping (s);
+
       if (s->img->mask)
-       {
-         Rect nr;
-         XRectangle clip_rect, image_rect, r;
-
-         get_glyph_string_clip_rect (s, &nr);
-         CONVERT_TO_XRECT (clip_rect, nr);
-         image_rect.x = x;
-         image_rect.y = y;
-         image_rect.width = s->slice.width;
-         image_rect.height = s->slice.height;
-         if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
-           mac_copy_area_with_mask (s->display, s->img->pixmap, s->img->mask,
-                                    s->window, s->gc,
-                                    s->slice.x + r.x - x, s->slice.y + r.y - y,
-                                    r.width, r.height, r.x, r.y);
-       }
+       mac_copy_area_with_mask (s->display, s->img->pixmap, s->img->mask,
+                                s->window, s->gc, s->slice.x, s->slice.y,
+                                s->slice.width, s->slice.height, x, y);
       else
        {
-         Rect nr;
-         XRectangle clip_rect, image_rect, r;
-
-         get_glyph_string_clip_rect (s, &nr);
-         CONVERT_TO_XRECT (clip_rect, nr);
-         image_rect.x = x;
-         image_rect.y = y;
-         image_rect.width = s->slice.width;
-         image_rect.height = s->slice.height;
-         if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
-           mac_copy_area (s->display, s->img->pixmap, s->window, s->gc,
-                          s->slice.x + r.x - x, s->slice.y + r.y - y,
-                          r.width, r.height, r.x, r.y);
+         mac_copy_area (s->display, s->img->pixmap,
+                        s->window, s->gc, s->slice.x, s->slice.y,
+                        s->slice.width, s->slice.height, x, y);
 
          /* When the image has a mask, we can expect that at
             least part of a mouse highlight or a block cursor will
@@ -2741,6 +2725,7 @@ x_draw_image_relief (s)
 }
 
 
+#if 0 /* TODO: figure out if we need to do this on Mac.  */
 /* Draw the foreground of image glyph string S to PIXMAP.  */
 
 static void
@@ -2801,6 +2786,7 @@ x_draw_image_foreground_1 (s, pixmap)
     mac_draw_rectangle_to_pixmap (s->display, pixmap, s->gc, x, y,
                                  s->slice.width - 1, s->slice.height - 1);
 }
+#endif
 
 
 /* Draw part of the background of glyph string S.  X, Y, W, and H
@@ -2872,6 +2858,7 @@ x_draw_image_glyph_string (s)
       if (s->slice.y == 0)
        y += box_line_vwidth;
 
+#if 0 /* TODO: figure out if we need to do this on Mac.  */
       if (s->img->mask)
        {
          /* Create a pixmap as large as the glyph string.  Fill it
@@ -2908,12 +2895,14 @@ x_draw_image_glyph_string (s)
            }
        }
       else
+#endif
        x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
 
       s->background_filled_p = 1;
     }
 
   /* Draw the foreground.  */
+#if 0 /* TODO: figure out if we need to do this on Mac.  */
   if (pixmap != 0)
     {
       x_draw_image_foreground_1 (s, pixmap);
@@ -2924,6 +2913,7 @@ x_draw_image_glyph_string (s)
       XFreePixmap (s->display, pixmap);
     }
   else
+#endif
     x_draw_image_foreground (s);
 
   /* If we must draw a relief around the image, do it.  */
@@ -3887,18 +3877,21 @@ remember_mouse_glyph (f1, gx, gy)
 
 
 static WindowPtr
-mac_front_window ()
+front_emacs_window ()
 {
 #if TARGET_API_MAC_CARBON
-  return GetFrontWindowOfClass (kDocumentWindowClass, true);
+  WindowPtr wp = GetFrontWindowOfClass (kDocumentWindowClass, true);
+
+  while (wp && !is_emacs_window (wp))
+    wp = GetNextWindowOfClass (wp, kDocumentWindowClass, true);
 #else
-  WindowPtr front_window = FrontWindow ();
+  WindowPtr wp = FrontWindow ();
 
-  if (tip_window && front_window == tip_window)
-    return GetNextWindow (front_window);
-  else
-    return front_window;
+  while (wp && (wp == tip_window || !is_emacs_window (wp)))
+    wp = GetNextWindow (wp);
 #endif
+
+  return wp;
 }
 
 #define mac_window_to_frame(wp) (((mac_output *) GetWRefCon (wp))->mFP)
@@ -3934,7 +3927,7 @@ XTmouse_position (fp, insist, bar_window, part, x, y, time)
 {
   Point mouse_pos;
   int ignore1, ignore2;
-  WindowPtr wp = mac_front_window ();
+  WindowPtr wp = front_emacs_window ();
   struct frame *f;
   Lisp_Object frame, tail;
 
@@ -4551,7 +4544,7 @@ x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
      unsigned long *time;
 {
   struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
-  WindowPtr wp = mac_front_window ();
+  WindowPtr wp = front_emacs_window ();
   Point mouse_pos;
   struct frame *f = mac_window_to_frame (wp);
   int win_y, top_range;
@@ -4608,18 +4601,19 @@ x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
    mode lines must be clipped to the whole window.  */
 
 static void
-x_clip_to_row (w, row, gc)
+x_clip_to_row (w, row, area, gc)
      struct window *w;
      struct glyph_row *row;
+     int area;
      GC gc;
 {
   struct frame *f = XFRAME (WINDOW_FRAME (w));
   Rect clip_rect;
-  int window_y, window_width;
+  int window_x, window_y, window_width;
 
-  window_box (w, -1, 0, &window_y, &window_width, 0);
+  window_box (w, area, &window_x, &window_y, &window_width, 0);
 
-  clip_rect.left = WINDOW_TO_FRAME_PIXEL_X (w, 0);
+  clip_rect.left = window_x;
   clip_rect.top = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
   clip_rect.top = max (clip_rect.top, window_y);
   clip_rect.right = clip_rect.left + window_width;
@@ -4685,7 +4679,7 @@ x_draw_hollow_cursor (w, row)
   gc = dpyinfo->scratch_cursor_gc;
 
   /* Set clipping, draw the rectangle, and reset clipping again.  */
-  x_clip_to_row (w, row, gc);
+  x_clip_to_row (w, row, TEXT_AREA, gc);
   mac_draw_rectangle (dpy, FRAME_MAC_WINDOW (f), gc, x, y, wd, h);
   mac_reset_clipping (dpy, FRAME_MAC_WINDOW (f));
 }
@@ -4756,7 +4750,7 @@ x_draw_bar_cursor (w, row, width, kind)
       width = min (cursor_glyph->pixel_width, width);
 
       w->phys_cursor_width = width;
-      x_clip_to_row (w, row, gc);
+      x_clip_to_row (w, row, TEXT_AREA, gc);
 
       if (kind == BAR_CURSOR)
        XFillRectangle (dpy, window, gc,
@@ -5036,6 +5030,26 @@ xim_close_dpy (dpyinfo)
  */
 
 \f
+void
+mac_get_window_bounds (f, inner, outer)
+     struct frame *f;
+     Rect *inner, *outer;
+{
+#if TARGET_API_MAC_CARBON
+  GetWindowBounds (FRAME_MAC_WINDOW (f), kWindowContentRgn, inner);
+  GetWindowBounds (FRAME_MAC_WINDOW (f), kWindowStructureRgn, outer);
+#else /* not TARGET_API_MAC_CARBON */
+  RgnHandle region = NewRgn ();
+
+  GetWindowRegion (FRAME_MAC_WINDOW (f), kWindowContentRgn, region);
+  *inner = (*region)->rgnBBox;
+  GetWindowRegion (FRAME_MAC_WINDOW (f), kWindowStructureRgn, region);
+  *outer = (*region)->rgnBBox;
+  DisposeRgn (region);
+#endif /* not TARGET_API_MAC_CARBON */
+}
+
+
 /* Calculate the absolute position in frame F
    from its current recorded position values and gravity.  */
 
@@ -5043,47 +5057,36 @@ void
 x_calc_absolute_position (f)
      struct frame *f;
 {
-  Point pt;
+  int width_diff = 0, height_diff = 0;
   int flags = f->size_hint_flags;
+  Rect inner, outer;
 
-  pt.h = pt.v = 0;
+  /* We have nothing to do if the current position
+     is already for the top-left corner.  */
+  if (! ((flags & XNegative) || (flags & YNegative)))
+    return;
 
-  /* Find the position of the outside upper-left corner of
+  /* Find the offsets of the outside upper-left corner of
      the inner window, with respect to the outer window.  */
-  if (f->output_data.mac->parent_desc != FRAME_MAC_DISPLAY_INFO (f)->root_window)
-    {
-      GrafPtr savePort;
-      GetPort (&savePort);
+  mac_get_window_bounds (f, &inner, &outer);
 
-      SetPortWindowPort (FRAME_MAC_WINDOW (f));
-
-#if TARGET_API_MAC_CARBON
-      {
-        Rect r;
-
-        GetWindowPortBounds (FRAME_MAC_WINDOW (f), &r);
-        SetPt(&pt, r.left,  r.top);
-      }
-#else /* not TARGET_API_MAC_CARBON */
-      SetPt(&pt, FRAME_MAC_WINDOW (f)->portRect.left,  FRAME_MAC_WINDOW (f)->portRect.top);
-#endif /* not TARGET_API_MAC_CARBON */
-      LocalToGlobal (&pt);
-      SetPort (savePort);
-    }
+  width_diff = (outer.right - outer.left) - (inner.right - inner.left);
+  height_diff = (outer.bottom - outer.top) - (inner.bottom - inner.top);
 
   /* Treat negative positions as relative to the leftmost bottommost
      position that fits on the screen.  */
   if (flags & XNegative)
     f->left_pos = (FRAME_MAC_DISPLAY_INFO (f)->width
-                  - 2 * f->border_width - pt.h
+                   - width_diff
                   - FRAME_PIXEL_WIDTH (f)
                   + f->left_pos);
-  /* NTEMACS_TODO: Subtract menubar height?  */
+
   if (flags & YNegative)
     f->top_pos = (FRAME_MAC_DISPLAY_INFO (f)->height
-                 - 2 * f->border_width - pt.v
+                 - height_diff
                  - FRAME_PIXEL_HEIGHT (f)
                  + f->top_pos);
+
   /* The left_pos and top_pos
      are now relative to the top and left screen edges,
      so the flags should correspond.  */
@@ -5102,8 +5105,6 @@ x_set_offset (f, xoff, yoff, change_gravity)
      register int xoff, yoff;
      int change_gravity;
 {
-  int modified_top, modified_left;
-
   if (change_gravity > 0)
     {
       f->top_pos = yoff;
@@ -5120,11 +5121,55 @@ x_set_offset (f, xoff, yoff, change_gravity)
   BLOCK_INPUT;
   x_wm_set_size_hint (f, (long) 0, 0);
 
-  modified_left = f->left_pos;
-  modified_top = f->top_pos;
-
-  MoveWindow (f->output_data.mac->mWP, modified_left + 6,
-             modified_top + 42, false);
+#if TARGET_API_MAC_CARBON
+  MoveWindowStructure (FRAME_MAC_WINDOW (f), f->left_pos, f->top_pos);
+  /* If the title bar is completely outside the screen, adjust the
+     position. */
+  ConstrainWindowToScreen (FRAME_MAC_WINDOW (f), kWindowTitleBarRgn,
+                          kWindowConstrainMoveRegardlessOfFit
+                          | kWindowConstrainAllowPartial, NULL, NULL);
+  x_real_positions (f, &f->left_pos, &f->top_pos);
+#else
+  {
+    Rect inner, outer, screen_rect, dummy;
+    RgnHandle region = NewRgn ();
+
+    mac_get_window_bounds (f, &inner, &outer);
+    f->x_pixels_diff = inner.left - outer.left;
+    f->y_pixels_diff = inner.top - outer.top;
+    MoveWindow (FRAME_MAC_WINDOW (f), f->left_pos + f->x_pixels_diff,
+               f->top_pos + f->y_pixels_diff, false);
+
+    /* If the title bar is completely outside the screen, adjust the
+       position.  The variable `outer' holds the title bar rectangle.
+       The variable `inner' holds slightly smaller one than `outer',
+       so that the calculation of overlapping may not become too
+       strict.  */
+    GetWindowRegion (FRAME_MAC_WINDOW (f), kWindowTitleBarRgn, region);
+    outer = (*region)->rgnBBox;
+    DisposeRgn (region);
+    inner = outer;
+    InsetRect (&inner, 8, 8);
+    screen_rect = qd.screenBits.bounds;
+    screen_rect.top += GetMBarHeight ();
+
+    if (!SectRect (&inner, &screen_rect, &dummy))
+      {
+       if (inner.right <= screen_rect.left)
+         f->left_pos = screen_rect.left;
+       else if (inner.left >= screen_rect.right)
+         f->left_pos = screen_rect.right - (outer.right - outer.left);
+
+       if (inner.bottom <= screen_rect.top)
+         f->top_pos = screen_rect.top;
+       else if (inner.top >= screen_rect.bottom)
+         f->top_pos = screen_rect.bottom - (outer.bottom - outer.top);
+
+       MoveWindow (FRAME_MAC_WINDOW (f), f->left_pos + f->x_pixels_diff,
+                   f->top_pos + f->y_pixels_diff, false);
+      }
+  }
+#endif
 
   UNBLOCK_INPUT;
 }
@@ -5263,7 +5308,11 @@ x_raise_frame (f)
      struct frame *f;
 {
   if (f->async_visible)
-    SelectWindow (FRAME_MAC_WINDOW (f));
+    {
+      BLOCK_INPUT;
+      SelectWindow (FRAME_MAC_WINDOW (f));
+      UNBLOCK_INPUT;
+    }
 }
 
 /* Lower frame F.  */
@@ -5272,7 +5321,11 @@ x_lower_frame (f)
      struct frame *f;
 {
   if (f->async_visible)
-    SendBehind (FRAME_MAC_WINDOW (f), nil);
+    {
+      BLOCK_INPUT;
+      SendBehind (FRAME_MAC_WINDOW (f), nil);
+      UNBLOCK_INPUT;
+    }
 }
 
 static void
@@ -5321,7 +5374,6 @@ x_make_frame_visible (f)
 
   XFlush (FRAME_MAC_DISPLAY (f));
 
-#if 0 /* MAC_TODO */
   /* Synchronize to ensure Emacs knows the frame is visible
      before we do anything else.  We do this loop with input not blocked
      so that incoming events are handled.  */
@@ -5365,9 +5417,6 @@ x_make_frame_visible (f)
        FRAME_SAMPLE_VISIBILITY (f);
       }
   }
-#else
-  UNBLOCK_INPUT;
-#endif /* MAC_TODO */
 }
 
 /* Change from mapped state to withdrawn state.  */
@@ -5836,7 +5885,14 @@ mac_font_match (char *mf, char *xf)
 static Lisp_Object Qbig5, Qcn_gb, Qsjis, Qeuc_kr;
 
 static void
-decode_mac_font_name (char *name, int size, short scriptcode)
+decode_mac_font_name (name, size, scriptcode)
+     char *name;
+     int size;
+#if TARGET_API_MAC_CARBON
+     int scriptcode;
+#else
+     short scriptcode;
+#endif
 {
   Lisp_Object coding_system;
   struct coding_system coding;
@@ -5874,7 +5930,15 @@ decode_mac_font_name (char *name, int size, short scriptcode)
 
 
 static char *
-mac_to_x_fontname (char *name, int size, Style style, short scriptcode)
+mac_to_x_fontname (name, size, style, scriptcode, encoding_base)
+     char *name;
+     int size;
+     Style style;
+#if TARGET_API_MAC_CARBON
+     int scriptcode;
+#else
+     short scriptcode;
+#endif
 {
   char foundry[32], family[32], cs[32];
   char xf[256], *result, *p;
@@ -5886,13 +5950,13 @@ mac_to_x_fontname (char *name, int size, Style style, short scriptcode)
 
       switch (scriptcode)
       {
-      case smTradChinese:
+      case smTradChinese:      /* == kTextEncodingMacChineseTrad */
         strcpy(cs, "big5-0");
         break;
-      case smSimpChinese:
+      case smSimpChinese:      /* == kTextEncodingMacChineseSimp */
         strcpy(cs, "gb2312.1980-0");
         break;
-      case smJapanese:
+      case smJapanese:         /* == kTextEncodingMacJapanese */
         strcpy(cs, "jisx0208.1983-sjis");
         break;
       case -smJapanese:
@@ -5904,12 +5968,24 @@ mac_to_x_fontname (char *name, int size, Style style, short scriptcode)
           font is being built.  */
        strcpy(cs, "jisx0201.1976-0");
        break;
-      case smKorean:
+      case smKorean:           /* == kTextEncodingMacKorean */
         strcpy(cs, "ksc5601.1989-0");
         break;
+#if TARGET_API_MAC_CARBON
+      case kTextEncodingMacCyrillic:
+       strcpy(cs, "mac-cyrillic");
+       break;
+      case kTextEncodingMacCentralEurRoman:
+       strcpy(cs, "mac-centraleurroman");
+       break;
+      case kTextEncodingMacSymbol:
+      case kTextEncodingMacDingbats:
+       strcpy(cs, "adobe-fontspecific");
+       break;
+#endif
       default:
-        strcpy(cs, "mac-roman");
-        break;
+       strcpy(cs, "mac-roman");
+       break;
       }
     }
 
@@ -5956,7 +6032,10 @@ x_font_name_to_mac_font_name (char *xf, char *mf)
     coding_system = Qsjis;
   else if (strcmp (cs, "ksc5601.1989-0") == 0)
     coding_system = Qeuc_kr;
-  else if (strcmp (cs, "mac-roman") == 0)
+  else if (strcmp (cs, "mac-roman") == 0
+          || strcmp (cs, "mac-cyrillic") == 0
+          || strcmp (cs, "mac-centraleurroman") == 0
+          || strcmp (cs, "adobe-fontspecific") == 0)
     strcpy (mf, family);
   else
     sprintf (mf, "%s-%s-%s", foundry, family, cs);
@@ -6028,7 +6107,8 @@ init_font_name_table ()
          FMFont font;
          FMFontStyle style;
          FMFontSize size;
-         SInt16 sc;
+         TextEncoding encoding;
+         TextEncodingBase sc;
 
          if (FMGetFontFamilyName (ff, name) != noErr)
            break;
@@ -6036,9 +6116,11 @@ init_font_name_table ()
          if (*name == '.')
            continue;
 
-         sc = FontToScript (ff);
+         if (FMGetFontFamilyTextEncoding (ff, &encoding) != noErr)
+           break;
+         sc = GetTextEncodingBase (encoding);
          decode_mac_font_name (name, sizeof (name), sc);
-
+                                      
          /* Point the instance iterator at the current font family.  */
          if (FMResetFontFamilyInstanceIterator (ff, &ffii) != noErr)
            break;
@@ -6193,6 +6275,28 @@ static int xlfd_scalable_fields[] =
     -1
   };
 
+static Lisp_Object
+mac_c_string_match (regexp, string, nonspecial, exact)
+     Lisp_Object regexp;
+     const char *string, *nonspecial;
+     int exact;
+{
+  if (exact)
+    {
+      if (strcmp (string, nonspecial) == 0)
+       return build_string (string);
+    }
+  else if (strstr (string, nonspecial))
+    {
+      Lisp_Object str = build_string (string);
+
+      if (fast_string_match (regexp, str) >= 0)
+       return str;
+    }
+
+  return Qnil;
+}
+
 static Lisp_Object
 mac_do_list_fonts (pattern, maxnames)
      char *pattern;
@@ -6204,6 +6308,8 @@ mac_do_list_fonts (pattern, maxnames)
   char scaled[256];
   char *ptr;
   int scl_val[XLFD_SCL_LAST], *field, *val;
+  char *longest_start, *cur_start, *nonspecial;
+  int longest_len, cur_len, exact;
 
   for (i = 0; i < XLFD_SCL_LAST; i++)
     scl_val[i] = -1;
@@ -6261,34 +6367,66 @@ mac_do_list_fonts (pattern, maxnames)
   ptr = regex;
   *ptr++ = '^';
 
-  /* Turn pattern into a regexp and do a regexp match.  */
+  longest_start = cur_start = ptr;
+  longest_len = cur_len = 0;
+  exact = 1;
+
+  /* Turn pattern into a regexp and do a regexp match.  Also find the
+     longest substring containing no special characters.  */
   for (; *pattern; pattern++)
     {
-      if (*pattern == '?')
-        *ptr++ = '.';
-      else if (*pattern == '*')
-        {
-          *ptr++ = '.';
-          *ptr++ = '*';
-        }
+      if (*pattern == '?' || *pattern == '*')
+       {
+         if (cur_len > longest_len)
+           {
+             longest_start = cur_start;
+             longest_len = cur_len;
+           }
+         cur_len = 0;
+         exact = 0;
+
+         if (*pattern == '?')
+           *ptr++ = '.';
+         else /* if (*pattern == '*') */
+           {
+             *ptr++ = '.';
+             *ptr++ = '*';
+           }
+       }
       else
-        *ptr++ = tolower (*pattern);
+       {
+         if (cur_len == 0)
+           cur_start = ptr;
+         cur_len++;
+
+         *ptr++ = tolower (*pattern);
+       }
     }
+
+  if (cur_len > longest_len)
+    {
+      longest_start = cur_start;
+      longest_len = cur_len;
+    }
+
   *ptr = '$';
   *(ptr + 1) = '\0';
 
+  nonspecial = xmalloc (longest_len + 1);
+  strncpy (nonspecial, longest_start, longest_len);
+  nonspecial[longest_len] = '\0';
+
   pattern_regex = build_string (regex);
 
   for (i = 0; i < font_name_count; i++)
     {
-      fontname = build_string (font_name_table[i]);
-      if (fast_string_match (pattern_regex, fontname) >= 0)
+      fontname = mac_c_string_match (pattern_regex, font_name_table[i],
+                                    nonspecial, exact);
+      if (!NILP (fontname))
        {
          font_list = Fcons (fontname, font_list);
-
-          n_fonts++;
-          if (maxnames > 0 && n_fonts >= maxnames)
-            break;
+         if (exact || maxnames > 0 && ++n_fonts >= maxnames)
+           break;
        }
       else if (scl_val[XLFD_SCL_PIXEL_SIZE] > 0
               && (ptr = strstr (font_name_table[i], "-0-0-75-75-m-0-")))
@@ -6302,17 +6440,19 @@ mac_do_list_fonts (pattern, maxnames)
                   scl_val[XLFD_SCL_POINT_SIZE],
                   scl_val[XLFD_SCL_AVGWIDTH],
                   ptr + sizeof ("-0-0-75-75-m-0-") - 1);
-         fontname = build_string (scaled);
-         if (fast_string_match (pattern_regex, fontname) >= 0)
+         fontname = mac_c_string_match (pattern_regex, scaled,
+                                        nonspecial, exact);
+         if (!NILP (fontname))
            {
              font_list = Fcons (fontname, font_list);
-
-             n_fonts++;
-             if (maxnames > 0 && n_fonts >= maxnames)
+             if (exact || maxnames > 0 && ++n_fonts >= maxnames)
                break;
            }
        }
     }
+
+  xfree (nonspecial);
+
   return font_list;
 }
 
@@ -6904,6 +7044,7 @@ x_find_ccl_program (fontp)
 /* true when cannot handle any Mac OS events */
 static int handling_window_update = 0;
 
+#if 0
 /* the flag appl_is_suspended is used both for determining the sleep
    time to be passed to WaitNextEvent and whether the cursor should be
    drawn when updating the display.  The cursor is turned off when
@@ -6913,6 +7054,7 @@ static int handling_window_update = 0;
    suspended.  */
 static Boolean app_is_suspended = false;
 static long app_sleep_time = WNE_SLEEP_AT_RESUME;
+#endif
 
 #define EXTRA_STACK_ALLOC (256 * 1024)
 
@@ -6928,6 +7070,9 @@ Lisp_Object Qreverse;
 /* True if using command key as meta key.  */
 Lisp_Object Vmac_command_key_is_meta;
 
+/* Modifier associated with the option key, or nil for normal behavior. */
+Lisp_Object Vmac_option_modifier;
+
 /* True if the ctrl and meta keys should be reversed.  */
 Lisp_Object Vmac_reverse_ctrl_meta;
 
@@ -7009,6 +7154,12 @@ mac_to_emacs_modifiers (EventModifiers mods)
     result |= meta_modifier;
   if (NILP (Vmac_command_key_is_meta) && (mods & macAltKey))
     result |= alt_modifier;
+  if (!NILP (Vmac_option_modifier) && (mods & optionKey)) {
+      Lisp_Object val = Fget(Vmac_option_modifier, Qmodifier_value);
+      if (!NILP(val))
+          result |= XUINT(val);
+  }
+
   return result;
 }
 
@@ -7018,7 +7169,7 @@ mac_get_emulated_btn ( UInt32 modifiers )
   int result = 0;
   if (!NILP (Vmac_emulate_three_button_mouse)) {
     int cmdIs3 = !EQ (Vmac_emulate_three_button_mouse, Qreverse);
-    if (modifiers & controlKey)
+    if (modifiers & cmdKey)
       result = cmdIs3 ? 2 : 1;
     else if (modifiers & optionKey)
       result = cmdIs3 ? 1 : 2;
@@ -7038,7 +7189,7 @@ mac_event_to_emacs_modifiers (EventRef eventRef)
   if (!NILP (Vmac_emulate_three_button_mouse) &&
       GetEventClass(eventRef) == kEventClassMouse)
     {
-      mods &= ~(optionKey & cmdKey);
+      mods &= ~(optionKey | cmdKey);
     }
   return mac_to_emacs_modifiers (mods);
 }
@@ -7236,48 +7387,16 @@ is_emacs_window (WindowPtr win)
   return 0;
 }
 
-static void
-do_window_activate (WindowPtr win)
-{
-  struct frame *f;
-
-  if (is_emacs_window (win))
-    {
-      f = mac_window_to_frame (win);
-
-      if (f)
-       {
-         x_new_focus_frame (FRAME_MAC_DISPLAY_INFO (f), f);
-         activate_scroll_bars (f);
-       }
-    }
-}
-
-static void
-do_window_deactivate (WindowPtr win)
-{
-  struct frame *f;
-
-  if (is_emacs_window (win))
-    {
-      f = mac_window_to_frame (win);
-
-      if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
-       {
-         x_new_focus_frame (FRAME_MAC_DISPLAY_INFO (f), 0);
-         deactivate_scroll_bars (f);
-       }
-    }
-}
-
 static void
 do_app_resume ()
 {
+  /* Window-activate events will do the job. */
+#if 0
   WindowPtr wp;
   struct frame *f;
 
-  wp = mac_front_window ();
-  if (is_emacs_window (wp))
+  wp = front_emacs_window ();
+  if (wp)
     {
       f = mac_window_to_frame (wp);
 
@@ -7290,16 +7409,19 @@ do_app_resume ()
 
   app_is_suspended = false;
   app_sleep_time = WNE_SLEEP_AT_RESUME;
+#endif
 }
 
 static void
 do_app_suspend ()
 {
+  /* Window-deactivate events will do the job. */
+#if 0
   WindowPtr wp;
   struct frame *f;
 
-  wp = mac_front_window ();
-  if (is_emacs_window (wp))
+  wp = front_emacs_window ();
+  if (wp)
     {
       f = mac_window_to_frame (wp);
 
@@ -7312,6 +7434,7 @@ do_app_suspend ()
 
   app_is_suspended = true;
   app_sleep_time = WNE_SLEEP_AT_SUSPEND;
+#endif
 }
 
 
@@ -7320,10 +7443,10 @@ do_mouse_moved (mouse_pos, f)
      Point mouse_pos;
      FRAME_PTR *f;
 {
-  WindowPtr wp = mac_front_window ();
+  WindowPtr wp = front_emacs_window ();
   struct x_display_info *dpyinfo;
 
-  if (is_emacs_window (wp))
+  if (wp)
     {
       *f = mac_window_to_frame (wp);
       dpyinfo = FRAME_MAC_DISPLAY_INFO (*f);
@@ -7385,7 +7508,7 @@ do_menu_choice (SInt32 menu_choice)
 
     default:
       {
-        struct frame *f = mac_window_to_frame (mac_front_window ());
+        struct frame *f = mac_window_to_frame (front_emacs_window ());
         MenuHandle menu = GetMenuHandle (menu_id);
         if (menu)
           {
@@ -7438,41 +7561,43 @@ do_zoom_window (WindowPtr w, int zoom_in_or_out)
   GrafPtr save_port;
   Rect zoom_rect, port_rect;
   Point top_left;
-  int w_title_height, columns, rows, width, height, dummy, x, y;
+  int w_title_height, columns, rows;
   struct frame *f = mac_window_to_frame (w);
 
-  GetPort (&save_port);
-
-  SetPortWindowPort (w);
-
-  /* Clear window to avoid flicker.  */
 #if TARGET_API_MAC_CARBON
   {
-    Rect r;
-    BitMap bm;
+    Point standard_size;
 
-    GetWindowPortBounds (w, &r);
-    EraseRect (&r);
+    standard_size.h = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, DEFAULT_NUM_COLS);
+    standard_size.v = FRAME_MAC_DISPLAY_INFO (f)->height;
 
-    if (zoom_in_or_out == inZoomOut)
+    if (IsWindowInStandardState (w, &standard_size, &zoom_rect))
+      zoom_in_or_out = inZoomIn;
+    else
       {
-        /* calculate height of window's title bar (hard card it for now).  */
-        w_title_height = 20 + GetMBarHeight ();
-
-        /* get maximum height of window into zoom_rect.bottom -
-          zoom_rect.top */
-        GetQDGlobalsScreenBits (&bm);
-        zoom_rect = bm.bounds;
-        zoom_rect.top += w_title_height;
-        InsetRect (&zoom_rect, 8, 4);  /* not too tight */
-
-        zoom_rect.right = zoom_rect.left
-         + FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, DEFAULT_NUM_COLS);
-
-        SetWindowStandardState (w, &zoom_rect);
+       /* Adjust the standard size according to character boundaries.  */
+
+       columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, zoom_rect.right - zoom_rect.left);
+       rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, zoom_rect.bottom - zoom_rect.top);
+       standard_size.h = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, columns);
+       standard_size.v = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
+       GetWindowBounds (w, kWindowContentRgn, &port_rect);
+       if (IsWindowInStandardState (w, &standard_size, &zoom_rect)
+           && port_rect.left == zoom_rect.left
+           && port_rect.top == zoom_rect.top)
+         zoom_in_or_out = inZoomIn;
+       else
+         zoom_in_or_out = inZoomOut;
       }
+
+    ZoomWindowIdeal (w, zoom_in_or_out, &standard_size);
   }
 #else /* not TARGET_API_MAC_CARBON */
+  GetPort (&save_port);
+
+  SetPortWindowPort (w);
+
+  /* Clear window to avoid flicker.  */
   EraseRect (&(w->portRect));
   if (zoom_in_or_out == inZoomOut)
     {
@@ -7491,12 +7616,19 @@ do_zoom_window (WindowPtr w, int zoom_in_or_out)
       zoom_rect.right = zoom_rect.left
        + FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, DEFAULT_NUM_COLS);
 
+      /* Adjust the standard size according to character boundaries.  */
+      rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, zoom_rect.bottom - zoom_rect.top);
+      zoom_rect.bottom =
+       zoom_rect.top + FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
+
       (**((WStateDataHandle) ((WindowPeek) w)->dataHandle)).stdState
        = zoom_rect;
     }
-#endif /* not TARGET_API_MAC_CARBON */
 
-  ZoomWindow (w, zoom_in_or_out, w == mac_front_window ());
+  ZoomWindow (w, zoom_in_or_out, w == front_emacs_window ());
+
+  SetPort (save_port);
+#endif /* not TARGET_API_MAC_CARBON */
 
   /* retrieve window size and update application values */
 #if TARGET_API_MAC_CARBON
@@ -7507,8 +7639,7 @@ do_zoom_window (WindowPtr w, int zoom_in_or_out)
   rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, port_rect.bottom - port_rect.top);
   columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, port_rect.right - port_rect.left);
   x_set_window_size (f, 0, columns, rows);
-
-  SetPort (save_port);
+  x_real_positions (f, &f->left_pos, &f->top_pos);
 }
 
 /* Initialize Drag And Drop to allow files to be dropped onto emacs frames */
@@ -8058,19 +8189,15 @@ XTread_socket (sd, expected, hold_quit)
   if (NILP (Fboundp (Qmac_ready_for_drag_n_drop)))
     event_mask -= highLevelEventMask;
 
-  while (WaitNextEvent (event_mask, &er,
-                       (expected ? app_sleep_time : 0L), NULL))
-#else
-  while (!ReceiveNextEvent (0, NULL,
-                           (expected ? TicksToEventTime (app_sleep_time) : 0),
+  while (WaitNextEvent (event_mask, &er, 0L, NULL))
+#else /* USE_CARBON_EVENTS */
+  while (!ReceiveNextEvent (0, NULL, kEventDurationNoWait,
                            kEventRemoveFromQueue, &eventRef))
-#endif /* !USE_CARBON_EVENTS */
+#endif /* USE_CARBON_EVENTS */
     {
       int do_help = 0;
       struct frame *f;
 
-      expected = 0;
-
       /* It is necessary to set this (additional) argument slot of an
         event to nil because keyboard.c protects incompletely
         processed event from being garbage collected by placing them
@@ -8084,12 +8211,25 @@ XTread_socket (sd, expected, hold_quit)
       if (!mac_convert_event_ref (eventRef, &er))
        switch (GetEventClass (eventRef))
          {
+         case kEventClassWindow:
+           if (GetEventKind (eventRef) == kEventWindowBoundsChanged)
+             {
+               WindowPtr window_ptr;
+               GetEventParameter(eventRef, kEventParamDirectObject,
+                                 typeWindowRef, NULL, sizeof(WindowPtr),
+                                 NULL, &window_ptr);
+               f = mac_window_to_frame (window_ptr);
+               if (f && !f->async_iconified)
+                 x_real_positions (f, &f->left_pos, &f->top_pos);
+               SendEventToEventTarget (eventRef, toolbox_dispatcher);
+             }
+           break;
          case kEventClassMouse:
            if (GetEventKind (eventRef) == kEventMouseWheelMoved)
              {
                SInt32 delta;
                Point point;
-               WindowPtr window_ptr = mac_front_window ();
+               WindowPtr window_ptr = front_emacs_window ();
 
                if (!IsValidWindowPtr (window_ptr))
                  {
@@ -8135,6 +8275,14 @@ XTread_socket (sd, expected, hold_quit)
            SInt16 part_code;
            int tool_bar_p = 0;
 
+#if USE_CARBON_EVENTS
+           /* This is needed to send mouse events like aqua window
+              buttons to the correct handler.  */
+           if (SendEventToEventTarget (eventRef, toolbox_dispatcher)
+               != eventNotHandledErr)
+             break;
+#endif
+
            if (dpyinfo->grabbed && last_mouse_frame
                && FRAME_LIVE_P (last_mouse_frame))
              {
@@ -8143,40 +8291,28 @@ XTread_socket (sd, expected, hold_quit)
              }
            else
              {
-               window_ptr = FrontWindow ();
+               part_code = FindWindow (er.where, &window_ptr);
                if (tip_window && window_ptr == tip_window)
                  {
                    HideWindow (tip_window);
-                   window_ptr = FrontWindow ();
+                   part_code = FindWindow (er.where, &window_ptr);
                  }
-
-#if USE_CARBON_EVENTS
-               /* This is needed to send mouse events like aqua
-                  window buttons to the correct handler.  */
-               if (SendEventToEventTarget (eventRef, toolbox_dispatcher)
-                   != eventNotHandledErr)
-                 break;
-
-               if (!is_emacs_window (window_ptr))
-                 break;
-#endif
-               part_code = FindWindow (er.where, &window_ptr);
              }
 
+           if (er.what != mouseDown && part_code != inContent)
+             break;
+
            switch (part_code)
              {
              case inMenuBar:
-               if (er.what == mouseDown)
-                 {
-                   f = mac_window_to_frame (mac_front_window ());
-                   saved_menu_event_location = er.where;
-                   inev.kind = MENU_BAR_ACTIVATE_EVENT;
-                   XSETFRAME (inev.frame_or_window, f);
-                 }
+               f = mac_window_to_frame (front_emacs_window ());
+               saved_menu_event_location = er.where;
+               inev.kind = MENU_BAR_ACTIVATE_EVENT;
+               XSETFRAME (inev.frame_or_window, f);
                break;
 
              case inContent:
-               if (window_ptr != mac_front_window ())
+               if (window_ptr != front_emacs_window ())
                  SelectWindow (window_ptr);
                else
                  {
@@ -8273,7 +8409,7 @@ XTread_socket (sd, expected, hold_quit)
                      }
                    else
                      {
-                       if (dpyinfo->grabbed & (1 << inev.code) == 0)
+                       if ((dpyinfo->grabbed & (1 << inev.code)) == 0)
                          /* If a button is released though it was not
                             previously pressed, that would be because
                             of multi-button emulation.  */
@@ -8296,16 +8432,17 @@ XTread_socket (sd, expected, hold_quit)
 
              case inDrag:
 #if TARGET_API_MAC_CARBON
-               if (er.what == mouseDown)
-                 {
-                   BitMap bm;
-
-                   GetQDGlobalsScreenBits (&bm);
-                   DragWindow (window_ptr, er.where, &bm.bounds);
-                 }
+               DragWindow (window_ptr, er.where, NULL);
 #else /* not TARGET_API_MAC_CARBON */
                DragWindow (window_ptr, er.where, &qd.screenBits.bounds);
 #endif /* not TARGET_API_MAC_CARBON */
+               /* Update the frame parameters.  */
+               {
+                 struct frame *f = mac_window_to_frame (window_ptr);
+
+                 if (f && !f->async_iconified)
+                   x_real_positions (f, &f->left_pos, &f->top_pos);
+               }
                break;
 
              case inGoAway:
@@ -8319,11 +8456,8 @@ XTread_socket (sd, expected, hold_quit)
 
                /* window resize handling added --ben */
              case inGrow:
-               if (er.what == mouseDown)
-                 {
-                   do_grow_window(window_ptr, &er);
-                   break;
-                 }
+               do_grow_window (window_ptr, &er);
+               break;
 
                /* window zoom handling added --ben */
              case inZoomIn:
@@ -8393,24 +8527,38 @@ XTread_socket (sd, expected, hold_quit)
                break;
              }
 
+           if (!is_emacs_window (window_ptr))
+             break;
+
+           f = mac_window_to_frame (window_ptr);
+
            if ((er.modifiers & activeFlag) != 0)
              {
+               /* A window has been activated */
                Point mouse_loc = er.where;
 
-               do_window_activate (window_ptr);
+               x_new_focus_frame (dpyinfo, f);
+               activate_scroll_bars (f);
 
                SetPortWindowPort (window_ptr);
                GlobalToLocal (&mouse_loc);
-               /* activateEvt counts as mouse movement,
+               /* Window-activated event counts as mouse movement,
                   so update things that depend on mouse position.  */
                note_mouse_movement (mac_window_to_frame (window_ptr),
                                     &mouse_loc);
              }
            else
              {
-               do_window_deactivate (window_ptr);
+               /* A window has been deactivated */
+               dpyinfo->grabbed = 0;
+
+               if (f == dpyinfo->x_focus_frame)
+                 {
+                   x_new_focus_frame (dpyinfo, 0);
+                   deactivate_scroll_bars (f);
+                 }
+
 
-               f = mac_window_to_frame (window_ptr);
                if (f == dpyinfo->mouse_face_mouse_frame)
                  {
                    /* If we move outside the frame, then we're
@@ -8452,7 +8600,7 @@ XTread_socket (sd, expected, hold_quit)
 #endif
 
 #if TARGET_API_MAC_CARBON
-           if (!IsValidWindowPtr (mac_front_window ()))
+           if (!IsValidWindowPtr (front_emacs_window ()))
              {
                SysBeep (1);
                break;
@@ -8492,7 +8640,18 @@ XTread_socket (sd, expected, hold_quit)
                    unsigned long some_state = 0;
                    inev.code = KeyTranslate (kchr_ptr, new_keycode,
                                              &some_state) & 0xff;
-                 }
+                 } else if (!NILP(Vmac_option_modifier) && (er.modifiers & optionKey))
+            {
+                /* When using the option key as an emacs modifier, convert
+                   the pressed key code back to one without the Mac option
+                   modifier applied. */
+                int new_modifiers = er.modifiers & ~optionKey;
+                int new_keycode = keycode | new_modifiers;
+                Ptr kchr_ptr = (Ptr) GetScriptManagerVariable (smKCHRCache);
+                unsigned long some_state = 0;
+                inev.code = KeyTranslate (kchr_ptr, new_keycode,
+                                          &some_state) & 0xff;
+            }
                else
                  inev.code = er.message & charCodeMask;
                inev.kind = ASCII_KEYSTROKE_EVENT;
@@ -8537,17 +8696,30 @@ XTread_socket (sd, expected, hold_quit)
                {
                  unsigned char ch = inev.code;
                  ByteCount actual_input_length, actual_output_length;
-                 unsigned char outch;
-
-                 convert_status = TECConvertText (converter, &ch, 1,
-                                                  &actual_input_length,
-                                                  &outch, 1,
-                                                  &actual_output_length);
-                 if (convert_status == noErr
-                     && actual_input_length == 1
-                     && actual_output_length == 1)
-                   inev.code = outch;
-               }
+                 unsigned char outbuf[32];
+                 
+                  convert_status = TECConvertText (converter, &ch, 1,
+                                                   &actual_input_length,
+                                                  outbuf, 1,
+                                                   &actual_output_length);
+                  if (convert_status == noErr
+                      && actual_input_length == 1
+                      && actual_output_length == 1)
+                   inev.code = *outbuf;
+                 
+                 /* Reset internal states of the converter object.
+                   If it fails, create another one. */
+                 convert_status = TECFlushText (converter, outbuf,
+                                                sizeof (outbuf),
+                                               &actual_output_length);
+                 if (convert_status != noErr)
+                   {
+                     TECDisposeConverter (converter);
+                     TECCreateConverter (&converter,
+                                         kTextEncodingMacRoman,
+                                         mac_keyboard_text_encoding);
+                   }
+                }
            }
 
 #if USE_CARBON_EVENTS
@@ -8556,7 +8728,7 @@ XTread_socket (sd, expected, hold_quit)
          inev.modifiers = mac_to_emacs_modifiers (er.modifiers);
 #endif
          XSETFRAME (inev.frame_or_window,
-                    mac_window_to_frame (mac_front_window ()));
+                    mac_window_to_frame (front_emacs_window ()));
          inev.timestamp = er.when * (1000 / 60);  /* ticks to milliseconds */
          break;
 
@@ -8573,16 +8745,16 @@ XTread_socket (sd, expected, hold_quit)
              WindowPtr wp;
              Lisp_Object frame;
 
-             wp = mac_front_window ();
+             wp = front_emacs_window ();
 
              if (!wp)
                {
                  struct frame *f = XFRAME (XCAR (Vframe_list));
                  CollapseWindow (FRAME_MAC_WINDOW (f), false);
-                 wp = mac_front_window ();
+                 wp = front_emacs_window ();
                }
 
-             if (wp && is_emacs_window (wp))
+             if (wp)
                f = mac_window_to_frame (wp);
 
              inev.kind = DRAG_N_DROP_EVENT;
@@ -8708,6 +8880,7 @@ make_mac_frame (FRAME_PTR fp)
 
   mwp = fp->output_data.mac;
 
+  BLOCK_INPUT;
   if (making_terminal_window)
     {
       if (!(mwp->mWP = GetNewCWindow (TERM_WINDOW_RESOURCE, NULL,
@@ -8735,9 +8908,8 @@ make_mac_frame (FRAME_PTR fp)
     /* so that update events can find this mac_output struct */
   mwp->mFP = fp;  /* point back to emacs frame */
 
-  SetPortWindowPort (mwp->mWP);
-
   SizeWindow (mwp->mWP, FRAME_PIXEL_WIDTH (fp), FRAME_PIXEL_HEIGHT (fp), false);
+  UNBLOCK_INPUT;
 }
 
 
@@ -8842,8 +9014,21 @@ mac_initialize_display_info ()
   dpyinfo->color_p = TestDeviceAttribute (main_device_handle, gdDevType);
 #ifdef MAC_OSX
   /* HasDepth returns true if it is possible to have a 32 bit display,
-     but this may not be what is actually used.  Mac OSX can do better.  */
-  dpyinfo->n_planes = CGDisplayBitsPerPixel (CGMainDisplayID ());
+     but this may not be what is actually used.  Mac OSX can do better.
+     CGMainDisplayID is only available on OSX 10.2 and higher, but the
+     header for CGGetActiveDisplayList says that the first display returned
+     is the active one, so we use that.  */
+  {
+    CGDirectDisplayID disp_id[1];
+    CGDisplayCount disp_count;
+    CGDisplayErr error_code;
+
+    error_code = CGGetActiveDisplayList (1, disp_id, &disp_count);
+    if (error_code != 0)
+      error ("No display found, CGGetActiveDisplayList error %d", error_code);
+
+    dpyinfo->n_planes = CGDisplayBitsPerPixel (disp_id[0]);
+  }
 #else
   for (dpyinfo->n_planes = 32; dpyinfo->n_planes > 0; dpyinfo->n_planes >>= 1)
     if (HasDepth (main_device_handle, dpyinfo->n_planes,
@@ -9026,7 +9211,7 @@ mac_check_for_quit_char ()
       e.arg = Qnil;
       e.modifiers = NULL;
       e.timestamp = EventTimeToTicks (GetEventTime (event)) * (1000/60);
-      XSETFRAME (e.frame_or_window, mac_window_to_frame (mac_front_window ()));
+      XSETFRAME (e.frame_or_window, mac_window_to_frame (front_emacs_window ()));
       /* Remove event from queue to prevent looping. */
       RemoveEventFromQueue (GetMainEventQueue (), event);
       ReleaseEvent (event);
@@ -9053,7 +9238,7 @@ static struct redisplay_interface x_redisplay_interface =
   x_update_window_end,
   x_cursor_to,
   x_flush,
-  x_flush,
+  0, /* flush_display_optional */
   x_clear_window_mouse_face,
   x_get_glyph_overhangs,
   x_fix_overlapping_area,
@@ -9148,6 +9333,7 @@ mac_initialize ()
   signal (SIGPIPE, x_connection_signal);
 #endif
 
+  BLOCK_INPUT;
   mac_initialize_display_info ();
 
 #if TARGET_API_MAC_CARBON
@@ -9166,6 +9352,7 @@ mac_initialize ()
   if (!inhibit_window_system)
     MakeMeTheFrontProcess ();
 #endif
+  UNBLOCK_INPUT;
 }
 
 
@@ -9177,6 +9364,14 @@ syms_of_macterm ()
   x_error_message_string = Qnil;
 #endif
 
+  Qmodifier_value = intern ("modifier-value");
+  Qalt = intern ("alt");
+  Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
+  Qhyper = intern ("hyper");
+  Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
+  Qsuper = intern ("super");
+  Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
+
   Fprovide (intern ("mac-carbon"), Qnil);
 
   staticpro (&Qreverse);
@@ -9233,6 +9428,12 @@ to 4.1, set this to nil.  */);
 Otherwise the option key is used.  */);
   Vmac_command_key_is_meta = Qt;
 
+  DEFVAR_LISP ("mac-option-modifier", &Vmac_option_modifier,
+    doc: /* Modifier to use for the Mac alt/option key.  The value can
+be alt, hyper, or super for the respective modifier.  If the value is
+nil then the key will act as the normal Mac option modifier.  */);
+  Vmac_option_modifier = Qnil;
+
   DEFVAR_LISP ("mac-reverse-ctrl-meta", &Vmac_reverse_ctrl_meta,
     doc: /* Non-nil means that the control and meta keys are reversed.  This is
            useful for non-standard keyboard layouts.  */);