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

serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 95a9cbc was 95a9cbc, checked in by jxsvoboda <5887334+jxsvoboda@…>, 5 years ago

UI menu unit tests

  • Property mode set to 100644
File size: 9.0 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 gfx_coord_t width;
72
73 mentry = calloc(1, sizeof(ui_menu_entry_t));
74 if (mentry == NULL)
75 return ENOMEM;
76
77 mentry->caption = str_dup(caption);
78 if (mentry->caption == NULL) {
79 free(mentry);
80 return ENOMEM;
81 }
82
83 mentry->menu = menu;
84 list_append(&mentry->lentries, &menu->entries);
85
86 /* Update accumulated menu entry dimensions */
87 width = ui_menu_entry_width(mentry);
88 if (width > menu->max_w)
89 menu->max_w = width;
90 menu->total_h += ui_menu_entry_height(mentry);
91
92 *rmentry = mentry;
93 return EOK;
94}
95
96/** Destroy menu entry.
97 *
98 * @param mentry Menu entry or @c NULL
99 */
100void ui_menu_entry_destroy(ui_menu_entry_t *mentry)
101{
102 if (mentry == NULL)
103 return;
104
105 list_remove(&mentry->lentries);
106 free(mentry->caption);
107 free(mentry);
108}
109
110/** Set menu entry callback.
111 *
112 * @param mentry Menu entry
113 * @param cb Menu entry callback
114 * @param arg Callback argument
115 */
116void ui_menu_entry_set_cb(ui_menu_entry_t *mentry, ui_menu_entry_cb_t cb,
117 void *arg)
118{
119 mentry->cb = cb;
120 mentry->arg = arg;
121}
122
123/** Get first menu entry in menu.
124 *
125 * @param menu Menu
126 * @return First menu entry or @c NULL if there is none
127 */
128ui_menu_entry_t *ui_menu_entry_first(ui_menu_t *menu)
129{
130 link_t *link;
131
132 link = list_first(&menu->entries);
133 if (link == NULL)
134 return NULL;
135
136 return list_get_instance(link, ui_menu_entry_t, lentries);
137}
138
139/** Get next menu entry in menu.
140 *
141 * @param cur Current menu entry
142 * @return Next menu entry or @c NULL if @a cur is the last one
143 */
144ui_menu_entry_t *ui_menu_entry_next(ui_menu_entry_t *cur)
145{
146 link_t *link;
147
148 link = list_next(&cur->lentries, &cur->menu->entries);
149 if (link == NULL)
150 return NULL;
151
152 return list_get_instance(link, ui_menu_entry_t, lentries);
153}
154
155/** Get width of menu entry.
156 *
157 * @param mentry Menu entry
158 * @return Width in pixels
159 */
160gfx_coord_t ui_menu_entry_width(ui_menu_entry_t *mentry)
161{
162 ui_resource_t *res;
163 gfx_coord_t hpad;
164
165 res = mentry->menu->mbar->res;
166
167 if (res->textmode) {
168 hpad = menu_entry_hpad_text;
169 } else {
170 hpad = menu_entry_hpad;
171 }
172
173 return gfx_text_width(res->font, mentry->caption) + 2 * hpad;
174}
175
176/** Get height of menu entry.
177 *
178 * @param mentry Menu entry
179 * @return Width in pixels
180 */
181gfx_coord_t ui_menu_entry_height(ui_menu_entry_t *mentry)
182{
183 ui_resource_t *res;
184 gfx_font_metrics_t metrics;
185 gfx_coord_t height;
186 gfx_coord_t vpad;
187
188 res = mentry->menu->mbar->res;
189
190 if (res->textmode) {
191 vpad = menu_entry_vpad_text;
192 } else {
193 vpad = menu_entry_vpad;
194 }
195
196 gfx_font_get_metrics(res->font, &metrics);
197 height = metrics.ascent + metrics.descent + 1;
198 return height + 2 * vpad;
199}
200
201/** Paint menu entry.
202 *
203 * @param mentry Menu entry
204 * @param pos Position where to paint entry
205 * @return EOK on success or an error code
206 */
207errno_t ui_menu_entry_paint(ui_menu_entry_t *mentry, gfx_coord2_t *pos)
208{
209 ui_resource_t *res;
210 gfx_text_fmt_t fmt;
211 gfx_color_t *bg_color;
212 const char *caption;
213 ui_menu_entry_geom_t geom;
214 errno_t rc;
215
216 res = mentry->menu->mbar->res;
217
218 ui_menu_entry_get_geom(mentry, pos, &geom);
219
220 gfx_text_fmt_init(&fmt);
221 fmt.halign = gfx_halign_left;
222 fmt.valign = gfx_valign_top;
223
224 caption = mentry->caption;
225
226 if ((mentry->held && mentry->inside) ||
227 mentry == mentry->menu->selected) {
228 fmt.color = res->wnd_sel_text_color;
229 bg_color = res->wnd_sel_text_bg_color;
230 } else {
231 fmt.color = res->wnd_text_color;
232 bg_color = res->wnd_face_color;
233 }
234
235 rc = gfx_set_color(res->gc, bg_color);
236 if (rc != EOK)
237 goto error;
238
239 rc = gfx_fill_rect(res->gc, &geom.outer_rect);
240 if (rc != EOK)
241 goto error;
242
243 rc = gfx_puttext(res->font, &geom.text_pos, &fmt, caption);
244 if (rc != EOK)
245 goto error;
246
247 rc = gfx_update(res->gc);
248 if (rc != EOK)
249 goto error;
250
251 return EOK;
252error:
253 return rc;
254}
255
256/** Handle button press in menu entry.
257 *
258 * @param mentry Menu entry
259 * @param pos Menu entry position
260 */
261void ui_menu_entry_press(ui_menu_entry_t *mentry, gfx_coord2_t *pos)
262{
263 if (mentry->held)
264 return;
265
266 mentry->inside = true;
267 mentry->held = true;
268 ui_menu_entry_paint(mentry, pos);
269}
270
271/** Handle button release in menu entry.
272 *
273 * @param mentry Menu entry
274 */
275void ui_menu_entry_release(ui_menu_entry_t *mentry)
276{
277 if (!mentry->held)
278 return;
279
280 mentry->held = false;
281
282 if (mentry->inside) {
283 /* Close menu */
284 ui_menu_bar_select(mentry->menu->mbar,
285 &mentry->menu->mbar->sel_pos, NULL);
286
287 /* Call back */
288 ui_menu_entry_cb(mentry);
289 }
290}
291
292/** Call menu entry callback.
293 *
294 * @param mentry Menu entry
295 */
296void ui_menu_entry_cb(ui_menu_entry_t *mentry)
297{
298 if (mentry->cb != NULL)
299 mentry->cb(mentry, mentry->arg);
300}
301
302/** Pointer entered menu entry.
303 *
304 * @param mentry Menu entry
305 * @param pos Menu entry position
306 */
307void ui_menu_entry_enter(ui_menu_entry_t *mentry, gfx_coord2_t *pos)
308{
309 if (mentry->inside)
310 return;
311
312 mentry->inside = true;
313 if (mentry->held)
314 (void) ui_menu_entry_paint(mentry, pos);
315}
316
317/** Pointer left menu entry.
318 *
319 * @param mentry Menu entry
320 * @param pos Menu entry position
321 */
322void ui_menu_entry_leave(ui_menu_entry_t *mentry, gfx_coord2_t *pos)
323{
324 if (!mentry->inside)
325 return;
326
327 mentry->inside = false;
328 if (mentry->held)
329 (void) ui_menu_entry_paint(mentry, pos);
330}
331
332/** Handle menu entry position event.
333 *
334 * @param mentry Menu entry
335 * @param pos Menu entry position (top-left corner)
336 * @param pos_event Position event
337 * @return @c ui_claimed iff the event is claimed
338 */
339ui_evclaim_t ui_menu_entry_pos_event(ui_menu_entry_t *mentry,
340 gfx_coord2_t *pos, pos_event_t *event)
341{
342 ui_menu_entry_geom_t geom;
343 gfx_coord2_t ppos;
344 bool inside;
345
346 ppos.x = event->hpos;
347 ppos.y = event->vpos;
348
349 ui_menu_entry_get_geom(mentry, pos, &geom);
350 inside = gfx_pix_inside_rect(&ppos, &geom.outer_rect);
351
352 switch (event->type) {
353 case POS_PRESS:
354 if (inside) {
355 ui_menu_entry_press(mentry, pos);
356 return ui_claimed;
357 }
358 break;
359 case POS_RELEASE:
360 if (mentry->held) {
361 ui_menu_entry_release(mentry);
362 return ui_claimed;
363 }
364 break;
365 case POS_UPDATE:
366 if (inside && !mentry->inside) {
367 ui_menu_entry_enter(mentry, pos);
368 return ui_claimed;
369 } else if (!inside && mentry->inside) {
370 ui_menu_entry_leave(mentry, pos);
371 }
372 break;
373 }
374
375 return ui_unclaimed;
376}
377
378/** Get menu entry geometry.
379 *
380 * @param mentry Menu entry
381 * @param spos Entry position
382 * @param geom Structure to fill in with computed geometry
383 */
384void ui_menu_entry_get_geom(ui_menu_entry_t *mentry, gfx_coord2_t *pos,
385 ui_menu_entry_geom_t *geom)
386{
387 ui_resource_t *res;
388 gfx_coord_t hpad;
389 gfx_coord_t vpad;
390 gfx_coord_t width;
391
392 res = mentry->menu->mbar->res;
393
394 if (res->textmode) {
395 hpad = menu_entry_hpad_text;
396 vpad = menu_entry_vpad_text;
397 } else {
398 hpad = menu_entry_hpad;
399 vpad = menu_entry_vpad;
400 }
401
402 width = mentry->menu->max_w;
403 geom->text_pos.x = pos->x + hpad;
404 geom->text_pos.y = pos->y + vpad;
405
406 geom->outer_rect.p0 = *pos;
407 geom->outer_rect.p1.x = geom->outer_rect.p0.x + width;
408 geom->outer_rect.p1.y = geom->outer_rect.p0.y +
409 ui_menu_entry_height(mentry);
410}
411
412/** @}
413 */
Note: See TracBrowser for help on using the repository browser.