update notebook control; fix rtgui_widget_hide issue.

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1226 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
bernard.xiong@gmail.com
2010-12-28 00:06:05 +00:00
parent 04727cb1db
commit 97cc2b9e4c
7 changed files with 730 additions and 479 deletions

View File

@@ -3,11 +3,18 @@
#include <rtgui/rtgui_system.h>
#include <rtgui/widgets/notebook.h>
static void _rtgui_notebook_get_bar_rect(rtgui_notebook_t *notebook, struct rtgui_rect* rect);
static void _rtgui_notebook_get_page_rect(rtgui_notebook_t *notebook, struct rtgui_rect* rect);
static void _rtgui_notebook_constructor(rtgui_notebook_t *notebook)
{
notebook->flag = 0;
notebook->childs = RT_NULL;
notebook->count = 0;
notebook->current = RTGUI_NOT_FOUND;
RTGUI_WIDGET(notebook)->gc.textalign = RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL;
rtgui_widget_set_event_handler(RTGUI_WIDGET(notebook), rtgui_notebook_event_handler);
}
static void _rtgui_notebook_destructor(rtgui_notebook_t *notebook)
@@ -43,9 +50,21 @@ static void _rtgui_notebook_ondraw(rtgui_notebook_t *notebook)
}
else
{
if (notebook->current == RTGUI_NOT_FOUND)
notebook->current = 0;
_rtgui_notebook_get_bar_rect(notebook, &rect);
rtgui_dc_fill_rect(dc, &rect);
rect.x2 = rect.x1 + 80;
/* draw tab bar */
for (index = 0; index < notebook->count; index ++)
{
if (notebook->current == index)
rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_SUNKEN);
else
rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_BOX);
rtgui_dc_draw_text(dc, notebook->childs[index].title, &rect);
rect.x1 += 80; rect.x2 += 80;
}
/* draw current tab */
@@ -54,13 +73,87 @@ static void _rtgui_notebook_ondraw(rtgui_notebook_t *notebook)
rtgui_dc_end_drawing(dc);
}
static void _rtgui_notebook_onmouse(rtgui_notebook_t *notebook, struct rtgui_event_mouse* emouse)
{
rtgui_rect_t rect;
/* handle notebook bar */
_rtgui_notebook_get_bar_rect(notebook, &rect);
rtgui_widget_rect_to_device(RTGUI_WIDGET(notebook), &rect);
if (rtgui_rect_contains_point(&rect, emouse->x, emouse->y) == RT_EOK)
{
int index;
struct rtgui_dc* dc;
index = (emouse->x - rect.x1) / 80;
if (index < notebook->count)
{
/* update tab bar */
dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(notebook));
if (dc == RT_NULL) return;
rtgui_notebook_set_current_by_index(notebook, index);
_rtgui_notebook_get_bar_rect(notebook, &rect);
rtgui_dc_fill_rect(dc, &rect);
rect.x2 = rect.x1 + 80;
/* draw tab bar */
for (index = 0; index < notebook->count; index ++)
{
if (notebook->current == index)
rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_SUNKEN);
else
rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_BOX);
rtgui_dc_draw_text(dc, notebook->childs[index].title, &rect);
rect.x1 += 80; rect.x2 += 80;
}
rtgui_dc_end_drawing(dc);
return;
}
}
/* handle on page */
if (notebook->childs[notebook->current].widget->event_handler != RT_NULL)
notebook->childs[notebook->current].widget->event_handler(notebook->childs[notebook->current].widget,
&(emouse->parent));
}
static void _rtgui_notebook_get_page_rect(rtgui_notebook_t *notebook, struct rtgui_rect* rect)
{
RT_ASSERT(notebook != RT_NULL);
RT_ASSERT(rect != RT_NULL);
rtgui_widget_get_rect(RTGUI_WIDGET(notebook), rect);
if (notebook->flag == RTGUI_NOTEBOOK_NOTAB) return;
else if (notebook->flag == RTGUI_NOTEBOOK_TOP)
rect->y1 = rect->y1 + 25;
else if (notebook->flag == RTGUI_NOTEBOOK_BOTTOM)
rect->y2 = rect->y2 - 25;
}
static void _rtgui_notebook_get_bar_rect(rtgui_notebook_t *notebook, struct rtgui_rect* rect)
{
RT_ASSERT(notebook != RT_NULL);
RT_ASSERT(rect != RT_NULL);
rtgui_widget_get_rect(RTGUI_WIDGET(notebook), rect);
if (notebook->flag == RTGUI_NOTEBOOK_NOTAB) return;
else if (notebook->flag == RTGUI_NOTEBOOK_TOP)
rect->y2 = rect->y1 + 25;
else if (notebook->flag == RTGUI_NOTEBOOK_BOTTOM)
rect->y1 = rect->y2 - 25;
}
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,
noteboot_type = rtgui_type_create("notebook", RTGUI_CONTAINER_TYPE,
sizeof(rtgui_notebook_t),
RTGUI_CONSTRUCTOR(_rtgui_notebook_constructor),
RTGUI_DESTRUCTOR(_rtgui_notebook_destructor));
@@ -69,16 +162,20 @@ rtgui_type_t *rtgui_notebook_type_get(void)
return noteboot_type;
}
rtgui_notebook_t* rtgui_notebook_create(const rtgui_rect_t* rect)
rtgui_notebook_tab_t *tabs;
struct rtgui_notebook *_notebook;
rtgui_notebook_t* rtgui_notebook_create(const rtgui_rect_t* rect, rt_uint8_t style)
{
struct rtgui_notebook* notebook;
notebook = (struct rtgui_notebook*) rtgui_widget_create (RTGUI_NOTEBOOK_TYPE);
if (notebook != RT_NULL)
{
notebook->flag = style;
rtgui_widget_set_rect(RTGUI_WIDGET(notebook), rect);
}
_notebook = notebook;
return notebook;
}
@@ -89,6 +186,7 @@ void rtgui_notebook_destroy(rtgui_notebook_t* notebook)
void rtgui_notebook_add(rtgui_notebook_t* notebook, const char* label, rtgui_widget_t* child)
{
rtgui_rect_t rect;
RT_ASSERT(notebook != RT_NULL);
notebook->count += 1;
@@ -97,6 +195,15 @@ void rtgui_notebook_add(rtgui_notebook_t* notebook, const char* label, rtgui_wid
notebook->childs[notebook->count - 1].title = rt_strdup(label);
notebook->childs[notebook->count - 1].widget = child;
tabs = notebook->childs;
/* set parent */
rtgui_widget_set_parent(child, RTGUI_WIDGET(notebook));
_rtgui_notebook_get_page_rect(notebook, &rect);
rtgui_widget_rect_to_device(RTGUI_WIDGET(notebook), &rect);
rtgui_widget_set_rect(child, &rect);
}
void rtgui_notebook_remove(rtgui_notebook_t* notebook, rt_uint16_t index)
@@ -176,7 +283,7 @@ void rtgui_notebook_set_current_by_index(rtgui_notebook_t* notebook, rt_uint16_t
{
if (notebook->current != RTGUI_NOT_FOUND)
rtgui_widget_hide(notebook->childs[notebook->current].widget);
notebook->current = index;
rtgui_widget_show(notebook->childs[notebook->current].widget);
rtgui_widget_update(notebook->childs[notebook->current].widget);
@@ -194,9 +301,25 @@ rtgui_widget_t* rtgui_notebook_get_index(rtgui_notebook_t* notebook, rt_uint16_t
rt_bool_t rtgui_notebook_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
{
struct rtgui_notebook* notebook;
notebook = RTGUI_NOTEBOOK(widget);
if (event->type == RTGUI_EVENT_PAINT)
{
_rtgui_notebook_ondraw(RTGUI_NOTEBOOK(widget));
_rtgui_notebook_ondraw(notebook);
}
else if (event->type == RTGUI_EVENT_MOUSE_BUTTON)
{
_rtgui_notebook_onmouse(notebook, (struct rtgui_event_mouse*)event);
}
else if (event->type == RTGUI_EVENT_KBD)
{
if (notebook->current != RTGUI_NOT_FOUND)
{
if (notebook->childs[notebook->current].widget->event_handler != RT_NULL)
return notebook->childs[notebook->current].widget->event_handler(notebook->childs[notebook->current].widget,
event);
}
}
else
{