topwin.c 26 KB

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