main.c

/*
    File:       main.c
    
    Description:This module contains all the Macintosh flow control
                for the ISO9660 floppy program.
 
    Author:
 
    Copyright:  Copyright: © 1990-1999 by Apple Computer, Inc.
                all rights reserved.
    
    Disclaimer: You may incorporate this sample code into your applications without
                restriction, though the sample code has been provided "AS IS" and the
                responsibility for its operation is 100% yours.  However, what you are
                not permitted to do is to redistribute the source as "DSC Sample Code"
                after having made changes. If you're going to re-distribute the source,
                we require that you make it clear in the source that the code was
                descended from Apple Sample Code, but that you've made changes.
    
    Change History (most recent first):
                6/24/99 Updated for Metrowerks Codewarror Pro 2.1(KG)
 
*/
 
#include <stdio.h>
#include <QuickDraw.h>
#include <Menus.h>
#include <Memory.h>
#include <Events.h>
#include <Windows.h>
#include <Dialogs.h>
#include <Desk.h>
#include <Files.h>
#include <ToolUtils.h>
#include <DiskInit.h>
#include <Fonts.h>
#include <Menus.h>
 
#include "HighSierra.h"
#include "BuildISO.h"
#include "DialogUtils.h"
#include "BuildISO.h"
#include "ErrorMsg.h"
#include "i_o.h"
 
QDGlobals   qd;
 
Str255          drvName = "\p.Sony";        /* name of the device driver for isoOpen() */
 
enum {
    AppleID = 128,
    FileID,
    EditID
};
 
enum {
    AppleM = 1,
    FileM,
    EditM
};
 
#define PROMPTID    258
 
#define HELP        1
#define CREATEFILES 2
#define QUIT        4
 
#define MENUCOUNT   3
 
#include "main.h"
#include "MyDialog.h"
 
static void DoDisk(long message);
static void InitializeVolume(void);
 
MenuHandle  gMenus[MENUCOUNT];
Boolean     gQuit;
EventRecord gEvent;
WindowPtr   gWindow;
 
short   gRefNum;
short   gDriveNumber = 0;
 
static void DoDisk(long);
static void InitializeVolume(void);
 
void SetUpMenus(void);
void DoAbout(void);
void DoCommand(long);
void HandleEvent(EventRecord *);
void main(void);
 
 
/************************************************************************
 *
 *  Function:       SetUpMenus
 *
 *  Purpose:        initialize menus
 *
 *  Returns:        nothing
 *
 *  Side Effects:   standard apple menus are set up
 *
 *  Description:    loop through MENUCOUNT times, inserting the menu
 *                  we got using GetMenu.  Draw the menu bar.
 *
 ************************************************************************/
void
SetUpMenus(void)
{
    short   i;
    
    gMenus[AppleM] = GetMenu(AppleID);
    AppendResMenu(gMenus[AppleM], 'DRVR');  /* add desk accessories */
    gMenus[FileM] = GetMenu(FileID);
    gMenus[EditM] = GetMenu(EditID);        /* only used by desk access. */
    
    for (i = 1; i <= MENUCOUNT; i++)
        InsertMenu(gMenus[i], 0);   /*install at end of Menu Bar */
 
    /* disable creating files until we initialize a floppy */
/***TO DO***    DisableItem(gMenus[FileM], CREATEFILES); */
    DrawMenuBar();
}
 
 
/************************************************************************
 *
 *  Function:       DoAbout
 *
 *  Purpose:        give ownership & copyright information
 *
 *  Returns:        nothing
 *
 *  Side Effects:   nothing
 *
 *  Description:    put up an alert, telling about us.
 *
 ************************************************************************/
void
DoAbout(void)
{
    Alert(DU_CenterALRT(128), 0L);
}
 
 
 
/************************************************************************
 *
 *  Function:       DoCommand
 *
 *  Purpose:        Handle command or command key equivalent
 *
 *  Returns:        void
 *
 *  Side Effects:   whatever it executes may have a side effect.
 *
 *  Description:    Look at the menu selected and the item of that menu,
 *                  and do a case statement on the item.  Execute the
 *                  appropriate item.
 *
 ************************************************************************/
void
DoCommand(long mResult)
{
    short   theItem;
    short   theMenu;
    Str255 name;
    GrafPtr savePort;
    
    theItem = mResult &0xFFFF;
    theMenu = (mResult >> 16) & 0xFFFF;
    
    switch (theMenu) {
    case AppleID:
        if (theItem == 1)    /* tell about the program */
            DoAbout();
        else {
            GetPort(&savePort);
            GetMenuItemText(gMenus[AppleM], theItem, name);
            OpenDeskAcc(name);
            SetPort(savePort);
        }
        break;
    case FileID:
        switch (theItem)
        {
            case    HELP:
                Help();
                break;
            case    CREATEFILES:
                CreatePathTable(gRefNum);
                CreateFiles(gRefNum);
                /* DisableItem(gMenus[FileM], CREATEFILES); */
                break;
            case    QUIT:
                gQuit = true;
                break;
        }
    case EditID:
        SystemEdit(theItem - 1);
        break;
    }
    HiliteMenu(0);
}
 
 
 
/************************************************************************
 *
 *  Function:       DoDisk
 *
 *  Purpose:        Handle disk inserted events
 *
 *  Returns:        nothing
 *
 *  Side Effects:   may initialize a disk
 *
 *  Description:    check the return code from the mount attempt (the
 *                  high word of the event message coming in.)  If the
 *                  mount failed, go ahead and initialize it using DIFormat.
 *                  This gets the basic format information, but no file
 *                  system information.
 *
 ************************************************************************/
 static void
 DoDisk(long message)
 {
    OSErr   result;
    CursHandle  cursor;
    
    gDriveNumber = message & 0xFFFF;
    result = (message >> 16) & 0xFFFF;
    
    /* if a mount failed (e.g. blank floppy) then we can format the floppy
    ** without doing anything.  If the mount succeeded, the floppy was in
    ** some format that the Mac recognized.  Unmount the volume and flush
    ** it out of the mac without ejecting it, and *then* format it.
    */
    if (result == noErr)
    {
        if (AskDestroyDisk(gDriveNumber) == true)
            result = UnmountVol(NULL, gDriveNumber);
        else
        {
            UnmountVol(NULL, gDriveNumber);
            Eject(NULL, gDriveNumber);
            return;             /* get rid of that volume and return now! */
        }
     }
    
    cursor = GetCursor(watchCursor);
    if (!cursor)
        SetCursor(*cursor);
    result = DIFormat(gDriveNumber);
    InitializeVolume();
    EnableItem(gMenus[FileM], CREATEFILES);
    SetCursor(&qd.arrow);
        
    if (result != noErr)
        ErrorMsg("Can't format volume! (result = %d)", result);
}
 
 
 
void
HandleEvent(EventRecord *gEvent)
{
    DialogPtr   dPtr;
    short       itemHit;
    
    switch (gEvent->what) {
    case mouseDown:
        switch (FindWindow(gEvent->where, &gWindow)) {
        case inSysWindow:
            SystemClick(gEvent, gWindow);
            break;
        case inMenuBar:
            DoCommand(MenuSelect(gEvent->where));
            break;
        }   /* end MouseDown */
        break;
    case keyDown:
    case autoKey:
        if ((gEvent->modifiers & cmdKey) != 0)
            DoCommand (MenuKey ((char) (gEvent->message & 0xFF)));
        break;
    case diskEvt:
        DoDisk(gEvent->message);
        break;
    case updateEvt:         /* These can only happen to our dialogs */
    case activateEvt:
        DialogSelect(gEvent, &dPtr, &itemHit);
    }   /* end gEvent.what */
}
 
/************************************************************************
 *
 *  Function:       main
 *
 *  Purpose:        control this puppy
 *
 *  Returns:        none    
 *
 *  Side Effects:   none inherient in this routine.  Routines called
 *                  from here do all the work.
 *
 *  Description:    Do standard Macintosh initialization.
 *
 ************************************************************************/
void
main(void)
{
    OSErr       result;
    DialogPtr   dPtr;
    
    
    InitGraf((Ptr)&qd.thePort);
    InitFonts();
    FlushEvents(everyEvent, 0);
    InitWindows();
    InitMenus();
    TEInit();
    InitDialogs(0L);
    InitCursor();
    MoreMasters();
    
    SetUpMenus();
 
    gRefNum = 0;
    result = isoOpen(drvName, &gRefNum);
    if (result != noErr)
    {
        ErrorMsg("isoOpen failed with code %d (0x%x)", result, result);
        return;
    }
 
    gQuit = false;
    
    dPtr = GetNewDialog(DU_CenterDLOG(PROMPTID), (DialogPeek)0L, (WindowPtr)-1L);
    do {
        SystemTask();
        if (GetNextEvent(everyEvent, &gEvent)) {
                HandleEvent(&gEvent);
        }   /* end if */
    } while (gQuit == false);
    DisposeDialog(dPtr);
 
    if (gDriveNumber != 0)
    {
        result = Eject(NULL, gDriveNumber);
        if (result != noErr)
            ErrorMsg("can't eject floppy! (%d)", result);
    }
}
 
 
/************************************************************************
 *
 *  Function:       InitializeVolume
 *
 *  Purpose:        initialize volume
 *
 *  Returns:        nothing
 *
 *  Side Effects:   destroys volume and create PVD and VDT instead
 *
 *  Description:
 *
 ************************************************************************/
static void
InitializeVolume(void)
{
    OSErr   result;
    
    result = ZeroDisk(gRefNum);
    if (result == noErr)
    {
        result = CreatePVD(gRefNum);
        if (result == noErr)
            result = CreateVDT(gRefNum);
    }
    if (result != noErr)
        ErrorMsg("Couldn't initialize volume");
}