source: mainline/uspace/app/nav/menu.c@ 43ef144

Last change on this file since 43ef144 was f9c4c433, checked in by Jiri Svoboda <jiri@…>, 3 months ago

Create new file in Navigator (WIP)

  • Property mode set to 100644
File size: 5.2 KB
Line 
1/*
2 * Copyright (c) 2025 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 *mnew;
57 ui_menu_entry_t *mopen;
58 ui_menu_entry_t *medit;
59 ui_menu_entry_t *mfsep;
60 ui_menu_entry_t *mexit;
61 gfx_rect_t arect;
62 gfx_rect_t rect;
63 errno_t rc;
64
65 menu = calloc(1, sizeof(nav_menu_t));
66 if (menu == NULL)
67 return ENOMEM;
68
69 menu->window = window;
70 menu->ui = ui_window_get_ui(window);
71
72 rc = ui_menu_bar_create(menu->ui, menu->window,
73 &menu->menubar);
74 if (rc != EOK)
75 goto error;
76
77 rc = ui_menu_dd_create(menu->menubar, "~F~ile", NULL, &mfile);
78 if (rc != EOK)
79 goto error;
80
81 rc = ui_menu_entry_create(mfile, "~N~ew File", "Ctrl-M", &mnew);
82 if (rc != EOK)
83 goto error;
84
85 ui_menu_entry_set_cb(mnew, nav_menu_file_new_file, (void *) menu);
86
87 rc = ui_menu_entry_create(mfile, "~O~pen", "Enter", &mopen);
88 if (rc != EOK)
89 goto error;
90
91 ui_menu_entry_set_cb(mopen, nav_menu_file_open, (void *) menu);
92
93 rc = ui_menu_entry_create(mfile, "~E~dit", "Ctrl-E", &medit);
94 if (rc != EOK)
95 goto error;
96
97 ui_menu_entry_set_cb(medit, nav_menu_file_edit, (void *) menu);
98
99 rc = ui_menu_entry_sep_create(mfile, &mfsep);
100 if (rc != EOK)
101 goto error;
102
103 rc = ui_menu_entry_create(mfile, "E~x~it", "Ctrl-Q", &mexit);
104 if (rc != EOK)
105 goto error;
106
107 ui_menu_entry_set_cb(mexit, nav_menu_file_exit, (void *) menu);
108
109 ui_window_get_app_rect(menu->window, &arect);
110
111 rect.p0 = arect.p0;
112 rect.p1.x = arect.p1.x;
113 rect.p1.y = arect.p0.y + 1;
114 ui_menu_bar_set_rect(menu->menubar, &rect);
115
116 *rmenu = menu;
117 return EOK;
118error:
119 nav_menu_destroy(menu);
120 return rc;
121}
122
123/** Set navigator menu callbacks.
124 *
125 * @param menu Menu
126 * @param cb Callbacks
127 * @param arg Argument to callback functions
128 */
129void nav_menu_set_cb(nav_menu_t *menu, nav_menu_cb_t *cb, void *arg)
130{
131 menu->cb = cb;
132 menu->cb_arg = arg;
133}
134
135/** Destroy navigator menu.
136 *
137 * @param menu Menu
138 */
139void nav_menu_destroy(nav_menu_t *menu)
140{
141 if (menu->menubar != NULL)
142 ui_menu_bar_destroy(menu->menubar);
143
144 free(menu);
145}
146
147/** Return base UI control for the menu bar.
148 *
149 * @param menu Navigator menu
150 * @return UI control
151 */
152ui_control_t *nav_menu_ctl(nav_menu_t *menu)
153{
154 return ui_menu_bar_ctl(menu->menubar);
155}
156
157/** File / New File menu entry selected.
158 *
159 * @param mentry Menu entry
160 * @param arg Argument (navigator_t *)
161 */
162void nav_menu_file_new_file(ui_menu_entry_t *mentry, void *arg)
163{
164 nav_menu_t *menu = (nav_menu_t *)arg;
165
166 if (menu->cb != NULL && menu->cb->file_new_file != NULL)
167 menu->cb->file_new_file(menu->cb_arg);
168}
169
170/** File / Open menu entry selected.
171 *
172 * @param mentry Menu entry
173 * @param arg Argument (navigator_t *)
174 */
175void nav_menu_file_open(ui_menu_entry_t *mentry, void *arg)
176{
177 nav_menu_t *menu = (nav_menu_t *)arg;
178
179 if (menu->cb != NULL && menu->cb->file_open != NULL)
180 menu->cb->file_open(menu->cb_arg);
181}
182
183/** File / Edit menu entry selected.
184 *
185 * @param mentry Menu entry
186 * @param arg Argument (navigator_t *)
187 */
188void nav_menu_file_edit(ui_menu_entry_t *mentry, void *arg)
189{
190 nav_menu_t *menu = (nav_menu_t *)arg;
191
192 if (menu->cb != NULL && menu->cb->file_edit != NULL)
193 menu->cb->file_edit(menu->cb_arg);
194}
195
196/** File / Exit menu entry selected.
197 *
198 * @param mentry Menu entry
199 * @param arg Argument (navigator_t *)
200 */
201void nav_menu_file_exit(ui_menu_entry_t *mentry, void *arg)
202{
203 nav_menu_t *menu = (nav_menu_t *)arg;
204
205 if (menu->cb != NULL && menu->cb->file_exit != NULL)
206 menu->cb->file_exit(menu->cb_arg);
207}
208
209/** @}
210 */
Note: See TracBrowser for help on using the repository browser.