HelenOS sources
This source file includes following definitions.
- wnd_close
- hello
- print_syntax
- main
#include <gfx/coord.h>
#include <stdio.h>
#include <str.h>
#include <ui/fixed.h>
#include <ui/label.h>
#include <ui/resource.h>
#include <ui/ui.h>
#include <ui/window.h>
#include "hello.h"
static void wnd_close(ui_window_t *, void *);
static ui_window_cb_t window_cb = {
.close = wnd_close
};
static void wnd_close(ui_window_t *window, void *arg)
{
hello_t *hello = (hello_t *) arg;
ui_quit(hello->ui);
}
static errno_t hello(const char *display_spec)
{
ui_t *ui = NULL;
ui_wnd_params_t params;
ui_window_t *window = NULL;
hello_t hello;
gfx_rect_t rect;
ui_resource_t *ui_res;
errno_t rc;
rc = ui_create(display_spec, &ui);
if (rc != EOK) {
printf("Error creating UI on display %s.\n", display_spec);
return rc;
}
ui_wnd_params_init(¶ms);
params.caption = "Hello World";
if (ui_is_textmode(ui)) {
params.rect.p0.x = 0;
params.rect.p0.y = 0;
params.rect.p1.x = 24;
params.rect.p1.y = 5;
} else {
params.rect.p0.x = 0;
params.rect.p0.y = 0;
params.rect.p1.x = 200;
params.rect.p1.y = 60;
}
memset((void *) &hello, 0, sizeof(hello));
hello.ui = ui;
rc = ui_window_create(ui, ¶ms, &window);
if (rc != EOK) {
printf("Error creating window.\n");
return rc;
}
ui_window_set_cb(window, &window_cb, (void *) &hello);
hello.window = window;
ui_res = ui_window_get_res(window);
rc = ui_fixed_create(&hello.fixed);
if (rc != EOK) {
printf("Error creating fixed layout.\n");
return rc;
}
rc = ui_label_create(ui_res, "Hello, world!", &hello.label);
if (rc != EOK) {
printf("Error creating label.\n");
return rc;
}
ui_window_get_app_rect(window, &rect);
ui_label_set_rect(hello.label, &rect);
ui_label_set_halign(hello.label, gfx_halign_center);
ui_label_set_valign(hello.label, gfx_valign_center);
rc = ui_fixed_add(hello.fixed, ui_label_ctl(hello.label));
if (rc != EOK) {
printf("Error adding control to layout.\n");
return rc;
}
ui_window_add(window, ui_fixed_ctl(hello.fixed));
rc = ui_window_paint(window);
if (rc != EOK) {
printf("Error painting window.\n");
return rc;
}
ui_run(ui);
ui_window_destroy(window);
ui_destroy(ui);
return EOK;
}
static void print_syntax(void)
{
printf("Syntax: hello [-d <display-spec>]\n");
}
int main(int argc, char *argv[])
{
const char *display_spec = UI_ANY_DEFAULT;
errno_t rc;
int i;
i = 1;
while (i < argc && argv[i][0] == '-') {
if (str_cmp(argv[i], "-d") == 0) {
++i;
if (i >= argc) {
printf("Argument missing.\n");
print_syntax();
return 1;
}
display_spec = argv[i++];
} else {
printf("Invalid option '%s'.\n", argv[i]);
print_syntax();
return 1;
}
}
if (i < argc) {
print_syntax();
return 1;
}
rc = hello(display_spec);
if (rc != EOK)
return 1;
return 0;
}
HelenOS homepage, sources at GitHub