panel.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * File : panel.c
  3. * This file is part of RTGUI in 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-04 Bernard first version
  13. */
  14. #include "panel.h"
  15. #include "mouse.h"
  16. #include <rtgui/rtgui_system.h>
  17. /* the global parameter */
  18. struct rtgui_list_node _rtgui_panel_list;
  19. void rtgui_panel_init()
  20. {
  21. rtgui_list_init(&_rtgui_panel_list);
  22. }
  23. void rtgui_panel_register(char* name, rtgui_rect_t* extent)
  24. {
  25. register rt_base_t temp;
  26. struct rtgui_panel* panel;
  27. panel = rtgui_panel_find(name);
  28. if (panel != RT_NULL )
  29. {
  30. /* there are already a same named panel exist. */
  31. return;
  32. }
  33. panel = rtgui_malloc(sizeof(struct rtgui_panel));
  34. if (panel == RT_NULL)
  35. {
  36. /* can't alloc memory */
  37. return;
  38. }
  39. /* copy name */
  40. for (temp = 0; temp < RTGUI_NAME_MAX; temp ++)
  41. {
  42. panel->name[temp] = name[temp];
  43. }
  44. /* copy extent */
  45. panel->extent = *extent;
  46. panel->wm_thread = RT_NULL;
  47. /* init list */
  48. rtgui_list_init(&(panel->sibling));
  49. rtgui_list_init(&(panel->thread_list));
  50. /* add panel to panel list */
  51. rtgui_list_insert(&_rtgui_panel_list, &(panel->sibling));
  52. }
  53. void rtgui_panel_deregister(char* name)
  54. {
  55. struct rtgui_panel* panel;
  56. panel = rtgui_panel_find(name);
  57. if (panel != RT_NULL)
  58. {
  59. rtgui_list_remove(&_rtgui_panel_list, &(panel->sibling));
  60. /* free pane node */
  61. rtgui_free(panel);
  62. }
  63. }
  64. /* set default focused panel, please use it after registered panel */
  65. void rtgui_panel_set_default_focused(char* name)
  66. {
  67. extern struct rtgui_panel* rtgui_server_focus_panel;
  68. struct rtgui_panel* panel;
  69. panel = rtgui_panel_find(name);
  70. if (panel != RT_NULL)
  71. {
  72. rtgui_server_focus_panel = panel;
  73. }
  74. }
  75. struct rtgui_panel* rtgui_panel_find(char* name)
  76. {
  77. struct rtgui_list_node* node;
  78. struct rtgui_panel* panel;
  79. rtgui_list_foreach(node, &_rtgui_panel_list)
  80. {
  81. panel = rtgui_list_entry(node, struct rtgui_panel, sibling);
  82. if (rt_strncmp(panel->name, name, RTGUI_NAME_MAX) == 0)
  83. {
  84. return panel;
  85. }
  86. }
  87. return RT_NULL;
  88. }
  89. struct rtgui_panel* rtgui_panel_thread_add(char* name, rt_thread_t tid)
  90. {
  91. struct rtgui_panel* panel;
  92. panel = rtgui_panel_find(name);
  93. if (panel != RT_NULL )
  94. {
  95. struct rtgui_panel_thread* thread;
  96. /* allocate panel thread node */
  97. thread = rtgui_malloc(sizeof(struct rtgui_panel_thread));
  98. if (thread == RT_NULL)
  99. {
  100. return RT_NULL;
  101. }
  102. /* construct panel thread node */
  103. thread->tid = tid;
  104. /* init list */
  105. rtgui_list_init(&(thread->list));
  106. rtgui_list_init(&(thread->monitor_list));
  107. /* append thread to the list */
  108. rtgui_list_append(&(panel->thread_list), &(thread->list));
  109. }
  110. return panel;
  111. }
  112. void rtgui_panel_thread_remove(rtgui_panel_t* panel, rt_thread_t tid)
  113. {
  114. if (panel != RT_NULL )
  115. {
  116. struct rtgui_list_node* node;
  117. struct rtgui_panel_thread* thread;
  118. rtgui_list_foreach(node, &(panel->thread_list))
  119. {
  120. thread = rtgui_list_entry(node, struct rtgui_panel_thread, list);
  121. if (thread->tid == tid)
  122. {
  123. /* remove node from list */
  124. rtgui_list_remove(&(panel->thread_list), &(thread->list));
  125. /* free the panel thread node */
  126. rtgui_free(thread);
  127. return;
  128. }
  129. }
  130. }
  131. }
  132. rt_thread_t rtgui_panel_get_active_thread(rtgui_panel_t* panel)
  133. {
  134. if (panel != RT_NULL)
  135. {
  136. if (panel->thread_list.next != RT_NULL)
  137. {
  138. struct rtgui_panel_thread* thread;
  139. thread = rtgui_list_entry(panel->thread_list.next, struct rtgui_panel_thread, list);
  140. return thread->tid;
  141. }
  142. }
  143. return RT_NULL;
  144. }
  145. void rtgui_panel_set_active_thread(rtgui_panel_t* panel, rt_thread_t tid)
  146. {
  147. /* get old active thread */
  148. rt_thread_t prev_actived = rtgui_panel_get_active_thread(panel);
  149. if (prev_actived != tid)
  150. {
  151. /* de-active old active workbench */
  152. struct rtgui_event_panel_hide ehide;
  153. RTGUI_EVENT_PANEL_HIDE_INIT(&ehide);
  154. ehide.panel = panel;
  155. ehide.workbench = RT_NULL;
  156. rtgui_thread_send_urgent(prev_actived, &(ehide.parent), sizeof (ehide));
  157. }
  158. if (panel != RT_NULL )
  159. {
  160. struct rtgui_list_node* node;
  161. struct rtgui_panel_thread* thread;
  162. rtgui_list_foreach(node, &(panel->thread_list))
  163. {
  164. thread = rtgui_list_entry(node, struct rtgui_panel_thread, list);
  165. if (thread->tid == tid)
  166. {
  167. /* remove node from list */
  168. rtgui_list_remove(&(panel->thread_list), &(thread->list));
  169. /* insert node to the header */
  170. rtgui_list_insert(&(panel->thread_list), &(thread->list));
  171. return;
  172. }
  173. }
  174. }
  175. }
  176. /* deactivate current activated thread -- move it to the end of list */
  177. void rtgui_panel_deactive_thread(rtgui_panel_t* panel)
  178. {
  179. RT_ASSERT(panel == RT_NULL);
  180. if (panel->thread_list.next != RT_NULL)
  181. {
  182. struct rtgui_panel_thread* thread;
  183. thread = rtgui_list_entry(panel->thread_list.next, struct rtgui_panel_thread, list);
  184. /* remove it */
  185. panel->thread_list.next = thread->list.next;
  186. /* append to the tail of thread list */
  187. rtgui_list_append(&(panel->thread_list), &(thread->list));
  188. }
  189. }
  190. /**
  191. * get the panel which contains a point(x, y)
  192. */
  193. rtgui_panel_t* rtgui_panel_get_contain(int x, int y)
  194. {
  195. struct rtgui_list_node* node;
  196. struct rtgui_panel* panel;
  197. rtgui_list_foreach(node, &(_rtgui_panel_list))
  198. {
  199. panel = rtgui_list_entry(node, struct rtgui_panel, sibling);
  200. if (rtgui_rect_contains_point(&(panel->extent), x, y) == RT_EOK)
  201. {
  202. return panel;
  203. }
  204. }
  205. return RT_NULL;
  206. }
  207. /**
  208. * append a rect to panel mouse monitor rect list
  209. */
  210. void rtgui_panel_append_monitor_rect(rtgui_panel_t* panel, rt_thread_t tid, rtgui_rect_t* rect)
  211. {
  212. if (panel != RT_NULL )
  213. {
  214. struct rtgui_list_node* node;
  215. struct rtgui_panel_thread* thread;
  216. rtgui_list_foreach(node, &(panel->thread_list))
  217. {
  218. thread = rtgui_list_entry(node, struct rtgui_panel_thread, list);
  219. if (thread->tid == tid)
  220. {
  221. /* add the monitor rect to list */
  222. rtgui_mouse_monitor_append(&(thread->monitor_list), rect);
  223. return;
  224. }
  225. }
  226. }
  227. }
  228. /**
  229. * remove a rect from panel mouse monitor rect list
  230. */
  231. void rtgui_panel_remove_monitor_rect(rtgui_panel_t* panel, rt_thread_t tid, rtgui_rect_t* rect)
  232. {
  233. if (panel != RT_NULL )
  234. {
  235. struct rtgui_list_node* node;
  236. struct rtgui_panel_thread* thread;
  237. rtgui_list_foreach(node, &(panel->thread_list))
  238. {
  239. thread = rtgui_list_entry(node, struct rtgui_panel_thread, list);
  240. if (thread->tid == tid)
  241. {
  242. /* remove the monitor rect from list */
  243. rtgui_mouse_monitor_remove(&(thread->monitor_list), rect);
  244. return;
  245. }
  246. }
  247. }
  248. }
  249. void rtgui_panel_set_wm(rtgui_panel_t* panel, rt_thread_t wm)
  250. {
  251. RT_ASSERT(wm != RT_NULL);
  252. RT_ASSERT(panel != RT_NULL);
  253. panel->wm_thread = wm;
  254. }