1
0

demo_view_menu.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 rt_bool_t _onmenuitem(struct rtgui_widget *widget, struct rtgui_event* event)
  10. {
  11. rt_kprintf("menu action!!\n");
  12. return RT_TRUE;
  13. }
  14. static const rtgui_menu_item_t sub_items[] =
  15. {
  16. {RTGUI_ITEM_NORMAL, "item #1", RT_NULL, RT_NULL, 0, _onmenuitem},
  17. {RTGUI_ITEM_NORMAL, "item #2", RT_NULL, RT_NULL, 0, RT_NULL},
  18. {RTGUI_ITEM_SEPARATOR, RT_NULL, RT_NULL, RT_NULL, 0, RT_NULL},
  19. {RTGUI_ITEM_NORMAL, "item #3", RT_NULL, RT_NULL, 0, RT_NULL},
  20. };
  21. static const rtgui_menu_item_t items[] =
  22. {
  23. {RTGUI_ITEM_NORMAL, "item #1", RT_NULL, RT_NULL, 0, RT_NULL},
  24. {RTGUI_ITEM_NORMAL, "item #2", RT_NULL, RT_NULL, 0, RT_NULL},
  25. {RTGUI_ITEM_SEPARATOR, RT_NULL, RT_NULL, RT_NULL, 0, RT_NULL},
  26. {RTGUI_ITEM_SUBMENU, "item #3", RT_NULL, (struct rtgui_menu_item_t *)sub_items, sizeof(sub_items)/sizeof(sub_items[0]), RT_NULL},
  27. };
  28. static rtgui_menu_t* menu;
  29. static _onmenu(struct rtgui_widget* widget, struct rtgui_event* event)
  30. {
  31. rtgui_rect_t rect;
  32. rtgui_widget_get_rect(widget, &rect);
  33. rtgui_widget_rect_to_device(widget, &rect);
  34. if (menu != RT_NULL)
  35. rtgui_menu_pop(menu, rect.x1, rect.y2 + 5);
  36. }
  37. /* 创建用于演示menu控件的视图 */
  38. rtgui_view_t* demo_view_menu(rtgui_workbench_t* workbench)
  39. {
  40. rtgui_rect_t rect;
  41. rtgui_view_t* view;
  42. rtgui_button_t* button;
  43. /* 先创建一个演示用的视图 */
  44. view = demo_view(workbench, "MENU View");
  45. /* 获得视图的位置信息 */
  46. demo_view_get_rect(view, &rect);
  47. rect.x1 += 5;
  48. rect.x2 = rect.x1 + 100;
  49. rect.y1 += 5;
  50. rect.y2 = rect.y1 + 20;
  51. /* 创建一个button控件 */
  52. button = rtgui_button_create("Pop Menu");
  53. /* 设置button的位置 */
  54. rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
  55. /* view是一个container控件,调用add_child方法添加这个button控件 */
  56. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(button));
  57. rtgui_button_set_onbutton(button, _onmenu);
  58. menu = rtgui_menu_create("Menu Test", RT_NULL, items, sizeof(items)/sizeof(items[0]));
  59. return view;
  60. }