source: mainline/uspace/app/nav/nav.c@ 0e80e40

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

Read and display directory contents

  • Property mode set to 100644
File size: 4.9 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 *);
50
51static ui_window_cb_t window_cb = {
52 .close = wnd_close
53};
54
55/** Window close button was clicked.
56 *
57 * @param window Window
58 * @param arg Argument (navigator)
59 */
60static void wnd_close(ui_window_t *window, void *arg)
61{
62 navigator_t *navigator = (navigator_t *) arg;
63
64 ui_quit(navigator->ui);
65}
66
67/** Create navigator.
68 *
69 * @param display_spec Display specification
70 * @param rnavigator Place to store pointer to new navigator
71 * @return EOK on success or ane error code
72 */
73errno_t navigator_create(const char *display_spec,
74 navigator_t **rnavigator)
75{
76 navigator_t *navigator;
77 ui_wnd_params_t params;
78 gfx_rect_t rect;
79 unsigned i;
80 errno_t rc;
81
82 navigator = calloc(1, sizeof(navigator_t));
83 if (navigator == NULL)
84 return ENOMEM;
85
86 rc = ui_create(display_spec, &navigator->ui);
87 if (rc != EOK) {
88 printf("Error creating UI on display %s.\n", display_spec);
89 goto error;
90 }
91
92 ui_wnd_params_init(&params);
93 params.caption = "Navigator";
94 params.style &= ~ui_wds_decorated;
95 params.placement = ui_wnd_place_full_screen;
96
97 rc = ui_window_create(navigator->ui, &params, &navigator->window);
98 if (rc != EOK) {
99 printf("Error creating window.\n");
100 goto error;
101 }
102
103 ui_window_set_cb(navigator->window, &window_cb, (void *) navigator);
104
105 rc = ui_fixed_create(&navigator->fixed);
106 if (rc != EOK) {
107 printf("Error creating fixed layout.\n");
108 goto error;
109 }
110
111 ui_window_add(navigator->window, ui_fixed_ctl(navigator->fixed));
112
113 rc = nav_menu_create(navigator->window, &navigator->menu);
114 if (rc != EOK)
115 goto error;
116
117 rc = ui_fixed_add(navigator->fixed, nav_menu_ctl(navigator->menu));
118 if (rc != EOK) {
119 printf("Error adding control to layout.\n");
120 return rc;
121 }
122
123 for (i = 0; i < 2; i++) {
124 rc = panel_create(navigator->window, &navigator->panel[i]);
125 if (rc != EOK)
126 goto error;
127
128 rect.p0.x = 40 * i;
129 rect.p0.y = 1;
130 rect.p1.x = 40 * (i + 1);
131 rect.p1.y = 24;
132 panel_set_rect(navigator->panel[i], &rect);
133
134 rc = ui_fixed_add(navigator->fixed,
135 panel_ctl(navigator->panel[i]));
136 if (rc != EOK) {
137 printf("Error adding control to layout.\n");
138 goto error;
139 }
140
141 rc = panel_read_dir(navigator->panel[i], ".");
142 if (rc != EOK) {
143 printf("Error reading directory.\n");
144 goto error;
145 }
146 }
147
148 rc = ui_window_paint(navigator->window);
149 if (rc != EOK) {
150 printf("Error painting window.\n");
151 goto error;
152 }
153
154 *rnavigator = navigator;
155 return EOK;
156error:
157 navigator_destroy(navigator);
158 return rc;
159}
160
161void navigator_destroy(navigator_t *navigator)
162{
163 unsigned i;
164
165 for (i = 0; i < 2; i++) {
166 ui_fixed_remove(navigator->fixed,
167 panel_ctl(navigator->panel[i]));
168 panel_destroy(navigator->panel[i]);
169 }
170
171 ui_fixed_remove(navigator->fixed, nav_menu_ctl(navigator->menu));
172
173 if (navigator->menu != NULL)
174 nav_menu_destroy(navigator->menu);
175 if (navigator->window != NULL)
176 ui_window_destroy(navigator->window);
177 if (navigator->ui != NULL)
178 ui_destroy(navigator->ui);
179 free(navigator);
180}
181
182/** Run navigator on the specified display. */
183errno_t navigator_run(const char *display_spec)
184{
185 navigator_t *navigator;
186 errno_t rc;
187
188 rc = navigator_create(display_spec, &navigator);
189 if (rc != EOK)
190 return rc;
191
192 ui_run(navigator->ui);
193
194 navigator_destroy(navigator);
195 return EOK;
196}
197
198/** @}
199 */
Note: See TracBrowser for help on using the repository browser.