topwin.c 24 KB

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