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

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

Let ui_window handle window decoration, display window

  • Property mode set to 100644
File size: 6.5 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/color.h>
36#include <gfx/coord.h>
37#include <gfx/render.h>
38#include <io/pos_event.h>
39#include <stdio.h>
40#include <str.h>
41#include <task.h>
42#include <ui/label.h>
43#include <ui/pbutton.h>
44#include <ui/resource.h>
45#include <ui/ui.h>
46#include <ui/wdecor.h>
47#include <ui/window.h>
48#include "uidemo.h"
49
50static void wnd_close(ui_window_t *, void *);
51static void wnd_pos(ui_window_t *, void *, pos_event_t *pos);
52
53static ui_window_cb_t window_cb = {
54 .close = wnd_close,
55 .pos = wnd_pos
56};
57
58static void pb_clicked(ui_pbutton_t *, void *);
59
60static ui_pbutton_cb_t pbutton_cb = {
61 .clicked = pb_clicked
62};
63
64/** Window close button was clicked.
65 *
66 * @param window Window
67 * @param arg Argument (demo)
68 */
69static void wnd_close(ui_window_t *window, void *arg)
70{
71 ui_demo_t *demo = (ui_demo_t *) arg;
72
73 ui_quit(demo->ui);
74}
75
76/** Window position event.
77 *
78 * @param window Window
79 * @param arg Argument (demo)
80 */
81static void wnd_pos(ui_window_t *window, void *arg, pos_event_t *event)
82{
83 ui_demo_t *demo = (ui_demo_t *) arg;
84
85 /* Make sure we don't process events until fully initialized */
86 if (demo->pb1 == NULL || demo->pb2 == NULL)
87 return;
88
89 ui_pbutton_pos_event(demo->pb1, event);
90 ui_pbutton_pos_event(demo->pb2, event);
91}
92
93/** Push button was clicked.
94 *
95 * @param pbutton Push button
96 * @param arg Argument (demo)
97 */
98static void pb_clicked(ui_pbutton_t *pbutton, void *arg)
99{
100 ui_demo_t *demo = (ui_demo_t *) arg;
101 errno_t rc;
102
103 if (pbutton == demo->pb1) {
104 rc = ui_label_set_text(demo->label, "Confirmed");
105 if (rc != EOK)
106 printf("Error changing label text.\n");
107 (void) ui_label_paint(demo->label);
108 } else {
109 rc = ui_label_set_text(demo->label, "Cancelled");
110 if (rc != EOK)
111 printf("Error changing label text.\n");
112 (void) ui_label_paint(demo->label);
113 }
114}
115
116/** Run UI demo on display server. */
117static errno_t ui_demo(const char *display_spec)
118{
119 ui_t *ui = NULL;
120 ui_wnd_params_t params;
121 ui_window_t *window = NULL;
122 ui_demo_t demo;
123 gfx_rect_t rect;
124 gfx_rect_t app_rect;
125 gfx_color_t *color;
126 ui_resource_t *ui_res;
127 gfx_context_t *gc;
128 errno_t rc;
129
130 rc = ui_create(display_spec, &ui);
131 if (rc != EOK) {
132 printf("Error creating UI on display %s.\n", display_spec);
133 return rc;
134 }
135
136 ui_wnd_params_init(&params);
137 params.caption = "UI Demo";
138 params.rect.p0.x = 0;
139 params.rect.p0.y = 0;
140 params.rect.p1.x = 220;
141 params.rect.p1.y = 100;
142
143 memset((void *) &demo, 0, sizeof(demo));
144 demo.ui = ui;
145
146 rc = ui_window_create(ui, &params, &window);
147 if (rc != EOK) {
148 printf("Error creating window.\n");
149 return rc;
150 }
151
152 ui_window_set_cb(window, &window_cb, (void *) &demo);
153 demo.window = window;
154
155 task_retval(0);
156
157 ui_res = ui_window_get_res(window);
158 gc = ui_window_get_gc(window);
159 ui_window_get_app_rect(window, &app_rect);
160
161 rc = ui_label_create(ui_res, "Hello there!", &demo.label);
162 if (rc != EOK) {
163 printf("Error creating label.\n");
164 return rc;
165 }
166
167 rect.p0.x = 60;
168 rect.p0.y = 37;
169 rect.p1.x = 160;
170 rect.p1.y = 50;
171 ui_label_set_rect(demo.label, &rect);
172 ui_label_set_halign(demo.label, gfx_halign_center);
173
174 rc = ui_pbutton_create(ui_res, "Confirm", &demo.pb1);
175 if (rc != EOK) {
176 printf("Error creating button.\n");
177 return rc;
178 }
179
180 ui_pbutton_set_cb(demo.pb1, &pbutton_cb, (void *) &demo);
181
182 rect.p0.x = 15;
183 rect.p0.y = 60;
184 rect.p1.x = 105;
185 rect.p1.y = 88;
186 ui_pbutton_set_rect(demo.pb1, &rect);
187
188 ui_pbutton_set_default(demo.pb1, true);
189
190 rc = ui_pbutton_create(ui_res, "Cancel", &demo.pb2);
191 if (rc != EOK) {
192 printf("Error creating button.\n");
193 return rc;
194 }
195
196 ui_pbutton_set_cb(demo.pb2, &pbutton_cb, (void *) &demo);
197
198 rect.p0.x = 115;
199 rect.p0.y = 60;
200 rect.p1.x = 205;
201 rect.p1.y = 88;
202 ui_pbutton_set_rect(demo.pb2, &rect);
203
204 rc = gfx_color_new_rgb_i16(0xc8c8, 0xc8c8, 0xc8c8, &color);
205 if (rc != EOK) {
206 printf("Error allocating color.\n");
207 return rc;
208 }
209
210 rc = gfx_set_color(gc, color);
211 if (rc != EOK) {
212 printf("Error setting color.\n");
213 return rc;
214 }
215
216 rc = gfx_fill_rect(gc, &app_rect);
217 if (rc != EOK) {
218 printf("Error filling background.\n");
219 return rc;
220 }
221
222 rc = ui_label_paint(demo.label);
223 if (rc != EOK) {
224 printf("Error painting button.\n");
225 return rc;
226 }
227
228 rc = ui_pbutton_paint(demo.pb1);
229 if (rc != EOK) {
230 printf("Error painting button.\n");
231 return rc;
232 }
233
234 rc = ui_pbutton_paint(demo.pb2);
235 if (rc != EOK) {
236 printf("Error painting button.\n");
237 return rc;
238 }
239
240 ui_run(ui);
241
242 ui_pbutton_destroy(demo.pb1);
243 ui_pbutton_destroy(demo.pb2);
244
245 ui_window_destroy(window);
246 ui_destroy(ui);
247
248 return EOK;
249}
250
251static void print_syntax(void)
252{
253 printf("Syntax: uidemo [-d <display-spec>]\n");
254}
255
256int main(int argc, char *argv[])
257{
258 const char *display_spec = UI_DISPLAY_DEFAULT;
259 errno_t rc;
260 int i;
261
262 i = 1;
263 while (i < argc && argv[i][0] == '-') {
264 if (str_cmp(argv[i], "-d") == 0) {
265 ++i;
266 if (i >= argc) {
267 printf("Argument missing.\n");
268 print_syntax();
269 return 1;
270 }
271
272 display_spec = argv[i++];
273 } else {
274 printf("Invalid option '%s'.\n", argv[i]);
275 print_syntax();
276 return 1;
277 }
278 }
279
280 if (i < argc) {
281 print_syntax();
282 return 1;
283 }
284
285 rc = ui_demo(display_spec);
286 if (rc != EOK)
287 return 1;
288
289 return 0;
290}
291
292/** @}
293 */
Note: See TracBrowser for help on using the repository browser.