Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ScrollBar.c « multitest « test « ghost « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cd690f6f14e67d2ad6552a064c370512984f3efe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
 * $Id$
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version. The Blender
 * Foundation also sells licenses for use in proprietary software under
 * the Blender License.  See http://www.blender.org/BL/ for information
 * about this.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): none yet.
 *
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
 */

#include <stdlib.h>

#include <math.h>

#include "MEM_guardedalloc.h"

#include "Basic.h"
#include "ScrollBar.h"

struct _ScrollBar {
	int		rect[2][2];
	float	thumbpos, thumbpct;
	
	int		inset;
	int		minthumb;

	int		scrolling;
	float	scrolloffs;
};

static int scrollbar_get_thumbH(ScrollBar *sb) {
	int scrollable_h= rect_height(sb->rect) - 2*sb->inset;
	
	return clamp_i(sb->thumbpct*scrollable_h, sb->minthumb, scrollable_h);
}
static int scrollbar_get_thumbableH(ScrollBar *sb) {
	int scrollable_h= rect_height(sb->rect) - 2*sb->inset;
	int thumb_h= scrollbar_get_thumbH(sb);
	
	return scrollable_h - thumb_h;
}

static float scrollbar_co_to_pos(ScrollBar *sb, int yco) {
	int thumb_h= scrollbar_get_thumbH(sb);
	int thumbable_h= scrollbar_get_thumbableH(sb);
	int thumbable_y= (sb->rect[0][1]+sb->inset) + thumb_h/2;

	return (float) (yco-thumbable_y)/thumbable_h;
}

/**/

ScrollBar *scrollbar_new(int inset, int minthumb) {
	ScrollBar *sb= MEM_callocN(sizeof(*sb), "scrollbar_new");
	sb->inset= inset;
	sb->minthumb= minthumb;
	
	return sb;
}

void scrollbar_get_thumb(ScrollBar *sb, int thumb_r[2][2]) {
	int thumb_h= scrollbar_get_thumbH(sb);
	int thumbable_h= scrollbar_get_thumbableH(sb);

	thumb_r[0][0]= sb->rect[0][0]+sb->inset;
	thumb_r[1][0]= sb->rect[1][0]-sb->inset;

	thumb_r[0][1]= sb->rect[0][1]+sb->inset + sb->thumbpos*thumbable_h;
	thumb_r[1][1]= thumb_r[0][1] + thumb_h;
}

int scrollbar_is_scrolling(ScrollBar *sb) {
	return sb->scrolling;
}
int scrollbar_contains_pt(ScrollBar *sb, int pt[2]) {
	return rect_contains_pt(sb->rect, pt);
}

void scrollbar_start_scrolling(ScrollBar *sb, int yco) {
	int thumb_h_2= scrollbar_get_thumbH(sb)/2;
	int thumbable_h= scrollbar_get_thumbableH(sb);
	float npos= scrollbar_co_to_pos(sb, yco);

	sb->scrolloffs= sb->thumbpos - npos;
	if (fabs(sb->scrolloffs) >= (float) thumb_h_2/thumbable_h) {
		sb->scrolloffs= 0.0;
	}

	sb->scrolling= 1;
	sb->thumbpos= clamp_f(npos + sb->scrolloffs, 0.0, 1.0);
}
void scrollbar_keep_scrolling(ScrollBar *sb, int yco) {
	float npos= scrollbar_co_to_pos(sb, yco);

	sb->thumbpos= clamp_f(npos + sb->scrolloffs, 0.0, 1.0);
}
void scrollbar_stop_scrolling(ScrollBar *sb) {
	sb->scrolling= 0;
	sb->scrolloffs= 0.0;
}

void scrollbar_set_thumbpct(ScrollBar *sb, float pct) {
	sb->thumbpct= pct;
}
void scrollbar_set_thumbpos(ScrollBar *sb, float pos) {
	sb->thumbpos= clamp_f(pos, 0.0, 1.0);
}
void scrollbar_set_rect(ScrollBar *sb, int rect[2][2]) {
	rect_copy(sb->rect, rect);
}

float scrollbar_get_thumbpct(ScrollBar *sb) {
	return sb->thumbpct;
}
float scrollbar_get_thumbpos(ScrollBar *sb) {
	return sb->thumbpos;
}
void scrollbar_get_rect(ScrollBar *sb, int rect_r[2][2]) {
	rect_copy(rect_r, sb->rect);
}

void scrollbar_free(ScrollBar *sb) {
	MEM_freeN(sb);
}