label.c 2.5 KB
Newer Older
B
bernard.xiong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * File      : label.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
 * 2009-10-16     Bernard      first version
 */
#include <rtgui/dc.h>
#include <rtgui/widgets/label.h>
#include <rtgui/rtgui_system.h>
#include <rtgui/rtgui_theme.h>

static void _rtgui_label_constructor(rtgui_label_t *label)
{
	/* init widget and set event handler */
22
	rtgui_object_set_event_handler(RTGUI_OBJECT(label), rtgui_label_event_handler);
B
bernard.xiong 已提交
23 24 25 26 27 28 29 30 31 32 33 34

	/* set field */
	label->text = RT_NULL;
}

static void _rtgui_label_destructor(rtgui_label_t *label)
{
	/* release text memory */
	rt_free(label->text);
	label->text = RT_NULL;
}

35 36 37 38 39
DEFINE_CLASS_TYPE(label, "label", 
	RTGUI_WIDGET_TYPE,
	_rtgui_label_constructor,
	_rtgui_label_destructor,
	sizeof(struct rtgui_label));
B
bernard.xiong 已提交
40

41
rt_bool_t rtgui_label_event_handler(struct rtgui_object *object, struct rtgui_event* event)
B
bernard.xiong 已提交
42
{
43 44
	struct rtgui_label *label;
	RTGUI_WIDGET_EVENT_HANDLER_PREPARE
B
bernard.xiong 已提交
45

46
	label = RTGUI_LABEL(object);
B
bernard.xiong 已提交
47 48 49 50 51 52 53 54 55 56
	switch (event->type)
	{
	case RTGUI_EVENT_PAINT:
		rtgui_theme_draw_label(label);
		break;
	}

	return RT_FALSE;
}

57
rtgui_label_t* rtgui_label_create(const char* text)
B
bernard.xiong 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
{
    struct rtgui_label* label;

    label = (struct rtgui_label*) rtgui_widget_create(RTGUI_LABEL_TYPE);
    if (label != RT_NULL)
    {
		rtgui_rect_t rect;

		/* set default rect */
		rtgui_font_get_metrics(rtgui_font_default(), text, &rect);
		rect.x2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1);
		rect.y2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1);
		rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);

		/* set text */
73
		label->text = (char*)rt_strdup((const char*)text);
B
bernard.xiong 已提交
74 75 76 77 78 79 80 81 82 83
    }

    return label;
}

void rtgui_label_destroy(rtgui_label_t* label)
{
	rtgui_widget_destroy(RTGUI_WIDGET(label));
}

84
char* rtgui_label_get_text(rtgui_label_t* label)
B
bernard.xiong 已提交
85 86 87 88 89 90
{
	RT_ASSERT(label != RT_NULL);

	return label->text;
}

91
void rtgui_label_set_text(rtgui_label_t* label, const char* text)
B
bernard.xiong 已提交
92 93 94 95 96
{
	RT_ASSERT(label != RT_NULL);

	if (label->text != RT_NULL)
	{
97 98 99
		/* it's a same text string */
		if (rt_strncmp(text, label->text, rt_strlen(text)) == 0) return;
		
B
bernard.xiong 已提交
100 101 102 103
		/* release old text memory */
		rt_free(label->text);
	}

104
	if (text != RT_NULL) label->text = (char*)rt_strdup((const char*)text);
B
bernard.xiong 已提交
105
	else label->text = RT_NULL;
106 107 108

	/* update widget */
	rtgui_theme_draw_label(label);
B
bernard.xiong 已提交
109
}
110