widget.c 18 KB

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