| 1 | /*
|
|---|
| 2 | * SPDX-FileCopyrightText: 2021 Jiri Svoboda
|
|---|
| 3 | *
|
|---|
| 4 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | /** @addtogroup libui
|
|---|
| 8 | * @{
|
|---|
| 9 | */
|
|---|
| 10 | /**
|
|---|
| 11 | * @file Message dialog
|
|---|
| 12 | */
|
|---|
| 13 |
|
|---|
| 14 | #include <errno.h>
|
|---|
| 15 | #include <mem.h>
|
|---|
| 16 | #include <stdlib.h>
|
|---|
| 17 | #include <ui/fixed.h>
|
|---|
| 18 | #include <ui/label.h>
|
|---|
| 19 | #include <ui/msgdialog.h>
|
|---|
| 20 | #include <ui/pbutton.h>
|
|---|
| 21 | #include <ui/resource.h>
|
|---|
| 22 | #include <ui/ui.h>
|
|---|
| 23 | #include <ui/window.h>
|
|---|
| 24 | #include "../private/msgdialog.h"
|
|---|
| 25 |
|
|---|
| 26 | static void ui_msg_dialog_wnd_close(ui_window_t *, void *);
|
|---|
| 27 |
|
|---|
| 28 | ui_window_cb_t ui_msg_dialog_wnd_cb = {
|
|---|
| 29 | .close = ui_msg_dialog_wnd_close
|
|---|
| 30 | };
|
|---|
| 31 |
|
|---|
| 32 | static void ui_msg_dialog_btn_clicked(ui_pbutton_t *, void *);
|
|---|
| 33 |
|
|---|
| 34 | ui_pbutton_cb_t ui_msg_dialog_btn_cb = {
|
|---|
| 35 | .clicked = ui_msg_dialog_btn_clicked
|
|---|
| 36 | };
|
|---|
| 37 |
|
|---|
| 38 | /** Initialize message dialog parameters structure.
|
|---|
| 39 | *
|
|---|
| 40 | * Message dialog parameters structure must always be initialized using
|
|---|
| 41 | * this function first.
|
|---|
| 42 | *
|
|---|
| 43 | * @param params Message dialog parameters structure
|
|---|
| 44 | */
|
|---|
| 45 | void ui_msg_dialog_params_init(ui_msg_dialog_params_t *params)
|
|---|
| 46 | {
|
|---|
| 47 | memset(params, 0, sizeof(ui_msg_dialog_params_t));
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | /** Create new message dialog.
|
|---|
| 51 | *
|
|---|
| 52 | * @param ui User interface
|
|---|
| 53 | * @param params Message dialog parameters
|
|---|
| 54 | * @param rdialog Place to store pointer to new dialog
|
|---|
| 55 | * @return EOK on success or an error code
|
|---|
| 56 | */
|
|---|
| 57 | errno_t ui_msg_dialog_create(ui_t *ui, ui_msg_dialog_params_t *params,
|
|---|
| 58 | ui_msg_dialog_t **rdialog)
|
|---|
| 59 | {
|
|---|
| 60 | errno_t rc;
|
|---|
| 61 | ui_msg_dialog_t *dialog;
|
|---|
| 62 | ui_window_t *window = NULL;
|
|---|
| 63 | ui_wnd_params_t wparams;
|
|---|
| 64 | ui_fixed_t *fixed = NULL;
|
|---|
| 65 | ui_label_t *label = NULL;
|
|---|
| 66 | ui_pbutton_t *bok = NULL;
|
|---|
| 67 | gfx_rect_t rect;
|
|---|
| 68 | ui_resource_t *ui_res;
|
|---|
| 69 |
|
|---|
| 70 | dialog = calloc(1, sizeof(ui_msg_dialog_t));
|
|---|
| 71 | if (dialog == NULL) {
|
|---|
| 72 | rc = ENOMEM;
|
|---|
| 73 | goto error;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | ui_wnd_params_init(&wparams);
|
|---|
| 77 | wparams.caption = params->caption;
|
|---|
| 78 |
|
|---|
| 79 | /* FIXME: Auto layout */
|
|---|
| 80 | if (ui_is_textmode(ui)) {
|
|---|
| 81 | wparams.rect.p0.x = 0;
|
|---|
| 82 | wparams.rect.p0.y = 0;
|
|---|
| 83 | wparams.rect.p1.x = 40;
|
|---|
| 84 | wparams.rect.p1.y = 7;
|
|---|
| 85 | } else {
|
|---|
| 86 | wparams.rect.p0.x = 0;
|
|---|
| 87 | wparams.rect.p0.y = 0;
|
|---|
| 88 | wparams.rect.p1.x = 200;
|
|---|
| 89 | wparams.rect.p1.y = 110;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | rc = ui_window_create(ui, &wparams, &window);
|
|---|
| 93 | if (rc != EOK)
|
|---|
| 94 | goto error;
|
|---|
| 95 |
|
|---|
| 96 | ui_window_set_cb(window, &ui_msg_dialog_wnd_cb, dialog);
|
|---|
| 97 |
|
|---|
| 98 | ui_res = ui_window_get_res(window);
|
|---|
| 99 |
|
|---|
| 100 | rc = ui_fixed_create(&fixed);
|
|---|
| 101 | if (rc != EOK)
|
|---|
| 102 | goto error;
|
|---|
| 103 |
|
|---|
| 104 | rc = ui_label_create(ui_res, params->text, &label);
|
|---|
| 105 | if (rc != EOK)
|
|---|
| 106 | goto error;
|
|---|
| 107 |
|
|---|
| 108 | /* FIXME: Auto layout */
|
|---|
| 109 | if (ui_is_textmode(ui)) {
|
|---|
| 110 | rect.p0.x = 3;
|
|---|
| 111 | rect.p0.y = 2;
|
|---|
| 112 | rect.p1.x = 17;
|
|---|
| 113 | rect.p1.y = 3;
|
|---|
| 114 | } else {
|
|---|
| 115 | rect.p0.x = 10;
|
|---|
| 116 | rect.p0.y = 35;
|
|---|
| 117 | rect.p1.x = 190;
|
|---|
| 118 | rect.p1.y = 50;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | ui_label_set_rect(label, &rect);
|
|---|
| 122 | ui_label_set_halign(label, gfx_halign_center);
|
|---|
| 123 |
|
|---|
| 124 | rc = ui_fixed_add(fixed, ui_label_ctl(label));
|
|---|
| 125 | if (rc != EOK)
|
|---|
| 126 | goto error;
|
|---|
| 127 |
|
|---|
| 128 | label = NULL;
|
|---|
| 129 |
|
|---|
| 130 | rc = ui_pbutton_create(ui_res, "OK", &bok);
|
|---|
| 131 | if (rc != EOK)
|
|---|
| 132 | goto error;
|
|---|
| 133 |
|
|---|
| 134 | ui_pbutton_set_cb(bok, &ui_msg_dialog_btn_cb, dialog);
|
|---|
| 135 |
|
|---|
| 136 | /* FIXME: Auto layout */
|
|---|
| 137 | if (ui_is_textmode(ui)) {
|
|---|
| 138 | rect.p0.x = 8;
|
|---|
| 139 | rect.p0.y = 4;
|
|---|
| 140 | rect.p1.x = 12;
|
|---|
| 141 | rect.p1.y = 5;
|
|---|
| 142 | } else {
|
|---|
| 143 | rect.p0.x = 55;
|
|---|
| 144 | rect.p0.y = 60;
|
|---|
| 145 | rect.p1.x = 145;
|
|---|
| 146 | rect.p1.y = 88;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | ui_pbutton_set_rect(bok, &rect);
|
|---|
| 150 |
|
|---|
| 151 | ui_pbutton_set_default(bok, true);
|
|---|
| 152 |
|
|---|
| 153 | rc = ui_fixed_add(fixed, ui_pbutton_ctl(bok));
|
|---|
| 154 | if (rc != EOK)
|
|---|
| 155 | goto error;
|
|---|
| 156 |
|
|---|
| 157 | dialog->bok = bok;
|
|---|
| 158 | bok = NULL;
|
|---|
| 159 |
|
|---|
| 160 | ui_window_add(window, ui_fixed_ctl(fixed));
|
|---|
| 161 | fixed = NULL;
|
|---|
| 162 |
|
|---|
| 163 | rc = ui_window_paint(window);
|
|---|
| 164 | if (rc != EOK)
|
|---|
| 165 | goto error;
|
|---|
| 166 |
|
|---|
| 167 | dialog->window = window;
|
|---|
| 168 | *rdialog = dialog;
|
|---|
| 169 | return EOK;
|
|---|
| 170 | error:
|
|---|
| 171 | if (bok != NULL)
|
|---|
| 172 | ui_pbutton_destroy(bok);
|
|---|
| 173 | if (label != NULL)
|
|---|
| 174 | ui_label_destroy(label);
|
|---|
| 175 | if (fixed != NULL)
|
|---|
| 176 | ui_fixed_destroy(fixed);
|
|---|
| 177 | if (window != NULL)
|
|---|
| 178 | ui_window_destroy(window);
|
|---|
| 179 | if (dialog != NULL)
|
|---|
| 180 | free(dialog);
|
|---|
| 181 | return rc;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | /** Destroy message dialog.
|
|---|
| 185 | *
|
|---|
| 186 | * @param dialog Message dialog or @c NULL
|
|---|
| 187 | */
|
|---|
| 188 | void ui_msg_dialog_destroy(ui_msg_dialog_t *dialog)
|
|---|
| 189 | {
|
|---|
| 190 | if (dialog == NULL)
|
|---|
| 191 | return;
|
|---|
| 192 |
|
|---|
| 193 | ui_window_destroy(dialog->window);
|
|---|
| 194 | free(dialog);
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | /** Set mesage dialog callback.
|
|---|
| 198 | *
|
|---|
| 199 | * @param dialog Message dialog
|
|---|
| 200 | * @param cb Message dialog callbacks
|
|---|
| 201 | * @param arg Callback argument
|
|---|
| 202 | */
|
|---|
| 203 | void ui_msg_dialog_set_cb(ui_msg_dialog_t *dialog, ui_msg_dialog_cb_t *cb,
|
|---|
| 204 | void *arg)
|
|---|
| 205 | {
|
|---|
| 206 | dialog->cb = cb;
|
|---|
| 207 | dialog->arg = arg;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | /** Message dialog window close handler.
|
|---|
| 211 | *
|
|---|
| 212 | * @param window Window
|
|---|
| 213 | * @param arg Argument (ui_msg_dialog_t *)
|
|---|
| 214 | */
|
|---|
| 215 | static void ui_msg_dialog_wnd_close(ui_window_t *window, void *arg)
|
|---|
| 216 | {
|
|---|
| 217 | ui_msg_dialog_t *dialog = (ui_msg_dialog_t *) arg;
|
|---|
| 218 |
|
|---|
| 219 | if (dialog->cb != NULL && dialog->cb->close != NULL)
|
|---|
| 220 | dialog->cb->close(dialog, dialog->arg);
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | /** Message dialog button click handler.
|
|---|
| 224 | *
|
|---|
| 225 | * @param pbutton Push button
|
|---|
| 226 | * @param arg Argument (ui_msg_dialog_t *)
|
|---|
| 227 | */
|
|---|
| 228 | static void ui_msg_dialog_btn_clicked(ui_pbutton_t *pbutton, void *arg)
|
|---|
| 229 | {
|
|---|
| 230 | ui_msg_dialog_t *dialog = (ui_msg_dialog_t *) arg;
|
|---|
| 231 |
|
|---|
| 232 | if (dialog->cb != NULL && dialog->cb->button != NULL)
|
|---|
| 233 | dialog->cb->button(dialog, dialog->arg, 0);
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | /** @}
|
|---|
| 237 | */
|
|---|