widget.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * File : widget.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/widgets/widget.h>
  15. #include <rtgui/widgets/window.h>
  16. #include <rtgui/widgets/view.h>
  17. static void _rtgui_widget_constructor(rtgui_widget_t *widget)
  18. {
  19. if (!widget) return;
  20. /* set default flag */
  21. widget->flag = RTGUI_WIDGET_FLAG_DEFAULT;
  22. /* init list */
  23. rtgui_list_init(&(widget->sibling));
  24. /* init gc */
  25. widget->gc.foreground = default_foreground;
  26. widget->gc.background = default_background;
  27. widget->gc.font = rtgui_font_default();
  28. widget->gc.textalign = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_TOP;
  29. widget->align = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_TOP;
  30. /* set parent and toplevel root */
  31. widget->parent = RT_NULL;
  32. widget->toplevel = RT_NULL;
  33. /* some common event handler */
  34. widget->on_draw = RT_NULL;
  35. widget->on_focus_in = RT_NULL;
  36. widget->on_focus_out = RT_NULL;
  37. widget->on_mouseclick = RT_NULL;
  38. widget->on_key = RT_NULL;
  39. widget->on_size = RT_NULL;
  40. widget->on_command = RT_NULL;
  41. /* set default event handler */
  42. widget->event_handler = rtgui_widget_event_handler;
  43. /* does not set widget extent and only set clip_sync to zero */
  44. rtgui_region_init(&(widget->clip));
  45. widget->clip_sync = 0;
  46. }
  47. /* Destroys the widget */
  48. static void _rtgui_widget_destructor(rtgui_widget_t *widget)
  49. {
  50. if (widget == RT_NULL) return;
  51. if (widget->parent != RT_NULL)
  52. {
  53. /* remove widget from parent's children list */
  54. rtgui_list_remove(&(RTGUI_CONTAINER(widget->parent)->children), &(widget->sibling));
  55. widget->parent = RT_NULL;
  56. }
  57. /* fini clip region */
  58. rtgui_region_fini(&(widget->clip));
  59. widget->clip_sync = 0;
  60. }
  61. rtgui_type_t *rtgui_widget_type_get(void)
  62. {
  63. static rtgui_type_t *widget_type = RT_NULL;
  64. if (!widget_type)
  65. {
  66. widget_type = rtgui_type_create("rtgui_widget", RTGUI_OBJECT_TYPE,
  67. sizeof(rtgui_widget_t), RTGUI_CONSTRUCTOR(_rtgui_widget_constructor),
  68. RTGUI_DESTRUCTOR(_rtgui_widget_destructor));
  69. }
  70. return widget_type;
  71. }
  72. rtgui_widget_t *rtgui_widget_create(rtgui_type_t *widget_type)
  73. {
  74. struct rtgui_widget* widget;
  75. widget = RTGUI_WIDGET(rtgui_object_create(widget_type));
  76. return widget;
  77. }
  78. void rtgui_widget_destroy(rtgui_widget_t* widget)
  79. {
  80. rtgui_object_destroy(RTGUI_OBJECT(widget));
  81. }
  82. void rtgui_widget_set_rect(rtgui_widget_t* widget, rtgui_rect_t* rect)
  83. {
  84. if (widget == RT_NULL || rect == RT_NULL) return;
  85. widget->extent = *rect;
  86. /* reset mini width and height */
  87. widget->mini_width = rtgui_rect_width(widget->extent);
  88. widget->mini_height = rtgui_rect_height(widget->extent);
  89. /* it's not empty, fini it */
  90. if (rtgui_region_not_empty(&(widget->clip)))
  91. {
  92. rtgui_region_fini(&(widget->clip));
  93. }
  94. /* reset clip info */
  95. rtgui_region_init_with_extents(&(widget->clip), rect);
  96. }
  97. void rtgui_widget_set_miniwidth(rtgui_widget_t* widget, int width)
  98. {
  99. RT_ASSERT(widget != RT_NULL);
  100. widget->mini_width = width;
  101. }
  102. void rtgui_widget_set_miniheight(rtgui_widget_t* widget, int height)
  103. {
  104. RT_ASSERT(widget != RT_NULL);
  105. widget->mini_height = height;
  106. }
  107. /*
  108. * This function moves widget and its children to a logic point
  109. */
  110. void rtgui_widget_move_to_logic(rtgui_widget_t* widget, int dx, int dy)
  111. {
  112. struct rtgui_list_node* node;
  113. rtgui_widget_t* child;
  114. if (widget == RT_NULL) return;
  115. rtgui_rect_moveto(&(widget->extent), dx, dy);
  116. /* move each child */
  117. if (RTGUI_IS_CONTAINER(widget))
  118. {
  119. rtgui_list_foreach(node, &(RTGUI_CONTAINER(widget)->children))
  120. {
  121. child = rtgui_list_entry(node, rtgui_widget_t, sibling);
  122. rtgui_widget_move_to_logic(child, dx, dy);
  123. }
  124. }
  125. }
  126. void rtgui_widget_set_event_handler(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  127. {
  128. RT_ASSERT(widget != RT_NULL);
  129. widget->event_handler = handler;
  130. }
  131. void rtgui_widget_get_rect(rtgui_widget_t* widget, rtgui_rect_t *rect)
  132. {
  133. RT_ASSERT(widget != RT_NULL);
  134. if (rect != RT_NULL)
  135. {
  136. rect->x1 = rect->y1 = 0;
  137. rect->x2 = widget->extent.x2 - widget->extent.x1;
  138. rect->y2 = widget->extent.y2 - widget->extent.y1;
  139. }
  140. }
  141. void rtgui_widget_set_ondraw(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  142. {
  143. RT_ASSERT(widget != RT_NULL);
  144. widget->on_draw = handler;
  145. }
  146. void rtgui_widget_set_onfocus(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  147. {
  148. RT_ASSERT(widget != RT_NULL);
  149. widget->on_focus_in = handler;
  150. }
  151. void rtgui_widget_set_onunfocus(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  152. {
  153. RT_ASSERT(widget != RT_NULL);
  154. widget->on_focus_out = handler;
  155. }
  156. void rtgui_widget_set_onmouseclick(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  157. {
  158. RT_ASSERT(widget != RT_NULL);
  159. widget->on_mouseclick = handler;
  160. }
  161. void rtgui_widget_set_onkey(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  162. {
  163. RT_ASSERT(widget != RT_NULL);
  164. widget->on_key = handler;
  165. }
  166. void rtgui_widget_set_onsize(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  167. {
  168. RT_ASSERT(widget != RT_NULL);
  169. widget->on_size = handler;
  170. }
  171. void rtgui_widget_set_oncommand(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  172. {
  173. RT_ASSERT(widget != RT_NULL);
  174. widget->on_command = handler;
  175. }
  176. /**
  177. * @brief Focuses the widget. The focused widget is the one which receives the keyboard events
  178. * @param widget a widget
  179. * @note The widget has to be attached to a toplevel widget, otherwise it will have no effect
  180. */
  181. void rtgui_widget_focus(rtgui_widget_t *widget)
  182. {
  183. rtgui_widget_t *focused;
  184. if (!widget || !widget->toplevel || !RTGUI_WIDGET_IS_FOCUSABLE(widget) || !RTGUI_WIDGET_IS_ENABLE(widget))
  185. return;
  186. focused = rtgui_toplevel_get_focus(RTGUI_TOPLEVEL(widget->toplevel));
  187. if ( focused != RT_NULL && widget == focused)
  188. return;
  189. if (focused != RT_NULL)
  190. {
  191. rtgui_widget_unfocus(focused);
  192. }
  193. rtgui_toplevel_set_focus(RTGUI_TOPLEVEL(widget->toplevel), widget);
  194. widget->flag |= RTGUI_WIDGET_FLAG_FOCUS;
  195. if (widget->on_focus_in)
  196. widget->on_focus_in(widget, RT_NULL);
  197. }
  198. /**
  199. * @brief Unfocused the widget
  200. * @param widget a widget
  201. */
  202. void rtgui_widget_unfocus(rtgui_widget_t *widget)
  203. {
  204. if (!widget || !widget->toplevel || !RTGUI_WIDGET_IS_FOCUS(widget))
  205. return;
  206. if (rtgui_toplevel_get_focus(RTGUI_TOPLEVEL(widget->toplevel)) == widget)
  207. {
  208. rtgui_toplevel_set_focus(RTGUI_TOPLEVEL(widget->toplevel), RT_NULL);
  209. }
  210. widget->flag &= ~RTGUI_WIDGET_FLAG_FOCUS;
  211. if (widget->on_focus_out)
  212. widget->on_focus_out(widget, RT_NULL);
  213. }
  214. void rtgui_widget_point_to_device(rtgui_widget_t* widget, rtgui_point_t* point)
  215. {
  216. RT_ASSERT(widget != RT_NULL);
  217. if (point != RT_NULL)
  218. {
  219. point->x += widget->extent.x1;
  220. point->y += widget->extent.y1;
  221. }
  222. }
  223. void rtgui_widget_rect_to_device(rtgui_widget_t* widget, rtgui_rect_t* rect)
  224. {
  225. RT_ASSERT(widget != RT_NULL);
  226. if (rect != RT_NULL)
  227. {
  228. rect->x1 += widget->extent.x1;
  229. rect->x2 += widget->extent.x1;
  230. rect->y1 += widget->extent.y1;
  231. rect->y2 += widget->extent.y1;
  232. }
  233. }
  234. void rtgui_widget_point_to_logic(rtgui_widget_t* widget, rtgui_point_t* point)
  235. {
  236. RT_ASSERT(widget != RT_NULL);
  237. if (point != RT_NULL)
  238. {
  239. point->x -= widget->extent.x1;
  240. point->y -= widget->extent.y1;
  241. }
  242. }
  243. void rtgui_widget_rect_to_logic(rtgui_widget_t* widget, rtgui_rect_t* rect)
  244. {
  245. RT_ASSERT(widget != RT_NULL);
  246. if (rect != RT_NULL)
  247. {
  248. rect->x1 -= widget->extent.x1;
  249. rect->x2 -= widget->extent.x1;
  250. rect->y1 -= widget->extent.y1;
  251. rect->y2 -= widget->extent.y1;
  252. }
  253. }
  254. rtgui_widget_t* rtgui_widget_get_toplevel(rtgui_widget_t* widget)
  255. {
  256. rtgui_widget_t* r;
  257. RT_ASSERT(widget != RT_NULL);
  258. if (widget->toplevel) return widget->toplevel;
  259. r = widget;
  260. /* get the toplevel widget */
  261. while (r->parent != RT_NULL) r = r->parent;
  262. /* set toplevel */
  263. widget->toplevel = r;
  264. return r;
  265. }
  266. rt_bool_t rtgui_widget_event_handler(rtgui_widget_t* widget, rtgui_event_t* event)
  267. {
  268. switch (event->type)
  269. {
  270. case RTGUI_EVENT_PAINT:
  271. if (widget->on_draw != RT_NULL) return widget->on_draw(widget, event);
  272. break;
  273. case RTGUI_EVENT_KBD:
  274. if (widget->on_key != RT_NULL) return widget->on_key(widget, event);
  275. break;
  276. case RTGUI_EVENT_MOUSE_BUTTON:
  277. if (widget->on_mouseclick != RT_NULL) return widget->on_mouseclick(widget, event);
  278. break;
  279. case RTGUI_EVENT_COMMAND:
  280. if (widget->on_command != RT_NULL) return widget->on_command(widget, event);
  281. break;
  282. case RTGUI_EVENT_RESIZE:
  283. if (widget->on_size != RT_NULL) return widget->on_size(widget, event);
  284. break;
  285. }
  286. return RT_FALSE;
  287. }
  288. /*
  289. * This function updates the clip info of widget
  290. */
  291. void rtgui_widget_update_clip(rtgui_widget_t* widget)
  292. {
  293. struct rtgui_list_node* node;
  294. rtgui_widget_t *parent;
  295. /* no widget or widget is hide, no update clip */
  296. if (widget == RT_NULL || RTGUI_WIDGET_IS_HIDE(widget)) return;
  297. parent = widget->parent;
  298. /* if there is no parent, do not update clip (please use toplevel widget API) */
  299. if (parent == RT_NULL) return;
  300. /* increase clip sync */
  301. widget->clip_sync ++;
  302. /* reset clip to extent */
  303. rtgui_region_reset(&(widget->clip), &(widget->extent));
  304. /* limit widget extent in parent extent */
  305. rtgui_region_intersect(&(widget->clip), &(widget->clip), &(parent->clip));
  306. /* get the no transparent parent */
  307. while (parent != RT_NULL && parent->flag & RTGUI_WIDGET_FLAG_TRANSPARENT)
  308. {
  309. parent = parent->parent;
  310. }
  311. if (parent != RT_NULL)
  312. {
  313. /* subtract widget clip in parent clip */
  314. if (!(widget->flag & RTGUI_WIDGET_FLAG_TRANSPARENT))
  315. {
  316. rtgui_region_subtract_rect(&(parent->clip), &(parent->clip),
  317. &(widget->extent));
  318. }
  319. }
  320. /*
  321. * note: since the layout widget introduction, the sibling widget will not
  322. * intersect.
  323. */
  324. /* if it's a container object, update the clip info of children */
  325. if (RTGUI_IS_CONTAINER(widget))
  326. {
  327. rtgui_widget_t* child;
  328. rtgui_list_foreach(node, &(RTGUI_CONTAINER(widget)->children))
  329. {
  330. child = rtgui_list_entry(node, rtgui_widget_t, sibling);
  331. rtgui_widget_update_clip(child);
  332. }
  333. }
  334. }
  335. void rtgui_widget_show(rtgui_widget_t* widget)
  336. {
  337. /* there is no parent or the parent is hide, no show at all */
  338. if (widget->parent == RT_NULL ||
  339. RTGUI_WIDGET_IS_HIDE(widget->parent)) return;
  340. /* update the clip info of widget */
  341. RTGUI_WIDGET_UNHIDE(widget);
  342. rtgui_widget_update_clip(widget);
  343. }
  344. void rtgui_widget_hide(rtgui_widget_t* widget)
  345. {
  346. /* hide this widget */
  347. RTGUI_WIDGET_HIDE(widget);
  348. /* update the clip info of widget parent */
  349. rtgui_widget_update_clip(widget->parent);
  350. }
  351. void rtgui_widget_update(rtgui_widget_t* widget)
  352. {
  353. struct rtgui_event_paint paint;
  354. RTGUI_EVENT_PAINT_INIT(&paint);
  355. paint.wid = RT_NULL;
  356. RT_ASSERT(widget != RT_NULL);
  357. if (widget->event_handler != RT_NULL)
  358. {
  359. widget->event_handler(widget, &paint.parent);
  360. }
  361. }
  362. rtgui_widget_t* rtgui_widget_get_next_sibling(rtgui_widget_t* widget)
  363. {
  364. rtgui_widget_t* sibling = RT_NULL;
  365. if (widget->sibling.next != RT_NULL)
  366. {
  367. sibling = rtgui_list_entry(widget->sibling.next, rtgui_widget_t, sibling);
  368. }
  369. return sibling;
  370. }
  371. rtgui_widget_t* rtgui_widget_get_prev_sibling(rtgui_widget_t* widget)
  372. {
  373. struct rtgui_list_node* node;
  374. rtgui_widget_t *sibling, *parent;
  375. node = RT_NULL; sibling = RT_NULL;
  376. parent = widget->parent;
  377. if (parent != RT_NULL)
  378. {
  379. rtgui_list_foreach(node, &(RTGUI_CONTAINER(parent)->children))
  380. {
  381. if (node->next == &(widget->sibling))
  382. break;
  383. }
  384. }
  385. if (node != RT_NULL)
  386. sibling = rtgui_list_entry(node, rtgui_widget_t, sibling);
  387. return sibling;
  388. }