window.c 11 KB

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