2009-11-24 07:34:58 +08:00
|
|
|
/*
|
|
|
|
* 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 */
|
2012-04-18 23:06:12 +08:00
|
|
|
rtgui_object_set_event_handler(RTGUI_OBJECT(label), rtgui_label_event_handler);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
/* set field */
|
|
|
|
label->text = RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _rtgui_label_destructor(rtgui_label_t *label)
|
|
|
|
{
|
|
|
|
/* release text memory */
|
2012-08-13 14:30:05 +08:00
|
|
|
if (label->text)
|
|
|
|
rt_free(label->text);
|
2009-11-24 07:34:58 +08:00
|
|
|
label->text = RT_NULL;
|
|
|
|
}
|
|
|
|
|
2011-05-13 18:53:51 +08:00
|
|
|
DEFINE_CLASS_TYPE(label, "label",
|
|
|
|
RTGUI_WIDGET_TYPE,
|
|
|
|
_rtgui_label_constructor,
|
|
|
|
_rtgui_label_destructor,
|
|
|
|
sizeof(struct rtgui_label));
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
rt_bool_t rtgui_label_event_handler(struct rtgui_object *object, struct rtgui_event* event)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-04-18 23:06:12 +08:00
|
|
|
struct rtgui_label *label;
|
|
|
|
RTGUI_WIDGET_EVENT_HANDLER_PREPARE
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
label = RTGUI_LABEL(object);
|
2009-11-24 07:34:58 +08:00
|
|
|
switch (event->type)
|
|
|
|
{
|
|
|
|
case RTGUI_EVENT_PAINT:
|
|
|
|
rtgui_theme_draw_label(label);
|
|
|
|
break;
|
2012-07-20 20:37:56 +08:00
|
|
|
default:
|
|
|
|
return rtgui_widget_event_handler(object, event);
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return RT_FALSE;
|
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_label_event_handler);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2010-02-18 07:56:49 +08:00
|
|
|
rtgui_label_t* rtgui_label_create(const char* text)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
|
|
|
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 */
|
2010-02-18 07:56:49 +08:00
|
|
|
label->text = (char*)rt_strdup((const char*)text);
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return label;
|
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_label_create);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
void rtgui_label_destroy(rtgui_label_t* label)
|
|
|
|
{
|
|
|
|
rtgui_widget_destroy(RTGUI_WIDGET(label));
|
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_label_destroy);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2010-02-18 07:56:49 +08:00
|
|
|
char* rtgui_label_get_text(rtgui_label_t* label)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
|
|
|
RT_ASSERT(label != RT_NULL);
|
|
|
|
|
|
|
|
return label->text;
|
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_label_get_text);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2010-02-18 07:56:49 +08:00
|
|
|
void rtgui_label_set_text(rtgui_label_t* label, const char* text)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
|
|
|
RT_ASSERT(label != RT_NULL);
|
|
|
|
|
|
|
|
if (label->text != RT_NULL)
|
|
|
|
{
|
2011-06-05 23:19:19 +08:00
|
|
|
/* it's a same text string */
|
|
|
|
if (rt_strncmp(text, label->text, rt_strlen(text)) == 0) return;
|
|
|
|
|
2009-11-24 07:34:58 +08:00
|
|
|
/* release old text memory */
|
2012-08-13 14:30:05 +08:00
|
|
|
rtgui_free(label->text);
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
|
2010-02-18 07:56:49 +08:00
|
|
|
if (text != RT_NULL) label->text = (char*)rt_strdup((const char*)text);
|
2009-11-24 07:34:58 +08:00
|
|
|
else label->text = RT_NULL;
|
2010-01-27 08:06:09 +08:00
|
|
|
|
|
|
|
/* update widget */
|
|
|
|
rtgui_theme_draw_label(label);
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_label_set_text);
|
2011-06-05 23:19:19 +08:00
|
|
|
|