panel.c 6.8 KB

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