iconbox.c 3.8 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 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/*
 * File      : iconbox.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/iconbox.h>
#include <rtgui/rtgui_theme.h>

static void _rtgui_iconbox_constructor(rtgui_iconbox_t *iconbox)
{
	/* init widget and set event handler */
	RTGUI_WIDGET(iconbox)->flag |= RTGUI_WIDGET_FLAG_TRANSPARENT;
	rtgui_widget_set_event_handler(RTGUI_WIDGET(iconbox), rtgui_iconbox_event_handler);

	/* set proper of control */
	iconbox->image = RT_NULL;
	iconbox->selected = RT_FALSE;
	iconbox->text = RT_NULL;
	iconbox->text_position = RTGUI_ICONBOX_TEXT_BELOW;
}

static void _rtgui_iconbox_destructor(rtgui_iconbox_t *iconbox)
{
	if (iconbox->image != RT_NULL)
	{
		rtgui_image_destroy(iconbox->image);
		iconbox->image = RT_NULL;
	}

	rt_free(iconbox->text);
	iconbox->text = RT_NULL;
}

43 44 45 46 47
DEFINE_CLASS_TYPE(iconbox, "iconbox", 
	RTGUI_WIDGET_TYPE,
	_rtgui_iconbox_constructor,
	_rtgui_iconbox_destructor,
	sizeof(struct rtgui_iconbox));
B
bernard.xiong 已提交
48 49 50 51 52 53 54 55

rt_bool_t rtgui_iconbox_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
{
	struct rtgui_iconbox* iconbox = (struct rtgui_iconbox*)widget;

	switch (event->type)
	{
	case RTGUI_EVENT_PAINT:
56
#ifndef RTGUI_USING_SMALL_SIZE
B
bernard.xiong 已提交
57 58
		if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
		else
59
#endif
B
bernard.xiong 已提交
60 61 62 63 64 65 66 67 68 69 70
		{
			rtgui_theme_draw_iconbox(iconbox);
		}

		break;
	}

	return RT_FALSE;
}

struct rtgui_iconbox* rtgui_iconbox_create(struct rtgui_image* image,
71
	const char* text,
B
bernard.xiong 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
	int position)
{
    struct rtgui_iconbox* iconbox;

	iconbox = (struct rtgui_iconbox*)rtgui_widget_create(RTGUI_ICONBOX_TYPE);
    if (iconbox != RT_NULL)
    {
		rtgui_rect_t rect = {0, 0, 0, 0}, text_rect;

		rect.x2 = image->w;
		rect.y2 = image->h;

		/* get text rect */
		rtgui_font_get_metrics(rtgui_font_default(), text, &text_rect);
		if (position == RTGUI_ICONBOX_TEXT_BELOW)
		{
			rect.y2 += RTGUI_WIDGET_DEFAULT_MARGIN;
			if (text_rect.x2 > rect.x2)
			{
				rect.x2 = text_rect.x2;
			}
			rect.y2 += text_rect.y2;
		}
		else if (position == RTGUI_ICONBOX_TEXT_RIGHT)
		{
			rect.x2 += RTGUI_WIDGET_DEFAULT_MARGIN;
			if (text_rect.y2 > rect.y2)
			{
				rect.y2 = text_rect.y2;
			}
			rect.x2 += text_rect.x2;
		}

		/* set widget rect */
		rtgui_widget_set_rect(RTGUI_WIDGET(iconbox), &rect);

		/* set image and text position */
		iconbox->image = image;
110
		iconbox->text = (char*)rt_strdup((const char*)text);
B
bernard.xiong 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
		iconbox->text_position = position;
	}

	return iconbox;
}

void rtgui_iconbox_destroy(struct rtgui_iconbox* iconbox)
{
	rtgui_widget_destroy(RTGUI_WIDGET(iconbox));
}

void rtgui_iconbox_set_text_position(struct rtgui_iconbox* iconbox, int position)
{
	rtgui_rect_t rect = {0, 0, 0, 0}, text_rect;

	RT_ASSERT(iconbox != RT_NULL);

	iconbox->text_position = position;

	/* set mini width and height */
	rect.x2 = iconbox->image->w;
	rect.y2 = iconbox->image->h;

	/* get text rect */
	if (iconbox->text != RT_NULL)
	{
		rtgui_font_get_metrics(rtgui_font_default(),
			iconbox->text, &text_rect);
		if (position == RTGUI_ICONBOX_TEXT_BELOW)
		{
			rect.y2 += RTGUI_WIDGET_DEFAULT_MARGIN;
			if (text_rect.x2 > rect.x2)
			{
				rect.x2 = text_rect.x2;
			}
			rect.y2 += text_rect.y2;
		}
		else if (position == RTGUI_ICONBOX_TEXT_RIGHT)
		{
			rect.x2 += RTGUI_WIDGET_DEFAULT_MARGIN;
			if (text_rect.y2 > rect.y2)
			{
				rect.y2 = text_rect.y2;
			}
			rect.x2 += text_rect.x2;
		}
	}

159
#ifndef RTGUI_USING_SMALL_SIZE
B
bernard.xiong 已提交
160 161
	rtgui_widget_set_miniwidth(RTGUI_WIDGET(iconbox), rect.x2);
	rtgui_widget_set_miniheight(RTGUI_WIDGET(iconbox), rect.y2);
162
#endif
B
bernard.xiong 已提交
163
}