]> git.ozlabs.org Git - petitboot/blob - ui/twin/pbt-menu.h
7547f16a3c08800a5e0ebed822a97320f6c3500f
[petitboot] / ui / twin / pbt-menu.h
1 /*
2  *  Copyright Geoff Levand <geoff@infradead.org>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; version 2 of the License.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License
14  *  along with this program; if not, write to the Free Software
15  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  */
17
18 #if !defined(_PBT_MENU_H)
19 #define _PBT_MENU_H
20
21 #include "list/list.h"
22 #include "pb-protocol/pb-protocol.h"
23
24 #include "pbt-scr.h"
25
26
27 /**
28  * struct pbt_item - A menu item.
29  */
30
31 struct pbt_item
32 {
33         struct list_item list;
34
35         struct pbt_menu *menu; // convinence pointer
36         struct pbt_client *pbt_client; // convinence pointer
37
38         twin_window_t *window;
39         twin_pixmap_t *pixmap_idle;
40         twin_pixmap_t *pixmap_selected;
41         twin_pixmap_t *pixmap_active;
42
43         struct pbt_menu *sub_menu;
44
45         int (*on_execute)(struct pbt_item *item);
46         int (*on_edit)(struct pbt_item *item);
47
48         union {
49                 struct device *pb_device;
50                 struct boot_option *pb_opt;
51         };
52         void *data;
53 };
54
55 struct pbt_item *pbt_item_create(struct pbt_menu *menu, const char *name,
56         unsigned int position, const char *icon_filename, const char *title,
57         const char *text);
58
59 static inline struct pbt_item *pbt_item_create_reduced(struct pbt_menu *menu,
60         const char *name, unsigned int position, const char *icon_filename)
61 {
62         return pbt_item_create(menu, name, position, icon_filename, NULL,
63                 NULL);
64 }
65
66 static inline const char *pbt_item_name(const struct pbt_item *item)
67 {
68         return item->window->name;
69 }
70
71 #define pbt_dump_item(_i) _pbt_dump_item(_i, __func__, __LINE__)
72 void _pbt_dump_item(const struct pbt_item* item, const char *func,
73         int line);
74
75 struct pbt_text_layout {
76         twin_argb32_t color;
77         unsigned int font_size;
78 };
79
80 struct pbt_menu_layout {
81         unsigned int item_height;
82         unsigned int item_space;
83         unsigned int text_space;
84         //unsigned int icon_height;
85         //unsigned int icon_width;
86         struct pbt_text_layout title;
87         struct pbt_text_layout text;
88 };
89
90  /**
91   * struct pbt_menu - A twin menu screen.
92   */
93
94 struct pbt_menu {
95         struct pbt_scr *scr; // convinence pointer
96         struct pbt_menu *parent;
97         twin_window_t *window;
98         twin_pixmap_t *pixmap;
99
100         struct pbt_border border;
101         twin_argb32_t background_color;
102         struct pbt_menu_layout layout;
103
104         struct list* item_list;
105         unsigned int n_items;
106         uint32_t default_item_hash;
107
108         int has_focus;
109         struct pbt_item *selected;
110         int (*on_open)(struct pbt_menu *menu);
111 };
112
113 struct pbt_menu *pbt_menu_create(void *talloc_ctx, const char *name,
114         struct pbt_scr *scr, struct pbt_menu *parent, const struct pbt_quad *q,
115         const struct pbt_menu_layout *layout);
116 void pbt_menu_set_focus(struct pbt_menu *menu, int focus);
117 void pbt_menu_set_selected(struct pbt_menu *menu, struct pbt_item *item);
118 struct pbt_quad *pbt_menu_get_item_quad(const struct pbt_menu *menu,
119         unsigned int pos, struct pbt_quad *q);
120 void pbt_menu_hide(struct pbt_menu *menu);
121 void pbt_menu_show(struct pbt_menu *menu, int hide);
122
123 static inline const char *pbt_menu_name(const struct pbt_menu *menu)
124 {
125         return menu->window->name;
126 }
127
128 static inline struct pbt_menu *pbt_menu_from_window(twin_window_t *window)
129 {
130         struct pbt_menu *menu = window->client_data;
131
132         assert(menu);
133         return menu;
134 }
135
136 static inline struct pbt_item *pbt_item_from_window(twin_window_t *window)
137 {
138         struct pbt_item *item = window->client_data;
139
140         assert(item);
141         return item;
142 }
143
144 static inline int pbt_item_is_selected(const struct pbt_item* item)
145 {
146         return item == item->menu->selected;
147 }
148
149 static inline void pbt_item_set_as_selected(struct pbt_item* item)
150 {
151         pbt_menu_set_selected(item->menu, item);
152 }
153
154 int pbt_item_editor(struct pbt_item *item);
155
156 static inline void pbt_item_redraw(struct pbt_item *item)
157 {
158         pbt_window_redraw(item->window);
159 }
160
161 static inline void pbt_menu_redraw(struct pbt_menu *menu)
162 {
163         pbt_window_redraw(menu->window);
164 }
165
166
167 #endif