widget.c 20 KB

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