source: mainline/uspace/app/nav/nav.c@ 39ab17c

serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 39ab17c was 39ab17c, checked in by jxsvoboda <5887334+jxsvoboda@…>, 4 years ago

Activating panel using the mouse

  • Property mode set to 100644
File size: 7.8 KB
Line 
1/*
2 * Copyright (c) 2021 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 <gfx/coord.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <str.h>
41#include <ui/fixed.h>
42#include <ui/resource.h>
43#include <ui/ui.h>
44#include <ui/window.h>
45#include "menu.h"
46#include "nav.h"
47#include "panel.h"
48
49static void wnd_close(ui_window_t *, void *);
50static void wnd_kbd(ui_window_t *, void *, kbd_event_t *);
51
52static ui_window_cb_t window_cb = {
53 .close = wnd_close,
54 .kbd = wnd_kbd
55};
56
57static void navigator_file_open(void *);
58static void navigator_file_exit(void *);
59
60static nav_menu_cb_t navigator_menu_cb = {
61 .file_open = navigator_file_open,
62 .file_exit = navigator_file_exit
63};
64
65static void navigator_panel_activate_req(void *, panel_t *);
66
67static panel_cb_t navigator_panel_cb = {
68 .activate_req = navigator_panel_activate_req
69};
70
71/** Window close button was clicked.
72 *
73 * @param window Window
74 * @param arg Argument (navigator)
75 */
76static void wnd_close(ui_window_t *window, void *arg)
77{
78 navigator_t *navigator = (navigator_t *) arg;
79
80 ui_quit(navigator->ui);
81}
82
83/** Window keyboard event handler.
84 *
85 * @param window Window
86 * @param arg Argument (navigator)
87 * @param event Keyboard event
88 */
89static void wnd_kbd(ui_window_t *window, void *arg, kbd_event_t *event)
90{
91 navigator_t *navigator = (navigator_t *) arg;
92
93 if (event->type == KEY_PRESS &&
94 ((event->mods & KM_ALT) == 0) &&
95 ((event->mods & KM_SHIFT) == 0) &&
96 (event->mods & KM_CTRL) != 0) {
97 switch (event->key) {
98 case KC_Q:
99 ui_quit(navigator->ui);
100 break;
101 default:
102 break;
103 }
104 }
105
106 if (event->type == KEY_PRESS &&
107 ((event->mods & (KM_CTRL | KM_ALT | KM_SHIFT)) == 0)) {
108 switch (event->key) {
109 case KC_TAB:
110 navigator_switch_panel(navigator);
111 break;
112 default:
113 break;
114 }
115 }
116
117 ui_window_def_kbd(window, event);
118}
119
120/** Create navigator.
121 *
122 * @param display_spec Display specification
123 * @param rnavigator Place to store pointer to new navigator
124 * @return EOK on success or ane error code
125 */
126errno_t navigator_create(const char *display_spec,
127 navigator_t **rnavigator)
128{
129 navigator_t *navigator;
130 ui_wnd_params_t params;
131 gfx_rect_t rect;
132 unsigned i;
133 errno_t rc;
134
135 navigator = calloc(1, sizeof(navigator_t));
136 if (navigator == NULL)
137 return ENOMEM;
138
139 rc = ui_create(display_spec, &navigator->ui);
140 if (rc != EOK) {
141 printf("Error creating UI on display %s.\n", display_spec);
142 goto error;
143 }
144
145 ui_wnd_params_init(&params);
146 params.caption = "Navigator";
147 params.style &= ~ui_wds_decorated;
148 params.placement = ui_wnd_place_full_screen;
149
150 rc = ui_window_create(navigator->ui, &params, &navigator->window);
151 if (rc != EOK) {
152 printf("Error creating window.\n");
153 goto error;
154 }
155
156 ui_window_set_cb(navigator->window, &window_cb, (void *) navigator);
157
158 rc = ui_fixed_create(&navigator->fixed);
159 if (rc != EOK) {
160 printf("Error creating fixed layout.\n");
161 goto error;
162 }
163
164 ui_window_add(navigator->window, ui_fixed_ctl(navigator->fixed));
165
166 rc = nav_menu_create(navigator->window, &navigator->menu);
167 if (rc != EOK)
168 goto error;
169
170 nav_menu_set_cb(navigator->menu, &navigator_menu_cb,
171 (void *)navigator);
172
173 rc = ui_fixed_add(navigator->fixed, nav_menu_ctl(navigator->menu));
174 if (rc != EOK) {
175 printf("Error adding control to layout.\n");
176 return rc;
177 }
178
179 for (i = 0; i < 2; i++) {
180 rc = panel_create(navigator->window, i == 0,
181 &navigator->panel[i]);
182 if (rc != EOK)
183 goto error;
184
185 rect.p0.x = 40 * i;
186 rect.p0.y = 1;
187 rect.p1.x = 40 * (i + 1);
188 rect.p1.y = 24;
189 panel_set_rect(navigator->panel[i], &rect);
190
191 panel_set_cb(navigator->panel[i], &navigator_panel_cb,
192 navigator);
193
194 rc = ui_fixed_add(navigator->fixed,
195 panel_ctl(navigator->panel[i]));
196 if (rc != EOK) {
197 printf("Error adding control to layout.\n");
198 goto error;
199 }
200
201 rc = panel_read_dir(navigator->panel[i], ".");
202 if (rc != EOK) {
203 printf("Error reading directory.\n");
204 goto error;
205 }
206 }
207
208 rc = ui_window_paint(navigator->window);
209 if (rc != EOK) {
210 printf("Error painting window.\n");
211 goto error;
212 }
213
214 *rnavigator = navigator;
215 return EOK;
216error:
217 navigator_destroy(navigator);
218 return rc;
219}
220
221void navigator_destroy(navigator_t *navigator)
222{
223 unsigned i;
224
225 for (i = 0; i < 2; i++) {
226 ui_fixed_remove(navigator->fixed,
227 panel_ctl(navigator->panel[i]));
228 panel_destroy(navigator->panel[i]);
229 }
230
231 ui_fixed_remove(navigator->fixed, nav_menu_ctl(navigator->menu));
232
233 if (navigator->menu != NULL)
234 nav_menu_destroy(navigator->menu);
235 if (navigator->window != NULL)
236 ui_window_destroy(navigator->window);
237 if (navigator->ui != NULL)
238 ui_destroy(navigator->ui);
239 free(navigator);
240}
241
242/** Run navigator on the specified display. */
243errno_t navigator_run(const char *display_spec)
244{
245 navigator_t *navigator;
246 errno_t rc;
247
248 rc = navigator_create(display_spec, &navigator);
249 if (rc != EOK)
250 return rc;
251
252 ui_run(navigator->ui);
253
254 navigator_destroy(navigator);
255 return EOK;
256}
257
258/** Get the currently active navigator panel.
259 *
260 * @param navigator Navigator
261 * @return Currently active panel
262 */
263panel_t *navigator_get_active_panel(navigator_t *navigator)
264{
265 int i;
266
267 for (i = 0; i < navigator_panels; i++) {
268 if (panel_is_active(navigator->panel[i]))
269 return navigator->panel[i];
270 }
271
272 /* This should not happen */
273 assert(false);
274 return NULL;
275}
276
277/** Switch to another navigator panel.
278 *
279 * Changes the currently active navigator panel to the next panel.
280 *
281 * @param navigator Navigator
282 */
283void navigator_switch_panel(navigator_t *navigator)
284{
285 errno_t rc;
286
287 if (panel_is_active(navigator->panel[0])) {
288 rc = panel_activate(navigator->panel[1]);
289 if (rc != EOK)
290 return;
291 panel_deactivate(navigator->panel[0]);
292 } else {
293 rc = panel_activate(navigator->panel[0]);
294 if (rc != EOK)
295 return;
296 panel_deactivate(navigator->panel[1]);
297 }
298}
299
300/** File / Open menu entry selected */
301static void navigator_file_open(void *arg)
302{
303 navigator_t *navigator = (navigator_t *)arg;
304 panel_t *panel;
305
306 panel = navigator_get_active_panel(navigator);
307 panel_open(panel, panel->cursor);
308}
309
310/** File / Exit menu entry selected */
311static void navigator_file_exit(void *arg)
312{
313 navigator_t *navigator = (navigator_t *)arg;
314
315 ui_quit(navigator->ui);
316}
317
318/** Panel callback requesting panel activation.
319 *
320 * @param arg Argument (navigator_t *)
321 * @param panel Panel
322 */
323void navigator_panel_activate_req(void *arg, panel_t *panel)
324{
325 navigator_t *navigator = (navigator_t *)arg;
326
327 if (!panel_is_active(panel))
328 navigator_switch_panel(navigator);
329}
330
331/** @}
332 */
Note: See TracBrowser for help on using the repository browser.