HelenOS sources
This source file includes following definitions.
- ui_label_create
- ui_label_destroy
- ui_label_ctl
- ui_label_set_rect
- ui_label_set_halign
- ui_label_set_valign
- ui_label_set_text
- ui_label_paint
- ui_label_ctl_destroy
- ui_label_ctl_paint
- ui_label_ctl_pos_event
#include <errno.h>
#include <gfx/context.h>
#include <gfx/render.h>
#include <gfx/text.h>
#include <stdlib.h>
#include <str.h>
#include <ui/control.h>
#include <ui/paint.h>
#include <ui/label.h>
#include "../private/label.h"
#include "../private/resource.h"
static void ui_label_ctl_destroy(void *);
static errno_t ui_label_ctl_paint(void *);
static ui_evclaim_t ui_label_ctl_pos_event(void *, pos_event_t *);
ui_control_ops_t ui_label_ops = {
.destroy = ui_label_ctl_destroy,
.paint = ui_label_ctl_paint,
.pos_event = ui_label_ctl_pos_event
};
errno_t ui_label_create(ui_resource_t *resource, const char *text,
ui_label_t **rlabel)
{
ui_label_t *label;
errno_t rc;
label = calloc(1, sizeof(ui_label_t));
if (label == NULL)
return ENOMEM;
rc = ui_control_new(&ui_label_ops, (void *) label, &label->control);
if (rc != EOK) {
free(label);
return rc;
}
label->text = str_dup(text);
if (label->text == NULL) {
ui_control_delete(label->control);
free(label);
return ENOMEM;
}
label->res = resource;
label->halign = gfx_halign_left;
*rlabel = label;
return EOK;
}
void ui_label_destroy(ui_label_t *label)
{
if (label == NULL)
return;
ui_control_delete(label->control);
free(label);
}
ui_control_t *ui_label_ctl(ui_label_t *label)
{
return label->control;
}
void ui_label_set_rect(ui_label_t *label, gfx_rect_t *rect)
{
label->rect = *rect;
}
void ui_label_set_halign(ui_label_t *label, gfx_halign_t halign)
{
label->halign = halign;
}
void ui_label_set_valign(ui_label_t *label, gfx_valign_t valign)
{
label->valign = valign;
}
errno_t ui_label_set_text(ui_label_t *label, const char *text)
{
char *tcopy;
tcopy = str_dup(text);
if (tcopy == NULL)
return ENOMEM;
free(label->text);
label->text = tcopy;
return EOK;
}
errno_t ui_label_paint(ui_label_t *label)
{
gfx_text_fmt_t fmt;
gfx_coord2_t pos;
errno_t rc;
rc = gfx_set_color(label->res->gc, label->res->wnd_face_color);
if (rc != EOK)
goto error;
rc = gfx_fill_rect(label->res->gc, &label->rect);
if (rc != EOK)
goto error;
switch (label->halign) {
case gfx_halign_left:
case gfx_halign_justify:
pos.x = label->rect.p0.x;
break;
case gfx_halign_center:
pos.x = (label->rect.p0.x + label->rect.p1.x) / 2;
break;
case gfx_halign_right:
pos.x = label->rect.p1.x;
break;
}
switch (label->valign) {
case gfx_valign_top:
pos.y = label->rect.p0.y;
break;
case gfx_valign_center:
pos.y = (label->rect.p0.y + label->rect.p1.y) / 2;
break;
case gfx_valign_bottom:
pos.y = label->rect.p1.y;
break;
case gfx_valign_baseline:
return EINVAL;
}
gfx_text_fmt_init(&fmt);
fmt.font = label->res->font;
fmt.color = label->res->wnd_text_color;
fmt.halign = label->halign;
fmt.valign = label->valign;
rc = gfx_puttext(&pos, &fmt, label->text);
if (rc != EOK)
goto error;
rc = gfx_update(label->res->gc);
if (rc != EOK)
goto error;
return EOK;
error:
return rc;
}
void ui_label_ctl_destroy(void *arg)
{
ui_label_t *label = (ui_label_t *) arg;
ui_label_destroy(label);
}
errno_t ui_label_ctl_paint(void *arg)
{
ui_label_t *label = (ui_label_t *) arg;
return ui_label_paint(label);
}
ui_evclaim_t ui_label_ctl_pos_event(void *arg, pos_event_t *event)
{
ui_label_t *label = (ui_label_t *) arg;
(void) label;
return ui_unclaimed;
}
HelenOS homepage, sources at GitHub