2009-11-24 07:34:58 +08:00
|
|
|
/*
|
|
|
|
* 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
|
2012-04-18 23:06:12 +08:00
|
|
|
* 2010-09-24 Bernard fix container destroy issue
|
2009-11-24 07:34:58 +08:00
|
|
|
*/
|
2012-04-18 23:06:12 +08:00
|
|
|
#include <rtgui/dc.h>
|
|
|
|
#include <rtgui/rtgui_system.h>
|
2012-07-20 20:37:56 +08:00
|
|
|
#include <rtgui/rtgui_app.h>
|
2009-11-24 07:34:58 +08:00
|
|
|
#include <rtgui/widgets/container.h>
|
2012-04-18 23:06:12 +08:00
|
|
|
#include <rtgui/widgets/window.h>
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
static void _rtgui_container_constructor(rtgui_container_t *container)
|
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
/* init container */
|
|
|
|
rtgui_object_set_event_handler(RTGUI_OBJECT(container),
|
|
|
|
rtgui_container_event_handler);
|
2012-04-18 23:06:12 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_list_init(&(container->children));
|
|
|
|
container->layout_box = RT_NULL;
|
2012-04-18 23:06:12 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
RTGUI_WIDGET(container)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void _rtgui_container_destructor(rtgui_container_t *container)
|
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_container_destroy_children(container);
|
2012-07-20 20:37:56 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
if (container->layout_box != RT_NULL)
|
|
|
|
rtgui_object_destroy(RTGUI_OBJECT(container->layout_box));
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
DEFINE_CLASS_TYPE(container, "container",
|
2012-11-26 10:42:52 +08:00
|
|
|
RTGUI_WIDGET_TYPE,
|
|
|
|
_rtgui_container_constructor,
|
|
|
|
_rtgui_container_destructor,
|
|
|
|
sizeof(struct rtgui_container));
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(_rtgui_container);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
rt_bool_t rtgui_container_dispatch_event(rtgui_container_t *container, rtgui_event_t *event)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
/* handle in child widget */
|
|
|
|
struct rtgui_list_node *node;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_list_foreach(node, &(container->children))
|
|
|
|
{
|
|
|
|
struct rtgui_widget *w;
|
|
|
|
w = rtgui_list_entry(node, struct rtgui_widget, sibling);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
if (RTGUI_OBJECT(w)->event_handler &&
|
|
|
|
RTGUI_OBJECT(w)->event_handler(RTGUI_OBJECT(w), event) == RT_TRUE)
|
|
|
|
return RT_TRUE;
|
|
|
|
}
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
return RT_FALSE;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_container_dispatch_event);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-07-21 20:18:51 +08:00
|
|
|
/* broadcast means that the return value of event handlers will be ignored. The
|
|
|
|
* events will always reach every child.*/
|
|
|
|
rt_bool_t rtgui_container_broadcast_event(struct rtgui_container *container, struct rtgui_event *event)
|
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
struct rtgui_list_node *node;
|
2012-07-21 20:18:51 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_list_foreach(node, &(container->children))
|
|
|
|
{
|
|
|
|
struct rtgui_widget *w;
|
|
|
|
w = rtgui_list_entry(node, struct rtgui_widget, sibling);
|
2012-07-21 20:18:51 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
if (RTGUI_OBJECT(w)->event_handler)
|
|
|
|
RTGUI_OBJECT(w)->event_handler(RTGUI_OBJECT(w), event);
|
|
|
|
}
|
2012-07-21 20:18:51 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
return RT_FALSE;
|
2012-07-21 20:18:51 +08:00
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_container_broadcast_event);
|
2012-07-21 20:18:51 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
rt_bool_t rtgui_container_dispatch_mouse_event(rtgui_container_t *container, struct rtgui_event_mouse *event)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
/* handle in child widget */
|
|
|
|
struct rtgui_list_node *node;
|
|
|
|
struct rtgui_widget *old_focus;
|
|
|
|
|
|
|
|
old_focus = RTGUI_WIDGET(container)->toplevel->focused_widget;
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if ((old_focus != w) && RTGUI_WIDGET_IS_FOCUSABLE(w))
|
|
|
|
rtgui_widget_focus(w);
|
|
|
|
if (RTGUI_OBJECT(w)->event_handler &&
|
|
|
|
RTGUI_OBJECT(w)->event_handler(RTGUI_OBJECT(w),
|
|
|
|
(rtgui_event_t *)event) == RT_TRUE)
|
|
|
|
return RT_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_FALSE;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_container_dispatch_mouse_event);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
rt_bool_t rtgui_container_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_container *container;
|
|
|
|
struct rtgui_widget *widget;
|
|
|
|
|
|
|
|
RT_ASSERT(object != RT_NULL);
|
|
|
|
RT_ASSERT(event != RT_NULL);
|
|
|
|
|
|
|
|
container = RTGUI_CONTAINER(object);
|
|
|
|
widget = RTGUI_WIDGET(object);
|
|
|
|
|
|
|
|
switch (event->type)
|
|
|
|
{
|
|
|
|
case RTGUI_EVENT_PAINT:
|
|
|
|
{
|
|
|
|
struct rtgui_dc *dc;
|
|
|
|
struct rtgui_rect rect;
|
|
|
|
|
|
|
|
dc = rtgui_dc_begin_drawing(widget);
|
|
|
|
if (dc == RT_NULL)
|
|
|
|
return RT_FALSE;
|
|
|
|
rtgui_widget_get_rect(widget, &rect);
|
|
|
|
|
|
|
|
/* fill container with background */
|
|
|
|
rtgui_dc_fill_rect(dc, &rect);
|
|
|
|
|
|
|
|
/* paint on each child */
|
|
|
|
rtgui_container_dispatch_event(container, event);
|
|
|
|
|
|
|
|
rtgui_dc_end_drawing(dc);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RTGUI_EVENT_KBD:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RTGUI_EVENT_MOUSE_BUTTON:
|
|
|
|
case RTGUI_EVENT_MOUSE_MOTION:
|
|
|
|
/* handle in child widget */
|
|
|
|
return rtgui_container_dispatch_mouse_event(container,
|
|
|
|
(struct rtgui_event_mouse *)event);
|
|
|
|
|
|
|
|
case RTGUI_EVENT_SHOW:
|
|
|
|
rtgui_widget_onshow(RTGUI_OBJECT(container), event);
|
|
|
|
rtgui_container_dispatch_event(container, event);
|
|
|
|
break;
|
|
|
|
case RTGUI_EVENT_HIDE:
|
|
|
|
rtgui_widget_onhide(RTGUI_OBJECT(container), event);
|
|
|
|
rtgui_container_dispatch_event(container, event);
|
|
|
|
break;
|
|
|
|
case RTGUI_EVENT_COMMAND:
|
|
|
|
rtgui_container_dispatch_event(container, event);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RTGUI_EVENT_UPDATE_TOPLVL:
|
|
|
|
/* call parent handler */
|
|
|
|
rtgui_widget_onupdate_toplvl(object, event);
|
|
|
|
/* update the children */
|
|
|
|
rtgui_container_broadcast_event(container, event);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RTGUI_EVENT_RESIZE:
|
|
|
|
/* re-layout container */
|
|
|
|
rtgui_container_layout(container);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* call parent widget event handler */
|
|
|
|
return rtgui_widget_event_handler(RTGUI_OBJECT(widget), event);
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_FALSE;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_container_event_handler);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_container_t *rtgui_container_create(void)
|
2012-04-18 23:06:12 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
struct rtgui_container *container;
|
2012-04-18 23:06:12 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
/* allocate container */
|
|
|
|
container = (struct rtgui_container *) rtgui_widget_create(RTGUI_CONTAINER_TYPE);
|
|
|
|
return container;
|
2012-04-18 23:06:12 +08:00
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_container_create);
|
2012-04-18 23:06:12 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
void rtgui_container_destroy(rtgui_container_t *container)
|
2012-04-18 23:06:12 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_widget_destroy(RTGUI_WIDGET(container));
|
2012-04-18 23:06:12 +08:00
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_container_destroy);
|
2012-04-18 23:06:12 +08:00
|
|
|
|
2009-11-24 07:34:58 +08:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2012-11-26 10:42:52 +08:00
|
|
|
void rtgui_container_add_child(rtgui_container_t *container, rtgui_widget_t *child)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
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_WIN(RTGUI_WIDGET(container)->toplevel))
|
|
|
|
{
|
|
|
|
struct rtgui_event_update_toplvl eup;
|
|
|
|
RTGUI_EVENT_UPDATE_TOPLVL_INIT(&eup);
|
|
|
|
eup.toplvl = RTGUI_WIDGET(container)->toplevel;
|
|
|
|
rtgui_container_broadcast_event(container, (struct rtgui_event *)&eup);
|
|
|
|
}
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_container_add_child);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
/* remove a child to widget */
|
2012-11-26 10:42:52 +08:00
|
|
|
void rtgui_container_remove_child(rtgui_container_t *container, rtgui_widget_t *child)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
RT_ASSERT(container != RT_NULL);
|
|
|
|
RT_ASSERT(child != RT_NULL);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_widget_unfocus(child);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
/* remove widget from parent's children list */
|
|
|
|
rtgui_list_remove(&(container->children), &(child->sibling));
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
/* set parent and toplevel widget */
|
|
|
|
child->parent = RT_NULL;
|
|
|
|
child->toplevel = RT_NULL;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_container_remove_child);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
/* destroy all children of container */
|
|
|
|
void rtgui_container_destroy_children(rtgui_container_t *container)
|
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
struct rtgui_list_node *node;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
if (container == RT_NULL)
|
|
|
|
return;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
node = container->children.next;
|
|
|
|
while (node != RT_NULL)
|
|
|
|
{
|
|
|
|
rtgui_widget_t *child = rtgui_list_entry(node, rtgui_widget_t, sibling);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
if (RTGUI_IS_CONTAINER(child))
|
|
|
|
{
|
|
|
|
/* break parent firstly */
|
|
|
|
child->parent = RT_NULL;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
/* destroy children of child */
|
|
|
|
rtgui_container_destroy_children(RTGUI_CONTAINER(child));
|
|
|
|
}
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
/* remove widget from parent's children list */
|
|
|
|
rtgui_list_remove(&(container->children), &(child->sibling));
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
/* set parent and toplevel widget */
|
|
|
|
child->parent = RT_NULL;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
/* destroy object and remove from parent */
|
|
|
|
rtgui_object_destroy(RTGUI_OBJECT(child));
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
node = container->children.next;
|
|
|
|
}
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
container->children.next = RT_NULL;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
/* update widget clip */
|
|
|
|
rtgui_win_update_clip(RTGUI_WIN(RTGUI_WIDGET(container)->toplevel));
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_container_destroy_children);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_widget_t *rtgui_container_get_first_child(rtgui_container_t *container)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_widget_t *child = RT_NULL;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
RT_ASSERT(container != RT_NULL);
|
2012-05-28 00:34:19 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
if (container->children.next != RT_NULL)
|
|
|
|
{
|
|
|
|
child = rtgui_list_entry(container->children.next, rtgui_widget_t, sibling);
|
|
|
|
}
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
return child;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_container_get_first_child);
|
2012-04-18 23:06:12 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
void rtgui_container_set_box(rtgui_container_t *container, struct rtgui_box *box)
|
2012-04-18 23:06:12 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
if (container == RT_NULL || box == RT_NULL)
|
2012-04-18 23:06:12 +08:00
|
|
|
return;
|
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
container->layout_box = box;
|
|
|
|
box->container = container;
|
2012-04-18 23:06:12 +08:00
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_container_set_box);
|
2012-04-18 23:06:12 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
void rtgui_container_layout(struct rtgui_container *container)
|
2012-04-18 23:06:12 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
if (container == RT_NULL || container->layout_box == RT_NULL)
|
|
|
|
return;
|
2012-04-18 23:06:12 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_box_layout(container->layout_box);
|
2012-04-18 23:06:12 +08:00
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_container_layout);
|
2012-04-18 23:06:12 +08:00
|
|
|
|