1 #include "platform/wxwidgets/scrollbar.hpp"
2 #include "platform/wxwidgets/platform.hpp"
3 #include "library/minmax.hpp"
6 scroll_bar::scroll_bar(wxWindow
* parent
, wxWindowID id
, bool vertical
)
7 : wxScrollBar(parent
, id
, wxDefaultPosition
, wxDefaultSize
, vertical
? wxSB_VERTICAL
: wxSB_HORIZONTAL
)
10 Connect(wxEVT_SCROLL_THUMBTRACK
, wxScrollEventHandler(scroll_bar::on_scroll
), NULL
, this);
11 Connect(wxEVT_SCROLL_PAGEDOWN
, wxScrollEventHandler(scroll_bar::on_scroll
), NULL
, this);
12 Connect(wxEVT_SCROLL_PAGEUP
, wxScrollEventHandler(scroll_bar::on_scroll
), NULL
, this);
13 Connect(wxEVT_SCROLL_LINEDOWN
, wxScrollEventHandler(scroll_bar::on_scroll
), NULL
, this);
14 Connect(wxEVT_SCROLL_LINEUP
, wxScrollEventHandler(scroll_bar::on_scroll
), NULL
, this);
15 Connect(wxEVT_SCROLL_TOP
, wxScrollEventHandler(scroll_bar::on_scroll
), NULL
, this);
16 Connect(wxEVT_SCROLL_BOTTOM
, wxScrollEventHandler(scroll_bar::on_scroll
), NULL
, this);
23 scroll_bar::~scroll_bar()
27 void scroll_bar::set_page_size(unsigned _pagesize
)
30 if(!_pagesize
) _pagesize
= 1;
33 SetScrollbar(position
, pagesize
, range
, max(pagesize
- 1, 1U));
35 SetScrollbar(0, 0, 0, 0);
38 void scroll_bar::set_range(unsigned _range
)
41 if(pagesize
>= _range
)
43 else if(position
+ pagesize
> _range
)
44 position
= _range
- pagesize
;
47 SetScrollbar(position
, pagesize
, range
, max(pagesize
- 1, 1U));
49 SetScrollbar(0, 0, 0, 0);
52 void scroll_bar::set_position(unsigned _position
)
57 else if(_position
+ pagesize
> range
)
58 _position
= range
- pagesize
;
61 SetScrollbar(position
, pagesize
, range
, max(pagesize
- 1, 1U));
63 SetScrollbar(0, 0, 0, 0);
66 void scroll_bar::apply_delta(int delta
)
69 unsigned maxscroll
= range
- pagesize
;
72 unsigned newscroll
= position
+ delta
;
74 newscroll
= (delta
< 0) ? 0 : maxscroll
;
76 if(position
> maxscroll
)
79 SetScrollbar(position
, pagesize
, range
, max(pagesize
- 1, 1U));
81 SetScrollbar(0, 0, 0, 0);
85 void scroll_bar::apply_wheel(int wheel
, int wheelunit
, unsigned speed
)
87 if(!wheel
|| !wheelunit
)
90 while(wheelunit
&& scroll_acc
<= -wheelunit
) {
91 apply_delta(static_cast<int>(speed
));
92 scroll_acc
+= wheelunit
;
94 while(wheelunit
&& scroll_acc
>= wheelunit
) {
95 apply_delta(-static_cast<int>(speed
));
96 scroll_acc
-= wheelunit
;
100 unsigned scroll_bar::get_position()
105 void scroll_bar::set_handler(std::function
<void(scroll_bar
&)> cb
)
110 void scroll_bar::on_scroll(wxScrollEvent
& e
)
114 position
= GetThumbPosition();
115 if(pagesize
>= range
)
117 else if(position
+ pagesize
> range
)
118 position
= range
- pagesize
;