[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 |
|
---|
[c9a7adc] | 35 | #include <gfx/color.h>
|
---|
| 36 | #include <gfx/coord.h>
|
---|
| 37 | #include <gfx/render.h>
|
---|
[d284ce9] | 38 | #include <io/pos_event.h>
|
---|
[f80690a] | 39 | #include <stdio.h>
|
---|
| 40 | #include <str.h>
|
---|
[47728678] | 41 | #include <task.h>
|
---|
[8009dc27] | 42 | #include <ui/fixed.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>
|
---|
[1769693] | 47 | #include <ui/wdecor.h>
|
---|
[d284ce9] | 48 | #include <ui/window.h>
|
---|
[f6df5a3] | 49 | #include "uidemo.h"
|
---|
[47728678] | 50 |
|
---|
[d284ce9] | 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
|
---|
[47728678] | 57 | };
|
---|
| 58 |
|
---|
[8ef48ece] | 59 | static void pb_clicked(ui_pbutton_t *, void *);
|
---|
| 60 |
|
---|
| 61 | static ui_pbutton_cb_t pbutton_cb = {
|
---|
| 62 | .clicked = pb_clicked
|
---|
| 63 | };
|
---|
| 64 |
|
---|
[d284ce9] | 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)
|
---|
[47728678] | 71 | {
|
---|
[20d2c6c] | 72 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
| 73 |
|
---|
[d284ce9] | 74 | ui_quit(demo->ui);
|
---|
[47728678] | 75 | }
|
---|
| 76 |
|
---|
[d284ce9] | 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)
|
---|
[f6df5a3] | 83 | {
|
---|
| 84 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
| 85 |
|
---|
[1769693] | 86 | /* Make sure we don't process events until fully initialized */
|
---|
[8009dc27] | 87 | if (demo->fixed == NULL)
|
---|
[1769693] | 88 | return;
|
---|
| 89 |
|
---|
[8009dc27] | 90 | ui_fixed_pos_event(demo->fixed, event);
|
---|
[f6df5a3] | 91 | }
|
---|
| 92 |
|
---|
[8ef48ece] | 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;
|
---|
[ba09d06] | 101 | errno_t rc;
|
---|
[8ef48ece] | 102 |
|
---|
| 103 | if (pbutton == demo->pb1) {
|
---|
[ba09d06] | 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);
|
---|
[8ef48ece] | 108 | } else {
|
---|
[ba09d06] | 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);
|
---|
[8ef48ece] | 113 | }
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[47728678] | 116 | /** Run UI demo on display server. */
|
---|
[d284ce9] | 117 | static errno_t ui_demo(const char *display_spec)
|
---|
[47728678] | 118 | {
|
---|
[d284ce9] | 119 | ui_t *ui = NULL;
|
---|
| 120 | ui_wnd_params_t params;
|
---|
| 121 | ui_window_t *window = NULL;
|
---|
[f6df5a3] | 122 | ui_demo_t demo;
|
---|
[47728678] | 123 | gfx_rect_t rect;
|
---|
[d284ce9] | 124 | gfx_rect_t app_rect;
|
---|
| 125 | gfx_color_t *color;
|
---|
| 126 | ui_resource_t *ui_res;
|
---|
| 127 | gfx_context_t *gc;
|
---|
[47728678] | 128 | errno_t rc;
|
---|
| 129 |
|
---|
[d284ce9] | 130 | rc = ui_create(display_spec, &ui);
|
---|
[47728678] | 131 | if (rc != EOK) {
|
---|
[d284ce9] | 132 | printf("Error creating UI on display %s.\n", display_spec);
|
---|
[47728678] | 133 | return rc;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[d284ce9] | 136 | ui_wnd_params_init(¶ms);
|
---|
| 137 | params.caption = "UI Demo";
|
---|
[47728678] | 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 |
|
---|
[1769693] | 143 | memset((void *) &demo, 0, sizeof(demo));
|
---|
[d284ce9] | 144 | demo.ui = ui;
|
---|
[1769693] | 145 |
|
---|
[d284ce9] | 146 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
[47728678] | 147 | if (rc != EOK) {
|
---|
| 148 | printf("Error creating window.\n");
|
---|
| 149 | return rc;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[d284ce9] | 152 | ui_window_set_cb(window, &window_cb, (void *) &demo);
|
---|
| 153 | demo.window = window;
|
---|
[47728678] | 154 |
|
---|
| 155 | task_retval(0);
|
---|
| 156 |
|
---|
[d284ce9] | 157 | ui_res = ui_window_get_res(window);
|
---|
| 158 | gc = ui_window_get_gc(window);
|
---|
| 159 | ui_window_get_app_rect(window, &app_rect);
|
---|
[1769693] | 160 |
|
---|
[8009dc27] | 161 | rc = ui_fixed_create(&demo.fixed);
|
---|
| 162 | if (rc != EOK) {
|
---|
| 163 | printf("Error creating fixed layout.\n");
|
---|
| 164 | return rc;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[ba09d06] | 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);
|
---|
[58a67050] | 178 | ui_label_set_halign(demo.label, gfx_halign_center);
|
---|
[ba09d06] | 179 |
|
---|
[8009dc27] | 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 |
|
---|
[f6df5a3] | 186 | rc = ui_pbutton_create(ui_res, "Confirm", &demo.pb1);
|
---|
[47728678] | 187 | if (rc != EOK) {
|
---|
| 188 | printf("Error creating button.\n");
|
---|
[f6df5a3] | 189 | return rc;
|
---|
[47728678] | 190 | }
|
---|
| 191 |
|
---|
[8ef48ece] | 192 | ui_pbutton_set_cb(demo.pb1, &pbutton_cb, (void *) &demo);
|
---|
| 193 |
|
---|
[ba09d06] | 194 | rect.p0.x = 15;
|
---|
| 195 | rect.p0.y = 60;
|
---|
| 196 | rect.p1.x = 105;
|
---|
| 197 | rect.p1.y = 88;
|
---|
[f6df5a3] | 198 | ui_pbutton_set_rect(demo.pb1, &rect);
|
---|
[47728678] | 199 |
|
---|
[c9a7adc] | 200 | ui_pbutton_set_default(demo.pb1, true);
|
---|
| 201 |
|
---|
[8009dc27] | 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 |
|
---|
[f6df5a3] | 208 | rc = ui_pbutton_create(ui_res, "Cancel", &demo.pb2);
|
---|
[47728678] | 209 | if (rc != EOK) {
|
---|
| 210 | printf("Error creating button.\n");
|
---|
[f6df5a3] | 211 | return rc;
|
---|
[47728678] | 212 | }
|
---|
| 213 |
|
---|
[8ef48ece] | 214 | ui_pbutton_set_cb(demo.pb2, &pbutton_cb, (void *) &demo);
|
---|
| 215 |
|
---|
[ba09d06] | 216 | rect.p0.x = 115;
|
---|
| 217 | rect.p0.y = 60;
|
---|
| 218 | rect.p1.x = 205;
|
---|
| 219 | rect.p1.y = 88;
|
---|
[f6df5a3] | 220 | ui_pbutton_set_rect(demo.pb2, &rect);
|
---|
[47728678] | 221 |
|
---|
[8009dc27] | 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 |
|
---|
[c9a7adc] | 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 |
|
---|
[d284ce9] | 240 | rc = gfx_fill_rect(gc, &app_rect);
|
---|
[c9a7adc] | 241 | if (rc != EOK) {
|
---|
| 242 | printf("Error filling background.\n");
|
---|
| 243 | return rc;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
[ba09d06] | 246 | rc = ui_label_paint(demo.label);
|
---|
| 247 | if (rc != EOK) {
|
---|
| 248 | printf("Error painting button.\n");
|
---|
| 249 | return rc;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
[f6df5a3] | 252 | rc = ui_pbutton_paint(demo.pb1);
|
---|
[47728678] | 253 | if (rc != EOK) {
|
---|
| 254 | printf("Error painting button.\n");
|
---|
[f6df5a3] | 255 | return rc;
|
---|
[47728678] | 256 | }
|
---|
| 257 |
|
---|
[f6df5a3] | 258 | rc = ui_pbutton_paint(demo.pb2);
|
---|
[47728678] | 259 | if (rc != EOK) {
|
---|
| 260 | printf("Error painting button.\n");
|
---|
[f6df5a3] | 261 | return rc;
|
---|
[47728678] | 262 | }
|
---|
| 263 |
|
---|
[d284ce9] | 264 | ui_run(ui);
|
---|
[47728678] | 265 |
|
---|
[8009dc27] | 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 |
|
---|
[f6df5a3] | 270 | ui_pbutton_destroy(demo.pb1);
|
---|
| 271 | ui_pbutton_destroy(demo.pb2);
|
---|
[47728678] | 272 |
|
---|
[d284ce9] | 273 | ui_window_destroy(window);
|
---|
| 274 | ui_destroy(ui);
|
---|
[47728678] | 275 |
|
---|
| 276 | return EOK;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[d284ce9] | 279 | static void print_syntax(void)
|
---|
| 280 | {
|
---|
| 281 | printf("Syntax: uidemo [-d <display-spec>]\n");
|
---|
| 282 | }
|
---|
| 283 |
|
---|
[f80690a] | 284 | int main(int argc, char *argv[])
|
---|
| 285 | {
|
---|
[d284ce9] | 286 | const char *display_spec = UI_DISPLAY_DEFAULT;
|
---|
[f80690a] | 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 |
|
---|
[d284ce9] | 300 | display_spec = argv[i++];
|
---|
[f80690a] | 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 |
|
---|
[d284ce9] | 313 | rc = ui_demo(display_spec);
|
---|
[47728678] | 314 | if (rc != EOK)
|
---|
[f80690a] | 315 | return 1;
|
---|
| 316 |
|
---|
| 317 | return 0;
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 | /** @}
|
---|
| 321 | */
|
---|