window.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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. rtgui_modal_code_t rtgui_win_show(struct rtgui_win* win, rt_bool_t is_modal)
  135. {
  136. rtgui_modal_code_t result;
  137. RT_ASSERT(win != RT_NULL);
  138. result = RTGUI_MODAL_CANCEL;
  139. /* if it does not register into server, create it in server */
  140. if (RTGUI_TOPLEVEL(win)->server == RT_NULL)
  141. {
  142. if (_rtgui_win_create_in_server(win) == RT_FALSE)
  143. return result;
  144. }
  145. if (RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(win)))
  146. {
  147. /* send show message to server */
  148. struct rtgui_event_win_show eshow;
  149. RTGUI_EVENT_WIN_SHOW_INIT(&eshow);
  150. eshow.wid = win;
  151. if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&eshow),
  152. sizeof(struct rtgui_event_win_show)) != RT_EOK)
  153. {
  154. /* hide window failed */
  155. return result;
  156. }
  157. /* set window unhidden */
  158. RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(win));
  159. }
  160. else rtgui_widget_update(RTGUI_WIDGET(win));
  161. if (is_modal == RT_TRUE)
  162. {
  163. if (win->parent_toplevel != RT_NULL)
  164. {
  165. rtgui_widget_t *parent_widget;
  166. /* set style */
  167. win->style |= RTGUI_WIN_STYLE_MODAL;
  168. /* get root toplevel */
  169. parent_widget = RTGUI_WIDGET(win->parent_toplevel);
  170. if (RTGUI_IS_WORKBENCH(parent_widget))
  171. {
  172. rtgui_workbench_t* workbench;
  173. workbench = RTGUI_WORKBENCH(win->parent_toplevel);
  174. workbench->flag |= RTGUI_WORKBENCH_FLAG_MODAL_MODE;
  175. workbench->modal_widget = RTGUI_WIDGET(win);
  176. rtgui_workbench_event_loop(workbench);
  177. result = workbench->modal_code;
  178. workbench->flag &= ~RTGUI_WORKBENCH_FLAG_MODAL_MODE;
  179. workbench->modal_widget = RT_NULL;
  180. }
  181. else if (RTGUI_IS_WIN(parent_widget))
  182. {
  183. rtgui_win_t* parent_win;
  184. parent_win = RTGUI_WIN(win->parent_toplevel);
  185. parent_win->flag |= RTGUI_WORKBENCH_FLAG_MODAL_MODE;
  186. parent_win->modal_widget = RTGUI_WIDGET(win);
  187. rtgui_win_event_loop(parent_win);
  188. result = parent_win->modal_code;
  189. parent_win->flag &= ~RTGUI_WORKBENCH_FLAG_MODAL_MODE;
  190. parent_win->modal_widget = RT_NULL;
  191. }
  192. }
  193. else
  194. {
  195. /* which is a root window */
  196. win->style |= RTGUI_WIN_STYLE_MODAL;
  197. rtgui_win_event_loop(win);
  198. result = win->modal_code;
  199. win->style &= ~RTGUI_WIN_STYLE_MODAL;
  200. }
  201. }
  202. return result;
  203. }
  204. void rtgui_win_end_modal(struct rtgui_win* win, rtgui_modal_code_t modal_code)
  205. {
  206. if (win->parent_toplevel != RT_NULL)
  207. {
  208. if (RTGUI_IS_WORKBENCH(win->parent_toplevel))
  209. {
  210. rtgui_workbench_t* workbench;
  211. /* which is shown under workbench */
  212. workbench = RTGUI_WORKBENCH(win->parent_toplevel);
  213. workbench->modal_code = modal_code;
  214. workbench->flag &= ~RTGUI_WORKBENCH_FLAG_MODAL_MODE;
  215. }
  216. else if (RTGUI_IS_WIN(win->parent_toplevel))
  217. {
  218. rtgui_win_t* parent_win;
  219. /* which is shown under win */
  220. parent_win = RTGUI_WIN(win->parent_toplevel);
  221. parent_win->modal_code = modal_code;
  222. parent_win->flag &= ~RTGUI_WORKBENCH_FLAG_MODAL_MODE;
  223. }
  224. }
  225. else
  226. {
  227. /* which is a stand alone window */
  228. win->modal_code = modal_code;
  229. }
  230. /* remove modal mode */
  231. win->style &= ~RTGUI_WIN_STYLE_MODAL;
  232. }
  233. void rtgui_win_hiden(struct rtgui_win* win)
  234. {
  235. RT_ASSERT(win != RT_NULL);
  236. if (!RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(win)) &&
  237. RTGUI_TOPLEVEL(win)->server != RT_NULL)
  238. {
  239. /* send hidden message to server */
  240. struct rtgui_event_win_hide ehide;
  241. RTGUI_EVENT_WIN_HIDE_INIT(&ehide);
  242. ehide.wid = win;
  243. if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&ehide),
  244. sizeof(struct rtgui_event_win_hide)) != RT_EOK)
  245. {
  246. rt_kprintf("hide win: %s failed\n", win->title);
  247. return;
  248. }
  249. /* set window hide and deactivated */
  250. RTGUI_WIDGET_HIDE(RTGUI_WIDGET(win));
  251. win->style &= ~RTGUI_WIN_STYLE_ACTIVATE;
  252. }
  253. }
  254. rt_bool_t rtgui_win_is_activated(struct rtgui_win* win)
  255. {
  256. RT_ASSERT(win != RT_NULL);
  257. return win->style & RTGUI_WIN_STYLE_ACTIVATE;
  258. }
  259. void rtgui_win_move(struct rtgui_win* win, int x, int y)
  260. {
  261. struct rtgui_event_win_move emove;
  262. RTGUI_EVENT_WIN_MOVE_INIT(&emove);
  263. if (win == RT_NULL) return;
  264. if (RTGUI_TOPLEVEL(win)->server != RT_NULL)
  265. {
  266. /* set win hide firstly */
  267. RTGUI_WIDGET_HIDE(RTGUI_WIDGET(win));
  268. emove.wid = win;
  269. emove.x = x;
  270. emove.y = y;
  271. if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&emove),
  272. sizeof(struct rtgui_event_win_move)) != RT_EOK)
  273. {
  274. return;
  275. }
  276. }
  277. /* move window to logic position */
  278. rtgui_widget_move_to_logic(RTGUI_WIDGET(win),
  279. x - RTGUI_WIDGET(win)->extent.x1,
  280. y - RTGUI_WIDGET(win)->extent.y1);
  281. /* set window visible */
  282. RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(win));
  283. return;
  284. }
  285. static rt_bool_t rtgui_win_ondraw(struct rtgui_win* win)
  286. {
  287. struct rtgui_dc* dc;
  288. struct rtgui_rect rect;
  289. struct rtgui_event_paint event;
  290. /* begin drawing */
  291. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(win));
  292. if (dc == RT_NULL) return RT_FALSE;
  293. /* get window rect */
  294. rtgui_widget_get_rect(RTGUI_WIDGET(win), &rect);
  295. /* fill area */
  296. rtgui_dc_fill_rect(dc, &rect);
  297. /* paint each widget */
  298. RTGUI_EVENT_PAINT_INIT(&event);
  299. event.wid = RT_NULL;
  300. rtgui_container_dispatch_event(RTGUI_CONTAINER(win), (rtgui_event_t*)&event);
  301. rtgui_dc_end_drawing(dc);
  302. return RT_FALSE;
  303. }
  304. rt_bool_t rtgui_win_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  305. {
  306. struct rtgui_win* win = (struct rtgui_win*)widget;
  307. RT_ASSERT((win != RT_NULL) && (event != RT_NULL));
  308. switch (event->type)
  309. {
  310. case RTGUI_EVENT_WIN_SHOW:
  311. rtgui_win_show(win, RT_FALSE);
  312. break;
  313. case RTGUI_EVENT_WIN_HIDE:
  314. rtgui_win_hiden(win);
  315. break;
  316. case RTGUI_EVENT_WIN_CLOSE:
  317. if (win->on_close != RT_NULL)
  318. {
  319. if (win->on_close(widget, event) == RT_FALSE) return RT_TRUE;
  320. }
  321. if (win->style & RTGUI_WIN_STYLE_MODAL)
  322. {
  323. rtgui_win_end_modal(win, RTGUI_MODAL_CANCEL);
  324. }
  325. else
  326. {
  327. /* destroy window */
  328. rtgui_win_destroy(win);
  329. }
  330. /* exit event loop */
  331. return RT_TRUE;
  332. case RTGUI_EVENT_WIN_MOVE:
  333. {
  334. struct rtgui_event_win_move* emove = (struct rtgui_event_win_move*)event;
  335. /* move window */
  336. rtgui_win_move(win, emove->x, emove->y);
  337. }
  338. break;
  339. case RTGUI_EVENT_WIN_ACTIVATE:
  340. if (RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(win)))
  341. {
  342. rt_kprintf("activate window, but window is hide!\n");
  343. }
  344. win->style |= RTGUI_WIN_STYLE_ACTIVATE;
  345. #ifndef RTGUI_USING_SMALL_SIZE
  346. if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
  347. else
  348. #endif
  349. rtgui_win_ondraw(win);
  350. if (win->on_activate != RT_NULL)
  351. {
  352. win->on_activate(widget, event);
  353. }
  354. break;
  355. case RTGUI_EVENT_WIN_DEACTIVATE:
  356. if (win->style & RTGUI_WIN_STYLE_MODAL)
  357. {
  358. /* do not deactivate a modal win */
  359. struct rtgui_event_win_show eshow;
  360. RTGUI_EVENT_WIN_SHOW_INIT(&eshow);
  361. eshow.wid = win;
  362. rtgui_thread_send(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&eshow),
  363. sizeof(struct rtgui_event_win_show));
  364. }
  365. else
  366. {
  367. win->style &= ~RTGUI_WIN_STYLE_ACTIVATE;
  368. #ifndef RTGUI_USING_SMALL_SIZE
  369. if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
  370. else
  371. #endif
  372. rtgui_win_ondraw(win);
  373. if (win->on_deactivate != RT_NULL)
  374. {
  375. win->on_deactivate(widget, event);
  376. }
  377. }
  378. break;
  379. case RTGUI_EVENT_PAINT:
  380. #ifndef RTGUI_USING_SMALL_SIZE
  381. if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
  382. else
  383. #endif
  384. rtgui_win_ondraw(win);
  385. break;
  386. case RTGUI_EVENT_MOUSE_BUTTON:
  387. if (win->flag & RTGUI_WORKBENCH_FLAG_MODAL_MODE)
  388. {
  389. if (win->modal_widget != RT_NULL)
  390. return win->modal_widget->event_handler(win->modal_widget, event);
  391. }
  392. else if (rtgui_container_dispatch_mouse_event(RTGUI_CONTAINER(win),
  393. (struct rtgui_event_mouse*)event) == RT_FALSE)
  394. {
  395. #ifndef RTGUI_USING_SMALL_SIZE
  396. if (widget->on_mouseclick != RT_NULL)
  397. {
  398. return widget->on_mouseclick(widget, event);
  399. }
  400. #endif
  401. }
  402. break;
  403. case RTGUI_EVENT_MOUSE_MOTION:
  404. #if 0
  405. if (rtgui_widget_dispatch_mouse_event(widget,
  406. (struct rtgui_event_mouse*)event) == RT_FALSE)
  407. {
  408. #ifndef RTGUI_USING_SMALL_SIZE
  409. /* handle event in current widget */
  410. if (widget->on_mousemotion != RT_NULL)
  411. {
  412. return widget->on_mousemotion(widget, event);
  413. }
  414. #endif
  415. }
  416. else return RT_TRUE;
  417. #endif
  418. break;
  419. case RTGUI_EVENT_KBD:
  420. if (win->flag & RTGUI_WORKBENCH_FLAG_MODAL_MODE)
  421. {
  422. if (win->modal_widget != RT_NULL)
  423. return win->modal_widget->event_handler(win->modal_widget, event);
  424. }
  425. else if (RTGUI_CONTAINER(win)->focused != widget)
  426. {
  427. RTGUI_CONTAINER(win)->focused->event_handler(RTGUI_CONTAINER(win)->focused, event);
  428. }
  429. break;
  430. default:
  431. /* call parent event handler */
  432. return rtgui_toplevel_event_handler(widget, event);
  433. }
  434. return RT_FALSE;
  435. }
  436. /* windows event loop */
  437. void rtgui_win_event_loop(rtgui_win_t* wnd)
  438. {
  439. /* the buffer uses to receive event */
  440. char event_buf[256];
  441. struct rtgui_event* event = (struct rtgui_event*)&event_buf[0];
  442. if (wnd->style & RTGUI_WIN_STYLE_MODAL)
  443. {
  444. while (wnd->style & RTGUI_WIN_STYLE_MODAL)
  445. {
  446. if (rtgui_thread_recv(event, sizeof(event_buf)) == RT_EOK)
  447. {
  448. /* perform event handler */
  449. RTGUI_WIDGET(wnd)->event_handler(RTGUI_WIDGET(wnd), event);
  450. }
  451. }
  452. }
  453. else
  454. {
  455. while (!(wnd->style & RTGUI_WIN_STYLE_CLOSED))
  456. {
  457. if (rtgui_thread_recv(event, sizeof(event_buf)) == RT_EOK)
  458. {
  459. /* perform event handler */
  460. RTGUI_WIDGET(wnd)->event_handler(RTGUI_WIDGET(wnd), event);
  461. }
  462. }
  463. }
  464. /* destroy window */
  465. rtgui_widget_destroy(RTGUI_WIDGET(wnd));
  466. }
  467. void rtgui_win_set_rect(rtgui_win_t* win, rtgui_rect_t* rect)
  468. {
  469. struct rtgui_event_win_resize event;
  470. if (win == RT_NULL || rect == RT_NULL) return;
  471. RTGUI_WIDGET(win)->extent = *rect;
  472. if (RTGUI_TOPLEVEL(win)->server != RT_NULL)
  473. {
  474. /* set window resize event to server */
  475. RTGUI_EVENT_WIN_RESIZE_INIT(&event);
  476. event.wid = win;
  477. event.rect = *rect;
  478. rtgui_thread_send(RTGUI_TOPLEVEL(win)->server, &(event.parent), sizeof(struct rtgui_event_win_resize));
  479. }
  480. }
  481. #ifndef RTGUI_USING_SMALL_SIZE
  482. void rtgui_win_set_box(rtgui_win_t* win, rtgui_box_t* box)
  483. {
  484. if (win == RT_NULL || box == RT_NULL) return;
  485. rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(box));
  486. rtgui_widget_set_rect(RTGUI_WIDGET(box), &(RTGUI_WIDGET(win)->extent));
  487. }
  488. #endif
  489. void rtgui_win_set_onactivate(rtgui_win_t* win, rtgui_event_handler_ptr handler)
  490. {
  491. if (win != RT_NULL)
  492. {
  493. win->on_activate = handler;
  494. }
  495. }
  496. void rtgui_win_set_ondeactivate(rtgui_win_t* win, rtgui_event_handler_ptr handler)
  497. {
  498. if (win != RT_NULL)
  499. {
  500. win->on_deactivate = handler;
  501. }
  502. }
  503. void rtgui_win_set_onclose(rtgui_win_t* win, rtgui_event_handler_ptr handler)
  504. {
  505. if (win != RT_NULL)
  506. {
  507. win->on_close = handler;
  508. }
  509. }
  510. void rtgui_win_set_title(rtgui_win_t* win, const char *title)
  511. {
  512. /* send title to server */
  513. if (RTGUI_TOPLEVEL(win)->server != RT_NULL)
  514. {
  515. }
  516. /* modify in local side */
  517. if (win->title != RT_NULL)
  518. {
  519. rtgui_free(win->title);
  520. win->title = RT_NULL;
  521. }
  522. if (title != RT_NULL)
  523. {
  524. win->title = rt_strdup(title);
  525. }
  526. }
  527. char* rtgui_win_get_title(rtgui_win_t* win)
  528. {
  529. RT_ASSERT(win != RT_NULL);
  530. return win->title;
  531. }