view.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. static void _rtgui_view_destructor(rtgui_view_t *view)
  26. {
  27. if (view->title != RT_NULL)
  28. {
  29. rt_free(view->title);
  30. view->title = RT_NULL;
  31. }
  32. }
  33. rtgui_type_t *rtgui_view_type_get(void)
  34. {
  35. static rtgui_type_t *view_type = RT_NULL;
  36. if (!view_type)
  37. {
  38. view_type = rtgui_type_create("view", RTGUI_CONTAINER_TYPE,
  39. sizeof(rtgui_view_t),
  40. RTGUI_CONSTRUCTOR(_rtgui_view_constructor),
  41. RTGUI_DESTRUCTOR(_rtgui_view_destructor));
  42. }
  43. return view_type;
  44. }
  45. rt_bool_t rtgui_view_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  46. {
  47. struct rtgui_view* view = (struct rtgui_view*) widget;
  48. RT_ASSERT(widget != RT_NULL);
  49. switch (event->type)
  50. {
  51. case RTGUI_EVENT_PAINT:
  52. if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
  53. else
  54. {
  55. struct rtgui_dc* dc;
  56. struct rtgui_rect rect;
  57. dc = rtgui_dc_begin_drawing(widget);
  58. if (dc == RT_NULL) return RT_FALSE;
  59. rtgui_widget_get_rect(widget, &rect);
  60. /* fill view with background */
  61. rtgui_dc_fill_rect(dc, &rect);
  62. /* paint on each child */
  63. rtgui_container_dispatch_event(RTGUI_CONTAINER(view), event);
  64. rtgui_dc_end_drawing(dc);
  65. }
  66. break;
  67. default:
  68. return rtgui_container_event_handler(widget, event);
  69. }
  70. return RT_FALSE;
  71. }
  72. rtgui_view_t* rtgui_view_create(const char* title)
  73. {
  74. struct rtgui_view* view;
  75. /* allocate view */
  76. view = (struct rtgui_view*) rtgui_widget_create (RTGUI_VIEW_TYPE);
  77. if (view != RT_NULL)
  78. {
  79. if (title != RT_NULL)
  80. view->title = rt_strdup(title);
  81. }
  82. return view;
  83. }
  84. void rtgui_view_destroy(rtgui_view_t* view)
  85. {
  86. rtgui_widget_destroy(RTGUI_WIDGET(view));
  87. }
  88. void rtgui_view_set_box(rtgui_view_t* view, rtgui_box_t* box)
  89. {
  90. if (view == RT_NULL ||
  91. box == RT_NULL) return;
  92. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(box));
  93. rtgui_widget_set_rect(RTGUI_WIDGET(box), &(RTGUI_WIDGET(view)->extent));
  94. }
  95. void rtgui_view_show(rtgui_view_t* view)
  96. {
  97. if (view == RT_NULL) return;
  98. if (RTGUI_WIDGET(view)->parent == RT_NULL)
  99. {
  100. RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(view));
  101. return;
  102. }
  103. rtgui_workbench_show_view((rtgui_workbench_t*)(RTGUI_WIDGET(view)->parent), view);
  104. if (RTGUI_WIDGET_IS_FOCUSABLE(RTGUI_WIDGET(view)))
  105. rtgui_widget_focus(RTGUI_WIDGET(view));
  106. }
  107. void rtgui_view_hide(rtgui_view_t* view)
  108. {
  109. if (view == RT_NULL) return;
  110. if (RTGUI_WIDGET(view)->parent == RT_NULL)
  111. {
  112. RTGUI_WIDGET_HIDE(RTGUI_WIDGET(view));
  113. return;
  114. }
  115. rtgui_workbench_hide_view((rtgui_workbench_t*)(RTGUI_WIDGET(view)->parent), view);
  116. }
  117. char* rtgui_view_get_title(rtgui_view_t* view)
  118. {
  119. RT_ASSERT(view != RT_NULL);
  120. return view->title;
  121. }
  122. void rtgui_view_set_title(rtgui_view_t* view, const char *title)
  123. {
  124. RT_ASSERT(view != RT_NULL);
  125. if (view->title != RT_NULL)
  126. {
  127. rtgui_free(view->title);
  128. if (title != RT_NULL) view->title = rt_strdup(title);
  129. else view->title = RT_NULL;
  130. }
  131. }