diff options
| author | [email protected] <[email protected]> | 2010-12-28 00:06:05 +0000 |
|---|---|---|
| committer | [email protected] <[email protected]> | 2010-12-28 00:06:05 +0000 |
| commit | 97cc2b9e4cdc80b883827f6f6326c93c7caf87ca (patch) | |
| tree | a68ae48e5d5dc73ed99d56b023acc9557f108051 /components/rtgui/widgets | |
| parent | 04727cb1db71547ffef44a7897ea9a71e3fb2330 (diff) | |
update notebook control; fix rtgui_widget_hide issue.
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1226 bbd45198-f89e-11dd-88c7-29a3b14d5316
Diffstat (limited to 'components/rtgui/widgets')
| -rw-r--r-- | components/rtgui/widgets/listctrl.c | 101 | ||||
| -rw-r--r-- | components/rtgui/widgets/notebook.c | 131 | ||||
| -rw-r--r-- | components/rtgui/widgets/widget.c | 43 |
3 files changed, 258 insertions, 17 deletions
diff --git a/components/rtgui/widgets/listctrl.c b/components/rtgui/widgets/listctrl.c index 360b2ad892..10087dc8d1 100644 --- a/components/rtgui/widgets/listctrl.c +++ b/components/rtgui/widgets/listctrl.c @@ -15,6 +15,8 @@ #include <rtgui/rtgui_theme.h>
#include <rtgui/widgets/listctrl.h>
+static void rtgui_listctrl_update_current(struct rtgui_listctrl* ctrl, rt_uint16_t old_item);
+
static void _rtgui_listctrl_constructor(struct rtgui_listctrl *ctrl)
{
/* set default widget rect and set event handler */
@@ -45,7 +47,78 @@ rtgui_type_t *rtgui_listctrl_type_get(void) return listctrl_type;
}
-void rtgui_listctrl_ondraw(struct rtgui_listctrl* ctrl)
+static void _rtgui_listctrl_get_rect(struct rtgui_listctrl* ctrl, rtgui_rect_t* rect)
+{
+ rtgui_widget_get_rect(RTGUI_WIDGET(ctrl), rect);
+ if (ctrl->items_count > rtgui_rect_height(*rect)/rtgui_theme_get_selected_height())
+ {
+ rect->x2 = rect->x2 - 8;
+ }
+}
+
+static void _rtgui_listctrl_get_scrollbar_rect(struct rtgui_listctrl* ctrl, rtgui_rect_t* rect)
+{
+ rtgui_widget_get_rect(RTGUI_WIDGET(ctrl), rect);
+ if (ctrl->items_count > rtgui_rect_height(*rect)/rtgui_theme_get_selected_height())
+ {
+ rect->x1 = rect->x2 - 8;
+ }
+ else
+ {
+ /* no scrollbar */
+ rt_memset(rect, 0, sizeof(rtgui_rect_t));
+ }
+}
+
+static void _rtgui_listctrl_scrollbar_ondraw(struct rtgui_listctrl* ctrl, struct rtgui_dc* dc)
+{
+ rtgui_rect_t rect;
+ rt_uint32_t height, y1;
+
+ /* get scrollbar rect */
+ _rtgui_listctrl_get_scrollbar_rect(ctrl, &rect);
+ rtgui_dc_fill_rect(dc, &rect);
+
+ height = rtgui_rect_height(rect);
+
+ height = height / ((ctrl->items_count + (ctrl->page_items - 1))/ctrl->page_items);
+ y1 = (ctrl->current_item / ctrl->page_items) * height;
+
+ rect.y1 = rect.y1 + y1; rect.y2 = rect.y1 + height;
+ rtgui_theme_draw_selected(dc, &rect);
+}
+
+static void _rtgui_listctrl_scrollbar_onmouse(struct rtgui_listctrl* ctrl, struct rtgui_event_mouse* mouse)
+{
+ rtgui_rect_t rect;
+ rt_uint32_t height, y1;
+ rt_uint16_t old_item;
+
+ /* get scrollbar rect */
+ _rtgui_listctrl_get_scrollbar_rect(ctrl, &rect);
+ height = rtgui_rect_height(rect);
+ height = height / ((ctrl->items_count + (ctrl->page_items - 1))/ctrl->page_items);
+ y1 = (ctrl->current_item / ctrl->page_items) * height;
+
+ rect.y1 = rect.y1 + y1; rect.y2 = rect.y1 + height;
+ rtgui_widget_rect_to_device(RTGUI_WIDGET(ctrl), &rect);
+
+ old_item = ctrl->current_item;
+ if (mouse->y < rect.y1)
+ {
+ if (ctrl->current_item - ctrl->page_items >= 0)
+ ctrl->current_item -= ctrl->page_items;
+ rtgui_listctrl_update_current(ctrl, old_item);
+ }
+ else if (mouse->y > rect.y2)
+ {
+ if (ctrl->current_item + ctrl->page_items < ctrl->items_count - 1)
+ ctrl->current_item += ctrl->page_items;
+ rtgui_listctrl_update_current(ctrl, old_item);
+ }
+}
+
+static void _rtgui_listctrl_ondraw(struct rtgui_listctrl* ctrl)
{
struct rtgui_rect rect, item_rect;
struct rtgui_dc* dc;
@@ -54,13 +127,10 @@ void rtgui_listctrl_ondraw(struct rtgui_listctrl* ctrl) dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(ctrl));
if (dc == RT_NULL) return;
- rtgui_widget_get_rect(RTGUI_WIDGET(ctrl), &rect);
+ _rtgui_listctrl_get_rect(ctrl, &rect);
rtgui_dc_fill_rect(dc, &rect);
rect.x2 -= 1; rect.y2 -= 1;
- /* draw focused border */
- // if (RTGUI_WIDGET_IS_FOCUSED(RTGUI_WIDGET(ctrl)))
- // rtgui_dc_draw_focus_rect(dc, &rect);
/* get item base rect */
item_rect = rect;
@@ -88,6 +158,9 @@ void rtgui_listctrl_ondraw(struct rtgui_listctrl* ctrl) item_rect.y1 += (rtgui_theme_get_selected_height() + 2);
item_rect.y2 += (rtgui_theme_get_selected_height() + 2);
}
+
+ /* draw scrollbar */
+ _rtgui_listctrl_scrollbar_ondraw(ctrl, dc);
rtgui_dc_end_drawing(dc);
}
@@ -106,7 +179,7 @@ void rtgui_listctrl_update_current(struct rtgui_listctrl* ctrl, rt_uint16_t old_ dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(ctrl));
if (dc == RT_NULL) return;
- rtgui_widget_get_rect(RTGUI_WIDGET(ctrl), &rect);
+ _rtgui_listctrl_get_rect(ctrl, &rect);
rect.x2 -= 1; rect.y2 -= 1;
item_rect = rect;
@@ -145,7 +218,7 @@ rt_bool_t rtgui_listctrl_event_handler(struct rtgui_widget* widget, struct rtgui switch (event->type)
{
case RTGUI_EVENT_PAINT:
- rtgui_listctrl_ondraw(ctrl);
+ _rtgui_listctrl_ondraw(ctrl);
return RT_FALSE;
case RTGUI_EVENT_RESIZE:
@@ -166,10 +239,19 @@ rt_bool_t rtgui_listctrl_event_handler(struct rtgui_widget* widget, struct rtgui emouse = (struct rtgui_event_mouse*)event;
+ /* get scrollbar rect */
+ _rtgui_listctrl_get_scrollbar_rect(ctrl, &rect);
+ rtgui_widget_rect_to_device(RTGUI_WIDGET(ctrl), &rect);
+ if (rtgui_rect_contains_point(&rect, emouse->x, emouse->y) == RT_EOK)
+ {
+ _rtgui_listctrl_scrollbar_onmouse(ctrl, emouse);
+ return RT_TRUE;
+ }
+
/* calculate selected item */
/* get physical extent information */
- rtgui_widget_get_rect(widget, &rect);
+ _rtgui_listctrl_get_rect(ctrl, &rect);
rtgui_widget_rect_to_device(widget, &rect);
if ((rtgui_rect_contains_point(&rect, emouse->x, emouse->y) == RT_EOK) && (ctrl->items_count > 0))
@@ -190,9 +272,6 @@ rt_bool_t rtgui_listctrl_event_handler(struct rtgui_widget* widget, struct rtgui rtgui_widget_get_rect(RTGUI_WIDGET(ctrl), &rect);
/* update focus border */
rect.x2 -= 1; rect.y2 -= 1;
- /* draw focused border */
- // if (RTGUI_WIDGET_IS_FOCUSED(RTGUI_WIDGET(ctrl)))
- // rtgui_dc_draw_focus_rect(dc, &rect);
rtgui_dc_end_drawing(dc);
}
}
diff --git a/components/rtgui/widgets/notebook.c b/components/rtgui/widgets/notebook.c index 0ab8a375a2..06fc47cddb 100644 --- a/components/rtgui/widgets/notebook.c +++ b/components/rtgui/widgets/notebook.c @@ -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
{
diff --git a/components/rtgui/widgets/widget.c b/components/rtgui/widgets/widget.c index 7023065bfa..ecdabf5bdc 100644 --- a/components/rtgui/widgets/widget.c +++ b/components/rtgui/widgets/widget.c @@ -138,6 +138,19 @@ void rtgui_widget_set_rect(rtgui_widget_t* widget, const rtgui_rect_t* rect) } } +void rtgui_widget_set_parent(rtgui_widget_t* widget, rtgui_widget_t* parent) +{ + /* set parent and toplevel widget */ + widget->parent = parent; + + /* update children toplevel */ + if (parent->toplevel != RT_NULL && + RTGUI_IS_TOPLEVEL(parent->toplevel)) + { + widget->toplevel = rtgui_widget_get_toplevel(parent); + } +} + void rtgui_widget_get_extent(rtgui_widget_t* widget, rtgui_rect_t *rect) { RT_ASSERT(widget != RT_NULL); @@ -488,11 +501,37 @@ void rtgui_widget_show(rtgui_widget_t* widget) void rtgui_widget_hide(rtgui_widget_t* widget) { + rtgui_rect_t rect; + /* hide this widget */ RTGUI_WIDGET_HIDE(widget); - /* update the clip info of widget parent */ - rtgui_widget_update_clip(widget->parent); + if (widget->parent != RT_NULL) + { + int index; + rtgui_widget_t *parent; + rtgui_toplevel_t *toplevel; + + rect = widget->extent; + parent = widget->parent; + /* get the no transparent parent */ + while (parent != RT_NULL && parent->flag & RTGUI_WIDGET_FLAG_TRANSPARENT) + { + parent = parent->parent; + } + + /* union widget rect */ + rtgui_region_union_rect(&(widget->parent->clip), &(widget->parent->clip), &rect); + + /* handle extern rect */ + toplevel = RTGUI_TOPLEVEL(widget->toplevel); + /* subtract the external rect */ + for (index = 0; index < toplevel->external_clip_size; index ++) + { + rtgui_region_subtract_rect(&(widget->parent->clip), &(widget->parent->clip), + &(toplevel->external_clip_rect[index])); + } + } } rtgui_color_t rtgui_widget_get_parent_foreground(rtgui_widget_t* widget) |
