]> code.delx.au - gnu-emacs/blobdiff - src/macfns.c
*** empty log message ***
[gnu-emacs] / src / macfns.c
index dbb8adb3fe1ac85e106bcae5347383416384a6af..408600c56bbdec27762769ed70eb4c5ae4722125 100644 (file)
@@ -1,5 +1,6 @@
 /* Graphical user interface functions for Mac OS.
-   Copyright (C) 2000, 2001 Free Software Foundation, Inc.
+   Copyright (C) 2000, 2001, 2002, 2003, 2004,
+                 2005, 2006 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -15,76 +16,40 @@ 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
-the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+Boston, MA 02110-1301, USA.  */
 
 /* Contributed by Andrew Choi (akochoi@mac.com).  */
 
 #include <config.h>
-
-#include <signal.h>
 #include <stdio.h>
 #include <math.h>
-#include <limits.h>
-#include <errno.h>
 
 #include "lisp.h"
-#include "charset.h"
 #include "macterm.h"
 #include "frame.h"
 #include "window.h"
 #include "buffer.h"
-#include "dispextern.h"
-#include "fontset.h"
 #include "intervals.h"
+#include "dispextern.h"
 #include "keyboard.h"
 #include "blockinput.h"
-#include "epaths.h"
-#include "termhooks.h"
+#include <epaths.h>
+#include "charset.h"
 #include "coding.h"
-#include "ccl.h"
+#include "fontset.h"
 #include "systime.h"
+#include "termhooks.h"
+#include "atimer.h"
 
-/* #include "bitmaps/gray.xbm" */
-#define gray_width 2
-#define gray_height 2
-static unsigned char gray_bits[] = {
-   0x01, 0x02};
-
-/*#include <commdlg.h>
-#include <shellapi.h>*/
 #include <ctype.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <limits.h>
+#include <errno.h>
+#include <sys/param.h>
 
-#include <stdlib.h>
-#include <string.h>
-#ifndef MAC_OSX
-#include <alloca.h>
-#endif
-
-#ifdef MAC_OSX
-#include <QuickTime/QuickTime.h>
-#else /* not MAC_OSX */
-#include <Windows.h>
-#include <Gestalt.h>
-#include <TextUtils.h>
-#endif /* not MAC_OSX */
-
-/*extern void free_frame_menubar ();
-extern double atof ();
-extern int w32_console_toggle_lock_key (int vk_code, Lisp_Object new_state);
-extern int quit_char;*/
-
-extern char *lispy_function_keys[];
-
-/* The gray bitmap `bitmaps/gray'.  This is done because macterm.c uses
-   it, and including `bitmaps/gray' more than once is a problem when
-   config.h defines `static' as an empty replacement string.  */
-
-int gray_bitmap_width = gray_width;
-int gray_bitmap_height = gray_height;
-unsigned char *gray_bitmap_bits = gray_bits;
+extern void free_frame_menubar ();
 
 /* Non-zero means we're allowed to display an hourglass cursor.  */
 
@@ -121,58 +86,21 @@ Lisp_Object Vx_no_window_manager;
 
 Lisp_Object Vx_pixel_size_width_font_regexp;
 
-/* Evaluate this expression to rebuild the section of syms_of_macfns
-   that initializes and staticpros the symbols declared below.  Note
-   that Emacs 18 has a bug that keeps C-x C-e from being able to
-   evaluate this expression.
-
-(progn
-  ;; Accumulate a list of the symbols we want to initialize from the
-  ;; declarations at the top of the file.
-  (goto-char (point-min))
-  (search-forward "/\*&&& symbols declared here &&&*\/\n")
-  (let (symbol-list)
-    (while (looking-at "Lisp_Object \\(Q[a-z_]+\\)")
-      (setq symbol-list
-           (cons (buffer-substring (match-beginning 1) (match-end 1))
-                 symbol-list))
-      (forward-line 1))
-    (setq symbol-list (nreverse symbol-list))
-    ;; Delete the section of syms_of_... where we initialize the symbols.
-    (search-forward "\n  /\*&&& init symbols here &&&*\/\n")
-    (let ((start (point)))
-      (while (looking-at "^  Q")
-       (forward-line 2))
-      (kill-region start (point)))
-    ;; Write a new symbol initialization section.
-    (while symbol-list
-      (insert (format "  %s = intern (\"" (car symbol-list)))
-      (let ((start (point)))
-       (insert (substring (car symbol-list) 1))
-       (subst-char-in-region start (point) ?_ ?-))
-      (insert (format "\");\n  staticpro (&%s);\n" (car symbol-list)))
-      (setq symbol-list (cdr symbol-list)))))
-
-  */
-
-/*&&& symbols declared here &&&*/
 Lisp_Object Qnone;
 Lisp_Object Qsuppress_icon;
 Lisp_Object Qundefined_color;
 Lisp_Object Qcancel_timer;
-Lisp_Object Qhyper;
-Lisp_Object Qsuper;
-Lisp_Object Qmeta;
-Lisp_Object Qalt;
-Lisp_Object Qctrl;
-Lisp_Object Qcontrol;
-Lisp_Object Qshift;
+
+/* In dispnew.c */
 
 extern Lisp_Object Vwindow_system_version;
 
-extern int mac_initialized;
+#if GLYPH_DEBUG
+int image_cache_refcount, dpyinfo_refcount;
+#endif
 
 
+#if 0 /* Use xstricmp instead.  */
 /* compare two strings ignoring case */
 
 static int
@@ -183,13 +111,14 @@ stricmp (const char *s, const char *t)
       return 0;
   return tolower (*s) - tolower (*t);
 }
+#endif
 
 /* compare two strings up to n characters, ignoring case */
 
 static int
 strnicmp (const char *s, const char *t, unsigned int n)
 {
-  for ( ; n-- > 0 && tolower (*s) == tolower (*t); s++, t++)
+  for ( ; n > 0 && tolower (*s) == tolower (*t); n--, s++, t++)
     if (*s == '\0')
       return 0;
   return n == 0 ? 0 : tolower (*s) - tolower (*t);
@@ -202,7 +131,7 @@ void
 check_mac ()
 {
   if (! mac_in_use)
-    error ("Mac OS not in use or not initialized");
+    error ("Mac native windows not in use or not initialized");
 }
 
 /* Nonzero if we can use mouse menus.
@@ -228,7 +157,7 @@ check_x_frame (frame)
   CHECK_LIVE_FRAME (frame);
   f = XFRAME (frame);
   if (! FRAME_MAC_P (f))
-    error ("non-mac frame used");
+    error ("Non-Mac frame used");
   return f;
 }
 
@@ -240,68 +169,34 @@ struct mac_display_info *
 check_x_display_info (frame)
      Lisp_Object frame;
 {
-  if (!mac_initialized)
-    {
-      mac_initialize ();
-      mac_initialized = 1;
-    }
+  struct mac_display_info *dpyinfo = NULL;
 
   if (NILP (frame))
     {
       struct frame *sf = XFRAME (selected_frame);
 
       if (FRAME_MAC_P (sf) && FRAME_LIVE_P (sf))
-       return FRAME_MAC_DISPLAY_INFO (sf);
+       dpyinfo = FRAME_MAC_DISPLAY_INFO (sf);
+      else if (x_display_list != 0)
+       dpyinfo = x_display_list;
       else
-       return &one_mac_display_info;
+       error ("Mac native windows are not in use or not initialized");
     }
   else if (STRINGP (frame))
-    return x_display_info_for_name (frame);
+    dpyinfo = x_display_info_for_name (frame);
   else
     {
-      FRAME_PTR f;
-
-      CHECK_LIVE_FRAME (frame);
-      f = XFRAME (frame);
-      if (! FRAME_MAC_P (f))
-       error ("non-mac frame used");
-      return FRAME_MAC_DISPLAY_INFO (f);
+      FRAME_PTR f = check_x_frame (frame);
+      dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
     }
-}
-\f
-/* Return the Emacs frame-object corresponding to a mac window.
-   It could be the frame's main window or an icon window.  */
-
-/* This function can be called during GC, so use GC_xxx type test macros.  */
-
-struct frame *
-x_window_to_frame (dpyinfo, wdesc)
-     struct mac_display_info *dpyinfo;
-     WindowPtr wdesc;
-{
-  Lisp_Object tail, frame;
-  struct frame *f;
 
-  for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
-    {
-      frame = XCAR (tail);
-      if (!GC_FRAMEP (frame))
-        continue;
-      f = XFRAME (frame);
-      if (!FRAME_W32_P (f) || FRAME_MAC_DISPLAY_INFO (f) != dpyinfo)
-       continue;
-      /*if (f->output_data.w32->hourglass_window == wdesc)
-        return f;*/
-
-      /* MAC_TODO: Check tooltips when supported.  */
-      if (FRAME_MAC_WINDOW (f) == wdesc)
-        return f;
-    }
-  return 0;
+  return dpyinfo;
 }
 
 \f
+
 static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
+static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
 
 void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
 void x_set_background_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
@@ -315,15 +210,11 @@ void x_explicitly_set_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
 void x_set_menu_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
 void x_set_title P_ ((struct frame *, Lisp_Object, Lisp_Object));
 void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
-void x_set_scroll_bar_foreground P_ ((struct frame *, Lisp_Object,
-                                     Lisp_Object));
-void x_set_scroll_bar_background P_ ((struct frame *, Lisp_Object,
-                                     Lisp_Object));
-static Lisp_Object x_default_scroll_bar_color_parameter P_ ((struct frame *,
-                                                            Lisp_Object,
-                                                            Lisp_Object,
-                                                            char *, char *,
-                                                            int));
+
+extern void mac_get_window_bounds P_ ((struct frame *, Rect *, Rect *));
+
+\f
+
 /* Store the screen positions of frame F into XPTR and YPTR.
    These are the positions of the containing window manager window,
    not Emacs's own window.  */
@@ -333,31 +224,15 @@ x_real_positions (f, xptr, yptr)
      FRAME_PTR f;
      int *xptr, *yptr;
 {
-  Point pt;
-  GrafPtr oldport;
+  Rect inner, outer;
 
-#ifdef TARGET_API_MAC_CARBON
-  {
-    Rect r;
+  mac_get_window_bounds (f, &inner, &outer);
 
-    GetWindowPortBounds (f->output_data.mac->mWP, &r);
-    SetPt (&pt, r.left, r.top);
-  }
-#else /* not TARGET_API_MAC_CARBON */
-  SetPt (&pt,
-        f->output_data.mac->mWP->portRect.left,
-        f->output_data.mac->mWP->portRect.top);
-#endif /* not TARGET_API_MAC_CARBON */
-  GetPort (&oldport);
-  LocalToGlobal (&pt);
-  SetPort (oldport);
-
-  /* MAC has no frame pixel diff.  */
-  f->x_pixels_diff = 0;
-  f->y_pixels_diff = 0;
-
-  *xptr = pt.h;
-  *yptr = pt.v;
+  f->x_pixels_diff = inner.left - outer.left;
+  f->y_pixels_diff = inner.top - outer.top;
+
+  *xptr = outer.left;
+  *yptr = outer.top;
 }
 
 \f
@@ -1134,7 +1009,7 @@ mac_color_map_lookup (colorname)
   BLOCK_INPUT;
 
   for (i = 0; i < sizeof (mac_color_map) / sizeof (mac_color_map[0]); i++)
-    if (stricmp (colorname, mac_color_map[i].name) == 0)
+    if (xstricmp (colorname, mac_color_map[i].name) == 0)
       {
         ret = make_number (mac_color_map[i].color);
         break;
@@ -1149,7 +1024,7 @@ Lisp_Object
 x_to_mac_color (colorname)
      char * colorname;
 {
-  register Lisp_Object tail, ret = Qnil;
+  register Lisp_Object ret = Qnil;
 
   BLOCK_INPUT;
 
@@ -1415,6 +1290,7 @@ x_set_foreground_color (f, arg, oldval)
      struct frame *f;
      Lisp_Object arg, oldval;
 {
+  struct mac_output *mac = f->output_data.mac;
   unsigned long fg, old_fg;
 
   fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
@@ -1423,10 +1299,28 @@ x_set_foreground_color (f, arg, oldval)
 
   if (FRAME_MAC_WINDOW (f) != 0)
     {
+      Display *dpy = FRAME_MAC_DISPLAY (f);
+
+      BLOCK_INPUT;
+      XSetForeground (dpy, mac->normal_gc, fg);
+      XSetBackground (dpy, mac->reverse_gc, fg);
+
+      if (mac->cursor_pixel == old_fg)
+       {
+         unload_color (f, mac->cursor_pixel);
+         mac->cursor_pixel = fg;
+         XSetBackground (dpy, mac->cursor_gc, mac->cursor_pixel);
+       }
+
+      UNBLOCK_INPUT;
+
       update_face_from_frame_parameter (f, Qforeground_color, arg);
+
       if (FRAME_VISIBLE_P (f))
         redraw_frame (f);
     }
+
+  unload_color (f, old_fg);
 }
 
 void
@@ -1434,11 +1328,24 @@ x_set_background_color (f, arg, oldval)
      struct frame *f;
      Lisp_Object arg, oldval;
 {
-  FRAME_BACKGROUND_PIXEL (f)
-    = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
+  struct mac_output *mac = f->output_data.mac;
+  unsigned long bg;
+
+  bg = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
+  unload_color (f, FRAME_BACKGROUND_PIXEL (f));
+  FRAME_BACKGROUND_PIXEL (f) = bg;
 
   if (FRAME_MAC_WINDOW (f) != 0)
     {
+      Display *dpy = FRAME_MAC_DISPLAY (f);
+
+      BLOCK_INPUT;
+      XSetBackground (dpy, mac->normal_gc, bg);
+      XSetForeground (dpy, mac->reverse_gc, bg);
+      XSetWindowBackground (dpy, FRAME_MAC_WINDOW (f), bg);
+      XSetForeground (dpy, mac->cursor_gc, bg);
+
+      UNBLOCK_INPUT;
       update_face_from_frame_parameter (f, Qbackground_color, arg);
 
       if (FRAME_VISIBLE_P (f))
@@ -1451,148 +1358,99 @@ x_set_mouse_color (f, arg, oldval)
      struct frame *f;
      Lisp_Object arg, oldval;
 {
+  struct x_output *x = f->output_data.x;
   Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
-  int count;
-  int mask_color;
-
-  if (!EQ (Qnil, arg))
-    f->output_data.mac->mouse_pixel
-      = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
-  mask_color = FRAME_BACKGROUND_PIXEL (f);
+  Cursor hourglass_cursor, horizontal_drag_cursor;
+  unsigned long pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
+  unsigned long mask_color = x->background_pixel;
 
   /* Don't let pointers be invisible.  */
-  if (mask_color == f->output_data.mac->mouse_pixel
-       && mask_color == FRAME_BACKGROUND_PIXEL (f))
-    f->output_data.mac->mouse_pixel = FRAME_FOREGROUND_PIXEL (f);
+  if (mask_color == pixel)
+    pixel = x->foreground_pixel;
 
-#if 0 /* MAC_TODO : cursor changes */
-  BLOCK_INPUT;
-
-  /* It's not okay to crash if the user selects a screwy cursor.  */
-  count = x_catch_errors (FRAME_W32_DISPLAY (f));
+  f->output_data.mac->mouse_pixel = pixel;
 
-  if (!EQ (Qnil, Vx_pointer_shape))
+  if (!NILP (Vx_pointer_shape))
     {
       CHECK_NUMBER (Vx_pointer_shape);
-      cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XINT (Vx_pointer_shape));
+      cursor = XINT (Vx_pointer_shape);
     }
   else
-    cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
-  x_check_errors (FRAME_W32_DISPLAY (f), "bad text pointer cursor: %s");
+    cursor = kThemeIBeamCursor;
 
-  if (!EQ (Qnil, Vx_nontext_pointer_shape))
+  if (!NILP (Vx_nontext_pointer_shape))
     {
       CHECK_NUMBER (Vx_nontext_pointer_shape);
-      nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
-                                         XINT (Vx_nontext_pointer_shape));
+      nontext_cursor = XINT (Vx_nontext_pointer_shape);
     }
   else
-    nontext_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_left_ptr);
-  x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
+    nontext_cursor = kThemeArrowCursor;
 
-  if (!EQ (Qnil, Vx_hourglass_pointer_shape))
+  if (!NILP (Vx_hourglass_pointer_shape))
     {
       CHECK_NUMBER (Vx_hourglass_pointer_shape);
-      hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
-                                           XINT (Vx_hourglass_pointer_shape));
+      hourglass_cursor = XINT (Vx_hourglass_pointer_shape);
     }
   else
-    hourglass_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_watch);
-  x_check_errors (FRAME_W32_DISPLAY (f), "bad busy pointer cursor: %s");
+    hourglass_cursor = kThemeWatchCursor;
 
-  x_check_errors (FRAME_W32_DISPLAY (f), "bad nontext pointer cursor: %s");
-  if (!EQ (Qnil, Vx_mode_pointer_shape))
+  if (!NILP (Vx_mode_pointer_shape))
     {
       CHECK_NUMBER (Vx_mode_pointer_shape);
-      mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f),
-                                      XINT (Vx_mode_pointer_shape));
+      mode_cursor = XINT (Vx_mode_pointer_shape);
     }
   else
-    mode_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_xterm);
-  x_check_errors (FRAME_W32_DISPLAY (f), "bad modeline pointer cursor: %s");
+    mode_cursor = kThemeArrowCursor;
 
-  if (!EQ (Qnil, Vx_sensitive_text_pointer_shape))
+  if (!NILP (Vx_sensitive_text_pointer_shape))
     {
       CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
-      hand_cursor
-       = XCreateFontCursor (FRAME_W32_DISPLAY (f),
-                            XINT (Vx_sensitive_text_pointer_shape));
+      hand_cursor = XINT (Vx_sensitive_text_pointer_shape);
     }
   else
-    hand_cursor = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_crosshair);
+    hand_cursor = kThemePointingHandCursor;
 
   if (!NILP (Vx_window_horizontal_drag_shape))
     {
       CHECK_NUMBER (Vx_window_horizontal_drag_shape);
-      horizontal_drag_cursor
-       = XCreateFontCursor (FRAME_W32_DISPLAY (f),
-                            XINT (Vx_window_horizontal_drag_shape));
+      horizontal_drag_cursor = XINT (Vx_window_horizontal_drag_shape);
     }
   else
-    horizontal_drag_cursor
-      = XCreateFontCursor (FRAME_W32_DISPLAY (f), XC_sb_h_double_arrow);
-
-  /* Check and report errors with the above calls.  */
-  x_check_errors (FRAME_W32_DISPLAY (f), "can't set cursor shape: %s");
-  x_uncatch_errors (FRAME_W32_DISPLAY (f), count);
+    horizontal_drag_cursor = kThemeResizeLeftRightCursor;
 
+#if 0 /* MAC_TODO: cursor color changes */
   {
     XColor fore_color, back_color;
 
-    fore_color.pixel = f->output_data.w32->mouse_pixel;
+    fore_color.pixel = f->output_data.mac->mouse_pixel;
+    x_query_color (f, &fore_color);
     back_color.pixel = mask_color;
-    XQueryColor (FRAME_W32_DISPLAY (f),
-                DefaultColormap (FRAME_W32_DISPLAY (f),
-                                 DefaultScreen (FRAME_W32_DISPLAY (f))),
-                &fore_color);
-    XQueryColor (FRAME_W32_DISPLAY (f),
-                DefaultColormap (FRAME_W32_DISPLAY (f),
-                                 DefaultScreen (FRAME_W32_DISPLAY (f))),
-                &back_color);
-    XRecolorCursor (FRAME_W32_DISPLAY (f), cursor,
-                   &fore_color, &back_color);
-    XRecolorCursor (FRAME_W32_DISPLAY (f), nontext_cursor,
-                   &fore_color, &back_color);
-    XRecolorCursor (FRAME_W32_DISPLAY (f), mode_cursor,
-                   &fore_color, &back_color);
-    XRecolorCursor (FRAME_W32_DISPLAY (f), hand_cursor,
-                    &fore_color, &back_color);
-    XRecolorCursor (FRAME_W32_DISPLAY (f), hourglass_cursor,
-                    &fore_color, &back_color);
+    x_query_color (f, &back_color);
+
+    XRecolorCursor (dpy, cursor, &fore_color, &back_color);
+    XRecolorCursor (dpy, nontext_cursor, &fore_color, &back_color);
+    XRecolorCursor (dpy, mode_cursor, &fore_color, &back_color);
+    XRecolorCursor (dpy, hand_cursor, &fore_color, &back_color);
+    XRecolorCursor (dpy, hourglass_cursor, &fore_color, &back_color);
+    XRecolorCursor (dpy, horizontal_drag_cursor, &fore_color, &back_color);
   }
+#endif
 
-  if (FRAME_W32_WINDOW (f) != 0)
-    XDefineCursor (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f), cursor);
-
-  if (cursor != f->output_data.w32->text_cursor && f->output_data.w32->text_cursor != 0)
-    XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->text_cursor);
-  f->output_data.w32->text_cursor = cursor;
-
-  if (nontext_cursor != f->output_data.w32->nontext_cursor
-      && f->output_data.w32->nontext_cursor != 0)
-    XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->nontext_cursor);
-  f->output_data.w32->nontext_cursor = nontext_cursor;
-
-  if (hourglass_cursor != f->output_data.w32->hourglass_cursor
-      && f->output_data.w32->hourglass_cursor != 0)
-    XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hourglass_cursor);
-  f->output_data.w32->hourglass_cursor = hourglass_cursor;
+  BLOCK_INPUT;
 
-  if (mode_cursor != f->output_data.w32->modeline_cursor
-      && f->output_data.w32->modeline_cursor != 0)
-    XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->modeline_cursor);
-  f->output_data.w32->modeline_cursor = mode_cursor;
+  if (FRAME_MAC_WINDOW (f) != 0)
+    rif->define_frame_cursor (f, cursor);
 
-  if (hand_cursor != f->output_data.w32->hand_cursor
-      && f->output_data.w32->hand_cursor != 0)
-    XFreeCursor (FRAME_W32_DISPLAY (f), f->output_data.w32->hand_cursor);
-  f->output_data.w32->hand_cursor = hand_cursor;
+  f->output_data.mac->text_cursor = cursor;
+  f->output_data.mac->nontext_cursor = nontext_cursor;
+  f->output_data.mac->hourglass_cursor = hourglass_cursor;
+  f->output_data.mac->modeline_cursor = mode_cursor;
+  f->output_data.mac->hand_cursor = hand_cursor;
+  f->output_data.mac->horizontal_drag_cursor = horizontal_drag_cursor;
 
-  XFlush (FRAME_W32_DISPLAY (f));
   UNBLOCK_INPUT;
 
   update_face_from_frame_parameter (f, Qmouse_color, arg);
-#endif /* MAC_TODO */
 }
 
 void
@@ -1625,9 +1483,10 @@ x_set_cursor_color (f, arg, oldval)
     {
       BLOCK_INPUT;
       /* Update frame's cursor_gc.  */
-      f->output_data.mac->cursor_gc->foreground = fore_pixel;
-      f->output_data.mac->cursor_gc->background = pixel;
-
+      XSetBackground (FRAME_MAC_DISPLAY (f),
+                     f->output_data.mac->cursor_gc, pixel);
+      XSetForeground (FRAME_MAC_DISPLAY (f),
+                     f->output_data.mac->cursor_gc, fore_pixel);
       UNBLOCK_INPUT;
 
       if (FRAME_VISIBLE_P (f))
@@ -1778,36 +1637,15 @@ x_set_menu_bar_lines (f, value, oldval)
      struct frame *f;
      Lisp_Object value, oldval;
 {
-  int nlines;
-  int olines = FRAME_MENU_BAR_LINES (f);
-
-  /* Right now, menu bars don't work properly in minibuf-only frames;
-     most of the commands try to apply themselves to the minibuffer
-     frame itself, and get an error because you can't switch buffers
-     in or split the minibuffer window.  */
-  if (FRAME_MINIBUF_ONLY_P (f))
-    return;
-
-  if (INTEGERP (value))
-    nlines = XINT (value);
-  else
-    nlines = 0;
+  /* Make sure we redisplay all windows in this frame.  */
+  windows_or_buffers_changed++;
 
   FRAME_MENU_BAR_LINES (f) = 0;
-  if (nlines)
-    FRAME_EXTERNAL_MENU_BAR (f) = 1;
-  else
-    {
-      if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
-       free_frame_menubar (f);
-      FRAME_EXTERNAL_MENU_BAR (f) = 0;
-
-      /* Adjust the frame size so that the client (text) dimensions
-        remain the same.  This depends on FRAME_EXTERNAL_MENU_BAR being
-        set correctly.  */
-      x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
-      do_pending_window_change (0);
-    }
+  /* The menu bar is always shown.  */
+  FRAME_EXTERNAL_MENU_BAR (f) = 1;
+  if (FRAME_MAC_P (f) && f->output_data.mac->menubar_widget == 0)
+    /* Make sure next redisplay shows the menu bar.  */
+    XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = Qt;
   adjust_glyphs (f);
 }
 
@@ -1878,8 +1716,7 @@ x_set_tool_bar_lines (f, value, oldval)
       int y = nlines * FRAME_LINE_HEIGHT (f);
 
       BLOCK_INPUT;
-      XClearArea (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f),
-                   0, y, width, height, 0);
+      mac_clear_area (f, 0, y, width, height);
       UNBLOCK_INPUT;
 
       if (WINDOWP (f->tool_bar_window))
@@ -1888,8 +1725,49 @@ x_set_tool_bar_lines (f, value, oldval)
 }
 
 
+\f
+/* Set the Mac window title to NAME for frame F.  */
+
+static void
+x_set_name_internal (f, name)
+     FRAME_PTR f;
+     Lisp_Object name;
+{
+  if (FRAME_MAC_WINDOW (f))
+    {
+      if (STRING_MULTIBYTE (name))
+#if TARGET_API_MAC_CARBON
+       name = ENCODE_UTF_8 (name);
+#else
+       name = ENCODE_SYSTEM (name);
+#endif
+
+      BLOCK_INPUT;
+
+      {
+#if TARGET_API_MAC_CARBON
+       CFStringRef windowTitle =
+         cfstring_create_with_utf8_cstring (SDATA (name));
+
+       SetWindowTitleWithCFString (FRAME_MAC_WINDOW (f), windowTitle);
+       CFRelease (windowTitle);
+#else
+       Str255 windowTitle;
+       if (strlen (SDATA (name)) < 255)
+         {
+           strcpy (windowTitle, SDATA (name));
+           c2pstr (windowTitle);
+           SetWTitle (FRAME_MAC_WINDOW (f), windowTitle);
+         }
+#endif
+      }
+
+      UNBLOCK_INPUT;
+    }
+}
+
 /* Change the name of frame F to NAME.  If NAME is nil, set F's name to
-       w32_id_name.
+       mac_id_name.
 
    If EXPLICIT is non-zero, that indicates that lisp code is setting the
        name; if NAME is a string, set F's name to NAME and set
@@ -1919,7 +1797,7 @@ x_set_name (f, name, explicit)
   else if (f->explicit_name)
     return;
 
-  /* If NAME is nil, set the name to the w32_id_name.  */
+  /* If NAME is nil, set the name to the mac_id_name.  */
   if (NILP (name))
     {
       /* Check for no change needed in this very common case
@@ -1943,29 +1821,7 @@ x_set_name (f, name, explicit)
   if (! NILP (f->title))
     name = f->title;
 
-  if (FRAME_MAC_WINDOW (f))
-    {
-      if (STRING_MULTIBYTE (name))
-#if 0 /* MAC_TODO: encoding title string */
-       name = ENCODE_SYSTEM (name);
-#else
-        return;
-#endif
-
-      BLOCK_INPUT;
-
-      {
-       Str255 windowTitle;
-       if (strlen (SDATA (name)) < 255)
-         {
-           strcpy (windowTitle, SDATA (name));
-           c2pstr (windowTitle);
-           SetWTitle (FRAME_MAC_WINDOW (f), windowTitle);
-         }
-      }
-
-      UNBLOCK_INPUT;
-    }
+  x_set_name_internal (f, name);
 }
 
 /* This function should be called when the user's lisp code has
@@ -2016,30 +1872,10 @@ x_set_title (f, name, old_name)
 
   if (NILP (name))
     name = f->name;
+  else
+    CHECK_STRING (name);
 
-  if (FRAME_MAC_WINDOW (f))
-    {
-      if (STRING_MULTIBYTE (name))
-#if 0 /* MAC_TODO: encoding title string */
-       name = ENCODE_SYSTEM (name);
-#else
-        return;
-#endif
-
-      BLOCK_INPUT;
-
-      {
-       Str255 windowTitle;
-       if (strlen (SDATA (name)) < 255)
-         {
-           strcpy (windowTitle, SDATA (name));
-           c2pstr (windowTitle);
-           SetWTitle (FRAME_MAC_WINDOW (f), windowTitle);
-         }
-      }
-
-      UNBLOCK_INPUT;
-    }
+  x_set_name_internal (f, name);
 }
 
 void
@@ -2051,7 +1887,7 @@ x_set_scroll_bar_default_width (f)
   int wid = FRAME_COLUMN_WIDTH (f);
 
 #ifdef MAC_OSX
-  FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = 16;  /* Aqua scroll bars.  */
+  FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = MAC_AQUA_VERTICAL_SCROLL_BAR_WIDTH;
   FRAME_CONFIG_SCROLL_BAR_COLS (f) = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) +
                                      wid - 1) / wid;
 #else /* not MAC_OSX */
@@ -2065,16 +1901,139 @@ x_set_scroll_bar_default_width (f)
 #endif /* not MAC_OSX */
 }
 
+void
+mac_set_scroll_bar_width (f, arg, oldval)
+     struct frame *f;
+     Lisp_Object arg, oldval;
+{
+#ifdef MAC_OSX
+  if (INTEGERP (arg) && XINT (arg) > 0)
+    {
+      if (XINT (arg) < (MAC_AQUA_SMALL_VERTICAL_SCROLL_BAR_WIDTH
+                       + MAC_AQUA_VERTICAL_SCROLL_BAR_WIDTH) / 2)
+       XSETINT (arg, MAC_AQUA_SMALL_VERTICAL_SCROLL_BAR_WIDTH);
+      else
+       XSETINT (arg, MAC_AQUA_VERTICAL_SCROLL_BAR_WIDTH);
+    }
+#endif
+  x_set_scroll_bar_width (f, arg, oldval);
+}
+
+#if TARGET_API_MAC_CARBON
+static void
+mac_update_proxy_icon (f)
+     struct frame *f;
+{
+  Lisp_Object file_name =
+    XBUFFER (XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer)->filename;
+  Window w = FRAME_MAC_WINDOW (f);
+
+  if (FRAME_FILE_NAME (f) == NULL && !STRINGP (file_name))
+    return;
+  if (FRAME_FILE_NAME (f) && STRINGP (file_name)
+      && strcmp (FRAME_FILE_NAME (f), SDATA (file_name)) == 0)
+    return;
+
+  if (FRAME_FILE_NAME (f))
+    {
+      xfree (FRAME_FILE_NAME (f));
+      FRAME_FILE_NAME (f) = NULL;
+    }
+
+  BLOCK_INPUT;
+
+  if (STRINGP (file_name))
+    {
+      OSStatus err;
+      AEDesc desc;
+      Lisp_Object encoded_file_name = ENCODE_FILE (file_name);
+
+#ifdef MAC_OS8
+      SetPortWindowPort (w);
+#endif
+      err = AECoercePtr (TYPE_FILE_NAME, SDATA (encoded_file_name),
+                        SBYTES (encoded_file_name), typeAlias, &desc);
+      if (err == noErr)
+       {
+         Size size = AEGetDescDataSize (&desc);
+         AliasHandle alias = (AliasHandle) NewHandle (size);
+
+         if (alias == NULL)
+           err = memFullErr;
+         else
+           {
+             HLock ((Handle) alias);
+             err = AEGetDescData (&desc, *alias, size);
+             HUnlock ((Handle) alias);
+             if (err == noErr)
+               err = SetWindowProxyAlias (w, alias);
+             DisposeHandle ((Handle) alias);
+           }
+         AEDisposeDesc (&desc);
+       }
+      if (err == noErr)
+       {
+         FRAME_FILE_NAME (f) = xmalloc (SBYTES (file_name) + 1);
+         strcpy (FRAME_FILE_NAME (f), SDATA (file_name));
+       }
+    }
+
+  if (FRAME_FILE_NAME (f) == NULL)
+    RemoveWindowProxy (w);
+
+  UNBLOCK_INPUT;
+}
+#endif
+
+void
+mac_update_title_bar (f, save_match_data)
+     struct frame *f;
+     int save_match_data;
+{
+#if TARGET_API_MAC_CARBON
+  struct window *w;
+  int modified_p;
+
+  if (!FRAME_MAC_P (f))
+    return;
+
+  w = XWINDOW (FRAME_SELECTED_WINDOW (f));
+  modified_p = (BUF_SAVE_MODIFF (XBUFFER (w->buffer))
+               < BUF_MODIFF (XBUFFER (w->buffer)));
+  if (windows_or_buffers_changed
+      /* Minibuffer modification status shown in the close button is
+        confusing.  */
+      || (!MINI_WINDOW_P (w)
+         && (modified_p != !NILP (w->last_had_star))))
+    SetWindowModified (FRAME_MAC_WINDOW (f),
+                      !MINI_WINDOW_P (w) && modified_p);
+
+  if (windows_or_buffers_changed)
+    mac_update_proxy_icon (f);
+#endif
+}
+
 \f
 /* Subroutines of creating a frame.  */
 
+/* Retrieve the string resource specified by NAME with CLASS from
+   database RDB.
+
+   The return value points to the contents of a Lisp string.  So it
+   will not be valid after the next GC where string compaction will
+   occur.  */
+
 char *
 x_get_string_resource (rdb, name, class)
      XrmDatabase rdb;
      char *name, *class;
 {
-  /* MAC_TODO: implement resource strings */
-  return (char *)0;
+  Lisp_Object value = xrm_get_resource (rdb, name, class);
+
+  if (STRINGP (value))
+    return SDATA (value);
+  else
+    return NULL;
 }
 
 /* Return the value of parameter PARAM.
@@ -2238,36 +2197,56 @@ XParseGeometry (string, x, y, width, height)
 }
 
 \f
-#if 0 /* MAC_TODO */
 /* Create and set up the Mac window for frame F.  */
 
 static void
-mac_window (f, window_prompting, minibuffer_only)
+mac_window (f)
      struct frame *f;
-     long window_prompting;
-     int minibuffer_only;
 {
   Rect r;
 
   BLOCK_INPUT;
 
-  /* Use the resource name as the top-level window name
-     for looking up resources.  Make a non-Lisp copy
-     for the window manager, so GC relocation won't bother it.
-
-     Elsewhere we specify the window name for the window manager.  */
-
-  {
-    char *str = (char *) SDATA (Vx_resource_name);
-    f->namebuf = (char *) xmalloc (strlen (str) + 1);
-    strcpy (f->namebuf, str);
-  }
-
   SetRect (&r, f->left_pos, f->top_pos,
            f->left_pos + FRAME_PIXEL_WIDTH (f),
            f->top_pos + FRAME_PIXEL_HEIGHT (f));
+#if TARGET_API_MAC_CARBON
+  CreateNewWindow (kDocumentWindowClass,
+                  kWindowStandardDocumentAttributes
+                  /* | kWindowToolbarButtonAttribute */,
+                  &r, &FRAME_MAC_WINDOW (f));
+  if (FRAME_MAC_WINDOW (f))
+    {
+      SetWRefCon (FRAME_MAC_WINDOW (f), (long) f->output_data.mac);
+      if (install_window_handler (FRAME_MAC_WINDOW (f)) != noErr)
+       {
+         DisposeWindow (FRAME_MAC_WINDOW (f));
+         FRAME_MAC_WINDOW (f) = NULL;
+       }
+    }
+#else
   FRAME_MAC_WINDOW (f)
-    = NewCWindow (NULL, &r, "\p", 1, zoomDocProc, (WindowPtr) -1, 1, (long) f->output_data.mac);
+    = NewCWindow (NULL, &r, "\p", false, zoomDocProc,
+                 (WindowPtr) -1, 1, (long) f->output_data.mac);
+#endif
+  /* so that update events can find this mac_output struct */
+  f->output_data.mac->mFP = f;  /* point back to emacs frame */
+
+#ifndef MAC_OSX
+  if (FRAME_MAC_WINDOW (f))
+    {
+      ControlRef root_control;
+
+      if (CreateRootControl (FRAME_MAC_WINDOW (f), &root_control) != noErr)
+       {
+         DisposeWindow (FRAME_MAC_WINDOW (f));
+         FRAME_MAC_WINDOW (f) = NULL;
+       }
+    }
+#endif
+  if (FRAME_MAC_WINDOW (f))
+    XSetWindowBackground (FRAME_MAC_DISPLAY(f), FRAME_MAC_WINDOW (f),
+                         FRAME_BACKGROUND_PIXEL (f));
 
   validate_x_resource_name ();
 
@@ -2285,17 +2264,11 @@ mac_window (f, window_prompting, minibuffer_only)
     x_set_name (f, name, explicit);
   }
 
-  ShowWindow (FRAME_MAC_WINDOW (f));
-
   UNBLOCK_INPUT;
 
-  if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
-    initialize_frame_menubar (f);
-
   if (FRAME_MAC_WINDOW (f) == 0)
     error ("Unable to create window");
 }
-#endif /* MAC_TODO */
 
 /* Handle the icon stuff for this window.  Perhaps later we might
    want an x_set_icon_position which can be called interactively as
@@ -2469,9 +2442,11 @@ unwind_create_frame (frame)
 
       x_free_frame_resources (f);
 
+#if GLYPH_DEBUG
       /* Check that reference counts are indeed correct.  */
       xassert (dpyinfo->reference_count == dpyinfo_refcount);
       xassert (dpyinfo->image_cache->refcount == image_cache_refcount);
+#endif
       return Qt;
     }
 
@@ -2481,7 +2456,7 @@ unwind_create_frame (frame)
 
 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
        1, 1, 0,
-       doc: /* Make a new window, which is called a \"frame\" in Emacs terms.
+       doc: /* Make a new window, which is called a "frame" in Emacs terms.
 Returns an Emacs frame object.
 ALIST is an alist of frame parameters.
 If the parameters specify that the frame should not have a minibuffer,
@@ -2490,7 +2465,7 @@ then `default-minibuffer-frame' must be a frame whose minibuffer can
 be shared by the new frame.
 
 This function is an internal primitive--use `make-frame' instead.  */)
-  (parms)
+     (parms)
      Lisp_Object parms;
 {
   struct frame *f;
@@ -2505,11 +2480,11 @@ This function is an internal primitive--use `make-frame' instead.  */)
   struct mac_display_info *dpyinfo = NULL;
   Lisp_Object parent;
   struct kboard *kb;
-  char x_frame_name[10];
-  static int x_frame_count = 2;  /* begins at 2 because terminal frame is F1 */
 
   check_mac ();
 
+  parms = Fcopy_alist (parms);
+
   /* Use this general default value to start with
      until we know if this frame has a specified name.  */
   Vx_resource_name = Vinvocation_name;
@@ -2546,7 +2521,7 @@ This function is an internal primitive--use `make-frame' instead.  */)
   frame = Qnil;
   GCPRO4 (parms, parent, name, frame);
   tem = mac_get_arg (parms, Qminibuffer, "minibuffer", "Minibuffer",
-                     RES_TYPE_SYMBOL);
+                    RES_TYPE_SYMBOL);
   if (EQ (tem, Qnone) || NILP (tem))
     f = make_frame_without_minibuffer (Qnil, kb, display);
   else if (EQ (tem, Qonly))
@@ -2559,18 +2534,6 @@ This function is an internal primitive--use `make-frame' instead.  */)
   else
     f = make_frame (1);
 
-  if (EQ (name, Qunbound) || NILP (name))
-    {
-      sprintf (x_frame_name, "F%d", x_frame_count++);
-      f->name = build_string (x_frame_name);
-      f->explicit_name = 0;
-    }
-  else
-    {
-      f->name = name;
-      f->explicit_name = 1;
-    }
-
   XSETFRAME (frame, f);
 
   /* Note that X Windows does support scroll bars.  */
@@ -2580,8 +2543,6 @@ This function is an internal primitive--use `make-frame' instead.  */)
   f->output_data.mac = (struct mac_output *) xmalloc (sizeof (struct mac_output));
   bzero (f->output_data.mac, sizeof (struct mac_output));
   FRAME_FONTSET (f) = -1;
-  f->output_data.mac->scroll_bar_foreground_pixel = -1;
-  f->output_data.mac->scroll_bar_background_pixel = -1;
   record_unwind_protect (unwind_create_frame, frame);
 
   f->icon_name
@@ -2589,7 +2550,11 @@ This function is an internal primitive--use `make-frame' instead.  */)
   if (! STRINGP (f->icon_name))
     f->icon_name = Qnil;
 
-/*  FRAME_W32_DISPLAY_INFO (f) = dpyinfo; */
+/*   FRAME_MAC_DISPLAY_INFO (f) = dpyinfo; */
+#if GLYPH_DEBUG
+  image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
+  dpyinfo_refcount = dpyinfo->reference_count;
+#endif /* GLYPH_DEBUG */
 #ifdef MULTI_KBOARD
   FRAME_KBOARD (f) = kb;
 #endif
@@ -2598,7 +2563,7 @@ This function is an internal primitive--use `make-frame' instead.  */)
 
   if (!NILP (parent))
     {
-      f->output_data.mac->parent_desc = (Window) parent;
+      f->output_data.mac->parent_desc = (Window) XFASTINT (parent);
       f->output_data.mac->explicit_parent = 1;
     }
   else
@@ -2641,9 +2606,15 @@ This function is an internal primitive--use `make-frame' instead.  */)
       }
 
     /* Try out a font which we hope has bold and italic variations.  */
+#if USE_ATSUI
+    if (! STRINGP (font))
+      font = x_new_font (f, "-*-monaco-medium-r-normal--12-*-*-*-*-*-iso10646-1");
+#endif
     if (! STRINGP (font))
       font = x_new_font (f, "-ETL-fixed-medium-r-*--*-160-*-*-*-*-iso8859-1");
     /* If those didn't work, look for something which will at least work.  */
+    if (! STRINGP (font))
+      font = x_new_fontset (f, "fontset-standard");
     if (! STRINGP (font))
       font = x_new_font (f, "-*-monaco-*-12-*-mac-roman");
     if (! STRINGP (font))
@@ -2708,34 +2679,27 @@ This function is an internal primitive--use `make-frame' instead.  */)
 
   x_default_parameter (f, parms, Qmenu_bar_lines, make_number (1),
                       "menuBar", "MenuBar", RES_TYPE_NUMBER);
-  x_default_parameter (f, parms, Qtool_bar_lines, make_number (0),
-                       "toolBar", "ToolBar", RES_TYPE_NUMBER);
+  x_default_parameter (f, parms, Qtool_bar_lines, make_number (1),
+                      "toolBar", "ToolBar", RES_TYPE_NUMBER);
   x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
-                      "bufferPredicate", "BufferPredicate", RES_TYPE_SYMBOL);
+                      "bufferPredicate", "BufferPredicate",
+                      RES_TYPE_SYMBOL);
   x_default_parameter (f, parms, Qtitle, Qnil,
                       "title", "Title", RES_TYPE_STRING);
+  x_default_parameter (f, parms, Qfullscreen, Qnil,
+                       "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
 
   f->output_data.mac->parent_desc = FRAME_MAC_DISPLAY_INFO (f)->root_window;
 
-  /* MAC_TODO: specify 1 below when toolbars are implemented.  */
-  window_prompting = x_figure_window_size (f, parms, 0);
+  /* Compute the size of the window.  */
+  window_prompting = x_figure_window_size (f, parms, 1);
 
   tem = mac_get_arg (parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
   f->no_split = minibuffer_only || EQ (tem, Qt);
 
-  /* Create the window. Add the tool-bar height to the initial frame
-     height so that the user gets a text display area of the size he
-     specified with -g or via the registry. Later changes of the
-     tool-bar height don't change the frame size. This is done so that
-     users can create tall Emacs frames without having to guess how
-     tall the tool-bar will get. */
-  FRAME_LINES (f) += FRAME_TOOL_BAR_LINES (f);
-
-  /* mac_window (f, window_prompting, minibuffer_only); */
-  make_mac_frame (f);
+  mac_window (f);
 
   x_icon (f, parms);
-
   x_make_gc (f);
 
   /* Now consider the frame official.  */
@@ -2754,7 +2718,8 @@ This function is an internal primitive--use `make-frame' instead.  */)
   x_default_parameter (f, parms, Qcursor_type, Qbox,
                       "cursorType", "CursorType", RES_TYPE_SYMBOL);
   x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
-                      "scrollBarWidth", "ScrollBarWidth", RES_TYPE_NUMBER);
+                      "scrollBarWidth", "ScrollBarWidth",
+                      RES_TYPE_NUMBER);
 
   /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
      Change will not be effected unless different from the current
@@ -2762,21 +2727,16 @@ This function is an internal primitive--use `make-frame' instead.  */)
   width = FRAME_COLS (f);
   height = FRAME_LINES (f);
 
-  FRAME_LINES (f) = 0;
   SET_FRAME_COLS (f, 0);
+  FRAME_LINES (f) = 0;
   change_frame_size (f, height, width, 1, 0, 0);
 
-  /* Set up faces after all frame parameters are known.  */
-  call1 (Qface_set_after_frame_default, frame);
-
-#if 0 /* MAC_TODO: when we have window manager hints */
   /* Tell the server what size and position, etc, we want, and how
      badly we want them.  This should be done after we have the menu
      bar so that its size can be taken into account.  */
   BLOCK_INPUT;
   x_wm_set_size_hint (f, window_prompting, 0);
   UNBLOCK_INPUT;
-#endif
 
   /* Make the window appear on the frame and enable display, unless
      the caller says not to.  However, with explicit parent, Emacs
@@ -2789,17 +2749,28 @@ This function is an internal primitive--use `make-frame' instead.  */)
       if (EQ (visibility, Qunbound))
        visibility = Qt;
 
-#if 0 /* MAC_TODO: really no iconify on Mac */
       if (EQ (visibility, Qicon))
        x_iconify_frame (f);
-      else
-#endif
-      if (! NILP (visibility))
+      else if (! NILP (visibility))
        x_make_frame_visible (f);
       else
        /* Must have been Qnil.  */
        ;
     }
+
+  /* Initialize `default-minibuffer-frame' in case this is the first
+     frame on this display device.  */
+  if (FRAME_HAS_MINIBUF_P (f)
+      && (!FRAMEP (kb->Vdefault_minibuffer_frame)
+          || !FRAME_LIVE_P (XFRAME (kb->Vdefault_minibuffer_frame))))
+    kb->Vdefault_minibuffer_frame = frame;
+
+  /* All remaining specified parameters, which have not been "used"
+     by x_get_arg and friends, now go in the misc. alist of the frame.  */
+  for (tem = parms; !NILP (tem); tem = XCDR (tem))
+    if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
+      f->param_alist = Fcons (XCAR (tem), f->param_alist);
+
   UNGCPRO;
 
   /* Make sure windows on this frame appear in calls to next-window
@@ -2809,9 +2780,11 @@ This function is an internal primitive--use `make-frame' instead.  */)
   return unbind_to (count, frame);
 }
 
+
 /* FRAME is used only to get a handle on the X display.  We don't pass the
    display info directly because we're called from frame.c, which doesn't
    know about that structure.  */
+
 Lisp_Object
 x_get_focus_frame (frame)
      struct frame *frame;
@@ -2824,10 +2797,43 @@ x_get_focus_frame (frame)
   XSETFRAME (xfocus, dpyinfo->x_focus_frame);
   return xfocus;
 }
+
+
+DEFUN ("x-focus-frame", Fx_focus_frame, Sx_focus_frame, 1, 1, 0,
+       doc: /* Set the input focus to FRAME.
+FRAME nil means use the selected frame.  */)
+     (frame)
+     Lisp_Object frame;
+{
+  struct frame *f = check_x_frame (frame);
+  struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
+
+  if (dpyinfo->x_focus_frame != f)
+    {
+      BLOCK_INPUT;
+#ifdef MAC_OSX
+      ActivateWindow (ActiveNonFloatingWindow (), false);
+      ActivateWindow (FRAME_MAC_WINDOW (f), true);
+#else
+#if !TARGET_API_MAC_CARBON
+      /* SelectWindow (Non-Carbon) does not issue deactivate events if
+        the possibly inactive window that is to be selected is
+        already the frontmost one.  */
+      SendBehind (FRAME_MAC_WINDOW (f), NULL);
+#endif
+      /* This brings the window to the front.  */
+      SelectWindow (FRAME_MAC_WINDOW (f));
+#endif
+      UNBLOCK_INPUT;
+    }
+
+  return Qnil;
+}
+
 \f
 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
        doc: /* Internal function called by `color-defined-p', which see.  */)
-  (color, frame)
+     (color, frame)
      Lisp_Object color, frame;
 {
   XColor foo;
@@ -2843,7 +2849,7 @@ DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
 
 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
        doc: /* Internal function called by `color-values', which see.  */)
-  (color, frame)
+     (color, frame)
      Lisp_Object color, frame;
 {
   XColor foo;
@@ -2866,7 +2872,7 @@ DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
 
 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
        doc: /* Internal function called by `display-color-p', which see.  */)
-  (display)
+     (display)
      Lisp_Object display;
 {
   struct mac_display_info *dpyinfo = check_x_display_info (display);
@@ -2879,12 +2885,12 @@ DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
 
 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
        0, 1, 0,
-       doc: /* Return t if the X display supports shades of gray.
+       doc: /* Return t if DISPLAY supports shades of gray.
 Note that color displays do support shades of gray.
 The optional argument DISPLAY specifies which display to ask about.
 DISPLAY should be either a frame or a display name (a string).
 If omitted or nil, that stands for the selected frame's display.  */)
-  (display)
+     (display)
      Lisp_Object display;
 {
   struct mac_display_info *dpyinfo = check_x_display_info (display);
@@ -2897,11 +2903,11 @@ If omitted or nil, that stands for the selected frame's display.  */)
 
 DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
        0, 1, 0,
-       doc: /* Returns the width in pixels of the X display DISPLAY.
+       doc: /* Returns the width in pixels of DISPLAY.
 The optional argument DISPLAY specifies which display to ask about.
 DISPLAY should be either a frame or a display name (a string).
 If omitted or nil, that stands for the selected frame's display.  */)
-  (display)
+     (display)
      Lisp_Object display;
 {
   struct mac_display_info *dpyinfo = check_x_display_info (display);
@@ -2911,11 +2917,11 @@ If omitted or nil, that stands for the selected frame's display.  */)
 
 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
        Sx_display_pixel_height, 0, 1, 0,
-       doc: /* Returns the height in pixels of the X display DISPLAY.
+       doc: /* Returns the height in pixels of DISPLAY.
 The optional argument DISPLAY specifies which display to ask about.
 DISPLAY should be either a frame or a display name (a string).
 If omitted or nil, that stands for the selected frame's display.  */)
-  (display)
+     (display)
      Lisp_Object display;
 {
   struct mac_display_info *dpyinfo = check_x_display_info (display);
@@ -2925,11 +2931,11 @@ If omitted or nil, that stands for the selected frame's display.  */)
 
 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
        0, 1, 0,
-       doc: /* Returns the number of bitplanes of the display DISPLAY.
+       doc: /* Returns the number of bitplanes of DISPLAY.
 The optional argument DISPLAY specifies which display to ask about.
 DISPLAY should be either a frame or a display name (a string).
 If omitted or nil, that stands for the selected frame's display.  */)
-  (display)
+     (display)
      Lisp_Object display;
 {
   struct mac_display_info *dpyinfo = check_x_display_info (display);
@@ -2939,27 +2945,27 @@ If omitted or nil, that stands for the selected frame's display.  */)
 
 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
        0, 1, 0,
-       doc: /* Returns the number of color cells of the display DISPLAY.
+       doc: /* Returns the number of color cells of DISPLAY.
 The optional argument DISPLAY specifies which display to ask about.
 DISPLAY should be either a frame or a display name (a string).
 If omitted or nil, that stands for the selected frame's display.  */)
-  (display)
+     (display)
      Lisp_Object display;
 {
   struct mac_display_info *dpyinfo = check_x_display_info (display);
 
-  /* MAC_TODO: check whether this is right */
-  return make_number (dpyinfo->n_planes >= 8 ? 256 : 1 << dpyinfo->n_planes - 1);
+  /* We force 24+ bit depths to 24-bit to prevent an overflow.  */
+  return make_number (1 << min (dpyinfo->n_planes, 24));
 }
 
 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
        Sx_server_max_request_size,
        0, 1, 0,
-       doc: /* Returns the maximum request size of the server of display DISPLAY.
+       doc: /* Returns the maximum request size of the server of DISPLAY.
 The optional argument DISPLAY specifies which display to ask about.
 DISPLAY should be either a frame or a display name (a string).
 If omitted or nil, that stands for the selected frame's display.  */)
-  (display)
+     (display)
      Lisp_Object display;
 {
   struct mac_display_info *dpyinfo = check_x_display_info (display);
@@ -2968,18 +2974,18 @@ If omitted or nil, that stands for the selected frame's display.  */)
 }
 
 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
-       doc: /* Returns the vendor ID string of the Mac OS system (Apple).
+       doc: /* Returns the "vendor ID" string of the Mac OS system (Apple).
 The optional argument DISPLAY specifies which display to ask about.
 DISPLAY should be either a frame or a display name (a string).
 If omitted or nil, that stands for the selected frame's display.  */)
-  (display)
+     (display)
      Lisp_Object display;
 {
   return build_string ("Apple Computers");
 }
 
 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
-       doc: /* Returns the version numbers of the server of display DISPLAY.
+       doc: /* Returns the version numbers of the Mac OS system.
 The value is a list of three integers: the major and minor
 version numbers, and the vendor-specific release
 number.  See also the function `x-server-vendor'.
@@ -2987,77 +2993,94 @@ number.  See also the function `x-server-vendor'.
 The optional argument DISPLAY specifies which display to ask about.
 DISPLAY should be either a frame or a display name (a string).
 If omitted or nil, that stands for the selected frame's display.  */)
-  (display)
+     (display)
      Lisp_Object display;
 {
-  int mac_major_version, mac_minor_version;
-  SInt32 response;
+  UInt32 response, major, minor, bugfix;
+  OSErr err;
 
-  if (Gestalt (gestaltSystemVersion, &response) != noErr)
-    error ("Cannot get Mac OS version");
+  BLOCK_INPUT;
+  err = Gestalt (gestaltSystemVersion, &response);
+  if (err == noErr)
+    {
+      if (response >= 0x00001040)
+       {
+         err = Gestalt ('sys1', &major); /* gestaltSystemVersionMajor */
+         if (err == noErr)
+           err = Gestalt ('sys2', &minor); /* gestaltSystemVersionMinor */
+         if (err == noErr)
+           err = Gestalt ('sys3', &bugfix); /* gestaltSystemVersionBugFix */
+       }
+      else
+       {
+         bugfix = response & 0xf;
+         response >>= 4;
+         minor = response & 0xf;
+         response >>= 4;
+         /* convert BCD to int */
+         major = response - (response >> 4) * 6;
+       }
+    }
+  UNBLOCK_INPUT;
 
-  mac_major_version = (response >> 8) & 0xf;
-  mac_minor_version = (response >> 4) & 0xf;
+  if (err != noErr)
+    error ("Cannot get Mac OS version");
 
-  return Fcons (make_number (mac_major_version),
-               Fcons (make_number (mac_minor_version), Qnil));
+  return Fcons (make_number (major),
+               Fcons (make_number (minor),
+                      Fcons (make_number (bugfix),
+                             Qnil)));
 }
 
 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
-       doc: /* Return the number of screens on the server of display DISPLAY.
+       doc: /* Return the number of screens on the server of DISPLAY.
 The optional argument DISPLAY specifies which display to ask about.
 DISPLAY should be either a frame or a display name (a string).
 If omitted or nil, that stands for the selected frame's display.  */)
-  (display)
+     (display)
      Lisp_Object display;
 {
   return make_number (1);
 }
 
 DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
-       doc: /* Return the height in millimeters of the X display DISPLAY.
+       doc: /* Return the height in millimeters of DISPLAY.
 The optional argument DISPLAY specifies which display to ask about.
 DISPLAY should be either a frame or a display name (a string).
 If omitted or nil, that stands for the selected frame's display.  */)
-  (display)
+     (display)
      Lisp_Object display;
 {
   /* MAC_TODO: this is an approximation, and only of the main display */
 
   struct mac_display_info *dpyinfo = check_x_display_info (display);
-  short h, v;
-
-  ScreenRes (&h, &v);
 
-  return make_number ((int) (v / 72.0 * 25.4));
+  return make_number ((int) (dpyinfo->height * 25.4 / dpyinfo->resy));
 }
 
 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
-       doc: /* Return the width in millimeters of the X display DISPLAY.
+       doc: /* Return the width in millimeters of DISPLAY.
 The optional argument DISPLAY specifies which display to ask about.
 DISPLAY should be either a frame or a display name (a string).
 If omitted or nil, that stands for the selected frame's display.  */)
-  (display)
+     (display)
      Lisp_Object display;
 {
   /* MAC_TODO: this is an approximation, and only of the main display */
 
   struct mac_display_info *dpyinfo = check_x_display_info (display);
-  short h, v;
 
-  ScreenRes (&h, &v);
-
-  return make_number ((int) (h / 72.0 * 25.4));
+  return make_number ((int) (dpyinfo->width * 25.4 / dpyinfo->resx));
 }
 
 DEFUN ("x-display-backing-store", Fx_display_backing_store,
        Sx_display_backing_store, 0, 1, 0,
-       doc: /* Returns an indication of whether display DISPLAY does backing store.
+       doc: /* Returns an indication of whether DISPLAY does backing store.
 The value may be `always', `when-mapped', or `not-useful'.
 The optional argument DISPLAY specifies which display to ask about.
 DISPLAY should be either a frame or a display name (a string).
 If omitted or nil, that stands for the selected frame's display.  */)
-  (display)
+     (display)
      Lisp_Object display;
 {
   return intern ("not-useful");
@@ -3065,14 +3088,14 @@ If omitted or nil, that stands for the selected frame's display.  */)
 
 DEFUN ("x-display-visual-class", Fx_display_visual_class,
        Sx_display_visual_class, 0, 1, 0,
-       doc: /* Returns the visual class of the display DISPLAY.
+       doc: /* Returns the visual class of DISPLAY.
 The value is one of the symbols `static-gray', `gray-scale',
 `static-color', `pseudo-color', `true-color', or `direct-color'.
 
 The optional argument DISPLAY specifies which display to ask about.
 DISPLAY should be either a frame or a display name (a string).
 If omitted or nil, that stands for the selected frame's display.  */)
-       (display)
+     (display)
      Lisp_Object display;
 {
   struct mac_display_info *dpyinfo = check_x_display_info (display);
@@ -3096,11 +3119,11 @@ If omitted or nil, that stands for the selected frame's display.  */)
 
 DEFUN ("x-display-save-under", Fx_display_save_under,
        Sx_display_save_under, 0, 1, 0,
-       doc: /* Returns t if the display DISPLAY supports the save-under feature.
+       doc: /* Returns t if DISPLAY supports the save-under feature.
 The optional argument DISPLAY specifies which display to ask about.
 DISPLAY should be either a frame or a display name (a string).
 If omitted or nil, that stands for the selected frame's display.  */)
-  (display)
+     (display)
      Lisp_Object display;
 {
   return Qnil;
@@ -3153,6 +3176,9 @@ x_display_info_for_name (name)
 
   CHECK_STRING (name);
 
+  if (! EQ (Vwindow_system, intern ("mac")))
+    error ("Not using Mac native windows");
+
   for (dpyinfo = &one_mac_display_info, names = x_display_name_list;
        dpyinfo;
        dpyinfo = dpyinfo->next, names = XCDR (names))
@@ -3180,7 +3206,6 @@ x_display_info_for_name (name)
   return dpyinfo;
 }
 
-#if 0 /* MAC_TODO: implement network support */
 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
        1, 3, 0,
        doc: /* Open a connection to a server.
@@ -3188,7 +3213,7 @@ DISPLAY is the name of the display to connect to.
 Optional second arg XRM-STRING is a string of resources in xrdb format.
 If the optional third arg MUST-SUCCEED is non-nil,
 terminate Emacs if we can't open the connection.  */)
-  (display, xrm_string, must_succeed)
+     (display, xrm_string, must_succeed)
      Lisp_Object display, xrm_string, must_succeed;
 {
   unsigned char *xrm_option;
@@ -3199,7 +3224,7 @@ terminate Emacs if we can't open the connection.  */)
     CHECK_STRING (xrm_string);
 
   if (! EQ (Vwindow_system, intern ("mac")))
-    error ("Not using Mac OS");
+    error ("Not using Mac native windows");
 
   if (! NILP (xrm_string))
     xrm_option = (unsigned char *) SDATA (xrm_string);
@@ -3233,8 +3258,8 @@ DEFUN ("x-close-connection", Fx_close_connection,
        doc: /* Close the connection to DISPLAY's server.
 For DISPLAY, specify either a frame or a display name (a string).
 If DISPLAY is nil, that stands for the selected frame's display.  */)
-  (display)
-  Lisp_Object display;
+     (display)
+     Lisp_Object display;
 {
   struct mac_display_info *dpyinfo = check_x_display_info (display);
   int i;
@@ -3247,11 +3272,9 @@ If DISPLAY is nil, that stands for the selected frame's display.  */)
   for (i = 0; i < dpyinfo->n_fonts; i++)
     if (dpyinfo->font_table[i].name)
       {
-        if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
-          xfree (dpyinfo->font_table[i].full_name);
-        xfree (dpyinfo->font_table[i].name);
-        x_unload_font (dpyinfo, dpyinfo->font_table[i].font);
+        mac_unload_font (dpyinfo, dpyinfo->font_table[i].font);
       }
+
   x_destroy_all_bitmaps (dpyinfo);
 
   x_delete_display (dpyinfo);
@@ -3259,11 +3282,10 @@ If DISPLAY is nil, that stands for the selected frame's display.  */)
 
   return Qnil;
 }
-#endif /* 0 */
 
 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
        doc: /* Return the list of display names that Emacs has connections to.  */)
-  ()
+     ()
 {
   Lisp_Object tail, result;
 
@@ -3275,18 +3297,21 @@ DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
 }
 
 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
-       doc: /* If ON is non-nil, report errors as soon as the erring request is made.
-If ON is nil, allow buffering of requests.
-This is a noop on Mac OS systems.
-The optional second argument DISPLAY specifies which display to act on.
-DISPLAY should be either a frame or a display name (a string).
-If DISPLAY is omitted or nil, that stands for the selected frame's display.  */)
-  (on, display)
+       doc: /* This is a noop on Mac OS systems.  */)
+    (on, display)
     Lisp_Object display, on;
 {
   return Qnil;
 }
 
+/* x_sync is a no-op on Mac.  */
+
+void
+x_sync (f)
+     FRAME_PTR f;
+{
+}
+
 \f
 /***********************************************************************
                            Window properties
@@ -3340,7 +3365,7 @@ DEFUN ("x-delete-window-property", Fx_delete_window_property,
        Sx_delete_window_property, 1, 2, 0,
        doc: /* Remove window property PROP from X window of FRAME.
 FRAME nil or omitted means use the selected frame.  Value is PROP.  */)
-  (prop, frame)
+     (prop, frame)
      Lisp_Object prop, frame;
 {
 #if 0 /* MAC_TODO : port window properties to Mac */
@@ -3368,7 +3393,7 @@ DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
 If FRAME is nil or omitted, use the selected frame.  Value is nil
 if FRAME hasn't a property with name PROP or if PROP has no string
 value.  */)
-  (prop, frame)
+     (prop, frame)
      Lisp_Object prop, frame;
 {
 #if 0 /* MAC_TODO : port window properties to Mac */
@@ -3419,7 +3444,7 @@ value.  */)
 
 \f
 /***********************************************************************
-                               Hourglass cursor
+                               Busy cursor
  ***********************************************************************/
 
 /* If non-null, an asynchronous timer that, when it expires, displays
@@ -3445,16 +3470,28 @@ static Lisp_Object Vhourglass_delay;
 static void show_hourglass P_ ((struct atimer *));
 static void hide_hourglass P_ ((void));
 
+/* Return non-zero if houglass timer has been started or hourglass is shown.  */
+
+int
+hourglass_started ()
+{
+  return hourglass_shown_p || hourglass_atimer != NULL;
+}
+
 
 /* Cancel a currently active hourglass timer, and start a new one.  */
 
 void
 start_hourglass ()
 {
-#if 0 /* MAC_TODO: cursor shape changes.  */
+#ifdef MAC_OSX
   EMACS_TIME delay;
   int secs, usecs = 0;
 
+  /* Don't bother for ttys.  */
+  if (NILP (Vwindow_system))
+    return;
+
   cancel_hourglass ();
 
   if (INTEGERP (Vhourglass_delay)
@@ -3474,16 +3511,17 @@ start_hourglass ()
   EMACS_SET_SECS_USECS (delay, secs, usecs);
   hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
                                     show_hourglass, NULL);
-#endif /* MAC_TODO */
+#endif /* MAC_OSX */
 }
 
 
-/* Cancel the hourglass cursor timer if active, hide an hourglass
-   cursor if shown.  */
+/* Cancel the hourglass cursor timer if active, hide a busy cursor if
+   shown.  */
 
 void
 cancel_hourglass ()
 {
+#ifdef MAC_OSX
   if (hourglass_atimer)
     {
       cancel_atimer (hourglass_atimer);
@@ -3492,22 +3530,22 @@ cancel_hourglass ()
 
   if (hourglass_shown_p)
     hide_hourglass ();
+#endif /* MAC_OSX */
 }
 
 
 /* Timer function of hourglass_atimer.  TIMER is equal to
    hourglass_atimer.
 
-   Display an hourglass cursor on all frames by mapping the frames'
-   hourglass_window.  Set the hourglass_p flag in the frames'
-   output_data.x structure to indicate that an hourglass cursor is
-   shown on the frames.  */
+   On Mac, busy status is shown by the progress indicator (chasing
+   arrows) at the upper-right corner of each frame instead of the
+   hourglass pointer.  */
 
 static void
 show_hourglass (timer)
      struct atimer *timer;
 {
-#if 0  /* MAC_TODO: cursor shape changes.  */
+#if TARGET_API_MAC_CARBON
   /* The timer implementation will cancel this timer automatically
      after this function has run.  Set hourglass_atimer to null
      so that we know the timer doesn't have to be canceled.  */
@@ -3520,46 +3558,47 @@ show_hourglass (timer)
       BLOCK_INPUT;
 
       FOR_EACH_FRAME (rest, frame)
-       if (FRAME_W32_P (XFRAME (frame)))
-         {
-           struct frame *f = XFRAME (frame);
-
-           f->output_data.w32->hourglass_p = 1;
+       {
+         struct frame *f = XFRAME (frame);
 
-           if (!f->output_data.w32->hourglass_window)
-             {
-               unsigned long mask = CWCursor;
-               XSetWindowAttributes attrs;
-
-               attrs.cursor = f->output_data.w32->hourglass_cursor;
-
-               f->output_data.w32->hourglass_window
-                 = XCreateWindow (FRAME_X_DISPLAY (f),
-                                  FRAME_OUTER_WINDOW (f),
-                                  0, 0, 32000, 32000, 0, 0,
-                                  InputOnly,
-                                  CopyFromParent,
-                                  mask, &attrs);
-             }
+         if (FRAME_LIVE_P (f) && FRAME_MAC_P (f)
+             && FRAME_MAC_WINDOW (f) != tip_window)
+           {
+#if USE_CG_DRAWING
+             mac_prepare_for_quickdraw (f);
+#endif
+             if (!f->output_data.mac->hourglass_control)
+               {
+                 Window w = FRAME_MAC_WINDOW (f);
+                 Rect r;
+                 ControlRef c;
+
+                 GetWindowPortBounds (w, &r);
+                 r.left = r.right - HOURGLASS_WIDTH;
+                 r.bottom = r.top + HOURGLASS_HEIGHT;
+                 if (CreateChasingArrowsControl (w, &r, &c) == noErr)
+                   f->output_data.mac->hourglass_control = c;
+               }
 
-           XMapRaised (FRAME_X_DISPLAY (f),
-                       f->output_data.w32->hourglass_window);
-           XFlush (FRAME_X_DISPLAY (f));
-         }
+             if (f->output_data.mac->hourglass_control)
+               ShowControl (f->output_data.mac->hourglass_control);
+           }
+       }
 
       hourglass_shown_p = 1;
       UNBLOCK_INPUT;
     }
-#endif /* MAC_TODO */
+#endif /* TARGET_API_MAC_CARBON */
 }
 
 
-/* Hide the hourglass cursor on all frames, if it is currently shown.  */
+/* Hide the progress indicators on all frames, if it is currently
+   shown.  */
 
 static void
 hide_hourglass ()
 {
-#if 0 /* MAC_TODO: cursor shape changes.  */
+#if TARGET_API_MAC_CARBON
   if (hourglass_shown_p)
     {
       Lisp_Object rest, frame;
@@ -3569,23 +3608,21 @@ hide_hourglass ()
        {
          struct frame *f = XFRAME (frame);
 
-         if (FRAME_W32_P (f)
+         if (FRAME_MAC_P (f)
              /* Watch out for newly created frames.  */
-             && f->output_data.x->hourglass_window)
+             && f->output_data.mac->hourglass_control)
            {
-             XUnmapWindow (FRAME_X_DISPLAY (f),
-                           f->output_data.x->hourglass_window);
-             /* Sync here because XTread_socket looks at the
-                hourglass_p flag that is reset to zero below.  */
-             XSync (FRAME_X_DISPLAY (f), False);
-             f->output_data.x->hourglass_p = 0;
+#if USE_CG_DRAWING
+             mac_prepare_for_quickdraw (f);
+#endif
+             HideControl (f->output_data.mac->hourglass_control);
            }
        }
 
       hourglass_shown_p = 0;
       UNBLOCK_INPUT;
     }
-#endif /* MAC_TODO */
+#endif /* TARGET_API_MAC_CARBON */
 }
 
 
@@ -3595,9 +3632,11 @@ hide_hourglass ()
  ***********************************************************************/
 
 static Lisp_Object x_create_tip_frame P_ ((struct mac_display_info *,
-                                          Lisp_Object));
+                                          Lisp_Object, Lisp_Object));
+static void compute_tip_xy P_ ((struct frame *, Lisp_Object, Lisp_Object,
+                               Lisp_Object, int, int, int *, int *));
 
-/* The frame of a currently visible tooltip, or null.  */
+/* The frame of a currently visible tooltip.  */
 
 Lisp_Object tip_frame;
 
@@ -3612,15 +3651,42 @@ Window tip_window;
 
 Lisp_Object last_show_tip_args;
 
+/* Maximum size for tooltips; a cons (COLUMNS . ROWS).  */
+
+Lisp_Object Vx_max_tooltip_size;
+
+
+static Lisp_Object
+unwind_create_tip_frame (frame)
+     Lisp_Object frame;
+{
+  Lisp_Object deleted;
+
+  deleted = unwind_create_frame (frame);
+  if (EQ (deleted, Qt))
+    {
+      tip_window = NULL;
+      tip_frame = Qnil;
+    }
+
+  return deleted;
+}
+
+
 /* Create a frame for a tooltip on the display described by DPYINFO.
-   PARMS is a list of frame parameters.  Value is the frame.  */
+   PARMS is a list of frame parameters.  TEXT is the string to
+   display in the tip frame.  Value is the frame.
+
+   Note that functions called here, esp. x_default_parameter can
+   signal errors, for instance when a specified color name is
+   undefined.  We have to make sure that we're in a consistent state
+   when this happens.  */
 
 static Lisp_Object
-x_create_tip_frame (dpyinfo, parms)
+x_create_tip_frame (dpyinfo, parms, text)
      struct mac_display_info *dpyinfo;
-     Lisp_Object parms;
+     Lisp_Object parms, text;
 {
-#if 0 /* MAC_TODO : Mac version */
   struct frame *f;
   Lisp_Object frame, tem;
   Lisp_Object name;
@@ -3629,12 +3695,13 @@ x_create_tip_frame (dpyinfo, parms)
   int count = SPECPDL_INDEX ();
   struct gcpro gcpro1, gcpro2, gcpro3;
   struct kboard *kb;
+  int face_change_count_before = face_change_count;
+  Lisp_Object buffer;
+  struct buffer *old_buffer;
 
-  check_x ();
+  check_mac ();
 
-  /* Use this general default value to start with until we know if
-     this frame has a specified name.  */
-  Vx_resource_name = Vinvocation_name;
+  parms = Fcopy_alist (parms);
 
 #ifdef MULTI_KBOARD
   kb = dpyinfo->kboard;
@@ -3643,40 +3710,58 @@ x_create_tip_frame (dpyinfo, parms)
 #endif
 
   /* Get the name of the frame to use for resource lookup.  */
-  name = w32_get_arg (parms, Qname, "name", "Name", RES_TYPE_STRING);
+  name = mac_get_arg (parms, Qname, "name", "Name", RES_TYPE_STRING);
   if (!STRINGP (name)
       && !EQ (name, Qunbound)
       && !NILP (name))
     error ("Invalid frame name--not a string or nil");
-  Vx_resource_name = name;
 
   frame = Qnil;
   GCPRO3 (parms, name, frame);
-  tip_frame = f = make_frame (1);
+  f = make_frame (1);
   XSETFRAME (frame, f);
+
+  buffer = Fget_buffer_create (build_string (" *tip*"));
+  Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer, Qnil);
+  old_buffer = current_buffer;
+  set_buffer_internal_1 (XBUFFER (buffer));
+  current_buffer->truncate_lines = Qnil;
+  specbind (Qinhibit_read_only, Qt);
+  specbind (Qinhibit_modification_hooks, Qt);
+  Ferase_buffer ();
+  Finsert (1, &text);
+  set_buffer_internal_1 (old_buffer);
+
   FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
+  record_unwind_protect (unwind_create_tip_frame, frame);
 
-  f->output_method = output_w32;
-  f->output_data.w32 =
-    (struct w32_output *) xmalloc (sizeof (struct w32_output));
-  bzero (f->output_data.w32, sizeof (struct w32_output));
-#if 0
-  f->output_data.w32->icon_bitmap = -1;
-#endif
-  FRAME_FONTSET (f) = -1;
-  f->icon_name = Qnil;
+  /* By setting the output method, we're essentially saying that
+     the frame is live, as per FRAME_LIVE_P.  If we get a signal
+     from this point on, x_destroy_window might screw up reference
+     counts etc.  */
+  f->output_method = output_mac;
+  f->output_data.mac =
+    (struct mac_output *) xmalloc (sizeof (struct mac_output));
+  bzero (f->output_data.mac, sizeof (struct mac_output));
 
+  FRAME_FONTSET (f)  = -1;
+  f->icon_name = Qnil;
+/*   FRAME_X_DISPLAY_INFO (f) = dpyinfo; */
+#if GLYPH_DEBUG
+  image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
+  dpyinfo_refcount = dpyinfo->reference_count;
+#endif /* GLYPH_DEBUG */
 #ifdef MULTI_KBOARD
   FRAME_KBOARD (f) = kb;
 #endif
-  f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
-  f->output_data.w32->explicit_parent = 0;
+  f->output_data.mac->parent_desc = FRAME_MAC_DISPLAY_INFO (f)->root_window;
+  f->output_data.mac->explicit_parent = 0;
 
   /* Set the name; the functions to which we pass f expect the name to
      be set.  */
   if (EQ (name, Qunbound) || NILP (name))
     {
-      f->name = build_string (dpyinfo->x_id_name);
+      f->name = build_string (dpyinfo->mac_id_name);
       f->explicit_name = 0;
     }
   else
@@ -3687,12 +3772,12 @@ x_create_tip_frame (dpyinfo, parms)
       specbind (Qx_resource_name, name);
     }
 
-  /* Extract the window parameters from the supplied values
-     that are needed to determine window geometry.  */
+  /* Extract the window parameters from the supplied values that are
+     needed to determine window geometry.  */
   {
     Lisp_Object font;
 
-    font = w32_get_arg (parms, Qfont, "font", "Font", RES_TYPE_STRING);
+    font = mac_get_arg (parms, Qfont, "font", "Font", RES_TYPE_STRING);
 
     BLOCK_INPUT;
     /* First, try whatever font the caller has specified.  */
@@ -3706,22 +3791,22 @@ x_create_tip_frame (dpyinfo, parms)
       }
 
     /* Try out a font which we hope has bold and italic variations.  */
-    if (!STRINGP (font))
-      font = x_new_font (f, "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1");
-    if (!STRINGP (font))
-      font = x_new_font (f, "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
+#if USE_ATSUI
     if (! STRINGP (font))
-      font = x_new_font (f, "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
+      font = x_new_font (f, "-*-monaco-medium-r-normal--12-*-*-*-*-*-iso10646-1");
+#endif
     if (! STRINGP (font))
-      /* This was formerly the first thing tried, but it finds too many fonts
-        and takes too long.  */
-      font = x_new_font (f, "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1");
+      font = x_new_font (f, "-ETL-fixed-medium-r-*--*-160-*-*-*-*-iso8859-1");
     /* If those didn't work, look for something which will at least work.  */
     if (! STRINGP (font))
-      font = x_new_font (f, "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1");
+      font = x_new_fontset (f, "fontset-standard");
+    if (! STRINGP (font))
+      font = x_new_font (f, "-*-monaco-*-12-*-mac-roman");
+    if (! STRINGP (font))
+      font = x_new_font (f, "-*-courier-*-10-*-mac-roman");
     UNBLOCK_INPUT;
     if (! STRINGP (font))
-      font = build_string ("fixed");
+      error ("Cannot find any usable font");
 
     x_default_parameter (f, parms, Qfont, font,
                         "font", "Font", RES_TYPE_STRING);
@@ -3737,7 +3822,7 @@ x_create_tip_frame (dpyinfo, parms)
     {
       Lisp_Object value;
 
-      value = w32_get_arg (parms, Qinternal_border_width,
+      value = mac_get_arg (parms, Qinternal_border_width,
                         "internalBorder", "internalBorder", RES_TYPE_NUMBER);
       if (! EQ (value, Qunbound))
        parms = Fcons (Fcons (Qinternal_border_width, value),
@@ -3768,34 +3853,35 @@ x_create_tip_frame (dpyinfo, parms)
      happen.  */
   init_frame_faces (f);
 
-  f->output_data.w32->parent_desc = FRAME_W32_DISPLAY_INFO (f)->root_window;
+  f->output_data.mac->parent_desc = FRAME_MAC_DISPLAY_INFO (f)->root_window;
 
   window_prompting = x_figure_window_size (f, parms, 0);
 
   {
-    XSetWindowAttributes attrs;
-    unsigned long mask;
+    Rect r;
 
     BLOCK_INPUT;
-    mask = CWBackPixel | CWOverrideRedirect | CWSaveUnder | CWEventMask;
-    /* Window managers looks at the override-redirect flag to
-       determine whether or net to give windows a decoration (Xlib
-       3.2.8).  */
-    attrs.override_redirect = True;
-    attrs.save_under = True;
-    attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
-    /* Arrange for getting MapNotify and UnmapNotify events.  */
-    attrs.event_mask = StructureNotifyMask;
-    tip_window
-      = FRAME_W32_WINDOW (f)
-      = XCreateWindow (FRAME_W32_DISPLAY (f),
-                      FRAME_W32_DISPLAY_INFO (f)->root_window,
-                      /* x, y, width, height */
-                      0, 0, 1, 1,
-                      /* Border.  */
-                      1,
-                      CopyFromParent, InputOutput, CopyFromParent,
-                      mask, &attrs);
+    SetRect (&r, 0, 0, 1, 1);
+#if TARGET_API_MAC_CARBON
+    if (CreateNewWindow (kHelpWindowClass,
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
+                        kWindowIgnoreClicksAttribute |
+#endif
+                        kWindowNoUpdatesAttribute |
+                        kWindowNoActivatesAttribute,
+                        &r, &tip_window) == noErr)
+#else
+    if (tip_window = NewCWindow (NULL, &r, "\p", false, plainDBox,
+                                NULL, false, 0L))
+#endif
+      {
+       FRAME_MAC_WINDOW (f) = tip_window;
+       XSetWindowBackground (FRAME_MAC_DISPLAY(f), tip_window,
+                             FRAME_BACKGROUND_PIXEL (f));
+       SetWRefCon (tip_window, (long) f->output_data.mac);
+       /* so that update events can find this mac_output struct */
+       f->output_data.mac->mFP = f;
+      }
     UNBLOCK_INPUT;
   }
 
@@ -3813,8 +3899,8 @@ x_create_tip_frame (dpyinfo, parms)
      FRAME_LINES (f).  */
   width = FRAME_COLS (f);
   height = FRAME_LINES (f);
-  FRAME_LINES (f) = 0;
   SET_FRAME_COLS (f, 0);
+  FRAME_LINES (f) = 0;
   change_frame_size (f, height, width, 1, 0, 0);
 
   /* Add `tooltip' frame parameter's default value. */
@@ -3822,6 +3908,26 @@ x_create_tip_frame (dpyinfo, parms)
     Fmodify_frame_parameters (frame, Fcons (Fcons (intern ("tooltip"), Qt),
                                            Qnil));
 
+  /* Set up faces after all frame parameters are known.  This call
+     also merges in face attributes specified for new frames.
+
+     Frame parameters may be changed if .Xdefaults contains
+     specifications for the default font.  For example, if there is an
+     `Emacs.default.attributeBackground: pink', the `background-color'
+     attribute of the frame get's set, which let's the internal border
+     of the tooltip frame appear in pink.  Prevent this.  */
+  {
+    Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
+
+    /* Set tip_frame here, so that */
+    tip_frame = frame;
+    call1 (Qface_set_after_frame_default, frame);
+
+    if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
+      Fmodify_frame_parameters (frame, Fcons (Fcons (Qbackground_color, bg),
+                                             Qnil));
+  }
+
   f->no_split = 1;
 
   UNGCPRO;
@@ -3833,16 +3939,85 @@ x_create_tip_frame (dpyinfo, parms)
 
   /* Now that the frame is official, it counts as a reference to
      its display.  */
-  FRAME_W32_DISPLAY_INFO (f)->reference_count++;
+  FRAME_MAC_DISPLAY_INFO (f)->reference_count++;
 
+  /* Setting attributes of faces of the tooltip frame from resources
+     and similar will increment face_change_count, which leads to the
+     clearing of all current matrices.  Since this isn't necessary
+     here, avoid it by resetting face_change_count to the value it
+     had before we created the tip frame.  */
+  face_change_count = face_change_count_before;
+
+  /* Discard the unwind_protect.  */
   return unbind_to (count, frame);
-#endif /* MAC_TODO */
-  return Qnil;
+}
+
+
+/* Compute where to display tip frame F.  PARMS is the list of frame
+   parameters for F.  DX and DY are specified offsets from the current
+   location of the mouse.  WIDTH and HEIGHT are the width and height
+   of the tooltip.  Return coordinates relative to the root window of
+   the display in *ROOT_X, and *ROOT_Y.  */
+
+static void
+compute_tip_xy (f, parms, dx, dy, width, height, root_x, root_y)
+     struct frame *f;
+     Lisp_Object parms, dx, dy;
+     int width, height;
+     int *root_x, *root_y;
+{
+  Lisp_Object left, top;
+
+  /* User-specified position?  */
+  left = Fcdr (Fassq (Qleft, parms));
+  top  = Fcdr (Fassq (Qtop, parms));
+
+  /* Move the tooltip window where the mouse pointer is.  Resize and
+     show it.  */
+  if (!INTEGERP (left) || !INTEGERP (top))
+    {
+      Point mouse_pos;
+
+      BLOCK_INPUT;
+      GetMouse (&mouse_pos);
+      LocalToGlobal (&mouse_pos);
+      *root_x = mouse_pos.h;
+      *root_y = mouse_pos.v;
+      UNBLOCK_INPUT;
+    }
+
+  if (INTEGERP (top))
+    *root_y = XINT (top);
+  else if (*root_y + XINT (dy) <= 0)
+    *root_y = 0; /* Can happen for negative dy */
+  else if (*root_y + XINT (dy) + height <= FRAME_MAC_DISPLAY_INFO (f)->height)
+    /* It fits below the pointer */
+    *root_y += XINT (dy);
+  else if (height + XINT (dy) <= *root_y)
+    /* It fits above the pointer.  */
+    *root_y -= height + XINT (dy);
+  else
+    /* Put it on the top.  */
+    *root_y = 0;
+
+  if (INTEGERP (left))
+    *root_x = XINT (left);
+  else if (*root_x + XINT (dx) <= 0)
+    *root_x = 0; /* Can happen for negative dx */
+  else if (*root_x + XINT (dx) + width <= FRAME_MAC_DISPLAY_INFO (f)->width)
+    /* It fits to the right of the pointer.  */
+    *root_x += XINT (dx);
+  else if (width + XINT (dx) <= *root_x)
+    /* It fits to the left of the pointer.  */
+    *root_x -= width + XINT (dx);
+  else
+    /* Put it left-justified on the screen -- it ought to fit that way.  */
+    *root_x = 0;
 }
 
 
 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
-       doc : /* Show STRING in a "tooltip" window on frame FRAME.
+       doc: /* Show STRING in a "tooltip" window on frame FRAME.
 A tooltip window is a small window displaying a string.
 
 FRAME nil or omitted means use the selected frame.
@@ -3853,25 +4028,25 @@ change the tooltip's appearance.
 Automatically hide the tooltip after TIMEOUT seconds.  TIMEOUT nil
 means use the default timeout of 5 seconds.
 
-If the list of frame parameters PARAMS contains a `left' parameters,
+If the list of frame parameters PARMS contains a `left' parameter,
 the tooltip is displayed at that x-position.  Otherwise it is
 displayed at the mouse position, with offset DX added (default is 5 if
 DX isn't specified).  Likewise for the y-position; if a `top' frame
 parameter is specified, it determines the y-position of the tooltip
 window, otherwise it is displayed at the mouse position, with offset
-DY added (default is 10).  */)
-  (string, frame, parms, timeout, dx, dy)
+DY added (default is -10).
+
+A tooltip's maximum size is specified by `x-max-tooltip-size'.
+Text larger than the specified size is clipped.  */)
+     (string, frame, parms, timeout, dx, dy)
      Lisp_Object string, frame, parms, timeout, dx, dy;
 {
   struct frame *f;
   struct window *w;
-  Window root, child;
-  Lisp_Object buffer, top, left;
+  int root_x, root_y;
   struct buffer *old_buffer;
   struct text_pos pos;
   int i, width, height;
-  int root_x, root_y, win_x, win_y;
-  unsigned pmask;
   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
   int old_windows_or_buffers_changed = windows_or_buffers_changed;
   int count = SPECPDL_INDEX ();
@@ -3920,13 +4095,11 @@ DY added (default is 10).  */)
              call1 (Qcancel_timer, timer);
            }
 
-#if 0 /* MAC_TODO : Mac specifics */
          BLOCK_INPUT;
-         compute_tip_xy (f, parms, dx, dy, &root_x, &root_y);
-         XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
-                      root_x, root_y - FRAME_PIXEL_HEIGHT (f));
+         compute_tip_xy (f, parms, dx, dy, FRAME_PIXEL_WIDTH (f),
+                         FRAME_PIXEL_HEIGHT (f), &root_x, &root_y);
+         MoveWindow (FRAME_MAC_WINDOW (f), root_x, root_y, false);
          UNBLOCK_INPUT;
-#endif /* MAC_TODO */
          goto start_timer;
        }
     }
@@ -3953,30 +4126,40 @@ DY added (default is 10).  */)
 
   /* Create a frame for the tooltip, and record it in the global
      variable tip_frame.  */
-  frame = x_create_tip_frame (FRAME_MAC_DISPLAY_INFO (f), parms);
+  frame = x_create_tip_frame (FRAME_MAC_DISPLAY_INFO (f), parms, string);
   f = XFRAME (frame);
 
-  /* Set up the frame's root window.  Currently we use a size of 80
-     columns x 40 lines.  If someone wants to show a larger tip, he
-     will loose.  I don't think this is a realistic case.  */
+  /* Set up the frame's root window.  */
   w = XWINDOW (FRAME_ROOT_WINDOW (f));
   w->left_col = w->top_line = make_number (0);
-  w->total_cols = make_number (80);
-  w->total_lines = make_number (40);
+
+  if (CONSP (Vx_max_tooltip_size)
+      && INTEGERP (XCAR (Vx_max_tooltip_size))
+      && XINT (XCAR (Vx_max_tooltip_size)) > 0
+      && INTEGERP (XCDR (Vx_max_tooltip_size))
+      && XINT (XCDR (Vx_max_tooltip_size)) > 0)
+    {
+      w->total_cols = XCAR (Vx_max_tooltip_size);
+      w->total_lines = XCDR (Vx_max_tooltip_size);
+    }
+  else
+    {
+      w->total_cols = make_number (80);
+      w->total_lines = make_number (40);
+    }
+
+  FRAME_TOTAL_COLS (f) = XINT (w->total_cols);
   adjust_glyphs (f);
   w->pseudo_window_p = 1;
 
   /* Display the tooltip text in a temporary buffer.  */
-  buffer = Fget_buffer_create (build_string (" *tip*"));
-  Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer, Qnil);
   old_buffer = current_buffer;
-  set_buffer_internal_1 (XBUFFER (buffer));
-  Ferase_buffer ();
-  Finsert (1, &string);
+  set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
+  current_buffer->truncate_lines = Qnil;
   clear_glyph_matrix (w->desired_matrix);
   clear_glyph_matrix (w->current_matrix);
   SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
-  try_window (FRAME_ROOT_WINDOW (f), pos);
+  try_window (FRAME_ROOT_WINDOW (f), pos, 0);
 
   /* Compute width and height of the tooltip.  */
   width = height = 0;
@@ -3993,7 +4176,7 @@ DY added (default is 10).  */)
       /* Let the row go over the full width of the frame.  */
       row->full_width_p = 1;
 
-      /* There's a glyph at the end of rows that is use to place
+      /* There's a glyph at the end of rows that is used to place
         the cursor there.  Don't include the width of this glyph.  */
       if (row->used[TEXT_AREA])
        {
@@ -4014,17 +4197,17 @@ DY added (default is 10).  */)
 
   /* Move the tooltip window where the mouse pointer is.  Resize and
      show it.  */
-#if 0 /* TODO : Mac specifics */
-  compute_tip_xy (f, parms, dx, dy, &root_x, &root_y);
+  compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
 
   BLOCK_INPUT;
-  XQueryPointer (FRAME_W32_DISPLAY (f), FRAME_W32_DISPLAY_INFO (f)->root_window,
-                &root, &child, &root_x, &root_y, &win_x, &win_y, &pmask);
-  XMoveResizeWindow (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f),
-                    root_x + 5, root_y - height - 5, width, height);
-  XMapRaised (FRAME_W32_DISPLAY (f), FRAME_W32_WINDOW (f));
+  MoveWindow (FRAME_MAC_WINDOW (f), root_x, root_y, false);
+  SizeWindow (FRAME_MAC_WINDOW (f), width, height, true);
+  ShowWindow (FRAME_MAC_WINDOW (f));
+  BringToFront (FRAME_MAC_WINDOW (f));
   UNBLOCK_INPUT;
-#endif /* MAC_TODO */
+
+  FRAME_PIXEL_WIDTH (f) = width;
+  FRAME_PIXEL_HEIGHT (f) = height;
 
   /* Draw into the window.  */
   w->must_be_updated_p = 1;
@@ -4046,8 +4229,8 @@ DY added (default is 10).  */)
 
 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
        doc: /* Hide the current tooltip window, if there is any.
-Value is t is tooltip was open, nil otherwise.  */)
-  ()
+Value is t if tooltip was open, nil otherwise.  */)
+     ()
 {
   int count;
   Lisp_Object deleted, frame, timer;
@@ -4081,106 +4264,188 @@ Value is t is tooltip was open, nil otherwise.  */)
 
 
 \f
+#if TARGET_API_MAC_CARBON
 /***********************************************************************
                        File selection dialog
  ***********************************************************************/
 
-#if 0 /* MAC_TODO: can standard file dialog */
+static pascal void mac_nav_event_callback P_ ((NavEventCallbackMessage,
+                                              NavCBRecPtr, void *));
+
+/**
+   There is a relatively standard way to do this using applescript to run
+   a (choose file) method.  However, this doesn't do "the right thing"
+   by working only if the find-file occurred during a menu or toolbar
+   click.  So we must do the file dialog by hand, using the navigation
+   manager.  This also has more flexibility in determining the default
+   directory and whether or not we are going to choose a file.
+ **/
+
 extern Lisp_Object Qfile_name_history;
 
-DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 4, 0,
+DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
        doc: /* Read file name, prompting with PROMPT in directory DIR.
 Use a file selection dialog.
 Select DEFAULT-FILENAME in the dialog's file selection box, if
-specified.  Don't let the user enter a file name in the file
-selection dialog's entry field, if MUSTMATCH is non-nil.  */)
-  (prompt, dir, default_filename, mustmatch)
-     Lisp_Object prompt, dir, default_filename, mustmatch;
+specified.  Ensure that file exists if MUSTMATCH is non-nil.
+If ONLY-DIR-P is non-nil, the user can only select directories.  */)
+     (prompt, dir, default_filename, mustmatch, only_dir_p)
+     Lisp_Object prompt, dir, default_filename, mustmatch, only_dir_p;
 {
   struct frame *f = SELECTED_FRAME ();
   Lisp_Object file = Qnil;
   int count = SPECPDL_INDEX ();
-  struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
-  char filename[MAX_PATH + 1];
-  char init_dir[MAX_PATH + 1];
-  int use_dialog_p = 1;
+  struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
+  char filename[MAXPATHLEN];
+  static NavEventUPP mac_nav_event_callbackUPP = NULL;
 
-  GCPRO5 (prompt, dir, default_filename, mustmatch, file);
+  GCPRO6 (prompt, dir, default_filename, mustmatch, file, only_dir_p);
   CHECK_STRING (prompt);
   CHECK_STRING (dir);
 
   /* Create the dialog with PROMPT as title, using DIR as initial
      directory and using "*" as pattern.  */
   dir = Fexpand_file_name (dir, Qnil);
-  strncpy (init_dir, SDATA (dir), MAX_PATH);
-  init_dir[MAX_PATH] = '\0';
-  unixtodos_filename (init_dir);
 
-  if (STRINGP (default_filename))
-    {
-      char *file_name_only;
-      char *full_path_name = SDATA (default_filename);
+  {
+    OSStatus status;
+    NavDialogCreationOptions options;
+    NavDialogRef dialogRef;
+    NavTypeListHandle fileTypes = NULL;
+    NavUserAction userAction;
+    CFStringRef message=NULL, saveName = NULL;
 
-      unixtodos_filename (full_path_name);
+    BLOCK_INPUT;
+    /* No need for a callback function because we are modal */
+    NavGetDefaultDialogCreationOptions(&options);
+    options.modality = kWindowModalityAppModal;
+    options.location.h = options.location.v = -1;
+    options.optionFlags = kNavDefaultNavDlogOptions;
+    options.optionFlags |= kNavAllFilesInPopup;  /* All files allowed */
+    options.optionFlags |= kNavSelectAllReadableItem;
+    options.optionFlags &= ~kNavAllowMultipleFiles;
+    if (!NILP(prompt))
+      {
+       message = cfstring_create_with_string (prompt);
+       options.message = message;
+      }
+    /* Don't set the application, let it use default.
+    options.clientName = CFSTR ("Emacs");
+    */
 
-      file_name_only = strrchr (full_path_name, '\\');
-      if (!file_name_only)
-        file_name_only = full_path_name;
-      else
-        {
-          file_name_only++;
+    if (mac_nav_event_callbackUPP == NULL)
+      mac_nav_event_callbackUPP = NewNavEventUPP (mac_nav_event_callback);
 
-          /* If default_file_name is a directory, don't use the open
-             file dialog, as it does not support selecting
-             directories. */
-          if (!(*file_name_only))
-            use_dialog_p = 0;
-        }
-
-      strncpy (filename, file_name_only, MAX_PATH);
-      filename[MAX_PATH] = '\0';
-    }
-  else
-    filename[0] = '\0';
+    if (!NILP (only_dir_p))
+      status = NavCreateChooseFolderDialog(&options, mac_nav_event_callbackUPP,
+                                          NULL, NULL, &dialogRef);
+    else if (NILP (mustmatch))
+      {
+       /* This is a save dialog */
+       options.optionFlags |= kNavDontConfirmReplacement;
+       options.actionButtonLabel = CFSTR ("Ok");
+       options.windowTitle = CFSTR ("Enter name");
 
-  if (use_dialog_p)
-    {
-      OPENFILENAME file_details;
-      char *filename_file;
+       if (STRINGP (default_filename))
+         {
+           Lisp_Object utf8 = ENCODE_UTF_8 (default_filename);
+           char *begPtr = SDATA(utf8);
+           char *filePtr = begPtr + SBYTES(utf8);
+           while (filePtr != begPtr && !IS_DIRECTORY_SEP(filePtr[-1]))
+             filePtr--;
+           saveName = cfstring_create_with_utf8_cstring (filePtr);
+           options.saveFileName = saveName;
+           options.optionFlags |= kNavSelectDefaultLocation;
+         }
+         status = NavCreatePutFileDialog(&options,
+                                         'TEXT', kNavGenericSignature,
+                                         mac_nav_event_callbackUPP, NULL,
+                                         &dialogRef);
+       }
+    else
+      {
+       /* This is an open dialog*/
+       status = NavCreateChooseFileDialog(&options, fileTypes,
+                                          mac_nav_event_callbackUPP, NULL,
+                                          NULL, NULL, &dialogRef);
+      }
 
-      /* Prevent redisplay.  */
-      specbind (Qinhibit_redisplay, Qt);
-      BLOCK_INPUT;
+    /* Set the default location and continue*/
+    if (status == noErr)
+      {
+       Lisp_Object encoded_dir = ENCODE_FILE (dir);
+       AEDesc defLocAed;
 
-      bzero (&file_details, sizeof (file_details));
-      file_details.lStructSize = sizeof (file_details);
-      file_details.hwndOwner = FRAME_W32_WINDOW (f);
-      file_details.lpstrFile = filename;
-      file_details.nMaxFile = sizeof (filename);
-      file_details.lpstrInitialDir = init_dir;
-      file_details.lpstrTitle = SDATA (prompt);
-      file_details.Flags = OFN_HIDEREADONLY | OFN_NOCHANGEDIR;
+       status = AECreateDesc (TYPE_FILE_NAME, SDATA (encoded_dir),
+                              SBYTES (encoded_dir), &defLocAed);
+       if (status == noErr)
+         {
+           NavCustomControl(dialogRef, kNavCtlSetLocation, (void*) &defLocAed);
+           AEDisposeDesc(&defLocAed);
+         }
+       status = NavDialogRun(dialogRef);
+      }
 
-      if (!NILP (mustmatch))
-        file_details.Flags |= OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
+    if (saveName) CFRelease(saveName);
+    if (message) CFRelease(message);
 
-      if (GetOpenFileName (&file_details))
-        {
-          dostounix_filename (filename);
-          file = build_string (filename);
-        }
-      else
-        file = Qnil;
+    if (status == noErr) {
+      userAction = NavDialogGetUserAction(dialogRef);
+      switch (userAction)
+       {
+       case kNavUserActionNone:
+       case kNavUserActionCancel:
+         break;                /* Treat cancel like C-g */
+       case kNavUserActionOpen:
+       case kNavUserActionChoose:
+       case kNavUserActionSaveAs:
+         {
+           NavReplyRecord reply;
+           Size len;
 
+           status = NavDialogGetReply(dialogRef, &reply);
+           if (status != noErr)
+             break;
+           status = AEGetNthPtr (&reply.selection, 1, TYPE_FILE_NAME,
+                                 NULL, NULL, filename,
+                                 sizeof (filename) - 1, &len);
+           if (status == noErr)
+             {
+               len = min (len, sizeof (filename) - 1);
+               filename[len] = '\0';
+               if (reply.saveFileName)
+                 {
+                   /* If it was a saved file, we need to add the file name */
+                   if (len && len < sizeof (filename) - 1
+                       && filename[len-1] != '/')
+                     filename[len++] = '/';
+                   CFStringGetCString(reply.saveFileName, filename+len,
+                                      sizeof (filename) - len,
+#if MAC_OSX
+                                      kCFStringEncodingUTF8
+#else
+                                      CFStringGetSystemEncoding ()
+#endif
+                                      );
+                 }
+               file = DECODE_FILE (make_unibyte_string (filename,
+                                                        strlen (filename)));
+             }
+           NavDisposeReply(&reply);
+         }
+         break;
+       }
+      NavDialogDispose(dialogRef);
       UNBLOCK_INPUT;
-      file = unbind_to (count, file);
     }
-  /* Open File dialog will not allow folders to be selected, so resort
-     to minibuffer completing reads for directories. */
-  else
-    file = Fcompleting_read (prompt, intern ("read-file-name-internal"),
-                             dir, mustmatch, dir, Qfile_name_history,
-                             default_filename, Qnil);
+    else {
+      UNBLOCK_INPUT;
+      /* Fall back on minibuffer if there was a problem */
+      file = Fcompleting_read (prompt, intern ("read-file-name-internal"),
+                              dir, mustmatch, dir, Qfile_name_history,
+                              default_filename, Qnil);
+    }
+  }
 
   UNGCPRO;
 
@@ -4190,15 +4455,24 @@ selection dialog's entry field, if MUSTMATCH is non-nil.  */)
 
   return unbind_to (count, file);
 }
-#endif /* MAC_TODO */
 
 
+/* Need to register some event callback function for enabling drag and
+   drop in Navigation Service dialogs.  */
+static pascal void
+mac_nav_event_callback (selector, parms, data)
+     NavEventCallbackMessage selector;
+     NavCBRecPtr parms;
+     void *data ;
+{
+}
+#endif
 \f
 /***********************************************************************
                            Initialization
  ***********************************************************************/
 
-/* Keep this list in the same order as frame_parms in frame.c. 
+/* Keep this list in the same order as frame_parms in frame.c.
    Use 0 for unsupported frame parameters.  */
 
 frame_parm_handler mac_frame_parm_handlers[] =
@@ -4218,7 +4492,7 @@ frame_parm_handler mac_frame_parm_handlers[] =
   x_set_menu_bar_lines,
   x_set_mouse_color,
   x_explicitly_set_name,
-  x_set_scroll_bar_width,
+  mac_set_scroll_bar_width,
   x_set_title,
   x_set_unsplittable,
   x_set_vertical_scroll_bars,
@@ -4228,17 +4502,22 @@ frame_parm_handler mac_frame_parm_handlers[] =
   0, /* MAC_TODO: x_set_scroll_bar_background, */
   x_set_screen_gamma,
   x_set_line_spacing,
-  0, /* MAC_TODO: x_set_fringe_width, */
-  0, /* MAC_TODO: x_set_fringe_width, */
+  x_set_fringe_width,
+  x_set_fringe_width,
   0, /* x_set_wait_for_wm, */
-  0, /* MAC_TODO: x_set_fullscreen, */
+  x_set_fullscreen,
 };
 
 void
 syms_of_macfns ()
 {
-  /* Certainly running on Mac.  */
+#ifdef MAC_OSX
+  /* This is zero if not using Mac native windows.  */
+  mac_in_use = 0;
+#else
+  /* Certainly running on Mac native windows.  */
   mac_in_use = 1;
+#endif
 
   /* The section below is built by the lisp expression at the top of the file,
      just above where these variables are declared.  */
@@ -4249,29 +4528,14 @@ syms_of_macfns ()
   staticpro (&Qsuppress_icon);
   Qundefined_color = intern ("undefined-color");
   staticpro (&Qundefined_color);
+  Qcancel_timer = intern ("cancel-timer");
+  staticpro (&Qcancel_timer);
   /* This is the end of symbol initialization.  */
 
-  Qhyper = intern ("hyper");
-  staticpro (&Qhyper);
-  Qsuper = intern ("super");
-  staticpro (&Qsuper);
-  Qmeta = intern ("meta");
-  staticpro (&Qmeta);
-  Qalt = intern ("alt");
-  staticpro (&Qalt);
-  Qctrl = intern ("ctrl");
-  staticpro (&Qctrl);
-  Qcontrol = intern ("control");
-  staticpro (&Qcontrol);
-  Qshift = intern ("shift");
-  staticpro (&Qshift);
-
   /* Text property `display' should be nonsticky by default.  */
   Vtext_property_default_nonsticky
     = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
 
-  Qface_set_after_frame_default = intern ("face-set-after-frame-default");
-  staticpro (&Qface_set_after_frame_default);
 
   Fput (Qundefined_color, Qerror_conditions,
        Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
@@ -4279,43 +4543,67 @@ syms_of_macfns ()
        build_string ("Undefined color"));
 
   DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
-              doc: /* The shape of the pointer when over text.
+    doc: /* The shape of the pointer when over text.
 Changing the value does not affect existing frames
 unless you set the mouse color.  */);
   Vx_pointer_shape = Qnil;
 
+#if 0 /* This doesn't really do anything.  */
+  DEFVAR_LISP ("x-nontext-pointer-shape", &Vx_nontext_pointer_shape,
+    doc: /* The shape of the pointer when not over text.
+This variable takes effect when you create a new frame
+or when you set the mouse color.  */);
+#endif
   Vx_nontext_pointer_shape = Qnil;
 
-  Vx_mode_pointer_shape = Qnil;
-
   DEFVAR_LISP ("x-hourglass-pointer-shape", &Vx_hourglass_pointer_shape,
-              doc: /* The shape of the pointer when Emacs is hourglass.
+    doc: /* The shape of the pointer when Emacs is busy.
 This variable takes effect when you create a new frame
 or when you set the mouse color.  */);
   Vx_hourglass_pointer_shape = Qnil;
 
   DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
-              doc: /* Non-zero means Emacs displays an hourglass pointer on window systems.  */);
+    doc: /* Non-zero means Emacs displays an hourglass pointer on window systems.  */);
   display_hourglass_p = 1;
 
   DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
-              doc: /* *Seconds to wait before displaying an hourglass pointer.
+    doc: /* *Seconds to wait before displaying an hourglass pointer.
 Value must be an integer or float.  */);
   Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
 
+#if 0 /* This doesn't really do anything.  */
+  DEFVAR_LISP ("x-mode-pointer-shape", &Vx_mode_pointer_shape,
+    doc: /* The shape of the pointer when over the mode line.
+This variable takes effect when you create a new frame
+or when you set the mouse color.  */);
+#endif
+  Vx_mode_pointer_shape = Qnil;
+
   DEFVAR_LISP ("x-sensitive-text-pointer-shape",
-              &Vx_sensitive_text_pointer_shape,
+             &Vx_sensitive_text_pointer_shape,
               doc: /* The shape of the pointer when over mouse-sensitive text.
 This variable takes effect when you create a new frame
 or when you set the mouse color.  */);
   Vx_sensitive_text_pointer_shape = Qnil;
 
+  DEFVAR_LISP ("x-window-horizontal-drag-cursor",
+             &Vx_window_horizontal_drag_shape,
+  doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
+This variable takes effect when you create a new frame
+or when you set the mouse color.  */);
+  Vx_window_horizontal_drag_shape = Qnil;
+
   DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
-              doc: /* A string indicating the foreground color of the cursor box.  */);
+    doc: /* A string indicating the foreground color of the cursor box.  */);
   Vx_cursor_fore_pixel = Qnil;
 
+  DEFVAR_LISP ("x-max-tooltip-size", &Vx_max_tooltip_size,
+    doc: /* Maximum size for tooltips.  Value is a pair (COLUMNS . ROWS).
+Text larger than this is clipped.  */);
+  Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
+
   DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
-              doc: /* Non-nil if no window manager is in use.
+    doc: /* Non-nil if no window manager is in use.
 Emacs doesn't try to figure this out; this is always nil
 unless you set it to something else.  */);
   /* We don't have any way to find this out, so set it to nil
@@ -4324,7 +4612,7 @@ unless you set it to something else.  */);
 
   DEFVAR_LISP ("x-pixel-size-width-font-regexp",
               &Vx_pixel_size_width_font_regexp,
-              doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
+    doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
 
 Since Emacs gets width of a font matching with this regexp from
 PIXEL_SIZE field of the name, font finding mechanism gets faster for
@@ -4355,12 +4643,11 @@ Chinese, Japanese, and Korean.  */);
   defsubr (&Sx_display_backing_store);
   defsubr (&Sx_display_save_under);
   defsubr (&Sx_create_frame);
-#if 0 /* MAC_TODO: implement network support */
   defsubr (&Sx_open_connection);
   defsubr (&Sx_close_connection);
-#endif
   defsubr (&Sx_display_list);
   defsubr (&Sx_synchronize);
+  defsubr (&Sx_focus_frame);
 
   /* Setting callback functions for fontset handler.  */
   get_font_info_func = x_get_font_info;
@@ -4386,7 +4673,10 @@ Chinese, Japanese, and Korean.  */);
   tip_frame = Qnil;
   staticpro (&tip_frame);
 
-#if 0 /* MAC_TODO */
+  last_show_tip_args = Qnil;
+  staticpro (&last_show_tip_args);
+
+#if TARGET_API_MAC_CARBON
   defsubr (&Sx_file_dialog);
 #endif
 }