File: menu.hh

package info (click to toggle)
aldo 0.7.3-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 684 kB
  • ctags: 454
  • sloc: cpp: 2,323; sh: 787; ansic: 157; makefile: 57
file content (138 lines) | stat: -rw-r--r-- 3,680 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// -*- C++ -*-
/***************************************************************************
                             libmenu
                          --------------------
    begin                : Sun May 6 2001
    copyright            : Giuseppe "denever" Martino
    email                : denever@users.sourceforge.net
 ***************************************************************************/
/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *  This program is distributed in the hope that it will be useful,        *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *  GNU General Public License for more details.                           *
 *                                                                         *
 *  You should have received a copy of the GNU General Public License      *
 *  along with this program; if not, write to the Free Software            *
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,             *
 *  MA 02110-1301 USA                                                      *
 *                                                                         *
 ***************************************************************************/

#ifndef MENU_H
#define MENU_H

#include <string>
#include <vector>

namespace libmenu
{
    class Menu;

    enum item_type { none = 0, type1 = 1, type2 = 2};

    typedef unsigned int id_type;

    typedef void (*Function1)();
    typedef void (*Function2)(Menu&);

    class Item
    {
    private:
	unsigned int m_id;
	std::string m_caption;
	
	item_type m_type;

	Function1 m_cmd1;
	Function2 m_cmd2;
	
    public:
	Item()
	    :m_id(0), m_type(none), m_cmd1(0), m_cmd2(0)
	{};

	Item(id_type id, std::string c, Function1 f1)
	    : m_id(id), m_caption(c), m_type(type1), m_cmd1(f1), m_cmd2(0)
	{};
	
	Item(id_type id, std::string c, Function2 f2)
	    : m_id(id), m_caption(c), m_type(type2), m_cmd1(0), m_cmd2(f2)
	{};

	Item(const Item& cpy);

	std::string caption() const;
	
	void exe(Menu&) const;
	
	item_type get_type()
	{
	    return m_type;
	}

	Item& operator=(const Item&);
	
	inline id_type id() const
	{
	    return m_id;
	}
    };

    class Menu
    {
    private:
	std::string m_title;
	std::string m_prompt;
	std::vector<Item> m_its;
	bool m_running;
	unsigned int m_last;
		
    public:
	Menu();
	Menu(const std::string&, const std::string&);
	Menu(const Menu& cpy);
	
	void show();
	void add_item(id_type, std::string, Function1);
	void add_item(id_type, std::string, Function2);
	void operator--();
	void add_item_at(unsigned int, id_type, std::string, Function1);
	void add_item_at(unsigned int, id_type, std::string, Function2);
	void delete_item_at(unsigned int);
	void delete_item(id_type);
	bool find(id_type);

	inline void set_title(std::string title)
	{
	    m_title = title;
	}

	inline void set_prompt(std::string prompt)
	{
	    m_prompt = prompt;
	}

	inline bool running() const
	{
	    return m_running;
	}
	
	inline void close()
	{
	    m_running = false;
	}
	
	inline unsigned last_selected()
	{
	    return m_last;
	}
    };
}
#endif // MENU_H