widget.c 15 KB

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