/* * Project: MoleCuilder * Description: creates and alters molecular systems * Copyright (C) 2010 University of Bonn. All rights reserved. * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. */ /* * TxMenu.cpp * * Created on: Dec 10, 2009 * Author: crueger */ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include "Helpers/MemDebug.hpp" #include #include #include #include #include "Actions/Action.hpp" #include "Actions/ActionTraits.hpp" #include "Menu/TextMenu/TxMenu.hpp" #include "Menu/TextMenu/MenuItem.hpp" #include "Helpers/Assert.hpp" /** Constructor for class TxMenu. * * produce a text menu with a given title. * The text will later be displayed using the stream passed to the constructor. * \param &_outputter output stream to use for displaying the text * \param _title title of this menu * \param _spacer key to separate trigger key from descriptive text shown * \param _length maximum length of the descriptive text */ TxMenu::TxMenu(std::ostream& _outputter, const std::string _title, char _spacer,int _length) : defaultItem(0), outputter(_outputter), title(_title), spacer(_spacer), length(_length), quit(false) { } /** Destructor for class TxMenu. * */ TxMenu::~TxMenu() { for(std::list::iterator it=items.begin(); it != items.end(); it++) delete (*it); } /** Adds an MenuItem to the internal list. * \param *item item to add */ void TxMenu::addItem(MenuItem* item) { items.push_back(item); } /** Removes an MenuItem to the internal list. * \param *item item to remove */ void TxMenu::removeItem(MenuItem* item) { items.remove(item); } /** Function to quit this TxMenu. */ void TxMenu::doQuit(){ quit = true; } /** Return the current state of quitting. * \return quit boolean */ bool TxMenu::hasQuit(){ return quit; } /** Display in a formatted manner a given entry of this menu. * \param *entry MenuItem to show */ void TxMenu::showEntry(MenuItem* entry){ if(entry->isActive()==false){ outputter << "("; } outputter << entry->formatEntry(); if(entry->isActive()==false){ outputter << ")"; } outputter << "\n"; } /** Display this menu. * */ void TxMenu::display() { char choice; bool somethingChosen = false; quit = false; do { int pre = floor((length - title.length()) /2.0); int post = ceil((length - title.length()) /2.0); for(int i=0;i> choice; std::list::iterator iter; for(iter = items.begin(); iter!=items.end();iter++){ if((*iter)->isActive()){ somethingChosen |= (*iter)->checkTrigger(choice); } } // see if something was chosen and call default Item if not if(!somethingChosen) { if(defaultItem){ defaultItem->doTrigger(); } else{ outputter << "Invalid Choice!" << std::endl; } } }while (!hasQuit()); } /** Return the internally stored title of the menu. * \return title string */ std::string TxMenu::getTitle(){ return title; } /** Return the internally stored outputter of the menu. * \return output stream reference */ std::ostream& TxMenu::getOutputter() { return outputter; } /** Add a default item to the menu. * \param *_defaultItem MenuItem to act as default item. */ void TxMenu::addDefault(MenuItem* _defaultItem) { defaultItem = _defaultItem; } /****************************** Contained Actions ****************/ /** Constructor for class TxMenu::LeaveAction. * \param _menu pointer to the containing TxMenu * \param &LeaveActionTrait ActionTraits for this Action */ TxMenu::LeaveAction::LeaveAction(TxMenu* const _menu, const ActionTraits & LeaveActionTrait) : Action(LeaveActionTrait, true), menu(_menu) {} /** Destructor for class TxMenu::LeaveAction. * */ TxMenu::LeaveAction::~LeaveAction(){} /** We can't undo the leave action. * \return false */ bool TxMenu::LeaveAction::canUndo(){ return false; } /** We should never undo the leave action. * \return false */ bool TxMenu::LeaveAction::shouldUndo(){ return false; } /** Internal function to obtain parameters from a storage. * We do not use this one as we don't need any parameters. */ void TxMenu::LeaveAction::getParametersfromValueStorage() {} /** Internal function to construct the dialog. * We do not need this function as there is no dialog to construct. */ Dialog* TxMenu::LeaveAction::fillDialog(Dialog *dialog){ ASSERT(dialog,"No Dialog given when filling action dialog"); return dialog; } /** Calls TxMenu::doQuit() on the stored menu reference. * \return ActionState pointer with success */ Action::state_ptr TxMenu::LeaveAction::performCall(){ menu->doQuit(); return Action::success; } /** Implementation of undo function for an Action. * We do not use this functionality. * \return ActionState pointer with failure */ Action::state_ptr TxMenu::LeaveAction::performUndo(Action::state_ptr){ ASSERT(0,"Cannot undo leaving a menu"); return Action::failure; } /** Implementation of redo function for an Action. * We do not use this functionality. * \return ActionState pointer with failure */ Action::state_ptr TxMenu::LeaveAction::performRedo(Action::state_ptr){ ASSERT(0,"Cannot redo leaving a menu"); return Action::failure; }