Array of strings sys_dirs must be NULL-terminated
[helenos.git] / uspace / app / nav / menu.c
blob8248530670c0d7e745a82bc6b611704dd2fca4a7
1 /*
2 * Copyright (c) 2023 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 /** @addtogroup nav
30 * @{
32 /** @file Navigator.
34 * HelenOS file manager.
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"
46 /** Create navigator menu.
48 * @param window Navigator window
49 * @param rmenu Place to store pointer to new menu
50 * @return EOK on success or an error code
52 errno_t nav_menu_create(ui_window_t *window, nav_menu_t **rmenu)
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;
63 menu = calloc(1, sizeof(nav_menu_t));
64 if (menu == NULL)
65 return ENOMEM;
67 menu->window = window;
68 menu->ui = ui_window_get_ui(window);
70 rc = ui_menu_bar_create(menu->ui, menu->window,
71 &menu->menubar);
72 if (rc != EOK)
73 goto error;
75 rc = ui_menu_dd_create(menu->menubar, "~F~ile", NULL, &mfile);
76 if (rc != EOK)
77 goto error;
79 rc = ui_menu_entry_create(mfile, "~O~pen", "Enter", &mopen);
80 if (rc != EOK)
81 goto error;
83 ui_menu_entry_set_cb(mopen, nav_menu_file_open, (void *) menu);
85 rc = ui_menu_entry_sep_create(mfile, &mfsep);
86 if (rc != EOK)
87 goto error;
89 rc = ui_menu_entry_create(mfile, "E~x~it", "Ctrl-Q", &mexit);
90 if (rc != EOK)
91 goto error;
93 ui_menu_entry_set_cb(mexit, nav_menu_file_exit, (void *) menu);
95 ui_window_get_app_rect(menu->window, &arect);
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);
102 *rmenu = menu;
103 return EOK;
104 error:
105 nav_menu_destroy(menu);
106 return rc;
109 /** Set navigator menu callbacks.
111 * @param menu Menu
112 * @param cb Callbacks
113 * @param arg Argument to callback functions
115 void nav_menu_set_cb(nav_menu_t *menu, nav_menu_cb_t *cb, void *arg)
117 menu->cb = cb;
118 menu->cb_arg = arg;
121 /** Destroy navigator menu.
123 * @param menu Menu
125 void nav_menu_destroy(nav_menu_t *menu)
127 if (menu->menubar != NULL)
128 ui_menu_bar_destroy(menu->menubar);
130 free(menu);
133 /** Return base UI control for the menu bar.
135 * @param menu Navigator menu
136 * @return UI control
138 ui_control_t *nav_menu_ctl(nav_menu_t *menu)
140 return ui_menu_bar_ctl(menu->menubar);
143 /** File / Open menu entry selected.
145 * @param mentry Menu entry
146 * @param arg Argument (navigator_t *)
148 void nav_menu_file_open(ui_menu_entry_t *mentry, void *arg)
150 nav_menu_t *menu = (nav_menu_t *)arg;
152 if (menu->cb != NULL && menu->cb->file_open != NULL)
153 menu->cb->file_open(menu->cb_arg);
156 /** File / Exit menu entry selected.
158 * @param mentry Menu entry
159 * @param arg Argument (navigator_t *)
161 void nav_menu_file_exit(ui_menu_entry_t *mentry, void *arg)
163 nav_menu_t *menu = (nav_menu_t *)arg;
165 if (menu->cb != NULL && menu->cb->file_exit != NULL)
166 menu->cb->file_exit(menu->cb_arg);
169 /** @}