[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>
|
---|
[f80690a] | 36 | #include <stdio.h>
|
---|
| 37 | #include <str.h>
|
---|
[8009dc27] | 38 | #include <ui/fixed.h>
|
---|
[ba09d06] | 39 | #include <ui/label.h>
|
---|
[f80690a] | 40 | #include <ui/pbutton.h>
|
---|
[47728678] | 41 | #include <ui/resource.h>
|
---|
[d284ce9] | 42 | #include <ui/ui.h>
|
---|
| 43 | #include <ui/window.h>
|
---|
[f6df5a3] | 44 | #include "uidemo.h"
|
---|
[47728678] | 45 |
|
---|
[d284ce9] | 46 | static void wnd_close(ui_window_t *, void *);
|
---|
| 47 |
|
---|
| 48 | static ui_window_cb_t window_cb = {
|
---|
[b71c0fc] | 49 | .close = wnd_close
|
---|
[47728678] | 50 | };
|
---|
| 51 |
|
---|
[8ef48ece] | 52 | static void pb_clicked(ui_pbutton_t *, void *);
|
---|
| 53 |
|
---|
| 54 | static ui_pbutton_cb_t pbutton_cb = {
|
---|
| 55 | .clicked = pb_clicked
|
---|
| 56 | };
|
---|
| 57 |
|
---|
[d284ce9] | 58 | /** Window close button was clicked.
|
---|
| 59 | *
|
---|
| 60 | * @param window Window
|
---|
| 61 | * @param arg Argument (demo)
|
---|
| 62 | */
|
---|
| 63 | static void wnd_close(ui_window_t *window, void *arg)
|
---|
[47728678] | 64 | {
|
---|
[20d2c6c] | 65 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
| 66 |
|
---|
[d284ce9] | 67 | ui_quit(demo->ui);
|
---|
[47728678] | 68 | }
|
---|
| 69 |
|
---|
[8ef48ece] | 70 | /** Push button was clicked.
|
---|
| 71 | *
|
---|
| 72 | * @param pbutton Push button
|
---|
| 73 | * @param arg Argument (demo)
|
---|
| 74 | */
|
---|
| 75 | static void pb_clicked(ui_pbutton_t *pbutton, void *arg)
|
---|
| 76 | {
|
---|
| 77 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
[ba09d06] | 78 | errno_t rc;
|
---|
[8ef48ece] | 79 |
|
---|
| 80 | if (pbutton == demo->pb1) {
|
---|
[ba09d06] | 81 | rc = ui_label_set_text(demo->label, "Confirmed");
|
---|
| 82 | if (rc != EOK)
|
---|
| 83 | printf("Error changing label text.\n");
|
---|
| 84 | (void) ui_label_paint(demo->label);
|
---|
[8ef48ece] | 85 | } else {
|
---|
[ba09d06] | 86 | rc = ui_label_set_text(demo->label, "Cancelled");
|
---|
| 87 | if (rc != EOK)
|
---|
| 88 | printf("Error changing label text.\n");
|
---|
| 89 | (void) ui_label_paint(demo->label);
|
---|
[8ef48ece] | 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[47728678] | 93 | /** Run UI demo on display server. */
|
---|
[d284ce9] | 94 | static errno_t ui_demo(const char *display_spec)
|
---|
[47728678] | 95 | {
|
---|
[d284ce9] | 96 | ui_t *ui = NULL;
|
---|
| 97 | ui_wnd_params_t params;
|
---|
| 98 | ui_window_t *window = NULL;
|
---|
[f6df5a3] | 99 | ui_demo_t demo;
|
---|
[47728678] | 100 | gfx_rect_t rect;
|
---|
[d284ce9] | 101 | ui_resource_t *ui_res;
|
---|
[47728678] | 102 | errno_t rc;
|
---|
| 103 |
|
---|
[d284ce9] | 104 | rc = ui_create(display_spec, &ui);
|
---|
[47728678] | 105 | if (rc != EOK) {
|
---|
[d284ce9] | 106 | printf("Error creating UI on display %s.\n", display_spec);
|
---|
[47728678] | 107 | return rc;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[d284ce9] | 110 | ui_wnd_params_init(¶ms);
|
---|
| 111 | params.caption = "UI Demo";
|
---|
[47728678] | 112 | params.rect.p0.x = 0;
|
---|
| 113 | params.rect.p0.y = 0;
|
---|
| 114 | params.rect.p1.x = 220;
|
---|
| 115 | params.rect.p1.y = 100;
|
---|
| 116 |
|
---|
[1769693] | 117 | memset((void *) &demo, 0, sizeof(demo));
|
---|
[d284ce9] | 118 | demo.ui = ui;
|
---|
[1769693] | 119 |
|
---|
[d284ce9] | 120 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
[47728678] | 121 | if (rc != EOK) {
|
---|
| 122 | printf("Error creating window.\n");
|
---|
| 123 | return rc;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[d284ce9] | 126 | ui_window_set_cb(window, &window_cb, (void *) &demo);
|
---|
| 127 | demo.window = window;
|
---|
[47728678] | 128 |
|
---|
[d284ce9] | 129 | ui_res = ui_window_get_res(window);
|
---|
[1769693] | 130 |
|
---|
[8009dc27] | 131 | rc = ui_fixed_create(&demo.fixed);
|
---|
| 132 | if (rc != EOK) {
|
---|
| 133 | printf("Error creating fixed layout.\n");
|
---|
| 134 | return rc;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[ba09d06] | 137 | rc = ui_label_create(ui_res, "Hello there!", &demo.label);
|
---|
| 138 | if (rc != EOK) {
|
---|
| 139 | printf("Error creating label.\n");
|
---|
| 140 | return rc;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | rect.p0.x = 60;
|
---|
| 144 | rect.p0.y = 37;
|
---|
| 145 | rect.p1.x = 160;
|
---|
| 146 | rect.p1.y = 50;
|
---|
| 147 | ui_label_set_rect(demo.label, &rect);
|
---|
[58a67050] | 148 | ui_label_set_halign(demo.label, gfx_halign_center);
|
---|
[ba09d06] | 149 |
|
---|
[8009dc27] | 150 | rc = ui_fixed_add(demo.fixed, ui_label_ctl(demo.label));
|
---|
| 151 | if (rc != EOK) {
|
---|
| 152 | printf("Error adding control to layout.\n");
|
---|
| 153 | return rc;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[f6df5a3] | 156 | rc = ui_pbutton_create(ui_res, "Confirm", &demo.pb1);
|
---|
[47728678] | 157 | if (rc != EOK) {
|
---|
| 158 | printf("Error creating button.\n");
|
---|
[f6df5a3] | 159 | return rc;
|
---|
[47728678] | 160 | }
|
---|
| 161 |
|
---|
[8ef48ece] | 162 | ui_pbutton_set_cb(demo.pb1, &pbutton_cb, (void *) &demo);
|
---|
| 163 |
|
---|
[ba09d06] | 164 | rect.p0.x = 15;
|
---|
| 165 | rect.p0.y = 60;
|
---|
| 166 | rect.p1.x = 105;
|
---|
| 167 | rect.p1.y = 88;
|
---|
[f6df5a3] | 168 | ui_pbutton_set_rect(demo.pb1, &rect);
|
---|
[47728678] | 169 |
|
---|
[c9a7adc] | 170 | ui_pbutton_set_default(demo.pb1, true);
|
---|
| 171 |
|
---|
[8009dc27] | 172 | rc = ui_fixed_add(demo.fixed, ui_pbutton_ctl(demo.pb1));
|
---|
| 173 | if (rc != EOK) {
|
---|
| 174 | printf("Error adding control to layout.\n");
|
---|
| 175 | return rc;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
[f6df5a3] | 178 | rc = ui_pbutton_create(ui_res, "Cancel", &demo.pb2);
|
---|
[47728678] | 179 | if (rc != EOK) {
|
---|
| 180 | printf("Error creating button.\n");
|
---|
[f6df5a3] | 181 | return rc;
|
---|
[47728678] | 182 | }
|
---|
| 183 |
|
---|
[8ef48ece] | 184 | ui_pbutton_set_cb(demo.pb2, &pbutton_cb, (void *) &demo);
|
---|
| 185 |
|
---|
[ba09d06] | 186 | rect.p0.x = 115;
|
---|
| 187 | rect.p0.y = 60;
|
---|
| 188 | rect.p1.x = 205;
|
---|
| 189 | rect.p1.y = 88;
|
---|
[f6df5a3] | 190 | ui_pbutton_set_rect(demo.pb2, &rect);
|
---|
[47728678] | 191 |
|
---|
[8009dc27] | 192 | rc = ui_fixed_add(demo.fixed, ui_pbutton_ctl(demo.pb2));
|
---|
| 193 | if (rc != EOK) {
|
---|
| 194 | printf("Error adding control to layout.\n");
|
---|
| 195 | return rc;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[b71c0fc] | 198 | ui_window_add(window, ui_fixed_ctl(demo.fixed));
|
---|
| 199 |
|
---|
[fa01c05] | 200 | rc = ui_window_paint(window);
|
---|
[ba09d06] | 201 | if (rc != EOK) {
|
---|
[fa01c05] | 202 | printf("Error painting window.\n");
|
---|
[f6df5a3] | 203 | return rc;
|
---|
[47728678] | 204 | }
|
---|
| 205 |
|
---|
[d284ce9] | 206 | ui_run(ui);
|
---|
[47728678] | 207 |
|
---|
[d284ce9] | 208 | ui_window_destroy(window);
|
---|
| 209 | ui_destroy(ui);
|
---|
[47728678] | 210 |
|
---|
| 211 | return EOK;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[d284ce9] | 214 | static void print_syntax(void)
|
---|
| 215 | {
|
---|
| 216 | printf("Syntax: uidemo [-d <display-spec>]\n");
|
---|
| 217 | }
|
---|
| 218 |
|
---|
[f80690a] | 219 | int main(int argc, char *argv[])
|
---|
| 220 | {
|
---|
[d284ce9] | 221 | const char *display_spec = UI_DISPLAY_DEFAULT;
|
---|
[f80690a] | 222 | errno_t rc;
|
---|
| 223 | int i;
|
---|
| 224 |
|
---|
| 225 | i = 1;
|
---|
| 226 | while (i < argc && argv[i][0] == '-') {
|
---|
| 227 | if (str_cmp(argv[i], "-d") == 0) {
|
---|
| 228 | ++i;
|
---|
| 229 | if (i >= argc) {
|
---|
| 230 | printf("Argument missing.\n");
|
---|
| 231 | print_syntax();
|
---|
| 232 | return 1;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
[d284ce9] | 235 | display_spec = argv[i++];
|
---|
[f80690a] | 236 | } else {
|
---|
| 237 | printf("Invalid option '%s'.\n", argv[i]);
|
---|
| 238 | print_syntax();
|
---|
| 239 | return 1;
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | if (i < argc) {
|
---|
| 244 | print_syntax();
|
---|
| 245 | return 1;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
[d284ce9] | 248 | rc = ui_demo(display_spec);
|
---|
[47728678] | 249 | if (rc != EOK)
|
---|
[f80690a] | 250 | return 1;
|
---|
| 251 |
|
---|
| 252 | return 0;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | /** @}
|
---|
| 256 | */
|
---|