Blender V2.61 - r43446

ScrollBar.c

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