topwin.c 22 KB

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