ColoredCheckBox.c

/*
    File:       ColoredCheckBox.c
 
    Contains:   This app demonstrates how to create a check box on the      
                gray window backgrounds.The key to this is making sure  
                that the background color for the window is set.            
                a 'wctb' was created using ResEdit which provides the       
                gray content for the window.
 
    Written by: Larry Lai   
 
    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):
                7/19/1999   Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
 
#include <Sound.h>
#include <Fonts.h>
#include <Windows.h>
#include <Menus.h>
#include <Events.h>
#include <Resources.h>
 
#define rColoredCheckBoxWindow      128
#define rCheckBox                   128
#define kSoundID                    128
 
Boolean gQuit;
 
void InitToolbox(void);
void MyCreateColoredCheckBoxWindow();
void DoEventLoop();
void DoContentClick(WindowPtr, EventRecord);
void DoCheckBox(Point, ControlHandle);
 
void InitToolbox()
{
    InitGraf((Ptr) &qd.thePort);
    InitFonts();
    InitWindows();
    InitMenus();
    FlushEvents(everyEvent,0);
    InitDialogs(0L);
    InitCursor();
}
 
void main()
{
    
    InitToolbox();
    
    MyCreateColoredCheckBoxWindow();
 
    DoEventLoop();
}
 
void MyCreateColoredCheckBoxWindow()
{
    WindowPtr       myWindow;
    
    myWindow = GetNewCWindow(rColoredCheckBoxWindow, nil, (WindowPtr) -1);
    if (myWindow != nil)
    {
        SetPort (myWindow);
        GetNewControl(rCheckBox, myWindow);
    }
    else
        SysBeep(10);
        
}
 
void DoEventLoop()
{
    EventRecord anEvent;
    WindowPtr   evtWind;
    short       clickArea;
    Rect        screenRect;
 
    do{
        if (WaitNextEvent( everyEvent, &anEvent, 0, nil ))
        {
            if (anEvent.what == mouseDown)
            {
                clickArea = FindWindow( anEvent.where, &evtWind );
                
                if (clickArea == inDrag)
                {
                    screenRect = (**GetGrayRgn ()).rgnBBox;
                    DragWindow( evtWind, anEvent.where, &screenRect );
                }
                else if (clickArea == inContent)
                {
                    if (evtWind != FrontWindow())
                        SelectWindow( evtWind );
                    else
                        DoContentClick(evtWind, anEvent);   
                    }
                else if (clickArea == inGoAway)
                    if (TrackGoAway( evtWind, anEvent.where ))
                        gQuit = true;
            }
            else if (anEvent.what == updateEvt)
            {
                evtWind = (WindowPtr)anEvent.message;   
                SetPort( evtWind );
                
                BeginUpdate( evtWind );
                UpdateControls(evtWind, evtWind->visRgn);
                EndUpdate (evtWind);
            }
        }
    }while (!gQuit);
}
 
    
void DoContentClick(WindowPtr theWindow, EventRecord theEvent)
{
    Point           mouse;
    ControlHandle   control;
    short           part;
    
    SetPort(theWindow);
    mouse = theEvent.where; /*Get the mouse location*/
    GlobalToLocal(&mouse);  /*convert to local coordinates*/
    part = FindControl(mouse, theWindow, &control);
 
    if(part == kControlCheckBoxPart)
        DoCheckBox(mouse, control);
 
}
 
void DoCheckBox(Point mouse, ControlHandle control)
{
    short   checkbox;
    Handle  theSound;
 
    if (TrackControl(control, mouse, nil))      /*user clicks checkbox*/
    {
        checkbox = GetControlValue(control);        /*get last value of checkbox*/
        checkbox = 1- checkbox;                 /*toggle value of checkbox*/
        SetControlValue(control, checkbox);         /*set checkbox to new value*/
        if(checkbox == 1)                       /*express yourself next time user clicks the checkbox*/
        {
            theSound = GetResource('snd ', kSoundID);
            if (theSound != nil) {
                SndPlay(nil, (SndListHandle)theSound, false);
                ReleaseResource(theSound);
            }
        }
    }
}