widget.c 13 KB

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