topwin.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  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(event->wid->title);
  128. #else
  129. topwin->title = rtgui_wintitle_create((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. rt_err_t rtgui_topwin_activate_topwin(struct rtgui_topwin* topwin)
  398. {
  399. struct rtgui_topwin *old_focus_topwin;
  400. RT_ASSERT(topwin != RT_NULL);
  401. if (!(topwin->flag & WINTITLE_SHOWN))
  402. return -RT_ERROR;
  403. if (topwin->flag & WINTITLE_NOFOCUS)
  404. {
  405. /* just raise it, not affect others. */
  406. _rtgui_topwin_raise_tree_from_root(topwin);
  407. /* update clip info */
  408. rtgui_topwin_update_clip();
  409. return RT_EOK;
  410. }
  411. if (topwin->flag & WINTITLE_ACTIVATE)
  412. return RT_EOK;
  413. old_focus_topwin = rtgui_topwin_get_focus();
  414. /* if topwin has the focus, it shoule have WINTITLE_ACTIVATE set and
  415. * returned above. */
  416. RT_ASSERT(old_focus_topwin != topwin);
  417. _rtgui_topwin_raise_tree_from_root(topwin);
  418. /* update clip info */
  419. rtgui_topwin_update_clip();
  420. if (old_focus_topwin != RT_NULL)
  421. {
  422. /* deactivate the old focus window firstly, otherwise it will make the new
  423. * window invisible. */
  424. _rtgui_topwin_deactivate(old_focus_topwin);
  425. }
  426. _rtgui_topwin_only_activate(topwin);
  427. return RT_EOK;
  428. }
  429. /* map func to the topwin tree in preorder.
  430. *
  431. * Remember that we are in a embedded system so write the @param func memory
  432. * efficiently.
  433. */
  434. rt_inline void _rtgui_topwin_preorder_map(struct rtgui_topwin *topwin, void (*func)(struct rtgui_topwin*))
  435. {
  436. struct rtgui_dlist_node *child;
  437. RT_ASSERT(topwin != RT_NULL);
  438. RT_ASSERT(func != RT_NULL);
  439. func(topwin);
  440. rtgui_dlist_foreach(child, &topwin->child_list, next)
  441. _rtgui_topwin_preorder_map(get_topwin_from_list(child), func);
  442. }
  443. rt_inline void _rtgui_topwin_mark_hidden(struct rtgui_topwin *topwin)
  444. {
  445. topwin->flag &= ~WINTITLE_SHOWN;
  446. if (topwin->title != RT_NULL)
  447. {
  448. RTGUI_WIDGET_HIDE(RTGUI_WIDGET(topwin->title));
  449. }
  450. RTGUI_WIDGET_HIDE(RTGUI_WIDGET(topwin->wid));
  451. }
  452. rt_inline void _rtgui_topwin_mark_shown(struct rtgui_topwin *topwin)
  453. {
  454. if (!(topwin->flag & WINTITLE_SHOWN)
  455. && RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(topwin->wid)))
  456. return;
  457. topwin->flag |= WINTITLE_SHOWN;
  458. if (topwin->title != RT_NULL)
  459. {
  460. RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(topwin->title));
  461. }
  462. RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(topwin->wid));
  463. }
  464. static void _rtgui_topwin_draw_tree(struct rtgui_topwin *topwin, struct rtgui_event_paint *epaint)
  465. {
  466. struct rtgui_dlist_node *node;
  467. if (topwin->title != RT_NULL)
  468. {
  469. rtgui_theme_draw_win(topwin);
  470. }
  471. epaint->wid = topwin->wid;
  472. rtgui_send(topwin->tid, &(epaint->parent), sizeof(struct rtgui_event_paint));
  473. rtgui_dlist_foreach(node, &topwin->child_list, prev)
  474. {
  475. if (!(get_topwin_from_list(node)->flag & WINTITLE_SHOWN))
  476. return;
  477. _rtgui_topwin_draw_tree(get_topwin_from_list(node), epaint);
  478. }
  479. }
  480. /* show the window tree. @param epaint can be initialized outside and reduce stack
  481. * usage. */
  482. static void _rtgui_topwin_show_tree(struct rtgui_topwin *topwin, struct rtgui_event_paint *epaint)
  483. {
  484. RT_ASSERT(topwin != RT_NULL);
  485. RT_ASSERT(epaint != RT_NULL);
  486. _rtgui_topwin_raise_tree_from_root(topwin);
  487. /* we have to mark the _all_ tree before update_clip because update_clip
  488. * will stop as hidden windows */
  489. _rtgui_topwin_preorder_map(topwin, _rtgui_topwin_mark_shown);
  490. // TODO: if all the window is shown already, there is no need to
  491. // update_clip. But since we use peorder_map, it seems it's a bit difficult
  492. // to tell whether @param topwin and it's children are all shown.
  493. rtgui_topwin_update_clip();
  494. _rtgui_topwin_draw_tree(topwin, epaint);
  495. }
  496. rt_err_t rtgui_topwin_show(struct rtgui_event_win* event)
  497. {
  498. struct rtgui_topwin *topwin, *old_focus;
  499. struct rtgui_win* wid = event->wid;
  500. struct rtgui_event_paint epaint;
  501. RTGUI_EVENT_PAINT_INIT(&epaint);
  502. topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_list);
  503. /* no such a window recorded */
  504. if (topwin == RT_NULL)
  505. return -RT_ERROR;
  506. /* if the parent is hidden, just mark it as shown. It will be shown when
  507. * the parent is shown. */
  508. if (!_rtgui_topwin_could_show(topwin))
  509. {
  510. topwin->flag |= WINTITLE_SHOWN;
  511. _rtgui_topwin_raise_in_sibling(topwin);
  512. return -RT_ERROR;
  513. }
  514. old_focus = rtgui_topwin_get_focus();
  515. _rtgui_topwin_show_tree(topwin, &epaint);
  516. /* we don't want double clipping(bare rtgui_topwin_activate_win will clip),
  517. * so we have to do deactivate/activate manually. */
  518. if (old_focus && old_focus != topwin)
  519. _rtgui_topwin_deactivate(old_focus);
  520. _rtgui_topwin_only_activate(topwin);
  521. return RT_EOK;
  522. }
  523. static void _rtgui_topwin_clear_modal_tree(struct rtgui_topwin *topwin)
  524. {
  525. struct rtgui_dlist_node *node;
  526. RT_ASSERT(topwin != RT_NULL);
  527. RT_ASSERT(topwin->parent != RT_NULL);
  528. while (!IS_ROOT_WIN(topwin))
  529. {
  530. rtgui_dlist_foreach(node, &topwin->parent->child_list, next)
  531. get_topwin_from_list(node)->flag &= ~WINTITLE_MODALED;
  532. topwin = topwin->parent;
  533. }
  534. /* clear the modal flag of the root window */
  535. topwin->flag &= ~WINTITLE_MODALED;
  536. }
  537. /* hide a window */
  538. rt_err_t rtgui_topwin_hide(struct rtgui_event_win* event)
  539. {
  540. struct rtgui_topwin *topwin;
  541. struct rtgui_topwin *old_focus_topwin = rtgui_topwin_get_focus();
  542. struct rtgui_win *wid = event->wid;
  543. struct rtgui_dlist_node *containing_list;
  544. /* find in show list */
  545. topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_list);
  546. if (topwin == RT_NULL)
  547. {
  548. return -RT_ERROR;
  549. }
  550. if (!(topwin->flag & WINTITLE_SHOWN))
  551. {
  552. return RT_EOK;
  553. }
  554. old_focus_topwin = rtgui_topwin_get_focus();
  555. _rtgui_topwin_preorder_map(topwin, _rtgui_topwin_mark_hidden);
  556. if (topwin->parent == RT_NULL)
  557. containing_list = &_rtgui_topwin_list;
  558. else
  559. containing_list = &topwin->parent->child_list;
  560. rtgui_dlist_remove(&topwin->list);
  561. rtgui_dlist_insert_before(containing_list, &topwin->list);
  562. /* update clip info */
  563. rtgui_topwin_update_clip();
  564. /* redraw the old rect */
  565. rtgui_topwin_redraw(&(topwin->extent));
  566. if (topwin->flag & WINTITLE_MODALING)
  567. {
  568. _rtgui_topwin_clear_modal_tree(topwin);
  569. topwin->flag &= ~WINTITLE_MODALING;
  570. }
  571. if (old_focus_topwin == topwin)
  572. {
  573. _rtgui_topwin_activate_next(topwin->flag);
  574. }
  575. return RT_EOK;
  576. }
  577. /* move top window */
  578. rt_err_t rtgui_topwin_move(struct rtgui_event_win_move* event)
  579. {
  580. struct rtgui_topwin* topwin;
  581. int dx, dy;
  582. rtgui_rect_t old_rect; /* the old topwin coverage area */
  583. struct rtgui_list_node* node;
  584. /* find in show list */
  585. topwin = rtgui_topwin_search_in_list(event->wid, &_rtgui_topwin_list);
  586. if (topwin == RT_NULL ||
  587. !(topwin->flag & WINTITLE_SHOWN))
  588. {
  589. return -RT_ERROR;
  590. }
  591. /* get the delta move x, y */
  592. dx = event->x - topwin->extent.x1;
  593. dy = event->y - topwin->extent.y1;
  594. old_rect = topwin->extent;
  595. /* move window rect */
  596. rtgui_rect_moveto(&(topwin->extent), dx, dy);
  597. /* move window title */
  598. if (topwin->title != RT_NULL)
  599. {
  600. old_rect = RTGUI_WIDGET(topwin->title)->extent;
  601. rtgui_widget_move_to_logic(RTGUI_WIDGET(topwin->title), dx, dy);
  602. }
  603. /* move the monitor rect list */
  604. rtgui_list_foreach(node, &(topwin->monitor_list))
  605. {
  606. struct rtgui_mouse_monitor* monitor = rtgui_list_entry(node,
  607. struct rtgui_mouse_monitor,
  608. list);
  609. rtgui_rect_moveto(&(monitor->rect), dx, dy);
  610. }
  611. /* update windows clip info */
  612. rtgui_topwin_update_clip();
  613. /* update old window coverage area */
  614. rtgui_topwin_redraw(&old_rect);
  615. /* update top window title */
  616. if (topwin->title != RT_NULL)
  617. rtgui_theme_draw_win(topwin);
  618. if (rtgui_rect_is_intersect(&old_rect, &(topwin->extent)) != RT_EOK)
  619. {
  620. /*
  621. * the old rect is not intersect with moved rect,
  622. * re-paint window
  623. */
  624. struct rtgui_event_paint epaint;
  625. RTGUI_EVENT_PAINT_INIT(&epaint);
  626. epaint.wid = topwin->wid;
  627. rtgui_send(topwin->tid, &(epaint.parent), sizeof(epaint));
  628. }
  629. return RT_EOK;
  630. }
  631. /*
  632. * resize a top win
  633. * Note: currently, only support resize hidden window
  634. */
  635. void rtgui_topwin_resize(struct rtgui_win* wid, rtgui_rect_t* rect)
  636. {
  637. struct rtgui_topwin* topwin;
  638. struct rtgui_region region;
  639. /* find in show list */
  640. topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_list);
  641. if (topwin == RT_NULL ||
  642. !(topwin->flag & WINTITLE_SHOWN))
  643. return;
  644. /* record the old rect */
  645. rtgui_region_init_with_extents(&region, &topwin->extent);
  646. /* union the new rect so this is the region we should redraw */
  647. rtgui_region_union_rect(&region, &region, rect);
  648. topwin->extent = *rect;
  649. if (topwin->title != RT_NULL)
  650. {
  651. /* get win extent */
  652. rtgui_rect_t rect = topwin->extent;
  653. /* add border rect */
  654. if (topwin->flag & WINTITLE_BORDER)
  655. {
  656. rtgui_rect_inflate(&rect, WINTITLE_BORDER_SIZE);
  657. }
  658. /* add title rect */
  659. if (!(topwin->flag & WINTITLE_NO))
  660. rect.y1 -= WINTITLE_HEIGHT;
  661. RTGUI_WIDGET(topwin->title)->extent = rect;
  662. }
  663. /* update windows clip info */
  664. rtgui_topwin_update_clip();
  665. /* update old window coverage area */
  666. rtgui_topwin_redraw(rtgui_region_extents(&region));
  667. }
  668. static struct rtgui_topwin* _rtgui_topwin_get_focus_from_list(struct rtgui_dlist_node *list)
  669. {
  670. struct rtgui_dlist_node *node;
  671. RT_ASSERT(list != RT_NULL);
  672. rtgui_dlist_foreach(node, list, next)
  673. {
  674. struct rtgui_topwin *child = get_topwin_from_list(node);
  675. if (child->flag & WINTITLE_ACTIVATE)
  676. return child;
  677. child = _rtgui_topwin_get_focus_from_list(&child->child_list);
  678. if (child != RT_NULL)
  679. return child;
  680. }
  681. return RT_NULL;
  682. }
  683. struct rtgui_topwin* rtgui_topwin_get_focus(void)
  684. {
  685. return _rtgui_topwin_get_focus_from_list(&_rtgui_topwin_list);
  686. }
  687. static struct rtgui_topwin* _rtgui_topwin_get_wnd_from_tree(struct rtgui_dlist_node *list,
  688. int x, int y,
  689. rt_bool_t exclude_modaled)
  690. {
  691. struct rtgui_dlist_node *node;
  692. struct rtgui_topwin *topwin, *target;
  693. RT_ASSERT(list != RT_NULL);
  694. rtgui_dlist_foreach(node, list, next)
  695. {
  696. topwin = get_topwin_from_list(node);
  697. if (!(topwin->flag & WINTITLE_SHOWN))
  698. break;
  699. /* if higher window have this point, return it */
  700. target = _rtgui_topwin_get_wnd_from_tree(&topwin->child_list, x, y, exclude_modaled);
  701. if (target != RT_NULL)
  702. return target;
  703. if (exclude_modaled && (topwin->flag & WINTITLE_MODALED))
  704. break;
  705. if ((topwin->title != RT_NULL) &&
  706. rtgui_rect_contains_point(&(RTGUI_WIDGET(topwin->title)->extent), x, y) == RT_EOK)
  707. {
  708. return topwin;
  709. }
  710. else if (rtgui_rect_contains_point(&(topwin->extent), x, y) == RT_EOK)
  711. {
  712. return topwin;
  713. }
  714. }
  715. return RT_NULL;
  716. }
  717. struct rtgui_topwin* rtgui_topwin_get_wnd(int x, int y)
  718. {
  719. return _rtgui_topwin_get_wnd_from_tree(&_rtgui_topwin_list, x, y, RT_FALSE);
  720. }
  721. struct rtgui_topwin* rtgui_topwin_get_wnd_no_modaled(int x, int y)
  722. {
  723. return _rtgui_topwin_get_wnd_from_tree(&_rtgui_topwin_list, x, y, RT_TRUE);
  724. }
  725. /* clip region from topwin, and the windows beneath it. */
  726. rt_inline void _rtgui_topwin_clip_to_region(struct rtgui_region *region,
  727. struct rtgui_topwin *topwin)
  728. {
  729. RT_ASSERT(region != RT_NULL);
  730. RT_ASSERT(topwin != RT_NULL);
  731. if (topwin->title != RT_NULL)
  732. {
  733. rtgui_region_reset(&(RTGUI_WIDGET(topwin->title)->clip),
  734. &(RTGUI_WIDGET(topwin->title)->extent));
  735. rtgui_region_intersect(&(RTGUI_WIDGET(topwin->title)->clip),
  736. &(RTGUI_WIDGET(topwin->title)->clip),
  737. region);
  738. rtgui_region_subtract_rect(&(RTGUI_WIDGET(topwin->title)->clip),
  739. &(RTGUI_WIDGET(topwin->title)->clip),
  740. &topwin->extent);
  741. }
  742. rtgui_region_reset(&RTGUI_WIDGET(topwin->wid)->clip,
  743. &RTGUI_WIDGET(topwin->wid)->extent);
  744. rtgui_region_intersect(&RTGUI_WIDGET(topwin->wid)->clip,
  745. &RTGUI_WIDGET(topwin->wid)->clip,
  746. region);
  747. }
  748. static void rtgui_topwin_update_clip(void)
  749. {
  750. struct rtgui_topwin *top;
  751. struct rtgui_event_clip_info eclip;
  752. /* Note that the region is a "female die", that means it's the region you
  753. * can paint to, not the region covered by others.
  754. */
  755. struct rtgui_region region_available;
  756. if (rtgui_dlist_isempty(&_rtgui_topwin_list) ||
  757. !(get_topwin_from_list(_rtgui_topwin_list.next)->flag & WINTITLE_SHOWN))
  758. return;
  759. RTGUI_EVENT_CLIP_INFO_INIT(&eclip);
  760. rtgui_region_init_rect(&region_available, 0, 0,
  761. rtgui_graphic_driver_get_default()->width,
  762. rtgui_graphic_driver_get_default()->height);
  763. /* from top to bottom. */
  764. top = _rtgui_topwin_get_topmost_window_shown(WINTITLE_ONTOP);
  765. /* 0 is normal layer */
  766. if (top == RT_NULL)
  767. top = _rtgui_topwin_get_topmost_window_shown(0);
  768. if (top == RT_NULL)
  769. top = _rtgui_topwin_get_topmost_window_shown(WINTITLE_ONBTM);
  770. while (top != RT_NULL)
  771. {
  772. /* clip the topwin */
  773. _rtgui_topwin_clip_to_region(&region_available, top);
  774. #if 0
  775. /* debug window clipping */
  776. rt_kprintf("clip %s ", top->wid->title);
  777. rtgui_region_dump(&region_available);
  778. rt_kprintf("\n");
  779. #endif
  780. /* update available region */
  781. if (top->title != RT_NULL)
  782. rtgui_region_subtract_rect(&region_available, &region_available, &RTGUI_WIDGET(top->title)->extent);
  783. else
  784. rtgui_region_subtract_rect(&region_available, &region_available, &top->extent);
  785. /* send clip event to destination window */
  786. eclip.wid = top->wid;
  787. rtgui_send(top->tid, &(eclip.parent), sizeof(struct rtgui_event_clip_info));
  788. /* move to next sibling tree */
  789. if (top->parent == RT_NULL)
  790. if (top->list.next != &_rtgui_topwin_list &&
  791. get_topwin_from_list(top->list.next)->flag & WINTITLE_SHOWN)
  792. top = _rtgui_topwin_get_topmost_child_shown(get_topwin_from_list(top->list.next));
  793. else
  794. break;
  795. /* move to next slibing topwin */
  796. else if (top->list.next != &top->parent->child_list &&
  797. get_topwin_from_list(top->list.next)->flag & WINTITLE_SHOWN)
  798. top = _rtgui_topwin_get_topmost_child_shown(get_topwin_from_list(top->list.next));
  799. /* level up */
  800. else
  801. top = top->parent;
  802. }
  803. }
  804. static void _rtgui_topwin_redraw_tree(struct rtgui_dlist_node *list,
  805. struct rtgui_rect* rect,
  806. struct rtgui_event_paint *epaint)
  807. {
  808. struct rtgui_dlist_node *node;
  809. RT_ASSERT(list != RT_NULL);
  810. RT_ASSERT(rect != RT_NULL);
  811. RT_ASSERT(epaint != RT_NULL);
  812. /* skip the hidden windows */
  813. rtgui_dlist_foreach(node, list, prev)
  814. {
  815. if (get_topwin_from_list(node)->flag & WINTITLE_SHOWN)
  816. break;
  817. }
  818. for (; node != list; node = node->prev)
  819. {
  820. struct rtgui_topwin *topwin;
  821. topwin = get_topwin_from_list(node);
  822. //FIXME: intersect with clip?
  823. if (rtgui_rect_is_intersect(rect, &(topwin->extent)) == RT_EOK)
  824. {
  825. epaint->wid = topwin->wid;
  826. rtgui_send(topwin->tid, &(epaint->parent), sizeof(*epaint));
  827. /* draw title */
  828. if (topwin->title != RT_NULL)
  829. {
  830. rtgui_theme_draw_win(topwin);
  831. }
  832. }
  833. _rtgui_topwin_redraw_tree(&topwin->child_list, rect, epaint);
  834. }
  835. }
  836. static void rtgui_topwin_redraw(struct rtgui_rect* rect)
  837. {
  838. struct rtgui_event_paint epaint;
  839. RTGUI_EVENT_PAINT_INIT(&epaint);
  840. epaint.wid = RT_NULL;
  841. _rtgui_topwin_redraw_tree(&_rtgui_topwin_list, rect, &epaint);
  842. }
  843. /* a window enter modal mode will modal all the sibling window and parent
  844. * window all along to the root window(which parent is RT_NULL or the desktop
  845. * window if there is). If a root window modals, there is nothing to do here.*/
  846. rt_err_t rtgui_topwin_modal_enter(struct rtgui_event_win_modal_enter* event)
  847. {
  848. struct rtgui_topwin *topwin, *parent_top;
  849. struct rtgui_dlist_node *node;
  850. topwin = rtgui_topwin_search_in_list(event->wid, &_rtgui_topwin_list);
  851. if (topwin == RT_NULL)
  852. return -RT_ERROR;
  853. if (IS_ROOT_WIN(topwin))
  854. return RT_EOK;
  855. parent_top = topwin->parent;
  856. /* modal window should be on top already */
  857. RT_ASSERT(get_topwin_from_list(parent_top->child_list.next) == topwin);
  858. while (!IS_ROOT_WIN(parent_top))
  859. {
  860. rtgui_dlist_foreach(node, &parent_top->child_list, next)
  861. get_topwin_from_list(node)->flag |= WINTITLE_MODALED;
  862. parent_top->flag |= WINTITLE_MODALED;
  863. parent_top = parent_top->parent;
  864. }
  865. /* mark root window as modaled */
  866. parent_top->flag |= WINTITLE_MODALED;
  867. topwin->flag &= ~WINTITLE_MODALED;
  868. topwin->flag |= WINTITLE_MODALING;
  869. return RT_EOK;
  870. }
  871. void rtgui_topwin_title_onmouse(struct rtgui_topwin* win, struct rtgui_event_mouse* event)
  872. {
  873. rtgui_rect_t rect;
  874. /* let window to process this mouse event */
  875. if (rtgui_rect_contains_point(&win->extent, event->x, event->y) == RT_EOK)
  876. {
  877. /* send mouse event to thread */
  878. rtgui_send(win->tid, &(event->parent), sizeof(struct rtgui_event_mouse));
  879. return;
  880. }
  881. /* get close button rect (device value) */
  882. rect.x1 = RTGUI_WIDGET(win->title)->extent.x2 - WINTITLE_BORDER_SIZE - WINTITLE_CB_WIDTH - 3;
  883. rect.y1 = RTGUI_WIDGET(win->title)->extent.y1 + WINTITLE_BORDER_SIZE + 3;
  884. rect.x2 = rect.x1 + WINTITLE_CB_WIDTH;
  885. rect.y2 = rect.y1 + WINTITLE_CB_HEIGHT;
  886. if (event->button & RTGUI_MOUSE_BUTTON_LEFT)
  887. {
  888. if (event->button & RTGUI_MOUSE_BUTTON_DOWN)
  889. {
  890. if (rtgui_rect_contains_point(&rect, event->x, event->y) == RT_EOK)
  891. {
  892. win->flag |= WINTITLE_CB_PRESSED;
  893. rtgui_theme_draw_win(win);
  894. }
  895. #ifdef RTGUI_USING_WINMOVE
  896. else
  897. {
  898. /* maybe move window */
  899. rtgui_winrect_set(win);
  900. }
  901. #endif
  902. }
  903. else if (win->flag & WINTITLE_CB_PRESSED && event->button & RTGUI_MOUSE_BUTTON_UP)
  904. {
  905. if (rtgui_rect_contains_point(&rect, event->x, event->y) == RT_EOK)
  906. {
  907. struct rtgui_event_win event;
  908. win->flag &= ~WINTITLE_CB_PRESSED;
  909. rtgui_theme_draw_win(win);
  910. /* send close event to window */
  911. RTGUI_EVENT_WIN_CLOSE_INIT(&event);
  912. event.wid = win->wid;
  913. rtgui_send(win->tid, &(event.parent), sizeof(struct rtgui_event_win));
  914. }
  915. }
  916. }
  917. }
  918. void rtgui_topwin_append_monitor_rect(struct rtgui_win* wid, rtgui_rect_t* rect)
  919. {
  920. struct rtgui_topwin* win;
  921. /* parameters check */
  922. if (wid == RT_NULL || rect == RT_NULL) return;
  923. /* find topwin */
  924. win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_list);
  925. if (win == RT_NULL)
  926. return;
  927. /* append rect to top window monitor rect list */
  928. rtgui_mouse_monitor_append(&(win->monitor_list), rect);
  929. }
  930. void rtgui_topwin_remove_monitor_rect(struct rtgui_win* wid, rtgui_rect_t* rect)
  931. {
  932. struct rtgui_topwin* win;
  933. /* parameters check */
  934. if (wid == RT_NULL || rect == RT_NULL)
  935. return;
  936. /* find topwin */
  937. win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_list);
  938. if (win == RT_NULL)
  939. return;
  940. /* remove rect from top window monitor rect list */
  941. rtgui_mouse_monitor_remove(&(win->monitor_list), rect);
  942. }
  943. static void _rtgui_topwin_dump(struct rtgui_topwin *topwin)
  944. {
  945. rt_kprintf("0x%p:%s,0x%x", topwin, topwin->wid->title, topwin->flag);
  946. }
  947. static void _rtgui_topwin_dump_tree(struct rtgui_topwin *topwin)
  948. {
  949. struct rtgui_dlist_node *node;
  950. _rtgui_topwin_dump(topwin);
  951. rt_kprintf("(");
  952. rtgui_dlist_foreach(node, &topwin->child_list, next)
  953. {
  954. _rtgui_topwin_dump_tree(get_topwin_from_list(node));
  955. }
  956. rt_kprintf(")");
  957. }
  958. void rtgui_topwin_dump_tree(void)
  959. {
  960. struct rtgui_dlist_node *node;
  961. rtgui_dlist_foreach(node, &_rtgui_topwin_list, next)
  962. {
  963. _rtgui_topwin_dump_tree(get_topwin_from_list(node));
  964. rt_kprintf("\n");
  965. }
  966. }
  967. #ifdef RT_USING_FINSH
  968. #include <finsh.h>
  969. void dump_tree()
  970. {
  971. rtgui_topwin_dump_tree();
  972. }
  973. FINSH_FUNCTION_EXPORT(dump_tree, dump rtgui topwin tree)
  974. #endif