[5e109e1] | 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;
|
---|
[45004f3] | 114 | wparams.rect.p1.y = 9;
|
---|
[5e109e1] | 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;
|
---|
[45004f3] | 141 | rect.p0.y = 2;
|
---|
[5e109e1] | 142 | rect.p1.x = 17;
|
---|
[45004f3] | 143 | rect.p1.y = 3;
|
---|
[5e109e1] | 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;
|
---|
[45004f3] | 166 | rect.p0.y = 4;
|
---|
[5e109e1] | 167 | rect.p1.x = 37;
|
---|
[45004f3] | 168 | rect.p1.y = 5;
|
---|
[5e109e1] | 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 |
|
---|
[a7a8f599] | 182 | ui_entry_activate(entry);
|
---|
| 183 |
|
---|
[5e109e1] | 184 | dialog->ename = entry;
|
---|
| 185 | entry = NULL;
|
---|
| 186 |
|
---|
| 187 | rc = ui_pbutton_create(ui_res, "OK", &bok);
|
---|
| 188 | if (rc != EOK)
|
---|
| 189 | goto error;
|
---|
| 190 |
|
---|
| 191 | ui_pbutton_set_cb(bok, &ui_file_dialog_bok_cb, dialog);
|
---|
| 192 |
|
---|
| 193 | /* FIXME: Auto layout */
|
---|
| 194 | if (ui_is_textmode(ui)) {
|
---|
| 195 | rect.p0.x = 10;
|
---|
[45004f3] | 196 | rect.p0.y = 6;
|
---|
[5e109e1] | 197 | rect.p1.x = 20;
|
---|
[45004f3] | 198 | rect.p1.y = 7;
|
---|
[5e109e1] | 199 | } else {
|
---|
| 200 | rect.p0.x = 55;
|
---|
| 201 | rect.p0.y = 90;
|
---|
| 202 | rect.p1.x = 145;
|
---|
| 203 | rect.p1.y = 118;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | ui_pbutton_set_rect(bok, &rect);
|
---|
| 207 |
|
---|
| 208 | ui_pbutton_set_default(bok, true);
|
---|
| 209 |
|
---|
| 210 | rc = ui_fixed_add(fixed, ui_pbutton_ctl(bok));
|
---|
| 211 | if (rc != EOK)
|
---|
| 212 | goto error;
|
---|
| 213 |
|
---|
| 214 | dialog->bok = bok;
|
---|
| 215 | bok = NULL;
|
---|
| 216 |
|
---|
| 217 | rc = ui_pbutton_create(ui_res, "Cancel", &bcancel);
|
---|
| 218 | if (rc != EOK)
|
---|
| 219 | goto error;
|
---|
| 220 |
|
---|
| 221 | ui_pbutton_set_cb(bcancel, &ui_file_dialog_bcancel_cb, dialog);
|
---|
| 222 |
|
---|
| 223 | /* FIXME: Auto layout */
|
---|
| 224 | if (ui_is_textmode(ui)) {
|
---|
| 225 | rect.p0.x = 22;
|
---|
[45004f3] | 226 | rect.p0.y = 6;
|
---|
[5e109e1] | 227 | rect.p1.x = 32;
|
---|
[45004f3] | 228 | rect.p1.y = 7;
|
---|
[5e109e1] | 229 | } else {
|
---|
| 230 | rect.p0.x = 155;
|
---|
| 231 | rect.p0.y = 90;
|
---|
| 232 | rect.p1.x = 245;
|
---|
| 233 | rect.p1.y = 118;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | ui_pbutton_set_rect(bcancel, &rect);
|
---|
| 237 |
|
---|
| 238 | rc = ui_fixed_add(fixed, ui_pbutton_ctl(bcancel));
|
---|
| 239 | if (rc != EOK)
|
---|
| 240 | goto error;
|
---|
| 241 |
|
---|
| 242 | dialog->bcancel = bcancel;
|
---|
| 243 | bcancel = NULL;
|
---|
| 244 |
|
---|
| 245 | ui_window_add(window, ui_fixed_ctl(fixed));
|
---|
| 246 | fixed = NULL;
|
---|
| 247 |
|
---|
| 248 | rc = ui_window_paint(window);
|
---|
| 249 | if (rc != EOK)
|
---|
| 250 | goto error;
|
---|
| 251 |
|
---|
| 252 | dialog->window = window;
|
---|
| 253 | *rdialog = dialog;
|
---|
| 254 | return EOK;
|
---|
| 255 | error:
|
---|
| 256 | if (entry != NULL)
|
---|
| 257 | ui_entry_destroy(entry);
|
---|
| 258 | if (bok != NULL)
|
---|
| 259 | ui_pbutton_destroy(bok);
|
---|
| 260 | if (bcancel != NULL)
|
---|
| 261 | ui_pbutton_destroy(bcancel);
|
---|
| 262 | if (label != NULL)
|
---|
| 263 | ui_label_destroy(label);
|
---|
| 264 | if (fixed != NULL)
|
---|
| 265 | ui_fixed_destroy(fixed);
|
---|
| 266 | if (window != NULL)
|
---|
| 267 | ui_window_destroy(window);
|
---|
| 268 | if (dialog != NULL)
|
---|
| 269 | free(dialog);
|
---|
| 270 | return rc;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | /** Destroy file dialog.
|
---|
| 274 | *
|
---|
| 275 | * @param dialog File dialog or @c NULL
|
---|
| 276 | */
|
---|
| 277 | void ui_file_dialog_destroy(ui_file_dialog_t *dialog)
|
---|
| 278 | {
|
---|
| 279 | if (dialog == NULL)
|
---|
| 280 | return;
|
---|
| 281 |
|
---|
| 282 | ui_window_destroy(dialog->window);
|
---|
| 283 | free(dialog);
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | /** Set mesage dialog callback.
|
---|
| 287 | *
|
---|
| 288 | * @param dialog File dialog
|
---|
| 289 | * @param cb File dialog callbacks
|
---|
| 290 | * @param arg Callback argument
|
---|
| 291 | */
|
---|
| 292 | void ui_file_dialog_set_cb(ui_file_dialog_t *dialog, ui_file_dialog_cb_t *cb,
|
---|
| 293 | void *arg)
|
---|
| 294 | {
|
---|
| 295 | dialog->cb = cb;
|
---|
| 296 | dialog->arg = arg;
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | /** File dialog window close handler.
|
---|
| 300 | *
|
---|
| 301 | * @param window Window
|
---|
| 302 | * @param arg Argument (ui_file_dialog_t *)
|
---|
| 303 | */
|
---|
| 304 | static void ui_file_dialog_wnd_close(ui_window_t *window, void *arg)
|
---|
| 305 | {
|
---|
| 306 | ui_file_dialog_t *dialog = (ui_file_dialog_t *) arg;
|
---|
| 307 |
|
---|
| 308 | if (dialog->cb != NULL && dialog->cb->close != NULL)
|
---|
| 309 | dialog->cb->close(dialog, dialog->arg);
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | /** File dialog OK button click handler.
|
---|
| 313 | *
|
---|
| 314 | * @param pbutton Push button
|
---|
| 315 | * @param arg Argument (ui_file_dialog_t *)
|
---|
| 316 | */
|
---|
| 317 | static void ui_file_dialog_bok_clicked(ui_pbutton_t *pbutton, void *arg)
|
---|
| 318 | {
|
---|
| 319 | ui_file_dialog_t *dialog = (ui_file_dialog_t *) arg;
|
---|
| 320 | const char *fname;
|
---|
| 321 |
|
---|
| 322 | if (dialog->cb != NULL && dialog->cb->bok != NULL) {
|
---|
| 323 | fname = ui_entry_get_text(dialog->ename);
|
---|
| 324 | dialog->cb->bok(dialog, dialog->arg, fname);
|
---|
| 325 | }
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | /** File dialog cancel button click handler.
|
---|
| 329 | *
|
---|
| 330 | * @param pbutton Push button
|
---|
| 331 | * @param arg Argument (ui_file_dialog_t *)
|
---|
| 332 | */
|
---|
| 333 | static void ui_file_dialog_bcancel_clicked(ui_pbutton_t *pbutton, void *arg)
|
---|
| 334 | {
|
---|
| 335 | ui_file_dialog_t *dialog = (ui_file_dialog_t *) arg;
|
---|
| 336 |
|
---|
| 337 | if (dialog->cb != NULL && dialog->cb->bcancel != NULL)
|
---|
| 338 | dialog->cb->bcancel(dialog, dialog->arg);
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | /** @}
|
---|
| 342 | */
|
---|