From: Mikko Rasa Date: Mon, 23 Sep 2019 22:26:30 +0000 (+0300) Subject: Add a page size feature to Slider X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=5f73de83d46138faa2b4c1b0a725d0a705988388;p=libs%2Fgltk.git Add a page size feature to Slider --- diff --git a/source/entry.cpp b/source/entry.cpp index 83ed563..4c1713a 100644 --- a/source/entry.cpp +++ b/source/entry.cpp @@ -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); } diff --git a/source/list.cpp b/source/list.cpp index 13a5619..a7185ac 100644 --- a/source/list.cpp +++ b/source/list.cpp @@ -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); } diff --git a/source/slider.cpp b/source/slider.cpp index 8ff342d..70798af 100644 --- a/source/slider.cpp +++ b/source/slider.cpp @@ -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(sgeom.w*fill_fraction)); + } else + { align.y = pos_fraction; + sgeom.h = std::max(slider_min_size+total_margin, static_cast(sgeom.h*fill_fraction)); + } + + sgeom.x = static_cast((geom.w-sgeom.w)*pos_fraction); + sgeom.y = static_cast((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((drag_area_size-slider_size)*(value-min)/(max-min)); if(p=static_cast(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); } diff --git a/source/slider.h b/source/slider.h index b6041af..4260564 100644 --- a/source/slider.h +++ b/source/slider.h @@ -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: