window.c 13 KB

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