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