window.c 13 KB

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