view.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * File : view.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009, 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. * 2009-10-16 Bernard first version
  13. */
  14. #include <rtgui/dc.h>
  15. #include <rtgui/rtgui_system.h>
  16. #include <rtgui/widgets/view.h>
  17. #include <rtgui/widgets/workbench.h>
  18. static void _rtgui_view_constructor(rtgui_view_t *view)
  19. {
  20. /* init view */
  21. rtgui_widget_set_event_handler(RTGUI_WIDGET(view),
  22. rtgui_view_event_handler);
  23. view->title = RT_NULL;
  24. }
  25. rtgui_type_t *rtgui_view_type_get(void)
  26. {
  27. static rtgui_type_t *view_type = RT_NULL;
  28. if (!view_type)
  29. {
  30. view_type = rtgui_type_create("view", RTGUI_CONTAINER_TYPE,
  31. sizeof(rtgui_view_t), RTGUI_CONSTRUCTOR(_rtgui_view_constructor), RT_NULL);
  32. }
  33. return view_type;
  34. }
  35. rt_bool_t rtgui_view_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  36. {
  37. struct rtgui_view* view = (struct rtgui_view*) widget;
  38. RT_ASSERT(widget != RT_NULL);
  39. switch (event->type)
  40. {
  41. case RTGUI_EVENT_PAINT:
  42. if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
  43. else
  44. {
  45. struct rtgui_dc* dc;
  46. struct rtgui_rect rect;
  47. dc = rtgui_dc_begin_drawing(widget);
  48. if (dc == RT_NULL) return RT_FALSE;
  49. rtgui_widget_get_rect(widget, &rect);
  50. /* fill view with background */
  51. rtgui_dc_fill_rect(dc, &rect);
  52. /* paint on each child */
  53. rtgui_container_dispatch_event(RTGUI_CONTAINER(view), event);
  54. rtgui_dc_end_drawing(dc);
  55. }
  56. break;
  57. default:
  58. return rtgui_container_event_handler(widget, event);
  59. }
  60. return RT_FALSE;
  61. }
  62. rtgui_view_t* rtgui_view_create(const char* title)
  63. {
  64. struct rtgui_view* view;
  65. /* allocate view */
  66. view = (struct rtgui_view*) rtgui_widget_create (RTGUI_VIEW_TYPE);
  67. if (view != RT_NULL)
  68. {
  69. if (title != RT_NULL)
  70. view->title = rt_strdup(title);
  71. }
  72. return view;
  73. }
  74. void rtgui_view_destroy(rtgui_view_t* view)
  75. {
  76. rtgui_widget_destroy(RTGUI_WIDGET(view));
  77. }
  78. void rtgui_view_set_box(rtgui_view_t* view, rtgui_box_t* box)
  79. {
  80. if (view == RT_NULL ||
  81. box == RT_NULL) return;
  82. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(box));
  83. rtgui_widget_set_rect(RTGUI_WIDGET(box), &(RTGUI_WIDGET(view)->extent));
  84. }
  85. void rtgui_view_show(rtgui_view_t* view)
  86. {
  87. if (view == RT_NULL) return;
  88. if (RTGUI_WIDGET(view)->parent == RT_NULL)
  89. {
  90. RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(view));
  91. return;
  92. }
  93. rtgui_workbench_show_view((rtgui_workbench_t*)(RTGUI_WIDGET(view)->parent), view);
  94. }
  95. void rtgui_view_hide(rtgui_view_t* view)
  96. {
  97. if (view == RT_NULL) return;
  98. if (RTGUI_WIDGET(view)->parent == RT_NULL)
  99. {
  100. RTGUI_WIDGET_HIDE(RTGUI_WIDGET(view));
  101. return;
  102. }
  103. rtgui_workbench_hide_view((rtgui_workbench_t*)(RTGUI_WIDGET(view)->parent), view);
  104. }
  105. char* rtgui_view_get_title(rtgui_view_t* view)
  106. {
  107. RT_ASSERT(view != RT_NULL);
  108. return view->title;
  109. }
  110. void rtgui_view_set_title(rtgui_view_t* view, const char *title)
  111. {
  112. RT_ASSERT(view != RT_NULL);
  113. if (view->title != RT_NULL)
  114. {
  115. rtgui_free(view->title);
  116. if (title != RT_NULL) view->title = rt_strdup(title);
  117. else view->title = RT_NULL;
  118. }
  119. }