topwin.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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 void rtgui_topwin_update_clip(void);
  26. static void rtgui_topwin_redraw(struct rtgui_rect* rect);
  27. #define WINTITLE_CB_WIDTH 14
  28. #define WINTITLE_CB_HEIGHT 14
  29. void rtgui_topwin_init()
  30. {
  31. /* init window list */
  32. rtgui_list_init(&_rtgui_topwin_show_list);
  33. rtgui_list_init(&_rtgui_topwin_hide_list);
  34. }
  35. static struct rtgui_topwin*
  36. rtgui_topwin_search_in_list(struct rtgui_win* wid, struct rtgui_list_node* list)
  37. {
  38. struct rtgui_list_node* node;
  39. struct rtgui_topwin* topwin;
  40. /* search in list */
  41. rtgui_list_foreach(node, list)
  42. {
  43. topwin = rtgui_list_entry(node, struct rtgui_topwin, list);
  44. /* is this node? */
  45. if (topwin->wid == wid)
  46. {
  47. return topwin;
  48. }
  49. }
  50. return RT_NULL;
  51. }
  52. /* add a window to window list[hide] */
  53. rt_err_t rtgui_topwin_add(struct rtgui_event_win_create* event)
  54. {
  55. struct rtgui_topwin* topwin;
  56. topwin = rtgui_malloc(sizeof(struct rtgui_topwin));
  57. if (topwin == RT_NULL) return -RT_ERROR;
  58. topwin->wid = event->wid;
  59. topwin->extent = event->extent;
  60. topwin->tid = event->parent.sender;
  61. topwin->mask = event->mask;
  62. topwin->flag = 0;
  63. if (event->flag & RTGUI_WIN_STYLE_NO_TITLE) topwin->flag |= WINTITLE_NO;
  64. if (event->flag & RTGUI_WIN_STYLE_CLOSEBOX) topwin->flag |= WINTITLE_CLOSEBOX;
  65. if (!(event->flag & RTGUI_WIN_STYLE_NO_BORDER)) topwin->flag |= WINTITLE_BORDER;
  66. if (event->flag & RTGUI_WIN_STYLE_NO_FOCUS) topwin->flag |= WINTITLE_NOFOCUS;
  67. if(!(topwin->flag & WINTITLE_NO) || (topwin->flag & WINTITLE_BORDER))
  68. {
  69. /* get win extent */
  70. rtgui_rect_t rect = topwin->extent;
  71. /* add border rect */
  72. if (topwin->flag & WINTITLE_BORDER)
  73. {
  74. rect.x1 -= WINTITLE_BORDER_SIZE;
  75. rect.y1 -= WINTITLE_BORDER_SIZE;
  76. rect.x2 += WINTITLE_BORDER_SIZE;
  77. rect.y2 += WINTITLE_BORDER_SIZE;
  78. }
  79. /* add title rect */
  80. if (!(topwin->flag & WINTITLE_NO)) rect.y1 -= WINTITLE_HEIGHT;
  81. topwin->title = rtgui_wintitle_create(event->title);
  82. rtgui_widget_set_rect(RTGUI_WIDGET(topwin->title), &rect);
  83. /* update clip info */
  84. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(topwin->title));
  85. rtgui_region_subtract_rect(&(RTGUI_WIDGET(topwin->title)->clip),
  86. &(RTGUI_WIDGET(topwin->title)->clip),
  87. &(topwin->extent));
  88. }
  89. else topwin->title = RT_NULL;
  90. rtgui_list_init(&topwin->list);
  91. rtgui_list_init(&topwin->monitor_list);
  92. /* add topwin node to the hidden window list */
  93. rtgui_list_insert(&(_rtgui_topwin_hide_list), &(topwin->list));
  94. return RT_EOK;
  95. }
  96. rt_err_t rtgui_topwin_remove(struct rtgui_win* wid)
  97. {
  98. struct rtgui_topwin* topwin;
  99. /* find the topwin node */
  100. topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
  101. if (topwin)
  102. {
  103. /* remove node from list */
  104. rtgui_list_remove(&_rtgui_topwin_show_list, &(topwin->list));
  105. rtgui_topwin_update_clip();
  106. /* redraw the old rect */
  107. rtgui_topwin_redraw(&(topwin->extent));
  108. if (rtgui_server_focus_topwin == topwin)
  109. {
  110. /* activate the next window */
  111. if (_rtgui_topwin_show_list.next != RT_NULL)
  112. {
  113. struct rtgui_event_win wevent;
  114. struct rtgui_topwin* wnd;
  115. /* get the topwin */
  116. wnd = rtgui_list_entry(_rtgui_topwin_show_list.next,
  117. struct rtgui_topwin, list);
  118. /* activate the window */
  119. RTGUI_EVENT_WIN_ACTIVATE_INIT(&wevent);
  120. wevent.wid = wnd->wid;
  121. rtgui_thread_send(wnd->tid, &(wevent.parent), sizeof(struct rtgui_event_win));
  122. /* set new focus topwin */
  123. rtgui_server_focus_topwin = wnd;
  124. }
  125. else
  126. {
  127. /* there is no shown window right now */
  128. rtgui_server_focus_topwin = RT_NULL;
  129. }
  130. }
  131. /* free the monitor rect list, topwin node and title */
  132. while (topwin->monitor_list.next != RT_NULL)
  133. {
  134. struct rtgui_mouse_monitor* monitor = rtgui_list_entry(topwin->monitor_list.next,
  135. struct rtgui_mouse_monitor, list);
  136. topwin->monitor_list.next = topwin->monitor_list.next->next;
  137. rtgui_free(monitor);
  138. }
  139. /* destroy win title */
  140. rtgui_wintitle_destroy(topwin->title);
  141. topwin->title = RT_NULL;
  142. rtgui_free(topwin);
  143. return RT_EOK;
  144. }
  145. topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list);
  146. if (topwin)
  147. {
  148. /* remove node from list */
  149. rtgui_list_remove(&_rtgui_topwin_hide_list, &(topwin->list));
  150. /* free the topwin node and title */
  151. rtgui_wintitle_destroy(topwin->title);
  152. topwin->title = RT_NULL;
  153. rtgui_free(topwin);
  154. return RT_EOK;
  155. }
  156. return -RT_ERROR;
  157. }
  158. /* activate a win
  159. * - deactivate the old focus win
  160. * - activate a win
  161. * - set the focus win to activate win
  162. * - draw win title
  163. */
  164. void rtgui_topwin_activate_win(struct rtgui_topwin* win)
  165. {
  166. struct rtgui_event_win event;
  167. /* activate the raised window */
  168. RTGUI_EVENT_WIN_ACTIVATE_INIT(&event);
  169. event.wid = win->wid;
  170. rtgui_thread_send(win->tid, &(event.parent), sizeof(struct rtgui_event_win));
  171. /* redraw title */
  172. if (win->title != RT_NULL)
  173. {
  174. win->flag |= WINTITLE_ACTIVATE;
  175. rtgui_theme_draw_win(win);
  176. }
  177. if (rtgui_server_focus_topwin != RT_NULL)
  178. {
  179. /* deactivate the old focus win */
  180. RTGUI_EVENT_WIN_DEACTIVATE_INIT(&event);
  181. event.wid = rtgui_server_focus_topwin->wid;
  182. rtgui_thread_send(rtgui_server_focus_topwin->tid,
  183. &event.parent, sizeof(struct rtgui_event_win));
  184. /* redraw title */
  185. if (rtgui_server_focus_topwin->title != RT_NULL)
  186. {
  187. rtgui_server_focus_topwin->flag &= ~WINTITLE_ACTIVATE;
  188. rtgui_theme_draw_win(rtgui_server_focus_topwin);
  189. }
  190. }
  191. rtgui_server_focus_topwin = win;
  192. }
  193. /*
  194. * deactivate a win
  195. * - deactivate the win
  196. * - redraw win title
  197. * - set rtgui_server_focus_topwin
  198. */
  199. void rtgui_topwin_deactivate_win(struct rtgui_topwin* win)
  200. {
  201. /* deactivate win */
  202. struct rtgui_event_win event;
  203. RTGUI_EVENT_WIN_DEACTIVATE_INIT(&event);
  204. event.wid = win->wid;
  205. rtgui_thread_send(win->tid,
  206. &event.parent, sizeof(struct rtgui_event_win));
  207. win->flag &= ~WINTITLE_ACTIVATE;
  208. rtgui_theme_draw_win(win);
  209. if (rtgui_server_focus_topwin == win)
  210. {
  211. rtgui_server_focus_topwin = RT_NULL;
  212. }
  213. }
  214. /* raise window to front */
  215. void rtgui_topwin_raise(struct rtgui_win* wid, rt_thread_t sender)
  216. {
  217. struct rtgui_topwin* topwin;
  218. /* find the topwin node */
  219. topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
  220. if (topwin)
  221. {
  222. rt_int32_t count;
  223. struct rtgui_list_node* node;
  224. struct rtgui_event_clip_info* eclip;
  225. struct rtgui_rect* rect;
  226. /* the window is already placed in front */
  227. if (&(topwin->list) == _rtgui_topwin_show_list.next)
  228. {
  229. rtgui_server_focus_topwin = RT_NULL;
  230. rtgui_topwin_activate_win(topwin);
  231. return ;
  232. }
  233. /* update clip info */
  234. count = 0;
  235. node = _rtgui_topwin_show_list.next;
  236. while (node != &(topwin->list))
  237. {
  238. count ++;
  239. node = node->next;
  240. }
  241. eclip = (struct rtgui_event_clip_info*)rtgui_malloc(sizeof(struct rtgui_event_clip_info)
  242. + (count + 1)* sizeof(struct rtgui_rect));
  243. /* reset clip info to top window */
  244. RTGUI_EVENT_CLIP_INFO_INIT(eclip);
  245. eclip->num_rect = 0;
  246. eclip->wid = topwin->wid;
  247. /* send to destination window */
  248. rtgui_thread_send(topwin->tid, &(eclip->parent), sizeof(struct rtgui_event_clip_info));
  249. /* reset clip info in title */
  250. rtgui_toplevel_handle_clip(RTGUI_TOPLEVEL(topwin->title), eclip);
  251. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(topwin->title));
  252. rtgui_region_subtract_rect(&(RTGUI_WIDGET(topwin->title)->clip),
  253. &(RTGUI_WIDGET(topwin->title)->clip),
  254. &(topwin->extent));
  255. rect = RTGUI_EVENT_GET_RECT(eclip, 0);
  256. *rect = (topwin->title != RT_NULL)? RTGUI_WIDGET(topwin->title)->extent : topwin->extent;
  257. count = 1;
  258. for (node = _rtgui_topwin_show_list.next;
  259. node != &(topwin->list);
  260. node = node->next)
  261. {
  262. struct rtgui_topwin* wnd;
  263. wnd = rtgui_list_entry(node, struct rtgui_topwin, list);
  264. eclip->num_rect = count;
  265. eclip->wid = wnd->wid;
  266. /* send to destination window */
  267. rtgui_thread_send(wnd->tid, &(eclip->parent),
  268. sizeof(struct rtgui_event_clip_info) + count * sizeof(struct rtgui_rect));
  269. /* reset clip info in title */
  270. rtgui_toplevel_handle_clip(RTGUI_TOPLEVEL(wnd->title), eclip);
  271. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(wnd->title));
  272. rtgui_region_subtract_rect(&(RTGUI_WIDGET(wnd->title)->clip),
  273. &(RTGUI_WIDGET(wnd->title)->clip),
  274. &(wnd->extent));
  275. rect = RTGUI_EVENT_GET_RECT(eclip, count++);
  276. *rect = (wnd->title != RT_NULL)? RTGUI_WIDGET(wnd->title)->extent : wnd->extent;
  277. }
  278. /* release clip info event */
  279. rtgui_free(eclip);
  280. /* remove node from list */
  281. rtgui_list_remove(&_rtgui_topwin_show_list, &(topwin->list));
  282. /* add to front */
  283. rtgui_list_insert(&_rtgui_topwin_show_list, &(topwin->list));
  284. rtgui_topwin_activate_win(topwin);
  285. }
  286. }
  287. /* show a window */
  288. void rtgui_topwin_show(struct rtgui_event_win* event)
  289. {
  290. struct rtgui_topwin* topwin;
  291. struct rtgui_win* wid = event->wid;
  292. rt_thread_t sender = RTGUI_EVENT(event)->sender;
  293. /* find in hide list */
  294. topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list);
  295. /* find it */
  296. if (topwin != RT_NULL)
  297. {
  298. /* remove node from hidden list */
  299. rtgui_list_remove(&_rtgui_topwin_hide_list, &(topwin->list));
  300. /* add node to show list */
  301. rtgui_list_insert(&_rtgui_topwin_show_list, &(topwin->list));
  302. /* show window title */
  303. if (topwin->title != RT_NULL)
  304. {
  305. RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(topwin->title));
  306. }
  307. /* update clip info */
  308. rtgui_topwin_update_clip();
  309. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
  310. /* activate this window */
  311. rtgui_topwin_activate_win(topwin);
  312. }
  313. else
  314. {
  315. /* the wnd is located in show list, raise wnd to front */
  316. topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
  317. if (topwin != RT_NULL)
  318. {
  319. if (_rtgui_topwin_show_list.next != &(topwin->list))
  320. {
  321. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
  322. /* not the front window, raise it */
  323. rtgui_topwin_raise(wid, sender);
  324. }
  325. }
  326. else
  327. {
  328. /* there is no wnd in wnd list */
  329. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_ERROR);
  330. }
  331. }
  332. }
  333. /* send clip info to panel */
  334. void rtgui_topwin_update_clip_to_panel(struct rtgui_panel* panel)
  335. {
  336. rt_uint32_t count;
  337. rt_thread_t tid;
  338. struct rtgui_list_node* node;
  339. struct rtgui_event_clip_info* eclip;
  340. /* get topwin count */
  341. count = 0;
  342. node = _rtgui_topwin_show_list.next;
  343. while (node != RT_NULL)
  344. {
  345. count ++;
  346. node = node->next;
  347. }
  348. eclip = (struct rtgui_event_clip_info*)rtgui_malloc(sizeof(struct rtgui_event_clip_info)
  349. + (count + 1)* sizeof(struct rtgui_rect));
  350. if (eclip == RT_NULL)
  351. {
  352. /* no memory */
  353. return ;
  354. }
  355. /* reset clip info to top window */
  356. RTGUI_EVENT_CLIP_INFO_INIT(eclip);
  357. eclip->num_rect = count; eclip->wid = RT_NULL;
  358. count = 0;
  359. for (node = _rtgui_topwin_show_list.next; node != RT_NULL; node = node->next)
  360. {
  361. struct rtgui_topwin* wnd;
  362. struct rtgui_rect* rect;
  363. wnd = rtgui_list_entry(node, struct rtgui_topwin, list);
  364. rect = RTGUI_EVENT_GET_RECT(eclip, count++);
  365. *rect = (wnd->title != RT_NULL)? RTGUI_WIDGET(wnd->title)->extent : wnd->extent;
  366. }
  367. /* send to the activated thread of panel */
  368. tid = rtgui_panel_get_active_thread(panel);
  369. rtgui_thread_send(tid, (struct rtgui_event*)eclip, sizeof(struct rtgui_event_clip_info)
  370. + count* sizeof(struct rtgui_rect));
  371. /* release clip info event */
  372. rtgui_free(eclip);
  373. }
  374. /* hide a window */
  375. void rtgui_topwin_hide(struct rtgui_event_win* event)
  376. {
  377. struct rtgui_topwin* topwin;
  378. struct rtgui_win* wid = event->wid;
  379. /* find in show list */
  380. topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
  381. /* found it */
  382. if (topwin)
  383. {
  384. /* remove node from show list */
  385. rtgui_list_remove(&_rtgui_topwin_show_list, &(topwin->list));
  386. /* add node to hidden list */
  387. rtgui_list_insert(&_rtgui_topwin_hide_list, &(topwin->list));
  388. /* show window title */
  389. if (topwin->title != RT_NULL)
  390. {
  391. RTGUI_WIDGET_HIDE(RTGUI_WIDGET(topwin->title));
  392. }
  393. /* update clip info */
  394. rtgui_topwin_update_clip();
  395. /* redraw the old rect */
  396. rtgui_topwin_redraw(&(topwin->extent));
  397. if (rtgui_server_focus_topwin == topwin)
  398. {
  399. /* activate the next window */
  400. if (_rtgui_topwin_show_list.next != RT_NULL)
  401. {
  402. /* get the topwin */
  403. topwin = rtgui_list_entry(_rtgui_topwin_show_list.next,
  404. struct rtgui_topwin, list);
  405. rtgui_server_focus_topwin = RT_NULL;
  406. rtgui_topwin_activate_win(topwin);
  407. }
  408. else
  409. {
  410. /* there is no shown window right now */
  411. rtgui_server_focus_topwin = RT_NULL;
  412. }
  413. }
  414. }
  415. else
  416. {
  417. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_ERROR);
  418. return;
  419. }
  420. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
  421. }
  422. /* move top window */
  423. void rtgui_topwin_move(struct rtgui_event_win_move* event)
  424. {
  425. struct rtgui_topwin* topwin;
  426. /* find in show list */
  427. topwin = rtgui_topwin_search_in_list(event->wid, &_rtgui_topwin_show_list);
  428. if (topwin != RT_NULL)
  429. {
  430. int dx, dy;
  431. rtgui_rect_t rect; /* the old topwin coverage area */
  432. struct rtgui_list_node* node;
  433. /* send status ok */
  434. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
  435. /* get the delta move x, y */
  436. dx = event->x - topwin->extent.x1;
  437. dy = event->y - topwin->extent.y1;
  438. rect = topwin->extent;
  439. /* move window rect */
  440. rtgui_rect_moveto(&(topwin->extent), dx, dy);
  441. /* move window title */
  442. if (topwin->title != RT_NULL)
  443. {
  444. rect = RTGUI_WIDGET(topwin->title)->extent;
  445. rtgui_widget_move_to_logic(RTGUI_WIDGET(topwin->title), dx, dy);
  446. }
  447. /* move the monitor rect list */
  448. rtgui_list_foreach(node, &(topwin->monitor_list))
  449. {
  450. struct rtgui_mouse_monitor* monitor = rtgui_list_entry(node,
  451. struct rtgui_mouse_monitor,
  452. list);
  453. rtgui_rect_moveto(&(monitor->rect), dx, dy);
  454. }
  455. /* update windows clip info */
  456. rtgui_topwin_update_clip();
  457. /* update top window title */
  458. if (topwin->title != RT_NULL) rtgui_theme_draw_win(topwin);
  459. if (rtgui_rect_is_intersect(&rect, &(topwin->extent)) != RT_EOK)
  460. {
  461. /*
  462. * the old rect is not intersect with moved rect,
  463. * re-paint window
  464. */
  465. struct rtgui_event_paint epaint;
  466. RTGUI_EVENT_PAINT_INIT(&epaint);
  467. epaint.wid = topwin->wid;
  468. rtgui_thread_send(topwin->tid, &(epaint.parent), sizeof(epaint));
  469. }
  470. /* update old window coverage area */
  471. rtgui_topwin_redraw(&rect);
  472. }
  473. else
  474. {
  475. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_ERROR);
  476. }
  477. }
  478. /*
  479. * resize a top win
  480. * Note: currently, only support resize hidden window
  481. */
  482. void rtgui_topwin_resize(struct rtgui_win* wid, rtgui_rect_t* r)
  483. {
  484. struct rtgui_topwin* topwin;
  485. /* find in show list */
  486. topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list);
  487. if (topwin)
  488. {
  489. topwin->extent = *r;
  490. if (topwin->title != RT_NULL)
  491. {
  492. /* get win extent */
  493. rtgui_rect_t rect = topwin->extent;
  494. /* add border rect */
  495. if (topwin->flag & WINTITLE_BORDER)
  496. {
  497. rect.x1 -= WINTITLE_BORDER_SIZE;
  498. rect.y1 -= WINTITLE_BORDER_SIZE;
  499. rect.x2 += WINTITLE_BORDER_SIZE;
  500. rect.y2 += WINTITLE_BORDER_SIZE;
  501. }
  502. /* add title rect */
  503. if (!(topwin->flag & WINTITLE_NO)) rect.y1 -= WINTITLE_HEIGHT;
  504. RTGUI_WIDGET(topwin->title)->extent = rect;
  505. /* update title & border clip info */
  506. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(topwin->title));
  507. rtgui_region_subtract_rect(&(RTGUI_WIDGET(topwin->title)->clip),
  508. &(RTGUI_WIDGET(topwin->title)->clip),
  509. &(topwin->extent));
  510. }
  511. }
  512. }
  513. struct rtgui_topwin* rtgui_topwin_get_wnd(int x, int y)
  514. {
  515. struct rtgui_list_node* node;
  516. struct rtgui_topwin* topwin;
  517. /* search in list */
  518. rtgui_list_foreach(node, &(_rtgui_topwin_show_list))
  519. {
  520. topwin = rtgui_list_entry(node, struct rtgui_topwin, list);
  521. /* is this window? */
  522. if ((topwin->title != RT_NULL) &&
  523. rtgui_rect_contains_point(&(RTGUI_WIDGET(topwin->title)->extent), x, y) == RT_EOK)
  524. {
  525. return topwin;
  526. }
  527. else if (rtgui_rect_contains_point(&(topwin->extent), x, y) == RT_EOK)
  528. {
  529. return topwin;
  530. }
  531. }
  532. return RT_NULL;
  533. }
  534. extern struct rtgui_list_node _rtgui_panel_list;
  535. static void rtgui_topwin_update_clip()
  536. {
  537. rt_int32_t count = 0;
  538. struct rtgui_event_clip_info* eclip;
  539. struct rtgui_list_node* node = _rtgui_topwin_show_list.next;
  540. /* calculate count */
  541. while (node != RT_NULL)
  542. {
  543. count ++;
  544. node = node->next;
  545. }
  546. eclip = (struct rtgui_event_clip_info*)rtgui_malloc(sizeof(struct rtgui_event_clip_info)
  547. + count * sizeof(struct rtgui_rect));
  548. RTGUI_EVENT_CLIP_INFO_INIT(eclip);
  549. count = 0;
  550. rtgui_list_foreach(node, &_rtgui_topwin_show_list)
  551. {
  552. struct rtgui_rect* rect;
  553. struct rtgui_topwin* wnd;
  554. wnd = rtgui_list_entry(node, struct rtgui_topwin, list);
  555. eclip->num_rect = count;
  556. eclip->wid = wnd->wid;
  557. /* send to destination window */
  558. rtgui_thread_send(wnd->tid, &(eclip->parent),
  559. sizeof(struct rtgui_event_clip_info) + count * sizeof(struct rtgui_rect));
  560. /* update clip in win title */
  561. if (wnd->title != RT_NULL)
  562. {
  563. /* reset clip info */
  564. rtgui_toplevel_handle_clip(RTGUI_TOPLEVEL(wnd->title), eclip);
  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. rect = RTGUI_EVENT_GET_RECT(eclip, count++);
  571. *rect = (wnd->title != RT_NULL)? RTGUI_WIDGET(wnd->title)->extent : wnd->extent;
  572. }
  573. /* send clip info to each panel */
  574. eclip->wid = RT_NULL;
  575. eclip->num_rect = count;
  576. rtgui_list_foreach(node, &(_rtgui_panel_list))
  577. {
  578. struct rtgui_panel* panel;
  579. struct rtgui_list_node* panel_node;
  580. panel = rtgui_list_entry(node, struct rtgui_panel, sibling);
  581. rtgui_list_foreach(panel_node, &(panel->thread_list))
  582. {
  583. struct rtgui_panel_thread* thread;
  584. thread = rtgui_list_entry(panel_node, struct rtgui_panel_thread, list);
  585. /* send clip info to panel */
  586. rtgui_thread_send(thread->tid, &(eclip->parent),
  587. sizeof(struct rtgui_event_clip_info) + count * sizeof(struct rtgui_rect));
  588. }
  589. }
  590. /* release clip info event */
  591. rtgui_free(eclip);
  592. }
  593. static void rtgui_topwin_redraw(struct rtgui_rect* rect)
  594. {
  595. struct rtgui_list_node* node;
  596. struct rtgui_event_paint epaint;
  597. RTGUI_EVENT_PAINT_INIT(&epaint);
  598. rtgui_list_foreach(node, &_rtgui_topwin_show_list)
  599. {
  600. struct rtgui_topwin* wnd = rtgui_list_entry(node, struct rtgui_topwin, list);
  601. if (rtgui_rect_is_intersect(rect, &(wnd->extent)) == RT_EOK)
  602. {
  603. /* draw window */
  604. epaint.wid = wnd->wid;
  605. rtgui_thread_send(wnd->tid, &(epaint.parent), sizeof(epaint));
  606. /* draw title */
  607. if (wnd->title != RT_NULL)
  608. {
  609. rtgui_theme_draw_win(wnd);
  610. }
  611. }
  612. }
  613. /* redraw the panel */
  614. rtgui_list_foreach(node, &(_rtgui_panel_list))
  615. {
  616. struct rtgui_panel* panel;
  617. panel = rtgui_list_entry(node, struct rtgui_panel, sibling);
  618. if (rtgui_rect_is_intersect(rect, &(panel->extent)) == RT_EOK)
  619. {
  620. /* draw panel */
  621. epaint.wid = RT_NULL;
  622. rtgui_thread_send(rtgui_panel_get_active_thread(panel),
  623. &(epaint.parent), sizeof(epaint));
  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. else
  652. {
  653. /* maybe move window */
  654. rtgui_winrect_set(win);
  655. }
  656. }
  657. else if (event->button & RTGUI_MOUSE_BUTTON_UP)
  658. {
  659. if (rtgui_rect_contains_point(&rect, event->x, event->y) == RT_EOK)
  660. {
  661. struct rtgui_event_win event;
  662. win->flag &= ~WINTITLE_CB_PRESSED;
  663. rtgui_theme_draw_win(win);
  664. /* send close event to window */
  665. RTGUI_EVENT_WIN_CLOSE_INIT(&event);
  666. event.wid = win->wid;
  667. rtgui_thread_send(win->tid, &(event.parent), sizeof(struct rtgui_event_win));
  668. }
  669. }
  670. }
  671. }
  672. void rtgui_topwin_append_monitor_rect(struct rtgui_win* wid, rtgui_rect_t* rect)
  673. {
  674. struct rtgui_topwin* win;
  675. /* parameters check */
  676. if (wid == RT_NULL || rect == RT_NULL) return;
  677. /* find topwin */
  678. win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
  679. if (win == RT_NULL)
  680. win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list);
  681. if (win == RT_NULL) return;
  682. /* append rect to top window monitor rect list */
  683. rtgui_mouse_monitor_append(&(win->monitor_list), rect);
  684. }
  685. void rtgui_topwin_remove_monitor_rect(struct rtgui_win* wid, rtgui_rect_t* rect)
  686. {
  687. struct rtgui_topwin* win;
  688. /* parameters check */
  689. if (wid == RT_NULL || rect == RT_NULL) return;
  690. /* find topwin */
  691. win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
  692. if (win == RT_NULL)
  693. win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list);
  694. if (win == RT_NULL) return;
  695. /* remove rect from top window monitor rect list */
  696. rtgui_mouse_monitor_remove(&(win->monitor_list), rect);
  697. }
  698. void rtgui_topwin_dump()
  699. {
  700. struct rtgui_list_node* node;
  701. rtgui_list_foreach(node, &_rtgui_topwin_show_list)
  702. {
  703. struct rtgui_topwin* wnd = rtgui_list_entry(node, struct rtgui_topwin, list);
  704. rt_kprintf("wnd at (%d, %d) - (%d, %d)\n",
  705. wnd->extent.x1, wnd->extent.y1, wnd->extent.x2, wnd->extent.y2);
  706. if (wnd->title != RT_NULL)
  707. {
  708. rt_kprintf("title[%s] border (%d, %d) - (%d, %d)\n", wnd->title->title,
  709. RTGUI_WIDGET(wnd->title)->extent.x1, RTGUI_WIDGET(wnd->title)->extent.y1,
  710. RTGUI_WIDGET(wnd->title)->extent.x2, RTGUI_WIDGET(wnd->title)->extent.y2);
  711. }
  712. }
  713. }