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)
|
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
RTGUI_WIDGET_TEXTALIGN(label) = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_CENTER_VERTICAL;
|
|
|
|
/* init widget and set event handler */
|
|
|
|
rtgui_object_set_event_handler(RTGUI_OBJECT(label), rtgui_label_event_handler);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
/* set field */
|
|
|
|
label->text = RT_NULL;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void _rtgui_label_destructor(rtgui_label_t *label)
|
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
/* release text memory */
|
|
|
|
if (label->text)
|
|
|
|
rt_free(label->text);
|
|
|
|
label->text = RT_NULL;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
|
2012-11-26 10:42:52 +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-11-26 10:42:52 +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-11-26 10:42:52 +08:00
|
|
|
struct rtgui_label *label;
|
|
|
|
RTGUI_WIDGET_EVENT_HANDLER_PREPARE
|
|
|
|
|
|
|
|
label = RTGUI_LABEL(object);
|
|
|
|
switch (event->type)
|
|
|
|
{
|
|
|
|
case RTGUI_EVENT_PAINT:
|
|
|
|
rtgui_theme_draw_label(label);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return rtgui_widget_event_handler(object, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_FALSE;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_label_event_handler);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_label_t *rtgui_label_create(const char *text)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
struct rtgui_label *label;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
label = (struct rtgui_label *) rtgui_widget_create(RTGUI_LABEL_TYPE);
|
2009-11-24 07:34:58 +08:00
|
|
|
if (label != RT_NULL)
|
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_rect_t rect;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
/* 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);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
/* set text */
|
|
|
|
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
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
void rtgui_label_destroy(rtgui_label_t *label)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_widget_destroy(RTGUI_WIDGET(label));
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_label_destroy);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
char *rtgui_label_get_text(rtgui_label_t *label)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
RT_ASSERT(label != RT_NULL);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
return label->text;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_label_get_text);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
void rtgui_label_set_text(rtgui_label_t *label, const char *text)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
RT_ASSERT(label != RT_NULL);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
if (label->text != RT_NULL)
|
|
|
|
{
|
|
|
|
/* it's a same text string */
|
|
|
|
if (rt_strcmp(text, label->text) == 0)
|
2012-09-20 16:13:23 +08:00
|
|
|
return;
|
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
/* release old text memory */
|
|
|
|
rt_free(label->text);
|
|
|
|
}
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
if (text != RT_NULL)
|
|
|
|
label->text = (char *)rt_strdup((const char *)text);
|
|
|
|
else
|
2012-09-20 16:13:23 +08:00
|
|
|
label->text = RT_NULL;
|
2010-01-27 08:06:09 +08:00
|
|
|
|
2012-11-26 10:42:52 +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
|
|
|
|