groupbox.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * File : groupbox.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2012-07-29 Bernard first version
  13. */
  14. #include <rtgui/dc.h>
  15. #include <rtgui/rtgui_system.h>
  16. #include <rtgui/rtgui_theme.h>
  17. #include <rtgui/widgets/groupbox.h>
  18. static void _rtgui_groupbox_constructor(rtgui_groupbox_t *box)
  19. {
  20. /* init widget and set event handler */
  21. rtgui_object_set_event_handler(RTGUI_OBJECT(box), rtgui_groupbox_event_handler);
  22. /* set field */
  23. box->box = RT_NULL;
  24. box->label = RT_NULL;
  25. box->selected = RT_NULL;
  26. box->on_selected = RT_NULL;
  27. }
  28. DEFINE_CLASS_TYPE(groupbox, "groupbox",
  29. RTGUI_PANEL_TYPE,
  30. _rtgui_groupbox_constructor,
  31. RT_NULL,
  32. sizeof(struct rtgui_groupbox));
  33. rt_bool_t rtgui_groupbox_event_handler(struct rtgui_object *object, struct rtgui_event *event)
  34. {
  35. struct rtgui_groupbox *box;
  36. box = RTGUI_GROUPBOX(object);
  37. switch (event->type)
  38. {
  39. case RTGUI_EVENT_PAINT:
  40. {
  41. rtgui_panel_event_handler(RTGUI_OBJECT(box), event);
  42. /* dispatch paint event to child */
  43. rtgui_container_dispatch_event(RTGUI_CONTAINER(box), event);
  44. }
  45. break;
  46. default:
  47. return rtgui_container_event_handler(object, event);
  48. }
  49. return RT_FALSE;
  50. }
  51. rtgui_groupbox_t *rtgui_groupbox_create(const char *label, struct rtgui_rect *rect,
  52. int style, widget_select_t select_func)
  53. {
  54. struct rtgui_groupbox *box;
  55. RT_ASSERT(select_func != RT_NULL);
  56. box = (struct rtgui_groupbox *) rtgui_widget_create(RTGUI_GROUPBOX_TYPE);
  57. if (box != RT_NULL)
  58. {
  59. rtgui_widget_set_rect(RTGUI_WIDGET(box), rect);
  60. if (label != RT_NULL)
  61. {
  62. box->label = rt_strdup(label);
  63. }
  64. /* create layout box */
  65. box->box = rtgui_box_create(style, RTGUI_WIDGET_DEFAULT_MARGIN + 1);
  66. rtgui_container_set_box(RTGUI_CONTAINER(box), box->box);
  67. rtgui_panel_set_border(RTGUI_PANEL(box), RTGUI_BORDER_NONE);
  68. box->select_func = select_func;
  69. }
  70. return box;
  71. }
  72. void rtgui_groupbox_destroy(rtgui_groupbox_t *groupbox)
  73. {
  74. rtgui_object_destroy(RTGUI_OBJECT(groupbox));
  75. }
  76. void rtgui_groupbox_select_widget(struct rtgui_groupbox *box, struct rtgui_widget *widget)
  77. {
  78. struct rtgui_event event;
  79. RT_ASSERT(box != RT_NULL);
  80. RT_ASSERT(widget != RT_NULL);
  81. if (box->selected != widget)
  82. {
  83. if (box->selected != RT_NULL)
  84. {
  85. box->select_func(box->selected, RT_FALSE);
  86. if (box->on_selected != RT_NULL)
  87. {
  88. RTGUI_EVENT_INIT(&event, RTGUI_EVENT_UNSELECTED);
  89. box->on_selected(RTGUI_OBJECT(widget), &event);
  90. }
  91. rtgui_widget_update(widget);
  92. }
  93. box->selected = widget;
  94. }
  95. box->select_func(box->selected, RT_TRUE);
  96. if (box->on_selected != RT_NULL)
  97. {
  98. RTGUI_EVENT_INIT(&event, RTGUI_EVENT_SELECTED);
  99. box->on_selected(RTGUI_OBJECT(widget), &event);
  100. }
  101. }
  102. struct rtgui_widget *rtgui_groupbox_get_selected(struct rtgui_groupbox *box)
  103. {
  104. RT_ASSERT(box != RT_NULL);
  105. return box->selected;
  106. }
  107. void rtgui_groupbox_add_widget(struct rtgui_groupbox *box, struct rtgui_widget *widget)
  108. {
  109. widget->user_data = (rt_uint32_t)box;
  110. rtgui_container_add_child(RTGUI_CONTAINER(box), widget);
  111. RTGUI_WIDGET_ALIGN(widget) = RTGUI_ALIGN_CENTER;
  112. RTGUI_WIDGET_BACKGROUND(widget) = RTGUI_WIDGET_BACKGROUND(box);
  113. }
  114. void rtgui_groupbox_layout(struct rtgui_groupbox *box)
  115. {
  116. if (RTGUI_PANEL(box)->border_style != RTGUI_BORDER_NONE)
  117. {
  118. rtgui_box_layout(box->box);
  119. }
  120. else
  121. {
  122. struct rtgui_rect extent;
  123. RT_ASSERT(box != RT_NULL);
  124. rtgui_widget_get_extent(RTGUI_WIDGET(box), &extent);
  125. rtgui_rect_inflate(&extent, -RTGUI_WIDGET_DEFAULT_MARGIN);
  126. rtgui_box_layout_rect(box->box, &extent);
  127. }
  128. }