toplevel.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * File : toplevel.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/rtgui_system.h>
  15. #include <rtgui/widgets/toplevel.h>
  16. static void _rtgui_toplevel_constructor(rtgui_toplevel_t *toplevel)
  17. {
  18. /* set event handler */
  19. rtgui_widget_set_event_handler(RTGUI_WIDGET(toplevel), rtgui_toplevel_event_handler);
  20. /* set toplevel to self */
  21. RTGUI_WIDGET(toplevel)->toplevel = RTGUI_WIDGET(toplevel);
  22. /* init toplevel property */
  23. toplevel->drawing = 0;
  24. toplevel->external_clip_rect = RT_NULL;
  25. toplevel->external_clip_size = 0;
  26. /* hide toplevel default */
  27. RTGUI_WIDGET_HIDE(RTGUI_WIDGET(toplevel));
  28. /* set server as RT_NULL (no connected) */
  29. toplevel->server = RT_NULL;
  30. }
  31. static void _rtgui_toplevel_destructor(rtgui_toplevel_t* toplevel)
  32. {
  33. /* release external clip info */
  34. toplevel->drawing = 0;
  35. if (toplevel->external_clip_size > 0)
  36. {
  37. rtgui_free(toplevel->external_clip_rect);
  38. toplevel->external_clip_rect = RT_NULL;
  39. toplevel->external_clip_size = 0;
  40. }
  41. }
  42. rtgui_type_t *rtgui_toplevel_type_get(void)
  43. {
  44. static rtgui_type_t *toplevel_type = RT_NULL;
  45. if (!toplevel_type)
  46. {
  47. toplevel_type = rtgui_type_create("toplevel", RTGUI_CONTAINER_TYPE,
  48. sizeof(rtgui_toplevel_t),
  49. RTGUI_CONSTRUCTOR(_rtgui_toplevel_constructor),
  50. RTGUI_DESTRUCTOR(_rtgui_toplevel_destructor));
  51. }
  52. return toplevel_type;
  53. }
  54. rt_bool_t rtgui_toplevel_event_handler(rtgui_widget_t* widget, rtgui_event_t* event)
  55. {
  56. rtgui_toplevel_t* toplevel = (rtgui_toplevel_t*)widget;
  57. switch (event->type)
  58. {
  59. case RTGUI_EVENT_KBD:
  60. if (RTGUI_CONTAINER(toplevel)->focused != RT_NULL)
  61. {
  62. RTGUI_CONTAINER(toplevel)->focused->event_handler(RTGUI_CONTAINER(toplevel)->focused, event);
  63. }
  64. break;
  65. case RTGUI_EVENT_CLIP_INFO:
  66. /* set toplevel external clip info */
  67. rtgui_toplevel_handle_clip(toplevel, (struct rtgui_event_clip_info*)event);
  68. /* update toplevel clip */
  69. rtgui_toplevel_update_clip(toplevel);
  70. break;
  71. case RTGUI_EVENT_TIMER:
  72. {
  73. struct rtgui_timer* timer;
  74. struct rtgui_event_timer* etimer = (struct rtgui_event_timer*) event;
  75. timer = etimer->timer;
  76. if (timer->timeout != RT_NULL)
  77. {
  78. /* call timeout function */
  79. timer->timeout(timer, timer->user_data);
  80. }
  81. }
  82. break;
  83. case RTGUI_EVENT_COMMAND:
  84. if (rtgui_container_dispatch_event(RTGUI_CONTAINER(widget), event) != RT_TRUE)
  85. {
  86. #ifndef RTGUI_USING_SMALL_SIZE
  87. if (widget->on_command != RT_NULL)
  88. {
  89. widget->on_command(widget, event);
  90. }
  91. #endif
  92. }
  93. else return RT_TRUE;
  94. break;
  95. default :
  96. return rtgui_container_event_handler(widget, event);
  97. }
  98. return RT_FALSE;
  99. }
  100. #include <rtgui/widgets/window.h>
  101. #include <rtgui/widgets/workbench.h>
  102. #include <rtgui/widgets/title.h>
  103. void rtgui_toplevel_handle_clip(struct rtgui_toplevel* top,
  104. struct rtgui_event_clip_info* info)
  105. {
  106. RT_ASSERT(top != RT_NULL);
  107. RT_ASSERT(info != RT_NULL);
  108. /* release old rect array */
  109. if (top->external_clip_size != 0)
  110. {
  111. rtgui_free(top->external_clip_rect);
  112. top->external_clip_rect = RT_NULL;
  113. top->external_clip_size = 0;
  114. }
  115. /* no rect info */
  116. if (info->num_rect == 0) return;
  117. top->external_clip_rect = (rtgui_rect_t*) rtgui_malloc(sizeof(rtgui_rect_t) *
  118. info->num_rect);
  119. top->external_clip_size = info->num_rect;
  120. #ifdef RTGUI_USING_SMALL_SIZE
  121. {
  122. extern void rtgui_topwin_get_clipinfo(struct rtgui_rect* list, rt_int32_t count);
  123. /* get rect list from topwin list */
  124. rtgui_topwin_get_clipinfo(top->external_clip_rect, top->external_clip_size);
  125. }
  126. #else
  127. /* copy rect array */
  128. rt_memcpy(top->external_clip_rect, (void*)(info + 1), sizeof(rtgui_rect_t) * info->num_rect);
  129. #endif
  130. }
  131. #include <rtgui/driver.h> /* to get screen rect */
  132. void rtgui_toplevel_update_clip(rtgui_toplevel_t* top)
  133. {
  134. rt_uint32_t idx;
  135. rtgui_container_t* container;
  136. struct rtgui_list_node* node;
  137. rtgui_rect_t screen_rect;
  138. if (top == RT_NULL) return;
  139. /* reset toplevel widget clip to extent */
  140. rtgui_region_reset(&(RTGUI_WIDGET(top)->clip), &(RTGUI_WIDGET(top)->extent));
  141. /* subtract the screen rect */
  142. screen_rect.x1 = screen_rect.y1 = 0;
  143. screen_rect.x2 = rtgui_graphic_driver_get_default()->width;
  144. screen_rect.y2 = rtgui_graphic_driver_get_default()->height;
  145. rtgui_region_intersect_rect(&(RTGUI_WIDGET(top)->clip), &(RTGUI_WIDGET(top)->clip),
  146. &screen_rect);
  147. /* subtract the external rect */
  148. for (idx = 0; idx < top->external_clip_size; idx ++)
  149. {
  150. rtgui_region_subtract_rect(&(RTGUI_WIDGET(top)->clip), &(RTGUI_WIDGET(top)->clip),
  151. &(top->external_clip_rect[idx]));
  152. }
  153. /* update the clip info of each child */
  154. container = RTGUI_CONTAINER(top);
  155. rtgui_list_foreach(node, &(container->children))
  156. {
  157. rtgui_widget_t* child = rtgui_list_entry(node, rtgui_widget_t, sibling);
  158. rtgui_widget_update_clip(child);
  159. }
  160. }