window.c 13 KB

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