Blender  V2.93
ScrollBar.c
Go to the documentation of this file.
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 
20 #include <stdlib.h>
21 
22 #include <math.h>
23 
24 #include "MEM_guardedalloc.h"
25 
26 #include "Basic.h"
27 #include "ScrollBar.h"
28 
29 struct _ScrollBar {
30  int rect[2][2];
32 
33  int inset;
34  int minthumb;
35 
36  int scrolling;
37  float scrolloffs;
38 };
39 
41 {
42  int scrollable_h = rect_height(sb->rect) - 2 * sb->inset;
43 
44  return clamp_i(sb->thumbpct * scrollable_h, sb->minthumb, scrollable_h);
45 }
46 
48 {
49  int scrollable_h = rect_height(sb->rect) - 2 * sb->inset;
50  int thumb_h = scrollbar_get_thumbH(sb);
51 
52  return scrollable_h - thumb_h;
53 }
54 
55 static float scrollbar_co_to_pos(ScrollBar *sb, int yco)
56 {
57  int thumb_h = scrollbar_get_thumbH(sb);
58  int thumbable_h = scrollbar_get_thumbableH(sb);
59  int thumbable_y = (sb->rect[0][1] + sb->inset) + thumb_h / 2;
60 
61  return (float)(yco - thumbable_y) / thumbable_h;
62 }
63 
64 
65 
66 ScrollBar *scrollbar_new(int inset, int minthumb)
67 {
68  ScrollBar *sb = MEM_callocN(sizeof(*sb), "scrollbar_new");
69  sb->inset = inset;
70  sb->minthumb = minthumb;
71 
72  return sb;
73 }
74 
75 void scrollbar_get_thumb(ScrollBar *sb, int thumb_r[2][2])
76 {
77  int thumb_h = scrollbar_get_thumbH(sb);
78  int thumbable_h = scrollbar_get_thumbableH(sb);
79 
80  thumb_r[0][0] = sb->rect[0][0] + sb->inset;
81  thumb_r[1][0] = sb->rect[1][0] - sb->inset;
82 
83  thumb_r[0][1] = sb->rect[0][1] + sb->inset + sb->thumbpos * thumbable_h;
84  thumb_r[1][1] = thumb_r[0][1] + thumb_h;
85 }
86 
88 {
89  return sb->scrolling;
90 }
91 int scrollbar_contains_pt(ScrollBar *sb, int pt[2])
92 {
93  return rect_contains_pt(sb->rect, pt);
94 }
95 
97 {
98  int thumb_h_2 = scrollbar_get_thumbH(sb) / 2;
99  int thumbable_h = scrollbar_get_thumbableH(sb);
100  float npos = scrollbar_co_to_pos(sb, yco);
101 
102  sb->scrolloffs = sb->thumbpos - npos;
103  if (fabs(sb->scrolloffs) >= (float)thumb_h_2 / thumbable_h) {
104  sb->scrolloffs = 0.0;
105  }
106 
107  sb->scrolling = 1;
108  sb->thumbpos = clamp_f(npos + sb->scrolloffs, 0.0, 1.0);
109 }
111 {
112  float npos = scrollbar_co_to_pos(sb, yco);
113 
114  sb->thumbpos = clamp_f(npos + sb->scrolloffs, 0.0, 1.0);
115 }
117 {
118  sb->scrolling = 0;
119  sb->scrolloffs = 0.0;
120 }
121 
122 void scrollbar_set_thumbpct(ScrollBar *sb, float pct)
123 {
124  sb->thumbpct = pct;
125 }
127 {
128  sb->thumbpos = clamp_f(pos, 0.0, 1.0);
129 }
130 void scrollbar_set_rect(ScrollBar *sb, int rect[2][2])
131 {
132  rect_copy(sb->rect, rect);
133 }
134 
136 {
137  return sb->thumbpct;
138 }
140 {
141  return sb->thumbpos;
142 }
143 void scrollbar_get_rect(ScrollBar *sb, int rect_r[2][2])
144 {
145  rect_copy(rect_r, sb->rect);
146 }
147 
149 {
150  MEM_freeN(sb);
151 }
MINLINE float clamp_f(float value, float min, float max)
MINLINE int clamp_i(int value, int min, int max)
int rect_contains_pt(int rect[2][2], int pt[2])
Definition: Basic.c:53
int rect_height(int rect[2][2])
Definition: Basic.c:62
void rect_copy(int dst[2][2], int src[2][2])
Definition: Basic.c:48
Read Guarded memory(de)allocation.
static int scrollbar_get_thumbH(ScrollBar *sb)
Definition: ScrollBar.c:40
void scrollbar_keep_scrolling(ScrollBar *sb, int yco)
Definition: ScrollBar.c:110
void scrollbar_start_scrolling(ScrollBar *sb, int yco)
Definition: ScrollBar.c:96
int scrollbar_is_scrolling(ScrollBar *sb)
Definition: ScrollBar.c:87
static int scrollbar_get_thumbableH(ScrollBar *sb)
Definition: ScrollBar.c:47
ScrollBar * scrollbar_new(int inset, int minthumb)
Definition: ScrollBar.c:66
float scrollbar_get_thumbpos(ScrollBar *sb)
Definition: ScrollBar.c:139
static float scrollbar_co_to_pos(ScrollBar *sb, int yco)
Definition: ScrollBar.c:55
void scrollbar_get_rect(ScrollBar *sb, int rect_r[2][2])
Definition: ScrollBar.c:143
void scrollbar_get_thumb(ScrollBar *sb, int thumb_r[2][2])
Definition: ScrollBar.c:75
void scrollbar_set_rect(ScrollBar *sb, int rect[2][2])
Definition: ScrollBar.c:130
float scrollbar_get_thumbpct(ScrollBar *sb)
Definition: ScrollBar.c:135
void scrollbar_free(ScrollBar *sb)
Definition: ScrollBar.c:148
int scrollbar_contains_pt(ScrollBar *sb, int pt[2])
Definition: ScrollBar.c:91
void scrollbar_set_thumbpct(ScrollBar *sb, float pct)
Definition: ScrollBar.c:122
void scrollbar_stop_scrolling(ScrollBar *sb)
Definition: ScrollBar.c:116
void scrollbar_set_thumbpos(ScrollBar *sb, float pos)
Definition: ScrollBar.c:126
uint pos
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
int scrolling
Definition: ScrollBar.c:36
int inset
Definition: ScrollBar.c:33
float thumbpos
Definition: ScrollBar.c:31
int minthumb
Definition: ScrollBar.c:34
float thumbpct
Definition: ScrollBar.c:31
float scrolloffs
Definition: ScrollBar.c:37
int rect[2][2]
Definition: ScrollBar.c:30
ccl_device_inline float2 fabs(const float2 &a)