widget.c 17 KB

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