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

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

Specify parent window when creating popup

This will be used in conjunction with ui_wnd_popup_params_t.place
(a rectangle relative to the parent window) to determine where on
the screen the popup window should appear.

  • 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 <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 rect.p0 = geom.caption_pos;
352 rect.p1.x = geom.shortcut_pos.x;
353 rect.p1.y = rect.p0.y + 2;
354 rc = ui_paint_bevel(res->gc, &rect, res->wnd_shadow_color,
355 res->wnd_highlight_color, 1, NULL);
356 if (rc != EOK)
357 goto error;
358 }
359
360 rc = gfx_update(res->gc);
361 if (rc != EOK)
362 goto error;
363
364 return EOK;
365error:
366 return rc;
367}
368
369/** Handle button press in menu entry.
370 *
371 * @param mentry Menu entry
372 * @param pos Menu entry position
373 */
374void ui_menu_entry_press(ui_menu_entry_t *mentry, gfx_coord2_t *pos)
375{
376 if (mentry->held)
377 return;
378
379 if (mentry->separator)
380 return;
381
382 mentry->inside = true;
383 mentry->held = true;
384 ui_menu_entry_paint(mentry, pos);
385}
386
387/** Handle button release in menu entry.
388 *
389 * @param mentry Menu entry
390 */
391void ui_menu_entry_release(ui_menu_entry_t *mentry)
392{
393 if (!mentry->held)
394 return;
395
396 mentry->held = false;
397
398 if (mentry->inside) {
399 /* Close menu */
400 ui_menu_bar_select(mentry->menu->mbar, NULL, NULL);
401
402 /* Call back */
403 ui_menu_entry_cb(mentry);
404 }
405}
406
407/** Call menu entry callback.
408 *
409 * @param mentry Menu entry
410 */
411void ui_menu_entry_cb(ui_menu_entry_t *mentry)
412{
413 if (mentry->cb != NULL)
414 mentry->cb(mentry, mentry->arg);
415}
416
417/** Pointer entered menu entry.
418 *
419 * @param mentry Menu entry
420 * @param pos Menu entry position
421 */
422void ui_menu_entry_enter(ui_menu_entry_t *mentry, gfx_coord2_t *pos)
423{
424 if (mentry->inside)
425 return;
426
427 mentry->inside = true;
428 if (mentry->held)
429 (void) ui_menu_entry_paint(mentry, pos);
430}
431
432/** Pointer left menu entry.
433 *
434 * @param mentry Menu entry
435 * @param pos Menu entry position
436 */
437void ui_menu_entry_leave(ui_menu_entry_t *mentry, gfx_coord2_t *pos)
438{
439 if (!mentry->inside)
440 return;
441
442 mentry->inside = false;
443 if (mentry->held)
444 (void) ui_menu_entry_paint(mentry, pos);
445}
446
447/** Handle menu entry position event.
448 *
449 * @param mentry Menu entry
450 * @param pos Menu entry position (top-left corner)
451 * @param pos_event Position event
452 * @return @c ui_claimed iff the event is claimed
453 */
454ui_evclaim_t ui_menu_entry_pos_event(ui_menu_entry_t *mentry,
455 gfx_coord2_t *pos, pos_event_t *event)
456{
457 ui_menu_entry_geom_t geom;
458 gfx_coord2_t ppos;
459 bool inside;
460
461 ppos.x = event->hpos;
462 ppos.y = event->vpos;
463
464 ui_menu_entry_get_geom(mentry, pos, &geom);
465 inside = gfx_pix_inside_rect(&ppos, &geom.outer_rect);
466
467 switch (event->type) {
468 case POS_PRESS:
469 if (inside) {
470 ui_menu_entry_press(mentry, pos);
471 return ui_claimed;
472 }
473 break;
474 case POS_RELEASE:
475 if (mentry->held) {
476 ui_menu_entry_release(mentry);
477 return ui_claimed;
478 }
479 break;
480 case POS_UPDATE:
481 if (inside && !mentry->inside) {
482 ui_menu_entry_enter(mentry, pos);
483 return ui_claimed;
484 } else if (!inside && mentry->inside) {
485 ui_menu_entry_leave(mentry, pos);
486 }
487 break;
488 }
489
490 return ui_unclaimed;
491}
492
493/** Get menu entry geometry.
494 *
495 * @param mentry Menu entry
496 * @param spos Entry position
497 * @param geom Structure to fill in with computed geometry
498 */
499void ui_menu_entry_get_geom(ui_menu_entry_t *mentry, gfx_coord2_t *pos,
500 ui_menu_entry_geom_t *geom)
501{
502 ui_resource_t *res;
503 gfx_coord_t hpad;
504 gfx_coord_t vpad;
505 gfx_coord_t width;
506
507 res = ui_menu_get_res(mentry->menu);
508
509 if (res->textmode) {
510 hpad = menu_entry_hpad_text;
511 vpad = menu_entry_vpad_text;
512 } else {
513 hpad = menu_entry_hpad;
514 vpad = menu_entry_vpad;
515 }
516
517 /* Compute total width of menu entry */
518 width = ui_menu_entry_calc_width(mentry->menu,
519 mentry->menu->max_caption_w, mentry->menu->max_shortcut_w);
520
521 geom->outer_rect.p0 = *pos;
522 geom->outer_rect.p1.x = geom->outer_rect.p0.x + width;
523 geom->outer_rect.p1.y = geom->outer_rect.p0.y +
524 ui_menu_entry_height(mentry);
525
526 geom->caption_pos.x = pos->x + hpad;
527 geom->caption_pos.y = pos->y + vpad;
528
529 geom->shortcut_pos.x = geom->outer_rect.p1.x - hpad;
530 geom->shortcut_pos.y = pos->y + vpad;
531}
532
533/** @}
534 */
Note: See TracBrowser for help on using the repository browser.