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/bitmap.h>
|
---|
36 | #include <gfx/coord.h>
|
---|
37 | #include <io/pixelmap.h>
|
---|
38 | #include <stdio.h>
|
---|
39 | #include <str.h>
|
---|
40 | #include <ui/entry.h>
|
---|
41 | #include <ui/fixed.h>
|
---|
42 | #include <ui/image.h>
|
---|
43 | #include <ui/label.h>
|
---|
44 | #include <ui/pbutton.h>
|
---|
45 | #include <ui/resource.h>
|
---|
46 | #include <ui/ui.h>
|
---|
47 | #include <ui/window.h>
|
---|
48 | #include "uidemo.h"
|
---|
49 |
|
---|
50 | static errno_t bitmap_moire(gfx_bitmap_t *, gfx_coord_t, gfx_coord_t);
|
---|
51 |
|
---|
52 | static void wnd_close(ui_window_t *, void *);
|
---|
53 |
|
---|
54 | static ui_window_cb_t window_cb = {
|
---|
55 | .close = wnd_close
|
---|
56 | };
|
---|
57 |
|
---|
58 | static void pb_clicked(ui_pbutton_t *, void *);
|
---|
59 |
|
---|
60 | static ui_pbutton_cb_t pbutton_cb = {
|
---|
61 | .clicked = pb_clicked
|
---|
62 | };
|
---|
63 |
|
---|
64 | /** Window close button was clicked.
|
---|
65 | *
|
---|
66 | * @param window Window
|
---|
67 | * @param arg Argument (demo)
|
---|
68 | */
|
---|
69 | static void wnd_close(ui_window_t *window, void *arg)
|
---|
70 | {
|
---|
71 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
72 |
|
---|
73 | ui_quit(demo->ui);
|
---|
74 | }
|
---|
75 |
|
---|
76 | /** Push button was clicked.
|
---|
77 | *
|
---|
78 | * @param pbutton Push button
|
---|
79 | * @param arg Argument (demo)
|
---|
80 | */
|
---|
81 | static void pb_clicked(ui_pbutton_t *pbutton, void *arg)
|
---|
82 | {
|
---|
83 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
84 | errno_t rc;
|
---|
85 |
|
---|
86 | if (pbutton == demo->pb1) {
|
---|
87 | rc = ui_entry_set_text(demo->entry, "OK pressed");
|
---|
88 | if (rc != EOK)
|
---|
89 | printf("Error changing entry text.\n");
|
---|
90 | (void) ui_entry_paint(demo->entry);
|
---|
91 | } else {
|
---|
92 | rc = ui_entry_set_text(demo->entry, "Cancel pressed");
|
---|
93 | if (rc != EOK)
|
---|
94 | printf("Error changing entry text.\n");
|
---|
95 | (void) ui_entry_paint(demo->entry);
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | /** Run UI demo on display server. */
|
---|
100 | static errno_t ui_demo(const char *display_spec)
|
---|
101 | {
|
---|
102 | ui_t *ui = NULL;
|
---|
103 | ui_wnd_params_t params;
|
---|
104 | ui_window_t *window = NULL;
|
---|
105 | ui_demo_t demo;
|
---|
106 | gfx_rect_t rect;
|
---|
107 | gfx_context_t *gc;
|
---|
108 | ui_resource_t *ui_res;
|
---|
109 | gfx_bitmap_params_t bparams;
|
---|
110 | gfx_bitmap_t *bitmap;
|
---|
111 | gfx_coord2_t off;
|
---|
112 | errno_t rc;
|
---|
113 |
|
---|
114 | rc = ui_create(display_spec, &ui);
|
---|
115 | if (rc != EOK) {
|
---|
116 | printf("Error creating UI on display %s.\n", display_spec);
|
---|
117 | return rc;
|
---|
118 | }
|
---|
119 |
|
---|
120 | ui_wnd_params_init(¶ms);
|
---|
121 | params.caption = "UI Demo";
|
---|
122 | params.rect.p0.x = 0;
|
---|
123 | params.rect.p0.y = 0;
|
---|
124 | params.rect.p1.x = 220;
|
---|
125 | params.rect.p1.y = 180;
|
---|
126 |
|
---|
127 | memset((void *) &demo, 0, sizeof(demo));
|
---|
128 | demo.ui = ui;
|
---|
129 |
|
---|
130 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
131 | if (rc != EOK) {
|
---|
132 | printf("Error creating window.\n");
|
---|
133 | return rc;
|
---|
134 | }
|
---|
135 |
|
---|
136 | ui_window_set_cb(window, &window_cb, (void *) &demo);
|
---|
137 | demo.window = window;
|
---|
138 |
|
---|
139 | ui_res = ui_window_get_res(window);
|
---|
140 | gc = ui_window_get_gc(window);
|
---|
141 |
|
---|
142 | rc = ui_fixed_create(&demo.fixed);
|
---|
143 | if (rc != EOK) {
|
---|
144 | printf("Error creating fixed layout.\n");
|
---|
145 | return rc;
|
---|
146 | }
|
---|
147 |
|
---|
148 | rc = ui_label_create(ui_res, "Text label", &demo.label);
|
---|
149 | if (rc != EOK) {
|
---|
150 | printf("Error creating label.\n");
|
---|
151 | return rc;
|
---|
152 | }
|
---|
153 |
|
---|
154 | rect.p0.x = 60;
|
---|
155 | rect.p0.y = 37;
|
---|
156 | rect.p1.x = 160;
|
---|
157 | rect.p1.y = 50;
|
---|
158 | ui_label_set_rect(demo.label, &rect);
|
---|
159 | ui_label_set_halign(demo.label, gfx_halign_center);
|
---|
160 |
|
---|
161 | rc = ui_fixed_add(demo.fixed, ui_label_ctl(demo.label));
|
---|
162 | if (rc != EOK) {
|
---|
163 | printf("Error adding control to layout.\n");
|
---|
164 | return rc;
|
---|
165 | }
|
---|
166 |
|
---|
167 | rc = ui_pbutton_create(ui_res, "OK", &demo.pb1);
|
---|
168 | if (rc != EOK) {
|
---|
169 | printf("Error creating button.\n");
|
---|
170 | return rc;
|
---|
171 | }
|
---|
172 |
|
---|
173 | ui_pbutton_set_cb(demo.pb1, &pbutton_cb, (void *) &demo);
|
---|
174 |
|
---|
175 | rect.p0.x = 15;
|
---|
176 | rect.p0.y = 70;
|
---|
177 | rect.p1.x = 105;
|
---|
178 | rect.p1.y = 98;
|
---|
179 | ui_pbutton_set_rect(demo.pb1, &rect);
|
---|
180 |
|
---|
181 | ui_pbutton_set_default(demo.pb1, true);
|
---|
182 |
|
---|
183 | rc = ui_fixed_add(demo.fixed, ui_pbutton_ctl(demo.pb1));
|
---|
184 | if (rc != EOK) {
|
---|
185 | printf("Error adding control to layout.\n");
|
---|
186 | return rc;
|
---|
187 | }
|
---|
188 |
|
---|
189 | rc = ui_pbutton_create(ui_res, "Cancel", &demo.pb2);
|
---|
190 | if (rc != EOK) {
|
---|
191 | printf("Error creating button.\n");
|
---|
192 | return rc;
|
---|
193 | }
|
---|
194 |
|
---|
195 | ui_pbutton_set_cb(demo.pb2, &pbutton_cb, (void *) &demo);
|
---|
196 |
|
---|
197 | rect.p0.x = 115;
|
---|
198 | rect.p0.y = 70;
|
---|
199 | rect.p1.x = 205;
|
---|
200 | rect.p1.y = 98;
|
---|
201 | ui_pbutton_set_rect(demo.pb2, &rect);
|
---|
202 |
|
---|
203 | rc = ui_fixed_add(demo.fixed, ui_pbutton_ctl(demo.pb2));
|
---|
204 | if (rc != EOK) {
|
---|
205 | printf("Error adding control to layout.\n");
|
---|
206 | return rc;
|
---|
207 | }
|
---|
208 |
|
---|
209 | rc = ui_entry_create(ui_res, "", &demo.entry);
|
---|
210 | if (rc != EOK) {
|
---|
211 | printf("Error creating entry.\n");
|
---|
212 | return rc;
|
---|
213 | }
|
---|
214 |
|
---|
215 | rect.p0.x = 15;
|
---|
216 | rect.p0.y = 110;
|
---|
217 | rect.p1.x = 205;
|
---|
218 | rect.p1.y = 135;
|
---|
219 | ui_entry_set_rect(demo.entry, &rect);
|
---|
220 | ui_entry_set_halign(demo.entry, gfx_halign_center);
|
---|
221 |
|
---|
222 | rc = ui_fixed_add(demo.fixed, ui_entry_ctl(demo.entry));
|
---|
223 | if (rc != EOK) {
|
---|
224 | printf("Error adding control to layout.\n");
|
---|
225 | return rc;
|
---|
226 | }
|
---|
227 |
|
---|
228 | gfx_bitmap_params_init(&bparams);
|
---|
229 | bparams.rect.p0.x = 0;
|
---|
230 | bparams.rect.p0.y = 0;
|
---|
231 | bparams.rect.p1.x = 188;
|
---|
232 | bparams.rect.p1.y = 24;
|
---|
233 |
|
---|
234 | rc = gfx_bitmap_create(gc, &bparams, NULL, &bitmap);
|
---|
235 | if (rc != EOK)
|
---|
236 | return rc;
|
---|
237 |
|
---|
238 | rc = bitmap_moire(bitmap, bparams.rect.p1.x, bparams.rect.p1.y);
|
---|
239 | if (rc != EOK)
|
---|
240 | return rc;
|
---|
241 |
|
---|
242 | rc = ui_image_create(ui_res, bitmap, ¶ms.rect, &demo.image);
|
---|
243 | if (rc != EOK) {
|
---|
244 | printf("Error creating label.\n");
|
---|
245 | return rc;
|
---|
246 | }
|
---|
247 |
|
---|
248 | off.x = 15;
|
---|
249 | off.y = 145;
|
---|
250 | gfx_rect_translate(&off, &bparams.rect, &rect);
|
---|
251 |
|
---|
252 | /* Adjust for frame width (2 x 1 pixel) */
|
---|
253 | rect.p1.x += 2;
|
---|
254 | rect.p1.y += 2;
|
---|
255 | ui_image_set_rect(demo.image, &rect);
|
---|
256 | ui_image_set_flags(demo.image, ui_imgf_frame);
|
---|
257 |
|
---|
258 | rc = ui_fixed_add(demo.fixed, ui_image_ctl(demo.image));
|
---|
259 | if (rc != EOK) {
|
---|
260 | printf("Error adding control to layout.\n");
|
---|
261 | return rc;
|
---|
262 | }
|
---|
263 |
|
---|
264 | ui_window_add(window, ui_fixed_ctl(demo.fixed));
|
---|
265 |
|
---|
266 | rc = ui_window_paint(window);
|
---|
267 | if (rc != EOK) {
|
---|
268 | printf("Error painting window.\n");
|
---|
269 | return rc;
|
---|
270 | }
|
---|
271 |
|
---|
272 | ui_run(ui);
|
---|
273 |
|
---|
274 | ui_window_destroy(window);
|
---|
275 | ui_destroy(ui);
|
---|
276 |
|
---|
277 | return EOK;
|
---|
278 | }
|
---|
279 |
|
---|
280 | /** Fill bitmap with moire pattern.
|
---|
281 | *
|
---|
282 | * @param bitmap Bitmap
|
---|
283 | * @param w Bitmap width
|
---|
284 | * @param h Bitmap height
|
---|
285 | * @return EOK on success or an error code
|
---|
286 | */
|
---|
287 | static errno_t bitmap_moire(gfx_bitmap_t *bitmap, gfx_coord_t w, gfx_coord_t h)
|
---|
288 | {
|
---|
289 | int i, j;
|
---|
290 | int k;
|
---|
291 | pixelmap_t pixelmap;
|
---|
292 | gfx_bitmap_alloc_t alloc;
|
---|
293 | errno_t rc;
|
---|
294 |
|
---|
295 | rc = gfx_bitmap_get_alloc(bitmap, &alloc);
|
---|
296 | if (rc != EOK)
|
---|
297 | return rc;
|
---|
298 |
|
---|
299 | /* In absence of anything else, use pixelmap */
|
---|
300 | pixelmap.width = w;
|
---|
301 | pixelmap.height = h;
|
---|
302 | pixelmap.data = alloc.pixels;
|
---|
303 |
|
---|
304 | for (i = 0; i < w; i++) {
|
---|
305 | for (j = 0; j < h; j++) {
|
---|
306 | k = i * i + j * j;
|
---|
307 | pixelmap_put_pixel(&pixelmap, i, j,
|
---|
308 | PIXEL(255, k, k, 255 - k));
|
---|
309 | }
|
---|
310 | }
|
---|
311 |
|
---|
312 | return EOK;
|
---|
313 | }
|
---|
314 |
|
---|
315 | static void print_syntax(void)
|
---|
316 | {
|
---|
317 | printf("Syntax: uidemo [-d <display-spec>]\n");
|
---|
318 | }
|
---|
319 |
|
---|
320 | int main(int argc, char *argv[])
|
---|
321 | {
|
---|
322 | const char *display_spec = UI_DISPLAY_DEFAULT;
|
---|
323 | errno_t rc;
|
---|
324 | int i;
|
---|
325 |
|
---|
326 | i = 1;
|
---|
327 | while (i < argc && argv[i][0] == '-') {
|
---|
328 | if (str_cmp(argv[i], "-d") == 0) {
|
---|
329 | ++i;
|
---|
330 | if (i >= argc) {
|
---|
331 | printf("Argument missing.\n");
|
---|
332 | print_syntax();
|
---|
333 | return 1;
|
---|
334 | }
|
---|
335 |
|
---|
336 | display_spec = argv[i++];
|
---|
337 | } else {
|
---|
338 | printf("Invalid option '%s'.\n", argv[i]);
|
---|
339 | print_syntax();
|
---|
340 | return 1;
|
---|
341 | }
|
---|
342 | }
|
---|
343 |
|
---|
344 | if (i < argc) {
|
---|
345 | print_syntax();
|
---|
346 | return 1;
|
---|
347 | }
|
---|
348 |
|
---|
349 | rc = ui_demo(display_spec);
|
---|
350 | if (rc != EOK)
|
---|
351 | return 1;
|
---|
352 |
|
---|
353 | return 0;
|
---|
354 | }
|
---|
355 |
|
---|
356 | /** @}
|
---|
357 | */
|
---|