window.c 11 KB

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