#include <stdio.h>
#include <stdlib.h>
#include <caca.h>
#include <gtcaca/custom.h>
#include <gtcaca/main.h>
gtcaca_custom_widget_t *gtcaca_custom_new(gtcaca_widget_t *parent, int x, int y, int width, int height)
{
gtcaca_custom_widget_t *c = malloc(sizeof *c);
if (!c) { fprintf(stderr, "Error: cannot allocate custom widget\n"); return NULL; }
c->id = gtcaca_get_newid();
c->has_focus = 0;
c->is_visible = 1;
c->type = GTCACA_WIDGET_CUSTOM;
c->parent = parent;
c->children = NULL;
gtcaca_widget_position_size_parent(parent, GTCACA_WIDGET(c), x, y);
c->width = width;
c->height = height;
c->color_focus_fg = gmo.theme.text.fg;
c->color_focus_bg = gmo.theme.text.bg;
c->color_nonfocus_fg = gmo.theme.text.fg;
c->color_nonfocus_bg = gmo.theme.text.bg;
c->draw_cb = NULL;
c->draw_cb_userdata = NULL;
c->key_cb = NULL;
c->key_cb_userdata = NULL;
c->focusable = 1;
CDL_APPEND(gmo.widgets_list, GTCACA_WIDGET(c));
return c;
}
void gtcaca_custom_free(gtcaca_custom_widget_t *widget)
{
if (!widget) return;
CDL_DELETE(gmo.widgets_list, GTCACA_WIDGET(widget));
free(widget);
}
void gtcaca_custom_set_draw_cb(gtcaca_custom_widget_t *widget, gtcaca_custom_draw_cb_t cb, void *userdata)
{
if (!widget) return;
widget->draw_cb = cb;
widget->draw_cb_userdata = userdata;
}
void gtcaca_custom_set_key_cb(gtcaca_custom_widget_t *widget, gtcaca_custom_key_cb_t cb, void *userdata)
{
if (!widget) return;
widget->key_cb = cb;
widget->key_cb_userdata = userdata;
}
void gtcaca_custom_set_focusable(gtcaca_custom_widget_t *widget, int focusable)
{
if (!widget) return;
widget->focusable = focusable;
}
void gtcaca_custom_draw(gtcaca_custom_widget_t *widget)
{
if (!widget || !widget->is_visible) return;
if (widget->draw_cb)
widget->draw_cb(widget, widget->draw_cb_userdata);
}