HelenOS sources
This source file includes following definitions.
- ui_slider_create
- ui_slider_destroy
- ui_slider_ctl
- ui_slider_set_cb
- ui_slider_set_rect
- ui_slider_paint_frame
- ui_slider_paint_outset
- ui_slider_btn_rect
- ui_slider_length
- ui_slider_paint_gfx
- ui_slider_paint_text
- ui_slider_paint
- ui_slider_btn_clear
- ui_slider_press
- ui_slider_release
- ui_slider_update
- ui_slider_moved
- ui_slider_pos_event
- ui_slider_ctl_destroy
- ui_slider_ctl_paint
- ui_slider_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/slider.h>
#include "../private/resource.h"
#include "../private/slider.h"
enum {
ui_slider_btn_w = 15,
ui_slider_btn_h = 23,
ui_slider_btn_w_text = 3,
ui_slider_btn_h_text = 1,
ui_slider_btn_frame_thickness = 1,
ui_slider_btn_bevel_width = 2,
ui_slider_groove_width = 4,
ui_slider_groove_margin = 2
};
static void ui_slider_ctl_destroy(void *);
static errno_t ui_slider_ctl_paint(void *);
static ui_evclaim_t ui_slider_ctl_pos_event(void *, pos_event_t *);
ui_control_ops_t ui_slider_ops = {
.destroy = ui_slider_ctl_destroy,
.paint = ui_slider_ctl_paint,
.pos_event = ui_slider_ctl_pos_event
};
errno_t ui_slider_create(ui_resource_t *resource, ui_slider_t **rslider)
{
ui_slider_t *slider;
errno_t rc;
slider = calloc(1, sizeof(ui_slider_t));
if (slider == NULL)
return ENOMEM;
rc = ui_control_new(&ui_slider_ops, (void *) slider,
&slider->control);
if (rc != EOK) {
free(slider);
return rc;
}
slider->res = resource;
*rslider = slider;
return EOK;
}
void ui_slider_destroy(ui_slider_t *slider)
{
if (slider == NULL)
return;
ui_control_delete(slider->control);
free(slider);
}
ui_control_t *ui_slider_ctl(ui_slider_t *slider)
{
return slider->control;
}
void ui_slider_set_cb(ui_slider_t *slider, ui_slider_cb_t *cb, void *arg)
{
slider->cb = cb;
slider->arg = arg;
}
void ui_slider_set_rect(ui_slider_t *slider, gfx_rect_t *rect)
{
slider->rect = *rect;
}
static errno_t ui_slider_paint_frame(ui_resource_t *res, gfx_rect_t *rect,
gfx_coord_t thickness, gfx_rect_t *inside)
{
gfx_rect_t drect;
errno_t rc;
rc = gfx_set_color(res->gc, res->btn_frame_color);
if (rc != EOK)
goto error;
drect.p0.x = rect->p0.x + 1;
drect.p0.y = rect->p0.y;
drect.p1.x = rect->p1.x - 1;
drect.p1.y = rect->p0.y + thickness;
rc = gfx_fill_rect(res->gc, &drect);
if (rc != EOK)
goto error;
drect.p0.x = rect->p0.x + 1;
drect.p0.y = rect->p1.y - thickness;
drect.p1.x = rect->p1.x - 1;
drect.p1.y = rect->p1.y;
rc = gfx_fill_rect(res->gc, &drect);
if (rc != EOK)
goto error;
drect.p0.x = rect->p0.x;
drect.p0.y = rect->p0.y + 1;
drect.p1.x = rect->p0.x + thickness;
drect.p1.y = rect->p1.y - 1;
rc = gfx_fill_rect(res->gc, &drect);
if (rc != EOK)
goto error;
drect.p0.x = rect->p1.x - thickness;
drect.p0.y = rect->p0.y + 1;
drect.p1.x = rect->p1.x;
drect.p1.y = rect->p1.y - 1;
rc = gfx_fill_rect(res->gc, &drect);
if (rc != EOK)
goto error;
if (inside != NULL) {
inside->p0.x = rect->p0.x + thickness;
inside->p0.y = rect->p0.y + thickness;
inside->p1.x = rect->p1.x - thickness;
inside->p1.y = rect->p1.y - thickness;
}
return EOK;
error:
return rc;
}
static errno_t ui_slider_paint_outset(ui_slider_t *slider,
gfx_rect_t *rect, gfx_rect_t *inside)
{
return ui_paint_bevel(slider->res->gc, rect,
slider->res->btn_highlight_color,
slider->res->btn_shadow_color, ui_slider_btn_bevel_width, inside);
}
static void ui_slider_btn_rect(ui_slider_t *slider, gfx_rect_t *rect)
{
gfx_coord2_t pos;
pos.x = slider->rect.p0.x + slider->pos;
pos.y = slider->rect.p0.y;
rect->p0.x = pos.x;
rect->p0.y = pos.y;
if (slider->res->textmode) {
rect->p1.x = pos.x + ui_slider_btn_w_text;
rect->p1.y = pos.y + ui_slider_btn_h_text;
} else {
rect->p1.x = pos.x + ui_slider_btn_w;
rect->p1.y = pos.y + ui_slider_btn_h;
}
}
gfx_coord_t ui_slider_length(ui_slider_t *slider)
{
gfx_coord2_t dims;
gfx_coord_t w;
gfx_rect_dims(&slider->rect, &dims);
w = slider->res->textmode ? ui_slider_btn_w_text :
ui_slider_btn_w;
return dims.x - w;
}
errno_t ui_slider_paint_gfx(ui_slider_t *slider)
{
gfx_coord2_t pos;
gfx_coord_t length;
gfx_rect_t rect;
gfx_rect_t brect;
gfx_rect_t irect;
errno_t rc;
pos = slider->rect.p0;
length = ui_slider_length(slider);
rect.p0.x = pos.x + ui_slider_btn_w / 2 - ui_slider_groove_margin;
rect.p0.y = pos.y + ui_slider_btn_h / 2 - ui_slider_groove_width / 2;
rect.p1.x = pos.x + ui_slider_btn_w / 2 + length +
ui_slider_groove_margin;
rect.p1.y = pos.y + ui_slider_btn_h / 2 + ui_slider_groove_width / 2;
rc = ui_paint_inset_frame(slider->res, &rect, NULL);
if (rc != EOK)
goto error;
ui_slider_btn_rect(slider, &rect);
rc = ui_slider_paint_frame(slider->res, &rect,
ui_slider_btn_frame_thickness, &brect);
if (rc != EOK)
goto error;
rc = ui_slider_paint_outset(slider, &brect, &irect);
if (rc != EOK)
goto error;
rc = gfx_set_color(slider->res->gc, slider->res->btn_face_color);
if (rc != EOK)
goto error;
rc = gfx_fill_rect(slider->res->gc, &irect);
if (rc != EOK)
goto error;
rc = gfx_update(slider->res->gc);
if (rc != EOK)
goto error;
return EOK;
error:
return rc;
}
errno_t ui_slider_paint_text(ui_slider_t *slider)
{
gfx_coord2_t pos;
gfx_text_fmt_t fmt;
gfx_coord_t w, i;
char *buf;
const char *gchar;
size_t gcharsz;
errno_t rc;
pos = slider->rect.p0;
gfx_text_fmt_init(&fmt);
fmt.font = slider->res->font;
fmt.color = slider->res->wnd_text_color;
fmt.halign = gfx_halign_left;
fmt.valign = gfx_valign_top;
w = slider->rect.p1.x - slider->rect.p0.x;
gchar = "\u2550";
gcharsz = str_size(gchar);
buf = malloc(w * gcharsz + 1);
if (buf == NULL)
return ENOMEM;
for (i = 0; i < w; i++)
str_cpy(buf + i * gcharsz, (w - i) * gcharsz + 1, gchar);
buf[w * gcharsz] = '\0';
rc = gfx_puttext(&pos, &fmt, buf);
free(buf);
if (rc != EOK)
goto error;
pos.x += slider->pos;
rc = gfx_puttext(&pos, &fmt, "[O]");
if (rc != EOK)
goto error;
rc = gfx_update(slider->res->gc);
if (rc != EOK)
goto error;
return EOK;
error:
return rc;
}
errno_t ui_slider_paint(ui_slider_t *slider)
{
if (slider->res->textmode)
return ui_slider_paint_text(slider);
else
return ui_slider_paint_gfx(slider);
}
errno_t ui_slider_btn_clear(ui_slider_t *slider)
{
gfx_rect_t rect;
errno_t rc;
if (slider->res->textmode)
return EOK;
ui_slider_btn_rect(slider, &rect);
rc = gfx_set_color(slider->res->gc, slider->res->wnd_face_color);
if (rc != EOK)
goto error;
rc = gfx_fill_rect(slider->res->gc, &rect);
if (rc != EOK)
goto error;
return EOK;
error:
return rc;
}
void ui_slider_press(ui_slider_t *slider, gfx_coord2_t *pos)
{
if (slider->held)
return;
slider->held = true;
slider->press_pos = *pos;
slider->last_pos = slider->pos;
(void) ui_slider_paint(slider);
}
void ui_slider_release(ui_slider_t *slider, gfx_coord2_t *pos)
{
if (!slider->held)
return;
ui_slider_update(slider, pos);
slider->held = false;
}
void ui_slider_update(ui_slider_t *slider, gfx_coord2_t *pos)
{
gfx_coord_t spos;
gfx_coord_t length;
if (slider->held) {
spos = slider->last_pos + pos->x - slider->press_pos.x;
length = ui_slider_length(slider);
if (spos < 0)
spos = 0;
if (spos > length)
spos = length;
if (spos != slider->pos) {
(void) ui_slider_btn_clear(slider);
slider->pos = spos;
(void) ui_slider_paint(slider);
ui_slider_moved(slider, spos);
}
}
}
void ui_slider_moved(ui_slider_t *slider, gfx_coord_t pos)
{
if (slider->cb != NULL && slider->cb->moved != NULL)
slider->cb->moved(slider, slider->arg, pos);
}
ui_evclaim_t ui_slider_pos_event(ui_slider_t *slider, pos_event_t *event)
{
gfx_coord2_t pos;
gfx_rect_t rect;
pos.x = event->hpos;
pos.y = event->vpos;
switch (event->type) {
case POS_PRESS:
ui_slider_btn_rect(slider, &rect);
if (gfx_pix_inside_rect(&pos, &rect)) {
ui_slider_press(slider, &pos);
slider->press_pos = pos;
return ui_claimed;
}
break;
case POS_RELEASE:
if (slider->held) {
ui_slider_release(slider, &pos);
return ui_claimed;
}
break;
case POS_UPDATE:
ui_slider_update(slider, &pos);
break;
case POS_DCLICK:
break;
}
return ui_unclaimed;
}
void ui_slider_ctl_destroy(void *arg)
{
ui_slider_t *slider = (ui_slider_t *) arg;
ui_slider_destroy(slider);
}
errno_t ui_slider_ctl_paint(void *arg)
{
ui_slider_t *slider = (ui_slider_t *) arg;
return ui_slider_paint(slider);
}
ui_evclaim_t ui_slider_ctl_pos_event(void *arg, pos_event_t *event)
{
ui_slider_t *slider = (ui_slider_t *) arg;
return ui_slider_pos_event(slider, event);
}
HelenOS homepage, sources at GitHub