source: mainline/uspace/lib/ui/src/menu.c@ d65accb

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

Close menu when button is pressed outside of it

  • Property mode set to 100644
File size: 7.2 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
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/menu.h>
48#include <ui/menuentry.h>
49#include <ui/resource.h>
50#include "../private/menubar.h"
51#include "../private/menu.h"
52#include "../private/resource.h"
53
54enum {
55 menu_frame_w = 4,
56 menu_frame_h = 4,
57 menu_frame_w_text = 2,
58 menu_frame_h_text = 1
59};
60
61/** Create new menu.
62 *
63 * @param mbar Menu bar
64 * @param caption Caption
65 * @param rmenu Place to store pointer to new menu
66 * @return EOK on success, ENOMEM if out of memory
67 */
68errno_t ui_menu_create(ui_menu_bar_t *mbar, const char *caption,
69 ui_menu_t **rmenu)
70{
71 ui_menu_t *menu;
72
73 menu = calloc(1, sizeof(ui_menu_t));
74 if (menu == NULL)
75 return ENOMEM;
76
77 menu->caption = str_dup(caption);
78 if (menu->caption == NULL) {
79 free(menu);
80 return ENOMEM;
81 }
82
83 menu->mbar = mbar;
84 list_append(&menu->lmenus, &mbar->menus);
85 list_initialize(&menu->entries);
86
87 *rmenu = menu;
88 return EOK;
89}
90
91/** Destroy menu.
92 *
93 * @param menu Menu or @c NULL
94 */
95void ui_menu_destroy(ui_menu_t *menu)
96{
97 ui_menu_entry_t *mentry;
98
99 if (menu == NULL)
100 return;
101
102 /* Destroy entries */
103 mentry = ui_menu_entry_first(menu);
104 while (mentry != NULL) {
105 ui_menu_entry_destroy(mentry);
106 mentry = ui_menu_entry_first(menu);
107 }
108
109 list_remove(&menu->lmenus);
110 free(menu->caption);
111 free(menu);
112}
113
114/** Get first menu in menu bar.
115 *
116 * @param mbar Menu bar
117 * @return First menu or @c NULL if there is none
118 */
119ui_menu_t *ui_menu_first(ui_menu_bar_t *mbar)
120{
121 link_t *link;
122
123 link = list_first(&mbar->menus);
124 if (link == NULL)
125 return NULL;
126
127 return list_get_instance(link, ui_menu_t, lmenus);
128}
129
130/** Get next menu in menu bar.
131 *
132 * @param cur Current menu
133 * @return Next menu or @c NULL if @a cur is the last one
134 */
135ui_menu_t *ui_menu_next(ui_menu_t *cur)
136{
137 link_t *link;
138
139 link = list_next(&cur->lmenus, &cur->mbar->menus);
140 if (link == NULL)
141 return NULL;
142
143 return list_get_instance(link, ui_menu_t, lmenus);
144}
145
146/** Get menu caption.
147 *
148 * @param menu Menu
149 * @return Caption (owned by @a menu)
150 */
151const char *ui_menu_caption(ui_menu_t *menu)
152{
153 return menu->caption;
154}
155
156/** Get menu geometry.
157 *
158 * @param menu Menu
159 * @param spos Starting position
160 * @param geom Structure to fill in with computed geometry
161 */
162void ui_menu_get_geom(ui_menu_t *menu, gfx_coord2_t *spos,
163 ui_menu_geom_t *geom)
164{
165 gfx_coord2_t edim;
166 gfx_coord_t frame_w;
167 gfx_coord_t frame_h;
168
169 if (menu->mbar->res->textmode) {
170 frame_w = menu_frame_w_text;
171 frame_h = menu_frame_h_text;
172 } else {
173 frame_w = menu_frame_w;
174 frame_h = menu_frame_h;
175 }
176
177 edim.x = menu->max_w;
178 edim.y = menu->total_h;
179
180 geom->outer_rect.p0 = *spos;
181 geom->outer_rect.p1.x = spos->x + edim.x + 2 * frame_w;
182 geom->outer_rect.p1.y = spos->y + edim.y + 2 * frame_h;
183
184 geom->entries_rect.p0.x = spos->x + frame_w;
185 geom->entries_rect.p0.y = spos->y + frame_h;
186 geom->entries_rect.p1.x = geom->entries_rect.p0.x + edim.x;
187 geom->entries_rect.p1.y = geom->entries_rect.p0.x + edim.y;
188}
189
190/** Get menu rectangle.
191 *
192 * @param menu Menu
193 * @param spos Starting position (top-left corner)
194 * @param rect Place to store menu rectangle
195 */
196void ui_menu_get_rect(ui_menu_t *menu, gfx_coord2_t *spos, gfx_rect_t *rect)
197{
198 ui_menu_geom_t geom;
199
200 ui_menu_get_geom(menu, spos, &geom);
201 *rect = geom.outer_rect;
202}
203
204/** Paint menu.
205 *
206 * @param menu Menu
207 * @param spos Starting position (top-left corner)
208 * @return EOK on success or an error code
209 */
210errno_t ui_menu_paint(ui_menu_t *menu, gfx_coord2_t *spos)
211{
212 ui_resource_t *res;
213 gfx_coord2_t pos;
214 ui_menu_entry_t *mentry;
215 ui_menu_geom_t geom;
216 gfx_rect_t bg_rect;
217 errno_t rc;
218
219 res = menu->mbar->res;
220 ui_menu_get_geom(menu, spos, &geom);
221
222 /* Paint menu frame */
223
224 rc = gfx_set_color(res->gc, res->wnd_face_color);
225 if (rc != EOK)
226 goto error;
227
228 rc = ui_paint_outset_frame(res, &geom.outer_rect, &bg_rect);
229 if (rc != EOK)
230 goto error;
231
232 /* Paint menu background */
233
234 rc = gfx_set_color(res->gc, res->wnd_face_color);
235 if (rc != EOK)
236 goto error;
237
238 rc = gfx_fill_rect(res->gc, &bg_rect);
239 if (rc != EOK)
240 goto error;
241
242 /* Paint entries */
243 pos = geom.entries_rect.p0;
244
245 mentry = ui_menu_entry_first(menu);
246 while (mentry != NULL) {
247 rc = ui_menu_entry_paint(mentry, &pos);
248 if (rc != EOK)
249 goto error;
250
251 pos.y += ui_menu_entry_height(mentry);
252 mentry = ui_menu_entry_next(mentry);
253 }
254
255 rc = gfx_update(res->gc);
256 if (rc != EOK)
257 goto error;
258
259 return EOK;
260error:
261 return rc;
262}
263
264/** Unpaint menu.
265 *
266 * @param menu Menu
267 * @return EOK on success or an error code
268 */
269errno_t ui_menu_unpaint(ui_menu_t *menu)
270{
271 ui_resource_expose(menu->mbar->res);
272 return EOK;
273}
274
275/** Handle position event in menu.
276 *
277 * @param menu Menu
278 * @param spos Starting position (top-left corner)
279 * @param event Position event
280 * @return ui_claimed iff the event was claimed
281 */
282ui_evclaim_t ui_menu_pos_event(ui_menu_t *menu, gfx_coord2_t *spos,
283 pos_event_t *event)
284{
285 ui_menu_geom_t geom;
286 ui_menu_entry_t *mentry;
287 gfx_coord2_t pos;
288 gfx_coord2_t epos;
289 ui_evclaim_t claimed;
290
291 ui_menu_get_geom(menu, spos, &geom);
292 epos.x = event->hpos;
293 epos.y = event->vpos;
294
295 pos = geom.entries_rect.p0;
296
297 mentry = ui_menu_entry_first(menu);
298 while (mentry != NULL) {
299 claimed = ui_menu_entry_pos_event(mentry, &pos, event);
300 if (claimed == ui_claimed)
301 return ui_claimed;
302
303 pos.y += ui_menu_entry_height(mentry);
304 mentry = ui_menu_entry_next(mentry);
305 }
306
307 /* Event inside menu? */
308 if (gfx_pix_inside_rect(&epos, &geom.outer_rect)) {
309 /* Claim event */
310 return ui_claimed;
311 } else {
312 /* Press outside menu - close it */
313 if (event->type == POS_PRESS)
314 ui_menu_bar_select(menu->mbar, NULL, NULL);
315 }
316
317 return ui_unclaimed;
318}
319
320/** @}
321 */
Note: See TracBrowser for help on using the repository browser.