demo_view_progressbar.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "demo_view.h"
  2. #include <rtgui/rtgui_system.h>
  3. #include <rtgui/widgets/label.h>
  4. #include <rtgui/widgets/progressbar.h>
  5. static rtgui_progressbar_t *hbar;
  6. static rtgui_progressbar_t *vbar;
  7. static rtgui_timer_t *bar_timer = RT_NULL;
  8. void progressbar_timeout(struct rtgui_timer *timer, void *parameter)
  9. {
  10. static rt_uint32_t value = 0;
  11. value++;
  12. if (value == 100)
  13. value = 0;
  14. rtgui_progressbar_set_value(hbar, value);
  15. rtgui_progressbar_set_value(vbar, value);
  16. }
  17. static rt_bool_t start_timer(struct rtgui_object *object, struct rtgui_event *event)
  18. {
  19. if (bar_timer != RT_NULL)
  20. rtgui_timer_start(bar_timer);
  21. return RT_TRUE;
  22. }
  23. static rt_bool_t stop_timer(struct rtgui_object *object, struct rtgui_event *event)
  24. {
  25. if (bar_timer != RT_NULL)
  26. rtgui_timer_stop(bar_timer);
  27. return RT_TRUE;
  28. }
  29. static rt_bool_t progressbar_event_handler(struct rtgui_object *object, rtgui_event_t *event)
  30. {
  31. struct rtgui_widget *widget = RTGUI_WIDGET(object);
  32. if (event->type == RTGUI_EVENT_SHOW)
  33. {
  34. rtgui_container_event_handler(object, event);
  35. start_timer(object, event);
  36. }
  37. else if (event->type == RTGUI_EVENT_HIDE)
  38. {
  39. rtgui_container_event_handler(object, event);
  40. stop_timer(object, event);
  41. }
  42. else
  43. {
  44. /* 调用默认的事件处理函数 */
  45. return rtgui_progressbar_event_handler(object, event);
  46. }
  47. return RT_FALSE;
  48. }
  49. rtgui_container_t *demo_view_progressbar(void)
  50. {
  51. rtgui_container_t *container;
  52. rtgui_rect_t rect;
  53. rtgui_label_t *label;
  54. /* create a demo container */
  55. container = demo_view("ProgressBar View");
  56. /* get demo container rect */
  57. demo_view_get_rect(container, &rect);
  58. label = rtgui_label_create("水平进度条:");
  59. rtgui_container_add_child(container, RTGUI_WIDGET(label));
  60. rect.x1 += 5;
  61. rect.x2 -= 5;
  62. rect.y1 += 5;
  63. rect.y2 = rect.y1 + 18;
  64. rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);
  65. rect.y1 += 20;
  66. rect.y2 = rect.y1 + 18;
  67. hbar = rtgui_progressbar_create(RTGUI_HORIZONTAL, 100, &rect);
  68. rtgui_container_add_child(container, RTGUI_WIDGET(hbar));
  69. /* get demo container rect */
  70. demo_view_get_rect(container, &rect);
  71. label = rtgui_label_create("垂直进度条:");
  72. rtgui_container_add_child(container, RTGUI_WIDGET(label));
  73. rect.x1 += 5;
  74. rect.x2 -= 5;
  75. rect.y1 += 45;
  76. rect.y2 = rect.y1 + 18;
  77. rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);
  78. rect.x1 += 110;
  79. rect.x2 = rect.x1 + 20;
  80. rect.y1 += 18 + 5;
  81. rect.y2 = rect.y1 + 150;
  82. vbar = rtgui_progressbar_create(RTGUI_VERTICAL, 100, &rect);
  83. rtgui_container_add_child(container, RTGUI_WIDGET(vbar));
  84. bar_timer = rtgui_timer_create(50, RT_TIMER_FLAG_PERIODIC,
  85. progressbar_timeout, RT_NULL);
  86. return container;
  87. }