MakeITable.c

/*
    File:       MakeITable.c
 
    Contains:   This is a very boring app which just forces QuickDraw to update the ITable of
                all the monitors.  The Resolution will end up at 5 bits.  The ITable will change
                when you select start from the file menu.
 
 
    Written by: RICHARD P. COLLYER  
 
    Copyright:  Copyright © 1987-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/9/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
 
#include    <CType.h>
#include    <Quickdraw.h>
#include    <QDOffscreen.h>
#include    <Windows.h>
#include    <OSUtils.h>
#include    <Controls.h>
#include    <desk.h>
#include    <dialogs.h>
#include    <Events.h>
#include    <Memory.h>
#include    <Menus.h>
#include    <Notification.h>
#include    <OSEvents.h>
#include    <Packages.h>
#include    <Palettes.h>
#include    <Printing.h>
#include    <Resources.h>
#include    <SegLoad.h>
#include    <Sound.h>
#include    <Slots.h>
#include    <ToolUtils.h>
#include    <StdIO.h>
#include    <math.h>
 
extern _DataInit();
 
#define TRUE            0xFF
#define FALSE           0
#define VERSION         1
#define SR_BIT          0
 
#define appleID         128         
#define appleMenu       0
#define aboutMeCommand  1
 
#define fileID          129
#define startCommand    1
#define printCommand    3
#define quitCommand     5
 
#define editID          130
 
#define aboutMeDLOG     128
#define okButton        1
#define authorItem      2
#define languageItem    3
 
long                Tick, WhereBoard;
int                 yieldTime, err, numcolor, offLeft, offTop, offRight, offBottom;
Rect                screenRect, BaseRect, TotalRect, minRect;
WindowPtr           whichWindow, myWindow;
CTabHandle          mycolors;
PaletteHandle       srcPalette;
MenuHandle          mymenu1, mymenu2, mymenu0;
EventRecord         myEvent;
Boolean             DoneFlag, Color, CPU20, FPU, Sys6, Memboard;
GDHandle            theGDevice;
SysEnvRec           theWorld;
OSErr               OSys;
SpBlockPtr          mySpBlockPtr;
SpBlock             mySpBlock;
 
void start();
void DrawStuff (Rect theRect);
void Print();
void showAboutMeDialog();
void doCommand(long mResult);
void init();
/*______________________________________________________*/
/*                What ever You want                    */
/*______________________________________________________*/
void start()
{
    GDHandle    theGDHand;
    
    theGDHand = GetDeviceList();
    do {
        HLock ((Handle) theGDHand);
        if (TestDeviceAttribute (theGDHand, 13) && 
                TestDeviceAttribute (theGDHand, 15) &&
                (*theGDHand)->gdType == 0) {
            (*theGDHand)->gdResPref = 5;
            (*(*theGDHand)->gdITable)->iTabSeed = GetCTSeed();
            GDeviceChanged (theGDHand);
            HUnlock ((Handle) theGDHand);
            }
        } while ((theGDHand = GetNextDevice(theGDHand)) != nil);
 
    return;
}
 
/*______________________________________________________*/
/*                     Draw Stuff                       */
/*______________________________________________________*/
void DrawStuff (Rect theRect)
{
    SetRect (&theRect, 10, 10, 200, 200);
    
    CopyBits (&qd.screenBits, &qd.thePort->portBits, &qd.screenBits.bounds, 
                &qd.screenBits.bounds, srcCopy, nil);
 
 
/**
    CopyBits (&window->portBits, &window->portBits, &window->portRect, &window->portRect, srcOr, nil);
    CopyBits (&windpeek->port.portBits, &windpeek->port.portBits, &window->portRect, &window->portRect, srcOr, nil);
    CopyBits (&screenBits, &(*thePort).portBits, &theWorld, &theWorld, srcOr, nil);
*/
    return;
}  /* DrawStuff */
 
/*______________________________________________________*/
/*                        Print                         */
/*______________________________________________________*/
void Print()
{
    TPPrPort    thePrPort;
    TPrStatus   theStatus;
    GrafPtr     oldPort;
    THPrint     thePrRecHdl;
    
    GetPort(&oldPort);
    
    thePrRecHdl = (THPrint)  NewHandle (sizeof (TPrint));
    
    PrOpen();
 
        if (PrError() == noErr)
         {
            PrintDefault(thePrRecHdl);
            
            if (! PrStlDialog(thePrRecHdl)) 
              ExitToShell();
              
            if (! PrJobDialog(thePrRecHdl)) 
              ExitToShell();
              
            thePrPort = PrOpenDoc(thePrRecHdl, nil, nil);
        
            if (PrError() == noErr) 
             {
                PrOpenPage(thePrPort, nil);
                if (PrError() == noErr) 
                 {
                    /********* Print from here ****************/
                    
                    DrawStuff ((**thePrRecHdl).prInfo.rPage);
                 }
             }
            PrClosePage(thePrPort);
        }
        PrCloseDoc(thePrPort);
    
    if ((((TPPrint)*thePrRecHdl)->prJob.bJDocLoop == bSpoolLoop) && (PrError() == noErr))
        PrPicFile(thePrRecHdl, nil, nil, nil, &theStatus);                
 
    PrClose();
 
    SetPort(oldPort);
    return;
}
 
/*______________________________________________________*/
/*                 About Prog Dialog                    */
/*______________________________________________________*/
void showAboutMeDialog()
{
    GrafPtr     savePort;
    DialogPtr   theDialog;
    short       itemHit;
 
    GetPort(&savePort);
    theDialog = GetNewDialog(aboutMeDLOG, nil, (WindowPtr) -1);
    SetPort(theDialog);
 
    do {
        ModalDialog(nil, &itemHit);
    } while (itemHit != okButton);
 
    CloseDialog(theDialog);
 
    SetPort(savePort);
    return;
}
 
/*______________________________________________________*/
/*                 Do Menu Function                     */
/*______________________________________________________*/
void doCommand(long mResult)
{
    int                     theMenu, theItem;
    Str255                  daName;
    GrafPtr                 savePort;
 
    theItem = LoWord(mResult);
    theMenu = HiWord(mResult);
    
    switch (theMenu) {
/*______________________________________________________*/
/*                    Do Apple Menu                     */
/*______________________________________________________*/
        case appleID:
            if (theItem == aboutMeCommand)
                showAboutMeDialog();
            else {
                GetMenuItemText(mymenu0, theItem, daName);
                GetPort(&savePort);
                (void) OpenDeskAcc(daName);
                SetPort(savePort);
            }
            break;
/*______________________________________________________*/
/*                     Do File Menu                     */
/*______________________________________________________*/
        case fileID:
            switch (theItem) {
                case startCommand:
                    start();
                    break;
                case printCommand:
                    Print();
                    break;
                case quitCommand:
                    DoneFlag = TRUE;
                    break;
                default:
                    break;
                }
            break;
/*______________________________________________________*/
/*                     Do Edit Menu                     */
/*______________________________________________________*/
        case editID:
            switch (theItem) {
                default:
                    break;
                }
            break;
        default:
            break;
        }
    HiliteMenu(0);
    return;
    
}
 
 
 
/*______________________________________________________*/
/*               Initialization traps                   */
/*______________________________________________________*/
void init()
{
    RgnHandle       tempRgn;
    
    UnloadSeg(_DataInit);
    InitGraf(&qd.thePort);
    FlushEvents(everyEvent, 0);
    InitWindows();
    InitDialogs(nil);
    InitCursor();
/*______________________________________________________*/
/*            If not right Machine then stop            */
/*______________________________________________________*/
/*  OSys = SysEnvirons(VERSION,&theWorld);
    if(!theWorld.hasColorQD)
        Color = TRUE;
    else
        Color = FALSE;
        
    if(theWorld.processor >= env68020)
        CPU20 = TRUE;
    else
        CPU20 = FALSE;
        
    if(!theWorld.hasFPU)
        FPU = TRUE;
    else
        FPU = FALSE;
        
    if(theWorld.systemVersion < 0x0600)
        Sys6 = TRUE;
    else
        Sys6 = FALSE;
        
    if (CPU20 && (GetTrapAddress(0xA06E) != GetTrapAddress(0xA89F))) {
        mySpBlockPtr = &mySpBlock;
        mySpBlockPtr->spSlot = 0;
        mySpBlockPtr->spTBMask = 0x03;
        mySpBlockPtr->spCategory = 9;
        mySpBlockPtr->spCType = 2;
                
        err = SNextTypeSRsrc (mySpBlockPtr);
                
        if (err) {
            mySpBlockPtr = &mySpBlock;
            mySpBlockPtr->spSlot = 0;
            mySpBlockPtr->spTBMask = 0x03;
            mySpBlockPtr->spCategory = 9;
            mySpBlockPtr->spCType = F;
                
            err = SNextTypeSRsrc (mySpBlockPtr);
                
            if (err)
                Memboard = FALSE;
            else {
                Memboard = TRUE;
                WhereBoard = mySpBlockPtr->spSlot;
                }
            }
        else {
            Memboard = TRUE;
            WhereBoard = mySpBlockPtr->spSlot;
            }
        }*/
                    
/*______________________________________________________*/
/*                     Set Rects                        */
/*______________________________________________________*/
    screenRect = qd.screenBits.bounds;
    offLeft = 0;
    offTop = 0;
    offRight = screenRect.right;
    offBottom = screenRect.bottom;
    SetRect(&BaseRect, 40, 60, 472, 282);
    tempRgn = GetGrayRgn();
    HLock ((Handle) tempRgn);
    TotalRect = (**tempRgn).rgnBBox;
    SetRect(&minRect, 80, 80, (**tempRgn).rgnBBox.right - 40, 
                (**tempRgn).rgnBBox.bottom - 40);
    HUnlock ((Handle) tempRgn);
 
/*______________________________________________________*/
/*        Open Window & set Palette & Picture           */
/*______________________________________________________*/
    theGDevice = GetMainDevice();
    HLock ((Handle) theGDevice);
    mycolors = (**(**theGDevice).gdPMap).pmTable;
    numcolor = (**(**theGDevice).gdPMap).pixelSize;
    HUnlock((Handle) theGDevice);
    switch (numcolor) {
        case 1:
            numcolor = 2;
            break;
        case 2:
            numcolor = 4;
            break;
        case 4:
            numcolor = 16;
            break;
        case 8:
            numcolor = 256;
            break;
        }
    
    myWindow = NewCWindow(nil, &BaseRect, "\p", TRUE, zoomDocProc, 
                            (WindowPtr) -1, TRUE, 150);
    SetPort((WindowPtr) myWindow);
    DrawGrowIcon (myWindow);
 
    srcPalette = NewPalette (numcolor, mycolors, pmCourteous, 0);
    SetPalette ((WindowPtr) myWindow, srcPalette, TRUE);
    
/*______________________________________________________*/
/*                    Set menus                         */
/*______________________________________________________*/
    mymenu0 = GetMenu(appleID);
    AppendResMenu(mymenu0, 'DRVR');
    InsertMenu(mymenu0,0);
    mymenu1 = newmenu(129,"File");
    appendmenu(mymenu1,"Start;-;Print;-;Quit");
    InsertMenu(mymenu1,0);
    mymenu2 = newmenu(130,"Edit");
    InsertMenu(mymenu2,0);
    DrawMenuBar();
 
/*______________________________________________________*/
/*                  Init variables                      */
/*______________________________________________________*/
    DoneFlag = FALSE;
    yieldTime = 0;
    return;
}
 
main()
{
    Boolean     track;
    long        growResult;
/*______________________________________________________*/
/*                   Main Event loop                    */
/*______________________________________________________*/
    init();
    for ( ;; ) {
        if (DoneFlag) {
            ExitToShell();
            }
        if (WaitNextEvent(everyEvent, &myEvent, yieldTime, nil)) {
            switch (myEvent.what) {
                case mouseDown:
                    switch (FindWindow(myEvent.where, &whichWindow)) {
                        case inSysWindow:
                            SystemClick(&myEvent, whichWindow);
                            break;
                        case inMenuBar:
                            doCommand(MenuSelect(myEvent.where));
                            break;
                        case inContent:
                            break;
                        case inDrag:
                            DragWindow (whichWindow, myEvent.where, &TotalRect);
                            EraseRect (&whichWindow->portRect);
                            DrawGrowIcon (whichWindow);
                            break;
                        case inGrow:
                            growResult = GrowWindow (whichWindow, myEvent.where,
                                                    &minRect);
                            SizeWindow(whichWindow, LoWord(growResult), 
                                    HiWord(growResult), TRUE);
                            EraseRect (&whichWindow->portRect);
                            DrawGrowIcon (whichWindow);
                            break;
                        case inGoAway:
                            track = TrackGoAway (whichWindow, myEvent.where);
                            if (track)
                                CloseWindow (whichWindow);
                            break;
                        case inZoomIn:
                            track = TrackBox (whichWindow, myEvent.where, inZoomIn);
                            if (track) {
                                ZoomWindow (whichWindow, inZoomIn, TRUE);
                                EraseRect (&whichWindow->portRect);
                                DrawGrowIcon (whichWindow);
                                }
                            break;
                        case inZoomOut:
                            track = TrackBox (whichWindow, myEvent.where, inZoomOut);
                            if (track) {
                                ZoomWindow (whichWindow, inZoomOut, TRUE);
                                EraseRect (&whichWindow->portRect);
                                DrawGrowIcon (whichWindow);
                                }
                            break;
                        default:
                            break;
                        }
                    break;
                case keyDown:
                    break;
                case autoKey:
                    break;
                case updateEvt:
                    if ((WindowPtr) myEvent.message == myWindow) {
                        BeginUpdate((WindowPtr) myWindow);
                        EndUpdate((WindowPtr) myWindow);
                        }
                    break;
                case diskEvt:
                    break;
                case activateEvt:
                    break;
                case app4Evt:
                    if ((myEvent.message << 31) == 0) { /* Suspend */
                        yieldTime = 30;
                        HideWindow((WindowPtr) myWindow);
                        }
                    else { /* Resume */
                        yieldTime = 0;
                        ShowWindow((WindowPtr) myWindow);
                        SetPort((WindowPtr) myWindow);
                        }
                    break;
                default:
                    break;
                }
            }
        }
}