toplevel.c 4.9 KB

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