#include #include "button.h" #include "log.h" Button *button_simple_create(PangoAlignment align, Color color) { Button *btn = g_malloc0(sizeof(ButtonSimple)); btn->simple = true; btn->align = align; btn->color = color; CAST(btn, ButtonSimple)->action = NULL; return btn; } void button_simple_set_text(Button *btn, char *text, Color text_color) { g_assert(btn->simple); ButtonSimple *sbtn = CAST(btn, ButtonSimple); g_free(sbtn->text); sbtn->text = text; sbtn->text_color = text_color; } const char *button_simple_get_text(Button *btn) { g_assert(btn->simple); return CAST(btn, ButtonSimple)->text; } void button_simple_set_action(Button *btn, ButtonAction action, gpointer data) { g_assert(btn->simple); CAST(btn, ButtonSimple)->action = action; CAST(btn, ButtonSimple)->data = data; } ButtonAction button_simple_get_action(Button *btn) { g_assert(btn->simple); return CAST(btn, ButtonSimple)->action; } Button *button_group_create(PangoAlignment align, Color color) { Button *btn = g_malloc0(sizeof(ButtonGroup)); btn->simple = false; btn->align = align; btn->color = color; return btn; } void button_group_append(Button *btn, Button *child) { g_assert(!btn->simple); CAST(btn, ButtonGroup)->children = g_list_append(CAST(btn, ButtonGroup)->children, child); } void button_set_padding(Button *btn, int x_pad, int y_pad) { g_assert(x_pad >= 0 && y_pad >= 0); btn->x_pad = x_pad; btn->y_pad = y_pad; } bool button_set_animation(Button *btn, Animation *anim) { if (btn->anim != NULL) return false; btn->anim = anim; return true; } void button_set_line(Button *btn, Color line_color, int line_width) { btn->line_color = line_color; btn->line_width = line_width; } void button_set_width(Button *btn, int max, int min) { g_assert(max == 0 || max >= min); g_assert(max >= 0 && min >= 0); btn->max_width = max; btn->min_width = min; } Button *button_copy(Button *btn) { if (btn->simple) { ButtonSimple *copy = g_malloc0(sizeof(ButtonSimple)); memcpy(copy, CAST(btn, ButtonSimple), sizeof(ButtonSimple)); copy->text = g_strdup(CAST(btn, ButtonSimple)->text); copy->btn.anim = NULL; // NOTE: What to do with data? return CAST(copy, Button); } else { ButtonGroup *copy = g_malloc0(sizeof(ButtonGroup)); memcpy(copy, CAST(btn, ButtonGroup), sizeof(ButtonGroup)); copy->btn.anim = NULL; copy->children = NULL; for (GList *it = CAST(btn, ButtonGroup)->children; it != NULL; it = it->next) { copy->children = g_list_prepend(copy->children, button_copy(it->data)); } copy->children = g_list_reverse(copy->children); return CAST(copy, Button); } } void button_destroy(Button *btn) { animation_destroy(btn->anim); if (btn->simple) g_free(CAST(btn, ButtonSimple)->text); else g_list_free_full(CAST(btn, ButtonGroup)->children, (GDestroyNotify)button_destroy); g_free(btn); }