void TBSlider::UpdateHandle() { // Calculate the handle position bool horizontal = m_axis == AXIS_X; int available_pixels = horizontal ? GetRect().w : GetRect().h; TBRect rect; if (m_max - m_min > 0) { PreferredSize ps = m_handle.GetPreferredSize(); int handle_pixels = horizontal ? ps.pref_w : ps.pref_h; m_to_pixel_factor = (double)(available_pixels - handle_pixels) / (m_max - m_min)/*+ 0.5*/; int pixel_pos = (int)((m_value - m_min) * m_to_pixel_factor); if (horizontal) rect.Set(pixel_pos, (GetRect().h - ps.pref_h) / 2, ps.pref_w, ps.pref_h); else rect.Set((GetRect().w - ps.pref_w) / 2, GetRect().h - handle_pixels - pixel_pos, ps.pref_w, ps.pref_h); } else m_to_pixel_factor = 0; m_handle.SetRect(rect); }
void TBScrollBar::UpdateHandle() { // Calculate the mover size and position bool horizontal = m_axis == AXIS_X; int available_pixels = horizontal ? GetRect().w : GetRect().h; int min_thickness_pixels = MIN(GetRect().h, GetRect().w); int visible_pixels = available_pixels; if (m_max - m_min > 0 && m_visible > 0) { double visible_proportion = m_visible / (m_visible + m_max - m_min); visible_pixels = (int)(visible_proportion * available_pixels); // Limit the size of the indicator to the slider thickness so that it doesn't // become too tiny when the visible proportion is very small. visible_pixels = MAX(visible_pixels, min_thickness_pixels); m_to_pixel_factor = (double)(available_pixels - visible_pixels) / (m_max - m_min)/*+ 0.5*/; } else { m_to_pixel_factor = 0; // If we can't scroll anything, make the handle invisible visible_pixels = 0; } int pixel_pos = (int)(m_value * m_to_pixel_factor); TBRect rect; if (horizontal) rect.Set(pixel_pos, 0, visible_pixels, GetRect().h); else rect.Set(0, pixel_pos, GetRect().w, visible_pixels); m_handle.SetRect(rect); }