ScrollBar.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License
  4. * as published by the Free Software Foundation; either version 2
  5. * of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software Foundation,
  14. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. *
  16. * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  17. * All rights reserved.
  18. */
  19. #include <stdlib.h>
  20. #include <math.h>
  21. #include "MEM_guardedalloc.h"
  22. #include "Basic.h"
  23. #include "ScrollBar.h"
  24. struct _ScrollBar {
  25. int rect[2][2];
  26. float thumbpos, thumbpct;
  27. int inset;
  28. int minthumb;
  29. int scrolling;
  30. float scrolloffs;
  31. };
  32. static int scrollbar_get_thumbH(ScrollBar *sb)
  33. {
  34. int scrollable_h = rect_height(sb->rect) - 2 * sb->inset;
  35. return clamp_i(sb->thumbpct * scrollable_h, sb->minthumb, scrollable_h);
  36. }
  37. static int scrollbar_get_thumbableH(ScrollBar *sb)
  38. {
  39. int scrollable_h = rect_height(sb->rect) - 2 * sb->inset;
  40. int thumb_h = scrollbar_get_thumbH(sb);
  41. return scrollable_h - thumb_h;
  42. }
  43. static float scrollbar_co_to_pos(ScrollBar *sb, int yco)
  44. {
  45. int thumb_h = scrollbar_get_thumbH(sb);
  46. int thumbable_h = scrollbar_get_thumbableH(sb);
  47. int thumbable_y = (sb->rect[0][1] + sb->inset) + thumb_h / 2;
  48. return (float)(yco - thumbable_y) / thumbable_h;
  49. }
  50. /**/
  51. ScrollBar *scrollbar_new(int inset, int minthumb)
  52. {
  53. ScrollBar *sb = MEM_callocN(sizeof(*sb), "scrollbar_new");
  54. sb->inset = inset;
  55. sb->minthumb = minthumb;
  56. return sb;
  57. }
  58. void scrollbar_get_thumb(ScrollBar *sb, int thumb_r[2][2])
  59. {
  60. int thumb_h = scrollbar_get_thumbH(sb);
  61. int thumbable_h = scrollbar_get_thumbableH(sb);
  62. thumb_r[0][0] = sb->rect[0][0] + sb->inset;
  63. thumb_r[1][0] = sb->rect[1][0] - sb->inset;
  64. thumb_r[0][1] = sb->rect[0][1] + sb->inset + sb->thumbpos * thumbable_h;
  65. thumb_r[1][1] = thumb_r[0][1] + thumb_h;
  66. }
  67. int scrollbar_is_scrolling(ScrollBar *sb)
  68. {
  69. return sb->scrolling;
  70. }
  71. int scrollbar_contains_pt(ScrollBar *sb, int pt[2])
  72. {
  73. return rect_contains_pt(sb->rect, pt);
  74. }
  75. void scrollbar_start_scrolling(ScrollBar *sb, int yco)
  76. {
  77. int thumb_h_2 = scrollbar_get_thumbH(sb) / 2;
  78. int thumbable_h = scrollbar_get_thumbableH(sb);
  79. float npos = scrollbar_co_to_pos(sb, yco);
  80. sb->scrolloffs = sb->thumbpos - npos;
  81. if (fabs(sb->scrolloffs) >= (float)thumb_h_2 / thumbable_h) {
  82. sb->scrolloffs = 0.0;
  83. }
  84. sb->scrolling = 1;
  85. sb->thumbpos = clamp_f(npos + sb->scrolloffs, 0.0, 1.0);
  86. }
  87. void scrollbar_keep_scrolling(ScrollBar *sb, int yco)
  88. {
  89. float npos = scrollbar_co_to_pos(sb, yco);
  90. sb->thumbpos = clamp_f(npos + sb->scrolloffs, 0.0, 1.0);
  91. }
  92. void scrollbar_stop_scrolling(ScrollBar *sb)
  93. {
  94. sb->scrolling = 0;
  95. sb->scrolloffs = 0.0;
  96. }
  97. void scrollbar_set_thumbpct(ScrollBar *sb, float pct)
  98. {
  99. sb->thumbpct = pct;
  100. }
  101. void scrollbar_set_thumbpos(ScrollBar *sb, float pos)
  102. {
  103. sb->thumbpos = clamp_f(pos, 0.0, 1.0);
  104. }
  105. void scrollbar_set_rect(ScrollBar *sb, int rect[2][2])
  106. {
  107. rect_copy(sb->rect, rect);
  108. }
  109. float scrollbar_get_thumbpct(ScrollBar *sb)
  110. {
  111. return sb->thumbpct;
  112. }
  113. float scrollbar_get_thumbpos(ScrollBar *sb)
  114. {
  115. return sb->thumbpos;
  116. }
  117. void scrollbar_get_rect(ScrollBar *sb, int rect_r[2][2])
  118. {
  119. rect_copy(rect_r, sb->rect);
  120. }
  121. void scrollbar_free(ScrollBar *sb)
  122. {
  123. MEM_freeN(sb);
  124. }