title.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * File : title.c
  3. * This file is part of RT-Thread GUI Engine
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2009-10-16 Bernard first version
  23. */
  24. #include <rtgui/rtgui_system.h>
  25. //#include <rtgui/rtgui_theme.h>
  26. #include <rtgui/widgets/window.h>
  27. #include <rtgui/widgets/title.h>
  28. //#include "../server/mouse.h"
  29. /* there is no event handler in wintitle but handle the event on topwin of server */
  30. static void _rtgui_wintitle_constructor(rtgui_wintitle_t *wintitle)
  31. {
  32. RTGUI_WIDGET(wintitle)->flag = RTGUI_WIDGET_FLAG_DEFAULT;
  33. RTGUI_WIDGET_TEXTALIGN(wintitle) = RTGUI_ALIGN_CENTER_VERTICAL;
  34. rtgui_object_set_event_handler(RTGUI_OBJECT(wintitle),
  35. rtgui_wintile_event_handler);
  36. }
  37. static void _rtgui_wintitle_deconstructor(rtgui_wintitle_t *wintitle)
  38. {
  39. }
  40. DEFINE_CLASS_TYPE(wintitle, "wintitle",
  41. RTGUI_PARENT_TYPE(widget),
  42. _rtgui_wintitle_constructor,
  43. _rtgui_wintitle_deconstructor,
  44. sizeof(struct rtgui_wintitle));
  45. rtgui_wintitle_t *rtgui_wintitle_create(struct rtgui_win *window)
  46. {
  47. rtgui_wintitle_t *wintitle;
  48. wintitle = (rtgui_wintitle_t *)rtgui_widget_create(RTGUI_WINTITLE_TYPE);
  49. if (wintitle != RT_NULL)
  50. {
  51. RTGUI_WIDGET(wintitle)->toplevel = window;
  52. }
  53. return wintitle;
  54. }
  55. void rtgui_wintitle_destroy(rtgui_wintitle_t *wintitle)
  56. {
  57. rtgui_widget_destroy(RTGUI_WIDGET(wintitle));
  58. }
  59. rt_bool_t rtgui_wintile_event_handler(struct rtgui_object *obj, rtgui_event_t *eve)
  60. {
  61. struct rtgui_wintitle *wint;
  62. struct rtgui_win *win;
  63. RT_ASSERT(obj);
  64. RT_ASSERT(eve);
  65. wint = RTGUI_WINTITLE(obj);
  66. win = RTGUI_WIDGET(wint)->toplevel;
  67. RT_ASSERT(win);
  68. switch (eve->type)
  69. {
  70. case RTGUI_EVENT_PAINT:
  71. rtgui_theme_draw_win(wint);
  72. return RT_FALSE;
  73. case RTGUI_EVENT_MOUSE_BUTTON:
  74. {
  75. struct rtgui_event_mouse *emou = (struct rtgui_event_mouse *)eve;
  76. if (win->style & RTGUI_WIN_STYLE_CLOSEBOX)
  77. {
  78. rtgui_rect_t rect;
  79. /* get close button rect (device value) */
  80. rect.x1 = RTGUI_WIDGET(wint)->extent.x2 - WINTITLE_BORDER_SIZE - WINTITLE_CB_WIDTH - 3;
  81. rect.y1 = RTGUI_WIDGET(wint)->extent.y1 + WINTITLE_BORDER_SIZE + 3;
  82. rect.x2 = rect.x1 + WINTITLE_CB_WIDTH;
  83. rect.y2 = rect.y1 + WINTITLE_CB_HEIGHT;
  84. if (emou->button & RTGUI_MOUSE_BUTTON_LEFT)
  85. {
  86. if (emou->button & RTGUI_MOUSE_BUTTON_DOWN)
  87. {
  88. if (rtgui_rect_contains_point(&rect, emou->x, emou->y) == RT_EOK)
  89. {
  90. win->flag |= RTGUI_WIN_FLAG_CB_PRESSED;
  91. rtgui_theme_draw_win(wint);
  92. }
  93. #ifdef RTGUI_USING_WINMOVE
  94. else
  95. {
  96. rtgui_winrect_set(win);
  97. }
  98. #endif
  99. }
  100. else if (emou->button & RTGUI_MOUSE_BUTTON_UP)
  101. {
  102. if (win->flag & RTGUI_WIN_FLAG_CB_PRESSED &&
  103. rtgui_rect_contains_point(&rect,
  104. emou->x, emou->y) == RT_EOK)
  105. {
  106. rtgui_win_close(win);
  107. return RT_TRUE;
  108. }
  109. win->flag &= ~RTGUI_WIN_FLAG_CB_PRESSED;
  110. rtgui_theme_draw_win(wint);
  111. #ifdef RTGUI_USING_WINMOVE
  112. /* Reset the window movement state machine. */
  113. rtgui_winrect_moved_done(RT_NULL, RT_NULL);
  114. #endif
  115. }
  116. }
  117. }
  118. else if (emou->button & RTGUI_MOUSE_BUTTON_DOWN)
  119. {
  120. #ifdef RTGUI_USING_WINMOVE
  121. rtgui_winrect_set(win);
  122. #endif
  123. }
  124. }
  125. return RT_TRUE;
  126. default:
  127. return rtgui_widget_event_handler(obj, eve);
  128. }
  129. }