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