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++ Core » Init a ctrl inside INITBLOCK
Init a ctrl inside INITBLOCK [message #35090] Wed, 11 January 2012 09:19 Go to next message
koldo is currently offline  koldo
Messages: 3354
Registered: August 2008
Senior Veteran
Hello all

I wanted to init a Ctrl inside INITBLOCK:

struct Tab1 : StaticRect {
...
}

INITBLOCK {
	RegisterExample("Basic", new Tab1);
}

It works but program look is weird:

index.php?t=getfile&id=3608&private=0

Is there a safe way to init a Ctrl inside INITBLOCK?
  • Attachment: dib.PNG
    (Size: 5.74KB, Downloaded 424 times)


Best regards
Iñaki
Re: Init a ctrl inside INITBLOCK [message #35094 is a reply to message #35090] Wed, 11 January 2012 10:39 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

I think it is because you are using some GUI stuff before it is initialized, like font sizes, styles etc. Safe solution should be to just make the RegisterExample() store the list of things to be initialized into some container. The actual initialization can then by done by separate function called in GUI_APP_MAIN or in the constructor of the application window.

Best regards,
Honza
Re: Init a ctrl inside INITBLOCK [message #35106 is a reply to message #35090] Thu, 12 January 2012 13:16 Go to previous messageGo to next message
mr_ped is currently offline  mr_ped
Messages: 825
Registered: November 2005
Location: Czech Republic - Praha
Experienced Contributor
"Is there a safe way to init a Ctrl inside INITBLOCK?"
no. Only after GUI is initialized.
Re: Init a ctrl inside INITBLOCK [message #35107 is a reply to message #35106] Thu, 12 January 2012 15:30 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3354
Registered: August 2008
Senior Veteran
Ok. Thank you.


Best regards
Iñaki
Re: Init a ctrl inside INITBLOCK [message #35108 is a reply to message #35107] Thu, 12 January 2012 15:50 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3354
Registered: August 2008
Senior Veteran
Hello Honza

I have successfully applied this:
Quote:

Safe solution should be to just make the RegisterExample() store the list of things to be initialized into some container. The actual initialization can then by done by separate function called in GUI_APP_MAIN or in the constructor of the application window.


Now code is like this:

struct MyExample : ParentCtrl {
	virtual void Init() = 0;
};

struct Tab1 : WithTab1<MyExample> {
	Tab1() {...}		// No GUI here
	virtual void Init() {
		CtrlLayout(*this);	
		...		// GUI here
	}
...
}

INITBLOCK {
	RegisterExample("Basic", new Tab1);
}

...

GUI_APP_MAIN
{
	for (int i = 0; i < Examples().GetCount(); ++i)
		Examples()[i].ctrl->Init();	// GUI loaded here

	ScatterCtrl_Demo().Run();
}


Best regards
Iñaki
Re: Init a ctrl inside INITBLOCK [message #35110 is a reply to message #35108] Thu, 12 January 2012 16:32 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Well, that looks like a valid solution too Smile

It never stops to amaze me how a simple task can be done in so many ways Smile

Honza
Re: Init a ctrl inside INITBLOCK [message #35111 is a reply to message #35090] Thu, 12 January 2012 18:23 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Just for the sake of completeness, here is another solution, based on templates:
template<class T>
inline Ctrl& Create() {
	return Single<T>();
};

typedef Ctrl& (*CreateFunc)();
GLOBAL_VAR(Vector<CreateFunc>,sRegisteredObjects);

template<class T>
void Register(){
	sRegisteredObjects().Add(Create<T>);
};

GUI_APP_MAIN{
	App a;
	for(int i=0;i<sRegisteredObjects().GetCount();i++){
		Ctrl& c = sRegisteredObjects()[i]();
		//do something with it...
	}
	a.Run();
};


INITBLOCK{
	Register<Button>();
	Register<EditString>();
};

It is not as simple as yours, but it is less demanding on the registered objects. Also, I'm pretty sure there is many other ways to do this Smile

Honza
Re: Init a ctrl inside INITBLOCK [message #35112 is a reply to message #35111] Thu, 12 January 2012 21:27 Go to previous message
koldo is currently offline  koldo
Messages: 3354
Registered: August 2008
Senior Veteran
Shocked

Best regards
Iñaki
Previous Topic: Date& operator++
Next Topic: random functions proposal
Goto Forum:
  


Current Time: Thu Mar 28 10:39:06 CET 2024

Total time taken to generate the page: 0.00829 seconds