demo_view_menu.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * 程序清单:menu控件演示
  3. *
  4. * 这个例子会在创建出的view上添加几个不同类型的label控件
  5. */
  6. #include "demo_view.h"
  7. #include <rtgui/widgets/menu.h>
  8. #include <rtgui/widgets/button.h>
  9. static rtgui_menu_item_t sub_items[] =
  10. {
  11. {RTGUI_ITEM_NORMAL, "item #1", RT_NULL, RT_NULL, 0, RT_NULL},
  12. {RTGUI_ITEM_NORMAL, "item #2", RT_NULL, RT_NULL, 0, RT_NULL},
  13. {RTGUI_ITEM_SEPARATOR, RT_NULL, RT_NULL, RT_NULL, 0, RT_NULL},
  14. {RTGUI_ITEM_NORMAL, "item #3", RT_NULL, RT_NULL, 0, RT_NULL},
  15. };
  16. static rtgui_menu_item_t items[] =
  17. {
  18. {RTGUI_ITEM_NORMAL, "item #1", RT_NULL, RT_NULL, 0, RT_NULL},
  19. {RTGUI_ITEM_NORMAL, "item #2", RT_NULL, RT_NULL, 0, RT_NULL},
  20. {RTGUI_ITEM_SEPARATOR, RT_NULL, RT_NULL, RT_NULL, 0, RT_NULL},
  21. {RTGUI_ITEM_NORMAL, "item #3", RT_NULL, RT_NULL, 0, RT_NULL},
  22. // {RTGUI_ITEM_SUBMENU, "item #3", RT_NULL, sub_items, sizeof(sub_items)/sizeof(sub_items[0]), RT_NULL},
  23. };
  24. static rtgui_menu_t* menu;
  25. static _onmenu(struct rtgui_widget* widget, struct rtgui_event* event)
  26. {
  27. rtgui_rect_t rect;
  28. rtgui_widget_get_rect(widget, &rect);
  29. rtgui_widget_rect_to_device(widget, &rect);
  30. if (menu != RT_NULL)
  31. rtgui_menu_pop(menu, rect.x1, rect.y2);
  32. }
  33. /* 创建用于演示menu控件的视图 */
  34. rtgui_view_t* demo_view_menu(rtgui_workbench_t* workbench)
  35. {
  36. rtgui_rect_t rect;
  37. rtgui_view_t* view;
  38. rtgui_button_t* button;
  39. /* 先创建一个演示用的视图 */
  40. view = demo_view(workbench, "MENU View");
  41. /* 获得视图的位置信息 */
  42. demo_view_get_rect(view, &rect);
  43. rect.x1 += 5;
  44. rect.x2 = rect.x1 + 100;
  45. rect.y1 += 5;
  46. rect.y2 = rect.y1 + 20;
  47. /* 创建一个button控件 */
  48. button = rtgui_button_create("Pop Menu");
  49. /* 设置button的位置 */
  50. rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
  51. /* view是一个container控件,调用add_child方法添加这个button控件 */
  52. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(button));
  53. rtgui_button_set_onbutton(button, _onmenu);
  54. menu = rtgui_menu_create("Menu Test", RT_NULL, items, sizeof(items)/sizeof(items[0]));
  55. return view;
  56. }