topwin.c 30 KB

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