workbench.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. /* send PANEL DETACH to server */
  124. if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(workbench)->server,
  125. RTGUI_EVENT(&edetach), sizeof(struct rtgui_event_panel_detach)) != RT_EOK)
  126. return;
  127. RTGUI_TOPLEVEL(workbench)->server = RT_NULL;
  128. }
  129. /* set status to close */
  130. workbench->flag |= RTGUI_WORKBENCH_FLAG_CLOSED;
  131. }
  132. void rtgui_workbench_set_flag(rtgui_workbench_t* workbench, rt_uint8_t flag)
  133. {
  134. RT_ASSERT(workbench != RT_NULL);
  135. workbench->flag = flag;
  136. }
  137. rt_bool_t rtgui_workbench_event_loop(rtgui_workbench_t* workbench)
  138. {
  139. rt_err_t result;
  140. rtgui_thread_t* tid;
  141. struct rtgui_event* event;
  142. tid = rtgui_thread_self();
  143. RT_ASSERT(tid != RT_NULL);
  144. /* point to event buffer */
  145. event = (struct rtgui_event*)tid->event_buffer;
  146. if (workbench->flag & RTGUI_WORKBENCH_FLAG_MODAL_MODE)
  147. {
  148. /* event loop for modal mode shown view */
  149. while (workbench->flag & RTGUI_WORKBENCH_FLAG_MODAL_MODE)
  150. {
  151. if (tid->on_idle != RT_NULL)
  152. {
  153. result = rtgui_thread_recv_nosuspend(event, RTGUI_EVENT_BUFFER_SIZE);
  154. if (result == RT_EOK)
  155. RTGUI_WIDGET(workbench)->event_handler(RTGUI_WIDGET(workbench), event);
  156. else if (result == -RT_ETIMEOUT)
  157. tid->on_idle(RTGUI_WIDGET(workbench), RT_NULL);
  158. }
  159. else
  160. {
  161. result = rtgui_thread_recv(event, RTGUI_EVENT_BUFFER_SIZE);
  162. if (result == RT_EOK)
  163. RTGUI_WIDGET(workbench)->event_handler(RTGUI_WIDGET(workbench), event);
  164. }
  165. }
  166. }
  167. else
  168. {
  169. /* show workbench firstly */
  170. rtgui_workbench_show(workbench);
  171. while (!(workbench->flag & RTGUI_WORKBENCH_FLAG_CLOSED))
  172. {
  173. if (tid->on_idle != RT_NULL)
  174. {
  175. result = rtgui_thread_recv_nosuspend(event, RTGUI_EVENT_BUFFER_SIZE);
  176. if (result == RT_EOK)
  177. RTGUI_WIDGET(workbench)->event_handler(RTGUI_WIDGET(workbench), event);
  178. else if (result == -RT_ETIMEOUT)
  179. tid->on_idle(RTGUI_WIDGET(workbench), RT_NULL);
  180. }
  181. else
  182. {
  183. result = rtgui_thread_recv(event, RTGUI_EVENT_BUFFER_SIZE);
  184. if (result == RT_EOK)
  185. RTGUI_WIDGET(workbench)->event_handler(RTGUI_WIDGET(workbench), event);
  186. }
  187. }
  188. }
  189. return RT_TRUE;
  190. }
  191. rt_err_t rtgui_workbench_show(rtgui_workbench_t* workbench)
  192. {
  193. RT_ASSERT(workbench != RT_NULL);
  194. if (RTGUI_TOPLEVEL(workbench)->server != RT_NULL)
  195. {
  196. struct rtgui_event_panel_show eraise;
  197. RTGUI_EVENT_PANEL_SHOW_INIT(&eraise);
  198. eraise.workbench = workbench;
  199. eraise.panel = workbench->panel;
  200. if (rtgui_thread_send_sync(workbench->parent.server, RTGUI_EVENT(&eraise),
  201. sizeof(struct rtgui_event_panel_show)) != RT_EOK)
  202. return -RT_ERROR;
  203. RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(workbench));
  204. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(workbench));
  205. }
  206. else return -RT_ERROR;
  207. return RT_EOK;
  208. }
  209. rt_err_t rtgui_workbench_hide(rtgui_workbench_t* workbench)
  210. {
  211. if (RTGUI_TOPLEVEL(workbench)->server != RT_NULL)
  212. {
  213. struct rtgui_event_panel_hide ehide;
  214. RTGUI_EVENT_PANEL_HIDE_INIT(&ehide);
  215. RT_ASSERT(workbench != RT_NULL);
  216. if (workbench->parent.server == RT_NULL) return -RT_ERROR;
  217. ehide.panel = workbench->panel;
  218. if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(workbench)->server, RTGUI_EVENT(&ehide),
  219. sizeof(struct rtgui_event_panel_hide)) != RT_EOK)
  220. return -RT_ERROR;
  221. RTGUI_WIDGET_HIDE(RTGUI_WIDGET(workbench));
  222. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(workbench));
  223. }
  224. return RT_EOK;
  225. }
  226. rt_bool_t rtgui_workbench_event_handler(rtgui_widget_t* widget, rtgui_event_t* event)
  227. {
  228. struct rtgui_workbench* workbench = (struct rtgui_workbench*)widget;
  229. switch (event->type)
  230. {
  231. case RTGUI_EVENT_PANEL_DETACH:
  232. RTGUI_WIDGET_HIDE(RTGUI_WIDGET(workbench));
  233. RTGUI_TOPLEVEL(workbench)->server = RT_NULL;
  234. return RT_TRUE;
  235. case RTGUI_EVENT_PANEL_SHOW:
  236. /* show workbench in server */
  237. rtgui_workbench_show(workbench);
  238. break;
  239. case RTGUI_EVENT_PANEL_HIDE:
  240. /* hide widget */
  241. RTGUI_WIDGET_HIDE(widget);
  242. break;
  243. case RTGUI_EVENT_MOUSE_MOTION:
  244. {
  245. struct rtgui_event_mouse* emouse = (struct rtgui_event_mouse*)event;
  246. struct rtgui_toplevel* top = RTGUI_TOPLEVEL(emouse->wid);
  247. /* check the destination window */
  248. if (top != RT_NULL && RTGUI_WIDGET(top)->event_handler != RT_NULL)
  249. {
  250. RTGUI_WIDGET(top)->event_handler(RTGUI_WIDGET(top), event);
  251. }
  252. else
  253. {
  254. /* let viewer to handle it */
  255. rtgui_view_t* view = workbench->current_view;
  256. if (view != RT_NULL &&
  257. RTGUI_WIDGET(view)->event_handler != RT_NULL)
  258. {
  259. RTGUI_WIDGET(view)->event_handler(RTGUI_WIDGET(view), event);
  260. }
  261. }
  262. }
  263. break;
  264. case RTGUI_EVENT_MOUSE_BUTTON:
  265. {
  266. struct rtgui_event_mouse* emouse = (struct rtgui_event_mouse*)event;
  267. struct rtgui_toplevel* top = RTGUI_TOPLEVEL(emouse->wid);
  268. /* check whether has widget which handled mouse event before */
  269. if (RTGUI_TOPLEVEL_LAST_MEVENT_WIDGET(workbench) != RT_NULL)
  270. {
  271. struct rtgui_event_mouse* emouse;
  272. emouse = (struct rtgui_event_mouse*)event;
  273. RTGUI_TOPLEVEL_LAST_MEVENT_WIDGET(workbench)->event_handler(RTGUI_TOPLEVEL_LAST_MEVENT_WIDGET(workbench), event);
  274. if (rtgui_rect_contains_point(&(RTGUI_TOPLEVEL_LAST_MEVENT_WIDGET(workbench)->extent),
  275. emouse->x, emouse->y) == RT_EOK)
  276. {
  277. RTGUI_TOPLEVEL_LAST_MEVENT_WIDGET(workbench) = RT_NULL;
  278. break; /* mouse event is inside of widget, do not handle it anymore */
  279. }
  280. /* clean last mouse event handled widget */
  281. RTGUI_TOPLEVEL_LAST_MEVENT_WIDGET(workbench) = RT_NULL;
  282. }
  283. /* check the destination window */
  284. if (top != RT_NULL && RTGUI_WIDGET(top)->event_handler != RT_NULL)
  285. {
  286. RTGUI_WIDGET(top)->event_handler(RTGUI_WIDGET(top), event);
  287. }
  288. else
  289. {
  290. if (RTGUI_WORKBENCH_IS_MODAL_MODE(workbench))
  291. {
  292. /* let modal widget to handle it */
  293. if (workbench->modal_widget != RT_NULL &&
  294. workbench->modal_widget->event_handler != RT_NULL)
  295. {
  296. workbench->modal_widget->event_handler(workbench->modal_widget, event);
  297. }
  298. }
  299. else
  300. {
  301. /* let viewer to handle it */
  302. rtgui_view_t* view = workbench->current_view;
  303. if (view != RT_NULL &&
  304. RTGUI_WIDGET(view)->event_handler != RT_NULL)
  305. {
  306. RTGUI_WIDGET(view)->event_handler(RTGUI_WIDGET(view), event);
  307. }
  308. }
  309. }
  310. }
  311. break;
  312. case RTGUI_EVENT_KBD:
  313. {
  314. struct rtgui_event_kbd* kbd = (struct rtgui_event_kbd*)event;
  315. struct rtgui_toplevel* top = RTGUI_TOPLEVEL(kbd->wid);
  316. /* check the destination window */
  317. if (top != RT_NULL && RTGUI_WIDGET(top)->event_handler != RT_NULL)
  318. {
  319. RTGUI_WIDGET(top)->event_handler(RTGUI_WIDGET(top), event);
  320. }
  321. else
  322. {
  323. if (RTGUI_WORKBENCH_IS_MODAL_MODE(workbench))
  324. {
  325. /* let modal widget to handle it */
  326. if (workbench->modal_widget != RT_NULL &&
  327. workbench->modal_widget->event_handler != RT_NULL)
  328. {
  329. workbench->modal_widget->event_handler(workbench->modal_widget, event);
  330. }
  331. }
  332. else
  333. {
  334. if (RTGUI_CONTAINER(widget)->focused == widget)
  335. {
  336. /* set focused widget to the current view */
  337. if (workbench->current_view != RT_NULL)
  338. rtgui_widget_focus(RTGUI_WIDGET(RTGUI_CONTAINER(workbench->current_view)->focused));
  339. }
  340. return rtgui_toplevel_event_handler(widget, event);
  341. }
  342. }
  343. }
  344. break;
  345. case RTGUI_EVENT_PAINT:
  346. {
  347. struct rtgui_event_paint* epaint = (struct rtgui_event_paint*)event;
  348. struct rtgui_toplevel* top = RTGUI_TOPLEVEL(epaint->wid);
  349. /* check the destination window */
  350. if (top != RT_NULL && RTGUI_WIDGET(top)->event_handler != RT_NULL)
  351. {
  352. RTGUI_WIDGET(top)->event_handler(RTGUI_WIDGET(top), event);
  353. }
  354. else
  355. {
  356. rtgui_view_t* view;
  357. /* un-hide workbench */
  358. RTGUI_WIDGET_UNHIDE(widget);
  359. /* paint a view */
  360. view = workbench->current_view;
  361. if (view != RT_NULL)
  362. {
  363. /* remake a paint event */
  364. RTGUI_EVENT_PAINT_INIT(epaint);
  365. epaint->wid = RT_NULL;
  366. /* send this event to the view */
  367. if (RTGUI_WIDGET(view)->event_handler != RT_NULL)
  368. {
  369. RTGUI_WIDGET(view)->event_handler(RTGUI_WIDGET(view), event);
  370. }
  371. }
  372. else
  373. {
  374. struct rtgui_dc* dc;
  375. struct rtgui_rect rect;
  376. dc = rtgui_dc_begin_drawing(widget);
  377. rtgui_widget_get_rect(widget, &rect);
  378. rtgui_dc_fill_rect(dc, &rect);
  379. rtgui_dc_end_drawing(dc);
  380. }
  381. }
  382. }
  383. break;
  384. case RTGUI_EVENT_CLIP_INFO:
  385. {
  386. struct rtgui_event_clip_info* eclip = (struct rtgui_event_clip_info*)event;
  387. struct rtgui_widget* dest_widget = RTGUI_WIDGET(eclip->wid);
  388. if (dest_widget != RT_NULL && dest_widget->event_handler != RT_NULL)
  389. {
  390. dest_widget->event_handler(dest_widget, event);
  391. }
  392. else
  393. {
  394. return rtgui_toplevel_event_handler(widget, event);
  395. }
  396. }
  397. break;
  398. case RTGUI_EVENT_WIN_CLOSE:
  399. case RTGUI_EVENT_WIN_ACTIVATE:
  400. case RTGUI_EVENT_WIN_DEACTIVATE:
  401. {
  402. struct rtgui_event_win* wevent = (struct rtgui_event_win*)event;
  403. struct rtgui_widget* dest_widget = RTGUI_WIDGET(wevent->wid);
  404. if (dest_widget != RT_NULL && dest_widget->event_handler != RT_NULL)
  405. {
  406. dest_widget->event_handler(dest_widget, event);
  407. }
  408. }
  409. break;
  410. case RTGUI_EVENT_WIN_MOVE:
  411. {
  412. struct rtgui_event_win_move* wevent = (struct rtgui_event_win_move*)event;
  413. struct rtgui_toplevel* top = RTGUI_TOPLEVEL(wevent->wid);
  414. if (top != RT_NULL && RTGUI_WIDGET(top)->event_handler != RT_NULL)
  415. {
  416. RTGUI_WIDGET(top)->event_handler(RTGUI_WIDGET(top), event);
  417. }
  418. }
  419. break;
  420. default:
  421. return rtgui_toplevel_event_handler(widget, event);
  422. }
  423. return RT_TRUE;
  424. }
  425. /*
  426. *
  427. * view on workbench
  428. *
  429. */
  430. void rtgui_workbench_add_view(rtgui_workbench_t* workbench, rtgui_view_t* view)
  431. {
  432. rtgui_container_add_child(RTGUI_CONTAINER(workbench), RTGUI_WIDGET(view));
  433. /* hide view in default */
  434. RTGUI_WIDGET_HIDE(RTGUI_WIDGET(view));
  435. /* reset view extent */
  436. rtgui_widget_set_rect(RTGUI_WIDGET(view), &(RTGUI_WIDGET(workbench)->extent));
  437. }
  438. void rtgui_workbench_remove_view(rtgui_workbench_t* workbench, rtgui_view_t* view)
  439. {
  440. if (view == workbench->current_view)
  441. rtgui_workbench_hide_view(workbench, view);
  442. rtgui_container_remove_child(RTGUI_CONTAINER(workbench), RTGUI_WIDGET(view));
  443. }
  444. void rtgui_workbench_show_view(rtgui_workbench_t* workbench, rtgui_view_t* view)
  445. {
  446. RT_ASSERT(workbench != RT_NULL);
  447. RT_ASSERT(view != RT_NULL);
  448. /* already shown */
  449. if (workbench->current_view == view) return;
  450. if (workbench->current_view != RT_NULL)
  451. {
  452. /* hide old view */
  453. RTGUI_WIDGET_HIDE(RTGUI_WIDGET(workbench->current_view));
  454. }
  455. /* show view */
  456. RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(view));
  457. workbench->current_view = view;
  458. /* update workbench clip */
  459. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(workbench));
  460. if (!RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(workbench)))
  461. {
  462. rtgui_widget_update(RTGUI_WIDGET(view));
  463. }
  464. }
  465. void rtgui_workbench_hide_view(rtgui_workbench_t* workbench, rtgui_view_t* view)
  466. {
  467. RT_ASSERT(workbench != RT_NULL);
  468. RT_ASSERT(view != RT_NULL);
  469. /* hide view */
  470. RTGUI_WIDGET_HIDE(RTGUI_WIDGET(view));
  471. if (view == workbench->current_view)
  472. {
  473. rtgui_view_t *next_view;
  474. workbench->current_view = RT_NULL;
  475. next_view = RTGUI_VIEW(rtgui_widget_get_next_sibling(RTGUI_WIDGET(view)));
  476. if (next_view == RT_NULL)
  477. next_view = RTGUI_VIEW(rtgui_widget_get_prev_sibling(RTGUI_WIDGET(view)));
  478. if (next_view != RT_NULL)
  479. {
  480. rtgui_view_show(next_view, RT_FALSE);
  481. }
  482. else
  483. {
  484. /* update workbench clip */
  485. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(workbench));
  486. }
  487. }
  488. }