]> git.tdb.fi Git - libs/gltk.git/commitdiff
Properly account for margins in sliders
authorMikko Rasa <tdb@tdb.fi>
Tue, 21 Jan 2014 21:00:16 +0000 (23:00 +0200)
committerMikko Rasa <tdb@tdb.fi>
Tue, 21 Jan 2014 21:00:16 +0000 (23:00 +0200)
source/hslider.cpp
source/hslider.h
source/slider.cpp
source/slider.h
source/vslider.cpp
source/vslider.h

index 4fecfa43d61038dbba1888a3e88800cdb5967c30..2af6f5a4a71a621574e253c37ee802d3010979c2 100644 (file)
@@ -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();
 }
index f0cf8d3f74c4bcde906af010dda08cc237c318b1..0d840fe961525ccab760bb9c9e4210a5ca6636a9 100644 (file)
@@ -12,6 +12,9 @@ the current value of the widget.
 */
 class HSlider: public Slider
 {
+private:
+       unsigned total_margin;
+
 public:
        HSlider();
 
index 15621db5b9b06e8035523cf2a479d3549ab7e587..1e763c639bf5309fba69d3a3ce3e680aba97eff1 100644 (file)
@@ -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<int>(drag_area_size*(value-min)/(max-min));
+       p -= drag_area_offset;
+       if(p<0 || p>=static_cast<int>(drag_area_size))
+               return;
+
+       int sp = static_cast<int>((drag_area_size-slider_size)*(value-min)/(max-min));
        if(p<sp)
                set_value(value-step*10);
        else if(p>=static_cast<int>(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()
index a2395766a258d6182a04d69fef4a1ccd5ee7bcb7..a95a6a57665fd905577d3e76f54afa76ef5313f1 100644 (file)
@@ -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();
index 4776e674a809b679e9bb7c0af6df48e3b400d8df..6bc1553ded3ad8df6f9e5e24eeeb48f582a1f3de 100644 (file)
@@ -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();
 }
index 987d83a1726d98440293af552b580ad7c6b56860..6086d3fcf3c6db791fa3ef6f4fa462b7accbdae5 100644 (file)
@@ -8,6 +8,9 @@ namespace GLtk {
 
 class VSlider: public Slider
 {
+private:
+       unsigned total_margin;
+
 public:
        VSlider();