summaryrefslogtreecommitdiff
path: root/src/gui/window.c
blob: 681c3b745a39f10563a2524c9bb38ddf59f5cc64 (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
#include "window.h"

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

static window_vtable_t const window_vtable = {
	{
		{
			window_draw,
			composite_widget_get_focus,
			widget_model_to_screen,
			widget_contains_coordinate,
			window_on_mouse_down,
			window_on_mouse_up,
			window_on_mouse_move,
			composite_widget_on_key_down,
			composite_widget_on_key_up
		},
		composite_widget_add_child
	}
};


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

	composite_widget_init( &window->base, parent, x, y, w, h, background_color );	

	window->dragging = false;

	((widget_t *)window)->vtable = (widget_vtable_t *)&window_vtable;
}

void window_draw( void *obj, graphics_context_t *context )
{
	window_t *widget = obj;

	if( ((widget_t *)widget)->x < 0 ) {
		((widget_t *)widget)->x = 0;
	}
	if( ((widget_t *)widget)->x > context->mode->base.x - ((widget_t *)widget)->w ) {
		((widget_t *)widget)->x = context->mode->base.x - ((widget_t *)widget)->w;
	}
	if( ((widget_t *)widget)->y < 0 ) {
		((widget_t *)widget)->y = 0;
	}
	if( ((widget_t *)widget)->y > context->mode->base.y - ((widget_t *)widget)->h ) {
		((widget_t *)widget)->y = context->mode->base.y - ((widget_t *)widget)->h;
	}

	composite_widget_draw( obj, context );
}

void window_on_mouse_down( void *obj, const int x, const int y )
{
	window_t *widget = obj;

	widget->dragging = true;
	composite_widget_on_mouse_down( obj, x, y );
}

void window_on_mouse_up( void *obj, const int x, const int y )
{
	window_t *widget = obj;

	widget->dragging = false;
	composite_widget_on_mouse_up( obj, x, y );
}

void window_on_mouse_move( void *obj, const int old_x, const int old_y, const int x, const int y )
{
	window_t *widget = obj;

	if( widget->dragging ) {
		((widget_t *)widget)->x += x - old_x;
		((widget_t *)widget)->y += y - old_y;
	}
}