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