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								~MenuItem();
34
35			void				SetTarget(menu_item_hook target);
36			menu_item_hook		Target() const { return fTarget; }
37
38			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			Menu*				Submenu() const { return fSubMenu; }
61
62private:
63			friend class Menu;
64			void				SetMenu(Menu* menu);
65
66private:
67			const char*			fLabel;
68			menu_item_hook		fTarget;
69			bool				fIsMarked;
70			bool				fIsSelected;
71			bool				fIsEnabled;
72			menu_item_type		fType;
73			Menu*				fMenu;
74			Menu*				fSubMenu;
75			const void*			fData;
76			const char*			fHelpText;
77			char				fShortcut;
78};
79
80
81typedef DoublyLinkedList<MenuItem> MenuItemList;
82typedef MenuItemList::Iterator MenuItemIterator;
83
84
85enum menu_type {
86	MAIN_MENU = 1,
87	SAFE_MODE_MENU,
88	STANDARD_MENU,
89	CHOICE_MENU,
90};
91
92
93class Menu {
94public:
95								Menu(menu_type type, const char* title = NULL);
96								~Menu();
97
98			menu_type			Type() const { return fType; }
99
100			void				Hide() { fIsHidden = true; }
101			void				Show() { fIsHidden = false; }
102			bool				IsHidden() const { return fIsHidden; }
103
104			MenuItemIterator	ItemIterator() { return fItems.GetIterator(); }
105			MenuItem*			ItemAt(int32 index);
106			int32				IndexOf(MenuItem* item);
107			int32				CountItems() const;
108
109			MenuItem*			FindItem(const char* label);
110			MenuItem*			FindMarked();
111			MenuItem*			FindSelected(int32* _index = NULL);
112
113			void				AddItem(MenuItem* item);
114			status_t			AddSeparatorItem();
115
116			MenuItem*			RemoveItemAt(int32 index);
117			void				RemoveItem(MenuItem* item);
118
119			MenuItem*			Superitem() const { return fSuperItem; }
120			Menu*				Supermenu() const
121									{ return fSuperItem
122										? fSuperItem->fMenu : NULL; }
123
124			const char*			Title() const { return fTitle; }
125
126			void				SetChoiceText(const char* text)
127									{ fChoiceText = text; }
128			const char*			ChoiceText() const { return fChoiceText; }
129
130			void				AddShortcut(char key, shortcut_hook function);
131			shortcut_hook		FindShortcut(char key) const;
132			MenuItem*			FindItemByShortcut(char key);
133
134			void				Run();
135
136private:
137			friend class MenuItem;
138			void				Draw(MenuItem* item);
139
140private:
141			struct shortcut {
142				shortcut*		next;
143				shortcut_hook	function;
144				char			key;
145			};
146
147			const char*			fTitle;
148			const char*			fChoiceText;
149			int32				fCount;
150			bool				fIsHidden;
151			MenuItemList		fItems;
152			menu_type			fType;
153			MenuItem*			fSuperItem;
154			shortcut*			fShortcuts;
155};
156
157
158#endif	/* KERNEL_BOOT_MENU_H */
159