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.
Menus.c
/* |
File: Menus.c |
Contains: Handles the application's menus |
Written by: Chris White |
Copyright: Copyright © 1996-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/10/1999 Karl Groethe Updated for Metrowerks Codewarror Pro 2.1 |
*/ |
#pragma segment Core |
#ifndef __MENUS__ |
#include <Menus.h> |
#endif |
#ifndef __WINDOWS__ |
#include <Windows.h> |
#endif |
#ifndef __DIALOGS__ |
#include <Dialogs.h> |
#endif |
#ifndef __TEXTUTILS__ |
#include <TextUtils.h> |
#endif |
#ifndef __DESK__ |
#include <Desk.h> |
#endif |
// Application includes |
#ifndef __BAREBONES__ |
#include "BareBones.h" |
#endif |
#ifndef __PROTOTYPES__ |
#include "Prototypes.h" |
#endif |
// static includes |
static void DoAppleCmds ( SInt16 theMenu, SInt16 theItem ); |
static void DoFileCmds ( SInt16 theMenu, SInt16 theItem ); |
static void DoProgressBarsCmds ( SInt16 theMenu, SInt16 theItem ); |
static Boolean EnableFileCmds ( WindowRef frontWindow ); |
static Boolean EnableProgressBarsCmds ( WindowRef frontWindow ); |
static void SetItemEnable ( MenuHandle theMenu, SInt16 theItem, Boolean bEnabled ); |
void MenuDispatch ( SInt32 menuResult ) |
{ |
SInt16 theMenu = (menuResult >> 16); // menu selected |
SInt16 theItem = (menuResult & 0x0000FFFF); // item selected |
switch (theMenu) |
{ |
case kAppleMenu: |
DoAppleCmds ( theMenu, theItem ); |
break; |
case kFileMenu: |
DoFileCmds ( theMenu, theItem ); |
break; |
case kProgressBarsMenu: |
DoProgressBarsCmds ( theMenu, theItem ); |
break; |
} |
HiliteMenu ( 0 ); // un-hilite selected menu |
return; |
} // MenuDispatch |
void AdjustMenus ( void ) |
{ |
Boolean bRedraw = false; |
WindowRef frontWindow; |
frontWindow = FrontWindow ( ); |
if ( EnableFileCmds ( frontWindow ) ) |
bRedraw = true; |
if ( EnableProgressBarsCmds ( frontWindow ) ) |
bRedraw = true; |
if ( bRedraw ) |
DrawMenuBar ( ); |
return; |
} // AdjustMenus |
static void DoAppleCmds ( SInt16 theMenu, SInt16 theItem ) |
{ |
#pragma unused(theMenu) |
Str255 name; // string for DA name |
switch ( theItem ) |
{ |
case cAbout: |
Alert ( kAboutDialog, nil ); |
break; |
default: |
GetMenuItemText ( GetMenuHandle ( kAppleMenu ), theItem, (StringPtr) &name ); |
OpenDeskAcc ( (StringPtr) &name ); |
break; |
} |
return; |
} // DoAppleCmds |
static void DoFileCmds ( SInt16 theMenu, SInt16 theItem ) |
{ |
#pragma unused(theMenu) |
switch ( theItem ) |
{ |
case cQuit: |
gQuit = true; |
break; |
} |
return; |
} // DoFileCmds |
static void DoProgressBarsCmds ( SInt16 theMenu, SInt16 theItem ) |
{ |
OSErr theErr = noErr; |
Str255 theText; |
static Boolean bUseThreads = false; |
switch ( theItem ) |
{ |
case cToggleThreads: |
bUseThreads = !bUseThreads; |
CheckItem ( GetMenuHandle ( theMenu ), theItem, bUseThreads ); |
break; |
case cStandard: |
GetIndString ( theText, kMiscStrings, kProgressStr ); |
if ( bUseThreads ) |
theErr = ThreadedProgressOperation ( ThreadedStandardDemoOperation, nil, |
theText, nil, false ); |
else |
theErr = ProgressOperation ( StandardDemoOperation, nil, theText ); |
break; |
case cBarberPole: |
GetIndString ( theText, kMiscStrings, kProgressStr ); |
if ( bUseThreads ) |
theErr = ThreadedProgressOperation ( ThreadedBarberPoleDemoOperation, nil, |
theText, nil, true ); |
else |
theErr = ProgressOperation ( BarberPoleDemoOperation, nil, theText ); |
break; |
} |
if ( theErr ) |
AlertUser ( kGenericErrorStr, theErr, "\p" ); |
return; |
} // DoProgressBarsCmds |
static Boolean EnableFileCmds ( WindowRef frontWindow ) |
{ |
static Boolean bIsEnabled = true; |
Boolean bHasDialog = false; |
Boolean bUpdate = false; |
MenuHandle theMenu = GetMenuHandle ( kFileMenu ); |
bHasDialog = frontWindow && GetWindowKind ( frontWindow ) == dialogKind; |
if ( bIsEnabled == bHasDialog ) // Or bIsEnabled != !bHasDialog |
{ |
SetItemEnable ( theMenu, 0, !bHasDialog ); |
bIsEnabled = !bHasDialog; |
bUpdate = true; |
} |
SetItemEnable ( theMenu, cQuit, !bHasDialog ); |
return bUpdate; |
} // EnableFileCmds |
static Boolean EnableProgressBarsCmds ( WindowRef frontWindow ) |
{ |
static Boolean bIsEnabled = true; |
Boolean bHasDialog = false; |
Boolean bUpdate = false; |
MenuHandle theMenu = GetMenuHandle ( kProgressBarsMenu ); |
bHasDialog = frontWindow && GetWindowKind ( frontWindow ) == dialogKind; |
if ( bIsEnabled == bHasDialog ) // Or bIsEnabled != !bHasDialog |
{ |
SetItemEnable ( theMenu, 0, !bHasDialog ); |
bIsEnabled = !bHasDialog; |
bUpdate = true; |
} |
SetItemEnable ( theMenu, cToggleThreads, gHasThreadManager ); |
return bUpdate; |
} // EnableProgressBarsCmds |
static void SetItemEnable ( MenuHandle theMenu, SInt16 theItem, Boolean bEnabled ) |
{ |
if ( theMenu ) |
{ |
if ( bEnabled ) |
EnableItem ( theMenu, theItem ); |
else |
DisableItem ( theMenu, theItem ); |
} |
return; |
} // SetItemEnable |
Copyright © 2003 Apple Computer, Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2003-01-30