/* ----------------------------------------------------------------------------
 * Moves the scrollbar's button to the desired coordinates.
 * The coordinates specify the center.
 */
void scrollbar::move_button(int x, int y) {
    if(low_value == high_value) return;
    
    button* but = (button*) widgets["but_bar"];
    
    if(vertical) {
        int bh = but->y2 - but->y1;
        int h = y2 - y1;
        
        but->y1 = y - (but->y2 - but->y1) / 2;
        if(but->y1 < y1) but->y1 = y1;
        if(but->y1 + bh > y2) but->y1 = y2 - bh;
        
        but->y2 = but->y1 + bh;
        
        low_value =  min_value + ((but->y1 - y1) / (float) h) * (max_value - min_value);
        high_value = min_value + ((but->y2 - y1) / (float) h) * (max_value - min_value);
    } else {
        int bw = but->x2 - but->x1;
        int w = x2 - x1;
        
        but->x1 = x - (but->x2 - but->x1) / 2;
        if(but->x1 < x1) but->x1 = x1;
        if(but->x1 + bw > x2) but->x1 = x2 - bw;
        
        but->x2 = but->x1 + bw;
        
        low_value =  min_value + ((but->x1 - x1) / (float) w) * (max_value - min_value);
        high_value = min_value + ((but->x2 - x1) / (float) w) * (max_value - min_value);
    }
    
    if(change_handler) change_handler(this);
}
/* ----------------------------------------------------------------------------
 * Sets the scrollbar's current value.
 */
void scrollbar::set_value(float new_low, bool call_handler) {
    float dif = high_value - low_value;
    if(new_low < min_value || new_low + dif > max_value) return;
    
    button* but = (button*) widgets["but_bar"];
    if(!but) return;
    
    low_value = new_low;
    high_value = new_low + dif;
    
    float ratio = (low_value - min_value) / (max_value - min_value - dif);
    
    if(vertical) {
        int but_h = but->y2 - but->y1;
        but->y1 = y1 + ratio * ((y2 - y1) - but_h);
        but->y2 = but->y1 + but_h;
    } else {
        int but_w = but->x2 - but->x1;
        but->x1 = x1 + ratio * ((x2 - x1) - but_w);
        but->x2 = but->x1 + but_w;
    }
    
    if(change_handler && call_handler) change_handler(this);
}
예제 #3
0
/* ----------------------------------------------------------------------------
 * Calls the function that handles a change of the text.
 */
void textbox::call_change_handler() { if(change_handler) change_handler(this); }