widget.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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_set_parent(rtgui_widget_t* widget, rtgui_widget_t* parent)
  113. {
  114. /* set parent and toplevel widget */
  115. widget->parent = parent;
  116. /* update children toplevel */
  117. if (parent->toplevel != RT_NULL &&
  118. RTGUI_IS_TOPLEVEL(parent->toplevel))
  119. {
  120. widget->toplevel = rtgui_widget_get_toplevel(parent);
  121. }
  122. }
  123. void rtgui_widget_get_extent(rtgui_widget_t* widget, rtgui_rect_t *rect)
  124. {
  125. RT_ASSERT(widget != RT_NULL);
  126. RT_ASSERT(rect != RT_NULL);
  127. *rect = widget->extent;
  128. }
  129. #ifndef RTGUI_USING_SMALL_SIZE
  130. void rtgui_widget_set_miniwidth(rtgui_widget_t* widget, int width)
  131. {
  132. RT_ASSERT(widget != RT_NULL);
  133. widget->mini_width = width;
  134. }
  135. void rtgui_widget_set_miniheight(rtgui_widget_t* widget, int height)
  136. {
  137. RT_ASSERT(widget != RT_NULL);
  138. widget->mini_height = height;
  139. }
  140. #endif
  141. /*
  142. * This function moves widget and its children to a logic point
  143. */
  144. void rtgui_widget_move_to_logic(rtgui_widget_t* widget, int dx, int dy)
  145. {
  146. struct rtgui_list_node* node;
  147. rtgui_widget_t* child;
  148. if (widget == RT_NULL) return;
  149. rtgui_rect_moveto(&(widget->extent), dx, dy);
  150. /* move each child */
  151. if (RTGUI_IS_CONTAINER(widget))
  152. {
  153. rtgui_list_foreach(node, &(RTGUI_CONTAINER(widget)->children))
  154. {
  155. child = rtgui_list_entry(node, rtgui_widget_t, sibling);
  156. rtgui_widget_move_to_logic(child, dx, dy);
  157. }
  158. }
  159. }
  160. void rtgui_widget_set_event_handler(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  161. {
  162. RT_ASSERT(widget != RT_NULL);
  163. widget->event_handler = handler;
  164. }
  165. void rtgui_widget_get_rect(rtgui_widget_t* widget, rtgui_rect_t *rect)
  166. {
  167. RT_ASSERT(widget != RT_NULL);
  168. if (rect != RT_NULL)
  169. {
  170. rect->x1 = rect->y1 = 0;
  171. rect->x2 = widget->extent.x2 - widget->extent.x1;
  172. rect->y2 = widget->extent.y2 - widget->extent.y1;
  173. }
  174. }
  175. void rtgui_widget_set_onfocus(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  176. {
  177. RT_ASSERT(widget != RT_NULL);
  178. widget->on_focus_in = handler;
  179. }
  180. void rtgui_widget_set_onunfocus(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  181. {
  182. RT_ASSERT(widget != RT_NULL);
  183. widget->on_focus_out = handler;
  184. }
  185. #ifndef RTGUI_USING_SMALL_SIZE
  186. void rtgui_widget_set_ondraw(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  187. {
  188. RT_ASSERT(widget != RT_NULL);
  189. widget->on_draw = handler;
  190. }
  191. void rtgui_widget_set_onmouseclick(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  192. {
  193. RT_ASSERT(widget != RT_NULL);
  194. widget->on_mouseclick = handler;
  195. }
  196. void rtgui_widget_set_onkey(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  197. {
  198. RT_ASSERT(widget != RT_NULL);
  199. widget->on_key = handler;
  200. }
  201. void rtgui_widget_set_onsize(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  202. {
  203. RT_ASSERT(widget != RT_NULL);
  204. widget->on_size = handler;
  205. }
  206. void rtgui_widget_set_oncommand(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  207. {
  208. RT_ASSERT(widget != RT_NULL);
  209. widget->on_command = handler;
  210. }
  211. #endif
  212. /**
  213. * @brief Focuses the widget. The focused widget is the widget which can receive the keyboard events
  214. * @param widget a widget
  215. * @note The widget has to be attached to a toplevel widget, otherwise it will have no effect
  216. */
  217. void rtgui_widget_focus(rtgui_widget_t *widget)
  218. {
  219. rtgui_container_t *parent;
  220. RT_ASSERT(widget != RT_NULL);
  221. if (!widget->parent || !widget->toplevel) return;
  222. if (!RTGUI_WIDGET_IS_FOCUSABLE(widget) || !RTGUI_WIDGET_IS_ENABLE(widget))
  223. return;
  224. /* set widget as focused */
  225. widget->flag |= RTGUI_WIDGET_FLAG_FOCUS;
  226. /* get root parent container and old focused widget */
  227. parent = RTGUI_CONTAINER(widget->toplevel);
  228. if (parent->focused == widget) return ; /* it's the same focused widget */
  229. /* unfocused the old widget */
  230. if (parent->focused != RT_NULL) rtgui_widget_unfocus(parent->focused);
  231. /* set widget as focused widget in parent link */
  232. parent = RTGUI_CONTAINER(widget->parent);
  233. do
  234. {
  235. parent->focused = widget;
  236. parent = RTGUI_CONTAINER(RTGUI_WIDGET(parent)->parent);
  237. } while ((parent != RT_NULL) && !RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(parent)));
  238. /* invoke on focus in call back */
  239. if (widget->on_focus_in != RT_NULL)
  240. widget->on_focus_in(widget, RT_NULL);
  241. }
  242. /**
  243. * @brief Unfocused the widget
  244. * @param widget a widget
  245. */
  246. void rtgui_widget_unfocus(rtgui_widget_t *widget)
  247. {
  248. RT_ASSERT(widget != RT_NULL);
  249. if (!widget->toplevel || !RTGUI_WIDGET_IS_FOCUSED(widget))
  250. return;
  251. widget->flag &= ~RTGUI_WIDGET_FLAG_FOCUS;
  252. if (widget->on_focus_out != RT_NULL)
  253. widget->on_focus_out(widget, RT_NULL);
  254. /* refresh widget */
  255. rtgui_widget_update(widget);
  256. }
  257. void rtgui_widget_point_to_device(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_device(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. void rtgui_widget_point_to_logic(rtgui_widget_t* widget, rtgui_point_t* point)
  278. {
  279. RT_ASSERT(widget != RT_NULL);
  280. if (point != RT_NULL)
  281. {
  282. point->x -= widget->extent.x1;
  283. point->y -= widget->extent.y1;
  284. }
  285. }
  286. void rtgui_widget_rect_to_logic(rtgui_widget_t* widget, rtgui_rect_t* rect)
  287. {
  288. RT_ASSERT(widget != RT_NULL);
  289. if (rect != RT_NULL)
  290. {
  291. rect->x1 -= widget->extent.x1;
  292. rect->x2 -= widget->extent.x1;
  293. rect->y1 -= widget->extent.y1;
  294. rect->y2 -= widget->extent.y1;
  295. }
  296. }
  297. rtgui_widget_t* rtgui_widget_get_toplevel(rtgui_widget_t* widget)
  298. {
  299. rtgui_widget_t* r;
  300. RT_ASSERT(widget != RT_NULL);
  301. if (widget->toplevel) return widget->toplevel;
  302. r = widget;
  303. /* get the toplevel widget */
  304. while (r->parent != RT_NULL) r = r->parent;
  305. /* set toplevel */
  306. widget->toplevel = r;
  307. return r;
  308. }
  309. rt_bool_t rtgui_widget_event_handler(rtgui_widget_t* widget, rtgui_event_t* event)
  310. {
  311. #ifndef RTGUI_USING_SMALL_SIZE
  312. switch (event->type)
  313. {
  314. case RTGUI_EVENT_PAINT:
  315. if (widget->on_draw != RT_NULL) return widget->on_draw(widget, event);
  316. break;
  317. case RTGUI_EVENT_KBD:
  318. if (widget->on_key != RT_NULL) return widget->on_key(widget, event);
  319. break;
  320. case RTGUI_EVENT_MOUSE_BUTTON:
  321. if (widget->on_mouseclick != RT_NULL) return widget->on_mouseclick(widget, event);
  322. break;
  323. case RTGUI_EVENT_COMMAND:
  324. if (widget->on_command != RT_NULL) return widget->on_command(widget, event);
  325. break;
  326. case RTGUI_EVENT_RESIZE:
  327. if (widget->on_size != RT_NULL) return widget->on_size(widget, event);
  328. break;
  329. }
  330. #endif
  331. return RT_FALSE;
  332. }
  333. /*
  334. * This function updates the clip info of widget
  335. */
  336. void rtgui_widget_update_clip(rtgui_widget_t* widget)
  337. {
  338. struct rtgui_list_node* node;
  339. rtgui_widget_t *parent;
  340. /* no widget or widget is hide, no update clip */
  341. if (widget == RT_NULL || RTGUI_WIDGET_IS_HIDE(widget)) return;
  342. parent = widget->parent;
  343. /* if there is no parent, do not update clip (please use toplevel widget API) */
  344. if (parent == RT_NULL)
  345. {
  346. if (RTGUI_IS_TOPLEVEL(widget))
  347. {
  348. /* if it's toplevel widget, update it by toplevel function */
  349. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(widget));
  350. }
  351. return;
  352. }
  353. /* reset clip to extent */
  354. rtgui_region_reset(&(widget->clip), &(widget->extent));
  355. /* limit widget extent in parent extent */
  356. rtgui_region_intersect(&(widget->clip), &(widget->clip), &(parent->clip));
  357. /* get the no transparent parent */
  358. while (parent != RT_NULL && parent->flag & RTGUI_WIDGET_FLAG_TRANSPARENT)
  359. {
  360. parent = parent->parent;
  361. }
  362. if (parent != RT_NULL)
  363. {
  364. /* subtract widget clip in parent clip */
  365. if (!(widget->flag & RTGUI_WIDGET_FLAG_TRANSPARENT))
  366. {
  367. rtgui_region_subtract_rect(&(parent->clip), &(parent->clip),
  368. &(widget->extent));
  369. }
  370. }
  371. /*
  372. * note: since the layout widget introduction, the sibling widget will not
  373. * intersect.
  374. */
  375. /* if it's a container object, update the clip info of children */
  376. if (RTGUI_IS_CONTAINER(widget))
  377. {
  378. rtgui_widget_t* child;
  379. rtgui_list_foreach(node, &(RTGUI_CONTAINER(widget)->children))
  380. {
  381. child = rtgui_list_entry(node, rtgui_widget_t, sibling);
  382. rtgui_widget_update_clip(child);
  383. }
  384. }
  385. }
  386. void rtgui_widget_show(rtgui_widget_t* widget)
  387. {
  388. /* there is no parent or the parent is hide, no show at all */
  389. if (widget->parent == RT_NULL ||
  390. RTGUI_WIDGET_IS_HIDE(widget->parent)) return;
  391. /* update the clip info of widget */
  392. RTGUI_WIDGET_UNHIDE(widget);
  393. rtgui_widget_update_clip(widget);
  394. }
  395. void rtgui_widget_hide(rtgui_widget_t* widget)
  396. {
  397. rtgui_rect_t rect;
  398. /* hide this widget */
  399. RTGUI_WIDGET_HIDE(widget);
  400. if (widget->parent != RT_NULL)
  401. {
  402. int index;
  403. rtgui_widget_t *parent;
  404. rtgui_toplevel_t *toplevel;
  405. rect = widget->extent;
  406. parent = widget->parent;
  407. /* get the no transparent parent */
  408. while (parent != RT_NULL && parent->flag & RTGUI_WIDGET_FLAG_TRANSPARENT)
  409. {
  410. parent = parent->parent;
  411. }
  412. /* union widget rect */
  413. rtgui_region_union_rect(&(widget->parent->clip), &(widget->parent->clip), &rect);
  414. /* handle extern rect */
  415. toplevel = RTGUI_TOPLEVEL(widget->toplevel);
  416. /* subtract the external rect */
  417. for (index = 0; index < toplevel->external_clip_size; index ++)
  418. {
  419. rtgui_region_subtract_rect(&(widget->parent->clip), &(widget->parent->clip),
  420. &(toplevel->external_clip_rect[index]));
  421. }
  422. }
  423. }
  424. rtgui_color_t rtgui_widget_get_parent_foreground(rtgui_widget_t* widget)
  425. {
  426. rtgui_widget_t* parent;
  427. /* get parent widget */
  428. parent = widget->parent;
  429. while (parent->parent != RT_NULL && (RTGUI_WIDGET_FLAG(parent) & RTGUI_WIDGET_FLAG_TRANSPARENT))
  430. parent = parent->parent;
  431. /* get parent's color */
  432. if (parent != RT_NULL)
  433. return RTGUI_WIDGET_FOREGROUND(parent);
  434. return RTGUI_WIDGET_FOREGROUND(widget);
  435. }
  436. rtgui_color_t rtgui_widget_get_parent_background(rtgui_widget_t* widget)
  437. {
  438. rtgui_widget_t* parent;
  439. /* get parent widget */
  440. parent = widget->parent;
  441. while (parent->parent != RT_NULL && (RTGUI_WIDGET_FLAG(parent) & RTGUI_WIDGET_FLAG_TRANSPARENT))
  442. parent = parent->parent;
  443. /* get parent's color */
  444. if (parent != RT_NULL)
  445. return RTGUI_WIDGET_BACKGROUND(parent);
  446. return RTGUI_WIDGET_BACKGROUND(widget);
  447. }
  448. void rtgui_widget_update(rtgui_widget_t* widget)
  449. {
  450. struct rtgui_event_paint paint;
  451. RTGUI_EVENT_PAINT_INIT(&paint);
  452. paint.wid = RT_NULL;
  453. RT_ASSERT(widget != RT_NULL);
  454. if (widget->event_handler != RT_NULL)
  455. {
  456. widget->event_handler(widget, &paint.parent);
  457. }
  458. }
  459. rtgui_widget_t* rtgui_widget_get_next_sibling(rtgui_widget_t* widget)
  460. {
  461. rtgui_widget_t* sibling = RT_NULL;
  462. if (widget->sibling.next != RT_NULL)
  463. {
  464. sibling = rtgui_list_entry(widget->sibling.next, rtgui_widget_t, sibling);
  465. }
  466. return sibling;
  467. }
  468. rtgui_widget_t* rtgui_widget_get_prev_sibling(rtgui_widget_t* widget)
  469. {
  470. struct rtgui_list_node* node;
  471. rtgui_widget_t *sibling, *parent;
  472. node = RT_NULL; sibling = RT_NULL;
  473. parent = widget->parent;
  474. if (parent != RT_NULL)
  475. {
  476. rtgui_list_foreach(node, &(RTGUI_CONTAINER(parent)->children))
  477. {
  478. if (node->next == &(widget->sibling))
  479. break;
  480. }
  481. }
  482. if (node != RT_NULL)
  483. sibling = rtgui_list_entry(node, rtgui_widget_t, sibling);
  484. return sibling;
  485. }
  486. #ifdef RTGUI_WIDGET_DEBUG
  487. #include <rtgui/widgets/label.h>
  488. #include <rtgui/widgets/button.h>
  489. void rtgui_widget_dump(rtgui_widget_t* widget)
  490. {
  491. struct rtgui_object *obj;
  492. obj = RTGUI_OBJECT(widget);
  493. rt_kprintf("widget type: %s ", obj->type->name);
  494. if (RTGUI_IS_VIEW(widget) == RT_TRUE)
  495. rt_kprintf(":%s ", RTGUI_VIEW(widget)->title);
  496. if (RTGUI_IS_WIN(widget) == RT_TRUE)
  497. rt_kprintf(":%s ", RTGUI_WIN(widget)->title);
  498. if ((RTGUI_IS_LABEL(widget) == RT_TRUE) || (RTGUI_IS_BUTTON(widget) == RT_TRUE))
  499. rt_kprintf(":%s ", RTGUI_LABEL(widget)->text);
  500. rt_kprintf("extent(%d, %d) - (%d, %d)\n", widget->extent.x1,
  501. widget->extent.y1, widget->extent.x2, widget->extent.y2);
  502. // rtgui_region_dump(&(widget->clip));
  503. }
  504. #endif