source: mainline/uspace/app/nav/menu.c@ 46bd63c9

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 46bd63c9 was 46bd63c9, checked in by Jiri Svoboda <jiri@…>, 22 months ago

Split drop-down menu into two classes: drop-down and menu

Naming is clearly the hardest problem in computer science.

  • Property mode set to 100644
File size: 4.2 KB
Line 
1/*
2 * Copyright (c) 2023 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 nav
30 * @{
31 */
32/** @file Navigator.
33 *
34 * HelenOS file manager.
35 */
36
37#include <errno.h>
38#include <stdlib.h>
39#include <ui/menu.h>
40#include <ui/menubar.h>
41#include <ui/menudd.h>
42#include <ui/menuentry.h>
43#include "menu.h"
44#include "nav.h"
45
46/** Create navigator menu.
47 *
48 * @param window Navigator window
49 * @param rmenu Place to store pointer to new menu
50 * @return EOK on success or an error code
51 */
52errno_t nav_menu_create(ui_window_t *window, nav_menu_t **rmenu)
53{
54 nav_menu_t *menu;
55 ui_menu_t *mfile;
56 ui_menu_entry_t *mopen;
57 ui_menu_entry_t *mfsep;
58 ui_menu_entry_t *mexit;
59 gfx_rect_t arect;
60 gfx_rect_t rect;
61 errno_t rc;
62
63 menu = calloc(1, sizeof(nav_menu_t));
64 if (menu == NULL)
65 return ENOMEM;
66
67 menu->window = window;
68 menu->ui = ui_window_get_ui(window);
69
70 rc = ui_menu_bar_create(menu->ui, menu->window,
71 &menu->menubar);
72 if (rc != EOK)
73 goto error;
74
75 rc = ui_menu_dd_create(menu->menubar, "~F~ile", NULL, &mfile);
76 if (rc != EOK)
77 goto error;
78
79 rc = ui_menu_entry_create(mfile, "~O~pen", "Enter", &mopen);
80 if (rc != EOK)
81 goto error;
82
83 ui_menu_entry_set_cb(mopen, nav_menu_file_open, (void *) menu);
84
85 rc = ui_menu_entry_sep_create(mfile, &mfsep);
86 if (rc != EOK)
87 goto error;
88
89 rc = ui_menu_entry_create(mfile, "E~x~it", "Ctrl-Q", &mexit);
90 if (rc != EOK)
91 goto error;
92
93 ui_menu_entry_set_cb(mexit, nav_menu_file_exit, (void *) menu);
94
95 ui_window_get_app_rect(menu->window, &arect);
96
97 rect.p0 = arect.p0;
98 rect.p1.x = arect.p1.x;
99 rect.p1.y = arect.p0.y + 1;
100 ui_menu_bar_set_rect(menu->menubar, &rect);
101
102 *rmenu = menu;
103 return EOK;
104error:
105 nav_menu_destroy(menu);
106 return rc;
107}
108
109/** Set navigator menu callbacks.
110 *
111 * @param menu Menu
112 * @param cb Callbacks
113 * @param arg Argument to callback functions
114 */
115void nav_menu_set_cb(nav_menu_t *menu, nav_menu_cb_t *cb, void *arg)
116{
117 menu->cb = cb;
118 menu->cb_arg = arg;
119}
120
121/** Destroy navigator menu.
122 *
123 * @param menu Menu
124 */
125void nav_menu_destroy(nav_menu_t *menu)
126{
127 if (menu->menubar != NULL)
128 ui_menu_bar_destroy(menu->menubar);
129
130 free(menu);
131}
132
133/** Return base UI control for the menu bar.
134 *
135 * @param menu Navigator menu
136 * @return UI control
137 */
138ui_control_t *nav_menu_ctl(nav_menu_t *menu)
139{
140 return ui_menu_bar_ctl(menu->menubar);
141}
142
143/** File / Open menu entry selected.
144 *
145 * @param mentry Menu entry
146 * @param arg Argument (navigator_t *)
147 */
148void nav_menu_file_open(ui_menu_entry_t *mentry, void *arg)
149{
150 nav_menu_t *menu = (nav_menu_t *)arg;
151
152 if (menu->cb != NULL && menu->cb->file_open != NULL)
153 menu->cb->file_open(menu->cb_arg);
154}
155
156/** File / Exit menu entry selected.
157 *
158 * @param mentry Menu entry
159 * @param arg Argument (navigator_t *)
160 */
161void nav_menu_file_exit(ui_menu_entry_t *mentry, void *arg)
162{
163 nav_menu_t *menu = (nav_menu_t *)arg;
164
165 if (menu->cb != NULL && menu->cb->file_exit != NULL)
166 menu->cb->file_exit(menu->cb_arg);
167}
168
169/** @}
170 */
Note: See TracBrowser for help on using the repository browser.