// $Revision: 1.2 $ // Copyright (C) 1994, 1995 Taligent, Inc. All rights reserved. #ifndef TaligentSample_MENUHOLDER #include "MenuHolder.h" #endif #ifndef Taligent_VIEW #include #endif #ifndef Taligent_BUNDLES #include #endif #ifndef Taligent_GRAPHICS #include #endif #ifndef Taligent_GRAFATTRIBUTE #include #endif #ifndef Taligent_GENERICEXCEPTIONS #include #endif #ifndef Taligent_MENUCONTROLS #include #endif class TDisableUpdates { public: TDisableUpdates(TMenu* menu) : fMenu(menu), fOldUpdate(menu->IsAutoUpdate()) { if ( fOldUpdate ) menu->SetAutoUpdate(false); }; ~TDisableUpdates() { if ( fOldUpdate ) fMenu->SetAutoUpdate(true); }; private: TMenu* fMenu; bool fOldUpdate; }; TMenuHolder::TMenuHolder() : fItems(), fState(kNew) { } TMenuHolder::~TMenuHolder() { Deactivate(); fItems.DeleteAll(); } TMenuHolder& TMenuHolder::operator=(TMenu* adoptMenu) { Deactivate(); fItems.DeleteAll(); while ( adoptMenu->GetCount() > 0) { fItems.AddLast(adoptMenu->Orphan(*adoptMenu->GetFirst())); } delete adoptMenu; fState = kInactive; return *this; } TMenuHolder& TMenuHolder::operator=(MMenuItem* adoptMenuItem) { Deactivate(); fItems.DeleteAll(); fItems.AddLast(adoptMenuItem); fState = kInactive; return *this; } TMenuHolder::operator bool() const { return fState > kNew; } void TMenuHolder::Deactivate() { if ( fState == kActive ) { TDequeOfIterator iterator(&fItems); for( MMenuItem* item = iterator.First(); item; item = iterator.Next() ) { item->GetOwner()->Orphan(*item); } fState = kInactive; } } void TMenuHolder::SetEnabled(bool enabled) { if ( fState == kActive ) { TDequeOfIterator iterator(&fItems); for( MMenuItem* item = iterator.First(); item; item = iterator.Next() ) { item->SetEnabled(enabled); } } } // Set each MMenuItem matching id to enabled. Recurse into submenus. void TMenuHolder::SetEnabled(bool enabled, TMenuItemID id) { if ( fState == kActive ) { TDequeOfIterator iterator(&fItems); for( MMenuItem* item = iterator.First(); item; item = iterator.Next() ) { if (item->GetID() == id) { item->SetEnabled(enabled); } if (item->GetSubMenu()) { SetEnabled(enabled, id, *(item->GetSubMenu())); } } } } // Recurse into submenus void TMenuHolder::SetEnabled(bool enabled, TMenuItemID id, TMenu& menu) { MMenuItem* item = menu.GetFirst(); while (item) { if (item->GetID() == id) { item->SetEnabled(enabled); } if (item->GetSubMenu()) { SetEnabled(enabled, id, *(item->GetSubMenu())); } item = menu.GetAfter(*item); } } void TMenuHolder::ActivateFirst(TMenu& menu) { if ( fState == kInactive ) { TDisableUpdates entry(&menu); TDequeOfIterator iterator(&fItems); for( MMenuItem* item = iterator.Last(); item; item = iterator.Previous() ) { menu.AdoptFirst(item); } fState = kActive; } } void TMenuHolder::ActivateLast(TMenu& menu) { if ( fState == kInactive ) { TDisableUpdates entry(&menu); TDequeOfIterator iterator(&fItems); for( MMenuItem* item = iterator.First(); item; item = iterator.Next() ) { menu.AdoptLast(item); } fState = kActive; } } void TMenuHolder::ActivateAfter(MMenuItem& after) { if ( fState == kInactive ) { TMenu* owner = after.GetOwner(); TDisableUpdates entry(owner); TDequeOfIterator iterator(&fItems); for( MMenuItem* item = iterator.Last(); item; item = iterator.Previous() ) { owner->AdoptAfter(after, item); } fState = kActive; } } void TMenuHolder::ActivateAfter(TMenu& parentMenu, const TMenuDomainID& domain, const TMenuItemID& id) { MMenuItem* item = parentMenu.FindItem(domain, id); if ( item ) { ActivateAfter(*item); } }