OSDN Git Service

#37449 (2.2.0.75) ゲームメッセージのログ拡張に伴って発生したセーブデータのトラブルを修正。 / Fix trouble of savedata...
[hengband/hengband.git] / src / main-mac.c
index 75be462..231d217 100644 (file)
@@ -1,4 +1,4 @@
-/* File: main-mac.c */
+/* File: main-mac.c */
 
 /* Purpose: Simple support for MACINTOSH Angband */
 
 
 #include "angband.h"
 
+#ifdef MACH_O_CARBON
+
+#include <Carbon/Carbon.h>
+#include <QuickTime/QuickTime.h>
+#include <CoreServices/CoreServices.h>
+#include <CoreFoundation/CoreFoundation.h>
+
+#define TARGET_API_MAC_CARBON 1
+
+#else /* MACH_O_CARBON */
+
 #include <Types.h>
 #include <Gestalt.h>
 #include <QuickDraw.h>
 #include <Memory.h>
 #include <QDOffscreen.h>
 #include <Sound.h>
+#if TARGET_API_MAC_CARBON
+#include <Navigation.h>
+#include <CFPreferences.h>
+#include <CFNumber.h>
+# ifdef MAC_MPW
+# include <CarbonStdCLib.h>
+# endif
+#endif
 
 #ifdef JP
 
 
 #endif
 
+#endif /* MACH_O_CARBON */
+
+/*
+ * Cleaning up a couple of things to make these easier to change --AR
+ */
+#define PREF_FILE_NAME _("Hengband Preferences", "Hengband-E Preferences")
+
 /*
  * Use "malloc()" instead of "NewPtr()"
  */
 /* #define USE_MALLOC */
 
 
+/* Default creator signature */
+#ifndef ANGBAND_CREATOR
+# define ANGBAND_CREATOR 'Heng'
+#endif
+
+
 #if defined(powerc) || defined(__powerc)
 
 /*
  */
 # define USE_SFL_CODE
 
+/*
+ * Use rewritten asynchronous sound player
+ */
+#define USE_ASYNC_SOUND
+
+
+#ifdef MACH_O_CARBON
+
+#define USE_QT_SOUND
+
+#endif /* MACH_O_CARBON */
 #endif /* ANGBAND_LITE_MAC */
 
 
 
+#ifndef MACH_O_CARBON
 #ifdef USE_SFL_CODE
 
 /*
 #include <Folders.h>
 
 #endif
-
+#endif /* !MACH_O_CARBON */
 
 /*
  * Globals for MPW compilation
  */
-#ifdef MAC_MPW
+#if defined(MACH_O_CARBON) || defined(MAC_MPW)
        /* Globals needed */
+#if !TARGET_API_MAC_CARBON
        QDGlobals qd;
-       u32b _ftype;
-       u32b _fcreator;
+#endif
+       OSType _ftype;
+       OSType _fcreator;
 #endif
 
 
@@ -297,12 +343,6 @@ struct term_data
 
 #endif /* ANGBAND_LITE_MAC */
 
-#ifdef JP
-       GWorldPtr               bufferPort;
-       PixMapHandle    bufferPixHndl;
-       PixMapPtr               bufferPix;
-#endif
-
        Str15           title;
 
        s16b            oops;
@@ -348,6 +388,7 @@ struct term_data
  */
 static bool CheckEvents(bool wait);
 
+#ifndef MACH_O_CARBON
 
 /*
  * Hack -- location of the main directory
@@ -355,6 +396,7 @@ static bool CheckEvents(bool wait);
 static short app_vol;
 static long  app_dir;
 
+#endif /* !MACH_O_CARBON */
 
 /*
  * Delay handling of double-clicked savefiles
@@ -392,13 +434,20 @@ static term_data data[MAX_TERM_DATA];
  */
 static bool initialized = FALSE;
 
+/*
+ * Version of Mac OS - for version specific bug workarounds (; ;)
+ */
+static long mac_os_version;
 
 
+#if defined(__MWERKS__)
 /*
  * CodeWarrior uses Universal Procedure Pointers
  */
 static ModalFilterUPP ynfilterUPP;
 
+#endif /* __MWERKS__ */
+
 #ifdef USE_SFL_CODE
 
 /*
@@ -412,98 +461,58 @@ AEEventHandlerUPP AEH_Open_UPP;
 #endif
 
 /*
-       Extra Sound Mode
-*/
-#ifdef JP
-static int ext_sound = 0;
-
-#define                SND_NON         0
-#define                SND_ATTACK      1
-#define                SND_MOVE                2
-#define                SND_TRAP                3
-#define                SND_SHOP                4
-#define                SND_ME          5
-#define                SND_CMD_ERROR   6
-
-static int soundchoice[] = {
-       SND_NON,
-       SND_ATTACK,
-       SND_ATTACK,
-       SND_ATTACK,
-       SND_TRAP,
-       SND_ATTACK,
-       SND_ME,
-       SND_ME,
-       SND_ME,
-       SND_MOVE,
-       SND_ATTACK,
-       SND_ME,
-       SND_ATTACK,
-       SND_NON,
-       SND_MOVE,
-       SND_MOVE,
-       SND_ME,
-       SND_SHOP,
-       SND_SHOP,
-       SND_SHOP,
-       SND_SHOP,
-       SND_MOVE,
-       SND_MOVE,
-       SND_MOVE,
-       SND_MOVE,
-       SND_ATTACK,
-       SND_SHOP,
-       SND_SHOP,
-       SND_ME,
-       SND_NON,
-       SND_ATTACK,
-       SND_NON,
-       SND_NON,
-       SND_NON,
-       SND_NON,
-       SND_ATTACK,
-       SND_ATTACK,
-       SND_NON,
-       SND_NON,
-       SND_ATTACK,
-       SND_NON,
-       SND_NON,
-       SND_NON,
-       SND_TRAP,
-       SND_ATTACK,
-       SND_ATTACK,
-       SND_ATTACK,
-       SND_ATTACK,
-       SND_ATTACK,
-       SND_NON,
-       SND_NON,
-       SND_NON,
-       SND_NON,
-       SND_NON,
-       SND_CMD_ERROR,
-       SND_TRAP,
-       SND_NON,
-       SND_NON,
-       SND_TRAP,
-       SND_ATTACK,
-       SND_TRAP,
-       SND_ATTACK,
-       SND_ATTACK,
-       SND_NON,
-       SND_TRAP,
-};
+ * Convert refnum+vrefnum+fname into a full file name
+ * Store this filename in 'buf' (make sure it is long enough)
+ * Note that 'fname' looks to be a "pascal" string
+ */
+#if TARGET_API_MAC_CARBON
+static void refnum_to_name(char *buf, long refnum, short vrefnum, char *fname)
+{
+       CInfoPBRec pb;
+       int err;
+       int i, j;
 
-static int                     soundmode[8];
+       char res[1000];
+       
+       FSSpec spec;
+       short   vref;
+    long       dirID;
+    
+       i=999;
 
+       res[i]=0; i--;
+       for (j=1; j<=fname[0]; j++)
+       {
+               res[i-fname[0]+j] = fname[j];
+       }
+       i-=fname[0];
 
-#endif
+       vref = vrefnum;
+       dirID = refnum;
 
+       while (1)
+       {
+               pb.dirInfo.ioDrDirID=pb.dirInfo.ioDrParID;
+               err = FSMakeFSSpec( vref, dirID, "\p", &spec );
+               
+               if( err != noErr )
+                   break;
+               
+               res[i] = ':'; i--;
+               for (j=1; j<=spec.name[0]; j++)
+               {
+                       res[i-spec.name[0]+j] = spec.name[j];
+               }
+               i -= spec.name[0];
+               
+               dirID = spec.parID;
+       }
 
-/*
- * Convert refnum+vrefnum+fname into a full file name
- * Store this filename in 'buf' (make sure it is long enough)
- * Note that 'fname' looks to be a "pascal" string
- */
+       /* Extract the result */
+       for (j = 0, i++; res[i]; j++, i++) buf[j] = res[i];
+       buf[j] = 0;
+}
+#else
 static void refnum_to_name(char *buf, long refnum, short vrefnum, char *fname)
 {
        DirInfo pb;
@@ -546,7 +555,39 @@ static void refnum_to_name(char *buf, long refnum, short vrefnum, char *fname)
        for (j = 0, i++; res[i]; j++, i++) buf[j] = res[i];
        buf[j] = 0;
 }
+#endif
 
+#if TARGET_API_MAC_CARBON
+pascal OSErr FSpLocationFromFullPath(short fullPathLength,
+                                                                        const void *fullPath,
+                                                                        FSSpec *spec)
+{
+       AliasHandle     alias;
+       OSErr           result;
+       Boolean         wasChanged;
+       Str32           nullString;
+       
+       /* Create a minimal alias from the full pathname */
+       nullString[0] = 0;      /* null string to indicate no zone or server name */
+       result = NewAliasMinimalFromFullPath(fullPathLength, fullPath, nullString, nullString, &alias);
+       if ( result == noErr )
+       {
+               /* Let the Alias Manager resolve the alias. */
+               result = ResolveAlias(NULL, alias, spec, &wasChanged);
+               
+               /* work around Alias Mgr sloppy volume matching bug */
+               if ( spec->vRefNum == 0 )
+               {
+                       /* invalidate wrong FSSpec */
+                       spec->parID = 0;
+                       spec->name[0] =  0;
+                       result = nsvErr;
+               }
+               DisposeHandle((Handle)alias);   /* Free up memory used */
+       }
+       return ( result );
+}
+#endif
 
 #if 0
 
@@ -592,109 +633,345 @@ static bool askfor_file(char *buf, int len)
 
 #endif
 
+#if !TARGET_API_MAC_CARBON
+static void local_to_global( Rect *r )
+{
+       Point           temp;
+       
+       temp.h = r->left;
+       temp.v = r->top;
+       
+       LocalToGlobal( &temp );
+       
+       r->left = temp.h;
+       r->top = temp.v;
+       
+       temp.h = r->right;
+       temp.v = r->bottom;
+       
+       LocalToGlobal( &temp );
+       
+       r->right = temp.h;
+       r->bottom = temp.v;
+}
+#endif /* !TARGET_API_MAC_CARBON */
+
+static void global_to_local( Rect *r )
+{
+       Point           temp;
+       
+       temp.h = r->left;
+       temp.v = r->top;
+       
+       GlobalToLocal( &temp );
+       
+       r->left = temp.h;
+       r->top = temp.v;
+       
+       temp.h = r->right;
+       temp.v = r->bottom;
+       
+       GlobalToLocal( &temp );
+       
+       r->right = temp.h;
+       r->bottom = temp.v;
+}
+
 
+#ifdef MAC_MPW
 
 /*
- * Center a rectangle inside another rectangle
+ * Convert pathname to an appropriate format, because MPW's
+ * CarbonStdCLib chose to use system's native path format,
+ * making our lives harder to create binaries that run on
+ * OS 8/9 and OS X :( -- pelpel
  */
-static void center_rect(Rect *r, Rect *s)
+void convert_pathname(char* path)
 {
-       int centerx = (s->left + s->right)/2;
-       int centery = (2*s->top + s->bottom)/3;
-       int dx = centerx - (r->right - r->left)/2 - r->left;
-       int dy = centery - (r->bottom - r->top)/2 - r->top;
-       r->left += dx;
-       r->right += dx;
-       r->top += dy;
-       r->bottom += dy;
+       char buf[1024];
+
+       /* Nothing has to be done for CarbonLib on Classic */
+       if (mac_os_version >= 0x1000)
+       {
+               /* Convert to POSIX style */
+               ConvertHFSPathToUnixPath(path, buf);
+
+               /* Copy the result back */
+               strcpy(path, buf);
+       }
+
+       /* Done. */
+       return;
 }
 
+# ifdef CHECK_MODIFICATION_TIME
 
 /*
- * Convert a pascal string in place
- *
- * This function may be defined elsewhere, but since it is so
- * small, it is not worth finding the proper function name for
- * all the different platforms.
+ * Although there is no easy way to emulate fstat in the old interface,
+ * we still can do stat-like things, because Mac OS is an OS.
  */
-static void ptocstr(StringPtr src)
+static int get_modification_time(cptr path, u32b *mod_time)
 {
+       CInfoPBRec pb;
+       Str255 pathname;
        int i;
 
-       /* Hack -- pointer */
-       char *s = (char*)(src);
+       /* Paranoia - make sure the pathname fits in Str255 */
+       i = strlen(path);
+       if (i > 255) return (-1);
 
-       /* Hack -- convert the string */
-       for (i = s[0]; i; i--, s++) s[0] = s[1];
+       /* Convert pathname to a Pascal string */
+       strncpy((char *)pathname + 1, path, 255);
+       pathname[0] = i;
 
-       /* Hack -- terminate the string */
-       s[0] = '\0';
-}
+       /* Set up parameter block */
+       pb.hFileInfo.ioNamePtr = pathname;
+       pb.hFileInfo.ioFDirIndex = 0;
+       pb.hFileInfo.ioVRefNum = app_vol;
+       pb.hFileInfo.ioDirID = 0;
 
+       /* Get catalog information of the file */
+       if (PBGetCatInfoSync(&pb) != noErr) return (-1);
 
-#if defined(USE_SFL_CODE)
+       /* Set modification date and time */
+       *mod_time = pb.hFileInfo.ioFlMdDat;
+
+       /* Success */
+       return (0);
+}
 
 
 /*
- * The following three routines (pstrcat, pstrinsert, and PathNameFromDirID)
- * were taken from the Think Reference section called "Getting a Full Pathname"
- * (under the File Manager section).  We need PathNameFromDirID to get the
- * full pathname of the opened savefile, making no assumptions about where it
- * is.
- *
- * I had to hack PathNameFromDirID a little for MetroWerks, but it's awfully
- * nice.
+ * A (non-Mach-O) Mac OS version of check_modification_time, for those
+ * compilers without good enough POSIX-compatibility libraries XXX XXX
  */
-static void pstrcat(StringPtr dst, StringPtr src)
+errr check_modification_date(int fd, cptr template_file)
 {
-       /* copy string in */
-       BlockMove(src + 1, dst + *dst + 1, *src);
+#pragma unused(fd)
+       u32b txt_stat, raw_stat;
+       char *p;
+       char fname[32];
+       char buf[1024];
 
-       /* adjust length byte */
-       *dst += *src;
-}
+       /* Build the file name */
+       path_build(buf, sizeof(buf), ANGBAND_DIR_EDIT, template_file);
 
-/*
- * pstrinsert - insert string 'src' at beginning of string 'dst'
- */
-static void pstrinsert(StringPtr dst, StringPtr src)
-{
-       /* make room for new string */
-       BlockMove(dst + 1, dst + *src + 1, *dst);
+       /* XXX XXX XXX */
+       convert_pathname(buf);
 
-       /* copy new string in */
-       BlockMove(src + 1, dst + 1, *src);
+       /* Obtain modification time */
+       if (get_modification_time(buf, &txt_stat)) return (-1);
 
-       /* adjust length byte */
-       *dst += *src;
-}
+       /* XXX Build filename of the corresponding *.raw file */
+       strnfmt(fname, sizeof(fname), "%s", template_file);
 
-static void PathNameFromDirID(long dirID, short vRefNum, StringPtr fullPathName)
-{
-       CInfoPBRec      block;
-       Str255  directoryName;
-       OSErr   err;
+       /* Find last '.' */
+       p = strrchr(fname, '.');
 
-       fullPathName[0] = '\0';
+       /* Can't happen */
+       if (p == NULL) return (-1);
 
-       block.dirInfo.ioDrParID = dirID;
-       block.dirInfo.ioNamePtr = directoryName;
+       /* Substitute ".raw" for ".txt" */
+       strcpy(p, ".raw");
 
-       while (1)
-       {
-               block.dirInfo.ioVRefNum = vRefNum;
-               block.dirInfo.ioFDirIndex = -1;
-               block.dirInfo.ioDrDirID = block.dirInfo.ioDrParID;
-               err = PBGetCatInfo(&block, FALSE);
-               pstrcat(directoryName, (StringPtr)"\p:");
-               pstrinsert(fullPathName, directoryName);
-               if (block.dirInfo.ioDrDirID == 2) break;
-       }
-}
+       /* Build the file name of the raw file */
+       path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, fname);
 
-#endif
+       /* XXX XXX XXX */
+       convert_pathname(buf);
 
+       /* Obtain modification time */
+       if (get_modification_time(buf, &raw_stat)) return (-1);
 
+       /* Ensure the text file is not newer than the raw file */
+       if (txt_stat > raw_stat) return (-1);
+
+       /* Keep using the current .raw file */
+       return (0);
+}
+
+# endif /* CHECK_MODIFICATION_TIME */
+
+#endif /* MAC_MPW */
+
+/*
+ * Center a rectangle inside another rectangle
+ */
+static void center_rect(Rect *r, Rect *s)
+{
+       int centerx = (s->left + s->right)/2;
+       int centery = (2*s->top + s->bottom)/3;
+       int dx = centerx - (r->right - r->left)/2 - r->left;
+       int dy = centery - (r->bottom - r->top)/2 - r->top;
+       r->left += dx;
+       r->right += dx;
+       r->top += dy;
+       r->bottom += dy;
+}
+
+
+#ifdef MACH_O_CARBON
+
+/* Carbon File Manager utilities by pelpel */
+
+/*
+ * (Carbon)
+ * Convert a pathname to a corresponding FSSpec.
+ * Returns noErr on success.
+ */
+static OSErr path_to_spec(const char *path, FSSpec *spec)
+{
+       OSErr err;
+       FSRef ref;
+
+       /* Convert pathname to FSRef ... */
+       err = FSPathMakeRef(path, &ref, NULL);
+       if (err != noErr) return (err);
+
+       /* ... then FSRef to FSSpec */
+       err = FSGetCatalogInfo(&ref, kFSCatInfoNone, NULL, NULL, spec, NULL);
+       
+       /* Inform caller of success or failure */
+       return (err);
+}
+
+
+/*
+ * (Carbon)
+ * Convert a FSSpec to a corresponding pathname.
+ * Returns noErr on success.
+ */
+static OSErr spec_to_path(const FSSpec *spec, char *buf, size_t size)
+{
+       OSErr err;
+       FSRef ref;
+
+       /* Convert FSSpec to FSRef ... */
+       err = FSpMakeFSRef(spec, &ref);
+       if (err != noErr) return (err);
+
+       /* ... then FSRef to pathname */
+       err = FSRefMakePath(&ref, buf, size);
+
+       /* Inform caller of success or failure */
+       return (err);
+}
+
+
+/*
+ * (Carbon) [via path_to_spec]
+ * Set creator and filetype of a file specified by POSIX-style pathname.
+ * Returns 0 on success, -1 in case of errors.
+ */
+void fsetfileinfo(cptr pathname, OSType fcreator, OSType ftype)
+{
+       OSErr err;
+       FSSpec spec;
+       FInfo info;
+
+       /* Convert pathname to FSSpec */
+       if (path_to_spec(pathname, &spec) != noErr) return;
+
+       /* Obtain current finder info of the file */
+       if (FSpGetFInfo(&spec, &info) != noErr) return;
+
+       /* Overwrite creator and type */
+       info.fdCreator = fcreator;
+       info.fdType = ftype;
+       err = FSpSetFInfo(&spec, &info);
+
+       /* Done */
+       return;
+}
+
+
+#else /* MACH_O_CARBON */
+
+
+/*
+ * Convert a pascal string in place
+ *
+ * This function may be defined elsewhere, but since it is so
+ * small, it is not worth finding the proper function name for
+ * all the different platforms.
+ */
+static void ptocstr(StringPtr src)
+{
+       int i;
+
+       /* Hack -- pointer */
+       char *s = (char*)(src);
+
+       /* Hack -- convert the string */
+       for (i = s[0]; i; i--, s++) s[0] = s[1];
+
+       /* Hack -- terminate the string */
+       s[0] = '\0';
+}
+
+
+#if defined(USE_SFL_CODE)
+
+
+/*
+ * The following three routines (pstrcat, pstrinsert, and PathNameFromDirID)
+ * were taken from the Think Reference section called "Getting a Full Pathname"
+ * (under the File Manager section).  We need PathNameFromDirID to get the
+ * full pathname of the opened savefile, making no assumptions about where it
+ * is.
+ *
+ * I had to hack PathNameFromDirID a little for MetroWerks, but it's awfully
+ * nice.
+ */
+static void pstrcat(StringPtr dst, StringPtr src)
+{
+       /* copy string in */
+       BlockMove(src + 1, dst + *dst + 1, *src);
+
+       /* adjust length byte */
+       *dst += *src;
+}
+
+/*
+ * pstrinsert - insert string 'src' at beginning of string 'dst'
+ */
+static void pstrinsert(StringPtr dst, StringPtr src)
+{
+       /* make room for new string */
+       BlockMove(dst + 1, dst + *src + 1, *dst);
+
+       /* copy new string in */
+       BlockMove(src + 1, dst + 1, *src);
+
+       /* adjust length byte */
+       *dst += *src;
+}
+
+static void PathNameFromDirID(long dirID, short vRefNum, StringPtr fullPathName)
+{
+       CInfoPBRec      block;
+       Str255  directoryName;
+       OSErr   err;
+
+       fullPathName[0] = '\0';
+
+       block.dirInfo.ioDrParID = dirID;
+       block.dirInfo.ioNamePtr = directoryName;
+
+       while (1)
+       {
+               block.dirInfo.ioVRefNum = vRefNum;
+               block.dirInfo.ioFDirIndex = -1;
+               block.dirInfo.ioDrDirID = block.dirInfo.ioDrParID;
+               err = PBGetCatInfo(&block, FALSE);
+               pstrcat(directoryName, (StringPtr)"\p:");
+               pstrinsert(fullPathName, directoryName);
+               if (block.dirInfo.ioDrDirID == 2) break;
+       }
+}
+
+#endif
+#endif /* MACH_O_CARBON */
 
 /*
  * Activate a given window, if necessary
@@ -705,7 +982,11 @@ static void activate(WindowPtr w)
        if (active != w)
        {
                /* Activate */
+#if TARGET_API_MAC_CARBON
+               if (w) SetPortWindowPort(w);
+#else
                if (w) SetPort(w);
+#endif
 
                /* Remember */
                active = w;
@@ -755,7 +1036,6 @@ static void mac_warning(cptr warning)
  */
 static void term_data_color(term_data *td, int a)
 {
-#ifdef JP
        u16b rv, gv, bv;
 
        RGBColor color;
@@ -775,31 +1055,6 @@ static void term_data_color(term_data *td, int a)
 
        /* Memorize color */
        td->last = a;
-#else
-       /* Activate the color */
-       if (td->last != a)
-       {
-               u16b rv, gv, bv;
-
-               RGBColor color;
-
-               /* Extract the R,G,B data */
-               rv = angband_color_table[a][1];
-               gv = angband_color_table[a][2];
-               bv = angband_color_table[a][3];
-
-               /* Set the color */
-               color.red = (rv | (rv << 8));
-               color.green = (gv | (gv << 8));
-               color.blue = (bv | (bv << 8));
-       
-               /* Activate the color */
-               RGBForeColor(&color);
-
-               /* Memorize color */
-               td->last = a;
-       }
-#endif
 }
 
 #endif /* ANGBAND_LITE_MAC */
@@ -854,15 +1109,10 @@ static void term_data_check_font(term_data *td)
        td->tile_o_y = td->font_o_y;
 
        /* Set default tile size */
-#ifdef JP
        if( td->tile_wid == 0 && td->tile_hgt == 0 ){
                td->tile_wid = td->font_wid;
                td->tile_hgt = td->font_hgt;
        }
-#else
-       td->tile_wid = td->font_wid;
-       td->tile_hgt = td->font_hgt;
-#endif
 
        /* Re-activate the old window */
        activate(old);
@@ -874,9 +1124,27 @@ static void term_data_check_font(term_data *td)
  */
 static void term_data_check_size(term_data *td)
 {
+       BitMap          screen;
+       
+#if TARGET_API_MAC_CARBON
+       GetQDGlobalsScreenBits( &screen );
+#else
+       screen = qd.screenBits;
+#endif
        /* Minimal window size */
-       if (td->cols < 1) td->cols = 1;
-       if (td->rows < 1) td->rows = 1;
+       if (td == &data[0])
+       {
+               /* Enforce minimal size */
+               if (td->cols < 80) td->cols = 80;
+               if (td->rows < 24) td->rows = 24;
+       }
+
+       /* Allow small windows for the rest */
+       else
+       {
+               if (td->cols < 1) td->cols = 1;
+               if (td->rows < 1) td->rows = 1;
+       }
 
        /* Minimal tile size */
        if (td->tile_wid < 4) td->tile_wid = 4;
@@ -899,27 +1167,27 @@ static void term_data_check_size(term_data *td)
        td->size_hgt = td->rows * td->tile_hgt + td->size_oh1 + td->size_oh2;
 
        /* Verify the top */
-       if (td->r.top > qd.screenBits.bounds.bottom - td->size_hgt)
+       if (td->r.top > screen.bounds.bottom - td->size_hgt)
        {
-               td->r.top = qd.screenBits.bounds.bottom - td->size_hgt;
+               td->r.top = screen.bounds.bottom - td->size_hgt;
        }
 
        /* Verify the top */
-       if (td->r.top < qd.screenBits.bounds.top + 30)
+       if (td->r.top < screen.bounds.top + 30)
        {
-               td->r.top = qd.screenBits.bounds.top + 30;
+               td->r.top = screen.bounds.top + 30;
        }
 
        /* Verify the left */
-       if (td->r.left > qd.screenBits.bounds.right - td->size_wid)
+       if (td->r.left > screen.bounds.right - td->size_wid)
        {
-               td->r.left = qd.screenBits.bounds.right - td->size_wid;
+               td->r.left = screen.bounds.right - td->size_wid;
        }
 
        /* Verify the left */
-       if (td->r.left < qd.screenBits.bounds.left)
+       if (td->r.left < screen.bounds.left)
        {
-               td->r.left = qd.screenBits.bounds.left;
+               td->r.left = screen.bounds.left;
        }
 
        /* Calculate bottom right corner */
@@ -927,6 +1195,7 @@ static void term_data_check_size(term_data *td)
        td->r.bottom = td->r.top + td->size_hgt;
 
        /* Assume no graphics */
+       td->t->higher_pict = FALSE;
        td->t->always_pict = FALSE;
 
 #ifdef ANGBAND_LITE_MAC
@@ -936,9 +1205,13 @@ static void term_data_check_size(term_data *td)
 #else /* ANGBAND_LITE_MAC */
 
        /* Handle graphics */
-       if (use_graphics && (td == &data[0]))
+       if (use_graphics)
        {
-               td->t->always_pict = TRUE;
+               /* Use higher_pict whenever possible */
+               if (td->font_mono) td->t->higher_pict = TRUE;
+
+               /* Use always_pict only when necessary */
+               else td->t->always_pict = TRUE;
        }
 
 #endif /* ANGBAND_LITE_MAC */
@@ -946,14 +1219,14 @@ static void term_data_check_size(term_data *td)
        /* Fake mono-space */
        if (!td->font_mono ||
            (td->font_wid != td->tile_wid) ||
-           (td->font_hgt != td->tile_hgt))
+               (td->font_hgt != td->tile_hgt))
        {
-               /* Handle fake monospace */
+               /* Handle fake monospace -- this is SLOW */
+               if (td->t->higher_pict) td->t->higher_pict = FALSE;
                td->t->always_pict = TRUE;
        }
 }
 
-static OSErr XDDSWUpDateGWorldFromPict( term_data *td );
 /*
  * Hack -- resize a term_data
  *
@@ -963,10 +1236,6 @@ static void term_data_resize(term_data *td)
 {
        /* Actually resize the window */
        SizeWindow(td->w, td->size_wid, td->size_hgt, 0);
-       
-#ifdef JP
-               XDDSWUpDateGWorldFromPict( td );
-#endif
 }
 
 
@@ -991,9 +1260,19 @@ static void term_data_redraw(term_data *td)
 
        /* Restore the old term */
        Term_activate(old);
-
+       
        /* No need to redraw */
+#if TARGET_API_MAC_CARBON
+       {
+               RgnHandle               theRgn = NewRgn();
+               GetWindowRegion( td->w, kWindowContentRgn, theRgn );
+               ValidWindowRgn( (WindowRef)(td->w), theRgn );
+               DisposeRgn( theRgn );
+       }
+#else
        ValidRect(&td->w->portRect);
+#endif
+
 }
 
 
@@ -1007,16 +1286,44 @@ static void term_data_redraw(term_data *td)
 
 
 /*
- * Constants
+ * Graphics support
+ */
+
+/*
+ * PICT id of image tiles, set by Term_xtra_mac_react
+ */
+#ifdef MACH_O_CARBON
+static CFStringRef pictID = NULL;
+#else
+static int pictID = 0;
+#endif /* MACH_O_CARBON */
+
+/*
+ * Width and height of a tile in pixels
  */
+static int grafWidth = 0;
+static int grafHeight = 0;
 
-#define kPictID                                        1002                    /* Graf 'pict' resource */
+/*
+ * Numbers of rows and columns in tiles, calculated by
+ * the PICT loading code
+ */
+static int pictCols = 0;
+static int pictRows = 0;
 
-#define kGrafWidth                             16                              /* Graf Size (X) */
-#define kGrafHeight                            16                              /* Graf Size (Y) */
+/*
+ * Available graphics modes - 32x32 tiles don't work on Classic
+ */
+#define GRAF_MODE_NONE 0
+#define GRAF_MODE_8X8  1
+#define GRAF_MODE_16X16        2
+#define GRAF_MODE_32X32        3
 
-#define kPictCols                              32                              /* Number of Cols in Pict */
-#define kPictRows                              60                              /* Number of Rows in Pict */
+/*
+ * Current and requested graphics modes
+ */
+static int graf_mode = GRAF_MODE_NONE;
+static int graf_mode_req = GRAF_MODE_NONE;
 
 
 /*
@@ -1057,26 +1364,14 @@ static void BenSWLockFrame(FrameRec *srcFrameP)
        (void)LockPixels(pixMapH);
        HLockHi((Handle)pixMapH);
        srcFrameP->framePixHndl = pixMapH;
+#if TARGET_API_MAC_CARBON
+       srcFrameP->framePix = (PixMapPtr)*(Handle)pixMapH;
+#else
        srcFrameP->framePix = (PixMapPtr)StripAddress(*(Handle)pixMapH);
+#endif
        
 }
 
-#ifdef JP
-/*
- * Lock a frame
- */
-static void XDDSWLockFrame( term_data *td )
-{
-       PixMapHandle            pixMapH;
-
-       pixMapH = GetGWorldPixMap(td->bufferPort);
-       (void)LockPixels(pixMapH);
-       HLockHi((Handle)pixMapH);
-       td->bufferPixHndl = pixMapH;
-       td->bufferPix = (PixMapPtr)*(Handle)pixMapH;
-}
-#endif
-
 
 /*
  * Unlock a frame
@@ -1093,193 +1388,182 @@ static void BenSWUnlockFrame(FrameRec *srcFrameP)
        
 }
 
-#ifdef JP
+#ifdef MACH_O_CARBON
+
 /*
- * Unlock a frame
+ * Moving graphics resources into data fork -- pelpel
+ *
+ * (Carbon, Bundle)
+ * Given base and type names of a resource, find a file in the
+ * current application bundle and return its FSSpec in the third argument.
+ * Returns true on success, false otherwise.
+ * e.g. get_resource_spec(CFSTR("8x8"), CFSTR("png"), &spec);
  */
-static void XDDSWUnlockFrame( term_data *td )
+static Boolean get_resource_spec(
+       CFStringRef base_name, CFStringRef type_name, FSSpec *spec)
 {
-       if (td->bufferPort != NULL)
-       {
-               HUnlock((Handle)td->bufferPixHndl);
-               UnlockPixels(td->bufferPixHndl);
-       }
+       CFURLRef res_url;
+       FSRef ref;
+
+       /* Find resource (=file) in the current bundle */
+       res_url = CFBundleCopyResourceURL(
+               CFBundleGetMainBundle(), base_name, type_name, NULL);
+
+       /* Oops */
+       if (res_url == NULL) return (false);
+
+       /* Convert CFURL to FSRef */
+       (void)CFURLGetFSRef(res_url, &ref);
 
-       td->bufferPix = NULL;
+       /* Convert FSRef to FSSpec */
+       (void)FSGetCatalogInfo(&ref, kFSCatInfoNone, NULL, NULL, spec, NULL);
+
+       /* Free allocated CF data */
+       CFRelease(res_url);
+
+       /* Success */
+       return (true);
 }
-#endif
 
-static OSErr BenSWCreateGWorldFromPict(
-       GWorldPtr *pictGWorld,
-       PicHandle pictH)
+
+/*
+ * (QuickTime)
+ * Create a off-screen GWorld from contents of a file specified by a FSSpec.
+ * Based on BenSWCreateGWorldFromPict.
+ *
+ * Globals referenced: data[0], graf_height, graf_width
+ * Globals updated: pict_rows, pict_cols.
+ */
+static OSErr create_gworld_from_spec(
+       GWorldPtr *tile_gw, FSSpec *tile_spec)
 {
        OSErr err;
-       GWorldPtr saveGWorld;
-       GDHandle saveGDevice;
-       GWorldPtr tempGWorld;
-       Rect pictRect;
-       short depth;
-       GDHandle theGDH;
+       GraphicsImportComponent gi;
+       GWorldPtr gw, tmp_gw;
+       GDHandle gdh, tmp_gdh;
+       Rect r;
+       SInt16 depth;
 
-       /* Reset */
-       *pictGWorld = NULL;
+       /* See if QuickTime understands the file format */
+       err = GetGraphicsImporterForFile(tile_spec, &gi);
+
+       /* Oops */
+       if (err != noErr) return (err);
 
        /* Get depth */
        depth = data[0].pixelDepth;
 
        /* Get GDH */
-       theGDH = data[0].theGDH;
+       gdh = data[0].theGDH;
 
-       /* Obtain size rectangle */
-       pictRect = (**pictH).picFrame;
-       OffsetRect(&pictRect, -pictRect.left, -pictRect.top);
+       /* Retrieve the rect of the image */
+       err = GraphicsImportGetNaturalBounds(gi, &r);
+
+       /* Adjust it, so that the upper left corner becomes (0, 0) */
+       OffsetRect(&r, -r.left, -r.top);
+
+       /* Calculate and set numbers of rows and columns */
+       pictRows = r.bottom / grafHeight;
+       pictCols = r.right / grafWidth;
 
        /* Create a GWorld */
-       err = NewGWorld(&tempGWorld, depth, &pictRect, nil, 
-                                       theGDH, noNewDevice);
+       err = NewGWorld(&gw, depth, &r, NULL, gdh, noNewDevice);
 
-       /* Success */
-       if (err != noErr)
-       {
-               return (err);
-       }
+       /* Oops */
+       if (err != noErr) return (err);
 
-       /* Save pointer */
-       *pictGWorld = tempGWorld;
+       /* Save the pointer to the GWorld */
+       *tile_gw = gw;
 
-       /* Save GWorld */
-       GetGWorld(&saveGWorld, &saveGDevice);
+       /* Save the current GWorld */
+       GetGWorld(&tmp_gw, &tmp_gdh);
 
-       /* Activate */
-       SetGWorld(tempGWorld, nil);
+       /* Activate the newly created GWorld */
+       (void)GraphicsImportSetGWorld(gi, gw, NULL);
 
-       /* Dump the pict into the GWorld */
-       (void)LockPixels(GetGWorldPixMap(tempGWorld));
-       EraseRect(&pictRect);
-       DrawPicture(pictH, &pictRect);
-       UnlockPixels(GetGWorldPixMap(tempGWorld));
+       /* Prevent pixmap from moving while drawing */
+       (void)LockPixels(GetGWorldPixMap(gw));
+
+       /* Clear the pixels */
+       EraseRect(&r);
+
+       /* Draw the image into it */
+       (void)GraphicsImportDraw(gi);
+
+       /* Release the lock*/
+       UnlockPixels(GetGWorldPixMap(gw));
 
        /* Restore GWorld */
-       SetGWorld(saveGWorld, saveGDevice);
-       
+       SetGWorld(tmp_gw, tmp_gdh);
+
+       /* Close the image importer */
+       CloseComponent(gi);
+
        /* Success */
-       return (0);
+       return (noErr);
 }
 
-#ifdef JP
+#else /* MACH_O_CARBON */
 
-static OSErr XDDSWCreateGWorldFromPict(
+static OSErr BenSWCreateGWorldFromPict(
        GWorldPtr *pictGWorld,
-       term_data *td )
+       PicHandle pictH)
 {
        OSErr err;
-/*     GWorldPtr saveGWorld;
-       GDHandle saveGDevice;*/
+       GWorldPtr saveGWorld;
+       GDHandle saveGDevice;
        GWorldPtr tempGWorld;
        Rect pictRect;
-/*     short depth; */
-/*     GDHandle theGDH; */
-       
-       tempGWorld = NULL;
-       
+       short depth;
+       GDHandle theGDH;
+
        /* Reset */
        *pictGWorld = NULL;
 
        /* Get depth */
-/*     depth = td->pixelDepth; */
+       depth = data[0].pixelDepth;
 
        /* Get GDH */
-/*     theGDH = td->theGDH; */
+       theGDH = data[0].theGDH;
 
        /* Obtain size rectangle */
-       pictRect.left = 0;
-       pictRect.right = td->size_wid;
-       pictRect.top = 0;
-       pictRect.bottom = td->tile_hgt;
-       
+       pictRect = (**pictH).picFrame;
+       OffsetRect(&pictRect, -pictRect.left, -pictRect.top);
+
        /* Create a GWorld */
-       err = NewGWorld(&tempGWorld, 0, &pictRect, 0, 0, 0);
-       
+       err = NewGWorld(&tempGWorld, depth, &pictRect, nil, 
+                                       theGDH, noNewDevice);
+
        /* Success */
        if (err != noErr)
        {
                return (err);
        }
-       
+
        /* Save pointer */
        *pictGWorld = tempGWorld;
-       
+
        /* Save GWorld */
-/*     GetGWorld(&saveGWorld, &saveGDevice); */
+       GetGWorld(&saveGWorld, &saveGDevice);
 
        /* Activate */
-/*     SetGWorld(tempGWorld, nil); */
+       SetGWorld(tempGWorld, nil);
 
        /* Dump the pict into the GWorld */
-/*     (void)LockPixels(GetGWorldPixMap(tempGWorld)); */
-/*     EraseRect(&pictRect); */
-/*     DrawPicture(pictH, &pictRect); */
-/*     UnlockPixels(GetGWorldPixMap(tempGWorld)); */
+       (void)LockPixels(GetGWorldPixMap(tempGWorld));
+       EraseRect(&pictRect);
+       DrawPicture(pictH, &pictRect);
+       UnlockPixels(GetGWorldPixMap(tempGWorld));
 
        /* Restore GWorld */
-/*     SetGWorld(saveGWorld, saveGDevice); */
+       SetGWorld(saveGWorld, saveGDevice);
        
+       /* Success */
        return (0);
 }
 
+#endif /* MACH_O_CARBON */
 
-static OSErr XDDSWUpDateGWorldFromPict( term_data *td )
-{
-/*     GWorldPtr saveGWorld; */
-/*     GDHandle saveGDevice; */
-       Rect pictRect;
-/*     short depth; */
-/*     GDHandle theGDH; */
-       
-       GWorldFlags     errflag;
-       
-       /*  */
-       
-       if( td->bufferPort == NULL )
-               return;
-       /* Get depth */
-/*     depth = td->pixelDepth; */
-
-       /* Get GDH */
-/*     theGDH = td->theGDH; */
-       
-       /* Obtain size rectangle */
-       pictRect.top = 0;
-       pictRect.left = 0;
-       pictRect.right = td->size_wid;
-       pictRect.bottom = td->tile_hgt;
-       
-       XDDSWUnlockFrame(td);
-       
-       errflag = UpdateGWorld( &td->bufferPort, 0, &pictRect, 0, 0, 0);
-       XDDSWLockFrame(td);
-       if( errflag & gwFlagErr ){
-               //SysBeep(0);
-               return;
-       }
-       
-       /* Save GWorld */
-/*     GetGWorld(&saveGWorld, &saveGDevice); */
-
-       /* Activate */
-/*     SetGWorld(td->bufferPort, nil); */
-
-       /* Dump the pict into the GWorld */
-/*     (void)LockPixels(GetGWorldPixMap(td->bufferPort)); */
-/*     EraseRect(&td->bufferPort->portRect); */
-       
-/*     UnlockPixels(GetGWorldPixMap(td->bufferPort)); */
-
-       /* Restore GWorld */
-/*     SetGWorld(saveGWorld, saveGDevice); */
-       
-}
-#endif
 
 /*
  * Init the global "frameP"
@@ -1291,14 +1575,54 @@ static errr globe_init(void)
        
        GWorldPtr tempPictGWorldP;
 
+#ifdef MACH_O_CARBON
+       FSSpec pict_spec;
+#else
        PicHandle newPictH;
+#endif /* MACH_O_CARBON */
 
        /* Use window XXX XXX XXX */
+#if TARGET_API_MAC_CARBON
+       SetPortWindowPort(data[0].w);
+#else
        SetPort(data[0].w);
+#endif
+
+
+#ifdef MACH_O_CARBON
+
+       /* Get the tile resources */
+       if (!get_resource_spec(pictID, CFSTR("png"), &pict_spec)) return (-1);
+
+       /* Create GWorld */
+       err = create_gworld_from_spec(&tempPictGWorldP, &pict_spec);
+
+       /* Error */
+       if (err != noErr) return (err);
 
+       /* Create the frame */
+       frameP = (FrameRec*)NewPtrClear((Size)sizeof(FrameRec));
+
+       /* Analyze result */
+       if (frameP == NULL)
+       {
+               /* Dispose of image GWorld */
+               DisposeGWorld(tempPictGWorldP);
+
+               /* Fake error code */
+               return (-1);
+       }
+
+       /* Save GWorld */
+       frameP->framePort = tempPictGWorldP;
+
+       /* Lock it */
+       BenSWLockFrame(frameP);
+
+#else /* MACH_O_CARBON */
 
        /* Get the pict resource */
-       newPictH = GetPicture(kPictID);
+       newPictH = GetPicture(pictID);
 
        /* Analyze result */
        err = (newPictH ? 0 : -1);
@@ -1333,6 +1657,8 @@ static errr globe_init(void)
                        }
                }
        }
+#endif /* MACH_O_CARBON */
+
 
        /* Result */
        return (err);
@@ -1368,6 +1694,556 @@ static errr globe_nuke(void)
 }
 
 
+# ifdef USE_ASYNC_SOUND
+
+/*
+ * Asynchronous sound player revised
+ */
+#if defined(USE_QT_SOUND) && !defined(MACH_O_CARBON)
+# undef USE_QT_SOUND
+#endif /* USE_QT_SOUND && !MACH_O_CARBON */
+
+
+/*
+ * Number of channels in the channel pool
+ */
+#if TARGET_API_MAC_CARBON
+#define MAX_CHANNELS           8
+#else
+#define MAX_CHANNELS           4
+#endif
+
+/*
+ * A pool of sound channels
+ */
+static SndChannelPtr channels[MAX_CHANNELS];
+
+/*
+ * Status of the channel pool
+ */
+static Boolean channel_initialised = FALSE;
+
+/*
+ * Data handles containing sound samples
+ */
+static SndListHandle samples[SOUND_MAX];
+
+/*
+ * Reference counts of sound samples
+ */
+static SInt16 sample_refs[SOUND_MAX];
+
+
+/*
+ * Sound effects
+ *
+ * These constants aren't used by the program at the moment.
+ */
+#define SOUND_VOLUME_MIN       0       /* Default minimum sound volume */
+#define SOUND_VOLUME_MAX       255     /* Default maximum sound volume */
+#define VOLUME_MIN                     0       /* Minimum sound volume in % */
+#define VOLUME_MAX                     100     /* Maximum sound volume in % */
+#define VOLUME_INC                     5       /* Increment sound volume in % */
+
+/* I'm just too lazy to write a panel for this XXX XXX */
+static SInt16 sound_volume = SOUND_VOLUME_MAX;
+
+#ifdef USE_QT_SOUND
+
+/*
+ * QuickTime sound, by Ron Anderson
+ *
+ * I didn't choose to use Windows-style .ini files (Ron wrote a parser
+ * for it, but...), nor did I use lib/xtra directory, hoping someone
+ * would code plist-based configuration code in the future -- pelpel
+ */
+
+/*
+ * (QuickTime)
+ * Load sound effects from data-fork resources.  They are wav files
+ * with the same names as angband_sound_name[] (variable.c)
+ *
+ * Globals referenced: angband_sound_name[]
+ * Globals updated: samples[] (they can be *huge*)
+ */
+static void load_sounds(void)
+{
+       OSErr err;
+       int i;
+
+       /* Start QuickTime */
+       err = EnterMovies();
+
+       /* Error */
+       if (err != noErr) return;
+
+       /*
+        * This loop may take a while depending on the count and size of samples
+        * to load.
+        *
+        * We should use a progress dialog for this.
+        */
+       for (i = 1; i < SOUND_MAX; i++)
+       {
+               /* Apple APIs always give me headacke :( */
+               CFStringRef name;
+               FSSpec spec;
+               SInt16 file_id;
+               SInt16 res_id;
+               Str255 movie_name;
+               Movie movie;
+               Track track;
+               Handle h;
+               Boolean res;
+
+               /* Allocate CFString with the name of sound event to be processed */
+               name = CFStringCreateWithCString(NULL, angband_sound_name[i],
+                       kTextEncodingUS_ASCII);
+
+               /* Error */
+               if (name == NULL) continue;
+
+               /* Find sound sample resource with the same name */
+               res = get_resource_spec(name, CFSTR("wav"), &spec);
+
+               /* Free the reference to CFString */
+               CFRelease(name);
+
+               /* Error */
+               if (!res) continue;
+
+               /* Open the sound file */
+               err = OpenMovieFile(&spec, &file_id, fsRdPerm);
+
+               /* Error */
+               if (err != noErr) continue;
+
+               /* Create Movie from the file */
+               err = NewMovieFromFile(&movie, file_id, &res_id, movie_name,
+                       newMovieActive, NULL);
+
+               /* Error */
+               if (err != noErr) goto close_file;
+
+               /* Get the first track of the movie */
+               track = GetMovieIndTrackType(movie, 1, AudioMediaCharacteristic,
+                       movieTrackCharacteristic | movieTrackEnabledOnly );
+
+               /* Error */
+               if (track == NULL) goto close_movie;
+
+               /* Allocate a handle to store sample */
+               h = NewHandle(0);
+
+               /* Error */
+               if (h == NULL) goto close_track;
+
+               /* Dump the sample into the handle */
+               err = PutMovieIntoTypedHandle(movie, track, soundListRsrc, h, 0,
+                       GetTrackDuration(track), 0L, NULL);
+
+               /* Success */
+               if (err == noErr)
+               {
+                       /* Store the handle in the sample list */
+                       samples[i] = (SndListHandle)h;
+               }
+
+               /* Failure */
+               else
+               {
+                       /* Free unused handle */
+                       DisposeHandle(h);
+               }
+
+               /* Free the track */
+               close_track: DisposeMovieTrack(track);
+
+               /* Free the movie */
+               close_movie: DisposeMovie(movie);
+
+               /* Close the movie file */
+               close_file: CloseMovieFile(file_id);
+       }
+
+       /* Stop QuickTime */
+       ExitMovies();
+}
+
+#else /* USE_QT_SOUND */
+
+/*
+ * Return a handle of 'snd ' resource given Angband sound event number,
+ * or NULL if it isn't found.
+ *
+ * Globals referenced: angband_sound_name[] (variable.c)
+ */
+static SndListHandle find_sound(int num)
+{
+       Str255 sound;
+
+       /* Get the proper sound name */
+       strnfmt((char*)sound + 1, 255, "%.16s.wav", angband_sound_name[num]);
+       sound[0] = strlen((char*)sound + 1);
+
+       /* Obtain resource XXX XXX XXX */
+       return ((SndListHandle)GetNamedResource('snd ', sound));
+}
+
+#endif /* USE_QT_SOUND */
+
+
+/*
+ * Clean up sound support - to be called when the game exits.
+ *
+ * Globals referenced: channels[], samples[], sample_refs[].
+ */
+static void cleanup_sound(void)
+{
+       int i;
+
+       /* No need to clean it up */
+       if (!channel_initialised) return;
+
+       /* Dispose channels */
+       for (i = 0; i < MAX_CHANNELS; i++)
+       {
+               /* Drain sound commands and free the channel */
+               SndDisposeChannel(channels[i], TRUE);
+       }
+
+       /* Free sound data */
+       for (i = 1; i < SOUND_MAX; i++)
+       {
+               /* Still locked */
+               if ((sample_refs[i] > 0) && (samples[i] != NULL))
+               {
+                       /* Unlock it */
+                       HUnlock((Handle)samples[i]);
+               }
+
+#ifndef USE_QT_SOUND
+
+               /* Release it */
+               if (samples[i]) ReleaseResource((Handle)samples[i]);
+#else
+
+               /* Free handle */
+               if (samples[i]) DisposeHandle((Handle)samples[i]);
+#endif /* !USE_QT_SOUND */
+       }
+}
+
+
+/*
+ * Play sound effects asynchronously -- pelpel
+ *
+ * I don't believe those who first started using the previous implementations
+ * imagined this is *much* more complicated as it may seem.  Anyway, 
+ * introduced round-robin scheduling of channels and made it much more
+ * paranoid about HLock/HUnlock.
+ *
+ * XXX XXX de-refcounting, HUnlock and ReleaseResource should be done
+ * using channel's callback procedures, which set global flags, and
+ * a procedure hooked into CheckEvents does housekeeping.  On the other
+ * hand, this lazy reclaiming strategy keeps things simple (no interrupt
+ * time code) and provides a sort of cache for sound data.
+ *
+ * Globals referenced: channel_initialised, channels[], samples[],
+ *   sample_refs[].
+ * Globals updated: ditto.
+ */
+static void play_sound(int num, SInt16 vol)
+{
+       OSErr err;
+       int i;
+       int prev_num;
+       SndListHandle h;
+       SndChannelPtr chan;
+       SCStatus status;
+
+       static int next_chan;
+       static SInt16 channel_occupants[MAX_CHANNELS];
+       static SndCommand volume_cmd, quiet_cmd;
+
+
+       /* Initialise sound channels */
+       if (!channel_initialised)
+       {
+               for (i = 0; i < MAX_CHANNELS; i++)
+               {
+                       /* Paranoia - Clear occupant table */
+                       /* channel_occupants[i] = 0; */
+
+                       /* Create sound channel for all sounds to play from */
+                       err = SndNewChannel(&channels[i], sampledSynth, initMono, 0L);
+
+                       /* Error */
+                       if (err != noErr)
+                       {
+                               /* Free channels */
+                               while (--i >= 0)
+                               {
+                                       SndDisposeChannel(channels[i], TRUE);
+                               }
+
+                               /* Notify error */
+                               plog(_("サウンドチャンネルを初期化出来ません!", "Cannot initialise sound channels!"));
+
+                               /* Cancel request */
+                               use_sound = arg_sound = FALSE;
+
+                               /* Failure */
+                               return;
+                       }
+               }
+
+               /* First channel to use */
+               next_chan = 0;
+
+               /* Prepare volume command */
+               volume_cmd.cmd = volumeCmd;
+               volume_cmd.param1 = 0;
+               volume_cmd.param2 = 0;
+
+               /* Prepare quiet command */
+               quiet_cmd.cmd = quietCmd;
+               quiet_cmd.param1 = 0;
+               quiet_cmd.param2 = 0;
+
+               /* Initialisation complete */
+               channel_initialised = TRUE;
+       }
+
+       /* Paranoia */
+       if ((num <= 0) || (num >= SOUND_MAX)) return;
+
+       /* Prepare volume command */
+       volume_cmd.param2 = ((SInt32)vol << 16) | vol;
+
+       /* Channel to use (round robin) */
+       chan = channels[next_chan];
+
+       /* See if the resource is already in use */
+       if (sample_refs[num] > 0)
+       {
+               /* Resource in use */
+               h = samples[num];
+
+               /* Increase the refcount */
+               sample_refs[num]++;
+       }
+
+       /* Sound is not currently in use */
+       else
+       {
+               /* Get handle for the sound */
+#ifdef USE_QT_SOUND
+               h = samples[num];
+#else
+               h = find_sound(num);
+#endif /* USE_QT_SOUND */
+
+               /* Sample not available */
+               if (h == NULL) return;
+
+#ifndef USE_QT_SOUND
+
+               /* Load resource */
+               LoadResource((Handle)h);
+
+               /* Remember it */
+               samples[num] = h;
+
+#endif /* !USE_QT_SOUND */
+
+               /* Lock the handle */
+               HLockHi((Handle)h);
+
+               /* Initialise refcount */
+               sample_refs[num] = 1;
+       }
+
+       /* Poll the channel */
+       err = SndChannelStatus(chan, sizeof(SCStatus), &status);
+
+       /* It isn't available */
+       if ((err != noErr) || status.scChannelBusy)
+       {
+               /* Shut it down */
+               SndDoImmediate(chan, &quiet_cmd);
+       }
+
+       /* Previously played sound on this channel */
+       prev_num = channel_occupants[next_chan];
+
+       /* Process previously played sound */
+       if (prev_num != 0)
+       {
+               /* Decrease refcount */
+               sample_refs[prev_num]--;
+
+               /* We can free it now */
+               if (sample_refs[prev_num] <= 0)
+               {
+                       /* Unlock */
+                       HUnlock((Handle)samples[prev_num]);
+
+#ifndef USE_QT_SOUND
+
+                       /* Release */
+                       ReleaseResource((Handle)samples[prev_num]);
+
+                       /* Forget handle */
+                       samples[prev_num] = NULL;
+
+#endif /* !USE_QT_SOUND */
+
+                       /* Paranoia */
+                       sample_refs[prev_num] = 0;
+               }
+       }
+
+       /* Remember this sound as the current occupant of the channel */
+       channel_occupants[next_chan] = num;
+
+       /* Set up volume for channel */
+       SndDoImmediate(chan, &volume_cmd);
+
+       /* Play new sound asynchronously */
+       SndPlay(chan, h, TRUE);
+
+       /* Schedule next channel (round robin) */
+       next_chan++;
+       if (next_chan >= MAX_CHANNELS) next_chan = 0;
+}
+
+# else /* USE_ASYNC_SOUND */
+
+/*
+ * Play sound synchronously
+ *
+ * This may not be your choice, but much safer and much less resource hungry.
+ */
+static void play_sound(int num, SInt16 vol)
+{
+       Handle handle;
+       Str255 sound;
+
+       /* Get the proper sound name */
+       strnfmt((char*)sound + 1, 255, "%.16s.wav", angband_sound_name[num]);
+       sound[0] = strlen((char*)sound + 1);
+
+       /* Obtain resource XXX XXX XXX */
+       handle = GetNamedResource('snd ', sound);
+
+       /* Oops */
+       if (handle == NULL) return;
+
+       /* Load and Lock */
+       LoadResource(handle);
+       HLockHi(handle);
+
+       /* Play sound (wait for completion) */
+       SndPlay(NULL, (SndListHandle)handle, FALSE);
+
+       /* Unlock and release */
+       HUnlock(handle);
+       ReleaseResource(handle);
+}
+
+# endif /* USE_ASYNC_SOUND */
+
+/*
+       Extra Sound Mode
+*/
+
+
+static short soundmode[8];
+
+#define                SND_NON         0
+#define                SND_ATTACK      1
+#define                SND_MOVE                2
+#define                SND_TRAP                3
+#define                SND_SHOP                4
+#define                SND_ME          5
+#define                SND_CMD_ERROR   6
+
+#ifndef MACH_O_CARBON
+
+static int soundchoice[] = {
+       SND_NON,
+       SND_ATTACK,
+       SND_ATTACK,
+       SND_ATTACK,
+       SND_TRAP,
+       SND_ATTACK,
+       SND_ME,
+       SND_ME,
+       SND_ME,
+       SND_MOVE,
+       SND_ATTACK,
+       SND_ME,
+       SND_ATTACK,
+       SND_NON,
+       SND_MOVE,
+       SND_MOVE,
+       SND_ME,
+       SND_SHOP,
+       SND_SHOP,
+       SND_SHOP,
+       SND_SHOP,
+       SND_MOVE,
+       SND_MOVE,
+       SND_MOVE,
+       SND_MOVE,
+       SND_ATTACK,
+       SND_SHOP,
+       SND_SHOP,
+       SND_ME,
+       SND_NON,
+       SND_ATTACK,
+       SND_NON,
+       SND_NON,
+       SND_NON,
+       SND_NON,
+       SND_ATTACK,
+       SND_ATTACK,
+       SND_NON,
+       SND_NON,
+       SND_ATTACK,
+       SND_NON,
+       SND_NON,
+       SND_NON,
+       SND_TRAP,
+       SND_ATTACK,
+       SND_ATTACK,
+       SND_ATTACK,
+       SND_ATTACK,
+       SND_ATTACK,
+       SND_NON,
+       SND_NON,
+       SND_NON,
+       SND_NON,
+       SND_NON,
+       SND_CMD_ERROR,
+       SND_TRAP,
+       SND_NON,
+       SND_NON,
+       SND_TRAP,
+       SND_ATTACK,
+       SND_TRAP,
+       SND_ATTACK,
+       SND_ATTACK,
+       SND_NON,
+       SND_TRAP,
+};
+
+static int ext_sound = 0;
+static int ext_graf = 0;
+
+#endif /* !MACH_O_CARBON */
+
 #endif /* ANGBAND_LITE_MAC */
 
 
@@ -1429,7 +2305,6 @@ static void Term_init_mac(term *t)
 
        /* Block */
        {
-               Rect tempRect;
                Rect globalRect;
                GDHandle mainGDH;
                GDHandle currentGDH;
@@ -1437,12 +2312,13 @@ static void Term_init_mac(term *t)
                PixMapHandle basePixMap;
 
                /* Obtain the rect */
-               tempRect = td->w->portRect;
-
-               /* Obtain the global rect */    
-               globalRect = tempRect;
+#if TARGET_API_MAC_CARBON
+               GetWindowBounds( (WindowRef)td->w, kWindowContentRgn, &globalRect );
+#else
+               globalRect = td->w->portRect;
                LocalToGlobal((Point*)&globalRect.top);
                LocalToGlobal((Point*)&globalRect.bottom);
+#endif
 
                /* Obtain the proper GDH */
                mainGDH = GetMaxDevice(&globalRect);
@@ -1468,33 +2344,42 @@ static void Term_init_mac(term *t)
 
 #endif /* ANGBAND_LITE_MAC */
 
-       /* Clip to the window */
-       ClipRect(&td->w->portRect);
+       {
+               Rect            portRect;
 
-       /* Erase the window */
-       EraseRect(&td->w->portRect);
+#if TARGET_API_MAC_CARBON
+               GetWindowBounds( (WindowRef)td->w, kWindowContentRgn, &portRect );
+               global_to_local( &portRect );
+#else
+               portRect = td->w->portRect;
+#endif
+               /* Clip to the window */
+               ClipRect(&portRect);
 
-       /* Invalidate the window */
-       InvalRect(&td->w->portRect);
+               /* Erase the window */
+               EraseRect(&portRect);
 
-       /* Display the window if needed */
-       if (td->mapped) ShowWindow(td->w);
+               /* Invalidate the window */
+#if TARGET_API_MAC_CARBON
+               InvalWindowRect((WindowRef)(td->w), (const Rect *)(&portRect));
+#else
+               InvalRect(&portRect);
+#endif
 
-       /* Hack -- set "mapped" flag */
-       t->mapped_flag = td->mapped;
+               /* Display the window if needed */
+               if (td->mapped) ShowWindow(td->w);
 
-       /* Forget color */
-       td->last = -1;
-#ifdef JP
-       XDDSWCreateGWorldFromPict( &td->bufferPort, td );
-       
-       XDDSWLockFrame( td );
+               /* Hack -- set "mapped" flag */
+               t->mapped_flag = td->mapped;
+
+               /* Forget color */
+               td->last = -1;
+       }
        /* Oops */
 /*     if (err == noErr)
        {
                
        }*/
-#endif
 }
 
 
@@ -1550,23 +2435,74 @@ static errr Term_xtra_mac_react(void)
                use_sound = arg_sound;
        }
 
+       
        /* Handle graphics */
-       if ((td == &data[0]) && (use_graphics != arg_graphics))
+       if (graf_mode_req != graf_mode)
        {
-               /* Initialize graphics */
+               /* dispose old GWorld's if present */
+               globe_nuke();
 
-               if (!use_graphics && !frameP && (globe_init() != 0))
+               /* Setup parameters according to request */
+               switch (graf_mode_req)
                {
-                       #ifdef JP
-                       plog("¥°¥é¥Õ¥£¥Ã¥¯¤Î½é´ü²½¤Ï½ÐÍè¤Þ¤»¤ó¤Ç¤·¤¿.");
-                       #else
-                       plog("Cannot initialize graphics!");
-                       #endif
-                       arg_graphics = FALSE;
+                       /* ASCII - no graphics whatsoever */
+                       case GRAF_MODE_NONE:
+                       {
+                               use_graphics = arg_graphics = GRAPHICS_NONE;
+                               break;
+                       }
+
+                       /*
+                        * 8x8 tiles (PICT id 1001)
+                        * no transparency effect
+                        * "old" graphics definitions
+                        */
+                       case GRAF_MODE_8X8:
+                       {
+                               use_graphics = arg_graphics = GRAPHICS_ORIGINAL;
+                               ANGBAND_GRAF = "old";
+#ifdef MACH_O_CARBON
+                               pictID = CFSTR("8x8");
+#else
+                               pictID = 1001;
+#endif /* MACH_O_CARBON */
+                               grafWidth = grafHeight = 8;
+                               break;
+                       }
+
+                       /*
+                        * 16x16 tiles (images: PICT id 1002)
+                        * with transparency effect
+                        * "new" graphics definitions
+                        */
+                       case GRAF_MODE_16X16:
+                       {
+                               use_graphics = arg_graphics = GRAPHICS_ADAM_BOLT;
+                               ANGBAND_GRAF = "new";
+#ifdef MACH_O_CARBON
+                               pictID = CFSTR("16x16");
+#else
+                               pictID = 1002;
+#endif /* MACH_O_CARBON */
+                               grafWidth = grafHeight = 16;
+                               break;
+                       }
                }
 
-               /* Apply request */
-               use_graphics = arg_graphics;
+               if ((graf_mode_req != GRAF_MODE_NONE) && !frameP && (globe_init() != 0))
+               {
+                       plog(_("グラフィックの初期化は出来ませんでした.", "Cannot initialize graphics!"));
+
+                       /* reject request */
+                       graf_mode_req = GRAF_MODE_NONE;
+
+                       /* reset graphics flags */
+                       use_graphics = arg_graphics = FALSE;
+
+               }
+
+               /* update current graphics mode */
+               graf_mode = graf_mode_req;
 
                /* Apply and Verify */
                term_data_check_size(td);
@@ -1616,65 +2552,9 @@ static errr Term_xtra_mac(int n, int v)
                /* Make a sound */
                case TERM_XTRA_SOUND:
                {
-                       Handle handle;
-
-                       Str255 sound;
-
-#if 0
-                       short oldResFile;
-                       short newResFile;
-
-                       /* Open the resource file */
-                       oldResFile = CurResFile();
-                       newResFile = OpenResFile(sound);
+                       /* Play sound */
+                       play_sound(v, sound_volume);
 
-                       /* Close the resource file */
-                       CloseResFile(newResFile);
-                       UseResFile(oldResFile);
-#endif
-
-                       /* Get the proper sound name */
-                       sprintf((char*)sound + 1, "%.16s.wav", angband_sound_name[v]);
-                       sound[0] = strlen((char*)sound + 1);
-
-                       /* Obtain resource XXX XXX XXX */
-#ifdef JP
-                       handle = Get1NamedResource('snd ', sound);
-                       if( handle == NULL || ext_sound )
-                               handle = GetNamedResource('snd ', sound);
-                       
-                       /* Oops */
-                       if (handle && soundmode[soundchoice[v]] == true)
-                       {
-                               /* Load and Lock */
-                               LoadResource(handle);
-                               HLock(handle);
-
-                               /* Play sound (wait for completion) */
-                               SndPlay(nil, (SndListHandle)handle, true);
-
-                               /* Unlock and release */
-                               HUnlock(handle);
-                               ReleaseResource(handle);
-                       }
-#else
-                       handle = GetNamedResource('snd ', sound);
-
-                       /* Oops */
-                       if (handle)
-                       {
-                               /* Load and Lock */
-                               LoadResource(handle);
-                               HLock(handle);
-
-                               /* Play sound (wait for completion) */
-                               SndPlay(nil, (SndListHandle)handle, true);
-
-                               /* Unlock and release */
-                               HUnlock(handle);
-                               ReleaseResource(handle);
-                       }
-#endif
                        /* Success */
                        return (0);
                }
@@ -1685,7 +2565,7 @@ static errr Term_xtra_mac(int n, int v)
                case TERM_XTRA_BORED:
                {
                        /* Process an event */
-                       (void)CheckEvents(0);
+                       (void)CheckEvents(FALSE);
 
                        /* Success */
                        return (0);
@@ -1724,11 +2604,20 @@ static errr Term_xtra_mac(int n, int v)
                /* Clear the screen */
                case TERM_XTRA_CLEAR:
                {
+                       Rect            portRect;
+                       
+#if TARGET_API_MAC_CARBON
+                       GetWindowBounds( (WindowRef)td->w, kWindowContentRgn, &portRect );
+                       global_to_local( &portRect );
+#else
+                       portRect = td->w->portRect;
+#endif
+
                        /* No clipping XXX XXX XXX */
-                       ClipRect(&td->w->portRect);
+                       ClipRect(&portRect);
 
                        /* Erase the window */
-                       EraseRect(&td->w->portRect);
+                       EraseRect(&portRect);
 
                        /* Set the color */
                        term_data_color(td, TERM_WHITE);
@@ -1740,10 +2629,10 @@ static errr Term_xtra_mac(int n, int v)
                        LineTo(td->size_wid-1, 0);
 
                        /* Clip to the new size */
-                       r.left = td->w->portRect.left + td->size_ow1;
-                       r.top = td->w->portRect.top + td->size_oh1;
-                       r.right = td->w->portRect.right - td->size_ow2;
-                       r.bottom = td->w->portRect.bottom - td->size_oh2;
+                       r.left = portRect.left + td->size_ow1;
+                       r.top = portRect.top + td->size_oh1;
+                       r.right = portRect.right - td->size_ow2;
+                       r.bottom = portRect.bottom - td->size_oh2;
                        ClipRect(&r);
 
                        /* Success */
@@ -1763,10 +2652,27 @@ static errr Term_xtra_mac(int n, int v)
                        /* If needed */
                        if (v > 0)
                        {
+#if TARGET_API_MAC_CARBON
+                               EventRecord tmp;
+                               UInt32 ticks;
+
+                               /* Convert millisecs to ticks */
+                               ticks = (v * 60L) / 1000;
+
+                               /*
+                                * Hack? - Put the programme into sleep.
+                                * No events match ~everyEvent, so nothing
+                                * should be lost in Angband's event queue.
+                                * Even if ticks are 0, it's worth calling for
+                                * the above mentioned reasons.
+                                */
+                               WaitNextEvent((EventMask)~everyEvent, &tmp, ticks, nil);
+#else
                                long m = TickCount() + (v * 60L) / 1000;
 
                                /* Wait for it */
                                while (TickCount() < m) /* loop */;
+#endif
                        }
 
                        /* Success */
@@ -1800,6 +2706,35 @@ static errr Term_curs_mac(int x, int y)
        r.right = r.left + td->tile_wid;
        r.top = y * td->tile_hgt + td->size_oh1;
        r.bottom = r.top + td->tile_hgt;
+
+       FrameRect(&r);
+
+       /* Success */
+       return (0);
+}
+
+
+/*
+ * Low level graphics (Assumes valid input).
+ * Draw a "big cursor" at (x,y), using a "yellow box".
+ * We are allowed to use "Term_grab()" to determine
+ * the current screen contents (for inverting, etc).
+ */
+static errr Term_bigcurs_mac(int x, int y)
+{
+       Rect r;
+
+       term_data *td = (term_data*)(Term->data);
+
+       /* Set the color */
+       term_data_color(td, TERM_YELLOW);
+
+       /* Frame the grid */
+       r.left = x * td->tile_wid + td->size_ow1;
+       r.right = r.left + 2 * td->tile_wid;
+       r.top = y * td->tile_hgt + td->size_oh1;
+       r.bottom = r.top + td->tile_hgt;
+
        FrameRect(&r);
 
        /* Success */
@@ -1867,37 +2802,25 @@ static errr Term_text_mac(int x, int y, int n, byte a, const char *cp)
  *
  * Erase "n" characters starting at (x,y)
  */
-static errr Term_pict_mac(int x, int y, int n, const byte *ap, const char *cp)
+static errr Term_pict_mac(int x, int y, int n, const byte *ap, const char *cp,
+                         const byte *tap, const char *tcp)
 {
        int i;
        Rect r2;
-       bool use_buffer = false;
        term_data *td = (term_data*)(Term->data);
        GDHandle saveGDevice;
        GWorldPtr saveGWorld;
        
-#ifdef JP
-       PixMapHandle PortPix;
-       
-#endif
-
        /* Save GWorld */
        GetGWorld(&saveGWorld, &saveGDevice);
        
-       if( n > 0 )
-       {
-               /* Destination rectangle */
-               r2.left = x * td->tile_wid + td->size_ow1;
-               r2.right = r2.left + td->tile_wid;
-               r2.top = 0;
-               r2.bottom = r2.top + td->tile_hgt;
+       r2.left = x * td->tile_wid + td->size_ow1;
+       r2.right = r2.left + td->tile_wid;
+       r2.top = y * td->tile_hgt + td->size_oh1;
+       r2.bottom = r2.top + td->tile_hgt;
        
-               /* Activate */
-               SetGWorld(td->bufferPort, nil);
-#ifdef JP
-               PortPix = GetGWorldPixMap(td->bufferPort );
-               LockPixels( PortPix );
-#endif
+       if( n > 1 )
+       {
                /* Instantiate font */
                TextFont(td->font_id);
                TextSize(td->font_size);
@@ -1906,25 +2829,14 @@ static errr Term_pict_mac(int x, int y, int n, const byte *ap, const char *cp)
                /* Restore colors */
                BackColor(blackColor);
                ForeColor(whiteColor);
-
-#ifdef JP
-               /* Erase */
-               EraseRect(&td->bufferPort->portRect);
-#endif
-
-               use_buffer = true;
        }
        else
        {
                /* Destination rectangle */
-               r2.left = x * td->tile_wid + td->size_ow1;
-               r2.right = r2.left + td->tile_wid;
+/*             r2.left = x * td->tile_wid + td->size_ow1;
                r2.top = y * td->tile_hgt + td->size_oh1;
-               r2.bottom = r2.top + td->tile_hgt;
+               r2.bottom = r2.top + td->tile_hgt;*/
                
-               /* no buffering, so we use the normal current port */
-               
-               use_buffer = false;
        }
                
        /* Scan the input */
@@ -1935,52 +2847,120 @@ static errr Term_pict_mac(int x, int y, int n, const byte *ap, const char *cp)
                byte a = ap[i];
                char c = cp[i];
 
+               /* Second byte of bigtile */
+               if (use_bigtile && a == 255)
+               {
+                       /* Advance */
+                       r2.left += td->tile_wid;
+
+                       continue;
+               }
+
+               /* Prepare right of rectangle now */
+               r2.right = r2.left + td->tile_wid;
+
 #ifdef ANGBAND_LITE_MAC
 
-               /* Nothing */
+               /* No graphics */
 
 #else /* ANGBAND_LITE_MAC */
 
                /* Graphics -- if Available and Needed */
-               if (use_graphics && ((td == &data[0]) || (td == &data[6])) &&
-                   ((byte)a & 0x80) && ((byte)c & 0x80))
+               if (use_graphics && ((byte)a & 0x80) && ((byte)c & 0x80))
                {
+#if TARGET_API_MAC_CARBON
+                       PixMapHandle    srcBitMap = GetGWorldPixMap(frameP->framePort);
+                       PixMapHandle    destBitMap;
+#else
+                       BitMapPtr srcBitMap = (BitMapPtr)(frameP->framePix);
+                       BitMapPtr destBitMap;
+#endif
+                               
                        int col, row;
                        Rect r1;
 
+                       Rect terrain_r;
+                       bool terrain_flag = FALSE;
+                       byte ta = tap[i];
+                       char tc = tcp[i];
+
+                       if ((a != ta || c != tc) &&
+                           ((byte)ta & 0x80) && ((byte)tc & 0x80))
+                       {
+                               /* Row and Col */
+                               row = ((byte)ta & 0x7F);
+                               col = ((byte)tc & 0x7F);
+
+                               /* Terrain Source rectangle */
+                               terrain_r.left = col * grafWidth;
+                               terrain_r.top = row * grafHeight;
+                               terrain_r.right = terrain_r.left + grafWidth;
+                               terrain_r.bottom = terrain_r.top + grafHeight;
+
+                               terrain_flag = TRUE;
+                       }
+
                        /* Row and Col */
-                       row = ((byte)a & 0x7F);// % kPictRows;
-                       col = ((byte)c & 0x7F);// % kPictCols;
+                       row = ((byte)a & 0x7F);
+                       col = ((byte)c & 0x7F);
                        
                        /* Source rectangle */
-                       r1.left = col * kGrafWidth;
-                       r1.top = row * kGrafHeight;
-                       r1.right = r1.left + kGrafWidth;
-                       r1.bottom = r1.top + kGrafHeight;
+                       r1.left = col * grafWidth;
+                       r1.top = row * grafHeight;
+                       r1.right = r1.left + grafWidth;
+                       r1.bottom = r1.top + grafHeight;
 
                        /* Hardwire CopyBits */
                        BackColor(whiteColor);
                        ForeColor(blackColor);
 
                        /* Draw the picture */
+#if TARGET_API_MAC_CARBON
+                       destBitMap = GetPortPixMap(GetWindowPort( td->w ));
+#else
+                       destBitMap = (BitMapPtr)&(td->w->portBits);
+#endif
+                       if (use_bigtile) r2.right += td->tile_wid;
+
+                       if (terrain_flag)
                        {
-                               BitMapPtr       srcBitMap = (BitMapPtr)(frameP->framePix);
-                               BitMapPtr       destBitMap;
-                               
-                               if( use_buffer )
-                               {
-                                       destBitMap = (BitMapPtr)(td->bufferPix);
-                               }
-                               else
-                               {
-                                       destBitMap = (BitMapPtr)&(td->w->portBits);
-                               }
-                               
-                               /* draw transparent tile */
-                               /* BackColor is ignored and the destination is left untouched */
+                               /*
+                                * Source mode const = srcCopy:
+                                *
+                                * determine how close the color of the source
+                                * pixel is to black, and assign this relative
+                                * amount of foreground color to the
+                                * destination pixel; determine how close the
+                                * color of the source pixel is to white, and
+                                * assign this relative amount of background
+                                * color to the destination pixel
+                                */
+#if TARGET_API_MAC_CARBON
+                               CopyBits( (BitMap *) *srcBitMap, (BitMap *) *destBitMap, &terrain_r, &r2, srcCopy, NULL);
+#else
+                               CopyBits( srcBitMap, destBitMap, &terrain_r, &r2, srcCopy, NULL );
+#endif
+                               /*
+                                * Draw transparent tile
+                                * BackColor is ignored and the destination is
+                                * left untouched
+                                */
                                BackColor(blackColor);
+#if TARGET_API_MAC_CARBON
+                               CopyBits( (BitMap *) *srcBitMap, (BitMap *) *destBitMap, &r1, &r2, transparent, NULL);
+#else
                                CopyBits( srcBitMap, destBitMap, &r1, &r2, transparent, NULL );
+#endif
+                       }
+                       else
+                       {
+#if TARGET_API_MAC_CARBON
+                               CopyBits( (BitMap *) *srcBitMap, (BitMap *) *destBitMap, &r1, &r2, srcCopy, NULL);
+#else
+                               CopyBits( srcBitMap, destBitMap, &r1, &r2, srcCopy, NULL );
+#endif
                        }
+
                        /* Restore colors */
                        BackColor(blackColor);
                        ForeColor(whiteColor);
@@ -1998,102 +2978,53 @@ static errr Term_pict_mac(int x, int y, int n, const byte *ap, const char *cp)
                if (!done)
                {
                        int xp, yp;
-#ifndef JP
-                       /* Erase */
-                       EraseRect(&r2);
-
-                       /* Set the color */
-                       term_data_color(td, (a & 0x0F));
-
-                       /* Starting pixel */
-                       xp = r2.left + td->tile_o_x;
-                       yp = r2.top + td->tile_o_y;
 
-                       /* Move to the correct location */
-                       MoveTo(xp, yp);
-                       
-                       /* Draw the character */
-                       DrawChar(c);
-#else
                        /* Set the color */
                        term_data_color(td, (a & 0x0F));
                        
                        /* Starting pixel */
                        xp = r2.left + td->tile_o_x;
-                       yp = td->tile_o_y;
+                       yp = r2.top + td->tile_o_y;
                        
                        /* Move to the correct location */
                        MoveTo(xp, yp);
 
-                       if(iskanji(c)){
+#ifdef JP
+                       if (iskanji(c))
+                       {
+                               /* Double width rectangle */
+                               r2.right += td->tile_wid;
+
+                               /* Erase */
+                               EraseRect(&r2);
+
                                /* Draw the character */
                                DrawText(cp, i, 2);
                                
                                i++;
                                
                                r2.left += td->tile_wid;
-                               r2.right += td->tile_wid;
-                       } else {
+                       }
+                       else
+#endif
+                       {
+                               /* Erase */
+                               EraseRect(&r2);
+
                                /* Draw the character */
                                DrawChar(c);
                        }
-#endif
                }
 
                /* Advance */
                r2.left += td->tile_wid;
-               r2.right += td->tile_wid;
        }
-       
-       if( use_buffer )
-       {
-               /* Now we blast the buffer pixmap onto the screen in the right place */
-               BitMapPtr       srcBitMap = (BitMapPtr)(td->bufferPix);
-
-               BitMapPtr       destBitMap = (BitMapPtr)&(td->w->portBits);
-
-               Rect            srcRect;
-               Rect            destRect;
-               
-               
-               srcRect.left = x * td->tile_wid + td->size_ow1;
-               srcRect.top = 0;
-               srcRect.right = srcRect.left + (td->tile_wid * n);
-               srcRect.bottom = td->tile_hgt;
-               
-               destRect.left = x * td->tile_wid + td->size_ow1;
-               destRect.right = destRect.left + (td->tile_wid * n);
-               destRect.top = y * td->tile_hgt + td->size_oh1;
-               destRect.bottom = destRect.top + td->tile_hgt;
-
-#ifdef JP
-               UnlockPixels( PortPix );
-#endif
-               /* Restore GWorld */
-               SetGWorld(saveGWorld, saveGDevice);
-               
-               /* Hardwire CopyBits */
-               BackColor(whiteColor);
-               ForeColor(blackColor);
                
-               //CopyBits( srcBitMap, destBitMap, &srcRect, &destRect, srcCopy, NULL );
-               CopyBits( (BitMapPtr)(td->bufferPix) 
-                       , &(td->w->portBits), &srcRect, &destRect, srcCopy, NULL );
-               
-               /* Restore colors */
-               BackColor(blackColor);
-               ForeColor(whiteColor);
-       }
-       
        /* Success */
        return (0);
 }
 
 
-
-
-
-
 /*
  * Create and initialize window number "i"
  */
@@ -2131,6 +3062,7 @@ static void term_data_link(int i)
        td->t->xtra_hook = Term_xtra_mac;
        td->t->wipe_hook = Term_wipe_mac;
        td->t->curs_hook = Term_curs_mac;
+       td->t->bigcurs_hook = Term_bigcurs_mac;
        td->t->text_hook = Term_text_mac;
        td->t->pict_hook = Term_pict_mac;
 
@@ -2149,6 +3081,74 @@ static void term_data_link(int i)
 
 
 
+#ifdef MACH_O_CARBON
+
+/*
+ * (Carbon, Bundle)
+ * Return a POSIX pathname of the lib directory, or NULL if it can't be
+ * located.  Caller must supply a buffer along with its size in bytes,
+ * where returned pathname will be stored.
+ * I prefer use of goto's to several nested if's, if they involve error
+ * handling.  Sorry if you are offended by their presence.  Modern
+ * languages have neater constructs for this kind of jobs -- pelpel
+ */
+static char *locate_lib(char *buf, size_t size)
+{
+       CFURLRef main_url = NULL;
+       CFStringRef main_str = NULL;
+       char *p;
+       char *res = NULL;
+
+       /* Obtain the URL of the main bundle */
+       main_url = CFBundleCopyBundleURL(CFBundleGetMainBundle());
+
+       /* Oops */
+       if (main_url == NULL) goto ret;
+
+       /* Convert it to POSIX pathname */
+       main_str = CFURLCopyFileSystemPath(main_url, kCFURLPOSIXPathStyle);
+
+       /* Oops */
+       if (main_str == NULL) goto ret;
+
+       /* Convert it again from darn unisomething encoding to ASCII */
+       if (CFStringGetCString(main_str, buf, size, kTextEncodingUS_ASCII) == FALSE)
+               goto ret;
+
+       /* Find the last '/' in the pathname */
+       p = strrchr(buf, '/');
+
+       /* Paranoia - cannot happen */
+       if (p == NULL) goto ret;
+
+       /* Remove the trailing path */
+       *p = '\0';
+
+       /*
+        * Paranoia - bounds check, with 5 being the length of "/lib/"
+        * and 1 for terminating '\0'.
+        */
+       if (strlen(buf) + 5 + 1 > size) goto ret;
+
+       /* Append "/lib/" */
+       strcat(buf, "/lib/");
+
+       /* Set result */
+       res = buf;
+
+ret:
+
+       /* Release objects allocated and implicitly retained by the program */
+       if (main_str) CFRelease(main_str);
+       if (main_url) CFRelease(main_url);
+
+       /* pathname of the lib folder or NULL */
+       return (res);
+}
+
+
+#else /* MACH_O_CARBON */
+
 
 /*
  * Set the "current working directory" (also known as the "default"
@@ -2173,36 +3173,265 @@ static void SetupAppDir(void)
        err = PBGetFCBInfo(&fcbBlock, FALSE);
        if (err != noErr)
        {
-#ifdef JP
-               sprintf(errString, "PBGetFCBInfo ¥¨¥é¡¼ #%d.\r ½ªÎ»¤·¤Þ¤¹.", err);
-#else
-               sprintf(errString, "Fatal PBGetFCBInfo Error #%d.\r Exiting.", err);
-#endif
+               sprintf(errString, _("PBGetFCBInfo エラー #%d.\r 終了します.", "Fatal PBGetFCBInfo Error #%d.\r Exiting."), err);
+               mac_warning(errString);
+               ExitToShell();
+       }
+
+       /* Extract the Vol and Dir */
+       app_vol = fcbBlock.ioFCBVRefNum;
+       app_dir = fcbBlock.ioFCBParID;
+
+       /* Set the current working directory to that location */
+       err = HSetVol(NULL, app_vol, app_dir);
+       if (err != noErr)
+       {
+               sprintf(errString, _("HSetVol エラー #%d.\r 終了します.", "Fatal HSetVol Error #%d.\r Exiting."), err);
                mac_warning(errString);
                ExitToShell();
        }
+}
+
+#endif
+
+
+
+#if TARGET_API_MAC_CARBON
+/*
+ * Using Core Foundation's Preferences services -- pelpel
+ *
+ * Requires OS 8.6 or greater with CarbonLib 1.1 or greater. Or OS X,
+ * of course.
+ *
+ * Without this, we can support older versions of OS 8 as well
+ * (with CarbonLib 1.0.4).
+ *
+ * Frequent allocation/deallocation of small chunks of data is
+ * far from my liking, but since this is only called at the
+ * beginning and the end of a session, I hope this hardly matters.
+ */
+
+
+/*
+ * Store "value" as the value for preferences item name
+ * pointed by key
+ */
+static void save_pref_short(const char *key, short value)
+{
+       CFStringRef cf_key;
+       CFNumberRef cf_value;
+
+       /* allocate and initialise the key */
+       cf_key = CFStringCreateWithCString(NULL, key, kTextEncodingUS_ASCII);
+
+       /* allocate and initialise the value */
+       cf_value = CFNumberCreate(NULL, kCFNumberShortType, &value);
+
+       if ((cf_key != NULL) && (cf_value != NULL))
+       {
+               /* Store the key-value pair in the applications preferences */
+               CFPreferencesSetAppValue(
+                       cf_key,
+                       cf_value,
+                       kCFPreferencesCurrentApplication);
+       }
+
+       /*
+        * Free CF data - the reverse order is a vain attempt to
+        * minimise memory fragmentation.
+        */
+       if (cf_value) CFRelease(cf_value);
+       if (cf_key) CFRelease(cf_key);
+}
+
+
+/*
+ * Load preference value for key, returns TRUE if it succeeds with
+ * vptr updated appropriately, FALSE otherwise.
+ */
+static bool query_load_pref_short(const char *key, short *vptr)
+{
+       CFStringRef cf_key;
+       CFNumberRef cf_value;
+
+       /* allocate and initialise the key */
+       cf_key = CFStringCreateWithCString(NULL, key, kTextEncodingUS_ASCII);
+
+       /* Oops */
+       if (cf_key == NULL) return (FALSE);
+
+       /* Retrieve value for the key */
+       cf_value = CFPreferencesCopyAppValue(
+               cf_key,
+               kCFPreferencesCurrentApplication);
+
+       /* Value not found */
+       if (cf_value == NULL)
+       {
+               CFRelease(cf_key);
+               return (FALSE);
+       }
+
+       /* Convert the value to short */
+       CFNumberGetValue(
+               cf_value,
+               kCFNumberShortType,
+               vptr);
+
+       /* Free CF data */
+       CFRelease(cf_value);
+       CFRelease(cf_key);
+
+       /* Success */
+       return (TRUE);
+}
+
+
+/*
+ * Update short data pointed by vptr only if preferences
+ * value for key is located.
+ */
+static void load_pref_short(const char *key, short *vptr)
+{
+       short tmp;
+
+       if (query_load_pref_short(key, &tmp)) *vptr = tmp;
+       return;
+}
+
+
+/*
+ * Save preferences to preferences file for current host+current user+
+ * current application.
+ */
+static void cf_save_prefs()
+{
+       int i;
+
+       /* Version stamp */
+       save_pref_short("version.major", FAKE_VERSION);
+       save_pref_short("version.minor", FAKE_VER_MAJOR);
+       save_pref_short("version.patch", FAKE_VER_MINOR);
+       save_pref_short("version.extra", FAKE_VER_PATCH);
+
+       /* Gfx settings */
+       save_pref_short("arg.arg_sound", arg_sound);
+       save_pref_short("arg.graf_mode", graf_mode);
+       save_pref_short("arg.arg_bigtile", arg_bigtile);
+
+#ifndef MACH_O_CARBON
+       /* SoundMode */
+       for( i = 0 ; i < 7 ; i++ )
+               save_pref_short(format("sound%d.on", i), soundmode[i]);
+#endif /* MACH_O_CARBON */
+
+       /* Windows */
+       for (i = 0; i < MAX_TERM_DATA; i++)
+       {
+               term_data *td = &data[i];
+
+               save_pref_short(format("term%d.mapped", i), td->mapped);
+
+               save_pref_short(format("term%d.font_id", i), td->font_id);
+               save_pref_short(format("term%d.font_size", i), td->font_size);
+               save_pref_short(format("term%d.font_face", i), td->font_face);
+
+               save_pref_short(format("term%d.tile_wid", i), td->tile_wid);
+               save_pref_short(format("term%d.tile_hgt", i), td->tile_hgt);
+
+               save_pref_short(format("term%d.cols", i), td->cols);
+               save_pref_short(format("term%d.rows", i), td->rows);
+               save_pref_short(format("term%d.left", i), td->r.left);
+               save_pref_short(format("term%d.top", i), td->r.top);
+       }
+
+       /*
+        * Make sure preferences are persistent
+        */
+       CFPreferencesAppSynchronize(
+               kCFPreferencesCurrentApplication);
+}
+
+
+/*
+ * Load preferences from preferences file for current host+current user+
+ * current application.
+ */
+static void cf_load_prefs()
+{
+       bool ok;
+       short pref_major, pref_minor, pref_patch, pref_extra;
+       int i;
+
+       /* Assume nothing is wrong, yet */
+       ok = TRUE;
+
+       /* Load version information */
+       ok &= query_load_pref_short("version.major", &pref_major);
+       ok &= query_load_pref_short("version.minor", &pref_minor);
+       ok &= query_load_pref_short("version.patch", &pref_patch);
+       ok &= query_load_pref_short("version.extra", &pref_extra);
+
+       /* Any of the above failed */
+       if (!ok)
+       {
+               /* This may be the first run */
+               mac_warning(_("初期設定ファイルが見つかりません。", "Preferences are not found."));
+
+               /* Ignore the rest */
+               return;
+       }
+
+       /* Gfx settings */
+       {
+               short pref_tmp;
 
-       /* Extract the Vol and Dir */
-       app_vol = fcbBlock.ioFCBVRefNum;
-       app_dir = fcbBlock.ioFCBParID;
+               /* sound */
+               if (query_load_pref_short("arg.arg_sound", &pref_tmp))
+                       arg_sound = pref_tmp;
 
-       /* Set the current working directory to that location */
-       err = HSetVol(NULL, app_vol, app_dir);
-       if (err != noErr)
+               /* graphics */
+               if (query_load_pref_short("arg.graf_mode", &pref_tmp))
+                       graf_mode_req = pref_tmp;
+
+               /* double-width tiles */
+               if (query_load_pref_short("arg.arg_bigtile", &pref_tmp))
+               {
+                       arg_bigtile = use_bigtile = pref_tmp;
+               }
+
+       }
+
+#ifndef MACH_O_CARBON
+       /* SoundMode */
+       for( i = 0 ; i < 7 ; i++ )
        {
-#ifdef JP
-               sprintf(errString, "HSetVol ¥¨¥é¡¼ #%d.\r ½ªÎ»¤·¤Þ¤¹.", err);
-#else
-               sprintf(errString, "Fatal HSetVol Error #%d.\r Exiting.", err);
-#endif
-               mac_warning(errString);
-               ExitToShell();
+               query_load_pref_short(format("sound%d.on", i), &soundmode[i]);
        }
-}
+#endif /* MACH_O_CARBON */
+
+       /* Windows */
+       for (i = 0; i < MAX_TERM_DATA; i++)
+       {
+               term_data *td = &data[i];
 
+               load_pref_short(format("term%d.mapped", i), &td->mapped);
 
+               load_pref_short(format("term%d.font_id", i), &td->font_id);
+               load_pref_short(format("term%d.font_size", i), &td->font_size);
+               load_pref_short(format("term%d.font_face", i), &td->font_face);
 
+               load_pref_short(format("term%d.tile_wid", i), &td->tile_wid);
+               load_pref_short(format("term%d.tile_hgt", i), &td->tile_hgt);
 
+               load_pref_short(format("term%d.cols", i), &td->cols);
+               load_pref_short(format("term%d.rows", i), &td->rows);
+               load_pref_short(format("term%d.left", i), &td->r.left);
+               load_pref_short(format("term%d.top", i), &td->r.top);
+       }
+}
+
+#else
 /*
  * Global "preference" file pointer
  */
@@ -2215,7 +3444,7 @@ static int getshort(void)
 {
        int x = 0;
        char buf[256];
-       if (0 == my_fgets(fff, buf, 256)) x = atoi(buf);
+       if (0 == my_fgets(fff, buf, sizeof(buf))) x = atoi(buf);
        return (x);
 }
 
@@ -2241,13 +3470,14 @@ static void save_prefs(void)
 
        /*** The current version ***/
 
-       putshort(VERSION_MAJOR);
-       putshort(VERSION_MINOR);
-       putshort(VERSION_PATCH);
-       putshort(VERSION_EXTRA);
+       putshort(FAKE_VERSION);
+       putshort(FAKE_VER_MAJOR);
+       putshort(FAKE_VER_MINOR);
+       putshort(FAKE_VER_PATCH);
 
        putshort(arg_sound);
-       putshort(arg_graphics);
+       putshort(graf_mode);
+       putshort(arg_bigtile);
        
        /* SoundMode */
        for( i = 0 ; i < 7 ; i++ )
@@ -2265,10 +3495,8 @@ static void save_prefs(void)
                putshort(td->font_size);
                putshort(td->font_face);
 
-#ifdef JP
                putshort(td->tile_wid);
                putshort(td->tile_hgt);
-#endif
 
                putshort(td->cols);
                putshort(td->rows);
@@ -2289,7 +3517,7 @@ static void load_prefs(void)
 {
        int i;
 
-       int old_major, old_minor, old_patch, old_extra;
+       int old_version, old_major, old_minor, old_patch;
 
        term_data *td;
        MenuHandle m;
@@ -2297,20 +3525,20 @@ static void load_prefs(void)
        /*** Version information ***/
 
        /* Preferences version */
+       old_version = getshort();
        old_major = getshort();
        old_minor = getshort();
        old_patch = getshort();
-       old_extra = getshort();
 
        /* Hack -- Verify or ignore */
-       if ((old_major != VERSION_MAJOR) ||
-           (old_minor != VERSION_MINOR) ||
-           (old_patch != VERSION_PATCH) ||
-           (old_extra != VERSION_EXTRA))
+       if ((old_version != FAKE_VERSION) ||
+           (old_major != FAKE_VER_MAJOR) ||
+           (old_minor != FAKE_VER_MINOR) ||
+           (old_patch != FAKE_VER_PATCH))
        {
                /* Message */
                #ifdef JP
-               mac_warning("¸Å¤¤½é´üÀßÄê¥Õ¥¡¥¤¥ë¤ò̵»ë¤·¤Þ¤¹.");
+               mac_warning("古い初期設定ファイルを無視します.");
                #else
                mac_warning("Ignoring old preferences.");
                #endif
@@ -2319,21 +3547,14 @@ static void load_prefs(void)
        }
 
        arg_sound = getshort();
-       arg_graphics = getshort();
+       graf_mode_req = getshort();
+       arg_bigtile = getshort();
+       use_bigtile = arg_bigtile;
        
        /* SoundMode */
        for( i = 0 ; i < 7 ; i++ )
                soundmode[i] = getshort();
        
-       /* Special menu */
-       m = GetMenuHandle(134); //m = GetMHandle(134);
-       
-       /* Item "arg_sound" */
-       CheckItem(m, 1, arg_sound);
-
-       /* Item "arg_graphics" */
-       CheckItem(m, 2, arg_graphics);
-
        /* Windows */
        for (i = 0; i < MAX_TERM_DATA; i++)
        {
@@ -2346,10 +3567,8 @@ static void load_prefs(void)
                td->font_size = getshort();
                td->font_face = getshort();
 
-#ifdef JP
                td->tile_wid = getshort();
                td->tile_hgt = getshort();
-#endif
 
                td->cols = getshort();
                td->rows = getshort();
@@ -2361,7 +3580,7 @@ static void load_prefs(void)
                if (feof(fff)) break;
        }
 }
-
+#endif /* TARGET_API_MAC_CARBON */
 
 
 
@@ -2372,13 +3591,25 @@ static void term_data_hack(term_data *td)
 {
        short fid;
 
-       #ifdef JP
-       GetFNum( "\pÅùÉýÌÀÄ«", &fid);     /* ¥Õ¥©¥ó¥È̾¤«¤éIDÈÖ¹æ¤òÄ´¤Ù¤ë  */
+#if TARGET_API_MAC_CARBON
+#ifdef JP
+       /* Default to Osaka font (Japanese) */
+       fid = FMGetFontFamilyFromName( "\pOsaka−等幅" );
+#else
+       /* Default to Monaco font */
+       fid = FMGetFontFamilyFromName("\pmonaco");
+#endif
+#else
+#ifdef JP
+       /* Default to 等幅明朝 font (Japanese) */
+       GetFNum( "\p等幅明朝", &fid);
        SetFScaleDisable( true );
-       #else
+#else
        /* Default to Monaco font */
        GetFNum("\pmonaco", &fid);
-       #endif
+#endif
+#endif
+
        /* Wipe it */
        WIPE(td, term_data);
 
@@ -2428,12 +3659,16 @@ static void init_windows(void)
 
        term_data *td;
 
+#if !TARGET_API_MAC_CARBON
+
        SysEnvRec env;
        short savev;
        long saved;
 
        bool oops;
 
+#endif /* !TARGET_API_MAC_CARBON */
+
 
        /*** Default values ***/
 
@@ -2475,7 +3710,10 @@ static void init_windows(void)
 
 
        /*** Load preferences ***/
-
+       
+#if TARGET_API_MAC_CARBON
+       cf_load_prefs();
+#else
        /* Assume failure */
        oops = TRUE;
 
@@ -2494,7 +3732,7 @@ static void init_windows(void)
 
                /* Find the folder */
                err = FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
-                                &vref, &dirID);
+                                &vref, &dirID);
 
                /* Success */
                if (!err)
@@ -2506,11 +3744,8 @@ static void init_windows(void)
                        ptocstr((StringPtr)foo);
 
                        /* Append the preference file name */
-#ifdef JP
-                       strcat(foo, "Hengband Preferences");
-#else
-                       strcat(foo, "Angband Preferences");
-#endif
+                       strcat(foo, PREF_FILE_NAME);
+
                        /* Open the preference file */
                        fff = fopen(foo, "r");
 
@@ -2532,13 +3767,8 @@ static void init_windows(void)
                SetVol(0, env.sysVRefNum);
 
                /* Open the file */
-#ifdef JP
-               fff = fopen(":Preferences:Hengband Preferences", "r");
-               if (!fff) fff = fopen(":Hengband Preferences", "r");
-#else
-               fff = fopen(":Preferences:Angband Preferences", "r");
-               if (!fff) fff = fopen(":Angband Preferences", "r");
-#endif
+               fff = fopen(PREF_FILE_NAME, "r");
+
                /* Restore */
                HSetVol(0, savev, saved);
        }
@@ -2552,6 +3782,7 @@ static void init_windows(void)
                /* Close the file */
                my_fclose(fff);
        }
+#endif /* TARGET_API_MAC_CARBON */
 
 
        /*** Instantiate ***/
@@ -2578,11 +3809,12 @@ static void init_windows(void)
        Term_activate(td->t);
 }
 
-#ifdef JP
+#ifndef MACH_O_CARBON
+
 static void init_sound( void )
 {
        int err, i;
-       DirInfo pb;
+       CInfoPBRec pb;
        SignedByte              permission = fsRdPerm;
        pascal short    ret;
        
@@ -2590,45 +3822,45 @@ static void init_sound( void )
        Str255 sound;
 
        /* Descend into "lib" folder */
-       pb.ioCompletion = NULL;
-       pb.ioNamePtr = "\plib";
-       pb.ioVRefNum = app_vol;
-       pb.ioDrDirID = app_dir;
-       pb.ioFDirIndex = 0;
+       pb.dirInfo.ioCompletion = NULL;
+       pb.dirInfo.ioNamePtr = "\plib";
+       pb.dirInfo.ioVRefNum = app_vol;
+       pb.dirInfo.ioDrDirID = app_dir;
+       pb.dirInfo.ioFDirIndex = 0;
 
        /* Check for errors */
        err = PBGetCatInfo((CInfoPBPtr)&pb, FALSE);
 
        /* Success */
-       if ((err == noErr) && (pb.ioFlAttrib & 0x10))
+       if ((err == noErr) && (pb.dirInfo.ioFlAttrib & 0x10))
        {
                /* Descend into "lib/save" folder */
-               pb.ioCompletion = NULL;
-               pb.ioNamePtr = "\pxtra";
-               pb.ioVRefNum = app_vol;
-               pb.ioDrDirID = pb.ioDrDirID;
-               pb.ioFDirIndex = 0;
+               pb.dirInfo.ioCompletion = NULL;
+               pb.dirInfo.ioNamePtr = "\pxtra";
+               pb.dirInfo.ioVRefNum = app_vol;
+               pb.dirInfo.ioDrDirID = pb.dirInfo.ioDrDirID;
+               pb.dirInfo.ioFDirIndex = 0;
 
                /* Check for errors */
                err = PBGetCatInfo((CInfoPBPtr)&pb, FALSE);
                        
                        /* Success */
-               if ((err == noErr) && (pb.ioFlAttrib & 0x10))
+               if ((err == noErr) && (pb.dirInfo.ioFlAttrib & 0x10))
                {
                        /* Descend into "lib/save" folder */
-                       pb.ioCompletion = NULL;
-                       pb.ioNamePtr = "\psound";
-                       pb.ioVRefNum = app_vol;
-                       pb.ioDrDirID = pb.ioDrDirID;
-                       pb.ioFDirIndex = 0;
+                       pb.dirInfo.ioCompletion = NULL;
+                       pb.dirInfo.ioNamePtr = "\psound";
+                       pb.dirInfo.ioVRefNum = app_vol;
+                       pb.dirInfo.ioDrDirID = pb.dirInfo.ioDrDirID;
+                       pb.dirInfo.ioFDirIndex = 0;
 
                        /* Check for errors */
                        err = PBGetCatInfo((CInfoPBPtr)&pb, FALSE);
 
                        /* Success */
-                       if ((err == noErr) && (pb.ioFlAttrib & 0x10))
+                       if ((err == noErr) && (pb.dirInfo.ioFlAttrib & 0x10))
                        {
-                               ret = HOpenResFile( app_vol , pb.ioDrDirID , "\psound.rsrc" , permission );
+                               ret = HOpenResFile( app_vol , pb.dirInfo.ioDrDirID , "\psound.rsrc" , permission );
                                if( ret != -1 ){
                                        ext_sound = 1;
                                        
@@ -2655,6 +3887,72 @@ static void init_sound( void )
        }
 }
 
+static void init_graf( void )
+{
+       int err, i;
+       CInfoPBRec pb;
+       SignedByte              permission = fsRdPerm;
+       pascal short    ret;
+       
+       Handle handle;
+       Str255 graf;
+
+       /* Descend into "lib" folder */
+       pb.dirInfo.ioCompletion = NULL;
+       pb.dirInfo.ioNamePtr = "\plib";
+       pb.dirInfo.ioVRefNum = app_vol;
+       pb.dirInfo.ioDrDirID = app_dir;
+       pb.dirInfo.ioFDirIndex = 0;
+
+       /* Check for errors */
+       err = PBGetCatInfo((CInfoPBPtr)&pb, FALSE);
+
+       /* Success */
+       if ((err == noErr) && (pb.dirInfo.ioFlAttrib & 0x10))
+       {
+               /* Descend into "lib/xtra" folder */
+               pb.dirInfo.ioCompletion = NULL;
+               pb.dirInfo.ioNamePtr = "\pxtra";
+               pb.dirInfo.ioVRefNum = app_vol;
+               pb.dirInfo.ioDrDirID = pb.dirInfo.ioDrDirID;
+               pb.dirInfo.ioFDirIndex = 0;
+
+               /* Check for errors */
+               err = PBGetCatInfo((CInfoPBPtr)&pb, FALSE);
+                       
+               /* Success */
+               if ((err == noErr) && (pb.dirInfo.ioFlAttrib & 0x10))
+               {
+                       /* Descend into "lib/xtra/graf" folder */
+                       pb.dirInfo.ioCompletion = NULL;
+                       pb.dirInfo.ioNamePtr = "\pgraf";
+                       pb.dirInfo.ioVRefNum = app_vol;
+                       pb.dirInfo.ioDrDirID = pb.dirInfo.ioDrDirID;
+                       pb.dirInfo.ioFDirIndex = 0;
+
+                       /* Check for errors */
+                       err = PBGetCatInfo((CInfoPBPtr)&pb, FALSE);
+
+                       /* Success */
+                       if ((err == noErr) && (pb.dirInfo.ioFlAttrib & 0x10))
+                       {
+                               ret = HOpenResFile( app_vol , pb.dirInfo.ioDrDirID , "\pgraf.rsrc" , permission );
+                               if (ret != -1)
+                               {
+                                       ext_graf = 1;
+
+                                       /* Obtain resource XXX XXX XXX */
+                                       handle = Get1NamedResource('PICT', graf);
+                                       if ( handle == NULL || ext_graf )
+                                               handle = GetNamedResource('PICT', "\pgraf.rsrc");
+                               }
+                       }
+               }
+       }
+}
+
+#endif /* MACH_O_CARBON */
+
 #ifdef CHUUKEI
 /*
 
@@ -2665,7 +3963,7 @@ static void init_chuukei( void )
        char tmp[1024];
        FILE *fp;
        
-       path_build(path, 1024, ANGBAND_DIR_XTRA, "chuukei.txt");
+       path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "chuukei.txt");
 
        fp = fopen(path, "r");
        if(!fp)
@@ -2757,7 +4055,6 @@ short GetCheck( DialogPtr targetDlg, short check )
 void SoundConfigDLog(void)
 {
        DialogPtr dialog;
-       Rect r;
        short item_hit;
        int     i;
 
@@ -2767,7 +4064,7 @@ void SoundConfigDLog(void)
        for( i = 1 ; i < 7 ; i++ )
                SetCheck( dialog, i+2 , soundmode[i] );
        
-       ShowWindow(dialog);
+       /* ShowWindow(dialog); */ 
        for( item_hit = 100 ; cancel < item_hit ; ){
                ModalDialog(0, &item_hit);
                
@@ -2786,11 +4083,16 @@ void SoundConfigDLog(void)
 
 }
 
-#endif
 
 /*
  * Exit the program
  */
+#if TARGET_API_MAC_CARBON
+static void save_pref_file(void)
+{
+       cf_save_prefs();
+}
+#else
 static void save_pref_file(void)
 {
        bool oops;
@@ -2822,7 +4124,7 @@ static void save_pref_file(void)
 
                /* Find the folder */
                err = FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
-                                &vref, &dirID);
+                                &vref, &dirID);
 
                /* Success */
                if (!err)
@@ -2834,11 +4136,8 @@ static void save_pref_file(void)
                        ptocstr((StringPtr)foo);
 
                        /* Append the preference file name */
-#ifdef JP
-                       strcat(foo, "Hengband Preferences");
-#else
-                       strcat(foo, "Angband Preferences");
-#endif
+                       strcat(foo, PREF_FILE_NAME);
+
                        /* Open the preference file */
                        /* my_fopen set file type and file creator for MPW */
                        fff = my_fopen(foo, "w");
@@ -2862,14 +4161,8 @@ static void save_pref_file(void)
 
                /* Open the preference file */
                /* my_fopen set file type and file creator for MPW */
-#ifdef JP
-               fff = my_fopen(":Preferences:Hengband Preferences", "w");
-               if (!fff) fff = my_fopen(":Hengband Preferences", "w");
+               fff = fopen(PREF_FILE_NAME, "w");
 
-#else
-               fff = my_fopen(":Preferences:Angband Preferences", "w");
-               if (!fff) fff = my_fopen(":Angband Preferences", "w");
-#endif
                /* Restore */
                HSetVol(0, savev, saved);
        }
@@ -2884,8 +4177,11 @@ static void save_pref_file(void)
                my_fclose(fff);
        }
 }
+#endif
+
 
 
+#if defined(__MWERKS__)
 
 /*
  * A simple "Yes/No" filter to parse "key press" events in dialog windows
@@ -2915,7 +4211,7 @@ static pascal Boolean ynfilter(DialogPtr dialog, EventRecord *event, short *ip)
                        Rect r;
 
                        /* Get the button */
-                       GetDialogItem(dialog, i, &type, (Handle*)&control, &r); //GetDItem(dialog, i, &type, (Handle*)&control, &r);
+                       GetDialogItem(dialog, i, &type, (Handle*)&control, &r);
 
                        /* Blink button for 1/10 second */
                        HiliteControl(control, 1);
@@ -2932,6 +4228,139 @@ static pascal Boolean ynfilter(DialogPtr dialog, EventRecord *event, short *ip)
        return (0);
 }
 
+#endif /* __MWERKS__ */
+
+
+#if TARGET_API_MAC_CARBON
+
+/*
+ * Prepare savefile dialogue and set the variable
+ * savefile accordingly. Returns true if it succeeds, false (or
+ * aborts) otherwise. If all is false, only allow files whose type
+ * is 'SAVE'.
+ * Originally written by Peter Ammon
+ */
+static bool select_savefile(bool all)
+{
+       OSErr err;
+       FSSpec theFolderSpec;
+       FSSpec savedGameSpec;
+       NavDialogOptions dialogOptions;
+       NavReplyRecord reply;
+       /* Used only when 'all' is true */
+       NavTypeList types = {ANGBAND_CREATOR, 1, 1, {'SAVE'}};
+       NavTypeListHandle myTypeList;
+       AEDesc defaultLocation;
+
+#ifdef MACH_O_CARBON
+
+       /* Find the save folder */
+       err = path_to_spec(ANGBAND_DIR_SAVE, &theFolderSpec);
+
+#else
+
+       /* Find :lib:save: folder */
+       err = FSMakeFSSpec(app_vol, app_dir, "\p:lib:save:", &theFolderSpec);
+
+#endif
+
+       /* Oops */
+       if (err != noErr) quit("Unable to find the folder :lib:save:");
+
+       /* Get default Navigator dialog options */
+       err = NavGetDefaultDialogOptions(&dialogOptions);
+
+       /* Clear preview option */
+       dialogOptions.dialogOptionFlags &= ~kNavAllowPreviews;
+
+       /* Disable multiple file selection */
+       dialogOptions.dialogOptionFlags &= ~kNavAllowMultipleFiles;
+
+       /* Make descriptor for default location */
+       err = AECreateDesc(typeFSS, &theFolderSpec, sizeof(FSSpec),
+               &defaultLocation);
+
+       /* Oops */
+       if (err != noErr) quit("Unable to allocate descriptor");
+
+       /* We are indifferent to signature and file types */
+       if (all)
+       {
+               myTypeList = (NavTypeListHandle)nil;
+       }
+
+       /* Set up type handle */
+       else
+       {
+               err = PtrToHand(&types, (Handle *)&myTypeList, sizeof(NavTypeList));
+
+               /* Oops */
+               if (err != noErr) quit("Error in PtrToHand. Try enlarging heap");
+
+       }
+
+       /* Call NavGetFile() with the types list */
+       err = NavChooseFile(&defaultLocation, &reply, &dialogOptions, NULL,
+               NULL, NULL, myTypeList, NULL);
+
+       /* Free type list */
+       if (!all) DisposeHandle((Handle)myTypeList);
+
+       /* Error */
+       if (err != noErr)
+       {
+               /* Nothing */
+       }
+
+       /* Invalid response -- allow the user to cancel */
+       else if (!reply.validRecord)
+       {
+               /* Hack -- Fake error */
+               err = -1;
+       }
+
+       /* Retrieve FSSpec from the reply */
+       else
+       {
+               AEKeyword theKeyword;
+               DescType actualType;
+               Size actualSize;
+
+               /* Get a pointer to selected file */
+               (void)AEGetNthPtr(&reply.selection, 1, typeFSS, &theKeyword,
+                       &actualType, &savedGameSpec, sizeof(FSSpec), &actualSize);
+
+               /* Dispose NavReplyRecord, resources and descriptors */
+               (void)NavDisposeReply(&reply);
+       }
+
+       /* Dispose location info */
+       AEDisposeDesc(&defaultLocation);
+
+       /* Error */
+       if (err != noErr) return (FALSE);
+
+#ifdef MACH_O_CARBON
+
+       /* Convert FSSpec to pathname and store it in variable savefile */
+       (void)spec_to_path(&savedGameSpec, savefile, sizeof(savefile));
+
+#else
+
+       /* Convert FSSpec to pathname and store it in variable savefile */
+       refnum_to_name(
+               savefile,
+               savedGameSpec.parID,
+               savedGameSpec.vRefNum,
+               (char *)savedGameSpec.name);
+
+#endif
+
+       /* Success */
+       return (TRUE);
+}
+#endif
+
 
 /*
  * Handle menu: "File" + "New"
@@ -2958,6 +4387,28 @@ static void do_menu_file_new(void)
 /*
  * Handle menu: "File" + "Open"
  */
+#if TARGET_API_MAC_CARBON
+static void do_menu_file_open(bool all)
+{
+       /* Let the player to choose savefile */
+       if (!select_savefile(all)) return;
+
+       /* Hack */
+       HiliteMenu(0);
+
+       /* Game is in progress */
+       game_in_progress = TRUE;
+
+       /* Flush input */
+       flush();
+
+       /* Play a game */
+       play_game(FALSE);
+
+       /* Hack -- quit */
+       quit(NULL);
+}
+#else
 static void do_menu_file_open(bool all)
 {
        int err;
@@ -3052,6 +4503,7 @@ static void do_menu_file_open(bool all)
        /* Hack -- quit */
        quit(NULL);
 }
+#endif
 
 
 /*
@@ -3087,11 +4539,6 @@ static void handle_open_when_ready(void)
 /*
  * Initialize the menus
  *
- * Verify menus 128, 129, 130
- * Create menus 131, 132, 133, 134
- *
- * The standard menus are:
- *
  *   Apple (128) =   { About, -, ... }
  *   File (129) =    { New,Open,Import,Close,Save,-,Exit,Quit }
  *   Edit (130) =    { Cut, Copy, Paste, Clear }   (?)
@@ -3099,9 +4546,70 @@ static void handle_open_when_ready(void)
  *   Size (132) =    { ... }
  *   Window (133) =  { Angband, Mirror, Recall, Choice,
  *                     Term-4, Term-5, Term-6, Term-7 }
- *   Special (134) = { arg_sound, arg_graphics, -,
- *                     arg_fiddle, arg_wizard }
+ *   Special (134) = { Sound, Graphics, TileWidth, TileHeight, -,
+ *                     Fiddle, Wizard }
  */
+
+/* Apple menu */
+#define MENU_APPLE     128
+#define ITEM_ABOUT     1
+
+/* File menu */
+#define MENU_FILE      129
+# define ITEM_NEW      1
+# define ITEM_OPEN     2
+# define ITEM_IMPORT   3
+# define ITEM_CLOSE    4
+# define ITEM_SAVE     5
+#  define ITEM_QUIT    7
+
+/* Edit menu */
+#define MENU_EDIT      130
+# define ITEM_UNDO     1
+# define ITEM_CUT      3
+# define ITEM_COPY     4
+# define ITEM_PASTE    5
+# define ITEM_CLEAR    6
+
+/* Font menu */
+#define MENU_FONT      131
+# define ITEM_BOLD     1
+# define ITEM_WIDE     2
+
+/* Size menu */
+#define MENU_SIZE      132
+
+/* Windows menu */
+#define MENU_WINDOWS   133
+
+/* Special menu */
+#define MENU_SPECIAL   134
+# define ITEM_SOUND    1
+# define ITEM_GRAPH    2
+# define ITEM_TILEWIDTH 3
+# define ITEM_TILEHEIGHT 4
+# define ITEM_FIDDLE   6
+# define ITEM_WIZARD   7
+
+/* Sounds submenu */
+#define SUBMENU_SOUND  143
+# define ITEM_USE_SOUND        1
+# define ITEM_SOUND_SETTING    2
+
+/* Graphics submenu */
+#define SUBMENU_GRAPH  144
+# define ITEM_NONE     1
+# define ITEM_8X8      2
+# define ITEM_16X16    3
+# define ITEM_BIGTILE 5        
+
+/* TileWidth submenu */
+#define SUBMENU_TILEWIDTH      145
+
+/* TileHeight submenu */
+#define SUBMENU_TILEHEIGHT     146
+
+
 static void init_menubar(void)
 {
        int i, n;
@@ -3112,49 +4620,50 @@ static void init_menubar(void)
 
        MenuHandle m;
 
+       Handle mbar;
 
-       /* Get the "apple" menu */
-       m = GetMenu(128);
-
-       /* Insert the menu */
-       InsertMenu(m, 0);
-
-       /* Add the DA's to the "apple" menu */
-       AppendResMenu   (m, 'DRVR');    //AddResMenu(m, 'DRVR');
-
-
-       /* Get the "File" menu */
-       m = GetMenu(129);
+#if TARGET_API_MAC_CARBON
+       OSErr err;
+       long response;
+#endif
 
-       /* Insert the menu */
-       InsertMenu(m, 0);
+       /* Get the "apple" menu */
+       mbar = GetNewMBar(128);
 
+       /* Whoops! */
+       if (mbar == nil) quit(_("メニューバー ID 128を見つける事ができません!", "Cannot find menubar('MBAR') id 128!"));
 
-       /* Get the "Edit" menu */
-       m = GetMenu(130);
+       /* Insert them into the current menu list */
+       SetMenuBar(mbar);
 
-       /* Insert the menu */
-       InsertMenu(m, 0);
+       /* Free handle */
+       DisposeHandle(mbar);
 
+#if !TARGET_API_MAC_CARBON
+       /* Apple menu (id 128) */
+       m = GetMenuHandle(MENU_APPLE);
 
-       /* Make the "Font" menu */
-       #ifdef JP
-       m = NewMenu(131, "\p¥Õ¥©¥ó¥È");
-       #else
-       m = NewMenu(131, "\pFont");
-       #endif
-       
-       /* Insert the menu */
-       InsertMenu(m, 0);
+       /* Add the DA's to the "apple" menu */
+       AppendResMenu   (m, 'DRVR');
+#endif
 
-       /* Add "bold" */
-       AppendMenu(m, "\pBold");
+       /* Get the "File" menu */
+#if TARGET_API_MAC_CARBON
+       m = GetMenuHandle(MENU_FILE);
+       err = Gestalt( gestaltSystemVersion, &response );
+       if ( (err == noErr) && (response >= 0x00000A00) )
+       {
+               DeleteMenuItem(m, ITEM_QUIT);
+       }
+#endif
 
-       /* Add "wide" */
-       AppendMenu(m, "\pWide");
+       /* Edit menu (id 130) - we don't have to do anything */
 
-       /* Add a separator */
-       AppendMenu(m, "\p-");
+       /*
+        * Font menu (id 131) - append names of mono-spaced fonts
+        * followed by all available ones
+        */
+       m = GetMenuHandle(MENU_FONT);
 
        /* Fake window */
        r.left = r.right = r.top = r.bottom = 0;
@@ -3163,7 +4672,11 @@ static void init_menubar(void)
        tmpw = NewWindow(0, &r, "\p", false, documentProc, 0, 0, 0);
 
        /* Activate the "fake" window */
+#if TARGET_API_MAC_CARBON
+       SetPortWindowPort(tmpw);
+#else
        SetPort(tmpw);
+#endif
 
        /* Default mode */
        TextMode(0);
@@ -3172,10 +4685,14 @@ static void init_menubar(void)
        TextSize(12);
 
        /* Add the fonts to the menu */
-       AppendResMenu(m, 'FONT');       //AddResMenu(m, 'FONT');
+       AppendResMenu(m, 'FONT');
 
        /* Size of menu */
+#if TARGET_API_MAC_CARBON
+       n = CountMenuItems(m);
+#else
        n = CountMItems(m);
+#endif
 
        /* Scan the menu */
        for (i = n; i >= 4; i--)
@@ -3184,11 +4701,14 @@ static void init_menubar(void)
                short fontNum;
 
                /* Acquire the font name */
-               /* GetMenuItemText(m, i, tmpName); */
-               GetMenuItemText(m, i, tmpName); //GetItem(m, i, tmpName);
+               GetMenuItemText(m, i, tmpName);
 
                /* Acquire the font index */
+#if TARGET_API_MAC_CARBON
+               fontNum = FMGetFontFamilyFromName( tmpName );
+#else
                GetFNum(tmpName, &fontNum);
+#endif
 
                /* Apply the font index */
                TextFont(fontNum);
@@ -3197,8 +4717,7 @@ static void init_menubar(void)
                if ((CharWidth('i') != CharWidth('W')) || (CharWidth('W') == 0))
                {
                        /* Delete the menu item XXX XXX XXX */
-                       /* DeleteMenuItem(m, i); */
-                       DeleteMenuItem  (m, i); //DelMenuItem(m, i);
+                       DeleteMenuItem  (m, i);
                }
        }
 
@@ -3208,19 +4727,12 @@ static void init_menubar(void)
        /* Add a separator */
        AppendMenu(m, "\p-");
 
-       /* Add the fonts to the menu */
-       AppendResMenu   (m, 'FONT');    //AddResMenu(m, 'FONT');
-
-
-       /* Make the "Size" menu */
-       #ifdef JP
-       m = NewMenu(132, "\p¥µ¥¤¥º");
-       #else
-       m = NewMenu(132, "\pSize");
-       #endif
-       
-       /* Insert the menu */
-       InsertMenu(m, 0);
+       /* Add the fonts to the menu */
+       AppendResMenu   (m, 'FONT');
+
+
+       /* Size menu (id 132) */
+       m = GetMenuHandle(MENU_SIZE);
 
        /* Add some sizes (stagger choices) */
        for (i = 8; i <= 32; i += ((i / 16) + 1))
@@ -3236,15 +4748,8 @@ static void init_menubar(void)
        }
 
 
-       /* Make the "Windows" menu */
-       #ifdef JP
-       m = NewMenu(133, "\p¥¦¥¤¥ó¥É¥¦");
-       #else
-       m = NewMenu(133, "\pWindows");
-       #endif
-       
-       /* Insert the menu */
-       InsertMenu(m, 0);
+       /* Windows menu (id 133) */
+       m = GetMenuHandle(MENU_WINDOWS);
 
        /* Default choices */
        for (i = 0; i < MAX_TERM_DATA; i++)
@@ -3263,42 +4768,104 @@ static void init_menubar(void)
        }
 
 
-       /* Make the "Special" menu */
-       #ifdef JP
-       m = NewMenu(134, "\pÆÃÊÌ");
-       #else
-       m = NewMenu(134, "\pSpecial");
-       #endif
-       
-       /* Insert the menu */
-       InsertMenu(m, 0);
+#if TARGET_API_MAC_CARBON && !defined(MAC_MPW)
 
-       /* Append the choices */
-       #ifdef JP
-       AppendMenu(m, "\p¥µ¥¦¥ó¥É»ÈÍÑ");
-       AppendMenu(m, "\p¥°¥é¥Õ¥£¥Ã¥¯»ÈÍÑ");
-       AppendMenu(m, "\p-");
-       AppendMenu(m, "\parg_fiddle");
-       AppendMenu(m, "\parg_wizard");
-       AppendMenu(m, "\p-");
-       AppendMenu(m, "\p¥µ¥¦¥ó¥ÉÀßÄê...");
-       #else
-       AppendMenu(m, "\parg_sound");
-       AppendMenu(m, "\parg_graphics");
-       AppendMenu(m, "\p-");
-       AppendMenu(m, "\parg_fiddle");
-       AppendMenu(m, "\parg_wizard");
-       #endif
+       /* CW or gcc -- Use recommended interface for hierarchical menus */
 
-       /* Make the "TileWidth" menu */
-       #ifdef JP
-       m = NewMenu(135, "\p¥¿¥¤¥ëÉý");
-       #else
-       m = NewMenu(135, "\pTileWidth");
-       #endif
+       /* Special menu (id 134) */
+       m = GetMenuHandle(MENU_SPECIAL);
 
-       /* Insert the menu */
-       InsertMenu(m, 0);
+       /* Insert Graphics submenu (id 143) */
+       {
+               MenuHandle submenu;
+
+               /* Get the submenu */
+               submenu = GetMenu(SUBMENU_SOUND);
+
+               /* Insert it */
+               SetMenuItemHierarchicalMenu(m, ITEM_SOUND, submenu);
+       }
+
+       /* Insert Graphics submenu (id 144) */
+       {
+               MenuHandle submenu;
+
+               /* Get the submenu */
+               submenu = GetMenu(SUBMENU_GRAPH);
+
+               /* Insert it */
+               SetMenuItemHierarchicalMenu(m, ITEM_GRAPH, submenu);
+       }
+
+       /* Insert TileWidth submenu (id 145) */
+       {
+               MenuHandle submenu;
+
+               /* Get the submenu */
+               submenu = GetMenu(SUBMENU_TILEWIDTH);
+
+               /* Add some sizes */
+               for (i = 4, n = 1; i <= 32; i++, n++)
+               {
+                       Str15 buf;
+
+                       /* Textual size */
+                       strnfmt((char*)buf + 1, 15, "%d", i);
+                       buf[0] = strlen((char*)buf + 1);
+
+                       /* Append item */
+                       AppendMenu(submenu, buf);
+               }
+
+               /* Insert it */
+               SetMenuItemHierarchicalMenu(m, ITEM_TILEWIDTH, submenu);
+       }
+
+       /* Insert TileHeight submenu (id 146) */
+       {
+               MenuHandle submenu;
+
+               /* Get the submenu */
+               submenu = GetMenu(SUBMENU_TILEHEIGHT);
+
+
+               /* Add some sizes */
+               for (i = 4, n = 1; i <= 32; i++, n++)
+               {
+                       Str15 buf;
+
+                       /* Textual size */
+                       strnfmt((char*)buf + 1, 15, "%d", i);
+                       buf[0] = strlen((char*)buf + 1);
+
+                       /* Append item */
+                       AppendMenu(submenu, buf);
+               }
+
+               /* Insert it */
+               SetMenuItemHierarchicalMenu(m, ITEM_TILEHEIGHT, submenu);
+       }
+
+#else
+
+       /* Special menu (id 134) */
+
+       /* Get graphics (sub)menu (id 143) */
+       m = GetMenu(SUBMENU_SOUND);
+
+       /* Insert it as a submenu */            
+       InsertMenu(m, hierMenu);
+
+
+       /* Get graphics (sub)menu (id 144) */
+       m = GetMenu(SUBMENU_GRAPH);
+
+       /* Insert it as a submenu */            
+       InsertMenu(m, hierMenu);
+
+
+       /* Get TileWidth (sub)menu (id 145) */
+       m = GetMenu(SUBMENU_TILEWIDTH);
 
        /* Add some sizes */
        for (i = 4; i <= 32; i++)
@@ -3314,15 +4881,11 @@ static void init_menubar(void)
        }
 
 
-       /* Make the "TileHeight" menu */
-       #ifdef JP
-       m = NewMenu(136, "\p¥¿¥¤¥ë¹â");
-       #else
-       m = NewMenu(136, "\pTileHeight");
-       #endif
+       /* Insert it as a submenu */
+       InsertMenu(m, hierMenu);
 
-       /* Insert the menu */
-       InsertMenu(m, 255);
+       /* Get TileHeight (sub)menu (id 146) */
+       m = GetMenu(SUBMENU_TILEHEIGHT);
 
        /* Add some sizes */
        for (i = 4; i <= 32; i++)
@@ -3338,6 +4901,10 @@ static void init_menubar(void)
        }
 
 
+       /* Insert the menu */
+       InsertMenu(m, hierMenu);
+
+#endif
        /* Update the menu bar */
        DrawMenuBar();
 }
@@ -3371,84 +4938,136 @@ static void setup_menus(void)
 
 
        /* File menu */
-       m = GetMenuHandle(129); //m = GetMHandle(129);
+       m = GetMenuHandle(MENU_FILE);
 
        /* Get menu size */
+#if TARGET_API_MAC_CARBON
+       n = CountMenuItems(m);
+#else
        n = CountMItems(m);
+#endif
 
        /* Reset menu */
        for (i = 1; i <= n; i++)
        {
                /* Reset */
+#if TARGET_API_MAC_CARBON
+               DisableMenuItem(m, i);
+               CheckMenuItem(m, i, FALSE);
+#else
                DisableItem(m, i);
                CheckItem(m, i, FALSE);
+#endif
        }
 
        /* Enable "new"/"open..."/"import..." */
        if (initialized && !game_in_progress)
        {
-               EnableItem(m, 1);
-               EnableItem(m, 2);
-               EnableItem(m, 3);
+#if TARGET_API_MAC_CARBON
+               EnableMenuItem(m, ITEM_NEW);
+               EnableMenuItem(m, ITEM_OPEN);
+               EnableMenuItem(m, ITEM_IMPORT);
+#else
+               EnableItem(m, ITEM_NEW);
+               EnableItem(m, ITEM_OPEN);
+               EnableItem(m, ITEM_IMPORT);
+#endif
        }
 
        /* Enable "close" */
        if (initialized)
        {
-               EnableItem(m, 4);
+#if TARGET_API_MAC_CARBON
+               EnableMenuItem(m, ITEM_CLOSE);
+#else
+               EnableItem(m, ITEM_CLOSE);
+#endif
        }
 
        /* Enable "save" */
        if (initialized && character_generated)
        {
-               EnableItem(m, 5);
+#if TARGET_API_MAC_CARBON
+               EnableMenuItem(m, ITEM_SAVE);
+#else
+               EnableItem(m, ITEM_SAVE);
+#endif
        }
 
        /* Enable "quit" */
        if (TRUE)
        {
-               EnableItem(m, 7);
-/*             EnableItem(m, 8); */
+#if TARGET_API_MAC_CARBON
+               EnableMenuItem(m, ITEM_QUIT);
+#else
+               EnableItem(m, ITEM_QUIT);
+#endif
        }
 
 
        /* Edit menu */
-       m = GetMenuHandle(130); //m = GetMHandle(130);
+       m = GetMenuHandle(MENU_EDIT);
 
        /* Get menu size */
+#if TARGET_API_MAC_CARBON
+       n = CountMenuItems(m);
+#else
        n = CountMItems(m);
+#endif
 
        /* Reset menu */
        for (i = 1; i <= n; i++)
        {
                /* Reset */
+#if TARGET_API_MAC_CARBON
+               DisableMenuItem(m, i);
+               CheckMenuItem(m, i, FALSE);
+#else
                DisableItem(m, i);
                CheckItem(m, i, FALSE);
+#endif
        }
 
        /* Enable "edit" options if "needed" */
        if (!td)
        {
-               EnableItem(m, 1);
-               EnableItem(m, 3);
-               EnableItem(m, 4);
-               EnableItem(m, 5);
-               EnableItem(m, 6);
+#if TARGET_API_MAC_CARBON
+               EnableMenuItem(m, ITEM_UNDO);
+               EnableMenuItem(m, ITEM_CUT);
+               EnableMenuItem(m, ITEM_COPY);
+               EnableMenuItem(m, ITEM_PASTE);
+               EnableMenuItem(m, ITEM_CLEAR);
+#else
+               EnableItem(m, ITEM_UNDO);
+               EnableItem(m, ITEM_CUT);
+               EnableItem(m, ITEM_COPY);
+               EnableItem(m, ITEM_PASTE);
+               EnableItem(m, ITEM_CLEAR);
+#endif
        }
 
 
        /* Font menu */
-       m = GetMenuHandle(131); //m = GetMHandle(131);
+       m = GetMenuHandle(MENU_FONT);
 
        /* Get menu size */
+#if TARGET_API_MAC_CARBON
+       n = CountMenuItems(m);
+#else
        n = CountMItems(m);
+#endif
 
        /* Reset menu */
        for (i = 1; i <= n; i++)
        {
                /* Reset */
+#if TARGET_API_MAC_CARBON
+               DisableMenuItem(m, i);
+               CheckMenuItem(m, i, FALSE);
+#else
                DisableItem(m, i);
                CheckItem(m, i, FALSE);
+#endif
        }
 
        /* Hack -- look cute XXX XXX */
@@ -3460,17 +5079,44 @@ static void setup_menus(void)
        /* Active window */
        if (td)
        {
+#if TARGET_API_MAC_CARBON
+               /* Enable "bold" */
+               EnableMenuItem(m, ITEM_BOLD);
+
+               /* Enable "extend" */
+               EnableMenuItem(m, ITEM_WIDE);
+
+               /* Check the appropriate "bold-ness" */
+               if (td->font_face & bold) CheckMenuItem(m, ITEM_BOLD, TRUE);
+
+               /* Check the appropriate "wide-ness" */
+               if (td->font_face & extend) CheckMenuItem(m, ITEM_WIDE, TRUE);
+
+               /* Analyze fonts */
+               for (i = 4; i <= n; i++)
+               {
+                       /* Enable it */
+                       EnableMenuItem(m, i);
+
+                       /* Analyze font */
+                       GetMenuItemText(m, i, s);
+                       GetFNum(s, &value);
+
+                       /* Check active font */
+                       if (td->font_id == value) CheckMenuItem(m, i, TRUE);
+               }
+#else
                /* Enable "bold" */
-               EnableItem(m, 1);
+               EnableItem(m, ITEM_BOLD);
 
                /* Enable "extend" */
-               EnableItem(m, 2);
+               EnableItem(m, ITEM_WIDE);
 
                /* Check the appropriate "bold-ness" */
-               if (td->font_face & bold) CheckItem(m, 1, TRUE);
+               if (td->font_face & bold) CheckItem(m, ITEM_BOLD, TRUE);
 
                /* Check the appropriate "wide-ness" */
-               if (td->font_face & extend) CheckItem(m, 2, TRUE);
+               if (td->font_face & extend) CheckItem(m, ITEM_WIDE, TRUE);
 
                /* Analyze fonts */
                for (i = 4; i <= n; i++)
@@ -3479,28 +5125,37 @@ static void setup_menus(void)
                        EnableItem(m, i);
 
                        /* Analyze font */
-                       /* GetMenuItemText(m,i,s); */
-                       GetMenuItemText(m, i, s);       //GetItem(m, i, s);
+                       GetMenuItemText(m, i, s);
                        GetFNum(s, &value);
 
                        /* Check active font */
                        if (td->font_id == value) CheckItem(m, i, TRUE);
                }
+#endif
        }
 
 
        /* Size menu */
-       m = GetMenuHandle(132); //m = GetMHandle(132);
+       m = GetMenuHandle(MENU_SIZE);
 
        /* Get menu size */
+#if TARGET_API_MAC_CARBON
+       n = CountMenuItems(m);
+#else
        n = CountMItems(m);
+#endif
 
        /* Reset menu */
        for (i = 1; i <= n; i++)
        {
                /* Reset */
+#if TARGET_API_MAC_CARBON
+               DisableMenuItem(m, i);
+               CheckMenuItem(m, i, FALSE);
+#else
                DisableItem(m, i);
                CheckItem(m, i, FALSE);
+#endif
        }
        
        /* Active window */
@@ -3510,36 +5165,330 @@ static void setup_menus(void)
                for (i = 1; i <= n; i++)
                {
                        /* Analyze size */
-                       /* GetMenuItemText(m,i,s); */
-                       GetMenuItemText(m, i, s);       //GetItem(m, i, s);
+                       GetMenuItemText(m, i, s);
                        s[s[0]+1] = '\0';
                        value = atoi((char*)(s+1));
 
-                       /* Enable the "real" sizes */
-                       if (RealFont(td->font_id, value)) EnableItem(m, i);
+#if TARGET_API_MAC_CARBON
+                       /* Enable the "real" sizes */
+                       if (RealFont(td->font_id, value)) EnableMenuItem(m, i);
+
+                       /* Check the current size */
+                       if (td->font_size == value) CheckMenuItem(m, i, TRUE);
+#else
+                       /* Enable the "real" sizes */
+                       if (RealFont(td->font_id, value)) EnableItem(m, i);
+
+                       /* Check the current size */
+                       if (td->font_size == value) CheckItem(m, i, TRUE);
+#endif
+               }
+       }
+
+
+       /* Windows menu */
+       m = GetMenuHandle(MENU_WINDOWS);
+
+       /* Get menu size */
+#if TARGET_API_MAC_CARBON
+       n = CountMenuItems(m);
+#else
+       n = CountMItems(m);
+#endif
+
+       /* Check active windows */
+       for (i = 1; i <= n; i++)
+       {
+               /* Check if needed */
+#if TARGET_API_MAC_CARBON
+               CheckMenuItem(m, i, data[i-1].mapped);
+#else
+               CheckItem(m, i, data[i-1].mapped);
+#endif
+       }
+
+
+       /* Special menu */
+       m = GetMenuHandle(MENU_SPECIAL);
+
+       /* Get menu size */
+#if TARGET_API_MAC_CARBON
+       n = CountMenuItems(m);
+#else
+       n = CountMItems(m);
+#endif
+
+       /* Reset menu */
+       for (i = 1; i <= n; i++)
+       {
+               /* Reset */
+#if TARGET_API_MAC_CARBON
+               DisableMenuItem(m, i);
+
+#ifdef MAC_MPW
+               /* XXX Oh no, this removes submenu... */
+               if ((i != ITEM_SOUND) &&
+                   (i != ITEM_GRAPH) &&
+                   (i != ITEM_TILEWIDTH) &&
+                   (i != ITEM_TILEHEIGHT)) CheckMenuItem(m, i, FALSE);
+#else
+
+               CheckMenuItem(m, i, FALSE);
+#endif
+
+#else
+               DisableItem(m, i);
+
+               /* XXX Oh no, this removes submenu... */
+               if ((i != ITEM_SOUND) &&
+                   (i != ITEM_GRAPH) &&
+                   (i != ITEM_TILEWIDTH) &&
+                   (i != ITEM_TILEHEIGHT)) CheckItem(m, i, FALSE);
+#endif
+       }
+
+#if TARGET_API_MAC_CARBON
+       /* Item "arg_sound" */
+       EnableMenuItem(m, ITEM_SOUND);
+       {
+               MenuRef submenu;
+
+#ifdef MAC_MPW
+
+               /* MPW's Universal Interface is a bit out of date */
+
+               /* Graphics submenu */
+               submenu = GetMenuHandle(SUBMENU_SOUND);
+
+#else
+
+               /* Graphics submenu */
+               (void)GetMenuItemHierarchicalMenu(m, ITEM_SOUND, &submenu);
+
+#endif
+
+               /* Get menu size */
+               n = CountMenuItems(submenu);
+
+               /* Reset menu */
+               for (i = 1; i <= n; i++)
+               {
+                       /* Reset */
+                       DisableMenuItem(submenu, i);
+                       CheckMenuItem(submenu, i, FALSE);
+               }
+
+               /* Item "Sound On/Off" */
+               EnableMenuItem(submenu, ITEM_USE_SOUND);
+               CheckMenuItem(submenu, ITEM_USE_SOUND, arg_sound);
+
+               /* Item "Sounf Config" */
+#ifndef MACH_O_CARBON
+               EnableMenuItem(submenu, ITEM_SOUND_SETTING);
+#endif
+       }
+
+       /* Item "Graphics" */
+       EnableMenuItem(m, ITEM_GRAPH);
+       {
+               MenuRef submenu;
+
+#ifdef MAC_MPW
+
+               /* MPW's Universal Interface is a bit out of date */
+
+               /* Graphics submenu */
+               submenu = GetMenuHandle(SUBMENU_GRAPH);
+
+#else
+
+               /* Graphics submenu */
+               (void)GetMenuItemHierarchicalMenu(m, ITEM_GRAPH, &submenu);
+
+#endif
+
+               /* Get menu size */
+               n = CountMenuItems(submenu);
+
+               /* Reset menu */
+               for (i = 1; i <= n; i++)
+               {
+                       /* Reset */
+                       DisableMenuItem(submenu, i);
+                       CheckMenuItem(submenu, i, FALSE);
+               }
+
+               /* Item "None" */
+               EnableMenuItem(submenu, ITEM_NONE);
+               CheckMenuItem(submenu, ITEM_NONE, (graf_mode == GRAF_MODE_NONE));
+
+               /* Item "8x8" */
+               EnableMenuItem(submenu, ITEM_8X8);
+               CheckMenuItem(submenu, ITEM_8X8, (graf_mode == GRAF_MODE_8X8));
+
+               /* Item "16x16" */
+               EnableMenuItem(submenu, ITEM_16X16);
+               CheckMenuItem(submenu, ITEM_16X16, (graf_mode == GRAF_MODE_16X16));
+
+               /* Item "Big tiles" */
+               EnableMenuItem(submenu, ITEM_BIGTILE);
+               CheckMenuItem(submenu, ITEM_BIGTILE, arg_bigtile);
+       }
+
+       /* Item "TileWidth" */
+       EnableMenuItem(m, ITEM_TILEWIDTH);
+       {
+               MenuRef submenu;
+
+#ifdef MAC_MPW
+
+               /* MPW's Universal Interface is a bit out of date */
+
+               /* TIleWidth submenu */
+               submenu = GetMenuHandle(SUBMENU_TILEWIDTH);
+
+#else
+
+               /* TileWidth submenu */
+               (void)GetMenuItemHierarchicalMenu(m, ITEM_TILEWIDTH, &submenu);
+
+#endif
+
+               /* Get menu size */
+               n = CountMenuItems(submenu);
+
+               /* Reset menu */
+               for (i = 1; i <= n; i++)
+               {
+                       /* Reset */
+                       DisableMenuItem(submenu, i);
+                       CheckMenuItem(submenu, i, FALSE);
+               }
+
+               /* Active window */
+               if (td)
+               {
+                       /* Analyze sizes */
+                       for (i = 1; i <= n; i++)
+                       {
+                               /* Analyze size */
+                               /* GetMenuItemText(m,i,s); */
+                               GetMenuItemText(submenu, i, s);
+                               s[s[0]+1] = '\0';
+                               value = atoi((char*)(s+1));
+
+                               /* Enable */
+                               if (value >= td->font_wid) EnableMenuItem(submenu, i);
+
+                               /* Check the current size */
+                               if (td->tile_wid == value) CheckMenuItem(submenu, i, TRUE);
+                       }
+               }
+       }
+
+       /* Item "TileHeight" */
+       EnableMenuItem(m, ITEM_TILEHEIGHT);
+       {
+               MenuRef submenu;
+
+#ifdef MAC_MPW
+
+               /* MPW's Universal Interface is a bit out of date */
+
+               /* TileHeight submenu */
+               submenu = GetMenuHandle(SUBMENU_TILEHEIGHT);
+
+#else
+
+               /* TileWidth submenu */
+               (void)GetMenuItemHierarchicalMenu(m, ITEM_TILEHEIGHT, &submenu);
+
+#endif
+
+               /* Get menu size */
+               n = CountMenuItems(submenu);
+
+               /* Reset menu */
+               for (i = 1; i <= n; i++)
+               {
+                       /* Reset */
+                       DisableMenuItem(submenu, i);
+                       CheckMenuItem(submenu, i, FALSE);
+               }
+
+               /* Active window */
+               if (td)
+               {
+                       /* Analyze sizes */
+                       for (i = 1; i <= n; i++)
+                       {
+                               /* Analyze size */
+                               /* GetMenuItemText(m,i,s); */
+                               GetMenuItemText(submenu, i, s);
+                               s[s[0]+1] = '\0';
+                               value = atoi((char*)(s+1));
+
+                               /* Enable */
+                               if (value >= td->font_hgt) EnableMenuItem(submenu, i);
 
-                       /* Check the current size */
-                       if (td->font_size == value) CheckItem(m, i, TRUE);
+                               /* Check the current size */
+                               if (td->tile_hgt == value) CheckMenuItem(submenu, i, TRUE);
+                       }
                }
        }
 
+       /* Item "arg_fiddle" */
+       EnableMenuItem(m, ITEM_FIDDLE);
+       CheckMenuItem(m, ITEM_FIDDLE, arg_fiddle);
 
-       /* Windows menu */
-       m = GetMenuHandle(133); //m = GetMHandle(133);
+       /* Item "arg_wizard" */
+       EnableMenuItem(m, ITEM_WIZARD);
+       CheckMenuItem(m, ITEM_WIZARD, arg_wizard);
+
+#else
+       /* Item "arg_sound" */
+       EnableItem(m, ITEM_SOUND);
+
+       /* Item "arg_graphics" */
+       EnableItem(m, ITEM_GRAPH);
+
+       /* Item "TileWidth" */
+       EnableItem(m, ITEM_TILEWIDTH);
+
+       /* Item "TileHeight" */
+       EnableItem(m, ITEM_TILEHEIGHT);
+
+       /* Item "arg_fiddle" */
+       EnableItem(m, ITEM_FIDDLE);
+       CheckItem(m, ITEM_FIDDLE, arg_fiddle);
+
+       /* Item "arg_wizard" */
+       EnableItem(m, ITEM_WIZARD);
+       CheckItem(m, ITEM_WIZARD, arg_wizard);
+
+       /* Sounds submenu */
+       m = GetMenuHandle(SUBMENU_SOUND);
 
        /* Get menu size */
        n = CountMItems(m);
 
-       /* Check active windows */
+       /* Reset menu */
        for (i = 1; i <= n; i++)
        {
-               /* Check if needed */
-               CheckItem(m, i, data[i-1].mapped);
+               /* Reset */
+               DisableItem(m, i);
+               CheckItem(m, i, FALSE);
        }
+       
+       /* Item "Sound On/Off" */
+       EnableItem(m, ITEM_USE_SOUND);
+       CheckItem(m, ITEM_USE_SOUND, arg_sound);
 
+       /* Item "Sound Config" */
+       EnableItem(m, ITEM_SOUND_SETTING);
 
-       /* Special menu */
-       m = GetMenuHandle(134); //m = GetMHandle(134);
+       /* Graphics submenu */
+       m = GetMenuHandle(SUBMENU_GRAPH);
 
        /* Get menu size */
        n = CountMItems(m);
@@ -3551,33 +5500,26 @@ static void setup_menus(void)
                DisableItem(m, i);
                CheckItem(m, i, FALSE);
        }
+       
+       /* Item "None" */
+       EnableItem(m, ITEM_NONE);
+       CheckItem(m, ITEM_NONE, (graf_mode == GRAF_MODE_NONE));
 
-       /* Item "arg_sound" */
-       EnableItem(m, 1);
-       CheckItem(m, 1, arg_sound);
-
-       /* Item "arg_graphics" */
-       EnableItem(m, 2);
-       CheckItem(m, 2, arg_graphics);
-
-       /* Item "arg_fiddle" */
-       EnableItem(m, 4);
-       CheckItem(m, 4, arg_fiddle);
+       /* Item "8x8" */
+       EnableItem(m, ITEM_8X8);
+       CheckItem(m, ITEM_8X8, (graf_mode == GRAF_MODE_8X8));
 
-       /* Item "arg_wizard" */
-       EnableItem(m, 5);
-       CheckItem(m, 5, arg_wizard);
+       /* Item "16x16" */
+       EnableItem(m, ITEM_16X16);
+       CheckItem(m, ITEM_16X16, (graf_mode == GRAF_MODE_16X16));
 
-#ifdef JP
-       /* Item "SoundSetting" */
-       EnableItem(m, 7);
-#endif
-       /* Item "Hack" */
-       /* EnableItem(m, 9); */
+       /* Item "Bigtile" */
+       EnableItem(m, ITEM_BIGTILE);
+       CheckItem(m, ITEM_BIGTILE, arg_bigtile);
 
 
-       /* TileWidth menu */
-       m = GetMenuHandle(135); //m = GetMHandle(135);
+       /* TIleWidth submenu */
+       m = GetMenuHandle(SUBMENU_TILEWIDTH);
 
        /* Get menu size */
        n = CountMItems(m);
@@ -3598,12 +5540,12 @@ static void setup_menus(void)
                {
                        /* Analyze size */
                        /* GetMenuItemText(m,i,s); */
-                       GetMenuItemText(m, i, s);       //GetItem(m, i, s);
+                       GetMenuItemText(m, i, s);
                        s[s[0]+1] = '\0';
                        value = atoi((char*)(s+1));
 
                        /* Enable */
-                       EnableItem(m, i);
+                       if (value >= td->font_wid) EnableItem(m, i);
 
                        /* Check the current size */
                        if (td->tile_wid == value) CheckItem(m, i, TRUE);
@@ -3611,8 +5553,8 @@ static void setup_menus(void)
        }
 
 
-       /* TileHeight menu */
-       m = GetMenuHandle(136); //m = GetMHandle(136);
+       /* TileHeight submenu */
+       m = GetMenuHandle(SUBMENU_TILEHEIGHT);
 
        /* Get menu size */
        n = CountMItems(m);
@@ -3632,18 +5574,19 @@ static void setup_menus(void)
                for (i = 1; i <= n; i++)
                {
                        /* Analyze size */
-                       /* GetMenuItemText(m,i,s); */
-                       GetMenuItemText(m, i, s);       //GetItem(m, i, s);
+                       GetMenuItemText(m, i, s);
                        s[s[0]+1] = '\0';
                        value = atoi((char*)(s+1));
 
                        /* Enable */
-                       EnableItem(m, i);
+                       if (value >= td->font_hgt) EnableItem(m, i);
 
                        /* Check the current size */
                        if (td->tile_hgt == value) CheckItem(m, i, TRUE);
                }
        }
+#endif
+
 }
 
 
@@ -3688,10 +5631,39 @@ static void menu(long mc)
        switch (menuid)
        {
                /* Apple Menu */
-               case 128:
+               case MENU_APPLE:
                {
                        /* About Angband... */
-                       if (selection == 1)
+#if TARGET_API_MAC_CARBON
+                       if (selection == ITEM_ABOUT)
+                       {
+                               DialogPtr dialog;
+                               short item_hit;
+
+                               /* Get the about dialogue */
+                               dialog=GetNewDialog(128, 0, (WindowPtr)-1);
+
+                               /* Move it to the middle of the screen */
+                               RepositionWindow(
+                                       GetDialogWindow(dialog),
+                                       NULL,
+                                       kWindowCenterOnMainScreen);
+
+                               /* Show the dialog */
+                               TransitionWindow(GetDialogWindow(dialog),
+                                       kWindowZoomTransitionEffect,
+                                       kWindowShowTransitionAction,
+                                       NULL);
+
+                               /* Wait for user to click on it */
+                               ModalDialog(0, &item_hit);
+
+                               /* Free the dialogue */
+                               DisposeDialog(dialog);
+                               break;
+                       }
+#else
+                       if (selection == ITEM_ABOUT)
                        {
                                DialogPtr dialog;
                                Rect r;
@@ -3699,50 +5671,50 @@ static void menu(long mc)
 
                                dialog=GetNewDialog(128, 0, (WindowPtr)-1);
 
-                       /*      r=dialog->portRect;
+                               r=dialog->portRect;
                                center_rect(&r, &qd.screenBits.bounds);
-                               MoveWindow(dialog, r.left, r.top, 1);*/
+                               MoveWindow(dialog, r.left, r.top, 1);
                                ShowWindow(dialog);
                                ModalDialog(0, &item_hit);
-                               DisposeDialog(dialog);          //DisposDialog(dialog);
+                               DisposeDialog(dialog);
                                break;
                        }
 
                        /* Desk accessory */
-                       /* GetMenuItemText(GetMHandle(128),selection,s); */
-                       GetMenuItemText(GetMenuHandle(128), selection, s);      //GetItem(GetMHandle(128), selection, s);
+                       GetMenuItemText(GetMenuHandle(MENU_APPLE), selection, s);
                        OpenDeskAcc(s);
                        break;
+#endif
                }
 
                /* File Menu */
-               case 129:
+               case MENU_FILE:
                {
                        switch (selection)
                        {
                                /* New */
-                               case 1:
+                               case ITEM_NEW:
                                {
                                        do_menu_file_new();
                                        break;
                                }
 
                                /* Open... */
-                               case 2:
+                               case ITEM_OPEN:
                                {
                                        do_menu_file_open(FALSE);
                                        break;
                                }
 
                                /* Import... */
-                               case 3:
+                               case ITEM_IMPORT:
                                {
                                        do_menu_file_open(TRUE);
                                        break;
                                }
 
                                /* Close */
-                               case 4:
+                               case ITEM_CLOSE:
                                {
                                        /* No window */
                                        if (!td) break;
@@ -3760,14 +5732,10 @@ static void menu(long mc)
                                }
 
                                /* Save */
-                               case 5:
+                               case ITEM_SAVE:
                                {
                                        if (!can_save){
-#ifdef JP
-                                               plog("º£¤Ï¥»¡¼¥Ö¤¹¤ë¤³¤È¤Ï½ÐÍè¤Þ¤»¤ó¡£");
-#else
-                                               plog("You may not do that right now.");
-#endif
+                                               plog(_("今はセーブすることは出来ません。", "You may not do that right now."));
                                                break;
                                        }
                                        
@@ -3781,24 +5749,24 @@ static void menu(long mc)
                                }
 
                                /* Quit (with save) */
-                               case 7:
+                               case ITEM_QUIT:
                                {
                                        /* Save the game (if necessary) */
                                        if (game_in_progress && character_generated)
                                        {
                                                if (!can_save){
-#ifdef JP
-                                                       plog("º£¤Ï¥»¡¼¥Ö¤¹¤ë¤³¤È¤Ï½ÐÍè¤Þ¤»¤ó¡£");
-#else
-                                                       plog("You may not do that right now.");
-#endif
+                                                       plog(_("今はセーブすることは出来ません。", "You may not do that right now."));
                                                        break;
                                                }
                                                /* Hack -- Forget messages */
                                                msg_flag = FALSE;
 
                                                /* Save the game */
+#if 0
                                                do_cmd_save_game(FALSE);
+#endif
+                                               Term_key_push(SPECIAL_KEY_QUIT);
+                                               break;
                                        }
 
                                        /* Quit */
@@ -3810,14 +5778,14 @@ static void menu(long mc)
                }
 
                /* Edit menu */
-               case 130:
+               case MENU_EDIT:
                {
                        /* Unused */
                        break;
                }
 
                /* Font menu */
-               case 131:
+               case MENU_FONT:
                {
                        /* Require a window */
                        if (!td) break;
@@ -3829,7 +5797,7 @@ static void menu(long mc)
                        activate(td->w);
 
                        /* Toggle the "bold" setting */
-                       if (selection == 1)
+                       if (selection == ITEM_BOLD)
                        {
                                /* Toggle the setting */
                                if (td->font_face & bold)
@@ -3841,10 +5809,9 @@ static void menu(long mc)
                                        td->font_face |= bold;
                                }
 
-#ifdef JP
                                /* Tile Width Hight Init */
                                td->tile_wid = td->tile_hgt = 0;
-#endif
+
                                /* Apply and Verify */
                                term_data_check_font(td);
                                term_data_check_size(td);
@@ -3857,7 +5824,7 @@ static void menu(long mc)
                        }
 
                        /* Toggle the "wide" setting */
-                       if (selection == 2)
+                       if (selection == ITEM_WIDE)
                        {
                                /* Toggle the setting */
                                if (td->font_face & extend)
@@ -3869,10 +5836,9 @@ static void menu(long mc)
                                        td->font_face |= extend;
                                }
 
-#ifdef JP
                                /* Tile Width Hight Init */
                                td->tile_wid = td->tile_hgt = 0;
-#endif
+
                                /* Apply and Verify */
                                term_data_check_font(td);
                                term_data_check_size(td);
@@ -3885,8 +5851,7 @@ static void menu(long mc)
                        }
 
                        /* Get a new font name */
-                       /* GetMenuItemText(GetMHandle(131), selection, s); */
-                       GetMenuItemText(GetMenuHandle(131), selection, s);              //GetItem(GetMHandle(131), selection, s);
+                       GetMenuItemText(GetMenuHandle(MENU_FONT), selection, s);
                        GetFNum(s, &fid);
 
                        /* Save the new font id */
@@ -3920,10 +5885,9 @@ static void menu(long mc)
                                }
                        }
 
-#ifdef JP
                        /* Tile Width Hight Init */
                        td->tile_wid = td->tile_hgt = 0;
-#endif
+
                        /* Apply and Verify */
                        term_data_check_font(td);
                        term_data_check_size(td);
@@ -3939,7 +5903,7 @@ static void menu(long mc)
                }
 
                /* Size menu */
-               case 132:
+               case MENU_SIZE:
                {
                        if (!td) break;
 
@@ -3949,15 +5913,13 @@ static void menu(long mc)
                        /* Activate */
                        activate(td->w);
 
-                       /* GetMenuItemText(GetMHandle(132), selection, s); */
-                       GetMenuItemText(GetMenuHandle(132), selection, s);      //GetItem(GetMHandle(132), selection, s);
+                       GetMenuItemText(GetMenuHandle(MENU_SIZE), selection, s);
                        s[s[0]+1]=0;
                        td->font_size = atoi((char*)(s+1));
 
-#ifdef JP
                        /* Tile Width Hight Init */
                        td->tile_wid = td->tile_hgt = 0;
-#endif
+
                        /* Apply and Verify */
                        term_data_check_font(td);
                        term_data_check_size(td);
@@ -3973,7 +5935,7 @@ static void menu(long mc)
                }
 
                /* Window menu */
-               case 133:
+               case MENU_WINDOWS:
                {
                        /* Parse */
                        i = selection - 1;
@@ -4003,11 +5965,32 @@ static void menu(long mc)
                }
 
                /* Special menu */
-               case 134:
+               case MENU_SPECIAL:
+               {
+                       switch (selection)
+                       {
+                               case ITEM_FIDDLE:
+                               {
+                                       arg_fiddle = !arg_fiddle;
+                                       break;
+                               }
+
+                               case ITEM_WIZARD:
+                               {
+                                       arg_wizard = !arg_wizard;
+                                       break;
+                               }
+                       }
+
+                       break;
+               }
+
+               /* Sounds submenu */
+               case SUBMENU_SOUND:
                {
                        switch (selection)
                        {
-                               case 1:
+                               case ITEM_USE_SOUND:
                                {
                                        /* Toggle arg_sound */
                                        arg_sound = !arg_sound;
@@ -4018,41 +6001,72 @@ static void menu(long mc)
                                        break;
                                }
 
-                               case 2:
+                               case ITEM_SOUND_SETTING:
                                {
-                                       /* Toggle arg_graphics */
-                                       arg_graphics = !arg_graphics;
+                                       SoundConfigDLog();
+
+                                       break;
+                               }
+                       }
+
+                       break;
+               }
 
-                                       /* Hack -- Force redraw */
-                                       Term_key_push(KTRL('R'));
+               /* Graphics submenu */
+               case SUBMENU_GRAPH:
+               {
+                       switch (selection)
+                       {
+                               case ITEM_NONE:
+                               {
+                                       graf_mode_req = GRAF_MODE_NONE;
 
                                        break;
                                }
 
-                               case 4:
+                               case ITEM_8X8:
                                {
-                                       arg_fiddle = !arg_fiddle;
+                                       graf_mode_req = GRAF_MODE_8X8;
+
                                        break;
                                }
 
-                               case 5:
+                               case ITEM_16X16:
                                {
-                                       arg_wizard = !arg_wizard;
+                                       graf_mode_req = GRAF_MODE_16X16;
+
                                        break;
                                }
-#ifdef JP
-                               case 7:
+
+                               case ITEM_BIGTILE:
                                {
-                                       SoundConfigDLog();
+                                       term *old = Term;
+                                       term_data *td = &data[0];
+
+                                       /* Toggle "arg_bigtile" */
+                                       arg_bigtile = !arg_bigtile;
+
+                                       /* Activate */
+                                       Term_activate(td->t);
+
+                                       /* Resize the term */
+                                       Term_resize(td->cols, td->rows);
+
+                                       /* Activate old */
+                                       Term_activate(old);
+
+                                       break;
                                }
-#endif
                        }
 
+                       /* Hack -- Force redraw */
+                       Term_key_push(KTRL('R'));
+
                        break;
                }
 
-               /* TileWidth menu */
-               case 135:
+               /* TileWidth submenu */
+               case SUBMENU_TILEWIDTH:
                {
                        if (!td) break;
 
@@ -4062,8 +6076,8 @@ static void menu(long mc)
                        /* Activate */
                        activate(td->w);
 
-                       /* GetMenuItemText(GetMHandle(135), selection, s); */
-                       GetMenuItemText(GetMenuHandle(135), selection, s);      //GetItem(GetMHandle(135), selection, s);
+                       /* Analyse value */
+                       GetMenuItemText(GetMenuHandle(SUBMENU_TILEWIDTH), selection, s);
                        s[s[0]+1]=0;
                        td->tile_wid = atoi((char*)(s+1));
 
@@ -4080,8 +6094,8 @@ static void menu(long mc)
                        break;
                }
 
-               /* TileHeight menu */
-               case 136:
+               /* TileHeight submenu */
+               case SUBMENU_TILEHEIGHT:
                {
                        if (!td) break;
 
@@ -4091,8 +6105,8 @@ static void menu(long mc)
                        /* Activate */
                        activate(td->w);
 
-                       /* GetMenuItemText(GetMHandle(136), selection, s); */
-                       GetMenuItemText(GetMenuHandle(136), selection, s);      //GetItem(GetMHandle(136), selection, s);
+                       /* Analyse value */
+                       GetMenuItemText(GetMenuHandle(SUBMENU_TILEHEIGHT), selection, s);
                        s[s[0]+1]=0;
                        td->tile_hgt = atoi((char*)(s+1));
 
@@ -4129,7 +6143,7 @@ static OSErr CheckRequiredAEParams(const AppleEvent *theAppleEvent)
        Size    actualSize;
 
        aeError = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard,
-                                   &returnedType, NULL, 0, &actualSize);
+                                   &returnedType, NULL, 0, &actualSize);
 
        if (aeError == errAEDescNotFound) return (noErr);
 
@@ -4143,7 +6157,7 @@ static OSErr CheckRequiredAEParams(const AppleEvent *theAppleEvent)
  * Apple Event Handler -- Open Application
  */
 static pascal OSErr AEH_Start(const AppleEvent *theAppleEvent,
-                              const AppleEvent *reply, long handlerRefCon)
+                             AppleEvent *reply, long handlerRefCon)
 {
 #pragma unused(reply, handlerRefCon)
 
@@ -4155,15 +6169,39 @@ static pascal OSErr AEH_Start(const AppleEvent *theAppleEvent,
  * Apple Event Handler -- Quit Application
  */
 static pascal OSErr AEH_Quit(const AppleEvent *theAppleEvent,
-                             const AppleEvent *reply, long handlerRefCon)
+                            AppleEvent *reply, long handlerRefCon)
 {
 #pragma unused(reply, handlerRefCon)
+#if TARGET_API_MAC_CARBON
+#pragma unused(theAppleEvent)
 
+       /* Save the game (if necessary) */
+       if (game_in_progress && character_generated)
+       {
+                       if (!can_save){
+                               plog(_("今はセーブすることは出来ません。", "You may not do that right now."));
+                               return;
+                       }
+                       /* Hack -- Forget messages */
+                       msg_flag = FALSE;
+
+                       /* Save the game */
+#if 0
+                       do_cmd_save_game(FALSE);
+#endif
+                       Term_key_push(SPECIAL_KEY_QUIT);
+                       return;
+               }
+
+               /* Quit */
+               quit(NULL);
+#else
        /* Quit later */
        quit_when_ready = TRUE;
 
        /* Check arguments */
        return (CheckRequiredAEParams(theAppleEvent));
+#endif
 }
 
 
@@ -4171,7 +6209,7 @@ static pascal OSErr AEH_Quit(const AppleEvent *theAppleEvent,
  * Apple Event Handler -- Print Documents
  */
 static pascal OSErr AEH_Print(const AppleEvent *theAppleEvent,
-                              const AppleEvent *reply, long handlerRefCon)
+                             AppleEvent *reply, long handlerRefCon)
 {
 #pragma unused(theAppleEvent, reply, handlerRefCon)
 
@@ -4193,8 +6231,8 @@ static pascal OSErr AEH_Print(const AppleEvent *theAppleEvent,
  * snippet from Think Reference 2.0.  (The prior sentence could read
  * "shamelessly swiped & hacked")
  */
-static pascal OSErr AEH_Open(AppleEvent *theAppleEvent,
-                             AppleEvent* reply, long handlerRefCon)
+static pascal OSErr AEH_Open(const AppleEvent *theAppleEvent,
+                            AppleEvent* reply, long handlerRefCon)
 {
 #pragma unused(reply, handlerRefCon)
 
@@ -4217,7 +6255,7 @@ static pascal OSErr AEH_Open(AppleEvent *theAppleEvent,
         */
 
        err = AEGetNthPtr(&docList, 1L, typeFSS, &keywd,
-                         &returnedType, (Ptr) &myFSS, sizeof(myFSS), &actualSize);
+                         &returnedType, (Ptr) &myFSS, sizeof(myFSS), &actualSize);
        if (err) return err;
 
        /* Only needed to check savefile type below */
@@ -4232,6 +6270,13 @@ static pascal OSErr AEH_Open(AppleEvent *theAppleEvent,
        /* Ignore non 'SAVE' files */
        if (myFileInfo.fdType != 'SAVE') return noErr;
 
+#ifdef MACH_O_CARBON
+
+       /* Extract a file name */
+       (void)spec_to_path(&myFSS, savefile, sizeof(savefile));
+
+#else
+
        /* XXX XXX XXX Extract a file name */
        PathNameFromDirID(myFSS.parID, myFSS.vRefNum, (StringPtr)savefile);
        pstrcat((StringPtr)savefile, (StringPtr)&myFSS.name);
@@ -4239,6 +6284,8 @@ static pascal OSErr AEH_Open(AppleEvent *theAppleEvent,
        /* Convert the string */
        ptocstr((StringPtr)savefile);
 
+#endif /* MACH_O_CARBON */
+
        /* Delay actual open */
        open_when_ready = TRUE;
 
@@ -4280,11 +6327,11 @@ static pascal OSErr AEH_Open(AppleEvent *theAppleEvent,
  * F7: 98
  * F3:99
  * F8:100
- * F10:101
+ * F10:109
  * F11:103
  * F13:105
  * F14:107
- * F9:109
+ * F9:101
  * F12:111
  * F15:113
  * Help:114
@@ -4347,16 +6394,19 @@ static bool CheckEvents(bool wait)
        /* Timestamp last check */
        lastTicks = curTicks;
 
+#if TARGET_API_MAC_CARBON
+       WaitNextEvent( everyEvent, &event, 1L, nil );
+#else
        /* Let the "system" run */
        SystemTask();
 
        /* Get an event (or null) */
        GetNextEvent(everyEvent, &event);
+#endif
 
        /* Hack -- Nothing is ready yet */
        if (event.what == nullEvent) return (FALSE);
 
-
        /* Analyze the event */
        switch (event.what)
        {
@@ -4420,9 +6470,6 @@ static bool CheckEvents(bool wait)
                                /* Hack -- Prepare the menus */
                                setup_menus();
 
-                               /* Mega-Hack -- allow easy exit if nothing to save */
-/*                             if (!character_generated && (ch=='Q' || ch=='q')) ch = 'e'; */
-
                                /* Run the Menu-Handler */
                                menu(MenuKey(ch));
 
@@ -4444,19 +6491,6 @@ static bool CheckEvents(bool wait)
                                Term_keypress(ch);
                        }
 
-                       /* Hack -- normal "keypad keys" -> special keypress */
-                       else if (!mc && !ms && !mo && !mx && (ck < 96))
-                       {
-                               /* Hack -- "enter" is confused */
-                               if (ck == 76) ch = '\n';
-
-                               /* Send control-caret as a trigger */
-                               Term_keypress(30);
-
-                               /* Send the "ascii" keypress */
-                               Term_keypress(ch);
-                       }
-
                        /* Bizarre key -> encoded keypress */
                        else if (ck <= 127)
                        {
@@ -4474,7 +6508,9 @@ static bool CheckEvents(bool wait)
                                Term_keypress('0' + (ck - 64) % 10);
 
                                /* Hack -- Terminate the sequence */
-                               Term_keypress(13);
+                               /* MPW can generate 10 or 13 for keycode of '\r' */
+                               /* -noMapCR option swaps '\r' and '\n' */
+                               Term_keypress('\r');
                        }
 
                        break;
@@ -4507,20 +6543,27 @@ static bool CheckEvents(bool wait)
                                        HiliteMenu(0);
                                        break;
                                }
-
+#if !TARGET_API_MAC_CARBON
                                case inSysWindow:
                                {
                                        SystemClick(&event, w);
                                        break;
                                }
+#endif
 
                                case inDrag:
                                {
                                        Point p;
 
                                        WindowPtr old_win;
-
-                                       r = qd.screenBits.bounds;
+                                       BitMap screen;
+                                       Rect portRect;
+#if TARGET_API_MAC_CARBON                                              
+                                       GetQDGlobalsScreenBits( &screen );
+#else
+                                       screen = qd.screenBits;
+#endif 
+                                       r = screen.bounds;
                                        r.top += 20; /* GetMBarHeight() XXX XXX XXX */
                                        InsetRect(&r, 4, 4);
                                        DragWindow(w, event.where, &r);
@@ -4535,9 +6578,17 @@ static bool CheckEvents(bool wait)
                                        activate(td->w);
 
                                        /* Analyze */
-                                       p.h = td->w->portRect.left;
-                                       p.v = td->w->portRect.top;
+#if TARGET_API_MAC_CARBON
+                                       GetWindowBounds( (WindowRef)td->w, kWindowContentRgn, &portRect );
+#else
+                                       portRect = td->w->portRect;
+                                       local_to_global( &portRect );
+#endif
+                                       p.h = portRect.left;
+                                       p.v = portRect.top;
+#if !TARGET_API_MAC_CARBON
                                        LocalToGlobal(&p);
+#endif
                                        td->r.left = p.h;
                                        td->r.top = p.v;
 
@@ -4573,18 +6624,24 @@ static bool CheckEvents(bool wait)
 
                                case inGrow:
                                {
-                                       int x, y;
+                                       s16b x, y;
 
                                        term *old = Term;
-
+                                       BitMap          screen;
+       
+#if TARGET_API_MAC_CARBON
+                                       GetQDGlobalsScreenBits( &screen );
+#else
+                                       screen = qd.screenBits;
+#endif
                                        /* Oops */
                                        if (!td) break;
 
                                        /* Fake rectangle */
                                        r.left = 20 * td->tile_wid + td->size_ow1;
-                                       r.right = 80 * td->tile_wid + td->size_ow1 + td->size_ow2 + 1;
+                                       r.right = screen.bounds.right;
                                        r.top = 1 * td->tile_hgt + td->size_oh1;
-                                       r.bottom = 24 * td->tile_hgt + td->size_oh1 + td->size_oh2 + 1;
+                                       r.bottom = screen.bounds.bottom;
 
                                        /* Grow the rectangle */
                                        newsize = GrowWindow(w, event.where, &r);
@@ -4602,7 +6659,6 @@ static bool CheckEvents(bool wait)
 
                                        /* Apply and Verify */
                                        term_data_check_size(td);
-
                                        /* Activate */
                                        Term_activate(td->t);
 
@@ -4633,6 +6689,9 @@ static bool CheckEvents(bool wait)
                /* Disk Event -- From "Maarten Hazewinkel" */
                case diskEvt:
                {
+
+#if TARGET_API_MAC_CARBON
+#else
                        /* check for error when mounting the disk */
                        if (HiWord(event.message) != noErr)
                        {
@@ -4643,7 +6702,7 @@ static bool CheckEvents(bool wait)
                                DIBadMount(p, event.message);
                                DIUnload();
                        }
-
+#endif
                        break;
                }
 
@@ -4657,8 +6716,17 @@ static bool CheckEvents(bool wait)
                                /* Resuming: activate the front window */
                                if (event.message & resumeFlag)
                                {
+#if TARGET_API_MAC_CARBON
+                                       Cursor  arrow;
+                                               
+                                       SetPortWindowPort( FrontWindow() );
+                                       
+                                       GetQDGlobalsArrow( &arrow );
+                                       SetCursor(&arrow);
+#else
                                        SetPort(FrontWindow());
                                        SetCursor(&qd.arrow);
+#endif
                                }
 
                                /* Suspend: deactivate the front window */
@@ -4678,11 +6746,14 @@ static bool CheckEvents(bool wait)
                /* From "Steve Linberg" and "Maarten Hazewinkel" */
                case kHighLevelEvent:
                {
+#if TARGET_API_MAC_CARBON
+                       AEProcessAppleEvent(&event);
+#else
                        /* Process apple events */
                        if (AEProcessAppleEvent(&event) != noErr)
                        {
                                #ifdef JP
-                               plog("Apple Event Handler¤Î¥¨¥é¡¼¤Ç¤¹.");
+                               plog("Apple Event Handlerのエラーです.");
                                #else
                                plog("Error in Apple Event Handler!");
                                #endif
@@ -4703,6 +6774,7 @@ static bool CheckEvents(bool wait)
 
                        /* Handle "open_when_ready" */
                        handle_open_when_ready();
+#endif
 
                        break;
                }
@@ -4780,7 +6852,7 @@ static vptr hook_rpanic(huge size)
 
 #pragma unused (size)
 
-       vptr mem = NULL;
+       /* vptr mem = NULL; */
 
        /* Free the lifeboat */
        if (lifeboat)
@@ -4793,7 +6865,7 @@ static vptr hook_rpanic(huge size)
 
                /* Mega-Hack -- Warning */
                #ifdef JP
-               mac_warning("¥á¥â¥ê¡¼¤¬Â­¤ê¤Þ¤»¤ó!\rº£¤¹¤°½ªÎ»¤·¤Æ²¼¤µ¤¤!");
+               mac_warning("メモリーが足りません!\r今すぐ終了して下さい!");
                #else
                mac_warning("Running out of Memory!\rAbort this process now!");
                #endif
@@ -4824,6 +6896,13 @@ static void hook_quit(cptr str)
        /* Warning if needed */
        if (str) mac_warning(str);
 
+#ifdef USE_ASYNC_SOUND
+
+       /* Clean up sound support */
+       cleanup_sound();
+
+#endif /* USE_ASYNC_SOUND */
+
        /* Write a preference file */
        save_pref_file();
 
@@ -4844,14 +6923,14 @@ static void hook_core(cptr str)
 
        /* Warn, then save player */
        #ifdef JP
-       mac_warning("Ã×̿Ū¤Ê¥¨¥é¡¼¤Ç¤¹.\r¶¯À©Åª¤Ë¥»¡¼¥Ö¤·¤Æ½ªÎ»¤·¤Þ¤¹.");
+       mac_warning("致命的なエラーです.\r強制的にセーブして終了します.");
        #else
        mac_warning("Fatal error.\rI will now attempt to save and quit.");
        #endif
 
        /* Attempt to save */
        #ifdef JP
-       if (!save_player()) mac_warning("·Ù¹ð -- ¥»¡¼¥Ö¤Ë¼ºÇÔ¤·¤Þ¤·¤¿!");
+       if (!save_player()) mac_warning("警告 -- セーブに失敗しました!");
        #else
        if (!save_player()) mac_warning("Warning -- save failed!");
        #endif
@@ -4883,19 +6962,29 @@ static void init_stuff(void)
 {
        int i;
 
+#if !TARGET_API_MAC_CARBON
        short vrefnum;
        long drefnum;
        long junk;
 
        SFTypeList types;
        SFReply reply;
+#endif
 
        Rect r;
        Point topleft;
 
        char path[1024];
 
+       BitMap screen;
+       Rect screenRect;
 
+#if TARGET_API_MAC_CARBON
+       OSErr err = noErr;
+       NavDialogOptions dialogOptions;
+       FSSpec theFolderSpec;
+       NavReplyRecord theReply;
+#endif
        /* Fake rectangle */
        r.left = 0;
        r.top = 0;
@@ -4903,7 +6992,12 @@ static void init_stuff(void)
        r.bottom = 188;
 
        /* Center it */
-       center_rect(&r, &qd.screenBits.bounds);
+#if TARGET_API_MAC_CARBON
+       screenRect = GetQDGlobalsScreenBits(&screen)->bounds;
+#else
+       screenRect = qd.screenBits.bounds;
+#endif
+       center_rect(&r, &screenRect);
 
        /* Extract corner */
        topleft.v = r.top;
@@ -4911,7 +7005,11 @@ static void init_stuff(void)
 
 
        /* Default to the "lib" folder with the application */
+#ifdef MACH_O_CARBON
+       if (locate_lib(path, sizeof(path)) == NULL) quit(NULL);
+#else
        refnum_to_name(path, app_dir, app_vol, (char*)("\plib:"));
+#endif
 
 
        /* Check until done */
@@ -4921,32 +7019,88 @@ static void init_stuff(void)
                init_file_paths(path);
 
                /* Build the filename */
-               #ifdef JP
-                       path_build(path, 1024, ANGBAND_DIR_FILE, "news_j.txt");
-               #else
-                       path_build(path, 1024, ANGBAND_DIR_FILE, "news.txt");
-               #endif
+               path_build(path, sizeof(path), ANGBAND_DIR_FILE, _("news_j.txt", "news.txt"));
 
                /* Attempt to open and close that file */
                if (0 == fd_close(fd_open(path, O_RDONLY))) break;
 
                /* Warning */
-               #ifdef JP
-                       plog_fmt("'%s' ¥Õ¥¡¥¤¥ë¤ò¥ª¡¼¥×¥ó½ÐÍè¤Þ¤»¤ó.", path);
-               #else
-                       plog_fmt("Unable to open the '%s' file.", path);
-               #endif
+               plog_fmt(_("'%s' ファイルをオープン出来ません.", "Unable to open the '%s' file."), path);
 
                /* Warning */
-               #ifdef JP
-                       plog("Hengband¤Î'lib'¥Õ¥©¥ë¥À¤¬Â¸ºß¤·¤Ê¤¤¤«Àµ¤·¤¯Ìµ¤¤²ÄǽÀ­¤¬¤¢¤ê¤Þ¤¹.");
-               #else
-                       plog("The Angband 'lib' folder is probably missing or misplaced.");
-               #endif
+               plog(_("Hengbandの'lib'フォルダが存在しないか正しく無い可能性があります.", "The Angband 'lib' folder is probably missing or misplaced."));
 
                /* Warning */
-               plog("Please 'open' any file in any sub-folder of the 'lib' folder.");
+               plog(_("Please 'open' any file in any sub-folder of the 'lib' folder.", "Please 'open' any file in any sub-folder of the 'lib' folder."));
+               
+#if TARGET_API_MAC_CARBON
+               /* Ask the user to choose the lib folder */
+               err = NavGetDefaultDialogOptions(&dialogOptions);
+
+               /* Paranoia */
+               if (err != noErr) quit(NULL);
+
+               /* Set default location option */
+               dialogOptions.dialogOptionFlags |= kNavSelectDefaultLocation;
+
+               /* Clear preview option */
+               dialogOptions.dialogOptionFlags &= ~(kNavAllowPreviews);
+
+               /* Forbit selection of multiple files */
+               dialogOptions.dialogOptionFlags &= ~(kNavAllowMultipleFiles);
+
+               /* Display location */
+               dialogOptions.location = topleft;
+
+               /* Load the message for the missing folder from the resource fork */
+               GetIndString(dialogOptions.message, 128, 1);
+
+               /* Wait for the user to choose a folder */
+               err = NavChooseFolder(
+                       nil,
+                       &theReply,
+                       &dialogOptions,
+                       nil,
+                       nil,
+                       nil);
+
+               /* Assume the player doesn't want to go on */
+               if ((err != noErr) || !theReply.validRecord) quit(NULL);
+
+               /* Retrieve FSSpec from the reply */
+               {
+                       AEKeyword theKeyword;
+                       DescType actualType;
+                       Size actualSize;
+
+                       /* Get a pointer to selected folder */
+                       err = AEGetNthPtr(
+                               &(theReply.selection),
+                               1,
+                               typeFSS,
+                               &theKeyword,
+                               &actualType,
+                               &theFolderSpec,
+                               sizeof(FSSpec),
+                               &actualSize);
+
+                       /* Paranoia */
+                       if (err != noErr) quit(NULL);
+               }
+
+               /* Free navitagor reply */
+               err = NavDisposeReply(&theReply);
 
+               /* Paranoia */
+               if (err != noErr) quit(NULL);
+
+               /* Extract textual file name for given file */
+               refnum_to_name(
+                       path,
+                       theFolderSpec.parID,
+                       theFolderSpec.vRefNum,
+                       (char *)theFolderSpec.name);
+#else
                /* Allow "text" files */
                types[0] = 'TEXT';
 
@@ -4965,6 +7119,7 @@ static void init_stuff(void)
                /* Extract textual file name for given file */
                GetWDInfo(reply.vRefNum, &vrefnum, &drefnum, &junk);
                refnum_to_name(path, drefnum, vrefnum, (char*)reply.fName);
+#endif
 
                /* Hack -- Remove the "filename" */
                i = strlen(path) - 1;
@@ -4985,20 +7140,23 @@ static void init_stuff(void)
 /*
  * Macintosh Main loop
  */
-void main(void)
+int main(void)
 {
        EventRecord tempEvent;
        int numberOfMasters = 10;
 
+#if !TARGET_API_MAC_CARBON
        /* Increase stack space by 64K */
        SetApplLimit(GetApplLimit() - 131072L);//65536L);
 
        /* Stretch out the heap to full size */
        MaxApplZone();
+#endif
 
        /* Get more Masters */
        while (numberOfMasters--) MoreMasters();
 
+#if !TARGET_API_MAC_CARBON
        /* Set up the Macintosh */
        InitGraf(&qd.thePort);
        InitFonts();
@@ -5006,6 +7164,7 @@ void main(void)
        InitMenus();
        /* TEInit(); */
        InitDialogs(NULL);
+#endif
        InitCursor();
 
 #ifdef JP
@@ -5035,6 +7194,19 @@ void main(void)
 
 # else
 
+#if TARGET_API_MAC_CARBON
+
+       {
+               OSErr err;
+               long response;
+
+               /* Check for existence of Carbon */
+               err = Gestalt(gestaltCarbonVersion, &response);
+
+               if (err != noErr) quit("This program requires Carbon API");
+       }
+
+#else
        /* Block */
        if (TRUE)
        {
@@ -5048,7 +7220,7 @@ void main(void)
                if ((err != noErr) || (versionNumber < 0x0700))
                {
                        #ifdef JP
-                       quit("¤³¤Î¥×¥í¥°¥é¥à¤Ï´Á»úTalk7.x.x°Ê¹ß¤ÇÆ°ºî¤·¤Þ¤¹.");
+                       quit("このプログラムは漢字Talk7.x.x以降で動作します.");
                        #else
                        quit("You must have System 7 to use this program.");
                        #endif
@@ -5064,7 +7236,7 @@ void main(void)
                if (SysEnvirons(1, &env) != noErr)
                {
                        #ifdef JP
-                       quit("SysEnvirons ¥³¡¼¥ë¤Ï¼ºÇÔ¤·¤Þ¤·¤¿¡ª");
+                       quit("SysEnvirons コールは失敗しました!");
                        #else
                        quit("The SysEnvirons call failed!");
                        #endif
@@ -5074,7 +7246,7 @@ void main(void)
                if (env.systemVersion < 0x0700)
                {
                        #ifdef JP
-                       quit("¤³¤Î¥×¥í¥°¥é¥à¤Ï´Á»úTalk7.x.x°Ê¹ß¤ÇÆ°ºî¤·¤Þ¤¹.");
+                       quit("このプログラムは漢字Talk7.x.x以降で動作します.");
                        #else
                        quit("You must have System 7 to use this program.");
                        #endif
@@ -5084,57 +7256,57 @@ void main(void)
                if (!env.hasColorQD)
                {
                        #ifdef JP
-                       quit("¤³¤Î¥×¥í¥°¥é¥à¤ÏColor Quickdraw¤¬Ìµ¤¤¤ÈÆ°ºî¤·¤Þ¤»¤ó.");
+                       quit("このプログラムはColor Quickdrawが無いと動作しません.");
                        #else
                        quit("You must have Color Quickdraw to use this program.");
                        #endif
                }
        }
 
-# endif
+#endif /* CARBON */
+#endif
 
 #endif /* ANGBAND_LITE_MAC */
 
+       /* 
+        * Remember Mac OS version, in case we have to cope with version-specific
+        * problems
+        */
+       (void)Gestalt(gestaltSystemVersion, &mac_os_version);
 
 #ifdef USE_SFL_CODE
-
        /* Obtain a "Universal Procedure Pointer" */
-       AEH_Start_UPP = NewAEEventHandlerProc(AEH_Start);
-
+       AEH_Start_UPP = NewAEEventHandlerUPP(AEH_Start);
        /* Install the hook (ignore error codes) */
        AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, AEH_Start_UPP,
-                             0L, FALSE);
+                             0L, FALSE);
 
        /* Obtain a "Universal Procedure Pointer" */
-       AEH_Quit_UPP = NewAEEventHandlerProc(AEH_Quit);
-
+       AEH_Quit_UPP = NewAEEventHandlerUPP(AEH_Quit);
        /* Install the hook (ignore error codes) */
        AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, AEH_Quit_UPP,
-                             0L, FALSE);
+                             0L, FALSE);
 
        /* Obtain a "Universal Procedure Pointer" */
-       AEH_Print_UPP = NewAEEventHandlerProc(AEH_Print);
-
+       AEH_Print_UPP = NewAEEventHandlerUPP(AEH_Print);
        /* Install the hook (ignore error codes) */
        AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, AEH_Print_UPP,
-                             0L, FALSE);
+                             0L, FALSE);
 
        /* Obtain a "Universal Procedure Pointer" */
-       AEH_Open_UPP = NewAEEventHandlerProc(AEH_Open);
-
+       AEH_Open_UPP = NewAEEventHandlerUPP(AEH_Open);
        /* Install the hook (ignore error codes) */
        AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, AEH_Open_UPP,
-                             0L, FALSE);
-
+                             0L, FALSE);
 #endif
 
-
+#ifndef MACH_O_CARBON
        /* Find the current application */
        SetupAppDir();
-
+#endif
 
        /* Mark ourself as the file creator */
-       _fcreator = 'Heng';
+       _fcreator = ANGBAND_CREATOR;
 
        /* Default to saving a "text" file */
        _ftype = 'TEXT';
@@ -5143,7 +7315,11 @@ void main(void)
 #if defined(__MWERKS__)
 
        /* Obtian a "Universal Procedure Pointer" */
+#if TARGET_API_MAC_CARBON
+       ynfilterUPP = NewModalFilterUPP(ynfilter);
+#else
        ynfilterUPP = NewModalFilterProc(ynfilter);
+#endif
 
 #endif
 
@@ -5158,8 +7334,8 @@ void main(void)
        quit_aux = hook_quit;
        core_aux = hook_core;
 
-BackColor(blackColor);
-               ForeColor(whiteColor);
+       BackColor(blackColor);
+       ForeColor(whiteColor);
 
        /* Show the "watch" cursor */
        SetCursor(*(GetCursor(watchCursor)));
@@ -5170,27 +7346,48 @@ BackColor(blackColor);
        /* Prepare the windows */
        init_windows();
 
-#ifdef JP
+#ifndef MACH_O_CARBON
+
        init_sound();
-#endif
 
+       init_graf();
+
+#endif
+       
        /* Hack -- process all events */
        while (CheckEvents(TRUE)) /* loop */;
 
        /* Reset the cursor */
-       SetCursor(&qd.arrow);
+#if TARGET_API_MAC_CARBON
+       {
+               Cursor  arrow;
+               GetQDGlobalsArrow( &arrow );
+               SetCursor(&arrow);
+       }
+#else
+       SetCursor( &qd.arrow );
+#endif
 
 
        /* Mega-Hack -- Allocate a "lifeboat" */
        lifeboat = NewPtr(16384);
 
+#ifdef USE_QT_SOUND
+
+       /* Load sound effect resources */
+       load_sounds();
+
+#endif /* USE_QT_SOUND */
+
        /* Note the "system" */
        ANGBAND_SYS = "mac";
-       ANGBAND_GRAF = "new";
 
        /* Initialize */
        init_stuff();
 
+       /* Catch nasty signals */
+       signals_init();
+
        /* Initialize */
        init_angband();
 
@@ -5211,11 +7408,7 @@ BackColor(blackColor);
 #endif
 
        /* Prompt the user */
-       #ifdef JP
-       prt("'¥Õ¥¡¥¤¥ë'¥á¥Ë¥å¡¼¤è¤ê'¿·µ¬'¤Þ¤¿¤Ï'³«¤¯...'¤òÁªÂò¤·¤Æ¤¯¤À¤µ¤¤¡£", 23, 10);
-       #else
-       prt("[Choose 'New' or 'Open' from the 'File' menu]", 23, 15);
-       #endif
+       prt(_("'ファイル'メニューより'新規'または'開く...'を選択してください。", "[Choose 'New' or 'Open' from the 'File' menu]"), 23, _(10, 15));
 
        /* Flush the prompt */
        Term_fresh();