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

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

static text_widget_vtable_t const text_widget_vtable = {
	{
		text_widget_draw,
		widget_get_focus,
		widget_model_to_screen,
		widget_contains_coordinate,
		widget_on_mouse_down,
		widget_on_mouse_up,
		widget_on_mouse_move,
		text_widget_on_key_down,
		widget_on_key_up
	},
	text_widget_set_text
};

void text_widget_init( text_widget_t *widget, widget_t *parent, const int x, const int y, const int w, const int h, const video_rgb_color_t background_color, const char *s )
{
	memset( widget, 0, sizeof( text_widget_t ) );

	widget_init( &widget->base, parent, x, y, w, h, background_color );	

	text_widget_set_text( widget, s );
	
	widget->base.focusable = true;

	((widget_t *)widget)->vtable = (widget_vtable_t *)&text_widget_vtable;
}

void text_widget_draw( void *obj, graphics_context_t *context )
{
	text_widget_t *widget = obj;

	widget_draw( obj, context );

	int x = 0;
	int y = 0;
	int w = 0;
	int h = 0;

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

	for( const char *p = widget->s; *p != '\0'; p++ ) {
		((video_vtable_t *)(context->base.vtable))->draw_char(
			context, *p, x + w, y + h,
			((widget_t *)widget)->background_color,
			VIDEO_RGB_COLOR_WHITE );
		w += 9;
		if( w >= ((widget_t *)widget)->w - 10 ) {
			h += 16;
			w = 0;
		}
	}

}

void text_widget_set_text( void *obj, const char *s )
{
	text_widget_t *widget = obj;

	strlcpy( widget->s, s, TEXT_WIDGET_MAX_TEXT_SIZE );
}

void text_widget_on_key_down( void *obj, char c )
{
	text_widget_t *widget = obj;

	if( strlen( widget->s ) < TEXT_WIDGET_MAX_TEXT_SIZE - 1 ) {
		widget->s[strlen( widget->s )] = c;
		widget->s[strlen( widget->s )+1] = '\0';
	}
}