]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/nc-menu.h
ui/ncurses: Remove pmenu_item_replace
[petitboot] / ui / ncurses / nc-menu.h
1 /*
2  *  Copyright (C) 2009 Sony Computer Entertainment Inc.
3  *  Copyright 2009 Sony Corp.
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; version 2 of the License.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #if !defined(_PB_NC_MENU_H)
20 #define _PB_NC_MENU_H
21
22 #include <assert.h>
23
24 #include <linux/input.h> /* This must be included before ncurses.h */
25 #if defined HAVE_NCURSESW_MENU_H
26 #  include <ncursesw/menu.h>
27 #elif defined HAVE_NCURSES_MENU_H
28 #  include <ncurses/menu.h>
29 #elif defined HAVE_MENU_H
30 #  include <menu.h>
31 #else
32 #  error "Curses menu.h not found."
33 #endif
34
35 #include "log/log.h"
36 #include "types/types.h"
37 #include "nc-scr.h"
38
39 struct pmenu;
40
41 /**
42  * struct pmenu_item - Hold the state of a single menu item.
43  * @i_sig: Signature for callback type checking, should be pmenu_item_sig.
44  * @nci: The ncurses menu item instance for this item.
45  */
46
47 struct pmenu_item {
48         enum pb_nc_sig i_sig;
49         ITEM *nci;
50         struct pmenu *pmenu;
51         void *data;
52         void (*on_edit)(struct pmenu_item *item);
53         int (*on_execute)(struct pmenu_item *item);
54 };
55
56 struct pmenu_item *pmenu_item_alloc(struct pmenu *menu);
57 struct pmenu_item *pmenu_item_setup(struct pmenu *menu, struct pmenu_item *i,
58         unsigned int index, const char *name);
59 void pmenu_item_delete(struct pmenu_item *item);
60
61 static inline struct pmenu_item *pmenu_item_from_arg(void *arg)
62 {
63         struct pmenu_item *item = (struct pmenu_item *)arg;
64
65         assert(item->i_sig == pb_item_sig);
66         return item;
67 }
68
69 static inline struct cui_opt_data *cod_from_item(struct pmenu_item *item)
70 {
71         return item->data;
72 }
73
74 static inline struct pmenu_item *pmenu_item_init(struct pmenu *menu,
75         unsigned int index, const char *name)
76 {
77         struct pmenu_item *item = pmenu_item_alloc(menu);
78
79         if (pmenu_item_setup(menu, item, index, name)) {
80                 talloc_free(item);
81                 item = NULL;
82         }
83
84         return item;
85 }
86
87 /**
88  * struct pmenu - Data structure defining complete menu.
89  * @insert_pt: Index in nc item array.
90  * @ncm: The ncurses menu instance for this menu.
91  */
92
93 struct pmenu {
94         struct nc_scr scr;
95         MENU *ncm;
96         ITEM **items;
97         unsigned int item_count;
98         unsigned int insert_pt;
99         const char *help_title;
100         const char *help_text;
101         int (*hot_key)(struct pmenu *menu, struct pmenu_item *item, int c);
102         void (*on_exit)(struct pmenu *menu);
103         void (*on_new)(struct pmenu *menu);
104 };
105
106 struct pmenu *pmenu_init(void *ui_ctx, unsigned int item_count,
107         void (*on_exit)(struct pmenu *));
108 int pmenu_setup(struct pmenu *menu);
109 void pmenu_delete(struct pmenu *menu);
110 unsigned int pmenu_grow(struct pmenu *menu, unsigned int count);
111 int pmenu_remove(struct pmenu *menu, struct pmenu_item *item);
112 struct pmenu_item *pmenu_find_selected(struct pmenu *menu);
113
114 /* convenience routines */
115
116 int pmenu_exit_cb(struct pmenu_item *item);
117
118 static inline struct pmenu *pmenu_from_scr(struct nc_scr *scr)
119 {
120         struct pmenu *pmenu;
121
122         assert(scr->sig == pb_pmenu_sig);
123         pmenu = (struct pmenu *)((char *)scr
124                 - (size_t)&((struct pmenu *)0)->scr);
125         assert(pmenu->scr.sig == pb_pmenu_sig);
126
127         return pmenu;
128 }
129
130 /* debug routines */
131
132 static inline void pmenu_dump_item(const ITEM *item)
133 {
134         pb_debug("%p %s\n", item, (item ? item->name.str : "(null)"));
135 }
136
137 static inline void pmenu_dump_items(ITEM *const *items, unsigned int count)
138 {
139         unsigned int i;
140
141         for (i = 0; i < count; i++)
142                 pb_debug("%u: %p %s\n", i, items[i],
143                         (items[i] ? items[i]->name.str : "(null)"));
144 }
145
146 #endif