HelenOS sources
This source file includes following definitions.
- ui_pbutton_create
- ui_pbutton_destroy
- ui_pbutton_ctl
- ui_pbutton_set_cb
- ui_pbutton_set_decor_ops
- ui_pbutton_set_flags
- ui_pbutton_set_rect
- ui_pbutton_set_default
- ui_pbutton_get_light
- ui_pbutton_set_light
- ui_pbutton_set_caption
- ui_pbutton_paint_frame
- ui_pbutton_paint_outset
- ui_pbutton_paint_inset
- ui_pbutton_paint_text_shadow
- ui_pbutton_paint_gfx
- ui_pbutton_paint_text
- ui_pbutton_paint
- ui_pbutton_press
- ui_pbutton_release
- ui_pbutton_enter
- ui_pbutton_leave
- ui_pbutton_clicked
- ui_pbutton_down
- ui_pbutton_up
- ui_pbutton_pos_event
- ui_pbutton_ctl_destroy
- ui_pbutton_ctl_paint
- ui_pbutton_ctl_pos_event
#include <errno.h>
#include <gfx/color.h>
#include <gfx/context.h>
#include <gfx/render.h>
#include <gfx/text.h>
#include <io/pos_event.h>
#include <stdlib.h>
#include <str.h>
#include <ui/control.h>
#include <ui/paint.h>
#include <ui/pbutton.h>
#include "../private/pbutton.h"
#include "../private/resource.h"
enum {
ui_pb_press_dx = 1,
ui_pb_press_dy = 1,
ui_pb_pad_x = 2,
ui_pb_pad_x_text = 1
};
static void ui_pbutton_ctl_destroy(void *);
static errno_t ui_pbutton_ctl_paint(void *);
static ui_evclaim_t ui_pbutton_ctl_pos_event(void *, pos_event_t *);
ui_control_ops_t ui_pbutton_ops = {
.destroy = ui_pbutton_ctl_destroy,
.paint = ui_pbutton_ctl_paint,
.pos_event = ui_pbutton_ctl_pos_event
};
errno_t ui_pbutton_create(ui_resource_t *resource, const char *caption,
ui_pbutton_t **rpbutton)
{
ui_pbutton_t *pbutton;
errno_t rc;
pbutton = calloc(1, sizeof(ui_pbutton_t));
if (pbutton == NULL)
return ENOMEM;
rc = ui_control_new(&ui_pbutton_ops, (void *) pbutton,
&pbutton->control);
if (rc != EOK) {
free(pbutton);
return rc;
}
pbutton->caption = str_dup(caption);
if (pbutton->caption == NULL) {
ui_control_delete(pbutton->control);
free(pbutton);
return ENOMEM;
}
pbutton->res = resource;
*rpbutton = pbutton;
return EOK;
}
void ui_pbutton_destroy(ui_pbutton_t *pbutton)
{
if (pbutton == NULL)
return;
ui_control_delete(pbutton->control);
free(pbutton->caption);
free(pbutton);
}
ui_control_t *ui_pbutton_ctl(ui_pbutton_t *pbutton)
{
return pbutton->control;
}
void ui_pbutton_set_cb(ui_pbutton_t *pbutton, ui_pbutton_cb_t *cb, void *arg)
{
pbutton->cb = cb;
pbutton->arg = arg;
}
void ui_pbutton_set_decor_ops(ui_pbutton_t *pbutton,
ui_pbutton_decor_ops_t *ops, void *arg)
{
pbutton->decor_ops = ops;
pbutton->decor_arg = arg;
}
void ui_pbutton_set_flags(ui_pbutton_t *pbutton, ui_pbutton_flags_t flags)
{
pbutton->flags = flags;
}
void ui_pbutton_set_rect(ui_pbutton_t *pbutton, gfx_rect_t *rect)
{
pbutton->rect = *rect;
}
void ui_pbutton_set_default(ui_pbutton_t *pbutton, bool isdefault)
{
pbutton->isdefault = isdefault;
}
bool ui_pbutton_get_light(ui_pbutton_t *pbutton)
{
return pbutton->light;
}
void ui_pbutton_set_light(ui_pbutton_t *pbutton, bool light)
{
pbutton->light = light;
}
errno_t ui_pbutton_set_caption(ui_pbutton_t *pbutton, const char *caption)
{
char *dcaption;
dcaption = str_dup(caption);
if (dcaption == NULL)
return ENOMEM;
free(pbutton->caption);
pbutton->caption = dcaption;
return EOK;
}
static errno_t ui_pbutton_paint_frame(ui_pbutton_t *pbutton)
{
gfx_rect_t rect;
gfx_coord_t thickness;
errno_t rc;
thickness = pbutton->isdefault ? 2 : 1;
rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_frame_color);
if (rc != EOK)
goto error;
rect.p0.x = pbutton->rect.p0.x + 1;
rect.p0.y = pbutton->rect.p0.y;
rect.p1.x = pbutton->rect.p1.x - 1;
rect.p1.y = pbutton->rect.p0.y + thickness;
rc = gfx_fill_rect(pbutton->res->gc, &rect);
if (rc != EOK)
goto error;
rect.p0.x = pbutton->rect.p0.x + 1;
rect.p0.y = pbutton->rect.p1.y - thickness;
rect.p1.x = pbutton->rect.p1.x - 1;
rect.p1.y = pbutton->rect.p1.y;
rc = gfx_fill_rect(pbutton->res->gc, &rect);
if (rc != EOK)
goto error;
rect.p0.x = pbutton->rect.p0.x;
rect.p0.y = pbutton->rect.p0.y + 1;
rect.p1.x = pbutton->rect.p0.x + thickness;
rect.p1.y = pbutton->rect.p1.y - 1;
rc = gfx_fill_rect(pbutton->res->gc, &rect);
if (rc != EOK)
goto error;
rect.p0.x = pbutton->rect.p1.x - thickness;
rect.p0.y = pbutton->rect.p0.y + 1;
rect.p1.x = pbutton->rect.p1.x;
rect.p1.y = pbutton->rect.p1.y - 1;
rc = gfx_fill_rect(pbutton->res->gc, &rect);
if (rc != EOK)
goto error;
return EOK;
error:
return rc;
}
static errno_t ui_pbutton_paint_outset(ui_pbutton_t *pbutton,
gfx_rect_t *rect)
{
return ui_paint_bevel(pbutton->res->gc, rect,
pbutton->res->btn_highlight_color,
pbutton->res->btn_shadow_color, 2, NULL);
}
static errno_t ui_pbutton_paint_inset(ui_pbutton_t *pbutton,
gfx_rect_t *rect)
{
return ui_paint_bevel(pbutton->res->gc, rect,
pbutton->res->btn_shadow_color,
pbutton->res->btn_face_color, 2, NULL);
}
static errno_t ui_pbutton_paint_text_shadow(ui_pbutton_t *pbutton)
{
gfx_rect_t rect;
errno_t rc;
rect.p0.x = pbutton->rect.p0.x + 1;
rect.p0.y = pbutton->rect.p0.y + 1;
rect.p1.x = pbutton->rect.p1.x;
rect.p1.y = pbutton->rect.p1.y;
rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_shadow_color);
if (rc != EOK)
goto error;
rc = gfx_fill_rect(pbutton->res->gc, &rect);
if (rc != EOK)
goto error;
return EOK;
error:
return rc;
}
static errno_t ui_pbutton_paint_gfx(ui_pbutton_t *pbutton)
{
gfx_coord2_t pos;
gfx_text_fmt_t fmt;
gfx_rect_t rect;
gfx_rect_t irect;
gfx_coord_t thickness;
gfx_color_t *color;
bool depressed;
errno_t rc;
thickness = pbutton->isdefault ? 2 : 1;
depressed = pbutton->held && pbutton->inside;
rect.p0.x = pbutton->rect.p0.x + thickness;
rect.p0.y = pbutton->rect.p0.y + thickness;
rect.p1.x = pbutton->rect.p1.x - thickness;
rect.p1.y = pbutton->rect.p1.y - thickness;
color = pbutton->light ? pbutton->res->btn_face_lit_color :
pbutton->res->btn_face_color;
rc = gfx_set_color(pbutton->res->gc, color);
if (rc != EOK)
goto error;
rc = gfx_fill_rect(pbutton->res->gc, &rect);
if (rc != EOK)
goto error;
pos.x = (rect.p0.x + rect.p1.x) / 2;
pos.y = (rect.p0.y + rect.p1.y) / 2;
if (depressed) {
pos.x += ui_pb_press_dx;
pos.y += ui_pb_press_dy;
}
if (pbutton->decor_ops != NULL && pbutton->decor_ops->paint != NULL) {
rc = pbutton->decor_ops->paint(pbutton, pbutton->decor_arg,
&pos);
if (rc != EOK)
goto error;
} else {
ui_paint_get_inset_frame_inside(pbutton->res, &rect, &irect);
gfx_text_fmt_init(&fmt);
fmt.font = pbutton->res->font;
fmt.color = pbutton->res->btn_text_color;
fmt.halign = gfx_halign_center;
fmt.valign = gfx_valign_center;
fmt.abbreviate = true;
fmt.width = irect.p1.x - irect.p0.x - 2 * ui_pb_pad_x;
rc = gfx_puttext(&pos, &fmt, pbutton->caption);
if (rc != EOK)
goto error;
}
rc = ui_pbutton_paint_frame(pbutton);
if (rc != EOK)
goto error;
if (depressed) {
rc = ui_pbutton_paint_inset(pbutton, &rect);
if (rc != EOK)
goto error;
} else {
rc = ui_pbutton_paint_outset(pbutton, &rect);
if (rc != EOK)
goto error;
}
rc = gfx_update(pbutton->res->gc);
if (rc != EOK)
goto error;
return EOK;
error:
return rc;
}
static errno_t ui_pbutton_paint_text(ui_pbutton_t *pbutton)
{
gfx_coord2_t pos;
gfx_text_fmt_t fmt;
gfx_rect_t rect;
bool depressed;
errno_t rc;
if ((pbutton->flags & ui_pbf_no_text_depress) == 0)
depressed = pbutton->held && pbutton->inside;
else
depressed = false;
rc = gfx_set_color(pbutton->res->gc, pbutton->res->wnd_face_color);
if (rc != EOK)
goto error;
rc = gfx_fill_rect(pbutton->res->gc, &pbutton->rect);
if (rc != EOK)
goto error;
rect.p0.x = pbutton->rect.p0.x + (depressed ? 1 : 0);
rect.p0.y = pbutton->rect.p0.y;
rect.p1.x = pbutton->rect.p1.x - 1 + (depressed ? 1 : 0);
rect.p1.y = pbutton->rect.p0.y + 1;
rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_face_color);
if (rc != EOK)
goto error;
rc = gfx_fill_rect(pbutton->res->gc, &rect);
if (rc != EOK)
goto error;
pos.x = (rect.p0.x + rect.p1.x) / 2;
pos.y = (rect.p0.y + rect.p1.y) / 2;
gfx_text_fmt_init(&fmt);
fmt.font = pbutton->res->font;
fmt.color = pbutton->res->btn_text_color;
fmt.halign = gfx_halign_center;
fmt.valign = gfx_valign_center;
fmt.abbreviate = true;
fmt.width = rect.p1.x - rect.p0.x - 2 * ui_pb_pad_x_text;
if (fmt.width < 1)
fmt.width = 1;
rc = gfx_puttext(&pos, &fmt, pbutton->caption);
if (rc != EOK)
goto error;
if (!depressed) {
rc = ui_pbutton_paint_text_shadow(pbutton);
if (rc != EOK)
goto error;
}
rc = gfx_update(pbutton->res->gc);
if (rc != EOK)
goto error;
return EOK;
error:
return rc;
}
errno_t ui_pbutton_paint(ui_pbutton_t *pbutton)
{
if (pbutton->res->textmode)
return ui_pbutton_paint_text(pbutton);
else
return ui_pbutton_paint_gfx(pbutton);
}
void ui_pbutton_press(ui_pbutton_t *pbutton)
{
if (pbutton->held)
return;
pbutton->inside = true;
pbutton->held = true;
(void) ui_pbutton_paint(pbutton);
ui_pbutton_down(pbutton);
}
void ui_pbutton_release(ui_pbutton_t *pbutton)
{
if (!pbutton->held)
return;
pbutton->held = false;
ui_pbutton_up(pbutton);
if (pbutton->inside) {
(void) ui_pbutton_paint(pbutton);
ui_pbutton_clicked(pbutton);
}
}
void ui_pbutton_enter(ui_pbutton_t *pbutton)
{
if (pbutton->inside)
return;
pbutton->inside = true;
if (pbutton->held)
(void) ui_pbutton_paint(pbutton);
}
void ui_pbutton_leave(ui_pbutton_t *pbutton)
{
if (!pbutton->inside)
return;
pbutton->inside = false;
if (pbutton->held)
(void) ui_pbutton_paint(pbutton);
}
void ui_pbutton_clicked(ui_pbutton_t *pbutton)
{
if (pbutton->cb != NULL && pbutton->cb->clicked != NULL)
pbutton->cb->clicked(pbutton, pbutton->arg);
}
void ui_pbutton_down(ui_pbutton_t *pbutton)
{
if (pbutton->cb != NULL && pbutton->cb->down != NULL)
pbutton->cb->down(pbutton, pbutton->arg);
}
void ui_pbutton_up(ui_pbutton_t *pbutton)
{
if (pbutton->cb != NULL && pbutton->cb->up != NULL)
pbutton->cb->up(pbutton, pbutton->arg);
}
ui_evclaim_t ui_pbutton_pos_event(ui_pbutton_t *pbutton, pos_event_t *event)
{
gfx_coord2_t pos;
bool inside;
pos.x = event->hpos;
pos.y = event->vpos;
inside = gfx_pix_inside_rect(&pos, &pbutton->rect);
switch (event->type) {
case POS_PRESS:
if (inside) {
ui_pbutton_press(pbutton);
return ui_claimed;
}
break;
case POS_RELEASE:
if (pbutton->held) {
ui_pbutton_release(pbutton);
return ui_claimed;
}
break;
case POS_UPDATE:
if (inside && !pbutton->inside) {
ui_pbutton_enter(pbutton);
return ui_claimed;
} else if (!inside && pbutton->inside) {
ui_pbutton_leave(pbutton);
}
break;
case POS_DCLICK:
break;
}
return ui_unclaimed;
}
void ui_pbutton_ctl_destroy(void *arg)
{
ui_pbutton_t *pbutton = (ui_pbutton_t *) arg;
ui_pbutton_destroy(pbutton);
}
errno_t ui_pbutton_ctl_paint(void *arg)
{
ui_pbutton_t *pbutton = (ui_pbutton_t *) arg;
return ui_pbutton_paint(pbutton);
}
ui_evclaim_t ui_pbutton_ctl_pos_event(void *arg, pos_event_t *event)
{
ui_pbutton_t *pbutton = (ui_pbutton_t *) arg;
return ui_pbutton_pos_event(pbutton, event);
}
HelenOS homepage, sources at GitHub