Application.c

/*
    File:       Application.c
 
    Contains:   DragWindowGrid- a sample showing how to write a function to drag a 
                window around on the screen so that it can only be released along
                grid lines.  No patches, no hooking into drag procs, nothing fancy.
                This just tracks an XOR frame around the screen similar to how the 
                Window Manager behaves, and when you let go in a different spot
                MoveWindow moves the window to the right place.
 
    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/5/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>
 
 
 
void InitApplication(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);
 
 
void DragWindowGrid(WindowPtr win, Point pt);
 
 
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;
    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)
                    {
                        DragWindowGrid(thisWindow, theEvent.where);
                    }
                    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;
    }
}