]> git.tdb.fi Git - libs/gltk.git/commitdiff
Move some more common slider code to the base class
authorMikko Rasa <tdb@tdb.fi>
Tue, 21 Jan 2014 20:53:24 +0000 (22:53 +0200)
committerMikko Rasa <tdb@tdb.fi>
Tue, 21 Jan 2014 20:53:24 +0000 (22:53 +0200)
source/hslider.cpp
source/hslider.h
source/slider.cpp
source/slider.h
source/vslider.cpp
source/vslider.h

index dcb7634da9e633b751df9315f36bc5c5088d7e1d..4fecfa43d61038dbba1888a3e88800cdb5967c30 100644 (file)
@@ -8,8 +8,7 @@
 namespace Msp {
 namespace GLtk {
 
-HSlider::HSlider():
-       slider_size(1)
+HSlider::HSlider()
 { }
 
 void HSlider::autosize_special(const Part &part, Geometry &ageom) const
@@ -47,15 +46,7 @@ void HSlider::rebuild_special(const Part &part)
 void HSlider::button_press(int x, int, unsigned btn)
 {
        if(btn==1 && max>min)
-       {
-               int sx = static_cast<int>((geom.w-slider_size)*(value-min)/(max-min));
-               if(x<sx)
-                       set_value(value-step*10);
-               else if(x>=static_cast<int>(sx+slider_size))
-                       set_value(value+step*10);
-               else
-                       start_drag(x);
-       }
+               click(x);
 }
 
 void HSlider::button_release(int, int, unsigned btn)
index 774c7369d418841d226afa833e3a9348ede6050a..f0cf8d3f74c4bcde906af010dda08cc237c318b1 100644 (file)
@@ -12,9 +12,6 @@ the current value of the widget.
 */
 class HSlider: public Slider
 {
-private:
-       unsigned slider_size;
-
 public:
        HSlider();
 
index 7123458c0e41b7268ce44b7c556543dfd2af9aa1..15621db5b9b06e8035523cf2a479d3549ab7e587 100644 (file)
@@ -9,7 +9,8 @@ Slider::Slider():
        value(0),
        step(0.1),
        dragging(false),
-       drag_area_size(0)
+       drag_area_size(0),
+       slider_size(1)
 { }
 
 void Slider::set_value(double v)
@@ -46,6 +47,17 @@ void Slider::set_step(double s)
        set_value(value);
 }
 
+void Slider::click(int p)
+{
+       int sp = static_cast<int>(drag_area_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);
+}
+
 void Slider::start_drag(int p)
 {
        dragging = true;
index 6612c619aaaea80ae2445cd548edd9d38fcd742e..a2395766a258d6182a04d69fef4a1ccd5ee7bcb7 100644 (file)
@@ -32,6 +32,7 @@ protected:
        double drag_start_pos;
        double drag_start_value;
        unsigned drag_area_size;
+       unsigned slider_size;
 
        Slider();
 
@@ -42,6 +43,7 @@ public:
        double get_value() const { return value; }
 
 protected:
+       void click(int);
        void start_drag(int);
        void drag(int);
        void end_drag();
index d32c57d1aa91b9d25c919f9d3f592b0dee9eb327..4776e674a809b679e9bb7c0af6df48e3b400d8df 100644 (file)
@@ -8,8 +8,7 @@
 namespace Msp {
 namespace GLtk {
 
-VSlider::VSlider():
-       slider_size(1)
+VSlider::VSlider()
 { }
 
 void VSlider::autosize_special(const Part &part, Geometry &ageom) const
@@ -47,15 +46,7 @@ void VSlider::rebuild_special(const Part &part)
 void VSlider::button_press(int, int y, unsigned btn)
 {
        if(btn==1 && max>min)
-       {
-               int sy = static_cast<int>((geom.h-slider_size)*(value-min)/(max-min));
-               if(y<sy)
-                       set_value(value-step*10);
-               else if(y>=static_cast<int>(sy+slider_size))
-                       set_value(value+step*10);
-               else
-                       start_drag(y);
-       }
+               click(y);
 }
 
 void VSlider::button_release(int, int, unsigned btn)
index ee12689ff0afb8b152e89ac10cae237b3c6a1b7f..987d83a1726d98440293af552b580ad7c6b56860 100644 (file)
@@ -8,9 +8,6 @@ namespace GLtk {
 
 class VSlider: public Slider
 {
-private:
-       unsigned slider_size;
-
 public:
        VSlider();