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

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

Open menu in separate popup window (WIP)

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