source: mainline/uspace/app/aboutos/aboutos.c@ 9f945464

Last change on this file since 9f945464 was 93de384, checked in by Wayne Thornton <wmthornton-dev@…>, 5 months ago

Created UI program 'date_cfg' for graphical setting of date/time

Set 'aboutos' dialog box to always start in center screen.

  • Property mode set to 100644
File size: 8.3 KB
Line 
1/*
2 * Copyright (c) 2024 Jiri Svoboda
3 * Copyright (c) 2012 Petr Koupy
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup aboutos
31 * @{
32 */
33/** @file About HelenOS
34 */
35
36#include <errno.h>
37#include <gfx/coord.h>
38#include <gfximage/tga.h>
39#include <macros.h>
40#include <stdbool.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <str.h>
44#include <str_error.h>
45#include <ui/fixed.h>
46#include <ui/image.h>
47#include <ui/label.h>
48#include <ui/pbutton.h>
49#include <ui/resource.h>
50#include <ui/ui.h>
51#include <ui/window.h>
52
53#include "aboutos.h"
54#include "images.h"
55
56#define NAME "aboutos"
57
58static void aboutos_wnd_close(ui_window_t *, void *);
59static void aboutos_wnd_kbd(ui_window_t *, void *, kbd_event_t *);
60
61static ui_window_cb_t window_cb = {
62 .close = aboutos_wnd_close,
63 .kbd = aboutos_wnd_kbd
64};
65
66static void pb_clicked(ui_pbutton_t *, void *);
67
68static ui_pbutton_cb_t pbutton_cb = {
69 .clicked = pb_clicked
70};
71
72/** Window close button was clicked.
73 *
74 * @param window Window
75 * @param arg Argument (aboutos)
76 */
77static void aboutos_wnd_close(ui_window_t *window, void *arg)
78{
79 aboutos_t *aboutos = (aboutos_t *) arg;
80
81 ui_quit(aboutos->ui);
82}
83
84/** About HelenOS window keyboard event handler.
85 *
86 * @param window Window
87 * @param arg Argument (ui_prompt_dialog_t *)
88 * @param event Keyboard event
89 */
90static void aboutos_wnd_kbd(ui_window_t *window, void *arg,
91 kbd_event_t *event)
92{
93 aboutos_t *aboutos = (aboutos_t *) arg;
94
95 (void)window;
96
97 if (event->type == KEY_PRESS &&
98 (event->mods & (KM_CTRL | KM_SHIFT | KM_ALT)) == 0) {
99 if (event->key == KC_ENTER) {
100 /* Quit */
101 ui_quit(aboutos->ui);
102
103 }
104 }
105
106 ui_window_def_kbd(window, event);
107}
108
109/** Push button was clicked.
110 *
111 * @param pbutton Push button
112 * @param arg Argument (aboutos)
113 */
114static void pb_clicked(ui_pbutton_t *pbutton, void *arg)
115{
116 aboutos_t *aboutos = (aboutos_t *) arg;
117
118 ui_quit(aboutos->ui);
119}
120
121static void print_syntax(void)
122{
123 printf("Syntax: %s [-d <display-spec>]\n", NAME);
124}
125
126int main(int argc, char *argv[])
127{
128 int i;
129 aboutos_t aboutos;
130 ui_t *ui = NULL;
131 ui_wnd_params_t params;
132 ui_window_t *window = NULL;
133 ui_resource_t *ui_res;
134 gfx_bitmap_params_t logo_params;
135 gfx_bitmap_t *logo_bmp;
136 gfx_context_t *gc;
137 gfx_rect_t logo_rect;
138 gfx_rect_t rect;
139 gfx_coord2_t off;
140 const char *dspec = UI_ANY_DEFAULT;
141 errno_t rc;
142
143 i = 1;
144 while (i < argc) {
145 if (str_cmp(argv[i], "-d") == 0) {
146 ++i;
147 if (i >= argc) {
148 printf("Argument missing.\n");
149 print_syntax();
150 return 1;
151 }
152
153 dspec = argv[i++];
154 } else {
155 printf("Invalid option '%s'.\n", argv[i]);
156 print_syntax();
157 return 1;
158 }
159 }
160
161 rc = ui_create(dspec, &ui);
162 if (rc != EOK) {
163 printf("Error creating UI on display %s.\n", dspec);
164 return rc;
165 }
166
167 ui_wnd_params_init(&params);
168 params.caption = "About HelenOS";
169 params.placement = ui_wnd_place_center;
170
171 /* FIXME: Auto layout */
172 if (ui_is_textmode(ui)) {
173 params.rect.p0.x = 0;
174 params.rect.p0.y = 0;
175 params.rect.p1.x = 45;
176 params.rect.p1.y = 15;
177 } else {
178 params.rect.p0.x = 0;
179 params.rect.p0.y = 0;
180 params.rect.p1.x = 350;
181 params.rect.p1.y = 275;
182 }
183
184 memset((void *) &aboutos, 0, sizeof(aboutos));
185 aboutos.ui = ui;
186
187 rc = ui_window_create(ui, &params, &window);
188 if (rc != EOK) {
189 printf("Error creating window.\n");
190 return rc;
191 }
192
193 ui_window_set_cb(window, &window_cb, (void *) &aboutos);
194 aboutos.window = window;
195
196 ui_res = ui_window_get_res(window);
197 gc = ui_window_get_gc(window);
198
199 rc = decode_tga(gc, (void *) helenos_tga, helenos_tga_size,
200 &logo_bmp, &logo_rect);
201 if (rc != EOK) {
202 printf("Unable to decode logo.\n");
203 return 1;
204 }
205
206 gfx_bitmap_params_init(&logo_params);
207 logo_params.rect = logo_rect;
208
209 rc = ui_fixed_create(&aboutos.fixed);
210 if (rc != EOK) {
211 printf("Error creating fixed layout.\n");
212 return rc;
213 }
214
215 rc = ui_image_create(ui_res, logo_bmp, &logo_rect, &aboutos.image);
216 if (rc != EOK) {
217 printf("Error creating label.\n");
218 return rc;
219 }
220
221 off.x = 76;
222 off.y = 42;
223 gfx_rect_translate(&off, &logo_rect, &rect);
224
225 /* Adjust for frame width (2 x 1 pixel) */
226 rect.p1.x += 2;
227 rect.p1.y += 2;
228 ui_image_set_rect(aboutos.image, &rect);
229 ui_image_set_flags(aboutos.image, ui_imgf_frame);
230
231 rc = ui_fixed_add(aboutos.fixed, ui_image_ctl(aboutos.image));
232 if (rc != EOK) {
233 printf("Error adding control to layout.\n");
234 return rc;
235 }
236
237 /* Release label */
238
239 rc = ui_label_create(ui_res, "HelenOS " STRING(HELENOS_RELEASE)
240 " (" STRING(HELENOS_CODENAME) ")", &aboutos.lrelease);
241 if (rc != EOK) {
242 printf("Error creating label.\n");
243 return rc;
244 }
245
246 if (ui_is_textmode(ui)) {
247 rect.p0.x = 1;
248 rect.p0.y = 5;
249 rect.p1.x = 44;
250 rect.p1.y = 6;
251 } else {
252 rect.p0.x = 10;
253 rect.p0.y = 140;
254 rect.p1.x = 340;
255 rect.p1.y = 160;
256 }
257
258 ui_label_set_rect(aboutos.lrelease, &rect);
259 ui_label_set_halign(aboutos.lrelease, gfx_halign_center);
260
261 rc = ui_fixed_add(aboutos.fixed, ui_label_ctl(aboutos.lrelease));
262 if (rc != EOK) {
263 printf("Error adding control to layout.\n");
264 return rc;
265 }
266
267 /* Copyright label */
268
269 rc = ui_label_create(ui_res, STRING(HELENOS_COPYRIGHT), &aboutos.lcopy);
270 if (rc != EOK) {
271 printf("Error creating label.\n");
272 return rc;
273 }
274
275 if (ui_is_textmode(ui)) {
276 rect.p0.x = 1;
277 rect.p0.y = 6;
278 rect.p1.x = 44;
279 rect.p1.y = 7;
280 } else {
281 rect.p0.x = 10;
282 rect.p0.y = 160;
283 rect.p1.x = 340;
284 rect.p1.y = 180;
285 }
286
287 ui_label_set_rect(aboutos.lcopy, &rect);
288 ui_label_set_halign(aboutos.lcopy, gfx_halign_center);
289
290 rc = ui_fixed_add(aboutos.fixed, ui_label_ctl(aboutos.lcopy));
291 if (rc != EOK) {
292 printf("Error adding control to layout.\n");
293 return rc;
294 }
295
296 /* Architecture label */
297
298 rc = ui_label_create(ui_res, "Running on " STRING(UARCH), &aboutos.larch);
299 if (rc != EOK) {
300 printf("Error creating label.\n");
301 return rc;
302 }
303
304 if (ui_is_textmode(ui)) {
305 rect.p0.x = 1;
306 rect.p0.y = 9;
307 rect.p1.x = 44;
308 rect.p1.y = 10;
309 } else {
310 rect.p0.x = 10;
311 rect.p0.y = 190;
312 rect.p1.x = 340;
313 rect.p1.y = 210;
314 }
315
316 ui_label_set_rect(aboutos.larch, &rect);
317 ui_label_set_halign(aboutos.larch, gfx_halign_center);
318
319 rc = ui_fixed_add(aboutos.fixed, ui_label_ctl(aboutos.larch));
320 if (rc != EOK) {
321 printf("Error adding control to layout.\n");
322 return rc;
323 }
324
325 /* OK button */
326
327 rc = ui_pbutton_create(ui_res, "OK", &aboutos.pbok);
328 if (rc != EOK) {
329 printf("Error creating button.\n");
330 return rc;
331 }
332
333 ui_pbutton_set_cb(aboutos.pbok, &pbutton_cb, (void *) &aboutos);
334
335 if (ui_is_textmode(ui)) {
336 rect.p0.x = 17;
337 rect.p0.y = 13;
338 rect.p1.x = 28;
339 rect.p1.y = 14;
340 } else {
341 rect.p0.x = 125;
342 rect.p0.y = 235;
343 rect.p1.x = 225;
344 rect.p1.y = rect.p0.y + 28;
345 }
346
347 ui_pbutton_set_rect(aboutos.pbok, &rect);
348 ui_pbutton_set_default(aboutos.pbok, true);
349
350 rc = ui_fixed_add(aboutos.fixed, ui_pbutton_ctl(aboutos.pbok));
351 if (rc != EOK) {
352 printf("Error adding control to layout.\n");
353 return rc;
354 }
355
356 ui_window_add(window, ui_fixed_ctl(aboutos.fixed));
357
358 rc = ui_window_paint(window);
359 if (rc != EOK) {
360 printf("Error painting window.\n");
361 return rc;
362 }
363
364 ui_run(ui);
365
366 ui_window_destroy(window);
367 ui_destroy(ui);
368
369 return 0;
370}
371
372/** @}
373 */
Note: See TracBrowser for help on using the repository browser.