topwin.c 27 KB

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