SetDeskCPat.c

/*
    File:       SetDeskCPat.c
 
    Contains:   Simple code sample which demonstrates how to call SetDeskCPat properly.  
 
    Written by: Pete Gontier    
 
    Copyright:  Copyright © 1997-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/14/1999   Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
 
#define OLDROUTINELOCATIONS         0
#define OLDROUTINENAMES             0
#define SystemSevenOrLater          1
 
#include <Sound.h>
 
#ifndef __FONTS__
#   include <Fonts.h>
#endif
 
#ifndef __ERRORS__
#   include <Errors.h>
#endif
 
#ifndef __RESOURCES__
#   include <Resources.h>
#endif
 
#include "MoveableModalDialog.h"
 
enum
{
    kDialogItem_Button_None,
    kDialogItem_Button_Quit,
    kDialogItem_Button_RGB,
    kDialogItem_Button_Resource,
    kDialogItem_Button_NIL
};
 
static pascal OSErr InitMac (void)
{
    MaxApplZone ( );
    InitGraf (&(qd.thePort));
    InitFonts ( );
    InitWindows ( );
    InitMenus ( );
    TEInit ( );
    InitDialogs (nil);
 
    return noErr;
}
 
static pascal OSErr myMakeRGBPat
    (const RGBColor *color, Boolean inSysHeap, PixPatHandle *pphp)
{
    OSErr err = noErr;
 
    THz savedZone;
 
    if (inSysHeap)
    {
        savedZone = GetZone ( );
        SetZone (SystemZone ( ));
    }
 
    if (!(*pphp = NewPixPat ( )))
        err = QDError ( );
    else
    {
        MakeRGBPat (*pphp,color);
        err = QDError ( );
        if (err)
        {
            DisposePixPat (*pphp);
            *pphp = nil;
        }
    }
 
    if (inSysHeap)
        SetZone (savedZone);
 
    return err;
}
 
static pascal OSErr myGetPixPat (short resID, Boolean inSysHeap, PixPatHandle *pphp)
{
    OSErr err = noErr;
 
    Handle pixPatResH = GetResource ('ppat',resID);
    if (!(err = ResError ( )))
    {
        
        if (!pixPatResH)
            err = resNotFound;
        else
        {
            THz savedZone;
 
            if (inSysHeap)
            {
                savedZone = GetZone ( );
                SetZone (SystemZone ( ));
            }
 
            *pphp = GetPixPat (resID);
            if (!*pphp) err = QDError ( );
 
            if (inSysHeap)
                SetZone (savedZone);
        }
    }
 
    return err;
}
 
static pascal OSErr SetDeskCPatFromRGB (void)
{
    PixPatHandle pph = nil;
    RGBColor rgbGray = { 0x7FFF, 0x7FFF, 0x7FFF };
    OSErr err = myMakeRGBPat (&rgbGray,true,&pph);
    if (!err) SetDeskCPat (pph);
    return err;
}
 
static pascal OSErr SetDeskCPatFromResource (void)
{
    PixPatHandle pph = nil;
    OSErr err = myGetPixPat (128,true,&pph);
    if (!err) SetDeskCPat (pph);
    return err;
}
 
void main (void)
{
    if (InitMac ( ))
        SysBeep (10);
    else
    {
        DialogRef dlgRef = GetNewDialog (128,nil,(WindowRef)-1);
        if (dlgRef)
        {
            short itemHit;
 
            SetDialogDefaultItem (dlgRef,kDialogItem_Button_Quit);
 
            do
            {
                MoveableModalDialog (NewModalFilterProc(StdFilterProc),&itemHit);
 
                switch (itemHit)
                {
                    case kDialogItem_Button_RGB         :
 
                        (void) SetDeskCPatFromRGB ( );
                        break;
 
                    case kDialogItem_Button_Resource    :
 
                        (void) SetDeskCPatFromResource ( );
                        break;
 
                    case kDialogItem_Button_NIL         :
 
                        SetDeskCPat (nil);
                        break;
                }
            }
            while (itemHit != kDialogItem_Button_Quit);
 
            DisposeDialog (dlgRef);
        }
    }
}