update menu control.

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1207 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
bernard.xiong@gmail.com 2010-12-17 02:39:06 +00:00
parent 7eee3d5e82
commit 6565e9511b
2 changed files with 85 additions and 127 deletions

View File

@ -1,6 +1,6 @@
#ifndef __RTGUI_MENU_H__
#define __RTGUI_MENU_H__
:q
#include <rtgui/image.h>
#include <rtgui/widgets/label.h>
#include <rtgui/widgets/window.h>
@ -23,14 +23,24 @@ struct rtgui_menu_item
const char* label;
/* menu image */
rtgui_image_t *image;
/* parent menu */
struct rtgui_menu *parent_menu, *sub_menu;
/* sub-menu item */
const struct rtgui_menu_item_t *submenu;
rt_uint16_t submenu_count;
/* menu action */
rt_bool_t (*on_menuaction)(rtgui_widget_t* widget, rtgui_event_t* event);
};
typedef struct rtgui_menu_item rtgui_menu_item_t;
rtgui_menu_item_t items[] =
{
{RTGUI_ITEM_NORMAL, "item #1", RT_NULL, RT_NULL, 0, RT_NULL},
{RTGUI_ITEM_NORMAL, "item #2", RT_NULL, RT_NULL, 0, RT_NULL},
{RTGUI_ITEM_SEPARATOR, RT_NULL, RT_NULL, RT_NULL, 0, RT_NULL},
{RTGUI_ITEM_NORMAL, "item #3", RT_NULL, RT_NULL, 0, RT_NULL},
};
/** Gets the type of a menu */
#define RTGUI_MENU_TYPE (rtgui_menu_type_get())
/** Casts the object to an rtgui_menu */
@ -39,9 +49,6 @@ typedef struct rtgui_menu_item rtgui_menu_item_t;
#define RTGUI_IS_MENU(obj) (RTGUI_OBJECT_CHECK_TYPE((obj), RTGUI_MENU_TYPE))
#define RTGUI_MENU_DEFAULT_WIDTH 100
struct rtgui_menu_item;
struct rtgui_menu
{
/* inherited from window */
@ -52,8 +59,10 @@ struct rtgui_menu
rt_uint16_t items_count;
rt_uint16_t current_item;
/* parent item */
struct rtgui_menu_item *parent_item;
/* parent menu */
struct rtgui_menu *parent_menu;
/* menu item list control */
struct rtgui_listctrl *items_list;
/* pop event handle */
rt_bool_t (*on_menupop)(rtgui_widget_t* widget, rtgui_event_t* event);
@ -63,17 +72,14 @@ typedef struct rtgui_menu rtgui_menu_t;
rtgui_type_t *rtgui_menu_type_get(void);
struct rtgui_menu* rtgui_menu_create(const char* title);
struct rtgui_menu* rtgui_menu_create(const char* title, struct rtgui_menu* parent_menu,
const struct rtgui_menu_item* items, rt_uint16_t count);
void rtgui_menu_destroy(struct rtgui_menu* menu);
void rtgui_menu_set_onmenupop(struct rtgui_menu* menu, rtgui_event_handler_ptr handler);
void rtgui_menu_set_onmenuhide(struct rtgui_menu* menu, rtgui_event_handler_ptr handler);
void rtgui_menu_pop(struct rtgui_menu* menu, int x, int y);
rtgui_menu_item_t* rtgui_menu_item_new(struct rtgui_menu* menu, char* text, int type,
rt_bool_t (*on_menu)(rtgui_widget_t* widget, rtgui_event_t* event));
rtgui_menu_item_t* rtgui_menu_item_new_separator(struct rtgui_menu* menu);
void rtgui_menu_item_add(struct rtgui_menu* menu, struct rtgui_menu_item* item);
struct rtgui_menu_item* rtgui_menu_get_first_item(struct rtgui_menu* menu);
#endif

View File

@ -11,13 +11,6 @@ static rt_bool_t rtgui_menu_on_deactivate(rtgui_widget_t* widget, rtgui_event_t*
static void _rtgui_menu_constructor(rtgui_menu_t *menu)
{
rtgui_box_t* box;
rtgui_rect_t rect = {0, 0, RTGUI_MENU_DEFAULT_WIDTH, RTGUI_BORDER_DEFAULT_WIDTH << 1};
/* set default rect */
menu->width = RTGUI_MENU_DEFAULT_WIDTH;
menu->height = (RTGUI_BORDER_DEFAULT_WIDTH << 1);
rtgui_widget_set_rect(RTGUI_WIDGET(menu), &rect);
/* set window style */
RTGUI_WIN(menu)->style = RTGUI_WIN_STYLE_NO_TITLE;
@ -25,14 +18,52 @@ static void _rtgui_menu_constructor(rtgui_menu_t *menu)
rtgui_win_set_ondeactivate(RTGUI_WIN(menu), rtgui_menu_on_deactivate);
/* set proper of control */
menu->parent_item = RT_NULL;
menu->select_item = RT_NULL;
menu->parent_menu = RT_NULL;
menu->items = RT_NULL;
menu->items_count = 0;
menu->current_item = 0;
menu->items_list = RT_NULL;
menu->on_menupop = RT_NULL;
menu->on_menuhide = RT_NULL;
}
/* create box */
box = rtgui_box_create(RTGUI_VERTICAL, &rect);
rtgui_container_add_child(RTGUI_CONTAINER(menu), RTGUI_WIDGET(box));
static void _rtgui_menu_destructor(rtgui_menu_t* menu)
{
}
static void _rtgui_menu_item_ondraw(struct rtgui_listctrl *list, struct rtgui_dc* dc, rtgui_rect_t* rect, rt_uint16_t index)
{
struct rtgui_menu_item* item;
/* get menu item */
item = (rtgui_menu_item_t*)list->items;
item = &item[index];
if (item->type == RTGUI_ITEM_SUBMENU)
{
rtgui_dc_draw_text(dc, item->label, &rect);
rtgui_dc_draw_byte(dc, left_arraw, &rect);
}
else if (item->type == RTGUI_ITEM_SEPARATOR)
{
rtgui_dc_draw_vline(dc, &rect);
}
else if (item->type == RTGUI_ITEM_CHECK)
{
/* not support right now */
}
else
{
/* normal menu item */
rtgui_dc_draw_text(dc, item->label, &rect);
if (item->image != RT_NULL)
rtgui_image_blit(item->image, dc, &rect);
}
}
static void _rtgui_menu_item_onmouse()
{
}
rtgui_type_t *rtgui_menu_type_get(void)
@ -42,7 +73,9 @@ rtgui_type_t *rtgui_menu_type_get(void)
if (!menu_type)
{
menu_type = rtgui_type_create("menu", RTGUI_WIN_TYPE,
sizeof(rtgui_menu_t), RTGUI_CONSTRUCTOR(_rtgui_menu_constructor), RT_NULL);
sizeof(rtgui_menu_t),
RTGUI_CONSTRUCTOR(_rtgui_menu_constructor),
RTGUI_DESTRUCTOR (_rtgui_menu_destructor));
}
return menu_type;
@ -52,10 +85,8 @@ static rt_bool_t rtgui_menu_on_deactivate(rtgui_widget_t* widget, rtgui_event_t*
{
rtgui_menu_t* menu = (rtgui_menu_t*) widget;
if (menu->select_item != RT_NULL)
{
/* try to wake through to find sub menu activated */
if (menu->select_item->type == RTGUI_ITEM_SUBMENU)
/* submenu is activate */
if (menu->items[menu->current_item].type == RTGUI_ITEM_SUBMEN)
{
/* if sub menu activated, not hide menu */
if (menu->select_item->sub_menu != RT_NULL &&
@ -64,7 +95,6 @@ static rt_bool_t rtgui_menu_on_deactivate(rtgui_widget_t* widget, rtgui_event_t*
return RT_TRUE;
}
}
}
rtgui_win_hiden(RTGUI_WIN(menu));
if (menu->on_menuhide != RT_NULL)
@ -73,29 +103,33 @@ static rt_bool_t rtgui_menu_on_deactivate(rtgui_widget_t* widget, rtgui_event_t*
}
/* if it's a submenu, try to hide parent menu */
if (menu->parent_item != RT_NULL &&
!rtgui_win_is_activated(RTGUI_WIN(menu->parent_item->parent_menu)))
if (menu->parent_menu != RT_NULL &&
!rtgui_win_is_activated(RTGUI_WIN(menu->parent_menu)))
{
rtgui_menu_on_deactivate(RTGUI_WIDGET(menu->parent_item->parent_menu), event);
}
/* unselected all items */
if (menu->select_item != RT_NULL)
{
// rtgui_menu_item_unselect(menu->select_item);
}
return RT_TRUE;
}
struct rtgui_menu* rtgui_menu_create(const char* title)
struct rtgui_menu* rtgui_menu_create(const char* title, struct rtgui_menu* parent_menu,
const struct rtgui_menu_item* items, rt_uint16_t count)
{
rtgui_rect_t rect = {0, 0, 100, 100};
struct rtgui_menu* menu;
menu = (struct rtgui_menu*) rtgui_widget_create ( RTGUI_MENU_TYPE );
if (menu != RT_NULL)
{
rtgui_win_set_title(RTGUI_WIN(menu), title);
menu->parent_menu = parent_menu;
menu->items = items;
menu->items_count = count;
rtgui_widget_set_rect(RTGUI_WIDGET(menu), &rect);
rtgui_rect_inflate(&rect, -1);
/* create menu item list */
menu->items_list = rtgui_listctrl_create(items, count, &rect, _rtgui_menu_item_ondraw);
}
return menu;
@ -134,11 +168,6 @@ void rtgui_menu_pop(struct rtgui_menu* menu, int x, int y)
rect.y2 = y + menu->height;
rtgui_win_set_rect(RTGUI_WIN(menu), &rect);
/* layout box */
box = (rtgui_box_t*) rtgui_container_get_first_child(RTGUI_CONTAINER(menu));
RTGUI_WIDGET(box)->extent = rect;
rtgui_box_layout(box);
/* on menu pop handler */
if (menu->on_menupop != RT_NULL)
{
@ -149,80 +178,3 @@ void rtgui_menu_pop(struct rtgui_menu* menu, int x, int y)
rtgui_win_show(RTGUI_WIN(menu), RT_FALSE);
}
rtgui_menu_item_t* rtgui_menu_item_new(struct rtgui_menu* menu, char* text, int type,
rt_bool_t (*on_menu)(rtgui_widget_t* widget, rtgui_event_t* event))
{
rtgui_menu_item_t* item;
if (menu == RT_NULL) return RT_NULL;
item = rtgui_menu_item_create(text);
if (item == RT_NULL) return RT_NULL;
item->type = type;
item->image = RT_NULL;
item->on_menuaction = on_menu;
rtgui_menu_item_add(menu, item);
return item;
}
rtgui_menu_item_t* rtgui_menu_item_new_separator(struct rtgui_menu* menu)
{
rtgui_menu_item_t* item;
if (menu == RT_NULL) return RT_NULL;
item = rtgui_menu_item_create("--");
if (item == RT_NULL) return RT_NULL;
item->type = RTGUI_ITEM_SEPARATOR;
item->image = RT_NULL;
item->on_menuaction = RT_NULL;
/* resize item extent */
rtgui_widget_set_miniheight(RTGUI_WIDGET(item), 2);
RTGUI_WIDGET(item)->extent.y2 = RTGUI_WIDGET(item)->extent.y1 + 2;
rtgui_menu_item_add(menu, item);
return item;
}
void rtgui_menu_item_add(struct rtgui_menu* menu, struct rtgui_menu_item* item)
{
rtgui_box_t* box;
if (menu == RT_NULL || item == RT_NULL) return;
box = (rtgui_box_t*) rtgui_container_get_first_child(RTGUI_CONTAINER(menu));
menu->height += rtgui_rect_height(RTGUI_WIDGET(item)->extent);
if (menu->width < rtgui_rect_width(RTGUI_WIDGET(item)->extent) + RTGUI_MENU_IMAGE_MAGIN + RTGUI_MENU_SUBMENU_MAGIN)
{
menu->width = rtgui_rect_width(RTGUI_WIDGET(item)->extent) + RTGUI_MENU_IMAGE_MAGIN + RTGUI_MENU_SUBMENU_MAGIN;
}
item->parent_menu = menu;
if (item->type == RTGUI_ITEM_SUBMENU)
{
item->sub_menu->parent_item = item;
}
rtgui_box_append(box, RTGUI_WIDGET(item));
}
struct rtgui_menu_item* rtgui_menu_get_first_item(struct rtgui_menu* menu)
{
rtgui_box_t* box;
rtgui_menu_item_t* item;
if (menu == RT_NULL) return RT_NULL;
box = (rtgui_box_t*) rtgui_container_get_first_child(RTGUI_CONTAINER(menu));
if (box == RT_NULL) return RT_NULL;
item = (rtgui_menu_item_t*) rtgui_container_get_first_child(RTGUI_CONTAINER(box));
return item;
}