summaryrefslogtreecommitdiff
path: root/src/widget/widget_check.c
blob: 2d9423822ad42e77863d096b81dc5512bee3b91c (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
123
124
125
126
127
128

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

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

#include "widget.h"
#include "widget_check.h"

widget_t* newWidgetCheck(int x, int y, bool_t status, void (*fce_event)(void *))
{
	widget_check_t *new;

	new = malloc( sizeof(widget_check_t) );
	new->time = 0;
	new->status = status;
	new->fce_event = fce_event;

	return newWidget(WIDGET_TYPE_CHECK, x, y, WIDGET_CHECK_WIDTH, WIDGET_CHECK_HEIGHT, new);
}

void drawWidgetCheck(widget_t *widget)
{
	widget_check_t *p;
	static image_t *g_check = NULL;
	int x, y;
	int offset;

	assert( widget != NULL );
	assert( widget->type == WIDGET_TYPE_CHECK );

	p = (widget_check_t *) widget->private_data;

	getMousePosition(&x, &y);

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

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

	drawImage(g_check, widget->x, widget->y, offset, 0, WIDGET_CHECK_WIDTH, WIDGET_CHECK_HEIGHT);
}

void eventWidgetCheck(widget_t *widget)
{
	widget_check_t *p;
	int x, y;

	assert( widget != NULL );
	assert( widget->type == WIDGET_TYPE_CHECK );

	p = (widget_check_t *) widget->private_data;

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

	getMousePosition(&x, &y);

	if( x >= widget->x && x <= widget->x+WIDGET_CHECK_WIDTH && 
	    y >= widget->y && y <= widget->y+WIDGET_CHECK_HEIGHT &&
	    isMouseClicked() )
	{
		if( p->status == TRUE )
		{
			p->status = FALSE;
			p->time = WIDGET_CHECK_TIME_SWITCH_STATUS;
		}
		else
		{
			p->status = TRUE;
			p->time = WIDGET_CHECK_TIME_SWITCH_STATUS;
		}

		if( p->fce_event != NULL )
		{
			p->fce_event(p);
		}
	}
}

bool_t getWidgetCheckStatus(widget_t *widget)
{
	widget_check_t *p;

	assert( widget != NULL );
	assert( widget->type == WIDGET_TYPE_CHECK );

	p = (widget_check_t *) widget->private_data;

	return p->status;
}

void setWidgetCheckStatus(widget_t *widget, bool_t status)
{
	widget_check_t *p;

	assert( widget != NULL );
	assert( widget->type == WIDGET_TYPE_CHECK );

	p = (widget_check_t *) widget->private_data;
	p->status = status;
}

void destroyWidgetCheck(widget_t *widget)
{
	widget_check_t *p;

	assert( widget != NULL );
	assert( widget->type == WIDGET_TYPE_CHECK );

	p = (widget_check_t *) widget->private_data;
	free(p);
	destroyWidget(widget);
}