[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>
|
---|
[ba09d06] | 42 | #include <ui/label.h>
|
---|
[f80690a] | 43 | #include <ui/pbutton.h>
|
---|
[47728678] | 44 | #include <ui/resource.h>
|
---|
[d284ce9] | 45 | #include <ui/ui.h>
|
---|
[1769693] | 46 | #include <ui/wdecor.h>
|
---|
[d284ce9] | 47 | #include <ui/window.h>
|
---|
[f6df5a3] | 48 | #include "uidemo.h"
|
---|
[47728678] | 49 |
|
---|
[d284ce9] | 50 | static void wnd_close(ui_window_t *, void *);
|
---|
| 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,
|
---|
| 55 | .pos = wnd_pos
|
---|
[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 |
|
---|
[d284ce9] | 76 | /** Window position event.
|
---|
| 77 | *
|
---|
| 78 | * @param window Window
|
---|
| 79 | * @param arg Argument (demo)
|
---|
| 80 | */
|
---|
| 81 | static void wnd_pos(ui_window_t *window, void *arg, pos_event_t *event)
|
---|
[f6df5a3] | 82 | {
|
---|
| 83 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
| 84 |
|
---|
[1769693] | 85 | /* Make sure we don't process events until fully initialized */
|
---|
[d284ce9] | 86 | if (demo->pb1 == NULL || demo->pb2 == NULL)
|
---|
[1769693] | 87 | return;
|
---|
| 88 |
|
---|
[faca61b8] | 89 | ui_pbutton_pos_event(demo->pb1, event);
|
---|
| 90 | ui_pbutton_pos_event(demo->pb2, 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 |
|
---|
[ba09d06] | 161 | rc = ui_label_create(ui_res, "Hello there!", &demo.label);
|
---|
| 162 | if (rc != EOK) {
|
---|
| 163 | printf("Error creating label.\n");
|
---|
| 164 | return rc;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | rect.p0.x = 60;
|
---|
| 168 | rect.p0.y = 37;
|
---|
| 169 | rect.p1.x = 160;
|
---|
| 170 | rect.p1.y = 50;
|
---|
| 171 | ui_label_set_rect(demo.label, &rect);
|
---|
[58a67050] | 172 | ui_label_set_halign(demo.label, gfx_halign_center);
|
---|
[ba09d06] | 173 |
|
---|
[f6df5a3] | 174 | rc = ui_pbutton_create(ui_res, "Confirm", &demo.pb1);
|
---|
[47728678] | 175 | if (rc != EOK) {
|
---|
| 176 | printf("Error creating button.\n");
|
---|
[f6df5a3] | 177 | return rc;
|
---|
[47728678] | 178 | }
|
---|
| 179 |
|
---|
[8ef48ece] | 180 | ui_pbutton_set_cb(demo.pb1, &pbutton_cb, (void *) &demo);
|
---|
| 181 |
|
---|
[ba09d06] | 182 | rect.p0.x = 15;
|
---|
| 183 | rect.p0.y = 60;
|
---|
| 184 | rect.p1.x = 105;
|
---|
| 185 | rect.p1.y = 88;
|
---|
[f6df5a3] | 186 | ui_pbutton_set_rect(demo.pb1, &rect);
|
---|
[47728678] | 187 |
|
---|
[c9a7adc] | 188 | ui_pbutton_set_default(demo.pb1, true);
|
---|
| 189 |
|
---|
[f6df5a3] | 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;
|
---|
| 199 | rect.p0.y = 60;
|
---|
| 200 | rect.p1.x = 205;
|
---|
| 201 | rect.p1.y = 88;
|
---|
[f6df5a3] | 202 | ui_pbutton_set_rect(demo.pb2, &rect);
|
---|
[47728678] | 203 |
|
---|
[c9a7adc] | 204 | rc = gfx_color_new_rgb_i16(0xc8c8, 0xc8c8, 0xc8c8, &color);
|
---|
| 205 | if (rc != EOK) {
|
---|
| 206 | printf("Error allocating color.\n");
|
---|
| 207 | return rc;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | rc = gfx_set_color(gc, color);
|
---|
| 211 | if (rc != EOK) {
|
---|
| 212 | printf("Error setting color.\n");
|
---|
| 213 | return rc;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[d284ce9] | 216 | rc = gfx_fill_rect(gc, &app_rect);
|
---|
[c9a7adc] | 217 | if (rc != EOK) {
|
---|
| 218 | printf("Error filling background.\n");
|
---|
| 219 | return rc;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
[ba09d06] | 222 | rc = ui_label_paint(demo.label);
|
---|
| 223 | if (rc != EOK) {
|
---|
| 224 | printf("Error painting button.\n");
|
---|
| 225 | return rc;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
[f6df5a3] | 228 | rc = ui_pbutton_paint(demo.pb1);
|
---|
[47728678] | 229 | if (rc != EOK) {
|
---|
| 230 | printf("Error painting button.\n");
|
---|
[f6df5a3] | 231 | return rc;
|
---|
[47728678] | 232 | }
|
---|
| 233 |
|
---|
[f6df5a3] | 234 | rc = ui_pbutton_paint(demo.pb2);
|
---|
[47728678] | 235 | if (rc != EOK) {
|
---|
| 236 | printf("Error painting button.\n");
|
---|
[f6df5a3] | 237 | return rc;
|
---|
[47728678] | 238 | }
|
---|
| 239 |
|
---|
[d284ce9] | 240 | ui_run(ui);
|
---|
[47728678] | 241 |
|
---|
[f6df5a3] | 242 | ui_pbutton_destroy(demo.pb1);
|
---|
| 243 | ui_pbutton_destroy(demo.pb2);
|
---|
[47728678] | 244 |
|
---|
[d284ce9] | 245 | ui_window_destroy(window);
|
---|
| 246 | ui_destroy(ui);
|
---|
[47728678] | 247 |
|
---|
| 248 | return EOK;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[d284ce9] | 251 | static void print_syntax(void)
|
---|
| 252 | {
|
---|
| 253 | printf("Syntax: uidemo [-d <display-spec>]\n");
|
---|
| 254 | }
|
---|
| 255 |
|
---|
[f80690a] | 256 | int main(int argc, char *argv[])
|
---|
| 257 | {
|
---|
[d284ce9] | 258 | const char *display_spec = UI_DISPLAY_DEFAULT;
|
---|
[f80690a] | 259 | errno_t rc;
|
---|
| 260 | int i;
|
---|
| 261 |
|
---|
| 262 | i = 1;
|
---|
| 263 | while (i < argc && argv[i][0] == '-') {
|
---|
| 264 | if (str_cmp(argv[i], "-d") == 0) {
|
---|
| 265 | ++i;
|
---|
| 266 | if (i >= argc) {
|
---|
| 267 | printf("Argument missing.\n");
|
---|
| 268 | print_syntax();
|
---|
| 269 | return 1;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
[d284ce9] | 272 | display_spec = argv[i++];
|
---|
[f80690a] | 273 | } else {
|
---|
| 274 | printf("Invalid option '%s'.\n", argv[i]);
|
---|
| 275 | print_syntax();
|
---|
| 276 | return 1;
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | if (i < argc) {
|
---|
| 281 | print_syntax();
|
---|
| 282 | return 1;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
[d284ce9] | 285 | rc = ui_demo(display_spec);
|
---|
[47728678] | 286 | if (rc != EOK)
|
---|
[f80690a] | 287 | return 1;
|
---|
| 288 |
|
---|
| 289 | return 0;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | /** @}
|
---|
| 293 | */
|
---|