source: mainline/uspace/app/uidemo/uidemo.c@ 2d879f7

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

Basic support for window resizing

  • Property mode set to 100644
File size: 8.0 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/bitmap.h>
36#include <gfx/coord.h>
37#include <io/pixelmap.h>
38#include <stdio.h>
39#include <str.h>
40#include <ui/entry.h>
41#include <ui/fixed.h>
42#include <ui/image.h>
43#include <ui/label.h>
44#include <ui/pbutton.h>
45#include <ui/resource.h>
46#include <ui/ui.h>
47#include <ui/window.h>
48#include "uidemo.h"
49
50static errno_t bitmap_moire(gfx_bitmap_t *, gfx_coord_t, gfx_coord_t);
51
52static void wnd_close(ui_window_t *, void *);
53
54static ui_window_cb_t window_cb = {
55 .close = wnd_close
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/** Push button was clicked.
77 *
78 * @param pbutton Push button
79 * @param arg Argument (demo)
80 */
81static void pb_clicked(ui_pbutton_t *pbutton, void *arg)
82{
83 ui_demo_t *demo = (ui_demo_t *) arg;
84 errno_t rc;
85
86 if (pbutton == demo->pb1) {
87 rc = ui_entry_set_text(demo->entry, "OK pressed");
88 if (rc != EOK)
89 printf("Error changing entry text.\n");
90 (void) ui_entry_paint(demo->entry);
91 } else {
92 rc = ui_entry_set_text(demo->entry, "Cancel pressed");
93 if (rc != EOK)
94 printf("Error changing entry text.\n");
95 (void) ui_entry_paint(demo->entry);
96 }
97}
98
99/** Run UI demo on display server. */
100static errno_t ui_demo(const char *display_spec)
101{
102 ui_t *ui = NULL;
103 ui_wnd_params_t params;
104 ui_window_t *window = NULL;
105 ui_demo_t demo;
106 gfx_rect_t rect;
107 gfx_context_t *gc;
108 ui_resource_t *ui_res;
109 gfx_bitmap_params_t bparams;
110 gfx_bitmap_t *bitmap;
111 gfx_coord2_t off;
112 errno_t rc;
113
114 rc = ui_create(display_spec, &ui);
115 if (rc != EOK) {
116 printf("Error creating UI on display %s.\n", display_spec);
117 return rc;
118 }
119
120 ui_wnd_params_init(&params);
121 params.caption = "UI Demo";
122 params.style |= ui_wds_resizable;
123 params.rect.p0.x = 0;
124 params.rect.p0.y = 0;
125 params.rect.p1.x = 220;
126 params.rect.p1.y = 180;
127
128 memset((void *) &demo, 0, sizeof(demo));
129 demo.ui = ui;
130
131 rc = ui_window_create(ui, &params, &window);
132 if (rc != EOK) {
133 printf("Error creating window.\n");
134 return rc;
135 }
136
137 ui_window_set_cb(window, &window_cb, (void *) &demo);
138 demo.window = window;
139
140 ui_res = ui_window_get_res(window);
141 gc = ui_window_get_gc(window);
142
143 rc = ui_fixed_create(&demo.fixed);
144 if (rc != EOK) {
145 printf("Error creating fixed layout.\n");
146 return rc;
147 }
148
149 rc = ui_label_create(ui_res, "Text label", &demo.label);
150 if (rc != EOK) {
151 printf("Error creating label.\n");
152 return rc;
153 }
154
155 rect.p0.x = 60;
156 rect.p0.y = 37;
157 rect.p1.x = 160;
158 rect.p1.y = 50;
159 ui_label_set_rect(demo.label, &rect);
160 ui_label_set_halign(demo.label, gfx_halign_center);
161
162 rc = ui_fixed_add(demo.fixed, ui_label_ctl(demo.label));
163 if (rc != EOK) {
164 printf("Error adding control to layout.\n");
165 return rc;
166 }
167
168 rc = ui_pbutton_create(ui_res, "OK", &demo.pb1);
169 if (rc != EOK) {
170 printf("Error creating button.\n");
171 return rc;
172 }
173
174 ui_pbutton_set_cb(demo.pb1, &pbutton_cb, (void *) &demo);
175
176 rect.p0.x = 15;
177 rect.p0.y = 70;
178 rect.p1.x = 105;
179 rect.p1.y = 98;
180 ui_pbutton_set_rect(demo.pb1, &rect);
181
182 ui_pbutton_set_default(demo.pb1, true);
183
184 rc = ui_fixed_add(demo.fixed, ui_pbutton_ctl(demo.pb1));
185 if (rc != EOK) {
186 printf("Error adding control to layout.\n");
187 return rc;
188 }
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 = 70;
200 rect.p1.x = 205;
201 rect.p1.y = 98;
202 ui_pbutton_set_rect(demo.pb2, &rect);
203
204 rc = ui_fixed_add(demo.fixed, ui_pbutton_ctl(demo.pb2));
205 if (rc != EOK) {
206 printf("Error adding control to layout.\n");
207 return rc;
208 }
209
210 rc = ui_entry_create(ui_res, "", &demo.entry);
211 if (rc != EOK) {
212 printf("Error creating entry.\n");
213 return rc;
214 }
215
216 rect.p0.x = 15;
217 rect.p0.y = 110;
218 rect.p1.x = 205;
219 rect.p1.y = 135;
220 ui_entry_set_rect(demo.entry, &rect);
221 ui_entry_set_halign(demo.entry, gfx_halign_center);
222
223 rc = ui_fixed_add(demo.fixed, ui_entry_ctl(demo.entry));
224 if (rc != EOK) {
225 printf("Error adding control to layout.\n");
226 return rc;
227 }
228
229 gfx_bitmap_params_init(&bparams);
230 bparams.rect.p0.x = 0;
231 bparams.rect.p0.y = 0;
232 bparams.rect.p1.x = 188;
233 bparams.rect.p1.y = 24;
234
235 rc = gfx_bitmap_create(gc, &bparams, NULL, &bitmap);
236 if (rc != EOK)
237 return rc;
238
239 rc = bitmap_moire(bitmap, bparams.rect.p1.x, bparams.rect.p1.y);
240 if (rc != EOK)
241 return rc;
242
243 rc = ui_image_create(ui_res, bitmap, &params.rect, &demo.image);
244 if (rc != EOK) {
245 printf("Error creating label.\n");
246 return rc;
247 }
248
249 off.x = 15;
250 off.y = 145;
251 gfx_rect_translate(&off, &bparams.rect, &rect);
252
253 /* Adjust for frame width (2 x 1 pixel) */
254 rect.p1.x += 2;
255 rect.p1.y += 2;
256 ui_image_set_rect(demo.image, &rect);
257 ui_image_set_flags(demo.image, ui_imgf_frame);
258
259 rc = ui_fixed_add(demo.fixed, ui_image_ctl(demo.image));
260 if (rc != EOK) {
261 printf("Error adding control to layout.\n");
262 return rc;
263 }
264
265 ui_window_add(window, ui_fixed_ctl(demo.fixed));
266
267 rc = ui_window_paint(window);
268 if (rc != EOK) {
269 printf("Error painting window.\n");
270 return rc;
271 }
272
273 ui_run(ui);
274
275 ui_window_destroy(window);
276 ui_destroy(ui);
277
278 return EOK;
279}
280
281/** Fill bitmap with moire pattern.
282 *
283 * @param bitmap Bitmap
284 * @param w Bitmap width
285 * @param h Bitmap height
286 * @return EOK on success or an error code
287 */
288static errno_t bitmap_moire(gfx_bitmap_t *bitmap, gfx_coord_t w, gfx_coord_t h)
289{
290 int i, j;
291 int k;
292 pixelmap_t pixelmap;
293 gfx_bitmap_alloc_t alloc;
294 errno_t rc;
295
296 rc = gfx_bitmap_get_alloc(bitmap, &alloc);
297 if (rc != EOK)
298 return rc;
299
300 /* In absence of anything else, use pixelmap */
301 pixelmap.width = w;
302 pixelmap.height = h;
303 pixelmap.data = alloc.pixels;
304
305 for (i = 0; i < w; i++) {
306 for (j = 0; j < h; j++) {
307 k = i * i + j * j;
308 pixelmap_put_pixel(&pixelmap, i, j,
309 PIXEL(255, k, k, 255 - k));
310 }
311 }
312
313 return EOK;
314}
315
316static void print_syntax(void)
317{
318 printf("Syntax: uidemo [-d <display-spec>]\n");
319}
320
321int main(int argc, char *argv[])
322{
323 const char *display_spec = UI_DISPLAY_DEFAULT;
324 errno_t rc;
325 int i;
326
327 i = 1;
328 while (i < argc && argv[i][0] == '-') {
329 if (str_cmp(argv[i], "-d") == 0) {
330 ++i;
331 if (i >= argc) {
332 printf("Argument missing.\n");
333 print_syntax();
334 return 1;
335 }
336
337 display_spec = argv[i++];
338 } else {
339 printf("Invalid option '%s'.\n", argv[i]);
340 print_syntax();
341 return 1;
342 }
343 }
344
345 if (i < argc) {
346 print_syntax();
347 return 1;
348 }
349
350 rc = ui_demo(display_spec);
351 if (rc != EOK)
352 return 1;
353
354 return 0;
355}
356
357/** @}
358 */
Note: See TracBrowser for help on using the repository browser.