widget.c 13 KB

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