1 | /*
|
---|
2 | * Copyright (c) 2020 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 uidemo
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file User interface demo
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <gfx/color.h>
|
---|
36 | #include <gfx/coord.h>
|
---|
37 | #include <gfx/render.h>
|
---|
38 | #include <io/pos_event.h>
|
---|
39 | #include <stdio.h>
|
---|
40 | #include <str.h>
|
---|
41 | #include <task.h>
|
---|
42 | #include <ui/fixed.h>
|
---|
43 | #include <ui/label.h>
|
---|
44 | #include <ui/pbutton.h>
|
---|
45 | #include <ui/resource.h>
|
---|
46 | #include <ui/ui.h>
|
---|
47 | #include <ui/wdecor.h>
|
---|
48 | #include <ui/window.h>
|
---|
49 | #include "uidemo.h"
|
---|
50 |
|
---|
51 | static void wnd_close(ui_window_t *, void *);
|
---|
52 | static void wnd_pos(ui_window_t *, void *, pos_event_t *pos);
|
---|
53 |
|
---|
54 | static ui_window_cb_t window_cb = {
|
---|
55 | .close = wnd_close,
|
---|
56 | .pos = wnd_pos
|
---|
57 | };
|
---|
58 |
|
---|
59 | static void pb_clicked(ui_pbutton_t *, void *);
|
---|
60 |
|
---|
61 | static ui_pbutton_cb_t pbutton_cb = {
|
---|
62 | .clicked = pb_clicked
|
---|
63 | };
|
---|
64 |
|
---|
65 | /** Window close button was clicked.
|
---|
66 | *
|
---|
67 | * @param window Window
|
---|
68 | * @param arg Argument (demo)
|
---|
69 | */
|
---|
70 | static void wnd_close(ui_window_t *window, void *arg)
|
---|
71 | {
|
---|
72 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
73 |
|
---|
74 | ui_quit(demo->ui);
|
---|
75 | }
|
---|
76 |
|
---|
77 | /** Window position event.
|
---|
78 | *
|
---|
79 | * @param window Window
|
---|
80 | * @param arg Argument (demo)
|
---|
81 | */
|
---|
82 | static void wnd_pos(ui_window_t *window, void *arg, pos_event_t *event)
|
---|
83 | {
|
---|
84 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
85 |
|
---|
86 | /* Make sure we don't process events until fully initialized */
|
---|
87 | if (demo->fixed == NULL)
|
---|
88 | return;
|
---|
89 |
|
---|
90 | ui_fixed_pos_event(demo->fixed, event);
|
---|
91 | }
|
---|
92 |
|
---|
93 | /** Push button was clicked.
|
---|
94 | *
|
---|
95 | * @param pbutton Push button
|
---|
96 | * @param arg Argument (demo)
|
---|
97 | */
|
---|
98 | static void pb_clicked(ui_pbutton_t *pbutton, void *arg)
|
---|
99 | {
|
---|
100 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
101 | errno_t rc;
|
---|
102 |
|
---|
103 | if (pbutton == demo->pb1) {
|
---|
104 | rc = ui_label_set_text(demo->label, "Confirmed");
|
---|
105 | if (rc != EOK)
|
---|
106 | printf("Error changing label text.\n");
|
---|
107 | (void) ui_label_paint(demo->label);
|
---|
108 | } else {
|
---|
109 | rc = ui_label_set_text(demo->label, "Cancelled");
|
---|
110 | if (rc != EOK)
|
---|
111 | printf("Error changing label text.\n");
|
---|
112 | (void) ui_label_paint(demo->label);
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | /** Run UI demo on display server. */
|
---|
117 | static errno_t ui_demo(const char *display_spec)
|
---|
118 | {
|
---|
119 | ui_t *ui = NULL;
|
---|
120 | ui_wnd_params_t params;
|
---|
121 | ui_window_t *window = NULL;
|
---|
122 | ui_demo_t demo;
|
---|
123 | gfx_rect_t rect;
|
---|
124 | gfx_rect_t app_rect;
|
---|
125 | gfx_color_t *color;
|
---|
126 | ui_resource_t *ui_res;
|
---|
127 | gfx_context_t *gc;
|
---|
128 | errno_t rc;
|
---|
129 |
|
---|
130 | rc = ui_create(display_spec, &ui);
|
---|
131 | if (rc != EOK) {
|
---|
132 | printf("Error creating UI on display %s.\n", display_spec);
|
---|
133 | return rc;
|
---|
134 | }
|
---|
135 |
|
---|
136 | ui_wnd_params_init(¶ms);
|
---|
137 | params.caption = "UI Demo";
|
---|
138 | params.rect.p0.x = 0;
|
---|
139 | params.rect.p0.y = 0;
|
---|
140 | params.rect.p1.x = 220;
|
---|
141 | params.rect.p1.y = 100;
|
---|
142 |
|
---|
143 | memset((void *) &demo, 0, sizeof(demo));
|
---|
144 | demo.ui = ui;
|
---|
145 |
|
---|
146 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
147 | if (rc != EOK) {
|
---|
148 | printf("Error creating window.\n");
|
---|
149 | return rc;
|
---|
150 | }
|
---|
151 |
|
---|
152 | ui_window_set_cb(window, &window_cb, (void *) &demo);
|
---|
153 | demo.window = window;
|
---|
154 |
|
---|
155 | task_retval(0);
|
---|
156 |
|
---|
157 | ui_res = ui_window_get_res(window);
|
---|
158 | gc = ui_window_get_gc(window);
|
---|
159 | ui_window_get_app_rect(window, &app_rect);
|
---|
160 |
|
---|
161 | rc = ui_fixed_create(&demo.fixed);
|
---|
162 | if (rc != EOK) {
|
---|
163 | printf("Error creating fixed layout.\n");
|
---|
164 | return rc;
|
---|
165 | }
|
---|
166 |
|
---|
167 | rc = ui_label_create(ui_res, "Hello there!", &demo.label);
|
---|
168 | if (rc != EOK) {
|
---|
169 | printf("Error creating label.\n");
|
---|
170 | return rc;
|
---|
171 | }
|
---|
172 |
|
---|
173 | rect.p0.x = 60;
|
---|
174 | rect.p0.y = 37;
|
---|
175 | rect.p1.x = 160;
|
---|
176 | rect.p1.y = 50;
|
---|
177 | ui_label_set_rect(demo.label, &rect);
|
---|
178 | ui_label_set_halign(demo.label, gfx_halign_center);
|
---|
179 |
|
---|
180 | rc = ui_fixed_add(demo.fixed, ui_label_ctl(demo.label));
|
---|
181 | if (rc != EOK) {
|
---|
182 | printf("Error adding control to layout.\n");
|
---|
183 | return rc;
|
---|
184 | }
|
---|
185 |
|
---|
186 | rc = ui_pbutton_create(ui_res, "Confirm", &demo.pb1);
|
---|
187 | if (rc != EOK) {
|
---|
188 | printf("Error creating button.\n");
|
---|
189 | return rc;
|
---|
190 | }
|
---|
191 |
|
---|
192 | ui_pbutton_set_cb(demo.pb1, &pbutton_cb, (void *) &demo);
|
---|
193 |
|
---|
194 | rect.p0.x = 15;
|
---|
195 | rect.p0.y = 60;
|
---|
196 | rect.p1.x = 105;
|
---|
197 | rect.p1.y = 88;
|
---|
198 | ui_pbutton_set_rect(demo.pb1, &rect);
|
---|
199 |
|
---|
200 | ui_pbutton_set_default(demo.pb1, true);
|
---|
201 |
|
---|
202 | rc = ui_fixed_add(demo.fixed, ui_pbutton_ctl(demo.pb1));
|
---|
203 | if (rc != EOK) {
|
---|
204 | printf("Error adding control to layout.\n");
|
---|
205 | return rc;
|
---|
206 | }
|
---|
207 |
|
---|
208 | rc = ui_pbutton_create(ui_res, "Cancel", &demo.pb2);
|
---|
209 | if (rc != EOK) {
|
---|
210 | printf("Error creating button.\n");
|
---|
211 | return rc;
|
---|
212 | }
|
---|
213 |
|
---|
214 | ui_pbutton_set_cb(demo.pb2, &pbutton_cb, (void *) &demo);
|
---|
215 |
|
---|
216 | rect.p0.x = 115;
|
---|
217 | rect.p0.y = 60;
|
---|
218 | rect.p1.x = 205;
|
---|
219 | rect.p1.y = 88;
|
---|
220 | ui_pbutton_set_rect(demo.pb2, &rect);
|
---|
221 |
|
---|
222 | rc = ui_fixed_add(demo.fixed, ui_pbutton_ctl(demo.pb2));
|
---|
223 | if (rc != EOK) {
|
---|
224 | printf("Error adding control to layout.\n");
|
---|
225 | return rc;
|
---|
226 | }
|
---|
227 |
|
---|
228 | rc = gfx_color_new_rgb_i16(0xc8c8, 0xc8c8, 0xc8c8, &color);
|
---|
229 | if (rc != EOK) {
|
---|
230 | printf("Error allocating color.\n");
|
---|
231 | return rc;
|
---|
232 | }
|
---|
233 |
|
---|
234 | rc = gfx_set_color(gc, color);
|
---|
235 | if (rc != EOK) {
|
---|
236 | printf("Error setting color.\n");
|
---|
237 | return rc;
|
---|
238 | }
|
---|
239 |
|
---|
240 | rc = gfx_fill_rect(gc, &app_rect);
|
---|
241 | if (rc != EOK) {
|
---|
242 | printf("Error filling background.\n");
|
---|
243 | return rc;
|
---|
244 | }
|
---|
245 |
|
---|
246 | rc = ui_label_paint(demo.label);
|
---|
247 | if (rc != EOK) {
|
---|
248 | printf("Error painting button.\n");
|
---|
249 | return rc;
|
---|
250 | }
|
---|
251 |
|
---|
252 | rc = ui_pbutton_paint(demo.pb1);
|
---|
253 | if (rc != EOK) {
|
---|
254 | printf("Error painting button.\n");
|
---|
255 | return rc;
|
---|
256 | }
|
---|
257 |
|
---|
258 | rc = ui_pbutton_paint(demo.pb2);
|
---|
259 | if (rc != EOK) {
|
---|
260 | printf("Error painting button.\n");
|
---|
261 | return rc;
|
---|
262 | }
|
---|
263 |
|
---|
264 | ui_run(ui);
|
---|
265 |
|
---|
266 | ui_fixed_remove(demo.fixed, ui_label_ctl(demo.label));
|
---|
267 | ui_fixed_remove(demo.fixed, ui_pbutton_ctl(demo.pb1));
|
---|
268 | ui_fixed_remove(demo.fixed, ui_pbutton_ctl(demo.pb2));
|
---|
269 |
|
---|
270 | ui_pbutton_destroy(demo.pb1);
|
---|
271 | ui_pbutton_destroy(demo.pb2);
|
---|
272 |
|
---|
273 | ui_window_destroy(window);
|
---|
274 | ui_destroy(ui);
|
---|
275 |
|
---|
276 | return EOK;
|
---|
277 | }
|
---|
278 |
|
---|
279 | static void print_syntax(void)
|
---|
280 | {
|
---|
281 | printf("Syntax: uidemo [-d <display-spec>]\n");
|
---|
282 | }
|
---|
283 |
|
---|
284 | int main(int argc, char *argv[])
|
---|
285 | {
|
---|
286 | const char *display_spec = UI_DISPLAY_DEFAULT;
|
---|
287 | errno_t rc;
|
---|
288 | int i;
|
---|
289 |
|
---|
290 | i = 1;
|
---|
291 | while (i < argc && argv[i][0] == '-') {
|
---|
292 | if (str_cmp(argv[i], "-d") == 0) {
|
---|
293 | ++i;
|
---|
294 | if (i >= argc) {
|
---|
295 | printf("Argument missing.\n");
|
---|
296 | print_syntax();
|
---|
297 | return 1;
|
---|
298 | }
|
---|
299 |
|
---|
300 | display_spec = argv[i++];
|
---|
301 | } else {
|
---|
302 | printf("Invalid option '%s'.\n", argv[i]);
|
---|
303 | print_syntax();
|
---|
304 | return 1;
|
---|
305 | }
|
---|
306 | }
|
---|
307 |
|
---|
308 | if (i < argc) {
|
---|
309 | print_syntax();
|
---|
310 | return 1;
|
---|
311 | }
|
---|
312 |
|
---|
313 | rc = ui_demo(display_spec);
|
---|
314 | if (rc != EOK)
|
---|
315 | return 1;
|
---|
316 |
|
---|
317 | return 0;
|
---|
318 | }
|
---|
319 |
|
---|
320 | /** @}
|
---|
321 | */
|
---|