window.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /*
  2. * File : window.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. */
  14. #include <rtgui/dc.h>
  15. #include <rtgui/color.h>
  16. #include <rtgui/image.h>
  17. #include <rtgui/rtgui_system.h>
  18. #include <rtgui/widgets/window.h>
  19. #include <rtgui/widgets/button.h>
  20. #include <rtgui/widgets/workbench.h>
  21. static void _rtgui_win_constructor(rtgui_win_t *win)
  22. {
  23. /* init window attribute */
  24. win->on_activate = RT_NULL;
  25. win->on_deactivate = RT_NULL;
  26. win->on_close = RT_NULL;
  27. win->title = RT_NULL;
  28. win->modal_code = RTGUI_MODAL_OK;
  29. win->modal_widget = RT_NULL;
  30. /* set window hide */
  31. RTGUI_WIDGET_HIDE(RTGUI_WIDGET(win));
  32. /* set window style */
  33. win->style = RTGUI_WIN_STYLE_DEFAULT;
  34. rtgui_widget_set_event_handler(RTGUI_WIDGET(win), rtgui_win_event_handler);
  35. /* init user data */
  36. win->user_data = 0;
  37. }
  38. static void _rtgui_win_destructor(rtgui_win_t* win)
  39. {
  40. struct rtgui_event_win_destroy edestroy;
  41. if (RTGUI_TOPLEVEL(win)->server != RT_NULL)
  42. {
  43. /* destroy in server */
  44. RTGUI_EVENT_WIN_DESTROY_INIT(&edestroy);
  45. edestroy.wid = win;
  46. if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&edestroy),
  47. sizeof(struct rtgui_event_win_destroy)) != RT_EOK)
  48. {
  49. /* destroy in server failed */
  50. return;
  51. }
  52. }
  53. /* release field */
  54. rt_free(win->title);
  55. }
  56. static rt_bool_t _rtgui_win_create_in_server(rtgui_win_t* win)
  57. {
  58. if (RTGUI_TOPLEVEL(win)->server == RT_NULL)
  59. {
  60. rt_thread_t server;
  61. struct rtgui_event_win_create ecreate;
  62. RTGUI_EVENT_WIN_CREATE_INIT(&ecreate);
  63. /* get server thread id */
  64. server = rtgui_thread_get_server();
  65. if (server == RT_NULL)
  66. {
  67. rt_kprintf("RTGUI server is not running...\n");
  68. return RT_FALSE;
  69. }
  70. /* send win create event to server */
  71. ecreate.wid = win;
  72. ecreate.parent.user = win->style;
  73. #ifndef RTGUI_USING_SMALL_SIZE
  74. ecreate.extent = RTGUI_WIDGET(win)->extent;
  75. rt_strncpy((char*)ecreate.title, (char*)win->title, RTGUI_NAME_MAX);
  76. #endif
  77. if (rtgui_thread_send_sync(server, RTGUI_EVENT(&ecreate),
  78. sizeof(struct rtgui_event_win_create)) != RT_EOK)
  79. {
  80. rt_kprintf("create win: %s failed\n", win->title);
  81. return RT_FALSE;
  82. }
  83. /* set server */
  84. RTGUI_TOPLEVEL(win)->server = server;
  85. }
  86. return RT_TRUE;
  87. }
  88. rtgui_type_t *rtgui_win_type_get(void)
  89. {
  90. static rtgui_type_t *win_type = RT_NULL;
  91. if (!win_type)
  92. {
  93. win_type = rtgui_type_create("win", RTGUI_TOPLEVEL_TYPE,
  94. sizeof(rtgui_win_t),
  95. RTGUI_CONSTRUCTOR(_rtgui_win_constructor),
  96. RTGUI_DESTRUCTOR(_rtgui_win_destructor));
  97. }
  98. return win_type;
  99. }
  100. rtgui_win_t* rtgui_win_create(rtgui_toplevel_t* parent_toplevel, const char* title, rtgui_rect_t *rect, rt_uint8_t style)
  101. {
  102. struct rtgui_win* win;
  103. /* allocate win memory */
  104. win = (struct rtgui_win*) rtgui_widget_create (RTGUI_WIN_TYPE);
  105. if (win != RT_NULL)
  106. {
  107. /* set parent toplevel */
  108. win->parent_toplevel = parent_toplevel;
  109. /* set title, rect and style */
  110. if (title != RT_NULL) win->title = rt_strdup(title);
  111. else win->title = RT_NULL;
  112. rtgui_widget_set_rect(RTGUI_WIDGET(win), rect);
  113. win->style = style;
  114. if (_rtgui_win_create_in_server(win) == RT_FALSE)
  115. {
  116. rtgui_widget_destroy(RTGUI_WIDGET(win));
  117. return RT_NULL;
  118. }
  119. }
  120. return win;
  121. }
  122. void rtgui_win_destroy(struct rtgui_win* win)
  123. {
  124. if (win->style & RTGUI_WIN_STYLE_MODAL)
  125. {
  126. /* end modal */
  127. rtgui_win_end_modal(win, RTGUI_MODAL_CANCEL);
  128. }
  129. else
  130. {
  131. rtgui_widget_destroy(RTGUI_WIDGET(win));
  132. }
  133. }
  134. void rtgui_win_close(struct rtgui_win* win)
  135. {
  136. win->style |= RTGUI_WIN_STYLE_CLOSED;
  137. }
  138. rtgui_modal_code_t rtgui_win_show(struct rtgui_win* win, rt_bool_t is_modal)
  139. {
  140. rtgui_modal_code_t result;
  141. RT_ASSERT(win != RT_NULL);
  142. result = RTGUI_MODAL_CANCEL;
  143. /* if it does not register into server, create it in server */
  144. if (RTGUI_TOPLEVEL(win)->server == RT_NULL)
  145. {
  146. if (_rtgui_win_create_in_server(win) == RT_FALSE)
  147. return result;
  148. }
  149. if (RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(win)))
  150. {
  151. /* send show message to server */
  152. struct rtgui_event_win_show eshow;
  153. RTGUI_EVENT_WIN_SHOW_INIT(&eshow);
  154. eshow.wid = win;
  155. if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&eshow),
  156. sizeof(struct rtgui_event_win_show)) != RT_EOK)
  157. {
  158. /* hide window failed */
  159. return result;
  160. }
  161. /* set window unhidden */
  162. RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(win));
  163. }
  164. else rtgui_widget_update(RTGUI_WIDGET(win));
  165. if (is_modal == RT_TRUE)
  166. {
  167. if (win->parent_toplevel != RT_NULL)
  168. {
  169. rtgui_widget_t *parent_widget;
  170. /* set style */
  171. win->style |= RTGUI_WIN_STYLE_MODAL;
  172. /* get root toplevel */
  173. parent_widget = RTGUI_WIDGET(win->parent_toplevel);
  174. if (RTGUI_IS_WORKBENCH(parent_widget))
  175. {
  176. rtgui_workbench_t* workbench;
  177. workbench = RTGUI_WORKBENCH(win->parent_toplevel);
  178. workbench->flag |= RTGUI_WORKBENCH_FLAG_MODAL_MODE;
  179. workbench->modal_widget = RTGUI_WIDGET(win);
  180. rtgui_workbench_event_loop(workbench);
  181. result = workbench->modal_code;
  182. workbench->flag &= ~RTGUI_WORKBENCH_FLAG_MODAL_MODE;
  183. workbench->modal_widget = RT_NULL;
  184. }
  185. else if (RTGUI_IS_WIN(parent_widget))
  186. {
  187. rtgui_win_t* parent_win;
  188. parent_win = RTGUI_WIN(win->parent_toplevel);
  189. parent_win->style |= RTGUI_WIN_STYLE_UNDER_MODAL;
  190. parent_win->modal_widget = RTGUI_WIDGET(win);
  191. rtgui_win_event_loop(parent_win);
  192. result = parent_win->modal_code;
  193. parent_win->style &= ~RTGUI_WIN_STYLE_UNDER_MODAL;
  194. parent_win->modal_widget = RT_NULL;
  195. }
  196. }
  197. else
  198. {
  199. /* which is a root window */
  200. win->style |= RTGUI_WIN_STYLE_MODAL;
  201. rtgui_win_event_loop(win);
  202. result = win->modal_code;
  203. win->style &= ~RTGUI_WIN_STYLE_MODAL;
  204. }
  205. }
  206. return result;
  207. }
  208. void rtgui_win_end_modal(struct rtgui_win* win, rtgui_modal_code_t modal_code)
  209. {
  210. if (win->parent_toplevel != RT_NULL)
  211. {
  212. if (RTGUI_IS_WORKBENCH(win->parent_toplevel))
  213. {
  214. rtgui_workbench_t* workbench;
  215. /* which is shown under workbench */
  216. workbench = RTGUI_WORKBENCH(win->parent_toplevel);
  217. workbench->modal_code = modal_code;
  218. workbench->flag &= ~RTGUI_WORKBENCH_FLAG_MODAL_MODE;
  219. }
  220. else if (RTGUI_IS_WIN(win->parent_toplevel))
  221. {
  222. rtgui_win_t* parent_win;
  223. /* which is shown under win */
  224. parent_win = RTGUI_WIN(win->parent_toplevel);
  225. parent_win->modal_code = modal_code;
  226. parent_win->style &= ~RTGUI_WIN_STYLE_UNDER_MODAL;
  227. }
  228. }
  229. else
  230. {
  231. /* which is a stand alone window */
  232. win->modal_code = modal_code;
  233. }
  234. /* remove modal mode */
  235. win->style &= ~RTGUI_WIN_STYLE_MODAL;
  236. }
  237. void rtgui_win_hiden(struct rtgui_win* win)
  238. {
  239. RT_ASSERT(win != RT_NULL);
  240. if (!RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(win)) &&
  241. RTGUI_TOPLEVEL(win)->server != RT_NULL)
  242. {
  243. /* send hidden message to server */
  244. struct rtgui_event_win_hide ehide;
  245. RTGUI_EVENT_WIN_HIDE_INIT(&ehide);
  246. ehide.wid = win;
  247. if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&ehide),
  248. sizeof(struct rtgui_event_win_hide)) != RT_EOK)
  249. {
  250. rt_kprintf("hide win: %s failed\n", win->title);
  251. return;
  252. }
  253. /* set window hide and deactivated */
  254. RTGUI_WIDGET_HIDE(RTGUI_WIDGET(win));
  255. win->style &= ~RTGUI_WIN_STYLE_ACTIVATE;
  256. }
  257. }
  258. rt_bool_t rtgui_win_is_activated(struct rtgui_win* win)
  259. {
  260. RT_ASSERT(win != RT_NULL);
  261. return win->style & RTGUI_WIN_STYLE_ACTIVATE;
  262. }
  263. void rtgui_win_move(struct rtgui_win* win, int x, int y)
  264. {
  265. struct rtgui_event_win_move emove;
  266. RTGUI_EVENT_WIN_MOVE_INIT(&emove);
  267. if (win == RT_NULL) return;
  268. if (RTGUI_TOPLEVEL(win)->server != RT_NULL)
  269. {
  270. /* set win hide firstly */
  271. RTGUI_WIDGET_HIDE(RTGUI_WIDGET(win));
  272. emove.wid = win;
  273. emove.x = x;
  274. emove.y = y;
  275. if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&emove),
  276. sizeof(struct rtgui_event_win_move)) != RT_EOK)
  277. {
  278. return;
  279. }
  280. }
  281. /* move window to logic position */
  282. rtgui_widget_move_to_logic(RTGUI_WIDGET(win),
  283. x - RTGUI_WIDGET(win)->extent.x1,
  284. y - RTGUI_WIDGET(win)->extent.y1);
  285. /* set window visible */
  286. RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(win));
  287. return;
  288. }
  289. static rt_bool_t rtgui_win_ondraw(struct rtgui_win* win)
  290. {
  291. struct rtgui_dc* dc;
  292. struct rtgui_rect rect;
  293. struct rtgui_event_paint event;
  294. /* begin drawing */
  295. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(win));
  296. if (dc == RT_NULL) return RT_FALSE;
  297. /* get window rect */
  298. rtgui_widget_get_rect(RTGUI_WIDGET(win), &rect);
  299. /* fill area */
  300. rtgui_dc_fill_rect(dc, &rect);
  301. /* paint each widget */
  302. RTGUI_EVENT_PAINT_INIT(&event);
  303. event.wid = RT_NULL;
  304. rtgui_container_dispatch_event(RTGUI_CONTAINER(win), (rtgui_event_t*)&event);
  305. rtgui_dc_end_drawing(dc);
  306. return RT_FALSE;
  307. }
  308. rt_bool_t rtgui_win_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  309. {
  310. struct rtgui_win* win = (struct rtgui_win*)widget;
  311. RT_ASSERT((win != RT_NULL) && (event != RT_NULL));
  312. switch (event->type)
  313. {
  314. case RTGUI_EVENT_WIN_SHOW:
  315. rtgui_win_show(win, RT_FALSE);
  316. break;
  317. case RTGUI_EVENT_WIN_HIDE:
  318. rtgui_win_hiden(win);
  319. break;
  320. case RTGUI_EVENT_WIN_CLOSE:
  321. if (win->on_close != RT_NULL)
  322. {
  323. if (win->on_close(widget, event) == RT_FALSE) return RT_TRUE;
  324. }
  325. if (win->style & RTGUI_WIN_STYLE_MODAL)
  326. {
  327. rtgui_win_end_modal(win, RTGUI_MODAL_CANCEL);
  328. }
  329. else
  330. {
  331. /* destroy window */
  332. rtgui_win_destroy(win);
  333. }
  334. /* exit event loop */
  335. return RT_TRUE;
  336. case RTGUI_EVENT_WIN_MOVE:
  337. {
  338. struct rtgui_event_win_move* emove = (struct rtgui_event_win_move*)event;
  339. /* move window */
  340. rtgui_win_move(win, emove->x, emove->y);
  341. }
  342. break;
  343. case RTGUI_EVENT_WIN_ACTIVATE:
  344. if (RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(win)))
  345. {
  346. rt_kprintf("activate window, but window is hide!\n");
  347. }
  348. win->style |= RTGUI_WIN_STYLE_ACTIVATE;
  349. #ifndef RTGUI_USING_SMALL_SIZE
  350. if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
  351. else
  352. #endif
  353. rtgui_widget_update(RTGUI_WIDGET(win));
  354. if (win->on_activate != RT_NULL)
  355. {
  356. win->on_activate(widget, event);
  357. }
  358. break;
  359. case RTGUI_EVENT_WIN_DEACTIVATE:
  360. if (win->style & RTGUI_WIN_STYLE_MODAL)
  361. {
  362. /* do not deactivate a modal win, re-send win-show event */
  363. struct rtgui_event_win_show eshow;
  364. RTGUI_EVENT_WIN_SHOW_INIT(&eshow);
  365. eshow.wid = win;
  366. rtgui_thread_send(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&eshow),
  367. sizeof(struct rtgui_event_win_show));
  368. }
  369. else
  370. {
  371. win->style &= ~RTGUI_WIN_STYLE_ACTIVATE;
  372. #ifndef RTGUI_USING_SMALL_SIZE
  373. if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
  374. else
  375. #endif
  376. rtgui_win_ondraw(win);
  377. if (win->on_deactivate != RT_NULL)
  378. {
  379. win->on_deactivate(widget, event);
  380. }
  381. }
  382. break;
  383. case RTGUI_EVENT_PAINT:
  384. #ifndef RTGUI_USING_SMALL_SIZE
  385. if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
  386. else
  387. #endif
  388. rtgui_win_ondraw(win);
  389. break;
  390. case RTGUI_EVENT_MOUSE_BUTTON:
  391. if (win->style & RTGUI_WIN_STYLE_UNDER_MODAL)
  392. {
  393. if (win->modal_widget != RT_NULL)
  394. return win->modal_widget->event_handler(win->modal_widget, event);
  395. }
  396. else if (rtgui_container_dispatch_mouse_event(RTGUI_CONTAINER(win),
  397. (struct rtgui_event_mouse*)event) == RT_FALSE)
  398. {
  399. #ifndef RTGUI_USING_SMALL_SIZE
  400. if (widget->on_mouseclick != RT_NULL)
  401. {
  402. return widget->on_mouseclick(widget, event);
  403. }
  404. #endif
  405. }
  406. break;
  407. case RTGUI_EVENT_MOUSE_MOTION:
  408. #if 0
  409. if (rtgui_widget_dispatch_mouse_event(widget,
  410. (struct rtgui_event_mouse*)event) == RT_FALSE)
  411. {
  412. #ifndef RTGUI_USING_SMALL_SIZE
  413. /* handle event in current widget */
  414. if (widget->on_mousemotion != RT_NULL)
  415. {
  416. return widget->on_mousemotion(widget, event);
  417. }
  418. #endif
  419. }
  420. else return RT_TRUE;
  421. #endif
  422. break;
  423. case RTGUI_EVENT_KBD:
  424. if (win->style & RTGUI_WIN_STYLE_UNDER_MODAL)
  425. {
  426. if (win->modal_widget != RT_NULL)
  427. return win->modal_widget->event_handler(win->modal_widget, event);
  428. }
  429. else if (RTGUI_CONTAINER(win)->focused != widget)
  430. {
  431. RTGUI_CONTAINER(win)->focused->event_handler(RTGUI_CONTAINER(win)->focused, event);
  432. }
  433. break;
  434. default:
  435. /* call parent event handler */
  436. return rtgui_toplevel_event_handler(widget, event);
  437. }
  438. return RT_FALSE;
  439. }
  440. /* windows event loop */
  441. void rtgui_win_event_loop(rtgui_win_t* wnd)
  442. {
  443. /* the buffer uses to receive event */
  444. char event_buf[256];
  445. struct rtgui_event* event = (struct rtgui_event*)&event_buf[0];
  446. if (wnd->style & RTGUI_WIN_STYLE_UNDER_MODAL)
  447. {
  448. while (wnd->style & RTGUI_WIN_STYLE_UNDER_MODAL)
  449. {
  450. if (rtgui_thread_recv(event, sizeof(event_buf)) == RT_EOK)
  451. {
  452. /* perform event handler */
  453. RTGUI_WIDGET(wnd)->event_handler(RTGUI_WIDGET(wnd), event);
  454. }
  455. }
  456. }
  457. else
  458. {
  459. while (!(wnd->style & RTGUI_WIN_STYLE_CLOSED))
  460. {
  461. if (rtgui_thread_recv(event, sizeof(event_buf)) == RT_EOK)
  462. {
  463. /* perform event handler */
  464. RTGUI_WIDGET(wnd)->event_handler(RTGUI_WIDGET(wnd), event);
  465. }
  466. }
  467. }
  468. /* destroy window */
  469. rtgui_widget_destroy(RTGUI_WIDGET(wnd));
  470. }
  471. void rtgui_win_set_rect(rtgui_win_t* win, rtgui_rect_t* rect)
  472. {
  473. struct rtgui_event_win_resize event;
  474. if (win == RT_NULL || rect == RT_NULL) return;
  475. RTGUI_WIDGET(win)->extent = *rect;
  476. if (RTGUI_TOPLEVEL(win)->server != RT_NULL)
  477. {
  478. /* set window resize event to server */
  479. RTGUI_EVENT_WIN_RESIZE_INIT(&event);
  480. event.wid = win;
  481. event.rect = *rect;
  482. rtgui_thread_send(RTGUI_TOPLEVEL(win)->server, &(event.parent), sizeof(struct rtgui_event_win_resize));
  483. }
  484. }
  485. #ifndef RTGUI_USING_SMALL_SIZE
  486. void rtgui_win_set_box(rtgui_win_t* win, rtgui_box_t* box)
  487. {
  488. if (win == RT_NULL || box == RT_NULL) return;
  489. rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(box));
  490. rtgui_widget_set_rect(RTGUI_WIDGET(box), &(RTGUI_WIDGET(win)->extent));
  491. }
  492. #endif
  493. void rtgui_win_set_onactivate(rtgui_win_t* win, rtgui_event_handler_ptr handler)
  494. {
  495. if (win != RT_NULL)
  496. {
  497. win->on_activate = handler;
  498. }
  499. }
  500. void rtgui_win_set_ondeactivate(rtgui_win_t* win, rtgui_event_handler_ptr handler)
  501. {
  502. if (win != RT_NULL)
  503. {
  504. win->on_deactivate = handler;
  505. }
  506. }
  507. void rtgui_win_set_onclose(rtgui_win_t* win, rtgui_event_handler_ptr handler)
  508. {
  509. if (win != RT_NULL)
  510. {
  511. win->on_close = handler;
  512. }
  513. }
  514. void rtgui_win_set_title(rtgui_win_t* win, const char *title)
  515. {
  516. /* send title to server */
  517. if (RTGUI_TOPLEVEL(win)->server != RT_NULL)
  518. {
  519. }
  520. /* modify in local side */
  521. if (win->title != RT_NULL)
  522. {
  523. rtgui_free(win->title);
  524. win->title = RT_NULL;
  525. }
  526. if (title != RT_NULL)
  527. {
  528. win->title = rt_strdup(title);
  529. }
  530. }
  531. char* rtgui_win_get_title(rtgui_win_t* win)
  532. {
  533. RT_ASSERT(win != RT_NULL);
  534. return win->title;
  535. }