widget.c 14 KB

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