Blender  V2.59
ScrollBar.c
Go to the documentation of this file.
00001 
00029 #include <stdlib.h>
00030 
00031 #include <math.h>
00032 
00033 #include "MEM_guardedalloc.h"
00034 
00035 #include "Basic.h"
00036 #include "ScrollBar.h"
00037 
00038 struct _ScrollBar {
00039         int             rect[2][2];
00040         float   thumbpos, thumbpct;
00041         
00042         int             inset;
00043         int             minthumb;
00044 
00045         int             scrolling;
00046         float   scrolloffs;
00047 };
00048 
00049 static int scrollbar_get_thumbH(ScrollBar *sb) {
00050         int scrollable_h= rect_height(sb->rect) - 2*sb->inset;
00051         
00052         return clamp_i(sb->thumbpct*scrollable_h, sb->minthumb, scrollable_h);
00053 }
00054 static int scrollbar_get_thumbableH(ScrollBar *sb) {
00055         int scrollable_h= rect_height(sb->rect) - 2*sb->inset;
00056         int thumb_h= scrollbar_get_thumbH(sb);
00057         
00058         return scrollable_h - thumb_h;
00059 }
00060 
00061 static float scrollbar_co_to_pos(ScrollBar *sb, int yco) {
00062         int thumb_h= scrollbar_get_thumbH(sb);
00063         int thumbable_h= scrollbar_get_thumbableH(sb);
00064         int thumbable_y= (sb->rect[0][1]+sb->inset) + thumb_h/2;
00065 
00066         return (float) (yco-thumbable_y)/thumbable_h;
00067 }
00068 
00069 
00070 
00071 ScrollBar *scrollbar_new(int inset, int minthumb) {
00072         ScrollBar *sb= MEM_callocN(sizeof(*sb), "scrollbar_new");
00073         sb->inset= inset;
00074         sb->minthumb= minthumb;
00075         
00076         return sb;
00077 }
00078 
00079 void scrollbar_get_thumb(ScrollBar *sb, int thumb_r[2][2]) {
00080         int thumb_h= scrollbar_get_thumbH(sb);
00081         int thumbable_h= scrollbar_get_thumbableH(sb);
00082 
00083         thumb_r[0][0]= sb->rect[0][0]+sb->inset;
00084         thumb_r[1][0]= sb->rect[1][0]-sb->inset;
00085 
00086         thumb_r[0][1]= sb->rect[0][1]+sb->inset + sb->thumbpos*thumbable_h;
00087         thumb_r[1][1]= thumb_r[0][1] + thumb_h;
00088 }
00089 
00090 int scrollbar_is_scrolling(ScrollBar *sb) {
00091         return sb->scrolling;
00092 }
00093 int scrollbar_contains_pt(ScrollBar *sb, int pt[2]) {
00094         return rect_contains_pt(sb->rect, pt);
00095 }
00096 
00097 void scrollbar_start_scrolling(ScrollBar *sb, int yco) {
00098         int thumb_h_2= scrollbar_get_thumbH(sb)/2;
00099         int thumbable_h= scrollbar_get_thumbableH(sb);
00100         float npos= scrollbar_co_to_pos(sb, yco);
00101 
00102         sb->scrolloffs= sb->thumbpos - npos;
00103         if (fabs(sb->scrolloffs) >= (float) thumb_h_2/thumbable_h) {
00104                 sb->scrolloffs= 0.0;
00105         }
00106 
00107         sb->scrolling= 1;
00108         sb->thumbpos= clamp_f(npos + sb->scrolloffs, 0.0, 1.0);
00109 }
00110 void scrollbar_keep_scrolling(ScrollBar *sb, int yco) {
00111         float npos= scrollbar_co_to_pos(sb, yco);
00112 
00113         sb->thumbpos= clamp_f(npos + sb->scrolloffs, 0.0, 1.0);
00114 }
00115 void scrollbar_stop_scrolling(ScrollBar *sb) {
00116         sb->scrolling= 0;
00117         sb->scrolloffs= 0.0;
00118 }
00119 
00120 void scrollbar_set_thumbpct(ScrollBar *sb, float pct) {
00121         sb->thumbpct= pct;
00122 }
00123 void scrollbar_set_thumbpos(ScrollBar *sb, float pos) {
00124         sb->thumbpos= clamp_f(pos, 0.0, 1.0);
00125 }
00126 void scrollbar_set_rect(ScrollBar *sb, int rect[2][2]) {
00127         rect_copy(sb->rect, rect);
00128 }
00129 
00130 float scrollbar_get_thumbpct(ScrollBar *sb) {
00131         return sb->thumbpct;
00132 }
00133 float scrollbar_get_thumbpos(ScrollBar *sb) {
00134         return sb->thumbpos;
00135 }
00136 void scrollbar_get_rect(ScrollBar *sb, int rect_r[2][2]) {
00137         rect_copy(rect_r, sb->rect);
00138 }
00139 
00140 void scrollbar_free(ScrollBar *sb) {
00141         MEM_freeN(sb);
00142 }