source: mainline/uspace/app/uidemo/uidemo.c@ 45004f3

serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 45004f3 was 45004f3, checked in by Jiri Svoboda <jiri@…>, 4 years ago

Text-style window title bar

  • Property mode set to 100644
File size: 20.7 KB
RevLine 
[f80690a]1/*
[7020d1f]2 * Copyright (c) 2021 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>
[47728678]51#include <ui/resource.h>
[d284ce9]52#include <ui/ui.h>
53#include <ui/window.h>
[f6df5a3]54#include "uidemo.h"
[47728678]55
[d8ddf7a]56static errno_t bitmap_moire(gfx_bitmap_t *, gfx_coord_t, gfx_coord_t);
57
[d284ce9]58static void wnd_close(ui_window_t *, void *);
59
60static ui_window_cb_t window_cb = {
[b71c0fc]61 .close = wnd_close
[47728678]62};
63
[8ef48ece]64static void pb_clicked(ui_pbutton_t *, void *);
65
66static ui_pbutton_cb_t pbutton_cb = {
67 .clicked = pb_clicked
68};
69
[d70dc1c4]70static void checkbox_switched(ui_checkbox_t *, void *, bool);
71
72static ui_checkbox_cb_t checkbox_cb = {
73 .switched = checkbox_switched
74};
75
[7020d1f]76static void rb_selected(ui_rbutton_group_t *, void *, void *);
77
78static ui_rbutton_group_cb_t rbutton_group_cb = {
79 .selected = rb_selected
80};
81
[ef734b7]82static void slider_moved(ui_slider_t *, void *, gfx_coord_t);
83
84static ui_slider_cb_t slider_cb = {
85 .moved = slider_moved
86};
87
[5e109e1]88static void uidemo_file_load(ui_menu_entry_t *, void *);
[252d03c]89static void uidemo_file_message(ui_menu_entry_t *, void *);
[214aefb]90static void uidemo_file_exit(ui_menu_entry_t *, void *);
91
[5e109e1]92static void file_dialog_bok(ui_file_dialog_t *, void *, const char *);
93static void file_dialog_bcancel(ui_file_dialog_t *, void *);
94static void file_dialog_close(ui_file_dialog_t *, void *);
95
96static ui_file_dialog_cb_t file_dialog_cb = {
97 .bok = file_dialog_bok,
98 .bcancel = file_dialog_bcancel,
99 .close = file_dialog_close
100};
101
[252d03c]102static void msg_dialog_button(ui_msg_dialog_t *, void *, unsigned);
103static void msg_dialog_close(ui_msg_dialog_t *, void *);
104
105static ui_msg_dialog_cb_t msg_dialog_cb = {
106 .button = msg_dialog_button,
107 .close = msg_dialog_close
108};
109
[5de71df]110/** Horizontal alignment selected by each radio button */
111static const gfx_halign_t uidemo_halign[3] = {
112 gfx_halign_left,
113 gfx_halign_center,
114 gfx_halign_right
115};
116
[d284ce9]117/** Window close button was clicked.
118 *
119 * @param window Window
120 * @param arg Argument (demo)
121 */
122static void wnd_close(ui_window_t *window, void *arg)
[47728678]123{
[20d2c6c]124 ui_demo_t *demo = (ui_demo_t *) arg;
125
[d284ce9]126 ui_quit(demo->ui);
[47728678]127}
128
[8ef48ece]129/** Push button was clicked.
130 *
131 * @param pbutton Push button
132 * @param arg Argument (demo)
133 */
134static void pb_clicked(ui_pbutton_t *pbutton, void *arg)
135{
136 ui_demo_t *demo = (ui_demo_t *) arg;
[ba09d06]137 errno_t rc;
[8ef48ece]138
139 if (pbutton == demo->pb1) {
[d8ddf7a]140 rc = ui_entry_set_text(demo->entry, "OK pressed");
[ba09d06]141 if (rc != EOK)
[d8ddf7a]142 printf("Error changing entry text.\n");
[8ef48ece]143 } else {
[d8ddf7a]144 rc = ui_entry_set_text(demo->entry, "Cancel pressed");
[ba09d06]145 if (rc != EOK)
[d8ddf7a]146 printf("Error changing entry text.\n");
[8ef48ece]147 }
148}
149
[d70dc1c4]150/** Check box was switched.
151 *
152 * @param checkbox Check box
153 * @param arg Argument (demo)
154 */
155static void checkbox_switched(ui_checkbox_t *checkbox, void *arg, bool enable)
156{
157 ui_demo_t *demo = (ui_demo_t *) arg;
158
[5de71df]159 ui_entry_set_read_only(demo->entry, enable);
[d70dc1c4]160}
161
[7020d1f]162/** Radio button was selected.
163 *
164 * @param rbgroup Radio button group
165 * @param garg Group argument (demo)
166 * @param barg Button argument
167 */
168static void rb_selected(ui_rbutton_group_t *rbgroup, void *garg, void *barg)
169{
170 ui_demo_t *demo = (ui_demo_t *) garg;
[5de71df]171 gfx_halign_t halign = *(gfx_halign_t *) barg;
[7020d1f]172
[5de71df]173 ui_entry_set_halign(demo->entry, halign);
[7020d1f]174 (void) ui_entry_paint(demo->entry);
175}
176
[ef734b7]177/** Slider was moved.
178 *
179 * @param slider Slider
180 * @param arg Argument (demo)
181 * @param pos Position
182 */
183static void slider_moved(ui_slider_t *slider, void *arg, gfx_coord_t pos)
184{
185 ui_demo_t *demo = (ui_demo_t *) arg;
186 char *str;
187 errno_t rc;
188 int rv;
189
190 rv = asprintf(&str, "Slider at %d of %d", (int) pos,
191 ui_slider_length(slider));
192 if (rv < 0) {
193 printf("Out of memory.\n");
194 return;
195 }
196
197 rc = ui_entry_set_text(demo->entry, str);
198 if (rc != EOK)
199 printf("Error changing entry text.\n");
200 (void) ui_entry_paint(demo->entry);
201
202 free(str);
203}
204
[5e109e1]205/** Display a message window.
206 *
207 * @param demo UI demo
208 * @param caption Window caption
209 * @param text Message text
210 */
211static void uidemo_show_message(ui_demo_t *demo, const char *caption,
212 const char *text)
213{
214 ui_msg_dialog_params_t mdparams;
215 ui_msg_dialog_t *dialog;
216 errno_t rc;
217
218 ui_msg_dialog_params_init(&mdparams);
219 mdparams.caption = caption;
220 mdparams.text = text;
221
222 rc = ui_msg_dialog_create(demo->ui, &mdparams, &dialog);
223 if (rc != EOK) {
224 printf("Error creating message dialog.\n");
225 return;
226 }
227
228 ui_msg_dialog_set_cb(dialog, &msg_dialog_cb, &demo);
229}
230
231/** File/load menu entry selected.
232 *
233 * @param mentry Menu entry
234 * @param arg Argument (demo)
235 */
236static void uidemo_file_load(ui_menu_entry_t *mentry, void *arg)
237{
238 ui_demo_t *demo = (ui_demo_t *) arg;
239 ui_file_dialog_params_t fdparams;
240 ui_file_dialog_t *dialog;
241 errno_t rc;
242
243 ui_file_dialog_params_init(&fdparams);
244 fdparams.caption = "Load File";
245
246 rc = ui_file_dialog_create(demo->ui, &fdparams, &dialog);
247 if (rc != EOK) {
248 printf("Error creating message dialog.\n");
249 return;
250 }
251
252 ui_file_dialog_set_cb(dialog, &file_dialog_cb, demo);
253}
254
[252d03c]255/** File/message menu entry selected.
256 *
257 * @param mentry Menu entry
258 * @param arg Argument (demo)
259 */
260static void uidemo_file_message(ui_menu_entry_t *mentry, void *arg)
261{
262 ui_demo_t *demo = (ui_demo_t *) arg;
263 ui_msg_dialog_params_t mdparams;
264 ui_msg_dialog_t *dialog;
265 errno_t rc;
266
267 ui_msg_dialog_params_init(&mdparams);
268 mdparams.caption = "Message For You";
269 mdparams.text = "Hello, world!";
270
271 rc = ui_msg_dialog_create(demo->ui, &mdparams, &dialog);
272 if (rc != EOK) {
273 printf("Error creating message dialog.\n");
274 return;
275 }
276
277 ui_msg_dialog_set_cb(dialog, &msg_dialog_cb, &demo);
278}
279
[214aefb]280/** File/exit menu entry selected.
281 *
282 * @param mentry Menu entry
283 * @param arg Argument (demo)
284 */
285static void uidemo_file_exit(ui_menu_entry_t *mentry, void *arg)
286{
287 ui_demo_t *demo = (ui_demo_t *) arg;
288
289 ui_quit(demo->ui);
290}
291
[5e109e1]292/** File dialog OK button press.
293 *
294 * @param dialog File dialog
295 * @param arg Argument (ui_demo_t *)
296 * @param fname File name
297 */
298static void file_dialog_bok(ui_file_dialog_t *dialog, void *arg,
299 const char *fname)
300{
301 ui_demo_t *demo = (ui_demo_t *) arg;
302 char buf[128];
303 char *p;
304 FILE *f;
305
306 ui_file_dialog_destroy(dialog);
307
308 f = fopen(fname, "rt");
309 if (f == NULL) {
310 uidemo_show_message(demo, "Error", "Error opening file.");
311 return;
312 }
313
314 p = fgets(buf, sizeof(buf), f);
315 if (p == NULL) {
316 uidemo_show_message(demo, "Error", "Error reading file.");
317 fclose(f);
318 return;
319 }
320
321 /* Cut string off at the first non-printable character */
322 p = buf;
323 while (*p != '\0') {
324 if (*p < ' ') {
325 *p = '\0';
326 break;
327 }
328 ++p;
329 }
330
331 ui_entry_set_text(demo->entry, buf);
332 fclose(f);
333}
334
335/** File dialog cancel button press.
336 *
337 * @param dialog File dialog
338 * @param arg Argument (ui_demo_t *)
339 */
340static void file_dialog_bcancel(ui_file_dialog_t *dialog, void *arg)
341{
342 ui_demo_t *demo = (ui_demo_t *) arg;
343
344 (void) demo;
345 ui_file_dialog_destroy(dialog);
346}
347
348/** Message dialog close request.
349 *
350 * @param dialog File dialog
351 * @param arg Argument (ui_demo_t *)
352 */
353static void file_dialog_close(ui_file_dialog_t *dialog, void *arg)
354{
355 ui_demo_t *demo = (ui_demo_t *) arg;
356
357 (void) demo;
358 ui_file_dialog_destroy(dialog);
359}
360
[252d03c]361/** Message dialog button press.
362 *
363 * @param dialog Message dialog
364 * @param arg Argument (ui_demo_t *)
365 * @param bnum Button number
366 */
367static void msg_dialog_button(ui_msg_dialog_t *dialog, void *arg,
368 unsigned bnum)
369{
370 ui_demo_t *demo = (ui_demo_t *) arg;
371
372 (void) demo;
373 ui_msg_dialog_destroy(dialog);
374}
375
376/** Message dialog close request.
377 *
378 * @param dialog Message dialog
379 * @param arg Argument (ui_demo_t *)
380 */
381static void msg_dialog_close(ui_msg_dialog_t *dialog, void *arg)
382{
383 ui_demo_t *demo = (ui_demo_t *) arg;
384
385 (void) demo;
386 ui_msg_dialog_destroy(dialog);
387}
388
[47728678]389/** Run UI demo on display server. */
[d284ce9]390static errno_t ui_demo(const char *display_spec)
[47728678]391{
[d284ce9]392 ui_t *ui = NULL;
393 ui_wnd_params_t params;
394 ui_window_t *window = NULL;
[f6df5a3]395 ui_demo_t demo;
[47728678]396 gfx_rect_t rect;
[d8ddf7a]397 gfx_context_t *gc;
[3583ffb]398 ui_resource_t *ui_res;
[d8ddf7a]399 gfx_bitmap_params_t bparams;
400 gfx_bitmap_t *bitmap;
401 gfx_coord2_t off;
[252d03c]402 ui_menu_entry_t *mmsg;
[5e109e1]403 ui_menu_entry_t *mload;
[214aefb]404 ui_menu_entry_t *mfoo;
405 ui_menu_entry_t *mbar;
406 ui_menu_entry_t *mfoobar;
407 ui_menu_entry_t *mexit;
408 ui_menu_entry_t *mabout;
[47728678]409 errno_t rc;
410
[d284ce9]411 rc = ui_create(display_spec, &ui);
[47728678]412 if (rc != EOK) {
[d284ce9]413 printf("Error creating UI on display %s.\n", display_spec);
[47728678]414 return rc;
415 }
416
[252d03c]417 memset((void *) &demo, 0, sizeof(demo));
418 demo.ui = ui;
419
[d284ce9]420 ui_wnd_params_init(&params);
421 params.caption = "UI Demo";
[2d879f7]422 params.style |= ui_wds_resizable;
[47728678]423
[252d03c]424 /* FIXME: Auto layout */
425 if (ui_is_textmode(ui)) {
426 params.rect.p0.x = 0;
427 params.rect.p0.y = 0;
428 params.rect.p1.x = 80;
429 params.rect.p1.y = 25;
430 } else {
431 params.rect.p0.x = 0;
432 params.rect.p0.y = 0;
433 params.rect.p1.x = 220;
434 params.rect.p1.y = 350;
435 }
[1769693]436
[d284ce9]437 rc = ui_window_create(ui, &params, &window);
[47728678]438 if (rc != EOK) {
439 printf("Error creating window.\n");
440 return rc;
441 }
442
[d284ce9]443 ui_window_set_cb(window, &window_cb, (void *) &demo);
444 demo.window = window;
[47728678]445
[3583ffb]446 ui_res = ui_window_get_res(window);
[d8ddf7a]447 gc = ui_window_get_gc(window);
[3583ffb]448
[8009dc27]449 rc = ui_fixed_create(&demo.fixed);
450 if (rc != EOK) {
451 printf("Error creating fixed layout.\n");
452 return rc;
453 }
454
[c68c18b9]455 rc = ui_menu_bar_create(ui, window, &demo.mbar);
[214aefb]456 if (rc != EOK) {
457 printf("Error creating menu bar.\n");
458 return rc;
459 }
460
461 rc = ui_menu_create(demo.mbar, "File", &demo.mfile);
462 if (rc != EOK) {
463 printf("Error creating menu.\n");
464 return rc;
465 }
466
[252d03c]467 rc = ui_menu_entry_create(demo.mfile, "Message", "", &mmsg);
468 if (rc != EOK) {
469 printf("Error creating menu.\n");
470 return rc;
471 }
472
473 ui_menu_entry_set_cb(mmsg, uidemo_file_message, (void *) &demo);
474
[5e109e1]475 rc = ui_menu_entry_create(demo.mfile, "Load", "", &mload);
476 if (rc != EOK) {
477 printf("Error creating menu.\n");
478 return rc;
479 }
480
481 ui_menu_entry_set_cb(mload, uidemo_file_load, (void *) &demo);
482
[b8b64a8]483 rc = ui_menu_entry_create(demo.mfile, "Foo", "Ctrl-Alt-Del", &mfoo);
[214aefb]484 if (rc != EOK) {
485 printf("Error creating menu.\n");
486 return rc;
487 }
488
[b8b64a8]489 rc = ui_menu_entry_create(demo.mfile, "Bar", "", &mbar);
[214aefb]490 if (rc != EOK) {
491 printf("Error creating menu.\n");
492 return rc;
493 }
494
[b8b64a8]495 rc = ui_menu_entry_create(demo.mfile, "Foobar", "", &mfoobar);
[214aefb]496 if (rc != EOK) {
497 printf("Error creating menu.\n");
498 return rc;
499 }
500
[6186f9f]501 rc = ui_menu_entry_sep_create(demo.mfile, &mexit);
502 if (rc != EOK) {
503 printf("Error creating menu.\n");
504 return rc;
505 }
506
[b8b64a8]507 rc = ui_menu_entry_create(demo.mfile, "Exit", "Alt-F4", &mexit);
[214aefb]508 if (rc != EOK) {
509 printf("Error creating menu.\n");
510 return rc;
511 }
512
513 ui_menu_entry_set_cb(mexit, uidemo_file_exit, (void *) &demo);
514
515 rc = ui_menu_create(demo.mbar, "Edit", &demo.medit);
516 if (rc != EOK) {
517 printf("Error creating menu.\n");
518 return rc;
519 }
520
521 rc = ui_menu_create(demo.mbar, "Preferences", &demo.mpreferences);
522 if (rc != EOK) {
523 printf("Error creating menu.\n");
524 return rc;
525 }
526
527 rc = ui_menu_create(demo.mbar, "Help", &demo.mhelp);
528 if (rc != EOK) {
529 printf("Error creating menu.\n");
530 return rc;
531 }
532
[b8b64a8]533 rc = ui_menu_entry_create(demo.mhelp, "About", "Ctrl-H, F1", &mabout);
[214aefb]534 if (rc != EOK) {
535 printf("Error creating menu.\n");
536 return rc;
537 }
538
[252d03c]539 /* FIXME: Auto layout */
540 if (ui_is_textmode(ui)) {
541 rect.p0.x = 1;
[45004f3]542 rect.p0.y = 1;
[252d03c]543 rect.p1.x = 79;
[45004f3]544 rect.p1.y = 2;
[252d03c]545 } else {
546 rect.p0.x = 4;
547 rect.p0.y = 30;
548 rect.p1.x = 216;
549 rect.p1.y = 52;
550 }
[214aefb]551 ui_menu_bar_set_rect(demo.mbar, &rect);
552
553 rc = ui_fixed_add(demo.fixed, ui_menu_bar_ctl(demo.mbar));
554 if (rc != EOK) {
555 printf("Error adding control to layout.\n");
556 return rc;
557 }
558
[db3895d]559 rc = ui_entry_create(window, "", &demo.entry);
[214aefb]560 if (rc != EOK) {
561 printf("Error creating entry.\n");
562 return rc;
563 }
564
[a977e37]565 /* FIXME: Auto layout */
566 if (ui_is_textmode(ui)) {
567 rect.p0.x = 20;
568 rect.p0.y = 4;
569 rect.p1.x = 60;
570 rect.p1.y = 5;
571 } else {
572 rect.p0.x = 15;
573 rect.p0.y = 53;
574 rect.p1.x = 205;
575 rect.p1.y = 78;
576 }
577
[214aefb]578 ui_entry_set_rect(demo.entry, &rect);
579 ui_entry_set_halign(demo.entry, gfx_halign_center);
580
581 rc = ui_fixed_add(demo.fixed, ui_entry_ctl(demo.entry));
582 if (rc != EOK) {
583 printf("Error adding control to layout.\n");
584 return rc;
585 }
586
[d8ddf7a]587 rc = ui_label_create(ui_res, "Text label", &demo.label);
[ba09d06]588 if (rc != EOK) {
589 printf("Error creating label.\n");
590 return rc;
591 }
592
[a977e37]593 /* FIXME: Auto layout */
594 if (ui_is_textmode(ui)) {
595 rect.p0.x = 20;
596 rect.p0.y = 6;
597 rect.p1.x = 60;
598 rect.p1.y = 7;
599 } else {
600 rect.p0.x = 60;
601 rect.p0.y = 88;
602 rect.p1.x = 160;
603 rect.p1.y = 101;
604 }
605
[ba09d06]606 ui_label_set_rect(demo.label, &rect);
[58a67050]607 ui_label_set_halign(demo.label, gfx_halign_center);
[ba09d06]608
[8009dc27]609 rc = ui_fixed_add(demo.fixed, ui_label_ctl(demo.label));
610 if (rc != EOK) {
611 printf("Error adding control to layout.\n");
612 return rc;
613 }
614
[d8ddf7a]615 rc = ui_pbutton_create(ui_res, "OK", &demo.pb1);
[47728678]616 if (rc != EOK) {
617 printf("Error creating button.\n");
[f6df5a3]618 return rc;
[47728678]619 }
620
[8ef48ece]621 ui_pbutton_set_cb(demo.pb1, &pbutton_cb, (void *) &demo);
622
[a977e37]623 /* FIXME: Auto layout */
624 if (ui_is_textmode(ui)) {
625 rect.p0.x = 20;
626 rect.p0.y = 8;
627 rect.p1.x = 30;
628 rect.p1.y = 9;
629 } else {
630 rect.p0.x = 15;
631 rect.p0.y = 111;
632 rect.p1.x = 105;
633 rect.p1.y = 139;
634 }
635
[f6df5a3]636 ui_pbutton_set_rect(demo.pb1, &rect);
[47728678]637
[c9a7adc]638 ui_pbutton_set_default(demo.pb1, true);
639
[8009dc27]640 rc = ui_fixed_add(demo.fixed, ui_pbutton_ctl(demo.pb1));
641 if (rc != EOK) {
642 printf("Error adding control to layout.\n");
643 return rc;
644 }
645
[3583ffb]646 rc = ui_pbutton_create(ui_res, "Cancel", &demo.pb2);
[47728678]647 if (rc != EOK) {
648 printf("Error creating button.\n");
[f6df5a3]649 return rc;
[47728678]650 }
651
[8ef48ece]652 ui_pbutton_set_cb(demo.pb2, &pbutton_cb, (void *) &demo);
653
[a977e37]654 if (ui_is_textmode(ui)) {
655 rect.p0.x = 50;
656 rect.p0.y = 8;
657 rect.p1.x = 60;
658 rect.p1.y = 9;
659 } else {
660 rect.p0.x = 115;
661 rect.p0.y = 111;
662 rect.p1.x = 205;
663 rect.p1.y = 139;
664 }
665
[f6df5a3]666 ui_pbutton_set_rect(demo.pb2, &rect);
[47728678]667
[8009dc27]668 rc = ui_fixed_add(demo.fixed, ui_pbutton_ctl(demo.pb2));
669 if (rc != EOK) {
670 printf("Error adding control to layout.\n");
671 return rc;
672 }
673
[d8ddf7a]674 gfx_bitmap_params_init(&bparams);
[de0c55a]675 if (ui_is_textmode(ui)) {
676 bparams.rect.p0.x = 0;
677 bparams.rect.p0.y = 0;
678 bparams.rect.p1.x = 40;
679 bparams.rect.p1.y = 2;
680 } else {
681 bparams.rect.p0.x = 0;
682 bparams.rect.p0.y = 0;
683 bparams.rect.p1.x = 188;
684 bparams.rect.p1.y = 24;
685 }
[d8ddf7a]686
687 rc = gfx_bitmap_create(gc, &bparams, NULL, &bitmap);
688 if (rc != EOK)
689 return rc;
690
691 rc = bitmap_moire(bitmap, bparams.rect.p1.x, bparams.rect.p1.y);
692 if (rc != EOK)
693 return rc;
694
695 rc = ui_image_create(ui_res, bitmap, &params.rect, &demo.image);
696 if (rc != EOK) {
697 printf("Error creating label.\n");
698 return rc;
699 }
700
[de0c55a]701 if (ui_is_textmode(ui)) {
702 off.x = 20;
703 off.y = 10;
704 } else {
705 off.x = 15;
706 off.y = 155;
707 }
708
[d8ddf7a]709 gfx_rect_translate(&off, &bparams.rect, &rect);
710
711 /* Adjust for frame width (2 x 1 pixel) */
[de0c55a]712 if (!ui_is_textmode(ui)) {
713 ui_image_set_flags(demo.image, ui_imgf_frame);
714 rect.p1.x += 2;
715 rect.p1.y += 2;
716 }
717
[d8ddf7a]718 ui_image_set_rect(demo.image, &rect);
719
720 rc = ui_fixed_add(demo.fixed, ui_image_ctl(demo.image));
721 if (rc != EOK) {
722 printf("Error adding control to layout.\n");
723 return rc;
724 }
725
[5de71df]726 rc = ui_checkbox_create(ui_res, "Read only", &demo.checkbox);
[d70dc1c4]727 if (rc != EOK) {
728 printf("Error creating check box.\n");
729 return rc;
730 }
731
732 ui_checkbox_set_cb(demo.checkbox, &checkbox_cb, (void *) &demo);
733
[307d4d2]734 /* FIXME: Auto layout */
735 if (ui_is_textmode(ui)) {
736 rect.p0.x = 20;
[de0c55a]737 rect.p0.y = 13;
[307d4d2]738 rect.p1.x = 40;
[de0c55a]739 rect.p1.y = 14;
[307d4d2]740 } else {
741 rect.p0.x = 15;
742 rect.p0.y = 190;
743 rect.p1.x = 140;
744 rect.p1.y = 210;
745 }
[de0c55a]746
[d70dc1c4]747 ui_checkbox_set_rect(demo.checkbox, &rect);
748
749 rc = ui_fixed_add(demo.fixed, ui_checkbox_ctl(demo.checkbox));
750 if (rc != EOK) {
751 printf("Error adding control to layout.\n");
752 return rc;
753 }
754
[7020d1f]755 rc = ui_rbutton_group_create(ui_res, &demo.rbgroup);
756 if (rc != EOK) {
757 printf("Error creating radio button group.\n");
758 return rc;
759 }
760
[5de71df]761 rc = ui_rbutton_create(demo.rbgroup, "Left", (void *) &uidemo_halign[0],
762 &demo.rbleft);
[7020d1f]763 if (rc != EOK) {
764 printf("Error creating radio button.\n");
765 return rc;
766 }
767
768 ui_rbutton_group_set_cb(demo.rbgroup, &rbutton_group_cb,
769 (void *) &demo);
770
[297b1b3]771 /* FIXME: Auto layout */
772 if (ui_is_textmode(ui)) {
773 rect.p0.x = 20;
[de0c55a]774 rect.p0.y = 15;
[297b1b3]775 rect.p1.x = 40;
[de0c55a]776 rect.p1.y = 16;
[297b1b3]777 } else {
778 rect.p0.x = 15;
779 rect.p0.y = 220;
780 rect.p1.x = 140;
781 rect.p1.y = 240;
782 }
[5de71df]783 ui_rbutton_set_rect(demo.rbleft, &rect);
[7020d1f]784
[5de71df]785 rc = ui_fixed_add(demo.fixed, ui_rbutton_ctl(demo.rbleft));
[7020d1f]786 if (rc != EOK) {
787 printf("Error adding control to layout.\n");
788 return rc;
789 }
790
[5de71df]791 rc = ui_rbutton_create(demo.rbgroup, "Center", (void *) &uidemo_halign[1],
792 &demo.rbcenter);
[7020d1f]793 if (rc != EOK) {
794 printf("Error creating radio button.\n");
795 return rc;
796 }
797
[297b1b3]798 /* FIXME: Auto layout */
799 if (ui_is_textmode(ui)) {
800 rect.p0.x = 20;
[de0c55a]801 rect.p0.y = 16;
[297b1b3]802 rect.p1.x = 40;
[de0c55a]803 rect.p1.y = 17;
[297b1b3]804 } else {
805 rect.p0.x = 15;
806 rect.p0.y = 250;
807 rect.p1.x = 140;
808 rect.p1.y = 270;
809 }
[5de71df]810 ui_rbutton_set_rect(demo.rbcenter, &rect);
811 ui_rbutton_select(demo.rbcenter);
[7020d1f]812
[5de71df]813 rc = ui_fixed_add(demo.fixed, ui_rbutton_ctl(demo.rbcenter));
[7020d1f]814 if (rc != EOK) {
815 printf("Error adding control to layout.\n");
816 return rc;
817 }
818
[5de71df]819 rc = ui_rbutton_create(demo.rbgroup, "Right", (void *) &uidemo_halign[2],
820 &demo.rbright);
[7020d1f]821 if (rc != EOK) {
822 printf("Error creating radio button.\n");
823 return rc;
824 }
825
[297b1b3]826 /* FIXME: Auto layout */
827 if (ui_is_textmode(ui)) {
828 rect.p0.x = 20;
[de0c55a]829 rect.p0.y = 17;
[297b1b3]830 rect.p1.x = 40;
[de0c55a]831 rect.p1.y = 18;
[297b1b3]832 } else {
833 rect.p0.x = 15;
834 rect.p0.y = 280;
835 rect.p1.x = 140;
836 rect.p1.y = 300;
837 }
[5de71df]838 ui_rbutton_set_rect(demo.rbright, &rect);
[7020d1f]839
[5de71df]840 rc = ui_fixed_add(demo.fixed, ui_rbutton_ctl(demo.rbright));
[7020d1f]841 if (rc != EOK) {
842 printf("Error adding control to layout.\n");
843 return rc;
844 }
845
[ef734b7]846 rc = ui_slider_create(ui_res, "Slide!", &demo.slider);
847 if (rc != EOK) {
848 printf("Error creating button.\n");
849 return rc;
850 }
851
852 ui_slider_set_cb(demo.slider, &slider_cb, (void *) &demo);
853
[943f032]854 /* FIXME: Auto layout */
855 if (ui_is_textmode(ui)) {
856 rect.p0.x = 20;
[de0c55a]857 rect.p0.y = 19;
[943f032]858 rect.p1.x = 40;
[de0c55a]859 rect.p1.y = 20;
[943f032]860 } else {
861 rect.p0.x = 15;
862 rect.p0.y = 310;
863 rect.p1.x = 130;
864 rect.p1.y = 330;
865 }
866
[ef734b7]867 ui_slider_set_rect(demo.slider, &rect);
868
869 rc = ui_fixed_add(demo.fixed, ui_slider_ctl(demo.slider));
870 if (rc != EOK) {
871 printf("Error adding control to layout.\n");
872 return rc;
873 }
874
[b71c0fc]875 ui_window_add(window, ui_fixed_ctl(demo.fixed));
876
[fa01c05]877 rc = ui_window_paint(window);
[ba09d06]878 if (rc != EOK) {
[fa01c05]879 printf("Error painting window.\n");
[f6df5a3]880 return rc;
[47728678]881 }
882
[d284ce9]883 ui_run(ui);
[47728678]884
[d284ce9]885 ui_window_destroy(window);
886 ui_destroy(ui);
[47728678]887
888 return EOK;
889}
890
[d8ddf7a]891/** Fill bitmap with moire pattern.
892 *
893 * @param bitmap Bitmap
894 * @param w Bitmap width
895 * @param h Bitmap height
896 * @return EOK on success or an error code
897 */
898static errno_t bitmap_moire(gfx_bitmap_t *bitmap, gfx_coord_t w, gfx_coord_t h)
899{
900 int i, j;
901 int k;
902 pixelmap_t pixelmap;
903 gfx_bitmap_alloc_t alloc;
904 errno_t rc;
905
906 rc = gfx_bitmap_get_alloc(bitmap, &alloc);
907 if (rc != EOK)
908 return rc;
909
910 /* In absence of anything else, use pixelmap */
911 pixelmap.width = w;
912 pixelmap.height = h;
913 pixelmap.data = alloc.pixels;
914
915 for (i = 0; i < w; i++) {
916 for (j = 0; j < h; j++) {
917 k = i * i + j * j;
918 pixelmap_put_pixel(&pixelmap, i, j,
[de0c55a]919 PIXEL(0, k, k, 255 - k));
[d8ddf7a]920 }
921 }
922
923 return EOK;
924}
925
[d284ce9]926static void print_syntax(void)
927{
928 printf("Syntax: uidemo [-d <display-spec>]\n");
929}
930
[f80690a]931int main(int argc, char *argv[])
932{
[d284ce9]933 const char *display_spec = UI_DISPLAY_DEFAULT;
[f80690a]934 errno_t rc;
935 int i;
936
937 i = 1;
938 while (i < argc && argv[i][0] == '-') {
939 if (str_cmp(argv[i], "-d") == 0) {
940 ++i;
941 if (i >= argc) {
942 printf("Argument missing.\n");
943 print_syntax();
944 return 1;
945 }
946
[d284ce9]947 display_spec = argv[i++];
[f80690a]948 } else {
949 printf("Invalid option '%s'.\n", argv[i]);
950 print_syntax();
951 return 1;
952 }
953 }
954
955 if (i < argc) {
956 print_syntax();
957 return 1;
958 }
959
[d284ce9]960 rc = ui_demo(display_spec);
[47728678]961 if (rc != EOK)
[f80690a]962 return 1;
963
964 return 0;
965}
966
967/** @}
968 */
Note: See TracBrowser for help on using the repository browser.