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