topwin.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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. rtgui_toplevel_handle_clip(RTGUI_TOPLEVEL(wnd->title), &eclip);
  249. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(wnd->title));
  250. rtgui_region_subtract_rect(&(RTGUI_WIDGET(wnd->title)->clip),
  251. &(RTGUI_WIDGET(wnd->title)->clip),
  252. &(wnd->extent));
  253. }
  254. rtgui_topwin_activate_win(topwin);
  255. }
  256. }
  257. #else
  258. void rtgui_topwin_raise(struct rtgui_win* wid, rt_thread_t sender)
  259. {
  260. struct rtgui_topwin* topwin;
  261. /* find the topwin node */
  262. topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
  263. if (topwin)
  264. {
  265. rt_int32_t count;
  266. struct rtgui_list_node* node;
  267. struct rtgui_event_clip_info* eclip;
  268. struct rtgui_rect* rect;
  269. /* the window is already placed in front */
  270. if (&(topwin->list) == _rtgui_topwin_show_list.next)
  271. {
  272. rtgui_server_focus_topwin = RT_NULL;
  273. rtgui_topwin_activate_win(topwin);
  274. return ;
  275. }
  276. /* update clip info */
  277. count = 0;
  278. node = _rtgui_topwin_show_list.next;
  279. while (node != &(topwin->list))
  280. {
  281. count ++;
  282. node = node->next;
  283. }
  284. eclip = (struct rtgui_event_clip_info*)rtgui_malloc(sizeof(struct rtgui_event_clip_info)
  285. + (count + 1)* sizeof(struct rtgui_rect));
  286. /* reset clip info to top window */
  287. RTGUI_EVENT_CLIP_INFO_INIT(eclip);
  288. eclip->num_rect = 0;
  289. eclip->wid = topwin->wid;
  290. /* send to destination window */
  291. rtgui_thread_send(topwin->tid, &(eclip->parent), sizeof(struct rtgui_event_clip_info));
  292. /* reset clip info in title */
  293. rtgui_toplevel_handle_clip(RTGUI_TOPLEVEL(topwin->title), eclip);
  294. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(topwin->title));
  295. rtgui_region_subtract_rect(&(RTGUI_WIDGET(topwin->title)->clip),
  296. &(RTGUI_WIDGET(topwin->title)->clip),
  297. &(topwin->extent));
  298. rect = RTGUI_EVENT_GET_RECT(eclip, 0);
  299. *rect = (topwin->title != RT_NULL)? RTGUI_WIDGET(topwin->title)->extent : topwin->extent;
  300. count = 1;
  301. for (node = _rtgui_topwin_show_list.next;
  302. node != &(topwin->list);
  303. node = node->next)
  304. {
  305. struct rtgui_topwin* wnd;
  306. wnd = rtgui_list_entry(node, struct rtgui_topwin, list);
  307. eclip->num_rect = count;
  308. eclip->wid = wnd->wid;
  309. /* send to destination window */
  310. rtgui_thread_send(wnd->tid, &(eclip->parent),
  311. sizeof(struct rtgui_event_clip_info) + count * sizeof(struct rtgui_rect));
  312. /* reset clip info in title */
  313. rtgui_toplevel_handle_clip(RTGUI_TOPLEVEL(wnd->title), eclip);
  314. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(wnd->title));
  315. rtgui_region_subtract_rect(&(RTGUI_WIDGET(wnd->title)->clip),
  316. &(RTGUI_WIDGET(wnd->title)->clip),
  317. &(wnd->extent));
  318. rect = RTGUI_EVENT_GET_RECT(eclip, count++);
  319. *rect = (wnd->title != RT_NULL)? RTGUI_WIDGET(wnd->title)->extent : wnd->extent;
  320. }
  321. /* release clip info event */
  322. rtgui_free(eclip);
  323. /* remove node from list */
  324. rtgui_list_remove(&_rtgui_topwin_show_list, &(topwin->list));
  325. /* add to front */
  326. rtgui_list_insert(&_rtgui_topwin_show_list, &(topwin->list));
  327. rtgui_topwin_activate_win(topwin);
  328. }
  329. }
  330. #endif
  331. /* show a window */
  332. void rtgui_topwin_show(struct rtgui_event_win* event)
  333. {
  334. struct rtgui_topwin* topwin;
  335. struct rtgui_win* wid = event->wid;
  336. rt_thread_t sender = RTGUI_EVENT(event)->sender;
  337. /* find in hide list */
  338. topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list);
  339. /* find it */
  340. if (topwin != RT_NULL)
  341. {
  342. /* remove node from hidden list */
  343. rtgui_list_remove(&_rtgui_topwin_hide_list, &(topwin->list));
  344. /* add node to show list */
  345. rtgui_list_insert(&_rtgui_topwin_show_list, &(topwin->list));
  346. /* show window title */
  347. if (topwin->title != RT_NULL)
  348. {
  349. RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(topwin->title));
  350. }
  351. /* update clip info */
  352. rtgui_topwin_update_clip();
  353. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
  354. /* activate this window */
  355. rtgui_topwin_activate_win(topwin);
  356. }
  357. else
  358. {
  359. /* the wnd is located in show list, raise wnd to front */
  360. topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
  361. if (topwin != RT_NULL)
  362. {
  363. if (_rtgui_topwin_show_list.next != &(topwin->list))
  364. {
  365. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
  366. /* not the front window, raise it */
  367. rtgui_topwin_raise(wid, sender);
  368. }
  369. }
  370. else
  371. {
  372. /* there is no wnd in wnd list */
  373. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_ERROR);
  374. }
  375. }
  376. }
  377. /* send clip info to panel */
  378. #ifdef RTGUI_USING_SMALL_SIZE
  379. void rtgui_topwin_update_clip_to_panel(struct rtgui_panel* panel)
  380. {
  381. rt_uint32_t count;
  382. rt_thread_t tid;
  383. struct rtgui_list_node* node;
  384. struct rtgui_event_clip_info eclip;
  385. /* get topwin count */
  386. count = 0;
  387. node = _rtgui_topwin_show_list.next;
  388. while (node != RT_NULL)
  389. {
  390. count ++;
  391. node = node->next;
  392. }
  393. /* send clip info event to panel */
  394. RTGUI_EVENT_CLIP_INFO_INIT(&eclip);
  395. eclip.num_rect = count; eclip.wid = RT_NULL;
  396. /* send to the activated thread of panel */
  397. tid = rtgui_panel_get_active_thread(panel);
  398. rtgui_thread_send(tid, &eclip.parent, sizeof(struct rtgui_event_clip_info));
  399. }
  400. #else
  401. void rtgui_topwin_update_clip_to_panel(struct rtgui_panel* panel)
  402. {
  403. rt_uint32_t count;
  404. rt_thread_t tid;
  405. struct rtgui_list_node* node;
  406. struct rtgui_event_clip_info* eclip;
  407. /* get topwin count */
  408. count = 0;
  409. node = _rtgui_topwin_show_list.next;
  410. while (node != RT_NULL)
  411. {
  412. count ++;
  413. node = node->next;
  414. }
  415. eclip = (struct rtgui_event_clip_info*)rtgui_malloc(sizeof(struct rtgui_event_clip_info)
  416. + (count + 1)* sizeof(struct rtgui_rect));
  417. if (eclip == RT_NULL)
  418. {
  419. /* no memory */
  420. return ;
  421. }
  422. /* reset clip info to top window */
  423. RTGUI_EVENT_CLIP_INFO_INIT(eclip);
  424. eclip->num_rect = count; eclip->wid = RT_NULL;
  425. count = 0;
  426. for (node = _rtgui_topwin_show_list.next; node != RT_NULL; node = node->next)
  427. {
  428. struct rtgui_topwin* wnd;
  429. struct rtgui_rect* rect;
  430. wnd = rtgui_list_entry(node, struct rtgui_topwin, list);
  431. rect = RTGUI_EVENT_GET_RECT(eclip, count++);
  432. *rect = (wnd->title != RT_NULL)? RTGUI_WIDGET(wnd->title)->extent : wnd->extent;
  433. }
  434. /* send to the activated thread of panel */
  435. tid = rtgui_panel_get_active_thread(panel);
  436. rtgui_thread_send(tid, (struct rtgui_event*)eclip, sizeof(struct rtgui_event_clip_info)
  437. + count* sizeof(struct rtgui_rect));
  438. /* release clip info event */
  439. rtgui_free(eclip);
  440. }
  441. #endif
  442. /* hide a window */
  443. void rtgui_topwin_hide(struct rtgui_event_win* event)
  444. {
  445. struct rtgui_topwin* topwin;
  446. struct rtgui_win* wid = event->wid;
  447. /* find in show list */
  448. topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
  449. /* found it */
  450. if (topwin)
  451. {
  452. /* remove node from show list */
  453. rtgui_list_remove(&_rtgui_topwin_show_list, &(topwin->list));
  454. /* add node to hidden list */
  455. rtgui_list_insert(&_rtgui_topwin_hide_list, &(topwin->list));
  456. /* show window title */
  457. if (topwin->title != RT_NULL)
  458. {
  459. RTGUI_WIDGET_HIDE(RTGUI_WIDGET(topwin->title));
  460. }
  461. /* update clip info */
  462. rtgui_topwin_update_clip();
  463. /* redraw the old rect */
  464. rtgui_topwin_redraw(&(topwin->extent));
  465. if (rtgui_server_focus_topwin == topwin)
  466. {
  467. /* activate the next window */
  468. if (_rtgui_topwin_show_list.next != RT_NULL)
  469. {
  470. /* get the topwin */
  471. topwin = rtgui_list_entry(_rtgui_topwin_show_list.next,
  472. struct rtgui_topwin, list);
  473. rtgui_server_focus_topwin = RT_NULL;
  474. rtgui_topwin_activate_win(topwin);
  475. }
  476. else
  477. {
  478. /* there is no shown window right now */
  479. rtgui_server_focus_topwin = RT_NULL;
  480. }
  481. }
  482. }
  483. else
  484. {
  485. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_ERROR);
  486. return;
  487. }
  488. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
  489. }
  490. /* move top window */
  491. void rtgui_topwin_move(struct rtgui_event_win_move* event)
  492. {
  493. struct rtgui_topwin* topwin;
  494. /* find in show list */
  495. topwin = rtgui_topwin_search_in_list(event->wid, &_rtgui_topwin_show_list);
  496. if (topwin != RT_NULL)
  497. {
  498. int dx, dy;
  499. rtgui_rect_t rect; /* the old topwin coverage area */
  500. struct rtgui_list_node* node;
  501. /* send status ok */
  502. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
  503. /* get the delta move x, y */
  504. dx = event->x - topwin->extent.x1;
  505. dy = event->y - topwin->extent.y1;
  506. rect = topwin->extent;
  507. /* move window rect */
  508. rtgui_rect_moveto(&(topwin->extent), dx, dy);
  509. /* move window title */
  510. if (topwin->title != RT_NULL)
  511. {
  512. rect = RTGUI_WIDGET(topwin->title)->extent;
  513. rtgui_widget_move_to_logic(RTGUI_WIDGET(topwin->title), dx, dy);
  514. }
  515. /* move the monitor rect list */
  516. rtgui_list_foreach(node, &(topwin->monitor_list))
  517. {
  518. struct rtgui_mouse_monitor* monitor = rtgui_list_entry(node,
  519. struct rtgui_mouse_monitor,
  520. list);
  521. rtgui_rect_moveto(&(monitor->rect), dx, dy);
  522. }
  523. /* update windows clip info */
  524. rtgui_topwin_update_clip();
  525. /* update top window title */
  526. if (topwin->title != RT_NULL) rtgui_theme_draw_win(topwin);
  527. if (rtgui_rect_is_intersect(&rect, &(topwin->extent)) != RT_EOK)
  528. {
  529. /*
  530. * the old rect is not intersect with moved rect,
  531. * re-paint window
  532. */
  533. struct rtgui_event_paint epaint;
  534. RTGUI_EVENT_PAINT_INIT(&epaint);
  535. epaint.wid = topwin->wid;
  536. rtgui_thread_send(topwin->tid, &(epaint.parent), sizeof(epaint));
  537. }
  538. /* update old window coverage area */
  539. rtgui_topwin_redraw(&rect);
  540. }
  541. else
  542. {
  543. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_ERROR);
  544. }
  545. }
  546. /*
  547. * resize a top win
  548. * Note: currently, only support resize hidden window
  549. */
  550. void rtgui_topwin_resize(struct rtgui_win* wid, rtgui_rect_t* r)
  551. {
  552. struct rtgui_topwin* topwin;
  553. /* find in show list */
  554. topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list);
  555. if (topwin)
  556. {
  557. topwin->extent = *r;
  558. if (topwin->title != RT_NULL)
  559. {
  560. /* get win extent */
  561. rtgui_rect_t rect = topwin->extent;
  562. /* add border rect */
  563. if (topwin->flag & WINTITLE_BORDER)
  564. {
  565. rtgui_rect_inflate(&rect, WINTITLE_BORDER_SIZE);
  566. }
  567. /* add title rect */
  568. if (!(topwin->flag & WINTITLE_NO)) rect.y1 -= WINTITLE_HEIGHT;
  569. RTGUI_WIDGET(topwin->title)->extent = rect;
  570. /* update title & border clip info */
  571. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(topwin->title));
  572. rtgui_region_subtract_rect(&(RTGUI_WIDGET(topwin->title)->clip),
  573. &(RTGUI_WIDGET(topwin->title)->clip),
  574. &(topwin->extent));
  575. }
  576. }
  577. }
  578. struct rtgui_topwin* rtgui_topwin_get_wnd(int x, int y)
  579. {
  580. struct rtgui_list_node* node;
  581. struct rtgui_topwin* topwin;
  582. /* search in list */
  583. rtgui_list_foreach(node, &(_rtgui_topwin_show_list))
  584. {
  585. topwin = rtgui_list_entry(node, struct rtgui_topwin, list);
  586. /* is this window? */
  587. if ((topwin->title != RT_NULL) &&
  588. rtgui_rect_contains_point(&(RTGUI_WIDGET(topwin->title)->extent), x, y) == RT_EOK)
  589. {
  590. return topwin;
  591. }
  592. else if (rtgui_rect_contains_point(&(topwin->extent), x, y) == RT_EOK)
  593. {
  594. return topwin;
  595. }
  596. }
  597. return RT_NULL;
  598. }
  599. extern struct rtgui_list_node _rtgui_panel_list;
  600. #ifdef RTGUI_USING_SMALL_SIZE
  601. static void rtgui_topwin_update_clip()
  602. {
  603. rt_int32_t count = 0;
  604. struct rtgui_event_clip_info eclip;
  605. struct rtgui_list_node* node = _rtgui_topwin_show_list.next;
  606. RTGUI_EVENT_CLIP_INFO_INIT(&eclip);
  607. rtgui_list_foreach(node, &_rtgui_topwin_show_list)
  608. {
  609. struct rtgui_topwin* wnd;
  610. wnd = rtgui_list_entry(node, struct rtgui_topwin, list);
  611. eclip.num_rect = count;
  612. eclip.wid = wnd->wid;
  613. count ++;
  614. /* send to destination window */
  615. rtgui_thread_send(wnd->tid, &(eclip.parent), sizeof(struct rtgui_event_clip_info));
  616. /* update clip in win title */
  617. if (wnd->title != RT_NULL)
  618. {
  619. /* reset clip info */
  620. rtgui_toplevel_handle_clip(RTGUI_TOPLEVEL(wnd->title), &eclip);
  621. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(wnd->title));
  622. rtgui_region_subtract_rect(&(RTGUI_WIDGET(wnd->title)->clip),
  623. &(RTGUI_WIDGET(wnd->title)->clip),
  624. &(wnd->extent));
  625. }
  626. }
  627. /* send clip info to each panel */
  628. eclip.wid = RT_NULL;
  629. eclip.num_rect = count;
  630. rtgui_list_foreach(node, &(_rtgui_panel_list))
  631. {
  632. struct rtgui_panel* panel;
  633. struct rtgui_list_node* panel_node;
  634. panel = rtgui_list_entry(node, struct rtgui_panel, sibling);
  635. rtgui_list_foreach(panel_node, &(panel->thread_list))
  636. {
  637. struct rtgui_panel_thread* thread;
  638. thread = rtgui_list_entry(panel_node, struct rtgui_panel_thread, list);
  639. /* send clip info to panel */
  640. rtgui_thread_send(thread->tid, &(eclip.parent), sizeof(struct rtgui_event_clip_info));
  641. }
  642. }
  643. }
  644. #else
  645. static void rtgui_topwin_update_clip()
  646. {
  647. rt_int32_t count = 0;
  648. struct rtgui_event_clip_info* eclip;
  649. struct rtgui_list_node* node = _rtgui_topwin_show_list.next;
  650. /* calculate count */
  651. while (node != RT_NULL)
  652. {
  653. count ++;
  654. node = node->next;
  655. }
  656. eclip = (struct rtgui_event_clip_info*)rtgui_malloc(sizeof(struct rtgui_event_clip_info)
  657. + count * sizeof(struct rtgui_rect));
  658. RTGUI_EVENT_CLIP_INFO_INIT(eclip);
  659. count = 0;
  660. rtgui_list_foreach(node, &_rtgui_topwin_show_list)
  661. {
  662. struct rtgui_rect* rect;
  663. struct rtgui_topwin* wnd;
  664. wnd = rtgui_list_entry(node, struct rtgui_topwin, list);
  665. eclip->num_rect = count;
  666. eclip->wid = wnd->wid;
  667. /* send to destination window */
  668. rtgui_thread_send(wnd->tid, &(eclip->parent),
  669. sizeof(struct rtgui_event_clip_info) + count * sizeof(struct rtgui_rect));
  670. /* update clip in win title */
  671. if (wnd->title != RT_NULL)
  672. {
  673. /* reset clip info */
  674. rtgui_toplevel_handle_clip(RTGUI_TOPLEVEL(wnd->title), eclip);
  675. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(wnd->title));
  676. rtgui_region_subtract_rect(&(RTGUI_WIDGET(wnd->title)->clip),
  677. &(RTGUI_WIDGET(wnd->title)->clip),
  678. &(wnd->extent));
  679. }
  680. rect = RTGUI_EVENT_GET_RECT(eclip, count++);
  681. *rect = (wnd->title != RT_NULL)? RTGUI_WIDGET(wnd->title)->extent : wnd->extent;
  682. }
  683. /* send clip info to each panel */
  684. eclip->wid = RT_NULL;
  685. eclip->num_rect = count;
  686. rtgui_list_foreach(node, &(_rtgui_panel_list))
  687. {
  688. struct rtgui_panel* panel;
  689. struct rtgui_list_node* panel_node;
  690. panel = rtgui_list_entry(node, struct rtgui_panel, sibling);
  691. rtgui_list_foreach(panel_node, &(panel->thread_list))
  692. {
  693. struct rtgui_panel_thread* thread;
  694. thread = rtgui_list_entry(panel_node, struct rtgui_panel_thread, list);
  695. /* send clip info to panel */
  696. rtgui_thread_send(thread->tid, &(eclip->parent),
  697. sizeof(struct rtgui_event_clip_info) + count * sizeof(struct rtgui_rect));
  698. }
  699. }
  700. /* release clip info event */
  701. rtgui_free(eclip);
  702. }
  703. #endif
  704. static void rtgui_topwin_redraw(struct rtgui_rect* rect)
  705. {
  706. struct rtgui_list_node* node;
  707. struct rtgui_event_paint epaint;
  708. RTGUI_EVENT_PAINT_INIT(&epaint);
  709. rtgui_list_foreach(node, &_rtgui_topwin_show_list)
  710. {
  711. struct rtgui_topwin* wnd = rtgui_list_entry(node, struct rtgui_topwin, list);
  712. if (rtgui_rect_is_intersect(rect, &(wnd->extent)) == RT_EOK)
  713. {
  714. /* draw window */
  715. epaint.wid = wnd->wid;
  716. rtgui_thread_send(wnd->tid, &(epaint.parent), sizeof(epaint));
  717. /* draw title */
  718. if (wnd->title != RT_NULL)
  719. {
  720. rtgui_theme_draw_win(wnd);
  721. }
  722. }
  723. }
  724. /* redraw the panel */
  725. rtgui_list_foreach(node, &(_rtgui_panel_list))
  726. {
  727. struct rtgui_panel* panel;
  728. panel = rtgui_list_entry(node, struct rtgui_panel, sibling);
  729. if (rtgui_rect_is_intersect(rect, &(panel->extent)) == RT_EOK)
  730. {
  731. rt_thread_t tid;
  732. tid = rtgui_panel_get_active_thread(panel);
  733. if (tid != RT_NULL)
  734. {
  735. /* draw panel */
  736. epaint.wid = RT_NULL;
  737. rtgui_thread_send(tid, &(epaint.parent), sizeof(epaint));
  738. }
  739. }
  740. }
  741. }
  742. void rtgui_topwin_title_onmouse(struct rtgui_topwin* win, struct rtgui_event_mouse* event)
  743. {
  744. rtgui_rect_t rect;
  745. /* let window to process this mouse event */
  746. if (rtgui_rect_contains_point(&win->extent, event->x, event->y) == RT_EOK)
  747. {
  748. /* send mouse event to thread */
  749. rtgui_thread_send(win->tid, &(event->parent), sizeof(struct rtgui_event_mouse));
  750. return;
  751. }
  752. /* get close button rect (device value) */
  753. rect.x1 = RTGUI_WIDGET(win->title)->extent.x2 - WINTITLE_BORDER_SIZE - WINTITLE_CB_WIDTH - 3;
  754. rect.y1 = RTGUI_WIDGET(win->title)->extent.y1 + WINTITLE_BORDER_SIZE + 3;
  755. rect.x2 = rect.x1 + WINTITLE_CB_WIDTH;
  756. rect.y2 = rect.y1 + WINTITLE_CB_HEIGHT;
  757. if (event->button & RTGUI_MOUSE_BUTTON_LEFT)
  758. {
  759. if (event->button & RTGUI_MOUSE_BUTTON_DOWN)
  760. {
  761. if (rtgui_rect_contains_point(&rect, event->x, event->y) == RT_EOK)
  762. {
  763. win->flag |= WINTITLE_CB_PRESSED;
  764. rtgui_theme_draw_win(win);
  765. }
  766. else
  767. {
  768. /* maybe move window */
  769. rtgui_winrect_set(win);
  770. }
  771. }
  772. else if (event->button & RTGUI_MOUSE_BUTTON_UP)
  773. {
  774. if (rtgui_rect_contains_point(&rect, event->x, event->y) == RT_EOK)
  775. {
  776. struct rtgui_event_win event;
  777. win->flag &= ~WINTITLE_CB_PRESSED;
  778. rtgui_theme_draw_win(win);
  779. /* send close event to window */
  780. RTGUI_EVENT_WIN_CLOSE_INIT(&event);
  781. event.wid = win->wid;
  782. rtgui_thread_send(win->tid, &(event.parent), sizeof(struct rtgui_event_win));
  783. }
  784. }
  785. }
  786. }
  787. void rtgui_topwin_append_monitor_rect(struct rtgui_win* wid, rtgui_rect_t* rect)
  788. {
  789. struct rtgui_topwin* win;
  790. /* parameters check */
  791. if (wid == RT_NULL || rect == RT_NULL) return;
  792. /* find topwin */
  793. win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
  794. if (win == RT_NULL)
  795. win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list);
  796. if (win == RT_NULL) return;
  797. /* append rect to top window monitor rect list */
  798. rtgui_mouse_monitor_append(&(win->monitor_list), rect);
  799. }
  800. void rtgui_topwin_remove_monitor_rect(struct rtgui_win* wid, rtgui_rect_t* rect)
  801. {
  802. struct rtgui_topwin* win;
  803. /* parameters check */
  804. if (wid == RT_NULL || rect == RT_NULL) return;
  805. /* find topwin */
  806. win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
  807. if (win == RT_NULL)
  808. win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list);
  809. if (win == RT_NULL) return;
  810. /* remove rect from top window monitor rect list */
  811. rtgui_mouse_monitor_remove(&(win->monitor_list), rect);
  812. }
  813. #ifdef RTGUI_USING_SMALL_SIZE
  814. /**
  815. * get clip information for topwin
  816. * wid, the self window id. If wid = NULL, get whole topwin clip information.
  817. *
  818. * @return the clip rect information
  819. */
  820. void rtgui_topwin_get_clipinfo(struct rtgui_rect* rect_list, rt_int32_t count)
  821. {
  822. struct rtgui_rect* rect;
  823. struct rtgui_topwin* topwin;
  824. struct rtgui_list_node* node;
  825. if ((rect_list == RT_NULL) || (count == 0)) return ;
  826. /* set to the header of list */
  827. rect = rect_list;
  828. rt_sem_take(&_rtgui_topwin_lock, RT_WAITING_FOREVER);
  829. /* get all of topwin rect list */
  830. rtgui_list_foreach(node, &_rtgui_topwin_show_list)
  831. {
  832. topwin = rtgui_list_entry(node, struct rtgui_topwin, list);
  833. if (topwin->title != RT_NULL)
  834. rtgui_widget_get_rect(RTGUI_WIDGET(topwin->title), rect);
  835. else *rect = topwin->extent;
  836. rect ++;
  837. count --;
  838. if (count < 0) break;
  839. }
  840. rt_sem_release(&_rtgui_topwin_lock);
  841. }
  842. #endif
  843. #ifdef RT_USING_FINSH
  844. #include <finsh.h>
  845. void rtgui_topwin_dump()
  846. {
  847. struct rtgui_list_node* node;
  848. rtgui_list_foreach(node, &_rtgui_topwin_show_list)
  849. {
  850. struct rtgui_topwin* wnd = rtgui_list_entry(node, struct rtgui_topwin, list);
  851. rt_kprintf("wnd at (%d, %d) - (%d, %d)\n",
  852. wnd->extent.x1, wnd->extent.y1, wnd->extent.x2, wnd->extent.y2);
  853. if (wnd->title != RT_NULL)
  854. {
  855. rt_kprintf("title[%s] border (%d, %d) - (%d, %d)\n", wnd->title->title,
  856. RTGUI_WIDGET(wnd->title)->extent.x1, RTGUI_WIDGET(wnd->title)->extent.y1,
  857. RTGUI_WIDGET(wnd->title)->extent.x2, RTGUI_WIDGET(wnd->title)->extent.y2);
  858. }
  859. }
  860. }
  861. FINSH_FUNCTION_EXPORT(rtgui_topwin_dump, dump topwindow list);
  862. #endif