blender/intern/ghost/test/multitest/ScrollBar.c

160 lines
3.7 KiB
C
Raw Normal View History

2002-10-12 11:37:38 +00:00
/**
* ***** BEGIN GPL LICENSE BLOCK *****
2002-10-12 11:37:38 +00:00
*
* 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.
2002-10-12 11:37:38 +00:00
*
* 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,
2010-02-12 13:34:04 +00:00
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2002-10-12 11:37:38 +00:00
*
* 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 LICENSE BLOCK *****
2002-10-12 11:37:38 +00:00
*/
#include <stdlib.h>
#include <math.h>
#include "MEM_guardedalloc.h"
#include "Basic.h"
#include "ScrollBar.h"
struct _ScrollBar {
2012-05-24 13:18:53 +00:00
int rect[2][2];
float thumbpos, thumbpct;
2002-10-12 11:37:38 +00:00
2012-05-24 13:18:53 +00:00
int inset;
int minthumb;
2002-10-12 11:37:38 +00:00
2012-05-24 13:18:53 +00:00
int scrolling;
float scrolloffs;
2002-10-12 11:37:38 +00:00
};
2012-05-24 13:18:53 +00:00
static int scrollbar_get_thumbH(ScrollBar *sb)
{
int scrollable_h = rect_height(sb->rect) - 2 * sb->inset;
2002-10-12 11:37:38 +00:00
2012-05-24 13:18:53 +00:00
return clamp_i(sb->thumbpct * scrollable_h, sb->minthumb, scrollable_h);
2002-10-12 11:37:38 +00:00
}
2012-05-24 13:18:53 +00:00
static int scrollbar_get_thumbableH(ScrollBar *sb)
{
int scrollable_h = rect_height(sb->rect) - 2 * sb->inset;
int thumb_h = scrollbar_get_thumbH(sb);
2002-10-12 11:37:38 +00:00
return scrollable_h - thumb_h;
}
2012-05-24 13:18:53 +00:00
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;
2002-10-12 11:37:38 +00:00
2012-05-24 13:18:53 +00:00
return (float) (yco - thumbable_y) / thumbable_h;
2002-10-12 11:37:38 +00:00
}
/**/
2012-05-24 13:18:53 +00:00
ScrollBar *scrollbar_new(int inset, int minthumb)
{
ScrollBar *sb = MEM_callocN(sizeof(*sb), "scrollbar_new");
sb->inset = inset;
sb->minthumb = minthumb;
2002-10-12 11:37:38 +00:00
return sb;
}
2012-05-24 13:18:53 +00:00
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);
2002-10-12 11:37:38 +00:00
2012-05-24 13:18:53 +00:00
thumb_r[0][0] = sb->rect[0][0] + sb->inset;
thumb_r[1][0] = sb->rect[1][0] - sb->inset;
2002-10-12 11:37:38 +00:00
2012-05-24 13:18:53 +00:00
thumb_r[0][1] = sb->rect[0][1] + sb->inset + sb->thumbpos * thumbable_h;
thumb_r[1][1] = thumb_r[0][1] + thumb_h;
2002-10-12 11:37:38 +00:00
}
2012-05-24 13:18:53 +00:00
int scrollbar_is_scrolling(ScrollBar *sb)
{
2002-10-12 11:37:38 +00:00
return sb->scrolling;
}
2012-05-27 00:36:50 +00:00
int scrollbar_contains_pt(ScrollBar *sb, int pt[2])
{
2002-10-12 11:37:38 +00:00
return rect_contains_pt(sb->rect, pt);
}
2012-05-24 13:18:53 +00:00
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);
2002-10-12 11:37:38 +00:00
2012-05-24 13:18:53 +00:00
sb->scrolloffs = sb->thumbpos - npos;
if (fabs(sb->scrolloffs) >= (float) thumb_h_2 / thumbable_h) {
sb->scrolloffs = 0.0;
2002-10-12 11:37:38 +00:00
}
2012-05-24 13:18:53 +00:00
sb->scrolling = 1;
sb->thumbpos = clamp_f(npos + sb->scrolloffs, 0.0, 1.0);
2002-10-12 11:37:38 +00:00
}
2012-05-24 13:18:53 +00:00
void scrollbar_keep_scrolling(ScrollBar *sb, int yco)
{
float npos = scrollbar_co_to_pos(sb, yco);
2002-10-12 11:37:38 +00:00
2012-05-24 13:18:53 +00:00
sb->thumbpos = clamp_f(npos + sb->scrolloffs, 0.0, 1.0);
2002-10-12 11:37:38 +00:00
}
2012-05-24 13:18:53 +00:00
void scrollbar_stop_scrolling(ScrollBar *sb)
{
sb->scrolling = 0;
sb->scrolloffs = 0.0;
2002-10-12 11:37:38 +00:00
}
2012-05-24 13:18:53 +00:00
void scrollbar_set_thumbpct(ScrollBar *sb, float pct)
{
sb->thumbpct = pct;
2002-10-12 11:37:38 +00:00
}
2012-05-24 13:18:53 +00:00
void scrollbar_set_thumbpos(ScrollBar *sb, float pos)
{
sb->thumbpos = clamp_f(pos, 0.0, 1.0);
2002-10-12 11:37:38 +00:00
}
2012-05-24 13:18:53 +00:00
void scrollbar_set_rect(ScrollBar *sb, int rect[2][2])
{
2002-10-12 11:37:38 +00:00
rect_copy(sb->rect, rect);
}
2012-05-24 13:18:53 +00:00
float scrollbar_get_thumbpct(ScrollBar *sb)
{
2002-10-12 11:37:38 +00:00
return sb->thumbpct;
}
2012-05-24 13:18:53 +00:00
float scrollbar_get_thumbpos(ScrollBar *sb)
{
2002-10-12 11:37:38 +00:00
return sb->thumbpos;
}
2012-05-24 13:18:53 +00:00
void scrollbar_get_rect(ScrollBar *sb, int rect_r[2][2])
{
2002-10-12 11:37:38 +00:00
rect_copy(rect_r, sb->rect);
}
2012-05-24 13:18:53 +00:00
void scrollbar_free(ScrollBar *sb)
{
2002-10-12 11:37:38 +00:00
MEM_freeN(sb);
}