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

serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6c0766b was 81ec7e1, checked in by Jiri Svoboda <jiri@…>, 4 years ago

Brace for a text mode menu separator entry

Pun intended.

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