mydialog.c

/*
    File:       mydialog.c
    
    Description:
 
    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 <strings.h>
#include <Dialogs.h>
#include <Windows.h>
#include <Fonts.h>
#include <Files.h>
#include <Resources.h>
#include <Memory.h>
#include <Sound.h>
 
#include "mydialog.h"
#include "DialogUtils.h"
#include "mydialog.h"
 
#define VOLNAME_DIALOG      256
#define DESTROY_DIALOG      257
#define FirstButton         1
#define SecondButton        2
#define StringField         3
 
static void strcpy255(char *dst, char *src);
 
extern void strcpy255(char *, char *);
 
 
/************************************************************************
 *
 *  Function:       strcpy255
 *
 *  Purpose:        copy a string
 *
 *  Returns:        nothing
 *
 *  Side Effects:   copies *src into *dst
 *
 *  Description:    loop copying until we hit the null byte of src.
 *                  We stop copying if we try to copy more than 255
 *                  chars at a time.
 *
 ************************************************************************/
static void
strcpy255(char *dst, char *src)
{
    short i = 0;
    while ( ( (*dst++ = *src++) != 0) & (i++ < 255) )
        /* nothing */ ;
}
 
/************************************************************************
 *
 *  Function:       AskForString
 *
 *  Purpose:        ask the user for a string.
 *
 *  Returns:        Boolean
 *                      true if the user entered something
 *                      false if the user asked to cancel out
 *
 *  Side Effects:
 *                  theString is filled with a C string if we return true.
 *
 *  Description:
 *                  put up a dialog.
 *
 ************************************************************************/
Boolean
AskForString(char *prompt, char *theString)
{
    DialogPtr       dPtr;
    short           result;
    
    short           unusedType; /* for hiliting okay button */
    Handle          hItem;
    Rect            dBox;
    
    Boolean         leaveYet;
    
    Str255          enteredString;
 
    dPtr = GetNewDialog(DU_CenterDLOG(VOLNAME_DIALOG), (DialogPeek)0L, (WindowPtr)-1);
    
    //HighLightDefault(dPtr);               /* hilite OK button */
    
    SelectDialogItemText(dPtr, StringField, 0, 999); /* all of the string field selected */
 
    ParamText((StringPtr)prompt, NULL, NULL, NULL);
    
    leaveYet = false;
    do
    {
        //HighLightDefault(dPtr);
        ModalDialog(0, &result);
        if (result == FirstButton) 
            leaveYet = true;
        if (result == SecondButton)
        {
            SysBeep(1);
            leaveYet = true;
            return false;
        }
    } while (leaveYet == false);
    
 
    GetDialogItem(dPtr, StringField, &unusedType, &hItem, &dBox);
    GetDialogItemText(hItem, (StringPtr)&enteredString);
    strcpy255(theString, P2CStr((StringPtr)&enteredString));
    DisposeDialog(dPtr);
    return true;
}
 
/************************************************************************
 *
 *  Function:       AskDestroyDisk
 *
 *  Purpose:        Check that you really want to nuke a disk
 *
 *  Returns:        Boolean
 *                  true = yes, destroy it
 *                  false = no, leave it alone, it was a mistake.
 *
 *  Side Effects:   none.
 *
 *  Description:    display our warning about destroying a disk.
 *                  Ask, using a modal dialog, whether the user really
 *                  wants to lose it.  (Default button is cancel, since
 *                  this is such a permanent thing...)
 *
 *
 ************************************************************************/
Boolean
AskDestroyDisk(short gDriveNumber)
{
    DialogPtr       dPtr;
    short           result;
    Boolean         leaveYet;
    Boolean         okayToDestroy;
    
    Str255          volumeName;
    short           vRefNum;
    long            freeBytes;
 
    okayToDestroy = true;
    dPtr = GetNewDialog(DU_CenterDLOG(DESTROY_DIALOG), (DialogPeek)0L, (WindowPtr)-1);
 
    //HighLightDefault(dPtr);
    
    if (GetVInfo(gDriveNumber, volumeName, &vRefNum, &freeBytes) != noErr)
        okayToDestroy = false;
    
    if (okayToDestroy)
    {
        ParamText((StringPtr)volumeName, NULL, NULL, NULL);
        
        leaveYet = false;
        do
        {
            //HighLightDefault(dPtr);
            ModalDialog(0, &result);
            if (result == SecondButton) 
                leaveYet = true;
            if (result == FirstButton)
            {
                SysBeep(1);
                leaveYet = true;
                okayToDestroy = false;
            }
        } while (leaveYet == false);
    }
 
    DisposeDialog(dPtr);
    return okayToDestroy;
}
 
 
 
/************************************************************************
 *
 *  Function:       Help
 *
 *  Purpose:        explain what we're doing
 *
 *  Returns:        void
 *
 *  Side Effects:   none.
 *
 *  Description:    display our help text.  Currently always returns true.
 *
 *
 ************************************************************************/
Boolean
Help(void)
{
    Handle  tHandle;
        
    tHandle = GetResource('TEXT', 1001);
    if (tHandle != (Handle)0)
    {
        HLock(tHandle);
        //TextDialog(1001, tHandle, times, 12, true);
        HUnlock(tHandle);
        ReleaseResource(tHandle);
    }
    return true;
}