widget.c 15 KB

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