Retired Document
Important: This sample code may not represent best practices for current development. The project may use deprecated symbols and illustrate technologies and techniques that are no longer recommended.
MenuStuff.c
/* |
File: MenuStuff.c |
Contains: Menu handling routines. |
Creates and destroys the menu bar |
Responds to user selecting menu item (mouse or keyboard) |
Does all the calls to open a single movie or a master |
and slave |
Written by: Jason Hodges-Harris & Don Swatman |
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): |
8/17/1999 Karl Groethe Updated for Metrowerks Codewarror Pro 2.1 |
*/ |
#include <Fonts.h> |
#include <Devices.h> |
#include <ToolUtils.h> |
#include "MenuStuff.h" |
#include "WindStuff.h" |
#include "MovieStuff.h" |
#include "MoviePrefs.h" |
//---------------------------------------------- |
// Define menubar constants |
//---------------------------------------------- |
#define kMenuBar 128 |
#define mApple 128 |
#define iAbout 1 |
#define mFile 129 |
#define iOpen 1 |
#define iOpenMS 2 |
#define iClose 3 |
#define iQuit 5 |
#define mEdit 130 |
#define iUndo 1 |
#define iCut 3 |
#define iCopy 4 |
#define iPaste 5 |
//---------------------------------------------- |
// Create the menu bar |
//---------------------------------------------- |
void MenuBarInit (void) |
{ |
Handle menuBar; |
MenuHandle hMenu; |
// Create the menu bar |
menuBar = GetNewMBar(kMenuBar); // Get a menu bar resource |
SetMenuBar (menuBar); // Set the current menu bar to it |
hMenu = GetMenuHandle(mApple); // Get a handle to the apple Menu |
AppendResMenu (hMenu,'DRVR'); // Attach the Desk Accessories on to the end |
DrawMenuBar(); // Ok, draw the menu bar |
} |
//============================================== |
// Various menu bar utilities |
//============================================== |
//---------------------------------------------- |
// SetMenuItemEnabled |
// |
// Grays or Un grays a menu item |
// Returns true if something was enabled |
//---------------------------------------------- |
Boolean SetMenuItemEnabled ( short menuID, |
short theItem, |
Boolean enableState ); |
Boolean SetMenuItemEnabled ( short menuID, |
short theItem, |
Boolean enableState ) |
{ |
MenuHandle hMenu; |
hMenu = GetMenuHandle(menuID); |
if (enableState) |
EnableItem(hMenu, theItem); |
else |
DisableItem(hMenu, theItem); |
return ( enableState ); |
} |
//---------------------------------------------- |
// DoAdjustFileMenu |
// |
// Sets each item in the file menu |
//---------------------------------------------- |
static Boolean DoAdjustFileMenu(WindowPtr window); |
static Boolean DoAdjustFileMenu(WindowPtr window) |
{ |
short newWind; // throw away value used by IsFreeWind |
Boolean menuEnabled; // true if any item is enabled |
Boolean redrawMenuBar; // true if we need to redraw the menu bar |
static |
Boolean fileMenuEnabled = true; // This is a static so it'll hang around |
// Reflects whither the file menu is currently |
// grayed(false) or enabled(true) |
// Open needs just one free window |
menuEnabled = SetMenuItemEnabled( mFile, iOpen, IsFreeWind(&newWind, 1) ); |
// Open master and slave needs 2 free windows |
menuEnabled |= SetMenuItemEnabled( mFile, iOpenMS, IsFreeWind(&newWind, 2) ); |
// Close is ok if it's our window |
menuEnabled |= SetMenuItemEnabled( mFile, iClose, IsOurWindow( window ) ); |
// Quit always enabled |
menuEnabled |= SetMenuItemEnabled( mFile, iQuit, true); |
// If nothing enabled the change then menu title to grayed out |
if (!menuEnabled) |
menuEnabled = SetMenuItemEnabled( mFile, 0, false); |
// If menu title has changed, then return the fact that we have |
// to redraw the menu bar |
redrawMenuBar = (fileMenuEnabled != menuEnabled); |
fileMenuEnabled = menuEnabled; |
return(redrawMenuBar); |
} |
//---------------------------------------------- |
// DoAdjustEditMenu |
// |
// As we don't do use this menu, we just gray |
// the whole lot out |
//---------------------------------------------- |
static Boolean DoAdjustEditMenu(WindowPtr window ) |
{ |
#pragma unused ( window ) |
Boolean menuEnabled; // true if any item is enabled |
Boolean redrawMenuBar; // true if we need to redraw the menu bar |
short itemCount; // count down each menu item |
static |
Boolean editMenuEnabled = true; // This is a static so it'll hang around |
// Reflects whither the file menu is currently |
// grayed(false) or enabled(true) |
// Gray out all the items |
menuEnabled = false; |
for (itemCount = iUndo; itemCount <= iPaste; ++itemCount) |
menuEnabled |= SetMenuItemEnabled(mEdit, itemCount, false); |
// If nothing enabled then change the menu title to grayed out |
if (!menuEnabled) |
menuEnabled |= SetMenuItemEnabled( mEdit, 0, false); |
// If menu title has changed, then return the fact that we have |
// to redraw the menu bar |
redrawMenuBar = (editMenuEnabled != menuEnabled); |
editMenuEnabled = menuEnabled; |
return(redrawMenuBar); |
} |
//---------------------------------------------- |
// DoAdjustMenus |
// |
// Adjust all the menu items and redraws the menu |
// bar if something has changed |
//---------------------------------------------- |
void DoAdjustMenus(void) |
{ |
WindowPtr window; |
Boolean redrawMenuBar; |
window = FrontWindow(); |
redrawMenuBar = DoAdjustFileMenu ( window ); // Adjust the file menu |
redrawMenuBar |= DoAdjustEditMenu ( window ); // Adjust the edit menu |
// redraw the menu bar if one of the menu titles has changed |
if (redrawMenuBar) |
DrawMenuBar(); |
} |
//---------------------------------------------- |
// DoSlaveMovieTest |
// |
// Open two movies and slave one of the other |
//---------------------------------------------- |
void DoSlaveMovieTest ( void ); |
void DoSlaveMovieTest ( void ) |
{ |
OSErr theErr = noErr; |
short masterNum = -1; // window number of master window |
short slaveNum = -1; // window number of slave window |
// Check to see if there is room for 2 movies |
if (IsFreeWind(&masterNum, 2)) |
{ |
// Let the user change any of the preferences |
theErr = OneMoviePref( &gDefaultMoviePrefs, true ); |
if (!theErr) |
{ |
// Create the master movie in the master Window |
CreateWindow( masterNum, "\pMaster Movie", kWindAtFront ); |
if (gTheWinds[masterNum]) |
{ |
// Create a movie |
theErr = OpenMovieWindow ( gTheWinds[masterNum], // WindowPtr |
kUserCloseWind, // Force user to close at end |
gDefaultMoviePrefs.hasController ); // Add controller if |
// user wants it |
if (!theErr) |
{ |
// Create the slave movie Window |
if (IsFreeWind(&slaveNum, 1)) |
{ |
CreateWindow( slaveNum, "\pSlave Movie", gTheWinds[masterNum] ); |
if (gTheWinds[slaveNum]) |
{ |
// Create a movie in the slave window |
theErr = OpenMovieWindow ( gTheWinds[slaveNum], // WindowPtr |
kUserCloseWind, // Force user to close at end |
kNoMovieController ); // No Movie Controller |
// Set up any changes to the masters movies rate |
if (!theErr) |
if (gDefaultMoviePrefs.rateChangeDelay) |
theErr = SetupMovieRate( gTheWinds[masterNum], gDefaultMoviePrefs.rateChangeDelay ); |
// Set up the looping in the master movie - 20 seconds from start go back to 10 secs |
if (!theErr) |
if (gDefaultMoviePrefs.do20to10Loop) |
theErr = SetupLoop( gTheWinds[masterNum], 20, 10 ); |
// Now hook the two movies together |
if (!theErr) |
theErr = SetupSlaveMovie( gTheWinds[masterNum], |
gTheWinds[slaveNum], |
gDefaultMoviePrefs.slaveAheadBy, |
gDefaultMoviePrefs.slaveStartDelay ); |
// Display and order the windows |
if (!theErr) |
{ |
// Show the windows |
ShowWindow( gTheWinds[slaveNum] ); |
ShowWindow( gTheWinds[masterNum] ); |
// Select the master window (as the front active window) |
SelectWindow ( gTheWinds[masterNum] ); |
// Move slave window to be behind the master in the window list |
SendBehind (gTheWinds[slaveNum], gTheWinds[masterNum]); |
} |
// If we don't have a controller then start the movie |
if (!theErr) |
if (!gDefaultMoviePrefs.hasController) |
theErr = StartMovieWindow( gTheWinds[masterNum],kStartFromBegining ); |
} |
} |
} |
// If we have an error, then close down the windows |
if (theErr) |
{ |
if (masterNum != -1) |
CloseOurWindow( masterNum ); |
if (slaveNum != -1) |
CloseOurWindow( slaveNum ); |
} |
} |
// Adjust the menus, whatever happens |
DoAdjustMenus(); |
} |
} |
} |
//---------------------------------------------- |
// DoOpenWindow |
// |
// Open one movie using the options setup |
//---------------------------------------------- |
void DoOpenWindow (void); |
void DoOpenWindow (void) |
{ |
OSErr theErr; |
short windNum; // The windows number in the array |
// Check there is a free window |
if (IsFreeWind(&windNum,1)) |
{ |
// Get any options the user might want |
theErr = OneMoviePref( &gDefaultMoviePrefs, false ); |
if (!theErr) |
{ |
// Open a window |
CreateWindow( windNum, "\pUntitled", kWindAtFront ); |
if (gTheWinds[windNum]) |
{ |
// Create a movie |
theErr = OpenMovieWindow ( gTheWinds[windNum], // windowPtr |
gDefaultMoviePrefs.closeAtEnd, // Users option for auto close |
gDefaultMoviePrefs.hasController ); // Users Option for controller |
// Set up the movie rate changes |
if (!theErr) |
if (gDefaultMoviePrefs.rateChangeDelay) |
theErr = SetupMovieRate( gTheWinds[windNum], gDefaultMoviePrefs.rateChangeDelay ); |
// Set up the looping in the master movie - 20 seconds from start go back to 10 secs |
if (!theErr) |
if (gDefaultMoviePrefs.do20to10Loop) |
theErr = SetupLoop( gTheWinds[windNum], |
gDefaultMoviePrefs.loopFrom, gDefaultMoviePrefs.loopTo ); |
// if no errors, then display the windows |
if (!theErr) |
{ |
// Show the windows |
ShowWindow(gTheWinds[windNum]); |
// Select the window (as the front active window) |
SelectWindow ( gTheWinds[windNum] ); |
} |
// If we don't have a controller then start the movie |
if (!theErr) |
if (!gDefaultMoviePrefs.hasController) |
theErr = StartMovieWindow( gTheWinds[windNum],kStartFromBegining ); |
// If we've had an error, then remove the window and clean up |
if (theErr) |
{ |
if (windNum != -1) |
CloseOurWindow( windNum ); |
} |
} |
} |
// Adjust the menus, whatever happens |
DoAdjustMenus(); |
} |
} |
//---------------------------------------------- |
// DoMenuCommand |
// |
// Handles selection of items in menu |
//---------------------------------------------- |
void DoMenuCommand(long menuResult) |
{ |
short menuID; |
short menuItem; |
short daRefNum; |
Str255 daName; |
short windNum; |
// convert menuResult into menu and item |
menuID = HiWord(menuResult); |
menuItem = LoWord(menuResult); |
switch (menuID) |
{ |
// Apple Menu |
case mApple: |
switch (menuItem) |
{ |
// Display about box |
case iAbout: |
AboutBox(); |
break; |
// Handle DA selected in Apple Menu |
default: |
GetMenuItemText(GetMenuHandle(mApple), menuItem, daName); |
daRefNum = OpenDeskAcc(daName); |
break; |
} |
break; |
// File Menu |
case mFile: |
switch (menuItem) |
{ |
// Open a movie window |
case iOpen: |
DoOpenWindow (); |
break; |
// Open a master and slave movie windows |
case iOpenMS: |
DoSlaveMovieTest( ); |
break; |
// Close front window |
case iClose: |
windNum = GetWindowNum ( FrontWindow() ); |
if (windNum != -1 ) |
CloseOurWindow( windNum ); |
break; |
// User wants to quit so set gDone to True |
case iQuit: |
gDone=true; |
break; |
} |
break; |
// Edit Menu |
// Don't do anything |
case mEdit: |
switch (menuItem) |
{ |
case iUndo: |
break; |
case iCut: |
break; |
case iCopy: |
break; |
case iPaste: |
break; |
} |
break; |
} |
// Un highlite the menu title |
HiliteMenu(0); |
} |
Copyright © 2003 Apple Computer, Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2003-01-14