| 1 | /*
|
|---|
| 2 | * Copyright (c) 2025 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 | /**
|
|---|
| 33 | * @file Progress dialog
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 | #include <fmgt.h>
|
|---|
| 38 | #include <mem.h>
|
|---|
| 39 | #include <stdlib.h>
|
|---|
| 40 | #include <ui/fixed.h>
|
|---|
| 41 | #include <ui/label.h>
|
|---|
| 42 | #include <ui/pbutton.h>
|
|---|
| 43 | #include <ui/resource.h>
|
|---|
| 44 | #include <ui/ui.h>
|
|---|
| 45 | #include <ui/window.h>
|
|---|
| 46 | #include "progress.h"
|
|---|
| 47 |
|
|---|
| 48 | static void progress_dlg_wnd_close(ui_window_t *, void *);
|
|---|
| 49 | static void progress_dlg_wnd_kbd(ui_window_t *, void *, kbd_event_t *);
|
|---|
| 50 |
|
|---|
| 51 | ui_window_cb_t progress_dlg_wnd_cb = {
|
|---|
| 52 | .close = progress_dlg_wnd_close,
|
|---|
| 53 | .kbd = progress_dlg_wnd_kbd
|
|---|
| 54 | };
|
|---|
| 55 |
|
|---|
| 56 | static void progress_dlg_babort_clicked(ui_pbutton_t *, void *);
|
|---|
| 57 |
|
|---|
| 58 | ui_pbutton_cb_t progress_dlg_babort_cb = {
|
|---|
| 59 | .clicked = progress_dlg_babort_clicked
|
|---|
| 60 | };
|
|---|
| 61 |
|
|---|
| 62 | /** Initialize progress dialog parameters structure.
|
|---|
| 63 | *
|
|---|
| 64 | * Progress dialog parameters structure must always be initialized using
|
|---|
| 65 | * this function first.
|
|---|
| 66 | *
|
|---|
| 67 | * @param params Progress dialog parameters structure
|
|---|
| 68 | */
|
|---|
| 69 | void progress_dlg_params_init(progress_dlg_params_t *params)
|
|---|
| 70 | {
|
|---|
| 71 | memset(params, 0, sizeof(progress_dlg_params_t));
|
|---|
| 72 | params->caption = "";
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | /** Create progress dialog.
|
|---|
| 76 | *
|
|---|
| 77 | * @param ui User interface
|
|---|
| 78 | * @param params Parameters
|
|---|
| 79 | * @param rdialog Place to store pointer to new dialog
|
|---|
| 80 | * @return EOK on success or an error code
|
|---|
| 81 | */
|
|---|
| 82 | errno_t progress_dlg_create(ui_t *ui, progress_dlg_params_t *params,
|
|---|
| 83 | progress_dlg_t **rdialog)
|
|---|
| 84 | {
|
|---|
| 85 | errno_t rc;
|
|---|
| 86 | progress_dlg_t *dialog;
|
|---|
| 87 | ui_window_t *window = NULL;
|
|---|
| 88 | ui_wnd_params_t wparams;
|
|---|
| 89 | ui_fixed_t *fixed = NULL;
|
|---|
| 90 | ui_label_t *label = NULL;
|
|---|
| 91 | ui_pbutton_t *babort = NULL;
|
|---|
| 92 | gfx_rect_t rect;
|
|---|
| 93 | ui_resource_t *ui_res;
|
|---|
| 94 | char *name = NULL;
|
|---|
| 95 |
|
|---|
| 96 | dialog = calloc(1, sizeof(progress_dlg_t));
|
|---|
| 97 | if (dialog == NULL) {
|
|---|
| 98 | rc = ENOMEM;
|
|---|
| 99 | goto error;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | ui_wnd_params_init(&wparams);
|
|---|
| 103 | wparams.caption = params->caption;
|
|---|
| 104 |
|
|---|
| 105 | /* FIXME: Auto layout */
|
|---|
| 106 | if (ui_is_textmode(ui)) {
|
|---|
| 107 | wparams.rect.p0.x = 0;
|
|---|
| 108 | wparams.rect.p0.y = 0;
|
|---|
| 109 | wparams.rect.p1.x = 50;
|
|---|
| 110 | wparams.rect.p1.y = 11;
|
|---|
| 111 | } else {
|
|---|
| 112 | wparams.rect.p0.x = 0;
|
|---|
| 113 | wparams.rect.p0.y = 0;
|
|---|
| 114 | wparams.rect.p1.x = 400;
|
|---|
| 115 | wparams.rect.p1.y = 135;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | rc = ui_window_create(ui, &wparams, &window);
|
|---|
| 119 | if (rc != EOK)
|
|---|
| 120 | goto error;
|
|---|
| 121 |
|
|---|
| 122 | ui_window_set_cb(window, &progress_dlg_wnd_cb, dialog);
|
|---|
| 123 |
|
|---|
| 124 | ui_res = ui_window_get_res(window);
|
|---|
| 125 |
|
|---|
| 126 | rc = ui_fixed_create(&fixed);
|
|---|
| 127 | if (rc != EOK)
|
|---|
| 128 | goto error;
|
|---|
| 129 |
|
|---|
| 130 | rc = ui_label_create(ui_res, "XXX of XXX (XXX%)", &label);
|
|---|
| 131 | if (rc != EOK)
|
|---|
| 132 | goto error;
|
|---|
| 133 |
|
|---|
| 134 | /* FIXME: Auto layout */
|
|---|
| 135 | if (ui_is_textmode(ui)) {
|
|---|
| 136 | rect.p0.x = 3;
|
|---|
| 137 | rect.p0.y = 2;
|
|---|
| 138 | rect.p1.x = 47;
|
|---|
| 139 | rect.p1.y = 3;
|
|---|
| 140 | } else {
|
|---|
| 141 | rect.p0.x = 10;
|
|---|
| 142 | rect.p0.y = 35;
|
|---|
| 143 | rect.p1.x = 390;
|
|---|
| 144 | rect.p1.y = 50;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | ui_label_set_rect(label, &rect);
|
|---|
| 148 | ui_label_set_halign(label, gfx_halign_center);
|
|---|
| 149 |
|
|---|
| 150 | rc = ui_fixed_add(fixed, ui_label_ctl(label));
|
|---|
| 151 | if (rc != EOK)
|
|---|
| 152 | goto error;
|
|---|
| 153 |
|
|---|
| 154 | dialog->lcurf_prog = label;
|
|---|
| 155 | label = NULL;
|
|---|
| 156 |
|
|---|
| 157 | rc = ui_pbutton_create(ui_res, "Abort", &babort);
|
|---|
| 158 | if (rc != EOK)
|
|---|
| 159 | goto error;
|
|---|
| 160 |
|
|---|
| 161 | ui_pbutton_set_cb(babort, &progress_dlg_babort_cb, dialog);
|
|---|
| 162 |
|
|---|
| 163 | /* FIXME: Auto layout */
|
|---|
| 164 | if (ui_is_textmode(ui)) {
|
|---|
| 165 | rect.p0.x = 20;
|
|---|
| 166 | rect.p0.y = 8;
|
|---|
| 167 | rect.p1.x = 30;
|
|---|
| 168 | rect.p1.y = 9;
|
|---|
| 169 | } else {
|
|---|
| 170 | rect.p0.x = 205;
|
|---|
| 171 | rect.p0.y = 90;
|
|---|
| 172 | rect.p1.x = 295;
|
|---|
| 173 | rect.p1.y = 118;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | ui_pbutton_set_rect(babort, &rect);
|
|---|
| 177 |
|
|---|
| 178 | ui_pbutton_set_default(babort, true);
|
|---|
| 179 |
|
|---|
| 180 | rc = ui_fixed_add(fixed, ui_pbutton_ctl(babort));
|
|---|
| 181 | if (rc != EOK)
|
|---|
| 182 | goto error;
|
|---|
| 183 |
|
|---|
| 184 | dialog->babort = babort;
|
|---|
| 185 | babort = NULL;
|
|---|
| 186 |
|
|---|
| 187 | ui_window_add(window, ui_fixed_ctl(fixed));
|
|---|
| 188 | fixed = NULL;
|
|---|
| 189 |
|
|---|
| 190 | rc = ui_window_paint(window);
|
|---|
| 191 | if (rc != EOK)
|
|---|
| 192 | goto error;
|
|---|
| 193 |
|
|---|
| 194 | dialog->window = window;
|
|---|
| 195 | *rdialog = dialog;
|
|---|
| 196 | return EOK;
|
|---|
| 197 | error:
|
|---|
| 198 | if (name != NULL)
|
|---|
| 199 | free(name);
|
|---|
| 200 | if (babort != NULL)
|
|---|
| 201 | ui_pbutton_destroy(babort);
|
|---|
| 202 | if (label != NULL)
|
|---|
| 203 | ui_label_destroy(label);
|
|---|
| 204 | if (fixed != NULL)
|
|---|
| 205 | ui_fixed_destroy(fixed);
|
|---|
| 206 | if (window != NULL)
|
|---|
| 207 | ui_window_destroy(window);
|
|---|
| 208 | if (dialog != NULL)
|
|---|
| 209 | free(dialog);
|
|---|
| 210 | return rc;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | /** Destroy progress dialog.
|
|---|
| 214 | *
|
|---|
| 215 | * @param dialog Progress dialog or @c NULL
|
|---|
| 216 | */
|
|---|
| 217 | void progress_dlg_destroy(progress_dlg_t *dialog)
|
|---|
| 218 | {
|
|---|
| 219 | if (dialog == NULL)
|
|---|
| 220 | return;
|
|---|
| 221 |
|
|---|
| 222 | ui_window_destroy(dialog->window);
|
|---|
| 223 | free(dialog);
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | /** Set mesage dialog callback.
|
|---|
| 227 | *
|
|---|
| 228 | * @param dialog Progress dialog
|
|---|
| 229 | * @param cb Progress dialog callbacks
|
|---|
| 230 | * @param arg Callback argument
|
|---|
| 231 | */
|
|---|
| 232 | void progress_dlg_set_cb(progress_dlg_t *dialog, progress_dlg_cb_t *cb,
|
|---|
| 233 | void *arg)
|
|---|
| 234 | {
|
|---|
| 235 | dialog->cb = cb;
|
|---|
| 236 | dialog->arg = arg;
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | /** Set current file progress text.
|
|---|
| 240 | *
|
|---|
| 241 | * @param dialog Progress dialog
|
|---|
| 242 | * @param text New text for current file progress
|
|---|
| 243 | *
|
|---|
| 244 | * @return EOK on success or an error code
|
|---|
| 245 | */
|
|---|
| 246 | errno_t progress_dlg_set_curf_prog(progress_dlg_t *dialog, const char *text)
|
|---|
| 247 | {
|
|---|
| 248 | errno_t rc;
|
|---|
| 249 |
|
|---|
| 250 | rc = ui_label_set_text(dialog->lcurf_prog, text);
|
|---|
| 251 | if (rc != EOK)
|
|---|
| 252 | return rc;
|
|---|
| 253 |
|
|---|
| 254 | return ui_label_paint(dialog->lcurf_prog);
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | /** Progress dialog window close handler.
|
|---|
| 258 | *
|
|---|
| 259 | * @param window Window
|
|---|
| 260 | * @param arg Argument (progress_dlg_t *)
|
|---|
| 261 | */
|
|---|
| 262 | static void progress_dlg_wnd_close(ui_window_t *window, void *arg)
|
|---|
| 263 | {
|
|---|
| 264 | progress_dlg_t *dialog = (progress_dlg_t *) arg;
|
|---|
| 265 |
|
|---|
| 266 | (void)window;
|
|---|
| 267 | if (dialog->cb != NULL && dialog->cb->close != NULL) {
|
|---|
| 268 | dialog->cb->close(dialog, dialog->arg);
|
|---|
| 269 | }
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | /** Progress dialog window keyboard event handler.
|
|---|
| 273 | *
|
|---|
| 274 | * @param window Window
|
|---|
| 275 | * @param arg Argument (progress_dlg_t *)
|
|---|
| 276 | * @param event Keyboard event
|
|---|
| 277 | */
|
|---|
| 278 | static void progress_dlg_wnd_kbd(ui_window_t *window, void *arg,
|
|---|
| 279 | kbd_event_t *event)
|
|---|
| 280 | {
|
|---|
| 281 | progress_dlg_t *dialog = (progress_dlg_t *) arg;
|
|---|
| 282 |
|
|---|
| 283 | if (event->type == KEY_PRESS &&
|
|---|
| 284 | (event->mods & (KM_CTRL | KM_SHIFT | KM_ALT)) == 0) {
|
|---|
| 285 | if (event->key == KC_ENTER || event->key == KC_ESCAPE) {
|
|---|
| 286 | /* Abort */
|
|---|
| 287 | if (dialog->cb != NULL && dialog->cb->babort != NULL) {
|
|---|
| 288 | dialog->cb->babort(dialog, dialog->arg);
|
|---|
| 289 | return;
|
|---|
| 290 | }
|
|---|
| 291 | }
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | ui_window_def_kbd(window, event);
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | /** Progress dialog Abort button click handler.
|
|---|
| 298 | *
|
|---|
| 299 | * @param pbutton Push button
|
|---|
| 300 | * @param arg Argument (progress_dlg_t *)
|
|---|
| 301 | */
|
|---|
| 302 | static void progress_dlg_babort_clicked(ui_pbutton_t *pbutton, void *arg)
|
|---|
| 303 | {
|
|---|
| 304 | progress_dlg_t *dialog = (progress_dlg_t *) arg;
|
|---|
| 305 |
|
|---|
| 306 | if (dialog->cb != NULL && dialog->cb->babort != NULL) {
|
|---|
| 307 | dialog->cb->babort(dialog, dialog->arg);
|
|---|
| 308 | }
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 | /** @}
|
|---|
| 312 | */
|
|---|