widget.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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 widget which can receive 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. rtgui_container_t *parent;
  185. RT_ASSERT(widget != RT_NULL);
  186. if (!widget->parent || !RTGUI_WIDGET_IS_FOCUSABLE(widget) || !RTGUI_WIDGET_IS_ENABLE(widget))
  187. return;
  188. /* set widget as focused */
  189. widget->flag |= RTGUI_WIDGET_FLAG_FOCUS;
  190. /* get parent container */
  191. parent = RTGUI_CONTAINER(widget->parent);
  192. /* get old focused widget */
  193. focused = parent->focused;
  194. if (focused == widget) return ; /* it's the same focused widget */
  195. if (focused != RT_NULL)
  196. rtgui_widget_unfocus(focused);
  197. /* set widget as focused widget in parent link */
  198. parent->focused = widget;
  199. while (RTGUI_WIDGET(parent)->parent != RT_NULL)
  200. {
  201. parent = RTGUI_CONTAINER(RTGUI_WIDGET(parent)->parent);
  202. parent->focused = widget;
  203. /* if the parent is hide, break it */
  204. if (RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(parent))) break;
  205. }
  206. /* invoke on focus in call back */
  207. if (widget->on_focus_in)
  208. widget->on_focus_in(widget, RT_NULL);
  209. }
  210. /**
  211. * @brief Unfocused the widget
  212. * @param widget a widget
  213. */
  214. void rtgui_widget_unfocus(rtgui_widget_t *widget)
  215. {
  216. RT_ASSERT(widget != RT_NULL);
  217. widget->flag &= ~RTGUI_WIDGET_FLAG_FOCUS;
  218. if (!widget->toplevel || !RTGUI_WIDGET_IS_FOCUS(widget))
  219. return;
  220. if (widget->on_focus_out)
  221. widget->on_focus_out(widget, RT_NULL);
  222. }
  223. void rtgui_widget_point_to_device(rtgui_widget_t* widget, rtgui_point_t* point)
  224. {
  225. RT_ASSERT(widget != RT_NULL);
  226. if (point != RT_NULL)
  227. {
  228. point->x += widget->extent.x1;
  229. point->y += widget->extent.y1;
  230. }
  231. }
  232. void rtgui_widget_rect_to_device(rtgui_widget_t* widget, rtgui_rect_t* rect)
  233. {
  234. RT_ASSERT(widget != RT_NULL);
  235. if (rect != RT_NULL)
  236. {
  237. rect->x1 += widget->extent.x1;
  238. rect->x2 += widget->extent.x1;
  239. rect->y1 += widget->extent.y1;
  240. rect->y2 += widget->extent.y1;
  241. }
  242. }
  243. void rtgui_widget_point_to_logic(rtgui_widget_t* widget, rtgui_point_t* point)
  244. {
  245. RT_ASSERT(widget != RT_NULL);
  246. if (point != RT_NULL)
  247. {
  248. point->x -= widget->extent.x1;
  249. point->y -= widget->extent.y1;
  250. }
  251. }
  252. void rtgui_widget_rect_to_logic(rtgui_widget_t* widget, rtgui_rect_t* rect)
  253. {
  254. RT_ASSERT(widget != RT_NULL);
  255. if (rect != RT_NULL)
  256. {
  257. rect->x1 -= widget->extent.x1;
  258. rect->x2 -= widget->extent.x1;
  259. rect->y1 -= widget->extent.y1;
  260. rect->y2 -= widget->extent.y1;
  261. }
  262. }
  263. rtgui_widget_t* rtgui_widget_get_toplevel(rtgui_widget_t* widget)
  264. {
  265. rtgui_widget_t* r;
  266. RT_ASSERT(widget != RT_NULL);
  267. if (widget->toplevel) return widget->toplevel;
  268. r = widget;
  269. /* get the toplevel widget */
  270. while (r->parent != RT_NULL) r = r->parent;
  271. /* set toplevel */
  272. widget->toplevel = r;
  273. return r;
  274. }
  275. rt_bool_t rtgui_widget_event_handler(rtgui_widget_t* widget, rtgui_event_t* event)
  276. {
  277. switch (event->type)
  278. {
  279. case RTGUI_EVENT_PAINT:
  280. if (widget->on_draw != RT_NULL) return widget->on_draw(widget, event);
  281. break;
  282. case RTGUI_EVENT_KBD:
  283. if (widget->on_key != RT_NULL) return widget->on_key(widget, event);
  284. break;
  285. case RTGUI_EVENT_MOUSE_BUTTON:
  286. if (widget->on_mouseclick != RT_NULL) return widget->on_mouseclick(widget, event);
  287. break;
  288. case RTGUI_EVENT_COMMAND:
  289. if (widget->on_command != RT_NULL) return widget->on_command(widget, event);
  290. break;
  291. case RTGUI_EVENT_RESIZE:
  292. if (widget->on_size != RT_NULL) return widget->on_size(widget, event);
  293. break;
  294. }
  295. return RT_FALSE;
  296. }
  297. /*
  298. * This function updates the clip info of widget
  299. */
  300. void rtgui_widget_update_clip(rtgui_widget_t* widget)
  301. {
  302. struct rtgui_list_node* node;
  303. rtgui_widget_t *parent;
  304. /* no widget or widget is hide, no update clip */
  305. if (widget == RT_NULL || RTGUI_WIDGET_IS_HIDE(widget)) return;
  306. parent = widget->parent;
  307. /* if there is no parent, do not update clip (please use toplevel widget API) */
  308. if (parent == RT_NULL) return;
  309. /* increase clip sync */
  310. widget->clip_sync ++;
  311. /* reset clip to extent */
  312. rtgui_region_reset(&(widget->clip), &(widget->extent));
  313. /* limit widget extent in parent extent */
  314. rtgui_region_intersect(&(widget->clip), &(widget->clip), &(parent->clip));
  315. /* get the no transparent parent */
  316. while (parent != RT_NULL && parent->flag & RTGUI_WIDGET_FLAG_TRANSPARENT)
  317. {
  318. parent = parent->parent;
  319. }
  320. if (parent != RT_NULL)
  321. {
  322. /* subtract widget clip in parent clip */
  323. if (!(widget->flag & RTGUI_WIDGET_FLAG_TRANSPARENT))
  324. {
  325. rtgui_region_subtract_rect(&(parent->clip), &(parent->clip),
  326. &(widget->extent));
  327. }
  328. }
  329. /*
  330. * note: since the layout widget introduction, the sibling widget will not
  331. * intersect.
  332. */
  333. /* if it's a container object, update the clip info of children */
  334. if (RTGUI_IS_CONTAINER(widget))
  335. {
  336. rtgui_widget_t* child;
  337. rtgui_list_foreach(node, &(RTGUI_CONTAINER(widget)->children))
  338. {
  339. child = rtgui_list_entry(node, rtgui_widget_t, sibling);
  340. rtgui_widget_update_clip(child);
  341. }
  342. }
  343. }
  344. void rtgui_widget_show(rtgui_widget_t* widget)
  345. {
  346. /* there is no parent or the parent is hide, no show at all */
  347. if (widget->parent == RT_NULL ||
  348. RTGUI_WIDGET_IS_HIDE(widget->parent)) return;
  349. /* update the clip info of widget */
  350. RTGUI_WIDGET_UNHIDE(widget);
  351. rtgui_widget_update_clip(widget);
  352. }
  353. void rtgui_widget_hide(rtgui_widget_t* widget)
  354. {
  355. /* hide this widget */
  356. RTGUI_WIDGET_HIDE(widget);
  357. /* update the clip info of widget parent */
  358. rtgui_widget_update_clip(widget->parent);
  359. }
  360. void rtgui_widget_update(rtgui_widget_t* widget)
  361. {
  362. struct rtgui_event_paint paint;
  363. RTGUI_EVENT_PAINT_INIT(&paint);
  364. paint.wid = RT_NULL;
  365. RT_ASSERT(widget != RT_NULL);
  366. if (widget->event_handler != RT_NULL)
  367. {
  368. widget->event_handler(widget, &paint.parent);
  369. }
  370. }
  371. rtgui_widget_t* rtgui_widget_get_next_sibling(rtgui_widget_t* widget)
  372. {
  373. rtgui_widget_t* sibling = RT_NULL;
  374. if (widget->sibling.next != RT_NULL)
  375. {
  376. sibling = rtgui_list_entry(widget->sibling.next, rtgui_widget_t, sibling);
  377. }
  378. return sibling;
  379. }
  380. rtgui_widget_t* rtgui_widget_get_prev_sibling(rtgui_widget_t* widget)
  381. {
  382. struct rtgui_list_node* node;
  383. rtgui_widget_t *sibling, *parent;
  384. node = RT_NULL; sibling = RT_NULL;
  385. parent = widget->parent;
  386. if (parent != RT_NULL)
  387. {
  388. rtgui_list_foreach(node, &(RTGUI_CONTAINER(parent)->children))
  389. {
  390. if (node->next == &(widget->sibling))
  391. break;
  392. }
  393. }
  394. if (node != RT_NULL)
  395. sibling = rtgui_list_entry(node, rtgui_widget_t, sibling);
  396. return sibling;
  397. }