toplevel.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. extern void rtgui_topwin_do_clip(rtgui_widget_t* widget);
  17. static void _rtgui_toplevel_constructor(rtgui_toplevel_t *toplevel)
  18. {
  19. /* set event handler */
  20. rtgui_widget_set_event_handler(RTGUI_WIDGET(toplevel), rtgui_toplevel_event_handler);
  21. /* set toplevel to self */
  22. RTGUI_WIDGET(toplevel)->toplevel = RTGUI_WIDGET(toplevel);
  23. /* init toplevel property */
  24. toplevel->drawing = 0;
  25. /* hide toplevel default */
  26. RTGUI_WIDGET_HIDE(RTGUI_WIDGET(toplevel));
  27. /* set server as RT_NULL (no connected) */
  28. toplevel->server = RT_NULL;
  29. /* initialize last mouse event handled widget */
  30. toplevel->last_mevent_widget = RT_NULL;
  31. }
  32. static void _rtgui_toplevel_destructor(rtgui_toplevel_t* toplevel)
  33. {
  34. /* release external clip info */
  35. toplevel->drawing = 0;
  36. }
  37. DEFINE_CLASS_TYPE(toplevel, "toplevel",
  38. RTGUI_CONTAINER_TYPE,
  39. _rtgui_toplevel_constructor,
  40. _rtgui_toplevel_destructor,
  41. sizeof(struct rtgui_toplevel));
  42. rt_bool_t rtgui_toplevel_event_handler(rtgui_widget_t* widget, rtgui_event_t* event)
  43. {
  44. rtgui_toplevel_t* toplevel = (rtgui_toplevel_t*)widget;
  45. switch (event->type)
  46. {
  47. case RTGUI_EVENT_KBD:
  48. if (RTGUI_CONTAINER(toplevel)->focused != RT_NULL)
  49. {
  50. RTGUI_CONTAINER(toplevel)->focused->event_handler(RTGUI_CONTAINER(toplevel)->focused, event);
  51. }
  52. break;
  53. case RTGUI_EVENT_CLIP_INFO:
  54. /* update toplevel clip */
  55. rtgui_toplevel_update_clip(toplevel);
  56. break;
  57. case RTGUI_EVENT_TIMER:
  58. {
  59. struct rtgui_timer* timer;
  60. struct rtgui_event_timer* etimer = (struct rtgui_event_timer*) event;
  61. timer = etimer->timer;
  62. if (timer->timeout != RT_NULL)
  63. {
  64. /* call timeout function */
  65. timer->timeout(timer, timer->user_data);
  66. }
  67. }
  68. break;
  69. case RTGUI_EVENT_COMMAND:
  70. if (rtgui_container_dispatch_event(RTGUI_CONTAINER(widget), event) != RT_TRUE)
  71. {
  72. #ifndef RTGUI_USING_SMALL_SIZE
  73. if (widget->on_command != RT_NULL)
  74. {
  75. widget->on_command(widget, event);
  76. }
  77. #endif
  78. }
  79. else return RT_TRUE;
  80. break;
  81. default :
  82. return rtgui_container_event_handler(widget, event);
  83. }
  84. return RT_FALSE;
  85. }
  86. #include <rtgui/driver.h> /* to get screen rect */
  87. void rtgui_toplevel_update_clip(rtgui_toplevel_t* top)
  88. {
  89. rtgui_container_t* container;
  90. struct rtgui_list_node* node;
  91. rtgui_rect_t screen_rect;
  92. if (top == RT_NULL) return;
  93. /* reset toplevel widget clip to extent */
  94. rtgui_region_reset(&(RTGUI_WIDGET(top)->clip), &(RTGUI_WIDGET(top)->extent));
  95. /* subtract the screen rect */
  96. screen_rect.x1 = screen_rect.y1 = 0;
  97. screen_rect.x2 = rtgui_graphic_driver_get_default()->width;
  98. screen_rect.y2 = rtgui_graphic_driver_get_default()->height;
  99. rtgui_region_intersect_rect(&(RTGUI_WIDGET(top)->clip), &(RTGUI_WIDGET(top)->clip),
  100. &screen_rect);
  101. /* subtract the external rect */
  102. rtgui_topwin_do_clip(RTGUI_WIDGET(top));
  103. /* update the clip info of each child */
  104. container = RTGUI_CONTAINER(top);
  105. rtgui_list_foreach(node, &(container->children))
  106. {
  107. rtgui_widget_t* child = rtgui_list_entry(node, rtgui_widget_t, sibling);
  108. rtgui_widget_update_clip(child);
  109. }
  110. }