From a380303d852a3ad3f9b73ccbfbec4cf7f118123e Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 21 Jan 2014 23:00:16 +0200 Subject: [PATCH] Properly account for margins in sliders --- source/hslider.cpp | 10 ++++++++-- source/hslider.h | 3 +++ source/slider.cpp | 11 ++++++++--- source/slider.h | 1 + source/vslider.cpp | 10 ++++++++-- source/vslider.h | 3 +++ 6 files changed, 31 insertions(+), 7 deletions(-) diff --git a/source/hslider.cpp b/source/hslider.cpp index 4fecfa4..2af6f5a 100644 --- a/source/hslider.cpp +++ b/source/hslider.cpp @@ -8,7 +8,8 @@ namespace Msp { namespace GLtk { -HSlider::HSlider() +HSlider::HSlider(): + total_margin(0) { } void HSlider::autosize_special(const Part &part, Geometry &ageom) const @@ -63,7 +64,7 @@ void HSlider::pointer_motion(int x, int) void HSlider::on_geometry_change() { - drag_area_size = geom.w-slider_size; + drag_area_size = geom.w-total_margin; } void HSlider::on_style_change() @@ -72,7 +73,12 @@ void HSlider::on_style_change() return; if(const Part *slider_part = style->get_part("slider")) + { slider_size = slider_part->get_geometry().w; + const Sides &margin = slider_part->get_margin(); + drag_area_offset = margin.left; + total_margin = margin.left+margin.right; + } on_geometry_change(); } diff --git a/source/hslider.h b/source/hslider.h index f0cf8d3..0d840fe 100644 --- a/source/hslider.h +++ b/source/hslider.h @@ -12,6 +12,9 @@ the current value of the widget. */ class HSlider: public Slider { +private: + unsigned total_margin; + public: HSlider(); diff --git a/source/slider.cpp b/source/slider.cpp index 15621db..1e763c6 100644 --- a/source/slider.cpp +++ b/source/slider.cpp @@ -10,6 +10,7 @@ Slider::Slider(): step(0.1), dragging(false), drag_area_size(0), + drag_area_offset(0), slider_size(1) { } @@ -49,13 +50,17 @@ void Slider::set_step(double s) void Slider::click(int p) { - int sp = static_cast(drag_area_size*(value-min)/(max-min)); + p -= drag_area_offset; + if(p<0 || p>=static_cast(drag_area_size)) + return; + + int sp = static_cast((drag_area_size-slider_size)*(value-min)/(max-min)); if(p=static_cast(sp+slider_size)) set_value(value+step*10); else - start_drag(p); + start_drag(drag_area_offset+p); } void Slider::start_drag(int p) @@ -69,7 +74,7 @@ void Slider::start_drag(int p) void Slider::drag(int p) { if(max>min) - set_value(drag_start_value+(p-drag_start_pos)*(max-min)/drag_area_size); + set_value(drag_start_value+(p-drag_start_pos)*(max-min)/(drag_area_size-slider_size)); } void Slider::end_drag() diff --git a/source/slider.h b/source/slider.h index a239576..a95a6a5 100644 --- a/source/slider.h +++ b/source/slider.h @@ -32,6 +32,7 @@ protected: double drag_start_pos; double drag_start_value; unsigned drag_area_size; + unsigned drag_area_offset; unsigned slider_size; Slider(); diff --git a/source/vslider.cpp b/source/vslider.cpp index 4776e67..6bc1553 100644 --- a/source/vslider.cpp +++ b/source/vslider.cpp @@ -8,7 +8,8 @@ namespace Msp { namespace GLtk { -VSlider::VSlider() +VSlider::VSlider(): + total_margin(0) { } void VSlider::autosize_special(const Part &part, Geometry &ageom) const @@ -63,7 +64,7 @@ void VSlider::pointer_motion(int, int y) void VSlider::on_geometry_change() { - drag_area_size = geom.h-slider_size; + drag_area_size = geom.h-total_margin; } void VSlider::on_style_change() @@ -72,7 +73,12 @@ void VSlider::on_style_change() return; if(const Part *slider_part = style->get_part("slider")) + { slider_size = slider_part->get_geometry().h; + const Sides &margin = slider_part->get_margin(); + drag_area_offset = margin.bottom; + total_margin = margin.bottom+margin.top; + } on_geometry_change(); } diff --git a/source/vslider.h b/source/vslider.h index 987d83a..6086d3f 100644 --- a/source/vslider.h +++ b/source/vslider.h @@ -8,6 +8,9 @@ namespace GLtk { class VSlider: public Slider { +private: + unsigned total_margin; + public: VSlider(); -- 2.45.2