topwin.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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. /* 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. #ifdef RTGUI_USING_WINMOVE
  787. else
  788. {
  789. /* maybe move window */
  790. rtgui_winrect_set(win);
  791. }
  792. #endif
  793. }
  794. else if (event->button & RTGUI_MOUSE_BUTTON_UP)
  795. {
  796. if (rtgui_rect_contains_point(&rect, event->x, event->y) == RT_EOK)
  797. {
  798. struct rtgui_event_win event;
  799. win->flag &= ~WINTITLE_CB_PRESSED;
  800. rtgui_theme_draw_win(win);
  801. /* send close event to window */
  802. RTGUI_EVENT_WIN_CLOSE_INIT(&event);
  803. event.wid = win->wid;
  804. rtgui_thread_send(win->tid, &(event.parent), sizeof(struct rtgui_event_win));
  805. }
  806. }
  807. }
  808. }
  809. void rtgui_topwin_append_monitor_rect(struct rtgui_win* wid, rtgui_rect_t* rect)
  810. {
  811. struct rtgui_topwin* win;
  812. /* parameters check */
  813. if (wid == RT_NULL || rect == RT_NULL) return;
  814. /* find topwin */
  815. win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
  816. if (win == RT_NULL)
  817. win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list);
  818. if (win == RT_NULL) return;
  819. /* append rect to top window monitor rect list */
  820. rtgui_mouse_monitor_append(&(win->monitor_list), rect);
  821. }
  822. void rtgui_topwin_remove_monitor_rect(struct rtgui_win* wid, rtgui_rect_t* rect)
  823. {
  824. struct rtgui_topwin* win;
  825. /* parameters check */
  826. if (wid == RT_NULL || rect == RT_NULL) return;
  827. /* find topwin */
  828. win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
  829. if (win == RT_NULL)
  830. win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list);
  831. if (win == RT_NULL) return;
  832. /* remove rect from top window monitor rect list */
  833. rtgui_mouse_monitor_remove(&(win->monitor_list), rect);
  834. }
  835. #ifdef RTGUI_USING_SMALL_SIZE
  836. /**
  837. * get clip information for topwin
  838. * wid, the self window id. If wid = NULL, get whole topwin clip information.
  839. *
  840. * @return the clip rect information
  841. */
  842. void rtgui_topwin_get_clipinfo(struct rtgui_rect* rect_list, rt_int32_t count)
  843. {
  844. struct rtgui_rect* rect;
  845. struct rtgui_topwin* topwin;
  846. struct rtgui_list_node* node;
  847. if ((rect_list == RT_NULL) || (count == 0)) return ;
  848. /* set to the header of list */
  849. rect = rect_list;
  850. rt_sem_take(&_rtgui_topwin_lock, RT_WAITING_FOREVER);
  851. /* get all of topwin rect list */
  852. rtgui_list_foreach(node, &_rtgui_topwin_show_list)
  853. {
  854. topwin = rtgui_list_entry(node, struct rtgui_topwin, list);
  855. if (topwin->title != RT_NULL)
  856. *rect = RTGUI_WIDGET(topwin->title)->extent;
  857. else
  858. *rect = topwin->extent;
  859. rect ++;
  860. count --;
  861. if (count <= 0) break;
  862. }
  863. rt_sem_release(&_rtgui_topwin_lock);
  864. }
  865. #endif