From c260b4d77fce75937738bf12e9ffa1c78e9dff4e Mon Sep 17 00:00:00 2001 From: "bernard.xiong@gmail.com" Date: Wed, 22 Dec 2010 23:57:44 +0000 Subject: [PATCH] update notebook control. git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1223 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- components/rtgui/common/rtgui_system.c | 18 +++ components/rtgui/include/rtgui/rtgui.h | 1 + components/rtgui/include/rtgui/rtgui_system.h | 3 +- .../rtgui/include/rtgui/widgets/notebook.h | 15 +- .../rtgui/include/rtgui/widgets/widget.h | 2 +- components/rtgui/widgets/notebook.c | 146 +++++++++++++++++- components/rtgui/widgets/widget.c | 2 +- 7 files changed, 173 insertions(+), 14 deletions(-) diff --git a/components/rtgui/common/rtgui_system.c b/components/rtgui/common/rtgui_system.c index 15843c51aa..4d5fe601b5 100644 --- a/components/rtgui/common/rtgui_system.c +++ b/components/rtgui/common/rtgui_system.c @@ -736,6 +736,24 @@ void* rtgui_malloc(rt_size_t size) return ptr; } +void* rtgui_realloc(void* ptr, rt_size_t size) +{ + void* new_ptr; + +#ifdef RTGUI_MEM_TRACE + new_ptr = rtgui_malloc(size); + if ((new_ptr != RT_NULL) && (ptr != RT_NULL)) + { + rt_memcpy(new_ptr, ptr, size); + rtgui_free(ptr); + } +#else + new_ptr = rt_realloc(ptr, size); +#endif + + return new_ptr; +} + void rtgui_free(void* ptr) { #ifdef RTGUI_MEM_TRACE diff --git a/components/rtgui/include/rtgui/rtgui.h b/components/rtgui/include/rtgui/rtgui.h index fe69610b15..780e55456e 100644 --- a/components/rtgui/include/rtgui/rtgui.h +++ b/components/rtgui/include/rtgui/rtgui.h @@ -19,6 +19,7 @@ #define RT_INT16_MAX 32767 #define RT_INT16_MIN (-RT_INT16_MAX-1) +#define RTGUI_NOT_FOUND (-1) struct rtgui_panel; struct rtgui_event; diff --git a/components/rtgui/include/rtgui/rtgui_system.h b/components/rtgui/include/rtgui/rtgui_system.h index 4b963a3529..0450aa6563 100644 --- a/components/rtgui/include/rtgui/rtgui_system.h +++ b/components/rtgui/include/rtgui/rtgui_system.h @@ -82,7 +82,7 @@ rt_err_t rtgui_thread_send(rt_thread_t tid, struct rtgui_event* event, rt_size_t 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_nosuspend(struct rtgui_event* event, rt_size_t event_size); +rt_err_t rtgui_thread_recv_nosuspend(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); @@ -91,6 +91,7 @@ void rtgui_system_server_init(void); void* rtgui_malloc(rt_size_t size); void rtgui_free(void* ptr); +void* rtgui_realloc(void* ptr, rt_size_t size); #endif diff --git a/components/rtgui/include/rtgui/widgets/notebook.h b/components/rtgui/include/rtgui/widgets/notebook.h index 76e4298ec2..477150c014 100644 --- a/components/rtgui/include/rtgui/widgets/notebook.h +++ b/components/rtgui/include/rtgui/widgets/notebook.h @@ -2,7 +2,7 @@ #define __RTGUI_NOTEBOOK_H__ #include -#include +#include /** Gets the type of a notebook */ #define RTGUI_NOTEBOOK_TYPE (rtgui_notebook_type_get()) @@ -11,12 +11,19 @@ /** Checks if the object is a notebook control */ #define RTGUI_IS_NOTEBOOK(obj) (RTGUI_OBJECT_CHECK_TYPE((obj), RTGUI_NOTEBOOK_TYPE)) +struct rtgui_notebook_tab +{ + char* title; + struct rtgui_widget* widget; +}; +typedef struct rtgui_notebook_tab rtgui_notebook_tab_t; + struct rtgui_notebook { - struct rtgui_container parent; + struct rtgui_widget parent; /* widget private data */ - struct rtgui_widget* childs; + rtgui_notebook_tab_t* childs; rt_uint16_t count; rt_int16_t current; }; @@ -28,7 +35,7 @@ rtgui_notebook_t* rtgui_notebook_create(const rtgui_rect_t* rect); void rtgui_notebook_destroy(rtgui_notebook_t* notebook); void rtgui_notebook_add(rtgui_notebook_t* notebook, const char* label, rtgui_widget_t* child); -int rtgui_notebook_get_count(rtgui_notebootk_t* notebook); +int rtgui_notebook_get_count(rtgui_notebook_t* notebook); rtgui_widget_t* rtgui_notebook_get_current(rtgui_notebook_t* notebook); void rtgui_notebook_set_current(rtgui_notebook_t* notebook, rtgui_widget_t* widget); void rtgui_notebook_set_current_by_index(rtgui_notebook_t* notebook, rt_uint16_t index); diff --git a/components/rtgui/include/rtgui/widgets/widget.h b/components/rtgui/include/rtgui/widgets/widget.h index 1a58d6bead..dc58cac76d 100644 --- a/components/rtgui/include/rtgui/widgets/widget.h +++ b/components/rtgui/include/rtgui/widgets/widget.h @@ -149,7 +149,7 @@ void rtgui_widget_set_oncommand(rtgui_widget_t* widget, rtgui_event_handler_ptr /* 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_rect(rtgui_widget_t* widget, const rtgui_rect_t* rect); void rtgui_widget_get_extent(rtgui_widget_t* widget, rtgui_rect_t *rect); #ifndef RTGUI_USING_SMALL_SIZE diff --git a/components/rtgui/widgets/notebook.c b/components/rtgui/widgets/notebook.c index eed59c99d4..0ab8a375a2 100644 --- a/components/rtgui/widgets/notebook.c +++ b/components/rtgui/widgets/notebook.c @@ -1,23 +1,145 @@ +#include #include +#include #include +static void _rtgui_notebook_constructor(rtgui_notebook_t *notebook) +{ + notebook->childs = RT_NULL; + notebook->count = 0; + notebook->current = RTGUI_NOT_FOUND; +} + +static void _rtgui_notebook_destructor(rtgui_notebook_t *notebook) +{ + int index; + + if (notebook->childs != RT_NULL) + { + for (index = 0; index < notebook->count; index ++) + { + rtgui_widget_destroy(notebook->childs[index].widget); + rt_free(notebook->childs[index].title); + } + + rtgui_free(notebook->childs); + } +} + +static void _rtgui_notebook_ondraw(rtgui_notebook_t *notebook) +{ + struct rtgui_dc* dc; + rtgui_rect_t rect; + int index; + + dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(notebook)); + if (dc == RT_NULL) return; + + rtgui_widget_get_rect(RTGUI_WIDGET(notebook), &rect); + + if (notebook->count == 0) + { + rtgui_dc_fill_rect(dc, &rect); + } + else + { + /* draw tab bar */ + for (index = 0; index < notebook->count; index ++) + { + } + + /* draw current tab */ + rtgui_widget_update(notebook->childs[notebook->current].widget); + } + rtgui_dc_end_drawing(dc); +} + rtgui_type_t *rtgui_notebook_type_get(void) { + static rtgui_type_t *noteboot_type = RT_NULL; + + if (!noteboot_type) + { + noteboot_type = rtgui_type_create("notebook", RTGUI_WIDGET_TYPE, + sizeof(rtgui_notebook_t), + RTGUI_CONSTRUCTOR(_rtgui_notebook_constructor), + RTGUI_DESTRUCTOR(_rtgui_notebook_destructor)); + } + + return noteboot_type; } rtgui_notebook_t* rtgui_notebook_create(const rtgui_rect_t* rect) { + struct rtgui_notebook* notebook; + + notebook = (struct rtgui_notebook*) rtgui_widget_create (RTGUI_NOTEBOOK_TYPE); + if (notebook != RT_NULL) + { + rtgui_widget_set_rect(RTGUI_WIDGET(notebook), rect); + } + + return notebook; } void rtgui_notebook_destroy(rtgui_notebook_t* notebook) { + rtgui_widget_destroy(RTGUI_WIDGET(notebook)); } void rtgui_notebook_add(rtgui_notebook_t* notebook, const char* label, rtgui_widget_t* child) { + RT_ASSERT(notebook != RT_NULL); + + notebook->count += 1; + notebook->childs = (struct rtgui_notebook_tab*) rtgui_realloc(notebook->childs, + sizeof(struct rtgui_notebook_tab) * notebook->count); + + notebook->childs[notebook->count - 1].title = rt_strdup(label); + notebook->childs[notebook->count - 1].widget = child; +} + +void rtgui_notebook_remove(rtgui_notebook_t* notebook, rt_uint16_t index) +{ + struct rtgui_notebook_tab tab; + RT_ASSERT(notebook != RT_NULL); + + if (index < notebook->count) + { + if (notebook->count == 1) + { + tab = notebook->childs[0]; + rtgui_free(notebook->childs); + notebook->childs = RT_NULL; + notebook->count = 0; + } + else + { + tab = notebook->childs[index]; + for (;index < notebook->count - 1; index ++) + { + notebook->childs[index] = notebook->childs[index + 1]; + } + + notebook->count -= 1; + notebook->childs = (struct rtgui_notebook_tab*) rtgui_realloc(notebook->childs, + sizeof(struct rtgui_notebook_tab) * notebook->count); + } + + rtgui_widget_destroy(tab.widget); + rtgui_free(tab.title); + + if (notebook->current == index) + { + /* update tab */ + if (notebook->current > notebook->count - 1) + notebook->current = notebook->count - 1; + rtgui_widget_update(RTGUI_WIDGET(notebook)); + } + } } -int rtgui_notebook_get_count(rtgui_notebootk_t* notebook) +int rtgui_notebook_get_count(rtgui_notebook_t* notebook) { return notebook->count; } @@ -26,7 +148,7 @@ rtgui_widget_t* rtgui_notebook_get_current(rtgui_notebook_t* notebook) { RT_ASSERT(notebook != RT_NULL); if (notebook->current != RTGUI_NOT_FOUND) - return ¬ebook->childs[notebook->current]; + return notebook->childs[notebook->current].widget; return RT_NULL; } @@ -38,7 +160,7 @@ void rtgui_notebook_set_current(rtgui_notebook_t* notebook, rtgui_widget_t* widg for (index = 0; index < notebook->count; index ++) { - if (widget == ¬ebook->childs[index]) + if (widget == notebook->childs[index].widget) { rtgui_notebook_set_current_by_index(notebook, index); return; @@ -53,11 +175,11 @@ void rtgui_notebook_set_current_by_index(rtgui_notebook_t* notebook, rt_uint16_t if ((index < notebook->count) && (notebook->current != index)) { if (notebook->current != RTGUI_NOT_FOUND) - rtgui_widget_hide(¬ebook->childs[notebook->current]); + rtgui_widget_hide(notebook->childs[notebook->current].widget); notebook->current = index; - rtgui_widget_show(¬ebook->childs[notebook->current]); - rtgui_widget_update(¬ebook->childs[notebook->current]); + rtgui_widget_show(notebook->childs[notebook->current].widget); + rtgui_widget_update(notebook->childs[notebook->current].widget); } } @@ -65,12 +187,22 @@ rtgui_widget_t* rtgui_notebook_get_index(rtgui_notebook_t* notebook, rt_uint16_t { RT_ASSERT(notebook != RT_NULL); if (index < notebook->count) - return ¬ebook->childs[index]; + return notebook->childs[index].widget; return RT_NULL; } rt_bool_t rtgui_notebook_event_handler(struct rtgui_widget* widget, struct rtgui_event* event) { + if (event->type == RTGUI_EVENT_PAINT) + { + _rtgui_notebook_ondraw(RTGUI_NOTEBOOK(widget)); + } + else + { + /* use parent event handler */ + return rtgui_widget_event_handler(widget, event); + } + return RT_FALSE; } diff --git a/components/rtgui/widgets/widget.c b/components/rtgui/widgets/widget.c index b3c5450444..7023065bfa 100644 --- a/components/rtgui/widgets/widget.c +++ b/components/rtgui/widgets/widget.c @@ -111,7 +111,7 @@ 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) +void rtgui_widget_set_rect(rtgui_widget_t* widget, const rtgui_rect_t* rect) { if (widget == RT_NULL || rect == RT_NULL) return; -- GitLab