[de227aba] | 1 | /*
|
---|
[9a07ee3] | 2 | * Copyright (c) 2024 Jiri Svoboda
|
---|
[de227aba] | 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 Message dialog
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <errno.h>
|
---|
| 37 | #include <mem.h>
|
---|
| 38 | #include <stdlib.h>
|
---|
| 39 | #include <ui/fixed.h>
|
---|
| 40 | #include <ui/label.h>
|
---|
| 41 | #include <ui/msgdialog.h>
|
---|
| 42 | #include <ui/pbutton.h>
|
---|
| 43 | #include <ui/resource.h>
|
---|
[252d03c] | 44 | #include <ui/ui.h>
|
---|
[de227aba] | 45 | #include <ui/window.h>
|
---|
| 46 | #include "../private/msgdialog.h"
|
---|
| 47 |
|
---|
| 48 | static void ui_msg_dialog_wnd_close(ui_window_t *, void *);
|
---|
[12dd36c] | 49 | static void ui_msg_dialog_wnd_kbd(ui_window_t *, void *, kbd_event_t *);
|
---|
[de227aba] | 50 |
|
---|
| 51 | ui_window_cb_t ui_msg_dialog_wnd_cb = {
|
---|
[12dd36c] | 52 | .close = ui_msg_dialog_wnd_close,
|
---|
| 53 | .kbd = ui_msg_dialog_wnd_kbd
|
---|
[de227aba] | 54 | };
|
---|
| 55 |
|
---|
| 56 | static void ui_msg_dialog_btn_clicked(ui_pbutton_t *, void *);
|
---|
| 57 |
|
---|
| 58 | ui_pbutton_cb_t ui_msg_dialog_btn_cb = {
|
---|
| 59 | .clicked = ui_msg_dialog_btn_clicked
|
---|
| 60 | };
|
---|
| 61 |
|
---|
[9a07ee3] | 62 | static const char *ui_msg_dialog_captions[][ui_msg_dialog_maxbtn + 1] = {
|
---|
| 63 | [umdc_ok] = { "OK", NULL },
|
---|
| 64 | [umdc_ok_cancel] = { "OK", "Cancel", NULL }
|
---|
| 65 | };
|
---|
| 66 |
|
---|
[de227aba] | 67 | /** Initialize message dialog parameters structure.
|
---|
| 68 | *
|
---|
| 69 | * Message dialog parameters structure must always be initialized using
|
---|
| 70 | * this function first.
|
---|
| 71 | *
|
---|
| 72 | * @param params Message dialog parameters structure
|
---|
| 73 | */
|
---|
| 74 | void ui_msg_dialog_params_init(ui_msg_dialog_params_t *params)
|
---|
| 75 | {
|
---|
| 76 | memset(params, 0, sizeof(ui_msg_dialog_params_t));
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | /** Create new message dialog.
|
---|
| 80 | *
|
---|
| 81 | * @param ui User interface
|
---|
| 82 | * @param params Message dialog parameters
|
---|
| 83 | * @param rdialog Place to store pointer to new dialog
|
---|
| 84 | * @return EOK on success or an error code
|
---|
| 85 | */
|
---|
| 86 | errno_t ui_msg_dialog_create(ui_t *ui, ui_msg_dialog_params_t *params,
|
---|
| 87 | ui_msg_dialog_t **rdialog)
|
---|
| 88 | {
|
---|
| 89 | errno_t rc;
|
---|
| 90 | ui_msg_dialog_t *dialog;
|
---|
| 91 | ui_window_t *window = NULL;
|
---|
| 92 | ui_wnd_params_t wparams;
|
---|
| 93 | ui_fixed_t *fixed = NULL;
|
---|
| 94 | ui_label_t *label = NULL;
|
---|
[9a07ee3] | 95 | ui_pbutton_t *btn[ui_msg_dialog_maxbtn];
|
---|
| 96 | const char **cp;
|
---|
| 97 | unsigned i, nb;
|
---|
[de227aba] | 98 | gfx_rect_t rect;
|
---|
[9a07ee3] | 99 | gfx_coord_t bw, bpad, btnsw, bp0x;
|
---|
[de227aba] | 100 | ui_resource_t *ui_res;
|
---|
| 101 |
|
---|
[9a07ee3] | 102 | for (i = 0; i < ui_msg_dialog_maxbtn; i++)
|
---|
| 103 | btn[i] = NULL;
|
---|
| 104 |
|
---|
[de227aba] | 105 | dialog = calloc(1, sizeof(ui_msg_dialog_t));
|
---|
| 106 | if (dialog == NULL) {
|
---|
| 107 | rc = ENOMEM;
|
---|
| 108 | goto error;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | ui_wnd_params_init(&wparams);
|
---|
| 112 | wparams.caption = params->caption;
|
---|
[2e08f2b6] | 113 | if ((params->flags & umdf_topmost) != 0)
|
---|
| 114 | wparams.flags |= wndf_topmost;
|
---|
| 115 | if ((params->flags & umdf_center) != 0)
|
---|
| 116 | wparams.placement = ui_wnd_place_center;
|
---|
[252d03c] | 117 |
|
---|
| 118 | /* FIXME: Auto layout */
|
---|
| 119 | if (ui_is_textmode(ui)) {
|
---|
| 120 | wparams.rect.p0.x = 0;
|
---|
| 121 | wparams.rect.p0.y = 0;
|
---|
[9a07ee3] | 122 | wparams.rect.p1.x = 60;
|
---|
[252d03c] | 123 | wparams.rect.p1.y = 7;
|
---|
| 124 | } else {
|
---|
| 125 | wparams.rect.p0.x = 0;
|
---|
| 126 | wparams.rect.p0.y = 0;
|
---|
[9a07ee3] | 127 | wparams.rect.p1.x = 400;
|
---|
[252d03c] | 128 | wparams.rect.p1.y = 110;
|
---|
| 129 | }
|
---|
[de227aba] | 130 |
|
---|
| 131 | rc = ui_window_create(ui, &wparams, &window);
|
---|
| 132 | if (rc != EOK)
|
---|
| 133 | goto error;
|
---|
| 134 |
|
---|
| 135 | ui_window_set_cb(window, &ui_msg_dialog_wnd_cb, dialog);
|
---|
| 136 |
|
---|
| 137 | ui_res = ui_window_get_res(window);
|
---|
| 138 |
|
---|
| 139 | rc = ui_fixed_create(&fixed);
|
---|
| 140 | if (rc != EOK)
|
---|
| 141 | goto error;
|
---|
| 142 |
|
---|
| 143 | rc = ui_label_create(ui_res, params->text, &label);
|
---|
| 144 | if (rc != EOK)
|
---|
| 145 | goto error;
|
---|
| 146 |
|
---|
[252d03c] | 147 | /* FIXME: Auto layout */
|
---|
| 148 | if (ui_is_textmode(ui)) {
|
---|
| 149 | rect.p0.x = 3;
|
---|
| 150 | rect.p0.y = 2;
|
---|
[9a07ee3] | 151 | rect.p1.x = 57;
|
---|
[252d03c] | 152 | rect.p1.y = 3;
|
---|
| 153 | } else {
|
---|
| 154 | rect.p0.x = 10;
|
---|
| 155 | rect.p0.y = 35;
|
---|
[9a07ee3] | 156 | rect.p1.x = 390;
|
---|
[252d03c] | 157 | rect.p1.y = 50;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[de227aba] | 160 | ui_label_set_rect(label, &rect);
|
---|
| 161 | ui_label_set_halign(label, gfx_halign_center);
|
---|
| 162 |
|
---|
| 163 | rc = ui_fixed_add(fixed, ui_label_ctl(label));
|
---|
| 164 | if (rc != EOK)
|
---|
| 165 | goto error;
|
---|
| 166 |
|
---|
| 167 | label = NULL;
|
---|
| 168 |
|
---|
[9a07ee3] | 169 | i = 0;
|
---|
| 170 | assert(params->choice == umdc_ok || params->choice == umdc_ok_cancel);
|
---|
| 171 | cp = ui_msg_dialog_captions[params->choice];
|
---|
[de227aba] | 172 |
|
---|
[9a07ee3] | 173 | while (*cp != NULL) {
|
---|
| 174 | rc = ui_pbutton_create(ui_res, *cp, &btn[i]);
|
---|
| 175 | if (rc != EOK)
|
---|
| 176 | goto error;
|
---|
| 177 |
|
---|
| 178 | ui_pbutton_set_cb(btn[i], &ui_msg_dialog_btn_cb, dialog);
|
---|
| 179 | ++cp;
|
---|
| 180 | ++i;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | nb = i;
|
---|
[de227aba] | 184 |
|
---|
[252d03c] | 185 | if (ui_is_textmode(ui)) {
|
---|
[9a07ee3] | 186 | bw = 12;
|
---|
| 187 | bpad = 2;
|
---|
[252d03c] | 188 | } else {
|
---|
[9a07ee3] | 189 | bw = 90;
|
---|
| 190 | bpad = 10;
|
---|
[252d03c] | 191 | }
|
---|
| 192 |
|
---|
[9a07ee3] | 193 | btnsw = (nb - 1) * (bw + bpad) + bw;
|
---|
| 194 | bp0x = (wparams.rect.p0.x + wparams.rect.p1.x - btnsw) / 2;
|
---|
| 195 |
|
---|
| 196 | for (i = 0; i < nb; i++) {
|
---|
| 197 | /* FIXME: Auto layout */
|
---|
| 198 | if (ui_is_textmode(ui)) {
|
---|
| 199 | rect.p0.x = bp0x + i * (bw + bpad);
|
---|
| 200 | rect.p0.y = 4;
|
---|
| 201 | rect.p1.x = bp0x + bw + i * (bw + bpad);
|
---|
| 202 | rect.p1.y = 5;
|
---|
| 203 | } else {
|
---|
| 204 | rect.p0.x = bp0x + i * (bw + bpad);
|
---|
| 205 | rect.p0.y = 60;
|
---|
| 206 | rect.p1.x = bp0x + bw + i * (bw + bpad);
|
---|
| 207 | rect.p1.y = 88;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | ui_pbutton_set_rect(btn[i], &rect);
|
---|
| 211 |
|
---|
| 212 | rc = ui_fixed_add(fixed, ui_pbutton_ctl(btn[i]));
|
---|
| 213 | if (rc != EOK)
|
---|
| 214 | goto error;
|
---|
| 215 | }
|
---|
[de227aba] | 216 |
|
---|
[9a07ee3] | 217 | ui_pbutton_set_default(btn[0], true);
|
---|
[de227aba] | 218 |
|
---|
[9a07ee3] | 219 | for (i = 0; i < ui_msg_dialog_maxbtn; i++) {
|
---|
| 220 | dialog->btn[i] = btn[i];
|
---|
| 221 | btn[i] = NULL;
|
---|
| 222 | }
|
---|
[de227aba] | 223 |
|
---|
| 224 | ui_window_add(window, ui_fixed_ctl(fixed));
|
---|
| 225 | fixed = NULL;
|
---|
| 226 |
|
---|
| 227 | rc = ui_window_paint(window);
|
---|
| 228 | if (rc != EOK)
|
---|
| 229 | goto error;
|
---|
| 230 |
|
---|
| 231 | dialog->window = window;
|
---|
| 232 | *rdialog = dialog;
|
---|
| 233 | return EOK;
|
---|
| 234 | error:
|
---|
[9a07ee3] | 235 | for (i = 0; i < ui_msg_dialog_maxbtn; i++) {
|
---|
| 236 | if (btn[i] != NULL)
|
---|
| 237 | ui_pbutton_destroy(btn[i]);
|
---|
| 238 | }
|
---|
[de227aba] | 239 | if (label != NULL)
|
---|
| 240 | ui_label_destroy(label);
|
---|
| 241 | if (fixed != NULL)
|
---|
| 242 | ui_fixed_destroy(fixed);
|
---|
| 243 | if (window != NULL)
|
---|
| 244 | ui_window_destroy(window);
|
---|
| 245 | if (dialog != NULL)
|
---|
| 246 | free(dialog);
|
---|
| 247 | return rc;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | /** Destroy message dialog.
|
---|
| 251 | *
|
---|
| 252 | * @param dialog Message dialog or @c NULL
|
---|
| 253 | */
|
---|
| 254 | void ui_msg_dialog_destroy(ui_msg_dialog_t *dialog)
|
---|
| 255 | {
|
---|
| 256 | if (dialog == NULL)
|
---|
| 257 | return;
|
---|
| 258 |
|
---|
| 259 | ui_window_destroy(dialog->window);
|
---|
| 260 | free(dialog);
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | /** Set mesage dialog callback.
|
---|
| 264 | *
|
---|
| 265 | * @param dialog Message dialog
|
---|
| 266 | * @param cb Message dialog callbacks
|
---|
| 267 | * @param arg Callback argument
|
---|
| 268 | */
|
---|
| 269 | void ui_msg_dialog_set_cb(ui_msg_dialog_t *dialog, ui_msg_dialog_cb_t *cb,
|
---|
| 270 | void *arg)
|
---|
| 271 | {
|
---|
| 272 | dialog->cb = cb;
|
---|
| 273 | dialog->arg = arg;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | /** Message dialog window close handler.
|
---|
| 277 | *
|
---|
| 278 | * @param window Window
|
---|
| 279 | * @param arg Argument (ui_msg_dialog_t *)
|
---|
| 280 | */
|
---|
| 281 | static void ui_msg_dialog_wnd_close(ui_window_t *window, void *arg)
|
---|
| 282 | {
|
---|
| 283 | ui_msg_dialog_t *dialog = (ui_msg_dialog_t *) arg;
|
---|
| 284 |
|
---|
| 285 | if (dialog->cb != NULL && dialog->cb->close != NULL)
|
---|
| 286 | dialog->cb->close(dialog, dialog->arg);
|
---|
| 287 | }
|
---|
| 288 |
|
---|
[12dd36c] | 289 | /** Message dialog window keyboard event handler.
|
---|
| 290 | *
|
---|
| 291 | * @param window Window
|
---|
| 292 | * @param arg Argument (ui_msg_dialog_t *)
|
---|
| 293 | * @param event Keyboard event
|
---|
| 294 | */
|
---|
| 295 | static void ui_msg_dialog_wnd_kbd(ui_window_t *window, void *arg,
|
---|
| 296 | kbd_event_t *event)
|
---|
| 297 | {
|
---|
| 298 | ui_msg_dialog_t *dialog = (ui_msg_dialog_t *) arg;
|
---|
| 299 | ui_evclaim_t claim;
|
---|
| 300 |
|
---|
| 301 | claim = ui_window_def_kbd(window, event);
|
---|
| 302 | if (claim == ui_claimed)
|
---|
| 303 | return;
|
---|
| 304 |
|
---|
| 305 | if (event->type == KEY_PRESS &&
|
---|
| 306 | (event->mods & (KM_CTRL | KM_SHIFT | KM_ALT)) == 0) {
|
---|
| 307 | if (event->key == KC_ENTER) {
|
---|
| 308 | /* OK / default button */
|
---|
| 309 | if (dialog->cb != NULL && dialog->cb->button != NULL) {
|
---|
| 310 | dialog->cb->button(dialog, dialog->arg, 0);
|
---|
| 311 | return;
|
---|
| 312 | }
|
---|
| 313 | } else if (event->key == KC_ESCAPE) {
|
---|
| 314 | /* Cancel */
|
---|
| 315 | if (dialog->cb != NULL && dialog->cb->close != NULL) {
|
---|
| 316 | dialog->cb->close(dialog, dialog->arg);
|
---|
| 317 | return;
|
---|
| 318 | }
|
---|
| 319 | }
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 | }
|
---|
| 323 |
|
---|
[de227aba] | 324 | /** Message dialog button click handler.
|
---|
| 325 | *
|
---|
| 326 | * @param pbutton Push button
|
---|
| 327 | * @param arg Argument (ui_msg_dialog_t *)
|
---|
| 328 | */
|
---|
| 329 | static void ui_msg_dialog_btn_clicked(ui_pbutton_t *pbutton, void *arg)
|
---|
| 330 | {
|
---|
| 331 | ui_msg_dialog_t *dialog = (ui_msg_dialog_t *) arg;
|
---|
[9a07ee3] | 332 | unsigned i;
|
---|
[de227aba] | 333 |
|
---|
[9a07ee3] | 334 | if (dialog->cb != NULL && dialog->cb->button != NULL) {
|
---|
| 335 | for (i = 0; i < ui_msg_dialog_maxbtn; i++) {
|
---|
| 336 | if (dialog->btn[i] == pbutton)
|
---|
| 337 | dialog->cb->button(dialog, dialog->arg, i);
|
---|
| 338 | }
|
---|
| 339 | }
|
---|
[de227aba] | 340 | }
|
---|
| 341 |
|
---|
| 342 | /** @}
|
---|
| 343 | */
|
---|