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

Last change on this file since 6baab83 was 6baab83, checked in by Jiri Svoboda <jiri@…>, 4 years ago

Set menu popup position based on parent window position

Added a method for getting the position of a display window.
This is then combined with the menu bar entry rectangle (which is
relative to the parent window) to compute a screen-relative
rectangle close to which the popup should be placed.

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