scrollbar.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * File : scrollbar.c
  3. * This file is part of 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. * 2010-08-09 Bernard first version
  13. */
  14. #include <rtgui/dc.h>
  15. #include <rtgui/rtgui_theme.h>
  16. #include <rtgui/widgets/scrollbar.h>
  17. static void _rtgui_scrollbar_constructor(rtgui_scrollbar_t *bar)
  18. {
  19. struct rtgui_rect rect = {0, 0, RTGUI_DEFAULT_SB_WIDTH, RTGUI_DEFAULT_SB_HEIGHT};
  20. /* set event handler */
  21. rtgui_widget_set_event_handler(RTGUI_WIDGET(bar), rtgui_scrollbar_event_handler);
  22. rtgui_scrollbar_set_range(bar, 0, 100);
  23. rtgui_scrollbar_set_page_step(bar, 20);
  24. rtgui_scrollbar_set_line_step(bar, 10);
  25. bar->status = 0;
  26. bar->thumb_position = 0;
  27. bar->thumb_size = 16;
  28. bar->on_scroll = RT_NULL;
  29. bar->orient = RTGUI_HORIZONTAL;
  30. rtgui_widget_set_rect(RTGUI_WIDGET(bar), &rect);
  31. /* set gc */
  32. RTGUI_WIDGET_TEXTALIGN(RTGUI_WIDGET(bar)) = RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL;
  33. }
  34. rt_inline rt_uint32_t _rtgui_scrollbar_get_length(rtgui_scrollbar_t *bar)
  35. {
  36. struct rtgui_rect rect;
  37. rtgui_widget_get_rect(RTGUI_WIDGET(bar), &rect);
  38. if (bar->orient & RTGUI_VERTICAL)
  39. return rect.y2 - 2 * (rect.x2 - rect.x1);
  40. return rect.x2 - 2 * (rect.y2 - rect.y1);
  41. }
  42. rt_inline rt_uint32_t _rtgui_scrollbar_get_thumb_position(rtgui_scrollbar_t* bar)
  43. {
  44. rt_uint32_t thumb_position;
  45. /* calculate thumb position */
  46. thumb_position = (rtgui_scrollbar_get_value(bar) - bar->min_position) * _rtgui_scrollbar_get_length(bar) /
  47. (bar->max_position - bar->min_position);
  48. return thumb_position;
  49. }
  50. void rtgui_scrollbar_get_thumb_rect(rtgui_scrollbar_t *bar, rtgui_rect_t *rect)
  51. {
  52. struct rtgui_rect scrollbar_rect;
  53. rtgui_widget_get_rect(RTGUI_WIDGET(bar), &scrollbar_rect);
  54. if (bar->orient & RTGUI_VERTICAL)
  55. {
  56. rt_uint32_t btn_width = scrollbar_rect.x2 - scrollbar_rect.x1;
  57. /* vertical scroll bar */
  58. rect->x1 = scrollbar_rect.x1;
  59. rect->x2 = scrollbar_rect.x2 - 1;
  60. rect->y1 = scrollbar_rect.y1 + btn_width + _rtgui_scrollbar_get_thumb_position(bar);
  61. rect->y2 = rect->y1 + btn_width;
  62. }
  63. else
  64. {
  65. rt_uint32_t btn_height = scrollbar_rect.y2 - scrollbar_rect.y1;
  66. /* horizontal scroll bar */
  67. rect->x1 = scrollbar_rect.x1 + btn_height + _rtgui_scrollbar_get_thumb_position(bar);
  68. rect->x2 = rect->x1 + btn_height;
  69. rect->y1 = scrollbar_rect.y1;
  70. rect->y2 = scrollbar_rect.y2 - 1;
  71. }
  72. }
  73. DEFINE_CLASS_TYPE(scrollbar, "scrollbar",
  74. RTGUI_WIDGET_TYPE,
  75. _rtgui_scrollbar_constructor,
  76. RT_NULL,
  77. sizeof(struct rtgui_scrollbar));
  78. static void _rtgui_scrollbar_on_mouseclick(struct rtgui_widget * widget, struct rtgui_event * event)
  79. {
  80. rtgui_rect_t btn_rect, bar_rect;
  81. rt_uint32_t thumb_size, thumb_position;
  82. struct rtgui_scrollbar* bar = (struct rtgui_scrollbar*)widget;
  83. struct rtgui_event_mouse* mouse = (struct rtgui_event_mouse*)event;
  84. /* get the thumb size and position */
  85. thumb_size = bar->thumb_size * (bar->max_position - bar->min_position) / _rtgui_scrollbar_get_length(bar);
  86. thumb_position = _rtgui_scrollbar_get_thumb_position(bar);
  87. if (bar->orient == RTGUI_VERTICAL)
  88. {
  89. /* get up arrow button rect */
  90. btn_rect.x1 = widget->extent.x1;
  91. btn_rect.x2 = widget->extent.x2;
  92. btn_rect.y1 = widget->extent.y1;
  93. btn_rect.y2 = widget->extent.y1 + (widget->extent.x2 - widget->extent.x1);
  94. if (rtgui_rect_contains_point(&btn_rect, mouse->x, mouse->y) == RT_EOK)
  95. {
  96. if ((mouse->button & (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN)) ==
  97. (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN))
  98. {
  99. bar->status |= SBS_UPARROW;
  100. /* line step */
  101. bar->thumb_position -= bar->line_step;
  102. if (bar->thumb_position < bar->min_position) bar->thumb_position = bar->min_position;
  103. }
  104. else if (mouse->button & RTGUI_MOUSE_BUTTON_UP)
  105. {
  106. bar->status = 0;
  107. }
  108. goto __exit;
  109. }
  110. /* get bar rect */
  111. bar_rect.x1 = widget->extent.x1;
  112. bar_rect.x2 = widget->extent.x2;
  113. bar_rect.y1 = widget->extent.y1 + (widget->extent.x2 - widget->extent.x1);
  114. bar_rect.y2 = widget->extent.y2 - (widget->extent.x2 - widget->extent.x1);
  115. if (rtgui_rect_contains_point(&bar_rect, mouse->x, mouse->y) == RT_EOK)
  116. {
  117. if ((mouse->button & (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN)) ==
  118. (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN))
  119. {
  120. /* page step */
  121. if (mouse->y < bar_rect.y1 + thumb_position)
  122. {
  123. bar->thumb_position -= bar->page_step;
  124. if (bar->thumb_position < bar->min_position)
  125. bar->thumb_position = bar->min_position;
  126. }
  127. else if (mouse->y > thumb_position + bar->thumb_size)
  128. {
  129. bar->thumb_position += bar->page_step;
  130. if (bar->thumb_position > bar->max_position - thumb_size)
  131. bar->thumb_position = bar->max_position - thumb_size;
  132. }
  133. }
  134. goto __exit;
  135. }
  136. /* get down arrow button rect */
  137. btn_rect.y1 = widget->extent.y2 - (widget->extent.x2 - widget->extent.x1);
  138. btn_rect.y2 = widget->extent.y2;
  139. bar_rect.y1 = widget->extent.y1 + ((widget->extent.y2 - widget->extent.y1)/2);
  140. bar_rect.y2 = widget->extent.y2 - (widget->extent.x2 - widget->extent.x1);
  141. if (rtgui_rect_contains_point(&btn_rect, mouse->x, mouse->y) == RT_EOK)
  142. {
  143. if ((mouse->button & (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN)) ==
  144. (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN))
  145. {
  146. bar->status |= SBS_DOWNARROW;
  147. /* line step */
  148. bar->thumb_position += bar->line_step;
  149. if (bar->thumb_position > bar->max_position - thumb_size)
  150. bar->thumb_position = bar->max_position - thumb_size;
  151. }
  152. else if (mouse->button & RTGUI_MOUSE_BUTTON_UP)
  153. bar->status = 0;
  154. }
  155. }
  156. else
  157. {
  158. /* get left arrow button rect */
  159. btn_rect.x1 = widget->extent.x1;
  160. btn_rect.x2 = widget->extent.x1 + (widget->extent.y2 - widget->extent.y1);
  161. btn_rect.y1 = widget->extent.y1;
  162. btn_rect.y2 = widget->extent.y2;
  163. if (rtgui_rect_contains_point(&btn_rect, mouse->x, mouse->y) == RT_EOK)
  164. {
  165. if ((mouse->button & (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN)) ==
  166. (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN))
  167. {
  168. bar->status |= SBS_LEFTARROW;
  169. /* line step */
  170. bar->thumb_position -= bar->line_step;
  171. if (bar->thumb_position < bar->min_position) bar->thumb_position = bar->min_position;
  172. }
  173. else if (mouse->button & RTGUI_MOUSE_BUTTON_UP)
  174. bar->status = 0;
  175. goto __exit;
  176. }
  177. /* get bar rect */
  178. bar_rect.x1 = widget->extent.x1 + (widget->extent.y2 - widget->extent.y1);
  179. bar_rect.x2 = widget->extent.x2 - (widget->extent.y2 - widget->extent.y1);
  180. bar_rect.y1 = widget->extent.y1;
  181. bar_rect.y2 = widget->extent.y2;
  182. if (rtgui_rect_contains_point(&bar_rect, mouse->x, mouse->y) == RT_EOK)
  183. {
  184. if ((mouse->button & (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN)) ==
  185. (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN))
  186. {
  187. /* page step */
  188. if (mouse->x < bar_rect.x1 + thumb_position)
  189. {
  190. bar->thumb_position -= bar->page_step;
  191. if (bar->thumb_position < bar->min_position)
  192. bar->thumb_position = bar->min_position;
  193. }
  194. else if (mouse->x > thumb_position + bar->thumb_size)
  195. {
  196. bar->thumb_position += bar->page_step;
  197. if (bar->thumb_position > bar->max_position - thumb_size)
  198. bar->thumb_position = bar->max_position - thumb_size;
  199. }
  200. }
  201. goto __exit;
  202. }
  203. /* get right arrow button rect */
  204. btn_rect.x1 = widget->extent.x2 - (widget->extent.y2 - widget->extent.y1);
  205. btn_rect.x2 = widget->extent.x2;
  206. bar_rect.x1 = widget->extent.x1 + ((widget->extent.x2 - widget->extent.x1)/2);
  207. bar_rect.x2 = widget->extent.x2 - (widget->extent.y2 - widget->extent.y1);
  208. if (rtgui_rect_contains_point(&btn_rect,
  209. mouse->x, mouse->y) == RT_EOK)
  210. {
  211. if ((mouse->button & (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN)) ==
  212. (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN))
  213. {
  214. bar->status |= SBS_RIGHTARROW;
  215. /* line step */
  216. bar->thumb_position += bar->line_step;
  217. if (bar->thumb_position > bar->max_position - bar->line_step)
  218. bar->thumb_position = bar->max_position - bar->line_step;
  219. }
  220. else if (mouse->button & RTGUI_MOUSE_BUTTON_UP)
  221. bar->status = 0;
  222. }
  223. }
  224. __exit:
  225. rtgui_theme_draw_scrollbar(bar);
  226. if ((mouse->button & (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN)) ==
  227. (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN))
  228. {
  229. if (bar->on_scroll != RT_NULL) bar->on_scroll(widget, RT_NULL);
  230. }
  231. }
  232. rt_bool_t rtgui_scrollbar_event_handler(struct rtgui_widget * widget,
  233. struct rtgui_event * event)
  234. {
  235. struct rtgui_scrollbar* bar = (struct rtgui_scrollbar*)widget;
  236. switch (event->type)
  237. {
  238. case RTGUI_EVENT_PAINT:
  239. #ifndef RTGUI_USING_SMALL_SIZE
  240. if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
  241. else
  242. #endif
  243. {
  244. rtgui_theme_draw_scrollbar(bar);
  245. }
  246. break;
  247. case RTGUI_EVENT_MOUSE_BUTTON:
  248. if (RTGUI_WIDGET_IS_ENABLE(widget) && !RTGUI_WIDGET_IS_HIDE(widget))
  249. {
  250. #ifndef RTGUI_USING_SMALL_SIZE
  251. if (widget->on_mouseclick != RT_NULL)
  252. {
  253. widget->on_mouseclick(widget, event);
  254. }
  255. else
  256. #endif
  257. {
  258. _rtgui_scrollbar_on_mouseclick(widget, event);
  259. }
  260. }
  261. break;
  262. default:
  263. break;
  264. }
  265. return RT_FALSE;
  266. }
  267. struct rtgui_scrollbar* rtgui_scrollbar_create(int orient, rtgui_rect_t* r)
  268. {
  269. struct rtgui_scrollbar* bar;
  270. bar = (struct rtgui_scrollbar*) rtgui_widget_create (RTGUI_SCROLLBAR_TYPE);
  271. if (bar != RT_NULL)
  272. {
  273. if (r != RT_NULL)
  274. {
  275. rtgui_widget_set_rect(RTGUI_WIDGET(bar), r);
  276. if (orient == RTGUI_VERTICAL)
  277. bar->thumb_size = (r->x2 - r->x1);
  278. else
  279. bar->thumb_size = (r->y2 - r->y1);
  280. }
  281. bar->orient = orient;
  282. }
  283. return bar;
  284. }
  285. void rtgui_scrollbar_destroy(struct rtgui_scrollbar* bar)
  286. {
  287. rtgui_widget_destroy(RTGUI_WIDGET(bar));
  288. }
  289. void rtgui_scrollbar_set_orientation(rtgui_scrollbar_t* bar, int orientation)
  290. {
  291. RT_ASSERT(bar != RT_NULL);
  292. bar->orient = orientation;
  293. #ifndef RTGUI_USING_SMALL_SIZE
  294. if (bar->orient == RTGUI_HORIZONTAL)
  295. {
  296. /* horizontal */
  297. rtgui_widget_set_miniwidth(RTGUI_WIDGET(bar), RTGUI_DEFAULT_SB_WIDTH);
  298. rtgui_widget_set_miniheight(RTGUI_WIDGET(bar), RTGUI_DEFAULT_SB_HEIGHT);
  299. }
  300. else
  301. {
  302. /* vertical */
  303. rtgui_widget_set_miniwidth(RTGUI_WIDGET(bar), RTGUI_DEFAULT_SB_HEIGHT);
  304. rtgui_widget_set_miniheight(RTGUI_WIDGET(bar), RTGUI_DEFAULT_SB_WIDTH);
  305. }
  306. #endif
  307. }
  308. void rtgui_scrollbar_set_range(struct rtgui_scrollbar* bar, int min, int max)
  309. {
  310. RT_ASSERT(bar != RT_NULL);
  311. if (min >= max )
  312. {
  313. RTGUI_WIDGET_DISABLE(RTGUI_WIDGET(bar));
  314. return;
  315. }
  316. bar->min_position = (rt_int16_t)min;
  317. bar->max_position = (rt_int16_t)max;
  318. }
  319. void rtgui_scrollbar_set_page_step(struct rtgui_scrollbar* bar, int step)
  320. {
  321. RT_ASSERT(bar != RT_NULL);
  322. bar->page_step = step;
  323. /* disable or enable scrollbar */
  324. if (bar->page_step > (bar->max_position - bar->min_position))
  325. {
  326. /* disable bar */
  327. RTGUI_WIDGET_DISABLE(RTGUI_WIDGET(bar));
  328. }
  329. else
  330. {
  331. /* enable bar */
  332. RTGUI_WIDGET_ENABLE(RTGUI_WIDGET(bar));
  333. }
  334. }
  335. void rtgui_scrollbar_set_line_step(struct rtgui_scrollbar* bar, int step)
  336. {
  337. RT_ASSERT(bar != RT_NULL);
  338. bar->line_step = step;
  339. }
  340. rt_int16_t rtgui_scrollbar_get_value(struct rtgui_scrollbar* bar)
  341. {
  342. RT_ASSERT(bar != RT_NULL);
  343. return bar->thumb_position;
  344. }
  345. void rtgui_scrollbar_set_value(struct rtgui_scrollbar* bar, rt_int16_t position)
  346. {
  347. RT_ASSERT(bar != RT_NULL);
  348. bar->thumb_position = position;
  349. rtgui_widget_update(RTGUI_WIDGET(bar));
  350. }
  351. void rtgui_scrollbar_set_onscroll(struct rtgui_scrollbar* bar,
  352. rtgui_event_handler_ptr handler)
  353. {
  354. if (bar == RT_NULL || handler == RT_NULL) return;
  355. bar->on_scroll = handler;
  356. }