Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
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 Go to previous message
jjacksonRIAB is currently offline  jjacksonRIAB
Messages: 220
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();
}


 
Read Message
Read Message
Read Message
Previous Topic: override TrayIcon::WindowProc
Next Topic: capture mouse and limit all events to window
Goto Forum:
  


Current Time: Thu Apr 25 21:24:01 CEST 2024

Total time taken to generate the page: 0.02436 seconds