summaryrefslogtreecommitdiff
path: root/src/widget_select.c
blob: dbd6fa9633ec94eda1aa68435a5544b1ba8363c2 (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

#include <stdlib.h>

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

/*
typedef struct widget_select
{
	int x, y;
	int w, h;
	list_t *list;
	void (*fce_event)(void *);
} widget_select_t;
*/

widget_select_t* newWidgetSelect(int x, int y, void (*fce_event)(void *))
{
	widget_select_t *new;

	new = malloc( sizeof(widget_select_t) );
	new->x = x;
	new->y = y;
	new->select = 0;
	new->fce_event = fce_event;
	new->list = newList();

	return new;
}

void addToWidgetSelect(widget_select_t *p, char *s)
{
	addList(p->list, strdup(s) );
}

void drawWidgetSelect(widget_select_t *p)
{
	int x, y, w, h;
	int i;

	getMousePosition(&x, &y);

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

		line = (char *)p->list->list[i];

		getTextSize(line, &w, &h);

		if( x > p->x && y > p->y + i*20 && x < p->x+w && y < p->y + i*20 + h )
		{
			drawFont(line, p->x, p->y + i*20, COLOR_YELLOW);
		}
		else
		{
			if( p->select == i )
			{
				drawFont(line, p->x, p->y + i*20, COLOR_RED);
			}
			else
			{
				drawFont(line, p->x, p->y + i*20, COLOR_WHITE);
			}
		}
	}
}

void eventWidgetSelect(widget_select_t *p)
{
	int x, y, w, h;
	int i;

	getMousePosition(&x, &y);

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

		line = (char *)p->list->list[i];

		getTextSize(line, &w, &h);

		if( x > p->x && y > p->y + i*20 && x < p->x+w && y < p->y + i*20 + h && isMouseClicked() )
		{
			p->select = i;
			p->fce_event(p);
		}
	}
}

void destroyWidgetSelect(widget_select_t *p)
{
	destroyListItem(p->list, free);
	free(p);
}