widget.c 14 KB

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