source: mainline/uspace/app/uidemo/uidemo.c@ d55ab823

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

Return task return code from ui_run()

  • Property mode set to 100644
File size: 5.8 KB
Line 
1/*
2 * Copyright (c) 2020 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup uidemo
30 * @{
31 */
32/** @file User interface demo
33 */
34
35#include <gfx/coord.h>
36#include <stdio.h>
37#include <str.h>
38#include <ui/fixed.h>
39#include <ui/label.h>
40#include <ui/pbutton.h>
41#include <ui/resource.h>
42#include <ui/ui.h>
43#include <ui/window.h>
44#include "uidemo.h"
45
46static void wnd_close(ui_window_t *, void *);
47
48static ui_window_cb_t window_cb = {
49 .close = wnd_close
50};
51
52static void pb_clicked(ui_pbutton_t *, void *);
53
54static ui_pbutton_cb_t pbutton_cb = {
55 .clicked = pb_clicked
56};
57
58/** Window close button was clicked.
59 *
60 * @param window Window
61 * @param arg Argument (demo)
62 */
63static void wnd_close(ui_window_t *window, void *arg)
64{
65 ui_demo_t *demo = (ui_demo_t *) arg;
66
67 ui_quit(demo->ui);
68}
69
70/** Push button was clicked.
71 *
72 * @param pbutton Push button
73 * @param arg Argument (demo)
74 */
75static void pb_clicked(ui_pbutton_t *pbutton, void *arg)
76{
77 ui_demo_t *demo = (ui_demo_t *) arg;
78 errno_t rc;
79
80 if (pbutton == demo->pb1) {
81 rc = ui_label_set_text(demo->label, "Confirmed");
82 if (rc != EOK)
83 printf("Error changing label text.\n");
84 (void) ui_label_paint(demo->label);
85 } else {
86 rc = ui_label_set_text(demo->label, "Cancelled");
87 if (rc != EOK)
88 printf("Error changing label text.\n");
89 (void) ui_label_paint(demo->label);
90 }
91}
92
93/** Run UI demo on display server. */
94static errno_t ui_demo(const char *display_spec)
95{
96 ui_t *ui = NULL;
97 ui_wnd_params_t params;
98 ui_window_t *window = NULL;
99 ui_demo_t demo;
100 gfx_rect_t rect;
101 ui_resource_t *ui_res;
102 errno_t rc;
103
104 rc = ui_create(display_spec, &ui);
105 if (rc != EOK) {
106 printf("Error creating UI on display %s.\n", display_spec);
107 return rc;
108 }
109
110 ui_wnd_params_init(&params);
111 params.caption = "UI Demo";
112 params.rect.p0.x = 0;
113 params.rect.p0.y = 0;
114 params.rect.p1.x = 220;
115 params.rect.p1.y = 100;
116
117 memset((void *) &demo, 0, sizeof(demo));
118 demo.ui = ui;
119
120 rc = ui_window_create(ui, &params, &window);
121 if (rc != EOK) {
122 printf("Error creating window.\n");
123 return rc;
124 }
125
126 ui_window_set_cb(window, &window_cb, (void *) &demo);
127 demo.window = window;
128
129 ui_res = ui_window_get_res(window);
130
131 rc = ui_fixed_create(&demo.fixed);
132 if (rc != EOK) {
133 printf("Error creating fixed layout.\n");
134 return rc;
135 }
136
137 rc = ui_label_create(ui_res, "Hello there!", &demo.label);
138 if (rc != EOK) {
139 printf("Error creating label.\n");
140 return rc;
141 }
142
143 rect.p0.x = 60;
144 rect.p0.y = 37;
145 rect.p1.x = 160;
146 rect.p1.y = 50;
147 ui_label_set_rect(demo.label, &rect);
148 ui_label_set_halign(demo.label, gfx_halign_center);
149
150 rc = ui_fixed_add(demo.fixed, ui_label_ctl(demo.label));
151 if (rc != EOK) {
152 printf("Error adding control to layout.\n");
153 return rc;
154 }
155
156 rc = ui_pbutton_create(ui_res, "Confirm", &demo.pb1);
157 if (rc != EOK) {
158 printf("Error creating button.\n");
159 return rc;
160 }
161
162 ui_pbutton_set_cb(demo.pb1, &pbutton_cb, (void *) &demo);
163
164 rect.p0.x = 15;
165 rect.p0.y = 60;
166 rect.p1.x = 105;
167 rect.p1.y = 88;
168 ui_pbutton_set_rect(demo.pb1, &rect);
169
170 ui_pbutton_set_default(demo.pb1, true);
171
172 rc = ui_fixed_add(demo.fixed, ui_pbutton_ctl(demo.pb1));
173 if (rc != EOK) {
174 printf("Error adding control to layout.\n");
175 return rc;
176 }
177
178 rc = ui_pbutton_create(ui_res, "Cancel", &demo.pb2);
179 if (rc != EOK) {
180 printf("Error creating button.\n");
181 return rc;
182 }
183
184 ui_pbutton_set_cb(demo.pb2, &pbutton_cb, (void *) &demo);
185
186 rect.p0.x = 115;
187 rect.p0.y = 60;
188 rect.p1.x = 205;
189 rect.p1.y = 88;
190 ui_pbutton_set_rect(demo.pb2, &rect);
191
192 rc = ui_fixed_add(demo.fixed, ui_pbutton_ctl(demo.pb2));
193 if (rc != EOK) {
194 printf("Error adding control to layout.\n");
195 return rc;
196 }
197
198 ui_window_add(window, ui_fixed_ctl(demo.fixed));
199
200 rc = ui_window_paint(window);
201 if (rc != EOK) {
202 printf("Error painting window.\n");
203 return rc;
204 }
205
206 ui_run(ui);
207
208 ui_window_destroy(window);
209 ui_destroy(ui);
210
211 return EOK;
212}
213
214static void print_syntax(void)
215{
216 printf("Syntax: uidemo [-d <display-spec>]\n");
217}
218
219int main(int argc, char *argv[])
220{
221 const char *display_spec = UI_DISPLAY_DEFAULT;
222 errno_t rc;
223 int i;
224
225 i = 1;
226 while (i < argc && argv[i][0] == '-') {
227 if (str_cmp(argv[i], "-d") == 0) {
228 ++i;
229 if (i >= argc) {
230 printf("Argument missing.\n");
231 print_syntax();
232 return 1;
233 }
234
235 display_spec = argv[i++];
236 } else {
237 printf("Invalid option '%s'.\n", argv[i]);
238 print_syntax();
239 return 1;
240 }
241 }
242
243 if (i < argc) {
244 print_syntax();
245 return 1;
246 }
247
248 rc = ui_demo(display_spec);
249 if (rc != EOK)
250 return 1;
251
252 return 0;
253}
254
255/** @}
256 */
Note: See TracBrowser for help on using the repository browser.