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