1/*
2 * Copyright 2004-2010, Axel D��rfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef KERNEL_BOOT_MENU_H
6#define KERNEL_BOOT_MENU_H
7
8
9#include <SupportDefs.h>
10#include <util/DoublyLinkedList.h>
11
12
13class Menu;
14class MenuItem;
15
16typedef bool (*menu_item_hook)(Menu* menu, MenuItem* item);
17typedef void (*shortcut_hook)(char key);
18
19
20enum menu_item_type {
21	MENU_ITEM_STANDARD = 1,
22	MENU_ITEM_MARKABLE,
23	MENU_ITEM_TITLE,
24	MENU_ITEM_NO_CHOICE,
25	MENU_ITEM_SEPARATOR,
26};
27
28
29class MenuItem : public DoublyLinkedListLinkImpl<MenuItem> {
30public:
31								MenuItem(const char* label = NULL,
32									Menu* subMenu = NULL);
33	virtual						~MenuItem();
34
35			void				SetTarget(menu_item_hook target);
36			menu_item_hook		Target() const { return fTarget; }
37
38	virtual	void				SetMarked(bool marked);
39			bool				IsMarked() const { return fIsMarked; }
40
41			void				Select(bool selected);
42			bool				IsSelected() const { return fIsSelected; }
43
44			void				SetEnabled(bool enabled);
45			bool				IsEnabled() const { return fIsEnabled; }
46
47			void				SetType(menu_item_type type);
48			menu_item_type		Type() const { return fType; }
49
50			void				SetData(const void* data);
51			const void*			Data() const { return fData; }
52
53			void				SetHelpText(const char* text);
54			const char*			HelpText() const { return fHelpText; }
55
56			void				SetShortcut(char key);
57			char				Shortcut() const { return fShortcut; }
58
59			const char*			Label() const { return fLabel; }
60			void				SetLabel(const char* label);
61
62			Menu*				Submenu() const { return fSubMenu; }
63			void				SetSubmenu(Menu* subMenu);
64
65			Menu*				Supermenu() const { return fMenu; }
66
67private:
68			friend class Menu;
69			void				SetMenu(Menu* menu);
70
71private:
72			const char*			fLabel;
73			menu_item_hook		fTarget;
74			bool				fIsMarked;
75			bool				fIsSelected;
76			bool				fIsEnabled;
77			menu_item_type		fType;
78			Menu*				fMenu;
79			Menu*				fSubMenu;
80			const void*			fData;
81			const char*			fHelpText;
82			char				fShortcut;
83};
84
85
86typedef DoublyLinkedList<MenuItem> MenuItemList;
87typedef MenuItemList::Iterator MenuItemIterator;
88
89
90enum menu_type {
91	MAIN_MENU = 1,
92	SAFE_MODE_MENU,
93	STANDARD_MENU,
94	CHOICE_MENU,
95};
96
97
98class Menu {
99public:
100								Menu(menu_type type, const char* title = NULL);
101	virtual						~Menu();
102
103			menu_type			Type() const { return fType; }
104
105	virtual	void				Entered();
106	virtual	void				Exited();
107
108			void				Hide() { fIsHidden = true; }
109			void				Show() { fIsHidden = false; }
110			bool				IsHidden() const { return fIsHidden; }
111
112			MenuItemIterator	ItemIterator() { return fItems.GetIterator(); }
113			MenuItem*			ItemAt(int32 index);
114			int32				IndexOf(MenuItem* item);
115			int32				CountItems() const;
116
117			MenuItem*			FindItem(const char* label);
118			MenuItem*			FindMarked();
119			MenuItem*			FindSelected(int32* _index = NULL);
120
121			void				AddItem(MenuItem* item);
122			status_t			AddSeparatorItem();
123
124			MenuItem*			RemoveItemAt(int32 index);
125			void				RemoveItem(MenuItem* item);
126
127			MenuItem*			Superitem() const { return fSuperItem; }
128			Menu*				Supermenu() const
129									{ return fSuperItem
130										? fSuperItem->fMenu : NULL; }
131
132			const char*			Title() const { return fTitle; }
133			void				SetTitle(const char* title)
134									{ fTitle = title; }
135
136			void				SetChoiceText(const char* text)
137									{ fChoiceText = text; }
138			const char*			ChoiceText() const { return fChoiceText; }
139
140			void				AddShortcut(char key, shortcut_hook function);
141			shortcut_hook		FindShortcut(char key) const;
142			MenuItem*			FindItemByShortcut(char key);
143
144			void				SortItems(bool (*less)(const MenuItem*,
145									const MenuItem*));
146
147			void				Run();
148
149private:
150			friend class MenuItem;
151			void				Draw(MenuItem* item);
152
153private:
154			struct shortcut {
155				shortcut*		next;
156				shortcut_hook	function;
157				char			key;
158			};
159
160			const char*			fTitle;
161			const char*			fChoiceText;
162			int32				fCount;
163			bool				fIsHidden;
164			MenuItemList		fItems;
165			menu_type			fType;
166			MenuItem*			fSuperItem;
167			shortcut*			fShortcuts;
168};
169
170
171#endif	/* KERNEL_BOOT_MENU_H */
172