source: mainline/uspace/app/nav/menu.c

Last change on this file was accdf882, checked in by Jiri Svoboda <jiri@…>, 5 months ago

File / Edit (Ctrl-E) in Navigator to start editor on current file

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