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