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