2010-12-23 07:57:44 +08:00
|
|
|
#include <rtgui/dc.h>
|
2010-12-22 07:50:25 +08:00
|
|
|
#include <rtgui/rtgui.h>
|
2010-12-23 07:57:44 +08:00
|
|
|
#include <rtgui/rtgui_system.h>
|
2010-12-22 07:50:25 +08:00
|
|
|
#include <rtgui/widgets/notebook.h>
|
|
|
|
|
2011-11-28 14:47:15 +08:00
|
|
|
#define RTGUI_NOTEBOOK_TAB_WIDTH 80
|
|
|
|
|
2010-12-28 08:06:05 +08:00
|
|
|
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);
|
|
|
|
|
2010-12-23 07:57:44 +08:00
|
|
|
static void _rtgui_notebook_constructor(rtgui_notebook_t *notebook)
|
|
|
|
{
|
2010-12-28 08:06:05 +08:00
|
|
|
notebook->flag = 0;
|
2010-12-23 07:57:44 +08:00
|
|
|
notebook->childs = RT_NULL;
|
|
|
|
notebook->count = 0;
|
|
|
|
notebook->current = RTGUI_NOT_FOUND;
|
2010-12-28 08:06:05 +08:00
|
|
|
|
|
|
|
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);
|
2010-12-23 07:57:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-28 15:20:14 +08:00
|
|
|
/* Draw tab bars of @param notebook. @param dc should be initialized and
|
|
|
|
* finished outside this function. Don't pass @param notebook or @param dc as
|
|
|
|
* RT_NULL, it should be checked outside.
|
|
|
|
*/
|
|
|
|
static void _rtgui_notebook_draw_bar(struct rtgui_notebook *notebook,
|
|
|
|
struct rtgui_dc *dc)
|
|
|
|
{
|
|
|
|
struct rtgui_rect rect;
|
|
|
|
int index;
|
|
|
|
|
|
|
|
RT_ASSERT((notebook != RT_NULL) && (dc != RT_NULL));
|
|
|
|
|
|
|
|
_rtgui_notebook_get_bar_rect(notebook, &rect);
|
|
|
|
rtgui_dc_fill_rect(dc, &rect);
|
|
|
|
|
|
|
|
rect.x2 = rect.x1 + RTGUI_NOTEBOOK_TAB_WIDTH;
|
|
|
|
/* 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 += RTGUI_NOTEBOOK_TAB_WIDTH;
|
|
|
|
rect.x2 += RTGUI_NOTEBOOK_TAB_WIDTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-12-23 07:57:44 +08:00
|
|
|
static void _rtgui_notebook_ondraw(rtgui_notebook_t *notebook)
|
|
|
|
{
|
|
|
|
struct rtgui_dc* dc;
|
|
|
|
|
|
|
|
dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(notebook));
|
|
|
|
if (dc == RT_NULL) return;
|
|
|
|
|
|
|
|
if (notebook->count == 0)
|
|
|
|
{
|
2011-11-28 16:01:15 +08:00
|
|
|
rtgui_rect_t rect;
|
|
|
|
rtgui_widget_get_rect(RTGUI_WIDGET(notebook), &rect);
|
2010-12-23 07:57:44 +08:00
|
|
|
rtgui_dc_fill_rect(dc, &rect);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-28 08:06:05 +08:00
|
|
|
if (notebook->current == RTGUI_NOT_FOUND)
|
|
|
|
notebook->current = 0;
|
|
|
|
|
2011-11-28 15:20:14 +08:00
|
|
|
_rtgui_notebook_draw_bar(notebook, dc);
|
2010-12-23 07:57:44 +08:00
|
|
|
|
|
|
|
/* draw current tab */
|
|
|
|
rtgui_widget_update(notebook->childs[notebook->current].widget);
|
|
|
|
}
|
|
|
|
rtgui_dc_end_drawing(dc);
|
|
|
|
}
|
|
|
|
|
2010-12-28 08:06:05 +08:00
|
|
|
static void _rtgui_notebook_onmouse(rtgui_notebook_t *notebook, struct rtgui_event_mouse* emouse)
|
|
|
|
{
|
|
|
|
rtgui_rect_t rect;
|
2011-11-28 15:20:14 +08:00
|
|
|
|
2010-12-28 08:06:05 +08:00
|
|
|
/* 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;
|
|
|
|
|
2011-11-28 14:47:15 +08:00
|
|
|
index = (emouse->x - rect.x1) / RTGUI_NOTEBOOK_TAB_WIDTH;
|
2011-12-02 23:02:25 +08:00
|
|
|
if (index < notebook->count && index != notebook->current)
|
2010-12-28 08:06:05 +08:00
|
|
|
{
|
|
|
|
/* update tab bar */
|
|
|
|
dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(notebook));
|
|
|
|
if (dc == RT_NULL) return;
|
|
|
|
|
|
|
|
rtgui_notebook_set_current_by_index(notebook, index);
|
|
|
|
|
2011-11-28 15:20:14 +08:00
|
|
|
_rtgui_notebook_draw_bar(notebook, dc);
|
2010-12-28 08:06:05 +08:00
|
|
|
|
|
|
|
rtgui_dc_end_drawing(dc);
|
|
|
|
}
|
|
|
|
}
|
2011-12-02 22:58:03 +08:00
|
|
|
else
|
|
|
|
{
|
2010-12-28 08:06:05 +08:00
|
|
|
/* handle on page */
|
2011-12-02 22:58:03 +08:00
|
|
|
if (notebook->childs[notebook->current].widget->event_handler != RT_NULL)
|
|
|
|
notebook->childs[notebook->current].widget->event_handler(
|
|
|
|
notebook->childs[notebook->current].widget,
|
|
|
|
&(emouse->parent));
|
|
|
|
}
|
2010-12-28 08:06:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-05-13 18:53:51 +08:00
|
|
|
DEFINE_CLASS_TYPE(notebook, "notebook",
|
|
|
|
RTGUI_CONTAINER_TYPE,
|
|
|
|
_rtgui_notebook_constructor,
|
|
|
|
_rtgui_notebook_destructor,
|
|
|
|
sizeof(struct rtgui_notebook));
|
2010-12-22 07:50:25 +08:00
|
|
|
|
2010-12-28 08:06:05 +08:00
|
|
|
rtgui_notebook_tab_t *tabs;
|
|
|
|
struct rtgui_notebook *_notebook;
|
|
|
|
rtgui_notebook_t* rtgui_notebook_create(const rtgui_rect_t* rect, rt_uint8_t style)
|
2010-12-22 07:50:25 +08:00
|
|
|
{
|
2010-12-23 07:57:44 +08:00
|
|
|
struct rtgui_notebook* notebook;
|
|
|
|
|
|
|
|
notebook = (struct rtgui_notebook*) rtgui_widget_create (RTGUI_NOTEBOOK_TYPE);
|
|
|
|
if (notebook != RT_NULL)
|
|
|
|
{
|
2010-12-28 08:06:05 +08:00
|
|
|
notebook->flag = style;
|
2010-12-23 07:57:44 +08:00
|
|
|
rtgui_widget_set_rect(RTGUI_WIDGET(notebook), rect);
|
|
|
|
}
|
|
|
|
|
2010-12-28 08:06:05 +08:00
|
|
|
_notebook = notebook;
|
2010-12-23 07:57:44 +08:00
|
|
|
return notebook;
|
2010-12-22 07:50:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void rtgui_notebook_destroy(rtgui_notebook_t* notebook)
|
|
|
|
{
|
2010-12-23 07:57:44 +08:00
|
|
|
rtgui_widget_destroy(RTGUI_WIDGET(notebook));
|
2010-12-22 07:50:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void rtgui_notebook_add(rtgui_notebook_t* notebook, const char* label, rtgui_widget_t* child)
|
|
|
|
{
|
2010-12-28 08:06:05 +08:00
|
|
|
rtgui_rect_t rect;
|
2010-12-23 07:57:44 +08:00
|
|
|
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;
|
2010-12-28 08:06:05 +08:00
|
|
|
|
|
|
|
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);
|
2010-12-23 07:57:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
2010-12-22 07:50:25 +08:00
|
|
|
}
|
|
|
|
|
2010-12-23 07:57:44 +08:00
|
|
|
int rtgui_notebook_get_count(rtgui_notebook_t* notebook)
|
2010-12-22 07:50:25 +08:00
|
|
|
{
|
|
|
|
return notebook->count;
|
|
|
|
}
|
|
|
|
|
|
|
|
rtgui_widget_t* rtgui_notebook_get_current(rtgui_notebook_t* notebook)
|
|
|
|
{
|
|
|
|
RT_ASSERT(notebook != RT_NULL);
|
|
|
|
if (notebook->current != RTGUI_NOT_FOUND)
|
2010-12-23 07:57:44 +08:00
|
|
|
return notebook->childs[notebook->current].widget;
|
2010-12-22 07:50:25 +08:00
|
|
|
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
void rtgui_notebook_set_current(rtgui_notebook_t* notebook, rtgui_widget_t* widget)
|
|
|
|
{
|
|
|
|
rt_int16_t index;
|
|
|
|
|
|
|
|
RT_ASSERT(notebook != RT_NULL);
|
|
|
|
|
|
|
|
for (index = 0; index < notebook->count; index ++)
|
|
|
|
{
|
2010-12-23 07:57:44 +08:00
|
|
|
if (widget == notebook->childs[index].widget)
|
2010-12-22 07:50:25 +08:00
|
|
|
{
|
|
|
|
rtgui_notebook_set_current_by_index(notebook, index);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void rtgui_notebook_set_current_by_index(rtgui_notebook_t* notebook, rt_uint16_t index)
|
|
|
|
{
|
|
|
|
RT_ASSERT(notebook != RT_NULL);
|
|
|
|
|
|
|
|
if ((index < notebook->count) && (notebook->current != index))
|
|
|
|
{
|
|
|
|
if (notebook->current != RTGUI_NOT_FOUND)
|
2010-12-23 07:57:44 +08:00
|
|
|
rtgui_widget_hide(notebook->childs[notebook->current].widget);
|
2010-12-28 08:06:05 +08:00
|
|
|
|
2010-12-22 07:50:25 +08:00
|
|
|
notebook->current = index;
|
2010-12-23 07:57:44 +08:00
|
|
|
rtgui_widget_show(notebook->childs[notebook->current].widget);
|
|
|
|
rtgui_widget_update(notebook->childs[notebook->current].widget);
|
2010-12-22 07:50:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rtgui_widget_t* rtgui_notebook_get_index(rtgui_notebook_t* notebook, rt_uint16_t index)
|
|
|
|
{
|
|
|
|
RT_ASSERT(notebook != RT_NULL);
|
|
|
|
if (index < notebook->count)
|
2010-12-23 07:57:44 +08:00
|
|
|
return notebook->childs[index].widget;
|
2010-12-22 07:50:25 +08:00
|
|
|
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_bool_t rtgui_notebook_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
|
|
|
|
{
|
2010-12-28 08:06:05 +08:00
|
|
|
struct rtgui_notebook* notebook;
|
|
|
|
|
|
|
|
notebook = RTGUI_NOTEBOOK(widget);
|
2010-12-23 07:57:44 +08:00
|
|
|
if (event->type == RTGUI_EVENT_PAINT)
|
|
|
|
{
|
2010-12-28 08:06:05 +08:00
|
|
|
_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);
|
|
|
|
}
|
2010-12-23 07:57:44 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* use parent event handler */
|
|
|
|
return rtgui_widget_event_handler(widget, event);
|
|
|
|
}
|
|
|
|
|
2010-12-22 07:50:25 +08:00
|
|
|
return RT_FALSE;
|
|
|
|
}
|