1 | /*
|
---|
2 | * Copyright (c) 2021 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 |
|
---|
35 | #include <gfx/bitmap.h>
|
---|
36 | #include <gfx/coord.h>
|
---|
37 | #include <io/pixelmap.h>
|
---|
38 | #include <stdio.h>
|
---|
39 | #include <stdlib.h>
|
---|
40 | #include <str.h>
|
---|
41 | #include <ui/entry.h>
|
---|
42 | #include <ui/filedialog.h>
|
---|
43 | #include <ui/fixed.h>
|
---|
44 | #include <ui/image.h>
|
---|
45 | #include <ui/label.h>
|
---|
46 | #include <ui/menubar.h>
|
---|
47 | #include <ui/menuentry.h>
|
---|
48 | #include <ui/menu.h>
|
---|
49 | #include <ui/msgdialog.h>
|
---|
50 | #include <ui/pbutton.h>
|
---|
51 | #include <ui/resource.h>
|
---|
52 | #include <ui/ui.h>
|
---|
53 | #include <ui/window.h>
|
---|
54 | #include "uidemo.h"
|
---|
55 |
|
---|
56 | static errno_t bitmap_moire(gfx_bitmap_t *, gfx_coord_t, gfx_coord_t);
|
---|
57 |
|
---|
58 | static void wnd_close(ui_window_t *, void *);
|
---|
59 |
|
---|
60 | static ui_window_cb_t window_cb = {
|
---|
61 | .close = wnd_close
|
---|
62 | };
|
---|
63 |
|
---|
64 | static void pb_clicked(ui_pbutton_t *, void *);
|
---|
65 |
|
---|
66 | static ui_pbutton_cb_t pbutton_cb = {
|
---|
67 | .clicked = pb_clicked
|
---|
68 | };
|
---|
69 |
|
---|
70 | static void checkbox_switched(ui_checkbox_t *, void *, bool);
|
---|
71 |
|
---|
72 | static ui_checkbox_cb_t checkbox_cb = {
|
---|
73 | .switched = checkbox_switched
|
---|
74 | };
|
---|
75 |
|
---|
76 | static void rb_selected(ui_rbutton_group_t *, void *, void *);
|
---|
77 |
|
---|
78 | static ui_rbutton_group_cb_t rbutton_group_cb = {
|
---|
79 | .selected = rb_selected
|
---|
80 | };
|
---|
81 |
|
---|
82 | static void slider_moved(ui_slider_t *, void *, gfx_coord_t);
|
---|
83 |
|
---|
84 | static ui_slider_cb_t slider_cb = {
|
---|
85 | .moved = slider_moved
|
---|
86 | };
|
---|
87 |
|
---|
88 | static void uidemo_file_load(ui_menu_entry_t *, void *);
|
---|
89 | static void uidemo_file_message(ui_menu_entry_t *, void *);
|
---|
90 | static void uidemo_file_exit(ui_menu_entry_t *, void *);
|
---|
91 |
|
---|
92 | static void file_dialog_bok(ui_file_dialog_t *, void *, const char *);
|
---|
93 | static void file_dialog_bcancel(ui_file_dialog_t *, void *);
|
---|
94 | static void file_dialog_close(ui_file_dialog_t *, void *);
|
---|
95 |
|
---|
96 | static 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 |
|
---|
102 | static void msg_dialog_button(ui_msg_dialog_t *, void *, unsigned);
|
---|
103 | static void msg_dialog_close(ui_msg_dialog_t *, void *);
|
---|
104 |
|
---|
105 | static ui_msg_dialog_cb_t msg_dialog_cb = {
|
---|
106 | .button = msg_dialog_button,
|
---|
107 | .close = msg_dialog_close
|
---|
108 | };
|
---|
109 |
|
---|
110 | /** Horizontal alignment selected by each radio button */
|
---|
111 | static const gfx_halign_t uidemo_halign[3] = {
|
---|
112 | gfx_halign_left,
|
---|
113 | gfx_halign_center,
|
---|
114 | gfx_halign_right
|
---|
115 | };
|
---|
116 |
|
---|
117 | /** Window close button was clicked.
|
---|
118 | *
|
---|
119 | * @param window Window
|
---|
120 | * @param arg Argument (demo)
|
---|
121 | */
|
---|
122 | static void wnd_close(ui_window_t *window, void *arg)
|
---|
123 | {
|
---|
124 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
125 |
|
---|
126 | ui_quit(demo->ui);
|
---|
127 | }
|
---|
128 |
|
---|
129 | /** Push button was clicked.
|
---|
130 | *
|
---|
131 | * @param pbutton Push button
|
---|
132 | * @param arg Argument (demo)
|
---|
133 | */
|
---|
134 | static void pb_clicked(ui_pbutton_t *pbutton, void *arg)
|
---|
135 | {
|
---|
136 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
137 | errno_t rc;
|
---|
138 |
|
---|
139 | if (pbutton == demo->pb1) {
|
---|
140 | rc = ui_entry_set_text(demo->entry, "OK pressed");
|
---|
141 | if (rc != EOK)
|
---|
142 | printf("Error changing entry text.\n");
|
---|
143 | } else {
|
---|
144 | rc = ui_entry_set_text(demo->entry, "Cancel pressed");
|
---|
145 | if (rc != EOK)
|
---|
146 | printf("Error changing entry text.\n");
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | /** Check box was switched.
|
---|
151 | *
|
---|
152 | * @param checkbox Check box
|
---|
153 | * @param arg Argument (demo)
|
---|
154 | */
|
---|
155 | static void checkbox_switched(ui_checkbox_t *checkbox, void *arg, bool enable)
|
---|
156 | {
|
---|
157 | ui_demo_t *demo = (ui_demo_t *) arg;
|
---|
158 |
|
---|
159 | ui_entry_set_read_only(demo->entry, enable);
|
---|
160 | }
|
---|
161 |
|
---|
162 | /** Radio button was selected.
|
---|
163 | *
|
---|
164 | * @param rbgroup Radio button group
|
---|
165 | * @param garg Group argument (demo)
|
---|
166 | * @param barg Button argument
|
---|
167 | */
|
---|
168 | static void rb_selected(ui_rbutton_group_t *rbgroup, void *garg, void *barg)
|
---|
169 | {
|
---|
170 | ui_demo_t *demo = (ui_demo_t *) garg;
|
---|
171 | gfx_halign_t halign = *(gfx_halign_t *) barg;
|
---|
172 |
|
---|
173 | ui_entry_set_halign(demo->entry, halign);
|
---|
174 | (void) ui_entry_paint(demo->entry);
|
---|
175 | }
|
---|
176 |
|
---|
177 | /** Slider was moved.
|
---|
178 | *
|
---|
179 | * @param slider Slider
|
---|
180 | * @param arg Argument (demo)
|
---|
181 | * @param pos Position
|
---|
182 | */
|
---|
183 | static 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 |
|
---|
205 | /** Display a message window.
|
---|
206 | *
|
---|
207 | * @param demo UI demo
|
---|
208 | * @param caption Window caption
|
---|
209 | * @param text Message text
|
---|
210 | */
|
---|
211 | static 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 | */
|
---|
236 | static 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 |
|
---|
255 | /** File/message menu entry selected.
|
---|
256 | *
|
---|
257 | * @param mentry Menu entry
|
---|
258 | * @param arg Argument (demo)
|
---|
259 | */
|
---|
260 | static 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 |
|
---|
280 | /** File/exit menu entry selected.
|
---|
281 | *
|
---|
282 | * @param mentry Menu entry
|
---|
283 | * @param arg Argument (demo)
|
---|
284 | */
|
---|
285 | static 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 |
|
---|
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 | */
|
---|
298 | static 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 | */
|
---|
340 | static 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 | */
|
---|
353 | static 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 |
|
---|
361 | /** Message dialog button press.
|
---|
362 | *
|
---|
363 | * @param dialog Message dialog
|
---|
364 | * @param arg Argument (ui_demo_t *)
|
---|
365 | * @param bnum Button number
|
---|
366 | */
|
---|
367 | static 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 | */
|
---|
381 | static 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 |
|
---|
389 | /** Run UI demo on display server. */
|
---|
390 | static errno_t ui_demo(const char *display_spec)
|
---|
391 | {
|
---|
392 | ui_t *ui = NULL;
|
---|
393 | ui_wnd_params_t params;
|
---|
394 | ui_window_t *window = NULL;
|
---|
395 | ui_demo_t demo;
|
---|
396 | gfx_rect_t rect;
|
---|
397 | gfx_context_t *gc;
|
---|
398 | ui_resource_t *ui_res;
|
---|
399 | gfx_bitmap_params_t bparams;
|
---|
400 | gfx_bitmap_t *bitmap;
|
---|
401 | gfx_coord2_t off;
|
---|
402 | ui_menu_entry_t *mmsg;
|
---|
403 | ui_menu_entry_t *mload;
|
---|
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;
|
---|
409 | errno_t rc;
|
---|
410 |
|
---|
411 | rc = ui_create(display_spec, &ui);
|
---|
412 | if (rc != EOK) {
|
---|
413 | printf("Error creating UI on display %s.\n", display_spec);
|
---|
414 | return rc;
|
---|
415 | }
|
---|
416 |
|
---|
417 | memset((void *) &demo, 0, sizeof(demo));
|
---|
418 | demo.ui = ui;
|
---|
419 |
|
---|
420 | ui_wnd_params_init(¶ms);
|
---|
421 | params.caption = "UI Demo";
|
---|
422 | params.style |= ui_wds_resizable;
|
---|
423 |
|
---|
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 | }
|
---|
436 |
|
---|
437 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
438 | if (rc != EOK) {
|
---|
439 | printf("Error creating window.\n");
|
---|
440 | return rc;
|
---|
441 | }
|
---|
442 |
|
---|
443 | ui_window_set_cb(window, &window_cb, (void *) &demo);
|
---|
444 | demo.window = window;
|
---|
445 |
|
---|
446 | ui_res = ui_window_get_res(window);
|
---|
447 | gc = ui_window_get_gc(window);
|
---|
448 |
|
---|
449 | rc = ui_fixed_create(&demo.fixed);
|
---|
450 | if (rc != EOK) {
|
---|
451 | printf("Error creating fixed layout.\n");
|
---|
452 | return rc;
|
---|
453 | }
|
---|
454 |
|
---|
455 | rc = ui_menu_bar_create(ui, window, &demo.mbar);
|
---|
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 |
|
---|
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 |
|
---|
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 |
|
---|
483 | rc = ui_menu_entry_create(demo.mfile, "Foo", "Ctrl-Alt-Del", &mfoo);
|
---|
484 | if (rc != EOK) {
|
---|
485 | printf("Error creating menu.\n");
|
---|
486 | return rc;
|
---|
487 | }
|
---|
488 |
|
---|
489 | rc = ui_menu_entry_create(demo.mfile, "Bar", "", &mbar);
|
---|
490 | if (rc != EOK) {
|
---|
491 | printf("Error creating menu.\n");
|
---|
492 | return rc;
|
---|
493 | }
|
---|
494 |
|
---|
495 | rc = ui_menu_entry_create(demo.mfile, "Foobar", "", &mfoobar);
|
---|
496 | if (rc != EOK) {
|
---|
497 | printf("Error creating menu.\n");
|
---|
498 | return rc;
|
---|
499 | }
|
---|
500 |
|
---|
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 |
|
---|
507 | rc = ui_menu_entry_create(demo.mfile, "Exit", "Alt-F4", &mexit);
|
---|
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 |
|
---|
533 | rc = ui_menu_entry_create(demo.mhelp, "About", "Ctrl-H, F1", &mabout);
|
---|
534 | if (rc != EOK) {
|
---|
535 | printf("Error creating menu.\n");
|
---|
536 | return rc;
|
---|
537 | }
|
---|
538 |
|
---|
539 | /* FIXME: Auto layout */
|
---|
540 | if (ui_is_textmode(ui)) {
|
---|
541 | rect.p0.x = 1;
|
---|
542 | rect.p0.y = 1;
|
---|
543 | rect.p1.x = 79;
|
---|
544 | rect.p1.y = 2;
|
---|
545 | } else {
|
---|
546 | rect.p0.x = 4;
|
---|
547 | rect.p0.y = 30;
|
---|
548 | rect.p1.x = 216;
|
---|
549 | rect.p1.y = 52;
|
---|
550 | }
|
---|
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 |
|
---|
559 | rc = ui_entry_create(window, "", &demo.entry);
|
---|
560 | if (rc != EOK) {
|
---|
561 | printf("Error creating entry.\n");
|
---|
562 | return rc;
|
---|
563 | }
|
---|
564 |
|
---|
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 |
|
---|
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 |
|
---|
587 | rc = ui_label_create(ui_res, "Text label", &demo.label);
|
---|
588 | if (rc != EOK) {
|
---|
589 | printf("Error creating label.\n");
|
---|
590 | return rc;
|
---|
591 | }
|
---|
592 |
|
---|
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 |
|
---|
606 | ui_label_set_rect(demo.label, &rect);
|
---|
607 | ui_label_set_halign(demo.label, gfx_halign_center);
|
---|
608 |
|
---|
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 |
|
---|
615 | rc = ui_pbutton_create(ui_res, "OK", &demo.pb1);
|
---|
616 | if (rc != EOK) {
|
---|
617 | printf("Error creating button.\n");
|
---|
618 | return rc;
|
---|
619 | }
|
---|
620 |
|
---|
621 | ui_pbutton_set_cb(demo.pb1, &pbutton_cb, (void *) &demo);
|
---|
622 |
|
---|
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 |
|
---|
636 | ui_pbutton_set_rect(demo.pb1, &rect);
|
---|
637 |
|
---|
638 | ui_pbutton_set_default(demo.pb1, true);
|
---|
639 |
|
---|
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 |
|
---|
646 | rc = ui_pbutton_create(ui_res, "Cancel", &demo.pb2);
|
---|
647 | if (rc != EOK) {
|
---|
648 | printf("Error creating button.\n");
|
---|
649 | return rc;
|
---|
650 | }
|
---|
651 |
|
---|
652 | ui_pbutton_set_cb(demo.pb2, &pbutton_cb, (void *) &demo);
|
---|
653 |
|
---|
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 |
|
---|
666 | ui_pbutton_set_rect(demo.pb2, &rect);
|
---|
667 |
|
---|
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 |
|
---|
674 | gfx_bitmap_params_init(&bparams);
|
---|
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 | }
|
---|
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, ¶ms.rect, &demo.image);
|
---|
696 | if (rc != EOK) {
|
---|
697 | printf("Error creating label.\n");
|
---|
698 | return rc;
|
---|
699 | }
|
---|
700 |
|
---|
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 |
|
---|
709 | gfx_rect_translate(&off, &bparams.rect, &rect);
|
---|
710 |
|
---|
711 | /* Adjust for frame width (2 x 1 pixel) */
|
---|
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 |
|
---|
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 |
|
---|
726 | rc = ui_checkbox_create(ui_res, "Read only", &demo.checkbox);
|
---|
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 |
|
---|
734 | /* FIXME: Auto layout */
|
---|
735 | if (ui_is_textmode(ui)) {
|
---|
736 | rect.p0.x = 20;
|
---|
737 | rect.p0.y = 13;
|
---|
738 | rect.p1.x = 40;
|
---|
739 | rect.p1.y = 14;
|
---|
740 | } else {
|
---|
741 | rect.p0.x = 15;
|
---|
742 | rect.p0.y = 190;
|
---|
743 | rect.p1.x = 140;
|
---|
744 | rect.p1.y = 210;
|
---|
745 | }
|
---|
746 |
|
---|
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 |
|
---|
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 |
|
---|
761 | rc = ui_rbutton_create(demo.rbgroup, "Left", (void *) &uidemo_halign[0],
|
---|
762 | &demo.rbleft);
|
---|
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 |
|
---|
771 | /* FIXME: Auto layout */
|
---|
772 | if (ui_is_textmode(ui)) {
|
---|
773 | rect.p0.x = 20;
|
---|
774 | rect.p0.y = 15;
|
---|
775 | rect.p1.x = 40;
|
---|
776 | rect.p1.y = 16;
|
---|
777 | } else {
|
---|
778 | rect.p0.x = 15;
|
---|
779 | rect.p0.y = 220;
|
---|
780 | rect.p1.x = 140;
|
---|
781 | rect.p1.y = 240;
|
---|
782 | }
|
---|
783 | ui_rbutton_set_rect(demo.rbleft, &rect);
|
---|
784 |
|
---|
785 | rc = ui_fixed_add(demo.fixed, ui_rbutton_ctl(demo.rbleft));
|
---|
786 | if (rc != EOK) {
|
---|
787 | printf("Error adding control to layout.\n");
|
---|
788 | return rc;
|
---|
789 | }
|
---|
790 |
|
---|
791 | rc = ui_rbutton_create(demo.rbgroup, "Center", (void *) &uidemo_halign[1],
|
---|
792 | &demo.rbcenter);
|
---|
793 | if (rc != EOK) {
|
---|
794 | printf("Error creating radio button.\n");
|
---|
795 | return rc;
|
---|
796 | }
|
---|
797 |
|
---|
798 | /* FIXME: Auto layout */
|
---|
799 | if (ui_is_textmode(ui)) {
|
---|
800 | rect.p0.x = 20;
|
---|
801 | rect.p0.y = 16;
|
---|
802 | rect.p1.x = 40;
|
---|
803 | rect.p1.y = 17;
|
---|
804 | } else {
|
---|
805 | rect.p0.x = 15;
|
---|
806 | rect.p0.y = 250;
|
---|
807 | rect.p1.x = 140;
|
---|
808 | rect.p1.y = 270;
|
---|
809 | }
|
---|
810 | ui_rbutton_set_rect(demo.rbcenter, &rect);
|
---|
811 | ui_rbutton_select(demo.rbcenter);
|
---|
812 |
|
---|
813 | rc = ui_fixed_add(demo.fixed, ui_rbutton_ctl(demo.rbcenter));
|
---|
814 | if (rc != EOK) {
|
---|
815 | printf("Error adding control to layout.\n");
|
---|
816 | return rc;
|
---|
817 | }
|
---|
818 |
|
---|
819 | rc = ui_rbutton_create(demo.rbgroup, "Right", (void *) &uidemo_halign[2],
|
---|
820 | &demo.rbright);
|
---|
821 | if (rc != EOK) {
|
---|
822 | printf("Error creating radio button.\n");
|
---|
823 | return rc;
|
---|
824 | }
|
---|
825 |
|
---|
826 | /* FIXME: Auto layout */
|
---|
827 | if (ui_is_textmode(ui)) {
|
---|
828 | rect.p0.x = 20;
|
---|
829 | rect.p0.y = 17;
|
---|
830 | rect.p1.x = 40;
|
---|
831 | rect.p1.y = 18;
|
---|
832 | } else {
|
---|
833 | rect.p0.x = 15;
|
---|
834 | rect.p0.y = 280;
|
---|
835 | rect.p1.x = 140;
|
---|
836 | rect.p1.y = 300;
|
---|
837 | }
|
---|
838 | ui_rbutton_set_rect(demo.rbright, &rect);
|
---|
839 |
|
---|
840 | rc = ui_fixed_add(demo.fixed, ui_rbutton_ctl(demo.rbright));
|
---|
841 | if (rc != EOK) {
|
---|
842 | printf("Error adding control to layout.\n");
|
---|
843 | return rc;
|
---|
844 | }
|
---|
845 |
|
---|
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 |
|
---|
854 | /* FIXME: Auto layout */
|
---|
855 | if (ui_is_textmode(ui)) {
|
---|
856 | rect.p0.x = 20;
|
---|
857 | rect.p0.y = 19;
|
---|
858 | rect.p1.x = 40;
|
---|
859 | rect.p1.y = 20;
|
---|
860 | } else {
|
---|
861 | rect.p0.x = 15;
|
---|
862 | rect.p0.y = 310;
|
---|
863 | rect.p1.x = 130;
|
---|
864 | rect.p1.y = 330;
|
---|
865 | }
|
---|
866 |
|
---|
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 |
|
---|
875 | ui_window_add(window, ui_fixed_ctl(demo.fixed));
|
---|
876 |
|
---|
877 | rc = ui_window_paint(window);
|
---|
878 | if (rc != EOK) {
|
---|
879 | printf("Error painting window.\n");
|
---|
880 | return rc;
|
---|
881 | }
|
---|
882 |
|
---|
883 | ui_run(ui);
|
---|
884 |
|
---|
885 | ui_window_destroy(window);
|
---|
886 | ui_destroy(ui);
|
---|
887 |
|
---|
888 | return EOK;
|
---|
889 | }
|
---|
890 |
|
---|
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 | */
|
---|
898 | static 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,
|
---|
919 | PIXEL(0, k, k, 255 - k));
|
---|
920 | }
|
---|
921 | }
|
---|
922 |
|
---|
923 | return EOK;
|
---|
924 | }
|
---|
925 |
|
---|
926 | static void print_syntax(void)
|
---|
927 | {
|
---|
928 | printf("Syntax: uidemo [-d <display-spec>]\n");
|
---|
929 | }
|
---|
930 |
|
---|
931 | int main(int argc, char *argv[])
|
---|
932 | {
|
---|
933 | const char *display_spec = UI_DISPLAY_DEFAULT;
|
---|
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 |
|
---|
947 | display_spec = argv[i++];
|
---|
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 |
|
---|
960 | rc = ui_demo(display_spec);
|
---|
961 | if (rc != EOK)
|
---|
962 | return 1;
|
---|
963 |
|
---|
964 | return 0;
|
---|
965 | }
|
---|
966 |
|
---|
967 | /** @}
|
---|
968 | */
|
---|