Home » U++ Library support » U++ Widgets - General questions or Mixed problems » Problem adapting child Ctrl to scroller example code
Problem adapting child Ctrl to scroller example code [message #51500] |
Fri, 05 April 2019 09:35 |
jjacksonRIAB
Messages: 221 Registered: June 2011
|
Experienced Member |
|
|
I end up with some strange pileup of Ctrls at either end of the document if I scroll quickly. What am I doing wrong?
#include <CtrlLib/CtrlLib.h>
using namespace Upp;
int count = 0;
struct ChildTest : Ctrl {
int num=0;
ChildTest() {
SetFrame(BlackFrame());
num = count;
count++;
SetRect(0, 0, 200, 50);
}
void Paint(Draw& w) override {
LogPos lpos = GetPos();
int y = lpos.y.GetA();
auto sz = GetSize();
w.DrawRect(sz, Color{ 255, 0, 0 } );
String pos;
pos << "item: " << num << " x: " << lpos.x.GetA() << " y: " << lpos.y.GetA();
w.DrawText(10, 10, pos, Arial(14).Bold(), Color(0,0,0));
}
};
struct App : TopWindow {
ScrollBar sb;
int count;
Vector<ChildTest*> items;
int GetLineHeight() {
return 50;
}
virtual void Paint(Draw& w)
{
Size sz = GetSize();
w.DrawRect(sz, SWhite);
int fcy = GetLineHeight();
int i = sb / fcy;
int y = i * fcy - sb;
String sbPos;
sbPos << i;
while(i < count && y < sz.cy) {
auto item = items[i++];
item->SetRect(0, y, 200, 45);
y += fcy;
}
w.DrawText(200, 10, sbPos, Arial(14).Bold(), Color(0,0,0));
}
virtual void Layout()
{
sb.SetPage(GetSize().cy);
}
virtual void MouseWheel(Point, int zdelta, dword)
{
sb.Wheel(zdelta);
}
bool Key(dword key, int)
{
return sb.VertKey(key);
}
void SetCount(int n)
{
count = n;
sb.SetTotal(n * GetLineHeight());
}
~App() {
for(auto item : items) {
delete item;
}
}
App() {
Sizeable().Zoomable().BackPaint();
AddFrame(sb);
sb.WhenScroll = [=] { Refresh(); };
sb.SetLine(GetLineHeight());
for(int i = 0; i < 100; i++) {
ChildTest* ctrl = new ChildTest();
ctrl->SetRect(0, i * 50, 200, 45);
items.Add(ctrl);
AddChild(ctrl);
}
}
};
GUI_APP_MAIN
{
App app;
app.SetRect(0, 0, 250, 500);
app.SetCount(100);
app.Run();
}
|
|
|
Goto Forum:
Current Time: Mon May 13 11:08:53 CEST 2024
Total time taken to generate the page: 0.02744 seconds
|