panel.c 6.8 KB

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