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 libui
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file File dialog
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <errno.h>
|
---|
37 | #include <mem.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 | #include <ui/entry.h>
|
---|
40 | #include <ui/fixed.h>
|
---|
41 | #include <ui/label.h>
|
---|
42 | #include <ui/filedialog.h>
|
---|
43 | #include <ui/pbutton.h>
|
---|
44 | #include <ui/resource.h>
|
---|
45 | #include <ui/ui.h>
|
---|
46 | #include <ui/window.h>
|
---|
47 | #include "../private/filedialog.h"
|
---|
48 |
|
---|
49 | static void ui_file_dialog_wnd_close(ui_window_t *, void *);
|
---|
50 |
|
---|
51 | ui_window_cb_t ui_file_dialog_wnd_cb = {
|
---|
52 | .close = ui_file_dialog_wnd_close
|
---|
53 | };
|
---|
54 |
|
---|
55 | static void ui_file_dialog_bok_clicked(ui_pbutton_t *, void *);
|
---|
56 | static void ui_file_dialog_bcancel_clicked(ui_pbutton_t *, void *);
|
---|
57 |
|
---|
58 | ui_pbutton_cb_t ui_file_dialog_bok_cb = {
|
---|
59 | .clicked = ui_file_dialog_bok_clicked
|
---|
60 | };
|
---|
61 |
|
---|
62 | ui_pbutton_cb_t ui_file_dialog_bcancel_cb = {
|
---|
63 | .clicked = ui_file_dialog_bcancel_clicked
|
---|
64 | };
|
---|
65 |
|
---|
66 | /** Initialize file dialog parameters structure.
|
---|
67 | *
|
---|
68 | * File dialog parameters structure must always be initialized using
|
---|
69 | * this function first.
|
---|
70 | *
|
---|
71 | * @param params File dialog parameters structure
|
---|
72 | */
|
---|
73 | void ui_file_dialog_params_init(ui_file_dialog_params_t *params)
|
---|
74 | {
|
---|
75 | memset(params, 0, sizeof(ui_file_dialog_params_t));
|
---|
76 | }
|
---|
77 |
|
---|
78 | /** Create new file dialog.
|
---|
79 | *
|
---|
80 | * @param ui User interface
|
---|
81 | * @param params File dialog parameters
|
---|
82 | * @param rdialog Place to store pointer to new dialog
|
---|
83 | * @return EOK on success or an error code
|
---|
84 | */
|
---|
85 | errno_t ui_file_dialog_create(ui_t *ui, ui_file_dialog_params_t *params,
|
---|
86 | ui_file_dialog_t **rdialog)
|
---|
87 | {
|
---|
88 | errno_t rc;
|
---|
89 | ui_file_dialog_t *dialog;
|
---|
90 | ui_window_t *window = NULL;
|
---|
91 | ui_wnd_params_t wparams;
|
---|
92 | ui_fixed_t *fixed = NULL;
|
---|
93 | ui_label_t *label = NULL;
|
---|
94 | ui_entry_t *entry = NULL;
|
---|
95 | ui_pbutton_t *bok = NULL;
|
---|
96 | ui_pbutton_t *bcancel = NULL;
|
---|
97 | gfx_rect_t rect;
|
---|
98 | ui_resource_t *ui_res;
|
---|
99 |
|
---|
100 | dialog = calloc(1, sizeof(ui_file_dialog_t));
|
---|
101 | if (dialog == NULL) {
|
---|
102 | rc = ENOMEM;
|
---|
103 | goto error;
|
---|
104 | }
|
---|
105 |
|
---|
106 | ui_wnd_params_init(&wparams);
|
---|
107 | wparams.caption = params->caption;
|
---|
108 |
|
---|
109 | /* FIXME: Auto layout */
|
---|
110 | if (ui_is_textmode(ui)) {
|
---|
111 | wparams.rect.p0.x = 0;
|
---|
112 | wparams.rect.p0.y = 0;
|
---|
113 | wparams.rect.p1.x = 40;
|
---|
114 | wparams.rect.p1.y = 9;
|
---|
115 | } else {
|
---|
116 | wparams.rect.p0.x = 0;
|
---|
117 | wparams.rect.p0.y = 0;
|
---|
118 | wparams.rect.p1.x = 300;
|
---|
119 | wparams.rect.p1.y = 135;
|
---|
120 | }
|
---|
121 |
|
---|
122 | rc = ui_window_create(ui, &wparams, &window);
|
---|
123 | if (rc != EOK)
|
---|
124 | goto error;
|
---|
125 |
|
---|
126 | ui_window_set_cb(window, &ui_file_dialog_wnd_cb, dialog);
|
---|
127 |
|
---|
128 | ui_res = ui_window_get_res(window);
|
---|
129 |
|
---|
130 | rc = ui_fixed_create(&fixed);
|
---|
131 | if (rc != EOK)
|
---|
132 | goto error;
|
---|
133 |
|
---|
134 | rc = ui_label_create(ui_res, "File Name:", &label);
|
---|
135 | if (rc != EOK)
|
---|
136 | goto error;
|
---|
137 |
|
---|
138 | /* FIXME: Auto layout */
|
---|
139 | if (ui_is_textmode(ui)) {
|
---|
140 | rect.p0.x = 3;
|
---|
141 | rect.p0.y = 2;
|
---|
142 | rect.p1.x = 17;
|
---|
143 | rect.p1.y = 3;
|
---|
144 | } else {
|
---|
145 | rect.p0.x = 10;
|
---|
146 | rect.p0.y = 35;
|
---|
147 | rect.p1.x = 190;
|
---|
148 | rect.p1.y = 50;
|
---|
149 | }
|
---|
150 |
|
---|
151 | ui_label_set_rect(label, &rect);
|
---|
152 |
|
---|
153 | rc = ui_fixed_add(fixed, ui_label_ctl(label));
|
---|
154 | if (rc != EOK)
|
---|
155 | goto error;
|
---|
156 |
|
---|
157 | label = NULL;
|
---|
158 |
|
---|
159 | rc = ui_entry_create(window, "", &entry);
|
---|
160 | if (rc != EOK)
|
---|
161 | goto error;
|
---|
162 |
|
---|
163 | /* FIXME: Auto layout */
|
---|
164 | if (ui_is_textmode(ui)) {
|
---|
165 | rect.p0.x = 3;
|
---|
166 | rect.p0.y = 4;
|
---|
167 | rect.p1.x = 37;
|
---|
168 | rect.p1.y = 5;
|
---|
169 | } else {
|
---|
170 | rect.p0.x = 10;
|
---|
171 | rect.p0.y = 55;
|
---|
172 | rect.p1.x = 290;
|
---|
173 | rect.p1.y = 80;
|
---|
174 | }
|
---|
175 |
|
---|
176 | ui_entry_set_rect(entry, &rect);
|
---|
177 |
|
---|
178 | rc = ui_fixed_add(fixed, ui_entry_ctl(entry));
|
---|
179 | if (rc != EOK)
|
---|
180 | goto error;
|
---|
181 |
|
---|
182 | dialog->ename = entry;
|
---|
183 | entry = NULL;
|
---|
184 |
|
---|
185 | rc = ui_pbutton_create(ui_res, "OK", &bok);
|
---|
186 | if (rc != EOK)
|
---|
187 | goto error;
|
---|
188 |
|
---|
189 | ui_pbutton_set_cb(bok, &ui_file_dialog_bok_cb, dialog);
|
---|
190 |
|
---|
191 | /* FIXME: Auto layout */
|
---|
192 | if (ui_is_textmode(ui)) {
|
---|
193 | rect.p0.x = 10;
|
---|
194 | rect.p0.y = 6;
|
---|
195 | rect.p1.x = 20;
|
---|
196 | rect.p1.y = 7;
|
---|
197 | } else {
|
---|
198 | rect.p0.x = 55;
|
---|
199 | rect.p0.y = 90;
|
---|
200 | rect.p1.x = 145;
|
---|
201 | rect.p1.y = 118;
|
---|
202 | }
|
---|
203 |
|
---|
204 | ui_pbutton_set_rect(bok, &rect);
|
---|
205 |
|
---|
206 | ui_pbutton_set_default(bok, true);
|
---|
207 |
|
---|
208 | rc = ui_fixed_add(fixed, ui_pbutton_ctl(bok));
|
---|
209 | if (rc != EOK)
|
---|
210 | goto error;
|
---|
211 |
|
---|
212 | dialog->bok = bok;
|
---|
213 | bok = NULL;
|
---|
214 |
|
---|
215 | rc = ui_pbutton_create(ui_res, "Cancel", &bcancel);
|
---|
216 | if (rc != EOK)
|
---|
217 | goto error;
|
---|
218 |
|
---|
219 | ui_pbutton_set_cb(bcancel, &ui_file_dialog_bcancel_cb, dialog);
|
---|
220 |
|
---|
221 | /* FIXME: Auto layout */
|
---|
222 | if (ui_is_textmode(ui)) {
|
---|
223 | rect.p0.x = 22;
|
---|
224 | rect.p0.y = 6;
|
---|
225 | rect.p1.x = 32;
|
---|
226 | rect.p1.y = 7;
|
---|
227 | } else {
|
---|
228 | rect.p0.x = 155;
|
---|
229 | rect.p0.y = 90;
|
---|
230 | rect.p1.x = 245;
|
---|
231 | rect.p1.y = 118;
|
---|
232 | }
|
---|
233 |
|
---|
234 | ui_pbutton_set_rect(bcancel, &rect);
|
---|
235 |
|
---|
236 | rc = ui_fixed_add(fixed, ui_pbutton_ctl(bcancel));
|
---|
237 | if (rc != EOK)
|
---|
238 | goto error;
|
---|
239 |
|
---|
240 | dialog->bcancel = bcancel;
|
---|
241 | bcancel = NULL;
|
---|
242 |
|
---|
243 | ui_window_add(window, ui_fixed_ctl(fixed));
|
---|
244 | fixed = NULL;
|
---|
245 |
|
---|
246 | rc = ui_window_paint(window);
|
---|
247 | if (rc != EOK)
|
---|
248 | goto error;
|
---|
249 |
|
---|
250 | dialog->window = window;
|
---|
251 | *rdialog = dialog;
|
---|
252 | return EOK;
|
---|
253 | error:
|
---|
254 | if (entry != NULL)
|
---|
255 | ui_entry_destroy(entry);
|
---|
256 | if (bok != NULL)
|
---|
257 | ui_pbutton_destroy(bok);
|
---|
258 | if (bcancel != NULL)
|
---|
259 | ui_pbutton_destroy(bcancel);
|
---|
260 | if (label != NULL)
|
---|
261 | ui_label_destroy(label);
|
---|
262 | if (fixed != NULL)
|
---|
263 | ui_fixed_destroy(fixed);
|
---|
264 | if (window != NULL)
|
---|
265 | ui_window_destroy(window);
|
---|
266 | if (dialog != NULL)
|
---|
267 | free(dialog);
|
---|
268 | return rc;
|
---|
269 | }
|
---|
270 |
|
---|
271 | /** Destroy file dialog.
|
---|
272 | *
|
---|
273 | * @param dialog File dialog or @c NULL
|
---|
274 | */
|
---|
275 | void ui_file_dialog_destroy(ui_file_dialog_t *dialog)
|
---|
276 | {
|
---|
277 | if (dialog == NULL)
|
---|
278 | return;
|
---|
279 |
|
---|
280 | ui_window_destroy(dialog->window);
|
---|
281 | free(dialog);
|
---|
282 | }
|
---|
283 |
|
---|
284 | /** Set mesage dialog callback.
|
---|
285 | *
|
---|
286 | * @param dialog File dialog
|
---|
287 | * @param cb File dialog callbacks
|
---|
288 | * @param arg Callback argument
|
---|
289 | */
|
---|
290 | void ui_file_dialog_set_cb(ui_file_dialog_t *dialog, ui_file_dialog_cb_t *cb,
|
---|
291 | void *arg)
|
---|
292 | {
|
---|
293 | dialog->cb = cb;
|
---|
294 | dialog->arg = arg;
|
---|
295 | }
|
---|
296 |
|
---|
297 | /** File dialog window close handler.
|
---|
298 | *
|
---|
299 | * @param window Window
|
---|
300 | * @param arg Argument (ui_file_dialog_t *)
|
---|
301 | */
|
---|
302 | static void ui_file_dialog_wnd_close(ui_window_t *window, void *arg)
|
---|
303 | {
|
---|
304 | ui_file_dialog_t *dialog = (ui_file_dialog_t *) arg;
|
---|
305 |
|
---|
306 | if (dialog->cb != NULL && dialog->cb->close != NULL)
|
---|
307 | dialog->cb->close(dialog, dialog->arg);
|
---|
308 | }
|
---|
309 |
|
---|
310 | /** File dialog OK button click handler.
|
---|
311 | *
|
---|
312 | * @param pbutton Push button
|
---|
313 | * @param arg Argument (ui_file_dialog_t *)
|
---|
314 | */
|
---|
315 | static void ui_file_dialog_bok_clicked(ui_pbutton_t *pbutton, void *arg)
|
---|
316 | {
|
---|
317 | ui_file_dialog_t *dialog = (ui_file_dialog_t *) arg;
|
---|
318 | const char *fname;
|
---|
319 |
|
---|
320 | if (dialog->cb != NULL && dialog->cb->bok != NULL) {
|
---|
321 | fname = ui_entry_get_text(dialog->ename);
|
---|
322 | dialog->cb->bok(dialog, dialog->arg, fname);
|
---|
323 | }
|
---|
324 | }
|
---|
325 |
|
---|
326 | /** File dialog cancel button click handler.
|
---|
327 | *
|
---|
328 | * @param pbutton Push button
|
---|
329 | * @param arg Argument (ui_file_dialog_t *)
|
---|
330 | */
|
---|
331 | static void ui_file_dialog_bcancel_clicked(ui_pbutton_t *pbutton, void *arg)
|
---|
332 | {
|
---|
333 | ui_file_dialog_t *dialog = (ui_file_dialog_t *) arg;
|
---|
334 |
|
---|
335 | if (dialog->cb != NULL && dialog->cb->bcancel != NULL)
|
---|
336 | dialog->cb->bcancel(dialog, dialog->arg);
|
---|
337 | }
|
---|
338 |
|
---|
339 | /** @}
|
---|
340 | */
|
---|