source: mainline/uspace/lib/ui/src/msgdialog.c@ 6a0b2cc

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6a0b2cc was 45004f3, checked in by Jiri Svoboda <jiri@…>, 4 years ago

Text-style window title bar

  • Property mode set to 100644
File size: 6.1 KB
Line 
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
48static void ui_msg_dialog_wnd_close(ui_window_t *, void *);
49
50ui_window_cb_t ui_msg_dialog_wnd_cb = {
51 .close = ui_msg_dialog_wnd_close
52};
53
54static void ui_msg_dialog_btn_clicked(ui_pbutton_t *, void *);
55
56ui_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 */
67void 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 */
79errno_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 = 40;
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 dialog->bok = bok;
180 bok = NULL;
181
182 ui_window_add(window, ui_fixed_ctl(fixed));
183 fixed = NULL;
184
185 rc = ui_window_paint(window);
186 if (rc != EOK)
187 goto error;
188
189 dialog->window = window;
190 *rdialog = dialog;
191 return EOK;
192error:
193 if (bok != NULL)
194 ui_pbutton_destroy(bok);
195 if (label != NULL)
196 ui_label_destroy(label);
197 if (fixed != NULL)
198 ui_fixed_destroy(fixed);
199 if (window != NULL)
200 ui_window_destroy(window);
201 if (dialog != NULL)
202 free(dialog);
203 return rc;
204}
205
206/** Destroy message dialog.
207 *
208 * @param dialog Message dialog or @c NULL
209 */
210void ui_msg_dialog_destroy(ui_msg_dialog_t *dialog)
211{
212 if (dialog == NULL)
213 return;
214
215 ui_window_destroy(dialog->window);
216 free(dialog);
217}
218
219/** Set mesage dialog callback.
220 *
221 * @param dialog Message dialog
222 * @param cb Message dialog callbacks
223 * @param arg Callback argument
224 */
225void ui_msg_dialog_set_cb(ui_msg_dialog_t *dialog, ui_msg_dialog_cb_t *cb,
226 void *arg)
227{
228 dialog->cb = cb;
229 dialog->arg = arg;
230}
231
232/** Message dialog window close handler.
233 *
234 * @param window Window
235 * @param arg Argument (ui_msg_dialog_t *)
236 */
237static void ui_msg_dialog_wnd_close(ui_window_t *window, void *arg)
238{
239 ui_msg_dialog_t *dialog = (ui_msg_dialog_t *) arg;
240
241 if (dialog->cb != NULL && dialog->cb->close != NULL)
242 dialog->cb->close(dialog, dialog->arg);
243}
244
245/** Message dialog button click handler.
246 *
247 * @param pbutton Push button
248 * @param arg Argument (ui_msg_dialog_t *)
249 */
250static void ui_msg_dialog_btn_clicked(ui_pbutton_t *pbutton, void *arg)
251{
252 ui_msg_dialog_t *dialog = (ui_msg_dialog_t *) arg;
253
254 if (dialog->cb != NULL && dialog->cb->button != NULL)
255 dialog->cb->button(dialog, dialog->arg, 0);
256}
257
258/** @}
259 */
Note: See TracBrowser for help on using the repository browser.