widget.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. /*
  2. * File : widget.c
  3. * This file is part of RT-Thread GUI
  4. * COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2009-10-04 Bernard first version
  23. * 2010-06-26 Bernard add user_data to widget structure
  24. * 2013-10-07 Bernard remove the win_check in update_clip.
  25. */
  26. #include <rtgui/dc_client.h>
  27. #include <rtgui/rtgui_app.h>
  28. #include <rtgui/widgets/widget.h>
  29. #include <rtgui/widgets/window.h>
  30. #include <rtgui/widgets/container.h>
  31. #ifdef RTGUI_USING_NOTEBOOK
  32. #include <rtgui/widgets/notebook.h>
  33. #endif
  34. static void _rtgui_widget_constructor(rtgui_widget_t *widget)
  35. {
  36. if (!widget) return;
  37. /* set widget as shown */
  38. widget->flag = RTGUI_WIDGET_FLAG_SHOWN;
  39. /* init list */
  40. rtgui_list_init(&(widget->sibling));
  41. /* init gc */
  42. widget->gc.foreground = default_foreground;
  43. widget->gc.background = default_background;
  44. widget->gc.font = rtgui_font_default();
  45. widget->gc.textstyle = RTGUI_TEXTSTYLE_NORMAL;
  46. widget->gc.textalign = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_TOP;
  47. widget->align = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_TOP;
  48. /* clear the garbage value of extent and clip */
  49. widget->extent.x1 = widget->extent.y1 = 0;
  50. widget->extent.x2 = widget->extent.y2 = 0;
  51. widget->min_width = widget->min_height = 0;
  52. rtgui_region_init_with_extents(&widget->clip, &widget->extent);
  53. /* set parent and toplevel root */
  54. widget->parent = RT_NULL;
  55. widget->toplevel = RT_NULL;
  56. /* some common event handler */
  57. widget->on_focus_in = RT_NULL;
  58. widget->on_focus_out = RT_NULL;
  59. widget->on_paint = RT_NULL;
  60. /* set default event handler */
  61. rtgui_object_set_event_handler(RTGUI_OBJECT(widget), rtgui_widget_event_handler);
  62. /* init user data private to 0 */
  63. widget->user_data = 0;
  64. /* init clip information */
  65. rtgui_region_init(&(widget->clip));
  66. /* init hardware dc */
  67. rtgui_dc_client_init(widget);
  68. }
  69. /* Destroys the widget */
  70. static void _rtgui_widget_destructor(rtgui_widget_t *widget)
  71. {
  72. if (widget == RT_NULL) return;
  73. if (widget->parent != RT_NULL && RTGUI_IS_CONTAINER(widget->parent))
  74. {
  75. /* remove widget from parent's children list */
  76. rtgui_list_remove(&(RTGUI_CONTAINER(widget->parent)->children), &(widget->sibling));
  77. widget->parent = RT_NULL;
  78. }
  79. /* fini clip region */
  80. rtgui_region_fini(&(widget->clip));
  81. }
  82. DEFINE_CLASS_TYPE(widget, "widget",
  83. RTGUI_PARENT_TYPE(object),
  84. _rtgui_widget_constructor,
  85. _rtgui_widget_destructor,
  86. sizeof(struct rtgui_widget));
  87. RTM_EXPORT(_rtgui_widget);
  88. rtgui_widget_t *rtgui_widget_create(const rtgui_type_t *widget_type)
  89. {
  90. struct rtgui_widget *widget;
  91. widget = RTGUI_WIDGET(rtgui_object_create(widget_type));
  92. return widget;
  93. }
  94. RTM_EXPORT(rtgui_widget_create);
  95. void rtgui_widget_destroy(rtgui_widget_t *widget)
  96. {
  97. rtgui_object_destroy(RTGUI_OBJECT(widget));
  98. }
  99. RTM_EXPORT(rtgui_widget_destroy);
  100. void rtgui_widget_set_rect(rtgui_widget_t *widget, const rtgui_rect_t *rect)
  101. {
  102. int delta_x, delta_y;
  103. if (widget == RT_NULL || rect == RT_NULL) return;
  104. /* update extent rectangle */
  105. widget->extent = *rect;
  106. if (RTGUI_IS_CONTAINER(widget))
  107. {
  108. /* re-do layout */
  109. rtgui_container_layout(RTGUI_CONTAINER(widget));
  110. }
  111. /* reset min width and height */
  112. widget->min_width = rtgui_rect_width(widget->extent);
  113. widget->min_height = rtgui_rect_height(widget->extent);
  114. /* it's not empty, fini it */
  115. if (rtgui_region_not_empty(&(widget->clip)))
  116. {
  117. rtgui_region_fini(&(widget->clip));
  118. }
  119. /* reset clip info */
  120. rtgui_region_init_with_extents(&(widget->clip), rect);
  121. if ((widget->parent != RT_NULL) && (widget->toplevel != RT_NULL))
  122. {
  123. if ((void*)widget->parent == (void*)widget->toplevel)
  124. {
  125. rtgui_win_update_clip(widget->toplevel);
  126. }
  127. else
  128. {
  129. /* update widget clip */
  130. rtgui_widget_update_clip(widget->parent);
  131. }
  132. }
  133. /* move to a logic position if it's a container widget */
  134. if (RTGUI_IS_CONTAINER(widget))
  135. {
  136. delta_x = rect->x1 - widget->extent.x1;
  137. delta_y = rect->y1 - widget->extent.y1;
  138. rtgui_widget_move_to_logic(widget, delta_x, delta_y);
  139. }
  140. }
  141. RTM_EXPORT(rtgui_widget_set_rect);
  142. void rtgui_widget_set_rectangle(rtgui_widget_t *widget, int x, int y, int width, int height)
  143. {
  144. rtgui_rect_t rect;
  145. rect.x1 = x;
  146. rect.y1 = y;
  147. rect.x2 = x + width;
  148. rect.y2 = y + height;
  149. rtgui_widget_set_rect(widget, &rect);
  150. }
  151. RTM_EXPORT(rtgui_widget_set_rectangle);
  152. void rtgui_widget_set_parent(rtgui_widget_t *widget, rtgui_widget_t *parent)
  153. {
  154. /* set parent and toplevel widget */
  155. widget->parent = parent;
  156. }
  157. RTM_EXPORT(rtgui_widget_set_parent);
  158. void rtgui_widget_get_extent(rtgui_widget_t *widget, rtgui_rect_t *rect)
  159. {
  160. RT_ASSERT(widget != RT_NULL);
  161. RT_ASSERT(rect != RT_NULL);
  162. *rect = widget->extent;
  163. }
  164. RTM_EXPORT(rtgui_widget_get_extent);
  165. void rtgui_widget_set_minsize(rtgui_widget_t *widget, int width, int height)
  166. {
  167. RT_ASSERT(widget != RT_NULL);
  168. widget->min_width = width;
  169. widget->min_height = height;
  170. }
  171. RTM_EXPORT(rtgui_widget_set_minsize);
  172. void rtgui_widget_set_minwidth(rtgui_widget_t *widget, int width)
  173. {
  174. RT_ASSERT(widget != RT_NULL);
  175. widget->min_width = width;
  176. }
  177. RTM_EXPORT(rtgui_widget_set_minwidth);
  178. void rtgui_widget_set_minheight(rtgui_widget_t *widget, int height)
  179. {
  180. RT_ASSERT(widget != RT_NULL);
  181. widget->min_height = height;
  182. }
  183. RTM_EXPORT(rtgui_widget_set_minheight);
  184. static void _widget_moveto(struct rtgui_widget* widget, int dx, int dy)
  185. {
  186. struct rtgui_list_node *node;
  187. rtgui_widget_t *child;
  188. rtgui_rect_moveto(&(widget->extent), dx, dy);
  189. /* reset clip info */
  190. rtgui_region_init_with_extents(&(widget->clip), &(widget->extent));
  191. /* move each child */
  192. if (RTGUI_IS_CONTAINER(widget))
  193. {
  194. rtgui_list_foreach(node, &(RTGUI_CONTAINER(widget)->children))
  195. {
  196. child = rtgui_list_entry(node, rtgui_widget_t, sibling);
  197. _widget_moveto(child, dx, dy);
  198. }
  199. }
  200. }
  201. /*
  202. * This function moves widget and its children to a logic point
  203. */
  204. void rtgui_widget_move_to_logic(rtgui_widget_t *widget, int dx, int dy)
  205. {
  206. rtgui_rect_t rect;
  207. rtgui_widget_t *parent;
  208. if (widget == RT_NULL)
  209. return;
  210. /* give clip of this widget back to parent */
  211. parent = widget->parent;
  212. if (parent != RT_NULL)
  213. {
  214. /* get the parent rect, even if it's a transparent parent. */
  215. rect = parent->clip.extents;
  216. }
  217. /* we should find out the none-transparent parent */
  218. while (parent != RT_NULL && parent->flag & RTGUI_WIDGET_FLAG_TRANSPARENT) parent = parent->parent;
  219. if (parent != RT_NULL)
  220. {
  221. /* reset clip info */
  222. rtgui_region_init_with_extents(&(widget->clip), &(widget->extent));
  223. rtgui_region_intersect_rect(&(widget->clip), &(widget->clip), &rect);
  224. rtgui_region_intersect_rect(&(widget->clip), &(widget->clip), &(parent->extent));
  225. /* give back the extent */
  226. rtgui_region_union(&(parent->clip), &(parent->clip), &(widget->clip));
  227. }
  228. /* move this widget (and its children if it's a container) to destination point */
  229. _widget_moveto(widget, dx, dy);
  230. /* update this widget */
  231. rtgui_widget_update_clip(widget);
  232. }
  233. RTM_EXPORT(rtgui_widget_move_to_logic);
  234. void rtgui_widget_get_rect(rtgui_widget_t *widget, rtgui_rect_t *rect)
  235. {
  236. RT_ASSERT(widget != RT_NULL);
  237. if (rect != RT_NULL)
  238. {
  239. rect->x1 = rect->y1 = 0;
  240. rect->x2 = widget->extent.x2 - widget->extent.x1;
  241. rect->y2 = widget->extent.y2 - widget->extent.y1;
  242. }
  243. }
  244. RTM_EXPORT(rtgui_widget_get_rect);
  245. /**
  246. * set widget draw style
  247. */
  248. void rtgui_widget_set_border(rtgui_widget_t *widget, rt_uint32_t style)
  249. {
  250. RT_ASSERT(widget != RT_NULL);
  251. widget->border_style = style;
  252. switch (style)
  253. {
  254. case RTGUI_BORDER_NONE:
  255. widget->border = 0;
  256. break;
  257. case RTGUI_BORDER_SIMPLE:
  258. case RTGUI_BORDER_UP:
  259. case RTGUI_BORDER_DOWN:
  260. widget->border = 1;
  261. break;
  262. case RTGUI_BORDER_STATIC:
  263. case RTGUI_BORDER_RAISE:
  264. case RTGUI_BORDER_SUNKEN:
  265. case RTGUI_BORDER_BOX:
  266. case RTGUI_BORDER_EXTRA:
  267. widget->border = 2;
  268. break;
  269. default:
  270. widget->border = 2;
  271. break;
  272. }
  273. }
  274. RTM_EXPORT(rtgui_widget_set_border);
  275. void rtgui_widget_set_onfocus(rtgui_widget_t *widget, rtgui_event_handler_ptr handler)
  276. {
  277. RT_ASSERT(widget != RT_NULL);
  278. widget->on_focus_in = handler;
  279. }
  280. RTM_EXPORT(rtgui_widget_set_onfocus);
  281. void rtgui_widget_set_onunfocus(rtgui_widget_t *widget, rtgui_event_handler_ptr handler)
  282. {
  283. RT_ASSERT(widget != RT_NULL);
  284. widget->on_focus_out = handler;
  285. }
  286. RTM_EXPORT(rtgui_widget_set_onunfocus);
  287. void rtgui_widget_set_onpaint(rtgui_widget_t *widget, rtgui_event_handler_ptr handler)
  288. {
  289. RT_ASSERT(widget != RT_NULL);
  290. widget->on_paint = handler;
  291. }
  292. RTM_EXPORT(rtgui_widget_set_onpaint);
  293. /**
  294. * @brief Focuses the widget. The focused widget is the widget which can receive the keyboard events
  295. * @param widget a widget
  296. * @note The widget has to be attached to a toplevel widget, otherwise it will have no effect
  297. */
  298. void rtgui_widget_focus(rtgui_widget_t *widget)
  299. {
  300. struct rtgui_widget *old_focus;
  301. RT_ASSERT(widget != RT_NULL);
  302. RT_ASSERT(widget->toplevel != RT_NULL);
  303. if (!RTGUI_WIDGET_IS_FOCUSABLE(widget) || !RTGUI_WIDGET_IS_ENABLE(widget))
  304. return;
  305. old_focus = RTGUI_WIN(widget->toplevel)->focused_widget;
  306. if (old_focus == widget)
  307. return; /* it's the same focused widget */
  308. /* unfocused the old widget */
  309. if (old_focus != RT_NULL)
  310. rtgui_widget_unfocus(old_focus);
  311. /* set widget as focused */
  312. widget->flag |= RTGUI_WIDGET_FLAG_FOCUS;
  313. RTGUI_WIN(widget->toplevel)->focused_widget = widget;
  314. /* invoke on focus in call back */
  315. if (widget->on_focus_in != RT_NULL)
  316. widget->on_focus_in(RTGUI_OBJECT(widget), RT_NULL);
  317. }
  318. RTM_EXPORT(rtgui_widget_focus);
  319. /**
  320. * @brief Unfocused the widget
  321. * @param widget a widget
  322. */
  323. void rtgui_widget_unfocus(rtgui_widget_t *widget)
  324. {
  325. RT_ASSERT(widget != RT_NULL);
  326. if (!widget->toplevel || !RTGUI_WIDGET_IS_FOCUSED(widget))
  327. return;
  328. widget->flag &= ~RTGUI_WIDGET_FLAG_FOCUS;
  329. if (widget->on_focus_out != RT_NULL)
  330. widget->on_focus_out(RTGUI_OBJECT(widget), RT_NULL);
  331. RTGUI_WIN(widget->toplevel)->focused_widget = RT_NULL;
  332. /* Ergodic constituent widget, make child loss of focus */
  333. if (RTGUI_IS_CONTAINER(widget))
  334. {
  335. rtgui_list_t *node;
  336. rtgui_list_foreach(node, &(RTGUI_CONTAINER(widget)->children))
  337. {
  338. rtgui_widget_t *child = rtgui_list_entry(node, rtgui_widget_t, sibling);
  339. if (RTGUI_WIDGET_IS_HIDE(child)) continue;
  340. rtgui_widget_unfocus(child);
  341. }
  342. }
  343. }
  344. RTM_EXPORT(rtgui_widget_unfocus);
  345. void rtgui_widget_point_to_device(rtgui_widget_t *widget, rtgui_point_t *point)
  346. {
  347. RT_ASSERT(widget != RT_NULL);
  348. if (point != RT_NULL)
  349. {
  350. point->x += widget->extent.x1;
  351. point->y += widget->extent.y1;
  352. }
  353. }
  354. RTM_EXPORT(rtgui_widget_point_to_device);
  355. void rtgui_widget_rect_to_device(rtgui_widget_t *widget, rtgui_rect_t *rect)
  356. {
  357. RT_ASSERT(widget != RT_NULL);
  358. if (rect != RT_NULL)
  359. {
  360. rect->x1 += widget->extent.x1;
  361. rect->x2 += widget->extent.x1;
  362. rect->y1 += widget->extent.y1;
  363. rect->y2 += widget->extent.y1;
  364. }
  365. }
  366. RTM_EXPORT(rtgui_widget_rect_to_device);
  367. void rtgui_widget_point_to_logic(rtgui_widget_t *widget, rtgui_point_t *point)
  368. {
  369. RT_ASSERT(widget != RT_NULL);
  370. if (point != RT_NULL)
  371. {
  372. point->x -= widget->extent.x1;
  373. point->y -= widget->extent.y1;
  374. }
  375. }
  376. RTM_EXPORT(rtgui_widget_point_to_logic);
  377. void rtgui_widget_rect_to_logic(rtgui_widget_t *widget, rtgui_rect_t *rect)
  378. {
  379. RT_ASSERT(widget != RT_NULL);
  380. if (rect != RT_NULL)
  381. {
  382. rect->x1 -= widget->extent.x1;
  383. rect->x2 -= widget->extent.x1;
  384. rect->y1 -= widget->extent.y1;
  385. rect->y2 -= widget->extent.y1;
  386. }
  387. }
  388. RTM_EXPORT(rtgui_widget_rect_to_logic);
  389. struct rtgui_win *rtgui_widget_get_toplevel(rtgui_widget_t *widget)
  390. {
  391. rtgui_widget_t *r;
  392. RT_ASSERT(widget != RT_NULL);
  393. if (widget->toplevel)
  394. return widget->toplevel;
  395. rt_kprintf("widget->toplevel not properly set\n");
  396. r = widget;
  397. /* get the toplevel widget */
  398. while (r->parent != RT_NULL)
  399. r = r->parent;
  400. /* set toplevel */
  401. widget->toplevel = RTGUI_WIN(r);
  402. return RTGUI_WIN(r);
  403. }
  404. RTM_EXPORT(rtgui_widget_get_toplevel);
  405. rt_bool_t rtgui_widget_onupdate_toplvl(struct rtgui_object *object, struct rtgui_event *event)
  406. {
  407. struct rtgui_widget *widget;
  408. struct rtgui_event_update_toplvl *eup;
  409. RT_ASSERT(object);
  410. RT_ASSERT(event);
  411. widget = RTGUI_WIDGET(object);
  412. eup = (struct rtgui_event_update_toplvl *)event;
  413. widget->toplevel = eup->toplvl;
  414. return RT_FALSE;
  415. }
  416. RTM_EXPORT(rtgui_widget_onupdate_toplvl);
  417. rt_bool_t rtgui_widget_onpaint(struct rtgui_object *object, struct rtgui_event *event)
  418. {
  419. if (RTGUI_WIDGET(object)->on_paint)
  420. return RTGUI_WIDGET(object)->on_paint(object, event);
  421. else
  422. return RT_FALSE;
  423. }
  424. RTM_EXPORT(rtgui_widget_onpaint);
  425. rt_bool_t rtgui_widget_event_handler(struct rtgui_object *object, rtgui_event_t *event)
  426. {
  427. RTGUI_WIDGET_EVENT_HANDLER_PREPARE;
  428. switch (event->type)
  429. {
  430. case RTGUI_EVENT_PAINT:
  431. return rtgui_widget_onpaint(object, event);
  432. case RTGUI_EVENT_SHOW:
  433. return rtgui_widget_onshow(object, event);
  434. case RTGUI_EVENT_HIDE:
  435. return rtgui_widget_onhide(object, event);
  436. case RTGUI_EVENT_UPDATE_TOPLVL:
  437. return rtgui_widget_onupdate_toplvl(object, event);
  438. default:
  439. break;
  440. }
  441. return rtgui_object_event_handler(object, event);
  442. }
  443. RTM_EXPORT(rtgui_widget_event_handler);
  444. /*
  445. * This function updates the clip info of widget
  446. */
  447. void rtgui_widget_update_clip(rtgui_widget_t *widget)
  448. {
  449. rtgui_rect_t rect;
  450. struct rtgui_list_node *node;
  451. rtgui_widget_t *parent;
  452. /* no widget or widget is hide, no update clip */
  453. if (widget == RT_NULL || RTGUI_WIDGET_IS_HIDE(widget) || widget->parent == RT_NULL)
  454. return;
  455. parent = widget->parent;
  456. rect = parent->clip.extents;
  457. /* reset clip to extent */
  458. rtgui_region_reset(&(widget->clip), &(widget->extent));
  459. /* limit widget extent in parent extent */
  460. rtgui_region_intersect_rect(&(widget->clip), &(widget->clip), &rect);
  461. /* get the no transparent parent */
  462. while (parent != RT_NULL && parent->flag & RTGUI_WIDGET_FLAG_TRANSPARENT)
  463. {
  464. parent = parent->parent;
  465. }
  466. if (parent != RT_NULL)
  467. {
  468. /* give my clip back to parent */
  469. rtgui_region_union(&(parent->clip), &(parent->clip), &(widget->clip));
  470. /* subtract widget clip in parent clip */
  471. if (!(widget->flag & RTGUI_WIDGET_FLAG_TRANSPARENT) && RTGUI_IS_CONTAINER(parent))
  472. {
  473. rtgui_region_subtract_rect(&(parent->clip), &(parent->clip), &(widget->extent));
  474. }
  475. }
  476. /*
  477. * note: since the layout widget introduction, the sibling widget should not intersect.
  478. */
  479. /* if it's a container object, update the clip info of children */
  480. if (RTGUI_IS_CONTAINER(widget))
  481. {
  482. rtgui_widget_t *child;
  483. rtgui_list_foreach(node, &(RTGUI_CONTAINER(widget)->children))
  484. {
  485. child = rtgui_list_entry(node, rtgui_widget_t, sibling);
  486. rtgui_widget_update_clip(child);
  487. }
  488. }
  489. }
  490. RTM_EXPORT(rtgui_widget_update_clip);
  491. void rtgui_widget_show(struct rtgui_widget *widget)
  492. {
  493. struct rtgui_event_show eshow;
  494. RT_ASSERT(widget != RT_NULL);
  495. if (!RTGUI_WIDGET_IS_HIDE(widget))
  496. return;
  497. RTGUI_WIDGET_UNHIDE(widget);
  498. if (widget->toplevel != RT_NULL)
  499. {
  500. RTGUI_EVENT_SHOW_INIT(&eshow);
  501. if (RTGUI_OBJECT(widget)->event_handler != RT_NULL)
  502. {
  503. RTGUI_OBJECT(widget)->event_handler(
  504. RTGUI_OBJECT(widget),
  505. &eshow);
  506. }
  507. }
  508. }
  509. RTM_EXPORT(rtgui_widget_show);
  510. void rtgui_widget_hide(struct rtgui_widget *widget)
  511. {
  512. struct rtgui_event_hide ehide;
  513. RT_ASSERT(widget != RT_NULL);
  514. if (RTGUI_WIDGET_IS_HIDE(widget))
  515. return;
  516. RTGUI_WIDGET_HIDE(widget);
  517. if (widget->toplevel != RT_NULL)
  518. {
  519. RTGUI_EVENT_HIDE_INIT(&ehide);
  520. if (RTGUI_OBJECT(widget)->event_handler != RT_NULL)
  521. {
  522. RTGUI_OBJECT(widget)->event_handler(
  523. RTGUI_OBJECT(widget),
  524. &ehide);
  525. }
  526. }
  527. }
  528. RTM_EXPORT(rtgui_widget_hide);
  529. rt_bool_t rtgui_widget_onshow(struct rtgui_object *object, struct rtgui_event *event)
  530. {
  531. struct rtgui_widget *widget = RTGUI_WIDGET(object);
  532. if (RTGUI_WIDGET_IS_HIDE(object)) return RT_FALSE;
  533. if (widget->parent != RT_NULL)
  534. {
  535. rtgui_widget_clip_parent(widget);
  536. }
  537. return RT_FALSE;
  538. }
  539. RTM_EXPORT(rtgui_widget_onshow);
  540. rt_bool_t rtgui_widget_onhide(struct rtgui_object *object, struct rtgui_event *event)
  541. {
  542. struct rtgui_widget *widget = RTGUI_WIDGET(object);
  543. if (RTGUI_WIDGET_IS_HIDE(object))
  544. return RT_FALSE;
  545. if (widget->parent != RT_NULL)
  546. {
  547. rtgui_widget_clip_return(widget);
  548. }
  549. return RT_FALSE;
  550. }
  551. RTM_EXPORT(rtgui_widget_onhide);
  552. rtgui_color_t rtgui_widget_get_parent_foreground(rtgui_widget_t *widget)
  553. {
  554. rtgui_widget_t *parent;
  555. /* get parent widget */
  556. parent = widget->parent;
  557. if (parent == RT_NULL)
  558. return RTGUI_WIDGET_FOREGROUND(widget);
  559. while (parent->parent != RT_NULL && (RTGUI_WIDGET_FLAG(parent) & RTGUI_WIDGET_FLAG_TRANSPARENT))
  560. parent = parent->parent;
  561. /* get parent's color */
  562. return RTGUI_WIDGET_FOREGROUND(parent);
  563. }
  564. RTM_EXPORT(rtgui_widget_get_parent_foreground);
  565. rtgui_color_t rtgui_widget_get_parent_background(rtgui_widget_t *widget)
  566. {
  567. rtgui_widget_t *parent;
  568. /* get parent widget */
  569. parent = widget->parent;
  570. if (parent == RT_NULL)
  571. return RTGUI_WIDGET_BACKGROUND(widget);
  572. while (parent->parent != RT_NULL && (RTGUI_WIDGET_FLAG(parent) & RTGUI_WIDGET_FLAG_TRANSPARENT))
  573. parent = parent->parent;
  574. /* get parent's color */
  575. return RTGUI_WIDGET_BACKGROUND(parent);
  576. }
  577. RTM_EXPORT(rtgui_widget_get_parent_background);
  578. void rtgui_widget_clip_parent(rtgui_widget_t *widget)
  579. {
  580. rtgui_widget_t *parent;
  581. parent = widget->parent;
  582. /* get the no transparent parent */
  583. while (parent != RT_NULL && parent->flag & RTGUI_WIDGET_FLAG_TRANSPARENT) parent = parent->parent;
  584. /* clip the widget extern from parent */
  585. if (parent != RT_NULL) rtgui_region_subtract(&(parent->clip), &(parent->clip), &(widget->clip));
  586. }
  587. RTM_EXPORT(rtgui_widget_clip_parent);
  588. void rtgui_widget_clip_return(rtgui_widget_t *widget)
  589. {
  590. rtgui_widget_t *parent;
  591. parent = widget->parent;
  592. /* get the no transparent parent */
  593. while (parent != RT_NULL && parent->flag & RTGUI_WIDGET_FLAG_TRANSPARENT) parent = parent->parent;
  594. /* give clip back to parent */
  595. if (parent != RT_NULL) rtgui_region_union(&(parent->clip), &(parent->clip), &(widget->clip));
  596. }
  597. RTM_EXPORT(rtgui_widget_clip_return);
  598. void rtgui_widget_update(rtgui_widget_t *widget)
  599. {
  600. struct rtgui_event_paint paint;
  601. RTGUI_EVENT_PAINT_INIT(&paint);
  602. paint.wid = RT_NULL;
  603. RT_ASSERT(widget != RT_NULL);
  604. if (RTGUI_OBJECT(widget)->event_handler != RT_NULL &&
  605. !(RTGUI_WIDGET_FLAG(widget) & RTGUI_WIDGET_FLAG_IN_ANIM))
  606. {
  607. RTGUI_OBJECT(widget)->event_handler(RTGUI_OBJECT(widget),
  608. &paint.parent);
  609. }
  610. }
  611. RTM_EXPORT(rtgui_widget_update);
  612. rtgui_widget_t *rtgui_widget_get_next_sibling(rtgui_widget_t *widget)
  613. {
  614. rtgui_widget_t *sibling = RT_NULL;
  615. if (widget->sibling.next != RT_NULL)
  616. {
  617. sibling = rtgui_list_entry(widget->sibling.next, rtgui_widget_t, sibling);
  618. }
  619. return sibling;
  620. }
  621. RTM_EXPORT(rtgui_widget_get_next_sibling);
  622. rtgui_widget_t *rtgui_widget_get_prev_sibling(rtgui_widget_t *widget)
  623. {
  624. struct rtgui_list_node *node;
  625. rtgui_widget_t *sibling, *parent;
  626. node = RT_NULL;
  627. sibling = RT_NULL;
  628. parent = widget->parent;
  629. if (parent != RT_NULL)
  630. {
  631. rtgui_list_foreach(node, &(RTGUI_CONTAINER(parent)->children))
  632. {
  633. if (node->next == &(widget->sibling))
  634. break;
  635. }
  636. }
  637. if (node != RT_NULL)
  638. sibling = rtgui_list_entry(node, rtgui_widget_t, sibling);
  639. return sibling;
  640. }
  641. RTM_EXPORT(rtgui_widget_get_prev_sibling);
  642. rt_bool_t rtgui_widget_is_in_animation(rtgui_widget_t *widget)
  643. {
  644. /* check the visible of widget */
  645. while (widget != RT_NULL)
  646. {
  647. if (widget->flag & RTGUI_WIDGET_FLAG_IN_ANIM)
  648. {
  649. return RT_TRUE;
  650. }
  651. widget = widget->parent;
  652. }
  653. return RT_FALSE;
  654. }
  655. RTM_EXPORT(rtgui_widget_is_in_animation);
  656. #ifdef RTGUI_WIDGET_DEBUG
  657. #include <rtgui/widgets/label.h>
  658. #include <rtgui/widgets/button.h>
  659. void rtgui_widget_dump(rtgui_widget_t *widget)
  660. {
  661. struct rtgui_object *obj;
  662. obj = RTGUI_OBJECT(widget);
  663. rt_kprintf("widget type: %s ", obj->type->name);
  664. if (RTGUI_IS_WIN(widget) == RT_TRUE)
  665. rt_kprintf(":%s ", RTGUI_WIN(widget)->title);
  666. else if ((RTGUI_IS_LABEL(widget) == RT_TRUE) || (RTGUI_IS_BUTTON(widget) == RT_TRUE))
  667. rt_kprintf(":%s ", RTGUI_LABEL(widget)->text);
  668. rt_kprintf("extent(%d, %d) - (%d, %d)\n", widget->extent.x1,
  669. widget->extent.y1, widget->extent.x2, widget->extent.y2);
  670. // rtgui_region_dump(&(widget->clip));
  671. }
  672. #endif