workbench.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /*
  2. * File : workbench.c
  3. * This file is part of RTGUI in 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-04 Bernard first version
  13. * 2010-09-24 Bernard fix workbench destroy issue
  14. */
  15. #include <rtgui/rtgui_system.h>
  16. #include <rtgui/widgets/window.h>
  17. #include <rtgui/widgets/workbench.h>
  18. static void _rtgui_workbench_constructor(rtgui_workbench_t *workbench)
  19. {
  20. /* set event handler */
  21. rtgui_widget_set_event_handler(RTGUI_WIDGET(workbench), rtgui_workbench_event_handler);
  22. /* set attributes */
  23. workbench->flag = RTGUI_WORKBENCH_FLAG_DEFAULT;
  24. workbench->panel = RT_NULL;
  25. workbench->title = RT_NULL;
  26. workbench->current_view = RT_NULL;
  27. workbench->modal_code = RTGUI_MODAL_OK;
  28. workbench->modal_widget = RT_NULL;
  29. }
  30. static void _rtgui_workbench_destructor(rtgui_workbench_t *workbench)
  31. {
  32. RT_ASSERT(workbench != RT_NULL);
  33. if (RTGUI_TOPLEVEL(workbench)->server != RT_NULL)
  34. {
  35. struct rtgui_event_panel_detach edetach;
  36. RTGUI_EVENT_PANEL_DETACH_INIT(&edetach);
  37. /* detach from panel */
  38. edetach.panel = workbench->panel;
  39. /* send PANEL DETACH to server */
  40. if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(workbench)->server,
  41. RTGUI_EVENT(&edetach), sizeof(struct rtgui_event_panel_detach)) != RT_EOK)
  42. return;
  43. RTGUI_TOPLEVEL(workbench)->server = RT_NULL;
  44. }
  45. /* release title */
  46. rt_free(workbench->title);
  47. workbench->title = RT_NULL;
  48. }
  49. DEFINE_CLASS_TYPE(workbench, "workbench",
  50. RTGUI_TOPLEVEL_TYPE,
  51. _rtgui_workbench_constructor,
  52. _rtgui_workbench_destructor,
  53. sizeof(struct rtgui_workbench));
  54. rtgui_workbench_t *rtgui_workbench_create(const char* panel_name, const unsigned char* title)
  55. {
  56. struct rtgui_workbench* workbench;
  57. /* the server thread id */
  58. rt_thread_t server = rtgui_thread_get_server();
  59. if (server == RT_NULL)
  60. {
  61. rt_kprintf("can't find rtgui server\n");
  62. return RT_NULL;
  63. }
  64. /* create workbench */
  65. workbench = (rtgui_workbench_t*) rtgui_widget_create (RTGUI_WORKBENCH_TYPE);
  66. if (workbench != RT_NULL)
  67. {
  68. /* the buffer uses to receive event */
  69. union
  70. {
  71. struct rtgui_event_panel_attach ecreate;
  72. struct rtgui_event_panel_info epanel;
  73. char buffer[256]; /* use to recv other information */
  74. } event;
  75. /* set workbench title */
  76. workbench->title = (unsigned char*)rt_strdup((char*)title);
  77. /* create application in server */
  78. RTGUI_EVENT_PANEL_ATTACH_INIT(&(event.ecreate));
  79. /* set the panel name and workbench */
  80. rt_strncpy(event.ecreate.panel_name, panel_name, RTGUI_NAME_MAX);
  81. event.ecreate.workbench = workbench;
  82. /* send PANEL ATTACH to server */
  83. if (rtgui_thread_send_sync(server,
  84. &(event.ecreate.parent), sizeof(struct rtgui_event_panel_attach)) != RTGUI_STATUS_OK)
  85. {
  86. return RT_NULL;
  87. }
  88. /* get PANEL INFO */
  89. rtgui_thread_recv_filter(RTGUI_EVENT_PANEL_INFO, &(event.epanel.parent), sizeof(event));
  90. /* set panel */
  91. workbench->panel = (struct rtgui_panel*)event.epanel.panel;
  92. /* connected */
  93. RTGUI_TOPLEVEL(workbench)->server = server;
  94. /* set extent of workbench */
  95. rtgui_widget_set_rect(RTGUI_WIDGET(workbench), &(event.epanel.extent));
  96. /* set workbench in thread */
  97. rtgui_thread_set_widget(RTGUI_WIDGET(workbench));
  98. }
  99. return workbench;
  100. }
  101. void rtgui_workbench_destroy(rtgui_workbench_t* workbench)
  102. {
  103. RT_ASSERT(workbench != RT_NULL);
  104. if (workbench->flag & RTGUI_WORKBENCH_FLAG_CLOSED)
  105. {
  106. rtgui_widget_destroy(RTGUI_WIDGET(workbench));
  107. }
  108. else
  109. {
  110. /* just close workbench */
  111. rtgui_workbench_close(workbench);
  112. }
  113. }
  114. void rtgui_workbench_close(rtgui_workbench_t* workbench)
  115. {
  116. /* detach workbench in server */
  117. if (RTGUI_TOPLEVEL(workbench)->server != RT_NULL)
  118. {
  119. struct rtgui_event_panel_detach edetach;
  120. RTGUI_EVENT_PANEL_DETACH_INIT(&edetach);
  121. /* detach from panel */
  122. edetach.panel = workbench->panel;
  123. edetach.workbench = workbench;
  124. /* send PANEL DETACH to server */
  125. if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(workbench)->server,
  126. RTGUI_EVENT(&edetach), sizeof(struct rtgui_event_panel_detach)) != RT_EOK)
  127. return;
  128. RTGUI_TOPLEVEL(workbench)->server = RT_NULL;
  129. }
  130. /* set status to close */
  131. workbench->flag |= RTGUI_WORKBENCH_FLAG_CLOSED;
  132. }
  133. void rtgui_workbench_set_flag(rtgui_workbench_t* workbench, rt_uint8_t flag)
  134. {
  135. RT_ASSERT(workbench != RT_NULL);
  136. workbench->flag = flag;
  137. }
  138. rt_bool_t rtgui_workbench_event_loop(rtgui_workbench_t* workbench)
  139. {
  140. rt_err_t result;
  141. rtgui_thread_t* tid;
  142. struct rtgui_event* event;
  143. tid = rtgui_thread_self();
  144. RT_ASSERT(tid != RT_NULL);
  145. /* point to event buffer */
  146. event = (struct rtgui_event*)tid->event_buffer;
  147. if (workbench->flag & RTGUI_WORKBENCH_FLAG_MODAL_MODE)
  148. {
  149. /* event loop for modal mode shown view */
  150. while (workbench->flag & RTGUI_WORKBENCH_FLAG_MODAL_MODE)
  151. {
  152. if (tid->on_idle != RT_NULL)
  153. {
  154. result = rtgui_thread_recv_nosuspend(event, RTGUI_EVENT_BUFFER_SIZE);
  155. if (result == RT_EOK)
  156. RTGUI_WIDGET(workbench)->event_handler(RTGUI_WIDGET(workbench), event);
  157. else if (result == -RT_ETIMEOUT)
  158. tid->on_idle(RTGUI_WIDGET(workbench), RT_NULL);
  159. }
  160. else
  161. {
  162. result = rtgui_thread_recv(event, RTGUI_EVENT_BUFFER_SIZE);
  163. if (result == RT_EOK)
  164. RTGUI_WIDGET(workbench)->event_handler(RTGUI_WIDGET(workbench), event);
  165. }
  166. }
  167. }
  168. else
  169. {
  170. /* show workbench firstly */
  171. rtgui_workbench_show(workbench);
  172. while (!(workbench->flag & RTGUI_WORKBENCH_FLAG_CLOSED))
  173. {
  174. if (tid->on_idle != RT_NULL)
  175. {
  176. result = rtgui_thread_recv_nosuspend(event, RTGUI_EVENT_BUFFER_SIZE);
  177. if (result == RT_EOK)
  178. RTGUI_WIDGET(workbench)->event_handler(RTGUI_WIDGET(workbench), event);
  179. else if (result == -RT_ETIMEOUT)
  180. tid->on_idle(RTGUI_WIDGET(workbench), RT_NULL);
  181. }
  182. else
  183. {
  184. result = rtgui_thread_recv(event, RTGUI_EVENT_BUFFER_SIZE);
  185. if (result == RT_EOK)
  186. RTGUI_WIDGET(workbench)->event_handler(RTGUI_WIDGET(workbench), event);
  187. }
  188. }
  189. }
  190. return RT_TRUE;
  191. }
  192. rt_err_t rtgui_workbench_show(rtgui_workbench_t* workbench)
  193. {
  194. RT_ASSERT(workbench != RT_NULL);
  195. if (RTGUI_TOPLEVEL(workbench)->server != RT_NULL)
  196. {
  197. struct rtgui_event_panel_show eraise;
  198. RTGUI_EVENT_PANEL_SHOW_INIT(&eraise);
  199. eraise.workbench = workbench;
  200. eraise.panel = workbench->panel;
  201. if (rtgui_thread_send_sync(workbench->parent.server, RTGUI_EVENT(&eraise),
  202. sizeof(struct rtgui_event_panel_show)) != RT_EOK)
  203. return -RT_ERROR;
  204. RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(workbench));
  205. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(workbench));
  206. }
  207. else return -RT_ERROR;
  208. return RT_EOK;
  209. }
  210. rt_err_t rtgui_workbench_hide(rtgui_workbench_t* workbench)
  211. {
  212. if (RTGUI_TOPLEVEL(workbench)->server != RT_NULL)
  213. {
  214. struct rtgui_event_panel_hide ehide;
  215. RTGUI_EVENT_PANEL_HIDE_INIT(&ehide);
  216. RT_ASSERT(workbench != RT_NULL);
  217. if (workbench->parent.server == RT_NULL) return -RT_ERROR;
  218. ehide.panel = workbench->panel;
  219. if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(workbench)->server, RTGUI_EVENT(&ehide),
  220. sizeof(struct rtgui_event_panel_hide)) != RT_EOK)
  221. return -RT_ERROR;
  222. RTGUI_WIDGET_HIDE(RTGUI_WIDGET(workbench));
  223. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(workbench));
  224. }
  225. return RT_EOK;
  226. }
  227. rt_bool_t rtgui_workbench_event_handler(rtgui_widget_t* widget, rtgui_event_t* event)
  228. {
  229. struct rtgui_workbench* workbench = (struct rtgui_workbench*)widget;
  230. switch (event->type)
  231. {
  232. case RTGUI_EVENT_PANEL_DETACH:
  233. RTGUI_WIDGET_HIDE(RTGUI_WIDGET(workbench));
  234. RTGUI_TOPLEVEL(workbench)->server = RT_NULL;
  235. return RT_TRUE;
  236. case RTGUI_EVENT_PANEL_SHOW:
  237. /* show workbench in server */
  238. rtgui_workbench_show(workbench);
  239. break;
  240. case RTGUI_EVENT_PANEL_HIDE:
  241. /* hide widget */
  242. RTGUI_WIDGET_HIDE(widget);
  243. break;
  244. case RTGUI_EVENT_MOUSE_MOTION:
  245. {
  246. struct rtgui_event_mouse* emouse = (struct rtgui_event_mouse*)event;
  247. struct rtgui_toplevel* top = RTGUI_TOPLEVEL(emouse->wid);
  248. /* check the destination window */
  249. if (top != RT_NULL && RTGUI_WIDGET(top)->event_handler != RT_NULL)
  250. {
  251. RTGUI_WIDGET(top)->event_handler(RTGUI_WIDGET(top), event);
  252. }
  253. else
  254. {
  255. /* let viewer to handle it */
  256. rtgui_view_t* view = workbench->current_view;
  257. if (view != RT_NULL &&
  258. RTGUI_WIDGET(view)->event_handler != RT_NULL)
  259. {
  260. RTGUI_WIDGET(view)->event_handler(RTGUI_WIDGET(view), event);
  261. }
  262. }
  263. }
  264. break;
  265. case RTGUI_EVENT_MOUSE_BUTTON:
  266. {
  267. struct rtgui_event_mouse* emouse = (struct rtgui_event_mouse*)event;
  268. struct rtgui_toplevel* top = RTGUI_TOPLEVEL(emouse->wid);
  269. /* check whether has widget which handled mouse event before */
  270. if (RTGUI_TOPLEVEL_LAST_MEVENT_WIDGET(workbench) != RT_NULL)
  271. {
  272. struct rtgui_event_mouse* emouse;
  273. emouse = (struct rtgui_event_mouse*)event;
  274. RTGUI_TOPLEVEL_LAST_MEVENT_WIDGET(workbench)->event_handler(RTGUI_TOPLEVEL_LAST_MEVENT_WIDGET(workbench), event);
  275. if (rtgui_rect_contains_point(&(RTGUI_TOPLEVEL_LAST_MEVENT_WIDGET(workbench)->extent),
  276. emouse->x, emouse->y) == RT_EOK)
  277. {
  278. RTGUI_TOPLEVEL_LAST_MEVENT_WIDGET(workbench) = RT_NULL;
  279. break; /* mouse event is inside of widget, do not handle it anymore */
  280. }
  281. /* clean last mouse event handled widget */
  282. RTGUI_TOPLEVEL_LAST_MEVENT_WIDGET(workbench) = RT_NULL;
  283. }
  284. /* check the destination window */
  285. if (top != RT_NULL && RTGUI_WIDGET(top)->event_handler != RT_NULL)
  286. {
  287. RTGUI_WIDGET(top)->event_handler(RTGUI_WIDGET(top), event);
  288. }
  289. else
  290. {
  291. if (RTGUI_WORKBENCH_IS_MODAL_MODE(workbench))
  292. {
  293. /* let modal widget to handle it */
  294. if (workbench->modal_widget != RT_NULL &&
  295. workbench->modal_widget->event_handler != RT_NULL)
  296. {
  297. workbench->modal_widget->event_handler(workbench->modal_widget, event);
  298. }
  299. }
  300. else
  301. {
  302. /* let viewer to handle it */
  303. rtgui_view_t* view = workbench->current_view;
  304. if (view != RT_NULL &&
  305. RTGUI_WIDGET(view)->event_handler != RT_NULL)
  306. {
  307. RTGUI_WIDGET(view)->event_handler(RTGUI_WIDGET(view), event);
  308. }
  309. }
  310. }
  311. }
  312. break;
  313. case RTGUI_EVENT_KBD:
  314. {
  315. struct rtgui_event_kbd* kbd = (struct rtgui_event_kbd*)event;
  316. struct rtgui_toplevel* top = RTGUI_TOPLEVEL(kbd->wid);
  317. /* check the destination window */
  318. if (top != RT_NULL && RTGUI_WIDGET(top)->event_handler != RT_NULL)
  319. {
  320. RTGUI_WIDGET(top)->event_handler(RTGUI_WIDGET(top), event);
  321. }
  322. else
  323. {
  324. if (RTGUI_WORKBENCH_IS_MODAL_MODE(workbench))
  325. {
  326. /* let modal widget to handle it */
  327. if (workbench->modal_widget != RT_NULL &&
  328. workbench->modal_widget->event_handler != RT_NULL)
  329. {
  330. workbench->modal_widget->event_handler(workbench->modal_widget, event);
  331. }
  332. }
  333. else
  334. {
  335. if (RTGUI_CONTAINER(widget)->focused == widget)
  336. {
  337. /* set focused widget to the current view */
  338. if (workbench->current_view != RT_NULL)
  339. rtgui_widget_focus(RTGUI_WIDGET(RTGUI_CONTAINER(workbench->current_view)->focused));
  340. }
  341. return rtgui_toplevel_event_handler(widget, event);
  342. }
  343. }
  344. }
  345. break;
  346. case RTGUI_EVENT_PAINT:
  347. {
  348. struct rtgui_event_paint* epaint = (struct rtgui_event_paint*)event;
  349. struct rtgui_toplevel* top = RTGUI_TOPLEVEL(epaint->wid);
  350. /* check the destination window */
  351. if (top != RT_NULL && RTGUI_WIDGET(top)->event_handler != RT_NULL)
  352. {
  353. RTGUI_WIDGET(top)->event_handler(RTGUI_WIDGET(top), event);
  354. }
  355. else
  356. {
  357. rtgui_view_t* view;
  358. /* un-hide workbench */
  359. RTGUI_WIDGET_UNHIDE(widget);
  360. /* paint a view */
  361. view = workbench->current_view;
  362. if (view != RT_NULL)
  363. {
  364. /* remake a paint event */
  365. RTGUI_EVENT_PAINT_INIT(epaint);
  366. epaint->wid = RT_NULL;
  367. /* send this event to the view */
  368. if (RTGUI_WIDGET(view)->event_handler != RT_NULL)
  369. {
  370. RTGUI_WIDGET(view)->event_handler(RTGUI_WIDGET(view), event);
  371. }
  372. }
  373. else
  374. {
  375. struct rtgui_dc* dc;
  376. struct rtgui_rect rect;
  377. dc = rtgui_dc_begin_drawing(widget);
  378. rtgui_widget_get_rect(widget, &rect);
  379. rtgui_dc_fill_rect(dc, &rect);
  380. rtgui_dc_end_drawing(dc);
  381. }
  382. }
  383. }
  384. break;
  385. case RTGUI_EVENT_CLIP_INFO:
  386. {
  387. struct rtgui_event_clip_info* eclip = (struct rtgui_event_clip_info*)event;
  388. struct rtgui_widget* dest_widget = RTGUI_WIDGET(eclip->wid);
  389. if (dest_widget != RT_NULL && dest_widget->event_handler != RT_NULL)
  390. {
  391. dest_widget->event_handler(dest_widget, event);
  392. }
  393. else
  394. {
  395. return rtgui_toplevel_event_handler(widget, event);
  396. }
  397. }
  398. break;
  399. case RTGUI_EVENT_WIN_CLOSE:
  400. case RTGUI_EVENT_WIN_ACTIVATE:
  401. case RTGUI_EVENT_WIN_DEACTIVATE:
  402. {
  403. struct rtgui_event_win* wevent = (struct rtgui_event_win*)event;
  404. struct rtgui_widget* dest_widget = RTGUI_WIDGET(wevent->wid);
  405. if (dest_widget != RT_NULL && dest_widget->event_handler != RT_NULL)
  406. {
  407. dest_widget->event_handler(dest_widget, event);
  408. }
  409. }
  410. break;
  411. case RTGUI_EVENT_WIN_MOVE:
  412. {
  413. struct rtgui_event_win_move* wevent = (struct rtgui_event_win_move*)event;
  414. struct rtgui_toplevel* top = RTGUI_TOPLEVEL(wevent->wid);
  415. if (top != RT_NULL && RTGUI_WIDGET(top)->event_handler != RT_NULL)
  416. {
  417. RTGUI_WIDGET(top)->event_handler(RTGUI_WIDGET(top), event);
  418. }
  419. }
  420. break;
  421. default:
  422. return rtgui_toplevel_event_handler(widget, event);
  423. }
  424. return RT_TRUE;
  425. }
  426. /*
  427. *
  428. * view on workbench
  429. *
  430. */
  431. void rtgui_workbench_add_view(rtgui_workbench_t* workbench, rtgui_view_t* view)
  432. {
  433. rtgui_container_add_child(RTGUI_CONTAINER(workbench), RTGUI_WIDGET(view));
  434. /* hide view in default */
  435. RTGUI_WIDGET_HIDE(RTGUI_WIDGET(view));
  436. /* reset view extent */
  437. rtgui_widget_set_rect(RTGUI_WIDGET(view), &(RTGUI_WIDGET(workbench)->extent));
  438. }
  439. void rtgui_workbench_remove_view(rtgui_workbench_t* workbench, rtgui_view_t* view)
  440. {
  441. if (view == workbench->current_view)
  442. rtgui_workbench_hide_view(workbench, view);
  443. rtgui_container_remove_child(RTGUI_CONTAINER(workbench), RTGUI_WIDGET(view));
  444. }
  445. void rtgui_workbench_show_view(rtgui_workbench_t* workbench, rtgui_view_t* view)
  446. {
  447. RT_ASSERT(workbench != RT_NULL);
  448. RT_ASSERT(view != RT_NULL);
  449. /* already shown */
  450. if (workbench->current_view == view) return;
  451. if (workbench->current_view != RT_NULL)
  452. {
  453. /* hide old view */
  454. RTGUI_WIDGET_HIDE(RTGUI_WIDGET(workbench->current_view));
  455. }
  456. /* show view */
  457. RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(view));
  458. workbench->current_view = view;
  459. /* update workbench clip */
  460. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(workbench));
  461. if (!RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(workbench)))
  462. {
  463. rtgui_widget_update(RTGUI_WIDGET(view));
  464. }
  465. }
  466. void rtgui_workbench_hide_view(rtgui_workbench_t* workbench, rtgui_view_t* view)
  467. {
  468. RT_ASSERT(workbench != RT_NULL);
  469. RT_ASSERT(view != RT_NULL);
  470. /* hide view */
  471. RTGUI_WIDGET_HIDE(RTGUI_WIDGET(view));
  472. if (view == workbench->current_view)
  473. {
  474. rtgui_view_t *next_view;
  475. workbench->current_view = RT_NULL;
  476. next_view = RTGUI_VIEW(rtgui_widget_get_next_sibling(RTGUI_WIDGET(view)));
  477. if (next_view == RT_NULL)
  478. next_view = RTGUI_VIEW(rtgui_widget_get_prev_sibling(RTGUI_WIDGET(view)));
  479. if (next_view != RT_NULL)
  480. {
  481. rtgui_view_show(next_view, RT_FALSE);
  482. }
  483. else
  484. {
  485. /* update workbench clip */
  486. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(workbench));
  487. }
  488. }
  489. }