window.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. static void _rtgui_win_constructor(rtgui_win_t *win)
  21. {
  22. /* init window attribute */
  23. win->on_activate = RT_NULL;
  24. win->on_deactivate = RT_NULL;
  25. win->on_close = RT_NULL;
  26. win->title = RT_NULL;
  27. /* set window hide */
  28. RTGUI_WIDGET_HIDE(RTGUI_WIDGET(win));
  29. /* set window style */
  30. win->style = RTGUI_WIN_STYLE_DEFAULT;
  31. rtgui_widget_set_event_handler(RTGUI_WIDGET(win), rtgui_win_event_handler);
  32. /* init user data */
  33. win->user_data = 0;
  34. }
  35. static void _rtgui_win_destructor(rtgui_win_t* win)
  36. {
  37. struct rtgui_event_win_destroy edestroy;
  38. if (RTGUI_TOPLEVEL(win)->server != RT_NULL)
  39. {
  40. /* destroy in server */
  41. RTGUI_EVENT_WIN_DESTROY_INIT(&edestroy);
  42. edestroy.wid = win;
  43. if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&edestroy),
  44. sizeof(struct rtgui_event_win_destroy)) != RT_EOK)
  45. {
  46. /* destroy in server failed */
  47. return;
  48. }
  49. }
  50. /* release field */
  51. rt_free(win->title);
  52. }
  53. static rt_bool_t _rtgui_win_create_in_server(rtgui_win_t* win)
  54. {
  55. if (RTGUI_TOPLEVEL(win)->server == RT_NULL)
  56. {
  57. rt_thread_t server;
  58. struct rtgui_event_win_create ecreate;
  59. RTGUI_EVENT_WIN_CREATE_INIT(&ecreate);
  60. /* get server thread id */
  61. server = rtgui_thread_get_server();
  62. if (server == RT_NULL)
  63. {
  64. rt_kprintf("RTGUI server is not running...\n");
  65. return RT_FALSE;
  66. }
  67. /* send win create event to server */
  68. ecreate.wid = win;
  69. ecreate.extent = RTGUI_WIDGET(win)->extent;
  70. ecreate.flag = win->style;
  71. ecreate.mask = 0;
  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. rtgui_thread_t *rtgui_tid;
  123. rtgui_tid = (rtgui_thread_t*) rt_thread_self()->user_data;
  124. rtgui_tid->is_quit = RT_TRUE;
  125. }
  126. rtgui_widget_destroy(RTGUI_WIDGET(win));
  127. }
  128. void rtgui_win_show(struct rtgui_win* win)
  129. {
  130. RT_ASSERT(win != RT_NULL);
  131. /* if it does not register into server, create it in server */
  132. if (RTGUI_TOPLEVEL(win)->server == RT_NULL)
  133. {
  134. if (_rtgui_win_create_in_server(win) == RT_FALSE)
  135. return;
  136. }
  137. if (RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(win)))
  138. {
  139. /* send show message to server */
  140. struct rtgui_event_win_show eshow;
  141. RTGUI_EVENT_WIN_SHOW_INIT(&eshow);
  142. eshow.wid = win;
  143. if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&eshow),
  144. sizeof(struct rtgui_event_win_show)) != RT_EOK)
  145. {
  146. /* hide window failed */
  147. return;
  148. }
  149. /* set window unhidden */
  150. RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(win));
  151. }
  152. else rtgui_widget_update(RTGUI_WIDGET(win));
  153. }
  154. void rtgui_win_hiden(struct rtgui_win* win)
  155. {
  156. RT_ASSERT(win != RT_NULL);
  157. if (!RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(win)) &&
  158. RTGUI_TOPLEVEL(win)->server != RT_NULL)
  159. {
  160. /* send hidden message to server */
  161. struct rtgui_event_win_hide ehide;
  162. RTGUI_EVENT_WIN_HIDE_INIT(&ehide);
  163. ehide.wid = win;
  164. if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&ehide),
  165. sizeof(struct rtgui_event_win_hide)) != RT_EOK)
  166. {
  167. rt_kprintf("hide win: %s failed\n", win->title);
  168. return;
  169. }
  170. /* set window hide and deactivated */
  171. RTGUI_WIDGET_HIDE(RTGUI_WIDGET(win));
  172. win->style &= ~RTGUI_WIN_STYLE_ACTIVATE;
  173. }
  174. }
  175. rt_bool_t rtgui_win_is_activated(struct rtgui_win* win)
  176. {
  177. RT_ASSERT(win != RT_NULL);
  178. return win->style & RTGUI_WIN_STYLE_ACTIVATE;
  179. }
  180. void rtgui_win_move(struct rtgui_win* win, int x, int y)
  181. {
  182. struct rtgui_event_win_move emove;
  183. RTGUI_EVENT_WIN_MOVE_INIT(&emove);
  184. if (win == RT_NULL) return;
  185. if (RTGUI_TOPLEVEL(win)->server != RT_NULL)
  186. {
  187. /* set win hide firstly */
  188. RTGUI_WIDGET_HIDE(RTGUI_WIDGET(win));
  189. emove.wid = win;
  190. emove.x = x;
  191. emove.y = y;
  192. if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&emove),
  193. sizeof(struct rtgui_event_win_move)) != RT_EOK)
  194. {
  195. return;
  196. }
  197. }
  198. /* move window to logic position */
  199. rtgui_widget_move_to_logic(RTGUI_WIDGET(win),
  200. x - RTGUI_WIDGET(win)->extent.x1,
  201. y - RTGUI_WIDGET(win)->extent.y1);
  202. /* set window visible */
  203. RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(win));
  204. /* update window */
  205. // rtgui_widget_update(RTGUI_WIDGET(win));
  206. return;
  207. }
  208. static rt_bool_t rtgui_win_ondraw(struct rtgui_win* win)
  209. {
  210. struct rtgui_dc* dc;
  211. struct rtgui_rect rect;
  212. struct rtgui_event_paint event;
  213. /* begin drawing */
  214. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(win));
  215. if (dc == RT_NULL) return RT_FALSE;
  216. /* get window rect */
  217. rtgui_widget_get_rect(RTGUI_WIDGET(win), &rect);
  218. /* fill area */
  219. rtgui_dc_fill_rect(dc, &rect);
  220. /* paint each widget */
  221. RTGUI_EVENT_PAINT_INIT(&event);
  222. event.wid = RT_NULL;
  223. rtgui_container_dispatch_event(RTGUI_CONTAINER(win), (rtgui_event_t*)&event);
  224. rtgui_dc_end_drawing(dc);
  225. return RT_FALSE;
  226. }
  227. rt_bool_t rtgui_win_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  228. {
  229. struct rtgui_win* win = (struct rtgui_win*)widget;
  230. RT_ASSERT((win != RT_NULL) && (event != RT_NULL));
  231. switch (event->type)
  232. {
  233. case RTGUI_EVENT_WIN_SHOW:
  234. rtgui_win_show(win);
  235. break;
  236. case RTGUI_EVENT_WIN_HIDE:
  237. rtgui_win_hiden(win);
  238. break;
  239. case RTGUI_EVENT_WIN_CLOSE:
  240. if (win->on_close != RT_NULL)
  241. {
  242. if (win->on_close(widget, event) == RT_FALSE) return RT_TRUE;
  243. }
  244. /* destroy window */
  245. rtgui_win_destroy(win);
  246. /* exit event loop */
  247. return RT_TRUE;
  248. case RTGUI_EVENT_WIN_MOVE:
  249. {
  250. struct rtgui_event_win_move* emove = (struct rtgui_event_win_move*)event;
  251. /* move window */
  252. rtgui_win_move(win, emove->x, emove->y);
  253. }
  254. break;
  255. case RTGUI_EVENT_WIN_ACTIVATE:
  256. if (RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(win)))
  257. {
  258. rt_kprintf("activate window, but window is hide!\n");
  259. }
  260. win->style |= RTGUI_WIN_STYLE_ACTIVATE;
  261. if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
  262. else rtgui_win_ondraw(win);
  263. if (win->on_activate != RT_NULL)
  264. {
  265. win->on_activate(widget, event);
  266. }
  267. break;
  268. case RTGUI_EVENT_WIN_DEACTIVATE:
  269. win->style &= ~RTGUI_WIN_STYLE_ACTIVATE;
  270. if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
  271. else rtgui_win_ondraw(win);
  272. if (win->on_deactivate != RT_NULL)
  273. {
  274. win->on_deactivate(widget, event);
  275. }
  276. break;
  277. case RTGUI_EVENT_PAINT:
  278. if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
  279. else rtgui_win_ondraw(win);
  280. break;
  281. case RTGUI_EVENT_MOUSE_BUTTON:
  282. if (rtgui_container_dispatch_mouse_event(RTGUI_CONTAINER(win), (struct rtgui_event_mouse*)event) == RT_FALSE)
  283. {
  284. if (widget->on_mouseclick != RT_NULL)
  285. {
  286. return widget->on_mouseclick(widget, event);
  287. }
  288. }
  289. break;
  290. case RTGUI_EVENT_MOUSE_MOTION:
  291. #if 0
  292. if (rtgui_widget_dispatch_mouse_event(widget,
  293. (struct rtgui_event_mouse*)event) == RT_FALSE)
  294. {
  295. /* handle event in current widget */
  296. if (widget->on_mousemotion != RT_NULL)
  297. {
  298. return widget->on_mousemotion(widget, event);
  299. }
  300. }
  301. else return RT_TRUE;
  302. #endif
  303. break;
  304. default:
  305. /* call parent event handler */
  306. return rtgui_toplevel_event_handler(widget, event);
  307. }
  308. return RT_FALSE;
  309. }
  310. /* windows event loop */
  311. void rtgui_win_event_loop(rtgui_win_t* wnd)
  312. {
  313. rtgui_thread_t *rtgui_tid;
  314. /* the buffer uses to receive event */
  315. char event_buf[256];
  316. struct rtgui_event* event = (struct rtgui_event*)&event_buf[0];
  317. rtgui_tid = (rtgui_thread_t*) rt_thread_self()->user_data;
  318. while (rtgui_tid->is_quit == RT_FALSE)
  319. {
  320. if (rtgui_thread_recv(event, sizeof(event_buf)) == RT_EOK)
  321. {
  322. if (RTGUI_WIDGET(wnd)->event_handler(RTGUI_WIDGET(wnd), event) == RT_TRUE)
  323. rtgui_tid->is_quit = RT_TRUE;
  324. }
  325. }
  326. }
  327. void rtgui_win_set_rect(rtgui_win_t* win, rtgui_rect_t* rect)
  328. {
  329. struct rtgui_event_win_resize event;
  330. if (win == RT_NULL || rect == RT_NULL) return;
  331. RTGUI_WIDGET(win)->extent = *rect;
  332. if (RTGUI_TOPLEVEL(win)->server != RT_NULL)
  333. {
  334. /* set window resize event to server */
  335. RTGUI_EVENT_WIN_RESIZE_INIT(&event);
  336. event.wid = win;
  337. event.rect = *rect;
  338. rtgui_thread_send(RTGUI_TOPLEVEL(win)->server, &(event.parent), sizeof(struct rtgui_event_win_resize));
  339. }
  340. }
  341. void rtgui_win_set_box(rtgui_win_t* win, rtgui_box_t* box)
  342. {
  343. if (win == RT_NULL || box == RT_NULL) return;
  344. rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(box));
  345. rtgui_widget_set_rect(RTGUI_WIDGET(box), &(RTGUI_WIDGET(win)->extent));
  346. }
  347. void rtgui_win_set_onactivate(rtgui_win_t* win, rtgui_event_handler_ptr handler)
  348. {
  349. if (win != RT_NULL)
  350. {
  351. win->on_activate = handler;
  352. }
  353. }
  354. void rtgui_win_set_ondeactivate(rtgui_win_t* win, rtgui_event_handler_ptr handler)
  355. {
  356. if (win != RT_NULL)
  357. {
  358. win->on_deactivate = handler;
  359. }
  360. }
  361. void rtgui_win_set_onclose(rtgui_win_t* win, rtgui_event_handler_ptr handler)
  362. {
  363. if (win != RT_NULL)
  364. {
  365. win->on_close = handler;
  366. }
  367. }
  368. void rtgui_win_set_title(rtgui_win_t* win, const char *title)
  369. {
  370. /* send title to server */
  371. if (RTGUI_TOPLEVEL(win)->server != RT_NULL)
  372. {
  373. }
  374. /* modify in local side */
  375. if (win->title != RT_NULL)
  376. {
  377. rtgui_free(win->title);
  378. win->title = RT_NULL;
  379. }
  380. if (title != RT_NULL)
  381. {
  382. win->title = rt_strdup(title);
  383. }
  384. }
  385. char* rtgui_win_get_title(rtgui_win_t* win)
  386. {
  387. RT_ASSERT(win != RT_NULL);
  388. return win->title;
  389. }