container.c 7.3 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 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 77 78 79 80 81 82 83 84 85 86
/*
 * File      : container.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/widgets/toplevel.h>
#include <rtgui/widgets/container.h>

static void _rtgui_container_constructor(rtgui_container_t *container)
{
	/* set event handler and init field */
	rtgui_widget_set_event_handler(RTGUI_WIDGET(container), rtgui_container_event_handler);
	rtgui_list_init(&(container->children));
	
	/* set focused widget to itself */
	container->focused = RTGUI_WIDGET(container);
	/* set container as focusable widget */
	RTGUI_WIDGET(container)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
}

static void _rtgui_container_destructor(rtgui_container_t *container)
{
	/* destroy children of container */
	rtgui_container_destroy_children(container);
}

static void _rtgui_container_update_toplevel(rtgui_container_t* container)
{
	struct rtgui_list_node* node;

	rtgui_list_foreach(node, &(container->children))
	{
		rtgui_widget_t* child = rtgui_list_entry(node, rtgui_widget_t, sibling);
		/* set child toplevel */
		child->toplevel = rtgui_widget_get_toplevel(RTGUI_WIDGET(container));

		if (RTGUI_IS_CONTAINER(child))
		{
			_rtgui_container_update_toplevel(RTGUI_CONTAINER(child));
		}
	}
}

rtgui_type_t *rtgui_container_type_get(void)
{
	static rtgui_type_t *container_type = RT_NULL;

	if (!container_type)
	{
		container_type = rtgui_type_create("container", RTGUI_WIDGET_TYPE,
			sizeof(rtgui_container_t),
			RTGUI_CONSTRUCTOR(_rtgui_container_constructor),
			RTGUI_DESTRUCTOR(_rtgui_container_destructor));
	}

	return container_type;
}

rt_bool_t rtgui_container_dispatch_event(rtgui_container_t *container, rtgui_event_t* event)
{
	/* handle in child widget */
	struct rtgui_list_node* node;

	rtgui_list_foreach(node, &(container->children))
	{
		struct rtgui_widget* w;
		w = rtgui_list_entry(node, struct rtgui_widget, sibling);

		if (w->event_handler(w, event) == RT_TRUE) return RT_TRUE;
	}

	return RT_FALSE;
}

rt_bool_t rtgui_container_dispatch_mouse_event(rtgui_container_t *container, struct rtgui_event_mouse* event)
{
	/* handle in child widget */
	struct rtgui_list_node* node;
87
	rtgui_widget_t *focus;
B
bernard.xiong 已提交
88

89 90
	/* get focus widget on toplevel */
	focus = RTGUI_CONTAINER(RTGUI_WIDGET(container)->toplevel)->focused;
B
bernard.xiong 已提交
91 92 93 94 95 96
	rtgui_list_foreach(node, &(container->children))
	{
		struct rtgui_widget* w;
		w = rtgui_list_entry(node, struct rtgui_widget, sibling);
		if (rtgui_rect_contains_point(&(w->extent), event->x, event->y) == RT_EOK)
		{
97 98
			if ((focus != w) && RTGUI_WIDGET_IS_FOCUSABLE(w))
				rtgui_widget_focus(w);
B
bernard.xiong 已提交
99 100 101 102 103 104 105 106 107 108 109 110 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
			if (w->event_handler(w, (rtgui_event_t*)event) == RT_TRUE) return RT_TRUE;
		}
	}

	return RT_FALSE;
}

rt_bool_t rtgui_container_event_handler(rtgui_widget_t* widget, rtgui_event_t* event)
{
	rtgui_container_t *container = RTGUI_CONTAINER(widget);

	switch (event->type)
	{
	case RTGUI_EVENT_PAINT:
		rtgui_container_dispatch_event(container, event);
		break;

	case RTGUI_EVENT_KBD:
		{
			/* let parent to handle keyboard event */
			if (widget->parent != RT_NULL && widget->parent != widget->toplevel)
			{
				return widget->parent->event_handler(widget->parent, event);
			}
		}
		break;

	case RTGUI_EVENT_MOUSE_BUTTON:
		/* handle in child widget */
		if (rtgui_container_dispatch_mouse_event(container,
			(struct rtgui_event_mouse*)event) == RT_FALSE)
		{
		}
		else return RT_TRUE;
		break;

	case RTGUI_EVENT_MOUSE_MOTION:
		if (rtgui_container_dispatch_mouse_event(container,
			(struct rtgui_event_mouse*)event) == RT_FALSE)
		{
139
#ifndef RTGUI_USING_SMALL_SIZE
B
bernard.xiong 已提交
140
#if 0
B
bernard.xiong 已提交
141 142 143 144 145
			/* handle event in current widget */
			if (widget->on_mousemotion != RT_NULL)
			{
				return widget->on_mousemotion(widget, event);
			}
B
bernard.xiong 已提交
146
#endif
B
bernard.xiong 已提交
147 148 149 150 151 152 153 154
#endif
		}
		else return RT_TRUE;
		break;

	case RTGUI_EVENT_COMMAND:
		if (rtgui_container_dispatch_event(container, event) == RT_FALSE)
		{
155
#ifndef RTGUI_USING_SMALL_SIZE
B
bernard.xiong 已提交
156 157 158 159
			if (widget->on_command != RT_NULL)
			{
				return widget->on_command(widget, event);
			}
160
#endif
B
bernard.xiong 已提交
161 162 163 164 165 166 167
		}
		else return RT_TRUE;
		break;

	case RTGUI_EVENT_RESIZE:
		if (rtgui_container_dispatch_event(container, event) == RT_FALSE)
		{
168
#ifndef RTGUI_USING_SMALL_SIZE
B
bernard.xiong 已提交
169 170
			if (widget->on_size != RT_NULL)
				return widget->on_size(widget, event);
171
#endif
B
bernard.xiong 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
		}
		else return RT_TRUE;
		break;

	default:
		/* call parent widget event handler */
		return rtgui_widget_event_handler(widget, event);
	}

	return RT_FALSE;
}

/*
 * This function will add a child to a container widget
 * Note: this function will not change the widget layout
 * the layout is the responsibility of layout widget, such as box.
 */
void rtgui_container_add_child(rtgui_container_t *container, rtgui_widget_t* child)
{
	RT_ASSERT(container != RT_NULL);
	RT_ASSERT(child != RT_NULL);

	/* set parent and toplevel widget */
	child->parent = RTGUI_WIDGET(container);
	/* put widget to parent's children list */
	rtgui_list_append(&(container->children), &(child->sibling));

	/* update children toplevel */
	if (RTGUI_WIDGET(container)->toplevel != RT_NULL &&
		RTGUI_IS_TOPLEVEL(RTGUI_WIDGET(container)->toplevel))
	{
		child->toplevel = rtgui_widget_get_toplevel(RTGUI_WIDGET(container));

		/* update all child toplevel */
		if (RTGUI_IS_CONTAINER(child))
		{
			_rtgui_container_update_toplevel(RTGUI_CONTAINER(child));
		}
	}
}

/* remove a child to widget */
void rtgui_container_remove_child(rtgui_container_t *container, rtgui_widget_t* child)
{
	RT_ASSERT(container != RT_NULL);
	RT_ASSERT(child != RT_NULL);

	if (child == container->focused)
	{
		/* set focused to itself */
		container->focused = RTGUI_WIDGET(container);

		rtgui_widget_focus(RTGUI_WIDGET(container));
	}

	/* remove widget from parent's children list */
	rtgui_list_remove(&(container->children), &(child->sibling));

	/* set parent and toplevel widget */
	child->parent = RT_NULL;
	child->toplevel = RT_NULL;
}

/* destroy all children of container */
void rtgui_container_destroy_children(rtgui_container_t *container)
{
	struct rtgui_list_node* node;

	if (container == RT_NULL) return;

	node = container->children.next;
	while (node != RT_NULL)
	{
		rtgui_widget_t* child = rtgui_list_entry(node, rtgui_widget_t, sibling);

		if (RTGUI_IS_CONTAINER(child))
		{
			/* break parent firstly */
			child->parent = RT_NULL;

			/* destroy children of child */
			rtgui_container_destroy_children(RTGUI_CONTAINER(child));
		}

		/* remove widget from parent's children list */
		rtgui_list_remove(&(container->children), &(child->sibling));

		/* set parent and toplevel widget */
		child->parent = RT_NULL;

		/* destroy object and remove from parent */
		rtgui_object_destroy(RTGUI_OBJECT(child));

		node = container->children.next;
	}

	container->children.next = RT_NULL;
	container->focused = RTGUI_WIDGET(container);
	if (RTGUI_WIDGET(container)->parent != RT_NULL)
		rtgui_widget_focus(RTGUI_WIDGET(container));

	/* update widget clip */
	rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(RTGUI_WIDGET(container)->toplevel));
}

rtgui_widget_t* rtgui_container_get_first_child(rtgui_container_t* container)
{
	rtgui_widget_t* child = RT_NULL;

	if (container->children.next != RT_NULL)
	{
		child = rtgui_list_entry(container->children.next, rtgui_widget_t, sibling);
	}

	return child;
}