2. CounterWindow/source/CCounterApp.cp

/*
    File:       CCounterApp.cp
 
    Contains:   Sample code to accompany Chapter 12 of 
                "An Introduction to Macintosh Programming for Windows Programmers".
                
    Written by: Worldwide Developer Technical Support
 
    Copyright:  1999 Apple Computer, Inc., All Rights Reserved
 
    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.
    
*/
#include "CCounterApp.h"
#include "CounterConstants.h"
 
#include <LStaticText.h>
 
#include <LGrowZone.h>
#include <LWindow.h>
#include <PP_Messages.h>
#include <PP_Resources.h>
#include <PPobClasses.h>
#include <UDrawingState.h>
#include <UMemoryMgr.h>
#include <URegistrar.h>
 
#include <UControlRegistry.h>
#include <UGraphicUtils.h>
#include <UEnvironment.h>
 
#ifndef __APPEARANCE__
#include <Appearance.h>
#endif
 
#include <Sound.h>
#include <ToolUtils.h>
 
// ===========================================================================
int main()
{
    SetDebugThrow_(debugAction_Alert);
    SetDebugSignal_(debugAction_Alert);
    InitializeHeap(3);                  // allocate 3 Master Pointer blocks
    UQDGlobals::InitializeToolbox(&qd);
    UEnvironment::InitEnvironment();
    new LGrowZone(20000);           // For low memory situations.
    CCounterApp theApp;         // stack allocation
    theApp.Run();
    return 0;
}
 
// ---------------------------------------------------------------------------
CCounterApp::CCounterApp()
{
    if ( UEnvironment::HasFeature( env_HasAppearance ) ) {
        ::RegisterAppearanceClient();
    }
    RegisterAllPPClasses(); // functions to create core PowerPlant classes
    UControlRegistry::RegisterClasses();    // Appearance Manager/GA classes
}
 
 
// ---------------------------------------------------------------------------
CCounterApp::~CCounterApp()
{
}
 
// ---------------------------------------------------------------------------
//  This function lets you do something when the application starts up
//  without a document.
void
CCounterApp::StartUp()
{
    LWindow* theWindow;
    theWindow = MakeControlsWindow();   
    ThrowIfNil_(theWindow );
}
 
// ---------------------------------------------------------------------------
//  Respond to commands from menus, buttons, etc.
Boolean
CCounterApp::ObeyCommand(CommandT   inCommand, void* ioParam)
{
    Boolean cmdHandled = true;
    switch (inCommand) {
        case cmd_Increment:
//          ::SysBeep(1);
            fCounter.Increment();
            fCaption->SetValue(fCounter.GetValue());
            break;
 
 
        case cmd_Decrement:
            fCounter.Decrement();
            fCaption->SetValue(fCounter.GetValue());
            break;
 
        default:
            cmdHandled = LApplication::ObeyCommand(inCommand, ioParam);
            break;
    }
    return cmdHandled;
}
 
// ---------------------------------------------------------------------------
//  This function enables menu commands.
void
CCounterApp::FindCommandStatus(
    CommandT    inCommand,
    Boolean     &outEnabled,
    Boolean     &outUsesMark,
    Char16      &outMark,
    Str255      outName)
{
    switch (inCommand) {
        // Return menu item status according to command messages.
        case cmd_New:
//          outEnabled = true;          // enable the New command
            break;
 
        case cmd_Increment:
            outEnabled = true;          // enable the button
            break;
 
 
        case cmd_Decrement:
            outEnabled = true;          // enable the button
            break;
 
        default:
            LApplication::FindCommandStatus(inCommand, outEnabled,
                                                outUsesMark, outMark, outName);
            break;
    }
}
 
// ---------------------------------------------------------------------------
LWindow*
CCounterApp::MakeControlsWindow()
{
    LWindow* theWindow = LWindow::CreateWindow(rWindow_Sample, this );
    
    fCaption = dynamic_cast<LCaption*>(theWindow->FindPaneByID(kCaption));
    ThrowIfNil_(fCaption);
        
    theWindow->Show();
    return theWindow;
}