Browse Source

add basic files of RTGUI

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@69 bbd45198-f89e-11dd-88c7-29a3b14d5316
bernard.xiong 15 years ago
parent
commit
90f9d0a2ab

+ 242 - 0
rtgui/common/rtgui_object.c

@@ -0,0 +1,242 @@
+/*
+ * File      : rtgui_object.c
+ * This file is part of RTGUI in 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-04     Bernard      first version
+ */
+
+#include <rtgui/rtgui_object.h>
+#include <rtgui/rtgui_system.h>
+
+static void _rtgui_object_constructor(rtgui_object_t *object)
+{
+   if (!object) return;
+
+   object->is_static = RT_FALSE;
+}
+
+/* Destroys the object */
+static void _rtgui_object_destructor(rtgui_object_t *object)
+{
+	/* nothing */
+}
+
+rtgui_type_t *rtgui_type_create(const char *type_name, rtgui_type_t *parent_type,
+						 int type_size, rtgui_constructor_t constructor,
+						 rtgui_destructor_t destructor)
+{
+	rtgui_type_t *new_type;
+
+	if (!type_name)
+		return RT_NULL;
+
+	new_type = rtgui_malloc(sizeof(rtgui_type_t));
+	new_type->name = rt_strdup(type_name);
+	new_type->size = type_size;
+	new_type->constructor = constructor;
+	new_type->destructor = destructor;
+
+	if (!parent_type)
+	{
+		new_type->hierarchy_depth = 0;
+		new_type->hierarchy = RT_NULL;
+	}
+	else
+	{
+		/* Build the type hierarchy */
+		new_type->hierarchy_depth = parent_type->hierarchy_depth + 1;
+		new_type->hierarchy = rtgui_malloc(sizeof(rtgui_type_t *) * new_type->hierarchy_depth);
+
+		new_type->hierarchy[0] = parent_type;
+		rt_memcpy(new_type->hierarchy + 1, parent_type->hierarchy,
+				  parent_type->hierarchy_depth * sizeof(rtgui_type_t *));
+	}
+
+	return new_type;
+}
+
+void rtgui_type_destroy(rtgui_type_t *type)
+{
+	if (!type) return;
+
+	if (type->hierarchy) rtgui_free(type->hierarchy);
+
+	rtgui_free(type->name);
+	rtgui_free(type);
+}
+
+void rtgui_type_object_construct(rtgui_type_t *type, rtgui_object_t *object)
+{
+	int i;
+
+	if (!type || !object) return;
+
+	/* Call the constructors */
+	for (i = type->hierarchy_depth - 1; i >= 0; i--)
+	{
+		if (type->hierarchy[i]->constructor)
+			type->hierarchy[i]->constructor(object);
+	}
+	if (type->constructor) type->constructor(object);
+}
+
+void rtgui_type_destructors_call(rtgui_type_t *type, rtgui_object_t *object)
+{
+	int i;
+
+	if (!type || !object) return;
+
+	if (type->destructor) type->destructor(object);
+	for (i = 0; i < type->hierarchy_depth; i++)
+	{
+		if (type->hierarchy[i]->destructor)
+			type->hierarchy[i]->destructor(object);
+	}
+}
+
+rt_bool_t rtgui_type_inherits_from(rtgui_type_t *type, rtgui_type_t *parent)
+{
+	int i;
+
+	if (!type || !parent) return RT_FALSE;
+
+	if (type == parent) return RT_TRUE;
+
+	for (i = 0; i < type->hierarchy_depth; i++)
+	{
+		if (type->hierarchy[i] == parent) return RT_TRUE;
+	}
+
+	return RT_FALSE;
+}
+
+rtgui_type_t *rtgui_type_parent_type_get(rtgui_type_t *type)
+{
+	if (!type || !type->hierarchy) return RT_NULL;
+
+	return type->hierarchy[0];
+}
+
+const char *rtgui_type_name_get(rtgui_type_t *type)
+{
+	if (!type) return RT_NULL;
+
+	return type->name;
+}
+
+struct rtgui_object_information
+{
+	rt_uint32_t objs_number;
+	rt_uint32_t allocated_size;
+	rt_uint32_t max_allocated;
+};
+struct rtgui_object_information obj_info = {0, 0, 0};
+
+/**
+ * @brief Creates a new object: it calls the corresponding constructors (from the constructor of the base class to the
+ * constructor of the more derived class) and then sets the values of the given properties
+ * @param object_type the type of object to create
+ * @return Returns the new Etk_Object of type @a object_type
+ */
+rtgui_object_t *rtgui_object_create(rtgui_type_t *object_type)
+{
+	rtgui_object_t *new_object;
+
+	if (!object_type)
+		return RT_NULL;
+
+	new_object = rtgui_malloc(object_type->size);
+	if (new_object == RT_NULL) return RT_NULL;
+
+	obj_info.objs_number ++;
+	obj_info.allocated_size += object_type->size;
+	if (obj_info.allocated_size > obj_info.max_allocated)
+		obj_info.max_allocated = obj_info.allocated_size;
+
+	new_object->type = object_type;
+	new_object->is_static = RT_FALSE;
+
+	rtgui_type_object_construct(object_type, new_object);
+
+	return new_object;
+}
+
+/**
+ * @brief Destroys the object: it first sets the weak-pointers to RT_NULL, emits the "destroyed" signal, and then
+ * queues the object in the list of objects to free. Thus, the destructors will only be called at the beginning of the
+ * next main loop iteration (from the destructor of the more derived class to the destructor of the ultimate base class).
+ * @param object the object to destroy
+ * @warning You should not assume that this function will call directly the destructors of the object!
+ */
+void rtgui_object_destroy(rtgui_object_t *object)
+{
+	if (!object || object->is_static == RT_TRUE) return;
+
+	obj_info.objs_number --;
+	obj_info.allocated_size -= object->type->size;
+
+	/* call destructor */
+	RT_ASSERT(object->type != RT_NULL);
+	rtgui_type_destructors_call(object->type, object);
+
+	/* release object */
+	rtgui_free(object);
+}
+
+/**
+ * @internal
+ * @brief Gets the type of a rtgui_object
+ * @return Returns the type of a rtgui_object
+ */
+rtgui_type_t *rtgui_object_type_get(void)
+{
+	static rtgui_type_t *object_type = RT_NULL;
+
+	if (!object_type)
+	{
+		object_type = rtgui_type_create("object", RT_NULL,
+			sizeof(rtgui_object_t), RTGUI_CONSTRUCTOR(_rtgui_object_constructor),
+			RTGUI_DESTRUCTOR(_rtgui_object_destructor));
+	}
+
+	return object_type;
+}
+
+/**
+ * @brief Checks if @a object can be cast to @a type.
+ * If @a object doesn't inherit from @a type, a warning is displayed in the console but the object is returned anyway.
+ * @param object the object to cast
+ * @param type the type to which we cast the object
+ * @return Returns the object
+ * @note You usually do not need to call this function, use specific macros instead (ETK_IS_WIDGET() for example)
+ */
+rtgui_object_t *rtgui_object_check_cast(rtgui_object_t *object, rtgui_type_t *type)
+{
+	if (!object) return RT_NULL;
+
+	if (!rtgui_type_inherits_from(object->type, type))
+	{
+		rt_kprintf("Invalid cast from \"%s\" to \"%s\"\n", rtgui_type_name_get(object->type), rtgui_type_name_get(type));
+	}
+
+	return object;
+}
+
+/**
+ * @brief Gets the type of the object
+ * @param object an object
+ * @return Returns the type of @a object (RT_NULL on failure)
+ */
+rtgui_type_t *rtgui_object_object_type_get(rtgui_object_t *object)
+{
+	if (!object) return RT_NULL;
+
+	return object->type;
+}

+ 667 - 0
rtgui/common/rtgui_system.c

@@ -0,0 +1,667 @@
+/*
+ * File      : rtgui_system.c
+ * This file is part of RTGUI in 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-04     Bernard      first version
+ */
+
+#include <rtgui/rtgui.h>
+#include <rtgui/driver.h>
+#include <rtgui/image.h>
+#include <rtgui/rtgui_system.h>
+#include <rtgui/rtgui_server.h>
+#include <rtgui/widgets/window.h>
+
+#ifdef __WIN32__
+#define RTGUI_EVENT_DEBUG
+#define RTGUI_MEM_TRACE
+#endif
+
+void rtgui_system_server_init()
+{
+	/* init rtgui_thread */
+	rtgui_thread_system_init();
+
+	/* init image */
+	rtgui_system_image_init();
+	/* init font */
+	rtgui_font_system_init();
+
+	/* init rtgui server */
+	rtgui_panel_init();
+	rtgui_topwin_init();
+	rtgui_server_init();
+}
+
+void rtgui_system_app_init()
+{
+	/* init launcher thread */
+	rtgui_launcher_init();
+
+	/* init pyim */
+	// rtgui_pyim_init();
+
+	/* init term win */
+	// rtgui_term_init();
+}
+
+/************************************************************************/
+/* RTGUI Thread Wrapper                                                 */
+/************************************************************************/
+#ifdef RTGUI_EVENT_DEBUG
+const char *event_string[] =
+{
+	/* panel event */
+	"PANEL_ATTACH",			/* attach to a panel	*/
+	"PANEL_DETACH",			/* detach from a panel	*/
+	"PANEL_SHOW",			/* show in a panel		*/
+	"PANEL_HIDE",			/* hide from a panel	*/
+	"PANEL_INFO",			/* panel information 	*/
+	"PANEL_RESIZE",			/* resize panel 		*/
+	"PANEL_FULLSCREEN",		/* to full screen 		*/
+	"PANEL_NORMAL",			/* to normal screen 	*/
+
+	/* window event */
+	"WIN_CREATE",			/* create a window 	*/
+	"WIN_DESTROY",			/* destroy a window 	*/
+	"WIN_SHOW",				/* show a window 		*/
+	"WIN_HIDE",				/* hide a window 		*/
+	"WIN_ACTIVATE", 		/* activate a window 	*/
+	"WIN_DEACTIVATE",		/* deactivate a window 	*/
+	"WIN_CLOSE",			/* close a window 		*/
+	"WIN_MOVE",				/* move a window 		*/
+	"WIN_RESIZE", 			/* resize a window 		*/
+
+	"SET_WM", 				/* set window manager	*/
+
+	"UPDATE_BEGIN",			/* begin of update rect */
+	"UPDATE_END",			/* end of update rect	*/
+	"MONITOR_ADD",			/* add a monitor rect 	*/
+	"MONITOR_REMOVE", 		/* remove a monitor rect*/
+	"PAINT",				/* paint on screen 		*/
+	"TIMER",				/* timer 				*/
+
+	/* clip rect information */
+	"CLIP_INFO",			/* clip rect info		*/
+
+	/* mouse and keyboard event */
+	"MOUSE_MOTION",			/* mouse motion */
+	"MOUSE_BUTTON",			/* mouse button info 	*/
+	"KBD",					/* keyboard info		*/
+
+	/* user command event */
+	"COMMAND",				/* user command 		*/
+
+	/* request's status event */
+	"STATUS",				/* request result 		*/
+	"SCROLLED",           	/* scroll bar scrolled  */
+	"RESIZE",				/* widget resize 		*/
+};
+
+#define DBG_MSG(x)	rt_kprintf x
+
+static void rtgui_event_dump(rt_thread_t tid, rtgui_event_t* event)
+{
+	char* sender = "(unknown)";
+
+	if (event->sender != RT_NULL) sender = event->sender->name;
+
+	if (event->type == RTGUI_EVENT_TIMER)
+	{
+		/* don't dump timer event */
+		return ;
+	}
+
+	rt_kprintf("%s -- %s --> %s ", sender, event_string[event->type], tid->name);
+	switch (event->type)
+	{
+	case RTGUI_EVENT_PAINT:
+		{
+			struct rtgui_event_paint *paint = (struct rtgui_event_paint *)event;
+
+			if(paint->wid != RT_NULL)
+				rt_kprintf("win: %s", paint->wid->title);
+		}
+		break;
+
+	case RTGUI_EVENT_CLIP_INFO:
+		{
+			struct rtgui_event_clip_info *info = (struct rtgui_event_clip_info *)event;
+
+			if(info->wid != RT_NULL)
+				rt_kprintf("win: %s", info->wid->title);
+		}
+		break;
+
+	case RTGUI_EVENT_WIN_CREATE:
+		{
+			struct rtgui_event_win_create *create = (struct rtgui_event_win_create*)event;
+
+			rt_kprintf(" win: %s at (x1:%d, y1:%d, x2:%d, y2:%d)",
+				create->title,
+				create->extent.x1,
+				create->extent.y1,
+				create->extent.x2,
+				create->extent.y2);
+		}
+		break;
+
+	case RTGUI_EVENT_UPDATE_END:
+		{
+			struct rtgui_event_update_end* update_end = (struct rtgui_event_update_end*)event;
+			rt_kprintf("(x:%d, y1:%d, x2:%d, y2:%d)", update_end->rect.x1,
+				update_end->rect.y1,
+				update_end->rect.x2,
+				update_end->rect.y2);
+		}
+		break;
+
+	case RTGUI_EVENT_WIN_ACTIVATE:
+	case RTGUI_EVENT_WIN_DEACTIVATE:
+	case RTGUI_EVENT_WIN_SHOW:
+		{
+			struct rtgui_event_win *win = (struct rtgui_event_win *)event;
+
+			if(win->wid != RT_NULL)
+				rt_kprintf("win: %s", win->wid->title);
+		}
+		break;
+
+	case RTGUI_EVENT_WIN_MOVE:
+		{
+			struct rtgui_event_win_move *win = (struct rtgui_event_win_move *)event;
+
+			if(win->wid != RT_NULL)
+				rt_kprintf("win: %s", win->wid->title);
+		}
+		break;
+
+	case RTGUI_EVENT_WIN_RESIZE:
+		{
+			struct rtgui_event_win_resize* win = (struct rtgui_event_win_resize *)event;
+
+			if (win->wid != RT_NULL)
+			{
+				rt_kprintf("win: %s, rect(x1:%d, y1:%d, x2:%d, y2:%d)", win->wid->title,
+					RTGUI_WIDGET(win->wid)->extent.x1,
+					RTGUI_WIDGET(win->wid)->extent.y1,
+					RTGUI_WIDGET(win->wid)->extent.x2,
+					RTGUI_WIDGET(win->wid)->extent.y2);
+			}
+		}
+		break;
+
+	case RTGUI_EVENT_MOUSE_BUTTON:
+	case RTGUI_EVENT_MOUSE_MOTION:
+		{
+			struct rtgui_event_mouse *mouse = (struct rtgui_event_mouse*)event;
+
+			if (mouse->button & RTGUI_MOUSE_BUTTON_LEFT) rt_kprintf("left ");
+			else rt_kprintf("right ");
+
+			if (mouse->button & RTGUI_MOUSE_BUTTON_DOWN) rt_kprintf("down ");
+			else rt_kprintf("up ");
+
+			if (mouse->wid != RT_NULL)
+				rt_kprintf("win: %s at (%d, %d)", mouse->wid->title,
+				mouse->x, mouse->y);
+			else
+				rt_kprintf("(%d, %d)", mouse->x, mouse->y);
+		}
+		break;
+
+	case RTGUI_EVENT_MONITOR_ADD:
+		{
+			struct rtgui_event_monitor *monitor = (struct rtgui_event_monitor*)event;
+			if (monitor->panel != RT_NULL)
+			{
+#if 0
+				rt_kprintf("panel: %s, the rect is:(%d, %d) - (%d, %d)", monitor->panel->name,
+					monitor->rect.x1, monitor->rect.y1,
+					monitor->rect.x2, monitor->rect.y2);
+#endif
+				rt_kprintf("the rect is:(%d, %d) - (%d, %d)",
+					monitor->rect.x1, monitor->rect.y1,
+					monitor->rect.x2, monitor->rect.y2);
+			}
+			else if (monitor->wid != RT_NULL)
+			{
+				rt_kprintf("win: %s, the rect is:(%d, %d) - (%d, %d)", monitor->wid->title,
+					monitor->rect.x1, monitor->rect.y1,
+					monitor->rect.x2, monitor->rect.y2);
+			}
+		}
+		break;
+	}
+
+	rt_kprintf("\n");
+}
+#else
+#define DBG_MSG(x)
+#define rtgui_event_dump(tid, event)
+#endif
+
+struct rt_semaphore _rtgui_thread_hash_semaphore;
+
+void rtgui_thread_system_init()
+{
+	rt_sem_init(&_rtgui_thread_hash_semaphore, "rtgui", 1, RT_IPC_FLAG_FIFO);
+}
+
+rtgui_thread_t* rtgui_thread_register(rt_thread_t tid, rt_mq_t mq)
+{
+	rtgui_thread_t* thread = rtgui_malloc(sizeof(struct rtgui_thread));
+
+	if (thread != RT_NULL)
+	{
+		DBG_MSG(("register a rtgui thread: %s, tid: 0x%p\n", tid->name, tid));
+
+		/* set tid and mq */
+		thread->tid			= tid;
+		thread->mq			= mq;
+		thread->widget		= RT_NULL;
+
+		/* take semaphore */
+		rt_sem_take(&_rtgui_thread_hash_semaphore, RT_WAITING_FOREVER);
+		/* set user thread */
+		tid->user_data = (rt_uint32_t)thread;
+		/* release semaphore */
+		rt_sem_release(&_rtgui_thread_hash_semaphore);
+	}
+
+	return thread;
+}
+
+void rtgui_thread_deregister(rt_thread_t tid)
+{
+	struct rtgui_thread* thread;
+
+	/* find rtgui_thread */
+	thread = (struct rtgui_thread*) (tid->user_data);
+
+	if (thread != RT_NULL)
+	{
+		/* take semaphore */
+		rt_sem_take(&_rtgui_thread_hash_semaphore, RT_WAITING_FOREVER);
+		/* remove rtgui_thread */
+		tid->user_data = 0;
+		/* release semaphore */
+		rt_sem_release(&_rtgui_thread_hash_semaphore);
+
+		/* free rtgui_thread */
+		rtgui_free(thread);
+	}
+}
+
+extern rt_thread_t rt_thread_find(char* name);
+rt_thread_t rtgui_thread_get_server()
+{
+	return rt_thread_find("rtgui");
+}
+
+void rtgui_thread_set_widget(struct rtgui_widget* widget)
+{
+	struct rtgui_thread* thread;
+
+	/* get rtgui_thread */
+	thread = (struct rtgui_thread*) (rt_thread_self()->user_data);
+
+	if (thread != RT_NULL) thread->widget = widget;
+}
+
+struct rtgui_widget* rtgui_thread_get_widget()
+{
+	struct rtgui_thread* thread;
+
+	/* get rtgui_thread */
+	thread = (struct rtgui_thread*) (rt_thread_self()->user_data);
+
+	return thread == RT_NULL? RT_NULL : thread->widget;
+}
+
+rt_err_t rtgui_thread_send(rt_thread_t tid, rtgui_event_t* event, rt_size_t event_size)
+{
+	struct rtgui_thread* thread;
+
+	rtgui_event_dump(tid, event);
+
+	/* find rtgui_thread */
+	thread = (struct rtgui_thread*) (tid->user_data);
+	if (thread == RT_NULL) return -RT_ERROR;
+
+	return rt_mq_send(thread->mq, event, event_size);
+}
+
+rt_err_t rtgui_thread_send_urgent(rt_thread_t tid, rtgui_event_t* event, rt_size_t event_size)
+{
+	struct rtgui_thread* thread;
+
+	rtgui_event_dump(tid, event);
+
+	/* find rtgui_thread */
+	thread = (struct rtgui_thread*) (tid->user_data);
+	if (thread == RT_NULL) return -RT_ERROR;
+
+	return rt_mq_urgent(thread->mq, event, event_size);
+}
+
+rt_err_t rtgui_thread_send_sync(rt_thread_t tid, rtgui_event_t* event, rt_size_t event_size)
+{
+	rt_err_t r;
+	struct rtgui_thread* thread;
+	rt_int32_t ack_buffer, ack_status;
+	struct rt_mailbox ack_mb;
+
+	rtgui_event_dump(tid, event);
+
+	/* init ack mailbox */
+	r = rt_mb_init(&ack_mb, "ack", &ack_buffer, 1, 0);
+	if ( r!= RT_EOK) goto __return;
+
+	/* find rtgui_thread */
+	thread = (struct rtgui_thread*) (tid->user_data);
+	if (thread == RT_NULL){ r = RT_ERROR; goto __return; }
+
+	event->ack = &ack_mb;
+	r = rt_mq_send(thread->mq, event, event_size);
+	if (r != RT_EOK) goto __return;
+
+	r = rt_mb_recv(&ack_mb, (rt_uint32_t*)&ack_status, RT_WAITING_FOREVER);
+	if ( r!= RT_EOK) goto __return;
+
+	if (ack_status != RTGUI_STATUS_OK) r = -RT_ERROR;
+	else r = RT_EOK;
+
+	/* fini ack mailbox */
+	rt_mb_detach(&ack_mb);
+
+__return:
+	return r;
+}
+
+rt_err_t rtgui_thread_ack(rtgui_event_t* event, rt_int32_t status)
+{
+	if (event != RT_NULL &&
+		event->ack != RT_NULL)
+	{
+		rt_mb_send(event->ack, status);
+	}
+
+	return RT_EOK;
+}
+
+rt_err_t rtgui_thread_recv(rtgui_event_t* event, rt_size_t event_size)
+{
+	struct rtgui_thread* thread;
+	rt_err_t r;
+
+	/* find rtgui_thread */
+	thread = (struct rtgui_thread*) (rt_thread_self()->user_data);
+	if (thread == RT_NULL) return -RT_ERROR;
+
+	r = rt_mq_recv(thread->mq, event, event_size, RT_WAITING_FOREVER);
+
+	return r;
+}
+
+rt_err_t rtgui_thread_recv_filter(rt_uint32_t type, rtgui_event_t* event, rt_size_t event_size)
+{
+	struct rtgui_thread* thread;
+
+	/* find rtgui_thread */
+	thread = (struct rtgui_thread*) (rt_thread_self()->user_data);
+	if (thread == RT_NULL) return -RT_ERROR;
+
+	while (rt_mq_recv(thread->mq, event, event_size, RT_WAITING_FOREVER) == RT_EOK)
+	{
+		if (event->type == type)
+		{
+			return RT_EOK;
+		}
+		else
+		{
+			/* let widget to handle event */
+			if (thread->widget != RT_NULL &&
+				thread->widget->event_handler != RT_NULL)
+			{
+				thread->widget->event_handler(thread->widget, event);
+			}
+		}
+	}
+
+	return -RT_ERROR;
+}
+
+/************************************************************************/
+/* RTGUI Timer                                                          */
+/************************************************************************/
+static void rtgui_time_out(void* parameter)
+{
+	rtgui_timer_t* timer;
+	struct rtgui_event_timer event;
+	timer = (rtgui_timer_t*)parameter;
+
+	/*
+	* Note: event_timer can not use RTGUI_EVENT_TIMER_INIT to init, for there is no
+	* thread context
+	*/
+	event.parent.type = RTGUI_EVENT_TIMER;
+	event.parent.sender = RT_NULL;
+
+	event.callback = timer->timeout;
+	event.parameter = timer->user_data;
+
+	rtgui_thread_send(timer->tid, &(event.parent), sizeof(struct rtgui_event_timer));
+}
+
+rtgui_timer_t* rtgui_timer_create(rt_int32_t time, rt_base_t flag, rtgui_timeout_func timeout, void* parameter)
+{
+	rtgui_timer_t* timer;
+
+	timer = (rtgui_timer_t*) rtgui_malloc(sizeof(struct rtgui_timer));
+	timer->tid = rt_thread_self();
+	timer->timeout = timeout;
+	timer->user_data = parameter;
+
+	/* init rt-thread timer */
+	rt_timer_init(&(timer->timer), "rtgui", rtgui_time_out, timer, time, (rt_uint8_t)flag);
+
+	return timer;
+}
+
+void rtgui_timer_destory(rtgui_timer_t* timer)
+{
+	RT_ASSERT(timer != RT_NULL);
+
+	/* stop timer firstly */
+	rtgui_timer_stop(timer);
+
+	/* detach rt-thread timer */
+	rt_timer_detach(&(timer->timer));
+
+	rtgui_free(timer);
+}
+
+void rtgui_timer_start(rtgui_timer_t* timer)
+{
+	RT_ASSERT(timer != RT_NULL);
+
+	/* start rt-thread timer */
+	rt_timer_start(&(timer->timer));
+}
+
+void rtgui_timer_stop (rtgui_timer_t* timer)
+{
+	RT_ASSERT(timer != RT_NULL);
+
+	/* stop rt-thread timer */
+	rt_timer_stop(&(timer->timer));
+}
+
+/************************************************************************/
+/* RTGUI Memory Management                                              */
+/************************************************************************/
+#ifdef RTGUI_MEM_TRACE
+struct rtgui_mem_info
+{
+	rt_uint32_t allocated_size;
+	rt_uint32_t max_allocated;
+};
+struct rtgui_mem_info mem_info;
+
+#define MEMTRACE_MAX		4096
+#define MEMTRACE_HASH_SIZE	256
+
+struct rti_memtrace_item
+{
+	void*		mb_ptr;		/* memory block pointer */
+	rt_uint32_t mb_len;		/* memory block length */
+
+	struct rti_memtrace_item* next;
+};
+struct rti_memtrace_item trace_list[MEMTRACE_MAX];
+struct rti_memtrace_item *item_hash[MEMTRACE_HASH_SIZE];
+struct rti_memtrace_item *item_free;
+
+rt_bool_t rti_memtrace_inited = 0;
+void rti_memtrace_init()
+{
+	struct rti_memtrace_item *item;
+	rt_uint32_t index;
+
+	rt_memset(trace_list, 0, sizeof(trace_list));
+	rt_memset(item_hash, 0, sizeof(item_hash));
+
+	item_free = &trace_list[0];
+	item = &trace_list[0];
+
+	for (index = 1; index < MEMTRACE_HASH_SIZE; index ++)
+	{
+		item->next = &trace_list[index];
+		item = item->next;
+	}
+
+	item->next = RT_NULL;
+}
+
+void rti_malloc_hook(void* ptr, rt_uint32_t len)
+{
+	rt_uint32_t index;
+	struct rti_memtrace_item* item;
+
+	if (item_free == RT_NULL) return;
+
+	mem_info.allocated_size += len;
+	if (mem_info.max_allocated < mem_info.allocated_size)
+		mem_info.max_allocated = mem_info.allocated_size;
+
+	/* lock context */
+	item = item_free;
+	item_free = item->next;
+
+	item->mb_ptr = ptr;
+	item->mb_len = len;
+	item->next   = RT_NULL;
+
+	/* get hash item index */
+	index = ((rt_uint32_t)ptr) % MEMTRACE_HASH_SIZE;
+	if (item_hash[index] != RT_NULL)
+	{
+		/* add to list */
+		item->next = item_hash[index];
+		item_hash[index] = item;
+	}
+	else
+	{
+		/* set list header */
+		item_hash[index] = item;
+	}
+	/* unlock context */
+}
+
+void rti_free_hook(void* ptr)
+{
+	rt_uint32_t index;
+	struct rti_memtrace_item *item;
+
+	/* get hash item index */
+	index = ((rt_uint32_t)ptr) % MEMTRACE_HASH_SIZE;
+	if (item_hash[index] != RT_NULL)
+	{
+		item = item_hash[index];
+		if (item->mb_ptr == ptr)
+		{
+			/* delete item from list */
+			item_hash[index] = item->next;
+		}
+		else
+		{
+			/* find ptr in list */
+			while (item->next != RT_NULL && item->next->mb_ptr != ptr)
+				item = item->next;
+
+			/* delete item from list */
+			if (item->next != RT_NULL)
+			{
+				struct rti_memtrace_item* i;
+
+				i = item->next;
+				item->next = item->next->next;
+
+				item = i;
+			}
+			else
+			{
+				/* not found */
+				return;
+			}
+		}
+
+		/* reduce allocated size */
+		mem_info.allocated_size -= item->mb_len;
+
+		/* clear item */
+		rt_memset(item, 0, sizeof(struct rti_memtrace_item));
+
+		/* add item to the free list */
+		item->next = item_free;
+		item_free = item;
+	}
+}
+#endif
+
+void* rtgui_malloc(rt_size_t size)
+{
+	void* ptr;
+
+	ptr = rt_malloc(size);
+#ifdef RTGUI_MEM_TRACE
+	if (rti_memtrace_inited == 0)
+	{
+		rti_memtrace_init();
+		rti_memtrace_inited = 1;
+	}
+
+	if (ptr != RT_NULL)
+		rti_malloc_hook(ptr, size);
+#endif
+
+	return ptr;
+}
+
+void rtgui_free(void* ptr)
+{
+#ifdef RTGUI_MEM_TRACE
+	if (ptr != RT_NULL)
+		rti_free_hook(ptr);
+#endif
+
+	rt_free(ptr);
+}

+ 439 - 0
rtgui/common/rtgui_theme.c

@@ -0,0 +1,439 @@
+/*
+ * File      : rtgui_theme.c
+ * This file is part of RTGUI in 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-04     Bernard      first version
+ */
+
+#include <rtgui/rtgui.h>
+#include <rtgui/dc.h>
+#include <rtgui/widgets/widget.h>
+#include <rtgui/widgets/button.h>
+#include <rtgui/widgets/label.h>
+#include <rtgui/widgets/textbox.h>
+#include <rtgui/widgets/iconbox.h>
+#include <rtgui/rtgui_theme.h>
+#include <rtgui/rtgui_server.h>
+
+#define WINTITLE_CB_WIDTH		14
+#define WINTITLE_CB_HEIGHT		14
+
+static const char * close_unpressed_xpm[] = {
+	"14 14 55 1",
+	" 	c None",
+	".	c #DCDFEA",
+	"+	c #A4ADD3",
+	"@	c #8F9ACA",
+	"#	c #98A2CD",
+	"$	c #D2D6E6",
+	"%	c #F7F7F7",
+	"&	c #F6F6F6",
+	"*	c #B9C1D0",
+	"=	c #7A8AAA",
+	"-	c #D6DAE2",
+	";	c #D8DCE3",
+	">	c #7485A5",
+	",	c #455C89",
+	"'	c #516690",
+	")	c #D3D8E0",
+	"!	c #536891",
+	"~	c #6D7FA1",
+	"{	c #F5F5F5",
+	"]	c #D1D6DF",
+	"^	c #D2D7DF",
+	"/	c #D5D9E1",
+	"(	c #4E648E",
+	"_	c #CFD4DE",
+	":	c #F4F4F4",
+	"<	c #D0D5DE",
+	"[	c #CED3DD",
+	"}	c #F3F3F3",
+	"|	c #CFD4DD",
+	"1	c #CDD2DC",
+	"2	c #F2F2F2",
+	"3	c #D3D7DF",
+	"4	c #526790",
+	"5	c #D0D5DD",
+	"6	c #F1F1F1",
+	"7	c #D2D6DE",
+	"8	c #CFD4DC",
+	"9	c #F0F0F0",
+	"0	c #D1D5DD",
+	"a	c #C9CED8",
+	"b	c #CDD2DB",
+	"c	c #50658F",
+	"d	c #CED3DB",
+	"e	c #7283A3",
+	"f	c #6E80A2",
+	"g	c #EFEFEF",
+	"h	c #B2BACA",
+	"i	c #7081A2",
+	"j	c #C8CDD7",
+	"k	c #CCD1DA",
+	"l	c #ACB5C7",
+	"m	c #D0D4E2",
+	"n	c #EEEEEE",
+	"o	c #D2D5E3",
+	"p	c #97A1CC",
+	".+@@@@@@@@@@+.",
+	"#$%%%%%%%%%%$#",
+	"@&*=-&&&&;=*&@",
+	"@&>,')&&-!,~&@",
+	"@{]','^/!,(_{@",
+	"@::<','!,([::@",
+	"@}}}|',,(1}}}@",
+	"@22234,,'5222@",
+	"@6674,(','866@",
+	"@904,(abc,cd9@",
+	"@9e,(a99bc,f9@",
+	"@ghijggggkelg@",
+	"#mnnnnnnnnnnm#",
+	"op@@@@@@@@@@po"};
+
+static const char * close_pressed_xpm[] = {
+	"14 14 66 1",
+	" 	c None",
+	".	c #CED4EE",
+	"+	c #7E90DD",
+	"@	c #6076D7",
+	"#	c #6C80D9",
+	"$	c #BFC8EA",
+	"%	c #F2F3F5",
+	"&	c #F0F2F3",
+	"*	c #A5B6D7",
+	"=	c #587ABB",
+	"-	c #C9D3E4",
+	";	c #CBD5E4",
+	">	c #EEF0F2",
+	",	c #5073B7",
+	"'	c #1746A3",
+	")	c #2551A8",
+	"!	c #C5CFE2",
+	"~	c #C8D1E3",
+	"{	c #2853A8",
+	"]	c #496DB4",
+	"^	c #ECEEF1",
+	"/	c #C0CCE0",
+	"(	c #C3CEE1",
+	"_	c #C6D0E2",
+	":	c #224FA7",
+	"<	c #BEC9DF",
+	"[	c #EAECF0",
+	"}	c #BFCAE0",
+	"|	c #2551A7",
+	"1	c #224EA7",
+	"2	c #BCC8DF",
+	"3	c #E8EBEE",
+	"4	c #BDCADE",
+	"5	c #BAC7DD",
+	"6	c #E6E9ED",
+	"7	c #C1CBDF",
+	"8	c #2753A8",
+	"9	c #BECADE",
+	"0	c #E4E7EB",
+	"a	c #BFCADD",
+	"b	c #224EA6",
+	"c	c #BDC8DC",
+	"d	c #E1E5EA",
+	"e	c #2752A8",
+	"f	c #B3C0D9",
+	"g	c #B8C5DB",
+	"h	c #2451A7",
+	"i	c #BAC6DB",
+	"j	c #DFE2E8",
+	"k	c #4C70B4",
+	"l	c #B2BED8",
+	"m	c #B6C2D9",
+	"n	c #2450A7",
+	"o	c #486BB3",
+	"p	c #DCE0E7",
+	"q	c #96A8CE",
+	"r	c #496CB3",
+	"s	c #AFBCD7",
+	"t	c #B4C1D8",
+	"u	c #4B6FB4",
+	"v	c #8EA4CC",
+	"w	c #6C80D8",
+	"x	c #B4BEDF",
+	"y	c #DADEE5",
+	"z	c #B5BEDE",
+	"A	c #6A7ED7",
+	".+@@@@@@@@@@+.",
+	"#$%%%%%%%%%%$#",
+	"@&*=-&&&&;=*&@",
+	"@>,')!>>~{']>@",
+	"@^/)')(_{':<^@",
+	"@[[}|'|{'12[[@",
+	"@3334|''15333@",
+	"@66678''|9666@",
+	"@00a8'b|'|c00@",
+	"@dce'bfgh'hid@",
+	"@jk'bljjmn'oj@",
+	"@pqrspppptuvp@",
+	"wxyyyyyyyyyyxw",
+	"zA@@@@@@@@@@Az"};
+
+static rtgui_image_t* close_pressed = RT_NULL;
+static rtgui_image_t* close_unpressed = RT_NULL;
+
+/* window drawing */
+void rtgui_theme_draw_win(struct rtgui_topwin* win)
+{
+	struct rtgui_dc* dc;
+	rtgui_rect_t rect;
+
+	/* init close box image */
+	if (close_pressed == RT_NULL)
+		close_pressed = rtgui_image_create_from_mem("xpm", close_pressed_xpm, sizeof(close_pressed_xpm));
+	if (close_unpressed == RT_NULL)
+		close_unpressed = rtgui_image_create_from_mem("xpm", close_unpressed_xpm, sizeof(close_unpressed_xpm));
+
+	/* begin drawing */
+	dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(win->title));
+
+	/* get rect */
+	rtgui_widget_get_rect(RTGUI_WIDGET(win->title), &rect);
+
+	/* draw border */
+	if (win->flag & WINTITLE_BORDER)
+	{
+		RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(win->title)) = RTGUI_RGB(219, 210, 243);
+		rtgui_dc_draw_rect(dc, &rect);
+
+		/* shrink border */
+		rect.x1 += WINTITLE_BORDER_SIZE;
+		rect.y1 += WINTITLE_BORDER_SIZE;
+		rect.x2 -= WINTITLE_BORDER_SIZE;
+		rect.y2 -= WINTITLE_BORDER_SIZE;
+	}
+
+	/* draw title */
+	if (!(win->flag & WINTITLE_NO))
+	{
+		if (win->flag & WINTITLE_ACTIVATE)
+		{
+			RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(win->title)) = RTGUI_RGB(229, 236, 249);
+			RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(win->title)) = RTGUI_RGB( 51, 102, 204);
+		}
+		else
+		{
+			RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(win->title)) = RTGUI_RGB(242, 245, 252);
+			RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(win->title)) = RTGUI_RGB(153, 178, 229);
+		}
+		rtgui_dc_fill_rect(dc, &rect);
+
+		rect.x1 += 4;
+		rect.y1 += 2;
+		rtgui_dc_draw_text(dc, rtgui_wintitle_get_title(win->title), &rect);
+
+		if (win->flag & WINTITLE_CLOSEBOX)
+		{
+			/* get close button rect */
+			rect.x1 = rtgui_rect_width(RTGUI_WIDGET(win->title)->extent) -
+				WINTITLE_BORDER_SIZE - WINTITLE_CB_WIDTH - 3;
+			rect.y1 = 3;
+			rect.x2 = rect.x1 + WINTITLE_CB_WIDTH;
+			rect.y2 = rect.y1 + WINTITLE_CB_HEIGHT;
+
+			/* draw close box */
+			if(win->flag & WINTITLE_CB_PRESSED) rtgui_image_blit(close_pressed, dc, &rect);
+			else rtgui_image_blit(close_unpressed, dc, &rect);
+		}
+	}
+
+	rtgui_dc_end_drawing(dc);
+}
+
+/* widget drawing */
+void rtgui_theme_draw_button(rtgui_button_t* btn)
+{
+	/* draw button */
+	struct rtgui_dc* dc;
+	struct rtgui_rect rect;
+
+	/* begin drawing */
+	dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(btn));
+	if (dc == RT_NULL) return;
+
+	rtgui_widget_get_rect(RTGUI_WIDGET(btn), &rect);
+
+	/* fill button rect with background color */
+	// RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(btn)) = RTGUI_RGB(212, 208, 200);
+	rtgui_dc_fill_rect(dc, &rect);
+
+	if (btn->flag & RTGUI_BUTTON_TYPE_PUSH && btn->flag & RTGUI_BUTTON_FLAG_PRESS)
+	{
+		/* draw border */
+		RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(btn)) = RTGUI_RGB(64, 64, 64);
+		rtgui_dc_draw_hline(dc, rect.x1, rect.x2, rect.y1);
+		rtgui_dc_draw_vline(dc, rect.x1, rect.y1, rect.y2);
+
+		RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(btn)) = RTGUI_RGB(128, 128, 128);
+		rtgui_dc_draw_hline(dc, rect.x1, rect.x2 - 1, rect.y1 + 1);
+		rtgui_dc_draw_vline(dc, rect.x1 + 1, rect.y1 + 1, rect.y2 - 2);
+
+		RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(btn)) = RTGUI_RGB(255, 255, 255);
+		rtgui_dc_draw_hline(dc, rect.x1, rect.x2 + 1, rect.y2 - 1);
+		rtgui_dc_draw_vline(dc, rect.x2 - 1, rect.y1, rect.y2);
+
+		if (btn->pressed_image != RT_NULL)
+		{
+			rtgui_rect_t image_rect = {0, 0, btn->unpressed_image->w, btn->unpressed_image->h};
+			rtgui_rect_moveto_align(&rect, &image_rect, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL);
+
+			rtgui_image_blit(btn->pressed_image, dc, &image_rect);
+		}
+	}
+	else if (btn->flag & RTGUI_BUTTON_FLAG_PRESS)
+	{
+		if (btn->pressed_image != RT_NULL)
+		{
+			rtgui_rect_t image_rect = {0, 0, btn->unpressed_image->w, btn->unpressed_image->h};
+			rtgui_rect_moveto_align(&rect, &image_rect, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL);
+
+			rtgui_image_blit(btn->pressed_image, dc, &image_rect);
+		}
+		else
+		{
+			/* draw border */
+			RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(0, 0, 0);
+			rtgui_dc_draw_rect(dc, &rect);
+
+			RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(128, 128, 128);
+			rect.x1 += 1; rect.y1 += 1; rect.x2 -= 1; rect.y2 -= 1;
+			rtgui_dc_draw_rect(dc, &rect);
+		}
+	}
+	else
+	{
+		if (btn->unpressed_image != RT_NULL)
+		{
+			rtgui_rect_t image_rect = {0, 0, btn->unpressed_image->w, btn->unpressed_image->h};
+			rtgui_rect_moveto_align(&rect, &image_rect, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL);
+
+			rtgui_image_blit(btn->unpressed_image, dc, &image_rect);
+		}
+		else
+		{
+			/* draw border */
+			RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(255, 255, 255);
+			rtgui_dc_draw_hline(dc, rect.x1, rect.x2, rect.y1);
+			rtgui_dc_draw_vline(dc, rect.x1, rect.y1, rect.y2);
+
+			RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(0, 0, 0);
+			rtgui_dc_draw_hline(dc, rect.x1, rect.x2 + 1, rect.y2);
+			rtgui_dc_draw_vline(dc, rect.x2, rect.y1, rect.y2);
+
+			RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(128, 128, 128);
+			rtgui_dc_draw_hline(dc, rect.x1 + 1, rect.x2, rect.y2 - 1);
+			rtgui_dc_draw_vline(dc, rect.x2 - 1, rect.y1 + 1, rect.y2 - 1);
+		}
+	}
+
+	if (btn->pressed_image == RT_NULL)
+	{
+		/* re-set foreground and get default rect */
+		RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(0, 0, 0);
+		rtgui_widget_get_rect(RTGUI_WIDGET(btn), &rect);
+
+		/* remove border */
+		rtgui_rect_inflate(&rect, -2);
+
+		/* draw text */
+		rtgui_dc_draw_text(dc, rtgui_label_get_text(RTGUI_LABEL(btn)), &rect);
+	}
+
+	/* end drawing */
+	rtgui_dc_end_drawing(dc);
+}
+
+void rtgui_theme_draw_label(rtgui_label_t* label)
+{
+	/* draw label */
+	struct rtgui_dc* dc;
+	struct rtgui_rect rect;
+
+	/* begin drawing */
+	dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(label));
+	if (dc == RT_NULL) return;
+
+	rtgui_widget_get_rect(RTGUI_WIDGET(label), &rect);
+	rtgui_dc_fill_rect(dc, &rect);
+
+	/* default left and center draw */
+	rect.y1 = rect.y1 + (rtgui_rect_height(rect) - 8)/2;
+	rtgui_dc_draw_text(dc, rtgui_label_get_text(label), &rect);
+
+	/* end drawing */
+	rtgui_dc_end_drawing(dc);
+}
+
+#define RTGUI_TEXTBOX_MARGIN	3
+void rtgui_theme_draw_textbox(rtgui_textbox_t* box)
+{
+	/* draw button */
+	struct rtgui_dc* dc;
+	struct rtgui_rect rect;
+
+	/* begin drawing */
+	dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(box));
+	if (dc == RT_NULL) return;
+
+	/* get widget rect */
+	rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect);
+
+	/* fill widget rect with background color */
+	rtgui_dc_fill_rect(dc, &rect);
+
+	/* draw border */
+	rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_STATIC);
+
+	/* draw text */
+	if (box->text != RT_NULL)
+	{
+		rect.x1 += RTGUI_TEXTBOX_MARGIN;
+
+		rtgui_dc_draw_text(dc, box->text, &rect);
+	}
+
+	/* end drawing */
+	rtgui_dc_end_drawing(dc);
+}
+
+void rtgui_theme_draw_iconbox(rtgui_iconbox_t* iconbox)
+{
+	struct rtgui_dc* dc;
+	struct rtgui_rect rect;
+
+	/* begin drawing */
+	dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(iconbox));
+	if (dc == RT_NULL) return;
+
+	/* get widget rect */
+	rtgui_widget_get_rect(RTGUI_WIDGET(iconbox), &rect);
+
+	/* draw icon */
+	rtgui_image_blit(iconbox->image, dc, &rect);
+
+	/* draw text */
+	if (iconbox->text_position == RTGUI_ICONBOX_TEXT_BELOW && iconbox->text != RT_NULL)
+	{
+		rect.y1 = iconbox->image->h + RTGUI_WIDGET_DEFAULT_MARGIN;
+		rtgui_dc_draw_text(dc, iconbox->text, &rect);
+	}
+	else if (iconbox->text_position == RTGUI_ICONBOX_TEXT_RIGHT && iconbox->text != RT_NULL)
+	{
+		rect.x1 = iconbox->image->w + RTGUI_WIDGET_DEFAULT_MARGIN;
+		rtgui_dc_draw_text(dc, iconbox->text, &rect);
+	}
+
+	/* end drawing */
+	rtgui_dc_end_drawing(dc);
+}

+ 58 - 0
rtgui/include/rtgui/driver.h

@@ -0,0 +1,58 @@
+/*
+ * File      : driver.h
+ * This file is part of RTGUI in 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-04     Bernard      first version
+ */
+#ifndef __RTGUI_DRIVER_H__
+#define __RTGUI_DRIVER_H__
+
+#include <rtgui/list.h>
+#include <rtgui/color.h>
+
+struct rtgui_graphic_driver
+{
+	/* driver name */
+	char* name;
+
+	/* byte per pixel */
+	rt_uint16_t byte_per_pixel;
+
+	/* screen width and height */
+	rt_uint16_t width;
+	rt_uint16_t height;
+
+	/* screen update */
+	void (*screen_update)(rtgui_rect_t* rect);
+
+	/* get video frame buffer */
+	rt_uint8_t* (*get_framebuffer)(void);
+
+	/* set and get pixel in (x, y) */
+	void (*set_pixel) (rtgui_color_t *c, rt_base_t x, rt_base_t y);
+	void (*get_pixel) (rtgui_color_t *c, rt_base_t x, rt_base_t y);
+
+	void (*draw_hline)(rtgui_color_t *c, rt_base_t x1, rt_base_t x2, rt_base_t y);
+	void (*draw_vline)(rtgui_color_t *c, rt_base_t x , rt_base_t y1, rt_base_t y2);
+
+	/* the driver list */
+	rtgui_list_t list;
+};
+
+void rtgui_graphic_driver_add(struct rtgui_graphic_driver* driver);
+void rtgui_graphic_driver_remove(struct rtgui_graphic_driver* driver);
+
+struct rtgui_graphic_driver* rtgui_graphic_driver_find(char* name);
+struct rtgui_graphic_driver* rtgui_graphic_driver_get_default(void);
+
+void rtgui_graphic_driver_get_rect(struct rtgui_graphic_driver *driver, rtgui_rect_t *rect);
+void rtgui_graphic_driver_get_default_rect(rtgui_rect_t *rect);
+
+#endif

+ 425 - 0
rtgui/include/rtgui/event.h

@@ -0,0 +1,425 @@
+/*
+ * File      : event.h
+ * This file is part of RTGUI in 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-04     Bernard      first version
+ */
+#ifndef __RTGUI_EVENT_H__
+#define __RTGUI_EVENT_H__
+
+#include <rtgui/rtgui.h>
+#include <rtgui/kbddef.h>
+
+enum _rtgui_event_type
+{
+	/* panel event */
+	RTGUI_EVENT_PANEL_ATTACH = 0,	/* attach to a panel	*/
+	RTGUI_EVENT_PANEL_DETACH,		/* detach from a panel	*/
+	RTGUI_EVENT_PANEL_SHOW,			/* show in a panel		*/
+	RTGUI_EVENT_PANEL_HIDE,			/* hide from a panel	*/
+	RTGUI_EVENT_PANEL_INFO,			/* panel information 	*/
+	RTGUI_EVENT_PANEL_RESIZE,		/* resize panel 		*/
+	RTGUI_EVENT_PANEL_FULLSCREEN,	/* to full screen 		*/
+	RTGUI_EVENT_PANEL_NORMAL,		/* to normal screen 	*/
+
+	/* window event */
+	RTGUI_EVENT_WIN_CREATE,			/* create a window 		*/
+	RTGUI_EVENT_WIN_DESTROY,		/* destroy a window 	*/
+	RTGUI_EVENT_WIN_SHOW,			/* show a window 		*/
+	RTGUI_EVENT_WIN_HIDE,			/* hide a window 		*/
+	RTGUI_EVENT_WIN_ACTIVATE, 		/* activate a window 	*/
+	RTGUI_EVENT_WIN_DEACTIVATE,		/* deactivate a window 	*/
+	RTGUI_EVENT_WIN_CLOSE,			/* close a window 		*/
+	RTGUI_EVENT_WIN_MOVE,			/* move a window 		*/
+	RTGUI_EVENT_WIN_RESIZE, 		/* resize a window 		*/
+
+	/* WM event */
+	RTGUI_EVENT_SET_WM,				/* set window manager   */
+
+	RTGUI_EVENT_UPDATE_BEGIN,		/* update a rect 		*/
+	RTGUI_EVENT_UPDATE_END,			/* update a rect 		*/
+	RTGUI_EVENT_MONITOR_ADD,		/* add a monitor rect 	*/
+	RTGUI_EVENT_MONITOR_REMOVE,		/* remove a monitor rect*/
+	RTGUI_EVENT_PAINT,				/* paint on screen 		*/
+	RTGUI_EVENT_TIMER,				/* timer 				*/
+
+	/* clip rect information */
+	RTGUI_EVENT_CLIP_INFO,			/* clip rect info		*/
+
+	/* mouse and keyboard event */
+	RTGUI_EVENT_MOUSE_MOTION,		/* mouse motion */
+	RTGUI_EVENT_MOUSE_BUTTON,		/* mouse button info 	*/
+	RTGUI_EVENT_KBD,				/* keyboard info		*/
+
+	/* user command event */
+	RTGUI_EVENT_COMMAND,			/* user command 		*/
+
+	/* widget event */
+	RTGUI_EVENT_FOCUSED,			/* widget focused       */
+	RTGUI_EVENT_SCROLLED,           /* scroll bar scrolled  */
+	RTGUI_EVENT_RESIZE,				/* widget resize 		*/
+};
+typedef enum _rtgui_event_type rtgui_event_type;
+
+enum {
+	RTGUI_STATUS_OK = 0,		/* status ok 		*/
+	RTGUI_STATUS_ERROR,			/* generic error 	*/
+	RTGUI_STATUS_NRC,			/* no resource 		*/
+};
+
+struct rtgui_event
+{
+	/* the event type */
+	rt_uint32_t type;
+
+	/* the event sender */
+	rt_thread_t sender;
+
+	/* mailbox to acknowledge request */
+	rt_mailbox_t ack;
+};
+typedef struct rtgui_event rtgui_event_t;
+#define RTGUI_EVENT(e)	((struct rtgui_event*)(e))
+
+#define RTGUI_EVENT_INIT(e, t)	do		\
+{										\
+	(e)->type = (t);					\
+	(e)->sender = rt_thread_self();		\
+	(e)->ack = RT_NULL;					\
+} while (0)
+
+/*
+ * RTGUI Panel Event
+ */
+struct rtgui_event_panel_attach
+{
+	struct rtgui_event parent;
+
+	/* the panel name to be attached */
+	char panel_name[RTGUI_NAME_MAX];
+
+	/* workbench, wm field */
+	rtgui_workbench_t* workbench;
+};
+
+struct rtgui_event_panel_detach
+{
+	struct rtgui_event parent;
+
+	/* the panel which thread belong to */
+	rtgui_panel_t* panel;
+
+	/* workbench, wm field */
+	rtgui_workbench_t* workbench;
+};
+
+struct rtgui_event_panel_show
+{
+	struct rtgui_event parent;
+
+	/* the panel which thread belong to */
+	rtgui_panel_t* panel;
+
+	/* workbench, wm field */
+	rtgui_workbench_t* workbench;
+};
+
+struct rtgui_event_panel_hide
+{
+	struct rtgui_event parent;
+
+	/* the panel which thread belong to */
+	rtgui_panel_t* panel;
+
+	/* workbench, wm field */
+	rtgui_workbench_t* workbench;
+};
+
+struct rtgui_event_panel_info
+{
+	struct rtgui_event parent;
+
+	/* panel info */
+	rtgui_panel_t* panel;
+	rtgui_rect_t extent;
+};
+
+#define RTGUI_EVENT_PANEL_ATTACH_INIT(e)	RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_PANEL_ATTACH)
+#define RTGUI_EVENT_PANEL_DETACH_INIT(e)	RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_PANEL_DETACH)
+#define RTGUI_EVENT_PANEL_SHOW_INIT(e)		RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_PANEL_SHOW)
+#define RTGUI_EVENT_PANEL_HIDE_INIT(e)		RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_PANEL_HIDE)
+#define RTGUI_EVENT_PANEL_INFO_INIT(e)		RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_PANEL_INFO)
+
+/*
+ * RTGUI Window Event
+ */
+struct rtgui_event_win
+{
+	struct rtgui_event parent;
+
+	/* the window id */
+	rtgui_win_t* wid;
+};
+
+struct rtgui_event_win_create
+{
+	struct rtgui_event parent;
+
+	/* the window event mask */
+	rt_uint32_t mask;
+
+	/* the window flag */
+	rt_uint32_t flag;
+
+	/* the window title */
+	rt_uint8_t title[RTGUI_NAME_MAX];
+
+	/* the window id */
+	rtgui_win_t* wid;
+	/* the window extent */
+	struct rtgui_rect extent;
+};
+
+struct rtgui_event_win_move
+{
+	struct rtgui_event parent;
+
+	/* the window id */
+	rtgui_win_t* wid;
+
+	rt_int16_t x, y;
+};
+
+struct rtgui_event_win_resize
+{
+	struct rtgui_event parent;
+
+	/* the window id */
+	rtgui_win_t* wid;
+
+	rtgui_rect_t rect;
+};
+
+#define	rtgui_event_win_destroy 	rtgui_event_win
+#define	rtgui_event_win_show 		rtgui_event_win
+#define	rtgui_event_win_hide 		rtgui_event_win
+#define	rtgui_event_win_activate	rtgui_event_win
+#define rtgui_event_win_deactivate 	rtgui_event_win
+#define rtgui_event_win_close 		rtgui_event_win
+
+/* window event init */
+#define RTGUI_EVENT_WIN_CREATE_INIT(e)		RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_WIN_CREATE)
+#define RTGUI_EVENT_WIN_DESTROY_INIT(e)		RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_WIN_DESTROY)
+#define RTGUI_EVENT_WIN_SHOW_INIT(e)		RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_WIN_SHOW)
+#define RTGUI_EVENT_WIN_HIDE_INIT(e)		RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_WIN_HIDE)
+#define RTGUI_EVENT_WIN_ACTIVATE_INIT(e)	RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_WIN_ACTIVATE)
+#define RTGUI_EVENT_WIN_DEACTIVATE_INIT(e)	RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_WIN_DEACTIVATE)
+#define RTGUI_EVENT_WIN_CLOSE_INIT(e)		RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_WIN_CLOSE)
+#define RTGUI_EVENT_WIN_MOVE_INIT(e)		RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_WIN_MOVE)
+#define RTGUI_EVENT_WIN_RESIZE_INIT(e)		RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_WIN_RESIZE)
+
+/*
+ * RTGUI Workbench Manager Event
+ */
+struct rtgui_event_set_wm
+{
+	struct rtgui_event parent;
+
+	/* the panel name to be managed */
+	char panel_name[RTGUI_NAME_MAX];
+};
+/* window event init */
+#define RTGUI_EVENT_SET_WM_INIT(e)		RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_SET_WM)
+
+/*
+ * RTGUI Other Event
+ */
+struct rtgui_event_update_begin
+{
+	struct rtgui_event parent;
+
+	/* the update rect */
+	rtgui_rect_t rect;
+};
+
+struct rtgui_event_update_end
+{
+	struct rtgui_event parent;
+
+	/* the update rect */
+	rtgui_rect_t rect;
+};
+
+struct rtgui_event_monitor
+{
+	struct rtgui_event parent;
+
+	/* the monitor rect */
+	rtgui_rect_t rect;
+
+	/* under panel */
+	rtgui_panel_t* panel;
+
+	/* or under window */
+	rtgui_win_t* wid;
+};
+
+struct rtgui_event_paint
+{
+	struct rtgui_event parent;
+
+	rtgui_win_t* wid;		/* destination window */
+};
+
+struct rtgui_timer;
+struct rtgui_event_timer
+{
+	struct rtgui_event parent;
+
+	void (*callback)(struct rtgui_timer* timer, void* parameter);
+	void *parameter;
+};
+
+struct rtgui_event_clip_info
+{
+	struct rtgui_event parent;
+
+	/* destination window */
+	rtgui_win_t* wid;
+
+	/* the number of rects */
+	rt_uint32_t num_rect;
+
+	struct rtgui_rect rects[0];
+};
+#define RTGUI_EVENT_GET_RECT(e, i)		(&(e->rects[0]) + i)
+
+#define RTGUI_EVENT_UPDATE_BEGIN_INIT(e)	RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_UPDATE_BEGIN)
+#define RTGUI_EVENT_UPDATE_END_INIT(e)		RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_UPDATE_END)
+#define RTGUI_EVENT_MONITOR_ADD_INIT(e)		RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_MONITOR_ADD)
+#define RTGUI_EVENT_MONITOR_REMOVE_INIT(e)	RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_MONITOR_REMOVE)
+#define RTGUI_EVENT_CLIP_INFO_INIT(e)		RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_CLIP_INFO)
+#define RTGUI_EVENT_PAINT_INIT(e)			RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_PAINT)
+#define RTGUI_EVENT_TIMER_INIT(e)			RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_TIMER)
+
+/*
+ * RTGUI Mouse and Keyboard Event
+ */
+struct rtgui_event_mouse
+{
+	struct rtgui_event parent;
+
+	rtgui_win_t* wid;		/* destination window */
+
+	rt_uint16_t x, y;
+	rt_uint16_t button;
+};
+#define RTGUI_MOUSE_BUTTON_LEFT			0x01
+#define RTGUI_MOUSE_BUTTON_RIGHT		0x02
+#define RTGUI_MOUSE_BUTTON_MIDDLE		0x03
+#define RTGUI_MOUSE_BUTTON_WHEELUP		0x04
+#define RTGUI_MOUSE_BUTTON_WHEELDOWN	0x08
+
+#define RTGUI_MOUSE_BUTTON_DOWN			0x10
+#define RTGUI_MOUSE_BUTTON_UP			0x20
+
+struct rtgui_event_kbd
+{
+	struct rtgui_event parent;
+
+	rtgui_win_t* wid;		/* destination window */
+
+	RTGUI_KBD_TYPE type;	/* key down or up */
+	RTGUI_KBD_KEY key;		/* current key */
+	RTGUI_KBD_MOD mod;		/* current key modifiers */
+	rt_uint16_t unicode;	/* translated character */
+};
+#define RTGUI_KBD_IS_SET_CTRL(e)	((e)->mod & (RTGUI_KMOD_LCTRL | RTGUI_KMOD_RCTRL)))
+#define RTGUI_KBD_IS_SET_ALT(e)		((e)->mod & (RTGUI_KMOD_LALT  | RTGUI_KMOD_RALT))
+#define RTGUI_KBD_IS_SET_SHIFT(e)	((e)->mod & (RTGUI_KMOD_LSHIFT| RTGUI_KMOD_RSHIFT))
+#define RTGUI_KBD_IS_UP(e)			((e)->type == RTGUI_KEYUP)
+#define RTGUI_KBD_IS_DOWN(e)		((e)->flag == RTGUI_KEYDOWN)
+
+#define RTGUI_EVENT_MOUSE_MOTION_INIT(e)	RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_MOUSE_MOTION)
+#define RTGUI_EVENT_MOUSE_BUTTON_INIT(e)	RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_MOUSE_BUTTON)
+#define RTGUI_EVENT_KBD_INIT(e)				RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_KBD)
+
+struct rtgui_event_command
+{
+	struct rtgui_event parent;
+
+	/* command type */
+	rt_int32_t type;
+
+	/* command id */
+	rt_int32_t command_id;
+
+	/* command integer */
+	rt_int32_t command_int;
+
+	/* command string */
+	char command_string[RTGUI_NAME_MAX];
+};
+#define RTGUI_EVENT_COMMAND_INIT(e)	RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_COMMAND)
+
+#define RTGUI_CMD_UNKNOWN		0x00
+#define RTGUI_CMD_WM_CLOSE		0x10
+
+#define RTGUI_CMD_USER_INT		0x20
+#define RTGUI_CMD_USER_STRING	0x21
+
+/************************************************************************/
+/* Widget Event                                                         */
+/************************************************************************/
+#define RTGUI_WIDGET_EVENT_INIT(e, t)	do		\
+{										\
+	(e)->type = (t);					\
+	(e)->sender = RT_NULL;				\
+	(e)->ack = RT_NULL;					\
+} while (0)
+
+/*
+ * RTGUI Scrollbar Event
+ */
+struct rtgui_event_scrollbar
+{
+    struct rtgui_event parent;
+
+    rt_uint8_t event;
+};
+#define RTGUI_SCROLL_LINEUP             0x01
+#define RTGUI_SCROLL_LINEDOWN           0x02
+#define RTGUI_SCROLL_PAGEUP             0x03
+#define RTGUI_SCROLL_PAGEDOWN           0x04
+#define RTGUI_EVENT_SCROLLED_INIT(e)	RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_SCROLLED)
+
+/*
+ * RTGUI Widget Focused Event
+ */
+struct rtgui_event_focused
+{
+    struct rtgui_event parent;
+
+	struct rtgui_widget* widget;
+};
+#define RTGUI_EVENT_FOCUSED_INIT(e)		RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_FOCUSED)
+
+/*
+ * RTGUI Widget Resize Event
+ */
+struct rtgui_event_resize
+{
+	struct rtgui_event parent;
+	rt_int16_t x, y;
+	rt_int16_t w, h;
+};
+#define RTGUI_EVENT_RESIZE_INIT(e) RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_RESIZE)
+
+#endif

+ 103 - 0
rtgui/include/rtgui/rtgui.h

@@ -0,0 +1,103 @@
+/*
+ * File      : rtgui.h
+ * This file is part of RTGUI in 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-04     Bernard      first version
+ */
+#ifndef __RT_GUI_H__
+#define __RT_GUI_H__
+
+#include <rtthread.h>
+
+/* RTGUI options */
+#define RTGUI_IMAGE_JPEG
+
+#define RTGUI_NAME_MAX		32
+
+#define RT_INT16_MAX		32767
+#define RT_INT16_MIN		(-RT_INT16_MAX-1)
+
+struct rtgui_panel;
+struct rtgui_event;
+
+struct rtgui_widget;
+struct rtgui_win;
+
+typedef struct rtgui_panel rtgui_panel_t;
+typedef struct rtgui_win rtgui_win_t;
+typedef struct rtgui_workbench rtgui_workbench_t;
+typedef rt_bool_t (*rtgui_event_handler_ptr)(struct rtgui_widget* widget, struct rtgui_event* event);
+
+struct rtgui_point
+{
+	rt_int16_t x, y;
+};
+typedef struct rtgui_point rtgui_point_t;
+
+struct rtgui_rect
+{
+	rt_int16_t x1, y1, x2, y2;
+};
+typedef struct rtgui_rect rtgui_rect_t;
+#define rtgui_rect_width(r)		((r).x2 - (r).x1)
+#define rtgui_rect_height(r)	((r).y2 - (r).y1)
+
+enum RTGUI_MARGIN_STYLE
+{
+	RTGUI_MARGIN_LEFT	= 0x01,
+	RTGUI_MARGIN_RIGHT	= 0x02,
+	RTGUI_MARGIN_TOP	= 0x04,
+	RTGUI_MARGIN_BOTTOM = 0x08,
+	RTGUI_MARGIN_ALL = RTGUI_MARGIN_LEFT | RTGUI_MARGIN_RIGHT | RTGUI_MARGIN_TOP | RTGUI_MARGIN_BOTTOM
+};
+enum RTGUI_BORDER_STYLE
+{
+	RTGUI_BORDER_NONE = 0,
+	RTGUI_BORDER_SIMPLE,
+	RTGUI_BORDER_RAISE,
+	RTGUI_BORDER_SUNKEN,
+	RTGUI_BORDER_BOX,
+	RTGUI_BORDER_STATIC,
+	RTGUI_BORDER_EXTRA
+};
+#define RTGUI_BORDER_DEFAULT_WIDTH	2
+#define RTGUI_WIDGET_DEFAULT_MARGIN	3
+
+enum RTGUI_ORIENTATION
+{
+    RTGUI_HORIZONTAL		= 0x01,
+	RTGUI_VERTICAL			= 0x02,
+	RTGUI_ORIENTATION_BOTH	= RTGUI_HORIZONTAL | RTGUI_VERTICAL
+};
+
+enum RTGUI_ALIGN
+{
+	RTGUI_ALIGN_NOT					= 0x00,
+	RTGUI_ALIGN_CENTER_HORIZONTAL	= 0x01,
+	RTGUI_ALIGN_LEFT				= RTGUI_ALIGN_NOT,
+	RTGUI_ALIGN_TOP					= RTGUI_ALIGN_NOT,
+	RTGUI_ALIGN_RIGHT				= 0x02,
+	RTGUI_ALIGN_BOTTOM				= 0x04,
+	RTGUI_ALIGN_CENTER_VERTICAL		= 0x08,
+	RTGUI_ALIGN_EXPAND				= 0x10,
+	RTGUI_ALIGN_STRETCH				= 0x20
+};
+
+enum RTGUI_ARRAW
+{
+	RTGUI_ARRAW_UP = 0,
+	RTGUI_ARRAW_DOWN,
+	RTGUI_ARRAW_LEFT,
+	RTGUI_ARRAW_RIGHT
+};
+
+#include <rtgui/rtgui_object.h>
+
+#endif

+ 109 - 0
rtgui/include/rtgui/rtgui_object.h

@@ -0,0 +1,109 @@
+/*
+ * File      : rtgui_object.h
+ * This file is part of RTGUI in 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-04     Bernard      first version
+ */
+#ifndef __RTGUI_OBJECT_H__
+#define __RTGUI_OBJECT_H__
+
+
+#include <rtthread.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* rtgui object type */
+
+/** Casts the function pointer to an rtgui_constructor */
+#define RTGUI_CONSTRUCTOR(constructor)                ((rtgui_constructor_t)(constructor))
+/** Casts the function pointer to an rtgui_constructor */
+#define RTGUI_DESTRUCTOR(destructor)                  ((rtgui_destructor_t)(destructor))
+
+/* pre-definetion */
+struct rtgui_object;
+typedef struct rtgui_object rtgui_object_t;
+typedef void (*rtgui_constructor_t)(rtgui_object_t *object);
+typedef void (*rtgui_destructor_t)(rtgui_object_t *object);
+
+/* rtgui type structure */
+struct rtgui_type
+{
+	/* type name */
+	char* name;
+
+	/* hierarchy and depth */
+	struct rtgui_type **hierarchy;
+	int hierarchy_depth;
+
+	/* constructor and destructor */
+	rtgui_constructor_t constructor;
+	rtgui_destructor_t destructor;
+
+	/* size of type */
+	int size;
+};
+typedef struct rtgui_type rtgui_type_t;
+
+rtgui_type_t	*rtgui_type_create(const char *type_name, rtgui_type_t *parent_type,
+                           int type_size, rtgui_constructor_t constructor,
+                           rtgui_destructor_t destructor);
+void          rtgui_type_destroy(rtgui_type_t *type);
+
+void          rtgui_type_object_construct(rtgui_type_t *type, rtgui_object_t *object);
+void          rtgui_type_destructors_call(rtgui_type_t *type, rtgui_object_t *object);
+rt_bool_t     rtgui_type_inherits_from(rtgui_type_t *type, rtgui_type_t *parent);
+rtgui_type_t  *rtgui_type_parent_type_get(rtgui_type_t *type);
+const char	  *rtgui_type_name_get(rtgui_type_t *type);
+rtgui_type_t  *rtgui_type_get_from_name(const char *name);
+
+#ifdef RTGUI_USING_CAST_CHECK
+	#define RTGUI_OBJECT_CAST(obj, rtgui_type_t, c_type) \
+		((c_type *)rtgui_object_check_cast((rtgui_object_t *)(obj), (rtgui_type_t)))
+#else
+	#define RTGUI_OBJECT_CAST(obj, rtgui_type_t, c_type)     ((c_type *)(obj))
+#endif
+
+#define RTGUI_OBJECT_CHECK_TYPE(_obj, _type) \
+	(rtgui_type_inherits_from(((rtgui_object_t *)(_obj))->type, (_type)))
+
+/** Gets the type of an object */
+#define RTGUI_OBJECT_TYPE       (rtgui_object_type_get())
+/** Casts the object to an rtgui_object_t */
+#define RTGUI_OBJECT(obj)       (RTGUI_OBJECT_CAST((obj), RTGUI_OBJECT_TYPE, rtgui_object_t))
+/** Checks if the object is an rtgui_Object */
+#define RTGUI_IS_OBJECT(obj)    (RTGUI_OBJECT_CHECK_TYPE((obj), RTGUI_OBJECT_TYPE))
+
+/* rtgui base object */
+struct rtgui_object
+{
+	/* object type */
+	rtgui_type_t* type;
+
+	char *name;
+	rt_bool_t is_static;
+};
+rtgui_type_t *rtgui_object_type_get(void);
+
+rtgui_object_t *rtgui_object_create(rtgui_type_t *object_type);
+void         rtgui_object_destroy(rtgui_object_t *object);
+
+void         rtgui_object_name_set(rtgui_object_t *object, const char *name);
+const char   *rtgui_object_name_get(rtgui_object_t *object);
+
+rtgui_object_t *rtgui_object_check_cast(rtgui_object_t *object, rtgui_type_t *type);
+rtgui_type_t   *rtk_object_object_type_get(rtgui_object_t *object);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif

+ 70 - 0
rtgui/include/rtgui/rtgui_server.h

@@ -0,0 +1,70 @@
+/*
+ * File      : rtgui_server.h
+ * This file is part of RTGUI in 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-04     Bernard      first version
+ */
+#ifndef __RTGUI_SERVER_H__
+#define __RTGUI_SERVER_H__
+
+#include <rtgui/list.h>
+
+/* RTGUI server definitions */
+
+/* top window definitions in server */
+enum
+{
+	WINTITLE_NO			= 0x01,
+	WINTITLE_BORDER		= 0x02,
+	WINTITLE_ACTIVATE	= 0x04,
+	WINTITLE_CLOSEBOX	= 0x08,
+	WINTITLE_MOVE		= 0x0C,
+	WINTITLE_CB_PRESSED	= 0x10,
+	WINTITLE_NOFOCUS	= 0x20
+};
+
+#define WINTITLE_HEIGHT			20
+#define WINTITLE_BORDER_SIZE	1
+
+struct rtgui_topwin
+{
+	/* the window flag */
+	rt_uint32_t flag;
+	/* event mask */
+	rt_uint32_t mask;
+
+	struct rtgui_wintitle* title;
+
+	/* the window id */
+	struct rtgui_win* wid;
+
+	/* the thread id */
+	rt_thread_t tid;
+
+	/* the extent information */
+	rtgui_rect_t extent;
+
+	/* the top window list */
+	rtgui_list_t list;
+
+	/* the monitor rect list */
+	rtgui_list_t monitor_list;
+};
+typedef struct rtgui_topwin rtgui_topwin_t;
+
+/* top win manager init */
+void rtgui_topwin_init(void);
+void rtgui_panel_init (void);
+void rtgui_server_init(void);
+
+/* post an event to server */
+void rtgui_server_post_event(struct rtgui_event* event, rt_size_t size);
+
+#endif

+ 81 - 0
rtgui/include/rtgui/rtgui_system.h

@@ -0,0 +1,81 @@
+/*
+ * File      : rtgui_system.h
+ * This file is part of RTGUI in 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-04     Bernard      first version
+ */
+#ifndef __RTGUI_SYSTEM_H__
+#define __RTGUI_SYSTEM_H__
+
+#include <rtthread.h>
+#include <rtgui/rtgui.h>
+
+struct rtgui_dc;
+struct rtgui_event;
+struct rtgui_widget;
+
+struct rtgui_thread
+{
+	/* the thread id */
+	rt_thread_t tid;
+
+	/* the message queue of thread */
+	rt_mq_t mq;
+
+	/* the owner of thread */
+	struct rtgui_widget* widget;
+};
+typedef struct rtgui_thread rtgui_thread_t;
+struct rtgui_timer;
+typedef void (*rtgui_timeout_func)(struct rtgui_timer* timer, void* parameter);
+
+struct rtgui_timer
+{
+	/* context thread id */
+	rt_thread_t tid;
+	/* rt timer */
+	struct rt_timer timer;
+
+	/* timeout function and user data */
+	rtgui_timeout_func timeout;
+	void* user_data;
+};
+typedef struct rtgui_timer rtgui_timer_t;
+
+rtgui_timer_t* rtgui_timer_create(rt_int32_t time, rt_base_t flag, rtgui_timeout_func timeout, void* parameter);
+void rtgui_timer_destory(rtgui_timer_t* timer);
+
+void rtgui_timer_start(rtgui_timer_t* timer);
+void rtgui_timer_stop (rtgui_timer_t* timer);
+
+void rtgui_thread_system_init(void);
+rtgui_thread_t* rtgui_thread_register(rt_thread_t tid, rt_mq_t mq);
+void rtgui_thread_deregister(rt_thread_t tid);
+
+rt_thread_t rtgui_thread_get_server(void);
+
+void rtgui_thread_set_widget(struct rtgui_widget* widget);
+struct rtgui_widget* rtgui_thread_get_widget(void);
+
+rt_err_t rtgui_thread_send(rt_thread_t tid, struct rtgui_event* event, rt_size_t event_size);
+rt_err_t rtgui_thread_send_urgent(rt_thread_t tid, struct rtgui_event* event, rt_size_t event_size);
+rt_err_t rtgui_thread_send_sync(rt_thread_t tid, struct rtgui_event* event, rt_size_t event_size);
+rt_err_t rtgui_thread_recv(struct rtgui_event* event, rt_size_t event_size);
+rt_err_t rtgui_thread_recv_filter(rt_uint32_t type, struct rtgui_event* event, rt_size_t event_size);
+rt_err_t rtgui_thread_ack(struct rtgui_event* event, rt_int32_t status);
+
+/* rtgui system initialization function */
+void rtgui_system_server_init(void);
+void rtgui_system_app_init(void);
+
+void* rtgui_malloc(rt_size_t size);
+void rtgui_free(void* ptr);
+
+#endif

+ 38 - 0
rtgui/include/rtgui/rtgui_theme.h

@@ -0,0 +1,38 @@
+/*
+ * File      : rtgui_theme.h
+ * This file is part of RTGUI in 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-04     Bernard      first version
+ */
+#ifndef __RTGUI_THEME_H__
+#define __RTGUI_THEME_H__
+
+#include <rtgui/rtgui.h>
+
+#include <rtgui/widgets/label.h>
+#include <rtgui/widgets/button.h>
+#include <rtgui/widgets/textbox.h>
+#include <rtgui/widgets/iconbox.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void rtgui_theme_draw_win(struct rtgui_topwin* win);
+void rtgui_theme_draw_button(rtgui_button_t* btn);
+void rtgui_theme_draw_label(rtgui_label_t* label);
+void rtgui_theme_draw_textbox(rtgui_textbox_t* box);
+void rtgui_theme_draw_iconbox(rtgui_iconbox_t* iconbox);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif

+ 197 - 0
rtgui/include/rtgui/widgets/widget.h

@@ -0,0 +1,197 @@
+/*
+ * File      : widget.h
+ * This file is part of RTGUI in 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-04     Bernard      first version
+ */
+#ifndef __RTGUI_WIDGET_H__
+#define __RTGUI_WIDGET_H__
+
+#include <rtgui/rtgui.h>
+#include <rtgui/list.h>
+#include <rtgui/region.h>
+#include <rtgui/event.h>
+#include <rtgui/color.h>
+#include <rtgui/font.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define RTGUI_WIDGET_FLAG_HIDE			0x01
+#define RTGUI_WIDGET_FLAG_DISABLE		0x02
+#define RTGUI_WIDGET_FLAG_FOCUS			0x04
+#define RTGUI_WIDGET_FLAG_TRANSPARENT	0x08
+#define RTGUI_WIDGET_FLAG_FOCUSABLE		0x10
+#define RTGUI_WIDGET_FLAG_DEFAULT		0x00
+
+#define RTGUI_WIDGET_UNHIDE(w)			(w)->flag &= ~RTGUI_WIDGET_FLAG_HIDE
+#define RTGUI_WIDGET_HIDE(w)			(w)->flag |= RTGUI_WIDGET_FLAG_HIDE
+#define RTGUI_WIDGET_IS_HIDE(w)			((w)->flag & RTGUI_WIDGET_FLAG_HIDE)
+
+#define RTGUI_WIDGET_ENABLE(w)			(w)->flag &= ~RTGUI_WIDGET_FLAG_DISABLE
+#define RTGUI_WIDGET_DISABLE(w)			(w)->flag |= RTGUI_WIDGET_FLAG_DISABLE
+#define RTGUI_WIDGET_IS_ENABLE(w)		!(w->flag & RTGUI_WIDGET_FLAG_DISABLE)
+
+#define RTGUI_WIDGET_UNFOCUS(w)			(w)->flag &= ~RTGUI_WIDGET_FLAG_FOCUS
+#define RTGUI_WIDGET_FOCUS(w)			(w)->flag |= RTGUI_WIDGET_FLAG_FOCUS
+#define RTGUI_WIDGET_IS_FOCUS(w)		((w)->flag & RTGUI_WIDGET_FLAG_FOCUS)
+
+#define RTGUI_WIDGET_IS_FOCUSABLE(w) 	((w)->flag & RTGUI_WIDGET_FLAG_FOCUSABLE)
+
+/* get rtgui widget object */
+#define RTGUI_WIDGET_FOREGROUND(w) 		((w)->gc.foreground)
+#define RTGUI_WIDGET_BACKGROUND(w)		((w)->gc.background)
+#define RTGUI_WIDGET_TEXTALIGN(w)		((w)->gc.textalign)
+#define RTGUI_WIDGET_FONT(w)			((w)->gc.font)
+#define RTGUI_WIDGET_FLAG(w)			((w)->flag)
+
+/** Gets the type of a widget */
+#define RTGUI_WIDGET_TYPE       (rtgui_widget_type_get())
+/** Casts the object to a rtgui_widget */
+#define RTGUI_WIDGET(obj)       (RTGUI_OBJECT_CAST((obj), RTGUI_WIDGET_TYPE, rtgui_widget_t))
+/** Check if the object is a rtgui_widget */
+#define RTGUI_IS_WIDGET(obj)    (RTGUI_OBJECT_CHECK_TYPE((obj), RTGUI_WIDGET_TYPE))
+
+struct rtgui_gc
+{
+	/* foreground and background color */
+	rtgui_color_t foreground, background;
+
+	/* text align */
+	rt_base_t textalign;
+
+	/* font */
+	rtgui_font_t* font;
+};
+typedef struct rtgui_gc rtgui_gc_t;
+
+/*
+ * the base widget object
+ */
+struct rtgui_widget
+{
+   /* inherit from rtgui_object */
+	struct rtgui_object object;
+
+	/* the parent and root widget */
+	struct rtgui_widget *parent, *toplevel;
+	/* the widget children and sibling */
+	rtgui_list_t sibling;
+
+	/* widget flag */
+	rt_int32_t flag;
+	/* widget align */
+	rt_int32_t align;
+
+	/* the graphic context of widget */
+	rtgui_gc_t gc;
+
+	/* the widget extent */
+	rtgui_rect_t extent;
+	rt_int16_t mini_width, mini_height;
+	rt_int16_t margin, margin_style;
+
+	/* the rect clip */
+	rtgui_region_t clip;
+	rt_uint32_t clip_sync;
+
+	/* the event handler */
+	rt_bool_t (*event_handler)	(struct rtgui_widget* widget, struct rtgui_event* event);
+
+	/* call back */
+	rt_bool_t (*on_draw)		(struct rtgui_widget* widget, struct rtgui_event* event);
+	rt_bool_t (*on_focus_in)	(struct rtgui_widget* widget, struct rtgui_event* event);
+	rt_bool_t (*on_focus_out)	(struct rtgui_widget* widget, struct rtgui_event* event);
+	rt_bool_t (*on_mouseclick)	(struct rtgui_widget* widget, struct rtgui_event* event);
+	rt_bool_t (*on_key)			(struct rtgui_widget* widget, struct rtgui_event* event);
+	rt_bool_t (*on_size)		(struct rtgui_widget* widget, struct rtgui_event* event);
+	rt_bool_t (*on_command)		(struct rtgui_widget* widget, struct rtgui_event* event);
+};
+typedef struct rtgui_widget rtgui_widget_t;
+
+rtgui_type_t *rtgui_widget_type_get(void);
+rtgui_widget_t *rtgui_widget_create(rtgui_type_t *widget_type);
+void rtgui_widget_destroy(rtgui_widget_t* widget);
+
+/* initial a widget */
+void rtgui_widget_init(rtgui_widget_t* widget, rtgui_type_t type, rtgui_rect_t* rect);
+/* detach a widget */
+void rtgui_widget_detach(rtgui_widget_t* widget);
+
+/* update toplevel widget */
+void rtgui_widget_update_toplevel(rtgui_widget_t* widget);
+
+/* set the event handler of widget */
+void rtgui_widget_set_event_handler(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
+/* widget default event handler */
+rt_bool_t rtgui_widget_event_handler(rtgui_widget_t* widget, rtgui_event_t* event);
+
+/* dispatch event to child widget */
+rt_bool_t rtgui_widget_dispatch_event(rtgui_widget_t* widget, rtgui_event_t* event);
+rt_bool_t rtgui_widget_dispatch_mouse_event(rtgui_widget_t* widget, struct rtgui_event_mouse* event);
+
+/* set and get widget label */
+char* rtgui_widget_get_label(rtgui_widget_t* widget);
+void rtgui_widget_set_label(rtgui_widget_t* widget, const char* label);
+
+/* focus and unfocus */
+void rtgui_widget_focus(rtgui_widget_t * widget);
+void rtgui_widget_unfocus(rtgui_widget_t *widget);
+
+/* event handler for each command */
+void rtgui_widget_set_ondraw(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
+void rtgui_widget_set_onfocus(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
+void rtgui_widget_set_onunfocus(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
+void rtgui_widget_set_onmouseclick(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
+void rtgui_widget_set_onkey(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
+void rtgui_widget_set_onsize(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
+void rtgui_widget_set_oncommand(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
+
+/* get and set rect of widget */
+void rtgui_widget_get_rect(rtgui_widget_t* widget, rtgui_rect_t *rect);
+void rtgui_widget_set_rect(rtgui_widget_t* widget, rtgui_rect_t* rect);
+
+void rtgui_widget_set_miniwidth(rtgui_widget_t* widget, int width);
+void rtgui_widget_set_miniheight(rtgui_widget_t* widget, int height);
+
+/* get the physical position of a logic point on widget */
+void rtgui_widget_point_to_device(rtgui_widget_t * widget, rtgui_point_t * point);
+/* get the physical position of a logic rect on widget */
+void rtgui_widget_rect_to_device(rtgui_widget_t * widget, rtgui_rect_t * rect);
+
+/* get the logic position of a physical point on widget */
+void rtgui_widget_point_to_logic(rtgui_widget_t* widget, rtgui_point_t * point);
+/* get the logic position of a physical rect on widget */
+void rtgui_widget_rect_to_logic(rtgui_widget_t* widget, rtgui_rect_t* rect);
+
+/* move widget and its children to a logic point */
+void rtgui_widget_move_to_logic(rtgui_widget_t* widget, int dx, int dy);
+
+/* update the clip info of widget */
+void rtgui_widget_update_clip(rtgui_widget_t* widget);
+
+/* get the toplevel widget of widget */
+rtgui_widget_t* rtgui_widget_get_toplevel(rtgui_widget_t* widget);
+
+void rtgui_widget_show(rtgui_widget_t* widget);
+void rtgui_widget_hide(rtgui_widget_t* widget);
+void rtgui_widget_update(rtgui_widget_t* widget);
+
+/* get the next sibling of widget */
+rtgui_widget_t* rtgui_widget_get_next_sibling(rtgui_widget_t* widget);
+/* get the prev sibling of widget */
+rtgui_widget_t* rtgui_widget_get_prev_sibling(rtgui_widget_t* widget);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif

+ 91 - 0
rtgui/include/rtgui/widgets/window.h

@@ -0,0 +1,91 @@
+/*
+ * File      : window.h
+ * This file is part of RTGUI in 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-04     Bernard      first version
+ */
+#ifndef __RTGUI_WINDOW_H__
+#define __RTGUI_WINDOW_H__
+
+#include <rtgui/rtgui.h>
+#include <rtgui/list.h>
+#include <rtgui/widgets/widget.h>
+#include <rtgui/widgets/toplevel.h>
+#include <rtgui/widgets/box.h>
+
+/** Gets the type of a win */
+#define RTGUI_WIN_TYPE       (rtgui_win_type_get())
+/** Casts the object to an rtgui_win */
+#define RTGUI_WIN(obj)       (RTGUI_OBJECT_CAST((obj), RTGUI_WIN_TYPE, rtgui_win_t))
+/** Checks if the object is an rtgui_win */
+#define RTGUI_IS_WIN(obj)    (RTGUI_OBJECT_CHECK_TYPE((obj), RTGUI_WIN_TYPE))
+
+#define RTGUI_WIN_STYLE_MODAL		0x00
+#define RTGUI_WIN_STYLE_MODAL_LESS	0x01
+#define RTGUI_WIN_STYLE_NO_TITLE	0x02
+#define RTGUI_WIN_STYLE_NO_BORDER	0x04
+#define RTGUI_WIN_STYLE_SHOW		0x08
+#define RTGUI_WIN_STYLE_CLOSEBOX	0x10
+#define RTGUI_WIN_STYLE_MINIBOX		0x20
+#define RTGUI_WIN_STYLE_ACTIVATE	0x40
+#define RTGUI_WIN_STYLE_NO_FOCUS	0x80
+
+#define RTGUI_WIN_STYLE_DEFAULT		(RTGUI_WIN_STYLE_CLOSEBOX | RTGUI_WIN_STYLE_MINIBOX)
+
+struct rtgui_win_title;
+struct rtgui_win_area;
+
+struct rtgui_win
+{
+	/* inherit from toplevel */
+	struct rtgui_toplevel parent;
+
+	/* top window style */
+	rt_uint32_t style;
+
+	/* window title */
+	char* title;
+
+	/* call back */
+	rt_bool_t (*on_activate)	(struct rtgui_widget* widget, struct rtgui_event* event);
+	rt_bool_t (*on_deactivate)	(struct rtgui_widget* widget, struct rtgui_event* event);
+	rt_bool_t (*on_close)		(struct rtgui_widget* widget, struct rtgui_event* event);
+
+	/* reserved user data */
+	rt_uint32_t user_data;
+};
+
+rtgui_type_t *rtgui_win_type_get(void);
+
+rtgui_win_t* rtgui_win_create(const char* title, rtgui_rect_t *rect, rt_uint32_t flag);
+void rtgui_win_destroy(rtgui_win_t* win);
+
+void rtgui_win_show(rtgui_win_t* win);
+void rtgui_win_hiden(rtgui_win_t* win);
+rt_bool_t rtgui_win_is_activated(struct rtgui_win* win);
+
+void rtgui_win_move(struct rtgui_win* win, int x, int y);
+
+/* reset extent of window */
+void rtgui_win_set_rect(rtgui_win_t* win, rtgui_rect_t* rect);
+void rtgui_win_set_box(rtgui_win_t* win, rtgui_box_t* box);
+
+void rtgui_win_set_onactivate(rtgui_win_t* win, rtgui_event_handler_ptr handler);
+void rtgui_win_set_ondeactivate(rtgui_win_t* win, rtgui_event_handler_ptr handler);
+void rtgui_win_set_onclose(rtgui_win_t* win, rtgui_event_handler_ptr handler);
+
+rt_bool_t rtgui_win_event_handler(rtgui_widget_t* win, struct rtgui_event* event);
+
+void rtgui_win_event_loop(rtgui_win_t* wnd);
+
+void rtgui_win_set_title(rtgui_win_t* win, const char *title);
+char* rtgui_win_get_title(rtgui_win_t* win);
+
+#endif

+ 77 - 0
rtgui/include/rtgui/widgets/workbench.h

@@ -0,0 +1,77 @@
+/*
+ * File      : workbench.h
+ * This file is part of RTGUI in 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-04     Bernard      first version
+ */
+#ifndef __RTGUI_WORKBENCH_H__
+#define __RTGUI_WORKBENCH_H__
+
+#include <rtgui/rtgui.h>
+#include <rtgui/list.h>
+#include <rtgui/region.h>
+#include <rtgui/dc.h>
+
+#include <rtgui/widgets/view.h>
+#include <rtgui/widgets/toplevel.h>
+
+#define RTGUI_WORKBENCH_FLAG_VISIBLE	0x00
+#define RTGUI_WORKBENCH_FLAG_INVISIBLE	0x01
+#define RTGUI_WORKBENCH_FLAG_FULLSCREEN	0x02
+
+#define RTGUI_WORKBENCH_FLAG_CLOSEBLE	0x00
+#define RTGUI_WORKBENCH_FLAG_UNCLOSEBLE	0x10
+
+#define RTGUI_WORKBENCH_FLAG_DEFAULT	RTGUI_WORKBENCH_FLAG_VISIBLE | RTGUI_WORKBENCH_FLAG_CLOSEBLE
+
+/** Gets the type of a workbench */
+#define RTGUI_WORKBENCH_TYPE       (rtgui_workbench_type_get())
+/** Casts the object to an rtgui_workbench */
+#define RTGUI_WORKBENCH(obj)       (RTGUI_OBJECT_CAST((obj), RTGUI_WORKBENCH_TYPE, rtgui_workbench_t))
+/** Checks if the object is an rtgui_workbench */
+#define RTGUI_IS_WORKBENCH(obj)    (RTGUI_OBJECT_CHECK_TYPE((obj), RTGUI_WORKBENCH_TYPE))
+
+struct rtgui_workbench
+{
+	/* inherit from toplevel */
+	struct rtgui_toplevel parent;
+
+	/* panel id */
+	rtgui_panel_t* panel;
+
+	/* workbench flag */
+	rt_uint8_t flag;
+
+	/* workbench title */
+	unsigned char* title;
+};
+
+rtgui_workbench_t *rtgui_workbench_create(const char* panel_name, const unsigned char* title);
+void rtgui_workbench_destroy(rtgui_workbench_t* workbench);
+
+rtgui_type_t* rtgui_workbench_type_get(void);
+
+rt_bool_t rtgui_workbench_event_handler(rtgui_widget_t* widget, rtgui_event_t* event);
+
+void rtgui_workbench_set_flag(rtgui_workbench_t* workbench, rt_uint8_t flag);
+
+void rtgui_workbench_event_loop(rtgui_workbench_t* workbench);
+
+rt_err_t rtgui_workbench_show (rtgui_workbench_t* workbench);
+rt_err_t rtgui_workbench_hide (rtgui_workbench_t* workbench);
+
+void rtgui_workbench_add_view(rtgui_workbench_t* workbench, rtgui_view_t* view);
+void rtgui_workbench_remove_view(rtgui_workbench_t* workbench, rtgui_view_t* view);
+void rtgui_workbench_show_view(rtgui_workbench_t* workbench, rtgui_view_t* view);
+void rtgui_workbench_hide_view(rtgui_workbench_t* workbench, rtgui_view_t* view);
+
+rtgui_view_t *rtgui_workbench_get_current_view(rtgui_workbench_t * workbench);
+
+#endif

+ 67 - 0
rtgui/server/driver.c

@@ -0,0 +1,67 @@
+/*
+ * File      : driver.c
+ * This file is part of RTGUI in 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-04     Bernard      first version
+ */
+#include <rtgui/driver.h>
+
+struct rtgui_list_node _rtgui_graphic_driver_list = {RT_NULL};
+
+void rtgui_graphic_driver_add(struct rtgui_graphic_driver* driver)
+{
+	rtgui_list_insert(&_rtgui_graphic_driver_list, &(driver->list));
+}
+
+void rtgui_graphic_driver_remove(struct rtgui_graphic_driver* driver)
+{
+	rtgui_list_remove(&_rtgui_graphic_driver_list, &(driver->list));
+}
+
+struct rtgui_graphic_driver* rtgui_graphic_driver_find(char* name)
+{
+	struct rtgui_list_node* node;
+	struct rtgui_graphic_driver* driver;
+
+	/* search in list */
+	rtgui_list_foreach(node, &(_rtgui_graphic_driver_list))
+	{
+		driver = rtgui_list_entry(node, struct rtgui_graphic_driver, list);
+
+		/* find it */
+		if (rt_strncmp(driver->name, name, RTGUI_NAME_MAX) == 0)
+		{
+			return driver;
+		}
+	}
+
+	return RT_NULL;
+}
+
+struct rtgui_graphic_driver* rtgui_graphic_driver_get_default()
+{
+	return rtgui_list_entry(_rtgui_graphic_driver_list.next,
+		struct rtgui_graphic_driver, list);
+}
+
+void rtgui_graphic_driver_get_rect(struct rtgui_graphic_driver *driver, rtgui_rect_t *rect)
+{
+	if (rect == RT_NULL || driver == RT_NULL) return;
+
+	rect->x1 = rect->y1 = 0;
+	rect->x2 = driver->width;
+	rect->y2 = driver->height;
+}
+
+void rtgui_graphic_driver_get_default_rect(rtgui_rect_t *rect)
+{
+	/* return default the extent of default driver */
+	rtgui_graphic_driver_get_rect(rtgui_graphic_driver_get_default(), rect);
+}

+ 293 - 0
rtgui/server/panel.c

@@ -0,0 +1,293 @@
+/*
+ * File      : panel.c
+ * This file is part of RTGUI in 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-04     Bernard      first version
+ */
+
+#include "panel.h"
+#include "mouse.h"
+
+#include <rtgui/rtgui_system.h>
+
+/* the global parameter */
+struct rtgui_list_node _rtgui_panel_list;
+
+void rtgui_panel_init()
+{
+	rtgui_list_init(&_rtgui_panel_list);
+}
+
+void rtgui_panel_register(char* name, rtgui_rect_t* extent)
+{
+	register rt_base_t temp;
+	struct rtgui_panel* panel;
+
+	panel = rtgui_panel_find(name);
+	if (panel != RT_NULL )
+	{
+		/* there are already a same named panel exist. */
+		return;
+	}
+
+	panel = rtgui_malloc(sizeof(struct rtgui_panel));
+	if (panel == RT_NULL)
+	{
+		/* can't alloc memory */
+		return;
+	}
+
+	/* copy name */
+	for (temp = 0; temp < RTGUI_NAME_MAX; temp ++)
+	{
+		panel->name[temp] = name[temp];
+	}
+
+	/* copy extent */
+	panel->extent = *extent;
+
+	panel->wm_thread = RT_NULL;
+
+	/* init list */
+	rtgui_list_init(&(panel->sibling));
+	rtgui_list_init(&(panel->thread_list));
+
+	/* add panel to panel list */
+	rtgui_list_insert(&_rtgui_panel_list, &(panel->sibling));
+}
+
+void rtgui_panel_deregister(char* name)
+{
+	struct rtgui_panel* panel;
+
+	panel = rtgui_panel_find(name);
+	if (panel != RT_NULL)
+	{
+		rtgui_list_remove(&_rtgui_panel_list, &(panel->sibling));
+
+		/* free pane node */
+		rtgui_free(panel);
+	}
+}
+
+struct rtgui_panel* rtgui_panel_find(char* name)
+{
+	struct rtgui_list_node* node;
+	struct rtgui_panel* panel;
+
+	rtgui_list_foreach(node, &_rtgui_panel_list)
+	{
+		panel = rtgui_list_entry(node, struct rtgui_panel, sibling);
+
+		if (rt_strncmp(panel->name, name, RTGUI_NAME_MAX) == 0)
+		{
+			return panel;
+		}
+	}
+
+	return RT_NULL;
+}
+
+struct rtgui_panel* rtgui_panel_thread_add(char* name, rt_thread_t tid)
+{
+	struct rtgui_panel* panel;
+
+	panel = rtgui_panel_find(name);
+	if (panel != RT_NULL )
+	{
+		struct rtgui_panel_thread* thread;
+
+		/* allocate panel thread node */
+		thread = rtgui_malloc(sizeof(struct rtgui_panel_thread));
+		if (thread == RT_NULL)
+		{
+			return RT_NULL;
+		}
+
+		/* construct panel thread node */
+		thread->tid = tid;
+
+		/* init list */
+		rtgui_list_init(&(thread->list));
+		rtgui_list_init(&(thread->monitor_list));
+
+		/* append thread to the list */
+		rtgui_list_append(&(panel->thread_list), &(thread->list));
+	}
+
+	return panel;
+}
+
+void rtgui_panel_thread_remove(rtgui_panel_t* panel, rt_thread_t tid)
+{
+	if (panel != RT_NULL )
+	{
+		struct rtgui_list_node* node;
+		struct rtgui_panel_thread* thread;
+
+		rtgui_list_foreach(node, &(panel->thread_list))
+		{
+			thread = rtgui_list_entry(node, struct rtgui_panel_thread, list);
+			if (thread->tid == tid)
+			{
+				/* remove node from list */
+				rtgui_list_remove(&(panel->thread_list), &(thread->list));
+
+				/* free the panel thread node */
+				rtgui_free(thread);
+				return;
+			}
+		}
+	}
+}
+
+rt_thread_t rtgui_panel_get_active_thread(rtgui_panel_t* panel)
+{
+	if (panel != RT_NULL)
+	{
+		if (panel->thread_list.next != RT_NULL)
+		{
+			struct rtgui_panel_thread* thread;
+			thread = rtgui_list_entry(panel->thread_list.next, struct rtgui_panel_thread, list);
+
+			return thread->tid;
+		}
+	}
+
+	return RT_NULL;
+}
+
+void rtgui_panel_set_active_thread(rtgui_panel_t* panel, rt_thread_t tid)
+{
+	/* get old active thread */
+	rt_thread_t prev_actived = rtgui_panel_get_active_thread(panel);
+	if (prev_actived != tid)
+	{
+		/* de-active old active workbench */
+		struct rtgui_event_panel_hide ehide;
+		RTGUI_EVENT_PANEL_HIDE_INIT(&ehide);
+
+		ehide.panel = panel;
+		ehide.workbench = RT_NULL;
+		rtgui_thread_send_urgent(prev_actived, &(ehide.parent), sizeof (ehide));
+	}
+
+	if (panel != RT_NULL )
+	{
+		struct rtgui_list_node* node;
+		struct rtgui_panel_thread* thread;
+
+		rtgui_list_foreach(node, &(panel->thread_list))
+		{
+			thread = rtgui_list_entry(node, struct rtgui_panel_thread, list);
+			if (thread->tid == tid)
+			{
+				/* remove node from list */
+				rtgui_list_remove(&(panel->thread_list), &(thread->list));
+
+				/* insert node to the header */
+				rtgui_list_insert(&(panel->thread_list), &(thread->list));
+				return;
+			}
+		}
+	}
+}
+
+/* deactivate current activated thread -- move it to the end of list */
+void rtgui_panel_deactive_thread(rtgui_panel_t* panel)
+{
+	RT_ASSERT(panel == RT_NULL);
+
+	if (panel->thread_list.next != RT_NULL)
+	{
+		struct rtgui_panel_thread* thread;
+		thread = rtgui_list_entry(panel->thread_list.next, struct rtgui_panel_thread, list);
+
+		/* remove it */
+		panel->thread_list.next = thread->list.next;
+
+		/* append to the tail of thread list */
+		rtgui_list_append(&(panel->thread_list), &(thread->list));
+	}
+}
+
+/**
+ * get the panel which contains a point(x, y)
+ */
+rtgui_panel_t* rtgui_panel_get_contain(int x, int y)
+{
+	struct rtgui_list_node* node;
+	struct rtgui_panel* panel;
+
+	rtgui_list_foreach(node, &(_rtgui_panel_list))
+	{
+		panel = rtgui_list_entry(node, struct rtgui_panel, sibling);
+		if (rtgui_rect_contains_point(&(panel->extent), x, y) == RT_EOK)
+		{
+			return panel;
+		}
+	}
+
+	return RT_NULL;
+}
+
+/**
+ * append a rect to panel mouse monitor rect list
+ */
+void rtgui_panel_append_monitor_rect(rtgui_panel_t* panel, rt_thread_t tid, rtgui_rect_t* rect)
+{
+	if (panel != RT_NULL )
+	{
+		struct rtgui_list_node* node;
+		struct rtgui_panel_thread* thread;
+
+		rtgui_list_foreach(node, &(panel->thread_list))
+		{
+			thread = rtgui_list_entry(node, struct rtgui_panel_thread, list);
+			if (thread->tid == tid)
+			{
+				/* add the monitor rect to list */
+				rtgui_mouse_monitor_append(&(thread->monitor_list), rect);
+				return;
+			}
+		}
+	}
+}
+
+/**
+ * remove a rect from panel mouse monitor rect list
+ */
+void rtgui_panel_remove_monitor_rect(rtgui_panel_t* panel, rt_thread_t tid, rtgui_rect_t* rect)
+{
+	if (panel != RT_NULL )
+	{
+		struct rtgui_list_node* node;
+		struct rtgui_panel_thread* thread;
+
+		rtgui_list_foreach(node, &(panel->thread_list))
+		{
+			thread = rtgui_list_entry(node, struct rtgui_panel_thread, list);
+			if (thread->tid == tid)
+			{
+				/* remove the monitor rect from list */
+				rtgui_mouse_monitor_remove(&(thread->monitor_list), rect);
+				return;
+			}
+		}
+	}
+}
+
+void rtgui_panel_set_wm(rtgui_panel_t* panel, rt_thread_t wm)
+{
+	RT_ASSERT(wm != RT_NULL);
+	RT_ASSERT(panel != RT_NULL);
+
+	panel->wm_thread = wm;
+}

+ 72 - 0
rtgui/server/panel.h

@@ -0,0 +1,72 @@
+/*
+ * File      : panel.h
+ * This file is part of RTGUI in 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-04     Bernard      first version
+ */
+
+#ifndef __RT_PANEL_H__
+#define __RT_PANEL_H__
+
+#include <rtgui/rtgui.h>
+#include <rtgui/list.h>
+#include <rtgui/region.h>
+
+struct rtgui_panel_thread
+{
+	/* thread id */
+	rt_thread_t tid;
+
+	/* the list of thread */
+	rtgui_list_t list;
+
+	/* monitor rect list */
+	rtgui_list_t monitor_list;
+};
+typedef struct rtgui_panel_thread rtgui_panel_thread_list_t;
+
+struct rtgui_panel
+{
+	char name[RTGUI_NAME_MAX];
+
+	/* the extent of panel */
+	rtgui_rect_t extent;
+
+	/* the list of panel */
+	rtgui_list_t sibling;
+
+	/* the thread list in this panel */
+	rtgui_list_t thread_list;
+
+	/* the workbench manager thread */
+	rt_thread_t wm_thread;
+};
+
+/* register or deregister panel in server */
+void rtgui_panel_register(char* name, rtgui_rect_t* extent);
+void rtgui_panel_deregister(char* name);
+
+/* find panel by name */
+struct rtgui_panel* rtgui_panel_find(char* name);
+
+/* add or remove application thread from specified panel */
+rtgui_panel_t* rtgui_panel_thread_add(char* name, rt_thread_t tid);
+void rtgui_panel_thread_remove(rtgui_panel_t* panel, rt_thread_t tid);
+
+rt_thread_t rtgui_panel_get_active_thread(rtgui_panel_t* panel);
+void rtgui_panel_set_active_thread(rtgui_panel_t* panel, rt_thread_t tid);
+
+rtgui_panel_t* rtgui_panel_get_contain(int x, int y);
+void rtgui_panel_set_wm(rtgui_panel_t* panel, rt_thread_t wm);
+
+void rtgui_panel_append_monitor_rect(rtgui_panel_t* panel, rt_thread_t tid, rtgui_rect_t* rect);
+void rtgui_panel_remove_monitor_rect(rtgui_panel_t* panel, rt_thread_t tid, rtgui_rect_t* rect);
+
+#endif

+ 633 - 0
rtgui/server/server.c

@@ -0,0 +1,633 @@
+/*
+ * File      : server.c
+ * This file is part of RTGUI in 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-04     Bernard      first version
+ */
+
+#include <rtgui/rtgui.h>
+#include <rtgui/event.h>
+#include <rtgui/rtgui_system.h>
+#include <rtgui/driver.h>
+
+#include "mouse.h"
+#include "panel.h"
+#include "topwin.h"
+
+#define RTGUI_SVR_THREAD_PRIORITY		96
+#define RTGUI_SVR_THREAD_TIMESLICE		20
+
+#define RTGUI_APP_THREAD_PRIORITY		220
+#define RTGUI_APP_THREAD_TIMESLICE		20
+
+static char rtgui_server_stack[2048];
+static struct rt_thread rtgui_server_thread;
+
+static struct rt_messagequeue rtgui_server_mq;
+static char rtgui_server_msg_pool[2048];
+
+extern struct rtgui_topwin* rtgui_server_focus_topwin;
+static struct rtgui_panel* rtgui_server_focus_panel = RT_NULL;
+
+void rtgui_server_create_application(struct rtgui_event_panel_attach* event)
+{
+	struct rtgui_panel* panel = rtgui_panel_find(event->panel_name);
+
+	if (panel != RT_NULL)
+	{
+		struct rtgui_event_panel_info ep;
+		RTGUI_EVENT_PANEL_INFO_INIT(&ep);
+
+		if (panel->wm_thread != RT_NULL)
+		{
+			/* notify to workbench */
+			rtgui_thread_send(panel->wm_thread, &(event->parent), sizeof(struct rtgui_event_panel_attach));
+		}
+
+		/* send the responses - ok */
+		rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
+
+		/* send the panel info */
+		ep.panel = panel;
+		ep.extent = panel->extent;
+		rtgui_thread_send(event->parent.sender, (struct rtgui_event*)&ep, sizeof(ep));
+
+		rtgui_panel_thread_add(event->panel_name, event->parent.sender);
+	}
+	else
+	{
+		/* send the responses - failure */
+		rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_NRC);
+	}
+}
+
+void rtgui_server_destroy_application(struct rtgui_event_panel_detach* event)
+{
+	struct rtgui_panel* panel = event->panel;
+
+	if (panel != RT_NULL)
+	{
+		if (panel->wm_thread != RT_NULL)
+		{
+			/* notify to workbench */
+			rtgui_thread_send(panel->wm_thread, &(event->parent), sizeof(struct rtgui_event_panel_detach));
+		}
+
+		/* send the responses */
+		rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
+
+		rtgui_panel_thread_remove(panel, event->parent.sender);
+
+		{
+			/* get next thread and active it */
+			rt_thread_t tid = rtgui_panel_get_active_thread(panel);
+
+			/* let this thread repaint */
+			struct rtgui_event_paint epaint;
+			RTGUI_EVENT_PAINT_INIT(&epaint);
+			epaint.wid = RT_NULL;
+			rtgui_thread_send(tid, (struct rtgui_event*)&epaint, sizeof(epaint));
+		}
+	}
+	else
+	{
+		/* send the responses - failure */
+		rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_NRC);
+	}
+}
+
+void rtgui_server_thread_panel_show(struct rtgui_event_panel_show* event)
+{
+	struct rtgui_panel* panel = event->panel;
+
+	if (panel != RT_NULL)
+	{
+		struct rtgui_event_paint epaint;
+
+		/* send the responses */
+		rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
+
+		if (panel->wm_thread != RT_NULL)
+		{
+			/* notify to workbench */
+			rtgui_thread_send(panel->wm_thread, &(event->parent), sizeof(struct rtgui_event_panel_show));
+		}
+
+		rtgui_panel_set_active_thread(panel, event->parent.sender);
+
+		/* send all topwin clip info */
+		rtgui_topwin_update_clip_to_panel(panel);
+
+		/* send paint event */
+		RTGUI_EVENT_PAINT_INIT(&epaint);
+		epaint.wid = RT_NULL;
+		rtgui_thread_send(event->parent.sender, (struct rtgui_event*)&epaint,
+			sizeof(epaint));
+	}
+	else
+	{
+		/* send failed */
+		rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_ERROR);
+	}
+}
+
+void rtgui_server_thread_panel_hide(struct rtgui_event_panel_hide* event)
+{
+	struct rtgui_panel* panel = event->panel;
+
+	if (panel != RT_NULL)
+	{
+		rt_thread_t tid;
+		struct rtgui_event_paint epaint;
+
+		/* send the responses */
+		rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
+
+		if (panel->thread_list.next != RT_NULL)
+		{
+			struct rtgui_panel_thread* thread;
+			thread = rtgui_list_entry(panel->thread_list.next, struct rtgui_panel_thread, list);
+
+			/* remove it */
+			panel->thread_list.next = thread->list.next;
+
+			/* append to the tail of thread list */
+			rtgui_list_append(&(panel->thread_list), &(thread->list));
+		}
+
+		/* get new active thread */
+		tid = rtgui_panel_get_active_thread(panel);
+		/* send all topwin clip info */
+		rtgui_topwin_update_clip_to_panel(panel);
+
+		/* send paint event */
+		RTGUI_EVENT_PAINT_INIT(&epaint);
+		epaint.wid = RT_NULL;
+		rtgui_thread_send(tid, (struct rtgui_event*)&epaint, sizeof(epaint));
+	}
+	else
+	{
+		/* send failed */
+		rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_ERROR);
+	}
+}
+
+void rtgui_server_handle_set_wm(struct rtgui_event_set_wm *event)
+{
+	struct rtgui_panel* panel = rtgui_panel_find(event->panel_name);
+
+	if (panel != RT_NULL)
+	{
+		rtgui_panel_set_wm(panel, event->parent.sender);
+	}
+}
+
+void rtgui_server_handle_update(struct rtgui_event_update_end* event)
+{
+	struct rtgui_graphic_driver* driver = rtgui_graphic_driver_get_default();
+	if (driver != RT_NULL)
+	{
+		driver->screen_update(&(event->rect));
+	}
+}
+
+void rtgui_server_handle_monitor_add(struct rtgui_event_monitor* event)
+{
+	if (event->panel != RT_NULL)
+	{
+		/* append monitor rect to panel list */
+		rtgui_panel_append_monitor_rect(event->panel, event->parent.sender, &(event->rect));
+	}
+	else
+	{
+		/* add monitor rect to top window list */
+		rtgui_topwin_append_monitor_rect(event->wid, &(event->rect));
+	}
+}
+
+void rtgui_server_handle_monitor_remove(struct rtgui_event_monitor* event)
+{
+	if (event->panel != RT_NULL)
+	{
+		/* add monitor rect to panel list */
+		rtgui_panel_remove_monitor_rect(event->panel, event->parent.sender, &(event->rect));
+	}
+	else
+	{
+		/* add monitor rect to top window list */
+		rtgui_topwin_remove_monitor_rect(event->wid, &(event->rect));
+	}
+}
+
+void rtgui_server_handle_mouse_btn(struct rtgui_event_mouse* event)
+{
+	struct rtgui_topwin* wnd;
+	struct rtgui_panel* panel;
+
+	/* re-init to server thread */
+	RTGUI_EVENT_MOUSE_BUTTON_INIT(event);
+
+	if (rtgui_winrect_is_moved() &&
+		event->button & (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_UP))
+	{
+		struct rtgui_topwin* topwin;
+		rtgui_rect_t rect;
+
+		if (rtgui_winrect_moved_done(&rect, &topwin) == RT_TRUE)
+		{
+			struct rtgui_event_win_move ewin;
+
+			/* move window */
+			RTGUI_EVENT_WIN_MOVE_INIT(&ewin);
+			ewin.wid = topwin->wid;
+			if (topwin->title != RT_NULL)
+			{
+				if (topwin->flag & WINTITLE_BORDER)
+				{
+					ewin.x = rect.x1 + WINTITLE_BORDER_SIZE;
+					ewin.y = rect.y1 + WINTITLE_BORDER_SIZE;
+				}
+				if (!(topwin->flag & WINTITLE_NO)) ewin.y += WINTITLE_HEIGHT;
+			}
+			else
+			{
+				ewin.x = rect.x1;
+				ewin.y = rect.y1;
+			}
+
+			/* send to client thread */
+			rtgui_thread_send(topwin->tid, &(ewin.parent), sizeof(ewin));
+
+			return;
+		}
+	}
+
+	/* get the wnd which contains the mouse */
+	wnd = rtgui_topwin_get_wnd(event->x, event->y);
+	if (wnd != RT_NULL)
+	{
+		event->wid = wnd->wid;
+
+		if (rtgui_server_focus_topwin != wnd)
+		{
+			/* raise this window */
+			rtgui_topwin_raise(wnd->wid, wnd->tid);
+			rtgui_server_focus_panel = RT_NULL;
+		}
+
+		if (wnd->title != RT_NULL &&
+			rtgui_rect_contains_point(&(RTGUI_WIDGET(wnd->title)->extent), event->x, event->y) == RT_EOK)
+		{
+			rtgui_topwin_title_onmouse(wnd, event);
+		}
+		else
+		{
+			/* send mouse event to thread */
+			rtgui_thread_send(wnd->tid, (struct rtgui_event*)event, sizeof(struct rtgui_event_mouse));
+		}
+		return ;
+	}
+
+	/* get the panel which contains the mouse */
+	panel = rtgui_panel_get_contain(event->x, event->y);
+	if (panel != RT_NULL)
+	{
+		/* deactivate old window */
+		if (rtgui_server_focus_topwin != RT_NULL)
+		{
+			rtgui_topwin_deactivate_win(rtgui_server_focus_topwin);
+		}
+
+		rtgui_server_focus_panel = panel;
+		rtgui_server_focus_topwin = RT_NULL;
+
+		/* set destination window to null */
+		event->wid = RT_NULL;
+
+		/* send mouse event to thread */
+		rtgui_thread_send(rtgui_panel_get_active_thread(panel),
+			(struct rtgui_event*)event,
+			sizeof(struct rtgui_event_mouse));
+	}
+}
+
+static struct rtgui_panel* last_monitor_panel = RT_NULL;
+static struct rtgui_topwin* last_monitor_topwin = RT_NULL;
+
+void rtgui_server_handle_mouse_motion(struct rtgui_event_mouse* event)
+{
+	/* the topwin contains current mouse */
+	struct rtgui_topwin* win 	= RT_NULL;
+	struct rtgui_panel* panel 	= RT_NULL;
+
+	/* re-init mouse event */
+	RTGUI_EVENT_MOUSE_MOTION_INIT(event);
+
+	/* find the panel or topwin which monitor the mouse motion */
+	win = rtgui_topwin_get_wnd(event->x, event->y);
+	if (win == RT_NULL)
+	{
+		/* try to find monitor on the panel */
+		panel = rtgui_panel_get_contain(event->x, event->y);
+
+		if (panel != RT_NULL)
+		{
+			struct rtgui_panel_thread* thread;
+
+			/* get active panel thread */
+			if (panel->thread_list.next != RT_NULL)
+			{
+				thread = rtgui_list_entry(panel->thread_list.next, struct rtgui_panel_thread, list);
+				if (!rtgui_mouse_monitor_contains_point(&(thread->monitor_list),
+					event->x, event->y) == RT_TRUE)
+				{
+					/* no monitor in this panel */
+					panel = RT_NULL;
+				}
+			}
+		}
+	}
+	else if (win->monitor_list.next != RT_NULL)
+	{
+		/* check whether the monitor exist */
+		if (rtgui_mouse_monitor_contains_point(&(win->monitor_list), event->x, event->y) != RT_TRUE)
+		{
+			win = RT_NULL;
+
+			/* try to find monitor on the panel */
+			panel = rtgui_panel_get_contain(event->x, event->y);
+			if (panel != RT_NULL)
+			{
+				struct rtgui_panel_thread* thread;
+
+				/* get active panel thread */
+				if (panel->thread_list.next != RT_NULL)
+				{
+					thread = rtgui_list_entry(panel->thread_list.next, struct rtgui_panel_thread, list);
+					if (!rtgui_mouse_monitor_contains_point(&(thread->monitor_list),
+						event->x, event->y) == RT_TRUE)
+					{
+						/* no monitor in this panel */
+						panel = RT_NULL;
+					}
+				}
+			}
+		}
+	}
+	else
+	{
+		win = RT_NULL;
+	}
+
+	/* check old panel or window */
+	if (last_monitor_panel != RT_NULL)
+	{
+		rt_thread_t tid = rtgui_panel_get_active_thread(last_monitor_panel);
+		event->wid = RT_NULL;
+
+		/* send mouse motion event */
+		rtgui_thread_send(tid, &(event->parent), sizeof(struct rtgui_event_mouse));
+	}
+	else if (last_monitor_topwin != RT_NULL)
+	{
+		event->wid = last_monitor_topwin->wid;
+
+		/* send mouse motion event */
+		rtgui_thread_send(last_monitor_topwin->tid, &(event->parent), sizeof(struct rtgui_event_mouse));
+	}
+
+	if (last_monitor_panel != panel)
+	{
+		last_monitor_panel = panel;
+		if (last_monitor_panel != RT_NULL)
+		{
+			rt_thread_t tid = rtgui_panel_get_active_thread(last_monitor_panel);
+			event->wid = RT_NULL;
+
+			/* send mouse motion event */
+			rtgui_thread_send(tid, &(event->parent), sizeof(struct rtgui_event_mouse));
+		}
+	}
+
+	if (last_monitor_topwin != win)
+	{
+		last_monitor_topwin = win;
+		if (last_monitor_topwin != RT_NULL)
+		{
+			event->wid = last_monitor_topwin->wid;
+
+			/* send mouse motion event */
+			rtgui_thread_send(last_monitor_topwin->tid, &(event->parent), sizeof(struct rtgui_event_mouse));
+		}
+	}
+
+	/* move mouse to (x, y) */
+	rtgui_mouse_moveto(event->x, event->y);
+}
+
+void rtgui_server_handle_kbd(struct rtgui_event_kbd* event)
+{
+	struct rtgui_topwin* wnd;
+	struct rtgui_panel* panel;
+
+	/* re-init to server thread */
+	RTGUI_EVENT_KBD_INIT(event);
+
+	/* todo: handle input method and global shortcut */
+
+	/* send to focus window or focus panel */
+	wnd = rtgui_server_focus_topwin;
+	if (wnd != RT_NULL && wnd->flag & WINTITLE_ACTIVATE)
+	{
+		/* send to focus window */
+		event->wid = wnd->wid;
+
+		/* send keyboard event to thread */
+		rtgui_thread_send(wnd->tid, (struct rtgui_event*)event, sizeof(struct rtgui_event_kbd));
+
+		return;
+	}
+
+	panel = rtgui_server_focus_panel;
+	if (panel != RT_NULL)
+	{
+		/* send to focus panel */
+		event->wid = RT_NULL;
+
+		/* send keyboard event to thread */
+		rtgui_thread_send(rtgui_panel_get_active_thread(panel),
+			(struct rtgui_event*)event, sizeof(struct rtgui_event_kbd));
+	}
+}
+
+#ifdef __WIN32__
+#include <windows.h>
+#endif
+
+/**
+ * rtgui server thread's entry
+ */
+static void rtgui_server_entry(void* parameter)
+{
+#ifdef __WIN32__
+	/* set the server thread to highest */
+	HANDLE hCurrentThread = GetCurrentThread();
+	SetThreadPriority(hCurrentThread, THREAD_PRIORITY_HIGHEST);
+#endif
+
+	/* init rtgui server msgq */
+	rt_mq_init(&rtgui_server_mq,
+		"rtgui",
+		&rtgui_server_msg_pool[0],
+		256,
+		sizeof(rtgui_server_msg_pool),
+		RT_IPC_FLAG_FIFO);
+
+	/* register rtgui server thread */
+	rtgui_thread_register(&rtgui_server_thread, &rtgui_server_mq);
+
+	/* init mouse and show */
+	rtgui_mouse_init();
+#ifdef RTGUI_USING_MOUSE_CURSOR
+	rtgui_mouse_show_cursor();
+#endif
+
+	while (1)
+	{
+		/* the buffer uses to receive event */
+		char event_buf[256];
+		struct rtgui_event* event = (struct rtgui_event*)&(event_buf[0]);
+
+		if (rtgui_thread_recv(event, sizeof(event_buf)) == RT_EOK)
+		{
+			/* dispatch event */
+			switch (event->type)
+			{
+			/* panel event */
+			case RTGUI_EVENT_PANEL_ATTACH:
+				/* register an application in panel */
+				rtgui_server_create_application((struct rtgui_event_panel_attach*)event);
+				break;
+
+			case RTGUI_EVENT_PANEL_DETACH:
+				/* unregister an application */
+				rtgui_server_destroy_application((struct rtgui_event_panel_detach*)event);
+				break;
+
+			case RTGUI_EVENT_PANEL_SHOW:
+				/* handle raise an application */
+				rtgui_server_thread_panel_show((struct rtgui_event_panel_show*)event);
+				break;
+
+			case RTGUI_EVENT_PANEL_HIDE:
+				/* handle hide an application */
+				rtgui_server_thread_panel_hide((struct rtgui_event_panel_hide*)event);
+				break;
+
+			case RTGUI_EVENT_SET_WM:
+				/* handle set workbench manager event */
+				rtgui_server_handle_set_wm((struct rtgui_event_set_wm*)event);
+				break;
+
+			/* window event */
+			case RTGUI_EVENT_WIN_CREATE:
+				rtgui_thread_ack(event, RTGUI_STATUS_OK);
+				rtgui_topwin_add((struct rtgui_event_win_create*)event);
+				break;
+
+			case RTGUI_EVENT_WIN_DESTROY:
+				if (rtgui_topwin_remove(((struct rtgui_event_win*)event)->wid) == RT_EOK)
+					rtgui_thread_ack(event, RTGUI_STATUS_OK);
+				else
+					rtgui_thread_ack(event, RTGUI_STATUS_ERROR);
+				break;
+
+			case RTGUI_EVENT_WIN_SHOW:
+				rtgui_topwin_show((struct rtgui_event_win*)event);
+				break;
+
+			case RTGUI_EVENT_WIN_HIDE:
+				rtgui_topwin_hide((struct rtgui_event_win*)event);
+				break;
+
+			case RTGUI_EVENT_WIN_MOVE:
+				rtgui_topwin_move((struct rtgui_event_win_move*)event);
+				break;
+
+			case RTGUI_EVENT_WIN_RESIZE:
+				rtgui_topwin_resize(((struct rtgui_event_win_resize*)event)->wid,
+					&(((struct rtgui_event_win_resize*)event)->rect));
+				break;
+
+			/* other event */
+			case RTGUI_EVENT_UPDATE_BEGIN:
+#ifdef RTGUI_USING_MOUSE_CURSOR
+				/* hide cursor */
+				rtgui_mouse_hide_cursor();
+#endif
+				break;
+
+			case RTGUI_EVENT_UPDATE_END:
+				/* handle screen update */
+				rtgui_server_handle_update((struct rtgui_event_update_end*)event);
+#ifdef RTGUI_USING_MOUSE_CURSOR
+				/* show cursor */
+				rtgui_mouse_show_cursor();
+#endif
+				break;
+
+			case RTGUI_EVENT_MONITOR_ADD:
+				/* handle mouse monitor */
+				rtgui_server_handle_monitor_add((struct rtgui_event_monitor*)event);
+				break;
+
+			/* mouse and keyboard event */
+			case RTGUI_EVENT_MOUSE_MOTION:
+				/* handle mouse motion event */
+				rtgui_server_handle_mouse_motion((struct rtgui_event_mouse*)event);
+				break;
+
+			case RTGUI_EVENT_MOUSE_BUTTON:
+				/* handle mouse button */
+				rtgui_server_handle_mouse_btn((struct rtgui_event_mouse*)event);
+				break;
+
+			case RTGUI_EVENT_KBD:
+				/* handle keyboard event */
+				rtgui_server_handle_kbd((struct rtgui_event_kbd*)event);
+				break;
+
+			case RTGUI_EVENT_COMMAND:
+				break;
+			}
+		}
+	}
+
+	/* unregister in rtgui thread */
+	// rtgui_thread_deregister(rt_thread_self());
+}
+
+void rtgui_server_post_event(struct rtgui_event* event, rt_size_t size)
+{
+	rt_mq_send(&rtgui_server_mq, event, size);
+}
+
+void rtgui_server_init()
+{
+	rt_thread_init(&rtgui_server_thread,
+		"rtgui",
+		rtgui_server_entry, RT_NULL,
+		&rtgui_server_stack[0], sizeof(rtgui_server_stack),
+		RTGUI_SVR_THREAD_PRIORITY,
+		RTGUI_SVR_THREAD_TIMESLICE);
+
+	/* start rtgui server thread */
+	rt_thread_startup(&rtgui_server_thread);
+}

+ 497 - 0
rtgui/widgets/widget.c

@@ -0,0 +1,497 @@
+/*
+ * File      : widget.c
+ * This file is part of RTGUI in 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-04     Bernard      first version
+ */
+#include <rtgui/widgets/widget.h>
+#include <rtgui/widgets/window.h>
+#include <rtgui/widgets/view.h>
+
+static void _rtgui_widget_constructor(rtgui_widget_t *widget)
+{
+   if (!widget) return;
+
+	/* set default flag */
+	widget->flag = RTGUI_WIDGET_FLAG_DEFAULT;
+
+	/* init list */
+	rtgui_list_init(&(widget->sibling));
+
+	/* init gc */
+	widget->gc.foreground = default_foreground;
+	widget->gc.background = default_background;
+	widget->gc.font = rtgui_font_default();
+	widget->gc.textalign = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_TOP;
+	widget->align = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_TOP;
+
+	/* set parent and toplevel root */
+	widget->parent			= RT_NULL;
+	widget->toplevel		= RT_NULL;
+
+	/* some common event handler */
+	widget->on_draw 		= RT_NULL;
+	widget->on_focus_in		= RT_NULL;
+	widget->on_focus_out	= RT_NULL;
+	widget->on_mouseclick 	= RT_NULL;
+	widget->on_key 			= RT_NULL;
+	widget->on_size 		= RT_NULL;
+	widget->on_command 		= RT_NULL;
+
+	/* set default event handler */
+	widget->event_handler = rtgui_widget_event_handler;
+
+	/* does not set widget extent and only set clip_sync to zero */
+	rtgui_region_init(&(widget->clip));
+	widget->clip_sync = 0;
+}
+
+/* Destroys the widget */
+static void _rtgui_widget_destructor(rtgui_widget_t *widget)
+{
+	if (widget == RT_NULL) return;
+
+	if (widget->parent != RT_NULL)
+	{
+		/* remove widget from parent's children list */
+		rtgui_list_remove(&(RTGUI_CONTAINER(widget->parent)->children), &(widget->sibling));
+
+		widget->parent = RT_NULL;
+	}
+
+	/* fini clip region */
+	rtgui_region_fini(&(widget->clip));
+	widget->clip_sync = 0;
+}
+
+rtgui_type_t *rtgui_widget_type_get(void)
+{
+	static rtgui_type_t *widget_type = RT_NULL;
+
+	if (!widget_type)
+	{
+		widget_type = rtgui_type_create("rtgui_widget", RTGUI_OBJECT_TYPE,
+			sizeof(rtgui_widget_t), RTGUI_CONSTRUCTOR(_rtgui_widget_constructor),
+			RTGUI_DESTRUCTOR(_rtgui_widget_destructor));
+	}
+
+	return widget_type;
+}
+
+rtgui_widget_t *rtgui_widget_create(rtgui_type_t *widget_type)
+{
+	struct rtgui_widget* widget;
+
+	widget = RTGUI_WIDGET(rtgui_object_create(widget_type));
+
+	return widget;
+}
+
+void rtgui_widget_destroy(rtgui_widget_t* widget)
+{
+	rtgui_object_destroy(RTGUI_OBJECT(widget));
+}
+
+void rtgui_widget_set_rect(rtgui_widget_t* widget, rtgui_rect_t* rect)
+{
+	if (widget == RT_NULL || rect == RT_NULL) return;
+
+	widget->extent = *rect;
+
+	/* reset mini width and height */
+	widget->mini_width  = rtgui_rect_width(widget->extent);
+	widget->mini_height = rtgui_rect_height(widget->extent);
+
+	/* it's not empty, fini it */
+	if (rtgui_region_not_empty(&(widget->clip)))
+	{
+		rtgui_region_fini(&(widget->clip));
+	}
+
+	/* reset clip info */
+	rtgui_region_init_with_extents(&(widget->clip), rect);
+}
+
+void rtgui_widget_set_miniwidth(rtgui_widget_t* widget, int width)
+{
+	RT_ASSERT(widget != RT_NULL);
+
+	widget->mini_width = width;
+}
+
+void rtgui_widget_set_miniheight(rtgui_widget_t* widget, int height)
+{
+	RT_ASSERT(widget != RT_NULL);
+
+	widget->mini_height = height;
+}
+
+/*
+ * This function moves widget and its children to a logic point
+ */
+void rtgui_widget_move_to_logic(rtgui_widget_t* widget, int dx, int dy)
+{
+	struct rtgui_list_node* node;
+	rtgui_widget_t* child;
+
+	if (widget == RT_NULL) return;
+
+	rtgui_rect_moveto(&(widget->extent), dx, dy);
+
+	/* move each child */
+	if (RTGUI_IS_CONTAINER(widget))
+	{
+		rtgui_list_foreach(node, &(RTGUI_CONTAINER(widget)->children))
+		{
+			child = rtgui_list_entry(node, rtgui_widget_t, sibling);
+
+			rtgui_widget_move_to_logic(child, dx, dy);
+		}
+	}
+}
+
+void rtgui_widget_set_event_handler(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
+{
+	RT_ASSERT(widget != RT_NULL);
+
+	widget->event_handler = handler;
+}
+
+void rtgui_widget_get_rect(rtgui_widget_t* widget, rtgui_rect_t *rect)
+{
+	RT_ASSERT(widget != RT_NULL);
+
+	if (rect != RT_NULL)
+	{
+		rect->x1 = rect->y1 = 0;
+		rect->x2 = widget->extent.x2 - widget->extent.x1;
+		rect->y2 = widget->extent.y2 - widget->extent.y1;
+	}
+}
+
+void rtgui_widget_set_ondraw(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
+{
+	RT_ASSERT(widget != RT_NULL);
+
+	widget->on_draw = handler;
+}
+
+void rtgui_widget_set_onfocus(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
+{
+	RT_ASSERT(widget != RT_NULL);
+
+	widget->on_focus_in = handler;
+}
+
+void rtgui_widget_set_onunfocus(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
+{
+	RT_ASSERT(widget != RT_NULL);
+
+	widget->on_focus_out = handler;
+}
+
+void rtgui_widget_set_onmouseclick(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
+{
+	RT_ASSERT(widget != RT_NULL);
+
+	widget->on_mouseclick = handler;
+}
+
+void rtgui_widget_set_onkey(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
+{
+	RT_ASSERT(widget != RT_NULL);
+
+	widget->on_key = handler;
+}
+
+void rtgui_widget_set_onsize(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
+{
+	RT_ASSERT(widget != RT_NULL);
+
+	widget->on_size = handler;
+}
+
+void rtgui_widget_set_oncommand(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
+{
+	RT_ASSERT(widget != RT_NULL);
+
+	widget->on_command = handler;
+}
+
+/**
+ * @brief Focuses the widget. The focused widget is the one which receives the keyboard events
+ * @param widget a widget
+ * @note The widget has to be attached to a toplevel widget, otherwise it will have no effect
+ */
+void rtgui_widget_focus(rtgui_widget_t *widget)
+{
+   rtgui_widget_t *focused;
+
+	if (!widget || !widget->toplevel || !RTGUI_WIDGET_IS_FOCUSABLE(widget) || !RTGUI_WIDGET_IS_ENABLE(widget))
+		return;
+
+	focused = rtgui_toplevel_get_focus(RTGUI_TOPLEVEL(widget->toplevel));
+
+	if ( focused != RT_NULL && widget == focused)
+		return;
+
+	if (focused != RT_NULL)
+	{
+		rtgui_widget_unfocus(focused);
+	}
+
+	rtgui_toplevel_set_focus(RTGUI_TOPLEVEL(widget->toplevel), widget);
+	widget->flag |= RTGUI_WIDGET_FLAG_FOCUS;
+
+	if (widget->on_focus_in)
+   		widget->on_focus_in(widget, RT_NULL);
+}
+
+/**
+ * @brief Unfocused the widget
+ * @param widget a widget
+ */
+void rtgui_widget_unfocus(rtgui_widget_t *widget)
+{
+	if (!widget || !widget->toplevel || !RTGUI_WIDGET_IS_FOCUS(widget))
+		return;
+
+	if (rtgui_toplevel_get_focus(RTGUI_TOPLEVEL(widget->toplevel)) == widget)
+	{
+		rtgui_toplevel_set_focus(RTGUI_TOPLEVEL(widget->toplevel), RT_NULL);
+	}
+
+	widget->flag &= ~RTGUI_WIDGET_FLAG_FOCUS;
+
+	if (widget->on_focus_out)
+   		widget->on_focus_out(widget, RT_NULL);
+}
+
+void rtgui_widget_point_to_device(rtgui_widget_t* widget, rtgui_point_t* point)
+{
+	RT_ASSERT(widget != RT_NULL);
+
+	if (point != RT_NULL)
+	{
+		point->x += widget->extent.x1;
+		point->y += widget->extent.y1;
+	}
+}
+
+void rtgui_widget_rect_to_device(rtgui_widget_t* widget, rtgui_rect_t* rect)
+{
+	RT_ASSERT(widget != RT_NULL);
+
+	if (rect != RT_NULL)
+	{
+		rect->x1 += widget->extent.x1;
+		rect->x2 += widget->extent.x1;
+
+		rect->y1 += widget->extent.y1;
+		rect->y2 += widget->extent.y1;
+	}
+}
+
+void rtgui_widget_point_to_logic(rtgui_widget_t* widget, rtgui_point_t* point)
+{
+	RT_ASSERT(widget != RT_NULL);
+
+	if (point != RT_NULL)
+	{
+		point->x -= widget->extent.x1;
+		point->y -= widget->extent.y1;
+	}
+}
+
+void rtgui_widget_rect_to_logic(rtgui_widget_t* widget, rtgui_rect_t* rect)
+{
+	RT_ASSERT(widget != RT_NULL);
+
+	if (rect != RT_NULL)
+	{
+		rect->x1 -= widget->extent.x1;
+		rect->x2 -= widget->extent.x1;
+
+		rect->y1 -= widget->extent.y1;
+		rect->y2 -= widget->extent.y1;
+	}
+}
+
+rtgui_widget_t* rtgui_widget_get_toplevel(rtgui_widget_t* widget)
+{
+	rtgui_widget_t* r;
+
+	RT_ASSERT(widget != RT_NULL);
+
+	if (widget->toplevel) return widget->toplevel;
+
+	r = widget;
+	/* get the toplevel widget */
+	while (r->parent != RT_NULL) r = r->parent;
+
+	/* set toplevel */
+	widget->toplevel = r;
+
+	return r;
+}
+
+rt_bool_t rtgui_widget_event_handler(rtgui_widget_t* widget, rtgui_event_t* event)
+{
+	switch (event->type)
+	{
+	case RTGUI_EVENT_PAINT:
+		if (widget->on_draw != RT_NULL) return widget->on_draw(widget, event);
+		break;
+
+	case RTGUI_EVENT_KBD:
+		if (widget->on_key != RT_NULL) return widget->on_key(widget, event);
+		break;
+
+	case RTGUI_EVENT_MOUSE_BUTTON:
+		if (widget->on_mouseclick != RT_NULL) return widget->on_mouseclick(widget, event);
+		break;
+
+	case RTGUI_EVENT_COMMAND:
+		if (widget->on_command != RT_NULL) return widget->on_command(widget, event);
+		break;
+
+	case RTGUI_EVENT_RESIZE:
+		if (widget->on_size != RT_NULL) return widget->on_size(widget, event);
+		break;
+	}
+
+	return RT_FALSE;
+}
+
+/*
+ * This function updates the clip info of widget
+ */
+void rtgui_widget_update_clip(rtgui_widget_t* widget)
+{
+	struct rtgui_list_node* node;
+	rtgui_widget_t *parent;
+
+	/* no widget or widget is hide, no update clip */
+	if (widget == RT_NULL || RTGUI_WIDGET_IS_HIDE(widget)) return;
+
+	parent = widget->parent;
+	/* if there is no parent, do not update clip (please use toplevel widget API) */
+	if (parent == RT_NULL) return;
+
+	/* increase clip sync */
+	widget->clip_sync ++;
+
+	/* reset clip to extent */
+	rtgui_region_reset(&(widget->clip), &(widget->extent));
+
+	/* limit widget extent in parent extent */
+	rtgui_region_intersect(&(widget->clip), &(widget->clip), &(parent->clip));
+
+	/* get the no transparent parent */
+	while (parent != RT_NULL && parent->flag & RTGUI_WIDGET_FLAG_TRANSPARENT)
+	{
+		parent = parent->parent;
+	}
+
+	if (parent != RT_NULL)
+	{
+		/* subtract widget clip in parent clip */
+		if (!(widget->flag & RTGUI_WIDGET_FLAG_TRANSPARENT))
+		{
+			rtgui_region_subtract_rect(&(parent->clip), &(parent->clip),
+				&(widget->extent));
+		}
+	}
+
+	/*
+	 * note: since the layout widget introduction, the sibling widget will not
+	 * intersect.
+	 */
+
+	/* if it's a container object, update the clip info of children */
+	if (RTGUI_IS_CONTAINER(widget))
+	{
+		rtgui_widget_t* child;
+		rtgui_list_foreach(node, &(RTGUI_CONTAINER(widget)->children))
+		{
+			child = rtgui_list_entry(node, rtgui_widget_t, sibling);
+
+			rtgui_widget_update_clip(child);
+		}
+	}
+}
+
+void rtgui_widget_show(rtgui_widget_t* widget)
+{
+	/* there is no parent or the parent is hide, no show at all */
+	if (widget->parent == RT_NULL ||
+		RTGUI_WIDGET_IS_HIDE(widget->parent)) return;
+
+	/* update the clip info of widget */
+	RTGUI_WIDGET_UNHIDE(widget);
+	rtgui_widget_update_clip(widget);
+}
+
+void rtgui_widget_hide(rtgui_widget_t* widget)
+{
+	/* hide this widget */
+	RTGUI_WIDGET_HIDE(widget);
+
+	/* update the clip info of widget parent */
+	rtgui_widget_update_clip(widget->parent);
+}
+
+void rtgui_widget_update(rtgui_widget_t* widget)
+{
+	struct rtgui_event_paint paint;
+	RTGUI_EVENT_PAINT_INIT(&paint);
+	paint.wid = RT_NULL;
+
+	RT_ASSERT(widget != RT_NULL);
+
+	if (widget->event_handler != RT_NULL)
+	{
+		widget->event_handler(widget, &paint.parent);
+	}
+}
+
+rtgui_widget_t* rtgui_widget_get_next_sibling(rtgui_widget_t* widget)
+{
+	rtgui_widget_t* sibling = RT_NULL;
+
+	if (widget->sibling.next != RT_NULL)
+	{
+		sibling = rtgui_list_entry(widget->sibling.next, rtgui_widget_t, sibling);
+	}
+
+	return sibling;
+}
+
+rtgui_widget_t* rtgui_widget_get_prev_sibling(rtgui_widget_t* widget)
+{
+	struct rtgui_list_node* node;
+	rtgui_widget_t *sibling, *parent;
+
+	node = RT_NULL; sibling = RT_NULL;
+	parent = widget->parent;
+	if (parent != RT_NULL)
+	{
+		rtgui_list_foreach(node, &(RTGUI_CONTAINER(parent)->children))
+		{
+			if (node->next == &(widget->sibling))
+				break;
+		}
+	}
+
+	if (node != RT_NULL)
+		sibling = rtgui_list_entry(node, rtgui_widget_t, sibling);
+
+	return sibling;
+}

+ 467 - 0
rtgui/widgets/window.c

@@ -0,0 +1,467 @@
+/*
+ * File      : window.c
+ * This file is part of RTGUI in 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-04     Bernard      first version
+ */
+#include <rtgui/dc.h>
+#include <rtgui/color.h>
+#include <rtgui/image.h>
+#include <rtgui/rtgui_system.h>
+
+#include <rtgui/widgets/window.h>
+#include <rtgui/widgets/button.h>
+
+static void _rtgui_win_constructor(rtgui_win_t *win)
+{
+	/* init window attribute */
+	win->on_activate	= RT_NULL;
+	win->on_deactivate	= RT_NULL;
+	win->on_close		= RT_NULL;
+	win->title			= RT_NULL;
+
+	/* set window hide */
+	RTGUI_WIDGET_HIDE(RTGUI_WIDGET(win));
+
+	/* set window style */
+	win->style = RTGUI_WIN_STYLE_DEFAULT;
+	rtgui_widget_set_event_handler(RTGUI_WIDGET(win), rtgui_win_event_handler);
+
+	/* init user data */
+	win->user_data = 0;
+}
+
+static void _rtgui_win_destructor(rtgui_win_t* win)
+{
+	struct rtgui_event_win_destroy edestroy;
+
+	if (RTGUI_TOPLEVEL(win)->server != RT_NULL)
+	{
+		/* destroy in server */
+		RTGUI_EVENT_WIN_DESTROY_INIT(&edestroy);
+		edestroy.wid = win;
+		if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&edestroy),
+			sizeof(struct rtgui_event_win_destroy)) != RT_EOK)
+		{
+			return;
+		}
+	}
+
+	/* release field */
+	rtgui_free(win->title);
+}
+
+static rt_bool_t _rtgui_win_create_in_server(rtgui_win_t* win)
+{
+	if (RTGUI_TOPLEVEL(win)->server == RT_NULL)
+	{
+		rt_thread_t server;
+		struct rtgui_event_win_create ecreate;
+		RTGUI_EVENT_WIN_CREATE_INIT(&ecreate);
+
+		/* get server thread id */
+		server = rtgui_thread_get_server();
+		if (server == RT_NULL)
+		{
+			rt_kprintf("RTGUI server is not running...\n");
+			return RT_FALSE;
+		}
+
+		/* send win create event to server */
+		ecreate.wid 	= win;
+		ecreate.extent 	= RTGUI_WIDGET(win)->extent;
+		ecreate.flag 	= win->style;
+		ecreate.mask 	= 0;
+		rt_strncpy((char*)ecreate.title, (char*)win->title, RTGUI_NAME_MAX);
+
+		if (rtgui_thread_send_sync(server, RTGUI_EVENT(&ecreate),
+			sizeof(struct rtgui_event_win_create)) != RT_EOK)
+		{
+			rt_kprintf("create win: %s failed\n", win->title);
+			return RT_FALSE;
+		}
+
+		/* set server */
+		RTGUI_TOPLEVEL(win)->server = server;
+	}
+
+	return RT_TRUE;
+}
+
+rtgui_type_t *rtgui_win_type_get(void)
+{
+	static rtgui_type_t *win_type = RT_NULL;
+
+	if (!win_type)
+	{
+		win_type = rtgui_type_create("win", RTGUI_TOPLEVEL_TYPE,
+			sizeof(rtgui_win_t),
+			RTGUI_CONSTRUCTOR(_rtgui_win_constructor),
+			RTGUI_DESTRUCTOR(_rtgui_win_destructor));
+	}
+
+	return win_type;
+}
+
+rtgui_win_t* rtgui_win_create(const char* title, rtgui_rect_t *rect, rt_uint32_t style)
+{
+	struct rtgui_win* win;
+
+	/* allocate win memory */
+	win = (struct rtgui_win*) rtgui_widget_create (RTGUI_WIN_TYPE);
+	if (win != RT_NULL)
+	{
+		/* set title, rect and style */
+		if (title != RT_NULL) win->title = rt_strdup(title);
+		else win->title = RT_NULL;
+
+		rtgui_widget_set_rect(RTGUI_WIDGET(win), rect);
+		win->style = style;
+
+		if (_rtgui_win_create_in_server(win) == RT_FALSE)
+		{
+			rtgui_widget_destroy(RTGUI_WIDGET(win));
+			return RT_NULL;
+		}
+	}
+
+	return win;
+}
+
+void rtgui_win_destroy(struct rtgui_win* win)
+{
+	rtgui_widget_destroy(RTGUI_WIDGET(win));
+}
+
+void rtgui_win_show(struct rtgui_win* win)
+{
+	RT_ASSERT(win != RT_NULL);
+
+	/* if it does not register into server, create it in server */
+	if (RTGUI_TOPLEVEL(win)->server == RT_NULL)
+	{
+		if (_rtgui_win_create_in_server(win) == RT_FALSE)
+			return;
+	}
+
+	if (RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(win)))
+	{
+		/* send show message to server */
+		struct rtgui_event_win_show eshow;
+		RTGUI_EVENT_WIN_SHOW_INIT(&eshow);
+		eshow.wid = win;
+
+		if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&eshow),
+			sizeof(struct rtgui_event_win_show)) != RT_EOK)
+		{
+			/* hide window failed */
+			return;
+		}
+
+		/* set window unhidden */
+		RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(win));
+	}
+	else rtgui_widget_update(RTGUI_WIDGET(win));
+}
+
+void rtgui_win_hiden(struct rtgui_win* win)
+{
+	RT_ASSERT(win != RT_NULL);
+
+	if (!RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(win)) &&
+		RTGUI_TOPLEVEL(win)->server != RT_NULL)
+	{
+		/* send hidden message to server */
+		struct rtgui_event_win_hide ehide;
+		RTGUI_EVENT_WIN_HIDE_INIT(&ehide);
+		ehide.wid = win;
+
+		if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&ehide),
+			sizeof(struct rtgui_event_win_hide)) != RT_EOK)
+		{
+			rt_kprintf("hide win: %s failed\n", win->title);
+			return;
+		}
+
+		/* set window hide and deactivated */
+		RTGUI_WIDGET_HIDE(RTGUI_WIDGET(win));
+		win->style &= ~RTGUI_WIN_STYLE_ACTIVATE;
+	}
+}
+
+rt_bool_t rtgui_win_is_activated(struct rtgui_win* win)
+{
+	RT_ASSERT(win != RT_NULL);
+
+	return win->style & RTGUI_WIN_STYLE_ACTIVATE;
+}
+
+void rtgui_win_move(struct rtgui_win* win, int x, int y)
+{
+	struct rtgui_event_win_move emove;
+	RTGUI_EVENT_WIN_MOVE_INIT(&emove);
+
+	if (win == RT_NULL) return;
+
+	if (RTGUI_TOPLEVEL(win)->server != RT_NULL)
+	{
+		/* set win hide firstly */
+		RTGUI_WIDGET_HIDE(RTGUI_WIDGET(win));
+
+		emove.wid 	= win;
+		emove.x		= x;
+		emove.y		= y;
+		if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&emove),
+			sizeof(struct rtgui_event_win_move)) != RT_EOK)
+		{
+			return;
+		}
+	}
+
+	/* move window to logic position */
+	rtgui_widget_move_to_logic(RTGUI_WIDGET(win),
+		x - RTGUI_WIDGET(win)->extent.x1,
+		y - RTGUI_WIDGET(win)->extent.y1);
+
+	/* set window visible */
+	RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(win));
+
+	/* update window */
+	// rtgui_widget_update(RTGUI_WIDGET(win));
+	return;
+}
+
+static rt_bool_t rtgui_win_ondraw(struct rtgui_win* win)
+{
+	struct rtgui_dc* dc;
+	struct rtgui_rect rect;
+	struct rtgui_event_paint event;
+
+	/* begin drawing */
+	dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(win));
+	if (dc == RT_NULL) return RT_FALSE;
+
+	/* get window rect */
+	rtgui_widget_get_rect(RTGUI_WIDGET(win), &rect);
+	/* fill area */
+	rtgui_dc_fill_rect(dc, &rect);
+
+	/* paint each widget */
+	RTGUI_EVENT_PAINT_INIT(&event);
+	event.wid = RT_NULL;
+	rtgui_container_dispatch_event(RTGUI_CONTAINER(win), (rtgui_event_t*)&event);
+
+	rtgui_dc_end_drawing(dc);
+
+	return RT_FALSE;
+}
+
+rt_bool_t rtgui_win_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
+{
+	struct rtgui_win* win = (struct rtgui_win*)widget;
+
+	RT_ASSERT((win != RT_NULL) && (event != RT_NULL));
+
+	switch (event->type)
+	{
+	case RTGUI_EVENT_WIN_SHOW:
+		rtgui_win_show(win);
+		break;
+
+	case RTGUI_EVENT_WIN_HIDE:
+		rtgui_win_hiden(win);
+		break;
+
+	case RTGUI_EVENT_WIN_CLOSE:
+		if (win->on_close != RT_NULL)
+		{
+			if (win->on_close(widget, event) == RT_FALSE) return RT_TRUE;
+		}
+
+		/* destroy window */
+		rtgui_win_destroy(win);
+
+		/* exit event loop */
+		return RT_TRUE;
+
+	case RTGUI_EVENT_WIN_MOVE:
+		{
+			struct rtgui_event_win_move* emove = (struct rtgui_event_win_move*)event;
+
+			/* move window */
+			rtgui_win_move(win, emove->x, emove->y);
+		}
+		break;
+
+	case RTGUI_EVENT_WIN_ACTIVATE:
+		if (RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(win)))
+		{
+			rt_kprintf("activate window, but window is hide!\n");
+		}
+
+		win->style |= RTGUI_WIN_STYLE_ACTIVATE;
+		if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
+		else rtgui_win_ondraw(win);
+
+		if (win->on_activate != RT_NULL)
+		{
+			win->on_activate(widget, event);
+		}
+		break;
+
+	case RTGUI_EVENT_WIN_DEACTIVATE:
+		win->style &= ~RTGUI_WIN_STYLE_ACTIVATE;
+		if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
+		else rtgui_win_ondraw(win);
+
+		if (win->on_deactivate != RT_NULL)
+		{
+			win->on_deactivate(widget, event);
+		}
+		break;
+
+	case RTGUI_EVENT_PAINT:
+		if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
+		else rtgui_win_ondraw(win);
+		break;
+
+	case RTGUI_EVENT_MOUSE_BUTTON:
+		if (rtgui_container_dispatch_mouse_event(RTGUI_CONTAINER(win), (struct rtgui_event_mouse*)event) == RT_FALSE)
+		{
+			if (widget->on_mouseclick != RT_NULL)
+			{
+				return widget->on_mouseclick(widget, event);
+			}
+		}
+		break;
+
+	case RTGUI_EVENT_MOUSE_MOTION:
+#if 0
+		if (rtgui_widget_dispatch_mouse_event(widget,
+			(struct rtgui_event_mouse*)event) == RT_FALSE)
+		{
+			/* handle event in current widget */
+			if (widget->on_mousemotion != RT_NULL)
+			{
+				return widget->on_mousemotion(widget, event);
+			}
+		}
+		else return RT_TRUE;
+#endif
+		break;
+
+	default:
+		/* call parent event handler */
+		return rtgui_toplevel_event_handler(widget, event);
+	}
+
+	return RT_FALSE;
+}
+
+/* windows event loop */
+void rtgui_win_event_loop(rtgui_win_t* wnd)
+{
+	int quit = 0;
+
+	/* the buffer uses to receive event */
+	char event_buf[256];
+
+	struct rtgui_event* event = (struct rtgui_event*)&event_buf[0];
+
+	while (!quit)
+	{
+		if (rtgui_thread_recv(event, sizeof(event_buf)) == RT_EOK)
+		{
+			if (RTGUI_WIDGET(wnd)->event_handler != RT_NULL)
+			{
+				if (RTGUI_WIDGET(wnd)->event_handler(RTGUI_WIDGET(wnd), event) == RT_TRUE)
+					quit = 1;
+			}
+		}
+	}
+}
+
+void rtgui_win_set_rect(rtgui_win_t* win, rtgui_rect_t* rect)
+{
+	struct rtgui_event_win_resize event;
+
+	if (win == RT_NULL || rect == RT_NULL) return;
+
+	RTGUI_WIDGET(win)->extent = *rect;
+
+	if (RTGUI_TOPLEVEL(win)->server != RT_NULL)
+	{
+		/* set window resize event to server */
+		RTGUI_EVENT_WIN_RESIZE_INIT(&event);
+		event.wid = win;
+		event.rect = *rect;
+
+		rtgui_thread_send(RTGUI_TOPLEVEL(win)->server, &(event.parent), sizeof(struct rtgui_event_win_resize));
+	}
+}
+
+void rtgui_win_set_box(rtgui_win_t* win, rtgui_box_t* box)
+{
+	if (win == RT_NULL || box == RT_NULL) return;
+
+	rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(box));
+	rtgui_widget_set_rect(RTGUI_WIDGET(box), &(RTGUI_WIDGET(win)->extent));
+}
+
+void rtgui_win_set_onactivate(rtgui_win_t* win, rtgui_event_handler_ptr handler)
+{
+	if (win != RT_NULL)
+	{
+		win->on_activate = handler;
+	}
+}
+
+void rtgui_win_set_ondeactivate(rtgui_win_t* win, rtgui_event_handler_ptr handler)
+{
+	if (win != RT_NULL)
+	{
+		win->on_deactivate = handler;
+	}
+}
+
+void rtgui_win_set_onclose(rtgui_win_t* win, rtgui_event_handler_ptr handler)
+{
+	if (win != RT_NULL)
+	{
+		win->on_close = handler;
+	}
+}
+
+void rtgui_win_set_title(rtgui_win_t* win, const char *title)
+{
+	/* send title to server */
+	if (RTGUI_TOPLEVEL(win)->server != RT_NULL)
+	{
+	}
+
+	/* modify in local side */
+	if (win->title != RT_NULL)
+	{
+		rtgui_free(win->title);
+		win->title = RT_NULL;
+	}
+
+	if (title != RT_NULL)
+	{
+		win->title = rt_strdup(title);
+	}
+}
+
+char* rtgui_win_get_title(rtgui_win_t* win)
+{
+	RT_ASSERT(win != RT_NULL);
+
+	return win->title;
+}

+ 472 - 0
rtgui/widgets/workbench.c

@@ -0,0 +1,472 @@
+/*
+ * File      : workbench.c
+ * This file is part of RTGUI in 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-04     Bernard      first version
+ */
+#include <rtgui/rtgui_system.h>
+#include <rtgui/widgets/window.h>
+#include <rtgui/widgets/workbench.h>
+
+static void _rtgui_workbench_constructor(rtgui_workbench_t *workbench)
+{
+	/* set event handler */
+	rtgui_widget_set_event_handler(RTGUI_WIDGET(workbench), rtgui_workbench_event_handler);
+
+	/* set attributes */
+	workbench->panel = RT_NULL;
+	workbench->flag = RTGUI_WORKBENCH_FLAG_DEFAULT;
+}
+
+static void _rtgui_workbench_destructor(rtgui_workbench_t *workbench)
+{
+	RT_ASSERT(workbench != RT_NULL);
+
+	if (RTGUI_TOPLEVEL(workbench)->server != RT_NULL)
+	{
+		struct rtgui_event_panel_detach edetach;
+		RTGUI_EVENT_PANEL_DETACH_INIT(&edetach);
+
+		/* detach from panel */
+		edetach.panel = workbench->panel;
+
+		/* send PANEL DETACH to server */
+		if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(workbench)->server,
+			RTGUI_EVENT(&edetach), sizeof(struct rtgui_event_panel_detach)) != RT_EOK)
+			return;
+
+		RTGUI_TOPLEVEL(workbench)->server = RT_NULL;
+	}
+
+	/* release title */
+	if (workbench->title != RT_NULL)
+	{
+		rtgui_free(workbench->title);
+	}
+}
+
+rtgui_type_t *rtgui_workbench_type_get(void)
+{
+	static rtgui_type_t *workbench_type = RT_NULL;
+
+	if (!workbench_type)
+	{
+		workbench_type = rtgui_type_create("workbench", RTGUI_TOPLEVEL_TYPE,
+			sizeof(rtgui_workbench_t),
+			RTGUI_CONSTRUCTOR(_rtgui_workbench_constructor),
+			RTGUI_DESTRUCTOR(_rtgui_workbench_destructor));
+	}
+
+	return workbench_type;
+}
+
+rtgui_workbench_t *rtgui_workbench_create(const char* panel_name, const unsigned char* title)
+{
+	struct rtgui_workbench* workbench;
+
+	/* the server thread id */
+	rt_thread_t server = rtgui_thread_get_server();
+	if (server == RT_NULL)
+	{
+		rt_kprintf("can't find rtgui server\n");
+		return RT_NULL;
+	}
+
+	/* create workbench */
+	workbench = (rtgui_workbench_t*) rtgui_widget_create (RTGUI_WORKBENCH_TYPE);
+	if (workbench != RT_NULL)
+	{
+		/* the buffer uses to receive event */
+		union
+		{
+			struct rtgui_event_panel_attach ecreate;
+			struct rtgui_event_panel_info epanel;
+
+			char buffer[256];	/* use to recv other information */
+		} event;
+
+		/* set workbench title */
+		workbench->title = (unsigned char*)rt_strdup((char*)title);
+
+		/* create application in server */
+		RTGUI_EVENT_PANEL_ATTACH_INIT(&(event.ecreate));
+
+		/* set the panel name and workbench */
+		rt_strncpy(event.ecreate.panel_name, panel_name, RTGUI_NAME_MAX);
+		event.ecreate.workbench = workbench;
+
+		/* send PANEL ATTACH to server */
+		if (rtgui_thread_send_sync(server,
+			&(event.ecreate.parent), sizeof(struct rtgui_event_panel_attach)) != RTGUI_STATUS_OK)
+		{
+			return RT_NULL;
+		}
+
+		/* get PANEL INFO */
+		rtgui_thread_recv_filter(RTGUI_EVENT_PANEL_INFO, &(event.epanel.parent), sizeof(event));
+
+		/* set panel */
+		workbench->panel = (struct rtgui_panel*)event.epanel.panel;
+
+		/* connected */
+		RTGUI_TOPLEVEL(workbench)->server = server;
+
+		/* set extent of workbench */
+		rtgui_widget_set_rect(RTGUI_WIDGET(workbench), &(event.epanel.extent));
+
+		/* set workbench in thread */
+		rtgui_thread_set_widget(RTGUI_WIDGET(workbench));
+	}
+
+	return workbench;
+}
+
+void rtgui_workbench_destroy(rtgui_workbench_t* workbench)
+{
+	RT_ASSERT(workbench != RT_NULL);
+
+	if (RTGUI_TOPLEVEL(workbench)->server != RT_NULL)
+	{
+		struct rtgui_event_panel_detach edetach;
+		RTGUI_EVENT_PANEL_DETACH_INIT(&edetach);
+
+		/* detach from panel */
+		edetach.panel = workbench->panel;
+
+		/* send PANEL DETACH to server */
+		if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(workbench)->server,
+			RTGUI_EVENT(&edetach), sizeof(struct rtgui_event_panel_detach)) != RT_EOK)
+			return;
+
+			RTGUI_TOPLEVEL(workbench)->server = RT_NULL;
+	}
+
+	rtgui_widget_destroy(RTGUI_WIDGET(workbench));
+}
+
+void rtgui_workbench_set_flag(rtgui_workbench_t* workbench, rt_uint8_t flag)
+{
+	RT_ASSERT(workbench != RT_NULL);
+
+	workbench->flag = flag;
+}
+
+rtgui_view_t *rtgui_workbench_get_current_view(rtgui_workbench_t * workbench)
+{
+	struct rtgui_list_node* node;
+	struct rtgui_widget* view;
+
+	RT_ASSERT(workbench != RT_NULL);
+
+	/* find the first shown view */
+	rtgui_list_foreach(node, &(RTGUI_CONTAINER(workbench)->children))
+	{
+		view = rtgui_list_entry(node, struct rtgui_widget, sibling);
+
+		/* is it a shown view? */
+		if (!RTGUI_WIDGET_IS_HIDE(view))
+		{
+			return (rtgui_view_t*)view;
+		}
+	}
+
+	return RT_NULL;
+}
+
+void rtgui_workbench_event_loop(rtgui_workbench_t* workbench)
+{
+	int quit = 0;
+
+	/* the buffer uses to receive event */
+	char event_buf[256];
+	struct rtgui_event* event = (struct rtgui_event*)&event_buf[0];
+
+	/* show workbench firstly */
+	rtgui_workbench_show(workbench);
+
+	while (!quit)
+	{
+		if (rtgui_thread_recv(event, sizeof(event_buf)) == RT_EOK)
+		{
+			RTGUI_WIDGET(workbench)->event_handler(RTGUI_WIDGET(workbench), event);
+		}
+	}
+}
+
+rt_err_t rtgui_workbench_show(rtgui_workbench_t* workbench)
+{
+	RT_ASSERT(workbench != RT_NULL);
+
+	if (RTGUI_TOPLEVEL(workbench)->server != RT_NULL)
+	{
+		struct rtgui_event_panel_show eraise;
+		RTGUI_EVENT_PANEL_SHOW_INIT(&eraise);
+		eraise.workbench = workbench;
+
+		eraise.panel = workbench->panel;
+		if (rtgui_thread_send_sync(workbench->parent.server, RTGUI_EVENT(&eraise),
+			sizeof(struct rtgui_event_panel_show)) != RT_EOK)
+			return -RT_ERROR;
+
+		RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(workbench));
+		rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(workbench));
+	}
+	else return -RT_ERROR;
+
+	return RT_EOK;
+}
+
+rt_err_t rtgui_workbench_hide(rtgui_workbench_t* workbench)
+{
+	if (RTGUI_TOPLEVEL(workbench)->server != RT_NULL)
+	{
+		struct rtgui_event_panel_hide ehide;
+		RTGUI_EVENT_PANEL_HIDE_INIT(&ehide);
+
+		RT_ASSERT(workbench != RT_NULL);
+		if (workbench->parent.server == RT_NULL) return -RT_ERROR;
+
+		ehide.panel = workbench->panel;
+		if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(workbench)->server, RTGUI_EVENT(&ehide),
+			sizeof(struct rtgui_event_panel_hide)) != RT_EOK)
+			return -RT_ERROR;
+
+		RTGUI_WIDGET_HIDE(RTGUI_WIDGET(workbench));
+		rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(workbench));
+	}
+
+	return RT_EOK;
+}
+
+rt_bool_t rtgui_workbench_event_handler(rtgui_widget_t* widget, rtgui_event_t* event)
+{
+	struct rtgui_workbench* workbench = (struct rtgui_workbench*)widget;
+
+	switch (event->type)
+	{
+	case RTGUI_EVENT_PANEL_DETACH:
+		RTGUI_WIDGET_HIDE(RTGUI_WIDGET(workbench));
+		RTGUI_TOPLEVEL(workbench)->server = RT_NULL;
+		return RT_TRUE;
+
+	case RTGUI_EVENT_PANEL_SHOW:
+		/* show workbench in server */
+		rtgui_workbench_show(workbench);
+		break;
+
+	case RTGUI_EVENT_PANEL_HIDE:
+		/* hide widget */
+		RTGUI_WIDGET_HIDE(widget);
+		break;
+
+	case RTGUI_EVENT_MOUSE_MOTION:
+		{
+			struct rtgui_event_mouse* emouse = (struct rtgui_event_mouse*)event;
+			struct rtgui_toplevel* top = RTGUI_TOPLEVEL(emouse->wid);
+
+			/* check the destination window */
+			if (top != RT_NULL && RTGUI_WIDGET(top)->event_handler != RT_NULL)
+			{
+				RTGUI_WIDGET(top)->event_handler(RTGUI_WIDGET(top), event);
+			}
+			else
+			{
+				/* let viewer to handle it */
+				rtgui_view_t* view = rtgui_workbench_get_current_view(workbench);
+				if (view != RT_NULL &&
+					RTGUI_WIDGET(view)->event_handler != RT_NULL)
+				{
+					RTGUI_WIDGET(view)->event_handler(RTGUI_WIDGET(view), event);
+				}
+			}
+		}
+		break;
+
+	case RTGUI_EVENT_MOUSE_BUTTON:
+		{
+			struct rtgui_event_mouse* emouse = (struct rtgui_event_mouse*)event;
+			struct rtgui_toplevel* top = RTGUI_TOPLEVEL(emouse->wid);
+
+			/* check the destination window */
+			if (top != RT_NULL && RTGUI_WIDGET(top)->event_handler != RT_NULL)
+			{
+				RTGUI_WIDGET(top)->event_handler(RTGUI_WIDGET(top), event);
+			}
+			else
+			{
+				/* let viewer to handle it */
+				rtgui_view_t* view = rtgui_workbench_get_current_view(workbench);
+				if (view != RT_NULL &&
+						RTGUI_WIDGET(view)->event_handler != RT_NULL)
+				{
+					RTGUI_WIDGET(view)->event_handler(RTGUI_WIDGET(view), event);
+				}
+			}
+		}
+		break;
+
+	case RTGUI_EVENT_KBD:
+		{
+			struct rtgui_event_kbd* kbd = (struct rtgui_event_kbd*)event;
+			struct rtgui_toplevel* top = RTGUI_TOPLEVEL(kbd->wid);
+
+			/* check the destination window */
+			if (top != RT_NULL && RTGUI_WIDGET(top)->event_handler != RT_NULL)
+			{
+				RTGUI_WIDGET(top)->event_handler(RTGUI_WIDGET(top), event);
+			}
+			else
+			{
+				return rtgui_toplevel_event_handler(widget, event);
+			}
+		}
+		break;
+
+	case RTGUI_EVENT_PAINT:
+		{
+			struct rtgui_event_paint* epaint = (struct rtgui_event_paint*)event;
+			struct rtgui_toplevel* top = RTGUI_TOPLEVEL(epaint->wid);
+
+			/* check the destination window */
+			if (top != RT_NULL && RTGUI_WIDGET(top)->event_handler != RT_NULL)
+			{
+				RTGUI_WIDGET(top)->event_handler(RTGUI_WIDGET(top), event);
+			}
+			else
+			{
+				rtgui_view_t* view;
+
+				/* paint a view */
+				view = rtgui_workbench_get_current_view(workbench);
+				if (view != RT_NULL)
+				{
+					/* remake a paint event */
+					RTGUI_EVENT_PAINT_INIT(epaint);
+					epaint->wid = RT_NULL;
+
+					/* send this event to the view */
+					if (RTGUI_WIDGET(view)->event_handler != RT_NULL)
+					{
+						RTGUI_WIDGET(view)->event_handler(RTGUI_WIDGET(view), event);
+					}
+				}
+			}
+		}
+		break;
+
+	case RTGUI_EVENT_CLIP_INFO:
+		{
+			struct rtgui_event_clip_info* eclip = (struct rtgui_event_clip_info*)event;
+			struct rtgui_widget* dest_widget = RTGUI_WIDGET(eclip->wid);
+
+			if (dest_widget != RT_NULL && dest_widget->event_handler != RT_NULL)
+			{
+				dest_widget->event_handler(dest_widget, event);
+			}
+			else
+			{
+				return rtgui_toplevel_event_handler(widget, event);
+			}
+		}
+		break;
+
+	case RTGUI_EVENT_WIN_CLOSE:
+	case RTGUI_EVENT_WIN_ACTIVATE:
+	case RTGUI_EVENT_WIN_DEACTIVATE:
+		{
+			struct rtgui_event_win* wevent = (struct rtgui_event_win*)event;
+			struct rtgui_widget* dest_widget = RTGUI_WIDGET(wevent->wid);
+			if (dest_widget != RT_NULL && dest_widget->event_handler != RT_NULL)
+			{
+				dest_widget->event_handler(dest_widget, event);
+			}
+		}
+		break;
+
+	case RTGUI_EVENT_WIN_MOVE:
+		{
+			struct rtgui_event_win_move* wevent = (struct rtgui_event_win_move*)event;
+			struct rtgui_toplevel* top = RTGUI_TOPLEVEL(wevent->wid);
+			if (top != RT_NULL && RTGUI_WIDGET(top)->event_handler != RT_NULL)
+			{
+				RTGUI_WIDGET(top)->event_handler(RTGUI_WIDGET(top), event);
+			}
+		}
+		break;
+
+	default:
+		return rtgui_toplevel_event_handler(widget, event);
+	}
+
+	return RT_TRUE;
+}
+
+void rtgui_workbench_add_view(rtgui_workbench_t* workbench, rtgui_view_t* view)
+{
+	rtgui_container_add_child(RTGUI_CONTAINER(workbench), RTGUI_WIDGET(view));
+
+	/* reset view extent */
+	rtgui_widget_set_rect(RTGUI_WIDGET(view), &(RTGUI_WIDGET(workbench)->extent));
+}
+
+void rtgui_workbench_remove_view(rtgui_workbench_t* workbench, rtgui_view_t* view)
+{
+	rtgui_container_remove_child(RTGUI_CONTAINER(workbench), RTGUI_WIDGET(view));
+}
+
+void rtgui_workbench_show_view(rtgui_workbench_t* workbench, rtgui_view_t* view)
+{
+	RT_ASSERT(workbench != RT_NULL);
+	RT_ASSERT(view != RT_NULL);
+
+	if (rtgui_workbench_get_current_view(workbench) == view &&
+		!RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(view))) return;
+
+	/* remove from child list */
+	rtgui_list_remove(&(RTGUI_CONTAINER(workbench)->children), &(RTGUI_WIDGET(view)->sibling));
+
+	/* insert to the head of child list */
+	rtgui_list_insert(&(RTGUI_CONTAINER(workbench)->children), &(RTGUI_WIDGET(view)->sibling));
+
+	/* show view */
+	RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(view));
+
+	/* update workbench clip */
+	rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(workbench));
+
+	if (!RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(workbench)))
+	{
+		rtgui_widget_update(RTGUI_WIDGET(view));
+	}
+}
+
+void rtgui_workbench_hide_view(rtgui_workbench_t* workbench, rtgui_view_t* view)
+{
+	RT_ASSERT(workbench != RT_NULL);
+	RT_ASSERT(view != RT_NULL);
+
+	/* remove from child list */
+	rtgui_list_remove(&(RTGUI_CONTAINER(workbench)->children), &(RTGUI_WIDGET(view)->sibling));
+
+	/* append to the end of child list */
+	rtgui_list_append(&(RTGUI_CONTAINER(workbench)->children), &(RTGUI_WIDGET(view)->sibling));
+
+	/* hide view */
+	RTGUI_WIDGET_HIDE(RTGUI_WIDGET(view));
+
+	/* update workbench clip */
+	rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(workbench));
+
+	if (!RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(workbench)))
+	{
+		view = rtgui_workbench_get_current_view(workbench);
+		rtgui_widget_update(RTGUI_WIDGET(view));
+	}
+}