1 #ifndef SCROLL_CONTAINER_HPP
2 #define SCROLL_CONTAINER_HPP
3
4 #include "FormView.hpp"
5
6 class LayoutCtrl : public ParentCtrl
7 {
8 public:
SetMinSize(Size sz)9 virtual void SetMinSize(Size sz) { _min = sz; }
GetMinSize() const10 virtual Size GetMinSize() const { return IsNull(_min) ? Ctrl::GetMinSize() : _min; }
11
12 virtual void MouseWheel(Point p, int zdelta, dword keyflags);
13
LayoutCtrl()14 LayoutCtrl() { _min = Null; Offset = 10; pScrollBars = NULL; }
15
16 int Offset;
17 ScrollBars* pScrollBars;
18
19 private:
20 Size _min;
21 };
22
MouseWheel(Point p,int zdelta,dword keyflags)23 inline void LayoutCtrl::MouseWheel(Point p, int zdelta, dword keyflags)
24 {
25 if (!pScrollBars) return;
26 pScrollBars->SetLine(40, 40);
27 zdelta < 0 ? pScrollBars->LineDown() : pScrollBars->LineUp();
28 }
29
30 class ScrollContainer : public ParentCtrl
31 {
32 public:
33 typedef ScrollContainer CLASSNAME;
34
ScrollContainer()35 ScrollContainer()
36 {
37 _transparent = false;
38 Offset = 20;
39 MouseReceiver = NULL;
40 Clear();
41 scrollbars.WhenScroll = THISBACK(Scroll);
42 AddFrame(scrollbars.AutoHide(true));
43 SetFrame(ThinInsetFrame());
44 }
45
~ScrollContainer()46 ~ScrollContainer() { Clear(); }
47
48 virtual ScrollContainer& Set(FormView &p, Size sz = Null);
Get() const49 virtual FormView* Get() const { return pane; }
50 virtual void Clear(bool remove = true);
51
Paint(Draw & w)52 void Paint(Draw& w)
53 {
54 if (!_transparent)
55 w.DrawRect(GetRect(), _color);
56 }
57
AddToScroll(LayoutCtrl & ctrl)58 inline void AddToScroll(LayoutCtrl& ctrl)
59 {
60 ctrl.pScrollBars = &scrollbars;
61 }
62
LeftDown(Point p,dword keyflags)63 virtual void LeftDown(Point p, dword keyflags)
64 {
65 if (MouseReceiver)
66 MouseReceiver->LeftDown(Point(-1, -1), keyflags);
67 }
68
RightDown(Point p,dword keyflags)69 virtual void RightDown(Point p, dword keyflags)
70 {
71 if (MouseReceiver)
72 MouseReceiver->RightDown(Point(-1, -1), keyflags);
73 }
74
LeftDouble(Point p,dword keyflags)75 virtual void LeftDouble(Point p, dword keyflags) { LeftDown(p, keyflags); }
RightDouble(Point p,dword keyflags)76 virtual void RightDouble(Point p, dword keyflags) { RightDown(p, keyflags); }
77
78 virtual void Layout();
79
80 Ctrl* MouseReceiver;
81 Callback1<Point> WhenScroll;
82
83 int Offset;
Transparent(bool flag=false,Color c=White ())84 void Transparent(bool flag = false, Color c = White())
85 {
86 _color = c;
87 _transparent = flag;
88 flag ? TransparentBackPaint() : NoTransparent();
89 }
90
91 protected:
92 ScrollBars scrollbars;
93 Point psb;
94 Ptr<FormView> pane;
95 bool _transparent;
96 Color _color;
97
ChildRemoved(Ctrl * child)98 virtual void ChildRemoved(Ctrl *child) { if (child == pane) Clear(false); }
99 virtual void Scroll();
100 };
101
Layout()102 inline void ScrollContainer::Layout()
103 {
104 if (pane)
105 {
106 scrollbars.SetPage(pane->DeZoom(scrollbars.GetReducedViewSize()));
107 scrollbars.SetTotal(pane->GetFormSize() + Size(45, 45));
108 }
109 }
110
Scroll()111 inline void ScrollContainer::Scroll()
112 {
113 Point newpos = scrollbars;
114 Point p;
115 if (pane)
116 {
117 p = psb-newpos;
118 pane->SetPageRect(pane->GetPageRect().Offseted(p));
119 pane->Refresh();
120 pane->WhenChildAllPos();
121 }
122 psb = newpos;
123 WhenScroll(p);
124 }
125
Set(FormView & p,Size sz)126 inline ScrollContainer& ScrollContainer::Set(FormView &p, Size sz)
127 {
128 Clear(true);
129 if (!p.IsLayout())
130 return *this;
131 if (IsNull(sz))
132 sz = p.GetMinSize();
133 p.SetPageRect(Rect(Point(Offset, Offset), sz));
134 p.SizePos();
135 pane = &p;
136 Add(p);
137 Layout();
138 Scroll();
139 return *this;
140 }
141
Clear(bool remove)142 inline void ScrollContainer::Clear(bool remove)
143 {
144 if (remove && pane && pane->GetParent() == this)
145 ((Ctrl*)pane)->Remove();
146 pane = NULL;
147 psb = Point(0, 0);
148 scrollbars.SetX(0, 0, 0);
149 scrollbars.SetY(0, 0, 0);
150 }
151
152 #endif // .. SCROLL_CONTAINER_HPP
153