AuntieDialogDemo.c

/*
    File            :   AuntieDialogDemo.c
    
    Description     :   Free of the Dialog Manager at last! See: "AuntieDialog.html"
 
    Author          :   PCG
 
    Copyright       :   © Copyright 1999-2000 Apple Computer, Inc. All rights reserved.
    
    Disclaimer      :   IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
                        ("Apple") in consideration of your agreement to the following terms, and your
                        use, installation, modification or redistribution of this Apple software
                        constitutes acceptance of these terms.  If you do not agree with these terms,
                        please do not use, install, modify or redistribute this Apple software.
 
                        In consideration of your agreement to abide by the following terms, and subject
                        to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs
                        copyrights in this original Apple software (the "Apple Software"), to use,
                        reproduce, modify and redistribute the Apple Software, with or without
                        modifications, in source and/or binary forms; provided that if you redistribute
                        the Apple Software in its entirety and without modifications, you must retain
                        this notice and the following text and disclaimers in all such redistributions of
                        the Apple Software.  Neither the name, trademarks, service marks or logos of
                        Apple Computer, Inc. may be used to endorse or promote products derived from the
                        Apple Software without specific prior written permission from Apple.  Except as
                        expressly stated in this notice, no other rights or licenses, express or implied,
                        are granted by Apple herein, including but not limited to any patent rights that
                        may be infringed by your derivative works or by other works in which the Apple
                        Software may be incorporated.
 
                        The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
                        WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
                        WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                        PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
                        COMBINATION WITH YOUR PRODUCTS.
 
                        IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
                        CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
                        GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                        ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
                        OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
                        (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
                        ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.          
*/
 
#include "AuntieDialog.h"
#include "AppearanceHelpers.h"
 
#ifdef __MWERKS__
 
// includes for MetroWerks CodeWarrior
 
#include <Sound.h>
#include <TextUtils.h>
 
#endif
 
enum
{
    kResID_Dialog_Mega                      = 6000,
    kResID_DialogItemList_AllowChangeFocus  = 8000,
    kResID_Dialog_NoChangeFocus             = 9000
};
 
enum
{
    kControlValue_Tabs_Classic = 1,
    kControlValue_Tabs_Sliders,
    kControlValue_Tabs_Text,
    kControlValue_Tabs_Progress,
    kControlValue_Tabs_Groups,
    kControlValue_Tabs_GrabBag
};
 
static          Boolean     gQuitting;
static const    SInt16      gResourceIDsForTabs [ ] = { 6003, 6002, 6004, 6001, 6005, 6006 };
 
static OSStatus PopulateListBox (WindowPtr window)
{
    OSStatus err = noErr;
 
    do
    {
        ListHandle  list;
        ControlRef  listControl;
        Cell        cell;
        SInt16      i;
        Str255      string;
        ControlID   controlID = { kAuntieDialogSignature, 8 };
 
        err = GetControlByID (window,&controlID,&listControl);
        if (err) break;
        err = GetListBoxListHandle (listControl, &list);
        if (err) break;
        
        cell.h = 0;
        
        for ( i = 1; true; i++ )
        {
            GetIndString( string, 130, i );
            if ( string[0] == 0 ) break;
        
            LAddRow( 1, (**list).dataBounds.bottom, list );
            cell.v = (SInt16) ((**list).dataBounds.bottom - 1);
            LSetCell( (Ptr)(string + 1), string[0], cell, list );
        }
    }
    while (false);
 
    return err;
}
 
static pascal OSStatus SetUpClassicTab (WindowRef window)
{
    OSStatus err = noErr;
 
    do
    {
        ControlID   controlID = { kAuntieDialogSignature, 3 };
        ControlRef  control;
 
        err = GetControlByID (window,&controlID,&control);
        if (err) break;
        err = SetDefaultControl (control,true);
        if (err) break;
        err = InvalControl (control,false);
        if (err) break;
    }
    while (false);
 
    return err;
}
 
static pascal OSStatus SetUpNewTab (ControlRef itemHit)
{
    OSStatus err = noErr;
 
    err = DisposeChildControls (itemHit);
 
    if (!err)
    {
        SInt16          tabIndex        = GetControlValue (itemHit);
        SInt16          resID           = gResourceIDsForTabs [tabIndex - 1];
        WindowRef       owningWindow    = GetControlOwner (itemHit);
 
        err = AppendDialogItemsAsControls (resID, owningWindow, nil, nil);
 
        if (!err)
        {
            ControlRef  control;
            ControlID   controlID;
 
            switch (tabIndex)
            {
                case kControlValue_Tabs_Classic :
 
                    err = SetUpClassicTab (owningWindow);
                    break;
 
                case kControlValue_Tabs_Text    :
 
                    err = PopulateListBox (owningWindow);
                    if (err) break;
                    err = AppendDialogItemsAsControls (kResID_DialogItemList_AllowChangeFocus, owningWindow, nil, nil);
                    break;
 
                case kControlValue_Tabs_Progress :
 
                    controlID.signature     = kAuntieDialogSignature;
                    controlID.id            = 3;
 
                    err = GetControlByID (owningWindow,&controlID,&control);
                    if (err) break;
                    err = SetProgressIndicatorState (control,false);
                    break;
            }
        }
    }
 
    return err;
}
 
static pascal OSStatus RespondToApplicationEvent (const EventRecord *event, RgnHandle mouseRgn)
{
    OSStatus err = noErr;
 
    if (event->what == mouseDown)
    {
        WindowRef   window;
        Rect        rect;
        BitMap      screenBits;
 
        switch (FindWindow (event->where, &window))
        {
            case inGoAway :
 
                if (TrackGoAway (window, event->where))
                {
                    gQuitting = true;
                }
                break;
 
            case inDrag :
 
                GetQDGlobalsScreenBits (&screenBits);
                rect = screenBits.bounds;
                InsetRect (&rect,4,4);
                DragWindow (window,event->where,&rect);
                SetEmptyRgn (mouseRgn);
                break;
        }
    }
 
    return err;
}
 
static pascal OSStatus FocusChangeValidatorControlCreationProc (ControlRef control, void *unknown)
{
#pragma unused (unknown)
    ControlID controlID;
 
    OSStatus err = GetControlID (control,&controlID);
 
    if (!err && controlID.id == 1)
    {
        err = SetPushButtonDefaultState (control,true);
    }
 
    return err;
}
 
static pascal OSStatus FocusChangeValidator (ControlRef control, Boolean *allow)
{
    OSStatus err = noErr;
 
    do
    {
        WindowRef   contrlOwner                 = GetControlOwner (control);
        ControlID   controlID                   = { kAuntieDialogSignature, 12 };
        ControlRef  allowFocusChangeCheckBox;
 
        err = GetControlByID (contrlOwner,&controlID,&allowFocusChangeCheckBox);
        if (err) break;
        *allow = (Boolean) GetControlValue (allowFocusChangeCheckBox);
 
        if (!*allow)
        {
            WindowRef   duhWindow;
            ControlRef  itemHit;
 
            err = GetAuntieDialog
                (kResID_Dialog_NoChangeFocus,kFirstWindowOfClass,
                    FocusChangeValidatorControlCreationProc,nil,&duhWindow);  
            if (err) break;
            err = AuntieModalDialog (duhWindow,&itemHit,nil,nil);
            DisposeWindow (duhWindow);
            if (err) break;
        }
    }
    while (false);
 
    return err;
}
 
static pascal OSStatus LetAuntieDialogRespondToEvent
    (RgnHandle mouseRgn, const EventRecord *event, WindowRef window)
{
    OSStatus err = noErr;
 
    do
    {
        ControlRef  tabsControl;
        SInt16      tabsControlValueBefore;
        ControlRef  itemHit;
        ControlID   controlID   = { kAuntieDialogSignature, 1 };
 
        err = GetControlByID (window,&controlID,&tabsControl);
        if (err) break;
 
        tabsControlValueBefore = GetControlValue (tabsControl);
 
        err = AuntieDialogSelect (event,mouseRgn,&itemHit,FocusChangeValidator);
        if (err || !itemHit) break;
 
        if (itemHit == tabsControl)
        {
            if (tabsControlValueBefore != GetControlValue (tabsControl))
            {
                err = SetUpNewTab (itemHit);
                if (err) break;
            }
        }
        else switch (GetControlValue (tabsControl))
        {
            case kControlValue_Tabs_Text :
 
                err = GetControlID (itemHit,&controlID);
                if (err) break;
 
                if (12 == controlID.id)
                {
                    SetControlValue (itemHit, !GetControlValue (itemHit));
                }
 
                break;
        }
    }
    while (false);
 
    return err;
}
 
static OSStatus EventLoop (WindowRef auntieDialogWindow)
{
    OSStatus err = noErr;
 
    RgnHandle mouseRgn = NewRgn ( );
 
    if (!mouseRgn)
    {
        err = QDError ( );
    }
    else
    {
        UInt32 adIdleTime = AuntieDialogGetIdleInterval (auntieDialogWindow);
 
        while (!gQuitting)
        {
            EventRecord event;
 
            UInt32 sleepTime = IsWindowHilited (auntieDialogWindow) ? adIdleTime : 0xFFFFFFFF;
 
            (void) WaitNextEvent (everyEvent, &event, sleepTime, mouseRgn);
 
            do
            {
                WindowRef window;
                err = IsAuntieDialogEvent (&event,&window);
                if (err) break;
 
                if (!window)
                {
                    err = RespondToApplicationEvent (&event,mouseRgn);
                }
                else if (window != auntieDialogWindow)
                {
                    DebugStr ("\punknown window produced by IsAuntieDialogEvent");
                }
                else
                {
                    err = LetAuntieDialogRespondToEvent (mouseRgn,&event,window);
                }
 
                if (err) break;
            }
            while (false);
        }
        DisposeRgn (mouseRgn);
    }
 
    return err;
}
 
int main (void)
{
    OSStatus err = noErr;
 
    WindowRef window;
 
    InsertMenu (GetMenu (147), -1);
 
    err = GetAuntieDialog (kResID_Dialog_Mega,kFirstWindowOfClass,nil,nil,&window);
 
    if (!err)
    {
        err = AppendDialogItemsAsControls (gResourceIDsForTabs [0], window, nil, nil);
 
        if (!err)
        {
            err = SetUpClassicTab (window);
 
            if (!err)
            {
                err = EventLoop (window);
            }
        }
 
        DisposeWindow (window);
    }
 
        return 0;
}