[f80690a] | 1 | /*
|
---|
[c88d7f99] | 2 | * Copyright (c) 2022 Jiri Svoboda
|
---|
[f80690a] | 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>
|
---|
[ef734b7] | 39 | #include <stdlib.h>
|
---|
[f80690a] | 40 | #include <str.h>
|
---|
[d8ddf7a] | 41 | #include <ui/entry.h>
|
---|
[5e109e1] | 42 | #include <ui/filedialog.h>
|
---|
[8009dc27] | 43 | #include <ui/fixed.h>
|
---|
[d8ddf7a] | 44 | #include <ui/image.h>
|
---|
[ba09d06] | 45 | #include <ui/label.h>
|
---|
[214aefb] | 46 | #include <ui/menubar.h>
|
---|
| 47 | #include <ui/menuentry.h>
|
---|
| 48 | #include <ui/menu.h>
|
---|
[252d03c] | 49 | #include <ui/msgdialog.h>
|
---|
[f80690a] | 50 | #include <ui/pbutton.h>
|
---|
[80d4aea] | 51 | #include <ui/promptdialog.h>
|
---|
[47728678] | 52 | #include <ui/resource.h>
|
---|
[d284ce9] | 53 | #include <ui/ui.h>
|
---|
| 54 | #include <ui/window.h>
|
---|
[f6df5a3] | 55 | #include "uidemo.h"
|
---|
[47728678] | 56 |
|
---|
[d8ddf7a] | 57 | static errno_t bitmap_moire(gfx_bitmap_t *, gfx_coord_t, gfx_coord_t);
|
---|
| 58 |
|
---|
[d284ce9] | 59 | static void wnd_close(ui_window_t *, void *);
|
---|
| 60 |
|
---|
| 61 | static ui_window_cb_t window_cb = {
|
---|
[b71c0fc] | 62 | .close = wnd_close
|
---|
[47728678] | 63 | };
|
---|
| 64 |
|
---|
[8ef48ece] | 65 | static void pb_clicked(ui_pbutton_t *, void *);
|
---|
| 66 |
|
---|
| 67 | static ui_pbutton_cb_t pbutton_cb = {
|
---|
| 68 | .clicked = pb_clicked
|
---|
| 69 | };
|
---|
| 70 |
|
---|
[d70dc1c4] | 71 | static void checkbox_switched(ui_checkbox_t *, void *, bool);
|
---|
| 72 |
|
---|
| 73 | static ui_checkbox_cb_t checkbox_cb = {
|
---|
| 74 | .switched = checkbox_switched
|
---|
| 75 | };
|
---|
| 76 |
|
---|
[7020d1f] | 77 | static void rb_selected(ui_rbutton_group_t *, void *, void *);
|
---|
| 78 |
|
---|
| 79 | static ui_rbutton_group_cb_t rbutton_group_cb = {
|
---|
| 80 | .selected = rb_selected
|
---|
| 81 | };
|
---|
| 82 |
|
---|
[ef734b7] | 83 | static void slider_moved(ui_slider_t *, void *, gfx_coord_t);
|
---|
| 84 |
|
---|
| 85 | static ui_slider_cb_t slider_cb = {
|
---|
| 86 | .moved = slider_moved
|
---|
| 87 | };
|
---|
| 88 |
|
---|
[bd16113] | 89 | static void scrollbar_up(ui_scrollbar_t *, void *);
|
---|
| 90 | static void scrollbar_down(ui_scrollbar_t *, void *);
|
---|
| 91 | static void scrollbar_moved(ui_scrollbar_t *, void *, gfx_coord_t);
|
---|
| 92 |
|
---|
| 93 | static ui_scrollbar_cb_t scrollbar_cb = {
|
---|
| 94 | .up = scrollbar_up,
|
---|
| 95 | .down = scrollbar_down,
|
---|
| 96 | .moved = scrollbar_moved
|
---|
| 97 | };
|
---|
| 98 |
|
---|
[5e109e1] | 99 | static void uidemo_file_load(ui_menu_entry_t *, void *);
|
---|
[252d03c] | 100 | static void uidemo_file_message(ui_menu_entry_t *, void *);
|
---|
[214aefb] | 101 | static void uidemo_file_exit(ui_menu_entry_t *, void *);
|
---|
[80d4aea] | 102 | static void uidemo_edit_modify(ui_menu_entry_t *, void *);
|
---|
[214aefb] | 103 |
|
---|
[5e109e1] | 104 | static void file_dialog_bok(ui_file_dialog_t *, void *, const char *);
|
---|
| 105 | static void file_dialog_bcancel(ui_file_dialog_t *, void *);
|
---|
| 106 | static void file_dialog_close(ui_file_dialog_t *, void *);
|
---|
| 107 |
|
---|
| 108 | static ui_file_dialog_cb_t file_dialog_cb = {
|
---|
| 109 | .bok = file_dialog_bok,
|
---|
| 110 | .bcancel = file_dialog_bcancel,
|
---|
| 111 | .close = file_dialog_close
|
---|
| 112 | };
|
---|
| 113 |
|
---|
[80d4aea] | 114 | static void prompt_dialog_bok(ui_prompt_dialog_t *, void *, const char *);
|
---|
| 115 | static void prompt_dialog_bcancel(ui_prompt_dialog_t *, void *);
|
---|
| 116 | static void prompt_dialog_close(ui_prompt_dialog_t *, void *);
|
---|
| 117 |
|
---|
| 118 | static ui_prompt_dialog_cb_t prompt_dialog_cb = {
|
---|
| 119 | .bok = prompt_dialog_bok,
|
---|
| 120 | .bcancel = prompt_dialog_bcancel,
|
---|
| 121 | .close = prompt_dialog_close
|
---|
| 122 | };
|
---|
| 123 |
|
---|
[252d03c] | 124 | static void msg_dialog_button(ui_msg_dialog_t *, void *, unsigned);
|
---|
| 125 | static void msg_dialog_close(ui_msg_dialog_t *, void *);
|
---|
| 126 |
|
---|
| 127 | static ui_msg_dialog_cb_t msg_dialog_cb = {
|
---|
| 128 | .button = msg_dialog_button,
|
---|
| 129 | .close = msg_dialog_close
|
---|
| 130 | };
|
---|
| 131 |
|
---|
[5de71df] | 132 | /** Horizontal alignment selected by each radio button */
|
---|
| 133 | static const gfx_halign_t uidemo_halign[3] = {
|
---|
| 134 | gfx_halign_left,
|
---|
| 135 | gfx_halign_center,
|
---|
| 136 | gfx_halign_right
|
---|
| 137 | };
|
---|
| 138 |
|
---|
[d284ce9] | 139 | /** Window close button was clicked.
|
---|
| 140 | *
|
---|
| 141 | * @param window Window
|
---|
| 142 | * @param arg Argument (demo)
|
---|
| 143 | */
|
---|
| 144 | static void wnd_close(ui_window_t *window, void *arg)
|
---|
[47728678] | 145 | {
|
---|
[20d2c6c] | 146 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
| 147 |
|
---|
[d284ce9] | 148 | ui_quit(demo->ui);
|
---|
[47728678] | 149 | }
|
---|
| 150 |
|
---|
[8ef48ece] | 151 | /** Push button was clicked.
|
---|
| 152 | *
|
---|
| 153 | * @param pbutton Push button
|
---|
| 154 | * @param arg Argument (demo)
|
---|
| 155 | */
|
---|
| 156 | static void pb_clicked(ui_pbutton_t *pbutton, void *arg)
|
---|
| 157 | {
|
---|
| 158 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
[ba09d06] | 159 | errno_t rc;
|
---|
[8ef48ece] | 160 |
|
---|
| 161 | if (pbutton == demo->pb1) {
|
---|
[d8ddf7a] | 162 | rc = ui_entry_set_text(demo->entry, "OK pressed");
|
---|
[ba09d06] | 163 | if (rc != EOK)
|
---|
[d8ddf7a] | 164 | printf("Error changing entry text.\n");
|
---|
[8ef48ece] | 165 | } else {
|
---|
[d8ddf7a] | 166 | rc = ui_entry_set_text(demo->entry, "Cancel pressed");
|
---|
[ba09d06] | 167 | if (rc != EOK)
|
---|
[d8ddf7a] | 168 | printf("Error changing entry text.\n");
|
---|
[8ef48ece] | 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[d70dc1c4] | 172 | /** Check box was switched.
|
---|
| 173 | *
|
---|
| 174 | * @param checkbox Check box
|
---|
| 175 | * @param arg Argument (demo)
|
---|
| 176 | */
|
---|
| 177 | static void checkbox_switched(ui_checkbox_t *checkbox, void *arg, bool enable)
|
---|
| 178 | {
|
---|
| 179 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
| 180 |
|
---|
[5de71df] | 181 | ui_entry_set_read_only(demo->entry, enable);
|
---|
[d70dc1c4] | 182 | }
|
---|
| 183 |
|
---|
[7020d1f] | 184 | /** Radio button was selected.
|
---|
| 185 | *
|
---|
| 186 | * @param rbgroup Radio button group
|
---|
| 187 | * @param garg Group argument (demo)
|
---|
| 188 | * @param barg Button argument
|
---|
| 189 | */
|
---|
| 190 | static void rb_selected(ui_rbutton_group_t *rbgroup, void *garg, void *barg)
|
---|
| 191 | {
|
---|
| 192 | ui_demo_t *demo = (ui_demo_t *) garg;
|
---|
[5de71df] | 193 | gfx_halign_t halign = *(gfx_halign_t *) barg;
|
---|
[7020d1f] | 194 |
|
---|
[5de71df] | 195 | ui_entry_set_halign(demo->entry, halign);
|
---|
[7020d1f] | 196 | (void) ui_entry_paint(demo->entry);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[ef734b7] | 199 | /** Slider was moved.
|
---|
| 200 | *
|
---|
| 201 | * @param slider Slider
|
---|
| 202 | * @param arg Argument (demo)
|
---|
| 203 | * @param pos Position
|
---|
| 204 | */
|
---|
| 205 | static void slider_moved(ui_slider_t *slider, void *arg, gfx_coord_t pos)
|
---|
| 206 | {
|
---|
| 207 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
| 208 | char *str;
|
---|
| 209 | errno_t rc;
|
---|
| 210 | int rv;
|
---|
| 211 |
|
---|
| 212 | rv = asprintf(&str, "Slider at %d of %d", (int) pos,
|
---|
| 213 | ui_slider_length(slider));
|
---|
| 214 | if (rv < 0) {
|
---|
| 215 | printf("Out of memory.\n");
|
---|
| 216 | return;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | rc = ui_entry_set_text(demo->entry, str);
|
---|
| 220 | if (rc != EOK)
|
---|
| 221 | printf("Error changing entry text.\n");
|
---|
| 222 | (void) ui_entry_paint(demo->entry);
|
---|
| 223 |
|
---|
| 224 | free(str);
|
---|
| 225 | }
|
---|
| 226 |
|
---|
[bd16113] | 227 | /** Scrollbar up button pressed.
|
---|
| 228 | *
|
---|
| 229 | * @param scrollbar Scrollbar
|
---|
| 230 | * @param arg Argument (demo)
|
---|
| 231 | */
|
---|
| 232 | static void scrollbar_up(ui_scrollbar_t *scrollbar, void *arg)
|
---|
| 233 | {
|
---|
| 234 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
| 235 | gfx_coord_t pos;
|
---|
| 236 | char *str;
|
---|
| 237 | errno_t rc;
|
---|
| 238 | int rv;
|
---|
| 239 |
|
---|
| 240 | pos = ui_scrollbar_get_pos(scrollbar);
|
---|
| 241 | ui_scrollbar_set_pos(scrollbar, pos - 1);
|
---|
| 242 |
|
---|
| 243 | pos = ui_scrollbar_get_pos(scrollbar);
|
---|
| 244 |
|
---|
| 245 | rv = asprintf(&str, "Scrollbar: %d of %d", (int) pos,
|
---|
| 246 | ui_scrollbar_move_length(scrollbar));
|
---|
| 247 | if (rv < 0) {
|
---|
| 248 | printf("Out of memory.\n");
|
---|
| 249 | return;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | rc = ui_entry_set_text(demo->entry, str);
|
---|
| 253 | if (rc != EOK)
|
---|
| 254 | printf("Error changing entry text.\n");
|
---|
| 255 | (void) ui_entry_paint(demo->entry);
|
---|
| 256 |
|
---|
| 257 | free(str);
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | /** Scrollbar down button pressed.
|
---|
| 261 | *
|
---|
| 262 | * @param scrollbar Scrollbar
|
---|
| 263 | * @param arg Argument (demo)
|
---|
| 264 | */
|
---|
| 265 | static void scrollbar_down(ui_scrollbar_t *scrollbar, void *arg)
|
---|
| 266 | {
|
---|
| 267 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
| 268 | gfx_coord_t pos;
|
---|
| 269 | char *str;
|
---|
| 270 | errno_t rc;
|
---|
| 271 | int rv;
|
---|
| 272 |
|
---|
| 273 | pos = ui_scrollbar_get_pos(scrollbar);
|
---|
| 274 | ui_scrollbar_set_pos(scrollbar, pos + 1);
|
---|
| 275 |
|
---|
| 276 | pos = ui_scrollbar_get_pos(scrollbar);
|
---|
| 277 |
|
---|
| 278 | rv = asprintf(&str, "Scrollbar: %d of %d", (int) pos,
|
---|
| 279 | ui_scrollbar_move_length(scrollbar));
|
---|
| 280 | if (rv < 0) {
|
---|
| 281 | printf("Out of memory.\n");
|
---|
| 282 | return;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | rc = ui_entry_set_text(demo->entry, str);
|
---|
| 286 | if (rc != EOK)
|
---|
| 287 | printf("Error changing entry text.\n");
|
---|
| 288 | (void) ui_entry_paint(demo->entry);
|
---|
| 289 |
|
---|
| 290 | free(str);
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | /** Scrollbar was moved.
|
---|
| 294 | *
|
---|
| 295 | * @param scrollbar Scrollbar
|
---|
| 296 | * @param arg Argument (demo)
|
---|
| 297 | * @param pos Position
|
---|
| 298 | */
|
---|
| 299 | static void scrollbar_moved(ui_scrollbar_t *scrollbar, void *arg,
|
---|
| 300 | gfx_coord_t pos)
|
---|
| 301 | {
|
---|
| 302 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
| 303 | char *str;
|
---|
| 304 | errno_t rc;
|
---|
| 305 | int rv;
|
---|
| 306 |
|
---|
| 307 | rv = asprintf(&str, "Scrollbar: %d of %d", (int) pos,
|
---|
| 308 | ui_scrollbar_move_length(scrollbar));
|
---|
| 309 | if (rv < 0) {
|
---|
| 310 | printf("Out of memory.\n");
|
---|
| 311 | return;
|
---|
| 312 | }
|
---|
| 313 |
|
---|
| 314 | rc = ui_entry_set_text(demo->entry, str);
|
---|
| 315 | if (rc != EOK)
|
---|
| 316 | printf("Error changing entry text.\n");
|
---|
| 317 | (void) ui_entry_paint(demo->entry);
|
---|
| 318 |
|
---|
| 319 | free(str);
|
---|
| 320 | }
|
---|
| 321 |
|
---|
[5e109e1] | 322 | /** Display a message window.
|
---|
| 323 | *
|
---|
| 324 | * @param demo UI demo
|
---|
| 325 | * @param caption Window caption
|
---|
| 326 | * @param text Message text
|
---|
| 327 | */
|
---|
| 328 | static void uidemo_show_message(ui_demo_t *demo, const char *caption,
|
---|
| 329 | const char *text)
|
---|
| 330 | {
|
---|
| 331 | ui_msg_dialog_params_t mdparams;
|
---|
| 332 | ui_msg_dialog_t *dialog;
|
---|
| 333 | errno_t rc;
|
---|
| 334 |
|
---|
| 335 | ui_msg_dialog_params_init(&mdparams);
|
---|
| 336 | mdparams.caption = caption;
|
---|
| 337 | mdparams.text = text;
|
---|
| 338 |
|
---|
| 339 | rc = ui_msg_dialog_create(demo->ui, &mdparams, &dialog);
|
---|
| 340 | if (rc != EOK) {
|
---|
| 341 | printf("Error creating message dialog.\n");
|
---|
| 342 | return;
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | ui_msg_dialog_set_cb(dialog, &msg_dialog_cb, &demo);
|
---|
| 346 | }
|
---|
| 347 |
|
---|
[80d4aea] | 348 | /** File / Load menu entry selected.
|
---|
[5e109e1] | 349 | *
|
---|
| 350 | * @param mentry Menu entry
|
---|
| 351 | * @param arg Argument (demo)
|
---|
| 352 | */
|
---|
| 353 | static void uidemo_file_load(ui_menu_entry_t *mentry, void *arg)
|
---|
| 354 | {
|
---|
| 355 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
| 356 | ui_file_dialog_params_t fdparams;
|
---|
| 357 | ui_file_dialog_t *dialog;
|
---|
| 358 | errno_t rc;
|
---|
| 359 |
|
---|
| 360 | ui_file_dialog_params_init(&fdparams);
|
---|
| 361 | fdparams.caption = "Load File";
|
---|
| 362 |
|
---|
| 363 | rc = ui_file_dialog_create(demo->ui, &fdparams, &dialog);
|
---|
| 364 | if (rc != EOK) {
|
---|
| 365 | printf("Error creating message dialog.\n");
|
---|
| 366 | return;
|
---|
| 367 | }
|
---|
| 368 |
|
---|
| 369 | ui_file_dialog_set_cb(dialog, &file_dialog_cb, demo);
|
---|
| 370 | }
|
---|
| 371 |
|
---|
[80d4aea] | 372 | /** File / Message menu entry selected.
|
---|
[252d03c] | 373 | *
|
---|
| 374 | * @param mentry Menu entry
|
---|
| 375 | * @param arg Argument (demo)
|
---|
| 376 | */
|
---|
| 377 | static void uidemo_file_message(ui_menu_entry_t *mentry, void *arg)
|
---|
| 378 | {
|
---|
| 379 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
| 380 | ui_msg_dialog_params_t mdparams;
|
---|
| 381 | ui_msg_dialog_t *dialog;
|
---|
| 382 | errno_t rc;
|
---|
| 383 |
|
---|
| 384 | ui_msg_dialog_params_init(&mdparams);
|
---|
| 385 | mdparams.caption = "Message For You";
|
---|
| 386 | mdparams.text = "Hello, world!";
|
---|
| 387 |
|
---|
| 388 | rc = ui_msg_dialog_create(demo->ui, &mdparams, &dialog);
|
---|
| 389 | if (rc != EOK) {
|
---|
| 390 | printf("Error creating message dialog.\n");
|
---|
| 391 | return;
|
---|
| 392 | }
|
---|
| 393 |
|
---|
| 394 | ui_msg_dialog_set_cb(dialog, &msg_dialog_cb, &demo);
|
---|
| 395 | }
|
---|
| 396 |
|
---|
[80d4aea] | 397 | /** File / Exit menu entry selected.
|
---|
[214aefb] | 398 | *
|
---|
| 399 | * @param mentry Menu entry
|
---|
| 400 | * @param arg Argument (demo)
|
---|
| 401 | */
|
---|
| 402 | static void uidemo_file_exit(ui_menu_entry_t *mentry, void *arg)
|
---|
| 403 | {
|
---|
| 404 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
| 405 |
|
---|
| 406 | ui_quit(demo->ui);
|
---|
| 407 | }
|
---|
| 408 |
|
---|
[80d4aea] | 409 | /** Edit / Modify menu entry selected.
|
---|
| 410 | *
|
---|
| 411 | * @param mentry Menu entry
|
---|
| 412 | * @param arg Argument (demo)
|
---|
| 413 | */
|
---|
| 414 | static void uidemo_edit_modify(ui_menu_entry_t *mentry, void *arg)
|
---|
| 415 | {
|
---|
| 416 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
| 417 | ui_prompt_dialog_params_t pdparams;
|
---|
| 418 | ui_prompt_dialog_t *dialog;
|
---|
| 419 | errno_t rc;
|
---|
| 420 |
|
---|
| 421 | ui_prompt_dialog_params_init(&pdparams);
|
---|
| 422 | pdparams.caption = "Modify Entry Text";
|
---|
| 423 | pdparams.prompt = "Enter New Text";
|
---|
| 424 |
|
---|
| 425 | rc = ui_prompt_dialog_create(demo->ui, &pdparams, &dialog);
|
---|
| 426 | if (rc != EOK) {
|
---|
| 427 | printf("Error creating message dialog.\n");
|
---|
| 428 | return;
|
---|
| 429 | }
|
---|
| 430 |
|
---|
| 431 | ui_prompt_dialog_set_cb(dialog, &prompt_dialog_cb, demo);
|
---|
| 432 | }
|
---|
| 433 |
|
---|
[5e109e1] | 434 | /** File dialog OK button press.
|
---|
| 435 | *
|
---|
| 436 | * @param dialog File dialog
|
---|
| 437 | * @param arg Argument (ui_demo_t *)
|
---|
| 438 | * @param fname File name
|
---|
| 439 | */
|
---|
| 440 | static void file_dialog_bok(ui_file_dialog_t *dialog, void *arg,
|
---|
| 441 | const char *fname)
|
---|
| 442 | {
|
---|
| 443 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
| 444 | char buf[128];
|
---|
| 445 | char *p;
|
---|
| 446 | FILE *f;
|
---|
| 447 |
|
---|
| 448 | ui_file_dialog_destroy(dialog);
|
---|
| 449 |
|
---|
| 450 | f = fopen(fname, "rt");
|
---|
| 451 | if (f == NULL) {
|
---|
| 452 | uidemo_show_message(demo, "Error", "Error opening file.");
|
---|
| 453 | return;
|
---|
| 454 | }
|
---|
| 455 |
|
---|
| 456 | p = fgets(buf, sizeof(buf), f);
|
---|
| 457 | if (p == NULL) {
|
---|
| 458 | uidemo_show_message(demo, "Error", "Error reading file.");
|
---|
| 459 | fclose(f);
|
---|
| 460 | return;
|
---|
| 461 | }
|
---|
| 462 |
|
---|
| 463 | /* Cut string off at the first non-printable character */
|
---|
| 464 | p = buf;
|
---|
| 465 | while (*p != '\0') {
|
---|
| 466 | if (*p < ' ') {
|
---|
| 467 | *p = '\0';
|
---|
| 468 | break;
|
---|
| 469 | }
|
---|
| 470 | ++p;
|
---|
| 471 | }
|
---|
| 472 |
|
---|
| 473 | ui_entry_set_text(demo->entry, buf);
|
---|
| 474 | fclose(f);
|
---|
| 475 | }
|
---|
| 476 |
|
---|
| 477 | /** File dialog cancel button press.
|
---|
| 478 | *
|
---|
| 479 | * @param dialog File dialog
|
---|
| 480 | * @param arg Argument (ui_demo_t *)
|
---|
| 481 | */
|
---|
| 482 | static void file_dialog_bcancel(ui_file_dialog_t *dialog, void *arg)
|
---|
| 483 | {
|
---|
| 484 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
| 485 |
|
---|
| 486 | (void) demo;
|
---|
| 487 | ui_file_dialog_destroy(dialog);
|
---|
| 488 | }
|
---|
| 489 |
|
---|
[80d4aea] | 490 | /** File dialog close request.
|
---|
[5e109e1] | 491 | *
|
---|
| 492 | * @param dialog File dialog
|
---|
| 493 | * @param arg Argument (ui_demo_t *)
|
---|
| 494 | */
|
---|
| 495 | static void file_dialog_close(ui_file_dialog_t *dialog, void *arg)
|
---|
| 496 | {
|
---|
| 497 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
| 498 |
|
---|
| 499 | (void) demo;
|
---|
| 500 | ui_file_dialog_destroy(dialog);
|
---|
| 501 | }
|
---|
| 502 |
|
---|
[80d4aea] | 503 | /** Prompt dialog OK button press.
|
---|
| 504 | *
|
---|
| 505 | * @param dialog Prompt dialog
|
---|
| 506 | * @param arg Argument (ui_demo_t *)
|
---|
| 507 | * @param text Submitted text
|
---|
| 508 | */
|
---|
| 509 | static void prompt_dialog_bok(ui_prompt_dialog_t *dialog, void *arg,
|
---|
| 510 | const char *text)
|
---|
| 511 | {
|
---|
| 512 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
| 513 |
|
---|
| 514 | ui_prompt_dialog_destroy(dialog);
|
---|
| 515 | ui_entry_set_text(demo->entry, text);
|
---|
| 516 | }
|
---|
| 517 |
|
---|
| 518 | /** Prompt dialog cancel button press.
|
---|
| 519 | *
|
---|
| 520 | * @param dialog File dialog
|
---|
| 521 | * @param arg Argument (ui_demo_t *)
|
---|
| 522 | */
|
---|
| 523 | static void prompt_dialog_bcancel(ui_prompt_dialog_t *dialog, void *arg)
|
---|
| 524 | {
|
---|
| 525 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
| 526 |
|
---|
| 527 | (void) demo;
|
---|
| 528 | ui_prompt_dialog_destroy(dialog);
|
---|
| 529 | }
|
---|
| 530 |
|
---|
| 531 | /** Prompt dialog close request.
|
---|
| 532 | *
|
---|
| 533 | * @param dialog File dialog
|
---|
| 534 | * @param arg Argument (ui_demo_t *)
|
---|
| 535 | */
|
---|
| 536 | static void prompt_dialog_close(ui_prompt_dialog_t *dialog, void *arg)
|
---|
| 537 | {
|
---|
| 538 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
| 539 |
|
---|
| 540 | (void) demo;
|
---|
| 541 | ui_prompt_dialog_destroy(dialog);
|
---|
| 542 | }
|
---|
| 543 |
|
---|
[252d03c] | 544 | /** Message dialog button press.
|
---|
| 545 | *
|
---|
| 546 | * @param dialog Message dialog
|
---|
| 547 | * @param arg Argument (ui_demo_t *)
|
---|
| 548 | * @param bnum Button number
|
---|
| 549 | */
|
---|
| 550 | static void msg_dialog_button(ui_msg_dialog_t *dialog, void *arg,
|
---|
| 551 | unsigned bnum)
|
---|
| 552 | {
|
---|
| 553 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
| 554 |
|
---|
| 555 | (void) demo;
|
---|
| 556 | ui_msg_dialog_destroy(dialog);
|
---|
| 557 | }
|
---|
| 558 |
|
---|
| 559 | /** Message dialog close request.
|
---|
| 560 | *
|
---|
| 561 | * @param dialog Message dialog
|
---|
| 562 | * @param arg Argument (ui_demo_t *)
|
---|
| 563 | */
|
---|
| 564 | static void msg_dialog_close(ui_msg_dialog_t *dialog, void *arg)
|
---|
| 565 | {
|
---|
| 566 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
| 567 |
|
---|
| 568 | (void) demo;
|
---|
| 569 | ui_msg_dialog_destroy(dialog);
|
---|
| 570 | }
|
---|
| 571 |
|
---|
[47728678] | 572 | /** Run UI demo on display server. */
|
---|
[d284ce9] | 573 | static errno_t ui_demo(const char *display_spec)
|
---|
[47728678] | 574 | {
|
---|
[d284ce9] | 575 | ui_t *ui = NULL;
|
---|
| 576 | ui_wnd_params_t params;
|
---|
| 577 | ui_window_t *window = NULL;
|
---|
[f6df5a3] | 578 | ui_demo_t demo;
|
---|
[47728678] | 579 | gfx_rect_t rect;
|
---|
[d8ddf7a] | 580 | gfx_context_t *gc;
|
---|
[3583ffb] | 581 | ui_resource_t *ui_res;
|
---|
[d8ddf7a] | 582 | gfx_bitmap_params_t bparams;
|
---|
| 583 | gfx_bitmap_t *bitmap;
|
---|
| 584 | gfx_coord2_t off;
|
---|
[252d03c] | 585 | ui_menu_entry_t *mmsg;
|
---|
[5e109e1] | 586 | ui_menu_entry_t *mload;
|
---|
[214aefb] | 587 | ui_menu_entry_t *mfoo;
|
---|
| 588 | ui_menu_entry_t *mbar;
|
---|
| 589 | ui_menu_entry_t *mfoobar;
|
---|
[80d4aea] | 590 | ui_menu_entry_t *msep;
|
---|
[214aefb] | 591 | ui_menu_entry_t *mexit;
|
---|
[80d4aea] | 592 | ui_menu_entry_t *mmodify;
|
---|
[214aefb] | 593 | ui_menu_entry_t *mabout;
|
---|
[47728678] | 594 | errno_t rc;
|
---|
| 595 |
|
---|
[d284ce9] | 596 | rc = ui_create(display_spec, &ui);
|
---|
[47728678] | 597 | if (rc != EOK) {
|
---|
[d284ce9] | 598 | printf("Error creating UI on display %s.\n", display_spec);
|
---|
[47728678] | 599 | return rc;
|
---|
| 600 | }
|
---|
| 601 |
|
---|
[252d03c] | 602 | memset((void *) &demo, 0, sizeof(demo));
|
---|
| 603 | demo.ui = ui;
|
---|
| 604 |
|
---|
[d284ce9] | 605 | ui_wnd_params_init(¶ms);
|
---|
| 606 | params.caption = "UI Demo";
|
---|
[2d879f7] | 607 | params.style |= ui_wds_resizable;
|
---|
[47728678] | 608 |
|
---|
[252d03c] | 609 | /* FIXME: Auto layout */
|
---|
| 610 | if (ui_is_textmode(ui)) {
|
---|
| 611 | params.rect.p0.x = 0;
|
---|
| 612 | params.rect.p0.y = 0;
|
---|
[ab3bfc1] | 613 | params.rect.p1.x = 44;
|
---|
[bd16113] | 614 | params.rect.p1.y = 23;
|
---|
[252d03c] | 615 | } else {
|
---|
| 616 | params.rect.p0.x = 0;
|
---|
| 617 | params.rect.p0.y = 0;
|
---|
| 618 | params.rect.p1.x = 220;
|
---|
[bd16113] | 619 | params.rect.p1.y = 370;
|
---|
[252d03c] | 620 | }
|
---|
[1769693] | 621 |
|
---|
[d284ce9] | 622 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
[47728678] | 623 | if (rc != EOK) {
|
---|
| 624 | printf("Error creating window.\n");
|
---|
| 625 | return rc;
|
---|
| 626 | }
|
---|
| 627 |
|
---|
[d284ce9] | 628 | ui_window_set_cb(window, &window_cb, (void *) &demo);
|
---|
| 629 | demo.window = window;
|
---|
[47728678] | 630 |
|
---|
[3583ffb] | 631 | ui_res = ui_window_get_res(window);
|
---|
[d8ddf7a] | 632 | gc = ui_window_get_gc(window);
|
---|
[3583ffb] | 633 |
|
---|
[8009dc27] | 634 | rc = ui_fixed_create(&demo.fixed);
|
---|
| 635 | if (rc != EOK) {
|
---|
| 636 | printf("Error creating fixed layout.\n");
|
---|
| 637 | return rc;
|
---|
| 638 | }
|
---|
| 639 |
|
---|
[c68c18b9] | 640 | rc = ui_menu_bar_create(ui, window, &demo.mbar);
|
---|
[214aefb] | 641 | if (rc != EOK) {
|
---|
| 642 | printf("Error creating menu bar.\n");
|
---|
| 643 | return rc;
|
---|
| 644 | }
|
---|
| 645 |
|
---|
[ca2680d] | 646 | rc = ui_menu_create(demo.mbar, "~F~ile", &demo.mfile);
|
---|
[214aefb] | 647 | if (rc != EOK) {
|
---|
| 648 | printf("Error creating menu.\n");
|
---|
| 649 | return rc;
|
---|
| 650 | }
|
---|
| 651 |
|
---|
[c88d7f99] | 652 | rc = ui_menu_entry_create(demo.mfile, "~M~essage", "", &mmsg);
|
---|
[252d03c] | 653 | if (rc != EOK) {
|
---|
| 654 | printf("Error creating menu.\n");
|
---|
| 655 | return rc;
|
---|
| 656 | }
|
---|
| 657 |
|
---|
| 658 | ui_menu_entry_set_cb(mmsg, uidemo_file_message, (void *) &demo);
|
---|
| 659 |
|
---|
[c88d7f99] | 660 | rc = ui_menu_entry_create(demo.mfile, "~L~oad", "", &mload);
|
---|
[5e109e1] | 661 | if (rc != EOK) {
|
---|
| 662 | printf("Error creating menu.\n");
|
---|
| 663 | return rc;
|
---|
| 664 | }
|
---|
| 665 |
|
---|
| 666 | ui_menu_entry_set_cb(mload, uidemo_file_load, (void *) &demo);
|
---|
| 667 |
|
---|
[c88d7f99] | 668 | rc = ui_menu_entry_create(demo.mfile, "~F~oo", "Ctrl-Alt-Del", &mfoo);
|
---|
[214aefb] | 669 | if (rc != EOK) {
|
---|
| 670 | printf("Error creating menu.\n");
|
---|
| 671 | return rc;
|
---|
| 672 | }
|
---|
| 673 |
|
---|
[c88d7f99] | 674 | rc = ui_menu_entry_create(demo.mfile, "~B~ar", "", &mbar);
|
---|
[214aefb] | 675 | if (rc != EOK) {
|
---|
| 676 | printf("Error creating menu.\n");
|
---|
| 677 | return rc;
|
---|
| 678 | }
|
---|
| 679 |
|
---|
[c88d7f99] | 680 | rc = ui_menu_entry_create(demo.mfile, "F~o~obar", "", &mfoobar);
|
---|
[214aefb] | 681 | if (rc != EOK) {
|
---|
| 682 | printf("Error creating menu.\n");
|
---|
| 683 | return rc;
|
---|
| 684 | }
|
---|
| 685 |
|
---|
[80d4aea] | 686 | rc = ui_menu_entry_sep_create(demo.mfile, &msep);
|
---|
[6186f9f] | 687 | if (rc != EOK) {
|
---|
| 688 | printf("Error creating menu.\n");
|
---|
| 689 | return rc;
|
---|
| 690 | }
|
---|
| 691 |
|
---|
[c88d7f99] | 692 | rc = ui_menu_entry_create(demo.mfile, "E~x~it", "Alt-F4", &mexit);
|
---|
[214aefb] | 693 | if (rc != EOK) {
|
---|
| 694 | printf("Error creating menu.\n");
|
---|
| 695 | return rc;
|
---|
| 696 | }
|
---|
| 697 |
|
---|
| 698 | ui_menu_entry_set_cb(mexit, uidemo_file_exit, (void *) &demo);
|
---|
| 699 |
|
---|
[ca2680d] | 700 | rc = ui_menu_create(demo.mbar, "~E~dit", &demo.medit);
|
---|
[214aefb] | 701 | if (rc != EOK) {
|
---|
| 702 | printf("Error creating menu.\n");
|
---|
| 703 | return rc;
|
---|
| 704 | }
|
---|
| 705 |
|
---|
[c88d7f99] | 706 | rc = ui_menu_entry_create(demo.medit, "~M~odify", "", &mmodify);
|
---|
[80d4aea] | 707 | if (rc != EOK) {
|
---|
| 708 | printf("Error creating menu.\n");
|
---|
| 709 | return rc;
|
---|
| 710 | }
|
---|
| 711 |
|
---|
| 712 | ui_menu_entry_set_cb(mmodify, uidemo_edit_modify, (void *) &demo);
|
---|
| 713 |
|
---|
[ca2680d] | 714 | rc = ui_menu_create(demo.mbar, "~P~references", &demo.mpreferences);
|
---|
[214aefb] | 715 | if (rc != EOK) {
|
---|
| 716 | printf("Error creating menu.\n");
|
---|
| 717 | return rc;
|
---|
| 718 | }
|
---|
| 719 |
|
---|
[ca2680d] | 720 | rc = ui_menu_create(demo.mbar, "~H~elp", &demo.mhelp);
|
---|
[214aefb] | 721 | if (rc != EOK) {
|
---|
| 722 | printf("Error creating menu.\n");
|
---|
| 723 | return rc;
|
---|
| 724 | }
|
---|
| 725 |
|
---|
[c88d7f99] | 726 | rc = ui_menu_entry_create(demo.mhelp, "~A~bout", "Ctrl-H, F1", &mabout);
|
---|
[214aefb] | 727 | if (rc != EOK) {
|
---|
| 728 | printf("Error creating menu.\n");
|
---|
| 729 | return rc;
|
---|
| 730 | }
|
---|
| 731 |
|
---|
[252d03c] | 732 | /* FIXME: Auto layout */
|
---|
| 733 | if (ui_is_textmode(ui)) {
|
---|
| 734 | rect.p0.x = 1;
|
---|
[45004f3] | 735 | rect.p0.y = 1;
|
---|
[ab3bfc1] | 736 | rect.p1.x = 43;
|
---|
[45004f3] | 737 | rect.p1.y = 2;
|
---|
[252d03c] | 738 | } else {
|
---|
| 739 | rect.p0.x = 4;
|
---|
| 740 | rect.p0.y = 30;
|
---|
| 741 | rect.p1.x = 216;
|
---|
| 742 | rect.p1.y = 52;
|
---|
| 743 | }
|
---|
[214aefb] | 744 | ui_menu_bar_set_rect(demo.mbar, &rect);
|
---|
| 745 |
|
---|
| 746 | rc = ui_fixed_add(demo.fixed, ui_menu_bar_ctl(demo.mbar));
|
---|
| 747 | if (rc != EOK) {
|
---|
| 748 | printf("Error adding control to layout.\n");
|
---|
| 749 | return rc;
|
---|
| 750 | }
|
---|
| 751 |
|
---|
[db3895d] | 752 | rc = ui_entry_create(window, "", &demo.entry);
|
---|
[214aefb] | 753 | if (rc != EOK) {
|
---|
| 754 | printf("Error creating entry.\n");
|
---|
| 755 | return rc;
|
---|
| 756 | }
|
---|
| 757 |
|
---|
[a977e37] | 758 | /* FIXME: Auto layout */
|
---|
| 759 | if (ui_is_textmode(ui)) {
|
---|
[ab3bfc1] | 760 | rect.p0.x = 2;
|
---|
| 761 | rect.p0.y = 3;
|
---|
| 762 | rect.p1.x = 42;
|
---|
| 763 | rect.p1.y = 4;
|
---|
[a977e37] | 764 | } else {
|
---|
| 765 | rect.p0.x = 15;
|
---|
| 766 | rect.p0.y = 53;
|
---|
| 767 | rect.p1.x = 205;
|
---|
| 768 | rect.p1.y = 78;
|
---|
| 769 | }
|
---|
| 770 |
|
---|
[214aefb] | 771 | ui_entry_set_rect(demo.entry, &rect);
|
---|
| 772 | ui_entry_set_halign(demo.entry, gfx_halign_center);
|
---|
| 773 |
|
---|
| 774 | rc = ui_fixed_add(demo.fixed, ui_entry_ctl(demo.entry));
|
---|
| 775 | if (rc != EOK) {
|
---|
| 776 | printf("Error adding control to layout.\n");
|
---|
| 777 | return rc;
|
---|
| 778 | }
|
---|
| 779 |
|
---|
[d8ddf7a] | 780 | rc = ui_label_create(ui_res, "Text label", &demo.label);
|
---|
[ba09d06] | 781 | if (rc != EOK) {
|
---|
| 782 | printf("Error creating label.\n");
|
---|
| 783 | return rc;
|
---|
| 784 | }
|
---|
| 785 |
|
---|
[a977e37] | 786 | /* FIXME: Auto layout */
|
---|
| 787 | if (ui_is_textmode(ui)) {
|
---|
[ab3bfc1] | 788 | rect.p0.x = 2;
|
---|
| 789 | rect.p0.y = 5;
|
---|
| 790 | rect.p1.x = 42;
|
---|
| 791 | rect.p1.y = 6;
|
---|
[a977e37] | 792 | } else {
|
---|
| 793 | rect.p0.x = 60;
|
---|
| 794 | rect.p0.y = 88;
|
---|
| 795 | rect.p1.x = 160;
|
---|
| 796 | rect.p1.y = 101;
|
---|
| 797 | }
|
---|
| 798 |
|
---|
[ba09d06] | 799 | ui_label_set_rect(demo.label, &rect);
|
---|
[58a67050] | 800 | ui_label_set_halign(demo.label, gfx_halign_center);
|
---|
[ba09d06] | 801 |
|
---|
[8009dc27] | 802 | rc = ui_fixed_add(demo.fixed, ui_label_ctl(demo.label));
|
---|
| 803 | if (rc != EOK) {
|
---|
| 804 | printf("Error adding control to layout.\n");
|
---|
| 805 | return rc;
|
---|
| 806 | }
|
---|
| 807 |
|
---|
[d8ddf7a] | 808 | rc = ui_pbutton_create(ui_res, "OK", &demo.pb1);
|
---|
[47728678] | 809 | if (rc != EOK) {
|
---|
| 810 | printf("Error creating button.\n");
|
---|
[f6df5a3] | 811 | return rc;
|
---|
[47728678] | 812 | }
|
---|
| 813 |
|
---|
[8ef48ece] | 814 | ui_pbutton_set_cb(demo.pb1, &pbutton_cb, (void *) &demo);
|
---|
| 815 |
|
---|
[a977e37] | 816 | /* FIXME: Auto layout */
|
---|
| 817 | if (ui_is_textmode(ui)) {
|
---|
[ab3bfc1] | 818 | rect.p0.x = 2;
|
---|
| 819 | rect.p0.y = 7;
|
---|
| 820 | rect.p1.x = 12;
|
---|
| 821 | rect.p1.y = 8;
|
---|
[a977e37] | 822 | } else {
|
---|
| 823 | rect.p0.x = 15;
|
---|
| 824 | rect.p0.y = 111;
|
---|
| 825 | rect.p1.x = 105;
|
---|
| 826 | rect.p1.y = 139;
|
---|
| 827 | }
|
---|
| 828 |
|
---|
[f6df5a3] | 829 | ui_pbutton_set_rect(demo.pb1, &rect);
|
---|
[47728678] | 830 |
|
---|
[c9a7adc] | 831 | ui_pbutton_set_default(demo.pb1, true);
|
---|
| 832 |
|
---|
[8009dc27] | 833 | rc = ui_fixed_add(demo.fixed, ui_pbutton_ctl(demo.pb1));
|
---|
| 834 | if (rc != EOK) {
|
---|
| 835 | printf("Error adding control to layout.\n");
|
---|
| 836 | return rc;
|
---|
| 837 | }
|
---|
| 838 |
|
---|
[3583ffb] | 839 | rc = ui_pbutton_create(ui_res, "Cancel", &demo.pb2);
|
---|
[47728678] | 840 | if (rc != EOK) {
|
---|
| 841 | printf("Error creating button.\n");
|
---|
[f6df5a3] | 842 | return rc;
|
---|
[47728678] | 843 | }
|
---|
| 844 |
|
---|
[8ef48ece] | 845 | ui_pbutton_set_cb(demo.pb2, &pbutton_cb, (void *) &demo);
|
---|
| 846 |
|
---|
[a977e37] | 847 | if (ui_is_textmode(ui)) {
|
---|
[ab3bfc1] | 848 | rect.p0.x = 32;
|
---|
| 849 | rect.p0.y = 7;
|
---|
| 850 | rect.p1.x = 42;
|
---|
| 851 | rect.p1.y = 8;
|
---|
[a977e37] | 852 | } else {
|
---|
| 853 | rect.p0.x = 115;
|
---|
| 854 | rect.p0.y = 111;
|
---|
| 855 | rect.p1.x = 205;
|
---|
| 856 | rect.p1.y = 139;
|
---|
| 857 | }
|
---|
| 858 |
|
---|
[f6df5a3] | 859 | ui_pbutton_set_rect(demo.pb2, &rect);
|
---|
[47728678] | 860 |
|
---|
[8009dc27] | 861 | rc = ui_fixed_add(demo.fixed, ui_pbutton_ctl(demo.pb2));
|
---|
| 862 | if (rc != EOK) {
|
---|
| 863 | printf("Error adding control to layout.\n");
|
---|
| 864 | return rc;
|
---|
| 865 | }
|
---|
| 866 |
|
---|
[d8ddf7a] | 867 | gfx_bitmap_params_init(&bparams);
|
---|
[de0c55a] | 868 | if (ui_is_textmode(ui)) {
|
---|
| 869 | bparams.rect.p0.x = 0;
|
---|
| 870 | bparams.rect.p0.y = 0;
|
---|
| 871 | bparams.rect.p1.x = 40;
|
---|
| 872 | bparams.rect.p1.y = 2;
|
---|
| 873 | } else {
|
---|
| 874 | bparams.rect.p0.x = 0;
|
---|
| 875 | bparams.rect.p0.y = 0;
|
---|
| 876 | bparams.rect.p1.x = 188;
|
---|
| 877 | bparams.rect.p1.y = 24;
|
---|
| 878 | }
|
---|
[d8ddf7a] | 879 |
|
---|
| 880 | rc = gfx_bitmap_create(gc, &bparams, NULL, &bitmap);
|
---|
| 881 | if (rc != EOK)
|
---|
| 882 | return rc;
|
---|
| 883 |
|
---|
| 884 | rc = bitmap_moire(bitmap, bparams.rect.p1.x, bparams.rect.p1.y);
|
---|
| 885 | if (rc != EOK)
|
---|
| 886 | return rc;
|
---|
| 887 |
|
---|
| 888 | rc = ui_image_create(ui_res, bitmap, ¶ms.rect, &demo.image);
|
---|
| 889 | if (rc != EOK) {
|
---|
| 890 | printf("Error creating label.\n");
|
---|
| 891 | return rc;
|
---|
| 892 | }
|
---|
| 893 |
|
---|
[de0c55a] | 894 | if (ui_is_textmode(ui)) {
|
---|
[ab3bfc1] | 895 | off.x = 2;
|
---|
| 896 | off.y = 9;
|
---|
[de0c55a] | 897 | } else {
|
---|
| 898 | off.x = 15;
|
---|
| 899 | off.y = 155;
|
---|
| 900 | }
|
---|
| 901 |
|
---|
[d8ddf7a] | 902 | gfx_rect_translate(&off, &bparams.rect, &rect);
|
---|
| 903 |
|
---|
| 904 | /* Adjust for frame width (2 x 1 pixel) */
|
---|
[de0c55a] | 905 | if (!ui_is_textmode(ui)) {
|
---|
| 906 | ui_image_set_flags(demo.image, ui_imgf_frame);
|
---|
| 907 | rect.p1.x += 2;
|
---|
| 908 | rect.p1.y += 2;
|
---|
| 909 | }
|
---|
| 910 |
|
---|
[d8ddf7a] | 911 | ui_image_set_rect(demo.image, &rect);
|
---|
| 912 |
|
---|
| 913 | rc = ui_fixed_add(demo.fixed, ui_image_ctl(demo.image));
|
---|
| 914 | if (rc != EOK) {
|
---|
| 915 | printf("Error adding control to layout.\n");
|
---|
| 916 | return rc;
|
---|
| 917 | }
|
---|
| 918 |
|
---|
[5de71df] | 919 | rc = ui_checkbox_create(ui_res, "Read only", &demo.checkbox);
|
---|
[d70dc1c4] | 920 | if (rc != EOK) {
|
---|
| 921 | printf("Error creating check box.\n");
|
---|
| 922 | return rc;
|
---|
| 923 | }
|
---|
| 924 |
|
---|
| 925 | ui_checkbox_set_cb(demo.checkbox, &checkbox_cb, (void *) &demo);
|
---|
| 926 |
|
---|
[307d4d2] | 927 | /* FIXME: Auto layout */
|
---|
| 928 | if (ui_is_textmode(ui)) {
|
---|
[ab3bfc1] | 929 | rect.p0.x = 2;
|
---|
| 930 | rect.p0.y = 12;
|
---|
| 931 | rect.p1.x = 12;
|
---|
| 932 | rect.p1.y = 13;
|
---|
[307d4d2] | 933 | } else {
|
---|
| 934 | rect.p0.x = 15;
|
---|
| 935 | rect.p0.y = 190;
|
---|
| 936 | rect.p1.x = 140;
|
---|
| 937 | rect.p1.y = 210;
|
---|
| 938 | }
|
---|
[de0c55a] | 939 |
|
---|
[d70dc1c4] | 940 | ui_checkbox_set_rect(demo.checkbox, &rect);
|
---|
| 941 |
|
---|
| 942 | rc = ui_fixed_add(demo.fixed, ui_checkbox_ctl(demo.checkbox));
|
---|
| 943 | if (rc != EOK) {
|
---|
| 944 | printf("Error adding control to layout.\n");
|
---|
| 945 | return rc;
|
---|
| 946 | }
|
---|
| 947 |
|
---|
[7020d1f] | 948 | rc = ui_rbutton_group_create(ui_res, &demo.rbgroup);
|
---|
| 949 | if (rc != EOK) {
|
---|
| 950 | printf("Error creating radio button group.\n");
|
---|
| 951 | return rc;
|
---|
| 952 | }
|
---|
| 953 |
|
---|
[5de71df] | 954 | rc = ui_rbutton_create(demo.rbgroup, "Left", (void *) &uidemo_halign[0],
|
---|
| 955 | &demo.rbleft);
|
---|
[7020d1f] | 956 | if (rc != EOK) {
|
---|
| 957 | printf("Error creating radio button.\n");
|
---|
| 958 | return rc;
|
---|
| 959 | }
|
---|
| 960 |
|
---|
| 961 | ui_rbutton_group_set_cb(demo.rbgroup, &rbutton_group_cb,
|
---|
| 962 | (void *) &demo);
|
---|
| 963 |
|
---|
[297b1b3] | 964 | /* FIXME: Auto layout */
|
---|
| 965 | if (ui_is_textmode(ui)) {
|
---|
[ab3bfc1] | 966 | rect.p0.x = 2;
|
---|
| 967 | rect.p0.y = 14;
|
---|
| 968 | rect.p1.x = 12;
|
---|
| 969 | rect.p1.y = 15;
|
---|
[297b1b3] | 970 | } else {
|
---|
| 971 | rect.p0.x = 15;
|
---|
| 972 | rect.p0.y = 220;
|
---|
| 973 | rect.p1.x = 140;
|
---|
| 974 | rect.p1.y = 240;
|
---|
| 975 | }
|
---|
[5de71df] | 976 | ui_rbutton_set_rect(demo.rbleft, &rect);
|
---|
[7020d1f] | 977 |
|
---|
[5de71df] | 978 | rc = ui_fixed_add(demo.fixed, ui_rbutton_ctl(demo.rbleft));
|
---|
[7020d1f] | 979 | if (rc != EOK) {
|
---|
| 980 | printf("Error adding control to layout.\n");
|
---|
| 981 | return rc;
|
---|
| 982 | }
|
---|
| 983 |
|
---|
[5de71df] | 984 | rc = ui_rbutton_create(demo.rbgroup, "Center", (void *) &uidemo_halign[1],
|
---|
| 985 | &demo.rbcenter);
|
---|
[7020d1f] | 986 | if (rc != EOK) {
|
---|
| 987 | printf("Error creating radio button.\n");
|
---|
| 988 | return rc;
|
---|
| 989 | }
|
---|
| 990 |
|
---|
[297b1b3] | 991 | /* FIXME: Auto layout */
|
---|
| 992 | if (ui_is_textmode(ui)) {
|
---|
[ab3bfc1] | 993 | rect.p0.x = 2;
|
---|
| 994 | rect.p0.y = 15;
|
---|
| 995 | rect.p1.x = 12;
|
---|
| 996 | rect.p1.y = 16;
|
---|
[297b1b3] | 997 | } else {
|
---|
| 998 | rect.p0.x = 15;
|
---|
| 999 | rect.p0.y = 250;
|
---|
| 1000 | rect.p1.x = 140;
|
---|
| 1001 | rect.p1.y = 270;
|
---|
| 1002 | }
|
---|
[5de71df] | 1003 | ui_rbutton_set_rect(demo.rbcenter, &rect);
|
---|
| 1004 | ui_rbutton_select(demo.rbcenter);
|
---|
[7020d1f] | 1005 |
|
---|
[5de71df] | 1006 | rc = ui_fixed_add(demo.fixed, ui_rbutton_ctl(demo.rbcenter));
|
---|
[7020d1f] | 1007 | if (rc != EOK) {
|
---|
| 1008 | printf("Error adding control to layout.\n");
|
---|
| 1009 | return rc;
|
---|
| 1010 | }
|
---|
| 1011 |
|
---|
[5de71df] | 1012 | rc = ui_rbutton_create(demo.rbgroup, "Right", (void *) &uidemo_halign[2],
|
---|
| 1013 | &demo.rbright);
|
---|
[7020d1f] | 1014 | if (rc != EOK) {
|
---|
| 1015 | printf("Error creating radio button.\n");
|
---|
| 1016 | return rc;
|
---|
| 1017 | }
|
---|
| 1018 |
|
---|
[297b1b3] | 1019 | /* FIXME: Auto layout */
|
---|
| 1020 | if (ui_is_textmode(ui)) {
|
---|
[ab3bfc1] | 1021 | rect.p0.x = 2;
|
---|
| 1022 | rect.p0.y = 16;
|
---|
| 1023 | rect.p1.x = 12;
|
---|
| 1024 | rect.p1.y = 17;
|
---|
[297b1b3] | 1025 | } else {
|
---|
| 1026 | rect.p0.x = 15;
|
---|
| 1027 | rect.p0.y = 280;
|
---|
| 1028 | rect.p1.x = 140;
|
---|
| 1029 | rect.p1.y = 300;
|
---|
| 1030 | }
|
---|
[5de71df] | 1031 | ui_rbutton_set_rect(demo.rbright, &rect);
|
---|
[7020d1f] | 1032 |
|
---|
[5de71df] | 1033 | rc = ui_fixed_add(demo.fixed, ui_rbutton_ctl(demo.rbright));
|
---|
[7020d1f] | 1034 | if (rc != EOK) {
|
---|
| 1035 | printf("Error adding control to layout.\n");
|
---|
| 1036 | return rc;
|
---|
| 1037 | }
|
---|
| 1038 |
|
---|
[ef734b7] | 1039 | rc = ui_slider_create(ui_res, "Slide!", &demo.slider);
|
---|
| 1040 | if (rc != EOK) {
|
---|
| 1041 | printf("Error creating button.\n");
|
---|
| 1042 | return rc;
|
---|
| 1043 | }
|
---|
| 1044 |
|
---|
| 1045 | ui_slider_set_cb(demo.slider, &slider_cb, (void *) &demo);
|
---|
| 1046 |
|
---|
[943f032] | 1047 | /* FIXME: Auto layout */
|
---|
| 1048 | if (ui_is_textmode(ui)) {
|
---|
[ab3bfc1] | 1049 | rect.p0.x = 2;
|
---|
| 1050 | rect.p0.y = 18;
|
---|
| 1051 | rect.p1.x = 12;
|
---|
| 1052 | rect.p1.y = 19;
|
---|
[943f032] | 1053 | } else {
|
---|
| 1054 | rect.p0.x = 15;
|
---|
| 1055 | rect.p0.y = 310;
|
---|
| 1056 | rect.p1.x = 130;
|
---|
| 1057 | rect.p1.y = 330;
|
---|
| 1058 | }
|
---|
| 1059 |
|
---|
[ef734b7] | 1060 | ui_slider_set_rect(demo.slider, &rect);
|
---|
| 1061 |
|
---|
| 1062 | rc = ui_fixed_add(demo.fixed, ui_slider_ctl(demo.slider));
|
---|
| 1063 | if (rc != EOK) {
|
---|
| 1064 | printf("Error adding control to layout.\n");
|
---|
| 1065 | return rc;
|
---|
| 1066 | }
|
---|
| 1067 |
|
---|
[bd16113] | 1068 | rc = ui_scrollbar_create(ui_res, &demo.scrollbar);
|
---|
| 1069 | if (rc != EOK) {
|
---|
| 1070 | printf("Error creating button.\n");
|
---|
| 1071 | return rc;
|
---|
| 1072 | }
|
---|
| 1073 |
|
---|
| 1074 | ui_scrollbar_set_cb(demo.scrollbar, &scrollbar_cb, (void *) &demo);
|
---|
| 1075 |
|
---|
| 1076 | /* FIXME: Auto layout */
|
---|
| 1077 | if (ui_is_textmode(ui)) {
|
---|
| 1078 | rect.p0.x = 2;
|
---|
| 1079 | rect.p0.y = 20;
|
---|
| 1080 | rect.p1.x = 12;
|
---|
| 1081 | rect.p1.y = 21;
|
---|
| 1082 | } else {
|
---|
| 1083 | rect.p0.x = 15;
|
---|
| 1084 | rect.p0.y = 340;
|
---|
| 1085 | rect.p1.x = 210;
|
---|
| 1086 | rect.p1.y = 362;
|
---|
| 1087 | }
|
---|
| 1088 |
|
---|
| 1089 | ui_scrollbar_set_rect(demo.scrollbar, &rect);
|
---|
| 1090 |
|
---|
| 1091 | ui_scrollbar_set_thumb_length(demo.scrollbar,
|
---|
| 1092 | ui_scrollbar_through_length(demo.scrollbar) / 4);
|
---|
| 1093 |
|
---|
| 1094 | rc = ui_fixed_add(demo.fixed, ui_scrollbar_ctl(demo.scrollbar));
|
---|
| 1095 | if (rc != EOK) {
|
---|
| 1096 | printf("Error adding control to layout.\n");
|
---|
| 1097 | return rc;
|
---|
| 1098 | }
|
---|
| 1099 |
|
---|
[b71c0fc] | 1100 | ui_window_add(window, ui_fixed_ctl(demo.fixed));
|
---|
| 1101 |
|
---|
[fa01c05] | 1102 | rc = ui_window_paint(window);
|
---|
[ba09d06] | 1103 | if (rc != EOK) {
|
---|
[fa01c05] | 1104 | printf("Error painting window.\n");
|
---|
[f6df5a3] | 1105 | return rc;
|
---|
[47728678] | 1106 | }
|
---|
| 1107 |
|
---|
[d284ce9] | 1108 | ui_run(ui);
|
---|
[47728678] | 1109 |
|
---|
[d284ce9] | 1110 | ui_window_destroy(window);
|
---|
| 1111 | ui_destroy(ui);
|
---|
[47728678] | 1112 |
|
---|
| 1113 | return EOK;
|
---|
| 1114 | }
|
---|
| 1115 |
|
---|
[d8ddf7a] | 1116 | /** Fill bitmap with moire pattern.
|
---|
| 1117 | *
|
---|
| 1118 | * @param bitmap Bitmap
|
---|
| 1119 | * @param w Bitmap width
|
---|
| 1120 | * @param h Bitmap height
|
---|
| 1121 | * @return EOK on success or an error code
|
---|
| 1122 | */
|
---|
| 1123 | static errno_t bitmap_moire(gfx_bitmap_t *bitmap, gfx_coord_t w, gfx_coord_t h)
|
---|
| 1124 | {
|
---|
| 1125 | int i, j;
|
---|
| 1126 | int k;
|
---|
| 1127 | pixelmap_t pixelmap;
|
---|
| 1128 | gfx_bitmap_alloc_t alloc;
|
---|
| 1129 | errno_t rc;
|
---|
| 1130 |
|
---|
| 1131 | rc = gfx_bitmap_get_alloc(bitmap, &alloc);
|
---|
| 1132 | if (rc != EOK)
|
---|
| 1133 | return rc;
|
---|
| 1134 |
|
---|
| 1135 | /* In absence of anything else, use pixelmap */
|
---|
| 1136 | pixelmap.width = w;
|
---|
| 1137 | pixelmap.height = h;
|
---|
| 1138 | pixelmap.data = alloc.pixels;
|
---|
| 1139 |
|
---|
| 1140 | for (i = 0; i < w; i++) {
|
---|
| 1141 | for (j = 0; j < h; j++) {
|
---|
| 1142 | k = i * i + j * j;
|
---|
| 1143 | pixelmap_put_pixel(&pixelmap, i, j,
|
---|
[de0c55a] | 1144 | PIXEL(0, k, k, 255 - k));
|
---|
[d8ddf7a] | 1145 | }
|
---|
| 1146 | }
|
---|
| 1147 |
|
---|
| 1148 | return EOK;
|
---|
| 1149 | }
|
---|
| 1150 |
|
---|
[d284ce9] | 1151 | static void print_syntax(void)
|
---|
| 1152 | {
|
---|
| 1153 | printf("Syntax: uidemo [-d <display-spec>]\n");
|
---|
| 1154 | }
|
---|
| 1155 |
|
---|
[f80690a] | 1156 | int main(int argc, char *argv[])
|
---|
| 1157 | {
|
---|
[552b69f] | 1158 | const char *display_spec = UI_ANY_DEFAULT;
|
---|
[f80690a] | 1159 | errno_t rc;
|
---|
| 1160 | int i;
|
---|
| 1161 |
|
---|
| 1162 | i = 1;
|
---|
| 1163 | while (i < argc && argv[i][0] == '-') {
|
---|
| 1164 | if (str_cmp(argv[i], "-d") == 0) {
|
---|
| 1165 | ++i;
|
---|
| 1166 | if (i >= argc) {
|
---|
| 1167 | printf("Argument missing.\n");
|
---|
| 1168 | print_syntax();
|
---|
| 1169 | return 1;
|
---|
| 1170 | }
|
---|
| 1171 |
|
---|
[d284ce9] | 1172 | display_spec = argv[i++];
|
---|
[f80690a] | 1173 | } else {
|
---|
| 1174 | printf("Invalid option '%s'.\n", argv[i]);
|
---|
| 1175 | print_syntax();
|
---|
| 1176 | return 1;
|
---|
| 1177 | }
|
---|
| 1178 | }
|
---|
| 1179 |
|
---|
| 1180 | if (i < argc) {
|
---|
| 1181 | print_syntax();
|
---|
| 1182 | return 1;
|
---|
| 1183 | }
|
---|
| 1184 |
|
---|
[d284ce9] | 1185 | rc = ui_demo(display_spec);
|
---|
[47728678] | 1186 | if (rc != EOK)
|
---|
[f80690a] | 1187 | return 1;
|
---|
| 1188 |
|
---|
| 1189 | return 0;
|
---|
| 1190 | }
|
---|
| 1191 |
|
---|
| 1192 | /** @}
|
---|
| 1193 | */
|
---|