summaryrefslogtreecommitdiff
path: root/src/widget/widget_choicegroup.c
blob: 540446181a363f9d4c9b3b5e4cf6543caa20db3b (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

#include <stdlib.h>
#include <assert.h>

#include "main.h"
#include "interface.h"
#include "image.h"
#include "widget_choicegroup.h"

widget_choicegroup_t* newWidgetChoicegroup(int x, int y, bool_t status, list_t *list, void (*fce_event)(void *))
{
	widget_choicegroup_t *new;

	new = malloc( sizeof(widget_choicegroup_t) );
	new->x = x;
	new->y = y;
	new->time = 0;
	new->status = status;
	new->fce_event = fce_event;
	new->list = list;
	addList(new->list, new);

	return new;
}

void drawWidgetChoicegroup(widget_choicegroup_t *p)
{
	static image_t *g_choicegroup = NULL;
	int x, y;
	int offset;

	getMousePosition(&x, &y);

	if( g_choicegroup == NULL )
	{
		g_choicegroup = addImageData("check.png", IMAGE_ALPHA, "choicegroup", IMAGE_GROUP_BASE);
	}

	if( p->status == TRUE )
	{
		offset = 0;
	}
	else
	{
		offset = WIDGET_CHOICEGROUP_WIDTH;
	}

	drawImage(g_choicegroup, p->x, p->y, offset, 0, WIDGET_CHOICEGROUP_WIDTH, WIDGET_CHOICEGROUP_HEIGHT);
}

static void setActive(widget_choicegroup_t *p)
{
	int i;

	for( i = 0 ; i < p->list->count ; i++ )
	{
		widget_choicegroup_t *this;

		this = (widget_choicegroup_t *) p->list->list[i];

		if( this == p )
		{
			this->status = TRUE;
			this->time = WIDGET_CHOICEGROUP_TIME_SWITCH_STATUS;
			this->fce_event(p);
		}
		else
		{
			this->status = FALSE;
		}
	}
}

void eventWidgetChoicegroup(widget_choicegroup_t *p)
{
	int x, y;

	if( p->time > 0 )
	{
		p->time--;
		return;
	}

	getMousePosition(&x, &y);

	if( x >= p->x && x <= p->x+WIDGET_CHOICEGROUP_WIDTH && 
	    y >= p->y && y <= p->y+WIDGET_CHOICEGROUP_HEIGHT &&
	    isMouseClicked() )
	{
		setActive(p);
	}
}

void destroyWidgetChoicegroup(widget_choicegroup_t *p)
{
	int index;

	index = searchListItem(p->list, p);
	assert( index != -1 );
	delList(p->list, index);

	free(p);
}