Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

menu.cpp

Go to the documentation of this file.
00001 /***************************************************************************************************
00002 *****           Copyright (C) 2005 John Schneiderman <JohnMS@member.fsf.org>                   *****
00003 *****                                                                                          *****
00004 *****           This program is free software; you can redistribute it and/or modify           *****
00005 *****           it under the terms of the GNU General Public License as published by           *****
00006 *****           the Free Software Foundation; either version 2 of the License, or              *****
00007 *****           (at your option) any later version.                                            *****
00008 *****                                                                                          *****
00009 *****           This program is distributed in the hope that it will be useful,                *****
00010 *****           but WITHOUT ANY WARRANTY; without even the implied warranty of                 *****
00011 *****           MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                  *****
00012 *****           GNU General Public License for more details.                                   *****
00013 *****                                                                                          *****
00014 *****           You should have received a copy of the GNU General Public License              *****
00015 *****           along with this program; if not, write to the Free Software                    *****
00016 *****           Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA     *****
00017 ***************************************************************************************************/
00018 #include "menu.h"
00019 
00020 #include <iostream>
00021 using std::cin;
00022 using std::cout;
00023 using std::endl;
00024 
00025 const string Menu::DEFAULT_MESSAGE="- - - - - - - - - - - - - - - - - - - - ";
00026 
00027 Menu::Menu(const string &title, const string &prompt)
00028 {
00029     if (system("cls") == 0)
00030         m_clearScreen="cls";
00031     else if (system("clear") == 0)
00032         m_clearScreen="clear";
00033     else
00034         m_clearScreen="Clear Screen not set!";
00035     m_title=title;
00036     m_feedback=DEFAULT_MESSAGE;
00037     m_prompt=prompt;
00038     m_menuChar = '*';
00039 }
00040 
00041 void Menu::addMenuItem(const string &menuItem)
00042 {
00043     if (m_menuItems.size() > 1)
00044         m_menuItems[m_menuItems.size() - 1] = menuItem;
00045     else
00046         m_menuItems.push_back(menuItem);
00047     m_menuItems.push_back("Exit");
00048 }
00049 
00050 int Menu::menuItem(const string &menuItem) const
00051 {
00052     for (int i=0; i < m_menuItems.size(); i++)
00053         if (m_menuItems[i] == menuItem)
00054             return i;
00055     return -1;
00056 }
00057 
00058 void Menu::setFeedback(const string &feedback)
00059 {
00060     m_feedback=feedback;
00061 }
00062 
00063 void Menu::draw() const
00064 {
00065     system(m_clearScreen.c_str());
00066     int length=((MAX_WIDTH - (m_title.length() + 2)) / 2);
00067 
00068     //Draw the Menu title
00069     for (int i = 0; i < length; i++)
00070         cout << m_menuChar;
00071     cout << " " << m_title << " ";
00072     for (int i = length + 2 + m_title.length(); i < MAX_WIDTH; i++)
00073         cout << m_menuChar;
00074     cout << endl;
00075     //Draw any error messages
00076     length=((MAX_WIDTH - (m_feedback.length() + 2)) / 2);
00077     for (int i = 0; i < length; i++)
00078         cout << m_menuChar;
00079     cout << " " << m_feedback << " ";
00080     for (int i = length + 2 + m_feedback.length(); i < MAX_WIDTH; i++)
00081         cout << m_menuChar;
00082     cout << endl;
00083     m_feedback=DEFAULT_MESSAGE;
00084     //Draw the Menu items
00085     for (int i = 0; i < m_menuItems.size(); i++)
00086     {
00087         //Draw starter spacers
00088         for (int j = 0; j < MENU_ITEM_SPACERS; j++)
00089             cout << m_menuChar;
00090         //Draw the numbers
00091         if (i < 10)
00092             cout << "  " << i << ") " << m_menuItems[i];
00093         else
00094             cout << " " << i << ") " << m_menuItems[i];
00095         //Draw space to the ending spacer for all options under 100
00096         for (int j = 0; j < (MAX_WIDTH -((MENU_ITEM_SPACERS * 2) + 5 + m_menuItems[i].size())); j++)
00097             cout << " ";
00098         //Draw ending spacers
00099         for (int j = 0; j < MENU_ITEM_SPACERS; j++)
00100             cout << m_menuChar;
00101         cout << endl;
00102     }
00103     for (int i = 0; i < MAX_WIDTH; i++)
00104         cout << m_menuChar;
00105     cout << endl;
00106 }
00107 
00108 int Menu::selectMenuItem() const
00109 {
00110     int menuItemSelection=-1;
00111 
00112     while (true)
00113     {
00114         cout << m_prompt;
00115         cin >> menuItemSelection;
00116         if (menuItemSelection == (m_menuItems.size() - 1))
00117             return MENU_EXIT;
00118         else if ((menuItemSelection >= 0) && (menuItemSelection < m_menuItems.size()))
00119             return menuItemSelection;
00120         else
00121         {
00122             m_feedback = "Invalid selection.";
00123             draw();
00124         }
00125     }
00126 }

Generated on Tue Mar 28 23:28:03 2006 for ClusterSim by  doxygen 1.4.4