Application.c

/*
    File:       Application.c
 
    Contains:   This shows how to obtain the color that the Drag Manager uses to
                hilite regions when ShowDragHilite is called.  Check out GetDragHilite.c
                for the code.  
 
                Please note this is only how it's done presently.  Since it is undocumented
                it can and will change in the future.
 
    Written by: Nitin Ganatra   
 
    Copyright:  Copyright © 1994-1999 by Apple Computer, Inc., All Rights Reserved.
 
                You may incorporate this Apple sample source code into your program(s) without
                restriction. This Apple sample source code has been provided "AS IS" and the
                responsibility for its operation is yours. You are not permitted to redistribute
                this Apple sample source code as "Apple sample source 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 source
                code, but that you've made changes.
 
    Change History (most recent first):
                8/6/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
 
#include <QuickDraw.h>
#include <Dialogs.h>
#include <Fonts.h>
#include <Processes.h>
#include <TextEdit.h>
#include <Events.h>
#include <Menus.h>
#include <Memory.h>
#include <Errors.h>
#include <ToolUtils.h>
#include <Resources.h>
 
 
void InitApplication(void);
void MyCreateNewWindow(void);
void MainEventLoop(void);
void MenuCommand(long whaHappened);
void DoAboutBox(void);
 
void PreEventLoop(void);
void PostEventLoop(void);
pascal void DrawWindowContent(short, short, GDHandle, long);
void DrawIt(WindowPtr win);
void DoUpdate(WindowPtr thisWindow);
 
static Boolean gDone;
 
 
/*-------------------------------------------------------------------------------------*/
 
void main()
{
 
    InitApplication();
    PreEventLoop();
    MainEventLoop();
}
 
 
/*-------------------------------------------------------------------------------------*/
 
void InitApplication()
{
    Handle theMenu;
 
    // Toolbox initialization
    MaxApplZone();
    InitGraf(&qd.thePort);
    InitFonts();
    InitWindows();
    InitMenus();
    TEInit();
    InitDialogs(nil);
    InitCursor();
    FlushEvents(0,everyEvent);
    
    // Application initialization
    gDone = false;
    
    theMenu = GetNewMBar(128);
    if ( theMenu == nil )
        goto MenuStuffFailed;
 
    SetMenuBar(theMenu);
    AppendResMenu(GetMenuHandle(128), 'DRVR');
    DrawMenuBar();
 
    return;
    
MenuStuffFailed:
    // If the menu stuff failed, something just ain't right (most likely some 
    // resources are missing.
    gDone = true;
    return;
 
}
 
 
/*-------------------------------------------------------------------------------------*/
 
void DoAboutBox()
{
    (void) Alert(128, nil);
}
 
 
/*-------------------------------------------------------------------------------------*/
 
void MainEventLoop()
{
    EventRecord     theEvent;
    WindowPtr       thisWindow;
    short           clickArea;
    Rect            screenRect;
    long            menuResult;
    char            charCode;
 
    while ( !gDone )
    {
        if ( WaitNextEvent(everyEvent, &theEvent, 0, nil) )
        {
            switch (theEvent.what)
            {
                case mouseDown:
                    clickArea = FindWindow(theEvent.where, &thisWindow);
                    
                    if (clickArea == inDrag)
                    {
                        screenRect = (**GetGrayRgn()).rgnBBox;
                        DragWindow(thisWindow, theEvent.where, &screenRect);
                    }
                    else if ( clickArea == inContent )
                    {
                        if ( thisWindow != FrontWindow() )
                            SelectWindow(thisWindow);
                    }
                    else if (clickArea == inGoAway)
                    {
                        if ( TrackGoAway(thisWindow, theEvent.where) )
                            gDone = true;
                    }
                    else if ( clickArea == inMenuBar ) 
                    {
                        menuResult = MenuSelect(theEvent.where);
                        if ( (menuResult  >> 16) != 0 )
                        {
                            MenuCommand(menuResult);
                            HiliteMenu(0);
                        }
                    }
                    break;
                case keyDown:
                    charCode = theEvent.message & charCodeMask;
 
                    if ( (theEvent.modifiers & cmdKey) != 0 ) 
                    {   
                        menuResult = MenuKey(charCode);
                
                        if ( (menuResult  >> 16) != 0 )
                            MenuCommand(menuResult);
                            
                    } 
                    break;
                case updateEvt:
                    thisWindow = (WindowPtr)theEvent.message;   
                    DoUpdate(thisWindow);
                
                    break;
                
            }
        }
    }
}
 
 
 
/*-------------------------------------------------------------------------------------*/
 
void MenuCommand(long whaHappened)
{
    short   menuID, menuItem;
    
    menuID = (whaHappened >> 16);
    menuItem = (whaHappened & 0xFFFF);
    
    if ( menuID == 128 )
    {
        if ( menuItem == 1)
            DoAboutBox();
    }
    else if ( menuID == 129 )
    {
        if (menuItem == 1)
            gDone = true;
    }
}