widget.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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/widgets/widget.h>
  17. #include <rtgui/widgets/window.h>
  18. #include <rtgui/widgets/view.h>
  19. static void _rtgui_widget_constructor(rtgui_widget_t *widget)
  20. {
  21. if (!widget) return;
  22. /* set default flag */
  23. widget->flag = RTGUI_WIDGET_FLAG_DEFAULT;
  24. /* init list */
  25. rtgui_list_init(&(widget->sibling));
  26. /* init gc */
  27. widget->gc.foreground = default_foreground;
  28. widget->gc.background = default_background;
  29. widget->gc.font = rtgui_font_default();
  30. widget->gc.textstyle = RTGUI_TEXTSTYLE_NORMAL;
  31. widget->gc.textalign = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_TOP;
  32. #ifndef RTGUI_USING_SMALL_SIZE
  33. widget->align = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_TOP;
  34. #endif
  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. #ifndef RTGUI_USING_SMALL_SIZE
  42. widget->on_draw = RT_NULL;
  43. widget->on_mouseclick = RT_NULL;
  44. widget->on_key = RT_NULL;
  45. widget->on_size = RT_NULL;
  46. widget->on_command = RT_NULL;
  47. #endif
  48. /* set default event handler */
  49. widget->event_handler = rtgui_widget_event_handler;
  50. /* init user data private to 0 */
  51. widget->user_data = 0;
  52. /* init clip information */
  53. rtgui_region_init(&(widget->clip));
  54. /* init hardware dc */
  55. rtgui_dc_client_init(widget);
  56. }
  57. /* Destroys the widget */
  58. static void _rtgui_widget_destructor(rtgui_widget_t *widget)
  59. {
  60. if (widget == RT_NULL) return;
  61. if (widget->parent != RT_NULL)
  62. {
  63. /* remove widget from parent's children list */
  64. rtgui_list_remove(&(RTGUI_CONTAINER(widget->parent)->children), &(widget->sibling));
  65. widget->parent = RT_NULL;
  66. }
  67. /* fini clip region */
  68. rtgui_region_fini(&(widget->clip));
  69. }
  70. rtgui_type_t *rtgui_widget_type_get(void)
  71. {
  72. static rtgui_type_t *widget_type = RT_NULL;
  73. if (!widget_type)
  74. {
  75. widget_type = rtgui_type_create("rtgui_widget", RTGUI_OBJECT_TYPE,
  76. sizeof(rtgui_widget_t), RTGUI_CONSTRUCTOR(_rtgui_widget_constructor),
  77. RTGUI_DESTRUCTOR(_rtgui_widget_destructor));
  78. }
  79. return widget_type;
  80. }
  81. rtgui_widget_t *rtgui_widget_create(rtgui_type_t *widget_type)
  82. {
  83. struct rtgui_widget* widget;
  84. widget = RTGUI_WIDGET(rtgui_object_create(widget_type));
  85. return widget;
  86. }
  87. void rtgui_widget_destroy(rtgui_widget_t* widget)
  88. {
  89. rtgui_object_destroy(RTGUI_OBJECT(widget));
  90. }
  91. void rtgui_widget_set_rect(rtgui_widget_t* widget, const rtgui_rect_t* rect)
  92. {
  93. if (widget == RT_NULL || rect == RT_NULL) return;
  94. widget->extent = *rect;
  95. #ifndef RTGUI_USING_SMALL_SIZE
  96. /* reset mini width and height */
  97. widget->mini_width = rtgui_rect_width(widget->extent);
  98. widget->mini_height = rtgui_rect_height(widget->extent);
  99. #endif
  100. /* it's not empty, fini it */
  101. if (rtgui_region_not_empty(&(widget->clip)))
  102. {
  103. rtgui_region_fini(&(widget->clip));
  104. }
  105. /* reset clip info */
  106. rtgui_region_init_with_extents(&(widget->clip), rect);
  107. if ((widget->parent != RT_NULL) && (widget->toplevel != RT_NULL))
  108. {
  109. /* update widget clip */
  110. rtgui_widget_update_clip(widget->parent);
  111. }
  112. }
  113. void rtgui_widget_set_parent(rtgui_widget_t* widget, rtgui_widget_t* parent)
  114. {
  115. /* set parent and toplevel widget */
  116. widget->parent = parent;
  117. /* update children toplevel */
  118. if (parent->toplevel != RT_NULL &&
  119. RTGUI_IS_TOPLEVEL(parent->toplevel))
  120. {
  121. widget->toplevel = rtgui_widget_get_toplevel(parent);
  122. }
  123. }
  124. void rtgui_widget_get_extent(rtgui_widget_t* widget, rtgui_rect_t *rect)
  125. {
  126. RT_ASSERT(widget != RT_NULL);
  127. RT_ASSERT(rect != RT_NULL);
  128. *rect = widget->extent;
  129. }
  130. #ifndef RTGUI_USING_SMALL_SIZE
  131. void rtgui_widget_set_miniwidth(rtgui_widget_t* widget, int width)
  132. {
  133. RT_ASSERT(widget != RT_NULL);
  134. widget->mini_width = width;
  135. }
  136. void rtgui_widget_set_miniheight(rtgui_widget_t* widget, int height)
  137. {
  138. RT_ASSERT(widget != RT_NULL);
  139. widget->mini_height = height;
  140. }
  141. #endif
  142. /*
  143. * This function moves widget and its children to a logic point
  144. */
  145. void rtgui_widget_move_to_logic(rtgui_widget_t* widget, int dx, int dy)
  146. {
  147. struct rtgui_list_node* node;
  148. rtgui_widget_t* child;
  149. if (widget == RT_NULL) return;
  150. rtgui_rect_moveto(&(widget->extent), dx, dy);
  151. /* move each child */
  152. if (RTGUI_IS_CONTAINER(widget))
  153. {
  154. rtgui_list_foreach(node, &(RTGUI_CONTAINER(widget)->children))
  155. {
  156. child = rtgui_list_entry(node, rtgui_widget_t, sibling);
  157. rtgui_widget_move_to_logic(child, dx, dy);
  158. }
  159. }
  160. }
  161. void rtgui_widget_set_event_handler(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  162. {
  163. RT_ASSERT(widget != RT_NULL);
  164. widget->event_handler = handler;
  165. }
  166. void rtgui_widget_get_rect(rtgui_widget_t* widget, rtgui_rect_t *rect)
  167. {
  168. RT_ASSERT(widget != RT_NULL);
  169. if (rect != RT_NULL)
  170. {
  171. rect->x1 = rect->y1 = 0;
  172. rect->x2 = widget->extent.x2 - widget->extent.x1;
  173. rect->y2 = widget->extent.y2 - widget->extent.y1;
  174. }
  175. }
  176. void rtgui_widget_set_onfocus(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  177. {
  178. RT_ASSERT(widget != RT_NULL);
  179. widget->on_focus_in = handler;
  180. }
  181. void rtgui_widget_set_onunfocus(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  182. {
  183. RT_ASSERT(widget != RT_NULL);
  184. widget->on_focus_out = handler;
  185. }
  186. #ifndef RTGUI_USING_SMALL_SIZE
  187. void rtgui_widget_set_ondraw(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  188. {
  189. RT_ASSERT(widget != RT_NULL);
  190. widget->on_draw = handler;
  191. }
  192. void rtgui_widget_set_onmouseclick(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  193. {
  194. RT_ASSERT(widget != RT_NULL);
  195. widget->on_mouseclick = handler;
  196. }
  197. void rtgui_widget_set_onkey(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  198. {
  199. RT_ASSERT(widget != RT_NULL);
  200. widget->on_key = handler;
  201. }
  202. void rtgui_widget_set_onsize(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  203. {
  204. RT_ASSERT(widget != RT_NULL);
  205. widget->on_size = handler;
  206. }
  207. void rtgui_widget_set_oncommand(rtgui_widget_t* widget, rtgui_event_handler_ptr handler)
  208. {
  209. RT_ASSERT(widget != RT_NULL);
  210. widget->on_command = handler;
  211. }
  212. #endif
  213. /**
  214. * @brief Focuses the widget. The focused widget is the widget which can receive the keyboard events
  215. * @param widget a widget
  216. * @note The widget has to be attached to a toplevel widget, otherwise it will have no effect
  217. */
  218. void rtgui_widget_focus(rtgui_widget_t *widget)
  219. {
  220. rtgui_container_t *parent;
  221. RT_ASSERT(widget != RT_NULL);
  222. if (!widget->parent || !widget->toplevel) return;
  223. if (!RTGUI_WIDGET_IS_FOCUSABLE(widget) || !RTGUI_WIDGET_IS_ENABLE(widget))
  224. return;
  225. /* set widget as focused */
  226. widget->flag |= RTGUI_WIDGET_FLAG_FOCUS;
  227. /* get root parent container and old focused widget */
  228. parent = RTGUI_CONTAINER(widget->toplevel);
  229. if (parent->focused == widget) return ; /* it's the same focused widget */
  230. /* unfocused the old widget */
  231. if (parent->focused != RT_NULL) rtgui_widget_unfocus(parent->focused);
  232. /* set widget as focused widget in parent link */
  233. parent = RTGUI_CONTAINER(widget->parent);
  234. do
  235. {
  236. parent->focused = widget;
  237. parent = RTGUI_CONTAINER(RTGUI_WIDGET(parent)->parent);
  238. } while ((parent != RT_NULL) && !RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(parent)));
  239. /* invoke on focus in call back */
  240. if (widget->on_focus_in != RT_NULL)
  241. widget->on_focus_in(widget, RT_NULL);
  242. }
  243. /**
  244. * @brief Unfocused the widget
  245. * @param widget a widget
  246. */
  247. void rtgui_widget_unfocus(rtgui_widget_t *widget)
  248. {
  249. RT_ASSERT(widget != RT_NULL);
  250. if (!widget->toplevel || !RTGUI_WIDGET_IS_FOCUSED(widget))
  251. return;
  252. widget->flag &= ~RTGUI_WIDGET_FLAG_FOCUS;
  253. if (widget->on_focus_out != RT_NULL)
  254. widget->on_focus_out(widget, RT_NULL);
  255. /* refresh widget */
  256. rtgui_widget_update(widget);
  257. }
  258. void rtgui_widget_point_to_device(rtgui_widget_t* widget, rtgui_point_t* point)
  259. {
  260. RT_ASSERT(widget != RT_NULL);
  261. if (point != RT_NULL)
  262. {
  263. point->x += widget->extent.x1;
  264. point->y += widget->extent.y1;
  265. }
  266. }
  267. void rtgui_widget_rect_to_device(rtgui_widget_t* widget, rtgui_rect_t* rect)
  268. {
  269. RT_ASSERT(widget != RT_NULL);
  270. if (rect != RT_NULL)
  271. {
  272. rect->x1 += widget->extent.x1;
  273. rect->x2 += widget->extent.x1;
  274. rect->y1 += widget->extent.y1;
  275. rect->y2 += widget->extent.y1;
  276. }
  277. }
  278. void rtgui_widget_point_to_logic(rtgui_widget_t* widget, rtgui_point_t* point)
  279. {
  280. RT_ASSERT(widget != RT_NULL);
  281. if (point != RT_NULL)
  282. {
  283. point->x -= widget->extent.x1;
  284. point->y -= widget->extent.y1;
  285. }
  286. }
  287. void rtgui_widget_rect_to_logic(rtgui_widget_t* widget, rtgui_rect_t* rect)
  288. {
  289. RT_ASSERT(widget != RT_NULL);
  290. if (rect != RT_NULL)
  291. {
  292. rect->x1 -= widget->extent.x1;
  293. rect->x2 -= widget->extent.x1;
  294. rect->y1 -= widget->extent.y1;
  295. rect->y2 -= widget->extent.y1;
  296. }
  297. }
  298. rtgui_widget_t* rtgui_widget_get_toplevel(rtgui_widget_t* widget)
  299. {
  300. rtgui_widget_t* r;
  301. RT_ASSERT(widget != RT_NULL);
  302. if (widget->toplevel) return widget->toplevel;
  303. r = widget;
  304. /* get the toplevel widget */
  305. while (r->parent != RT_NULL) r = r->parent;
  306. /* set toplevel */
  307. widget->toplevel = r;
  308. return r;
  309. }
  310. rt_bool_t rtgui_widget_event_handler(rtgui_widget_t* widget, rtgui_event_t* event)
  311. {
  312. #ifndef RTGUI_USING_SMALL_SIZE
  313. switch (event->type)
  314. {
  315. case RTGUI_EVENT_PAINT:
  316. if (widget->on_draw != RT_NULL) return widget->on_draw(widget, event);
  317. break;
  318. case RTGUI_EVENT_KBD:
  319. if (widget->on_key != RT_NULL) return widget->on_key(widget, event);
  320. break;
  321. case RTGUI_EVENT_MOUSE_BUTTON:
  322. if (widget->on_mouseclick != RT_NULL) return widget->on_mouseclick(widget, event);
  323. break;
  324. case RTGUI_EVENT_COMMAND:
  325. if (widget->on_command != RT_NULL) return widget->on_command(widget, event);
  326. break;
  327. case RTGUI_EVENT_RESIZE:
  328. if (widget->on_size != RT_NULL) return widget->on_size(widget, event);
  329. break;
  330. }
  331. #endif
  332. return RT_FALSE;
  333. }
  334. /*
  335. * This function updates the clip info of widget
  336. */
  337. void rtgui_widget_update_clip(rtgui_widget_t* widget)
  338. {
  339. struct rtgui_list_node* node;
  340. rtgui_widget_t *parent;
  341. /* no widget or widget is hide, no update clip */
  342. if (widget == RT_NULL || RTGUI_WIDGET_IS_HIDE(widget)) return;
  343. parent = widget->parent;
  344. /* if there is no parent, do not update clip (please use toplevel widget API) */
  345. if (parent == RT_NULL)
  346. {
  347. if (RTGUI_IS_TOPLEVEL(widget))
  348. {
  349. /* if it's toplevel widget, update it by toplevel function */
  350. rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(widget));
  351. }
  352. return;
  353. }
  354. /* reset clip to extent */
  355. rtgui_region_reset(&(widget->clip), &(widget->extent));
  356. /* limit widget extent in parent extent */
  357. rtgui_region_intersect(&(widget->clip), &(widget->clip), &(parent->clip));
  358. /* get the no transparent parent */
  359. while (parent != RT_NULL && parent->flag & RTGUI_WIDGET_FLAG_TRANSPARENT)
  360. {
  361. parent = parent->parent;
  362. }
  363. if (parent != RT_NULL)
  364. {
  365. /* subtract widget clip in parent clip */
  366. if (!(widget->flag & RTGUI_WIDGET_FLAG_TRANSPARENT))
  367. {
  368. rtgui_region_subtract_rect(&(parent->clip), &(parent->clip),
  369. &(widget->extent));
  370. }
  371. }
  372. /*
  373. * note: since the layout widget introduction, the sibling widget will not
  374. * intersect.
  375. */
  376. /* if it's a container object, update the clip info of children */
  377. if (RTGUI_IS_CONTAINER(widget))
  378. {
  379. rtgui_widget_t* child;
  380. rtgui_list_foreach(node, &(RTGUI_CONTAINER(widget)->children))
  381. {
  382. child = rtgui_list_entry(node, rtgui_widget_t, sibling);
  383. rtgui_widget_update_clip(child);
  384. }
  385. }
  386. }
  387. void rtgui_widget_show(rtgui_widget_t* widget)
  388. {
  389. /* there is no parent or the parent is hide, no show at all */
  390. if (widget->parent == RT_NULL ||
  391. RTGUI_WIDGET_IS_HIDE(widget->parent)) return;
  392. /* update the clip info of widget */
  393. RTGUI_WIDGET_UNHIDE(widget);
  394. rtgui_widget_update_clip(widget);
  395. }
  396. void rtgui_widget_hide(rtgui_widget_t* widget)
  397. {
  398. rtgui_rect_t rect;
  399. /* hide this widget */
  400. RTGUI_WIDGET_HIDE(widget);
  401. if (widget->parent != RT_NULL)
  402. {
  403. int index;
  404. rtgui_widget_t *parent;
  405. rtgui_toplevel_t *toplevel;
  406. rect = widget->extent;
  407. parent = widget->parent;
  408. /* get the no transparent parent */
  409. while (parent != RT_NULL && parent->flag & RTGUI_WIDGET_FLAG_TRANSPARENT)
  410. {
  411. parent = parent->parent;
  412. }
  413. /* union widget rect */
  414. rtgui_region_union_rect(&(widget->parent->clip), &(widget->parent->clip), &rect);
  415. /* handle extern rect */
  416. toplevel = RTGUI_TOPLEVEL(widget->toplevel);
  417. /* subtract the external rect */
  418. for (index = 0; index < toplevel->external_clip_size; index ++)
  419. {
  420. rtgui_region_subtract_rect(&(widget->parent->clip), &(widget->parent->clip),
  421. &(toplevel->external_clip_rect[index]));
  422. }
  423. }
  424. }
  425. rtgui_color_t rtgui_widget_get_parent_foreground(rtgui_widget_t* widget)
  426. {
  427. rtgui_widget_t* parent;
  428. /* get parent widget */
  429. parent = widget->parent;
  430. while (parent->parent != RT_NULL && (RTGUI_WIDGET_FLAG(parent) & RTGUI_WIDGET_FLAG_TRANSPARENT))
  431. parent = parent->parent;
  432. /* get parent's color */
  433. if (parent != RT_NULL)
  434. return RTGUI_WIDGET_FOREGROUND(parent);
  435. return RTGUI_WIDGET_FOREGROUND(widget);
  436. }
  437. rtgui_color_t rtgui_widget_get_parent_background(rtgui_widget_t* widget)
  438. {
  439. rtgui_widget_t* parent;
  440. /* get parent widget */
  441. parent = widget->parent;
  442. while (parent->parent != RT_NULL && (RTGUI_WIDGET_FLAG(parent) & RTGUI_WIDGET_FLAG_TRANSPARENT))
  443. parent = parent->parent;
  444. /* get parent's color */
  445. if (parent != RT_NULL)
  446. return RTGUI_WIDGET_BACKGROUND(parent);
  447. return RTGUI_WIDGET_BACKGROUND(widget);
  448. }
  449. void rtgui_widget_update(rtgui_widget_t* widget)
  450. {
  451. struct rtgui_event_paint paint;
  452. RTGUI_EVENT_PAINT_INIT(&paint);
  453. paint.wid = RT_NULL;
  454. RT_ASSERT(widget != RT_NULL);
  455. if (widget->event_handler != RT_NULL)
  456. {
  457. widget->event_handler(widget, &paint.parent);
  458. }
  459. }
  460. rtgui_widget_t* rtgui_widget_get_next_sibling(rtgui_widget_t* widget)
  461. {
  462. rtgui_widget_t* sibling = RT_NULL;
  463. if (widget->sibling.next != RT_NULL)
  464. {
  465. sibling = rtgui_list_entry(widget->sibling.next, rtgui_widget_t, sibling);
  466. }
  467. return sibling;
  468. }
  469. rtgui_widget_t* rtgui_widget_get_prev_sibling(rtgui_widget_t* widget)
  470. {
  471. struct rtgui_list_node* node;
  472. rtgui_widget_t *sibling, *parent;
  473. node = RT_NULL; sibling = RT_NULL;
  474. parent = widget->parent;
  475. if (parent != RT_NULL)
  476. {
  477. rtgui_list_foreach(node, &(RTGUI_CONTAINER(parent)->children))
  478. {
  479. if (node->next == &(widget->sibling))
  480. break;
  481. }
  482. }
  483. if (node != RT_NULL)
  484. sibling = rtgui_list_entry(node, rtgui_widget_t, sibling);
  485. return sibling;
  486. }
  487. #ifdef RTGUI_WIDGET_DEBUG
  488. #include <rtgui/widgets/label.h>
  489. #include <rtgui/widgets/button.h>
  490. void rtgui_widget_dump(rtgui_widget_t* widget)
  491. {
  492. struct rtgui_object *obj;
  493. obj = RTGUI_OBJECT(widget);
  494. rt_kprintf("widget type: %s ", obj->type->name);
  495. if (RTGUI_IS_VIEW(widget) == RT_TRUE)
  496. rt_kprintf(":%s ", RTGUI_VIEW(widget)->title);
  497. if (RTGUI_IS_WIN(widget) == RT_TRUE)
  498. rt_kprintf(":%s ", RTGUI_WIN(widget)->title);
  499. if ((RTGUI_IS_LABEL(widget) == RT_TRUE) || (RTGUI_IS_BUTTON(widget) == RT_TRUE))
  500. rt_kprintf(":%s ", RTGUI_LABEL(widget)->text);
  501. rt_kprintf("extent(%d, %d) - (%d, %d)\n", widget->extent.x1,
  502. widget->extent.y1, widget->extent.x2, widget->extent.y2);
  503. // rtgui_region_dump(&(widget->clip));
  504. }
  505. #endif