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