menu.c

/*
    File:       menu.c
 
    Contains:   
 
    Written by: Jason Hodges-Harris 
 
    Copyright:  Copyright © 1995-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/28/1999   Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
 
// Mac Toolbox headers
 
#ifndef __DESK__
#include <Desk.h>
#endif
 
#ifndef __ERRORS__
#include <Errors.h>
#endif
 
#ifndef __MEMORY__
#include <Memory.h>
#endif
 
#ifndef __MENUS__
#include <Menus.h>
#endif
 
#ifndef __QUICKDRAW__
#include <QuickDraw.h>
#endif
 
#ifndef __TOOLUTILS__
#include <ToolUtils.h>
#endif
 
#ifndef __WINDOWS__
#include <Windows.h>
#endif
 
 
// Program headers
 
#ifndef __CHROMAPPHEADER__
#include "ChromaKeyMovie.h"
#endif
 
 
 
//  Global Variables
 
extern short            gKeyMode;
extern Boolean          gDone;              // program loop test condition
extern Boolean          gMovieOpen;
extern Boolean          gMovieBackGrnd;
 
 
extern GWorldPtr        gOffscreenPort,
                        gBackGroundPort,
                        gBackGroundPicture;
extern PixMapHandle     gMoviePixmap,
                        gBackGndPixmap,
                        gBackGndPictPM;
 
 
// Initialise the application menubar.
 
#pragma segment Menu
void MenuBarInit (void)
{
    SetMenuBar (GetNewMBar(rMenuBar));
    AppendResMenu (GetMenuHandle(mApple),'DRVR');
    DrawMenuBar();
}
 
 
// Sets the menubar to the default settings during the application initialisation phase.
// i.e. In the 'Keying Mode' menu, the 'Transparent Color Transfer' menu option has a
// tick mark against it to indicate it as the currently selected mode.
 
#pragma segment Menu
void    DoAdjustMenus(void)
{
    DoAdjustFileMenu();
    DoAdjustEditMenu();
    DoAdjustKeyMode();
    DoAdjustOptions(false,true);    // set the default to loop movie and not play every frame
    DrawMenuBar();
    return;
}
 
 
 
// The DoMenuCommand() function performs all of the handling of 
// the user's interaction with the application
 
#pragma segment Menu
void    DoMenuCommand(long menuResult)
{
    MovieDocHndl        theDocHandle;
    Str255              daName;
    WindowPtr           window,theNextWindow;
    short               menuID,
                        menuItem,
                        daRefNum;
    static Boolean      sLoopMovie = true;          // set the movie to loop as default
    static Boolean      sPlayAllFrames = false;     // set don't play all frames as default
    
    menuID   = HiWord(menuResult);
    menuItem = LoWord(menuResult);
 
    switch (menuID) 
    {
 
        case mApple:                // Apple menubar items
            switch (menuItem) 
            {
                /* Display the application about box.
                   App name, copyright etc. */
                case iAbout:        
                    DisplayAlert (rAboutBox,0,0);
                break;
 
                // handle all menubar Desk Accessories.
                default:            
                    GetMenuItemText(GetMenuHandle(mApple), menuItem, daName);
                    daRefNum = OpenDeskAcc(daName);
                break;
            }
            break;
 
        case mFile:                     // File menubar items
            switch (menuItem) 
            {
                case iOpen:             // Open movie file
                /* Do nothing if a window is already open.
                   Currently this application can only handle
                   one open window manily due to processor 
                   requirements in keeping a movie serviced
                   at a high frame rate. */
                if (!gMovieOpen)
                    {
                         /* initialise environment for movie playback.
                            If an error is encountered, the PlayMovieChroma()
                            function will return false and the application will
                            bail without displaying an error as it should have
                            recovered gracefully. */
                        if (PlayMovieChroma()) 
                        {
                            gMovieOpen = true;  // set boolean to indicate movie window open
                            /* Perform tests to see if menu items to loop and play all
                              frames of the movie are selected and set the movie according */
                            if (sPlayAllFrames)
                                sPlayAllFrames = SetPlayAllFrames(!sPlayAllFrames);
                            if (sLoopMovie)
                                sLoopMovie = SetLoopMovie(!sLoopMovie);
 
                            /* Test for which croma key mode is selected and set
                               if the default Transparent Color Transfer mode isn't used. */
                            if (gKeyMode == graphix)
                                VideoGraphicsMode(FrontWindow(),true);
                            if (gKeyMode == modifierTrax)
                                ModifierTrackMode(FrontWindow());
                            DoAdjustFileMenu();
                        }
                    }
                break;
                case iClose:
                    if (gMovieOpen)
                    {
                        /* close open movie window and reset boolean to indicate
                           that another window can be opened */
                        gMovieOpen = false;
                        DisposeWindowDocs (FrontWindow());
                    }
                break;
                case iQuit:
                    /* As quit menu option selected, close the window (if open),
                       and set the loop boolean to true to allow application to exit. */
                    if (gMovieOpen)
                    {
                        window = FrontWindow();
                        while (window)
                        {
                            theNextWindow = &((WindowPeek)window)->nextWindow->port;
                            DisposeWindowDocs (window);
                            window = theNextWindow;
                        }
                        // SetPort(FrontWindow());
                    }
                    gMovieOpen = false;
                    gDone=true;
                break;
            }
            break;
 
        /*  Edit menubar options. These aren't
            implemented but included for completeness. */
        case mEdit:     
            switch (menuItem) 
            {
                case iUndo:
                    break;
                case iCut:
                    break;
                case iCopy:
                    break;
                case iPaste:
                    break;
            }
            DoAdjustEditMenu();
        break;
        /* The (Keying Mode) mMode menu, controls the user interaction
           with the different methods of keying a movie 
           and setting up the display options. */
        case mMode:
                
            window = FrontWindow();
            switch (menuItem)
            {
                case iKeyColor:
                    /* Sets the color used as transparent to allow the background
                     image to display thro the foreground image and updates
                     the modifier track / graphix modes if either is the selected
                     mode, as these only set the transparent color when initialised. */
                    TransparentColor(); 
                    switch (gKeyMode)
                    {
                        case modifierTrax:
                            // update the modifier track information
                        break;
                        case graphix:
                            // update the graphix mode to key out new color
                            VideoGraphicsMode(window,true); 
                        break;
                    }
                break;
                /* Set the gMovieBackGrnd variable to position the movie in front or behind of the 
                   background image when using the 'Transparent Color Transfer' mode */
                case iMovieBack:
                    gMovieBackGrnd = true;
                break;
                case iMovieFront:
                    gMovieBackGrnd = false;
                break;
                /* 'Transparent Color Transfer' mode selected. If a window isn't open
                    just set the transfer mode global, else check the current
                    mode of the movie window and remove information set by previous mode. */
                case iTransparent:
                    if (gMovieOpen)
                    {
                        theDocHandle=(MovieDocHndl)GetWRefCon((WindowPtr)window);
                        switch (gKeyMode)
                        {
                            case modifierTrax:
                                HLock((Handle)theDocHandle);
                                if (DestroyModifierTrack(theDocHandle))
                                    DisplayAlert (rGenAlert,rErrMessages,4);
                                HUnlock((Handle)theDocHandle);
                            break;
                            case graphix:
                                VideoGraphicsMode(window,false);
                            break;
                            
                        }
                        SetMovieGWorld((**theDocHandle).theMovie,gOffscreenPort,nil);
                    }
                    gKeyMode = transparentMode;
                break;
                /* 'Graphix Mode' mode selected. If a window isn't open
                    just set the transfer mode global, else check the current
                    mode of the movie window and remove information set by previous mode. */
                case iGraphix:
                    if (gMovieOpen)
                    {
                        theDocHandle=(MovieDocHndl)GetWRefCon((WindowPtr)window);
                        switch (gKeyMode)
                        {
                            case modifierTrax:
                                HLock((Handle)theDocHandle);
                                if (DestroyModifierTrack(theDocHandle))
                                    DisplayAlert (rGenAlert,rErrMessages,4);
                                HUnlock((Handle)theDocHandle);
                            break;
                        }
                        VideoGraphicsMode(window,true); // use VideoMediaGraphicsMode. See IM QuickTime 2:287
                    }
                    gKeyMode = graphix;
                break;
                /* 'Modifier Track' mode selected. If a window isn't open
                    just set the transfer mode global, else check the current
                    mode of the movie window and remove information set by previous mode. */
                case iModifier:
                    if (gMovieOpen)
                    {
                        theDocHandle=(MovieDocHndl)GetWRefCon((WindowPtr)window);
                        switch (gKeyMode)
                        {
                            case graphix:
                                VideoGraphicsMode(window,false);
                            break;
                        }
                        ModifierTrackMode(window);
                    }
                    gKeyMode = modifierTrax;
                break;
            }
            DoAdjustKeyMode();
        break;
        /* Options menu selected. These options set the playback options of the movie.
           The available options are currently looping and play every frame. Note
           that the Play Every Frame option mutes any active audio tracks when selected. */
        case mOptions:
            switch (menuItem)
            {
                case iEveryFrame:
                    sPlayAllFrames = SetPlayAllFrames(sPlayAllFrames);
                break;
                case iLoopMovie:
                    sLoopMovie = SetLoopMovie(sLoopMovie);
                break;
            }
            DoAdjustOptions(sPlayAllFrames,sLoopMovie);
        break;
    }
    HiliteMenu(0);      // Unhighlight what MenuSelect (or MenuKey) hilited.
}
 
 
// Adjust the file menubar items
 
#pragma segment Menu
void    DoAdjustFileMenu(void)
{
    MenuHandle          menu;
 
    /* Enable the Open and Close File menu items dependent
     on whether a window is currently open */
    menu = GetMenuHandle(mFile);
    if (gMovieOpen)
    {
        DisableItem(menu,iOpen);
        EnableItem(menu,iClose);
    }
    else if (!gMovieOpen)
    {
        EnableItem(menu,iOpen);
        DisableItem(menu,iClose);
    }
    return;
}
 
 
/* Adjust the Edit menu bar items. Currently as these items
   are disabled as they're not supported. */
 
#pragma segment Menu
void    DoAdjustEditMenu(void)
{
    MenuHandle      menu;
    short           i;
    
    menu = GetMenuHandle(mEdit);
    for (i = iUndo; i <= iPaste; ++i)
        DisableItem(menu, i);
    return;
}
 
 
/* Sets the Keying Mode menu items. This is responsible for setting
   check marks against the currently selected transfer method and
   the position of the movie relative to the background. It also
   enables or disables the movie position items when the graphix
   and modifierTrax methods are used, as these both display in the foreground. */
 
#pragma segment Menu
void    DoAdjustKeyMode(void)
{
    MenuHandle      menu;
    short           itemMark;
    
    menu = GetMenuHandle(mMode);
    switch (gKeyMode)
    {
        case transparentMode:
            GetItemMark(menu,iTransparent,&itemMark);
            if (itemMark == 0x00)
            {
                SetItemMark(menu,iTransparent,0x12);
                SetItemMark(menu,iGraphix,0x00);
                SetItemMark(menu,iModifier,0x00);
                EnableItem(menu,iMovieBack);
                EnableItem(menu,iMovieFront);
            }
        break;
        case graphix:
            GetItemMark(menu,iGraphix,&itemMark);
            if (itemMark == 0x00)
            {
                SetItemMark(menu,iTransparent,0x00);
                SetItemMark(menu,iGraphix,0x12);
                SetItemMark(menu,iModifier,0x00);
                DisableItem(menu,iMovieBack);
                DisableItem(menu,iMovieFront);
            }
        break;
        case modifierTrax:
            GetItemMark(menu,iModifier,&itemMark);
            if (itemMark == 0x00)
            {
                SetItemMark(menu,iTransparent,0x00);
                SetItemMark(menu,iGraphix,0x00);
                SetItemMark(menu,iModifier,0x12);
                DisableItem(menu,iMovieBack);
                DisableItem(menu,iMovieFront);
            }
        break;
    }
    GetItemMark(menu,iMovieBack,&itemMark);
    if (gMovieBackGrnd)
    {
        if (itemMark == 0x00)
        {
            SetItemMark(menu,iMovieBack,0x12);
            SetItemMark(menu,iMovieFront,0x00);
        }
    }
    else
    {
        if (itemMark != 0x00)
        {
            SetItemMark(menu,iMovieBack,0x00);
            SetItemMark(menu,iMovieFront,0x12);
        }
    }
    return;
}
 
 
 
/* DoAdjustOptions() adjusts the Options menu items and 
   placed / removes check marks against these items when
   they're selected / deselected. */
 
#pragma segment Menu
void DoAdjustOptions(Boolean playAllFrames, Boolean loopMovie)
{
    MenuHandle      menu;
    
    menu = GetMenuHandle(mOptions);
    if (playAllFrames)
        SetItemMark(menu,iEveryFrame,0x12);
    else
        SetItemMark(menu,iEveryFrame,0x00);
    if (loopMovie)
        SetItemMark(menu,iLoopMovie,0x12);
    else
        SetItemMark(menu,iLoopMovie,0x00);
    return;
}