topwin.c 27 KB

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