topwin.c 27 KB

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