topwin.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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 "panel.h"
  15. #include "topwin.h"
  16. #include "mouse.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/widgets/window.h>
  22. struct rtgui_topwin* rtgui_server_focus_topwin = RT_NULL;
  23. static struct rtgui_list_node _rtgui_topwin_show_list;
  24. static struct rtgui_list_node _rtgui_topwin_hide_list;
  25. static struct rt_semaphore _rtgui_topwin_lock;
  26. static void rtgui_topwin_update_clip(void);
  27. static void rtgui_topwin_redraw(struct rtgui_rect* rect);
  28. void rtgui_topwin_init()
  29. {
  30. /* init window list */
  31. rtgui_list_init(&_rtgui_topwin_show_list);
  32. rtgui_list_init(&_rtgui_topwin_hide_list);
  33. rt_sem_init(&_rtgui_topwin_lock,
  34. "topwin", 1, RT_IPC_FLAG_FIFO);
  35. }
  36. static struct rtgui_topwin*
  37. rtgui_topwin_search_in_list(struct rtgui_win* wid, struct rtgui_list_node* list)
  38. {
  39. struct rtgui_list_node* node;
  40. struct rtgui_topwin* topwin;
  41. /* search in list */
  42. rtgui_list_foreach(node, list)
  43. {
  44. topwin = rtgui_list_entry(node, struct rtgui_topwin, list);
  45. /* is this node? */
  46. if (topwin->wid == wid)
  47. {
  48. return topwin;
  49. }
  50. }
  51. return RT_NULL;
  52. }
  53. /* add a window to window list[hide] */
  54. rt_err_t rtgui_topwin_add(struct rtgui_event_win_create* event)
  55. {
  56. struct rtgui_topwin* topwin;
  57. topwin = rtgui_malloc(sizeof(struct rtgui_topwin));
  58. if (topwin == RT_NULL) return -RT_ERROR;
  59. topwin->wid = event->wid;
  60. topwin->extent = event->extent;
  61. topwin->tid = event->parent.sender;
  62. topwin->flag = 0;
  63. if (event->flag & RTGUI_WIN_STYLE_NO_TITLE) topwin->flag |= WINTITLE_NO;
  64. if (event->flag & RTGUI_WIN_STYLE_CLOSEBOX) topwin->flag |= WINTITLE_CLOSEBOX;
  65. if (!(event->flag & RTGUI_WIN_STYLE_NO_BORDER)) topwin->flag |= WINTITLE_BORDER;
  66. if (event->flag & RTGUI_WIN_STYLE_NO_FOCUS) topwin->flag |= WINTITLE_NOFOCUS;
  67. if(!(topwin->flag & WINTITLE_NO) || (topwin->flag & WINTITLE_BORDER))
  68. {
  69. /* get win extent */
  70. rtgui_rect_t rect = topwin->extent;
  71. /* add border rect */
  72. if (topwin->flag & WINTITLE_BORDER)
  73. {
  74. rtgui_rect_inflate(&rect, WINTITLE_BORDER_SIZE);
  75. }
  76. /* add title rect */
  77. if (!(topwin->flag & WINTITLE_NO)) rect.y1 -= WINTITLE_HEIGHT;
  78. topwin->title = rtgui_wintitle_create(event->title);
  79. rtgui_widget_set_rect(RTGUI_WIDGET(topwin->title), &rect);
  80. /* update clip info */
  81. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(topwin->title));
  82. rtgui_region_subtract_rect(&(RTGUI_WIDGET(topwin->title)->clip),
  83. &(RTGUI_WIDGET(topwin->title)->clip),
  84. &(topwin->extent));
  85. }
  86. else topwin->title = RT_NULL;
  87. rtgui_list_init(&topwin->list);
  88. rtgui_list_init(&topwin->monitor_list);
  89. /* add topwin node to the hidden window list */
  90. rtgui_list_insert(&(_rtgui_topwin_hide_list), &(topwin->list));
  91. return RT_EOK;
  92. }
  93. rt_err_t rtgui_topwin_remove(struct rtgui_win* wid)
  94. {
  95. struct rtgui_topwin* topwin;
  96. /* find the topwin node */
  97. topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
  98. if (topwin)
  99. {
  100. /* remove node from list */
  101. rtgui_list_remove(&_rtgui_topwin_show_list, &(topwin->list));
  102. rtgui_topwin_update_clip();
  103. /* redraw the old rect */
  104. rtgui_topwin_redraw(&(topwin->extent));
  105. if (rtgui_server_focus_topwin == topwin)
  106. {
  107. /* activate the next window */
  108. if (_rtgui_topwin_show_list.next != RT_NULL)
  109. {
  110. struct rtgui_event_win wevent;
  111. struct rtgui_topwin* wnd;
  112. /* get the topwin */
  113. wnd = rtgui_list_entry(_rtgui_topwin_show_list.next,
  114. struct rtgui_topwin, list);
  115. /* activate the window */
  116. RTGUI_EVENT_WIN_ACTIVATE_INIT(&wevent);
  117. wevent.wid = wnd->wid;
  118. rtgui_thread_send(wnd->tid, &(wevent.parent), sizeof(struct rtgui_event_win));
  119. /* set new focus topwin */
  120. rtgui_server_focus_topwin = wnd;
  121. }
  122. else
  123. {
  124. /* there is no shown window right now */
  125. rtgui_server_focus_topwin = RT_NULL;
  126. }
  127. }
  128. /* free the monitor rect list, topwin node and title */
  129. while (topwin->monitor_list.next != RT_NULL)
  130. {
  131. struct rtgui_mouse_monitor* monitor = rtgui_list_entry(topwin->monitor_list.next,
  132. struct rtgui_mouse_monitor, list);
  133. topwin->monitor_list.next = topwin->monitor_list.next->next;
  134. rtgui_free(monitor);
  135. }
  136. /* destroy win title */
  137. rtgui_wintitle_destroy(topwin->title);
  138. topwin->title = RT_NULL;
  139. rtgui_free(topwin);
  140. return RT_EOK;
  141. }
  142. topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list);
  143. if (topwin)
  144. {
  145. /* remove node from list */
  146. rtgui_list_remove(&_rtgui_topwin_hide_list, &(topwin->list));
  147. /* free the topwin node and title */
  148. rtgui_wintitle_destroy(topwin->title);
  149. topwin->title = RT_NULL;
  150. rtgui_free(topwin);
  151. return RT_EOK;
  152. }
  153. return -RT_ERROR;
  154. }
  155. /* activate a win
  156. * - deactivate the old focus win
  157. * - activate a win
  158. * - set the focus win to activate win
  159. * - draw win title
  160. */
  161. void rtgui_topwin_activate_win(struct rtgui_topwin* win)
  162. {
  163. struct rtgui_event_win event;
  164. /* activate the raised window */
  165. RTGUI_EVENT_WIN_ACTIVATE_INIT(&event);
  166. event.wid = win->wid;
  167. rtgui_thread_send(win->tid, &(event.parent), sizeof(struct rtgui_event_win));
  168. /* redraw title */
  169. if (win->title != RT_NULL)
  170. {
  171. win->flag |= WINTITLE_ACTIVATE;
  172. rtgui_theme_draw_win(win);
  173. }
  174. if (rtgui_server_focus_topwin != RT_NULL)
  175. {
  176. /* deactivate the old focus win */
  177. RTGUI_EVENT_WIN_DEACTIVATE_INIT(&event);
  178. event.wid = rtgui_server_focus_topwin->wid;
  179. rtgui_thread_send(rtgui_server_focus_topwin->tid,
  180. &event.parent, sizeof(struct rtgui_event_win));
  181. /* redraw title */
  182. if (rtgui_server_focus_topwin->title != RT_NULL)
  183. {
  184. rtgui_server_focus_topwin->flag &= ~WINTITLE_ACTIVATE;
  185. rtgui_theme_draw_win(rtgui_server_focus_topwin);
  186. }
  187. }
  188. rtgui_server_focus_topwin = win;
  189. }
  190. /*
  191. * deactivate a win
  192. * - deactivate the win
  193. * - redraw win title
  194. * - set rtgui_server_focus_topwin
  195. */
  196. void rtgui_topwin_deactivate_win(struct rtgui_topwin* win)
  197. {
  198. /* deactivate win */
  199. struct rtgui_event_win event;
  200. RTGUI_EVENT_WIN_DEACTIVATE_INIT(&event);
  201. event.wid = win->wid;
  202. rtgui_thread_send(win->tid,
  203. &event.parent, sizeof(struct rtgui_event_win));
  204. win->flag &= ~WINTITLE_ACTIVATE;
  205. rtgui_theme_draw_win(win);
  206. if (rtgui_server_focus_topwin == win)
  207. {
  208. rtgui_server_focus_topwin = RT_NULL;
  209. }
  210. }
  211. /* raise window to front */
  212. #ifdef RTGUI_USING_SMALL_SIZE
  213. void rtgui_topwin_raise(struct rtgui_win* wid, rt_thread_t sender)
  214. {
  215. struct rtgui_topwin* topwin;
  216. /* find the topwin node */
  217. topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
  218. if (topwin)
  219. {
  220. rt_int32_t count;
  221. struct rtgui_list_node* node;
  222. struct rtgui_event_clip_info eclip;
  223. RTGUI_EVENT_CLIP_INFO_INIT(&eclip);
  224. /* the window is already placed in front */
  225. if (&(topwin->list) == _rtgui_topwin_show_list.next)
  226. {
  227. rtgui_server_focus_topwin = RT_NULL;
  228. rtgui_topwin_activate_win(topwin);
  229. return ;
  230. }
  231. /* remove node from list */
  232. rtgui_list_remove(&_rtgui_topwin_show_list, &(topwin->list));
  233. /* add to front */
  234. rtgui_list_insert(&_rtgui_topwin_show_list, &(topwin->list));
  235. /* send clip info event */
  236. count = 0;
  237. for (node = _rtgui_topwin_show_list.next;
  238. node != &(topwin->list);
  239. node = node->next)
  240. {
  241. struct rtgui_topwin* wnd = rtgui_list_entry(node, struct rtgui_topwin, list);
  242. eclip.num_rect = count;
  243. eclip.wid = wnd->wid;
  244. count ++;
  245. /* send to destination window */
  246. rtgui_thread_send(wnd->tid, &(eclip.parent), sizeof(struct rtgui_event_clip_info));
  247. /* reset clip info in title */
  248. if (wnd->title != RT_NULL)
  249. {
  250. rtgui_toplevel_handle_clip(RTGUI_TOPLEVEL(wnd->title), &eclip);
  251. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(wnd->title));
  252. rtgui_region_subtract_rect(&(RTGUI_WIDGET(wnd->title)->clip),
  253. &(RTGUI_WIDGET(wnd->title)->clip),
  254. &(wnd->extent));
  255. }
  256. }
  257. rtgui_topwin_activate_win(topwin);
  258. }
  259. }
  260. #else
  261. void rtgui_topwin_raise(struct rtgui_win* wid, rt_thread_t sender)
  262. {
  263. struct rtgui_topwin* topwin;
  264. /* find the topwin node */
  265. topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
  266. if (topwin)
  267. {
  268. rt_int32_t count;
  269. struct rtgui_list_node* node;
  270. struct rtgui_event_clip_info* eclip;
  271. struct rtgui_rect* rect;
  272. /* the window is already placed in front */
  273. if (&(topwin->list) == _rtgui_topwin_show_list.next)
  274. {
  275. rtgui_server_focus_topwin = RT_NULL;
  276. rtgui_topwin_activate_win(topwin);
  277. return ;
  278. }
  279. /* update clip info */
  280. count = 0;
  281. node = _rtgui_topwin_show_list.next;
  282. while (node != &(topwin->list))
  283. {
  284. count ++;
  285. node = node->next;
  286. }
  287. eclip = (struct rtgui_event_clip_info*)rtgui_malloc(sizeof(struct rtgui_event_clip_info)
  288. + (count + 1)* sizeof(struct rtgui_rect));
  289. /* reset clip info to top window */
  290. RTGUI_EVENT_CLIP_INFO_INIT(eclip);
  291. eclip->num_rect = 0;
  292. eclip->wid = topwin->wid;
  293. /* send to destination window */
  294. rtgui_thread_send(topwin->tid, &(eclip->parent), sizeof(struct rtgui_event_clip_info));
  295. /* reset clip info in title */
  296. if (topwin->title != RT_NULL)
  297. {
  298. rtgui_toplevel_handle_clip(RTGUI_TOPLEVEL(topwin->title), eclip);
  299. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(topwin->title));
  300. rtgui_region_subtract_rect(&(RTGUI_WIDGET(topwin->title)->clip),
  301. &(RTGUI_WIDGET(topwin->title)->clip),
  302. &(topwin->extent));
  303. }
  304. rect = RTGUI_EVENT_GET_RECT(eclip, 0);
  305. *rect = (topwin->title != RT_NULL)? RTGUI_WIDGET(topwin->title)->extent : topwin->extent;
  306. count = 1;
  307. for (node = _rtgui_topwin_show_list.next;
  308. node != &(topwin->list);
  309. node = node->next)
  310. {
  311. struct rtgui_topwin* wnd;
  312. wnd = rtgui_list_entry(node, struct rtgui_topwin, list);
  313. eclip->num_rect = count;
  314. eclip->wid = wnd->wid;
  315. /* send to destination window */
  316. rtgui_thread_send(wnd->tid, &(eclip->parent),
  317. sizeof(struct rtgui_event_clip_info) + count * sizeof(struct rtgui_rect));
  318. /* reset clip info in title */
  319. if (wnd->title != RT_NULL)
  320. {
  321. rtgui_toplevel_handle_clip(RTGUI_TOPLEVEL(wnd->title), eclip);
  322. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(wnd->title));
  323. rtgui_region_subtract_rect(&(RTGUI_WIDGET(wnd->title)->clip),
  324. &(RTGUI_WIDGET(wnd->title)->clip),
  325. &(wnd->extent));
  326. }
  327. rect = RTGUI_EVENT_GET_RECT(eclip, count++);
  328. *rect = (wnd->title != RT_NULL)? RTGUI_WIDGET(wnd->title)->extent : wnd->extent;
  329. }
  330. /* release clip info event */
  331. rtgui_free(eclip);
  332. /* remove node from list */
  333. rtgui_list_remove(&_rtgui_topwin_show_list, &(topwin->list));
  334. /* add to front */
  335. rtgui_list_insert(&_rtgui_topwin_show_list, &(topwin->list));
  336. rtgui_topwin_activate_win(topwin);
  337. }
  338. }
  339. #endif
  340. /* show a window */
  341. void rtgui_topwin_show(struct rtgui_event_win* event)
  342. {
  343. struct rtgui_topwin* topwin;
  344. struct rtgui_win* wid = event->wid;
  345. rt_thread_t sender = RTGUI_EVENT(event)->sender;
  346. /* find in hide list */
  347. topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list);
  348. /* find it */
  349. if (topwin != RT_NULL)
  350. {
  351. /* remove node from hidden list */
  352. rtgui_list_remove(&_rtgui_topwin_hide_list, &(topwin->list));
  353. /* add node to show list */
  354. rtgui_list_insert(&_rtgui_topwin_show_list, &(topwin->list));
  355. /* show window title */
  356. if (topwin->title != RT_NULL)
  357. {
  358. RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(topwin->title));
  359. }
  360. /* update clip info */
  361. rtgui_topwin_update_clip();
  362. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
  363. /* activate this window */
  364. rtgui_topwin_activate_win(topwin);
  365. }
  366. else
  367. {
  368. /* the wnd is located in show list, raise wnd to front */
  369. topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
  370. if (topwin != RT_NULL)
  371. {
  372. if (_rtgui_topwin_show_list.next != &(topwin->list))
  373. {
  374. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
  375. /* not the front window, raise it */
  376. rtgui_topwin_raise(wid, sender);
  377. }
  378. }
  379. else
  380. {
  381. /* there is no wnd in wnd list */
  382. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_ERROR);
  383. }
  384. }
  385. }
  386. /* send clip info to panel */
  387. #ifdef RTGUI_USING_SMALL_SIZE
  388. void rtgui_topwin_update_clip_to_panel(struct rtgui_panel* panel)
  389. {
  390. rt_uint32_t count;
  391. rt_thread_t tid;
  392. struct rtgui_list_node* node;
  393. struct rtgui_event_clip_info eclip;
  394. /* get topwin count */
  395. count = 0;
  396. node = _rtgui_topwin_show_list.next;
  397. while (node != RT_NULL)
  398. {
  399. count ++;
  400. node = node->next;
  401. }
  402. /* send clip info event to panel */
  403. RTGUI_EVENT_CLIP_INFO_INIT(&eclip);
  404. eclip.num_rect = count; eclip.wid = RT_NULL;
  405. /* send to the activated thread of panel */
  406. tid = rtgui_panel_get_active_thread(panel);
  407. rtgui_thread_send(tid, &eclip.parent, sizeof(struct rtgui_event_clip_info));
  408. }
  409. #else
  410. void rtgui_topwin_update_clip_to_panel(struct rtgui_panel* panel)
  411. {
  412. rt_uint32_t count;
  413. rt_thread_t tid;
  414. struct rtgui_list_node* node;
  415. struct rtgui_event_clip_info* eclip;
  416. /* get topwin count */
  417. count = 0;
  418. node = _rtgui_topwin_show_list.next;
  419. while (node != RT_NULL)
  420. {
  421. count ++;
  422. node = node->next;
  423. }
  424. eclip = (struct rtgui_event_clip_info*)rtgui_malloc(sizeof(struct rtgui_event_clip_info)
  425. + (count + 1)* sizeof(struct rtgui_rect));
  426. if (eclip == RT_NULL)
  427. {
  428. /* no memory */
  429. return ;
  430. }
  431. /* reset clip info to top window */
  432. RTGUI_EVENT_CLIP_INFO_INIT(eclip);
  433. eclip->num_rect = count; eclip->wid = RT_NULL;
  434. count = 0;
  435. for (node = _rtgui_topwin_show_list.next; node != RT_NULL; node = node->next)
  436. {
  437. struct rtgui_topwin* wnd;
  438. struct rtgui_rect* rect;
  439. wnd = rtgui_list_entry(node, struct rtgui_topwin, list);
  440. rect = RTGUI_EVENT_GET_RECT(eclip, count++);
  441. *rect = (wnd->title != RT_NULL)? RTGUI_WIDGET(wnd->title)->extent : wnd->extent;
  442. }
  443. /* send to the activated thread of panel */
  444. tid = rtgui_panel_get_active_thread(panel);
  445. rtgui_thread_send(tid, (struct rtgui_event*)eclip, sizeof(struct rtgui_event_clip_info)
  446. + count* sizeof(struct rtgui_rect));
  447. /* release clip info event */
  448. rtgui_free(eclip);
  449. }
  450. #endif
  451. /* hide a window */
  452. void rtgui_topwin_hide(struct rtgui_event_win* event)
  453. {
  454. struct rtgui_topwin* topwin;
  455. struct rtgui_win* wid = event->wid;
  456. /* find in show list */
  457. topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
  458. /* found it */
  459. if (topwin)
  460. {
  461. /* remove node from show list */
  462. rtgui_list_remove(&_rtgui_topwin_show_list, &(topwin->list));
  463. /* add node to hidden list */
  464. rtgui_list_insert(&_rtgui_topwin_hide_list, &(topwin->list));
  465. /* show window title */
  466. if (topwin->title != RT_NULL)
  467. {
  468. RTGUI_WIDGET_HIDE(RTGUI_WIDGET(topwin->title));
  469. }
  470. /* update clip info */
  471. rtgui_topwin_update_clip();
  472. /* redraw the old rect */
  473. rtgui_topwin_redraw(&(topwin->extent));
  474. if (rtgui_server_focus_topwin == topwin)
  475. {
  476. /* activate the next window */
  477. if (_rtgui_topwin_show_list.next != RT_NULL)
  478. {
  479. /* get the topwin */
  480. topwin = rtgui_list_entry(_rtgui_topwin_show_list.next,
  481. struct rtgui_topwin, list);
  482. rtgui_server_focus_topwin = RT_NULL;
  483. rtgui_topwin_activate_win(topwin);
  484. }
  485. else
  486. {
  487. /* there is no shown window right now */
  488. rtgui_server_focus_topwin = RT_NULL;
  489. }
  490. }
  491. }
  492. else
  493. {
  494. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_ERROR);
  495. return;
  496. }
  497. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
  498. }
  499. /* move top window */
  500. void rtgui_topwin_move(struct rtgui_event_win_move* event)
  501. {
  502. struct rtgui_topwin* topwin;
  503. /* find in show list */
  504. topwin = rtgui_topwin_search_in_list(event->wid, &_rtgui_topwin_show_list);
  505. if (topwin != RT_NULL)
  506. {
  507. int dx, dy;
  508. rtgui_rect_t rect; /* the old topwin coverage area */
  509. struct rtgui_list_node* node;
  510. /* send status ok */
  511. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
  512. /* get the delta move x, y */
  513. dx = event->x - topwin->extent.x1;
  514. dy = event->y - topwin->extent.y1;
  515. rect = topwin->extent;
  516. /* move window rect */
  517. rtgui_rect_moveto(&(topwin->extent), dx, dy);
  518. /* move window title */
  519. if (topwin->title != RT_NULL)
  520. {
  521. rect = RTGUI_WIDGET(topwin->title)->extent;
  522. rtgui_widget_move_to_logic(RTGUI_WIDGET(topwin->title), dx, dy);
  523. }
  524. /* move the monitor rect list */
  525. rtgui_list_foreach(node, &(topwin->monitor_list))
  526. {
  527. struct rtgui_mouse_monitor* monitor = rtgui_list_entry(node,
  528. struct rtgui_mouse_monitor,
  529. list);
  530. rtgui_rect_moveto(&(monitor->rect), dx, dy);
  531. }
  532. /* update windows clip info */
  533. rtgui_topwin_update_clip();
  534. /* update top window title */
  535. if (topwin->title != RT_NULL) rtgui_theme_draw_win(topwin);
  536. if (rtgui_rect_is_intersect(&rect, &(topwin->extent)) != RT_EOK)
  537. {
  538. /*
  539. * the old rect is not intersect with moved rect,
  540. * re-paint window
  541. */
  542. struct rtgui_event_paint epaint;
  543. RTGUI_EVENT_PAINT_INIT(&epaint);
  544. epaint.wid = topwin->wid;
  545. rtgui_thread_send(topwin->tid, &(epaint.parent), sizeof(epaint));
  546. }
  547. /* update old window coverage area */
  548. rtgui_topwin_redraw(&rect);
  549. }
  550. else
  551. {
  552. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_ERROR);
  553. }
  554. }
  555. /*
  556. * resize a top win
  557. * Note: currently, only support resize hidden window
  558. */
  559. void rtgui_topwin_resize(struct rtgui_win* wid, rtgui_rect_t* r)
  560. {
  561. struct rtgui_topwin* topwin;
  562. /* find in show list */
  563. topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list);
  564. if (topwin)
  565. {
  566. topwin->extent = *r;
  567. if (topwin->title != RT_NULL)
  568. {
  569. /* get win extent */
  570. rtgui_rect_t rect = topwin->extent;
  571. /* add border rect */
  572. if (topwin->flag & WINTITLE_BORDER)
  573. {
  574. rtgui_rect_inflate(&rect, WINTITLE_BORDER_SIZE);
  575. }
  576. /* add title rect */
  577. if (!(topwin->flag & WINTITLE_NO)) rect.y1 -= WINTITLE_HEIGHT;
  578. RTGUI_WIDGET(topwin->title)->extent = rect;
  579. /* update title & border clip info */
  580. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(topwin->title));
  581. rtgui_region_subtract_rect(&(RTGUI_WIDGET(topwin->title)->clip),
  582. &(RTGUI_WIDGET(topwin->title)->clip),
  583. &(topwin->extent));
  584. }
  585. }
  586. }
  587. struct rtgui_topwin* rtgui_topwin_get_wnd(int x, int y)
  588. {
  589. struct rtgui_list_node* node;
  590. struct rtgui_topwin* topwin;
  591. /* search in list */
  592. rtgui_list_foreach(node, &(_rtgui_topwin_show_list))
  593. {
  594. topwin = rtgui_list_entry(node, struct rtgui_topwin, list);
  595. /* is this window? */
  596. if ((topwin->title != RT_NULL) &&
  597. rtgui_rect_contains_point(&(RTGUI_WIDGET(topwin->title)->extent), x, y) == RT_EOK)
  598. {
  599. return topwin;
  600. }
  601. else if (rtgui_rect_contains_point(&(topwin->extent), x, y) == RT_EOK)
  602. {
  603. return topwin;
  604. }
  605. }
  606. return RT_NULL;
  607. }
  608. extern struct rtgui_list_node _rtgui_panel_list;
  609. #ifdef RTGUI_USING_SMALL_SIZE
  610. static void rtgui_topwin_update_clip()
  611. {
  612. rt_int32_t count = 0;
  613. struct rtgui_event_clip_info eclip;
  614. struct rtgui_list_node* node = _rtgui_topwin_show_list.next;
  615. RTGUI_EVENT_CLIP_INFO_INIT(&eclip);
  616. rtgui_list_foreach(node, &_rtgui_topwin_show_list)
  617. {
  618. struct rtgui_topwin* wnd;
  619. wnd = rtgui_list_entry(node, struct rtgui_topwin, list);
  620. eclip.num_rect = count;
  621. eclip.wid = wnd->wid;
  622. count ++;
  623. /* send to destination window */
  624. rtgui_thread_send(wnd->tid, &(eclip.parent), sizeof(struct rtgui_event_clip_info));
  625. /* update clip in win title */
  626. if (wnd->title != RT_NULL)
  627. {
  628. /* reset clip info */
  629. rtgui_toplevel_handle_clip(RTGUI_TOPLEVEL(wnd->title), &eclip);
  630. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(wnd->title));
  631. rtgui_region_subtract_rect(&(RTGUI_WIDGET(wnd->title)->clip),
  632. &(RTGUI_WIDGET(wnd->title)->clip),
  633. &(wnd->extent));
  634. }
  635. }
  636. /* send clip info to each panel */
  637. eclip.wid = RT_NULL;
  638. eclip.num_rect = count;
  639. rtgui_list_foreach(node, &(_rtgui_panel_list))
  640. {
  641. struct rtgui_panel* panel;
  642. struct rtgui_list_node* panel_node;
  643. panel = rtgui_list_entry(node, struct rtgui_panel, sibling);
  644. rtgui_list_foreach(panel_node, &(panel->thread_list))
  645. {
  646. struct rtgui_panel_thread* thread;
  647. thread = rtgui_list_entry(panel_node, struct rtgui_panel_thread, list);
  648. /* send clip info to panel */
  649. rtgui_thread_send(thread->tid, &(eclip.parent), sizeof(struct rtgui_event_clip_info));
  650. }
  651. }
  652. }
  653. #else
  654. static void rtgui_topwin_update_clip()
  655. {
  656. rt_int32_t count = 0;
  657. struct rtgui_event_clip_info* eclip;
  658. struct rtgui_list_node* node = _rtgui_topwin_show_list.next;
  659. /* calculate count */
  660. while (node != RT_NULL)
  661. {
  662. count ++;
  663. node = node->next;
  664. }
  665. eclip = (struct rtgui_event_clip_info*)rtgui_malloc(sizeof(struct rtgui_event_clip_info)
  666. + count * sizeof(struct rtgui_rect));
  667. RTGUI_EVENT_CLIP_INFO_INIT(eclip);
  668. count = 0;
  669. rtgui_list_foreach(node, &_rtgui_topwin_show_list)
  670. {
  671. struct rtgui_rect* rect;
  672. struct rtgui_topwin* wnd;
  673. wnd = rtgui_list_entry(node, struct rtgui_topwin, list);
  674. eclip->num_rect = count;
  675. eclip->wid = wnd->wid;
  676. /* send to destination window */
  677. rtgui_thread_send(wnd->tid, &(eclip->parent),
  678. sizeof(struct rtgui_event_clip_info) + count * sizeof(struct rtgui_rect));
  679. /* update clip in win title */
  680. if (wnd->title != RT_NULL)
  681. {
  682. /* reset clip info */
  683. rtgui_toplevel_handle_clip(RTGUI_TOPLEVEL(wnd->title), eclip);
  684. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(wnd->title));
  685. rtgui_region_subtract_rect(&(RTGUI_WIDGET(wnd->title)->clip),
  686. &(RTGUI_WIDGET(wnd->title)->clip),
  687. &(wnd->extent));
  688. }
  689. rect = RTGUI_EVENT_GET_RECT(eclip, count++);
  690. *rect = (wnd->title != RT_NULL)? RTGUI_WIDGET(wnd->title)->extent : wnd->extent;
  691. }
  692. /* send clip info to each panel */
  693. eclip->wid = RT_NULL;
  694. eclip->num_rect = count;
  695. rtgui_list_foreach(node, &(_rtgui_panel_list))
  696. {
  697. struct rtgui_panel* panel;
  698. struct rtgui_list_node* panel_node;
  699. panel = rtgui_list_entry(node, struct rtgui_panel, sibling);
  700. rtgui_list_foreach(panel_node, &(panel->thread_list))
  701. {
  702. struct rtgui_panel_thread* thread;
  703. thread = rtgui_list_entry(panel_node, struct rtgui_panel_thread, list);
  704. /* send clip info to panel */
  705. rtgui_thread_send(thread->tid, &(eclip->parent),
  706. sizeof(struct rtgui_event_clip_info) + count * sizeof(struct rtgui_rect));
  707. }
  708. }
  709. /* release clip info event */
  710. rtgui_free(eclip);
  711. }
  712. #endif
  713. static void rtgui_topwin_redraw(struct rtgui_rect* rect)
  714. {
  715. struct rtgui_list_node* node;
  716. struct rtgui_event_paint epaint;
  717. RTGUI_EVENT_PAINT_INIT(&epaint);
  718. rtgui_list_foreach(node, &_rtgui_topwin_show_list)
  719. {
  720. struct rtgui_topwin* wnd = rtgui_list_entry(node, struct rtgui_topwin, list);
  721. if (rtgui_rect_is_intersect(rect, &(wnd->extent)) == RT_EOK)
  722. {
  723. /* draw window */
  724. epaint.wid = wnd->wid;
  725. rtgui_thread_send(wnd->tid, &(epaint.parent), sizeof(epaint));
  726. /* draw title */
  727. if (wnd->title != RT_NULL)
  728. {
  729. rtgui_theme_draw_win(wnd);
  730. }
  731. }
  732. }
  733. /* redraw the panel */
  734. rtgui_list_foreach(node, &(_rtgui_panel_list))
  735. {
  736. struct rtgui_panel* panel;
  737. panel = rtgui_list_entry(node, struct rtgui_panel, sibling);
  738. if (rtgui_rect_is_intersect(rect, &(panel->extent)) == RT_EOK)
  739. {
  740. rt_thread_t tid;
  741. tid = rtgui_panel_get_active_thread(panel);
  742. if (tid != RT_NULL)
  743. {
  744. /* draw panel */
  745. epaint.wid = RT_NULL;
  746. rtgui_thread_send(tid, &(epaint.parent), sizeof(epaint));
  747. }
  748. }
  749. }
  750. }
  751. void rtgui_topwin_title_onmouse(struct rtgui_topwin* win, struct rtgui_event_mouse* event)
  752. {
  753. rtgui_rect_t rect;
  754. /* let window to process this mouse event */
  755. if (rtgui_rect_contains_point(&win->extent, event->x, event->y) == RT_EOK)
  756. {
  757. /* send mouse event to thread */
  758. rtgui_thread_send(win->tid, &(event->parent), sizeof(struct rtgui_event_mouse));
  759. return;
  760. }
  761. /* get close button rect (device value) */
  762. rect.x1 = RTGUI_WIDGET(win->title)->extent.x2 - WINTITLE_BORDER_SIZE - WINTITLE_CB_WIDTH - 3;
  763. rect.y1 = RTGUI_WIDGET(win->title)->extent.y1 + WINTITLE_BORDER_SIZE + 3;
  764. rect.x2 = rect.x1 + WINTITLE_CB_WIDTH;
  765. rect.y2 = rect.y1 + WINTITLE_CB_HEIGHT;
  766. if (event->button & RTGUI_MOUSE_BUTTON_LEFT)
  767. {
  768. if (event->button & RTGUI_MOUSE_BUTTON_DOWN)
  769. {
  770. if (rtgui_rect_contains_point(&rect, event->x, event->y) == RT_EOK)
  771. {
  772. win->flag |= WINTITLE_CB_PRESSED;
  773. rtgui_theme_draw_win(win);
  774. }
  775. else
  776. {
  777. /* maybe move window */
  778. rtgui_winrect_set(win);
  779. }
  780. }
  781. else if (event->button & RTGUI_MOUSE_BUTTON_UP)
  782. {
  783. if (rtgui_rect_contains_point(&rect, event->x, event->y) == RT_EOK)
  784. {
  785. struct rtgui_event_win event;
  786. win->flag &= ~WINTITLE_CB_PRESSED;
  787. rtgui_theme_draw_win(win);
  788. /* send close event to window */
  789. RTGUI_EVENT_WIN_CLOSE_INIT(&event);
  790. event.wid = win->wid;
  791. rtgui_thread_send(win->tid, &(event.parent), sizeof(struct rtgui_event_win));
  792. }
  793. }
  794. }
  795. }
  796. void rtgui_topwin_append_monitor_rect(struct rtgui_win* wid, rtgui_rect_t* rect)
  797. {
  798. struct rtgui_topwin* win;
  799. /* parameters check */
  800. if (wid == RT_NULL || rect == RT_NULL) return;
  801. /* find topwin */
  802. win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
  803. if (win == RT_NULL)
  804. win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list);
  805. if (win == RT_NULL) return;
  806. /* append rect to top window monitor rect list */
  807. rtgui_mouse_monitor_append(&(win->monitor_list), rect);
  808. }
  809. void rtgui_topwin_remove_monitor_rect(struct rtgui_win* wid, rtgui_rect_t* rect)
  810. {
  811. struct rtgui_topwin* win;
  812. /* parameters check */
  813. if (wid == RT_NULL || rect == RT_NULL) return;
  814. /* find topwin */
  815. win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
  816. if (win == RT_NULL)
  817. win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list);
  818. if (win == RT_NULL) return;
  819. /* remove rect from top window monitor rect list */
  820. rtgui_mouse_monitor_remove(&(win->monitor_list), rect);
  821. }
  822. #ifdef RTGUI_USING_SMALL_SIZE
  823. /**
  824. * get clip information for topwin
  825. * wid, the self window id. If wid = NULL, get whole topwin clip information.
  826. *
  827. * @return the clip rect information
  828. */
  829. void rtgui_topwin_get_clipinfo(struct rtgui_rect* rect_list, rt_int32_t count)
  830. {
  831. struct rtgui_rect* rect;
  832. struct rtgui_topwin* topwin;
  833. struct rtgui_list_node* node;
  834. if ((rect_list == RT_NULL) || (count == 0)) return ;
  835. /* set to the header of list */
  836. rect = rect_list;
  837. rt_sem_take(&_rtgui_topwin_lock, RT_WAITING_FOREVER);
  838. /* get all of topwin rect list */
  839. rtgui_list_foreach(node, &_rtgui_topwin_show_list)
  840. {
  841. topwin = rtgui_list_entry(node, struct rtgui_topwin, list);
  842. if (topwin->title != RT_NULL)
  843. rtgui_widget_get_rect(RTGUI_WIDGET(topwin->title), rect);
  844. else *rect = topwin->extent;
  845. rect ++;
  846. count --;
  847. if (count < 0) break;
  848. }
  849. rt_sem_release(&_rtgui_topwin_lock);
  850. }
  851. #endif
  852. #ifdef RT_USING_FINSH
  853. #include <finsh.h>
  854. void rtgui_topwin_dump()
  855. {
  856. struct rtgui_list_node* node;
  857. rtgui_list_foreach(node, &_rtgui_topwin_show_list)
  858. {
  859. struct rtgui_topwin* wnd = rtgui_list_entry(node, struct rtgui_topwin, list);
  860. rt_kprintf("wnd at (%d, %d) - (%d, %d)\n",
  861. wnd->extent.x1, wnd->extent.y1, wnd->extent.x2, wnd->extent.y2);
  862. if (wnd->title != RT_NULL)
  863. {
  864. rt_kprintf("title[%s] border (%d, %d) - (%d, %d)\n", wnd->title->title,
  865. RTGUI_WIDGET(wnd->title)->extent.x1, RTGUI_WIDGET(wnd->title)->extent.y1,
  866. RTGUI_WIDGET(wnd->title)->extent.x2, RTGUI_WIDGET(wnd->title)->extent.y2);
  867. }
  868. }
  869. }
  870. FINSH_FUNCTION_EXPORT(rtgui_topwin_dump, dump topwindow list);
  871. #endif