summaryrefslogtreecommitdiff
path: root/src/gui/widget.c
blob: 58b03efd03548e556704e255a172a3224e37d804 (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
#include "widget.h"

#include "string.h"
#include <stddef.h>

static widget_vtable_t const widget_vtable = {
	widget_draw,
	widget_get_focus,
	widget_model_to_screen,
	widget_contains_coordinate,
	widget_on_mouse_down,
	widget_on_mouse_up,
	widget_on_mouse_move,
	widget_on_key_down,
	widget_on_key_up
};

void widget_init( widget_t *widget, widget_t *parent, const int x, const int y, const int w, const int h, const vga_color_t background_color )
{
	memset( widget, 0, sizeof( widget_t ) );

	widget->parent = parent;
	widget->x = x;
	widget->y = y;
	widget->w = w;
	widget->h = h;
	widget->background_color = background_color;

	widget->vtable = &widget_vtable;
}

void widget_draw( void *obj, graphics_context_t *context )
{
	widget_t *widget = obj;
	int x = 0;
	int y = 0;

	widget->vtable->model_to_screen( widget, &x, &y );

	vga_draw_rectangle( context, x, y, widget->w, widget->h,
		widget->background_color );
}

void widget_get_focus( void *obj, widget_t *widget )
{
	widget_t *o = obj;

	if( o->parent != NULL ) {
		o->parent->vtable->get_focus( o->parent, widget );
	}
}

void widget_model_to_screen( void *obj, int *x, int *y )
{
	widget_t *widget = obj;	

	if( widget->parent != NULL ) {
		widget->parent->vtable->model_to_screen( widget->parent, x, y );
	}

	*x += widget->x;
	*y += widget->y;
}

bool widget_contains_coordinate( void *obj, const int x, const int y )
{
	widget_t *widget = obj;

	return widget->x <= x && x < widget->x + widget->w &&
		widget->y <= y && y < widget->y + widget->h;
}

void widget_on_mouse_down( void *obj, const int x, const int y )
{
	widget_t *widget = obj;	

	if( widget->focusable ) {
		widget->vtable->get_focus( widget, widget );
	}
}

void widget_on_mouse_up( void *obj, const int x, const int y )
{
}

void widget_on_mouse_move( void *obj, const int old_x, const int old_y, const int x, const int y )
{
}

void widget_on_key_down( void *obj, char c )
{
}

void widget_on_key_up( void *obj, char c )
{
}