aboutsummaryrefslogtreecommitdiff
path: root/src/button.c
blob: b11f5b16774aeae797b89991167361de909811ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <glib.h>

#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);
}