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

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

Menu entries need to stretch to the width of the menu

  • Property mode set to 100644
File size: 8.9 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;
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 if (mentry->cb != NULL)
289 mentry->cb(mentry, mentry->arg);
290 }
291}
292
293/** Pointer entered menu entry.
294 *
295 * @param mentry Menu entry
296 * @param pos Menu entry position
297 */
298void ui_menu_entry_enter(ui_menu_entry_t *mentry, gfx_coord2_t *pos)
299{
300 if (mentry->inside)
301 return;
302
303 mentry->inside = true;
304 if (mentry->held)
305 (void) ui_menu_entry_paint(mentry, pos);
306}
307
308/** Pointer left menu entry.
309 *
310 * @param mentry Menu entry
311 * @param pos Menu entry position
312 */
313void ui_menu_entry_leave(ui_menu_entry_t *mentry, gfx_coord2_t *pos)
314{
315 if (!mentry->inside)
316 return;
317
318 mentry->inside = false;
319 if (mentry->held)
320 (void) ui_menu_entry_paint(mentry, pos);
321}
322
323/** Handle menu entry position event.
324 *
325 * @param mentry Menu entry
326 * @param pos Menu entry position (top-left corner)
327 * @param pos_event Position event
328 * @return @c ui_claimed iff the event is claimed
329 */
330ui_evclaim_t ui_menu_entry_pos_event(ui_menu_entry_t *mentry,
331 gfx_coord2_t *pos, pos_event_t *event)
332{
333 ui_menu_entry_geom_t geom;
334 gfx_coord2_t ppos;
335 bool inside;
336
337 ppos.x = event->hpos;
338 ppos.y = event->vpos;
339
340 ui_menu_entry_get_geom(mentry, pos, &geom);
341 inside = gfx_pix_inside_rect(&ppos, &geom.outer_rect);
342
343 switch (event->type) {
344 case POS_PRESS:
345 if (inside) {
346 ui_menu_entry_press(mentry, pos);
347 return ui_claimed;
348 }
349 break;
350 case POS_RELEASE:
351 if (mentry->held) {
352 ui_menu_entry_release(mentry);
353 return ui_claimed;
354 }
355 break;
356 case POS_UPDATE:
357 if (inside && !mentry->inside) {
358 ui_menu_entry_enter(mentry, pos);
359 return ui_claimed;
360 } else if (!inside && mentry->inside) {
361 ui_menu_entry_leave(mentry, pos);
362 }
363 break;
364 }
365
366 return ui_unclaimed;
367}
368
369/** Get menu entry geometry.
370 *
371 * @param mentry Menu entry
372 * @param spos Entry position
373 * @param geom Structure to fill in with computed geometry
374 */
375void ui_menu_entry_get_geom(ui_menu_entry_t *mentry, gfx_coord2_t *pos,
376 ui_menu_entry_geom_t *geom)
377{
378 ui_resource_t *res;
379 gfx_coord_t hpad;
380 gfx_coord_t vpad;
381 gfx_coord_t width;
382
383 res = mentry->menu->mbar->res;
384
385 if (res->textmode) {
386 hpad = menu_entry_hpad_text;
387 vpad = menu_entry_vpad_text;
388 } else {
389 hpad = menu_entry_hpad;
390 vpad = menu_entry_vpad;
391 }
392
393 width = mentry->menu->max_w;
394 geom->text_pos.x = pos->x + hpad;
395 geom->text_pos.y = pos->y + vpad;
396
397 geom->outer_rect.p0 = *pos;
398 geom->outer_rect.p1.x = geom->outer_rect.p0.x + width;
399 geom->outer_rect.p1.y = geom->outer_rect.p0.y +
400 ui_menu_entry_height(mentry);
401}
402
403/** @}
404 */
Note: See TracBrowser for help on using the repository browser.