static rt_bool_t _rtgui_menu_onitem(struct rtgui_object* object, struct rtgui_event* event) { struct rtgui_menu* menu; /* event will be NULL, don't check it. */ RT_ASSERT(object); /* get menu */ menu = RTGUI_MENU(rtgui_widget_get_toplevel(RTGUI_WIDGET(object))); if (menu->items[menu->items_list->current_item].type == RTGUI_ITEM_SUBMENU) { const rtgui_menu_item_t* items; rt_uint16_t count; rtgui_rect_t item_rect; items = (rtgui_menu_item_t*)menu->items[menu->items_list->current_item].submenu; count = menu->items[menu->items_list->current_item].submenu_count; if (menu->sub_menu != RT_NULL) { if (menu->sub_menu->items == items) { if (!RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(menu->sub_menu))) { /* hide this sub menu */ rtgui_win_hiden(RTGUI_WIN(menu->sub_menu)); return RT_FALSE; } /* show this sub menu */ rtgui_listctrl_get_item_rect(menu->items_list, menu->items_list->current_item, &item_rect); rtgui_menu_pop(menu->sub_menu, item_rect.x2, item_rect.y1); return RT_FALSE; } /* delete sub menu */ rtgui_menu_destroy(menu->sub_menu); menu->sub_menu = RT_NULL; } /* create sub menu */ menu->sub_menu = rtgui_menu_create("submenu", menu, items, count); rtgui_listctrl_get_item_rect(menu->items_list, menu->items_list->current_item, &item_rect); rtgui_menu_pop(menu->sub_menu, item_rect.x2 + 5, item_rect.y1); } else /* other menu item */ { /* invoke action */ if (menu->items[menu->items_list->current_item].on_menuaction != RT_NULL) menu->items[menu->items_list->current_item].on_menuaction(RTGUI_OBJECT(menu), RT_NULL); /* hide sub-menu */ if (menu->sub_menu != RT_NULL) { rtgui_menu_hiden(menu->sub_menu); } rtgui_menu_hiden(menu); } return RT_FALSE; }
/* 创建用于演示menu控件的视图 */ rtgui_view_t* demo_gui_menu(rtgui_view_t* parent_view) { rtgui_view_t *view; rtgui_menu_t *main_menu, *sub_menu, *toolmenu; /* 先创建一个演示用的视图 */ view = demo_view_create(parent_view, "MENU View"); /* 1.使用动态方式创建菜单 */ main_menu = rtgui_menu_create(view, "menu", 5, 40, RTGUI_MENU_NORMAL); sub_menu = rtgui_menu_create(view, "File", 0, 0, RTGUI_MENU_POPUP); rtgui_menu_append(sub_menu, 0, 0x20001, "New", RT_NULL); rtgui_menu_append(sub_menu, 0, 0x20002, "Open", RT_NULL); rtgui_menu_append(sub_menu, 0, 0x20003, "Save", RT_NULL); rtgui_menu_append(sub_menu, 0, 0x20004, "Print", RT_NULL); rtgui_menu_append(sub_menu, 0, 0x20005, "Exit", RT_NULL); rtgui_menu_append(main_menu, RTGUI_MENU_POPUP, (rt_uint32_t)sub_menu, sub_menu->name, RT_NULL); sub_menu = rtgui_menu_create(view, "EditDocument", 0, 0, RTGUI_MENU_POPUP); rtgui_menu_append(sub_menu, 0, 0x30001, "Cut", RT_NULL); rtgui_menu_append(sub_menu, 0, 0x30002, "Copy", RT_NULL); rtgui_menu_append(sub_menu, 0, 0x30002, "Paste", RT_NULL); rtgui_menu_append(sub_menu, 0, 0x30003, "Find...", RT_NULL); toolmenu = rtgui_menu_create(view, "Toolbars", 0, 0, RTGUI_MENU_POPUP); rtgui_menu_append(toolmenu, 0, 0x50001, "File Tools", RT_NULL); rtgui_menu_append(toolmenu, 0, 0x50002, "build Tools", RT_NULL); rtgui_menu_append(sub_menu, RTGUI_MENU_POPUP, (rt_uint32_t)toolmenu, toolmenu->name, RT_NULL); rtgui_menu_append(main_menu, RTGUI_MENU_POPUP, (rt_uint32_t)sub_menu, sub_menu->name, RT_NULL); sub_menu = rtgui_menu_create(view, "View", 0, 0, RTGUI_MENU_POPUP); rtgui_menu_append(sub_menu, 0, 0x40001, "Status bar", RT_NULL); rtgui_menu_append(sub_menu, 0, 0x40001, "Tool bar", RT_NULL); rtgui_menu_append(sub_menu, 0, 0x40001, "Project window", RT_NULL); rtgui_menu_append(sub_menu, 0, 0x40001, "Books window", RT_NULL); rtgui_menu_append(sub_menu, 0, 0x40001, "Functions window", RT_NULL); rtgui_menu_append(sub_menu, 0, 0x40001, "Full screen", RT_NULL); rtgui_menu_append(main_menu, RTGUI_MENU_POPUP, (rt_uint32_t)sub_menu, sub_menu->name, RT_NULL); rtgui_menu_append(main_menu, 0, 0x10004, "Help", RT_NULL); /* 2.使用静态方式创建菜单 */ /* 菜单的一些功能还在调试中... */ return view; }