topwin.c 27 KB

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