]> git.tdb.fi Git - libs/gltk.git/commitdiff
Add a page size feature to Slider
authorMikko Rasa <tdb@tdb.fi>
Mon, 23 Sep 2019 22:26:30 +0000 (01:26 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 23 Sep 2019 22:26:30 +0000 (01:26 +0300)
source/entry.cpp
source/list.cpp
source/slider.cpp
source/slider.h

index 83ed563298b793b919d31d1a7bc442b0d3b24e2d..4c1713a022306d4b7670ef3953be2a62457f85a9 100644 (file)
@@ -487,6 +487,7 @@ void Entry::check_view_range()
                signal_scroll_position_changed.emit(first_row);
 
        slider->set_range(0, scroll);
+       slider->set_page_size(visible_rows);
        slider->set_value(scroll-first_row);
 }
 
index 13a5619a2e4d96bcbb72d83d46b5874b9ae96309..a7185acab422eee81b86a2021bbf018d78c85073 100644 (file)
@@ -506,6 +506,7 @@ void List::check_view_range()
                first_row = max_scroll;
 
        slider.set_range(0, max_scroll);
+       slider.set_page_size(rows.size()-max_scroll);
        slider.set_value(max_scroll-first_row);
 }
 
index 8ff342d56a81646aa6fe1786c58fcaf39a816068..70798af9c24a814f262777d5dbc0064912cb2096 100644 (file)
@@ -14,9 +14,11 @@ Slider::Slider(Direction d):
        max(1),
        value(0),
        step(0.1),
+       page_size(0.25),
        dragging(false),
        drag_area_size(0),
        drag_area_offset(0),
+       slider_min_size(1),
        slider_size(1),
        total_margin(0)
 { }
@@ -55,6 +57,12 @@ void Slider::set_step(double s)
        set_value(value);
 }
 
+void Slider::set_page_size(double p)
+{
+       page_size = p;
+       mark_rebuild();
+}
+
 void Slider::autosize_special(const Part &part, Geometry &ageom) const
 {
        if(part.get_name()=="slider")
@@ -75,18 +83,30 @@ void Slider::rebuild_special(const Part &part)
                        return;
 
                float pos_fraction = ((max>min) ? (value-min)/(max-min) : 0);
+               float fill_fraction = ((max>min) ? page_size/(max-min+page_size) : 1);
 
                Alignment align = part.get_alignment();
+               Geometry sgeom = geom;
                if(dir==HORIZONTAL)
+               {
                        align.x = pos_fraction;
+                       sgeom.w = std::max(slider_min_size+total_margin, static_cast<unsigned>(sgeom.w*fill_fraction));
+               }
                else
+               {
                        align.y = pos_fraction;
+                       sgeom.h = std::max(slider_min_size+total_margin, static_cast<unsigned>(sgeom.h*fill_fraction));
+               }
+
+               sgeom.x = static_cast<unsigned>((geom.w-sgeom.w)*pos_fraction);
+               sgeom.y = static_cast<unsigned>((geom.h-sgeom.h)*pos_fraction);
 
                Geometry rgeom = part.get_geometry();
-               align.apply(rgeom, geom, part.get_margin());
+               align.apply(rgeom, sgeom, part.get_margin());
+               slider_size = (dir==HORIZONTAL ? rgeom.w : rgeom.h);
 
                GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
-               bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
+               bld.matrix() *= GL::Matrix::translation(sgeom.x+rgeom.x, sgeom.y+rgeom.y, 0);
                graphic->build(rgeom.w, rgeom.h, bld);
        }
 }
@@ -104,9 +124,9 @@ void Slider::button_press(int x, int y, unsigned btn)
 
        int sp = static_cast<int>((drag_area_size-slider_size)*(value-min)/(max-min));
        if(p<sp)
-               set_value(value-step*10);
+               set_value(value-page_size);
        else if(p>=static_cast<int>(sp+slider_size))
-               set_value(value+step*10);
+               set_value(value+page_size);
        else
        {
                dragging = true;
@@ -150,13 +170,13 @@ void Slider::on_style_change()
                const Sides &margin = slider_part->get_margin();
                if(dir==HORIZONTAL)
                {
-                       slider_size = pgeom.w;
+                       slider_min_size = pgeom.w;
                        drag_area_offset = margin.left;
                        total_margin = margin.left+margin.right;
                }
                else
                {
-                       slider_size = pgeom.h;
+                       slider_min_size = pgeom.h;
                        drag_area_offset = margin.bottom;
                        total_margin = margin.bottom+margin.top;
                }
@@ -171,6 +191,7 @@ Slider::Loader::Loader(Slider &s):
 {
        add("range", &Slider::min, &Slider::max);
        add("step",  &Slider::step);
+       add("page_size", &Slider::page_size);
        add("value", &Slider::value);
 }
 
index b6041af96480320c8fa5643a313c6cdb96958b69..4260564a9f9985d80a0a7dc9171ae506beda6eaa 100644 (file)
@@ -36,12 +36,14 @@ protected:
        double min, max;
        double value;
        double step;
+       double page_size;
 
        bool dragging;
        double drag_start_pos;
        double drag_start_value;
        unsigned drag_area_size;
        unsigned drag_area_offset;
+       unsigned slider_min_size;
        unsigned slider_size;
        unsigned total_margin;
 
@@ -51,6 +53,7 @@ public:
        void set_value(double);
        void set_range(double, double);
        void set_step(double);
+       void set_page_size(double);
        double get_value() const { return value; }
 
 protected: