source: molecuilder/src/Menu/TextMenu.cpp@ f00f02

Last change on this file since f00f02 was 7cb300, checked in by Frederik Heber <heber@…>, 16 years ago

Changed formating of menus and added protection to keep MenuItems from being added twice. Signed-off-by: Tillmann Crueger <crueger@…>

  • Property mode set to 100644
File size: 1.5 KB
Line 
1/*
2 * TextMenu.cpp
3 *
4 * Created on: Dec 10, 2009
5 * Author: crueger
6 */
7
8#include <boost/bind.hpp>
9#include <iostream>
10#include <cmath>
11#include "defs.hpp"
12#include "Menu/TextMenu.hpp"
13#include "Menu/MenuItem.hpp"
14
15TextMenu::TextMenu(ostream& _outputter, string _title, char _spacer,int _length) :
16outputter(_outputter),
17title(_title),
18spacer(_spacer),
19length(_length),
20quit(false)
21{
22}
23
24TextMenu::TextMenu(ostream& _outputter, string _title) :
25outputter(_outputter),
26title(_title),
27spacer(STD_MENU_TITLE_SPACER),
28length(STD_MENU_LENGTH),
29quit(false)
30{
31}
32
33TextMenu::~TextMenu()
34{
35 for(list<MenuItem*>::iterator it=items.begin(); it != items.end(); it++)
36 delete (*it);
37}
38
39
40void TextMenu::addItem(MenuItem* item) {
41 items.push_back(item);
42}
43
44void TextMenu::removeItem(MenuItem* item) {
45 items.remove(item);
46}
47
48void TextMenu::doQuit(){
49 quit = true;
50}
51
52bool TextMenu::hasQuit(){
53 return quit;
54}
55
56void TextMenu::showEntry(MenuItem* entry){
57 outputter << entry->formatEntry() << "\n";
58}
59
60void TextMenu::display() {
61 char choice;
62 do {
63 int pre = floor((length - title.length()) /2.0);
64 int post = ceil((length - title.length()) /2.0);
65 for(int i=0;i<pre;i++)
66 outputter << spacer;
67 outputter << title;
68 for(int i=0;i<post;i++)
69 outputter << spacer;
70 outputter << '\n';
71 for_each(items.begin(), items.end(), boost::bind(&TextMenu::showEntry,this,_1));
72 outputter.flush();
73
74 cin >> choice;
75
76 for_each(items.begin(), items.end(), boost::bind(&MenuItem::checkTrigger,_1,choice));
77 }while (!hasQuit());
78}
Note: See TracBrowser for help on using the repository browser.