window.c 14 KB

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