diff options
| author | [email protected] <[email protected]> | 2010-12-12 23:40:04 +0000 |
|---|---|---|
| committer | [email protected] <[email protected]> | 2010-12-12 23:40:04 +0000 |
| commit | 5d0786513e57042f01b43e4531ec4e15a1bcc256 (patch) | |
| tree | 4c01d57d000409bc208c774c70c42f6fd60e88aa | |
| parent | 3ba6d887daed668c7296359c0ca8207771b33832 (diff) | |
update combobox widget; add listctrl.
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1193 bbd45198-f89e-11dd-88c7-29a3b14d5316
| -rw-r--r-- | components/rtgui/include/rtgui/widgets/combobox.h | 20 | ||||
| -rw-r--r-- | components/rtgui/include/rtgui/widgets/listctrl.h | 64 | ||||
| -rw-r--r-- | components/rtgui/widgets/combobox.c | 95 | ||||
| -rw-r--r-- | components/rtgui/widgets/listbox.c | 3 | ||||
| -rw-r--r-- | components/rtgui/widgets/listctrl.c | 324 |
5 files changed, 473 insertions, 33 deletions
diff --git a/components/rtgui/include/rtgui/widgets/combobox.h b/components/rtgui/include/rtgui/widgets/combobox.h index 517b125d06..36895dc1a7 100644 --- a/components/rtgui/include/rtgui/widgets/combobox.h +++ b/components/rtgui/include/rtgui/widgets/combobox.h @@ -4,13 +4,7 @@ #include <rtgui/rtgui.h>
#include <rtgui/image.h>
#include <rtgui/widgets/window.h>
-
-/* combobox item */
-struct rtgui_combobox_item
-{
- char* name;
- rtgui_image_t *image;
-};
+#include <rtgui/widgets/listbox.h>
/** Gets the type of a combobox */
#define RTGUI_COMBOBOX_TYPE (rtgui_combobox_type_get())
@@ -32,24 +26,24 @@ struct rtgui_combobox /* pull down window */
struct rtgui_win* pd_win;
rt_bool_t pd_pressed;
- rt_uint16_t pd_win_width;
- rt_uint16_t pd_win_height;
/* combobox items */
- struct rtgui_combobox_item* items;
+ struct rtgui_listbox_item* items;
rt_uint16_t items_count;
rt_uint16_t current_item;
/* call back */
- rt_bool_t (*on_selected) (struct rtgui_widget* widget, struct rtgui_event* event);
+ void (*on_selected) (struct rtgui_widget* widget, struct rtgui_event* event);
};
typedef struct rtgui_combobox rtgui_combobox_t;
rtgui_type_t *rtgui_combobox_type_get(void);
-rtgui_combobox_t *rtgui_combobox_create();
+rtgui_combobox_t *rtgui_combobox_create(struct rtgui_listbox_item* items, rt_uint16_t counter, struct rtgui_rect* rect);
void rtgui_combobox_destroy(rtgui_combobox_t* box);
rt_bool_t rtgui_combobox_event_handler(struct rtgui_widget* widget, struct rtgui_event* event);
-struct rtgui_combobox_item* rtgui_combox_get_select(struct rtgui_combobox* box);
+struct rtgui_listbox_item* rtgui_combox_get_select(struct rtgui_combobox* box);
+
+void rtgui_combobox_set_onselected(struct rtgui_combobox* box, rtgui_onitem_func_t func);
#endif
diff --git a/components/rtgui/include/rtgui/widgets/listctrl.h b/components/rtgui/include/rtgui/widgets/listctrl.h new file mode 100644 index 0000000000..9ce3263d54 --- /dev/null +++ b/components/rtgui/include/rtgui/widgets/listctrl.h @@ -0,0 +1,64 @@ +/*
+ * File : listctrl.h
+ * This file is part of RTGUI in RT-Thread RTOS
+ * COPYRIGHT (C) 2010, 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
+ * 2010-12-06 Bernard first version
+ */
+
+#ifndef __RTGUI_LISTCTRL_H__
+#define __RTGUI_LISTCTRL_H__
+
+#include <rtgui/rtgui.h>
+#include <rtgui/image.h>
+#include <rtgui/rtgui_system.h>
+#include <rtgui/widgets/widget.h>
+
+/** Gets the type of a list ctrl */
+#define RTGUI_LISTCTRL_TYPE (rtgui_listctrl_type_get())
+/** Casts the object to a filelist */
+#define RTGUI_LISTCTRL(obj) (RTGUI_OBJECT_CAST((obj), RTGUI_LISTCTRL_TYPE, rtgui_listctrl_t))
+/** Checks if the object is a filelist ctrl */
+#define RTGUI_IS_LISTCTRL(obj) (RTGUI_OBJECT_CHECK_TYPE((obj), RTGUI_LISTCTRL_TYPE))
+
+struct rtgui_listctrl
+{
+ struct rtgui_widget parent;
+
+ /* widget private data */
+ /* listctrl items */
+ rt_uint32_t items;
+
+ /* total number of items */
+ rt_uint16_t items_count;
+ /* the number of item in a page */
+ rt_uint16_t page_items;
+ /* current item */
+ rt_uint16_t current_item;
+
+ /* item event handler */
+ void (*on_item)(rtgui_widget_t *widget, struct rtgui_event* event);
+ void (*on_item_draw)(struct rtgui_listctrl *list, struct rtgui_dc* dc, rtgui_rect_t* rect, rt_uint16_t index);
+};
+typedef struct rtgui_listctrl rtgui_listctrl_t;
+
+typedef void (*rtgui_onitem_func_t)(struct rtgui_widget* widget, rtgui_event_t *event);
+typedef void (*rtgui_onitem_draw_t)(struct rtgui_listctrl *list, struct rtgui_dc* dc, rtgui_rect_t* rect, rt_uint16_t index);
+
+rtgui_type_t *rtgui_listctrl_type_get(void);
+
+rtgui_listctrl_t* rtgui_listctrl_create(rt_uint32_t items, rt_uint16_t count,
+ rtgui_rect_t *rect, rtgui_onitem_draw_t ondraw);
+void rtgui_listctrl_destroy(rtgui_listctrl_t* ctrl);
+
+rt_bool_t rtgui_listctrl_event_handler(struct rtgui_widget* widget, struct rtgui_event* event);
+void rtgui_listctrl_set_onitem(rtgui_listctrl_t* ctrl, rtgui_onitem_func_t func);
+void rtgui_listctrl_set_items(rtgui_listctrl_t* ctrl, rt_uint32_t items, rt_uint16_t count);
+
+#endif
diff --git a/components/rtgui/widgets/combobox.c b/components/rtgui/widgets/combobox.c index fabc9404c5..8613f787cb 100644 --- a/components/rtgui/widgets/combobox.c +++ b/components/rtgui/widgets/combobox.c @@ -1,4 +1,5 @@ #include <rtgui/dc.h>
+#include <rtgui/rtgui_theme.h>
#include <rtgui/widgets/combobox.h>
static rt_bool_t rtgui_combobox_pulldown_hide(struct rtgui_widget* widget, struct rtgui_event* event);
@@ -18,10 +19,7 @@ static void _rtgui_combobox_constructor(rtgui_combobox_t *box) box->pd_pressed = RT_FALSE;
box->current_item = 0;
box->on_selected = RT_NULL;
-
- box->pd_win = rtgui_win_create(RT_NULL, "combo", &rect, RTGUI_WIN_STYLE_NO_TITLE);
- box->pd_win->user_data = (rt_uint32_t)box;
- rtgui_win_set_ondeactivate(RTGUI_WIN(box->pd_win), rtgui_combobox_pulldown_hide);
+ box->pd_win = RT_NULL;
}
static void _rtgui_combobox_destructor(rtgui_combobox_t *box)
@@ -33,6 +31,32 @@ static void _rtgui_combobox_destructor(rtgui_combobox_t *box) box->pd_win = RT_NULL;
}
+void rtgui_combobox_pdwin_onitem(struct rtgui_widget* widget, struct rtgui_event* event)
+{
+ rtgui_win_t* pd_win;
+ rtgui_combobox_t* combo;
+ rtgui_listbox_t* list;
+
+ list = RTGUI_LISTBOX(widget);
+ pd_win = RTGUI_WIN(rtgui_widget_get_toplevel(widget));
+ combo = RTGUI_COMBOBOX(pd_win->user_data);
+ combo->current_item = list->current_item;
+
+ if (combo->on_selected != RT_NULL)
+ combo->on_selected(RTGUI_WIDGET(combo), RT_NULL);
+
+ rtgui_win_hiden(pd_win);
+ rtgui_widget_update(RTGUI_WIDGET(combo));
+
+ return ;
+}
+
+rt_bool_t rtgui_combobox_pdwin_ondeactive(struct rtgui_widget* widget, struct rtgui_event* event)
+{
+ rtgui_win_hiden(RTGUI_WIN(widget));
+ return RT_TRUE;
+}
+
rtgui_type_t *rtgui_combobox_type_get(void)
{
static rtgui_type_t *combobox_type = RT_NULL;
@@ -48,11 +72,16 @@ rtgui_type_t *rtgui_combobox_type_get(void) return combobox_type;
}
-rtgui_combobox_t *rtgui_combobox_create()
+rtgui_combobox_t *rtgui_combobox_create(struct rtgui_listbox_item* items, rt_uint16_t count, struct rtgui_rect* rect)
{
- rtgui_combobox_t* box;
+ rtgui_combobox_t *box;
box = (rtgui_combobox_t*)rtgui_widget_create(RTGUI_COMBOBOX_TYPE);
+ box->items_count = count;
+ box->items = items;
+ rtgui_widget_set_rect(RTGUI_WIDGET(box), rect);
+
+ box->pd_win = RT_NULL;
return box;
}
@@ -65,18 +94,23 @@ void rtgui_combobox_destroy(rtgui_combobox_t* box) static void rtgui_combobox_ondraw(struct rtgui_combobox* box)
{
/* draw button */
+ rtgui_color_t bc;
struct rtgui_dc* dc;
- struct rtgui_rect rect;
+ struct rtgui_rect rect, r;
/* begin drawing */
dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(box));
if (dc == RT_NULL) return;
+ bc = RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(box));
+
/* get widget rect */
rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect);
+ RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(box)) = white;
+
/* fill widget rect with background color */
rtgui_dc_fill_rect(dc, &rect);
- rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_RAISE);
+ rtgui_dc_draw_rect(dc, &rect);
/* draw current item */
if (box->current_item < box->items_count)
@@ -85,12 +119,19 @@ static void rtgui_combobox_ondraw(struct rtgui_combobox* box) rtgui_dc_draw_text(dc, box->items[box->current_item].name, &rect);
}
+ /* restore background color */
+ RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(box)) = bc;
+
/* draw pull down button */
rect.x1 = rect.x2 - RTGUI_COMBOBOX_BUTTON_WIDTH;
rtgui_rect_inflate(&rect, -1);
+ rtgui_dc_fill_rect(dc, &rect);
if (box->pd_pressed == RT_TRUE) rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_SUNKEN);
else rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_RAISE);
- rtgui_dc_draw_byte(dc, rect.x1, rect.y1, rect.y2, down_arrow);
+
+ r.x1 = 0; r.y1 = 0; r.x2 = 8; r.y2 = 4;
+ rtgui_rect_moveto_align(&rect, &r, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL);
+ rtgui_dc_draw_byte(dc, r.x1, r.y1, 4, down_arrow);
/* end drawing */
rtgui_dc_end_drawing(dc);
@@ -122,20 +163,31 @@ static rt_bool_t rtgui_combobox_onmouse_button(struct rtgui_combobox* box, struc rtgui_widget_update(RTGUI_WIDGET(box));
/* pop pull down window */
- if (box->pd_win != RT_NULL)
+ if (box->pd_win == RT_NULL)
{
- struct rtgui_rect r;
+ rtgui_listbox_t *list;
- r.x1 = RTGUI_WIDGET(box)->extent.x1;
- r.y1 = RTGUI_WIDGET(box)->extent.y2 + 2;
- r.x2 = RTGUI_WIDGET(box)->extent.x2;
- r.y2 = r.y1 + box->pd_win_height;
+ /* create pull down window */
+ rect = RTGUI_WIDGET(box)->extent;
+ rect.y1 = rect.y2;
+ rect.y2 = rect.y1 + 5 * (2 + rtgui_theme_get_selected_height());
+ box->pd_win = rtgui_win_create(RT_NULL, "combo", &rect, RTGUI_WIN_STYLE_NO_TITLE);
+ rtgui_win_set_ondeactivate(RTGUI_WIN(box->pd_win), rtgui_combobox_pulldown_hide);
+ /* set user data to parent combobox */
+ box->pd_win->user_data = (rt_uint32_t)box;
- rtgui_win_set_rect(RTGUI_WIN(box->pd_win), &r);
+ /* create list box */
+ rtgui_rect_inflate(&rect, -1);
+ list = rtgui_listbox_create(box->items, box->items_count, &rect);
+ rtgui_container_add_child(RTGUI_CONTAINER(box->pd_win), RTGUI_WIDGET(list));
+ rtgui_widget_focus(RTGUI_WIDGET(list));
- /* show combo box pull down window */
- rtgui_win_show(RTGUI_WIN(box->pd_win), RT_FALSE);
+ rtgui_listbox_set_onitem(list, rtgui_combobox_pdwin_onitem);
+ rtgui_win_set_ondeactivate(box->pd_win, rtgui_combobox_pdwin_ondeactive);
}
+
+ /* show combo box pull down window */
+ rtgui_win_show(RTGUI_WIN(box->pd_win), RT_FALSE);
}
return RT_TRUE;
@@ -203,7 +255,7 @@ static rt_bool_t rtgui_combobox_pulldown_hide(struct rtgui_widget* widget, struc return RT_TRUE;
}
-struct rtgui_combobox_item* rtgui_combox_get_select(struct rtgui_combobox* box)
+struct rtgui_listbox_item* rtgui_combox_get_select(struct rtgui_combobox* box)
{
if ((box != RT_NULL) && (box->current_item < box->items_count))
{
@@ -212,3 +264,8 @@ struct rtgui_combobox_item* rtgui_combox_get_select(struct rtgui_combobox* box) return RT_NULL;
}
+
+void rtgui_combobox_set_onselected(struct rtgui_combobox* box, rtgui_onitem_func_t func)
+{
+ box->on_selected = func;
+}
diff --git a/components/rtgui/widgets/listbox.c b/components/rtgui/widgets/listbox.c index 62e550f958..53921d9032 100644 --- a/components/rtgui/widgets/listbox.c +++ b/components/rtgui/widgets/listbox.c @@ -26,7 +26,7 @@ static void _rtgui_listbox_constructor(struct rtgui_listbox *box) box->current_item = 0;
box->items_count = 0;
- box->page_items = 0;
+ box->page_items = 1;
box->on_item = 0;
RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(box)) = white;
@@ -317,6 +317,7 @@ rtgui_listbox_t* rtgui_listbox_create(const struct rtgui_listbox_item* items, rt box->items_count = count;
box->page_items = rtgui_rect_height(*rect) / (2 + rtgui_theme_get_selected_height());
+ if (box->page_items == 0) box->page_items = 1;
rtgui_widget_set_rect(RTGUI_WIDGET(box), rect);
}
diff --git a/components/rtgui/widgets/listctrl.c b/components/rtgui/widgets/listctrl.c new file mode 100644 index 0000000000..943f5793fb --- /dev/null +++ b/components/rtgui/widgets/listctrl.c @@ -0,0 +1,324 @@ +/*
+ * File : listctrl.c
+ * This file is part of RTGUI in RT-Thread RTOS
+ * COPYRIGHT (C) 2010, 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
+ * 2010-01-06 Bernard first version
+ */
+
+#include <rtgui/rtgui_theme.h>
+#include <rtgui/widgets/listctrl.h>
+
+static void _rtgui_listctrl_constructor(struct rtgui_listctrl *ctrl)
+{
+ /* set default widget rect and set event handler */
+ rtgui_widget_set_event_handler(RTGUI_WIDGET(ctrl),rtgui_listctrl_event_handler);
+
+ RTGUI_WIDGET(ctrl)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
+
+ ctrl->current_item = 0;
+ ctrl->items_count = 0;
+ ctrl->page_items = 0;
+ ctrl->on_item = 0;
+ ctrl->on_item_draw = RT_NULL;
+
+ RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(ctrl)) = white;
+ RTGUI_WIDGET_TEXTALIGN(RTGUI_WIDGET(ctrl)) = RTGUI_ALIGN_CENTER_VERTICAL;
+}
+
+rtgui_type_t *rtgui_listctrl_type_get(void)
+{
+ static rtgui_type_t *listctrl_type = RT_NULL;
+
+ if (!listctrl_type)
+ {
+ listctrl_type = rtgui_type_create("listctrl", RTGUI_WIDGET_TYPE,
+ sizeof(rtgui_listctrl_t), RTGUI_CONSTRUCTOR(_rtgui_listctrl_constructor), RT_NULL);
+ }
+
+ return listctrl_type;
+}
+
+void rtgui_listctrl_ondraw(struct rtgui_listctrl* ctrl)
+{
+ struct rtgui_rect rect, item_rect;
+ struct rtgui_dc* dc;
+ rt_uint16_t page_index, index;
+
+ dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(ctrl));
+ if (dc == RT_NULL) return;
+
+ rtgui_widget_get_rect(RTGUI_WIDGET(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;
+ item_rect.x1 += 1; item_rect.x2 -= 1;
+ item_rect.y1 += 2;
+ item_rect.y2 = item_rect.y1 + (2 + rtgui_theme_get_selected_height());
+
+ /* get current page */
+ page_index = (ctrl->current_item / ctrl->page_items) * ctrl->page_items;
+ for (index = 0; index < ctrl->page_items; index ++)
+ {
+ if (page_index + index >= ctrl->items_count) break;
+
+ if (page_index + index == ctrl->current_item)
+ {
+ rtgui_theme_draw_selected(dc, &item_rect);
+ }
+
+ if (ctrl->on_item_draw != RT_NULL)
+ {
+ ctrl->on_item_draw(ctrl, dc, &item_rect, page_index + index);
+ }
+
+ /* move to next item position */
+ item_rect.y1 += (rtgui_theme_get_selected_height() + 2);
+ item_rect.y2 += (rtgui_theme_get_selected_height() + 2);
+ }
+ rtgui_dc_end_drawing(dc);
+}
+
+void rtgui_listctrl_update_current(struct rtgui_listctrl* ctrl, rt_uint16_t old_item)
+{
+ struct rtgui_dc* dc;
+ rtgui_rect_t rect, item_rect;
+
+ if (old_item/ctrl->page_items != ctrl->current_item/ctrl->page_items)
+ {
+ /* it's not a same page, update all */
+ rtgui_widget_update(RTGUI_WIDGET(ctrl));
+ return;
+ }
+
+ dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(ctrl));
+ if (dc == RT_NULL) return;
+
+ rtgui_widget_get_rect(RTGUI_WIDGET(ctrl), &rect);
+ rect.x2 -= 1; rect.y2 -= 1;
+
+ item_rect = rect;
+ /* get old item's rect */
+ item_rect.x1 += 1; item_rect.x2 -= 1;
+ item_rect.y1 += 2;
+ item_rect.y1 += (old_item % ctrl->page_items) * (2 + rtgui_theme_get_selected_height());
+ item_rect.y2 = item_rect.y1 + (2 + rtgui_theme_get_selected_height());
+
+ /* draw old item */
+ rtgui_dc_fill_rect(dc, &item_rect);
+ if (ctrl->on_item_draw != RT_NULL)
+ ctrl->on_item_draw(ctrl, dc, &item_rect, old_item);
+
+ /* draw current item */
+ item_rect = rect;
+ /* get current item's rect */
+ item_rect.x1 += 1; item_rect.x2 -= 1;
+ item_rect.y1 += 2;
+ item_rect.y1 += (ctrl->current_item % ctrl->page_items) * (2 + rtgui_theme_get_selected_height());
+ item_rect.y2 = item_rect.y1 + (2 + rtgui_theme_get_selected_height());
+
+ /* draw current item */
+ rtgui_theme_draw_selected(dc, &item_rect);
+ if (ctrl->on_item_draw != RT_NULL)
+ ctrl->on_item_draw(ctrl, dc, &item_rect, ctrl->current_item);
+
+ rtgui_dc_end_drawing(dc);
+}
+
+rt_bool_t rtgui_listctrl_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
+{
+ struct rtgui_listctrl* ctrl = RT_NULL;
+
+ ctrl = RTGUI_LISTCTRL(widget);
+ switch (event->type)
+ {
+ case RTGUI_EVENT_PAINT:
+ rtgui_listctrl_ondraw(ctrl);
+ return RT_FALSE;
+
+ case RTGUI_EVENT_RESIZE:
+ {
+ struct rtgui_event_resize* resize;
+
+ resize = (struct rtgui_event_resize*)event;
+
+ /* recalculate page items */
+ ctrl->page_items = resize->h / (2 + rtgui_theme_get_selected_height());
+ }
+ break;
+
+ case RTGUI_EVENT_MOUSE_BUTTON:
+ {
+ rtgui_rect_t rect;
+ struct rtgui_event_mouse* emouse;
+
+ emouse = (struct rtgui_event_mouse*)event;
+
+ /* calculate selected item */
+
+ /* get physical extent information */
+ rtgui_widget_get_rect(widget, &rect);
+ rtgui_widget_rect_to_device(widget, &rect);
+
+ if ((rtgui_rect_contains_point(&rect, emouse->x, emouse->y) == RT_EOK) && (ctrl->items_count > 0))
+ {
+ rt_uint16_t index;
+ index = (emouse->y - rect.y1) / (2 + rtgui_theme_get_selected_height());
+
+ /* set focus */
+ rtgui_widget_focus(widget);
+ {
+ struct rtgui_rect rect;
+ struct rtgui_dc* dc;
+
+ dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(ctrl));
+ if (dc != RT_NULL)
+ {
+ /* get widget rect */
+ 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);
+ }
+ }
+
+ if ((index < ctrl->items_count) && (index < ctrl->page_items))
+ {
+ rt_uint16_t old_item;
+
+ old_item = ctrl->current_item;
+
+ /* set selected item */
+ ctrl->current_item = (ctrl->current_item/ctrl->page_items) * ctrl->page_items + index;
+ if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN)
+ {
+ /* down event */
+ rtgui_listctrl_update_current(ctrl, old_item);
+ }
+ else
+ {
+ /* up event */
+ if (ctrl->on_item != RT_NULL)
+ {
+ ctrl->on_item(RTGUI_WIDGET(ctrl), RT_NULL);
+ }
+ }
+ }
+ }
+
+ return RT_TRUE;
+ }
+
+ case RTGUI_EVENT_KBD:
+ {
+ struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*)event;
+ if ((ekbd->type == RTGUI_KEYDOWN) && (ctrl->items_count > 0))
+ {
+ rt_uint16_t old_item;
+
+ old_item = ctrl->current_item;
+ switch (ekbd->key)
+ {
+ case RTGUIK_LEFT:
+ if (ctrl->current_item - ctrl->page_items >= 0)
+ ctrl->current_item -= ctrl->page_items;
+ rtgui_listctrl_update_current(ctrl, old_item);
+ return RT_FALSE;
+
+ case RTGUIK_UP:
+ if (ctrl->current_item > 0)
+ ctrl->current_item --;
+ rtgui_listctrl_update_current(ctrl, old_item);
+ return RT_FALSE;
+
+ case RTGUIK_RIGHT:
+ if (ctrl->current_item + ctrl->page_items < ctrl->items_count - 1)
+ ctrl->current_item += ctrl->page_items;
+ rtgui_listctrl_update_current(ctrl, old_item);
+ return RT_FALSE;
+
+ case RTGUIK_DOWN:
+ if (ctrl->current_item < ctrl->items_count - 1)
+ ctrl->current_item ++;
+ rtgui_listctrl_update_current(ctrl, old_item);
+ return RT_FALSE;
+
+ case RTGUIK_RETURN:
+ if (ctrl->on_item != RT_NULL)
+ {
+ ctrl->on_item(RTGUI_WIDGET(ctrl), RT_NULL);
+ }
+ return RT_FALSE;
+
+ default:
+ break;
+ }
+ }
+ }
+ return RT_FALSE;
+ }
+
+ /* use ctrl event handler */
+ return rtgui_widget_event_handler(widget, event);
+}
+
+rtgui_listctrl_t* rtgui_listctrl_create(rt_uint32_t items, rt_uint16_t count, rtgui_rect_t *rect,
+ rtgui_onitem_draw_t ondraw)
+{
+ struct rtgui_listctrl* ctrl = RT_NULL;
+
+ ctrl = (struct rtgui_listctrl*) rtgui_widget_create(RTGUI_LISTCTRL_TYPE);
+ if (ctrl != RT_NULL)
+ {
+ ctrl->items = items;
+ ctrl->items_count = count;
+ ctrl->on_item_draw = ondraw;
+
+ ctrl->page_items = rtgui_rect_height(*rect) / (2 + rtgui_theme_get_selected_height());
+ rtgui_widget_set_rect(RTGUI_WIDGET(ctrl), rect);
+ }
+
+ return ctrl;
+}
+
+void rtgui_listctrl_destroy(rtgui_listctrl_t* ctrl)
+{
+ /* destroy ctrl */
+ rtgui_widget_destroy(RTGUI_WIDGET(ctrl));
+}
+
+void rtgui_listctrl_set_onitem(rtgui_listctrl_t* ctrl, rtgui_onitem_func_t func)
+{
+ RT_ASSERT(ctrl != RT_NULL);
+
+ ctrl->on_item = func;
+}
+
+void rtgui_listctrl_set_items(rtgui_listctrl_t* ctrl, rt_uint32_t items, rt_uint16_t count)
+{
+ rtgui_rect_t rect;
+
+ ctrl->items = items;
+ ctrl->items_count = count;
+ ctrl->current_item = 0;
+
+ rtgui_widget_get_rect(RTGUI_WIDGET(ctrl), &rect);
+ ctrl->page_items = rtgui_rect_height(rect) / (2 + rtgui_theme_get_selected_height());
+
+ rtgui_widget_update(RTGUI_WIDGET(ctrl));
+}
|
