widget.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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/rtgui_app.h>
  17. #include <rtgui/widgets/widget.h>
  18. #include <rtgui/widgets/window.h>
  19. #include <rtgui/widgets/container.h>
  20. #include <rtgui/widgets/notebook.h>
  21. static void _rtgui_widget_constructor(rtgui_widget_t *widget)
  22. {
  23. if (!widget) return;
  24. /* set default flag */
  25. widget->flag = RTGUI_WIDGET_FLAG_DEFAULT;
  26. /* init list */
  27. rtgui_list_init(&(widget->sibling));
  28. /* init gc */
  29. widget->gc.foreground = default_foreground;
  30. widget->gc.background = default_background;
  31. widget->gc.font = rtgui_font_default();
  32. widget->gc.textstyle = RTGUI_TEXTSTYLE_NORMAL;
  33. widget->gc.textalign = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_TOP;
  34. widget->align = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_TOP;
  35. /* set parent and toplevel root */
  36. widget->parent = RT_NULL;
  37. widget->toplevel = RT_NULL;
  38. /* some common event handler */
  39. widget->on_focus_in = RT_NULL;
  40. widget->on_focus_out = RT_NULL;
  41. widget->on_show = RT_NULL;
  42. widget->on_hide = RT_NULL;
  43. #ifndef RTGUI_USING_SMALL_SIZE
  44. widget->on_draw = RT_NULL;
  45. widget->on_mouseclick = RT_NULL;
  46. widget->on_key = RT_NULL;
  47. widget->on_size = RT_NULL;
  48. widget->on_command = RT_NULL;
  49. #endif
  50. /* set default event handler */
  51. rtgui_object_set_event_handler(RTGUI_OBJECT(widget), rtgui_widget_event_handler);
  52. /* init user data private to 0 */
  53. widget->user_data = 0;
  54. /* init clip information */
  55. rtgui_region_init(&(widget->clip));
  56. /* init hardware dc */
  57. rtgui_dc_client_init(widget);
  58. }
  59. /* Destroys the widget */
  60. static void _rtgui_widget_destructor(rtgui_widget_t *widget)
  61. {
  62. if (widget == RT_NULL) return;
  63. if (widget->parent != RT_NULL && RTGUI_IS_CONTAINER(widget->parent))
  64. {
  65. /* remove widget from parent's children list */
  66. rtgui_list_remove(&(RTGUI_CONTAINER(widget->parent)->children), &(widget->sibling));
  67. widget->parent = RT_NULL;
  68. }
  69. /* fini clip region */
  70. rtgui_region_fini(&(widget->clip));
  71. }
  72. DEFINE_CLASS_TYPE(widget, "widget",
  73. RTGUI_OBJECT_TYPE,
  74. _rtgui_widget_constructor,
  75. _rtgui_widget_destructor,
  76. sizeof(struct rtgui_widget));
  77. RTM_EXPORT(_rtgui_widget);
  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. RTM_EXPORT(rtgui_widget_create);
  85. void rtgui_widget_destroy(rtgui_widget_t* widget)
  86. {
  87. rtgui_object_destroy(RTGUI_OBJECT(widget));
  88. }
  89. RTM_EXPORT(rtgui_widget_destroy);
  90. void rtgui_widget_set_rect(rtgui_widget_t* widget, const rtgui_rect_t* rect)
  91. {
  92. int delta_x, delta_y;
  93. if (widget == RT_NULL || rect == RT_NULL) return;
  94. /* move to a logic position if it's a container widget */
  95. if (RTGUI_IS_CONTAINER(widget))
  96. {
  97. delta_x = rect->x1 - widget->extent.x1;
  98. delta_y = rect->y1 - widget->extent.y1;
  99. rtgui_widget_move_to_logic(widget, delta_x, delta_y);
  100. }
  101. /* update extent rectangle */
  102. widget->extent = *rect;
  103. /* reset mini width and height */
  104. widget->mini_width = rtgui_rect_width(widget->extent);
  105. widget->mini_height = rtgui_rect_height(widget->extent);
  106. /* it's not empty, fini it */
  107. if (rtgui_region_not_empty(&(widget->clip)))
  108. {
  109. rtgui_region_fini(&(widget->clip));
  110. }
  111. /* reset clip info */
  112. rtgui_region_init_with_extents(&(widget->clip), rect);
  113. if ((widget->parent != RT_NULL) && (widget->toplevel != RT_NULL))
  114. {
  115. /* update widget clip */
  116. rtgui_widget_update_clip(widget->parent);
  117. }
  118. }
  119. RTM_EXPORT(rtgui_widget_set_rect);
  120. void rtgui_widget_set_rectangle(rtgui_widget_t* widget, int x, int y, int width, int height)
  121. {
  122. rtgui_rect_t rect;
  123. rect.x1 = x; rect.y1 = y;
  124. rect.x2 = x + width; rect.y2 = y + height;
  125. rtgui_widget_set_rect(widget, &rect);
  126. }
  127. RTM_EXPORT(rtgui_widget_set_rectangle);
  128. void rtgui_widget_set_parent(rtgui_widget_t* widget, rtgui_widget_t* parent)
  129. {
  130. /* set parent and toplevel widget */
  131. widget->parent = parent;
  132. }
  133. RTM_EXPORT(rtgui_widget_set_parent);
  134. void rtgui_widget_get_extent(rtgui_widget_t* widget, rtgui_rect_t *rect)
  135. {
  136. RT_ASSERT(widget != RT_NULL);
  137. RT_ASSERT(rect != RT_NULL);
  138. *rect = widget->extent;
  139. }
  140. RTM_EXPORT(rtgui_widget_get_extent);
  141. void rtgui_widget_set_miniwidth(rtgui_widget_t* widget, int width)
  142. {
  143. RT_ASSERT(widget != RT_NULL);
  144. widget->mini_width = width;
  145. }
  146. RTM_EXPORT(rtgui_widget_set_miniwidth);
  147. void rtgui_widget_set_miniheight(rtgui_widget_t* widget, int height)
  148. {
  149. RT_ASSERT(widget != RT_NULL);
  150. widget->mini_height = height;
  151. }
  152. RTM_EXPORT(rtgui_widget_set_miniheight);
  153. /*
  154. * This function moves widget and its children to a logic point
  155. */
  156. void rtgui_widget_move_to_logic(rtgui_widget_t* widget, int dx, int dy)
  157. {
  158. struct rtgui_list_node* node;
  159. rtgui_widget_t* child;
  160. if (widget == RT_NULL) return;
  161. rtgui_rect_moveto(&(widget->extent), dx, dy);
  162. /* move each child */
  163. if (RTGUI_IS_CONTAINER(widget))
  164. {
  165. rtgui_list_foreach(node, &(RTGUI_CONTAINER(widget)->children))
  166. {
  167. child = rtgui_list_entry(node, rtgui_widget_t, sibling);
  168. rtgui_widget_move_to_logic(child, dx, dy);
  169. }
  170. }
  171. }
  172. RTM_EXPORT(rtgui_widget_move_to_logic);
  173. void rtgui_widget_get_rect(rtgui_widget_t* widget, rtgui_rect_t *rect)
  174. {
  175. RT_ASSERT(widget != RT_NULL);
  176. if (rect != RT_NULL)
  177. {
  178. rect->x1 = rect->y1 = 0;
  179. rect->x2 = widget->extent.x2 - widget->extent.x1;
  180. rect->y2 = widget->extent.y2 - widget->extent.y1;
  181. }
  182. }
  183. RTM_EXPORT(rtgui_widget_get_rect);
  184. /**
  185. * set widget draw style
  186. */
  187. void rtgui_widget_set_border(rtgui_widget_t* widget, rt_uint32_t style)
  188. {
  189. RT_ASSERT(widget != RT_NULL);
  190. widget->border_style = style;
  191. switch(style)
  192. {
  193. case RTGUI_BORDER_NONE:
  194. widget->border = 0;
  195. break;
  196. case RTGUI_BORDER_SIMPLE:
  197. case RTGUI_BORDER_UP:
  198. case RTGUI_BORDER_DOWN:
  199. widget->border = 1;
  200. break;
  201. case RTGUI_BORDER_STATIC:
  202. case RTGUI_BORDER_RAISE:
  203. case RTGUI_BORDER_SUNKEN:
  204. case RTGUI_BORDER_BOX:
  205. case RTGUI_BORDER_EXTRA:
  206. widget->border = 2;
  207. break;
  208. default:
  209. widget->border = 2;
  210. break;
  211. }
  212. }
  213. RTM_EXPORT(rtgui_widget_set_border);
  214. void rtgui_widget_set_onfocus(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  215. {
  216. RT_ASSERT(widget != RT_NULL);
  217. widget->on_focus_in = handler;
  218. }
  219. RTM_EXPORT(rtgui_widget_set_onfocus);
  220. void rtgui_widget_set_onunfocus(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  221. {
  222. RT_ASSERT(widget != RT_NULL);
  223. widget->on_focus_out = handler;
  224. }
  225. RTM_EXPORT(rtgui_widget_set_onunfocus);
  226. void rtgui_widget_set_onshow(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  227. {
  228. RT_ASSERT(widget != RT_NULL);
  229. widget->on_show = handler;
  230. }
  231. RTM_EXPORT(rtgui_widget_set_onshow);
  232. void rtgui_widget_set_onhide(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  233. {
  234. RT_ASSERT(widget != RT_NULL);
  235. widget->on_hide = handler;
  236. }
  237. RTM_EXPORT(rtgui_widget_set_onhide);
  238. #ifndef RTGUI_USING_SMALL_SIZE
  239. void rtgui_widget_set_ondraw(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  240. {
  241. RT_ASSERT(widget != RT_NULL);
  242. widget->on_draw = handler;
  243. }
  244. RTM_EXPORT(rtgui_widget_set_ondraw);
  245. void rtgui_widget_set_onmouseclick(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  246. {
  247. RT_ASSERT(widget != RT_NULL);
  248. widget->on_mouseclick = handler;
  249. }
  250. RTM_EXPORT(rtgui_widget_set_onmouseclick);
  251. void rtgui_widget_set_onkey(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  252. {
  253. RT_ASSERT(widget != RT_NULL);
  254. widget->on_key = handler;
  255. }
  256. RTM_EXPORT(rtgui_widget_set_onkey);
  257. void rtgui_widget_set_onsize(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  258. {
  259. RT_ASSERT(widget != RT_NULL);
  260. widget->on_size = handler;
  261. }
  262. RTM_EXPORT(rtgui_widget_set_onsize);
  263. void rtgui_widget_set_oncommand(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  264. {
  265. RT_ASSERT(widget != RT_NULL);
  266. widget->on_command = handler;
  267. }
  268. RTM_EXPORT(rtgui_widget_set_oncommand);
  269. #endif
  270. /**
  271. * @brief Focuses the widget. The focused widget is the widget which can receive the keyboard events
  272. * @param widget a widget
  273. * @note The widget has to be attached to a toplevel widget, otherwise it will have no effect
  274. */
  275. void rtgui_widget_focus(rtgui_widget_t *widget)
  276. {
  277. struct rtgui_widget *old_focus;
  278. RT_ASSERT(widget != RT_NULL);
  279. if (!RTGUI_WIDGET_IS_FOCUSABLE(widget) || !RTGUI_WIDGET_IS_ENABLE(widget))
  280. return;
  281. old_focus = RTGUI_WIN(widget->toplevel)->focused_widget;
  282. if (old_focus == widget)
  283. return; /* it's the same focused widget */
  284. /* unfocused the old widget */
  285. if (old_focus != RT_NULL)
  286. rtgui_widget_unfocus(old_focus);
  287. /* set widget as focused */
  288. widget->flag |= RTGUI_WIDGET_FLAG_FOCUS;
  289. RTGUI_WIN(widget->toplevel)->focused_widget = widget;
  290. /* invoke on focus in call back */
  291. if (widget->on_focus_in != RT_NULL)
  292. widget->on_focus_in(RTGUI_OBJECT(widget), RT_NULL);
  293. }
  294. RTM_EXPORT(rtgui_widget_focus);
  295. /**
  296. * @brief Unfocused the widget
  297. * @param widget a widget
  298. */
  299. void rtgui_widget_unfocus(rtgui_widget_t *widget)
  300. {
  301. RT_ASSERT(widget != RT_NULL);
  302. if (!widget->toplevel || !RTGUI_WIDGET_IS_FOCUSED(widget))
  303. return;
  304. widget->flag &= ~RTGUI_WIDGET_FLAG_FOCUS;
  305. if (widget->on_focus_out != RT_NULL)
  306. widget->on_focus_out(RTGUI_OBJECT(widget), RT_NULL);
  307. RTGUI_WIN(widget->toplevel)->focused_widget = RT_NULL;
  308. /* refresh widget */
  309. rtgui_widget_update(widget);
  310. }
  311. RTM_EXPORT(rtgui_widget_unfocus);
  312. void rtgui_widget_point_to_device(rtgui_widget_t* widget, rtgui_point_t* point)
  313. {
  314. RT_ASSERT(widget != RT_NULL);
  315. if (point != RT_NULL)
  316. {
  317. point->x += widget->extent.x1;
  318. point->y += widget->extent.y1;
  319. }
  320. }
  321. RTM_EXPORT(rtgui_widget_point_to_device);
  322. void rtgui_widget_rect_to_device(rtgui_widget_t* widget, rtgui_rect_t* rect)
  323. {
  324. RT_ASSERT(widget != RT_NULL);
  325. if (rect != RT_NULL)
  326. {
  327. rect->x1 += widget->extent.x1;
  328. rect->x2 += widget->extent.x1;
  329. rect->y1 += widget->extent.y1;
  330. rect->y2 += widget->extent.y1;
  331. }
  332. }
  333. RTM_EXPORT(rtgui_widget_rect_to_device);
  334. void rtgui_widget_point_to_logic(rtgui_widget_t* widget, rtgui_point_t* point)
  335. {
  336. RT_ASSERT(widget != RT_NULL);
  337. if (point != RT_NULL)
  338. {
  339. point->x -= widget->extent.x1;
  340. point->y -= widget->extent.y1;
  341. }
  342. }
  343. RTM_EXPORT(rtgui_widget_point_to_logic);
  344. void rtgui_widget_rect_to_logic(rtgui_widget_t* widget, rtgui_rect_t* rect)
  345. {
  346. RT_ASSERT(widget != RT_NULL);
  347. if (rect != RT_NULL)
  348. {
  349. rect->x1 -= widget->extent.x1;
  350. rect->x2 -= widget->extent.x1;
  351. rect->y1 -= widget->extent.y1;
  352. rect->y2 -= widget->extent.y1;
  353. }
  354. }
  355. RTM_EXPORT(rtgui_widget_rect_to_logic);
  356. struct rtgui_win* rtgui_widget_get_toplevel(rtgui_widget_t* widget)
  357. {
  358. rtgui_widget_t* r;
  359. RT_ASSERT(widget != RT_NULL);
  360. if (widget->toplevel)
  361. return widget->toplevel;
  362. rt_kprintf("widget->toplevel not properly set\n");
  363. r = widget;
  364. /* get the toplevel widget */
  365. while (r->parent != RT_NULL)
  366. r = r->parent;
  367. /* set toplevel */
  368. widget->toplevel = RTGUI_WIN(r);
  369. return RTGUI_WIN(r);
  370. }
  371. RTM_EXPORT(rtgui_widget_get_toplevel);
  372. rt_bool_t rtgui_widget_onupdate_toplvl(struct rtgui_object *object, struct rtgui_event *event)
  373. {
  374. struct rtgui_widget *widget;
  375. struct rtgui_event_update_toplvl *eup;
  376. RT_ASSERT(object);
  377. RT_ASSERT(event);
  378. widget = RTGUI_WIDGET(object);
  379. eup = (struct rtgui_event_update_toplvl*)event;
  380. widget->toplevel = eup->toplvl;
  381. return RT_FALSE;
  382. }
  383. RTM_EXPORT(rtgui_widget_onupdate_toplvl);
  384. rt_bool_t rtgui_widget_event_handler(struct rtgui_object* object, rtgui_event_t* event)
  385. {
  386. RTGUI_WIDGET_EVENT_HANDLER_PREPARE;
  387. switch (event->type)
  388. {
  389. case RTGUI_EVENT_SHOW:
  390. return rtgui_widget_onshow(object, event);
  391. case RTGUI_EVENT_HIDE:
  392. return rtgui_widget_onhide(object, event);
  393. case RTGUI_EVENT_UPDATE_TOPLVL:
  394. return rtgui_widget_onupdate_toplvl(object, event);
  395. #ifndef RTGUI_USING_SMALL_SIZE
  396. case RTGUI_EVENT_PAINT:
  397. if (widget->on_draw != RT_NULL)
  398. return widget->on_draw(RTGUI_OBJECT(widget), event);
  399. break;
  400. case RTGUI_EVENT_KBD:
  401. if (widget->on_key != RT_NULL)
  402. return widget->on_key(RTGUI_OBJECT(widget), event);
  403. break;
  404. case RTGUI_EVENT_MOUSE_BUTTON:
  405. if (widget->on_mouseclick != RT_NULL)
  406. return widget->on_mouseclick(RTGUI_OBJECT(widget), event);
  407. break;
  408. case RTGUI_EVENT_COMMAND:
  409. if (widget->on_command != RT_NULL)
  410. return widget->on_command(RTGUI_OBJECT(widget), event);
  411. break;
  412. case RTGUI_EVENT_RESIZE:
  413. if (widget->on_size != RT_NULL)
  414. return widget->on_size(RTGUI_OBJECT(widget), event);
  415. break;
  416. #endif
  417. }
  418. return rtgui_object_event_handler(object, event);
  419. }
  420. RTM_EXPORT(rtgui_widget_event_handler);
  421. /*
  422. * This function updates the clip info of widget
  423. */
  424. void rtgui_widget_update_clip(rtgui_widget_t* widget)
  425. {
  426. struct rtgui_list_node* node;
  427. rtgui_widget_t *parent;
  428. /* no widget or widget is hide, no update clip */
  429. if (widget == RT_NULL || RTGUI_WIDGET_IS_HIDE(widget)) return;
  430. parent = widget->parent;
  431. /* if there is no parent, there is no clip to update. */
  432. if (parent == RT_NULL)
  433. {
  434. return;
  435. }
  436. /* reset clip to extent */
  437. rtgui_region_reset(&(widget->clip), &(widget->extent));
  438. /* limit widget extent in parent extent */
  439. rtgui_region_intersect(&(widget->clip), &(widget->clip), &(parent->clip));
  440. /* get the no transparent parent */
  441. while (parent != RT_NULL && parent->flag & RTGUI_WIDGET_FLAG_TRANSPARENT)
  442. {
  443. parent = parent->parent;
  444. }
  445. if (parent != RT_NULL)
  446. {
  447. /* subtract widget clip in parent clip */
  448. if (!(widget->flag & RTGUI_WIDGET_FLAG_TRANSPARENT))
  449. {
  450. rtgui_region_subtract_rect(&(parent->clip), &(parent->clip),
  451. &(widget->extent));
  452. }
  453. }
  454. /*
  455. * note: since the layout widget introduction, the sibling widget will not
  456. * intersect.
  457. */
  458. /* if it's a view object, update the clip info of children */
  459. if (RTGUI_IS_CONTAINER(widget))
  460. {
  461. rtgui_widget_t* child;
  462. rtgui_list_foreach(node, &(RTGUI_CONTAINER(widget)->children))
  463. {
  464. child = rtgui_list_entry(node, rtgui_widget_t, sibling);
  465. rtgui_widget_update_clip(child);
  466. }
  467. }
  468. else if (RTGUI_IS_NOTEBOOK(widget))
  469. {
  470. rtgui_widget_update_clip(rtgui_notebook_get_current(RTGUI_NOTEBOOK(widget)));
  471. }
  472. }
  473. RTM_EXPORT(rtgui_widget_update_clip);
  474. void rtgui_widget_show(struct rtgui_widget *widget)
  475. {
  476. struct rtgui_event_show eshow;
  477. RT_ASSERT(widget != RT_NULL);
  478. if (!RTGUI_WIDGET_IS_HIDE(widget))
  479. return;
  480. RTGUI_EVENT_SHOW_INIT(&eshow);
  481. if (RTGUI_OBJECT(widget)->event_handler != RT_NULL)
  482. {
  483. RTGUI_OBJECT(widget)->event_handler(
  484. RTGUI_OBJECT(widget),
  485. &eshow);
  486. }
  487. }
  488. RTM_EXPORT(rtgui_widget_show);
  489. rt_bool_t rtgui_widget_onshow(struct rtgui_object *object, struct rtgui_event *event)
  490. {
  491. struct rtgui_widget *widget = RTGUI_WIDGET(object);
  492. if (!RTGUI_WIDGET_IS_HIDE(object))
  493. return RT_FALSE;
  494. RTGUI_WIDGET_UNHIDE(widget);
  495. if (widget->on_show != RT_NULL)
  496. widget->on_show(RTGUI_OBJECT(widget), RT_NULL);
  497. return RT_FALSE;
  498. }
  499. RTM_EXPORT(rtgui_widget_onshow);
  500. rt_bool_t rtgui_widget_onhide(struct rtgui_object *object, struct rtgui_event *event)
  501. {
  502. struct rtgui_widget *widget = RTGUI_WIDGET(object);
  503. if (RTGUI_WIDGET_IS_HIDE(object))
  504. return RT_FALSE;
  505. /* hide this widget */
  506. RTGUI_WIDGET_HIDE(widget);
  507. if (widget->parent != RT_NULL)
  508. {
  509. rtgui_widget_t *parent;
  510. parent = widget->parent;
  511. /* get the no transparent parent */
  512. while (parent != RT_NULL && parent->flag & RTGUI_WIDGET_FLAG_TRANSPARENT)
  513. {
  514. parent = parent->parent;
  515. }
  516. /* union widget rect */
  517. rtgui_region_union_rect(&(parent->clip), &(parent->clip), &(widget->extent));
  518. }
  519. if (widget->on_hide != RT_NULL)
  520. widget->on_hide(RTGUI_OBJECT(widget), RT_NULL);
  521. return RT_FALSE;
  522. }
  523. RTM_EXPORT(rtgui_widget_onhide);
  524. void rtgui_widget_hide(struct rtgui_widget *widget)
  525. {
  526. struct rtgui_event_hide ehide;
  527. RT_ASSERT(widget != RT_NULL);
  528. if (RTGUI_WIDGET_IS_HIDE(widget))
  529. return;
  530. RTGUI_EVENT_HIDE_INIT(&ehide);
  531. if (RTGUI_OBJECT(widget)->event_handler != RT_NULL)
  532. {
  533. RTGUI_OBJECT(widget)->event_handler(
  534. RTGUI_OBJECT(widget),
  535. &ehide);
  536. }
  537. }
  538. RTM_EXPORT(rtgui_widget_hide);
  539. rtgui_color_t rtgui_widget_get_parent_foreground(rtgui_widget_t* widget)
  540. {
  541. rtgui_widget_t* parent;
  542. /* get parent widget */
  543. parent = widget->parent;
  544. while (parent->parent != RT_NULL && (RTGUI_WIDGET_FLAG(parent) & RTGUI_WIDGET_FLAG_TRANSPARENT))
  545. parent = parent->parent;
  546. /* get parent's color */
  547. if (parent != RT_NULL)
  548. return RTGUI_WIDGET_FOREGROUND(parent);
  549. return RTGUI_WIDGET_FOREGROUND(widget);
  550. }
  551. RTM_EXPORT(rtgui_widget_get_parent_foreground);
  552. rtgui_color_t rtgui_widget_get_parent_background(rtgui_widget_t* widget)
  553. {
  554. rtgui_widget_t* parent;
  555. /* get parent widget */
  556. parent = widget->parent;
  557. while (parent->parent != RT_NULL && (RTGUI_WIDGET_FLAG(parent) & RTGUI_WIDGET_FLAG_TRANSPARENT))
  558. parent = parent->parent;
  559. /* get parent's color */
  560. if (parent != RT_NULL)
  561. return RTGUI_WIDGET_BACKGROUND(parent);
  562. return RTGUI_WIDGET_BACKGROUND(widget);
  563. }
  564. RTM_EXPORT(rtgui_widget_get_parent_background);
  565. void rtgui_widget_update(rtgui_widget_t* widget)
  566. {
  567. struct rtgui_event_paint paint;
  568. RTGUI_EVENT_PAINT_INIT(&paint);
  569. paint.wid = RT_NULL;
  570. RT_ASSERT(widget != RT_NULL);
  571. if (RTGUI_WIDGET_IS_HIDE(widget))
  572. return;
  573. if (RTGUI_OBJECT(widget)->event_handler != RT_NULL)
  574. {
  575. RTGUI_OBJECT(widget)->event_handler(
  576. RTGUI_OBJECT(widget),
  577. &paint.parent);
  578. }
  579. }
  580. RTM_EXPORT(rtgui_widget_update);
  581. rtgui_widget_t* rtgui_widget_get_next_sibling(rtgui_widget_t* widget)
  582. {
  583. rtgui_widget_t* sibling = RT_NULL;
  584. if (widget->sibling.next != RT_NULL)
  585. {
  586. sibling = rtgui_list_entry(widget->sibling.next, rtgui_widget_t, sibling);
  587. }
  588. return sibling;
  589. }
  590. RTM_EXPORT(rtgui_widget_get_next_sibling);
  591. rtgui_widget_t* rtgui_widget_get_prev_sibling(rtgui_widget_t* widget)
  592. {
  593. struct rtgui_list_node* node;
  594. rtgui_widget_t *sibling, *parent;
  595. node = RT_NULL; sibling = RT_NULL;
  596. parent = widget->parent;
  597. if (parent != RT_NULL)
  598. {
  599. rtgui_list_foreach(node, &(RTGUI_CONTAINER(parent)->children))
  600. {
  601. if (node->next == &(widget->sibling))
  602. break;
  603. }
  604. }
  605. if (node != RT_NULL)
  606. sibling = rtgui_list_entry(node, rtgui_widget_t, sibling);
  607. return sibling;
  608. }
  609. RTM_EXPORT(rtgui_widget_get_prev_sibling);
  610. #ifdef RTGUI_WIDGET_DEBUG
  611. #include <rtgui/widgets/label.h>
  612. #include <rtgui/widgets/button.h>
  613. void rtgui_widget_dump(rtgui_widget_t* widget)
  614. {
  615. struct rtgui_object *obj;
  616. obj = RTGUI_OBJECT(widget);
  617. rt_kprintf("widget type: %s ", obj->type->name);
  618. if (RTGUI_IS_WIN(widget) == RT_TRUE)
  619. rt_kprintf(":%s ", RTGUI_WIN(widget)->title);
  620. else if ((RTGUI_IS_LABEL(widget) == RT_TRUE) || (RTGUI_IS_BUTTON(widget) == RT_TRUE))
  621. rt_kprintf(":%s ", RTGUI_LABEL(widget)->text);
  622. rt_kprintf("extent(%d, %d) - (%d, %d)\n", widget->extent.x1,
  623. widget->extent.y1, widget->extent.x2, widget->extent.y2);
  624. // rtgui_region_dump(&(widget->clip));
  625. }
  626. #endif