topwin.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. /*
  2. * File : topwin.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 "topwin.h"
  15. #include "mouse.h"
  16. #include <rtgui/dlist.h>
  17. #include <rtgui/event.h>
  18. #include <rtgui/image.h>
  19. #include <rtgui/rtgui_theme.h>
  20. #include <rtgui/rtgui_system.h>
  21. #include <rtgui/rtgui_app.h>
  22. #include <rtgui/widgets/window.h>
  23. /* This list is divided into two parts. The first part is the shown list, in
  24. * which all the windows have the WINTITLE_SHOWN flag set. Second part is the
  25. * hidden items, in which all the windows don't have WINTITLE_SHOWN flag.
  26. *
  27. * The active window is the one that would receive kbd events. It should always
  28. * be in the first tree. The order of this list is the order of the windows.
  29. * Top window can always clip the window beneath it when the two
  30. * overlapping. Child window can always clip it's parent. Slibing windows can
  31. * clip each other with the same rule as this list. Each child list is the same
  32. * as _rtgui_topwin_list. This forms the hierarchy tree structure of all
  33. * windows.
  34. *
  35. * Thus, the left most leaf of the tree is the top most window and the right
  36. * most root node is the bottom window. The hidden part have no specific
  37. * order.
  38. */
  39. static struct rtgui_dlist_node _rtgui_topwin_list;
  40. #define get_topwin_from_list(list_entry) \
  41. (rtgui_dlist_entry((list_entry), struct rtgui_topwin, list))
  42. #define IS_ROOT_WIN(topwin) ((topwin)->parent == RT_NULL)
  43. static struct rt_semaphore _rtgui_topwin_lock;
  44. static void rtgui_topwin_update_clip(void);
  45. static void rtgui_topwin_redraw(struct rtgui_rect *rect);
  46. static void _rtgui_topwin_activate_next(enum rtgui_topwin_flag);
  47. void rtgui_topwin_init(void)
  48. {
  49. /* init window list */
  50. rtgui_dlist_init(&_rtgui_topwin_list);
  51. rt_sem_init(&_rtgui_topwin_lock,
  52. "topwin", 1, RT_IPC_FLAG_FIFO);
  53. }
  54. static struct rtgui_topwin *rtgui_topwin_search_in_list(struct rtgui_win *window,
  55. struct rtgui_dlist_node *list)
  56. {
  57. /* TODO: use a cache to speed up the search. */
  58. struct rtgui_dlist_node *node;
  59. struct rtgui_topwin *topwin;
  60. /* the action is tend to operate on the top most window. So we search in a
  61. * depth first order.
  62. */
  63. rtgui_dlist_foreach(node, list, next)
  64. {
  65. topwin = rtgui_dlist_entry(node, struct rtgui_topwin, list);
  66. /* is this node? */
  67. if (topwin->wid == window)
  68. {
  69. return topwin;
  70. }
  71. topwin = rtgui_topwin_search_in_list(window, &topwin->child_list);
  72. if (topwin != RT_NULL)
  73. return topwin;
  74. }
  75. return RT_NULL;
  76. }
  77. /* add a window to window list[hide] */
  78. rt_err_t rtgui_topwin_add(struct rtgui_event_win_create *event)
  79. {
  80. struct rtgui_topwin *topwin;
  81. topwin = rtgui_malloc(sizeof(struct rtgui_topwin));
  82. if (topwin == RT_NULL)
  83. return -RT_ERROR;
  84. topwin->wid = event->wid;
  85. #ifdef RTGUI_USING_SMALL_SIZE
  86. topwin->extent = RTGUI_WIDGET(event->wid)->extent;
  87. #else
  88. topwin->extent = event->extent;
  89. #endif
  90. topwin->tid = event->parent.sender;
  91. if (event->parent_window == RT_NULL)
  92. {
  93. topwin->parent = RT_NULL;
  94. rtgui_dlist_insert_before(&_rtgui_topwin_list, &topwin->list);
  95. }
  96. else
  97. {
  98. topwin->parent = rtgui_topwin_search_in_list(event->parent_window, &_rtgui_topwin_list);
  99. if (topwin->parent == RT_NULL)
  100. {
  101. /* parent does not exist. Orphan window? */
  102. rtgui_free(topwin);
  103. return -RT_ERROR;
  104. }
  105. rtgui_dlist_insert_before(&topwin->parent->child_list, &topwin->list);
  106. }
  107. rtgui_dlist_init(&topwin->child_list);
  108. topwin->flag = 0;
  109. if (event->parent.user & RTGUI_WIN_STYLE_NO_TITLE) topwin->flag |= WINTITLE_NO;
  110. if (event->parent.user & RTGUI_WIN_STYLE_CLOSEBOX) topwin->flag |= WINTITLE_CLOSEBOX;
  111. if (!(event->parent.user & RTGUI_WIN_STYLE_NO_BORDER)) topwin->flag |= WINTITLE_BORDER;
  112. if (event->parent.user & RTGUI_WIN_STYLE_NO_FOCUS) topwin->flag |= WINTITLE_NOFOCUS;
  113. if (event->parent.user & RTGUI_WIN_STYLE_ONTOP) topwin->flag |= WINTITLE_ONTOP;
  114. if (event->parent.user & RTGUI_WIN_STYLE_ONBTM) topwin->flag |= WINTITLE_ONBTM;
  115. if (!(topwin->flag & WINTITLE_NO) || (topwin->flag & WINTITLE_BORDER))
  116. {
  117. /* get win extent */
  118. rtgui_rect_t rect = topwin->extent;
  119. /* add border rect */
  120. if (topwin->flag & WINTITLE_BORDER)
  121. {
  122. rtgui_rect_inflate(&rect, WINTITLE_BORDER_SIZE);
  123. }
  124. /* add title rect */
  125. if (!(topwin->flag & WINTITLE_NO)) rect.y1 -= WINTITLE_HEIGHT;
  126. #ifdef RTGUI_USING_SMALL_SIZE
  127. topwin->title = rtgui_wintitle_create(topwin->wid, event->wid->title);
  128. #else
  129. topwin->title = rtgui_wintitle_create(topwin->wid, (const char *)event->title);
  130. #endif
  131. rtgui_widget_set_rect(RTGUI_WIDGET(topwin->title), &rect);
  132. /* update clip info */
  133. rtgui_region_subtract_rect(&(RTGUI_WIDGET(topwin->title)->clip),
  134. &(RTGUI_WIDGET(topwin->title)->clip),
  135. &(topwin->extent));
  136. }
  137. else
  138. topwin->title = RT_NULL;
  139. rtgui_list_init(&topwin->monitor_list);
  140. return RT_EOK;
  141. }
  142. static struct rtgui_topwin *_rtgui_topwin_get_root_win(struct rtgui_topwin *topwin)
  143. {
  144. struct rtgui_topwin *topparent;
  145. RT_ASSERT(topwin != RT_NULL);
  146. topparent = topwin;
  147. while (topparent && !IS_ROOT_WIN(topparent))
  148. topparent = topparent->parent;
  149. return topparent;
  150. }
  151. static struct rtgui_topwin *_rtgui_topwin_get_topmost_child_shown(struct rtgui_topwin *topwin)
  152. {
  153. RT_ASSERT(topwin != RT_NULL);
  154. while (!(rtgui_dlist_isempty(&topwin->child_list)) &&
  155. get_topwin_from_list(topwin->child_list.next)->flag &WINTITLE_SHOWN)
  156. topwin = get_topwin_from_list(topwin->child_list.next);
  157. return topwin;
  158. }
  159. static rt_bool_t _rtgui_topwin_in_layer(struct rtgui_topwin *topwin, enum rtgui_topwin_flag flag)
  160. {
  161. return (topwin->flag & (WINTITLE_ONTOP | WINTITLE_ONBTM))
  162. == (flag & (WINTITLE_ONTOP | WINTITLE_ONBTM));
  163. }
  164. /* find the topmost window shown in the layer set by flag. The flag has many
  165. * other infomations but we only use the ONTOP/ONBTM */
  166. static struct rtgui_topwin *_rtgui_topwin_get_topmost_window_shown(enum rtgui_topwin_flag flag)
  167. {
  168. struct rtgui_dlist_node *node;
  169. rtgui_dlist_foreach(node, &_rtgui_topwin_list, next)
  170. {
  171. struct rtgui_topwin *topwin = get_topwin_from_list(node);
  172. /* reach the hidden region no window shown in current layer */
  173. if (!(topwin->flag & WINTITLE_SHOWN))
  174. return RT_NULL;
  175. if (_rtgui_topwin_in_layer(topwin, flag))
  176. return _rtgui_topwin_get_topmost_child_shown(topwin);
  177. }
  178. /* no window in current layer is shown */
  179. return RT_NULL;
  180. }
  181. /* a hidden parent will hide it's children. Top level window can be shown at
  182. * any time. */
  183. static rt_bool_t _rtgui_topwin_could_show(struct rtgui_topwin *topwin)
  184. {
  185. struct rtgui_topwin *parent;
  186. RT_ASSERT(topwin != RT_NULL);
  187. for (parent = topwin->parent; parent != RT_NULL; parent = parent->parent)
  188. {
  189. if (!(parent->flag & WINTITLE_SHOWN))
  190. return RT_FALSE;
  191. }
  192. return RT_TRUE;
  193. }
  194. static void _rtgui_topwin_union_region_tree(struct rtgui_topwin *topwin,
  195. struct rtgui_region *region)
  196. {
  197. struct rtgui_dlist_node *node;
  198. RT_ASSERT(topwin != RT_NULL);
  199. rtgui_dlist_foreach(node, &topwin->child_list, next)
  200. _rtgui_topwin_union_region_tree(get_topwin_from_list(node), region);
  201. if (topwin->title != RT_NULL)
  202. rtgui_region_union_rect(region, region, &RTGUI_WIDGET(topwin->title)->extent);
  203. else
  204. rtgui_region_union_rect(region, region, &topwin->extent);
  205. }
  206. /* The return value of this function is the next node in tree.
  207. *
  208. * As we freed the node in this function, it would be a null reference error of
  209. * the caller iterate the tree normally.
  210. */
  211. static struct rtgui_dlist_node *_rtgui_topwin_free_tree(struct rtgui_topwin *topwin)
  212. {
  213. struct rtgui_dlist_node *node, *next_node;
  214. RT_ASSERT(topwin != RT_NULL);
  215. node = topwin->child_list.next;
  216. while (node != &topwin->child_list)
  217. node = _rtgui_topwin_free_tree(get_topwin_from_list(node));
  218. next_node = topwin->list.next;
  219. rtgui_dlist_remove(&topwin->list);
  220. /* free the monitor rect list, topwin node and title */
  221. while (topwin->monitor_list.next != RT_NULL)
  222. {
  223. struct rtgui_mouse_monitor *monitor = rtgui_list_entry(topwin->monitor_list.next,
  224. struct rtgui_mouse_monitor, list);
  225. topwin->monitor_list.next = topwin->monitor_list.next->next;
  226. rtgui_free(monitor);
  227. }
  228. /* destroy win title */
  229. rtgui_wintitle_destroy(topwin->title);
  230. topwin->title = RT_NULL;
  231. rtgui_free(topwin);
  232. return next_node;
  233. }
  234. rt_err_t rtgui_topwin_remove(struct rtgui_win *wid)
  235. {
  236. struct rtgui_topwin *topwin, *old_focus;
  237. struct rtgui_region region;
  238. /* find the topwin node */
  239. topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_list);
  240. if (topwin == RT_NULL)
  241. return -RT_ERROR;
  242. rtgui_region_init(&region);
  243. old_focus = rtgui_topwin_get_focus();
  244. /* remove the root from _rtgui_topwin_list will remove the whole tree from
  245. * _rtgui_topwin_list. */
  246. rtgui_dlist_remove(&topwin->list);
  247. if (old_focus == topwin)
  248. {
  249. _rtgui_topwin_activate_next(topwin->flag);
  250. }
  251. if (topwin->flag & WINTITLE_SHOWN)
  252. {
  253. rtgui_topwin_update_clip();
  254. /* redraw the old rect */
  255. _rtgui_topwin_union_region_tree(topwin, &region);
  256. rtgui_topwin_redraw(rtgui_region_extents(&region));
  257. }
  258. _rtgui_topwin_free_tree(topwin);
  259. return RT_EOK;
  260. }
  261. /* neither deactivate the old focus nor change _rtgui_topwin_list.
  262. * Suitable to be called when the first item is the window to be activated
  263. * already. */
  264. static void _rtgui_topwin_only_activate(struct rtgui_topwin *topwin)
  265. {
  266. struct rtgui_event_win event;
  267. RT_ASSERT(topwin != RT_NULL);
  268. if (topwin->flag & WINTITLE_NOFOCUS)
  269. return;
  270. /* activate the raised window */
  271. RTGUI_EVENT_WIN_ACTIVATE_INIT(&event);
  272. topwin->flag |= WINTITLE_ACTIVATE;
  273. event.wid = topwin->wid;
  274. rtgui_send(topwin->tid, &(event.parent), sizeof(struct rtgui_event_win));
  275. /* redraw title */
  276. if (topwin->title != RT_NULL)
  277. {
  278. rtgui_theme_draw_win(topwin);
  279. }
  280. }
  281. /* activate next window in the same layer as flag. The flag has many other
  282. * infomations but we only use the ONTOP/ONBTM */
  283. static void _rtgui_topwin_activate_next(enum rtgui_topwin_flag flag)
  284. {
  285. struct rtgui_topwin *topwin;
  286. topwin = _rtgui_topwin_get_topmost_window_shown(flag);
  287. if (topwin == RT_NULL)
  288. return;
  289. _rtgui_topwin_only_activate(topwin);
  290. }
  291. /* this function does not update the clip(to avoid doubel clipping). So if the
  292. * tree has changed, make sure it has already updated outside. */
  293. static void _rtgui_topwin_deactivate(struct rtgui_topwin *topwin)
  294. {
  295. struct rtgui_event_win event;
  296. RT_ASSERT(topwin != RT_NULL);
  297. RT_ASSERT(topwin->tid != RT_NULL);
  298. RTGUI_EVENT_WIN_DEACTIVATE_INIT(&event);
  299. event.wid = topwin->wid;
  300. rtgui_send(topwin->tid,
  301. &event.parent, sizeof(struct rtgui_event_win));
  302. topwin->flag &= ~WINTITLE_ACTIVATE;
  303. /* redraw title */
  304. if (topwin->title != RT_NULL)
  305. {
  306. rtgui_theme_draw_win(topwin);
  307. }
  308. }
  309. static void _rtgui_topwin_move_whole_tree2top(struct rtgui_topwin *topwin)
  310. {
  311. struct rtgui_topwin *topparent;
  312. RT_ASSERT(topwin != RT_NULL);
  313. /* move the whole tree */
  314. topparent = _rtgui_topwin_get_root_win(topwin);
  315. RT_ASSERT(topparent != RT_NULL);
  316. /* remove node from hidden list */
  317. rtgui_dlist_remove(&topparent->list);
  318. /* add node to show list */
  319. if (topwin->flag & WINTITLE_ONTOP)
  320. {
  321. rtgui_dlist_insert_after(&_rtgui_topwin_list, &(topparent->list));
  322. }
  323. else if (topwin->flag & WINTITLE_ONBTM)
  324. {
  325. /* botton layer window, before the fisrt bottom window or hidden window. */
  326. struct rtgui_topwin *ntopwin = get_topwin_from_list(&_rtgui_topwin_list);
  327. struct rtgui_dlist_node *node;
  328. rtgui_dlist_foreach(node, &_rtgui_topwin_list, next)
  329. {
  330. ntopwin = get_topwin_from_list(node);
  331. if ((ntopwin->flag & WINTITLE_ONBTM)
  332. || !(ntopwin->flag & WINTITLE_SHOWN))
  333. break;
  334. }
  335. /* all other windows are shown top/normal layer windows. Insert it as
  336. * the last window. */
  337. if (node == &_rtgui_topwin_list)
  338. rtgui_dlist_insert_before(&_rtgui_topwin_list, &(topparent->list));
  339. else
  340. rtgui_dlist_insert_before(&ntopwin->list, &(topparent->list));
  341. }
  342. else
  343. {
  344. /* normal layer window, before the fisrt shown normal layer window. */
  345. struct rtgui_topwin *ntopwin = get_topwin_from_list(&_rtgui_topwin_list);
  346. struct rtgui_dlist_node *node;
  347. rtgui_dlist_foreach(node, &_rtgui_topwin_list, next)
  348. {
  349. ntopwin = get_topwin_from_list(node);
  350. if (!((ntopwin->flag & WINTITLE_ONTOP)
  351. && (ntopwin->flag & WINTITLE_SHOWN)))
  352. break;
  353. }
  354. /* all other windows are shown top layer windows. Insert it as
  355. * the last window. */
  356. if (node == &_rtgui_topwin_list)
  357. rtgui_dlist_insert_before(&_rtgui_topwin_list, &(topparent->list));
  358. else
  359. rtgui_dlist_insert_before(&ntopwin->list, &(topparent->list));
  360. }
  361. }
  362. static void _rtgui_topwin_raise_in_sibling(struct rtgui_topwin *topwin)
  363. {
  364. struct rtgui_dlist_node *win_level;
  365. RT_ASSERT(topwin != RT_NULL);
  366. if (topwin->parent == RT_NULL)
  367. win_level = &_rtgui_topwin_list;
  368. else
  369. win_level = &topwin->parent->child_list;
  370. rtgui_dlist_remove(&topwin->list);
  371. rtgui_dlist_insert_after(win_level, &topwin->list);
  372. }
  373. /* it will do 2 things. One is moving the whole tree(the root of the tree) to
  374. * the front and the other is moving topwin to the front of it's siblings. */
  375. static void _rtgui_topwin_raise_tree_from_root(struct rtgui_topwin *topwin)
  376. {
  377. RT_ASSERT(topwin != RT_NULL);
  378. _rtgui_topwin_move_whole_tree2top(topwin);
  379. /* root win is aleady moved by _rtgui_topwin_move_whole_tree2top */
  380. if (!IS_ROOT_WIN(topwin))
  381. _rtgui_topwin_raise_in_sibling(topwin);
  382. }
  383. /* activate a win means:
  384. * - deactivate the old focus win if any
  385. * - raise the window to the front of it's siblings
  386. * - activate a win
  387. */
  388. rt_err_t rtgui_topwin_activate(struct rtgui_event_win_activate *event)
  389. {
  390. struct rtgui_topwin *topwin;
  391. RT_ASSERT(event);
  392. topwin = rtgui_topwin_search_in_list(event->wid, &_rtgui_topwin_list);
  393. if (topwin == RT_NULL)
  394. return -RT_ERROR;
  395. return rtgui_topwin_activate_topwin(topwin);
  396. }
  397. static void _rtgui_topwin_draw_tree(struct rtgui_topwin *topwin, struct rtgui_event_paint *epaint)
  398. {
  399. struct rtgui_dlist_node *node;
  400. if (topwin->title != RT_NULL)
  401. {
  402. rtgui_theme_draw_win(topwin);
  403. }
  404. epaint->wid = topwin->wid;
  405. rtgui_send(topwin->tid, &(epaint->parent), sizeof(struct rtgui_event_paint));
  406. rtgui_dlist_foreach(node, &topwin->child_list, prev)
  407. {
  408. if (!(get_topwin_from_list(node)->flag & WINTITLE_SHOWN))
  409. return;
  410. _rtgui_topwin_draw_tree(get_topwin_from_list(node), epaint);
  411. }
  412. }
  413. rt_err_t rtgui_topwin_activate_topwin(struct rtgui_topwin *topwin)
  414. {
  415. struct rtgui_topwin *old_focus_topwin;
  416. struct rtgui_event_paint epaint;
  417. RT_ASSERT(topwin != RT_NULL);
  418. RTGUI_EVENT_PAINT_INIT(&epaint);
  419. if (!(topwin->flag & WINTITLE_SHOWN))
  420. return -RT_ERROR;
  421. if (topwin->flag & WINTITLE_NOFOCUS)
  422. {
  423. /* just raise and show, not affect others. */
  424. _rtgui_topwin_raise_tree_from_root(topwin);
  425. rtgui_topwin_update_clip();
  426. _rtgui_topwin_draw_tree(topwin, &epaint);
  427. return RT_EOK;
  428. }
  429. if (topwin->flag & WINTITLE_ACTIVATE)
  430. return RT_EOK;
  431. old_focus_topwin = rtgui_topwin_get_focus();
  432. /* if topwin has the focus, it shoule have WINTITLE_ACTIVATE set and
  433. * returned above. */
  434. RT_ASSERT(old_focus_topwin != topwin);
  435. _rtgui_topwin_raise_tree_from_root(topwin);
  436. /* clip before active the window, so we could get right boarder region. */
  437. rtgui_topwin_update_clip();
  438. if (old_focus_topwin != RT_NULL)
  439. {
  440. /* deactivate the old focus window firstly, otherwise it will make the new
  441. * window invisible. */
  442. _rtgui_topwin_deactivate(old_focus_topwin);
  443. }
  444. _rtgui_topwin_only_activate(topwin);
  445. _rtgui_topwin_draw_tree(topwin, &epaint);
  446. return RT_EOK;
  447. }
  448. /* map func to the topwin tree in preorder.
  449. *
  450. * Remember that we are in a embedded system so write the @param func memory
  451. * efficiently.
  452. */
  453. rt_inline void _rtgui_topwin_preorder_map(struct rtgui_topwin *topwin, void (*func)(struct rtgui_topwin *))
  454. {
  455. struct rtgui_dlist_node *child;
  456. RT_ASSERT(topwin != RT_NULL);
  457. RT_ASSERT(func != RT_NULL);
  458. func(topwin);
  459. rtgui_dlist_foreach(child, &topwin->child_list, next)
  460. _rtgui_topwin_preorder_map(get_topwin_from_list(child), func);
  461. }
  462. rt_inline void _rtgui_topwin_mark_hidden(struct rtgui_topwin *topwin)
  463. {
  464. topwin->flag &= ~WINTITLE_SHOWN;
  465. if (topwin->title != RT_NULL)
  466. {
  467. RTGUI_WIDGET_HIDE(topwin->title);
  468. }
  469. RTGUI_WIDGET_HIDE(topwin->wid);
  470. }
  471. rt_inline void _rtgui_topwin_mark_shown(struct rtgui_topwin *topwin)
  472. {
  473. if (!(topwin->flag & WINTITLE_SHOWN)
  474. && RTGUI_WIDGET_IS_HIDE(topwin->wid))
  475. return;
  476. topwin->flag |= WINTITLE_SHOWN;
  477. if (topwin->title != RT_NULL)
  478. {
  479. RTGUI_WIDGET_UNHIDE(topwin->title);
  480. }
  481. RTGUI_WIDGET_UNHIDE(topwin->wid);
  482. }
  483. rt_err_t rtgui_topwin_show(struct rtgui_event_win *event)
  484. {
  485. struct rtgui_topwin *topwin;
  486. struct rtgui_win *wid = event->wid;
  487. topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_list);
  488. /* no such a window recorded */
  489. if (topwin == RT_NULL)
  490. return -RT_ERROR;
  491. /* if the parent is hidden, just mark it as shown. It will be shown when
  492. * the parent is shown. */
  493. if (!_rtgui_topwin_could_show(topwin))
  494. {
  495. topwin->flag |= WINTITLE_SHOWN;
  496. _rtgui_topwin_raise_in_sibling(topwin);
  497. return -RT_ERROR;
  498. }
  499. _rtgui_topwin_preorder_map(topwin, _rtgui_topwin_mark_shown);
  500. rtgui_topwin_activate_topwin(topwin);
  501. return RT_EOK;
  502. }
  503. static void _rtgui_topwin_clear_modal_tree(struct rtgui_topwin *topwin)
  504. {
  505. struct rtgui_dlist_node *node;
  506. RT_ASSERT(topwin != RT_NULL);
  507. RT_ASSERT(topwin->parent != RT_NULL);
  508. while (!IS_ROOT_WIN(topwin))
  509. {
  510. rtgui_dlist_foreach(node, &topwin->parent->child_list, next)
  511. get_topwin_from_list(node)->flag &= ~WINTITLE_MODALED;
  512. topwin = topwin->parent;
  513. }
  514. /* clear the modal flag of the root window */
  515. topwin->flag &= ~WINTITLE_MODALED;
  516. }
  517. /* hide a window */
  518. rt_err_t rtgui_topwin_hide(struct rtgui_event_win *event)
  519. {
  520. struct rtgui_topwin *topwin;
  521. struct rtgui_topwin *old_focus_topwin = rtgui_topwin_get_focus();
  522. struct rtgui_win *wid = event->wid;
  523. struct rtgui_dlist_node *containing_list;
  524. /* find in show list */
  525. topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_list);
  526. if (topwin == RT_NULL)
  527. {
  528. return -RT_ERROR;
  529. }
  530. if (!(topwin->flag & WINTITLE_SHOWN))
  531. {
  532. return RT_EOK;
  533. }
  534. old_focus_topwin = rtgui_topwin_get_focus();
  535. _rtgui_topwin_preorder_map(topwin, _rtgui_topwin_mark_hidden);
  536. if (topwin->parent == RT_NULL)
  537. containing_list = &_rtgui_topwin_list;
  538. else
  539. containing_list = &topwin->parent->child_list;
  540. rtgui_dlist_remove(&topwin->list);
  541. rtgui_dlist_insert_before(containing_list, &topwin->list);
  542. /* update clip info */
  543. rtgui_topwin_update_clip();
  544. /* redraw the old rect */
  545. rtgui_topwin_redraw(&(topwin->extent));
  546. if (topwin->flag & WINTITLE_MODALING)
  547. {
  548. _rtgui_topwin_clear_modal_tree(topwin);
  549. topwin->flag &= ~WINTITLE_MODALING;
  550. }
  551. if (old_focus_topwin == topwin)
  552. {
  553. _rtgui_topwin_activate_next(topwin->flag);
  554. }
  555. topwin->flag &= ~WINTITLE_ACTIVATE;
  556. return RT_EOK;
  557. }
  558. /* move top window */
  559. rt_err_t rtgui_topwin_move(struct rtgui_event_win_move *event)
  560. {
  561. struct rtgui_topwin *topwin;
  562. int dx, dy;
  563. rtgui_rect_t old_rect; /* the old topwin coverage area */
  564. struct rtgui_list_node *node;
  565. /* find in show list */
  566. topwin = rtgui_topwin_search_in_list(event->wid, &_rtgui_topwin_list);
  567. if (topwin == RT_NULL ||
  568. !(topwin->flag & WINTITLE_SHOWN))
  569. {
  570. return -RT_ERROR;
  571. }
  572. /* get the delta move x, y */
  573. dx = event->x - topwin->extent.x1;
  574. dy = event->y - topwin->extent.y1;
  575. old_rect = topwin->extent;
  576. /* move window rect */
  577. rtgui_rect_moveto(&(topwin->extent), dx, dy);
  578. /* move window title */
  579. if (topwin->title != RT_NULL)
  580. {
  581. old_rect = RTGUI_WIDGET(topwin->title)->extent;
  582. rtgui_widget_move_to_logic(RTGUI_WIDGET(topwin->title), dx, dy);
  583. }
  584. /* move the monitor rect list */
  585. rtgui_list_foreach(node, &(topwin->monitor_list))
  586. {
  587. struct rtgui_mouse_monitor *monitor = rtgui_list_entry(node,
  588. struct rtgui_mouse_monitor,
  589. list);
  590. rtgui_rect_moveto(&(monitor->rect), dx, dy);
  591. }
  592. /* update windows clip info */
  593. rtgui_topwin_update_clip();
  594. /* update old window coverage area */
  595. rtgui_topwin_redraw(&old_rect);
  596. /* update top window title */
  597. if (topwin->title != RT_NULL)
  598. rtgui_theme_draw_win(topwin);
  599. if (rtgui_rect_is_intersect(&old_rect, &(topwin->extent)) != RT_EOK)
  600. {
  601. /*
  602. * the old rect is not intersect with moved rect,
  603. * re-paint window
  604. */
  605. struct rtgui_event_paint epaint;
  606. RTGUI_EVENT_PAINT_INIT(&epaint);
  607. epaint.wid = topwin->wid;
  608. rtgui_send(topwin->tid, &(epaint.parent), sizeof(epaint));
  609. }
  610. return RT_EOK;
  611. }
  612. /*
  613. * resize a top win
  614. * Note: currently, only support resize hidden window
  615. */
  616. void rtgui_topwin_resize(struct rtgui_win *wid, rtgui_rect_t *rect)
  617. {
  618. struct rtgui_topwin *topwin;
  619. struct rtgui_region region;
  620. /* find in show list */
  621. topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_list);
  622. if (topwin == RT_NULL ||
  623. !(topwin->flag & WINTITLE_SHOWN))
  624. return;
  625. /* record the old rect */
  626. rtgui_region_init_with_extents(&region, &topwin->extent);
  627. /* union the new rect so this is the region we should redraw */
  628. rtgui_region_union_rect(&region, &region, rect);
  629. topwin->extent = *rect;
  630. if (topwin->title != RT_NULL)
  631. {
  632. /* get win extent */
  633. rtgui_rect_t rect = topwin->extent;
  634. /* add border rect */
  635. if (topwin->flag & WINTITLE_BORDER)
  636. {
  637. rtgui_rect_inflate(&rect, WINTITLE_BORDER_SIZE);
  638. }
  639. /* add title rect */
  640. if (!(topwin->flag & WINTITLE_NO))
  641. rect.y1 -= WINTITLE_HEIGHT;
  642. RTGUI_WIDGET(topwin->title)->extent = rect;
  643. }
  644. /* update windows clip info */
  645. rtgui_topwin_update_clip();
  646. /* update old window coverage area */
  647. rtgui_topwin_redraw(rtgui_region_extents(&region));
  648. }
  649. static struct rtgui_topwin *_rtgui_topwin_get_focus_from_list(struct rtgui_dlist_node *list)
  650. {
  651. struct rtgui_dlist_node *node;
  652. RT_ASSERT(list != RT_NULL);
  653. rtgui_dlist_foreach(node, list, next)
  654. {
  655. struct rtgui_topwin *child = get_topwin_from_list(node);
  656. if (child->flag & WINTITLE_ACTIVATE)
  657. return child;
  658. child = _rtgui_topwin_get_focus_from_list(&child->child_list);
  659. if (child != RT_NULL)
  660. return child;
  661. }
  662. return RT_NULL;
  663. }
  664. struct rtgui_topwin *rtgui_topwin_get_focus(void)
  665. {
  666. return _rtgui_topwin_get_focus_from_list(&_rtgui_topwin_list);
  667. }
  668. static struct rtgui_topwin *_rtgui_topwin_get_wnd_from_tree(struct rtgui_dlist_node *list,
  669. int x, int y,
  670. rt_bool_t exclude_modaled)
  671. {
  672. struct rtgui_dlist_node *node;
  673. struct rtgui_topwin *topwin, *target;
  674. RT_ASSERT(list != RT_NULL);
  675. rtgui_dlist_foreach(node, list, next)
  676. {
  677. topwin = get_topwin_from_list(node);
  678. if (!(topwin->flag & WINTITLE_SHOWN))
  679. break;
  680. /* if higher window have this point, return it */
  681. target = _rtgui_topwin_get_wnd_from_tree(&topwin->child_list, x, y, exclude_modaled);
  682. if (target != RT_NULL)
  683. return target;
  684. if (exclude_modaled && (topwin->flag & WINTITLE_MODALED))
  685. break;
  686. if ((topwin->title != RT_NULL) &&
  687. rtgui_rect_contains_point(&(RTGUI_WIDGET(topwin->title)->extent), x, y) == RT_EOK)
  688. {
  689. return topwin;
  690. }
  691. else if (rtgui_rect_contains_point(&(topwin->extent), x, y) == RT_EOK)
  692. {
  693. return topwin;
  694. }
  695. }
  696. return RT_NULL;
  697. }
  698. struct rtgui_topwin *rtgui_topwin_get_wnd(int x, int y)
  699. {
  700. return _rtgui_topwin_get_wnd_from_tree(&_rtgui_topwin_list, x, y, RT_FALSE);
  701. }
  702. struct rtgui_topwin *rtgui_topwin_get_wnd_no_modaled(int x, int y)
  703. {
  704. return _rtgui_topwin_get_wnd_from_tree(&_rtgui_topwin_list, x, y, RT_TRUE);
  705. }
  706. /* clip region from topwin, and the windows beneath it. */
  707. rt_inline void _rtgui_topwin_clip_to_region(struct rtgui_region *region,
  708. struct rtgui_topwin *topwin)
  709. {
  710. RT_ASSERT(region != RT_NULL);
  711. RT_ASSERT(topwin != RT_NULL);
  712. if (topwin->title != RT_NULL)
  713. {
  714. rtgui_region_reset(&(RTGUI_WIDGET(topwin->title)->clip),
  715. &(RTGUI_WIDGET(topwin->title)->extent));
  716. rtgui_region_intersect(&(RTGUI_WIDGET(topwin->title)->clip),
  717. &(RTGUI_WIDGET(topwin->title)->clip),
  718. region);
  719. rtgui_region_subtract_rect(&(RTGUI_WIDGET(topwin->title)->clip),
  720. &(RTGUI_WIDGET(topwin->title)->clip),
  721. &topwin->extent);
  722. }
  723. rtgui_region_reset(&RTGUI_WIDGET(topwin->wid)->clip,
  724. &RTGUI_WIDGET(topwin->wid)->extent);
  725. rtgui_region_intersect(&RTGUI_WIDGET(topwin->wid)->clip,
  726. &RTGUI_WIDGET(topwin->wid)->clip,
  727. region);
  728. }
  729. static void rtgui_topwin_update_clip(void)
  730. {
  731. struct rtgui_topwin *top;
  732. struct rtgui_event_clip_info eclip;
  733. /* Note that the region is a "female die", that means it's the region you
  734. * can paint to, not the region covered by others.
  735. */
  736. struct rtgui_region region_available;
  737. if (rtgui_dlist_isempty(&_rtgui_topwin_list) ||
  738. !(get_topwin_from_list(_rtgui_topwin_list.next)->flag & WINTITLE_SHOWN))
  739. return;
  740. RTGUI_EVENT_CLIP_INFO_INIT(&eclip);
  741. rtgui_region_init_rect(&region_available, 0, 0,
  742. rtgui_graphic_driver_get_default()->width,
  743. rtgui_graphic_driver_get_default()->height);
  744. /* from top to bottom. */
  745. top = _rtgui_topwin_get_topmost_window_shown(WINTITLE_ONTOP);
  746. /* 0 is normal layer */
  747. if (top == RT_NULL)
  748. top = _rtgui_topwin_get_topmost_window_shown(0);
  749. if (top == RT_NULL)
  750. top = _rtgui_topwin_get_topmost_window_shown(WINTITLE_ONBTM);
  751. while (top != RT_NULL)
  752. {
  753. /* clip the topwin */
  754. _rtgui_topwin_clip_to_region(&region_available, top);
  755. #if 0
  756. /* debug window clipping */
  757. rt_kprintf("clip %s ", top->wid->title);
  758. rtgui_region_dump(&region_available);
  759. rt_kprintf("\n");
  760. #endif
  761. /* update available region */
  762. if (top->title != RT_NULL)
  763. rtgui_region_subtract_rect(&region_available, &region_available, &RTGUI_WIDGET(top->title)->extent);
  764. else
  765. rtgui_region_subtract_rect(&region_available, &region_available, &top->extent);
  766. /* send clip event to destination window */
  767. eclip.wid = top->wid;
  768. rtgui_send(top->tid, &(eclip.parent), sizeof(struct rtgui_event_clip_info));
  769. /* move to next sibling tree */
  770. if (top->parent == RT_NULL)
  771. if (top->list.next != &_rtgui_topwin_list &&
  772. get_topwin_from_list(top->list.next)->flag & WINTITLE_SHOWN)
  773. top = _rtgui_topwin_get_topmost_child_shown(get_topwin_from_list(top->list.next));
  774. else
  775. break;
  776. /* move to next slibing topwin */
  777. else if (top->list.next != &top->parent->child_list &&
  778. get_topwin_from_list(top->list.next)->flag & WINTITLE_SHOWN)
  779. top = _rtgui_topwin_get_topmost_child_shown(get_topwin_from_list(top->list.next));
  780. /* level up */
  781. else
  782. top = top->parent;
  783. }
  784. }
  785. static void _rtgui_topwin_redraw_tree(struct rtgui_dlist_node *list,
  786. struct rtgui_rect *rect,
  787. struct rtgui_event_paint *epaint)
  788. {
  789. struct rtgui_dlist_node *node;
  790. RT_ASSERT(list != RT_NULL);
  791. RT_ASSERT(rect != RT_NULL);
  792. RT_ASSERT(epaint != RT_NULL);
  793. /* skip the hidden windows */
  794. rtgui_dlist_foreach(node, list, prev)
  795. {
  796. if (get_topwin_from_list(node)->flag & WINTITLE_SHOWN)
  797. break;
  798. }
  799. for (; node != list; node = node->prev)
  800. {
  801. struct rtgui_topwin *topwin;
  802. topwin = get_topwin_from_list(node);
  803. //FIXME: intersect with clip?
  804. if (rtgui_rect_is_intersect(rect, &(topwin->extent)) == RT_EOK)
  805. {
  806. epaint->wid = topwin->wid;
  807. rtgui_send(topwin->tid, &(epaint->parent), sizeof(*epaint));
  808. /* draw title */
  809. if (topwin->title != RT_NULL)
  810. {
  811. rtgui_theme_draw_win(topwin);
  812. }
  813. }
  814. _rtgui_topwin_redraw_tree(&topwin->child_list, rect, epaint);
  815. }
  816. }
  817. static void rtgui_topwin_redraw(struct rtgui_rect *rect)
  818. {
  819. struct rtgui_event_paint epaint;
  820. RTGUI_EVENT_PAINT_INIT(&epaint);
  821. epaint.wid = RT_NULL;
  822. _rtgui_topwin_redraw_tree(&_rtgui_topwin_list, rect, &epaint);
  823. }
  824. /* a window enter modal mode will modal all the sibling window and parent
  825. * window all along to the root window(which parent is RT_NULL or the desktop
  826. * window if there is). If a root window modals, there is nothing to do here.*/
  827. rt_err_t rtgui_topwin_modal_enter(struct rtgui_event_win_modal_enter *event)
  828. {
  829. struct rtgui_topwin *topwin, *parent_top;
  830. struct rtgui_dlist_node *node;
  831. topwin = rtgui_topwin_search_in_list(event->wid, &_rtgui_topwin_list);
  832. if (topwin == RT_NULL)
  833. return -RT_ERROR;
  834. if (IS_ROOT_WIN(topwin))
  835. return RT_EOK;
  836. parent_top = topwin->parent;
  837. /* modal window should be on top already */
  838. RT_ASSERT(get_topwin_from_list(parent_top->child_list.next) == topwin);
  839. while (!IS_ROOT_WIN(parent_top))
  840. {
  841. rtgui_dlist_foreach(node, &parent_top->child_list, next)
  842. get_topwin_from_list(node)->flag |= WINTITLE_MODALED;
  843. parent_top->flag |= WINTITLE_MODALED;
  844. parent_top = parent_top->parent;
  845. }
  846. /* mark root window as modaled */
  847. parent_top->flag |= WINTITLE_MODALED;
  848. topwin->flag &= ~WINTITLE_MODALED;
  849. topwin->flag |= WINTITLE_MODALING;
  850. return RT_EOK;
  851. }
  852. void rtgui_topwin_title_onmouse(struct rtgui_topwin *win, struct rtgui_event_mouse *event)
  853. {
  854. rtgui_rect_t rect;
  855. /* let window to process this mouse event */
  856. if (rtgui_rect_contains_point(&win->extent, event->x, event->y) == RT_EOK)
  857. {
  858. /* send mouse event to thread */
  859. rtgui_send(win->tid, &(event->parent), sizeof(struct rtgui_event_mouse));
  860. return;
  861. }
  862. /* get close button rect (device value) */
  863. rect.x1 = RTGUI_WIDGET(win->title)->extent.x2 - WINTITLE_BORDER_SIZE - WINTITLE_CB_WIDTH - 3;
  864. rect.y1 = RTGUI_WIDGET(win->title)->extent.y1 + WINTITLE_BORDER_SIZE + 3;
  865. rect.x2 = rect.x1 + WINTITLE_CB_WIDTH;
  866. rect.y2 = rect.y1 + WINTITLE_CB_HEIGHT;
  867. if (event->button & RTGUI_MOUSE_BUTTON_LEFT)
  868. {
  869. if (event->button & RTGUI_MOUSE_BUTTON_DOWN)
  870. {
  871. if (rtgui_rect_contains_point(&rect, event->x, event->y) == RT_EOK)
  872. {
  873. win->flag |= WINTITLE_CB_PRESSED;
  874. rtgui_theme_draw_win(win);
  875. }
  876. #ifdef RTGUI_USING_WINMOVE
  877. else
  878. {
  879. /* maybe move window */
  880. rtgui_winrect_set(win);
  881. }
  882. #endif
  883. }
  884. else if (win->flag & WINTITLE_CB_PRESSED && event->button & RTGUI_MOUSE_BUTTON_UP)
  885. {
  886. if (rtgui_rect_contains_point(&rect, event->x, event->y) == RT_EOK)
  887. {
  888. struct rtgui_event_win event;
  889. win->flag &= ~WINTITLE_CB_PRESSED;
  890. rtgui_theme_draw_win(win);
  891. /* send close event to window */
  892. RTGUI_EVENT_WIN_CLOSE_INIT(&event);
  893. event.wid = win->wid;
  894. rtgui_send(win->tid, &(event.parent), sizeof(struct rtgui_event_win));
  895. }
  896. }
  897. }
  898. }
  899. void rtgui_topwin_append_monitor_rect(struct rtgui_win *wid, rtgui_rect_t *rect)
  900. {
  901. struct rtgui_topwin *win;
  902. /* parameters check */
  903. if (wid == RT_NULL || rect == RT_NULL) return;
  904. /* find topwin */
  905. win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_list);
  906. if (win == RT_NULL)
  907. return;
  908. /* append rect to top window monitor rect list */
  909. rtgui_mouse_monitor_append(&(win->monitor_list), rect);
  910. }
  911. void rtgui_topwin_remove_monitor_rect(struct rtgui_win *wid, rtgui_rect_t *rect)
  912. {
  913. struct rtgui_topwin *win;
  914. /* parameters check */
  915. if (wid == RT_NULL || rect == RT_NULL)
  916. return;
  917. /* find topwin */
  918. win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_list);
  919. if (win == RT_NULL)
  920. return;
  921. /* remove rect from top window monitor rect list */
  922. rtgui_mouse_monitor_remove(&(win->monitor_list), rect);
  923. }
  924. static void _rtgui_topwin_dump(struct rtgui_topwin *topwin)
  925. {
  926. rt_kprintf("0x%p:%s,0x%x", topwin, topwin->wid->title, topwin->flag);
  927. }
  928. static void _rtgui_topwin_dump_tree(struct rtgui_topwin *topwin)
  929. {
  930. struct rtgui_dlist_node *node;
  931. _rtgui_topwin_dump(topwin);
  932. rt_kprintf("(");
  933. rtgui_dlist_foreach(node, &topwin->child_list, next)
  934. {
  935. _rtgui_topwin_dump_tree(get_topwin_from_list(node));
  936. }
  937. rt_kprintf(")");
  938. }
  939. void rtgui_topwin_dump_tree(void)
  940. {
  941. struct rtgui_dlist_node *node;
  942. rtgui_dlist_foreach(node, &_rtgui_topwin_list, next)
  943. {
  944. _rtgui_topwin_dump_tree(get_topwin_from_list(node));
  945. rt_kprintf("\n");
  946. }
  947. }
  948. #ifdef RT_USING_FINSH
  949. #include <finsh.h>
  950. void dump_tree()
  951. {
  952. rtgui_topwin_dump_tree();
  953. }
  954. FINSH_FUNCTION_EXPORT(dump_tree, dump rtgui topwin tree)
  955. #endif