source: mainline/uspace/lib/ui/src/menuentry.c@ bfb055b

Last change on this file since bfb055b was bfb055b, checked in by Jiri Svoboda <jiri@…>, 4 years ago

UI menu (WIP)

  • Property mode set to 100644
File size: 7.1 KB
Line 
1/*
2 * Copyright (c) 2021 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libui
30 * @{
31 */
32/**
33 * @file Menu entry
34 */
35
36#include <adt/list.h>
37#include <errno.h>
38#include <gfx/color.h>
39#include <gfx/context.h>
40#include <gfx/render.h>
41#include <gfx/text.h>
42#include <io/pos_event.h>
43#include <stdlib.h>
44#include <str.h>
45#include <ui/control.h>
46#include <ui/paint.h>
47#include <ui/menuentry.h>
48#include "../private/menubar.h"
49#include "../private/menuentry.h"
50#include "../private/menu.h"
51#include "../private/resource.h"
52
53enum {
54 menu_entry_hpad = 4,
55 menu_entry_vpad = 4,
56 menu_entry_hpad_text = 1,
57 menu_entry_vpad_text = 0
58};
59
60/** Create new menu entry.
61 *
62 * @param menu Menu
63 * @param caption Caption
64 * @param rmentry Place to store pointer to new menu entry
65 * @return EOK on success, ENOMEM if out of memory
66 */
67errno_t ui_menu_entry_create(ui_menu_t *menu, const char *caption,
68 ui_menu_entry_t **rmentry)
69{
70 ui_menu_entry_t *mentry;
71
72 mentry = calloc(1, sizeof(ui_menu_entry_t));
73 if (mentry == NULL)
74 return ENOMEM;
75
76 mentry->caption = str_dup(caption);
77 if (mentry->caption == NULL) {
78 free(mentry);
79 return ENOMEM;
80 }
81
82 mentry->menu = menu;
83 list_append(&mentry->lentries, &menu->entries);
84
85 *rmentry = mentry;
86 return EOK;
87}
88
89/** Destroy menu entry.
90 *
91 * @param mentry Menu entry or @c NULL
92 */
93void ui_menu_entry_destroy(ui_menu_entry_t *mentry)
94{
95 if (mentry == NULL)
96 return;
97
98 list_remove(&mentry->lentries);
99 free(mentry->caption);
100 free(mentry);
101}
102
103/** Set menu entry callback.
104 *
105 * @param mentry Menu entry
106 * @param cb Menu entry callback
107 * @param arg Callback argument
108 */
109void ui_menu_entry_set_cb(ui_menu_entry_t *mentry, ui_menu_entry_cb_t cb,
110 void *arg)
111{
112 mentry->cb = cb;
113 mentry->arg = arg;
114}
115
116/** Get first menu entry in menu.
117 *
118 * @param menu Menu
119 * @return First menu entry or @c NULL if there is none
120 */
121ui_menu_entry_t *ui_menu_entry_first(ui_menu_t *menu)
122{
123 link_t *link;
124
125 link = list_first(&menu->entries);
126 if (link == NULL)
127 return NULL;
128
129 return list_get_instance(link, ui_menu_entry_t, lentries);
130}
131
132/** Get next menu entry in menu.
133 *
134 * @param cur Current menu entry
135 * @return Next menu entry or @c NULL if @a cur is the last one
136 */
137ui_menu_entry_t *ui_menu_entry_next(ui_menu_entry_t *cur)
138{
139 link_t *link;
140
141 link = list_next(&cur->lentries, &cur->menu->entries);
142 if (link == NULL)
143 return NULL;
144
145 return list_get_instance(link, ui_menu_entry_t, lentries);
146}
147
148/** Get width of menu entry.
149 *
150 * @param mentry Menu entry
151 * @return Width in pixels
152 */
153gfx_coord_t ui_menu_entry_width(ui_menu_entry_t *mentry)
154{
155 ui_resource_t *res;
156 gfx_coord_t hpad;
157
158 res = mentry->menu->mbar->res;
159
160 if (res->textmode) {
161 hpad = menu_entry_hpad_text;
162 } else {
163 hpad = menu_entry_hpad;
164 }
165
166 return gfx_text_width(res->font, mentry->caption) + 2 * hpad;
167}
168
169/** Get height of menu entry.
170 *
171 * @param mentry Menu entry
172 * @return Width in pixels
173 */
174gfx_coord_t ui_menu_entry_height(ui_menu_entry_t *mentry)
175{
176 ui_resource_t *res;
177 gfx_font_metrics_t metrics;
178 gfx_coord_t height;
179 gfx_coord_t vpad;
180
181 res = mentry->menu->mbar->res;
182
183 if (res->textmode) {
184 vpad = menu_entry_vpad_text;
185 } else {
186 vpad = menu_entry_vpad;
187 }
188
189 gfx_font_get_metrics(res->font, &metrics);
190 height = metrics.ascent + metrics.descent;
191 return height + 2 * vpad;
192}
193
194/** Paint menu entry.
195 *
196 * @param mentry Menu entry
197 * @param pos Position where to paint entry
198 * @return EOK on success or an error code
199 */
200errno_t ui_menu_entry_paint(ui_menu_entry_t *mentry, gfx_coord2_t *pos)
201{
202 ui_resource_t *res;
203 gfx_text_fmt_t fmt;
204 gfx_color_t *bg_color;
205 const char *caption;
206 ui_menu_entry_geom_t geom;
207 errno_t rc;
208
209 res = mentry->menu->mbar->res;
210
211 ui_menu_entry_get_geom(mentry, pos, &geom);
212
213 gfx_text_fmt_init(&fmt);
214 fmt.halign = gfx_halign_left;
215 fmt.valign = gfx_valign_top;
216
217 caption = mentry->caption;
218
219 if (mentry == mentry->menu->selected) {
220 fmt.color = res->wnd_sel_text_color;
221 bg_color = res->wnd_sel_text_bg_color;
222 } else {
223 fmt.color = res->wnd_text_color;
224 bg_color = res->wnd_face_color;
225 }
226
227 rc = gfx_set_color(res->gc, bg_color);
228 if (rc != EOK)
229 goto error;
230
231 rc = gfx_fill_rect(res->gc, &geom.outer_rect);
232 if (rc != EOK)
233 goto error;
234
235 rc = gfx_puttext(res->font, &geom.text_pos, &fmt, caption);
236 if (rc != EOK)
237 goto error;
238
239 rc = gfx_update(res->gc);
240 if (rc != EOK)
241 goto error;
242
243 return EOK;
244error:
245 return rc;
246}
247
248/** Handle button press in menu entry.
249 *
250 * @param mentry Menu entry
251 * @param pos Position (top-left corner)
252 * @param ppos Press position
253 */
254void ui_menu_entry_press(ui_menu_entry_t *mentry, gfx_coord2_t *pos,
255 gfx_coord2_t *ppos)
256{
257 ui_menu_entry_geom_t geom;
258
259 ui_menu_entry_get_geom(mentry, pos, &geom);
260
261 if (gfx_pix_inside_rect(ppos, &geom.outer_rect)) {
262 /* Press inside menu entry */
263
264 /* Close menu */
265 ui_menu_bar_select(mentry->menu->mbar,
266 &mentry->menu->mbar->sel_pos, NULL);
267
268 /* Call back */
269 if (mentry->cb != NULL)
270 mentry->cb(mentry, mentry->arg);
271 }
272}
273
274/** Get menu entry geometry.
275 *
276 * @param mentry Menu entry
277 * @param spos Entry position
278 * @param geom Structure to fill in with computed geometry
279 */
280void ui_menu_entry_get_geom(ui_menu_entry_t *mentry, gfx_coord2_t *pos,
281 ui_menu_entry_geom_t *geom)
282{
283 ui_resource_t *res;
284 gfx_coord_t hpad;
285 gfx_coord_t vpad;
286 const char *caption;
287 gfx_coord_t width;
288
289 res = mentry->menu->mbar->res;
290
291 if (res->textmode) {
292 hpad = menu_entry_hpad_text;
293 vpad = menu_entry_vpad_text;
294 } else {
295 hpad = menu_entry_hpad;
296 vpad = menu_entry_vpad;
297 }
298
299 caption = mentry->caption;
300 width = gfx_text_width(res->font, caption) + 2 * hpad;
301 geom->text_pos.x = pos->x + hpad;
302 geom->text_pos.y = pos->y + vpad;
303
304 geom->outer_rect.p0 = *pos;
305 geom->outer_rect.p1.x = geom->outer_rect.p0.x + width;
306 geom->outer_rect.p1.y = geom->outer_rect.p0.y +
307 ui_menu_entry_height(mentry);
308}
309
310/** @}
311 */
Note: See TracBrowser for help on using the repository browser.