Home » U++ Library support » U++ Widgets - General questions or Mixed problems » Problem adapting child Ctrl to scroller example code
Re: Problem adapting child Ctrl to scroller example code [message #51501 is a reply to message #51500] |
Fri, 05 April 2019 10:23 |
|
mirek
Messages: 13984 Registered: November 2005
|
Ultimate Member |
|
|
Well, the reason is pretty obvious here - you are only setting new positions to a subset of widgets, so the rest of them stays where they are.
I have fixed it for you, being there I could not resist to "beautify" and "make more U++" it...
#include <CtrlLib/CtrlLib.h>
using namespace Upp;
struct ChildTest : Ctrl {
int num=0;
ChildTest(int num) : num(num) {
SetFrame(BlackFrame());
SetRect(0, 0, 200, 50);
}
void Paint(Draw& w) override {
LogPos lpos = GetPos();
int y = lpos.y.GetA();
Size sz = GetSize();
w.DrawRect(sz, AdjustIfDark(Color{ 255, 0, 0 }) );
String pos;
pos << "item: " << num << " x: " << lpos.x.GetA() << " y: " << lpos.y.GetA();
w.DrawText(Zx(10), Zy(10), pos, Arial(14).Bold());
}
};
struct App : TopWindow {
ScrollBar sb;
Array<ChildTest> items;
int GetLineHeight() {
return Zy(50);
}
void Sync() {
for(int i = 0; i < items.GetCount(); i++)
items[i].SetRect(0, i * GetLineHeight() - sb, Zx(200), Zy(45));
}
void Paint(Draw& w) override
{
Size sz = GetSize();
w.DrawRect(sz, SWhite());
}
void Layout() override
{
sb.SetPage(GetSize().cy);
}
void MouseWheel(Point, int zdelta, dword) override
{
sb.Wheel(zdelta);
}
bool Key(dword key, int) override
{
return sb.VertKey(key);
}
App() {
Sizeable().Zoomable(); // Backpaint is now default
for(int i = 0; i < 100; i++)
Add(items.Create<ChildTest>(i));
AddFrame(sb);
sb.WhenScroll = [=] { Sync(); };
sb.SetLine(GetLineHeight());
sb.SetTotal(items.GetCount() * GetLineHeight());
Sync();
}
};
GUI_APP_MAIN
{
App app;
app.SetRect(0, 0, Zx(250), Zy(500));
app.Run();
}
Most important changes: Doing that in Paint is ugly, using 'new' is shunned upon. And you better make your app ready for UHD...
|
|
|
Goto Forum:
Current Time: Thu Jun 06 23:05:12 CEST 2024
Total time taken to generate the page: 0.02253 seconds
|